[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; Inst : Entity_Id := Empty);
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 -- If Inst is present, it is the entity of the package instance. This
835 -- entity is marked as having a limited_view actual when some actual is
836 -- a limited view. This is used to place the instance body properly.
837
838 procedure Remove_Parent (In_Body : Boolean := False);
839 -- Reverse effect after instantiation of child is complete
840
841 procedure Restore_Hidden_Primitives (Prims_List : in out Elist_Id);
842 -- Restore suffix 'P' to primitives of Prims_List and leave Prims_List
843 -- set to No_Elist.
844
845 procedure Save_Global_References_In_Aspects (N : Node_Id);
846 -- Save all global references found within the expressions of all aspects
847 -- that appear on node N.
848
849 procedure Set_Instance_Env
850 (Gen_Unit : Entity_Id;
851 Act_Unit : Entity_Id);
852 -- Save current instance on saved environment, to be used to determine
853 -- the global status of entities in nested instances. Part of Save_Env.
854 -- called after verifying that the generic unit is legal for the instance,
855 -- The procedure also examines whether the generic unit is a predefined
856 -- unit, in order to set configuration switches accordingly. As a result
857 -- the procedure must be called after analyzing and freezing the actuals.
858
859 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id);
860 -- Associate analyzed generic parameter with corresponding instance. Used
861 -- for semantic checks at instantiation time.
862
863 function True_Parent (N : Node_Id) return Node_Id;
864 -- For a subunit, return parent of corresponding stub, else return
865 -- parent of node.
866
867 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id);
868 -- Verify that an attribute that appears as the default for a formal
869 -- subprogram is a function or procedure with the correct profile.
870
871 -------------------------------------------
872 -- Data Structures for Generic Renamings --
873 -------------------------------------------
874
875 -- The map Generic_Renamings associates generic entities with their
876 -- corresponding actuals. Currently used to validate type instances. It
877 -- will eventually be used for all generic parameters to eliminate the
878 -- need for overload resolution in the instance.
879
880 type Assoc_Ptr is new Int;
881
882 Assoc_Null : constant Assoc_Ptr := -1;
883
884 type Assoc is record
885 Gen_Id : Entity_Id;
886 Act_Id : Entity_Id;
887 Next_In_HTable : Assoc_Ptr;
888 end record;
889
890 package Generic_Renamings is new Table.Table
891 (Table_Component_Type => Assoc,
892 Table_Index_Type => Assoc_Ptr,
893 Table_Low_Bound => 0,
894 Table_Initial => 10,
895 Table_Increment => 100,
896 Table_Name => "Generic_Renamings");
897
898 -- Variable to hold enclosing instantiation. When the environment is
899 -- saved for a subprogram inlining, the corresponding Act_Id is empty.
900
901 Current_Instantiated_Parent : Assoc := (Empty, Empty, Assoc_Null);
902
903 -- Hash table for associations
904
905 HTable_Size : constant := 37;
906 type HTable_Range is range 0 .. HTable_Size - 1;
907
908 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr);
909 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr;
910 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id;
911 function Hash (F : Entity_Id) return HTable_Range;
912
913 package Generic_Renamings_HTable is new GNAT.HTable.Static_HTable (
914 Header_Num => HTable_Range,
915 Element => Assoc,
916 Elmt_Ptr => Assoc_Ptr,
917 Null_Ptr => Assoc_Null,
918 Set_Next => Set_Next_Assoc,
919 Next => Next_Assoc,
920 Key => Entity_Id,
921 Get_Key => Get_Gen_Id,
922 Hash => Hash,
923 Equal => "=");
924
925 Exchanged_Views : Elist_Id;
926 -- This list holds the private views that have been exchanged during
927 -- instantiation to restore the visibility of the generic declaration.
928 -- (see comments above). After instantiation, the current visibility is
929 -- reestablished by means of a traversal of this list.
930
931 Hidden_Entities : Elist_Id;
932 -- This list holds the entities of the current scope that are removed
933 -- from immediate visibility when instantiating a child unit. Their
934 -- visibility is restored in Remove_Parent.
935
936 -- Because instantiations can be recursive, the following must be saved
937 -- on entry and restored on exit from an instantiation (spec or body).
938 -- This is done by the two procedures Save_Env and Restore_Env. For
939 -- package and subprogram instantiations (but not for the body instances)
940 -- the action of Save_Env is done in two steps: Init_Env is called before
941 -- Check_Generic_Child_Unit, because setting the parent instances requires
942 -- that the visibility data structures be properly initialized. Once the
943 -- generic is unit is validated, Set_Instance_Env completes Save_Env.
944
945 Parent_Unit_Visible : Boolean := False;
946 -- Parent_Unit_Visible is used when the generic is a child unit, and
947 -- indicates whether the ultimate parent of the generic is visible in the
948 -- instantiation environment. It is used to reset the visibility of the
949 -- parent at the end of the instantiation (see Remove_Parent).
950
951 Instance_Parent_Unit : Entity_Id := Empty;
952 -- This records the ultimate parent unit of an instance of a generic
953 -- child unit and is used in conjunction with Parent_Unit_Visible to
954 -- indicate the unit to which the Parent_Unit_Visible flag corresponds.
955
956 type Instance_Env is record
957 Instantiated_Parent : Assoc;
958 Exchanged_Views : Elist_Id;
959 Hidden_Entities : Elist_Id;
960 Current_Sem_Unit : Unit_Number_Type;
961 Parent_Unit_Visible : Boolean := False;
962 Instance_Parent_Unit : Entity_Id := Empty;
963 Switches : Config_Switches_Type;
964 end record;
965
966 package Instance_Envs is new Table.Table (
967 Table_Component_Type => Instance_Env,
968 Table_Index_Type => Int,
969 Table_Low_Bound => 0,
970 Table_Initial => 32,
971 Table_Increment => 100,
972 Table_Name => "Instance_Envs");
973
974 procedure Restore_Private_Views
975 (Pack_Id : Entity_Id;
976 Is_Package : Boolean := True);
977 -- Restore the private views of external types, and unmark the generic
978 -- renamings of actuals, so that they become compatible subtypes again.
979 -- For subprograms, Pack_Id is the package constructed to hold the
980 -- renamings.
981
982 procedure Switch_View (T : Entity_Id);
983 -- Switch the partial and full views of a type and its private
984 -- dependents (i.e. its subtypes and derived types).
985
986 ------------------------------------
987 -- Structures for Error Reporting --
988 ------------------------------------
989
990 Instantiation_Node : Node_Id;
991 -- Used by subprograms that validate instantiation of formal parameters
992 -- where there might be no actual on which to place the error message.
993 -- Also used to locate the instantiation node for generic subunits.
994
995 Instantiation_Error : exception;
996 -- When there is a semantic error in the generic parameter matching,
997 -- there is no point in continuing the instantiation, because the
998 -- number of cascaded errors is unpredictable. This exception aborts
999 -- the instantiation process altogether.
1000
1001 S_Adjustment : Sloc_Adjustment;
1002 -- Offset created for each node in an instantiation, in order to keep
1003 -- track of the source position of the instantiation in each of its nodes.
1004 -- A subsequent semantic error or warning on a construct of the instance
1005 -- points to both places: the original generic node, and the point of
1006 -- instantiation. See Sinput and Sinput.L for additional details.
1007
1008 ------------------------------------------------------------
1009 -- Data structure for keeping track when inside a Generic --
1010 ------------------------------------------------------------
1011
1012 -- The following table is used to save values of the Inside_A_Generic
1013 -- flag (see spec of Sem) when they are saved by Start_Generic.
1014
1015 package Generic_Flags is new Table.Table (
1016 Table_Component_Type => Boolean,
1017 Table_Index_Type => Int,
1018 Table_Low_Bound => 0,
1019 Table_Initial => 32,
1020 Table_Increment => 200,
1021 Table_Name => "Generic_Flags");
1022
1023 ---------------------------
1024 -- Abandon_Instantiation --
1025 ---------------------------
1026
1027 procedure Abandon_Instantiation (N : Node_Id) is
1028 begin
1029 Error_Msg_N ("\instantiation abandoned!", N);
1030 raise Instantiation_Error;
1031 end Abandon_Instantiation;
1032
1033 --------------------------
1034 -- Analyze_Associations --
1035 --------------------------
1036
1037 function Analyze_Associations
1038 (I_Node : Node_Id;
1039 Formals : List_Id;
1040 F_Copy : List_Id) return List_Id
1041 is
1042 Actuals_To_Freeze : constant Elist_Id := New_Elmt_List;
1043 Assoc : constant List_Id := New_List;
1044 Default_Actuals : constant List_Id := New_List;
1045 Gen_Unit : constant Entity_Id :=
1046 Defining_Entity (Parent (F_Copy));
1047
1048 Actuals : List_Id;
1049 Actual : Node_Id;
1050 Analyzed_Formal : Node_Id;
1051 First_Named : Node_Id := Empty;
1052 Formal : Node_Id;
1053 Match : Node_Id;
1054 Named : Node_Id;
1055 Saved_Formal : Node_Id;
1056
1057 Default_Formals : constant List_Id := New_List;
1058 -- If an Others_Choice is present, some of the formals may be defaulted.
1059 -- To simplify the treatment of visibility in an instance, we introduce
1060 -- individual defaults for each such formal. These defaults are
1061 -- appended to the list of associations and replace the Others_Choice.
1062
1063 Found_Assoc : Node_Id;
1064 -- Association for the current formal being match. Empty if there are
1065 -- no remaining actuals, or if there is no named association with the
1066 -- name of the formal.
1067
1068 Is_Named_Assoc : Boolean;
1069 Num_Matched : Int := 0;
1070 Num_Actuals : Int := 0;
1071
1072 Others_Present : Boolean := False;
1073 Others_Choice : Node_Id := Empty;
1074 -- In Ada 2005, indicates partial parameterization of a formal
1075 -- package. As usual an other association must be last in the list.
1076
1077 procedure Check_Overloaded_Formal_Subprogram (Formal : Entity_Id);
1078 -- Apply RM 12.3(9): if a formal subprogram is overloaded, the instance
1079 -- cannot have a named association for it. AI05-0025 extends this rule
1080 -- to formals of formal packages by AI05-0025, and it also applies to
1081 -- box-initialized formals.
1082
1083 function Has_Fully_Defined_Profile (Subp : Entity_Id) return Boolean;
1084 -- Determine whether the parameter types and the return type of Subp
1085 -- are fully defined at the point of instantiation.
1086
1087 function Matching_Actual
1088 (F : Entity_Id;
1089 A_F : Entity_Id) return Node_Id;
1090 -- Find actual that corresponds to a given a formal parameter. If the
1091 -- actuals are positional, return the next one, if any. If the actuals
1092 -- are named, scan the parameter associations to find the right one.
1093 -- A_F is the corresponding entity in the analyzed generic,which is
1094 -- placed on the selector name for ASIS use.
1095 --
1096 -- In Ada 2005, a named association may be given with a box, in which
1097 -- case Matching_Actual sets Found_Assoc to the generic association,
1098 -- but return Empty for the actual itself. In this case the code below
1099 -- creates a corresponding declaration for the formal.
1100
1101 function Partial_Parameterization return Boolean;
1102 -- Ada 2005: if no match is found for a given formal, check if the
1103 -- association for it includes a box, or whether the associations
1104 -- include an Others clause.
1105
1106 procedure Process_Default (F : Entity_Id);
1107 -- Add a copy of the declaration of generic formal F to the list of
1108 -- associations, and add an explicit box association for F if there
1109 -- is none yet, and the default comes from an Others_Choice.
1110
1111 function Renames_Standard_Subprogram (Subp : Entity_Id) return Boolean;
1112 -- Determine whether Subp renames one of the subprograms defined in the
1113 -- generated package Standard.
1114
1115 procedure Set_Analyzed_Formal;
1116 -- Find the node in the generic copy that corresponds to a given formal.
1117 -- The semantic information on this node is used to perform legality
1118 -- checks on the actuals. Because semantic analysis can introduce some
1119 -- anonymous entities or modify the declaration node itself, the
1120 -- correspondence between the two lists is not one-one. In addition to
1121 -- anonymous types, the presence a formal equality will introduce an
1122 -- implicit declaration for the corresponding inequality.
1123
1124 ----------------------------------------
1125 -- Check_Overloaded_Formal_Subprogram --
1126 ----------------------------------------
1127
1128 procedure Check_Overloaded_Formal_Subprogram (Formal : Entity_Id) is
1129 Temp_Formal : Entity_Id;
1130
1131 begin
1132 Temp_Formal := First (Formals);
1133 while Present (Temp_Formal) loop
1134 if Nkind (Temp_Formal) in N_Formal_Subprogram_Declaration
1135 and then Temp_Formal /= Formal
1136 and then
1137 Chars (Defining_Unit_Name (Specification (Formal))) =
1138 Chars (Defining_Unit_Name (Specification (Temp_Formal)))
1139 then
1140 if Present (Found_Assoc) then
1141 Error_Msg_N
1142 ("named association not allowed for overloaded formal",
1143 Found_Assoc);
1144
1145 else
1146 Error_Msg_N
1147 ("named association not allowed for overloaded formal",
1148 Others_Choice);
1149 end if;
1150
1151 Abandon_Instantiation (Instantiation_Node);
1152 end if;
1153
1154 Next (Temp_Formal);
1155 end loop;
1156 end Check_Overloaded_Formal_Subprogram;
1157
1158 -------------------------------
1159 -- Has_Fully_Defined_Profile --
1160 -------------------------------
1161
1162 function Has_Fully_Defined_Profile (Subp : Entity_Id) return Boolean is
1163 function Is_Fully_Defined_Type (Typ : Entity_Id) return Boolean;
1164 -- Determine whethet type Typ is fully defined
1165
1166 ---------------------------
1167 -- Is_Fully_Defined_Type --
1168 ---------------------------
1169
1170 function Is_Fully_Defined_Type (Typ : Entity_Id) return Boolean is
1171 begin
1172 -- A private type without a full view is not fully defined
1173
1174 if Is_Private_Type (Typ)
1175 and then No (Full_View (Typ))
1176 then
1177 return False;
1178
1179 -- An incomplete type is never fully defined
1180
1181 elsif Is_Incomplete_Type (Typ) then
1182 return False;
1183
1184 -- All other types are fully defined
1185
1186 else
1187 return True;
1188 end if;
1189 end Is_Fully_Defined_Type;
1190
1191 -- Local declarations
1192
1193 Param : Entity_Id;
1194
1195 -- Start of processing for Has_Fully_Defined_Profile
1196
1197 begin
1198 -- Check the parameters
1199
1200 Param := First_Formal (Subp);
1201 while Present (Param) loop
1202 if not Is_Fully_Defined_Type (Etype (Param)) then
1203 return False;
1204 end if;
1205
1206 Next_Formal (Param);
1207 end loop;
1208
1209 -- Check the return type
1210
1211 return Is_Fully_Defined_Type (Etype (Subp));
1212 end Has_Fully_Defined_Profile;
1213
1214 ---------------------
1215 -- Matching_Actual --
1216 ---------------------
1217
1218 function Matching_Actual
1219 (F : Entity_Id;
1220 A_F : Entity_Id) return Node_Id
1221 is
1222 Prev : Node_Id;
1223 Act : Node_Id;
1224
1225 begin
1226 Is_Named_Assoc := False;
1227
1228 -- End of list of purely positional parameters
1229
1230 if No (Actual) or else Nkind (Actual) = N_Others_Choice then
1231 Found_Assoc := Empty;
1232 Act := Empty;
1233
1234 -- Case of positional parameter corresponding to current formal
1235
1236 elsif No (Selector_Name (Actual)) then
1237 Found_Assoc := Actual;
1238 Act := Explicit_Generic_Actual_Parameter (Actual);
1239 Num_Matched := Num_Matched + 1;
1240 Next (Actual);
1241
1242 -- Otherwise scan list of named actuals to find the one with the
1243 -- desired name. All remaining actuals have explicit names.
1244
1245 else
1246 Is_Named_Assoc := True;
1247 Found_Assoc := Empty;
1248 Act := Empty;
1249 Prev := Empty;
1250
1251 while Present (Actual) loop
1252 if Chars (Selector_Name (Actual)) = Chars (F) then
1253 Set_Entity (Selector_Name (Actual), A_F);
1254 Set_Etype (Selector_Name (Actual), Etype (A_F));
1255 Generate_Reference (A_F, Selector_Name (Actual));
1256 Found_Assoc := Actual;
1257 Act := Explicit_Generic_Actual_Parameter (Actual);
1258 Num_Matched := Num_Matched + 1;
1259 exit;
1260 end if;
1261
1262 Prev := Actual;
1263 Next (Actual);
1264 end loop;
1265
1266 -- Reset for subsequent searches. In most cases the named
1267 -- associations are in order. If they are not, we reorder them
1268 -- to avoid scanning twice the same actual. This is not just a
1269 -- question of efficiency: there may be multiple defaults with
1270 -- boxes that have the same name. In a nested instantiation we
1271 -- insert actuals for those defaults, and cannot rely on their
1272 -- names to disambiguate them.
1273
1274 if Actual = First_Named then
1275 Next (First_Named);
1276
1277 elsif Present (Actual) then
1278 Insert_Before (First_Named, Remove_Next (Prev));
1279 end if;
1280
1281 Actual := First_Named;
1282 end if;
1283
1284 if Is_Entity_Name (Act) and then Present (Entity (Act)) then
1285 Set_Used_As_Generic_Actual (Entity (Act));
1286 end if;
1287
1288 return Act;
1289 end Matching_Actual;
1290
1291 ------------------------------
1292 -- Partial_Parameterization --
1293 ------------------------------
1294
1295 function Partial_Parameterization return Boolean is
1296 begin
1297 return Others_Present
1298 or else (Present (Found_Assoc) and then Box_Present (Found_Assoc));
1299 end Partial_Parameterization;
1300
1301 ---------------------
1302 -- Process_Default --
1303 ---------------------
1304
1305 procedure Process_Default (F : Entity_Id) is
1306 Loc : constant Source_Ptr := Sloc (I_Node);
1307 F_Id : constant Entity_Id := Defining_Entity (F);
1308 Decl : Node_Id;
1309 Default : Node_Id;
1310 Id : Entity_Id;
1311
1312 begin
1313 -- Append copy of formal declaration to associations, and create new
1314 -- defining identifier for it.
1315
1316 Decl := New_Copy_Tree (F);
1317 Id := Make_Defining_Identifier (Sloc (F_Id), Chars (F_Id));
1318
1319 if Nkind (F) in N_Formal_Subprogram_Declaration then
1320 Set_Defining_Unit_Name (Specification (Decl), Id);
1321
1322 else
1323 Set_Defining_Identifier (Decl, Id);
1324 end if;
1325
1326 Append (Decl, Assoc);
1327
1328 if No (Found_Assoc) then
1329 Default :=
1330 Make_Generic_Association (Loc,
1331 Selector_Name =>
1332 New_Occurrence_Of (Id, Loc),
1333 Explicit_Generic_Actual_Parameter => Empty);
1334 Set_Box_Present (Default);
1335 Append (Default, Default_Formals);
1336 end if;
1337 end Process_Default;
1338
1339 ---------------------------------
1340 -- Renames_Standard_Subprogram --
1341 ---------------------------------
1342
1343 function Renames_Standard_Subprogram (Subp : Entity_Id) return Boolean is
1344 Id : Entity_Id;
1345
1346 begin
1347 Id := Alias (Subp);
1348 while Present (Id) loop
1349 if Scope (Id) = Standard_Standard then
1350 return True;
1351 end if;
1352
1353 Id := Alias (Id);
1354 end loop;
1355
1356 return False;
1357 end Renames_Standard_Subprogram;
1358
1359 -------------------------
1360 -- Set_Analyzed_Formal --
1361 -------------------------
1362
1363 procedure Set_Analyzed_Formal is
1364 Kind : Node_Kind;
1365
1366 begin
1367 while Present (Analyzed_Formal) loop
1368 Kind := Nkind (Analyzed_Formal);
1369
1370 case Nkind (Formal) is
1371
1372 when N_Formal_Subprogram_Declaration =>
1373 exit when Kind in N_Formal_Subprogram_Declaration
1374 and then
1375 Chars
1376 (Defining_Unit_Name (Specification (Formal))) =
1377 Chars
1378 (Defining_Unit_Name (Specification (Analyzed_Formal)));
1379
1380 when N_Formal_Package_Declaration =>
1381 exit when Nkind_In (Kind, N_Formal_Package_Declaration,
1382 N_Generic_Package_Declaration,
1383 N_Package_Declaration);
1384
1385 when N_Use_Package_Clause | N_Use_Type_Clause => exit;
1386
1387 when others =>
1388
1389 -- Skip freeze nodes, and nodes inserted to replace
1390 -- unrecognized pragmas.
1391
1392 exit when
1393 Kind not in N_Formal_Subprogram_Declaration
1394 and then not Nkind_In (Kind, N_Subprogram_Declaration,
1395 N_Freeze_Entity,
1396 N_Null_Statement,
1397 N_Itype_Reference)
1398 and then Chars (Defining_Identifier (Formal)) =
1399 Chars (Defining_Identifier (Analyzed_Formal));
1400 end case;
1401
1402 Next (Analyzed_Formal);
1403 end loop;
1404 end Set_Analyzed_Formal;
1405
1406 -- Start of processing for Analyze_Associations
1407
1408 begin
1409 Actuals := Generic_Associations (I_Node);
1410
1411 if Present (Actuals) then
1412
1413 -- Check for an Others choice, indicating a partial parameterization
1414 -- for a formal package.
1415
1416 Actual := First (Actuals);
1417 while Present (Actual) loop
1418 if Nkind (Actual) = N_Others_Choice then
1419 Others_Present := True;
1420 Others_Choice := Actual;
1421
1422 if Present (Next (Actual)) then
1423 Error_Msg_N ("others must be last association", Actual);
1424 end if;
1425
1426 -- This subprogram is used both for formal packages and for
1427 -- instantiations. For the latter, associations must all be
1428 -- explicit.
1429
1430 if Nkind (I_Node) /= N_Formal_Package_Declaration
1431 and then Comes_From_Source (I_Node)
1432 then
1433 Error_Msg_N
1434 ("others association not allowed in an instance",
1435 Actual);
1436 end if;
1437
1438 -- In any case, nothing to do after the others association
1439
1440 exit;
1441
1442 elsif Box_Present (Actual)
1443 and then Comes_From_Source (I_Node)
1444 and then Nkind (I_Node) /= N_Formal_Package_Declaration
1445 then
1446 Error_Msg_N
1447 ("box association not allowed in an instance", Actual);
1448 end if;
1449
1450 Next (Actual);
1451 end loop;
1452
1453 -- If named associations are present, save first named association
1454 -- (it may of course be Empty) to facilitate subsequent name search.
1455
1456 First_Named := First (Actuals);
1457 while Present (First_Named)
1458 and then Nkind (First_Named) /= N_Others_Choice
1459 and then No (Selector_Name (First_Named))
1460 loop
1461 Num_Actuals := Num_Actuals + 1;
1462 Next (First_Named);
1463 end loop;
1464 end if;
1465
1466 Named := First_Named;
1467 while Present (Named) loop
1468 if Nkind (Named) /= N_Others_Choice
1469 and then No (Selector_Name (Named))
1470 then
1471 Error_Msg_N ("invalid positional actual after named one", Named);
1472 Abandon_Instantiation (Named);
1473 end if;
1474
1475 -- A named association may lack an actual parameter, if it was
1476 -- introduced for a default subprogram that turns out to be local
1477 -- to the outer instantiation.
1478
1479 if Nkind (Named) /= N_Others_Choice
1480 and then Present (Explicit_Generic_Actual_Parameter (Named))
1481 then
1482 Num_Actuals := Num_Actuals + 1;
1483 end if;
1484
1485 Next (Named);
1486 end loop;
1487
1488 if Present (Formals) then
1489 Formal := First_Non_Pragma (Formals);
1490 Analyzed_Formal := First_Non_Pragma (F_Copy);
1491
1492 if Present (Actuals) then
1493 Actual := First (Actuals);
1494
1495 -- All formals should have default values
1496
1497 else
1498 Actual := Empty;
1499 end if;
1500
1501 while Present (Formal) loop
1502 Set_Analyzed_Formal;
1503 Saved_Formal := Next_Non_Pragma (Formal);
1504
1505 case Nkind (Formal) is
1506 when N_Formal_Object_Declaration =>
1507 Match :=
1508 Matching_Actual
1509 (Defining_Identifier (Formal),
1510 Defining_Identifier (Analyzed_Formal));
1511
1512 if No (Match) and then Partial_Parameterization then
1513 Process_Default (Formal);
1514
1515 else
1516 Append_List
1517 (Instantiate_Object (Formal, Match, Analyzed_Formal),
1518 Assoc);
1519
1520 -- For a defaulted in_parameter, create an entry in the
1521 -- the list of defaulted actuals, for GNATProve use. Do
1522 -- not included these defaults for an instance nested
1523 -- within a generic, because the defaults are also used
1524 -- in the analysis of the enclosing generic, and only
1525 -- defaulted subprograms are relevant there.
1526
1527 if No (Match) and then not Inside_A_Generic then
1528 Append_To (Default_Actuals,
1529 Make_Generic_Association (Sloc (I_Node),
1530 Selector_Name =>
1531 New_Occurrence_Of
1532 (Defining_Identifier (Formal), Sloc (I_Node)),
1533 Explicit_Generic_Actual_Parameter =>
1534 New_Copy_Tree (Default_Expression (Formal))));
1535 end if;
1536 end if;
1537
1538 -- If the object is a call to an expression function, this
1539 -- is a freezing point for it.
1540
1541 if Is_Entity_Name (Match)
1542 and then Present (Entity (Match))
1543 and then Nkind
1544 (Original_Node (Unit_Declaration_Node (Entity (Match))))
1545 = N_Expression_Function
1546 then
1547 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1548 end if;
1549
1550 when N_Formal_Type_Declaration =>
1551 Match :=
1552 Matching_Actual
1553 (Defining_Identifier (Formal),
1554 Defining_Identifier (Analyzed_Formal));
1555
1556 if No (Match) then
1557 if Partial_Parameterization then
1558 Process_Default (Formal);
1559
1560 else
1561 Error_Msg_Sloc := Sloc (Gen_Unit);
1562 Error_Msg_NE
1563 ("missing actual&",
1564 Instantiation_Node, Defining_Identifier (Formal));
1565 Error_Msg_NE
1566 ("\in instantiation of & declared#",
1567 Instantiation_Node, Gen_Unit);
1568 Abandon_Instantiation (Instantiation_Node);
1569 end if;
1570
1571 else
1572 Analyze (Match);
1573 Append_List
1574 (Instantiate_Type
1575 (Formal, Match, Analyzed_Formal, Assoc),
1576 Assoc);
1577
1578 -- An instantiation is a freeze point for the actuals,
1579 -- unless this is a rewritten formal package, or the
1580 -- formal is an Ada 2012 formal incomplete type.
1581
1582 if Nkind (I_Node) = N_Formal_Package_Declaration
1583 or else
1584 (Ada_Version >= Ada_2012
1585 and then
1586 Ekind (Defining_Identifier (Analyzed_Formal)) =
1587 E_Incomplete_Type)
1588 then
1589 null;
1590
1591 else
1592 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1593 end if;
1594 end if;
1595
1596 -- A remote access-to-class-wide type is not a legal actual
1597 -- for a generic formal of an access type (E.2.2(17/2)).
1598 -- In GNAT an exception to this rule is introduced when
1599 -- the formal is marked as remote using implementation
1600 -- defined aspect/pragma Remote_Access_Type. In that case
1601 -- the actual must be remote as well.
1602
1603 -- If the current instantiation is the construction of a
1604 -- local copy for a formal package the actuals may be
1605 -- defaulted, and there is no matching actual to check.
1606
1607 if Nkind (Analyzed_Formal) = N_Formal_Type_Declaration
1608 and then
1609 Nkind (Formal_Type_Definition (Analyzed_Formal)) =
1610 N_Access_To_Object_Definition
1611 and then Present (Match)
1612 then
1613 declare
1614 Formal_Ent : constant Entity_Id :=
1615 Defining_Identifier (Analyzed_Formal);
1616 begin
1617 if Is_Remote_Access_To_Class_Wide_Type (Entity (Match))
1618 = Is_Remote_Types (Formal_Ent)
1619 then
1620 -- Remoteness of formal and actual match
1621
1622 null;
1623
1624 elsif Is_Remote_Types (Formal_Ent) then
1625
1626 -- Remote formal, non-remote actual
1627
1628 Error_Msg_NE
1629 ("actual for& must be remote", Match, Formal_Ent);
1630
1631 else
1632 -- Non-remote formal, remote actual
1633
1634 Error_Msg_NE
1635 ("actual for& may not be remote",
1636 Match, Formal_Ent);
1637 end if;
1638 end;
1639 end if;
1640
1641 when N_Formal_Subprogram_Declaration =>
1642 Match :=
1643 Matching_Actual
1644 (Defining_Unit_Name (Specification (Formal)),
1645 Defining_Unit_Name (Specification (Analyzed_Formal)));
1646
1647 -- If the formal subprogram has the same name as another
1648 -- formal subprogram of the generic, then a named
1649 -- association is illegal (12.3(9)). Exclude named
1650 -- associations that are generated for a nested instance.
1651
1652 if Present (Match)
1653 and then Is_Named_Assoc
1654 and then Comes_From_Source (Found_Assoc)
1655 then
1656 Check_Overloaded_Formal_Subprogram (Formal);
1657 end if;
1658
1659 -- If there is no corresponding actual, this may be case
1660 -- of partial parameterization, or else the formal has a
1661 -- default or a box.
1662
1663 if No (Match) and then Partial_Parameterization then
1664 Process_Default (Formal);
1665
1666 if Nkind (I_Node) = N_Formal_Package_Declaration then
1667 Check_Overloaded_Formal_Subprogram (Formal);
1668 end if;
1669
1670 else
1671 Append_To (Assoc,
1672 Instantiate_Formal_Subprogram
1673 (Formal, Match, Analyzed_Formal));
1674
1675 -- An instantiation is a freeze point for the actuals,
1676 -- unless this is a rewritten formal package.
1677
1678 if Nkind (I_Node) /= N_Formal_Package_Declaration
1679 and then Nkind (Match) = N_Identifier
1680 and then Is_Subprogram (Entity (Match))
1681
1682 -- The actual subprogram may rename a routine defined
1683 -- in Standard. Avoid freezing such renamings because
1684 -- subprograms coming from Standard cannot be frozen.
1685
1686 and then
1687 not Renames_Standard_Subprogram (Entity (Match))
1688
1689 -- If the actual subprogram comes from a different
1690 -- unit, it is already frozen, either by a body in
1691 -- that unit or by the end of the declarative part
1692 -- of the unit. This check avoids the freezing of
1693 -- subprograms defined in Standard which are used
1694 -- as generic actuals.
1695
1696 and then In_Same_Code_Unit (Entity (Match), I_Node)
1697 and then Has_Fully_Defined_Profile (Entity (Match))
1698 then
1699 -- Mark the subprogram as having a delayed freeze
1700 -- since this may be an out-of-order action.
1701
1702 Set_Has_Delayed_Freeze (Entity (Match));
1703 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1704 end if;
1705 end if;
1706
1707 -- If this is a nested generic, preserve default for later
1708 -- instantiations. We do this as well for GNATProve use,
1709 -- so that the list of generic associations is complete.
1710
1711 if No (Match) and then Box_Present (Formal) then
1712 declare
1713 Subp : constant Entity_Id :=
1714 Defining_Unit_Name (Specification (Last (Assoc)));
1715
1716 begin
1717 Append_To (Default_Actuals,
1718 Make_Generic_Association (Sloc (I_Node),
1719 Selector_Name =>
1720 New_Occurrence_Of (Subp, Sloc (I_Node)),
1721 Explicit_Generic_Actual_Parameter =>
1722 New_Occurrence_Of (Subp, Sloc (I_Node))));
1723 end;
1724 end if;
1725
1726 when N_Formal_Package_Declaration =>
1727 Match :=
1728 Matching_Actual
1729 (Defining_Identifier (Formal),
1730 Defining_Identifier (Original_Node (Analyzed_Formal)));
1731
1732 if No (Match) then
1733 if Partial_Parameterization then
1734 Process_Default (Formal);
1735
1736 else
1737 Error_Msg_Sloc := Sloc (Gen_Unit);
1738 Error_Msg_NE
1739 ("missing actual&",
1740 Instantiation_Node, Defining_Identifier (Formal));
1741 Error_Msg_NE
1742 ("\in instantiation of & declared#",
1743 Instantiation_Node, Gen_Unit);
1744
1745 Abandon_Instantiation (Instantiation_Node);
1746 end if;
1747
1748 else
1749 Analyze (Match);
1750 Append_List
1751 (Instantiate_Formal_Package
1752 (Formal, Match, Analyzed_Formal),
1753 Assoc);
1754 end if;
1755
1756 -- For use type and use package appearing in the generic part,
1757 -- we have already copied them, so we can just move them where
1758 -- they belong (we mustn't recopy them since this would mess up
1759 -- the Sloc values).
1760
1761 when N_Use_Package_Clause |
1762 N_Use_Type_Clause =>
1763 if Nkind (Original_Node (I_Node)) =
1764 N_Formal_Package_Declaration
1765 then
1766 Append (New_Copy_Tree (Formal), Assoc);
1767 else
1768 Remove (Formal);
1769 Append (Formal, Assoc);
1770 end if;
1771
1772 when others =>
1773 raise Program_Error;
1774
1775 end case;
1776
1777 Formal := Saved_Formal;
1778 Next_Non_Pragma (Analyzed_Formal);
1779 end loop;
1780
1781 if Num_Actuals > Num_Matched then
1782 Error_Msg_Sloc := Sloc (Gen_Unit);
1783
1784 if Present (Selector_Name (Actual)) then
1785 Error_Msg_NE
1786 ("unmatched actual &", Actual, Selector_Name (Actual));
1787 Error_Msg_NE
1788 ("\in instantiation of & declared#", Actual, Gen_Unit);
1789 else
1790 Error_Msg_NE
1791 ("unmatched actual in instantiation of & declared#",
1792 Actual, Gen_Unit);
1793 end if;
1794 end if;
1795
1796 elsif Present (Actuals) then
1797 Error_Msg_N
1798 ("too many actuals in generic instantiation", Instantiation_Node);
1799 end if;
1800
1801 -- An instantiation freezes all generic actuals. The only exceptions
1802 -- to this are incomplete types and subprograms which are not fully
1803 -- defined at the point of instantiation.
1804
1805 declare
1806 Elmt : Elmt_Id := First_Elmt (Actuals_To_Freeze);
1807 begin
1808 while Present (Elmt) loop
1809 Freeze_Before (I_Node, Node (Elmt));
1810 Next_Elmt (Elmt);
1811 end loop;
1812 end;
1813
1814 -- If there are default subprograms, normalize the tree by adding
1815 -- explicit associations for them. This is required if the instance
1816 -- appears within a generic.
1817
1818 if not Is_Empty_List (Default_Actuals) then
1819 declare
1820 Default : Node_Id;
1821
1822 begin
1823 Default := First (Default_Actuals);
1824 while Present (Default) loop
1825 Mark_Rewrite_Insertion (Default);
1826 Next (Default);
1827 end loop;
1828
1829 if No (Actuals) then
1830 Set_Generic_Associations (I_Node, Default_Actuals);
1831 else
1832 Append_List_To (Actuals, Default_Actuals);
1833 end if;
1834 end;
1835 end if;
1836
1837 -- If this is a formal package, normalize the parameter list by adding
1838 -- explicit box associations for the formals that are covered by an
1839 -- Others_Choice.
1840
1841 if not Is_Empty_List (Default_Formals) then
1842 Append_List (Default_Formals, Formals);
1843 end if;
1844
1845 return Assoc;
1846 end Analyze_Associations;
1847
1848 -------------------------------
1849 -- Analyze_Formal_Array_Type --
1850 -------------------------------
1851
1852 procedure Analyze_Formal_Array_Type
1853 (T : in out Entity_Id;
1854 Def : Node_Id)
1855 is
1856 DSS : Node_Id;
1857
1858 begin
1859 -- Treated like a non-generic array declaration, with additional
1860 -- semantic checks.
1861
1862 Enter_Name (T);
1863
1864 if Nkind (Def) = N_Constrained_Array_Definition then
1865 DSS := First (Discrete_Subtype_Definitions (Def));
1866 while Present (DSS) loop
1867 if Nkind_In (DSS, N_Subtype_Indication,
1868 N_Range,
1869 N_Attribute_Reference)
1870 then
1871 Error_Msg_N ("only a subtype mark is allowed in a formal", DSS);
1872 end if;
1873
1874 Next (DSS);
1875 end loop;
1876 end if;
1877
1878 Array_Type_Declaration (T, Def);
1879 Set_Is_Generic_Type (Base_Type (T));
1880
1881 if Ekind (Component_Type (T)) = E_Incomplete_Type
1882 and then No (Full_View (Component_Type (T)))
1883 then
1884 Error_Msg_N ("premature usage of incomplete type", Def);
1885
1886 -- Check that range constraint is not allowed on the component type
1887 -- of a generic formal array type (AARM 12.5.3(3))
1888
1889 elsif Is_Internal (Component_Type (T))
1890 and then Present (Subtype_Indication (Component_Definition (Def)))
1891 and then Nkind (Original_Node
1892 (Subtype_Indication (Component_Definition (Def)))) =
1893 N_Subtype_Indication
1894 then
1895 Error_Msg_N
1896 ("in a formal, a subtype indication can only be "
1897 & "a subtype mark (RM 12.5.3(3))",
1898 Subtype_Indication (Component_Definition (Def)));
1899 end if;
1900
1901 end Analyze_Formal_Array_Type;
1902
1903 ---------------------------------------------
1904 -- Analyze_Formal_Decimal_Fixed_Point_Type --
1905 ---------------------------------------------
1906
1907 -- As for other generic types, we create a valid type representation with
1908 -- legal but arbitrary attributes, whose values are never considered
1909 -- static. For all scalar types we introduce an anonymous base type, with
1910 -- the same attributes. We choose the corresponding integer type to be
1911 -- Standard_Integer.
1912 -- Here and in other similar routines, the Sloc of the generated internal
1913 -- type must be the same as the sloc of the defining identifier of the
1914 -- formal type declaration, to provide proper source navigation.
1915
1916 procedure Analyze_Formal_Decimal_Fixed_Point_Type
1917 (T : Entity_Id;
1918 Def : Node_Id)
1919 is
1920 Loc : constant Source_Ptr := Sloc (Def);
1921
1922 Base : constant Entity_Id :=
1923 New_Internal_Entity
1924 (E_Decimal_Fixed_Point_Type,
1925 Current_Scope,
1926 Sloc (Defining_Identifier (Parent (Def))), 'G');
1927
1928 Int_Base : constant Entity_Id := Standard_Integer;
1929 Delta_Val : constant Ureal := Ureal_1;
1930 Digs_Val : constant Uint := Uint_6;
1931
1932 function Make_Dummy_Bound return Node_Id;
1933 -- Return a properly typed universal real literal to use as a bound
1934
1935 ----------------------
1936 -- Make_Dummy_Bound --
1937 ----------------------
1938
1939 function Make_Dummy_Bound return Node_Id is
1940 Bound : constant Node_Id := Make_Real_Literal (Loc, Ureal_1);
1941 begin
1942 Set_Etype (Bound, Universal_Real);
1943 return Bound;
1944 end Make_Dummy_Bound;
1945
1946 -- Start of processing for Analyze_Formal_Decimal_Fixed_Point_Type
1947
1948 begin
1949 Enter_Name (T);
1950
1951 Set_Etype (Base, Base);
1952 Set_Size_Info (Base, Int_Base);
1953 Set_RM_Size (Base, RM_Size (Int_Base));
1954 Set_First_Rep_Item (Base, First_Rep_Item (Int_Base));
1955 Set_Digits_Value (Base, Digs_Val);
1956 Set_Delta_Value (Base, Delta_Val);
1957 Set_Small_Value (Base, Delta_Val);
1958 Set_Scalar_Range (Base,
1959 Make_Range (Loc,
1960 Low_Bound => Make_Dummy_Bound,
1961 High_Bound => Make_Dummy_Bound));
1962
1963 Set_Is_Generic_Type (Base);
1964 Set_Parent (Base, Parent (Def));
1965
1966 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
1967 Set_Etype (T, Base);
1968 Set_Size_Info (T, Int_Base);
1969 Set_RM_Size (T, RM_Size (Int_Base));
1970 Set_First_Rep_Item (T, First_Rep_Item (Int_Base));
1971 Set_Digits_Value (T, Digs_Val);
1972 Set_Delta_Value (T, Delta_Val);
1973 Set_Small_Value (T, Delta_Val);
1974 Set_Scalar_Range (T, Scalar_Range (Base));
1975 Set_Is_Constrained (T);
1976
1977 Check_Restriction (No_Fixed_Point, Def);
1978 end Analyze_Formal_Decimal_Fixed_Point_Type;
1979
1980 -------------------------------------------
1981 -- Analyze_Formal_Derived_Interface_Type --
1982 -------------------------------------------
1983
1984 procedure Analyze_Formal_Derived_Interface_Type
1985 (N : Node_Id;
1986 T : Entity_Id;
1987 Def : Node_Id)
1988 is
1989 Loc : constant Source_Ptr := Sloc (Def);
1990
1991 begin
1992 -- Rewrite as a type declaration of a derived type. This ensures that
1993 -- the interface list and primitive operations are properly captured.
1994
1995 Rewrite (N,
1996 Make_Full_Type_Declaration (Loc,
1997 Defining_Identifier => T,
1998 Type_Definition => Def));
1999 Analyze (N);
2000 Set_Is_Generic_Type (T);
2001 end Analyze_Formal_Derived_Interface_Type;
2002
2003 ---------------------------------
2004 -- Analyze_Formal_Derived_Type --
2005 ---------------------------------
2006
2007 procedure Analyze_Formal_Derived_Type
2008 (N : Node_Id;
2009 T : Entity_Id;
2010 Def : Node_Id)
2011 is
2012 Loc : constant Source_Ptr := Sloc (Def);
2013 Unk_Disc : constant Boolean := Unknown_Discriminants_Present (N);
2014 New_N : Node_Id;
2015
2016 begin
2017 Set_Is_Generic_Type (T);
2018
2019 if Private_Present (Def) then
2020 New_N :=
2021 Make_Private_Extension_Declaration (Loc,
2022 Defining_Identifier => T,
2023 Discriminant_Specifications => Discriminant_Specifications (N),
2024 Unknown_Discriminants_Present => Unk_Disc,
2025 Subtype_Indication => Subtype_Mark (Def),
2026 Interface_List => Interface_List (Def));
2027
2028 Set_Abstract_Present (New_N, Abstract_Present (Def));
2029 Set_Limited_Present (New_N, Limited_Present (Def));
2030 Set_Synchronized_Present (New_N, Synchronized_Present (Def));
2031
2032 else
2033 New_N :=
2034 Make_Full_Type_Declaration (Loc,
2035 Defining_Identifier => T,
2036 Discriminant_Specifications =>
2037 Discriminant_Specifications (Parent (T)),
2038 Type_Definition =>
2039 Make_Derived_Type_Definition (Loc,
2040 Subtype_Indication => Subtype_Mark (Def)));
2041
2042 Set_Abstract_Present
2043 (Type_Definition (New_N), Abstract_Present (Def));
2044 Set_Limited_Present
2045 (Type_Definition (New_N), Limited_Present (Def));
2046 end if;
2047
2048 Rewrite (N, New_N);
2049 Analyze (N);
2050
2051 if Unk_Disc then
2052 if not Is_Composite_Type (T) then
2053 Error_Msg_N
2054 ("unknown discriminants not allowed for elementary types", N);
2055 else
2056 Set_Has_Unknown_Discriminants (T);
2057 Set_Is_Constrained (T, False);
2058 end if;
2059 end if;
2060
2061 -- If the parent type has a known size, so does the formal, which makes
2062 -- legal representation clauses that involve the formal.
2063
2064 Set_Size_Known_At_Compile_Time
2065 (T, Size_Known_At_Compile_Time (Entity (Subtype_Mark (Def))));
2066 end Analyze_Formal_Derived_Type;
2067
2068 ----------------------------------
2069 -- Analyze_Formal_Discrete_Type --
2070 ----------------------------------
2071
2072 -- The operations defined for a discrete types are those of an enumeration
2073 -- type. The size is set to an arbitrary value, for use in analyzing the
2074 -- generic unit.
2075
2076 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id) is
2077 Loc : constant Source_Ptr := Sloc (Def);
2078 Lo : Node_Id;
2079 Hi : Node_Id;
2080
2081 Base : constant Entity_Id :=
2082 New_Internal_Entity
2083 (E_Floating_Point_Type, Current_Scope,
2084 Sloc (Defining_Identifier (Parent (Def))), 'G');
2085
2086 begin
2087 Enter_Name (T);
2088 Set_Ekind (T, E_Enumeration_Subtype);
2089 Set_Etype (T, Base);
2090 Init_Size (T, 8);
2091 Init_Alignment (T);
2092 Set_Is_Generic_Type (T);
2093 Set_Is_Constrained (T);
2094
2095 -- For semantic analysis, the bounds of the type must be set to some
2096 -- non-static value. The simplest is to create attribute nodes for those
2097 -- bounds, that refer to the type itself. These bounds are never
2098 -- analyzed but serve as place-holders.
2099
2100 Lo :=
2101 Make_Attribute_Reference (Loc,
2102 Attribute_Name => Name_First,
2103 Prefix => New_Occurrence_Of (T, Loc));
2104 Set_Etype (Lo, T);
2105
2106 Hi :=
2107 Make_Attribute_Reference (Loc,
2108 Attribute_Name => Name_Last,
2109 Prefix => New_Occurrence_Of (T, Loc));
2110 Set_Etype (Hi, T);
2111
2112 Set_Scalar_Range (T,
2113 Make_Range (Loc,
2114 Low_Bound => Lo,
2115 High_Bound => Hi));
2116
2117 Set_Ekind (Base, E_Enumeration_Type);
2118 Set_Etype (Base, Base);
2119 Init_Size (Base, 8);
2120 Init_Alignment (Base);
2121 Set_Is_Generic_Type (Base);
2122 Set_Scalar_Range (Base, Scalar_Range (T));
2123 Set_Parent (Base, Parent (Def));
2124 end Analyze_Formal_Discrete_Type;
2125
2126 ----------------------------------
2127 -- Analyze_Formal_Floating_Type --
2128 ---------------------------------
2129
2130 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id) is
2131 Base : constant Entity_Id :=
2132 New_Internal_Entity
2133 (E_Floating_Point_Type, Current_Scope,
2134 Sloc (Defining_Identifier (Parent (Def))), 'G');
2135
2136 begin
2137 -- The various semantic attributes are taken from the predefined type
2138 -- Float, just so that all of them are initialized. Their values are
2139 -- never used because no constant folding or expansion takes place in
2140 -- the generic itself.
2141
2142 Enter_Name (T);
2143 Set_Ekind (T, E_Floating_Point_Subtype);
2144 Set_Etype (T, Base);
2145 Set_Size_Info (T, (Standard_Float));
2146 Set_RM_Size (T, RM_Size (Standard_Float));
2147 Set_Digits_Value (T, Digits_Value (Standard_Float));
2148 Set_Scalar_Range (T, Scalar_Range (Standard_Float));
2149 Set_Is_Constrained (T);
2150
2151 Set_Is_Generic_Type (Base);
2152 Set_Etype (Base, Base);
2153 Set_Size_Info (Base, (Standard_Float));
2154 Set_RM_Size (Base, RM_Size (Standard_Float));
2155 Set_Digits_Value (Base, Digits_Value (Standard_Float));
2156 Set_Scalar_Range (Base, Scalar_Range (Standard_Float));
2157 Set_Parent (Base, Parent (Def));
2158
2159 Check_Restriction (No_Floating_Point, Def);
2160 end Analyze_Formal_Floating_Type;
2161
2162 -----------------------------------
2163 -- Analyze_Formal_Interface_Type;--
2164 -----------------------------------
2165
2166 procedure Analyze_Formal_Interface_Type
2167 (N : Node_Id;
2168 T : Entity_Id;
2169 Def : Node_Id)
2170 is
2171 Loc : constant Source_Ptr := Sloc (N);
2172 New_N : Node_Id;
2173
2174 begin
2175 New_N :=
2176 Make_Full_Type_Declaration (Loc,
2177 Defining_Identifier => T,
2178 Type_Definition => Def);
2179
2180 Rewrite (N, New_N);
2181 Analyze (N);
2182 Set_Is_Generic_Type (T);
2183 end Analyze_Formal_Interface_Type;
2184
2185 ---------------------------------
2186 -- Analyze_Formal_Modular_Type --
2187 ---------------------------------
2188
2189 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id) is
2190 begin
2191 -- Apart from their entity kind, generic modular types are treated like
2192 -- signed integer types, and have the same attributes.
2193
2194 Analyze_Formal_Signed_Integer_Type (T, Def);
2195 Set_Ekind (T, E_Modular_Integer_Subtype);
2196 Set_Ekind (Etype (T), E_Modular_Integer_Type);
2197
2198 end Analyze_Formal_Modular_Type;
2199
2200 ---------------------------------------
2201 -- Analyze_Formal_Object_Declaration --
2202 ---------------------------------------
2203
2204 procedure Analyze_Formal_Object_Declaration (N : Node_Id) is
2205 E : constant Node_Id := Default_Expression (N);
2206 Id : constant Node_Id := Defining_Identifier (N);
2207 K : Entity_Kind;
2208 T : Node_Id;
2209
2210 begin
2211 Enter_Name (Id);
2212
2213 -- Determine the mode of the formal object
2214
2215 if Out_Present (N) then
2216 K := E_Generic_In_Out_Parameter;
2217
2218 if not In_Present (N) then
2219 Error_Msg_N ("formal generic objects cannot have mode OUT", N);
2220 end if;
2221
2222 else
2223 K := E_Generic_In_Parameter;
2224 end if;
2225
2226 if Present (Subtype_Mark (N)) then
2227 Find_Type (Subtype_Mark (N));
2228 T := Entity (Subtype_Mark (N));
2229
2230 -- Verify that there is no redundant null exclusion
2231
2232 if Null_Exclusion_Present (N) then
2233 if not Is_Access_Type (T) then
2234 Error_Msg_N
2235 ("null exclusion can only apply to an access type", N);
2236
2237 elsif Can_Never_Be_Null (T) then
2238 Error_Msg_NE
2239 ("`NOT NULL` not allowed (& already excludes null)", N, T);
2240 end if;
2241 end if;
2242
2243 -- Ada 2005 (AI-423): Formal object with an access definition
2244
2245 else
2246 Check_Access_Definition (N);
2247 T := Access_Definition
2248 (Related_Nod => N,
2249 N => Access_Definition (N));
2250 end if;
2251
2252 if Ekind (T) = E_Incomplete_Type then
2253 declare
2254 Error_Node : Node_Id;
2255
2256 begin
2257 if Present (Subtype_Mark (N)) then
2258 Error_Node := Subtype_Mark (N);
2259 else
2260 Check_Access_Definition (N);
2261 Error_Node := Access_Definition (N);
2262 end if;
2263
2264 Error_Msg_N ("premature usage of incomplete type", Error_Node);
2265 end;
2266 end if;
2267
2268 if K = E_Generic_In_Parameter then
2269
2270 -- Ada 2005 (AI-287): Limited aggregates allowed in generic formals
2271
2272 if Ada_Version < Ada_2005 and then Is_Limited_Type (T) then
2273 Error_Msg_N
2274 ("generic formal of mode IN must not be of limited type", N);
2275 Explain_Limited_Type (T, N);
2276 end if;
2277
2278 if Is_Abstract_Type (T) then
2279 Error_Msg_N
2280 ("generic formal of mode IN must not be of abstract type", N);
2281 end if;
2282
2283 if Present (E) then
2284 Preanalyze_Spec_Expression (E, T);
2285
2286 if Is_Limited_Type (T) and then not OK_For_Limited_Init (T, E) then
2287 Error_Msg_N
2288 ("initialization not allowed for limited types", E);
2289 Explain_Limited_Type (T, E);
2290 end if;
2291 end if;
2292
2293 Set_Ekind (Id, K);
2294 Set_Etype (Id, T);
2295
2296 -- Case of generic IN OUT parameter
2297
2298 else
2299 -- If the formal has an unconstrained type, construct its actual
2300 -- subtype, as is done for subprogram formals. In this fashion, all
2301 -- its uses can refer to specific bounds.
2302
2303 Set_Ekind (Id, K);
2304 Set_Etype (Id, T);
2305
2306 if (Is_Array_Type (T) and then not Is_Constrained (T))
2307 or else (Ekind (T) = E_Record_Type and then Has_Discriminants (T))
2308 then
2309 declare
2310 Non_Freezing_Ref : constant Node_Id :=
2311 New_Occurrence_Of (Id, Sloc (Id));
2312 Decl : Node_Id;
2313
2314 begin
2315 -- Make sure the actual subtype doesn't generate bogus freezing
2316
2317 Set_Must_Not_Freeze (Non_Freezing_Ref);
2318 Decl := Build_Actual_Subtype (T, Non_Freezing_Ref);
2319 Insert_Before_And_Analyze (N, Decl);
2320 Set_Actual_Subtype (Id, Defining_Identifier (Decl));
2321 end;
2322 else
2323 Set_Actual_Subtype (Id, T);
2324 end if;
2325
2326 if Present (E) then
2327 Error_Msg_N
2328 ("initialization not allowed for `IN OUT` formals", N);
2329 end if;
2330 end if;
2331
2332 if Has_Aspects (N) then
2333 Analyze_Aspect_Specifications (N, Id);
2334 end if;
2335 end Analyze_Formal_Object_Declaration;
2336
2337 ----------------------------------------------
2338 -- Analyze_Formal_Ordinary_Fixed_Point_Type --
2339 ----------------------------------------------
2340
2341 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
2342 (T : Entity_Id;
2343 Def : Node_Id)
2344 is
2345 Loc : constant Source_Ptr := Sloc (Def);
2346 Base : constant Entity_Id :=
2347 New_Internal_Entity
2348 (E_Ordinary_Fixed_Point_Type, Current_Scope,
2349 Sloc (Defining_Identifier (Parent (Def))), 'G');
2350
2351 begin
2352 -- The semantic attributes are set for completeness only, their values
2353 -- will never be used, since all properties of the type are non-static.
2354
2355 Enter_Name (T);
2356 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
2357 Set_Etype (T, Base);
2358 Set_Size_Info (T, Standard_Integer);
2359 Set_RM_Size (T, RM_Size (Standard_Integer));
2360 Set_Small_Value (T, Ureal_1);
2361 Set_Delta_Value (T, Ureal_1);
2362 Set_Scalar_Range (T,
2363 Make_Range (Loc,
2364 Low_Bound => Make_Real_Literal (Loc, Ureal_1),
2365 High_Bound => Make_Real_Literal (Loc, Ureal_1)));
2366 Set_Is_Constrained (T);
2367
2368 Set_Is_Generic_Type (Base);
2369 Set_Etype (Base, Base);
2370 Set_Size_Info (Base, Standard_Integer);
2371 Set_RM_Size (Base, RM_Size (Standard_Integer));
2372 Set_Small_Value (Base, Ureal_1);
2373 Set_Delta_Value (Base, Ureal_1);
2374 Set_Scalar_Range (Base, Scalar_Range (T));
2375 Set_Parent (Base, Parent (Def));
2376
2377 Check_Restriction (No_Fixed_Point, Def);
2378 end Analyze_Formal_Ordinary_Fixed_Point_Type;
2379
2380 ----------------------------------------
2381 -- Analyze_Formal_Package_Declaration --
2382 ----------------------------------------
2383
2384 procedure Analyze_Formal_Package_Declaration (N : Node_Id) is
2385 Loc : constant Source_Ptr := Sloc (N);
2386 Pack_Id : constant Entity_Id := Defining_Identifier (N);
2387 Formal : Entity_Id;
2388 Gen_Id : constant Node_Id := Name (N);
2389 Gen_Decl : Node_Id;
2390 Gen_Unit : Entity_Id;
2391 New_N : Node_Id;
2392 Parent_Installed : Boolean := False;
2393 Renaming : Node_Id;
2394 Parent_Instance : Entity_Id;
2395 Renaming_In_Par : Entity_Id;
2396 Associations : Boolean := True;
2397
2398 Vis_Prims_List : Elist_Id := No_Elist;
2399 -- List of primitives made temporarily visible in the instantiation
2400 -- to match the visibility of the formal type
2401
2402 function Build_Local_Package return Node_Id;
2403 -- The formal package is rewritten so that its parameters are replaced
2404 -- with corresponding declarations. For parameters with bona fide
2405 -- associations these declarations are created by Analyze_Associations
2406 -- as for a regular instantiation. For boxed parameters, we preserve
2407 -- the formal declarations and analyze them, in order to introduce
2408 -- entities of the right kind in the environment of the formal.
2409
2410 -------------------------
2411 -- Build_Local_Package --
2412 -------------------------
2413
2414 function Build_Local_Package return Node_Id is
2415 Decls : List_Id;
2416 Pack_Decl : Node_Id;
2417
2418 begin
2419 -- Within the formal, the name of the generic package is a renaming
2420 -- of the formal (as for a regular instantiation).
2421
2422 Pack_Decl :=
2423 Make_Package_Declaration (Loc,
2424 Specification =>
2425 Copy_Generic_Node
2426 (Specification (Original_Node (Gen_Decl)),
2427 Empty, Instantiating => True));
2428
2429 Renaming :=
2430 Make_Package_Renaming_Declaration (Loc,
2431 Defining_Unit_Name =>
2432 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
2433 Name => New_Occurrence_Of (Formal, Loc));
2434
2435 if Nkind (Gen_Id) = N_Identifier
2436 and then Chars (Gen_Id) = Chars (Pack_Id)
2437 then
2438 Error_Msg_NE
2439 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
2440 end if;
2441
2442 -- If the formal is declared with a box, or with an others choice,
2443 -- create corresponding declarations for all entities in the formal
2444 -- part, so that names with the proper types are available in the
2445 -- specification of the formal package.
2446
2447 -- On the other hand, if there are no associations, then all the
2448 -- formals must have defaults, and this will be checked by the
2449 -- call to Analyze_Associations.
2450
2451 if Box_Present (N)
2452 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2453 then
2454 declare
2455 Formal_Decl : Node_Id;
2456
2457 begin
2458 -- TBA : for a formal package, need to recurse ???
2459
2460 Decls := New_List;
2461 Formal_Decl :=
2462 First
2463 (Generic_Formal_Declarations (Original_Node (Gen_Decl)));
2464 while Present (Formal_Decl) loop
2465 Append_To
2466 (Decls, Copy_Generic_Node (Formal_Decl, Empty, True));
2467 Next (Formal_Decl);
2468 end loop;
2469 end;
2470
2471 -- If generic associations are present, use Analyze_Associations to
2472 -- create the proper renaming declarations.
2473
2474 else
2475 declare
2476 Act_Tree : constant Node_Id :=
2477 Copy_Generic_Node
2478 (Original_Node (Gen_Decl), Empty,
2479 Instantiating => True);
2480
2481 begin
2482 Generic_Renamings.Set_Last (0);
2483 Generic_Renamings_HTable.Reset;
2484 Instantiation_Node := N;
2485
2486 Decls :=
2487 Analyze_Associations
2488 (I_Node => Original_Node (N),
2489 Formals => Generic_Formal_Declarations (Act_Tree),
2490 F_Copy => Generic_Formal_Declarations (Gen_Decl));
2491
2492 Vis_Prims_List := Check_Hidden_Primitives (Decls);
2493 end;
2494 end if;
2495
2496 Append (Renaming, To => Decls);
2497
2498 -- Add generated declarations ahead of local declarations in
2499 -- the package.
2500
2501 if No (Visible_Declarations (Specification (Pack_Decl))) then
2502 Set_Visible_Declarations (Specification (Pack_Decl), Decls);
2503 else
2504 Insert_List_Before
2505 (First (Visible_Declarations (Specification (Pack_Decl))),
2506 Decls);
2507 end if;
2508
2509 return Pack_Decl;
2510 end Build_Local_Package;
2511
2512 -- Start of processing for Analyze_Formal_Package_Declaration
2513
2514 begin
2515 Check_Text_IO_Special_Unit (Gen_Id);
2516
2517 Init_Env;
2518 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
2519 Gen_Unit := Entity (Gen_Id);
2520
2521 -- Check for a formal package that is a package renaming
2522
2523 if Present (Renamed_Object (Gen_Unit)) then
2524
2525 -- Indicate that unit is used, before replacing it with renamed
2526 -- entity for use below.
2527
2528 if In_Extended_Main_Source_Unit (N) then
2529 Set_Is_Instantiated (Gen_Unit);
2530 Generate_Reference (Gen_Unit, N);
2531 end if;
2532
2533 Gen_Unit := Renamed_Object (Gen_Unit);
2534 end if;
2535
2536 if Ekind (Gen_Unit) /= E_Generic_Package then
2537 Error_Msg_N ("expect generic package name", Gen_Id);
2538 Restore_Env;
2539 goto Leave;
2540
2541 elsif Gen_Unit = Current_Scope then
2542 Error_Msg_N
2543 ("generic package cannot be used as a formal package of itself",
2544 Gen_Id);
2545 Restore_Env;
2546 goto Leave;
2547
2548 elsif In_Open_Scopes (Gen_Unit) then
2549 if Is_Compilation_Unit (Gen_Unit)
2550 and then Is_Child_Unit (Current_Scope)
2551 then
2552 -- Special-case the error when the formal is a parent, and
2553 -- continue analysis to minimize cascaded errors.
2554
2555 Error_Msg_N
2556 ("generic parent cannot be used as formal package "
2557 & "of a child unit", Gen_Id);
2558
2559 else
2560 Error_Msg_N
2561 ("generic package cannot be used as a formal package "
2562 & "within itself", Gen_Id);
2563 Restore_Env;
2564 goto Leave;
2565 end if;
2566 end if;
2567
2568 -- Check that name of formal package does not hide name of generic,
2569 -- or its leading prefix. This check must be done separately because
2570 -- the name of the generic has already been analyzed.
2571
2572 declare
2573 Gen_Name : Entity_Id;
2574
2575 begin
2576 Gen_Name := Gen_Id;
2577 while Nkind (Gen_Name) = N_Expanded_Name loop
2578 Gen_Name := Prefix (Gen_Name);
2579 end loop;
2580
2581 if Chars (Gen_Name) = Chars (Pack_Id) then
2582 Error_Msg_NE
2583 ("& is hidden within declaration of formal package",
2584 Gen_Id, Gen_Name);
2585 end if;
2586 end;
2587
2588 if Box_Present (N)
2589 or else No (Generic_Associations (N))
2590 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2591 then
2592 Associations := False;
2593 end if;
2594
2595 -- If there are no generic associations, the generic parameters appear
2596 -- as local entities and are instantiated like them. We copy the generic
2597 -- package declaration as if it were an instantiation, and analyze it
2598 -- like a regular package, except that we treat the formals as
2599 -- additional visible components.
2600
2601 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
2602
2603 if In_Extended_Main_Source_Unit (N) then
2604 Set_Is_Instantiated (Gen_Unit);
2605 Generate_Reference (Gen_Unit, N);
2606 end if;
2607
2608 Formal := New_Copy (Pack_Id);
2609 Create_Instantiation_Source (N, Gen_Unit, False, S_Adjustment);
2610
2611 begin
2612 -- Make local generic without formals. The formals will be replaced
2613 -- with internal declarations.
2614
2615 New_N := Build_Local_Package;
2616
2617 -- If there are errors in the parameter list, Analyze_Associations
2618 -- raises Instantiation_Error. Patch the declaration to prevent
2619 -- further exception propagation.
2620
2621 exception
2622 when Instantiation_Error =>
2623
2624 Enter_Name (Formal);
2625 Set_Ekind (Formal, E_Variable);
2626 Set_Etype (Formal, Any_Type);
2627 Restore_Hidden_Primitives (Vis_Prims_List);
2628
2629 if Parent_Installed then
2630 Remove_Parent;
2631 end if;
2632
2633 goto Leave;
2634 end;
2635
2636 Rewrite (N, New_N);
2637 Set_Defining_Unit_Name (Specification (New_N), Formal);
2638 Set_Generic_Parent (Specification (N), Gen_Unit);
2639 Set_Instance_Env (Gen_Unit, Formal);
2640 Set_Is_Generic_Instance (Formal);
2641
2642 Enter_Name (Formal);
2643 Set_Ekind (Formal, E_Package);
2644 Set_Etype (Formal, Standard_Void_Type);
2645 Set_Inner_Instances (Formal, New_Elmt_List);
2646 Push_Scope (Formal);
2647
2648 if Is_Child_Unit (Gen_Unit) and then Parent_Installed then
2649
2650 -- Similarly, we have to make the name of the formal visible in the
2651 -- parent instance, to resolve properly fully qualified names that
2652 -- may appear in the generic unit. The parent instance has been
2653 -- placed on the scope stack ahead of the current scope.
2654
2655 Parent_Instance := Scope_Stack.Table (Scope_Stack.Last - 1).Entity;
2656
2657 Renaming_In_Par :=
2658 Make_Defining_Identifier (Loc, Chars (Gen_Unit));
2659 Set_Ekind (Renaming_In_Par, E_Package);
2660 Set_Etype (Renaming_In_Par, Standard_Void_Type);
2661 Set_Scope (Renaming_In_Par, Parent_Instance);
2662 Set_Parent (Renaming_In_Par, Parent (Formal));
2663 Set_Renamed_Object (Renaming_In_Par, Formal);
2664 Append_Entity (Renaming_In_Par, Parent_Instance);
2665 end if;
2666
2667 Analyze (Specification (N));
2668
2669 -- The formals for which associations are provided are not visible
2670 -- outside of the formal package. The others are still declared by a
2671 -- formal parameter declaration.
2672
2673 -- If there are no associations, the only local entity to hide is the
2674 -- generated package renaming itself.
2675
2676 declare
2677 E : Entity_Id;
2678
2679 begin
2680 E := First_Entity (Formal);
2681 while Present (E) loop
2682 if Associations and then not Is_Generic_Formal (E) then
2683 Set_Is_Hidden (E);
2684 end if;
2685
2686 if Ekind (E) = E_Package and then Renamed_Entity (E) = Formal then
2687 Set_Is_Hidden (E);
2688 exit;
2689 end if;
2690
2691 Next_Entity (E);
2692 end loop;
2693 end;
2694
2695 End_Package_Scope (Formal);
2696 Restore_Hidden_Primitives (Vis_Prims_List);
2697
2698 if Parent_Installed then
2699 Remove_Parent;
2700 end if;
2701
2702 Restore_Env;
2703
2704 -- Inside the generic unit, the formal package is a regular package, but
2705 -- no body is needed for it. Note that after instantiation, the defining
2706 -- unit name we need is in the new tree and not in the original (see
2707 -- Package_Instantiation). A generic formal package is an instance, and
2708 -- can be used as an actual for an inner instance.
2709
2710 Set_Has_Completion (Formal, True);
2711
2712 -- Add semantic information to the original defining identifier.
2713 -- for ASIS use.
2714
2715 Set_Ekind (Pack_Id, E_Package);
2716 Set_Etype (Pack_Id, Standard_Void_Type);
2717 Set_Scope (Pack_Id, Scope (Formal));
2718 Set_Has_Completion (Pack_Id, True);
2719
2720 <<Leave>>
2721 if Has_Aspects (N) then
2722 Analyze_Aspect_Specifications (N, Pack_Id);
2723 end if;
2724 end Analyze_Formal_Package_Declaration;
2725
2726 ---------------------------------
2727 -- Analyze_Formal_Private_Type --
2728 ---------------------------------
2729
2730 procedure Analyze_Formal_Private_Type
2731 (N : Node_Id;
2732 T : Entity_Id;
2733 Def : Node_Id)
2734 is
2735 begin
2736 New_Private_Type (N, T, Def);
2737
2738 -- Set the size to an arbitrary but legal value
2739
2740 Set_Size_Info (T, Standard_Integer);
2741 Set_RM_Size (T, RM_Size (Standard_Integer));
2742 end Analyze_Formal_Private_Type;
2743
2744 ------------------------------------
2745 -- Analyze_Formal_Incomplete_Type --
2746 ------------------------------------
2747
2748 procedure Analyze_Formal_Incomplete_Type
2749 (T : Entity_Id;
2750 Def : Node_Id)
2751 is
2752 begin
2753 Enter_Name (T);
2754 Set_Ekind (T, E_Incomplete_Type);
2755 Set_Etype (T, T);
2756 Set_Private_Dependents (T, New_Elmt_List);
2757
2758 if Tagged_Present (Def) then
2759 Set_Is_Tagged_Type (T);
2760 Make_Class_Wide_Type (T);
2761 Set_Direct_Primitive_Operations (T, New_Elmt_List);
2762 end if;
2763 end Analyze_Formal_Incomplete_Type;
2764
2765 ----------------------------------------
2766 -- Analyze_Formal_Signed_Integer_Type --
2767 ----------------------------------------
2768
2769 procedure Analyze_Formal_Signed_Integer_Type
2770 (T : Entity_Id;
2771 Def : Node_Id)
2772 is
2773 Base : constant Entity_Id :=
2774 New_Internal_Entity
2775 (E_Signed_Integer_Type,
2776 Current_Scope,
2777 Sloc (Defining_Identifier (Parent (Def))), 'G');
2778
2779 begin
2780 Enter_Name (T);
2781
2782 Set_Ekind (T, E_Signed_Integer_Subtype);
2783 Set_Etype (T, Base);
2784 Set_Size_Info (T, Standard_Integer);
2785 Set_RM_Size (T, RM_Size (Standard_Integer));
2786 Set_Scalar_Range (T, Scalar_Range (Standard_Integer));
2787 Set_Is_Constrained (T);
2788
2789 Set_Is_Generic_Type (Base);
2790 Set_Size_Info (Base, Standard_Integer);
2791 Set_RM_Size (Base, RM_Size (Standard_Integer));
2792 Set_Etype (Base, Base);
2793 Set_Scalar_Range (Base, Scalar_Range (Standard_Integer));
2794 Set_Parent (Base, Parent (Def));
2795 end Analyze_Formal_Signed_Integer_Type;
2796
2797 -------------------------------------------
2798 -- Analyze_Formal_Subprogram_Declaration --
2799 -------------------------------------------
2800
2801 procedure Analyze_Formal_Subprogram_Declaration (N : Node_Id) is
2802 Spec : constant Node_Id := Specification (N);
2803 Def : constant Node_Id := Default_Name (N);
2804 Nam : constant Entity_Id := Defining_Unit_Name (Spec);
2805 Subp : Entity_Id;
2806
2807 begin
2808 if Nam = Error then
2809 return;
2810 end if;
2811
2812 if Nkind (Nam) = N_Defining_Program_Unit_Name then
2813 Error_Msg_N ("name of formal subprogram must be a direct name", Nam);
2814 goto Leave;
2815 end if;
2816
2817 Analyze_Subprogram_Declaration (N);
2818 Set_Is_Formal_Subprogram (Nam);
2819 Set_Has_Completion (Nam);
2820
2821 if Nkind (N) = N_Formal_Abstract_Subprogram_Declaration then
2822 Set_Is_Abstract_Subprogram (Nam);
2823
2824 Set_Is_Dispatching_Operation (Nam);
2825
2826 -- A formal abstract procedure cannot have a null default
2827 -- (RM 12.6(4 1.2)).
2828
2829 if Nkind (Spec) = N_Procedure_Specification
2830 and then Null_Present (Spec)
2831 then
2832 Error_Msg_N
2833 ("a formal abstract subprogram cannot default to null", Spec);
2834 end if;
2835
2836 declare
2837 Ctrl_Type : constant Entity_Id := Find_Dispatching_Type (Nam);
2838 begin
2839 if No (Ctrl_Type) then
2840 Error_Msg_N
2841 ("abstract formal subprogram must have a controlling type",
2842 N);
2843
2844 elsif Ada_Version >= Ada_2012
2845 and then Is_Incomplete_Type (Ctrl_Type)
2846 then
2847 Error_Msg_NE
2848 ("controlling type of abstract formal subprogram cannot "
2849 & "be incomplete type", N, Ctrl_Type);
2850
2851 else
2852 Check_Controlling_Formals (Ctrl_Type, Nam);
2853 end if;
2854 end;
2855 end if;
2856
2857 -- Default name is resolved at the point of instantiation
2858
2859 if Box_Present (N) then
2860 null;
2861
2862 -- Else default is bound at the point of generic declaration
2863
2864 elsif Present (Def) then
2865 if Nkind (Def) = N_Operator_Symbol then
2866 Find_Direct_Name (Def);
2867
2868 elsif Nkind (Def) /= N_Attribute_Reference then
2869 Analyze (Def);
2870
2871 else
2872 -- For an attribute reference, analyze the prefix and verify
2873 -- that it has the proper profile for the subprogram.
2874
2875 Analyze (Prefix (Def));
2876 Valid_Default_Attribute (Nam, Def);
2877 goto Leave;
2878 end if;
2879
2880 -- Default name may be overloaded, in which case the interpretation
2881 -- with the correct profile must be selected, as for a renaming.
2882 -- If the definition is an indexed component, it must denote a
2883 -- member of an entry family. If it is a selected component, it
2884 -- can be a protected operation.
2885
2886 if Etype (Def) = Any_Type then
2887 goto Leave;
2888
2889 elsif Nkind (Def) = N_Selected_Component then
2890 if not Is_Overloadable (Entity (Selector_Name (Def))) then
2891 Error_Msg_N ("expect valid subprogram name as default", Def);
2892 end if;
2893
2894 elsif Nkind (Def) = N_Indexed_Component then
2895 if Is_Entity_Name (Prefix (Def)) then
2896 if Ekind (Entity (Prefix (Def))) /= E_Entry_Family then
2897 Error_Msg_N ("expect valid subprogram name as default", Def);
2898 end if;
2899
2900 elsif Nkind (Prefix (Def)) = N_Selected_Component then
2901 if Ekind (Entity (Selector_Name (Prefix (Def)))) /=
2902 E_Entry_Family
2903 then
2904 Error_Msg_N ("expect valid subprogram name as default", Def);
2905 end if;
2906
2907 else
2908 Error_Msg_N ("expect valid subprogram name as default", Def);
2909 goto Leave;
2910 end if;
2911
2912 elsif Nkind (Def) = N_Character_Literal then
2913
2914 -- Needs some type checks: subprogram should be parameterless???
2915
2916 Resolve (Def, (Etype (Nam)));
2917
2918 elsif not Is_Entity_Name (Def)
2919 or else not Is_Overloadable (Entity (Def))
2920 then
2921 Error_Msg_N ("expect valid subprogram name as default", Def);
2922 goto Leave;
2923
2924 elsif not Is_Overloaded (Def) then
2925 Subp := Entity (Def);
2926
2927 if Subp = Nam then
2928 Error_Msg_N ("premature usage of formal subprogram", Def);
2929
2930 elsif not Entity_Matches_Spec (Subp, Nam) then
2931 Error_Msg_N ("no visible entity matches specification", Def);
2932 end if;
2933
2934 -- More than one interpretation, so disambiguate as for a renaming
2935
2936 else
2937 declare
2938 I : Interp_Index;
2939 I1 : Interp_Index := 0;
2940 It : Interp;
2941 It1 : Interp;
2942
2943 begin
2944 Subp := Any_Id;
2945 Get_First_Interp (Def, I, It);
2946 while Present (It.Nam) loop
2947 if Entity_Matches_Spec (It.Nam, Nam) then
2948 if Subp /= Any_Id then
2949 It1 := Disambiguate (Def, I1, I, Etype (Subp));
2950
2951 if It1 = No_Interp then
2952 Error_Msg_N ("ambiguous default subprogram", Def);
2953 else
2954 Subp := It1.Nam;
2955 end if;
2956
2957 exit;
2958
2959 else
2960 I1 := I;
2961 Subp := It.Nam;
2962 end if;
2963 end if;
2964
2965 Get_Next_Interp (I, It);
2966 end loop;
2967 end;
2968
2969 if Subp /= Any_Id then
2970
2971 -- Subprogram found, generate reference to it
2972
2973 Set_Entity (Def, Subp);
2974 Generate_Reference (Subp, Def);
2975
2976 if Subp = Nam then
2977 Error_Msg_N ("premature usage of formal subprogram", Def);
2978
2979 elsif Ekind (Subp) /= E_Operator then
2980 Check_Mode_Conformant (Subp, Nam);
2981 end if;
2982
2983 else
2984 Error_Msg_N ("no visible subprogram matches specification", N);
2985 end if;
2986 end if;
2987 end if;
2988
2989 <<Leave>>
2990 if Has_Aspects (N) then
2991 Analyze_Aspect_Specifications (N, Nam);
2992 end if;
2993
2994 end Analyze_Formal_Subprogram_Declaration;
2995
2996 -------------------------------------
2997 -- Analyze_Formal_Type_Declaration --
2998 -------------------------------------
2999
3000 procedure Analyze_Formal_Type_Declaration (N : Node_Id) is
3001 Def : constant Node_Id := Formal_Type_Definition (N);
3002 T : Entity_Id;
3003
3004 begin
3005 T := Defining_Identifier (N);
3006
3007 if Present (Discriminant_Specifications (N))
3008 and then Nkind (Def) /= N_Formal_Private_Type_Definition
3009 then
3010 Error_Msg_N
3011 ("discriminants not allowed for this formal type", T);
3012 end if;
3013
3014 -- Enter the new name, and branch to specific routine
3015
3016 case Nkind (Def) is
3017 when N_Formal_Private_Type_Definition =>
3018 Analyze_Formal_Private_Type (N, T, Def);
3019
3020 when N_Formal_Derived_Type_Definition =>
3021 Analyze_Formal_Derived_Type (N, T, Def);
3022
3023 when N_Formal_Incomplete_Type_Definition =>
3024 Analyze_Formal_Incomplete_Type (T, Def);
3025
3026 when N_Formal_Discrete_Type_Definition =>
3027 Analyze_Formal_Discrete_Type (T, Def);
3028
3029 when N_Formal_Signed_Integer_Type_Definition =>
3030 Analyze_Formal_Signed_Integer_Type (T, Def);
3031
3032 when N_Formal_Modular_Type_Definition =>
3033 Analyze_Formal_Modular_Type (T, Def);
3034
3035 when N_Formal_Floating_Point_Definition =>
3036 Analyze_Formal_Floating_Type (T, Def);
3037
3038 when N_Formal_Ordinary_Fixed_Point_Definition =>
3039 Analyze_Formal_Ordinary_Fixed_Point_Type (T, Def);
3040
3041 when N_Formal_Decimal_Fixed_Point_Definition =>
3042 Analyze_Formal_Decimal_Fixed_Point_Type (T, Def);
3043
3044 when N_Array_Type_Definition =>
3045 Analyze_Formal_Array_Type (T, Def);
3046
3047 when N_Access_To_Object_Definition |
3048 N_Access_Function_Definition |
3049 N_Access_Procedure_Definition =>
3050 Analyze_Generic_Access_Type (T, Def);
3051
3052 -- Ada 2005: a interface declaration is encoded as an abstract
3053 -- record declaration or a abstract type derivation.
3054
3055 when N_Record_Definition =>
3056 Analyze_Formal_Interface_Type (N, T, Def);
3057
3058 when N_Derived_Type_Definition =>
3059 Analyze_Formal_Derived_Interface_Type (N, T, Def);
3060
3061 when N_Error =>
3062 null;
3063
3064 when others =>
3065 raise Program_Error;
3066
3067 end case;
3068
3069 Set_Is_Generic_Type (T);
3070
3071 if Has_Aspects (N) then
3072 Analyze_Aspect_Specifications (N, T);
3073 end if;
3074 end Analyze_Formal_Type_Declaration;
3075
3076 ------------------------------------
3077 -- Analyze_Function_Instantiation --
3078 ------------------------------------
3079
3080 procedure Analyze_Function_Instantiation (N : Node_Id) is
3081 begin
3082 Analyze_Subprogram_Instantiation (N, E_Function);
3083 end Analyze_Function_Instantiation;
3084
3085 ---------------------------------
3086 -- Analyze_Generic_Access_Type --
3087 ---------------------------------
3088
3089 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id) is
3090 begin
3091 Enter_Name (T);
3092
3093 if Nkind (Def) = N_Access_To_Object_Definition then
3094 Access_Type_Declaration (T, Def);
3095
3096 if Is_Incomplete_Or_Private_Type (Designated_Type (T))
3097 and then No (Full_View (Designated_Type (T)))
3098 and then not Is_Generic_Type (Designated_Type (T))
3099 then
3100 Error_Msg_N ("premature usage of incomplete type", Def);
3101
3102 elsif not Is_Entity_Name (Subtype_Indication (Def)) then
3103 Error_Msg_N
3104 ("only a subtype mark is allowed in a formal", Def);
3105 end if;
3106
3107 else
3108 Access_Subprogram_Declaration (T, Def);
3109 end if;
3110 end Analyze_Generic_Access_Type;
3111
3112 ---------------------------------
3113 -- Analyze_Generic_Formal_Part --
3114 ---------------------------------
3115
3116 procedure Analyze_Generic_Formal_Part (N : Node_Id) is
3117 Gen_Parm_Decl : Node_Id;
3118
3119 begin
3120 -- The generic formals are processed in the scope of the generic unit,
3121 -- where they are immediately visible. The scope is installed by the
3122 -- caller.
3123
3124 Gen_Parm_Decl := First (Generic_Formal_Declarations (N));
3125 while Present (Gen_Parm_Decl) loop
3126 Analyze (Gen_Parm_Decl);
3127 Next (Gen_Parm_Decl);
3128 end loop;
3129
3130 Generate_Reference_To_Generic_Formals (Current_Scope);
3131 end Analyze_Generic_Formal_Part;
3132
3133 ------------------------------------------
3134 -- Analyze_Generic_Package_Declaration --
3135 ------------------------------------------
3136
3137 procedure Analyze_Generic_Package_Declaration (N : Node_Id) is
3138 GM : constant Ghost_Mode_Type := Ghost_Mode;
3139 Loc : constant Source_Ptr := Sloc (N);
3140 Decls : constant List_Id :=
3141 Visible_Declarations (Specification (N));
3142 Decl : Node_Id;
3143 Id : Entity_Id;
3144 New_N : Node_Id;
3145 Renaming : Node_Id;
3146 Save_Parent : Node_Id;
3147
3148 begin
3149 -- The generic package declaration may be subject to pragma Ghost with
3150 -- policy Ignore. Set the mode now to ensure that any nodes generated
3151 -- during analysis and expansion are properly flagged as ignored Ghost.
3152
3153 Set_Ghost_Mode (N);
3154 Check_SPARK_05_Restriction ("generic is not allowed", N);
3155
3156 -- We introduce a renaming of the enclosing package, to have a usable
3157 -- entity as the prefix of an expanded name for a local entity of the
3158 -- form Par.P.Q, where P is the generic package. This is because a local
3159 -- entity named P may hide it, so that the usual visibility rules in
3160 -- the instance will not resolve properly.
3161
3162 Renaming :=
3163 Make_Package_Renaming_Declaration (Loc,
3164 Defining_Unit_Name =>
3165 Make_Defining_Identifier (Loc,
3166 Chars => New_External_Name (Chars (Defining_Entity (N)), "GH")),
3167 Name =>
3168 Make_Identifier (Loc, Chars (Defining_Entity (N))));
3169
3170 if Present (Decls) then
3171 Decl := First (Decls);
3172 while Present (Decl) and then Nkind (Decl) = N_Pragma loop
3173 Next (Decl);
3174 end loop;
3175
3176 if Present (Decl) then
3177 Insert_Before (Decl, Renaming);
3178 else
3179 Append (Renaming, Visible_Declarations (Specification (N)));
3180 end if;
3181
3182 else
3183 Set_Visible_Declarations (Specification (N), New_List (Renaming));
3184 end if;
3185
3186 -- Create copy of generic unit, and save for instantiation. If the unit
3187 -- is a child unit, do not copy the specifications for the parent, which
3188 -- are not part of the generic tree.
3189
3190 Save_Parent := Parent_Spec (N);
3191 Set_Parent_Spec (N, Empty);
3192
3193 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
3194 Set_Parent_Spec (New_N, Save_Parent);
3195 Rewrite (N, New_N);
3196
3197 -- Once the contents of the generic copy and the template are swapped,
3198 -- do the same for their respective aspect specifications.
3199
3200 Exchange_Aspects (N, New_N);
3201
3202 -- Collect all contract-related source pragmas found within the template
3203 -- and attach them to the contract of the package spec. This contract is
3204 -- used in the capture of global references within annotations.
3205
3206 Create_Generic_Contract (N);
3207
3208 Id := Defining_Entity (N);
3209 Generate_Definition (Id);
3210
3211 -- Expansion is not applied to generic units
3212
3213 Start_Generic;
3214
3215 Enter_Name (Id);
3216 Set_Ekind (Id, E_Generic_Package);
3217 Set_Etype (Id, Standard_Void_Type);
3218
3219 -- A generic package declared within a Ghost region is rendered Ghost
3220 -- (SPARK RM 6.9(2)).
3221
3222 if Ghost_Mode > None then
3223 Set_Is_Ghost_Entity (Id);
3224 end if;
3225
3226 -- Analyze aspects now, so that generated pragmas appear in the
3227 -- declarations before building and analyzing the generic copy.
3228
3229 if Has_Aspects (N) then
3230 Analyze_Aspect_Specifications (N, Id);
3231 end if;
3232
3233 Push_Scope (Id);
3234 Enter_Generic_Scope (Id);
3235 Set_Inner_Instances (Id, New_Elmt_List);
3236
3237 Set_Categorization_From_Pragmas (N);
3238 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3239
3240 -- Link the declaration of the generic homonym in the generic copy to
3241 -- the package it renames, so that it is always resolved properly.
3242
3243 Set_Generic_Homonym (Id, Defining_Unit_Name (Renaming));
3244 Set_Entity (Associated_Node (Name (Renaming)), Id);
3245
3246 -- For a library unit, we have reconstructed the entity for the unit,
3247 -- and must reset it in the library tables.
3248
3249 if Nkind (Parent (N)) = N_Compilation_Unit then
3250 Set_Cunit_Entity (Current_Sem_Unit, Id);
3251 end if;
3252
3253 Analyze_Generic_Formal_Part (N);
3254
3255 -- After processing the generic formals, analysis proceeds as for a
3256 -- non-generic package.
3257
3258 Analyze (Specification (N));
3259
3260 Validate_Categorization_Dependency (N, Id);
3261
3262 End_Generic;
3263
3264 End_Package_Scope (Id);
3265 Exit_Generic_Scope (Id);
3266
3267 if Nkind (Parent (N)) /= N_Compilation_Unit then
3268 Move_Freeze_Nodes (Id, N, Visible_Declarations (Specification (N)));
3269 Move_Freeze_Nodes (Id, N, Private_Declarations (Specification (N)));
3270 Move_Freeze_Nodes (Id, N, Generic_Formal_Declarations (N));
3271
3272 else
3273 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
3274 Validate_RT_RAT_Component (N);
3275
3276 -- If this is a spec without a body, check that generic parameters
3277 -- are referenced.
3278
3279 if not Body_Required (Parent (N)) then
3280 Check_References (Id);
3281 end if;
3282 end if;
3283
3284 -- If there is a specified storage pool in the context, create an
3285 -- aspect on the package declaration, so that it is used in any
3286 -- instance that does not override it.
3287
3288 if Present (Default_Pool) then
3289 declare
3290 ASN : Node_Id;
3291
3292 begin
3293 ASN :=
3294 Make_Aspect_Specification (Loc,
3295 Identifier => Make_Identifier (Loc, Name_Default_Storage_Pool),
3296 Expression => New_Copy (Default_Pool));
3297
3298 if No (Aspect_Specifications (Specification (N))) then
3299 Set_Aspect_Specifications (Specification (N), New_List (ASN));
3300 else
3301 Append (ASN, Aspect_Specifications (Specification (N)));
3302 end if;
3303 end;
3304 end if;
3305
3306 -- Restore the original Ghost mode once analysis and expansion have
3307 -- taken place.
3308
3309 Ghost_Mode := GM;
3310 end Analyze_Generic_Package_Declaration;
3311
3312 --------------------------------------------
3313 -- Analyze_Generic_Subprogram_Declaration --
3314 --------------------------------------------
3315
3316 procedure Analyze_Generic_Subprogram_Declaration (N : Node_Id) is
3317 GM : constant Ghost_Mode_Type := Ghost_Mode;
3318 Formals : List_Id;
3319 Id : Entity_Id;
3320 New_N : Node_Id;
3321 Result_Type : Entity_Id;
3322 Save_Parent : Node_Id;
3323 Spec : Node_Id;
3324 Typ : Entity_Id;
3325
3326 begin
3327 -- The generic subprogram declaration may be subject to pragma Ghost
3328 -- with policy Ignore. Set the mode now to ensure that any nodes
3329 -- generated during analysis and expansion are properly flagged as
3330 -- ignored Ghost.
3331
3332 Set_Ghost_Mode (N);
3333 Check_SPARK_05_Restriction ("generic is not allowed", N);
3334
3335 -- Create copy of generic unit, and save for instantiation. If the unit
3336 -- is a child unit, do not copy the specifications for the parent, which
3337 -- are not part of the generic tree.
3338
3339 Save_Parent := Parent_Spec (N);
3340 Set_Parent_Spec (N, Empty);
3341
3342 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
3343 Set_Parent_Spec (New_N, Save_Parent);
3344 Rewrite (N, New_N);
3345
3346 -- Once the contents of the generic copy and the template are swapped,
3347 -- do the same for their respective aspect specifications.
3348
3349 Exchange_Aspects (N, New_N);
3350
3351 -- Collect all contract-related source pragmas found within the template
3352 -- and attach them to the contract of the subprogram spec. This contract
3353 -- is used in the capture of global references within annotations.
3354
3355 Create_Generic_Contract (N);
3356
3357 Spec := Specification (N);
3358 Id := Defining_Entity (Spec);
3359 Generate_Definition (Id);
3360
3361 if Nkind (Id) = N_Defining_Operator_Symbol then
3362 Error_Msg_N
3363 ("operator symbol not allowed for generic subprogram", Id);
3364 end if;
3365
3366 Start_Generic;
3367
3368 Enter_Name (Id);
3369 Set_Scope_Depth_Value (Id, Scope_Depth (Current_Scope) + 1);
3370
3371 -- Analyze the aspects of the generic copy to ensure that all generated
3372 -- pragmas (if any) perform their semantic effects.
3373
3374 if Has_Aspects (N) then
3375 Analyze_Aspect_Specifications (N, Id);
3376 end if;
3377
3378 Push_Scope (Id);
3379 Enter_Generic_Scope (Id);
3380 Set_Inner_Instances (Id, New_Elmt_List);
3381 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3382
3383 Analyze_Generic_Formal_Part (N);
3384
3385 Formals := Parameter_Specifications (Spec);
3386
3387 if Nkind (Spec) = N_Function_Specification then
3388 Set_Ekind (Id, E_Generic_Function);
3389 else
3390 Set_Ekind (Id, E_Generic_Procedure);
3391 end if;
3392
3393 if Present (Formals) then
3394 Process_Formals (Formals, Spec);
3395 end if;
3396
3397 if Nkind (Spec) = N_Function_Specification then
3398 if Nkind (Result_Definition (Spec)) = N_Access_Definition then
3399 Result_Type := Access_Definition (Spec, Result_Definition (Spec));
3400 Set_Etype (Id, Result_Type);
3401
3402 -- Check restriction imposed by AI05-073: a generic function
3403 -- cannot return an abstract type or an access to such.
3404
3405 -- This is a binding interpretation should it apply to earlier
3406 -- versions of Ada as well as Ada 2012???
3407
3408 if Is_Abstract_Type (Designated_Type (Result_Type))
3409 and then Ada_Version >= Ada_2012
3410 then
3411 Error_Msg_N
3412 ("generic function cannot have an access result "
3413 & "that designates an abstract type", Spec);
3414 end if;
3415
3416 else
3417 Find_Type (Result_Definition (Spec));
3418 Typ := Entity (Result_Definition (Spec));
3419
3420 if Is_Abstract_Type (Typ)
3421 and then Ada_Version >= Ada_2012
3422 then
3423 Error_Msg_N
3424 ("generic function cannot have abstract result type", Spec);
3425 end if;
3426
3427 -- If a null exclusion is imposed on the result type, then create
3428 -- a null-excluding itype (an access subtype) and use it as the
3429 -- function's Etype.
3430
3431 if Is_Access_Type (Typ)
3432 and then Null_Exclusion_Present (Spec)
3433 then
3434 Set_Etype (Id,
3435 Create_Null_Excluding_Itype
3436 (T => Typ,
3437 Related_Nod => Spec,
3438 Scope_Id => Defining_Unit_Name (Spec)));
3439 else
3440 Set_Etype (Id, Typ);
3441 end if;
3442 end if;
3443
3444 else
3445 Set_Etype (Id, Standard_Void_Type);
3446 end if;
3447
3448 -- A generic subprogram declared within a Ghost region is rendered Ghost
3449 -- (SPARK RM 6.9(2)).
3450
3451 if Ghost_Mode > None then
3452 Set_Is_Ghost_Entity (Id);
3453 end if;
3454
3455 -- For a library unit, we have reconstructed the entity for the unit,
3456 -- and must reset it in the library tables. We also make sure that
3457 -- Body_Required is set properly in the original compilation unit node.
3458
3459 if Nkind (Parent (N)) = N_Compilation_Unit then
3460 Set_Cunit_Entity (Current_Sem_Unit, Id);
3461 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
3462 end if;
3463
3464 Set_Categorization_From_Pragmas (N);
3465 Validate_Categorization_Dependency (N, Id);
3466
3467 -- Capture all global references that occur within the profile of the
3468 -- generic subprogram. Aspects are not part of this processing because
3469 -- they must be delayed. If processed now, Save_Global_References will
3470 -- destroy the Associated_Node links and prevent the capture of global
3471 -- references when the contract of the generic subprogram is analyzed.
3472
3473 Save_Global_References (Original_Node (N));
3474
3475 End_Generic;
3476 End_Scope;
3477 Exit_Generic_Scope (Id);
3478 Generate_Reference_To_Formals (Id);
3479
3480 List_Inherited_Pre_Post_Aspects (Id);
3481
3482 -- Restore the original Ghost mode once analysis and expansion have
3483 -- taken place.
3484
3485 Ghost_Mode := GM;
3486 end Analyze_Generic_Subprogram_Declaration;
3487
3488 -----------------------------------
3489 -- Analyze_Package_Instantiation --
3490 -----------------------------------
3491
3492 procedure Analyze_Package_Instantiation (N : Node_Id) is
3493 Loc : constant Source_Ptr := Sloc (N);
3494 Gen_Id : constant Node_Id := Name (N);
3495
3496 Act_Decl : Node_Id;
3497 Act_Decl_Name : Node_Id;
3498 Act_Decl_Id : Entity_Id;
3499 Act_Spec : Node_Id;
3500 Act_Tree : Node_Id;
3501
3502 Gen_Decl : Node_Id;
3503 Gen_Spec : Node_Id;
3504 Gen_Unit : Entity_Id;
3505
3506 Is_Actual_Pack : constant Boolean :=
3507 Is_Internal (Defining_Entity (N));
3508
3509 Env_Installed : Boolean := False;
3510 Parent_Installed : Boolean := False;
3511 Renaming_List : List_Id;
3512 Unit_Renaming : Node_Id;
3513 Needs_Body : Boolean;
3514 Inline_Now : Boolean := False;
3515 Has_Inline_Always : Boolean := False;
3516
3517 Save_IPSM : constant Boolean := Ignore_Pragma_SPARK_Mode;
3518 -- Save flag Ignore_Pragma_SPARK_Mode for restore on exit
3519
3520 Save_SM : constant SPARK_Mode_Type := SPARK_Mode;
3521 Save_SMP : constant Node_Id := SPARK_Mode_Pragma;
3522 -- Save the SPARK_Mode-related data for restore on exit
3523
3524 Save_Style_Check : constant Boolean := Style_Check;
3525 -- Save style check mode for restore on exit
3526
3527 procedure Delay_Descriptors (E : Entity_Id);
3528 -- Delay generation of subprogram descriptors for given entity
3529
3530 function Might_Inline_Subp return Boolean;
3531 -- If inlining is active and the generic contains inlined subprograms,
3532 -- we instantiate the body. This may cause superfluous instantiations,
3533 -- but it is simpler than detecting the need for the body at the point
3534 -- of inlining, when the context of the instance is not available.
3535
3536 -----------------------
3537 -- Delay_Descriptors --
3538 -----------------------
3539
3540 procedure Delay_Descriptors (E : Entity_Id) is
3541 begin
3542 if not Delay_Subprogram_Descriptors (E) then
3543 Set_Delay_Subprogram_Descriptors (E);
3544 Pending_Descriptor.Append (E);
3545 end if;
3546 end Delay_Descriptors;
3547
3548 -----------------------
3549 -- Might_Inline_Subp --
3550 -----------------------
3551
3552 function Might_Inline_Subp return Boolean is
3553 E : Entity_Id;
3554
3555 begin
3556 if not Inline_Processing_Required then
3557 return False;
3558
3559 else
3560 E := First_Entity (Gen_Unit);
3561 while Present (E) loop
3562 if Is_Subprogram (E) and then Is_Inlined (E) then
3563 -- Remember if there are any subprograms with Inline_Always
3564
3565 if Has_Pragma_Inline_Always (E) then
3566 Has_Inline_Always := True;
3567 end if;
3568
3569 return True;
3570 end if;
3571
3572 Next_Entity (E);
3573 end loop;
3574 end if;
3575
3576 return False;
3577 end Might_Inline_Subp;
3578
3579 -- Local declarations
3580
3581 Vis_Prims_List : Elist_Id := No_Elist;
3582 -- List of primitives made temporarily visible in the instantiation
3583 -- to match the visibility of the formal type
3584
3585 -- Start of processing for Analyze_Package_Instantiation
3586
3587 begin
3588 Check_SPARK_05_Restriction ("generic is not allowed", N);
3589
3590 -- Very first thing: check for Text_IO sp[ecial unit in case we are
3591 -- instantiating one of the children of [[Wide_]Wide_]Text_IO.
3592
3593 Check_Text_IO_Special_Unit (Name (N));
3594
3595 -- Make node global for error reporting
3596
3597 Instantiation_Node := N;
3598
3599 -- Turn off style checking in instances. If the check is enabled on the
3600 -- generic unit, a warning in an instance would just be noise. If not
3601 -- enabled on the generic, then a warning in an instance is just wrong.
3602
3603 Style_Check := False;
3604
3605 -- Case of instantiation of a generic package
3606
3607 if Nkind (N) = N_Package_Instantiation then
3608 Act_Decl_Id := New_Copy (Defining_Entity (N));
3609 Set_Comes_From_Source (Act_Decl_Id, True);
3610
3611 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name then
3612 Act_Decl_Name :=
3613 Make_Defining_Program_Unit_Name (Loc,
3614 Name =>
3615 New_Copy_Tree (Name (Defining_Unit_Name (N))),
3616 Defining_Identifier => Act_Decl_Id);
3617 else
3618 Act_Decl_Name := Act_Decl_Id;
3619 end if;
3620
3621 -- Case of instantiation of a formal package
3622
3623 else
3624 Act_Decl_Id := Defining_Identifier (N);
3625 Act_Decl_Name := Act_Decl_Id;
3626 end if;
3627
3628 Generate_Definition (Act_Decl_Id);
3629 Set_Ekind (Act_Decl_Id, E_Package);
3630
3631 -- Initialize list of incomplete actuals before analysis
3632
3633 Set_Incomplete_Actuals (Act_Decl_Id, New_Elmt_List);
3634
3635 Preanalyze_Actuals (N, Act_Decl_Id);
3636
3637 Init_Env;
3638 Env_Installed := True;
3639
3640 -- Reset renaming map for formal types. The mapping is established
3641 -- when analyzing the generic associations, but some mappings are
3642 -- inherited from formal packages of parent units, and these are
3643 -- constructed when the parents are installed.
3644
3645 Generic_Renamings.Set_Last (0);
3646 Generic_Renamings_HTable.Reset;
3647
3648 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
3649 Gen_Unit := Entity (Gen_Id);
3650
3651 -- Verify that it is the name of a generic package
3652
3653 -- A visibility glitch: if the instance is a child unit and the generic
3654 -- is the generic unit of a parent instance (i.e. both the parent and
3655 -- the child units are instances of the same package) the name now
3656 -- denotes the renaming within the parent, not the intended generic
3657 -- unit. See if there is a homonym that is the desired generic. The
3658 -- renaming declaration must be visible inside the instance of the
3659 -- child, but not when analyzing the name in the instantiation itself.
3660
3661 if Ekind (Gen_Unit) = E_Package
3662 and then Present (Renamed_Entity (Gen_Unit))
3663 and then In_Open_Scopes (Renamed_Entity (Gen_Unit))
3664 and then Is_Generic_Instance (Renamed_Entity (Gen_Unit))
3665 and then Present (Homonym (Gen_Unit))
3666 then
3667 Gen_Unit := Homonym (Gen_Unit);
3668 end if;
3669
3670 if Etype (Gen_Unit) = Any_Type then
3671 Restore_Env;
3672 goto Leave;
3673
3674 elsif Ekind (Gen_Unit) /= E_Generic_Package then
3675
3676 -- Ada 2005 (AI-50217): Cannot use instance in limited with_clause
3677
3678 if From_Limited_With (Gen_Unit) then
3679 Error_Msg_N
3680 ("cannot instantiate a limited withed package", Gen_Id);
3681 else
3682 Error_Msg_NE
3683 ("& is not the name of a generic package", Gen_Id, Gen_Unit);
3684 end if;
3685
3686 Restore_Env;
3687 goto Leave;
3688 end if;
3689
3690 if In_Extended_Main_Source_Unit (N) then
3691 Set_Is_Instantiated (Gen_Unit);
3692 Generate_Reference (Gen_Unit, N);
3693
3694 if Present (Renamed_Object (Gen_Unit)) then
3695 Set_Is_Instantiated (Renamed_Object (Gen_Unit));
3696 Generate_Reference (Renamed_Object (Gen_Unit), N);
3697 end if;
3698 end if;
3699
3700 if Nkind (Gen_Id) = N_Identifier
3701 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
3702 then
3703 Error_Msg_NE
3704 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
3705
3706 elsif Nkind (Gen_Id) = N_Expanded_Name
3707 and then Is_Child_Unit (Gen_Unit)
3708 and then Nkind (Prefix (Gen_Id)) = N_Identifier
3709 and then Chars (Act_Decl_Id) = Chars (Prefix (Gen_Id))
3710 then
3711 Error_Msg_N
3712 ("& is hidden within declaration of instance ", Prefix (Gen_Id));
3713 end if;
3714
3715 Set_Entity (Gen_Id, Gen_Unit);
3716
3717 -- If generic is a renaming, get original generic unit
3718
3719 if Present (Renamed_Object (Gen_Unit))
3720 and then Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Package
3721 then
3722 Gen_Unit := Renamed_Object (Gen_Unit);
3723 end if;
3724
3725 -- Verify that there are no circular instantiations
3726
3727 if In_Open_Scopes (Gen_Unit) then
3728 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
3729 Restore_Env;
3730 goto Leave;
3731
3732 elsif Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
3733 Error_Msg_Node_2 := Current_Scope;
3734 Error_Msg_NE
3735 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
3736 Circularity_Detected := True;
3737 Restore_Env;
3738 goto Leave;
3739
3740 else
3741 -- If the context of the instance is subject to SPARK_Mode "off",
3742 -- set the global flag which signals Analyze_Pragma to ignore all
3743 -- SPARK_Mode pragmas within the instance.
3744
3745 if SPARK_Mode = Off then
3746 Ignore_Pragma_SPARK_Mode := True;
3747 end if;
3748
3749 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
3750 Gen_Spec := Specification (Gen_Decl);
3751
3752 -- Initialize renamings map, for error checking, and the list that
3753 -- holds private entities whose views have changed between generic
3754 -- definition and instantiation. If this is the instance created to
3755 -- validate an actual package, the instantiation environment is that
3756 -- of the enclosing instance.
3757
3758 Create_Instantiation_Source (N, Gen_Unit, False, S_Adjustment);
3759
3760 -- Copy original generic tree, to produce text for instantiation
3761
3762 Act_Tree :=
3763 Copy_Generic_Node
3764 (Original_Node (Gen_Decl), Empty, Instantiating => True);
3765
3766 Act_Spec := Specification (Act_Tree);
3767
3768 -- If this is the instance created to validate an actual package,
3769 -- only the formals matter, do not examine the package spec itself.
3770
3771 if Is_Actual_Pack then
3772 Set_Visible_Declarations (Act_Spec, New_List);
3773 Set_Private_Declarations (Act_Spec, New_List);
3774 end if;
3775
3776 Renaming_List :=
3777 Analyze_Associations
3778 (I_Node => N,
3779 Formals => Generic_Formal_Declarations (Act_Tree),
3780 F_Copy => Generic_Formal_Declarations (Gen_Decl));
3781
3782 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
3783
3784 Set_Instance_Env (Gen_Unit, Act_Decl_Id);
3785 Set_Defining_Unit_Name (Act_Spec, Act_Decl_Name);
3786 Set_Is_Generic_Instance (Act_Decl_Id);
3787 Set_Generic_Parent (Act_Spec, Gen_Unit);
3788
3789 -- References to the generic in its own declaration or its body are
3790 -- references to the instance. Add a renaming declaration for the
3791 -- generic unit itself. This declaration, as well as the renaming
3792 -- declarations for the generic formals, must remain private to the
3793 -- unit: the formals, because this is the language semantics, and
3794 -- the unit because its use is an artifact of the implementation.
3795
3796 Unit_Renaming :=
3797 Make_Package_Renaming_Declaration (Loc,
3798 Defining_Unit_Name =>
3799 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
3800 Name => New_Occurrence_Of (Act_Decl_Id, Loc));
3801
3802 Append (Unit_Renaming, Renaming_List);
3803
3804 -- The renaming declarations are the first local declarations of the
3805 -- new unit.
3806
3807 if Is_Non_Empty_List (Visible_Declarations (Act_Spec)) then
3808 Insert_List_Before
3809 (First (Visible_Declarations (Act_Spec)), Renaming_List);
3810 else
3811 Set_Visible_Declarations (Act_Spec, Renaming_List);
3812 end if;
3813
3814 Act_Decl := Make_Package_Declaration (Loc, Specification => Act_Spec);
3815
3816 -- Propagate the aspect specifications from the package declaration
3817 -- template to the instantiated version of the package declaration.
3818
3819 if Has_Aspects (Act_Tree) then
3820 Set_Aspect_Specifications (Act_Decl,
3821 New_Copy_List_Tree (Aspect_Specifications (Act_Tree)));
3822 end if;
3823
3824 -- The generic may have a generated Default_Storage_Pool aspect,
3825 -- set at the point of generic declaration. If the instance has
3826 -- that aspect, it overrides the one inherited from the generic.
3827
3828 if Has_Aspects (Gen_Spec) then
3829 if No (Aspect_Specifications (N)) then
3830 Set_Aspect_Specifications (N,
3831 (New_Copy_List_Tree
3832 (Aspect_Specifications (Gen_Spec))));
3833
3834 else
3835 declare
3836 ASN1, ASN2 : Node_Id;
3837
3838 begin
3839 ASN1 := First (Aspect_Specifications (N));
3840 while Present (ASN1) loop
3841 if Chars (Identifier (ASN1)) = Name_Default_Storage_Pool
3842 then
3843 -- If generic carries a default storage pool, remove
3844 -- it in favor of the instance one.
3845
3846 ASN2 := First (Aspect_Specifications (Gen_Spec));
3847 while Present (ASN2) loop
3848 if Chars (Identifier (ASN2)) =
3849 Name_Default_Storage_Pool
3850 then
3851 Remove (ASN2);
3852 exit;
3853 end if;
3854
3855 Next (ASN2);
3856 end loop;
3857 end if;
3858
3859 Next (ASN1);
3860 end loop;
3861
3862 Prepend_List_To (Aspect_Specifications (N),
3863 (New_Copy_List_Tree
3864 (Aspect_Specifications (Gen_Spec))));
3865 end;
3866 end if;
3867 end if;
3868
3869 -- Save the instantiation node, for subsequent instantiation of the
3870 -- body, if there is one and we are generating code for the current
3871 -- unit. Mark unit as having a body (avoids premature error message).
3872
3873 -- We instantiate the body if we are generating code, if we are
3874 -- generating cross-reference information, or if we are building
3875 -- trees for ASIS use or GNATprove use.
3876
3877 declare
3878 Enclosing_Body_Present : Boolean := False;
3879 -- If the generic unit is not a compilation unit, then a body may
3880 -- be present in its parent even if none is required. We create a
3881 -- tentative pending instantiation for the body, which will be
3882 -- discarded if none is actually present.
3883
3884 Scop : Entity_Id;
3885
3886 begin
3887 if Scope (Gen_Unit) /= Standard_Standard
3888 and then not Is_Child_Unit (Gen_Unit)
3889 then
3890 Scop := Scope (Gen_Unit);
3891 while Present (Scop) and then Scop /= Standard_Standard loop
3892 if Unit_Requires_Body (Scop) then
3893 Enclosing_Body_Present := True;
3894 exit;
3895
3896 elsif In_Open_Scopes (Scop)
3897 and then In_Package_Body (Scop)
3898 then
3899 Enclosing_Body_Present := True;
3900 exit;
3901 end if;
3902
3903 exit when Is_Compilation_Unit (Scop);
3904 Scop := Scope (Scop);
3905 end loop;
3906 end if;
3907
3908 -- If front-end inlining is enabled or there are any subprograms
3909 -- marked with Inline_Always, and this is a unit for which code
3910 -- will be generated, we instantiate the body at once.
3911
3912 -- This is done if the instance is not the main unit, and if the
3913 -- generic is not a child unit of another generic, to avoid scope
3914 -- problems and the reinstallation of parent instances.
3915
3916 if Expander_Active
3917 and then (not Is_Child_Unit (Gen_Unit)
3918 or else not Is_Generic_Unit (Scope (Gen_Unit)))
3919 and then Might_Inline_Subp
3920 and then not Is_Actual_Pack
3921 then
3922 if not Back_End_Inlining
3923 and then (Front_End_Inlining or else Has_Inline_Always)
3924 and then (Is_In_Main_Unit (N)
3925 or else In_Main_Context (Current_Scope))
3926 and then Nkind (Parent (N)) /= N_Compilation_Unit
3927 then
3928 Inline_Now := True;
3929
3930 -- In configurable_run_time mode we force the inlining of
3931 -- predefined subprograms marked Inline_Always, to minimize
3932 -- the use of the run-time library.
3933
3934 elsif Is_Predefined_File_Name
3935 (Unit_File_Name (Get_Source_Unit (Gen_Decl)))
3936 and then Configurable_Run_Time_Mode
3937 and then Nkind (Parent (N)) /= N_Compilation_Unit
3938 then
3939 Inline_Now := True;
3940 end if;
3941
3942 -- If the current scope is itself an instance within a child
3943 -- unit, there will be duplications in the scope stack, and the
3944 -- unstacking mechanism in Inline_Instance_Body will fail.
3945 -- This loses some rare cases of optimization, and might be
3946 -- improved some day, if we can find a proper abstraction for
3947 -- "the complete compilation context" that can be saved and
3948 -- restored. ???
3949
3950 if Is_Generic_Instance (Current_Scope) then
3951 declare
3952 Curr_Unit : constant Entity_Id :=
3953 Cunit_Entity (Current_Sem_Unit);
3954 begin
3955 if Curr_Unit /= Current_Scope
3956 and then Is_Child_Unit (Curr_Unit)
3957 then
3958 Inline_Now := False;
3959 end if;
3960 end;
3961 end if;
3962 end if;
3963
3964 Needs_Body :=
3965 (Unit_Requires_Body (Gen_Unit)
3966 or else Enclosing_Body_Present
3967 or else Present (Corresponding_Body (Gen_Decl)))
3968 and then (Is_In_Main_Unit (N) or else Might_Inline_Subp)
3969 and then not Is_Actual_Pack
3970 and then not Inline_Now
3971 and then (Operating_Mode = Generate_Code
3972
3973 -- Need comment for this check ???
3974
3975 or else (Operating_Mode = Check_Semantics
3976 and then (ASIS_Mode or GNATprove_Mode)));
3977
3978 -- If front-end inlining is enabled or there are any subprograms
3979 -- marked with Inline_Always, do not instantiate body when within
3980 -- a generic context.
3981
3982 if ((Front_End_Inlining or else Has_Inline_Always)
3983 and then not Expander_Active)
3984 or else Is_Generic_Unit (Cunit_Entity (Main_Unit))
3985 then
3986 Needs_Body := False;
3987 end if;
3988
3989 -- If the current context is generic, and the package being
3990 -- instantiated is declared within a formal package, there is no
3991 -- body to instantiate until the enclosing generic is instantiated
3992 -- and there is an actual for the formal package. If the formal
3993 -- package has parameters, we build a regular package instance for
3994 -- it, that precedes the original formal package declaration.
3995
3996 if In_Open_Scopes (Scope (Scope (Gen_Unit))) then
3997 declare
3998 Decl : constant Node_Id :=
3999 Original_Node
4000 (Unit_Declaration_Node (Scope (Gen_Unit)));
4001 begin
4002 if Nkind (Decl) = N_Formal_Package_Declaration
4003 or else (Nkind (Decl) = N_Package_Declaration
4004 and then Is_List_Member (Decl)
4005 and then Present (Next (Decl))
4006 and then
4007 Nkind (Next (Decl)) =
4008 N_Formal_Package_Declaration)
4009 then
4010 Needs_Body := False;
4011 end if;
4012 end;
4013 end if;
4014 end;
4015
4016 -- For RCI unit calling stubs, we omit the instance body if the
4017 -- instance is the RCI library unit itself.
4018
4019 -- However there is a special case for nested instances: in this case
4020 -- we do generate the instance body, as it might be required, e.g.
4021 -- because it provides stream attributes for some type used in the
4022 -- profile of a remote subprogram. This is consistent with 12.3(12),
4023 -- which indicates that the instance body occurs at the place of the
4024 -- instantiation, and thus is part of the RCI declaration, which is
4025 -- present on all client partitions (this is E.2.3(18)).
4026
4027 -- Note that AI12-0002 may make it illegal at some point to have
4028 -- stream attributes defined in an RCI unit, in which case this
4029 -- special case will become unnecessary. In the meantime, there
4030 -- is known application code in production that depends on this
4031 -- being possible, so we definitely cannot eliminate the body in
4032 -- the case of nested instances for the time being.
4033
4034 -- When we generate a nested instance body, calling stubs for any
4035 -- relevant subprogram will be be inserted immediately after the
4036 -- subprogram declarations, and will take precedence over the
4037 -- subsequent (original) body. (The stub and original body will be
4038 -- complete homographs, but this is permitted in an instance).
4039 -- (Could we do better and remove the original body???)
4040
4041 if Distribution_Stub_Mode = Generate_Caller_Stub_Body
4042 and then Comes_From_Source (N)
4043 and then Nkind (Parent (N)) = N_Compilation_Unit
4044 then
4045 Needs_Body := False;
4046 end if;
4047
4048 if Needs_Body then
4049
4050 -- Here is a defence against a ludicrous number of instantiations
4051 -- caused by a circular set of instantiation attempts.
4052
4053 if Pending_Instantiations.Last > Maximum_Instantiations then
4054 Error_Msg_Uint_1 := UI_From_Int (Maximum_Instantiations);
4055 Error_Msg_N ("too many instantiations, exceeds max of^", N);
4056 Error_Msg_N ("\limit can be changed using -gnateinn switch", N);
4057 raise Unrecoverable_Error;
4058 end if;
4059
4060 -- Indicate that the enclosing scopes contain an instantiation,
4061 -- and that cleanup actions should be delayed until after the
4062 -- instance body is expanded.
4063
4064 Check_Forward_Instantiation (Gen_Decl);
4065 if Nkind (N) = N_Package_Instantiation then
4066 declare
4067 Enclosing_Master : Entity_Id;
4068
4069 begin
4070 -- Loop to search enclosing masters
4071
4072 Enclosing_Master := Current_Scope;
4073 Scope_Loop : while Enclosing_Master /= Standard_Standard loop
4074 if Ekind (Enclosing_Master) = E_Package then
4075 if Is_Compilation_Unit (Enclosing_Master) then
4076 if In_Package_Body (Enclosing_Master) then
4077 Delay_Descriptors
4078 (Body_Entity (Enclosing_Master));
4079 else
4080 Delay_Descriptors
4081 (Enclosing_Master);
4082 end if;
4083
4084 exit Scope_Loop;
4085
4086 else
4087 Enclosing_Master := Scope (Enclosing_Master);
4088 end if;
4089
4090 elsif Is_Generic_Unit (Enclosing_Master)
4091 or else Ekind (Enclosing_Master) = E_Void
4092 then
4093 -- Cleanup actions will eventually be performed on the
4094 -- enclosing subprogram or package instance, if any.
4095 -- Enclosing scope is void in the formal part of a
4096 -- generic subprogram.
4097
4098 exit Scope_Loop;
4099
4100 else
4101 if Ekind (Enclosing_Master) = E_Entry
4102 and then
4103 Ekind (Scope (Enclosing_Master)) = E_Protected_Type
4104 then
4105 if not Expander_Active then
4106 exit Scope_Loop;
4107 else
4108 Enclosing_Master :=
4109 Protected_Body_Subprogram (Enclosing_Master);
4110 end if;
4111 end if;
4112
4113 Set_Delay_Cleanups (Enclosing_Master);
4114
4115 while Ekind (Enclosing_Master) = E_Block loop
4116 Enclosing_Master := Scope (Enclosing_Master);
4117 end loop;
4118
4119 if Is_Subprogram (Enclosing_Master) then
4120 Delay_Descriptors (Enclosing_Master);
4121
4122 elsif Is_Task_Type (Enclosing_Master) then
4123 declare
4124 TBP : constant Node_Id :=
4125 Get_Task_Body_Procedure
4126 (Enclosing_Master);
4127 begin
4128 if Present (TBP) then
4129 Delay_Descriptors (TBP);
4130 Set_Delay_Cleanups (TBP);
4131 end if;
4132 end;
4133 end if;
4134
4135 exit Scope_Loop;
4136 end if;
4137 end loop Scope_Loop;
4138 end;
4139
4140 -- Make entry in table
4141
4142 Pending_Instantiations.Append
4143 ((Inst_Node => N,
4144 Act_Decl => Act_Decl,
4145 Expander_Status => Expander_Active,
4146 Current_Sem_Unit => Current_Sem_Unit,
4147 Scope_Suppress => Scope_Suppress,
4148 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
4149 Version => Ada_Version,
4150 Version_Pragma => Ada_Version_Pragma,
4151 Warnings => Save_Warnings,
4152 SPARK_Mode => SPARK_Mode,
4153 SPARK_Mode_Pragma => SPARK_Mode_Pragma));
4154 end if;
4155 end if;
4156
4157 Set_Categorization_From_Pragmas (Act_Decl);
4158
4159 if Parent_Installed then
4160 Hide_Current_Scope;
4161 end if;
4162
4163 Set_Instance_Spec (N, Act_Decl);
4164
4165 -- If not a compilation unit, insert the package declaration before
4166 -- the original instantiation node.
4167
4168 if Nkind (Parent (N)) /= N_Compilation_Unit then
4169 Mark_Rewrite_Insertion (Act_Decl);
4170 Insert_Before (N, Act_Decl);
4171
4172 if Has_Aspects (N) then
4173 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4174
4175 -- The pragma created for a Default_Storage_Pool aspect must
4176 -- appear ahead of the declarations in the instance spec.
4177 -- Analysis has placed it after the instance node, so remove
4178 -- it and reinsert it properly now.
4179
4180 declare
4181 ASN : constant Node_Id := First (Aspect_Specifications (N));
4182 A_Name : constant Name_Id := Chars (Identifier (ASN));
4183 Decl : Node_Id;
4184
4185 begin
4186 if A_Name = Name_Default_Storage_Pool then
4187 if No (Visible_Declarations (Act_Spec)) then
4188 Set_Visible_Declarations (Act_Spec, New_List);
4189 end if;
4190
4191 Decl := Next (N);
4192 while Present (Decl) loop
4193 if Nkind (Decl) = N_Pragma then
4194 Remove (Decl);
4195 Prepend (Decl, Visible_Declarations (Act_Spec));
4196 exit;
4197 end if;
4198
4199 Next (Decl);
4200 end loop;
4201 end if;
4202 end;
4203 end if;
4204
4205 Analyze (Act_Decl);
4206
4207 -- For an instantiation that is a compilation unit, place
4208 -- declaration on current node so context is complete for analysis
4209 -- (including nested instantiations). If this is the main unit,
4210 -- the declaration eventually replaces the instantiation node.
4211 -- If the instance body is created later, it replaces the
4212 -- instance node, and the declaration is attached to it
4213 -- (see Build_Instance_Compilation_Unit_Nodes).
4214
4215 else
4216 if Cunit_Entity (Current_Sem_Unit) = Defining_Entity (N) then
4217
4218 -- The entity for the current unit is the newly created one,
4219 -- and all semantic information is attached to it.
4220
4221 Set_Cunit_Entity (Current_Sem_Unit, Act_Decl_Id);
4222
4223 -- If this is the main unit, replace the main entity as well
4224
4225 if Current_Sem_Unit = Main_Unit then
4226 Main_Unit_Entity := Act_Decl_Id;
4227 end if;
4228 end if;
4229
4230 Set_Unit (Parent (N), Act_Decl);
4231 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
4232 Set_Package_Instantiation (Act_Decl_Id, N);
4233
4234 -- Process aspect specifications of the instance node, if any, to
4235 -- take into account categorization pragmas before analyzing the
4236 -- instance.
4237
4238 if Has_Aspects (N) then
4239 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4240 end if;
4241
4242 Analyze (Act_Decl);
4243 Set_Unit (Parent (N), N);
4244 Set_Body_Required (Parent (N), False);
4245
4246 -- We never need elaboration checks on instantiations, since by
4247 -- definition, the body instantiation is elaborated at the same
4248 -- time as the spec instantiation.
4249
4250 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
4251 Set_Kill_Elaboration_Checks (Act_Decl_Id);
4252 end if;
4253
4254 Check_Elab_Instantiation (N);
4255
4256 if ABE_Is_Certain (N) and then Needs_Body then
4257 Pending_Instantiations.Decrement_Last;
4258 end if;
4259
4260 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
4261
4262 Set_First_Private_Entity (Defining_Unit_Name (Unit_Renaming),
4263 First_Private_Entity (Act_Decl_Id));
4264
4265 -- If the instantiation will receive a body, the unit will be
4266 -- transformed into a package body, and receive its own elaboration
4267 -- entity. Otherwise, the nature of the unit is now a package
4268 -- declaration.
4269
4270 if Nkind (Parent (N)) = N_Compilation_Unit
4271 and then not Needs_Body
4272 then
4273 Rewrite (N, Act_Decl);
4274 end if;
4275
4276 if Present (Corresponding_Body (Gen_Decl))
4277 or else Unit_Requires_Body (Gen_Unit)
4278 then
4279 Set_Has_Completion (Act_Decl_Id);
4280 end if;
4281
4282 Check_Formal_Packages (Act_Decl_Id);
4283
4284 Restore_Hidden_Primitives (Vis_Prims_List);
4285 Restore_Private_Views (Act_Decl_Id);
4286
4287 Inherit_Context (Gen_Decl, N);
4288
4289 if Parent_Installed then
4290 Remove_Parent;
4291 end if;
4292
4293 Restore_Env;
4294 Env_Installed := False;
4295 end if;
4296
4297 Validate_Categorization_Dependency (N, Act_Decl_Id);
4298
4299 -- There used to be a check here to prevent instantiations in local
4300 -- contexts if the No_Local_Allocators restriction was active. This
4301 -- check was removed by a binding interpretation in AI-95-00130/07,
4302 -- but we retain the code for documentation purposes.
4303
4304 -- if Ekind (Act_Decl_Id) /= E_Void
4305 -- and then not Is_Library_Level_Entity (Act_Decl_Id)
4306 -- then
4307 -- Check_Restriction (No_Local_Allocators, N);
4308 -- end if;
4309
4310 if Inline_Now then
4311 Inline_Instance_Body (N, Gen_Unit, Act_Decl);
4312 end if;
4313
4314 -- The following is a tree patch for ASIS: ASIS needs separate nodes to
4315 -- be used as defining identifiers for a formal package and for the
4316 -- corresponding expanded package.
4317
4318 if Nkind (N) = N_Formal_Package_Declaration then
4319 Act_Decl_Id := New_Copy (Defining_Entity (N));
4320 Set_Comes_From_Source (Act_Decl_Id, True);
4321 Set_Is_Generic_Instance (Act_Decl_Id, False);
4322 Set_Defining_Identifier (N, Act_Decl_Id);
4323 end if;
4324
4325 Ignore_Pragma_SPARK_Mode := Save_IPSM;
4326 SPARK_Mode := Save_SM;
4327 SPARK_Mode_Pragma := Save_SMP;
4328 Style_Check := Save_Style_Check;
4329
4330 if SPARK_Mode = On then
4331 Dynamic_Elaboration_Checks := False;
4332 end if;
4333
4334 -- Check that if N is an instantiation of System.Dim_Float_IO or
4335 -- System.Dim_Integer_IO, the formal type has a dimension system.
4336
4337 if Nkind (N) = N_Package_Instantiation
4338 and then Is_Dim_IO_Package_Instantiation (N)
4339 then
4340 declare
4341 Assoc : constant Node_Id := First (Generic_Associations (N));
4342 begin
4343 if not Has_Dimension_System
4344 (Etype (Explicit_Generic_Actual_Parameter (Assoc)))
4345 then
4346 Error_Msg_N ("type with a dimension system expected", Assoc);
4347 end if;
4348 end;
4349 end if;
4350
4351 <<Leave>>
4352 if Has_Aspects (N) and then Nkind (Parent (N)) /= N_Compilation_Unit then
4353 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4354 end if;
4355
4356 exception
4357 when Instantiation_Error =>
4358 if Parent_Installed then
4359 Remove_Parent;
4360 end if;
4361
4362 if Env_Installed then
4363 Restore_Env;
4364 end if;
4365
4366 Ignore_Pragma_SPARK_Mode := Save_IPSM;
4367 SPARK_Mode := Save_SM;
4368 SPARK_Mode_Pragma := Save_SMP;
4369 Style_Check := Save_Style_Check;
4370
4371 if SPARK_Mode = On then
4372 Dynamic_Elaboration_Checks := False;
4373 end if;
4374 end Analyze_Package_Instantiation;
4375
4376 --------------------------
4377 -- Inline_Instance_Body --
4378 --------------------------
4379
4380 procedure Inline_Instance_Body
4381 (N : Node_Id;
4382 Gen_Unit : Entity_Id;
4383 Act_Decl : Node_Id)
4384 is
4385 Curr_Comp : constant Node_Id := Cunit (Current_Sem_Unit);
4386 Curr_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
4387 Gen_Comp : constant Entity_Id :=
4388 Cunit_Entity (Get_Source_Unit (Gen_Unit));
4389
4390 Save_SM : constant SPARK_Mode_Type := SPARK_Mode;
4391 Save_SMP : constant Node_Id := SPARK_Mode_Pragma;
4392 -- Save all SPARK_Mode-related attributes as removing enclosing scopes
4393 -- to provide a clean environment for analysis of the inlined body will
4394 -- eliminate any previously set SPARK_Mode.
4395
4396 Scope_Stack_Depth : constant Int :=
4397 Scope_Stack.Last - Scope_Stack.First + 1;
4398
4399 Use_Clauses : array (1 .. Scope_Stack_Depth) of Node_Id;
4400 Instances : array (1 .. Scope_Stack_Depth) of Entity_Id;
4401 Inner_Scopes : array (1 .. Scope_Stack_Depth) of Entity_Id;
4402 Curr_Scope : Entity_Id := Empty;
4403 List : Elist_Id;
4404 Num_Inner : Int := 0;
4405 Num_Scopes : Int := 0;
4406 N_Instances : Int := 0;
4407 Removed : Boolean := False;
4408 S : Entity_Id;
4409 Vis : Boolean;
4410
4411 begin
4412 -- Case of generic unit defined in another unit. We must remove the
4413 -- complete context of the current unit to install that of the generic.
4414
4415 if Gen_Comp /= Cunit_Entity (Current_Sem_Unit) then
4416
4417 -- Add some comments for the following two loops ???
4418
4419 S := Current_Scope;
4420 while Present (S) and then S /= Standard_Standard loop
4421 loop
4422 Num_Scopes := Num_Scopes + 1;
4423
4424 Use_Clauses (Num_Scopes) :=
4425 (Scope_Stack.Table
4426 (Scope_Stack.Last - Num_Scopes + 1).
4427 First_Use_Clause);
4428 End_Use_Clauses (Use_Clauses (Num_Scopes));
4429
4430 exit when Scope_Stack.Last - Num_Scopes + 1 = Scope_Stack.First
4431 or else Scope_Stack.Table
4432 (Scope_Stack.Last - Num_Scopes).Entity = Scope (S);
4433 end loop;
4434
4435 exit when Is_Generic_Instance (S)
4436 and then (In_Package_Body (S)
4437 or else Ekind (S) = E_Procedure
4438 or else Ekind (S) = E_Function);
4439 S := Scope (S);
4440 end loop;
4441
4442 Vis := Is_Immediately_Visible (Gen_Comp);
4443
4444 -- Find and save all enclosing instances
4445
4446 S := Current_Scope;
4447
4448 while Present (S)
4449 and then S /= Standard_Standard
4450 loop
4451 if Is_Generic_Instance (S) then
4452 N_Instances := N_Instances + 1;
4453 Instances (N_Instances) := S;
4454
4455 exit when In_Package_Body (S);
4456 end if;
4457
4458 S := Scope (S);
4459 end loop;
4460
4461 -- Remove context of current compilation unit, unless we are within a
4462 -- nested package instantiation, in which case the context has been
4463 -- removed previously.
4464
4465 -- If current scope is the body of a child unit, remove context of
4466 -- spec as well. If an enclosing scope is an instance body, the
4467 -- context has already been removed, but the entities in the body
4468 -- must be made invisible as well.
4469
4470 S := Current_Scope;
4471 while Present (S) and then S /= Standard_Standard loop
4472 if Is_Generic_Instance (S)
4473 and then (In_Package_Body (S)
4474 or else Ekind_In (S, E_Procedure, E_Function))
4475 then
4476 -- We still have to remove the entities of the enclosing
4477 -- instance from direct visibility.
4478
4479 declare
4480 E : Entity_Id;
4481 begin
4482 E := First_Entity (S);
4483 while Present (E) loop
4484 Set_Is_Immediately_Visible (E, False);
4485 Next_Entity (E);
4486 end loop;
4487 end;
4488
4489 exit;
4490 end if;
4491
4492 if S = Curr_Unit
4493 or else (Ekind (Curr_Unit) = E_Package_Body
4494 and then S = Spec_Entity (Curr_Unit))
4495 or else (Ekind (Curr_Unit) = E_Subprogram_Body
4496 and then S = Corresponding_Spec
4497 (Unit_Declaration_Node (Curr_Unit)))
4498 then
4499 Removed := True;
4500
4501 -- Remove entities in current scopes from visibility, so that
4502 -- instance body is compiled in a clean environment.
4503
4504 List := Save_Scope_Stack (Handle_Use => False);
4505
4506 if Is_Child_Unit (S) then
4507
4508 -- Remove child unit from stack, as well as inner scopes.
4509 -- Removing the context of a child unit removes parent units
4510 -- as well.
4511
4512 while Current_Scope /= S loop
4513 Num_Inner := Num_Inner + 1;
4514 Inner_Scopes (Num_Inner) := Current_Scope;
4515 Pop_Scope;
4516 end loop;
4517
4518 Pop_Scope;
4519 Remove_Context (Curr_Comp);
4520 Curr_Scope := S;
4521
4522 else
4523 Remove_Context (Curr_Comp);
4524 end if;
4525
4526 if Ekind (Curr_Unit) = E_Package_Body then
4527 Remove_Context (Library_Unit (Curr_Comp));
4528 end if;
4529 end if;
4530
4531 S := Scope (S);
4532 end loop;
4533
4534 pragma Assert (Num_Inner < Num_Scopes);
4535
4536 -- The inlined package body must be analyzed with the SPARK_Mode of
4537 -- the enclosing context, otherwise the body may cause bogus errors
4538 -- if a configuration SPARK_Mode pragma in in effect.
4539
4540 Push_Scope (Standard_Standard);
4541 Scope_Stack.Table (Scope_Stack.Last).Is_Active_Stack_Base := True;
4542 Instantiate_Package_Body
4543 (Body_Info =>
4544 ((Inst_Node => N,
4545 Act_Decl => Act_Decl,
4546 Expander_Status => Expander_Active,
4547 Current_Sem_Unit => Current_Sem_Unit,
4548 Scope_Suppress => Scope_Suppress,
4549 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
4550 Version => Ada_Version,
4551 Version_Pragma => Ada_Version_Pragma,
4552 Warnings => Save_Warnings,
4553 SPARK_Mode => Save_SM,
4554 SPARK_Mode_Pragma => Save_SMP)),
4555 Inlined_Body => True);
4556
4557 Pop_Scope;
4558
4559 -- Restore context
4560
4561 Set_Is_Immediately_Visible (Gen_Comp, Vis);
4562
4563 -- Reset Generic_Instance flag so that use clauses can be installed
4564 -- in the proper order. (See Use_One_Package for effect of enclosing
4565 -- instances on processing of use clauses).
4566
4567 for J in 1 .. N_Instances loop
4568 Set_Is_Generic_Instance (Instances (J), False);
4569 end loop;
4570
4571 if Removed then
4572 Install_Context (Curr_Comp);
4573
4574 if Present (Curr_Scope)
4575 and then Is_Child_Unit (Curr_Scope)
4576 then
4577 Push_Scope (Curr_Scope);
4578 Set_Is_Immediately_Visible (Curr_Scope);
4579
4580 -- Finally, restore inner scopes as well
4581
4582 for J in reverse 1 .. Num_Inner loop
4583 Push_Scope (Inner_Scopes (J));
4584 end loop;
4585 end if;
4586
4587 Restore_Scope_Stack (List, Handle_Use => False);
4588
4589 if Present (Curr_Scope)
4590 and then
4591 (In_Private_Part (Curr_Scope)
4592 or else In_Package_Body (Curr_Scope))
4593 then
4594 -- Install private declaration of ancestor units, which are
4595 -- currently available. Restore_Scope_Stack and Install_Context
4596 -- only install the visible part of parents.
4597
4598 declare
4599 Par : Entity_Id;
4600 begin
4601 Par := Scope (Curr_Scope);
4602 while (Present (Par)) and then Par /= Standard_Standard loop
4603 Install_Private_Declarations (Par);
4604 Par := Scope (Par);
4605 end loop;
4606 end;
4607 end if;
4608 end if;
4609
4610 -- Restore use clauses. For a child unit, use clauses in the parents
4611 -- are restored when installing the context, so only those in inner
4612 -- scopes (and those local to the child unit itself) need to be
4613 -- installed explicitly.
4614
4615 if Is_Child_Unit (Curr_Unit) and then Removed then
4616 for J in reverse 1 .. Num_Inner + 1 loop
4617 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
4618 Use_Clauses (J);
4619 Install_Use_Clauses (Use_Clauses (J));
4620 end loop;
4621
4622 else
4623 for J in reverse 1 .. Num_Scopes loop
4624 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
4625 Use_Clauses (J);
4626 Install_Use_Clauses (Use_Clauses (J));
4627 end loop;
4628 end if;
4629
4630 -- Restore status of instances. If one of them is a body, make its
4631 -- local entities visible again.
4632
4633 declare
4634 E : Entity_Id;
4635 Inst : Entity_Id;
4636
4637 begin
4638 for J in 1 .. N_Instances loop
4639 Inst := Instances (J);
4640 Set_Is_Generic_Instance (Inst, True);
4641
4642 if In_Package_Body (Inst)
4643 or else Ekind_In (S, E_Procedure, E_Function)
4644 then
4645 E := First_Entity (Instances (J));
4646 while Present (E) loop
4647 Set_Is_Immediately_Visible (E);
4648 Next_Entity (E);
4649 end loop;
4650 end if;
4651 end loop;
4652 end;
4653
4654 -- If generic unit is in current unit, current context is correct. Note
4655 -- that the context is guaranteed to carry the correct SPARK_Mode as no
4656 -- enclosing scopes were removed.
4657
4658 else
4659 Instantiate_Package_Body
4660 (Body_Info =>
4661 ((Inst_Node => N,
4662 Act_Decl => Act_Decl,
4663 Expander_Status => Expander_Active,
4664 Current_Sem_Unit => Current_Sem_Unit,
4665 Scope_Suppress => Scope_Suppress,
4666 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
4667 Version => Ada_Version,
4668 Version_Pragma => Ada_Version_Pragma,
4669 Warnings => Save_Warnings,
4670 SPARK_Mode => SPARK_Mode,
4671 SPARK_Mode_Pragma => SPARK_Mode_Pragma)),
4672 Inlined_Body => True);
4673 end if;
4674 end Inline_Instance_Body;
4675
4676 -------------------------------------
4677 -- Analyze_Procedure_Instantiation --
4678 -------------------------------------
4679
4680 procedure Analyze_Procedure_Instantiation (N : Node_Id) is
4681 begin
4682 Analyze_Subprogram_Instantiation (N, E_Procedure);
4683 end Analyze_Procedure_Instantiation;
4684
4685 -----------------------------------
4686 -- Need_Subprogram_Instance_Body --
4687 -----------------------------------
4688
4689 function Need_Subprogram_Instance_Body
4690 (N : Node_Id;
4691 Subp : Entity_Id) return Boolean
4692 is
4693 begin
4694 -- Must be inlined (or inlined renaming)
4695
4696 if (Is_In_Main_Unit (N)
4697 or else Is_Inlined (Subp)
4698 or else Is_Inlined (Alias (Subp)))
4699
4700 -- Must be generating code or analyzing code in ASIS/GNATprove mode
4701
4702 and then (Operating_Mode = Generate_Code
4703 or else (Operating_Mode = Check_Semantics
4704 and then (ASIS_Mode or GNATprove_Mode)))
4705
4706 -- The body is needed when generating code (full expansion), in ASIS
4707 -- mode for other tools, and in GNATprove mode (special expansion) for
4708 -- formal verification of the body itself.
4709
4710 and then (Expander_Active or ASIS_Mode or GNATprove_Mode)
4711
4712 -- No point in inlining if ABE is inevitable
4713
4714 and then not ABE_Is_Certain (N)
4715
4716 -- Or if subprogram is eliminated
4717
4718 and then not Is_Eliminated (Subp)
4719 then
4720 Pending_Instantiations.Append
4721 ((Inst_Node => N,
4722 Act_Decl => Unit_Declaration_Node (Subp),
4723 Expander_Status => Expander_Active,
4724 Current_Sem_Unit => Current_Sem_Unit,
4725 Scope_Suppress => Scope_Suppress,
4726 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
4727 Version => Ada_Version,
4728 Version_Pragma => Ada_Version_Pragma,
4729 Warnings => Save_Warnings,
4730 SPARK_Mode => SPARK_Mode,
4731 SPARK_Mode_Pragma => SPARK_Mode_Pragma));
4732 return True;
4733
4734 -- Here if not inlined, or we ignore the inlining
4735
4736 else
4737 return False;
4738 end if;
4739 end Need_Subprogram_Instance_Body;
4740
4741 --------------------------------------
4742 -- Analyze_Subprogram_Instantiation --
4743 --------------------------------------
4744
4745 procedure Analyze_Subprogram_Instantiation
4746 (N : Node_Id;
4747 K : Entity_Kind)
4748 is
4749 Loc : constant Source_Ptr := Sloc (N);
4750 Gen_Id : constant Node_Id := Name (N);
4751
4752 Anon_Id : constant Entity_Id :=
4753 Make_Defining_Identifier (Sloc (Defining_Entity (N)),
4754 Chars => New_External_Name
4755 (Chars (Defining_Entity (N)), 'R'));
4756
4757 Act_Decl_Id : Entity_Id;
4758 Act_Decl : Node_Id;
4759 Act_Spec : Node_Id;
4760 Act_Tree : Node_Id;
4761
4762 Env_Installed : Boolean := False;
4763 Gen_Unit : Entity_Id;
4764 Gen_Decl : Node_Id;
4765 Pack_Id : Entity_Id;
4766 Parent_Installed : Boolean := False;
4767
4768 Renaming_List : List_Id;
4769 -- The list of declarations that link formals and actuals of the
4770 -- instance. These are subtype declarations for formal types, and
4771 -- renaming declarations for other formals. The subprogram declaration
4772 -- for the instance is then appended to the list, and the last item on
4773 -- the list is the renaming declaration for the instance.
4774
4775 procedure Analyze_Instance_And_Renamings;
4776 -- The instance must be analyzed in a context that includes the mappings
4777 -- of generic parameters into actuals. We create a package declaration
4778 -- for this purpose, and a subprogram with an internal name within the
4779 -- package. The subprogram instance is simply an alias for the internal
4780 -- subprogram, declared in the current scope.
4781
4782 procedure Build_Subprogram_Renaming;
4783 -- If the subprogram is recursive, there are occurrences of the name of
4784 -- the generic within the body, which must resolve to the current
4785 -- instance. We add a renaming declaration after the declaration, which
4786 -- is available in the instance body, as well as in the analysis of
4787 -- aspects that appear in the generic. This renaming declaration is
4788 -- inserted after the instance declaration which it renames.
4789
4790 procedure Instantiate_Subprogram_Contract (Templ : Node_Id);
4791 -- Instantiate all source pragmas found in the contract of the generic
4792 -- subprogram declaration template denoted by Templ. The instantiated
4793 -- pragmas are added to list Renaming_List.
4794
4795 ------------------------------------
4796 -- Analyze_Instance_And_Renamings --
4797 ------------------------------------
4798
4799 procedure Analyze_Instance_And_Renamings is
4800 Def_Ent : constant Entity_Id := Defining_Entity (N);
4801 Pack_Decl : Node_Id;
4802
4803 begin
4804 if Nkind (Parent (N)) = N_Compilation_Unit then
4805
4806 -- For the case of a compilation unit, the container package has
4807 -- the same name as the instantiation, to insure that the binder
4808 -- calls the elaboration procedure with the right name. Copy the
4809 -- entity of the instance, which may have compilation level flags
4810 -- (e.g. Is_Child_Unit) set.
4811
4812 Pack_Id := New_Copy (Def_Ent);
4813
4814 else
4815 -- Otherwise we use the name of the instantiation concatenated
4816 -- with its source position to ensure uniqueness if there are
4817 -- several instantiations with the same name.
4818
4819 Pack_Id :=
4820 Make_Defining_Identifier (Loc,
4821 Chars => New_External_Name
4822 (Related_Id => Chars (Def_Ent),
4823 Suffix => "GP",
4824 Suffix_Index => Source_Offset (Sloc (Def_Ent))));
4825 end if;
4826
4827 Pack_Decl :=
4828 Make_Package_Declaration (Loc,
4829 Specification => Make_Package_Specification (Loc,
4830 Defining_Unit_Name => Pack_Id,
4831 Visible_Declarations => Renaming_List,
4832 End_Label => Empty));
4833
4834 Set_Instance_Spec (N, Pack_Decl);
4835 Set_Is_Generic_Instance (Pack_Id);
4836 Set_Debug_Info_Needed (Pack_Id);
4837
4838 -- Case of not a compilation unit
4839
4840 if Nkind (Parent (N)) /= N_Compilation_Unit then
4841 Mark_Rewrite_Insertion (Pack_Decl);
4842 Insert_Before (N, Pack_Decl);
4843 Set_Has_Completion (Pack_Id);
4844
4845 -- Case of an instantiation that is a compilation unit
4846
4847 -- Place declaration on current node so context is complete for
4848 -- analysis (including nested instantiations), and for use in a
4849 -- context_clause (see Analyze_With_Clause).
4850
4851 else
4852 Set_Unit (Parent (N), Pack_Decl);
4853 Set_Parent_Spec (Pack_Decl, Parent_Spec (N));
4854 end if;
4855
4856 Analyze (Pack_Decl);
4857 Check_Formal_Packages (Pack_Id);
4858 Set_Is_Generic_Instance (Pack_Id, False);
4859
4860 -- Why do we clear Is_Generic_Instance??? We set it 20 lines
4861 -- above???
4862
4863 -- Body of the enclosing package is supplied when instantiating the
4864 -- subprogram body, after semantic analysis is completed.
4865
4866 if Nkind (Parent (N)) = N_Compilation_Unit then
4867
4868 -- Remove package itself from visibility, so it does not
4869 -- conflict with subprogram.
4870
4871 Set_Name_Entity_Id (Chars (Pack_Id), Homonym (Pack_Id));
4872
4873 -- Set name and scope of internal subprogram so that the proper
4874 -- external name will be generated. The proper scope is the scope
4875 -- of the wrapper package. We need to generate debugging info for
4876 -- the internal subprogram, so set flag accordingly.
4877
4878 Set_Chars (Anon_Id, Chars (Defining_Entity (N)));
4879 Set_Scope (Anon_Id, Scope (Pack_Id));
4880
4881 -- Mark wrapper package as referenced, to avoid spurious warnings
4882 -- if the instantiation appears in various with_ clauses of
4883 -- subunits of the main unit.
4884
4885 Set_Referenced (Pack_Id);
4886 end if;
4887
4888 Set_Is_Generic_Instance (Anon_Id);
4889 Set_Debug_Info_Needed (Anon_Id);
4890 Act_Decl_Id := New_Copy (Anon_Id);
4891
4892 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
4893 Set_Chars (Act_Decl_Id, Chars (Defining_Entity (N)));
4894 Set_Sloc (Act_Decl_Id, Sloc (Defining_Entity (N)));
4895
4896 -- Subprogram instance comes from source only if generic does
4897
4898 Set_Comes_From_Source (Act_Decl_Id, Comes_From_Source (Gen_Unit));
4899
4900 -- The signature may involve types that are not frozen yet, but the
4901 -- subprogram will be frozen at the point the wrapper package is
4902 -- frozen, so it does not need its own freeze node. In fact, if one
4903 -- is created, it might conflict with the freezing actions from the
4904 -- wrapper package.
4905
4906 Set_Has_Delayed_Freeze (Anon_Id, False);
4907
4908 -- If the instance is a child unit, mark the Id accordingly. Mark
4909 -- the anonymous entity as well, which is the real subprogram and
4910 -- which is used when the instance appears in a context clause.
4911 -- Similarly, propagate the Is_Eliminated flag to handle properly
4912 -- nested eliminated subprograms.
4913
4914 Set_Is_Child_Unit (Act_Decl_Id, Is_Child_Unit (Defining_Entity (N)));
4915 Set_Is_Child_Unit (Anon_Id, Is_Child_Unit (Defining_Entity (N)));
4916 New_Overloaded_Entity (Act_Decl_Id);
4917 Check_Eliminated (Act_Decl_Id);
4918 Set_Is_Eliminated (Anon_Id, Is_Eliminated (Act_Decl_Id));
4919
4920 -- In compilation unit case, kill elaboration checks on the
4921 -- instantiation, since they are never needed -- the body is
4922 -- instantiated at the same point as the spec.
4923
4924 if Nkind (Parent (N)) = N_Compilation_Unit then
4925 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
4926 Set_Kill_Elaboration_Checks (Act_Decl_Id);
4927 Set_Is_Compilation_Unit (Anon_Id);
4928
4929 Set_Cunit_Entity (Current_Sem_Unit, Pack_Id);
4930 end if;
4931
4932 -- The instance is not a freezing point for the new subprogram
4933
4934 Set_Is_Frozen (Act_Decl_Id, False);
4935
4936 if Nkind (Defining_Entity (N)) = N_Defining_Operator_Symbol then
4937 Valid_Operator_Definition (Act_Decl_Id);
4938 end if;
4939
4940 Set_Alias (Act_Decl_Id, Anon_Id);
4941 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
4942 Set_Has_Completion (Act_Decl_Id);
4943 Set_Related_Instance (Pack_Id, Act_Decl_Id);
4944
4945 if Nkind (Parent (N)) = N_Compilation_Unit then
4946 Set_Body_Required (Parent (N), False);
4947 end if;
4948 end Analyze_Instance_And_Renamings;
4949
4950 -------------------------------
4951 -- Build_Subprogram_Renaming --
4952 -------------------------------
4953
4954 procedure Build_Subprogram_Renaming is
4955 Renaming_Decl : Node_Id;
4956 Unit_Renaming : Node_Id;
4957
4958 begin
4959 Unit_Renaming :=
4960 Make_Subprogram_Renaming_Declaration (Loc,
4961 Specification =>
4962 Copy_Generic_Node
4963 (Specification (Original_Node (Gen_Decl)),
4964 Empty,
4965 Instantiating => True),
4966 Name => New_Occurrence_Of (Anon_Id, Loc));
4967
4968 -- The generic may be a a child unit. The renaming needs an
4969 -- identifier with the proper name.
4970
4971 Set_Defining_Unit_Name (Specification (Unit_Renaming),
4972 Make_Defining_Identifier (Loc, Chars (Gen_Unit)));
4973
4974 -- If there is a formal subprogram with the same name as the unit
4975 -- itself, do not add this renaming declaration, to prevent
4976 -- ambiguities when there is a call with that name in the body.
4977 -- This is a partial and ugly fix for one ACATS test. ???
4978
4979 Renaming_Decl := First (Renaming_List);
4980 while Present (Renaming_Decl) loop
4981 if Nkind (Renaming_Decl) = N_Subprogram_Renaming_Declaration
4982 and then
4983 Chars (Defining_Entity (Renaming_Decl)) = Chars (Gen_Unit)
4984 then
4985 exit;
4986 end if;
4987
4988 Next (Renaming_Decl);
4989 end loop;
4990
4991 if No (Renaming_Decl) then
4992 Append (Unit_Renaming, Renaming_List);
4993 end if;
4994 end Build_Subprogram_Renaming;
4995
4996 -------------------------------------
4997 -- Instantiate_Subprogram_Contract --
4998 -------------------------------------
4999
5000 procedure Instantiate_Subprogram_Contract (Templ : Node_Id) is
5001 procedure Instantiate_Pragmas (First_Prag : Node_Id);
5002 -- Instantiate all contract-related source pragmas found in the list
5003 -- starting with pragma First_Prag. Each instantiated pragma is added
5004 -- to list Renaming_List.
5005
5006 -------------------------
5007 -- Instantiate_Pragmas --
5008 -------------------------
5009
5010 procedure Instantiate_Pragmas (First_Prag : Node_Id) is
5011 Inst_Prag : Node_Id;
5012 Prag : Node_Id;
5013
5014 begin
5015 Prag := First_Prag;
5016 while Present (Prag) loop
5017 if Is_Generic_Contract_Pragma (Prag) then
5018 Inst_Prag :=
5019 Copy_Generic_Node (Prag, Empty, Instantiating => True);
5020
5021 Set_Analyzed (Inst_Prag, False);
5022 Append_To (Renaming_List, Inst_Prag);
5023 end if;
5024
5025 Prag := Next_Pragma (Prag);
5026 end loop;
5027 end Instantiate_Pragmas;
5028
5029 -- Local variables
5030
5031 Items : constant Node_Id := Contract (Defining_Entity (Templ));
5032
5033 -- Start of processing for Instantiate_Subprogram_Contract
5034
5035 begin
5036 if Present (Items) then
5037 Instantiate_Pragmas (Pre_Post_Conditions (Items));
5038 Instantiate_Pragmas (Contract_Test_Cases (Items));
5039 Instantiate_Pragmas (Classifications (Items));
5040 end if;
5041 end Instantiate_Subprogram_Contract;
5042
5043 -- Local variables
5044
5045 Save_IPSM : constant Boolean := Ignore_Pragma_SPARK_Mode;
5046 -- Save flag Ignore_Pragma_SPARK_Mode for restore on exit
5047
5048 Save_SM : constant SPARK_Mode_Type := SPARK_Mode;
5049 Save_SMP : constant Node_Id := SPARK_Mode_Pragma;
5050 -- Save the SPARK_Mode-related data for restore on exit
5051
5052 Vis_Prims_List : Elist_Id := No_Elist;
5053 -- List of primitives made temporarily visible in the instantiation
5054 -- to match the visibility of the formal type
5055
5056 -- Start of processing for Analyze_Subprogram_Instantiation
5057
5058 begin
5059 Check_SPARK_05_Restriction ("generic is not allowed", N);
5060
5061 -- Very first thing: check for special Text_IO unit in case we are
5062 -- instantiating one of the children of [[Wide_]Wide_]Text_IO. Of course
5063 -- such an instantiation is bogus (these are packages, not subprograms),
5064 -- but we get a better error message if we do this.
5065
5066 Check_Text_IO_Special_Unit (Gen_Id);
5067
5068 -- Make node global for error reporting
5069
5070 Instantiation_Node := N;
5071
5072 -- For package instantiations we turn off style checks, because they
5073 -- will have been emitted in the generic. For subprogram instantiations
5074 -- we want to apply at least the check on overriding indicators so we
5075 -- do not modify the style check status.
5076
5077 -- The renaming declarations for the actuals do not come from source and
5078 -- will not generate spurious warnings.
5079
5080 Preanalyze_Actuals (N);
5081
5082 Init_Env;
5083 Env_Installed := True;
5084 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
5085 Gen_Unit := Entity (Gen_Id);
5086
5087 Generate_Reference (Gen_Unit, Gen_Id);
5088
5089 if Nkind (Gen_Id) = N_Identifier
5090 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
5091 then
5092 Error_Msg_NE
5093 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
5094 end if;
5095
5096 if Etype (Gen_Unit) = Any_Type then
5097 Restore_Env;
5098 return;
5099 end if;
5100
5101 -- Verify that it is a generic subprogram of the right kind, and that
5102 -- it does not lead to a circular instantiation.
5103
5104 if K = E_Procedure and then Ekind (Gen_Unit) /= E_Generic_Procedure then
5105 Error_Msg_NE
5106 ("& is not the name of a generic procedure", Gen_Id, Gen_Unit);
5107
5108 elsif K = E_Function and then Ekind (Gen_Unit) /= E_Generic_Function then
5109 Error_Msg_NE
5110 ("& is not the name of a generic function", Gen_Id, Gen_Unit);
5111
5112 elsif In_Open_Scopes (Gen_Unit) then
5113 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
5114
5115 else
5116 -- If the context of the instance is subject to SPARK_Mode "off",
5117 -- set the global flag which signals Analyze_Pragma to ignore all
5118 -- SPARK_Mode pragmas within the instance.
5119
5120 if SPARK_Mode = Off then
5121 Ignore_Pragma_SPARK_Mode := True;
5122 end if;
5123
5124 Set_Entity (Gen_Id, Gen_Unit);
5125 Set_Is_Instantiated (Gen_Unit);
5126
5127 if In_Extended_Main_Source_Unit (N) then
5128 Generate_Reference (Gen_Unit, N);
5129 end if;
5130
5131 -- If renaming, get original unit
5132
5133 if Present (Renamed_Object (Gen_Unit))
5134 and then Ekind_In (Renamed_Object (Gen_Unit), E_Generic_Procedure,
5135 E_Generic_Function)
5136 then
5137 Gen_Unit := Renamed_Object (Gen_Unit);
5138 Set_Is_Instantiated (Gen_Unit);
5139 Generate_Reference (Gen_Unit, N);
5140 end if;
5141
5142 if Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
5143 Error_Msg_Node_2 := Current_Scope;
5144 Error_Msg_NE
5145 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
5146 Circularity_Detected := True;
5147 Restore_Hidden_Primitives (Vis_Prims_List);
5148 goto Leave;
5149 end if;
5150
5151 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
5152
5153 -- Initialize renamings map, for error checking
5154
5155 Generic_Renamings.Set_Last (0);
5156 Generic_Renamings_HTable.Reset;
5157
5158 Create_Instantiation_Source (N, Gen_Unit, False, S_Adjustment);
5159
5160 -- Copy original generic tree, to produce text for instantiation
5161
5162 Act_Tree :=
5163 Copy_Generic_Node
5164 (Original_Node (Gen_Decl), Empty, Instantiating => True);
5165
5166 -- Inherit overriding indicator from instance node
5167
5168 Act_Spec := Specification (Act_Tree);
5169 Set_Must_Override (Act_Spec, Must_Override (N));
5170 Set_Must_Not_Override (Act_Spec, Must_Not_Override (N));
5171
5172 Renaming_List :=
5173 Analyze_Associations
5174 (I_Node => N,
5175 Formals => Generic_Formal_Declarations (Act_Tree),
5176 F_Copy => Generic_Formal_Declarations (Gen_Decl));
5177
5178 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
5179
5180 -- The subprogram itself cannot contain a nested instance, so the
5181 -- current parent is left empty.
5182
5183 Set_Instance_Env (Gen_Unit, Empty);
5184
5185 -- Build the subprogram declaration, which does not appear in the
5186 -- generic template, and give it a sloc consistent with that of the
5187 -- template.
5188
5189 Set_Defining_Unit_Name (Act_Spec, Anon_Id);
5190 Set_Generic_Parent (Act_Spec, Gen_Unit);
5191 Act_Decl :=
5192 Make_Subprogram_Declaration (Sloc (Act_Spec),
5193 Specification => Act_Spec);
5194
5195 -- The aspects have been copied previously, but they have to be
5196 -- linked explicitly to the new subprogram declaration. Explicit
5197 -- pre/postconditions on the instance are analyzed below, in a
5198 -- separate step.
5199
5200 Move_Aspects (Act_Tree, To => Act_Decl);
5201 Set_Categorization_From_Pragmas (Act_Decl);
5202
5203 if Parent_Installed then
5204 Hide_Current_Scope;
5205 end if;
5206
5207 Append (Act_Decl, Renaming_List);
5208
5209 -- Contract-related source pragmas that follow a generic subprogram
5210 -- must be instantiated explicitly because they are not part of the
5211 -- subprogram template.
5212
5213 Instantiate_Subprogram_Contract (Original_Node (Gen_Decl));
5214 Build_Subprogram_Renaming;
5215
5216 Analyze_Instance_And_Renamings;
5217
5218 -- If the generic is marked Import (Intrinsic), then so is the
5219 -- instance. This indicates that there is no body to instantiate. If
5220 -- generic is marked inline, so it the instance, and the anonymous
5221 -- subprogram it renames. If inlined, or else if inlining is enabled
5222 -- for the compilation, we generate the instance body even if it is
5223 -- not within the main unit.
5224
5225 if Is_Intrinsic_Subprogram (Gen_Unit) then
5226 Set_Is_Intrinsic_Subprogram (Anon_Id);
5227 Set_Is_Intrinsic_Subprogram (Act_Decl_Id);
5228
5229 if Chars (Gen_Unit) = Name_Unchecked_Conversion then
5230 Validate_Unchecked_Conversion (N, Act_Decl_Id);
5231 end if;
5232 end if;
5233
5234 -- Inherit convention from generic unit. Intrinsic convention, as for
5235 -- an instance of unchecked conversion, is not inherited because an
5236 -- explicit Ada instance has been created.
5237
5238 if Has_Convention_Pragma (Gen_Unit)
5239 and then Convention (Gen_Unit) /= Convention_Intrinsic
5240 then
5241 Set_Convention (Act_Decl_Id, Convention (Gen_Unit));
5242 Set_Is_Exported (Act_Decl_Id, Is_Exported (Gen_Unit));
5243 end if;
5244
5245 Generate_Definition (Act_Decl_Id);
5246
5247 -- Inherit all inlining-related flags which apply to the generic in
5248 -- the subprogram and its declaration.
5249
5250 Set_Is_Inlined (Act_Decl_Id, Is_Inlined (Gen_Unit));
5251 Set_Is_Inlined (Anon_Id, Is_Inlined (Gen_Unit));
5252
5253 Set_Has_Pragma_Inline (Act_Decl_Id, Has_Pragma_Inline (Gen_Unit));
5254 Set_Has_Pragma_Inline (Anon_Id, Has_Pragma_Inline (Gen_Unit));
5255
5256 Set_Has_Pragma_Inline_Always
5257 (Act_Decl_Id, Has_Pragma_Inline_Always (Gen_Unit));
5258 Set_Has_Pragma_Inline_Always
5259 (Anon_Id, Has_Pragma_Inline_Always (Gen_Unit));
5260
5261 if not Is_Intrinsic_Subprogram (Gen_Unit) then
5262 Check_Elab_Instantiation (N);
5263 end if;
5264
5265 if Is_Dispatching_Operation (Act_Decl_Id)
5266 and then Ada_Version >= Ada_2005
5267 then
5268 declare
5269 Formal : Entity_Id;
5270
5271 begin
5272 Formal := First_Formal (Act_Decl_Id);
5273 while Present (Formal) loop
5274 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type
5275 and then Is_Controlling_Formal (Formal)
5276 and then not Can_Never_Be_Null (Formal)
5277 then
5278 Error_Msg_NE
5279 ("access parameter& is controlling,", N, Formal);
5280 Error_Msg_NE
5281 ("\corresponding parameter of & must be "
5282 & "explicitly null-excluding", N, Gen_Id);
5283 end if;
5284
5285 Next_Formal (Formal);
5286 end loop;
5287 end;
5288 end if;
5289
5290 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
5291
5292 Validate_Categorization_Dependency (N, Act_Decl_Id);
5293
5294 if not Is_Intrinsic_Subprogram (Act_Decl_Id) then
5295 Inherit_Context (Gen_Decl, N);
5296
5297 Restore_Private_Views (Pack_Id, False);
5298
5299 -- If the context requires a full instantiation, mark node for
5300 -- subsequent construction of the body.
5301
5302 if Need_Subprogram_Instance_Body (N, Act_Decl_Id) then
5303 Check_Forward_Instantiation (Gen_Decl);
5304
5305 -- The wrapper package is always delayed, because it does not
5306 -- constitute a freeze point, but to insure that the freeze
5307 -- node is placed properly, it is created directly when
5308 -- instantiating the body (otherwise the freeze node might
5309 -- appear to early for nested instantiations).
5310
5311 elsif Nkind (Parent (N)) = N_Compilation_Unit then
5312
5313 -- For ASIS purposes, indicate that the wrapper package has
5314 -- replaced the instantiation node.
5315
5316 Rewrite (N, Unit (Parent (N)));
5317 Set_Unit (Parent (N), N);
5318 end if;
5319
5320 elsif Nkind (Parent (N)) = N_Compilation_Unit then
5321
5322 -- Replace instance node for library-level instantiations of
5323 -- intrinsic subprograms, for ASIS use.
5324
5325 Rewrite (N, Unit (Parent (N)));
5326 Set_Unit (Parent (N), N);
5327 end if;
5328
5329 if Parent_Installed then
5330 Remove_Parent;
5331 end if;
5332
5333 Restore_Hidden_Primitives (Vis_Prims_List);
5334 Restore_Env;
5335 Env_Installed := False;
5336 Generic_Renamings.Set_Last (0);
5337 Generic_Renamings_HTable.Reset;
5338
5339 Ignore_Pragma_SPARK_Mode := Save_IPSM;
5340 SPARK_Mode := Save_SM;
5341 SPARK_Mode_Pragma := Save_SMP;
5342
5343 if SPARK_Mode = On then
5344 Dynamic_Elaboration_Checks := False;
5345 end if;
5346
5347 end if;
5348
5349 <<Leave>>
5350 if Has_Aspects (N) then
5351 Analyze_Aspect_Specifications (N, Act_Decl_Id);
5352 end if;
5353
5354 exception
5355 when Instantiation_Error =>
5356 if Parent_Installed then
5357 Remove_Parent;
5358 end if;
5359
5360 if Env_Installed then
5361 Restore_Env;
5362 end if;
5363
5364 Ignore_Pragma_SPARK_Mode := Save_IPSM;
5365 SPARK_Mode := Save_SM;
5366 SPARK_Mode_Pragma := Save_SMP;
5367
5368 if SPARK_Mode = On then
5369 Dynamic_Elaboration_Checks := False;
5370 end if;
5371 end Analyze_Subprogram_Instantiation;
5372
5373 -------------------------
5374 -- Get_Associated_Node --
5375 -------------------------
5376
5377 function Get_Associated_Node (N : Node_Id) return Node_Id is
5378 Assoc : Node_Id;
5379
5380 begin
5381 Assoc := Associated_Node (N);
5382
5383 if Nkind (Assoc) /= Nkind (N) then
5384 return Assoc;
5385
5386 elsif Nkind_In (Assoc, N_Aggregate, N_Extension_Aggregate) then
5387 return Assoc;
5388
5389 else
5390 -- If the node is part of an inner generic, it may itself have been
5391 -- remapped into a further generic copy. Associated_Node is otherwise
5392 -- used for the entity of the node, and will be of a different node
5393 -- kind, or else N has been rewritten as a literal or function call.
5394
5395 while Present (Associated_Node (Assoc))
5396 and then Nkind (Associated_Node (Assoc)) = Nkind (Assoc)
5397 loop
5398 Assoc := Associated_Node (Assoc);
5399 end loop;
5400
5401 -- Follow and additional link in case the final node was rewritten.
5402 -- This can only happen with nested generic units.
5403
5404 if (Nkind (Assoc) = N_Identifier or else Nkind (Assoc) in N_Op)
5405 and then Present (Associated_Node (Assoc))
5406 and then (Nkind_In (Associated_Node (Assoc), N_Function_Call,
5407 N_Explicit_Dereference,
5408 N_Integer_Literal,
5409 N_Real_Literal,
5410 N_String_Literal))
5411 then
5412 Assoc := Associated_Node (Assoc);
5413 end if;
5414
5415 -- An additional special case: an unconstrained type in an object
5416 -- declaration may have been rewritten as a local subtype constrained
5417 -- by the expression in the declaration. We need to recover the
5418 -- original entity which may be global.
5419
5420 if Present (Original_Node (Assoc))
5421 and then Nkind (Parent (N)) = N_Object_Declaration
5422 then
5423 Assoc := Original_Node (Assoc);
5424 end if;
5425
5426 return Assoc;
5427 end if;
5428 end Get_Associated_Node;
5429
5430 ----------------------------
5431 -- Build_Function_Wrapper --
5432 ----------------------------
5433
5434 function Build_Function_Wrapper
5435 (Formal_Subp : Entity_Id;
5436 Actual_Subp : Entity_Id) return Node_Id
5437 is
5438 Loc : constant Source_Ptr := Sloc (Current_Scope);
5439 Ret_Type : constant Entity_Id := Get_Instance_Of (Etype (Formal_Subp));
5440 Actuals : List_Id;
5441 Decl : Node_Id;
5442 Func_Name : Node_Id;
5443 Func : Entity_Id;
5444 Parm_Type : Node_Id;
5445 Profile : List_Id := New_List;
5446 Spec : Node_Id;
5447 Act_F : Entity_Id;
5448 Form_F : Entity_Id;
5449 New_F : Entity_Id;
5450
5451 begin
5452 Func_Name := New_Occurrence_Of (Actual_Subp, Loc);
5453
5454 Func := Make_Defining_Identifier (Loc, Chars (Formal_Subp));
5455 Set_Ekind (Func, E_Function);
5456 Set_Is_Generic_Actual_Subprogram (Func);
5457
5458 Actuals := New_List;
5459 Profile := New_List;
5460
5461 Act_F := First_Formal (Actual_Subp);
5462 Form_F := First_Formal (Formal_Subp);
5463 while Present (Form_F) loop
5464
5465 -- Create new formal for profile of wrapper, and add a reference
5466 -- to it in the list of actuals for the enclosing call. The name
5467 -- must be that of the formal in the formal subprogram, because
5468 -- calls to it in the generic body may use named associations.
5469
5470 New_F := Make_Defining_Identifier (Loc, Chars (Form_F));
5471
5472 Parm_Type :=
5473 New_Occurrence_Of (Get_Instance_Of (Etype (Form_F)), Loc);
5474
5475 Append_To (Profile,
5476 Make_Parameter_Specification (Loc,
5477 Defining_Identifier => New_F,
5478 Parameter_Type => Parm_Type));
5479
5480 Append_To (Actuals, New_Occurrence_Of (New_F, Loc));
5481 Next_Formal (Form_F);
5482
5483 if Present (Act_F) then
5484 Next_Formal (Act_F);
5485 end if;
5486 end loop;
5487
5488 Spec :=
5489 Make_Function_Specification (Loc,
5490 Defining_Unit_Name => Func,
5491 Parameter_Specifications => Profile,
5492 Result_Definition => New_Occurrence_Of (Ret_Type, Loc));
5493
5494 Decl :=
5495 Make_Expression_Function (Loc,
5496 Specification => Spec,
5497 Expression =>
5498 Make_Function_Call (Loc,
5499 Name => Func_Name,
5500 Parameter_Associations => Actuals));
5501
5502 return Decl;
5503 end Build_Function_Wrapper;
5504
5505 ----------------------------
5506 -- Build_Operator_Wrapper --
5507 ----------------------------
5508
5509 function Build_Operator_Wrapper
5510 (Formal_Subp : Entity_Id;
5511 Actual_Subp : Entity_Id) return Node_Id
5512 is
5513 Loc : constant Source_Ptr := Sloc (Current_Scope);
5514 Ret_Type : constant Entity_Id :=
5515 Get_Instance_Of (Etype (Formal_Subp));
5516 Op_Type : constant Entity_Id :=
5517 Get_Instance_Of (Etype (First_Formal (Formal_Subp)));
5518 Is_Binary : constant Boolean :=
5519 Present (Next_Formal (First_Formal (Formal_Subp)));
5520
5521 Decl : Node_Id;
5522 Expr : Node_Id;
5523 F1, F2 : Entity_Id;
5524 Func : Entity_Id;
5525 Op_Name : Name_Id;
5526 Spec : Node_Id;
5527 L, R : Node_Id;
5528
5529 begin
5530 Op_Name := Chars (Actual_Subp);
5531
5532 -- Create entities for wrapper function and its formals
5533
5534 F1 := Make_Temporary (Loc, 'A');
5535 F2 := Make_Temporary (Loc, 'B');
5536 L := New_Occurrence_Of (F1, Loc);
5537 R := New_Occurrence_Of (F2, Loc);
5538
5539 Func := Make_Defining_Identifier (Loc, Chars (Formal_Subp));
5540 Set_Ekind (Func, E_Function);
5541 Set_Is_Generic_Actual_Subprogram (Func);
5542
5543 Spec :=
5544 Make_Function_Specification (Loc,
5545 Defining_Unit_Name => Func,
5546 Parameter_Specifications => New_List (
5547 Make_Parameter_Specification (Loc,
5548 Defining_Identifier => F1,
5549 Parameter_Type => New_Occurrence_Of (Op_Type, Loc))),
5550 Result_Definition => New_Occurrence_Of (Ret_Type, Loc));
5551
5552 if Is_Binary then
5553 Append_To (Parameter_Specifications (Spec),
5554 Make_Parameter_Specification (Loc,
5555 Defining_Identifier => F2,
5556 Parameter_Type => New_Occurrence_Of (Op_Type, Loc)));
5557 end if;
5558
5559 -- Build expression as a function call, or as an operator node
5560 -- that corresponds to the name of the actual, starting with
5561 -- binary operators.
5562
5563 if Op_Name not in Any_Operator_Name then
5564 Expr :=
5565 Make_Function_Call (Loc,
5566 Name =>
5567 New_Occurrence_Of (Actual_Subp, Loc),
5568 Parameter_Associations => New_List (L));
5569
5570 if Is_Binary then
5571 Append_To (Parameter_Associations (Expr), R);
5572 end if;
5573
5574 -- Binary operators
5575
5576 elsif Is_Binary then
5577 if Op_Name = Name_Op_And then
5578 Expr := Make_Op_And (Loc, Left_Opnd => L, Right_Opnd => R);
5579 elsif Op_Name = Name_Op_Or then
5580 Expr := Make_Op_Or (Loc, Left_Opnd => L, Right_Opnd => R);
5581 elsif Op_Name = Name_Op_Xor then
5582 Expr := Make_Op_Xor (Loc, Left_Opnd => L, Right_Opnd => R);
5583 elsif Op_Name = Name_Op_Eq then
5584 Expr := Make_Op_Eq (Loc, Left_Opnd => L, Right_Opnd => R);
5585 elsif Op_Name = Name_Op_Ne then
5586 Expr := Make_Op_Ne (Loc, Left_Opnd => L, Right_Opnd => R);
5587 elsif Op_Name = Name_Op_Le then
5588 Expr := Make_Op_Le (Loc, Left_Opnd => L, Right_Opnd => R);
5589 elsif Op_Name = Name_Op_Gt then
5590 Expr := Make_Op_Gt (Loc, Left_Opnd => L, Right_Opnd => R);
5591 elsif Op_Name = Name_Op_Ge then
5592 Expr := Make_Op_Ge (Loc, Left_Opnd => L, Right_Opnd => R);
5593 elsif Op_Name = Name_Op_Lt then
5594 Expr := Make_Op_Lt (Loc, Left_Opnd => L, Right_Opnd => R);
5595 elsif Op_Name = Name_Op_Add then
5596 Expr := Make_Op_Add (Loc, Left_Opnd => L, Right_Opnd => R);
5597 elsif Op_Name = Name_Op_Subtract then
5598 Expr := Make_Op_Subtract (Loc, Left_Opnd => L, Right_Opnd => R);
5599 elsif Op_Name = Name_Op_Concat then
5600 Expr := Make_Op_Concat (Loc, Left_Opnd => L, Right_Opnd => R);
5601 elsif Op_Name = Name_Op_Multiply then
5602 Expr := Make_Op_Multiply (Loc, Left_Opnd => L, Right_Opnd => R);
5603 elsif Op_Name = Name_Op_Divide then
5604 Expr := Make_Op_Divide (Loc, Left_Opnd => L, Right_Opnd => R);
5605 elsif Op_Name = Name_Op_Mod then
5606 Expr := Make_Op_Mod (Loc, Left_Opnd => L, Right_Opnd => R);
5607 elsif Op_Name = Name_Op_Rem then
5608 Expr := Make_Op_Rem (Loc, Left_Opnd => L, Right_Opnd => R);
5609 elsif Op_Name = Name_Op_Expon then
5610 Expr := Make_Op_Expon (Loc, Left_Opnd => L, Right_Opnd => R);
5611 end if;
5612
5613 -- Unary operators
5614
5615 else
5616 if Op_Name = Name_Op_Add then
5617 Expr := Make_Op_Plus (Loc, Right_Opnd => L);
5618 elsif Op_Name = Name_Op_Subtract then
5619 Expr := Make_Op_Minus (Loc, Right_Opnd => L);
5620 elsif Op_Name = Name_Op_Abs then
5621 Expr := Make_Op_Abs (Loc, Right_Opnd => L);
5622 elsif Op_Name = Name_Op_Not then
5623 Expr := Make_Op_Not (Loc, Right_Opnd => L);
5624 end if;
5625 end if;
5626
5627 Decl :=
5628 Make_Expression_Function (Loc,
5629 Specification => Spec,
5630 Expression => Expr);
5631
5632 return Decl;
5633 end Build_Operator_Wrapper;
5634
5635 -------------------------------------------
5636 -- Build_Instance_Compilation_Unit_Nodes --
5637 -------------------------------------------
5638
5639 procedure Build_Instance_Compilation_Unit_Nodes
5640 (N : Node_Id;
5641 Act_Body : Node_Id;
5642 Act_Decl : Node_Id)
5643 is
5644 Decl_Cunit : Node_Id;
5645 Body_Cunit : Node_Id;
5646 Citem : Node_Id;
5647 New_Main : constant Entity_Id := Defining_Entity (Act_Decl);
5648 Old_Main : constant Entity_Id := Cunit_Entity (Main_Unit);
5649
5650 begin
5651 -- A new compilation unit node is built for the instance declaration
5652
5653 Decl_Cunit :=
5654 Make_Compilation_Unit (Sloc (N),
5655 Context_Items => Empty_List,
5656 Unit => Act_Decl,
5657 Aux_Decls_Node => Make_Compilation_Unit_Aux (Sloc (N)));
5658
5659 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
5660
5661 -- The new compilation unit is linked to its body, but both share the
5662 -- same file, so we do not set Body_Required on the new unit so as not
5663 -- to create a spurious dependency on a non-existent body in the ali.
5664 -- This simplifies CodePeer unit traversal.
5665
5666 -- We use the original instantiation compilation unit as the resulting
5667 -- compilation unit of the instance, since this is the main unit.
5668
5669 Rewrite (N, Act_Body);
5670
5671 -- Propagate the aspect specifications from the package body template to
5672 -- the instantiated version of the package body.
5673
5674 if Has_Aspects (Act_Body) then
5675 Set_Aspect_Specifications
5676 (N, New_Copy_List_Tree (Aspect_Specifications (Act_Body)));
5677 end if;
5678
5679 Body_Cunit := Parent (N);
5680
5681 -- The two compilation unit nodes are linked by the Library_Unit field
5682
5683 Set_Library_Unit (Decl_Cunit, Body_Cunit);
5684 Set_Library_Unit (Body_Cunit, Decl_Cunit);
5685
5686 -- Preserve the private nature of the package if needed
5687
5688 Set_Private_Present (Decl_Cunit, Private_Present (Body_Cunit));
5689
5690 -- If the instance is not the main unit, its context, categorization
5691 -- and elaboration entity are not relevant to the compilation.
5692
5693 if Body_Cunit /= Cunit (Main_Unit) then
5694 Make_Instance_Unit (Body_Cunit, In_Main => False);
5695 return;
5696 end if;
5697
5698 -- The context clause items on the instantiation, which are now attached
5699 -- to the body compilation unit (since the body overwrote the original
5700 -- instantiation node), semantically belong on the spec, so copy them
5701 -- there. It's harmless to leave them on the body as well. In fact one
5702 -- could argue that they belong in both places.
5703
5704 Citem := First (Context_Items (Body_Cunit));
5705 while Present (Citem) loop
5706 Append (New_Copy (Citem), Context_Items (Decl_Cunit));
5707 Next (Citem);
5708 end loop;
5709
5710 -- Propagate categorization flags on packages, so that they appear in
5711 -- the ali file for the spec of the unit.
5712
5713 if Ekind (New_Main) = E_Package then
5714 Set_Is_Pure (Old_Main, Is_Pure (New_Main));
5715 Set_Is_Preelaborated (Old_Main, Is_Preelaborated (New_Main));
5716 Set_Is_Remote_Types (Old_Main, Is_Remote_Types (New_Main));
5717 Set_Is_Shared_Passive (Old_Main, Is_Shared_Passive (New_Main));
5718 Set_Is_Remote_Call_Interface
5719 (Old_Main, Is_Remote_Call_Interface (New_Main));
5720 end if;
5721
5722 -- Make entry in Units table, so that binder can generate call to
5723 -- elaboration procedure for body, if any.
5724
5725 Make_Instance_Unit (Body_Cunit, In_Main => True);
5726 Main_Unit_Entity := New_Main;
5727 Set_Cunit_Entity (Main_Unit, Main_Unit_Entity);
5728
5729 -- Build elaboration entity, since the instance may certainly generate
5730 -- elaboration code requiring a flag for protection.
5731
5732 Build_Elaboration_Entity (Decl_Cunit, New_Main);
5733 end Build_Instance_Compilation_Unit_Nodes;
5734
5735 -----------------------------
5736 -- Check_Access_Definition --
5737 -----------------------------
5738
5739 procedure Check_Access_Definition (N : Node_Id) is
5740 begin
5741 pragma Assert
5742 (Ada_Version >= Ada_2005 and then Present (Access_Definition (N)));
5743 null;
5744 end Check_Access_Definition;
5745
5746 -----------------------------------
5747 -- Check_Formal_Package_Instance --
5748 -----------------------------------
5749
5750 -- If the formal has specific parameters, they must match those of the
5751 -- actual. Both of them are instances, and the renaming declarations for
5752 -- their formal parameters appear in the same order in both. The analyzed
5753 -- formal has been analyzed in the context of the current instance.
5754
5755 procedure Check_Formal_Package_Instance
5756 (Formal_Pack : Entity_Id;
5757 Actual_Pack : Entity_Id)
5758 is
5759 E1 : Entity_Id := First_Entity (Actual_Pack);
5760 E2 : Entity_Id := First_Entity (Formal_Pack);
5761
5762 Expr1 : Node_Id;
5763 Expr2 : Node_Id;
5764
5765 procedure Check_Mismatch (B : Boolean);
5766 -- Common error routine for mismatch between the parameters of the
5767 -- actual instance and those of the formal package.
5768
5769 function Same_Instantiated_Constant (E1, E2 : Entity_Id) return Boolean;
5770 -- The formal may come from a nested formal package, and the actual may
5771 -- have been constant-folded. To determine whether the two denote the
5772 -- same entity we may have to traverse several definitions to recover
5773 -- the ultimate entity that they refer to.
5774
5775 function Same_Instantiated_Variable (E1, E2 : Entity_Id) return Boolean;
5776 -- Similarly, if the formal comes from a nested formal package, the
5777 -- actual may designate the formal through multiple renamings, which
5778 -- have to be followed to determine the original variable in question.
5779
5780 --------------------
5781 -- Check_Mismatch --
5782 --------------------
5783
5784 procedure Check_Mismatch (B : Boolean) is
5785 Kind : constant Node_Kind := Nkind (Parent (E2));
5786
5787 begin
5788 if Kind = N_Formal_Type_Declaration then
5789 return;
5790
5791 elsif Nkind_In (Kind, N_Formal_Object_Declaration,
5792 N_Formal_Package_Declaration)
5793 or else Kind in N_Formal_Subprogram_Declaration
5794 then
5795 null;
5796
5797 -- Ada 2012: If both formal and actual are incomplete types they
5798 -- are conformant.
5799
5800 elsif Is_Incomplete_Type (E1) and then Is_Incomplete_Type (E2) then
5801 null;
5802
5803 elsif B then
5804 Error_Msg_NE
5805 ("actual for & in actual instance does not match formal",
5806 Parent (Actual_Pack), E1);
5807 end if;
5808 end Check_Mismatch;
5809
5810 --------------------------------
5811 -- Same_Instantiated_Constant --
5812 --------------------------------
5813
5814 function Same_Instantiated_Constant
5815 (E1, E2 : Entity_Id) return Boolean
5816 is
5817 Ent : Entity_Id;
5818
5819 begin
5820 Ent := E2;
5821 while Present (Ent) loop
5822 if E1 = Ent then
5823 return True;
5824
5825 elsif Ekind (Ent) /= E_Constant then
5826 return False;
5827
5828 elsif Is_Entity_Name (Constant_Value (Ent)) then
5829 if Entity (Constant_Value (Ent)) = E1 then
5830 return True;
5831 else
5832 Ent := Entity (Constant_Value (Ent));
5833 end if;
5834
5835 -- The actual may be a constant that has been folded. Recover
5836 -- original name.
5837
5838 elsif Is_Entity_Name (Original_Node (Constant_Value (Ent))) then
5839 Ent := Entity (Original_Node (Constant_Value (Ent)));
5840
5841 else
5842 return False;
5843 end if;
5844 end loop;
5845
5846 return False;
5847 end Same_Instantiated_Constant;
5848
5849 --------------------------------
5850 -- Same_Instantiated_Variable --
5851 --------------------------------
5852
5853 function Same_Instantiated_Variable
5854 (E1, E2 : Entity_Id) return Boolean
5855 is
5856 function Original_Entity (E : Entity_Id) return Entity_Id;
5857 -- Follow chain of renamings to the ultimate ancestor
5858
5859 ---------------------
5860 -- Original_Entity --
5861 ---------------------
5862
5863 function Original_Entity (E : Entity_Id) return Entity_Id is
5864 Orig : Entity_Id;
5865
5866 begin
5867 Orig := E;
5868 while Nkind (Parent (Orig)) = N_Object_Renaming_Declaration
5869 and then Present (Renamed_Object (Orig))
5870 and then Is_Entity_Name (Renamed_Object (Orig))
5871 loop
5872 Orig := Entity (Renamed_Object (Orig));
5873 end loop;
5874
5875 return Orig;
5876 end Original_Entity;
5877
5878 -- Start of processing for Same_Instantiated_Variable
5879
5880 begin
5881 return Ekind (E1) = Ekind (E2)
5882 and then Original_Entity (E1) = Original_Entity (E2);
5883 end Same_Instantiated_Variable;
5884
5885 -- Start of processing for Check_Formal_Package_Instance
5886
5887 begin
5888 while Present (E1) and then Present (E2) loop
5889 exit when Ekind (E1) = E_Package
5890 and then Renamed_Entity (E1) = Renamed_Entity (Actual_Pack);
5891
5892 -- If the formal is the renaming of the formal package, this
5893 -- is the end of its formal part, which may occur before the
5894 -- end of the formal part in the actual in the presence of
5895 -- defaulted parameters in the formal package.
5896
5897 exit when Nkind (Parent (E2)) = N_Package_Renaming_Declaration
5898 and then Renamed_Entity (E2) = Scope (E2);
5899
5900 -- The analysis of the actual may generate additional internal
5901 -- entities. If the formal is defaulted, there is no corresponding
5902 -- analysis and the internal entities must be skipped, until we
5903 -- find corresponding entities again.
5904
5905 if Comes_From_Source (E2)
5906 and then not Comes_From_Source (E1)
5907 and then Chars (E1) /= Chars (E2)
5908 then
5909 while Present (E1) and then Chars (E1) /= Chars (E2) loop
5910 Next_Entity (E1);
5911 end loop;
5912 end if;
5913
5914 if No (E1) then
5915 return;
5916
5917 -- If the formal entity comes from a formal declaration, it was
5918 -- defaulted in the formal package, and no check is needed on it.
5919
5920 elsif Nkind (Parent (E2)) = N_Formal_Object_Declaration then
5921 goto Next_E;
5922
5923 -- Ditto for defaulted formal subprograms.
5924
5925 elsif Is_Overloadable (E1)
5926 and then Nkind (Unit_Declaration_Node (E2)) in
5927 N_Formal_Subprogram_Declaration
5928 then
5929 goto Next_E;
5930
5931 elsif Is_Type (E1) then
5932
5933 -- Subtypes must statically match. E1, E2 are the local entities
5934 -- that are subtypes of the actuals. Itypes generated for other
5935 -- parameters need not be checked, the check will be performed
5936 -- on the parameters themselves.
5937
5938 -- If E2 is a formal type declaration, it is a defaulted parameter
5939 -- and needs no checking.
5940
5941 if not Is_Itype (E1) and then not Is_Itype (E2) then
5942 Check_Mismatch
5943 (not Is_Type (E2)
5944 or else Etype (E1) /= Etype (E2)
5945 or else not Subtypes_Statically_Match (E1, E2));
5946 end if;
5947
5948 elsif Ekind (E1) = E_Constant then
5949
5950 -- IN parameters must denote the same static value, or the same
5951 -- constant, or the literal null.
5952
5953 Expr1 := Expression (Parent (E1));
5954
5955 if Ekind (E2) /= E_Constant then
5956 Check_Mismatch (True);
5957 goto Next_E;
5958 else
5959 Expr2 := Expression (Parent (E2));
5960 end if;
5961
5962 if Is_OK_Static_Expression (Expr1) then
5963 if not Is_OK_Static_Expression (Expr2) then
5964 Check_Mismatch (True);
5965
5966 elsif Is_Discrete_Type (Etype (E1)) then
5967 declare
5968 V1 : constant Uint := Expr_Value (Expr1);
5969 V2 : constant Uint := Expr_Value (Expr2);
5970 begin
5971 Check_Mismatch (V1 /= V2);
5972 end;
5973
5974 elsif Is_Real_Type (Etype (E1)) then
5975 declare
5976 V1 : constant Ureal := Expr_Value_R (Expr1);
5977 V2 : constant Ureal := Expr_Value_R (Expr2);
5978 begin
5979 Check_Mismatch (V1 /= V2);
5980 end;
5981
5982 elsif Is_String_Type (Etype (E1))
5983 and then Nkind (Expr1) = N_String_Literal
5984 then
5985 if Nkind (Expr2) /= N_String_Literal then
5986 Check_Mismatch (True);
5987 else
5988 Check_Mismatch
5989 (not String_Equal (Strval (Expr1), Strval (Expr2)));
5990 end if;
5991 end if;
5992
5993 elsif Is_Entity_Name (Expr1) then
5994 if Is_Entity_Name (Expr2) then
5995 if Entity (Expr1) = Entity (Expr2) then
5996 null;
5997 else
5998 Check_Mismatch
5999 (not Same_Instantiated_Constant
6000 (Entity (Expr1), Entity (Expr2)));
6001 end if;
6002
6003 else
6004 Check_Mismatch (True);
6005 end if;
6006
6007 elsif Is_Entity_Name (Original_Node (Expr1))
6008 and then Is_Entity_Name (Expr2)
6009 and then Same_Instantiated_Constant
6010 (Entity (Original_Node (Expr1)), Entity (Expr2))
6011 then
6012 null;
6013
6014 elsif Nkind (Expr1) = N_Null then
6015 Check_Mismatch (Nkind (Expr1) /= N_Null);
6016
6017 else
6018 Check_Mismatch (True);
6019 end if;
6020
6021 elsif Ekind (E1) = E_Variable then
6022 Check_Mismatch (not Same_Instantiated_Variable (E1, E2));
6023
6024 elsif Ekind (E1) = E_Package then
6025 Check_Mismatch
6026 (Ekind (E1) /= Ekind (E2)
6027 or else Renamed_Object (E1) /= Renamed_Object (E2));
6028
6029 elsif Is_Overloadable (E1) then
6030
6031 -- Verify that the actual subprograms match. Note that actuals
6032 -- that are attributes are rewritten as subprograms. If the
6033 -- subprogram in the formal package is defaulted, no check is
6034 -- needed. Note that this can only happen in Ada 2005 when the
6035 -- formal package can be partially parameterized.
6036
6037 if Nkind (Unit_Declaration_Node (E1)) =
6038 N_Subprogram_Renaming_Declaration
6039 and then From_Default (Unit_Declaration_Node (E1))
6040 then
6041 null;
6042
6043 -- If the formal package has an "others" box association that
6044 -- covers this formal, there is no need for a check either.
6045
6046 elsif Nkind (Unit_Declaration_Node (E2)) in
6047 N_Formal_Subprogram_Declaration
6048 and then Box_Present (Unit_Declaration_Node (E2))
6049 then
6050 null;
6051
6052 -- No check needed if subprogram is a defaulted null procedure
6053
6054 elsif No (Alias (E2))
6055 and then Ekind (E2) = E_Procedure
6056 and then
6057 Null_Present (Specification (Unit_Declaration_Node (E2)))
6058 then
6059 null;
6060
6061 -- Otherwise the actual in the formal and the actual in the
6062 -- instantiation of the formal must match, up to renamings.
6063
6064 else
6065 Check_Mismatch
6066 (Ekind (E2) /= Ekind (E1) or else (Alias (E1)) /= Alias (E2));
6067 end if;
6068
6069 else
6070 raise Program_Error;
6071 end if;
6072
6073 <<Next_E>>
6074 Next_Entity (E1);
6075 Next_Entity (E2);
6076 end loop;
6077 end Check_Formal_Package_Instance;
6078
6079 ---------------------------
6080 -- Check_Formal_Packages --
6081 ---------------------------
6082
6083 procedure Check_Formal_Packages (P_Id : Entity_Id) is
6084 E : Entity_Id;
6085 Formal_P : Entity_Id;
6086
6087 begin
6088 -- Iterate through the declarations in the instance, looking for package
6089 -- renaming declarations that denote instances of formal packages. Stop
6090 -- when we find the renaming of the current package itself. The
6091 -- declaration for a formal package without a box is followed by an
6092 -- internal entity that repeats the instantiation.
6093
6094 E := First_Entity (P_Id);
6095 while Present (E) loop
6096 if Ekind (E) = E_Package then
6097 if Renamed_Object (E) = P_Id then
6098 exit;
6099
6100 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
6101 null;
6102
6103 elsif not Box_Present (Parent (Associated_Formal_Package (E))) then
6104 Formal_P := Next_Entity (E);
6105 Check_Formal_Package_Instance (Formal_P, E);
6106
6107 -- After checking, remove the internal validating package. It
6108 -- is only needed for semantic checks, and as it may contain
6109 -- generic formal declarations it should not reach gigi.
6110
6111 Remove (Unit_Declaration_Node (Formal_P));
6112 end if;
6113 end if;
6114
6115 Next_Entity (E);
6116 end loop;
6117 end Check_Formal_Packages;
6118
6119 ---------------------------------
6120 -- Check_Forward_Instantiation --
6121 ---------------------------------
6122
6123 procedure Check_Forward_Instantiation (Decl : Node_Id) is
6124 S : Entity_Id;
6125 Gen_Comp : Entity_Id := Cunit_Entity (Get_Source_Unit (Decl));
6126
6127 begin
6128 -- The instantiation appears before the generic body if we are in the
6129 -- scope of the unit containing the generic, either in its spec or in
6130 -- the package body, and before the generic body.
6131
6132 if Ekind (Gen_Comp) = E_Package_Body then
6133 Gen_Comp := Spec_Entity (Gen_Comp);
6134 end if;
6135
6136 if In_Open_Scopes (Gen_Comp)
6137 and then No (Corresponding_Body (Decl))
6138 then
6139 S := Current_Scope;
6140
6141 while Present (S)
6142 and then not Is_Compilation_Unit (S)
6143 and then not Is_Child_Unit (S)
6144 loop
6145 if Ekind (S) = E_Package then
6146 Set_Has_Forward_Instantiation (S);
6147 end if;
6148
6149 S := Scope (S);
6150 end loop;
6151 end if;
6152 end Check_Forward_Instantiation;
6153
6154 ---------------------------
6155 -- Check_Generic_Actuals --
6156 ---------------------------
6157
6158 -- The visibility of the actuals may be different between the point of
6159 -- generic instantiation and the instantiation of the body.
6160
6161 procedure Check_Generic_Actuals
6162 (Instance : Entity_Id;
6163 Is_Formal_Box : Boolean)
6164 is
6165 E : Entity_Id;
6166 Astype : Entity_Id;
6167
6168 function Denotes_Previous_Actual (Typ : Entity_Id) return Boolean;
6169 -- For a formal that is an array type, the component type is often a
6170 -- previous formal in the same unit. The privacy status of the component
6171 -- type will have been examined earlier in the traversal of the
6172 -- corresponding actuals, and this status should not be modified for
6173 -- the array (sub)type itself. However, if the base type of the array
6174 -- (sub)type is private, its full view must be restored in the body to
6175 -- be consistent with subsequent index subtypes, etc.
6176 --
6177 -- To detect this case we have to rescan the list of formals, which is
6178 -- usually short enough to ignore the resulting inefficiency.
6179
6180 -----------------------------
6181 -- Denotes_Previous_Actual --
6182 -----------------------------
6183
6184 function Denotes_Previous_Actual (Typ : Entity_Id) return Boolean is
6185 Prev : Entity_Id;
6186
6187 begin
6188 Prev := First_Entity (Instance);
6189 while Present (Prev) loop
6190 if Is_Type (Prev)
6191 and then Nkind (Parent (Prev)) = N_Subtype_Declaration
6192 and then Is_Entity_Name (Subtype_Indication (Parent (Prev)))
6193 and then Entity (Subtype_Indication (Parent (Prev))) = Typ
6194 then
6195 return True;
6196
6197 elsif Prev = E then
6198 return False;
6199
6200 else
6201 Next_Entity (Prev);
6202 end if;
6203 end loop;
6204
6205 return False;
6206 end Denotes_Previous_Actual;
6207
6208 -- Start of processing for Check_Generic_Actuals
6209
6210 begin
6211 E := First_Entity (Instance);
6212 while Present (E) loop
6213 if Is_Type (E)
6214 and then Nkind (Parent (E)) = N_Subtype_Declaration
6215 and then Scope (Etype (E)) /= Instance
6216 and then Is_Entity_Name (Subtype_Indication (Parent (E)))
6217 then
6218 if Is_Array_Type (E)
6219 and then not Is_Private_Type (Etype (E))
6220 and then Denotes_Previous_Actual (Component_Type (E))
6221 then
6222 null;
6223 else
6224 Check_Private_View (Subtype_Indication (Parent (E)));
6225 end if;
6226
6227 Set_Is_Generic_Actual_Type (E, True);
6228 Set_Is_Hidden (E, False);
6229 Set_Is_Potentially_Use_Visible (E,
6230 In_Use (Instance));
6231
6232 -- We constructed the generic actual type as a subtype of the
6233 -- supplied type. This means that it normally would not inherit
6234 -- subtype specific attributes of the actual, which is wrong for
6235 -- the generic case.
6236
6237 Astype := Ancestor_Subtype (E);
6238
6239 if No (Astype) then
6240
6241 -- This can happen when E is an itype that is the full view of
6242 -- a private type completed, e.g. with a constrained array. In
6243 -- that case, use the first subtype, which will carry size
6244 -- information. The base type itself is unconstrained and will
6245 -- not carry it.
6246
6247 Astype := First_Subtype (E);
6248 end if;
6249
6250 Set_Size_Info (E, (Astype));
6251 Set_RM_Size (E, RM_Size (Astype));
6252 Set_First_Rep_Item (E, First_Rep_Item (Astype));
6253
6254 if Is_Discrete_Or_Fixed_Point_Type (E) then
6255 Set_RM_Size (E, RM_Size (Astype));
6256
6257 -- In nested instances, the base type of an access actual may
6258 -- itself be private, and need to be exchanged.
6259
6260 elsif Is_Access_Type (E)
6261 and then Is_Private_Type (Etype (E))
6262 then
6263 Check_Private_View
6264 (New_Occurrence_Of (Etype (E), Sloc (Instance)));
6265 end if;
6266
6267 elsif Ekind (E) = E_Package then
6268
6269 -- If this is the renaming for the current instance, we're done.
6270 -- Otherwise it is a formal package. If the corresponding formal
6271 -- was declared with a box, the (instantiations of the) generic
6272 -- formal part are also visible. Otherwise, ignore the entity
6273 -- created to validate the actuals.
6274
6275 if Renamed_Object (E) = Instance then
6276 exit;
6277
6278 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
6279 null;
6280
6281 -- The visibility of a formal of an enclosing generic is already
6282 -- correct.
6283
6284 elsif Denotes_Formal_Package (E) then
6285 null;
6286
6287 elsif Present (Associated_Formal_Package (E))
6288 and then not Is_Generic_Formal (E)
6289 then
6290 if Box_Present (Parent (Associated_Formal_Package (E))) then
6291 Check_Generic_Actuals (Renamed_Object (E), True);
6292
6293 else
6294 Check_Generic_Actuals (Renamed_Object (E), False);
6295 end if;
6296
6297 Set_Is_Hidden (E, False);
6298 end if;
6299
6300 -- If this is a subprogram instance (in a wrapper package) the
6301 -- actual is fully visible.
6302
6303 elsif Is_Wrapper_Package (Instance) then
6304 Set_Is_Hidden (E, False);
6305
6306 -- If the formal package is declared with a box, or if the formal
6307 -- parameter is defaulted, it is visible in the body.
6308
6309 elsif Is_Formal_Box or else Is_Visible_Formal (E) then
6310 Set_Is_Hidden (E, False);
6311 end if;
6312
6313 if Ekind (E) = E_Constant then
6314
6315 -- If the type of the actual is a private type declared in the
6316 -- enclosing scope of the generic unit, the body of the generic
6317 -- sees the full view of the type (because it has to appear in
6318 -- the corresponding package body). If the type is private now,
6319 -- exchange views to restore the proper visiblity in the instance.
6320
6321 declare
6322 Typ : constant Entity_Id := Base_Type (Etype (E));
6323 -- The type of the actual
6324
6325 Gen_Id : Entity_Id;
6326 -- The generic unit
6327
6328 Parent_Scope : Entity_Id;
6329 -- The enclosing scope of the generic unit
6330
6331 begin
6332 if Is_Wrapper_Package (Instance) then
6333 Gen_Id :=
6334 Generic_Parent
6335 (Specification
6336 (Unit_Declaration_Node
6337 (Related_Instance (Instance))));
6338 else
6339 Gen_Id :=
6340 Generic_Parent (Package_Specification (Instance));
6341 end if;
6342
6343 Parent_Scope := Scope (Gen_Id);
6344
6345 -- The exchange is only needed if the generic is defined
6346 -- within a package which is not a common ancestor of the
6347 -- scope of the instance, and is not already in scope.
6348
6349 if Is_Private_Type (Typ)
6350 and then Scope (Typ) = Parent_Scope
6351 and then Scope (Instance) /= Parent_Scope
6352 and then Ekind (Parent_Scope) = E_Package
6353 and then not Is_Child_Unit (Gen_Id)
6354 then
6355 Switch_View (Typ);
6356
6357 -- If the type of the entity is a subtype, it may also have
6358 -- to be made visible, together with the base type of its
6359 -- full view, after exchange.
6360
6361 if Is_Private_Type (Etype (E)) then
6362 Switch_View (Etype (E));
6363 Switch_View (Base_Type (Etype (E)));
6364 end if;
6365 end if;
6366 end;
6367 end if;
6368
6369 Next_Entity (E);
6370 end loop;
6371 end Check_Generic_Actuals;
6372
6373 ------------------------------
6374 -- Check_Generic_Child_Unit --
6375 ------------------------------
6376
6377 procedure Check_Generic_Child_Unit
6378 (Gen_Id : Node_Id;
6379 Parent_Installed : in out Boolean)
6380 is
6381 Loc : constant Source_Ptr := Sloc (Gen_Id);
6382 Gen_Par : Entity_Id := Empty;
6383 E : Entity_Id;
6384 Inst_Par : Entity_Id;
6385 S : Node_Id;
6386
6387 function Find_Generic_Child
6388 (Scop : Entity_Id;
6389 Id : Node_Id) return Entity_Id;
6390 -- Search generic parent for possible child unit with the given name
6391
6392 function In_Enclosing_Instance return Boolean;
6393 -- Within an instance of the parent, the child unit may be denoted by
6394 -- a simple name, or an abbreviated expanded name. Examine enclosing
6395 -- scopes to locate a possible parent instantiation.
6396
6397 ------------------------
6398 -- Find_Generic_Child --
6399 ------------------------
6400
6401 function Find_Generic_Child
6402 (Scop : Entity_Id;
6403 Id : Node_Id) return Entity_Id
6404 is
6405 E : Entity_Id;
6406
6407 begin
6408 -- If entity of name is already set, instance has already been
6409 -- resolved, e.g. in an enclosing instantiation.
6410
6411 if Present (Entity (Id)) then
6412 if Scope (Entity (Id)) = Scop then
6413 return Entity (Id);
6414 else
6415 return Empty;
6416 end if;
6417
6418 else
6419 E := First_Entity (Scop);
6420 while Present (E) loop
6421 if Chars (E) = Chars (Id)
6422 and then Is_Child_Unit (E)
6423 then
6424 if Is_Child_Unit (E)
6425 and then not Is_Visible_Lib_Unit (E)
6426 then
6427 Error_Msg_NE
6428 ("generic child unit& is not visible", Gen_Id, E);
6429 end if;
6430
6431 Set_Entity (Id, E);
6432 return E;
6433 end if;
6434
6435 Next_Entity (E);
6436 end loop;
6437
6438 return Empty;
6439 end if;
6440 end Find_Generic_Child;
6441
6442 ---------------------------
6443 -- In_Enclosing_Instance --
6444 ---------------------------
6445
6446 function In_Enclosing_Instance return Boolean is
6447 Enclosing_Instance : Node_Id;
6448 Instance_Decl : Node_Id;
6449
6450 begin
6451 -- We do not inline any call that contains instantiations, except
6452 -- for instantiations of Unchecked_Conversion, so if we are within
6453 -- an inlined body the current instance does not require parents.
6454
6455 if In_Inlined_Body then
6456 pragma Assert (Chars (Gen_Id) = Name_Unchecked_Conversion);
6457 return False;
6458 end if;
6459
6460 -- Loop to check enclosing scopes
6461
6462 Enclosing_Instance := Current_Scope;
6463 while Present (Enclosing_Instance) loop
6464 Instance_Decl := Unit_Declaration_Node (Enclosing_Instance);
6465
6466 if Ekind (Enclosing_Instance) = E_Package
6467 and then Is_Generic_Instance (Enclosing_Instance)
6468 and then Present
6469 (Generic_Parent (Specification (Instance_Decl)))
6470 then
6471 -- Check whether the generic we are looking for is a child of
6472 -- this instance.
6473
6474 E := Find_Generic_Child
6475 (Generic_Parent (Specification (Instance_Decl)), Gen_Id);
6476 exit when Present (E);
6477
6478 else
6479 E := Empty;
6480 end if;
6481
6482 Enclosing_Instance := Scope (Enclosing_Instance);
6483 end loop;
6484
6485 if No (E) then
6486
6487 -- Not a child unit
6488
6489 Analyze (Gen_Id);
6490 return False;
6491
6492 else
6493 Rewrite (Gen_Id,
6494 Make_Expanded_Name (Loc,
6495 Chars => Chars (E),
6496 Prefix => New_Occurrence_Of (Enclosing_Instance, Loc),
6497 Selector_Name => New_Occurrence_Of (E, Loc)));
6498
6499 Set_Entity (Gen_Id, E);
6500 Set_Etype (Gen_Id, Etype (E));
6501 Parent_Installed := False; -- Already in scope.
6502 return True;
6503 end if;
6504 end In_Enclosing_Instance;
6505
6506 -- Start of processing for Check_Generic_Child_Unit
6507
6508 begin
6509 -- If the name of the generic is given by a selected component, it may
6510 -- be the name of a generic child unit, and the prefix is the name of an
6511 -- instance of the parent, in which case the child unit must be visible.
6512 -- If this instance is not in scope, it must be placed there and removed
6513 -- after instantiation, because what is being instantiated is not the
6514 -- original child, but the corresponding child present in the instance
6515 -- of the parent.
6516
6517 -- If the child is instantiated within the parent, it can be given by
6518 -- a simple name. In this case the instance is already in scope, but
6519 -- the child generic must be recovered from the generic parent as well.
6520
6521 if Nkind (Gen_Id) = N_Selected_Component then
6522 S := Selector_Name (Gen_Id);
6523 Analyze (Prefix (Gen_Id));
6524 Inst_Par := Entity (Prefix (Gen_Id));
6525
6526 if Ekind (Inst_Par) = E_Package
6527 and then Present (Renamed_Object (Inst_Par))
6528 then
6529 Inst_Par := Renamed_Object (Inst_Par);
6530 end if;
6531
6532 if Ekind (Inst_Par) = E_Package then
6533 if Nkind (Parent (Inst_Par)) = N_Package_Specification then
6534 Gen_Par := Generic_Parent (Parent (Inst_Par));
6535
6536 elsif Nkind (Parent (Inst_Par)) = N_Defining_Program_Unit_Name
6537 and then
6538 Nkind (Parent (Parent (Inst_Par))) = N_Package_Specification
6539 then
6540 Gen_Par := Generic_Parent (Parent (Parent (Inst_Par)));
6541 end if;
6542
6543 elsif Ekind (Inst_Par) = E_Generic_Package
6544 and then Nkind (Parent (Gen_Id)) = N_Formal_Package_Declaration
6545 then
6546 -- A formal package may be a real child package, and not the
6547 -- implicit instance within a parent. In this case the child is
6548 -- not visible and has to be retrieved explicitly as well.
6549
6550 Gen_Par := Inst_Par;
6551 end if;
6552
6553 if Present (Gen_Par) then
6554
6555 -- The prefix denotes an instantiation. The entity itself may be a
6556 -- nested generic, or a child unit.
6557
6558 E := Find_Generic_Child (Gen_Par, S);
6559
6560 if Present (E) then
6561 Change_Selected_Component_To_Expanded_Name (Gen_Id);
6562 Set_Entity (Gen_Id, E);
6563 Set_Etype (Gen_Id, Etype (E));
6564 Set_Entity (S, E);
6565 Set_Etype (S, Etype (E));
6566
6567 -- Indicate that this is a reference to the parent
6568
6569 if In_Extended_Main_Source_Unit (Gen_Id) then
6570 Set_Is_Instantiated (Inst_Par);
6571 end if;
6572
6573 -- A common mistake is to replicate the naming scheme of a
6574 -- hierarchy by instantiating a generic child directly, rather
6575 -- than the implicit child in a parent instance:
6576
6577 -- generic .. package Gpar is ..
6578 -- generic .. package Gpar.Child is ..
6579 -- package Par is new Gpar ();
6580
6581 -- with Gpar.Child;
6582 -- package Par.Child is new Gpar.Child ();
6583 -- rather than Par.Child
6584
6585 -- In this case the instantiation is within Par, which is an
6586 -- instance, but Gpar does not denote Par because we are not IN
6587 -- the instance of Gpar, so this is illegal. The test below
6588 -- recognizes this particular case.
6589
6590 if Is_Child_Unit (E)
6591 and then not Comes_From_Source (Entity (Prefix (Gen_Id)))
6592 and then (not In_Instance
6593 or else Nkind (Parent (Parent (Gen_Id))) =
6594 N_Compilation_Unit)
6595 then
6596 Error_Msg_N
6597 ("prefix of generic child unit must be instance of parent",
6598 Gen_Id);
6599 end if;
6600
6601 if not In_Open_Scopes (Inst_Par)
6602 and then Nkind (Parent (Gen_Id)) not in
6603 N_Generic_Renaming_Declaration
6604 then
6605 Install_Parent (Inst_Par);
6606 Parent_Installed := True;
6607
6608 elsif In_Open_Scopes (Inst_Par) then
6609
6610 -- If the parent is already installed, install the actuals
6611 -- for its formal packages. This is necessary when the child
6612 -- instance is a child of the parent instance: in this case,
6613 -- the parent is placed on the scope stack but the formal
6614 -- packages are not made visible.
6615
6616 Install_Formal_Packages (Inst_Par);
6617 end if;
6618
6619 else
6620 -- If the generic parent does not contain an entity that
6621 -- corresponds to the selector, the instance doesn't either.
6622 -- Analyzing the node will yield the appropriate error message.
6623 -- If the entity is not a child unit, then it is an inner
6624 -- generic in the parent.
6625
6626 Analyze (Gen_Id);
6627 end if;
6628
6629 else
6630 Analyze (Gen_Id);
6631
6632 if Is_Child_Unit (Entity (Gen_Id))
6633 and then
6634 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
6635 and then not In_Open_Scopes (Inst_Par)
6636 then
6637 Install_Parent (Inst_Par);
6638 Parent_Installed := True;
6639
6640 -- The generic unit may be the renaming of the implicit child
6641 -- present in an instance. In that case the parent instance is
6642 -- obtained from the name of the renamed entity.
6643
6644 elsif Ekind (Entity (Gen_Id)) = E_Generic_Package
6645 and then Present (Renamed_Entity (Entity (Gen_Id)))
6646 and then Is_Child_Unit (Renamed_Entity (Entity (Gen_Id)))
6647 then
6648 declare
6649 Renamed_Package : constant Node_Id :=
6650 Name (Parent (Entity (Gen_Id)));
6651 begin
6652 if Nkind (Renamed_Package) = N_Expanded_Name then
6653 Inst_Par := Entity (Prefix (Renamed_Package));
6654 Install_Parent (Inst_Par);
6655 Parent_Installed := True;
6656 end if;
6657 end;
6658 end if;
6659 end if;
6660
6661 elsif Nkind (Gen_Id) = N_Expanded_Name then
6662
6663 -- Entity already present, analyze prefix, whose meaning may be
6664 -- an instance in the current context. If it is an instance of
6665 -- a relative within another, the proper parent may still have
6666 -- to be installed, if they are not of the same generation.
6667
6668 Analyze (Prefix (Gen_Id));
6669
6670 -- In the unlikely case that a local declaration hides the name
6671 -- of the parent package, locate it on the homonym chain. If the
6672 -- context is an instance of the parent, the renaming entity is
6673 -- flagged as such.
6674
6675 Inst_Par := Entity (Prefix (Gen_Id));
6676 while Present (Inst_Par)
6677 and then not Is_Package_Or_Generic_Package (Inst_Par)
6678 loop
6679 Inst_Par := Homonym (Inst_Par);
6680 end loop;
6681
6682 pragma Assert (Present (Inst_Par));
6683 Set_Entity (Prefix (Gen_Id), Inst_Par);
6684
6685 if In_Enclosing_Instance then
6686 null;
6687
6688 elsif Present (Entity (Gen_Id))
6689 and then Is_Child_Unit (Entity (Gen_Id))
6690 and then not In_Open_Scopes (Inst_Par)
6691 then
6692 Install_Parent (Inst_Par);
6693 Parent_Installed := True;
6694 end if;
6695
6696 elsif In_Enclosing_Instance then
6697
6698 -- The child unit is found in some enclosing scope
6699
6700 null;
6701
6702 else
6703 Analyze (Gen_Id);
6704
6705 -- If this is the renaming of the implicit child in a parent
6706 -- instance, recover the parent name and install it.
6707
6708 if Is_Entity_Name (Gen_Id) then
6709 E := Entity (Gen_Id);
6710
6711 if Is_Generic_Unit (E)
6712 and then Nkind (Parent (E)) in N_Generic_Renaming_Declaration
6713 and then Is_Child_Unit (Renamed_Object (E))
6714 and then Is_Generic_Unit (Scope (Renamed_Object (E)))
6715 and then Nkind (Name (Parent (E))) = N_Expanded_Name
6716 then
6717 Rewrite (Gen_Id, New_Copy_Tree (Name (Parent (E))));
6718 Inst_Par := Entity (Prefix (Gen_Id));
6719
6720 if not In_Open_Scopes (Inst_Par) then
6721 Install_Parent (Inst_Par);
6722 Parent_Installed := True;
6723 end if;
6724
6725 -- If it is a child unit of a non-generic parent, it may be
6726 -- use-visible and given by a direct name. Install parent as
6727 -- for other cases.
6728
6729 elsif Is_Generic_Unit (E)
6730 and then Is_Child_Unit (E)
6731 and then
6732 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
6733 and then not Is_Generic_Unit (Scope (E))
6734 then
6735 if not In_Open_Scopes (Scope (E)) then
6736 Install_Parent (Scope (E));
6737 Parent_Installed := True;
6738 end if;
6739 end if;
6740 end if;
6741 end if;
6742 end Check_Generic_Child_Unit;
6743
6744 -----------------------------
6745 -- Check_Hidden_Child_Unit --
6746 -----------------------------
6747
6748 procedure Check_Hidden_Child_Unit
6749 (N : Node_Id;
6750 Gen_Unit : Entity_Id;
6751 Act_Decl_Id : Entity_Id)
6752 is
6753 Gen_Id : constant Node_Id := Name (N);
6754
6755 begin
6756 if Is_Child_Unit (Gen_Unit)
6757 and then Is_Child_Unit (Act_Decl_Id)
6758 and then Nkind (Gen_Id) = N_Expanded_Name
6759 and then Entity (Prefix (Gen_Id)) = Scope (Act_Decl_Id)
6760 and then Chars (Gen_Unit) = Chars (Act_Decl_Id)
6761 then
6762 Error_Msg_Node_2 := Scope (Act_Decl_Id);
6763 Error_Msg_NE
6764 ("generic unit & is implicitly declared in &",
6765 Defining_Unit_Name (N), Gen_Unit);
6766 Error_Msg_N ("\instance must have different name",
6767 Defining_Unit_Name (N));
6768 end if;
6769 end Check_Hidden_Child_Unit;
6770
6771 ------------------------
6772 -- Check_Private_View --
6773 ------------------------
6774
6775 procedure Check_Private_View (N : Node_Id) is
6776 T : constant Entity_Id := Etype (N);
6777 BT : Entity_Id;
6778
6779 begin
6780 -- Exchange views if the type was not private in the generic but is
6781 -- private at the point of instantiation. Do not exchange views if
6782 -- the scope of the type is in scope. This can happen if both generic
6783 -- and instance are sibling units, or if type is defined in a parent.
6784 -- In this case the visibility of the type will be correct for all
6785 -- semantic checks.
6786
6787 if Present (T) then
6788 BT := Base_Type (T);
6789
6790 if Is_Private_Type (T)
6791 and then not Has_Private_View (N)
6792 and then Present (Full_View (T))
6793 and then not In_Open_Scopes (Scope (T))
6794 then
6795 -- In the generic, the full type was visible. Save the private
6796 -- entity, for subsequent exchange.
6797
6798 Switch_View (T);
6799
6800 elsif Has_Private_View (N)
6801 and then not Is_Private_Type (T)
6802 and then not Has_Been_Exchanged (T)
6803 and then Etype (Get_Associated_Node (N)) /= T
6804 then
6805 -- Only the private declaration was visible in the generic. If
6806 -- the type appears in a subtype declaration, the subtype in the
6807 -- instance must have a view compatible with that of its parent,
6808 -- which must be exchanged (see corresponding code in Restore_
6809 -- Private_Views). Otherwise, if the type is defined in a parent
6810 -- unit, leave full visibility within instance, which is safe.
6811
6812 if In_Open_Scopes (Scope (Base_Type (T)))
6813 and then not Is_Private_Type (Base_Type (T))
6814 and then Comes_From_Source (Base_Type (T))
6815 then
6816 null;
6817
6818 elsif Nkind (Parent (N)) = N_Subtype_Declaration
6819 or else not In_Private_Part (Scope (Base_Type (T)))
6820 then
6821 Prepend_Elmt (T, Exchanged_Views);
6822 Exchange_Declarations (Etype (Get_Associated_Node (N)));
6823 end if;
6824
6825 -- For composite types with inconsistent representation exchange
6826 -- component types accordingly.
6827
6828 elsif Is_Access_Type (T)
6829 and then Is_Private_Type (Designated_Type (T))
6830 and then not Has_Private_View (N)
6831 and then Present (Full_View (Designated_Type (T)))
6832 then
6833 Switch_View (Designated_Type (T));
6834
6835 elsif Is_Array_Type (T) then
6836 if Is_Private_Type (Component_Type (T))
6837 and then not Has_Private_View (N)
6838 and then Present (Full_View (Component_Type (T)))
6839 then
6840 Switch_View (Component_Type (T));
6841 end if;
6842
6843 -- The normal exchange mechanism relies on the setting of a
6844 -- flag on the reference in the generic. However, an additional
6845 -- mechanism is needed for types that are not explicitly
6846 -- mentioned in the generic, but may be needed in expanded code
6847 -- in the instance. This includes component types of arrays and
6848 -- designated types of access types. This processing must also
6849 -- include the index types of arrays which we take care of here.
6850
6851 declare
6852 Indx : Node_Id;
6853 Typ : Entity_Id;
6854
6855 begin
6856 Indx := First_Index (T);
6857 while Present (Indx) loop
6858 Typ := Base_Type (Etype (Indx));
6859
6860 if Is_Private_Type (Typ)
6861 and then Present (Full_View (Typ))
6862 then
6863 Switch_View (Typ);
6864 end if;
6865
6866 Next_Index (Indx);
6867 end loop;
6868 end;
6869
6870 elsif Is_Private_Type (T)
6871 and then Present (Full_View (T))
6872 and then Is_Array_Type (Full_View (T))
6873 and then Is_Private_Type (Component_Type (Full_View (T)))
6874 then
6875 Switch_View (T);
6876
6877 -- Finally, a non-private subtype may have a private base type, which
6878 -- must be exchanged for consistency. This can happen when a package
6879 -- body is instantiated, when the scope stack is empty but in fact
6880 -- the subtype and the base type are declared in an enclosing scope.
6881
6882 -- Note that in this case we introduce an inconsistency in the view
6883 -- set, because we switch the base type BT, but there could be some
6884 -- private dependent subtypes of BT which remain unswitched. Such
6885 -- subtypes might need to be switched at a later point (see specific
6886 -- provision for that case in Switch_View).
6887
6888 elsif not Is_Private_Type (T)
6889 and then not Has_Private_View (N)
6890 and then Is_Private_Type (BT)
6891 and then Present (Full_View (BT))
6892 and then not Is_Generic_Type (BT)
6893 and then not In_Open_Scopes (BT)
6894 then
6895 Prepend_Elmt (Full_View (BT), Exchanged_Views);
6896 Exchange_Declarations (BT);
6897 end if;
6898 end if;
6899 end Check_Private_View;
6900
6901 -----------------------------
6902 -- Check_Hidden_Primitives --
6903 -----------------------------
6904
6905 function Check_Hidden_Primitives (Assoc_List : List_Id) return Elist_Id is
6906 Actual : Node_Id;
6907 Gen_T : Entity_Id;
6908 Result : Elist_Id := No_Elist;
6909
6910 begin
6911 if No (Assoc_List) then
6912 return No_Elist;
6913 end if;
6914
6915 -- Traverse the list of associations between formals and actuals
6916 -- searching for renamings of tagged types
6917
6918 Actual := First (Assoc_List);
6919 while Present (Actual) loop
6920 if Nkind (Actual) = N_Subtype_Declaration then
6921 Gen_T := Generic_Parent_Type (Actual);
6922
6923 if Present (Gen_T) and then Is_Tagged_Type (Gen_T) then
6924
6925 -- Traverse the list of primitives of the actual types
6926 -- searching for hidden primitives that are visible in the
6927 -- corresponding generic formal; leave them visible and
6928 -- append them to Result to restore their decoration later.
6929
6930 Install_Hidden_Primitives
6931 (Prims_List => Result,
6932 Gen_T => Gen_T,
6933 Act_T => Entity (Subtype_Indication (Actual)));
6934 end if;
6935 end if;
6936
6937 Next (Actual);
6938 end loop;
6939
6940 return Result;
6941 end Check_Hidden_Primitives;
6942
6943 --------------------------
6944 -- Contains_Instance_Of --
6945 --------------------------
6946
6947 function Contains_Instance_Of
6948 (Inner : Entity_Id;
6949 Outer : Entity_Id;
6950 N : Node_Id) return Boolean
6951 is
6952 Elmt : Elmt_Id;
6953 Scop : Entity_Id;
6954
6955 begin
6956 Scop := Outer;
6957
6958 -- Verify that there are no circular instantiations. We check whether
6959 -- the unit contains an instance of the current scope or some enclosing
6960 -- scope (in case one of the instances appears in a subunit). Longer
6961 -- circularities involving subunits might seem too pathological to
6962 -- consider, but they were not too pathological for the authors of
6963 -- DEC bc30vsq, so we loop over all enclosing scopes, and mark all
6964 -- enclosing generic scopes as containing an instance.
6965
6966 loop
6967 -- Within a generic subprogram body, the scope is not generic, to
6968 -- allow for recursive subprograms. Use the declaration to determine
6969 -- whether this is a generic unit.
6970
6971 if Ekind (Scop) = E_Generic_Package
6972 or else (Is_Subprogram (Scop)
6973 and then Nkind (Unit_Declaration_Node (Scop)) =
6974 N_Generic_Subprogram_Declaration)
6975 then
6976 Elmt := First_Elmt (Inner_Instances (Inner));
6977
6978 while Present (Elmt) loop
6979 if Node (Elmt) = Scop then
6980 Error_Msg_Node_2 := Inner;
6981 Error_Msg_NE
6982 ("circular Instantiation: & instantiated within &!",
6983 N, Scop);
6984 return True;
6985
6986 elsif Node (Elmt) = Inner then
6987 return True;
6988
6989 elsif Contains_Instance_Of (Node (Elmt), Scop, N) then
6990 Error_Msg_Node_2 := Inner;
6991 Error_Msg_NE
6992 ("circular Instantiation: & instantiated within &!",
6993 N, Node (Elmt));
6994 return True;
6995 end if;
6996
6997 Next_Elmt (Elmt);
6998 end loop;
6999
7000 -- Indicate that Inner is being instantiated within Scop
7001
7002 Append_Elmt (Inner, Inner_Instances (Scop));
7003 end if;
7004
7005 if Scop = Standard_Standard then
7006 exit;
7007 else
7008 Scop := Scope (Scop);
7009 end if;
7010 end loop;
7011
7012 return False;
7013 end Contains_Instance_Of;
7014
7015 -----------------------
7016 -- Copy_Generic_Node --
7017 -----------------------
7018
7019 function Copy_Generic_Node
7020 (N : Node_Id;
7021 Parent_Id : Node_Id;
7022 Instantiating : Boolean) return Node_Id
7023 is
7024 Ent : Entity_Id;
7025 New_N : Node_Id;
7026
7027 function Copy_Generic_Descendant (D : Union_Id) return Union_Id;
7028 -- Check the given value of one of the Fields referenced by the current
7029 -- node to determine whether to copy it recursively. The field may hold
7030 -- a Node_Id, a List_Id, or an Elist_Id, or a plain value (Sloc, Uint,
7031 -- Char) in which case it need not be copied.
7032
7033 procedure Copy_Descendants;
7034 -- Common utility for various nodes
7035
7036 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id;
7037 -- Make copy of element list
7038
7039 function Copy_Generic_List
7040 (L : List_Id;
7041 Parent_Id : Node_Id) return List_Id;
7042 -- Apply Copy_Node recursively to the members of a node list
7043
7044 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean;
7045 -- True if an identifier is part of the defining program unit name of
7046 -- a child unit. The entity of such an identifier must be kept (for
7047 -- ASIS use) even though as the name of an enclosing generic it would
7048 -- otherwise not be preserved in the generic tree.
7049
7050 ----------------------
7051 -- Copy_Descendants --
7052 ----------------------
7053
7054 procedure Copy_Descendants is
7055 use Atree.Unchecked_Access;
7056 -- This code section is part of the implementation of an untyped
7057 -- tree traversal, so it needs direct access to node fields.
7058
7059 begin
7060 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
7061 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
7062 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
7063 Set_Field4 (New_N, Copy_Generic_Descendant (Field4 (N)));
7064 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
7065 end Copy_Descendants;
7066
7067 -----------------------------
7068 -- Copy_Generic_Descendant --
7069 -----------------------------
7070
7071 function Copy_Generic_Descendant (D : Union_Id) return Union_Id is
7072 begin
7073 if D = Union_Id (Empty) then
7074 return D;
7075
7076 elsif D in Node_Range then
7077 return Union_Id
7078 (Copy_Generic_Node (Node_Id (D), New_N, Instantiating));
7079
7080 elsif D in List_Range then
7081 return Union_Id (Copy_Generic_List (List_Id (D), New_N));
7082
7083 elsif D in Elist_Range then
7084 return Union_Id (Copy_Generic_Elist (Elist_Id (D)));
7085
7086 -- Nothing else is copyable (e.g. Uint values), return as is
7087
7088 else
7089 return D;
7090 end if;
7091 end Copy_Generic_Descendant;
7092
7093 ------------------------
7094 -- Copy_Generic_Elist --
7095 ------------------------
7096
7097 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id is
7098 M : Elmt_Id;
7099 L : Elist_Id;
7100
7101 begin
7102 if Present (E) then
7103 L := New_Elmt_List;
7104 M := First_Elmt (E);
7105 while Present (M) loop
7106 Append_Elmt
7107 (Copy_Generic_Node (Node (M), Empty, Instantiating), L);
7108 Next_Elmt (M);
7109 end loop;
7110
7111 return L;
7112
7113 else
7114 return No_Elist;
7115 end if;
7116 end Copy_Generic_Elist;
7117
7118 -----------------------
7119 -- Copy_Generic_List --
7120 -----------------------
7121
7122 function Copy_Generic_List
7123 (L : List_Id;
7124 Parent_Id : Node_Id) return List_Id
7125 is
7126 N : Node_Id;
7127 New_L : List_Id;
7128
7129 begin
7130 if Present (L) then
7131 New_L := New_List;
7132 Set_Parent (New_L, Parent_Id);
7133
7134 N := First (L);
7135 while Present (N) loop
7136 Append (Copy_Generic_Node (N, Empty, Instantiating), New_L);
7137 Next (N);
7138 end loop;
7139
7140 return New_L;
7141
7142 else
7143 return No_List;
7144 end if;
7145 end Copy_Generic_List;
7146
7147 ---------------------------
7148 -- In_Defining_Unit_Name --
7149 ---------------------------
7150
7151 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean is
7152 begin
7153 return
7154 Present (Parent (Nam))
7155 and then (Nkind (Parent (Nam)) = N_Defining_Program_Unit_Name
7156 or else
7157 (Nkind (Parent (Nam)) = N_Expanded_Name
7158 and then In_Defining_Unit_Name (Parent (Nam))));
7159 end In_Defining_Unit_Name;
7160
7161 -- Start of processing for Copy_Generic_Node
7162
7163 begin
7164 if N = Empty then
7165 return N;
7166 end if;
7167
7168 New_N := New_Copy (N);
7169
7170 -- Copy aspects if present
7171
7172 if Has_Aspects (N) then
7173 Set_Has_Aspects (New_N, False);
7174 Set_Aspect_Specifications
7175 (New_N, Copy_Generic_List (Aspect_Specifications (N), Parent_Id));
7176 end if;
7177
7178 if Instantiating then
7179 Adjust_Instantiation_Sloc (New_N, S_Adjustment);
7180 end if;
7181
7182 if not Is_List_Member (N) then
7183 Set_Parent (New_N, Parent_Id);
7184 end if;
7185
7186 -- Special casing for identifiers and other entity names and operators
7187
7188 if Nkind_In (New_N, N_Character_Literal,
7189 N_Expanded_Name,
7190 N_Identifier,
7191 N_Operator_Symbol)
7192 or else Nkind (New_N) in N_Op
7193 then
7194 if not Instantiating then
7195
7196 -- Link both nodes in order to assign subsequently the entity of
7197 -- the copy to the original node, in case this is a global
7198 -- reference.
7199
7200 Set_Associated_Node (N, New_N);
7201
7202 -- If we are within an instantiation, this is a nested generic
7203 -- that has already been analyzed at the point of definition.
7204 -- We must preserve references that were global to the enclosing
7205 -- parent at that point. Other occurrences, whether global or
7206 -- local to the current generic, must be resolved anew, so we
7207 -- reset the entity in the generic copy. A global reference has a
7208 -- smaller depth than the parent, or else the same depth in case
7209 -- both are distinct compilation units.
7210
7211 -- A child unit is implicitly declared within the enclosing parent
7212 -- but is in fact global to it, and must be preserved.
7213
7214 -- It is also possible for Current_Instantiated_Parent to be
7215 -- defined, and for this not to be a nested generic, namely if
7216 -- the unit is loaded through Rtsfind. In that case, the entity of
7217 -- New_N is only a link to the associated node, and not a defining
7218 -- occurrence.
7219
7220 -- The entities for parent units in the defining_program_unit of a
7221 -- generic child unit are established when the context of the unit
7222 -- is first analyzed, before the generic copy is made. They are
7223 -- preserved in the copy for use in ASIS queries.
7224
7225 Ent := Entity (New_N);
7226
7227 if No (Current_Instantiated_Parent.Gen_Id) then
7228 if No (Ent)
7229 or else Nkind (Ent) /= N_Defining_Identifier
7230 or else not In_Defining_Unit_Name (N)
7231 then
7232 Set_Associated_Node (New_N, Empty);
7233 end if;
7234
7235 elsif No (Ent)
7236 or else
7237 not Nkind_In (Ent, N_Defining_Identifier,
7238 N_Defining_Character_Literal,
7239 N_Defining_Operator_Symbol)
7240 or else No (Scope (Ent))
7241 or else
7242 (Scope (Ent) = Current_Instantiated_Parent.Gen_Id
7243 and then not Is_Child_Unit (Ent))
7244 or else
7245 (Scope_Depth (Scope (Ent)) >
7246 Scope_Depth (Current_Instantiated_Parent.Gen_Id)
7247 and then
7248 Get_Source_Unit (Ent) =
7249 Get_Source_Unit (Current_Instantiated_Parent.Gen_Id))
7250 then
7251 Set_Associated_Node (New_N, Empty);
7252 end if;
7253
7254 -- Case of instantiating identifier or some other name or operator
7255
7256 else
7257 -- If the associated node is still defined, the entity in it
7258 -- is global, and must be copied to the instance. If this copy
7259 -- is being made for a body to inline, it is applied to an
7260 -- instantiated tree, and the entity is already present and
7261 -- must be also preserved.
7262
7263 declare
7264 Assoc : constant Node_Id := Get_Associated_Node (N);
7265
7266 begin
7267 if Present (Assoc) then
7268 if Nkind (Assoc) = Nkind (N) then
7269 Set_Entity (New_N, Entity (Assoc));
7270 Check_Private_View (N);
7271
7272 -- The name in the call may be a selected component if the
7273 -- call has not been analyzed yet, as may be the case for
7274 -- pre/post conditions in a generic unit.
7275
7276 elsif Nkind (Assoc) = N_Function_Call
7277 and then Is_Entity_Name (Name (Assoc))
7278 then
7279 Set_Entity (New_N, Entity (Name (Assoc)));
7280
7281 elsif Nkind_In (Assoc, N_Defining_Identifier,
7282 N_Defining_Character_Literal,
7283 N_Defining_Operator_Symbol)
7284 and then Expander_Active
7285 then
7286 -- Inlining case: we are copying a tree that contains
7287 -- global entities, which are preserved in the copy to be
7288 -- used for subsequent inlining.
7289
7290 null;
7291
7292 else
7293 Set_Entity (New_N, Empty);
7294 end if;
7295 end if;
7296 end;
7297 end if;
7298
7299 -- For expanded name, we must copy the Prefix and Selector_Name
7300
7301 if Nkind (N) = N_Expanded_Name then
7302 Set_Prefix
7303 (New_N, Copy_Generic_Node (Prefix (N), New_N, Instantiating));
7304
7305 Set_Selector_Name (New_N,
7306 Copy_Generic_Node (Selector_Name (N), New_N, Instantiating));
7307
7308 -- For operators, we must copy the right operand
7309
7310 elsif Nkind (N) in N_Op then
7311 Set_Right_Opnd (New_N,
7312 Copy_Generic_Node (Right_Opnd (N), New_N, Instantiating));
7313
7314 -- And for binary operators, the left operand as well
7315
7316 if Nkind (N) in N_Binary_Op then
7317 Set_Left_Opnd (New_N,
7318 Copy_Generic_Node (Left_Opnd (N), New_N, Instantiating));
7319 end if;
7320 end if;
7321
7322 -- Establish a link between an entity from the generic template and the
7323 -- corresponding entity in the generic copy to be analyzed.
7324
7325 elsif Nkind (N) in N_Entity then
7326 if not Instantiating then
7327 Set_Associated_Entity (N, New_N);
7328 end if;
7329
7330 -- Clear any existing link the copy may inherit from the replicated
7331 -- generic template entity.
7332
7333 Set_Associated_Entity (New_N, Empty);
7334
7335 -- Special casing for stubs
7336
7337 elsif Nkind (N) in N_Body_Stub then
7338
7339 -- In any case, we must copy the specification or defining
7340 -- identifier as appropriate.
7341
7342 if Nkind (N) = N_Subprogram_Body_Stub then
7343 Set_Specification (New_N,
7344 Copy_Generic_Node (Specification (N), New_N, Instantiating));
7345
7346 else
7347 Set_Defining_Identifier (New_N,
7348 Copy_Generic_Node
7349 (Defining_Identifier (N), New_N, Instantiating));
7350 end if;
7351
7352 -- If we are not instantiating, then this is where we load and
7353 -- analyze subunits, i.e. at the point where the stub occurs. A
7354 -- more permissive system might defer this analysis to the point
7355 -- of instantiation, but this seems too complicated for now.
7356
7357 if not Instantiating then
7358 declare
7359 Subunit_Name : constant Unit_Name_Type := Get_Unit_Name (N);
7360 Subunit : Node_Id;
7361 Unum : Unit_Number_Type;
7362 New_Body : Node_Id;
7363
7364 begin
7365 -- Make sure that, if it is a subunit of the main unit that is
7366 -- preprocessed and if -gnateG is specified, the preprocessed
7367 -- file will be written.
7368
7369 Lib.Analysing_Subunit_Of_Main :=
7370 Lib.In_Extended_Main_Source_Unit (N);
7371 Unum :=
7372 Load_Unit
7373 (Load_Name => Subunit_Name,
7374 Required => False,
7375 Subunit => True,
7376 Error_Node => N);
7377 Lib.Analysing_Subunit_Of_Main := False;
7378
7379 -- If the proper body is not found, a warning message will be
7380 -- emitted when analyzing the stub, or later at the point of
7381 -- instantiation. Here we just leave the stub as is.
7382
7383 if Unum = No_Unit then
7384 Subunits_Missing := True;
7385 goto Subunit_Not_Found;
7386 end if;
7387
7388 Subunit := Cunit (Unum);
7389
7390 if Nkind (Unit (Subunit)) /= N_Subunit then
7391 Error_Msg_N
7392 ("found child unit instead of expected SEPARATE subunit",
7393 Subunit);
7394 Error_Msg_Sloc := Sloc (N);
7395 Error_Msg_N ("\to complete stub #", Subunit);
7396 goto Subunit_Not_Found;
7397 end if;
7398
7399 -- We must create a generic copy of the subunit, in order to
7400 -- perform semantic analysis on it, and we must replace the
7401 -- stub in the original generic unit with the subunit, in order
7402 -- to preserve non-local references within.
7403
7404 -- Only the proper body needs to be copied. Library_Unit and
7405 -- context clause are simply inherited by the generic copy.
7406 -- Note that the copy (which may be recursive if there are
7407 -- nested subunits) must be done first, before attaching it to
7408 -- the enclosing generic.
7409
7410 New_Body :=
7411 Copy_Generic_Node
7412 (Proper_Body (Unit (Subunit)),
7413 Empty, Instantiating => False);
7414
7415 -- Now place the original proper body in the original generic
7416 -- unit. This is a body, not a compilation unit.
7417
7418 Rewrite (N, Proper_Body (Unit (Subunit)));
7419 Set_Is_Compilation_Unit (Defining_Entity (N), False);
7420 Set_Was_Originally_Stub (N);
7421
7422 -- Finally replace the body of the subunit with its copy, and
7423 -- make this new subunit into the library unit of the generic
7424 -- copy, which does not have stubs any longer.
7425
7426 Set_Proper_Body (Unit (Subunit), New_Body);
7427 Set_Library_Unit (New_N, Subunit);
7428 Inherit_Context (Unit (Subunit), N);
7429 end;
7430
7431 -- If we are instantiating, this must be an error case, since
7432 -- otherwise we would have replaced the stub node by the proper body
7433 -- that corresponds. So just ignore it in the copy (i.e. we have
7434 -- copied it, and that is good enough).
7435
7436 else
7437 null;
7438 end if;
7439
7440 <<Subunit_Not_Found>> null;
7441
7442 -- If the node is a compilation unit, it is the subunit of a stub, which
7443 -- has been loaded already (see code below). In this case, the library
7444 -- unit field of N points to the parent unit (which is a compilation
7445 -- unit) and need not (and cannot) be copied.
7446
7447 -- When the proper body of the stub is analyzed, the library_unit link
7448 -- is used to establish the proper context (see sem_ch10).
7449
7450 -- The other fields of a compilation unit are copied as usual
7451
7452 elsif Nkind (N) = N_Compilation_Unit then
7453
7454 -- This code can only be executed when not instantiating, because in
7455 -- the copy made for an instantiation, the compilation unit node has
7456 -- disappeared at the point that a stub is replaced by its proper
7457 -- body.
7458
7459 pragma Assert (not Instantiating);
7460
7461 Set_Context_Items (New_N,
7462 Copy_Generic_List (Context_Items (N), New_N));
7463
7464 Set_Unit (New_N,
7465 Copy_Generic_Node (Unit (N), New_N, False));
7466
7467 Set_First_Inlined_Subprogram (New_N,
7468 Copy_Generic_Node
7469 (First_Inlined_Subprogram (N), New_N, False));
7470
7471 Set_Aux_Decls_Node (New_N,
7472 Copy_Generic_Node (Aux_Decls_Node (N), New_N, False));
7473
7474 -- For an assignment node, the assignment is known to be semantically
7475 -- legal if we are instantiating the template. This avoids incorrect
7476 -- diagnostics in generated code.
7477
7478 elsif Nkind (N) = N_Assignment_Statement then
7479
7480 -- Copy name and expression fields in usual manner
7481
7482 Set_Name (New_N,
7483 Copy_Generic_Node (Name (N), New_N, Instantiating));
7484
7485 Set_Expression (New_N,
7486 Copy_Generic_Node (Expression (N), New_N, Instantiating));
7487
7488 if Instantiating then
7489 Set_Assignment_OK (Name (New_N), True);
7490 end if;
7491
7492 elsif Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
7493 if not Instantiating then
7494 Set_Associated_Node (N, New_N);
7495
7496 else
7497 if Present (Get_Associated_Node (N))
7498 and then Nkind (Get_Associated_Node (N)) = Nkind (N)
7499 then
7500 -- In the generic the aggregate has some composite type. If at
7501 -- the point of instantiation the type has a private view,
7502 -- install the full view (and that of its ancestors, if any).
7503
7504 declare
7505 T : Entity_Id := (Etype (Get_Associated_Node (New_N)));
7506 Rt : Entity_Id;
7507
7508 begin
7509 if Present (T) and then Is_Private_Type (T) then
7510 Switch_View (T);
7511 end if;
7512
7513 if Present (T)
7514 and then Is_Tagged_Type (T)
7515 and then Is_Derived_Type (T)
7516 then
7517 Rt := Root_Type (T);
7518
7519 loop
7520 T := Etype (T);
7521
7522 if Is_Private_Type (T) then
7523 Switch_View (T);
7524 end if;
7525
7526 exit when T = Rt;
7527 end loop;
7528 end if;
7529 end;
7530 end if;
7531 end if;
7532
7533 -- Do not copy the associated node, which points to the generic copy
7534 -- of the aggregate.
7535
7536 declare
7537 use Atree.Unchecked_Access;
7538 -- This code section is part of the implementation of an untyped
7539 -- tree traversal, so it needs direct access to node fields.
7540
7541 begin
7542 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
7543 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
7544 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
7545 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
7546 end;
7547
7548 -- Allocators do not have an identifier denoting the access type, so we
7549 -- must locate it through the expression to check whether the views are
7550 -- consistent.
7551
7552 elsif Nkind (N) = N_Allocator
7553 and then Nkind (Expression (N)) = N_Qualified_Expression
7554 and then Is_Entity_Name (Subtype_Mark (Expression (N)))
7555 and then Instantiating
7556 then
7557 declare
7558 T : constant Node_Id :=
7559 Get_Associated_Node (Subtype_Mark (Expression (N)));
7560 Acc_T : Entity_Id;
7561
7562 begin
7563 if Present (T) then
7564
7565 -- Retrieve the allocator node in the generic copy
7566
7567 Acc_T := Etype (Parent (Parent (T)));
7568
7569 if Present (Acc_T) and then Is_Private_Type (Acc_T) then
7570 Switch_View (Acc_T);
7571 end if;
7572 end if;
7573
7574 Copy_Descendants;
7575 end;
7576
7577 -- For a proper body, we must catch the case of a proper body that
7578 -- replaces a stub. This represents the point at which a separate
7579 -- compilation unit, and hence template file, may be referenced, so we
7580 -- must make a new source instantiation entry for the template of the
7581 -- subunit, and ensure that all nodes in the subunit are adjusted using
7582 -- this new source instantiation entry.
7583
7584 elsif Nkind (N) in N_Proper_Body then
7585 declare
7586 Save_Adjustment : constant Sloc_Adjustment := S_Adjustment;
7587
7588 begin
7589 if Instantiating and then Was_Originally_Stub (N) then
7590 Create_Instantiation_Source
7591 (Instantiation_Node,
7592 Defining_Entity (N),
7593 False,
7594 S_Adjustment);
7595 end if;
7596
7597 -- Now copy the fields of the proper body, using the new
7598 -- adjustment factor if one was needed as per test above.
7599
7600 Copy_Descendants;
7601
7602 -- Restore the original adjustment factor in case changed
7603
7604 S_Adjustment := Save_Adjustment;
7605 end;
7606
7607 elsif Nkind (N) = N_Pragma and then Instantiating then
7608
7609 -- Do not copy Comment or Ident pragmas their content is relevant to
7610 -- the generic unit, not to the instantiating unit.
7611
7612 if Nam_In (Pragma_Name (N), Name_Comment, Name_Ident) then
7613 New_N := Make_Null_Statement (Sloc (N));
7614
7615 -- Do not copy pragmas generated from aspects because the pragmas do
7616 -- not carry any semantic information, plus they will be regenerated
7617 -- in the instance.
7618
7619 elsif From_Aspect_Specification (N) then
7620 New_N := Make_Null_Statement (Sloc (N));
7621
7622 else
7623 Copy_Descendants;
7624 end if;
7625
7626 elsif Nkind_In (N, N_Integer_Literal, N_Real_Literal) then
7627
7628 -- No descendant fields need traversing
7629
7630 null;
7631
7632 elsif Nkind (N) = N_String_Literal
7633 and then Present (Etype (N))
7634 and then Instantiating
7635 then
7636 -- If the string is declared in an outer scope, the string_literal
7637 -- subtype created for it may have the wrong scope. Force reanalysis
7638 -- of the constant to generate a new itype in the proper context.
7639
7640 Set_Etype (New_N, Empty);
7641 Set_Analyzed (New_N, False);
7642
7643 -- For the remaining nodes, copy their descendants recursively
7644
7645 else
7646 Copy_Descendants;
7647
7648 if Instantiating and then Nkind (N) = N_Subprogram_Body then
7649 Set_Generic_Parent (Specification (New_N), N);
7650
7651 -- Should preserve Corresponding_Spec??? (12.3(14))
7652 end if;
7653 end if;
7654
7655 return New_N;
7656 end Copy_Generic_Node;
7657
7658 ----------------------------
7659 -- Denotes_Formal_Package --
7660 ----------------------------
7661
7662 function Denotes_Formal_Package
7663 (Pack : Entity_Id;
7664 On_Exit : Boolean := False;
7665 Instance : Entity_Id := Empty) return Boolean
7666 is
7667 Par : Entity_Id;
7668 Scop : constant Entity_Id := Scope (Pack);
7669 E : Entity_Id;
7670
7671 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean;
7672 -- The package in question may be an actual for a previous formal
7673 -- package P of the current instance, so examine its actuals as well.
7674 -- This must be recursive over other formal packages.
7675
7676 ----------------------------------
7677 -- Is_Actual_Of_Previous_Formal --
7678 ----------------------------------
7679
7680 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean is
7681 E1 : Entity_Id;
7682
7683 begin
7684 E1 := First_Entity (P);
7685 while Present (E1) and then E1 /= Instance loop
7686 if Ekind (E1) = E_Package
7687 and then Nkind (Parent (E1)) = N_Package_Renaming_Declaration
7688 then
7689 if Renamed_Object (E1) = Pack then
7690 return True;
7691
7692 elsif E1 = P or else Renamed_Object (E1) = P then
7693 return False;
7694
7695 elsif Is_Actual_Of_Previous_Formal (E1) then
7696 return True;
7697 end if;
7698 end if;
7699
7700 Next_Entity (E1);
7701 end loop;
7702
7703 return False;
7704 end Is_Actual_Of_Previous_Formal;
7705
7706 -- Start of processing for Denotes_Formal_Package
7707
7708 begin
7709 if On_Exit then
7710 Par :=
7711 Instance_Envs.Table
7712 (Instance_Envs.Last).Instantiated_Parent.Act_Id;
7713 else
7714 Par := Current_Instantiated_Parent.Act_Id;
7715 end if;
7716
7717 if Ekind (Scop) = E_Generic_Package
7718 or else Nkind (Unit_Declaration_Node (Scop)) =
7719 N_Generic_Subprogram_Declaration
7720 then
7721 return True;
7722
7723 elsif Nkind (Original_Node (Unit_Declaration_Node (Pack))) =
7724 N_Formal_Package_Declaration
7725 then
7726 return True;
7727
7728 elsif No (Par) then
7729 return False;
7730
7731 else
7732 -- Check whether this package is associated with a formal package of
7733 -- the enclosing instantiation. Iterate over the list of renamings.
7734
7735 E := First_Entity (Par);
7736 while Present (E) loop
7737 if Ekind (E) /= E_Package
7738 or else Nkind (Parent (E)) /= N_Package_Renaming_Declaration
7739 then
7740 null;
7741
7742 elsif Renamed_Object (E) = Par then
7743 return False;
7744
7745 elsif Renamed_Object (E) = Pack then
7746 return True;
7747
7748 elsif Is_Actual_Of_Previous_Formal (E) then
7749 return True;
7750
7751 end if;
7752
7753 Next_Entity (E);
7754 end loop;
7755
7756 return False;
7757 end if;
7758 end Denotes_Formal_Package;
7759
7760 -----------------
7761 -- End_Generic --
7762 -----------------
7763
7764 procedure End_Generic is
7765 begin
7766 -- ??? More things could be factored out in this routine. Should
7767 -- probably be done at a later stage.
7768
7769 Inside_A_Generic := Generic_Flags.Table (Generic_Flags.Last);
7770 Generic_Flags.Decrement_Last;
7771
7772 Expander_Mode_Restore;
7773 end End_Generic;
7774
7775 -------------
7776 -- Earlier --
7777 -------------
7778
7779 function Earlier (N1, N2 : Node_Id) return Boolean is
7780 procedure Find_Depth (P : in out Node_Id; D : in out Integer);
7781 -- Find distance from given node to enclosing compilation unit
7782
7783 ----------------
7784 -- Find_Depth --
7785 ----------------
7786
7787 procedure Find_Depth (P : in out Node_Id; D : in out Integer) is
7788 begin
7789 while Present (P)
7790 and then Nkind (P) /= N_Compilation_Unit
7791 loop
7792 P := True_Parent (P);
7793 D := D + 1;
7794 end loop;
7795 end Find_Depth;
7796
7797 -- Local declarations
7798
7799 D1 : Integer := 0;
7800 D2 : Integer := 0;
7801 P1 : Node_Id := N1;
7802 P2 : Node_Id := N2;
7803 T1 : Source_Ptr;
7804 T2 : Source_Ptr;
7805
7806 -- Start of processing for Earlier
7807
7808 begin
7809 Find_Depth (P1, D1);
7810 Find_Depth (P2, D2);
7811
7812 if P1 /= P2 then
7813 return False;
7814 else
7815 P1 := N1;
7816 P2 := N2;
7817 end if;
7818
7819 while D1 > D2 loop
7820 P1 := True_Parent (P1);
7821 D1 := D1 - 1;
7822 end loop;
7823
7824 while D2 > D1 loop
7825 P2 := True_Parent (P2);
7826 D2 := D2 - 1;
7827 end loop;
7828
7829 -- At this point P1 and P2 are at the same distance from the root.
7830 -- We examine their parents until we find a common declarative list.
7831 -- If we reach the root, N1 and N2 do not descend from the same
7832 -- declarative list (e.g. one is nested in the declarative part and
7833 -- the other is in a block in the statement part) and the earlier
7834 -- one is already frozen.
7835
7836 while not Is_List_Member (P1)
7837 or else not Is_List_Member (P2)
7838 or else List_Containing (P1) /= List_Containing (P2)
7839 loop
7840 P1 := True_Parent (P1);
7841 P2 := True_Parent (P2);
7842
7843 if Nkind (Parent (P1)) = N_Subunit then
7844 P1 := Corresponding_Stub (Parent (P1));
7845 end if;
7846
7847 if Nkind (Parent (P2)) = N_Subunit then
7848 P2 := Corresponding_Stub (Parent (P2));
7849 end if;
7850
7851 if P1 = P2 then
7852 return False;
7853 end if;
7854 end loop;
7855
7856 -- Expanded code usually shares the source location of the original
7857 -- construct it was generated for. This however may not necessarely
7858 -- reflect the true location of the code within the tree.
7859
7860 -- Before comparing the slocs of the two nodes, make sure that we are
7861 -- working with correct source locations. Assume that P1 is to the left
7862 -- of P2. If either one does not come from source, traverse the common
7863 -- list heading towards the other node and locate the first source
7864 -- statement.
7865
7866 -- P1 P2
7867 -- ----+===+===+--------------+===+===+----
7868 -- expanded code expanded code
7869
7870 if not Comes_From_Source (P1) then
7871 while Present (P1) loop
7872
7873 -- Neither P2 nor a source statement were located during the
7874 -- search. If we reach the end of the list, then P1 does not
7875 -- occur earlier than P2.
7876
7877 -- ---->
7878 -- start --- P2 ----- P1 --- end
7879
7880 if No (Next (P1)) then
7881 return False;
7882
7883 -- We encounter P2 while going to the right 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 P1 = P2 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 (P1) then
7897 exit;
7898 end if;
7899
7900 -- Keep going right
7901
7902 Next (P1);
7903 end loop;
7904 end if;
7905
7906 if not Comes_From_Source (P2) then
7907 while Present (P2) loop
7908
7909 -- Neither P1 nor a source statement were located during the
7910 -- search. If we reach the start of the list, then P1 does not
7911 -- occur earlier than P2.
7912
7913 -- <----
7914 -- start --- P2 --- P1 --- end
7915
7916 if No (Prev (P2)) then
7917 return False;
7918
7919 -- We encounter P1 while going to the left of the list. This
7920 -- means that P1 does indeed appear earlier.
7921
7922 -- <----
7923 -- start --- P1 ===== P2 --- end
7924 -- expanded code in between
7925
7926 elsif P2 = P1 then
7927 return True;
7928
7929 -- No need to look any further since we have located a source
7930 -- statement.
7931
7932 elsif Comes_From_Source (P2) then
7933 exit;
7934 end if;
7935
7936 -- Keep going left
7937
7938 Prev (P2);
7939 end loop;
7940 end if;
7941
7942 -- At this point either both nodes came from source or we approximated
7943 -- their source locations through neighbouring source statements.
7944
7945 T1 := Top_Level_Location (Sloc (P1));
7946 T2 := Top_Level_Location (Sloc (P2));
7947
7948 -- When two nodes come from the same instance, they have identical top
7949 -- level locations. To determine proper relation within the tree, check
7950 -- their locations within the template.
7951
7952 if T1 = T2 then
7953 return Sloc (P1) < Sloc (P2);
7954
7955 -- The two nodes either come from unrelated instances or do not come
7956 -- from instantiated code at all.
7957
7958 else
7959 return T1 < T2;
7960 end if;
7961 end Earlier;
7962
7963 ----------------------
7964 -- Find_Actual_Type --
7965 ----------------------
7966
7967 function Find_Actual_Type
7968 (Typ : Entity_Id;
7969 Gen_Type : Entity_Id) return Entity_Id
7970 is
7971 Gen_Scope : constant Entity_Id := Scope (Gen_Type);
7972 T : Entity_Id;
7973
7974 begin
7975 -- Special processing only applies to child units
7976
7977 if not Is_Child_Unit (Gen_Scope) then
7978 return Get_Instance_Of (Typ);
7979
7980 -- If designated or component type is itself a formal of the child unit,
7981 -- its instance is available.
7982
7983 elsif Scope (Typ) = Gen_Scope then
7984 return Get_Instance_Of (Typ);
7985
7986 -- If the array or access type is not declared in the parent unit,
7987 -- no special processing needed.
7988
7989 elsif not Is_Generic_Type (Typ)
7990 and then Scope (Gen_Scope) /= Scope (Typ)
7991 then
7992 return Get_Instance_Of (Typ);
7993
7994 -- Otherwise, retrieve designated or component type by visibility
7995
7996 else
7997 T := Current_Entity (Typ);
7998 while Present (T) loop
7999 if In_Open_Scopes (Scope (T)) then
8000 return T;
8001 elsif Is_Generic_Actual_Type (T) then
8002 return T;
8003 end if;
8004
8005 T := Homonym (T);
8006 end loop;
8007
8008 return Typ;
8009 end if;
8010 end Find_Actual_Type;
8011
8012 ----------------------------
8013 -- Freeze_Subprogram_Body --
8014 ----------------------------
8015
8016 procedure Freeze_Subprogram_Body
8017 (Inst_Node : Node_Id;
8018 Gen_Body : Node_Id;
8019 Pack_Id : Entity_Id)
8020 is
8021 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
8022 Par : constant Entity_Id := Scope (Gen_Unit);
8023 E_G_Id : Entity_Id;
8024 Enc_G : Entity_Id;
8025 Enc_I : Node_Id;
8026 F_Node : Node_Id;
8027
8028 function Enclosing_Package_Body (N : Node_Id) return Node_Id;
8029 -- Find innermost package body that encloses the given node, and which
8030 -- is not a compilation unit. Freeze nodes for the instance, or for its
8031 -- enclosing body, may be inserted after the enclosing_body of the
8032 -- generic unit. Used to determine proper placement of freeze node for
8033 -- both package and subprogram instances.
8034
8035 function Package_Freeze_Node (B : Node_Id) return Node_Id;
8036 -- Find entity for given package body, and locate or create a freeze
8037 -- node for it.
8038
8039 ----------------------------
8040 -- Enclosing_Package_Body --
8041 ----------------------------
8042
8043 function Enclosing_Package_Body (N : Node_Id) return Node_Id is
8044 P : Node_Id;
8045
8046 begin
8047 P := Parent (N);
8048 while Present (P)
8049 and then Nkind (Parent (P)) /= N_Compilation_Unit
8050 loop
8051 if Nkind (P) = N_Package_Body then
8052 if Nkind (Parent (P)) = N_Subunit then
8053 return Corresponding_Stub (Parent (P));
8054 else
8055 return P;
8056 end if;
8057 end if;
8058
8059 P := True_Parent (P);
8060 end loop;
8061
8062 return Empty;
8063 end Enclosing_Package_Body;
8064
8065 -------------------------
8066 -- Package_Freeze_Node --
8067 -------------------------
8068
8069 function Package_Freeze_Node (B : Node_Id) return Node_Id is
8070 Id : Entity_Id;
8071
8072 begin
8073 if Nkind (B) = N_Package_Body then
8074 Id := Corresponding_Spec (B);
8075 else pragma Assert (Nkind (B) = N_Package_Body_Stub);
8076 Id := Corresponding_Spec (Proper_Body (Unit (Library_Unit (B))));
8077 end if;
8078
8079 Ensure_Freeze_Node (Id);
8080 return Freeze_Node (Id);
8081 end Package_Freeze_Node;
8082
8083 -- Start of processing of Freeze_Subprogram_Body
8084
8085 begin
8086 -- If the instance and the generic body appear within the same unit, and
8087 -- the instance precedes the generic, the freeze node for the instance
8088 -- must appear after that of the generic. If the generic is nested
8089 -- within another instance I2, then current instance must be frozen
8090 -- after I2. In both cases, the freeze nodes are those of enclosing
8091 -- packages. Otherwise, the freeze node is placed at the end of the
8092 -- current declarative part.
8093
8094 Enc_G := Enclosing_Package_Body (Gen_Body);
8095 Enc_I := Enclosing_Package_Body (Inst_Node);
8096 Ensure_Freeze_Node (Pack_Id);
8097 F_Node := Freeze_Node (Pack_Id);
8098
8099 if Is_Generic_Instance (Par)
8100 and then Present (Freeze_Node (Par))
8101 and then In_Same_Declarative_Part (Freeze_Node (Par), Inst_Node)
8102 then
8103 -- The parent was a premature instantiation. Insert freeze node at
8104 -- the end the current declarative part.
8105
8106 if ABE_Is_Certain (Get_Package_Instantiation_Node (Par)) then
8107 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8108
8109 -- Handle the following case:
8110 --
8111 -- package Parent_Inst is new ...
8112 -- Parent_Inst []
8113 --
8114 -- procedure P ... -- this body freezes Parent_Inst
8115 --
8116 -- package Inst is new ...
8117 --
8118 -- In this particular scenario, the freeze node for Inst must be
8119 -- inserted in the same manner as that of Parent_Inst - before the
8120 -- next source body or at the end of the declarative list (body not
8121 -- available). If body P did not exist and Parent_Inst was frozen
8122 -- after Inst, either by a body following Inst or at the end of the
8123 -- declarative region, the freeze node for Inst must be inserted
8124 -- after that of Parent_Inst. This relation is established by
8125 -- comparing the Slocs of Parent_Inst freeze node and Inst.
8126
8127 elsif List_Containing (Get_Package_Instantiation_Node (Par)) =
8128 List_Containing (Inst_Node)
8129 and then Sloc (Freeze_Node (Par)) < Sloc (Inst_Node)
8130 then
8131 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8132
8133 else
8134 Insert_After (Freeze_Node (Par), F_Node);
8135 end if;
8136
8137 -- The body enclosing the instance should be frozen after the body that
8138 -- includes the generic, because the body of the instance may make
8139 -- references to entities therein. If the two are not in the same
8140 -- declarative part, or if the one enclosing the instance is frozen
8141 -- already, freeze the instance at the end of the current declarative
8142 -- part.
8143
8144 elsif Is_Generic_Instance (Par)
8145 and then Present (Freeze_Node (Par))
8146 and then Present (Enc_I)
8147 then
8148 if In_Same_Declarative_Part (Freeze_Node (Par), Enc_I)
8149 or else
8150 (Nkind (Enc_I) = N_Package_Body
8151 and then
8152 In_Same_Declarative_Part (Freeze_Node (Par), Parent (Enc_I)))
8153 then
8154 -- The enclosing package may contain several instances. Rather
8155 -- than computing the earliest point at which to insert its freeze
8156 -- node, we place it at the end of the declarative part of the
8157 -- parent of the generic.
8158
8159 Insert_Freeze_Node_For_Instance
8160 (Freeze_Node (Par), Package_Freeze_Node (Enc_I));
8161 end if;
8162
8163 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8164
8165 elsif Present (Enc_G)
8166 and then Present (Enc_I)
8167 and then Enc_G /= Enc_I
8168 and then Earlier (Inst_Node, Gen_Body)
8169 then
8170 if Nkind (Enc_G) = N_Package_Body then
8171 E_G_Id :=
8172 Corresponding_Spec (Enc_G);
8173 else pragma Assert (Nkind (Enc_G) = N_Package_Body_Stub);
8174 E_G_Id :=
8175 Corresponding_Spec (Proper_Body (Unit (Library_Unit (Enc_G))));
8176 end if;
8177
8178 -- Freeze package that encloses instance, and place node after the
8179 -- package that encloses generic. If enclosing package is already
8180 -- frozen we have to assume it is at the proper place. This may be a
8181 -- potential ABE that requires dynamic checking. Do not add a freeze
8182 -- node if the package that encloses the generic is inside the body
8183 -- that encloses the instance, because the freeze node would be in
8184 -- the wrong scope. Additional contortions needed if the bodies are
8185 -- within a subunit.
8186
8187 declare
8188 Enclosing_Body : Node_Id;
8189
8190 begin
8191 if Nkind (Enc_I) = N_Package_Body_Stub then
8192 Enclosing_Body := Proper_Body (Unit (Library_Unit (Enc_I)));
8193 else
8194 Enclosing_Body := Enc_I;
8195 end if;
8196
8197 if Parent (List_Containing (Enc_G)) /= Enclosing_Body then
8198 Insert_Freeze_Node_For_Instance
8199 (Enc_G, Package_Freeze_Node (Enc_I));
8200 end if;
8201 end;
8202
8203 -- Freeze enclosing subunit before instance
8204
8205 Ensure_Freeze_Node (E_G_Id);
8206
8207 if not Is_List_Member (Freeze_Node (E_G_Id)) then
8208 Insert_After (Enc_G, Freeze_Node (E_G_Id));
8209 end if;
8210
8211 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8212
8213 else
8214 -- If none of the above, insert freeze node at the end of the current
8215 -- declarative part.
8216
8217 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8218 end if;
8219 end Freeze_Subprogram_Body;
8220
8221 ----------------
8222 -- Get_Gen_Id --
8223 ----------------
8224
8225 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id is
8226 begin
8227 return Generic_Renamings.Table (E).Gen_Id;
8228 end Get_Gen_Id;
8229
8230 ---------------------
8231 -- Get_Instance_Of --
8232 ---------------------
8233
8234 function Get_Instance_Of (A : Entity_Id) return Entity_Id is
8235 Res : constant Assoc_Ptr := Generic_Renamings_HTable.Get (A);
8236
8237 begin
8238 if Res /= Assoc_Null then
8239 return Generic_Renamings.Table (Res).Act_Id;
8240
8241 else
8242 -- On exit, entity is not instantiated: not a generic parameter, or
8243 -- else parameter of an inner generic unit.
8244
8245 return A;
8246 end if;
8247 end Get_Instance_Of;
8248
8249 ------------------------------------
8250 -- Get_Package_Instantiation_Node --
8251 ------------------------------------
8252
8253 function Get_Package_Instantiation_Node (A : Entity_Id) return Node_Id is
8254 Decl : Node_Id := Unit_Declaration_Node (A);
8255 Inst : Node_Id;
8256
8257 begin
8258 -- If the Package_Instantiation attribute has been set on the package
8259 -- entity, then use it directly when it (or its Original_Node) refers
8260 -- to an N_Package_Instantiation node. In principle it should be
8261 -- possible to have this field set in all cases, which should be
8262 -- investigated, and would allow this function to be significantly
8263 -- simplified. ???
8264
8265 Inst := Package_Instantiation (A);
8266
8267 if Present (Inst) then
8268 if Nkind (Inst) = N_Package_Instantiation then
8269 return Inst;
8270
8271 elsif Nkind (Original_Node (Inst)) = N_Package_Instantiation then
8272 return Original_Node (Inst);
8273 end if;
8274 end if;
8275
8276 -- If the instantiation is a compilation unit that does not need body
8277 -- then the instantiation node has been rewritten as a package
8278 -- declaration for the instance, and we return the original node.
8279
8280 -- If it is a compilation unit and the instance node has not been
8281 -- rewritten, then it is still the unit of the compilation. Finally, if
8282 -- a body is present, this is a parent of the main unit whose body has
8283 -- been compiled for inlining purposes, and the instantiation node has
8284 -- been rewritten with the instance body.
8285
8286 -- Otherwise the instantiation node appears after the declaration. If
8287 -- the entity is a formal package, the declaration may have been
8288 -- rewritten as a generic declaration (in the case of a formal with box)
8289 -- or left as a formal package declaration if it has actuals, and is
8290 -- found with a forward search.
8291
8292 if Nkind (Parent (Decl)) = N_Compilation_Unit then
8293 if Nkind (Decl) = N_Package_Declaration
8294 and then Present (Corresponding_Body (Decl))
8295 then
8296 Decl := Unit_Declaration_Node (Corresponding_Body (Decl));
8297 end if;
8298
8299 if Nkind (Original_Node (Decl)) = N_Package_Instantiation then
8300 return Original_Node (Decl);
8301 else
8302 return Unit (Parent (Decl));
8303 end if;
8304
8305 elsif Nkind (Decl) = N_Package_Declaration
8306 and then Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration
8307 then
8308 return Original_Node (Decl);
8309
8310 else
8311 Inst := Next (Decl);
8312 while not Nkind_In (Inst, N_Package_Instantiation,
8313 N_Formal_Package_Declaration)
8314 loop
8315 Next (Inst);
8316 end loop;
8317
8318 return Inst;
8319 end if;
8320 end Get_Package_Instantiation_Node;
8321
8322 ------------------------
8323 -- Has_Been_Exchanged --
8324 ------------------------
8325
8326 function Has_Been_Exchanged (E : Entity_Id) return Boolean is
8327 Next : Elmt_Id;
8328
8329 begin
8330 Next := First_Elmt (Exchanged_Views);
8331 while Present (Next) loop
8332 if Full_View (Node (Next)) = E then
8333 return True;
8334 end if;
8335
8336 Next_Elmt (Next);
8337 end loop;
8338
8339 return False;
8340 end Has_Been_Exchanged;
8341
8342 ----------
8343 -- Hash --
8344 ----------
8345
8346 function Hash (F : Entity_Id) return HTable_Range is
8347 begin
8348 return HTable_Range (F mod HTable_Size);
8349 end Hash;
8350
8351 ------------------------
8352 -- Hide_Current_Scope --
8353 ------------------------
8354
8355 procedure Hide_Current_Scope is
8356 C : constant Entity_Id := Current_Scope;
8357 E : Entity_Id;
8358
8359 begin
8360 Set_Is_Hidden_Open_Scope (C);
8361
8362 E := First_Entity (C);
8363 while Present (E) loop
8364 if Is_Immediately_Visible (E) then
8365 Set_Is_Immediately_Visible (E, False);
8366 Append_Elmt (E, Hidden_Entities);
8367 end if;
8368
8369 Next_Entity (E);
8370 end loop;
8371
8372 -- Make the scope name invisible as well. This is necessary, but might
8373 -- conflict with calls to Rtsfind later on, in case the scope is a
8374 -- predefined one. There is no clean solution to this problem, so for
8375 -- now we depend on the user not redefining Standard itself in one of
8376 -- the parent units.
8377
8378 if Is_Immediately_Visible (C) and then C /= Standard_Standard then
8379 Set_Is_Immediately_Visible (C, False);
8380 Append_Elmt (C, Hidden_Entities);
8381 end if;
8382
8383 end Hide_Current_Scope;
8384
8385 --------------
8386 -- Init_Env --
8387 --------------
8388
8389 procedure Init_Env is
8390 Saved : Instance_Env;
8391
8392 begin
8393 Saved.Instantiated_Parent := Current_Instantiated_Parent;
8394 Saved.Exchanged_Views := Exchanged_Views;
8395 Saved.Hidden_Entities := Hidden_Entities;
8396 Saved.Current_Sem_Unit := Current_Sem_Unit;
8397 Saved.Parent_Unit_Visible := Parent_Unit_Visible;
8398 Saved.Instance_Parent_Unit := Instance_Parent_Unit;
8399
8400 -- Save configuration switches. These may be reset if the unit is a
8401 -- predefined unit, and the current mode is not Ada 2005.
8402
8403 Save_Opt_Config_Switches (Saved.Switches);
8404
8405 Instance_Envs.Append (Saved);
8406
8407 Exchanged_Views := New_Elmt_List;
8408 Hidden_Entities := New_Elmt_List;
8409
8410 -- Make dummy entry for Instantiated parent. If generic unit is legal,
8411 -- this is set properly in Set_Instance_Env.
8412
8413 Current_Instantiated_Parent :=
8414 (Current_Scope, Current_Scope, Assoc_Null);
8415 end Init_Env;
8416
8417 ------------------------------
8418 -- In_Same_Declarative_Part --
8419 ------------------------------
8420
8421 function In_Same_Declarative_Part
8422 (F_Node : Node_Id;
8423 Inst : Node_Id) return Boolean
8424 is
8425 Decls : constant Node_Id := Parent (F_Node);
8426 Nod : Node_Id;
8427
8428 begin
8429 Nod := Parent (Inst);
8430 while Present (Nod) loop
8431 if Nod = Decls then
8432 return True;
8433
8434 elsif Nkind_In (Nod, N_Subprogram_Body,
8435 N_Package_Body,
8436 N_Package_Declaration,
8437 N_Task_Body,
8438 N_Protected_Body,
8439 N_Block_Statement)
8440 then
8441 return False;
8442
8443 elsif Nkind (Nod) = N_Subunit then
8444 Nod := Corresponding_Stub (Nod);
8445
8446 elsif Nkind (Nod) = N_Compilation_Unit then
8447 return False;
8448
8449 else
8450 Nod := Parent (Nod);
8451 end if;
8452 end loop;
8453
8454 return False;
8455 end In_Same_Declarative_Part;
8456
8457 ---------------------
8458 -- In_Main_Context --
8459 ---------------------
8460
8461 function In_Main_Context (E : Entity_Id) return Boolean is
8462 Context : List_Id;
8463 Clause : Node_Id;
8464 Nam : Node_Id;
8465
8466 begin
8467 if not Is_Compilation_Unit (E)
8468 or else Ekind (E) /= E_Package
8469 or else In_Private_Part (E)
8470 then
8471 return False;
8472 end if;
8473
8474 Context := Context_Items (Cunit (Main_Unit));
8475
8476 Clause := First (Context);
8477 while Present (Clause) loop
8478 if Nkind (Clause) = N_With_Clause then
8479 Nam := Name (Clause);
8480
8481 -- If the current scope is part of the context of the main unit,
8482 -- analysis of the corresponding with_clause is not complete, and
8483 -- the entity is not set. We use the Chars field directly, which
8484 -- might produce false positives in rare cases, but guarantees
8485 -- that we produce all the instance bodies we will need.
8486
8487 if (Is_Entity_Name (Nam) and then Chars (Nam) = Chars (E))
8488 or else (Nkind (Nam) = N_Selected_Component
8489 and then Chars (Selector_Name (Nam)) = Chars (E))
8490 then
8491 return True;
8492 end if;
8493 end if;
8494
8495 Next (Clause);
8496 end loop;
8497
8498 return False;
8499 end In_Main_Context;
8500
8501 ---------------------
8502 -- Inherit_Context --
8503 ---------------------
8504
8505 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id) is
8506 Current_Context : List_Id;
8507 Current_Unit : Node_Id;
8508 Item : Node_Id;
8509 New_I : Node_Id;
8510
8511 Clause : Node_Id;
8512 OK : Boolean;
8513 Lib_Unit : Node_Id;
8514
8515 begin
8516 if Nkind (Parent (Gen_Decl)) = N_Compilation_Unit then
8517
8518 -- The inherited context is attached to the enclosing compilation
8519 -- unit. This is either the main unit, or the declaration for the
8520 -- main unit (in case the instantiation appears within the package
8521 -- declaration and the main unit is its body).
8522
8523 Current_Unit := Parent (Inst);
8524 while Present (Current_Unit)
8525 and then Nkind (Current_Unit) /= N_Compilation_Unit
8526 loop
8527 Current_Unit := Parent (Current_Unit);
8528 end loop;
8529
8530 Current_Context := Context_Items (Current_Unit);
8531
8532 Item := First (Context_Items (Parent (Gen_Decl)));
8533 while Present (Item) loop
8534 if Nkind (Item) = N_With_Clause then
8535 Lib_Unit := Library_Unit (Item);
8536
8537 -- Take care to prevent direct cyclic with's
8538
8539 if Lib_Unit /= Current_Unit then
8540
8541 -- Do not add a unit if it is already in the context
8542
8543 Clause := First (Current_Context);
8544 OK := True;
8545 while Present (Clause) loop
8546 if Nkind (Clause) = N_With_Clause and then
8547 Library_Unit (Clause) = Lib_Unit
8548 then
8549 OK := False;
8550 exit;
8551 end if;
8552
8553 Next (Clause);
8554 end loop;
8555
8556 if OK then
8557 New_I := New_Copy (Item);
8558 Set_Implicit_With (New_I, True);
8559 Set_Implicit_With_From_Instantiation (New_I, True);
8560 Append (New_I, Current_Context);
8561 end if;
8562 end if;
8563 end if;
8564
8565 Next (Item);
8566 end loop;
8567 end if;
8568 end Inherit_Context;
8569
8570 ----------------
8571 -- Initialize --
8572 ----------------
8573
8574 procedure Initialize is
8575 begin
8576 Generic_Renamings.Init;
8577 Instance_Envs.Init;
8578 Generic_Flags.Init;
8579 Generic_Renamings_HTable.Reset;
8580 Circularity_Detected := False;
8581 Exchanged_Views := No_Elist;
8582 Hidden_Entities := No_Elist;
8583 end Initialize;
8584
8585 -------------------------------------
8586 -- Insert_Freeze_Node_For_Instance --
8587 -------------------------------------
8588
8589 procedure Insert_Freeze_Node_For_Instance
8590 (N : Node_Id;
8591 F_Node : Node_Id)
8592 is
8593 Decl : Node_Id;
8594 Decls : List_Id;
8595 Inst : Entity_Id;
8596 Par_N : Node_Id;
8597
8598 function Enclosing_Body (N : Node_Id) return Node_Id;
8599 -- Find enclosing package or subprogram body, if any. Freeze node may
8600 -- be placed at end of current declarative list if previous instance
8601 -- and current one have different enclosing bodies.
8602
8603 function Previous_Instance (Gen : Entity_Id) return Entity_Id;
8604 -- Find the local instance, if any, that declares the generic that is
8605 -- being instantiated. If present, the freeze node for this instance
8606 -- must follow the freeze node for the previous instance.
8607
8608 --------------------
8609 -- Enclosing_Body --
8610 --------------------
8611
8612 function Enclosing_Body (N : Node_Id) return Node_Id is
8613 P : Node_Id;
8614
8615 begin
8616 P := Parent (N);
8617 while Present (P)
8618 and then Nkind (Parent (P)) /= N_Compilation_Unit
8619 loop
8620 if Nkind_In (P, N_Package_Body, N_Subprogram_Body) then
8621 if Nkind (Parent (P)) = N_Subunit then
8622 return Corresponding_Stub (Parent (P));
8623 else
8624 return P;
8625 end if;
8626 end if;
8627
8628 P := True_Parent (P);
8629 end loop;
8630
8631 return Empty;
8632 end Enclosing_Body;
8633
8634 -----------------------
8635 -- Previous_Instance --
8636 -----------------------
8637
8638 function Previous_Instance (Gen : Entity_Id) return Entity_Id is
8639 S : Entity_Id;
8640
8641 begin
8642 S := Scope (Gen);
8643 while Present (S) and then S /= Standard_Standard loop
8644 if Is_Generic_Instance (S)
8645 and then In_Same_Source_Unit (S, N)
8646 then
8647 return S;
8648 end if;
8649
8650 S := Scope (S);
8651 end loop;
8652
8653 return Empty;
8654 end Previous_Instance;
8655
8656 -- Start of processing for Insert_Freeze_Node_For_Instance
8657
8658 begin
8659 if not Is_List_Member (F_Node) then
8660 Decl := N;
8661 Decls := List_Containing (N);
8662 Inst := Entity (F_Node);
8663 Par_N := Parent (Decls);
8664
8665 -- When processing a subprogram instantiation, utilize the actual
8666 -- subprogram instantiation rather than its package wrapper as it
8667 -- carries all the context information.
8668
8669 if Is_Wrapper_Package (Inst) then
8670 Inst := Related_Instance (Inst);
8671 end if;
8672
8673 -- If this is a package instance, check whether the generic is
8674 -- declared in a previous instance and the current instance is
8675 -- not within the previous one.
8676
8677 if Present (Generic_Parent (Parent (Inst)))
8678 and then Is_In_Main_Unit (N)
8679 then
8680 declare
8681 Enclosing_N : constant Node_Id := Enclosing_Body (N);
8682 Par_I : constant Entity_Id :=
8683 Previous_Instance
8684 (Generic_Parent (Parent (Inst)));
8685 Scop : Entity_Id;
8686
8687 begin
8688 if Present (Par_I)
8689 and then Earlier (N, Freeze_Node (Par_I))
8690 then
8691 Scop := Scope (Inst);
8692
8693 -- If the current instance is within the one that contains
8694 -- the generic, the freeze node for the current one must
8695 -- appear in the current declarative part. Ditto, if the
8696 -- current instance is within another package instance or
8697 -- within a body that does not enclose the current instance.
8698 -- In these three cases the freeze node of the previous
8699 -- instance is not relevant.
8700
8701 while Present (Scop) and then Scop /= Standard_Standard loop
8702 exit when Scop = Par_I
8703 or else
8704 (Is_Generic_Instance (Scop)
8705 and then Scope_Depth (Scop) > Scope_Depth (Par_I));
8706 Scop := Scope (Scop);
8707 end loop;
8708
8709 -- Previous instance encloses current instance
8710
8711 if Scop = Par_I then
8712 null;
8713
8714 -- If the next node is a source body we must freeze in
8715 -- the current scope as well.
8716
8717 elsif Present (Next (N))
8718 and then Nkind_In (Next (N), N_Subprogram_Body,
8719 N_Package_Body)
8720 and then Comes_From_Source (Next (N))
8721 then
8722 null;
8723
8724 -- Current instance is within an unrelated instance
8725
8726 elsif Is_Generic_Instance (Scop) then
8727 null;
8728
8729 -- Current instance is within an unrelated body
8730
8731 elsif Present (Enclosing_N)
8732 and then Enclosing_N /= Enclosing_Body (Par_I)
8733 then
8734 null;
8735
8736 else
8737 Insert_After (Freeze_Node (Par_I), F_Node);
8738 return;
8739 end if;
8740 end if;
8741 end;
8742 end if;
8743
8744 -- When the instantiation occurs in a package declaration, append the
8745 -- freeze node to the private declarations (if any).
8746
8747 if Nkind (Par_N) = N_Package_Specification
8748 and then Decls = Visible_Declarations (Par_N)
8749 and then Present (Private_Declarations (Par_N))
8750 and then not Is_Empty_List (Private_Declarations (Par_N))
8751 then
8752 Decls := Private_Declarations (Par_N);
8753 Decl := First (Decls);
8754 end if;
8755
8756 -- Determine the proper freeze point of a package instantiation. We
8757 -- adhere to the general rule of a package or subprogram body causing
8758 -- freezing of anything before it in the same declarative region. In
8759 -- this case, the proper freeze point of a package instantiation is
8760 -- before the first source body which follows, or before a stub. This
8761 -- ensures that entities coming from the instance are already frozen
8762 -- and usable in source bodies.
8763
8764 if Nkind (Par_N) /= N_Package_Declaration
8765 and then Ekind (Inst) = E_Package
8766 and then Is_Generic_Instance (Inst)
8767 and then
8768 not In_Same_Source_Unit (Generic_Parent (Parent (Inst)), Inst)
8769 then
8770 while Present (Decl) loop
8771 if (Nkind (Decl) in N_Unit_Body
8772 or else
8773 Nkind (Decl) in N_Body_Stub)
8774 and then Comes_From_Source (Decl)
8775 then
8776 Insert_Before (Decl, F_Node);
8777 return;
8778 end if;
8779
8780 Next (Decl);
8781 end loop;
8782 end if;
8783
8784 -- In a package declaration, or if no previous body, insert at end
8785 -- of list.
8786
8787 Set_Sloc (F_Node, Sloc (Last (Decls)));
8788 Insert_After (Last (Decls), F_Node);
8789 end if;
8790 end Insert_Freeze_Node_For_Instance;
8791
8792 ------------------
8793 -- Install_Body --
8794 ------------------
8795
8796 procedure Install_Body
8797 (Act_Body : Node_Id;
8798 N : Node_Id;
8799 Gen_Body : Node_Id;
8800 Gen_Decl : Node_Id)
8801 is
8802 Act_Id : constant Entity_Id := Corresponding_Spec (Act_Body);
8803 Act_Unit : constant Node_Id := Unit (Cunit (Get_Source_Unit (N)));
8804 Gen_Id : constant Entity_Id := Corresponding_Spec (Gen_Body);
8805 Par : constant Entity_Id := Scope (Gen_Id);
8806 Gen_Unit : constant Node_Id :=
8807 Unit (Cunit (Get_Source_Unit (Gen_Decl)));
8808 Orig_Body : Node_Id := Gen_Body;
8809 F_Node : Node_Id;
8810 Body_Unit : Node_Id;
8811
8812 Must_Delay : Boolean;
8813
8814 function In_Same_Enclosing_Subp return Boolean;
8815 -- Check whether instance and generic body are within same subprogram.
8816
8817 function True_Sloc (N : Node_Id) return Source_Ptr;
8818 -- If the instance is nested inside a generic unit, the Sloc of the
8819 -- instance indicates the place of the original definition, not the
8820 -- point of the current enclosing instance. Pending a better usage of
8821 -- Slocs to indicate instantiation places, we determine the place of
8822 -- origin of a node by finding the maximum sloc of any ancestor node.
8823 -- Why is this not equivalent to Top_Level_Location ???
8824
8825 ----------------------------
8826 -- In_Same_Enclosing_Subp --
8827 ----------------------------
8828
8829 function In_Same_Enclosing_Subp return Boolean is
8830 Scop : Entity_Id;
8831 Subp : Entity_Id;
8832
8833 begin
8834 Scop := Scope (Act_Id);
8835 while Scop /= Standard_Standard
8836 and then not Is_Overloadable (Scop)
8837 loop
8838 Scop := Scope (Scop);
8839 end loop;
8840
8841 if Scop = Standard_Standard then
8842 return False;
8843 else
8844 Subp := Scop;
8845 end if;
8846
8847 Scop := Scope (Gen_Id);
8848 while Scop /= Standard_Standard loop
8849 if Scop = Subp then
8850 return True;
8851 else
8852 Scop := Scope (Scop);
8853 end if;
8854 end loop;
8855
8856 return False;
8857 end In_Same_Enclosing_Subp;
8858
8859 ---------------
8860 -- True_Sloc --
8861 ---------------
8862
8863 function True_Sloc (N : Node_Id) return Source_Ptr is
8864 Res : Source_Ptr;
8865 N1 : Node_Id;
8866
8867 begin
8868 Res := Sloc (N);
8869 N1 := N;
8870 while Present (N1) and then N1 /= Act_Unit loop
8871 if Sloc (N1) > Res then
8872 Res := Sloc (N1);
8873 end if;
8874
8875 N1 := Parent (N1);
8876 end loop;
8877
8878 return Res;
8879 end True_Sloc;
8880
8881 -- Start of processing for Install_Body
8882
8883 begin
8884 -- Handle first the case of an instance with incomplete actual types.
8885 -- The instance body cannot be placed after the declaration because
8886 -- full views have not been seen yet. Any use of the non-limited views
8887 -- in the instance body requires the presence of a regular with_clause
8888 -- in the enclosing unit, and will fail if this with_clause is missing.
8889 -- We place the instance body at the beginning of the enclosing body,
8890 -- which is the unit being compiled. The freeze node for the instance
8891 -- is then placed after the instance body.
8892
8893 if not Is_Empty_Elmt_List (Incomplete_Actuals (Act_Id))
8894 and then Expander_Active
8895 and then Ekind (Scope (Act_Id)) = E_Package
8896 then
8897 declare
8898 Scop : constant Entity_Id := Scope (Act_Id);
8899 Body_Id : constant Node_Id :=
8900 Corresponding_Body (Unit_Declaration_Node (Scop));
8901
8902 begin
8903 Ensure_Freeze_Node (Act_Id);
8904 F_Node := Freeze_Node (Act_Id);
8905 if Present (Body_Id) then
8906 Set_Is_Frozen (Act_Id, False);
8907 Prepend (Act_Body, Declarations (Parent (Body_Id)));
8908 if Is_List_Member (F_Node) then
8909 Remove (F_Node);
8910 end if;
8911
8912 Insert_After (Act_Body, F_Node);
8913 end if;
8914 end;
8915 return;
8916 end if;
8917
8918 -- If the body is a subunit, the freeze point is the corresponding stub
8919 -- in the current compilation, not the subunit itself.
8920
8921 if Nkind (Parent (Gen_Body)) = N_Subunit then
8922 Orig_Body := Corresponding_Stub (Parent (Gen_Body));
8923 else
8924 Orig_Body := Gen_Body;
8925 end if;
8926
8927 Body_Unit := Unit (Cunit (Get_Source_Unit (Orig_Body)));
8928
8929 -- If the instantiation and the generic definition appear in the same
8930 -- package declaration, this is an early instantiation. If they appear
8931 -- in the same declarative part, it is an early instantiation only if
8932 -- the generic body appears textually later, and the generic body is
8933 -- also in the main unit.
8934
8935 -- If instance is nested within a subprogram, and the generic body
8936 -- is not, the instance is delayed because the enclosing body is. If
8937 -- instance and body are within the same scope, or the same subprogram
8938 -- body, indicate explicitly that the instance is delayed.
8939
8940 Must_Delay :=
8941 (Gen_Unit = Act_Unit
8942 and then (Nkind_In (Gen_Unit, N_Package_Declaration,
8943 N_Generic_Package_Declaration)
8944 or else (Gen_Unit = Body_Unit
8945 and then True_Sloc (N) < Sloc (Orig_Body)))
8946 and then Is_In_Main_Unit (Gen_Unit)
8947 and then (Scope (Act_Id) = Scope (Gen_Id)
8948 or else In_Same_Enclosing_Subp));
8949
8950 -- If this is an early instantiation, the freeze node is placed after
8951 -- the generic body. Otherwise, if the generic appears in an instance,
8952 -- we cannot freeze the current instance until the outer one is frozen.
8953 -- This is only relevant if the current instance is nested within some
8954 -- inner scope not itself within the outer instance. If this scope is
8955 -- a package body in the same declarative part as the outer instance,
8956 -- then that body needs to be frozen after the outer instance. Finally,
8957 -- if no delay is needed, we place the freeze node at the end of the
8958 -- current declarative part.
8959
8960 if Expander_Active then
8961 Ensure_Freeze_Node (Act_Id);
8962 F_Node := Freeze_Node (Act_Id);
8963
8964 if Must_Delay then
8965 Insert_After (Orig_Body, F_Node);
8966
8967 elsif Is_Generic_Instance (Par)
8968 and then Present (Freeze_Node (Par))
8969 and then Scope (Act_Id) /= Par
8970 then
8971 -- Freeze instance of inner generic after instance of enclosing
8972 -- generic.
8973
8974 if In_Same_Declarative_Part (Freeze_Node (Par), N) then
8975
8976 -- Handle the following case:
8977
8978 -- package Parent_Inst is new ...
8979 -- Parent_Inst []
8980
8981 -- procedure P ... -- this body freezes Parent_Inst
8982
8983 -- package Inst is new ...
8984
8985 -- In this particular scenario, the freeze node for Inst must
8986 -- be inserted in the same manner as that of Parent_Inst,
8987 -- before the next source body or at the end of the declarative
8988 -- list (body not available). If body P did not exist and
8989 -- Parent_Inst was frozen after Inst, either by a body
8990 -- following Inst or at the end of the declarative region,
8991 -- the freeze node for Inst must be inserted after that of
8992 -- Parent_Inst. This relation is established by comparing
8993 -- the Slocs of Parent_Inst freeze node and Inst.
8994
8995 if List_Containing (Get_Package_Instantiation_Node (Par)) =
8996 List_Containing (N)
8997 and then Sloc (Freeze_Node (Par)) < Sloc (N)
8998 then
8999 Insert_Freeze_Node_For_Instance (N, F_Node);
9000 else
9001 Insert_After (Freeze_Node (Par), F_Node);
9002 end if;
9003
9004 -- Freeze package enclosing instance of inner generic after
9005 -- instance of enclosing generic.
9006
9007 elsif Nkind_In (Parent (N), N_Package_Body, N_Subprogram_Body)
9008 and then In_Same_Declarative_Part (Freeze_Node (Par), Parent (N))
9009 then
9010 declare
9011 Enclosing : Entity_Id;
9012
9013 begin
9014 Enclosing := Corresponding_Spec (Parent (N));
9015
9016 if No (Enclosing) then
9017 Enclosing := Defining_Entity (Parent (N));
9018 end if;
9019
9020 Insert_Freeze_Node_For_Instance (N, F_Node);
9021 Ensure_Freeze_Node (Enclosing);
9022
9023 if not Is_List_Member (Freeze_Node (Enclosing)) then
9024
9025 -- The enclosing context is a subunit, insert the freeze
9026 -- node after the stub.
9027
9028 if Nkind (Parent (Parent (N))) = N_Subunit then
9029 Insert_Freeze_Node_For_Instance
9030 (Corresponding_Stub (Parent (Parent (N))),
9031 Freeze_Node (Enclosing));
9032
9033 -- The enclosing context is a package with a stub body
9034 -- which has already been replaced by the real body.
9035 -- Insert the freeze node after the actual body.
9036
9037 elsif Ekind (Enclosing) = E_Package
9038 and then Present (Body_Entity (Enclosing))
9039 and then Was_Originally_Stub
9040 (Parent (Body_Entity (Enclosing)))
9041 then
9042 Insert_Freeze_Node_For_Instance
9043 (Parent (Body_Entity (Enclosing)),
9044 Freeze_Node (Enclosing));
9045
9046 -- The parent instance has been frozen before the body of
9047 -- the enclosing package, insert the freeze node after
9048 -- the body.
9049
9050 elsif List_Containing (Freeze_Node (Par)) =
9051 List_Containing (Parent (N))
9052 and then Sloc (Freeze_Node (Par)) < Sloc (Parent (N))
9053 then
9054 Insert_Freeze_Node_For_Instance
9055 (Parent (N), Freeze_Node (Enclosing));
9056
9057 else
9058 Insert_After
9059 (Freeze_Node (Par), Freeze_Node (Enclosing));
9060 end if;
9061 end if;
9062 end;
9063
9064 else
9065 Insert_Freeze_Node_For_Instance (N, F_Node);
9066 end if;
9067
9068 else
9069 Insert_Freeze_Node_For_Instance (N, F_Node);
9070 end if;
9071 end if;
9072
9073 Set_Is_Frozen (Act_Id);
9074 Insert_Before (N, Act_Body);
9075 Mark_Rewrite_Insertion (Act_Body);
9076 end Install_Body;
9077
9078 -----------------------------
9079 -- Install_Formal_Packages --
9080 -----------------------------
9081
9082 procedure Install_Formal_Packages (Par : Entity_Id) is
9083 E : Entity_Id;
9084 Gen : Entity_Id;
9085 Gen_E : Entity_Id := Empty;
9086
9087 begin
9088 E := First_Entity (Par);
9089
9090 -- If we are installing an instance parent, locate the formal packages
9091 -- of its generic parent.
9092
9093 if Is_Generic_Instance (Par) then
9094 Gen := Generic_Parent (Package_Specification (Par));
9095 Gen_E := First_Entity (Gen);
9096 end if;
9097
9098 while Present (E) loop
9099 if Ekind (E) = E_Package
9100 and then Nkind (Parent (E)) = N_Package_Renaming_Declaration
9101 then
9102 -- If this is the renaming for the parent instance, done
9103
9104 if Renamed_Object (E) = Par then
9105 exit;
9106
9107 -- The visibility of a formal of an enclosing generic is already
9108 -- correct.
9109
9110 elsif Denotes_Formal_Package (E) then
9111 null;
9112
9113 elsif Present (Associated_Formal_Package (E)) then
9114 Check_Generic_Actuals (Renamed_Object (E), True);
9115 Set_Is_Hidden (E, False);
9116
9117 -- Find formal package in generic unit that corresponds to
9118 -- (instance of) formal package in instance.
9119
9120 while Present (Gen_E) and then Chars (Gen_E) /= Chars (E) loop
9121 Next_Entity (Gen_E);
9122 end loop;
9123
9124 if Present (Gen_E) then
9125 Map_Formal_Package_Entities (Gen_E, E);
9126 end if;
9127 end if;
9128 end if;
9129
9130 Next_Entity (E);
9131
9132 if Present (Gen_E) then
9133 Next_Entity (Gen_E);
9134 end if;
9135 end loop;
9136 end Install_Formal_Packages;
9137
9138 --------------------
9139 -- Install_Parent --
9140 --------------------
9141
9142 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False) is
9143 Ancestors : constant Elist_Id := New_Elmt_List;
9144 S : constant Entity_Id := Current_Scope;
9145 Inst_Par : Entity_Id;
9146 First_Par : Entity_Id;
9147 Inst_Node : Node_Id;
9148 Gen_Par : Entity_Id;
9149 First_Gen : Entity_Id;
9150 Elmt : Elmt_Id;
9151
9152 procedure Install_Noninstance_Specs (Par : Entity_Id);
9153 -- Install the scopes of noninstance parent units ending with Par
9154
9155 procedure Install_Spec (Par : Entity_Id);
9156 -- The child unit is within the declarative part of the parent, so the
9157 -- declarations within the parent are immediately visible.
9158
9159 -------------------------------
9160 -- Install_Noninstance_Specs --
9161 -------------------------------
9162
9163 procedure Install_Noninstance_Specs (Par : Entity_Id) is
9164 begin
9165 if Present (Par)
9166 and then Par /= Standard_Standard
9167 and then not In_Open_Scopes (Par)
9168 then
9169 Install_Noninstance_Specs (Scope (Par));
9170 Install_Spec (Par);
9171 end if;
9172 end Install_Noninstance_Specs;
9173
9174 ------------------
9175 -- Install_Spec --
9176 ------------------
9177
9178 procedure Install_Spec (Par : Entity_Id) is
9179 Spec : constant Node_Id := Package_Specification (Par);
9180
9181 begin
9182 -- If this parent of the child instance is a top-level unit,
9183 -- then record the unit and its visibility for later resetting in
9184 -- Remove_Parent. We exclude units that are generic instances, as we
9185 -- only want to record this information for the ultimate top-level
9186 -- noninstance parent (is that always correct???).
9187
9188 if Scope (Par) = Standard_Standard
9189 and then not Is_Generic_Instance (Par)
9190 then
9191 Parent_Unit_Visible := Is_Immediately_Visible (Par);
9192 Instance_Parent_Unit := Par;
9193 end if;
9194
9195 -- Open the parent scope and make it and its declarations visible.
9196 -- If this point is not within a body, then only the visible
9197 -- declarations should be made visible, and installation of the
9198 -- private declarations is deferred until the appropriate point
9199 -- within analysis of the spec being instantiated (see the handling
9200 -- of parent visibility in Analyze_Package_Specification). This is
9201 -- relaxed in the case where the parent unit is Ada.Tags, to avoid
9202 -- private view problems that occur when compiling instantiations of
9203 -- a generic child of that package (Generic_Dispatching_Constructor).
9204 -- If the instance freezes a tagged type, inlinings of operations
9205 -- from Ada.Tags may need the full view of type Tag. If inlining took
9206 -- proper account of establishing visibility of inlined subprograms'
9207 -- parents then it should be possible to remove this
9208 -- special check. ???
9209
9210 Push_Scope (Par);
9211 Set_Is_Immediately_Visible (Par);
9212 Install_Visible_Declarations (Par);
9213 Set_Use (Visible_Declarations (Spec));
9214
9215 if In_Body or else Is_RTU (Par, Ada_Tags) then
9216 Install_Private_Declarations (Par);
9217 Set_Use (Private_Declarations (Spec));
9218 end if;
9219 end Install_Spec;
9220
9221 -- Start of processing for Install_Parent
9222
9223 begin
9224 -- We need to install the parent instance to compile the instantiation
9225 -- of the child, but the child instance must appear in the current
9226 -- scope. Given that we cannot place the parent above the current scope
9227 -- in the scope stack, we duplicate the current scope and unstack both
9228 -- after the instantiation is complete.
9229
9230 -- If the parent is itself the instantiation of a child unit, we must
9231 -- also stack the instantiation of its parent, and so on. Each such
9232 -- ancestor is the prefix of the name in a prior instantiation.
9233
9234 -- If this is a nested instance, the parent unit itself resolves to
9235 -- a renaming of the parent instance, whose declaration we need.
9236
9237 -- Finally, the parent may be a generic (not an instance) when the
9238 -- child unit appears as a formal package.
9239
9240 Inst_Par := P;
9241
9242 if Present (Renamed_Entity (Inst_Par)) then
9243 Inst_Par := Renamed_Entity (Inst_Par);
9244 end if;
9245
9246 First_Par := Inst_Par;
9247
9248 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
9249
9250 First_Gen := Gen_Par;
9251
9252 while Present (Gen_Par) and then Is_Child_Unit (Gen_Par) loop
9253
9254 -- Load grandparent instance as well
9255
9256 Inst_Node := Get_Package_Instantiation_Node (Inst_Par);
9257
9258 if Nkind (Name (Inst_Node)) = N_Expanded_Name then
9259 Inst_Par := Entity (Prefix (Name (Inst_Node)));
9260
9261 if Present (Renamed_Entity (Inst_Par)) then
9262 Inst_Par := Renamed_Entity (Inst_Par);
9263 end if;
9264
9265 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
9266
9267 if Present (Gen_Par) then
9268 Prepend_Elmt (Inst_Par, Ancestors);
9269
9270 else
9271 -- Parent is not the name of an instantiation
9272
9273 Install_Noninstance_Specs (Inst_Par);
9274 exit;
9275 end if;
9276
9277 else
9278 -- Previous error
9279
9280 exit;
9281 end if;
9282 end loop;
9283
9284 if Present (First_Gen) then
9285 Append_Elmt (First_Par, Ancestors);
9286 else
9287 Install_Noninstance_Specs (First_Par);
9288 end if;
9289
9290 if not Is_Empty_Elmt_List (Ancestors) then
9291 Elmt := First_Elmt (Ancestors);
9292 while Present (Elmt) loop
9293 Install_Spec (Node (Elmt));
9294 Install_Formal_Packages (Node (Elmt));
9295 Next_Elmt (Elmt);
9296 end loop;
9297 end if;
9298
9299 if not In_Body then
9300 Push_Scope (S);
9301 end if;
9302 end Install_Parent;
9303
9304 -------------------------------
9305 -- Install_Hidden_Primitives --
9306 -------------------------------
9307
9308 procedure Install_Hidden_Primitives
9309 (Prims_List : in out Elist_Id;
9310 Gen_T : Entity_Id;
9311 Act_T : Entity_Id)
9312 is
9313 Elmt : Elmt_Id;
9314 List : Elist_Id := No_Elist;
9315 Prim_G_Elmt : Elmt_Id;
9316 Prim_A_Elmt : Elmt_Id;
9317 Prim_G : Node_Id;
9318 Prim_A : Node_Id;
9319
9320 begin
9321 -- No action needed in case of serious errors because we cannot trust
9322 -- in the order of primitives
9323
9324 if Serious_Errors_Detected > 0 then
9325 return;
9326
9327 -- No action possible if we don't have available the list of primitive
9328 -- operations
9329
9330 elsif No (Gen_T)
9331 or else not Is_Record_Type (Gen_T)
9332 or else not Is_Tagged_Type (Gen_T)
9333 or else not Is_Record_Type (Act_T)
9334 or else not Is_Tagged_Type (Act_T)
9335 then
9336 return;
9337
9338 -- There is no need to handle interface types since their primitives
9339 -- cannot be hidden
9340
9341 elsif Is_Interface (Gen_T) then
9342 return;
9343 end if;
9344
9345 Prim_G_Elmt := First_Elmt (Primitive_Operations (Gen_T));
9346
9347 if not Is_Class_Wide_Type (Act_T) then
9348 Prim_A_Elmt := First_Elmt (Primitive_Operations (Act_T));
9349 else
9350 Prim_A_Elmt := First_Elmt (Primitive_Operations (Root_Type (Act_T)));
9351 end if;
9352
9353 loop
9354 -- Skip predefined primitives in the generic formal
9355
9356 while Present (Prim_G_Elmt)
9357 and then Is_Predefined_Dispatching_Operation (Node (Prim_G_Elmt))
9358 loop
9359 Next_Elmt (Prim_G_Elmt);
9360 end loop;
9361
9362 -- Skip predefined primitives in the generic actual
9363
9364 while Present (Prim_A_Elmt)
9365 and then Is_Predefined_Dispatching_Operation (Node (Prim_A_Elmt))
9366 loop
9367 Next_Elmt (Prim_A_Elmt);
9368 end loop;
9369
9370 exit when No (Prim_G_Elmt) or else No (Prim_A_Elmt);
9371
9372 Prim_G := Node (Prim_G_Elmt);
9373 Prim_A := Node (Prim_A_Elmt);
9374
9375 -- There is no need to handle interface primitives because their
9376 -- primitives are not hidden
9377
9378 exit when Present (Interface_Alias (Prim_G));
9379
9380 -- Here we install one hidden primitive
9381
9382 if Chars (Prim_G) /= Chars (Prim_A)
9383 and then Has_Suffix (Prim_A, 'P')
9384 and then Remove_Suffix (Prim_A, 'P') = Chars (Prim_G)
9385 then
9386 Set_Chars (Prim_A, Chars (Prim_G));
9387 Append_New_Elmt (Prim_A, To => List);
9388 end if;
9389
9390 Next_Elmt (Prim_A_Elmt);
9391 Next_Elmt (Prim_G_Elmt);
9392 end loop;
9393
9394 -- Append the elements to the list of temporarily visible primitives
9395 -- avoiding duplicates.
9396
9397 if Present (List) then
9398 if No (Prims_List) then
9399 Prims_List := New_Elmt_List;
9400 end if;
9401
9402 Elmt := First_Elmt (List);
9403 while Present (Elmt) loop
9404 Append_Unique_Elmt (Node (Elmt), Prims_List);
9405 Next_Elmt (Elmt);
9406 end loop;
9407 end if;
9408 end Install_Hidden_Primitives;
9409
9410 -------------------------------
9411 -- Restore_Hidden_Primitives --
9412 -------------------------------
9413
9414 procedure Restore_Hidden_Primitives (Prims_List : in out Elist_Id) is
9415 Prim_Elmt : Elmt_Id;
9416 Prim : Node_Id;
9417
9418 begin
9419 if Prims_List /= No_Elist then
9420 Prim_Elmt := First_Elmt (Prims_List);
9421 while Present (Prim_Elmt) loop
9422 Prim := Node (Prim_Elmt);
9423 Set_Chars (Prim, Add_Suffix (Prim, 'P'));
9424 Next_Elmt (Prim_Elmt);
9425 end loop;
9426
9427 Prims_List := No_Elist;
9428 end if;
9429 end Restore_Hidden_Primitives;
9430
9431 --------------------------------
9432 -- Instantiate_Formal_Package --
9433 --------------------------------
9434
9435 function Instantiate_Formal_Package
9436 (Formal : Node_Id;
9437 Actual : Node_Id;
9438 Analyzed_Formal : Node_Id) return List_Id
9439 is
9440 Loc : constant Source_Ptr := Sloc (Actual);
9441 Actual_Pack : Entity_Id;
9442 Formal_Pack : Entity_Id;
9443 Gen_Parent : Entity_Id;
9444 Decls : List_Id;
9445 Nod : Node_Id;
9446 Parent_Spec : Node_Id;
9447
9448 procedure Find_Matching_Actual
9449 (F : Node_Id;
9450 Act : in out Entity_Id);
9451 -- We need to associate each formal entity in the formal package with
9452 -- the corresponding entity in the actual package. The actual package
9453 -- has been analyzed and possibly expanded, and as a result there is
9454 -- no one-to-one correspondence between the two lists (for example,
9455 -- the actual may include subtypes, itypes, and inherited primitive
9456 -- operations, interspersed among the renaming declarations for the
9457 -- actuals) . We retrieve the corresponding actual by name because each
9458 -- actual has the same name as the formal, and they do appear in the
9459 -- same order.
9460
9461 function Get_Formal_Entity (N : Node_Id) return Entity_Id;
9462 -- Retrieve entity of defining entity of generic formal parameter.
9463 -- Only the declarations of formals need to be considered when
9464 -- linking them to actuals, but the declarative list may include
9465 -- internal entities generated during analysis, and those are ignored.
9466
9467 procedure Match_Formal_Entity
9468 (Formal_Node : Node_Id;
9469 Formal_Ent : Entity_Id;
9470 Actual_Ent : Entity_Id);
9471 -- Associates the formal entity with the actual. In the case where
9472 -- Formal_Ent is a formal package, this procedure iterates through all
9473 -- of its formals and enters associations between the actuals occurring
9474 -- in the formal package's corresponding actual package (given by
9475 -- Actual_Ent) and the formal package's formal parameters. This
9476 -- procedure recurses if any of the parameters is itself a package.
9477
9478 function Is_Instance_Of
9479 (Act_Spec : Entity_Id;
9480 Gen_Anc : Entity_Id) return Boolean;
9481 -- The actual can be an instantiation of a generic within another
9482 -- instance, in which case there is no direct link from it to the
9483 -- original generic ancestor. In that case, we recognize that the
9484 -- ultimate ancestor is the same by examining names and scopes.
9485
9486 procedure Process_Nested_Formal (Formal : Entity_Id);
9487 -- If the current formal is declared with a box, its own formals are
9488 -- visible in the instance, as they were in the generic, and their
9489 -- Hidden flag must be reset. If some of these formals are themselves
9490 -- packages declared with a box, the processing must be recursive.
9491
9492 --------------------------
9493 -- Find_Matching_Actual --
9494 --------------------------
9495
9496 procedure Find_Matching_Actual
9497 (F : Node_Id;
9498 Act : in out Entity_Id)
9499 is
9500 Formal_Ent : Entity_Id;
9501
9502 begin
9503 case Nkind (Original_Node (F)) is
9504 when N_Formal_Object_Declaration |
9505 N_Formal_Type_Declaration =>
9506 Formal_Ent := Defining_Identifier (F);
9507
9508 while Chars (Act) /= Chars (Formal_Ent) loop
9509 Next_Entity (Act);
9510 end loop;
9511
9512 when N_Formal_Subprogram_Declaration |
9513 N_Formal_Package_Declaration |
9514 N_Package_Declaration |
9515 N_Generic_Package_Declaration =>
9516 Formal_Ent := Defining_Entity (F);
9517
9518 while Chars (Act) /= Chars (Formal_Ent) loop
9519 Next_Entity (Act);
9520 end loop;
9521
9522 when others =>
9523 raise Program_Error;
9524 end case;
9525 end Find_Matching_Actual;
9526
9527 -------------------------
9528 -- Match_Formal_Entity --
9529 -------------------------
9530
9531 procedure Match_Formal_Entity
9532 (Formal_Node : Node_Id;
9533 Formal_Ent : Entity_Id;
9534 Actual_Ent : Entity_Id)
9535 is
9536 Act_Pkg : Entity_Id;
9537
9538 begin
9539 Set_Instance_Of (Formal_Ent, Actual_Ent);
9540
9541 if Ekind (Actual_Ent) = E_Package then
9542
9543 -- Record associations for each parameter
9544
9545 Act_Pkg := Actual_Ent;
9546
9547 declare
9548 A_Ent : Entity_Id := First_Entity (Act_Pkg);
9549 F_Ent : Entity_Id;
9550 F_Node : Node_Id;
9551
9552 Gen_Decl : Node_Id;
9553 Formals : List_Id;
9554 Actual : Entity_Id;
9555
9556 begin
9557 -- Retrieve the actual given in the formal package declaration
9558
9559 Actual := Entity (Name (Original_Node (Formal_Node)));
9560
9561 -- The actual in the formal package declaration may be a
9562 -- renamed generic package, in which case we want to retrieve
9563 -- the original generic in order to traverse its formal part.
9564
9565 if Present (Renamed_Entity (Actual)) then
9566 Gen_Decl := Unit_Declaration_Node (Renamed_Entity (Actual));
9567 else
9568 Gen_Decl := Unit_Declaration_Node (Actual);
9569 end if;
9570
9571 Formals := Generic_Formal_Declarations (Gen_Decl);
9572
9573 if Present (Formals) then
9574 F_Node := First_Non_Pragma (Formals);
9575 else
9576 F_Node := Empty;
9577 end if;
9578
9579 while Present (A_Ent)
9580 and then Present (F_Node)
9581 and then A_Ent /= First_Private_Entity (Act_Pkg)
9582 loop
9583 F_Ent := Get_Formal_Entity (F_Node);
9584
9585 if Present (F_Ent) then
9586
9587 -- This is a formal of the original package. Record
9588 -- association and recurse.
9589
9590 Find_Matching_Actual (F_Node, A_Ent);
9591 Match_Formal_Entity (F_Node, F_Ent, A_Ent);
9592 Next_Entity (A_Ent);
9593 end if;
9594
9595 Next_Non_Pragma (F_Node);
9596 end loop;
9597 end;
9598 end if;
9599 end Match_Formal_Entity;
9600
9601 -----------------------
9602 -- Get_Formal_Entity --
9603 -----------------------
9604
9605 function Get_Formal_Entity (N : Node_Id) return Entity_Id is
9606 Kind : constant Node_Kind := Nkind (Original_Node (N));
9607 begin
9608 case Kind is
9609 when N_Formal_Object_Declaration =>
9610 return Defining_Identifier (N);
9611
9612 when N_Formal_Type_Declaration =>
9613 return Defining_Identifier (N);
9614
9615 when N_Formal_Subprogram_Declaration =>
9616 return Defining_Unit_Name (Specification (N));
9617
9618 when N_Formal_Package_Declaration =>
9619 return Defining_Identifier (Original_Node (N));
9620
9621 when N_Generic_Package_Declaration =>
9622 return Defining_Identifier (Original_Node (N));
9623
9624 -- All other declarations are introduced by semantic analysis and
9625 -- have no match in the actual.
9626
9627 when others =>
9628 return Empty;
9629 end case;
9630 end Get_Formal_Entity;
9631
9632 --------------------
9633 -- Is_Instance_Of --
9634 --------------------
9635
9636 function Is_Instance_Of
9637 (Act_Spec : Entity_Id;
9638 Gen_Anc : Entity_Id) return Boolean
9639 is
9640 Gen_Par : constant Entity_Id := Generic_Parent (Act_Spec);
9641
9642 begin
9643 if No (Gen_Par) then
9644 return False;
9645
9646 -- Simplest case: the generic parent of the actual is the formal
9647
9648 elsif Gen_Par = Gen_Anc then
9649 return True;
9650
9651 elsif Chars (Gen_Par) /= Chars (Gen_Anc) then
9652 return False;
9653
9654 -- The actual may be obtained through several instantiations. Its
9655 -- scope must itself be an instance of a generic declared in the
9656 -- same scope as the formal. Any other case is detected above.
9657
9658 elsif not Is_Generic_Instance (Scope (Gen_Par)) then
9659 return False;
9660
9661 else
9662 return Generic_Parent (Parent (Scope (Gen_Par))) = Scope (Gen_Anc);
9663 end if;
9664 end Is_Instance_Of;
9665
9666 ---------------------------
9667 -- Process_Nested_Formal --
9668 ---------------------------
9669
9670 procedure Process_Nested_Formal (Formal : Entity_Id) is
9671 Ent : Entity_Id;
9672
9673 begin
9674 if Present (Associated_Formal_Package (Formal))
9675 and then Box_Present (Parent (Associated_Formal_Package (Formal)))
9676 then
9677 Ent := First_Entity (Formal);
9678 while Present (Ent) loop
9679 Set_Is_Hidden (Ent, False);
9680 Set_Is_Visible_Formal (Ent);
9681 Set_Is_Potentially_Use_Visible
9682 (Ent, Is_Potentially_Use_Visible (Formal));
9683
9684 if Ekind (Ent) = E_Package then
9685 exit when Renamed_Entity (Ent) = Renamed_Entity (Formal);
9686 Process_Nested_Formal (Ent);
9687 end if;
9688
9689 Next_Entity (Ent);
9690 end loop;
9691 end if;
9692 end Process_Nested_Formal;
9693
9694 -- Start of processing for Instantiate_Formal_Package
9695
9696 begin
9697 Analyze (Actual);
9698
9699 if not Is_Entity_Name (Actual)
9700 or else Ekind (Entity (Actual)) /= E_Package
9701 then
9702 Error_Msg_N
9703 ("expect package instance to instantiate formal", Actual);
9704 Abandon_Instantiation (Actual);
9705 raise Program_Error;
9706
9707 else
9708 Actual_Pack := Entity (Actual);
9709 Set_Is_Instantiated (Actual_Pack);
9710
9711 -- The actual may be a renamed package, or an outer generic formal
9712 -- package whose instantiation is converted into a renaming.
9713
9714 if Present (Renamed_Object (Actual_Pack)) then
9715 Actual_Pack := Renamed_Object (Actual_Pack);
9716 end if;
9717
9718 if Nkind (Analyzed_Formal) = N_Formal_Package_Declaration then
9719 Gen_Parent := Get_Instance_Of (Entity (Name (Analyzed_Formal)));
9720 Formal_Pack := Defining_Identifier (Analyzed_Formal);
9721 else
9722 Gen_Parent :=
9723 Generic_Parent (Specification (Analyzed_Formal));
9724 Formal_Pack :=
9725 Defining_Unit_Name (Specification (Analyzed_Formal));
9726 end if;
9727
9728 if Nkind (Parent (Actual_Pack)) = N_Defining_Program_Unit_Name then
9729 Parent_Spec := Package_Specification (Actual_Pack);
9730 else
9731 Parent_Spec := Parent (Actual_Pack);
9732 end if;
9733
9734 if Gen_Parent = Any_Id then
9735 Error_Msg_N
9736 ("previous error in declaration of formal package", Actual);
9737 Abandon_Instantiation (Actual);
9738
9739 elsif
9740 Is_Instance_Of (Parent_Spec, Get_Instance_Of (Gen_Parent))
9741 then
9742 null;
9743
9744 else
9745 Error_Msg_NE
9746 ("actual parameter must be instance of&", Actual, Gen_Parent);
9747 Abandon_Instantiation (Actual);
9748 end if;
9749
9750 Set_Instance_Of (Defining_Identifier (Formal), Actual_Pack);
9751 Map_Formal_Package_Entities (Formal_Pack, Actual_Pack);
9752
9753 Nod :=
9754 Make_Package_Renaming_Declaration (Loc,
9755 Defining_Unit_Name => New_Copy (Defining_Identifier (Formal)),
9756 Name => New_Occurrence_Of (Actual_Pack, Loc));
9757
9758 Set_Associated_Formal_Package
9759 (Defining_Unit_Name (Nod), Defining_Identifier (Formal));
9760 Decls := New_List (Nod);
9761
9762 -- If the formal F has a box, then the generic declarations are
9763 -- visible in the generic G. In an instance of G, the corresponding
9764 -- entities in the actual for F (which are the actuals for the
9765 -- instantiation of the generic that F denotes) must also be made
9766 -- visible for analysis of the current instance. On exit from the
9767 -- current instance, those entities are made private again. If the
9768 -- actual is currently in use, these entities are also use-visible.
9769
9770 -- The loop through the actual entities also steps through the formal
9771 -- entities and enters associations from formals to actuals into the
9772 -- renaming map. This is necessary to properly handle checking of
9773 -- actual parameter associations for later formals that depend on
9774 -- actuals declared in the formal package.
9775
9776 -- In Ada 2005, partial parameterization requires that we make
9777 -- visible the actuals corresponding to formals that were defaulted
9778 -- in the formal package. There formals are identified because they
9779 -- remain formal generics within the formal package, rather than
9780 -- being renamings of the actuals supplied.
9781
9782 declare
9783 Gen_Decl : constant Node_Id :=
9784 Unit_Declaration_Node (Gen_Parent);
9785 Formals : constant List_Id :=
9786 Generic_Formal_Declarations (Gen_Decl);
9787
9788 Actual_Ent : Entity_Id;
9789 Actual_Of_Formal : Node_Id;
9790 Formal_Node : Node_Id;
9791 Formal_Ent : Entity_Id;
9792
9793 begin
9794 if Present (Formals) then
9795 Formal_Node := First_Non_Pragma (Formals);
9796 else
9797 Formal_Node := Empty;
9798 end if;
9799
9800 Actual_Ent := First_Entity (Actual_Pack);
9801 Actual_Of_Formal :=
9802 First (Visible_Declarations (Specification (Analyzed_Formal)));
9803 while Present (Actual_Ent)
9804 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
9805 loop
9806 if Present (Formal_Node) then
9807 Formal_Ent := Get_Formal_Entity (Formal_Node);
9808
9809 if Present (Formal_Ent) then
9810 Find_Matching_Actual (Formal_Node, Actual_Ent);
9811 Match_Formal_Entity (Formal_Node, Formal_Ent, Actual_Ent);
9812
9813 -- We iterate at the same time over the actuals of the
9814 -- local package created for the formal, to determine
9815 -- which one of the formals of the original generic were
9816 -- defaulted in the formal. The corresponding actual
9817 -- entities are visible in the enclosing instance.
9818
9819 if Box_Present (Formal)
9820 or else
9821 (Present (Actual_Of_Formal)
9822 and then
9823 Is_Generic_Formal
9824 (Get_Formal_Entity (Actual_Of_Formal)))
9825 then
9826 Set_Is_Hidden (Actual_Ent, False);
9827 Set_Is_Visible_Formal (Actual_Ent);
9828 Set_Is_Potentially_Use_Visible
9829 (Actual_Ent, In_Use (Actual_Pack));
9830
9831 if Ekind (Actual_Ent) = E_Package then
9832 Process_Nested_Formal (Actual_Ent);
9833 end if;
9834
9835 else
9836 Set_Is_Hidden (Actual_Ent);
9837 Set_Is_Potentially_Use_Visible (Actual_Ent, False);
9838 end if;
9839 end if;
9840
9841 Next_Non_Pragma (Formal_Node);
9842 Next (Actual_Of_Formal);
9843
9844 else
9845 -- No further formals to match, but the generic part may
9846 -- contain inherited operation that are not hidden in the
9847 -- enclosing instance.
9848
9849 Next_Entity (Actual_Ent);
9850 end if;
9851 end loop;
9852
9853 -- Inherited subprograms generated by formal derived types are
9854 -- also visible if the types are.
9855
9856 Actual_Ent := First_Entity (Actual_Pack);
9857 while Present (Actual_Ent)
9858 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
9859 loop
9860 if Is_Overloadable (Actual_Ent)
9861 and then
9862 Nkind (Parent (Actual_Ent)) = N_Subtype_Declaration
9863 and then
9864 not Is_Hidden (Defining_Identifier (Parent (Actual_Ent)))
9865 then
9866 Set_Is_Hidden (Actual_Ent, False);
9867 Set_Is_Potentially_Use_Visible
9868 (Actual_Ent, In_Use (Actual_Pack));
9869 end if;
9870
9871 Next_Entity (Actual_Ent);
9872 end loop;
9873 end;
9874
9875 -- If the formal is not declared with a box, reanalyze it as an
9876 -- abbreviated instantiation, to verify the matching rules of 12.7.
9877 -- The actual checks are performed after the generic associations
9878 -- have been analyzed, to guarantee the same visibility for this
9879 -- instantiation and for the actuals.
9880
9881 -- In Ada 2005, the generic associations for the formal can include
9882 -- defaulted parameters. These are ignored during check. This
9883 -- internal instantiation is removed from the tree after conformance
9884 -- checking, because it contains formal declarations for those
9885 -- defaulted parameters, and those should not reach the back-end.
9886
9887 if not Box_Present (Formal) then
9888 declare
9889 I_Pack : constant Entity_Id :=
9890 Make_Temporary (Sloc (Actual), 'P');
9891
9892 begin
9893 Set_Is_Internal (I_Pack);
9894
9895 Append_To (Decls,
9896 Make_Package_Instantiation (Sloc (Actual),
9897 Defining_Unit_Name => I_Pack,
9898 Name =>
9899 New_Occurrence_Of
9900 (Get_Instance_Of (Gen_Parent), Sloc (Actual)),
9901 Generic_Associations => Generic_Associations (Formal)));
9902 end;
9903 end if;
9904
9905 return Decls;
9906 end if;
9907 end Instantiate_Formal_Package;
9908
9909 -----------------------------------
9910 -- Instantiate_Formal_Subprogram --
9911 -----------------------------------
9912
9913 function Instantiate_Formal_Subprogram
9914 (Formal : Node_Id;
9915 Actual : Node_Id;
9916 Analyzed_Formal : Node_Id) return Node_Id
9917 is
9918 Analyzed_S : constant Entity_Id :=
9919 Defining_Unit_Name (Specification (Analyzed_Formal));
9920 Formal_Sub : constant Entity_Id :=
9921 Defining_Unit_Name (Specification (Formal));
9922
9923 function From_Parent_Scope (Subp : Entity_Id) return Boolean;
9924 -- If the generic is a child unit, the parent has been installed on the
9925 -- scope stack, but a default subprogram cannot resolve to something
9926 -- on the parent because that parent is not really part of the visible
9927 -- context (it is there to resolve explicit local entities). If the
9928 -- default has resolved in this way, we remove the entity from immediate
9929 -- visibility and analyze the node again to emit an error message or
9930 -- find another visible candidate.
9931
9932 procedure Valid_Actual_Subprogram (Act : Node_Id);
9933 -- Perform legality check and raise exception on failure
9934
9935 -----------------------
9936 -- From_Parent_Scope --
9937 -----------------------
9938
9939 function From_Parent_Scope (Subp : Entity_Id) return Boolean is
9940 Gen_Scope : Node_Id;
9941
9942 begin
9943 Gen_Scope := Scope (Analyzed_S);
9944 while Present (Gen_Scope) and then Is_Child_Unit (Gen_Scope) loop
9945 if Scope (Subp) = Scope (Gen_Scope) then
9946 return True;
9947 end if;
9948
9949 Gen_Scope := Scope (Gen_Scope);
9950 end loop;
9951
9952 return False;
9953 end From_Parent_Scope;
9954
9955 -----------------------------
9956 -- Valid_Actual_Subprogram --
9957 -----------------------------
9958
9959 procedure Valid_Actual_Subprogram (Act : Node_Id) is
9960 Act_E : Entity_Id;
9961
9962 begin
9963 if Is_Entity_Name (Act) then
9964 Act_E := Entity (Act);
9965
9966 elsif Nkind (Act) = N_Selected_Component
9967 and then Is_Entity_Name (Selector_Name (Act))
9968 then
9969 Act_E := Entity (Selector_Name (Act));
9970
9971 else
9972 Act_E := Empty;
9973 end if;
9974
9975 if (Present (Act_E) and then Is_Overloadable (Act_E))
9976 or else Nkind_In (Act, N_Attribute_Reference,
9977 N_Indexed_Component,
9978 N_Character_Literal,
9979 N_Explicit_Dereference)
9980 then
9981 return;
9982 end if;
9983
9984 Error_Msg_NE
9985 ("expect subprogram or entry name in instantiation of &",
9986 Instantiation_Node, Formal_Sub);
9987 Abandon_Instantiation (Instantiation_Node);
9988 end Valid_Actual_Subprogram;
9989
9990 -- Local variables
9991
9992 Decl_Node : Node_Id;
9993 Loc : Source_Ptr;
9994 Nam : Node_Id;
9995 New_Spec : Node_Id;
9996 New_Subp : Entity_Id;
9997
9998 -- Start of processing for Instantiate_Formal_Subprogram
9999
10000 begin
10001 New_Spec := New_Copy_Tree (Specification (Formal));
10002
10003 -- The tree copy has created the proper instantiation sloc for the
10004 -- new specification. Use this location for all other constructed
10005 -- declarations.
10006
10007 Loc := Sloc (Defining_Unit_Name (New_Spec));
10008
10009 -- Create new entity for the actual (New_Copy_Tree does not), and
10010 -- indicate that it is an actual.
10011
10012 New_Subp := Make_Defining_Identifier (Loc, Chars (Formal_Sub));
10013 Set_Ekind (New_Subp, Ekind (Analyzed_S));
10014 Set_Is_Generic_Actual_Subprogram (New_Subp);
10015 Set_Defining_Unit_Name (New_Spec, New_Subp);
10016
10017 -- Create new entities for the each of the formals in the specification
10018 -- of the renaming declaration built for the actual.
10019
10020 if Present (Parameter_Specifications (New_Spec)) then
10021 declare
10022 F : Node_Id;
10023 F_Id : Entity_Id;
10024
10025 begin
10026 F := First (Parameter_Specifications (New_Spec));
10027 while Present (F) loop
10028 F_Id := Defining_Identifier (F);
10029
10030 Set_Defining_Identifier (F,
10031 Make_Defining_Identifier (Sloc (F_Id), Chars (F_Id)));
10032 Next (F);
10033 end loop;
10034 end;
10035 end if;
10036
10037 -- Find entity of actual. If the actual is an attribute reference, it
10038 -- cannot be resolved here (its formal is missing) but is handled
10039 -- instead in Attribute_Renaming. If the actual is overloaded, it is
10040 -- fully resolved subsequently, when the renaming declaration for the
10041 -- formal is analyzed. If it is an explicit dereference, resolve the
10042 -- prefix but not the actual itself, to prevent interpretation as call.
10043
10044 if Present (Actual) then
10045 Loc := Sloc (Actual);
10046 Set_Sloc (New_Spec, Loc);
10047
10048 if Nkind (Actual) = N_Operator_Symbol then
10049 Find_Direct_Name (Actual);
10050
10051 elsif Nkind (Actual) = N_Explicit_Dereference then
10052 Analyze (Prefix (Actual));
10053
10054 elsif Nkind (Actual) /= N_Attribute_Reference then
10055 Analyze (Actual);
10056 end if;
10057
10058 Valid_Actual_Subprogram (Actual);
10059 Nam := Actual;
10060
10061 elsif Present (Default_Name (Formal)) then
10062 if not Nkind_In (Default_Name (Formal), N_Attribute_Reference,
10063 N_Selected_Component,
10064 N_Indexed_Component,
10065 N_Character_Literal)
10066 and then Present (Entity (Default_Name (Formal)))
10067 then
10068 Nam := New_Occurrence_Of (Entity (Default_Name (Formal)), Loc);
10069 else
10070 Nam := New_Copy (Default_Name (Formal));
10071 Set_Sloc (Nam, Loc);
10072 end if;
10073
10074 elsif Box_Present (Formal) then
10075
10076 -- Actual is resolved at the point of instantiation. Create an
10077 -- identifier or operator with the same name as the formal.
10078
10079 if Nkind (Formal_Sub) = N_Defining_Operator_Symbol then
10080 Nam :=
10081 Make_Operator_Symbol (Loc,
10082 Chars => Chars (Formal_Sub),
10083 Strval => No_String);
10084 else
10085 Nam := Make_Identifier (Loc, Chars (Formal_Sub));
10086 end if;
10087
10088 elsif Nkind (Specification (Formal)) = N_Procedure_Specification
10089 and then Null_Present (Specification (Formal))
10090 then
10091 -- Generate null body for procedure, for use in the instance
10092
10093 Decl_Node :=
10094 Make_Subprogram_Body (Loc,
10095 Specification => New_Spec,
10096 Declarations => New_List,
10097 Handled_Statement_Sequence =>
10098 Make_Handled_Sequence_Of_Statements (Loc,
10099 Statements => New_List (Make_Null_Statement (Loc))));
10100
10101 Set_Is_Intrinsic_Subprogram (Defining_Unit_Name (New_Spec));
10102 return Decl_Node;
10103
10104 else
10105 Error_Msg_Sloc := Sloc (Scope (Analyzed_S));
10106 Error_Msg_NE
10107 ("missing actual&", Instantiation_Node, Formal_Sub);
10108 Error_Msg_NE
10109 ("\in instantiation of & declared#",
10110 Instantiation_Node, Scope (Analyzed_S));
10111 Abandon_Instantiation (Instantiation_Node);
10112 end if;
10113
10114 Decl_Node :=
10115 Make_Subprogram_Renaming_Declaration (Loc,
10116 Specification => New_Spec,
10117 Name => Nam);
10118
10119 -- If we do not have an actual and the formal specified <> then set to
10120 -- get proper default.
10121
10122 if No (Actual) and then Box_Present (Formal) then
10123 Set_From_Default (Decl_Node);
10124 end if;
10125
10126 -- Gather possible interpretations for the actual before analyzing the
10127 -- instance. If overloaded, it will be resolved when analyzing the
10128 -- renaming declaration.
10129
10130 if Box_Present (Formal) and then No (Actual) then
10131 Analyze (Nam);
10132
10133 if Is_Child_Unit (Scope (Analyzed_S))
10134 and then Present (Entity (Nam))
10135 then
10136 if not Is_Overloaded (Nam) then
10137 if From_Parent_Scope (Entity (Nam)) then
10138 Set_Is_Immediately_Visible (Entity (Nam), False);
10139 Set_Entity (Nam, Empty);
10140 Set_Etype (Nam, Empty);
10141
10142 Analyze (Nam);
10143 Set_Is_Immediately_Visible (Entity (Nam));
10144 end if;
10145
10146 else
10147 declare
10148 I : Interp_Index;
10149 It : Interp;
10150
10151 begin
10152 Get_First_Interp (Nam, I, It);
10153 while Present (It.Nam) loop
10154 if From_Parent_Scope (It.Nam) then
10155 Remove_Interp (I);
10156 end if;
10157
10158 Get_Next_Interp (I, It);
10159 end loop;
10160 end;
10161 end if;
10162 end if;
10163 end if;
10164
10165 -- The generic instantiation freezes the actual. This can only be done
10166 -- once the actual is resolved, in the analysis of the renaming
10167 -- declaration. To make the formal subprogram entity available, we set
10168 -- Corresponding_Formal_Spec to point to the formal subprogram entity.
10169 -- This is also needed in Analyze_Subprogram_Renaming for the processing
10170 -- of formal abstract subprograms.
10171
10172 Set_Corresponding_Formal_Spec (Decl_Node, Analyzed_S);
10173
10174 -- We cannot analyze the renaming declaration, and thus find the actual,
10175 -- until all the actuals are assembled in the instance. For subsequent
10176 -- checks of other actuals, indicate the node that will hold the
10177 -- instance of this formal.
10178
10179 Set_Instance_Of (Analyzed_S, Nam);
10180
10181 if Nkind (Actual) = N_Selected_Component
10182 and then Is_Task_Type (Etype (Prefix (Actual)))
10183 and then not Is_Frozen (Etype (Prefix (Actual)))
10184 then
10185 -- The renaming declaration will create a body, which must appear
10186 -- outside of the instantiation, We move the renaming declaration
10187 -- out of the instance, and create an additional renaming inside,
10188 -- to prevent freezing anomalies.
10189
10190 declare
10191 Anon_Id : constant Entity_Id := Make_Temporary (Loc, 'E');
10192
10193 begin
10194 Set_Defining_Unit_Name (New_Spec, Anon_Id);
10195 Insert_Before (Instantiation_Node, Decl_Node);
10196 Analyze (Decl_Node);
10197
10198 -- Now create renaming within the instance
10199
10200 Decl_Node :=
10201 Make_Subprogram_Renaming_Declaration (Loc,
10202 Specification => New_Copy_Tree (New_Spec),
10203 Name => New_Occurrence_Of (Anon_Id, Loc));
10204
10205 Set_Defining_Unit_Name (Specification (Decl_Node),
10206 Make_Defining_Identifier (Loc, Chars (Formal_Sub)));
10207 end;
10208 end if;
10209
10210 return Decl_Node;
10211 end Instantiate_Formal_Subprogram;
10212
10213 ------------------------
10214 -- Instantiate_Object --
10215 ------------------------
10216
10217 function Instantiate_Object
10218 (Formal : Node_Id;
10219 Actual : Node_Id;
10220 Analyzed_Formal : Node_Id) return List_Id
10221 is
10222 Gen_Obj : constant Entity_Id := Defining_Identifier (Formal);
10223 A_Gen_Obj : constant Entity_Id :=
10224 Defining_Identifier (Analyzed_Formal);
10225 Acc_Def : Node_Id := Empty;
10226 Act_Assoc : constant Node_Id := Parent (Actual);
10227 Actual_Decl : Node_Id := Empty;
10228 Decl_Node : Node_Id;
10229 Def : Node_Id;
10230 Ftyp : Entity_Id;
10231 List : constant List_Id := New_List;
10232 Loc : constant Source_Ptr := Sloc (Actual);
10233 Orig_Ftyp : constant Entity_Id := Etype (A_Gen_Obj);
10234 Subt_Decl : Node_Id := Empty;
10235 Subt_Mark : Node_Id := Empty;
10236
10237 function Copy_Access_Def return Node_Id;
10238 -- If formal is an anonymous access, copy access definition of formal
10239 -- for generated object declaration.
10240
10241 ---------------------
10242 -- Copy_Access_Def --
10243 ---------------------
10244
10245 function Copy_Access_Def return Node_Id is
10246 begin
10247 Def := New_Copy_Tree (Acc_Def);
10248
10249 -- In addition, if formal is an access to subprogram we need to
10250 -- generate new formals for the signature of the default, so that
10251 -- the tree is properly formatted for ASIS use.
10252
10253 if Present (Access_To_Subprogram_Definition (Acc_Def)) then
10254 declare
10255 Par_Spec : Node_Id;
10256 begin
10257 Par_Spec :=
10258 First (Parameter_Specifications
10259 (Access_To_Subprogram_Definition (Def)));
10260 while Present (Par_Spec) loop
10261 Set_Defining_Identifier (Par_Spec,
10262 Make_Defining_Identifier (Sloc (Acc_Def),
10263 Chars => Chars (Defining_Identifier (Par_Spec))));
10264 Next (Par_Spec);
10265 end loop;
10266 end;
10267 end if;
10268
10269 return Def;
10270 end Copy_Access_Def;
10271
10272 -- Start of processing for Instantiate_Object
10273
10274 begin
10275 -- Formal may be an anonymous access
10276
10277 if Present (Subtype_Mark (Formal)) then
10278 Subt_Mark := Subtype_Mark (Formal);
10279 else
10280 Check_Access_Definition (Formal);
10281 Acc_Def := Access_Definition (Formal);
10282 end if;
10283
10284 -- Sloc for error message on missing actual
10285
10286 Error_Msg_Sloc := Sloc (Scope (A_Gen_Obj));
10287
10288 if Get_Instance_Of (Gen_Obj) /= Gen_Obj then
10289 Error_Msg_N ("duplicate instantiation of generic parameter", Actual);
10290 end if;
10291
10292 Set_Parent (List, Parent (Actual));
10293
10294 -- OUT present
10295
10296 if Out_Present (Formal) then
10297
10298 -- An IN OUT generic actual must be a name. The instantiation is a
10299 -- renaming declaration. The actual is the name being renamed. We
10300 -- use the actual directly, rather than a copy, because it is not
10301 -- used further in the list of actuals, and because a copy or a use
10302 -- of relocate_node is incorrect if the instance is nested within a
10303 -- generic. In order to simplify ASIS searches, the Generic_Parent
10304 -- field links the declaration to the generic association.
10305
10306 if No (Actual) then
10307 Error_Msg_NE
10308 ("missing actual &",
10309 Instantiation_Node, Gen_Obj);
10310 Error_Msg_NE
10311 ("\in instantiation of & declared#",
10312 Instantiation_Node, Scope (A_Gen_Obj));
10313 Abandon_Instantiation (Instantiation_Node);
10314 end if;
10315
10316 if Present (Subt_Mark) then
10317 Decl_Node :=
10318 Make_Object_Renaming_Declaration (Loc,
10319 Defining_Identifier => New_Copy (Gen_Obj),
10320 Subtype_Mark => New_Copy_Tree (Subt_Mark),
10321 Name => Actual);
10322
10323 else pragma Assert (Present (Acc_Def));
10324 Decl_Node :=
10325 Make_Object_Renaming_Declaration (Loc,
10326 Defining_Identifier => New_Copy (Gen_Obj),
10327 Access_Definition => New_Copy_Tree (Acc_Def),
10328 Name => Actual);
10329 end if;
10330
10331 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
10332
10333 -- The analysis of the actual may produce Insert_Action nodes, so
10334 -- the declaration must have a context in which to attach them.
10335
10336 Append (Decl_Node, List);
10337 Analyze (Actual);
10338
10339 -- Return if the analysis of the actual reported some error
10340
10341 if Etype (Actual) = Any_Type then
10342 return List;
10343 end if;
10344
10345 -- This check is performed here because Analyze_Object_Renaming will
10346 -- not check it when Comes_From_Source is False. Note though that the
10347 -- check for the actual being the name of an object will be performed
10348 -- in Analyze_Object_Renaming.
10349
10350 if Is_Object_Reference (Actual)
10351 and then Is_Dependent_Component_Of_Mutable_Object (Actual)
10352 then
10353 Error_Msg_N
10354 ("illegal discriminant-dependent component for in out parameter",
10355 Actual);
10356 end if;
10357
10358 -- The actual has to be resolved in order to check that it is a
10359 -- variable (due to cases such as F (1), where F returns access to
10360 -- an array, and for overloaded prefixes).
10361
10362 Ftyp := Get_Instance_Of (Etype (A_Gen_Obj));
10363
10364 -- If the type of the formal is not itself a formal, and the current
10365 -- unit is a child unit, the formal type must be declared in a
10366 -- parent, and must be retrieved by visibility.
10367
10368 if Ftyp = Orig_Ftyp
10369 and then Is_Generic_Unit (Scope (Ftyp))
10370 and then Is_Child_Unit (Scope (A_Gen_Obj))
10371 then
10372 declare
10373 Temp : constant Node_Id :=
10374 New_Copy_Tree (Subtype_Mark (Analyzed_Formal));
10375 begin
10376 Set_Entity (Temp, Empty);
10377 Find_Type (Temp);
10378 Ftyp := Entity (Temp);
10379 end;
10380 end if;
10381
10382 if Is_Private_Type (Ftyp)
10383 and then not Is_Private_Type (Etype (Actual))
10384 and then (Base_Type (Full_View (Ftyp)) = Base_Type (Etype (Actual))
10385 or else Base_Type (Etype (Actual)) = Ftyp)
10386 then
10387 -- If the actual has the type of the full view of the formal, or
10388 -- else a non-private subtype of the formal, then the visibility
10389 -- of the formal type has changed. Add to the actuals a subtype
10390 -- declaration that will force the exchange of views in the body
10391 -- of the instance as well.
10392
10393 Subt_Decl :=
10394 Make_Subtype_Declaration (Loc,
10395 Defining_Identifier => Make_Temporary (Loc, 'P'),
10396 Subtype_Indication => New_Occurrence_Of (Ftyp, Loc));
10397
10398 Prepend (Subt_Decl, List);
10399
10400 Prepend_Elmt (Full_View (Ftyp), Exchanged_Views);
10401 Exchange_Declarations (Ftyp);
10402 end if;
10403
10404 Resolve (Actual, Ftyp);
10405
10406 if not Denotes_Variable (Actual) then
10407 Error_Msg_NE ("actual for& must be a variable", Actual, Gen_Obj);
10408
10409 elsif Base_Type (Ftyp) /= Base_Type (Etype (Actual)) then
10410
10411 -- Ada 2005 (AI-423): For a generic formal object of mode in out,
10412 -- the type of the actual shall resolve to a specific anonymous
10413 -- access type.
10414
10415 if Ada_Version < Ada_2005
10416 or else Ekind (Base_Type (Ftyp)) /=
10417 E_Anonymous_Access_Type
10418 or else Ekind (Base_Type (Etype (Actual))) /=
10419 E_Anonymous_Access_Type
10420 then
10421 Error_Msg_NE
10422 ("type of actual does not match type of&", Actual, Gen_Obj);
10423 end if;
10424 end if;
10425
10426 Note_Possible_Modification (Actual, Sure => True);
10427
10428 -- Check for instantiation of atomic/volatile actual for
10429 -- non-atomic/volatile formal (RM C.6 (12)).
10430
10431 if Is_Atomic_Object (Actual) and then not Is_Atomic (Orig_Ftyp) then
10432 Error_Msg_N
10433 ("cannot instantiate non-atomic formal object "
10434 & "with atomic actual", Actual);
10435
10436 elsif Is_Volatile_Object (Actual) and then not Is_Volatile (Orig_Ftyp)
10437 then
10438 Error_Msg_N
10439 ("cannot instantiate non-volatile formal object "
10440 & "with volatile actual", Actual);
10441 end if;
10442
10443 -- Formal in-parameter
10444
10445 else
10446 -- The instantiation of a generic formal in-parameter is constant
10447 -- declaration. The actual is the expression for that declaration.
10448 -- Its type is a full copy of the type of the formal. This may be
10449 -- an access to subprogram, for which we need to generate entities
10450 -- for the formals in the new signature.
10451
10452 if Present (Actual) then
10453 if Present (Subt_Mark) then
10454 Def := New_Copy_Tree (Subt_Mark);
10455 else pragma Assert (Present (Acc_Def));
10456 Def := Copy_Access_Def;
10457 end if;
10458
10459 Decl_Node :=
10460 Make_Object_Declaration (Loc,
10461 Defining_Identifier => New_Copy (Gen_Obj),
10462 Constant_Present => True,
10463 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
10464 Object_Definition => Def,
10465 Expression => Actual);
10466
10467 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
10468
10469 -- A generic formal object of a tagged type is defined to be
10470 -- aliased so the new constant must also be treated as aliased.
10471
10472 if Is_Tagged_Type (Etype (A_Gen_Obj)) then
10473 Set_Aliased_Present (Decl_Node);
10474 end if;
10475
10476 Append (Decl_Node, List);
10477
10478 -- No need to repeat (pre-)analysis of some expression nodes
10479 -- already handled in Preanalyze_Actuals.
10480
10481 if Nkind (Actual) /= N_Allocator then
10482 Analyze (Actual);
10483
10484 -- Return if the analysis of the actual reported some error
10485
10486 if Etype (Actual) = Any_Type then
10487 return List;
10488 end if;
10489 end if;
10490
10491 declare
10492 Formal_Type : constant Entity_Id := Etype (A_Gen_Obj);
10493 Typ : Entity_Id;
10494
10495 begin
10496 Typ := Get_Instance_Of (Formal_Type);
10497
10498 -- If the actual appears in the current or an enclosing scope,
10499 -- use its type directly. This is relevant if it has an actual
10500 -- subtype that is distinct from its nominal one. This cannot
10501 -- be done in general because the type of the actual may
10502 -- depend on other actuals, and only be fully determined when
10503 -- the enclosing instance is analyzed.
10504
10505 if Present (Etype (Actual))
10506 and then Is_Constr_Subt_For_U_Nominal (Etype (Actual))
10507 then
10508 Freeze_Before (Instantiation_Node, Etype (Actual));
10509 else
10510 Freeze_Before (Instantiation_Node, Typ);
10511 end if;
10512
10513 -- If the actual is an aggregate, perform name resolution on
10514 -- its components (the analysis of an aggregate does not do it)
10515 -- to capture local names that may be hidden if the generic is
10516 -- a child unit.
10517
10518 if Nkind (Actual) = N_Aggregate then
10519 Preanalyze_And_Resolve (Actual, Typ);
10520 end if;
10521
10522 if Is_Limited_Type (Typ)
10523 and then not OK_For_Limited_Init (Typ, Actual)
10524 then
10525 Error_Msg_N
10526 ("initialization not allowed for limited types", Actual);
10527 Explain_Limited_Type (Typ, Actual);
10528 end if;
10529 end;
10530
10531 elsif Present (Default_Expression (Formal)) then
10532
10533 -- Use default to construct declaration
10534
10535 if Present (Subt_Mark) then
10536 Def := New_Copy (Subt_Mark);
10537 else pragma Assert (Present (Acc_Def));
10538 Def := Copy_Access_Def;
10539 end if;
10540
10541 Decl_Node :=
10542 Make_Object_Declaration (Sloc (Formal),
10543 Defining_Identifier => New_Copy (Gen_Obj),
10544 Constant_Present => True,
10545 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
10546 Object_Definition => Def,
10547 Expression => New_Copy_Tree
10548 (Default_Expression (Formal)));
10549
10550 Append (Decl_Node, List);
10551 Set_Analyzed (Expression (Decl_Node), False);
10552
10553 else
10554 Error_Msg_NE ("missing actual&", Instantiation_Node, Gen_Obj);
10555 Error_Msg_NE ("\in instantiation of & declared#",
10556 Instantiation_Node, Scope (A_Gen_Obj));
10557
10558 if Is_Scalar_Type (Etype (A_Gen_Obj)) then
10559
10560 -- Create dummy constant declaration so that instance can be
10561 -- analyzed, to minimize cascaded visibility errors.
10562
10563 if Present (Subt_Mark) then
10564 Def := Subt_Mark;
10565 else pragma Assert (Present (Acc_Def));
10566 Def := Acc_Def;
10567 end if;
10568
10569 Decl_Node :=
10570 Make_Object_Declaration (Loc,
10571 Defining_Identifier => New_Copy (Gen_Obj),
10572 Constant_Present => True,
10573 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
10574 Object_Definition => New_Copy (Def),
10575 Expression =>
10576 Make_Attribute_Reference (Sloc (Gen_Obj),
10577 Attribute_Name => Name_First,
10578 Prefix => New_Copy (Def)));
10579
10580 Append (Decl_Node, List);
10581
10582 else
10583 Abandon_Instantiation (Instantiation_Node);
10584 end if;
10585 end if;
10586 end if;
10587
10588 if Nkind (Actual) in N_Has_Entity then
10589 Actual_Decl := Parent (Entity (Actual));
10590 end if;
10591
10592 -- Ada 2005 (AI-423): For a formal object declaration with a null
10593 -- exclusion or an access definition that has a null exclusion: If the
10594 -- actual matching the formal object declaration denotes a generic
10595 -- formal object of another generic unit G, and the instantiation
10596 -- containing the actual occurs within the body of G or within the body
10597 -- of a generic unit declared within the declarative region of G, then
10598 -- the declaration of the formal object of G must have a null exclusion.
10599 -- Otherwise, the subtype of the actual matching the formal object
10600 -- declaration shall exclude null.
10601
10602 if Ada_Version >= Ada_2005
10603 and then Present (Actual_Decl)
10604 and then Nkind_In (Actual_Decl, N_Formal_Object_Declaration,
10605 N_Object_Declaration)
10606 and then Nkind (Analyzed_Formal) = N_Formal_Object_Declaration
10607 and then not Has_Null_Exclusion (Actual_Decl)
10608 and then Has_Null_Exclusion (Analyzed_Formal)
10609 then
10610 Error_Msg_Sloc := Sloc (Analyzed_Formal);
10611 Error_Msg_N
10612 ("actual must exclude null to match generic formal#", Actual);
10613 end if;
10614
10615 -- An effectively volatile object cannot be used as an actual in
10616 -- a generic instance. The following check is only relevant when
10617 -- SPARK_Mode is on as it is not a standard Ada legality rule.
10618
10619 if SPARK_Mode = On
10620 and then Present (Actual)
10621 and then Is_Effectively_Volatile_Object (Actual)
10622 then
10623 Error_Msg_N
10624 ("volatile object cannot act as actual in generic instantiation "
10625 & "(SPARK RM 7.1.3(8))", Actual);
10626 end if;
10627
10628 return List;
10629 end Instantiate_Object;
10630
10631 ------------------------------
10632 -- Instantiate_Package_Body --
10633 ------------------------------
10634
10635 procedure Instantiate_Package_Body
10636 (Body_Info : Pending_Body_Info;
10637 Inlined_Body : Boolean := False;
10638 Body_Optional : Boolean := False)
10639 is
10640 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
10641 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
10642 Loc : constant Source_Ptr := Sloc (Inst_Node);
10643
10644 Gen_Id : constant Node_Id := Name (Inst_Node);
10645 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
10646 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
10647 Act_Spec : constant Node_Id := Specification (Act_Decl);
10648 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Spec);
10649
10650 Act_Body_Name : Node_Id;
10651 Gen_Body : Node_Id;
10652 Gen_Body_Id : Node_Id;
10653 Act_Body : Node_Id;
10654 Act_Body_Id : Entity_Id;
10655
10656 Parent_Installed : Boolean := False;
10657 Save_Style_Check : constant Boolean := Style_Check;
10658
10659 Par_Ent : Entity_Id := Empty;
10660 Par_Vis : Boolean := False;
10661
10662 Vis_Prims_List : Elist_Id := No_Elist;
10663 -- List of primitives made temporarily visible in the instantiation
10664 -- to match the visibility of the formal type
10665
10666 procedure Check_Initialized_Types;
10667 -- In a generic package body, an entity of a generic private type may
10668 -- appear uninitialized. This is suspicious, unless the actual is a
10669 -- fully initialized type.
10670
10671 -----------------------------
10672 -- Check_Initialized_Types --
10673 -----------------------------
10674
10675 procedure Check_Initialized_Types is
10676 Decl : Node_Id;
10677 Formal : Entity_Id;
10678 Actual : Entity_Id;
10679 Uninit_Var : Entity_Id;
10680
10681 begin
10682 Decl := First (Generic_Formal_Declarations (Gen_Decl));
10683 while Present (Decl) loop
10684 Uninit_Var := Empty;
10685
10686 if Nkind (Decl) = N_Private_Extension_Declaration then
10687 Uninit_Var := Uninitialized_Variable (Decl);
10688
10689 elsif Nkind (Decl) = N_Formal_Type_Declaration
10690 and then Nkind (Formal_Type_Definition (Decl)) =
10691 N_Formal_Private_Type_Definition
10692 then
10693 Uninit_Var :=
10694 Uninitialized_Variable (Formal_Type_Definition (Decl));
10695 end if;
10696
10697 if Present (Uninit_Var) then
10698 Formal := Defining_Identifier (Decl);
10699 Actual := First_Entity (Act_Decl_Id);
10700
10701 -- For each formal there is a subtype declaration that renames
10702 -- the actual and has the same name as the formal. Locate the
10703 -- formal for warning message about uninitialized variables
10704 -- in the generic, for which the actual type should be a fully
10705 -- initialized type.
10706
10707 while Present (Actual) loop
10708 exit when Ekind (Actual) = E_Package
10709 and then Present (Renamed_Object (Actual));
10710
10711 if Chars (Actual) = Chars (Formal)
10712 and then not Is_Scalar_Type (Actual)
10713 and then not Is_Fully_Initialized_Type (Actual)
10714 and then Warn_On_No_Value_Assigned
10715 then
10716 Error_Msg_Node_2 := Formal;
10717 Error_Msg_NE
10718 ("generic unit has uninitialized variable& of "
10719 & "formal private type &?v?", Actual, Uninit_Var);
10720 Error_Msg_NE
10721 ("actual type for& should be fully initialized type?v?",
10722 Actual, Formal);
10723 exit;
10724 end if;
10725
10726 Next_Entity (Actual);
10727 end loop;
10728 end if;
10729
10730 Next (Decl);
10731 end loop;
10732 end Check_Initialized_Types;
10733
10734 -- Start of processing for Instantiate_Package_Body
10735
10736 begin
10737 Gen_Body_Id := Corresponding_Body (Gen_Decl);
10738
10739 -- The instance body may already have been processed, as the parent of
10740 -- another instance that is inlined (Load_Parent_Of_Generic).
10741
10742 if Present (Corresponding_Body (Instance_Spec (Inst_Node))) then
10743 return;
10744 end if;
10745
10746 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
10747
10748 -- Re-establish the state of information on which checks are suppressed.
10749 -- This information was set in Body_Info at the point of instantiation,
10750 -- and now we restore it so that the instance is compiled using the
10751 -- check status at the instantiation (RM 11.5(7.2/2), AI95-00224-01).
10752
10753 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
10754 Scope_Suppress := Body_Info.Scope_Suppress;
10755 Opt.Ada_Version := Body_Info.Version;
10756 Opt.Ada_Version_Pragma := Body_Info.Version_Pragma;
10757 Restore_Warnings (Body_Info.Warnings);
10758 Opt.SPARK_Mode := Body_Info.SPARK_Mode;
10759 Opt.SPARK_Mode_Pragma := Body_Info.SPARK_Mode_Pragma;
10760
10761 if No (Gen_Body_Id) then
10762
10763 -- Do not look for parent of generic body if none is required.
10764 -- This may happen when the routine is called as part of the
10765 -- Pending_Instantiations processing, when nested instances
10766 -- may precede the one generated from the main unit.
10767
10768 if not Unit_Requires_Body (Defining_Entity (Gen_Decl))
10769 and then Body_Optional
10770 then
10771 return;
10772 else
10773 Load_Parent_Of_Generic
10774 (Inst_Node, Specification (Gen_Decl), Body_Optional);
10775 Gen_Body_Id := Corresponding_Body (Gen_Decl);
10776 end if;
10777 end if;
10778
10779 -- Establish global variable for sloc adjustment and for error recovery
10780 -- In the case of an instance body for an instantiation with actuals
10781 -- from a limited view, the instance body is placed at the beginning
10782 -- of the enclosing package body: use the body entity as the source
10783 -- location for nodes of the instance body.
10784
10785 if not Is_Empty_Elmt_List (Incomplete_Actuals (Act_Decl_Id)) then
10786 declare
10787 Scop : constant Entity_Id := Scope (Act_Decl_Id);
10788 Body_Id : constant Node_Id :=
10789 Corresponding_Body (Unit_Declaration_Node (Scop));
10790
10791 begin
10792 Instantiation_Node := Body_Id;
10793 end;
10794 else
10795 Instantiation_Node := Inst_Node;
10796 end if;
10797
10798 if Present (Gen_Body_Id) then
10799 Save_Env (Gen_Unit, Act_Decl_Id);
10800 Style_Check := False;
10801 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
10802
10803 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
10804
10805 Create_Instantiation_Source
10806 (Inst_Node, Gen_Body_Id, False, S_Adjustment);
10807
10808 Act_Body :=
10809 Copy_Generic_Node
10810 (Original_Node (Gen_Body), Empty, Instantiating => True);
10811
10812 -- Build new name (possibly qualified) for body declaration
10813
10814 Act_Body_Id := New_Copy (Act_Decl_Id);
10815
10816 -- Some attributes of spec entity are not inherited by body entity
10817
10818 Set_Handler_Records (Act_Body_Id, No_List);
10819
10820 if Nkind (Defining_Unit_Name (Act_Spec)) =
10821 N_Defining_Program_Unit_Name
10822 then
10823 Act_Body_Name :=
10824 Make_Defining_Program_Unit_Name (Loc,
10825 Name => New_Copy_Tree (Name (Defining_Unit_Name (Act_Spec))),
10826 Defining_Identifier => Act_Body_Id);
10827 else
10828 Act_Body_Name := Act_Body_Id;
10829 end if;
10830
10831 Set_Defining_Unit_Name (Act_Body, Act_Body_Name);
10832
10833 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
10834 Check_Generic_Actuals (Act_Decl_Id, False);
10835 Check_Initialized_Types;
10836
10837 -- Install primitives hidden at the point of the instantiation but
10838 -- visible when processing the generic formals
10839
10840 declare
10841 E : Entity_Id;
10842
10843 begin
10844 E := First_Entity (Act_Decl_Id);
10845 while Present (E) loop
10846 if Is_Type (E)
10847 and then Is_Generic_Actual_Type (E)
10848 and then Is_Tagged_Type (E)
10849 then
10850 Install_Hidden_Primitives
10851 (Prims_List => Vis_Prims_List,
10852 Gen_T => Generic_Parent_Type (Parent (E)),
10853 Act_T => E);
10854 end if;
10855
10856 Next_Entity (E);
10857 end loop;
10858 end;
10859
10860 -- If it is a child unit, make the parent instance (which is an
10861 -- instance of the parent of the generic) visible. The parent
10862 -- instance is the prefix of the name of the generic unit.
10863
10864 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
10865 and then Nkind (Gen_Id) = N_Expanded_Name
10866 then
10867 Par_Ent := Entity (Prefix (Gen_Id));
10868 Par_Vis := Is_Immediately_Visible (Par_Ent);
10869 Install_Parent (Par_Ent, In_Body => True);
10870 Parent_Installed := True;
10871
10872 elsif Is_Child_Unit (Gen_Unit) then
10873 Par_Ent := Scope (Gen_Unit);
10874 Par_Vis := Is_Immediately_Visible (Par_Ent);
10875 Install_Parent (Par_Ent, In_Body => True);
10876 Parent_Installed := True;
10877 end if;
10878
10879 -- If the instantiation is a library unit, and this is the main unit,
10880 -- then build the resulting compilation unit nodes for the instance.
10881 -- If this is a compilation unit but it is not the main unit, then it
10882 -- is the body of a unit in the context, that is being compiled
10883 -- because it is encloses some inlined unit or another generic unit
10884 -- being instantiated. In that case, this body is not part of the
10885 -- current compilation, and is not attached to the tree, but its
10886 -- parent must be set for analysis.
10887
10888 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
10889
10890 -- Replace instance node with body of instance, and create new
10891 -- node for corresponding instance declaration.
10892
10893 Build_Instance_Compilation_Unit_Nodes
10894 (Inst_Node, Act_Body, Act_Decl);
10895 Analyze (Inst_Node);
10896
10897 if Parent (Inst_Node) = Cunit (Main_Unit) then
10898
10899 -- If the instance is a child unit itself, then set the scope
10900 -- of the expanded body to be the parent of the instantiation
10901 -- (ensuring that the fully qualified name will be generated
10902 -- for the elaboration subprogram).
10903
10904 if Nkind (Defining_Unit_Name (Act_Spec)) =
10905 N_Defining_Program_Unit_Name
10906 then
10907 Set_Scope (Defining_Entity (Inst_Node), Scope (Act_Decl_Id));
10908 end if;
10909 end if;
10910
10911 -- Case where instantiation is not a library unit
10912
10913 else
10914 -- If this is an early instantiation, i.e. appears textually
10915 -- before the corresponding body and must be elaborated first,
10916 -- indicate that the body instance is to be delayed.
10917
10918 Install_Body (Act_Body, Inst_Node, Gen_Body, Gen_Decl);
10919
10920 -- Now analyze the body. We turn off all checks if this is an
10921 -- internal unit, since there is no reason to have checks on for
10922 -- any predefined run-time library code. All such code is designed
10923 -- to be compiled with checks off.
10924
10925 -- Note that we do NOT apply this criterion to children of GNAT
10926 -- The latter units must suppress checks explicitly if needed.
10927
10928 if Is_Predefined_File_Name
10929 (Unit_File_Name (Get_Source_Unit (Gen_Decl)))
10930 then
10931 Analyze (Act_Body, Suppress => All_Checks);
10932 else
10933 Analyze (Act_Body);
10934 end if;
10935 end if;
10936
10937 Inherit_Context (Gen_Body, Inst_Node);
10938
10939 -- Remove the parent instances if they have been placed on the scope
10940 -- stack to compile the body.
10941
10942 if Parent_Installed then
10943 Remove_Parent (In_Body => True);
10944
10945 -- Restore the previous visibility of the parent
10946
10947 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
10948 end if;
10949
10950 Restore_Hidden_Primitives (Vis_Prims_List);
10951 Restore_Private_Views (Act_Decl_Id);
10952
10953 -- Remove the current unit from visibility if this is an instance
10954 -- that is not elaborated on the fly for inlining purposes.
10955
10956 if not Inlined_Body then
10957 Set_Is_Immediately_Visible (Act_Decl_Id, False);
10958 end if;
10959
10960 Restore_Env;
10961 Style_Check := Save_Style_Check;
10962
10963 -- If we have no body, and the unit requires a body, then complain. This
10964 -- complaint is suppressed if we have detected other errors (since a
10965 -- common reason for missing the body is that it had errors).
10966 -- In CodePeer mode, a warning has been emitted already, no need for
10967 -- further messages.
10968
10969 elsif Unit_Requires_Body (Gen_Unit)
10970 and then not Body_Optional
10971 then
10972 if CodePeer_Mode then
10973 null;
10974
10975 elsif Serious_Errors_Detected = 0 then
10976 Error_Msg_NE
10977 ("cannot find body of generic package &", Inst_Node, Gen_Unit);
10978
10979 -- Don't attempt to perform any cleanup actions if some other error
10980 -- was already detected, since this can cause blowups.
10981
10982 else
10983 return;
10984 end if;
10985
10986 -- Case of package that does not need a body
10987
10988 else
10989 -- If the instantiation of the declaration is a library unit, rewrite
10990 -- the original package instantiation as a package declaration in the
10991 -- compilation unit node.
10992
10993 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
10994 Set_Parent_Spec (Act_Decl, Parent_Spec (Inst_Node));
10995 Rewrite (Inst_Node, Act_Decl);
10996
10997 -- Generate elaboration entity, in case spec has elaboration code.
10998 -- This cannot be done when the instance is analyzed, because it
10999 -- is not known yet whether the body exists.
11000
11001 Set_Elaboration_Entity_Required (Act_Decl_Id, False);
11002 Build_Elaboration_Entity (Parent (Inst_Node), Act_Decl_Id);
11003
11004 -- If the instantiation is not a library unit, then append the
11005 -- declaration to the list of implicitly generated entities, unless
11006 -- it is already a list member which means that it was already
11007 -- processed
11008
11009 elsif not Is_List_Member (Act_Decl) then
11010 Mark_Rewrite_Insertion (Act_Decl);
11011 Insert_Before (Inst_Node, Act_Decl);
11012 end if;
11013 end if;
11014
11015 Expander_Mode_Restore;
11016 end Instantiate_Package_Body;
11017
11018 ---------------------------------
11019 -- Instantiate_Subprogram_Body --
11020 ---------------------------------
11021
11022 procedure Instantiate_Subprogram_Body
11023 (Body_Info : Pending_Body_Info;
11024 Body_Optional : Boolean := False)
11025 is
11026 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
11027 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
11028 Loc : constant Source_Ptr := Sloc (Inst_Node);
11029 Gen_Id : constant Node_Id := Name (Inst_Node);
11030 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
11031 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
11032 Anon_Id : constant Entity_Id :=
11033 Defining_Unit_Name (Specification (Act_Decl));
11034 Pack_Id : constant Entity_Id :=
11035 Defining_Unit_Name (Parent (Act_Decl));
11036
11037 Saved_Style_Check : constant Boolean := Style_Check;
11038 Saved_Warnings : constant Warning_Record := Save_Warnings;
11039
11040 Act_Body : Node_Id;
11041 Gen_Body : Node_Id;
11042 Gen_Body_Id : Node_Id;
11043 Pack_Body : Node_Id;
11044 Par_Ent : Entity_Id := Empty;
11045 Par_Vis : Boolean := False;
11046 Ret_Expr : Node_Id;
11047
11048 Parent_Installed : Boolean := False;
11049
11050 begin
11051 Gen_Body_Id := Corresponding_Body (Gen_Decl);
11052
11053 -- Subprogram body may have been created already because of an inline
11054 -- pragma, or because of multiple elaborations of the enclosing package
11055 -- when several instances of the subprogram appear in the main unit.
11056
11057 if Present (Corresponding_Body (Act_Decl)) then
11058 return;
11059 end if;
11060
11061 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
11062
11063 -- Re-establish the state of information on which checks are suppressed.
11064 -- This information was set in Body_Info at the point of instantiation,
11065 -- and now we restore it so that the instance is compiled using the
11066 -- check status at the instantiation (RM 11.5(7.2/2), AI95-00224-01).
11067
11068 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
11069 Scope_Suppress := Body_Info.Scope_Suppress;
11070 Opt.Ada_Version := Body_Info.Version;
11071 Opt.Ada_Version_Pragma := Body_Info.Version_Pragma;
11072 Restore_Warnings (Body_Info.Warnings);
11073 Opt.SPARK_Mode := Body_Info.SPARK_Mode;
11074 Opt.SPARK_Mode_Pragma := Body_Info.SPARK_Mode_Pragma;
11075
11076 if No (Gen_Body_Id) then
11077
11078 -- For imported generic subprogram, no body to compile, complete
11079 -- the spec entity appropriately.
11080
11081 if Is_Imported (Gen_Unit) then
11082 Set_Is_Imported (Anon_Id);
11083 Set_First_Rep_Item (Anon_Id, First_Rep_Item (Gen_Unit));
11084 Set_Interface_Name (Anon_Id, Interface_Name (Gen_Unit));
11085 Set_Convention (Anon_Id, Convention (Gen_Unit));
11086 Set_Has_Completion (Anon_Id);
11087 return;
11088
11089 -- For other cases, compile the body
11090
11091 else
11092 Load_Parent_Of_Generic
11093 (Inst_Node, Specification (Gen_Decl), Body_Optional);
11094 Gen_Body_Id := Corresponding_Body (Gen_Decl);
11095 end if;
11096 end if;
11097
11098 Instantiation_Node := Inst_Node;
11099
11100 if Present (Gen_Body_Id) then
11101 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
11102
11103 if Nkind (Gen_Body) = N_Subprogram_Body_Stub then
11104
11105 -- Either body is not present, or context is non-expanding, as
11106 -- when compiling a subunit. Mark the instance as completed, and
11107 -- diagnose a missing body when needed.
11108
11109 if Expander_Active
11110 and then Operating_Mode = Generate_Code
11111 then
11112 Error_Msg_N
11113 ("missing proper body for instantiation", Gen_Body);
11114 end if;
11115
11116 Set_Has_Completion (Anon_Id);
11117 return;
11118 end if;
11119
11120 Save_Env (Gen_Unit, Anon_Id);
11121 Style_Check := False;
11122 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
11123 Create_Instantiation_Source
11124 (Inst_Node,
11125 Gen_Body_Id,
11126 False,
11127 S_Adjustment);
11128
11129 Act_Body :=
11130 Copy_Generic_Node
11131 (Original_Node (Gen_Body), Empty, Instantiating => True);
11132
11133 -- Create proper defining name for the body, to correspond to
11134 -- the one in the spec.
11135
11136 Set_Defining_Unit_Name (Specification (Act_Body),
11137 Make_Defining_Identifier
11138 (Sloc (Defining_Entity (Inst_Node)), Chars (Anon_Id)));
11139 Set_Corresponding_Spec (Act_Body, Anon_Id);
11140 Set_Has_Completion (Anon_Id);
11141 Check_Generic_Actuals (Pack_Id, False);
11142
11143 -- Generate a reference to link the visible subprogram instance to
11144 -- the generic body, which for navigation purposes is the only
11145 -- available source for the instance.
11146
11147 Generate_Reference
11148 (Related_Instance (Pack_Id),
11149 Gen_Body_Id, 'b', Set_Ref => False, Force => True);
11150
11151 -- If it is a child unit, make the parent instance (which is an
11152 -- instance of the parent of the generic) visible. The parent
11153 -- instance is the prefix of the name of the generic unit.
11154
11155 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
11156 and then Nkind (Gen_Id) = N_Expanded_Name
11157 then
11158 Par_Ent := Entity (Prefix (Gen_Id));
11159 Par_Vis := Is_Immediately_Visible (Par_Ent);
11160 Install_Parent (Par_Ent, In_Body => True);
11161 Parent_Installed := True;
11162
11163 elsif Is_Child_Unit (Gen_Unit) then
11164 Par_Ent := Scope (Gen_Unit);
11165 Par_Vis := Is_Immediately_Visible (Par_Ent);
11166 Install_Parent (Par_Ent, In_Body => True);
11167 Parent_Installed := True;
11168 end if;
11169
11170 -- Subprogram body is placed in the body of wrapper package,
11171 -- whose spec contains the subprogram declaration as well as
11172 -- the renaming declarations for the generic parameters.
11173
11174 Pack_Body :=
11175 Make_Package_Body (Loc,
11176 Defining_Unit_Name => New_Copy (Pack_Id),
11177 Declarations => New_List (Act_Body));
11178
11179 Set_Corresponding_Spec (Pack_Body, Pack_Id);
11180
11181 -- If the instantiation is a library unit, then build resulting
11182 -- compilation unit nodes for the instance. The declaration of
11183 -- the enclosing package is the grandparent of the subprogram
11184 -- declaration. First replace the instantiation node as the unit
11185 -- of the corresponding compilation.
11186
11187 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
11188 if Parent (Inst_Node) = Cunit (Main_Unit) then
11189 Set_Unit (Parent (Inst_Node), Inst_Node);
11190 Build_Instance_Compilation_Unit_Nodes
11191 (Inst_Node, Pack_Body, Parent (Parent (Act_Decl)));
11192 Analyze (Inst_Node);
11193 else
11194 Set_Parent (Pack_Body, Parent (Inst_Node));
11195 Analyze (Pack_Body);
11196 end if;
11197
11198 else
11199 Insert_Before (Inst_Node, Pack_Body);
11200 Mark_Rewrite_Insertion (Pack_Body);
11201 Analyze (Pack_Body);
11202
11203 if Expander_Active then
11204 Freeze_Subprogram_Body (Inst_Node, Gen_Body, Pack_Id);
11205 end if;
11206 end if;
11207
11208 Inherit_Context (Gen_Body, Inst_Node);
11209
11210 Restore_Private_Views (Pack_Id, False);
11211
11212 if Parent_Installed then
11213 Remove_Parent (In_Body => True);
11214
11215 -- Restore the previous visibility of the parent
11216
11217 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
11218 end if;
11219
11220 Restore_Env;
11221 Style_Check := Saved_Style_Check;
11222 Restore_Warnings (Saved_Warnings);
11223
11224 -- Body not found. Error was emitted already. If there were no previous
11225 -- errors, this may be an instance whose scope is a premature instance.
11226 -- In that case we must insure that the (legal) program does raise
11227 -- program error if executed. We generate a subprogram body for this
11228 -- purpose. See DEC ac30vso.
11229
11230 -- Should not reference proprietary DEC tests in comments ???
11231
11232 elsif Serious_Errors_Detected = 0
11233 and then Nkind (Parent (Inst_Node)) /= N_Compilation_Unit
11234 then
11235 if Body_Optional then
11236 return;
11237
11238 elsif Ekind (Anon_Id) = E_Procedure then
11239 Act_Body :=
11240 Make_Subprogram_Body (Loc,
11241 Specification =>
11242 Make_Procedure_Specification (Loc,
11243 Defining_Unit_Name =>
11244 Make_Defining_Identifier (Loc, Chars (Anon_Id)),
11245 Parameter_Specifications =>
11246 New_Copy_List
11247 (Parameter_Specifications (Parent (Anon_Id)))),
11248
11249 Declarations => Empty_List,
11250 Handled_Statement_Sequence =>
11251 Make_Handled_Sequence_Of_Statements (Loc,
11252 Statements =>
11253 New_List (
11254 Make_Raise_Program_Error (Loc,
11255 Reason =>
11256 PE_Access_Before_Elaboration))));
11257
11258 else
11259 Ret_Expr :=
11260 Make_Raise_Program_Error (Loc,
11261 Reason => PE_Access_Before_Elaboration);
11262
11263 Set_Etype (Ret_Expr, (Etype (Anon_Id)));
11264 Set_Analyzed (Ret_Expr);
11265
11266 Act_Body :=
11267 Make_Subprogram_Body (Loc,
11268 Specification =>
11269 Make_Function_Specification (Loc,
11270 Defining_Unit_Name =>
11271 Make_Defining_Identifier (Loc, Chars (Anon_Id)),
11272 Parameter_Specifications =>
11273 New_Copy_List
11274 (Parameter_Specifications (Parent (Anon_Id))),
11275 Result_Definition =>
11276 New_Occurrence_Of (Etype (Anon_Id), Loc)),
11277
11278 Declarations => Empty_List,
11279 Handled_Statement_Sequence =>
11280 Make_Handled_Sequence_Of_Statements (Loc,
11281 Statements =>
11282 New_List
11283 (Make_Simple_Return_Statement (Loc, Ret_Expr))));
11284 end if;
11285
11286 Pack_Body := Make_Package_Body (Loc,
11287 Defining_Unit_Name => New_Copy (Pack_Id),
11288 Declarations => New_List (Act_Body));
11289
11290 Insert_After (Inst_Node, Pack_Body);
11291 Set_Corresponding_Spec (Pack_Body, Pack_Id);
11292 Analyze (Pack_Body);
11293 end if;
11294
11295 Expander_Mode_Restore;
11296 end Instantiate_Subprogram_Body;
11297
11298 ----------------------
11299 -- Instantiate_Type --
11300 ----------------------
11301
11302 function Instantiate_Type
11303 (Formal : Node_Id;
11304 Actual : Node_Id;
11305 Analyzed_Formal : Node_Id;
11306 Actual_Decls : List_Id) return List_Id
11307 is
11308 Gen_T : constant Entity_Id := Defining_Identifier (Formal);
11309 A_Gen_T : constant Entity_Id :=
11310 Defining_Identifier (Analyzed_Formal);
11311 Ancestor : Entity_Id := Empty;
11312 Def : constant Node_Id := Formal_Type_Definition (Formal);
11313 Act_T : Entity_Id;
11314 Decl_Node : Node_Id;
11315 Decl_Nodes : List_Id;
11316 Loc : Source_Ptr;
11317 Subt : Entity_Id;
11318
11319 procedure Diagnose_Predicated_Actual;
11320 -- There are a number of constructs in which a discrete type with
11321 -- predicates is illegal, e.g. as an index in an array type declaration.
11322 -- If a generic type is used is such a construct in a generic package
11323 -- declaration, it carries the flag No_Predicate_On_Actual. it is part
11324 -- of the generic contract that the actual cannot have predicates.
11325
11326 procedure Validate_Array_Type_Instance;
11327 procedure Validate_Access_Subprogram_Instance;
11328 procedure Validate_Access_Type_Instance;
11329 procedure Validate_Derived_Type_Instance;
11330 procedure Validate_Derived_Interface_Type_Instance;
11331 procedure Validate_Discriminated_Formal_Type;
11332 procedure Validate_Interface_Type_Instance;
11333 procedure Validate_Private_Type_Instance;
11334 procedure Validate_Incomplete_Type_Instance;
11335 -- These procedures perform validation tests for the named case.
11336 -- Validate_Discriminated_Formal_Type is shared by formal private
11337 -- types and Ada 2012 formal incomplete types.
11338
11339 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean;
11340 -- Check that base types are the same and that the subtypes match
11341 -- statically. Used in several of the above.
11342
11343 ---------------------------------
11344 -- Diagnose_Predicated_Actual --
11345 ---------------------------------
11346
11347 procedure Diagnose_Predicated_Actual is
11348 begin
11349 if No_Predicate_On_Actual (A_Gen_T)
11350 and then Has_Predicates (Act_T)
11351 then
11352 Error_Msg_NE
11353 ("actual for& cannot be a type with predicate",
11354 Instantiation_Node, A_Gen_T);
11355
11356 elsif No_Dynamic_Predicate_On_Actual (A_Gen_T)
11357 and then Has_Predicates (Act_T)
11358 and then not Has_Static_Predicate_Aspect (Act_T)
11359 then
11360 Error_Msg_NE
11361 ("actual for& cannot be a type with a dynamic predicate",
11362 Instantiation_Node, A_Gen_T);
11363 end if;
11364 end Diagnose_Predicated_Actual;
11365
11366 --------------------
11367 -- Subtypes_Match --
11368 --------------------
11369
11370 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean is
11371 T : constant Entity_Id := Get_Instance_Of (Gen_T);
11372
11373 begin
11374 -- Some detailed comments would be useful here ???
11375
11376 return ((Base_Type (T) = Act_T
11377 or else Base_Type (T) = Base_Type (Act_T))
11378 and then Subtypes_Statically_Match (T, Act_T))
11379
11380 or else (Is_Class_Wide_Type (Gen_T)
11381 and then Is_Class_Wide_Type (Act_T)
11382 and then Subtypes_Match
11383 (Get_Instance_Of (Root_Type (Gen_T)),
11384 Root_Type (Act_T)))
11385
11386 or else
11387 (Ekind_In (Gen_T, E_Anonymous_Access_Subprogram_Type,
11388 E_Anonymous_Access_Type)
11389 and then Ekind (Act_T) = Ekind (Gen_T)
11390 and then Subtypes_Statically_Match
11391 (Designated_Type (Gen_T), Designated_Type (Act_T)));
11392 end Subtypes_Match;
11393
11394 -----------------------------------------
11395 -- Validate_Access_Subprogram_Instance --
11396 -----------------------------------------
11397
11398 procedure Validate_Access_Subprogram_Instance is
11399 begin
11400 if not Is_Access_Type (Act_T)
11401 or else Ekind (Designated_Type (Act_T)) /= E_Subprogram_Type
11402 then
11403 Error_Msg_NE
11404 ("expect access type in instantiation of &", Actual, Gen_T);
11405 Abandon_Instantiation (Actual);
11406 end if;
11407
11408 -- According to AI05-288, actuals for access_to_subprograms must be
11409 -- subtype conformant with the generic formal. Previous to AI05-288
11410 -- only mode conformance was required.
11411
11412 -- This is a binding interpretation that applies to previous versions
11413 -- of the language, no need to maintain previous weaker checks.
11414
11415 Check_Subtype_Conformant
11416 (Designated_Type (Act_T),
11417 Designated_Type (A_Gen_T),
11418 Actual,
11419 Get_Inst => True);
11420
11421 if Ekind (Base_Type (Act_T)) = E_Access_Protected_Subprogram_Type then
11422 if Ekind (A_Gen_T) = E_Access_Subprogram_Type then
11423 Error_Msg_NE
11424 ("protected access type not allowed for formal &",
11425 Actual, Gen_T);
11426 end if;
11427
11428 elsif Ekind (A_Gen_T) = E_Access_Protected_Subprogram_Type then
11429 Error_Msg_NE
11430 ("expect protected access type for formal &",
11431 Actual, Gen_T);
11432 end if;
11433
11434 -- If the formal has a specified convention (which in most cases
11435 -- will be StdCall) verify that the actual has the same convention.
11436
11437 if Has_Convention_Pragma (A_Gen_T)
11438 and then Convention (A_Gen_T) /= Convention (Act_T)
11439 then
11440 Error_Msg_Name_1 := Get_Convention_Name (Convention (A_Gen_T));
11441 Error_Msg_NE
11442 ("actual for formal & must have convention %", Actual, Gen_T);
11443 end if;
11444 end Validate_Access_Subprogram_Instance;
11445
11446 -----------------------------------
11447 -- Validate_Access_Type_Instance --
11448 -----------------------------------
11449
11450 procedure Validate_Access_Type_Instance is
11451 Desig_Type : constant Entity_Id :=
11452 Find_Actual_Type (Designated_Type (A_Gen_T), A_Gen_T);
11453 Desig_Act : Entity_Id;
11454
11455 begin
11456 if not Is_Access_Type (Act_T) then
11457 Error_Msg_NE
11458 ("expect access type in instantiation of &", Actual, Gen_T);
11459 Abandon_Instantiation (Actual);
11460 end if;
11461
11462 if Is_Access_Constant (A_Gen_T) then
11463 if not Is_Access_Constant (Act_T) then
11464 Error_Msg_N
11465 ("actual type must be access-to-constant type", Actual);
11466 Abandon_Instantiation (Actual);
11467 end if;
11468 else
11469 if Is_Access_Constant (Act_T) then
11470 Error_Msg_N
11471 ("actual type must be access-to-variable type", Actual);
11472 Abandon_Instantiation (Actual);
11473
11474 elsif Ekind (A_Gen_T) = E_General_Access_Type
11475 and then Ekind (Base_Type (Act_T)) /= E_General_Access_Type
11476 then
11477 Error_Msg_N -- CODEFIX
11478 ("actual must be general access type!", Actual);
11479 Error_Msg_NE -- CODEFIX
11480 ("add ALL to }!", Actual, Act_T);
11481 Abandon_Instantiation (Actual);
11482 end if;
11483 end if;
11484
11485 -- The designated subtypes, that is to say the subtypes introduced
11486 -- by an access type declaration (and not by a subtype declaration)
11487 -- must match.
11488
11489 Desig_Act := Designated_Type (Base_Type (Act_T));
11490
11491 -- The designated type may have been introduced through a limited_
11492 -- with clause, in which case retrieve the non-limited view. This
11493 -- applies to incomplete types as well as to class-wide types.
11494
11495 if From_Limited_With (Desig_Act) then
11496 Desig_Act := Available_View (Desig_Act);
11497 end if;
11498
11499 if not Subtypes_Match (Desig_Type, Desig_Act) then
11500 Error_Msg_NE
11501 ("designated type of actual does not match that of formal &",
11502 Actual, Gen_T);
11503
11504 if not Predicates_Match (Desig_Type, Desig_Act) then
11505 Error_Msg_N ("\predicates do not match", Actual);
11506 end if;
11507
11508 Abandon_Instantiation (Actual);
11509
11510 elsif Is_Access_Type (Designated_Type (Act_T))
11511 and then Is_Constrained (Designated_Type (Designated_Type (Act_T)))
11512 /=
11513 Is_Constrained (Designated_Type (Desig_Type))
11514 then
11515 Error_Msg_NE
11516 ("designated type of actual does not match that of formal &",
11517 Actual, Gen_T);
11518
11519 if not Predicates_Match (Desig_Type, Desig_Act) then
11520 Error_Msg_N ("\predicates do not match", Actual);
11521 end if;
11522
11523 Abandon_Instantiation (Actual);
11524 end if;
11525
11526 -- Ada 2005: null-exclusion indicators of the two types must agree
11527
11528 if Can_Never_Be_Null (A_Gen_T) /= Can_Never_Be_Null (Act_T) then
11529 Error_Msg_NE
11530 ("non null exclusion of actual and formal & do not match",
11531 Actual, Gen_T);
11532 end if;
11533 end Validate_Access_Type_Instance;
11534
11535 ----------------------------------
11536 -- Validate_Array_Type_Instance --
11537 ----------------------------------
11538
11539 procedure Validate_Array_Type_Instance is
11540 I1 : Node_Id;
11541 I2 : Node_Id;
11542 T2 : Entity_Id;
11543
11544 function Formal_Dimensions return Int;
11545 -- Count number of dimensions in array type formal
11546
11547 -----------------------
11548 -- Formal_Dimensions --
11549 -----------------------
11550
11551 function Formal_Dimensions return Int is
11552 Num : Int := 0;
11553 Index : Node_Id;
11554
11555 begin
11556 if Nkind (Def) = N_Constrained_Array_Definition then
11557 Index := First (Discrete_Subtype_Definitions (Def));
11558 else
11559 Index := First (Subtype_Marks (Def));
11560 end if;
11561
11562 while Present (Index) loop
11563 Num := Num + 1;
11564 Next_Index (Index);
11565 end loop;
11566
11567 return Num;
11568 end Formal_Dimensions;
11569
11570 -- Start of processing for Validate_Array_Type_Instance
11571
11572 begin
11573 if not Is_Array_Type (Act_T) then
11574 Error_Msg_NE
11575 ("expect array type in instantiation of &", Actual, Gen_T);
11576 Abandon_Instantiation (Actual);
11577
11578 elsif Nkind (Def) = N_Constrained_Array_Definition then
11579 if not (Is_Constrained (Act_T)) then
11580 Error_Msg_NE
11581 ("expect constrained array in instantiation of &",
11582 Actual, Gen_T);
11583 Abandon_Instantiation (Actual);
11584 end if;
11585
11586 else
11587 if Is_Constrained (Act_T) then
11588 Error_Msg_NE
11589 ("expect unconstrained array in instantiation of &",
11590 Actual, Gen_T);
11591 Abandon_Instantiation (Actual);
11592 end if;
11593 end if;
11594
11595 if Formal_Dimensions /= Number_Dimensions (Act_T) then
11596 Error_Msg_NE
11597 ("dimensions of actual do not match formal &", Actual, Gen_T);
11598 Abandon_Instantiation (Actual);
11599 end if;
11600
11601 I1 := First_Index (A_Gen_T);
11602 I2 := First_Index (Act_T);
11603 for J in 1 .. Formal_Dimensions loop
11604
11605 -- If the indexes of the actual were given by a subtype_mark,
11606 -- the index was transformed into a range attribute. Retrieve
11607 -- the original type mark for checking.
11608
11609 if Is_Entity_Name (Original_Node (I2)) then
11610 T2 := Entity (Original_Node (I2));
11611 else
11612 T2 := Etype (I2);
11613 end if;
11614
11615 if not Subtypes_Match
11616 (Find_Actual_Type (Etype (I1), A_Gen_T), T2)
11617 then
11618 Error_Msg_NE
11619 ("index types of actual do not match those of formal &",
11620 Actual, Gen_T);
11621 Abandon_Instantiation (Actual);
11622 end if;
11623
11624 Next_Index (I1);
11625 Next_Index (I2);
11626 end loop;
11627
11628 -- Check matching subtypes. Note that there are complex visibility
11629 -- issues when the generic is a child unit and some aspect of the
11630 -- generic type is declared in a parent unit of the generic. We do
11631 -- the test to handle this special case only after a direct check
11632 -- for static matching has failed. The case where both the component
11633 -- type and the array type are separate formals, and the component
11634 -- type is a private view may also require special checking in
11635 -- Subtypes_Match.
11636
11637 if Subtypes_Match
11638 (Component_Type (A_Gen_T), Component_Type (Act_T))
11639 or else
11640 Subtypes_Match
11641 (Find_Actual_Type (Component_Type (A_Gen_T), A_Gen_T),
11642 Component_Type (Act_T))
11643 then
11644 null;
11645 else
11646 Error_Msg_NE
11647 ("component subtype of actual does not match that of formal &",
11648 Actual, Gen_T);
11649 Abandon_Instantiation (Actual);
11650 end if;
11651
11652 if Has_Aliased_Components (A_Gen_T)
11653 and then not Has_Aliased_Components (Act_T)
11654 then
11655 Error_Msg_NE
11656 ("actual must have aliased components to match formal type &",
11657 Actual, Gen_T);
11658 end if;
11659 end Validate_Array_Type_Instance;
11660
11661 -----------------------------------------------
11662 -- Validate_Derived_Interface_Type_Instance --
11663 -----------------------------------------------
11664
11665 procedure Validate_Derived_Interface_Type_Instance is
11666 Par : constant Entity_Id := Entity (Subtype_Indication (Def));
11667 Elmt : Elmt_Id;
11668
11669 begin
11670 -- First apply interface instance checks
11671
11672 Validate_Interface_Type_Instance;
11673
11674 -- Verify that immediate parent interface is an ancestor of
11675 -- the actual.
11676
11677 if Present (Par)
11678 and then not Interface_Present_In_Ancestor (Act_T, Par)
11679 then
11680 Error_Msg_NE
11681 ("interface actual must include progenitor&", Actual, Par);
11682 end if;
11683
11684 -- Now verify that the actual includes all other ancestors of
11685 -- the formal.
11686
11687 Elmt := First_Elmt (Interfaces (A_Gen_T));
11688 while Present (Elmt) loop
11689 if not Interface_Present_In_Ancestor
11690 (Act_T, Get_Instance_Of (Node (Elmt)))
11691 then
11692 Error_Msg_NE
11693 ("interface actual must include progenitor&",
11694 Actual, Node (Elmt));
11695 end if;
11696
11697 Next_Elmt (Elmt);
11698 end loop;
11699 end Validate_Derived_Interface_Type_Instance;
11700
11701 ------------------------------------
11702 -- Validate_Derived_Type_Instance --
11703 ------------------------------------
11704
11705 procedure Validate_Derived_Type_Instance is
11706 Actual_Discr : Entity_Id;
11707 Ancestor_Discr : Entity_Id;
11708
11709 begin
11710 -- If the parent type in the generic declaration is itself a previous
11711 -- formal type, then it is local to the generic and absent from the
11712 -- analyzed generic definition. In that case the ancestor is the
11713 -- instance of the formal (which must have been instantiated
11714 -- previously), unless the ancestor is itself a formal derived type.
11715 -- In this latter case (which is the subject of Corrigendum 8652/0038
11716 -- (AI-202) the ancestor of the formals is the ancestor of its
11717 -- parent. Otherwise, the analyzed generic carries the parent type.
11718 -- If the parent type is defined in a previous formal package, then
11719 -- the scope of that formal package is that of the generic type
11720 -- itself, and it has already been mapped into the corresponding type
11721 -- in the actual package.
11722
11723 -- Common case: parent type defined outside of the generic
11724
11725 if Is_Entity_Name (Subtype_Mark (Def))
11726 and then Present (Entity (Subtype_Mark (Def)))
11727 then
11728 Ancestor := Get_Instance_Of (Entity (Subtype_Mark (Def)));
11729
11730 -- Check whether parent is defined in a previous formal package
11731
11732 elsif
11733 Scope (Scope (Base_Type (Etype (A_Gen_T)))) = Scope (A_Gen_T)
11734 then
11735 Ancestor :=
11736 Get_Instance_Of (Base_Type (Etype (A_Gen_T)));
11737
11738 -- The type may be a local derivation, or a type extension of a
11739 -- previous formal, or of a formal of a parent package.
11740
11741 elsif Is_Derived_Type (Get_Instance_Of (A_Gen_T))
11742 or else
11743 Ekind (Get_Instance_Of (A_Gen_T)) = E_Record_Type_With_Private
11744 then
11745 -- Check whether the parent is another derived formal type in the
11746 -- same generic unit.
11747
11748 if Etype (A_Gen_T) /= A_Gen_T
11749 and then Is_Generic_Type (Etype (A_Gen_T))
11750 and then Scope (Etype (A_Gen_T)) = Scope (A_Gen_T)
11751 and then Etype (Etype (A_Gen_T)) /= Etype (A_Gen_T)
11752 then
11753 -- Locate ancestor of parent from the subtype declaration
11754 -- created for the actual.
11755
11756 declare
11757 Decl : Node_Id;
11758
11759 begin
11760 Decl := First (Actual_Decls);
11761 while Present (Decl) loop
11762 if Nkind (Decl) = N_Subtype_Declaration
11763 and then Chars (Defining_Identifier (Decl)) =
11764 Chars (Etype (A_Gen_T))
11765 then
11766 Ancestor := Generic_Parent_Type (Decl);
11767 exit;
11768 else
11769 Next (Decl);
11770 end if;
11771 end loop;
11772 end;
11773
11774 pragma Assert (Present (Ancestor));
11775
11776 -- The ancestor itself may be a previous formal that has been
11777 -- instantiated.
11778
11779 Ancestor := Get_Instance_Of (Ancestor);
11780
11781 else
11782 Ancestor :=
11783 Get_Instance_Of (Base_Type (Get_Instance_Of (A_Gen_T)));
11784 end if;
11785
11786 -- Check whether parent is a previous formal of the current generic
11787
11788 elsif Is_Derived_Type (A_Gen_T)
11789 and then Is_Generic_Type (Etype (A_Gen_T))
11790 and then Scope (A_Gen_T) = Scope (Etype (A_Gen_T))
11791 then
11792 Ancestor := Get_Instance_Of (First_Subtype (Etype (A_Gen_T)));
11793
11794 -- An unusual case: the actual is a type declared in a parent unit,
11795 -- but is not a formal type so there is no instance_of for it.
11796 -- Retrieve it by analyzing the record extension.
11797
11798 elsif Is_Child_Unit (Scope (A_Gen_T))
11799 and then In_Open_Scopes (Scope (Act_T))
11800 and then Is_Generic_Instance (Scope (Act_T))
11801 then
11802 Analyze (Subtype_Mark (Def));
11803 Ancestor := Entity (Subtype_Mark (Def));
11804
11805 else
11806 Ancestor := Get_Instance_Of (Etype (Base_Type (A_Gen_T)));
11807 end if;
11808
11809 -- If the formal derived type has pragma Preelaborable_Initialization
11810 -- then the actual type must have preelaborable initialization.
11811
11812 if Known_To_Have_Preelab_Init (A_Gen_T)
11813 and then not Has_Preelaborable_Initialization (Act_T)
11814 then
11815 Error_Msg_NE
11816 ("actual for & must have preelaborable initialization",
11817 Actual, Gen_T);
11818 end if;
11819
11820 -- Ada 2005 (AI-251)
11821
11822 if Ada_Version >= Ada_2005 and then Is_Interface (Ancestor) then
11823 if not Interface_Present_In_Ancestor (Act_T, Ancestor) then
11824 Error_Msg_NE
11825 ("(Ada 2005) expected type implementing & in instantiation",
11826 Actual, Ancestor);
11827 end if;
11828
11829 -- Finally verify that the (instance of) the ancestor is an ancestor
11830 -- of the actual.
11831
11832 elsif not Is_Ancestor (Base_Type (Ancestor), Act_T) then
11833 Error_Msg_NE
11834 ("expect type derived from & in instantiation",
11835 Actual, First_Subtype (Ancestor));
11836 Abandon_Instantiation (Actual);
11837 end if;
11838
11839 -- Ada 2005 (AI-443): Synchronized formal derived type checks. Note
11840 -- that the formal type declaration has been rewritten as a private
11841 -- extension.
11842
11843 if Ada_Version >= Ada_2005
11844 and then Nkind (Parent (A_Gen_T)) = N_Private_Extension_Declaration
11845 and then Synchronized_Present (Parent (A_Gen_T))
11846 then
11847 -- The actual must be a synchronized tagged type
11848
11849 if not Is_Tagged_Type (Act_T) then
11850 Error_Msg_N
11851 ("actual of synchronized type must be tagged", Actual);
11852 Abandon_Instantiation (Actual);
11853
11854 elsif Nkind (Parent (Act_T)) = N_Full_Type_Declaration
11855 and then Nkind (Type_Definition (Parent (Act_T))) =
11856 N_Derived_Type_Definition
11857 and then not Synchronized_Present
11858 (Type_Definition (Parent (Act_T)))
11859 then
11860 Error_Msg_N
11861 ("actual of synchronized type must be synchronized", Actual);
11862 Abandon_Instantiation (Actual);
11863 end if;
11864 end if;
11865
11866 -- Perform atomic/volatile checks (RM C.6(12)). Note that AI05-0218-1
11867 -- removes the second instance of the phrase "or allow pass by copy".
11868
11869 if Is_Atomic (Act_T) and then not Is_Atomic (Ancestor) then
11870 Error_Msg_N
11871 ("cannot have atomic actual type for non-atomic formal type",
11872 Actual);
11873
11874 elsif Is_Volatile (Act_T) and then not Is_Volatile (Ancestor) then
11875 Error_Msg_N
11876 ("cannot have volatile actual type for non-volatile formal type",
11877 Actual);
11878 end if;
11879
11880 -- It should not be necessary to check for unknown discriminants on
11881 -- Formal, but for some reason Has_Unknown_Discriminants is false for
11882 -- A_Gen_T, so Is_Definite_Subtype incorrectly returns True. This
11883 -- needs fixing. ???
11884
11885 if Is_Definite_Subtype (A_Gen_T)
11886 and then not Unknown_Discriminants_Present (Formal)
11887 and then not Is_Definite_Subtype (Act_T)
11888 then
11889 Error_Msg_N ("actual subtype must be constrained", Actual);
11890 Abandon_Instantiation (Actual);
11891 end if;
11892
11893 if not Unknown_Discriminants_Present (Formal) then
11894 if Is_Constrained (Ancestor) then
11895 if not Is_Constrained (Act_T) then
11896 Error_Msg_N ("actual subtype must be constrained", Actual);
11897 Abandon_Instantiation (Actual);
11898 end if;
11899
11900 -- Ancestor is unconstrained, Check if generic formal and actual
11901 -- agree on constrainedness. The check only applies to array types
11902 -- and discriminated types.
11903
11904 elsif Is_Constrained (Act_T) then
11905 if Ekind (Ancestor) = E_Access_Type
11906 or else (not Is_Constrained (A_Gen_T)
11907 and then Is_Composite_Type (A_Gen_T))
11908 then
11909 Error_Msg_N ("actual subtype must be unconstrained", Actual);
11910 Abandon_Instantiation (Actual);
11911 end if;
11912
11913 -- A class-wide type is only allowed if the formal has unknown
11914 -- discriminants.
11915
11916 elsif Is_Class_Wide_Type (Act_T)
11917 and then not Has_Unknown_Discriminants (Ancestor)
11918 then
11919 Error_Msg_NE
11920 ("actual for & cannot be a class-wide type", Actual, Gen_T);
11921 Abandon_Instantiation (Actual);
11922
11923 -- Otherwise, the formal and actual must have the same number
11924 -- of discriminants and each discriminant of the actual must
11925 -- correspond to a discriminant of the formal.
11926
11927 elsif Has_Discriminants (Act_T)
11928 and then not Has_Unknown_Discriminants (Act_T)
11929 and then Has_Discriminants (Ancestor)
11930 then
11931 Actual_Discr := First_Discriminant (Act_T);
11932 Ancestor_Discr := First_Discriminant (Ancestor);
11933 while Present (Actual_Discr)
11934 and then Present (Ancestor_Discr)
11935 loop
11936 if Base_Type (Act_T) /= Base_Type (Ancestor) and then
11937 No (Corresponding_Discriminant (Actual_Discr))
11938 then
11939 Error_Msg_NE
11940 ("discriminant & does not correspond "
11941 & "to ancestor discriminant", Actual, Actual_Discr);
11942 Abandon_Instantiation (Actual);
11943 end if;
11944
11945 Next_Discriminant (Actual_Discr);
11946 Next_Discriminant (Ancestor_Discr);
11947 end loop;
11948
11949 if Present (Actual_Discr) or else Present (Ancestor_Discr) then
11950 Error_Msg_NE
11951 ("actual for & must have same number of discriminants",
11952 Actual, Gen_T);
11953 Abandon_Instantiation (Actual);
11954 end if;
11955
11956 -- This case should be caught by the earlier check for
11957 -- constrainedness, but the check here is added for completeness.
11958
11959 elsif Has_Discriminants (Act_T)
11960 and then not Has_Unknown_Discriminants (Act_T)
11961 then
11962 Error_Msg_NE
11963 ("actual for & must not have discriminants", Actual, Gen_T);
11964 Abandon_Instantiation (Actual);
11965
11966 elsif Has_Discriminants (Ancestor) then
11967 Error_Msg_NE
11968 ("actual for & must have known discriminants", Actual, Gen_T);
11969 Abandon_Instantiation (Actual);
11970 end if;
11971
11972 if not Subtypes_Statically_Compatible
11973 (Act_T, Ancestor, Formal_Derived_Matching => True)
11974 then
11975 Error_Msg_N
11976 ("constraint on actual is incompatible with formal", Actual);
11977 Abandon_Instantiation (Actual);
11978 end if;
11979 end if;
11980
11981 -- If the formal and actual types are abstract, check that there
11982 -- are no abstract primitives of the actual type that correspond to
11983 -- nonabstract primitives of the formal type (second sentence of
11984 -- RM95 3.9.3(9)).
11985
11986 if Is_Abstract_Type (A_Gen_T) and then Is_Abstract_Type (Act_T) then
11987 Check_Abstract_Primitives : declare
11988 Gen_Prims : constant Elist_Id :=
11989 Primitive_Operations (A_Gen_T);
11990 Gen_Elmt : Elmt_Id;
11991 Gen_Subp : Entity_Id;
11992 Anc_Subp : Entity_Id;
11993 Anc_Formal : Entity_Id;
11994 Anc_F_Type : Entity_Id;
11995
11996 Act_Prims : constant Elist_Id := Primitive_Operations (Act_T);
11997 Act_Elmt : Elmt_Id;
11998 Act_Subp : Entity_Id;
11999 Act_Formal : Entity_Id;
12000 Act_F_Type : Entity_Id;
12001
12002 Subprograms_Correspond : Boolean;
12003
12004 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean;
12005 -- Returns true if T2 is derived directly or indirectly from
12006 -- T1, including derivations from interfaces. T1 and T2 are
12007 -- required to be specific tagged base types.
12008
12009 ------------------------
12010 -- Is_Tagged_Ancestor --
12011 ------------------------
12012
12013 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean
12014 is
12015 Intfc_Elmt : Elmt_Id;
12016
12017 begin
12018 -- The predicate is satisfied if the types are the same
12019
12020 if T1 = T2 then
12021 return True;
12022
12023 -- If we've reached the top of the derivation chain then
12024 -- we know that T1 is not an ancestor of T2.
12025
12026 elsif Etype (T2) = T2 then
12027 return False;
12028
12029 -- Proceed to check T2's immediate parent
12030
12031 elsif Is_Ancestor (T1, Base_Type (Etype (T2))) then
12032 return True;
12033
12034 -- Finally, check to see if T1 is an ancestor of any of T2's
12035 -- progenitors.
12036
12037 else
12038 Intfc_Elmt := First_Elmt (Interfaces (T2));
12039 while Present (Intfc_Elmt) loop
12040 if Is_Ancestor (T1, Node (Intfc_Elmt)) then
12041 return True;
12042 end if;
12043
12044 Next_Elmt (Intfc_Elmt);
12045 end loop;
12046 end if;
12047
12048 return False;
12049 end Is_Tagged_Ancestor;
12050
12051 -- Start of processing for Check_Abstract_Primitives
12052
12053 begin
12054 -- Loop over all of the formal derived type's primitives
12055
12056 Gen_Elmt := First_Elmt (Gen_Prims);
12057 while Present (Gen_Elmt) loop
12058 Gen_Subp := Node (Gen_Elmt);
12059
12060 -- If the primitive of the formal is not abstract, then
12061 -- determine whether there is a corresponding primitive of
12062 -- the actual type that's abstract.
12063
12064 if not Is_Abstract_Subprogram (Gen_Subp) then
12065 Act_Elmt := First_Elmt (Act_Prims);
12066 while Present (Act_Elmt) loop
12067 Act_Subp := Node (Act_Elmt);
12068
12069 -- If we find an abstract primitive of the actual,
12070 -- then we need to test whether it corresponds to the
12071 -- subprogram from which the generic formal primitive
12072 -- is inherited.
12073
12074 if Is_Abstract_Subprogram (Act_Subp) then
12075 Anc_Subp := Alias (Gen_Subp);
12076
12077 -- Test whether we have a corresponding primitive
12078 -- by comparing names, kinds, formal types, and
12079 -- result types.
12080
12081 if Chars (Anc_Subp) = Chars (Act_Subp)
12082 and then Ekind (Anc_Subp) = Ekind (Act_Subp)
12083 then
12084 Anc_Formal := First_Formal (Anc_Subp);
12085 Act_Formal := First_Formal (Act_Subp);
12086 while Present (Anc_Formal)
12087 and then Present (Act_Formal)
12088 loop
12089 Anc_F_Type := Etype (Anc_Formal);
12090 Act_F_Type := Etype (Act_Formal);
12091
12092 if Ekind (Anc_F_Type) =
12093 E_Anonymous_Access_Type
12094 then
12095 Anc_F_Type := Designated_Type (Anc_F_Type);
12096
12097 if Ekind (Act_F_Type) =
12098 E_Anonymous_Access_Type
12099 then
12100 Act_F_Type :=
12101 Designated_Type (Act_F_Type);
12102 else
12103 exit;
12104 end if;
12105
12106 elsif
12107 Ekind (Act_F_Type) = E_Anonymous_Access_Type
12108 then
12109 exit;
12110 end if;
12111
12112 Anc_F_Type := Base_Type (Anc_F_Type);
12113 Act_F_Type := Base_Type (Act_F_Type);
12114
12115 -- If the formal is controlling, then the
12116 -- the type of the actual primitive's formal
12117 -- must be derived directly or indirectly
12118 -- from the type of the ancestor primitive's
12119 -- formal.
12120
12121 if Is_Controlling_Formal (Anc_Formal) then
12122 if not Is_Tagged_Ancestor
12123 (Anc_F_Type, Act_F_Type)
12124 then
12125 exit;
12126 end if;
12127
12128 -- Otherwise the types of the formals must
12129 -- be the same.
12130
12131 elsif Anc_F_Type /= Act_F_Type then
12132 exit;
12133 end if;
12134
12135 Next_Entity (Anc_Formal);
12136 Next_Entity (Act_Formal);
12137 end loop;
12138
12139 -- If we traversed through all of the formals
12140 -- then so far the subprograms correspond, so
12141 -- now check that any result types correspond.
12142
12143 if No (Anc_Formal) and then No (Act_Formal) then
12144 Subprograms_Correspond := True;
12145
12146 if Ekind (Act_Subp) = E_Function then
12147 Anc_F_Type := Etype (Anc_Subp);
12148 Act_F_Type := Etype (Act_Subp);
12149
12150 if Ekind (Anc_F_Type) =
12151 E_Anonymous_Access_Type
12152 then
12153 Anc_F_Type :=
12154 Designated_Type (Anc_F_Type);
12155
12156 if Ekind (Act_F_Type) =
12157 E_Anonymous_Access_Type
12158 then
12159 Act_F_Type :=
12160 Designated_Type (Act_F_Type);
12161 else
12162 Subprograms_Correspond := False;
12163 end if;
12164
12165 elsif
12166 Ekind (Act_F_Type)
12167 = E_Anonymous_Access_Type
12168 then
12169 Subprograms_Correspond := False;
12170 end if;
12171
12172 Anc_F_Type := Base_Type (Anc_F_Type);
12173 Act_F_Type := Base_Type (Act_F_Type);
12174
12175 -- Now either the result types must be
12176 -- the same or, if the result type is
12177 -- controlling, the result type of the
12178 -- actual primitive must descend from the
12179 -- result type of the ancestor primitive.
12180
12181 if Subprograms_Correspond
12182 and then Anc_F_Type /= Act_F_Type
12183 and then
12184 Has_Controlling_Result (Anc_Subp)
12185 and then not Is_Tagged_Ancestor
12186 (Anc_F_Type, Act_F_Type)
12187 then
12188 Subprograms_Correspond := False;
12189 end if;
12190 end if;
12191
12192 -- Found a matching subprogram belonging to
12193 -- formal ancestor type, so actual subprogram
12194 -- corresponds and this violates 3.9.3(9).
12195
12196 if Subprograms_Correspond then
12197 Error_Msg_NE
12198 ("abstract subprogram & overrides "
12199 & "nonabstract subprogram of ancestor",
12200 Actual, Act_Subp);
12201 end if;
12202 end if;
12203 end if;
12204 end if;
12205
12206 Next_Elmt (Act_Elmt);
12207 end loop;
12208 end if;
12209
12210 Next_Elmt (Gen_Elmt);
12211 end loop;
12212 end Check_Abstract_Primitives;
12213 end if;
12214
12215 -- Verify that limitedness matches. If parent is a limited
12216 -- interface then the generic formal is not unless declared
12217 -- explicitly so. If not declared limited, the actual cannot be
12218 -- limited (see AI05-0087).
12219
12220 -- Even though this AI is a binding interpretation, we enable the
12221 -- check only in Ada 2012 mode, because this improper construct
12222 -- shows up in user code and in existing B-tests.
12223
12224 if Is_Limited_Type (Act_T)
12225 and then not Is_Limited_Type (A_Gen_T)
12226 and then Ada_Version >= Ada_2012
12227 then
12228 if In_Instance then
12229 null;
12230 else
12231 Error_Msg_NE
12232 ("actual for non-limited & cannot be a limited type",
12233 Actual, Gen_T);
12234 Explain_Limited_Type (Act_T, Actual);
12235 Abandon_Instantiation (Actual);
12236 end if;
12237 end if;
12238 end Validate_Derived_Type_Instance;
12239
12240 ----------------------------------------
12241 -- Validate_Discriminated_Formal_Type --
12242 ----------------------------------------
12243
12244 procedure Validate_Discriminated_Formal_Type is
12245 Formal_Discr : Entity_Id;
12246 Actual_Discr : Entity_Id;
12247 Formal_Subt : Entity_Id;
12248
12249 begin
12250 if Has_Discriminants (A_Gen_T) then
12251 if not Has_Discriminants (Act_T) then
12252 Error_Msg_NE
12253 ("actual for & must have discriminants", Actual, Gen_T);
12254 Abandon_Instantiation (Actual);
12255
12256 elsif Is_Constrained (Act_T) then
12257 Error_Msg_NE
12258 ("actual for & must be unconstrained", Actual, Gen_T);
12259 Abandon_Instantiation (Actual);
12260
12261 else
12262 Formal_Discr := First_Discriminant (A_Gen_T);
12263 Actual_Discr := First_Discriminant (Act_T);
12264 while Formal_Discr /= Empty loop
12265 if Actual_Discr = Empty then
12266 Error_Msg_NE
12267 ("discriminants on actual do not match formal",
12268 Actual, Gen_T);
12269 Abandon_Instantiation (Actual);
12270 end if;
12271
12272 Formal_Subt := Get_Instance_Of (Etype (Formal_Discr));
12273
12274 -- Access discriminants match if designated types do
12275
12276 if Ekind (Base_Type (Formal_Subt)) = E_Anonymous_Access_Type
12277 and then (Ekind (Base_Type (Etype (Actual_Discr)))) =
12278 E_Anonymous_Access_Type
12279 and then
12280 Get_Instance_Of
12281 (Designated_Type (Base_Type (Formal_Subt))) =
12282 Designated_Type (Base_Type (Etype (Actual_Discr)))
12283 then
12284 null;
12285
12286 elsif Base_Type (Formal_Subt) /=
12287 Base_Type (Etype (Actual_Discr))
12288 then
12289 Error_Msg_NE
12290 ("types of actual discriminants must match formal",
12291 Actual, Gen_T);
12292 Abandon_Instantiation (Actual);
12293
12294 elsif not Subtypes_Statically_Match
12295 (Formal_Subt, Etype (Actual_Discr))
12296 and then Ada_Version >= Ada_95
12297 then
12298 Error_Msg_NE
12299 ("subtypes of actual discriminants must match formal",
12300 Actual, Gen_T);
12301 Abandon_Instantiation (Actual);
12302 end if;
12303
12304 Next_Discriminant (Formal_Discr);
12305 Next_Discriminant (Actual_Discr);
12306 end loop;
12307
12308 if Actual_Discr /= Empty then
12309 Error_Msg_NE
12310 ("discriminants on actual do not match formal",
12311 Actual, Gen_T);
12312 Abandon_Instantiation (Actual);
12313 end if;
12314 end if;
12315 end if;
12316 end Validate_Discriminated_Formal_Type;
12317
12318 ---------------------------------------
12319 -- Validate_Incomplete_Type_Instance --
12320 ---------------------------------------
12321
12322 procedure Validate_Incomplete_Type_Instance is
12323 begin
12324 if not Is_Tagged_Type (Act_T)
12325 and then Is_Tagged_Type (A_Gen_T)
12326 then
12327 Error_Msg_NE
12328 ("actual for & must be a tagged type", Actual, Gen_T);
12329 end if;
12330
12331 Validate_Discriminated_Formal_Type;
12332 end Validate_Incomplete_Type_Instance;
12333
12334 --------------------------------------
12335 -- Validate_Interface_Type_Instance --
12336 --------------------------------------
12337
12338 procedure Validate_Interface_Type_Instance is
12339 begin
12340 if not Is_Interface (Act_T) then
12341 Error_Msg_NE
12342 ("actual for formal interface type must be an interface",
12343 Actual, Gen_T);
12344
12345 elsif Is_Limited_Type (Act_T) /= Is_Limited_Type (A_Gen_T)
12346 or else Is_Task_Interface (A_Gen_T) /= Is_Task_Interface (Act_T)
12347 or else Is_Protected_Interface (A_Gen_T) /=
12348 Is_Protected_Interface (Act_T)
12349 or else Is_Synchronized_Interface (A_Gen_T) /=
12350 Is_Synchronized_Interface (Act_T)
12351 then
12352 Error_Msg_NE
12353 ("actual for interface& does not match (RM 12.5.5(4))",
12354 Actual, Gen_T);
12355 end if;
12356 end Validate_Interface_Type_Instance;
12357
12358 ------------------------------------
12359 -- Validate_Private_Type_Instance --
12360 ------------------------------------
12361
12362 procedure Validate_Private_Type_Instance is
12363 begin
12364 if Is_Limited_Type (Act_T)
12365 and then not Is_Limited_Type (A_Gen_T)
12366 then
12367 if In_Instance then
12368 null;
12369 else
12370 Error_Msg_NE
12371 ("actual for non-limited & cannot be a limited type", Actual,
12372 Gen_T);
12373 Explain_Limited_Type (Act_T, Actual);
12374 Abandon_Instantiation (Actual);
12375 end if;
12376
12377 elsif Known_To_Have_Preelab_Init (A_Gen_T)
12378 and then not Has_Preelaborable_Initialization (Act_T)
12379 then
12380 Error_Msg_NE
12381 ("actual for & must have preelaborable initialization", Actual,
12382 Gen_T);
12383
12384 elsif not Is_Definite_Subtype (Act_T)
12385 and then Is_Definite_Subtype (A_Gen_T)
12386 and then Ada_Version >= Ada_95
12387 then
12388 Error_Msg_NE
12389 ("actual for & must be a definite subtype", Actual, Gen_T);
12390
12391 elsif not Is_Tagged_Type (Act_T)
12392 and then Is_Tagged_Type (A_Gen_T)
12393 then
12394 Error_Msg_NE
12395 ("actual for & must be a tagged type", Actual, Gen_T);
12396 end if;
12397
12398 Validate_Discriminated_Formal_Type;
12399 Ancestor := Gen_T;
12400 end Validate_Private_Type_Instance;
12401
12402 -- Start of processing for Instantiate_Type
12403
12404 begin
12405 if Get_Instance_Of (A_Gen_T) /= A_Gen_T then
12406 Error_Msg_N ("duplicate instantiation of generic type", Actual);
12407 return New_List (Error);
12408
12409 elsif not Is_Entity_Name (Actual)
12410 or else not Is_Type (Entity (Actual))
12411 then
12412 Error_Msg_NE
12413 ("expect valid subtype mark to instantiate &", Actual, Gen_T);
12414 Abandon_Instantiation (Actual);
12415
12416 else
12417 Act_T := Entity (Actual);
12418
12419 -- Ada 2005 (AI-216): An Unchecked_Union subtype shall only be passed
12420 -- as a generic actual parameter if the corresponding formal type
12421 -- does not have a known_discriminant_part, or is a formal derived
12422 -- type that is an Unchecked_Union type.
12423
12424 if Is_Unchecked_Union (Base_Type (Act_T)) then
12425 if not Has_Discriminants (A_Gen_T)
12426 or else (Is_Derived_Type (A_Gen_T)
12427 and then Is_Unchecked_Union (A_Gen_T))
12428 then
12429 null;
12430 else
12431 Error_Msg_N ("unchecked union cannot be the actual for a "
12432 & "discriminated formal type", Act_T);
12433
12434 end if;
12435 end if;
12436
12437 -- Deal with fixed/floating restrictions
12438
12439 if Is_Floating_Point_Type (Act_T) then
12440 Check_Restriction (No_Floating_Point, Actual);
12441 elsif Is_Fixed_Point_Type (Act_T) then
12442 Check_Restriction (No_Fixed_Point, Actual);
12443 end if;
12444
12445 -- Deal with error of using incomplete type as generic actual.
12446 -- This includes limited views of a type, even if the non-limited
12447 -- view may be available.
12448
12449 if Ekind (Act_T) = E_Incomplete_Type
12450 or else (Is_Class_Wide_Type (Act_T)
12451 and then Ekind (Root_Type (Act_T)) = E_Incomplete_Type)
12452 then
12453 -- If the formal is an incomplete type, the actual can be
12454 -- incomplete as well.
12455
12456 if Ekind (A_Gen_T) = E_Incomplete_Type then
12457 null;
12458
12459 elsif Is_Class_Wide_Type (Act_T)
12460 or else No (Full_View (Act_T))
12461 then
12462 Error_Msg_N ("premature use of incomplete type", Actual);
12463 Abandon_Instantiation (Actual);
12464 else
12465 Act_T := Full_View (Act_T);
12466 Set_Entity (Actual, Act_T);
12467
12468 if Has_Private_Component (Act_T) then
12469 Error_Msg_N
12470 ("premature use of type with private component", Actual);
12471 end if;
12472 end if;
12473
12474 -- Deal with error of premature use of private type as generic actual
12475
12476 elsif Is_Private_Type (Act_T)
12477 and then Is_Private_Type (Base_Type (Act_T))
12478 and then not Is_Generic_Type (Act_T)
12479 and then not Is_Derived_Type (Act_T)
12480 and then No (Full_View (Root_Type (Act_T)))
12481 then
12482 -- If the formal is an incomplete type, the actual can be
12483 -- private or incomplete as well.
12484
12485 if Ekind (A_Gen_T) = E_Incomplete_Type then
12486 null;
12487 else
12488 Error_Msg_N ("premature use of private type", Actual);
12489 end if;
12490
12491 elsif Has_Private_Component (Act_T) then
12492 Error_Msg_N
12493 ("premature use of type with private component", Actual);
12494 end if;
12495
12496 Set_Instance_Of (A_Gen_T, Act_T);
12497
12498 -- If the type is generic, the class-wide type may also be used
12499
12500 if Is_Tagged_Type (A_Gen_T)
12501 and then Is_Tagged_Type (Act_T)
12502 and then not Is_Class_Wide_Type (A_Gen_T)
12503 then
12504 Set_Instance_Of (Class_Wide_Type (A_Gen_T),
12505 Class_Wide_Type (Act_T));
12506 end if;
12507
12508 if not Is_Abstract_Type (A_Gen_T)
12509 and then Is_Abstract_Type (Act_T)
12510 then
12511 Error_Msg_N
12512 ("actual of non-abstract formal cannot be abstract", Actual);
12513 end if;
12514
12515 -- A generic scalar type is a first subtype for which we generate
12516 -- an anonymous base type. Indicate that the instance of this base
12517 -- is the base type of the actual.
12518
12519 if Is_Scalar_Type (A_Gen_T) then
12520 Set_Instance_Of (Etype (A_Gen_T), Etype (Act_T));
12521 end if;
12522 end if;
12523
12524 if Error_Posted (Act_T) then
12525 null;
12526 else
12527 case Nkind (Def) is
12528 when N_Formal_Private_Type_Definition =>
12529 Validate_Private_Type_Instance;
12530
12531 when N_Formal_Incomplete_Type_Definition =>
12532 Validate_Incomplete_Type_Instance;
12533
12534 when N_Formal_Derived_Type_Definition =>
12535 Validate_Derived_Type_Instance;
12536
12537 when N_Formal_Discrete_Type_Definition =>
12538 if not Is_Discrete_Type (Act_T) then
12539 Error_Msg_NE
12540 ("expect discrete type in instantiation of&",
12541 Actual, Gen_T);
12542 Abandon_Instantiation (Actual);
12543 end if;
12544
12545 Diagnose_Predicated_Actual;
12546
12547 when N_Formal_Signed_Integer_Type_Definition =>
12548 if not Is_Signed_Integer_Type (Act_T) then
12549 Error_Msg_NE
12550 ("expect signed integer type in instantiation of&",
12551 Actual, Gen_T);
12552 Abandon_Instantiation (Actual);
12553 end if;
12554
12555 Diagnose_Predicated_Actual;
12556
12557 when N_Formal_Modular_Type_Definition =>
12558 if not Is_Modular_Integer_Type (Act_T) then
12559 Error_Msg_NE
12560 ("expect modular type in instantiation of &",
12561 Actual, Gen_T);
12562 Abandon_Instantiation (Actual);
12563 end if;
12564
12565 Diagnose_Predicated_Actual;
12566
12567 when N_Formal_Floating_Point_Definition =>
12568 if not Is_Floating_Point_Type (Act_T) then
12569 Error_Msg_NE
12570 ("expect float type in instantiation of &", Actual, Gen_T);
12571 Abandon_Instantiation (Actual);
12572 end if;
12573
12574 when N_Formal_Ordinary_Fixed_Point_Definition =>
12575 if not Is_Ordinary_Fixed_Point_Type (Act_T) then
12576 Error_Msg_NE
12577 ("expect ordinary fixed point type in instantiation of &",
12578 Actual, Gen_T);
12579 Abandon_Instantiation (Actual);
12580 end if;
12581
12582 when N_Formal_Decimal_Fixed_Point_Definition =>
12583 if not Is_Decimal_Fixed_Point_Type (Act_T) then
12584 Error_Msg_NE
12585 ("expect decimal type in instantiation of &",
12586 Actual, Gen_T);
12587 Abandon_Instantiation (Actual);
12588 end if;
12589
12590 when N_Array_Type_Definition =>
12591 Validate_Array_Type_Instance;
12592
12593 when N_Access_To_Object_Definition =>
12594 Validate_Access_Type_Instance;
12595
12596 when N_Access_Function_Definition |
12597 N_Access_Procedure_Definition =>
12598 Validate_Access_Subprogram_Instance;
12599
12600 when N_Record_Definition =>
12601 Validate_Interface_Type_Instance;
12602
12603 when N_Derived_Type_Definition =>
12604 Validate_Derived_Interface_Type_Instance;
12605
12606 when others =>
12607 raise Program_Error;
12608
12609 end case;
12610 end if;
12611
12612 Subt := New_Copy (Gen_T);
12613
12614 -- Use adjusted sloc of subtype name as the location for other nodes in
12615 -- the subtype declaration.
12616
12617 Loc := Sloc (Subt);
12618
12619 Decl_Node :=
12620 Make_Subtype_Declaration (Loc,
12621 Defining_Identifier => Subt,
12622 Subtype_Indication => New_Occurrence_Of (Act_T, Loc));
12623
12624 if Is_Private_Type (Act_T) then
12625 Set_Has_Private_View (Subtype_Indication (Decl_Node));
12626
12627 elsif Is_Access_Type (Act_T)
12628 and then Is_Private_Type (Designated_Type (Act_T))
12629 then
12630 Set_Has_Private_View (Subtype_Indication (Decl_Node));
12631 end if;
12632
12633 -- In Ada 2012 the actual may be a limited view. Indicate that
12634 -- the local subtype must be treated as such.
12635
12636 if From_Limited_With (Act_T) then
12637 Set_Ekind (Subt, E_Incomplete_Subtype);
12638 Set_From_Limited_With (Subt);
12639 end if;
12640
12641 Decl_Nodes := New_List (Decl_Node);
12642
12643 -- Flag actual derived types so their elaboration produces the
12644 -- appropriate renamings for the primitive operations of the ancestor.
12645 -- Flag actual for formal private types as well, to determine whether
12646 -- operations in the private part may override inherited operations.
12647 -- If the formal has an interface list, the ancestor is not the
12648 -- parent, but the analyzed formal that includes the interface
12649 -- operations of all its progenitors.
12650
12651 -- Same treatment for formal private types, so we can check whether the
12652 -- type is tagged limited when validating derivations in the private
12653 -- part. (See AI05-096).
12654
12655 if Nkind (Def) = N_Formal_Derived_Type_Definition then
12656 if Present (Interface_List (Def)) then
12657 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
12658 else
12659 Set_Generic_Parent_Type (Decl_Node, Ancestor);
12660 end if;
12661
12662 elsif Nkind_In (Def, N_Formal_Private_Type_Definition,
12663 N_Formal_Incomplete_Type_Definition)
12664 then
12665 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
12666 end if;
12667
12668 -- If the actual is a synchronized type that implements an interface,
12669 -- the primitive operations are attached to the corresponding record,
12670 -- and we have to treat it as an additional generic actual, so that its
12671 -- primitive operations become visible in the instance. The task or
12672 -- protected type itself does not carry primitive operations.
12673
12674 if Is_Concurrent_Type (Act_T)
12675 and then Is_Tagged_Type (Act_T)
12676 and then Present (Corresponding_Record_Type (Act_T))
12677 and then Present (Ancestor)
12678 and then Is_Interface (Ancestor)
12679 then
12680 declare
12681 Corr_Rec : constant Entity_Id :=
12682 Corresponding_Record_Type (Act_T);
12683 New_Corr : Entity_Id;
12684 Corr_Decl : Node_Id;
12685
12686 begin
12687 New_Corr := Make_Temporary (Loc, 'S');
12688 Corr_Decl :=
12689 Make_Subtype_Declaration (Loc,
12690 Defining_Identifier => New_Corr,
12691 Subtype_Indication =>
12692 New_Occurrence_Of (Corr_Rec, Loc));
12693 Append_To (Decl_Nodes, Corr_Decl);
12694
12695 if Ekind (Act_T) = E_Task_Type then
12696 Set_Ekind (Subt, E_Task_Subtype);
12697 else
12698 Set_Ekind (Subt, E_Protected_Subtype);
12699 end if;
12700
12701 Set_Corresponding_Record_Type (Subt, Corr_Rec);
12702 Set_Generic_Parent_Type (Corr_Decl, Ancestor);
12703 Set_Generic_Parent_Type (Decl_Node, Empty);
12704 end;
12705 end if;
12706
12707 -- For a floating-point type, capture dimension info if any, because
12708 -- the generated subtype declaration does not come from source and
12709 -- will not process dimensions.
12710
12711 if Is_Floating_Point_Type (Act_T) then
12712 Copy_Dimensions (Act_T, Subt);
12713 end if;
12714
12715 return Decl_Nodes;
12716 end Instantiate_Type;
12717
12718 ---------------------
12719 -- Is_In_Main_Unit --
12720 ---------------------
12721
12722 function Is_In_Main_Unit (N : Node_Id) return Boolean is
12723 Unum : constant Unit_Number_Type := Get_Source_Unit (N);
12724 Current_Unit : Node_Id;
12725
12726 begin
12727 if Unum = Main_Unit then
12728 return True;
12729
12730 -- If the current unit is a subunit then it is either the main unit or
12731 -- is being compiled as part of the main unit.
12732
12733 elsif Nkind (N) = N_Compilation_Unit then
12734 return Nkind (Unit (N)) = N_Subunit;
12735 end if;
12736
12737 Current_Unit := Parent (N);
12738 while Present (Current_Unit)
12739 and then Nkind (Current_Unit) /= N_Compilation_Unit
12740 loop
12741 Current_Unit := Parent (Current_Unit);
12742 end loop;
12743
12744 -- The instantiation node is in the main unit, or else the current node
12745 -- (perhaps as the result of nested instantiations) is in the main unit,
12746 -- or in the declaration of the main unit, which in this last case must
12747 -- be a body.
12748
12749 return Unum = Main_Unit
12750 or else Current_Unit = Cunit (Main_Unit)
12751 or else Current_Unit = Library_Unit (Cunit (Main_Unit))
12752 or else (Present (Library_Unit (Current_Unit))
12753 and then Is_In_Main_Unit (Library_Unit (Current_Unit)));
12754 end Is_In_Main_Unit;
12755
12756 ----------------------------
12757 -- Load_Parent_Of_Generic --
12758 ----------------------------
12759
12760 procedure Load_Parent_Of_Generic
12761 (N : Node_Id;
12762 Spec : Node_Id;
12763 Body_Optional : Boolean := False)
12764 is
12765 Comp_Unit : constant Node_Id := Cunit (Get_Source_Unit (Spec));
12766 Saved_Style_Check : constant Boolean := Style_Check;
12767 Saved_Warnings : constant Warning_Record := Save_Warnings;
12768 True_Parent : Node_Id;
12769 Inst_Node : Node_Id;
12770 OK : Boolean;
12771 Previous_Instances : constant Elist_Id := New_Elmt_List;
12772
12773 procedure Collect_Previous_Instances (Decls : List_Id);
12774 -- Collect all instantiations in the given list of declarations, that
12775 -- precede the generic that we need to load. If the bodies of these
12776 -- instantiations are available, we must analyze them, to ensure that
12777 -- the public symbols generated are the same when the unit is compiled
12778 -- to generate code, and when it is compiled in the context of a unit
12779 -- that needs a particular nested instance. This process is applied to
12780 -- both package and subprogram instances.
12781
12782 --------------------------------
12783 -- Collect_Previous_Instances --
12784 --------------------------------
12785
12786 procedure Collect_Previous_Instances (Decls : List_Id) is
12787 Decl : Node_Id;
12788
12789 begin
12790 Decl := First (Decls);
12791 while Present (Decl) loop
12792 if Sloc (Decl) >= Sloc (Inst_Node) then
12793 return;
12794
12795 -- If Decl is an instantiation, then record it as requiring
12796 -- instantiation of the corresponding body, except if it is an
12797 -- abbreviated instantiation generated internally for conformance
12798 -- checking purposes only for the case of a formal package
12799 -- declared without a box (see Instantiate_Formal_Package). Such
12800 -- an instantiation does not generate any code (the actual code
12801 -- comes from actual) and thus does not need to be analyzed here.
12802 -- If the instantiation appears with a generic package body it is
12803 -- not analyzed here either.
12804
12805 elsif Nkind (Decl) = N_Package_Instantiation
12806 and then not Is_Internal (Defining_Entity (Decl))
12807 then
12808 Append_Elmt (Decl, Previous_Instances);
12809
12810 -- For a subprogram instantiation, omit instantiations intrinsic
12811 -- operations (Unchecked_Conversions, etc.) that have no bodies.
12812
12813 elsif Nkind_In (Decl, N_Function_Instantiation,
12814 N_Procedure_Instantiation)
12815 and then not Is_Intrinsic_Subprogram (Entity (Name (Decl)))
12816 then
12817 Append_Elmt (Decl, Previous_Instances);
12818
12819 elsif Nkind (Decl) = N_Package_Declaration then
12820 Collect_Previous_Instances
12821 (Visible_Declarations (Specification (Decl)));
12822 Collect_Previous_Instances
12823 (Private_Declarations (Specification (Decl)));
12824
12825 -- Previous non-generic bodies may contain instances as well
12826
12827 elsif Nkind (Decl) = N_Package_Body
12828 and then Ekind (Corresponding_Spec (Decl)) /= E_Generic_Package
12829 then
12830 Collect_Previous_Instances (Declarations (Decl));
12831
12832 elsif Nkind (Decl) = N_Subprogram_Body
12833 and then not Acts_As_Spec (Decl)
12834 and then not Is_Generic_Subprogram (Corresponding_Spec (Decl))
12835 then
12836 Collect_Previous_Instances (Declarations (Decl));
12837 end if;
12838
12839 Next (Decl);
12840 end loop;
12841 end Collect_Previous_Instances;
12842
12843 -- Start of processing for Load_Parent_Of_Generic
12844
12845 begin
12846 if not In_Same_Source_Unit (N, Spec)
12847 or else Nkind (Unit (Comp_Unit)) = N_Package_Declaration
12848 or else (Nkind (Unit (Comp_Unit)) = N_Package_Body
12849 and then not Is_In_Main_Unit (Spec))
12850 then
12851 -- Find body of parent of spec, and analyze it. A special case arises
12852 -- when the parent is an instantiation, that is to say when we are
12853 -- currently instantiating a nested generic. In that case, there is
12854 -- no separate file for the body of the enclosing instance. Instead,
12855 -- the enclosing body must be instantiated as if it were a pending
12856 -- instantiation, in order to produce the body for the nested generic
12857 -- we require now. Note that in that case the generic may be defined
12858 -- in a package body, the instance defined in the same package body,
12859 -- and the original enclosing body may not be in the main unit.
12860
12861 Inst_Node := Empty;
12862
12863 True_Parent := Parent (Spec);
12864 while Present (True_Parent)
12865 and then Nkind (True_Parent) /= N_Compilation_Unit
12866 loop
12867 if Nkind (True_Parent) = N_Package_Declaration
12868 and then
12869 Nkind (Original_Node (True_Parent)) = N_Package_Instantiation
12870 then
12871 -- Parent is a compilation unit that is an instantiation.
12872 -- Instantiation node has been replaced with package decl.
12873
12874 Inst_Node := Original_Node (True_Parent);
12875 exit;
12876
12877 elsif Nkind (True_Parent) = N_Package_Declaration
12878 and then Present (Generic_Parent (Specification (True_Parent)))
12879 and then Nkind (Parent (True_Parent)) /= N_Compilation_Unit
12880 then
12881 -- Parent is an instantiation within another specification.
12882 -- Declaration for instance has been inserted before original
12883 -- instantiation node. A direct link would be preferable?
12884
12885 Inst_Node := Next (True_Parent);
12886 while Present (Inst_Node)
12887 and then Nkind (Inst_Node) /= N_Package_Instantiation
12888 loop
12889 Next (Inst_Node);
12890 end loop;
12891
12892 -- If the instance appears within a generic, and the generic
12893 -- unit is defined within a formal package of the enclosing
12894 -- generic, there is no generic body available, and none
12895 -- needed. A more precise test should be used ???
12896
12897 if No (Inst_Node) then
12898 return;
12899 end if;
12900
12901 exit;
12902
12903 else
12904 True_Parent := Parent (True_Parent);
12905 end if;
12906 end loop;
12907
12908 -- Case where we are currently instantiating a nested generic
12909
12910 if Present (Inst_Node) then
12911 if Nkind (Parent (True_Parent)) = N_Compilation_Unit then
12912
12913 -- Instantiation node and declaration of instantiated package
12914 -- were exchanged when only the declaration was needed.
12915 -- Restore instantiation node before proceeding with body.
12916
12917 Set_Unit (Parent (True_Parent), Inst_Node);
12918 end if;
12919
12920 -- Now complete instantiation of enclosing body, if it appears in
12921 -- some other unit. If it appears in the current unit, the body
12922 -- will have been instantiated already.
12923
12924 if No (Corresponding_Body (Instance_Spec (Inst_Node))) then
12925
12926 -- We need to determine the expander mode to instantiate the
12927 -- enclosing body. Because the generic body we need may use
12928 -- global entities declared in the enclosing package (including
12929 -- aggregates) it is in general necessary to compile this body
12930 -- with expansion enabled, except if we are within a generic
12931 -- package, in which case the usual generic rule applies.
12932
12933 declare
12934 Exp_Status : Boolean := True;
12935 Scop : Entity_Id;
12936
12937 begin
12938 -- Loop through scopes looking for generic package
12939
12940 Scop := Scope (Defining_Entity (Instance_Spec (Inst_Node)));
12941 while Present (Scop)
12942 and then Scop /= Standard_Standard
12943 loop
12944 if Ekind (Scop) = E_Generic_Package then
12945 Exp_Status := False;
12946 exit;
12947 end if;
12948
12949 Scop := Scope (Scop);
12950 end loop;
12951
12952 -- Collect previous instantiations in the unit that contains
12953 -- the desired generic.
12954
12955 if Nkind (Parent (True_Parent)) /= N_Compilation_Unit
12956 and then not Body_Optional
12957 then
12958 declare
12959 Decl : Elmt_Id;
12960 Info : Pending_Body_Info;
12961 Par : Node_Id;
12962
12963 begin
12964 Par := Parent (Inst_Node);
12965 while Present (Par) loop
12966 exit when Nkind (Parent (Par)) = N_Compilation_Unit;
12967 Par := Parent (Par);
12968 end loop;
12969
12970 pragma Assert (Present (Par));
12971
12972 if Nkind (Par) = N_Package_Body then
12973 Collect_Previous_Instances (Declarations (Par));
12974
12975 elsif Nkind (Par) = N_Package_Declaration then
12976 Collect_Previous_Instances
12977 (Visible_Declarations (Specification (Par)));
12978 Collect_Previous_Instances
12979 (Private_Declarations (Specification (Par)));
12980
12981 else
12982 -- Enclosing unit is a subprogram body. In this
12983 -- case all instance bodies are processed in order
12984 -- and there is no need to collect them separately.
12985
12986 null;
12987 end if;
12988
12989 Decl := First_Elmt (Previous_Instances);
12990 while Present (Decl) loop
12991 Info :=
12992 (Inst_Node => Node (Decl),
12993 Act_Decl =>
12994 Instance_Spec (Node (Decl)),
12995 Expander_Status => Exp_Status,
12996 Current_Sem_Unit =>
12997 Get_Code_Unit (Sloc (Node (Decl))),
12998 Scope_Suppress => Scope_Suppress,
12999 Local_Suppress_Stack_Top =>
13000 Local_Suppress_Stack_Top,
13001 Version => Ada_Version,
13002 Version_Pragma => Ada_Version_Pragma,
13003 Warnings => Save_Warnings,
13004 SPARK_Mode => SPARK_Mode,
13005 SPARK_Mode_Pragma => SPARK_Mode_Pragma);
13006
13007 -- Package instance
13008
13009 if
13010 Nkind (Node (Decl)) = N_Package_Instantiation
13011 then
13012 Instantiate_Package_Body
13013 (Info, Body_Optional => True);
13014
13015 -- Subprogram instance
13016
13017 else
13018 -- The instance_spec is in the wrapper package,
13019 -- usually followed by its local renaming
13020 -- declaration. See Build_Subprogram_Renaming
13021 -- for details.
13022
13023 declare
13024 Decl : Node_Id :=
13025 (Last (Visible_Declarations
13026 (Specification (Info.Act_Decl))));
13027 begin
13028 if Nkind (Decl) =
13029 N_Subprogram_Renaming_Declaration
13030 then
13031 Decl := Prev (Decl);
13032 end if;
13033
13034 Info.Act_Decl := Decl;
13035 end;
13036
13037 Instantiate_Subprogram_Body
13038 (Info, Body_Optional => True);
13039 end if;
13040
13041 Next_Elmt (Decl);
13042 end loop;
13043 end;
13044 end if;
13045
13046 Instantiate_Package_Body
13047 (Body_Info =>
13048 ((Inst_Node => Inst_Node,
13049 Act_Decl => True_Parent,
13050 Expander_Status => Exp_Status,
13051 Current_Sem_Unit => Get_Code_Unit
13052 (Sloc (Inst_Node)),
13053 Scope_Suppress => Scope_Suppress,
13054 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
13055 Version => Ada_Version,
13056 Version_Pragma => Ada_Version_Pragma,
13057 Warnings => Save_Warnings,
13058 SPARK_Mode => SPARK_Mode,
13059 SPARK_Mode_Pragma => SPARK_Mode_Pragma)),
13060 Body_Optional => Body_Optional);
13061 end;
13062 end if;
13063
13064 -- Case where we are not instantiating a nested generic
13065
13066 else
13067 Opt.Style_Check := False;
13068 Expander_Mode_Save_And_Set (True);
13069 Load_Needed_Body (Comp_Unit, OK);
13070 Opt.Style_Check := Saved_Style_Check;
13071 Restore_Warnings (Saved_Warnings);
13072 Expander_Mode_Restore;
13073
13074 if not OK
13075 and then Unit_Requires_Body (Defining_Entity (Spec))
13076 and then not Body_Optional
13077 then
13078 declare
13079 Bname : constant Unit_Name_Type :=
13080 Get_Body_Name (Get_Unit_Name (Unit (Comp_Unit)));
13081
13082 begin
13083 -- In CodePeer mode, the missing body may make the analysis
13084 -- incomplete, but we do not treat it as fatal.
13085
13086 if CodePeer_Mode then
13087 return;
13088
13089 else
13090 Error_Msg_Unit_1 := Bname;
13091 Error_Msg_N ("this instantiation requires$!", N);
13092 Error_Msg_File_1 :=
13093 Get_File_Name (Bname, Subunit => False);
13094 Error_Msg_N ("\but file{ was not found!", N);
13095 raise Unrecoverable_Error;
13096 end if;
13097 end;
13098 end if;
13099 end if;
13100 end if;
13101
13102 -- If loading parent of the generic caused an instantiation circularity,
13103 -- we abandon compilation at this point, because otherwise in some cases
13104 -- we get into trouble with infinite recursions after this point.
13105
13106 if Circularity_Detected then
13107 raise Unrecoverable_Error;
13108 end if;
13109 end Load_Parent_Of_Generic;
13110
13111 ---------------------------------
13112 -- Map_Formal_Package_Entities --
13113 ---------------------------------
13114
13115 procedure Map_Formal_Package_Entities (Form : Entity_Id; Act : Entity_Id) is
13116 E1 : Entity_Id;
13117 E2 : Entity_Id;
13118
13119 begin
13120 Set_Instance_Of (Form, Act);
13121
13122 -- Traverse formal and actual package to map the corresponding entities.
13123 -- We skip over internal entities that may be generated during semantic
13124 -- analysis, and find the matching entities by name, given that they
13125 -- must appear in the same order.
13126
13127 E1 := First_Entity (Form);
13128 E2 := First_Entity (Act);
13129 while Present (E1) and then E1 /= First_Private_Entity (Form) loop
13130 -- Could this test be a single condition??? Seems like it could, and
13131 -- isn't FPE (Form) a constant anyway???
13132
13133 if not Is_Internal (E1)
13134 and then Present (Parent (E1))
13135 and then not Is_Class_Wide_Type (E1)
13136 and then not Is_Internal_Name (Chars (E1))
13137 then
13138 while Present (E2) and then Chars (E2) /= Chars (E1) loop
13139 Next_Entity (E2);
13140 end loop;
13141
13142 if No (E2) then
13143 exit;
13144 else
13145 Set_Instance_Of (E1, E2);
13146
13147 if Is_Type (E1) and then Is_Tagged_Type (E2) then
13148 Set_Instance_Of (Class_Wide_Type (E1), Class_Wide_Type (E2));
13149 end if;
13150
13151 if Is_Constrained (E1) then
13152 Set_Instance_Of (Base_Type (E1), Base_Type (E2));
13153 end if;
13154
13155 if Ekind (E1) = E_Package and then No (Renamed_Object (E1)) then
13156 Map_Formal_Package_Entities (E1, E2);
13157 end if;
13158 end if;
13159 end if;
13160
13161 Next_Entity (E1);
13162 end loop;
13163 end Map_Formal_Package_Entities;
13164
13165 -----------------------
13166 -- Move_Freeze_Nodes --
13167 -----------------------
13168
13169 procedure Move_Freeze_Nodes
13170 (Out_Of : Entity_Id;
13171 After : Node_Id;
13172 L : List_Id)
13173 is
13174 Decl : Node_Id;
13175 Next_Decl : Node_Id;
13176 Next_Node : Node_Id := After;
13177 Spec : Node_Id;
13178
13179 function Is_Outer_Type (T : Entity_Id) return Boolean;
13180 -- Check whether entity is declared in a scope external to that of the
13181 -- generic unit.
13182
13183 -------------------
13184 -- Is_Outer_Type --
13185 -------------------
13186
13187 function Is_Outer_Type (T : Entity_Id) return Boolean is
13188 Scop : Entity_Id := Scope (T);
13189
13190 begin
13191 if Scope_Depth (Scop) < Scope_Depth (Out_Of) then
13192 return True;
13193
13194 else
13195 while Scop /= Standard_Standard loop
13196 if Scop = Out_Of then
13197 return False;
13198 else
13199 Scop := Scope (Scop);
13200 end if;
13201 end loop;
13202
13203 return True;
13204 end if;
13205 end Is_Outer_Type;
13206
13207 -- Start of processing for Move_Freeze_Nodes
13208
13209 begin
13210 if No (L) then
13211 return;
13212 end if;
13213
13214 -- First remove the freeze nodes that may appear before all other
13215 -- declarations.
13216
13217 Decl := First (L);
13218 while Present (Decl)
13219 and then Nkind (Decl) = N_Freeze_Entity
13220 and then Is_Outer_Type (Entity (Decl))
13221 loop
13222 Decl := Remove_Head (L);
13223 Insert_After (Next_Node, Decl);
13224 Set_Analyzed (Decl, False);
13225 Next_Node := Decl;
13226 Decl := First (L);
13227 end loop;
13228
13229 -- Next scan the list of declarations and remove each freeze node that
13230 -- appears ahead of the current node.
13231
13232 while Present (Decl) loop
13233 while Present (Next (Decl))
13234 and then Nkind (Next (Decl)) = N_Freeze_Entity
13235 and then Is_Outer_Type (Entity (Next (Decl)))
13236 loop
13237 Next_Decl := Remove_Next (Decl);
13238 Insert_After (Next_Node, Next_Decl);
13239 Set_Analyzed (Next_Decl, False);
13240 Next_Node := Next_Decl;
13241 end loop;
13242
13243 -- If the declaration is a nested package or concurrent type, then
13244 -- recurse. Nested generic packages will have been processed from the
13245 -- inside out.
13246
13247 case Nkind (Decl) is
13248 when N_Package_Declaration =>
13249 Spec := Specification (Decl);
13250
13251 when N_Task_Type_Declaration =>
13252 Spec := Task_Definition (Decl);
13253
13254 when N_Protected_Type_Declaration =>
13255 Spec := Protected_Definition (Decl);
13256
13257 when others =>
13258 Spec := Empty;
13259 end case;
13260
13261 if Present (Spec) then
13262 Move_Freeze_Nodes (Out_Of, Next_Node, Visible_Declarations (Spec));
13263 Move_Freeze_Nodes (Out_Of, Next_Node, Private_Declarations (Spec));
13264 end if;
13265
13266 Next (Decl);
13267 end loop;
13268 end Move_Freeze_Nodes;
13269
13270 ----------------
13271 -- Next_Assoc --
13272 ----------------
13273
13274 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr is
13275 begin
13276 return Generic_Renamings.Table (E).Next_In_HTable;
13277 end Next_Assoc;
13278
13279 ------------------------
13280 -- Preanalyze_Actuals --
13281 ------------------------
13282
13283 procedure Preanalyze_Actuals (N : Node_Id; Inst : Entity_Id := Empty) is
13284 Assoc : Node_Id;
13285 Act : Node_Id;
13286 Errs : constant Int := Serious_Errors_Detected;
13287
13288 Cur : Entity_Id := Empty;
13289 -- Current homograph of the instance name
13290
13291 Vis : Boolean;
13292 -- Saved visibility status of the current homograph
13293
13294 begin
13295 Assoc := First (Generic_Associations (N));
13296
13297 -- If the instance is a child unit, its name may hide an outer homonym,
13298 -- so make it invisible to perform name resolution on the actuals.
13299
13300 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name
13301 and then Present
13302 (Current_Entity (Defining_Identifier (Defining_Unit_Name (N))))
13303 then
13304 Cur := Current_Entity (Defining_Identifier (Defining_Unit_Name (N)));
13305
13306 if Is_Compilation_Unit (Cur) then
13307 Vis := Is_Immediately_Visible (Cur);
13308 Set_Is_Immediately_Visible (Cur, False);
13309 else
13310 Cur := Empty;
13311 end if;
13312 end if;
13313
13314 while Present (Assoc) loop
13315 if Nkind (Assoc) /= N_Others_Choice then
13316 Act := Explicit_Generic_Actual_Parameter (Assoc);
13317
13318 -- Within a nested instantiation, a defaulted actual is an empty
13319 -- association, so nothing to analyze. If the subprogram actual
13320 -- is an attribute, analyze prefix only, because actual is not a
13321 -- complete attribute reference.
13322
13323 -- If actual is an allocator, analyze expression only. The full
13324 -- analysis can generate code, and if instance is a compilation
13325 -- unit we have to wait until the package instance is installed
13326 -- to have a proper place to insert this code.
13327
13328 -- String literals may be operators, but at this point we do not
13329 -- know whether the actual is a formal subprogram or a string.
13330
13331 if No (Act) then
13332 null;
13333
13334 elsif Nkind (Act) = N_Attribute_Reference then
13335 Analyze (Prefix (Act));
13336
13337 elsif Nkind (Act) = N_Explicit_Dereference then
13338 Analyze (Prefix (Act));
13339
13340 elsif Nkind (Act) = N_Allocator then
13341 declare
13342 Expr : constant Node_Id := Expression (Act);
13343
13344 begin
13345 if Nkind (Expr) = N_Subtype_Indication then
13346 Analyze (Subtype_Mark (Expr));
13347
13348 -- Analyze separately each discriminant constraint, when
13349 -- given with a named association.
13350
13351 declare
13352 Constr : Node_Id;
13353
13354 begin
13355 Constr := First (Constraints (Constraint (Expr)));
13356 while Present (Constr) loop
13357 if Nkind (Constr) = N_Discriminant_Association then
13358 Analyze (Expression (Constr));
13359 else
13360 Analyze (Constr);
13361 end if;
13362
13363 Next (Constr);
13364 end loop;
13365 end;
13366
13367 else
13368 Analyze (Expr);
13369 end if;
13370 end;
13371
13372 elsif Nkind (Act) /= N_Operator_Symbol then
13373 Analyze (Act);
13374
13375 if Is_Entity_Name (Act)
13376 and then Is_Type (Entity (Act))
13377 and then From_Limited_With (Entity (Act))
13378 then
13379 Append_Elmt (Entity (Act), Incomplete_Actuals (Inst));
13380 end if;
13381 end if;
13382
13383 if Errs /= Serious_Errors_Detected then
13384
13385 -- Do a minimal analysis of the generic, to prevent spurious
13386 -- warnings complaining about the generic being unreferenced,
13387 -- before abandoning the instantiation.
13388
13389 Analyze (Name (N));
13390
13391 if Is_Entity_Name (Name (N))
13392 and then Etype (Name (N)) /= Any_Type
13393 then
13394 Generate_Reference (Entity (Name (N)), Name (N));
13395 Set_Is_Instantiated (Entity (Name (N)));
13396 end if;
13397
13398 if Present (Cur) then
13399
13400 -- For the case of a child instance hiding an outer homonym,
13401 -- provide additional warning which might explain the error.
13402
13403 Set_Is_Immediately_Visible (Cur, Vis);
13404 Error_Msg_NE
13405 ("& hides outer unit with the same name??",
13406 N, Defining_Unit_Name (N));
13407 end if;
13408
13409 Abandon_Instantiation (Act);
13410 end if;
13411 end if;
13412
13413 Next (Assoc);
13414 end loop;
13415
13416 if Present (Cur) then
13417 Set_Is_Immediately_Visible (Cur, Vis);
13418 end if;
13419 end Preanalyze_Actuals;
13420
13421 -------------------
13422 -- Remove_Parent --
13423 -------------------
13424
13425 procedure Remove_Parent (In_Body : Boolean := False) is
13426 S : Entity_Id := Current_Scope;
13427 -- S is the scope containing the instantiation just completed. The scope
13428 -- stack contains the parent instances of the instantiation, followed by
13429 -- the original S.
13430
13431 Cur_P : Entity_Id;
13432 E : Entity_Id;
13433 P : Entity_Id;
13434 Hidden : Elmt_Id;
13435
13436 begin
13437 -- After child instantiation is complete, remove from scope stack the
13438 -- extra copy of the current scope, and then remove parent instances.
13439
13440 if not In_Body then
13441 Pop_Scope;
13442
13443 while Current_Scope /= S loop
13444 P := Current_Scope;
13445 End_Package_Scope (Current_Scope);
13446
13447 if In_Open_Scopes (P) then
13448 E := First_Entity (P);
13449 while Present (E) loop
13450 Set_Is_Immediately_Visible (E, True);
13451 Next_Entity (E);
13452 end loop;
13453
13454 -- If instantiation is declared in a block, it is the enclosing
13455 -- scope that might be a parent instance. Note that only one
13456 -- block can be involved, because the parent instances have
13457 -- been installed within it.
13458
13459 if Ekind (P) = E_Block then
13460 Cur_P := Scope (P);
13461 else
13462 Cur_P := P;
13463 end if;
13464
13465 if Is_Generic_Instance (Cur_P) and then P /= Current_Scope then
13466 -- We are within an instance of some sibling. Retain
13467 -- visibility of parent, for proper subsequent cleanup, and
13468 -- reinstall private declarations as well.
13469
13470 Set_In_Private_Part (P);
13471 Install_Private_Declarations (P);
13472 end if;
13473
13474 -- If the ultimate parent is a top-level unit recorded in
13475 -- Instance_Parent_Unit, then reset its visibility to what it was
13476 -- before instantiation. (It's not clear what the purpose is of
13477 -- testing whether Scope (P) is In_Open_Scopes, but that test was
13478 -- present before the ultimate parent test was added.???)
13479
13480 elsif not In_Open_Scopes (Scope (P))
13481 or else (P = Instance_Parent_Unit
13482 and then not Parent_Unit_Visible)
13483 then
13484 Set_Is_Immediately_Visible (P, False);
13485
13486 -- If the current scope is itself an instantiation of a generic
13487 -- nested within P, and we are in the private part of body of this
13488 -- instantiation, restore the full views of P, that were removed
13489 -- in End_Package_Scope above. This obscure case can occur when a
13490 -- subunit of a generic contains an instance of a child unit of
13491 -- its generic parent unit.
13492
13493 elsif S = Current_Scope and then Is_Generic_Instance (S) then
13494 declare
13495 Par : constant Entity_Id :=
13496 Generic_Parent (Package_Specification (S));
13497 begin
13498 if Present (Par)
13499 and then P = Scope (Par)
13500 and then (In_Package_Body (S) or else In_Private_Part (S))
13501 then
13502 Set_In_Private_Part (P);
13503 Install_Private_Declarations (P);
13504 end if;
13505 end;
13506 end if;
13507 end loop;
13508
13509 -- Reset visibility of entities in the enclosing scope
13510
13511 Set_Is_Hidden_Open_Scope (Current_Scope, False);
13512
13513 Hidden := First_Elmt (Hidden_Entities);
13514 while Present (Hidden) loop
13515 Set_Is_Immediately_Visible (Node (Hidden), True);
13516 Next_Elmt (Hidden);
13517 end loop;
13518
13519 else
13520 -- Each body is analyzed separately, and there is no context that
13521 -- needs preserving from one body instance to the next, so remove all
13522 -- parent scopes that have been installed.
13523
13524 while Present (S) loop
13525 End_Package_Scope (S);
13526 Set_Is_Immediately_Visible (S, False);
13527 S := Current_Scope;
13528 exit when S = Standard_Standard;
13529 end loop;
13530 end if;
13531 end Remove_Parent;
13532
13533 -----------------
13534 -- Restore_Env --
13535 -----------------
13536
13537 procedure Restore_Env is
13538 Saved : Instance_Env renames Instance_Envs.Table (Instance_Envs.Last);
13539
13540 begin
13541 if No (Current_Instantiated_Parent.Act_Id) then
13542 -- Restore environment after subprogram inlining
13543
13544 Restore_Private_Views (Empty);
13545 end if;
13546
13547 Current_Instantiated_Parent := Saved.Instantiated_Parent;
13548 Exchanged_Views := Saved.Exchanged_Views;
13549 Hidden_Entities := Saved.Hidden_Entities;
13550 Current_Sem_Unit := Saved.Current_Sem_Unit;
13551 Parent_Unit_Visible := Saved.Parent_Unit_Visible;
13552 Instance_Parent_Unit := Saved.Instance_Parent_Unit;
13553
13554 Restore_Opt_Config_Switches (Saved.Switches);
13555
13556 Instance_Envs.Decrement_Last;
13557 end Restore_Env;
13558
13559 ---------------------------
13560 -- Restore_Private_Views --
13561 ---------------------------
13562
13563 procedure Restore_Private_Views
13564 (Pack_Id : Entity_Id;
13565 Is_Package : Boolean := True)
13566 is
13567 M : Elmt_Id;
13568 E : Entity_Id;
13569 Typ : Entity_Id;
13570 Dep_Elmt : Elmt_Id;
13571 Dep_Typ : Node_Id;
13572
13573 procedure Restore_Nested_Formal (Formal : Entity_Id);
13574 -- Hide the generic formals of formal packages declared with box which
13575 -- were reachable in the current instantiation.
13576
13577 ---------------------------
13578 -- Restore_Nested_Formal --
13579 ---------------------------
13580
13581 procedure Restore_Nested_Formal (Formal : Entity_Id) is
13582 Ent : Entity_Id;
13583
13584 begin
13585 if Present (Renamed_Object (Formal))
13586 and then Denotes_Formal_Package (Renamed_Object (Formal), True)
13587 then
13588 return;
13589
13590 elsif Present (Associated_Formal_Package (Formal)) then
13591 Ent := First_Entity (Formal);
13592 while Present (Ent) loop
13593 exit when Ekind (Ent) = E_Package
13594 and then Renamed_Entity (Ent) = Renamed_Entity (Formal);
13595
13596 Set_Is_Hidden (Ent);
13597 Set_Is_Potentially_Use_Visible (Ent, False);
13598
13599 -- If package, then recurse
13600
13601 if Ekind (Ent) = E_Package then
13602 Restore_Nested_Formal (Ent);
13603 end if;
13604
13605 Next_Entity (Ent);
13606 end loop;
13607 end if;
13608 end Restore_Nested_Formal;
13609
13610 -- Start of processing for Restore_Private_Views
13611
13612 begin
13613 M := First_Elmt (Exchanged_Views);
13614 while Present (M) loop
13615 Typ := Node (M);
13616
13617 -- Subtypes of types whose views have been exchanged, and that are
13618 -- defined within the instance, were not on the Private_Dependents
13619 -- list on entry to the instance, so they have to be exchanged
13620 -- explicitly now, in order to remain consistent with the view of the
13621 -- parent type.
13622
13623 if Ekind_In (Typ, E_Private_Type,
13624 E_Limited_Private_Type,
13625 E_Record_Type_With_Private)
13626 then
13627 Dep_Elmt := First_Elmt (Private_Dependents (Typ));
13628 while Present (Dep_Elmt) loop
13629 Dep_Typ := Node (Dep_Elmt);
13630
13631 if Scope (Dep_Typ) = Pack_Id
13632 and then Present (Full_View (Dep_Typ))
13633 then
13634 Replace_Elmt (Dep_Elmt, Full_View (Dep_Typ));
13635 Exchange_Declarations (Dep_Typ);
13636 end if;
13637
13638 Next_Elmt (Dep_Elmt);
13639 end loop;
13640 end if;
13641
13642 Exchange_Declarations (Node (M));
13643 Next_Elmt (M);
13644 end loop;
13645
13646 if No (Pack_Id) then
13647 return;
13648 end if;
13649
13650 -- Make the generic formal parameters private, and make the formal types
13651 -- into subtypes of the actuals again.
13652
13653 E := First_Entity (Pack_Id);
13654 while Present (E) loop
13655 Set_Is_Hidden (E, True);
13656
13657 if Is_Type (E)
13658 and then Nkind (Parent (E)) = N_Subtype_Declaration
13659 then
13660 -- If the actual for E is itself a generic actual type from
13661 -- an enclosing instance, E is still a generic actual type
13662 -- outside of the current instance. This matter when resolving
13663 -- an overloaded call that may be ambiguous in the enclosing
13664 -- instance, when two of its actuals coincide.
13665
13666 if Is_Entity_Name (Subtype_Indication (Parent (E)))
13667 and then Is_Generic_Actual_Type
13668 (Entity (Subtype_Indication (Parent (E))))
13669 then
13670 null;
13671 else
13672 Set_Is_Generic_Actual_Type (E, False);
13673 end if;
13674
13675 -- An unusual case of aliasing: the actual may also be directly
13676 -- visible in the generic, and be private there, while it is fully
13677 -- visible in the context of the instance. The internal subtype
13678 -- is private in the instance but has full visibility like its
13679 -- parent in the enclosing scope. This enforces the invariant that
13680 -- the privacy status of all private dependents of a type coincide
13681 -- with that of the parent type. This can only happen when a
13682 -- generic child unit is instantiated within a sibling.
13683
13684 if Is_Private_Type (E)
13685 and then not Is_Private_Type (Etype (E))
13686 then
13687 Exchange_Declarations (E);
13688 end if;
13689
13690 elsif Ekind (E) = E_Package then
13691
13692 -- The end of the renaming list is the renaming of the generic
13693 -- package itself. If the instance is a subprogram, all entities
13694 -- in the corresponding package are renamings. If this entity is
13695 -- a formal package, make its own formals private as well. The
13696 -- actual in this case is itself the renaming of an instantiation.
13697 -- If the entity is not a package renaming, it is the entity
13698 -- created to validate formal package actuals: ignore it.
13699
13700 -- If the actual is itself a formal package for the enclosing
13701 -- generic, or the actual for such a formal package, it remains
13702 -- visible on exit from the instance, and therefore nothing needs
13703 -- to be done either, except to keep it accessible.
13704
13705 if Is_Package and then Renamed_Object (E) = Pack_Id then
13706 exit;
13707
13708 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
13709 null;
13710
13711 elsif
13712 Denotes_Formal_Package (Renamed_Object (E), True, Pack_Id)
13713 then
13714 Set_Is_Hidden (E, False);
13715
13716 else
13717 declare
13718 Act_P : constant Entity_Id := Renamed_Object (E);
13719 Id : Entity_Id;
13720
13721 begin
13722 Id := First_Entity (Act_P);
13723 while Present (Id)
13724 and then Id /= First_Private_Entity (Act_P)
13725 loop
13726 exit when Ekind (Id) = E_Package
13727 and then Renamed_Object (Id) = Act_P;
13728
13729 Set_Is_Hidden (Id, True);
13730 Set_Is_Potentially_Use_Visible (Id, In_Use (Act_P));
13731
13732 if Ekind (Id) = E_Package then
13733 Restore_Nested_Formal (Id);
13734 end if;
13735
13736 Next_Entity (Id);
13737 end loop;
13738 end;
13739 end if;
13740 end if;
13741
13742 Next_Entity (E);
13743 end loop;
13744 end Restore_Private_Views;
13745
13746 --------------
13747 -- Save_Env --
13748 --------------
13749
13750 procedure Save_Env
13751 (Gen_Unit : Entity_Id;
13752 Act_Unit : Entity_Id)
13753 is
13754 begin
13755 Init_Env;
13756 Set_Instance_Env (Gen_Unit, Act_Unit);
13757 end Save_Env;
13758
13759 ----------------------------
13760 -- Save_Global_References --
13761 ----------------------------
13762
13763 procedure Save_Global_References (Templ : Node_Id) is
13764
13765 -- ??? it is horrible to use global variables in highly recursive code
13766
13767 E : Entity_Id;
13768 -- The entity of the current associated node
13769
13770 Gen_Scope : Entity_Id;
13771 -- The scope of the generic for which references are being saved
13772
13773 N2 : Node_Id;
13774 -- The current associated node
13775
13776 function Is_Global (E : Entity_Id) return Boolean;
13777 -- Check whether entity is defined outside of generic unit. Examine the
13778 -- scope of an entity, and the scope of the scope, etc, until we find
13779 -- either Standard, in which case the entity is global, or the generic
13780 -- unit itself, which indicates that the entity is local. If the entity
13781 -- is the generic unit itself, as in the case of a recursive call, or
13782 -- the enclosing generic unit, if different from the current scope, then
13783 -- it is local as well, because it will be replaced at the point of
13784 -- instantiation. On the other hand, if it is a reference to a child
13785 -- unit of a common ancestor, which appears in an instantiation, it is
13786 -- global because it is used to denote a specific compilation unit at
13787 -- the time the instantiations will be analyzed.
13788
13789 procedure Reset_Entity (N : Node_Id);
13790 -- Save semantic information on global entity so that it is not resolved
13791 -- again at instantiation time.
13792
13793 procedure Save_Entity_Descendants (N : Node_Id);
13794 -- Apply Save_Global_References to the two syntactic descendants of
13795 -- non-terminal nodes that carry an Associated_Node and are processed
13796 -- through Reset_Entity. Once the global entity (if any) has been
13797 -- captured together with its type, only two syntactic descendants need
13798 -- to be traversed to complete the processing of the tree rooted at N.
13799 -- This applies to Selected_Components, Expanded_Names, and to Operator
13800 -- nodes. N can also be a character literal, identifier, or operator
13801 -- symbol node, but the call has no effect in these cases.
13802
13803 procedure Save_Global_Defaults (N1 : Node_Id; N2 : Node_Id);
13804 -- Default actuals in nested instances must be handled specially
13805 -- because there is no link to them from the original tree. When an
13806 -- actual subprogram is given by a default, we add an explicit generic
13807 -- association for it in the instantiation node. When we save the
13808 -- global references on the name of the instance, we recover the list
13809 -- of generic associations, and add an explicit one to the original
13810 -- generic tree, through which a global actual can be preserved.
13811 -- Similarly, if a child unit is instantiated within a sibling, in the
13812 -- context of the parent, we must preserve the identifier of the parent
13813 -- so that it can be properly resolved in a subsequent instantiation.
13814
13815 procedure Save_Global_Descendant (D : Union_Id);
13816 -- Apply Save_References recursively to the descendents of node D
13817
13818 procedure Save_References (N : Node_Id);
13819 -- This is the recursive procedure that does the work, once the
13820 -- enclosing generic scope has been established.
13821
13822 ---------------
13823 -- Is_Global --
13824 ---------------
13825
13826 function Is_Global (E : Entity_Id) return Boolean is
13827 Se : Entity_Id;
13828
13829 function Is_Instance_Node (Decl : Node_Id) return Boolean;
13830 -- Determine whether the parent node of a reference to a child unit
13831 -- denotes an instantiation or a formal package, in which case the
13832 -- reference to the child unit is global, even if it appears within
13833 -- the current scope (e.g. when the instance appears within the body
13834 -- of an ancestor).
13835
13836 ----------------------
13837 -- Is_Instance_Node --
13838 ----------------------
13839
13840 function Is_Instance_Node (Decl : Node_Id) return Boolean is
13841 begin
13842 return Nkind (Decl) in N_Generic_Instantiation
13843 or else
13844 Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration;
13845 end Is_Instance_Node;
13846
13847 -- Start of processing for Is_Global
13848
13849 begin
13850 if E = Gen_Scope then
13851 return False;
13852
13853 elsif E = Standard_Standard then
13854 return True;
13855
13856 elsif Is_Child_Unit (E)
13857 and then (Is_Instance_Node (Parent (N2))
13858 or else (Nkind (Parent (N2)) = N_Expanded_Name
13859 and then N2 = Selector_Name (Parent (N2))
13860 and then
13861 Is_Instance_Node (Parent (Parent (N2)))))
13862 then
13863 return True;
13864
13865 else
13866 Se := Scope (E);
13867 while Se /= Gen_Scope loop
13868 if Se = Standard_Standard then
13869 return True;
13870 else
13871 Se := Scope (Se);
13872 end if;
13873 end loop;
13874
13875 return False;
13876 end if;
13877 end Is_Global;
13878
13879 ------------------
13880 -- Reset_Entity --
13881 ------------------
13882
13883 procedure Reset_Entity (N : Node_Id) is
13884 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id);
13885 -- If the type of N2 is global to the generic unit, save the type in
13886 -- the generic node. Just as we perform name capture for explicit
13887 -- references within the generic, we must capture the global types
13888 -- of local entities because they may participate in resolution in
13889 -- the instance.
13890
13891 function Top_Ancestor (E : Entity_Id) return Entity_Id;
13892 -- Find the ultimate ancestor of the current unit. If it is not a
13893 -- generic unit, then the name of the current unit in the prefix of
13894 -- an expanded name must be replaced with its generic homonym to
13895 -- ensure that it will be properly resolved in an instance.
13896
13897 ---------------------
13898 -- Set_Global_Type --
13899 ---------------------
13900
13901 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id) is
13902 Typ : constant Entity_Id := Etype (N2);
13903
13904 begin
13905 Set_Etype (N, Typ);
13906
13907 -- If the entity of N is not the associated node, this is a
13908 -- nested generic and it has an associated node as well, whose
13909 -- type is already the full view (see below). Indicate that the
13910 -- original node has a private view.
13911
13912 if Entity (N) /= N2 and then Has_Private_View (Entity (N)) then
13913 Set_Has_Private_View (N);
13914 end if;
13915
13916 -- If not a private type, nothing else to do
13917
13918 if not Is_Private_Type (Typ) then
13919 if Is_Array_Type (Typ)
13920 and then Is_Private_Type (Component_Type (Typ))
13921 then
13922 Set_Has_Private_View (N);
13923 end if;
13924
13925 -- If it is a derivation of a private type in a context where no
13926 -- full view is needed, nothing to do either.
13927
13928 elsif No (Full_View (Typ)) and then Typ /= Etype (Typ) then
13929 null;
13930
13931 -- Otherwise mark the type for flipping and use the full view when
13932 -- available.
13933
13934 else
13935 Set_Has_Private_View (N);
13936
13937 if Present (Full_View (Typ)) then
13938 Set_Etype (N2, Full_View (Typ));
13939 end if;
13940 end if;
13941 end Set_Global_Type;
13942
13943 ------------------
13944 -- Top_Ancestor --
13945 ------------------
13946
13947 function Top_Ancestor (E : Entity_Id) return Entity_Id is
13948 Par : Entity_Id;
13949
13950 begin
13951 Par := E;
13952 while Is_Child_Unit (Par) loop
13953 Par := Scope (Par);
13954 end loop;
13955
13956 return Par;
13957 end Top_Ancestor;
13958
13959 -- Start of processing for Reset_Entity
13960
13961 begin
13962 N2 := Get_Associated_Node (N);
13963 E := Entity (N2);
13964
13965 if Present (E) then
13966
13967 -- If the node is an entry call to an entry in an enclosing task,
13968 -- it is rewritten as a selected component. No global entity to
13969 -- preserve in this case, since the expansion will be redone in
13970 -- the instance.
13971
13972 if not Nkind_In (E, N_Defining_Character_Literal,
13973 N_Defining_Identifier,
13974 N_Defining_Operator_Symbol)
13975 then
13976 Set_Associated_Node (N, Empty);
13977 Set_Etype (N, Empty);
13978 return;
13979 end if;
13980
13981 -- If the entity is an itype created as a subtype of an access
13982 -- type with a null exclusion restore source entity for proper
13983 -- visibility. The itype will be created anew in the instance.
13984
13985 if Is_Itype (E)
13986 and then Ekind (E) = E_Access_Subtype
13987 and then Is_Entity_Name (N)
13988 and then Chars (Etype (E)) = Chars (N)
13989 then
13990 E := Etype (E);
13991 Set_Entity (N2, E);
13992 Set_Etype (N2, E);
13993 end if;
13994
13995 if Is_Global (E) then
13996
13997 -- If the entity is a package renaming that is the prefix of
13998 -- an expanded name, it has been rewritten as the renamed
13999 -- package, which is necessary semantically but complicates
14000 -- ASIS tree traversal, so we recover the original entity to
14001 -- expose the renaming. Take into account that the context may
14002 -- be a nested generic, that the original node may itself have
14003 -- an associated node that had better be an entity, and that
14004 -- the current node is still a selected component.
14005
14006 if Ekind (E) = E_Package
14007 and then Nkind (N) = N_Selected_Component
14008 and then Nkind (Parent (N)) = N_Expanded_Name
14009 and then Present (Original_Node (N2))
14010 and then Is_Entity_Name (Original_Node (N2))
14011 and then Present (Entity (Original_Node (N2)))
14012 then
14013 if Is_Global (Entity (Original_Node (N2))) then
14014 N2 := Original_Node (N2);
14015 Set_Associated_Node (N, N2);
14016 Set_Global_Type (N, N2);
14017
14018 -- Renaming is local, and will be resolved in instance
14019
14020 else
14021 Set_Associated_Node (N, Empty);
14022 Set_Etype (N, Empty);
14023 end if;
14024
14025 else
14026 Set_Global_Type (N, N2);
14027 end if;
14028
14029 elsif Nkind (N) = N_Op_Concat
14030 and then Is_Generic_Type (Etype (N2))
14031 and then (Base_Type (Etype (Right_Opnd (N2))) = Etype (N2)
14032 or else
14033 Base_Type (Etype (Left_Opnd (N2))) = Etype (N2))
14034 and then Is_Intrinsic_Subprogram (E)
14035 then
14036 null;
14037
14038 -- Entity is local. Mark generic node as unresolved. Note that now
14039 -- it does not have an entity.
14040
14041 else
14042 Set_Associated_Node (N, Empty);
14043 Set_Etype (N, Empty);
14044 end if;
14045
14046 if Nkind (Parent (N)) in N_Generic_Instantiation
14047 and then N = Name (Parent (N))
14048 then
14049 Save_Global_Defaults (Parent (N), Parent (N2));
14050 end if;
14051
14052 elsif Nkind (Parent (N)) = N_Selected_Component
14053 and then Nkind (Parent (N2)) = N_Expanded_Name
14054 then
14055 if Is_Global (Entity (Parent (N2))) then
14056 Change_Selected_Component_To_Expanded_Name (Parent (N));
14057 Set_Associated_Node (Parent (N), Parent (N2));
14058 Set_Global_Type (Parent (N), Parent (N2));
14059 Save_Entity_Descendants (N);
14060
14061 -- If this is a reference to the current generic entity, replace
14062 -- by the name of the generic homonym of the current package. This
14063 -- is because in an instantiation Par.P.Q will not resolve to the
14064 -- name of the instance, whose enclosing scope is not necessarily
14065 -- Par. We use the generic homonym rather that the name of the
14066 -- generic itself because it may be hidden by a local declaration.
14067
14068 elsif In_Open_Scopes (Entity (Parent (N2)))
14069 and then not
14070 Is_Generic_Unit (Top_Ancestor (Entity (Prefix (Parent (N2)))))
14071 then
14072 if Ekind (Entity (Parent (N2))) = E_Generic_Package then
14073 Rewrite (Parent (N),
14074 Make_Identifier (Sloc (N),
14075 Chars =>
14076 Chars (Generic_Homonym (Entity (Parent (N2))))));
14077 else
14078 Rewrite (Parent (N),
14079 Make_Identifier (Sloc (N),
14080 Chars => Chars (Selector_Name (Parent (N2)))));
14081 end if;
14082 end if;
14083
14084 if Nkind (Parent (Parent (N))) in N_Generic_Instantiation
14085 and then Parent (N) = Name (Parent (Parent (N)))
14086 then
14087 Save_Global_Defaults
14088 (Parent (Parent (N)), Parent (Parent (N2)));
14089 end if;
14090
14091 -- A selected component may denote a static constant that has been
14092 -- folded. If the static constant is global to the generic, capture
14093 -- its value. Otherwise the folding will happen in any instantiation.
14094
14095 elsif Nkind (Parent (N)) = N_Selected_Component
14096 and then Nkind_In (Parent (N2), N_Integer_Literal, N_Real_Literal)
14097 then
14098 if Present (Entity (Original_Node (Parent (N2))))
14099 and then Is_Global (Entity (Original_Node (Parent (N2))))
14100 then
14101 Rewrite (Parent (N), New_Copy (Parent (N2)));
14102 Set_Analyzed (Parent (N), False);
14103 end if;
14104
14105 -- A selected component may be transformed into a parameterless
14106 -- function call. If the called entity is global, rewrite the node
14107 -- appropriately, i.e. as an extended name for the global entity.
14108
14109 elsif Nkind (Parent (N)) = N_Selected_Component
14110 and then Nkind (Parent (N2)) = N_Function_Call
14111 and then N = Selector_Name (Parent (N))
14112 then
14113 if No (Parameter_Associations (Parent (N2))) then
14114 if Is_Global (Entity (Name (Parent (N2)))) then
14115 Change_Selected_Component_To_Expanded_Name (Parent (N));
14116 Set_Associated_Node (Parent (N), Name (Parent (N2)));
14117 Set_Global_Type (Parent (N), Name (Parent (N2)));
14118 Save_Entity_Descendants (N);
14119
14120 else
14121 Set_Is_Prefixed_Call (Parent (N));
14122 Set_Associated_Node (N, Empty);
14123 Set_Etype (N, Empty);
14124 end if;
14125
14126 -- In Ada 2005, X.F may be a call to a primitive operation,
14127 -- rewritten as F (X). This rewriting will be done again in an
14128 -- instance, so keep the original node. Global entities will be
14129 -- captured as for other constructs. Indicate that this must
14130 -- resolve as a call, to prevent accidental overloading in the
14131 -- instance, if both a component and a primitive operation appear
14132 -- as candidates.
14133
14134 else
14135 Set_Is_Prefixed_Call (Parent (N));
14136 end if;
14137
14138 -- Entity is local. Reset in generic unit, so that node is resolved
14139 -- anew at the point of instantiation.
14140
14141 else
14142 Set_Associated_Node (N, Empty);
14143 Set_Etype (N, Empty);
14144 end if;
14145 end Reset_Entity;
14146
14147 -----------------------------
14148 -- Save_Entity_Descendants --
14149 -----------------------------
14150
14151 procedure Save_Entity_Descendants (N : Node_Id) is
14152 begin
14153 case Nkind (N) is
14154 when N_Binary_Op =>
14155 Save_Global_Descendant (Union_Id (Left_Opnd (N)));
14156 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
14157
14158 when N_Unary_Op =>
14159 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
14160
14161 when N_Expanded_Name |
14162 N_Selected_Component =>
14163 Save_Global_Descendant (Union_Id (Prefix (N)));
14164 Save_Global_Descendant (Union_Id (Selector_Name (N)));
14165
14166 when N_Identifier |
14167 N_Character_Literal |
14168 N_Operator_Symbol =>
14169 null;
14170
14171 when others =>
14172 raise Program_Error;
14173 end case;
14174 end Save_Entity_Descendants;
14175
14176 --------------------------
14177 -- Save_Global_Defaults --
14178 --------------------------
14179
14180 procedure Save_Global_Defaults (N1 : Node_Id; N2 : Node_Id) is
14181 Loc : constant Source_Ptr := Sloc (N1);
14182 Assoc2 : constant List_Id := Generic_Associations (N2);
14183 Gen_Id : constant Entity_Id := Get_Generic_Entity (N2);
14184 Assoc1 : List_Id;
14185 Act1 : Node_Id;
14186 Act2 : Node_Id;
14187 Def : Node_Id;
14188 Ndec : Node_Id;
14189 Subp : Entity_Id;
14190 Actual : Entity_Id;
14191
14192 begin
14193 Assoc1 := Generic_Associations (N1);
14194
14195 if Present (Assoc1) then
14196 Act1 := First (Assoc1);
14197 else
14198 Act1 := Empty;
14199 Set_Generic_Associations (N1, New_List);
14200 Assoc1 := Generic_Associations (N1);
14201 end if;
14202
14203 if Present (Assoc2) then
14204 Act2 := First (Assoc2);
14205 else
14206 return;
14207 end if;
14208
14209 while Present (Act1) and then Present (Act2) loop
14210 Next (Act1);
14211 Next (Act2);
14212 end loop;
14213
14214 -- Find the associations added for default subprograms
14215
14216 if Present (Act2) then
14217 while Nkind (Act2) /= N_Generic_Association
14218 or else No (Entity (Selector_Name (Act2)))
14219 or else not Is_Overloadable (Entity (Selector_Name (Act2)))
14220 loop
14221 Next (Act2);
14222 end loop;
14223
14224 -- Add a similar association if the default is global. The
14225 -- renaming declaration for the actual has been analyzed, and
14226 -- its alias is the program it renames. Link the actual in the
14227 -- original generic tree with the node in the analyzed tree.
14228
14229 while Present (Act2) loop
14230 Subp := Entity (Selector_Name (Act2));
14231 Def := Explicit_Generic_Actual_Parameter (Act2);
14232
14233 -- Following test is defence against rubbish errors
14234
14235 if No (Alias (Subp)) then
14236 return;
14237 end if;
14238
14239 -- Retrieve the resolved actual from the renaming declaration
14240 -- created for the instantiated formal.
14241
14242 Actual := Entity (Name (Parent (Parent (Subp))));
14243 Set_Entity (Def, Actual);
14244 Set_Etype (Def, Etype (Actual));
14245
14246 if Is_Global (Actual) then
14247 Ndec :=
14248 Make_Generic_Association (Loc,
14249 Selector_Name =>
14250 New_Occurrence_Of (Subp, Loc),
14251 Explicit_Generic_Actual_Parameter =>
14252 New_Occurrence_Of (Actual, Loc));
14253
14254 Set_Associated_Node
14255 (Explicit_Generic_Actual_Parameter (Ndec), Def);
14256
14257 Append (Ndec, Assoc1);
14258
14259 -- If there are other defaults, add a dummy association in case
14260 -- there are other defaulted formals with the same name.
14261
14262 elsif Present (Next (Act2)) then
14263 Ndec :=
14264 Make_Generic_Association (Loc,
14265 Selector_Name =>
14266 New_Occurrence_Of (Subp, Loc),
14267 Explicit_Generic_Actual_Parameter => Empty);
14268
14269 Append (Ndec, Assoc1);
14270 end if;
14271
14272 Next (Act2);
14273 end loop;
14274 end if;
14275
14276 if Nkind (Name (N1)) = N_Identifier
14277 and then Is_Child_Unit (Gen_Id)
14278 and then Is_Global (Gen_Id)
14279 and then Is_Generic_Unit (Scope (Gen_Id))
14280 and then In_Open_Scopes (Scope (Gen_Id))
14281 then
14282 -- This is an instantiation of a child unit within a sibling, so
14283 -- that the generic parent is in scope. An eventual instance must
14284 -- occur within the scope of an instance of the parent. Make name
14285 -- in instance into an expanded name, to preserve the identifier
14286 -- of the parent, so it can be resolved subsequently.
14287
14288 Rewrite (Name (N2),
14289 Make_Expanded_Name (Loc,
14290 Chars => Chars (Gen_Id),
14291 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
14292 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
14293 Set_Entity (Name (N2), Gen_Id);
14294
14295 Rewrite (Name (N1),
14296 Make_Expanded_Name (Loc,
14297 Chars => Chars (Gen_Id),
14298 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
14299 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
14300
14301 Set_Associated_Node (Name (N1), Name (N2));
14302 Set_Associated_Node (Prefix (Name (N1)), Empty);
14303 Set_Associated_Node
14304 (Selector_Name (Name (N1)), Selector_Name (Name (N2)));
14305 Set_Etype (Name (N1), Etype (Gen_Id));
14306 end if;
14307 end Save_Global_Defaults;
14308
14309 ----------------------------
14310 -- Save_Global_Descendant --
14311 ----------------------------
14312
14313 procedure Save_Global_Descendant (D : Union_Id) is
14314 N1 : Node_Id;
14315
14316 begin
14317 if D in Node_Range then
14318 if D = Union_Id (Empty) then
14319 null;
14320
14321 elsif Nkind (Node_Id (D)) /= N_Compilation_Unit then
14322 Save_References (Node_Id (D));
14323 end if;
14324
14325 elsif D in List_Range then
14326 if D = Union_Id (No_List) or else Is_Empty_List (List_Id (D)) then
14327 null;
14328
14329 else
14330 N1 := First (List_Id (D));
14331 while Present (N1) loop
14332 Save_References (N1);
14333 Next (N1);
14334 end loop;
14335 end if;
14336
14337 -- Element list or other non-node field, nothing to do
14338
14339 else
14340 null;
14341 end if;
14342 end Save_Global_Descendant;
14343
14344 ---------------------
14345 -- Save_References --
14346 ---------------------
14347
14348 -- This is the recursive procedure that does the work once the enclosing
14349 -- generic scope has been established. We have to treat specially a
14350 -- number of node rewritings that are required by semantic processing
14351 -- and which change the kind of nodes in the generic copy: typically
14352 -- constant-folding, replacing an operator node by a string literal, or
14353 -- a selected component by an expanded name. In each of those cases, the
14354 -- transformation is propagated to the generic unit.
14355
14356 procedure Save_References (N : Node_Id) is
14357 Loc : constant Source_Ptr := Sloc (N);
14358
14359 function Requires_Delayed_Save (Nod : Node_Id) return Boolean;
14360 -- Determine whether arbitrary node Nod requires delayed capture of
14361 -- global references within its aspect specifications.
14362
14363 procedure Save_References_In_Aggregate (N : Node_Id);
14364 -- Save all global references in [extension] aggregate node N
14365
14366 procedure Save_References_In_Char_Lit_Or_Op_Symbol (N : Node_Id);
14367 -- Save all global references in a character literal or operator
14368 -- symbol denoted by N.
14369
14370 procedure Save_References_In_Descendants (N : Node_Id);
14371 -- Save all global references in all descendants of node N
14372
14373 procedure Save_References_In_Identifier (N : Node_Id);
14374 -- Save all global references in identifier node N
14375
14376 procedure Save_References_In_Operator (N : Node_Id);
14377 -- Save all global references in operator node N
14378
14379 procedure Save_References_In_Pragma (Prag : Node_Id);
14380 -- Save all global references found within the expression of pragma
14381 -- Prag.
14382
14383 ---------------------------
14384 -- Requires_Delayed_Save --
14385 ---------------------------
14386
14387 function Requires_Delayed_Save (Nod : Node_Id) return Boolean is
14388 begin
14389 -- Generic packages and subprograms require delayed capture of
14390 -- global references within their aspects due to the timing of
14391 -- annotation analysis.
14392
14393 if Nkind_In (Nod, N_Generic_Package_Declaration,
14394 N_Generic_Subprogram_Declaration,
14395 N_Package_Body,
14396 N_Package_Body_Stub,
14397 N_Subprogram_Body,
14398 N_Subprogram_Body_Stub)
14399 then
14400 -- Since the capture of global references is done on the
14401 -- unanalyzed generic template, there is no information around
14402 -- to infer the context. Use the Associated_Entity linkages to
14403 -- peek into the analyzed generic copy and determine what the
14404 -- template corresponds to.
14405
14406 if Nod = Templ then
14407 return
14408 Is_Generic_Declaration_Or_Body
14409 (Unit_Declaration_Node
14410 (Associated_Entity (Defining_Entity (Nod))));
14411
14412 -- Otherwise the generic unit being processed is not the top
14413 -- level template. It is safe to capture of global references
14414 -- within the generic unit because at this point the top level
14415 -- copy is fully analyzed.
14416
14417 else
14418 return False;
14419 end if;
14420
14421 -- Otherwise capture the global references without interference
14422
14423 else
14424 return False;
14425 end if;
14426 end Requires_Delayed_Save;
14427
14428 ----------------------------------
14429 -- Save_References_In_Aggregate --
14430 ----------------------------------
14431
14432 procedure Save_References_In_Aggregate (N : Node_Id) is
14433 Nam : Node_Id;
14434 Qual : Node_Id := Empty;
14435 Typ : Entity_Id := Empty;
14436
14437 use Atree.Unchecked_Access;
14438 -- This code section is part of implementing an untyped tree
14439 -- traversal, so it needs direct access to node fields.
14440
14441 begin
14442 N2 := Get_Associated_Node (N);
14443
14444 if Present (N2) then
14445 Typ := Etype (N2);
14446
14447 -- In an instance within a generic, use the name of the actual
14448 -- and not the original generic parameter. If the actual is
14449 -- global in the current generic it must be preserved for its
14450 -- instantiation.
14451
14452 if Nkind (Parent (Typ)) = N_Subtype_Declaration
14453 and then Present (Generic_Parent_Type (Parent (Typ)))
14454 then
14455 Typ := Base_Type (Typ);
14456 Set_Etype (N2, Typ);
14457 end if;
14458 end if;
14459
14460 if No (N2) or else No (Typ) or else not Is_Global (Typ) then
14461 Set_Associated_Node (N, Empty);
14462
14463 -- If the aggregate is an actual in a call, it has been
14464 -- resolved in the current context, to some local type. The
14465 -- enclosing call may have been disambiguated by the aggregate,
14466 -- and this disambiguation might fail at instantiation time
14467 -- because the type to which the aggregate did resolve is not
14468 -- preserved. In order to preserve some of this information,
14469 -- wrap the aggregate in a qualified expression, using the id
14470 -- of its type. For further disambiguation we qualify the type
14471 -- name with its scope (if visible) because both id's will have
14472 -- corresponding entities in an instance. This resolves most of
14473 -- the problems with missing type information on aggregates in
14474 -- instances.
14475
14476 if Present (N2)
14477 and then Nkind (N2) = Nkind (N)
14478 and then Nkind (Parent (N2)) in N_Subprogram_Call
14479 and then Present (Typ)
14480 and then Comes_From_Source (Typ)
14481 then
14482 Nam := Make_Identifier (Loc, Chars (Typ));
14483
14484 if Is_Immediately_Visible (Scope (Typ)) then
14485 Nam :=
14486 Make_Selected_Component (Loc,
14487 Prefix =>
14488 Make_Identifier (Loc, Chars (Scope (Typ))),
14489 Selector_Name => Nam);
14490 end if;
14491
14492 Qual :=
14493 Make_Qualified_Expression (Loc,
14494 Subtype_Mark => Nam,
14495 Expression => Relocate_Node (N));
14496 end if;
14497 end if;
14498
14499 Save_Global_Descendant (Field1 (N));
14500 Save_Global_Descendant (Field2 (N));
14501 Save_Global_Descendant (Field3 (N));
14502 Save_Global_Descendant (Field5 (N));
14503
14504 if Present (Qual) then
14505 Rewrite (N, Qual);
14506 end if;
14507 end Save_References_In_Aggregate;
14508
14509 ----------------------------------------------
14510 -- Save_References_In_Char_Lit_Or_Op_Symbol --
14511 ----------------------------------------------
14512
14513 procedure Save_References_In_Char_Lit_Or_Op_Symbol (N : Node_Id) is
14514 begin
14515 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
14516 Reset_Entity (N);
14517
14518 elsif Nkind (N) = N_Operator_Symbol
14519 and then Nkind (Get_Associated_Node (N)) = N_String_Literal
14520 then
14521 Change_Operator_Symbol_To_String_Literal (N);
14522 end if;
14523 end Save_References_In_Char_Lit_Or_Op_Symbol;
14524
14525 ------------------------------------
14526 -- Save_References_In_Descendants --
14527 ------------------------------------
14528
14529 procedure Save_References_In_Descendants (N : Node_Id) is
14530 use Atree.Unchecked_Access;
14531 -- This code section is part of implementing an untyped tree
14532 -- traversal, so it needs direct access to node fields.
14533
14534 begin
14535 Save_Global_Descendant (Field1 (N));
14536 Save_Global_Descendant (Field2 (N));
14537 Save_Global_Descendant (Field3 (N));
14538 Save_Global_Descendant (Field4 (N));
14539 Save_Global_Descendant (Field5 (N));
14540 end Save_References_In_Descendants;
14541
14542 -----------------------------------
14543 -- Save_References_In_Identifier --
14544 -----------------------------------
14545
14546 procedure Save_References_In_Identifier (N : Node_Id) is
14547 begin
14548 -- The node did not undergo a transformation
14549
14550 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
14551
14552 -- If this is a discriminant reference, always save it. It is
14553 -- used in the instance to find the corresponding discriminant
14554 -- positionally rather than by name.
14555
14556 Set_Original_Discriminant
14557 (N, Original_Discriminant (Get_Associated_Node (N)));
14558 Reset_Entity (N);
14559
14560 -- The analysis of the generic copy transformed the identifier
14561 -- into another construct. Propagate the changes to the template.
14562
14563 else
14564 N2 := Get_Associated_Node (N);
14565
14566 -- The identifier denotes a call to a parameterless function.
14567 -- Mark the node as resolved when the function is external.
14568
14569 if Nkind (N2) = N_Function_Call then
14570 E := Entity (Name (N2));
14571
14572 if Present (E) and then Is_Global (E) then
14573 Set_Etype (N, Etype (N2));
14574 else
14575 Set_Associated_Node (N, Empty);
14576 Set_Etype (N, Empty);
14577 end if;
14578
14579 -- The identifier denotes a named number that was constant
14580 -- folded. Preserve the original name for ASIS and undo the
14581 -- constant folding which will be repeated in the instance.
14582
14583 elsif Nkind_In (N2, N_Integer_Literal, N_Real_Literal)
14584 and then Is_Entity_Name (Original_Node (N2))
14585 then
14586 Set_Associated_Node (N, Original_Node (N2));
14587 Reset_Entity (N);
14588
14589 -- The identifier resolved to a string literal. Propagate this
14590 -- information to the generic template.
14591
14592 elsif Nkind (N2) = N_String_Literal then
14593 Rewrite (N, New_Copy (N2));
14594
14595 -- The identifier is rewritten as a dereference if it is the
14596 -- prefix of an implicit dereference. Preserve the original
14597 -- tree as the analysis of the instance will expand the node
14598 -- again, but preserve the resolved entity if it is global.
14599
14600 elsif Nkind (N2) = N_Explicit_Dereference then
14601 if Is_Entity_Name (Prefix (N2))
14602 and then Present (Entity (Prefix (N2)))
14603 and then Is_Global (Entity (Prefix (N2)))
14604 then
14605 Set_Associated_Node (N, Prefix (N2));
14606
14607 elsif Nkind (Prefix (N2)) = N_Function_Call
14608 and then Present (Entity (Name (Prefix (N2))))
14609 and then Is_Global (Entity (Name (Prefix (N2))))
14610 then
14611 Rewrite (N,
14612 Make_Explicit_Dereference (Loc,
14613 Prefix =>
14614 Make_Function_Call (Loc,
14615 Name =>
14616 New_Occurrence_Of
14617 (Entity (Name (Prefix (N2))), Loc))));
14618
14619 else
14620 Set_Associated_Node (N, Empty);
14621 Set_Etype (N, Empty);
14622 end if;
14623
14624 -- The subtype mark of a nominally unconstrained object is
14625 -- rewritten as a subtype indication using the bounds of the
14626 -- expression. Recover the original subtype mark.
14627
14628 elsif Nkind (N2) = N_Subtype_Indication
14629 and then Is_Entity_Name (Original_Node (N2))
14630 then
14631 Set_Associated_Node (N, Original_Node (N2));
14632 Reset_Entity (N);
14633 end if;
14634 end if;
14635 end Save_References_In_Identifier;
14636
14637 ---------------------------------
14638 -- Save_References_In_Operator --
14639 ---------------------------------
14640
14641 procedure Save_References_In_Operator (N : Node_Id) is
14642 begin
14643 -- The node did not undergo a transformation
14644
14645 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
14646 if Nkind (N) = N_Op_Concat then
14647 Set_Is_Component_Left_Opnd (N,
14648 Is_Component_Left_Opnd (Get_Associated_Node (N)));
14649
14650 Set_Is_Component_Right_Opnd (N,
14651 Is_Component_Right_Opnd (Get_Associated_Node (N)));
14652 end if;
14653
14654 Reset_Entity (N);
14655
14656 -- The analysis of the generic copy transformed the operator into
14657 -- some other construct. Propagate the changes to the template.
14658
14659 else
14660 N2 := Get_Associated_Node (N);
14661
14662 -- The operator resoved to a function call
14663
14664 if Nkind (N2) = N_Function_Call then
14665 E := Entity (Name (N2));
14666
14667 if Present (E) and then Is_Global (E) then
14668 Set_Etype (N, Etype (N2));
14669 else
14670 Set_Associated_Node (N, Empty);
14671 Set_Etype (N, Empty);
14672 end if;
14673
14674 -- The operator was folded into a literal
14675
14676 elsif Nkind_In (N2, N_Integer_Literal,
14677 N_Real_Literal,
14678 N_String_Literal)
14679 then
14680 if Present (Original_Node (N2))
14681 and then Nkind (Original_Node (N2)) = Nkind (N)
14682 then
14683 -- Operation was constant-folded. Whenever possible,
14684 -- recover semantic information from unfolded node,
14685 -- for ASIS use.
14686
14687 Set_Associated_Node (N, Original_Node (N2));
14688
14689 if Nkind (N) = N_Op_Concat then
14690 Set_Is_Component_Left_Opnd (N,
14691 Is_Component_Left_Opnd (Get_Associated_Node (N)));
14692 Set_Is_Component_Right_Opnd (N,
14693 Is_Component_Right_Opnd (Get_Associated_Node (N)));
14694 end if;
14695
14696 Reset_Entity (N);
14697
14698 -- Propagate the constant folding back to the template
14699
14700 else
14701 Rewrite (N, New_Copy (N2));
14702 Set_Analyzed (N, False);
14703 end if;
14704
14705 -- The operator was folded into an enumeration literal. Retain
14706 -- the entity to avoid spurious ambiguities if it is overloaded
14707 -- at the point of instantiation or inlining.
14708
14709 elsif Nkind (N2) = N_Identifier
14710 and then Ekind (Entity (N2)) = E_Enumeration_Literal
14711 then
14712 Rewrite (N, New_Copy (N2));
14713 Set_Analyzed (N, False);
14714 end if;
14715 end if;
14716
14717 -- Complete the operands check if node has not been constant
14718 -- folded.
14719
14720 if Nkind (N) in N_Op then
14721 Save_Entity_Descendants (N);
14722 end if;
14723 end Save_References_In_Operator;
14724
14725 -------------------------------
14726 -- Save_References_In_Pragma --
14727 -------------------------------
14728
14729 procedure Save_References_In_Pragma (Prag : Node_Id) is
14730 Context : Node_Id;
14731 Do_Save : Boolean := True;
14732
14733 use Atree.Unchecked_Access;
14734 -- This code section is part of implementing an untyped tree
14735 -- traversal, so it needs direct access to node fields.
14736
14737 begin
14738 -- Do not save global references in pragmas generated from aspects
14739 -- because the pragmas will be regenerated at instantiation time.
14740
14741 if From_Aspect_Specification (Prag) then
14742 Do_Save := False;
14743
14744 -- The capture of global references within contract-related source
14745 -- pragmas associated with generic packages, subprograms or their
14746 -- respective bodies must be delayed due to timing of annotation
14747 -- analysis. Global references are still captured in routine
14748 -- Save_Global_References_In_Contract.
14749
14750 elsif Is_Generic_Contract_Pragma (Prag) and then Prag /= Templ then
14751 if Is_Package_Contract_Annotation (Prag) then
14752 Context := Find_Related_Package_Or_Body (Prag);
14753
14754 else
14755 pragma Assert (Is_Subprogram_Contract_Annotation (Prag));
14756 Context := Find_Related_Subprogram_Or_Body (Prag);
14757 end if;
14758
14759 -- The use of Original_Node accounts for the case when the
14760 -- related context is generic template.
14761
14762 if Requires_Delayed_Save (Original_Node (Context)) then
14763 Do_Save := False;
14764 end if;
14765 end if;
14766
14767 -- For all other cases, save all global references within the
14768 -- descendants, but skip the following semantic fields:
14769
14770 -- Field1 - Next_Pragma
14771 -- Field3 - Corresponding_Aspect
14772 -- Field5 - Next_Rep_Item
14773
14774 if Do_Save then
14775 Save_Global_Descendant (Field2 (Prag));
14776 Save_Global_Descendant (Field4 (Prag));
14777 end if;
14778 end Save_References_In_Pragma;
14779
14780 -- Start of processing for Save_References
14781
14782 begin
14783 if N = Empty then
14784 null;
14785
14786 -- Aggregates
14787
14788 elsif Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
14789 Save_References_In_Aggregate (N);
14790
14791 -- Character literals, operator symbols
14792
14793 elsif Nkind_In (N, N_Character_Literal, N_Operator_Symbol) then
14794 Save_References_In_Char_Lit_Or_Op_Symbol (N);
14795
14796 -- Defining identifiers
14797
14798 elsif Nkind (N) in N_Entity then
14799 null;
14800
14801 -- Identifiers
14802
14803 elsif Nkind (N) = N_Identifier then
14804 Save_References_In_Identifier (N);
14805
14806 -- Operators
14807
14808 elsif Nkind (N) in N_Op then
14809 Save_References_In_Operator (N);
14810
14811 -- Pragmas
14812
14813 elsif Nkind (N) = N_Pragma then
14814 Save_References_In_Pragma (N);
14815
14816 else
14817 Save_References_In_Descendants (N);
14818 end if;
14819
14820 -- Save all global references found within the aspect specifications
14821 -- of the related node.
14822
14823 if Permits_Aspect_Specifications (N) and then Has_Aspects (N) then
14824
14825 -- The capture of global references within aspects associated with
14826 -- generic packages, subprograms or their bodies must be delayed
14827 -- due to timing of annotation analysis. Global references are
14828 -- still captured in routine Save_Global_References_In_Contract.
14829
14830 if Requires_Delayed_Save (N) then
14831 null;
14832
14833 -- Otherwise save all global references within the aspects
14834
14835 else
14836 Save_Global_References_In_Aspects (N);
14837 end if;
14838 end if;
14839 end Save_References;
14840
14841 -- Start of processing for Save_Global_References
14842
14843 begin
14844 Gen_Scope := Current_Scope;
14845
14846 -- If the generic unit is a child unit, references to entities in the
14847 -- parent are treated as local, because they will be resolved anew in
14848 -- the context of the instance of the parent.
14849
14850 while Is_Child_Unit (Gen_Scope)
14851 and then Ekind (Scope (Gen_Scope)) = E_Generic_Package
14852 loop
14853 Gen_Scope := Scope (Gen_Scope);
14854 end loop;
14855
14856 Save_References (Templ);
14857 end Save_Global_References;
14858
14859 ---------------------------------------
14860 -- Save_Global_References_In_Aspects --
14861 ---------------------------------------
14862
14863 procedure Save_Global_References_In_Aspects (N : Node_Id) is
14864 Asp : Node_Id;
14865 Expr : Node_Id;
14866
14867 begin
14868 Asp := First (Aspect_Specifications (N));
14869 while Present (Asp) loop
14870 Expr := Expression (Asp);
14871
14872 if Present (Expr) then
14873 Save_Global_References (Expr);
14874 end if;
14875
14876 Next (Asp);
14877 end loop;
14878 end Save_Global_References_In_Aspects;
14879
14880 ----------------------------------------
14881 -- Save_Global_References_In_Contract --
14882 ----------------------------------------
14883
14884 procedure Save_Global_References_In_Contract
14885 (Templ : Node_Id;
14886 Gen_Id : Entity_Id)
14887 is
14888 procedure Save_Global_References_In_List (First_Prag : Node_Id);
14889 -- Save all global references in contract-related source pragmas found
14890 -- in the list starting with pragma First_Prag.
14891
14892 ------------------------------------
14893 -- Save_Global_References_In_List --
14894 ------------------------------------
14895
14896 procedure Save_Global_References_In_List (First_Prag : Node_Id) is
14897 Prag : Node_Id;
14898
14899 begin
14900 Prag := First_Prag;
14901 while Present (Prag) loop
14902 if Is_Generic_Contract_Pragma (Prag) then
14903 Save_Global_References (Prag);
14904 end if;
14905
14906 Prag := Next_Pragma (Prag);
14907 end loop;
14908 end Save_Global_References_In_List;
14909
14910 -- Local variables
14911
14912 Items : constant Node_Id := Contract (Defining_Entity (Templ));
14913
14914 -- Start of processing for Save_Global_References_In_Contract
14915
14916 begin
14917 -- The entity of the analyzed generic copy must be on the scope stack
14918 -- to ensure proper detection of global references.
14919
14920 Push_Scope (Gen_Id);
14921
14922 if Permits_Aspect_Specifications (Templ)
14923 and then Has_Aspects (Templ)
14924 then
14925 Save_Global_References_In_Aspects (Templ);
14926 end if;
14927
14928 if Present (Items) then
14929 Save_Global_References_In_List (Pre_Post_Conditions (Items));
14930 Save_Global_References_In_List (Contract_Test_Cases (Items));
14931 Save_Global_References_In_List (Classifications (Items));
14932 end if;
14933
14934 Pop_Scope;
14935 end Save_Global_References_In_Contract;
14936
14937 --------------------------------------
14938 -- Set_Copied_Sloc_For_Inlined_Body --
14939 --------------------------------------
14940
14941 procedure Set_Copied_Sloc_For_Inlined_Body (N : Node_Id; E : Entity_Id) is
14942 begin
14943 Create_Instantiation_Source (N, E, True, S_Adjustment);
14944 end Set_Copied_Sloc_For_Inlined_Body;
14945
14946 ---------------------
14947 -- Set_Instance_Of --
14948 ---------------------
14949
14950 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id) is
14951 begin
14952 Generic_Renamings.Table (Generic_Renamings.Last) := (A, B, Assoc_Null);
14953 Generic_Renamings_HTable.Set (Generic_Renamings.Last);
14954 Generic_Renamings.Increment_Last;
14955 end Set_Instance_Of;
14956
14957 --------------------
14958 -- Set_Next_Assoc --
14959 --------------------
14960
14961 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr) is
14962 begin
14963 Generic_Renamings.Table (E).Next_In_HTable := Next;
14964 end Set_Next_Assoc;
14965
14966 -------------------
14967 -- Start_Generic --
14968 -------------------
14969
14970 procedure Start_Generic is
14971 begin
14972 -- ??? More things could be factored out in this routine.
14973 -- Should probably be done at a later stage.
14974
14975 Generic_Flags.Append (Inside_A_Generic);
14976 Inside_A_Generic := True;
14977
14978 Expander_Mode_Save_And_Set (False);
14979 end Start_Generic;
14980
14981 ----------------------
14982 -- Set_Instance_Env --
14983 ----------------------
14984
14985 procedure Set_Instance_Env
14986 (Gen_Unit : Entity_Id;
14987 Act_Unit : Entity_Id)
14988 is
14989 Assertion_Status : constant Boolean := Assertions_Enabled;
14990 Save_SPARK_Mode : constant SPARK_Mode_Type := SPARK_Mode;
14991 Save_SPARK_Mode_Pragma : constant Node_Id := SPARK_Mode_Pragma;
14992
14993 begin
14994 -- Regardless of the current mode, predefined units are analyzed in the
14995 -- most current Ada mode, and earlier version Ada checks do not apply
14996 -- to predefined units. Nothing needs to be done for non-internal units.
14997 -- These are always analyzed in the current mode.
14998
14999 if Is_Internal_File_Name
15000 (Fname => Unit_File_Name (Get_Source_Unit (Gen_Unit)),
15001 Renamings_Included => True)
15002 then
15003 Set_Opt_Config_Switches (True, Current_Sem_Unit = Main_Unit);
15004
15005 -- In Ada2012 we may want to enable assertions in an instance of a
15006 -- predefined unit, in which case we need to preserve the current
15007 -- setting for the Assertions_Enabled flag. This will become more
15008 -- critical when pre/postconditions are added to predefined units,
15009 -- as is already the case for some numeric libraries.
15010
15011 if Ada_Version >= Ada_2012 then
15012 Assertions_Enabled := Assertion_Status;
15013 end if;
15014
15015 -- SPARK_Mode for an instance is the one applicable at the point of
15016 -- instantiation.
15017
15018 SPARK_Mode := Save_SPARK_Mode;
15019 SPARK_Mode_Pragma := Save_SPARK_Mode_Pragma;
15020
15021 -- Make sure dynamic elaboration checks are off in SPARK Mode
15022
15023 if SPARK_Mode = On then
15024 Dynamic_Elaboration_Checks := False;
15025 end if;
15026 end if;
15027
15028 Current_Instantiated_Parent :=
15029 (Gen_Id => Gen_Unit,
15030 Act_Id => Act_Unit,
15031 Next_In_HTable => Assoc_Null);
15032 end Set_Instance_Env;
15033
15034 -----------------
15035 -- Switch_View --
15036 -----------------
15037
15038 procedure Switch_View (T : Entity_Id) is
15039 BT : constant Entity_Id := Base_Type (T);
15040 Priv_Elmt : Elmt_Id := No_Elmt;
15041 Priv_Sub : Entity_Id;
15042
15043 begin
15044 -- T may be private but its base type may have been exchanged through
15045 -- some other occurrence, in which case there is nothing to switch
15046 -- besides T itself. Note that a private dependent subtype of a private
15047 -- type might not have been switched even if the base type has been,
15048 -- because of the last branch of Check_Private_View (see comment there).
15049
15050 if not Is_Private_Type (BT) then
15051 Prepend_Elmt (Full_View (T), Exchanged_Views);
15052 Exchange_Declarations (T);
15053 return;
15054 end if;
15055
15056 Priv_Elmt := First_Elmt (Private_Dependents (BT));
15057
15058 if Present (Full_View (BT)) then
15059 Prepend_Elmt (Full_View (BT), Exchanged_Views);
15060 Exchange_Declarations (BT);
15061 end if;
15062
15063 while Present (Priv_Elmt) loop
15064 Priv_Sub := (Node (Priv_Elmt));
15065
15066 -- We avoid flipping the subtype if the Etype of its full view is
15067 -- private because this would result in a malformed subtype. This
15068 -- occurs when the Etype of the subtype full view is the full view of
15069 -- the base type (and since the base types were just switched, the
15070 -- subtype is pointing to the wrong view). This is currently the case
15071 -- for tagged record types, access types (maybe more?) and needs to
15072 -- be resolved. ???
15073
15074 if Present (Full_View (Priv_Sub))
15075 and then not Is_Private_Type (Etype (Full_View (Priv_Sub)))
15076 then
15077 Prepend_Elmt (Full_View (Priv_Sub), Exchanged_Views);
15078 Exchange_Declarations (Priv_Sub);
15079 end if;
15080
15081 Next_Elmt (Priv_Elmt);
15082 end loop;
15083 end Switch_View;
15084
15085 -----------------
15086 -- True_Parent --
15087 -----------------
15088
15089 function True_Parent (N : Node_Id) return Node_Id is
15090 begin
15091 if Nkind (Parent (N)) = N_Subunit then
15092 return Parent (Corresponding_Stub (Parent (N)));
15093 else
15094 return Parent (N);
15095 end if;
15096 end True_Parent;
15097
15098 -----------------------------
15099 -- Valid_Default_Attribute --
15100 -----------------------------
15101
15102 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id) is
15103 Attr_Id : constant Attribute_Id :=
15104 Get_Attribute_Id (Attribute_Name (Def));
15105 T : constant Entity_Id := Entity (Prefix (Def));
15106 Is_Fun : constant Boolean := (Ekind (Nam) = E_Function);
15107 F : Entity_Id;
15108 Num_F : Int;
15109 OK : Boolean;
15110
15111 begin
15112 if No (T) or else T = Any_Id then
15113 return;
15114 end if;
15115
15116 Num_F := 0;
15117 F := First_Formal (Nam);
15118 while Present (F) loop
15119 Num_F := Num_F + 1;
15120 Next_Formal (F);
15121 end loop;
15122
15123 case Attr_Id is
15124 when Attribute_Adjacent | Attribute_Ceiling | Attribute_Copy_Sign |
15125 Attribute_Floor | Attribute_Fraction | Attribute_Machine |
15126 Attribute_Model | Attribute_Remainder | Attribute_Rounding |
15127 Attribute_Unbiased_Rounding =>
15128 OK := Is_Fun
15129 and then Num_F = 1
15130 and then Is_Floating_Point_Type (T);
15131
15132 when Attribute_Image | Attribute_Pred | Attribute_Succ |
15133 Attribute_Value | Attribute_Wide_Image |
15134 Attribute_Wide_Value =>
15135 OK := (Is_Fun and then Num_F = 1 and then Is_Scalar_Type (T));
15136
15137 when Attribute_Max | Attribute_Min =>
15138 OK := (Is_Fun and then Num_F = 2 and then Is_Scalar_Type (T));
15139
15140 when Attribute_Input =>
15141 OK := (Is_Fun and then Num_F = 1);
15142
15143 when Attribute_Output | Attribute_Read | Attribute_Write =>
15144 OK := (not Is_Fun and then Num_F = 2);
15145
15146 when others =>
15147 OK := False;
15148 end case;
15149
15150 if not OK then
15151 Error_Msg_N
15152 ("attribute reference has wrong profile for subprogram", Def);
15153 end if;
15154 end Valid_Default_Attribute;
15155
15156 end Sem_Ch12;