exp_ch3.adb (Expand_N_Full_Type_Declaration): Do not capture, set and restore the...
[gcc.git] / gcc / ada / sem_ch12.adb
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S E M _ C H 1 2 --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2015, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
25
26 with Aspects; use Aspects;
27 with Atree; use Atree;
28 with Einfo; use Einfo;
29 with Elists; use Elists;
30 with Errout; use Errout;
31 with Expander; use Expander;
32 with Exp_Disp; use Exp_Disp;
33 with Fname; use Fname;
34 with Fname.UF; use Fname.UF;
35 with Freeze; use Freeze;
36 with Ghost; use Ghost;
37 with Itypes; use Itypes;
38 with Lib; use Lib;
39 with Lib.Load; use Lib.Load;
40 with Lib.Xref; use Lib.Xref;
41 with Nlists; use Nlists;
42 with Namet; use Namet;
43 with Nmake; use Nmake;
44 with Opt; use Opt;
45 with Rident; use Rident;
46 with Restrict; use Restrict;
47 with Rtsfind; use Rtsfind;
48 with Sem; use Sem;
49 with Sem_Aux; use Sem_Aux;
50 with Sem_Cat; use Sem_Cat;
51 with Sem_Ch3; use Sem_Ch3;
52 with Sem_Ch6; use Sem_Ch6;
53 with Sem_Ch7; use Sem_Ch7;
54 with Sem_Ch8; use Sem_Ch8;
55 with Sem_Ch10; use Sem_Ch10;
56 with Sem_Ch13; use Sem_Ch13;
57 with Sem_Dim; use Sem_Dim;
58 with Sem_Disp; use Sem_Disp;
59 with Sem_Elab; use Sem_Elab;
60 with Sem_Elim; use Sem_Elim;
61 with Sem_Eval; use Sem_Eval;
62 with Sem_Prag; use Sem_Prag;
63 with Sem_Res; use Sem_Res;
64 with Sem_Type; use Sem_Type;
65 with Sem_Util; use Sem_Util;
66 with Sem_Warn; use Sem_Warn;
67 with Stand; use Stand;
68 with Sinfo; use Sinfo;
69 with Sinfo.CN; use Sinfo.CN;
70 with Sinput; use Sinput;
71 with Sinput.L; use Sinput.L;
72 with Snames; use Snames;
73 with Stringt; use Stringt;
74 with Uname; use Uname;
75 with Table;
76 with Tbuild; use Tbuild;
77 with Uintp; use Uintp;
78 with Urealp; use Urealp;
79 with Warnsw; use Warnsw;
80
81 with GNAT.HTable;
82
83 package body Sem_Ch12 is
84
85 ----------------------------------------------------------
86 -- Implementation of Generic Analysis and Instantiation --
87 ----------------------------------------------------------
88
89 -- GNAT implements generics by macro expansion. No attempt is made to share
90 -- generic instantiations (for now). Analysis of a generic definition does
91 -- not perform any expansion action, but the expander must be called on the
92 -- tree for each instantiation, because the expansion may of course depend
93 -- on the generic actuals. All of this is best achieved as follows:
94 --
95 -- a) Semantic analysis of a generic unit is performed on a copy of the
96 -- tree for the generic unit. All tree modifications that follow analysis
97 -- do not affect the original tree. Links are kept between the original
98 -- tree and the copy, in order to recognize non-local references within
99 -- the generic, and propagate them to each instance (recall that name
100 -- resolution is done on the generic declaration: generics are not really
101 -- macros). This is summarized in the following diagram:
102
103 -- .-----------. .----------.
104 -- | semantic |<--------------| generic |
105 -- | copy | | unit |
106 -- | |==============>| |
107 -- |___________| global |__________|
108 -- references | | |
109 -- | | |
110 -- .-----|--|.
111 -- | .-----|---.
112 -- | | .----------.
113 -- | | | generic |
114 -- |__| | |
115 -- |__| instance |
116 -- |__________|
117
118 -- b) Each instantiation copies the original tree, and inserts into it a
119 -- series of declarations that describe the mapping between generic formals
120 -- and actuals. For example, a generic In OUT parameter is an object
121 -- renaming of the corresponding actual, etc. Generic IN parameters are
122 -- constant declarations.
123
124 -- c) In order to give the right visibility for these renamings, we use
125 -- a different scheme for package and subprogram instantiations. For
126 -- packages, the list of renamings is inserted into the package
127 -- specification, before the visible declarations of the package. The
128 -- renamings are analyzed before any of the text of the instance, and are
129 -- thus visible at the right place. Furthermore, outside of the instance,
130 -- the generic parameters are visible and denote their corresponding
131 -- actuals.
132
133 -- For subprograms, we create a container package to hold the renamings
134 -- and the subprogram instance itself. Analysis of the package makes the
135 -- renaming declarations visible to the subprogram. After analyzing the
136 -- package, the defining entity for the subprogram is touched-up so that
137 -- it appears declared in the current scope, and not inside the container
138 -- package.
139
140 -- If the instantiation is a compilation unit, the container package is
141 -- given the same name as the subprogram instance. This ensures that
142 -- the elaboration procedure called by the binder, using the compilation
143 -- unit name, calls in fact the elaboration procedure for the package.
144
145 -- Not surprisingly, private types complicate this approach. By saving in
146 -- the original generic object the non-local references, we guarantee that
147 -- the proper entities are referenced at the point of instantiation.
148 -- However, for private types, this by itself does not insure that the
149 -- proper VIEW of the entity is used (the full type may be visible at the
150 -- point of generic definition, but not at instantiation, or vice-versa).
151 -- In order to reference the proper view, we special-case any reference
152 -- to private types in the generic object, by saving both views, one in
153 -- the generic and one in the semantic copy. At time of instantiation, we
154 -- check whether the two views are consistent, and exchange declarations if
155 -- necessary, in order to restore the correct visibility. Similarly, if
156 -- the instance view is private when the generic view was not, we perform
157 -- the exchange. After completing the instantiation, we restore the
158 -- current visibility. The flag Has_Private_View marks identifiers in the
159 -- the generic unit that require checking.
160
161 -- Visibility within nested generic units requires special handling.
162 -- Consider the following scheme:
163
164 -- type Global is ... -- outside of generic unit.
165 -- generic ...
166 -- package Outer is
167 -- ...
168 -- type Semi_Global is ... -- global to inner.
169
170 -- generic ... -- 1
171 -- procedure inner (X1 : Global; X2 : Semi_Global);
172
173 -- procedure in2 is new inner (...); -- 4
174 -- end Outer;
175
176 -- package New_Outer is new Outer (...); -- 2
177 -- procedure New_Inner is new New_Outer.Inner (...); -- 3
178
179 -- The semantic analysis of Outer captures all occurrences of Global.
180 -- The semantic analysis of Inner (at 1) captures both occurrences of
181 -- Global and Semi_Global.
182
183 -- At point 2 (instantiation of Outer), we also produce a generic copy
184 -- of Inner, even though Inner is, at that point, not being instantiated.
185 -- (This is just part of the semantic analysis of New_Outer).
186
187 -- Critically, references to Global within Inner must be preserved, while
188 -- references to Semi_Global should not preserved, because they must now
189 -- resolve to an entity within New_Outer. To distinguish between these, we
190 -- use a global variable, Current_Instantiated_Parent, which is set when
191 -- performing a generic copy during instantiation (at 2). This variable is
192 -- used when performing a generic copy that is not an instantiation, but
193 -- that is nested within one, as the occurrence of 1 within 2. The analysis
194 -- of a nested generic only preserves references that are global to the
195 -- enclosing Current_Instantiated_Parent. We use the Scope_Depth value to
196 -- determine whether a reference is external to the given parent.
197
198 -- The instantiation at point 3 requires no special treatment. The method
199 -- works as well for further nestings of generic units, but of course the
200 -- variable Current_Instantiated_Parent must be stacked because nested
201 -- instantiations can occur, e.g. the occurrence of 4 within 2.
202
203 -- The instantiation of package and subprogram bodies is handled in a
204 -- similar manner, except that it is delayed until after semantic
205 -- analysis is complete. In this fashion complex cross-dependencies
206 -- between several package declarations and bodies containing generics
207 -- can be compiled which otherwise would diagnose spurious circularities.
208
209 -- For example, it is possible to compile two packages A and B that
210 -- have the following structure:
211
212 -- package A is package B is
213 -- generic ... generic ...
214 -- package G_A is package G_B is
215
216 -- with B; with A;
217 -- package body A is package body B is
218 -- package N_B is new G_B (..) package N_A is new G_A (..)
219
220 -- The table Pending_Instantiations in package Inline is used to keep
221 -- track of body instantiations that are delayed in this manner. Inline
222 -- handles the actual calls to do the body instantiations. This activity
223 -- is part of Inline, since the processing occurs at the same point, and
224 -- for essentially the same reason, as the handling of inlined routines.
225
226 ----------------------------------------------
227 -- Detection of Instantiation Circularities --
228 ----------------------------------------------
229
230 -- If we have a chain of instantiations that is circular, this is static
231 -- error which must be detected at compile time. The detection of these
232 -- circularities is carried out at the point that we insert a generic
233 -- instance spec or body. If there is a circularity, then the analysis of
234 -- the offending spec or body will eventually result in trying to load the
235 -- same unit again, and we detect this problem as we analyze the package
236 -- instantiation for the second time.
237
238 -- At least in some cases after we have detected the circularity, we get
239 -- into trouble if we try to keep going. The following flag is set if a
240 -- circularity is detected, and used to abandon compilation after the
241 -- messages have been posted.
242
243 -----------------------------------------
244 -- Implementation of Generic Contracts --
245 -----------------------------------------
246
247 -- A "contract" is a collection of aspects and pragmas that either verify a
248 -- property of a construct at runtime or classify the data flow to and from
249 -- the construct in some fashion.
250
251 -- Generic packages, subprograms and their respective bodies may be subject
252 -- to the following contract-related aspects or pragmas collectively known
253 -- as annotations:
254
255 -- package subprogram [body]
256 -- Abstract_State Contract_Cases
257 -- Initial_Condition Depends
258 -- Initializes Extensions_Visible
259 -- Global
260 -- package body Post
261 -- Refined_State Post_Class
262 -- Postcondition
263 -- Pre
264 -- Pre_Class
265 -- Precondition
266 -- Refined_Depends
267 -- Refined_Global
268 -- Refined_Post
269 -- Test_Case
270
271 -- Most package contract annotations utilize forward references to classify
272 -- data declared within the package [body]. Subprogram annotations then use
273 -- the classifications to further refine them. These inter dependencies are
274 -- problematic with respect to the implementation of generics because their
275 -- analysis, capture of global references and instantiation does not mesh
276 -- well with the existing mechanism.
277
278 -- 1) Analysis of generic contracts is carried out the same way non-generic
279 -- contracts are analyzed:
280
281 -- 1.1) General rule - a contract is analyzed after all related aspects
282 -- and pragmas are analyzed. This is done by routines
283
284 -- Analyze_Package_Body_Contract
285 -- Analyze_Package_Contract
286 -- Analyze_Subprogram_Body_Contract
287 -- Analyze_Subprogram_Contract
288
289 -- 1.2) Compilation unit - the contract is analyzed after Pragmas_After
290 -- are processed.
291
292 -- 1.3) Compilation unit body - the contract is analyzed at the end of
293 -- the body declaration list.
294
295 -- 1.4) Package - the contract is analyzed at the end of the private or
296 -- visible declarations, prior to analyzing the contracts of any nested
297 -- packages or subprograms.
298
299 -- 1.5) Package body - the contract is analyzed at the end of the body
300 -- declaration list, prior to analyzing the contracts of any nested
301 -- packages or subprograms.
302
303 -- 1.6) Subprogram - if the subprogram is declared inside a block, a
304 -- package or a subprogram, then its contract is analyzed at the end of
305 -- the enclosing declarations, otherwise the subprogram is a compilation
306 -- unit 1.2).
307
308 -- 1.7) Subprogram body - if the subprogram body is declared inside a
309 -- block, a package body or a subprogram body, then its contract is
310 -- analyzed at the end of the enclosing declarations, otherwise the
311 -- subprogram is a compilation unit 1.3).
312
313 -- 2) Capture of global references within contracts is done after capturing
314 -- global references within the generic template. There are two reasons for
315 -- this delay - pragma annotations are not part of the generic template in
316 -- the case of a generic subprogram declaration, and analysis of contracts
317 -- is delayed.
318
319 -- Contract-related source pragmas within generic templates are prepared
320 -- for delayed capture of global references by routine
321
322 -- Create_Generic_Contract
323
324 -- The routine associates these pragmas with the contract of the template.
325 -- In the case of a generic subprogram declaration, the routine creates
326 -- generic templates for the pragmas declared after the subprogram because
327 -- they are not part of the template.
328
329 -- generic -- template starts
330 -- procedure Gen_Proc (Input : Integer); -- template ends
331 -- pragma Precondition (Input > 0); -- requires own template
332
333 -- 2.1) The capture of global references with aspect specifications and
334 -- source pragmas that apply to a generic unit must be suppressed when
335 -- the generic template is being processed because the contracts have not
336 -- been analyzed yet. Any attempts to capture global references at that
337 -- point will destroy the Associated_Node linkages and leave the template
338 -- undecorated. This delay is controlled by routine
339
340 -- Requires_Delayed_Save
341
342 -- 2.2) The real capture of global references within a contract is done
343 -- after the contract has been analyzed, by routine
344
345 -- Save_Global_References_In_Contract
346
347 -- 3) The instantiation of a generic contract occurs as part of the
348 -- instantiation of the contract owner. Generic subprogram declarations
349 -- require additional processing when the contract is specified by pragmas
350 -- because the pragmas are not part of the generic template. This is done
351 -- by routine
352
353 -- Instantiate_Subprogram_Contract
354
355 Circularity_Detected : Boolean := False;
356 -- This should really be reset on encountering a new main unit, but in
357 -- practice we are not using multiple main units so it is not critical.
358
359 --------------------------------------------------
360 -- Formal packages and partial parameterization --
361 --------------------------------------------------
362
363 -- When compiling a generic, a formal package is a local instantiation. If
364 -- declared with a box, its generic formals are visible in the enclosing
365 -- generic. If declared with a partial list of actuals, those actuals that
366 -- are defaulted (covered by an Others clause, or given an explicit box
367 -- initialization) are also visible in the enclosing generic, while those
368 -- that have a corresponding actual are not.
369
370 -- In our source model of instantiation, the same visibility must be
371 -- present in the spec and body of an instance: the names of the formals
372 -- that are defaulted must be made visible within the instance, and made
373 -- invisible (hidden) after the instantiation is complete, so that they
374 -- are not accessible outside of the instance.
375
376 -- In a generic, a formal package is treated like a special instantiation.
377 -- Our Ada 95 compiler handled formals with and without box in different
378 -- ways. With partial parameterization, we use a single model for both.
379 -- We create a package declaration that consists of the specification of
380 -- the generic package, and a set of declarations that map the actuals
381 -- into local renamings, just as we do for bona fide instantiations. For
382 -- defaulted parameters and formals with a box, we copy directly the
383 -- declarations of the formal into this local package. The result is a
384 -- a package whose visible declarations may include generic formals. This
385 -- package is only used for type checking and visibility analysis, and
386 -- never reaches the back-end, so it can freely violate the placement
387 -- rules for generic formal declarations.
388
389 -- The list of declarations (renamings and copies of formals) is built
390 -- by Analyze_Associations, just as for regular instantiations.
391
392 -- At the point of instantiation, conformance checking must be applied only
393 -- to those parameters that were specified in the formal. We perform this
394 -- checking by creating another internal instantiation, this one including
395 -- only the renamings and the formals (the rest of the package spec is not
396 -- relevant to conformance checking). We can then traverse two lists: the
397 -- list of actuals in the instance that corresponds to the formal package,
398 -- and the list of actuals produced for this bogus instantiation. We apply
399 -- the conformance rules to those actuals that are not defaulted (i.e.
400 -- which still appear as generic formals.
401
402 -- When we compile an instance body we must make the right parameters
403 -- visible again. The predicate Is_Generic_Formal indicates which of the
404 -- formals should have its Is_Hidden flag reset.
405
406 -----------------------
407 -- Local subprograms --
408 -----------------------
409
410 procedure Abandon_Instantiation (N : Node_Id);
411 pragma No_Return (Abandon_Instantiation);
412 -- Posts an error message "instantiation abandoned" at the indicated node
413 -- and then raises the exception Instantiation_Error to do it.
414
415 procedure Analyze_Formal_Array_Type
416 (T : in out Entity_Id;
417 Def : Node_Id);
418 -- A formal array type is treated like an array type declaration, and
419 -- invokes Array_Type_Declaration (sem_ch3) whose first parameter is
420 -- in-out, because in the case of an anonymous type the entity is
421 -- actually created in the procedure.
422
423 -- The following procedures treat other kinds of formal parameters
424
425 procedure Analyze_Formal_Derived_Interface_Type
426 (N : Node_Id;
427 T : Entity_Id;
428 Def : Node_Id);
429
430 procedure Analyze_Formal_Derived_Type
431 (N : Node_Id;
432 T : Entity_Id;
433 Def : Node_Id);
434
435 procedure Analyze_Formal_Interface_Type
436 (N : Node_Id;
437 T : Entity_Id;
438 Def : Node_Id);
439
440 -- The following subprograms create abbreviated declarations for formal
441 -- scalar types. We introduce an anonymous base of the proper class for
442 -- each of them, and define the formals as constrained first subtypes of
443 -- their bases. The bounds are expressions that are non-static in the
444 -- generic.
445
446 procedure Analyze_Formal_Decimal_Fixed_Point_Type
447 (T : Entity_Id; Def : Node_Id);
448 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id);
449 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id);
450 procedure Analyze_Formal_Signed_Integer_Type (T : Entity_Id; Def : Node_Id);
451 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id);
452 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
453 (T : Entity_Id; Def : Node_Id);
454
455 procedure Analyze_Formal_Private_Type
456 (N : Node_Id;
457 T : Entity_Id;
458 Def : Node_Id);
459 -- Creates a new private type, which does not require completion
460
461 procedure Analyze_Formal_Incomplete_Type (T : Entity_Id; Def : Node_Id);
462 -- Ada 2012: Creates a new incomplete type whose actual does not freeze
463
464 procedure Analyze_Generic_Formal_Part (N : Node_Id);
465 -- Analyze generic formal part
466
467 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id);
468 -- Create a new access type with the given designated type
469
470 function Analyze_Associations
471 (I_Node : Node_Id;
472 Formals : List_Id;
473 F_Copy : List_Id) return List_Id;
474 -- At instantiation time, build the list of associations between formals
475 -- and actuals. Each association becomes a renaming declaration for the
476 -- formal entity. F_Copy is the analyzed list of formals in the generic
477 -- copy. It is used to apply legality checks to the actuals. I_Node is the
478 -- instantiation node itself.
479
480 procedure Analyze_Subprogram_Instantiation
481 (N : Node_Id;
482 K : Entity_Kind);
483
484 procedure Build_Instance_Compilation_Unit_Nodes
485 (N : Node_Id;
486 Act_Body : Node_Id;
487 Act_Decl : Node_Id);
488 -- This procedure is used in the case where the generic instance of a
489 -- subprogram body or package body is a library unit. In this case, the
490 -- original library unit node for the generic instantiation must be
491 -- replaced by the resulting generic body, and a link made to a new
492 -- compilation unit node for the generic declaration. The argument N is
493 -- the original generic instantiation. Act_Body and Act_Decl are the body
494 -- and declaration of the instance (either package body and declaration
495 -- nodes or subprogram body and declaration nodes depending on the case).
496 -- On return, the node N has been rewritten with the actual body.
497
498 procedure Check_Access_Definition (N : Node_Id);
499 -- Subsidiary routine to null exclusion processing. Perform an assertion
500 -- check on Ada version and the presence of an access definition in N.
501
502 procedure Check_Formal_Packages (P_Id : Entity_Id);
503 -- Apply the following to all formal packages in generic associations
504
505 procedure Check_Formal_Package_Instance
506 (Formal_Pack : Entity_Id;
507 Actual_Pack : Entity_Id);
508 -- Verify that the actuals of the actual instance match the actuals of
509 -- the template for a formal package that is not declared with a box.
510
511 procedure Check_Forward_Instantiation (Decl : Node_Id);
512 -- If the generic is a local entity and the corresponding body has not
513 -- been seen yet, flag enclosing packages to indicate that it will be
514 -- elaborated after the generic body. Subprograms declared in the same
515 -- package cannot be inlined by the front-end because front-end inlining
516 -- requires a strict linear order of elaboration.
517
518 function Check_Hidden_Primitives (Assoc_List : List_Id) return Elist_Id;
519 -- Check if some association between formals and actuals requires to make
520 -- visible primitives of a tagged type, and make those primitives visible.
521 -- Return the list of primitives whose visibility is modified (to restore
522 -- their visibility later through Restore_Hidden_Primitives). If no
523 -- candidate is found then return No_Elist.
524
525 procedure Check_Hidden_Child_Unit
526 (N : Node_Id;
527 Gen_Unit : Entity_Id;
528 Act_Decl_Id : Entity_Id);
529 -- If the generic unit is an implicit child instance within a parent
530 -- instance, we need to make an explicit test that it is not hidden by
531 -- a child instance of the same name and parent.
532
533 procedure Check_Generic_Actuals
534 (Instance : Entity_Id;
535 Is_Formal_Box : Boolean);
536 -- Similar to previous one. Check the actuals in the instantiation,
537 -- whose views can change between the point of instantiation and the point
538 -- of instantiation of the body. In addition, mark the generic renamings
539 -- as generic actuals, so that they are not compatible with other actuals.
540 -- Recurse on an actual that is a formal package whose declaration has
541 -- a box.
542
543 function Contains_Instance_Of
544 (Inner : Entity_Id;
545 Outer : Entity_Id;
546 N : Node_Id) return Boolean;
547 -- Inner is instantiated within the generic Outer. Check whether Inner
548 -- directly or indirectly contains an instance of Outer or of one of its
549 -- parents, in the case of a subunit. Each generic unit holds a list of
550 -- the entities instantiated within (at any depth). This procedure
551 -- determines whether the set of such lists contains a cycle, i.e. an
552 -- illegal circular instantiation.
553
554 function Denotes_Formal_Package
555 (Pack : Entity_Id;
556 On_Exit : Boolean := False;
557 Instance : Entity_Id := Empty) return Boolean;
558 -- Returns True if E is a formal package of an enclosing generic, or
559 -- the actual for such a formal in an enclosing instantiation. If such
560 -- a package is used as a formal in an nested generic, or as an actual
561 -- in a nested instantiation, the visibility of ITS formals should not
562 -- be modified. When called from within Restore_Private_Views, the flag
563 -- On_Exit is true, to indicate that the search for a possible enclosing
564 -- instance should ignore the current one. In that case Instance denotes
565 -- the declaration for which this is an actual. This declaration may be
566 -- an instantiation in the source, or the internal instantiation that
567 -- corresponds to the actual for a formal package.
568
569 function Earlier (N1, N2 : Node_Id) return Boolean;
570 -- Yields True if N1 and N2 appear in the same compilation unit,
571 -- ignoring subunits, and if N1 is to the left of N2 in a left-to-right
572 -- traversal of the tree for the unit. Used to determine the placement
573 -- of freeze nodes for instance bodies that may depend on other instances.
574
575 function Find_Actual_Type
576 (Typ : Entity_Id;
577 Gen_Type : Entity_Id) return Entity_Id;
578 -- When validating the actual types of a child instance, check whether
579 -- the formal is a formal type of the parent unit, and retrieve the current
580 -- actual for it. Typ is the entity in the analyzed formal type declaration
581 -- (component or index type of an array type, or designated type of an
582 -- access formal) and Gen_Type is the enclosing analyzed formal array
583 -- or access type. The desired actual may be a formal of a parent, or may
584 -- be declared in a formal package of a parent. In both cases it is a
585 -- generic actual type because it appears within a visible instance.
586 -- Finally, it may be declared in a parent unit without being a formal
587 -- of that unit, in which case it must be retrieved by visibility.
588 -- Ambiguities may still arise if two homonyms are declared in two formal
589 -- packages, and the prefix of the formal type may be needed to resolve
590 -- the ambiguity in the instance ???
591
592 procedure Freeze_Subprogram_Body
593 (Inst_Node : Node_Id;
594 Gen_Body : Node_Id;
595 Pack_Id : Entity_Id);
596 -- The generic body may appear textually after the instance, including
597 -- in the proper body of a stub, or within a different package instance.
598 -- Given that the instance can only be elaborated after the generic, we
599 -- place freeze_nodes for the instance and/or for packages that may enclose
600 -- the instance and the generic, so that the back-end can establish the
601 -- proper order of elaboration.
602
603 function Get_Associated_Node (N : Node_Id) return Node_Id;
604 -- In order to propagate semantic information back from the analyzed copy
605 -- to the original generic, we maintain links between selected nodes in the
606 -- generic and their corresponding copies. At the end of generic analysis,
607 -- the routine Save_Global_References traverses the generic tree, examines
608 -- the semantic information, and preserves the links to those nodes that
609 -- contain global information. At instantiation, the information from the
610 -- associated node is placed on the new copy, so that name resolution is
611 -- not repeated.
612 --
613 -- Three kinds of source nodes have associated nodes:
614 --
615 -- a) those that can reference (denote) entities, that is identifiers,
616 -- character literals, expanded_names, operator symbols, operators,
617 -- and attribute reference nodes. These nodes have an Entity field
618 -- and are the set of nodes that are in N_Has_Entity.
619 --
620 -- b) aggregates (N_Aggregate and N_Extension_Aggregate)
621 --
622 -- c) selected components (N_Selected_Component)
623 --
624 -- For the first class, the associated node preserves the entity if it is
625 -- global. If the generic contains nested instantiations, the associated
626 -- node itself has been recopied, and a chain of them must be followed.
627 --
628 -- For aggregates, the associated node allows retrieval of the type, which
629 -- may otherwise not appear in the generic. The view of this type may be
630 -- different between generic and instantiation, and the full view can be
631 -- installed before the instantiation is analyzed. For aggregates of type
632 -- extensions, the same view exchange may have to be performed for some of
633 -- the ancestor types, if their view is private at the point of
634 -- instantiation.
635 --
636 -- Nodes that are selected components in the parse tree may be rewritten
637 -- as expanded names after resolution, and must be treated as potential
638 -- entity holders, which is why they also have an Associated_Node.
639 --
640 -- Nodes that do not come from source, such as freeze nodes, do not appear
641 -- in the generic tree, and need not have an associated node.
642 --
643 -- The associated node is stored in the Associated_Node field. Note that
644 -- this field overlaps Entity, which is fine, because the whole point is
645 -- that we don't need or want the normal Entity field in this situation.
646
647 function Has_Been_Exchanged (E : Entity_Id) return Boolean;
648 -- Traverse the Exchanged_Views list to see if a type was private
649 -- and has already been flipped during this phase of instantiation.
650
651 procedure Hide_Current_Scope;
652 -- When instantiating a generic child unit, the parent context must be
653 -- present, but the instance and all entities that may be generated
654 -- must be inserted in the current scope. We leave the current scope
655 -- on the stack, but make its entities invisible to avoid visibility
656 -- problems. This is reversed at the end of the instantiation. This is
657 -- not done for the instantiation of the bodies, which only require the
658 -- instances of the generic parents to be in scope.
659
660 function In_Same_Declarative_Part
661 (F_Node : Node_Id;
662 Inst : Node_Id) return Boolean;
663 -- True if the instantiation Inst and the given freeze_node F_Node appear
664 -- within the same declarative part, ignoring subunits, but with no inter-
665 -- vening subprograms or concurrent units. Used to find the proper plave
666 -- for the freeze node of an instance, when the generic is declared in a
667 -- previous instance. If predicate is true, the freeze node of the instance
668 -- can be placed after the freeze node of the previous instance, Otherwise
669 -- it has to be placed at the end of the current declarative part.
670
671 function In_Main_Context (E : Entity_Id) return Boolean;
672 -- Check whether an instantiation is in the context of the main unit.
673 -- Used to determine whether its body should be elaborated to allow
674 -- front-end inlining.
675
676 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id);
677 -- Add the context clause of the unit containing a generic unit to a
678 -- compilation unit that is, or contains, an instantiation.
679
680 procedure Init_Env;
681 -- Establish environment for subsequent instantiation. Separated from
682 -- Save_Env because data-structures for visibility handling must be
683 -- initialized before call to Check_Generic_Child_Unit.
684
685 procedure Inline_Instance_Body
686 (N : Node_Id;
687 Gen_Unit : Entity_Id;
688 Act_Decl : Node_Id);
689 -- If front-end inlining is requested, instantiate the package body,
690 -- and preserve the visibility of its compilation unit, to insure
691 -- that successive instantiations succeed.
692
693 procedure Insert_Freeze_Node_For_Instance
694 (N : Node_Id;
695 F_Node : Node_Id);
696 -- N denotes a package or a subprogram instantiation and F_Node is the
697 -- associated freeze node. Insert the freeze node before the first source
698 -- body which follows immediately after N. If no such body is found, the
699 -- freeze node is inserted at the end of the declarative region which
700 -- contains N.
701
702 procedure Install_Body
703 (Act_Body : Node_Id;
704 N : Node_Id;
705 Gen_Body : Node_Id;
706 Gen_Decl : Node_Id);
707 -- If the instantiation happens textually before the body of the generic,
708 -- the instantiation of the body must be analyzed after the generic body,
709 -- and not at the point of instantiation. Such early instantiations can
710 -- happen if the generic and the instance appear in a package declaration
711 -- because the generic body can only appear in the corresponding package
712 -- body. Early instantiations can also appear if generic, instance and
713 -- body are all in the declarative part of a subprogram or entry. Entities
714 -- of packages that are early instantiations are delayed, and their freeze
715 -- node appears after the generic body.
716
717 procedure Install_Formal_Packages (Par : Entity_Id);
718 -- Install the visible part of any formal of the parent that is a formal
719 -- package. Note that for the case of a formal package with a box, this
720 -- includes the formal part of the formal package (12.7(10/2)).
721
722 procedure Install_Hidden_Primitives
723 (Prims_List : in out Elist_Id;
724 Gen_T : Entity_Id;
725 Act_T : Entity_Id);
726 -- Remove suffix 'P' from hidden primitives of Act_T to match the
727 -- visibility of primitives of Gen_T. The list of primitives to which
728 -- the suffix is removed is added to Prims_List to restore them later.
729
730 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False);
731 -- When compiling an instance of a child unit the parent (which is
732 -- itself an instance) is an enclosing scope that must be made
733 -- immediately visible. This procedure is also used to install the non-
734 -- generic parent of a generic child unit when compiling its body, so
735 -- that full views of types in the parent are made visible.
736
737 -- The functions Instantiate_XXX perform various legality checks and build
738 -- the declarations for instantiated generic parameters. In all of these
739 -- Formal is the entity in the generic unit, Actual is the entity of
740 -- expression in the generic associations, and Analyzed_Formal is the
741 -- formal in the generic copy, which contains the semantic information to
742 -- be used to validate the actual.
743
744 function Instantiate_Object
745 (Formal : Node_Id;
746 Actual : Node_Id;
747 Analyzed_Formal : Node_Id) return List_Id;
748
749 function Instantiate_Type
750 (Formal : Node_Id;
751 Actual : Node_Id;
752 Analyzed_Formal : Node_Id;
753 Actual_Decls : List_Id) return List_Id;
754
755 function Instantiate_Formal_Subprogram
756 (Formal : Node_Id;
757 Actual : Node_Id;
758 Analyzed_Formal : Node_Id) return Node_Id;
759
760 function Instantiate_Formal_Package
761 (Formal : Node_Id;
762 Actual : Node_Id;
763 Analyzed_Formal : Node_Id) return List_Id;
764 -- If the formal package is declared with a box, special visibility rules
765 -- apply to its formals: they are in the visible part of the package. This
766 -- is true in the declarative region of the formal package, that is to say
767 -- in the enclosing generic or instantiation. For an instantiation, the
768 -- parameters of the formal package are made visible in an explicit step.
769 -- Furthermore, if the actual has a visible USE clause, these formals must
770 -- be made potentially use-visible as well. On exit from the enclosing
771 -- instantiation, the reverse must be done.
772
773 -- For a formal package declared without a box, there are conformance rules
774 -- that apply to the actuals in the generic declaration and the actuals of
775 -- the actual package in the enclosing instantiation. The simplest way to
776 -- apply these rules is to repeat the instantiation of the formal package
777 -- in the context of the enclosing instance, and compare the generic
778 -- associations of this instantiation with those of the actual package.
779 -- This internal instantiation only needs to contain the renamings of the
780 -- formals: the visible and private declarations themselves need not be
781 -- created.
782
783 -- In Ada 2005, the formal package may be only partially parameterized.
784 -- In that case the visibility step must make visible those actuals whose
785 -- corresponding formals were given with a box. A final complication
786 -- involves inherited operations from formal derived types, which must
787 -- be visible if the type is.
788
789 function Is_In_Main_Unit (N : Node_Id) return Boolean;
790 -- Test if given node is in the main unit
791
792 procedure Load_Parent_Of_Generic
793 (N : Node_Id;
794 Spec : Node_Id;
795 Body_Optional : Boolean := False);
796 -- If the generic appears in a separate non-generic library unit, load the
797 -- corresponding body to retrieve the body of the generic. N is the node
798 -- for the generic instantiation, Spec is the generic package declaration.
799 --
800 -- Body_Optional is a flag that indicates that the body is being loaded to
801 -- ensure that temporaries are generated consistently when there are other
802 -- instances in the current declarative part that precede the one being
803 -- loaded. In that case a missing body is acceptable.
804
805 procedure Map_Formal_Package_Entities (Form : Entity_Id; Act : Entity_Id);
806 -- Within the generic part, entities in the formal package are
807 -- visible. To validate subsequent type declarations, indicate
808 -- the correspondence between the entities in the analyzed formal,
809 -- and the entities in the actual package. There are three packages
810 -- involved in the instantiation of a formal package: the parent
811 -- generic P1 which appears in the generic declaration, the fake
812 -- instantiation P2 which appears in the analyzed generic, and whose
813 -- visible entities may be used in subsequent formals, and the actual
814 -- P3 in the instance. To validate subsequent formals, me indicate
815 -- that the entities in P2 are mapped into those of P3. The mapping of
816 -- entities has to be done recursively for nested packages.
817
818 procedure Move_Freeze_Nodes
819 (Out_Of : Entity_Id;
820 After : Node_Id;
821 L : List_Id);
822 -- Freeze nodes can be generated in the analysis of a generic unit, but
823 -- will not be seen by the back-end. It is necessary to move those nodes
824 -- to the enclosing scope if they freeze an outer entity. We place them
825 -- at the end of the enclosing generic package, which is semantically
826 -- neutral.
827
828 procedure Preanalyze_Actuals (N : Node_Id; Inst : Entity_Id := Empty);
829 -- Analyze actuals to perform name resolution. Full resolution is done
830 -- later, when the expected types are known, but names have to be captured
831 -- before installing parents of generics, that are not visible for the
832 -- actuals themselves.
833 --
834 -- If Inst is present, it is the entity of the package instance. This
835 -- entity is marked as having a limited_view actual when some actual is
836 -- a limited view. This is used to place the instance body properly.
837
838 procedure Remove_Parent (In_Body : Boolean := False);
839 -- Reverse effect after instantiation of child is complete
840
841 procedure Restore_Hidden_Primitives (Prims_List : in out Elist_Id);
842 -- Restore suffix 'P' to primitives of Prims_List and leave Prims_List
843 -- set to No_Elist.
844
845 procedure Save_Global_References_In_Aspects (N : Node_Id);
846 -- Save all global references found within the expressions of all aspects
847 -- that appear on node N.
848
849 procedure Set_Instance_Env
850 (Gen_Unit : Entity_Id;
851 Act_Unit : Entity_Id);
852 -- Save current instance on saved environment, to be used to determine
853 -- the global status of entities in nested instances. Part of Save_Env.
854 -- called after verifying that the generic unit is legal for the instance,
855 -- The procedure also examines whether the generic unit is a predefined
856 -- unit, in order to set configuration switches accordingly. As a result
857 -- the procedure must be called after analyzing and freezing the actuals.
858
859 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id);
860 -- Associate analyzed generic parameter with corresponding instance. Used
861 -- for semantic checks at instantiation time.
862
863 function True_Parent (N : Node_Id) return Node_Id;
864 -- For a subunit, return parent of corresponding stub, else return
865 -- parent of node.
866
867 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id);
868 -- Verify that an attribute that appears as the default for a formal
869 -- subprogram is a function or procedure with the correct profile.
870
871 -------------------------------------------
872 -- Data Structures for Generic Renamings --
873 -------------------------------------------
874
875 -- The map Generic_Renamings associates generic entities with their
876 -- corresponding actuals. Currently used to validate type instances. It
877 -- will eventually be used for all generic parameters to eliminate the
878 -- need for overload resolution in the instance.
879
880 type Assoc_Ptr is new Int;
881
882 Assoc_Null : constant Assoc_Ptr := -1;
883
884 type Assoc is record
885 Gen_Id : Entity_Id;
886 Act_Id : Entity_Id;
887 Next_In_HTable : Assoc_Ptr;
888 end record;
889
890 package Generic_Renamings is new Table.Table
891 (Table_Component_Type => Assoc,
892 Table_Index_Type => Assoc_Ptr,
893 Table_Low_Bound => 0,
894 Table_Initial => 10,
895 Table_Increment => 100,
896 Table_Name => "Generic_Renamings");
897
898 -- Variable to hold enclosing instantiation. When the environment is
899 -- saved for a subprogram inlining, the corresponding Act_Id is empty.
900
901 Current_Instantiated_Parent : Assoc := (Empty, Empty, Assoc_Null);
902
903 -- Hash table for associations
904
905 HTable_Size : constant := 37;
906 type HTable_Range is range 0 .. HTable_Size - 1;
907
908 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr);
909 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr;
910 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id;
911 function Hash (F : Entity_Id) return HTable_Range;
912
913 package Generic_Renamings_HTable is new GNAT.HTable.Static_HTable (
914 Header_Num => HTable_Range,
915 Element => Assoc,
916 Elmt_Ptr => Assoc_Ptr,
917 Null_Ptr => Assoc_Null,
918 Set_Next => Set_Next_Assoc,
919 Next => Next_Assoc,
920 Key => Entity_Id,
921 Get_Key => Get_Gen_Id,
922 Hash => Hash,
923 Equal => "=");
924
925 Exchanged_Views : Elist_Id;
926 -- This list holds the private views that have been exchanged during
927 -- instantiation to restore the visibility of the generic declaration.
928 -- (see comments above). After instantiation, the current visibility is
929 -- reestablished by means of a traversal of this list.
930
931 Hidden_Entities : Elist_Id;
932 -- This list holds the entities of the current scope that are removed
933 -- from immediate visibility when instantiating a child unit. Their
934 -- visibility is restored in Remove_Parent.
935
936 -- Because instantiations can be recursive, the following must be saved
937 -- on entry and restored on exit from an instantiation (spec or body).
938 -- This is done by the two procedures Save_Env and Restore_Env. For
939 -- package and subprogram instantiations (but not for the body instances)
940 -- the action of Save_Env is done in two steps: Init_Env is called before
941 -- Check_Generic_Child_Unit, because setting the parent instances requires
942 -- that the visibility data structures be properly initialized. Once the
943 -- generic is unit is validated, Set_Instance_Env completes Save_Env.
944
945 Parent_Unit_Visible : Boolean := False;
946 -- Parent_Unit_Visible is used when the generic is a child unit, and
947 -- indicates whether the ultimate parent of the generic is visible in the
948 -- instantiation environment. It is used to reset the visibility of the
949 -- parent at the end of the instantiation (see Remove_Parent).
950
951 Instance_Parent_Unit : Entity_Id := Empty;
952 -- This records the ultimate parent unit of an instance of a generic
953 -- child unit and is used in conjunction with Parent_Unit_Visible to
954 -- indicate the unit to which the Parent_Unit_Visible flag corresponds.
955
956 type Instance_Env is record
957 Instantiated_Parent : Assoc;
958 Exchanged_Views : Elist_Id;
959 Hidden_Entities : Elist_Id;
960 Current_Sem_Unit : Unit_Number_Type;
961 Parent_Unit_Visible : Boolean := False;
962 Instance_Parent_Unit : Entity_Id := Empty;
963 Switches : Config_Switches_Type;
964 end record;
965
966 package Instance_Envs is new Table.Table (
967 Table_Component_Type => Instance_Env,
968 Table_Index_Type => Int,
969 Table_Low_Bound => 0,
970 Table_Initial => 32,
971 Table_Increment => 100,
972 Table_Name => "Instance_Envs");
973
974 procedure Restore_Private_Views
975 (Pack_Id : Entity_Id;
976 Is_Package : Boolean := True);
977 -- Restore the private views of external types, and unmark the generic
978 -- renamings of actuals, so that they become compatible subtypes again.
979 -- For subprograms, Pack_Id is the package constructed to hold the
980 -- renamings.
981
982 procedure Switch_View (T : Entity_Id);
983 -- Switch the partial and full views of a type and its private
984 -- dependents (i.e. its subtypes and derived types).
985
986 ------------------------------------
987 -- Structures for Error Reporting --
988 ------------------------------------
989
990 Instantiation_Node : Node_Id;
991 -- Used by subprograms that validate instantiation of formal parameters
992 -- where there might be no actual on which to place the error message.
993 -- Also used to locate the instantiation node for generic subunits.
994
995 Instantiation_Error : exception;
996 -- When there is a semantic error in the generic parameter matching,
997 -- there is no point in continuing the instantiation, because the
998 -- number of cascaded errors is unpredictable. This exception aborts
999 -- the instantiation process altogether.
1000
1001 S_Adjustment : Sloc_Adjustment;
1002 -- Offset created for each node in an instantiation, in order to keep
1003 -- track of the source position of the instantiation in each of its nodes.
1004 -- A subsequent semantic error or warning on a construct of the instance
1005 -- points to both places: the original generic node, and the point of
1006 -- instantiation. See Sinput and Sinput.L for additional details.
1007
1008 ------------------------------------------------------------
1009 -- Data structure for keeping track when inside a Generic --
1010 ------------------------------------------------------------
1011
1012 -- The following table is used to save values of the Inside_A_Generic
1013 -- flag (see spec of Sem) when they are saved by Start_Generic.
1014
1015 package Generic_Flags is new Table.Table (
1016 Table_Component_Type => Boolean,
1017 Table_Index_Type => Int,
1018 Table_Low_Bound => 0,
1019 Table_Initial => 32,
1020 Table_Increment => 200,
1021 Table_Name => "Generic_Flags");
1022
1023 ---------------------------
1024 -- Abandon_Instantiation --
1025 ---------------------------
1026
1027 procedure Abandon_Instantiation (N : Node_Id) is
1028 begin
1029 Error_Msg_N ("\instantiation abandoned!", N);
1030 raise Instantiation_Error;
1031 end Abandon_Instantiation;
1032
1033 --------------------------
1034 -- Analyze_Associations --
1035 --------------------------
1036
1037 function Analyze_Associations
1038 (I_Node : Node_Id;
1039 Formals : List_Id;
1040 F_Copy : List_Id) return List_Id
1041 is
1042 Actuals_To_Freeze : constant Elist_Id := New_Elmt_List;
1043 Assoc : constant List_Id := New_List;
1044 Default_Actuals : constant List_Id := New_List;
1045 Gen_Unit : constant Entity_Id :=
1046 Defining_Entity (Parent (F_Copy));
1047
1048 Actuals : List_Id;
1049 Actual : Node_Id;
1050 Analyzed_Formal : Node_Id;
1051 First_Named : Node_Id := Empty;
1052 Formal : Node_Id;
1053 Match : Node_Id;
1054 Named : Node_Id;
1055 Saved_Formal : Node_Id;
1056
1057 Default_Formals : constant List_Id := New_List;
1058 -- If an Others_Choice is present, some of the formals may be defaulted.
1059 -- To simplify the treatment of visibility in an instance, we introduce
1060 -- individual defaults for each such formal. These defaults are
1061 -- appended to the list of associations and replace the Others_Choice.
1062
1063 Found_Assoc : Node_Id;
1064 -- Association for the current formal being match. Empty if there are
1065 -- no remaining actuals, or if there is no named association with the
1066 -- name of the formal.
1067
1068 Is_Named_Assoc : Boolean;
1069 Num_Matched : Int := 0;
1070 Num_Actuals : Int := 0;
1071
1072 Others_Present : Boolean := False;
1073 Others_Choice : Node_Id := Empty;
1074 -- In Ada 2005, indicates partial parameterization of a formal
1075 -- package. As usual an other association must be last in the list.
1076
1077 procedure Check_Overloaded_Formal_Subprogram (Formal : Entity_Id);
1078 -- Apply RM 12.3(9): if a formal subprogram is overloaded, the instance
1079 -- cannot have a named association for it. AI05-0025 extends this rule
1080 -- to formals of formal packages by AI05-0025, and it also applies to
1081 -- box-initialized formals.
1082
1083 function Has_Fully_Defined_Profile (Subp : Entity_Id) return Boolean;
1084 -- Determine whether the parameter types and the return type of Subp
1085 -- are fully defined at the point of instantiation.
1086
1087 function Matching_Actual
1088 (F : Entity_Id;
1089 A_F : Entity_Id) return Node_Id;
1090 -- Find actual that corresponds to a given a formal parameter. If the
1091 -- actuals are positional, return the next one, if any. If the actuals
1092 -- are named, scan the parameter associations to find the right one.
1093 -- A_F is the corresponding entity in the analyzed generic,which is
1094 -- placed on the selector name for ASIS use.
1095 --
1096 -- In Ada 2005, a named association may be given with a box, in which
1097 -- case Matching_Actual sets Found_Assoc to the generic association,
1098 -- but return Empty for the actual itself. In this case the code below
1099 -- creates a corresponding declaration for the formal.
1100
1101 function Partial_Parameterization return Boolean;
1102 -- Ada 2005: if no match is found for a given formal, check if the
1103 -- association for it includes a box, or whether the associations
1104 -- include an Others clause.
1105
1106 procedure Process_Default (F : Entity_Id);
1107 -- Add a copy of the declaration of generic formal F to the list of
1108 -- associations, and add an explicit box association for F if there
1109 -- is none yet, and the default comes from an Others_Choice.
1110
1111 function Renames_Standard_Subprogram (Subp : Entity_Id) return Boolean;
1112 -- Determine whether Subp renames one of the subprograms defined in the
1113 -- generated package Standard.
1114
1115 procedure Set_Analyzed_Formal;
1116 -- Find the node in the generic copy that corresponds to a given formal.
1117 -- The semantic information on this node is used to perform legality
1118 -- checks on the actuals. Because semantic analysis can introduce some
1119 -- anonymous entities or modify the declaration node itself, the
1120 -- correspondence between the two lists is not one-one. In addition to
1121 -- anonymous types, the presence a formal equality will introduce an
1122 -- implicit declaration for the corresponding inequality.
1123
1124 ----------------------------------------
1125 -- Check_Overloaded_Formal_Subprogram --
1126 ----------------------------------------
1127
1128 procedure Check_Overloaded_Formal_Subprogram (Formal : Entity_Id) is
1129 Temp_Formal : Entity_Id;
1130
1131 begin
1132 Temp_Formal := First (Formals);
1133 while Present (Temp_Formal) loop
1134 if Nkind (Temp_Formal) in N_Formal_Subprogram_Declaration
1135 and then Temp_Formal /= Formal
1136 and then
1137 Chars (Defining_Unit_Name (Specification (Formal))) =
1138 Chars (Defining_Unit_Name (Specification (Temp_Formal)))
1139 then
1140 if Present (Found_Assoc) then
1141 Error_Msg_N
1142 ("named association not allowed for overloaded formal",
1143 Found_Assoc);
1144
1145 else
1146 Error_Msg_N
1147 ("named association not allowed for overloaded formal",
1148 Others_Choice);
1149 end if;
1150
1151 Abandon_Instantiation (Instantiation_Node);
1152 end if;
1153
1154 Next (Temp_Formal);
1155 end loop;
1156 end Check_Overloaded_Formal_Subprogram;
1157
1158 -------------------------------
1159 -- Has_Fully_Defined_Profile --
1160 -------------------------------
1161
1162 function Has_Fully_Defined_Profile (Subp : Entity_Id) return Boolean is
1163 function Is_Fully_Defined_Type (Typ : Entity_Id) return Boolean;
1164 -- Determine whethet type Typ is fully defined
1165
1166 ---------------------------
1167 -- Is_Fully_Defined_Type --
1168 ---------------------------
1169
1170 function Is_Fully_Defined_Type (Typ : Entity_Id) return Boolean is
1171 begin
1172 -- A private type without a full view is not fully defined
1173
1174 if Is_Private_Type (Typ)
1175 and then No (Full_View (Typ))
1176 then
1177 return False;
1178
1179 -- An incomplete type is never fully defined
1180
1181 elsif Is_Incomplete_Type (Typ) then
1182 return False;
1183
1184 -- All other types are fully defined
1185
1186 else
1187 return True;
1188 end if;
1189 end Is_Fully_Defined_Type;
1190
1191 -- Local declarations
1192
1193 Param : Entity_Id;
1194
1195 -- Start of processing for Has_Fully_Defined_Profile
1196
1197 begin
1198 -- Check the parameters
1199
1200 Param := First_Formal (Subp);
1201 while Present (Param) loop
1202 if not Is_Fully_Defined_Type (Etype (Param)) then
1203 return False;
1204 end if;
1205
1206 Next_Formal (Param);
1207 end loop;
1208
1209 -- Check the return type
1210
1211 return Is_Fully_Defined_Type (Etype (Subp));
1212 end Has_Fully_Defined_Profile;
1213
1214 ---------------------
1215 -- Matching_Actual --
1216 ---------------------
1217
1218 function Matching_Actual
1219 (F : Entity_Id;
1220 A_F : Entity_Id) return Node_Id
1221 is
1222 Prev : Node_Id;
1223 Act : Node_Id;
1224
1225 begin
1226 Is_Named_Assoc := False;
1227
1228 -- End of list of purely positional parameters
1229
1230 if No (Actual) or else Nkind (Actual) = N_Others_Choice then
1231 Found_Assoc := Empty;
1232 Act := Empty;
1233
1234 -- Case of positional parameter corresponding to current formal
1235
1236 elsif No (Selector_Name (Actual)) then
1237 Found_Assoc := Actual;
1238 Act := Explicit_Generic_Actual_Parameter (Actual);
1239 Num_Matched := Num_Matched + 1;
1240 Next (Actual);
1241
1242 -- Otherwise scan list of named actuals to find the one with the
1243 -- desired name. All remaining actuals have explicit names.
1244
1245 else
1246 Is_Named_Assoc := True;
1247 Found_Assoc := Empty;
1248 Act := Empty;
1249 Prev := Empty;
1250
1251 while Present (Actual) loop
1252 if Chars (Selector_Name (Actual)) = Chars (F) then
1253 Set_Entity (Selector_Name (Actual), A_F);
1254 Set_Etype (Selector_Name (Actual), Etype (A_F));
1255 Generate_Reference (A_F, Selector_Name (Actual));
1256 Found_Assoc := Actual;
1257 Act := Explicit_Generic_Actual_Parameter (Actual);
1258 Num_Matched := Num_Matched + 1;
1259 exit;
1260 end if;
1261
1262 Prev := Actual;
1263 Next (Actual);
1264 end loop;
1265
1266 -- Reset for subsequent searches. In most cases the named
1267 -- associations are in order. If they are not, we reorder them
1268 -- to avoid scanning twice the same actual. This is not just a
1269 -- question of efficiency: there may be multiple defaults with
1270 -- boxes that have the same name. In a nested instantiation we
1271 -- insert actuals for those defaults, and cannot rely on their
1272 -- names to disambiguate them.
1273
1274 if Actual = First_Named then
1275 Next (First_Named);
1276
1277 elsif Present (Actual) then
1278 Insert_Before (First_Named, Remove_Next (Prev));
1279 end if;
1280
1281 Actual := First_Named;
1282 end if;
1283
1284 if Is_Entity_Name (Act) and then Present (Entity (Act)) then
1285 Set_Used_As_Generic_Actual (Entity (Act));
1286 end if;
1287
1288 return Act;
1289 end Matching_Actual;
1290
1291 ------------------------------
1292 -- Partial_Parameterization --
1293 ------------------------------
1294
1295 function Partial_Parameterization return Boolean is
1296 begin
1297 return Others_Present
1298 or else (Present (Found_Assoc) and then Box_Present (Found_Assoc));
1299 end Partial_Parameterization;
1300
1301 ---------------------
1302 -- Process_Default --
1303 ---------------------
1304
1305 procedure Process_Default (F : Entity_Id) is
1306 Loc : constant Source_Ptr := Sloc (I_Node);
1307 F_Id : constant Entity_Id := Defining_Entity (F);
1308 Decl : Node_Id;
1309 Default : Node_Id;
1310 Id : Entity_Id;
1311
1312 begin
1313 -- Append copy of formal declaration to associations, and create new
1314 -- defining identifier for it.
1315
1316 Decl := New_Copy_Tree (F);
1317 Id := Make_Defining_Identifier (Sloc (F_Id), Chars (F_Id));
1318
1319 if Nkind (F) in N_Formal_Subprogram_Declaration then
1320 Set_Defining_Unit_Name (Specification (Decl), Id);
1321
1322 else
1323 Set_Defining_Identifier (Decl, Id);
1324 end if;
1325
1326 Append (Decl, Assoc);
1327
1328 if No (Found_Assoc) then
1329 Default :=
1330 Make_Generic_Association (Loc,
1331 Selector_Name =>
1332 New_Occurrence_Of (Id, Loc),
1333 Explicit_Generic_Actual_Parameter => Empty);
1334 Set_Box_Present (Default);
1335 Append (Default, Default_Formals);
1336 end if;
1337 end Process_Default;
1338
1339 ---------------------------------
1340 -- Renames_Standard_Subprogram --
1341 ---------------------------------
1342
1343 function Renames_Standard_Subprogram (Subp : Entity_Id) return Boolean is
1344 Id : Entity_Id;
1345
1346 begin
1347 Id := Alias (Subp);
1348 while Present (Id) loop
1349 if Scope (Id) = Standard_Standard then
1350 return True;
1351 end if;
1352
1353 Id := Alias (Id);
1354 end loop;
1355
1356 return False;
1357 end Renames_Standard_Subprogram;
1358
1359 -------------------------
1360 -- Set_Analyzed_Formal --
1361 -------------------------
1362
1363 procedure Set_Analyzed_Formal is
1364 Kind : Node_Kind;
1365
1366 begin
1367 while Present (Analyzed_Formal) loop
1368 Kind := Nkind (Analyzed_Formal);
1369
1370 case Nkind (Formal) is
1371
1372 when N_Formal_Subprogram_Declaration =>
1373 exit when Kind in N_Formal_Subprogram_Declaration
1374 and then
1375 Chars
1376 (Defining_Unit_Name (Specification (Formal))) =
1377 Chars
1378 (Defining_Unit_Name (Specification (Analyzed_Formal)));
1379
1380 when N_Formal_Package_Declaration =>
1381 exit when Nkind_In (Kind, N_Formal_Package_Declaration,
1382 N_Generic_Package_Declaration,
1383 N_Package_Declaration);
1384
1385 when N_Use_Package_Clause | N_Use_Type_Clause => exit;
1386
1387 when others =>
1388
1389 -- Skip freeze nodes, and nodes inserted to replace
1390 -- unrecognized pragmas.
1391
1392 exit when
1393 Kind not in N_Formal_Subprogram_Declaration
1394 and then not Nkind_In (Kind, N_Subprogram_Declaration,
1395 N_Freeze_Entity,
1396 N_Null_Statement,
1397 N_Itype_Reference)
1398 and then Chars (Defining_Identifier (Formal)) =
1399 Chars (Defining_Identifier (Analyzed_Formal));
1400 end case;
1401
1402 Next (Analyzed_Formal);
1403 end loop;
1404 end Set_Analyzed_Formal;
1405
1406 -- Start of processing for Analyze_Associations
1407
1408 begin
1409 Actuals := Generic_Associations (I_Node);
1410
1411 if Present (Actuals) then
1412
1413 -- Check for an Others choice, indicating a partial parameterization
1414 -- for a formal package.
1415
1416 Actual := First (Actuals);
1417 while Present (Actual) loop
1418 if Nkind (Actual) = N_Others_Choice then
1419 Others_Present := True;
1420 Others_Choice := Actual;
1421
1422 if Present (Next (Actual)) then
1423 Error_Msg_N ("others must be last association", Actual);
1424 end if;
1425
1426 -- This subprogram is used both for formal packages and for
1427 -- instantiations. For the latter, associations must all be
1428 -- explicit.
1429
1430 if Nkind (I_Node) /= N_Formal_Package_Declaration
1431 and then Comes_From_Source (I_Node)
1432 then
1433 Error_Msg_N
1434 ("others association not allowed in an instance",
1435 Actual);
1436 end if;
1437
1438 -- In any case, nothing to do after the others association
1439
1440 exit;
1441
1442 elsif Box_Present (Actual)
1443 and then Comes_From_Source (I_Node)
1444 and then Nkind (I_Node) /= N_Formal_Package_Declaration
1445 then
1446 Error_Msg_N
1447 ("box association not allowed in an instance", Actual);
1448 end if;
1449
1450 Next (Actual);
1451 end loop;
1452
1453 -- If named associations are present, save first named association
1454 -- (it may of course be Empty) to facilitate subsequent name search.
1455
1456 First_Named := First (Actuals);
1457 while Present (First_Named)
1458 and then Nkind (First_Named) /= N_Others_Choice
1459 and then No (Selector_Name (First_Named))
1460 loop
1461 Num_Actuals := Num_Actuals + 1;
1462 Next (First_Named);
1463 end loop;
1464 end if;
1465
1466 Named := First_Named;
1467 while Present (Named) loop
1468 if Nkind (Named) /= N_Others_Choice
1469 and then No (Selector_Name (Named))
1470 then
1471 Error_Msg_N ("invalid positional actual after named one", Named);
1472 Abandon_Instantiation (Named);
1473 end if;
1474
1475 -- A named association may lack an actual parameter, if it was
1476 -- introduced for a default subprogram that turns out to be local
1477 -- to the outer instantiation.
1478
1479 if Nkind (Named) /= N_Others_Choice
1480 and then Present (Explicit_Generic_Actual_Parameter (Named))
1481 then
1482 Num_Actuals := Num_Actuals + 1;
1483 end if;
1484
1485 Next (Named);
1486 end loop;
1487
1488 if Present (Formals) then
1489 Formal := First_Non_Pragma (Formals);
1490 Analyzed_Formal := First_Non_Pragma (F_Copy);
1491
1492 if Present (Actuals) then
1493 Actual := First (Actuals);
1494
1495 -- All formals should have default values
1496
1497 else
1498 Actual := Empty;
1499 end if;
1500
1501 while Present (Formal) loop
1502 Set_Analyzed_Formal;
1503 Saved_Formal := Next_Non_Pragma (Formal);
1504
1505 case Nkind (Formal) is
1506 when N_Formal_Object_Declaration =>
1507 Match :=
1508 Matching_Actual
1509 (Defining_Identifier (Formal),
1510 Defining_Identifier (Analyzed_Formal));
1511
1512 if No (Match) and then Partial_Parameterization then
1513 Process_Default (Formal);
1514
1515 else
1516 Append_List
1517 (Instantiate_Object (Formal, Match, Analyzed_Formal),
1518 Assoc);
1519
1520 -- For a defaulted in_parameter, create an entry in the
1521 -- the list of defaulted actuals, for GNATProve use. Do
1522 -- not included these defaults for an instance nested
1523 -- within a generic, because the defaults are also used
1524 -- in the analysis of the enclosing generic, and only
1525 -- defaulted subprograms are relevant there.
1526
1527 if No (Match) and then not Inside_A_Generic then
1528 Append_To (Default_Actuals,
1529 Make_Generic_Association (Sloc (I_Node),
1530 Selector_Name =>
1531 New_Occurrence_Of
1532 (Defining_Identifier (Formal), Sloc (I_Node)),
1533 Explicit_Generic_Actual_Parameter =>
1534 New_Copy_Tree (Default_Expression (Formal))));
1535 end if;
1536 end if;
1537
1538 -- If the object is a call to an expression function, this
1539 -- is a freezing point for it.
1540
1541 if Is_Entity_Name (Match)
1542 and then Present (Entity (Match))
1543 and then Nkind
1544 (Original_Node (Unit_Declaration_Node (Entity (Match))))
1545 = N_Expression_Function
1546 then
1547 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1548 end if;
1549
1550 when N_Formal_Type_Declaration =>
1551 Match :=
1552 Matching_Actual
1553 (Defining_Identifier (Formal),
1554 Defining_Identifier (Analyzed_Formal));
1555
1556 if No (Match) then
1557 if Partial_Parameterization then
1558 Process_Default (Formal);
1559
1560 else
1561 Error_Msg_Sloc := Sloc (Gen_Unit);
1562 Error_Msg_NE
1563 ("missing actual&",
1564 Instantiation_Node, Defining_Identifier (Formal));
1565 Error_Msg_NE
1566 ("\in instantiation of & declared#",
1567 Instantiation_Node, Gen_Unit);
1568 Abandon_Instantiation (Instantiation_Node);
1569 end if;
1570
1571 else
1572 Analyze (Match);
1573 Append_List
1574 (Instantiate_Type
1575 (Formal, Match, Analyzed_Formal, Assoc),
1576 Assoc);
1577
1578 -- An instantiation is a freeze point for the actuals,
1579 -- unless this is a rewritten formal package, or the
1580 -- formal is an Ada 2012 formal incomplete type.
1581
1582 if Nkind (I_Node) = N_Formal_Package_Declaration
1583 or else
1584 (Ada_Version >= Ada_2012
1585 and then
1586 Ekind (Defining_Identifier (Analyzed_Formal)) =
1587 E_Incomplete_Type)
1588 then
1589 null;
1590
1591 else
1592 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1593 end if;
1594 end if;
1595
1596 -- A remote access-to-class-wide type is not a legal actual
1597 -- for a generic formal of an access type (E.2.2(17/2)).
1598 -- In GNAT an exception to this rule is introduced when
1599 -- the formal is marked as remote using implementation
1600 -- defined aspect/pragma Remote_Access_Type. In that case
1601 -- the actual must be remote as well.
1602
1603 -- If the current instantiation is the construction of a
1604 -- local copy for a formal package the actuals may be
1605 -- defaulted, and there is no matching actual to check.
1606
1607 if Nkind (Analyzed_Formal) = N_Formal_Type_Declaration
1608 and then
1609 Nkind (Formal_Type_Definition (Analyzed_Formal)) =
1610 N_Access_To_Object_Definition
1611 and then Present (Match)
1612 then
1613 declare
1614 Formal_Ent : constant Entity_Id :=
1615 Defining_Identifier (Analyzed_Formal);
1616 begin
1617 if Is_Remote_Access_To_Class_Wide_Type (Entity (Match))
1618 = Is_Remote_Types (Formal_Ent)
1619 then
1620 -- Remoteness of formal and actual match
1621
1622 null;
1623
1624 elsif Is_Remote_Types (Formal_Ent) then
1625
1626 -- Remote formal, non-remote actual
1627
1628 Error_Msg_NE
1629 ("actual for& must be remote", Match, Formal_Ent);
1630
1631 else
1632 -- Non-remote formal, remote actual
1633
1634 Error_Msg_NE
1635 ("actual for& may not be remote",
1636 Match, Formal_Ent);
1637 end if;
1638 end;
1639 end if;
1640
1641 when N_Formal_Subprogram_Declaration =>
1642 Match :=
1643 Matching_Actual
1644 (Defining_Unit_Name (Specification (Formal)),
1645 Defining_Unit_Name (Specification (Analyzed_Formal)));
1646
1647 -- If the formal subprogram has the same name as another
1648 -- formal subprogram of the generic, then a named
1649 -- association is illegal (12.3(9)). Exclude named
1650 -- associations that are generated for a nested instance.
1651
1652 if Present (Match)
1653 and then Is_Named_Assoc
1654 and then Comes_From_Source (Found_Assoc)
1655 then
1656 Check_Overloaded_Formal_Subprogram (Formal);
1657 end if;
1658
1659 -- If there is no corresponding actual, this may be case
1660 -- of partial parameterization, or else the formal has a
1661 -- default or a box.
1662
1663 if No (Match) and then Partial_Parameterization then
1664 Process_Default (Formal);
1665
1666 if Nkind (I_Node) = N_Formal_Package_Declaration then
1667 Check_Overloaded_Formal_Subprogram (Formal);
1668 end if;
1669
1670 else
1671 Append_To (Assoc,
1672 Instantiate_Formal_Subprogram
1673 (Formal, Match, Analyzed_Formal));
1674
1675 -- An instantiation is a freeze point for the actuals,
1676 -- unless this is a rewritten formal package.
1677
1678 if Nkind (I_Node) /= N_Formal_Package_Declaration
1679 and then Nkind (Match) = N_Identifier
1680 and then Is_Subprogram (Entity (Match))
1681
1682 -- The actual subprogram may rename a routine defined
1683 -- in Standard. Avoid freezing such renamings because
1684 -- subprograms coming from Standard cannot be frozen.
1685
1686 and then
1687 not Renames_Standard_Subprogram (Entity (Match))
1688
1689 -- If the actual subprogram comes from a different
1690 -- unit, it is already frozen, either by a body in
1691 -- that unit or by the end of the declarative part
1692 -- of the unit. This check avoids the freezing of
1693 -- subprograms defined in Standard which are used
1694 -- as generic actuals.
1695
1696 and then In_Same_Code_Unit (Entity (Match), I_Node)
1697 and then Has_Fully_Defined_Profile (Entity (Match))
1698 then
1699 -- Mark the subprogram as having a delayed freeze
1700 -- since this may be an out-of-order action.
1701
1702 Set_Has_Delayed_Freeze (Entity (Match));
1703 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1704 end if;
1705 end if;
1706
1707 -- If this is a nested generic, preserve default for later
1708 -- instantiations. We do this as well for GNATProve use,
1709 -- so that the list of generic associations is complete.
1710
1711 if No (Match) and then Box_Present (Formal) then
1712 declare
1713 Subp : constant Entity_Id :=
1714 Defining_Unit_Name (Specification (Last (Assoc)));
1715
1716 begin
1717 Append_To (Default_Actuals,
1718 Make_Generic_Association (Sloc (I_Node),
1719 Selector_Name =>
1720 New_Occurrence_Of (Subp, Sloc (I_Node)),
1721 Explicit_Generic_Actual_Parameter =>
1722 New_Occurrence_Of (Subp, Sloc (I_Node))));
1723 end;
1724 end if;
1725
1726 when N_Formal_Package_Declaration =>
1727 Match :=
1728 Matching_Actual
1729 (Defining_Identifier (Formal),
1730 Defining_Identifier (Original_Node (Analyzed_Formal)));
1731
1732 if No (Match) then
1733 if Partial_Parameterization then
1734 Process_Default (Formal);
1735
1736 else
1737 Error_Msg_Sloc := Sloc (Gen_Unit);
1738 Error_Msg_NE
1739 ("missing actual&",
1740 Instantiation_Node, Defining_Identifier (Formal));
1741 Error_Msg_NE
1742 ("\in instantiation of & declared#",
1743 Instantiation_Node, Gen_Unit);
1744
1745 Abandon_Instantiation (Instantiation_Node);
1746 end if;
1747
1748 else
1749 Analyze (Match);
1750 Append_List
1751 (Instantiate_Formal_Package
1752 (Formal, Match, Analyzed_Formal),
1753 Assoc);
1754 end if;
1755
1756 -- For use type and use package appearing in the generic part,
1757 -- we have already copied them, so we can just move them where
1758 -- they belong (we mustn't recopy them since this would mess up
1759 -- the Sloc values).
1760
1761 when N_Use_Package_Clause |
1762 N_Use_Type_Clause =>
1763 if Nkind (Original_Node (I_Node)) =
1764 N_Formal_Package_Declaration
1765 then
1766 Append (New_Copy_Tree (Formal), Assoc);
1767 else
1768 Remove (Formal);
1769 Append (Formal, Assoc);
1770 end if;
1771
1772 when others =>
1773 raise Program_Error;
1774
1775 end case;
1776
1777 Formal := Saved_Formal;
1778 Next_Non_Pragma (Analyzed_Formal);
1779 end loop;
1780
1781 if Num_Actuals > Num_Matched then
1782 Error_Msg_Sloc := Sloc (Gen_Unit);
1783
1784 if Present (Selector_Name (Actual)) then
1785 Error_Msg_NE
1786 ("unmatched actual &", Actual, Selector_Name (Actual));
1787 Error_Msg_NE
1788 ("\in instantiation of & declared#", Actual, Gen_Unit);
1789 else
1790 Error_Msg_NE
1791 ("unmatched actual in instantiation of & declared#",
1792 Actual, Gen_Unit);
1793 end if;
1794 end if;
1795
1796 elsif Present (Actuals) then
1797 Error_Msg_N
1798 ("too many actuals in generic instantiation", Instantiation_Node);
1799 end if;
1800
1801 -- An instantiation freezes all generic actuals. The only exceptions
1802 -- to this are incomplete types and subprograms which are not fully
1803 -- defined at the point of instantiation.
1804
1805 declare
1806 Elmt : Elmt_Id := First_Elmt (Actuals_To_Freeze);
1807 begin
1808 while Present (Elmt) loop
1809 Freeze_Before (I_Node, Node (Elmt));
1810 Next_Elmt (Elmt);
1811 end loop;
1812 end;
1813
1814 -- If there are default subprograms, normalize the tree by adding
1815 -- explicit associations for them. This is required if the instance
1816 -- appears within a generic.
1817
1818 if not Is_Empty_List (Default_Actuals) then
1819 declare
1820 Default : Node_Id;
1821
1822 begin
1823 Default := First (Default_Actuals);
1824 while Present (Default) loop
1825 Mark_Rewrite_Insertion (Default);
1826 Next (Default);
1827 end loop;
1828
1829 if No (Actuals) then
1830 Set_Generic_Associations (I_Node, Default_Actuals);
1831 else
1832 Append_List_To (Actuals, Default_Actuals);
1833 end if;
1834 end;
1835 end if;
1836
1837 -- If this is a formal package, normalize the parameter list by adding
1838 -- explicit box associations for the formals that are covered by an
1839 -- Others_Choice.
1840
1841 if not Is_Empty_List (Default_Formals) then
1842 Append_List (Default_Formals, Formals);
1843 end if;
1844
1845 return Assoc;
1846 end Analyze_Associations;
1847
1848 -------------------------------
1849 -- Analyze_Formal_Array_Type --
1850 -------------------------------
1851
1852 procedure Analyze_Formal_Array_Type
1853 (T : in out Entity_Id;
1854 Def : Node_Id)
1855 is
1856 DSS : Node_Id;
1857
1858 begin
1859 -- Treated like a non-generic array declaration, with additional
1860 -- semantic checks.
1861
1862 Enter_Name (T);
1863
1864 if Nkind (Def) = N_Constrained_Array_Definition then
1865 DSS := First (Discrete_Subtype_Definitions (Def));
1866 while Present (DSS) loop
1867 if Nkind_In (DSS, N_Subtype_Indication,
1868 N_Range,
1869 N_Attribute_Reference)
1870 then
1871 Error_Msg_N ("only a subtype mark is allowed in a formal", DSS);
1872 end if;
1873
1874 Next (DSS);
1875 end loop;
1876 end if;
1877
1878 Array_Type_Declaration (T, Def);
1879 Set_Is_Generic_Type (Base_Type (T));
1880
1881 if Ekind (Component_Type (T)) = E_Incomplete_Type
1882 and then No (Full_View (Component_Type (T)))
1883 then
1884 Error_Msg_N ("premature usage of incomplete type", Def);
1885
1886 -- Check that range constraint is not allowed on the component type
1887 -- of a generic formal array type (AARM 12.5.3(3))
1888
1889 elsif Is_Internal (Component_Type (T))
1890 and then Present (Subtype_Indication (Component_Definition (Def)))
1891 and then Nkind (Original_Node
1892 (Subtype_Indication (Component_Definition (Def)))) =
1893 N_Subtype_Indication
1894 then
1895 Error_Msg_N
1896 ("in a formal, a subtype indication can only be "
1897 & "a subtype mark (RM 12.5.3(3))",
1898 Subtype_Indication (Component_Definition (Def)));
1899 end if;
1900
1901 end Analyze_Formal_Array_Type;
1902
1903 ---------------------------------------------
1904 -- Analyze_Formal_Decimal_Fixed_Point_Type --
1905 ---------------------------------------------
1906
1907 -- As for other generic types, we create a valid type representation with
1908 -- legal but arbitrary attributes, whose values are never considered
1909 -- static. For all scalar types we introduce an anonymous base type, with
1910 -- the same attributes. We choose the corresponding integer type to be
1911 -- Standard_Integer.
1912 -- Here and in other similar routines, the Sloc of the generated internal
1913 -- type must be the same as the sloc of the defining identifier of the
1914 -- formal type declaration, to provide proper source navigation.
1915
1916 procedure Analyze_Formal_Decimal_Fixed_Point_Type
1917 (T : Entity_Id;
1918 Def : Node_Id)
1919 is
1920 Loc : constant Source_Ptr := Sloc (Def);
1921
1922 Base : constant Entity_Id :=
1923 New_Internal_Entity
1924 (E_Decimal_Fixed_Point_Type,
1925 Current_Scope,
1926 Sloc (Defining_Identifier (Parent (Def))), 'G');
1927
1928 Int_Base : constant Entity_Id := Standard_Integer;
1929 Delta_Val : constant Ureal := Ureal_1;
1930 Digs_Val : constant Uint := Uint_6;
1931
1932 function Make_Dummy_Bound return Node_Id;
1933 -- Return a properly typed universal real literal to use as a bound
1934
1935 ----------------------
1936 -- Make_Dummy_Bound --
1937 ----------------------
1938
1939 function Make_Dummy_Bound return Node_Id is
1940 Bound : constant Node_Id := Make_Real_Literal (Loc, Ureal_1);
1941 begin
1942 Set_Etype (Bound, Universal_Real);
1943 return Bound;
1944 end Make_Dummy_Bound;
1945
1946 -- Start of processing for Analyze_Formal_Decimal_Fixed_Point_Type
1947
1948 begin
1949 Enter_Name (T);
1950
1951 Set_Etype (Base, Base);
1952 Set_Size_Info (Base, Int_Base);
1953 Set_RM_Size (Base, RM_Size (Int_Base));
1954 Set_First_Rep_Item (Base, First_Rep_Item (Int_Base));
1955 Set_Digits_Value (Base, Digs_Val);
1956 Set_Delta_Value (Base, Delta_Val);
1957 Set_Small_Value (Base, Delta_Val);
1958 Set_Scalar_Range (Base,
1959 Make_Range (Loc,
1960 Low_Bound => Make_Dummy_Bound,
1961 High_Bound => Make_Dummy_Bound));
1962
1963 Set_Is_Generic_Type (Base);
1964 Set_Parent (Base, Parent (Def));
1965
1966 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
1967 Set_Etype (T, Base);
1968 Set_Size_Info (T, Int_Base);
1969 Set_RM_Size (T, RM_Size (Int_Base));
1970 Set_First_Rep_Item (T, First_Rep_Item (Int_Base));
1971 Set_Digits_Value (T, Digs_Val);
1972 Set_Delta_Value (T, Delta_Val);
1973 Set_Small_Value (T, Delta_Val);
1974 Set_Scalar_Range (T, Scalar_Range (Base));
1975 Set_Is_Constrained (T);
1976
1977 Check_Restriction (No_Fixed_Point, Def);
1978 end Analyze_Formal_Decimal_Fixed_Point_Type;
1979
1980 -------------------------------------------
1981 -- Analyze_Formal_Derived_Interface_Type --
1982 -------------------------------------------
1983
1984 procedure Analyze_Formal_Derived_Interface_Type
1985 (N : Node_Id;
1986 T : Entity_Id;
1987 Def : Node_Id)
1988 is
1989 Loc : constant Source_Ptr := Sloc (Def);
1990
1991 begin
1992 -- Rewrite as a type declaration of a derived type. This ensures that
1993 -- the interface list and primitive operations are properly captured.
1994
1995 Rewrite (N,
1996 Make_Full_Type_Declaration (Loc,
1997 Defining_Identifier => T,
1998 Type_Definition => Def));
1999 Analyze (N);
2000 Set_Is_Generic_Type (T);
2001 end Analyze_Formal_Derived_Interface_Type;
2002
2003 ---------------------------------
2004 -- Analyze_Formal_Derived_Type --
2005 ---------------------------------
2006
2007 procedure Analyze_Formal_Derived_Type
2008 (N : Node_Id;
2009 T : Entity_Id;
2010 Def : Node_Id)
2011 is
2012 Loc : constant Source_Ptr := Sloc (Def);
2013 Unk_Disc : constant Boolean := Unknown_Discriminants_Present (N);
2014 New_N : Node_Id;
2015
2016 begin
2017 Set_Is_Generic_Type (T);
2018
2019 if Private_Present (Def) then
2020 New_N :=
2021 Make_Private_Extension_Declaration (Loc,
2022 Defining_Identifier => T,
2023 Discriminant_Specifications => Discriminant_Specifications (N),
2024 Unknown_Discriminants_Present => Unk_Disc,
2025 Subtype_Indication => Subtype_Mark (Def),
2026 Interface_List => Interface_List (Def));
2027
2028 Set_Abstract_Present (New_N, Abstract_Present (Def));
2029 Set_Limited_Present (New_N, Limited_Present (Def));
2030 Set_Synchronized_Present (New_N, Synchronized_Present (Def));
2031
2032 else
2033 New_N :=
2034 Make_Full_Type_Declaration (Loc,
2035 Defining_Identifier => T,
2036 Discriminant_Specifications =>
2037 Discriminant_Specifications (Parent (T)),
2038 Type_Definition =>
2039 Make_Derived_Type_Definition (Loc,
2040 Subtype_Indication => Subtype_Mark (Def)));
2041
2042 Set_Abstract_Present
2043 (Type_Definition (New_N), Abstract_Present (Def));
2044 Set_Limited_Present
2045 (Type_Definition (New_N), Limited_Present (Def));
2046 end if;
2047
2048 Rewrite (N, New_N);
2049 Analyze (N);
2050
2051 if Unk_Disc then
2052 if not Is_Composite_Type (T) then
2053 Error_Msg_N
2054 ("unknown discriminants not allowed for elementary types", N);
2055 else
2056 Set_Has_Unknown_Discriminants (T);
2057 Set_Is_Constrained (T, False);
2058 end if;
2059 end if;
2060
2061 -- If the parent type has a known size, so does the formal, which makes
2062 -- legal representation clauses that involve the formal.
2063
2064 Set_Size_Known_At_Compile_Time
2065 (T, Size_Known_At_Compile_Time (Entity (Subtype_Mark (Def))));
2066 end Analyze_Formal_Derived_Type;
2067
2068 ----------------------------------
2069 -- Analyze_Formal_Discrete_Type --
2070 ----------------------------------
2071
2072 -- The operations defined for a discrete types are those of an enumeration
2073 -- type. The size is set to an arbitrary value, for use in analyzing the
2074 -- generic unit.
2075
2076 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id) is
2077 Loc : constant Source_Ptr := Sloc (Def);
2078 Lo : Node_Id;
2079 Hi : Node_Id;
2080
2081 Base : constant Entity_Id :=
2082 New_Internal_Entity
2083 (E_Floating_Point_Type, Current_Scope,
2084 Sloc (Defining_Identifier (Parent (Def))), 'G');
2085
2086 begin
2087 Enter_Name (T);
2088 Set_Ekind (T, E_Enumeration_Subtype);
2089 Set_Etype (T, Base);
2090 Init_Size (T, 8);
2091 Init_Alignment (T);
2092 Set_Is_Generic_Type (T);
2093 Set_Is_Constrained (T);
2094
2095 -- For semantic analysis, the bounds of the type must be set to some
2096 -- non-static value. The simplest is to create attribute nodes for those
2097 -- bounds, that refer to the type itself. These bounds are never
2098 -- analyzed but serve as place-holders.
2099
2100 Lo :=
2101 Make_Attribute_Reference (Loc,
2102 Attribute_Name => Name_First,
2103 Prefix => New_Occurrence_Of (T, Loc));
2104 Set_Etype (Lo, T);
2105
2106 Hi :=
2107 Make_Attribute_Reference (Loc,
2108 Attribute_Name => Name_Last,
2109 Prefix => New_Occurrence_Of (T, Loc));
2110 Set_Etype (Hi, T);
2111
2112 Set_Scalar_Range (T,
2113 Make_Range (Loc,
2114 Low_Bound => Lo,
2115 High_Bound => Hi));
2116
2117 Set_Ekind (Base, E_Enumeration_Type);
2118 Set_Etype (Base, Base);
2119 Init_Size (Base, 8);
2120 Init_Alignment (Base);
2121 Set_Is_Generic_Type (Base);
2122 Set_Scalar_Range (Base, Scalar_Range (T));
2123 Set_Parent (Base, Parent (Def));
2124 end Analyze_Formal_Discrete_Type;
2125
2126 ----------------------------------
2127 -- Analyze_Formal_Floating_Type --
2128 ---------------------------------
2129
2130 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id) is
2131 Base : constant Entity_Id :=
2132 New_Internal_Entity
2133 (E_Floating_Point_Type, Current_Scope,
2134 Sloc (Defining_Identifier (Parent (Def))), 'G');
2135
2136 begin
2137 -- The various semantic attributes are taken from the predefined type
2138 -- Float, just so that all of them are initialized. Their values are
2139 -- never used because no constant folding or expansion takes place in
2140 -- the generic itself.
2141
2142 Enter_Name (T);
2143 Set_Ekind (T, E_Floating_Point_Subtype);
2144 Set_Etype (T, Base);
2145 Set_Size_Info (T, (Standard_Float));
2146 Set_RM_Size (T, RM_Size (Standard_Float));
2147 Set_Digits_Value (T, Digits_Value (Standard_Float));
2148 Set_Scalar_Range (T, Scalar_Range (Standard_Float));
2149 Set_Is_Constrained (T);
2150
2151 Set_Is_Generic_Type (Base);
2152 Set_Etype (Base, Base);
2153 Set_Size_Info (Base, (Standard_Float));
2154 Set_RM_Size (Base, RM_Size (Standard_Float));
2155 Set_Digits_Value (Base, Digits_Value (Standard_Float));
2156 Set_Scalar_Range (Base, Scalar_Range (Standard_Float));
2157 Set_Parent (Base, Parent (Def));
2158
2159 Check_Restriction (No_Floating_Point, Def);
2160 end Analyze_Formal_Floating_Type;
2161
2162 -----------------------------------
2163 -- Analyze_Formal_Interface_Type;--
2164 -----------------------------------
2165
2166 procedure Analyze_Formal_Interface_Type
2167 (N : Node_Id;
2168 T : Entity_Id;
2169 Def : Node_Id)
2170 is
2171 Loc : constant Source_Ptr := Sloc (N);
2172 New_N : Node_Id;
2173
2174 begin
2175 New_N :=
2176 Make_Full_Type_Declaration (Loc,
2177 Defining_Identifier => T,
2178 Type_Definition => Def);
2179
2180 Rewrite (N, New_N);
2181 Analyze (N);
2182 Set_Is_Generic_Type (T);
2183 end Analyze_Formal_Interface_Type;
2184
2185 ---------------------------------
2186 -- Analyze_Formal_Modular_Type --
2187 ---------------------------------
2188
2189 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id) is
2190 begin
2191 -- Apart from their entity kind, generic modular types are treated like
2192 -- signed integer types, and have the same attributes.
2193
2194 Analyze_Formal_Signed_Integer_Type (T, Def);
2195 Set_Ekind (T, E_Modular_Integer_Subtype);
2196 Set_Ekind (Etype (T), E_Modular_Integer_Type);
2197
2198 end Analyze_Formal_Modular_Type;
2199
2200 ---------------------------------------
2201 -- Analyze_Formal_Object_Declaration --
2202 ---------------------------------------
2203
2204 procedure Analyze_Formal_Object_Declaration (N : Node_Id) is
2205 E : constant Node_Id := Default_Expression (N);
2206 Id : constant Node_Id := Defining_Identifier (N);
2207 K : Entity_Kind;
2208 T : Node_Id;
2209
2210 begin
2211 Enter_Name (Id);
2212
2213 -- Determine the mode of the formal object
2214
2215 if Out_Present (N) then
2216 K := E_Generic_In_Out_Parameter;
2217
2218 if not In_Present (N) then
2219 Error_Msg_N ("formal generic objects cannot have mode OUT", N);
2220 end if;
2221
2222 else
2223 K := E_Generic_In_Parameter;
2224 end if;
2225
2226 if Present (Subtype_Mark (N)) then
2227 Find_Type (Subtype_Mark (N));
2228 T := Entity (Subtype_Mark (N));
2229
2230 -- Verify that there is no redundant null exclusion
2231
2232 if Null_Exclusion_Present (N) then
2233 if not Is_Access_Type (T) then
2234 Error_Msg_N
2235 ("null exclusion can only apply to an access type", N);
2236
2237 elsif Can_Never_Be_Null (T) then
2238 Error_Msg_NE
2239 ("`NOT NULL` not allowed (& already excludes null)", N, T);
2240 end if;
2241 end if;
2242
2243 -- Ada 2005 (AI-423): Formal object with an access definition
2244
2245 else
2246 Check_Access_Definition (N);
2247 T := Access_Definition
2248 (Related_Nod => N,
2249 N => Access_Definition (N));
2250 end if;
2251
2252 if Ekind (T) = E_Incomplete_Type then
2253 declare
2254 Error_Node : Node_Id;
2255
2256 begin
2257 if Present (Subtype_Mark (N)) then
2258 Error_Node := Subtype_Mark (N);
2259 else
2260 Check_Access_Definition (N);
2261 Error_Node := Access_Definition (N);
2262 end if;
2263
2264 Error_Msg_N ("premature usage of incomplete type", Error_Node);
2265 end;
2266 end if;
2267
2268 if K = E_Generic_In_Parameter then
2269
2270 -- Ada 2005 (AI-287): Limited aggregates allowed in generic formals
2271
2272 if Ada_Version < Ada_2005 and then Is_Limited_Type (T) then
2273 Error_Msg_N
2274 ("generic formal of mode IN must not be of limited type", N);
2275 Explain_Limited_Type (T, N);
2276 end if;
2277
2278 if Is_Abstract_Type (T) then
2279 Error_Msg_N
2280 ("generic formal of mode IN must not be of abstract type", N);
2281 end if;
2282
2283 if Present (E) then
2284 Preanalyze_Spec_Expression (E, T);
2285
2286 if Is_Limited_Type (T) and then not OK_For_Limited_Init (T, E) then
2287 Error_Msg_N
2288 ("initialization not allowed for limited types", E);
2289 Explain_Limited_Type (T, E);
2290 end if;
2291 end if;
2292
2293 Set_Ekind (Id, K);
2294 Set_Etype (Id, T);
2295
2296 -- Case of generic IN OUT parameter
2297
2298 else
2299 -- If the formal has an unconstrained type, construct its actual
2300 -- subtype, as is done for subprogram formals. In this fashion, all
2301 -- its uses can refer to specific bounds.
2302
2303 Set_Ekind (Id, K);
2304 Set_Etype (Id, T);
2305
2306 if (Is_Array_Type (T) and then not Is_Constrained (T))
2307 or else (Ekind (T) = E_Record_Type and then Has_Discriminants (T))
2308 then
2309 declare
2310 Non_Freezing_Ref : constant Node_Id :=
2311 New_Occurrence_Of (Id, Sloc (Id));
2312 Decl : Node_Id;
2313
2314 begin
2315 -- Make sure the actual subtype doesn't generate bogus freezing
2316
2317 Set_Must_Not_Freeze (Non_Freezing_Ref);
2318 Decl := Build_Actual_Subtype (T, Non_Freezing_Ref);
2319 Insert_Before_And_Analyze (N, Decl);
2320 Set_Actual_Subtype (Id, Defining_Identifier (Decl));
2321 end;
2322 else
2323 Set_Actual_Subtype (Id, T);
2324 end if;
2325
2326 if Present (E) then
2327 Error_Msg_N
2328 ("initialization not allowed for `IN OUT` formals", N);
2329 end if;
2330 end if;
2331
2332 if Has_Aspects (N) then
2333 Analyze_Aspect_Specifications (N, Id);
2334 end if;
2335 end Analyze_Formal_Object_Declaration;
2336
2337 ----------------------------------------------
2338 -- Analyze_Formal_Ordinary_Fixed_Point_Type --
2339 ----------------------------------------------
2340
2341 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
2342 (T : Entity_Id;
2343 Def : Node_Id)
2344 is
2345 Loc : constant Source_Ptr := Sloc (Def);
2346 Base : constant Entity_Id :=
2347 New_Internal_Entity
2348 (E_Ordinary_Fixed_Point_Type, Current_Scope,
2349 Sloc (Defining_Identifier (Parent (Def))), 'G');
2350
2351 begin
2352 -- The semantic attributes are set for completeness only, their values
2353 -- will never be used, since all properties of the type are non-static.
2354
2355 Enter_Name (T);
2356 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
2357 Set_Etype (T, Base);
2358 Set_Size_Info (T, Standard_Integer);
2359 Set_RM_Size (T, RM_Size (Standard_Integer));
2360 Set_Small_Value (T, Ureal_1);
2361 Set_Delta_Value (T, Ureal_1);
2362 Set_Scalar_Range (T,
2363 Make_Range (Loc,
2364 Low_Bound => Make_Real_Literal (Loc, Ureal_1),
2365 High_Bound => Make_Real_Literal (Loc, Ureal_1)));
2366 Set_Is_Constrained (T);
2367
2368 Set_Is_Generic_Type (Base);
2369 Set_Etype (Base, Base);
2370 Set_Size_Info (Base, Standard_Integer);
2371 Set_RM_Size (Base, RM_Size (Standard_Integer));
2372 Set_Small_Value (Base, Ureal_1);
2373 Set_Delta_Value (Base, Ureal_1);
2374 Set_Scalar_Range (Base, Scalar_Range (T));
2375 Set_Parent (Base, Parent (Def));
2376
2377 Check_Restriction (No_Fixed_Point, Def);
2378 end Analyze_Formal_Ordinary_Fixed_Point_Type;
2379
2380 ----------------------------------------
2381 -- Analyze_Formal_Package_Declaration --
2382 ----------------------------------------
2383
2384 procedure Analyze_Formal_Package_Declaration (N : Node_Id) is
2385 Loc : constant Source_Ptr := Sloc (N);
2386 Pack_Id : constant Entity_Id := Defining_Identifier (N);
2387 Formal : Entity_Id;
2388 Gen_Id : constant Node_Id := Name (N);
2389 Gen_Decl : Node_Id;
2390 Gen_Unit : Entity_Id;
2391 New_N : Node_Id;
2392 Parent_Installed : Boolean := False;
2393 Renaming : Node_Id;
2394 Parent_Instance : Entity_Id;
2395 Renaming_In_Par : Entity_Id;
2396 Associations : Boolean := True;
2397
2398 Vis_Prims_List : Elist_Id := No_Elist;
2399 -- List of primitives made temporarily visible in the instantiation
2400 -- to match the visibility of the formal type
2401
2402 function Build_Local_Package return Node_Id;
2403 -- The formal package is rewritten so that its parameters are replaced
2404 -- with corresponding declarations. For parameters with bona fide
2405 -- associations these declarations are created by Analyze_Associations
2406 -- as for a regular instantiation. For boxed parameters, we preserve
2407 -- the formal declarations and analyze them, in order to introduce
2408 -- entities of the right kind in the environment of the formal.
2409
2410 -------------------------
2411 -- Build_Local_Package --
2412 -------------------------
2413
2414 function Build_Local_Package return Node_Id is
2415 Decls : List_Id;
2416 Pack_Decl : Node_Id;
2417
2418 begin
2419 -- Within the formal, the name of the generic package is a renaming
2420 -- of the formal (as for a regular instantiation).
2421
2422 Pack_Decl :=
2423 Make_Package_Declaration (Loc,
2424 Specification =>
2425 Copy_Generic_Node
2426 (Specification (Original_Node (Gen_Decl)),
2427 Empty, Instantiating => True));
2428
2429 Renaming :=
2430 Make_Package_Renaming_Declaration (Loc,
2431 Defining_Unit_Name =>
2432 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
2433 Name => New_Occurrence_Of (Formal, Loc));
2434
2435 if Nkind (Gen_Id) = N_Identifier
2436 and then Chars (Gen_Id) = Chars (Pack_Id)
2437 then
2438 Error_Msg_NE
2439 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
2440 end if;
2441
2442 -- If the formal is declared with a box, or with an others choice,
2443 -- create corresponding declarations for all entities in the formal
2444 -- part, so that names with the proper types are available in the
2445 -- specification of the formal package.
2446
2447 -- On the other hand, if there are no associations, then all the
2448 -- formals must have defaults, and this will be checked by the
2449 -- call to Analyze_Associations.
2450
2451 if Box_Present (N)
2452 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2453 then
2454 declare
2455 Formal_Decl : Node_Id;
2456
2457 begin
2458 -- TBA : for a formal package, need to recurse ???
2459
2460 Decls := New_List;
2461 Formal_Decl :=
2462 First
2463 (Generic_Formal_Declarations (Original_Node (Gen_Decl)));
2464 while Present (Formal_Decl) loop
2465 Append_To
2466 (Decls, Copy_Generic_Node (Formal_Decl, Empty, True));
2467 Next (Formal_Decl);
2468 end loop;
2469 end;
2470
2471 -- If generic associations are present, use Analyze_Associations to
2472 -- create the proper renaming declarations.
2473
2474 else
2475 declare
2476 Act_Tree : constant Node_Id :=
2477 Copy_Generic_Node
2478 (Original_Node (Gen_Decl), Empty,
2479 Instantiating => True);
2480
2481 begin
2482 Generic_Renamings.Set_Last (0);
2483 Generic_Renamings_HTable.Reset;
2484 Instantiation_Node := N;
2485
2486 Decls :=
2487 Analyze_Associations
2488 (I_Node => Original_Node (N),
2489 Formals => Generic_Formal_Declarations (Act_Tree),
2490 F_Copy => Generic_Formal_Declarations (Gen_Decl));
2491
2492 Vis_Prims_List := Check_Hidden_Primitives (Decls);
2493 end;
2494 end if;
2495
2496 Append (Renaming, To => Decls);
2497
2498 -- Add generated declarations ahead of local declarations in
2499 -- the package.
2500
2501 if No (Visible_Declarations (Specification (Pack_Decl))) then
2502 Set_Visible_Declarations (Specification (Pack_Decl), Decls);
2503 else
2504 Insert_List_Before
2505 (First (Visible_Declarations (Specification (Pack_Decl))),
2506 Decls);
2507 end if;
2508
2509 return Pack_Decl;
2510 end Build_Local_Package;
2511
2512 -- Start of processing for Analyze_Formal_Package_Declaration
2513
2514 begin
2515 Check_Text_IO_Special_Unit (Gen_Id);
2516
2517 Init_Env;
2518 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
2519 Gen_Unit := Entity (Gen_Id);
2520
2521 -- Check for a formal package that is a package renaming
2522
2523 if Present (Renamed_Object (Gen_Unit)) then
2524
2525 -- Indicate that unit is used, before replacing it with renamed
2526 -- entity for use below.
2527
2528 if In_Extended_Main_Source_Unit (N) then
2529 Set_Is_Instantiated (Gen_Unit);
2530 Generate_Reference (Gen_Unit, N);
2531 end if;
2532
2533 Gen_Unit := Renamed_Object (Gen_Unit);
2534 end if;
2535
2536 if Ekind (Gen_Unit) /= E_Generic_Package then
2537 Error_Msg_N ("expect generic package name", Gen_Id);
2538 Restore_Env;
2539 goto Leave;
2540
2541 elsif Gen_Unit = Current_Scope then
2542 Error_Msg_N
2543 ("generic package cannot be used as a formal package of itself",
2544 Gen_Id);
2545 Restore_Env;
2546 goto Leave;
2547
2548 elsif In_Open_Scopes (Gen_Unit) then
2549 if Is_Compilation_Unit (Gen_Unit)
2550 and then Is_Child_Unit (Current_Scope)
2551 then
2552 -- Special-case the error when the formal is a parent, and
2553 -- continue analysis to minimize cascaded errors.
2554
2555 Error_Msg_N
2556 ("generic parent cannot be used as formal package "
2557 & "of a child unit", Gen_Id);
2558
2559 else
2560 Error_Msg_N
2561 ("generic package cannot be used as a formal package "
2562 & "within itself", Gen_Id);
2563 Restore_Env;
2564 goto Leave;
2565 end if;
2566 end if;
2567
2568 -- Check that name of formal package does not hide name of generic,
2569 -- or its leading prefix. This check must be done separately because
2570 -- the name of the generic has already been analyzed.
2571
2572 declare
2573 Gen_Name : Entity_Id;
2574
2575 begin
2576 Gen_Name := Gen_Id;
2577 while Nkind (Gen_Name) = N_Expanded_Name loop
2578 Gen_Name := Prefix (Gen_Name);
2579 end loop;
2580
2581 if Chars (Gen_Name) = Chars (Pack_Id) then
2582 Error_Msg_NE
2583 ("& is hidden within declaration of formal package",
2584 Gen_Id, Gen_Name);
2585 end if;
2586 end;
2587
2588 if Box_Present (N)
2589 or else No (Generic_Associations (N))
2590 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2591 then
2592 Associations := False;
2593 end if;
2594
2595 -- If there are no generic associations, the generic parameters appear
2596 -- as local entities and are instantiated like them. We copy the generic
2597 -- package declaration as if it were an instantiation, and analyze it
2598 -- like a regular package, except that we treat the formals as
2599 -- additional visible components.
2600
2601 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
2602
2603 if In_Extended_Main_Source_Unit (N) then
2604 Set_Is_Instantiated (Gen_Unit);
2605 Generate_Reference (Gen_Unit, N);
2606 end if;
2607
2608 Formal := New_Copy (Pack_Id);
2609 Create_Instantiation_Source (N, Gen_Unit, False, S_Adjustment);
2610
2611 begin
2612 -- Make local generic without formals. The formals will be replaced
2613 -- with internal declarations.
2614
2615 New_N := Build_Local_Package;
2616
2617 -- If there are errors in the parameter list, Analyze_Associations
2618 -- raises Instantiation_Error. Patch the declaration to prevent
2619 -- further exception propagation.
2620
2621 exception
2622 when Instantiation_Error =>
2623
2624 Enter_Name (Formal);
2625 Set_Ekind (Formal, E_Variable);
2626 Set_Etype (Formal, Any_Type);
2627 Restore_Hidden_Primitives (Vis_Prims_List);
2628
2629 if Parent_Installed then
2630 Remove_Parent;
2631 end if;
2632
2633 goto Leave;
2634 end;
2635
2636 Rewrite (N, New_N);
2637 Set_Defining_Unit_Name (Specification (New_N), Formal);
2638 Set_Generic_Parent (Specification (N), Gen_Unit);
2639 Set_Instance_Env (Gen_Unit, Formal);
2640 Set_Is_Generic_Instance (Formal);
2641
2642 Enter_Name (Formal);
2643 Set_Ekind (Formal, E_Package);
2644 Set_Etype (Formal, Standard_Void_Type);
2645 Set_Inner_Instances (Formal, New_Elmt_List);
2646 Push_Scope (Formal);
2647
2648 if Is_Child_Unit (Gen_Unit) and then Parent_Installed then
2649
2650 -- Similarly, we have to make the name of the formal visible in the
2651 -- parent instance, to resolve properly fully qualified names that
2652 -- may appear in the generic unit. The parent instance has been
2653 -- placed on the scope stack ahead of the current scope.
2654
2655 Parent_Instance := Scope_Stack.Table (Scope_Stack.Last - 1).Entity;
2656
2657 Renaming_In_Par :=
2658 Make_Defining_Identifier (Loc, Chars (Gen_Unit));
2659 Set_Ekind (Renaming_In_Par, E_Package);
2660 Set_Etype (Renaming_In_Par, Standard_Void_Type);
2661 Set_Scope (Renaming_In_Par, Parent_Instance);
2662 Set_Parent (Renaming_In_Par, Parent (Formal));
2663 Set_Renamed_Object (Renaming_In_Par, Formal);
2664 Append_Entity (Renaming_In_Par, Parent_Instance);
2665 end if;
2666
2667 Analyze (Specification (N));
2668
2669 -- The formals for which associations are provided are not visible
2670 -- outside of the formal package. The others are still declared by a
2671 -- formal parameter declaration.
2672
2673 -- If there are no associations, the only local entity to hide is the
2674 -- generated package renaming itself.
2675
2676 declare
2677 E : Entity_Id;
2678
2679 begin
2680 E := First_Entity (Formal);
2681 while Present (E) loop
2682 if Associations and then not Is_Generic_Formal (E) then
2683 Set_Is_Hidden (E);
2684 end if;
2685
2686 if Ekind (E) = E_Package and then Renamed_Entity (E) = Formal then
2687 Set_Is_Hidden (E);
2688 exit;
2689 end if;
2690
2691 Next_Entity (E);
2692 end loop;
2693 end;
2694
2695 End_Package_Scope (Formal);
2696 Restore_Hidden_Primitives (Vis_Prims_List);
2697
2698 if Parent_Installed then
2699 Remove_Parent;
2700 end if;
2701
2702 Restore_Env;
2703
2704 -- Inside the generic unit, the formal package is a regular package, but
2705 -- no body is needed for it. Note that after instantiation, the defining
2706 -- unit name we need is in the new tree and not in the original (see
2707 -- Package_Instantiation). A generic formal package is an instance, and
2708 -- can be used as an actual for an inner instance.
2709
2710 Set_Has_Completion (Formal, True);
2711
2712 -- Add semantic information to the original defining identifier.
2713 -- for ASIS use.
2714
2715 Set_Ekind (Pack_Id, E_Package);
2716 Set_Etype (Pack_Id, Standard_Void_Type);
2717 Set_Scope (Pack_Id, Scope (Formal));
2718 Set_Has_Completion (Pack_Id, True);
2719
2720 <<Leave>>
2721 if Has_Aspects (N) then
2722 Analyze_Aspect_Specifications (N, Pack_Id);
2723 end if;
2724 end Analyze_Formal_Package_Declaration;
2725
2726 ---------------------------------
2727 -- Analyze_Formal_Private_Type --
2728 ---------------------------------
2729
2730 procedure Analyze_Formal_Private_Type
2731 (N : Node_Id;
2732 T : Entity_Id;
2733 Def : Node_Id)
2734 is
2735 begin
2736 New_Private_Type (N, T, Def);
2737
2738 -- Set the size to an arbitrary but legal value
2739
2740 Set_Size_Info (T, Standard_Integer);
2741 Set_RM_Size (T, RM_Size (Standard_Integer));
2742 end Analyze_Formal_Private_Type;
2743
2744 ------------------------------------
2745 -- Analyze_Formal_Incomplete_Type --
2746 ------------------------------------
2747
2748 procedure Analyze_Formal_Incomplete_Type
2749 (T : Entity_Id;
2750 Def : Node_Id)
2751 is
2752 begin
2753 Enter_Name (T);
2754 Set_Ekind (T, E_Incomplete_Type);
2755 Set_Etype (T, T);
2756 Set_Private_Dependents (T, New_Elmt_List);
2757
2758 if Tagged_Present (Def) then
2759 Set_Is_Tagged_Type (T);
2760 Make_Class_Wide_Type (T);
2761 Set_Direct_Primitive_Operations (T, New_Elmt_List);
2762 end if;
2763 end Analyze_Formal_Incomplete_Type;
2764
2765 ----------------------------------------
2766 -- Analyze_Formal_Signed_Integer_Type --
2767 ----------------------------------------
2768
2769 procedure Analyze_Formal_Signed_Integer_Type
2770 (T : Entity_Id;
2771 Def : Node_Id)
2772 is
2773 Base : constant Entity_Id :=
2774 New_Internal_Entity
2775 (E_Signed_Integer_Type,
2776 Current_Scope,
2777 Sloc (Defining_Identifier (Parent (Def))), 'G');
2778
2779 begin
2780 Enter_Name (T);
2781
2782 Set_Ekind (T, E_Signed_Integer_Subtype);
2783 Set_Etype (T, Base);
2784 Set_Size_Info (T, Standard_Integer);
2785 Set_RM_Size (T, RM_Size (Standard_Integer));
2786 Set_Scalar_Range (T, Scalar_Range (Standard_Integer));
2787 Set_Is_Constrained (T);
2788
2789 Set_Is_Generic_Type (Base);
2790 Set_Size_Info (Base, Standard_Integer);
2791 Set_RM_Size (Base, RM_Size (Standard_Integer));
2792 Set_Etype (Base, Base);
2793 Set_Scalar_Range (Base, Scalar_Range (Standard_Integer));
2794 Set_Parent (Base, Parent (Def));
2795 end Analyze_Formal_Signed_Integer_Type;
2796
2797 -------------------------------------------
2798 -- Analyze_Formal_Subprogram_Declaration --
2799 -------------------------------------------
2800
2801 procedure Analyze_Formal_Subprogram_Declaration (N : Node_Id) is
2802 Spec : constant Node_Id := Specification (N);
2803 Def : constant Node_Id := Default_Name (N);
2804 Nam : constant Entity_Id := Defining_Unit_Name (Spec);
2805 Subp : Entity_Id;
2806
2807 begin
2808 if Nam = Error then
2809 return;
2810 end if;
2811
2812 if Nkind (Nam) = N_Defining_Program_Unit_Name then
2813 Error_Msg_N ("name of formal subprogram must be a direct name", Nam);
2814 goto Leave;
2815 end if;
2816
2817 Analyze_Subprogram_Declaration (N);
2818 Set_Is_Formal_Subprogram (Nam);
2819 Set_Has_Completion (Nam);
2820
2821 if Nkind (N) = N_Formal_Abstract_Subprogram_Declaration then
2822 Set_Is_Abstract_Subprogram (Nam);
2823
2824 Set_Is_Dispatching_Operation (Nam);
2825
2826 -- A formal abstract procedure cannot have a null default
2827 -- (RM 12.6(4 1.2)).
2828
2829 if Nkind (Spec) = N_Procedure_Specification
2830 and then Null_Present (Spec)
2831 then
2832 Error_Msg_N
2833 ("a formal abstract subprogram cannot default to null", Spec);
2834 end if;
2835
2836 declare
2837 Ctrl_Type : constant Entity_Id := Find_Dispatching_Type (Nam);
2838 begin
2839 if No (Ctrl_Type) then
2840 Error_Msg_N
2841 ("abstract formal subprogram must have a controlling type",
2842 N);
2843
2844 elsif Ada_Version >= Ada_2012
2845 and then Is_Incomplete_Type (Ctrl_Type)
2846 then
2847 Error_Msg_NE
2848 ("controlling type of abstract formal subprogram cannot "
2849 & "be incomplete type", N, Ctrl_Type);
2850
2851 else
2852 Check_Controlling_Formals (Ctrl_Type, Nam);
2853 end if;
2854 end;
2855 end if;
2856
2857 -- Default name is resolved at the point of instantiation
2858
2859 if Box_Present (N) then
2860 null;
2861
2862 -- Else default is bound at the point of generic declaration
2863
2864 elsif Present (Def) then
2865 if Nkind (Def) = N_Operator_Symbol then
2866 Find_Direct_Name (Def);
2867
2868 elsif Nkind (Def) /= N_Attribute_Reference then
2869 Analyze (Def);
2870
2871 else
2872 -- For an attribute reference, analyze the prefix and verify
2873 -- that it has the proper profile for the subprogram.
2874
2875 Analyze (Prefix (Def));
2876 Valid_Default_Attribute (Nam, Def);
2877 goto Leave;
2878 end if;
2879
2880 -- Default name may be overloaded, in which case the interpretation
2881 -- with the correct profile must be selected, as for a renaming.
2882 -- If the definition is an indexed component, it must denote a
2883 -- member of an entry family. If it is a selected component, it
2884 -- can be a protected operation.
2885
2886 if Etype (Def) = Any_Type then
2887 goto Leave;
2888
2889 elsif Nkind (Def) = N_Selected_Component then
2890 if not Is_Overloadable (Entity (Selector_Name (Def))) then
2891 Error_Msg_N ("expect valid subprogram name as default", Def);
2892 end if;
2893
2894 elsif Nkind (Def) = N_Indexed_Component then
2895 if Is_Entity_Name (Prefix (Def)) then
2896 if Ekind (Entity (Prefix (Def))) /= E_Entry_Family then
2897 Error_Msg_N ("expect valid subprogram name as default", Def);
2898 end if;
2899
2900 elsif Nkind (Prefix (Def)) = N_Selected_Component then
2901 if Ekind (Entity (Selector_Name (Prefix (Def)))) /=
2902 E_Entry_Family
2903 then
2904 Error_Msg_N ("expect valid subprogram name as default", Def);
2905 end if;
2906
2907 else
2908 Error_Msg_N ("expect valid subprogram name as default", Def);
2909 goto Leave;
2910 end if;
2911
2912 elsif Nkind (Def) = N_Character_Literal then
2913
2914 -- Needs some type checks: subprogram should be parameterless???
2915
2916 Resolve (Def, (Etype (Nam)));
2917
2918 elsif not Is_Entity_Name (Def)
2919 or else not Is_Overloadable (Entity (Def))
2920 then
2921 Error_Msg_N ("expect valid subprogram name as default", Def);
2922 goto Leave;
2923
2924 elsif not Is_Overloaded (Def) then
2925 Subp := Entity (Def);
2926
2927 if Subp = Nam then
2928 Error_Msg_N ("premature usage of formal subprogram", Def);
2929
2930 elsif not Entity_Matches_Spec (Subp, Nam) then
2931 Error_Msg_N ("no visible entity matches specification", Def);
2932 end if;
2933
2934 -- More than one interpretation, so disambiguate as for a renaming
2935
2936 else
2937 declare
2938 I : Interp_Index;
2939 I1 : Interp_Index := 0;
2940 It : Interp;
2941 It1 : Interp;
2942
2943 begin
2944 Subp := Any_Id;
2945 Get_First_Interp (Def, I, It);
2946 while Present (It.Nam) loop
2947 if Entity_Matches_Spec (It.Nam, Nam) then
2948 if Subp /= Any_Id then
2949 It1 := Disambiguate (Def, I1, I, Etype (Subp));
2950
2951 if It1 = No_Interp then
2952 Error_Msg_N ("ambiguous default subprogram", Def);
2953 else
2954 Subp := It1.Nam;
2955 end if;
2956
2957 exit;
2958
2959 else
2960 I1 := I;
2961 Subp := It.Nam;
2962 end if;
2963 end if;
2964
2965 Get_Next_Interp (I, It);
2966 end loop;
2967 end;
2968
2969 if Subp /= Any_Id then
2970
2971 -- Subprogram found, generate reference to it
2972
2973 Set_Entity (Def, Subp);
2974 Generate_Reference (Subp, Def);
2975
2976 if Subp = Nam then
2977 Error_Msg_N ("premature usage of formal subprogram", Def);
2978
2979 elsif Ekind (Subp) /= E_Operator then
2980 Check_Mode_Conformant (Subp, Nam);
2981 end if;
2982
2983 else
2984 Error_Msg_N ("no visible subprogram matches specification", N);
2985 end if;
2986 end if;
2987 end if;
2988
2989 <<Leave>>
2990 if Has_Aspects (N) then
2991 Analyze_Aspect_Specifications (N, Nam);
2992 end if;
2993
2994 end Analyze_Formal_Subprogram_Declaration;
2995
2996 -------------------------------------
2997 -- Analyze_Formal_Type_Declaration --
2998 -------------------------------------
2999
3000 procedure Analyze_Formal_Type_Declaration (N : Node_Id) is
3001 Def : constant Node_Id := Formal_Type_Definition (N);
3002 T : Entity_Id;
3003
3004 begin
3005 T := Defining_Identifier (N);
3006
3007 if Present (Discriminant_Specifications (N))
3008 and then Nkind (Def) /= N_Formal_Private_Type_Definition
3009 then
3010 Error_Msg_N
3011 ("discriminants not allowed for this formal type", T);
3012 end if;
3013
3014 -- Enter the new name, and branch to specific routine
3015
3016 case Nkind (Def) is
3017 when N_Formal_Private_Type_Definition =>
3018 Analyze_Formal_Private_Type (N, T, Def);
3019
3020 when N_Formal_Derived_Type_Definition =>
3021 Analyze_Formal_Derived_Type (N, T, Def);
3022
3023 when N_Formal_Incomplete_Type_Definition =>
3024 Analyze_Formal_Incomplete_Type (T, Def);
3025
3026 when N_Formal_Discrete_Type_Definition =>
3027 Analyze_Formal_Discrete_Type (T, Def);
3028
3029 when N_Formal_Signed_Integer_Type_Definition =>
3030 Analyze_Formal_Signed_Integer_Type (T, Def);
3031
3032 when N_Formal_Modular_Type_Definition =>
3033 Analyze_Formal_Modular_Type (T, Def);
3034
3035 when N_Formal_Floating_Point_Definition =>
3036 Analyze_Formal_Floating_Type (T, Def);
3037
3038 when N_Formal_Ordinary_Fixed_Point_Definition =>
3039 Analyze_Formal_Ordinary_Fixed_Point_Type (T, Def);
3040
3041 when N_Formal_Decimal_Fixed_Point_Definition =>
3042 Analyze_Formal_Decimal_Fixed_Point_Type (T, Def);
3043
3044 when N_Array_Type_Definition =>
3045 Analyze_Formal_Array_Type (T, Def);
3046
3047 when N_Access_To_Object_Definition |
3048 N_Access_Function_Definition |
3049 N_Access_Procedure_Definition =>
3050 Analyze_Generic_Access_Type (T, Def);
3051
3052 -- Ada 2005: a interface declaration is encoded as an abstract
3053 -- record declaration or a abstract type derivation.
3054
3055 when N_Record_Definition =>
3056 Analyze_Formal_Interface_Type (N, T, Def);
3057
3058 when N_Derived_Type_Definition =>
3059 Analyze_Formal_Derived_Interface_Type (N, T, Def);
3060
3061 when N_Error =>
3062 null;
3063
3064 when others =>
3065 raise Program_Error;
3066
3067 end case;
3068
3069 Set_Is_Generic_Type (T);
3070
3071 if Has_Aspects (N) then
3072 Analyze_Aspect_Specifications (N, T);
3073 end if;
3074 end Analyze_Formal_Type_Declaration;
3075
3076 ------------------------------------
3077 -- Analyze_Function_Instantiation --
3078 ------------------------------------
3079
3080 procedure Analyze_Function_Instantiation (N : Node_Id) is
3081 begin
3082 Analyze_Subprogram_Instantiation (N, E_Function);
3083 end Analyze_Function_Instantiation;
3084
3085 ---------------------------------
3086 -- Analyze_Generic_Access_Type --
3087 ---------------------------------
3088
3089 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id) is
3090 begin
3091 Enter_Name (T);
3092
3093 if Nkind (Def) = N_Access_To_Object_Definition then
3094 Access_Type_Declaration (T, Def);
3095
3096 if Is_Incomplete_Or_Private_Type (Designated_Type (T))
3097 and then No (Full_View (Designated_Type (T)))
3098 and then not Is_Generic_Type (Designated_Type (T))
3099 then
3100 Error_Msg_N ("premature usage of incomplete type", Def);
3101
3102 elsif not Is_Entity_Name (Subtype_Indication (Def)) then
3103 Error_Msg_N
3104 ("only a subtype mark is allowed in a formal", Def);
3105 end if;
3106
3107 else
3108 Access_Subprogram_Declaration (T, Def);
3109 end if;
3110 end Analyze_Generic_Access_Type;
3111
3112 ---------------------------------
3113 -- Analyze_Generic_Formal_Part --
3114 ---------------------------------
3115
3116 procedure Analyze_Generic_Formal_Part (N : Node_Id) is
3117 Gen_Parm_Decl : Node_Id;
3118
3119 begin
3120 -- The generic formals are processed in the scope of the generic unit,
3121 -- where they are immediately visible. The scope is installed by the
3122 -- caller.
3123
3124 Gen_Parm_Decl := First (Generic_Formal_Declarations (N));
3125 while Present (Gen_Parm_Decl) loop
3126 Analyze (Gen_Parm_Decl);
3127 Next (Gen_Parm_Decl);
3128 end loop;
3129
3130 Generate_Reference_To_Generic_Formals (Current_Scope);
3131 end Analyze_Generic_Formal_Part;
3132
3133 ------------------------------------------
3134 -- Analyze_Generic_Package_Declaration --
3135 ------------------------------------------
3136
3137 procedure Analyze_Generic_Package_Declaration (N : Node_Id) is
3138 Loc : constant Source_Ptr := Sloc (N);
3139 Decls : constant List_Id :=
3140 Visible_Declarations (Specification (N));
3141 Decl : Node_Id;
3142 Id : Entity_Id;
3143 New_N : Node_Id;
3144 Renaming : Node_Id;
3145 Save_Parent : Node_Id;
3146
3147 begin
3148 Check_SPARK_05_Restriction ("generic is not allowed", N);
3149
3150 -- We introduce a renaming of the enclosing package, to have a usable
3151 -- entity as the prefix of an expanded name for a local entity of the
3152 -- form Par.P.Q, where P is the generic package. This is because a local
3153 -- entity named P may hide it, so that the usual visibility rules in
3154 -- the instance will not resolve properly.
3155
3156 Renaming :=
3157 Make_Package_Renaming_Declaration (Loc,
3158 Defining_Unit_Name =>
3159 Make_Defining_Identifier (Loc,
3160 Chars => New_External_Name (Chars (Defining_Entity (N)), "GH")),
3161 Name =>
3162 Make_Identifier (Loc, Chars (Defining_Entity (N))));
3163
3164 if Present (Decls) then
3165 Decl := First (Decls);
3166 while Present (Decl) and then Nkind (Decl) = N_Pragma loop
3167 Next (Decl);
3168 end loop;
3169
3170 if Present (Decl) then
3171 Insert_Before (Decl, Renaming);
3172 else
3173 Append (Renaming, Visible_Declarations (Specification (N)));
3174 end if;
3175
3176 else
3177 Set_Visible_Declarations (Specification (N), New_List (Renaming));
3178 end if;
3179
3180 -- Create copy of generic unit, and save for instantiation. If the unit
3181 -- is a child unit, do not copy the specifications for the parent, which
3182 -- are not part of the generic tree.
3183
3184 Save_Parent := Parent_Spec (N);
3185 Set_Parent_Spec (N, Empty);
3186
3187 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
3188 Set_Parent_Spec (New_N, Save_Parent);
3189 Rewrite (N, New_N);
3190
3191 -- Once the contents of the generic copy and the template are swapped,
3192 -- do the same for their respective aspect specifications.
3193
3194 Exchange_Aspects (N, New_N);
3195
3196 -- Collect all contract-related source pragmas found within the template
3197 -- and attach them to the contract of the package spec. This contract is
3198 -- used in the capture of global references within annotations.
3199
3200 Create_Generic_Contract (N);
3201
3202 Id := Defining_Entity (N);
3203 Generate_Definition (Id);
3204
3205 -- Expansion is not applied to generic units
3206
3207 Start_Generic;
3208
3209 Enter_Name (Id);
3210 Set_Ekind (Id, E_Generic_Package);
3211 Set_Etype (Id, Standard_Void_Type);
3212
3213 -- A generic package declared within a Ghost region is rendered Ghost
3214 -- (SPARK RM 6.9(2)).
3215
3216 if Ghost_Mode > None then
3217 Set_Is_Ghost_Entity (Id);
3218 end if;
3219
3220 -- Analyze aspects now, so that generated pragmas appear in the
3221 -- declarations before building and analyzing the generic copy.
3222
3223 if Has_Aspects (N) then
3224 Analyze_Aspect_Specifications (N, Id);
3225 end if;
3226
3227 Push_Scope (Id);
3228 Enter_Generic_Scope (Id);
3229 Set_Inner_Instances (Id, New_Elmt_List);
3230
3231 Set_Categorization_From_Pragmas (N);
3232 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3233
3234 -- Link the declaration of the generic homonym in the generic copy to
3235 -- the package it renames, so that it is always resolved properly.
3236
3237 Set_Generic_Homonym (Id, Defining_Unit_Name (Renaming));
3238 Set_Entity (Associated_Node (Name (Renaming)), Id);
3239
3240 -- For a library unit, we have reconstructed the entity for the unit,
3241 -- and must reset it in the library tables.
3242
3243 if Nkind (Parent (N)) = N_Compilation_Unit then
3244 Set_Cunit_Entity (Current_Sem_Unit, Id);
3245 end if;
3246
3247 Analyze_Generic_Formal_Part (N);
3248
3249 -- After processing the generic formals, analysis proceeds as for a
3250 -- non-generic package.
3251
3252 Analyze (Specification (N));
3253
3254 Validate_Categorization_Dependency (N, Id);
3255
3256 End_Generic;
3257
3258 End_Package_Scope (Id);
3259 Exit_Generic_Scope (Id);
3260
3261 if Nkind (Parent (N)) /= N_Compilation_Unit then
3262 Move_Freeze_Nodes (Id, N, Visible_Declarations (Specification (N)));
3263 Move_Freeze_Nodes (Id, N, Private_Declarations (Specification (N)));
3264 Move_Freeze_Nodes (Id, N, Generic_Formal_Declarations (N));
3265
3266 else
3267 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
3268 Validate_RT_RAT_Component (N);
3269
3270 -- If this is a spec without a body, check that generic parameters
3271 -- are referenced.
3272
3273 if not Body_Required (Parent (N)) then
3274 Check_References (Id);
3275 end if;
3276 end if;
3277
3278 -- If there is a specified storage pool in the context, create an
3279 -- aspect on the package declaration, so that it is used in any
3280 -- instance that does not override it.
3281
3282 if Present (Default_Pool) then
3283 declare
3284 ASN : Node_Id;
3285
3286 begin
3287 ASN :=
3288 Make_Aspect_Specification (Loc,
3289 Identifier => Make_Identifier (Loc, Name_Default_Storage_Pool),
3290 Expression => New_Copy (Default_Pool));
3291
3292 if No (Aspect_Specifications (Specification (N))) then
3293 Set_Aspect_Specifications (Specification (N), New_List (ASN));
3294 else
3295 Append (ASN, Aspect_Specifications (Specification (N)));
3296 end if;
3297 end;
3298 end if;
3299 end Analyze_Generic_Package_Declaration;
3300
3301 --------------------------------------------
3302 -- Analyze_Generic_Subprogram_Declaration --
3303 --------------------------------------------
3304
3305 procedure Analyze_Generic_Subprogram_Declaration (N : Node_Id) is
3306 Formals : List_Id;
3307 Id : Entity_Id;
3308 New_N : Node_Id;
3309 Result_Type : Entity_Id;
3310 Save_Parent : Node_Id;
3311 Spec : Node_Id;
3312 Typ : Entity_Id;
3313
3314 begin
3315 Check_SPARK_05_Restriction ("generic is not allowed", N);
3316
3317 -- Create copy of generic unit, and save for instantiation. If the unit
3318 -- is a child unit, do not copy the specifications for the parent, which
3319 -- are not part of the generic tree.
3320
3321 Save_Parent := Parent_Spec (N);
3322 Set_Parent_Spec (N, Empty);
3323
3324 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
3325 Set_Parent_Spec (New_N, Save_Parent);
3326 Rewrite (N, New_N);
3327
3328 -- Once the contents of the generic copy and the template are swapped,
3329 -- do the same for their respective aspect specifications.
3330
3331 Exchange_Aspects (N, New_N);
3332
3333 -- Collect all contract-related source pragmas found within the template
3334 -- and attach them to the contract of the subprogram spec. This contract
3335 -- is used in the capture of global references within annotations.
3336
3337 Create_Generic_Contract (N);
3338
3339 Spec := Specification (N);
3340 Id := Defining_Entity (Spec);
3341 Generate_Definition (Id);
3342
3343 if Nkind (Id) = N_Defining_Operator_Symbol then
3344 Error_Msg_N
3345 ("operator symbol not allowed for generic subprogram", Id);
3346 end if;
3347
3348 Start_Generic;
3349
3350 Enter_Name (Id);
3351 Set_Scope_Depth_Value (Id, Scope_Depth (Current_Scope) + 1);
3352
3353 -- Analyze the aspects of the generic copy to ensure that all generated
3354 -- pragmas (if any) perform their semantic effects.
3355
3356 if Has_Aspects (N) then
3357 Analyze_Aspect_Specifications (N, Id);
3358 end if;
3359
3360 Push_Scope (Id);
3361 Enter_Generic_Scope (Id);
3362 Set_Inner_Instances (Id, New_Elmt_List);
3363 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3364
3365 Analyze_Generic_Formal_Part (N);
3366
3367 Formals := Parameter_Specifications (Spec);
3368
3369 if Nkind (Spec) = N_Function_Specification then
3370 Set_Ekind (Id, E_Generic_Function);
3371 else
3372 Set_Ekind (Id, E_Generic_Procedure);
3373 end if;
3374
3375 if Present (Formals) then
3376 Process_Formals (Formals, Spec);
3377 end if;
3378
3379 if Nkind (Spec) = N_Function_Specification then
3380 if Nkind (Result_Definition (Spec)) = N_Access_Definition then
3381 Result_Type := Access_Definition (Spec, Result_Definition (Spec));
3382 Set_Etype (Id, Result_Type);
3383
3384 -- Check restriction imposed by AI05-073: a generic function
3385 -- cannot return an abstract type or an access to such.
3386
3387 -- This is a binding interpretation should it apply to earlier
3388 -- versions of Ada as well as Ada 2012???
3389
3390 if Is_Abstract_Type (Designated_Type (Result_Type))
3391 and then Ada_Version >= Ada_2012
3392 then
3393 Error_Msg_N
3394 ("generic function cannot have an access result "
3395 & "that designates an abstract type", Spec);
3396 end if;
3397
3398 else
3399 Find_Type (Result_Definition (Spec));
3400 Typ := Entity (Result_Definition (Spec));
3401
3402 if Is_Abstract_Type (Typ)
3403 and then Ada_Version >= Ada_2012
3404 then
3405 Error_Msg_N
3406 ("generic function cannot have abstract result type", Spec);
3407 end if;
3408
3409 -- If a null exclusion is imposed on the result type, then create
3410 -- a null-excluding itype (an access subtype) and use it as the
3411 -- function's Etype.
3412
3413 if Is_Access_Type (Typ)
3414 and then Null_Exclusion_Present (Spec)
3415 then
3416 Set_Etype (Id,
3417 Create_Null_Excluding_Itype
3418 (T => Typ,
3419 Related_Nod => Spec,
3420 Scope_Id => Defining_Unit_Name (Spec)));
3421 else
3422 Set_Etype (Id, Typ);
3423 end if;
3424 end if;
3425
3426 else
3427 Set_Etype (Id, Standard_Void_Type);
3428 end if;
3429
3430 -- A generic subprogram declared within a Ghost region is rendered Ghost
3431 -- (SPARK RM 6.9(2)).
3432
3433 if Ghost_Mode > None then
3434 Set_Is_Ghost_Entity (Id);
3435 end if;
3436
3437 -- For a library unit, we have reconstructed the entity for the unit,
3438 -- and must reset it in the library tables. We also make sure that
3439 -- Body_Required is set properly in the original compilation unit node.
3440
3441 if Nkind (Parent (N)) = N_Compilation_Unit then
3442 Set_Cunit_Entity (Current_Sem_Unit, Id);
3443 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
3444 end if;
3445
3446 Set_Categorization_From_Pragmas (N);
3447 Validate_Categorization_Dependency (N, Id);
3448
3449 -- Capture all global references that occur within the profile of the
3450 -- generic subprogram. Aspects are not part of this processing because
3451 -- they must be delayed. If processed now, Save_Global_References will
3452 -- destroy the Associated_Node links and prevent the capture of global
3453 -- references when the contract of the generic subprogram is analyzed.
3454
3455 Save_Global_References (Original_Node (N));
3456
3457 End_Generic;
3458 End_Scope;
3459 Exit_Generic_Scope (Id);
3460 Generate_Reference_To_Formals (Id);
3461
3462 List_Inherited_Pre_Post_Aspects (Id);
3463 end Analyze_Generic_Subprogram_Declaration;
3464
3465 -----------------------------------
3466 -- Analyze_Package_Instantiation --
3467 -----------------------------------
3468
3469 procedure Analyze_Package_Instantiation (N : Node_Id) is
3470 Loc : constant Source_Ptr := Sloc (N);
3471 Gen_Id : constant Node_Id := Name (N);
3472
3473 Act_Decl : Node_Id;
3474 Act_Decl_Name : Node_Id;
3475 Act_Decl_Id : Entity_Id;
3476 Act_Spec : Node_Id;
3477 Act_Tree : Node_Id;
3478
3479 Gen_Decl : Node_Id;
3480 Gen_Spec : Node_Id;
3481 Gen_Unit : Entity_Id;
3482
3483 Is_Actual_Pack : constant Boolean :=
3484 Is_Internal (Defining_Entity (N));
3485
3486 Env_Installed : Boolean := False;
3487 Parent_Installed : Boolean := False;
3488 Renaming_List : List_Id;
3489 Unit_Renaming : Node_Id;
3490 Needs_Body : Boolean;
3491 Inline_Now : Boolean := False;
3492 Has_Inline_Always : Boolean := False;
3493
3494 Save_IPSM : constant Boolean := Ignore_Pragma_SPARK_Mode;
3495 -- Save flag Ignore_Pragma_SPARK_Mode for restore on exit
3496
3497 Save_SM : constant SPARK_Mode_Type := SPARK_Mode;
3498 Save_SMP : constant Node_Id := SPARK_Mode_Pragma;
3499 -- Save the SPARK_Mode-related data for restore on exit
3500
3501 Save_Style_Check : constant Boolean := Style_Check;
3502 -- Save style check mode for restore on exit
3503
3504 procedure Delay_Descriptors (E : Entity_Id);
3505 -- Delay generation of subprogram descriptors for given entity
3506
3507 function Might_Inline_Subp return Boolean;
3508 -- If inlining is active and the generic contains inlined subprograms,
3509 -- we instantiate the body. This may cause superfluous instantiations,
3510 -- but it is simpler than detecting the need for the body at the point
3511 -- of inlining, when the context of the instance is not available.
3512
3513 -----------------------
3514 -- Delay_Descriptors --
3515 -----------------------
3516
3517 procedure Delay_Descriptors (E : Entity_Id) is
3518 begin
3519 if not Delay_Subprogram_Descriptors (E) then
3520 Set_Delay_Subprogram_Descriptors (E);
3521 Pending_Descriptor.Append (E);
3522 end if;
3523 end Delay_Descriptors;
3524
3525 -----------------------
3526 -- Might_Inline_Subp --
3527 -----------------------
3528
3529 function Might_Inline_Subp return Boolean is
3530 E : Entity_Id;
3531
3532 begin
3533 if not Inline_Processing_Required then
3534 return False;
3535
3536 else
3537 E := First_Entity (Gen_Unit);
3538 while Present (E) loop
3539 if Is_Subprogram (E) and then Is_Inlined (E) then
3540 -- Remember if there are any subprograms with Inline_Always
3541
3542 if Has_Pragma_Inline_Always (E) then
3543 Has_Inline_Always := True;
3544 end if;
3545
3546 return True;
3547 end if;
3548
3549 Next_Entity (E);
3550 end loop;
3551 end if;
3552
3553 return False;
3554 end Might_Inline_Subp;
3555
3556 -- Local declarations
3557
3558 Vis_Prims_List : Elist_Id := No_Elist;
3559 -- List of primitives made temporarily visible in the instantiation
3560 -- to match the visibility of the formal type
3561
3562 -- Start of processing for Analyze_Package_Instantiation
3563
3564 begin
3565 Check_SPARK_05_Restriction ("generic is not allowed", N);
3566
3567 -- Very first thing: check for Text_IO sp[ecial unit in case we are
3568 -- instantiating one of the children of [[Wide_]Wide_]Text_IO.
3569
3570 Check_Text_IO_Special_Unit (Name (N));
3571
3572 -- Make node global for error reporting
3573
3574 Instantiation_Node := N;
3575
3576 -- Turn off style checking in instances. If the check is enabled on the
3577 -- generic unit, a warning in an instance would just be noise. If not
3578 -- enabled on the generic, then a warning in an instance is just wrong.
3579
3580 Style_Check := False;
3581
3582 -- Case of instantiation of a generic package
3583
3584 if Nkind (N) = N_Package_Instantiation then
3585 Act_Decl_Id := New_Copy (Defining_Entity (N));
3586 Set_Comes_From_Source (Act_Decl_Id, True);
3587
3588 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name then
3589 Act_Decl_Name :=
3590 Make_Defining_Program_Unit_Name (Loc,
3591 Name =>
3592 New_Copy_Tree (Name (Defining_Unit_Name (N))),
3593 Defining_Identifier => Act_Decl_Id);
3594 else
3595 Act_Decl_Name := Act_Decl_Id;
3596 end if;
3597
3598 -- Case of instantiation of a formal package
3599
3600 else
3601 Act_Decl_Id := Defining_Identifier (N);
3602 Act_Decl_Name := Act_Decl_Id;
3603 end if;
3604
3605 Generate_Definition (Act_Decl_Id);
3606 Set_Ekind (Act_Decl_Id, E_Package);
3607
3608 -- Initialize list of incomplete actuals before analysis
3609
3610 Set_Incomplete_Actuals (Act_Decl_Id, New_Elmt_List);
3611
3612 Preanalyze_Actuals (N, Act_Decl_Id);
3613
3614 Init_Env;
3615 Env_Installed := True;
3616
3617 -- Reset renaming map for formal types. The mapping is established
3618 -- when analyzing the generic associations, but some mappings are
3619 -- inherited from formal packages of parent units, and these are
3620 -- constructed when the parents are installed.
3621
3622 Generic_Renamings.Set_Last (0);
3623 Generic_Renamings_HTable.Reset;
3624
3625 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
3626 Gen_Unit := Entity (Gen_Id);
3627
3628 -- Verify that it is the name of a generic package
3629
3630 -- A visibility glitch: if the instance is a child unit and the generic
3631 -- is the generic unit of a parent instance (i.e. both the parent and
3632 -- the child units are instances of the same package) the name now
3633 -- denotes the renaming within the parent, not the intended generic
3634 -- unit. See if there is a homonym that is the desired generic. The
3635 -- renaming declaration must be visible inside the instance of the
3636 -- child, but not when analyzing the name in the instantiation itself.
3637
3638 if Ekind (Gen_Unit) = E_Package
3639 and then Present (Renamed_Entity (Gen_Unit))
3640 and then In_Open_Scopes (Renamed_Entity (Gen_Unit))
3641 and then Is_Generic_Instance (Renamed_Entity (Gen_Unit))
3642 and then Present (Homonym (Gen_Unit))
3643 then
3644 Gen_Unit := Homonym (Gen_Unit);
3645 end if;
3646
3647 if Etype (Gen_Unit) = Any_Type then
3648 Restore_Env;
3649 goto Leave;
3650
3651 elsif Ekind (Gen_Unit) /= E_Generic_Package then
3652
3653 -- Ada 2005 (AI-50217): Cannot use instance in limited with_clause
3654
3655 if From_Limited_With (Gen_Unit) then
3656 Error_Msg_N
3657 ("cannot instantiate a limited withed package", Gen_Id);
3658 else
3659 Error_Msg_NE
3660 ("& is not the name of a generic package", Gen_Id, Gen_Unit);
3661 end if;
3662
3663 Restore_Env;
3664 goto Leave;
3665 end if;
3666
3667 if In_Extended_Main_Source_Unit (N) then
3668 Set_Is_Instantiated (Gen_Unit);
3669 Generate_Reference (Gen_Unit, N);
3670
3671 if Present (Renamed_Object (Gen_Unit)) then
3672 Set_Is_Instantiated (Renamed_Object (Gen_Unit));
3673 Generate_Reference (Renamed_Object (Gen_Unit), N);
3674 end if;
3675 end if;
3676
3677 if Nkind (Gen_Id) = N_Identifier
3678 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
3679 then
3680 Error_Msg_NE
3681 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
3682
3683 elsif Nkind (Gen_Id) = N_Expanded_Name
3684 and then Is_Child_Unit (Gen_Unit)
3685 and then Nkind (Prefix (Gen_Id)) = N_Identifier
3686 and then Chars (Act_Decl_Id) = Chars (Prefix (Gen_Id))
3687 then
3688 Error_Msg_N
3689 ("& is hidden within declaration of instance ", Prefix (Gen_Id));
3690 end if;
3691
3692 Set_Entity (Gen_Id, Gen_Unit);
3693
3694 -- If generic is a renaming, get original generic unit
3695
3696 if Present (Renamed_Object (Gen_Unit))
3697 and then Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Package
3698 then
3699 Gen_Unit := Renamed_Object (Gen_Unit);
3700 end if;
3701
3702 -- Verify that there are no circular instantiations
3703
3704 if In_Open_Scopes (Gen_Unit) then
3705 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
3706 Restore_Env;
3707 goto Leave;
3708
3709 elsif Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
3710 Error_Msg_Node_2 := Current_Scope;
3711 Error_Msg_NE
3712 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
3713 Circularity_Detected := True;
3714 Restore_Env;
3715 goto Leave;
3716
3717 else
3718 -- If the context of the instance is subject to SPARK_Mode "off",
3719 -- set the global flag which signals Analyze_Pragma to ignore all
3720 -- SPARK_Mode pragmas within the instance.
3721
3722 if SPARK_Mode = Off then
3723 Ignore_Pragma_SPARK_Mode := True;
3724 end if;
3725
3726 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
3727 Gen_Spec := Specification (Gen_Decl);
3728
3729 -- Initialize renamings map, for error checking, and the list that
3730 -- holds private entities whose views have changed between generic
3731 -- definition and instantiation. If this is the instance created to
3732 -- validate an actual package, the instantiation environment is that
3733 -- of the enclosing instance.
3734
3735 Create_Instantiation_Source (N, Gen_Unit, False, S_Adjustment);
3736
3737 -- Copy original generic tree, to produce text for instantiation
3738
3739 Act_Tree :=
3740 Copy_Generic_Node
3741 (Original_Node (Gen_Decl), Empty, Instantiating => True);
3742
3743 Act_Spec := Specification (Act_Tree);
3744
3745 -- If this is the instance created to validate an actual package,
3746 -- only the formals matter, do not examine the package spec itself.
3747
3748 if Is_Actual_Pack then
3749 Set_Visible_Declarations (Act_Spec, New_List);
3750 Set_Private_Declarations (Act_Spec, New_List);
3751 end if;
3752
3753 Renaming_List :=
3754 Analyze_Associations
3755 (I_Node => N,
3756 Formals => Generic_Formal_Declarations (Act_Tree),
3757 F_Copy => Generic_Formal_Declarations (Gen_Decl));
3758
3759 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
3760
3761 Set_Instance_Env (Gen_Unit, Act_Decl_Id);
3762 Set_Defining_Unit_Name (Act_Spec, Act_Decl_Name);
3763 Set_Is_Generic_Instance (Act_Decl_Id);
3764 Set_Generic_Parent (Act_Spec, Gen_Unit);
3765
3766 -- References to the generic in its own declaration or its body are
3767 -- references to the instance. Add a renaming declaration for the
3768 -- generic unit itself. This declaration, as well as the renaming
3769 -- declarations for the generic formals, must remain private to the
3770 -- unit: the formals, because this is the language semantics, and
3771 -- the unit because its use is an artifact of the implementation.
3772
3773 Unit_Renaming :=
3774 Make_Package_Renaming_Declaration (Loc,
3775 Defining_Unit_Name =>
3776 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
3777 Name => New_Occurrence_Of (Act_Decl_Id, Loc));
3778
3779 Append (Unit_Renaming, Renaming_List);
3780
3781 -- The renaming declarations are the first local declarations of the
3782 -- new unit.
3783
3784 if Is_Non_Empty_List (Visible_Declarations (Act_Spec)) then
3785 Insert_List_Before
3786 (First (Visible_Declarations (Act_Spec)), Renaming_List);
3787 else
3788 Set_Visible_Declarations (Act_Spec, Renaming_List);
3789 end if;
3790
3791 Act_Decl := Make_Package_Declaration (Loc, Specification => Act_Spec);
3792
3793 -- Propagate the aspect specifications from the package declaration
3794 -- template to the instantiated version of the package declaration.
3795
3796 if Has_Aspects (Act_Tree) then
3797 Set_Aspect_Specifications (Act_Decl,
3798 New_Copy_List_Tree (Aspect_Specifications (Act_Tree)));
3799 end if;
3800
3801 -- The generic may have a generated Default_Storage_Pool aspect,
3802 -- set at the point of generic declaration. If the instance has
3803 -- that aspect, it overrides the one inherited from the generic.
3804
3805 if Has_Aspects (Gen_Spec) then
3806 if No (Aspect_Specifications (N)) then
3807 Set_Aspect_Specifications (N,
3808 (New_Copy_List_Tree
3809 (Aspect_Specifications (Gen_Spec))));
3810
3811 else
3812 declare
3813 ASN1, ASN2 : Node_Id;
3814
3815 begin
3816 ASN1 := First (Aspect_Specifications (N));
3817 while Present (ASN1) loop
3818 if Chars (Identifier (ASN1)) = Name_Default_Storage_Pool
3819 then
3820 -- If generic carries a default storage pool, remove
3821 -- it in favor of the instance one.
3822
3823 ASN2 := First (Aspect_Specifications (Gen_Spec));
3824 while Present (ASN2) loop
3825 if Chars (Identifier (ASN2)) =
3826 Name_Default_Storage_Pool
3827 then
3828 Remove (ASN2);
3829 exit;
3830 end if;
3831
3832 Next (ASN2);
3833 end loop;
3834 end if;
3835
3836 Next (ASN1);
3837 end loop;
3838
3839 Prepend_List_To (Aspect_Specifications (N),
3840 (New_Copy_List_Tree
3841 (Aspect_Specifications (Gen_Spec))));
3842 end;
3843 end if;
3844 end if;
3845
3846 -- Save the instantiation node, for subsequent instantiation of the
3847 -- body, if there is one and we are generating code for the current
3848 -- unit. Mark unit as having a body (avoids premature error message).
3849
3850 -- We instantiate the body if we are generating code, if we are
3851 -- generating cross-reference information, or if we are building
3852 -- trees for ASIS use or GNATprove use.
3853
3854 declare
3855 Enclosing_Body_Present : Boolean := False;
3856 -- If the generic unit is not a compilation unit, then a body may
3857 -- be present in its parent even if none is required. We create a
3858 -- tentative pending instantiation for the body, which will be
3859 -- discarded if none is actually present.
3860
3861 Scop : Entity_Id;
3862
3863 begin
3864 if Scope (Gen_Unit) /= Standard_Standard
3865 and then not Is_Child_Unit (Gen_Unit)
3866 then
3867 Scop := Scope (Gen_Unit);
3868 while Present (Scop) and then Scop /= Standard_Standard loop
3869 if Unit_Requires_Body (Scop) then
3870 Enclosing_Body_Present := True;
3871 exit;
3872
3873 elsif In_Open_Scopes (Scop)
3874 and then In_Package_Body (Scop)
3875 then
3876 Enclosing_Body_Present := True;
3877 exit;
3878 end if;
3879
3880 exit when Is_Compilation_Unit (Scop);
3881 Scop := Scope (Scop);
3882 end loop;
3883 end if;
3884
3885 -- If front-end inlining is enabled or there are any subprograms
3886 -- marked with Inline_Always, and this is a unit for which code
3887 -- will be generated, we instantiate the body at once.
3888
3889 -- This is done if the instance is not the main unit, and if the
3890 -- generic is not a child unit of another generic, to avoid scope
3891 -- problems and the reinstallation of parent instances.
3892
3893 if Expander_Active
3894 and then (not Is_Child_Unit (Gen_Unit)
3895 or else not Is_Generic_Unit (Scope (Gen_Unit)))
3896 and then Might_Inline_Subp
3897 and then not Is_Actual_Pack
3898 then
3899 if not Back_End_Inlining
3900 and then (Front_End_Inlining or else Has_Inline_Always)
3901 and then (Is_In_Main_Unit (N)
3902 or else In_Main_Context (Current_Scope))
3903 and then Nkind (Parent (N)) /= N_Compilation_Unit
3904 then
3905 Inline_Now := True;
3906
3907 -- In configurable_run_time mode we force the inlining of
3908 -- predefined subprograms marked Inline_Always, to minimize
3909 -- the use of the run-time library.
3910
3911 elsif Is_Predefined_File_Name
3912 (Unit_File_Name (Get_Source_Unit (Gen_Decl)))
3913 and then Configurable_Run_Time_Mode
3914 and then Nkind (Parent (N)) /= N_Compilation_Unit
3915 then
3916 Inline_Now := True;
3917 end if;
3918
3919 -- If the current scope is itself an instance within a child
3920 -- unit, there will be duplications in the scope stack, and the
3921 -- unstacking mechanism in Inline_Instance_Body will fail.
3922 -- This loses some rare cases of optimization, and might be
3923 -- improved some day, if we can find a proper abstraction for
3924 -- "the complete compilation context" that can be saved and
3925 -- restored. ???
3926
3927 if Is_Generic_Instance (Current_Scope) then
3928 declare
3929 Curr_Unit : constant Entity_Id :=
3930 Cunit_Entity (Current_Sem_Unit);
3931 begin
3932 if Curr_Unit /= Current_Scope
3933 and then Is_Child_Unit (Curr_Unit)
3934 then
3935 Inline_Now := False;
3936 end if;
3937 end;
3938 end if;
3939 end if;
3940
3941 Needs_Body :=
3942 (Unit_Requires_Body (Gen_Unit)
3943 or else Enclosing_Body_Present
3944 or else Present (Corresponding_Body (Gen_Decl)))
3945 and then (Is_In_Main_Unit (N) or else Might_Inline_Subp)
3946 and then not Is_Actual_Pack
3947 and then not Inline_Now
3948 and then (Operating_Mode = Generate_Code
3949
3950 -- Need comment for this check ???
3951
3952 or else (Operating_Mode = Check_Semantics
3953 and then (ASIS_Mode or GNATprove_Mode)));
3954
3955 -- If front-end inlining is enabled or there are any subprograms
3956 -- marked with Inline_Always, do not instantiate body when within
3957 -- a generic context.
3958
3959 if ((Front_End_Inlining or else Has_Inline_Always)
3960 and then not Expander_Active)
3961 or else Is_Generic_Unit (Cunit_Entity (Main_Unit))
3962 then
3963 Needs_Body := False;
3964 end if;
3965
3966 -- If the current context is generic, and the package being
3967 -- instantiated is declared within a formal package, there is no
3968 -- body to instantiate until the enclosing generic is instantiated
3969 -- and there is an actual for the formal package. If the formal
3970 -- package has parameters, we build a regular package instance for
3971 -- it, that precedes the original formal package declaration.
3972
3973 if In_Open_Scopes (Scope (Scope (Gen_Unit))) then
3974 declare
3975 Decl : constant Node_Id :=
3976 Original_Node
3977 (Unit_Declaration_Node (Scope (Gen_Unit)));
3978 begin
3979 if Nkind (Decl) = N_Formal_Package_Declaration
3980 or else (Nkind (Decl) = N_Package_Declaration
3981 and then Is_List_Member (Decl)
3982 and then Present (Next (Decl))
3983 and then
3984 Nkind (Next (Decl)) =
3985 N_Formal_Package_Declaration)
3986 then
3987 Needs_Body := False;
3988 end if;
3989 end;
3990 end if;
3991 end;
3992
3993 -- For RCI unit calling stubs, we omit the instance body if the
3994 -- instance is the RCI library unit itself.
3995
3996 -- However there is a special case for nested instances: in this case
3997 -- we do generate the instance body, as it might be required, e.g.
3998 -- because it provides stream attributes for some type used in the
3999 -- profile of a remote subprogram. This is consistent with 12.3(12),
4000 -- which indicates that the instance body occurs at the place of the
4001 -- instantiation, and thus is part of the RCI declaration, which is
4002 -- present on all client partitions (this is E.2.3(18)).
4003
4004 -- Note that AI12-0002 may make it illegal at some point to have
4005 -- stream attributes defined in an RCI unit, in which case this
4006 -- special case will become unnecessary. In the meantime, there
4007 -- is known application code in production that depends on this
4008 -- being possible, so we definitely cannot eliminate the body in
4009 -- the case of nested instances for the time being.
4010
4011 -- When we generate a nested instance body, calling stubs for any
4012 -- relevant subprogram will be be inserted immediately after the
4013 -- subprogram declarations, and will take precedence over the
4014 -- subsequent (original) body. (The stub and original body will be
4015 -- complete homographs, but this is permitted in an instance).
4016 -- (Could we do better and remove the original body???)
4017
4018 if Distribution_Stub_Mode = Generate_Caller_Stub_Body
4019 and then Comes_From_Source (N)
4020 and then Nkind (Parent (N)) = N_Compilation_Unit
4021 then
4022 Needs_Body := False;
4023 end if;
4024
4025 if Needs_Body then
4026
4027 -- Here is a defence against a ludicrous number of instantiations
4028 -- caused by a circular set of instantiation attempts.
4029
4030 if Pending_Instantiations.Last > Maximum_Instantiations then
4031 Error_Msg_Uint_1 := UI_From_Int (Maximum_Instantiations);
4032 Error_Msg_N ("too many instantiations, exceeds max of^", N);
4033 Error_Msg_N ("\limit can be changed using -gnateinn switch", N);
4034 raise Unrecoverable_Error;
4035 end if;
4036
4037 -- Indicate that the enclosing scopes contain an instantiation,
4038 -- and that cleanup actions should be delayed until after the
4039 -- instance body is expanded.
4040
4041 Check_Forward_Instantiation (Gen_Decl);
4042 if Nkind (N) = N_Package_Instantiation then
4043 declare
4044 Enclosing_Master : Entity_Id;
4045
4046 begin
4047 -- Loop to search enclosing masters
4048
4049 Enclosing_Master := Current_Scope;
4050 Scope_Loop : while Enclosing_Master /= Standard_Standard loop
4051 if Ekind (Enclosing_Master) = E_Package then
4052 if Is_Compilation_Unit (Enclosing_Master) then
4053 if In_Package_Body (Enclosing_Master) then
4054 Delay_Descriptors
4055 (Body_Entity (Enclosing_Master));
4056 else
4057 Delay_Descriptors
4058 (Enclosing_Master);
4059 end if;
4060
4061 exit Scope_Loop;
4062
4063 else
4064 Enclosing_Master := Scope (Enclosing_Master);
4065 end if;
4066
4067 elsif Is_Generic_Unit (Enclosing_Master)
4068 or else Ekind (Enclosing_Master) = E_Void
4069 then
4070 -- Cleanup actions will eventually be performed on the
4071 -- enclosing subprogram or package instance, if any.
4072 -- Enclosing scope is void in the formal part of a
4073 -- generic subprogram.
4074
4075 exit Scope_Loop;
4076
4077 else
4078 if Ekind (Enclosing_Master) = E_Entry
4079 and then
4080 Ekind (Scope (Enclosing_Master)) = E_Protected_Type
4081 then
4082 if not Expander_Active then
4083 exit Scope_Loop;
4084 else
4085 Enclosing_Master :=
4086 Protected_Body_Subprogram (Enclosing_Master);
4087 end if;
4088 end if;
4089
4090 Set_Delay_Cleanups (Enclosing_Master);
4091
4092 while Ekind (Enclosing_Master) = E_Block loop
4093 Enclosing_Master := Scope (Enclosing_Master);
4094 end loop;
4095
4096 if Is_Subprogram (Enclosing_Master) then
4097 Delay_Descriptors (Enclosing_Master);
4098
4099 elsif Is_Task_Type (Enclosing_Master) then
4100 declare
4101 TBP : constant Node_Id :=
4102 Get_Task_Body_Procedure
4103 (Enclosing_Master);
4104 begin
4105 if Present (TBP) then
4106 Delay_Descriptors (TBP);
4107 Set_Delay_Cleanups (TBP);
4108 end if;
4109 end;
4110 end if;
4111
4112 exit Scope_Loop;
4113 end if;
4114 end loop Scope_Loop;
4115 end;
4116
4117 -- Make entry in table
4118
4119 Pending_Instantiations.Append
4120 ((Inst_Node => N,
4121 Act_Decl => Act_Decl,
4122 Expander_Status => Expander_Active,
4123 Current_Sem_Unit => Current_Sem_Unit,
4124 Scope_Suppress => Scope_Suppress,
4125 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
4126 Version => Ada_Version,
4127 Version_Pragma => Ada_Version_Pragma,
4128 Warnings => Save_Warnings,
4129 SPARK_Mode => SPARK_Mode,
4130 SPARK_Mode_Pragma => SPARK_Mode_Pragma));
4131 end if;
4132 end if;
4133
4134 Set_Categorization_From_Pragmas (Act_Decl);
4135
4136 if Parent_Installed then
4137 Hide_Current_Scope;
4138 end if;
4139
4140 Set_Instance_Spec (N, Act_Decl);
4141
4142 -- If not a compilation unit, insert the package declaration before
4143 -- the original instantiation node.
4144
4145 if Nkind (Parent (N)) /= N_Compilation_Unit then
4146 Mark_Rewrite_Insertion (Act_Decl);
4147 Insert_Before (N, Act_Decl);
4148
4149 if Has_Aspects (N) then
4150 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4151
4152 -- The pragma created for a Default_Storage_Pool aspect must
4153 -- appear ahead of the declarations in the instance spec.
4154 -- Analysis has placed it after the instance node, so remove
4155 -- it and reinsert it properly now.
4156
4157 declare
4158 ASN : constant Node_Id := First (Aspect_Specifications (N));
4159 A_Name : constant Name_Id := Chars (Identifier (ASN));
4160 Decl : Node_Id;
4161
4162 begin
4163 if A_Name = Name_Default_Storage_Pool then
4164 if No (Visible_Declarations (Act_Spec)) then
4165 Set_Visible_Declarations (Act_Spec, New_List);
4166 end if;
4167
4168 Decl := Next (N);
4169 while Present (Decl) loop
4170 if Nkind (Decl) = N_Pragma then
4171 Remove (Decl);
4172 Prepend (Decl, Visible_Declarations (Act_Spec));
4173 exit;
4174 end if;
4175
4176 Next (Decl);
4177 end loop;
4178 end if;
4179 end;
4180 end if;
4181
4182 Analyze (Act_Decl);
4183
4184 -- For an instantiation that is a compilation unit, place
4185 -- declaration on current node so context is complete for analysis
4186 -- (including nested instantiations). If this is the main unit,
4187 -- the declaration eventually replaces the instantiation node.
4188 -- If the instance body is created later, it replaces the
4189 -- instance node, and the declaration is attached to it
4190 -- (see Build_Instance_Compilation_Unit_Nodes).
4191
4192 else
4193 if Cunit_Entity (Current_Sem_Unit) = Defining_Entity (N) then
4194
4195 -- The entity for the current unit is the newly created one,
4196 -- and all semantic information is attached to it.
4197
4198 Set_Cunit_Entity (Current_Sem_Unit, Act_Decl_Id);
4199
4200 -- If this is the main unit, replace the main entity as well
4201
4202 if Current_Sem_Unit = Main_Unit then
4203 Main_Unit_Entity := Act_Decl_Id;
4204 end if;
4205 end if;
4206
4207 Set_Unit (Parent (N), Act_Decl);
4208 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
4209 Set_Package_Instantiation (Act_Decl_Id, N);
4210
4211 -- Process aspect specifications of the instance node, if any, to
4212 -- take into account categorization pragmas before analyzing the
4213 -- instance.
4214
4215 if Has_Aspects (N) then
4216 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4217 end if;
4218
4219 Analyze (Act_Decl);
4220 Set_Unit (Parent (N), N);
4221 Set_Body_Required (Parent (N), False);
4222
4223 -- We never need elaboration checks on instantiations, since by
4224 -- definition, the body instantiation is elaborated at the same
4225 -- time as the spec instantiation.
4226
4227 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
4228 Set_Kill_Elaboration_Checks (Act_Decl_Id);
4229 end if;
4230
4231 Check_Elab_Instantiation (N);
4232
4233 if ABE_Is_Certain (N) and then Needs_Body then
4234 Pending_Instantiations.Decrement_Last;
4235 end if;
4236
4237 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
4238
4239 Set_First_Private_Entity (Defining_Unit_Name (Unit_Renaming),
4240 First_Private_Entity (Act_Decl_Id));
4241
4242 -- If the instantiation will receive a body, the unit will be
4243 -- transformed into a package body, and receive its own elaboration
4244 -- entity. Otherwise, the nature of the unit is now a package
4245 -- declaration.
4246
4247 if Nkind (Parent (N)) = N_Compilation_Unit
4248 and then not Needs_Body
4249 then
4250 Rewrite (N, Act_Decl);
4251 end if;
4252
4253 if Present (Corresponding_Body (Gen_Decl))
4254 or else Unit_Requires_Body (Gen_Unit)
4255 then
4256 Set_Has_Completion (Act_Decl_Id);
4257 end if;
4258
4259 Check_Formal_Packages (Act_Decl_Id);
4260
4261 Restore_Hidden_Primitives (Vis_Prims_List);
4262 Restore_Private_Views (Act_Decl_Id);
4263
4264 Inherit_Context (Gen_Decl, N);
4265
4266 if Parent_Installed then
4267 Remove_Parent;
4268 end if;
4269
4270 Restore_Env;
4271 Env_Installed := False;
4272 end if;
4273
4274 Validate_Categorization_Dependency (N, Act_Decl_Id);
4275
4276 -- There used to be a check here to prevent instantiations in local
4277 -- contexts if the No_Local_Allocators restriction was active. This
4278 -- check was removed by a binding interpretation in AI-95-00130/07,
4279 -- but we retain the code for documentation purposes.
4280
4281 -- if Ekind (Act_Decl_Id) /= E_Void
4282 -- and then not Is_Library_Level_Entity (Act_Decl_Id)
4283 -- then
4284 -- Check_Restriction (No_Local_Allocators, N);
4285 -- end if;
4286
4287 if Inline_Now then
4288 Inline_Instance_Body (N, Gen_Unit, Act_Decl);
4289 end if;
4290
4291 -- The following is a tree patch for ASIS: ASIS needs separate nodes to
4292 -- be used as defining identifiers for a formal package and for the
4293 -- corresponding expanded package.
4294
4295 if Nkind (N) = N_Formal_Package_Declaration then
4296 Act_Decl_Id := New_Copy (Defining_Entity (N));
4297 Set_Comes_From_Source (Act_Decl_Id, True);
4298 Set_Is_Generic_Instance (Act_Decl_Id, False);
4299 Set_Defining_Identifier (N, Act_Decl_Id);
4300 end if;
4301
4302 Ignore_Pragma_SPARK_Mode := Save_IPSM;
4303 SPARK_Mode := Save_SM;
4304 SPARK_Mode_Pragma := Save_SMP;
4305 Style_Check := Save_Style_Check;
4306
4307 if SPARK_Mode = On then
4308 Dynamic_Elaboration_Checks := False;
4309 end if;
4310
4311 -- Check that if N is an instantiation of System.Dim_Float_IO or
4312 -- System.Dim_Integer_IO, the formal type has a dimension system.
4313
4314 if Nkind (N) = N_Package_Instantiation
4315 and then Is_Dim_IO_Package_Instantiation (N)
4316 then
4317 declare
4318 Assoc : constant Node_Id := First (Generic_Associations (N));
4319 begin
4320 if not Has_Dimension_System
4321 (Etype (Explicit_Generic_Actual_Parameter (Assoc)))
4322 then
4323 Error_Msg_N ("type with a dimension system expected", Assoc);
4324 end if;
4325 end;
4326 end if;
4327
4328 <<Leave>>
4329 if Has_Aspects (N) and then Nkind (Parent (N)) /= N_Compilation_Unit then
4330 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4331 end if;
4332
4333 exception
4334 when Instantiation_Error =>
4335 if Parent_Installed then
4336 Remove_Parent;
4337 end if;
4338
4339 if Env_Installed then
4340 Restore_Env;
4341 end if;
4342
4343 Ignore_Pragma_SPARK_Mode := Save_IPSM;
4344 SPARK_Mode := Save_SM;
4345 SPARK_Mode_Pragma := Save_SMP;
4346 Style_Check := Save_Style_Check;
4347
4348 if SPARK_Mode = On then
4349 Dynamic_Elaboration_Checks := False;
4350 end if;
4351 end Analyze_Package_Instantiation;
4352
4353 --------------------------
4354 -- Inline_Instance_Body --
4355 --------------------------
4356
4357 procedure Inline_Instance_Body
4358 (N : Node_Id;
4359 Gen_Unit : Entity_Id;
4360 Act_Decl : Node_Id)
4361 is
4362 Curr_Comp : constant Node_Id := Cunit (Current_Sem_Unit);
4363 Curr_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
4364 Gen_Comp : constant Entity_Id :=
4365 Cunit_Entity (Get_Source_Unit (Gen_Unit));
4366
4367 Save_SM : constant SPARK_Mode_Type := SPARK_Mode;
4368 Save_SMP : constant Node_Id := SPARK_Mode_Pragma;
4369 -- Save all SPARK_Mode-related attributes as removing enclosing scopes
4370 -- to provide a clean environment for analysis of the inlined body will
4371 -- eliminate any previously set SPARK_Mode.
4372
4373 Scope_Stack_Depth : constant Int :=
4374 Scope_Stack.Last - Scope_Stack.First + 1;
4375
4376 Use_Clauses : array (1 .. Scope_Stack_Depth) of Node_Id;
4377 Instances : array (1 .. Scope_Stack_Depth) of Entity_Id;
4378 Inner_Scopes : array (1 .. Scope_Stack_Depth) of Entity_Id;
4379 Curr_Scope : Entity_Id := Empty;
4380 List : Elist_Id;
4381 Num_Inner : Int := 0;
4382 Num_Scopes : Int := 0;
4383 N_Instances : Int := 0;
4384 Removed : Boolean := False;
4385 S : Entity_Id;
4386 Vis : Boolean;
4387
4388 begin
4389 -- Case of generic unit defined in another unit. We must remove the
4390 -- complete context of the current unit to install that of the generic.
4391
4392 if Gen_Comp /= Cunit_Entity (Current_Sem_Unit) then
4393
4394 -- Add some comments for the following two loops ???
4395
4396 S := Current_Scope;
4397 while Present (S) and then S /= Standard_Standard loop
4398 loop
4399 Num_Scopes := Num_Scopes + 1;
4400
4401 Use_Clauses (Num_Scopes) :=
4402 (Scope_Stack.Table
4403 (Scope_Stack.Last - Num_Scopes + 1).
4404 First_Use_Clause);
4405 End_Use_Clauses (Use_Clauses (Num_Scopes));
4406
4407 exit when Scope_Stack.Last - Num_Scopes + 1 = Scope_Stack.First
4408 or else Scope_Stack.Table
4409 (Scope_Stack.Last - Num_Scopes).Entity = Scope (S);
4410 end loop;
4411
4412 exit when Is_Generic_Instance (S)
4413 and then (In_Package_Body (S)
4414 or else Ekind (S) = E_Procedure
4415 or else Ekind (S) = E_Function);
4416 S := Scope (S);
4417 end loop;
4418
4419 Vis := Is_Immediately_Visible (Gen_Comp);
4420
4421 -- Find and save all enclosing instances
4422
4423 S := Current_Scope;
4424
4425 while Present (S)
4426 and then S /= Standard_Standard
4427 loop
4428 if Is_Generic_Instance (S) then
4429 N_Instances := N_Instances + 1;
4430 Instances (N_Instances) := S;
4431
4432 exit when In_Package_Body (S);
4433 end if;
4434
4435 S := Scope (S);
4436 end loop;
4437
4438 -- Remove context of current compilation unit, unless we are within a
4439 -- nested package instantiation, in which case the context has been
4440 -- removed previously.
4441
4442 -- If current scope is the body of a child unit, remove context of
4443 -- spec as well. If an enclosing scope is an instance body, the
4444 -- context has already been removed, but the entities in the body
4445 -- must be made invisible as well.
4446
4447 S := Current_Scope;
4448 while Present (S) and then S /= Standard_Standard loop
4449 if Is_Generic_Instance (S)
4450 and then (In_Package_Body (S)
4451 or else Ekind_In (S, E_Procedure, E_Function))
4452 then
4453 -- We still have to remove the entities of the enclosing
4454 -- instance from direct visibility.
4455
4456 declare
4457 E : Entity_Id;
4458 begin
4459 E := First_Entity (S);
4460 while Present (E) loop
4461 Set_Is_Immediately_Visible (E, False);
4462 Next_Entity (E);
4463 end loop;
4464 end;
4465
4466 exit;
4467 end if;
4468
4469 if S = Curr_Unit
4470 or else (Ekind (Curr_Unit) = E_Package_Body
4471 and then S = Spec_Entity (Curr_Unit))
4472 or else (Ekind (Curr_Unit) = E_Subprogram_Body
4473 and then S = Corresponding_Spec
4474 (Unit_Declaration_Node (Curr_Unit)))
4475 then
4476 Removed := True;
4477
4478 -- Remove entities in current scopes from visibility, so that
4479 -- instance body is compiled in a clean environment.
4480
4481 List := Save_Scope_Stack (Handle_Use => False);
4482
4483 if Is_Child_Unit (S) then
4484
4485 -- Remove child unit from stack, as well as inner scopes.
4486 -- Removing the context of a child unit removes parent units
4487 -- as well.
4488
4489 while Current_Scope /= S loop
4490 Num_Inner := Num_Inner + 1;
4491 Inner_Scopes (Num_Inner) := Current_Scope;
4492 Pop_Scope;
4493 end loop;
4494
4495 Pop_Scope;
4496 Remove_Context (Curr_Comp);
4497 Curr_Scope := S;
4498
4499 else
4500 Remove_Context (Curr_Comp);
4501 end if;
4502
4503 if Ekind (Curr_Unit) = E_Package_Body then
4504 Remove_Context (Library_Unit (Curr_Comp));
4505 end if;
4506 end if;
4507
4508 S := Scope (S);
4509 end loop;
4510
4511 pragma Assert (Num_Inner < Num_Scopes);
4512
4513 -- The inlined package body must be analyzed with the SPARK_Mode of
4514 -- the enclosing context, otherwise the body may cause bogus errors
4515 -- if a configuration SPARK_Mode pragma in in effect.
4516
4517 Push_Scope (Standard_Standard);
4518 Scope_Stack.Table (Scope_Stack.Last).Is_Active_Stack_Base := True;
4519 Instantiate_Package_Body
4520 (Body_Info =>
4521 ((Inst_Node => N,
4522 Act_Decl => Act_Decl,
4523 Expander_Status => Expander_Active,
4524 Current_Sem_Unit => Current_Sem_Unit,
4525 Scope_Suppress => Scope_Suppress,
4526 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
4527 Version => Ada_Version,
4528 Version_Pragma => Ada_Version_Pragma,
4529 Warnings => Save_Warnings,
4530 SPARK_Mode => Save_SM,
4531 SPARK_Mode_Pragma => Save_SMP)),
4532 Inlined_Body => True);
4533
4534 Pop_Scope;
4535
4536 -- Restore context
4537
4538 Set_Is_Immediately_Visible (Gen_Comp, Vis);
4539
4540 -- Reset Generic_Instance flag so that use clauses can be installed
4541 -- in the proper order. (See Use_One_Package for effect of enclosing
4542 -- instances on processing of use clauses).
4543
4544 for J in 1 .. N_Instances loop
4545 Set_Is_Generic_Instance (Instances (J), False);
4546 end loop;
4547
4548 if Removed then
4549 Install_Context (Curr_Comp);
4550
4551 if Present (Curr_Scope)
4552 and then Is_Child_Unit (Curr_Scope)
4553 then
4554 Push_Scope (Curr_Scope);
4555 Set_Is_Immediately_Visible (Curr_Scope);
4556
4557 -- Finally, restore inner scopes as well
4558
4559 for J in reverse 1 .. Num_Inner loop
4560 Push_Scope (Inner_Scopes (J));
4561 end loop;
4562 end if;
4563
4564 Restore_Scope_Stack (List, Handle_Use => False);
4565
4566 if Present (Curr_Scope)
4567 and then
4568 (In_Private_Part (Curr_Scope)
4569 or else In_Package_Body (Curr_Scope))
4570 then
4571 -- Install private declaration of ancestor units, which are
4572 -- currently available. Restore_Scope_Stack and Install_Context
4573 -- only install the visible part of parents.
4574
4575 declare
4576 Par : Entity_Id;
4577 begin
4578 Par := Scope (Curr_Scope);
4579 while (Present (Par)) and then Par /= Standard_Standard loop
4580 Install_Private_Declarations (Par);
4581 Par := Scope (Par);
4582 end loop;
4583 end;
4584 end if;
4585 end if;
4586
4587 -- Restore use clauses. For a child unit, use clauses in the parents
4588 -- are restored when installing the context, so only those in inner
4589 -- scopes (and those local to the child unit itself) need to be
4590 -- installed explicitly.
4591
4592 if Is_Child_Unit (Curr_Unit) and then Removed then
4593 for J in reverse 1 .. Num_Inner + 1 loop
4594 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
4595 Use_Clauses (J);
4596 Install_Use_Clauses (Use_Clauses (J));
4597 end loop;
4598
4599 else
4600 for J in reverse 1 .. Num_Scopes loop
4601 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
4602 Use_Clauses (J);
4603 Install_Use_Clauses (Use_Clauses (J));
4604 end loop;
4605 end if;
4606
4607 -- Restore status of instances. If one of them is a body, make its
4608 -- local entities visible again.
4609
4610 declare
4611 E : Entity_Id;
4612 Inst : Entity_Id;
4613
4614 begin
4615 for J in 1 .. N_Instances loop
4616 Inst := Instances (J);
4617 Set_Is_Generic_Instance (Inst, True);
4618
4619 if In_Package_Body (Inst)
4620 or else Ekind_In (S, E_Procedure, E_Function)
4621 then
4622 E := First_Entity (Instances (J));
4623 while Present (E) loop
4624 Set_Is_Immediately_Visible (E);
4625 Next_Entity (E);
4626 end loop;
4627 end if;
4628 end loop;
4629 end;
4630
4631 -- If generic unit is in current unit, current context is correct. Note
4632 -- that the context is guaranteed to carry the correct SPARK_Mode as no
4633 -- enclosing scopes were removed.
4634
4635 else
4636 Instantiate_Package_Body
4637 (Body_Info =>
4638 ((Inst_Node => N,
4639 Act_Decl => Act_Decl,
4640 Expander_Status => Expander_Active,
4641 Current_Sem_Unit => Current_Sem_Unit,
4642 Scope_Suppress => Scope_Suppress,
4643 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
4644 Version => Ada_Version,
4645 Version_Pragma => Ada_Version_Pragma,
4646 Warnings => Save_Warnings,
4647 SPARK_Mode => SPARK_Mode,
4648 SPARK_Mode_Pragma => SPARK_Mode_Pragma)),
4649 Inlined_Body => True);
4650 end if;
4651 end Inline_Instance_Body;
4652
4653 -------------------------------------
4654 -- Analyze_Procedure_Instantiation --
4655 -------------------------------------
4656
4657 procedure Analyze_Procedure_Instantiation (N : Node_Id) is
4658 begin
4659 Analyze_Subprogram_Instantiation (N, E_Procedure);
4660 end Analyze_Procedure_Instantiation;
4661
4662 -----------------------------------
4663 -- Need_Subprogram_Instance_Body --
4664 -----------------------------------
4665
4666 function Need_Subprogram_Instance_Body
4667 (N : Node_Id;
4668 Subp : Entity_Id) return Boolean
4669 is
4670 begin
4671 -- Must be inlined (or inlined renaming)
4672
4673 if (Is_In_Main_Unit (N)
4674 or else Is_Inlined (Subp)
4675 or else Is_Inlined (Alias (Subp)))
4676
4677 -- Must be generating code or analyzing code in ASIS/GNATprove mode
4678
4679 and then (Operating_Mode = Generate_Code
4680 or else (Operating_Mode = Check_Semantics
4681 and then (ASIS_Mode or GNATprove_Mode)))
4682
4683 -- The body is needed when generating code (full expansion), in ASIS
4684 -- mode for other tools, and in GNATprove mode (special expansion) for
4685 -- formal verification of the body itself.
4686
4687 and then (Expander_Active or ASIS_Mode or GNATprove_Mode)
4688
4689 -- No point in inlining if ABE is inevitable
4690
4691 and then not ABE_Is_Certain (N)
4692
4693 -- Or if subprogram is eliminated
4694
4695 and then not Is_Eliminated (Subp)
4696 then
4697 Pending_Instantiations.Append
4698 ((Inst_Node => N,
4699 Act_Decl => Unit_Declaration_Node (Subp),
4700 Expander_Status => Expander_Active,
4701 Current_Sem_Unit => Current_Sem_Unit,
4702 Scope_Suppress => Scope_Suppress,
4703 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
4704 Version => Ada_Version,
4705 Version_Pragma => Ada_Version_Pragma,
4706 Warnings => Save_Warnings,
4707 SPARK_Mode => SPARK_Mode,
4708 SPARK_Mode_Pragma => SPARK_Mode_Pragma));
4709 return True;
4710
4711 -- Here if not inlined, or we ignore the inlining
4712
4713 else
4714 return False;
4715 end if;
4716 end Need_Subprogram_Instance_Body;
4717
4718 --------------------------------------
4719 -- Analyze_Subprogram_Instantiation --
4720 --------------------------------------
4721
4722 procedure Analyze_Subprogram_Instantiation
4723 (N : Node_Id;
4724 K : Entity_Kind)
4725 is
4726 Loc : constant Source_Ptr := Sloc (N);
4727 Gen_Id : constant Node_Id := Name (N);
4728
4729 Anon_Id : constant Entity_Id :=
4730 Make_Defining_Identifier (Sloc (Defining_Entity (N)),
4731 Chars => New_External_Name
4732 (Chars (Defining_Entity (N)), 'R'));
4733
4734 Act_Decl_Id : Entity_Id;
4735 Act_Decl : Node_Id;
4736 Act_Spec : Node_Id;
4737 Act_Tree : Node_Id;
4738
4739 Env_Installed : Boolean := False;
4740 Gen_Unit : Entity_Id;
4741 Gen_Decl : Node_Id;
4742 Pack_Id : Entity_Id;
4743 Parent_Installed : Boolean := False;
4744
4745 Renaming_List : List_Id;
4746 -- The list of declarations that link formals and actuals of the
4747 -- instance. These are subtype declarations for formal types, and
4748 -- renaming declarations for other formals. The subprogram declaration
4749 -- for the instance is then appended to the list, and the last item on
4750 -- the list is the renaming declaration for the instance.
4751
4752 procedure Analyze_Instance_And_Renamings;
4753 -- The instance must be analyzed in a context that includes the mappings
4754 -- of generic parameters into actuals. We create a package declaration
4755 -- for this purpose, and a subprogram with an internal name within the
4756 -- package. The subprogram instance is simply an alias for the internal
4757 -- subprogram, declared in the current scope.
4758
4759 procedure Build_Subprogram_Renaming;
4760 -- If the subprogram is recursive, there are occurrences of the name of
4761 -- the generic within the body, which must resolve to the current
4762 -- instance. We add a renaming declaration after the declaration, which
4763 -- is available in the instance body, as well as in the analysis of
4764 -- aspects that appear in the generic. This renaming declaration is
4765 -- inserted after the instance declaration which it renames.
4766
4767 procedure Instantiate_Subprogram_Contract (Templ : Node_Id);
4768 -- Instantiate all source pragmas found in the contract of the generic
4769 -- subprogram declaration template denoted by Templ. The instantiated
4770 -- pragmas are added to list Renaming_List.
4771
4772 ------------------------------------
4773 -- Analyze_Instance_And_Renamings --
4774 ------------------------------------
4775
4776 procedure Analyze_Instance_And_Renamings is
4777 Def_Ent : constant Entity_Id := Defining_Entity (N);
4778 Pack_Decl : Node_Id;
4779
4780 begin
4781 if Nkind (Parent (N)) = N_Compilation_Unit then
4782
4783 -- For the case of a compilation unit, the container package has
4784 -- the same name as the instantiation, to insure that the binder
4785 -- calls the elaboration procedure with the right name. Copy the
4786 -- entity of the instance, which may have compilation level flags
4787 -- (e.g. Is_Child_Unit) set.
4788
4789 Pack_Id := New_Copy (Def_Ent);
4790
4791 else
4792 -- Otherwise we use the name of the instantiation concatenated
4793 -- with its source position to ensure uniqueness if there are
4794 -- several instantiations with the same name.
4795
4796 Pack_Id :=
4797 Make_Defining_Identifier (Loc,
4798 Chars => New_External_Name
4799 (Related_Id => Chars (Def_Ent),
4800 Suffix => "GP",
4801 Suffix_Index => Source_Offset (Sloc (Def_Ent))));
4802 end if;
4803
4804 Pack_Decl :=
4805 Make_Package_Declaration (Loc,
4806 Specification => Make_Package_Specification (Loc,
4807 Defining_Unit_Name => Pack_Id,
4808 Visible_Declarations => Renaming_List,
4809 End_Label => Empty));
4810
4811 Set_Instance_Spec (N, Pack_Decl);
4812 Set_Is_Generic_Instance (Pack_Id);
4813 Set_Debug_Info_Needed (Pack_Id);
4814
4815 -- Case of not a compilation unit
4816
4817 if Nkind (Parent (N)) /= N_Compilation_Unit then
4818 Mark_Rewrite_Insertion (Pack_Decl);
4819 Insert_Before (N, Pack_Decl);
4820 Set_Has_Completion (Pack_Id);
4821
4822 -- Case of an instantiation that is a compilation unit
4823
4824 -- Place declaration on current node so context is complete for
4825 -- analysis (including nested instantiations), and for use in a
4826 -- context_clause (see Analyze_With_Clause).
4827
4828 else
4829 Set_Unit (Parent (N), Pack_Decl);
4830 Set_Parent_Spec (Pack_Decl, Parent_Spec (N));
4831 end if;
4832
4833 Analyze (Pack_Decl);
4834 Check_Formal_Packages (Pack_Id);
4835 Set_Is_Generic_Instance (Pack_Id, False);
4836
4837 -- Why do we clear Is_Generic_Instance??? We set it 20 lines
4838 -- above???
4839
4840 -- Body of the enclosing package is supplied when instantiating the
4841 -- subprogram body, after semantic analysis is completed.
4842
4843 if Nkind (Parent (N)) = N_Compilation_Unit then
4844
4845 -- Remove package itself from visibility, so it does not
4846 -- conflict with subprogram.
4847
4848 Set_Name_Entity_Id (Chars (Pack_Id), Homonym (Pack_Id));
4849
4850 -- Set name and scope of internal subprogram so that the proper
4851 -- external name will be generated. The proper scope is the scope
4852 -- of the wrapper package. We need to generate debugging info for
4853 -- the internal subprogram, so set flag accordingly.
4854
4855 Set_Chars (Anon_Id, Chars (Defining_Entity (N)));
4856 Set_Scope (Anon_Id, Scope (Pack_Id));
4857
4858 -- Mark wrapper package as referenced, to avoid spurious warnings
4859 -- if the instantiation appears in various with_ clauses of
4860 -- subunits of the main unit.
4861
4862 Set_Referenced (Pack_Id);
4863 end if;
4864
4865 Set_Is_Generic_Instance (Anon_Id);
4866 Set_Debug_Info_Needed (Anon_Id);
4867 Act_Decl_Id := New_Copy (Anon_Id);
4868
4869 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
4870 Set_Chars (Act_Decl_Id, Chars (Defining_Entity (N)));
4871 Set_Sloc (Act_Decl_Id, Sloc (Defining_Entity (N)));
4872
4873 -- Subprogram instance comes from source only if generic does
4874
4875 Set_Comes_From_Source (Act_Decl_Id, Comes_From_Source (Gen_Unit));
4876
4877 -- The signature may involve types that are not frozen yet, but the
4878 -- subprogram will be frozen at the point the wrapper package is
4879 -- frozen, so it does not need its own freeze node. In fact, if one
4880 -- is created, it might conflict with the freezing actions from the
4881 -- wrapper package.
4882
4883 Set_Has_Delayed_Freeze (Anon_Id, False);
4884
4885 -- If the instance is a child unit, mark the Id accordingly. Mark
4886 -- the anonymous entity as well, which is the real subprogram and
4887 -- which is used when the instance appears in a context clause.
4888 -- Similarly, propagate the Is_Eliminated flag to handle properly
4889 -- nested eliminated subprograms.
4890
4891 Set_Is_Child_Unit (Act_Decl_Id, Is_Child_Unit (Defining_Entity (N)));
4892 Set_Is_Child_Unit (Anon_Id, Is_Child_Unit (Defining_Entity (N)));
4893 New_Overloaded_Entity (Act_Decl_Id);
4894 Check_Eliminated (Act_Decl_Id);
4895 Set_Is_Eliminated (Anon_Id, Is_Eliminated (Act_Decl_Id));
4896
4897 -- In compilation unit case, kill elaboration checks on the
4898 -- instantiation, since they are never needed -- the body is
4899 -- instantiated at the same point as the spec.
4900
4901 if Nkind (Parent (N)) = N_Compilation_Unit then
4902 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
4903 Set_Kill_Elaboration_Checks (Act_Decl_Id);
4904 Set_Is_Compilation_Unit (Anon_Id);
4905
4906 Set_Cunit_Entity (Current_Sem_Unit, Pack_Id);
4907 end if;
4908
4909 -- The instance is not a freezing point for the new subprogram
4910
4911 Set_Is_Frozen (Act_Decl_Id, False);
4912
4913 if Nkind (Defining_Entity (N)) = N_Defining_Operator_Symbol then
4914 Valid_Operator_Definition (Act_Decl_Id);
4915 end if;
4916
4917 Set_Alias (Act_Decl_Id, Anon_Id);
4918 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
4919 Set_Has_Completion (Act_Decl_Id);
4920 Set_Related_Instance (Pack_Id, Act_Decl_Id);
4921
4922 if Nkind (Parent (N)) = N_Compilation_Unit then
4923 Set_Body_Required (Parent (N), False);
4924 end if;
4925 end Analyze_Instance_And_Renamings;
4926
4927 -------------------------------
4928 -- Build_Subprogram_Renaming --
4929 -------------------------------
4930
4931 procedure Build_Subprogram_Renaming is
4932 Renaming_Decl : Node_Id;
4933 Unit_Renaming : Node_Id;
4934
4935 begin
4936 Unit_Renaming :=
4937 Make_Subprogram_Renaming_Declaration (Loc,
4938 Specification =>
4939 Copy_Generic_Node
4940 (Specification (Original_Node (Gen_Decl)),
4941 Empty,
4942 Instantiating => True),
4943 Name => New_Occurrence_Of (Anon_Id, Loc));
4944
4945 -- The generic may be a a child unit. The renaming needs an
4946 -- identifier with the proper name.
4947
4948 Set_Defining_Unit_Name (Specification (Unit_Renaming),
4949 Make_Defining_Identifier (Loc, Chars (Gen_Unit)));
4950
4951 -- If there is a formal subprogram with the same name as the unit
4952 -- itself, do not add this renaming declaration, to prevent
4953 -- ambiguities when there is a call with that name in the body.
4954 -- This is a partial and ugly fix for one ACATS test. ???
4955
4956 Renaming_Decl := First (Renaming_List);
4957 while Present (Renaming_Decl) loop
4958 if Nkind (Renaming_Decl) = N_Subprogram_Renaming_Declaration
4959 and then
4960 Chars (Defining_Entity (Renaming_Decl)) = Chars (Gen_Unit)
4961 then
4962 exit;
4963 end if;
4964
4965 Next (Renaming_Decl);
4966 end loop;
4967
4968 if No (Renaming_Decl) then
4969 Append (Unit_Renaming, Renaming_List);
4970 end if;
4971 end Build_Subprogram_Renaming;
4972
4973 -------------------------------------
4974 -- Instantiate_Subprogram_Contract --
4975 -------------------------------------
4976
4977 procedure Instantiate_Subprogram_Contract (Templ : Node_Id) is
4978 procedure Instantiate_Pragmas (First_Prag : Node_Id);
4979 -- Instantiate all contract-related source pragmas found in the list
4980 -- starting with pragma First_Prag. Each instantiated pragma is added
4981 -- to list Renaming_List.
4982
4983 -------------------------
4984 -- Instantiate_Pragmas --
4985 -------------------------
4986
4987 procedure Instantiate_Pragmas (First_Prag : Node_Id) is
4988 Inst_Prag : Node_Id;
4989 Prag : Node_Id;
4990
4991 begin
4992 Prag := First_Prag;
4993 while Present (Prag) loop
4994 if Is_Generic_Contract_Pragma (Prag) then
4995 Inst_Prag :=
4996 Copy_Generic_Node (Prag, Empty, Instantiating => True);
4997
4998 Set_Analyzed (Inst_Prag, False);
4999 Append_To (Renaming_List, Inst_Prag);
5000 end if;
5001
5002 Prag := Next_Pragma (Prag);
5003 end loop;
5004 end Instantiate_Pragmas;
5005
5006 -- Local variables
5007
5008 Items : constant Node_Id := Contract (Defining_Entity (Templ));
5009
5010 -- Start of processing for Instantiate_Subprogram_Contract
5011
5012 begin
5013 if Present (Items) then
5014 Instantiate_Pragmas (Pre_Post_Conditions (Items));
5015 Instantiate_Pragmas (Contract_Test_Cases (Items));
5016 Instantiate_Pragmas (Classifications (Items));
5017 end if;
5018 end Instantiate_Subprogram_Contract;
5019
5020 -- Local variables
5021
5022 Save_IPSM : constant Boolean := Ignore_Pragma_SPARK_Mode;
5023 -- Save flag Ignore_Pragma_SPARK_Mode for restore on exit
5024
5025 Save_SM : constant SPARK_Mode_Type := SPARK_Mode;
5026 Save_SMP : constant Node_Id := SPARK_Mode_Pragma;
5027 -- Save the SPARK_Mode-related data for restore on exit
5028
5029 Vis_Prims_List : Elist_Id := No_Elist;
5030 -- List of primitives made temporarily visible in the instantiation
5031 -- to match the visibility of the formal type
5032
5033 -- Start of processing for Analyze_Subprogram_Instantiation
5034
5035 begin
5036 Check_SPARK_05_Restriction ("generic is not allowed", N);
5037
5038 -- Very first thing: check for special Text_IO unit in case we are
5039 -- instantiating one of the children of [[Wide_]Wide_]Text_IO. Of course
5040 -- such an instantiation is bogus (these are packages, not subprograms),
5041 -- but we get a better error message if we do this.
5042
5043 Check_Text_IO_Special_Unit (Gen_Id);
5044
5045 -- Make node global for error reporting
5046
5047 Instantiation_Node := N;
5048
5049 -- For package instantiations we turn off style checks, because they
5050 -- will have been emitted in the generic. For subprogram instantiations
5051 -- we want to apply at least the check on overriding indicators so we
5052 -- do not modify the style check status.
5053
5054 -- The renaming declarations for the actuals do not come from source and
5055 -- will not generate spurious warnings.
5056
5057 Preanalyze_Actuals (N);
5058
5059 Init_Env;
5060 Env_Installed := True;
5061 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
5062 Gen_Unit := Entity (Gen_Id);
5063
5064 Generate_Reference (Gen_Unit, Gen_Id);
5065
5066 if Nkind (Gen_Id) = N_Identifier
5067 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
5068 then
5069 Error_Msg_NE
5070 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
5071 end if;
5072
5073 if Etype (Gen_Unit) = Any_Type then
5074 Restore_Env;
5075 return;
5076 end if;
5077
5078 -- Verify that it is a generic subprogram of the right kind, and that
5079 -- it does not lead to a circular instantiation.
5080
5081 if K = E_Procedure and then Ekind (Gen_Unit) /= E_Generic_Procedure then
5082 Error_Msg_NE
5083 ("& is not the name of a generic procedure", Gen_Id, Gen_Unit);
5084
5085 elsif K = E_Function and then Ekind (Gen_Unit) /= E_Generic_Function then
5086 Error_Msg_NE
5087 ("& is not the name of a generic function", Gen_Id, Gen_Unit);
5088
5089 elsif In_Open_Scopes (Gen_Unit) then
5090 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
5091
5092 else
5093 -- If the context of the instance is subject to SPARK_Mode "off",
5094 -- set the global flag which signals Analyze_Pragma to ignore all
5095 -- SPARK_Mode pragmas within the instance.
5096
5097 if SPARK_Mode = Off then
5098 Ignore_Pragma_SPARK_Mode := True;
5099 end if;
5100
5101 Set_Entity (Gen_Id, Gen_Unit);
5102 Set_Is_Instantiated (Gen_Unit);
5103
5104 if In_Extended_Main_Source_Unit (N) then
5105 Generate_Reference (Gen_Unit, N);
5106 end if;
5107
5108 -- If renaming, get original unit
5109
5110 if Present (Renamed_Object (Gen_Unit))
5111 and then Ekind_In (Renamed_Object (Gen_Unit), E_Generic_Procedure,
5112 E_Generic_Function)
5113 then
5114 Gen_Unit := Renamed_Object (Gen_Unit);
5115 Set_Is_Instantiated (Gen_Unit);
5116 Generate_Reference (Gen_Unit, N);
5117 end if;
5118
5119 if Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
5120 Error_Msg_Node_2 := Current_Scope;
5121 Error_Msg_NE
5122 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
5123 Circularity_Detected := True;
5124 Restore_Hidden_Primitives (Vis_Prims_List);
5125 goto Leave;
5126 end if;
5127
5128 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
5129
5130 -- Initialize renamings map, for error checking
5131
5132 Generic_Renamings.Set_Last (0);
5133 Generic_Renamings_HTable.Reset;
5134
5135 Create_Instantiation_Source (N, Gen_Unit, False, S_Adjustment);
5136
5137 -- Copy original generic tree, to produce text for instantiation
5138
5139 Act_Tree :=
5140 Copy_Generic_Node
5141 (Original_Node (Gen_Decl), Empty, Instantiating => True);
5142
5143 -- Inherit overriding indicator from instance node
5144
5145 Act_Spec := Specification (Act_Tree);
5146 Set_Must_Override (Act_Spec, Must_Override (N));
5147 Set_Must_Not_Override (Act_Spec, Must_Not_Override (N));
5148
5149 Renaming_List :=
5150 Analyze_Associations
5151 (I_Node => N,
5152 Formals => Generic_Formal_Declarations (Act_Tree),
5153 F_Copy => Generic_Formal_Declarations (Gen_Decl));
5154
5155 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
5156
5157 -- The subprogram itself cannot contain a nested instance, so the
5158 -- current parent is left empty.
5159
5160 Set_Instance_Env (Gen_Unit, Empty);
5161
5162 -- Build the subprogram declaration, which does not appear in the
5163 -- generic template, and give it a sloc consistent with that of the
5164 -- template.
5165
5166 Set_Defining_Unit_Name (Act_Spec, Anon_Id);
5167 Set_Generic_Parent (Act_Spec, Gen_Unit);
5168 Act_Decl :=
5169 Make_Subprogram_Declaration (Sloc (Act_Spec),
5170 Specification => Act_Spec);
5171
5172 -- The aspects have been copied previously, but they have to be
5173 -- linked explicitly to the new subprogram declaration. Explicit
5174 -- pre/postconditions on the instance are analyzed below, in a
5175 -- separate step.
5176
5177 Move_Aspects (Act_Tree, To => Act_Decl);
5178 Set_Categorization_From_Pragmas (Act_Decl);
5179
5180 if Parent_Installed then
5181 Hide_Current_Scope;
5182 end if;
5183
5184 Append (Act_Decl, Renaming_List);
5185
5186 -- Contract-related source pragmas that follow a generic subprogram
5187 -- must be instantiated explicitly because they are not part of the
5188 -- subprogram template.
5189
5190 Instantiate_Subprogram_Contract (Original_Node (Gen_Decl));
5191 Build_Subprogram_Renaming;
5192
5193 Analyze_Instance_And_Renamings;
5194
5195 -- If the generic is marked Import (Intrinsic), then so is the
5196 -- instance. This indicates that there is no body to instantiate. If
5197 -- generic is marked inline, so it the instance, and the anonymous
5198 -- subprogram it renames. If inlined, or else if inlining is enabled
5199 -- for the compilation, we generate the instance body even if it is
5200 -- not within the main unit.
5201
5202 if Is_Intrinsic_Subprogram (Gen_Unit) then
5203 Set_Is_Intrinsic_Subprogram (Anon_Id);
5204 Set_Is_Intrinsic_Subprogram (Act_Decl_Id);
5205
5206 if Chars (Gen_Unit) = Name_Unchecked_Conversion then
5207 Validate_Unchecked_Conversion (N, Act_Decl_Id);
5208 end if;
5209 end if;
5210
5211 -- Inherit convention from generic unit. Intrinsic convention, as for
5212 -- an instance of unchecked conversion, is not inherited because an
5213 -- explicit Ada instance has been created.
5214
5215 if Has_Convention_Pragma (Gen_Unit)
5216 and then Convention (Gen_Unit) /= Convention_Intrinsic
5217 then
5218 Set_Convention (Act_Decl_Id, Convention (Gen_Unit));
5219 Set_Is_Exported (Act_Decl_Id, Is_Exported (Gen_Unit));
5220 end if;
5221
5222 Generate_Definition (Act_Decl_Id);
5223
5224 -- Inherit all inlining-related flags which apply to the generic in
5225 -- the subprogram and its declaration.
5226
5227 Set_Is_Inlined (Act_Decl_Id, Is_Inlined (Gen_Unit));
5228 Set_Is_Inlined (Anon_Id, Is_Inlined (Gen_Unit));
5229
5230 Set_Has_Pragma_Inline (Act_Decl_Id, Has_Pragma_Inline (Gen_Unit));
5231 Set_Has_Pragma_Inline (Anon_Id, Has_Pragma_Inline (Gen_Unit));
5232
5233 Set_Has_Pragma_Inline_Always
5234 (Act_Decl_Id, Has_Pragma_Inline_Always (Gen_Unit));
5235 Set_Has_Pragma_Inline_Always
5236 (Anon_Id, Has_Pragma_Inline_Always (Gen_Unit));
5237
5238 if not Is_Intrinsic_Subprogram (Gen_Unit) then
5239 Check_Elab_Instantiation (N);
5240 end if;
5241
5242 if Is_Dispatching_Operation (Act_Decl_Id)
5243 and then Ada_Version >= Ada_2005
5244 then
5245 declare
5246 Formal : Entity_Id;
5247
5248 begin
5249 Formal := First_Formal (Act_Decl_Id);
5250 while Present (Formal) loop
5251 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type
5252 and then Is_Controlling_Formal (Formal)
5253 and then not Can_Never_Be_Null (Formal)
5254 then
5255 Error_Msg_NE
5256 ("access parameter& is controlling,", N, Formal);
5257 Error_Msg_NE
5258 ("\corresponding parameter of & must be "
5259 & "explicitly null-excluding", N, Gen_Id);
5260 end if;
5261
5262 Next_Formal (Formal);
5263 end loop;
5264 end;
5265 end if;
5266
5267 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
5268
5269 Validate_Categorization_Dependency (N, Act_Decl_Id);
5270
5271 if not Is_Intrinsic_Subprogram (Act_Decl_Id) then
5272 Inherit_Context (Gen_Decl, N);
5273
5274 Restore_Private_Views (Pack_Id, False);
5275
5276 -- If the context requires a full instantiation, mark node for
5277 -- subsequent construction of the body.
5278
5279 if Need_Subprogram_Instance_Body (N, Act_Decl_Id) then
5280 Check_Forward_Instantiation (Gen_Decl);
5281
5282 -- The wrapper package is always delayed, because it does not
5283 -- constitute a freeze point, but to insure that the freeze
5284 -- node is placed properly, it is created directly when
5285 -- instantiating the body (otherwise the freeze node might
5286 -- appear to early for nested instantiations).
5287
5288 elsif Nkind (Parent (N)) = N_Compilation_Unit then
5289
5290 -- For ASIS purposes, indicate that the wrapper package has
5291 -- replaced the instantiation node.
5292
5293 Rewrite (N, Unit (Parent (N)));
5294 Set_Unit (Parent (N), N);
5295 end if;
5296
5297 elsif Nkind (Parent (N)) = N_Compilation_Unit then
5298
5299 -- Replace instance node for library-level instantiations of
5300 -- intrinsic subprograms, for ASIS use.
5301
5302 Rewrite (N, Unit (Parent (N)));
5303 Set_Unit (Parent (N), N);
5304 end if;
5305
5306 if Parent_Installed then
5307 Remove_Parent;
5308 end if;
5309
5310 Restore_Hidden_Primitives (Vis_Prims_List);
5311 Restore_Env;
5312 Env_Installed := False;
5313 Generic_Renamings.Set_Last (0);
5314 Generic_Renamings_HTable.Reset;
5315
5316 Ignore_Pragma_SPARK_Mode := Save_IPSM;
5317 SPARK_Mode := Save_SM;
5318 SPARK_Mode_Pragma := Save_SMP;
5319
5320 if SPARK_Mode = On then
5321 Dynamic_Elaboration_Checks := False;
5322 end if;
5323
5324 end if;
5325
5326 <<Leave>>
5327 if Has_Aspects (N) then
5328 Analyze_Aspect_Specifications (N, Act_Decl_Id);
5329 end if;
5330
5331 exception
5332 when Instantiation_Error =>
5333 if Parent_Installed then
5334 Remove_Parent;
5335 end if;
5336
5337 if Env_Installed then
5338 Restore_Env;
5339 end if;
5340
5341 Ignore_Pragma_SPARK_Mode := Save_IPSM;
5342 SPARK_Mode := Save_SM;
5343 SPARK_Mode_Pragma := Save_SMP;
5344
5345 if SPARK_Mode = On then
5346 Dynamic_Elaboration_Checks := False;
5347 end if;
5348 end Analyze_Subprogram_Instantiation;
5349
5350 -------------------------
5351 -- Get_Associated_Node --
5352 -------------------------
5353
5354 function Get_Associated_Node (N : Node_Id) return Node_Id is
5355 Assoc : Node_Id;
5356
5357 begin
5358 Assoc := Associated_Node (N);
5359
5360 if Nkind (Assoc) /= Nkind (N) then
5361 return Assoc;
5362
5363 elsif Nkind_In (Assoc, N_Aggregate, N_Extension_Aggregate) then
5364 return Assoc;
5365
5366 else
5367 -- If the node is part of an inner generic, it may itself have been
5368 -- remapped into a further generic copy. Associated_Node is otherwise
5369 -- used for the entity of the node, and will be of a different node
5370 -- kind, or else N has been rewritten as a literal or function call.
5371
5372 while Present (Associated_Node (Assoc))
5373 and then Nkind (Associated_Node (Assoc)) = Nkind (Assoc)
5374 loop
5375 Assoc := Associated_Node (Assoc);
5376 end loop;
5377
5378 -- Follow and additional link in case the final node was rewritten.
5379 -- This can only happen with nested generic units.
5380
5381 if (Nkind (Assoc) = N_Identifier or else Nkind (Assoc) in N_Op)
5382 and then Present (Associated_Node (Assoc))
5383 and then (Nkind_In (Associated_Node (Assoc), N_Function_Call,
5384 N_Explicit_Dereference,
5385 N_Integer_Literal,
5386 N_Real_Literal,
5387 N_String_Literal))
5388 then
5389 Assoc := Associated_Node (Assoc);
5390 end if;
5391
5392 -- An additional special case: an unconstrained type in an object
5393 -- declaration may have been rewritten as a local subtype constrained
5394 -- by the expression in the declaration. We need to recover the
5395 -- original entity which may be global.
5396
5397 if Present (Original_Node (Assoc))
5398 and then Nkind (Parent (N)) = N_Object_Declaration
5399 then
5400 Assoc := Original_Node (Assoc);
5401 end if;
5402
5403 return Assoc;
5404 end if;
5405 end Get_Associated_Node;
5406
5407 ----------------------------
5408 -- Build_Function_Wrapper --
5409 ----------------------------
5410
5411 function Build_Function_Wrapper
5412 (Formal_Subp : Entity_Id;
5413 Actual_Subp : Entity_Id) return Node_Id
5414 is
5415 Loc : constant Source_Ptr := Sloc (Current_Scope);
5416 Ret_Type : constant Entity_Id := Get_Instance_Of (Etype (Formal_Subp));
5417 Actuals : List_Id;
5418 Decl : Node_Id;
5419 Func_Name : Node_Id;
5420 Func : Entity_Id;
5421 Parm_Type : Node_Id;
5422 Profile : List_Id := New_List;
5423 Spec : Node_Id;
5424 Act_F : Entity_Id;
5425 Form_F : Entity_Id;
5426 New_F : Entity_Id;
5427
5428 begin
5429 Func_Name := New_Occurrence_Of (Actual_Subp, Loc);
5430
5431 Func := Make_Defining_Identifier (Loc, Chars (Formal_Subp));
5432 Set_Ekind (Func, E_Function);
5433 Set_Is_Generic_Actual_Subprogram (Func);
5434
5435 Actuals := New_List;
5436 Profile := New_List;
5437
5438 Act_F := First_Formal (Actual_Subp);
5439 Form_F := First_Formal (Formal_Subp);
5440 while Present (Form_F) loop
5441
5442 -- Create new formal for profile of wrapper, and add a reference
5443 -- to it in the list of actuals for the enclosing call. The name
5444 -- must be that of the formal in the formal subprogram, because
5445 -- calls to it in the generic body may use named associations.
5446
5447 New_F := Make_Defining_Identifier (Loc, Chars (Form_F));
5448
5449 Parm_Type :=
5450 New_Occurrence_Of (Get_Instance_Of (Etype (Form_F)), Loc);
5451
5452 Append_To (Profile,
5453 Make_Parameter_Specification (Loc,
5454 Defining_Identifier => New_F,
5455 Parameter_Type => Parm_Type));
5456
5457 Append_To (Actuals, New_Occurrence_Of (New_F, Loc));
5458 Next_Formal (Form_F);
5459
5460 if Present (Act_F) then
5461 Next_Formal (Act_F);
5462 end if;
5463 end loop;
5464
5465 Spec :=
5466 Make_Function_Specification (Loc,
5467 Defining_Unit_Name => Func,
5468 Parameter_Specifications => Profile,
5469 Result_Definition => New_Occurrence_Of (Ret_Type, Loc));
5470
5471 Decl :=
5472 Make_Expression_Function (Loc,
5473 Specification => Spec,
5474 Expression =>
5475 Make_Function_Call (Loc,
5476 Name => Func_Name,
5477 Parameter_Associations => Actuals));
5478
5479 return Decl;
5480 end Build_Function_Wrapper;
5481
5482 ----------------------------
5483 -- Build_Operator_Wrapper --
5484 ----------------------------
5485
5486 function Build_Operator_Wrapper
5487 (Formal_Subp : Entity_Id;
5488 Actual_Subp : Entity_Id) return Node_Id
5489 is
5490 Loc : constant Source_Ptr := Sloc (Current_Scope);
5491 Ret_Type : constant Entity_Id :=
5492 Get_Instance_Of (Etype (Formal_Subp));
5493 Op_Type : constant Entity_Id :=
5494 Get_Instance_Of (Etype (First_Formal (Formal_Subp)));
5495 Is_Binary : constant Boolean :=
5496 Present (Next_Formal (First_Formal (Formal_Subp)));
5497
5498 Decl : Node_Id;
5499 Expr : Node_Id;
5500 F1, F2 : Entity_Id;
5501 Func : Entity_Id;
5502 Op_Name : Name_Id;
5503 Spec : Node_Id;
5504 L, R : Node_Id;
5505
5506 begin
5507 Op_Name := Chars (Actual_Subp);
5508
5509 -- Create entities for wrapper function and its formals
5510
5511 F1 := Make_Temporary (Loc, 'A');
5512 F2 := Make_Temporary (Loc, 'B');
5513 L := New_Occurrence_Of (F1, Loc);
5514 R := New_Occurrence_Of (F2, Loc);
5515
5516 Func := Make_Defining_Identifier (Loc, Chars (Formal_Subp));
5517 Set_Ekind (Func, E_Function);
5518 Set_Is_Generic_Actual_Subprogram (Func);
5519
5520 Spec :=
5521 Make_Function_Specification (Loc,
5522 Defining_Unit_Name => Func,
5523 Parameter_Specifications => New_List (
5524 Make_Parameter_Specification (Loc,
5525 Defining_Identifier => F1,
5526 Parameter_Type => New_Occurrence_Of (Op_Type, Loc))),
5527 Result_Definition => New_Occurrence_Of (Ret_Type, Loc));
5528
5529 if Is_Binary then
5530 Append_To (Parameter_Specifications (Spec),
5531 Make_Parameter_Specification (Loc,
5532 Defining_Identifier => F2,
5533 Parameter_Type => New_Occurrence_Of (Op_Type, Loc)));
5534 end if;
5535
5536 -- Build expression as a function call, or as an operator node
5537 -- that corresponds to the name of the actual, starting with
5538 -- binary operators.
5539
5540 if Op_Name not in Any_Operator_Name then
5541 Expr :=
5542 Make_Function_Call (Loc,
5543 Name =>
5544 New_Occurrence_Of (Actual_Subp, Loc),
5545 Parameter_Associations => New_List (L));
5546
5547 if Is_Binary then
5548 Append_To (Parameter_Associations (Expr), R);
5549 end if;
5550
5551 -- Binary operators
5552
5553 elsif Is_Binary then
5554 if Op_Name = Name_Op_And then
5555 Expr := Make_Op_And (Loc, Left_Opnd => L, Right_Opnd => R);
5556 elsif Op_Name = Name_Op_Or then
5557 Expr := Make_Op_Or (Loc, Left_Opnd => L, Right_Opnd => R);
5558 elsif Op_Name = Name_Op_Xor then
5559 Expr := Make_Op_Xor (Loc, Left_Opnd => L, Right_Opnd => R);
5560 elsif Op_Name = Name_Op_Eq then
5561 Expr := Make_Op_Eq (Loc, Left_Opnd => L, Right_Opnd => R);
5562 elsif Op_Name = Name_Op_Ne then
5563 Expr := Make_Op_Ne (Loc, Left_Opnd => L, Right_Opnd => R);
5564 elsif Op_Name = Name_Op_Le then
5565 Expr := Make_Op_Le (Loc, Left_Opnd => L, Right_Opnd => R);
5566 elsif Op_Name = Name_Op_Gt then
5567 Expr := Make_Op_Gt (Loc, Left_Opnd => L, Right_Opnd => R);
5568 elsif Op_Name = Name_Op_Ge then
5569 Expr := Make_Op_Ge (Loc, Left_Opnd => L, Right_Opnd => R);
5570 elsif Op_Name = Name_Op_Lt then
5571 Expr := Make_Op_Lt (Loc, Left_Opnd => L, Right_Opnd => R);
5572 elsif Op_Name = Name_Op_Add then
5573 Expr := Make_Op_Add (Loc, Left_Opnd => L, Right_Opnd => R);
5574 elsif Op_Name = Name_Op_Subtract then
5575 Expr := Make_Op_Subtract (Loc, Left_Opnd => L, Right_Opnd => R);
5576 elsif Op_Name = Name_Op_Concat then
5577 Expr := Make_Op_Concat (Loc, Left_Opnd => L, Right_Opnd => R);
5578 elsif Op_Name = Name_Op_Multiply then
5579 Expr := Make_Op_Multiply (Loc, Left_Opnd => L, Right_Opnd => R);
5580 elsif Op_Name = Name_Op_Divide then
5581 Expr := Make_Op_Divide (Loc, Left_Opnd => L, Right_Opnd => R);
5582 elsif Op_Name = Name_Op_Mod then
5583 Expr := Make_Op_Mod (Loc, Left_Opnd => L, Right_Opnd => R);
5584 elsif Op_Name = Name_Op_Rem then
5585 Expr := Make_Op_Rem (Loc, Left_Opnd => L, Right_Opnd => R);
5586 elsif Op_Name = Name_Op_Expon then
5587 Expr := Make_Op_Expon (Loc, Left_Opnd => L, Right_Opnd => R);
5588 end if;
5589
5590 -- Unary operators
5591
5592 else
5593 if Op_Name = Name_Op_Add then
5594 Expr := Make_Op_Plus (Loc, Right_Opnd => L);
5595 elsif Op_Name = Name_Op_Subtract then
5596 Expr := Make_Op_Minus (Loc, Right_Opnd => L);
5597 elsif Op_Name = Name_Op_Abs then
5598 Expr := Make_Op_Abs (Loc, Right_Opnd => L);
5599 elsif Op_Name = Name_Op_Not then
5600 Expr := Make_Op_Not (Loc, Right_Opnd => L);
5601 end if;
5602 end if;
5603
5604 Decl :=
5605 Make_Expression_Function (Loc,
5606 Specification => Spec,
5607 Expression => Expr);
5608
5609 return Decl;
5610 end Build_Operator_Wrapper;
5611
5612 -------------------------------------------
5613 -- Build_Instance_Compilation_Unit_Nodes --
5614 -------------------------------------------
5615
5616 procedure Build_Instance_Compilation_Unit_Nodes
5617 (N : Node_Id;
5618 Act_Body : Node_Id;
5619 Act_Decl : Node_Id)
5620 is
5621 Decl_Cunit : Node_Id;
5622 Body_Cunit : Node_Id;
5623 Citem : Node_Id;
5624 New_Main : constant Entity_Id := Defining_Entity (Act_Decl);
5625 Old_Main : constant Entity_Id := Cunit_Entity (Main_Unit);
5626
5627 begin
5628 -- A new compilation unit node is built for the instance declaration
5629
5630 Decl_Cunit :=
5631 Make_Compilation_Unit (Sloc (N),
5632 Context_Items => Empty_List,
5633 Unit => Act_Decl,
5634 Aux_Decls_Node => Make_Compilation_Unit_Aux (Sloc (N)));
5635
5636 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
5637
5638 -- The new compilation unit is linked to its body, but both share the
5639 -- same file, so we do not set Body_Required on the new unit so as not
5640 -- to create a spurious dependency on a non-existent body in the ali.
5641 -- This simplifies CodePeer unit traversal.
5642
5643 -- We use the original instantiation compilation unit as the resulting
5644 -- compilation unit of the instance, since this is the main unit.
5645
5646 Rewrite (N, Act_Body);
5647
5648 -- Propagate the aspect specifications from the package body template to
5649 -- the instantiated version of the package body.
5650
5651 if Has_Aspects (Act_Body) then
5652 Set_Aspect_Specifications
5653 (N, New_Copy_List_Tree (Aspect_Specifications (Act_Body)));
5654 end if;
5655
5656 Body_Cunit := Parent (N);
5657
5658 -- The two compilation unit nodes are linked by the Library_Unit field
5659
5660 Set_Library_Unit (Decl_Cunit, Body_Cunit);
5661 Set_Library_Unit (Body_Cunit, Decl_Cunit);
5662
5663 -- Preserve the private nature of the package if needed
5664
5665 Set_Private_Present (Decl_Cunit, Private_Present (Body_Cunit));
5666
5667 -- If the instance is not the main unit, its context, categorization
5668 -- and elaboration entity are not relevant to the compilation.
5669
5670 if Body_Cunit /= Cunit (Main_Unit) then
5671 Make_Instance_Unit (Body_Cunit, In_Main => False);
5672 return;
5673 end if;
5674
5675 -- The context clause items on the instantiation, which are now attached
5676 -- to the body compilation unit (since the body overwrote the original
5677 -- instantiation node), semantically belong on the spec, so copy them
5678 -- there. It's harmless to leave them on the body as well. In fact one
5679 -- could argue that they belong in both places.
5680
5681 Citem := First (Context_Items (Body_Cunit));
5682 while Present (Citem) loop
5683 Append (New_Copy (Citem), Context_Items (Decl_Cunit));
5684 Next (Citem);
5685 end loop;
5686
5687 -- Propagate categorization flags on packages, so that they appear in
5688 -- the ali file for the spec of the unit.
5689
5690 if Ekind (New_Main) = E_Package then
5691 Set_Is_Pure (Old_Main, Is_Pure (New_Main));
5692 Set_Is_Preelaborated (Old_Main, Is_Preelaborated (New_Main));
5693 Set_Is_Remote_Types (Old_Main, Is_Remote_Types (New_Main));
5694 Set_Is_Shared_Passive (Old_Main, Is_Shared_Passive (New_Main));
5695 Set_Is_Remote_Call_Interface
5696 (Old_Main, Is_Remote_Call_Interface (New_Main));
5697 end if;
5698
5699 -- Make entry in Units table, so that binder can generate call to
5700 -- elaboration procedure for body, if any.
5701
5702 Make_Instance_Unit (Body_Cunit, In_Main => True);
5703 Main_Unit_Entity := New_Main;
5704 Set_Cunit_Entity (Main_Unit, Main_Unit_Entity);
5705
5706 -- Build elaboration entity, since the instance may certainly generate
5707 -- elaboration code requiring a flag for protection.
5708
5709 Build_Elaboration_Entity (Decl_Cunit, New_Main);
5710 end Build_Instance_Compilation_Unit_Nodes;
5711
5712 -----------------------------
5713 -- Check_Access_Definition --
5714 -----------------------------
5715
5716 procedure Check_Access_Definition (N : Node_Id) is
5717 begin
5718 pragma Assert
5719 (Ada_Version >= Ada_2005 and then Present (Access_Definition (N)));
5720 null;
5721 end Check_Access_Definition;
5722
5723 -----------------------------------
5724 -- Check_Formal_Package_Instance --
5725 -----------------------------------
5726
5727 -- If the formal has specific parameters, they must match those of the
5728 -- actual. Both of them are instances, and the renaming declarations for
5729 -- their formal parameters appear in the same order in both. The analyzed
5730 -- formal has been analyzed in the context of the current instance.
5731
5732 procedure Check_Formal_Package_Instance
5733 (Formal_Pack : Entity_Id;
5734 Actual_Pack : Entity_Id)
5735 is
5736 E1 : Entity_Id := First_Entity (Actual_Pack);
5737 E2 : Entity_Id := First_Entity (Formal_Pack);
5738
5739 Expr1 : Node_Id;
5740 Expr2 : Node_Id;
5741
5742 procedure Check_Mismatch (B : Boolean);
5743 -- Common error routine for mismatch between the parameters of the
5744 -- actual instance and those of the formal package.
5745
5746 function Same_Instantiated_Constant (E1, E2 : Entity_Id) return Boolean;
5747 -- The formal may come from a nested formal package, and the actual may
5748 -- have been constant-folded. To determine whether the two denote the
5749 -- same entity we may have to traverse several definitions to recover
5750 -- the ultimate entity that they refer to.
5751
5752 function Same_Instantiated_Variable (E1, E2 : Entity_Id) return Boolean;
5753 -- Similarly, if the formal comes from a nested formal package, the
5754 -- actual may designate the formal through multiple renamings, which
5755 -- have to be followed to determine the original variable in question.
5756
5757 --------------------
5758 -- Check_Mismatch --
5759 --------------------
5760
5761 procedure Check_Mismatch (B : Boolean) is
5762 Kind : constant Node_Kind := Nkind (Parent (E2));
5763
5764 begin
5765 if Kind = N_Formal_Type_Declaration then
5766 return;
5767
5768 elsif Nkind_In (Kind, N_Formal_Object_Declaration,
5769 N_Formal_Package_Declaration)
5770 or else Kind in N_Formal_Subprogram_Declaration
5771 then
5772 null;
5773
5774 -- Ada 2012: If both formal and actual are incomplete types they
5775 -- are conformant.
5776
5777 elsif Is_Incomplete_Type (E1) and then Is_Incomplete_Type (E2) then
5778 null;
5779
5780 elsif B then
5781 Error_Msg_NE
5782 ("actual for & in actual instance does not match formal",
5783 Parent (Actual_Pack), E1);
5784 end if;
5785 end Check_Mismatch;
5786
5787 --------------------------------
5788 -- Same_Instantiated_Constant --
5789 --------------------------------
5790
5791 function Same_Instantiated_Constant
5792 (E1, E2 : Entity_Id) return Boolean
5793 is
5794 Ent : Entity_Id;
5795
5796 begin
5797 Ent := E2;
5798 while Present (Ent) loop
5799 if E1 = Ent then
5800 return True;
5801
5802 elsif Ekind (Ent) /= E_Constant then
5803 return False;
5804
5805 elsif Is_Entity_Name (Constant_Value (Ent)) then
5806 if Entity (Constant_Value (Ent)) = E1 then
5807 return True;
5808 else
5809 Ent := Entity (Constant_Value (Ent));
5810 end if;
5811
5812 -- The actual may be a constant that has been folded. Recover
5813 -- original name.
5814
5815 elsif Is_Entity_Name (Original_Node (Constant_Value (Ent))) then
5816 Ent := Entity (Original_Node (Constant_Value (Ent)));
5817
5818 else
5819 return False;
5820 end if;
5821 end loop;
5822
5823 return False;
5824 end Same_Instantiated_Constant;
5825
5826 --------------------------------
5827 -- Same_Instantiated_Variable --
5828 --------------------------------
5829
5830 function Same_Instantiated_Variable
5831 (E1, E2 : Entity_Id) return Boolean
5832 is
5833 function Original_Entity (E : Entity_Id) return Entity_Id;
5834 -- Follow chain of renamings to the ultimate ancestor
5835
5836 ---------------------
5837 -- Original_Entity --
5838 ---------------------
5839
5840 function Original_Entity (E : Entity_Id) return Entity_Id is
5841 Orig : Entity_Id;
5842
5843 begin
5844 Orig := E;
5845 while Nkind (Parent (Orig)) = N_Object_Renaming_Declaration
5846 and then Present (Renamed_Object (Orig))
5847 and then Is_Entity_Name (Renamed_Object (Orig))
5848 loop
5849 Orig := Entity (Renamed_Object (Orig));
5850 end loop;
5851
5852 return Orig;
5853 end Original_Entity;
5854
5855 -- Start of processing for Same_Instantiated_Variable
5856
5857 begin
5858 return Ekind (E1) = Ekind (E2)
5859 and then Original_Entity (E1) = Original_Entity (E2);
5860 end Same_Instantiated_Variable;
5861
5862 -- Start of processing for Check_Formal_Package_Instance
5863
5864 begin
5865 while Present (E1) and then Present (E2) loop
5866 exit when Ekind (E1) = E_Package
5867 and then Renamed_Entity (E1) = Renamed_Entity (Actual_Pack);
5868
5869 -- If the formal is the renaming of the formal package, this
5870 -- is the end of its formal part, which may occur before the
5871 -- end of the formal part in the actual in the presence of
5872 -- defaulted parameters in the formal package.
5873
5874 exit when Nkind (Parent (E2)) = N_Package_Renaming_Declaration
5875 and then Renamed_Entity (E2) = Scope (E2);
5876
5877 -- The analysis of the actual may generate additional internal
5878 -- entities. If the formal is defaulted, there is no corresponding
5879 -- analysis and the internal entities must be skipped, until we
5880 -- find corresponding entities again.
5881
5882 if Comes_From_Source (E2)
5883 and then not Comes_From_Source (E1)
5884 and then Chars (E1) /= Chars (E2)
5885 then
5886 while Present (E1) and then Chars (E1) /= Chars (E2) loop
5887 Next_Entity (E1);
5888 end loop;
5889 end if;
5890
5891 if No (E1) then
5892 return;
5893
5894 -- If the formal entity comes from a formal declaration, it was
5895 -- defaulted in the formal package, and no check is needed on it.
5896
5897 elsif Nkind (Parent (E2)) = N_Formal_Object_Declaration then
5898 goto Next_E;
5899
5900 -- Ditto for defaulted formal subprograms.
5901
5902 elsif Is_Overloadable (E1)
5903 and then Nkind (Unit_Declaration_Node (E2)) in
5904 N_Formal_Subprogram_Declaration
5905 then
5906 goto Next_E;
5907
5908 elsif Is_Type (E1) then
5909
5910 -- Subtypes must statically match. E1, E2 are the local entities
5911 -- that are subtypes of the actuals. Itypes generated for other
5912 -- parameters need not be checked, the check will be performed
5913 -- on the parameters themselves.
5914
5915 -- If E2 is a formal type declaration, it is a defaulted parameter
5916 -- and needs no checking.
5917
5918 if not Is_Itype (E1) and then not Is_Itype (E2) then
5919 Check_Mismatch
5920 (not Is_Type (E2)
5921 or else Etype (E1) /= Etype (E2)
5922 or else not Subtypes_Statically_Match (E1, E2));
5923 end if;
5924
5925 elsif Ekind (E1) = E_Constant then
5926
5927 -- IN parameters must denote the same static value, or the same
5928 -- constant, or the literal null.
5929
5930 Expr1 := Expression (Parent (E1));
5931
5932 if Ekind (E2) /= E_Constant then
5933 Check_Mismatch (True);
5934 goto Next_E;
5935 else
5936 Expr2 := Expression (Parent (E2));
5937 end if;
5938
5939 if Is_OK_Static_Expression (Expr1) then
5940 if not Is_OK_Static_Expression (Expr2) then
5941 Check_Mismatch (True);
5942
5943 elsif Is_Discrete_Type (Etype (E1)) then
5944 declare
5945 V1 : constant Uint := Expr_Value (Expr1);
5946 V2 : constant Uint := Expr_Value (Expr2);
5947 begin
5948 Check_Mismatch (V1 /= V2);
5949 end;
5950
5951 elsif Is_Real_Type (Etype (E1)) then
5952 declare
5953 V1 : constant Ureal := Expr_Value_R (Expr1);
5954 V2 : constant Ureal := Expr_Value_R (Expr2);
5955 begin
5956 Check_Mismatch (V1 /= V2);
5957 end;
5958
5959 elsif Is_String_Type (Etype (E1))
5960 and then Nkind (Expr1) = N_String_Literal
5961 then
5962 if Nkind (Expr2) /= N_String_Literal then
5963 Check_Mismatch (True);
5964 else
5965 Check_Mismatch
5966 (not String_Equal (Strval (Expr1), Strval (Expr2)));
5967 end if;
5968 end if;
5969
5970 elsif Is_Entity_Name (Expr1) then
5971 if Is_Entity_Name (Expr2) then
5972 if Entity (Expr1) = Entity (Expr2) then
5973 null;
5974 else
5975 Check_Mismatch
5976 (not Same_Instantiated_Constant
5977 (Entity (Expr1), Entity (Expr2)));
5978 end if;
5979
5980 else
5981 Check_Mismatch (True);
5982 end if;
5983
5984 elsif Is_Entity_Name (Original_Node (Expr1))
5985 and then Is_Entity_Name (Expr2)
5986 and then Same_Instantiated_Constant
5987 (Entity (Original_Node (Expr1)), Entity (Expr2))
5988 then
5989 null;
5990
5991 elsif Nkind (Expr1) = N_Null then
5992 Check_Mismatch (Nkind (Expr1) /= N_Null);
5993
5994 else
5995 Check_Mismatch (True);
5996 end if;
5997
5998 elsif Ekind (E1) = E_Variable then
5999 Check_Mismatch (not Same_Instantiated_Variable (E1, E2));
6000
6001 elsif Ekind (E1) = E_Package then
6002 Check_Mismatch
6003 (Ekind (E1) /= Ekind (E2)
6004 or else Renamed_Object (E1) /= Renamed_Object (E2));
6005
6006 elsif Is_Overloadable (E1) then
6007
6008 -- Verify that the actual subprograms match. Note that actuals
6009 -- that are attributes are rewritten as subprograms. If the
6010 -- subprogram in the formal package is defaulted, no check is
6011 -- needed. Note that this can only happen in Ada 2005 when the
6012 -- formal package can be partially parameterized.
6013
6014 if Nkind (Unit_Declaration_Node (E1)) =
6015 N_Subprogram_Renaming_Declaration
6016 and then From_Default (Unit_Declaration_Node (E1))
6017 then
6018 null;
6019
6020 -- If the formal package has an "others" box association that
6021 -- covers this formal, there is no need for a check either.
6022
6023 elsif Nkind (Unit_Declaration_Node (E2)) in
6024 N_Formal_Subprogram_Declaration
6025 and then Box_Present (Unit_Declaration_Node (E2))
6026 then
6027 null;
6028
6029 -- No check needed if subprogram is a defaulted null procedure
6030
6031 elsif No (Alias (E2))
6032 and then Ekind (E2) = E_Procedure
6033 and then
6034 Null_Present (Specification (Unit_Declaration_Node (E2)))
6035 then
6036 null;
6037
6038 -- Otherwise the actual in the formal and the actual in the
6039 -- instantiation of the formal must match, up to renamings.
6040
6041 else
6042 Check_Mismatch
6043 (Ekind (E2) /= Ekind (E1) or else (Alias (E1)) /= Alias (E2));
6044 end if;
6045
6046 else
6047 raise Program_Error;
6048 end if;
6049
6050 <<Next_E>>
6051 Next_Entity (E1);
6052 Next_Entity (E2);
6053 end loop;
6054 end Check_Formal_Package_Instance;
6055
6056 ---------------------------
6057 -- Check_Formal_Packages --
6058 ---------------------------
6059
6060 procedure Check_Formal_Packages (P_Id : Entity_Id) is
6061 E : Entity_Id;
6062 Formal_P : Entity_Id;
6063
6064 begin
6065 -- Iterate through the declarations in the instance, looking for package
6066 -- renaming declarations that denote instances of formal packages. Stop
6067 -- when we find the renaming of the current package itself. The
6068 -- declaration for a formal package without a box is followed by an
6069 -- internal entity that repeats the instantiation.
6070
6071 E := First_Entity (P_Id);
6072 while Present (E) loop
6073 if Ekind (E) = E_Package then
6074 if Renamed_Object (E) = P_Id then
6075 exit;
6076
6077 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
6078 null;
6079
6080 elsif not Box_Present (Parent (Associated_Formal_Package (E))) then
6081 Formal_P := Next_Entity (E);
6082 Check_Formal_Package_Instance (Formal_P, E);
6083
6084 -- After checking, remove the internal validating package. It
6085 -- is only needed for semantic checks, and as it may contain
6086 -- generic formal declarations it should not reach gigi.
6087
6088 Remove (Unit_Declaration_Node (Formal_P));
6089 end if;
6090 end if;
6091
6092 Next_Entity (E);
6093 end loop;
6094 end Check_Formal_Packages;
6095
6096 ---------------------------------
6097 -- Check_Forward_Instantiation --
6098 ---------------------------------
6099
6100 procedure Check_Forward_Instantiation (Decl : Node_Id) is
6101 S : Entity_Id;
6102 Gen_Comp : Entity_Id := Cunit_Entity (Get_Source_Unit (Decl));
6103
6104 begin
6105 -- The instantiation appears before the generic body if we are in the
6106 -- scope of the unit containing the generic, either in its spec or in
6107 -- the package body, and before the generic body.
6108
6109 if Ekind (Gen_Comp) = E_Package_Body then
6110 Gen_Comp := Spec_Entity (Gen_Comp);
6111 end if;
6112
6113 if In_Open_Scopes (Gen_Comp)
6114 and then No (Corresponding_Body (Decl))
6115 then
6116 S := Current_Scope;
6117
6118 while Present (S)
6119 and then not Is_Compilation_Unit (S)
6120 and then not Is_Child_Unit (S)
6121 loop
6122 if Ekind (S) = E_Package then
6123 Set_Has_Forward_Instantiation (S);
6124 end if;
6125
6126 S := Scope (S);
6127 end loop;
6128 end if;
6129 end Check_Forward_Instantiation;
6130
6131 ---------------------------
6132 -- Check_Generic_Actuals --
6133 ---------------------------
6134
6135 -- The visibility of the actuals may be different between the point of
6136 -- generic instantiation and the instantiation of the body.
6137
6138 procedure Check_Generic_Actuals
6139 (Instance : Entity_Id;
6140 Is_Formal_Box : Boolean)
6141 is
6142 E : Entity_Id;
6143 Astype : Entity_Id;
6144
6145 function Denotes_Previous_Actual (Typ : Entity_Id) return Boolean;
6146 -- For a formal that is an array type, the component type is often a
6147 -- previous formal in the same unit. The privacy status of the component
6148 -- type will have been examined earlier in the traversal of the
6149 -- corresponding actuals, and this status should not be modified for
6150 -- the array (sub)type itself. However, if the base type of the array
6151 -- (sub)type is private, its full view must be restored in the body to
6152 -- be consistent with subsequent index subtypes, etc.
6153 --
6154 -- To detect this case we have to rescan the list of formals, which is
6155 -- usually short enough to ignore the resulting inefficiency.
6156
6157 -----------------------------
6158 -- Denotes_Previous_Actual --
6159 -----------------------------
6160
6161 function Denotes_Previous_Actual (Typ : Entity_Id) return Boolean is
6162 Prev : Entity_Id;
6163
6164 begin
6165 Prev := First_Entity (Instance);
6166 while Present (Prev) loop
6167 if Is_Type (Prev)
6168 and then Nkind (Parent (Prev)) = N_Subtype_Declaration
6169 and then Is_Entity_Name (Subtype_Indication (Parent (Prev)))
6170 and then Entity (Subtype_Indication (Parent (Prev))) = Typ
6171 then
6172 return True;
6173
6174 elsif Prev = E then
6175 return False;
6176
6177 else
6178 Next_Entity (Prev);
6179 end if;
6180 end loop;
6181
6182 return False;
6183 end Denotes_Previous_Actual;
6184
6185 -- Start of processing for Check_Generic_Actuals
6186
6187 begin
6188 E := First_Entity (Instance);
6189 while Present (E) loop
6190 if Is_Type (E)
6191 and then Nkind (Parent (E)) = N_Subtype_Declaration
6192 and then Scope (Etype (E)) /= Instance
6193 and then Is_Entity_Name (Subtype_Indication (Parent (E)))
6194 then
6195 if Is_Array_Type (E)
6196 and then not Is_Private_Type (Etype (E))
6197 and then Denotes_Previous_Actual (Component_Type (E))
6198 then
6199 null;
6200 else
6201 Check_Private_View (Subtype_Indication (Parent (E)));
6202 end if;
6203
6204 Set_Is_Generic_Actual_Type (E, True);
6205 Set_Is_Hidden (E, False);
6206 Set_Is_Potentially_Use_Visible (E,
6207 In_Use (Instance));
6208
6209 -- We constructed the generic actual type as a subtype of the
6210 -- supplied type. This means that it normally would not inherit
6211 -- subtype specific attributes of the actual, which is wrong for
6212 -- the generic case.
6213
6214 Astype := Ancestor_Subtype (E);
6215
6216 if No (Astype) then
6217
6218 -- This can happen when E is an itype that is the full view of
6219 -- a private type completed, e.g. with a constrained array. In
6220 -- that case, use the first subtype, which will carry size
6221 -- information. The base type itself is unconstrained and will
6222 -- not carry it.
6223
6224 Astype := First_Subtype (E);
6225 end if;
6226
6227 Set_Size_Info (E, (Astype));
6228 Set_RM_Size (E, RM_Size (Astype));
6229 Set_First_Rep_Item (E, First_Rep_Item (Astype));
6230
6231 if Is_Discrete_Or_Fixed_Point_Type (E) then
6232 Set_RM_Size (E, RM_Size (Astype));
6233
6234 -- In nested instances, the base type of an access actual may
6235 -- itself be private, and need to be exchanged.
6236
6237 elsif Is_Access_Type (E)
6238 and then Is_Private_Type (Etype (E))
6239 then
6240 Check_Private_View
6241 (New_Occurrence_Of (Etype (E), Sloc (Instance)));
6242 end if;
6243
6244 elsif Ekind (E) = E_Package then
6245
6246 -- If this is the renaming for the current instance, we're done.
6247 -- Otherwise it is a formal package. If the corresponding formal
6248 -- was declared with a box, the (instantiations of the) generic
6249 -- formal part are also visible. Otherwise, ignore the entity
6250 -- created to validate the actuals.
6251
6252 if Renamed_Object (E) = Instance then
6253 exit;
6254
6255 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
6256 null;
6257
6258 -- The visibility of a formal of an enclosing generic is already
6259 -- correct.
6260
6261 elsif Denotes_Formal_Package (E) then
6262 null;
6263
6264 elsif Present (Associated_Formal_Package (E))
6265 and then not Is_Generic_Formal (E)
6266 then
6267 if Box_Present (Parent (Associated_Formal_Package (E))) then
6268 Check_Generic_Actuals (Renamed_Object (E), True);
6269
6270 else
6271 Check_Generic_Actuals (Renamed_Object (E), False);
6272 end if;
6273
6274 Set_Is_Hidden (E, False);
6275 end if;
6276
6277 -- If this is a subprogram instance (in a wrapper package) the
6278 -- actual is fully visible.
6279
6280 elsif Is_Wrapper_Package (Instance) then
6281 Set_Is_Hidden (E, False);
6282
6283 -- If the formal package is declared with a box, or if the formal
6284 -- parameter is defaulted, it is visible in the body.
6285
6286 elsif Is_Formal_Box or else Is_Visible_Formal (E) then
6287 Set_Is_Hidden (E, False);
6288 end if;
6289
6290 if Ekind (E) = E_Constant then
6291
6292 -- If the type of the actual is a private type declared in the
6293 -- enclosing scope of the generic unit, the body of the generic
6294 -- sees the full view of the type (because it has to appear in
6295 -- the corresponding package body). If the type is private now,
6296 -- exchange views to restore the proper visiblity in the instance.
6297
6298 declare
6299 Typ : constant Entity_Id := Base_Type (Etype (E));
6300 -- The type of the actual
6301
6302 Gen_Id : Entity_Id;
6303 -- The generic unit
6304
6305 Parent_Scope : Entity_Id;
6306 -- The enclosing scope of the generic unit
6307
6308 begin
6309 if Is_Wrapper_Package (Instance) then
6310 Gen_Id :=
6311 Generic_Parent
6312 (Specification
6313 (Unit_Declaration_Node
6314 (Related_Instance (Instance))));
6315 else
6316 Gen_Id :=
6317 Generic_Parent (Package_Specification (Instance));
6318 end if;
6319
6320 Parent_Scope := Scope (Gen_Id);
6321
6322 -- The exchange is only needed if the generic is defined
6323 -- within a package which is not a common ancestor of the
6324 -- scope of the instance, and is not already in scope.
6325
6326 if Is_Private_Type (Typ)
6327 and then Scope (Typ) = Parent_Scope
6328 and then Scope (Instance) /= Parent_Scope
6329 and then Ekind (Parent_Scope) = E_Package
6330 and then not Is_Child_Unit (Gen_Id)
6331 then
6332 Switch_View (Typ);
6333
6334 -- If the type of the entity is a subtype, it may also have
6335 -- to be made visible, together with the base type of its
6336 -- full view, after exchange.
6337
6338 if Is_Private_Type (Etype (E)) then
6339 Switch_View (Etype (E));
6340 Switch_View (Base_Type (Etype (E)));
6341 end if;
6342 end if;
6343 end;
6344 end if;
6345
6346 Next_Entity (E);
6347 end loop;
6348 end Check_Generic_Actuals;
6349
6350 ------------------------------
6351 -- Check_Generic_Child_Unit --
6352 ------------------------------
6353
6354 procedure Check_Generic_Child_Unit
6355 (Gen_Id : Node_Id;
6356 Parent_Installed : in out Boolean)
6357 is
6358 Loc : constant Source_Ptr := Sloc (Gen_Id);
6359 Gen_Par : Entity_Id := Empty;
6360 E : Entity_Id;
6361 Inst_Par : Entity_Id;
6362 S : Node_Id;
6363
6364 function Find_Generic_Child
6365 (Scop : Entity_Id;
6366 Id : Node_Id) return Entity_Id;
6367 -- Search generic parent for possible child unit with the given name
6368
6369 function In_Enclosing_Instance return Boolean;
6370 -- Within an instance of the parent, the child unit may be denoted by
6371 -- a simple name, or an abbreviated expanded name. Examine enclosing
6372 -- scopes to locate a possible parent instantiation.
6373
6374 ------------------------
6375 -- Find_Generic_Child --
6376 ------------------------
6377
6378 function Find_Generic_Child
6379 (Scop : Entity_Id;
6380 Id : Node_Id) return Entity_Id
6381 is
6382 E : Entity_Id;
6383
6384 begin
6385 -- If entity of name is already set, instance has already been
6386 -- resolved, e.g. in an enclosing instantiation.
6387
6388 if Present (Entity (Id)) then
6389 if Scope (Entity (Id)) = Scop then
6390 return Entity (Id);
6391 else
6392 return Empty;
6393 end if;
6394
6395 else
6396 E := First_Entity (Scop);
6397 while Present (E) loop
6398 if Chars (E) = Chars (Id)
6399 and then Is_Child_Unit (E)
6400 then
6401 if Is_Child_Unit (E)
6402 and then not Is_Visible_Lib_Unit (E)
6403 then
6404 Error_Msg_NE
6405 ("generic child unit& is not visible", Gen_Id, E);
6406 end if;
6407
6408 Set_Entity (Id, E);
6409 return E;
6410 end if;
6411
6412 Next_Entity (E);
6413 end loop;
6414
6415 return Empty;
6416 end if;
6417 end Find_Generic_Child;
6418
6419 ---------------------------
6420 -- In_Enclosing_Instance --
6421 ---------------------------
6422
6423 function In_Enclosing_Instance return Boolean is
6424 Enclosing_Instance : Node_Id;
6425 Instance_Decl : Node_Id;
6426
6427 begin
6428 -- We do not inline any call that contains instantiations, except
6429 -- for instantiations of Unchecked_Conversion, so if we are within
6430 -- an inlined body the current instance does not require parents.
6431
6432 if In_Inlined_Body then
6433 pragma Assert (Chars (Gen_Id) = Name_Unchecked_Conversion);
6434 return False;
6435 end if;
6436
6437 -- Loop to check enclosing scopes
6438
6439 Enclosing_Instance := Current_Scope;
6440 while Present (Enclosing_Instance) loop
6441 Instance_Decl := Unit_Declaration_Node (Enclosing_Instance);
6442
6443 if Ekind (Enclosing_Instance) = E_Package
6444 and then Is_Generic_Instance (Enclosing_Instance)
6445 and then Present
6446 (Generic_Parent (Specification (Instance_Decl)))
6447 then
6448 -- Check whether the generic we are looking for is a child of
6449 -- this instance.
6450
6451 E := Find_Generic_Child
6452 (Generic_Parent (Specification (Instance_Decl)), Gen_Id);
6453 exit when Present (E);
6454
6455 else
6456 E := Empty;
6457 end if;
6458
6459 Enclosing_Instance := Scope (Enclosing_Instance);
6460 end loop;
6461
6462 if No (E) then
6463
6464 -- Not a child unit
6465
6466 Analyze (Gen_Id);
6467 return False;
6468
6469 else
6470 Rewrite (Gen_Id,
6471 Make_Expanded_Name (Loc,
6472 Chars => Chars (E),
6473 Prefix => New_Occurrence_Of (Enclosing_Instance, Loc),
6474 Selector_Name => New_Occurrence_Of (E, Loc)));
6475
6476 Set_Entity (Gen_Id, E);
6477 Set_Etype (Gen_Id, Etype (E));
6478 Parent_Installed := False; -- Already in scope.
6479 return True;
6480 end if;
6481 end In_Enclosing_Instance;
6482
6483 -- Start of processing for Check_Generic_Child_Unit
6484
6485 begin
6486 -- If the name of the generic is given by a selected component, it may
6487 -- be the name of a generic child unit, and the prefix is the name of an
6488 -- instance of the parent, in which case the child unit must be visible.
6489 -- If this instance is not in scope, it must be placed there and removed
6490 -- after instantiation, because what is being instantiated is not the
6491 -- original child, but the corresponding child present in the instance
6492 -- of the parent.
6493
6494 -- If the child is instantiated within the parent, it can be given by
6495 -- a simple name. In this case the instance is already in scope, but
6496 -- the child generic must be recovered from the generic parent as well.
6497
6498 if Nkind (Gen_Id) = N_Selected_Component then
6499 S := Selector_Name (Gen_Id);
6500 Analyze (Prefix (Gen_Id));
6501 Inst_Par := Entity (Prefix (Gen_Id));
6502
6503 if Ekind (Inst_Par) = E_Package
6504 and then Present (Renamed_Object (Inst_Par))
6505 then
6506 Inst_Par := Renamed_Object (Inst_Par);
6507 end if;
6508
6509 if Ekind (Inst_Par) = E_Package then
6510 if Nkind (Parent (Inst_Par)) = N_Package_Specification then
6511 Gen_Par := Generic_Parent (Parent (Inst_Par));
6512
6513 elsif Nkind (Parent (Inst_Par)) = N_Defining_Program_Unit_Name
6514 and then
6515 Nkind (Parent (Parent (Inst_Par))) = N_Package_Specification
6516 then
6517 Gen_Par := Generic_Parent (Parent (Parent (Inst_Par)));
6518 end if;
6519
6520 elsif Ekind (Inst_Par) = E_Generic_Package
6521 and then Nkind (Parent (Gen_Id)) = N_Formal_Package_Declaration
6522 then
6523 -- A formal package may be a real child package, and not the
6524 -- implicit instance within a parent. In this case the child is
6525 -- not visible and has to be retrieved explicitly as well.
6526
6527 Gen_Par := Inst_Par;
6528 end if;
6529
6530 if Present (Gen_Par) then
6531
6532 -- The prefix denotes an instantiation. The entity itself may be a
6533 -- nested generic, or a child unit.
6534
6535 E := Find_Generic_Child (Gen_Par, S);
6536
6537 if Present (E) then
6538 Change_Selected_Component_To_Expanded_Name (Gen_Id);
6539 Set_Entity (Gen_Id, E);
6540 Set_Etype (Gen_Id, Etype (E));
6541 Set_Entity (S, E);
6542 Set_Etype (S, Etype (E));
6543
6544 -- Indicate that this is a reference to the parent
6545
6546 if In_Extended_Main_Source_Unit (Gen_Id) then
6547 Set_Is_Instantiated (Inst_Par);
6548 end if;
6549
6550 -- A common mistake is to replicate the naming scheme of a
6551 -- hierarchy by instantiating a generic child directly, rather
6552 -- than the implicit child in a parent instance:
6553
6554 -- generic .. package Gpar is ..
6555 -- generic .. package Gpar.Child is ..
6556 -- package Par is new Gpar ();
6557
6558 -- with Gpar.Child;
6559 -- package Par.Child is new Gpar.Child ();
6560 -- rather than Par.Child
6561
6562 -- In this case the instantiation is within Par, which is an
6563 -- instance, but Gpar does not denote Par because we are not IN
6564 -- the instance of Gpar, so this is illegal. The test below
6565 -- recognizes this particular case.
6566
6567 if Is_Child_Unit (E)
6568 and then not Comes_From_Source (Entity (Prefix (Gen_Id)))
6569 and then (not In_Instance
6570 or else Nkind (Parent (Parent (Gen_Id))) =
6571 N_Compilation_Unit)
6572 then
6573 Error_Msg_N
6574 ("prefix of generic child unit must be instance of parent",
6575 Gen_Id);
6576 end if;
6577
6578 if not In_Open_Scopes (Inst_Par)
6579 and then Nkind (Parent (Gen_Id)) not in
6580 N_Generic_Renaming_Declaration
6581 then
6582 Install_Parent (Inst_Par);
6583 Parent_Installed := True;
6584
6585 elsif In_Open_Scopes (Inst_Par) then
6586
6587 -- If the parent is already installed, install the actuals
6588 -- for its formal packages. This is necessary when the child
6589 -- instance is a child of the parent instance: in this case,
6590 -- the parent is placed on the scope stack but the formal
6591 -- packages are not made visible.
6592
6593 Install_Formal_Packages (Inst_Par);
6594 end if;
6595
6596 else
6597 -- If the generic parent does not contain an entity that
6598 -- corresponds to the selector, the instance doesn't either.
6599 -- Analyzing the node will yield the appropriate error message.
6600 -- If the entity is not a child unit, then it is an inner
6601 -- generic in the parent.
6602
6603 Analyze (Gen_Id);
6604 end if;
6605
6606 else
6607 Analyze (Gen_Id);
6608
6609 if Is_Child_Unit (Entity (Gen_Id))
6610 and then
6611 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
6612 and then not In_Open_Scopes (Inst_Par)
6613 then
6614 Install_Parent (Inst_Par);
6615 Parent_Installed := True;
6616
6617 -- The generic unit may be the renaming of the implicit child
6618 -- present in an instance. In that case the parent instance is
6619 -- obtained from the name of the renamed entity.
6620
6621 elsif Ekind (Entity (Gen_Id)) = E_Generic_Package
6622 and then Present (Renamed_Entity (Entity (Gen_Id)))
6623 and then Is_Child_Unit (Renamed_Entity (Entity (Gen_Id)))
6624 then
6625 declare
6626 Renamed_Package : constant Node_Id :=
6627 Name (Parent (Entity (Gen_Id)));
6628 begin
6629 if Nkind (Renamed_Package) = N_Expanded_Name then
6630 Inst_Par := Entity (Prefix (Renamed_Package));
6631 Install_Parent (Inst_Par);
6632 Parent_Installed := True;
6633 end if;
6634 end;
6635 end if;
6636 end if;
6637
6638 elsif Nkind (Gen_Id) = N_Expanded_Name then
6639
6640 -- Entity already present, analyze prefix, whose meaning may be
6641 -- an instance in the current context. If it is an instance of
6642 -- a relative within another, the proper parent may still have
6643 -- to be installed, if they are not of the same generation.
6644
6645 Analyze (Prefix (Gen_Id));
6646
6647 -- In the unlikely case that a local declaration hides the name
6648 -- of the parent package, locate it on the homonym chain. If the
6649 -- context is an instance of the parent, the renaming entity is
6650 -- flagged as such.
6651
6652 Inst_Par := Entity (Prefix (Gen_Id));
6653 while Present (Inst_Par)
6654 and then not Is_Package_Or_Generic_Package (Inst_Par)
6655 loop
6656 Inst_Par := Homonym (Inst_Par);
6657 end loop;
6658
6659 pragma Assert (Present (Inst_Par));
6660 Set_Entity (Prefix (Gen_Id), Inst_Par);
6661
6662 if In_Enclosing_Instance then
6663 null;
6664
6665 elsif Present (Entity (Gen_Id))
6666 and then Is_Child_Unit (Entity (Gen_Id))
6667 and then not In_Open_Scopes (Inst_Par)
6668 then
6669 Install_Parent (Inst_Par);
6670 Parent_Installed := True;
6671 end if;
6672
6673 elsif In_Enclosing_Instance then
6674
6675 -- The child unit is found in some enclosing scope
6676
6677 null;
6678
6679 else
6680 Analyze (Gen_Id);
6681
6682 -- If this is the renaming of the implicit child in a parent
6683 -- instance, recover the parent name and install it.
6684
6685 if Is_Entity_Name (Gen_Id) then
6686 E := Entity (Gen_Id);
6687
6688 if Is_Generic_Unit (E)
6689 and then Nkind (Parent (E)) in N_Generic_Renaming_Declaration
6690 and then Is_Child_Unit (Renamed_Object (E))
6691 and then Is_Generic_Unit (Scope (Renamed_Object (E)))
6692 and then Nkind (Name (Parent (E))) = N_Expanded_Name
6693 then
6694 Rewrite (Gen_Id, New_Copy_Tree (Name (Parent (E))));
6695 Inst_Par := Entity (Prefix (Gen_Id));
6696
6697 if not In_Open_Scopes (Inst_Par) then
6698 Install_Parent (Inst_Par);
6699 Parent_Installed := True;
6700 end if;
6701
6702 -- If it is a child unit of a non-generic parent, it may be
6703 -- use-visible and given by a direct name. Install parent as
6704 -- for other cases.
6705
6706 elsif Is_Generic_Unit (E)
6707 and then Is_Child_Unit (E)
6708 and then
6709 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
6710 and then not Is_Generic_Unit (Scope (E))
6711 then
6712 if not In_Open_Scopes (Scope (E)) then
6713 Install_Parent (Scope (E));
6714 Parent_Installed := True;
6715 end if;
6716 end if;
6717 end if;
6718 end if;
6719 end Check_Generic_Child_Unit;
6720
6721 -----------------------------
6722 -- Check_Hidden_Child_Unit --
6723 -----------------------------
6724
6725 procedure Check_Hidden_Child_Unit
6726 (N : Node_Id;
6727 Gen_Unit : Entity_Id;
6728 Act_Decl_Id : Entity_Id)
6729 is
6730 Gen_Id : constant Node_Id := Name (N);
6731
6732 begin
6733 if Is_Child_Unit (Gen_Unit)
6734 and then Is_Child_Unit (Act_Decl_Id)
6735 and then Nkind (Gen_Id) = N_Expanded_Name
6736 and then Entity (Prefix (Gen_Id)) = Scope (Act_Decl_Id)
6737 and then Chars (Gen_Unit) = Chars (Act_Decl_Id)
6738 then
6739 Error_Msg_Node_2 := Scope (Act_Decl_Id);
6740 Error_Msg_NE
6741 ("generic unit & is implicitly declared in &",
6742 Defining_Unit_Name (N), Gen_Unit);
6743 Error_Msg_N ("\instance must have different name",
6744 Defining_Unit_Name (N));
6745 end if;
6746 end Check_Hidden_Child_Unit;
6747
6748 ------------------------
6749 -- Check_Private_View --
6750 ------------------------
6751
6752 procedure Check_Private_View (N : Node_Id) is
6753 T : constant Entity_Id := Etype (N);
6754 BT : Entity_Id;
6755
6756 begin
6757 -- Exchange views if the type was not private in the generic but is
6758 -- private at the point of instantiation. Do not exchange views if
6759 -- the scope of the type is in scope. This can happen if both generic
6760 -- and instance are sibling units, or if type is defined in a parent.
6761 -- In this case the visibility of the type will be correct for all
6762 -- semantic checks.
6763
6764 if Present (T) then
6765 BT := Base_Type (T);
6766
6767 if Is_Private_Type (T)
6768 and then not Has_Private_View (N)
6769 and then Present (Full_View (T))
6770 and then not In_Open_Scopes (Scope (T))
6771 then
6772 -- In the generic, the full type was visible. Save the private
6773 -- entity, for subsequent exchange.
6774
6775 Switch_View (T);
6776
6777 elsif Has_Private_View (N)
6778 and then not Is_Private_Type (T)
6779 and then not Has_Been_Exchanged (T)
6780 and then Etype (Get_Associated_Node (N)) /= T
6781 then
6782 -- Only the private declaration was visible in the generic. If
6783 -- the type appears in a subtype declaration, the subtype in the
6784 -- instance must have a view compatible with that of its parent,
6785 -- which must be exchanged (see corresponding code in Restore_
6786 -- Private_Views). Otherwise, if the type is defined in a parent
6787 -- unit, leave full visibility within instance, which is safe.
6788
6789 if In_Open_Scopes (Scope (Base_Type (T)))
6790 and then not Is_Private_Type (Base_Type (T))
6791 and then Comes_From_Source (Base_Type (T))
6792 then
6793 null;
6794
6795 elsif Nkind (Parent (N)) = N_Subtype_Declaration
6796 or else not In_Private_Part (Scope (Base_Type (T)))
6797 then
6798 Prepend_Elmt (T, Exchanged_Views);
6799 Exchange_Declarations (Etype (Get_Associated_Node (N)));
6800 end if;
6801
6802 -- For composite types with inconsistent representation exchange
6803 -- component types accordingly.
6804
6805 elsif Is_Access_Type (T)
6806 and then Is_Private_Type (Designated_Type (T))
6807 and then not Has_Private_View (N)
6808 and then Present (Full_View (Designated_Type (T)))
6809 then
6810 Switch_View (Designated_Type (T));
6811
6812 elsif Is_Array_Type (T) then
6813 if Is_Private_Type (Component_Type (T))
6814 and then not Has_Private_View (N)
6815 and then Present (Full_View (Component_Type (T)))
6816 then
6817 Switch_View (Component_Type (T));
6818 end if;
6819
6820 -- The normal exchange mechanism relies on the setting of a
6821 -- flag on the reference in the generic. However, an additional
6822 -- mechanism is needed for types that are not explicitly
6823 -- mentioned in the generic, but may be needed in expanded code
6824 -- in the instance. This includes component types of arrays and
6825 -- designated types of access types. This processing must also
6826 -- include the index types of arrays which we take care of here.
6827
6828 declare
6829 Indx : Node_Id;
6830 Typ : Entity_Id;
6831
6832 begin
6833 Indx := First_Index (T);
6834 while Present (Indx) loop
6835 Typ := Base_Type (Etype (Indx));
6836
6837 if Is_Private_Type (Typ)
6838 and then Present (Full_View (Typ))
6839 then
6840 Switch_View (Typ);
6841 end if;
6842
6843 Next_Index (Indx);
6844 end loop;
6845 end;
6846
6847 elsif Is_Private_Type (T)
6848 and then Present (Full_View (T))
6849 and then Is_Array_Type (Full_View (T))
6850 and then Is_Private_Type (Component_Type (Full_View (T)))
6851 then
6852 Switch_View (T);
6853
6854 -- Finally, a non-private subtype may have a private base type, which
6855 -- must be exchanged for consistency. This can happen when a package
6856 -- body is instantiated, when the scope stack is empty but in fact
6857 -- the subtype and the base type are declared in an enclosing scope.
6858
6859 -- Note that in this case we introduce an inconsistency in the view
6860 -- set, because we switch the base type BT, but there could be some
6861 -- private dependent subtypes of BT which remain unswitched. Such
6862 -- subtypes might need to be switched at a later point (see specific
6863 -- provision for that case in Switch_View).
6864
6865 elsif not Is_Private_Type (T)
6866 and then not Has_Private_View (N)
6867 and then Is_Private_Type (BT)
6868 and then Present (Full_View (BT))
6869 and then not Is_Generic_Type (BT)
6870 and then not In_Open_Scopes (BT)
6871 then
6872 Prepend_Elmt (Full_View (BT), Exchanged_Views);
6873 Exchange_Declarations (BT);
6874 end if;
6875 end if;
6876 end Check_Private_View;
6877
6878 -----------------------------
6879 -- Check_Hidden_Primitives --
6880 -----------------------------
6881
6882 function Check_Hidden_Primitives (Assoc_List : List_Id) return Elist_Id is
6883 Actual : Node_Id;
6884 Gen_T : Entity_Id;
6885 Result : Elist_Id := No_Elist;
6886
6887 begin
6888 if No (Assoc_List) then
6889 return No_Elist;
6890 end if;
6891
6892 -- Traverse the list of associations between formals and actuals
6893 -- searching for renamings of tagged types
6894
6895 Actual := First (Assoc_List);
6896 while Present (Actual) loop
6897 if Nkind (Actual) = N_Subtype_Declaration then
6898 Gen_T := Generic_Parent_Type (Actual);
6899
6900 if Present (Gen_T) and then Is_Tagged_Type (Gen_T) then
6901
6902 -- Traverse the list of primitives of the actual types
6903 -- searching for hidden primitives that are visible in the
6904 -- corresponding generic formal; leave them visible and
6905 -- append them to Result to restore their decoration later.
6906
6907 Install_Hidden_Primitives
6908 (Prims_List => Result,
6909 Gen_T => Gen_T,
6910 Act_T => Entity (Subtype_Indication (Actual)));
6911 end if;
6912 end if;
6913
6914 Next (Actual);
6915 end loop;
6916
6917 return Result;
6918 end Check_Hidden_Primitives;
6919
6920 --------------------------
6921 -- Contains_Instance_Of --
6922 --------------------------
6923
6924 function Contains_Instance_Of
6925 (Inner : Entity_Id;
6926 Outer : Entity_Id;
6927 N : Node_Id) return Boolean
6928 is
6929 Elmt : Elmt_Id;
6930 Scop : Entity_Id;
6931
6932 begin
6933 Scop := Outer;
6934
6935 -- Verify that there are no circular instantiations. We check whether
6936 -- the unit contains an instance of the current scope or some enclosing
6937 -- scope (in case one of the instances appears in a subunit). Longer
6938 -- circularities involving subunits might seem too pathological to
6939 -- consider, but they were not too pathological for the authors of
6940 -- DEC bc30vsq, so we loop over all enclosing scopes, and mark all
6941 -- enclosing generic scopes as containing an instance.
6942
6943 loop
6944 -- Within a generic subprogram body, the scope is not generic, to
6945 -- allow for recursive subprograms. Use the declaration to determine
6946 -- whether this is a generic unit.
6947
6948 if Ekind (Scop) = E_Generic_Package
6949 or else (Is_Subprogram (Scop)
6950 and then Nkind (Unit_Declaration_Node (Scop)) =
6951 N_Generic_Subprogram_Declaration)
6952 then
6953 Elmt := First_Elmt (Inner_Instances (Inner));
6954
6955 while Present (Elmt) loop
6956 if Node (Elmt) = Scop then
6957 Error_Msg_Node_2 := Inner;
6958 Error_Msg_NE
6959 ("circular Instantiation: & instantiated within &!",
6960 N, Scop);
6961 return True;
6962
6963 elsif Node (Elmt) = Inner then
6964 return True;
6965
6966 elsif Contains_Instance_Of (Node (Elmt), Scop, N) then
6967 Error_Msg_Node_2 := Inner;
6968 Error_Msg_NE
6969 ("circular Instantiation: & instantiated within &!",
6970 N, Node (Elmt));
6971 return True;
6972 end if;
6973
6974 Next_Elmt (Elmt);
6975 end loop;
6976
6977 -- Indicate that Inner is being instantiated within Scop
6978
6979 Append_Elmt (Inner, Inner_Instances (Scop));
6980 end if;
6981
6982 if Scop = Standard_Standard then
6983 exit;
6984 else
6985 Scop := Scope (Scop);
6986 end if;
6987 end loop;
6988
6989 return False;
6990 end Contains_Instance_Of;
6991
6992 -----------------------
6993 -- Copy_Generic_Node --
6994 -----------------------
6995
6996 function Copy_Generic_Node
6997 (N : Node_Id;
6998 Parent_Id : Node_Id;
6999 Instantiating : Boolean) return Node_Id
7000 is
7001 Ent : Entity_Id;
7002 New_N : Node_Id;
7003
7004 function Copy_Generic_Descendant (D : Union_Id) return Union_Id;
7005 -- Check the given value of one of the Fields referenced by the current
7006 -- node to determine whether to copy it recursively. The field may hold
7007 -- a Node_Id, a List_Id, or an Elist_Id, or a plain value (Sloc, Uint,
7008 -- Char) in which case it need not be copied.
7009
7010 procedure Copy_Descendants;
7011 -- Common utility for various nodes
7012
7013 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id;
7014 -- Make copy of element list
7015
7016 function Copy_Generic_List
7017 (L : List_Id;
7018 Parent_Id : Node_Id) return List_Id;
7019 -- Apply Copy_Node recursively to the members of a node list
7020
7021 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean;
7022 -- True if an identifier is part of the defining program unit name of
7023 -- a child unit. The entity of such an identifier must be kept (for
7024 -- ASIS use) even though as the name of an enclosing generic it would
7025 -- otherwise not be preserved in the generic tree.
7026
7027 ----------------------
7028 -- Copy_Descendants --
7029 ----------------------
7030
7031 procedure Copy_Descendants is
7032 use Atree.Unchecked_Access;
7033 -- This code section is part of the implementation of an untyped
7034 -- tree traversal, so it needs direct access to node fields.
7035
7036 begin
7037 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
7038 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
7039 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
7040 Set_Field4 (New_N, Copy_Generic_Descendant (Field4 (N)));
7041 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
7042 end Copy_Descendants;
7043
7044 -----------------------------
7045 -- Copy_Generic_Descendant --
7046 -----------------------------
7047
7048 function Copy_Generic_Descendant (D : Union_Id) return Union_Id is
7049 begin
7050 if D = Union_Id (Empty) then
7051 return D;
7052
7053 elsif D in Node_Range then
7054 return Union_Id
7055 (Copy_Generic_Node (Node_Id (D), New_N, Instantiating));
7056
7057 elsif D in List_Range then
7058 return Union_Id (Copy_Generic_List (List_Id (D), New_N));
7059
7060 elsif D in Elist_Range then
7061 return Union_Id (Copy_Generic_Elist (Elist_Id (D)));
7062
7063 -- Nothing else is copyable (e.g. Uint values), return as is
7064
7065 else
7066 return D;
7067 end if;
7068 end Copy_Generic_Descendant;
7069
7070 ------------------------
7071 -- Copy_Generic_Elist --
7072 ------------------------
7073
7074 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id is
7075 M : Elmt_Id;
7076 L : Elist_Id;
7077
7078 begin
7079 if Present (E) then
7080 L := New_Elmt_List;
7081 M := First_Elmt (E);
7082 while Present (M) loop
7083 Append_Elmt
7084 (Copy_Generic_Node (Node (M), Empty, Instantiating), L);
7085 Next_Elmt (M);
7086 end loop;
7087
7088 return L;
7089
7090 else
7091 return No_Elist;
7092 end if;
7093 end Copy_Generic_Elist;
7094
7095 -----------------------
7096 -- Copy_Generic_List --
7097 -----------------------
7098
7099 function Copy_Generic_List
7100 (L : List_Id;
7101 Parent_Id : Node_Id) return List_Id
7102 is
7103 N : Node_Id;
7104 New_L : List_Id;
7105
7106 begin
7107 if Present (L) then
7108 New_L := New_List;
7109 Set_Parent (New_L, Parent_Id);
7110
7111 N := First (L);
7112 while Present (N) loop
7113 Append (Copy_Generic_Node (N, Empty, Instantiating), New_L);
7114 Next (N);
7115 end loop;
7116
7117 return New_L;
7118
7119 else
7120 return No_List;
7121 end if;
7122 end Copy_Generic_List;
7123
7124 ---------------------------
7125 -- In_Defining_Unit_Name --
7126 ---------------------------
7127
7128 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean is
7129 begin
7130 return
7131 Present (Parent (Nam))
7132 and then (Nkind (Parent (Nam)) = N_Defining_Program_Unit_Name
7133 or else
7134 (Nkind (Parent (Nam)) = N_Expanded_Name
7135 and then In_Defining_Unit_Name (Parent (Nam))));
7136 end In_Defining_Unit_Name;
7137
7138 -- Start of processing for Copy_Generic_Node
7139
7140 begin
7141 if N = Empty then
7142 return N;
7143 end if;
7144
7145 New_N := New_Copy (N);
7146
7147 -- Copy aspects if present
7148
7149 if Has_Aspects (N) then
7150 Set_Has_Aspects (New_N, False);
7151 Set_Aspect_Specifications
7152 (New_N, Copy_Generic_List (Aspect_Specifications (N), Parent_Id));
7153 end if;
7154
7155 if Instantiating then
7156 Adjust_Instantiation_Sloc (New_N, S_Adjustment);
7157 end if;
7158
7159 if not Is_List_Member (N) then
7160 Set_Parent (New_N, Parent_Id);
7161 end if;
7162
7163 -- Special casing for identifiers and other entity names and operators
7164
7165 if Nkind_In (New_N, N_Character_Literal,
7166 N_Expanded_Name,
7167 N_Identifier,
7168 N_Operator_Symbol)
7169 or else Nkind (New_N) in N_Op
7170 then
7171 if not Instantiating then
7172
7173 -- Link both nodes in order to assign subsequently the entity of
7174 -- the copy to the original node, in case this is a global
7175 -- reference.
7176
7177 Set_Associated_Node (N, New_N);
7178
7179 -- If we are within an instantiation, this is a nested generic
7180 -- that has already been analyzed at the point of definition.
7181 -- We must preserve references that were global to the enclosing
7182 -- parent at that point. Other occurrences, whether global or
7183 -- local to the current generic, must be resolved anew, so we
7184 -- reset the entity in the generic copy. A global reference has a
7185 -- smaller depth than the parent, or else the same depth in case
7186 -- both are distinct compilation units.
7187
7188 -- A child unit is implicitly declared within the enclosing parent
7189 -- but is in fact global to it, and must be preserved.
7190
7191 -- It is also possible for Current_Instantiated_Parent to be
7192 -- defined, and for this not to be a nested generic, namely if
7193 -- the unit is loaded through Rtsfind. In that case, the entity of
7194 -- New_N is only a link to the associated node, and not a defining
7195 -- occurrence.
7196
7197 -- The entities for parent units in the defining_program_unit of a
7198 -- generic child unit are established when the context of the unit
7199 -- is first analyzed, before the generic copy is made. They are
7200 -- preserved in the copy for use in ASIS queries.
7201
7202 Ent := Entity (New_N);
7203
7204 if No (Current_Instantiated_Parent.Gen_Id) then
7205 if No (Ent)
7206 or else Nkind (Ent) /= N_Defining_Identifier
7207 or else not In_Defining_Unit_Name (N)
7208 then
7209 Set_Associated_Node (New_N, Empty);
7210 end if;
7211
7212 elsif No (Ent)
7213 or else
7214 not Nkind_In (Ent, N_Defining_Identifier,
7215 N_Defining_Character_Literal,
7216 N_Defining_Operator_Symbol)
7217 or else No (Scope (Ent))
7218 or else
7219 (Scope (Ent) = Current_Instantiated_Parent.Gen_Id
7220 and then not Is_Child_Unit (Ent))
7221 or else
7222 (Scope_Depth (Scope (Ent)) >
7223 Scope_Depth (Current_Instantiated_Parent.Gen_Id)
7224 and then
7225 Get_Source_Unit (Ent) =
7226 Get_Source_Unit (Current_Instantiated_Parent.Gen_Id))
7227 then
7228 Set_Associated_Node (New_N, Empty);
7229 end if;
7230
7231 -- Case of instantiating identifier or some other name or operator
7232
7233 else
7234 -- If the associated node is still defined, the entity in it
7235 -- is global, and must be copied to the instance. If this copy
7236 -- is being made for a body to inline, it is applied to an
7237 -- instantiated tree, and the entity is already present and
7238 -- must be also preserved.
7239
7240 declare
7241 Assoc : constant Node_Id := Get_Associated_Node (N);
7242
7243 begin
7244 if Present (Assoc) then
7245 if Nkind (Assoc) = Nkind (N) then
7246 Set_Entity (New_N, Entity (Assoc));
7247 Check_Private_View (N);
7248
7249 -- The name in the call may be a selected component if the
7250 -- call has not been analyzed yet, as may be the case for
7251 -- pre/post conditions in a generic unit.
7252
7253 elsif Nkind (Assoc) = N_Function_Call
7254 and then Is_Entity_Name (Name (Assoc))
7255 then
7256 Set_Entity (New_N, Entity (Name (Assoc)));
7257
7258 elsif Nkind_In (Assoc, N_Defining_Identifier,
7259 N_Defining_Character_Literal,
7260 N_Defining_Operator_Symbol)
7261 and then Expander_Active
7262 then
7263 -- Inlining case: we are copying a tree that contains
7264 -- global entities, which are preserved in the copy to be
7265 -- used for subsequent inlining.
7266
7267 null;
7268
7269 else
7270 Set_Entity (New_N, Empty);
7271 end if;
7272 end if;
7273 end;
7274 end if;
7275
7276 -- For expanded name, we must copy the Prefix and Selector_Name
7277
7278 if Nkind (N) = N_Expanded_Name then
7279 Set_Prefix
7280 (New_N, Copy_Generic_Node (Prefix (N), New_N, Instantiating));
7281
7282 Set_Selector_Name (New_N,
7283 Copy_Generic_Node (Selector_Name (N), New_N, Instantiating));
7284
7285 -- For operators, we must copy the right operand
7286
7287 elsif Nkind (N) in N_Op then
7288 Set_Right_Opnd (New_N,
7289 Copy_Generic_Node (Right_Opnd (N), New_N, Instantiating));
7290
7291 -- And for binary operators, the left operand as well
7292
7293 if Nkind (N) in N_Binary_Op then
7294 Set_Left_Opnd (New_N,
7295 Copy_Generic_Node (Left_Opnd (N), New_N, Instantiating));
7296 end if;
7297 end if;
7298
7299 -- Establish a link between an entity from the generic template and the
7300 -- corresponding entity in the generic copy to be analyzed.
7301
7302 elsif Nkind (N) in N_Entity then
7303 if not Instantiating then
7304 Set_Associated_Entity (N, New_N);
7305 end if;
7306
7307 -- Clear any existing link the copy may inherit from the replicated
7308 -- generic template entity.
7309
7310 Set_Associated_Entity (New_N, Empty);
7311
7312 -- Special casing for stubs
7313
7314 elsif Nkind (N) in N_Body_Stub then
7315
7316 -- In any case, we must copy the specification or defining
7317 -- identifier as appropriate.
7318
7319 if Nkind (N) = N_Subprogram_Body_Stub then
7320 Set_Specification (New_N,
7321 Copy_Generic_Node (Specification (N), New_N, Instantiating));
7322
7323 else
7324 Set_Defining_Identifier (New_N,
7325 Copy_Generic_Node
7326 (Defining_Identifier (N), New_N, Instantiating));
7327 end if;
7328
7329 -- If we are not instantiating, then this is where we load and
7330 -- analyze subunits, i.e. at the point where the stub occurs. A
7331 -- more permissive system might defer this analysis to the point
7332 -- of instantiation, but this seems too complicated for now.
7333
7334 if not Instantiating then
7335 declare
7336 Subunit_Name : constant Unit_Name_Type := Get_Unit_Name (N);
7337 Subunit : Node_Id;
7338 Unum : Unit_Number_Type;
7339 New_Body : Node_Id;
7340
7341 begin
7342 -- Make sure that, if it is a subunit of the main unit that is
7343 -- preprocessed and if -gnateG is specified, the preprocessed
7344 -- file will be written.
7345
7346 Lib.Analysing_Subunit_Of_Main :=
7347 Lib.In_Extended_Main_Source_Unit (N);
7348 Unum :=
7349 Load_Unit
7350 (Load_Name => Subunit_Name,
7351 Required => False,
7352 Subunit => True,
7353 Error_Node => N);
7354 Lib.Analysing_Subunit_Of_Main := False;
7355
7356 -- If the proper body is not found, a warning message will be
7357 -- emitted when analyzing the stub, or later at the point of
7358 -- instantiation. Here we just leave the stub as is.
7359
7360 if Unum = No_Unit then
7361 Subunits_Missing := True;
7362 goto Subunit_Not_Found;
7363 end if;
7364
7365 Subunit := Cunit (Unum);
7366
7367 if Nkind (Unit (Subunit)) /= N_Subunit then
7368 Error_Msg_N
7369 ("found child unit instead of expected SEPARATE subunit",
7370 Subunit);
7371 Error_Msg_Sloc := Sloc (N);
7372 Error_Msg_N ("\to complete stub #", Subunit);
7373 goto Subunit_Not_Found;
7374 end if;
7375
7376 -- We must create a generic copy of the subunit, in order to
7377 -- perform semantic analysis on it, and we must replace the
7378 -- stub in the original generic unit with the subunit, in order
7379 -- to preserve non-local references within.
7380
7381 -- Only the proper body needs to be copied. Library_Unit and
7382 -- context clause are simply inherited by the generic copy.
7383 -- Note that the copy (which may be recursive if there are
7384 -- nested subunits) must be done first, before attaching it to
7385 -- the enclosing generic.
7386
7387 New_Body :=
7388 Copy_Generic_Node
7389 (Proper_Body (Unit (Subunit)),
7390 Empty, Instantiating => False);
7391
7392 -- Now place the original proper body in the original generic
7393 -- unit. This is a body, not a compilation unit.
7394
7395 Rewrite (N, Proper_Body (Unit (Subunit)));
7396 Set_Is_Compilation_Unit (Defining_Entity (N), False);
7397 Set_Was_Originally_Stub (N);
7398
7399 -- Finally replace the body of the subunit with its copy, and
7400 -- make this new subunit into the library unit of the generic
7401 -- copy, which does not have stubs any longer.
7402
7403 Set_Proper_Body (Unit (Subunit), New_Body);
7404 Set_Library_Unit (New_N, Subunit);
7405 Inherit_Context (Unit (Subunit), N);
7406 end;
7407
7408 -- If we are instantiating, this must be an error case, since
7409 -- otherwise we would have replaced the stub node by the proper body
7410 -- that corresponds. So just ignore it in the copy (i.e. we have
7411 -- copied it, and that is good enough).
7412
7413 else
7414 null;
7415 end if;
7416
7417 <<Subunit_Not_Found>> null;
7418
7419 -- If the node is a compilation unit, it is the subunit of a stub, which
7420 -- has been loaded already (see code below). In this case, the library
7421 -- unit field of N points to the parent unit (which is a compilation
7422 -- unit) and need not (and cannot) be copied.
7423
7424 -- When the proper body of the stub is analyzed, the library_unit link
7425 -- is used to establish the proper context (see sem_ch10).
7426
7427 -- The other fields of a compilation unit are copied as usual
7428
7429 elsif Nkind (N) = N_Compilation_Unit then
7430
7431 -- This code can only be executed when not instantiating, because in
7432 -- the copy made for an instantiation, the compilation unit node has
7433 -- disappeared at the point that a stub is replaced by its proper
7434 -- body.
7435
7436 pragma Assert (not Instantiating);
7437
7438 Set_Context_Items (New_N,
7439 Copy_Generic_List (Context_Items (N), New_N));
7440
7441 Set_Unit (New_N,
7442 Copy_Generic_Node (Unit (N), New_N, False));
7443
7444 Set_First_Inlined_Subprogram (New_N,
7445 Copy_Generic_Node
7446 (First_Inlined_Subprogram (N), New_N, False));
7447
7448 Set_Aux_Decls_Node (New_N,
7449 Copy_Generic_Node (Aux_Decls_Node (N), New_N, False));
7450
7451 -- For an assignment node, the assignment is known to be semantically
7452 -- legal if we are instantiating the template. This avoids incorrect
7453 -- diagnostics in generated code.
7454
7455 elsif Nkind (N) = N_Assignment_Statement then
7456
7457 -- Copy name and expression fields in usual manner
7458
7459 Set_Name (New_N,
7460 Copy_Generic_Node (Name (N), New_N, Instantiating));
7461
7462 Set_Expression (New_N,
7463 Copy_Generic_Node (Expression (N), New_N, Instantiating));
7464
7465 if Instantiating then
7466 Set_Assignment_OK (Name (New_N), True);
7467 end if;
7468
7469 elsif Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
7470 if not Instantiating then
7471 Set_Associated_Node (N, New_N);
7472
7473 else
7474 if Present (Get_Associated_Node (N))
7475 and then Nkind (Get_Associated_Node (N)) = Nkind (N)
7476 then
7477 -- In the generic the aggregate has some composite type. If at
7478 -- the point of instantiation the type has a private view,
7479 -- install the full view (and that of its ancestors, if any).
7480
7481 declare
7482 T : Entity_Id := (Etype (Get_Associated_Node (New_N)));
7483 Rt : Entity_Id;
7484
7485 begin
7486 if Present (T) and then Is_Private_Type (T) then
7487 Switch_View (T);
7488 end if;
7489
7490 if Present (T)
7491 and then Is_Tagged_Type (T)
7492 and then Is_Derived_Type (T)
7493 then
7494 Rt := Root_Type (T);
7495
7496 loop
7497 T := Etype (T);
7498
7499 if Is_Private_Type (T) then
7500 Switch_View (T);
7501 end if;
7502
7503 exit when T = Rt;
7504 end loop;
7505 end if;
7506 end;
7507 end if;
7508 end if;
7509
7510 -- Do not copy the associated node, which points to the generic copy
7511 -- of the aggregate.
7512
7513 declare
7514 use Atree.Unchecked_Access;
7515 -- This code section is part of the implementation of an untyped
7516 -- tree traversal, so it needs direct access to node fields.
7517
7518 begin
7519 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
7520 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
7521 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
7522 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
7523 end;
7524
7525 -- Allocators do not have an identifier denoting the access type, so we
7526 -- must locate it through the expression to check whether the views are
7527 -- consistent.
7528
7529 elsif Nkind (N) = N_Allocator
7530 and then Nkind (Expression (N)) = N_Qualified_Expression
7531 and then Is_Entity_Name (Subtype_Mark (Expression (N)))
7532 and then Instantiating
7533 then
7534 declare
7535 T : constant Node_Id :=
7536 Get_Associated_Node (Subtype_Mark (Expression (N)));
7537 Acc_T : Entity_Id;
7538
7539 begin
7540 if Present (T) then
7541
7542 -- Retrieve the allocator node in the generic copy
7543
7544 Acc_T := Etype (Parent (Parent (T)));
7545
7546 if Present (Acc_T) and then Is_Private_Type (Acc_T) then
7547 Switch_View (Acc_T);
7548 end if;
7549 end if;
7550
7551 Copy_Descendants;
7552 end;
7553
7554 -- For a proper body, we must catch the case of a proper body that
7555 -- replaces a stub. This represents the point at which a separate
7556 -- compilation unit, and hence template file, may be referenced, so we
7557 -- must make a new source instantiation entry for the template of the
7558 -- subunit, and ensure that all nodes in the subunit are adjusted using
7559 -- this new source instantiation entry.
7560
7561 elsif Nkind (N) in N_Proper_Body then
7562 declare
7563 Save_Adjustment : constant Sloc_Adjustment := S_Adjustment;
7564
7565 begin
7566 if Instantiating and then Was_Originally_Stub (N) then
7567 Create_Instantiation_Source
7568 (Instantiation_Node,
7569 Defining_Entity (N),
7570 False,
7571 S_Adjustment);
7572 end if;
7573
7574 -- Now copy the fields of the proper body, using the new
7575 -- adjustment factor if one was needed as per test above.
7576
7577 Copy_Descendants;
7578
7579 -- Restore the original adjustment factor in case changed
7580
7581 S_Adjustment := Save_Adjustment;
7582 end;
7583
7584 elsif Nkind (N) = N_Pragma and then Instantiating then
7585
7586 -- Do not copy Comment or Ident pragmas their content is relevant to
7587 -- the generic unit, not to the instantiating unit.
7588
7589 if Nam_In (Pragma_Name (N), Name_Comment, Name_Ident) then
7590 New_N := Make_Null_Statement (Sloc (N));
7591
7592 -- Do not copy pragmas generated from aspects because the pragmas do
7593 -- not carry any semantic information, plus they will be regenerated
7594 -- in the instance.
7595
7596 elsif From_Aspect_Specification (N) then
7597 New_N := Make_Null_Statement (Sloc (N));
7598
7599 else
7600 Copy_Descendants;
7601 end if;
7602
7603 elsif Nkind_In (N, N_Integer_Literal, N_Real_Literal) then
7604
7605 -- No descendant fields need traversing
7606
7607 null;
7608
7609 elsif Nkind (N) = N_String_Literal
7610 and then Present (Etype (N))
7611 and then Instantiating
7612 then
7613 -- If the string is declared in an outer scope, the string_literal
7614 -- subtype created for it may have the wrong scope. Force reanalysis
7615 -- of the constant to generate a new itype in the proper context.
7616
7617 Set_Etype (New_N, Empty);
7618 Set_Analyzed (New_N, False);
7619
7620 -- For the remaining nodes, copy their descendants recursively
7621
7622 else
7623 Copy_Descendants;
7624
7625 if Instantiating and then Nkind (N) = N_Subprogram_Body then
7626 Set_Generic_Parent (Specification (New_N), N);
7627
7628 -- Should preserve Corresponding_Spec??? (12.3(14))
7629 end if;
7630 end if;
7631
7632 return New_N;
7633 end Copy_Generic_Node;
7634
7635 ----------------------------
7636 -- Denotes_Formal_Package --
7637 ----------------------------
7638
7639 function Denotes_Formal_Package
7640 (Pack : Entity_Id;
7641 On_Exit : Boolean := False;
7642 Instance : Entity_Id := Empty) return Boolean
7643 is
7644 Par : Entity_Id;
7645 Scop : constant Entity_Id := Scope (Pack);
7646 E : Entity_Id;
7647
7648 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean;
7649 -- The package in question may be an actual for a previous formal
7650 -- package P of the current instance, so examine its actuals as well.
7651 -- This must be recursive over other formal packages.
7652
7653 ----------------------------------
7654 -- Is_Actual_Of_Previous_Formal --
7655 ----------------------------------
7656
7657 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean is
7658 E1 : Entity_Id;
7659
7660 begin
7661 E1 := First_Entity (P);
7662 while Present (E1) and then E1 /= Instance loop
7663 if Ekind (E1) = E_Package
7664 and then Nkind (Parent (E1)) = N_Package_Renaming_Declaration
7665 then
7666 if Renamed_Object (E1) = Pack then
7667 return True;
7668
7669 elsif E1 = P or else Renamed_Object (E1) = P then
7670 return False;
7671
7672 elsif Is_Actual_Of_Previous_Formal (E1) then
7673 return True;
7674 end if;
7675 end if;
7676
7677 Next_Entity (E1);
7678 end loop;
7679
7680 return False;
7681 end Is_Actual_Of_Previous_Formal;
7682
7683 -- Start of processing for Denotes_Formal_Package
7684
7685 begin
7686 if On_Exit then
7687 Par :=
7688 Instance_Envs.Table
7689 (Instance_Envs.Last).Instantiated_Parent.Act_Id;
7690 else
7691 Par := Current_Instantiated_Parent.Act_Id;
7692 end if;
7693
7694 if Ekind (Scop) = E_Generic_Package
7695 or else Nkind (Unit_Declaration_Node (Scop)) =
7696 N_Generic_Subprogram_Declaration
7697 then
7698 return True;
7699
7700 elsif Nkind (Original_Node (Unit_Declaration_Node (Pack))) =
7701 N_Formal_Package_Declaration
7702 then
7703 return True;
7704
7705 elsif No (Par) then
7706 return False;
7707
7708 else
7709 -- Check whether this package is associated with a formal package of
7710 -- the enclosing instantiation. Iterate over the list of renamings.
7711
7712 E := First_Entity (Par);
7713 while Present (E) loop
7714 if Ekind (E) /= E_Package
7715 or else Nkind (Parent (E)) /= N_Package_Renaming_Declaration
7716 then
7717 null;
7718
7719 elsif Renamed_Object (E) = Par then
7720 return False;
7721
7722 elsif Renamed_Object (E) = Pack then
7723 return True;
7724
7725 elsif Is_Actual_Of_Previous_Formal (E) then
7726 return True;
7727
7728 end if;
7729
7730 Next_Entity (E);
7731 end loop;
7732
7733 return False;
7734 end if;
7735 end Denotes_Formal_Package;
7736
7737 -----------------
7738 -- End_Generic --
7739 -----------------
7740
7741 procedure End_Generic is
7742 begin
7743 -- ??? More things could be factored out in this routine. Should
7744 -- probably be done at a later stage.
7745
7746 Inside_A_Generic := Generic_Flags.Table (Generic_Flags.Last);
7747 Generic_Flags.Decrement_Last;
7748
7749 Expander_Mode_Restore;
7750 end End_Generic;
7751
7752 -------------
7753 -- Earlier --
7754 -------------
7755
7756 function Earlier (N1, N2 : Node_Id) return Boolean is
7757 procedure Find_Depth (P : in out Node_Id; D : in out Integer);
7758 -- Find distance from given node to enclosing compilation unit
7759
7760 ----------------
7761 -- Find_Depth --
7762 ----------------
7763
7764 procedure Find_Depth (P : in out Node_Id; D : in out Integer) is
7765 begin
7766 while Present (P)
7767 and then Nkind (P) /= N_Compilation_Unit
7768 loop
7769 P := True_Parent (P);
7770 D := D + 1;
7771 end loop;
7772 end Find_Depth;
7773
7774 -- Local declarations
7775
7776 D1 : Integer := 0;
7777 D2 : Integer := 0;
7778 P1 : Node_Id := N1;
7779 P2 : Node_Id := N2;
7780 T1 : Source_Ptr;
7781 T2 : Source_Ptr;
7782
7783 -- Start of processing for Earlier
7784
7785 begin
7786 Find_Depth (P1, D1);
7787 Find_Depth (P2, D2);
7788
7789 if P1 /= P2 then
7790 return False;
7791 else
7792 P1 := N1;
7793 P2 := N2;
7794 end if;
7795
7796 while D1 > D2 loop
7797 P1 := True_Parent (P1);
7798 D1 := D1 - 1;
7799 end loop;
7800
7801 while D2 > D1 loop
7802 P2 := True_Parent (P2);
7803 D2 := D2 - 1;
7804 end loop;
7805
7806 -- At this point P1 and P2 are at the same distance from the root.
7807 -- We examine their parents until we find a common declarative list.
7808 -- If we reach the root, N1 and N2 do not descend from the same
7809 -- declarative list (e.g. one is nested in the declarative part and
7810 -- the other is in a block in the statement part) and the earlier
7811 -- one is already frozen.
7812
7813 while not Is_List_Member (P1)
7814 or else not Is_List_Member (P2)
7815 or else List_Containing (P1) /= List_Containing (P2)
7816 loop
7817 P1 := True_Parent (P1);
7818 P2 := True_Parent (P2);
7819
7820 if Nkind (Parent (P1)) = N_Subunit then
7821 P1 := Corresponding_Stub (Parent (P1));
7822 end if;
7823
7824 if Nkind (Parent (P2)) = N_Subunit then
7825 P2 := Corresponding_Stub (Parent (P2));
7826 end if;
7827
7828 if P1 = P2 then
7829 return False;
7830 end if;
7831 end loop;
7832
7833 -- Expanded code usually shares the source location of the original
7834 -- construct it was generated for. This however may not necessarely
7835 -- reflect the true location of the code within the tree.
7836
7837 -- Before comparing the slocs of the two nodes, make sure that we are
7838 -- working with correct source locations. Assume that P1 is to the left
7839 -- of P2. If either one does not come from source, traverse the common
7840 -- list heading towards the other node and locate the first source
7841 -- statement.
7842
7843 -- P1 P2
7844 -- ----+===+===+--------------+===+===+----
7845 -- expanded code expanded code
7846
7847 if not Comes_From_Source (P1) then
7848 while Present (P1) loop
7849
7850 -- Neither P2 nor a source statement were located during the
7851 -- search. If we reach the end of the list, then P1 does not
7852 -- occur earlier than P2.
7853
7854 -- ---->
7855 -- start --- P2 ----- P1 --- end
7856
7857 if No (Next (P1)) then
7858 return False;
7859
7860 -- We encounter P2 while going to the right of the list. This
7861 -- means that P1 does indeed appear earlier.
7862
7863 -- ---->
7864 -- start --- P1 ===== P2 --- end
7865 -- expanded code in between
7866
7867 elsif P1 = P2 then
7868 return True;
7869
7870 -- No need to look any further since we have located a source
7871 -- statement.
7872
7873 elsif Comes_From_Source (P1) then
7874 exit;
7875 end if;
7876
7877 -- Keep going right
7878
7879 Next (P1);
7880 end loop;
7881 end if;
7882
7883 if not Comes_From_Source (P2) then
7884 while Present (P2) loop
7885
7886 -- Neither P1 nor a source statement were located during the
7887 -- search. If we reach the start of the list, then P1 does not
7888 -- occur earlier than P2.
7889
7890 -- <----
7891 -- start --- P2 --- P1 --- end
7892
7893 if No (Prev (P2)) then
7894 return False;
7895
7896 -- We encounter P1 while going to the left of the list. This
7897 -- means that P1 does indeed appear earlier.
7898
7899 -- <----
7900 -- start --- P1 ===== P2 --- end
7901 -- expanded code in between
7902
7903 elsif P2 = P1 then
7904 return True;
7905
7906 -- No need to look any further since we have located a source
7907 -- statement.
7908
7909 elsif Comes_From_Source (P2) then
7910 exit;
7911 end if;
7912
7913 -- Keep going left
7914
7915 Prev (P2);
7916 end loop;
7917 end if;
7918
7919 -- At this point either both nodes came from source or we approximated
7920 -- their source locations through neighbouring source statements.
7921
7922 T1 := Top_Level_Location (Sloc (P1));
7923 T2 := Top_Level_Location (Sloc (P2));
7924
7925 -- When two nodes come from the same instance, they have identical top
7926 -- level locations. To determine proper relation within the tree, check
7927 -- their locations within the template.
7928
7929 if T1 = T2 then
7930 return Sloc (P1) < Sloc (P2);
7931
7932 -- The two nodes either come from unrelated instances or do not come
7933 -- from instantiated code at all.
7934
7935 else
7936 return T1 < T2;
7937 end if;
7938 end Earlier;
7939
7940 ----------------------
7941 -- Find_Actual_Type --
7942 ----------------------
7943
7944 function Find_Actual_Type
7945 (Typ : Entity_Id;
7946 Gen_Type : Entity_Id) return Entity_Id
7947 is
7948 Gen_Scope : constant Entity_Id := Scope (Gen_Type);
7949 T : Entity_Id;
7950
7951 begin
7952 -- Special processing only applies to child units
7953
7954 if not Is_Child_Unit (Gen_Scope) then
7955 return Get_Instance_Of (Typ);
7956
7957 -- If designated or component type is itself a formal of the child unit,
7958 -- its instance is available.
7959
7960 elsif Scope (Typ) = Gen_Scope then
7961 return Get_Instance_Of (Typ);
7962
7963 -- If the array or access type is not declared in the parent unit,
7964 -- no special processing needed.
7965
7966 elsif not Is_Generic_Type (Typ)
7967 and then Scope (Gen_Scope) /= Scope (Typ)
7968 then
7969 return Get_Instance_Of (Typ);
7970
7971 -- Otherwise, retrieve designated or component type by visibility
7972
7973 else
7974 T := Current_Entity (Typ);
7975 while Present (T) loop
7976 if In_Open_Scopes (Scope (T)) then
7977 return T;
7978 elsif Is_Generic_Actual_Type (T) then
7979 return T;
7980 end if;
7981
7982 T := Homonym (T);
7983 end loop;
7984
7985 return Typ;
7986 end if;
7987 end Find_Actual_Type;
7988
7989 ----------------------------
7990 -- Freeze_Subprogram_Body --
7991 ----------------------------
7992
7993 procedure Freeze_Subprogram_Body
7994 (Inst_Node : Node_Id;
7995 Gen_Body : Node_Id;
7996 Pack_Id : Entity_Id)
7997 is
7998 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
7999 Par : constant Entity_Id := Scope (Gen_Unit);
8000 E_G_Id : Entity_Id;
8001 Enc_G : Entity_Id;
8002 Enc_I : Node_Id;
8003 F_Node : Node_Id;
8004
8005 function Enclosing_Package_Body (N : Node_Id) return Node_Id;
8006 -- Find innermost package body that encloses the given node, and which
8007 -- is not a compilation unit. Freeze nodes for the instance, or for its
8008 -- enclosing body, may be inserted after the enclosing_body of the
8009 -- generic unit. Used to determine proper placement of freeze node for
8010 -- both package and subprogram instances.
8011
8012 function Package_Freeze_Node (B : Node_Id) return Node_Id;
8013 -- Find entity for given package body, and locate or create a freeze
8014 -- node for it.
8015
8016 ----------------------------
8017 -- Enclosing_Package_Body --
8018 ----------------------------
8019
8020 function Enclosing_Package_Body (N : Node_Id) return Node_Id is
8021 P : Node_Id;
8022
8023 begin
8024 P := Parent (N);
8025 while Present (P)
8026 and then Nkind (Parent (P)) /= N_Compilation_Unit
8027 loop
8028 if Nkind (P) = N_Package_Body then
8029 if Nkind (Parent (P)) = N_Subunit then
8030 return Corresponding_Stub (Parent (P));
8031 else
8032 return P;
8033 end if;
8034 end if;
8035
8036 P := True_Parent (P);
8037 end loop;
8038
8039 return Empty;
8040 end Enclosing_Package_Body;
8041
8042 -------------------------
8043 -- Package_Freeze_Node --
8044 -------------------------
8045
8046 function Package_Freeze_Node (B : Node_Id) return Node_Id is
8047 Id : Entity_Id;
8048
8049 begin
8050 if Nkind (B) = N_Package_Body then
8051 Id := Corresponding_Spec (B);
8052 else pragma Assert (Nkind (B) = N_Package_Body_Stub);
8053 Id := Corresponding_Spec (Proper_Body (Unit (Library_Unit (B))));
8054 end if;
8055
8056 Ensure_Freeze_Node (Id);
8057 return Freeze_Node (Id);
8058 end Package_Freeze_Node;
8059
8060 -- Start of processing of Freeze_Subprogram_Body
8061
8062 begin
8063 -- If the instance and the generic body appear within the same unit, and
8064 -- the instance precedes the generic, the freeze node for the instance
8065 -- must appear after that of the generic. If the generic is nested
8066 -- within another instance I2, then current instance must be frozen
8067 -- after I2. In both cases, the freeze nodes are those of enclosing
8068 -- packages. Otherwise, the freeze node is placed at the end of the
8069 -- current declarative part.
8070
8071 Enc_G := Enclosing_Package_Body (Gen_Body);
8072 Enc_I := Enclosing_Package_Body (Inst_Node);
8073 Ensure_Freeze_Node (Pack_Id);
8074 F_Node := Freeze_Node (Pack_Id);
8075
8076 if Is_Generic_Instance (Par)
8077 and then Present (Freeze_Node (Par))
8078 and then In_Same_Declarative_Part (Freeze_Node (Par), Inst_Node)
8079 then
8080 -- The parent was a premature instantiation. Insert freeze node at
8081 -- the end the current declarative part.
8082
8083 if ABE_Is_Certain (Get_Package_Instantiation_Node (Par)) then
8084 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8085
8086 -- Handle the following case:
8087 --
8088 -- package Parent_Inst is new ...
8089 -- Parent_Inst []
8090 --
8091 -- procedure P ... -- this body freezes Parent_Inst
8092 --
8093 -- package Inst is new ...
8094 --
8095 -- In this particular scenario, the freeze node for Inst must be
8096 -- inserted in the same manner as that of Parent_Inst - before the
8097 -- next source body or at the end of the declarative list (body not
8098 -- available). If body P did not exist and Parent_Inst was frozen
8099 -- after Inst, either by a body following Inst or at the end of the
8100 -- declarative region, the freeze node for Inst must be inserted
8101 -- after that of Parent_Inst. This relation is established by
8102 -- comparing the Slocs of Parent_Inst freeze node and Inst.
8103
8104 elsif List_Containing (Get_Package_Instantiation_Node (Par)) =
8105 List_Containing (Inst_Node)
8106 and then Sloc (Freeze_Node (Par)) < Sloc (Inst_Node)
8107 then
8108 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8109
8110 else
8111 Insert_After (Freeze_Node (Par), F_Node);
8112 end if;
8113
8114 -- The body enclosing the instance should be frozen after the body that
8115 -- includes the generic, because the body of the instance may make
8116 -- references to entities therein. If the two are not in the same
8117 -- declarative part, or if the one enclosing the instance is frozen
8118 -- already, freeze the instance at the end of the current declarative
8119 -- part.
8120
8121 elsif Is_Generic_Instance (Par)
8122 and then Present (Freeze_Node (Par))
8123 and then Present (Enc_I)
8124 then
8125 if In_Same_Declarative_Part (Freeze_Node (Par), Enc_I)
8126 or else
8127 (Nkind (Enc_I) = N_Package_Body
8128 and then
8129 In_Same_Declarative_Part (Freeze_Node (Par), Parent (Enc_I)))
8130 then
8131 -- The enclosing package may contain several instances. Rather
8132 -- than computing the earliest point at which to insert its freeze
8133 -- node, we place it at the end of the declarative part of the
8134 -- parent of the generic.
8135
8136 Insert_Freeze_Node_For_Instance
8137 (Freeze_Node (Par), Package_Freeze_Node (Enc_I));
8138 end if;
8139
8140 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8141
8142 elsif Present (Enc_G)
8143 and then Present (Enc_I)
8144 and then Enc_G /= Enc_I
8145 and then Earlier (Inst_Node, Gen_Body)
8146 then
8147 if Nkind (Enc_G) = N_Package_Body then
8148 E_G_Id :=
8149 Corresponding_Spec (Enc_G);
8150 else pragma Assert (Nkind (Enc_G) = N_Package_Body_Stub);
8151 E_G_Id :=
8152 Corresponding_Spec (Proper_Body (Unit (Library_Unit (Enc_G))));
8153 end if;
8154
8155 -- Freeze package that encloses instance, and place node after the
8156 -- package that encloses generic. If enclosing package is already
8157 -- frozen we have to assume it is at the proper place. This may be a
8158 -- potential ABE that requires dynamic checking. Do not add a freeze
8159 -- node if the package that encloses the generic is inside the body
8160 -- that encloses the instance, because the freeze node would be in
8161 -- the wrong scope. Additional contortions needed if the bodies are
8162 -- within a subunit.
8163
8164 declare
8165 Enclosing_Body : Node_Id;
8166
8167 begin
8168 if Nkind (Enc_I) = N_Package_Body_Stub then
8169 Enclosing_Body := Proper_Body (Unit (Library_Unit (Enc_I)));
8170 else
8171 Enclosing_Body := Enc_I;
8172 end if;
8173
8174 if Parent (List_Containing (Enc_G)) /= Enclosing_Body then
8175 Insert_Freeze_Node_For_Instance
8176 (Enc_G, Package_Freeze_Node (Enc_I));
8177 end if;
8178 end;
8179
8180 -- Freeze enclosing subunit before instance
8181
8182 Ensure_Freeze_Node (E_G_Id);
8183
8184 if not Is_List_Member (Freeze_Node (E_G_Id)) then
8185 Insert_After (Enc_G, Freeze_Node (E_G_Id));
8186 end if;
8187
8188 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8189
8190 else
8191 -- If none of the above, insert freeze node at the end of the current
8192 -- declarative part.
8193
8194 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8195 end if;
8196 end Freeze_Subprogram_Body;
8197
8198 ----------------
8199 -- Get_Gen_Id --
8200 ----------------
8201
8202 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id is
8203 begin
8204 return Generic_Renamings.Table (E).Gen_Id;
8205 end Get_Gen_Id;
8206
8207 ---------------------
8208 -- Get_Instance_Of --
8209 ---------------------
8210
8211 function Get_Instance_Of (A : Entity_Id) return Entity_Id is
8212 Res : constant Assoc_Ptr := Generic_Renamings_HTable.Get (A);
8213
8214 begin
8215 if Res /= Assoc_Null then
8216 return Generic_Renamings.Table (Res).Act_Id;
8217
8218 else
8219 -- On exit, entity is not instantiated: not a generic parameter, or
8220 -- else parameter of an inner generic unit.
8221
8222 return A;
8223 end if;
8224 end Get_Instance_Of;
8225
8226 ------------------------------------
8227 -- Get_Package_Instantiation_Node --
8228 ------------------------------------
8229
8230 function Get_Package_Instantiation_Node (A : Entity_Id) return Node_Id is
8231 Decl : Node_Id := Unit_Declaration_Node (A);
8232 Inst : Node_Id;
8233
8234 begin
8235 -- If the Package_Instantiation attribute has been set on the package
8236 -- entity, then use it directly when it (or its Original_Node) refers
8237 -- to an N_Package_Instantiation node. In principle it should be
8238 -- possible to have this field set in all cases, which should be
8239 -- investigated, and would allow this function to be significantly
8240 -- simplified. ???
8241
8242 Inst := Package_Instantiation (A);
8243
8244 if Present (Inst) then
8245 if Nkind (Inst) = N_Package_Instantiation then
8246 return Inst;
8247
8248 elsif Nkind (Original_Node (Inst)) = N_Package_Instantiation then
8249 return Original_Node (Inst);
8250 end if;
8251 end if;
8252
8253 -- If the instantiation is a compilation unit that does not need body
8254 -- then the instantiation node has been rewritten as a package
8255 -- declaration for the instance, and we return the original node.
8256
8257 -- If it is a compilation unit and the instance node has not been
8258 -- rewritten, then it is still the unit of the compilation. Finally, if
8259 -- a body is present, this is a parent of the main unit whose body has
8260 -- been compiled for inlining purposes, and the instantiation node has
8261 -- been rewritten with the instance body.
8262
8263 -- Otherwise the instantiation node appears after the declaration. If
8264 -- the entity is a formal package, the declaration may have been
8265 -- rewritten as a generic declaration (in the case of a formal with box)
8266 -- or left as a formal package declaration if it has actuals, and is
8267 -- found with a forward search.
8268
8269 if Nkind (Parent (Decl)) = N_Compilation_Unit then
8270 if Nkind (Decl) = N_Package_Declaration
8271 and then Present (Corresponding_Body (Decl))
8272 then
8273 Decl := Unit_Declaration_Node (Corresponding_Body (Decl));
8274 end if;
8275
8276 if Nkind (Original_Node (Decl)) = N_Package_Instantiation then
8277 return Original_Node (Decl);
8278 else
8279 return Unit (Parent (Decl));
8280 end if;
8281
8282 elsif Nkind (Decl) = N_Package_Declaration
8283 and then Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration
8284 then
8285 return Original_Node (Decl);
8286
8287 else
8288 Inst := Next (Decl);
8289 while not Nkind_In (Inst, N_Package_Instantiation,
8290 N_Formal_Package_Declaration)
8291 loop
8292 Next (Inst);
8293 end loop;
8294
8295 return Inst;
8296 end if;
8297 end Get_Package_Instantiation_Node;
8298
8299 ------------------------
8300 -- Has_Been_Exchanged --
8301 ------------------------
8302
8303 function Has_Been_Exchanged (E : Entity_Id) return Boolean is
8304 Next : Elmt_Id;
8305
8306 begin
8307 Next := First_Elmt (Exchanged_Views);
8308 while Present (Next) loop
8309 if Full_View (Node (Next)) = E then
8310 return True;
8311 end if;
8312
8313 Next_Elmt (Next);
8314 end loop;
8315
8316 return False;
8317 end Has_Been_Exchanged;
8318
8319 ----------
8320 -- Hash --
8321 ----------
8322
8323 function Hash (F : Entity_Id) return HTable_Range is
8324 begin
8325 return HTable_Range (F mod HTable_Size);
8326 end Hash;
8327
8328 ------------------------
8329 -- Hide_Current_Scope --
8330 ------------------------
8331
8332 procedure Hide_Current_Scope is
8333 C : constant Entity_Id := Current_Scope;
8334 E : Entity_Id;
8335
8336 begin
8337 Set_Is_Hidden_Open_Scope (C);
8338
8339 E := First_Entity (C);
8340 while Present (E) loop
8341 if Is_Immediately_Visible (E) then
8342 Set_Is_Immediately_Visible (E, False);
8343 Append_Elmt (E, Hidden_Entities);
8344 end if;
8345
8346 Next_Entity (E);
8347 end loop;
8348
8349 -- Make the scope name invisible as well. This is necessary, but might
8350 -- conflict with calls to Rtsfind later on, in case the scope is a
8351 -- predefined one. There is no clean solution to this problem, so for
8352 -- now we depend on the user not redefining Standard itself in one of
8353 -- the parent units.
8354
8355 if Is_Immediately_Visible (C) and then C /= Standard_Standard then
8356 Set_Is_Immediately_Visible (C, False);
8357 Append_Elmt (C, Hidden_Entities);
8358 end if;
8359
8360 end Hide_Current_Scope;
8361
8362 --------------
8363 -- Init_Env --
8364 --------------
8365
8366 procedure Init_Env is
8367 Saved : Instance_Env;
8368
8369 begin
8370 Saved.Instantiated_Parent := Current_Instantiated_Parent;
8371 Saved.Exchanged_Views := Exchanged_Views;
8372 Saved.Hidden_Entities := Hidden_Entities;
8373 Saved.Current_Sem_Unit := Current_Sem_Unit;
8374 Saved.Parent_Unit_Visible := Parent_Unit_Visible;
8375 Saved.Instance_Parent_Unit := Instance_Parent_Unit;
8376
8377 -- Save configuration switches. These may be reset if the unit is a
8378 -- predefined unit, and the current mode is not Ada 2005.
8379
8380 Save_Opt_Config_Switches (Saved.Switches);
8381
8382 Instance_Envs.Append (Saved);
8383
8384 Exchanged_Views := New_Elmt_List;
8385 Hidden_Entities := New_Elmt_List;
8386
8387 -- Make dummy entry for Instantiated parent. If generic unit is legal,
8388 -- this is set properly in Set_Instance_Env.
8389
8390 Current_Instantiated_Parent :=
8391 (Current_Scope, Current_Scope, Assoc_Null);
8392 end Init_Env;
8393
8394 ------------------------------
8395 -- In_Same_Declarative_Part --
8396 ------------------------------
8397
8398 function In_Same_Declarative_Part
8399 (F_Node : Node_Id;
8400 Inst : Node_Id) return Boolean
8401 is
8402 Decls : constant Node_Id := Parent (F_Node);
8403 Nod : Node_Id;
8404
8405 begin
8406 Nod := Parent (Inst);
8407 while Present (Nod) loop
8408 if Nod = Decls then
8409 return True;
8410
8411 elsif Nkind_In (Nod, N_Subprogram_Body,
8412 N_Package_Body,
8413 N_Package_Declaration,
8414 N_Task_Body,
8415 N_Protected_Body,
8416 N_Block_Statement)
8417 then
8418 return False;
8419
8420 elsif Nkind (Nod) = N_Subunit then
8421 Nod := Corresponding_Stub (Nod);
8422
8423 elsif Nkind (Nod) = N_Compilation_Unit then
8424 return False;
8425
8426 else
8427 Nod := Parent (Nod);
8428 end if;
8429 end loop;
8430
8431 return False;
8432 end In_Same_Declarative_Part;
8433
8434 ---------------------
8435 -- In_Main_Context --
8436 ---------------------
8437
8438 function In_Main_Context (E : Entity_Id) return Boolean is
8439 Context : List_Id;
8440 Clause : Node_Id;
8441 Nam : Node_Id;
8442
8443 begin
8444 if not Is_Compilation_Unit (E)
8445 or else Ekind (E) /= E_Package
8446 or else In_Private_Part (E)
8447 then
8448 return False;
8449 end if;
8450
8451 Context := Context_Items (Cunit (Main_Unit));
8452
8453 Clause := First (Context);
8454 while Present (Clause) loop
8455 if Nkind (Clause) = N_With_Clause then
8456 Nam := Name (Clause);
8457
8458 -- If the current scope is part of the context of the main unit,
8459 -- analysis of the corresponding with_clause is not complete, and
8460 -- the entity is not set. We use the Chars field directly, which
8461 -- might produce false positives in rare cases, but guarantees
8462 -- that we produce all the instance bodies we will need.
8463
8464 if (Is_Entity_Name (Nam) and then Chars (Nam) = Chars (E))
8465 or else (Nkind (Nam) = N_Selected_Component
8466 and then Chars (Selector_Name (Nam)) = Chars (E))
8467 then
8468 return True;
8469 end if;
8470 end if;
8471
8472 Next (Clause);
8473 end loop;
8474
8475 return False;
8476 end In_Main_Context;
8477
8478 ---------------------
8479 -- Inherit_Context --
8480 ---------------------
8481
8482 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id) is
8483 Current_Context : List_Id;
8484 Current_Unit : Node_Id;
8485 Item : Node_Id;
8486 New_I : Node_Id;
8487
8488 Clause : Node_Id;
8489 OK : Boolean;
8490 Lib_Unit : Node_Id;
8491
8492 begin
8493 if Nkind (Parent (Gen_Decl)) = N_Compilation_Unit then
8494
8495 -- The inherited context is attached to the enclosing compilation
8496 -- unit. This is either the main unit, or the declaration for the
8497 -- main unit (in case the instantiation appears within the package
8498 -- declaration and the main unit is its body).
8499
8500 Current_Unit := Parent (Inst);
8501 while Present (Current_Unit)
8502 and then Nkind (Current_Unit) /= N_Compilation_Unit
8503 loop
8504 Current_Unit := Parent (Current_Unit);
8505 end loop;
8506
8507 Current_Context := Context_Items (Current_Unit);
8508
8509 Item := First (Context_Items (Parent (Gen_Decl)));
8510 while Present (Item) loop
8511 if Nkind (Item) = N_With_Clause then
8512 Lib_Unit := Library_Unit (Item);
8513
8514 -- Take care to prevent direct cyclic with's
8515
8516 if Lib_Unit /= Current_Unit then
8517
8518 -- Do not add a unit if it is already in the context
8519
8520 Clause := First (Current_Context);
8521 OK := True;
8522 while Present (Clause) loop
8523 if Nkind (Clause) = N_With_Clause and then
8524 Library_Unit (Clause) = Lib_Unit
8525 then
8526 OK := False;
8527 exit;
8528 end if;
8529
8530 Next (Clause);
8531 end loop;
8532
8533 if OK then
8534 New_I := New_Copy (Item);
8535 Set_Implicit_With (New_I, True);
8536 Set_Implicit_With_From_Instantiation (New_I, True);
8537 Append (New_I, Current_Context);
8538 end if;
8539 end if;
8540 end if;
8541
8542 Next (Item);
8543 end loop;
8544 end if;
8545 end Inherit_Context;
8546
8547 ----------------
8548 -- Initialize --
8549 ----------------
8550
8551 procedure Initialize is
8552 begin
8553 Generic_Renamings.Init;
8554 Instance_Envs.Init;
8555 Generic_Flags.Init;
8556 Generic_Renamings_HTable.Reset;
8557 Circularity_Detected := False;
8558 Exchanged_Views := No_Elist;
8559 Hidden_Entities := No_Elist;
8560 end Initialize;
8561
8562 -------------------------------------
8563 -- Insert_Freeze_Node_For_Instance --
8564 -------------------------------------
8565
8566 procedure Insert_Freeze_Node_For_Instance
8567 (N : Node_Id;
8568 F_Node : Node_Id)
8569 is
8570 Decl : Node_Id;
8571 Decls : List_Id;
8572 Inst : Entity_Id;
8573 Par_N : Node_Id;
8574
8575 function Enclosing_Body (N : Node_Id) return Node_Id;
8576 -- Find enclosing package or subprogram body, if any. Freeze node may
8577 -- be placed at end of current declarative list if previous instance
8578 -- and current one have different enclosing bodies.
8579
8580 function Previous_Instance (Gen : Entity_Id) return Entity_Id;
8581 -- Find the local instance, if any, that declares the generic that is
8582 -- being instantiated. If present, the freeze node for this instance
8583 -- must follow the freeze node for the previous instance.
8584
8585 --------------------
8586 -- Enclosing_Body --
8587 --------------------
8588
8589 function Enclosing_Body (N : Node_Id) return Node_Id is
8590 P : Node_Id;
8591
8592 begin
8593 P := Parent (N);
8594 while Present (P)
8595 and then Nkind (Parent (P)) /= N_Compilation_Unit
8596 loop
8597 if Nkind_In (P, N_Package_Body, N_Subprogram_Body) then
8598 if Nkind (Parent (P)) = N_Subunit then
8599 return Corresponding_Stub (Parent (P));
8600 else
8601 return P;
8602 end if;
8603 end if;
8604
8605 P := True_Parent (P);
8606 end loop;
8607
8608 return Empty;
8609 end Enclosing_Body;
8610
8611 -----------------------
8612 -- Previous_Instance --
8613 -----------------------
8614
8615 function Previous_Instance (Gen : Entity_Id) return Entity_Id is
8616 S : Entity_Id;
8617
8618 begin
8619 S := Scope (Gen);
8620 while Present (S) and then S /= Standard_Standard loop
8621 if Is_Generic_Instance (S)
8622 and then In_Same_Source_Unit (S, N)
8623 then
8624 return S;
8625 end if;
8626
8627 S := Scope (S);
8628 end loop;
8629
8630 return Empty;
8631 end Previous_Instance;
8632
8633 -- Start of processing for Insert_Freeze_Node_For_Instance
8634
8635 begin
8636 if not Is_List_Member (F_Node) then
8637 Decl := N;
8638 Decls := List_Containing (N);
8639 Inst := Entity (F_Node);
8640 Par_N := Parent (Decls);
8641
8642 -- When processing a subprogram instantiation, utilize the actual
8643 -- subprogram instantiation rather than its package wrapper as it
8644 -- carries all the context information.
8645
8646 if Is_Wrapper_Package (Inst) then
8647 Inst := Related_Instance (Inst);
8648 end if;
8649
8650 -- If this is a package instance, check whether the generic is
8651 -- declared in a previous instance and the current instance is
8652 -- not within the previous one.
8653
8654 if Present (Generic_Parent (Parent (Inst)))
8655 and then Is_In_Main_Unit (N)
8656 then
8657 declare
8658 Enclosing_N : constant Node_Id := Enclosing_Body (N);
8659 Par_I : constant Entity_Id :=
8660 Previous_Instance
8661 (Generic_Parent (Parent (Inst)));
8662 Scop : Entity_Id;
8663
8664 begin
8665 if Present (Par_I)
8666 and then Earlier (N, Freeze_Node (Par_I))
8667 then
8668 Scop := Scope (Inst);
8669
8670 -- If the current instance is within the one that contains
8671 -- the generic, the freeze node for the current one must
8672 -- appear in the current declarative part. Ditto, if the
8673 -- current instance is within another package instance or
8674 -- within a body that does not enclose the current instance.
8675 -- In these three cases the freeze node of the previous
8676 -- instance is not relevant.
8677
8678 while Present (Scop) and then Scop /= Standard_Standard loop
8679 exit when Scop = Par_I
8680 or else
8681 (Is_Generic_Instance (Scop)
8682 and then Scope_Depth (Scop) > Scope_Depth (Par_I));
8683 Scop := Scope (Scop);
8684 end loop;
8685
8686 -- Previous instance encloses current instance
8687
8688 if Scop = Par_I then
8689 null;
8690
8691 -- If the next node is a source body we must freeze in
8692 -- the current scope as well.
8693
8694 elsif Present (Next (N))
8695 and then Nkind_In (Next (N), N_Subprogram_Body,
8696 N_Package_Body)
8697 and then Comes_From_Source (Next (N))
8698 then
8699 null;
8700
8701 -- Current instance is within an unrelated instance
8702
8703 elsif Is_Generic_Instance (Scop) then
8704 null;
8705
8706 -- Current instance is within an unrelated body
8707
8708 elsif Present (Enclosing_N)
8709 and then Enclosing_N /= Enclosing_Body (Par_I)
8710 then
8711 null;
8712
8713 else
8714 Insert_After (Freeze_Node (Par_I), F_Node);
8715 return;
8716 end if;
8717 end if;
8718 end;
8719 end if;
8720
8721 -- When the instantiation occurs in a package declaration, append the
8722 -- freeze node to the private declarations (if any).
8723
8724 if Nkind (Par_N) = N_Package_Specification
8725 and then Decls = Visible_Declarations (Par_N)
8726 and then Present (Private_Declarations (Par_N))
8727 and then not Is_Empty_List (Private_Declarations (Par_N))
8728 then
8729 Decls := Private_Declarations (Par_N);
8730 Decl := First (Decls);
8731 end if;
8732
8733 -- Determine the proper freeze point of a package instantiation. We
8734 -- adhere to the general rule of a package or subprogram body causing
8735 -- freezing of anything before it in the same declarative region. In
8736 -- this case, the proper freeze point of a package instantiation is
8737 -- before the first source body which follows, or before a stub. This
8738 -- ensures that entities coming from the instance are already frozen
8739 -- and usable in source bodies.
8740
8741 if Nkind (Par_N) /= N_Package_Declaration
8742 and then Ekind (Inst) = E_Package
8743 and then Is_Generic_Instance (Inst)
8744 and then
8745 not In_Same_Source_Unit (Generic_Parent (Parent (Inst)), Inst)
8746 then
8747 while Present (Decl) loop
8748 if (Nkind (Decl) in N_Unit_Body
8749 or else
8750 Nkind (Decl) in N_Body_Stub)
8751 and then Comes_From_Source (Decl)
8752 then
8753 Insert_Before (Decl, F_Node);
8754 return;
8755 end if;
8756
8757 Next (Decl);
8758 end loop;
8759 end if;
8760
8761 -- In a package declaration, or if no previous body, insert at end
8762 -- of list.
8763
8764 Set_Sloc (F_Node, Sloc (Last (Decls)));
8765 Insert_After (Last (Decls), F_Node);
8766 end if;
8767 end Insert_Freeze_Node_For_Instance;
8768
8769 ------------------
8770 -- Install_Body --
8771 ------------------
8772
8773 procedure Install_Body
8774 (Act_Body : Node_Id;
8775 N : Node_Id;
8776 Gen_Body : Node_Id;
8777 Gen_Decl : Node_Id)
8778 is
8779 Act_Id : constant Entity_Id := Corresponding_Spec (Act_Body);
8780 Act_Unit : constant Node_Id := Unit (Cunit (Get_Source_Unit (N)));
8781 Gen_Id : constant Entity_Id := Corresponding_Spec (Gen_Body);
8782 Par : constant Entity_Id := Scope (Gen_Id);
8783 Gen_Unit : constant Node_Id :=
8784 Unit (Cunit (Get_Source_Unit (Gen_Decl)));
8785 Orig_Body : Node_Id := Gen_Body;
8786 F_Node : Node_Id;
8787 Body_Unit : Node_Id;
8788
8789 Must_Delay : Boolean;
8790
8791 function In_Same_Enclosing_Subp return Boolean;
8792 -- Check whether instance and generic body are within same subprogram.
8793
8794 function True_Sloc (N : Node_Id) return Source_Ptr;
8795 -- If the instance is nested inside a generic unit, the Sloc of the
8796 -- instance indicates the place of the original definition, not the
8797 -- point of the current enclosing instance. Pending a better usage of
8798 -- Slocs to indicate instantiation places, we determine the place of
8799 -- origin of a node by finding the maximum sloc of any ancestor node.
8800 -- Why is this not equivalent to Top_Level_Location ???
8801
8802 ----------------------------
8803 -- In_Same_Enclosing_Subp --
8804 ----------------------------
8805
8806 function In_Same_Enclosing_Subp return Boolean is
8807 Scop : Entity_Id;
8808 Subp : Entity_Id;
8809
8810 begin
8811 Scop := Scope (Act_Id);
8812 while Scop /= Standard_Standard
8813 and then not Is_Overloadable (Scop)
8814 loop
8815 Scop := Scope (Scop);
8816 end loop;
8817
8818 if Scop = Standard_Standard then
8819 return False;
8820 else
8821 Subp := Scop;
8822 end if;
8823
8824 Scop := Scope (Gen_Id);
8825 while Scop /= Standard_Standard loop
8826 if Scop = Subp then
8827 return True;
8828 else
8829 Scop := Scope (Scop);
8830 end if;
8831 end loop;
8832
8833 return False;
8834 end In_Same_Enclosing_Subp;
8835
8836 ---------------
8837 -- True_Sloc --
8838 ---------------
8839
8840 function True_Sloc (N : Node_Id) return Source_Ptr is
8841 Res : Source_Ptr;
8842 N1 : Node_Id;
8843
8844 begin
8845 Res := Sloc (N);
8846 N1 := N;
8847 while Present (N1) and then N1 /= Act_Unit loop
8848 if Sloc (N1) > Res then
8849 Res := Sloc (N1);
8850 end if;
8851
8852 N1 := Parent (N1);
8853 end loop;
8854
8855 return Res;
8856 end True_Sloc;
8857
8858 -- Start of processing for Install_Body
8859
8860 begin
8861 -- Handle first the case of an instance with incomplete actual types.
8862 -- The instance body cannot be placed after the declaration because
8863 -- full views have not been seen yet. Any use of the non-limited views
8864 -- in the instance body requires the presence of a regular with_clause
8865 -- in the enclosing unit, and will fail if this with_clause is missing.
8866 -- We place the instance body at the beginning of the enclosing body,
8867 -- which is the unit being compiled. The freeze node for the instance
8868 -- is then placed after the instance body.
8869
8870 if not Is_Empty_Elmt_List (Incomplete_Actuals (Act_Id))
8871 and then Expander_Active
8872 and then Ekind (Scope (Act_Id)) = E_Package
8873 then
8874 declare
8875 Scop : constant Entity_Id := Scope (Act_Id);
8876 Body_Id : constant Node_Id :=
8877 Corresponding_Body (Unit_Declaration_Node (Scop));
8878
8879 begin
8880 Ensure_Freeze_Node (Act_Id);
8881 F_Node := Freeze_Node (Act_Id);
8882 if Present (Body_Id) then
8883 Set_Is_Frozen (Act_Id, False);
8884 Prepend (Act_Body, Declarations (Parent (Body_Id)));
8885 if Is_List_Member (F_Node) then
8886 Remove (F_Node);
8887 end if;
8888
8889 Insert_After (Act_Body, F_Node);
8890 end if;
8891 end;
8892 return;
8893 end if;
8894
8895 -- If the body is a subunit, the freeze point is the corresponding stub
8896 -- in the current compilation, not the subunit itself.
8897
8898 if Nkind (Parent (Gen_Body)) = N_Subunit then
8899 Orig_Body := Corresponding_Stub (Parent (Gen_Body));
8900 else
8901 Orig_Body := Gen_Body;
8902 end if;
8903
8904 Body_Unit := Unit (Cunit (Get_Source_Unit (Orig_Body)));
8905
8906 -- If the instantiation and the generic definition appear in the same
8907 -- package declaration, this is an early instantiation. If they appear
8908 -- in the same declarative part, it is an early instantiation only if
8909 -- the generic body appears textually later, and the generic body is
8910 -- also in the main unit.
8911
8912 -- If instance is nested within a subprogram, and the generic body
8913 -- is not, the instance is delayed because the enclosing body is. If
8914 -- instance and body are within the same scope, or the same subprogram
8915 -- body, indicate explicitly that the instance is delayed.
8916
8917 Must_Delay :=
8918 (Gen_Unit = Act_Unit
8919 and then (Nkind_In (Gen_Unit, N_Package_Declaration,
8920 N_Generic_Package_Declaration)
8921 or else (Gen_Unit = Body_Unit
8922 and then True_Sloc (N) < Sloc (Orig_Body)))
8923 and then Is_In_Main_Unit (Gen_Unit)
8924 and then (Scope (Act_Id) = Scope (Gen_Id)
8925 or else In_Same_Enclosing_Subp));
8926
8927 -- If this is an early instantiation, the freeze node is placed after
8928 -- the generic body. Otherwise, if the generic appears in an instance,
8929 -- we cannot freeze the current instance until the outer one is frozen.
8930 -- This is only relevant if the current instance is nested within some
8931 -- inner scope not itself within the outer instance. If this scope is
8932 -- a package body in the same declarative part as the outer instance,
8933 -- then that body needs to be frozen after the outer instance. Finally,
8934 -- if no delay is needed, we place the freeze node at the end of the
8935 -- current declarative part.
8936
8937 if Expander_Active then
8938 Ensure_Freeze_Node (Act_Id);
8939 F_Node := Freeze_Node (Act_Id);
8940
8941 if Must_Delay then
8942 Insert_After (Orig_Body, F_Node);
8943
8944 elsif Is_Generic_Instance (Par)
8945 and then Present (Freeze_Node (Par))
8946 and then Scope (Act_Id) /= Par
8947 then
8948 -- Freeze instance of inner generic after instance of enclosing
8949 -- generic.
8950
8951 if In_Same_Declarative_Part (Freeze_Node (Par), N) then
8952
8953 -- Handle the following case:
8954
8955 -- package Parent_Inst is new ...
8956 -- Parent_Inst []
8957
8958 -- procedure P ... -- this body freezes Parent_Inst
8959
8960 -- package Inst is new ...
8961
8962 -- In this particular scenario, the freeze node for Inst must
8963 -- be inserted in the same manner as that of Parent_Inst,
8964 -- before the next source body or at the end of the declarative
8965 -- list (body not available). If body P did not exist and
8966 -- Parent_Inst was frozen after Inst, either by a body
8967 -- following Inst or at the end of the declarative region,
8968 -- the freeze node for Inst must be inserted after that of
8969 -- Parent_Inst. This relation is established by comparing
8970 -- the Slocs of Parent_Inst freeze node and Inst.
8971
8972 if List_Containing (Get_Package_Instantiation_Node (Par)) =
8973 List_Containing (N)
8974 and then Sloc (Freeze_Node (Par)) < Sloc (N)
8975 then
8976 Insert_Freeze_Node_For_Instance (N, F_Node);
8977 else
8978 Insert_After (Freeze_Node (Par), F_Node);
8979 end if;
8980
8981 -- Freeze package enclosing instance of inner generic after
8982 -- instance of enclosing generic.
8983
8984 elsif Nkind_In (Parent (N), N_Package_Body, N_Subprogram_Body)
8985 and then In_Same_Declarative_Part (Freeze_Node (Par), Parent (N))
8986 then
8987 declare
8988 Enclosing : Entity_Id;
8989
8990 begin
8991 Enclosing := Corresponding_Spec (Parent (N));
8992
8993 if No (Enclosing) then
8994 Enclosing := Defining_Entity (Parent (N));
8995 end if;
8996
8997 Insert_Freeze_Node_For_Instance (N, F_Node);
8998 Ensure_Freeze_Node (Enclosing);
8999
9000 if not Is_List_Member (Freeze_Node (Enclosing)) then
9001
9002 -- The enclosing context is a subunit, insert the freeze
9003 -- node after the stub.
9004
9005 if Nkind (Parent (Parent (N))) = N_Subunit then
9006 Insert_Freeze_Node_For_Instance
9007 (Corresponding_Stub (Parent (Parent (N))),
9008 Freeze_Node (Enclosing));
9009
9010 -- The enclosing context is a package with a stub body
9011 -- which has already been replaced by the real body.
9012 -- Insert the freeze node after the actual body.
9013
9014 elsif Ekind (Enclosing) = E_Package
9015 and then Present (Body_Entity (Enclosing))
9016 and then Was_Originally_Stub
9017 (Parent (Body_Entity (Enclosing)))
9018 then
9019 Insert_Freeze_Node_For_Instance
9020 (Parent (Body_Entity (Enclosing)),
9021 Freeze_Node (Enclosing));
9022
9023 -- The parent instance has been frozen before the body of
9024 -- the enclosing package, insert the freeze node after
9025 -- the body.
9026
9027 elsif List_Containing (Freeze_Node (Par)) =
9028 List_Containing (Parent (N))
9029 and then Sloc (Freeze_Node (Par)) < Sloc (Parent (N))
9030 then
9031 Insert_Freeze_Node_For_Instance
9032 (Parent (N), Freeze_Node (Enclosing));
9033
9034 else
9035 Insert_After
9036 (Freeze_Node (Par), Freeze_Node (Enclosing));
9037 end if;
9038 end if;
9039 end;
9040
9041 else
9042 Insert_Freeze_Node_For_Instance (N, F_Node);
9043 end if;
9044
9045 else
9046 Insert_Freeze_Node_For_Instance (N, F_Node);
9047 end if;
9048 end if;
9049
9050 Set_Is_Frozen (Act_Id);
9051 Insert_Before (N, Act_Body);
9052 Mark_Rewrite_Insertion (Act_Body);
9053 end Install_Body;
9054
9055 -----------------------------
9056 -- Install_Formal_Packages --
9057 -----------------------------
9058
9059 procedure Install_Formal_Packages (Par : Entity_Id) is
9060 E : Entity_Id;
9061 Gen : Entity_Id;
9062 Gen_E : Entity_Id := Empty;
9063
9064 begin
9065 E := First_Entity (Par);
9066
9067 -- If we are installing an instance parent, locate the formal packages
9068 -- of its generic parent.
9069
9070 if Is_Generic_Instance (Par) then
9071 Gen := Generic_Parent (Package_Specification (Par));
9072 Gen_E := First_Entity (Gen);
9073 end if;
9074
9075 while Present (E) loop
9076 if Ekind (E) = E_Package
9077 and then Nkind (Parent (E)) = N_Package_Renaming_Declaration
9078 then
9079 -- If this is the renaming for the parent instance, done
9080
9081 if Renamed_Object (E) = Par then
9082 exit;
9083
9084 -- The visibility of a formal of an enclosing generic is already
9085 -- correct.
9086
9087 elsif Denotes_Formal_Package (E) then
9088 null;
9089
9090 elsif Present (Associated_Formal_Package (E)) then
9091 Check_Generic_Actuals (Renamed_Object (E), True);
9092 Set_Is_Hidden (E, False);
9093
9094 -- Find formal package in generic unit that corresponds to
9095 -- (instance of) formal package in instance.
9096
9097 while Present (Gen_E) and then Chars (Gen_E) /= Chars (E) loop
9098 Next_Entity (Gen_E);
9099 end loop;
9100
9101 if Present (Gen_E) then
9102 Map_Formal_Package_Entities (Gen_E, E);
9103 end if;
9104 end if;
9105 end if;
9106
9107 Next_Entity (E);
9108
9109 if Present (Gen_E) then
9110 Next_Entity (Gen_E);
9111 end if;
9112 end loop;
9113 end Install_Formal_Packages;
9114
9115 --------------------
9116 -- Install_Parent --
9117 --------------------
9118
9119 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False) is
9120 Ancestors : constant Elist_Id := New_Elmt_List;
9121 S : constant Entity_Id := Current_Scope;
9122 Inst_Par : Entity_Id;
9123 First_Par : Entity_Id;
9124 Inst_Node : Node_Id;
9125 Gen_Par : Entity_Id;
9126 First_Gen : Entity_Id;
9127 Elmt : Elmt_Id;
9128
9129 procedure Install_Noninstance_Specs (Par : Entity_Id);
9130 -- Install the scopes of noninstance parent units ending with Par
9131
9132 procedure Install_Spec (Par : Entity_Id);
9133 -- The child unit is within the declarative part of the parent, so the
9134 -- declarations within the parent are immediately visible.
9135
9136 -------------------------------
9137 -- Install_Noninstance_Specs --
9138 -------------------------------
9139
9140 procedure Install_Noninstance_Specs (Par : Entity_Id) is
9141 begin
9142 if Present (Par)
9143 and then Par /= Standard_Standard
9144 and then not In_Open_Scopes (Par)
9145 then
9146 Install_Noninstance_Specs (Scope (Par));
9147 Install_Spec (Par);
9148 end if;
9149 end Install_Noninstance_Specs;
9150
9151 ------------------
9152 -- Install_Spec --
9153 ------------------
9154
9155 procedure Install_Spec (Par : Entity_Id) is
9156 Spec : constant Node_Id := Package_Specification (Par);
9157
9158 begin
9159 -- If this parent of the child instance is a top-level unit,
9160 -- then record the unit and its visibility for later resetting in
9161 -- Remove_Parent. We exclude units that are generic instances, as we
9162 -- only want to record this information for the ultimate top-level
9163 -- noninstance parent (is that always correct???).
9164
9165 if Scope (Par) = Standard_Standard
9166 and then not Is_Generic_Instance (Par)
9167 then
9168 Parent_Unit_Visible := Is_Immediately_Visible (Par);
9169 Instance_Parent_Unit := Par;
9170 end if;
9171
9172 -- Open the parent scope and make it and its declarations visible.
9173 -- If this point is not within a body, then only the visible
9174 -- declarations should be made visible, and installation of the
9175 -- private declarations is deferred until the appropriate point
9176 -- within analysis of the spec being instantiated (see the handling
9177 -- of parent visibility in Analyze_Package_Specification). This is
9178 -- relaxed in the case where the parent unit is Ada.Tags, to avoid
9179 -- private view problems that occur when compiling instantiations of
9180 -- a generic child of that package (Generic_Dispatching_Constructor).
9181 -- If the instance freezes a tagged type, inlinings of operations
9182 -- from Ada.Tags may need the full view of type Tag. If inlining took
9183 -- proper account of establishing visibility of inlined subprograms'
9184 -- parents then it should be possible to remove this
9185 -- special check. ???
9186
9187 Push_Scope (Par);
9188 Set_Is_Immediately_Visible (Par);
9189 Install_Visible_Declarations (Par);
9190 Set_Use (Visible_Declarations (Spec));
9191
9192 if In_Body or else Is_RTU (Par, Ada_Tags) then
9193 Install_Private_Declarations (Par);
9194 Set_Use (Private_Declarations (Spec));
9195 end if;
9196 end Install_Spec;
9197
9198 -- Start of processing for Install_Parent
9199
9200 begin
9201 -- We need to install the parent instance to compile the instantiation
9202 -- of the child, but the child instance must appear in the current
9203 -- scope. Given that we cannot place the parent above the current scope
9204 -- in the scope stack, we duplicate the current scope and unstack both
9205 -- after the instantiation is complete.
9206
9207 -- If the parent is itself the instantiation of a child unit, we must
9208 -- also stack the instantiation of its parent, and so on. Each such
9209 -- ancestor is the prefix of the name in a prior instantiation.
9210
9211 -- If this is a nested instance, the parent unit itself resolves to
9212 -- a renaming of the parent instance, whose declaration we need.
9213
9214 -- Finally, the parent may be a generic (not an instance) when the
9215 -- child unit appears as a formal package.
9216
9217 Inst_Par := P;
9218
9219 if Present (Renamed_Entity (Inst_Par)) then
9220 Inst_Par := Renamed_Entity (Inst_Par);
9221 end if;
9222
9223 First_Par := Inst_Par;
9224
9225 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
9226
9227 First_Gen := Gen_Par;
9228
9229 while Present (Gen_Par) and then Is_Child_Unit (Gen_Par) loop
9230
9231 -- Load grandparent instance as well
9232
9233 Inst_Node := Get_Package_Instantiation_Node (Inst_Par);
9234
9235 if Nkind (Name (Inst_Node)) = N_Expanded_Name then
9236 Inst_Par := Entity (Prefix (Name (Inst_Node)));
9237
9238 if Present (Renamed_Entity (Inst_Par)) then
9239 Inst_Par := Renamed_Entity (Inst_Par);
9240 end if;
9241
9242 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
9243
9244 if Present (Gen_Par) then
9245 Prepend_Elmt (Inst_Par, Ancestors);
9246
9247 else
9248 -- Parent is not the name of an instantiation
9249
9250 Install_Noninstance_Specs (Inst_Par);
9251 exit;
9252 end if;
9253
9254 else
9255 -- Previous error
9256
9257 exit;
9258 end if;
9259 end loop;
9260
9261 if Present (First_Gen) then
9262 Append_Elmt (First_Par, Ancestors);
9263 else
9264 Install_Noninstance_Specs (First_Par);
9265 end if;
9266
9267 if not Is_Empty_Elmt_List (Ancestors) then
9268 Elmt := First_Elmt (Ancestors);
9269 while Present (Elmt) loop
9270 Install_Spec (Node (Elmt));
9271 Install_Formal_Packages (Node (Elmt));
9272 Next_Elmt (Elmt);
9273 end loop;
9274 end if;
9275
9276 if not In_Body then
9277 Push_Scope (S);
9278 end if;
9279 end Install_Parent;
9280
9281 -------------------------------
9282 -- Install_Hidden_Primitives --
9283 -------------------------------
9284
9285 procedure Install_Hidden_Primitives
9286 (Prims_List : in out Elist_Id;
9287 Gen_T : Entity_Id;
9288 Act_T : Entity_Id)
9289 is
9290 Elmt : Elmt_Id;
9291 List : Elist_Id := No_Elist;
9292 Prim_G_Elmt : Elmt_Id;
9293 Prim_A_Elmt : Elmt_Id;
9294 Prim_G : Node_Id;
9295 Prim_A : Node_Id;
9296
9297 begin
9298 -- No action needed in case of serious errors because we cannot trust
9299 -- in the order of primitives
9300
9301 if Serious_Errors_Detected > 0 then
9302 return;
9303
9304 -- No action possible if we don't have available the list of primitive
9305 -- operations
9306
9307 elsif No (Gen_T)
9308 or else not Is_Record_Type (Gen_T)
9309 or else not Is_Tagged_Type (Gen_T)
9310 or else not Is_Record_Type (Act_T)
9311 or else not Is_Tagged_Type (Act_T)
9312 then
9313 return;
9314
9315 -- There is no need to handle interface types since their primitives
9316 -- cannot be hidden
9317
9318 elsif Is_Interface (Gen_T) then
9319 return;
9320 end if;
9321
9322 Prim_G_Elmt := First_Elmt (Primitive_Operations (Gen_T));
9323
9324 if not Is_Class_Wide_Type (Act_T) then
9325 Prim_A_Elmt := First_Elmt (Primitive_Operations (Act_T));
9326 else
9327 Prim_A_Elmt := First_Elmt (Primitive_Operations (Root_Type (Act_T)));
9328 end if;
9329
9330 loop
9331 -- Skip predefined primitives in the generic formal
9332
9333 while Present (Prim_G_Elmt)
9334 and then Is_Predefined_Dispatching_Operation (Node (Prim_G_Elmt))
9335 loop
9336 Next_Elmt (Prim_G_Elmt);
9337 end loop;
9338
9339 -- Skip predefined primitives in the generic actual
9340
9341 while Present (Prim_A_Elmt)
9342 and then Is_Predefined_Dispatching_Operation (Node (Prim_A_Elmt))
9343 loop
9344 Next_Elmt (Prim_A_Elmt);
9345 end loop;
9346
9347 exit when No (Prim_G_Elmt) or else No (Prim_A_Elmt);
9348
9349 Prim_G := Node (Prim_G_Elmt);
9350 Prim_A := Node (Prim_A_Elmt);
9351
9352 -- There is no need to handle interface primitives because their
9353 -- primitives are not hidden
9354
9355 exit when Present (Interface_Alias (Prim_G));
9356
9357 -- Here we install one hidden primitive
9358
9359 if Chars (Prim_G) /= Chars (Prim_A)
9360 and then Has_Suffix (Prim_A, 'P')
9361 and then Remove_Suffix (Prim_A, 'P') = Chars (Prim_G)
9362 then
9363 Set_Chars (Prim_A, Chars (Prim_G));
9364 Append_New_Elmt (Prim_A, To => List);
9365 end if;
9366
9367 Next_Elmt (Prim_A_Elmt);
9368 Next_Elmt (Prim_G_Elmt);
9369 end loop;
9370
9371 -- Append the elements to the list of temporarily visible primitives
9372 -- avoiding duplicates.
9373
9374 if Present (List) then
9375 if No (Prims_List) then
9376 Prims_List := New_Elmt_List;
9377 end if;
9378
9379 Elmt := First_Elmt (List);
9380 while Present (Elmt) loop
9381 Append_Unique_Elmt (Node (Elmt), Prims_List);
9382 Next_Elmt (Elmt);
9383 end loop;
9384 end if;
9385 end Install_Hidden_Primitives;
9386
9387 -------------------------------
9388 -- Restore_Hidden_Primitives --
9389 -------------------------------
9390
9391 procedure Restore_Hidden_Primitives (Prims_List : in out Elist_Id) is
9392 Prim_Elmt : Elmt_Id;
9393 Prim : Node_Id;
9394
9395 begin
9396 if Prims_List /= No_Elist then
9397 Prim_Elmt := First_Elmt (Prims_List);
9398 while Present (Prim_Elmt) loop
9399 Prim := Node (Prim_Elmt);
9400 Set_Chars (Prim, Add_Suffix (Prim, 'P'));
9401 Next_Elmt (Prim_Elmt);
9402 end loop;
9403
9404 Prims_List := No_Elist;
9405 end if;
9406 end Restore_Hidden_Primitives;
9407
9408 --------------------------------
9409 -- Instantiate_Formal_Package --
9410 --------------------------------
9411
9412 function Instantiate_Formal_Package
9413 (Formal : Node_Id;
9414 Actual : Node_Id;
9415 Analyzed_Formal : Node_Id) return List_Id
9416 is
9417 Loc : constant Source_Ptr := Sloc (Actual);
9418 Actual_Pack : Entity_Id;
9419 Formal_Pack : Entity_Id;
9420 Gen_Parent : Entity_Id;
9421 Decls : List_Id;
9422 Nod : Node_Id;
9423 Parent_Spec : Node_Id;
9424
9425 procedure Find_Matching_Actual
9426 (F : Node_Id;
9427 Act : in out Entity_Id);
9428 -- We need to associate each formal entity in the formal package with
9429 -- the corresponding entity in the actual package. The actual package
9430 -- has been analyzed and possibly expanded, and as a result there is
9431 -- no one-to-one correspondence between the two lists (for example,
9432 -- the actual may include subtypes, itypes, and inherited primitive
9433 -- operations, interspersed among the renaming declarations for the
9434 -- actuals) . We retrieve the corresponding actual by name because each
9435 -- actual has the same name as the formal, and they do appear in the
9436 -- same order.
9437
9438 function Get_Formal_Entity (N : Node_Id) return Entity_Id;
9439 -- Retrieve entity of defining entity of generic formal parameter.
9440 -- Only the declarations of formals need to be considered when
9441 -- linking them to actuals, but the declarative list may include
9442 -- internal entities generated during analysis, and those are ignored.
9443
9444 procedure Match_Formal_Entity
9445 (Formal_Node : Node_Id;
9446 Formal_Ent : Entity_Id;
9447 Actual_Ent : Entity_Id);
9448 -- Associates the formal entity with the actual. In the case where
9449 -- Formal_Ent is a formal package, this procedure iterates through all
9450 -- of its formals and enters associations between the actuals occurring
9451 -- in the formal package's corresponding actual package (given by
9452 -- Actual_Ent) and the formal package's formal parameters. This
9453 -- procedure recurses if any of the parameters is itself a package.
9454
9455 function Is_Instance_Of
9456 (Act_Spec : Entity_Id;
9457 Gen_Anc : Entity_Id) return Boolean;
9458 -- The actual can be an instantiation of a generic within another
9459 -- instance, in which case there is no direct link from it to the
9460 -- original generic ancestor. In that case, we recognize that the
9461 -- ultimate ancestor is the same by examining names and scopes.
9462
9463 procedure Process_Nested_Formal (Formal : Entity_Id);
9464 -- If the current formal is declared with a box, its own formals are
9465 -- visible in the instance, as they were in the generic, and their
9466 -- Hidden flag must be reset. If some of these formals are themselves
9467 -- packages declared with a box, the processing must be recursive.
9468
9469 --------------------------
9470 -- Find_Matching_Actual --
9471 --------------------------
9472
9473 procedure Find_Matching_Actual
9474 (F : Node_Id;
9475 Act : in out Entity_Id)
9476 is
9477 Formal_Ent : Entity_Id;
9478
9479 begin
9480 case Nkind (Original_Node (F)) is
9481 when N_Formal_Object_Declaration |
9482 N_Formal_Type_Declaration =>
9483 Formal_Ent := Defining_Identifier (F);
9484
9485 while Chars (Act) /= Chars (Formal_Ent) loop
9486 Next_Entity (Act);
9487 end loop;
9488
9489 when N_Formal_Subprogram_Declaration |
9490 N_Formal_Package_Declaration |
9491 N_Package_Declaration |
9492 N_Generic_Package_Declaration =>
9493 Formal_Ent := Defining_Entity (F);
9494
9495 while Chars (Act) /= Chars (Formal_Ent) loop
9496 Next_Entity (Act);
9497 end loop;
9498
9499 when others =>
9500 raise Program_Error;
9501 end case;
9502 end Find_Matching_Actual;
9503
9504 -------------------------
9505 -- Match_Formal_Entity --
9506 -------------------------
9507
9508 procedure Match_Formal_Entity
9509 (Formal_Node : Node_Id;
9510 Formal_Ent : Entity_Id;
9511 Actual_Ent : Entity_Id)
9512 is
9513 Act_Pkg : Entity_Id;
9514
9515 begin
9516 Set_Instance_Of (Formal_Ent, Actual_Ent);
9517
9518 if Ekind (Actual_Ent) = E_Package then
9519
9520 -- Record associations for each parameter
9521
9522 Act_Pkg := Actual_Ent;
9523
9524 declare
9525 A_Ent : Entity_Id := First_Entity (Act_Pkg);
9526 F_Ent : Entity_Id;
9527 F_Node : Node_Id;
9528
9529 Gen_Decl : Node_Id;
9530 Formals : List_Id;
9531 Actual : Entity_Id;
9532
9533 begin
9534 -- Retrieve the actual given in the formal package declaration
9535
9536 Actual := Entity (Name (Original_Node (Formal_Node)));
9537
9538 -- The actual in the formal package declaration may be a
9539 -- renamed generic package, in which case we want to retrieve
9540 -- the original generic in order to traverse its formal part.
9541
9542 if Present (Renamed_Entity (Actual)) then
9543 Gen_Decl := Unit_Declaration_Node (Renamed_Entity (Actual));
9544 else
9545 Gen_Decl := Unit_Declaration_Node (Actual);
9546 end if;
9547
9548 Formals := Generic_Formal_Declarations (Gen_Decl);
9549
9550 if Present (Formals) then
9551 F_Node := First_Non_Pragma (Formals);
9552 else
9553 F_Node := Empty;
9554 end if;
9555
9556 while Present (A_Ent)
9557 and then Present (F_Node)
9558 and then A_Ent /= First_Private_Entity (Act_Pkg)
9559 loop
9560 F_Ent := Get_Formal_Entity (F_Node);
9561
9562 if Present (F_Ent) then
9563
9564 -- This is a formal of the original package. Record
9565 -- association and recurse.
9566
9567 Find_Matching_Actual (F_Node, A_Ent);
9568 Match_Formal_Entity (F_Node, F_Ent, A_Ent);
9569 Next_Entity (A_Ent);
9570 end if;
9571
9572 Next_Non_Pragma (F_Node);
9573 end loop;
9574 end;
9575 end if;
9576 end Match_Formal_Entity;
9577
9578 -----------------------
9579 -- Get_Formal_Entity --
9580 -----------------------
9581
9582 function Get_Formal_Entity (N : Node_Id) return Entity_Id is
9583 Kind : constant Node_Kind := Nkind (Original_Node (N));
9584 begin
9585 case Kind is
9586 when N_Formal_Object_Declaration =>
9587 return Defining_Identifier (N);
9588
9589 when N_Formal_Type_Declaration =>
9590 return Defining_Identifier (N);
9591
9592 when N_Formal_Subprogram_Declaration =>
9593 return Defining_Unit_Name (Specification (N));
9594
9595 when N_Formal_Package_Declaration =>
9596 return Defining_Identifier (Original_Node (N));
9597
9598 when N_Generic_Package_Declaration =>
9599 return Defining_Identifier (Original_Node (N));
9600
9601 -- All other declarations are introduced by semantic analysis and
9602 -- have no match in the actual.
9603
9604 when others =>
9605 return Empty;
9606 end case;
9607 end Get_Formal_Entity;
9608
9609 --------------------
9610 -- Is_Instance_Of --
9611 --------------------
9612
9613 function Is_Instance_Of
9614 (Act_Spec : Entity_Id;
9615 Gen_Anc : Entity_Id) return Boolean
9616 is
9617 Gen_Par : constant Entity_Id := Generic_Parent (Act_Spec);
9618
9619 begin
9620 if No (Gen_Par) then
9621 return False;
9622
9623 -- Simplest case: the generic parent of the actual is the formal
9624
9625 elsif Gen_Par = Gen_Anc then
9626 return True;
9627
9628 elsif Chars (Gen_Par) /= Chars (Gen_Anc) then
9629 return False;
9630
9631 -- The actual may be obtained through several instantiations. Its
9632 -- scope must itself be an instance of a generic declared in the
9633 -- same scope as the formal. Any other case is detected above.
9634
9635 elsif not Is_Generic_Instance (Scope (Gen_Par)) then
9636 return False;
9637
9638 else
9639 return Generic_Parent (Parent (Scope (Gen_Par))) = Scope (Gen_Anc);
9640 end if;
9641 end Is_Instance_Of;
9642
9643 ---------------------------
9644 -- Process_Nested_Formal --
9645 ---------------------------
9646
9647 procedure Process_Nested_Formal (Formal : Entity_Id) is
9648 Ent : Entity_Id;
9649
9650 begin
9651 if Present (Associated_Formal_Package (Formal))
9652 and then Box_Present (Parent (Associated_Formal_Package (Formal)))
9653 then
9654 Ent := First_Entity (Formal);
9655 while Present (Ent) loop
9656 Set_Is_Hidden (Ent, False);
9657 Set_Is_Visible_Formal (Ent);
9658 Set_Is_Potentially_Use_Visible
9659 (Ent, Is_Potentially_Use_Visible (Formal));
9660
9661 if Ekind (Ent) = E_Package then
9662 exit when Renamed_Entity (Ent) = Renamed_Entity (Formal);
9663 Process_Nested_Formal (Ent);
9664 end if;
9665
9666 Next_Entity (Ent);
9667 end loop;
9668 end if;
9669 end Process_Nested_Formal;
9670
9671 -- Start of processing for Instantiate_Formal_Package
9672
9673 begin
9674 Analyze (Actual);
9675
9676 if not Is_Entity_Name (Actual)
9677 or else Ekind (Entity (Actual)) /= E_Package
9678 then
9679 Error_Msg_N
9680 ("expect package instance to instantiate formal", Actual);
9681 Abandon_Instantiation (Actual);
9682 raise Program_Error;
9683
9684 else
9685 Actual_Pack := Entity (Actual);
9686 Set_Is_Instantiated (Actual_Pack);
9687
9688 -- The actual may be a renamed package, or an outer generic formal
9689 -- package whose instantiation is converted into a renaming.
9690
9691 if Present (Renamed_Object (Actual_Pack)) then
9692 Actual_Pack := Renamed_Object (Actual_Pack);
9693 end if;
9694
9695 if Nkind (Analyzed_Formal) = N_Formal_Package_Declaration then
9696 Gen_Parent := Get_Instance_Of (Entity (Name (Analyzed_Formal)));
9697 Formal_Pack := Defining_Identifier (Analyzed_Formal);
9698 else
9699 Gen_Parent :=
9700 Generic_Parent (Specification (Analyzed_Formal));
9701 Formal_Pack :=
9702 Defining_Unit_Name (Specification (Analyzed_Formal));
9703 end if;
9704
9705 if Nkind (Parent (Actual_Pack)) = N_Defining_Program_Unit_Name then
9706 Parent_Spec := Package_Specification (Actual_Pack);
9707 else
9708 Parent_Spec := Parent (Actual_Pack);
9709 end if;
9710
9711 if Gen_Parent = Any_Id then
9712 Error_Msg_N
9713 ("previous error in declaration of formal package", Actual);
9714 Abandon_Instantiation (Actual);
9715
9716 elsif
9717 Is_Instance_Of (Parent_Spec, Get_Instance_Of (Gen_Parent))
9718 then
9719 null;
9720
9721 else
9722 Error_Msg_NE
9723 ("actual parameter must be instance of&", Actual, Gen_Parent);
9724 Abandon_Instantiation (Actual);
9725 end if;
9726
9727 Set_Instance_Of (Defining_Identifier (Formal), Actual_Pack);
9728 Map_Formal_Package_Entities (Formal_Pack, Actual_Pack);
9729
9730 Nod :=
9731 Make_Package_Renaming_Declaration (Loc,
9732 Defining_Unit_Name => New_Copy (Defining_Identifier (Formal)),
9733 Name => New_Occurrence_Of (Actual_Pack, Loc));
9734
9735 Set_Associated_Formal_Package
9736 (Defining_Unit_Name (Nod), Defining_Identifier (Formal));
9737 Decls := New_List (Nod);
9738
9739 -- If the formal F has a box, then the generic declarations are
9740 -- visible in the generic G. In an instance of G, the corresponding
9741 -- entities in the actual for F (which are the actuals for the
9742 -- instantiation of the generic that F denotes) must also be made
9743 -- visible for analysis of the current instance. On exit from the
9744 -- current instance, those entities are made private again. If the
9745 -- actual is currently in use, these entities are also use-visible.
9746
9747 -- The loop through the actual entities also steps through the formal
9748 -- entities and enters associations from formals to actuals into the
9749 -- renaming map. This is necessary to properly handle checking of
9750 -- actual parameter associations for later formals that depend on
9751 -- actuals declared in the formal package.
9752
9753 -- In Ada 2005, partial parameterization requires that we make
9754 -- visible the actuals corresponding to formals that were defaulted
9755 -- in the formal package. There formals are identified because they
9756 -- remain formal generics within the formal package, rather than
9757 -- being renamings of the actuals supplied.
9758
9759 declare
9760 Gen_Decl : constant Node_Id :=
9761 Unit_Declaration_Node (Gen_Parent);
9762 Formals : constant List_Id :=
9763 Generic_Formal_Declarations (Gen_Decl);
9764
9765 Actual_Ent : Entity_Id;
9766 Actual_Of_Formal : Node_Id;
9767 Formal_Node : Node_Id;
9768 Formal_Ent : Entity_Id;
9769
9770 begin
9771 if Present (Formals) then
9772 Formal_Node := First_Non_Pragma (Formals);
9773 else
9774 Formal_Node := Empty;
9775 end if;
9776
9777 Actual_Ent := First_Entity (Actual_Pack);
9778 Actual_Of_Formal :=
9779 First (Visible_Declarations (Specification (Analyzed_Formal)));
9780 while Present (Actual_Ent)
9781 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
9782 loop
9783 if Present (Formal_Node) then
9784 Formal_Ent := Get_Formal_Entity (Formal_Node);
9785
9786 if Present (Formal_Ent) then
9787 Find_Matching_Actual (Formal_Node, Actual_Ent);
9788 Match_Formal_Entity (Formal_Node, Formal_Ent, Actual_Ent);
9789
9790 -- We iterate at the same time over the actuals of the
9791 -- local package created for the formal, to determine
9792 -- which one of the formals of the original generic were
9793 -- defaulted in the formal. The corresponding actual
9794 -- entities are visible in the enclosing instance.
9795
9796 if Box_Present (Formal)
9797 or else
9798 (Present (Actual_Of_Formal)
9799 and then
9800 Is_Generic_Formal
9801 (Get_Formal_Entity (Actual_Of_Formal)))
9802 then
9803 Set_Is_Hidden (Actual_Ent, False);
9804 Set_Is_Visible_Formal (Actual_Ent);
9805 Set_Is_Potentially_Use_Visible
9806 (Actual_Ent, In_Use (Actual_Pack));
9807
9808 if Ekind (Actual_Ent) = E_Package then
9809 Process_Nested_Formal (Actual_Ent);
9810 end if;
9811
9812 else
9813 Set_Is_Hidden (Actual_Ent);
9814 Set_Is_Potentially_Use_Visible (Actual_Ent, False);
9815 end if;
9816 end if;
9817
9818 Next_Non_Pragma (Formal_Node);
9819 Next (Actual_Of_Formal);
9820
9821 else
9822 -- No further formals to match, but the generic part may
9823 -- contain inherited operation that are not hidden in the
9824 -- enclosing instance.
9825
9826 Next_Entity (Actual_Ent);
9827 end if;
9828 end loop;
9829
9830 -- Inherited subprograms generated by formal derived types are
9831 -- also visible if the types are.
9832
9833 Actual_Ent := First_Entity (Actual_Pack);
9834 while Present (Actual_Ent)
9835 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
9836 loop
9837 if Is_Overloadable (Actual_Ent)
9838 and then
9839 Nkind (Parent (Actual_Ent)) = N_Subtype_Declaration
9840 and then
9841 not Is_Hidden (Defining_Identifier (Parent (Actual_Ent)))
9842 then
9843 Set_Is_Hidden (Actual_Ent, False);
9844 Set_Is_Potentially_Use_Visible
9845 (Actual_Ent, In_Use (Actual_Pack));
9846 end if;
9847
9848 Next_Entity (Actual_Ent);
9849 end loop;
9850 end;
9851
9852 -- If the formal is not declared with a box, reanalyze it as an
9853 -- abbreviated instantiation, to verify the matching rules of 12.7.
9854 -- The actual checks are performed after the generic associations
9855 -- have been analyzed, to guarantee the same visibility for this
9856 -- instantiation and for the actuals.
9857
9858 -- In Ada 2005, the generic associations for the formal can include
9859 -- defaulted parameters. These are ignored during check. This
9860 -- internal instantiation is removed from the tree after conformance
9861 -- checking, because it contains formal declarations for those
9862 -- defaulted parameters, and those should not reach the back-end.
9863
9864 if not Box_Present (Formal) then
9865 declare
9866 I_Pack : constant Entity_Id :=
9867 Make_Temporary (Sloc (Actual), 'P');
9868
9869 begin
9870 Set_Is_Internal (I_Pack);
9871
9872 Append_To (Decls,
9873 Make_Package_Instantiation (Sloc (Actual),
9874 Defining_Unit_Name => I_Pack,
9875 Name =>
9876 New_Occurrence_Of
9877 (Get_Instance_Of (Gen_Parent), Sloc (Actual)),
9878 Generic_Associations => Generic_Associations (Formal)));
9879 end;
9880 end if;
9881
9882 return Decls;
9883 end if;
9884 end Instantiate_Formal_Package;
9885
9886 -----------------------------------
9887 -- Instantiate_Formal_Subprogram --
9888 -----------------------------------
9889
9890 function Instantiate_Formal_Subprogram
9891 (Formal : Node_Id;
9892 Actual : Node_Id;
9893 Analyzed_Formal : Node_Id) return Node_Id
9894 is
9895 Analyzed_S : constant Entity_Id :=
9896 Defining_Unit_Name (Specification (Analyzed_Formal));
9897 Formal_Sub : constant Entity_Id :=
9898 Defining_Unit_Name (Specification (Formal));
9899
9900 function From_Parent_Scope (Subp : Entity_Id) return Boolean;
9901 -- If the generic is a child unit, the parent has been installed on the
9902 -- scope stack, but a default subprogram cannot resolve to something
9903 -- on the parent because that parent is not really part of the visible
9904 -- context (it is there to resolve explicit local entities). If the
9905 -- default has resolved in this way, we remove the entity from immediate
9906 -- visibility and analyze the node again to emit an error message or
9907 -- find another visible candidate.
9908
9909 procedure Valid_Actual_Subprogram (Act : Node_Id);
9910 -- Perform legality check and raise exception on failure
9911
9912 -----------------------
9913 -- From_Parent_Scope --
9914 -----------------------
9915
9916 function From_Parent_Scope (Subp : Entity_Id) return Boolean is
9917 Gen_Scope : Node_Id;
9918
9919 begin
9920 Gen_Scope := Scope (Analyzed_S);
9921 while Present (Gen_Scope) and then Is_Child_Unit (Gen_Scope) loop
9922 if Scope (Subp) = Scope (Gen_Scope) then
9923 return True;
9924 end if;
9925
9926 Gen_Scope := Scope (Gen_Scope);
9927 end loop;
9928
9929 return False;
9930 end From_Parent_Scope;
9931
9932 -----------------------------
9933 -- Valid_Actual_Subprogram --
9934 -----------------------------
9935
9936 procedure Valid_Actual_Subprogram (Act : Node_Id) is
9937 Act_E : Entity_Id;
9938
9939 begin
9940 if Is_Entity_Name (Act) then
9941 Act_E := Entity (Act);
9942
9943 elsif Nkind (Act) = N_Selected_Component
9944 and then Is_Entity_Name (Selector_Name (Act))
9945 then
9946 Act_E := Entity (Selector_Name (Act));
9947
9948 else
9949 Act_E := Empty;
9950 end if;
9951
9952 if (Present (Act_E) and then Is_Overloadable (Act_E))
9953 or else Nkind_In (Act, N_Attribute_Reference,
9954 N_Indexed_Component,
9955 N_Character_Literal,
9956 N_Explicit_Dereference)
9957 then
9958 return;
9959 end if;
9960
9961 Error_Msg_NE
9962 ("expect subprogram or entry name in instantiation of &",
9963 Instantiation_Node, Formal_Sub);
9964 Abandon_Instantiation (Instantiation_Node);
9965 end Valid_Actual_Subprogram;
9966
9967 -- Local variables
9968
9969 Decl_Node : Node_Id;
9970 Loc : Source_Ptr;
9971 Nam : Node_Id;
9972 New_Spec : Node_Id;
9973 New_Subp : Entity_Id;
9974
9975 -- Start of processing for Instantiate_Formal_Subprogram
9976
9977 begin
9978 New_Spec := New_Copy_Tree (Specification (Formal));
9979
9980 -- The tree copy has created the proper instantiation sloc for the
9981 -- new specification. Use this location for all other constructed
9982 -- declarations.
9983
9984 Loc := Sloc (Defining_Unit_Name (New_Spec));
9985
9986 -- Create new entity for the actual (New_Copy_Tree does not), and
9987 -- indicate that it is an actual.
9988
9989 New_Subp := Make_Defining_Identifier (Loc, Chars (Formal_Sub));
9990 Set_Ekind (New_Subp, Ekind (Analyzed_S));
9991 Set_Is_Generic_Actual_Subprogram (New_Subp);
9992 Set_Defining_Unit_Name (New_Spec, New_Subp);
9993
9994 -- Create new entities for the each of the formals in the specification
9995 -- of the renaming declaration built for the actual.
9996
9997 if Present (Parameter_Specifications (New_Spec)) then
9998 declare
9999 F : Node_Id;
10000 F_Id : Entity_Id;
10001
10002 begin
10003 F := First (Parameter_Specifications (New_Spec));
10004 while Present (F) loop
10005 F_Id := Defining_Identifier (F);
10006
10007 Set_Defining_Identifier (F,
10008 Make_Defining_Identifier (Sloc (F_Id), Chars (F_Id)));
10009 Next (F);
10010 end loop;
10011 end;
10012 end if;
10013
10014 -- Find entity of actual. If the actual is an attribute reference, it
10015 -- cannot be resolved here (its formal is missing) but is handled
10016 -- instead in Attribute_Renaming. If the actual is overloaded, it is
10017 -- fully resolved subsequently, when the renaming declaration for the
10018 -- formal is analyzed. If it is an explicit dereference, resolve the
10019 -- prefix but not the actual itself, to prevent interpretation as call.
10020
10021 if Present (Actual) then
10022 Loc := Sloc (Actual);
10023 Set_Sloc (New_Spec, Loc);
10024
10025 if Nkind (Actual) = N_Operator_Symbol then
10026 Find_Direct_Name (Actual);
10027
10028 elsif Nkind (Actual) = N_Explicit_Dereference then
10029 Analyze (Prefix (Actual));
10030
10031 elsif Nkind (Actual) /= N_Attribute_Reference then
10032 Analyze (Actual);
10033 end if;
10034
10035 Valid_Actual_Subprogram (Actual);
10036 Nam := Actual;
10037
10038 elsif Present (Default_Name (Formal)) then
10039 if not Nkind_In (Default_Name (Formal), N_Attribute_Reference,
10040 N_Selected_Component,
10041 N_Indexed_Component,
10042 N_Character_Literal)
10043 and then Present (Entity (Default_Name (Formal)))
10044 then
10045 Nam := New_Occurrence_Of (Entity (Default_Name (Formal)), Loc);
10046 else
10047 Nam := New_Copy (Default_Name (Formal));
10048 Set_Sloc (Nam, Loc);
10049 end if;
10050
10051 elsif Box_Present (Formal) then
10052
10053 -- Actual is resolved at the point of instantiation. Create an
10054 -- identifier or operator with the same name as the formal.
10055
10056 if Nkind (Formal_Sub) = N_Defining_Operator_Symbol then
10057 Nam :=
10058 Make_Operator_Symbol (Loc,
10059 Chars => Chars (Formal_Sub),
10060 Strval => No_String);
10061 else
10062 Nam := Make_Identifier (Loc, Chars (Formal_Sub));
10063 end if;
10064
10065 elsif Nkind (Specification (Formal)) = N_Procedure_Specification
10066 and then Null_Present (Specification (Formal))
10067 then
10068 -- Generate null body for procedure, for use in the instance
10069
10070 Decl_Node :=
10071 Make_Subprogram_Body (Loc,
10072 Specification => New_Spec,
10073 Declarations => New_List,
10074 Handled_Statement_Sequence =>
10075 Make_Handled_Sequence_Of_Statements (Loc,
10076 Statements => New_List (Make_Null_Statement (Loc))));
10077
10078 Set_Is_Intrinsic_Subprogram (Defining_Unit_Name (New_Spec));
10079 return Decl_Node;
10080
10081 else
10082 Error_Msg_Sloc := Sloc (Scope (Analyzed_S));
10083 Error_Msg_NE
10084 ("missing actual&", Instantiation_Node, Formal_Sub);
10085 Error_Msg_NE
10086 ("\in instantiation of & declared#",
10087 Instantiation_Node, Scope (Analyzed_S));
10088 Abandon_Instantiation (Instantiation_Node);
10089 end if;
10090
10091 Decl_Node :=
10092 Make_Subprogram_Renaming_Declaration (Loc,
10093 Specification => New_Spec,
10094 Name => Nam);
10095
10096 -- If we do not have an actual and the formal specified <> then set to
10097 -- get proper default.
10098
10099 if No (Actual) and then Box_Present (Formal) then
10100 Set_From_Default (Decl_Node);
10101 end if;
10102
10103 -- Gather possible interpretations for the actual before analyzing the
10104 -- instance. If overloaded, it will be resolved when analyzing the
10105 -- renaming declaration.
10106
10107 if Box_Present (Formal) and then No (Actual) then
10108 Analyze (Nam);
10109
10110 if Is_Child_Unit (Scope (Analyzed_S))
10111 and then Present (Entity (Nam))
10112 then
10113 if not Is_Overloaded (Nam) then
10114 if From_Parent_Scope (Entity (Nam)) then
10115 Set_Is_Immediately_Visible (Entity (Nam), False);
10116 Set_Entity (Nam, Empty);
10117 Set_Etype (Nam, Empty);
10118
10119 Analyze (Nam);
10120 Set_Is_Immediately_Visible (Entity (Nam));
10121 end if;
10122
10123 else
10124 declare
10125 I : Interp_Index;
10126 It : Interp;
10127
10128 begin
10129 Get_First_Interp (Nam, I, It);
10130 while Present (It.Nam) loop
10131 if From_Parent_Scope (It.Nam) then
10132 Remove_Interp (I);
10133 end if;
10134
10135 Get_Next_Interp (I, It);
10136 end loop;
10137 end;
10138 end if;
10139 end if;
10140 end if;
10141
10142 -- The generic instantiation freezes the actual. This can only be done
10143 -- once the actual is resolved, in the analysis of the renaming
10144 -- declaration. To make the formal subprogram entity available, we set
10145 -- Corresponding_Formal_Spec to point to the formal subprogram entity.
10146 -- This is also needed in Analyze_Subprogram_Renaming for the processing
10147 -- of formal abstract subprograms.
10148
10149 Set_Corresponding_Formal_Spec (Decl_Node, Analyzed_S);
10150
10151 -- We cannot analyze the renaming declaration, and thus find the actual,
10152 -- until all the actuals are assembled in the instance. For subsequent
10153 -- checks of other actuals, indicate the node that will hold the
10154 -- instance of this formal.
10155
10156 Set_Instance_Of (Analyzed_S, Nam);
10157
10158 if Nkind (Actual) = N_Selected_Component
10159 and then Is_Task_Type (Etype (Prefix (Actual)))
10160 and then not Is_Frozen (Etype (Prefix (Actual)))
10161 then
10162 -- The renaming declaration will create a body, which must appear
10163 -- outside of the instantiation, We move the renaming declaration
10164 -- out of the instance, and create an additional renaming inside,
10165 -- to prevent freezing anomalies.
10166
10167 declare
10168 Anon_Id : constant Entity_Id := Make_Temporary (Loc, 'E');
10169
10170 begin
10171 Set_Defining_Unit_Name (New_Spec, Anon_Id);
10172 Insert_Before (Instantiation_Node, Decl_Node);
10173 Analyze (Decl_Node);
10174
10175 -- Now create renaming within the instance
10176
10177 Decl_Node :=
10178 Make_Subprogram_Renaming_Declaration (Loc,
10179 Specification => New_Copy_Tree (New_Spec),
10180 Name => New_Occurrence_Of (Anon_Id, Loc));
10181
10182 Set_Defining_Unit_Name (Specification (Decl_Node),
10183 Make_Defining_Identifier (Loc, Chars (Formal_Sub)));
10184 end;
10185 end if;
10186
10187 return Decl_Node;
10188 end Instantiate_Formal_Subprogram;
10189
10190 ------------------------
10191 -- Instantiate_Object --
10192 ------------------------
10193
10194 function Instantiate_Object
10195 (Formal : Node_Id;
10196 Actual : Node_Id;
10197 Analyzed_Formal : Node_Id) return List_Id
10198 is
10199 Gen_Obj : constant Entity_Id := Defining_Identifier (Formal);
10200 A_Gen_Obj : constant Entity_Id :=
10201 Defining_Identifier (Analyzed_Formal);
10202 Acc_Def : Node_Id := Empty;
10203 Act_Assoc : constant Node_Id := Parent (Actual);
10204 Actual_Decl : Node_Id := Empty;
10205 Decl_Node : Node_Id;
10206 Def : Node_Id;
10207 Ftyp : Entity_Id;
10208 List : constant List_Id := New_List;
10209 Loc : constant Source_Ptr := Sloc (Actual);
10210 Orig_Ftyp : constant Entity_Id := Etype (A_Gen_Obj);
10211 Subt_Decl : Node_Id := Empty;
10212 Subt_Mark : Node_Id := Empty;
10213
10214 function Copy_Access_Def return Node_Id;
10215 -- If formal is an anonymous access, copy access definition of formal
10216 -- for generated object declaration.
10217
10218 ---------------------
10219 -- Copy_Access_Def --
10220 ---------------------
10221
10222 function Copy_Access_Def return Node_Id is
10223 begin
10224 Def := New_Copy_Tree (Acc_Def);
10225
10226 -- In addition, if formal is an access to subprogram we need to
10227 -- generate new formals for the signature of the default, so that
10228 -- the tree is properly formatted for ASIS use.
10229
10230 if Present (Access_To_Subprogram_Definition (Acc_Def)) then
10231 declare
10232 Par_Spec : Node_Id;
10233 begin
10234 Par_Spec :=
10235 First (Parameter_Specifications
10236 (Access_To_Subprogram_Definition (Def)));
10237 while Present (Par_Spec) loop
10238 Set_Defining_Identifier (Par_Spec,
10239 Make_Defining_Identifier (Sloc (Acc_Def),
10240 Chars => Chars (Defining_Identifier (Par_Spec))));
10241 Next (Par_Spec);
10242 end loop;
10243 end;
10244 end if;
10245
10246 return Def;
10247 end Copy_Access_Def;
10248
10249 -- Start of processing for Instantiate_Object
10250
10251 begin
10252 -- Formal may be an anonymous access
10253
10254 if Present (Subtype_Mark (Formal)) then
10255 Subt_Mark := Subtype_Mark (Formal);
10256 else
10257 Check_Access_Definition (Formal);
10258 Acc_Def := Access_Definition (Formal);
10259 end if;
10260
10261 -- Sloc for error message on missing actual
10262
10263 Error_Msg_Sloc := Sloc (Scope (A_Gen_Obj));
10264
10265 if Get_Instance_Of (Gen_Obj) /= Gen_Obj then
10266 Error_Msg_N ("duplicate instantiation of generic parameter", Actual);
10267 end if;
10268
10269 Set_Parent (List, Parent (Actual));
10270
10271 -- OUT present
10272
10273 if Out_Present (Formal) then
10274
10275 -- An IN OUT generic actual must be a name. The instantiation is a
10276 -- renaming declaration. The actual is the name being renamed. We
10277 -- use the actual directly, rather than a copy, because it is not
10278 -- used further in the list of actuals, and because a copy or a use
10279 -- of relocate_node is incorrect if the instance is nested within a
10280 -- generic. In order to simplify ASIS searches, the Generic_Parent
10281 -- field links the declaration to the generic association.
10282
10283 if No (Actual) then
10284 Error_Msg_NE
10285 ("missing actual &",
10286 Instantiation_Node, Gen_Obj);
10287 Error_Msg_NE
10288 ("\in instantiation of & declared#",
10289 Instantiation_Node, Scope (A_Gen_Obj));
10290 Abandon_Instantiation (Instantiation_Node);
10291 end if;
10292
10293 if Present (Subt_Mark) then
10294 Decl_Node :=
10295 Make_Object_Renaming_Declaration (Loc,
10296 Defining_Identifier => New_Copy (Gen_Obj),
10297 Subtype_Mark => New_Copy_Tree (Subt_Mark),
10298 Name => Actual);
10299
10300 else pragma Assert (Present (Acc_Def));
10301 Decl_Node :=
10302 Make_Object_Renaming_Declaration (Loc,
10303 Defining_Identifier => New_Copy (Gen_Obj),
10304 Access_Definition => New_Copy_Tree (Acc_Def),
10305 Name => Actual);
10306 end if;
10307
10308 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
10309
10310 -- The analysis of the actual may produce Insert_Action nodes, so
10311 -- the declaration must have a context in which to attach them.
10312
10313 Append (Decl_Node, List);
10314 Analyze (Actual);
10315
10316 -- Return if the analysis of the actual reported some error
10317
10318 if Etype (Actual) = Any_Type then
10319 return List;
10320 end if;
10321
10322 -- This check is performed here because Analyze_Object_Renaming will
10323 -- not check it when Comes_From_Source is False. Note though that the
10324 -- check for the actual being the name of an object will be performed
10325 -- in Analyze_Object_Renaming.
10326
10327 if Is_Object_Reference (Actual)
10328 and then Is_Dependent_Component_Of_Mutable_Object (Actual)
10329 then
10330 Error_Msg_N
10331 ("illegal discriminant-dependent component for in out parameter",
10332 Actual);
10333 end if;
10334
10335 -- The actual has to be resolved in order to check that it is a
10336 -- variable (due to cases such as F (1), where F returns access to
10337 -- an array, and for overloaded prefixes).
10338
10339 Ftyp := Get_Instance_Of (Etype (A_Gen_Obj));
10340
10341 -- If the type of the formal is not itself a formal, and the current
10342 -- unit is a child unit, the formal type must be declared in a
10343 -- parent, and must be retrieved by visibility.
10344
10345 if Ftyp = Orig_Ftyp
10346 and then Is_Generic_Unit (Scope (Ftyp))
10347 and then Is_Child_Unit (Scope (A_Gen_Obj))
10348 then
10349 declare
10350 Temp : constant Node_Id :=
10351 New_Copy_Tree (Subtype_Mark (Analyzed_Formal));
10352 begin
10353 Set_Entity (Temp, Empty);
10354 Find_Type (Temp);
10355 Ftyp := Entity (Temp);
10356 end;
10357 end if;
10358
10359 if Is_Private_Type (Ftyp)
10360 and then not Is_Private_Type (Etype (Actual))
10361 and then (Base_Type (Full_View (Ftyp)) = Base_Type (Etype (Actual))
10362 or else Base_Type (Etype (Actual)) = Ftyp)
10363 then
10364 -- If the actual has the type of the full view of the formal, or
10365 -- else a non-private subtype of the formal, then the visibility
10366 -- of the formal type has changed. Add to the actuals a subtype
10367 -- declaration that will force the exchange of views in the body
10368 -- of the instance as well.
10369
10370 Subt_Decl :=
10371 Make_Subtype_Declaration (Loc,
10372 Defining_Identifier => Make_Temporary (Loc, 'P'),
10373 Subtype_Indication => New_Occurrence_Of (Ftyp, Loc));
10374
10375 Prepend (Subt_Decl, List);
10376
10377 Prepend_Elmt (Full_View (Ftyp), Exchanged_Views);
10378 Exchange_Declarations (Ftyp);
10379 end if;
10380
10381 Resolve (Actual, Ftyp);
10382
10383 if not Denotes_Variable (Actual) then
10384 Error_Msg_NE ("actual for& must be a variable", Actual, Gen_Obj);
10385
10386 elsif Base_Type (Ftyp) /= Base_Type (Etype (Actual)) then
10387
10388 -- Ada 2005 (AI-423): For a generic formal object of mode in out,
10389 -- the type of the actual shall resolve to a specific anonymous
10390 -- access type.
10391
10392 if Ada_Version < Ada_2005
10393 or else Ekind (Base_Type (Ftyp)) /=
10394 E_Anonymous_Access_Type
10395 or else Ekind (Base_Type (Etype (Actual))) /=
10396 E_Anonymous_Access_Type
10397 then
10398 Error_Msg_NE
10399 ("type of actual does not match type of&", Actual, Gen_Obj);
10400 end if;
10401 end if;
10402
10403 Note_Possible_Modification (Actual, Sure => True);
10404
10405 -- Check for instantiation of atomic/volatile actual for
10406 -- non-atomic/volatile formal (RM C.6 (12)).
10407
10408 if Is_Atomic_Object (Actual) and then not Is_Atomic (Orig_Ftyp) then
10409 Error_Msg_N
10410 ("cannot instantiate non-atomic formal object "
10411 & "with atomic actual", Actual);
10412
10413 elsif Is_Volatile_Object (Actual) and then not Is_Volatile (Orig_Ftyp)
10414 then
10415 Error_Msg_N
10416 ("cannot instantiate non-volatile formal object "
10417 & "with volatile actual", Actual);
10418 end if;
10419
10420 -- Formal in-parameter
10421
10422 else
10423 -- The instantiation of a generic formal in-parameter is constant
10424 -- declaration. The actual is the expression for that declaration.
10425 -- Its type is a full copy of the type of the formal. This may be
10426 -- an access to subprogram, for which we need to generate entities
10427 -- for the formals in the new signature.
10428
10429 if Present (Actual) then
10430 if Present (Subt_Mark) then
10431 Def := New_Copy_Tree (Subt_Mark);
10432 else pragma Assert (Present (Acc_Def));
10433 Def := Copy_Access_Def;
10434 end if;
10435
10436 Decl_Node :=
10437 Make_Object_Declaration (Loc,
10438 Defining_Identifier => New_Copy (Gen_Obj),
10439 Constant_Present => True,
10440 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
10441 Object_Definition => Def,
10442 Expression => Actual);
10443
10444 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
10445
10446 -- A generic formal object of a tagged type is defined to be
10447 -- aliased so the new constant must also be treated as aliased.
10448
10449 if Is_Tagged_Type (Etype (A_Gen_Obj)) then
10450 Set_Aliased_Present (Decl_Node);
10451 end if;
10452
10453 Append (Decl_Node, List);
10454
10455 -- No need to repeat (pre-)analysis of some expression nodes
10456 -- already handled in Preanalyze_Actuals.
10457
10458 if Nkind (Actual) /= N_Allocator then
10459 Analyze (Actual);
10460
10461 -- Return if the analysis of the actual reported some error
10462
10463 if Etype (Actual) = Any_Type then
10464 return List;
10465 end if;
10466 end if;
10467
10468 declare
10469 Formal_Type : constant Entity_Id := Etype (A_Gen_Obj);
10470 Typ : Entity_Id;
10471
10472 begin
10473 Typ := Get_Instance_Of (Formal_Type);
10474
10475 -- If the actual appears in the current or an enclosing scope,
10476 -- use its type directly. This is relevant if it has an actual
10477 -- subtype that is distinct from its nominal one. This cannot
10478 -- be done in general because the type of the actual may
10479 -- depend on other actuals, and only be fully determined when
10480 -- the enclosing instance is analyzed.
10481
10482 if Present (Etype (Actual))
10483 and then Is_Constr_Subt_For_U_Nominal (Etype (Actual))
10484 then
10485 Freeze_Before (Instantiation_Node, Etype (Actual));
10486 else
10487 Freeze_Before (Instantiation_Node, Typ);
10488 end if;
10489
10490 -- If the actual is an aggregate, perform name resolution on
10491 -- its components (the analysis of an aggregate does not do it)
10492 -- to capture local names that may be hidden if the generic is
10493 -- a child unit.
10494
10495 if Nkind (Actual) = N_Aggregate then
10496 Preanalyze_And_Resolve (Actual, Typ);
10497 end if;
10498
10499 if Is_Limited_Type (Typ)
10500 and then not OK_For_Limited_Init (Typ, Actual)
10501 then
10502 Error_Msg_N
10503 ("initialization not allowed for limited types", Actual);
10504 Explain_Limited_Type (Typ, Actual);
10505 end if;
10506 end;
10507
10508 elsif Present (Default_Expression (Formal)) then
10509
10510 -- Use default to construct declaration
10511
10512 if Present (Subt_Mark) then
10513 Def := New_Copy (Subt_Mark);
10514 else pragma Assert (Present (Acc_Def));
10515 Def := Copy_Access_Def;
10516 end if;
10517
10518 Decl_Node :=
10519 Make_Object_Declaration (Sloc (Formal),
10520 Defining_Identifier => New_Copy (Gen_Obj),
10521 Constant_Present => True,
10522 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
10523 Object_Definition => Def,
10524 Expression => New_Copy_Tree
10525 (Default_Expression (Formal)));
10526
10527 Append (Decl_Node, List);
10528 Set_Analyzed (Expression (Decl_Node), False);
10529
10530 else
10531 Error_Msg_NE ("missing actual&", Instantiation_Node, Gen_Obj);
10532 Error_Msg_NE ("\in instantiation of & declared#",
10533 Instantiation_Node, Scope (A_Gen_Obj));
10534
10535 if Is_Scalar_Type (Etype (A_Gen_Obj)) then
10536
10537 -- Create dummy constant declaration so that instance can be
10538 -- analyzed, to minimize cascaded visibility errors.
10539
10540 if Present (Subt_Mark) then
10541 Def := Subt_Mark;
10542 else pragma Assert (Present (Acc_Def));
10543 Def := Acc_Def;
10544 end if;
10545
10546 Decl_Node :=
10547 Make_Object_Declaration (Loc,
10548 Defining_Identifier => New_Copy (Gen_Obj),
10549 Constant_Present => True,
10550 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
10551 Object_Definition => New_Copy (Def),
10552 Expression =>
10553 Make_Attribute_Reference (Sloc (Gen_Obj),
10554 Attribute_Name => Name_First,
10555 Prefix => New_Copy (Def)));
10556
10557 Append (Decl_Node, List);
10558
10559 else
10560 Abandon_Instantiation (Instantiation_Node);
10561 end if;
10562 end if;
10563 end if;
10564
10565 if Nkind (Actual) in N_Has_Entity then
10566 Actual_Decl := Parent (Entity (Actual));
10567 end if;
10568
10569 -- Ada 2005 (AI-423): For a formal object declaration with a null
10570 -- exclusion or an access definition that has a null exclusion: If the
10571 -- actual matching the formal object declaration denotes a generic
10572 -- formal object of another generic unit G, and the instantiation
10573 -- containing the actual occurs within the body of G or within the body
10574 -- of a generic unit declared within the declarative region of G, then
10575 -- the declaration of the formal object of G must have a null exclusion.
10576 -- Otherwise, the subtype of the actual matching the formal object
10577 -- declaration shall exclude null.
10578
10579 if Ada_Version >= Ada_2005
10580 and then Present (Actual_Decl)
10581 and then Nkind_In (Actual_Decl, N_Formal_Object_Declaration,
10582 N_Object_Declaration)
10583 and then Nkind (Analyzed_Formal) = N_Formal_Object_Declaration
10584 and then not Has_Null_Exclusion (Actual_Decl)
10585 and then Has_Null_Exclusion (Analyzed_Formal)
10586 then
10587 Error_Msg_Sloc := Sloc (Analyzed_Formal);
10588 Error_Msg_N
10589 ("actual must exclude null to match generic formal#", Actual);
10590 end if;
10591
10592 -- An effectively volatile object cannot be used as an actual in
10593 -- a generic instance. The following check is only relevant when
10594 -- SPARK_Mode is on as it is not a standard Ada legality rule.
10595
10596 if SPARK_Mode = On
10597 and then Present (Actual)
10598 and then Is_Effectively_Volatile_Object (Actual)
10599 then
10600 Error_Msg_N
10601 ("volatile object cannot act as actual in generic instantiation "
10602 & "(SPARK RM 7.1.3(8))", Actual);
10603 end if;
10604
10605 return List;
10606 end Instantiate_Object;
10607
10608 ------------------------------
10609 -- Instantiate_Package_Body --
10610 ------------------------------
10611
10612 procedure Instantiate_Package_Body
10613 (Body_Info : Pending_Body_Info;
10614 Inlined_Body : Boolean := False;
10615 Body_Optional : Boolean := False)
10616 is
10617 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
10618 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
10619 Loc : constant Source_Ptr := Sloc (Inst_Node);
10620
10621 Gen_Id : constant Node_Id := Name (Inst_Node);
10622 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
10623 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
10624 Act_Spec : constant Node_Id := Specification (Act_Decl);
10625 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Spec);
10626
10627 Act_Body_Name : Node_Id;
10628 Gen_Body : Node_Id;
10629 Gen_Body_Id : Node_Id;
10630 Act_Body : Node_Id;
10631 Act_Body_Id : Entity_Id;
10632
10633 Parent_Installed : Boolean := False;
10634 Save_Style_Check : constant Boolean := Style_Check;
10635
10636 Par_Ent : Entity_Id := Empty;
10637 Par_Vis : Boolean := False;
10638
10639 Vis_Prims_List : Elist_Id := No_Elist;
10640 -- List of primitives made temporarily visible in the instantiation
10641 -- to match the visibility of the formal type
10642
10643 procedure Check_Initialized_Types;
10644 -- In a generic package body, an entity of a generic private type may
10645 -- appear uninitialized. This is suspicious, unless the actual is a
10646 -- fully initialized type.
10647
10648 -----------------------------
10649 -- Check_Initialized_Types --
10650 -----------------------------
10651
10652 procedure Check_Initialized_Types is
10653 Decl : Node_Id;
10654 Formal : Entity_Id;
10655 Actual : Entity_Id;
10656 Uninit_Var : Entity_Id;
10657
10658 begin
10659 Decl := First (Generic_Formal_Declarations (Gen_Decl));
10660 while Present (Decl) loop
10661 Uninit_Var := Empty;
10662
10663 if Nkind (Decl) = N_Private_Extension_Declaration then
10664 Uninit_Var := Uninitialized_Variable (Decl);
10665
10666 elsif Nkind (Decl) = N_Formal_Type_Declaration
10667 and then Nkind (Formal_Type_Definition (Decl)) =
10668 N_Formal_Private_Type_Definition
10669 then
10670 Uninit_Var :=
10671 Uninitialized_Variable (Formal_Type_Definition (Decl));
10672 end if;
10673
10674 if Present (Uninit_Var) then
10675 Formal := Defining_Identifier (Decl);
10676 Actual := First_Entity (Act_Decl_Id);
10677
10678 -- For each formal there is a subtype declaration that renames
10679 -- the actual and has the same name as the formal. Locate the
10680 -- formal for warning message about uninitialized variables
10681 -- in the generic, for which the actual type should be a fully
10682 -- initialized type.
10683
10684 while Present (Actual) loop
10685 exit when Ekind (Actual) = E_Package
10686 and then Present (Renamed_Object (Actual));
10687
10688 if Chars (Actual) = Chars (Formal)
10689 and then not Is_Scalar_Type (Actual)
10690 and then not Is_Fully_Initialized_Type (Actual)
10691 and then Warn_On_No_Value_Assigned
10692 then
10693 Error_Msg_Node_2 := Formal;
10694 Error_Msg_NE
10695 ("generic unit has uninitialized variable& of "
10696 & "formal private type &?v?", Actual, Uninit_Var);
10697 Error_Msg_NE
10698 ("actual type for& should be fully initialized type?v?",
10699 Actual, Formal);
10700 exit;
10701 end if;
10702
10703 Next_Entity (Actual);
10704 end loop;
10705 end if;
10706
10707 Next (Decl);
10708 end loop;
10709 end Check_Initialized_Types;
10710
10711 -- Start of processing for Instantiate_Package_Body
10712
10713 begin
10714 Gen_Body_Id := Corresponding_Body (Gen_Decl);
10715
10716 -- The instance body may already have been processed, as the parent of
10717 -- another instance that is inlined (Load_Parent_Of_Generic).
10718
10719 if Present (Corresponding_Body (Instance_Spec (Inst_Node))) then
10720 return;
10721 end if;
10722
10723 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
10724
10725 -- Re-establish the state of information on which checks are suppressed.
10726 -- This information was set in Body_Info at the point of instantiation,
10727 -- and now we restore it so that the instance is compiled using the
10728 -- check status at the instantiation (RM 11.5(7.2/2), AI95-00224-01).
10729
10730 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
10731 Scope_Suppress := Body_Info.Scope_Suppress;
10732 Opt.Ada_Version := Body_Info.Version;
10733 Opt.Ada_Version_Pragma := Body_Info.Version_Pragma;
10734 Restore_Warnings (Body_Info.Warnings);
10735 Opt.SPARK_Mode := Body_Info.SPARK_Mode;
10736 Opt.SPARK_Mode_Pragma := Body_Info.SPARK_Mode_Pragma;
10737
10738 if No (Gen_Body_Id) then
10739
10740 -- Do not look for parent of generic body if none is required.
10741 -- This may happen when the routine is called as part of the
10742 -- Pending_Instantiations processing, when nested instances
10743 -- may precede the one generated from the main unit.
10744
10745 if not Unit_Requires_Body (Defining_Entity (Gen_Decl))
10746 and then Body_Optional
10747 then
10748 return;
10749 else
10750 Load_Parent_Of_Generic
10751 (Inst_Node, Specification (Gen_Decl), Body_Optional);
10752 Gen_Body_Id := Corresponding_Body (Gen_Decl);
10753 end if;
10754 end if;
10755
10756 -- Establish global variable for sloc adjustment and for error recovery
10757 -- In the case of an instance body for an instantiation with actuals
10758 -- from a limited view, the instance body is placed at the beginning
10759 -- of the enclosing package body: use the body entity as the source
10760 -- location for nodes of the instance body.
10761
10762 if not Is_Empty_Elmt_List (Incomplete_Actuals (Act_Decl_Id)) then
10763 declare
10764 Scop : constant Entity_Id := Scope (Act_Decl_Id);
10765 Body_Id : constant Node_Id :=
10766 Corresponding_Body (Unit_Declaration_Node (Scop));
10767
10768 begin
10769 Instantiation_Node := Body_Id;
10770 end;
10771 else
10772 Instantiation_Node := Inst_Node;
10773 end if;
10774
10775 if Present (Gen_Body_Id) then
10776 Save_Env (Gen_Unit, Act_Decl_Id);
10777 Style_Check := False;
10778 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
10779
10780 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
10781
10782 Create_Instantiation_Source
10783 (Inst_Node, Gen_Body_Id, False, S_Adjustment);
10784
10785 Act_Body :=
10786 Copy_Generic_Node
10787 (Original_Node (Gen_Body), Empty, Instantiating => True);
10788
10789 -- Build new name (possibly qualified) for body declaration
10790
10791 Act_Body_Id := New_Copy (Act_Decl_Id);
10792
10793 -- Some attributes of spec entity are not inherited by body entity
10794
10795 Set_Handler_Records (Act_Body_Id, No_List);
10796
10797 if Nkind (Defining_Unit_Name (Act_Spec)) =
10798 N_Defining_Program_Unit_Name
10799 then
10800 Act_Body_Name :=
10801 Make_Defining_Program_Unit_Name (Loc,
10802 Name => New_Copy_Tree (Name (Defining_Unit_Name (Act_Spec))),
10803 Defining_Identifier => Act_Body_Id);
10804 else
10805 Act_Body_Name := Act_Body_Id;
10806 end if;
10807
10808 Set_Defining_Unit_Name (Act_Body, Act_Body_Name);
10809
10810 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
10811 Check_Generic_Actuals (Act_Decl_Id, False);
10812 Check_Initialized_Types;
10813
10814 -- Install primitives hidden at the point of the instantiation but
10815 -- visible when processing the generic formals
10816
10817 declare
10818 E : Entity_Id;
10819
10820 begin
10821 E := First_Entity (Act_Decl_Id);
10822 while Present (E) loop
10823 if Is_Type (E)
10824 and then Is_Generic_Actual_Type (E)
10825 and then Is_Tagged_Type (E)
10826 then
10827 Install_Hidden_Primitives
10828 (Prims_List => Vis_Prims_List,
10829 Gen_T => Generic_Parent_Type (Parent (E)),
10830 Act_T => E);
10831 end if;
10832
10833 Next_Entity (E);
10834 end loop;
10835 end;
10836
10837 -- If it is a child unit, make the parent instance (which is an
10838 -- instance of the parent of the generic) visible. The parent
10839 -- instance is the prefix of the name of the generic unit.
10840
10841 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
10842 and then Nkind (Gen_Id) = N_Expanded_Name
10843 then
10844 Par_Ent := Entity (Prefix (Gen_Id));
10845 Par_Vis := Is_Immediately_Visible (Par_Ent);
10846 Install_Parent (Par_Ent, In_Body => True);
10847 Parent_Installed := True;
10848
10849 elsif Is_Child_Unit (Gen_Unit) then
10850 Par_Ent := Scope (Gen_Unit);
10851 Par_Vis := Is_Immediately_Visible (Par_Ent);
10852 Install_Parent (Par_Ent, In_Body => True);
10853 Parent_Installed := True;
10854 end if;
10855
10856 -- If the instantiation is a library unit, and this is the main unit,
10857 -- then build the resulting compilation unit nodes for the instance.
10858 -- If this is a compilation unit but it is not the main unit, then it
10859 -- is the body of a unit in the context, that is being compiled
10860 -- because it is encloses some inlined unit or another generic unit
10861 -- being instantiated. In that case, this body is not part of the
10862 -- current compilation, and is not attached to the tree, but its
10863 -- parent must be set for analysis.
10864
10865 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
10866
10867 -- Replace instance node with body of instance, and create new
10868 -- node for corresponding instance declaration.
10869
10870 Build_Instance_Compilation_Unit_Nodes
10871 (Inst_Node, Act_Body, Act_Decl);
10872 Analyze (Inst_Node);
10873
10874 if Parent (Inst_Node) = Cunit (Main_Unit) then
10875
10876 -- If the instance is a child unit itself, then set the scope
10877 -- of the expanded body to be the parent of the instantiation
10878 -- (ensuring that the fully qualified name will be generated
10879 -- for the elaboration subprogram).
10880
10881 if Nkind (Defining_Unit_Name (Act_Spec)) =
10882 N_Defining_Program_Unit_Name
10883 then
10884 Set_Scope (Defining_Entity (Inst_Node), Scope (Act_Decl_Id));
10885 end if;
10886 end if;
10887
10888 -- Case where instantiation is not a library unit
10889
10890 else
10891 -- If this is an early instantiation, i.e. appears textually
10892 -- before the corresponding body and must be elaborated first,
10893 -- indicate that the body instance is to be delayed.
10894
10895 Install_Body (Act_Body, Inst_Node, Gen_Body, Gen_Decl);
10896
10897 -- Now analyze the body. We turn off all checks if this is an
10898 -- internal unit, since there is no reason to have checks on for
10899 -- any predefined run-time library code. All such code is designed
10900 -- to be compiled with checks off.
10901
10902 -- Note that we do NOT apply this criterion to children of GNAT
10903 -- The latter units must suppress checks explicitly if needed.
10904
10905 if Is_Predefined_File_Name
10906 (Unit_File_Name (Get_Source_Unit (Gen_Decl)))
10907 then
10908 Analyze (Act_Body, Suppress => All_Checks);
10909 else
10910 Analyze (Act_Body);
10911 end if;
10912 end if;
10913
10914 Inherit_Context (Gen_Body, Inst_Node);
10915
10916 -- Remove the parent instances if they have been placed on the scope
10917 -- stack to compile the body.
10918
10919 if Parent_Installed then
10920 Remove_Parent (In_Body => True);
10921
10922 -- Restore the previous visibility of the parent
10923
10924 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
10925 end if;
10926
10927 Restore_Hidden_Primitives (Vis_Prims_List);
10928 Restore_Private_Views (Act_Decl_Id);
10929
10930 -- Remove the current unit from visibility if this is an instance
10931 -- that is not elaborated on the fly for inlining purposes.
10932
10933 if not Inlined_Body then
10934 Set_Is_Immediately_Visible (Act_Decl_Id, False);
10935 end if;
10936
10937 Restore_Env;
10938 Style_Check := Save_Style_Check;
10939
10940 -- If we have no body, and the unit requires a body, then complain. This
10941 -- complaint is suppressed if we have detected other errors (since a
10942 -- common reason for missing the body is that it had errors).
10943 -- In CodePeer mode, a warning has been emitted already, no need for
10944 -- further messages.
10945
10946 elsif Unit_Requires_Body (Gen_Unit)
10947 and then not Body_Optional
10948 then
10949 if CodePeer_Mode then
10950 null;
10951
10952 elsif Serious_Errors_Detected = 0 then
10953 Error_Msg_NE
10954 ("cannot find body of generic package &", Inst_Node, Gen_Unit);
10955
10956 -- Don't attempt to perform any cleanup actions if some other error
10957 -- was already detected, since this can cause blowups.
10958
10959 else
10960 return;
10961 end if;
10962
10963 -- Case of package that does not need a body
10964
10965 else
10966 -- If the instantiation of the declaration is a library unit, rewrite
10967 -- the original package instantiation as a package declaration in the
10968 -- compilation unit node.
10969
10970 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
10971 Set_Parent_Spec (Act_Decl, Parent_Spec (Inst_Node));
10972 Rewrite (Inst_Node, Act_Decl);
10973
10974 -- Generate elaboration entity, in case spec has elaboration code.
10975 -- This cannot be done when the instance is analyzed, because it
10976 -- is not known yet whether the body exists.
10977
10978 Set_Elaboration_Entity_Required (Act_Decl_Id, False);
10979 Build_Elaboration_Entity (Parent (Inst_Node), Act_Decl_Id);
10980
10981 -- If the instantiation is not a library unit, then append the
10982 -- declaration to the list of implicitly generated entities, unless
10983 -- it is already a list member which means that it was already
10984 -- processed
10985
10986 elsif not Is_List_Member (Act_Decl) then
10987 Mark_Rewrite_Insertion (Act_Decl);
10988 Insert_Before (Inst_Node, Act_Decl);
10989 end if;
10990 end if;
10991
10992 Expander_Mode_Restore;
10993 end Instantiate_Package_Body;
10994
10995 ---------------------------------
10996 -- Instantiate_Subprogram_Body --
10997 ---------------------------------
10998
10999 procedure Instantiate_Subprogram_Body
11000 (Body_Info : Pending_Body_Info;
11001 Body_Optional : Boolean := False)
11002 is
11003 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
11004 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
11005 Loc : constant Source_Ptr := Sloc (Inst_Node);
11006 Gen_Id : constant Node_Id := Name (Inst_Node);
11007 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
11008 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
11009 Anon_Id : constant Entity_Id :=
11010 Defining_Unit_Name (Specification (Act_Decl));
11011 Pack_Id : constant Entity_Id :=
11012 Defining_Unit_Name (Parent (Act_Decl));
11013
11014 Saved_Style_Check : constant Boolean := Style_Check;
11015 Saved_Warnings : constant Warning_Record := Save_Warnings;
11016
11017 Act_Body : Node_Id;
11018 Gen_Body : Node_Id;
11019 Gen_Body_Id : Node_Id;
11020 Pack_Body : Node_Id;
11021 Par_Ent : Entity_Id := Empty;
11022 Par_Vis : Boolean := False;
11023 Ret_Expr : Node_Id;
11024
11025 Parent_Installed : Boolean := False;
11026
11027 begin
11028 Gen_Body_Id := Corresponding_Body (Gen_Decl);
11029
11030 -- Subprogram body may have been created already because of an inline
11031 -- pragma, or because of multiple elaborations of the enclosing package
11032 -- when several instances of the subprogram appear in the main unit.
11033
11034 if Present (Corresponding_Body (Act_Decl)) then
11035 return;
11036 end if;
11037
11038 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
11039
11040 -- Re-establish the state of information on which checks are suppressed.
11041 -- This information was set in Body_Info at the point of instantiation,
11042 -- and now we restore it so that the instance is compiled using the
11043 -- check status at the instantiation (RM 11.5(7.2/2), AI95-00224-01).
11044
11045 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
11046 Scope_Suppress := Body_Info.Scope_Suppress;
11047 Opt.Ada_Version := Body_Info.Version;
11048 Opt.Ada_Version_Pragma := Body_Info.Version_Pragma;
11049 Restore_Warnings (Body_Info.Warnings);
11050 Opt.SPARK_Mode := Body_Info.SPARK_Mode;
11051 Opt.SPARK_Mode_Pragma := Body_Info.SPARK_Mode_Pragma;
11052
11053 if No (Gen_Body_Id) then
11054
11055 -- For imported generic subprogram, no body to compile, complete
11056 -- the spec entity appropriately.
11057
11058 if Is_Imported (Gen_Unit) then
11059 Set_Is_Imported (Anon_Id);
11060 Set_First_Rep_Item (Anon_Id, First_Rep_Item (Gen_Unit));
11061 Set_Interface_Name (Anon_Id, Interface_Name (Gen_Unit));
11062 Set_Convention (Anon_Id, Convention (Gen_Unit));
11063 Set_Has_Completion (Anon_Id);
11064 return;
11065
11066 -- For other cases, compile the body
11067
11068 else
11069 Load_Parent_Of_Generic
11070 (Inst_Node, Specification (Gen_Decl), Body_Optional);
11071 Gen_Body_Id := Corresponding_Body (Gen_Decl);
11072 end if;
11073 end if;
11074
11075 Instantiation_Node := Inst_Node;
11076
11077 if Present (Gen_Body_Id) then
11078 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
11079
11080 if Nkind (Gen_Body) = N_Subprogram_Body_Stub then
11081
11082 -- Either body is not present, or context is non-expanding, as
11083 -- when compiling a subunit. Mark the instance as completed, and
11084 -- diagnose a missing body when needed.
11085
11086 if Expander_Active
11087 and then Operating_Mode = Generate_Code
11088 then
11089 Error_Msg_N
11090 ("missing proper body for instantiation", Gen_Body);
11091 end if;
11092
11093 Set_Has_Completion (Anon_Id);
11094 return;
11095 end if;
11096
11097 Save_Env (Gen_Unit, Anon_Id);
11098 Style_Check := False;
11099 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
11100 Create_Instantiation_Source
11101 (Inst_Node,
11102 Gen_Body_Id,
11103 False,
11104 S_Adjustment);
11105
11106 Act_Body :=
11107 Copy_Generic_Node
11108 (Original_Node (Gen_Body), Empty, Instantiating => True);
11109
11110 -- Create proper defining name for the body, to correspond to
11111 -- the one in the spec.
11112
11113 Set_Defining_Unit_Name (Specification (Act_Body),
11114 Make_Defining_Identifier
11115 (Sloc (Defining_Entity (Inst_Node)), Chars (Anon_Id)));
11116 Set_Corresponding_Spec (Act_Body, Anon_Id);
11117 Set_Has_Completion (Anon_Id);
11118 Check_Generic_Actuals (Pack_Id, False);
11119
11120 -- Generate a reference to link the visible subprogram instance to
11121 -- the generic body, which for navigation purposes is the only
11122 -- available source for the instance.
11123
11124 Generate_Reference
11125 (Related_Instance (Pack_Id),
11126 Gen_Body_Id, 'b', Set_Ref => False, Force => True);
11127
11128 -- If it is a child unit, make the parent instance (which is an
11129 -- instance of the parent of the generic) visible. The parent
11130 -- instance is the prefix of the name of the generic unit.
11131
11132 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
11133 and then Nkind (Gen_Id) = N_Expanded_Name
11134 then
11135 Par_Ent := Entity (Prefix (Gen_Id));
11136 Par_Vis := Is_Immediately_Visible (Par_Ent);
11137 Install_Parent (Par_Ent, In_Body => True);
11138 Parent_Installed := True;
11139
11140 elsif Is_Child_Unit (Gen_Unit) then
11141 Par_Ent := Scope (Gen_Unit);
11142 Par_Vis := Is_Immediately_Visible (Par_Ent);
11143 Install_Parent (Par_Ent, In_Body => True);
11144 Parent_Installed := True;
11145 end if;
11146
11147 -- Subprogram body is placed in the body of wrapper package,
11148 -- whose spec contains the subprogram declaration as well as
11149 -- the renaming declarations for the generic parameters.
11150
11151 Pack_Body :=
11152 Make_Package_Body (Loc,
11153 Defining_Unit_Name => New_Copy (Pack_Id),
11154 Declarations => New_List (Act_Body));
11155
11156 Set_Corresponding_Spec (Pack_Body, Pack_Id);
11157
11158 -- If the instantiation is a library unit, then build resulting
11159 -- compilation unit nodes for the instance. The declaration of
11160 -- the enclosing package is the grandparent of the subprogram
11161 -- declaration. First replace the instantiation node as the unit
11162 -- of the corresponding compilation.
11163
11164 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
11165 if Parent (Inst_Node) = Cunit (Main_Unit) then
11166 Set_Unit (Parent (Inst_Node), Inst_Node);
11167 Build_Instance_Compilation_Unit_Nodes
11168 (Inst_Node, Pack_Body, Parent (Parent (Act_Decl)));
11169 Analyze (Inst_Node);
11170 else
11171 Set_Parent (Pack_Body, Parent (Inst_Node));
11172 Analyze (Pack_Body);
11173 end if;
11174
11175 else
11176 Insert_Before (Inst_Node, Pack_Body);
11177 Mark_Rewrite_Insertion (Pack_Body);
11178 Analyze (Pack_Body);
11179
11180 if Expander_Active then
11181 Freeze_Subprogram_Body (Inst_Node, Gen_Body, Pack_Id);
11182 end if;
11183 end if;
11184
11185 Inherit_Context (Gen_Body, Inst_Node);
11186
11187 Restore_Private_Views (Pack_Id, False);
11188
11189 if Parent_Installed then
11190 Remove_Parent (In_Body => True);
11191
11192 -- Restore the previous visibility of the parent
11193
11194 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
11195 end if;
11196
11197 Restore_Env;
11198 Style_Check := Saved_Style_Check;
11199 Restore_Warnings (Saved_Warnings);
11200
11201 -- Body not found. Error was emitted already. If there were no previous
11202 -- errors, this may be an instance whose scope is a premature instance.
11203 -- In that case we must insure that the (legal) program does raise
11204 -- program error if executed. We generate a subprogram body for this
11205 -- purpose. See DEC ac30vso.
11206
11207 -- Should not reference proprietary DEC tests in comments ???
11208
11209 elsif Serious_Errors_Detected = 0
11210 and then Nkind (Parent (Inst_Node)) /= N_Compilation_Unit
11211 then
11212 if Body_Optional then
11213 return;
11214
11215 elsif Ekind (Anon_Id) = E_Procedure then
11216 Act_Body :=
11217 Make_Subprogram_Body (Loc,
11218 Specification =>
11219 Make_Procedure_Specification (Loc,
11220 Defining_Unit_Name =>
11221 Make_Defining_Identifier (Loc, Chars (Anon_Id)),
11222 Parameter_Specifications =>
11223 New_Copy_List
11224 (Parameter_Specifications (Parent (Anon_Id)))),
11225
11226 Declarations => Empty_List,
11227 Handled_Statement_Sequence =>
11228 Make_Handled_Sequence_Of_Statements (Loc,
11229 Statements =>
11230 New_List (
11231 Make_Raise_Program_Error (Loc,
11232 Reason =>
11233 PE_Access_Before_Elaboration))));
11234
11235 else
11236 Ret_Expr :=
11237 Make_Raise_Program_Error (Loc,
11238 Reason => PE_Access_Before_Elaboration);
11239
11240 Set_Etype (Ret_Expr, (Etype (Anon_Id)));
11241 Set_Analyzed (Ret_Expr);
11242
11243 Act_Body :=
11244 Make_Subprogram_Body (Loc,
11245 Specification =>
11246 Make_Function_Specification (Loc,
11247 Defining_Unit_Name =>
11248 Make_Defining_Identifier (Loc, Chars (Anon_Id)),
11249 Parameter_Specifications =>
11250 New_Copy_List
11251 (Parameter_Specifications (Parent (Anon_Id))),
11252 Result_Definition =>
11253 New_Occurrence_Of (Etype (Anon_Id), Loc)),
11254
11255 Declarations => Empty_List,
11256 Handled_Statement_Sequence =>
11257 Make_Handled_Sequence_Of_Statements (Loc,
11258 Statements =>
11259 New_List
11260 (Make_Simple_Return_Statement (Loc, Ret_Expr))));
11261 end if;
11262
11263 Pack_Body := Make_Package_Body (Loc,
11264 Defining_Unit_Name => New_Copy (Pack_Id),
11265 Declarations => New_List (Act_Body));
11266
11267 Insert_After (Inst_Node, Pack_Body);
11268 Set_Corresponding_Spec (Pack_Body, Pack_Id);
11269 Analyze (Pack_Body);
11270 end if;
11271
11272 Expander_Mode_Restore;
11273 end Instantiate_Subprogram_Body;
11274
11275 ----------------------
11276 -- Instantiate_Type --
11277 ----------------------
11278
11279 function Instantiate_Type
11280 (Formal : Node_Id;
11281 Actual : Node_Id;
11282 Analyzed_Formal : Node_Id;
11283 Actual_Decls : List_Id) return List_Id
11284 is
11285 Gen_T : constant Entity_Id := Defining_Identifier (Formal);
11286 A_Gen_T : constant Entity_Id :=
11287 Defining_Identifier (Analyzed_Formal);
11288 Ancestor : Entity_Id := Empty;
11289 Def : constant Node_Id := Formal_Type_Definition (Formal);
11290 Act_T : Entity_Id;
11291 Decl_Node : Node_Id;
11292 Decl_Nodes : List_Id;
11293 Loc : Source_Ptr;
11294 Subt : Entity_Id;
11295
11296 procedure Diagnose_Predicated_Actual;
11297 -- There are a number of constructs in which a discrete type with
11298 -- predicates is illegal, e.g. as an index in an array type declaration.
11299 -- If a generic type is used is such a construct in a generic package
11300 -- declaration, it carries the flag No_Predicate_On_Actual. it is part
11301 -- of the generic contract that the actual cannot have predicates.
11302
11303 procedure Validate_Array_Type_Instance;
11304 procedure Validate_Access_Subprogram_Instance;
11305 procedure Validate_Access_Type_Instance;
11306 procedure Validate_Derived_Type_Instance;
11307 procedure Validate_Derived_Interface_Type_Instance;
11308 procedure Validate_Discriminated_Formal_Type;
11309 procedure Validate_Interface_Type_Instance;
11310 procedure Validate_Private_Type_Instance;
11311 procedure Validate_Incomplete_Type_Instance;
11312 -- These procedures perform validation tests for the named case.
11313 -- Validate_Discriminated_Formal_Type is shared by formal private
11314 -- types and Ada 2012 formal incomplete types.
11315
11316 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean;
11317 -- Check that base types are the same and that the subtypes match
11318 -- statically. Used in several of the above.
11319
11320 ---------------------------------
11321 -- Diagnose_Predicated_Actual --
11322 ---------------------------------
11323
11324 procedure Diagnose_Predicated_Actual is
11325 begin
11326 if No_Predicate_On_Actual (A_Gen_T)
11327 and then Has_Predicates (Act_T)
11328 then
11329 Error_Msg_NE
11330 ("actual for& cannot be a type with predicate",
11331 Instantiation_Node, A_Gen_T);
11332
11333 elsif No_Dynamic_Predicate_On_Actual (A_Gen_T)
11334 and then Has_Predicates (Act_T)
11335 and then not Has_Static_Predicate_Aspect (Act_T)
11336 then
11337 Error_Msg_NE
11338 ("actual for& cannot be a type with a dynamic predicate",
11339 Instantiation_Node, A_Gen_T);
11340 end if;
11341 end Diagnose_Predicated_Actual;
11342
11343 --------------------
11344 -- Subtypes_Match --
11345 --------------------
11346
11347 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean is
11348 T : constant Entity_Id := Get_Instance_Of (Gen_T);
11349
11350 begin
11351 -- Some detailed comments would be useful here ???
11352
11353 return ((Base_Type (T) = Act_T
11354 or else Base_Type (T) = Base_Type (Act_T))
11355 and then Subtypes_Statically_Match (T, Act_T))
11356
11357 or else (Is_Class_Wide_Type (Gen_T)
11358 and then Is_Class_Wide_Type (Act_T)
11359 and then Subtypes_Match
11360 (Get_Instance_Of (Root_Type (Gen_T)),
11361 Root_Type (Act_T)))
11362
11363 or else
11364 (Ekind_In (Gen_T, E_Anonymous_Access_Subprogram_Type,
11365 E_Anonymous_Access_Type)
11366 and then Ekind (Act_T) = Ekind (Gen_T)
11367 and then Subtypes_Statically_Match
11368 (Designated_Type (Gen_T), Designated_Type (Act_T)));
11369 end Subtypes_Match;
11370
11371 -----------------------------------------
11372 -- Validate_Access_Subprogram_Instance --
11373 -----------------------------------------
11374
11375 procedure Validate_Access_Subprogram_Instance is
11376 begin
11377 if not Is_Access_Type (Act_T)
11378 or else Ekind (Designated_Type (Act_T)) /= E_Subprogram_Type
11379 then
11380 Error_Msg_NE
11381 ("expect access type in instantiation of &", Actual, Gen_T);
11382 Abandon_Instantiation (Actual);
11383 end if;
11384
11385 -- According to AI05-288, actuals for access_to_subprograms must be
11386 -- subtype conformant with the generic formal. Previous to AI05-288
11387 -- only mode conformance was required.
11388
11389 -- This is a binding interpretation that applies to previous versions
11390 -- of the language, no need to maintain previous weaker checks.
11391
11392 Check_Subtype_Conformant
11393 (Designated_Type (Act_T),
11394 Designated_Type (A_Gen_T),
11395 Actual,
11396 Get_Inst => True);
11397
11398 if Ekind (Base_Type (Act_T)) = E_Access_Protected_Subprogram_Type then
11399 if Ekind (A_Gen_T) = E_Access_Subprogram_Type then
11400 Error_Msg_NE
11401 ("protected access type not allowed for formal &",
11402 Actual, Gen_T);
11403 end if;
11404
11405 elsif Ekind (A_Gen_T) = E_Access_Protected_Subprogram_Type then
11406 Error_Msg_NE
11407 ("expect protected access type for formal &",
11408 Actual, Gen_T);
11409 end if;
11410
11411 -- If the formal has a specified convention (which in most cases
11412 -- will be StdCall) verify that the actual has the same convention.
11413
11414 if Has_Convention_Pragma (A_Gen_T)
11415 and then Convention (A_Gen_T) /= Convention (Act_T)
11416 then
11417 Error_Msg_Name_1 := Get_Convention_Name (Convention (A_Gen_T));
11418 Error_Msg_NE
11419 ("actual for formal & must have convention %", Actual, Gen_T);
11420 end if;
11421 end Validate_Access_Subprogram_Instance;
11422
11423 -----------------------------------
11424 -- Validate_Access_Type_Instance --
11425 -----------------------------------
11426
11427 procedure Validate_Access_Type_Instance is
11428 Desig_Type : constant Entity_Id :=
11429 Find_Actual_Type (Designated_Type (A_Gen_T), A_Gen_T);
11430 Desig_Act : Entity_Id;
11431
11432 begin
11433 if not Is_Access_Type (Act_T) then
11434 Error_Msg_NE
11435 ("expect access type in instantiation of &", Actual, Gen_T);
11436 Abandon_Instantiation (Actual);
11437 end if;
11438
11439 if Is_Access_Constant (A_Gen_T) then
11440 if not Is_Access_Constant (Act_T) then
11441 Error_Msg_N
11442 ("actual type must be access-to-constant type", Actual);
11443 Abandon_Instantiation (Actual);
11444 end if;
11445 else
11446 if Is_Access_Constant (Act_T) then
11447 Error_Msg_N
11448 ("actual type must be access-to-variable type", Actual);
11449 Abandon_Instantiation (Actual);
11450
11451 elsif Ekind (A_Gen_T) = E_General_Access_Type
11452 and then Ekind (Base_Type (Act_T)) /= E_General_Access_Type
11453 then
11454 Error_Msg_N -- CODEFIX
11455 ("actual must be general access type!", Actual);
11456 Error_Msg_NE -- CODEFIX
11457 ("add ALL to }!", Actual, Act_T);
11458 Abandon_Instantiation (Actual);
11459 end if;
11460 end if;
11461
11462 -- The designated subtypes, that is to say the subtypes introduced
11463 -- by an access type declaration (and not by a subtype declaration)
11464 -- must match.
11465
11466 Desig_Act := Designated_Type (Base_Type (Act_T));
11467
11468 -- The designated type may have been introduced through a limited_
11469 -- with clause, in which case retrieve the non-limited view. This
11470 -- applies to incomplete types as well as to class-wide types.
11471
11472 if From_Limited_With (Desig_Act) then
11473 Desig_Act := Available_View (Desig_Act);
11474 end if;
11475
11476 if not Subtypes_Match (Desig_Type, Desig_Act) then
11477 Error_Msg_NE
11478 ("designated type of actual does not match that of formal &",
11479 Actual, Gen_T);
11480
11481 if not Predicates_Match (Desig_Type, Desig_Act) then
11482 Error_Msg_N ("\predicates do not match", Actual);
11483 end if;
11484
11485 Abandon_Instantiation (Actual);
11486
11487 elsif Is_Access_Type (Designated_Type (Act_T))
11488 and then Is_Constrained (Designated_Type (Designated_Type (Act_T)))
11489 /=
11490 Is_Constrained (Designated_Type (Desig_Type))
11491 then
11492 Error_Msg_NE
11493 ("designated type of actual does not match that of formal &",
11494 Actual, Gen_T);
11495
11496 if not Predicates_Match (Desig_Type, Desig_Act) then
11497 Error_Msg_N ("\predicates do not match", Actual);
11498 end if;
11499
11500 Abandon_Instantiation (Actual);
11501 end if;
11502
11503 -- Ada 2005: null-exclusion indicators of the two types must agree
11504
11505 if Can_Never_Be_Null (A_Gen_T) /= Can_Never_Be_Null (Act_T) then
11506 Error_Msg_NE
11507 ("non null exclusion of actual and formal & do not match",
11508 Actual, Gen_T);
11509 end if;
11510 end Validate_Access_Type_Instance;
11511
11512 ----------------------------------
11513 -- Validate_Array_Type_Instance --
11514 ----------------------------------
11515
11516 procedure Validate_Array_Type_Instance is
11517 I1 : Node_Id;
11518 I2 : Node_Id;
11519 T2 : Entity_Id;
11520
11521 function Formal_Dimensions return Int;
11522 -- Count number of dimensions in array type formal
11523
11524 -----------------------
11525 -- Formal_Dimensions --
11526 -----------------------
11527
11528 function Formal_Dimensions return Int is
11529 Num : Int := 0;
11530 Index : Node_Id;
11531
11532 begin
11533 if Nkind (Def) = N_Constrained_Array_Definition then
11534 Index := First (Discrete_Subtype_Definitions (Def));
11535 else
11536 Index := First (Subtype_Marks (Def));
11537 end if;
11538
11539 while Present (Index) loop
11540 Num := Num + 1;
11541 Next_Index (Index);
11542 end loop;
11543
11544 return Num;
11545 end Formal_Dimensions;
11546
11547 -- Start of processing for Validate_Array_Type_Instance
11548
11549 begin
11550 if not Is_Array_Type (Act_T) then
11551 Error_Msg_NE
11552 ("expect array type in instantiation of &", Actual, Gen_T);
11553 Abandon_Instantiation (Actual);
11554
11555 elsif Nkind (Def) = N_Constrained_Array_Definition then
11556 if not (Is_Constrained (Act_T)) then
11557 Error_Msg_NE
11558 ("expect constrained array in instantiation of &",
11559 Actual, Gen_T);
11560 Abandon_Instantiation (Actual);
11561 end if;
11562
11563 else
11564 if Is_Constrained (Act_T) then
11565 Error_Msg_NE
11566 ("expect unconstrained array in instantiation of &",
11567 Actual, Gen_T);
11568 Abandon_Instantiation (Actual);
11569 end if;
11570 end if;
11571
11572 if Formal_Dimensions /= Number_Dimensions (Act_T) then
11573 Error_Msg_NE
11574 ("dimensions of actual do not match formal &", Actual, Gen_T);
11575 Abandon_Instantiation (Actual);
11576 end if;
11577
11578 I1 := First_Index (A_Gen_T);
11579 I2 := First_Index (Act_T);
11580 for J in 1 .. Formal_Dimensions loop
11581
11582 -- If the indexes of the actual were given by a subtype_mark,
11583 -- the index was transformed into a range attribute. Retrieve
11584 -- the original type mark for checking.
11585
11586 if Is_Entity_Name (Original_Node (I2)) then
11587 T2 := Entity (Original_Node (I2));
11588 else
11589 T2 := Etype (I2);
11590 end if;
11591
11592 if not Subtypes_Match
11593 (Find_Actual_Type (Etype (I1), A_Gen_T), T2)
11594 then
11595 Error_Msg_NE
11596 ("index types of actual do not match those of formal &",
11597 Actual, Gen_T);
11598 Abandon_Instantiation (Actual);
11599 end if;
11600
11601 Next_Index (I1);
11602 Next_Index (I2);
11603 end loop;
11604
11605 -- Check matching subtypes. Note that there are complex visibility
11606 -- issues when the generic is a child unit and some aspect of the
11607 -- generic type is declared in a parent unit of the generic. We do
11608 -- the test to handle this special case only after a direct check
11609 -- for static matching has failed. The case where both the component
11610 -- type and the array type are separate formals, and the component
11611 -- type is a private view may also require special checking in
11612 -- Subtypes_Match.
11613
11614 if Subtypes_Match
11615 (Component_Type (A_Gen_T), Component_Type (Act_T))
11616 or else
11617 Subtypes_Match
11618 (Find_Actual_Type (Component_Type (A_Gen_T), A_Gen_T),
11619 Component_Type (Act_T))
11620 then
11621 null;
11622 else
11623 Error_Msg_NE
11624 ("component subtype of actual does not match that of formal &",
11625 Actual, Gen_T);
11626 Abandon_Instantiation (Actual);
11627 end if;
11628
11629 if Has_Aliased_Components (A_Gen_T)
11630 and then not Has_Aliased_Components (Act_T)
11631 then
11632 Error_Msg_NE
11633 ("actual must have aliased components to match formal type &",
11634 Actual, Gen_T);
11635 end if;
11636 end Validate_Array_Type_Instance;
11637
11638 -----------------------------------------------
11639 -- Validate_Derived_Interface_Type_Instance --
11640 -----------------------------------------------
11641
11642 procedure Validate_Derived_Interface_Type_Instance is
11643 Par : constant Entity_Id := Entity (Subtype_Indication (Def));
11644 Elmt : Elmt_Id;
11645
11646 begin
11647 -- First apply interface instance checks
11648
11649 Validate_Interface_Type_Instance;
11650
11651 -- Verify that immediate parent interface is an ancestor of
11652 -- the actual.
11653
11654 if Present (Par)
11655 and then not Interface_Present_In_Ancestor (Act_T, Par)
11656 then
11657 Error_Msg_NE
11658 ("interface actual must include progenitor&", Actual, Par);
11659 end if;
11660
11661 -- Now verify that the actual includes all other ancestors of
11662 -- the formal.
11663
11664 Elmt := First_Elmt (Interfaces (A_Gen_T));
11665 while Present (Elmt) loop
11666 if not Interface_Present_In_Ancestor
11667 (Act_T, Get_Instance_Of (Node (Elmt)))
11668 then
11669 Error_Msg_NE
11670 ("interface actual must include progenitor&",
11671 Actual, Node (Elmt));
11672 end if;
11673
11674 Next_Elmt (Elmt);
11675 end loop;
11676 end Validate_Derived_Interface_Type_Instance;
11677
11678 ------------------------------------
11679 -- Validate_Derived_Type_Instance --
11680 ------------------------------------
11681
11682 procedure Validate_Derived_Type_Instance is
11683 Actual_Discr : Entity_Id;
11684 Ancestor_Discr : Entity_Id;
11685
11686 begin
11687 -- If the parent type in the generic declaration is itself a previous
11688 -- formal type, then it is local to the generic and absent from the
11689 -- analyzed generic definition. In that case the ancestor is the
11690 -- instance of the formal (which must have been instantiated
11691 -- previously), unless the ancestor is itself a formal derived type.
11692 -- In this latter case (which is the subject of Corrigendum 8652/0038
11693 -- (AI-202) the ancestor of the formals is the ancestor of its
11694 -- parent. Otherwise, the analyzed generic carries the parent type.
11695 -- If the parent type is defined in a previous formal package, then
11696 -- the scope of that formal package is that of the generic type
11697 -- itself, and it has already been mapped into the corresponding type
11698 -- in the actual package.
11699
11700 -- Common case: parent type defined outside of the generic
11701
11702 if Is_Entity_Name (Subtype_Mark (Def))
11703 and then Present (Entity (Subtype_Mark (Def)))
11704 then
11705 Ancestor := Get_Instance_Of (Entity (Subtype_Mark (Def)));
11706
11707 -- Check whether parent is defined in a previous formal package
11708
11709 elsif
11710 Scope (Scope (Base_Type (Etype (A_Gen_T)))) = Scope (A_Gen_T)
11711 then
11712 Ancestor :=
11713 Get_Instance_Of (Base_Type (Etype (A_Gen_T)));
11714
11715 -- The type may be a local derivation, or a type extension of a
11716 -- previous formal, or of a formal of a parent package.
11717
11718 elsif Is_Derived_Type (Get_Instance_Of (A_Gen_T))
11719 or else
11720 Ekind (Get_Instance_Of (A_Gen_T)) = E_Record_Type_With_Private
11721 then
11722 -- Check whether the parent is another derived formal type in the
11723 -- same generic unit.
11724
11725 if Etype (A_Gen_T) /= A_Gen_T
11726 and then Is_Generic_Type (Etype (A_Gen_T))
11727 and then Scope (Etype (A_Gen_T)) = Scope (A_Gen_T)
11728 and then Etype (Etype (A_Gen_T)) /= Etype (A_Gen_T)
11729 then
11730 -- Locate ancestor of parent from the subtype declaration
11731 -- created for the actual.
11732
11733 declare
11734 Decl : Node_Id;
11735
11736 begin
11737 Decl := First (Actual_Decls);
11738 while Present (Decl) loop
11739 if Nkind (Decl) = N_Subtype_Declaration
11740 and then Chars (Defining_Identifier (Decl)) =
11741 Chars (Etype (A_Gen_T))
11742 then
11743 Ancestor := Generic_Parent_Type (Decl);
11744 exit;
11745 else
11746 Next (Decl);
11747 end if;
11748 end loop;
11749 end;
11750
11751 pragma Assert (Present (Ancestor));
11752
11753 -- The ancestor itself may be a previous formal that has been
11754 -- instantiated.
11755
11756 Ancestor := Get_Instance_Of (Ancestor);
11757
11758 else
11759 Ancestor :=
11760 Get_Instance_Of (Base_Type (Get_Instance_Of (A_Gen_T)));
11761 end if;
11762
11763 -- Check whether parent is a previous formal of the current generic
11764
11765 elsif Is_Derived_Type (A_Gen_T)
11766 and then Is_Generic_Type (Etype (A_Gen_T))
11767 and then Scope (A_Gen_T) = Scope (Etype (A_Gen_T))
11768 then
11769 Ancestor := Get_Instance_Of (First_Subtype (Etype (A_Gen_T)));
11770
11771 -- An unusual case: the actual is a type declared in a parent unit,
11772 -- but is not a formal type so there is no instance_of for it.
11773 -- Retrieve it by analyzing the record extension.
11774
11775 elsif Is_Child_Unit (Scope (A_Gen_T))
11776 and then In_Open_Scopes (Scope (Act_T))
11777 and then Is_Generic_Instance (Scope (Act_T))
11778 then
11779 Analyze (Subtype_Mark (Def));
11780 Ancestor := Entity (Subtype_Mark (Def));
11781
11782 else
11783 Ancestor := Get_Instance_Of (Etype (Base_Type (A_Gen_T)));
11784 end if;
11785
11786 -- If the formal derived type has pragma Preelaborable_Initialization
11787 -- then the actual type must have preelaborable initialization.
11788
11789 if Known_To_Have_Preelab_Init (A_Gen_T)
11790 and then not Has_Preelaborable_Initialization (Act_T)
11791 then
11792 Error_Msg_NE
11793 ("actual for & must have preelaborable initialization",
11794 Actual, Gen_T);
11795 end if;
11796
11797 -- Ada 2005 (AI-251)
11798
11799 if Ada_Version >= Ada_2005 and then Is_Interface (Ancestor) then
11800 if not Interface_Present_In_Ancestor (Act_T, Ancestor) then
11801 Error_Msg_NE
11802 ("(Ada 2005) expected type implementing & in instantiation",
11803 Actual, Ancestor);
11804 end if;
11805
11806 -- Finally verify that the (instance of) the ancestor is an ancestor
11807 -- of the actual.
11808
11809 elsif not Is_Ancestor (Base_Type (Ancestor), Act_T) then
11810 Error_Msg_NE
11811 ("expect type derived from & in instantiation",
11812 Actual, First_Subtype (Ancestor));
11813 Abandon_Instantiation (Actual);
11814 end if;
11815
11816 -- Ada 2005 (AI-443): Synchronized formal derived type checks. Note
11817 -- that the formal type declaration has been rewritten as a private
11818 -- extension.
11819
11820 if Ada_Version >= Ada_2005
11821 and then Nkind (Parent (A_Gen_T)) = N_Private_Extension_Declaration
11822 and then Synchronized_Present (Parent (A_Gen_T))
11823 then
11824 -- The actual must be a synchronized tagged type
11825
11826 if not Is_Tagged_Type (Act_T) then
11827 Error_Msg_N
11828 ("actual of synchronized type must be tagged", Actual);
11829 Abandon_Instantiation (Actual);
11830
11831 elsif Nkind (Parent (Act_T)) = N_Full_Type_Declaration
11832 and then Nkind (Type_Definition (Parent (Act_T))) =
11833 N_Derived_Type_Definition
11834 and then not Synchronized_Present
11835 (Type_Definition (Parent (Act_T)))
11836 then
11837 Error_Msg_N
11838 ("actual of synchronized type must be synchronized", Actual);
11839 Abandon_Instantiation (Actual);
11840 end if;
11841 end if;
11842
11843 -- Perform atomic/volatile checks (RM C.6(12)). Note that AI05-0218-1
11844 -- removes the second instance of the phrase "or allow pass by copy".
11845
11846 if Is_Atomic (Act_T) and then not Is_Atomic (Ancestor) then
11847 Error_Msg_N
11848 ("cannot have atomic actual type for non-atomic formal type",
11849 Actual);
11850
11851 elsif Is_Volatile (Act_T) and then not Is_Volatile (Ancestor) then
11852 Error_Msg_N
11853 ("cannot have volatile actual type for non-volatile formal type",
11854 Actual);
11855 end if;
11856
11857 -- It should not be necessary to check for unknown discriminants on
11858 -- Formal, but for some reason Has_Unknown_Discriminants is false for
11859 -- A_Gen_T, so Is_Definite_Subtype incorrectly returns True. This
11860 -- needs fixing. ???
11861
11862 if Is_Definite_Subtype (A_Gen_T)
11863 and then not Unknown_Discriminants_Present (Formal)
11864 and then not Is_Definite_Subtype (Act_T)
11865 then
11866 Error_Msg_N ("actual subtype must be constrained", Actual);
11867 Abandon_Instantiation (Actual);
11868 end if;
11869
11870 if not Unknown_Discriminants_Present (Formal) then
11871 if Is_Constrained (Ancestor) then
11872 if not Is_Constrained (Act_T) then
11873 Error_Msg_N ("actual subtype must be constrained", Actual);
11874 Abandon_Instantiation (Actual);
11875 end if;
11876
11877 -- Ancestor is unconstrained, Check if generic formal and actual
11878 -- agree on constrainedness. The check only applies to array types
11879 -- and discriminated types.
11880
11881 elsif Is_Constrained (Act_T) then
11882 if Ekind (Ancestor) = E_Access_Type
11883 or else (not Is_Constrained (A_Gen_T)
11884 and then Is_Composite_Type (A_Gen_T))
11885 then
11886 Error_Msg_N ("actual subtype must be unconstrained", Actual);
11887 Abandon_Instantiation (Actual);
11888 end if;
11889
11890 -- A class-wide type is only allowed if the formal has unknown
11891 -- discriminants.
11892
11893 elsif Is_Class_Wide_Type (Act_T)
11894 and then not Has_Unknown_Discriminants (Ancestor)
11895 then
11896 Error_Msg_NE
11897 ("actual for & cannot be a class-wide type", Actual, Gen_T);
11898 Abandon_Instantiation (Actual);
11899
11900 -- Otherwise, the formal and actual must have the same number
11901 -- of discriminants and each discriminant of the actual must
11902 -- correspond to a discriminant of the formal.
11903
11904 elsif Has_Discriminants (Act_T)
11905 and then not Has_Unknown_Discriminants (Act_T)
11906 and then Has_Discriminants (Ancestor)
11907 then
11908 Actual_Discr := First_Discriminant (Act_T);
11909 Ancestor_Discr := First_Discriminant (Ancestor);
11910 while Present (Actual_Discr)
11911 and then Present (Ancestor_Discr)
11912 loop
11913 if Base_Type (Act_T) /= Base_Type (Ancestor) and then
11914 No (Corresponding_Discriminant (Actual_Discr))
11915 then
11916 Error_Msg_NE
11917 ("discriminant & does not correspond "
11918 & "to ancestor discriminant", Actual, Actual_Discr);
11919 Abandon_Instantiation (Actual);
11920 end if;
11921
11922 Next_Discriminant (Actual_Discr);
11923 Next_Discriminant (Ancestor_Discr);
11924 end loop;
11925
11926 if Present (Actual_Discr) or else Present (Ancestor_Discr) then
11927 Error_Msg_NE
11928 ("actual for & must have same number of discriminants",
11929 Actual, Gen_T);
11930 Abandon_Instantiation (Actual);
11931 end if;
11932
11933 -- This case should be caught by the earlier check for
11934 -- constrainedness, but the check here is added for completeness.
11935
11936 elsif Has_Discriminants (Act_T)
11937 and then not Has_Unknown_Discriminants (Act_T)
11938 then
11939 Error_Msg_NE
11940 ("actual for & must not have discriminants", Actual, Gen_T);
11941 Abandon_Instantiation (Actual);
11942
11943 elsif Has_Discriminants (Ancestor) then
11944 Error_Msg_NE
11945 ("actual for & must have known discriminants", Actual, Gen_T);
11946 Abandon_Instantiation (Actual);
11947 end if;
11948
11949 if not Subtypes_Statically_Compatible
11950 (Act_T, Ancestor, Formal_Derived_Matching => True)
11951 then
11952 Error_Msg_N
11953 ("constraint on actual is incompatible with formal", Actual);
11954 Abandon_Instantiation (Actual);
11955 end if;
11956 end if;
11957
11958 -- If the formal and actual types are abstract, check that there
11959 -- are no abstract primitives of the actual type that correspond to
11960 -- nonabstract primitives of the formal type (second sentence of
11961 -- RM95 3.9.3(9)).
11962
11963 if Is_Abstract_Type (A_Gen_T) and then Is_Abstract_Type (Act_T) then
11964 Check_Abstract_Primitives : declare
11965 Gen_Prims : constant Elist_Id :=
11966 Primitive_Operations (A_Gen_T);
11967 Gen_Elmt : Elmt_Id;
11968 Gen_Subp : Entity_Id;
11969 Anc_Subp : Entity_Id;
11970 Anc_Formal : Entity_Id;
11971 Anc_F_Type : Entity_Id;
11972
11973 Act_Prims : constant Elist_Id := Primitive_Operations (Act_T);
11974 Act_Elmt : Elmt_Id;
11975 Act_Subp : Entity_Id;
11976 Act_Formal : Entity_Id;
11977 Act_F_Type : Entity_Id;
11978
11979 Subprograms_Correspond : Boolean;
11980
11981 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean;
11982 -- Returns true if T2 is derived directly or indirectly from
11983 -- T1, including derivations from interfaces. T1 and T2 are
11984 -- required to be specific tagged base types.
11985
11986 ------------------------
11987 -- Is_Tagged_Ancestor --
11988 ------------------------
11989
11990 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean
11991 is
11992 Intfc_Elmt : Elmt_Id;
11993
11994 begin
11995 -- The predicate is satisfied if the types are the same
11996
11997 if T1 = T2 then
11998 return True;
11999
12000 -- If we've reached the top of the derivation chain then
12001 -- we know that T1 is not an ancestor of T2.
12002
12003 elsif Etype (T2) = T2 then
12004 return False;
12005
12006 -- Proceed to check T2's immediate parent
12007
12008 elsif Is_Ancestor (T1, Base_Type (Etype (T2))) then
12009 return True;
12010
12011 -- Finally, check to see if T1 is an ancestor of any of T2's
12012 -- progenitors.
12013
12014 else
12015 Intfc_Elmt := First_Elmt (Interfaces (T2));
12016 while Present (Intfc_Elmt) loop
12017 if Is_Ancestor (T1, Node (Intfc_Elmt)) then
12018 return True;
12019 end if;
12020
12021 Next_Elmt (Intfc_Elmt);
12022 end loop;
12023 end if;
12024
12025 return False;
12026 end Is_Tagged_Ancestor;
12027
12028 -- Start of processing for Check_Abstract_Primitives
12029
12030 begin
12031 -- Loop over all of the formal derived type's primitives
12032
12033 Gen_Elmt := First_Elmt (Gen_Prims);
12034 while Present (Gen_Elmt) loop
12035 Gen_Subp := Node (Gen_Elmt);
12036
12037 -- If the primitive of the formal is not abstract, then
12038 -- determine whether there is a corresponding primitive of
12039 -- the actual type that's abstract.
12040
12041 if not Is_Abstract_Subprogram (Gen_Subp) then
12042 Act_Elmt := First_Elmt (Act_Prims);
12043 while Present (Act_Elmt) loop
12044 Act_Subp := Node (Act_Elmt);
12045
12046 -- If we find an abstract primitive of the actual,
12047 -- then we need to test whether it corresponds to the
12048 -- subprogram from which the generic formal primitive
12049 -- is inherited.
12050
12051 if Is_Abstract_Subprogram (Act_Subp) then
12052 Anc_Subp := Alias (Gen_Subp);
12053
12054 -- Test whether we have a corresponding primitive
12055 -- by comparing names, kinds, formal types, and
12056 -- result types.
12057
12058 if Chars (Anc_Subp) = Chars (Act_Subp)
12059 and then Ekind (Anc_Subp) = Ekind (Act_Subp)
12060 then
12061 Anc_Formal := First_Formal (Anc_Subp);
12062 Act_Formal := First_Formal (Act_Subp);
12063 while Present (Anc_Formal)
12064 and then Present (Act_Formal)
12065 loop
12066 Anc_F_Type := Etype (Anc_Formal);
12067 Act_F_Type := Etype (Act_Formal);
12068
12069 if Ekind (Anc_F_Type) =
12070 E_Anonymous_Access_Type
12071 then
12072 Anc_F_Type := Designated_Type (Anc_F_Type);
12073
12074 if Ekind (Act_F_Type) =
12075 E_Anonymous_Access_Type
12076 then
12077 Act_F_Type :=
12078 Designated_Type (Act_F_Type);
12079 else
12080 exit;
12081 end if;
12082
12083 elsif
12084 Ekind (Act_F_Type) = E_Anonymous_Access_Type
12085 then
12086 exit;
12087 end if;
12088
12089 Anc_F_Type := Base_Type (Anc_F_Type);
12090 Act_F_Type := Base_Type (Act_F_Type);
12091
12092 -- If the formal is controlling, then the
12093 -- the type of the actual primitive's formal
12094 -- must be derived directly or indirectly
12095 -- from the type of the ancestor primitive's
12096 -- formal.
12097
12098 if Is_Controlling_Formal (Anc_Formal) then
12099 if not Is_Tagged_Ancestor
12100 (Anc_F_Type, Act_F_Type)
12101 then
12102 exit;
12103 end if;
12104
12105 -- Otherwise the types of the formals must
12106 -- be the same.
12107
12108 elsif Anc_F_Type /= Act_F_Type then
12109 exit;
12110 end if;
12111
12112 Next_Entity (Anc_Formal);
12113 Next_Entity (Act_Formal);
12114 end loop;
12115
12116 -- If we traversed through all of the formals
12117 -- then so far the subprograms correspond, so
12118 -- now check that any result types correspond.
12119
12120 if No (Anc_Formal) and then No (Act_Formal) then
12121 Subprograms_Correspond := True;
12122
12123 if Ekind (Act_Subp) = E_Function then
12124 Anc_F_Type := Etype (Anc_Subp);
12125 Act_F_Type := Etype (Act_Subp);
12126
12127 if Ekind (Anc_F_Type) =
12128 E_Anonymous_Access_Type
12129 then
12130 Anc_F_Type :=
12131 Designated_Type (Anc_F_Type);
12132
12133 if Ekind (Act_F_Type) =
12134 E_Anonymous_Access_Type
12135 then
12136 Act_F_Type :=
12137 Designated_Type (Act_F_Type);
12138 else
12139 Subprograms_Correspond := False;
12140 end if;
12141
12142 elsif
12143 Ekind (Act_F_Type)
12144 = E_Anonymous_Access_Type
12145 then
12146 Subprograms_Correspond := False;
12147 end if;
12148
12149 Anc_F_Type := Base_Type (Anc_F_Type);
12150 Act_F_Type := Base_Type (Act_F_Type);
12151
12152 -- Now either the result types must be
12153 -- the same or, if the result type is
12154 -- controlling, the result type of the
12155 -- actual primitive must descend from the
12156 -- result type of the ancestor primitive.
12157
12158 if Subprograms_Correspond
12159 and then Anc_F_Type /= Act_F_Type
12160 and then
12161 Has_Controlling_Result (Anc_Subp)
12162 and then not Is_Tagged_Ancestor
12163 (Anc_F_Type, Act_F_Type)
12164 then
12165 Subprograms_Correspond := False;
12166 end if;
12167 end if;
12168
12169 -- Found a matching subprogram belonging to
12170 -- formal ancestor type, so actual subprogram
12171 -- corresponds and this violates 3.9.3(9).
12172
12173 if Subprograms_Correspond then
12174 Error_Msg_NE
12175 ("abstract subprogram & overrides "
12176 & "nonabstract subprogram of ancestor",
12177 Actual, Act_Subp);
12178 end if;
12179 end if;
12180 end if;
12181 end if;
12182
12183 Next_Elmt (Act_Elmt);
12184 end loop;
12185 end if;
12186
12187 Next_Elmt (Gen_Elmt);
12188 end loop;
12189 end Check_Abstract_Primitives;
12190 end if;
12191
12192 -- Verify that limitedness matches. If parent is a limited
12193 -- interface then the generic formal is not unless declared
12194 -- explicitly so. If not declared limited, the actual cannot be
12195 -- limited (see AI05-0087).
12196
12197 -- Even though this AI is a binding interpretation, we enable the
12198 -- check only in Ada 2012 mode, because this improper construct
12199 -- shows up in user code and in existing B-tests.
12200
12201 if Is_Limited_Type (Act_T)
12202 and then not Is_Limited_Type (A_Gen_T)
12203 and then Ada_Version >= Ada_2012
12204 then
12205 if In_Instance then
12206 null;
12207 else
12208 Error_Msg_NE
12209 ("actual for non-limited & cannot be a limited type",
12210 Actual, Gen_T);
12211 Explain_Limited_Type (Act_T, Actual);
12212 Abandon_Instantiation (Actual);
12213 end if;
12214 end if;
12215 end Validate_Derived_Type_Instance;
12216
12217 ----------------------------------------
12218 -- Validate_Discriminated_Formal_Type --
12219 ----------------------------------------
12220
12221 procedure Validate_Discriminated_Formal_Type is
12222 Formal_Discr : Entity_Id;
12223 Actual_Discr : Entity_Id;
12224 Formal_Subt : Entity_Id;
12225
12226 begin
12227 if Has_Discriminants (A_Gen_T) then
12228 if not Has_Discriminants (Act_T) then
12229 Error_Msg_NE
12230 ("actual for & must have discriminants", Actual, Gen_T);
12231 Abandon_Instantiation (Actual);
12232
12233 elsif Is_Constrained (Act_T) then
12234 Error_Msg_NE
12235 ("actual for & must be unconstrained", Actual, Gen_T);
12236 Abandon_Instantiation (Actual);
12237
12238 else
12239 Formal_Discr := First_Discriminant (A_Gen_T);
12240 Actual_Discr := First_Discriminant (Act_T);
12241 while Formal_Discr /= Empty loop
12242 if Actual_Discr = Empty then
12243 Error_Msg_NE
12244 ("discriminants on actual do not match formal",
12245 Actual, Gen_T);
12246 Abandon_Instantiation (Actual);
12247 end if;
12248
12249 Formal_Subt := Get_Instance_Of (Etype (Formal_Discr));
12250
12251 -- Access discriminants match if designated types do
12252
12253 if Ekind (Base_Type (Formal_Subt)) = E_Anonymous_Access_Type
12254 and then (Ekind (Base_Type (Etype (Actual_Discr)))) =
12255 E_Anonymous_Access_Type
12256 and then
12257 Get_Instance_Of
12258 (Designated_Type (Base_Type (Formal_Subt))) =
12259 Designated_Type (Base_Type (Etype (Actual_Discr)))
12260 then
12261 null;
12262
12263 elsif Base_Type (Formal_Subt) /=
12264 Base_Type (Etype (Actual_Discr))
12265 then
12266 Error_Msg_NE
12267 ("types of actual discriminants must match formal",
12268 Actual, Gen_T);
12269 Abandon_Instantiation (Actual);
12270
12271 elsif not Subtypes_Statically_Match
12272 (Formal_Subt, Etype (Actual_Discr))
12273 and then Ada_Version >= Ada_95
12274 then
12275 Error_Msg_NE
12276 ("subtypes of actual discriminants must match formal",
12277 Actual, Gen_T);
12278 Abandon_Instantiation (Actual);
12279 end if;
12280
12281 Next_Discriminant (Formal_Discr);
12282 Next_Discriminant (Actual_Discr);
12283 end loop;
12284
12285 if Actual_Discr /= Empty then
12286 Error_Msg_NE
12287 ("discriminants on actual do not match formal",
12288 Actual, Gen_T);
12289 Abandon_Instantiation (Actual);
12290 end if;
12291 end if;
12292 end if;
12293 end Validate_Discriminated_Formal_Type;
12294
12295 ---------------------------------------
12296 -- Validate_Incomplete_Type_Instance --
12297 ---------------------------------------
12298
12299 procedure Validate_Incomplete_Type_Instance is
12300 begin
12301 if not Is_Tagged_Type (Act_T)
12302 and then Is_Tagged_Type (A_Gen_T)
12303 then
12304 Error_Msg_NE
12305 ("actual for & must be a tagged type", Actual, Gen_T);
12306 end if;
12307
12308 Validate_Discriminated_Formal_Type;
12309 end Validate_Incomplete_Type_Instance;
12310
12311 --------------------------------------
12312 -- Validate_Interface_Type_Instance --
12313 --------------------------------------
12314
12315 procedure Validate_Interface_Type_Instance is
12316 begin
12317 if not Is_Interface (Act_T) then
12318 Error_Msg_NE
12319 ("actual for formal interface type must be an interface",
12320 Actual, Gen_T);
12321
12322 elsif Is_Limited_Type (Act_T) /= Is_Limited_Type (A_Gen_T)
12323 or else Is_Task_Interface (A_Gen_T) /= Is_Task_Interface (Act_T)
12324 or else Is_Protected_Interface (A_Gen_T) /=
12325 Is_Protected_Interface (Act_T)
12326 or else Is_Synchronized_Interface (A_Gen_T) /=
12327 Is_Synchronized_Interface (Act_T)
12328 then
12329 Error_Msg_NE
12330 ("actual for interface& does not match (RM 12.5.5(4))",
12331 Actual, Gen_T);
12332 end if;
12333 end Validate_Interface_Type_Instance;
12334
12335 ------------------------------------
12336 -- Validate_Private_Type_Instance --
12337 ------------------------------------
12338
12339 procedure Validate_Private_Type_Instance is
12340 begin
12341 if Is_Limited_Type (Act_T)
12342 and then not Is_Limited_Type (A_Gen_T)
12343 then
12344 if In_Instance then
12345 null;
12346 else
12347 Error_Msg_NE
12348 ("actual for non-limited & cannot be a limited type", Actual,
12349 Gen_T);
12350 Explain_Limited_Type (Act_T, Actual);
12351 Abandon_Instantiation (Actual);
12352 end if;
12353
12354 elsif Known_To_Have_Preelab_Init (A_Gen_T)
12355 and then not Has_Preelaborable_Initialization (Act_T)
12356 then
12357 Error_Msg_NE
12358 ("actual for & must have preelaborable initialization", Actual,
12359 Gen_T);
12360
12361 elsif not Is_Definite_Subtype (Act_T)
12362 and then Is_Definite_Subtype (A_Gen_T)
12363 and then Ada_Version >= Ada_95
12364 then
12365 Error_Msg_NE
12366 ("actual for & must be a definite subtype", Actual, Gen_T);
12367
12368 elsif not Is_Tagged_Type (Act_T)
12369 and then Is_Tagged_Type (A_Gen_T)
12370 then
12371 Error_Msg_NE
12372 ("actual for & must be a tagged type", Actual, Gen_T);
12373 end if;
12374
12375 Validate_Discriminated_Formal_Type;
12376 Ancestor := Gen_T;
12377 end Validate_Private_Type_Instance;
12378
12379 -- Start of processing for Instantiate_Type
12380
12381 begin
12382 if Get_Instance_Of (A_Gen_T) /= A_Gen_T then
12383 Error_Msg_N ("duplicate instantiation of generic type", Actual);
12384 return New_List (Error);
12385
12386 elsif not Is_Entity_Name (Actual)
12387 or else not Is_Type (Entity (Actual))
12388 then
12389 Error_Msg_NE
12390 ("expect valid subtype mark to instantiate &", Actual, Gen_T);
12391 Abandon_Instantiation (Actual);
12392
12393 else
12394 Act_T := Entity (Actual);
12395
12396 -- Ada 2005 (AI-216): An Unchecked_Union subtype shall only be passed
12397 -- as a generic actual parameter if the corresponding formal type
12398 -- does not have a known_discriminant_part, or is a formal derived
12399 -- type that is an Unchecked_Union type.
12400
12401 if Is_Unchecked_Union (Base_Type (Act_T)) then
12402 if not Has_Discriminants (A_Gen_T)
12403 or else (Is_Derived_Type (A_Gen_T)
12404 and then Is_Unchecked_Union (A_Gen_T))
12405 then
12406 null;
12407 else
12408 Error_Msg_N ("unchecked union cannot be the actual for a "
12409 & "discriminated formal type", Act_T);
12410
12411 end if;
12412 end if;
12413
12414 -- Deal with fixed/floating restrictions
12415
12416 if Is_Floating_Point_Type (Act_T) then
12417 Check_Restriction (No_Floating_Point, Actual);
12418 elsif Is_Fixed_Point_Type (Act_T) then
12419 Check_Restriction (No_Fixed_Point, Actual);
12420 end if;
12421
12422 -- Deal with error of using incomplete type as generic actual.
12423 -- This includes limited views of a type, even if the non-limited
12424 -- view may be available.
12425
12426 if Ekind (Act_T) = E_Incomplete_Type
12427 or else (Is_Class_Wide_Type (Act_T)
12428 and then Ekind (Root_Type (Act_T)) = E_Incomplete_Type)
12429 then
12430 -- If the formal is an incomplete type, the actual can be
12431 -- incomplete as well.
12432
12433 if Ekind (A_Gen_T) = E_Incomplete_Type then
12434 null;
12435
12436 elsif Is_Class_Wide_Type (Act_T)
12437 or else No (Full_View (Act_T))
12438 then
12439 Error_Msg_N ("premature use of incomplete type", Actual);
12440 Abandon_Instantiation (Actual);
12441 else
12442 Act_T := Full_View (Act_T);
12443 Set_Entity (Actual, Act_T);
12444
12445 if Has_Private_Component (Act_T) then
12446 Error_Msg_N
12447 ("premature use of type with private component", Actual);
12448 end if;
12449 end if;
12450
12451 -- Deal with error of premature use of private type as generic actual
12452
12453 elsif Is_Private_Type (Act_T)
12454 and then Is_Private_Type (Base_Type (Act_T))
12455 and then not Is_Generic_Type (Act_T)
12456 and then not Is_Derived_Type (Act_T)
12457 and then No (Full_View (Root_Type (Act_T)))
12458 then
12459 -- If the formal is an incomplete type, the actual can be
12460 -- private or incomplete as well.
12461
12462 if Ekind (A_Gen_T) = E_Incomplete_Type then
12463 null;
12464 else
12465 Error_Msg_N ("premature use of private type", Actual);
12466 end if;
12467
12468 elsif Has_Private_Component (Act_T) then
12469 Error_Msg_N
12470 ("premature use of type with private component", Actual);
12471 end if;
12472
12473 Set_Instance_Of (A_Gen_T, Act_T);
12474
12475 -- If the type is generic, the class-wide type may also be used
12476
12477 if Is_Tagged_Type (A_Gen_T)
12478 and then Is_Tagged_Type (Act_T)
12479 and then not Is_Class_Wide_Type (A_Gen_T)
12480 then
12481 Set_Instance_Of (Class_Wide_Type (A_Gen_T),
12482 Class_Wide_Type (Act_T));
12483 end if;
12484
12485 if not Is_Abstract_Type (A_Gen_T)
12486 and then Is_Abstract_Type (Act_T)
12487 then
12488 Error_Msg_N
12489 ("actual of non-abstract formal cannot be abstract", Actual);
12490 end if;
12491
12492 -- A generic scalar type is a first subtype for which we generate
12493 -- an anonymous base type. Indicate that the instance of this base
12494 -- is the base type of the actual.
12495
12496 if Is_Scalar_Type (A_Gen_T) then
12497 Set_Instance_Of (Etype (A_Gen_T), Etype (Act_T));
12498 end if;
12499 end if;
12500
12501 if Error_Posted (Act_T) then
12502 null;
12503 else
12504 case Nkind (Def) is
12505 when N_Formal_Private_Type_Definition =>
12506 Validate_Private_Type_Instance;
12507
12508 when N_Formal_Incomplete_Type_Definition =>
12509 Validate_Incomplete_Type_Instance;
12510
12511 when N_Formal_Derived_Type_Definition =>
12512 Validate_Derived_Type_Instance;
12513
12514 when N_Formal_Discrete_Type_Definition =>
12515 if not Is_Discrete_Type (Act_T) then
12516 Error_Msg_NE
12517 ("expect discrete type in instantiation of&",
12518 Actual, Gen_T);
12519 Abandon_Instantiation (Actual);
12520 end if;
12521
12522 Diagnose_Predicated_Actual;
12523
12524 when N_Formal_Signed_Integer_Type_Definition =>
12525 if not Is_Signed_Integer_Type (Act_T) then
12526 Error_Msg_NE
12527 ("expect signed integer type in instantiation of&",
12528 Actual, Gen_T);
12529 Abandon_Instantiation (Actual);
12530 end if;
12531
12532 Diagnose_Predicated_Actual;
12533
12534 when N_Formal_Modular_Type_Definition =>
12535 if not Is_Modular_Integer_Type (Act_T) then
12536 Error_Msg_NE
12537 ("expect modular type in instantiation of &",
12538 Actual, Gen_T);
12539 Abandon_Instantiation (Actual);
12540 end if;
12541
12542 Diagnose_Predicated_Actual;
12543
12544 when N_Formal_Floating_Point_Definition =>
12545 if not Is_Floating_Point_Type (Act_T) then
12546 Error_Msg_NE
12547 ("expect float type in instantiation of &", Actual, Gen_T);
12548 Abandon_Instantiation (Actual);
12549 end if;
12550
12551 when N_Formal_Ordinary_Fixed_Point_Definition =>
12552 if not Is_Ordinary_Fixed_Point_Type (Act_T) then
12553 Error_Msg_NE
12554 ("expect ordinary fixed point type in instantiation of &",
12555 Actual, Gen_T);
12556 Abandon_Instantiation (Actual);
12557 end if;
12558
12559 when N_Formal_Decimal_Fixed_Point_Definition =>
12560 if not Is_Decimal_Fixed_Point_Type (Act_T) then
12561 Error_Msg_NE
12562 ("expect decimal type in instantiation of &",
12563 Actual, Gen_T);
12564 Abandon_Instantiation (Actual);
12565 end if;
12566
12567 when N_Array_Type_Definition =>
12568 Validate_Array_Type_Instance;
12569
12570 when N_Access_To_Object_Definition =>
12571 Validate_Access_Type_Instance;
12572
12573 when N_Access_Function_Definition |
12574 N_Access_Procedure_Definition =>
12575 Validate_Access_Subprogram_Instance;
12576
12577 when N_Record_Definition =>
12578 Validate_Interface_Type_Instance;
12579
12580 when N_Derived_Type_Definition =>
12581 Validate_Derived_Interface_Type_Instance;
12582
12583 when others =>
12584 raise Program_Error;
12585
12586 end case;
12587 end if;
12588
12589 Subt := New_Copy (Gen_T);
12590
12591 -- Use adjusted sloc of subtype name as the location for other nodes in
12592 -- the subtype declaration.
12593
12594 Loc := Sloc (Subt);
12595
12596 Decl_Node :=
12597 Make_Subtype_Declaration (Loc,
12598 Defining_Identifier => Subt,
12599 Subtype_Indication => New_Occurrence_Of (Act_T, Loc));
12600
12601 if Is_Private_Type (Act_T) then
12602 Set_Has_Private_View (Subtype_Indication (Decl_Node));
12603
12604 elsif Is_Access_Type (Act_T)
12605 and then Is_Private_Type (Designated_Type (Act_T))
12606 then
12607 Set_Has_Private_View (Subtype_Indication (Decl_Node));
12608 end if;
12609
12610 -- In Ada 2012 the actual may be a limited view. Indicate that
12611 -- the local subtype must be treated as such.
12612
12613 if From_Limited_With (Act_T) then
12614 Set_Ekind (Subt, E_Incomplete_Subtype);
12615 Set_From_Limited_With (Subt);
12616 end if;
12617
12618 Decl_Nodes := New_List (Decl_Node);
12619
12620 -- Flag actual derived types so their elaboration produces the
12621 -- appropriate renamings for the primitive operations of the ancestor.
12622 -- Flag actual for formal private types as well, to determine whether
12623 -- operations in the private part may override inherited operations.
12624 -- If the formal has an interface list, the ancestor is not the
12625 -- parent, but the analyzed formal that includes the interface
12626 -- operations of all its progenitors.
12627
12628 -- Same treatment for formal private types, so we can check whether the
12629 -- type is tagged limited when validating derivations in the private
12630 -- part. (See AI05-096).
12631
12632 if Nkind (Def) = N_Formal_Derived_Type_Definition then
12633 if Present (Interface_List (Def)) then
12634 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
12635 else
12636 Set_Generic_Parent_Type (Decl_Node, Ancestor);
12637 end if;
12638
12639 elsif Nkind_In (Def, N_Formal_Private_Type_Definition,
12640 N_Formal_Incomplete_Type_Definition)
12641 then
12642 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
12643 end if;
12644
12645 -- If the actual is a synchronized type that implements an interface,
12646 -- the primitive operations are attached to the corresponding record,
12647 -- and we have to treat it as an additional generic actual, so that its
12648 -- primitive operations become visible in the instance. The task or
12649 -- protected type itself does not carry primitive operations.
12650
12651 if Is_Concurrent_Type (Act_T)
12652 and then Is_Tagged_Type (Act_T)
12653 and then Present (Corresponding_Record_Type (Act_T))
12654 and then Present (Ancestor)
12655 and then Is_Interface (Ancestor)
12656 then
12657 declare
12658 Corr_Rec : constant Entity_Id :=
12659 Corresponding_Record_Type (Act_T);
12660 New_Corr : Entity_Id;
12661 Corr_Decl : Node_Id;
12662
12663 begin
12664 New_Corr := Make_Temporary (Loc, 'S');
12665 Corr_Decl :=
12666 Make_Subtype_Declaration (Loc,
12667 Defining_Identifier => New_Corr,
12668 Subtype_Indication =>
12669 New_Occurrence_Of (Corr_Rec, Loc));
12670 Append_To (Decl_Nodes, Corr_Decl);
12671
12672 if Ekind (Act_T) = E_Task_Type then
12673 Set_Ekind (Subt, E_Task_Subtype);
12674 else
12675 Set_Ekind (Subt, E_Protected_Subtype);
12676 end if;
12677
12678 Set_Corresponding_Record_Type (Subt, Corr_Rec);
12679 Set_Generic_Parent_Type (Corr_Decl, Ancestor);
12680 Set_Generic_Parent_Type (Decl_Node, Empty);
12681 end;
12682 end if;
12683
12684 -- For a floating-point type, capture dimension info if any, because
12685 -- the generated subtype declaration does not come from source and
12686 -- will not process dimensions.
12687
12688 if Is_Floating_Point_Type (Act_T) then
12689 Copy_Dimensions (Act_T, Subt);
12690 end if;
12691
12692 return Decl_Nodes;
12693 end Instantiate_Type;
12694
12695 ---------------------
12696 -- Is_In_Main_Unit --
12697 ---------------------
12698
12699 function Is_In_Main_Unit (N : Node_Id) return Boolean is
12700 Unum : constant Unit_Number_Type := Get_Source_Unit (N);
12701 Current_Unit : Node_Id;
12702
12703 begin
12704 if Unum = Main_Unit then
12705 return True;
12706
12707 -- If the current unit is a subunit then it is either the main unit or
12708 -- is being compiled as part of the main unit.
12709
12710 elsif Nkind (N) = N_Compilation_Unit then
12711 return Nkind (Unit (N)) = N_Subunit;
12712 end if;
12713
12714 Current_Unit := Parent (N);
12715 while Present (Current_Unit)
12716 and then Nkind (Current_Unit) /= N_Compilation_Unit
12717 loop
12718 Current_Unit := Parent (Current_Unit);
12719 end loop;
12720
12721 -- The instantiation node is in the main unit, or else the current node
12722 -- (perhaps as the result of nested instantiations) is in the main unit,
12723 -- or in the declaration of the main unit, which in this last case must
12724 -- be a body.
12725
12726 return Unum = Main_Unit
12727 or else Current_Unit = Cunit (Main_Unit)
12728 or else Current_Unit = Library_Unit (Cunit (Main_Unit))
12729 or else (Present (Library_Unit (Current_Unit))
12730 and then Is_In_Main_Unit (Library_Unit (Current_Unit)));
12731 end Is_In_Main_Unit;
12732
12733 ----------------------------
12734 -- Load_Parent_Of_Generic --
12735 ----------------------------
12736
12737 procedure Load_Parent_Of_Generic
12738 (N : Node_Id;
12739 Spec : Node_Id;
12740 Body_Optional : Boolean := False)
12741 is
12742 Comp_Unit : constant Node_Id := Cunit (Get_Source_Unit (Spec));
12743 Saved_Style_Check : constant Boolean := Style_Check;
12744 Saved_Warnings : constant Warning_Record := Save_Warnings;
12745 True_Parent : Node_Id;
12746 Inst_Node : Node_Id;
12747 OK : Boolean;
12748 Previous_Instances : constant Elist_Id := New_Elmt_List;
12749
12750 procedure Collect_Previous_Instances (Decls : List_Id);
12751 -- Collect all instantiations in the given list of declarations, that
12752 -- precede the generic that we need to load. If the bodies of these
12753 -- instantiations are available, we must analyze them, to ensure that
12754 -- the public symbols generated are the same when the unit is compiled
12755 -- to generate code, and when it is compiled in the context of a unit
12756 -- that needs a particular nested instance. This process is applied to
12757 -- both package and subprogram instances.
12758
12759 --------------------------------
12760 -- Collect_Previous_Instances --
12761 --------------------------------
12762
12763 procedure Collect_Previous_Instances (Decls : List_Id) is
12764 Decl : Node_Id;
12765
12766 begin
12767 Decl := First (Decls);
12768 while Present (Decl) loop
12769 if Sloc (Decl) >= Sloc (Inst_Node) then
12770 return;
12771
12772 -- If Decl is an instantiation, then record it as requiring
12773 -- instantiation of the corresponding body, except if it is an
12774 -- abbreviated instantiation generated internally for conformance
12775 -- checking purposes only for the case of a formal package
12776 -- declared without a box (see Instantiate_Formal_Package). Such
12777 -- an instantiation does not generate any code (the actual code
12778 -- comes from actual) and thus does not need to be analyzed here.
12779 -- If the instantiation appears with a generic package body it is
12780 -- not analyzed here either.
12781
12782 elsif Nkind (Decl) = N_Package_Instantiation
12783 and then not Is_Internal (Defining_Entity (Decl))
12784 then
12785 Append_Elmt (Decl, Previous_Instances);
12786
12787 -- For a subprogram instantiation, omit instantiations intrinsic
12788 -- operations (Unchecked_Conversions, etc.) that have no bodies.
12789
12790 elsif Nkind_In (Decl, N_Function_Instantiation,
12791 N_Procedure_Instantiation)
12792 and then not Is_Intrinsic_Subprogram (Entity (Name (Decl)))
12793 then
12794 Append_Elmt (Decl, Previous_Instances);
12795
12796 elsif Nkind (Decl) = N_Package_Declaration then
12797 Collect_Previous_Instances
12798 (Visible_Declarations (Specification (Decl)));
12799 Collect_Previous_Instances
12800 (Private_Declarations (Specification (Decl)));
12801
12802 -- Previous non-generic bodies may contain instances as well
12803
12804 elsif Nkind (Decl) = N_Package_Body
12805 and then Ekind (Corresponding_Spec (Decl)) /= E_Generic_Package
12806 then
12807 Collect_Previous_Instances (Declarations (Decl));
12808
12809 elsif Nkind (Decl) = N_Subprogram_Body
12810 and then not Acts_As_Spec (Decl)
12811 and then not Is_Generic_Subprogram (Corresponding_Spec (Decl))
12812 then
12813 Collect_Previous_Instances (Declarations (Decl));
12814 end if;
12815
12816 Next (Decl);
12817 end loop;
12818 end Collect_Previous_Instances;
12819
12820 -- Start of processing for Load_Parent_Of_Generic
12821
12822 begin
12823 if not In_Same_Source_Unit (N, Spec)
12824 or else Nkind (Unit (Comp_Unit)) = N_Package_Declaration
12825 or else (Nkind (Unit (Comp_Unit)) = N_Package_Body
12826 and then not Is_In_Main_Unit (Spec))
12827 then
12828 -- Find body of parent of spec, and analyze it. A special case arises
12829 -- when the parent is an instantiation, that is to say when we are
12830 -- currently instantiating a nested generic. In that case, there is
12831 -- no separate file for the body of the enclosing instance. Instead,
12832 -- the enclosing body must be instantiated as if it were a pending
12833 -- instantiation, in order to produce the body for the nested generic
12834 -- we require now. Note that in that case the generic may be defined
12835 -- in a package body, the instance defined in the same package body,
12836 -- and the original enclosing body may not be in the main unit.
12837
12838 Inst_Node := Empty;
12839
12840 True_Parent := Parent (Spec);
12841 while Present (True_Parent)
12842 and then Nkind (True_Parent) /= N_Compilation_Unit
12843 loop
12844 if Nkind (True_Parent) = N_Package_Declaration
12845 and then
12846 Nkind (Original_Node (True_Parent)) = N_Package_Instantiation
12847 then
12848 -- Parent is a compilation unit that is an instantiation.
12849 -- Instantiation node has been replaced with package decl.
12850
12851 Inst_Node := Original_Node (True_Parent);
12852 exit;
12853
12854 elsif Nkind (True_Parent) = N_Package_Declaration
12855 and then Present (Generic_Parent (Specification (True_Parent)))
12856 and then Nkind (Parent (True_Parent)) /= N_Compilation_Unit
12857 then
12858 -- Parent is an instantiation within another specification.
12859 -- Declaration for instance has been inserted before original
12860 -- instantiation node. A direct link would be preferable?
12861
12862 Inst_Node := Next (True_Parent);
12863 while Present (Inst_Node)
12864 and then Nkind (Inst_Node) /= N_Package_Instantiation
12865 loop
12866 Next (Inst_Node);
12867 end loop;
12868
12869 -- If the instance appears within a generic, and the generic
12870 -- unit is defined within a formal package of the enclosing
12871 -- generic, there is no generic body available, and none
12872 -- needed. A more precise test should be used ???
12873
12874 if No (Inst_Node) then
12875 return;
12876 end if;
12877
12878 exit;
12879
12880 else
12881 True_Parent := Parent (True_Parent);
12882 end if;
12883 end loop;
12884
12885 -- Case where we are currently instantiating a nested generic
12886
12887 if Present (Inst_Node) then
12888 if Nkind (Parent (True_Parent)) = N_Compilation_Unit then
12889
12890 -- Instantiation node and declaration of instantiated package
12891 -- were exchanged when only the declaration was needed.
12892 -- Restore instantiation node before proceeding with body.
12893
12894 Set_Unit (Parent (True_Parent), Inst_Node);
12895 end if;
12896
12897 -- Now complete instantiation of enclosing body, if it appears in
12898 -- some other unit. If it appears in the current unit, the body
12899 -- will have been instantiated already.
12900
12901 if No (Corresponding_Body (Instance_Spec (Inst_Node))) then
12902
12903 -- We need to determine the expander mode to instantiate the
12904 -- enclosing body. Because the generic body we need may use
12905 -- global entities declared in the enclosing package (including
12906 -- aggregates) it is in general necessary to compile this body
12907 -- with expansion enabled, except if we are within a generic
12908 -- package, in which case the usual generic rule applies.
12909
12910 declare
12911 Exp_Status : Boolean := True;
12912 Scop : Entity_Id;
12913
12914 begin
12915 -- Loop through scopes looking for generic package
12916
12917 Scop := Scope (Defining_Entity (Instance_Spec (Inst_Node)));
12918 while Present (Scop)
12919 and then Scop /= Standard_Standard
12920 loop
12921 if Ekind (Scop) = E_Generic_Package then
12922 Exp_Status := False;
12923 exit;
12924 end if;
12925
12926 Scop := Scope (Scop);
12927 end loop;
12928
12929 -- Collect previous instantiations in the unit that contains
12930 -- the desired generic.
12931
12932 if Nkind (Parent (True_Parent)) /= N_Compilation_Unit
12933 and then not Body_Optional
12934 then
12935 declare
12936 Decl : Elmt_Id;
12937 Info : Pending_Body_Info;
12938 Par : Node_Id;
12939
12940 begin
12941 Par := Parent (Inst_Node);
12942 while Present (Par) loop
12943 exit when Nkind (Parent (Par)) = N_Compilation_Unit;
12944 Par := Parent (Par);
12945 end loop;
12946
12947 pragma Assert (Present (Par));
12948
12949 if Nkind (Par) = N_Package_Body then
12950 Collect_Previous_Instances (Declarations (Par));
12951
12952 elsif Nkind (Par) = N_Package_Declaration then
12953 Collect_Previous_Instances
12954 (Visible_Declarations (Specification (Par)));
12955 Collect_Previous_Instances
12956 (Private_Declarations (Specification (Par)));
12957
12958 else
12959 -- Enclosing unit is a subprogram body. In this
12960 -- case all instance bodies are processed in order
12961 -- and there is no need to collect them separately.
12962
12963 null;
12964 end if;
12965
12966 Decl := First_Elmt (Previous_Instances);
12967 while Present (Decl) loop
12968 Info :=
12969 (Inst_Node => Node (Decl),
12970 Act_Decl =>
12971 Instance_Spec (Node (Decl)),
12972 Expander_Status => Exp_Status,
12973 Current_Sem_Unit =>
12974 Get_Code_Unit (Sloc (Node (Decl))),
12975 Scope_Suppress => Scope_Suppress,
12976 Local_Suppress_Stack_Top =>
12977 Local_Suppress_Stack_Top,
12978 Version => Ada_Version,
12979 Version_Pragma => Ada_Version_Pragma,
12980 Warnings => Save_Warnings,
12981 SPARK_Mode => SPARK_Mode,
12982 SPARK_Mode_Pragma => SPARK_Mode_Pragma);
12983
12984 -- Package instance
12985
12986 if
12987 Nkind (Node (Decl)) = N_Package_Instantiation
12988 then
12989 Instantiate_Package_Body
12990 (Info, Body_Optional => True);
12991
12992 -- Subprogram instance
12993
12994 else
12995 -- The instance_spec is in the wrapper package,
12996 -- usually followed by its local renaming
12997 -- declaration. See Build_Subprogram_Renaming
12998 -- for details.
12999
13000 declare
13001 Decl : Node_Id :=
13002 (Last (Visible_Declarations
13003 (Specification (Info.Act_Decl))));
13004 begin
13005 if Nkind (Decl) =
13006 N_Subprogram_Renaming_Declaration
13007 then
13008 Decl := Prev (Decl);
13009 end if;
13010
13011 Info.Act_Decl := Decl;
13012 end;
13013
13014 Instantiate_Subprogram_Body
13015 (Info, Body_Optional => True);
13016 end if;
13017
13018 Next_Elmt (Decl);
13019 end loop;
13020 end;
13021 end if;
13022
13023 Instantiate_Package_Body
13024 (Body_Info =>
13025 ((Inst_Node => Inst_Node,
13026 Act_Decl => True_Parent,
13027 Expander_Status => Exp_Status,
13028 Current_Sem_Unit => Get_Code_Unit
13029 (Sloc (Inst_Node)),
13030 Scope_Suppress => Scope_Suppress,
13031 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
13032 Version => Ada_Version,
13033 Version_Pragma => Ada_Version_Pragma,
13034 Warnings => Save_Warnings,
13035 SPARK_Mode => SPARK_Mode,
13036 SPARK_Mode_Pragma => SPARK_Mode_Pragma)),
13037 Body_Optional => Body_Optional);
13038 end;
13039 end if;
13040
13041 -- Case where we are not instantiating a nested generic
13042
13043 else
13044 Opt.Style_Check := False;
13045 Expander_Mode_Save_And_Set (True);
13046 Load_Needed_Body (Comp_Unit, OK);
13047 Opt.Style_Check := Saved_Style_Check;
13048 Restore_Warnings (Saved_Warnings);
13049 Expander_Mode_Restore;
13050
13051 if not OK
13052 and then Unit_Requires_Body (Defining_Entity (Spec))
13053 and then not Body_Optional
13054 then
13055 declare
13056 Bname : constant Unit_Name_Type :=
13057 Get_Body_Name (Get_Unit_Name (Unit (Comp_Unit)));
13058
13059 begin
13060 -- In CodePeer mode, the missing body may make the analysis
13061 -- incomplete, but we do not treat it as fatal.
13062
13063 if CodePeer_Mode then
13064 return;
13065
13066 else
13067 Error_Msg_Unit_1 := Bname;
13068 Error_Msg_N ("this instantiation requires$!", N);
13069 Error_Msg_File_1 :=
13070 Get_File_Name (Bname, Subunit => False);
13071 Error_Msg_N ("\but file{ was not found!", N);
13072 raise Unrecoverable_Error;
13073 end if;
13074 end;
13075 end if;
13076 end if;
13077 end if;
13078
13079 -- If loading parent of the generic caused an instantiation circularity,
13080 -- we abandon compilation at this point, because otherwise in some cases
13081 -- we get into trouble with infinite recursions after this point.
13082
13083 if Circularity_Detected then
13084 raise Unrecoverable_Error;
13085 end if;
13086 end Load_Parent_Of_Generic;
13087
13088 ---------------------------------
13089 -- Map_Formal_Package_Entities --
13090 ---------------------------------
13091
13092 procedure Map_Formal_Package_Entities (Form : Entity_Id; Act : Entity_Id) is
13093 E1 : Entity_Id;
13094 E2 : Entity_Id;
13095
13096 begin
13097 Set_Instance_Of (Form, Act);
13098
13099 -- Traverse formal and actual package to map the corresponding entities.
13100 -- We skip over internal entities that may be generated during semantic
13101 -- analysis, and find the matching entities by name, given that they
13102 -- must appear in the same order.
13103
13104 E1 := First_Entity (Form);
13105 E2 := First_Entity (Act);
13106 while Present (E1) and then E1 /= First_Private_Entity (Form) loop
13107 -- Could this test be a single condition??? Seems like it could, and
13108 -- isn't FPE (Form) a constant anyway???
13109
13110 if not Is_Internal (E1)
13111 and then Present (Parent (E1))
13112 and then not Is_Class_Wide_Type (E1)
13113 and then not Is_Internal_Name (Chars (E1))
13114 then
13115 while Present (E2) and then Chars (E2) /= Chars (E1) loop
13116 Next_Entity (E2);
13117 end loop;
13118
13119 if No (E2) then
13120 exit;
13121 else
13122 Set_Instance_Of (E1, E2);
13123
13124 if Is_Type (E1) and then Is_Tagged_Type (E2) then
13125 Set_Instance_Of (Class_Wide_Type (E1), Class_Wide_Type (E2));
13126 end if;
13127
13128 if Is_Constrained (E1) then
13129 Set_Instance_Of (Base_Type (E1), Base_Type (E2));
13130 end if;
13131
13132 if Ekind (E1) = E_Package and then No (Renamed_Object (E1)) then
13133 Map_Formal_Package_Entities (E1, E2);
13134 end if;
13135 end if;
13136 end if;
13137
13138 Next_Entity (E1);
13139 end loop;
13140 end Map_Formal_Package_Entities;
13141
13142 -----------------------
13143 -- Move_Freeze_Nodes --
13144 -----------------------
13145
13146 procedure Move_Freeze_Nodes
13147 (Out_Of : Entity_Id;
13148 After : Node_Id;
13149 L : List_Id)
13150 is
13151 Decl : Node_Id;
13152 Next_Decl : Node_Id;
13153 Next_Node : Node_Id := After;
13154 Spec : Node_Id;
13155
13156 function Is_Outer_Type (T : Entity_Id) return Boolean;
13157 -- Check whether entity is declared in a scope external to that of the
13158 -- generic unit.
13159
13160 -------------------
13161 -- Is_Outer_Type --
13162 -------------------
13163
13164 function Is_Outer_Type (T : Entity_Id) return Boolean is
13165 Scop : Entity_Id := Scope (T);
13166
13167 begin
13168 if Scope_Depth (Scop) < Scope_Depth (Out_Of) then
13169 return True;
13170
13171 else
13172 while Scop /= Standard_Standard loop
13173 if Scop = Out_Of then
13174 return False;
13175 else
13176 Scop := Scope (Scop);
13177 end if;
13178 end loop;
13179
13180 return True;
13181 end if;
13182 end Is_Outer_Type;
13183
13184 -- Start of processing for Move_Freeze_Nodes
13185
13186 begin
13187 if No (L) then
13188 return;
13189 end if;
13190
13191 -- First remove the freeze nodes that may appear before all other
13192 -- declarations.
13193
13194 Decl := First (L);
13195 while Present (Decl)
13196 and then Nkind (Decl) = N_Freeze_Entity
13197 and then Is_Outer_Type (Entity (Decl))
13198 loop
13199 Decl := Remove_Head (L);
13200 Insert_After (Next_Node, Decl);
13201 Set_Analyzed (Decl, False);
13202 Next_Node := Decl;
13203 Decl := First (L);
13204 end loop;
13205
13206 -- Next scan the list of declarations and remove each freeze node that
13207 -- appears ahead of the current node.
13208
13209 while Present (Decl) loop
13210 while Present (Next (Decl))
13211 and then Nkind (Next (Decl)) = N_Freeze_Entity
13212 and then Is_Outer_Type (Entity (Next (Decl)))
13213 loop
13214 Next_Decl := Remove_Next (Decl);
13215 Insert_After (Next_Node, Next_Decl);
13216 Set_Analyzed (Next_Decl, False);
13217 Next_Node := Next_Decl;
13218 end loop;
13219
13220 -- If the declaration is a nested package or concurrent type, then
13221 -- recurse. Nested generic packages will have been processed from the
13222 -- inside out.
13223
13224 case Nkind (Decl) is
13225 when N_Package_Declaration =>
13226 Spec := Specification (Decl);
13227
13228 when N_Task_Type_Declaration =>
13229 Spec := Task_Definition (Decl);
13230
13231 when N_Protected_Type_Declaration =>
13232 Spec := Protected_Definition (Decl);
13233
13234 when others =>
13235 Spec := Empty;
13236 end case;
13237
13238 if Present (Spec) then
13239 Move_Freeze_Nodes (Out_Of, Next_Node, Visible_Declarations (Spec));
13240 Move_Freeze_Nodes (Out_Of, Next_Node, Private_Declarations (Spec));
13241 end if;
13242
13243 Next (Decl);
13244 end loop;
13245 end Move_Freeze_Nodes;
13246
13247 ----------------
13248 -- Next_Assoc --
13249 ----------------
13250
13251 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr is
13252 begin
13253 return Generic_Renamings.Table (E).Next_In_HTable;
13254 end Next_Assoc;
13255
13256 ------------------------
13257 -- Preanalyze_Actuals --
13258 ------------------------
13259
13260 procedure Preanalyze_Actuals (N : Node_Id; Inst : Entity_Id := Empty) is
13261 Assoc : Node_Id;
13262 Act : Node_Id;
13263 Errs : constant Int := Serious_Errors_Detected;
13264
13265 Cur : Entity_Id := Empty;
13266 -- Current homograph of the instance name
13267
13268 Vis : Boolean;
13269 -- Saved visibility status of the current homograph
13270
13271 begin
13272 Assoc := First (Generic_Associations (N));
13273
13274 -- If the instance is a child unit, its name may hide an outer homonym,
13275 -- so make it invisible to perform name resolution on the actuals.
13276
13277 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name
13278 and then Present
13279 (Current_Entity (Defining_Identifier (Defining_Unit_Name (N))))
13280 then
13281 Cur := Current_Entity (Defining_Identifier (Defining_Unit_Name (N)));
13282
13283 if Is_Compilation_Unit (Cur) then
13284 Vis := Is_Immediately_Visible (Cur);
13285 Set_Is_Immediately_Visible (Cur, False);
13286 else
13287 Cur := Empty;
13288 end if;
13289 end if;
13290
13291 while Present (Assoc) loop
13292 if Nkind (Assoc) /= N_Others_Choice then
13293 Act := Explicit_Generic_Actual_Parameter (Assoc);
13294
13295 -- Within a nested instantiation, a defaulted actual is an empty
13296 -- association, so nothing to analyze. If the subprogram actual
13297 -- is an attribute, analyze prefix only, because actual is not a
13298 -- complete attribute reference.
13299
13300 -- If actual is an allocator, analyze expression only. The full
13301 -- analysis can generate code, and if instance is a compilation
13302 -- unit we have to wait until the package instance is installed
13303 -- to have a proper place to insert this code.
13304
13305 -- String literals may be operators, but at this point we do not
13306 -- know whether the actual is a formal subprogram or a string.
13307
13308 if No (Act) then
13309 null;
13310
13311 elsif Nkind (Act) = N_Attribute_Reference then
13312 Analyze (Prefix (Act));
13313
13314 elsif Nkind (Act) = N_Explicit_Dereference then
13315 Analyze (Prefix (Act));
13316
13317 elsif Nkind (Act) = N_Allocator then
13318 declare
13319 Expr : constant Node_Id := Expression (Act);
13320
13321 begin
13322 if Nkind (Expr) = N_Subtype_Indication then
13323 Analyze (Subtype_Mark (Expr));
13324
13325 -- Analyze separately each discriminant constraint, when
13326 -- given with a named association.
13327
13328 declare
13329 Constr : Node_Id;
13330
13331 begin
13332 Constr := First (Constraints (Constraint (Expr)));
13333 while Present (Constr) loop
13334 if Nkind (Constr) = N_Discriminant_Association then
13335 Analyze (Expression (Constr));
13336 else
13337 Analyze (Constr);
13338 end if;
13339
13340 Next (Constr);
13341 end loop;
13342 end;
13343
13344 else
13345 Analyze (Expr);
13346 end if;
13347 end;
13348
13349 elsif Nkind (Act) /= N_Operator_Symbol then
13350 Analyze (Act);
13351
13352 if Is_Entity_Name (Act)
13353 and then Is_Type (Entity (Act))
13354 and then From_Limited_With (Entity (Act))
13355 then
13356 Append_Elmt (Entity (Act), Incomplete_Actuals (Inst));
13357 end if;
13358 end if;
13359
13360 if Errs /= Serious_Errors_Detected then
13361
13362 -- Do a minimal analysis of the generic, to prevent spurious
13363 -- warnings complaining about the generic being unreferenced,
13364 -- before abandoning the instantiation.
13365
13366 Analyze (Name (N));
13367
13368 if Is_Entity_Name (Name (N))
13369 and then Etype (Name (N)) /= Any_Type
13370 then
13371 Generate_Reference (Entity (Name (N)), Name (N));
13372 Set_Is_Instantiated (Entity (Name (N)));
13373 end if;
13374
13375 if Present (Cur) then
13376
13377 -- For the case of a child instance hiding an outer homonym,
13378 -- provide additional warning which might explain the error.
13379
13380 Set_Is_Immediately_Visible (Cur, Vis);
13381 Error_Msg_NE
13382 ("& hides outer unit with the same name??",
13383 N, Defining_Unit_Name (N));
13384 end if;
13385
13386 Abandon_Instantiation (Act);
13387 end if;
13388 end if;
13389
13390 Next (Assoc);
13391 end loop;
13392
13393 if Present (Cur) then
13394 Set_Is_Immediately_Visible (Cur, Vis);
13395 end if;
13396 end Preanalyze_Actuals;
13397
13398 -------------------
13399 -- Remove_Parent --
13400 -------------------
13401
13402 procedure Remove_Parent (In_Body : Boolean := False) is
13403 S : Entity_Id := Current_Scope;
13404 -- S is the scope containing the instantiation just completed. The scope
13405 -- stack contains the parent instances of the instantiation, followed by
13406 -- the original S.
13407
13408 Cur_P : Entity_Id;
13409 E : Entity_Id;
13410 P : Entity_Id;
13411 Hidden : Elmt_Id;
13412
13413 begin
13414 -- After child instantiation is complete, remove from scope stack the
13415 -- extra copy of the current scope, and then remove parent instances.
13416
13417 if not In_Body then
13418 Pop_Scope;
13419
13420 while Current_Scope /= S loop
13421 P := Current_Scope;
13422 End_Package_Scope (Current_Scope);
13423
13424 if In_Open_Scopes (P) then
13425 E := First_Entity (P);
13426 while Present (E) loop
13427 Set_Is_Immediately_Visible (E, True);
13428 Next_Entity (E);
13429 end loop;
13430
13431 -- If instantiation is declared in a block, it is the enclosing
13432 -- scope that might be a parent instance. Note that only one
13433 -- block can be involved, because the parent instances have
13434 -- been installed within it.
13435
13436 if Ekind (P) = E_Block then
13437 Cur_P := Scope (P);
13438 else
13439 Cur_P := P;
13440 end if;
13441
13442 if Is_Generic_Instance (Cur_P) and then P /= Current_Scope then
13443 -- We are within an instance of some sibling. Retain
13444 -- visibility of parent, for proper subsequent cleanup, and
13445 -- reinstall private declarations as well.
13446
13447 Set_In_Private_Part (P);
13448 Install_Private_Declarations (P);
13449 end if;
13450
13451 -- If the ultimate parent is a top-level unit recorded in
13452 -- Instance_Parent_Unit, then reset its visibility to what it was
13453 -- before instantiation. (It's not clear what the purpose is of
13454 -- testing whether Scope (P) is In_Open_Scopes, but that test was
13455 -- present before the ultimate parent test was added.???)
13456
13457 elsif not In_Open_Scopes (Scope (P))
13458 or else (P = Instance_Parent_Unit
13459 and then not Parent_Unit_Visible)
13460 then
13461 Set_Is_Immediately_Visible (P, False);
13462
13463 -- If the current scope is itself an instantiation of a generic
13464 -- nested within P, and we are in the private part of body of this
13465 -- instantiation, restore the full views of P, that were removed
13466 -- in End_Package_Scope above. This obscure case can occur when a
13467 -- subunit of a generic contains an instance of a child unit of
13468 -- its generic parent unit.
13469
13470 elsif S = Current_Scope and then Is_Generic_Instance (S) then
13471 declare
13472 Par : constant Entity_Id :=
13473 Generic_Parent (Package_Specification (S));
13474 begin
13475 if Present (Par)
13476 and then P = Scope (Par)
13477 and then (In_Package_Body (S) or else In_Private_Part (S))
13478 then
13479 Set_In_Private_Part (P);
13480 Install_Private_Declarations (P);
13481 end if;
13482 end;
13483 end if;
13484 end loop;
13485
13486 -- Reset visibility of entities in the enclosing scope
13487
13488 Set_Is_Hidden_Open_Scope (Current_Scope, False);
13489
13490 Hidden := First_Elmt (Hidden_Entities);
13491 while Present (Hidden) loop
13492 Set_Is_Immediately_Visible (Node (Hidden), True);
13493 Next_Elmt (Hidden);
13494 end loop;
13495
13496 else
13497 -- Each body is analyzed separately, and there is no context that
13498 -- needs preserving from one body instance to the next, so remove all
13499 -- parent scopes that have been installed.
13500
13501 while Present (S) loop
13502 End_Package_Scope (S);
13503 Set_Is_Immediately_Visible (S, False);
13504 S := Current_Scope;
13505 exit when S = Standard_Standard;
13506 end loop;
13507 end if;
13508 end Remove_Parent;
13509
13510 -----------------
13511 -- Restore_Env --
13512 -----------------
13513
13514 procedure Restore_Env is
13515 Saved : Instance_Env renames Instance_Envs.Table (Instance_Envs.Last);
13516
13517 begin
13518 if No (Current_Instantiated_Parent.Act_Id) then
13519 -- Restore environment after subprogram inlining
13520
13521 Restore_Private_Views (Empty);
13522 end if;
13523
13524 Current_Instantiated_Parent := Saved.Instantiated_Parent;
13525 Exchanged_Views := Saved.Exchanged_Views;
13526 Hidden_Entities := Saved.Hidden_Entities;
13527 Current_Sem_Unit := Saved.Current_Sem_Unit;
13528 Parent_Unit_Visible := Saved.Parent_Unit_Visible;
13529 Instance_Parent_Unit := Saved.Instance_Parent_Unit;
13530
13531 Restore_Opt_Config_Switches (Saved.Switches);
13532
13533 Instance_Envs.Decrement_Last;
13534 end Restore_Env;
13535
13536 ---------------------------
13537 -- Restore_Private_Views --
13538 ---------------------------
13539
13540 procedure Restore_Private_Views
13541 (Pack_Id : Entity_Id;
13542 Is_Package : Boolean := True)
13543 is
13544 M : Elmt_Id;
13545 E : Entity_Id;
13546 Typ : Entity_Id;
13547 Dep_Elmt : Elmt_Id;
13548 Dep_Typ : Node_Id;
13549
13550 procedure Restore_Nested_Formal (Formal : Entity_Id);
13551 -- Hide the generic formals of formal packages declared with box which
13552 -- were reachable in the current instantiation.
13553
13554 ---------------------------
13555 -- Restore_Nested_Formal --
13556 ---------------------------
13557
13558 procedure Restore_Nested_Formal (Formal : Entity_Id) is
13559 Ent : Entity_Id;
13560
13561 begin
13562 if Present (Renamed_Object (Formal))
13563 and then Denotes_Formal_Package (Renamed_Object (Formal), True)
13564 then
13565 return;
13566
13567 elsif Present (Associated_Formal_Package (Formal)) then
13568 Ent := First_Entity (Formal);
13569 while Present (Ent) loop
13570 exit when Ekind (Ent) = E_Package
13571 and then Renamed_Entity (Ent) = Renamed_Entity (Formal);
13572
13573 Set_Is_Hidden (Ent);
13574 Set_Is_Potentially_Use_Visible (Ent, False);
13575
13576 -- If package, then recurse
13577
13578 if Ekind (Ent) = E_Package then
13579 Restore_Nested_Formal (Ent);
13580 end if;
13581
13582 Next_Entity (Ent);
13583 end loop;
13584 end if;
13585 end Restore_Nested_Formal;
13586
13587 -- Start of processing for Restore_Private_Views
13588
13589 begin
13590 M := First_Elmt (Exchanged_Views);
13591 while Present (M) loop
13592 Typ := Node (M);
13593
13594 -- Subtypes of types whose views have been exchanged, and that are
13595 -- defined within the instance, were not on the Private_Dependents
13596 -- list on entry to the instance, so they have to be exchanged
13597 -- explicitly now, in order to remain consistent with the view of the
13598 -- parent type.
13599
13600 if Ekind_In (Typ, E_Private_Type,
13601 E_Limited_Private_Type,
13602 E_Record_Type_With_Private)
13603 then
13604 Dep_Elmt := First_Elmt (Private_Dependents (Typ));
13605 while Present (Dep_Elmt) loop
13606 Dep_Typ := Node (Dep_Elmt);
13607
13608 if Scope (Dep_Typ) = Pack_Id
13609 and then Present (Full_View (Dep_Typ))
13610 then
13611 Replace_Elmt (Dep_Elmt, Full_View (Dep_Typ));
13612 Exchange_Declarations (Dep_Typ);
13613 end if;
13614
13615 Next_Elmt (Dep_Elmt);
13616 end loop;
13617 end if;
13618
13619 Exchange_Declarations (Node (M));
13620 Next_Elmt (M);
13621 end loop;
13622
13623 if No (Pack_Id) then
13624 return;
13625 end if;
13626
13627 -- Make the generic formal parameters private, and make the formal types
13628 -- into subtypes of the actuals again.
13629
13630 E := First_Entity (Pack_Id);
13631 while Present (E) loop
13632 Set_Is_Hidden (E, True);
13633
13634 if Is_Type (E)
13635 and then Nkind (Parent (E)) = N_Subtype_Declaration
13636 then
13637 -- If the actual for E is itself a generic actual type from
13638 -- an enclosing instance, E is still a generic actual type
13639 -- outside of the current instance. This matter when resolving
13640 -- an overloaded call that may be ambiguous in the enclosing
13641 -- instance, when two of its actuals coincide.
13642
13643 if Is_Entity_Name (Subtype_Indication (Parent (E)))
13644 and then Is_Generic_Actual_Type
13645 (Entity (Subtype_Indication (Parent (E))))
13646 then
13647 null;
13648 else
13649 Set_Is_Generic_Actual_Type (E, False);
13650 end if;
13651
13652 -- An unusual case of aliasing: the actual may also be directly
13653 -- visible in the generic, and be private there, while it is fully
13654 -- visible in the context of the instance. The internal subtype
13655 -- is private in the instance but has full visibility like its
13656 -- parent in the enclosing scope. This enforces the invariant that
13657 -- the privacy status of all private dependents of a type coincide
13658 -- with that of the parent type. This can only happen when a
13659 -- generic child unit is instantiated within a sibling.
13660
13661 if Is_Private_Type (E)
13662 and then not Is_Private_Type (Etype (E))
13663 then
13664 Exchange_Declarations (E);
13665 end if;
13666
13667 elsif Ekind (E) = E_Package then
13668
13669 -- The end of the renaming list is the renaming of the generic
13670 -- package itself. If the instance is a subprogram, all entities
13671 -- in the corresponding package are renamings. If this entity is
13672 -- a formal package, make its own formals private as well. The
13673 -- actual in this case is itself the renaming of an instantiation.
13674 -- If the entity is not a package renaming, it is the entity
13675 -- created to validate formal package actuals: ignore it.
13676
13677 -- If the actual is itself a formal package for the enclosing
13678 -- generic, or the actual for such a formal package, it remains
13679 -- visible on exit from the instance, and therefore nothing needs
13680 -- to be done either, except to keep it accessible.
13681
13682 if Is_Package and then Renamed_Object (E) = Pack_Id then
13683 exit;
13684
13685 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
13686 null;
13687
13688 elsif
13689 Denotes_Formal_Package (Renamed_Object (E), True, Pack_Id)
13690 then
13691 Set_Is_Hidden (E, False);
13692
13693 else
13694 declare
13695 Act_P : constant Entity_Id := Renamed_Object (E);
13696 Id : Entity_Id;
13697
13698 begin
13699 Id := First_Entity (Act_P);
13700 while Present (Id)
13701 and then Id /= First_Private_Entity (Act_P)
13702 loop
13703 exit when Ekind (Id) = E_Package
13704 and then Renamed_Object (Id) = Act_P;
13705
13706 Set_Is_Hidden (Id, True);
13707 Set_Is_Potentially_Use_Visible (Id, In_Use (Act_P));
13708
13709 if Ekind (Id) = E_Package then
13710 Restore_Nested_Formal (Id);
13711 end if;
13712
13713 Next_Entity (Id);
13714 end loop;
13715 end;
13716 end if;
13717 end if;
13718
13719 Next_Entity (E);
13720 end loop;
13721 end Restore_Private_Views;
13722
13723 --------------
13724 -- Save_Env --
13725 --------------
13726
13727 procedure Save_Env
13728 (Gen_Unit : Entity_Id;
13729 Act_Unit : Entity_Id)
13730 is
13731 begin
13732 Init_Env;
13733 Set_Instance_Env (Gen_Unit, Act_Unit);
13734 end Save_Env;
13735
13736 ----------------------------
13737 -- Save_Global_References --
13738 ----------------------------
13739
13740 procedure Save_Global_References (Templ : Node_Id) is
13741
13742 -- ??? it is horrible to use global variables in highly recursive code
13743
13744 E : Entity_Id;
13745 -- The entity of the current associated node
13746
13747 Gen_Scope : Entity_Id;
13748 -- The scope of the generic for which references are being saved
13749
13750 N2 : Node_Id;
13751 -- The current associated node
13752
13753 function Is_Global (E : Entity_Id) return Boolean;
13754 -- Check whether entity is defined outside of generic unit. Examine the
13755 -- scope of an entity, and the scope of the scope, etc, until we find
13756 -- either Standard, in which case the entity is global, or the generic
13757 -- unit itself, which indicates that the entity is local. If the entity
13758 -- is the generic unit itself, as in the case of a recursive call, or
13759 -- the enclosing generic unit, if different from the current scope, then
13760 -- it is local as well, because it will be replaced at the point of
13761 -- instantiation. On the other hand, if it is a reference to a child
13762 -- unit of a common ancestor, which appears in an instantiation, it is
13763 -- global because it is used to denote a specific compilation unit at
13764 -- the time the instantiations will be analyzed.
13765
13766 procedure Reset_Entity (N : Node_Id);
13767 -- Save semantic information on global entity so that it is not resolved
13768 -- again at instantiation time.
13769
13770 procedure Save_Entity_Descendants (N : Node_Id);
13771 -- Apply Save_Global_References to the two syntactic descendants of
13772 -- non-terminal nodes that carry an Associated_Node and are processed
13773 -- through Reset_Entity. Once the global entity (if any) has been
13774 -- captured together with its type, only two syntactic descendants need
13775 -- to be traversed to complete the processing of the tree rooted at N.
13776 -- This applies to Selected_Components, Expanded_Names, and to Operator
13777 -- nodes. N can also be a character literal, identifier, or operator
13778 -- symbol node, but the call has no effect in these cases.
13779
13780 procedure Save_Global_Defaults (N1 : Node_Id; N2 : Node_Id);
13781 -- Default actuals in nested instances must be handled specially
13782 -- because there is no link to them from the original tree. When an
13783 -- actual subprogram is given by a default, we add an explicit generic
13784 -- association for it in the instantiation node. When we save the
13785 -- global references on the name of the instance, we recover the list
13786 -- of generic associations, and add an explicit one to the original
13787 -- generic tree, through which a global actual can be preserved.
13788 -- Similarly, if a child unit is instantiated within a sibling, in the
13789 -- context of the parent, we must preserve the identifier of the parent
13790 -- so that it can be properly resolved in a subsequent instantiation.
13791
13792 procedure Save_Global_Descendant (D : Union_Id);
13793 -- Apply Save_References recursively to the descendents of node D
13794
13795 procedure Save_References (N : Node_Id);
13796 -- This is the recursive procedure that does the work, once the
13797 -- enclosing generic scope has been established.
13798
13799 ---------------
13800 -- Is_Global --
13801 ---------------
13802
13803 function Is_Global (E : Entity_Id) return Boolean is
13804 Se : Entity_Id;
13805
13806 function Is_Instance_Node (Decl : Node_Id) return Boolean;
13807 -- Determine whether the parent node of a reference to a child unit
13808 -- denotes an instantiation or a formal package, in which case the
13809 -- reference to the child unit is global, even if it appears within
13810 -- the current scope (e.g. when the instance appears within the body
13811 -- of an ancestor).
13812
13813 ----------------------
13814 -- Is_Instance_Node --
13815 ----------------------
13816
13817 function Is_Instance_Node (Decl : Node_Id) return Boolean is
13818 begin
13819 return Nkind (Decl) in N_Generic_Instantiation
13820 or else
13821 Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration;
13822 end Is_Instance_Node;
13823
13824 -- Start of processing for Is_Global
13825
13826 begin
13827 if E = Gen_Scope then
13828 return False;
13829
13830 elsif E = Standard_Standard then
13831 return True;
13832
13833 elsif Is_Child_Unit (E)
13834 and then (Is_Instance_Node (Parent (N2))
13835 or else (Nkind (Parent (N2)) = N_Expanded_Name
13836 and then N2 = Selector_Name (Parent (N2))
13837 and then
13838 Is_Instance_Node (Parent (Parent (N2)))))
13839 then
13840 return True;
13841
13842 else
13843 Se := Scope (E);
13844 while Se /= Gen_Scope loop
13845 if Se = Standard_Standard then
13846 return True;
13847 else
13848 Se := Scope (Se);
13849 end if;
13850 end loop;
13851
13852 return False;
13853 end if;
13854 end Is_Global;
13855
13856 ------------------
13857 -- Reset_Entity --
13858 ------------------
13859
13860 procedure Reset_Entity (N : Node_Id) is
13861 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id);
13862 -- If the type of N2 is global to the generic unit, save the type in
13863 -- the generic node. Just as we perform name capture for explicit
13864 -- references within the generic, we must capture the global types
13865 -- of local entities because they may participate in resolution in
13866 -- the instance.
13867
13868 function Top_Ancestor (E : Entity_Id) return Entity_Id;
13869 -- Find the ultimate ancestor of the current unit. If it is not a
13870 -- generic unit, then the name of the current unit in the prefix of
13871 -- an expanded name must be replaced with its generic homonym to
13872 -- ensure that it will be properly resolved in an instance.
13873
13874 ---------------------
13875 -- Set_Global_Type --
13876 ---------------------
13877
13878 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id) is
13879 Typ : constant Entity_Id := Etype (N2);
13880
13881 begin
13882 Set_Etype (N, Typ);
13883
13884 -- If the entity of N is not the associated node, this is a
13885 -- nested generic and it has an associated node as well, whose
13886 -- type is already the full view (see below). Indicate that the
13887 -- original node has a private view.
13888
13889 if Entity (N) /= N2 and then Has_Private_View (Entity (N)) then
13890 Set_Has_Private_View (N);
13891 end if;
13892
13893 -- If not a private type, nothing else to do
13894
13895 if not Is_Private_Type (Typ) then
13896 if Is_Array_Type (Typ)
13897 and then Is_Private_Type (Component_Type (Typ))
13898 then
13899 Set_Has_Private_View (N);
13900 end if;
13901
13902 -- If it is a derivation of a private type in a context where no
13903 -- full view is needed, nothing to do either.
13904
13905 elsif No (Full_View (Typ)) and then Typ /= Etype (Typ) then
13906 null;
13907
13908 -- Otherwise mark the type for flipping and use the full view when
13909 -- available.
13910
13911 else
13912 Set_Has_Private_View (N);
13913
13914 if Present (Full_View (Typ)) then
13915 Set_Etype (N2, Full_View (Typ));
13916 end if;
13917 end if;
13918 end Set_Global_Type;
13919
13920 ------------------
13921 -- Top_Ancestor --
13922 ------------------
13923
13924 function Top_Ancestor (E : Entity_Id) return Entity_Id is
13925 Par : Entity_Id;
13926
13927 begin
13928 Par := E;
13929 while Is_Child_Unit (Par) loop
13930 Par := Scope (Par);
13931 end loop;
13932
13933 return Par;
13934 end Top_Ancestor;
13935
13936 -- Start of processing for Reset_Entity
13937
13938 begin
13939 N2 := Get_Associated_Node (N);
13940 E := Entity (N2);
13941
13942 if Present (E) then
13943
13944 -- If the node is an entry call to an entry in an enclosing task,
13945 -- it is rewritten as a selected component. No global entity to
13946 -- preserve in this case, since the expansion will be redone in
13947 -- the instance.
13948
13949 if not Nkind_In (E, N_Defining_Character_Literal,
13950 N_Defining_Identifier,
13951 N_Defining_Operator_Symbol)
13952 then
13953 Set_Associated_Node (N, Empty);
13954 Set_Etype (N, Empty);
13955 return;
13956 end if;
13957
13958 -- If the entity is an itype created as a subtype of an access
13959 -- type with a null exclusion restore source entity for proper
13960 -- visibility. The itype will be created anew in the instance.
13961
13962 if Is_Itype (E)
13963 and then Ekind (E) = E_Access_Subtype
13964 and then Is_Entity_Name (N)
13965 and then Chars (Etype (E)) = Chars (N)
13966 then
13967 E := Etype (E);
13968 Set_Entity (N2, E);
13969 Set_Etype (N2, E);
13970 end if;
13971
13972 if Is_Global (E) then
13973
13974 -- If the entity is a package renaming that is the prefix of
13975 -- an expanded name, it has been rewritten as the renamed
13976 -- package, which is necessary semantically but complicates
13977 -- ASIS tree traversal, so we recover the original entity to
13978 -- expose the renaming. Take into account that the context may
13979 -- be a nested generic, that the original node may itself have
13980 -- an associated node that had better be an entity, and that
13981 -- the current node is still a selected component.
13982
13983 if Ekind (E) = E_Package
13984 and then Nkind (N) = N_Selected_Component
13985 and then Nkind (Parent (N)) = N_Expanded_Name
13986 and then Present (Original_Node (N2))
13987 and then Is_Entity_Name (Original_Node (N2))
13988 and then Present (Entity (Original_Node (N2)))
13989 then
13990 if Is_Global (Entity (Original_Node (N2))) then
13991 N2 := Original_Node (N2);
13992 Set_Associated_Node (N, N2);
13993 Set_Global_Type (N, N2);
13994
13995 -- Renaming is local, and will be resolved in instance
13996
13997 else
13998 Set_Associated_Node (N, Empty);
13999 Set_Etype (N, Empty);
14000 end if;
14001
14002 else
14003 Set_Global_Type (N, N2);
14004 end if;
14005
14006 elsif Nkind (N) = N_Op_Concat
14007 and then Is_Generic_Type (Etype (N2))
14008 and then (Base_Type (Etype (Right_Opnd (N2))) = Etype (N2)
14009 or else
14010 Base_Type (Etype (Left_Opnd (N2))) = Etype (N2))
14011 and then Is_Intrinsic_Subprogram (E)
14012 then
14013 null;
14014
14015 -- Entity is local. Mark generic node as unresolved. Note that now
14016 -- it does not have an entity.
14017
14018 else
14019 Set_Associated_Node (N, Empty);
14020 Set_Etype (N, Empty);
14021 end if;
14022
14023 if Nkind (Parent (N)) in N_Generic_Instantiation
14024 and then N = Name (Parent (N))
14025 then
14026 Save_Global_Defaults (Parent (N), Parent (N2));
14027 end if;
14028
14029 elsif Nkind (Parent (N)) = N_Selected_Component
14030 and then Nkind (Parent (N2)) = N_Expanded_Name
14031 then
14032 if Is_Global (Entity (Parent (N2))) then
14033 Change_Selected_Component_To_Expanded_Name (Parent (N));
14034 Set_Associated_Node (Parent (N), Parent (N2));
14035 Set_Global_Type (Parent (N), Parent (N2));
14036 Save_Entity_Descendants (N);
14037
14038 -- If this is a reference to the current generic entity, replace
14039 -- by the name of the generic homonym of the current package. This
14040 -- is because in an instantiation Par.P.Q will not resolve to the
14041 -- name of the instance, whose enclosing scope is not necessarily
14042 -- Par. We use the generic homonym rather that the name of the
14043 -- generic itself because it may be hidden by a local declaration.
14044
14045 elsif In_Open_Scopes (Entity (Parent (N2)))
14046 and then not
14047 Is_Generic_Unit (Top_Ancestor (Entity (Prefix (Parent (N2)))))
14048 then
14049 if Ekind (Entity (Parent (N2))) = E_Generic_Package then
14050 Rewrite (Parent (N),
14051 Make_Identifier (Sloc (N),
14052 Chars =>
14053 Chars (Generic_Homonym (Entity (Parent (N2))))));
14054 else
14055 Rewrite (Parent (N),
14056 Make_Identifier (Sloc (N),
14057 Chars => Chars (Selector_Name (Parent (N2)))));
14058 end if;
14059 end if;
14060
14061 if Nkind (Parent (Parent (N))) in N_Generic_Instantiation
14062 and then Parent (N) = Name (Parent (Parent (N)))
14063 then
14064 Save_Global_Defaults
14065 (Parent (Parent (N)), Parent (Parent (N2)));
14066 end if;
14067
14068 -- A selected component may denote a static constant that has been
14069 -- folded. If the static constant is global to the generic, capture
14070 -- its value. Otherwise the folding will happen in any instantiation.
14071
14072 elsif Nkind (Parent (N)) = N_Selected_Component
14073 and then Nkind_In (Parent (N2), N_Integer_Literal, N_Real_Literal)
14074 then
14075 if Present (Entity (Original_Node (Parent (N2))))
14076 and then Is_Global (Entity (Original_Node (Parent (N2))))
14077 then
14078 Rewrite (Parent (N), New_Copy (Parent (N2)));
14079 Set_Analyzed (Parent (N), False);
14080 end if;
14081
14082 -- A selected component may be transformed into a parameterless
14083 -- function call. If the called entity is global, rewrite the node
14084 -- appropriately, i.e. as an extended name for the global entity.
14085
14086 elsif Nkind (Parent (N)) = N_Selected_Component
14087 and then Nkind (Parent (N2)) = N_Function_Call
14088 and then N = Selector_Name (Parent (N))
14089 then
14090 if No (Parameter_Associations (Parent (N2))) then
14091 if Is_Global (Entity (Name (Parent (N2)))) then
14092 Change_Selected_Component_To_Expanded_Name (Parent (N));
14093 Set_Associated_Node (Parent (N), Name (Parent (N2)));
14094 Set_Global_Type (Parent (N), Name (Parent (N2)));
14095 Save_Entity_Descendants (N);
14096
14097 else
14098 Set_Is_Prefixed_Call (Parent (N));
14099 Set_Associated_Node (N, Empty);
14100 Set_Etype (N, Empty);
14101 end if;
14102
14103 -- In Ada 2005, X.F may be a call to a primitive operation,
14104 -- rewritten as F (X). This rewriting will be done again in an
14105 -- instance, so keep the original node. Global entities will be
14106 -- captured as for other constructs. Indicate that this must
14107 -- resolve as a call, to prevent accidental overloading in the
14108 -- instance, if both a component and a primitive operation appear
14109 -- as candidates.
14110
14111 else
14112 Set_Is_Prefixed_Call (Parent (N));
14113 end if;
14114
14115 -- Entity is local. Reset in generic unit, so that node is resolved
14116 -- anew at the point of instantiation.
14117
14118 else
14119 Set_Associated_Node (N, Empty);
14120 Set_Etype (N, Empty);
14121 end if;
14122 end Reset_Entity;
14123
14124 -----------------------------
14125 -- Save_Entity_Descendants --
14126 -----------------------------
14127
14128 procedure Save_Entity_Descendants (N : Node_Id) is
14129 begin
14130 case Nkind (N) is
14131 when N_Binary_Op =>
14132 Save_Global_Descendant (Union_Id (Left_Opnd (N)));
14133 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
14134
14135 when N_Unary_Op =>
14136 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
14137
14138 when N_Expanded_Name |
14139 N_Selected_Component =>
14140 Save_Global_Descendant (Union_Id (Prefix (N)));
14141 Save_Global_Descendant (Union_Id (Selector_Name (N)));
14142
14143 when N_Identifier |
14144 N_Character_Literal |
14145 N_Operator_Symbol =>
14146 null;
14147
14148 when others =>
14149 raise Program_Error;
14150 end case;
14151 end Save_Entity_Descendants;
14152
14153 --------------------------
14154 -- Save_Global_Defaults --
14155 --------------------------
14156
14157 procedure Save_Global_Defaults (N1 : Node_Id; N2 : Node_Id) is
14158 Loc : constant Source_Ptr := Sloc (N1);
14159 Assoc2 : constant List_Id := Generic_Associations (N2);
14160 Gen_Id : constant Entity_Id := Get_Generic_Entity (N2);
14161 Assoc1 : List_Id;
14162 Act1 : Node_Id;
14163 Act2 : Node_Id;
14164 Def : Node_Id;
14165 Ndec : Node_Id;
14166 Subp : Entity_Id;
14167 Actual : Entity_Id;
14168
14169 begin
14170 Assoc1 := Generic_Associations (N1);
14171
14172 if Present (Assoc1) then
14173 Act1 := First (Assoc1);
14174 else
14175 Act1 := Empty;
14176 Set_Generic_Associations (N1, New_List);
14177 Assoc1 := Generic_Associations (N1);
14178 end if;
14179
14180 if Present (Assoc2) then
14181 Act2 := First (Assoc2);
14182 else
14183 return;
14184 end if;
14185
14186 while Present (Act1) and then Present (Act2) loop
14187 Next (Act1);
14188 Next (Act2);
14189 end loop;
14190
14191 -- Find the associations added for default subprograms
14192
14193 if Present (Act2) then
14194 while Nkind (Act2) /= N_Generic_Association
14195 or else No (Entity (Selector_Name (Act2)))
14196 or else not Is_Overloadable (Entity (Selector_Name (Act2)))
14197 loop
14198 Next (Act2);
14199 end loop;
14200
14201 -- Add a similar association if the default is global. The
14202 -- renaming declaration for the actual has been analyzed, and
14203 -- its alias is the program it renames. Link the actual in the
14204 -- original generic tree with the node in the analyzed tree.
14205
14206 while Present (Act2) loop
14207 Subp := Entity (Selector_Name (Act2));
14208 Def := Explicit_Generic_Actual_Parameter (Act2);
14209
14210 -- Following test is defence against rubbish errors
14211
14212 if No (Alias (Subp)) then
14213 return;
14214 end if;
14215
14216 -- Retrieve the resolved actual from the renaming declaration
14217 -- created for the instantiated formal.
14218
14219 Actual := Entity (Name (Parent (Parent (Subp))));
14220 Set_Entity (Def, Actual);
14221 Set_Etype (Def, Etype (Actual));
14222
14223 if Is_Global (Actual) then
14224 Ndec :=
14225 Make_Generic_Association (Loc,
14226 Selector_Name =>
14227 New_Occurrence_Of (Subp, Loc),
14228 Explicit_Generic_Actual_Parameter =>
14229 New_Occurrence_Of (Actual, Loc));
14230
14231 Set_Associated_Node
14232 (Explicit_Generic_Actual_Parameter (Ndec), Def);
14233
14234 Append (Ndec, Assoc1);
14235
14236 -- If there are other defaults, add a dummy association in case
14237 -- there are other defaulted formals with the same name.
14238
14239 elsif Present (Next (Act2)) then
14240 Ndec :=
14241 Make_Generic_Association (Loc,
14242 Selector_Name =>
14243 New_Occurrence_Of (Subp, Loc),
14244 Explicit_Generic_Actual_Parameter => Empty);
14245
14246 Append (Ndec, Assoc1);
14247 end if;
14248
14249 Next (Act2);
14250 end loop;
14251 end if;
14252
14253 if Nkind (Name (N1)) = N_Identifier
14254 and then Is_Child_Unit (Gen_Id)
14255 and then Is_Global (Gen_Id)
14256 and then Is_Generic_Unit (Scope (Gen_Id))
14257 and then In_Open_Scopes (Scope (Gen_Id))
14258 then
14259 -- This is an instantiation of a child unit within a sibling, so
14260 -- that the generic parent is in scope. An eventual instance must
14261 -- occur within the scope of an instance of the parent. Make name
14262 -- in instance into an expanded name, to preserve the identifier
14263 -- of the parent, so it can be resolved subsequently.
14264
14265 Rewrite (Name (N2),
14266 Make_Expanded_Name (Loc,
14267 Chars => Chars (Gen_Id),
14268 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
14269 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
14270 Set_Entity (Name (N2), Gen_Id);
14271
14272 Rewrite (Name (N1),
14273 Make_Expanded_Name (Loc,
14274 Chars => Chars (Gen_Id),
14275 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
14276 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
14277
14278 Set_Associated_Node (Name (N1), Name (N2));
14279 Set_Associated_Node (Prefix (Name (N1)), Empty);
14280 Set_Associated_Node
14281 (Selector_Name (Name (N1)), Selector_Name (Name (N2)));
14282 Set_Etype (Name (N1), Etype (Gen_Id));
14283 end if;
14284 end Save_Global_Defaults;
14285
14286 ----------------------------
14287 -- Save_Global_Descendant --
14288 ----------------------------
14289
14290 procedure Save_Global_Descendant (D : Union_Id) is
14291 N1 : Node_Id;
14292
14293 begin
14294 if D in Node_Range then
14295 if D = Union_Id (Empty) then
14296 null;
14297
14298 elsif Nkind (Node_Id (D)) /= N_Compilation_Unit then
14299 Save_References (Node_Id (D));
14300 end if;
14301
14302 elsif D in List_Range then
14303 if D = Union_Id (No_List) or else Is_Empty_List (List_Id (D)) then
14304 null;
14305
14306 else
14307 N1 := First (List_Id (D));
14308 while Present (N1) loop
14309 Save_References (N1);
14310 Next (N1);
14311 end loop;
14312 end if;
14313
14314 -- Element list or other non-node field, nothing to do
14315
14316 else
14317 null;
14318 end if;
14319 end Save_Global_Descendant;
14320
14321 ---------------------
14322 -- Save_References --
14323 ---------------------
14324
14325 -- This is the recursive procedure that does the work once the enclosing
14326 -- generic scope has been established. We have to treat specially a
14327 -- number of node rewritings that are required by semantic processing
14328 -- and which change the kind of nodes in the generic copy: typically
14329 -- constant-folding, replacing an operator node by a string literal, or
14330 -- a selected component by an expanded name. In each of those cases, the
14331 -- transformation is propagated to the generic unit.
14332
14333 procedure Save_References (N : Node_Id) is
14334 Loc : constant Source_Ptr := Sloc (N);
14335
14336 function Requires_Delayed_Save (Nod : Node_Id) return Boolean;
14337 -- Determine whether arbitrary node Nod requires delayed capture of
14338 -- global references within its aspect specifications.
14339
14340 procedure Save_References_In_Aggregate (N : Node_Id);
14341 -- Save all global references in [extension] aggregate node N
14342
14343 procedure Save_References_In_Char_Lit_Or_Op_Symbol (N : Node_Id);
14344 -- Save all global references in a character literal or operator
14345 -- symbol denoted by N.
14346
14347 procedure Save_References_In_Descendants (N : Node_Id);
14348 -- Save all global references in all descendants of node N
14349
14350 procedure Save_References_In_Identifier (N : Node_Id);
14351 -- Save all global references in identifier node N
14352
14353 procedure Save_References_In_Operator (N : Node_Id);
14354 -- Save all global references in operator node N
14355
14356 procedure Save_References_In_Pragma (Prag : Node_Id);
14357 -- Save all global references found within the expression of pragma
14358 -- Prag.
14359
14360 ---------------------------
14361 -- Requires_Delayed_Save --
14362 ---------------------------
14363
14364 function Requires_Delayed_Save (Nod : Node_Id) return Boolean is
14365 begin
14366 -- Generic packages and subprograms require delayed capture of
14367 -- global references within their aspects due to the timing of
14368 -- annotation analysis.
14369
14370 if Nkind_In (Nod, N_Generic_Package_Declaration,
14371 N_Generic_Subprogram_Declaration,
14372 N_Package_Body,
14373 N_Package_Body_Stub,
14374 N_Subprogram_Body,
14375 N_Subprogram_Body_Stub)
14376 then
14377 -- Since the capture of global references is done on the
14378 -- unanalyzed generic template, there is no information around
14379 -- to infer the context. Use the Associated_Entity linkages to
14380 -- peek into the analyzed generic copy and determine what the
14381 -- template corresponds to.
14382
14383 if Nod = Templ then
14384 return
14385 Is_Generic_Declaration_Or_Body
14386 (Unit_Declaration_Node
14387 (Associated_Entity (Defining_Entity (Nod))));
14388
14389 -- Otherwise the generic unit being processed is not the top
14390 -- level template. It is safe to capture of global references
14391 -- within the generic unit because at this point the top level
14392 -- copy is fully analyzed.
14393
14394 else
14395 return False;
14396 end if;
14397
14398 -- Otherwise capture the global references without interference
14399
14400 else
14401 return False;
14402 end if;
14403 end Requires_Delayed_Save;
14404
14405 ----------------------------------
14406 -- Save_References_In_Aggregate --
14407 ----------------------------------
14408
14409 procedure Save_References_In_Aggregate (N : Node_Id) is
14410 Nam : Node_Id;
14411 Qual : Node_Id := Empty;
14412 Typ : Entity_Id := Empty;
14413
14414 use Atree.Unchecked_Access;
14415 -- This code section is part of implementing an untyped tree
14416 -- traversal, so it needs direct access to node fields.
14417
14418 begin
14419 N2 := Get_Associated_Node (N);
14420
14421 if Present (N2) then
14422 Typ := Etype (N2);
14423
14424 -- In an instance within a generic, use the name of the actual
14425 -- and not the original generic parameter. If the actual is
14426 -- global in the current generic it must be preserved for its
14427 -- instantiation.
14428
14429 if Nkind (Parent (Typ)) = N_Subtype_Declaration
14430 and then Present (Generic_Parent_Type (Parent (Typ)))
14431 then
14432 Typ := Base_Type (Typ);
14433 Set_Etype (N2, Typ);
14434 end if;
14435 end if;
14436
14437 if No (N2) or else No (Typ) or else not Is_Global (Typ) then
14438 Set_Associated_Node (N, Empty);
14439
14440 -- If the aggregate is an actual in a call, it has been
14441 -- resolved in the current context, to some local type. The
14442 -- enclosing call may have been disambiguated by the aggregate,
14443 -- and this disambiguation might fail at instantiation time
14444 -- because the type to which the aggregate did resolve is not
14445 -- preserved. In order to preserve some of this information,
14446 -- wrap the aggregate in a qualified expression, using the id
14447 -- of its type. For further disambiguation we qualify the type
14448 -- name with its scope (if visible) because both id's will have
14449 -- corresponding entities in an instance. This resolves most of
14450 -- the problems with missing type information on aggregates in
14451 -- instances.
14452
14453 if Present (N2)
14454 and then Nkind (N2) = Nkind (N)
14455 and then Nkind (Parent (N2)) in N_Subprogram_Call
14456 and then Present (Typ)
14457 and then Comes_From_Source (Typ)
14458 then
14459 Nam := Make_Identifier (Loc, Chars (Typ));
14460
14461 if Is_Immediately_Visible (Scope (Typ)) then
14462 Nam :=
14463 Make_Selected_Component (Loc,
14464 Prefix =>
14465 Make_Identifier (Loc, Chars (Scope (Typ))),
14466 Selector_Name => Nam);
14467 end if;
14468
14469 Qual :=
14470 Make_Qualified_Expression (Loc,
14471 Subtype_Mark => Nam,
14472 Expression => Relocate_Node (N));
14473 end if;
14474 end if;
14475
14476 Save_Global_Descendant (Field1 (N));
14477 Save_Global_Descendant (Field2 (N));
14478 Save_Global_Descendant (Field3 (N));
14479 Save_Global_Descendant (Field5 (N));
14480
14481 if Present (Qual) then
14482 Rewrite (N, Qual);
14483 end if;
14484 end Save_References_In_Aggregate;
14485
14486 ----------------------------------------------
14487 -- Save_References_In_Char_Lit_Or_Op_Symbol --
14488 ----------------------------------------------
14489
14490 procedure Save_References_In_Char_Lit_Or_Op_Symbol (N : Node_Id) is
14491 begin
14492 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
14493 Reset_Entity (N);
14494
14495 elsif Nkind (N) = N_Operator_Symbol
14496 and then Nkind (Get_Associated_Node (N)) = N_String_Literal
14497 then
14498 Change_Operator_Symbol_To_String_Literal (N);
14499 end if;
14500 end Save_References_In_Char_Lit_Or_Op_Symbol;
14501
14502 ------------------------------------
14503 -- Save_References_In_Descendants --
14504 ------------------------------------
14505
14506 procedure Save_References_In_Descendants (N : Node_Id) is
14507 use Atree.Unchecked_Access;
14508 -- This code section is part of implementing an untyped tree
14509 -- traversal, so it needs direct access to node fields.
14510
14511 begin
14512 Save_Global_Descendant (Field1 (N));
14513 Save_Global_Descendant (Field2 (N));
14514 Save_Global_Descendant (Field3 (N));
14515 Save_Global_Descendant (Field4 (N));
14516 Save_Global_Descendant (Field5 (N));
14517 end Save_References_In_Descendants;
14518
14519 -----------------------------------
14520 -- Save_References_In_Identifier --
14521 -----------------------------------
14522
14523 procedure Save_References_In_Identifier (N : Node_Id) is
14524 begin
14525 -- The node did not undergo a transformation
14526
14527 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
14528
14529 -- If this is a discriminant reference, always save it. It is
14530 -- used in the instance to find the corresponding discriminant
14531 -- positionally rather than by name.
14532
14533 Set_Original_Discriminant
14534 (N, Original_Discriminant (Get_Associated_Node (N)));
14535 Reset_Entity (N);
14536
14537 -- The analysis of the generic copy transformed the identifier
14538 -- into another construct. Propagate the changes to the template.
14539
14540 else
14541 N2 := Get_Associated_Node (N);
14542
14543 -- The identifier denotes a call to a parameterless function.
14544 -- Mark the node as resolved when the function is external.
14545
14546 if Nkind (N2) = N_Function_Call then
14547 E := Entity (Name (N2));
14548
14549 if Present (E) and then Is_Global (E) then
14550 Set_Etype (N, Etype (N2));
14551 else
14552 Set_Associated_Node (N, Empty);
14553 Set_Etype (N, Empty);
14554 end if;
14555
14556 -- The identifier denotes a named number that was constant
14557 -- folded. Preserve the original name for ASIS and undo the
14558 -- constant folding which will be repeated in the instance.
14559
14560 elsif Nkind_In (N2, N_Integer_Literal, N_Real_Literal)
14561 and then Is_Entity_Name (Original_Node (N2))
14562 then
14563 Set_Associated_Node (N, Original_Node (N2));
14564 Reset_Entity (N);
14565
14566 -- The identifier resolved to a string literal. Propagate this
14567 -- information to the generic template.
14568
14569 elsif Nkind (N2) = N_String_Literal then
14570 Rewrite (N, New_Copy (N2));
14571
14572 -- The identifier is rewritten as a dereference if it is the
14573 -- prefix of an implicit dereference. Preserve the original
14574 -- tree as the analysis of the instance will expand the node
14575 -- again, but preserve the resolved entity if it is global.
14576
14577 elsif Nkind (N2) = N_Explicit_Dereference then
14578 if Is_Entity_Name (Prefix (N2))
14579 and then Present (Entity (Prefix (N2)))
14580 and then Is_Global (Entity (Prefix (N2)))
14581 then
14582 Set_Associated_Node (N, Prefix (N2));
14583
14584 elsif Nkind (Prefix (N2)) = N_Function_Call
14585 and then Present (Entity (Name (Prefix (N2))))
14586 and then Is_Global (Entity (Name (Prefix (N2))))
14587 then
14588 Rewrite (N,
14589 Make_Explicit_Dereference (Loc,
14590 Prefix =>
14591 Make_Function_Call (Loc,
14592 Name =>
14593 New_Occurrence_Of
14594 (Entity (Name (Prefix (N2))), Loc))));
14595
14596 else
14597 Set_Associated_Node (N, Empty);
14598 Set_Etype (N, Empty);
14599 end if;
14600
14601 -- The subtype mark of a nominally unconstrained object is
14602 -- rewritten as a subtype indication using the bounds of the
14603 -- expression. Recover the original subtype mark.
14604
14605 elsif Nkind (N2) = N_Subtype_Indication
14606 and then Is_Entity_Name (Original_Node (N2))
14607 then
14608 Set_Associated_Node (N, Original_Node (N2));
14609 Reset_Entity (N);
14610 end if;
14611 end if;
14612 end Save_References_In_Identifier;
14613
14614 ---------------------------------
14615 -- Save_References_In_Operator --
14616 ---------------------------------
14617
14618 procedure Save_References_In_Operator (N : Node_Id) is
14619 begin
14620 -- The node did not undergo a transformation
14621
14622 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
14623 if Nkind (N) = N_Op_Concat then
14624 Set_Is_Component_Left_Opnd (N,
14625 Is_Component_Left_Opnd (Get_Associated_Node (N)));
14626
14627 Set_Is_Component_Right_Opnd (N,
14628 Is_Component_Right_Opnd (Get_Associated_Node (N)));
14629 end if;
14630
14631 Reset_Entity (N);
14632
14633 -- The analysis of the generic copy transformed the operator into
14634 -- some other construct. Propagate the changes to the template.
14635
14636 else
14637 N2 := Get_Associated_Node (N);
14638
14639 -- The operator resoved to a function call
14640
14641 if Nkind (N2) = N_Function_Call then
14642 E := Entity (Name (N2));
14643
14644 if Present (E) and then Is_Global (E) then
14645 Set_Etype (N, Etype (N2));
14646 else
14647 Set_Associated_Node (N, Empty);
14648 Set_Etype (N, Empty);
14649 end if;
14650
14651 -- The operator was folded into a literal
14652
14653 elsif Nkind_In (N2, N_Integer_Literal,
14654 N_Real_Literal,
14655 N_String_Literal)
14656 then
14657 if Present (Original_Node (N2))
14658 and then Nkind (Original_Node (N2)) = Nkind (N)
14659 then
14660 -- Operation was constant-folded. Whenever possible,
14661 -- recover semantic information from unfolded node,
14662 -- for ASIS use.
14663
14664 Set_Associated_Node (N, Original_Node (N2));
14665
14666 if Nkind (N) = N_Op_Concat then
14667 Set_Is_Component_Left_Opnd (N,
14668 Is_Component_Left_Opnd (Get_Associated_Node (N)));
14669 Set_Is_Component_Right_Opnd (N,
14670 Is_Component_Right_Opnd (Get_Associated_Node (N)));
14671 end if;
14672
14673 Reset_Entity (N);
14674
14675 -- Propagate the constant folding back to the template
14676
14677 else
14678 Rewrite (N, New_Copy (N2));
14679 Set_Analyzed (N, False);
14680 end if;
14681
14682 -- The operator was folded into an enumeration literal. Retain
14683 -- the entity to avoid spurious ambiguities if it is overloaded
14684 -- at the point of instantiation or inlining.
14685
14686 elsif Nkind (N2) = N_Identifier
14687 and then Ekind (Entity (N2)) = E_Enumeration_Literal
14688 then
14689 Rewrite (N, New_Copy (N2));
14690 Set_Analyzed (N, False);
14691 end if;
14692 end if;
14693
14694 -- Complete the operands check if node has not been constant
14695 -- folded.
14696
14697 if Nkind (N) in N_Op then
14698 Save_Entity_Descendants (N);
14699 end if;
14700 end Save_References_In_Operator;
14701
14702 -------------------------------
14703 -- Save_References_In_Pragma --
14704 -------------------------------
14705
14706 procedure Save_References_In_Pragma (Prag : Node_Id) is
14707 Context : Node_Id;
14708 Do_Save : Boolean := True;
14709
14710 use Atree.Unchecked_Access;
14711 -- This code section is part of implementing an untyped tree
14712 -- traversal, so it needs direct access to node fields.
14713
14714 begin
14715 -- Do not save global references in pragmas generated from aspects
14716 -- because the pragmas will be regenerated at instantiation time.
14717
14718 if From_Aspect_Specification (Prag) then
14719 Do_Save := False;
14720
14721 -- The capture of global references within contract-related source
14722 -- pragmas associated with generic packages, subprograms or their
14723 -- respective bodies must be delayed due to timing of annotation
14724 -- analysis. Global references are still captured in routine
14725 -- Save_Global_References_In_Contract.
14726
14727 elsif Is_Generic_Contract_Pragma (Prag) and then Prag /= Templ then
14728 if Is_Package_Contract_Annotation (Prag) then
14729 Context := Find_Related_Package_Or_Body (Prag);
14730
14731 else
14732 pragma Assert (Is_Subprogram_Contract_Annotation (Prag));
14733 Context := Find_Related_Subprogram_Or_Body (Prag);
14734 end if;
14735
14736 -- The use of Original_Node accounts for the case when the
14737 -- related context is generic template.
14738
14739 if Requires_Delayed_Save (Original_Node (Context)) then
14740 Do_Save := False;
14741 end if;
14742 end if;
14743
14744 -- For all other cases, save all global references within the
14745 -- descendants, but skip the following semantic fields:
14746
14747 -- Field1 - Next_Pragma
14748 -- Field3 - Corresponding_Aspect
14749 -- Field5 - Next_Rep_Item
14750
14751 if Do_Save then
14752 Save_Global_Descendant (Field2 (Prag));
14753 Save_Global_Descendant (Field4 (Prag));
14754 end if;
14755 end Save_References_In_Pragma;
14756
14757 -- Start of processing for Save_References
14758
14759 begin
14760 if N = Empty then
14761 null;
14762
14763 -- Aggregates
14764
14765 elsif Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
14766 Save_References_In_Aggregate (N);
14767
14768 -- Character literals, operator symbols
14769
14770 elsif Nkind_In (N, N_Character_Literal, N_Operator_Symbol) then
14771 Save_References_In_Char_Lit_Or_Op_Symbol (N);
14772
14773 -- Defining identifiers
14774
14775 elsif Nkind (N) in N_Entity then
14776 null;
14777
14778 -- Identifiers
14779
14780 elsif Nkind (N) = N_Identifier then
14781 Save_References_In_Identifier (N);
14782
14783 -- Operators
14784
14785 elsif Nkind (N) in N_Op then
14786 Save_References_In_Operator (N);
14787
14788 -- Pragmas
14789
14790 elsif Nkind (N) = N_Pragma then
14791 Save_References_In_Pragma (N);
14792
14793 else
14794 Save_References_In_Descendants (N);
14795 end if;
14796
14797 -- Save all global references found within the aspect specifications
14798 -- of the related node.
14799
14800 if Permits_Aspect_Specifications (N) and then Has_Aspects (N) then
14801
14802 -- The capture of global references within aspects associated with
14803 -- generic packages, subprograms or their bodies must be delayed
14804 -- due to timing of annotation analysis. Global references are
14805 -- still captured in routine Save_Global_References_In_Contract.
14806
14807 if Requires_Delayed_Save (N) then
14808 null;
14809
14810 -- Otherwise save all global references within the aspects
14811
14812 else
14813 Save_Global_References_In_Aspects (N);
14814 end if;
14815 end if;
14816 end Save_References;
14817
14818 -- Start of processing for Save_Global_References
14819
14820 begin
14821 Gen_Scope := Current_Scope;
14822
14823 -- If the generic unit is a child unit, references to entities in the
14824 -- parent are treated as local, because they will be resolved anew in
14825 -- the context of the instance of the parent.
14826
14827 while Is_Child_Unit (Gen_Scope)
14828 and then Ekind (Scope (Gen_Scope)) = E_Generic_Package
14829 loop
14830 Gen_Scope := Scope (Gen_Scope);
14831 end loop;
14832
14833 Save_References (Templ);
14834 end Save_Global_References;
14835
14836 ---------------------------------------
14837 -- Save_Global_References_In_Aspects --
14838 ---------------------------------------
14839
14840 procedure Save_Global_References_In_Aspects (N : Node_Id) is
14841 Asp : Node_Id;
14842 Expr : Node_Id;
14843
14844 begin
14845 Asp := First (Aspect_Specifications (N));
14846 while Present (Asp) loop
14847 Expr := Expression (Asp);
14848
14849 if Present (Expr) then
14850 Save_Global_References (Expr);
14851 end if;
14852
14853 Next (Asp);
14854 end loop;
14855 end Save_Global_References_In_Aspects;
14856
14857 ----------------------------------------
14858 -- Save_Global_References_In_Contract --
14859 ----------------------------------------
14860
14861 procedure Save_Global_References_In_Contract
14862 (Templ : Node_Id;
14863 Gen_Id : Entity_Id)
14864 is
14865 procedure Save_Global_References_In_List (First_Prag : Node_Id);
14866 -- Save all global references in contract-related source pragmas found
14867 -- in the list starting with pragma First_Prag.
14868
14869 ------------------------------------
14870 -- Save_Global_References_In_List --
14871 ------------------------------------
14872
14873 procedure Save_Global_References_In_List (First_Prag : Node_Id) is
14874 Prag : Node_Id;
14875
14876 begin
14877 Prag := First_Prag;
14878 while Present (Prag) loop
14879 if Is_Generic_Contract_Pragma (Prag) then
14880 Save_Global_References (Prag);
14881 end if;
14882
14883 Prag := Next_Pragma (Prag);
14884 end loop;
14885 end Save_Global_References_In_List;
14886
14887 -- Local variables
14888
14889 Items : constant Node_Id := Contract (Defining_Entity (Templ));
14890
14891 -- Start of processing for Save_Global_References_In_Contract
14892
14893 begin
14894 -- The entity of the analyzed generic copy must be on the scope stack
14895 -- to ensure proper detection of global references.
14896
14897 Push_Scope (Gen_Id);
14898
14899 if Permits_Aspect_Specifications (Templ)
14900 and then Has_Aspects (Templ)
14901 then
14902 Save_Global_References_In_Aspects (Templ);
14903 end if;
14904
14905 if Present (Items) then
14906 Save_Global_References_In_List (Pre_Post_Conditions (Items));
14907 Save_Global_References_In_List (Contract_Test_Cases (Items));
14908 Save_Global_References_In_List (Classifications (Items));
14909 end if;
14910
14911 Pop_Scope;
14912 end Save_Global_References_In_Contract;
14913
14914 --------------------------------------
14915 -- Set_Copied_Sloc_For_Inlined_Body --
14916 --------------------------------------
14917
14918 procedure Set_Copied_Sloc_For_Inlined_Body (N : Node_Id; E : Entity_Id) is
14919 begin
14920 Create_Instantiation_Source (N, E, True, S_Adjustment);
14921 end Set_Copied_Sloc_For_Inlined_Body;
14922
14923 ---------------------
14924 -- Set_Instance_Of --
14925 ---------------------
14926
14927 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id) is
14928 begin
14929 Generic_Renamings.Table (Generic_Renamings.Last) := (A, B, Assoc_Null);
14930 Generic_Renamings_HTable.Set (Generic_Renamings.Last);
14931 Generic_Renamings.Increment_Last;
14932 end Set_Instance_Of;
14933
14934 --------------------
14935 -- Set_Next_Assoc --
14936 --------------------
14937
14938 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr) is
14939 begin
14940 Generic_Renamings.Table (E).Next_In_HTable := Next;
14941 end Set_Next_Assoc;
14942
14943 -------------------
14944 -- Start_Generic --
14945 -------------------
14946
14947 procedure Start_Generic is
14948 begin
14949 -- ??? More things could be factored out in this routine.
14950 -- Should probably be done at a later stage.
14951
14952 Generic_Flags.Append (Inside_A_Generic);
14953 Inside_A_Generic := True;
14954
14955 Expander_Mode_Save_And_Set (False);
14956 end Start_Generic;
14957
14958 ----------------------
14959 -- Set_Instance_Env --
14960 ----------------------
14961
14962 procedure Set_Instance_Env
14963 (Gen_Unit : Entity_Id;
14964 Act_Unit : Entity_Id)
14965 is
14966 Assertion_Status : constant Boolean := Assertions_Enabled;
14967 Save_SPARK_Mode : constant SPARK_Mode_Type := SPARK_Mode;
14968 Save_SPARK_Mode_Pragma : constant Node_Id := SPARK_Mode_Pragma;
14969
14970 begin
14971 -- Regardless of the current mode, predefined units are analyzed in the
14972 -- most current Ada mode, and earlier version Ada checks do not apply
14973 -- to predefined units. Nothing needs to be done for non-internal units.
14974 -- These are always analyzed in the current mode.
14975
14976 if Is_Internal_File_Name
14977 (Fname => Unit_File_Name (Get_Source_Unit (Gen_Unit)),
14978 Renamings_Included => True)
14979 then
14980 Set_Opt_Config_Switches (True, Current_Sem_Unit = Main_Unit);
14981
14982 -- In Ada2012 we may want to enable assertions in an instance of a
14983 -- predefined unit, in which case we need to preserve the current
14984 -- setting for the Assertions_Enabled flag. This will become more
14985 -- critical when pre/postconditions are added to predefined units,
14986 -- as is already the case for some numeric libraries.
14987
14988 if Ada_Version >= Ada_2012 then
14989 Assertions_Enabled := Assertion_Status;
14990 end if;
14991
14992 -- SPARK_Mode for an instance is the one applicable at the point of
14993 -- instantiation.
14994
14995 SPARK_Mode := Save_SPARK_Mode;
14996 SPARK_Mode_Pragma := Save_SPARK_Mode_Pragma;
14997
14998 -- Make sure dynamic elaboration checks are off in SPARK Mode
14999
15000 if SPARK_Mode = On then
15001 Dynamic_Elaboration_Checks := False;
15002 end if;
15003 end if;
15004
15005 Current_Instantiated_Parent :=
15006 (Gen_Id => Gen_Unit,
15007 Act_Id => Act_Unit,
15008 Next_In_HTable => Assoc_Null);
15009 end Set_Instance_Env;
15010
15011 -----------------
15012 -- Switch_View --
15013 -----------------
15014
15015 procedure Switch_View (T : Entity_Id) is
15016 BT : constant Entity_Id := Base_Type (T);
15017 Priv_Elmt : Elmt_Id := No_Elmt;
15018 Priv_Sub : Entity_Id;
15019
15020 begin
15021 -- T may be private but its base type may have been exchanged through
15022 -- some other occurrence, in which case there is nothing to switch
15023 -- besides T itself. Note that a private dependent subtype of a private
15024 -- type might not have been switched even if the base type has been,
15025 -- because of the last branch of Check_Private_View (see comment there).
15026
15027 if not Is_Private_Type (BT) then
15028 Prepend_Elmt (Full_View (T), Exchanged_Views);
15029 Exchange_Declarations (T);
15030 return;
15031 end if;
15032
15033 Priv_Elmt := First_Elmt (Private_Dependents (BT));
15034
15035 if Present (Full_View (BT)) then
15036 Prepend_Elmt (Full_View (BT), Exchanged_Views);
15037 Exchange_Declarations (BT);
15038 end if;
15039
15040 while Present (Priv_Elmt) loop
15041 Priv_Sub := (Node (Priv_Elmt));
15042
15043 -- We avoid flipping the subtype if the Etype of its full view is
15044 -- private because this would result in a malformed subtype. This
15045 -- occurs when the Etype of the subtype full view is the full view of
15046 -- the base type (and since the base types were just switched, the
15047 -- subtype is pointing to the wrong view). This is currently the case
15048 -- for tagged record types, access types (maybe more?) and needs to
15049 -- be resolved. ???
15050
15051 if Present (Full_View (Priv_Sub))
15052 and then not Is_Private_Type (Etype (Full_View (Priv_Sub)))
15053 then
15054 Prepend_Elmt (Full_View (Priv_Sub), Exchanged_Views);
15055 Exchange_Declarations (Priv_Sub);
15056 end if;
15057
15058 Next_Elmt (Priv_Elmt);
15059 end loop;
15060 end Switch_View;
15061
15062 -----------------
15063 -- True_Parent --
15064 -----------------
15065
15066 function True_Parent (N : Node_Id) return Node_Id is
15067 begin
15068 if Nkind (Parent (N)) = N_Subunit then
15069 return Parent (Corresponding_Stub (Parent (N)));
15070 else
15071 return Parent (N);
15072 end if;
15073 end True_Parent;
15074
15075 -----------------------------
15076 -- Valid_Default_Attribute --
15077 -----------------------------
15078
15079 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id) is
15080 Attr_Id : constant Attribute_Id :=
15081 Get_Attribute_Id (Attribute_Name (Def));
15082 T : constant Entity_Id := Entity (Prefix (Def));
15083 Is_Fun : constant Boolean := (Ekind (Nam) = E_Function);
15084 F : Entity_Id;
15085 Num_F : Int;
15086 OK : Boolean;
15087
15088 begin
15089 if No (T) or else T = Any_Id then
15090 return;
15091 end if;
15092
15093 Num_F := 0;
15094 F := First_Formal (Nam);
15095 while Present (F) loop
15096 Num_F := Num_F + 1;
15097 Next_Formal (F);
15098 end loop;
15099
15100 case Attr_Id is
15101 when Attribute_Adjacent | Attribute_Ceiling | Attribute_Copy_Sign |
15102 Attribute_Floor | Attribute_Fraction | Attribute_Machine |
15103 Attribute_Model | Attribute_Remainder | Attribute_Rounding |
15104 Attribute_Unbiased_Rounding =>
15105 OK := Is_Fun
15106 and then Num_F = 1
15107 and then Is_Floating_Point_Type (T);
15108
15109 when Attribute_Image | Attribute_Pred | Attribute_Succ |
15110 Attribute_Value | Attribute_Wide_Image |
15111 Attribute_Wide_Value =>
15112 OK := (Is_Fun and then Num_F = 1 and then Is_Scalar_Type (T));
15113
15114 when Attribute_Max | Attribute_Min =>
15115 OK := (Is_Fun and then Num_F = 2 and then Is_Scalar_Type (T));
15116
15117 when Attribute_Input =>
15118 OK := (Is_Fun and then Num_F = 1);
15119
15120 when Attribute_Output | Attribute_Read | Attribute_Write =>
15121 OK := (not Is_Fun and then Num_F = 2);
15122
15123 when others =>
15124 OK := False;
15125 end case;
15126
15127 if not OK then
15128 Error_Msg_N
15129 ("attribute reference has wrong profile for subprogram", Def);
15130 end if;
15131 end Valid_Default_Attribute;
15132
15133 end Sem_Ch12;