[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 -- Manually set the SPARK_Mode from the context because the package
2649 -- declaration is never analyzed.
2650
2651 Set_SPARK_Pragma (Formal, SPARK_Mode_Pragma);
2652 Set_SPARK_Aux_Pragma (Formal, SPARK_Mode_Pragma);
2653 Set_SPARK_Pragma_Inherited (Formal);
2654 Set_SPARK_Aux_Pragma_Inherited (Formal);
2655
2656 if Is_Child_Unit (Gen_Unit) and then Parent_Installed then
2657
2658 -- Similarly, we have to make the name of the formal visible in the
2659 -- parent instance, to resolve properly fully qualified names that
2660 -- may appear in the generic unit. The parent instance has been
2661 -- placed on the scope stack ahead of the current scope.
2662
2663 Parent_Instance := Scope_Stack.Table (Scope_Stack.Last - 1).Entity;
2664
2665 Renaming_In_Par :=
2666 Make_Defining_Identifier (Loc, Chars (Gen_Unit));
2667 Set_Ekind (Renaming_In_Par, E_Package);
2668 Set_Etype (Renaming_In_Par, Standard_Void_Type);
2669 Set_Scope (Renaming_In_Par, Parent_Instance);
2670 Set_Parent (Renaming_In_Par, Parent (Formal));
2671 Set_Renamed_Object (Renaming_In_Par, Formal);
2672 Append_Entity (Renaming_In_Par, Parent_Instance);
2673 end if;
2674
2675 Analyze (Specification (N));
2676
2677 -- The formals for which associations are provided are not visible
2678 -- outside of the formal package. The others are still declared by a
2679 -- formal parameter declaration.
2680
2681 -- If there are no associations, the only local entity to hide is the
2682 -- generated package renaming itself.
2683
2684 declare
2685 E : Entity_Id;
2686
2687 begin
2688 E := First_Entity (Formal);
2689 while Present (E) loop
2690 if Associations and then not Is_Generic_Formal (E) then
2691 Set_Is_Hidden (E);
2692 end if;
2693
2694 if Ekind (E) = E_Package and then Renamed_Entity (E) = Formal then
2695 Set_Is_Hidden (E);
2696 exit;
2697 end if;
2698
2699 Next_Entity (E);
2700 end loop;
2701 end;
2702
2703 End_Package_Scope (Formal);
2704 Restore_Hidden_Primitives (Vis_Prims_List);
2705
2706 if Parent_Installed then
2707 Remove_Parent;
2708 end if;
2709
2710 Restore_Env;
2711
2712 -- Inside the generic unit, the formal package is a regular package, but
2713 -- no body is needed for it. Note that after instantiation, the defining
2714 -- unit name we need is in the new tree and not in the original (see
2715 -- Package_Instantiation). A generic formal package is an instance, and
2716 -- can be used as an actual for an inner instance.
2717
2718 Set_Has_Completion (Formal, True);
2719
2720 -- Add semantic information to the original defining identifier.
2721 -- for ASIS use.
2722
2723 Set_Ekind (Pack_Id, E_Package);
2724 Set_Etype (Pack_Id, Standard_Void_Type);
2725 Set_Scope (Pack_Id, Scope (Formal));
2726 Set_Has_Completion (Pack_Id, True);
2727
2728 <<Leave>>
2729 if Has_Aspects (N) then
2730 Analyze_Aspect_Specifications (N, Pack_Id);
2731 end if;
2732 end Analyze_Formal_Package_Declaration;
2733
2734 ---------------------------------
2735 -- Analyze_Formal_Private_Type --
2736 ---------------------------------
2737
2738 procedure Analyze_Formal_Private_Type
2739 (N : Node_Id;
2740 T : Entity_Id;
2741 Def : Node_Id)
2742 is
2743 begin
2744 New_Private_Type (N, T, Def);
2745
2746 -- Set the size to an arbitrary but legal value
2747
2748 Set_Size_Info (T, Standard_Integer);
2749 Set_RM_Size (T, RM_Size (Standard_Integer));
2750 end Analyze_Formal_Private_Type;
2751
2752 ------------------------------------
2753 -- Analyze_Formal_Incomplete_Type --
2754 ------------------------------------
2755
2756 procedure Analyze_Formal_Incomplete_Type
2757 (T : Entity_Id;
2758 Def : Node_Id)
2759 is
2760 begin
2761 Enter_Name (T);
2762 Set_Ekind (T, E_Incomplete_Type);
2763 Set_Etype (T, T);
2764 Set_Private_Dependents (T, New_Elmt_List);
2765
2766 if Tagged_Present (Def) then
2767 Set_Is_Tagged_Type (T);
2768 Make_Class_Wide_Type (T);
2769 Set_Direct_Primitive_Operations (T, New_Elmt_List);
2770 end if;
2771 end Analyze_Formal_Incomplete_Type;
2772
2773 ----------------------------------------
2774 -- Analyze_Formal_Signed_Integer_Type --
2775 ----------------------------------------
2776
2777 procedure Analyze_Formal_Signed_Integer_Type
2778 (T : Entity_Id;
2779 Def : Node_Id)
2780 is
2781 Base : constant Entity_Id :=
2782 New_Internal_Entity
2783 (E_Signed_Integer_Type,
2784 Current_Scope,
2785 Sloc (Defining_Identifier (Parent (Def))), 'G');
2786
2787 begin
2788 Enter_Name (T);
2789
2790 Set_Ekind (T, E_Signed_Integer_Subtype);
2791 Set_Etype (T, Base);
2792 Set_Size_Info (T, Standard_Integer);
2793 Set_RM_Size (T, RM_Size (Standard_Integer));
2794 Set_Scalar_Range (T, Scalar_Range (Standard_Integer));
2795 Set_Is_Constrained (T);
2796
2797 Set_Is_Generic_Type (Base);
2798 Set_Size_Info (Base, Standard_Integer);
2799 Set_RM_Size (Base, RM_Size (Standard_Integer));
2800 Set_Etype (Base, Base);
2801 Set_Scalar_Range (Base, Scalar_Range (Standard_Integer));
2802 Set_Parent (Base, Parent (Def));
2803 end Analyze_Formal_Signed_Integer_Type;
2804
2805 -------------------------------------------
2806 -- Analyze_Formal_Subprogram_Declaration --
2807 -------------------------------------------
2808
2809 procedure Analyze_Formal_Subprogram_Declaration (N : Node_Id) is
2810 Spec : constant Node_Id := Specification (N);
2811 Def : constant Node_Id := Default_Name (N);
2812 Nam : constant Entity_Id := Defining_Unit_Name (Spec);
2813 Subp : Entity_Id;
2814
2815 begin
2816 if Nam = Error then
2817 return;
2818 end if;
2819
2820 if Nkind (Nam) = N_Defining_Program_Unit_Name then
2821 Error_Msg_N ("name of formal subprogram must be a direct name", Nam);
2822 goto Leave;
2823 end if;
2824
2825 Analyze_Subprogram_Declaration (N);
2826 Set_Is_Formal_Subprogram (Nam);
2827 Set_Has_Completion (Nam);
2828
2829 if Nkind (N) = N_Formal_Abstract_Subprogram_Declaration then
2830 Set_Is_Abstract_Subprogram (Nam);
2831
2832 Set_Is_Dispatching_Operation (Nam);
2833
2834 -- A formal abstract procedure cannot have a null default
2835 -- (RM 12.6(4.1/2)).
2836
2837 if Nkind (Spec) = N_Procedure_Specification
2838 and then Null_Present (Spec)
2839 then
2840 Error_Msg_N
2841 ("a formal abstract subprogram cannot default to null", Spec);
2842 end if;
2843
2844 declare
2845 Ctrl_Type : constant Entity_Id := Find_Dispatching_Type (Nam);
2846 begin
2847 if No (Ctrl_Type) then
2848 Error_Msg_N
2849 ("abstract formal subprogram must have a controlling type",
2850 N);
2851
2852 elsif Ada_Version >= Ada_2012
2853 and then Is_Incomplete_Type (Ctrl_Type)
2854 then
2855 Error_Msg_NE
2856 ("controlling type of abstract formal subprogram cannot "
2857 & "be incomplete type", N, Ctrl_Type);
2858
2859 else
2860 Check_Controlling_Formals (Ctrl_Type, Nam);
2861 end if;
2862 end;
2863 end if;
2864
2865 -- Default name is resolved at the point of instantiation
2866
2867 if Box_Present (N) then
2868 null;
2869
2870 -- Else default is bound at the point of generic declaration
2871
2872 elsif Present (Def) then
2873 if Nkind (Def) = N_Operator_Symbol then
2874 Find_Direct_Name (Def);
2875
2876 elsif Nkind (Def) /= N_Attribute_Reference then
2877 Analyze (Def);
2878
2879 else
2880 -- For an attribute reference, analyze the prefix and verify
2881 -- that it has the proper profile for the subprogram.
2882
2883 Analyze (Prefix (Def));
2884 Valid_Default_Attribute (Nam, Def);
2885 goto Leave;
2886 end if;
2887
2888 -- Default name may be overloaded, in which case the interpretation
2889 -- with the correct profile must be selected, as for a renaming.
2890 -- If the definition is an indexed component, it must denote a
2891 -- member of an entry family. If it is a selected component, it
2892 -- can be a protected operation.
2893
2894 if Etype (Def) = Any_Type then
2895 goto Leave;
2896
2897 elsif Nkind (Def) = N_Selected_Component then
2898 if not Is_Overloadable (Entity (Selector_Name (Def))) then
2899 Error_Msg_N ("expect valid subprogram name as default", Def);
2900 end if;
2901
2902 elsif Nkind (Def) = N_Indexed_Component then
2903 if Is_Entity_Name (Prefix (Def)) then
2904 if Ekind (Entity (Prefix (Def))) /= E_Entry_Family then
2905 Error_Msg_N ("expect valid subprogram name as default", Def);
2906 end if;
2907
2908 elsif Nkind (Prefix (Def)) = N_Selected_Component then
2909 if Ekind (Entity (Selector_Name (Prefix (Def)))) /=
2910 E_Entry_Family
2911 then
2912 Error_Msg_N ("expect valid subprogram name as default", Def);
2913 end if;
2914
2915 else
2916 Error_Msg_N ("expect valid subprogram name as default", Def);
2917 goto Leave;
2918 end if;
2919
2920 elsif Nkind (Def) = N_Character_Literal then
2921
2922 -- Needs some type checks: subprogram should be parameterless???
2923
2924 Resolve (Def, (Etype (Nam)));
2925
2926 elsif not Is_Entity_Name (Def)
2927 or else not Is_Overloadable (Entity (Def))
2928 then
2929 Error_Msg_N ("expect valid subprogram name as default", Def);
2930 goto Leave;
2931
2932 elsif not Is_Overloaded (Def) then
2933 Subp := Entity (Def);
2934
2935 if Subp = Nam then
2936 Error_Msg_N ("premature usage of formal subprogram", Def);
2937
2938 elsif not Entity_Matches_Spec (Subp, Nam) then
2939 Error_Msg_N ("no visible entity matches specification", Def);
2940 end if;
2941
2942 -- More than one interpretation, so disambiguate as for a renaming
2943
2944 else
2945 declare
2946 I : Interp_Index;
2947 I1 : Interp_Index := 0;
2948 It : Interp;
2949 It1 : Interp;
2950
2951 begin
2952 Subp := Any_Id;
2953 Get_First_Interp (Def, I, It);
2954 while Present (It.Nam) loop
2955 if Entity_Matches_Spec (It.Nam, Nam) then
2956 if Subp /= Any_Id then
2957 It1 := Disambiguate (Def, I1, I, Etype (Subp));
2958
2959 if It1 = No_Interp then
2960 Error_Msg_N ("ambiguous default subprogram", Def);
2961 else
2962 Subp := It1.Nam;
2963 end if;
2964
2965 exit;
2966
2967 else
2968 I1 := I;
2969 Subp := It.Nam;
2970 end if;
2971 end if;
2972
2973 Get_Next_Interp (I, It);
2974 end loop;
2975 end;
2976
2977 if Subp /= Any_Id then
2978
2979 -- Subprogram found, generate reference to it
2980
2981 Set_Entity (Def, Subp);
2982 Generate_Reference (Subp, Def);
2983
2984 if Subp = Nam then
2985 Error_Msg_N ("premature usage of formal subprogram", Def);
2986
2987 elsif Ekind (Subp) /= E_Operator then
2988 Check_Mode_Conformant (Subp, Nam);
2989 end if;
2990
2991 else
2992 Error_Msg_N ("no visible subprogram matches specification", N);
2993 end if;
2994 end if;
2995 end if;
2996
2997 <<Leave>>
2998 if Has_Aspects (N) then
2999 Analyze_Aspect_Specifications (N, Nam);
3000 end if;
3001
3002 end Analyze_Formal_Subprogram_Declaration;
3003
3004 -------------------------------------
3005 -- Analyze_Formal_Type_Declaration --
3006 -------------------------------------
3007
3008 procedure Analyze_Formal_Type_Declaration (N : Node_Id) is
3009 Def : constant Node_Id := Formal_Type_Definition (N);
3010 T : Entity_Id;
3011
3012 begin
3013 T := Defining_Identifier (N);
3014
3015 if Present (Discriminant_Specifications (N))
3016 and then Nkind (Def) /= N_Formal_Private_Type_Definition
3017 then
3018 Error_Msg_N
3019 ("discriminants not allowed for this formal type", T);
3020 end if;
3021
3022 -- Enter the new name, and branch to specific routine
3023
3024 case Nkind (Def) is
3025 when N_Formal_Private_Type_Definition =>
3026 Analyze_Formal_Private_Type (N, T, Def);
3027
3028 when N_Formal_Derived_Type_Definition =>
3029 Analyze_Formal_Derived_Type (N, T, Def);
3030
3031 when N_Formal_Incomplete_Type_Definition =>
3032 Analyze_Formal_Incomplete_Type (T, Def);
3033
3034 when N_Formal_Discrete_Type_Definition =>
3035 Analyze_Formal_Discrete_Type (T, Def);
3036
3037 when N_Formal_Signed_Integer_Type_Definition =>
3038 Analyze_Formal_Signed_Integer_Type (T, Def);
3039
3040 when N_Formal_Modular_Type_Definition =>
3041 Analyze_Formal_Modular_Type (T, Def);
3042
3043 when N_Formal_Floating_Point_Definition =>
3044 Analyze_Formal_Floating_Type (T, Def);
3045
3046 when N_Formal_Ordinary_Fixed_Point_Definition =>
3047 Analyze_Formal_Ordinary_Fixed_Point_Type (T, Def);
3048
3049 when N_Formal_Decimal_Fixed_Point_Definition =>
3050 Analyze_Formal_Decimal_Fixed_Point_Type (T, Def);
3051
3052 when N_Array_Type_Definition =>
3053 Analyze_Formal_Array_Type (T, Def);
3054
3055 when N_Access_To_Object_Definition |
3056 N_Access_Function_Definition |
3057 N_Access_Procedure_Definition =>
3058 Analyze_Generic_Access_Type (T, Def);
3059
3060 -- Ada 2005: a interface declaration is encoded as an abstract
3061 -- record declaration or a abstract type derivation.
3062
3063 when N_Record_Definition =>
3064 Analyze_Formal_Interface_Type (N, T, Def);
3065
3066 when N_Derived_Type_Definition =>
3067 Analyze_Formal_Derived_Interface_Type (N, T, Def);
3068
3069 when N_Error =>
3070 null;
3071
3072 when others =>
3073 raise Program_Error;
3074
3075 end case;
3076
3077 Set_Is_Generic_Type (T);
3078
3079 if Has_Aspects (N) then
3080 Analyze_Aspect_Specifications (N, T);
3081 end if;
3082 end Analyze_Formal_Type_Declaration;
3083
3084 ------------------------------------
3085 -- Analyze_Function_Instantiation --
3086 ------------------------------------
3087
3088 procedure Analyze_Function_Instantiation (N : Node_Id) is
3089 begin
3090 Analyze_Subprogram_Instantiation (N, E_Function);
3091 end Analyze_Function_Instantiation;
3092
3093 ---------------------------------
3094 -- Analyze_Generic_Access_Type --
3095 ---------------------------------
3096
3097 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id) is
3098 begin
3099 Enter_Name (T);
3100
3101 if Nkind (Def) = N_Access_To_Object_Definition then
3102 Access_Type_Declaration (T, Def);
3103
3104 if Is_Incomplete_Or_Private_Type (Designated_Type (T))
3105 and then No (Full_View (Designated_Type (T)))
3106 and then not Is_Generic_Type (Designated_Type (T))
3107 then
3108 Error_Msg_N ("premature usage of incomplete type", Def);
3109
3110 elsif not Is_Entity_Name (Subtype_Indication (Def)) then
3111 Error_Msg_N
3112 ("only a subtype mark is allowed in a formal", Def);
3113 end if;
3114
3115 else
3116 Access_Subprogram_Declaration (T, Def);
3117 end if;
3118 end Analyze_Generic_Access_Type;
3119
3120 ---------------------------------
3121 -- Analyze_Generic_Formal_Part --
3122 ---------------------------------
3123
3124 procedure Analyze_Generic_Formal_Part (N : Node_Id) is
3125 Gen_Parm_Decl : Node_Id;
3126
3127 begin
3128 -- The generic formals are processed in the scope of the generic unit,
3129 -- where they are immediately visible. The scope is installed by the
3130 -- caller.
3131
3132 Gen_Parm_Decl := First (Generic_Formal_Declarations (N));
3133 while Present (Gen_Parm_Decl) loop
3134 Analyze (Gen_Parm_Decl);
3135 Next (Gen_Parm_Decl);
3136 end loop;
3137
3138 Generate_Reference_To_Generic_Formals (Current_Scope);
3139 end Analyze_Generic_Formal_Part;
3140
3141 ------------------------------------------
3142 -- Analyze_Generic_Package_Declaration --
3143 ------------------------------------------
3144
3145 procedure Analyze_Generic_Package_Declaration (N : Node_Id) is
3146 Loc : constant Source_Ptr := Sloc (N);
3147 Decls : constant List_Id :=
3148 Visible_Declarations (Specification (N));
3149 Decl : Node_Id;
3150 Id : Entity_Id;
3151 New_N : Node_Id;
3152 Renaming : Node_Id;
3153 Save_Parent : Node_Id;
3154
3155 begin
3156 Check_SPARK_05_Restriction ("generic is not allowed", N);
3157
3158 -- We introduce a renaming of the enclosing package, to have a usable
3159 -- entity as the prefix of an expanded name for a local entity of the
3160 -- form Par.P.Q, where P is the generic package. This is because a local
3161 -- entity named P may hide it, so that the usual visibility rules in
3162 -- the instance will not resolve properly.
3163
3164 Renaming :=
3165 Make_Package_Renaming_Declaration (Loc,
3166 Defining_Unit_Name =>
3167 Make_Defining_Identifier (Loc,
3168 Chars => New_External_Name (Chars (Defining_Entity (N)), "GH")),
3169 Name =>
3170 Make_Identifier (Loc, Chars (Defining_Entity (N))));
3171
3172 if Present (Decls) then
3173 Decl := First (Decls);
3174 while Present (Decl) and then Nkind (Decl) = N_Pragma loop
3175 Next (Decl);
3176 end loop;
3177
3178 if Present (Decl) then
3179 Insert_Before (Decl, Renaming);
3180 else
3181 Append (Renaming, Visible_Declarations (Specification (N)));
3182 end if;
3183
3184 else
3185 Set_Visible_Declarations (Specification (N), New_List (Renaming));
3186 end if;
3187
3188 -- Create copy of generic unit, and save for instantiation. If the unit
3189 -- is a child unit, do not copy the specifications for the parent, which
3190 -- are not part of the generic tree.
3191
3192 Save_Parent := Parent_Spec (N);
3193 Set_Parent_Spec (N, Empty);
3194
3195 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
3196 Set_Parent_Spec (New_N, Save_Parent);
3197 Rewrite (N, New_N);
3198
3199 -- Once the contents of the generic copy and the template are swapped,
3200 -- do the same for their respective aspect specifications.
3201
3202 Exchange_Aspects (N, New_N);
3203
3204 -- Collect all contract-related source pragmas found within the template
3205 -- and attach them to the contract of the package spec. This contract is
3206 -- used in the capture of global references within annotations.
3207
3208 Create_Generic_Contract (N);
3209
3210 Id := Defining_Entity (N);
3211 Generate_Definition (Id);
3212
3213 -- Expansion is not applied to generic units
3214
3215 Start_Generic;
3216
3217 Enter_Name (Id);
3218 Set_Ekind (Id, E_Generic_Package);
3219 Set_Etype (Id, Standard_Void_Type);
3220
3221 -- A generic package declared within a Ghost region is rendered Ghost
3222 -- (SPARK RM 6.9(2)).
3223
3224 if Ghost_Mode > None then
3225 Set_Is_Ghost_Entity (Id);
3226 end if;
3227
3228 -- Analyze aspects now, so that generated pragmas appear in the
3229 -- declarations before building and analyzing the generic copy.
3230
3231 if Has_Aspects (N) then
3232 Analyze_Aspect_Specifications (N, Id);
3233 end if;
3234
3235 Push_Scope (Id);
3236 Enter_Generic_Scope (Id);
3237 Set_Inner_Instances (Id, New_Elmt_List);
3238
3239 Set_Categorization_From_Pragmas (N);
3240 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3241
3242 -- Link the declaration of the generic homonym in the generic copy to
3243 -- the package it renames, so that it is always resolved properly.
3244
3245 Set_Generic_Homonym (Id, Defining_Unit_Name (Renaming));
3246 Set_Entity (Associated_Node (Name (Renaming)), Id);
3247
3248 -- For a library unit, we have reconstructed the entity for the unit,
3249 -- and must reset it in the library tables.
3250
3251 if Nkind (Parent (N)) = N_Compilation_Unit then
3252 Set_Cunit_Entity (Current_Sem_Unit, Id);
3253 end if;
3254
3255 Analyze_Generic_Formal_Part (N);
3256
3257 -- After processing the generic formals, analysis proceeds as for a
3258 -- non-generic package.
3259
3260 Analyze (Specification (N));
3261
3262 Validate_Categorization_Dependency (N, Id);
3263
3264 End_Generic;
3265
3266 End_Package_Scope (Id);
3267 Exit_Generic_Scope (Id);
3268
3269 if Nkind (Parent (N)) /= N_Compilation_Unit then
3270 Move_Freeze_Nodes (Id, N, Visible_Declarations (Specification (N)));
3271 Move_Freeze_Nodes (Id, N, Private_Declarations (Specification (N)));
3272 Move_Freeze_Nodes (Id, N, Generic_Formal_Declarations (N));
3273
3274 else
3275 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
3276 Validate_RT_RAT_Component (N);
3277
3278 -- If this is a spec without a body, check that generic parameters
3279 -- are referenced.
3280
3281 if not Body_Required (Parent (N)) then
3282 Check_References (Id);
3283 end if;
3284 end if;
3285
3286 -- If there is a specified storage pool in the context, create an
3287 -- aspect on the package declaration, so that it is used in any
3288 -- instance that does not override it.
3289
3290 if Present (Default_Pool) then
3291 declare
3292 ASN : Node_Id;
3293
3294 begin
3295 ASN :=
3296 Make_Aspect_Specification (Loc,
3297 Identifier => Make_Identifier (Loc, Name_Default_Storage_Pool),
3298 Expression => New_Copy (Default_Pool));
3299
3300 if No (Aspect_Specifications (Specification (N))) then
3301 Set_Aspect_Specifications (Specification (N), New_List (ASN));
3302 else
3303 Append (ASN, Aspect_Specifications (Specification (N)));
3304 end if;
3305 end;
3306 end if;
3307 end Analyze_Generic_Package_Declaration;
3308
3309 --------------------------------------------
3310 -- Analyze_Generic_Subprogram_Declaration --
3311 --------------------------------------------
3312
3313 procedure Analyze_Generic_Subprogram_Declaration (N : Node_Id) is
3314 Formals : List_Id;
3315 Id : Entity_Id;
3316 New_N : Node_Id;
3317 Result_Type : Entity_Id;
3318 Save_Parent : Node_Id;
3319 Spec : Node_Id;
3320 Typ : Entity_Id;
3321
3322 begin
3323 Check_SPARK_05_Restriction ("generic is not allowed", N);
3324
3325 -- Create copy of generic unit, and save for instantiation. If the unit
3326 -- is a child unit, do not copy the specifications for the parent, which
3327 -- are not part of the generic tree.
3328
3329 Save_Parent := Parent_Spec (N);
3330 Set_Parent_Spec (N, Empty);
3331
3332 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
3333 Set_Parent_Spec (New_N, Save_Parent);
3334 Rewrite (N, New_N);
3335
3336 -- Once the contents of the generic copy and the template are swapped,
3337 -- do the same for their respective aspect specifications.
3338
3339 Exchange_Aspects (N, New_N);
3340
3341 -- Collect all contract-related source pragmas found within the template
3342 -- and attach them to the contract of the subprogram spec. This contract
3343 -- is used in the capture of global references within annotations.
3344
3345 Create_Generic_Contract (N);
3346
3347 Spec := Specification (N);
3348 Id := Defining_Entity (Spec);
3349 Generate_Definition (Id);
3350
3351 if Nkind (Id) = N_Defining_Operator_Symbol then
3352 Error_Msg_N
3353 ("operator symbol not allowed for generic subprogram", Id);
3354 end if;
3355
3356 Start_Generic;
3357
3358 Enter_Name (Id);
3359 Set_Scope_Depth_Value (Id, Scope_Depth (Current_Scope) + 1);
3360
3361 -- Analyze the aspects of the generic copy to ensure that all generated
3362 -- pragmas (if any) perform their semantic effects.
3363
3364 if Has_Aspects (N) then
3365 Analyze_Aspect_Specifications (N, Id);
3366 end if;
3367
3368 Push_Scope (Id);
3369 Enter_Generic_Scope (Id);
3370 Set_Inner_Instances (Id, New_Elmt_List);
3371 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3372
3373 Analyze_Generic_Formal_Part (N);
3374
3375 Formals := Parameter_Specifications (Spec);
3376
3377 if Nkind (Spec) = N_Function_Specification then
3378 Set_Ekind (Id, E_Generic_Function);
3379 else
3380 Set_Ekind (Id, E_Generic_Procedure);
3381 end if;
3382
3383 if Present (Formals) then
3384 Process_Formals (Formals, Spec);
3385 end if;
3386
3387 if Nkind (Spec) = N_Function_Specification then
3388 if Nkind (Result_Definition (Spec)) = N_Access_Definition then
3389 Result_Type := Access_Definition (Spec, Result_Definition (Spec));
3390 Set_Etype (Id, Result_Type);
3391
3392 -- Check restriction imposed by AI05-073: a generic function
3393 -- cannot return an abstract type or an access to such.
3394
3395 -- This is a binding interpretation should it apply to earlier
3396 -- versions of Ada as well as Ada 2012???
3397
3398 if Is_Abstract_Type (Designated_Type (Result_Type))
3399 and then Ada_Version >= Ada_2012
3400 then
3401 Error_Msg_N
3402 ("generic function cannot have an access result "
3403 & "that designates an abstract type", Spec);
3404 end if;
3405
3406 else
3407 Find_Type (Result_Definition (Spec));
3408 Typ := Entity (Result_Definition (Spec));
3409
3410 if Is_Abstract_Type (Typ)
3411 and then Ada_Version >= Ada_2012
3412 then
3413 Error_Msg_N
3414 ("generic function cannot have abstract result type", Spec);
3415 end if;
3416
3417 -- If a null exclusion is imposed on the result type, then create
3418 -- a null-excluding itype (an access subtype) and use it as the
3419 -- function's Etype.
3420
3421 if Is_Access_Type (Typ)
3422 and then Null_Exclusion_Present (Spec)
3423 then
3424 Set_Etype (Id,
3425 Create_Null_Excluding_Itype
3426 (T => Typ,
3427 Related_Nod => Spec,
3428 Scope_Id => Defining_Unit_Name (Spec)));
3429 else
3430 Set_Etype (Id, Typ);
3431 end if;
3432 end if;
3433
3434 else
3435 Set_Etype (Id, Standard_Void_Type);
3436 end if;
3437
3438 -- A generic subprogram declared within a Ghost region is rendered Ghost
3439 -- (SPARK RM 6.9(2)).
3440
3441 if Ghost_Mode > None then
3442 Set_Is_Ghost_Entity (Id);
3443 end if;
3444
3445 -- For a library unit, we have reconstructed the entity for the unit,
3446 -- and must reset it in the library tables. We also make sure that
3447 -- Body_Required is set properly in the original compilation unit node.
3448
3449 if Nkind (Parent (N)) = N_Compilation_Unit then
3450 Set_Cunit_Entity (Current_Sem_Unit, Id);
3451 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
3452 end if;
3453
3454 Set_Categorization_From_Pragmas (N);
3455 Validate_Categorization_Dependency (N, Id);
3456
3457 -- Capture all global references that occur within the profile of the
3458 -- generic subprogram. Aspects are not part of this processing because
3459 -- they must be delayed. If processed now, Save_Global_References will
3460 -- destroy the Associated_Node links and prevent the capture of global
3461 -- references when the contract of the generic subprogram is analyzed.
3462
3463 Save_Global_References (Original_Node (N));
3464
3465 End_Generic;
3466 End_Scope;
3467 Exit_Generic_Scope (Id);
3468 Generate_Reference_To_Formals (Id);
3469
3470 List_Inherited_Pre_Post_Aspects (Id);
3471 end Analyze_Generic_Subprogram_Declaration;
3472
3473 -----------------------------------
3474 -- Analyze_Package_Instantiation --
3475 -----------------------------------
3476
3477 procedure Analyze_Package_Instantiation (N : Node_Id) is
3478 Loc : constant Source_Ptr := Sloc (N);
3479 Gen_Id : constant Node_Id := Name (N);
3480
3481 Act_Decl : Node_Id;
3482 Act_Decl_Name : Node_Id;
3483 Act_Decl_Id : Entity_Id;
3484 Act_Spec : Node_Id;
3485 Act_Tree : Node_Id;
3486
3487 Gen_Decl : Node_Id;
3488 Gen_Spec : Node_Id;
3489 Gen_Unit : Entity_Id;
3490
3491 Is_Actual_Pack : constant Boolean :=
3492 Is_Internal (Defining_Entity (N));
3493
3494 Env_Installed : Boolean := False;
3495 Parent_Installed : Boolean := False;
3496 Renaming_List : List_Id;
3497 Unit_Renaming : Node_Id;
3498 Needs_Body : Boolean;
3499 Inline_Now : Boolean := False;
3500 Has_Inline_Always : Boolean := False;
3501
3502 Save_IPSM : constant Boolean := Ignore_Pragma_SPARK_Mode;
3503 -- Save flag Ignore_Pragma_SPARK_Mode for restore on exit
3504
3505 Save_SM : constant SPARK_Mode_Type := SPARK_Mode;
3506 Save_SMP : constant Node_Id := SPARK_Mode_Pragma;
3507 -- Save the SPARK_Mode-related data for restore on exit
3508
3509 Save_Style_Check : constant Boolean := Style_Check;
3510 -- Save style check mode for restore on exit
3511
3512 procedure Delay_Descriptors (E : Entity_Id);
3513 -- Delay generation of subprogram descriptors for given entity
3514
3515 function Might_Inline_Subp return Boolean;
3516 -- If inlining is active and the generic contains inlined subprograms,
3517 -- we instantiate the body. This may cause superfluous instantiations,
3518 -- but it is simpler than detecting the need for the body at the point
3519 -- of inlining, when the context of the instance is not available.
3520
3521 -----------------------
3522 -- Delay_Descriptors --
3523 -----------------------
3524
3525 procedure Delay_Descriptors (E : Entity_Id) is
3526 begin
3527 if not Delay_Subprogram_Descriptors (E) then
3528 Set_Delay_Subprogram_Descriptors (E);
3529 Pending_Descriptor.Append (E);
3530 end if;
3531 end Delay_Descriptors;
3532
3533 -----------------------
3534 -- Might_Inline_Subp --
3535 -----------------------
3536
3537 function Might_Inline_Subp return Boolean is
3538 E : Entity_Id;
3539
3540 begin
3541 if not Inline_Processing_Required then
3542 return False;
3543
3544 else
3545 E := First_Entity (Gen_Unit);
3546 while Present (E) loop
3547 if Is_Subprogram (E) and then Is_Inlined (E) then
3548 -- Remember if there are any subprograms with Inline_Always
3549
3550 if Has_Pragma_Inline_Always (E) then
3551 Has_Inline_Always := True;
3552 end if;
3553
3554 return True;
3555 end if;
3556
3557 Next_Entity (E);
3558 end loop;
3559 end if;
3560
3561 return False;
3562 end Might_Inline_Subp;
3563
3564 -- Local declarations
3565
3566 Vis_Prims_List : Elist_Id := No_Elist;
3567 -- List of primitives made temporarily visible in the instantiation
3568 -- to match the visibility of the formal type
3569
3570 -- Start of processing for Analyze_Package_Instantiation
3571
3572 begin
3573 Check_SPARK_05_Restriction ("generic is not allowed", N);
3574
3575 -- Very first thing: check for Text_IO sp[ecial unit in case we are
3576 -- instantiating one of the children of [[Wide_]Wide_]Text_IO.
3577
3578 Check_Text_IO_Special_Unit (Name (N));
3579
3580 -- Make node global for error reporting
3581
3582 Instantiation_Node := N;
3583
3584 -- Turn off style checking in instances. If the check is enabled on the
3585 -- generic unit, a warning in an instance would just be noise. If not
3586 -- enabled on the generic, then a warning in an instance is just wrong.
3587
3588 Style_Check := False;
3589
3590 -- Case of instantiation of a generic package
3591
3592 if Nkind (N) = N_Package_Instantiation then
3593 Act_Decl_Id := New_Copy (Defining_Entity (N));
3594 Set_Comes_From_Source (Act_Decl_Id, True);
3595
3596 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name then
3597 Act_Decl_Name :=
3598 Make_Defining_Program_Unit_Name (Loc,
3599 Name =>
3600 New_Copy_Tree (Name (Defining_Unit_Name (N))),
3601 Defining_Identifier => Act_Decl_Id);
3602 else
3603 Act_Decl_Name := Act_Decl_Id;
3604 end if;
3605
3606 -- Case of instantiation of a formal package
3607
3608 else
3609 Act_Decl_Id := Defining_Identifier (N);
3610 Act_Decl_Name := Act_Decl_Id;
3611 end if;
3612
3613 Generate_Definition (Act_Decl_Id);
3614 Set_Ekind (Act_Decl_Id, E_Package);
3615
3616 -- Initialize list of incomplete actuals before analysis
3617
3618 Set_Incomplete_Actuals (Act_Decl_Id, New_Elmt_List);
3619
3620 Preanalyze_Actuals (N, Act_Decl_Id);
3621
3622 Init_Env;
3623 Env_Installed := True;
3624
3625 -- Reset renaming map for formal types. The mapping is established
3626 -- when analyzing the generic associations, but some mappings are
3627 -- inherited from formal packages of parent units, and these are
3628 -- constructed when the parents are installed.
3629
3630 Generic_Renamings.Set_Last (0);
3631 Generic_Renamings_HTable.Reset;
3632
3633 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
3634 Gen_Unit := Entity (Gen_Id);
3635
3636 -- Verify that it is the name of a generic package
3637
3638 -- A visibility glitch: if the instance is a child unit and the generic
3639 -- is the generic unit of a parent instance (i.e. both the parent and
3640 -- the child units are instances of the same package) the name now
3641 -- denotes the renaming within the parent, not the intended generic
3642 -- unit. See if there is a homonym that is the desired generic. The
3643 -- renaming declaration must be visible inside the instance of the
3644 -- child, but not when analyzing the name in the instantiation itself.
3645
3646 if Ekind (Gen_Unit) = E_Package
3647 and then Present (Renamed_Entity (Gen_Unit))
3648 and then In_Open_Scopes (Renamed_Entity (Gen_Unit))
3649 and then Is_Generic_Instance (Renamed_Entity (Gen_Unit))
3650 and then Present (Homonym (Gen_Unit))
3651 then
3652 Gen_Unit := Homonym (Gen_Unit);
3653 end if;
3654
3655 if Etype (Gen_Unit) = Any_Type then
3656 Restore_Env;
3657 goto Leave;
3658
3659 elsif Ekind (Gen_Unit) /= E_Generic_Package then
3660
3661 -- Ada 2005 (AI-50217): Cannot use instance in limited with_clause
3662
3663 if From_Limited_With (Gen_Unit) then
3664 Error_Msg_N
3665 ("cannot instantiate a limited withed package", Gen_Id);
3666 else
3667 Error_Msg_NE
3668 ("& is not the name of a generic package", Gen_Id, Gen_Unit);
3669 end if;
3670
3671 Restore_Env;
3672 goto Leave;
3673 end if;
3674
3675 if In_Extended_Main_Source_Unit (N) then
3676 Set_Is_Instantiated (Gen_Unit);
3677 Generate_Reference (Gen_Unit, N);
3678
3679 if Present (Renamed_Object (Gen_Unit)) then
3680 Set_Is_Instantiated (Renamed_Object (Gen_Unit));
3681 Generate_Reference (Renamed_Object (Gen_Unit), N);
3682 end if;
3683 end if;
3684
3685 if Nkind (Gen_Id) = N_Identifier
3686 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
3687 then
3688 Error_Msg_NE
3689 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
3690
3691 elsif Nkind (Gen_Id) = N_Expanded_Name
3692 and then Is_Child_Unit (Gen_Unit)
3693 and then Nkind (Prefix (Gen_Id)) = N_Identifier
3694 and then Chars (Act_Decl_Id) = Chars (Prefix (Gen_Id))
3695 then
3696 Error_Msg_N
3697 ("& is hidden within declaration of instance ", Prefix (Gen_Id));
3698 end if;
3699
3700 Set_Entity (Gen_Id, Gen_Unit);
3701
3702 -- If generic is a renaming, get original generic unit
3703
3704 if Present (Renamed_Object (Gen_Unit))
3705 and then Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Package
3706 then
3707 Gen_Unit := Renamed_Object (Gen_Unit);
3708 end if;
3709
3710 -- Verify that there are no circular instantiations
3711
3712 if In_Open_Scopes (Gen_Unit) then
3713 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
3714 Restore_Env;
3715 goto Leave;
3716
3717 elsif Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
3718 Error_Msg_Node_2 := Current_Scope;
3719 Error_Msg_NE
3720 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
3721 Circularity_Detected := True;
3722 Restore_Env;
3723 goto Leave;
3724
3725 else
3726 -- If the context of the instance is subject to SPARK_Mode "off",
3727 -- set the global flag which signals Analyze_Pragma to ignore all
3728 -- SPARK_Mode pragmas within the instance.
3729
3730 if SPARK_Mode = Off then
3731 Ignore_Pragma_SPARK_Mode := True;
3732 end if;
3733
3734 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
3735 Gen_Spec := Specification (Gen_Decl);
3736
3737 -- Initialize renamings map, for error checking, and the list that
3738 -- holds private entities whose views have changed between generic
3739 -- definition and instantiation. If this is the instance created to
3740 -- validate an actual package, the instantiation environment is that
3741 -- of the enclosing instance.
3742
3743 Create_Instantiation_Source (N, Gen_Unit, False, S_Adjustment);
3744
3745 -- Copy original generic tree, to produce text for instantiation
3746
3747 Act_Tree :=
3748 Copy_Generic_Node
3749 (Original_Node (Gen_Decl), Empty, Instantiating => True);
3750
3751 Act_Spec := Specification (Act_Tree);
3752
3753 -- If this is the instance created to validate an actual package,
3754 -- only the formals matter, do not examine the package spec itself.
3755
3756 if Is_Actual_Pack then
3757 Set_Visible_Declarations (Act_Spec, New_List);
3758 Set_Private_Declarations (Act_Spec, New_List);
3759 end if;
3760
3761 Renaming_List :=
3762 Analyze_Associations
3763 (I_Node => N,
3764 Formals => Generic_Formal_Declarations (Act_Tree),
3765 F_Copy => Generic_Formal_Declarations (Gen_Decl));
3766
3767 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
3768
3769 Set_Instance_Env (Gen_Unit, Act_Decl_Id);
3770 Set_Defining_Unit_Name (Act_Spec, Act_Decl_Name);
3771 Set_Is_Generic_Instance (Act_Decl_Id);
3772 Set_Generic_Parent (Act_Spec, Gen_Unit);
3773
3774 -- References to the generic in its own declaration or its body are
3775 -- references to the instance. Add a renaming declaration for the
3776 -- generic unit itself. This declaration, as well as the renaming
3777 -- declarations for the generic formals, must remain private to the
3778 -- unit: the formals, because this is the language semantics, and
3779 -- the unit because its use is an artifact of the implementation.
3780
3781 Unit_Renaming :=
3782 Make_Package_Renaming_Declaration (Loc,
3783 Defining_Unit_Name =>
3784 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
3785 Name => New_Occurrence_Of (Act_Decl_Id, Loc));
3786
3787 Append (Unit_Renaming, Renaming_List);
3788
3789 -- The renaming declarations are the first local declarations of the
3790 -- new unit.
3791
3792 if Is_Non_Empty_List (Visible_Declarations (Act_Spec)) then
3793 Insert_List_Before
3794 (First (Visible_Declarations (Act_Spec)), Renaming_List);
3795 else
3796 Set_Visible_Declarations (Act_Spec, Renaming_List);
3797 end if;
3798
3799 Act_Decl := Make_Package_Declaration (Loc, Specification => Act_Spec);
3800
3801 -- Propagate the aspect specifications from the package declaration
3802 -- template to the instantiated version of the package declaration.
3803
3804 if Has_Aspects (Act_Tree) then
3805 Set_Aspect_Specifications (Act_Decl,
3806 New_Copy_List_Tree (Aspect_Specifications (Act_Tree)));
3807 end if;
3808
3809 -- The generic may have a generated Default_Storage_Pool aspect,
3810 -- set at the point of generic declaration. If the instance has
3811 -- that aspect, it overrides the one inherited from the generic.
3812
3813 if Has_Aspects (Gen_Spec) then
3814 if No (Aspect_Specifications (N)) then
3815 Set_Aspect_Specifications (N,
3816 (New_Copy_List_Tree
3817 (Aspect_Specifications (Gen_Spec))));
3818
3819 else
3820 declare
3821 ASN1, ASN2 : Node_Id;
3822
3823 begin
3824 ASN1 := First (Aspect_Specifications (N));
3825 while Present (ASN1) loop
3826 if Chars (Identifier (ASN1)) = Name_Default_Storage_Pool
3827 then
3828 -- If generic carries a default storage pool, remove
3829 -- it in favor of the instance one.
3830
3831 ASN2 := First (Aspect_Specifications (Gen_Spec));
3832 while Present (ASN2) loop
3833 if Chars (Identifier (ASN2)) =
3834 Name_Default_Storage_Pool
3835 then
3836 Remove (ASN2);
3837 exit;
3838 end if;
3839
3840 Next (ASN2);
3841 end loop;
3842 end if;
3843
3844 Next (ASN1);
3845 end loop;
3846
3847 Prepend_List_To (Aspect_Specifications (N),
3848 (New_Copy_List_Tree
3849 (Aspect_Specifications (Gen_Spec))));
3850 end;
3851 end if;
3852 end if;
3853
3854 -- Save the instantiation node, for subsequent instantiation of the
3855 -- body, if there is one and we are generating code for the current
3856 -- unit. Mark unit as having a body (avoids premature error message).
3857
3858 -- We instantiate the body if we are generating code, if we are
3859 -- generating cross-reference information, or if we are building
3860 -- trees for ASIS use or GNATprove use.
3861
3862 declare
3863 Enclosing_Body_Present : Boolean := False;
3864 -- If the generic unit is not a compilation unit, then a body may
3865 -- be present in its parent even if none is required. We create a
3866 -- tentative pending instantiation for the body, which will be
3867 -- discarded if none is actually present.
3868
3869 Scop : Entity_Id;
3870
3871 begin
3872 if Scope (Gen_Unit) /= Standard_Standard
3873 and then not Is_Child_Unit (Gen_Unit)
3874 then
3875 Scop := Scope (Gen_Unit);
3876 while Present (Scop) and then Scop /= Standard_Standard loop
3877 if Unit_Requires_Body (Scop) then
3878 Enclosing_Body_Present := True;
3879 exit;
3880
3881 elsif In_Open_Scopes (Scop)
3882 and then In_Package_Body (Scop)
3883 then
3884 Enclosing_Body_Present := True;
3885 exit;
3886 end if;
3887
3888 exit when Is_Compilation_Unit (Scop);
3889 Scop := Scope (Scop);
3890 end loop;
3891 end if;
3892
3893 -- If front-end inlining is enabled or there are any subprograms
3894 -- marked with Inline_Always, and this is a unit for which code
3895 -- will be generated, we instantiate the body at once.
3896
3897 -- This is done if the instance is not the main unit, and if the
3898 -- generic is not a child unit of another generic, to avoid scope
3899 -- problems and the reinstallation of parent instances.
3900
3901 if Expander_Active
3902 and then (not Is_Child_Unit (Gen_Unit)
3903 or else not Is_Generic_Unit (Scope (Gen_Unit)))
3904 and then Might_Inline_Subp
3905 and then not Is_Actual_Pack
3906 then
3907 if not Back_End_Inlining
3908 and then (Front_End_Inlining or else Has_Inline_Always)
3909 and then (Is_In_Main_Unit (N)
3910 or else In_Main_Context (Current_Scope))
3911 and then Nkind (Parent (N)) /= N_Compilation_Unit
3912 then
3913 Inline_Now := True;
3914
3915 -- In configurable_run_time mode we force the inlining of
3916 -- predefined subprograms marked Inline_Always, to minimize
3917 -- the use of the run-time library.
3918
3919 elsif Is_Predefined_File_Name
3920 (Unit_File_Name (Get_Source_Unit (Gen_Decl)))
3921 and then Configurable_Run_Time_Mode
3922 and then Nkind (Parent (N)) /= N_Compilation_Unit
3923 then
3924 Inline_Now := True;
3925 end if;
3926
3927 -- If the current scope is itself an instance within a child
3928 -- unit, there will be duplications in the scope stack, and the
3929 -- unstacking mechanism in Inline_Instance_Body will fail.
3930 -- This loses some rare cases of optimization, and might be
3931 -- improved some day, if we can find a proper abstraction for
3932 -- "the complete compilation context" that can be saved and
3933 -- restored. ???
3934
3935 if Is_Generic_Instance (Current_Scope) then
3936 declare
3937 Curr_Unit : constant Entity_Id :=
3938 Cunit_Entity (Current_Sem_Unit);
3939 begin
3940 if Curr_Unit /= Current_Scope
3941 and then Is_Child_Unit (Curr_Unit)
3942 then
3943 Inline_Now := False;
3944 end if;
3945 end;
3946 end if;
3947 end if;
3948
3949 Needs_Body :=
3950 (Unit_Requires_Body (Gen_Unit)
3951 or else Enclosing_Body_Present
3952 or else Present (Corresponding_Body (Gen_Decl)))
3953 and then (Is_In_Main_Unit (N) or else Might_Inline_Subp)
3954 and then not Is_Actual_Pack
3955 and then not Inline_Now
3956 and then (Operating_Mode = Generate_Code
3957
3958 -- Need comment for this check ???
3959
3960 or else (Operating_Mode = Check_Semantics
3961 and then (ASIS_Mode or GNATprove_Mode)));
3962
3963 -- If front-end inlining is enabled or there are any subprograms
3964 -- marked with Inline_Always, do not instantiate body when within
3965 -- a generic context.
3966
3967 if ((Front_End_Inlining or else Has_Inline_Always)
3968 and then not Expander_Active)
3969 or else Is_Generic_Unit (Cunit_Entity (Main_Unit))
3970 then
3971 Needs_Body := False;
3972 end if;
3973
3974 -- If the current context is generic, and the package being
3975 -- instantiated is declared within a formal package, there is no
3976 -- body to instantiate until the enclosing generic is instantiated
3977 -- and there is an actual for the formal package. If the formal
3978 -- package has parameters, we build a regular package instance for
3979 -- it, that precedes the original formal package declaration.
3980
3981 if In_Open_Scopes (Scope (Scope (Gen_Unit))) then
3982 declare
3983 Decl : constant Node_Id :=
3984 Original_Node
3985 (Unit_Declaration_Node (Scope (Gen_Unit)));
3986 begin
3987 if Nkind (Decl) = N_Formal_Package_Declaration
3988 or else (Nkind (Decl) = N_Package_Declaration
3989 and then Is_List_Member (Decl)
3990 and then Present (Next (Decl))
3991 and then
3992 Nkind (Next (Decl)) =
3993 N_Formal_Package_Declaration)
3994 then
3995 Needs_Body := False;
3996 end if;
3997 end;
3998 end if;
3999 end;
4000
4001 -- For RCI unit calling stubs, we omit the instance body if the
4002 -- instance is the RCI library unit itself.
4003
4004 -- However there is a special case for nested instances: in this case
4005 -- we do generate the instance body, as it might be required, e.g.
4006 -- because it provides stream attributes for some type used in the
4007 -- profile of a remote subprogram. This is consistent with 12.3(12),
4008 -- which indicates that the instance body occurs at the place of the
4009 -- instantiation, and thus is part of the RCI declaration, which is
4010 -- present on all client partitions (this is E.2.3(18)).
4011
4012 -- Note that AI12-0002 may make it illegal at some point to have
4013 -- stream attributes defined in an RCI unit, in which case this
4014 -- special case will become unnecessary. In the meantime, there
4015 -- is known application code in production that depends on this
4016 -- being possible, so we definitely cannot eliminate the body in
4017 -- the case of nested instances for the time being.
4018
4019 -- When we generate a nested instance body, calling stubs for any
4020 -- relevant subprogram will be be inserted immediately after the
4021 -- subprogram declarations, and will take precedence over the
4022 -- subsequent (original) body. (The stub and original body will be
4023 -- complete homographs, but this is permitted in an instance).
4024 -- (Could we do better and remove the original body???)
4025
4026 if Distribution_Stub_Mode = Generate_Caller_Stub_Body
4027 and then Comes_From_Source (N)
4028 and then Nkind (Parent (N)) = N_Compilation_Unit
4029 then
4030 Needs_Body := False;
4031 end if;
4032
4033 if Needs_Body then
4034
4035 -- Here is a defence against a ludicrous number of instantiations
4036 -- caused by a circular set of instantiation attempts.
4037
4038 if Pending_Instantiations.Last > Maximum_Instantiations then
4039 Error_Msg_Uint_1 := UI_From_Int (Maximum_Instantiations);
4040 Error_Msg_N ("too many instantiations, exceeds max of^", N);
4041 Error_Msg_N ("\limit can be changed using -gnateinn switch", N);
4042 raise Unrecoverable_Error;
4043 end if;
4044
4045 -- Indicate that the enclosing scopes contain an instantiation,
4046 -- and that cleanup actions should be delayed until after the
4047 -- instance body is expanded.
4048
4049 Check_Forward_Instantiation (Gen_Decl);
4050 if Nkind (N) = N_Package_Instantiation then
4051 declare
4052 Enclosing_Master : Entity_Id;
4053
4054 begin
4055 -- Loop to search enclosing masters
4056
4057 Enclosing_Master := Current_Scope;
4058 Scope_Loop : while Enclosing_Master /= Standard_Standard loop
4059 if Ekind (Enclosing_Master) = E_Package then
4060 if Is_Compilation_Unit (Enclosing_Master) then
4061 if In_Package_Body (Enclosing_Master) then
4062 Delay_Descriptors
4063 (Body_Entity (Enclosing_Master));
4064 else
4065 Delay_Descriptors
4066 (Enclosing_Master);
4067 end if;
4068
4069 exit Scope_Loop;
4070
4071 else
4072 Enclosing_Master := Scope (Enclosing_Master);
4073 end if;
4074
4075 elsif Is_Generic_Unit (Enclosing_Master)
4076 or else Ekind (Enclosing_Master) = E_Void
4077 then
4078 -- Cleanup actions will eventually be performed on the
4079 -- enclosing subprogram or package instance, if any.
4080 -- Enclosing scope is void in the formal part of a
4081 -- generic subprogram.
4082
4083 exit Scope_Loop;
4084
4085 else
4086 if Ekind (Enclosing_Master) = E_Entry
4087 and then
4088 Ekind (Scope (Enclosing_Master)) = E_Protected_Type
4089 then
4090 if not Expander_Active then
4091 exit Scope_Loop;
4092 else
4093 Enclosing_Master :=
4094 Protected_Body_Subprogram (Enclosing_Master);
4095 end if;
4096 end if;
4097
4098 Set_Delay_Cleanups (Enclosing_Master);
4099
4100 while Ekind (Enclosing_Master) = E_Block loop
4101 Enclosing_Master := Scope (Enclosing_Master);
4102 end loop;
4103
4104 if Is_Subprogram (Enclosing_Master) then
4105 Delay_Descriptors (Enclosing_Master);
4106
4107 elsif Is_Task_Type (Enclosing_Master) then
4108 declare
4109 TBP : constant Node_Id :=
4110 Get_Task_Body_Procedure
4111 (Enclosing_Master);
4112 begin
4113 if Present (TBP) then
4114 Delay_Descriptors (TBP);
4115 Set_Delay_Cleanups (TBP);
4116 end if;
4117 end;
4118 end if;
4119
4120 exit Scope_Loop;
4121 end if;
4122 end loop Scope_Loop;
4123 end;
4124
4125 -- Make entry in table
4126
4127 Pending_Instantiations.Append
4128 ((Inst_Node => N,
4129 Act_Decl => Act_Decl,
4130 Expander_Status => Expander_Active,
4131 Current_Sem_Unit => Current_Sem_Unit,
4132 Scope_Suppress => Scope_Suppress,
4133 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
4134 Version => Ada_Version,
4135 Version_Pragma => Ada_Version_Pragma,
4136 Warnings => Save_Warnings,
4137 SPARK_Mode => SPARK_Mode,
4138 SPARK_Mode_Pragma => SPARK_Mode_Pragma));
4139 end if;
4140 end if;
4141
4142 Set_Categorization_From_Pragmas (Act_Decl);
4143
4144 if Parent_Installed then
4145 Hide_Current_Scope;
4146 end if;
4147
4148 Set_Instance_Spec (N, Act_Decl);
4149
4150 -- If not a compilation unit, insert the package declaration before
4151 -- the original instantiation node.
4152
4153 if Nkind (Parent (N)) /= N_Compilation_Unit then
4154 Mark_Rewrite_Insertion (Act_Decl);
4155 Insert_Before (N, Act_Decl);
4156
4157 if Has_Aspects (N) then
4158 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4159
4160 -- The pragma created for a Default_Storage_Pool aspect must
4161 -- appear ahead of the declarations in the instance spec.
4162 -- Analysis has placed it after the instance node, so remove
4163 -- it and reinsert it properly now.
4164
4165 declare
4166 ASN : constant Node_Id := First (Aspect_Specifications (N));
4167 A_Name : constant Name_Id := Chars (Identifier (ASN));
4168 Decl : Node_Id;
4169
4170 begin
4171 if A_Name = Name_Default_Storage_Pool then
4172 if No (Visible_Declarations (Act_Spec)) then
4173 Set_Visible_Declarations (Act_Spec, New_List);
4174 end if;
4175
4176 Decl := Next (N);
4177 while Present (Decl) loop
4178 if Nkind (Decl) = N_Pragma then
4179 Remove (Decl);
4180 Prepend (Decl, Visible_Declarations (Act_Spec));
4181 exit;
4182 end if;
4183
4184 Next (Decl);
4185 end loop;
4186 end if;
4187 end;
4188 end if;
4189
4190 Analyze (Act_Decl);
4191
4192 -- For an instantiation that is a compilation unit, place
4193 -- declaration on current node so context is complete for analysis
4194 -- (including nested instantiations). If this is the main unit,
4195 -- the declaration eventually replaces the instantiation node.
4196 -- If the instance body is created later, it replaces the
4197 -- instance node, and the declaration is attached to it
4198 -- (see Build_Instance_Compilation_Unit_Nodes).
4199
4200 else
4201 if Cunit_Entity (Current_Sem_Unit) = Defining_Entity (N) then
4202
4203 -- The entity for the current unit is the newly created one,
4204 -- and all semantic information is attached to it.
4205
4206 Set_Cunit_Entity (Current_Sem_Unit, Act_Decl_Id);
4207
4208 -- If this is the main unit, replace the main entity as well
4209
4210 if Current_Sem_Unit = Main_Unit then
4211 Main_Unit_Entity := Act_Decl_Id;
4212 end if;
4213 end if;
4214
4215 Set_Unit (Parent (N), Act_Decl);
4216 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
4217 Set_Package_Instantiation (Act_Decl_Id, N);
4218
4219 -- Process aspect specifications of the instance node, if any, to
4220 -- take into account categorization pragmas before analyzing the
4221 -- instance.
4222
4223 if Has_Aspects (N) then
4224 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4225 end if;
4226
4227 Analyze (Act_Decl);
4228 Set_Unit (Parent (N), N);
4229 Set_Body_Required (Parent (N), False);
4230
4231 -- We never need elaboration checks on instantiations, since by
4232 -- definition, the body instantiation is elaborated at the same
4233 -- time as the spec instantiation.
4234
4235 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
4236 Set_Kill_Elaboration_Checks (Act_Decl_Id);
4237 end if;
4238
4239 Check_Elab_Instantiation (N);
4240
4241 if ABE_Is_Certain (N) and then Needs_Body then
4242 Pending_Instantiations.Decrement_Last;
4243 end if;
4244
4245 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
4246
4247 Set_First_Private_Entity (Defining_Unit_Name (Unit_Renaming),
4248 First_Private_Entity (Act_Decl_Id));
4249
4250 -- If the instantiation will receive a body, the unit will be
4251 -- transformed into a package body, and receive its own elaboration
4252 -- entity. Otherwise, the nature of the unit is now a package
4253 -- declaration.
4254
4255 if Nkind (Parent (N)) = N_Compilation_Unit
4256 and then not Needs_Body
4257 then
4258 Rewrite (N, Act_Decl);
4259 end if;
4260
4261 if Present (Corresponding_Body (Gen_Decl))
4262 or else Unit_Requires_Body (Gen_Unit)
4263 then
4264 Set_Has_Completion (Act_Decl_Id);
4265 end if;
4266
4267 Check_Formal_Packages (Act_Decl_Id);
4268
4269 Restore_Hidden_Primitives (Vis_Prims_List);
4270 Restore_Private_Views (Act_Decl_Id);
4271
4272 Inherit_Context (Gen_Decl, N);
4273
4274 if Parent_Installed then
4275 Remove_Parent;
4276 end if;
4277
4278 Restore_Env;
4279 Env_Installed := False;
4280 end if;
4281
4282 Validate_Categorization_Dependency (N, Act_Decl_Id);
4283
4284 -- There used to be a check here to prevent instantiations in local
4285 -- contexts if the No_Local_Allocators restriction was active. This
4286 -- check was removed by a binding interpretation in AI-95-00130/07,
4287 -- but we retain the code for documentation purposes.
4288
4289 -- if Ekind (Act_Decl_Id) /= E_Void
4290 -- and then not Is_Library_Level_Entity (Act_Decl_Id)
4291 -- then
4292 -- Check_Restriction (No_Local_Allocators, N);
4293 -- end if;
4294
4295 if Inline_Now then
4296 Inline_Instance_Body (N, Gen_Unit, Act_Decl);
4297 end if;
4298
4299 -- The following is a tree patch for ASIS: ASIS needs separate nodes to
4300 -- be used as defining identifiers for a formal package and for the
4301 -- corresponding expanded package.
4302
4303 if Nkind (N) = N_Formal_Package_Declaration then
4304 Act_Decl_Id := New_Copy (Defining_Entity (N));
4305 Set_Comes_From_Source (Act_Decl_Id, True);
4306 Set_Is_Generic_Instance (Act_Decl_Id, False);
4307 Set_Defining_Identifier (N, Act_Decl_Id);
4308 end if;
4309
4310 Ignore_Pragma_SPARK_Mode := Save_IPSM;
4311 SPARK_Mode := Save_SM;
4312 SPARK_Mode_Pragma := Save_SMP;
4313 Style_Check := Save_Style_Check;
4314
4315 if SPARK_Mode = On then
4316 Dynamic_Elaboration_Checks := False;
4317 end if;
4318
4319 -- Check that if N is an instantiation of System.Dim_Float_IO or
4320 -- System.Dim_Integer_IO, the formal type has a dimension system.
4321
4322 if Nkind (N) = N_Package_Instantiation
4323 and then Is_Dim_IO_Package_Instantiation (N)
4324 then
4325 declare
4326 Assoc : constant Node_Id := First (Generic_Associations (N));
4327 begin
4328 if not Has_Dimension_System
4329 (Etype (Explicit_Generic_Actual_Parameter (Assoc)))
4330 then
4331 Error_Msg_N ("type with a dimension system expected", Assoc);
4332 end if;
4333 end;
4334 end if;
4335
4336 <<Leave>>
4337 if Has_Aspects (N) and then Nkind (Parent (N)) /= N_Compilation_Unit then
4338 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4339 end if;
4340
4341 exception
4342 when Instantiation_Error =>
4343 if Parent_Installed then
4344 Remove_Parent;
4345 end if;
4346
4347 if Env_Installed then
4348 Restore_Env;
4349 end if;
4350
4351 Ignore_Pragma_SPARK_Mode := Save_IPSM;
4352 SPARK_Mode := Save_SM;
4353 SPARK_Mode_Pragma := Save_SMP;
4354 Style_Check := Save_Style_Check;
4355
4356 if SPARK_Mode = On then
4357 Dynamic_Elaboration_Checks := False;
4358 end if;
4359 end Analyze_Package_Instantiation;
4360
4361 --------------------------
4362 -- Inline_Instance_Body --
4363 --------------------------
4364
4365 procedure Inline_Instance_Body
4366 (N : Node_Id;
4367 Gen_Unit : Entity_Id;
4368 Act_Decl : Node_Id)
4369 is
4370 Curr_Comp : constant Node_Id := Cunit (Current_Sem_Unit);
4371 Curr_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
4372 Gen_Comp : constant Entity_Id :=
4373 Cunit_Entity (Get_Source_Unit (Gen_Unit));
4374
4375 Save_SM : constant SPARK_Mode_Type := SPARK_Mode;
4376 Save_SMP : constant Node_Id := SPARK_Mode_Pragma;
4377 -- Save all SPARK_Mode-related attributes as removing enclosing scopes
4378 -- to provide a clean environment for analysis of the inlined body will
4379 -- eliminate any previously set SPARK_Mode.
4380
4381 Scope_Stack_Depth : constant Int :=
4382 Scope_Stack.Last - Scope_Stack.First + 1;
4383
4384 Use_Clauses : array (1 .. Scope_Stack_Depth) of Node_Id;
4385 Instances : array (1 .. Scope_Stack_Depth) of Entity_Id;
4386 Inner_Scopes : array (1 .. Scope_Stack_Depth) of Entity_Id;
4387 Curr_Scope : Entity_Id := Empty;
4388 List : Elist_Id;
4389 Num_Inner : Int := 0;
4390 Num_Scopes : Int := 0;
4391 N_Instances : Int := 0;
4392 Removed : Boolean := False;
4393 S : Entity_Id;
4394 Vis : Boolean;
4395
4396 begin
4397 -- Case of generic unit defined in another unit. We must remove the
4398 -- complete context of the current unit to install that of the generic.
4399
4400 if Gen_Comp /= Cunit_Entity (Current_Sem_Unit) then
4401
4402 -- Add some comments for the following two loops ???
4403
4404 S := Current_Scope;
4405 while Present (S) and then S /= Standard_Standard loop
4406 loop
4407 Num_Scopes := Num_Scopes + 1;
4408
4409 Use_Clauses (Num_Scopes) :=
4410 (Scope_Stack.Table
4411 (Scope_Stack.Last - Num_Scopes + 1).
4412 First_Use_Clause);
4413 End_Use_Clauses (Use_Clauses (Num_Scopes));
4414
4415 exit when Scope_Stack.Last - Num_Scopes + 1 = Scope_Stack.First
4416 or else Scope_Stack.Table
4417 (Scope_Stack.Last - Num_Scopes).Entity = Scope (S);
4418 end loop;
4419
4420 exit when Is_Generic_Instance (S)
4421 and then (In_Package_Body (S)
4422 or else Ekind (S) = E_Procedure
4423 or else Ekind (S) = E_Function);
4424 S := Scope (S);
4425 end loop;
4426
4427 Vis := Is_Immediately_Visible (Gen_Comp);
4428
4429 -- Find and save all enclosing instances
4430
4431 S := Current_Scope;
4432
4433 while Present (S)
4434 and then S /= Standard_Standard
4435 loop
4436 if Is_Generic_Instance (S) then
4437 N_Instances := N_Instances + 1;
4438 Instances (N_Instances) := S;
4439
4440 exit when In_Package_Body (S);
4441 end if;
4442
4443 S := Scope (S);
4444 end loop;
4445
4446 -- Remove context of current compilation unit, unless we are within a
4447 -- nested package instantiation, in which case the context has been
4448 -- removed previously.
4449
4450 -- If current scope is the body of a child unit, remove context of
4451 -- spec as well. If an enclosing scope is an instance body, the
4452 -- context has already been removed, but the entities in the body
4453 -- must be made invisible as well.
4454
4455 S := Current_Scope;
4456 while Present (S) and then S /= Standard_Standard loop
4457 if Is_Generic_Instance (S)
4458 and then (In_Package_Body (S)
4459 or else Ekind_In (S, E_Procedure, E_Function))
4460 then
4461 -- We still have to remove the entities of the enclosing
4462 -- instance from direct visibility.
4463
4464 declare
4465 E : Entity_Id;
4466 begin
4467 E := First_Entity (S);
4468 while Present (E) loop
4469 Set_Is_Immediately_Visible (E, False);
4470 Next_Entity (E);
4471 end loop;
4472 end;
4473
4474 exit;
4475 end if;
4476
4477 if S = Curr_Unit
4478 or else (Ekind (Curr_Unit) = E_Package_Body
4479 and then S = Spec_Entity (Curr_Unit))
4480 or else (Ekind (Curr_Unit) = E_Subprogram_Body
4481 and then S = Corresponding_Spec
4482 (Unit_Declaration_Node (Curr_Unit)))
4483 then
4484 Removed := True;
4485
4486 -- Remove entities in current scopes from visibility, so that
4487 -- instance body is compiled in a clean environment.
4488
4489 List := Save_Scope_Stack (Handle_Use => False);
4490
4491 if Is_Child_Unit (S) then
4492
4493 -- Remove child unit from stack, as well as inner scopes.
4494 -- Removing the context of a child unit removes parent units
4495 -- as well.
4496
4497 while Current_Scope /= S loop
4498 Num_Inner := Num_Inner + 1;
4499 Inner_Scopes (Num_Inner) := Current_Scope;
4500 Pop_Scope;
4501 end loop;
4502
4503 Pop_Scope;
4504 Remove_Context (Curr_Comp);
4505 Curr_Scope := S;
4506
4507 else
4508 Remove_Context (Curr_Comp);
4509 end if;
4510
4511 if Ekind (Curr_Unit) = E_Package_Body then
4512 Remove_Context (Library_Unit (Curr_Comp));
4513 end if;
4514 end if;
4515
4516 S := Scope (S);
4517 end loop;
4518
4519 pragma Assert (Num_Inner < Num_Scopes);
4520
4521 -- The inlined package body must be analyzed with the SPARK_Mode of
4522 -- the enclosing context, otherwise the body may cause bogus errors
4523 -- if a configuration SPARK_Mode pragma in in effect.
4524
4525 Push_Scope (Standard_Standard);
4526 Scope_Stack.Table (Scope_Stack.Last).Is_Active_Stack_Base := True;
4527 Instantiate_Package_Body
4528 (Body_Info =>
4529 ((Inst_Node => N,
4530 Act_Decl => Act_Decl,
4531 Expander_Status => Expander_Active,
4532 Current_Sem_Unit => Current_Sem_Unit,
4533 Scope_Suppress => Scope_Suppress,
4534 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
4535 Version => Ada_Version,
4536 Version_Pragma => Ada_Version_Pragma,
4537 Warnings => Save_Warnings,
4538 SPARK_Mode => Save_SM,
4539 SPARK_Mode_Pragma => Save_SMP)),
4540 Inlined_Body => True);
4541
4542 Pop_Scope;
4543
4544 -- Restore context
4545
4546 Set_Is_Immediately_Visible (Gen_Comp, Vis);
4547
4548 -- Reset Generic_Instance flag so that use clauses can be installed
4549 -- in the proper order. (See Use_One_Package for effect of enclosing
4550 -- instances on processing of use clauses).
4551
4552 for J in 1 .. N_Instances loop
4553 Set_Is_Generic_Instance (Instances (J), False);
4554 end loop;
4555
4556 if Removed then
4557 Install_Context (Curr_Comp);
4558
4559 if Present (Curr_Scope)
4560 and then Is_Child_Unit (Curr_Scope)
4561 then
4562 Push_Scope (Curr_Scope);
4563 Set_Is_Immediately_Visible (Curr_Scope);
4564
4565 -- Finally, restore inner scopes as well
4566
4567 for J in reverse 1 .. Num_Inner loop
4568 Push_Scope (Inner_Scopes (J));
4569 end loop;
4570 end if;
4571
4572 Restore_Scope_Stack (List, Handle_Use => False);
4573
4574 if Present (Curr_Scope)
4575 and then
4576 (In_Private_Part (Curr_Scope)
4577 or else In_Package_Body (Curr_Scope))
4578 then
4579 -- Install private declaration of ancestor units, which are
4580 -- currently available. Restore_Scope_Stack and Install_Context
4581 -- only install the visible part of parents.
4582
4583 declare
4584 Par : Entity_Id;
4585 begin
4586 Par := Scope (Curr_Scope);
4587 while (Present (Par)) and then Par /= Standard_Standard loop
4588 Install_Private_Declarations (Par);
4589 Par := Scope (Par);
4590 end loop;
4591 end;
4592 end if;
4593 end if;
4594
4595 -- Restore use clauses. For a child unit, use clauses in the parents
4596 -- are restored when installing the context, so only those in inner
4597 -- scopes (and those local to the child unit itself) need to be
4598 -- installed explicitly.
4599
4600 if Is_Child_Unit (Curr_Unit) and then Removed then
4601 for J in reverse 1 .. Num_Inner + 1 loop
4602 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
4603 Use_Clauses (J);
4604 Install_Use_Clauses (Use_Clauses (J));
4605 end loop;
4606
4607 else
4608 for J in reverse 1 .. Num_Scopes loop
4609 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
4610 Use_Clauses (J);
4611 Install_Use_Clauses (Use_Clauses (J));
4612 end loop;
4613 end if;
4614
4615 -- Restore status of instances. If one of them is a body, make its
4616 -- local entities visible again.
4617
4618 declare
4619 E : Entity_Id;
4620 Inst : Entity_Id;
4621
4622 begin
4623 for J in 1 .. N_Instances loop
4624 Inst := Instances (J);
4625 Set_Is_Generic_Instance (Inst, True);
4626
4627 if In_Package_Body (Inst)
4628 or else Ekind_In (S, E_Procedure, E_Function)
4629 then
4630 E := First_Entity (Instances (J));
4631 while Present (E) loop
4632 Set_Is_Immediately_Visible (E);
4633 Next_Entity (E);
4634 end loop;
4635 end if;
4636 end loop;
4637 end;
4638
4639 -- If generic unit is in current unit, current context is correct. Note
4640 -- that the context is guaranteed to carry the correct SPARK_Mode as no
4641 -- enclosing scopes were removed.
4642
4643 else
4644 Instantiate_Package_Body
4645 (Body_Info =>
4646 ((Inst_Node => N,
4647 Act_Decl => Act_Decl,
4648 Expander_Status => Expander_Active,
4649 Current_Sem_Unit => Current_Sem_Unit,
4650 Scope_Suppress => Scope_Suppress,
4651 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
4652 Version => Ada_Version,
4653 Version_Pragma => Ada_Version_Pragma,
4654 Warnings => Save_Warnings,
4655 SPARK_Mode => SPARK_Mode,
4656 SPARK_Mode_Pragma => SPARK_Mode_Pragma)),
4657 Inlined_Body => True);
4658 end if;
4659 end Inline_Instance_Body;
4660
4661 -------------------------------------
4662 -- Analyze_Procedure_Instantiation --
4663 -------------------------------------
4664
4665 procedure Analyze_Procedure_Instantiation (N : Node_Id) is
4666 begin
4667 Analyze_Subprogram_Instantiation (N, E_Procedure);
4668 end Analyze_Procedure_Instantiation;
4669
4670 -----------------------------------
4671 -- Need_Subprogram_Instance_Body --
4672 -----------------------------------
4673
4674 function Need_Subprogram_Instance_Body
4675 (N : Node_Id;
4676 Subp : Entity_Id) return Boolean
4677 is
4678 begin
4679 -- Must be inlined (or inlined renaming)
4680
4681 if (Is_In_Main_Unit (N)
4682 or else Is_Inlined (Subp)
4683 or else Is_Inlined (Alias (Subp)))
4684
4685 -- Must be generating code or analyzing code in ASIS/GNATprove mode
4686
4687 and then (Operating_Mode = Generate_Code
4688 or else (Operating_Mode = Check_Semantics
4689 and then (ASIS_Mode or GNATprove_Mode)))
4690
4691 -- The body is needed when generating code (full expansion), in ASIS
4692 -- mode for other tools, and in GNATprove mode (special expansion) for
4693 -- formal verification of the body itself.
4694
4695 and then (Expander_Active or ASIS_Mode or GNATprove_Mode)
4696
4697 -- No point in inlining if ABE is inevitable
4698
4699 and then not ABE_Is_Certain (N)
4700
4701 -- Or if subprogram is eliminated
4702
4703 and then not Is_Eliminated (Subp)
4704 then
4705 Pending_Instantiations.Append
4706 ((Inst_Node => N,
4707 Act_Decl => Unit_Declaration_Node (Subp),
4708 Expander_Status => Expander_Active,
4709 Current_Sem_Unit => Current_Sem_Unit,
4710 Scope_Suppress => Scope_Suppress,
4711 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
4712 Version => Ada_Version,
4713 Version_Pragma => Ada_Version_Pragma,
4714 Warnings => Save_Warnings,
4715 SPARK_Mode => SPARK_Mode,
4716 SPARK_Mode_Pragma => SPARK_Mode_Pragma));
4717 return True;
4718
4719 -- Here if not inlined, or we ignore the inlining
4720
4721 else
4722 return False;
4723 end if;
4724 end Need_Subprogram_Instance_Body;
4725
4726 --------------------------------------
4727 -- Analyze_Subprogram_Instantiation --
4728 --------------------------------------
4729
4730 procedure Analyze_Subprogram_Instantiation
4731 (N : Node_Id;
4732 K : Entity_Kind)
4733 is
4734 Loc : constant Source_Ptr := Sloc (N);
4735 Gen_Id : constant Node_Id := Name (N);
4736
4737 Anon_Id : constant Entity_Id :=
4738 Make_Defining_Identifier (Sloc (Defining_Entity (N)),
4739 Chars => New_External_Name
4740 (Chars (Defining_Entity (N)), 'R'));
4741
4742 Act_Decl_Id : Entity_Id;
4743 Act_Decl : Node_Id;
4744 Act_Spec : Node_Id;
4745 Act_Tree : Node_Id;
4746
4747 Env_Installed : Boolean := False;
4748 Gen_Unit : Entity_Id;
4749 Gen_Decl : Node_Id;
4750 Pack_Id : Entity_Id;
4751 Parent_Installed : Boolean := False;
4752
4753 Renaming_List : List_Id;
4754 -- The list of declarations that link formals and actuals of the
4755 -- instance. These are subtype declarations for formal types, and
4756 -- renaming declarations for other formals. The subprogram declaration
4757 -- for the instance is then appended to the list, and the last item on
4758 -- the list is the renaming declaration for the instance.
4759
4760 procedure Analyze_Instance_And_Renamings;
4761 -- The instance must be analyzed in a context that includes the mappings
4762 -- of generic parameters into actuals. We create a package declaration
4763 -- for this purpose, and a subprogram with an internal name within the
4764 -- package. The subprogram instance is simply an alias for the internal
4765 -- subprogram, declared in the current scope.
4766
4767 procedure Build_Subprogram_Renaming;
4768 -- If the subprogram is recursive, there are occurrences of the name of
4769 -- the generic within the body, which must resolve to the current
4770 -- instance. We add a renaming declaration after the declaration, which
4771 -- is available in the instance body, as well as in the analysis of
4772 -- aspects that appear in the generic. This renaming declaration is
4773 -- inserted after the instance declaration which it renames.
4774
4775 procedure Instantiate_Subprogram_Contract (Templ : Node_Id);
4776 -- Instantiate all source pragmas found in the contract of the generic
4777 -- subprogram declaration template denoted by Templ. The instantiated
4778 -- pragmas are added to list Renaming_List.
4779
4780 ------------------------------------
4781 -- Analyze_Instance_And_Renamings --
4782 ------------------------------------
4783
4784 procedure Analyze_Instance_And_Renamings is
4785 Def_Ent : constant Entity_Id := Defining_Entity (N);
4786 Pack_Decl : Node_Id;
4787
4788 begin
4789 if Nkind (Parent (N)) = N_Compilation_Unit then
4790
4791 -- For the case of a compilation unit, the container package has
4792 -- the same name as the instantiation, to insure that the binder
4793 -- calls the elaboration procedure with the right name. Copy the
4794 -- entity of the instance, which may have compilation level flags
4795 -- (e.g. Is_Child_Unit) set.
4796
4797 Pack_Id := New_Copy (Def_Ent);
4798
4799 else
4800 -- Otherwise we use the name of the instantiation concatenated
4801 -- with its source position to ensure uniqueness if there are
4802 -- several instantiations with the same name.
4803
4804 Pack_Id :=
4805 Make_Defining_Identifier (Loc,
4806 Chars => New_External_Name
4807 (Related_Id => Chars (Def_Ent),
4808 Suffix => "GP",
4809 Suffix_Index => Source_Offset (Sloc (Def_Ent))));
4810 end if;
4811
4812 Pack_Decl :=
4813 Make_Package_Declaration (Loc,
4814 Specification => Make_Package_Specification (Loc,
4815 Defining_Unit_Name => Pack_Id,
4816 Visible_Declarations => Renaming_List,
4817 End_Label => Empty));
4818
4819 Set_Instance_Spec (N, Pack_Decl);
4820 Set_Is_Generic_Instance (Pack_Id);
4821 Set_Debug_Info_Needed (Pack_Id);
4822
4823 -- Case of not a compilation unit
4824
4825 if Nkind (Parent (N)) /= N_Compilation_Unit then
4826 Mark_Rewrite_Insertion (Pack_Decl);
4827 Insert_Before (N, Pack_Decl);
4828 Set_Has_Completion (Pack_Id);
4829
4830 -- Case of an instantiation that is a compilation unit
4831
4832 -- Place declaration on current node so context is complete for
4833 -- analysis (including nested instantiations), and for use in a
4834 -- context_clause (see Analyze_With_Clause).
4835
4836 else
4837 Set_Unit (Parent (N), Pack_Decl);
4838 Set_Parent_Spec (Pack_Decl, Parent_Spec (N));
4839 end if;
4840
4841 Analyze (Pack_Decl);
4842 Check_Formal_Packages (Pack_Id);
4843 Set_Is_Generic_Instance (Pack_Id, False);
4844
4845 -- Why do we clear Is_Generic_Instance??? We set it 20 lines
4846 -- above???
4847
4848 -- Body of the enclosing package is supplied when instantiating the
4849 -- subprogram body, after semantic analysis is completed.
4850
4851 if Nkind (Parent (N)) = N_Compilation_Unit then
4852
4853 -- Remove package itself from visibility, so it does not
4854 -- conflict with subprogram.
4855
4856 Set_Name_Entity_Id (Chars (Pack_Id), Homonym (Pack_Id));
4857
4858 -- Set name and scope of internal subprogram so that the proper
4859 -- external name will be generated. The proper scope is the scope
4860 -- of the wrapper package. We need to generate debugging info for
4861 -- the internal subprogram, so set flag accordingly.
4862
4863 Set_Chars (Anon_Id, Chars (Defining_Entity (N)));
4864 Set_Scope (Anon_Id, Scope (Pack_Id));
4865
4866 -- Mark wrapper package as referenced, to avoid spurious warnings
4867 -- if the instantiation appears in various with_ clauses of
4868 -- subunits of the main unit.
4869
4870 Set_Referenced (Pack_Id);
4871 end if;
4872
4873 Set_Is_Generic_Instance (Anon_Id);
4874 Set_Debug_Info_Needed (Anon_Id);
4875 Act_Decl_Id := New_Copy (Anon_Id);
4876
4877 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
4878 Set_Chars (Act_Decl_Id, Chars (Defining_Entity (N)));
4879 Set_Sloc (Act_Decl_Id, Sloc (Defining_Entity (N)));
4880
4881 -- Subprogram instance comes from source only if generic does
4882
4883 Set_Comes_From_Source (Act_Decl_Id, Comes_From_Source (Gen_Unit));
4884
4885 -- The signature may involve types that are not frozen yet, but the
4886 -- subprogram will be frozen at the point the wrapper package is
4887 -- frozen, so it does not need its own freeze node. In fact, if one
4888 -- is created, it might conflict with the freezing actions from the
4889 -- wrapper package.
4890
4891 Set_Has_Delayed_Freeze (Anon_Id, False);
4892
4893 -- If the instance is a child unit, mark the Id accordingly. Mark
4894 -- the anonymous entity as well, which is the real subprogram and
4895 -- which is used when the instance appears in a context clause.
4896 -- Similarly, propagate the Is_Eliminated flag to handle properly
4897 -- nested eliminated subprograms.
4898
4899 Set_Is_Child_Unit (Act_Decl_Id, Is_Child_Unit (Defining_Entity (N)));
4900 Set_Is_Child_Unit (Anon_Id, Is_Child_Unit (Defining_Entity (N)));
4901 New_Overloaded_Entity (Act_Decl_Id);
4902 Check_Eliminated (Act_Decl_Id);
4903 Set_Is_Eliminated (Anon_Id, Is_Eliminated (Act_Decl_Id));
4904
4905 -- In compilation unit case, kill elaboration checks on the
4906 -- instantiation, since they are never needed -- the body is
4907 -- instantiated at the same point as the spec.
4908
4909 if Nkind (Parent (N)) = N_Compilation_Unit then
4910 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
4911 Set_Kill_Elaboration_Checks (Act_Decl_Id);
4912 Set_Is_Compilation_Unit (Anon_Id);
4913
4914 Set_Cunit_Entity (Current_Sem_Unit, Pack_Id);
4915 end if;
4916
4917 -- The instance is not a freezing point for the new subprogram
4918
4919 Set_Is_Frozen (Act_Decl_Id, False);
4920
4921 if Nkind (Defining_Entity (N)) = N_Defining_Operator_Symbol then
4922 Valid_Operator_Definition (Act_Decl_Id);
4923 end if;
4924
4925 Set_Alias (Act_Decl_Id, Anon_Id);
4926 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
4927 Set_Has_Completion (Act_Decl_Id);
4928 Set_Related_Instance (Pack_Id, Act_Decl_Id);
4929
4930 if Nkind (Parent (N)) = N_Compilation_Unit then
4931 Set_Body_Required (Parent (N), False);
4932 end if;
4933 end Analyze_Instance_And_Renamings;
4934
4935 -------------------------------
4936 -- Build_Subprogram_Renaming --
4937 -------------------------------
4938
4939 procedure Build_Subprogram_Renaming is
4940 Renaming_Decl : Node_Id;
4941 Unit_Renaming : Node_Id;
4942
4943 begin
4944 Unit_Renaming :=
4945 Make_Subprogram_Renaming_Declaration (Loc,
4946 Specification =>
4947 Copy_Generic_Node
4948 (Specification (Original_Node (Gen_Decl)),
4949 Empty,
4950 Instantiating => True),
4951 Name => New_Occurrence_Of (Anon_Id, Loc));
4952
4953 -- The generic may be a a child unit. The renaming needs an
4954 -- identifier with the proper name.
4955
4956 Set_Defining_Unit_Name (Specification (Unit_Renaming),
4957 Make_Defining_Identifier (Loc, Chars (Gen_Unit)));
4958
4959 -- If there is a formal subprogram with the same name as the unit
4960 -- itself, do not add this renaming declaration, to prevent
4961 -- ambiguities when there is a call with that name in the body.
4962 -- This is a partial and ugly fix for one ACATS test. ???
4963
4964 Renaming_Decl := First (Renaming_List);
4965 while Present (Renaming_Decl) loop
4966 if Nkind (Renaming_Decl) = N_Subprogram_Renaming_Declaration
4967 and then
4968 Chars (Defining_Entity (Renaming_Decl)) = Chars (Gen_Unit)
4969 then
4970 exit;
4971 end if;
4972
4973 Next (Renaming_Decl);
4974 end loop;
4975
4976 if No (Renaming_Decl) then
4977 Append (Unit_Renaming, Renaming_List);
4978 end if;
4979 end Build_Subprogram_Renaming;
4980
4981 -------------------------------------
4982 -- Instantiate_Subprogram_Contract --
4983 -------------------------------------
4984
4985 procedure Instantiate_Subprogram_Contract (Templ : Node_Id) is
4986 procedure Instantiate_Pragmas (First_Prag : Node_Id);
4987 -- Instantiate all contract-related source pragmas found in the list
4988 -- starting with pragma First_Prag. Each instantiated pragma is added
4989 -- to list Renaming_List.
4990
4991 -------------------------
4992 -- Instantiate_Pragmas --
4993 -------------------------
4994
4995 procedure Instantiate_Pragmas (First_Prag : Node_Id) is
4996 Inst_Prag : Node_Id;
4997 Prag : Node_Id;
4998
4999 begin
5000 Prag := First_Prag;
5001 while Present (Prag) loop
5002 if Is_Generic_Contract_Pragma (Prag) then
5003 Inst_Prag :=
5004 Copy_Generic_Node (Prag, Empty, Instantiating => True);
5005
5006 Set_Analyzed (Inst_Prag, False);
5007 Append_To (Renaming_List, Inst_Prag);
5008 end if;
5009
5010 Prag := Next_Pragma (Prag);
5011 end loop;
5012 end Instantiate_Pragmas;
5013
5014 -- Local variables
5015
5016 Items : constant Node_Id := Contract (Defining_Entity (Templ));
5017
5018 -- Start of processing for Instantiate_Subprogram_Contract
5019
5020 begin
5021 if Present (Items) then
5022 Instantiate_Pragmas (Pre_Post_Conditions (Items));
5023 Instantiate_Pragmas (Contract_Test_Cases (Items));
5024 Instantiate_Pragmas (Classifications (Items));
5025 end if;
5026 end Instantiate_Subprogram_Contract;
5027
5028 -- Local variables
5029
5030 Save_IPSM : constant Boolean := Ignore_Pragma_SPARK_Mode;
5031 -- Save flag Ignore_Pragma_SPARK_Mode for restore on exit
5032
5033 Save_SM : constant SPARK_Mode_Type := SPARK_Mode;
5034 Save_SMP : constant Node_Id := SPARK_Mode_Pragma;
5035 -- Save the SPARK_Mode-related data for restore on exit
5036
5037 Vis_Prims_List : Elist_Id := No_Elist;
5038 -- List of primitives made temporarily visible in the instantiation
5039 -- to match the visibility of the formal type
5040
5041 -- Start of processing for Analyze_Subprogram_Instantiation
5042
5043 begin
5044 Check_SPARK_05_Restriction ("generic is not allowed", N);
5045
5046 -- Very first thing: check for special Text_IO unit in case we are
5047 -- instantiating one of the children of [[Wide_]Wide_]Text_IO. Of course
5048 -- such an instantiation is bogus (these are packages, not subprograms),
5049 -- but we get a better error message if we do this.
5050
5051 Check_Text_IO_Special_Unit (Gen_Id);
5052
5053 -- Make node global for error reporting
5054
5055 Instantiation_Node := N;
5056
5057 -- For package instantiations we turn off style checks, because they
5058 -- will have been emitted in the generic. For subprogram instantiations
5059 -- we want to apply at least the check on overriding indicators so we
5060 -- do not modify the style check status.
5061
5062 -- The renaming declarations for the actuals do not come from source and
5063 -- will not generate spurious warnings.
5064
5065 Preanalyze_Actuals (N);
5066
5067 Init_Env;
5068 Env_Installed := True;
5069 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
5070 Gen_Unit := Entity (Gen_Id);
5071
5072 Generate_Reference (Gen_Unit, Gen_Id);
5073
5074 if Nkind (Gen_Id) = N_Identifier
5075 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
5076 then
5077 Error_Msg_NE
5078 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
5079 end if;
5080
5081 if Etype (Gen_Unit) = Any_Type then
5082 Restore_Env;
5083 return;
5084 end if;
5085
5086 -- Verify that it is a generic subprogram of the right kind, and that
5087 -- it does not lead to a circular instantiation.
5088
5089 if K = E_Procedure and then Ekind (Gen_Unit) /= E_Generic_Procedure then
5090 Error_Msg_NE
5091 ("& is not the name of a generic procedure", Gen_Id, Gen_Unit);
5092
5093 elsif K = E_Function and then Ekind (Gen_Unit) /= E_Generic_Function then
5094 Error_Msg_NE
5095 ("& is not the name of a generic function", Gen_Id, Gen_Unit);
5096
5097 elsif In_Open_Scopes (Gen_Unit) then
5098 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
5099
5100 else
5101 -- If the context of the instance is subject to SPARK_Mode "off",
5102 -- set the global flag which signals Analyze_Pragma to ignore all
5103 -- SPARK_Mode pragmas within the instance.
5104
5105 if SPARK_Mode = Off then
5106 Ignore_Pragma_SPARK_Mode := True;
5107 end if;
5108
5109 Set_Entity (Gen_Id, Gen_Unit);
5110 Set_Is_Instantiated (Gen_Unit);
5111
5112 if In_Extended_Main_Source_Unit (N) then
5113 Generate_Reference (Gen_Unit, N);
5114 end if;
5115
5116 -- If renaming, get original unit
5117
5118 if Present (Renamed_Object (Gen_Unit))
5119 and then Ekind_In (Renamed_Object (Gen_Unit), E_Generic_Procedure,
5120 E_Generic_Function)
5121 then
5122 Gen_Unit := Renamed_Object (Gen_Unit);
5123 Set_Is_Instantiated (Gen_Unit);
5124 Generate_Reference (Gen_Unit, N);
5125 end if;
5126
5127 if Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
5128 Error_Msg_Node_2 := Current_Scope;
5129 Error_Msg_NE
5130 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
5131 Circularity_Detected := True;
5132 Restore_Hidden_Primitives (Vis_Prims_List);
5133 goto Leave;
5134 end if;
5135
5136 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
5137
5138 -- Initialize renamings map, for error checking
5139
5140 Generic_Renamings.Set_Last (0);
5141 Generic_Renamings_HTable.Reset;
5142
5143 Create_Instantiation_Source (N, Gen_Unit, False, S_Adjustment);
5144
5145 -- Copy original generic tree, to produce text for instantiation
5146
5147 Act_Tree :=
5148 Copy_Generic_Node
5149 (Original_Node (Gen_Decl), Empty, Instantiating => True);
5150
5151 -- Inherit overriding indicator from instance node
5152
5153 Act_Spec := Specification (Act_Tree);
5154 Set_Must_Override (Act_Spec, Must_Override (N));
5155 Set_Must_Not_Override (Act_Spec, Must_Not_Override (N));
5156
5157 Renaming_List :=
5158 Analyze_Associations
5159 (I_Node => N,
5160 Formals => Generic_Formal_Declarations (Act_Tree),
5161 F_Copy => Generic_Formal_Declarations (Gen_Decl));
5162
5163 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
5164
5165 -- The subprogram itself cannot contain a nested instance, so the
5166 -- current parent is left empty.
5167
5168 Set_Instance_Env (Gen_Unit, Empty);
5169
5170 -- Build the subprogram declaration, which does not appear in the
5171 -- generic template, and give it a sloc consistent with that of the
5172 -- template.
5173
5174 Set_Defining_Unit_Name (Act_Spec, Anon_Id);
5175 Set_Generic_Parent (Act_Spec, Gen_Unit);
5176 Act_Decl :=
5177 Make_Subprogram_Declaration (Sloc (Act_Spec),
5178 Specification => Act_Spec);
5179
5180 -- The aspects have been copied previously, but they have to be
5181 -- linked explicitly to the new subprogram declaration. Explicit
5182 -- pre/postconditions on the instance are analyzed below, in a
5183 -- separate step.
5184
5185 Move_Aspects (Act_Tree, To => Act_Decl);
5186 Set_Categorization_From_Pragmas (Act_Decl);
5187
5188 if Parent_Installed then
5189 Hide_Current_Scope;
5190 end if;
5191
5192 Append (Act_Decl, Renaming_List);
5193
5194 -- Contract-related source pragmas that follow a generic subprogram
5195 -- must be instantiated explicitly because they are not part of the
5196 -- subprogram template.
5197
5198 Instantiate_Subprogram_Contract (Original_Node (Gen_Decl));
5199 Build_Subprogram_Renaming;
5200
5201 Analyze_Instance_And_Renamings;
5202
5203 -- If the generic is marked Import (Intrinsic), then so is the
5204 -- instance. This indicates that there is no body to instantiate. If
5205 -- generic is marked inline, so it the instance, and the anonymous
5206 -- subprogram it renames. If inlined, or else if inlining is enabled
5207 -- for the compilation, we generate the instance body even if it is
5208 -- not within the main unit.
5209
5210 if Is_Intrinsic_Subprogram (Gen_Unit) then
5211 Set_Is_Intrinsic_Subprogram (Anon_Id);
5212 Set_Is_Intrinsic_Subprogram (Act_Decl_Id);
5213
5214 if Chars (Gen_Unit) = Name_Unchecked_Conversion then
5215 Validate_Unchecked_Conversion (N, Act_Decl_Id);
5216 end if;
5217 end if;
5218
5219 -- Inherit convention from generic unit. Intrinsic convention, as for
5220 -- an instance of unchecked conversion, is not inherited because an
5221 -- explicit Ada instance has been created.
5222
5223 if Has_Convention_Pragma (Gen_Unit)
5224 and then Convention (Gen_Unit) /= Convention_Intrinsic
5225 then
5226 Set_Convention (Act_Decl_Id, Convention (Gen_Unit));
5227 Set_Is_Exported (Act_Decl_Id, Is_Exported (Gen_Unit));
5228 end if;
5229
5230 Generate_Definition (Act_Decl_Id);
5231
5232 -- Inherit all inlining-related flags which apply to the generic in
5233 -- the subprogram and its declaration.
5234
5235 Set_Is_Inlined (Act_Decl_Id, Is_Inlined (Gen_Unit));
5236 Set_Is_Inlined (Anon_Id, Is_Inlined (Gen_Unit));
5237
5238 Set_Has_Pragma_Inline (Act_Decl_Id, Has_Pragma_Inline (Gen_Unit));
5239 Set_Has_Pragma_Inline (Anon_Id, Has_Pragma_Inline (Gen_Unit));
5240
5241 Set_Has_Pragma_Inline_Always
5242 (Act_Decl_Id, Has_Pragma_Inline_Always (Gen_Unit));
5243 Set_Has_Pragma_Inline_Always
5244 (Anon_Id, Has_Pragma_Inline_Always (Gen_Unit));
5245
5246 if not Is_Intrinsic_Subprogram (Gen_Unit) then
5247 Check_Elab_Instantiation (N);
5248 end if;
5249
5250 if Is_Dispatching_Operation (Act_Decl_Id)
5251 and then Ada_Version >= Ada_2005
5252 then
5253 declare
5254 Formal : Entity_Id;
5255
5256 begin
5257 Formal := First_Formal (Act_Decl_Id);
5258 while Present (Formal) loop
5259 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type
5260 and then Is_Controlling_Formal (Formal)
5261 and then not Can_Never_Be_Null (Formal)
5262 then
5263 Error_Msg_NE
5264 ("access parameter& is controlling,", N, Formal);
5265 Error_Msg_NE
5266 ("\corresponding parameter of & must be "
5267 & "explicitly null-excluding", N, Gen_Id);
5268 end if;
5269
5270 Next_Formal (Formal);
5271 end loop;
5272 end;
5273 end if;
5274
5275 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
5276
5277 Validate_Categorization_Dependency (N, Act_Decl_Id);
5278
5279 if not Is_Intrinsic_Subprogram (Act_Decl_Id) then
5280 Inherit_Context (Gen_Decl, N);
5281
5282 Restore_Private_Views (Pack_Id, False);
5283
5284 -- If the context requires a full instantiation, mark node for
5285 -- subsequent construction of the body.
5286
5287 if Need_Subprogram_Instance_Body (N, Act_Decl_Id) then
5288 Check_Forward_Instantiation (Gen_Decl);
5289
5290 -- The wrapper package is always delayed, because it does not
5291 -- constitute a freeze point, but to insure that the freeze
5292 -- node is placed properly, it is created directly when
5293 -- instantiating the body (otherwise the freeze node might
5294 -- appear to early for nested instantiations).
5295
5296 elsif Nkind (Parent (N)) = N_Compilation_Unit then
5297
5298 -- For ASIS purposes, indicate that the wrapper package has
5299 -- replaced the instantiation node.
5300
5301 Rewrite (N, Unit (Parent (N)));
5302 Set_Unit (Parent (N), N);
5303 end if;
5304
5305 elsif Nkind (Parent (N)) = N_Compilation_Unit then
5306
5307 -- Replace instance node for library-level instantiations of
5308 -- intrinsic subprograms, for ASIS use.
5309
5310 Rewrite (N, Unit (Parent (N)));
5311 Set_Unit (Parent (N), N);
5312 end if;
5313
5314 if Parent_Installed then
5315 Remove_Parent;
5316 end if;
5317
5318 Restore_Hidden_Primitives (Vis_Prims_List);
5319 Restore_Env;
5320 Env_Installed := False;
5321 Generic_Renamings.Set_Last (0);
5322 Generic_Renamings_HTable.Reset;
5323
5324 Ignore_Pragma_SPARK_Mode := Save_IPSM;
5325 SPARK_Mode := Save_SM;
5326 SPARK_Mode_Pragma := Save_SMP;
5327
5328 if SPARK_Mode = On then
5329 Dynamic_Elaboration_Checks := False;
5330 end if;
5331
5332 end if;
5333
5334 <<Leave>>
5335 if Has_Aspects (N) then
5336 Analyze_Aspect_Specifications (N, Act_Decl_Id);
5337 end if;
5338
5339 exception
5340 when Instantiation_Error =>
5341 if Parent_Installed then
5342 Remove_Parent;
5343 end if;
5344
5345 if Env_Installed then
5346 Restore_Env;
5347 end if;
5348
5349 Ignore_Pragma_SPARK_Mode := Save_IPSM;
5350 SPARK_Mode := Save_SM;
5351 SPARK_Mode_Pragma := Save_SMP;
5352
5353 if SPARK_Mode = On then
5354 Dynamic_Elaboration_Checks := False;
5355 end if;
5356 end Analyze_Subprogram_Instantiation;
5357
5358 -------------------------
5359 -- Get_Associated_Node --
5360 -------------------------
5361
5362 function Get_Associated_Node (N : Node_Id) return Node_Id is
5363 Assoc : Node_Id;
5364
5365 begin
5366 Assoc := Associated_Node (N);
5367
5368 if Nkind (Assoc) /= Nkind (N) then
5369 return Assoc;
5370
5371 elsif Nkind_In (Assoc, N_Aggregate, N_Extension_Aggregate) then
5372 return Assoc;
5373
5374 else
5375 -- If the node is part of an inner generic, it may itself have been
5376 -- remapped into a further generic copy. Associated_Node is otherwise
5377 -- used for the entity of the node, and will be of a different node
5378 -- kind, or else N has been rewritten as a literal or function call.
5379
5380 while Present (Associated_Node (Assoc))
5381 and then Nkind (Associated_Node (Assoc)) = Nkind (Assoc)
5382 loop
5383 Assoc := Associated_Node (Assoc);
5384 end loop;
5385
5386 -- Follow and additional link in case the final node was rewritten.
5387 -- This can only happen with nested generic units.
5388
5389 if (Nkind (Assoc) = N_Identifier or else Nkind (Assoc) in N_Op)
5390 and then Present (Associated_Node (Assoc))
5391 and then (Nkind_In (Associated_Node (Assoc), N_Function_Call,
5392 N_Explicit_Dereference,
5393 N_Integer_Literal,
5394 N_Real_Literal,
5395 N_String_Literal))
5396 then
5397 Assoc := Associated_Node (Assoc);
5398 end if;
5399
5400 -- An additional special case: an unconstrained type in an object
5401 -- declaration may have been rewritten as a local subtype constrained
5402 -- by the expression in the declaration. We need to recover the
5403 -- original entity which may be global.
5404
5405 if Present (Original_Node (Assoc))
5406 and then Nkind (Parent (N)) = N_Object_Declaration
5407 then
5408 Assoc := Original_Node (Assoc);
5409 end if;
5410
5411 return Assoc;
5412 end if;
5413 end Get_Associated_Node;
5414
5415 ----------------------------
5416 -- Build_Function_Wrapper --
5417 ----------------------------
5418
5419 function Build_Function_Wrapper
5420 (Formal_Subp : Entity_Id;
5421 Actual_Subp : Entity_Id) return Node_Id
5422 is
5423 Loc : constant Source_Ptr := Sloc (Current_Scope);
5424 Ret_Type : constant Entity_Id := Get_Instance_Of (Etype (Formal_Subp));
5425 Actuals : List_Id;
5426 Decl : Node_Id;
5427 Func_Name : Node_Id;
5428 Func : Entity_Id;
5429 Parm_Type : Node_Id;
5430 Profile : List_Id := New_List;
5431 Spec : Node_Id;
5432 Act_F : Entity_Id;
5433 Form_F : Entity_Id;
5434 New_F : Entity_Id;
5435
5436 begin
5437 Func_Name := New_Occurrence_Of (Actual_Subp, Loc);
5438
5439 Func := Make_Defining_Identifier (Loc, Chars (Formal_Subp));
5440 Set_Ekind (Func, E_Function);
5441 Set_Is_Generic_Actual_Subprogram (Func);
5442
5443 Actuals := New_List;
5444 Profile := New_List;
5445
5446 Act_F := First_Formal (Actual_Subp);
5447 Form_F := First_Formal (Formal_Subp);
5448 while Present (Form_F) loop
5449
5450 -- Create new formal for profile of wrapper, and add a reference
5451 -- to it in the list of actuals for the enclosing call. The name
5452 -- must be that of the formal in the formal subprogram, because
5453 -- calls to it in the generic body may use named associations.
5454
5455 New_F := Make_Defining_Identifier (Loc, Chars (Form_F));
5456
5457 Parm_Type :=
5458 New_Occurrence_Of (Get_Instance_Of (Etype (Form_F)), Loc);
5459
5460 Append_To (Profile,
5461 Make_Parameter_Specification (Loc,
5462 Defining_Identifier => New_F,
5463 Parameter_Type => Parm_Type));
5464
5465 Append_To (Actuals, New_Occurrence_Of (New_F, Loc));
5466 Next_Formal (Form_F);
5467
5468 if Present (Act_F) then
5469 Next_Formal (Act_F);
5470 end if;
5471 end loop;
5472
5473 Spec :=
5474 Make_Function_Specification (Loc,
5475 Defining_Unit_Name => Func,
5476 Parameter_Specifications => Profile,
5477 Result_Definition => New_Occurrence_Of (Ret_Type, Loc));
5478
5479 Decl :=
5480 Make_Expression_Function (Loc,
5481 Specification => Spec,
5482 Expression =>
5483 Make_Function_Call (Loc,
5484 Name => Func_Name,
5485 Parameter_Associations => Actuals));
5486
5487 return Decl;
5488 end Build_Function_Wrapper;
5489
5490 ----------------------------
5491 -- Build_Operator_Wrapper --
5492 ----------------------------
5493
5494 function Build_Operator_Wrapper
5495 (Formal_Subp : Entity_Id;
5496 Actual_Subp : Entity_Id) return Node_Id
5497 is
5498 Loc : constant Source_Ptr := Sloc (Current_Scope);
5499 Ret_Type : constant Entity_Id :=
5500 Get_Instance_Of (Etype (Formal_Subp));
5501 Op_Type : constant Entity_Id :=
5502 Get_Instance_Of (Etype (First_Formal (Formal_Subp)));
5503 Is_Binary : constant Boolean :=
5504 Present (Next_Formal (First_Formal (Formal_Subp)));
5505
5506 Decl : Node_Id;
5507 Expr : Node_Id;
5508 F1, F2 : Entity_Id;
5509 Func : Entity_Id;
5510 Op_Name : Name_Id;
5511 Spec : Node_Id;
5512 L, R : Node_Id;
5513
5514 begin
5515 Op_Name := Chars (Actual_Subp);
5516
5517 -- Create entities for wrapper function and its formals
5518
5519 F1 := Make_Temporary (Loc, 'A');
5520 F2 := Make_Temporary (Loc, 'B');
5521 L := New_Occurrence_Of (F1, Loc);
5522 R := New_Occurrence_Of (F2, Loc);
5523
5524 Func := Make_Defining_Identifier (Loc, Chars (Formal_Subp));
5525 Set_Ekind (Func, E_Function);
5526 Set_Is_Generic_Actual_Subprogram (Func);
5527
5528 Spec :=
5529 Make_Function_Specification (Loc,
5530 Defining_Unit_Name => Func,
5531 Parameter_Specifications => New_List (
5532 Make_Parameter_Specification (Loc,
5533 Defining_Identifier => F1,
5534 Parameter_Type => New_Occurrence_Of (Op_Type, Loc))),
5535 Result_Definition => New_Occurrence_Of (Ret_Type, Loc));
5536
5537 if Is_Binary then
5538 Append_To (Parameter_Specifications (Spec),
5539 Make_Parameter_Specification (Loc,
5540 Defining_Identifier => F2,
5541 Parameter_Type => New_Occurrence_Of (Op_Type, Loc)));
5542 end if;
5543
5544 -- Build expression as a function call, or as an operator node
5545 -- that corresponds to the name of the actual, starting with
5546 -- binary operators.
5547
5548 if Op_Name not in Any_Operator_Name then
5549 Expr :=
5550 Make_Function_Call (Loc,
5551 Name =>
5552 New_Occurrence_Of (Actual_Subp, Loc),
5553 Parameter_Associations => New_List (L));
5554
5555 if Is_Binary then
5556 Append_To (Parameter_Associations (Expr), R);
5557 end if;
5558
5559 -- Binary operators
5560
5561 elsif Is_Binary then
5562 if Op_Name = Name_Op_And then
5563 Expr := Make_Op_And (Loc, Left_Opnd => L, Right_Opnd => R);
5564 elsif Op_Name = Name_Op_Or then
5565 Expr := Make_Op_Or (Loc, Left_Opnd => L, Right_Opnd => R);
5566 elsif Op_Name = Name_Op_Xor then
5567 Expr := Make_Op_Xor (Loc, Left_Opnd => L, Right_Opnd => R);
5568 elsif Op_Name = Name_Op_Eq then
5569 Expr := Make_Op_Eq (Loc, Left_Opnd => L, Right_Opnd => R);
5570 elsif Op_Name = Name_Op_Ne then
5571 Expr := Make_Op_Ne (Loc, Left_Opnd => L, Right_Opnd => R);
5572 elsif Op_Name = Name_Op_Le then
5573 Expr := Make_Op_Le (Loc, Left_Opnd => L, Right_Opnd => R);
5574 elsif Op_Name = Name_Op_Gt then
5575 Expr := Make_Op_Gt (Loc, Left_Opnd => L, Right_Opnd => R);
5576 elsif Op_Name = Name_Op_Ge then
5577 Expr := Make_Op_Ge (Loc, Left_Opnd => L, Right_Opnd => R);
5578 elsif Op_Name = Name_Op_Lt then
5579 Expr := Make_Op_Lt (Loc, Left_Opnd => L, Right_Opnd => R);
5580 elsif Op_Name = Name_Op_Add then
5581 Expr := Make_Op_Add (Loc, Left_Opnd => L, Right_Opnd => R);
5582 elsif Op_Name = Name_Op_Subtract then
5583 Expr := Make_Op_Subtract (Loc, Left_Opnd => L, Right_Opnd => R);
5584 elsif Op_Name = Name_Op_Concat then
5585 Expr := Make_Op_Concat (Loc, Left_Opnd => L, Right_Opnd => R);
5586 elsif Op_Name = Name_Op_Multiply then
5587 Expr := Make_Op_Multiply (Loc, Left_Opnd => L, Right_Opnd => R);
5588 elsif Op_Name = Name_Op_Divide then
5589 Expr := Make_Op_Divide (Loc, Left_Opnd => L, Right_Opnd => R);
5590 elsif Op_Name = Name_Op_Mod then
5591 Expr := Make_Op_Mod (Loc, Left_Opnd => L, Right_Opnd => R);
5592 elsif Op_Name = Name_Op_Rem then
5593 Expr := Make_Op_Rem (Loc, Left_Opnd => L, Right_Opnd => R);
5594 elsif Op_Name = Name_Op_Expon then
5595 Expr := Make_Op_Expon (Loc, Left_Opnd => L, Right_Opnd => R);
5596 end if;
5597
5598 -- Unary operators
5599
5600 else
5601 if Op_Name = Name_Op_Add then
5602 Expr := Make_Op_Plus (Loc, Right_Opnd => L);
5603 elsif Op_Name = Name_Op_Subtract then
5604 Expr := Make_Op_Minus (Loc, Right_Opnd => L);
5605 elsif Op_Name = Name_Op_Abs then
5606 Expr := Make_Op_Abs (Loc, Right_Opnd => L);
5607 elsif Op_Name = Name_Op_Not then
5608 Expr := Make_Op_Not (Loc, Right_Opnd => L);
5609 end if;
5610 end if;
5611
5612 Decl :=
5613 Make_Expression_Function (Loc,
5614 Specification => Spec,
5615 Expression => Expr);
5616
5617 return Decl;
5618 end Build_Operator_Wrapper;
5619
5620 -------------------------------------------
5621 -- Build_Instance_Compilation_Unit_Nodes --
5622 -------------------------------------------
5623
5624 procedure Build_Instance_Compilation_Unit_Nodes
5625 (N : Node_Id;
5626 Act_Body : Node_Id;
5627 Act_Decl : Node_Id)
5628 is
5629 Decl_Cunit : Node_Id;
5630 Body_Cunit : Node_Id;
5631 Citem : Node_Id;
5632 New_Main : constant Entity_Id := Defining_Entity (Act_Decl);
5633 Old_Main : constant Entity_Id := Cunit_Entity (Main_Unit);
5634
5635 begin
5636 -- A new compilation unit node is built for the instance declaration
5637
5638 Decl_Cunit :=
5639 Make_Compilation_Unit (Sloc (N),
5640 Context_Items => Empty_List,
5641 Unit => Act_Decl,
5642 Aux_Decls_Node => Make_Compilation_Unit_Aux (Sloc (N)));
5643
5644 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
5645
5646 -- The new compilation unit is linked to its body, but both share the
5647 -- same file, so we do not set Body_Required on the new unit so as not
5648 -- to create a spurious dependency on a non-existent body in the ali.
5649 -- This simplifies CodePeer unit traversal.
5650
5651 -- We use the original instantiation compilation unit as the resulting
5652 -- compilation unit of the instance, since this is the main unit.
5653
5654 Rewrite (N, Act_Body);
5655
5656 -- Propagate the aspect specifications from the package body template to
5657 -- the instantiated version of the package body.
5658
5659 if Has_Aspects (Act_Body) then
5660 Set_Aspect_Specifications
5661 (N, New_Copy_List_Tree (Aspect_Specifications (Act_Body)));
5662 end if;
5663
5664 Body_Cunit := Parent (N);
5665
5666 -- The two compilation unit nodes are linked by the Library_Unit field
5667
5668 Set_Library_Unit (Decl_Cunit, Body_Cunit);
5669 Set_Library_Unit (Body_Cunit, Decl_Cunit);
5670
5671 -- Preserve the private nature of the package if needed
5672
5673 Set_Private_Present (Decl_Cunit, Private_Present (Body_Cunit));
5674
5675 -- If the instance is not the main unit, its context, categorization
5676 -- and elaboration entity are not relevant to the compilation.
5677
5678 if Body_Cunit /= Cunit (Main_Unit) then
5679 Make_Instance_Unit (Body_Cunit, In_Main => False);
5680 return;
5681 end if;
5682
5683 -- The context clause items on the instantiation, which are now attached
5684 -- to the body compilation unit (since the body overwrote the original
5685 -- instantiation node), semantically belong on the spec, so copy them
5686 -- there. It's harmless to leave them on the body as well. In fact one
5687 -- could argue that they belong in both places.
5688
5689 Citem := First (Context_Items (Body_Cunit));
5690 while Present (Citem) loop
5691 Append (New_Copy (Citem), Context_Items (Decl_Cunit));
5692 Next (Citem);
5693 end loop;
5694
5695 -- Propagate categorization flags on packages, so that they appear in
5696 -- the ali file for the spec of the unit.
5697
5698 if Ekind (New_Main) = E_Package then
5699 Set_Is_Pure (Old_Main, Is_Pure (New_Main));
5700 Set_Is_Preelaborated (Old_Main, Is_Preelaborated (New_Main));
5701 Set_Is_Remote_Types (Old_Main, Is_Remote_Types (New_Main));
5702 Set_Is_Shared_Passive (Old_Main, Is_Shared_Passive (New_Main));
5703 Set_Is_Remote_Call_Interface
5704 (Old_Main, Is_Remote_Call_Interface (New_Main));
5705 end if;
5706
5707 -- Make entry in Units table, so that binder can generate call to
5708 -- elaboration procedure for body, if any.
5709
5710 Make_Instance_Unit (Body_Cunit, In_Main => True);
5711 Main_Unit_Entity := New_Main;
5712 Set_Cunit_Entity (Main_Unit, Main_Unit_Entity);
5713
5714 -- Build elaboration entity, since the instance may certainly generate
5715 -- elaboration code requiring a flag for protection.
5716
5717 Build_Elaboration_Entity (Decl_Cunit, New_Main);
5718 end Build_Instance_Compilation_Unit_Nodes;
5719
5720 -----------------------------
5721 -- Check_Access_Definition --
5722 -----------------------------
5723
5724 procedure Check_Access_Definition (N : Node_Id) is
5725 begin
5726 pragma Assert
5727 (Ada_Version >= Ada_2005 and then Present (Access_Definition (N)));
5728 null;
5729 end Check_Access_Definition;
5730
5731 -----------------------------------
5732 -- Check_Formal_Package_Instance --
5733 -----------------------------------
5734
5735 -- If the formal has specific parameters, they must match those of the
5736 -- actual. Both of them are instances, and the renaming declarations for
5737 -- their formal parameters appear in the same order in both. The analyzed
5738 -- formal has been analyzed in the context of the current instance.
5739
5740 procedure Check_Formal_Package_Instance
5741 (Formal_Pack : Entity_Id;
5742 Actual_Pack : Entity_Id)
5743 is
5744 E1 : Entity_Id := First_Entity (Actual_Pack);
5745 E2 : Entity_Id := First_Entity (Formal_Pack);
5746
5747 Expr1 : Node_Id;
5748 Expr2 : Node_Id;
5749
5750 procedure Check_Mismatch (B : Boolean);
5751 -- Common error routine for mismatch between the parameters of the
5752 -- actual instance and those of the formal package.
5753
5754 function Same_Instantiated_Constant (E1, E2 : Entity_Id) return Boolean;
5755 -- The formal may come from a nested formal package, and the actual may
5756 -- have been constant-folded. To determine whether the two denote the
5757 -- same entity we may have to traverse several definitions to recover
5758 -- the ultimate entity that they refer to.
5759
5760 function Same_Instantiated_Variable (E1, E2 : Entity_Id) return Boolean;
5761 -- Similarly, if the formal comes from a nested formal package, the
5762 -- actual may designate the formal through multiple renamings, which
5763 -- have to be followed to determine the original variable in question.
5764
5765 --------------------
5766 -- Check_Mismatch --
5767 --------------------
5768
5769 procedure Check_Mismatch (B : Boolean) is
5770 Kind : constant Node_Kind := Nkind (Parent (E2));
5771
5772 begin
5773 if Kind = N_Formal_Type_Declaration then
5774 return;
5775
5776 elsif Nkind_In (Kind, N_Formal_Object_Declaration,
5777 N_Formal_Package_Declaration)
5778 or else Kind in N_Formal_Subprogram_Declaration
5779 then
5780 null;
5781
5782 -- Ada 2012: If both formal and actual are incomplete types they
5783 -- are conformant.
5784
5785 elsif Is_Incomplete_Type (E1) and then Is_Incomplete_Type (E2) then
5786 null;
5787
5788 elsif B then
5789 Error_Msg_NE
5790 ("actual for & in actual instance does not match formal",
5791 Parent (Actual_Pack), E1);
5792 end if;
5793 end Check_Mismatch;
5794
5795 --------------------------------
5796 -- Same_Instantiated_Constant --
5797 --------------------------------
5798
5799 function Same_Instantiated_Constant
5800 (E1, E2 : Entity_Id) return Boolean
5801 is
5802 Ent : Entity_Id;
5803
5804 begin
5805 Ent := E2;
5806 while Present (Ent) loop
5807 if E1 = Ent then
5808 return True;
5809
5810 elsif Ekind (Ent) /= E_Constant then
5811 return False;
5812
5813 elsif Is_Entity_Name (Constant_Value (Ent)) then
5814 if Entity (Constant_Value (Ent)) = E1 then
5815 return True;
5816 else
5817 Ent := Entity (Constant_Value (Ent));
5818 end if;
5819
5820 -- The actual may be a constant that has been folded. Recover
5821 -- original name.
5822
5823 elsif Is_Entity_Name (Original_Node (Constant_Value (Ent))) then
5824 Ent := Entity (Original_Node (Constant_Value (Ent)));
5825
5826 else
5827 return False;
5828 end if;
5829 end loop;
5830
5831 return False;
5832 end Same_Instantiated_Constant;
5833
5834 --------------------------------
5835 -- Same_Instantiated_Variable --
5836 --------------------------------
5837
5838 function Same_Instantiated_Variable
5839 (E1, E2 : Entity_Id) return Boolean
5840 is
5841 function Original_Entity (E : Entity_Id) return Entity_Id;
5842 -- Follow chain of renamings to the ultimate ancestor
5843
5844 ---------------------
5845 -- Original_Entity --
5846 ---------------------
5847
5848 function Original_Entity (E : Entity_Id) return Entity_Id is
5849 Orig : Entity_Id;
5850
5851 begin
5852 Orig := E;
5853 while Nkind (Parent (Orig)) = N_Object_Renaming_Declaration
5854 and then Present (Renamed_Object (Orig))
5855 and then Is_Entity_Name (Renamed_Object (Orig))
5856 loop
5857 Orig := Entity (Renamed_Object (Orig));
5858 end loop;
5859
5860 return Orig;
5861 end Original_Entity;
5862
5863 -- Start of processing for Same_Instantiated_Variable
5864
5865 begin
5866 return Ekind (E1) = Ekind (E2)
5867 and then Original_Entity (E1) = Original_Entity (E2);
5868 end Same_Instantiated_Variable;
5869
5870 -- Start of processing for Check_Formal_Package_Instance
5871
5872 begin
5873 while Present (E1) and then Present (E2) loop
5874 exit when Ekind (E1) = E_Package
5875 and then Renamed_Entity (E1) = Renamed_Entity (Actual_Pack);
5876
5877 -- If the formal is the renaming of the formal package, this
5878 -- is the end of its formal part, which may occur before the
5879 -- end of the formal part in the actual in the presence of
5880 -- defaulted parameters in the formal package.
5881
5882 exit when Nkind (Parent (E2)) = N_Package_Renaming_Declaration
5883 and then Renamed_Entity (E2) = Scope (E2);
5884
5885 -- The analysis of the actual may generate additional internal
5886 -- entities. If the formal is defaulted, there is no corresponding
5887 -- analysis and the internal entities must be skipped, until we
5888 -- find corresponding entities again.
5889
5890 if Comes_From_Source (E2)
5891 and then not Comes_From_Source (E1)
5892 and then Chars (E1) /= Chars (E2)
5893 then
5894 while Present (E1) and then Chars (E1) /= Chars (E2) loop
5895 Next_Entity (E1);
5896 end loop;
5897 end if;
5898
5899 if No (E1) then
5900 return;
5901
5902 -- If the formal entity comes from a formal declaration, it was
5903 -- defaulted in the formal package, and no check is needed on it.
5904
5905 elsif Nkind (Parent (E2)) = N_Formal_Object_Declaration then
5906 goto Next_E;
5907
5908 -- Ditto for defaulted formal subprograms.
5909
5910 elsif Is_Overloadable (E1)
5911 and then Nkind (Unit_Declaration_Node (E2)) in
5912 N_Formal_Subprogram_Declaration
5913 then
5914 goto Next_E;
5915
5916 elsif Is_Type (E1) then
5917
5918 -- Subtypes must statically match. E1, E2 are the local entities
5919 -- that are subtypes of the actuals. Itypes generated for other
5920 -- parameters need not be checked, the check will be performed
5921 -- on the parameters themselves.
5922
5923 -- If E2 is a formal type declaration, it is a defaulted parameter
5924 -- and needs no checking.
5925
5926 if not Is_Itype (E1) and then not Is_Itype (E2) then
5927 Check_Mismatch
5928 (not Is_Type (E2)
5929 or else Etype (E1) /= Etype (E2)
5930 or else not Subtypes_Statically_Match (E1, E2));
5931 end if;
5932
5933 elsif Ekind (E1) = E_Constant then
5934
5935 -- IN parameters must denote the same static value, or the same
5936 -- constant, or the literal null.
5937
5938 Expr1 := Expression (Parent (E1));
5939
5940 if Ekind (E2) /= E_Constant then
5941 Check_Mismatch (True);
5942 goto Next_E;
5943 else
5944 Expr2 := Expression (Parent (E2));
5945 end if;
5946
5947 if Is_OK_Static_Expression (Expr1) then
5948 if not Is_OK_Static_Expression (Expr2) then
5949 Check_Mismatch (True);
5950
5951 elsif Is_Discrete_Type (Etype (E1)) then
5952 declare
5953 V1 : constant Uint := Expr_Value (Expr1);
5954 V2 : constant Uint := Expr_Value (Expr2);
5955 begin
5956 Check_Mismatch (V1 /= V2);
5957 end;
5958
5959 elsif Is_Real_Type (Etype (E1)) then
5960 declare
5961 V1 : constant Ureal := Expr_Value_R (Expr1);
5962 V2 : constant Ureal := Expr_Value_R (Expr2);
5963 begin
5964 Check_Mismatch (V1 /= V2);
5965 end;
5966
5967 elsif Is_String_Type (Etype (E1))
5968 and then Nkind (Expr1) = N_String_Literal
5969 then
5970 if Nkind (Expr2) /= N_String_Literal then
5971 Check_Mismatch (True);
5972 else
5973 Check_Mismatch
5974 (not String_Equal (Strval (Expr1), Strval (Expr2)));
5975 end if;
5976 end if;
5977
5978 elsif Is_Entity_Name (Expr1) then
5979 if Is_Entity_Name (Expr2) then
5980 if Entity (Expr1) = Entity (Expr2) then
5981 null;
5982 else
5983 Check_Mismatch
5984 (not Same_Instantiated_Constant
5985 (Entity (Expr1), Entity (Expr2)));
5986 end if;
5987
5988 else
5989 Check_Mismatch (True);
5990 end if;
5991
5992 elsif Is_Entity_Name (Original_Node (Expr1))
5993 and then Is_Entity_Name (Expr2)
5994 and then Same_Instantiated_Constant
5995 (Entity (Original_Node (Expr1)), Entity (Expr2))
5996 then
5997 null;
5998
5999 elsif Nkind (Expr1) = N_Null then
6000 Check_Mismatch (Nkind (Expr1) /= N_Null);
6001
6002 else
6003 Check_Mismatch (True);
6004 end if;
6005
6006 elsif Ekind (E1) = E_Variable then
6007 Check_Mismatch (not Same_Instantiated_Variable (E1, E2));
6008
6009 elsif Ekind (E1) = E_Package then
6010 Check_Mismatch
6011 (Ekind (E1) /= Ekind (E2)
6012 or else Renamed_Object (E1) /= Renamed_Object (E2));
6013
6014 elsif Is_Overloadable (E1) then
6015
6016 -- Verify that the actual subprograms match. Note that actuals
6017 -- that are attributes are rewritten as subprograms. If the
6018 -- subprogram in the formal package is defaulted, no check is
6019 -- needed. Note that this can only happen in Ada 2005 when the
6020 -- formal package can be partially parameterized.
6021
6022 if Nkind (Unit_Declaration_Node (E1)) =
6023 N_Subprogram_Renaming_Declaration
6024 and then From_Default (Unit_Declaration_Node (E1))
6025 then
6026 null;
6027
6028 -- If the formal package has an "others" box association that
6029 -- covers this formal, there is no need for a check either.
6030
6031 elsif Nkind (Unit_Declaration_Node (E2)) in
6032 N_Formal_Subprogram_Declaration
6033 and then Box_Present (Unit_Declaration_Node (E2))
6034 then
6035 null;
6036
6037 -- No check needed if subprogram is a defaulted null procedure
6038
6039 elsif No (Alias (E2))
6040 and then Ekind (E2) = E_Procedure
6041 and then
6042 Null_Present (Specification (Unit_Declaration_Node (E2)))
6043 then
6044 null;
6045
6046 -- Otherwise the actual in the formal and the actual in the
6047 -- instantiation of the formal must match, up to renamings.
6048
6049 else
6050 Check_Mismatch
6051 (Ekind (E2) /= Ekind (E1) or else (Alias (E1)) /= Alias (E2));
6052 end if;
6053
6054 else
6055 raise Program_Error;
6056 end if;
6057
6058 <<Next_E>>
6059 Next_Entity (E1);
6060 Next_Entity (E2);
6061 end loop;
6062 end Check_Formal_Package_Instance;
6063
6064 ---------------------------
6065 -- Check_Formal_Packages --
6066 ---------------------------
6067
6068 procedure Check_Formal_Packages (P_Id : Entity_Id) is
6069 E : Entity_Id;
6070 Formal_P : Entity_Id;
6071
6072 begin
6073 -- Iterate through the declarations in the instance, looking for package
6074 -- renaming declarations that denote instances of formal packages. Stop
6075 -- when we find the renaming of the current package itself. The
6076 -- declaration for a formal package without a box is followed by an
6077 -- internal entity that repeats the instantiation.
6078
6079 E := First_Entity (P_Id);
6080 while Present (E) loop
6081 if Ekind (E) = E_Package then
6082 if Renamed_Object (E) = P_Id then
6083 exit;
6084
6085 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
6086 null;
6087
6088 elsif not Box_Present (Parent (Associated_Formal_Package (E))) then
6089 Formal_P := Next_Entity (E);
6090 Check_Formal_Package_Instance (Formal_P, E);
6091
6092 -- After checking, remove the internal validating package. It
6093 -- is only needed for semantic checks, and as it may contain
6094 -- generic formal declarations it should not reach gigi.
6095
6096 Remove (Unit_Declaration_Node (Formal_P));
6097 end if;
6098 end if;
6099
6100 Next_Entity (E);
6101 end loop;
6102 end Check_Formal_Packages;
6103
6104 ---------------------------------
6105 -- Check_Forward_Instantiation --
6106 ---------------------------------
6107
6108 procedure Check_Forward_Instantiation (Decl : Node_Id) is
6109 S : Entity_Id;
6110 Gen_Comp : Entity_Id := Cunit_Entity (Get_Source_Unit (Decl));
6111
6112 begin
6113 -- The instantiation appears before the generic body if we are in the
6114 -- scope of the unit containing the generic, either in its spec or in
6115 -- the package body, and before the generic body.
6116
6117 if Ekind (Gen_Comp) = E_Package_Body then
6118 Gen_Comp := Spec_Entity (Gen_Comp);
6119 end if;
6120
6121 if In_Open_Scopes (Gen_Comp)
6122 and then No (Corresponding_Body (Decl))
6123 then
6124 S := Current_Scope;
6125
6126 while Present (S)
6127 and then not Is_Compilation_Unit (S)
6128 and then not Is_Child_Unit (S)
6129 loop
6130 if Ekind (S) = E_Package then
6131 Set_Has_Forward_Instantiation (S);
6132 end if;
6133
6134 S := Scope (S);
6135 end loop;
6136 end if;
6137 end Check_Forward_Instantiation;
6138
6139 ---------------------------
6140 -- Check_Generic_Actuals --
6141 ---------------------------
6142
6143 -- The visibility of the actuals may be different between the point of
6144 -- generic instantiation and the instantiation of the body.
6145
6146 procedure Check_Generic_Actuals
6147 (Instance : Entity_Id;
6148 Is_Formal_Box : Boolean)
6149 is
6150 E : Entity_Id;
6151 Astype : Entity_Id;
6152
6153 function Denotes_Previous_Actual (Typ : Entity_Id) return Boolean;
6154 -- For a formal that is an array type, the component type is often a
6155 -- previous formal in the same unit. The privacy status of the component
6156 -- type will have been examined earlier in the traversal of the
6157 -- corresponding actuals, and this status should not be modified for
6158 -- the array (sub)type itself. However, if the base type of the array
6159 -- (sub)type is private, its full view must be restored in the body to
6160 -- be consistent with subsequent index subtypes, etc.
6161 --
6162 -- To detect this case we have to rescan the list of formals, which is
6163 -- usually short enough to ignore the resulting inefficiency.
6164
6165 -----------------------------
6166 -- Denotes_Previous_Actual --
6167 -----------------------------
6168
6169 function Denotes_Previous_Actual (Typ : Entity_Id) return Boolean is
6170 Prev : Entity_Id;
6171
6172 begin
6173 Prev := First_Entity (Instance);
6174 while Present (Prev) loop
6175 if Is_Type (Prev)
6176 and then Nkind (Parent (Prev)) = N_Subtype_Declaration
6177 and then Is_Entity_Name (Subtype_Indication (Parent (Prev)))
6178 and then Entity (Subtype_Indication (Parent (Prev))) = Typ
6179 then
6180 return True;
6181
6182 elsif Prev = E then
6183 return False;
6184
6185 else
6186 Next_Entity (Prev);
6187 end if;
6188 end loop;
6189
6190 return False;
6191 end Denotes_Previous_Actual;
6192
6193 -- Start of processing for Check_Generic_Actuals
6194
6195 begin
6196 E := First_Entity (Instance);
6197 while Present (E) loop
6198 if Is_Type (E)
6199 and then Nkind (Parent (E)) = N_Subtype_Declaration
6200 and then Scope (Etype (E)) /= Instance
6201 and then Is_Entity_Name (Subtype_Indication (Parent (E)))
6202 then
6203 if Is_Array_Type (E)
6204 and then not Is_Private_Type (Etype (E))
6205 and then Denotes_Previous_Actual (Component_Type (E))
6206 then
6207 null;
6208 else
6209 Check_Private_View (Subtype_Indication (Parent (E)));
6210 end if;
6211
6212 Set_Is_Generic_Actual_Type (E, True);
6213 Set_Is_Hidden (E, False);
6214 Set_Is_Potentially_Use_Visible (E,
6215 In_Use (Instance));
6216
6217 -- We constructed the generic actual type as a subtype of the
6218 -- supplied type. This means that it normally would not inherit
6219 -- subtype specific attributes of the actual, which is wrong for
6220 -- the generic case.
6221
6222 Astype := Ancestor_Subtype (E);
6223
6224 if No (Astype) then
6225
6226 -- This can happen when E is an itype that is the full view of
6227 -- a private type completed, e.g. with a constrained array. In
6228 -- that case, use the first subtype, which will carry size
6229 -- information. The base type itself is unconstrained and will
6230 -- not carry it.
6231
6232 Astype := First_Subtype (E);
6233 end if;
6234
6235 Set_Size_Info (E, (Astype));
6236 Set_RM_Size (E, RM_Size (Astype));
6237 Set_First_Rep_Item (E, First_Rep_Item (Astype));
6238
6239 if Is_Discrete_Or_Fixed_Point_Type (E) then
6240 Set_RM_Size (E, RM_Size (Astype));
6241
6242 -- In nested instances, the base type of an access actual may
6243 -- itself be private, and need to be exchanged.
6244
6245 elsif Is_Access_Type (E)
6246 and then Is_Private_Type (Etype (E))
6247 then
6248 Check_Private_View
6249 (New_Occurrence_Of (Etype (E), Sloc (Instance)));
6250 end if;
6251
6252 elsif Ekind (E) = E_Package then
6253
6254 -- If this is the renaming for the current instance, we're done.
6255 -- Otherwise it is a formal package. If the corresponding formal
6256 -- was declared with a box, the (instantiations of the) generic
6257 -- formal part are also visible. Otherwise, ignore the entity
6258 -- created to validate the actuals.
6259
6260 if Renamed_Object (E) = Instance then
6261 exit;
6262
6263 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
6264 null;
6265
6266 -- The visibility of a formal of an enclosing generic is already
6267 -- correct.
6268
6269 elsif Denotes_Formal_Package (E) then
6270 null;
6271
6272 elsif Present (Associated_Formal_Package (E))
6273 and then not Is_Generic_Formal (E)
6274 then
6275 if Box_Present (Parent (Associated_Formal_Package (E))) then
6276 Check_Generic_Actuals (Renamed_Object (E), True);
6277
6278 else
6279 Check_Generic_Actuals (Renamed_Object (E), False);
6280 end if;
6281
6282 Set_Is_Hidden (E, False);
6283 end if;
6284
6285 -- If this is a subprogram instance (in a wrapper package) the
6286 -- actual is fully visible.
6287
6288 elsif Is_Wrapper_Package (Instance) then
6289 Set_Is_Hidden (E, False);
6290
6291 -- If the formal package is declared with a box, or if the formal
6292 -- parameter is defaulted, it is visible in the body.
6293
6294 elsif Is_Formal_Box or else Is_Visible_Formal (E) then
6295 Set_Is_Hidden (E, False);
6296 end if;
6297
6298 if Ekind (E) = E_Constant then
6299
6300 -- If the type of the actual is a private type declared in the
6301 -- enclosing scope of the generic unit, the body of the generic
6302 -- sees the full view of the type (because it has to appear in
6303 -- the corresponding package body). If the type is private now,
6304 -- exchange views to restore the proper visiblity in the instance.
6305
6306 declare
6307 Typ : constant Entity_Id := Base_Type (Etype (E));
6308 -- The type of the actual
6309
6310 Gen_Id : Entity_Id;
6311 -- The generic unit
6312
6313 Parent_Scope : Entity_Id;
6314 -- The enclosing scope of the generic unit
6315
6316 begin
6317 if Is_Wrapper_Package (Instance) then
6318 Gen_Id :=
6319 Generic_Parent
6320 (Specification
6321 (Unit_Declaration_Node
6322 (Related_Instance (Instance))));
6323 else
6324 Gen_Id :=
6325 Generic_Parent (Package_Specification (Instance));
6326 end if;
6327
6328 Parent_Scope := Scope (Gen_Id);
6329
6330 -- The exchange is only needed if the generic is defined
6331 -- within a package which is not a common ancestor of the
6332 -- scope of the instance, and is not already in scope.
6333
6334 if Is_Private_Type (Typ)
6335 and then Scope (Typ) = Parent_Scope
6336 and then Scope (Instance) /= Parent_Scope
6337 and then Ekind (Parent_Scope) = E_Package
6338 and then not Is_Child_Unit (Gen_Id)
6339 then
6340 Switch_View (Typ);
6341
6342 -- If the type of the entity is a subtype, it may also have
6343 -- to be made visible, together with the base type of its
6344 -- full view, after exchange.
6345
6346 if Is_Private_Type (Etype (E)) then
6347 Switch_View (Etype (E));
6348 Switch_View (Base_Type (Etype (E)));
6349 end if;
6350 end if;
6351 end;
6352 end if;
6353
6354 Next_Entity (E);
6355 end loop;
6356 end Check_Generic_Actuals;
6357
6358 ------------------------------
6359 -- Check_Generic_Child_Unit --
6360 ------------------------------
6361
6362 procedure Check_Generic_Child_Unit
6363 (Gen_Id : Node_Id;
6364 Parent_Installed : in out Boolean)
6365 is
6366 Loc : constant Source_Ptr := Sloc (Gen_Id);
6367 Gen_Par : Entity_Id := Empty;
6368 E : Entity_Id;
6369 Inst_Par : Entity_Id;
6370 S : Node_Id;
6371
6372 function Find_Generic_Child
6373 (Scop : Entity_Id;
6374 Id : Node_Id) return Entity_Id;
6375 -- Search generic parent for possible child unit with the given name
6376
6377 function In_Enclosing_Instance return Boolean;
6378 -- Within an instance of the parent, the child unit may be denoted by
6379 -- a simple name, or an abbreviated expanded name. Examine enclosing
6380 -- scopes to locate a possible parent instantiation.
6381
6382 ------------------------
6383 -- Find_Generic_Child --
6384 ------------------------
6385
6386 function Find_Generic_Child
6387 (Scop : Entity_Id;
6388 Id : Node_Id) return Entity_Id
6389 is
6390 E : Entity_Id;
6391
6392 begin
6393 -- If entity of name is already set, instance has already been
6394 -- resolved, e.g. in an enclosing instantiation.
6395
6396 if Present (Entity (Id)) then
6397 if Scope (Entity (Id)) = Scop then
6398 return Entity (Id);
6399 else
6400 return Empty;
6401 end if;
6402
6403 else
6404 E := First_Entity (Scop);
6405 while Present (E) loop
6406 if Chars (E) = Chars (Id)
6407 and then Is_Child_Unit (E)
6408 then
6409 if Is_Child_Unit (E)
6410 and then not Is_Visible_Lib_Unit (E)
6411 then
6412 Error_Msg_NE
6413 ("generic child unit& is not visible", Gen_Id, E);
6414 end if;
6415
6416 Set_Entity (Id, E);
6417 return E;
6418 end if;
6419
6420 Next_Entity (E);
6421 end loop;
6422
6423 return Empty;
6424 end if;
6425 end Find_Generic_Child;
6426
6427 ---------------------------
6428 -- In_Enclosing_Instance --
6429 ---------------------------
6430
6431 function In_Enclosing_Instance return Boolean is
6432 Enclosing_Instance : Node_Id;
6433 Instance_Decl : Node_Id;
6434
6435 begin
6436 -- We do not inline any call that contains instantiations, except
6437 -- for instantiations of Unchecked_Conversion, so if we are within
6438 -- an inlined body the current instance does not require parents.
6439
6440 if In_Inlined_Body then
6441 pragma Assert (Chars (Gen_Id) = Name_Unchecked_Conversion);
6442 return False;
6443 end if;
6444
6445 -- Loop to check enclosing scopes
6446
6447 Enclosing_Instance := Current_Scope;
6448 while Present (Enclosing_Instance) loop
6449 Instance_Decl := Unit_Declaration_Node (Enclosing_Instance);
6450
6451 if Ekind (Enclosing_Instance) = E_Package
6452 and then Is_Generic_Instance (Enclosing_Instance)
6453 and then Present
6454 (Generic_Parent (Specification (Instance_Decl)))
6455 then
6456 -- Check whether the generic we are looking for is a child of
6457 -- this instance.
6458
6459 E := Find_Generic_Child
6460 (Generic_Parent (Specification (Instance_Decl)), Gen_Id);
6461 exit when Present (E);
6462
6463 else
6464 E := Empty;
6465 end if;
6466
6467 Enclosing_Instance := Scope (Enclosing_Instance);
6468 end loop;
6469
6470 if No (E) then
6471
6472 -- Not a child unit
6473
6474 Analyze (Gen_Id);
6475 return False;
6476
6477 else
6478 Rewrite (Gen_Id,
6479 Make_Expanded_Name (Loc,
6480 Chars => Chars (E),
6481 Prefix => New_Occurrence_Of (Enclosing_Instance, Loc),
6482 Selector_Name => New_Occurrence_Of (E, Loc)));
6483
6484 Set_Entity (Gen_Id, E);
6485 Set_Etype (Gen_Id, Etype (E));
6486 Parent_Installed := False; -- Already in scope.
6487 return True;
6488 end if;
6489 end In_Enclosing_Instance;
6490
6491 -- Start of processing for Check_Generic_Child_Unit
6492
6493 begin
6494 -- If the name of the generic is given by a selected component, it may
6495 -- be the name of a generic child unit, and the prefix is the name of an
6496 -- instance of the parent, in which case the child unit must be visible.
6497 -- If this instance is not in scope, it must be placed there and removed
6498 -- after instantiation, because what is being instantiated is not the
6499 -- original child, but the corresponding child present in the instance
6500 -- of the parent.
6501
6502 -- If the child is instantiated within the parent, it can be given by
6503 -- a simple name. In this case the instance is already in scope, but
6504 -- the child generic must be recovered from the generic parent as well.
6505
6506 if Nkind (Gen_Id) = N_Selected_Component then
6507 S := Selector_Name (Gen_Id);
6508 Analyze (Prefix (Gen_Id));
6509 Inst_Par := Entity (Prefix (Gen_Id));
6510
6511 if Ekind (Inst_Par) = E_Package
6512 and then Present (Renamed_Object (Inst_Par))
6513 then
6514 Inst_Par := Renamed_Object (Inst_Par);
6515 end if;
6516
6517 if Ekind (Inst_Par) = E_Package then
6518 if Nkind (Parent (Inst_Par)) = N_Package_Specification then
6519 Gen_Par := Generic_Parent (Parent (Inst_Par));
6520
6521 elsif Nkind (Parent (Inst_Par)) = N_Defining_Program_Unit_Name
6522 and then
6523 Nkind (Parent (Parent (Inst_Par))) = N_Package_Specification
6524 then
6525 Gen_Par := Generic_Parent (Parent (Parent (Inst_Par)));
6526 end if;
6527
6528 elsif Ekind (Inst_Par) = E_Generic_Package
6529 and then Nkind (Parent (Gen_Id)) = N_Formal_Package_Declaration
6530 then
6531 -- A formal package may be a real child package, and not the
6532 -- implicit instance within a parent. In this case the child is
6533 -- not visible and has to be retrieved explicitly as well.
6534
6535 Gen_Par := Inst_Par;
6536 end if;
6537
6538 if Present (Gen_Par) then
6539
6540 -- The prefix denotes an instantiation. The entity itself may be a
6541 -- nested generic, or a child unit.
6542
6543 E := Find_Generic_Child (Gen_Par, S);
6544
6545 if Present (E) then
6546 Change_Selected_Component_To_Expanded_Name (Gen_Id);
6547 Set_Entity (Gen_Id, E);
6548 Set_Etype (Gen_Id, Etype (E));
6549 Set_Entity (S, E);
6550 Set_Etype (S, Etype (E));
6551
6552 -- Indicate that this is a reference to the parent
6553
6554 if In_Extended_Main_Source_Unit (Gen_Id) then
6555 Set_Is_Instantiated (Inst_Par);
6556 end if;
6557
6558 -- A common mistake is to replicate the naming scheme of a
6559 -- hierarchy by instantiating a generic child directly, rather
6560 -- than the implicit child in a parent instance:
6561
6562 -- generic .. package Gpar is ..
6563 -- generic .. package Gpar.Child is ..
6564 -- package Par is new Gpar ();
6565
6566 -- with Gpar.Child;
6567 -- package Par.Child is new Gpar.Child ();
6568 -- rather than Par.Child
6569
6570 -- In this case the instantiation is within Par, which is an
6571 -- instance, but Gpar does not denote Par because we are not IN
6572 -- the instance of Gpar, so this is illegal. The test below
6573 -- recognizes this particular case.
6574
6575 if Is_Child_Unit (E)
6576 and then not Comes_From_Source (Entity (Prefix (Gen_Id)))
6577 and then (not In_Instance
6578 or else Nkind (Parent (Parent (Gen_Id))) =
6579 N_Compilation_Unit)
6580 then
6581 Error_Msg_N
6582 ("prefix of generic child unit must be instance of parent",
6583 Gen_Id);
6584 end if;
6585
6586 if not In_Open_Scopes (Inst_Par)
6587 and then Nkind (Parent (Gen_Id)) not in
6588 N_Generic_Renaming_Declaration
6589 then
6590 Install_Parent (Inst_Par);
6591 Parent_Installed := True;
6592
6593 elsif In_Open_Scopes (Inst_Par) then
6594
6595 -- If the parent is already installed, install the actuals
6596 -- for its formal packages. This is necessary when the child
6597 -- instance is a child of the parent instance: in this case,
6598 -- the parent is placed on the scope stack but the formal
6599 -- packages are not made visible.
6600
6601 Install_Formal_Packages (Inst_Par);
6602 end if;
6603
6604 else
6605 -- If the generic parent does not contain an entity that
6606 -- corresponds to the selector, the instance doesn't either.
6607 -- Analyzing the node will yield the appropriate error message.
6608 -- If the entity is not a child unit, then it is an inner
6609 -- generic in the parent.
6610
6611 Analyze (Gen_Id);
6612 end if;
6613
6614 else
6615 Analyze (Gen_Id);
6616
6617 if Is_Child_Unit (Entity (Gen_Id))
6618 and then
6619 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
6620 and then not In_Open_Scopes (Inst_Par)
6621 then
6622 Install_Parent (Inst_Par);
6623 Parent_Installed := True;
6624
6625 -- The generic unit may be the renaming of the implicit child
6626 -- present in an instance. In that case the parent instance is
6627 -- obtained from the name of the renamed entity.
6628
6629 elsif Ekind (Entity (Gen_Id)) = E_Generic_Package
6630 and then Present (Renamed_Entity (Entity (Gen_Id)))
6631 and then Is_Child_Unit (Renamed_Entity (Entity (Gen_Id)))
6632 then
6633 declare
6634 Renamed_Package : constant Node_Id :=
6635 Name (Parent (Entity (Gen_Id)));
6636 begin
6637 if Nkind (Renamed_Package) = N_Expanded_Name then
6638 Inst_Par := Entity (Prefix (Renamed_Package));
6639 Install_Parent (Inst_Par);
6640 Parent_Installed := True;
6641 end if;
6642 end;
6643 end if;
6644 end if;
6645
6646 elsif Nkind (Gen_Id) = N_Expanded_Name then
6647
6648 -- Entity already present, analyze prefix, whose meaning may be
6649 -- an instance in the current context. If it is an instance of
6650 -- a relative within another, the proper parent may still have
6651 -- to be installed, if they are not of the same generation.
6652
6653 Analyze (Prefix (Gen_Id));
6654
6655 -- In the unlikely case that a local declaration hides the name
6656 -- of the parent package, locate it on the homonym chain. If the
6657 -- context is an instance of the parent, the renaming entity is
6658 -- flagged as such.
6659
6660 Inst_Par := Entity (Prefix (Gen_Id));
6661 while Present (Inst_Par)
6662 and then not Is_Package_Or_Generic_Package (Inst_Par)
6663 loop
6664 Inst_Par := Homonym (Inst_Par);
6665 end loop;
6666
6667 pragma Assert (Present (Inst_Par));
6668 Set_Entity (Prefix (Gen_Id), Inst_Par);
6669
6670 if In_Enclosing_Instance then
6671 null;
6672
6673 elsif Present (Entity (Gen_Id))
6674 and then Is_Child_Unit (Entity (Gen_Id))
6675 and then not In_Open_Scopes (Inst_Par)
6676 then
6677 Install_Parent (Inst_Par);
6678 Parent_Installed := True;
6679 end if;
6680
6681 elsif In_Enclosing_Instance then
6682
6683 -- The child unit is found in some enclosing scope
6684
6685 null;
6686
6687 else
6688 Analyze (Gen_Id);
6689
6690 -- If this is the renaming of the implicit child in a parent
6691 -- instance, recover the parent name and install it.
6692
6693 if Is_Entity_Name (Gen_Id) then
6694 E := Entity (Gen_Id);
6695
6696 if Is_Generic_Unit (E)
6697 and then Nkind (Parent (E)) in N_Generic_Renaming_Declaration
6698 and then Is_Child_Unit (Renamed_Object (E))
6699 and then Is_Generic_Unit (Scope (Renamed_Object (E)))
6700 and then Nkind (Name (Parent (E))) = N_Expanded_Name
6701 then
6702 Rewrite (Gen_Id, New_Copy_Tree (Name (Parent (E))));
6703 Inst_Par := Entity (Prefix (Gen_Id));
6704
6705 if not In_Open_Scopes (Inst_Par) then
6706 Install_Parent (Inst_Par);
6707 Parent_Installed := True;
6708 end if;
6709
6710 -- If it is a child unit of a non-generic parent, it may be
6711 -- use-visible and given by a direct name. Install parent as
6712 -- for other cases.
6713
6714 elsif Is_Generic_Unit (E)
6715 and then Is_Child_Unit (E)
6716 and then
6717 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
6718 and then not Is_Generic_Unit (Scope (E))
6719 then
6720 if not In_Open_Scopes (Scope (E)) then
6721 Install_Parent (Scope (E));
6722 Parent_Installed := True;
6723 end if;
6724 end if;
6725 end if;
6726 end if;
6727 end Check_Generic_Child_Unit;
6728
6729 -----------------------------
6730 -- Check_Hidden_Child_Unit --
6731 -----------------------------
6732
6733 procedure Check_Hidden_Child_Unit
6734 (N : Node_Id;
6735 Gen_Unit : Entity_Id;
6736 Act_Decl_Id : Entity_Id)
6737 is
6738 Gen_Id : constant Node_Id := Name (N);
6739
6740 begin
6741 if Is_Child_Unit (Gen_Unit)
6742 and then Is_Child_Unit (Act_Decl_Id)
6743 and then Nkind (Gen_Id) = N_Expanded_Name
6744 and then Entity (Prefix (Gen_Id)) = Scope (Act_Decl_Id)
6745 and then Chars (Gen_Unit) = Chars (Act_Decl_Id)
6746 then
6747 Error_Msg_Node_2 := Scope (Act_Decl_Id);
6748 Error_Msg_NE
6749 ("generic unit & is implicitly declared in &",
6750 Defining_Unit_Name (N), Gen_Unit);
6751 Error_Msg_N ("\instance must have different name",
6752 Defining_Unit_Name (N));
6753 end if;
6754 end Check_Hidden_Child_Unit;
6755
6756 ------------------------
6757 -- Check_Private_View --
6758 ------------------------
6759
6760 procedure Check_Private_View (N : Node_Id) is
6761 T : constant Entity_Id := Etype (N);
6762 BT : Entity_Id;
6763
6764 begin
6765 -- Exchange views if the type was not private in the generic but is
6766 -- private at the point of instantiation. Do not exchange views if
6767 -- the scope of the type is in scope. This can happen if both generic
6768 -- and instance are sibling units, or if type is defined in a parent.
6769 -- In this case the visibility of the type will be correct for all
6770 -- semantic checks.
6771
6772 if Present (T) then
6773 BT := Base_Type (T);
6774
6775 if Is_Private_Type (T)
6776 and then not Has_Private_View (N)
6777 and then Present (Full_View (T))
6778 and then not In_Open_Scopes (Scope (T))
6779 then
6780 -- In the generic, the full type was visible. Save the private
6781 -- entity, for subsequent exchange.
6782
6783 Switch_View (T);
6784
6785 elsif Has_Private_View (N)
6786 and then not Is_Private_Type (T)
6787 and then not Has_Been_Exchanged (T)
6788 and then Etype (Get_Associated_Node (N)) /= T
6789 then
6790 -- Only the private declaration was visible in the generic. If
6791 -- the type appears in a subtype declaration, the subtype in the
6792 -- instance must have a view compatible with that of its parent,
6793 -- which must be exchanged (see corresponding code in Restore_
6794 -- Private_Views). Otherwise, if the type is defined in a parent
6795 -- unit, leave full visibility within instance, which is safe.
6796
6797 if In_Open_Scopes (Scope (Base_Type (T)))
6798 and then not Is_Private_Type (Base_Type (T))
6799 and then Comes_From_Source (Base_Type (T))
6800 then
6801 null;
6802
6803 elsif Nkind (Parent (N)) = N_Subtype_Declaration
6804 or else not In_Private_Part (Scope (Base_Type (T)))
6805 then
6806 Prepend_Elmt (T, Exchanged_Views);
6807 Exchange_Declarations (Etype (Get_Associated_Node (N)));
6808 end if;
6809
6810 -- For composite types with inconsistent representation exchange
6811 -- component types accordingly.
6812
6813 elsif Is_Access_Type (T)
6814 and then Is_Private_Type (Designated_Type (T))
6815 and then not Has_Private_View (N)
6816 and then Present (Full_View (Designated_Type (T)))
6817 then
6818 Switch_View (Designated_Type (T));
6819
6820 elsif Is_Array_Type (T) then
6821 if Is_Private_Type (Component_Type (T))
6822 and then not Has_Private_View (N)
6823 and then Present (Full_View (Component_Type (T)))
6824 then
6825 Switch_View (Component_Type (T));
6826 end if;
6827
6828 -- The normal exchange mechanism relies on the setting of a
6829 -- flag on the reference in the generic. However, an additional
6830 -- mechanism is needed for types that are not explicitly
6831 -- mentioned in the generic, but may be needed in expanded code
6832 -- in the instance. This includes component types of arrays and
6833 -- designated types of access types. This processing must also
6834 -- include the index types of arrays which we take care of here.
6835
6836 declare
6837 Indx : Node_Id;
6838 Typ : Entity_Id;
6839
6840 begin
6841 Indx := First_Index (T);
6842 while Present (Indx) loop
6843 Typ := Base_Type (Etype (Indx));
6844
6845 if Is_Private_Type (Typ)
6846 and then Present (Full_View (Typ))
6847 then
6848 Switch_View (Typ);
6849 end if;
6850
6851 Next_Index (Indx);
6852 end loop;
6853 end;
6854
6855 elsif Is_Private_Type (T)
6856 and then Present (Full_View (T))
6857 and then Is_Array_Type (Full_View (T))
6858 and then Is_Private_Type (Component_Type (Full_View (T)))
6859 then
6860 Switch_View (T);
6861
6862 -- Finally, a non-private subtype may have a private base type, which
6863 -- must be exchanged for consistency. This can happen when a package
6864 -- body is instantiated, when the scope stack is empty but in fact
6865 -- the subtype and the base type are declared in an enclosing scope.
6866
6867 -- Note that in this case we introduce an inconsistency in the view
6868 -- set, because we switch the base type BT, but there could be some
6869 -- private dependent subtypes of BT which remain unswitched. Such
6870 -- subtypes might need to be switched at a later point (see specific
6871 -- provision for that case in Switch_View).
6872
6873 elsif not Is_Private_Type (T)
6874 and then not Has_Private_View (N)
6875 and then Is_Private_Type (BT)
6876 and then Present (Full_View (BT))
6877 and then not Is_Generic_Type (BT)
6878 and then not In_Open_Scopes (BT)
6879 then
6880 Prepend_Elmt (Full_View (BT), Exchanged_Views);
6881 Exchange_Declarations (BT);
6882 end if;
6883 end if;
6884 end Check_Private_View;
6885
6886 -----------------------------
6887 -- Check_Hidden_Primitives --
6888 -----------------------------
6889
6890 function Check_Hidden_Primitives (Assoc_List : List_Id) return Elist_Id is
6891 Actual : Node_Id;
6892 Gen_T : Entity_Id;
6893 Result : Elist_Id := No_Elist;
6894
6895 begin
6896 if No (Assoc_List) then
6897 return No_Elist;
6898 end if;
6899
6900 -- Traverse the list of associations between formals and actuals
6901 -- searching for renamings of tagged types
6902
6903 Actual := First (Assoc_List);
6904 while Present (Actual) loop
6905 if Nkind (Actual) = N_Subtype_Declaration then
6906 Gen_T := Generic_Parent_Type (Actual);
6907
6908 if Present (Gen_T) and then Is_Tagged_Type (Gen_T) then
6909
6910 -- Traverse the list of primitives of the actual types
6911 -- searching for hidden primitives that are visible in the
6912 -- corresponding generic formal; leave them visible and
6913 -- append them to Result to restore their decoration later.
6914
6915 Install_Hidden_Primitives
6916 (Prims_List => Result,
6917 Gen_T => Gen_T,
6918 Act_T => Entity (Subtype_Indication (Actual)));
6919 end if;
6920 end if;
6921
6922 Next (Actual);
6923 end loop;
6924
6925 return Result;
6926 end Check_Hidden_Primitives;
6927
6928 --------------------------
6929 -- Contains_Instance_Of --
6930 --------------------------
6931
6932 function Contains_Instance_Of
6933 (Inner : Entity_Id;
6934 Outer : Entity_Id;
6935 N : Node_Id) return Boolean
6936 is
6937 Elmt : Elmt_Id;
6938 Scop : Entity_Id;
6939
6940 begin
6941 Scop := Outer;
6942
6943 -- Verify that there are no circular instantiations. We check whether
6944 -- the unit contains an instance of the current scope or some enclosing
6945 -- scope (in case one of the instances appears in a subunit). Longer
6946 -- circularities involving subunits might seem too pathological to
6947 -- consider, but they were not too pathological for the authors of
6948 -- DEC bc30vsq, so we loop over all enclosing scopes, and mark all
6949 -- enclosing generic scopes as containing an instance.
6950
6951 loop
6952 -- Within a generic subprogram body, the scope is not generic, to
6953 -- allow for recursive subprograms. Use the declaration to determine
6954 -- whether this is a generic unit.
6955
6956 if Ekind (Scop) = E_Generic_Package
6957 or else (Is_Subprogram (Scop)
6958 and then Nkind (Unit_Declaration_Node (Scop)) =
6959 N_Generic_Subprogram_Declaration)
6960 then
6961 Elmt := First_Elmt (Inner_Instances (Inner));
6962
6963 while Present (Elmt) loop
6964 if Node (Elmt) = Scop then
6965 Error_Msg_Node_2 := Inner;
6966 Error_Msg_NE
6967 ("circular Instantiation: & instantiated within &!",
6968 N, Scop);
6969 return True;
6970
6971 elsif Node (Elmt) = Inner then
6972 return True;
6973
6974 elsif Contains_Instance_Of (Node (Elmt), Scop, N) then
6975 Error_Msg_Node_2 := Inner;
6976 Error_Msg_NE
6977 ("circular Instantiation: & instantiated within &!",
6978 N, Node (Elmt));
6979 return True;
6980 end if;
6981
6982 Next_Elmt (Elmt);
6983 end loop;
6984
6985 -- Indicate that Inner is being instantiated within Scop
6986
6987 Append_Elmt (Inner, Inner_Instances (Scop));
6988 end if;
6989
6990 if Scop = Standard_Standard then
6991 exit;
6992 else
6993 Scop := Scope (Scop);
6994 end if;
6995 end loop;
6996
6997 return False;
6998 end Contains_Instance_Of;
6999
7000 -----------------------
7001 -- Copy_Generic_Node --
7002 -----------------------
7003
7004 function Copy_Generic_Node
7005 (N : Node_Id;
7006 Parent_Id : Node_Id;
7007 Instantiating : Boolean) return Node_Id
7008 is
7009 Ent : Entity_Id;
7010 New_N : Node_Id;
7011
7012 function Copy_Generic_Descendant (D : Union_Id) return Union_Id;
7013 -- Check the given value of one of the Fields referenced by the current
7014 -- node to determine whether to copy it recursively. The field may hold
7015 -- a Node_Id, a List_Id, or an Elist_Id, or a plain value (Sloc, Uint,
7016 -- Char) in which case it need not be copied.
7017
7018 procedure Copy_Descendants;
7019 -- Common utility for various nodes
7020
7021 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id;
7022 -- Make copy of element list
7023
7024 function Copy_Generic_List
7025 (L : List_Id;
7026 Parent_Id : Node_Id) return List_Id;
7027 -- Apply Copy_Node recursively to the members of a node list
7028
7029 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean;
7030 -- True if an identifier is part of the defining program unit name of
7031 -- a child unit. The entity of such an identifier must be kept (for
7032 -- ASIS use) even though as the name of an enclosing generic it would
7033 -- otherwise not be preserved in the generic tree.
7034
7035 ----------------------
7036 -- Copy_Descendants --
7037 ----------------------
7038
7039 procedure Copy_Descendants is
7040 use Atree.Unchecked_Access;
7041 -- This code section is part of the implementation of an untyped
7042 -- tree traversal, so it needs direct access to node fields.
7043
7044 begin
7045 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
7046 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
7047 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
7048 Set_Field4 (New_N, Copy_Generic_Descendant (Field4 (N)));
7049 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
7050 end Copy_Descendants;
7051
7052 -----------------------------
7053 -- Copy_Generic_Descendant --
7054 -----------------------------
7055
7056 function Copy_Generic_Descendant (D : Union_Id) return Union_Id is
7057 begin
7058 if D = Union_Id (Empty) then
7059 return D;
7060
7061 elsif D in Node_Range then
7062 return Union_Id
7063 (Copy_Generic_Node (Node_Id (D), New_N, Instantiating));
7064
7065 elsif D in List_Range then
7066 return Union_Id (Copy_Generic_List (List_Id (D), New_N));
7067
7068 elsif D in Elist_Range then
7069 return Union_Id (Copy_Generic_Elist (Elist_Id (D)));
7070
7071 -- Nothing else is copyable (e.g. Uint values), return as is
7072
7073 else
7074 return D;
7075 end if;
7076 end Copy_Generic_Descendant;
7077
7078 ------------------------
7079 -- Copy_Generic_Elist --
7080 ------------------------
7081
7082 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id is
7083 M : Elmt_Id;
7084 L : Elist_Id;
7085
7086 begin
7087 if Present (E) then
7088 L := New_Elmt_List;
7089 M := First_Elmt (E);
7090 while Present (M) loop
7091 Append_Elmt
7092 (Copy_Generic_Node (Node (M), Empty, Instantiating), L);
7093 Next_Elmt (M);
7094 end loop;
7095
7096 return L;
7097
7098 else
7099 return No_Elist;
7100 end if;
7101 end Copy_Generic_Elist;
7102
7103 -----------------------
7104 -- Copy_Generic_List --
7105 -----------------------
7106
7107 function Copy_Generic_List
7108 (L : List_Id;
7109 Parent_Id : Node_Id) return List_Id
7110 is
7111 N : Node_Id;
7112 New_L : List_Id;
7113
7114 begin
7115 if Present (L) then
7116 New_L := New_List;
7117 Set_Parent (New_L, Parent_Id);
7118
7119 N := First (L);
7120 while Present (N) loop
7121 Append (Copy_Generic_Node (N, Empty, Instantiating), New_L);
7122 Next (N);
7123 end loop;
7124
7125 return New_L;
7126
7127 else
7128 return No_List;
7129 end if;
7130 end Copy_Generic_List;
7131
7132 ---------------------------
7133 -- In_Defining_Unit_Name --
7134 ---------------------------
7135
7136 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean is
7137 begin
7138 return
7139 Present (Parent (Nam))
7140 and then (Nkind (Parent (Nam)) = N_Defining_Program_Unit_Name
7141 or else
7142 (Nkind (Parent (Nam)) = N_Expanded_Name
7143 and then In_Defining_Unit_Name (Parent (Nam))));
7144 end In_Defining_Unit_Name;
7145
7146 -- Start of processing for Copy_Generic_Node
7147
7148 begin
7149 if N = Empty then
7150 return N;
7151 end if;
7152
7153 New_N := New_Copy (N);
7154
7155 -- Copy aspects if present
7156
7157 if Has_Aspects (N) then
7158 Set_Has_Aspects (New_N, False);
7159 Set_Aspect_Specifications
7160 (New_N, Copy_Generic_List (Aspect_Specifications (N), Parent_Id));
7161 end if;
7162
7163 if Instantiating then
7164 Adjust_Instantiation_Sloc (New_N, S_Adjustment);
7165 end if;
7166
7167 if not Is_List_Member (N) then
7168 Set_Parent (New_N, Parent_Id);
7169 end if;
7170
7171 -- Special casing for identifiers and other entity names and operators
7172
7173 if Nkind_In (New_N, N_Character_Literal,
7174 N_Expanded_Name,
7175 N_Identifier,
7176 N_Operator_Symbol)
7177 or else Nkind (New_N) in N_Op
7178 then
7179 if not Instantiating then
7180
7181 -- Link both nodes in order to assign subsequently the entity of
7182 -- the copy to the original node, in case this is a global
7183 -- reference.
7184
7185 Set_Associated_Node (N, New_N);
7186
7187 -- If we are within an instantiation, this is a nested generic
7188 -- that has already been analyzed at the point of definition.
7189 -- We must preserve references that were global to the enclosing
7190 -- parent at that point. Other occurrences, whether global or
7191 -- local to the current generic, must be resolved anew, so we
7192 -- reset the entity in the generic copy. A global reference has a
7193 -- smaller depth than the parent, or else the same depth in case
7194 -- both are distinct compilation units.
7195
7196 -- A child unit is implicitly declared within the enclosing parent
7197 -- but is in fact global to it, and must be preserved.
7198
7199 -- It is also possible for Current_Instantiated_Parent to be
7200 -- defined, and for this not to be a nested generic, namely if
7201 -- the unit is loaded through Rtsfind. In that case, the entity of
7202 -- New_N is only a link to the associated node, and not a defining
7203 -- occurrence.
7204
7205 -- The entities for parent units in the defining_program_unit of a
7206 -- generic child unit are established when the context of the unit
7207 -- is first analyzed, before the generic copy is made. They are
7208 -- preserved in the copy for use in ASIS queries.
7209
7210 Ent := Entity (New_N);
7211
7212 if No (Current_Instantiated_Parent.Gen_Id) then
7213 if No (Ent)
7214 or else Nkind (Ent) /= N_Defining_Identifier
7215 or else not In_Defining_Unit_Name (N)
7216 then
7217 Set_Associated_Node (New_N, Empty);
7218 end if;
7219
7220 elsif No (Ent)
7221 or else
7222 not Nkind_In (Ent, N_Defining_Identifier,
7223 N_Defining_Character_Literal,
7224 N_Defining_Operator_Symbol)
7225 or else No (Scope (Ent))
7226 or else
7227 (Scope (Ent) = Current_Instantiated_Parent.Gen_Id
7228 and then not Is_Child_Unit (Ent))
7229 or else
7230 (Scope_Depth (Scope (Ent)) >
7231 Scope_Depth (Current_Instantiated_Parent.Gen_Id)
7232 and then
7233 Get_Source_Unit (Ent) =
7234 Get_Source_Unit (Current_Instantiated_Parent.Gen_Id))
7235 then
7236 Set_Associated_Node (New_N, Empty);
7237 end if;
7238
7239 -- Case of instantiating identifier or some other name or operator
7240
7241 else
7242 -- If the associated node is still defined, the entity in it
7243 -- is global, and must be copied to the instance. If this copy
7244 -- is being made for a body to inline, it is applied to an
7245 -- instantiated tree, and the entity is already present and
7246 -- must be also preserved.
7247
7248 declare
7249 Assoc : constant Node_Id := Get_Associated_Node (N);
7250
7251 begin
7252 if Present (Assoc) then
7253 if Nkind (Assoc) = Nkind (N) then
7254 Set_Entity (New_N, Entity (Assoc));
7255 Check_Private_View (N);
7256
7257 -- The name in the call may be a selected component if the
7258 -- call has not been analyzed yet, as may be the case for
7259 -- pre/post conditions in a generic unit.
7260
7261 elsif Nkind (Assoc) = N_Function_Call
7262 and then Is_Entity_Name (Name (Assoc))
7263 then
7264 Set_Entity (New_N, Entity (Name (Assoc)));
7265
7266 elsif Nkind_In (Assoc, N_Defining_Identifier,
7267 N_Defining_Character_Literal,
7268 N_Defining_Operator_Symbol)
7269 and then Expander_Active
7270 then
7271 -- Inlining case: we are copying a tree that contains
7272 -- global entities, which are preserved in the copy to be
7273 -- used for subsequent inlining.
7274
7275 null;
7276
7277 else
7278 Set_Entity (New_N, Empty);
7279 end if;
7280 end if;
7281 end;
7282 end if;
7283
7284 -- For expanded name, we must copy the Prefix and Selector_Name
7285
7286 if Nkind (N) = N_Expanded_Name then
7287 Set_Prefix
7288 (New_N, Copy_Generic_Node (Prefix (N), New_N, Instantiating));
7289
7290 Set_Selector_Name (New_N,
7291 Copy_Generic_Node (Selector_Name (N), New_N, Instantiating));
7292
7293 -- For operators, we must copy the right operand
7294
7295 elsif Nkind (N) in N_Op then
7296 Set_Right_Opnd (New_N,
7297 Copy_Generic_Node (Right_Opnd (N), New_N, Instantiating));
7298
7299 -- And for binary operators, the left operand as well
7300
7301 if Nkind (N) in N_Binary_Op then
7302 Set_Left_Opnd (New_N,
7303 Copy_Generic_Node (Left_Opnd (N), New_N, Instantiating));
7304 end if;
7305 end if;
7306
7307 -- Establish a link between an entity from the generic template and the
7308 -- corresponding entity in the generic copy to be analyzed.
7309
7310 elsif Nkind (N) in N_Entity then
7311 if not Instantiating then
7312 Set_Associated_Entity (N, New_N);
7313 end if;
7314
7315 -- Clear any existing link the copy may inherit from the replicated
7316 -- generic template entity.
7317
7318 Set_Associated_Entity (New_N, Empty);
7319
7320 -- Special casing for stubs
7321
7322 elsif Nkind (N) in N_Body_Stub then
7323
7324 -- In any case, we must copy the specification or defining
7325 -- identifier as appropriate.
7326
7327 if Nkind (N) = N_Subprogram_Body_Stub then
7328 Set_Specification (New_N,
7329 Copy_Generic_Node (Specification (N), New_N, Instantiating));
7330
7331 else
7332 Set_Defining_Identifier (New_N,
7333 Copy_Generic_Node
7334 (Defining_Identifier (N), New_N, Instantiating));
7335 end if;
7336
7337 -- If we are not instantiating, then this is where we load and
7338 -- analyze subunits, i.e. at the point where the stub occurs. A
7339 -- more permissive system might defer this analysis to the point
7340 -- of instantiation, but this seems too complicated for now.
7341
7342 if not Instantiating then
7343 declare
7344 Subunit_Name : constant Unit_Name_Type := Get_Unit_Name (N);
7345 Subunit : Node_Id;
7346 Unum : Unit_Number_Type;
7347 New_Body : Node_Id;
7348
7349 begin
7350 -- Make sure that, if it is a subunit of the main unit that is
7351 -- preprocessed and if -gnateG is specified, the preprocessed
7352 -- file will be written.
7353
7354 Lib.Analysing_Subunit_Of_Main :=
7355 Lib.In_Extended_Main_Source_Unit (N);
7356 Unum :=
7357 Load_Unit
7358 (Load_Name => Subunit_Name,
7359 Required => False,
7360 Subunit => True,
7361 Error_Node => N);
7362 Lib.Analysing_Subunit_Of_Main := False;
7363
7364 -- If the proper body is not found, a warning message will be
7365 -- emitted when analyzing the stub, or later at the point of
7366 -- instantiation. Here we just leave the stub as is.
7367
7368 if Unum = No_Unit then
7369 Subunits_Missing := True;
7370 goto Subunit_Not_Found;
7371 end if;
7372
7373 Subunit := Cunit (Unum);
7374
7375 if Nkind (Unit (Subunit)) /= N_Subunit then
7376 Error_Msg_N
7377 ("found child unit instead of expected SEPARATE subunit",
7378 Subunit);
7379 Error_Msg_Sloc := Sloc (N);
7380 Error_Msg_N ("\to complete stub #", Subunit);
7381 goto Subunit_Not_Found;
7382 end if;
7383
7384 -- We must create a generic copy of the subunit, in order to
7385 -- perform semantic analysis on it, and we must replace the
7386 -- stub in the original generic unit with the subunit, in order
7387 -- to preserve non-local references within.
7388
7389 -- Only the proper body needs to be copied. Library_Unit and
7390 -- context clause are simply inherited by the generic copy.
7391 -- Note that the copy (which may be recursive if there are
7392 -- nested subunits) must be done first, before attaching it to
7393 -- the enclosing generic.
7394
7395 New_Body :=
7396 Copy_Generic_Node
7397 (Proper_Body (Unit (Subunit)),
7398 Empty, Instantiating => False);
7399
7400 -- Now place the original proper body in the original generic
7401 -- unit. This is a body, not a compilation unit.
7402
7403 Rewrite (N, Proper_Body (Unit (Subunit)));
7404 Set_Is_Compilation_Unit (Defining_Entity (N), False);
7405 Set_Was_Originally_Stub (N);
7406
7407 -- Finally replace the body of the subunit with its copy, and
7408 -- make this new subunit into the library unit of the generic
7409 -- copy, which does not have stubs any longer.
7410
7411 Set_Proper_Body (Unit (Subunit), New_Body);
7412 Set_Library_Unit (New_N, Subunit);
7413 Inherit_Context (Unit (Subunit), N);
7414 end;
7415
7416 -- If we are instantiating, this must be an error case, since
7417 -- otherwise we would have replaced the stub node by the proper body
7418 -- that corresponds. So just ignore it in the copy (i.e. we have
7419 -- copied it, and that is good enough).
7420
7421 else
7422 null;
7423 end if;
7424
7425 <<Subunit_Not_Found>> null;
7426
7427 -- If the node is a compilation unit, it is the subunit of a stub, which
7428 -- has been loaded already (see code below). In this case, the library
7429 -- unit field of N points to the parent unit (which is a compilation
7430 -- unit) and need not (and cannot) be copied.
7431
7432 -- When the proper body of the stub is analyzed, the library_unit link
7433 -- is used to establish the proper context (see sem_ch10).
7434
7435 -- The other fields of a compilation unit are copied as usual
7436
7437 elsif Nkind (N) = N_Compilation_Unit then
7438
7439 -- This code can only be executed when not instantiating, because in
7440 -- the copy made for an instantiation, the compilation unit node has
7441 -- disappeared at the point that a stub is replaced by its proper
7442 -- body.
7443
7444 pragma Assert (not Instantiating);
7445
7446 Set_Context_Items (New_N,
7447 Copy_Generic_List (Context_Items (N), New_N));
7448
7449 Set_Unit (New_N,
7450 Copy_Generic_Node (Unit (N), New_N, False));
7451
7452 Set_First_Inlined_Subprogram (New_N,
7453 Copy_Generic_Node
7454 (First_Inlined_Subprogram (N), New_N, False));
7455
7456 Set_Aux_Decls_Node (New_N,
7457 Copy_Generic_Node (Aux_Decls_Node (N), New_N, False));
7458
7459 -- For an assignment node, the assignment is known to be semantically
7460 -- legal if we are instantiating the template. This avoids incorrect
7461 -- diagnostics in generated code.
7462
7463 elsif Nkind (N) = N_Assignment_Statement then
7464
7465 -- Copy name and expression fields in usual manner
7466
7467 Set_Name (New_N,
7468 Copy_Generic_Node (Name (N), New_N, Instantiating));
7469
7470 Set_Expression (New_N,
7471 Copy_Generic_Node (Expression (N), New_N, Instantiating));
7472
7473 if Instantiating then
7474 Set_Assignment_OK (Name (New_N), True);
7475 end if;
7476
7477 elsif Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
7478 if not Instantiating then
7479 Set_Associated_Node (N, New_N);
7480
7481 else
7482 if Present (Get_Associated_Node (N))
7483 and then Nkind (Get_Associated_Node (N)) = Nkind (N)
7484 then
7485 -- In the generic the aggregate has some composite type. If at
7486 -- the point of instantiation the type has a private view,
7487 -- install the full view (and that of its ancestors, if any).
7488
7489 declare
7490 T : Entity_Id := (Etype (Get_Associated_Node (New_N)));
7491 Rt : Entity_Id;
7492
7493 begin
7494 if Present (T) and then Is_Private_Type (T) then
7495 Switch_View (T);
7496 end if;
7497
7498 if Present (T)
7499 and then Is_Tagged_Type (T)
7500 and then Is_Derived_Type (T)
7501 then
7502 Rt := Root_Type (T);
7503
7504 loop
7505 T := Etype (T);
7506
7507 if Is_Private_Type (T) then
7508 Switch_View (T);
7509 end if;
7510
7511 exit when T = Rt;
7512 end loop;
7513 end if;
7514 end;
7515 end if;
7516 end if;
7517
7518 -- Do not copy the associated node, which points to the generic copy
7519 -- of the aggregate.
7520
7521 declare
7522 use Atree.Unchecked_Access;
7523 -- This code section is part of the implementation of an untyped
7524 -- tree traversal, so it needs direct access to node fields.
7525
7526 begin
7527 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
7528 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
7529 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
7530 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
7531 end;
7532
7533 -- Allocators do not have an identifier denoting the access type, so we
7534 -- must locate it through the expression to check whether the views are
7535 -- consistent.
7536
7537 elsif Nkind (N) = N_Allocator
7538 and then Nkind (Expression (N)) = N_Qualified_Expression
7539 and then Is_Entity_Name (Subtype_Mark (Expression (N)))
7540 and then Instantiating
7541 then
7542 declare
7543 T : constant Node_Id :=
7544 Get_Associated_Node (Subtype_Mark (Expression (N)));
7545 Acc_T : Entity_Id;
7546
7547 begin
7548 if Present (T) then
7549
7550 -- Retrieve the allocator node in the generic copy
7551
7552 Acc_T := Etype (Parent (Parent (T)));
7553
7554 if Present (Acc_T) and then Is_Private_Type (Acc_T) then
7555 Switch_View (Acc_T);
7556 end if;
7557 end if;
7558
7559 Copy_Descendants;
7560 end;
7561
7562 -- For a proper body, we must catch the case of a proper body that
7563 -- replaces a stub. This represents the point at which a separate
7564 -- compilation unit, and hence template file, may be referenced, so we
7565 -- must make a new source instantiation entry for the template of the
7566 -- subunit, and ensure that all nodes in the subunit are adjusted using
7567 -- this new source instantiation entry.
7568
7569 elsif Nkind (N) in N_Proper_Body then
7570 declare
7571 Save_Adjustment : constant Sloc_Adjustment := S_Adjustment;
7572
7573 begin
7574 if Instantiating and then Was_Originally_Stub (N) then
7575 Create_Instantiation_Source
7576 (Instantiation_Node,
7577 Defining_Entity (N),
7578 False,
7579 S_Adjustment);
7580 end if;
7581
7582 -- Now copy the fields of the proper body, using the new
7583 -- adjustment factor if one was needed as per test above.
7584
7585 Copy_Descendants;
7586
7587 -- Restore the original adjustment factor in case changed
7588
7589 S_Adjustment := Save_Adjustment;
7590 end;
7591
7592 elsif Nkind (N) = N_Pragma and then Instantiating then
7593
7594 -- Do not copy Comment or Ident pragmas their content is relevant to
7595 -- the generic unit, not to the instantiating unit.
7596
7597 if Nam_In (Pragma_Name (N), Name_Comment, Name_Ident) then
7598 New_N := Make_Null_Statement (Sloc (N));
7599
7600 -- Do not copy pragmas generated from aspects because the pragmas do
7601 -- not carry any semantic information, plus they will be regenerated
7602 -- in the instance.
7603
7604 elsif From_Aspect_Specification (N) then
7605 New_N := Make_Null_Statement (Sloc (N));
7606
7607 else
7608 Copy_Descendants;
7609 end if;
7610
7611 elsif Nkind_In (N, N_Integer_Literal, N_Real_Literal) then
7612
7613 -- No descendant fields need traversing
7614
7615 null;
7616
7617 elsif Nkind (N) = N_String_Literal
7618 and then Present (Etype (N))
7619 and then Instantiating
7620 then
7621 -- If the string is declared in an outer scope, the string_literal
7622 -- subtype created for it may have the wrong scope. Force reanalysis
7623 -- of the constant to generate a new itype in the proper context.
7624
7625 Set_Etype (New_N, Empty);
7626 Set_Analyzed (New_N, False);
7627
7628 -- For the remaining nodes, copy their descendants recursively
7629
7630 else
7631 Copy_Descendants;
7632
7633 if Instantiating and then Nkind (N) = N_Subprogram_Body then
7634 Set_Generic_Parent (Specification (New_N), N);
7635
7636 -- Should preserve Corresponding_Spec??? (12.3(14))
7637 end if;
7638 end if;
7639
7640 return New_N;
7641 end Copy_Generic_Node;
7642
7643 ----------------------------
7644 -- Denotes_Formal_Package --
7645 ----------------------------
7646
7647 function Denotes_Formal_Package
7648 (Pack : Entity_Id;
7649 On_Exit : Boolean := False;
7650 Instance : Entity_Id := Empty) return Boolean
7651 is
7652 Par : Entity_Id;
7653 Scop : constant Entity_Id := Scope (Pack);
7654 E : Entity_Id;
7655
7656 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean;
7657 -- The package in question may be an actual for a previous formal
7658 -- package P of the current instance, so examine its actuals as well.
7659 -- This must be recursive over other formal packages.
7660
7661 ----------------------------------
7662 -- Is_Actual_Of_Previous_Formal --
7663 ----------------------------------
7664
7665 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean is
7666 E1 : Entity_Id;
7667
7668 begin
7669 E1 := First_Entity (P);
7670 while Present (E1) and then E1 /= Instance loop
7671 if Ekind (E1) = E_Package
7672 and then Nkind (Parent (E1)) = N_Package_Renaming_Declaration
7673 then
7674 if Renamed_Object (E1) = Pack then
7675 return True;
7676
7677 elsif E1 = P or else Renamed_Object (E1) = P then
7678 return False;
7679
7680 elsif Is_Actual_Of_Previous_Formal (E1) then
7681 return True;
7682 end if;
7683 end if;
7684
7685 Next_Entity (E1);
7686 end loop;
7687
7688 return False;
7689 end Is_Actual_Of_Previous_Formal;
7690
7691 -- Start of processing for Denotes_Formal_Package
7692
7693 begin
7694 if On_Exit then
7695 Par :=
7696 Instance_Envs.Table
7697 (Instance_Envs.Last).Instantiated_Parent.Act_Id;
7698 else
7699 Par := Current_Instantiated_Parent.Act_Id;
7700 end if;
7701
7702 if Ekind (Scop) = E_Generic_Package
7703 or else Nkind (Unit_Declaration_Node (Scop)) =
7704 N_Generic_Subprogram_Declaration
7705 then
7706 return True;
7707
7708 elsif Nkind (Original_Node (Unit_Declaration_Node (Pack))) =
7709 N_Formal_Package_Declaration
7710 then
7711 return True;
7712
7713 elsif No (Par) then
7714 return False;
7715
7716 else
7717 -- Check whether this package is associated with a formal package of
7718 -- the enclosing instantiation. Iterate over the list of renamings.
7719
7720 E := First_Entity (Par);
7721 while Present (E) loop
7722 if Ekind (E) /= E_Package
7723 or else Nkind (Parent (E)) /= N_Package_Renaming_Declaration
7724 then
7725 null;
7726
7727 elsif Renamed_Object (E) = Par then
7728 return False;
7729
7730 elsif Renamed_Object (E) = Pack then
7731 return True;
7732
7733 elsif Is_Actual_Of_Previous_Formal (E) then
7734 return True;
7735
7736 end if;
7737
7738 Next_Entity (E);
7739 end loop;
7740
7741 return False;
7742 end if;
7743 end Denotes_Formal_Package;
7744
7745 -----------------
7746 -- End_Generic --
7747 -----------------
7748
7749 procedure End_Generic is
7750 begin
7751 -- ??? More things could be factored out in this routine. Should
7752 -- probably be done at a later stage.
7753
7754 Inside_A_Generic := Generic_Flags.Table (Generic_Flags.Last);
7755 Generic_Flags.Decrement_Last;
7756
7757 Expander_Mode_Restore;
7758 end End_Generic;
7759
7760 -------------
7761 -- Earlier --
7762 -------------
7763
7764 function Earlier (N1, N2 : Node_Id) return Boolean is
7765 procedure Find_Depth (P : in out Node_Id; D : in out Integer);
7766 -- Find distance from given node to enclosing compilation unit
7767
7768 ----------------
7769 -- Find_Depth --
7770 ----------------
7771
7772 procedure Find_Depth (P : in out Node_Id; D : in out Integer) is
7773 begin
7774 while Present (P)
7775 and then Nkind (P) /= N_Compilation_Unit
7776 loop
7777 P := True_Parent (P);
7778 D := D + 1;
7779 end loop;
7780 end Find_Depth;
7781
7782 -- Local declarations
7783
7784 D1 : Integer := 0;
7785 D2 : Integer := 0;
7786 P1 : Node_Id := N1;
7787 P2 : Node_Id := N2;
7788 T1 : Source_Ptr;
7789 T2 : Source_Ptr;
7790
7791 -- Start of processing for Earlier
7792
7793 begin
7794 Find_Depth (P1, D1);
7795 Find_Depth (P2, D2);
7796
7797 if P1 /= P2 then
7798 return False;
7799 else
7800 P1 := N1;
7801 P2 := N2;
7802 end if;
7803
7804 while D1 > D2 loop
7805 P1 := True_Parent (P1);
7806 D1 := D1 - 1;
7807 end loop;
7808
7809 while D2 > D1 loop
7810 P2 := True_Parent (P2);
7811 D2 := D2 - 1;
7812 end loop;
7813
7814 -- At this point P1 and P2 are at the same distance from the root.
7815 -- We examine their parents until we find a common declarative list.
7816 -- If we reach the root, N1 and N2 do not descend from the same
7817 -- declarative list (e.g. one is nested in the declarative part and
7818 -- the other is in a block in the statement part) and the earlier
7819 -- one is already frozen.
7820
7821 while not Is_List_Member (P1)
7822 or else not Is_List_Member (P2)
7823 or else List_Containing (P1) /= List_Containing (P2)
7824 loop
7825 P1 := True_Parent (P1);
7826 P2 := True_Parent (P2);
7827
7828 if Nkind (Parent (P1)) = N_Subunit then
7829 P1 := Corresponding_Stub (Parent (P1));
7830 end if;
7831
7832 if Nkind (Parent (P2)) = N_Subunit then
7833 P2 := Corresponding_Stub (Parent (P2));
7834 end if;
7835
7836 if P1 = P2 then
7837 return False;
7838 end if;
7839 end loop;
7840
7841 -- Expanded code usually shares the source location of the original
7842 -- construct it was generated for. This however may not necessarely
7843 -- reflect the true location of the code within the tree.
7844
7845 -- Before comparing the slocs of the two nodes, make sure that we are
7846 -- working with correct source locations. Assume that P1 is to the left
7847 -- of P2. If either one does not come from source, traverse the common
7848 -- list heading towards the other node and locate the first source
7849 -- statement.
7850
7851 -- P1 P2
7852 -- ----+===+===+--------------+===+===+----
7853 -- expanded code expanded code
7854
7855 if not Comes_From_Source (P1) then
7856 while Present (P1) loop
7857
7858 -- Neither P2 nor a source statement were located during the
7859 -- search. If we reach the end of the list, then P1 does not
7860 -- occur earlier than P2.
7861
7862 -- ---->
7863 -- start --- P2 ----- P1 --- end
7864
7865 if No (Next (P1)) then
7866 return False;
7867
7868 -- We encounter P2 while going to the right of the list. This
7869 -- means that P1 does indeed appear earlier.
7870
7871 -- ---->
7872 -- start --- P1 ===== P2 --- end
7873 -- expanded code in between
7874
7875 elsif P1 = P2 then
7876 return True;
7877
7878 -- No need to look any further since we have located a source
7879 -- statement.
7880
7881 elsif Comes_From_Source (P1) then
7882 exit;
7883 end if;
7884
7885 -- Keep going right
7886
7887 Next (P1);
7888 end loop;
7889 end if;
7890
7891 if not Comes_From_Source (P2) then
7892 while Present (P2) loop
7893
7894 -- Neither P1 nor a source statement were located during the
7895 -- search. If we reach the start of the list, then P1 does not
7896 -- occur earlier than P2.
7897
7898 -- <----
7899 -- start --- P2 --- P1 --- end
7900
7901 if No (Prev (P2)) then
7902 return False;
7903
7904 -- We encounter P1 while going to the left of the list. This
7905 -- means that P1 does indeed appear earlier.
7906
7907 -- <----
7908 -- start --- P1 ===== P2 --- end
7909 -- expanded code in between
7910
7911 elsif P2 = P1 then
7912 return True;
7913
7914 -- No need to look any further since we have located a source
7915 -- statement.
7916
7917 elsif Comes_From_Source (P2) then
7918 exit;
7919 end if;
7920
7921 -- Keep going left
7922
7923 Prev (P2);
7924 end loop;
7925 end if;
7926
7927 -- At this point either both nodes came from source or we approximated
7928 -- their source locations through neighbouring source statements.
7929
7930 T1 := Top_Level_Location (Sloc (P1));
7931 T2 := Top_Level_Location (Sloc (P2));
7932
7933 -- When two nodes come from the same instance, they have identical top
7934 -- level locations. To determine proper relation within the tree, check
7935 -- their locations within the template.
7936
7937 if T1 = T2 then
7938 return Sloc (P1) < Sloc (P2);
7939
7940 -- The two nodes either come from unrelated instances or do not come
7941 -- from instantiated code at all.
7942
7943 else
7944 return T1 < T2;
7945 end if;
7946 end Earlier;
7947
7948 ----------------------
7949 -- Find_Actual_Type --
7950 ----------------------
7951
7952 function Find_Actual_Type
7953 (Typ : Entity_Id;
7954 Gen_Type : Entity_Id) return Entity_Id
7955 is
7956 Gen_Scope : constant Entity_Id := Scope (Gen_Type);
7957 T : Entity_Id;
7958
7959 begin
7960 -- Special processing only applies to child units
7961
7962 if not Is_Child_Unit (Gen_Scope) then
7963 return Get_Instance_Of (Typ);
7964
7965 -- If designated or component type is itself a formal of the child unit,
7966 -- its instance is available.
7967
7968 elsif Scope (Typ) = Gen_Scope then
7969 return Get_Instance_Of (Typ);
7970
7971 -- If the array or access type is not declared in the parent unit,
7972 -- no special processing needed.
7973
7974 elsif not Is_Generic_Type (Typ)
7975 and then Scope (Gen_Scope) /= Scope (Typ)
7976 then
7977 return Get_Instance_Of (Typ);
7978
7979 -- Otherwise, retrieve designated or component type by visibility
7980
7981 else
7982 T := Current_Entity (Typ);
7983 while Present (T) loop
7984 if In_Open_Scopes (Scope (T)) then
7985 return T;
7986 elsif Is_Generic_Actual_Type (T) then
7987 return T;
7988 end if;
7989
7990 T := Homonym (T);
7991 end loop;
7992
7993 return Typ;
7994 end if;
7995 end Find_Actual_Type;
7996
7997 ----------------------------
7998 -- Freeze_Subprogram_Body --
7999 ----------------------------
8000
8001 procedure Freeze_Subprogram_Body
8002 (Inst_Node : Node_Id;
8003 Gen_Body : Node_Id;
8004 Pack_Id : Entity_Id)
8005 is
8006 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
8007 Par : constant Entity_Id := Scope (Gen_Unit);
8008 E_G_Id : Entity_Id;
8009 Enc_G : Entity_Id;
8010 Enc_I : Node_Id;
8011 F_Node : Node_Id;
8012
8013 function Enclosing_Package_Body (N : Node_Id) return Node_Id;
8014 -- Find innermost package body that encloses the given node, and which
8015 -- is not a compilation unit. Freeze nodes for the instance, or for its
8016 -- enclosing body, may be inserted after the enclosing_body of the
8017 -- generic unit. Used to determine proper placement of freeze node for
8018 -- both package and subprogram instances.
8019
8020 function Package_Freeze_Node (B : Node_Id) return Node_Id;
8021 -- Find entity for given package body, and locate or create a freeze
8022 -- node for it.
8023
8024 ----------------------------
8025 -- Enclosing_Package_Body --
8026 ----------------------------
8027
8028 function Enclosing_Package_Body (N : Node_Id) return Node_Id is
8029 P : Node_Id;
8030
8031 begin
8032 P := Parent (N);
8033 while Present (P)
8034 and then Nkind (Parent (P)) /= N_Compilation_Unit
8035 loop
8036 if Nkind (P) = N_Package_Body then
8037 if Nkind (Parent (P)) = N_Subunit then
8038 return Corresponding_Stub (Parent (P));
8039 else
8040 return P;
8041 end if;
8042 end if;
8043
8044 P := True_Parent (P);
8045 end loop;
8046
8047 return Empty;
8048 end Enclosing_Package_Body;
8049
8050 -------------------------
8051 -- Package_Freeze_Node --
8052 -------------------------
8053
8054 function Package_Freeze_Node (B : Node_Id) return Node_Id is
8055 Id : Entity_Id;
8056
8057 begin
8058 if Nkind (B) = N_Package_Body then
8059 Id := Corresponding_Spec (B);
8060 else pragma Assert (Nkind (B) = N_Package_Body_Stub);
8061 Id := Corresponding_Spec (Proper_Body (Unit (Library_Unit (B))));
8062 end if;
8063
8064 Ensure_Freeze_Node (Id);
8065 return Freeze_Node (Id);
8066 end Package_Freeze_Node;
8067
8068 -- Start of processing of Freeze_Subprogram_Body
8069
8070 begin
8071 -- If the instance and the generic body appear within the same unit, and
8072 -- the instance precedes the generic, the freeze node for the instance
8073 -- must appear after that of the generic. If the generic is nested
8074 -- within another instance I2, then current instance must be frozen
8075 -- after I2. In both cases, the freeze nodes are those of enclosing
8076 -- packages. Otherwise, the freeze node is placed at the end of the
8077 -- current declarative part.
8078
8079 Enc_G := Enclosing_Package_Body (Gen_Body);
8080 Enc_I := Enclosing_Package_Body (Inst_Node);
8081 Ensure_Freeze_Node (Pack_Id);
8082 F_Node := Freeze_Node (Pack_Id);
8083
8084 if Is_Generic_Instance (Par)
8085 and then Present (Freeze_Node (Par))
8086 and then In_Same_Declarative_Part (Freeze_Node (Par), Inst_Node)
8087 then
8088 -- The parent was a premature instantiation. Insert freeze node at
8089 -- the end the current declarative part.
8090
8091 if ABE_Is_Certain (Get_Package_Instantiation_Node (Par)) then
8092 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8093
8094 -- Handle the following case:
8095 --
8096 -- package Parent_Inst is new ...
8097 -- Parent_Inst []
8098 --
8099 -- procedure P ... -- this body freezes Parent_Inst
8100 --
8101 -- package Inst is new ...
8102 --
8103 -- In this particular scenario, the freeze node for Inst must be
8104 -- inserted in the same manner as that of Parent_Inst - before the
8105 -- next source body or at the end of the declarative list (body not
8106 -- available). If body P did not exist and Parent_Inst was frozen
8107 -- after Inst, either by a body following Inst or at the end of the
8108 -- declarative region, the freeze node for Inst must be inserted
8109 -- after that of Parent_Inst. This relation is established by
8110 -- comparing the Slocs of Parent_Inst freeze node and Inst.
8111
8112 elsif List_Containing (Get_Package_Instantiation_Node (Par)) =
8113 List_Containing (Inst_Node)
8114 and then Sloc (Freeze_Node (Par)) < Sloc (Inst_Node)
8115 then
8116 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8117
8118 else
8119 Insert_After (Freeze_Node (Par), F_Node);
8120 end if;
8121
8122 -- The body enclosing the instance should be frozen after the body that
8123 -- includes the generic, because the body of the instance may make
8124 -- references to entities therein. If the two are not in the same
8125 -- declarative part, or if the one enclosing the instance is frozen
8126 -- already, freeze the instance at the end of the current declarative
8127 -- part.
8128
8129 elsif Is_Generic_Instance (Par)
8130 and then Present (Freeze_Node (Par))
8131 and then Present (Enc_I)
8132 then
8133 if In_Same_Declarative_Part (Freeze_Node (Par), Enc_I)
8134 or else
8135 (Nkind (Enc_I) = N_Package_Body
8136 and then
8137 In_Same_Declarative_Part (Freeze_Node (Par), Parent (Enc_I)))
8138 then
8139 -- The enclosing package may contain several instances. Rather
8140 -- than computing the earliest point at which to insert its freeze
8141 -- node, we place it at the end of the declarative part of the
8142 -- parent of the generic.
8143
8144 Insert_Freeze_Node_For_Instance
8145 (Freeze_Node (Par), Package_Freeze_Node (Enc_I));
8146 end if;
8147
8148 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8149
8150 elsif Present (Enc_G)
8151 and then Present (Enc_I)
8152 and then Enc_G /= Enc_I
8153 and then Earlier (Inst_Node, Gen_Body)
8154 then
8155 if Nkind (Enc_G) = N_Package_Body then
8156 E_G_Id :=
8157 Corresponding_Spec (Enc_G);
8158 else pragma Assert (Nkind (Enc_G) = N_Package_Body_Stub);
8159 E_G_Id :=
8160 Corresponding_Spec (Proper_Body (Unit (Library_Unit (Enc_G))));
8161 end if;
8162
8163 -- Freeze package that encloses instance, and place node after the
8164 -- package that encloses generic. If enclosing package is already
8165 -- frozen we have to assume it is at the proper place. This may be a
8166 -- potential ABE that requires dynamic checking. Do not add a freeze
8167 -- node if the package that encloses the generic is inside the body
8168 -- that encloses the instance, because the freeze node would be in
8169 -- the wrong scope. Additional contortions needed if the bodies are
8170 -- within a subunit.
8171
8172 declare
8173 Enclosing_Body : Node_Id;
8174
8175 begin
8176 if Nkind (Enc_I) = N_Package_Body_Stub then
8177 Enclosing_Body := Proper_Body (Unit (Library_Unit (Enc_I)));
8178 else
8179 Enclosing_Body := Enc_I;
8180 end if;
8181
8182 if Parent (List_Containing (Enc_G)) /= Enclosing_Body then
8183 Insert_Freeze_Node_For_Instance
8184 (Enc_G, Package_Freeze_Node (Enc_I));
8185 end if;
8186 end;
8187
8188 -- Freeze enclosing subunit before instance
8189
8190 Ensure_Freeze_Node (E_G_Id);
8191
8192 if not Is_List_Member (Freeze_Node (E_G_Id)) then
8193 Insert_After (Enc_G, Freeze_Node (E_G_Id));
8194 end if;
8195
8196 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8197
8198 else
8199 -- If none of the above, insert freeze node at the end of the current
8200 -- declarative part.
8201
8202 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8203 end if;
8204 end Freeze_Subprogram_Body;
8205
8206 ----------------
8207 -- Get_Gen_Id --
8208 ----------------
8209
8210 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id is
8211 begin
8212 return Generic_Renamings.Table (E).Gen_Id;
8213 end Get_Gen_Id;
8214
8215 ---------------------
8216 -- Get_Instance_Of --
8217 ---------------------
8218
8219 function Get_Instance_Of (A : Entity_Id) return Entity_Id is
8220 Res : constant Assoc_Ptr := Generic_Renamings_HTable.Get (A);
8221
8222 begin
8223 if Res /= Assoc_Null then
8224 return Generic_Renamings.Table (Res).Act_Id;
8225
8226 else
8227 -- On exit, entity is not instantiated: not a generic parameter, or
8228 -- else parameter of an inner generic unit.
8229
8230 return A;
8231 end if;
8232 end Get_Instance_Of;
8233
8234 ------------------------------------
8235 -- Get_Package_Instantiation_Node --
8236 ------------------------------------
8237
8238 function Get_Package_Instantiation_Node (A : Entity_Id) return Node_Id is
8239 Decl : Node_Id := Unit_Declaration_Node (A);
8240 Inst : Node_Id;
8241
8242 begin
8243 -- If the Package_Instantiation attribute has been set on the package
8244 -- entity, then use it directly when it (or its Original_Node) refers
8245 -- to an N_Package_Instantiation node. In principle it should be
8246 -- possible to have this field set in all cases, which should be
8247 -- investigated, and would allow this function to be significantly
8248 -- simplified. ???
8249
8250 Inst := Package_Instantiation (A);
8251
8252 if Present (Inst) then
8253 if Nkind (Inst) = N_Package_Instantiation then
8254 return Inst;
8255
8256 elsif Nkind (Original_Node (Inst)) = N_Package_Instantiation then
8257 return Original_Node (Inst);
8258 end if;
8259 end if;
8260
8261 -- If the instantiation is a compilation unit that does not need body
8262 -- then the instantiation node has been rewritten as a package
8263 -- declaration for the instance, and we return the original node.
8264
8265 -- If it is a compilation unit and the instance node has not been
8266 -- rewritten, then it is still the unit of the compilation. Finally, if
8267 -- a body is present, this is a parent of the main unit whose body has
8268 -- been compiled for inlining purposes, and the instantiation node has
8269 -- been rewritten with the instance body.
8270
8271 -- Otherwise the instantiation node appears after the declaration. If
8272 -- the entity is a formal package, the declaration may have been
8273 -- rewritten as a generic declaration (in the case of a formal with box)
8274 -- or left as a formal package declaration if it has actuals, and is
8275 -- found with a forward search.
8276
8277 if Nkind (Parent (Decl)) = N_Compilation_Unit then
8278 if Nkind (Decl) = N_Package_Declaration
8279 and then Present (Corresponding_Body (Decl))
8280 then
8281 Decl := Unit_Declaration_Node (Corresponding_Body (Decl));
8282 end if;
8283
8284 if Nkind (Original_Node (Decl)) = N_Package_Instantiation then
8285 return Original_Node (Decl);
8286 else
8287 return Unit (Parent (Decl));
8288 end if;
8289
8290 elsif Nkind (Decl) = N_Package_Declaration
8291 and then Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration
8292 then
8293 return Original_Node (Decl);
8294
8295 else
8296 Inst := Next (Decl);
8297 while not Nkind_In (Inst, N_Package_Instantiation,
8298 N_Formal_Package_Declaration)
8299 loop
8300 Next (Inst);
8301 end loop;
8302
8303 return Inst;
8304 end if;
8305 end Get_Package_Instantiation_Node;
8306
8307 ------------------------
8308 -- Has_Been_Exchanged --
8309 ------------------------
8310
8311 function Has_Been_Exchanged (E : Entity_Id) return Boolean is
8312 Next : Elmt_Id;
8313
8314 begin
8315 Next := First_Elmt (Exchanged_Views);
8316 while Present (Next) loop
8317 if Full_View (Node (Next)) = E then
8318 return True;
8319 end if;
8320
8321 Next_Elmt (Next);
8322 end loop;
8323
8324 return False;
8325 end Has_Been_Exchanged;
8326
8327 ----------
8328 -- Hash --
8329 ----------
8330
8331 function Hash (F : Entity_Id) return HTable_Range is
8332 begin
8333 return HTable_Range (F mod HTable_Size);
8334 end Hash;
8335
8336 ------------------------
8337 -- Hide_Current_Scope --
8338 ------------------------
8339
8340 procedure Hide_Current_Scope is
8341 C : constant Entity_Id := Current_Scope;
8342 E : Entity_Id;
8343
8344 begin
8345 Set_Is_Hidden_Open_Scope (C);
8346
8347 E := First_Entity (C);
8348 while Present (E) loop
8349 if Is_Immediately_Visible (E) then
8350 Set_Is_Immediately_Visible (E, False);
8351 Append_Elmt (E, Hidden_Entities);
8352 end if;
8353
8354 Next_Entity (E);
8355 end loop;
8356
8357 -- Make the scope name invisible as well. This is necessary, but might
8358 -- conflict with calls to Rtsfind later on, in case the scope is a
8359 -- predefined one. There is no clean solution to this problem, so for
8360 -- now we depend on the user not redefining Standard itself in one of
8361 -- the parent units.
8362
8363 if Is_Immediately_Visible (C) and then C /= Standard_Standard then
8364 Set_Is_Immediately_Visible (C, False);
8365 Append_Elmt (C, Hidden_Entities);
8366 end if;
8367
8368 end Hide_Current_Scope;
8369
8370 --------------
8371 -- Init_Env --
8372 --------------
8373
8374 procedure Init_Env is
8375 Saved : Instance_Env;
8376
8377 begin
8378 Saved.Instantiated_Parent := Current_Instantiated_Parent;
8379 Saved.Exchanged_Views := Exchanged_Views;
8380 Saved.Hidden_Entities := Hidden_Entities;
8381 Saved.Current_Sem_Unit := Current_Sem_Unit;
8382 Saved.Parent_Unit_Visible := Parent_Unit_Visible;
8383 Saved.Instance_Parent_Unit := Instance_Parent_Unit;
8384
8385 -- Save configuration switches. These may be reset if the unit is a
8386 -- predefined unit, and the current mode is not Ada 2005.
8387
8388 Save_Opt_Config_Switches (Saved.Switches);
8389
8390 Instance_Envs.Append (Saved);
8391
8392 Exchanged_Views := New_Elmt_List;
8393 Hidden_Entities := New_Elmt_List;
8394
8395 -- Make dummy entry for Instantiated parent. If generic unit is legal,
8396 -- this is set properly in Set_Instance_Env.
8397
8398 Current_Instantiated_Parent :=
8399 (Current_Scope, Current_Scope, Assoc_Null);
8400 end Init_Env;
8401
8402 ------------------------------
8403 -- In_Same_Declarative_Part --
8404 ------------------------------
8405
8406 function In_Same_Declarative_Part
8407 (F_Node : Node_Id;
8408 Inst : Node_Id) return Boolean
8409 is
8410 Decls : constant Node_Id := Parent (F_Node);
8411 Nod : Node_Id;
8412
8413 begin
8414 Nod := Parent (Inst);
8415 while Present (Nod) loop
8416 if Nod = Decls then
8417 return True;
8418
8419 elsif Nkind_In (Nod, N_Subprogram_Body,
8420 N_Package_Body,
8421 N_Package_Declaration,
8422 N_Task_Body,
8423 N_Protected_Body,
8424 N_Block_Statement)
8425 then
8426 return False;
8427
8428 elsif Nkind (Nod) = N_Subunit then
8429 Nod := Corresponding_Stub (Nod);
8430
8431 elsif Nkind (Nod) = N_Compilation_Unit then
8432 return False;
8433
8434 else
8435 Nod := Parent (Nod);
8436 end if;
8437 end loop;
8438
8439 return False;
8440 end In_Same_Declarative_Part;
8441
8442 ---------------------
8443 -- In_Main_Context --
8444 ---------------------
8445
8446 function In_Main_Context (E : Entity_Id) return Boolean is
8447 Context : List_Id;
8448 Clause : Node_Id;
8449 Nam : Node_Id;
8450
8451 begin
8452 if not Is_Compilation_Unit (E)
8453 or else Ekind (E) /= E_Package
8454 or else In_Private_Part (E)
8455 then
8456 return False;
8457 end if;
8458
8459 Context := Context_Items (Cunit (Main_Unit));
8460
8461 Clause := First (Context);
8462 while Present (Clause) loop
8463 if Nkind (Clause) = N_With_Clause then
8464 Nam := Name (Clause);
8465
8466 -- If the current scope is part of the context of the main unit,
8467 -- analysis of the corresponding with_clause is not complete, and
8468 -- the entity is not set. We use the Chars field directly, which
8469 -- might produce false positives in rare cases, but guarantees
8470 -- that we produce all the instance bodies we will need.
8471
8472 if (Is_Entity_Name (Nam) and then Chars (Nam) = Chars (E))
8473 or else (Nkind (Nam) = N_Selected_Component
8474 and then Chars (Selector_Name (Nam)) = Chars (E))
8475 then
8476 return True;
8477 end if;
8478 end if;
8479
8480 Next (Clause);
8481 end loop;
8482
8483 return False;
8484 end In_Main_Context;
8485
8486 ---------------------
8487 -- Inherit_Context --
8488 ---------------------
8489
8490 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id) is
8491 Current_Context : List_Id;
8492 Current_Unit : Node_Id;
8493 Item : Node_Id;
8494 New_I : Node_Id;
8495
8496 Clause : Node_Id;
8497 OK : Boolean;
8498 Lib_Unit : Node_Id;
8499
8500 begin
8501 if Nkind (Parent (Gen_Decl)) = N_Compilation_Unit then
8502
8503 -- The inherited context is attached to the enclosing compilation
8504 -- unit. This is either the main unit, or the declaration for the
8505 -- main unit (in case the instantiation appears within the package
8506 -- declaration and the main unit is its body).
8507
8508 Current_Unit := Parent (Inst);
8509 while Present (Current_Unit)
8510 and then Nkind (Current_Unit) /= N_Compilation_Unit
8511 loop
8512 Current_Unit := Parent (Current_Unit);
8513 end loop;
8514
8515 Current_Context := Context_Items (Current_Unit);
8516
8517 Item := First (Context_Items (Parent (Gen_Decl)));
8518 while Present (Item) loop
8519 if Nkind (Item) = N_With_Clause then
8520 Lib_Unit := Library_Unit (Item);
8521
8522 -- Take care to prevent direct cyclic with's
8523
8524 if Lib_Unit /= Current_Unit then
8525
8526 -- Do not add a unit if it is already in the context
8527
8528 Clause := First (Current_Context);
8529 OK := True;
8530 while Present (Clause) loop
8531 if Nkind (Clause) = N_With_Clause and then
8532 Library_Unit (Clause) = Lib_Unit
8533 then
8534 OK := False;
8535 exit;
8536 end if;
8537
8538 Next (Clause);
8539 end loop;
8540
8541 if OK then
8542 New_I := New_Copy (Item);
8543 Set_Implicit_With (New_I, True);
8544 Set_Implicit_With_From_Instantiation (New_I, True);
8545 Append (New_I, Current_Context);
8546 end if;
8547 end if;
8548 end if;
8549
8550 Next (Item);
8551 end loop;
8552 end if;
8553 end Inherit_Context;
8554
8555 ----------------
8556 -- Initialize --
8557 ----------------
8558
8559 procedure Initialize is
8560 begin
8561 Generic_Renamings.Init;
8562 Instance_Envs.Init;
8563 Generic_Flags.Init;
8564 Generic_Renamings_HTable.Reset;
8565 Circularity_Detected := False;
8566 Exchanged_Views := No_Elist;
8567 Hidden_Entities := No_Elist;
8568 end Initialize;
8569
8570 -------------------------------------
8571 -- Insert_Freeze_Node_For_Instance --
8572 -------------------------------------
8573
8574 procedure Insert_Freeze_Node_For_Instance
8575 (N : Node_Id;
8576 F_Node : Node_Id)
8577 is
8578 Decl : Node_Id;
8579 Decls : List_Id;
8580 Inst : Entity_Id;
8581 Par_N : Node_Id;
8582
8583 function Enclosing_Body (N : Node_Id) return Node_Id;
8584 -- Find enclosing package or subprogram body, if any. Freeze node may
8585 -- be placed at end of current declarative list if previous instance
8586 -- and current one have different enclosing bodies.
8587
8588 function Previous_Instance (Gen : Entity_Id) return Entity_Id;
8589 -- Find the local instance, if any, that declares the generic that is
8590 -- being instantiated. If present, the freeze node for this instance
8591 -- must follow the freeze node for the previous instance.
8592
8593 --------------------
8594 -- Enclosing_Body --
8595 --------------------
8596
8597 function Enclosing_Body (N : Node_Id) return Node_Id is
8598 P : Node_Id;
8599
8600 begin
8601 P := Parent (N);
8602 while Present (P)
8603 and then Nkind (Parent (P)) /= N_Compilation_Unit
8604 loop
8605 if Nkind_In (P, N_Package_Body, N_Subprogram_Body) then
8606 if Nkind (Parent (P)) = N_Subunit then
8607 return Corresponding_Stub (Parent (P));
8608 else
8609 return P;
8610 end if;
8611 end if;
8612
8613 P := True_Parent (P);
8614 end loop;
8615
8616 return Empty;
8617 end Enclosing_Body;
8618
8619 -----------------------
8620 -- Previous_Instance --
8621 -----------------------
8622
8623 function Previous_Instance (Gen : Entity_Id) return Entity_Id is
8624 S : Entity_Id;
8625
8626 begin
8627 S := Scope (Gen);
8628 while Present (S) and then S /= Standard_Standard loop
8629 if Is_Generic_Instance (S)
8630 and then In_Same_Source_Unit (S, N)
8631 then
8632 return S;
8633 end if;
8634
8635 S := Scope (S);
8636 end loop;
8637
8638 return Empty;
8639 end Previous_Instance;
8640
8641 -- Start of processing for Insert_Freeze_Node_For_Instance
8642
8643 begin
8644 if not Is_List_Member (F_Node) then
8645 Decl := N;
8646 Decls := List_Containing (N);
8647 Inst := Entity (F_Node);
8648 Par_N := Parent (Decls);
8649
8650 -- When processing a subprogram instantiation, utilize the actual
8651 -- subprogram instantiation rather than its package wrapper as it
8652 -- carries all the context information.
8653
8654 if Is_Wrapper_Package (Inst) then
8655 Inst := Related_Instance (Inst);
8656 end if;
8657
8658 -- If this is a package instance, check whether the generic is
8659 -- declared in a previous instance and the current instance is
8660 -- not within the previous one.
8661
8662 if Present (Generic_Parent (Parent (Inst)))
8663 and then Is_In_Main_Unit (N)
8664 then
8665 declare
8666 Enclosing_N : constant Node_Id := Enclosing_Body (N);
8667 Par_I : constant Entity_Id :=
8668 Previous_Instance
8669 (Generic_Parent (Parent (Inst)));
8670 Scop : Entity_Id;
8671
8672 begin
8673 if Present (Par_I)
8674 and then Earlier (N, Freeze_Node (Par_I))
8675 then
8676 Scop := Scope (Inst);
8677
8678 -- If the current instance is within the one that contains
8679 -- the generic, the freeze node for the current one must
8680 -- appear in the current declarative part. Ditto, if the
8681 -- current instance is within another package instance or
8682 -- within a body that does not enclose the current instance.
8683 -- In these three cases the freeze node of the previous
8684 -- instance is not relevant.
8685
8686 while Present (Scop) and then Scop /= Standard_Standard loop
8687 exit when Scop = Par_I
8688 or else
8689 (Is_Generic_Instance (Scop)
8690 and then Scope_Depth (Scop) > Scope_Depth (Par_I));
8691 Scop := Scope (Scop);
8692 end loop;
8693
8694 -- Previous instance encloses current instance
8695
8696 if Scop = Par_I then
8697 null;
8698
8699 -- If the next node is a source body we must freeze in
8700 -- the current scope as well.
8701
8702 elsif Present (Next (N))
8703 and then Nkind_In (Next (N), N_Subprogram_Body,
8704 N_Package_Body)
8705 and then Comes_From_Source (Next (N))
8706 then
8707 null;
8708
8709 -- Current instance is within an unrelated instance
8710
8711 elsif Is_Generic_Instance (Scop) then
8712 null;
8713
8714 -- Current instance is within an unrelated body
8715
8716 elsif Present (Enclosing_N)
8717 and then Enclosing_N /= Enclosing_Body (Par_I)
8718 then
8719 null;
8720
8721 else
8722 Insert_After (Freeze_Node (Par_I), F_Node);
8723 return;
8724 end if;
8725 end if;
8726 end;
8727 end if;
8728
8729 -- When the instantiation occurs in a package declaration, append the
8730 -- freeze node to the private declarations (if any).
8731
8732 if Nkind (Par_N) = N_Package_Specification
8733 and then Decls = Visible_Declarations (Par_N)
8734 and then Present (Private_Declarations (Par_N))
8735 and then not Is_Empty_List (Private_Declarations (Par_N))
8736 then
8737 Decls := Private_Declarations (Par_N);
8738 Decl := First (Decls);
8739 end if;
8740
8741 -- Determine the proper freeze point of a package instantiation. We
8742 -- adhere to the general rule of a package or subprogram body causing
8743 -- freezing of anything before it in the same declarative region. In
8744 -- this case, the proper freeze point of a package instantiation is
8745 -- before the first source body which follows, or before a stub. This
8746 -- ensures that entities coming from the instance are already frozen
8747 -- and usable in source bodies.
8748
8749 if Nkind (Par_N) /= N_Package_Declaration
8750 and then Ekind (Inst) = E_Package
8751 and then Is_Generic_Instance (Inst)
8752 and then
8753 not In_Same_Source_Unit (Generic_Parent (Parent (Inst)), Inst)
8754 then
8755 while Present (Decl) loop
8756 if (Nkind (Decl) in N_Unit_Body
8757 or else
8758 Nkind (Decl) in N_Body_Stub)
8759 and then Comes_From_Source (Decl)
8760 then
8761 Insert_Before (Decl, F_Node);
8762 return;
8763 end if;
8764
8765 Next (Decl);
8766 end loop;
8767 end if;
8768
8769 -- In a package declaration, or if no previous body, insert at end
8770 -- of list.
8771
8772 Set_Sloc (F_Node, Sloc (Last (Decls)));
8773 Insert_After (Last (Decls), F_Node);
8774 end if;
8775 end Insert_Freeze_Node_For_Instance;
8776
8777 ------------------
8778 -- Install_Body --
8779 ------------------
8780
8781 procedure Install_Body
8782 (Act_Body : Node_Id;
8783 N : Node_Id;
8784 Gen_Body : Node_Id;
8785 Gen_Decl : Node_Id)
8786 is
8787 Act_Id : constant Entity_Id := Corresponding_Spec (Act_Body);
8788 Act_Unit : constant Node_Id := Unit (Cunit (Get_Source_Unit (N)));
8789 Gen_Id : constant Entity_Id := Corresponding_Spec (Gen_Body);
8790 Par : constant Entity_Id := Scope (Gen_Id);
8791 Gen_Unit : constant Node_Id :=
8792 Unit (Cunit (Get_Source_Unit (Gen_Decl)));
8793 Orig_Body : Node_Id := Gen_Body;
8794 F_Node : Node_Id;
8795 Body_Unit : Node_Id;
8796
8797 Must_Delay : Boolean;
8798
8799 function In_Same_Enclosing_Subp return Boolean;
8800 -- Check whether instance and generic body are within same subprogram.
8801
8802 function True_Sloc (N : Node_Id) return Source_Ptr;
8803 -- If the instance is nested inside a generic unit, the Sloc of the
8804 -- instance indicates the place of the original definition, not the
8805 -- point of the current enclosing instance. Pending a better usage of
8806 -- Slocs to indicate instantiation places, we determine the place of
8807 -- origin of a node by finding the maximum sloc of any ancestor node.
8808 -- Why is this not equivalent to Top_Level_Location ???
8809
8810 ----------------------------
8811 -- In_Same_Enclosing_Subp --
8812 ----------------------------
8813
8814 function In_Same_Enclosing_Subp return Boolean is
8815 Scop : Entity_Id;
8816 Subp : Entity_Id;
8817
8818 begin
8819 Scop := Scope (Act_Id);
8820 while Scop /= Standard_Standard
8821 and then not Is_Overloadable (Scop)
8822 loop
8823 Scop := Scope (Scop);
8824 end loop;
8825
8826 if Scop = Standard_Standard then
8827 return False;
8828 else
8829 Subp := Scop;
8830 end if;
8831
8832 Scop := Scope (Gen_Id);
8833 while Scop /= Standard_Standard loop
8834 if Scop = Subp then
8835 return True;
8836 else
8837 Scop := Scope (Scop);
8838 end if;
8839 end loop;
8840
8841 return False;
8842 end In_Same_Enclosing_Subp;
8843
8844 ---------------
8845 -- True_Sloc --
8846 ---------------
8847
8848 function True_Sloc (N : Node_Id) return Source_Ptr is
8849 Res : Source_Ptr;
8850 N1 : Node_Id;
8851
8852 begin
8853 Res := Sloc (N);
8854 N1 := N;
8855 while Present (N1) and then N1 /= Act_Unit loop
8856 if Sloc (N1) > Res then
8857 Res := Sloc (N1);
8858 end if;
8859
8860 N1 := Parent (N1);
8861 end loop;
8862
8863 return Res;
8864 end True_Sloc;
8865
8866 -- Start of processing for Install_Body
8867
8868 begin
8869 -- Handle first the case of an instance with incomplete actual types.
8870 -- The instance body cannot be placed after the declaration because
8871 -- full views have not been seen yet. Any use of the non-limited views
8872 -- in the instance body requires the presence of a regular with_clause
8873 -- in the enclosing unit, and will fail if this with_clause is missing.
8874 -- We place the instance body at the beginning of the enclosing body,
8875 -- which is the unit being compiled. The freeze node for the instance
8876 -- is then placed after the instance body.
8877
8878 if not Is_Empty_Elmt_List (Incomplete_Actuals (Act_Id))
8879 and then Expander_Active
8880 and then Ekind (Scope (Act_Id)) = E_Package
8881 then
8882 declare
8883 Scop : constant Entity_Id := Scope (Act_Id);
8884 Body_Id : constant Node_Id :=
8885 Corresponding_Body (Unit_Declaration_Node (Scop));
8886
8887 begin
8888 Ensure_Freeze_Node (Act_Id);
8889 F_Node := Freeze_Node (Act_Id);
8890 if Present (Body_Id) then
8891 Set_Is_Frozen (Act_Id, False);
8892 Prepend (Act_Body, Declarations (Parent (Body_Id)));
8893 if Is_List_Member (F_Node) then
8894 Remove (F_Node);
8895 end if;
8896
8897 Insert_After (Act_Body, F_Node);
8898 end if;
8899 end;
8900 return;
8901 end if;
8902
8903 -- If the body is a subunit, the freeze point is the corresponding stub
8904 -- in the current compilation, not the subunit itself.
8905
8906 if Nkind (Parent (Gen_Body)) = N_Subunit then
8907 Orig_Body := Corresponding_Stub (Parent (Gen_Body));
8908 else
8909 Orig_Body := Gen_Body;
8910 end if;
8911
8912 Body_Unit := Unit (Cunit (Get_Source_Unit (Orig_Body)));
8913
8914 -- If the instantiation and the generic definition appear in the same
8915 -- package declaration, this is an early instantiation. If they appear
8916 -- in the same declarative part, it is an early instantiation only if
8917 -- the generic body appears textually later, and the generic body is
8918 -- also in the main unit.
8919
8920 -- If instance is nested within a subprogram, and the generic body
8921 -- is not, the instance is delayed because the enclosing body is. If
8922 -- instance and body are within the same scope, or the same subprogram
8923 -- body, indicate explicitly that the instance is delayed.
8924
8925 Must_Delay :=
8926 (Gen_Unit = Act_Unit
8927 and then (Nkind_In (Gen_Unit, N_Package_Declaration,
8928 N_Generic_Package_Declaration)
8929 or else (Gen_Unit = Body_Unit
8930 and then True_Sloc (N) < Sloc (Orig_Body)))
8931 and then Is_In_Main_Unit (Gen_Unit)
8932 and then (Scope (Act_Id) = Scope (Gen_Id)
8933 or else In_Same_Enclosing_Subp));
8934
8935 -- If this is an early instantiation, the freeze node is placed after
8936 -- the generic body. Otherwise, if the generic appears in an instance,
8937 -- we cannot freeze the current instance until the outer one is frozen.
8938 -- This is only relevant if the current instance is nested within some
8939 -- inner scope not itself within the outer instance. If this scope is
8940 -- a package body in the same declarative part as the outer instance,
8941 -- then that body needs to be frozen after the outer instance. Finally,
8942 -- if no delay is needed, we place the freeze node at the end of the
8943 -- current declarative part.
8944
8945 if Expander_Active then
8946 Ensure_Freeze_Node (Act_Id);
8947 F_Node := Freeze_Node (Act_Id);
8948
8949 if Must_Delay then
8950 Insert_After (Orig_Body, F_Node);
8951
8952 elsif Is_Generic_Instance (Par)
8953 and then Present (Freeze_Node (Par))
8954 and then Scope (Act_Id) /= Par
8955 then
8956 -- Freeze instance of inner generic after instance of enclosing
8957 -- generic.
8958
8959 if In_Same_Declarative_Part (Freeze_Node (Par), N) then
8960
8961 -- Handle the following case:
8962
8963 -- package Parent_Inst is new ...
8964 -- Parent_Inst []
8965
8966 -- procedure P ... -- this body freezes Parent_Inst
8967
8968 -- package Inst is new ...
8969
8970 -- In this particular scenario, the freeze node for Inst must
8971 -- be inserted in the same manner as that of Parent_Inst,
8972 -- before the next source body or at the end of the declarative
8973 -- list (body not available). If body P did not exist and
8974 -- Parent_Inst was frozen after Inst, either by a body
8975 -- following Inst or at the end of the declarative region,
8976 -- the freeze node for Inst must be inserted after that of
8977 -- Parent_Inst. This relation is established by comparing
8978 -- the Slocs of Parent_Inst freeze node and Inst.
8979
8980 if List_Containing (Get_Package_Instantiation_Node (Par)) =
8981 List_Containing (N)
8982 and then Sloc (Freeze_Node (Par)) < Sloc (N)
8983 then
8984 Insert_Freeze_Node_For_Instance (N, F_Node);
8985 else
8986 Insert_After (Freeze_Node (Par), F_Node);
8987 end if;
8988
8989 -- Freeze package enclosing instance of inner generic after
8990 -- instance of enclosing generic.
8991
8992 elsif Nkind_In (Parent (N), N_Package_Body, N_Subprogram_Body)
8993 and then In_Same_Declarative_Part (Freeze_Node (Par), Parent (N))
8994 then
8995 declare
8996 Enclosing : Entity_Id;
8997
8998 begin
8999 Enclosing := Corresponding_Spec (Parent (N));
9000
9001 if No (Enclosing) then
9002 Enclosing := Defining_Entity (Parent (N));
9003 end if;
9004
9005 Insert_Freeze_Node_For_Instance (N, F_Node);
9006 Ensure_Freeze_Node (Enclosing);
9007
9008 if not Is_List_Member (Freeze_Node (Enclosing)) then
9009
9010 -- The enclosing context is a subunit, insert the freeze
9011 -- node after the stub.
9012
9013 if Nkind (Parent (Parent (N))) = N_Subunit then
9014 Insert_Freeze_Node_For_Instance
9015 (Corresponding_Stub (Parent (Parent (N))),
9016 Freeze_Node (Enclosing));
9017
9018 -- The enclosing context is a package with a stub body
9019 -- which has already been replaced by the real body.
9020 -- Insert the freeze node after the actual body.
9021
9022 elsif Ekind (Enclosing) = E_Package
9023 and then Present (Body_Entity (Enclosing))
9024 and then Was_Originally_Stub
9025 (Parent (Body_Entity (Enclosing)))
9026 then
9027 Insert_Freeze_Node_For_Instance
9028 (Parent (Body_Entity (Enclosing)),
9029 Freeze_Node (Enclosing));
9030
9031 -- The parent instance has been frozen before the body of
9032 -- the enclosing package, insert the freeze node after
9033 -- the body.
9034
9035 elsif List_Containing (Freeze_Node (Par)) =
9036 List_Containing (Parent (N))
9037 and then Sloc (Freeze_Node (Par)) < Sloc (Parent (N))
9038 then
9039 Insert_Freeze_Node_For_Instance
9040 (Parent (N), Freeze_Node (Enclosing));
9041
9042 else
9043 Insert_After
9044 (Freeze_Node (Par), Freeze_Node (Enclosing));
9045 end if;
9046 end if;
9047 end;
9048
9049 else
9050 Insert_Freeze_Node_For_Instance (N, F_Node);
9051 end if;
9052
9053 else
9054 Insert_Freeze_Node_For_Instance (N, F_Node);
9055 end if;
9056 end if;
9057
9058 Set_Is_Frozen (Act_Id);
9059 Insert_Before (N, Act_Body);
9060 Mark_Rewrite_Insertion (Act_Body);
9061 end Install_Body;
9062
9063 -----------------------------
9064 -- Install_Formal_Packages --
9065 -----------------------------
9066
9067 procedure Install_Formal_Packages (Par : Entity_Id) is
9068 E : Entity_Id;
9069 Gen : Entity_Id;
9070 Gen_E : Entity_Id := Empty;
9071
9072 begin
9073 E := First_Entity (Par);
9074
9075 -- If we are installing an instance parent, locate the formal packages
9076 -- of its generic parent.
9077
9078 if Is_Generic_Instance (Par) then
9079 Gen := Generic_Parent (Package_Specification (Par));
9080 Gen_E := First_Entity (Gen);
9081 end if;
9082
9083 while Present (E) loop
9084 if Ekind (E) = E_Package
9085 and then Nkind (Parent (E)) = N_Package_Renaming_Declaration
9086 then
9087 -- If this is the renaming for the parent instance, done
9088
9089 if Renamed_Object (E) = Par then
9090 exit;
9091
9092 -- The visibility of a formal of an enclosing generic is already
9093 -- correct.
9094
9095 elsif Denotes_Formal_Package (E) then
9096 null;
9097
9098 elsif Present (Associated_Formal_Package (E)) then
9099 Check_Generic_Actuals (Renamed_Object (E), True);
9100 Set_Is_Hidden (E, False);
9101
9102 -- Find formal package in generic unit that corresponds to
9103 -- (instance of) formal package in instance.
9104
9105 while Present (Gen_E) and then Chars (Gen_E) /= Chars (E) loop
9106 Next_Entity (Gen_E);
9107 end loop;
9108
9109 if Present (Gen_E) then
9110 Map_Formal_Package_Entities (Gen_E, E);
9111 end if;
9112 end if;
9113 end if;
9114
9115 Next_Entity (E);
9116
9117 if Present (Gen_E) then
9118 Next_Entity (Gen_E);
9119 end if;
9120 end loop;
9121 end Install_Formal_Packages;
9122
9123 --------------------
9124 -- Install_Parent --
9125 --------------------
9126
9127 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False) is
9128 Ancestors : constant Elist_Id := New_Elmt_List;
9129 S : constant Entity_Id := Current_Scope;
9130 Inst_Par : Entity_Id;
9131 First_Par : Entity_Id;
9132 Inst_Node : Node_Id;
9133 Gen_Par : Entity_Id;
9134 First_Gen : Entity_Id;
9135 Elmt : Elmt_Id;
9136
9137 procedure Install_Noninstance_Specs (Par : Entity_Id);
9138 -- Install the scopes of noninstance parent units ending with Par
9139
9140 procedure Install_Spec (Par : Entity_Id);
9141 -- The child unit is within the declarative part of the parent, so the
9142 -- declarations within the parent are immediately visible.
9143
9144 -------------------------------
9145 -- Install_Noninstance_Specs --
9146 -------------------------------
9147
9148 procedure Install_Noninstance_Specs (Par : Entity_Id) is
9149 begin
9150 if Present (Par)
9151 and then Par /= Standard_Standard
9152 and then not In_Open_Scopes (Par)
9153 then
9154 Install_Noninstance_Specs (Scope (Par));
9155 Install_Spec (Par);
9156 end if;
9157 end Install_Noninstance_Specs;
9158
9159 ------------------
9160 -- Install_Spec --
9161 ------------------
9162
9163 procedure Install_Spec (Par : Entity_Id) is
9164 Spec : constant Node_Id := Package_Specification (Par);
9165
9166 begin
9167 -- If this parent of the child instance is a top-level unit,
9168 -- then record the unit and its visibility for later resetting in
9169 -- Remove_Parent. We exclude units that are generic instances, as we
9170 -- only want to record this information for the ultimate top-level
9171 -- noninstance parent (is that always correct???).
9172
9173 if Scope (Par) = Standard_Standard
9174 and then not Is_Generic_Instance (Par)
9175 then
9176 Parent_Unit_Visible := Is_Immediately_Visible (Par);
9177 Instance_Parent_Unit := Par;
9178 end if;
9179
9180 -- Open the parent scope and make it and its declarations visible.
9181 -- If this point is not within a body, then only the visible
9182 -- declarations should be made visible, and installation of the
9183 -- private declarations is deferred until the appropriate point
9184 -- within analysis of the spec being instantiated (see the handling
9185 -- of parent visibility in Analyze_Package_Specification). This is
9186 -- relaxed in the case where the parent unit is Ada.Tags, to avoid
9187 -- private view problems that occur when compiling instantiations of
9188 -- a generic child of that package (Generic_Dispatching_Constructor).
9189 -- If the instance freezes a tagged type, inlinings of operations
9190 -- from Ada.Tags may need the full view of type Tag. If inlining took
9191 -- proper account of establishing visibility of inlined subprograms'
9192 -- parents then it should be possible to remove this
9193 -- special check. ???
9194
9195 Push_Scope (Par);
9196 Set_Is_Immediately_Visible (Par);
9197 Install_Visible_Declarations (Par);
9198 Set_Use (Visible_Declarations (Spec));
9199
9200 if In_Body or else Is_RTU (Par, Ada_Tags) then
9201 Install_Private_Declarations (Par);
9202 Set_Use (Private_Declarations (Spec));
9203 end if;
9204 end Install_Spec;
9205
9206 -- Start of processing for Install_Parent
9207
9208 begin
9209 -- We need to install the parent instance to compile the instantiation
9210 -- of the child, but the child instance must appear in the current
9211 -- scope. Given that we cannot place the parent above the current scope
9212 -- in the scope stack, we duplicate the current scope and unstack both
9213 -- after the instantiation is complete.
9214
9215 -- If the parent is itself the instantiation of a child unit, we must
9216 -- also stack the instantiation of its parent, and so on. Each such
9217 -- ancestor is the prefix of the name in a prior instantiation.
9218
9219 -- If this is a nested instance, the parent unit itself resolves to
9220 -- a renaming of the parent instance, whose declaration we need.
9221
9222 -- Finally, the parent may be a generic (not an instance) when the
9223 -- child unit appears as a formal package.
9224
9225 Inst_Par := P;
9226
9227 if Present (Renamed_Entity (Inst_Par)) then
9228 Inst_Par := Renamed_Entity (Inst_Par);
9229 end if;
9230
9231 First_Par := Inst_Par;
9232
9233 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
9234
9235 First_Gen := Gen_Par;
9236
9237 while Present (Gen_Par) and then Is_Child_Unit (Gen_Par) loop
9238
9239 -- Load grandparent instance as well
9240
9241 Inst_Node := Get_Package_Instantiation_Node (Inst_Par);
9242
9243 if Nkind (Name (Inst_Node)) = N_Expanded_Name then
9244 Inst_Par := Entity (Prefix (Name (Inst_Node)));
9245
9246 if Present (Renamed_Entity (Inst_Par)) then
9247 Inst_Par := Renamed_Entity (Inst_Par);
9248 end if;
9249
9250 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
9251
9252 if Present (Gen_Par) then
9253 Prepend_Elmt (Inst_Par, Ancestors);
9254
9255 else
9256 -- Parent is not the name of an instantiation
9257
9258 Install_Noninstance_Specs (Inst_Par);
9259 exit;
9260 end if;
9261
9262 else
9263 -- Previous error
9264
9265 exit;
9266 end if;
9267 end loop;
9268
9269 if Present (First_Gen) then
9270 Append_Elmt (First_Par, Ancestors);
9271 else
9272 Install_Noninstance_Specs (First_Par);
9273 end if;
9274
9275 if not Is_Empty_Elmt_List (Ancestors) then
9276 Elmt := First_Elmt (Ancestors);
9277 while Present (Elmt) loop
9278 Install_Spec (Node (Elmt));
9279 Install_Formal_Packages (Node (Elmt));
9280 Next_Elmt (Elmt);
9281 end loop;
9282 end if;
9283
9284 if not In_Body then
9285 Push_Scope (S);
9286 end if;
9287 end Install_Parent;
9288
9289 -------------------------------
9290 -- Install_Hidden_Primitives --
9291 -------------------------------
9292
9293 procedure Install_Hidden_Primitives
9294 (Prims_List : in out Elist_Id;
9295 Gen_T : Entity_Id;
9296 Act_T : Entity_Id)
9297 is
9298 Elmt : Elmt_Id;
9299 List : Elist_Id := No_Elist;
9300 Prim_G_Elmt : Elmt_Id;
9301 Prim_A_Elmt : Elmt_Id;
9302 Prim_G : Node_Id;
9303 Prim_A : Node_Id;
9304
9305 begin
9306 -- No action needed in case of serious errors because we cannot trust
9307 -- in the order of primitives
9308
9309 if Serious_Errors_Detected > 0 then
9310 return;
9311
9312 -- No action possible if we don't have available the list of primitive
9313 -- operations
9314
9315 elsif No (Gen_T)
9316 or else not Is_Record_Type (Gen_T)
9317 or else not Is_Tagged_Type (Gen_T)
9318 or else not Is_Record_Type (Act_T)
9319 or else not Is_Tagged_Type (Act_T)
9320 then
9321 return;
9322
9323 -- There is no need to handle interface types since their primitives
9324 -- cannot be hidden
9325
9326 elsif Is_Interface (Gen_T) then
9327 return;
9328 end if;
9329
9330 Prim_G_Elmt := First_Elmt (Primitive_Operations (Gen_T));
9331
9332 if not Is_Class_Wide_Type (Act_T) then
9333 Prim_A_Elmt := First_Elmt (Primitive_Operations (Act_T));
9334 else
9335 Prim_A_Elmt := First_Elmt (Primitive_Operations (Root_Type (Act_T)));
9336 end if;
9337
9338 loop
9339 -- Skip predefined primitives in the generic formal
9340
9341 while Present (Prim_G_Elmt)
9342 and then Is_Predefined_Dispatching_Operation (Node (Prim_G_Elmt))
9343 loop
9344 Next_Elmt (Prim_G_Elmt);
9345 end loop;
9346
9347 -- Skip predefined primitives in the generic actual
9348
9349 while Present (Prim_A_Elmt)
9350 and then Is_Predefined_Dispatching_Operation (Node (Prim_A_Elmt))
9351 loop
9352 Next_Elmt (Prim_A_Elmt);
9353 end loop;
9354
9355 exit when No (Prim_G_Elmt) or else No (Prim_A_Elmt);
9356
9357 Prim_G := Node (Prim_G_Elmt);
9358 Prim_A := Node (Prim_A_Elmt);
9359
9360 -- There is no need to handle interface primitives because their
9361 -- primitives are not hidden
9362
9363 exit when Present (Interface_Alias (Prim_G));
9364
9365 -- Here we install one hidden primitive
9366
9367 if Chars (Prim_G) /= Chars (Prim_A)
9368 and then Has_Suffix (Prim_A, 'P')
9369 and then Remove_Suffix (Prim_A, 'P') = Chars (Prim_G)
9370 then
9371 Set_Chars (Prim_A, Chars (Prim_G));
9372 Append_New_Elmt (Prim_A, To => List);
9373 end if;
9374
9375 Next_Elmt (Prim_A_Elmt);
9376 Next_Elmt (Prim_G_Elmt);
9377 end loop;
9378
9379 -- Append the elements to the list of temporarily visible primitives
9380 -- avoiding duplicates.
9381
9382 if Present (List) then
9383 if No (Prims_List) then
9384 Prims_List := New_Elmt_List;
9385 end if;
9386
9387 Elmt := First_Elmt (List);
9388 while Present (Elmt) loop
9389 Append_Unique_Elmt (Node (Elmt), Prims_List);
9390 Next_Elmt (Elmt);
9391 end loop;
9392 end if;
9393 end Install_Hidden_Primitives;
9394
9395 -------------------------------
9396 -- Restore_Hidden_Primitives --
9397 -------------------------------
9398
9399 procedure Restore_Hidden_Primitives (Prims_List : in out Elist_Id) is
9400 Prim_Elmt : Elmt_Id;
9401 Prim : Node_Id;
9402
9403 begin
9404 if Prims_List /= No_Elist then
9405 Prim_Elmt := First_Elmt (Prims_List);
9406 while Present (Prim_Elmt) loop
9407 Prim := Node (Prim_Elmt);
9408 Set_Chars (Prim, Add_Suffix (Prim, 'P'));
9409 Next_Elmt (Prim_Elmt);
9410 end loop;
9411
9412 Prims_List := No_Elist;
9413 end if;
9414 end Restore_Hidden_Primitives;
9415
9416 --------------------------------
9417 -- Instantiate_Formal_Package --
9418 --------------------------------
9419
9420 function Instantiate_Formal_Package
9421 (Formal : Node_Id;
9422 Actual : Node_Id;
9423 Analyzed_Formal : Node_Id) return List_Id
9424 is
9425 Loc : constant Source_Ptr := Sloc (Actual);
9426 Actual_Pack : Entity_Id;
9427 Formal_Pack : Entity_Id;
9428 Gen_Parent : Entity_Id;
9429 Decls : List_Id;
9430 Nod : Node_Id;
9431 Parent_Spec : Node_Id;
9432
9433 procedure Find_Matching_Actual
9434 (F : Node_Id;
9435 Act : in out Entity_Id);
9436 -- We need to associate each formal entity in the formal package with
9437 -- the corresponding entity in the actual package. The actual package
9438 -- has been analyzed and possibly expanded, and as a result there is
9439 -- no one-to-one correspondence between the two lists (for example,
9440 -- the actual may include subtypes, itypes, and inherited primitive
9441 -- operations, interspersed among the renaming declarations for the
9442 -- actuals). We retrieve the corresponding actual by name because each
9443 -- actual has the same name as the formal, and they do appear in the
9444 -- same order.
9445
9446 function Get_Formal_Entity (N : Node_Id) return Entity_Id;
9447 -- Retrieve entity of defining entity of generic formal parameter.
9448 -- Only the declarations of formals need to be considered when
9449 -- linking them to actuals, but the declarative list may include
9450 -- internal entities generated during analysis, and those are ignored.
9451
9452 procedure Match_Formal_Entity
9453 (Formal_Node : Node_Id;
9454 Formal_Ent : Entity_Id;
9455 Actual_Ent : Entity_Id);
9456 -- Associates the formal entity with the actual. In the case where
9457 -- Formal_Ent is a formal package, this procedure iterates through all
9458 -- of its formals and enters associations between the actuals occurring
9459 -- in the formal package's corresponding actual package (given by
9460 -- Actual_Ent) and the formal package's formal parameters. This
9461 -- procedure recurses if any of the parameters is itself a package.
9462
9463 function Is_Instance_Of
9464 (Act_Spec : Entity_Id;
9465 Gen_Anc : Entity_Id) return Boolean;
9466 -- The actual can be an instantiation of a generic within another
9467 -- instance, in which case there is no direct link from it to the
9468 -- original generic ancestor. In that case, we recognize that the
9469 -- ultimate ancestor is the same by examining names and scopes.
9470
9471 procedure Process_Nested_Formal (Formal : Entity_Id);
9472 -- If the current formal is declared with a box, its own formals are
9473 -- visible in the instance, as they were in the generic, and their
9474 -- Hidden flag must be reset. If some of these formals are themselves
9475 -- packages declared with a box, the processing must be recursive.
9476
9477 --------------------------
9478 -- Find_Matching_Actual --
9479 --------------------------
9480
9481 procedure Find_Matching_Actual
9482 (F : Node_Id;
9483 Act : in out Entity_Id)
9484 is
9485 Formal_Ent : Entity_Id;
9486
9487 begin
9488 case Nkind (Original_Node (F)) is
9489 when N_Formal_Object_Declaration |
9490 N_Formal_Type_Declaration =>
9491 Formal_Ent := Defining_Identifier (F);
9492
9493 while Chars (Act) /= Chars (Formal_Ent) loop
9494 Next_Entity (Act);
9495 end loop;
9496
9497 when N_Formal_Subprogram_Declaration |
9498 N_Formal_Package_Declaration |
9499 N_Package_Declaration |
9500 N_Generic_Package_Declaration =>
9501 Formal_Ent := Defining_Entity (F);
9502
9503 while Chars (Act) /= Chars (Formal_Ent) loop
9504 Next_Entity (Act);
9505 end loop;
9506
9507 when others =>
9508 raise Program_Error;
9509 end case;
9510 end Find_Matching_Actual;
9511
9512 -------------------------
9513 -- Match_Formal_Entity --
9514 -------------------------
9515
9516 procedure Match_Formal_Entity
9517 (Formal_Node : Node_Id;
9518 Formal_Ent : Entity_Id;
9519 Actual_Ent : Entity_Id)
9520 is
9521 Act_Pkg : Entity_Id;
9522
9523 begin
9524 Set_Instance_Of (Formal_Ent, Actual_Ent);
9525
9526 if Ekind (Actual_Ent) = E_Package then
9527
9528 -- Record associations for each parameter
9529
9530 Act_Pkg := Actual_Ent;
9531
9532 declare
9533 A_Ent : Entity_Id := First_Entity (Act_Pkg);
9534 F_Ent : Entity_Id;
9535 F_Node : Node_Id;
9536
9537 Gen_Decl : Node_Id;
9538 Formals : List_Id;
9539 Actual : Entity_Id;
9540
9541 begin
9542 -- Retrieve the actual given in the formal package declaration
9543
9544 Actual := Entity (Name (Original_Node (Formal_Node)));
9545
9546 -- The actual in the formal package declaration may be a
9547 -- renamed generic package, in which case we want to retrieve
9548 -- the original generic in order to traverse its formal part.
9549
9550 if Present (Renamed_Entity (Actual)) then
9551 Gen_Decl := Unit_Declaration_Node (Renamed_Entity (Actual));
9552 else
9553 Gen_Decl := Unit_Declaration_Node (Actual);
9554 end if;
9555
9556 Formals := Generic_Formal_Declarations (Gen_Decl);
9557
9558 if Present (Formals) then
9559 F_Node := First_Non_Pragma (Formals);
9560 else
9561 F_Node := Empty;
9562 end if;
9563
9564 while Present (A_Ent)
9565 and then Present (F_Node)
9566 and then A_Ent /= First_Private_Entity (Act_Pkg)
9567 loop
9568 F_Ent := Get_Formal_Entity (F_Node);
9569
9570 if Present (F_Ent) then
9571
9572 -- This is a formal of the original package. Record
9573 -- association and recurse.
9574
9575 Find_Matching_Actual (F_Node, A_Ent);
9576 Match_Formal_Entity (F_Node, F_Ent, A_Ent);
9577 Next_Entity (A_Ent);
9578 end if;
9579
9580 Next_Non_Pragma (F_Node);
9581 end loop;
9582 end;
9583 end if;
9584 end Match_Formal_Entity;
9585
9586 -----------------------
9587 -- Get_Formal_Entity --
9588 -----------------------
9589
9590 function Get_Formal_Entity (N : Node_Id) return Entity_Id is
9591 Kind : constant Node_Kind := Nkind (Original_Node (N));
9592 begin
9593 case Kind is
9594 when N_Formal_Object_Declaration =>
9595 return Defining_Identifier (N);
9596
9597 when N_Formal_Type_Declaration =>
9598 return Defining_Identifier (N);
9599
9600 when N_Formal_Subprogram_Declaration =>
9601 return Defining_Unit_Name (Specification (N));
9602
9603 when N_Formal_Package_Declaration =>
9604 return Defining_Identifier (Original_Node (N));
9605
9606 when N_Generic_Package_Declaration =>
9607 return Defining_Identifier (Original_Node (N));
9608
9609 -- All other declarations are introduced by semantic analysis and
9610 -- have no match in the actual.
9611
9612 when others =>
9613 return Empty;
9614 end case;
9615 end Get_Formal_Entity;
9616
9617 --------------------
9618 -- Is_Instance_Of --
9619 --------------------
9620
9621 function Is_Instance_Of
9622 (Act_Spec : Entity_Id;
9623 Gen_Anc : Entity_Id) return Boolean
9624 is
9625 Gen_Par : constant Entity_Id := Generic_Parent (Act_Spec);
9626
9627 begin
9628 if No (Gen_Par) then
9629 return False;
9630
9631 -- Simplest case: the generic parent of the actual is the formal
9632
9633 elsif Gen_Par = Gen_Anc then
9634 return True;
9635
9636 elsif Chars (Gen_Par) /= Chars (Gen_Anc) then
9637 return False;
9638
9639 -- The actual may be obtained through several instantiations. Its
9640 -- scope must itself be an instance of a generic declared in the
9641 -- same scope as the formal. Any other case is detected above.
9642
9643 elsif not Is_Generic_Instance (Scope (Gen_Par)) then
9644 return False;
9645
9646 else
9647 return Generic_Parent (Parent (Scope (Gen_Par))) = Scope (Gen_Anc);
9648 end if;
9649 end Is_Instance_Of;
9650
9651 ---------------------------
9652 -- Process_Nested_Formal --
9653 ---------------------------
9654
9655 procedure Process_Nested_Formal (Formal : Entity_Id) is
9656 Ent : Entity_Id;
9657
9658 begin
9659 if Present (Associated_Formal_Package (Formal))
9660 and then Box_Present (Parent (Associated_Formal_Package (Formal)))
9661 then
9662 Ent := First_Entity (Formal);
9663 while Present (Ent) loop
9664 Set_Is_Hidden (Ent, False);
9665 Set_Is_Visible_Formal (Ent);
9666 Set_Is_Potentially_Use_Visible
9667 (Ent, Is_Potentially_Use_Visible (Formal));
9668
9669 if Ekind (Ent) = E_Package then
9670 exit when Renamed_Entity (Ent) = Renamed_Entity (Formal);
9671 Process_Nested_Formal (Ent);
9672 end if;
9673
9674 Next_Entity (Ent);
9675 end loop;
9676 end if;
9677 end Process_Nested_Formal;
9678
9679 -- Start of processing for Instantiate_Formal_Package
9680
9681 begin
9682 Analyze (Actual);
9683
9684 if not Is_Entity_Name (Actual)
9685 or else Ekind (Entity (Actual)) /= E_Package
9686 then
9687 Error_Msg_N
9688 ("expect package instance to instantiate formal", Actual);
9689 Abandon_Instantiation (Actual);
9690 raise Program_Error;
9691
9692 else
9693 Actual_Pack := Entity (Actual);
9694 Set_Is_Instantiated (Actual_Pack);
9695
9696 -- The actual may be a renamed package, or an outer generic formal
9697 -- package whose instantiation is converted into a renaming.
9698
9699 if Present (Renamed_Object (Actual_Pack)) then
9700 Actual_Pack := Renamed_Object (Actual_Pack);
9701 end if;
9702
9703 if Nkind (Analyzed_Formal) = N_Formal_Package_Declaration then
9704 Gen_Parent := Get_Instance_Of (Entity (Name (Analyzed_Formal)));
9705 Formal_Pack := Defining_Identifier (Analyzed_Formal);
9706 else
9707 Gen_Parent :=
9708 Generic_Parent (Specification (Analyzed_Formal));
9709 Formal_Pack :=
9710 Defining_Unit_Name (Specification (Analyzed_Formal));
9711 end if;
9712
9713 if Nkind (Parent (Actual_Pack)) = N_Defining_Program_Unit_Name then
9714 Parent_Spec := Package_Specification (Actual_Pack);
9715 else
9716 Parent_Spec := Parent (Actual_Pack);
9717 end if;
9718
9719 if Gen_Parent = Any_Id then
9720 Error_Msg_N
9721 ("previous error in declaration of formal package", Actual);
9722 Abandon_Instantiation (Actual);
9723
9724 elsif
9725 Is_Instance_Of (Parent_Spec, Get_Instance_Of (Gen_Parent))
9726 then
9727 null;
9728
9729 else
9730 Error_Msg_NE
9731 ("actual parameter must be instance of&", Actual, Gen_Parent);
9732 Abandon_Instantiation (Actual);
9733 end if;
9734
9735 Set_Instance_Of (Defining_Identifier (Formal), Actual_Pack);
9736 Map_Formal_Package_Entities (Formal_Pack, Actual_Pack);
9737
9738 Nod :=
9739 Make_Package_Renaming_Declaration (Loc,
9740 Defining_Unit_Name => New_Copy (Defining_Identifier (Formal)),
9741 Name => New_Occurrence_Of (Actual_Pack, Loc));
9742
9743 Set_Associated_Formal_Package
9744 (Defining_Unit_Name (Nod), Defining_Identifier (Formal));
9745 Decls := New_List (Nod);
9746
9747 -- If the formal F has a box, then the generic declarations are
9748 -- visible in the generic G. In an instance of G, the corresponding
9749 -- entities in the actual for F (which are the actuals for the
9750 -- instantiation of the generic that F denotes) must also be made
9751 -- visible for analysis of the current instance. On exit from the
9752 -- current instance, those entities are made private again. If the
9753 -- actual is currently in use, these entities are also use-visible.
9754
9755 -- The loop through the actual entities also steps through the formal
9756 -- entities and enters associations from formals to actuals into the
9757 -- renaming map. This is necessary to properly handle checking of
9758 -- actual parameter associations for later formals that depend on
9759 -- actuals declared in the formal package.
9760
9761 -- In Ada 2005, partial parameterization requires that we make
9762 -- visible the actuals corresponding to formals that were defaulted
9763 -- in the formal package. There formals are identified because they
9764 -- remain formal generics within the formal package, rather than
9765 -- being renamings of the actuals supplied.
9766
9767 declare
9768 Gen_Decl : constant Node_Id :=
9769 Unit_Declaration_Node (Gen_Parent);
9770 Formals : constant List_Id :=
9771 Generic_Formal_Declarations (Gen_Decl);
9772
9773 Actual_Ent : Entity_Id;
9774 Actual_Of_Formal : Node_Id;
9775 Formal_Node : Node_Id;
9776 Formal_Ent : Entity_Id;
9777
9778 begin
9779 if Present (Formals) then
9780 Formal_Node := First_Non_Pragma (Formals);
9781 else
9782 Formal_Node := Empty;
9783 end if;
9784
9785 Actual_Ent := First_Entity (Actual_Pack);
9786 Actual_Of_Formal :=
9787 First (Visible_Declarations (Specification (Analyzed_Formal)));
9788 while Present (Actual_Ent)
9789 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
9790 loop
9791 if Present (Formal_Node) then
9792 Formal_Ent := Get_Formal_Entity (Formal_Node);
9793
9794 if Present (Formal_Ent) then
9795 Find_Matching_Actual (Formal_Node, Actual_Ent);
9796 Match_Formal_Entity (Formal_Node, Formal_Ent, Actual_Ent);
9797
9798 -- We iterate at the same time over the actuals of the
9799 -- local package created for the formal, to determine
9800 -- which one of the formals of the original generic were
9801 -- defaulted in the formal. The corresponding actual
9802 -- entities are visible in the enclosing instance.
9803
9804 if Box_Present (Formal)
9805 or else
9806 (Present (Actual_Of_Formal)
9807 and then
9808 Is_Generic_Formal
9809 (Get_Formal_Entity (Actual_Of_Formal)))
9810 then
9811 Set_Is_Hidden (Actual_Ent, False);
9812 Set_Is_Visible_Formal (Actual_Ent);
9813 Set_Is_Potentially_Use_Visible
9814 (Actual_Ent, In_Use (Actual_Pack));
9815
9816 if Ekind (Actual_Ent) = E_Package then
9817 Process_Nested_Formal (Actual_Ent);
9818 end if;
9819
9820 else
9821 Set_Is_Hidden (Actual_Ent);
9822 Set_Is_Potentially_Use_Visible (Actual_Ent, False);
9823 end if;
9824 end if;
9825
9826 Next_Non_Pragma (Formal_Node);
9827 Next (Actual_Of_Formal);
9828
9829 else
9830 -- No further formals to match, but the generic part may
9831 -- contain inherited operation that are not hidden in the
9832 -- enclosing instance.
9833
9834 Next_Entity (Actual_Ent);
9835 end if;
9836 end loop;
9837
9838 -- Inherited subprograms generated by formal derived types are
9839 -- also visible if the types are.
9840
9841 Actual_Ent := First_Entity (Actual_Pack);
9842 while Present (Actual_Ent)
9843 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
9844 loop
9845 if Is_Overloadable (Actual_Ent)
9846 and then
9847 Nkind (Parent (Actual_Ent)) = N_Subtype_Declaration
9848 and then
9849 not Is_Hidden (Defining_Identifier (Parent (Actual_Ent)))
9850 then
9851 Set_Is_Hidden (Actual_Ent, False);
9852 Set_Is_Potentially_Use_Visible
9853 (Actual_Ent, In_Use (Actual_Pack));
9854 end if;
9855
9856 Next_Entity (Actual_Ent);
9857 end loop;
9858 end;
9859
9860 -- If the formal is not declared with a box, reanalyze it as an
9861 -- abbreviated instantiation, to verify the matching rules of 12.7.
9862 -- The actual checks are performed after the generic associations
9863 -- have been analyzed, to guarantee the same visibility for this
9864 -- instantiation and for the actuals.
9865
9866 -- In Ada 2005, the generic associations for the formal can include
9867 -- defaulted parameters. These are ignored during check. This
9868 -- internal instantiation is removed from the tree after conformance
9869 -- checking, because it contains formal declarations for those
9870 -- defaulted parameters, and those should not reach the back-end.
9871
9872 if not Box_Present (Formal) then
9873 declare
9874 I_Pack : constant Entity_Id :=
9875 Make_Temporary (Sloc (Actual), 'P');
9876
9877 begin
9878 Set_Is_Internal (I_Pack);
9879
9880 Append_To (Decls,
9881 Make_Package_Instantiation (Sloc (Actual),
9882 Defining_Unit_Name => I_Pack,
9883 Name =>
9884 New_Occurrence_Of
9885 (Get_Instance_Of (Gen_Parent), Sloc (Actual)),
9886 Generic_Associations => Generic_Associations (Formal)));
9887 end;
9888 end if;
9889
9890 return Decls;
9891 end if;
9892 end Instantiate_Formal_Package;
9893
9894 -----------------------------------
9895 -- Instantiate_Formal_Subprogram --
9896 -----------------------------------
9897
9898 function Instantiate_Formal_Subprogram
9899 (Formal : Node_Id;
9900 Actual : Node_Id;
9901 Analyzed_Formal : Node_Id) return Node_Id
9902 is
9903 Analyzed_S : constant Entity_Id :=
9904 Defining_Unit_Name (Specification (Analyzed_Formal));
9905 Formal_Sub : constant Entity_Id :=
9906 Defining_Unit_Name (Specification (Formal));
9907
9908 function From_Parent_Scope (Subp : Entity_Id) return Boolean;
9909 -- If the generic is a child unit, the parent has been installed on the
9910 -- scope stack, but a default subprogram cannot resolve to something
9911 -- on the parent because that parent is not really part of the visible
9912 -- context (it is there to resolve explicit local entities). If the
9913 -- default has resolved in this way, we remove the entity from immediate
9914 -- visibility and analyze the node again to emit an error message or
9915 -- find another visible candidate.
9916
9917 procedure Valid_Actual_Subprogram (Act : Node_Id);
9918 -- Perform legality check and raise exception on failure
9919
9920 -----------------------
9921 -- From_Parent_Scope --
9922 -----------------------
9923
9924 function From_Parent_Scope (Subp : Entity_Id) return Boolean is
9925 Gen_Scope : Node_Id;
9926
9927 begin
9928 Gen_Scope := Scope (Analyzed_S);
9929 while Present (Gen_Scope) and then Is_Child_Unit (Gen_Scope) loop
9930 if Scope (Subp) = Scope (Gen_Scope) then
9931 return True;
9932 end if;
9933
9934 Gen_Scope := Scope (Gen_Scope);
9935 end loop;
9936
9937 return False;
9938 end From_Parent_Scope;
9939
9940 -----------------------------
9941 -- Valid_Actual_Subprogram --
9942 -----------------------------
9943
9944 procedure Valid_Actual_Subprogram (Act : Node_Id) is
9945 Act_E : Entity_Id;
9946
9947 begin
9948 if Is_Entity_Name (Act) then
9949 Act_E := Entity (Act);
9950
9951 elsif Nkind (Act) = N_Selected_Component
9952 and then Is_Entity_Name (Selector_Name (Act))
9953 then
9954 Act_E := Entity (Selector_Name (Act));
9955
9956 else
9957 Act_E := Empty;
9958 end if;
9959
9960 if (Present (Act_E) and then Is_Overloadable (Act_E))
9961 or else Nkind_In (Act, N_Attribute_Reference,
9962 N_Indexed_Component,
9963 N_Character_Literal,
9964 N_Explicit_Dereference)
9965 then
9966 return;
9967 end if;
9968
9969 Error_Msg_NE
9970 ("expect subprogram or entry name in instantiation of &",
9971 Instantiation_Node, Formal_Sub);
9972 Abandon_Instantiation (Instantiation_Node);
9973 end Valid_Actual_Subprogram;
9974
9975 -- Local variables
9976
9977 Decl_Node : Node_Id;
9978 Loc : Source_Ptr;
9979 Nam : Node_Id;
9980 New_Spec : Node_Id;
9981 New_Subp : Entity_Id;
9982
9983 -- Start of processing for Instantiate_Formal_Subprogram
9984
9985 begin
9986 New_Spec := New_Copy_Tree (Specification (Formal));
9987
9988 -- The tree copy has created the proper instantiation sloc for the
9989 -- new specification. Use this location for all other constructed
9990 -- declarations.
9991
9992 Loc := Sloc (Defining_Unit_Name (New_Spec));
9993
9994 -- Create new entity for the actual (New_Copy_Tree does not), and
9995 -- indicate that it is an actual.
9996
9997 New_Subp := Make_Defining_Identifier (Loc, Chars (Formal_Sub));
9998 Set_Ekind (New_Subp, Ekind (Analyzed_S));
9999 Set_Is_Generic_Actual_Subprogram (New_Subp);
10000 Set_Defining_Unit_Name (New_Spec, New_Subp);
10001
10002 -- Create new entities for the each of the formals in the specification
10003 -- of the renaming declaration built for the actual.
10004
10005 if Present (Parameter_Specifications (New_Spec)) then
10006 declare
10007 F : Node_Id;
10008 F_Id : Entity_Id;
10009
10010 begin
10011 F := First (Parameter_Specifications (New_Spec));
10012 while Present (F) loop
10013 F_Id := Defining_Identifier (F);
10014
10015 Set_Defining_Identifier (F,
10016 Make_Defining_Identifier (Sloc (F_Id), Chars (F_Id)));
10017 Next (F);
10018 end loop;
10019 end;
10020 end if;
10021
10022 -- Find entity of actual. If the actual is an attribute reference, it
10023 -- cannot be resolved here (its formal is missing) but is handled
10024 -- instead in Attribute_Renaming. If the actual is overloaded, it is
10025 -- fully resolved subsequently, when the renaming declaration for the
10026 -- formal is analyzed. If it is an explicit dereference, resolve the
10027 -- prefix but not the actual itself, to prevent interpretation as call.
10028
10029 if Present (Actual) then
10030 Loc := Sloc (Actual);
10031 Set_Sloc (New_Spec, Loc);
10032
10033 if Nkind (Actual) = N_Operator_Symbol then
10034 Find_Direct_Name (Actual);
10035
10036 elsif Nkind (Actual) = N_Explicit_Dereference then
10037 Analyze (Prefix (Actual));
10038
10039 elsif Nkind (Actual) /= N_Attribute_Reference then
10040 Analyze (Actual);
10041 end if;
10042
10043 Valid_Actual_Subprogram (Actual);
10044 Nam := Actual;
10045
10046 elsif Present (Default_Name (Formal)) then
10047 if not Nkind_In (Default_Name (Formal), N_Attribute_Reference,
10048 N_Selected_Component,
10049 N_Indexed_Component,
10050 N_Character_Literal)
10051 and then Present (Entity (Default_Name (Formal)))
10052 then
10053 Nam := New_Occurrence_Of (Entity (Default_Name (Formal)), Loc);
10054 else
10055 Nam := New_Copy (Default_Name (Formal));
10056 Set_Sloc (Nam, Loc);
10057 end if;
10058
10059 elsif Box_Present (Formal) then
10060
10061 -- Actual is resolved at the point of instantiation. Create an
10062 -- identifier or operator with the same name as the formal.
10063
10064 if Nkind (Formal_Sub) = N_Defining_Operator_Symbol then
10065 Nam :=
10066 Make_Operator_Symbol (Loc,
10067 Chars => Chars (Formal_Sub),
10068 Strval => No_String);
10069 else
10070 Nam := Make_Identifier (Loc, Chars (Formal_Sub));
10071 end if;
10072
10073 elsif Nkind (Specification (Formal)) = N_Procedure_Specification
10074 and then Null_Present (Specification (Formal))
10075 then
10076 -- Generate null body for procedure, for use in the instance
10077
10078 Decl_Node :=
10079 Make_Subprogram_Body (Loc,
10080 Specification => New_Spec,
10081 Declarations => New_List,
10082 Handled_Statement_Sequence =>
10083 Make_Handled_Sequence_Of_Statements (Loc,
10084 Statements => New_List (Make_Null_Statement (Loc))));
10085
10086 Set_Is_Intrinsic_Subprogram (Defining_Unit_Name (New_Spec));
10087 return Decl_Node;
10088
10089 else
10090 Error_Msg_Sloc := Sloc (Scope (Analyzed_S));
10091 Error_Msg_NE
10092 ("missing actual&", Instantiation_Node, Formal_Sub);
10093 Error_Msg_NE
10094 ("\in instantiation of & declared#",
10095 Instantiation_Node, Scope (Analyzed_S));
10096 Abandon_Instantiation (Instantiation_Node);
10097 end if;
10098
10099 Decl_Node :=
10100 Make_Subprogram_Renaming_Declaration (Loc,
10101 Specification => New_Spec,
10102 Name => Nam);
10103
10104 -- If we do not have an actual and the formal specified <> then set to
10105 -- get proper default.
10106
10107 if No (Actual) and then Box_Present (Formal) then
10108 Set_From_Default (Decl_Node);
10109 end if;
10110
10111 -- Gather possible interpretations for the actual before analyzing the
10112 -- instance. If overloaded, it will be resolved when analyzing the
10113 -- renaming declaration.
10114
10115 if Box_Present (Formal) and then No (Actual) then
10116 Analyze (Nam);
10117
10118 if Is_Child_Unit (Scope (Analyzed_S))
10119 and then Present (Entity (Nam))
10120 then
10121 if not Is_Overloaded (Nam) then
10122 if From_Parent_Scope (Entity (Nam)) then
10123 Set_Is_Immediately_Visible (Entity (Nam), False);
10124 Set_Entity (Nam, Empty);
10125 Set_Etype (Nam, Empty);
10126
10127 Analyze (Nam);
10128 Set_Is_Immediately_Visible (Entity (Nam));
10129 end if;
10130
10131 else
10132 declare
10133 I : Interp_Index;
10134 It : Interp;
10135
10136 begin
10137 Get_First_Interp (Nam, I, It);
10138 while Present (It.Nam) loop
10139 if From_Parent_Scope (It.Nam) then
10140 Remove_Interp (I);
10141 end if;
10142
10143 Get_Next_Interp (I, It);
10144 end loop;
10145 end;
10146 end if;
10147 end if;
10148 end if;
10149
10150 -- The generic instantiation freezes the actual. This can only be done
10151 -- once the actual is resolved, in the analysis of the renaming
10152 -- declaration. To make the formal subprogram entity available, we set
10153 -- Corresponding_Formal_Spec to point to the formal subprogram entity.
10154 -- This is also needed in Analyze_Subprogram_Renaming for the processing
10155 -- of formal abstract subprograms.
10156
10157 Set_Corresponding_Formal_Spec (Decl_Node, Analyzed_S);
10158
10159 -- We cannot analyze the renaming declaration, and thus find the actual,
10160 -- until all the actuals are assembled in the instance. For subsequent
10161 -- checks of other actuals, indicate the node that will hold the
10162 -- instance of this formal.
10163
10164 Set_Instance_Of (Analyzed_S, Nam);
10165
10166 if Nkind (Actual) = N_Selected_Component
10167 and then Is_Task_Type (Etype (Prefix (Actual)))
10168 and then not Is_Frozen (Etype (Prefix (Actual)))
10169 then
10170 -- The renaming declaration will create a body, which must appear
10171 -- outside of the instantiation, We move the renaming declaration
10172 -- out of the instance, and create an additional renaming inside,
10173 -- to prevent freezing anomalies.
10174
10175 declare
10176 Anon_Id : constant Entity_Id := Make_Temporary (Loc, 'E');
10177
10178 begin
10179 Set_Defining_Unit_Name (New_Spec, Anon_Id);
10180 Insert_Before (Instantiation_Node, Decl_Node);
10181 Analyze (Decl_Node);
10182
10183 -- Now create renaming within the instance
10184
10185 Decl_Node :=
10186 Make_Subprogram_Renaming_Declaration (Loc,
10187 Specification => New_Copy_Tree (New_Spec),
10188 Name => New_Occurrence_Of (Anon_Id, Loc));
10189
10190 Set_Defining_Unit_Name (Specification (Decl_Node),
10191 Make_Defining_Identifier (Loc, Chars (Formal_Sub)));
10192 end;
10193 end if;
10194
10195 return Decl_Node;
10196 end Instantiate_Formal_Subprogram;
10197
10198 ------------------------
10199 -- Instantiate_Object --
10200 ------------------------
10201
10202 function Instantiate_Object
10203 (Formal : Node_Id;
10204 Actual : Node_Id;
10205 Analyzed_Formal : Node_Id) return List_Id
10206 is
10207 Gen_Obj : constant Entity_Id := Defining_Identifier (Formal);
10208 A_Gen_Obj : constant Entity_Id :=
10209 Defining_Identifier (Analyzed_Formal);
10210 Acc_Def : Node_Id := Empty;
10211 Act_Assoc : constant Node_Id := Parent (Actual);
10212 Actual_Decl : Node_Id := Empty;
10213 Decl_Node : Node_Id;
10214 Def : Node_Id;
10215 Ftyp : Entity_Id;
10216 List : constant List_Id := New_List;
10217 Loc : constant Source_Ptr := Sloc (Actual);
10218 Orig_Ftyp : constant Entity_Id := Etype (A_Gen_Obj);
10219 Subt_Decl : Node_Id := Empty;
10220 Subt_Mark : Node_Id := Empty;
10221
10222 function Copy_Access_Def return Node_Id;
10223 -- If formal is an anonymous access, copy access definition of formal
10224 -- for generated object declaration.
10225
10226 ---------------------
10227 -- Copy_Access_Def --
10228 ---------------------
10229
10230 function Copy_Access_Def return Node_Id is
10231 begin
10232 Def := New_Copy_Tree (Acc_Def);
10233
10234 -- In addition, if formal is an access to subprogram we need to
10235 -- generate new formals for the signature of the default, so that
10236 -- the tree is properly formatted for ASIS use.
10237
10238 if Present (Access_To_Subprogram_Definition (Acc_Def)) then
10239 declare
10240 Par_Spec : Node_Id;
10241 begin
10242 Par_Spec :=
10243 First (Parameter_Specifications
10244 (Access_To_Subprogram_Definition (Def)));
10245 while Present (Par_Spec) loop
10246 Set_Defining_Identifier (Par_Spec,
10247 Make_Defining_Identifier (Sloc (Acc_Def),
10248 Chars => Chars (Defining_Identifier (Par_Spec))));
10249 Next (Par_Spec);
10250 end loop;
10251 end;
10252 end if;
10253
10254 return Def;
10255 end Copy_Access_Def;
10256
10257 -- Start of processing for Instantiate_Object
10258
10259 begin
10260 -- Formal may be an anonymous access
10261
10262 if Present (Subtype_Mark (Formal)) then
10263 Subt_Mark := Subtype_Mark (Formal);
10264 else
10265 Check_Access_Definition (Formal);
10266 Acc_Def := Access_Definition (Formal);
10267 end if;
10268
10269 -- Sloc for error message on missing actual
10270
10271 Error_Msg_Sloc := Sloc (Scope (A_Gen_Obj));
10272
10273 if Get_Instance_Of (Gen_Obj) /= Gen_Obj then
10274 Error_Msg_N ("duplicate instantiation of generic parameter", Actual);
10275 end if;
10276
10277 Set_Parent (List, Parent (Actual));
10278
10279 -- OUT present
10280
10281 if Out_Present (Formal) then
10282
10283 -- An IN OUT generic actual must be a name. The instantiation is a
10284 -- renaming declaration. The actual is the name being renamed. We
10285 -- use the actual directly, rather than a copy, because it is not
10286 -- used further in the list of actuals, and because a copy or a use
10287 -- of relocate_node is incorrect if the instance is nested within a
10288 -- generic. In order to simplify ASIS searches, the Generic_Parent
10289 -- field links the declaration to the generic association.
10290
10291 if No (Actual) then
10292 Error_Msg_NE
10293 ("missing actual &",
10294 Instantiation_Node, Gen_Obj);
10295 Error_Msg_NE
10296 ("\in instantiation of & declared#",
10297 Instantiation_Node, Scope (A_Gen_Obj));
10298 Abandon_Instantiation (Instantiation_Node);
10299 end if;
10300
10301 if Present (Subt_Mark) then
10302 Decl_Node :=
10303 Make_Object_Renaming_Declaration (Loc,
10304 Defining_Identifier => New_Copy (Gen_Obj),
10305 Subtype_Mark => New_Copy_Tree (Subt_Mark),
10306 Name => Actual);
10307
10308 else pragma Assert (Present (Acc_Def));
10309 Decl_Node :=
10310 Make_Object_Renaming_Declaration (Loc,
10311 Defining_Identifier => New_Copy (Gen_Obj),
10312 Access_Definition => New_Copy_Tree (Acc_Def),
10313 Name => Actual);
10314 end if;
10315
10316 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
10317
10318 -- The analysis of the actual may produce Insert_Action nodes, so
10319 -- the declaration must have a context in which to attach them.
10320
10321 Append (Decl_Node, List);
10322 Analyze (Actual);
10323
10324 -- Return if the analysis of the actual reported some error
10325
10326 if Etype (Actual) = Any_Type then
10327 return List;
10328 end if;
10329
10330 -- This check is performed here because Analyze_Object_Renaming will
10331 -- not check it when Comes_From_Source is False. Note though that the
10332 -- check for the actual being the name of an object will be performed
10333 -- in Analyze_Object_Renaming.
10334
10335 if Is_Object_Reference (Actual)
10336 and then Is_Dependent_Component_Of_Mutable_Object (Actual)
10337 then
10338 Error_Msg_N
10339 ("illegal discriminant-dependent component for in out parameter",
10340 Actual);
10341 end if;
10342
10343 -- The actual has to be resolved in order to check that it is a
10344 -- variable (due to cases such as F (1), where F returns access to
10345 -- an array, and for overloaded prefixes).
10346
10347 Ftyp := Get_Instance_Of (Etype (A_Gen_Obj));
10348
10349 -- If the type of the formal is not itself a formal, and the current
10350 -- unit is a child unit, the formal type must be declared in a
10351 -- parent, and must be retrieved by visibility.
10352
10353 if Ftyp = Orig_Ftyp
10354 and then Is_Generic_Unit (Scope (Ftyp))
10355 and then Is_Child_Unit (Scope (A_Gen_Obj))
10356 then
10357 declare
10358 Temp : constant Node_Id :=
10359 New_Copy_Tree (Subtype_Mark (Analyzed_Formal));
10360 begin
10361 Set_Entity (Temp, Empty);
10362 Find_Type (Temp);
10363 Ftyp := Entity (Temp);
10364 end;
10365 end if;
10366
10367 if Is_Private_Type (Ftyp)
10368 and then not Is_Private_Type (Etype (Actual))
10369 and then (Base_Type (Full_View (Ftyp)) = Base_Type (Etype (Actual))
10370 or else Base_Type (Etype (Actual)) = Ftyp)
10371 then
10372 -- If the actual has the type of the full view of the formal, or
10373 -- else a non-private subtype of the formal, then the visibility
10374 -- of the formal type has changed. Add to the actuals a subtype
10375 -- declaration that will force the exchange of views in the body
10376 -- of the instance as well.
10377
10378 Subt_Decl :=
10379 Make_Subtype_Declaration (Loc,
10380 Defining_Identifier => Make_Temporary (Loc, 'P'),
10381 Subtype_Indication => New_Occurrence_Of (Ftyp, Loc));
10382
10383 Prepend (Subt_Decl, List);
10384
10385 Prepend_Elmt (Full_View (Ftyp), Exchanged_Views);
10386 Exchange_Declarations (Ftyp);
10387 end if;
10388
10389 Resolve (Actual, Ftyp);
10390
10391 if not Denotes_Variable (Actual) then
10392 Error_Msg_NE ("actual for& must be a variable", Actual, Gen_Obj);
10393
10394 elsif Base_Type (Ftyp) /= Base_Type (Etype (Actual)) then
10395
10396 -- Ada 2005 (AI-423): For a generic formal object of mode in out,
10397 -- the type of the actual shall resolve to a specific anonymous
10398 -- access type.
10399
10400 if Ada_Version < Ada_2005
10401 or else Ekind (Base_Type (Ftyp)) /=
10402 E_Anonymous_Access_Type
10403 or else Ekind (Base_Type (Etype (Actual))) /=
10404 E_Anonymous_Access_Type
10405 then
10406 Error_Msg_NE
10407 ("type of actual does not match type of&", Actual, Gen_Obj);
10408 end if;
10409 end if;
10410
10411 Note_Possible_Modification (Actual, Sure => True);
10412
10413 -- Check for instantiation of atomic/volatile actual for
10414 -- non-atomic/volatile formal (RM C.6 (12)).
10415
10416 if Is_Atomic_Object (Actual) and then not Is_Atomic (Orig_Ftyp) then
10417 Error_Msg_N
10418 ("cannot instantiate non-atomic formal object "
10419 & "with atomic actual", Actual);
10420
10421 elsif Is_Volatile_Object (Actual) and then not Is_Volatile (Orig_Ftyp)
10422 then
10423 Error_Msg_N
10424 ("cannot instantiate non-volatile formal object "
10425 & "with volatile actual", Actual);
10426 end if;
10427
10428 -- Formal in-parameter
10429
10430 else
10431 -- The instantiation of a generic formal in-parameter is constant
10432 -- declaration. The actual is the expression for that declaration.
10433 -- Its type is a full copy of the type of the formal. This may be
10434 -- an access to subprogram, for which we need to generate entities
10435 -- for the formals in the new signature.
10436
10437 if Present (Actual) then
10438 if Present (Subt_Mark) then
10439 Def := New_Copy_Tree (Subt_Mark);
10440 else pragma Assert (Present (Acc_Def));
10441 Def := Copy_Access_Def;
10442 end if;
10443
10444 Decl_Node :=
10445 Make_Object_Declaration (Loc,
10446 Defining_Identifier => New_Copy (Gen_Obj),
10447 Constant_Present => True,
10448 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
10449 Object_Definition => Def,
10450 Expression => Actual);
10451
10452 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
10453
10454 -- A generic formal object of a tagged type is defined to be
10455 -- aliased so the new constant must also be treated as aliased.
10456
10457 if Is_Tagged_Type (Etype (A_Gen_Obj)) then
10458 Set_Aliased_Present (Decl_Node);
10459 end if;
10460
10461 Append (Decl_Node, List);
10462
10463 -- No need to repeat (pre-)analysis of some expression nodes
10464 -- already handled in Preanalyze_Actuals.
10465
10466 if Nkind (Actual) /= N_Allocator then
10467 Analyze (Actual);
10468
10469 -- Return if the analysis of the actual reported some error
10470
10471 if Etype (Actual) = Any_Type then
10472 return List;
10473 end if;
10474 end if;
10475
10476 declare
10477 Formal_Type : constant Entity_Id := Etype (A_Gen_Obj);
10478 Typ : Entity_Id;
10479
10480 begin
10481 Typ := Get_Instance_Of (Formal_Type);
10482
10483 -- If the actual appears in the current or an enclosing scope,
10484 -- use its type directly. This is relevant if it has an actual
10485 -- subtype that is distinct from its nominal one. This cannot
10486 -- be done in general because the type of the actual may
10487 -- depend on other actuals, and only be fully determined when
10488 -- the enclosing instance is analyzed.
10489
10490 if Present (Etype (Actual))
10491 and then Is_Constr_Subt_For_U_Nominal (Etype (Actual))
10492 then
10493 Freeze_Before (Instantiation_Node, Etype (Actual));
10494 else
10495 Freeze_Before (Instantiation_Node, Typ);
10496 end if;
10497
10498 -- If the actual is an aggregate, perform name resolution on
10499 -- its components (the analysis of an aggregate does not do it)
10500 -- to capture local names that may be hidden if the generic is
10501 -- a child unit.
10502
10503 if Nkind (Actual) = N_Aggregate then
10504 Preanalyze_And_Resolve (Actual, Typ);
10505 end if;
10506
10507 if Is_Limited_Type (Typ)
10508 and then not OK_For_Limited_Init (Typ, Actual)
10509 then
10510 Error_Msg_N
10511 ("initialization not allowed for limited types", Actual);
10512 Explain_Limited_Type (Typ, Actual);
10513 end if;
10514 end;
10515
10516 elsif Present (Default_Expression (Formal)) then
10517
10518 -- Use default to construct declaration
10519
10520 if Present (Subt_Mark) then
10521 Def := New_Copy (Subt_Mark);
10522 else pragma Assert (Present (Acc_Def));
10523 Def := Copy_Access_Def;
10524 end if;
10525
10526 Decl_Node :=
10527 Make_Object_Declaration (Sloc (Formal),
10528 Defining_Identifier => New_Copy (Gen_Obj),
10529 Constant_Present => True,
10530 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
10531 Object_Definition => Def,
10532 Expression => New_Copy_Tree
10533 (Default_Expression (Formal)));
10534
10535 Append (Decl_Node, List);
10536 Set_Analyzed (Expression (Decl_Node), False);
10537
10538 else
10539 Error_Msg_NE ("missing actual&", Instantiation_Node, Gen_Obj);
10540 Error_Msg_NE ("\in instantiation of & declared#",
10541 Instantiation_Node, Scope (A_Gen_Obj));
10542
10543 if Is_Scalar_Type (Etype (A_Gen_Obj)) then
10544
10545 -- Create dummy constant declaration so that instance can be
10546 -- analyzed, to minimize cascaded visibility errors.
10547
10548 if Present (Subt_Mark) then
10549 Def := Subt_Mark;
10550 else pragma Assert (Present (Acc_Def));
10551 Def := Acc_Def;
10552 end if;
10553
10554 Decl_Node :=
10555 Make_Object_Declaration (Loc,
10556 Defining_Identifier => New_Copy (Gen_Obj),
10557 Constant_Present => True,
10558 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
10559 Object_Definition => New_Copy (Def),
10560 Expression =>
10561 Make_Attribute_Reference (Sloc (Gen_Obj),
10562 Attribute_Name => Name_First,
10563 Prefix => New_Copy (Def)));
10564
10565 Append (Decl_Node, List);
10566
10567 else
10568 Abandon_Instantiation (Instantiation_Node);
10569 end if;
10570 end if;
10571 end if;
10572
10573 if Nkind (Actual) in N_Has_Entity then
10574 Actual_Decl := Parent (Entity (Actual));
10575 end if;
10576
10577 -- Ada 2005 (AI-423): For a formal object declaration with a null
10578 -- exclusion or an access definition that has a null exclusion: If the
10579 -- actual matching the formal object declaration denotes a generic
10580 -- formal object of another generic unit G, and the instantiation
10581 -- containing the actual occurs within the body of G or within the body
10582 -- of a generic unit declared within the declarative region of G, then
10583 -- the declaration of the formal object of G must have a null exclusion.
10584 -- Otherwise, the subtype of the actual matching the formal object
10585 -- declaration shall exclude null.
10586
10587 if Ada_Version >= Ada_2005
10588 and then Present (Actual_Decl)
10589 and then Nkind_In (Actual_Decl, N_Formal_Object_Declaration,
10590 N_Object_Declaration)
10591 and then Nkind (Analyzed_Formal) = N_Formal_Object_Declaration
10592 and then not Has_Null_Exclusion (Actual_Decl)
10593 and then Has_Null_Exclusion (Analyzed_Formal)
10594 then
10595 Error_Msg_Sloc := Sloc (Analyzed_Formal);
10596 Error_Msg_N
10597 ("actual must exclude null to match generic formal#", Actual);
10598 end if;
10599
10600 -- An effectively volatile object cannot be used as an actual in
10601 -- a generic instance. The following check is only relevant when
10602 -- SPARK_Mode is on as it is not a standard Ada legality rule.
10603
10604 if SPARK_Mode = On
10605 and then Present (Actual)
10606 and then Is_Effectively_Volatile_Object (Actual)
10607 then
10608 Error_Msg_N
10609 ("volatile object cannot act as actual in generic instantiation "
10610 & "(SPARK RM 7.1.3(8))", Actual);
10611 end if;
10612
10613 return List;
10614 end Instantiate_Object;
10615
10616 ------------------------------
10617 -- Instantiate_Package_Body --
10618 ------------------------------
10619
10620 procedure Instantiate_Package_Body
10621 (Body_Info : Pending_Body_Info;
10622 Inlined_Body : Boolean := False;
10623 Body_Optional : Boolean := False)
10624 is
10625 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
10626 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
10627 Loc : constant Source_Ptr := Sloc (Inst_Node);
10628
10629 Gen_Id : constant Node_Id := Name (Inst_Node);
10630 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
10631 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
10632 Act_Spec : constant Node_Id := Specification (Act_Decl);
10633 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Spec);
10634
10635 Act_Body_Name : Node_Id;
10636 Gen_Body : Node_Id;
10637 Gen_Body_Id : Node_Id;
10638 Act_Body : Node_Id;
10639 Act_Body_Id : Entity_Id;
10640
10641 Parent_Installed : Boolean := False;
10642 Save_Style_Check : constant Boolean := Style_Check;
10643
10644 Par_Ent : Entity_Id := Empty;
10645 Par_Vis : Boolean := False;
10646
10647 Vis_Prims_List : Elist_Id := No_Elist;
10648 -- List of primitives made temporarily visible in the instantiation
10649 -- to match the visibility of the formal type
10650
10651 procedure Check_Initialized_Types;
10652 -- In a generic package body, an entity of a generic private type may
10653 -- appear uninitialized. This is suspicious, unless the actual is a
10654 -- fully initialized type.
10655
10656 -----------------------------
10657 -- Check_Initialized_Types --
10658 -----------------------------
10659
10660 procedure Check_Initialized_Types is
10661 Decl : Node_Id;
10662 Formal : Entity_Id;
10663 Actual : Entity_Id;
10664 Uninit_Var : Entity_Id;
10665
10666 begin
10667 Decl := First (Generic_Formal_Declarations (Gen_Decl));
10668 while Present (Decl) loop
10669 Uninit_Var := Empty;
10670
10671 if Nkind (Decl) = N_Private_Extension_Declaration then
10672 Uninit_Var := Uninitialized_Variable (Decl);
10673
10674 elsif Nkind (Decl) = N_Formal_Type_Declaration
10675 and then Nkind (Formal_Type_Definition (Decl)) =
10676 N_Formal_Private_Type_Definition
10677 then
10678 Uninit_Var :=
10679 Uninitialized_Variable (Formal_Type_Definition (Decl));
10680 end if;
10681
10682 if Present (Uninit_Var) then
10683 Formal := Defining_Identifier (Decl);
10684 Actual := First_Entity (Act_Decl_Id);
10685
10686 -- For each formal there is a subtype declaration that renames
10687 -- the actual and has the same name as the formal. Locate the
10688 -- formal for warning message about uninitialized variables
10689 -- in the generic, for which the actual type should be a fully
10690 -- initialized type.
10691
10692 while Present (Actual) loop
10693 exit when Ekind (Actual) = E_Package
10694 and then Present (Renamed_Object (Actual));
10695
10696 if Chars (Actual) = Chars (Formal)
10697 and then not Is_Scalar_Type (Actual)
10698 and then not Is_Fully_Initialized_Type (Actual)
10699 and then Warn_On_No_Value_Assigned
10700 then
10701 Error_Msg_Node_2 := Formal;
10702 Error_Msg_NE
10703 ("generic unit has uninitialized variable& of "
10704 & "formal private type &?v?", Actual, Uninit_Var);
10705 Error_Msg_NE
10706 ("actual type for& should be fully initialized type?v?",
10707 Actual, Formal);
10708 exit;
10709 end if;
10710
10711 Next_Entity (Actual);
10712 end loop;
10713 end if;
10714
10715 Next (Decl);
10716 end loop;
10717 end Check_Initialized_Types;
10718
10719 -- Start of processing for Instantiate_Package_Body
10720
10721 begin
10722 Gen_Body_Id := Corresponding_Body (Gen_Decl);
10723
10724 -- The instance body may already have been processed, as the parent of
10725 -- another instance that is inlined (Load_Parent_Of_Generic).
10726
10727 if Present (Corresponding_Body (Instance_Spec (Inst_Node))) then
10728 return;
10729 end if;
10730
10731 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
10732
10733 -- Re-establish the state of information on which checks are suppressed.
10734 -- This information was set in Body_Info at the point of instantiation,
10735 -- and now we restore it so that the instance is compiled using the
10736 -- check status at the instantiation (RM 11.5(7.2/2), AI95-00224-01).
10737
10738 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
10739 Scope_Suppress := Body_Info.Scope_Suppress;
10740 Opt.Ada_Version := Body_Info.Version;
10741 Opt.Ada_Version_Pragma := Body_Info.Version_Pragma;
10742 Restore_Warnings (Body_Info.Warnings);
10743 Opt.SPARK_Mode := Body_Info.SPARK_Mode;
10744 Opt.SPARK_Mode_Pragma := Body_Info.SPARK_Mode_Pragma;
10745
10746 if No (Gen_Body_Id) then
10747
10748 -- Do not look for parent of generic body if none is required.
10749 -- This may happen when the routine is called as part of the
10750 -- Pending_Instantiations processing, when nested instances
10751 -- may precede the one generated from the main unit.
10752
10753 if not Unit_Requires_Body (Defining_Entity (Gen_Decl))
10754 and then Body_Optional
10755 then
10756 return;
10757 else
10758 Load_Parent_Of_Generic
10759 (Inst_Node, Specification (Gen_Decl), Body_Optional);
10760 Gen_Body_Id := Corresponding_Body (Gen_Decl);
10761 end if;
10762 end if;
10763
10764 -- Establish global variable for sloc adjustment and for error recovery
10765 -- In the case of an instance body for an instantiation with actuals
10766 -- from a limited view, the instance body is placed at the beginning
10767 -- of the enclosing package body: use the body entity as the source
10768 -- location for nodes of the instance body.
10769
10770 if not Is_Empty_Elmt_List (Incomplete_Actuals (Act_Decl_Id)) then
10771 declare
10772 Scop : constant Entity_Id := Scope (Act_Decl_Id);
10773 Body_Id : constant Node_Id :=
10774 Corresponding_Body (Unit_Declaration_Node (Scop));
10775
10776 begin
10777 Instantiation_Node := Body_Id;
10778 end;
10779 else
10780 Instantiation_Node := Inst_Node;
10781 end if;
10782
10783 if Present (Gen_Body_Id) then
10784 Save_Env (Gen_Unit, Act_Decl_Id);
10785 Style_Check := False;
10786 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
10787
10788 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
10789
10790 Create_Instantiation_Source
10791 (Inst_Node, Gen_Body_Id, False, S_Adjustment);
10792
10793 Act_Body :=
10794 Copy_Generic_Node
10795 (Original_Node (Gen_Body), Empty, Instantiating => True);
10796
10797 -- Build new name (possibly qualified) for body declaration
10798
10799 Act_Body_Id := New_Copy (Act_Decl_Id);
10800
10801 -- Some attributes of spec entity are not inherited by body entity
10802
10803 Set_Handler_Records (Act_Body_Id, No_List);
10804
10805 if Nkind (Defining_Unit_Name (Act_Spec)) =
10806 N_Defining_Program_Unit_Name
10807 then
10808 Act_Body_Name :=
10809 Make_Defining_Program_Unit_Name (Loc,
10810 Name => New_Copy_Tree (Name (Defining_Unit_Name (Act_Spec))),
10811 Defining_Identifier => Act_Body_Id);
10812 else
10813 Act_Body_Name := Act_Body_Id;
10814 end if;
10815
10816 Set_Defining_Unit_Name (Act_Body, Act_Body_Name);
10817
10818 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
10819 Check_Generic_Actuals (Act_Decl_Id, False);
10820 Check_Initialized_Types;
10821
10822 -- Install primitives hidden at the point of the instantiation but
10823 -- visible when processing the generic formals
10824
10825 declare
10826 E : Entity_Id;
10827
10828 begin
10829 E := First_Entity (Act_Decl_Id);
10830 while Present (E) loop
10831 if Is_Type (E)
10832 and then Is_Generic_Actual_Type (E)
10833 and then Is_Tagged_Type (E)
10834 then
10835 Install_Hidden_Primitives
10836 (Prims_List => Vis_Prims_List,
10837 Gen_T => Generic_Parent_Type (Parent (E)),
10838 Act_T => E);
10839 end if;
10840
10841 Next_Entity (E);
10842 end loop;
10843 end;
10844
10845 -- If it is a child unit, make the parent instance (which is an
10846 -- instance of the parent of the generic) visible. The parent
10847 -- instance is the prefix of the name of the generic unit.
10848
10849 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
10850 and then Nkind (Gen_Id) = N_Expanded_Name
10851 then
10852 Par_Ent := Entity (Prefix (Gen_Id));
10853 Par_Vis := Is_Immediately_Visible (Par_Ent);
10854 Install_Parent (Par_Ent, In_Body => True);
10855 Parent_Installed := True;
10856
10857 elsif Is_Child_Unit (Gen_Unit) then
10858 Par_Ent := Scope (Gen_Unit);
10859 Par_Vis := Is_Immediately_Visible (Par_Ent);
10860 Install_Parent (Par_Ent, In_Body => True);
10861 Parent_Installed := True;
10862 end if;
10863
10864 -- If the instantiation is a library unit, and this is the main unit,
10865 -- then build the resulting compilation unit nodes for the instance.
10866 -- If this is a compilation unit but it is not the main unit, then it
10867 -- is the body of a unit in the context, that is being compiled
10868 -- because it is encloses some inlined unit or another generic unit
10869 -- being instantiated. In that case, this body is not part of the
10870 -- current compilation, and is not attached to the tree, but its
10871 -- parent must be set for analysis.
10872
10873 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
10874
10875 -- Replace instance node with body of instance, and create new
10876 -- node for corresponding instance declaration.
10877
10878 Build_Instance_Compilation_Unit_Nodes
10879 (Inst_Node, Act_Body, Act_Decl);
10880 Analyze (Inst_Node);
10881
10882 if Parent (Inst_Node) = Cunit (Main_Unit) then
10883
10884 -- If the instance is a child unit itself, then set the scope
10885 -- of the expanded body to be the parent of the instantiation
10886 -- (ensuring that the fully qualified name will be generated
10887 -- for the elaboration subprogram).
10888
10889 if Nkind (Defining_Unit_Name (Act_Spec)) =
10890 N_Defining_Program_Unit_Name
10891 then
10892 Set_Scope (Defining_Entity (Inst_Node), Scope (Act_Decl_Id));
10893 end if;
10894 end if;
10895
10896 -- Case where instantiation is not a library unit
10897
10898 else
10899 -- If this is an early instantiation, i.e. appears textually
10900 -- before the corresponding body and must be elaborated first,
10901 -- indicate that the body instance is to be delayed.
10902
10903 Install_Body (Act_Body, Inst_Node, Gen_Body, Gen_Decl);
10904
10905 -- Now analyze the body. We turn off all checks if this is an
10906 -- internal unit, since there is no reason to have checks on for
10907 -- any predefined run-time library code. All such code is designed
10908 -- to be compiled with checks off.
10909
10910 -- Note that we do NOT apply this criterion to children of GNAT
10911 -- The latter units must suppress checks explicitly if needed.
10912
10913 if Is_Predefined_File_Name
10914 (Unit_File_Name (Get_Source_Unit (Gen_Decl)))
10915 then
10916 Analyze (Act_Body, Suppress => All_Checks);
10917 else
10918 Analyze (Act_Body);
10919 end if;
10920 end if;
10921
10922 Inherit_Context (Gen_Body, Inst_Node);
10923
10924 -- Remove the parent instances if they have been placed on the scope
10925 -- stack to compile the body.
10926
10927 if Parent_Installed then
10928 Remove_Parent (In_Body => True);
10929
10930 -- Restore the previous visibility of the parent
10931
10932 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
10933 end if;
10934
10935 Restore_Hidden_Primitives (Vis_Prims_List);
10936 Restore_Private_Views (Act_Decl_Id);
10937
10938 -- Remove the current unit from visibility if this is an instance
10939 -- that is not elaborated on the fly for inlining purposes.
10940
10941 if not Inlined_Body then
10942 Set_Is_Immediately_Visible (Act_Decl_Id, False);
10943 end if;
10944
10945 Restore_Env;
10946 Style_Check := Save_Style_Check;
10947
10948 -- If we have no body, and the unit requires a body, then complain. This
10949 -- complaint is suppressed if we have detected other errors (since a
10950 -- common reason for missing the body is that it had errors).
10951 -- In CodePeer mode, a warning has been emitted already, no need for
10952 -- further messages.
10953
10954 elsif Unit_Requires_Body (Gen_Unit)
10955 and then not Body_Optional
10956 then
10957 if CodePeer_Mode then
10958 null;
10959
10960 elsif Serious_Errors_Detected = 0 then
10961 Error_Msg_NE
10962 ("cannot find body of generic package &", Inst_Node, Gen_Unit);
10963
10964 -- Don't attempt to perform any cleanup actions if some other error
10965 -- was already detected, since this can cause blowups.
10966
10967 else
10968 return;
10969 end if;
10970
10971 -- Case of package that does not need a body
10972
10973 else
10974 -- If the instantiation of the declaration is a library unit, rewrite
10975 -- the original package instantiation as a package declaration in the
10976 -- compilation unit node.
10977
10978 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
10979 Set_Parent_Spec (Act_Decl, Parent_Spec (Inst_Node));
10980 Rewrite (Inst_Node, Act_Decl);
10981
10982 -- Generate elaboration entity, in case spec has elaboration code.
10983 -- This cannot be done when the instance is analyzed, because it
10984 -- is not known yet whether the body exists.
10985
10986 Set_Elaboration_Entity_Required (Act_Decl_Id, False);
10987 Build_Elaboration_Entity (Parent (Inst_Node), Act_Decl_Id);
10988
10989 -- If the instantiation is not a library unit, then append the
10990 -- declaration to the list of implicitly generated entities, unless
10991 -- it is already a list member which means that it was already
10992 -- processed
10993
10994 elsif not Is_List_Member (Act_Decl) then
10995 Mark_Rewrite_Insertion (Act_Decl);
10996 Insert_Before (Inst_Node, Act_Decl);
10997 end if;
10998 end if;
10999
11000 Expander_Mode_Restore;
11001 end Instantiate_Package_Body;
11002
11003 ---------------------------------
11004 -- Instantiate_Subprogram_Body --
11005 ---------------------------------
11006
11007 procedure Instantiate_Subprogram_Body
11008 (Body_Info : Pending_Body_Info;
11009 Body_Optional : Boolean := False)
11010 is
11011 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
11012 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
11013 Loc : constant Source_Ptr := Sloc (Inst_Node);
11014 Gen_Id : constant Node_Id := Name (Inst_Node);
11015 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
11016 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
11017 Anon_Id : constant Entity_Id :=
11018 Defining_Unit_Name (Specification (Act_Decl));
11019 Pack_Id : constant Entity_Id :=
11020 Defining_Unit_Name (Parent (Act_Decl));
11021
11022 Saved_Style_Check : constant Boolean := Style_Check;
11023 Saved_Warnings : constant Warning_Record := Save_Warnings;
11024
11025 Act_Body : Node_Id;
11026 Gen_Body : Node_Id;
11027 Gen_Body_Id : Node_Id;
11028 Pack_Body : Node_Id;
11029 Par_Ent : Entity_Id := Empty;
11030 Par_Vis : Boolean := False;
11031 Ret_Expr : Node_Id;
11032
11033 Parent_Installed : Boolean := False;
11034
11035 begin
11036 Gen_Body_Id := Corresponding_Body (Gen_Decl);
11037
11038 -- Subprogram body may have been created already because of an inline
11039 -- pragma, or because of multiple elaborations of the enclosing package
11040 -- when several instances of the subprogram appear in the main unit.
11041
11042 if Present (Corresponding_Body (Act_Decl)) then
11043 return;
11044 end if;
11045
11046 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
11047
11048 -- Re-establish the state of information on which checks are suppressed.
11049 -- This information was set in Body_Info at the point of instantiation,
11050 -- and now we restore it so that the instance is compiled using the
11051 -- check status at the instantiation (RM 11.5(7.2/2), AI95-00224-01).
11052
11053 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
11054 Scope_Suppress := Body_Info.Scope_Suppress;
11055 Opt.Ada_Version := Body_Info.Version;
11056 Opt.Ada_Version_Pragma := Body_Info.Version_Pragma;
11057 Restore_Warnings (Body_Info.Warnings);
11058 Opt.SPARK_Mode := Body_Info.SPARK_Mode;
11059 Opt.SPARK_Mode_Pragma := Body_Info.SPARK_Mode_Pragma;
11060
11061 if No (Gen_Body_Id) then
11062
11063 -- For imported generic subprogram, no body to compile, complete
11064 -- the spec entity appropriately.
11065
11066 if Is_Imported (Gen_Unit) then
11067 Set_Is_Imported (Anon_Id);
11068 Set_First_Rep_Item (Anon_Id, First_Rep_Item (Gen_Unit));
11069 Set_Interface_Name (Anon_Id, Interface_Name (Gen_Unit));
11070 Set_Convention (Anon_Id, Convention (Gen_Unit));
11071 Set_Has_Completion (Anon_Id);
11072 return;
11073
11074 -- For other cases, compile the body
11075
11076 else
11077 Load_Parent_Of_Generic
11078 (Inst_Node, Specification (Gen_Decl), Body_Optional);
11079 Gen_Body_Id := Corresponding_Body (Gen_Decl);
11080 end if;
11081 end if;
11082
11083 Instantiation_Node := Inst_Node;
11084
11085 if Present (Gen_Body_Id) then
11086 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
11087
11088 if Nkind (Gen_Body) = N_Subprogram_Body_Stub then
11089
11090 -- Either body is not present, or context is non-expanding, as
11091 -- when compiling a subunit. Mark the instance as completed, and
11092 -- diagnose a missing body when needed.
11093
11094 if Expander_Active
11095 and then Operating_Mode = Generate_Code
11096 then
11097 Error_Msg_N
11098 ("missing proper body for instantiation", Gen_Body);
11099 end if;
11100
11101 Set_Has_Completion (Anon_Id);
11102 return;
11103 end if;
11104
11105 Save_Env (Gen_Unit, Anon_Id);
11106 Style_Check := False;
11107 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
11108 Create_Instantiation_Source
11109 (Inst_Node,
11110 Gen_Body_Id,
11111 False,
11112 S_Adjustment);
11113
11114 Act_Body :=
11115 Copy_Generic_Node
11116 (Original_Node (Gen_Body), Empty, Instantiating => True);
11117
11118 -- Create proper defining name for the body, to correspond to
11119 -- the one in the spec.
11120
11121 Set_Defining_Unit_Name (Specification (Act_Body),
11122 Make_Defining_Identifier
11123 (Sloc (Defining_Entity (Inst_Node)), Chars (Anon_Id)));
11124 Set_Corresponding_Spec (Act_Body, Anon_Id);
11125 Set_Has_Completion (Anon_Id);
11126 Check_Generic_Actuals (Pack_Id, False);
11127
11128 -- Generate a reference to link the visible subprogram instance to
11129 -- the generic body, which for navigation purposes is the only
11130 -- available source for the instance.
11131
11132 Generate_Reference
11133 (Related_Instance (Pack_Id),
11134 Gen_Body_Id, 'b', Set_Ref => False, Force => True);
11135
11136 -- If it is a child unit, make the parent instance (which is an
11137 -- instance of the parent of the generic) visible. The parent
11138 -- instance is the prefix of the name of the generic unit.
11139
11140 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
11141 and then Nkind (Gen_Id) = N_Expanded_Name
11142 then
11143 Par_Ent := Entity (Prefix (Gen_Id));
11144 Par_Vis := Is_Immediately_Visible (Par_Ent);
11145 Install_Parent (Par_Ent, In_Body => True);
11146 Parent_Installed := True;
11147
11148 elsif Is_Child_Unit (Gen_Unit) then
11149 Par_Ent := Scope (Gen_Unit);
11150 Par_Vis := Is_Immediately_Visible (Par_Ent);
11151 Install_Parent (Par_Ent, In_Body => True);
11152 Parent_Installed := True;
11153 end if;
11154
11155 -- Subprogram body is placed in the body of wrapper package,
11156 -- whose spec contains the subprogram declaration as well as
11157 -- the renaming declarations for the generic parameters.
11158
11159 Pack_Body :=
11160 Make_Package_Body (Loc,
11161 Defining_Unit_Name => New_Copy (Pack_Id),
11162 Declarations => New_List (Act_Body));
11163
11164 Set_Corresponding_Spec (Pack_Body, Pack_Id);
11165
11166 -- If the instantiation is a library unit, then build resulting
11167 -- compilation unit nodes for the instance. The declaration of
11168 -- the enclosing package is the grandparent of the subprogram
11169 -- declaration. First replace the instantiation node as the unit
11170 -- of the corresponding compilation.
11171
11172 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
11173 if Parent (Inst_Node) = Cunit (Main_Unit) then
11174 Set_Unit (Parent (Inst_Node), Inst_Node);
11175 Build_Instance_Compilation_Unit_Nodes
11176 (Inst_Node, Pack_Body, Parent (Parent (Act_Decl)));
11177 Analyze (Inst_Node);
11178 else
11179 Set_Parent (Pack_Body, Parent (Inst_Node));
11180 Analyze (Pack_Body);
11181 end if;
11182
11183 else
11184 Insert_Before (Inst_Node, Pack_Body);
11185 Mark_Rewrite_Insertion (Pack_Body);
11186 Analyze (Pack_Body);
11187
11188 if Expander_Active then
11189 Freeze_Subprogram_Body (Inst_Node, Gen_Body, Pack_Id);
11190 end if;
11191 end if;
11192
11193 Inherit_Context (Gen_Body, Inst_Node);
11194
11195 Restore_Private_Views (Pack_Id, False);
11196
11197 if Parent_Installed then
11198 Remove_Parent (In_Body => True);
11199
11200 -- Restore the previous visibility of the parent
11201
11202 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
11203 end if;
11204
11205 Restore_Env;
11206 Style_Check := Saved_Style_Check;
11207 Restore_Warnings (Saved_Warnings);
11208
11209 -- Body not found. Error was emitted already. If there were no previous
11210 -- errors, this may be an instance whose scope is a premature instance.
11211 -- In that case we must insure that the (legal) program does raise
11212 -- program error if executed. We generate a subprogram body for this
11213 -- purpose. See DEC ac30vso.
11214
11215 -- Should not reference proprietary DEC tests in comments ???
11216
11217 elsif Serious_Errors_Detected = 0
11218 and then Nkind (Parent (Inst_Node)) /= N_Compilation_Unit
11219 then
11220 if Body_Optional then
11221 return;
11222
11223 elsif Ekind (Anon_Id) = E_Procedure then
11224 Act_Body :=
11225 Make_Subprogram_Body (Loc,
11226 Specification =>
11227 Make_Procedure_Specification (Loc,
11228 Defining_Unit_Name =>
11229 Make_Defining_Identifier (Loc, Chars (Anon_Id)),
11230 Parameter_Specifications =>
11231 New_Copy_List
11232 (Parameter_Specifications (Parent (Anon_Id)))),
11233
11234 Declarations => Empty_List,
11235 Handled_Statement_Sequence =>
11236 Make_Handled_Sequence_Of_Statements (Loc,
11237 Statements =>
11238 New_List (
11239 Make_Raise_Program_Error (Loc,
11240 Reason =>
11241 PE_Access_Before_Elaboration))));
11242
11243 else
11244 Ret_Expr :=
11245 Make_Raise_Program_Error (Loc,
11246 Reason => PE_Access_Before_Elaboration);
11247
11248 Set_Etype (Ret_Expr, (Etype (Anon_Id)));
11249 Set_Analyzed (Ret_Expr);
11250
11251 Act_Body :=
11252 Make_Subprogram_Body (Loc,
11253 Specification =>
11254 Make_Function_Specification (Loc,
11255 Defining_Unit_Name =>
11256 Make_Defining_Identifier (Loc, Chars (Anon_Id)),
11257 Parameter_Specifications =>
11258 New_Copy_List
11259 (Parameter_Specifications (Parent (Anon_Id))),
11260 Result_Definition =>
11261 New_Occurrence_Of (Etype (Anon_Id), Loc)),
11262
11263 Declarations => Empty_List,
11264 Handled_Statement_Sequence =>
11265 Make_Handled_Sequence_Of_Statements (Loc,
11266 Statements =>
11267 New_List
11268 (Make_Simple_Return_Statement (Loc, Ret_Expr))));
11269 end if;
11270
11271 Pack_Body := Make_Package_Body (Loc,
11272 Defining_Unit_Name => New_Copy (Pack_Id),
11273 Declarations => New_List (Act_Body));
11274
11275 Insert_After (Inst_Node, Pack_Body);
11276 Set_Corresponding_Spec (Pack_Body, Pack_Id);
11277 Analyze (Pack_Body);
11278 end if;
11279
11280 Expander_Mode_Restore;
11281 end Instantiate_Subprogram_Body;
11282
11283 ----------------------
11284 -- Instantiate_Type --
11285 ----------------------
11286
11287 function Instantiate_Type
11288 (Formal : Node_Id;
11289 Actual : Node_Id;
11290 Analyzed_Formal : Node_Id;
11291 Actual_Decls : List_Id) return List_Id
11292 is
11293 Gen_T : constant Entity_Id := Defining_Identifier (Formal);
11294 A_Gen_T : constant Entity_Id :=
11295 Defining_Identifier (Analyzed_Formal);
11296 Ancestor : Entity_Id := Empty;
11297 Def : constant Node_Id := Formal_Type_Definition (Formal);
11298 Act_T : Entity_Id;
11299 Decl_Node : Node_Id;
11300 Decl_Nodes : List_Id;
11301 Loc : Source_Ptr;
11302 Subt : Entity_Id;
11303
11304 procedure Diagnose_Predicated_Actual;
11305 -- There are a number of constructs in which a discrete type with
11306 -- predicates is illegal, e.g. as an index in an array type declaration.
11307 -- If a generic type is used is such a construct in a generic package
11308 -- declaration, it carries the flag No_Predicate_On_Actual. it is part
11309 -- of the generic contract that the actual cannot have predicates.
11310
11311 procedure Validate_Array_Type_Instance;
11312 procedure Validate_Access_Subprogram_Instance;
11313 procedure Validate_Access_Type_Instance;
11314 procedure Validate_Derived_Type_Instance;
11315 procedure Validate_Derived_Interface_Type_Instance;
11316 procedure Validate_Discriminated_Formal_Type;
11317 procedure Validate_Interface_Type_Instance;
11318 procedure Validate_Private_Type_Instance;
11319 procedure Validate_Incomplete_Type_Instance;
11320 -- These procedures perform validation tests for the named case.
11321 -- Validate_Discriminated_Formal_Type is shared by formal private
11322 -- types and Ada 2012 formal incomplete types.
11323
11324 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean;
11325 -- Check that base types are the same and that the subtypes match
11326 -- statically. Used in several of the above.
11327
11328 ---------------------------------
11329 -- Diagnose_Predicated_Actual --
11330 ---------------------------------
11331
11332 procedure Diagnose_Predicated_Actual is
11333 begin
11334 if No_Predicate_On_Actual (A_Gen_T)
11335 and then Has_Predicates (Act_T)
11336 then
11337 Error_Msg_NE
11338 ("actual for& cannot be a type with predicate",
11339 Instantiation_Node, A_Gen_T);
11340
11341 elsif No_Dynamic_Predicate_On_Actual (A_Gen_T)
11342 and then Has_Predicates (Act_T)
11343 and then not Has_Static_Predicate_Aspect (Act_T)
11344 then
11345 Error_Msg_NE
11346 ("actual for& cannot be a type with a dynamic predicate",
11347 Instantiation_Node, A_Gen_T);
11348 end if;
11349 end Diagnose_Predicated_Actual;
11350
11351 --------------------
11352 -- Subtypes_Match --
11353 --------------------
11354
11355 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean is
11356 T : constant Entity_Id := Get_Instance_Of (Gen_T);
11357
11358 begin
11359 -- Some detailed comments would be useful here ???
11360
11361 return ((Base_Type (T) = Act_T
11362 or else Base_Type (T) = Base_Type (Act_T))
11363 and then Subtypes_Statically_Match (T, Act_T))
11364
11365 or else (Is_Class_Wide_Type (Gen_T)
11366 and then Is_Class_Wide_Type (Act_T)
11367 and then Subtypes_Match
11368 (Get_Instance_Of (Root_Type (Gen_T)),
11369 Root_Type (Act_T)))
11370
11371 or else
11372 (Ekind_In (Gen_T, E_Anonymous_Access_Subprogram_Type,
11373 E_Anonymous_Access_Type)
11374 and then Ekind (Act_T) = Ekind (Gen_T)
11375 and then Subtypes_Statically_Match
11376 (Designated_Type (Gen_T), Designated_Type (Act_T)));
11377 end Subtypes_Match;
11378
11379 -----------------------------------------
11380 -- Validate_Access_Subprogram_Instance --
11381 -----------------------------------------
11382
11383 procedure Validate_Access_Subprogram_Instance is
11384 begin
11385 if not Is_Access_Type (Act_T)
11386 or else Ekind (Designated_Type (Act_T)) /= E_Subprogram_Type
11387 then
11388 Error_Msg_NE
11389 ("expect access type in instantiation of &", Actual, Gen_T);
11390 Abandon_Instantiation (Actual);
11391 end if;
11392
11393 -- According to AI05-288, actuals for access_to_subprograms must be
11394 -- subtype conformant with the generic formal. Previous to AI05-288
11395 -- only mode conformance was required.
11396
11397 -- This is a binding interpretation that applies to previous versions
11398 -- of the language, no need to maintain previous weaker checks.
11399
11400 Check_Subtype_Conformant
11401 (Designated_Type (Act_T),
11402 Designated_Type (A_Gen_T),
11403 Actual,
11404 Get_Inst => True);
11405
11406 if Ekind (Base_Type (Act_T)) = E_Access_Protected_Subprogram_Type then
11407 if Ekind (A_Gen_T) = E_Access_Subprogram_Type then
11408 Error_Msg_NE
11409 ("protected access type not allowed for formal &",
11410 Actual, Gen_T);
11411 end if;
11412
11413 elsif Ekind (A_Gen_T) = E_Access_Protected_Subprogram_Type then
11414 Error_Msg_NE
11415 ("expect protected access type for formal &",
11416 Actual, Gen_T);
11417 end if;
11418
11419 -- If the formal has a specified convention (which in most cases
11420 -- will be StdCall) verify that the actual has the same convention.
11421
11422 if Has_Convention_Pragma (A_Gen_T)
11423 and then Convention (A_Gen_T) /= Convention (Act_T)
11424 then
11425 Error_Msg_Name_1 := Get_Convention_Name (Convention (A_Gen_T));
11426 Error_Msg_NE
11427 ("actual for formal & must have convention %", Actual, Gen_T);
11428 end if;
11429 end Validate_Access_Subprogram_Instance;
11430
11431 -----------------------------------
11432 -- Validate_Access_Type_Instance --
11433 -----------------------------------
11434
11435 procedure Validate_Access_Type_Instance is
11436 Desig_Type : constant Entity_Id :=
11437 Find_Actual_Type (Designated_Type (A_Gen_T), A_Gen_T);
11438 Desig_Act : Entity_Id;
11439
11440 begin
11441 if not Is_Access_Type (Act_T) then
11442 Error_Msg_NE
11443 ("expect access type in instantiation of &", Actual, Gen_T);
11444 Abandon_Instantiation (Actual);
11445 end if;
11446
11447 if Is_Access_Constant (A_Gen_T) then
11448 if not Is_Access_Constant (Act_T) then
11449 Error_Msg_N
11450 ("actual type must be access-to-constant type", Actual);
11451 Abandon_Instantiation (Actual);
11452 end if;
11453 else
11454 if Is_Access_Constant (Act_T) then
11455 Error_Msg_N
11456 ("actual type must be access-to-variable type", Actual);
11457 Abandon_Instantiation (Actual);
11458
11459 elsif Ekind (A_Gen_T) = E_General_Access_Type
11460 and then Ekind (Base_Type (Act_T)) /= E_General_Access_Type
11461 then
11462 Error_Msg_N -- CODEFIX
11463 ("actual must be general access type!", Actual);
11464 Error_Msg_NE -- CODEFIX
11465 ("add ALL to }!", Actual, Act_T);
11466 Abandon_Instantiation (Actual);
11467 end if;
11468 end if;
11469
11470 -- The designated subtypes, that is to say the subtypes introduced
11471 -- by an access type declaration (and not by a subtype declaration)
11472 -- must match.
11473
11474 Desig_Act := Designated_Type (Base_Type (Act_T));
11475
11476 -- The designated type may have been introduced through a limited_
11477 -- with clause, in which case retrieve the non-limited view. This
11478 -- applies to incomplete types as well as to class-wide types.
11479
11480 if From_Limited_With (Desig_Act) then
11481 Desig_Act := Available_View (Desig_Act);
11482 end if;
11483
11484 if not Subtypes_Match (Desig_Type, Desig_Act) then
11485 Error_Msg_NE
11486 ("designated type of actual does not match that of formal &",
11487 Actual, Gen_T);
11488
11489 if not Predicates_Match (Desig_Type, Desig_Act) then
11490 Error_Msg_N ("\predicates do not match", Actual);
11491 end if;
11492
11493 Abandon_Instantiation (Actual);
11494
11495 elsif Is_Access_Type (Designated_Type (Act_T))
11496 and then Is_Constrained (Designated_Type (Designated_Type (Act_T)))
11497 /=
11498 Is_Constrained (Designated_Type (Desig_Type))
11499 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 end if;
11510
11511 -- Ada 2005: null-exclusion indicators of the two types must agree
11512
11513 if Can_Never_Be_Null (A_Gen_T) /= Can_Never_Be_Null (Act_T) then
11514 Error_Msg_NE
11515 ("non null exclusion of actual and formal & do not match",
11516 Actual, Gen_T);
11517 end if;
11518 end Validate_Access_Type_Instance;
11519
11520 ----------------------------------
11521 -- Validate_Array_Type_Instance --
11522 ----------------------------------
11523
11524 procedure Validate_Array_Type_Instance is
11525 I1 : Node_Id;
11526 I2 : Node_Id;
11527 T2 : Entity_Id;
11528
11529 function Formal_Dimensions return Int;
11530 -- Count number of dimensions in array type formal
11531
11532 -----------------------
11533 -- Formal_Dimensions --
11534 -----------------------
11535
11536 function Formal_Dimensions return Int is
11537 Num : Int := 0;
11538 Index : Node_Id;
11539
11540 begin
11541 if Nkind (Def) = N_Constrained_Array_Definition then
11542 Index := First (Discrete_Subtype_Definitions (Def));
11543 else
11544 Index := First (Subtype_Marks (Def));
11545 end if;
11546
11547 while Present (Index) loop
11548 Num := Num + 1;
11549 Next_Index (Index);
11550 end loop;
11551
11552 return Num;
11553 end Formal_Dimensions;
11554
11555 -- Start of processing for Validate_Array_Type_Instance
11556
11557 begin
11558 if not Is_Array_Type (Act_T) then
11559 Error_Msg_NE
11560 ("expect array type in instantiation of &", Actual, Gen_T);
11561 Abandon_Instantiation (Actual);
11562
11563 elsif Nkind (Def) = N_Constrained_Array_Definition then
11564 if not (Is_Constrained (Act_T)) then
11565 Error_Msg_NE
11566 ("expect constrained array in instantiation of &",
11567 Actual, Gen_T);
11568 Abandon_Instantiation (Actual);
11569 end if;
11570
11571 else
11572 if Is_Constrained (Act_T) then
11573 Error_Msg_NE
11574 ("expect unconstrained array in instantiation of &",
11575 Actual, Gen_T);
11576 Abandon_Instantiation (Actual);
11577 end if;
11578 end if;
11579
11580 if Formal_Dimensions /= Number_Dimensions (Act_T) then
11581 Error_Msg_NE
11582 ("dimensions of actual do not match formal &", Actual, Gen_T);
11583 Abandon_Instantiation (Actual);
11584 end if;
11585
11586 I1 := First_Index (A_Gen_T);
11587 I2 := First_Index (Act_T);
11588 for J in 1 .. Formal_Dimensions loop
11589
11590 -- If the indexes of the actual were given by a subtype_mark,
11591 -- the index was transformed into a range attribute. Retrieve
11592 -- the original type mark for checking.
11593
11594 if Is_Entity_Name (Original_Node (I2)) then
11595 T2 := Entity (Original_Node (I2));
11596 else
11597 T2 := Etype (I2);
11598 end if;
11599
11600 if not Subtypes_Match
11601 (Find_Actual_Type (Etype (I1), A_Gen_T), T2)
11602 then
11603 Error_Msg_NE
11604 ("index types of actual do not match those of formal &",
11605 Actual, Gen_T);
11606 Abandon_Instantiation (Actual);
11607 end if;
11608
11609 Next_Index (I1);
11610 Next_Index (I2);
11611 end loop;
11612
11613 -- Check matching subtypes. Note that there are complex visibility
11614 -- issues when the generic is a child unit and some aspect of the
11615 -- generic type is declared in a parent unit of the generic. We do
11616 -- the test to handle this special case only after a direct check
11617 -- for static matching has failed. The case where both the component
11618 -- type and the array type are separate formals, and the component
11619 -- type is a private view may also require special checking in
11620 -- Subtypes_Match.
11621
11622 if Subtypes_Match
11623 (Component_Type (A_Gen_T), Component_Type (Act_T))
11624 or else
11625 Subtypes_Match
11626 (Find_Actual_Type (Component_Type (A_Gen_T), A_Gen_T),
11627 Component_Type (Act_T))
11628 then
11629 null;
11630 else
11631 Error_Msg_NE
11632 ("component subtype of actual does not match that of formal &",
11633 Actual, Gen_T);
11634 Abandon_Instantiation (Actual);
11635 end if;
11636
11637 if Has_Aliased_Components (A_Gen_T)
11638 and then not Has_Aliased_Components (Act_T)
11639 then
11640 Error_Msg_NE
11641 ("actual must have aliased components to match formal type &",
11642 Actual, Gen_T);
11643 end if;
11644 end Validate_Array_Type_Instance;
11645
11646 -----------------------------------------------
11647 -- Validate_Derived_Interface_Type_Instance --
11648 -----------------------------------------------
11649
11650 procedure Validate_Derived_Interface_Type_Instance is
11651 Par : constant Entity_Id := Entity (Subtype_Indication (Def));
11652 Elmt : Elmt_Id;
11653
11654 begin
11655 -- First apply interface instance checks
11656
11657 Validate_Interface_Type_Instance;
11658
11659 -- Verify that immediate parent interface is an ancestor of
11660 -- the actual.
11661
11662 if Present (Par)
11663 and then not Interface_Present_In_Ancestor (Act_T, Par)
11664 then
11665 Error_Msg_NE
11666 ("interface actual must include progenitor&", Actual, Par);
11667 end if;
11668
11669 -- Now verify that the actual includes all other ancestors of
11670 -- the formal.
11671
11672 Elmt := First_Elmt (Interfaces (A_Gen_T));
11673 while Present (Elmt) loop
11674 if not Interface_Present_In_Ancestor
11675 (Act_T, Get_Instance_Of (Node (Elmt)))
11676 then
11677 Error_Msg_NE
11678 ("interface actual must include progenitor&",
11679 Actual, Node (Elmt));
11680 end if;
11681
11682 Next_Elmt (Elmt);
11683 end loop;
11684 end Validate_Derived_Interface_Type_Instance;
11685
11686 ------------------------------------
11687 -- Validate_Derived_Type_Instance --
11688 ------------------------------------
11689
11690 procedure Validate_Derived_Type_Instance is
11691 Actual_Discr : Entity_Id;
11692 Ancestor_Discr : Entity_Id;
11693
11694 begin
11695 -- If the parent type in the generic declaration is itself a previous
11696 -- formal type, then it is local to the generic and absent from the
11697 -- analyzed generic definition. In that case the ancestor is the
11698 -- instance of the formal (which must have been instantiated
11699 -- previously), unless the ancestor is itself a formal derived type.
11700 -- In this latter case (which is the subject of Corrigendum 8652/0038
11701 -- (AI-202) the ancestor of the formals is the ancestor of its
11702 -- parent. Otherwise, the analyzed generic carries the parent type.
11703 -- If the parent type is defined in a previous formal package, then
11704 -- the scope of that formal package is that of the generic type
11705 -- itself, and it has already been mapped into the corresponding type
11706 -- in the actual package.
11707
11708 -- Common case: parent type defined outside of the generic
11709
11710 if Is_Entity_Name (Subtype_Mark (Def))
11711 and then Present (Entity (Subtype_Mark (Def)))
11712 then
11713 Ancestor := Get_Instance_Of (Entity (Subtype_Mark (Def)));
11714
11715 -- Check whether parent is defined in a previous formal package
11716
11717 elsif
11718 Scope (Scope (Base_Type (Etype (A_Gen_T)))) = Scope (A_Gen_T)
11719 then
11720 Ancestor :=
11721 Get_Instance_Of (Base_Type (Etype (A_Gen_T)));
11722
11723 -- The type may be a local derivation, or a type extension of a
11724 -- previous formal, or of a formal of a parent package.
11725
11726 elsif Is_Derived_Type (Get_Instance_Of (A_Gen_T))
11727 or else
11728 Ekind (Get_Instance_Of (A_Gen_T)) = E_Record_Type_With_Private
11729 then
11730 -- Check whether the parent is another derived formal type in the
11731 -- same generic unit.
11732
11733 if Etype (A_Gen_T) /= A_Gen_T
11734 and then Is_Generic_Type (Etype (A_Gen_T))
11735 and then Scope (Etype (A_Gen_T)) = Scope (A_Gen_T)
11736 and then Etype (Etype (A_Gen_T)) /= Etype (A_Gen_T)
11737 then
11738 -- Locate ancestor of parent from the subtype declaration
11739 -- created for the actual.
11740
11741 declare
11742 Decl : Node_Id;
11743
11744 begin
11745 Decl := First (Actual_Decls);
11746 while Present (Decl) loop
11747 if Nkind (Decl) = N_Subtype_Declaration
11748 and then Chars (Defining_Identifier (Decl)) =
11749 Chars (Etype (A_Gen_T))
11750 then
11751 Ancestor := Generic_Parent_Type (Decl);
11752 exit;
11753 else
11754 Next (Decl);
11755 end if;
11756 end loop;
11757 end;
11758
11759 pragma Assert (Present (Ancestor));
11760
11761 -- The ancestor itself may be a previous formal that has been
11762 -- instantiated.
11763
11764 Ancestor := Get_Instance_Of (Ancestor);
11765
11766 else
11767 Ancestor :=
11768 Get_Instance_Of (Base_Type (Get_Instance_Of (A_Gen_T)));
11769 end if;
11770
11771 -- Check whether parent is a previous formal of the current generic
11772
11773 elsif Is_Derived_Type (A_Gen_T)
11774 and then Is_Generic_Type (Etype (A_Gen_T))
11775 and then Scope (A_Gen_T) = Scope (Etype (A_Gen_T))
11776 then
11777 Ancestor := Get_Instance_Of (First_Subtype (Etype (A_Gen_T)));
11778
11779 -- An unusual case: the actual is a type declared in a parent unit,
11780 -- but is not a formal type so there is no instance_of for it.
11781 -- Retrieve it by analyzing the record extension.
11782
11783 elsif Is_Child_Unit (Scope (A_Gen_T))
11784 and then In_Open_Scopes (Scope (Act_T))
11785 and then Is_Generic_Instance (Scope (Act_T))
11786 then
11787 Analyze (Subtype_Mark (Def));
11788 Ancestor := Entity (Subtype_Mark (Def));
11789
11790 else
11791 Ancestor := Get_Instance_Of (Etype (Base_Type (A_Gen_T)));
11792 end if;
11793
11794 -- If the formal derived type has pragma Preelaborable_Initialization
11795 -- then the actual type must have preelaborable initialization.
11796
11797 if Known_To_Have_Preelab_Init (A_Gen_T)
11798 and then not Has_Preelaborable_Initialization (Act_T)
11799 then
11800 Error_Msg_NE
11801 ("actual for & must have preelaborable initialization",
11802 Actual, Gen_T);
11803 end if;
11804
11805 -- Ada 2005 (AI-251)
11806
11807 if Ada_Version >= Ada_2005 and then Is_Interface (Ancestor) then
11808 if not Interface_Present_In_Ancestor (Act_T, Ancestor) then
11809 Error_Msg_NE
11810 ("(Ada 2005) expected type implementing & in instantiation",
11811 Actual, Ancestor);
11812 end if;
11813
11814 -- Finally verify that the (instance of) the ancestor is an ancestor
11815 -- of the actual.
11816
11817 elsif not Is_Ancestor (Base_Type (Ancestor), Act_T) then
11818 Error_Msg_NE
11819 ("expect type derived from & in instantiation",
11820 Actual, First_Subtype (Ancestor));
11821 Abandon_Instantiation (Actual);
11822 end if;
11823
11824 -- Ada 2005 (AI-443): Synchronized formal derived type checks. Note
11825 -- that the formal type declaration has been rewritten as a private
11826 -- extension.
11827
11828 if Ada_Version >= Ada_2005
11829 and then Nkind (Parent (A_Gen_T)) = N_Private_Extension_Declaration
11830 and then Synchronized_Present (Parent (A_Gen_T))
11831 then
11832 -- The actual must be a synchronized tagged type
11833
11834 if not Is_Tagged_Type (Act_T) then
11835 Error_Msg_N
11836 ("actual of synchronized type must be tagged", Actual);
11837 Abandon_Instantiation (Actual);
11838
11839 elsif Nkind (Parent (Act_T)) = N_Full_Type_Declaration
11840 and then Nkind (Type_Definition (Parent (Act_T))) =
11841 N_Derived_Type_Definition
11842 and then not Synchronized_Present
11843 (Type_Definition (Parent (Act_T)))
11844 then
11845 Error_Msg_N
11846 ("actual of synchronized type must be synchronized", Actual);
11847 Abandon_Instantiation (Actual);
11848 end if;
11849 end if;
11850
11851 -- Perform atomic/volatile checks (RM C.6(12)). Note that AI05-0218-1
11852 -- removes the second instance of the phrase "or allow pass by copy".
11853
11854 if Is_Atomic (Act_T) and then not Is_Atomic (Ancestor) then
11855 Error_Msg_N
11856 ("cannot have atomic actual type for non-atomic formal type",
11857 Actual);
11858
11859 elsif Is_Volatile (Act_T) and then not Is_Volatile (Ancestor) then
11860 Error_Msg_N
11861 ("cannot have volatile actual type for non-volatile formal type",
11862 Actual);
11863 end if;
11864
11865 -- It should not be necessary to check for unknown discriminants on
11866 -- Formal, but for some reason Has_Unknown_Discriminants is false for
11867 -- A_Gen_T, so Is_Definite_Subtype incorrectly returns True. This
11868 -- needs fixing. ???
11869
11870 if Is_Definite_Subtype (A_Gen_T)
11871 and then not Unknown_Discriminants_Present (Formal)
11872 and then not Is_Definite_Subtype (Act_T)
11873 then
11874 Error_Msg_N ("actual subtype must be constrained", Actual);
11875 Abandon_Instantiation (Actual);
11876 end if;
11877
11878 if not Unknown_Discriminants_Present (Formal) then
11879 if Is_Constrained (Ancestor) then
11880 if not Is_Constrained (Act_T) then
11881 Error_Msg_N ("actual subtype must be constrained", Actual);
11882 Abandon_Instantiation (Actual);
11883 end if;
11884
11885 -- Ancestor is unconstrained, Check if generic formal and actual
11886 -- agree on constrainedness. The check only applies to array types
11887 -- and discriminated types.
11888
11889 elsif Is_Constrained (Act_T) then
11890 if Ekind (Ancestor) = E_Access_Type
11891 or else (not Is_Constrained (A_Gen_T)
11892 and then Is_Composite_Type (A_Gen_T))
11893 then
11894 Error_Msg_N ("actual subtype must be unconstrained", Actual);
11895 Abandon_Instantiation (Actual);
11896 end if;
11897
11898 -- A class-wide type is only allowed if the formal has unknown
11899 -- discriminants.
11900
11901 elsif Is_Class_Wide_Type (Act_T)
11902 and then not Has_Unknown_Discriminants (Ancestor)
11903 then
11904 Error_Msg_NE
11905 ("actual for & cannot be a class-wide type", Actual, Gen_T);
11906 Abandon_Instantiation (Actual);
11907
11908 -- Otherwise, the formal and actual must have the same number
11909 -- of discriminants and each discriminant of the actual must
11910 -- correspond to a discriminant of the formal.
11911
11912 elsif Has_Discriminants (Act_T)
11913 and then not Has_Unknown_Discriminants (Act_T)
11914 and then Has_Discriminants (Ancestor)
11915 then
11916 Actual_Discr := First_Discriminant (Act_T);
11917 Ancestor_Discr := First_Discriminant (Ancestor);
11918 while Present (Actual_Discr)
11919 and then Present (Ancestor_Discr)
11920 loop
11921 if Base_Type (Act_T) /= Base_Type (Ancestor) and then
11922 No (Corresponding_Discriminant (Actual_Discr))
11923 then
11924 Error_Msg_NE
11925 ("discriminant & does not correspond "
11926 & "to ancestor discriminant", Actual, Actual_Discr);
11927 Abandon_Instantiation (Actual);
11928 end if;
11929
11930 Next_Discriminant (Actual_Discr);
11931 Next_Discriminant (Ancestor_Discr);
11932 end loop;
11933
11934 if Present (Actual_Discr) or else Present (Ancestor_Discr) then
11935 Error_Msg_NE
11936 ("actual for & must have same number of discriminants",
11937 Actual, Gen_T);
11938 Abandon_Instantiation (Actual);
11939 end if;
11940
11941 -- This case should be caught by the earlier check for
11942 -- constrainedness, but the check here is added for completeness.
11943
11944 elsif Has_Discriminants (Act_T)
11945 and then not Has_Unknown_Discriminants (Act_T)
11946 then
11947 Error_Msg_NE
11948 ("actual for & must not have discriminants", Actual, Gen_T);
11949 Abandon_Instantiation (Actual);
11950
11951 elsif Has_Discriminants (Ancestor) then
11952 Error_Msg_NE
11953 ("actual for & must have known discriminants", Actual, Gen_T);
11954 Abandon_Instantiation (Actual);
11955 end if;
11956
11957 if not Subtypes_Statically_Compatible
11958 (Act_T, Ancestor, Formal_Derived_Matching => True)
11959 then
11960 Error_Msg_N
11961 ("constraint on actual is incompatible with formal", Actual);
11962 Abandon_Instantiation (Actual);
11963 end if;
11964 end if;
11965
11966 -- If the formal and actual types are abstract, check that there
11967 -- are no abstract primitives of the actual type that correspond to
11968 -- nonabstract primitives of the formal type (second sentence of
11969 -- RM95 3.9.3(9)).
11970
11971 if Is_Abstract_Type (A_Gen_T) and then Is_Abstract_Type (Act_T) then
11972 Check_Abstract_Primitives : declare
11973 Gen_Prims : constant Elist_Id :=
11974 Primitive_Operations (A_Gen_T);
11975 Gen_Elmt : Elmt_Id;
11976 Gen_Subp : Entity_Id;
11977 Anc_Subp : Entity_Id;
11978 Anc_Formal : Entity_Id;
11979 Anc_F_Type : Entity_Id;
11980
11981 Act_Prims : constant Elist_Id := Primitive_Operations (Act_T);
11982 Act_Elmt : Elmt_Id;
11983 Act_Subp : Entity_Id;
11984 Act_Formal : Entity_Id;
11985 Act_F_Type : Entity_Id;
11986
11987 Subprograms_Correspond : Boolean;
11988
11989 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean;
11990 -- Returns true if T2 is derived directly or indirectly from
11991 -- T1, including derivations from interfaces. T1 and T2 are
11992 -- required to be specific tagged base types.
11993
11994 ------------------------
11995 -- Is_Tagged_Ancestor --
11996 ------------------------
11997
11998 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean
11999 is
12000 Intfc_Elmt : Elmt_Id;
12001
12002 begin
12003 -- The predicate is satisfied if the types are the same
12004
12005 if T1 = T2 then
12006 return True;
12007
12008 -- If we've reached the top of the derivation chain then
12009 -- we know that T1 is not an ancestor of T2.
12010
12011 elsif Etype (T2) = T2 then
12012 return False;
12013
12014 -- Proceed to check T2's immediate parent
12015
12016 elsif Is_Ancestor (T1, Base_Type (Etype (T2))) then
12017 return True;
12018
12019 -- Finally, check to see if T1 is an ancestor of any of T2's
12020 -- progenitors.
12021
12022 else
12023 Intfc_Elmt := First_Elmt (Interfaces (T2));
12024 while Present (Intfc_Elmt) loop
12025 if Is_Ancestor (T1, Node (Intfc_Elmt)) then
12026 return True;
12027 end if;
12028
12029 Next_Elmt (Intfc_Elmt);
12030 end loop;
12031 end if;
12032
12033 return False;
12034 end Is_Tagged_Ancestor;
12035
12036 -- Start of processing for Check_Abstract_Primitives
12037
12038 begin
12039 -- Loop over all of the formal derived type's primitives
12040
12041 Gen_Elmt := First_Elmt (Gen_Prims);
12042 while Present (Gen_Elmt) loop
12043 Gen_Subp := Node (Gen_Elmt);
12044
12045 -- If the primitive of the formal is not abstract, then
12046 -- determine whether there is a corresponding primitive of
12047 -- the actual type that's abstract.
12048
12049 if not Is_Abstract_Subprogram (Gen_Subp) then
12050 Act_Elmt := First_Elmt (Act_Prims);
12051 while Present (Act_Elmt) loop
12052 Act_Subp := Node (Act_Elmt);
12053
12054 -- If we find an abstract primitive of the actual,
12055 -- then we need to test whether it corresponds to the
12056 -- subprogram from which the generic formal primitive
12057 -- is inherited.
12058
12059 if Is_Abstract_Subprogram (Act_Subp) then
12060 Anc_Subp := Alias (Gen_Subp);
12061
12062 -- Test whether we have a corresponding primitive
12063 -- by comparing names, kinds, formal types, and
12064 -- result types.
12065
12066 if Chars (Anc_Subp) = Chars (Act_Subp)
12067 and then Ekind (Anc_Subp) = Ekind (Act_Subp)
12068 then
12069 Anc_Formal := First_Formal (Anc_Subp);
12070 Act_Formal := First_Formal (Act_Subp);
12071 while Present (Anc_Formal)
12072 and then Present (Act_Formal)
12073 loop
12074 Anc_F_Type := Etype (Anc_Formal);
12075 Act_F_Type := Etype (Act_Formal);
12076
12077 if Ekind (Anc_F_Type) =
12078 E_Anonymous_Access_Type
12079 then
12080 Anc_F_Type := Designated_Type (Anc_F_Type);
12081
12082 if Ekind (Act_F_Type) =
12083 E_Anonymous_Access_Type
12084 then
12085 Act_F_Type :=
12086 Designated_Type (Act_F_Type);
12087 else
12088 exit;
12089 end if;
12090
12091 elsif
12092 Ekind (Act_F_Type) = E_Anonymous_Access_Type
12093 then
12094 exit;
12095 end if;
12096
12097 Anc_F_Type := Base_Type (Anc_F_Type);
12098 Act_F_Type := Base_Type (Act_F_Type);
12099
12100 -- If the formal is controlling, then the
12101 -- the type of the actual primitive's formal
12102 -- must be derived directly or indirectly
12103 -- from the type of the ancestor primitive's
12104 -- formal.
12105
12106 if Is_Controlling_Formal (Anc_Formal) then
12107 if not Is_Tagged_Ancestor
12108 (Anc_F_Type, Act_F_Type)
12109 then
12110 exit;
12111 end if;
12112
12113 -- Otherwise the types of the formals must
12114 -- be the same.
12115
12116 elsif Anc_F_Type /= Act_F_Type then
12117 exit;
12118 end if;
12119
12120 Next_Entity (Anc_Formal);
12121 Next_Entity (Act_Formal);
12122 end loop;
12123
12124 -- If we traversed through all of the formals
12125 -- then so far the subprograms correspond, so
12126 -- now check that any result types correspond.
12127
12128 if No (Anc_Formal) and then No (Act_Formal) then
12129 Subprograms_Correspond := True;
12130
12131 if Ekind (Act_Subp) = E_Function then
12132 Anc_F_Type := Etype (Anc_Subp);
12133 Act_F_Type := Etype (Act_Subp);
12134
12135 if Ekind (Anc_F_Type) =
12136 E_Anonymous_Access_Type
12137 then
12138 Anc_F_Type :=
12139 Designated_Type (Anc_F_Type);
12140
12141 if Ekind (Act_F_Type) =
12142 E_Anonymous_Access_Type
12143 then
12144 Act_F_Type :=
12145 Designated_Type (Act_F_Type);
12146 else
12147 Subprograms_Correspond := False;
12148 end if;
12149
12150 elsif
12151 Ekind (Act_F_Type)
12152 = E_Anonymous_Access_Type
12153 then
12154 Subprograms_Correspond := False;
12155 end if;
12156
12157 Anc_F_Type := Base_Type (Anc_F_Type);
12158 Act_F_Type := Base_Type (Act_F_Type);
12159
12160 -- Now either the result types must be
12161 -- the same or, if the result type is
12162 -- controlling, the result type of the
12163 -- actual primitive must descend from the
12164 -- result type of the ancestor primitive.
12165
12166 if Subprograms_Correspond
12167 and then Anc_F_Type /= Act_F_Type
12168 and then
12169 Has_Controlling_Result (Anc_Subp)
12170 and then not Is_Tagged_Ancestor
12171 (Anc_F_Type, Act_F_Type)
12172 then
12173 Subprograms_Correspond := False;
12174 end if;
12175 end if;
12176
12177 -- Found a matching subprogram belonging to
12178 -- formal ancestor type, so actual subprogram
12179 -- corresponds and this violates 3.9.3(9).
12180
12181 if Subprograms_Correspond then
12182 Error_Msg_NE
12183 ("abstract subprogram & overrides "
12184 & "nonabstract subprogram of ancestor",
12185 Actual, Act_Subp);
12186 end if;
12187 end if;
12188 end if;
12189 end if;
12190
12191 Next_Elmt (Act_Elmt);
12192 end loop;
12193 end if;
12194
12195 Next_Elmt (Gen_Elmt);
12196 end loop;
12197 end Check_Abstract_Primitives;
12198 end if;
12199
12200 -- Verify that limitedness matches. If parent is a limited
12201 -- interface then the generic formal is not unless declared
12202 -- explicitly so. If not declared limited, the actual cannot be
12203 -- limited (see AI05-0087).
12204
12205 -- Even though this AI is a binding interpretation, we enable the
12206 -- check only in Ada 2012 mode, because this improper construct
12207 -- shows up in user code and in existing B-tests.
12208
12209 if Is_Limited_Type (Act_T)
12210 and then not Is_Limited_Type (A_Gen_T)
12211 and then Ada_Version >= Ada_2012
12212 then
12213 if In_Instance then
12214 null;
12215 else
12216 Error_Msg_NE
12217 ("actual for non-limited & cannot be a limited type",
12218 Actual, Gen_T);
12219 Explain_Limited_Type (Act_T, Actual);
12220 Abandon_Instantiation (Actual);
12221 end if;
12222 end if;
12223 end Validate_Derived_Type_Instance;
12224
12225 ----------------------------------------
12226 -- Validate_Discriminated_Formal_Type --
12227 ----------------------------------------
12228
12229 procedure Validate_Discriminated_Formal_Type is
12230 Formal_Discr : Entity_Id;
12231 Actual_Discr : Entity_Id;
12232 Formal_Subt : Entity_Id;
12233
12234 begin
12235 if Has_Discriminants (A_Gen_T) then
12236 if not Has_Discriminants (Act_T) then
12237 Error_Msg_NE
12238 ("actual for & must have discriminants", Actual, Gen_T);
12239 Abandon_Instantiation (Actual);
12240
12241 elsif Is_Constrained (Act_T) then
12242 Error_Msg_NE
12243 ("actual for & must be unconstrained", Actual, Gen_T);
12244 Abandon_Instantiation (Actual);
12245
12246 else
12247 Formal_Discr := First_Discriminant (A_Gen_T);
12248 Actual_Discr := First_Discriminant (Act_T);
12249 while Formal_Discr /= Empty loop
12250 if Actual_Discr = Empty then
12251 Error_Msg_NE
12252 ("discriminants on actual do not match formal",
12253 Actual, Gen_T);
12254 Abandon_Instantiation (Actual);
12255 end if;
12256
12257 Formal_Subt := Get_Instance_Of (Etype (Formal_Discr));
12258
12259 -- Access discriminants match if designated types do
12260
12261 if Ekind (Base_Type (Formal_Subt)) = E_Anonymous_Access_Type
12262 and then (Ekind (Base_Type (Etype (Actual_Discr)))) =
12263 E_Anonymous_Access_Type
12264 and then
12265 Get_Instance_Of
12266 (Designated_Type (Base_Type (Formal_Subt))) =
12267 Designated_Type (Base_Type (Etype (Actual_Discr)))
12268 then
12269 null;
12270
12271 elsif Base_Type (Formal_Subt) /=
12272 Base_Type (Etype (Actual_Discr))
12273 then
12274 Error_Msg_NE
12275 ("types of actual discriminants must match formal",
12276 Actual, Gen_T);
12277 Abandon_Instantiation (Actual);
12278
12279 elsif not Subtypes_Statically_Match
12280 (Formal_Subt, Etype (Actual_Discr))
12281 and then Ada_Version >= Ada_95
12282 then
12283 Error_Msg_NE
12284 ("subtypes of actual discriminants must match formal",
12285 Actual, Gen_T);
12286 Abandon_Instantiation (Actual);
12287 end if;
12288
12289 Next_Discriminant (Formal_Discr);
12290 Next_Discriminant (Actual_Discr);
12291 end loop;
12292
12293 if Actual_Discr /= Empty then
12294 Error_Msg_NE
12295 ("discriminants on actual do not match formal",
12296 Actual, Gen_T);
12297 Abandon_Instantiation (Actual);
12298 end if;
12299 end if;
12300 end if;
12301 end Validate_Discriminated_Formal_Type;
12302
12303 ---------------------------------------
12304 -- Validate_Incomplete_Type_Instance --
12305 ---------------------------------------
12306
12307 procedure Validate_Incomplete_Type_Instance is
12308 begin
12309 if not Is_Tagged_Type (Act_T)
12310 and then Is_Tagged_Type (A_Gen_T)
12311 then
12312 Error_Msg_NE
12313 ("actual for & must be a tagged type", Actual, Gen_T);
12314 end if;
12315
12316 Validate_Discriminated_Formal_Type;
12317 end Validate_Incomplete_Type_Instance;
12318
12319 --------------------------------------
12320 -- Validate_Interface_Type_Instance --
12321 --------------------------------------
12322
12323 procedure Validate_Interface_Type_Instance is
12324 begin
12325 if not Is_Interface (Act_T) then
12326 Error_Msg_NE
12327 ("actual for formal interface type must be an interface",
12328 Actual, Gen_T);
12329
12330 elsif Is_Limited_Type (Act_T) /= Is_Limited_Type (A_Gen_T)
12331 or else Is_Task_Interface (A_Gen_T) /= Is_Task_Interface (Act_T)
12332 or else Is_Protected_Interface (A_Gen_T) /=
12333 Is_Protected_Interface (Act_T)
12334 or else Is_Synchronized_Interface (A_Gen_T) /=
12335 Is_Synchronized_Interface (Act_T)
12336 then
12337 Error_Msg_NE
12338 ("actual for interface& does not match (RM 12.5.5(4))",
12339 Actual, Gen_T);
12340 end if;
12341 end Validate_Interface_Type_Instance;
12342
12343 ------------------------------------
12344 -- Validate_Private_Type_Instance --
12345 ------------------------------------
12346
12347 procedure Validate_Private_Type_Instance is
12348 begin
12349 if Is_Limited_Type (Act_T)
12350 and then not Is_Limited_Type (A_Gen_T)
12351 then
12352 if In_Instance then
12353 null;
12354 else
12355 Error_Msg_NE
12356 ("actual for non-limited & cannot be a limited type", Actual,
12357 Gen_T);
12358 Explain_Limited_Type (Act_T, Actual);
12359 Abandon_Instantiation (Actual);
12360 end if;
12361
12362 elsif Known_To_Have_Preelab_Init (A_Gen_T)
12363 and then not Has_Preelaborable_Initialization (Act_T)
12364 then
12365 Error_Msg_NE
12366 ("actual for & must have preelaborable initialization", Actual,
12367 Gen_T);
12368
12369 elsif not Is_Definite_Subtype (Act_T)
12370 and then Is_Definite_Subtype (A_Gen_T)
12371 and then Ada_Version >= Ada_95
12372 then
12373 Error_Msg_NE
12374 ("actual for & must be a definite subtype", Actual, Gen_T);
12375
12376 elsif not Is_Tagged_Type (Act_T)
12377 and then Is_Tagged_Type (A_Gen_T)
12378 then
12379 Error_Msg_NE
12380 ("actual for & must be a tagged type", Actual, Gen_T);
12381 end if;
12382
12383 Validate_Discriminated_Formal_Type;
12384 Ancestor := Gen_T;
12385 end Validate_Private_Type_Instance;
12386
12387 -- Start of processing for Instantiate_Type
12388
12389 begin
12390 if Get_Instance_Of (A_Gen_T) /= A_Gen_T then
12391 Error_Msg_N ("duplicate instantiation of generic type", Actual);
12392 return New_List (Error);
12393
12394 elsif not Is_Entity_Name (Actual)
12395 or else not Is_Type (Entity (Actual))
12396 then
12397 Error_Msg_NE
12398 ("expect valid subtype mark to instantiate &", Actual, Gen_T);
12399 Abandon_Instantiation (Actual);
12400
12401 else
12402 Act_T := Entity (Actual);
12403
12404 -- Ada 2005 (AI-216): An Unchecked_Union subtype shall only be passed
12405 -- as a generic actual parameter if the corresponding formal type
12406 -- does not have a known_discriminant_part, or is a formal derived
12407 -- type that is an Unchecked_Union type.
12408
12409 if Is_Unchecked_Union (Base_Type (Act_T)) then
12410 if not Has_Discriminants (A_Gen_T)
12411 or else (Is_Derived_Type (A_Gen_T)
12412 and then Is_Unchecked_Union (A_Gen_T))
12413 then
12414 null;
12415 else
12416 Error_Msg_N ("unchecked union cannot be the actual for a "
12417 & "discriminated formal type", Act_T);
12418
12419 end if;
12420 end if;
12421
12422 -- Deal with fixed/floating restrictions
12423
12424 if Is_Floating_Point_Type (Act_T) then
12425 Check_Restriction (No_Floating_Point, Actual);
12426 elsif Is_Fixed_Point_Type (Act_T) then
12427 Check_Restriction (No_Fixed_Point, Actual);
12428 end if;
12429
12430 -- Deal with error of using incomplete type as generic actual.
12431 -- This includes limited views of a type, even if the non-limited
12432 -- view may be available.
12433
12434 if Ekind (Act_T) = E_Incomplete_Type
12435 or else (Is_Class_Wide_Type (Act_T)
12436 and then Ekind (Root_Type (Act_T)) = E_Incomplete_Type)
12437 then
12438 -- If the formal is an incomplete type, the actual can be
12439 -- incomplete as well.
12440
12441 if Ekind (A_Gen_T) = E_Incomplete_Type then
12442 null;
12443
12444 elsif Is_Class_Wide_Type (Act_T)
12445 or else No (Full_View (Act_T))
12446 then
12447 Error_Msg_N ("premature use of incomplete type", Actual);
12448 Abandon_Instantiation (Actual);
12449 else
12450 Act_T := Full_View (Act_T);
12451 Set_Entity (Actual, Act_T);
12452
12453 if Has_Private_Component (Act_T) then
12454 Error_Msg_N
12455 ("premature use of type with private component", Actual);
12456 end if;
12457 end if;
12458
12459 -- Deal with error of premature use of private type as generic actual
12460
12461 elsif Is_Private_Type (Act_T)
12462 and then Is_Private_Type (Base_Type (Act_T))
12463 and then not Is_Generic_Type (Act_T)
12464 and then not Is_Derived_Type (Act_T)
12465 and then No (Full_View (Root_Type (Act_T)))
12466 then
12467 -- If the formal is an incomplete type, the actual can be
12468 -- private or incomplete as well.
12469
12470 if Ekind (A_Gen_T) = E_Incomplete_Type then
12471 null;
12472 else
12473 Error_Msg_N ("premature use of private type", Actual);
12474 end if;
12475
12476 elsif Has_Private_Component (Act_T) then
12477 Error_Msg_N
12478 ("premature use of type with private component", Actual);
12479 end if;
12480
12481 Set_Instance_Of (A_Gen_T, Act_T);
12482
12483 -- If the type is generic, the class-wide type may also be used
12484
12485 if Is_Tagged_Type (A_Gen_T)
12486 and then Is_Tagged_Type (Act_T)
12487 and then not Is_Class_Wide_Type (A_Gen_T)
12488 then
12489 Set_Instance_Of (Class_Wide_Type (A_Gen_T),
12490 Class_Wide_Type (Act_T));
12491 end if;
12492
12493 if not Is_Abstract_Type (A_Gen_T)
12494 and then Is_Abstract_Type (Act_T)
12495 then
12496 Error_Msg_N
12497 ("actual of non-abstract formal cannot be abstract", Actual);
12498 end if;
12499
12500 -- A generic scalar type is a first subtype for which we generate
12501 -- an anonymous base type. Indicate that the instance of this base
12502 -- is the base type of the actual.
12503
12504 if Is_Scalar_Type (A_Gen_T) then
12505 Set_Instance_Of (Etype (A_Gen_T), Etype (Act_T));
12506 end if;
12507 end if;
12508
12509 if Error_Posted (Act_T) then
12510 null;
12511 else
12512 case Nkind (Def) is
12513 when N_Formal_Private_Type_Definition =>
12514 Validate_Private_Type_Instance;
12515
12516 when N_Formal_Incomplete_Type_Definition =>
12517 Validate_Incomplete_Type_Instance;
12518
12519 when N_Formal_Derived_Type_Definition =>
12520 Validate_Derived_Type_Instance;
12521
12522 when N_Formal_Discrete_Type_Definition =>
12523 if not Is_Discrete_Type (Act_T) then
12524 Error_Msg_NE
12525 ("expect discrete type in instantiation of&",
12526 Actual, Gen_T);
12527 Abandon_Instantiation (Actual);
12528 end if;
12529
12530 Diagnose_Predicated_Actual;
12531
12532 when N_Formal_Signed_Integer_Type_Definition =>
12533 if not Is_Signed_Integer_Type (Act_T) then
12534 Error_Msg_NE
12535 ("expect signed integer type in instantiation of&",
12536 Actual, Gen_T);
12537 Abandon_Instantiation (Actual);
12538 end if;
12539
12540 Diagnose_Predicated_Actual;
12541
12542 when N_Formal_Modular_Type_Definition =>
12543 if not Is_Modular_Integer_Type (Act_T) then
12544 Error_Msg_NE
12545 ("expect modular type in instantiation of &",
12546 Actual, Gen_T);
12547 Abandon_Instantiation (Actual);
12548 end if;
12549
12550 Diagnose_Predicated_Actual;
12551
12552 when N_Formal_Floating_Point_Definition =>
12553 if not Is_Floating_Point_Type (Act_T) then
12554 Error_Msg_NE
12555 ("expect float type in instantiation of &", Actual, Gen_T);
12556 Abandon_Instantiation (Actual);
12557 end if;
12558
12559 when N_Formal_Ordinary_Fixed_Point_Definition =>
12560 if not Is_Ordinary_Fixed_Point_Type (Act_T) then
12561 Error_Msg_NE
12562 ("expect ordinary fixed point type in instantiation of &",
12563 Actual, Gen_T);
12564 Abandon_Instantiation (Actual);
12565 end if;
12566
12567 when N_Formal_Decimal_Fixed_Point_Definition =>
12568 if not Is_Decimal_Fixed_Point_Type (Act_T) then
12569 Error_Msg_NE
12570 ("expect decimal type in instantiation of &",
12571 Actual, Gen_T);
12572 Abandon_Instantiation (Actual);
12573 end if;
12574
12575 when N_Array_Type_Definition =>
12576 Validate_Array_Type_Instance;
12577
12578 when N_Access_To_Object_Definition =>
12579 Validate_Access_Type_Instance;
12580
12581 when N_Access_Function_Definition |
12582 N_Access_Procedure_Definition =>
12583 Validate_Access_Subprogram_Instance;
12584
12585 when N_Record_Definition =>
12586 Validate_Interface_Type_Instance;
12587
12588 when N_Derived_Type_Definition =>
12589 Validate_Derived_Interface_Type_Instance;
12590
12591 when others =>
12592 raise Program_Error;
12593
12594 end case;
12595 end if;
12596
12597 Subt := New_Copy (Gen_T);
12598
12599 -- Use adjusted sloc of subtype name as the location for other nodes in
12600 -- the subtype declaration.
12601
12602 Loc := Sloc (Subt);
12603
12604 Decl_Node :=
12605 Make_Subtype_Declaration (Loc,
12606 Defining_Identifier => Subt,
12607 Subtype_Indication => New_Occurrence_Of (Act_T, Loc));
12608
12609 if Is_Private_Type (Act_T) then
12610 Set_Has_Private_View (Subtype_Indication (Decl_Node));
12611
12612 elsif Is_Access_Type (Act_T)
12613 and then Is_Private_Type (Designated_Type (Act_T))
12614 then
12615 Set_Has_Private_View (Subtype_Indication (Decl_Node));
12616 end if;
12617
12618 -- In Ada 2012 the actual may be a limited view. Indicate that
12619 -- the local subtype must be treated as such.
12620
12621 if From_Limited_With (Act_T) then
12622 Set_Ekind (Subt, E_Incomplete_Subtype);
12623 Set_From_Limited_With (Subt);
12624 end if;
12625
12626 Decl_Nodes := New_List (Decl_Node);
12627
12628 -- Flag actual derived types so their elaboration produces the
12629 -- appropriate renamings for the primitive operations of the ancestor.
12630 -- Flag actual for formal private types as well, to determine whether
12631 -- operations in the private part may override inherited operations.
12632 -- If the formal has an interface list, the ancestor is not the
12633 -- parent, but the analyzed formal that includes the interface
12634 -- operations of all its progenitors.
12635
12636 -- Same treatment for formal private types, so we can check whether the
12637 -- type is tagged limited when validating derivations in the private
12638 -- part. (See AI05-096).
12639
12640 if Nkind (Def) = N_Formal_Derived_Type_Definition then
12641 if Present (Interface_List (Def)) then
12642 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
12643 else
12644 Set_Generic_Parent_Type (Decl_Node, Ancestor);
12645 end if;
12646
12647 elsif Nkind_In (Def, N_Formal_Private_Type_Definition,
12648 N_Formal_Incomplete_Type_Definition)
12649 then
12650 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
12651 end if;
12652
12653 -- If the actual is a synchronized type that implements an interface,
12654 -- the primitive operations are attached to the corresponding record,
12655 -- and we have to treat it as an additional generic actual, so that its
12656 -- primitive operations become visible in the instance. The task or
12657 -- protected type itself does not carry primitive operations.
12658
12659 if Is_Concurrent_Type (Act_T)
12660 and then Is_Tagged_Type (Act_T)
12661 and then Present (Corresponding_Record_Type (Act_T))
12662 and then Present (Ancestor)
12663 and then Is_Interface (Ancestor)
12664 then
12665 declare
12666 Corr_Rec : constant Entity_Id :=
12667 Corresponding_Record_Type (Act_T);
12668 New_Corr : Entity_Id;
12669 Corr_Decl : Node_Id;
12670
12671 begin
12672 New_Corr := Make_Temporary (Loc, 'S');
12673 Corr_Decl :=
12674 Make_Subtype_Declaration (Loc,
12675 Defining_Identifier => New_Corr,
12676 Subtype_Indication =>
12677 New_Occurrence_Of (Corr_Rec, Loc));
12678 Append_To (Decl_Nodes, Corr_Decl);
12679
12680 if Ekind (Act_T) = E_Task_Type then
12681 Set_Ekind (Subt, E_Task_Subtype);
12682 else
12683 Set_Ekind (Subt, E_Protected_Subtype);
12684 end if;
12685
12686 Set_Corresponding_Record_Type (Subt, Corr_Rec);
12687 Set_Generic_Parent_Type (Corr_Decl, Ancestor);
12688 Set_Generic_Parent_Type (Decl_Node, Empty);
12689 end;
12690 end if;
12691
12692 -- For a floating-point type, capture dimension info if any, because
12693 -- the generated subtype declaration does not come from source and
12694 -- will not process dimensions.
12695
12696 if Is_Floating_Point_Type (Act_T) then
12697 Copy_Dimensions (Act_T, Subt);
12698 end if;
12699
12700 return Decl_Nodes;
12701 end Instantiate_Type;
12702
12703 ---------------------
12704 -- Is_In_Main_Unit --
12705 ---------------------
12706
12707 function Is_In_Main_Unit (N : Node_Id) return Boolean is
12708 Unum : constant Unit_Number_Type := Get_Source_Unit (N);
12709 Current_Unit : Node_Id;
12710
12711 begin
12712 if Unum = Main_Unit then
12713 return True;
12714
12715 -- If the current unit is a subunit then it is either the main unit or
12716 -- is being compiled as part of the main unit.
12717
12718 elsif Nkind (N) = N_Compilation_Unit then
12719 return Nkind (Unit (N)) = N_Subunit;
12720 end if;
12721
12722 Current_Unit := Parent (N);
12723 while Present (Current_Unit)
12724 and then Nkind (Current_Unit) /= N_Compilation_Unit
12725 loop
12726 Current_Unit := Parent (Current_Unit);
12727 end loop;
12728
12729 -- The instantiation node is in the main unit, or else the current node
12730 -- (perhaps as the result of nested instantiations) is in the main unit,
12731 -- or in the declaration of the main unit, which in this last case must
12732 -- be a body.
12733
12734 return Unum = Main_Unit
12735 or else Current_Unit = Cunit (Main_Unit)
12736 or else Current_Unit = Library_Unit (Cunit (Main_Unit))
12737 or else (Present (Library_Unit (Current_Unit))
12738 and then Is_In_Main_Unit (Library_Unit (Current_Unit)));
12739 end Is_In_Main_Unit;
12740
12741 ----------------------------
12742 -- Load_Parent_Of_Generic --
12743 ----------------------------
12744
12745 procedure Load_Parent_Of_Generic
12746 (N : Node_Id;
12747 Spec : Node_Id;
12748 Body_Optional : Boolean := False)
12749 is
12750 Comp_Unit : constant Node_Id := Cunit (Get_Source_Unit (Spec));
12751 Saved_Style_Check : constant Boolean := Style_Check;
12752 Saved_Warnings : constant Warning_Record := Save_Warnings;
12753 True_Parent : Node_Id;
12754 Inst_Node : Node_Id;
12755 OK : Boolean;
12756 Previous_Instances : constant Elist_Id := New_Elmt_List;
12757
12758 procedure Collect_Previous_Instances (Decls : List_Id);
12759 -- Collect all instantiations in the given list of declarations, that
12760 -- precede the generic that we need to load. If the bodies of these
12761 -- instantiations are available, we must analyze them, to ensure that
12762 -- the public symbols generated are the same when the unit is compiled
12763 -- to generate code, and when it is compiled in the context of a unit
12764 -- that needs a particular nested instance. This process is applied to
12765 -- both package and subprogram instances.
12766
12767 --------------------------------
12768 -- Collect_Previous_Instances --
12769 --------------------------------
12770
12771 procedure Collect_Previous_Instances (Decls : List_Id) is
12772 Decl : Node_Id;
12773
12774 begin
12775 Decl := First (Decls);
12776 while Present (Decl) loop
12777 if Sloc (Decl) >= Sloc (Inst_Node) then
12778 return;
12779
12780 -- If Decl is an instantiation, then record it as requiring
12781 -- instantiation of the corresponding body, except if it is an
12782 -- abbreviated instantiation generated internally for conformance
12783 -- checking purposes only for the case of a formal package
12784 -- declared without a box (see Instantiate_Formal_Package). Such
12785 -- an instantiation does not generate any code (the actual code
12786 -- comes from actual) and thus does not need to be analyzed here.
12787 -- If the instantiation appears with a generic package body it is
12788 -- not analyzed here either.
12789
12790 elsif Nkind (Decl) = N_Package_Instantiation
12791 and then not Is_Internal (Defining_Entity (Decl))
12792 then
12793 Append_Elmt (Decl, Previous_Instances);
12794
12795 -- For a subprogram instantiation, omit instantiations intrinsic
12796 -- operations (Unchecked_Conversions, etc.) that have no bodies.
12797
12798 elsif Nkind_In (Decl, N_Function_Instantiation,
12799 N_Procedure_Instantiation)
12800 and then not Is_Intrinsic_Subprogram (Entity (Name (Decl)))
12801 then
12802 Append_Elmt (Decl, Previous_Instances);
12803
12804 elsif Nkind (Decl) = N_Package_Declaration then
12805 Collect_Previous_Instances
12806 (Visible_Declarations (Specification (Decl)));
12807 Collect_Previous_Instances
12808 (Private_Declarations (Specification (Decl)));
12809
12810 -- Previous non-generic bodies may contain instances as well
12811
12812 elsif Nkind (Decl) = N_Package_Body
12813 and then Ekind (Corresponding_Spec (Decl)) /= E_Generic_Package
12814 then
12815 Collect_Previous_Instances (Declarations (Decl));
12816
12817 elsif Nkind (Decl) = N_Subprogram_Body
12818 and then not Acts_As_Spec (Decl)
12819 and then not Is_Generic_Subprogram (Corresponding_Spec (Decl))
12820 then
12821 Collect_Previous_Instances (Declarations (Decl));
12822 end if;
12823
12824 Next (Decl);
12825 end loop;
12826 end Collect_Previous_Instances;
12827
12828 -- Start of processing for Load_Parent_Of_Generic
12829
12830 begin
12831 if not In_Same_Source_Unit (N, Spec)
12832 or else Nkind (Unit (Comp_Unit)) = N_Package_Declaration
12833 or else (Nkind (Unit (Comp_Unit)) = N_Package_Body
12834 and then not Is_In_Main_Unit (Spec))
12835 then
12836 -- Find body of parent of spec, and analyze it. A special case arises
12837 -- when the parent is an instantiation, that is to say when we are
12838 -- currently instantiating a nested generic. In that case, there is
12839 -- no separate file for the body of the enclosing instance. Instead,
12840 -- the enclosing body must be instantiated as if it were a pending
12841 -- instantiation, in order to produce the body for the nested generic
12842 -- we require now. Note that in that case the generic may be defined
12843 -- in a package body, the instance defined in the same package body,
12844 -- and the original enclosing body may not be in the main unit.
12845
12846 Inst_Node := Empty;
12847
12848 True_Parent := Parent (Spec);
12849 while Present (True_Parent)
12850 and then Nkind (True_Parent) /= N_Compilation_Unit
12851 loop
12852 if Nkind (True_Parent) = N_Package_Declaration
12853 and then
12854 Nkind (Original_Node (True_Parent)) = N_Package_Instantiation
12855 then
12856 -- Parent is a compilation unit that is an instantiation.
12857 -- Instantiation node has been replaced with package decl.
12858
12859 Inst_Node := Original_Node (True_Parent);
12860 exit;
12861
12862 elsif Nkind (True_Parent) = N_Package_Declaration
12863 and then Present (Generic_Parent (Specification (True_Parent)))
12864 and then Nkind (Parent (True_Parent)) /= N_Compilation_Unit
12865 then
12866 -- Parent is an instantiation within another specification.
12867 -- Declaration for instance has been inserted before original
12868 -- instantiation node. A direct link would be preferable?
12869
12870 Inst_Node := Next (True_Parent);
12871 while Present (Inst_Node)
12872 and then Nkind (Inst_Node) /= N_Package_Instantiation
12873 loop
12874 Next (Inst_Node);
12875 end loop;
12876
12877 -- If the instance appears within a generic, and the generic
12878 -- unit is defined within a formal package of the enclosing
12879 -- generic, there is no generic body available, and none
12880 -- needed. A more precise test should be used ???
12881
12882 if No (Inst_Node) then
12883 return;
12884 end if;
12885
12886 exit;
12887
12888 else
12889 True_Parent := Parent (True_Parent);
12890 end if;
12891 end loop;
12892
12893 -- Case where we are currently instantiating a nested generic
12894
12895 if Present (Inst_Node) then
12896 if Nkind (Parent (True_Parent)) = N_Compilation_Unit then
12897
12898 -- Instantiation node and declaration of instantiated package
12899 -- were exchanged when only the declaration was needed.
12900 -- Restore instantiation node before proceeding with body.
12901
12902 Set_Unit (Parent (True_Parent), Inst_Node);
12903 end if;
12904
12905 -- Now complete instantiation of enclosing body, if it appears in
12906 -- some other unit. If it appears in the current unit, the body
12907 -- will have been instantiated already.
12908
12909 if No (Corresponding_Body (Instance_Spec (Inst_Node))) then
12910
12911 -- We need to determine the expander mode to instantiate the
12912 -- enclosing body. Because the generic body we need may use
12913 -- global entities declared in the enclosing package (including
12914 -- aggregates) it is in general necessary to compile this body
12915 -- with expansion enabled, except if we are within a generic
12916 -- package, in which case the usual generic rule applies.
12917
12918 declare
12919 Exp_Status : Boolean := True;
12920 Scop : Entity_Id;
12921
12922 begin
12923 -- Loop through scopes looking for generic package
12924
12925 Scop := Scope (Defining_Entity (Instance_Spec (Inst_Node)));
12926 while Present (Scop)
12927 and then Scop /= Standard_Standard
12928 loop
12929 if Ekind (Scop) = E_Generic_Package then
12930 Exp_Status := False;
12931 exit;
12932 end if;
12933
12934 Scop := Scope (Scop);
12935 end loop;
12936
12937 -- Collect previous instantiations in the unit that contains
12938 -- the desired generic.
12939
12940 if Nkind (Parent (True_Parent)) /= N_Compilation_Unit
12941 and then not Body_Optional
12942 then
12943 declare
12944 Decl : Elmt_Id;
12945 Info : Pending_Body_Info;
12946 Par : Node_Id;
12947
12948 begin
12949 Par := Parent (Inst_Node);
12950 while Present (Par) loop
12951 exit when Nkind (Parent (Par)) = N_Compilation_Unit;
12952 Par := Parent (Par);
12953 end loop;
12954
12955 pragma Assert (Present (Par));
12956
12957 if Nkind (Par) = N_Package_Body then
12958 Collect_Previous_Instances (Declarations (Par));
12959
12960 elsif Nkind (Par) = N_Package_Declaration then
12961 Collect_Previous_Instances
12962 (Visible_Declarations (Specification (Par)));
12963 Collect_Previous_Instances
12964 (Private_Declarations (Specification (Par)));
12965
12966 else
12967 -- Enclosing unit is a subprogram body. In this
12968 -- case all instance bodies are processed in order
12969 -- and there is no need to collect them separately.
12970
12971 null;
12972 end if;
12973
12974 Decl := First_Elmt (Previous_Instances);
12975 while Present (Decl) loop
12976 Info :=
12977 (Inst_Node => Node (Decl),
12978 Act_Decl =>
12979 Instance_Spec (Node (Decl)),
12980 Expander_Status => Exp_Status,
12981 Current_Sem_Unit =>
12982 Get_Code_Unit (Sloc (Node (Decl))),
12983 Scope_Suppress => Scope_Suppress,
12984 Local_Suppress_Stack_Top =>
12985 Local_Suppress_Stack_Top,
12986 Version => Ada_Version,
12987 Version_Pragma => Ada_Version_Pragma,
12988 Warnings => Save_Warnings,
12989 SPARK_Mode => SPARK_Mode,
12990 SPARK_Mode_Pragma => SPARK_Mode_Pragma);
12991
12992 -- Package instance
12993
12994 if
12995 Nkind (Node (Decl)) = N_Package_Instantiation
12996 then
12997 Instantiate_Package_Body
12998 (Info, Body_Optional => True);
12999
13000 -- Subprogram instance
13001
13002 else
13003 -- The instance_spec is in the wrapper package,
13004 -- usually followed by its local renaming
13005 -- declaration. See Build_Subprogram_Renaming
13006 -- for details.
13007
13008 declare
13009 Decl : Node_Id :=
13010 (Last (Visible_Declarations
13011 (Specification (Info.Act_Decl))));
13012 begin
13013 if Nkind (Decl) =
13014 N_Subprogram_Renaming_Declaration
13015 then
13016 Decl := Prev (Decl);
13017 end if;
13018
13019 Info.Act_Decl := Decl;
13020 end;
13021
13022 Instantiate_Subprogram_Body
13023 (Info, Body_Optional => True);
13024 end if;
13025
13026 Next_Elmt (Decl);
13027 end loop;
13028 end;
13029 end if;
13030
13031 Instantiate_Package_Body
13032 (Body_Info =>
13033 ((Inst_Node => Inst_Node,
13034 Act_Decl => True_Parent,
13035 Expander_Status => Exp_Status,
13036 Current_Sem_Unit => Get_Code_Unit
13037 (Sloc (Inst_Node)),
13038 Scope_Suppress => Scope_Suppress,
13039 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
13040 Version => Ada_Version,
13041 Version_Pragma => Ada_Version_Pragma,
13042 Warnings => Save_Warnings,
13043 SPARK_Mode => SPARK_Mode,
13044 SPARK_Mode_Pragma => SPARK_Mode_Pragma)),
13045 Body_Optional => Body_Optional);
13046 end;
13047 end if;
13048
13049 -- Case where we are not instantiating a nested generic
13050
13051 else
13052 Opt.Style_Check := False;
13053 Expander_Mode_Save_And_Set (True);
13054 Load_Needed_Body (Comp_Unit, OK);
13055 Opt.Style_Check := Saved_Style_Check;
13056 Restore_Warnings (Saved_Warnings);
13057 Expander_Mode_Restore;
13058
13059 if not OK
13060 and then Unit_Requires_Body (Defining_Entity (Spec))
13061 and then not Body_Optional
13062 then
13063 declare
13064 Bname : constant Unit_Name_Type :=
13065 Get_Body_Name (Get_Unit_Name (Unit (Comp_Unit)));
13066
13067 begin
13068 -- In CodePeer mode, the missing body may make the analysis
13069 -- incomplete, but we do not treat it as fatal.
13070
13071 if CodePeer_Mode then
13072 return;
13073
13074 else
13075 Error_Msg_Unit_1 := Bname;
13076 Error_Msg_N ("this instantiation requires$!", N);
13077 Error_Msg_File_1 :=
13078 Get_File_Name (Bname, Subunit => False);
13079 Error_Msg_N ("\but file{ was not found!", N);
13080 raise Unrecoverable_Error;
13081 end if;
13082 end;
13083 end if;
13084 end if;
13085 end if;
13086
13087 -- If loading parent of the generic caused an instantiation circularity,
13088 -- we abandon compilation at this point, because otherwise in some cases
13089 -- we get into trouble with infinite recursions after this point.
13090
13091 if Circularity_Detected then
13092 raise Unrecoverable_Error;
13093 end if;
13094 end Load_Parent_Of_Generic;
13095
13096 ---------------------------------
13097 -- Map_Formal_Package_Entities --
13098 ---------------------------------
13099
13100 procedure Map_Formal_Package_Entities (Form : Entity_Id; Act : Entity_Id) is
13101 E1 : Entity_Id;
13102 E2 : Entity_Id;
13103
13104 begin
13105 Set_Instance_Of (Form, Act);
13106
13107 -- Traverse formal and actual package to map the corresponding entities.
13108 -- We skip over internal entities that may be generated during semantic
13109 -- analysis, and find the matching entities by name, given that they
13110 -- must appear in the same order.
13111
13112 E1 := First_Entity (Form);
13113 E2 := First_Entity (Act);
13114 while Present (E1) and then E1 /= First_Private_Entity (Form) loop
13115 -- Could this test be a single condition??? Seems like it could, and
13116 -- isn't FPE (Form) a constant anyway???
13117
13118 if not Is_Internal (E1)
13119 and then Present (Parent (E1))
13120 and then not Is_Class_Wide_Type (E1)
13121 and then not Is_Internal_Name (Chars (E1))
13122 then
13123 while Present (E2) and then Chars (E2) /= Chars (E1) loop
13124 Next_Entity (E2);
13125 end loop;
13126
13127 if No (E2) then
13128 exit;
13129 else
13130 Set_Instance_Of (E1, E2);
13131
13132 if Is_Type (E1) and then Is_Tagged_Type (E2) then
13133 Set_Instance_Of (Class_Wide_Type (E1), Class_Wide_Type (E2));
13134 end if;
13135
13136 if Is_Constrained (E1) then
13137 Set_Instance_Of (Base_Type (E1), Base_Type (E2));
13138 end if;
13139
13140 if Ekind (E1) = E_Package and then No (Renamed_Object (E1)) then
13141 Map_Formal_Package_Entities (E1, E2);
13142 end if;
13143 end if;
13144 end if;
13145
13146 Next_Entity (E1);
13147 end loop;
13148 end Map_Formal_Package_Entities;
13149
13150 -----------------------
13151 -- Move_Freeze_Nodes --
13152 -----------------------
13153
13154 procedure Move_Freeze_Nodes
13155 (Out_Of : Entity_Id;
13156 After : Node_Id;
13157 L : List_Id)
13158 is
13159 Decl : Node_Id;
13160 Next_Decl : Node_Id;
13161 Next_Node : Node_Id := After;
13162 Spec : Node_Id;
13163
13164 function Is_Outer_Type (T : Entity_Id) return Boolean;
13165 -- Check whether entity is declared in a scope external to that of the
13166 -- generic unit.
13167
13168 -------------------
13169 -- Is_Outer_Type --
13170 -------------------
13171
13172 function Is_Outer_Type (T : Entity_Id) return Boolean is
13173 Scop : Entity_Id := Scope (T);
13174
13175 begin
13176 if Scope_Depth (Scop) < Scope_Depth (Out_Of) then
13177 return True;
13178
13179 else
13180 while Scop /= Standard_Standard loop
13181 if Scop = Out_Of then
13182 return False;
13183 else
13184 Scop := Scope (Scop);
13185 end if;
13186 end loop;
13187
13188 return True;
13189 end if;
13190 end Is_Outer_Type;
13191
13192 -- Start of processing for Move_Freeze_Nodes
13193
13194 begin
13195 if No (L) then
13196 return;
13197 end if;
13198
13199 -- First remove the freeze nodes that may appear before all other
13200 -- declarations.
13201
13202 Decl := First (L);
13203 while Present (Decl)
13204 and then Nkind (Decl) = N_Freeze_Entity
13205 and then Is_Outer_Type (Entity (Decl))
13206 loop
13207 Decl := Remove_Head (L);
13208 Insert_After (Next_Node, Decl);
13209 Set_Analyzed (Decl, False);
13210 Next_Node := Decl;
13211 Decl := First (L);
13212 end loop;
13213
13214 -- Next scan the list of declarations and remove each freeze node that
13215 -- appears ahead of the current node.
13216
13217 while Present (Decl) loop
13218 while Present (Next (Decl))
13219 and then Nkind (Next (Decl)) = N_Freeze_Entity
13220 and then Is_Outer_Type (Entity (Next (Decl)))
13221 loop
13222 Next_Decl := Remove_Next (Decl);
13223 Insert_After (Next_Node, Next_Decl);
13224 Set_Analyzed (Next_Decl, False);
13225 Next_Node := Next_Decl;
13226 end loop;
13227
13228 -- If the declaration is a nested package or concurrent type, then
13229 -- recurse. Nested generic packages will have been processed from the
13230 -- inside out.
13231
13232 case Nkind (Decl) is
13233 when N_Package_Declaration =>
13234 Spec := Specification (Decl);
13235
13236 when N_Task_Type_Declaration =>
13237 Spec := Task_Definition (Decl);
13238
13239 when N_Protected_Type_Declaration =>
13240 Spec := Protected_Definition (Decl);
13241
13242 when others =>
13243 Spec := Empty;
13244 end case;
13245
13246 if Present (Spec) then
13247 Move_Freeze_Nodes (Out_Of, Next_Node, Visible_Declarations (Spec));
13248 Move_Freeze_Nodes (Out_Of, Next_Node, Private_Declarations (Spec));
13249 end if;
13250
13251 Next (Decl);
13252 end loop;
13253 end Move_Freeze_Nodes;
13254
13255 ----------------
13256 -- Next_Assoc --
13257 ----------------
13258
13259 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr is
13260 begin
13261 return Generic_Renamings.Table (E).Next_In_HTable;
13262 end Next_Assoc;
13263
13264 ------------------------
13265 -- Preanalyze_Actuals --
13266 ------------------------
13267
13268 procedure Preanalyze_Actuals (N : Node_Id; Inst : Entity_Id := Empty) is
13269 Assoc : Node_Id;
13270 Act : Node_Id;
13271 Errs : constant Int := Serious_Errors_Detected;
13272
13273 Cur : Entity_Id := Empty;
13274 -- Current homograph of the instance name
13275
13276 Vis : Boolean;
13277 -- Saved visibility status of the current homograph
13278
13279 begin
13280 Assoc := First (Generic_Associations (N));
13281
13282 -- If the instance is a child unit, its name may hide an outer homonym,
13283 -- so make it invisible to perform name resolution on the actuals.
13284
13285 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name
13286 and then Present
13287 (Current_Entity (Defining_Identifier (Defining_Unit_Name (N))))
13288 then
13289 Cur := Current_Entity (Defining_Identifier (Defining_Unit_Name (N)));
13290
13291 if Is_Compilation_Unit (Cur) then
13292 Vis := Is_Immediately_Visible (Cur);
13293 Set_Is_Immediately_Visible (Cur, False);
13294 else
13295 Cur := Empty;
13296 end if;
13297 end if;
13298
13299 while Present (Assoc) loop
13300 if Nkind (Assoc) /= N_Others_Choice then
13301 Act := Explicit_Generic_Actual_Parameter (Assoc);
13302
13303 -- Within a nested instantiation, a defaulted actual is an empty
13304 -- association, so nothing to analyze. If the subprogram actual
13305 -- is an attribute, analyze prefix only, because actual is not a
13306 -- complete attribute reference.
13307
13308 -- If actual is an allocator, analyze expression only. The full
13309 -- analysis can generate code, and if instance is a compilation
13310 -- unit we have to wait until the package instance is installed
13311 -- to have a proper place to insert this code.
13312
13313 -- String literals may be operators, but at this point we do not
13314 -- know whether the actual is a formal subprogram or a string.
13315
13316 if No (Act) then
13317 null;
13318
13319 elsif Nkind (Act) = N_Attribute_Reference then
13320 Analyze (Prefix (Act));
13321
13322 elsif Nkind (Act) = N_Explicit_Dereference then
13323 Analyze (Prefix (Act));
13324
13325 elsif Nkind (Act) = N_Allocator then
13326 declare
13327 Expr : constant Node_Id := Expression (Act);
13328
13329 begin
13330 if Nkind (Expr) = N_Subtype_Indication then
13331 Analyze (Subtype_Mark (Expr));
13332
13333 -- Analyze separately each discriminant constraint, when
13334 -- given with a named association.
13335
13336 declare
13337 Constr : Node_Id;
13338
13339 begin
13340 Constr := First (Constraints (Constraint (Expr)));
13341 while Present (Constr) loop
13342 if Nkind (Constr) = N_Discriminant_Association then
13343 Analyze (Expression (Constr));
13344 else
13345 Analyze (Constr);
13346 end if;
13347
13348 Next (Constr);
13349 end loop;
13350 end;
13351
13352 else
13353 Analyze (Expr);
13354 end if;
13355 end;
13356
13357 elsif Nkind (Act) /= N_Operator_Symbol then
13358 Analyze (Act);
13359
13360 if Is_Entity_Name (Act)
13361 and then Is_Type (Entity (Act))
13362 and then From_Limited_With (Entity (Act))
13363 then
13364 Append_Elmt (Entity (Act), Incomplete_Actuals (Inst));
13365 end if;
13366 end if;
13367
13368 if Errs /= Serious_Errors_Detected then
13369
13370 -- Do a minimal analysis of the generic, to prevent spurious
13371 -- warnings complaining about the generic being unreferenced,
13372 -- before abandoning the instantiation.
13373
13374 Analyze (Name (N));
13375
13376 if Is_Entity_Name (Name (N))
13377 and then Etype (Name (N)) /= Any_Type
13378 then
13379 Generate_Reference (Entity (Name (N)), Name (N));
13380 Set_Is_Instantiated (Entity (Name (N)));
13381 end if;
13382
13383 if Present (Cur) then
13384
13385 -- For the case of a child instance hiding an outer homonym,
13386 -- provide additional warning which might explain the error.
13387
13388 Set_Is_Immediately_Visible (Cur, Vis);
13389 Error_Msg_NE
13390 ("& hides outer unit with the same name??",
13391 N, Defining_Unit_Name (N));
13392 end if;
13393
13394 Abandon_Instantiation (Act);
13395 end if;
13396 end if;
13397
13398 Next (Assoc);
13399 end loop;
13400
13401 if Present (Cur) then
13402 Set_Is_Immediately_Visible (Cur, Vis);
13403 end if;
13404 end Preanalyze_Actuals;
13405
13406 -------------------
13407 -- Remove_Parent --
13408 -------------------
13409
13410 procedure Remove_Parent (In_Body : Boolean := False) is
13411 S : Entity_Id := Current_Scope;
13412 -- S is the scope containing the instantiation just completed. The scope
13413 -- stack contains the parent instances of the instantiation, followed by
13414 -- the original S.
13415
13416 Cur_P : Entity_Id;
13417 E : Entity_Id;
13418 P : Entity_Id;
13419 Hidden : Elmt_Id;
13420
13421 begin
13422 -- After child instantiation is complete, remove from scope stack the
13423 -- extra copy of the current scope, and then remove parent instances.
13424
13425 if not In_Body then
13426 Pop_Scope;
13427
13428 while Current_Scope /= S loop
13429 P := Current_Scope;
13430 End_Package_Scope (Current_Scope);
13431
13432 if In_Open_Scopes (P) then
13433 E := First_Entity (P);
13434 while Present (E) loop
13435 Set_Is_Immediately_Visible (E, True);
13436 Next_Entity (E);
13437 end loop;
13438
13439 -- If instantiation is declared in a block, it is the enclosing
13440 -- scope that might be a parent instance. Note that only one
13441 -- block can be involved, because the parent instances have
13442 -- been installed within it.
13443
13444 if Ekind (P) = E_Block then
13445 Cur_P := Scope (P);
13446 else
13447 Cur_P := P;
13448 end if;
13449
13450 if Is_Generic_Instance (Cur_P) and then P /= Current_Scope then
13451 -- We are within an instance of some sibling. Retain
13452 -- visibility of parent, for proper subsequent cleanup, and
13453 -- reinstall private declarations as well.
13454
13455 Set_In_Private_Part (P);
13456 Install_Private_Declarations (P);
13457 end if;
13458
13459 -- If the ultimate parent is a top-level unit recorded in
13460 -- Instance_Parent_Unit, then reset its visibility to what it was
13461 -- before instantiation. (It's not clear what the purpose is of
13462 -- testing whether Scope (P) is In_Open_Scopes, but that test was
13463 -- present before the ultimate parent test was added.???)
13464
13465 elsif not In_Open_Scopes (Scope (P))
13466 or else (P = Instance_Parent_Unit
13467 and then not Parent_Unit_Visible)
13468 then
13469 Set_Is_Immediately_Visible (P, False);
13470
13471 -- If the current scope is itself an instantiation of a generic
13472 -- nested within P, and we are in the private part of body of this
13473 -- instantiation, restore the full views of P, that were removed
13474 -- in End_Package_Scope above. This obscure case can occur when a
13475 -- subunit of a generic contains an instance of a child unit of
13476 -- its generic parent unit.
13477
13478 elsif S = Current_Scope and then Is_Generic_Instance (S) then
13479 declare
13480 Par : constant Entity_Id :=
13481 Generic_Parent (Package_Specification (S));
13482 begin
13483 if Present (Par)
13484 and then P = Scope (Par)
13485 and then (In_Package_Body (S) or else In_Private_Part (S))
13486 then
13487 Set_In_Private_Part (P);
13488 Install_Private_Declarations (P);
13489 end if;
13490 end;
13491 end if;
13492 end loop;
13493
13494 -- Reset visibility of entities in the enclosing scope
13495
13496 Set_Is_Hidden_Open_Scope (Current_Scope, False);
13497
13498 Hidden := First_Elmt (Hidden_Entities);
13499 while Present (Hidden) loop
13500 Set_Is_Immediately_Visible (Node (Hidden), True);
13501 Next_Elmt (Hidden);
13502 end loop;
13503
13504 else
13505 -- Each body is analyzed separately, and there is no context that
13506 -- needs preserving from one body instance to the next, so remove all
13507 -- parent scopes that have been installed.
13508
13509 while Present (S) loop
13510 End_Package_Scope (S);
13511 Set_Is_Immediately_Visible (S, False);
13512 S := Current_Scope;
13513 exit when S = Standard_Standard;
13514 end loop;
13515 end if;
13516 end Remove_Parent;
13517
13518 -----------------
13519 -- Restore_Env --
13520 -----------------
13521
13522 procedure Restore_Env is
13523 Saved : Instance_Env renames Instance_Envs.Table (Instance_Envs.Last);
13524
13525 begin
13526 if No (Current_Instantiated_Parent.Act_Id) then
13527 -- Restore environment after subprogram inlining
13528
13529 Restore_Private_Views (Empty);
13530 end if;
13531
13532 Current_Instantiated_Parent := Saved.Instantiated_Parent;
13533 Exchanged_Views := Saved.Exchanged_Views;
13534 Hidden_Entities := Saved.Hidden_Entities;
13535 Current_Sem_Unit := Saved.Current_Sem_Unit;
13536 Parent_Unit_Visible := Saved.Parent_Unit_Visible;
13537 Instance_Parent_Unit := Saved.Instance_Parent_Unit;
13538
13539 Restore_Opt_Config_Switches (Saved.Switches);
13540
13541 Instance_Envs.Decrement_Last;
13542 end Restore_Env;
13543
13544 ---------------------------
13545 -- Restore_Private_Views --
13546 ---------------------------
13547
13548 procedure Restore_Private_Views
13549 (Pack_Id : Entity_Id;
13550 Is_Package : Boolean := True)
13551 is
13552 M : Elmt_Id;
13553 E : Entity_Id;
13554 Typ : Entity_Id;
13555 Dep_Elmt : Elmt_Id;
13556 Dep_Typ : Node_Id;
13557
13558 procedure Restore_Nested_Formal (Formal : Entity_Id);
13559 -- Hide the generic formals of formal packages declared with box which
13560 -- were reachable in the current instantiation.
13561
13562 ---------------------------
13563 -- Restore_Nested_Formal --
13564 ---------------------------
13565
13566 procedure Restore_Nested_Formal (Formal : Entity_Id) is
13567 Ent : Entity_Id;
13568
13569 begin
13570 if Present (Renamed_Object (Formal))
13571 and then Denotes_Formal_Package (Renamed_Object (Formal), True)
13572 then
13573 return;
13574
13575 elsif Present (Associated_Formal_Package (Formal)) then
13576 Ent := First_Entity (Formal);
13577 while Present (Ent) loop
13578 exit when Ekind (Ent) = E_Package
13579 and then Renamed_Entity (Ent) = Renamed_Entity (Formal);
13580
13581 Set_Is_Hidden (Ent);
13582 Set_Is_Potentially_Use_Visible (Ent, False);
13583
13584 -- If package, then recurse
13585
13586 if Ekind (Ent) = E_Package then
13587 Restore_Nested_Formal (Ent);
13588 end if;
13589
13590 Next_Entity (Ent);
13591 end loop;
13592 end if;
13593 end Restore_Nested_Formal;
13594
13595 -- Start of processing for Restore_Private_Views
13596
13597 begin
13598 M := First_Elmt (Exchanged_Views);
13599 while Present (M) loop
13600 Typ := Node (M);
13601
13602 -- Subtypes of types whose views have been exchanged, and that are
13603 -- defined within the instance, were not on the Private_Dependents
13604 -- list on entry to the instance, so they have to be exchanged
13605 -- explicitly now, in order to remain consistent with the view of the
13606 -- parent type.
13607
13608 if Ekind_In (Typ, E_Private_Type,
13609 E_Limited_Private_Type,
13610 E_Record_Type_With_Private)
13611 then
13612 Dep_Elmt := First_Elmt (Private_Dependents (Typ));
13613 while Present (Dep_Elmt) loop
13614 Dep_Typ := Node (Dep_Elmt);
13615
13616 if Scope (Dep_Typ) = Pack_Id
13617 and then Present (Full_View (Dep_Typ))
13618 then
13619 Replace_Elmt (Dep_Elmt, Full_View (Dep_Typ));
13620 Exchange_Declarations (Dep_Typ);
13621 end if;
13622
13623 Next_Elmt (Dep_Elmt);
13624 end loop;
13625 end if;
13626
13627 Exchange_Declarations (Node (M));
13628 Next_Elmt (M);
13629 end loop;
13630
13631 if No (Pack_Id) then
13632 return;
13633 end if;
13634
13635 -- Make the generic formal parameters private, and make the formal types
13636 -- into subtypes of the actuals again.
13637
13638 E := First_Entity (Pack_Id);
13639 while Present (E) loop
13640 Set_Is_Hidden (E, True);
13641
13642 if Is_Type (E)
13643 and then Nkind (Parent (E)) = N_Subtype_Declaration
13644 then
13645 -- If the actual for E is itself a generic actual type from
13646 -- an enclosing instance, E is still a generic actual type
13647 -- outside of the current instance. This matter when resolving
13648 -- an overloaded call that may be ambiguous in the enclosing
13649 -- instance, when two of its actuals coincide.
13650
13651 if Is_Entity_Name (Subtype_Indication (Parent (E)))
13652 and then Is_Generic_Actual_Type
13653 (Entity (Subtype_Indication (Parent (E))))
13654 then
13655 null;
13656 else
13657 Set_Is_Generic_Actual_Type (E, False);
13658 end if;
13659
13660 -- An unusual case of aliasing: the actual may also be directly
13661 -- visible in the generic, and be private there, while it is fully
13662 -- visible in the context of the instance. The internal subtype
13663 -- is private in the instance but has full visibility like its
13664 -- parent in the enclosing scope. This enforces the invariant that
13665 -- the privacy status of all private dependents of a type coincide
13666 -- with that of the parent type. This can only happen when a
13667 -- generic child unit is instantiated within a sibling.
13668
13669 if Is_Private_Type (E)
13670 and then not Is_Private_Type (Etype (E))
13671 then
13672 Exchange_Declarations (E);
13673 end if;
13674
13675 elsif Ekind (E) = E_Package then
13676
13677 -- The end of the renaming list is the renaming of the generic
13678 -- package itself. If the instance is a subprogram, all entities
13679 -- in the corresponding package are renamings. If this entity is
13680 -- a formal package, make its own formals private as well. The
13681 -- actual in this case is itself the renaming of an instantiation.
13682 -- If the entity is not a package renaming, it is the entity
13683 -- created to validate formal package actuals: ignore it.
13684
13685 -- If the actual is itself a formal package for the enclosing
13686 -- generic, or the actual for such a formal package, it remains
13687 -- visible on exit from the instance, and therefore nothing needs
13688 -- to be done either, except to keep it accessible.
13689
13690 if Is_Package and then Renamed_Object (E) = Pack_Id then
13691 exit;
13692
13693 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
13694 null;
13695
13696 elsif
13697 Denotes_Formal_Package (Renamed_Object (E), True, Pack_Id)
13698 then
13699 Set_Is_Hidden (E, False);
13700
13701 else
13702 declare
13703 Act_P : constant Entity_Id := Renamed_Object (E);
13704 Id : Entity_Id;
13705
13706 begin
13707 Id := First_Entity (Act_P);
13708 while Present (Id)
13709 and then Id /= First_Private_Entity (Act_P)
13710 loop
13711 exit when Ekind (Id) = E_Package
13712 and then Renamed_Object (Id) = Act_P;
13713
13714 Set_Is_Hidden (Id, True);
13715 Set_Is_Potentially_Use_Visible (Id, In_Use (Act_P));
13716
13717 if Ekind (Id) = E_Package then
13718 Restore_Nested_Formal (Id);
13719 end if;
13720
13721 Next_Entity (Id);
13722 end loop;
13723 end;
13724 end if;
13725 end if;
13726
13727 Next_Entity (E);
13728 end loop;
13729 end Restore_Private_Views;
13730
13731 --------------
13732 -- Save_Env --
13733 --------------
13734
13735 procedure Save_Env
13736 (Gen_Unit : Entity_Id;
13737 Act_Unit : Entity_Id)
13738 is
13739 begin
13740 Init_Env;
13741 Set_Instance_Env (Gen_Unit, Act_Unit);
13742 end Save_Env;
13743
13744 ----------------------------
13745 -- Save_Global_References --
13746 ----------------------------
13747
13748 procedure Save_Global_References (Templ : Node_Id) is
13749
13750 -- ??? it is horrible to use global variables in highly recursive code
13751
13752 E : Entity_Id;
13753 -- The entity of the current associated node
13754
13755 Gen_Scope : Entity_Id;
13756 -- The scope of the generic for which references are being saved
13757
13758 N2 : Node_Id;
13759 -- The current associated node
13760
13761 function Is_Global (E : Entity_Id) return Boolean;
13762 -- Check whether entity is defined outside of generic unit. Examine the
13763 -- scope of an entity, and the scope of the scope, etc, until we find
13764 -- either Standard, in which case the entity is global, or the generic
13765 -- unit itself, which indicates that the entity is local. If the entity
13766 -- is the generic unit itself, as in the case of a recursive call, or
13767 -- the enclosing generic unit, if different from the current scope, then
13768 -- it is local as well, because it will be replaced at the point of
13769 -- instantiation. On the other hand, if it is a reference to a child
13770 -- unit of a common ancestor, which appears in an instantiation, it is
13771 -- global because it is used to denote a specific compilation unit at
13772 -- the time the instantiations will be analyzed.
13773
13774 procedure Reset_Entity (N : Node_Id);
13775 -- Save semantic information on global entity so that it is not resolved
13776 -- again at instantiation time.
13777
13778 procedure Save_Entity_Descendants (N : Node_Id);
13779 -- Apply Save_Global_References to the two syntactic descendants of
13780 -- non-terminal nodes that carry an Associated_Node and are processed
13781 -- through Reset_Entity. Once the global entity (if any) has been
13782 -- captured together with its type, only two syntactic descendants need
13783 -- to be traversed to complete the processing of the tree rooted at N.
13784 -- This applies to Selected_Components, Expanded_Names, and to Operator
13785 -- nodes. N can also be a character literal, identifier, or operator
13786 -- symbol node, but the call has no effect in these cases.
13787
13788 procedure Save_Global_Defaults (N1 : Node_Id; N2 : Node_Id);
13789 -- Default actuals in nested instances must be handled specially
13790 -- because there is no link to them from the original tree. When an
13791 -- actual subprogram is given by a default, we add an explicit generic
13792 -- association for it in the instantiation node. When we save the
13793 -- global references on the name of the instance, we recover the list
13794 -- of generic associations, and add an explicit one to the original
13795 -- generic tree, through which a global actual can be preserved.
13796 -- Similarly, if a child unit is instantiated within a sibling, in the
13797 -- context of the parent, we must preserve the identifier of the parent
13798 -- so that it can be properly resolved in a subsequent instantiation.
13799
13800 procedure Save_Global_Descendant (D : Union_Id);
13801 -- Apply Save_References recursively to the descendents of node D
13802
13803 procedure Save_References (N : Node_Id);
13804 -- This is the recursive procedure that does the work, once the
13805 -- enclosing generic scope has been established.
13806
13807 ---------------
13808 -- Is_Global --
13809 ---------------
13810
13811 function Is_Global (E : Entity_Id) return Boolean is
13812 Se : Entity_Id;
13813
13814 function Is_Instance_Node (Decl : Node_Id) return Boolean;
13815 -- Determine whether the parent node of a reference to a child unit
13816 -- denotes an instantiation or a formal package, in which case the
13817 -- reference to the child unit is global, even if it appears within
13818 -- the current scope (e.g. when the instance appears within the body
13819 -- of an ancestor).
13820
13821 ----------------------
13822 -- Is_Instance_Node --
13823 ----------------------
13824
13825 function Is_Instance_Node (Decl : Node_Id) return Boolean is
13826 begin
13827 return Nkind (Decl) in N_Generic_Instantiation
13828 or else
13829 Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration;
13830 end Is_Instance_Node;
13831
13832 -- Start of processing for Is_Global
13833
13834 begin
13835 if E = Gen_Scope then
13836 return False;
13837
13838 elsif E = Standard_Standard then
13839 return True;
13840
13841 elsif Is_Child_Unit (E)
13842 and then (Is_Instance_Node (Parent (N2))
13843 or else (Nkind (Parent (N2)) = N_Expanded_Name
13844 and then N2 = Selector_Name (Parent (N2))
13845 and then
13846 Is_Instance_Node (Parent (Parent (N2)))))
13847 then
13848 return True;
13849
13850 else
13851 Se := Scope (E);
13852 while Se /= Gen_Scope loop
13853 if Se = Standard_Standard then
13854 return True;
13855 else
13856 Se := Scope (Se);
13857 end if;
13858 end loop;
13859
13860 return False;
13861 end if;
13862 end Is_Global;
13863
13864 ------------------
13865 -- Reset_Entity --
13866 ------------------
13867
13868 procedure Reset_Entity (N : Node_Id) is
13869 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id);
13870 -- If the type of N2 is global to the generic unit, save the type in
13871 -- the generic node. Just as we perform name capture for explicit
13872 -- references within the generic, we must capture the global types
13873 -- of local entities because they may participate in resolution in
13874 -- the instance.
13875
13876 function Top_Ancestor (E : Entity_Id) return Entity_Id;
13877 -- Find the ultimate ancestor of the current unit. If it is not a
13878 -- generic unit, then the name of the current unit in the prefix of
13879 -- an expanded name must be replaced with its generic homonym to
13880 -- ensure that it will be properly resolved in an instance.
13881
13882 ---------------------
13883 -- Set_Global_Type --
13884 ---------------------
13885
13886 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id) is
13887 Typ : constant Entity_Id := Etype (N2);
13888
13889 begin
13890 Set_Etype (N, Typ);
13891
13892 -- If the entity of N is not the associated node, this is a
13893 -- nested generic and it has an associated node as well, whose
13894 -- type is already the full view (see below). Indicate that the
13895 -- original node has a private view.
13896
13897 if Entity (N) /= N2 and then Has_Private_View (Entity (N)) then
13898 Set_Has_Private_View (N);
13899 end if;
13900
13901 -- If not a private type, nothing else to do
13902
13903 if not Is_Private_Type (Typ) then
13904 if Is_Array_Type (Typ)
13905 and then Is_Private_Type (Component_Type (Typ))
13906 then
13907 Set_Has_Private_View (N);
13908 end if;
13909
13910 -- If it is a derivation of a private type in a context where no
13911 -- full view is needed, nothing to do either.
13912
13913 elsif No (Full_View (Typ)) and then Typ /= Etype (Typ) then
13914 null;
13915
13916 -- Otherwise mark the type for flipping and use the full view when
13917 -- available.
13918
13919 else
13920 Set_Has_Private_View (N);
13921
13922 if Present (Full_View (Typ)) then
13923 Set_Etype (N2, Full_View (Typ));
13924 end if;
13925 end if;
13926 end Set_Global_Type;
13927
13928 ------------------
13929 -- Top_Ancestor --
13930 ------------------
13931
13932 function Top_Ancestor (E : Entity_Id) return Entity_Id is
13933 Par : Entity_Id;
13934
13935 begin
13936 Par := E;
13937 while Is_Child_Unit (Par) loop
13938 Par := Scope (Par);
13939 end loop;
13940
13941 return Par;
13942 end Top_Ancestor;
13943
13944 -- Start of processing for Reset_Entity
13945
13946 begin
13947 N2 := Get_Associated_Node (N);
13948 E := Entity (N2);
13949
13950 if Present (E) then
13951
13952 -- If the node is an entry call to an entry in an enclosing task,
13953 -- it is rewritten as a selected component. No global entity to
13954 -- preserve in this case, since the expansion will be redone in
13955 -- the instance.
13956
13957 if not Nkind_In (E, N_Defining_Character_Literal,
13958 N_Defining_Identifier,
13959 N_Defining_Operator_Symbol)
13960 then
13961 Set_Associated_Node (N, Empty);
13962 Set_Etype (N, Empty);
13963 return;
13964 end if;
13965
13966 -- If the entity is an itype created as a subtype of an access
13967 -- type with a null exclusion restore source entity for proper
13968 -- visibility. The itype will be created anew in the instance.
13969
13970 if Is_Itype (E)
13971 and then Ekind (E) = E_Access_Subtype
13972 and then Is_Entity_Name (N)
13973 and then Chars (Etype (E)) = Chars (N)
13974 then
13975 E := Etype (E);
13976 Set_Entity (N2, E);
13977 Set_Etype (N2, E);
13978 end if;
13979
13980 if Is_Global (E) then
13981
13982 -- If the entity is a package renaming that is the prefix of
13983 -- an expanded name, it has been rewritten as the renamed
13984 -- package, which is necessary semantically but complicates
13985 -- ASIS tree traversal, so we recover the original entity to
13986 -- expose the renaming. Take into account that the context may
13987 -- be a nested generic, that the original node may itself have
13988 -- an associated node that had better be an entity, and that
13989 -- the current node is still a selected component.
13990
13991 if Ekind (E) = E_Package
13992 and then Nkind (N) = N_Selected_Component
13993 and then Nkind (Parent (N)) = N_Expanded_Name
13994 and then Present (Original_Node (N2))
13995 and then Is_Entity_Name (Original_Node (N2))
13996 and then Present (Entity (Original_Node (N2)))
13997 then
13998 if Is_Global (Entity (Original_Node (N2))) then
13999 N2 := Original_Node (N2);
14000 Set_Associated_Node (N, N2);
14001 Set_Global_Type (N, N2);
14002
14003 -- Renaming is local, and will be resolved in instance
14004
14005 else
14006 Set_Associated_Node (N, Empty);
14007 Set_Etype (N, Empty);
14008 end if;
14009
14010 else
14011 Set_Global_Type (N, N2);
14012 end if;
14013
14014 elsif Nkind (N) = N_Op_Concat
14015 and then Is_Generic_Type (Etype (N2))
14016 and then (Base_Type (Etype (Right_Opnd (N2))) = Etype (N2)
14017 or else
14018 Base_Type (Etype (Left_Opnd (N2))) = Etype (N2))
14019 and then Is_Intrinsic_Subprogram (E)
14020 then
14021 null;
14022
14023 -- Entity is local. Mark generic node as unresolved. Note that now
14024 -- it does not have an entity.
14025
14026 else
14027 Set_Associated_Node (N, Empty);
14028 Set_Etype (N, Empty);
14029 end if;
14030
14031 if Nkind (Parent (N)) in N_Generic_Instantiation
14032 and then N = Name (Parent (N))
14033 then
14034 Save_Global_Defaults (Parent (N), Parent (N2));
14035 end if;
14036
14037 elsif Nkind (Parent (N)) = N_Selected_Component
14038 and then Nkind (Parent (N2)) = N_Expanded_Name
14039 then
14040 if Is_Global (Entity (Parent (N2))) then
14041 Change_Selected_Component_To_Expanded_Name (Parent (N));
14042 Set_Associated_Node (Parent (N), Parent (N2));
14043 Set_Global_Type (Parent (N), Parent (N2));
14044 Save_Entity_Descendants (N);
14045
14046 -- If this is a reference to the current generic entity, replace
14047 -- by the name of the generic homonym of the current package. This
14048 -- is because in an instantiation Par.P.Q will not resolve to the
14049 -- name of the instance, whose enclosing scope is not necessarily
14050 -- Par. We use the generic homonym rather that the name of the
14051 -- generic itself because it may be hidden by a local declaration.
14052
14053 elsif In_Open_Scopes (Entity (Parent (N2)))
14054 and then not
14055 Is_Generic_Unit (Top_Ancestor (Entity (Prefix (Parent (N2)))))
14056 then
14057 if Ekind (Entity (Parent (N2))) = E_Generic_Package then
14058 Rewrite (Parent (N),
14059 Make_Identifier (Sloc (N),
14060 Chars =>
14061 Chars (Generic_Homonym (Entity (Parent (N2))))));
14062 else
14063 Rewrite (Parent (N),
14064 Make_Identifier (Sloc (N),
14065 Chars => Chars (Selector_Name (Parent (N2)))));
14066 end if;
14067 end if;
14068
14069 if Nkind (Parent (Parent (N))) in N_Generic_Instantiation
14070 and then Parent (N) = Name (Parent (Parent (N)))
14071 then
14072 Save_Global_Defaults
14073 (Parent (Parent (N)), Parent (Parent (N2)));
14074 end if;
14075
14076 -- A selected component may denote a static constant that has been
14077 -- folded. If the static constant is global to the generic, capture
14078 -- its value. Otherwise the folding will happen in any instantiation.
14079
14080 elsif Nkind (Parent (N)) = N_Selected_Component
14081 and then Nkind_In (Parent (N2), N_Integer_Literal, N_Real_Literal)
14082 then
14083 if Present (Entity (Original_Node (Parent (N2))))
14084 and then Is_Global (Entity (Original_Node (Parent (N2))))
14085 then
14086 Rewrite (Parent (N), New_Copy (Parent (N2)));
14087 Set_Analyzed (Parent (N), False);
14088 end if;
14089
14090 -- A selected component may be transformed into a parameterless
14091 -- function call. If the called entity is global, rewrite the node
14092 -- appropriately, i.e. as an extended name for the global entity.
14093
14094 elsif Nkind (Parent (N)) = N_Selected_Component
14095 and then Nkind (Parent (N2)) = N_Function_Call
14096 and then N = Selector_Name (Parent (N))
14097 then
14098 if No (Parameter_Associations (Parent (N2))) then
14099 if Is_Global (Entity (Name (Parent (N2)))) then
14100 Change_Selected_Component_To_Expanded_Name (Parent (N));
14101 Set_Associated_Node (Parent (N), Name (Parent (N2)));
14102 Set_Global_Type (Parent (N), Name (Parent (N2)));
14103 Save_Entity_Descendants (N);
14104
14105 else
14106 Set_Is_Prefixed_Call (Parent (N));
14107 Set_Associated_Node (N, Empty);
14108 Set_Etype (N, Empty);
14109 end if;
14110
14111 -- In Ada 2005, X.F may be a call to a primitive operation,
14112 -- rewritten as F (X). This rewriting will be done again in an
14113 -- instance, so keep the original node. Global entities will be
14114 -- captured as for other constructs. Indicate that this must
14115 -- resolve as a call, to prevent accidental overloading in the
14116 -- instance, if both a component and a primitive operation appear
14117 -- as candidates.
14118
14119 else
14120 Set_Is_Prefixed_Call (Parent (N));
14121 end if;
14122
14123 -- Entity is local. Reset in generic unit, so that node is resolved
14124 -- anew at the point of instantiation.
14125
14126 else
14127 Set_Associated_Node (N, Empty);
14128 Set_Etype (N, Empty);
14129 end if;
14130 end Reset_Entity;
14131
14132 -----------------------------
14133 -- Save_Entity_Descendants --
14134 -----------------------------
14135
14136 procedure Save_Entity_Descendants (N : Node_Id) is
14137 begin
14138 case Nkind (N) is
14139 when N_Binary_Op =>
14140 Save_Global_Descendant (Union_Id (Left_Opnd (N)));
14141 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
14142
14143 when N_Unary_Op =>
14144 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
14145
14146 when N_Expanded_Name |
14147 N_Selected_Component =>
14148 Save_Global_Descendant (Union_Id (Prefix (N)));
14149 Save_Global_Descendant (Union_Id (Selector_Name (N)));
14150
14151 when N_Identifier |
14152 N_Character_Literal |
14153 N_Operator_Symbol =>
14154 null;
14155
14156 when others =>
14157 raise Program_Error;
14158 end case;
14159 end Save_Entity_Descendants;
14160
14161 --------------------------
14162 -- Save_Global_Defaults --
14163 --------------------------
14164
14165 procedure Save_Global_Defaults (N1 : Node_Id; N2 : Node_Id) is
14166 Loc : constant Source_Ptr := Sloc (N1);
14167 Assoc2 : constant List_Id := Generic_Associations (N2);
14168 Gen_Id : constant Entity_Id := Get_Generic_Entity (N2);
14169 Assoc1 : List_Id;
14170 Act1 : Node_Id;
14171 Act2 : Node_Id;
14172 Def : Node_Id;
14173 Ndec : Node_Id;
14174 Subp : Entity_Id;
14175 Actual : Entity_Id;
14176
14177 begin
14178 Assoc1 := Generic_Associations (N1);
14179
14180 if Present (Assoc1) then
14181 Act1 := First (Assoc1);
14182 else
14183 Act1 := Empty;
14184 Set_Generic_Associations (N1, New_List);
14185 Assoc1 := Generic_Associations (N1);
14186 end if;
14187
14188 if Present (Assoc2) then
14189 Act2 := First (Assoc2);
14190 else
14191 return;
14192 end if;
14193
14194 while Present (Act1) and then Present (Act2) loop
14195 Next (Act1);
14196 Next (Act2);
14197 end loop;
14198
14199 -- Find the associations added for default subprograms
14200
14201 if Present (Act2) then
14202 while Nkind (Act2) /= N_Generic_Association
14203 or else No (Entity (Selector_Name (Act2)))
14204 or else not Is_Overloadable (Entity (Selector_Name (Act2)))
14205 loop
14206 Next (Act2);
14207 end loop;
14208
14209 -- Add a similar association if the default is global. The
14210 -- renaming declaration for the actual has been analyzed, and
14211 -- its alias is the program it renames. Link the actual in the
14212 -- original generic tree with the node in the analyzed tree.
14213
14214 while Present (Act2) loop
14215 Subp := Entity (Selector_Name (Act2));
14216 Def := Explicit_Generic_Actual_Parameter (Act2);
14217
14218 -- Following test is defence against rubbish errors
14219
14220 if No (Alias (Subp)) then
14221 return;
14222 end if;
14223
14224 -- Retrieve the resolved actual from the renaming declaration
14225 -- created for the instantiated formal.
14226
14227 Actual := Entity (Name (Parent (Parent (Subp))));
14228 Set_Entity (Def, Actual);
14229 Set_Etype (Def, Etype (Actual));
14230
14231 if Is_Global (Actual) then
14232 Ndec :=
14233 Make_Generic_Association (Loc,
14234 Selector_Name =>
14235 New_Occurrence_Of (Subp, Loc),
14236 Explicit_Generic_Actual_Parameter =>
14237 New_Occurrence_Of (Actual, Loc));
14238
14239 Set_Associated_Node
14240 (Explicit_Generic_Actual_Parameter (Ndec), Def);
14241
14242 Append (Ndec, Assoc1);
14243
14244 -- If there are other defaults, add a dummy association in case
14245 -- there are other defaulted formals with the same name.
14246
14247 elsif Present (Next (Act2)) then
14248 Ndec :=
14249 Make_Generic_Association (Loc,
14250 Selector_Name =>
14251 New_Occurrence_Of (Subp, Loc),
14252 Explicit_Generic_Actual_Parameter => Empty);
14253
14254 Append (Ndec, Assoc1);
14255 end if;
14256
14257 Next (Act2);
14258 end loop;
14259 end if;
14260
14261 if Nkind (Name (N1)) = N_Identifier
14262 and then Is_Child_Unit (Gen_Id)
14263 and then Is_Global (Gen_Id)
14264 and then Is_Generic_Unit (Scope (Gen_Id))
14265 and then In_Open_Scopes (Scope (Gen_Id))
14266 then
14267 -- This is an instantiation of a child unit within a sibling, so
14268 -- that the generic parent is in scope. An eventual instance must
14269 -- occur within the scope of an instance of the parent. Make name
14270 -- in instance into an expanded name, to preserve the identifier
14271 -- of the parent, so it can be resolved subsequently.
14272
14273 Rewrite (Name (N2),
14274 Make_Expanded_Name (Loc,
14275 Chars => Chars (Gen_Id),
14276 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
14277 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
14278 Set_Entity (Name (N2), Gen_Id);
14279
14280 Rewrite (Name (N1),
14281 Make_Expanded_Name (Loc,
14282 Chars => Chars (Gen_Id),
14283 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
14284 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
14285
14286 Set_Associated_Node (Name (N1), Name (N2));
14287 Set_Associated_Node (Prefix (Name (N1)), Empty);
14288 Set_Associated_Node
14289 (Selector_Name (Name (N1)), Selector_Name (Name (N2)));
14290 Set_Etype (Name (N1), Etype (Gen_Id));
14291 end if;
14292 end Save_Global_Defaults;
14293
14294 ----------------------------
14295 -- Save_Global_Descendant --
14296 ----------------------------
14297
14298 procedure Save_Global_Descendant (D : Union_Id) is
14299 N1 : Node_Id;
14300
14301 begin
14302 if D in Node_Range then
14303 if D = Union_Id (Empty) then
14304 null;
14305
14306 elsif Nkind (Node_Id (D)) /= N_Compilation_Unit then
14307 Save_References (Node_Id (D));
14308 end if;
14309
14310 elsif D in List_Range then
14311 if D = Union_Id (No_List) or else Is_Empty_List (List_Id (D)) then
14312 null;
14313
14314 else
14315 N1 := First (List_Id (D));
14316 while Present (N1) loop
14317 Save_References (N1);
14318 Next (N1);
14319 end loop;
14320 end if;
14321
14322 -- Element list or other non-node field, nothing to do
14323
14324 else
14325 null;
14326 end if;
14327 end Save_Global_Descendant;
14328
14329 ---------------------
14330 -- Save_References --
14331 ---------------------
14332
14333 -- This is the recursive procedure that does the work once the enclosing
14334 -- generic scope has been established. We have to treat specially a
14335 -- number of node rewritings that are required by semantic processing
14336 -- and which change the kind of nodes in the generic copy: typically
14337 -- constant-folding, replacing an operator node by a string literal, or
14338 -- a selected component by an expanded name. In each of those cases, the
14339 -- transformation is propagated to the generic unit.
14340
14341 procedure Save_References (N : Node_Id) is
14342 Loc : constant Source_Ptr := Sloc (N);
14343
14344 function Requires_Delayed_Save (Nod : Node_Id) return Boolean;
14345 -- Determine whether arbitrary node Nod requires delayed capture of
14346 -- global references within its aspect specifications.
14347
14348 procedure Save_References_In_Aggregate (N : Node_Id);
14349 -- Save all global references in [extension] aggregate node N
14350
14351 procedure Save_References_In_Char_Lit_Or_Op_Symbol (N : Node_Id);
14352 -- Save all global references in a character literal or operator
14353 -- symbol denoted by N.
14354
14355 procedure Save_References_In_Descendants (N : Node_Id);
14356 -- Save all global references in all descendants of node N
14357
14358 procedure Save_References_In_Identifier (N : Node_Id);
14359 -- Save all global references in identifier node N
14360
14361 procedure Save_References_In_Operator (N : Node_Id);
14362 -- Save all global references in operator node N
14363
14364 procedure Save_References_In_Pragma (Prag : Node_Id);
14365 -- Save all global references found within the expression of pragma
14366 -- Prag.
14367
14368 ---------------------------
14369 -- Requires_Delayed_Save --
14370 ---------------------------
14371
14372 function Requires_Delayed_Save (Nod : Node_Id) return Boolean is
14373 begin
14374 -- Generic packages and subprograms require delayed capture of
14375 -- global references within their aspects due to the timing of
14376 -- annotation analysis.
14377
14378 if Nkind_In (Nod, N_Generic_Package_Declaration,
14379 N_Generic_Subprogram_Declaration,
14380 N_Package_Body,
14381 N_Package_Body_Stub,
14382 N_Subprogram_Body,
14383 N_Subprogram_Body_Stub)
14384 then
14385 -- Since the capture of global references is done on the
14386 -- unanalyzed generic template, there is no information around
14387 -- to infer the context. Use the Associated_Entity linkages to
14388 -- peek into the analyzed generic copy and determine what the
14389 -- template corresponds to.
14390
14391 if Nod = Templ then
14392 return
14393 Is_Generic_Declaration_Or_Body
14394 (Unit_Declaration_Node
14395 (Associated_Entity (Defining_Entity (Nod))));
14396
14397 -- Otherwise the generic unit being processed is not the top
14398 -- level template. It is safe to capture of global references
14399 -- within the generic unit because at this point the top level
14400 -- copy is fully analyzed.
14401
14402 else
14403 return False;
14404 end if;
14405
14406 -- Otherwise capture the global references without interference
14407
14408 else
14409 return False;
14410 end if;
14411 end Requires_Delayed_Save;
14412
14413 ----------------------------------
14414 -- Save_References_In_Aggregate --
14415 ----------------------------------
14416
14417 procedure Save_References_In_Aggregate (N : Node_Id) is
14418 Nam : Node_Id;
14419 Qual : Node_Id := Empty;
14420 Typ : Entity_Id := Empty;
14421
14422 use Atree.Unchecked_Access;
14423 -- This code section is part of implementing an untyped tree
14424 -- traversal, so it needs direct access to node fields.
14425
14426 begin
14427 N2 := Get_Associated_Node (N);
14428
14429 if Present (N2) then
14430 Typ := Etype (N2);
14431
14432 -- In an instance within a generic, use the name of the actual
14433 -- and not the original generic parameter. If the actual is
14434 -- global in the current generic it must be preserved for its
14435 -- instantiation.
14436
14437 if Nkind (Parent (Typ)) = N_Subtype_Declaration
14438 and then Present (Generic_Parent_Type (Parent (Typ)))
14439 then
14440 Typ := Base_Type (Typ);
14441 Set_Etype (N2, Typ);
14442 end if;
14443 end if;
14444
14445 if No (N2) or else No (Typ) or else not Is_Global (Typ) then
14446 Set_Associated_Node (N, Empty);
14447
14448 -- If the aggregate is an actual in a call, it has been
14449 -- resolved in the current context, to some local type. The
14450 -- enclosing call may have been disambiguated by the aggregate,
14451 -- and this disambiguation might fail at instantiation time
14452 -- because the type to which the aggregate did resolve is not
14453 -- preserved. In order to preserve some of this information,
14454 -- wrap the aggregate in a qualified expression, using the id
14455 -- of its type. For further disambiguation we qualify the type
14456 -- name with its scope (if visible) because both id's will have
14457 -- corresponding entities in an instance. This resolves most of
14458 -- the problems with missing type information on aggregates in
14459 -- instances.
14460
14461 if Present (N2)
14462 and then Nkind (N2) = Nkind (N)
14463 and then Nkind (Parent (N2)) in N_Subprogram_Call
14464 and then Present (Typ)
14465 and then Comes_From_Source (Typ)
14466 then
14467 Nam := Make_Identifier (Loc, Chars (Typ));
14468
14469 if Is_Immediately_Visible (Scope (Typ)) then
14470 Nam :=
14471 Make_Selected_Component (Loc,
14472 Prefix =>
14473 Make_Identifier (Loc, Chars (Scope (Typ))),
14474 Selector_Name => Nam);
14475 end if;
14476
14477 Qual :=
14478 Make_Qualified_Expression (Loc,
14479 Subtype_Mark => Nam,
14480 Expression => Relocate_Node (N));
14481 end if;
14482 end if;
14483
14484 Save_Global_Descendant (Field1 (N));
14485 Save_Global_Descendant (Field2 (N));
14486 Save_Global_Descendant (Field3 (N));
14487 Save_Global_Descendant (Field5 (N));
14488
14489 if Present (Qual) then
14490 Rewrite (N, Qual);
14491 end if;
14492 end Save_References_In_Aggregate;
14493
14494 ----------------------------------------------
14495 -- Save_References_In_Char_Lit_Or_Op_Symbol --
14496 ----------------------------------------------
14497
14498 procedure Save_References_In_Char_Lit_Or_Op_Symbol (N : Node_Id) is
14499 begin
14500 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
14501 Reset_Entity (N);
14502
14503 elsif Nkind (N) = N_Operator_Symbol
14504 and then Nkind (Get_Associated_Node (N)) = N_String_Literal
14505 then
14506 Change_Operator_Symbol_To_String_Literal (N);
14507 end if;
14508 end Save_References_In_Char_Lit_Or_Op_Symbol;
14509
14510 ------------------------------------
14511 -- Save_References_In_Descendants --
14512 ------------------------------------
14513
14514 procedure Save_References_In_Descendants (N : Node_Id) is
14515 use Atree.Unchecked_Access;
14516 -- This code section is part of implementing an untyped tree
14517 -- traversal, so it needs direct access to node fields.
14518
14519 begin
14520 Save_Global_Descendant (Field1 (N));
14521 Save_Global_Descendant (Field2 (N));
14522 Save_Global_Descendant (Field3 (N));
14523 Save_Global_Descendant (Field4 (N));
14524 Save_Global_Descendant (Field5 (N));
14525 end Save_References_In_Descendants;
14526
14527 -----------------------------------
14528 -- Save_References_In_Identifier --
14529 -----------------------------------
14530
14531 procedure Save_References_In_Identifier (N : Node_Id) is
14532 begin
14533 -- The node did not undergo a transformation
14534
14535 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
14536
14537 -- If this is a discriminant reference, always save it. It is
14538 -- used in the instance to find the corresponding discriminant
14539 -- positionally rather than by name.
14540
14541 Set_Original_Discriminant
14542 (N, Original_Discriminant (Get_Associated_Node (N)));
14543 Reset_Entity (N);
14544
14545 -- The analysis of the generic copy transformed the identifier
14546 -- into another construct. Propagate the changes to the template.
14547
14548 else
14549 N2 := Get_Associated_Node (N);
14550
14551 -- The identifier denotes a call to a parameterless function.
14552 -- Mark the node as resolved when the function is external.
14553
14554 if Nkind (N2) = N_Function_Call then
14555 E := Entity (Name (N2));
14556
14557 if Present (E) and then Is_Global (E) then
14558 Set_Etype (N, Etype (N2));
14559 else
14560 Set_Associated_Node (N, Empty);
14561 Set_Etype (N, Empty);
14562 end if;
14563
14564 -- The identifier denotes a named number that was constant
14565 -- folded. Preserve the original name for ASIS and undo the
14566 -- constant folding which will be repeated in the instance.
14567
14568 elsif Nkind_In (N2, N_Integer_Literal, N_Real_Literal)
14569 and then Is_Entity_Name (Original_Node (N2))
14570 then
14571 Set_Associated_Node (N, Original_Node (N2));
14572 Reset_Entity (N);
14573
14574 -- The identifier resolved to a string literal. Propagate this
14575 -- information to the generic template.
14576
14577 elsif Nkind (N2) = N_String_Literal then
14578 Rewrite (N, New_Copy (N2));
14579
14580 -- The identifier is rewritten as a dereference if it is the
14581 -- prefix of an implicit dereference. Preserve the original
14582 -- tree as the analysis of the instance will expand the node
14583 -- again, but preserve the resolved entity if it is global.
14584
14585 elsif Nkind (N2) = N_Explicit_Dereference then
14586 if Is_Entity_Name (Prefix (N2))
14587 and then Present (Entity (Prefix (N2)))
14588 and then Is_Global (Entity (Prefix (N2)))
14589 then
14590 Set_Associated_Node (N, Prefix (N2));
14591
14592 elsif Nkind (Prefix (N2)) = N_Function_Call
14593 and then Present (Entity (Name (Prefix (N2))))
14594 and then Is_Global (Entity (Name (Prefix (N2))))
14595 then
14596 Rewrite (N,
14597 Make_Explicit_Dereference (Loc,
14598 Prefix =>
14599 Make_Function_Call (Loc,
14600 Name =>
14601 New_Occurrence_Of
14602 (Entity (Name (Prefix (N2))), Loc))));
14603
14604 else
14605 Set_Associated_Node (N, Empty);
14606 Set_Etype (N, Empty);
14607 end if;
14608
14609 -- The subtype mark of a nominally unconstrained object is
14610 -- rewritten as a subtype indication using the bounds of the
14611 -- expression. Recover the original subtype mark.
14612
14613 elsif Nkind (N2) = N_Subtype_Indication
14614 and then Is_Entity_Name (Original_Node (N2))
14615 then
14616 Set_Associated_Node (N, Original_Node (N2));
14617 Reset_Entity (N);
14618 end if;
14619 end if;
14620 end Save_References_In_Identifier;
14621
14622 ---------------------------------
14623 -- Save_References_In_Operator --
14624 ---------------------------------
14625
14626 procedure Save_References_In_Operator (N : Node_Id) is
14627 begin
14628 -- The node did not undergo a transformation
14629
14630 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
14631 if Nkind (N) = N_Op_Concat then
14632 Set_Is_Component_Left_Opnd (N,
14633 Is_Component_Left_Opnd (Get_Associated_Node (N)));
14634
14635 Set_Is_Component_Right_Opnd (N,
14636 Is_Component_Right_Opnd (Get_Associated_Node (N)));
14637 end if;
14638
14639 Reset_Entity (N);
14640
14641 -- The analysis of the generic copy transformed the operator into
14642 -- some other construct. Propagate the changes to the template.
14643
14644 else
14645 N2 := Get_Associated_Node (N);
14646
14647 -- The operator resoved to a function call
14648
14649 if Nkind (N2) = N_Function_Call then
14650 E := Entity (Name (N2));
14651
14652 if Present (E) and then Is_Global (E) then
14653 Set_Etype (N, Etype (N2));
14654 else
14655 Set_Associated_Node (N, Empty);
14656 Set_Etype (N, Empty);
14657 end if;
14658
14659 -- The operator was folded into a literal
14660
14661 elsif Nkind_In (N2, N_Integer_Literal,
14662 N_Real_Literal,
14663 N_String_Literal)
14664 then
14665 if Present (Original_Node (N2))
14666 and then Nkind (Original_Node (N2)) = Nkind (N)
14667 then
14668 -- Operation was constant-folded. Whenever possible,
14669 -- recover semantic information from unfolded node,
14670 -- for ASIS use.
14671
14672 Set_Associated_Node (N, Original_Node (N2));
14673
14674 if Nkind (N) = N_Op_Concat then
14675 Set_Is_Component_Left_Opnd (N,
14676 Is_Component_Left_Opnd (Get_Associated_Node (N)));
14677 Set_Is_Component_Right_Opnd (N,
14678 Is_Component_Right_Opnd (Get_Associated_Node (N)));
14679 end if;
14680
14681 Reset_Entity (N);
14682
14683 -- Propagate the constant folding back to the template
14684
14685 else
14686 Rewrite (N, New_Copy (N2));
14687 Set_Analyzed (N, False);
14688 end if;
14689
14690 -- The operator was folded into an enumeration literal. Retain
14691 -- the entity to avoid spurious ambiguities if it is overloaded
14692 -- at the point of instantiation or inlining.
14693
14694 elsif Nkind (N2) = N_Identifier
14695 and then Ekind (Entity (N2)) = E_Enumeration_Literal
14696 then
14697 Rewrite (N, New_Copy (N2));
14698 Set_Analyzed (N, False);
14699 end if;
14700 end if;
14701
14702 -- Complete the operands check if node has not been constant
14703 -- folded.
14704
14705 if Nkind (N) in N_Op then
14706 Save_Entity_Descendants (N);
14707 end if;
14708 end Save_References_In_Operator;
14709
14710 -------------------------------
14711 -- Save_References_In_Pragma --
14712 -------------------------------
14713
14714 procedure Save_References_In_Pragma (Prag : Node_Id) is
14715 Context : Node_Id;
14716 Do_Save : Boolean := True;
14717
14718 use Atree.Unchecked_Access;
14719 -- This code section is part of implementing an untyped tree
14720 -- traversal, so it needs direct access to node fields.
14721
14722 begin
14723 -- Do not save global references in pragmas generated from aspects
14724 -- because the pragmas will be regenerated at instantiation time.
14725
14726 if From_Aspect_Specification (Prag) then
14727 Do_Save := False;
14728
14729 -- The capture of global references within contract-related source
14730 -- pragmas associated with generic packages, subprograms or their
14731 -- respective bodies must be delayed due to timing of annotation
14732 -- analysis. Global references are still captured in routine
14733 -- Save_Global_References_In_Contract.
14734
14735 elsif Is_Generic_Contract_Pragma (Prag) and then Prag /= Templ then
14736 if Is_Package_Contract_Annotation (Prag) then
14737 Context := Find_Related_Package_Or_Body (Prag);
14738
14739 else
14740 pragma Assert (Is_Subprogram_Contract_Annotation (Prag));
14741 Context := Find_Related_Subprogram_Or_Body (Prag);
14742 end if;
14743
14744 -- The use of Original_Node accounts for the case when the
14745 -- related context is generic template.
14746
14747 if Requires_Delayed_Save (Original_Node (Context)) then
14748 Do_Save := False;
14749 end if;
14750 end if;
14751
14752 -- For all other cases, save all global references within the
14753 -- descendants, but skip the following semantic fields:
14754
14755 -- Field1 - Next_Pragma
14756 -- Field3 - Corresponding_Aspect
14757 -- Field5 - Next_Rep_Item
14758
14759 if Do_Save then
14760 Save_Global_Descendant (Field2 (Prag));
14761 Save_Global_Descendant (Field4 (Prag));
14762 end if;
14763 end Save_References_In_Pragma;
14764
14765 -- Start of processing for Save_References
14766
14767 begin
14768 if N = Empty then
14769 null;
14770
14771 -- Aggregates
14772
14773 elsif Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
14774 Save_References_In_Aggregate (N);
14775
14776 -- Character literals, operator symbols
14777
14778 elsif Nkind_In (N, N_Character_Literal, N_Operator_Symbol) then
14779 Save_References_In_Char_Lit_Or_Op_Symbol (N);
14780
14781 -- Defining identifiers
14782
14783 elsif Nkind (N) in N_Entity then
14784 null;
14785
14786 -- Identifiers
14787
14788 elsif Nkind (N) = N_Identifier then
14789 Save_References_In_Identifier (N);
14790
14791 -- Operators
14792
14793 elsif Nkind (N) in N_Op then
14794 Save_References_In_Operator (N);
14795
14796 -- Pragmas
14797
14798 elsif Nkind (N) = N_Pragma then
14799 Save_References_In_Pragma (N);
14800
14801 else
14802 Save_References_In_Descendants (N);
14803 end if;
14804
14805 -- Save all global references found within the aspect specifications
14806 -- of the related node.
14807
14808 if Permits_Aspect_Specifications (N) and then Has_Aspects (N) then
14809
14810 -- The capture of global references within aspects associated with
14811 -- generic packages, subprograms or their bodies must be delayed
14812 -- due to timing of annotation analysis. Global references are
14813 -- still captured in routine Save_Global_References_In_Contract.
14814
14815 if Requires_Delayed_Save (N) then
14816 null;
14817
14818 -- Otherwise save all global references within the aspects
14819
14820 else
14821 Save_Global_References_In_Aspects (N);
14822 end if;
14823 end if;
14824 end Save_References;
14825
14826 -- Start of processing for Save_Global_References
14827
14828 begin
14829 Gen_Scope := Current_Scope;
14830
14831 -- If the generic unit is a child unit, references to entities in the
14832 -- parent are treated as local, because they will be resolved anew in
14833 -- the context of the instance of the parent.
14834
14835 while Is_Child_Unit (Gen_Scope)
14836 and then Ekind (Scope (Gen_Scope)) = E_Generic_Package
14837 loop
14838 Gen_Scope := Scope (Gen_Scope);
14839 end loop;
14840
14841 Save_References (Templ);
14842 end Save_Global_References;
14843
14844 ---------------------------------------
14845 -- Save_Global_References_In_Aspects --
14846 ---------------------------------------
14847
14848 procedure Save_Global_References_In_Aspects (N : Node_Id) is
14849 Asp : Node_Id;
14850 Expr : Node_Id;
14851
14852 begin
14853 Asp := First (Aspect_Specifications (N));
14854 while Present (Asp) loop
14855 Expr := Expression (Asp);
14856
14857 if Present (Expr) then
14858 Save_Global_References (Expr);
14859 end if;
14860
14861 Next (Asp);
14862 end loop;
14863 end Save_Global_References_In_Aspects;
14864
14865 ----------------------------------------
14866 -- Save_Global_References_In_Contract --
14867 ----------------------------------------
14868
14869 procedure Save_Global_References_In_Contract
14870 (Templ : Node_Id;
14871 Gen_Id : Entity_Id)
14872 is
14873 procedure Save_Global_References_In_List (First_Prag : Node_Id);
14874 -- Save all global references in contract-related source pragmas found
14875 -- in the list starting with pragma First_Prag.
14876
14877 ------------------------------------
14878 -- Save_Global_References_In_List --
14879 ------------------------------------
14880
14881 procedure Save_Global_References_In_List (First_Prag : Node_Id) is
14882 Prag : Node_Id;
14883
14884 begin
14885 Prag := First_Prag;
14886 while Present (Prag) loop
14887 if Is_Generic_Contract_Pragma (Prag) then
14888 Save_Global_References (Prag);
14889 end if;
14890
14891 Prag := Next_Pragma (Prag);
14892 end loop;
14893 end Save_Global_References_In_List;
14894
14895 -- Local variables
14896
14897 Items : constant Node_Id := Contract (Defining_Entity (Templ));
14898
14899 -- Start of processing for Save_Global_References_In_Contract
14900
14901 begin
14902 -- The entity of the analyzed generic copy must be on the scope stack
14903 -- to ensure proper detection of global references.
14904
14905 Push_Scope (Gen_Id);
14906
14907 if Permits_Aspect_Specifications (Templ)
14908 and then Has_Aspects (Templ)
14909 then
14910 Save_Global_References_In_Aspects (Templ);
14911 end if;
14912
14913 if Present (Items) then
14914 Save_Global_References_In_List (Pre_Post_Conditions (Items));
14915 Save_Global_References_In_List (Contract_Test_Cases (Items));
14916 Save_Global_References_In_List (Classifications (Items));
14917 end if;
14918
14919 Pop_Scope;
14920 end Save_Global_References_In_Contract;
14921
14922 --------------------------------------
14923 -- Set_Copied_Sloc_For_Inlined_Body --
14924 --------------------------------------
14925
14926 procedure Set_Copied_Sloc_For_Inlined_Body (N : Node_Id; E : Entity_Id) is
14927 begin
14928 Create_Instantiation_Source (N, E, True, S_Adjustment);
14929 end Set_Copied_Sloc_For_Inlined_Body;
14930
14931 ---------------------
14932 -- Set_Instance_Of --
14933 ---------------------
14934
14935 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id) is
14936 begin
14937 Generic_Renamings.Table (Generic_Renamings.Last) := (A, B, Assoc_Null);
14938 Generic_Renamings_HTable.Set (Generic_Renamings.Last);
14939 Generic_Renamings.Increment_Last;
14940 end Set_Instance_Of;
14941
14942 --------------------
14943 -- Set_Next_Assoc --
14944 --------------------
14945
14946 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr) is
14947 begin
14948 Generic_Renamings.Table (E).Next_In_HTable := Next;
14949 end Set_Next_Assoc;
14950
14951 -------------------
14952 -- Start_Generic --
14953 -------------------
14954
14955 procedure Start_Generic is
14956 begin
14957 -- ??? More things could be factored out in this routine.
14958 -- Should probably be done at a later stage.
14959
14960 Generic_Flags.Append (Inside_A_Generic);
14961 Inside_A_Generic := True;
14962
14963 Expander_Mode_Save_And_Set (False);
14964 end Start_Generic;
14965
14966 ----------------------
14967 -- Set_Instance_Env --
14968 ----------------------
14969
14970 procedure Set_Instance_Env
14971 (Gen_Unit : Entity_Id;
14972 Act_Unit : Entity_Id)
14973 is
14974 Assertion_Status : constant Boolean := Assertions_Enabled;
14975 Save_SPARK_Mode : constant SPARK_Mode_Type := SPARK_Mode;
14976 Save_SPARK_Mode_Pragma : constant Node_Id := SPARK_Mode_Pragma;
14977
14978 begin
14979 -- Regardless of the current mode, predefined units are analyzed in the
14980 -- most current Ada mode, and earlier version Ada checks do not apply
14981 -- to predefined units. Nothing needs to be done for non-internal units.
14982 -- These are always analyzed in the current mode.
14983
14984 if Is_Internal_File_Name
14985 (Fname => Unit_File_Name (Get_Source_Unit (Gen_Unit)),
14986 Renamings_Included => True)
14987 then
14988 Set_Opt_Config_Switches (True, Current_Sem_Unit = Main_Unit);
14989
14990 -- In Ada2012 we may want to enable assertions in an instance of a
14991 -- predefined unit, in which case we need to preserve the current
14992 -- setting for the Assertions_Enabled flag. This will become more
14993 -- critical when pre/postconditions are added to predefined units,
14994 -- as is already the case for some numeric libraries.
14995
14996 if Ada_Version >= Ada_2012 then
14997 Assertions_Enabled := Assertion_Status;
14998 end if;
14999
15000 -- SPARK_Mode for an instance is the one applicable at the point of
15001 -- instantiation.
15002
15003 SPARK_Mode := Save_SPARK_Mode;
15004 SPARK_Mode_Pragma := Save_SPARK_Mode_Pragma;
15005
15006 -- Make sure dynamic elaboration checks are off in SPARK Mode
15007
15008 if SPARK_Mode = On then
15009 Dynamic_Elaboration_Checks := False;
15010 end if;
15011 end if;
15012
15013 Current_Instantiated_Parent :=
15014 (Gen_Id => Gen_Unit,
15015 Act_Id => Act_Unit,
15016 Next_In_HTable => Assoc_Null);
15017 end Set_Instance_Env;
15018
15019 -----------------
15020 -- Switch_View --
15021 -----------------
15022
15023 procedure Switch_View (T : Entity_Id) is
15024 BT : constant Entity_Id := Base_Type (T);
15025 Priv_Elmt : Elmt_Id := No_Elmt;
15026 Priv_Sub : Entity_Id;
15027
15028 begin
15029 -- T may be private but its base type may have been exchanged through
15030 -- some other occurrence, in which case there is nothing to switch
15031 -- besides T itself. Note that a private dependent subtype of a private
15032 -- type might not have been switched even if the base type has been,
15033 -- because of the last branch of Check_Private_View (see comment there).
15034
15035 if not Is_Private_Type (BT) then
15036 Prepend_Elmt (Full_View (T), Exchanged_Views);
15037 Exchange_Declarations (T);
15038 return;
15039 end if;
15040
15041 Priv_Elmt := First_Elmt (Private_Dependents (BT));
15042
15043 if Present (Full_View (BT)) then
15044 Prepend_Elmt (Full_View (BT), Exchanged_Views);
15045 Exchange_Declarations (BT);
15046 end if;
15047
15048 while Present (Priv_Elmt) loop
15049 Priv_Sub := (Node (Priv_Elmt));
15050
15051 -- We avoid flipping the subtype if the Etype of its full view is
15052 -- private because this would result in a malformed subtype. This
15053 -- occurs when the Etype of the subtype full view is the full view of
15054 -- the base type (and since the base types were just switched, the
15055 -- subtype is pointing to the wrong view). This is currently the case
15056 -- for tagged record types, access types (maybe more?) and needs to
15057 -- be resolved. ???
15058
15059 if Present (Full_View (Priv_Sub))
15060 and then not Is_Private_Type (Etype (Full_View (Priv_Sub)))
15061 then
15062 Prepend_Elmt (Full_View (Priv_Sub), Exchanged_Views);
15063 Exchange_Declarations (Priv_Sub);
15064 end if;
15065
15066 Next_Elmt (Priv_Elmt);
15067 end loop;
15068 end Switch_View;
15069
15070 -----------------
15071 -- True_Parent --
15072 -----------------
15073
15074 function True_Parent (N : Node_Id) return Node_Id is
15075 begin
15076 if Nkind (Parent (N)) = N_Subunit then
15077 return Parent (Corresponding_Stub (Parent (N)));
15078 else
15079 return Parent (N);
15080 end if;
15081 end True_Parent;
15082
15083 -----------------------------
15084 -- Valid_Default_Attribute --
15085 -----------------------------
15086
15087 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id) is
15088 Attr_Id : constant Attribute_Id :=
15089 Get_Attribute_Id (Attribute_Name (Def));
15090 T : constant Entity_Id := Entity (Prefix (Def));
15091 Is_Fun : constant Boolean := (Ekind (Nam) = E_Function);
15092 F : Entity_Id;
15093 Num_F : Int;
15094 OK : Boolean;
15095
15096 begin
15097 if No (T) or else T = Any_Id then
15098 return;
15099 end if;
15100
15101 Num_F := 0;
15102 F := First_Formal (Nam);
15103 while Present (F) loop
15104 Num_F := Num_F + 1;
15105 Next_Formal (F);
15106 end loop;
15107
15108 case Attr_Id is
15109 when Attribute_Adjacent | Attribute_Ceiling | Attribute_Copy_Sign |
15110 Attribute_Floor | Attribute_Fraction | Attribute_Machine |
15111 Attribute_Model | Attribute_Remainder | Attribute_Rounding |
15112 Attribute_Unbiased_Rounding =>
15113 OK := Is_Fun
15114 and then Num_F = 1
15115 and then Is_Floating_Point_Type (T);
15116
15117 when Attribute_Image | Attribute_Pred | Attribute_Succ |
15118 Attribute_Value | Attribute_Wide_Image |
15119 Attribute_Wide_Value =>
15120 OK := (Is_Fun and then Num_F = 1 and then Is_Scalar_Type (T));
15121
15122 when Attribute_Max | Attribute_Min =>
15123 OK := (Is_Fun and then Num_F = 2 and then Is_Scalar_Type (T));
15124
15125 when Attribute_Input =>
15126 OK := (Is_Fun and then Num_F = 1);
15127
15128 when Attribute_Output | Attribute_Read | Attribute_Write =>
15129 OK := (not Is_Fun and then Num_F = 2);
15130
15131 when others =>
15132 OK := False;
15133 end case;
15134
15135 if not OK then
15136 Error_Msg_N
15137 ("attribute reference has wrong profile for subprogram", Def);
15138 end if;
15139 end Valid_Default_Attribute;
15140
15141 end Sem_Ch12;