[multiple changes]
[gcc.git] / gcc / ada / sem_ch12.adb
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S E M _ C H 1 2 --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2016, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
25
26 with Aspects; use Aspects;
27 with Atree; use Atree;
28 with Contracts; use Contracts;
29 with Einfo; use Einfo;
30 with Elists; use Elists;
31 with Errout; use Errout;
32 with Expander; use Expander;
33 with Exp_Disp; use Exp_Disp;
34 with Fname; use Fname;
35 with Fname.UF; use Fname.UF;
36 with Freeze; use Freeze;
37 with Ghost; use Ghost;
38 with Itypes; use Itypes;
39 with Lib; use Lib;
40 with Lib.Load; use Lib.Load;
41 with Lib.Xref; use Lib.Xref;
42 with Nlists; use Nlists;
43 with Namet; use Namet;
44 with Nmake; use Nmake;
45 with Opt; use Opt;
46 with Rident; use Rident;
47 with Restrict; use Restrict;
48 with Rtsfind; use Rtsfind;
49 with Sem; use Sem;
50 with Sem_Aux; use Sem_Aux;
51 with Sem_Cat; use Sem_Cat;
52 with Sem_Ch3; use Sem_Ch3;
53 with Sem_Ch6; use Sem_Ch6;
54 with Sem_Ch7; use Sem_Ch7;
55 with Sem_Ch8; use Sem_Ch8;
56 with Sem_Ch10; use Sem_Ch10;
57 with Sem_Ch13; use Sem_Ch13;
58 with Sem_Dim; use Sem_Dim;
59 with Sem_Disp; use Sem_Disp;
60 with Sem_Elab; use Sem_Elab;
61 with Sem_Elim; use Sem_Elim;
62 with Sem_Eval; use Sem_Eval;
63 with Sem_Prag; use Sem_Prag;
64 with Sem_Res; use Sem_Res;
65 with Sem_Type; use Sem_Type;
66 with Sem_Util; use Sem_Util;
67 with Sem_Warn; use Sem_Warn;
68 with Stand; use Stand;
69 with Sinfo; use Sinfo;
70 with Sinfo.CN; use Sinfo.CN;
71 with Sinput; use Sinput;
72 with Sinput.L; use Sinput.L;
73 with Snames; use Snames;
74 with Stringt; use Stringt;
75 with Uname; use Uname;
76 with Table;
77 with Tbuild; use Tbuild;
78 with Uintp; use Uintp;
79 with Urealp; use Urealp;
80 with Warnsw; use Warnsw;
81
82 with GNAT.HTable;
83
84 package body Sem_Ch12 is
85
86 ----------------------------------------------------------
87 -- Implementation of Generic Analysis and Instantiation --
88 ----------------------------------------------------------
89
90 -- GNAT implements generics by macro expansion. No attempt is made to share
91 -- generic instantiations (for now). Analysis of a generic definition does
92 -- not perform any expansion action, but the expander must be called on the
93 -- tree for each instantiation, because the expansion may of course depend
94 -- on the generic actuals. All of this is best achieved as follows:
95 --
96 -- a) Semantic analysis of a generic unit is performed on a copy of the
97 -- tree for the generic unit. All tree modifications that follow analysis
98 -- do not affect the original tree. Links are kept between the original
99 -- tree and the copy, in order to recognize non-local references within
100 -- the generic, and propagate them to each instance (recall that name
101 -- resolution is done on the generic declaration: generics are not really
102 -- macros). This is summarized in the following diagram:
103
104 -- .-----------. .----------.
105 -- | semantic |<--------------| generic |
106 -- | copy | | unit |
107 -- | |==============>| |
108 -- |___________| global |__________|
109 -- references | | |
110 -- | | |
111 -- .-----|--|.
112 -- | .-----|---.
113 -- | | .----------.
114 -- | | | generic |
115 -- |__| | |
116 -- |__| instance |
117 -- |__________|
118
119 -- b) Each instantiation copies the original tree, and inserts into it a
120 -- series of declarations that describe the mapping between generic formals
121 -- and actuals. For example, a generic In OUT parameter is an object
122 -- renaming of the corresponding actual, etc. Generic IN parameters are
123 -- constant declarations.
124
125 -- c) In order to give the right visibility for these renamings, we use
126 -- a different scheme for package and subprogram instantiations. For
127 -- packages, the list of renamings is inserted into the package
128 -- specification, before the visible declarations of the package. The
129 -- renamings are analyzed before any of the text of the instance, and are
130 -- thus visible at the right place. Furthermore, outside of the instance,
131 -- the generic parameters are visible and denote their corresponding
132 -- actuals.
133
134 -- For subprograms, we create a container package to hold the renamings
135 -- and the subprogram instance itself. Analysis of the package makes the
136 -- renaming declarations visible to the subprogram. After analyzing the
137 -- package, the defining entity for the subprogram is touched-up so that
138 -- it appears declared in the current scope, and not inside the container
139 -- package.
140
141 -- If the instantiation is a compilation unit, the container package is
142 -- given the same name as the subprogram instance. This ensures that
143 -- the elaboration procedure called by the binder, using the compilation
144 -- unit name, calls in fact the elaboration procedure for the package.
145
146 -- Not surprisingly, private types complicate this approach. By saving in
147 -- the original generic object the non-local references, we guarantee that
148 -- the proper entities are referenced at the point of instantiation.
149 -- However, for private types, this by itself does not insure that the
150 -- proper VIEW of the entity is used (the full type may be visible at the
151 -- point of generic definition, but not at instantiation, or vice-versa).
152 -- In order to reference the proper view, we special-case any reference
153 -- to private types in the generic object, by saving both views, one in
154 -- the generic and one in the semantic copy. At time of instantiation, we
155 -- check whether the two views are consistent, and exchange declarations if
156 -- necessary, in order to restore the correct visibility. Similarly, if
157 -- the instance view is private when the generic view was not, we perform
158 -- the exchange. After completing the instantiation, we restore the
159 -- current visibility. The flag Has_Private_View marks identifiers in the
160 -- the generic unit that require checking.
161
162 -- Visibility within nested generic units requires special handling.
163 -- Consider the following scheme:
164
165 -- type Global is ... -- outside of generic unit.
166 -- generic ...
167 -- package Outer is
168 -- ...
169 -- type Semi_Global is ... -- global to inner.
170
171 -- generic ... -- 1
172 -- procedure inner (X1 : Global; X2 : Semi_Global);
173
174 -- procedure in2 is new inner (...); -- 4
175 -- end Outer;
176
177 -- package New_Outer is new Outer (...); -- 2
178 -- procedure New_Inner is new New_Outer.Inner (...); -- 3
179
180 -- The semantic analysis of Outer captures all occurrences of Global.
181 -- The semantic analysis of Inner (at 1) captures both occurrences of
182 -- Global and Semi_Global.
183
184 -- At point 2 (instantiation of Outer), we also produce a generic copy
185 -- of Inner, even though Inner is, at that point, not being instantiated.
186 -- (This is just part of the semantic analysis of New_Outer).
187
188 -- Critically, references to Global within Inner must be preserved, while
189 -- references to Semi_Global should not preserved, because they must now
190 -- resolve to an entity within New_Outer. To distinguish between these, we
191 -- use a global variable, Current_Instantiated_Parent, which is set when
192 -- performing a generic copy during instantiation (at 2). This variable is
193 -- used when performing a generic copy that is not an instantiation, but
194 -- that is nested within one, as the occurrence of 1 within 2. The analysis
195 -- of a nested generic only preserves references that are global to the
196 -- enclosing Current_Instantiated_Parent. We use the Scope_Depth value to
197 -- determine whether a reference is external to the given parent.
198
199 -- The instantiation at point 3 requires no special treatment. The method
200 -- works as well for further nestings of generic units, but of course the
201 -- variable Current_Instantiated_Parent must be stacked because nested
202 -- instantiations can occur, e.g. the occurrence of 4 within 2.
203
204 -- The instantiation of package and subprogram bodies is handled in a
205 -- similar manner, except that it is delayed until after semantic
206 -- analysis is complete. In this fashion complex cross-dependencies
207 -- between several package declarations and bodies containing generics
208 -- can be compiled which otherwise would diagnose spurious circularities.
209
210 -- For example, it is possible to compile two packages A and B that
211 -- have the following structure:
212
213 -- package A is package B is
214 -- generic ... generic ...
215 -- package G_A is package G_B is
216
217 -- with B; with A;
218 -- package body A is package body B is
219 -- package N_B is new G_B (..) package N_A is new G_A (..)
220
221 -- The table Pending_Instantiations in package Inline is used to keep
222 -- track of body instantiations that are delayed in this manner. Inline
223 -- handles the actual calls to do the body instantiations. This activity
224 -- is part of Inline, since the processing occurs at the same point, and
225 -- for essentially the same reason, as the handling of inlined routines.
226
227 ----------------------------------------------
228 -- Detection of Instantiation Circularities --
229 ----------------------------------------------
230
231 -- If we have a chain of instantiations that is circular, this is static
232 -- error which must be detected at compile time. The detection of these
233 -- circularities is carried out at the point that we insert a generic
234 -- instance spec or body. If there is a circularity, then the analysis of
235 -- the offending spec or body will eventually result in trying to load the
236 -- same unit again, and we detect this problem as we analyze the package
237 -- instantiation for the second time.
238
239 -- At least in some cases after we have detected the circularity, we get
240 -- into trouble if we try to keep going. The following flag is set if a
241 -- circularity is detected, and used to abandon compilation after the
242 -- messages have been posted.
243
244 -----------------------------------------
245 -- Implementation of Generic Contracts --
246 -----------------------------------------
247
248 -- A "contract" is a collection of aspects and pragmas that either verify a
249 -- property of a construct at runtime or classify the data flow to and from
250 -- the construct in some fashion.
251
252 -- Generic packages, subprograms and their respective bodies may be subject
253 -- to the following contract-related aspects or pragmas collectively known
254 -- as annotations:
255
256 -- package subprogram [body]
257 -- Abstract_State Contract_Cases
258 -- Initial_Condition Depends
259 -- Initializes Extensions_Visible
260 -- Global
261 -- package body Post
262 -- Refined_State Post_Class
263 -- Postcondition
264 -- Pre
265 -- Pre_Class
266 -- Precondition
267 -- Refined_Depends
268 -- Refined_Global
269 -- Refined_Post
270 -- Test_Case
271
272 -- Most package contract annotations utilize forward references to classify
273 -- data declared within the package [body]. Subprogram annotations then use
274 -- the classifications to further refine them. These inter dependencies are
275 -- problematic with respect to the implementation of generics because their
276 -- analysis, capture of global references and instantiation does not mesh
277 -- well with the existing mechanism.
278
279 -- 1) Analysis of generic contracts is carried out the same way non-generic
280 -- contracts are analyzed:
281
282 -- 1.1) General rule - a contract is analyzed after all related aspects
283 -- and pragmas are analyzed. This is done by routines
284
285 -- Analyze_Package_Body_Contract
286 -- Analyze_Package_Contract
287 -- Analyze_Subprogram_Body_Contract
288 -- Analyze_Subprogram_Contract
289
290 -- 1.2) Compilation unit - the contract is analyzed after Pragmas_After
291 -- are processed.
292
293 -- 1.3) Compilation unit body - the contract is analyzed at the end of
294 -- the body declaration list.
295
296 -- 1.4) Package - the contract is analyzed at the end of the private or
297 -- visible declarations, prior to analyzing the contracts of any nested
298 -- packages or subprograms.
299
300 -- 1.5) Package body - the contract is analyzed at the end of the body
301 -- declaration list, prior to analyzing the contracts of any nested
302 -- packages or subprograms.
303
304 -- 1.6) Subprogram - if the subprogram is declared inside a block, a
305 -- package or a subprogram, then its contract is analyzed at the end of
306 -- the enclosing declarations, otherwise the subprogram is a compilation
307 -- unit 1.2).
308
309 -- 1.7) Subprogram body - if the subprogram body is declared inside a
310 -- block, a package body or a subprogram body, then its contract is
311 -- analyzed at the end of the enclosing declarations, otherwise the
312 -- subprogram is a compilation unit 1.3).
313
314 -- 2) Capture of global references within contracts is done after capturing
315 -- global references within the generic template. There are two reasons for
316 -- this delay - pragma annotations are not part of the generic template in
317 -- the case of a generic subprogram declaration, and analysis of contracts
318 -- is delayed.
319
320 -- Contract-related source pragmas within generic templates are prepared
321 -- for delayed capture of global references by routine
322
323 -- Create_Generic_Contract
324
325 -- The routine associates these pragmas with the contract of the template.
326 -- In the case of a generic subprogram declaration, the routine creates
327 -- generic templates for the pragmas declared after the subprogram because
328 -- they are not part of the template.
329
330 -- generic -- template starts
331 -- procedure Gen_Proc (Input : Integer); -- template ends
332 -- pragma Precondition (Input > 0); -- requires own template
333
334 -- 2.1) The capture of global references with aspect specifications and
335 -- source pragmas that apply to a generic unit must be suppressed when
336 -- the generic template is being processed because the contracts have not
337 -- been analyzed yet. Any attempts to capture global references at that
338 -- point will destroy the Associated_Node linkages and leave the template
339 -- undecorated. This delay is controlled by routine
340
341 -- Requires_Delayed_Save
342
343 -- 2.2) The real capture of global references within a contract is done
344 -- after the contract has been analyzed, by routine
345
346 -- Save_Global_References_In_Contract
347
348 -- 3) The instantiation of a generic contract occurs as part of the
349 -- instantiation of the contract owner. Generic subprogram declarations
350 -- require additional processing when the contract is specified by pragmas
351 -- because the pragmas are not part of the generic template. This is done
352 -- by routine
353
354 -- Instantiate_Subprogram_Contract
355
356 Circularity_Detected : Boolean := False;
357 -- This should really be reset on encountering a new main unit, but in
358 -- practice we are not using multiple main units so it is not critical.
359
360 --------------------------------------------------
361 -- Formal packages and partial parameterization --
362 --------------------------------------------------
363
364 -- When compiling a generic, a formal package is a local instantiation. If
365 -- declared with a box, its generic formals are visible in the enclosing
366 -- generic. If declared with a partial list of actuals, those actuals that
367 -- are defaulted (covered by an Others clause, or given an explicit box
368 -- initialization) are also visible in the enclosing generic, while those
369 -- that have a corresponding actual are not.
370
371 -- In our source model of instantiation, the same visibility must be
372 -- present in the spec and body of an instance: the names of the formals
373 -- that are defaulted must be made visible within the instance, and made
374 -- invisible (hidden) after the instantiation is complete, so that they
375 -- are not accessible outside of the instance.
376
377 -- In a generic, a formal package is treated like a special instantiation.
378 -- Our Ada 95 compiler handled formals with and without box in different
379 -- ways. With partial parameterization, we use a single model for both.
380 -- We create a package declaration that consists of the specification of
381 -- the generic package, and a set of declarations that map the actuals
382 -- into local renamings, just as we do for bona fide instantiations. For
383 -- defaulted parameters and formals with a box, we copy directly the
384 -- declarations of the formal into this local package. The result is a
385 -- a package whose visible declarations may include generic formals. This
386 -- package is only used for type checking and visibility analysis, and
387 -- never reaches the back-end, so it can freely violate the placement
388 -- rules for generic formal declarations.
389
390 -- The list of declarations (renamings and copies of formals) is built
391 -- by Analyze_Associations, just as for regular instantiations.
392
393 -- At the point of instantiation, conformance checking must be applied only
394 -- to those parameters that were specified in the formal. We perform this
395 -- checking by creating another internal instantiation, this one including
396 -- only the renamings and the formals (the rest of the package spec is not
397 -- relevant to conformance checking). We can then traverse two lists: the
398 -- list of actuals in the instance that corresponds to the formal package,
399 -- and the list of actuals produced for this bogus instantiation. We apply
400 -- the conformance rules to those actuals that are not defaulted (i.e.
401 -- which still appear as generic formals.
402
403 -- When we compile an instance body we must make the right parameters
404 -- visible again. The predicate Is_Generic_Formal indicates which of the
405 -- formals should have its Is_Hidden flag reset.
406
407 -----------------------
408 -- Local subprograms --
409 -----------------------
410
411 procedure Abandon_Instantiation (N : Node_Id);
412 pragma No_Return (Abandon_Instantiation);
413 -- Posts an error message "instantiation abandoned" at the indicated node
414 -- and then raises the exception Instantiation_Error to do it.
415
416 procedure Analyze_Formal_Array_Type
417 (T : in out Entity_Id;
418 Def : Node_Id);
419 -- A formal array type is treated like an array type declaration, and
420 -- invokes Array_Type_Declaration (sem_ch3) whose first parameter is
421 -- in-out, because in the case of an anonymous type the entity is
422 -- actually created in the procedure.
423
424 -- The following procedures treat other kinds of formal parameters
425
426 procedure Analyze_Formal_Derived_Interface_Type
427 (N : Node_Id;
428 T : Entity_Id;
429 Def : Node_Id);
430
431 procedure Analyze_Formal_Derived_Type
432 (N : Node_Id;
433 T : Entity_Id;
434 Def : Node_Id);
435
436 procedure Analyze_Formal_Interface_Type
437 (N : Node_Id;
438 T : Entity_Id;
439 Def : Node_Id);
440
441 -- The following subprograms create abbreviated declarations for formal
442 -- scalar types. We introduce an anonymous base of the proper class for
443 -- each of them, and define the formals as constrained first subtypes of
444 -- their bases. The bounds are expressions that are non-static in the
445 -- generic.
446
447 procedure Analyze_Formal_Decimal_Fixed_Point_Type
448 (T : Entity_Id; Def : Node_Id);
449 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id);
450 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id);
451 procedure Analyze_Formal_Signed_Integer_Type (T : Entity_Id; Def : Node_Id);
452 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id);
453 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
454 (T : Entity_Id; Def : Node_Id);
455
456 procedure Analyze_Formal_Private_Type
457 (N : Node_Id;
458 T : Entity_Id;
459 Def : Node_Id);
460 -- Creates a new private type, which does not require completion
461
462 procedure Analyze_Formal_Incomplete_Type (T : Entity_Id; Def : Node_Id);
463 -- Ada 2012: Creates a new incomplete type whose actual does not freeze
464
465 procedure Analyze_Generic_Formal_Part (N : Node_Id);
466 -- Analyze generic formal part
467
468 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id);
469 -- Create a new access type with the given designated type
470
471 function Analyze_Associations
472 (I_Node : Node_Id;
473 Formals : List_Id;
474 F_Copy : List_Id) return List_Id;
475 -- At instantiation time, build the list of associations between formals
476 -- and actuals. Each association becomes a renaming declaration for the
477 -- formal entity. F_Copy is the analyzed list of formals in the generic
478 -- copy. It is used to apply legality checks to the actuals. I_Node is the
479 -- instantiation node itself.
480
481 procedure Analyze_Subprogram_Instantiation
482 (N : Node_Id;
483 K : Entity_Kind);
484
485 procedure Build_Instance_Compilation_Unit_Nodes
486 (N : Node_Id;
487 Act_Body : Node_Id;
488 Act_Decl : Node_Id);
489 -- This procedure is used in the case where the generic instance of a
490 -- subprogram body or package body is a library unit. In this case, the
491 -- original library unit node for the generic instantiation must be
492 -- replaced by the resulting generic body, and a link made to a new
493 -- compilation unit node for the generic declaration. The argument N is
494 -- the original generic instantiation. Act_Body and Act_Decl are the body
495 -- and declaration of the instance (either package body and declaration
496 -- nodes or subprogram body and declaration nodes depending on the case).
497 -- On return, the node N has been rewritten with the actual body.
498
499 procedure Check_Access_Definition (N : Node_Id);
500 -- Subsidiary routine to null exclusion processing. Perform an assertion
501 -- check on Ada version and the presence of an access definition in N.
502
503 procedure Check_Formal_Packages (P_Id : Entity_Id);
504 -- Apply the following to all formal packages in generic associations
505
506 procedure Check_Formal_Package_Instance
507 (Formal_Pack : Entity_Id;
508 Actual_Pack : Entity_Id);
509 -- Verify that the actuals of the actual instance match the actuals of
510 -- the template for a formal package that is not declared with a box.
511
512 procedure Check_Forward_Instantiation (Decl : Node_Id);
513 -- If the generic is a local entity and the corresponding body has not
514 -- been seen yet, flag enclosing packages to indicate that it will be
515 -- elaborated after the generic body. Subprograms declared in the same
516 -- package cannot be inlined by the front end because front-end inlining
517 -- requires a strict linear order of elaboration.
518
519 function Check_Hidden_Primitives (Assoc_List : List_Id) return Elist_Id;
520 -- Check if some association between formals and actuals requires to make
521 -- visible primitives of a tagged type, and make those primitives visible.
522 -- Return the list of primitives whose visibility is modified (to restore
523 -- their visibility later through Restore_Hidden_Primitives). If no
524 -- candidate is found then return No_Elist.
525
526 procedure Check_Hidden_Child_Unit
527 (N : Node_Id;
528 Gen_Unit : Entity_Id;
529 Act_Decl_Id : Entity_Id);
530 -- If the generic unit is an implicit child instance within a parent
531 -- instance, we need to make an explicit test that it is not hidden by
532 -- a child instance of the same name and parent.
533
534 procedure Check_Generic_Actuals
535 (Instance : Entity_Id;
536 Is_Formal_Box : Boolean);
537 -- Similar to previous one. Check the actuals in the instantiation,
538 -- whose views can change between the point of instantiation and the point
539 -- of instantiation of the body. In addition, mark the generic renamings
540 -- as generic actuals, so that they are not compatible with other actuals.
541 -- Recurse on an actual that is a formal package whose declaration has
542 -- a box.
543
544 function Contains_Instance_Of
545 (Inner : Entity_Id;
546 Outer : Entity_Id;
547 N : Node_Id) return Boolean;
548 -- Inner is instantiated within the generic Outer. Check whether Inner
549 -- directly or indirectly contains an instance of Outer or of one of its
550 -- parents, in the case of a subunit. Each generic unit holds a list of
551 -- the entities instantiated within (at any depth). This procedure
552 -- determines whether the set of such lists contains a cycle, i.e. an
553 -- illegal circular instantiation.
554
555 function Denotes_Formal_Package
556 (Pack : Entity_Id;
557 On_Exit : Boolean := False;
558 Instance : Entity_Id := Empty) return Boolean;
559 -- Returns True if E is a formal package of an enclosing generic, or
560 -- the actual for such a formal in an enclosing instantiation. If such
561 -- a package is used as a formal in an nested generic, or as an actual
562 -- in a nested instantiation, the visibility of ITS formals should not
563 -- be modified. When called from within Restore_Private_Views, the flag
564 -- On_Exit is true, to indicate that the search for a possible enclosing
565 -- instance should ignore the current one. In that case Instance denotes
566 -- the declaration for which this is an actual. This declaration may be
567 -- an instantiation in the source, or the internal instantiation that
568 -- corresponds to the actual for a formal package.
569
570 function Earlier (N1, N2 : Node_Id) return Boolean;
571 -- Yields True if N1 and N2 appear in the same compilation unit,
572 -- ignoring subunits, and if N1 is to the left of N2 in a left-to-right
573 -- traversal of the tree for the unit. Used to determine the placement
574 -- of freeze nodes for instance bodies that may depend on other instances.
575
576 function Find_Actual_Type
577 (Typ : Entity_Id;
578 Gen_Type : Entity_Id) return Entity_Id;
579 -- When validating the actual types of a child instance, check whether
580 -- the formal is a formal type of the parent unit, and retrieve the current
581 -- actual for it. Typ is the entity in the analyzed formal type declaration
582 -- (component or index type of an array type, or designated type of an
583 -- access formal) and Gen_Type is the enclosing analyzed formal array
584 -- or access type. The desired actual may be a formal of a parent, or may
585 -- be declared in a formal package of a parent. In both cases it is a
586 -- generic actual type because it appears within a visible instance.
587 -- Finally, it may be declared in a parent unit without being a formal
588 -- of that unit, in which case it must be retrieved by visibility.
589 -- Ambiguities may still arise if two homonyms are declared in two formal
590 -- packages, and the prefix of the formal type may be needed to resolve
591 -- the ambiguity in the instance ???
592
593 procedure Freeze_Subprogram_Body
594 (Inst_Node : Node_Id;
595 Gen_Body : Node_Id;
596 Pack_Id : Entity_Id);
597 -- The generic body may appear textually after the instance, including
598 -- in the proper body of a stub, or within a different package instance.
599 -- Given that the instance can only be elaborated after the generic, we
600 -- place freeze_nodes for the instance and/or for packages that may enclose
601 -- the instance and the generic, so that the back-end can establish the
602 -- proper order of elaboration.
603
604 function Get_Associated_Node (N : Node_Id) return Node_Id;
605 -- In order to propagate semantic information back from the analyzed copy
606 -- to the original generic, we maintain links between selected nodes in the
607 -- generic and their corresponding copies. At the end of generic analysis,
608 -- the routine Save_Global_References traverses the generic tree, examines
609 -- the semantic information, and preserves the links to those nodes that
610 -- contain global information. At instantiation, the information from the
611 -- associated node is placed on the new copy, so that name resolution is
612 -- not repeated.
613 --
614 -- Three kinds of source nodes have associated nodes:
615 --
616 -- a) those that can reference (denote) entities, that is identifiers,
617 -- character literals, expanded_names, operator symbols, operators,
618 -- and attribute reference nodes. These nodes have an Entity field
619 -- and are the set of nodes that are in N_Has_Entity.
620 --
621 -- b) aggregates (N_Aggregate and N_Extension_Aggregate)
622 --
623 -- c) selected components (N_Selected_Component)
624 --
625 -- For the first class, the associated node preserves the entity if it is
626 -- global. If the generic contains nested instantiations, the associated
627 -- node itself has been recopied, and a chain of them must be followed.
628 --
629 -- For aggregates, the associated node allows retrieval of the type, which
630 -- may otherwise not appear in the generic. The view of this type may be
631 -- different between generic and instantiation, and the full view can be
632 -- installed before the instantiation is analyzed. For aggregates of type
633 -- extensions, the same view exchange may have to be performed for some of
634 -- the ancestor types, if their view is private at the point of
635 -- instantiation.
636 --
637 -- Nodes that are selected components in the parse tree may be rewritten
638 -- as expanded names after resolution, and must be treated as potential
639 -- entity holders, which is why they also have an Associated_Node.
640 --
641 -- Nodes that do not come from source, such as freeze nodes, do not appear
642 -- in the generic tree, and need not have an associated node.
643 --
644 -- The associated node is stored in the Associated_Node field. Note that
645 -- this field overlaps Entity, which is fine, because the whole point is
646 -- that we don't need or want the normal Entity field in this situation.
647
648 function Has_Been_Exchanged (E : Entity_Id) return Boolean;
649 -- Traverse the Exchanged_Views list to see if a type was private
650 -- and has already been flipped during this phase of instantiation.
651
652 procedure Hide_Current_Scope;
653 -- When instantiating a generic child unit, the parent context must be
654 -- present, but the instance and all entities that may be generated
655 -- must be inserted in the current scope. We leave the current scope
656 -- on the stack, but make its entities invisible to avoid visibility
657 -- problems. This is reversed at the end of the instantiation. This is
658 -- not done for the instantiation of the bodies, which only require the
659 -- instances of the generic parents to be in scope.
660
661 function In_Same_Declarative_Part
662 (F_Node : Node_Id;
663 Inst : Node_Id) return Boolean;
664 -- True if the instantiation Inst and the given freeze_node F_Node appear
665 -- within the same declarative part, ignoring subunits, but with no inter-
666 -- vening subprograms or concurrent units. Used to find the proper plave
667 -- for the freeze node of an instance, when the generic is declared in a
668 -- previous instance. If predicate is true, the freeze node of the instance
669 -- can be placed after the freeze node of the previous instance, Otherwise
670 -- it has to be placed at the end of the current declarative part.
671
672 function In_Main_Context (E : Entity_Id) return Boolean;
673 -- Check whether an instantiation is in the context of the main unit.
674 -- Used to determine whether its body should be elaborated to allow
675 -- front-end inlining.
676
677 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id);
678 -- Add the context clause of the unit containing a generic unit to a
679 -- compilation unit that is, or contains, an instantiation.
680
681 procedure Init_Env;
682 -- Establish environment for subsequent instantiation. Separated from
683 -- Save_Env because data-structures for visibility handling must be
684 -- initialized before call to Check_Generic_Child_Unit.
685
686 procedure Inline_Instance_Body
687 (N : Node_Id;
688 Gen_Unit : Entity_Id;
689 Act_Decl : Node_Id);
690 -- If front-end inlining is requested, instantiate the package body,
691 -- and preserve the visibility of its compilation unit, to insure
692 -- that successive instantiations succeed.
693
694 procedure Insert_Freeze_Node_For_Instance
695 (N : Node_Id;
696 F_Node : Node_Id);
697 -- N denotes a package or a subprogram instantiation and F_Node is the
698 -- associated freeze node. Insert the freeze node before the first source
699 -- body which follows immediately after N. If no such body is found, the
700 -- freeze node is inserted at the end of the declarative region which
701 -- contains N.
702
703 procedure Install_Body
704 (Act_Body : Node_Id;
705 N : Node_Id;
706 Gen_Body : Node_Id;
707 Gen_Decl : Node_Id);
708 -- If the instantiation happens textually before the body of the generic,
709 -- the instantiation of the body must be analyzed after the generic body,
710 -- and not at the point of instantiation. Such early instantiations can
711 -- happen if the generic and the instance appear in a package declaration
712 -- because the generic body can only appear in the corresponding package
713 -- body. Early instantiations can also appear if generic, instance and
714 -- body are all in the declarative part of a subprogram or entry. Entities
715 -- of packages that are early instantiations are delayed, and their freeze
716 -- node appears after the generic body. This rather complex machinery is
717 -- needed when nested instantiations are present, because the source does
718 -- not carry any indication of where the corresponding instance bodies must
719 -- be installed and frozen.
720
721 procedure Install_Formal_Packages (Par : Entity_Id);
722 -- Install the visible part of any formal of the parent that is a formal
723 -- package. Note that for the case of a formal package with a box, this
724 -- includes the formal part of the formal package (12.7(10/2)).
725
726 procedure Install_Hidden_Primitives
727 (Prims_List : in out Elist_Id;
728 Gen_T : Entity_Id;
729 Act_T : Entity_Id);
730 -- Remove suffix 'P' from hidden primitives of Act_T to match the
731 -- visibility of primitives of Gen_T. The list of primitives to which
732 -- the suffix is removed is added to Prims_List to restore them later.
733
734 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False);
735 -- When compiling an instance of a child unit the parent (which is
736 -- itself an instance) is an enclosing scope that must be made
737 -- immediately visible. This procedure is also used to install the non-
738 -- generic parent of a generic child unit when compiling its body, so
739 -- that full views of types in the parent are made visible.
740
741 -- The functions Instantiate_XXX perform various legality checks and build
742 -- the declarations for instantiated generic parameters. In all of these
743 -- Formal is the entity in the generic unit, Actual is the entity of
744 -- expression in the generic associations, and Analyzed_Formal is the
745 -- formal in the generic copy, which contains the semantic information to
746 -- be used to validate the actual.
747
748 function Instantiate_Object
749 (Formal : Node_Id;
750 Actual : Node_Id;
751 Analyzed_Formal : Node_Id) return List_Id;
752
753 function Instantiate_Type
754 (Formal : Node_Id;
755 Actual : Node_Id;
756 Analyzed_Formal : Node_Id;
757 Actual_Decls : List_Id) return List_Id;
758
759 function Instantiate_Formal_Subprogram
760 (Formal : Node_Id;
761 Actual : Node_Id;
762 Analyzed_Formal : Node_Id) return Node_Id;
763
764 function Instantiate_Formal_Package
765 (Formal : Node_Id;
766 Actual : Node_Id;
767 Analyzed_Formal : Node_Id) return List_Id;
768 -- If the formal package is declared with a box, special visibility rules
769 -- apply to its formals: they are in the visible part of the package. This
770 -- is true in the declarative region of the formal package, that is to say
771 -- in the enclosing generic or instantiation. For an instantiation, the
772 -- parameters of the formal package are made visible in an explicit step.
773 -- Furthermore, if the actual has a visible USE clause, these formals must
774 -- be made potentially use-visible as well. On exit from the enclosing
775 -- instantiation, the reverse must be done.
776
777 -- For a formal package declared without a box, there are conformance rules
778 -- that apply to the actuals in the generic declaration and the actuals of
779 -- the actual package in the enclosing instantiation. The simplest way to
780 -- apply these rules is to repeat the instantiation of the formal package
781 -- in the context of the enclosing instance, and compare the generic
782 -- associations of this instantiation with those of the actual package.
783 -- This internal instantiation only needs to contain the renamings of the
784 -- formals: the visible and private declarations themselves need not be
785 -- created.
786
787 -- In Ada 2005, the formal package may be only partially parameterized.
788 -- In that case the visibility step must make visible those actuals whose
789 -- corresponding formals were given with a box. A final complication
790 -- involves inherited operations from formal derived types, which must
791 -- be visible if the type is.
792
793 function Is_In_Main_Unit (N : Node_Id) return Boolean;
794 -- Test if given node is in the main unit
795
796 procedure Load_Parent_Of_Generic
797 (N : Node_Id;
798 Spec : Node_Id;
799 Body_Optional : Boolean := False);
800 -- If the generic appears in a separate non-generic library unit, load the
801 -- corresponding body to retrieve the body of the generic. N is the node
802 -- for the generic instantiation, Spec is the generic package declaration.
803 --
804 -- Body_Optional is a flag that indicates that the body is being loaded to
805 -- ensure that temporaries are generated consistently when there are other
806 -- instances in the current declarative part that precede the one being
807 -- loaded. In that case a missing body is acceptable.
808
809 procedure Map_Formal_Package_Entities (Form : Entity_Id; Act : Entity_Id);
810 -- Within the generic part, entities in the formal package are
811 -- visible. To validate subsequent type declarations, indicate
812 -- the correspondence between the entities in the analyzed formal,
813 -- and the entities in the actual package. There are three packages
814 -- involved in the instantiation of a formal package: the parent
815 -- generic P1 which appears in the generic declaration, the fake
816 -- instantiation P2 which appears in the analyzed generic, and whose
817 -- visible entities may be used in subsequent formals, and the actual
818 -- P3 in the instance. To validate subsequent formals, me indicate
819 -- that the entities in P2 are mapped into those of P3. The mapping of
820 -- entities has to be done recursively for nested packages.
821
822 procedure Move_Freeze_Nodes
823 (Out_Of : Entity_Id;
824 After : Node_Id;
825 L : List_Id);
826 -- Freeze nodes can be generated in the analysis of a generic unit, but
827 -- will not be seen by the back-end. It is necessary to move those nodes
828 -- to the enclosing scope if they freeze an outer entity. We place them
829 -- at the end of the enclosing generic package, which is semantically
830 -- neutral.
831
832 procedure Preanalyze_Actuals (N : Node_Id; Inst : Entity_Id := Empty);
833 -- Analyze actuals to perform name resolution. Full resolution is done
834 -- later, when the expected types are known, but names have to be captured
835 -- before installing parents of generics, that are not visible for the
836 -- actuals themselves.
837 --
838 -- If Inst is present, it is the entity of the package instance. This
839 -- entity is marked as having a limited_view actual when some actual is
840 -- a limited view. This is used to place the instance body properly.
841
842 procedure Remove_Parent (In_Body : Boolean := False);
843 -- Reverse effect after instantiation of child is complete
844
845 procedure Restore_Hidden_Primitives (Prims_List : in out Elist_Id);
846 -- Restore suffix 'P' to primitives of Prims_List and leave Prims_List
847 -- set to No_Elist.
848
849 procedure Set_Instance_Env
850 (Gen_Unit : Entity_Id;
851 Act_Unit : Entity_Id);
852 -- Save current instance on saved environment, to be used to determine
853 -- the global status of entities in nested instances. Part of Save_Env.
854 -- called after verifying that the generic unit is legal for the instance,
855 -- The procedure also examines whether the generic unit is a predefined
856 -- unit, in order to set configuration switches accordingly. As a result
857 -- the procedure must be called after analyzing and freezing the actuals.
858
859 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id);
860 -- Associate analyzed generic parameter with corresponding instance. Used
861 -- for semantic checks at instantiation time.
862
863 function True_Parent (N : Node_Id) return Node_Id;
864 -- For a subunit, return parent of corresponding stub, else return
865 -- parent of node.
866
867 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id);
868 -- Verify that an attribute that appears as the default for a formal
869 -- subprogram is a function or procedure with the correct profile.
870
871 -------------------------------------------
872 -- Data Structures for Generic Renamings --
873 -------------------------------------------
874
875 -- The map Generic_Renamings associates generic entities with their
876 -- corresponding actuals. Currently used to validate type instances. It
877 -- will eventually be used for all generic parameters to eliminate the
878 -- need for overload resolution in the instance.
879
880 type Assoc_Ptr is new Int;
881
882 Assoc_Null : constant Assoc_Ptr := -1;
883
884 type Assoc is record
885 Gen_Id : Entity_Id;
886 Act_Id : Entity_Id;
887 Next_In_HTable : Assoc_Ptr;
888 end record;
889
890 package Generic_Renamings is new Table.Table
891 (Table_Component_Type => Assoc,
892 Table_Index_Type => Assoc_Ptr,
893 Table_Low_Bound => 0,
894 Table_Initial => 10,
895 Table_Increment => 100,
896 Table_Name => "Generic_Renamings");
897
898 -- Variable to hold enclosing instantiation. When the environment is
899 -- saved for a subprogram inlining, the corresponding Act_Id is empty.
900
901 Current_Instantiated_Parent : Assoc := (Empty, Empty, Assoc_Null);
902
903 -- Hash table for associations
904
905 HTable_Size : constant := 37;
906 type HTable_Range is range 0 .. HTable_Size - 1;
907
908 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr);
909 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr;
910 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id;
911 function Hash (F : Entity_Id) return HTable_Range;
912
913 package Generic_Renamings_HTable is new GNAT.HTable.Static_HTable (
914 Header_Num => HTable_Range,
915 Element => Assoc,
916 Elmt_Ptr => Assoc_Ptr,
917 Null_Ptr => Assoc_Null,
918 Set_Next => Set_Next_Assoc,
919 Next => Next_Assoc,
920 Key => Entity_Id,
921 Get_Key => Get_Gen_Id,
922 Hash => Hash,
923 Equal => "=");
924
925 Exchanged_Views : Elist_Id;
926 -- This list holds the private views that have been exchanged during
927 -- instantiation to restore the visibility of the generic declaration.
928 -- (see comments above). After instantiation, the current visibility is
929 -- reestablished by means of a traversal of this list.
930
931 Hidden_Entities : Elist_Id;
932 -- This list holds the entities of the current scope that are removed
933 -- from immediate visibility when instantiating a child unit. Their
934 -- visibility is restored in Remove_Parent.
935
936 -- Because instantiations can be recursive, the following must be saved
937 -- on entry and restored on exit from an instantiation (spec or body).
938 -- This is done by the two procedures Save_Env and Restore_Env. For
939 -- package and subprogram instantiations (but not for the body instances)
940 -- the action of Save_Env is done in two steps: Init_Env is called before
941 -- Check_Generic_Child_Unit, because setting the parent instances requires
942 -- that the visibility data structures be properly initialized. Once the
943 -- generic is unit is validated, Set_Instance_Env completes Save_Env.
944
945 Parent_Unit_Visible : Boolean := False;
946 -- Parent_Unit_Visible is used when the generic is a child unit, and
947 -- indicates whether the ultimate parent of the generic is visible in the
948 -- instantiation environment. It is used to reset the visibility of the
949 -- parent at the end of the instantiation (see Remove_Parent).
950
951 Instance_Parent_Unit : Entity_Id := Empty;
952 -- This records the ultimate parent unit of an instance of a generic
953 -- child unit and is used in conjunction with Parent_Unit_Visible to
954 -- indicate the unit to which the Parent_Unit_Visible flag corresponds.
955
956 type Instance_Env is record
957 Instantiated_Parent : Assoc;
958 Exchanged_Views : Elist_Id;
959 Hidden_Entities : Elist_Id;
960 Current_Sem_Unit : Unit_Number_Type;
961 Parent_Unit_Visible : Boolean := False;
962 Instance_Parent_Unit : Entity_Id := Empty;
963 Switches : Config_Switches_Type;
964 end record;
965
966 package Instance_Envs is new Table.Table (
967 Table_Component_Type => Instance_Env,
968 Table_Index_Type => Int,
969 Table_Low_Bound => 0,
970 Table_Initial => 32,
971 Table_Increment => 100,
972 Table_Name => "Instance_Envs");
973
974 procedure Restore_Private_Views
975 (Pack_Id : Entity_Id;
976 Is_Package : Boolean := True);
977 -- Restore the private views of external types, and unmark the generic
978 -- renamings of actuals, so that they become compatible subtypes again.
979 -- For subprograms, Pack_Id is the package constructed to hold the
980 -- renamings.
981
982 procedure Switch_View (T : Entity_Id);
983 -- Switch the partial and full views of a type and its private
984 -- dependents (i.e. its subtypes and derived types).
985
986 ------------------------------------
987 -- Structures for Error Reporting --
988 ------------------------------------
989
990 Instantiation_Node : Node_Id;
991 -- Used by subprograms that validate instantiation of formal parameters
992 -- where there might be no actual on which to place the error message.
993 -- Also used to locate the instantiation node for generic subunits.
994
995 Instantiation_Error : exception;
996 -- When there is a semantic error in the generic parameter matching,
997 -- there is no point in continuing the instantiation, because the
998 -- number of cascaded errors is unpredictable. This exception aborts
999 -- the instantiation process altogether.
1000
1001 S_Adjustment : Sloc_Adjustment;
1002 -- Offset created for each node in an instantiation, in order to keep
1003 -- track of the source position of the instantiation in each of its nodes.
1004 -- A subsequent semantic error or warning on a construct of the instance
1005 -- points to both places: the original generic node, and the point of
1006 -- instantiation. See Sinput and Sinput.L for additional details.
1007
1008 ------------------------------------------------------------
1009 -- Data structure for keeping track when inside a Generic --
1010 ------------------------------------------------------------
1011
1012 -- The following table is used to save values of the Inside_A_Generic
1013 -- flag (see spec of Sem) when they are saved by Start_Generic.
1014
1015 package Generic_Flags is new Table.Table (
1016 Table_Component_Type => Boolean,
1017 Table_Index_Type => Int,
1018 Table_Low_Bound => 0,
1019 Table_Initial => 32,
1020 Table_Increment => 200,
1021 Table_Name => "Generic_Flags");
1022
1023 ---------------------------
1024 -- Abandon_Instantiation --
1025 ---------------------------
1026
1027 procedure Abandon_Instantiation (N : Node_Id) is
1028 begin
1029 Error_Msg_N ("\instantiation abandoned!", N);
1030 raise Instantiation_Error;
1031 end Abandon_Instantiation;
1032
1033 --------------------------------
1034 -- Add_Pending_Instantiation --
1035 --------------------------------
1036
1037 procedure Add_Pending_Instantiation (Inst : Node_Id; Act_Decl : Node_Id) is
1038 begin
1039
1040 -- Add to the instantiation node and the corresponding unit declaration
1041 -- the current values of global flags to be used when analyzing the
1042 -- instance body.
1043
1044 Pending_Instantiations.Append
1045 ((Inst_Node => Inst,
1046 Act_Decl => Act_Decl,
1047 Expander_Status => Expander_Active,
1048 Current_Sem_Unit => Current_Sem_Unit,
1049 Scope_Suppress => Scope_Suppress,
1050 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
1051 Version => Ada_Version,
1052 Version_Pragma => Ada_Version_Pragma,
1053 Warnings => Save_Warnings,
1054 SPARK_Mode => SPARK_Mode,
1055 SPARK_Mode_Pragma => SPARK_Mode_Pragma));
1056 end Add_Pending_Instantiation;
1057
1058 ----------------------------------
1059 -- Adjust_Inherited_Pragma_Sloc --
1060 ----------------------------------
1061
1062 procedure Adjust_Inherited_Pragma_Sloc (N : Node_Id) is
1063 begin
1064 Adjust_Instantiation_Sloc (N, S_Adjustment);
1065 end Adjust_Inherited_Pragma_Sloc;
1066
1067 --------------------------
1068 -- Analyze_Associations --
1069 --------------------------
1070
1071 function Analyze_Associations
1072 (I_Node : Node_Id;
1073 Formals : List_Id;
1074 F_Copy : List_Id) return List_Id
1075 is
1076 Actuals_To_Freeze : constant Elist_Id := New_Elmt_List;
1077 Assoc : constant List_Id := New_List;
1078 Default_Actuals : constant List_Id := New_List;
1079 Gen_Unit : constant Entity_Id :=
1080 Defining_Entity (Parent (F_Copy));
1081
1082 Actuals : List_Id;
1083 Actual : Node_Id;
1084 Analyzed_Formal : Node_Id;
1085 First_Named : Node_Id := Empty;
1086 Formal : Node_Id;
1087 Match : Node_Id;
1088 Named : Node_Id;
1089 Saved_Formal : Node_Id;
1090
1091 Default_Formals : constant List_Id := New_List;
1092 -- If an Others_Choice is present, some of the formals may be defaulted.
1093 -- To simplify the treatment of visibility in an instance, we introduce
1094 -- individual defaults for each such formal. These defaults are
1095 -- appended to the list of associations and replace the Others_Choice.
1096
1097 Found_Assoc : Node_Id;
1098 -- Association for the current formal being match. Empty if there are
1099 -- no remaining actuals, or if there is no named association with the
1100 -- name of the formal.
1101
1102 Is_Named_Assoc : Boolean;
1103 Num_Matched : Nat := 0;
1104 Num_Actuals : Nat := 0;
1105
1106 Others_Present : Boolean := False;
1107 Others_Choice : Node_Id := Empty;
1108 -- In Ada 2005, indicates partial parameterization of a formal
1109 -- package. As usual an other association must be last in the list.
1110
1111 procedure Check_Fixed_Point_Actual (Actual : Node_Id);
1112 -- Warn if an actual fixed-point type has user-defined arithmetic
1113 -- operations, but there is no corresponding formal in the generic,
1114 -- in which case the predefined operations will be used. This merits
1115 -- a warning because of the special semantics of fixed point ops.
1116
1117 procedure Check_Overloaded_Formal_Subprogram (Formal : Entity_Id);
1118 -- Apply RM 12.3(9): if a formal subprogram is overloaded, the instance
1119 -- cannot have a named association for it. AI05-0025 extends this rule
1120 -- to formals of formal packages by AI05-0025, and it also applies to
1121 -- box-initialized formals.
1122
1123 function Has_Fully_Defined_Profile (Subp : Entity_Id) return Boolean;
1124 -- Determine whether the parameter types and the return type of Subp
1125 -- are fully defined at the point of instantiation.
1126
1127 function Matching_Actual
1128 (F : Entity_Id;
1129 A_F : Entity_Id) return Node_Id;
1130 -- Find actual that corresponds to a given a formal parameter. If the
1131 -- actuals are positional, return the next one, if any. If the actuals
1132 -- are named, scan the parameter associations to find the right one.
1133 -- A_F is the corresponding entity in the analyzed generic, which is
1134 -- placed on the selector name for ASIS use.
1135 --
1136 -- In Ada 2005, a named association may be given with a box, in which
1137 -- case Matching_Actual sets Found_Assoc to the generic association,
1138 -- but return Empty for the actual itself. In this case the code below
1139 -- creates a corresponding declaration for the formal.
1140
1141 function Partial_Parameterization return Boolean;
1142 -- Ada 2005: if no match is found for a given formal, check if the
1143 -- association for it includes a box, or whether the associations
1144 -- include an Others clause.
1145
1146 procedure Process_Default (F : Entity_Id);
1147 -- Add a copy of the declaration of generic formal F to the list of
1148 -- associations, and add an explicit box association for F if there
1149 -- is none yet, and the default comes from an Others_Choice.
1150
1151 function Renames_Standard_Subprogram (Subp : Entity_Id) return Boolean;
1152 -- Determine whether Subp renames one of the subprograms defined in the
1153 -- generated package Standard.
1154
1155 procedure Set_Analyzed_Formal;
1156 -- Find the node in the generic copy that corresponds to a given formal.
1157 -- The semantic information on this node is used to perform legality
1158 -- checks on the actuals. Because semantic analysis can introduce some
1159 -- anonymous entities or modify the declaration node itself, the
1160 -- correspondence between the two lists is not one-one. In addition to
1161 -- anonymous types, the presence a formal equality will introduce an
1162 -- implicit declaration for the corresponding inequality.
1163
1164 ----------------------------------------
1165 -- Check_Overloaded_Formal_Subprogram --
1166 ----------------------------------------
1167
1168 procedure Check_Overloaded_Formal_Subprogram (Formal : Entity_Id) is
1169 Temp_Formal : Entity_Id;
1170
1171 begin
1172 Temp_Formal := First (Formals);
1173 while Present (Temp_Formal) loop
1174 if Nkind (Temp_Formal) in N_Formal_Subprogram_Declaration
1175 and then Temp_Formal /= Formal
1176 and then
1177 Chars (Defining_Unit_Name (Specification (Formal))) =
1178 Chars (Defining_Unit_Name (Specification (Temp_Formal)))
1179 then
1180 if Present (Found_Assoc) then
1181 Error_Msg_N
1182 ("named association not allowed for overloaded formal",
1183 Found_Assoc);
1184
1185 else
1186 Error_Msg_N
1187 ("named association not allowed for overloaded formal",
1188 Others_Choice);
1189 end if;
1190
1191 Abandon_Instantiation (Instantiation_Node);
1192 end if;
1193
1194 Next (Temp_Formal);
1195 end loop;
1196 end Check_Overloaded_Formal_Subprogram;
1197
1198 -------------------------------
1199 -- Check_Fixed_Point_Actual --
1200 -------------------------------
1201
1202 procedure Check_Fixed_Point_Actual (Actual : Node_Id) is
1203 Typ : constant Entity_Id := Entity (Actual);
1204 Prims : constant Elist_Id := Collect_Primitive_Operations (Typ);
1205 Elem : Elmt_Id;
1206 Formal : Node_Id;
1207
1208 begin
1209 -- Locate primitive operations of the type that are arithmetic
1210 -- operations.
1211
1212 Elem := First_Elmt (Prims);
1213 while Present (Elem) loop
1214 if Nkind (Node (Elem)) = N_Defining_Operator_Symbol then
1215
1216 -- Check whether the generic unit has a formal subprogram of
1217 -- the same name. This does not check types but is good enough
1218 -- to justify a warning.
1219
1220 Formal := First_Non_Pragma (Formals);
1221 while Present (Formal) loop
1222 if Nkind (Formal) = N_Formal_Concrete_Subprogram_Declaration
1223 and then Chars (Defining_Entity (Formal)) =
1224 Chars (Node (Elem))
1225 then
1226 exit;
1227 end if;
1228
1229 Next (Formal);
1230 end loop;
1231
1232 if No (Formal) then
1233 Error_Msg_Sloc := Sloc (Node (Elem));
1234 Error_Msg_NE
1235 ("?instance does not use primitive operation&#",
1236 Actual, Node (Elem));
1237 end if;
1238 end if;
1239
1240 Next_Elmt (Elem);
1241 end loop;
1242 end Check_Fixed_Point_Actual;
1243
1244 -------------------------------
1245 -- Has_Fully_Defined_Profile --
1246 -------------------------------
1247
1248 function Has_Fully_Defined_Profile (Subp : Entity_Id) return Boolean is
1249 function Is_Fully_Defined_Type (Typ : Entity_Id) return Boolean;
1250 -- Determine whethet type Typ is fully defined
1251
1252 ---------------------------
1253 -- Is_Fully_Defined_Type --
1254 ---------------------------
1255
1256 function Is_Fully_Defined_Type (Typ : Entity_Id) return Boolean is
1257 begin
1258 -- A private type without a full view is not fully defined
1259
1260 if Is_Private_Type (Typ)
1261 and then No (Full_View (Typ))
1262 then
1263 return False;
1264
1265 -- An incomplete type is never fully defined
1266
1267 elsif Is_Incomplete_Type (Typ) then
1268 return False;
1269
1270 -- All other types are fully defined
1271
1272 else
1273 return True;
1274 end if;
1275 end Is_Fully_Defined_Type;
1276
1277 -- Local declarations
1278
1279 Param : Entity_Id;
1280
1281 -- Start of processing for Has_Fully_Defined_Profile
1282
1283 begin
1284 -- Check the parameters
1285
1286 Param := First_Formal (Subp);
1287 while Present (Param) loop
1288 if not Is_Fully_Defined_Type (Etype (Param)) then
1289 return False;
1290 end if;
1291
1292 Next_Formal (Param);
1293 end loop;
1294
1295 -- Check the return type
1296
1297 return Is_Fully_Defined_Type (Etype (Subp));
1298 end Has_Fully_Defined_Profile;
1299
1300 ---------------------
1301 -- Matching_Actual --
1302 ---------------------
1303
1304 function Matching_Actual
1305 (F : Entity_Id;
1306 A_F : Entity_Id) return Node_Id
1307 is
1308 Prev : Node_Id;
1309 Act : Node_Id;
1310
1311 begin
1312 Is_Named_Assoc := False;
1313
1314 -- End of list of purely positional parameters
1315
1316 if No (Actual) or else Nkind (Actual) = N_Others_Choice then
1317 Found_Assoc := Empty;
1318 Act := Empty;
1319
1320 -- Case of positional parameter corresponding to current formal
1321
1322 elsif No (Selector_Name (Actual)) then
1323 Found_Assoc := Actual;
1324 Act := Explicit_Generic_Actual_Parameter (Actual);
1325 Num_Matched := Num_Matched + 1;
1326 Next (Actual);
1327
1328 -- Otherwise scan list of named actuals to find the one with the
1329 -- desired name. All remaining actuals have explicit names.
1330
1331 else
1332 Is_Named_Assoc := True;
1333 Found_Assoc := Empty;
1334 Act := Empty;
1335 Prev := Empty;
1336
1337 while Present (Actual) loop
1338 if Nkind (Actual) = N_Others_Choice then
1339 Found_Assoc := Empty;
1340 Act := Empty;
1341
1342 elsif Chars (Selector_Name (Actual)) = Chars (F) then
1343 Set_Entity (Selector_Name (Actual), A_F);
1344 Set_Etype (Selector_Name (Actual), Etype (A_F));
1345 Generate_Reference (A_F, Selector_Name (Actual));
1346
1347 Found_Assoc := Actual;
1348 Act := Explicit_Generic_Actual_Parameter (Actual);
1349 Num_Matched := Num_Matched + 1;
1350 exit;
1351 end if;
1352
1353 Prev := Actual;
1354 Next (Actual);
1355 end loop;
1356
1357 -- Reset for subsequent searches. In most cases the named
1358 -- associations are in order. If they are not, we reorder them
1359 -- to avoid scanning twice the same actual. This is not just a
1360 -- question of efficiency: there may be multiple defaults with
1361 -- boxes that have the same name. In a nested instantiation we
1362 -- insert actuals for those defaults, and cannot rely on their
1363 -- names to disambiguate them.
1364
1365 if Actual = First_Named then
1366 Next (First_Named);
1367
1368 elsif Present (Actual) then
1369 Insert_Before (First_Named, Remove_Next (Prev));
1370 end if;
1371
1372 Actual := First_Named;
1373 end if;
1374
1375 if Is_Entity_Name (Act) and then Present (Entity (Act)) then
1376 Set_Used_As_Generic_Actual (Entity (Act));
1377 end if;
1378
1379 return Act;
1380 end Matching_Actual;
1381
1382 ------------------------------
1383 -- Partial_Parameterization --
1384 ------------------------------
1385
1386 function Partial_Parameterization return Boolean is
1387 begin
1388 return Others_Present
1389 or else (Present (Found_Assoc) and then Box_Present (Found_Assoc));
1390 end Partial_Parameterization;
1391
1392 ---------------------
1393 -- Process_Default --
1394 ---------------------
1395
1396 procedure Process_Default (F : Entity_Id) is
1397 Loc : constant Source_Ptr := Sloc (I_Node);
1398 F_Id : constant Entity_Id := Defining_Entity (F);
1399 Decl : Node_Id;
1400 Default : Node_Id;
1401 Id : Entity_Id;
1402
1403 begin
1404 -- Append copy of formal declaration to associations, and create new
1405 -- defining identifier for it.
1406
1407 Decl := New_Copy_Tree (F);
1408 Id := Make_Defining_Identifier (Sloc (F_Id), Chars (F_Id));
1409
1410 if Nkind (F) in N_Formal_Subprogram_Declaration then
1411 Set_Defining_Unit_Name (Specification (Decl), Id);
1412
1413 else
1414 Set_Defining_Identifier (Decl, Id);
1415 end if;
1416
1417 Append (Decl, Assoc);
1418
1419 if No (Found_Assoc) then
1420 Default :=
1421 Make_Generic_Association (Loc,
1422 Selector_Name =>
1423 New_Occurrence_Of (Id, Loc),
1424 Explicit_Generic_Actual_Parameter => Empty);
1425 Set_Box_Present (Default);
1426 Append (Default, Default_Formals);
1427 end if;
1428 end Process_Default;
1429
1430 ---------------------------------
1431 -- Renames_Standard_Subprogram --
1432 ---------------------------------
1433
1434 function Renames_Standard_Subprogram (Subp : Entity_Id) return Boolean is
1435 Id : Entity_Id;
1436
1437 begin
1438 Id := Alias (Subp);
1439 while Present (Id) loop
1440 if Scope (Id) = Standard_Standard then
1441 return True;
1442 end if;
1443
1444 Id := Alias (Id);
1445 end loop;
1446
1447 return False;
1448 end Renames_Standard_Subprogram;
1449
1450 -------------------------
1451 -- Set_Analyzed_Formal --
1452 -------------------------
1453
1454 procedure Set_Analyzed_Formal is
1455 Kind : Node_Kind;
1456
1457 begin
1458 while Present (Analyzed_Formal) loop
1459 Kind := Nkind (Analyzed_Formal);
1460
1461 case Nkind (Formal) is
1462
1463 when N_Formal_Subprogram_Declaration =>
1464 exit when Kind in N_Formal_Subprogram_Declaration
1465 and then
1466 Chars
1467 (Defining_Unit_Name (Specification (Formal))) =
1468 Chars
1469 (Defining_Unit_Name (Specification (Analyzed_Formal)));
1470
1471 when N_Formal_Package_Declaration =>
1472 exit when Nkind_In (Kind, N_Formal_Package_Declaration,
1473 N_Generic_Package_Declaration,
1474 N_Package_Declaration);
1475
1476 when N_Use_Package_Clause | N_Use_Type_Clause => exit;
1477
1478 when others =>
1479
1480 -- Skip freeze nodes, and nodes inserted to replace
1481 -- unrecognized pragmas.
1482
1483 exit when
1484 Kind not in N_Formal_Subprogram_Declaration
1485 and then not Nkind_In (Kind, N_Subprogram_Declaration,
1486 N_Freeze_Entity,
1487 N_Null_Statement,
1488 N_Itype_Reference)
1489 and then Chars (Defining_Identifier (Formal)) =
1490 Chars (Defining_Identifier (Analyzed_Formal));
1491 end case;
1492
1493 Next (Analyzed_Formal);
1494 end loop;
1495 end Set_Analyzed_Formal;
1496
1497 -- Start of processing for Analyze_Associations
1498
1499 begin
1500 Actuals := Generic_Associations (I_Node);
1501
1502 if Present (Actuals) then
1503
1504 -- Check for an Others choice, indicating a partial parameterization
1505 -- for a formal package.
1506
1507 Actual := First (Actuals);
1508 while Present (Actual) loop
1509 if Nkind (Actual) = N_Others_Choice then
1510 Others_Present := True;
1511 Others_Choice := Actual;
1512
1513 if Present (Next (Actual)) then
1514 Error_Msg_N ("others must be last association", Actual);
1515 end if;
1516
1517 -- This subprogram is used both for formal packages and for
1518 -- instantiations. For the latter, associations must all be
1519 -- explicit.
1520
1521 if Nkind (I_Node) /= N_Formal_Package_Declaration
1522 and then Comes_From_Source (I_Node)
1523 then
1524 Error_Msg_N
1525 ("others association not allowed in an instance",
1526 Actual);
1527 end if;
1528
1529 -- In any case, nothing to do after the others association
1530
1531 exit;
1532
1533 elsif Box_Present (Actual)
1534 and then Comes_From_Source (I_Node)
1535 and then Nkind (I_Node) /= N_Formal_Package_Declaration
1536 then
1537 Error_Msg_N
1538 ("box association not allowed in an instance", Actual);
1539 end if;
1540
1541 Next (Actual);
1542 end loop;
1543
1544 -- If named associations are present, save first named association
1545 -- (it may of course be Empty) to facilitate subsequent name search.
1546
1547 First_Named := First (Actuals);
1548 while Present (First_Named)
1549 and then Nkind (First_Named) /= N_Others_Choice
1550 and then No (Selector_Name (First_Named))
1551 loop
1552 Num_Actuals := Num_Actuals + 1;
1553 Next (First_Named);
1554 end loop;
1555 end if;
1556
1557 Named := First_Named;
1558 while Present (Named) loop
1559 if Nkind (Named) /= N_Others_Choice
1560 and then No (Selector_Name (Named))
1561 then
1562 Error_Msg_N ("invalid positional actual after named one", Named);
1563 Abandon_Instantiation (Named);
1564 end if;
1565
1566 -- A named association may lack an actual parameter, if it was
1567 -- introduced for a default subprogram that turns out to be local
1568 -- to the outer instantiation. If it has a box association it must
1569 -- correspond to some formal in the generic.
1570
1571 if Nkind (Named) /= N_Others_Choice
1572 and then (Present (Explicit_Generic_Actual_Parameter (Named))
1573 or else Box_Present (Named))
1574 then
1575 Num_Actuals := Num_Actuals + 1;
1576 end if;
1577
1578 Next (Named);
1579 end loop;
1580
1581 if Present (Formals) then
1582 Formal := First_Non_Pragma (Formals);
1583 Analyzed_Formal := First_Non_Pragma (F_Copy);
1584
1585 if Present (Actuals) then
1586 Actual := First (Actuals);
1587
1588 -- All formals should have default values
1589
1590 else
1591 Actual := Empty;
1592 end if;
1593
1594 while Present (Formal) loop
1595 Set_Analyzed_Formal;
1596 Saved_Formal := Next_Non_Pragma (Formal);
1597
1598 case Nkind (Formal) is
1599 when N_Formal_Object_Declaration =>
1600 Match :=
1601 Matching_Actual
1602 (Defining_Identifier (Formal),
1603 Defining_Identifier (Analyzed_Formal));
1604
1605 if No (Match) and then Partial_Parameterization then
1606 Process_Default (Formal);
1607
1608 else
1609 Append_List
1610 (Instantiate_Object (Formal, Match, Analyzed_Formal),
1611 Assoc);
1612
1613 -- For a defaulted in_parameter, create an entry in the
1614 -- the list of defaulted actuals, for GNATProve use. Do
1615 -- not included these defaults for an instance nested
1616 -- within a generic, because the defaults are also used
1617 -- in the analysis of the enclosing generic, and only
1618 -- defaulted subprograms are relevant there.
1619
1620 if No (Match) and then not Inside_A_Generic then
1621 Append_To (Default_Actuals,
1622 Make_Generic_Association (Sloc (I_Node),
1623 Selector_Name =>
1624 New_Occurrence_Of
1625 (Defining_Identifier (Formal), Sloc (I_Node)),
1626 Explicit_Generic_Actual_Parameter =>
1627 New_Copy_Tree (Default_Expression (Formal))));
1628 end if;
1629 end if;
1630
1631 -- If the object is a call to an expression function, this
1632 -- is a freezing point for it.
1633
1634 if Is_Entity_Name (Match)
1635 and then Present (Entity (Match))
1636 and then Nkind
1637 (Original_Node (Unit_Declaration_Node (Entity (Match))))
1638 = N_Expression_Function
1639 then
1640 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1641 end if;
1642
1643 when N_Formal_Type_Declaration =>
1644 Match :=
1645 Matching_Actual
1646 (Defining_Identifier (Formal),
1647 Defining_Identifier (Analyzed_Formal));
1648
1649 if No (Match) then
1650 if Partial_Parameterization then
1651 Process_Default (Formal);
1652
1653 else
1654 Error_Msg_Sloc := Sloc (Gen_Unit);
1655 Error_Msg_NE
1656 ("missing actual&",
1657 Instantiation_Node, Defining_Identifier (Formal));
1658 Error_Msg_NE
1659 ("\in instantiation of & declared#",
1660 Instantiation_Node, Gen_Unit);
1661 Abandon_Instantiation (Instantiation_Node);
1662 end if;
1663
1664 else
1665 Analyze (Match);
1666 Append_List
1667 (Instantiate_Type
1668 (Formal, Match, Analyzed_Formal, Assoc),
1669 Assoc);
1670
1671 if Is_Fixed_Point_Type (Entity (Match)) then
1672 Check_Fixed_Point_Actual (Match);
1673 end if;
1674
1675 -- An instantiation is a freeze point for the actuals,
1676 -- unless this is a rewritten formal package, or the
1677 -- formal is an Ada 2012 formal incomplete type.
1678
1679 if Nkind (I_Node) = N_Formal_Package_Declaration
1680 or else
1681 (Ada_Version >= Ada_2012
1682 and then
1683 Ekind (Defining_Identifier (Analyzed_Formal)) =
1684 E_Incomplete_Type)
1685 then
1686 null;
1687
1688 else
1689 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1690 end if;
1691 end if;
1692
1693 -- A remote access-to-class-wide type is not a legal actual
1694 -- for a generic formal of an access type (E.2.2(17/2)).
1695 -- In GNAT an exception to this rule is introduced when
1696 -- the formal is marked as remote using implementation
1697 -- defined aspect/pragma Remote_Access_Type. In that case
1698 -- the actual must be remote as well.
1699
1700 -- If the current instantiation is the construction of a
1701 -- local copy for a formal package the actuals may be
1702 -- defaulted, and there is no matching actual to check.
1703
1704 if Nkind (Analyzed_Formal) = N_Formal_Type_Declaration
1705 and then
1706 Nkind (Formal_Type_Definition (Analyzed_Formal)) =
1707 N_Access_To_Object_Definition
1708 and then Present (Match)
1709 then
1710 declare
1711 Formal_Ent : constant Entity_Id :=
1712 Defining_Identifier (Analyzed_Formal);
1713 begin
1714 if Is_Remote_Access_To_Class_Wide_Type (Entity (Match))
1715 = Is_Remote_Types (Formal_Ent)
1716 then
1717 -- Remoteness of formal and actual match
1718
1719 null;
1720
1721 elsif Is_Remote_Types (Formal_Ent) then
1722
1723 -- Remote formal, non-remote actual
1724
1725 Error_Msg_NE
1726 ("actual for& must be remote", Match, Formal_Ent);
1727
1728 else
1729 -- Non-remote formal, remote actual
1730
1731 Error_Msg_NE
1732 ("actual for& may not be remote",
1733 Match, Formal_Ent);
1734 end if;
1735 end;
1736 end if;
1737
1738 when N_Formal_Subprogram_Declaration =>
1739 Match :=
1740 Matching_Actual
1741 (Defining_Unit_Name (Specification (Formal)),
1742 Defining_Unit_Name (Specification (Analyzed_Formal)));
1743
1744 -- If the formal subprogram has the same name as another
1745 -- formal subprogram of the generic, then a named
1746 -- association is illegal (12.3(9)). Exclude named
1747 -- associations that are generated for a nested instance.
1748
1749 if Present (Match)
1750 and then Is_Named_Assoc
1751 and then Comes_From_Source (Found_Assoc)
1752 then
1753 Check_Overloaded_Formal_Subprogram (Formal);
1754 end if;
1755
1756 -- If there is no corresponding actual, this may be case
1757 -- of partial parameterization, or else the formal has a
1758 -- default or a box.
1759
1760 if No (Match) and then Partial_Parameterization then
1761 Process_Default (Formal);
1762
1763 if Nkind (I_Node) = N_Formal_Package_Declaration then
1764 Check_Overloaded_Formal_Subprogram (Formal);
1765 end if;
1766
1767 else
1768 Append_To (Assoc,
1769 Instantiate_Formal_Subprogram
1770 (Formal, Match, Analyzed_Formal));
1771
1772 -- An instantiation is a freeze point for the actuals,
1773 -- unless this is a rewritten formal package.
1774
1775 if Nkind (I_Node) /= N_Formal_Package_Declaration
1776 and then Nkind (Match) = N_Identifier
1777 and then Is_Subprogram (Entity (Match))
1778
1779 -- The actual subprogram may rename a routine defined
1780 -- in Standard. Avoid freezing such renamings because
1781 -- subprograms coming from Standard cannot be frozen.
1782
1783 and then
1784 not Renames_Standard_Subprogram (Entity (Match))
1785
1786 -- If the actual subprogram comes from a different
1787 -- unit, it is already frozen, either by a body in
1788 -- that unit or by the end of the declarative part
1789 -- of the unit. This check avoids the freezing of
1790 -- subprograms defined in Standard which are used
1791 -- as generic actuals.
1792
1793 and then In_Same_Code_Unit (Entity (Match), I_Node)
1794 and then Has_Fully_Defined_Profile (Entity (Match))
1795 then
1796 -- Mark the subprogram as having a delayed freeze
1797 -- since this may be an out-of-order action.
1798
1799 Set_Has_Delayed_Freeze (Entity (Match));
1800 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1801 end if;
1802 end if;
1803
1804 -- If this is a nested generic, preserve default for later
1805 -- instantiations. We do this as well for GNATProve use,
1806 -- so that the list of generic associations is complete.
1807
1808 if No (Match) and then Box_Present (Formal) then
1809 declare
1810 Subp : constant Entity_Id :=
1811 Defining_Unit_Name (Specification (Last (Assoc)));
1812
1813 begin
1814 Append_To (Default_Actuals,
1815 Make_Generic_Association (Sloc (I_Node),
1816 Selector_Name =>
1817 New_Occurrence_Of (Subp, Sloc (I_Node)),
1818 Explicit_Generic_Actual_Parameter =>
1819 New_Occurrence_Of (Subp, Sloc (I_Node))));
1820 end;
1821 end if;
1822
1823 when N_Formal_Package_Declaration =>
1824 Match :=
1825 Matching_Actual
1826 (Defining_Identifier (Formal),
1827 Defining_Identifier (Original_Node (Analyzed_Formal)));
1828
1829 if No (Match) then
1830 if Partial_Parameterization then
1831 Process_Default (Formal);
1832
1833 else
1834 Error_Msg_Sloc := Sloc (Gen_Unit);
1835 Error_Msg_NE
1836 ("missing actual&",
1837 Instantiation_Node, Defining_Identifier (Formal));
1838 Error_Msg_NE
1839 ("\in instantiation of & declared#",
1840 Instantiation_Node, Gen_Unit);
1841
1842 Abandon_Instantiation (Instantiation_Node);
1843 end if;
1844
1845 else
1846 Analyze (Match);
1847 Append_List
1848 (Instantiate_Formal_Package
1849 (Formal, Match, Analyzed_Formal),
1850 Assoc);
1851 end if;
1852
1853 -- For use type and use package appearing in the generic part,
1854 -- we have already copied them, so we can just move them where
1855 -- they belong (we mustn't recopy them since this would mess up
1856 -- the Sloc values).
1857
1858 when N_Use_Package_Clause |
1859 N_Use_Type_Clause =>
1860 if Nkind (Original_Node (I_Node)) =
1861 N_Formal_Package_Declaration
1862 then
1863 Append (New_Copy_Tree (Formal), Assoc);
1864 else
1865 Remove (Formal);
1866 Append (Formal, Assoc);
1867 end if;
1868
1869 when others =>
1870 raise Program_Error;
1871
1872 end case;
1873
1874 Formal := Saved_Formal;
1875 Next_Non_Pragma (Analyzed_Formal);
1876 end loop;
1877
1878 if Num_Actuals > Num_Matched then
1879 Error_Msg_Sloc := Sloc (Gen_Unit);
1880
1881 if Present (Selector_Name (Actual)) then
1882 Error_Msg_NE
1883 ("unmatched actual &", Actual, Selector_Name (Actual));
1884 Error_Msg_NE
1885 ("\in instantiation of & declared#", Actual, Gen_Unit);
1886 else
1887 Error_Msg_NE
1888 ("unmatched actual in instantiation of & declared#",
1889 Actual, Gen_Unit);
1890 end if;
1891 end if;
1892
1893 elsif Present (Actuals) then
1894 Error_Msg_N
1895 ("too many actuals in generic instantiation", Instantiation_Node);
1896 end if;
1897
1898 -- An instantiation freezes all generic actuals. The only exceptions
1899 -- to this are incomplete types and subprograms which are not fully
1900 -- defined at the point of instantiation.
1901
1902 declare
1903 Elmt : Elmt_Id := First_Elmt (Actuals_To_Freeze);
1904 begin
1905 while Present (Elmt) loop
1906 Freeze_Before (I_Node, Node (Elmt));
1907 Next_Elmt (Elmt);
1908 end loop;
1909 end;
1910
1911 -- If there are default subprograms, normalize the tree by adding
1912 -- explicit associations for them. This is required if the instance
1913 -- appears within a generic.
1914
1915 if not Is_Empty_List (Default_Actuals) then
1916 declare
1917 Default : Node_Id;
1918
1919 begin
1920 Default := First (Default_Actuals);
1921 while Present (Default) loop
1922 Mark_Rewrite_Insertion (Default);
1923 Next (Default);
1924 end loop;
1925
1926 if No (Actuals) then
1927 Set_Generic_Associations (I_Node, Default_Actuals);
1928 else
1929 Append_List_To (Actuals, Default_Actuals);
1930 end if;
1931 end;
1932 end if;
1933
1934 -- If this is a formal package, normalize the parameter list by adding
1935 -- explicit box associations for the formals that are covered by an
1936 -- Others_Choice.
1937
1938 if not Is_Empty_List (Default_Formals) then
1939 Append_List (Default_Formals, Formals);
1940 end if;
1941
1942 return Assoc;
1943 end Analyze_Associations;
1944
1945 -------------------------------
1946 -- Analyze_Formal_Array_Type --
1947 -------------------------------
1948
1949 procedure Analyze_Formal_Array_Type
1950 (T : in out Entity_Id;
1951 Def : Node_Id)
1952 is
1953 DSS : Node_Id;
1954
1955 begin
1956 -- Treated like a non-generic array declaration, with additional
1957 -- semantic checks.
1958
1959 Enter_Name (T);
1960
1961 if Nkind (Def) = N_Constrained_Array_Definition then
1962 DSS := First (Discrete_Subtype_Definitions (Def));
1963 while Present (DSS) loop
1964 if Nkind_In (DSS, N_Subtype_Indication,
1965 N_Range,
1966 N_Attribute_Reference)
1967 then
1968 Error_Msg_N ("only a subtype mark is allowed in a formal", DSS);
1969 end if;
1970
1971 Next (DSS);
1972 end loop;
1973 end if;
1974
1975 Array_Type_Declaration (T, Def);
1976 Set_Is_Generic_Type (Base_Type (T));
1977
1978 if Ekind (Component_Type (T)) = E_Incomplete_Type
1979 and then No (Full_View (Component_Type (T)))
1980 then
1981 Error_Msg_N ("premature usage of incomplete type", Def);
1982
1983 -- Check that range constraint is not allowed on the component type
1984 -- of a generic formal array type (AARM 12.5.3(3))
1985
1986 elsif Is_Internal (Component_Type (T))
1987 and then Present (Subtype_Indication (Component_Definition (Def)))
1988 and then Nkind (Original_Node
1989 (Subtype_Indication (Component_Definition (Def)))) =
1990 N_Subtype_Indication
1991 then
1992 Error_Msg_N
1993 ("in a formal, a subtype indication can only be "
1994 & "a subtype mark (RM 12.5.3(3))",
1995 Subtype_Indication (Component_Definition (Def)));
1996 end if;
1997
1998 end Analyze_Formal_Array_Type;
1999
2000 ---------------------------------------------
2001 -- Analyze_Formal_Decimal_Fixed_Point_Type --
2002 ---------------------------------------------
2003
2004 -- As for other generic types, we create a valid type representation with
2005 -- legal but arbitrary attributes, whose values are never considered
2006 -- static. For all scalar types we introduce an anonymous base type, with
2007 -- the same attributes. We choose the corresponding integer type to be
2008 -- Standard_Integer.
2009 -- Here and in other similar routines, the Sloc of the generated internal
2010 -- type must be the same as the sloc of the defining identifier of the
2011 -- formal type declaration, to provide proper source navigation.
2012
2013 procedure Analyze_Formal_Decimal_Fixed_Point_Type
2014 (T : Entity_Id;
2015 Def : Node_Id)
2016 is
2017 Loc : constant Source_Ptr := Sloc (Def);
2018
2019 Base : constant Entity_Id :=
2020 New_Internal_Entity
2021 (E_Decimal_Fixed_Point_Type,
2022 Current_Scope,
2023 Sloc (Defining_Identifier (Parent (Def))), 'G');
2024
2025 Int_Base : constant Entity_Id := Standard_Integer;
2026 Delta_Val : constant Ureal := Ureal_1;
2027 Digs_Val : constant Uint := Uint_6;
2028
2029 function Make_Dummy_Bound return Node_Id;
2030 -- Return a properly typed universal real literal to use as a bound
2031
2032 ----------------------
2033 -- Make_Dummy_Bound --
2034 ----------------------
2035
2036 function Make_Dummy_Bound return Node_Id is
2037 Bound : constant Node_Id := Make_Real_Literal (Loc, Ureal_1);
2038 begin
2039 Set_Etype (Bound, Universal_Real);
2040 return Bound;
2041 end Make_Dummy_Bound;
2042
2043 -- Start of processing for Analyze_Formal_Decimal_Fixed_Point_Type
2044
2045 begin
2046 Enter_Name (T);
2047
2048 Set_Etype (Base, Base);
2049 Set_Size_Info (Base, Int_Base);
2050 Set_RM_Size (Base, RM_Size (Int_Base));
2051 Set_First_Rep_Item (Base, First_Rep_Item (Int_Base));
2052 Set_Digits_Value (Base, Digs_Val);
2053 Set_Delta_Value (Base, Delta_Val);
2054 Set_Small_Value (Base, Delta_Val);
2055 Set_Scalar_Range (Base,
2056 Make_Range (Loc,
2057 Low_Bound => Make_Dummy_Bound,
2058 High_Bound => Make_Dummy_Bound));
2059
2060 Set_Is_Generic_Type (Base);
2061 Set_Parent (Base, Parent (Def));
2062
2063 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
2064 Set_Etype (T, Base);
2065 Set_Size_Info (T, Int_Base);
2066 Set_RM_Size (T, RM_Size (Int_Base));
2067 Set_First_Rep_Item (T, First_Rep_Item (Int_Base));
2068 Set_Digits_Value (T, Digs_Val);
2069 Set_Delta_Value (T, Delta_Val);
2070 Set_Small_Value (T, Delta_Val);
2071 Set_Scalar_Range (T, Scalar_Range (Base));
2072 Set_Is_Constrained (T);
2073
2074 Check_Restriction (No_Fixed_Point, Def);
2075 end Analyze_Formal_Decimal_Fixed_Point_Type;
2076
2077 -------------------------------------------
2078 -- Analyze_Formal_Derived_Interface_Type --
2079 -------------------------------------------
2080
2081 procedure Analyze_Formal_Derived_Interface_Type
2082 (N : Node_Id;
2083 T : Entity_Id;
2084 Def : Node_Id)
2085 is
2086 Loc : constant Source_Ptr := Sloc (Def);
2087
2088 begin
2089 -- Rewrite as a type declaration of a derived type. This ensures that
2090 -- the interface list and primitive operations are properly captured.
2091
2092 Rewrite (N,
2093 Make_Full_Type_Declaration (Loc,
2094 Defining_Identifier => T,
2095 Type_Definition => Def));
2096 Analyze (N);
2097 Set_Is_Generic_Type (T);
2098 end Analyze_Formal_Derived_Interface_Type;
2099
2100 ---------------------------------
2101 -- Analyze_Formal_Derived_Type --
2102 ---------------------------------
2103
2104 procedure Analyze_Formal_Derived_Type
2105 (N : Node_Id;
2106 T : Entity_Id;
2107 Def : Node_Id)
2108 is
2109 Loc : constant Source_Ptr := Sloc (Def);
2110 Unk_Disc : constant Boolean := Unknown_Discriminants_Present (N);
2111 New_N : Node_Id;
2112
2113 begin
2114 Set_Is_Generic_Type (T);
2115
2116 if Private_Present (Def) then
2117 New_N :=
2118 Make_Private_Extension_Declaration (Loc,
2119 Defining_Identifier => T,
2120 Discriminant_Specifications => Discriminant_Specifications (N),
2121 Unknown_Discriminants_Present => Unk_Disc,
2122 Subtype_Indication => Subtype_Mark (Def),
2123 Interface_List => Interface_List (Def));
2124
2125 Set_Abstract_Present (New_N, Abstract_Present (Def));
2126 Set_Limited_Present (New_N, Limited_Present (Def));
2127 Set_Synchronized_Present (New_N, Synchronized_Present (Def));
2128
2129 else
2130 New_N :=
2131 Make_Full_Type_Declaration (Loc,
2132 Defining_Identifier => T,
2133 Discriminant_Specifications =>
2134 Discriminant_Specifications (Parent (T)),
2135 Type_Definition =>
2136 Make_Derived_Type_Definition (Loc,
2137 Subtype_Indication => Subtype_Mark (Def)));
2138
2139 Set_Abstract_Present
2140 (Type_Definition (New_N), Abstract_Present (Def));
2141 Set_Limited_Present
2142 (Type_Definition (New_N), Limited_Present (Def));
2143 end if;
2144
2145 Rewrite (N, New_N);
2146 Analyze (N);
2147
2148 if Unk_Disc then
2149 if not Is_Composite_Type (T) then
2150 Error_Msg_N
2151 ("unknown discriminants not allowed for elementary types", N);
2152 else
2153 Set_Has_Unknown_Discriminants (T);
2154 Set_Is_Constrained (T, False);
2155 end if;
2156 end if;
2157
2158 -- If the parent type has a known size, so does the formal, which makes
2159 -- legal representation clauses that involve the formal.
2160
2161 Set_Size_Known_At_Compile_Time
2162 (T, Size_Known_At_Compile_Time (Entity (Subtype_Mark (Def))));
2163 end Analyze_Formal_Derived_Type;
2164
2165 ----------------------------------
2166 -- Analyze_Formal_Discrete_Type --
2167 ----------------------------------
2168
2169 -- The operations defined for a discrete types are those of an enumeration
2170 -- type. The size is set to an arbitrary value, for use in analyzing the
2171 -- generic unit.
2172
2173 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id) is
2174 Loc : constant Source_Ptr := Sloc (Def);
2175 Lo : Node_Id;
2176 Hi : Node_Id;
2177
2178 Base : constant Entity_Id :=
2179 New_Internal_Entity
2180 (E_Floating_Point_Type, Current_Scope,
2181 Sloc (Defining_Identifier (Parent (Def))), 'G');
2182
2183 begin
2184 Enter_Name (T);
2185 Set_Ekind (T, E_Enumeration_Subtype);
2186 Set_Etype (T, Base);
2187 Init_Size (T, 8);
2188 Init_Alignment (T);
2189 Set_Is_Generic_Type (T);
2190 Set_Is_Constrained (T);
2191
2192 -- For semantic analysis, the bounds of the type must be set to some
2193 -- non-static value. The simplest is to create attribute nodes for those
2194 -- bounds, that refer to the type itself. These bounds are never
2195 -- analyzed but serve as place-holders.
2196
2197 Lo :=
2198 Make_Attribute_Reference (Loc,
2199 Attribute_Name => Name_First,
2200 Prefix => New_Occurrence_Of (T, Loc));
2201 Set_Etype (Lo, T);
2202
2203 Hi :=
2204 Make_Attribute_Reference (Loc,
2205 Attribute_Name => Name_Last,
2206 Prefix => New_Occurrence_Of (T, Loc));
2207 Set_Etype (Hi, T);
2208
2209 Set_Scalar_Range (T,
2210 Make_Range (Loc,
2211 Low_Bound => Lo,
2212 High_Bound => Hi));
2213
2214 Set_Ekind (Base, E_Enumeration_Type);
2215 Set_Etype (Base, Base);
2216 Init_Size (Base, 8);
2217 Init_Alignment (Base);
2218 Set_Is_Generic_Type (Base);
2219 Set_Scalar_Range (Base, Scalar_Range (T));
2220 Set_Parent (Base, Parent (Def));
2221 end Analyze_Formal_Discrete_Type;
2222
2223 ----------------------------------
2224 -- Analyze_Formal_Floating_Type --
2225 ---------------------------------
2226
2227 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id) is
2228 Base : constant Entity_Id :=
2229 New_Internal_Entity
2230 (E_Floating_Point_Type, Current_Scope,
2231 Sloc (Defining_Identifier (Parent (Def))), 'G');
2232
2233 begin
2234 -- The various semantic attributes are taken from the predefined type
2235 -- Float, just so that all of them are initialized. Their values are
2236 -- never used because no constant folding or expansion takes place in
2237 -- the generic itself.
2238
2239 Enter_Name (T);
2240 Set_Ekind (T, E_Floating_Point_Subtype);
2241 Set_Etype (T, Base);
2242 Set_Size_Info (T, (Standard_Float));
2243 Set_RM_Size (T, RM_Size (Standard_Float));
2244 Set_Digits_Value (T, Digits_Value (Standard_Float));
2245 Set_Scalar_Range (T, Scalar_Range (Standard_Float));
2246 Set_Is_Constrained (T);
2247
2248 Set_Is_Generic_Type (Base);
2249 Set_Etype (Base, Base);
2250 Set_Size_Info (Base, (Standard_Float));
2251 Set_RM_Size (Base, RM_Size (Standard_Float));
2252 Set_Digits_Value (Base, Digits_Value (Standard_Float));
2253 Set_Scalar_Range (Base, Scalar_Range (Standard_Float));
2254 Set_Parent (Base, Parent (Def));
2255
2256 Check_Restriction (No_Floating_Point, Def);
2257 end Analyze_Formal_Floating_Type;
2258
2259 -----------------------------------
2260 -- Analyze_Formal_Interface_Type;--
2261 -----------------------------------
2262
2263 procedure Analyze_Formal_Interface_Type
2264 (N : Node_Id;
2265 T : Entity_Id;
2266 Def : Node_Id)
2267 is
2268 Loc : constant Source_Ptr := Sloc (N);
2269 New_N : Node_Id;
2270
2271 begin
2272 New_N :=
2273 Make_Full_Type_Declaration (Loc,
2274 Defining_Identifier => T,
2275 Type_Definition => Def);
2276
2277 Rewrite (N, New_N);
2278 Analyze (N);
2279 Set_Is_Generic_Type (T);
2280 end Analyze_Formal_Interface_Type;
2281
2282 ---------------------------------
2283 -- Analyze_Formal_Modular_Type --
2284 ---------------------------------
2285
2286 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id) is
2287 begin
2288 -- Apart from their entity kind, generic modular types are treated like
2289 -- signed integer types, and have the same attributes.
2290
2291 Analyze_Formal_Signed_Integer_Type (T, Def);
2292 Set_Ekind (T, E_Modular_Integer_Subtype);
2293 Set_Ekind (Etype (T), E_Modular_Integer_Type);
2294
2295 end Analyze_Formal_Modular_Type;
2296
2297 ---------------------------------------
2298 -- Analyze_Formal_Object_Declaration --
2299 ---------------------------------------
2300
2301 procedure Analyze_Formal_Object_Declaration (N : Node_Id) is
2302 E : constant Node_Id := Default_Expression (N);
2303 Id : constant Node_Id := Defining_Identifier (N);
2304 K : Entity_Kind;
2305 T : Node_Id;
2306
2307 begin
2308 Enter_Name (Id);
2309
2310 -- Determine the mode of the formal object
2311
2312 if Out_Present (N) then
2313 K := E_Generic_In_Out_Parameter;
2314
2315 if not In_Present (N) then
2316 Error_Msg_N ("formal generic objects cannot have mode OUT", N);
2317 end if;
2318
2319 else
2320 K := E_Generic_In_Parameter;
2321 end if;
2322
2323 if Present (Subtype_Mark (N)) then
2324 Find_Type (Subtype_Mark (N));
2325 T := Entity (Subtype_Mark (N));
2326
2327 -- Verify that there is no redundant null exclusion
2328
2329 if Null_Exclusion_Present (N) then
2330 if not Is_Access_Type (T) then
2331 Error_Msg_N
2332 ("null exclusion can only apply to an access type", N);
2333
2334 elsif Can_Never_Be_Null (T) then
2335 Error_Msg_NE
2336 ("`NOT NULL` not allowed (& already excludes null)", N, T);
2337 end if;
2338 end if;
2339
2340 -- Ada 2005 (AI-423): Formal object with an access definition
2341
2342 else
2343 Check_Access_Definition (N);
2344 T := Access_Definition
2345 (Related_Nod => N,
2346 N => Access_Definition (N));
2347 end if;
2348
2349 if Ekind (T) = E_Incomplete_Type then
2350 declare
2351 Error_Node : Node_Id;
2352
2353 begin
2354 if Present (Subtype_Mark (N)) then
2355 Error_Node := Subtype_Mark (N);
2356 else
2357 Check_Access_Definition (N);
2358 Error_Node := Access_Definition (N);
2359 end if;
2360
2361 Error_Msg_N ("premature usage of incomplete type", Error_Node);
2362 end;
2363 end if;
2364
2365 if K = E_Generic_In_Parameter then
2366
2367 -- Ada 2005 (AI-287): Limited aggregates allowed in generic formals
2368
2369 if Ada_Version < Ada_2005 and then Is_Limited_Type (T) then
2370 Error_Msg_N
2371 ("generic formal of mode IN must not be of limited type", N);
2372 Explain_Limited_Type (T, N);
2373 end if;
2374
2375 if Is_Abstract_Type (T) then
2376 Error_Msg_N
2377 ("generic formal of mode IN must not be of abstract type", N);
2378 end if;
2379
2380 if Present (E) then
2381 Preanalyze_Spec_Expression (E, T);
2382
2383 if Is_Limited_Type (T) and then not OK_For_Limited_Init (T, E) then
2384 Error_Msg_N
2385 ("initialization not allowed for limited types", E);
2386 Explain_Limited_Type (T, E);
2387 end if;
2388 end if;
2389
2390 Set_Ekind (Id, K);
2391 Set_Etype (Id, T);
2392
2393 -- Case of generic IN OUT parameter
2394
2395 else
2396 -- If the formal has an unconstrained type, construct its actual
2397 -- subtype, as is done for subprogram formals. In this fashion, all
2398 -- its uses can refer to specific bounds.
2399
2400 Set_Ekind (Id, K);
2401 Set_Etype (Id, T);
2402
2403 if (Is_Array_Type (T) and then not Is_Constrained (T))
2404 or else (Ekind (T) = E_Record_Type and then Has_Discriminants (T))
2405 then
2406 declare
2407 Non_Freezing_Ref : constant Node_Id :=
2408 New_Occurrence_Of (Id, Sloc (Id));
2409 Decl : Node_Id;
2410
2411 begin
2412 -- Make sure the actual subtype doesn't generate bogus freezing
2413
2414 Set_Must_Not_Freeze (Non_Freezing_Ref);
2415 Decl := Build_Actual_Subtype (T, Non_Freezing_Ref);
2416 Insert_Before_And_Analyze (N, Decl);
2417 Set_Actual_Subtype (Id, Defining_Identifier (Decl));
2418 end;
2419 else
2420 Set_Actual_Subtype (Id, T);
2421 end if;
2422
2423 if Present (E) then
2424 Error_Msg_N
2425 ("initialization not allowed for `IN OUT` formals", N);
2426 end if;
2427 end if;
2428
2429 if Has_Aspects (N) then
2430 Analyze_Aspect_Specifications (N, Id);
2431 end if;
2432 end Analyze_Formal_Object_Declaration;
2433
2434 ----------------------------------------------
2435 -- Analyze_Formal_Ordinary_Fixed_Point_Type --
2436 ----------------------------------------------
2437
2438 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
2439 (T : Entity_Id;
2440 Def : Node_Id)
2441 is
2442 Loc : constant Source_Ptr := Sloc (Def);
2443 Base : constant Entity_Id :=
2444 New_Internal_Entity
2445 (E_Ordinary_Fixed_Point_Type, Current_Scope,
2446 Sloc (Defining_Identifier (Parent (Def))), 'G');
2447
2448 begin
2449 -- The semantic attributes are set for completeness only, their values
2450 -- will never be used, since all properties of the type are non-static.
2451
2452 Enter_Name (T);
2453 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
2454 Set_Etype (T, Base);
2455 Set_Size_Info (T, Standard_Integer);
2456 Set_RM_Size (T, RM_Size (Standard_Integer));
2457 Set_Small_Value (T, Ureal_1);
2458 Set_Delta_Value (T, Ureal_1);
2459 Set_Scalar_Range (T,
2460 Make_Range (Loc,
2461 Low_Bound => Make_Real_Literal (Loc, Ureal_1),
2462 High_Bound => Make_Real_Literal (Loc, Ureal_1)));
2463 Set_Is_Constrained (T);
2464
2465 Set_Is_Generic_Type (Base);
2466 Set_Etype (Base, Base);
2467 Set_Size_Info (Base, Standard_Integer);
2468 Set_RM_Size (Base, RM_Size (Standard_Integer));
2469 Set_Small_Value (Base, Ureal_1);
2470 Set_Delta_Value (Base, Ureal_1);
2471 Set_Scalar_Range (Base, Scalar_Range (T));
2472 Set_Parent (Base, Parent (Def));
2473
2474 Check_Restriction (No_Fixed_Point, Def);
2475 end Analyze_Formal_Ordinary_Fixed_Point_Type;
2476
2477 ----------------------------------------
2478 -- Analyze_Formal_Package_Declaration --
2479 ----------------------------------------
2480
2481 procedure Analyze_Formal_Package_Declaration (N : Node_Id) is
2482 Gen_Id : constant Node_Id := Name (N);
2483 Loc : constant Source_Ptr := Sloc (N);
2484 Pack_Id : constant Entity_Id := Defining_Identifier (N);
2485 Formal : Entity_Id;
2486 Gen_Decl : Node_Id;
2487 Gen_Unit : Entity_Id;
2488 Renaming : Node_Id;
2489
2490 Vis_Prims_List : Elist_Id := No_Elist;
2491 -- List of primitives made temporarily visible in the instantiation
2492 -- to match the visibility of the formal type.
2493
2494 function Build_Local_Package return Node_Id;
2495 -- The formal package is rewritten so that its parameters are replaced
2496 -- with corresponding declarations. For parameters with bona fide
2497 -- associations these declarations are created by Analyze_Associations
2498 -- as for a regular instantiation. For boxed parameters, we preserve
2499 -- the formal declarations and analyze them, in order to introduce
2500 -- entities of the right kind in the environment of the formal.
2501
2502 -------------------------
2503 -- Build_Local_Package --
2504 -------------------------
2505
2506 function Build_Local_Package return Node_Id is
2507 Decls : List_Id;
2508 Pack_Decl : Node_Id;
2509
2510 begin
2511 -- Within the formal, the name of the generic package is a renaming
2512 -- of the formal (as for a regular instantiation).
2513
2514 Pack_Decl :=
2515 Make_Package_Declaration (Loc,
2516 Specification =>
2517 Copy_Generic_Node
2518 (Specification (Original_Node (Gen_Decl)),
2519 Empty, Instantiating => True));
2520
2521 Renaming :=
2522 Make_Package_Renaming_Declaration (Loc,
2523 Defining_Unit_Name =>
2524 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
2525 Name => New_Occurrence_Of (Formal, Loc));
2526
2527 if Nkind (Gen_Id) = N_Identifier
2528 and then Chars (Gen_Id) = Chars (Pack_Id)
2529 then
2530 Error_Msg_NE
2531 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
2532 end if;
2533
2534 -- If the formal is declared with a box, or with an others choice,
2535 -- create corresponding declarations for all entities in the formal
2536 -- part, so that names with the proper types are available in the
2537 -- specification of the formal package.
2538
2539 -- On the other hand, if there are no associations, then all the
2540 -- formals must have defaults, and this will be checked by the
2541 -- call to Analyze_Associations.
2542
2543 if Box_Present (N)
2544 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2545 then
2546 declare
2547 Formal_Decl : Node_Id;
2548
2549 begin
2550 -- TBA : for a formal package, need to recurse ???
2551
2552 Decls := New_List;
2553 Formal_Decl :=
2554 First
2555 (Generic_Formal_Declarations (Original_Node (Gen_Decl)));
2556 while Present (Formal_Decl) loop
2557 Append_To
2558 (Decls, Copy_Generic_Node (Formal_Decl, Empty, True));
2559 Next (Formal_Decl);
2560 end loop;
2561 end;
2562
2563 -- If generic associations are present, use Analyze_Associations to
2564 -- create the proper renaming declarations.
2565
2566 else
2567 declare
2568 Act_Tree : constant Node_Id :=
2569 Copy_Generic_Node
2570 (Original_Node (Gen_Decl), Empty,
2571 Instantiating => True);
2572
2573 begin
2574 Generic_Renamings.Set_Last (0);
2575 Generic_Renamings_HTable.Reset;
2576 Instantiation_Node := N;
2577
2578 Decls :=
2579 Analyze_Associations
2580 (I_Node => Original_Node (N),
2581 Formals => Generic_Formal_Declarations (Act_Tree),
2582 F_Copy => Generic_Formal_Declarations (Gen_Decl));
2583
2584 Vis_Prims_List := Check_Hidden_Primitives (Decls);
2585 end;
2586 end if;
2587
2588 Append (Renaming, To => Decls);
2589
2590 -- Add generated declarations ahead of local declarations in
2591 -- the package.
2592
2593 if No (Visible_Declarations (Specification (Pack_Decl))) then
2594 Set_Visible_Declarations (Specification (Pack_Decl), Decls);
2595 else
2596 Insert_List_Before
2597 (First (Visible_Declarations (Specification (Pack_Decl))),
2598 Decls);
2599 end if;
2600
2601 return Pack_Decl;
2602 end Build_Local_Package;
2603
2604 -- Local variables
2605
2606 Save_IPSM : constant Boolean := Ignore_Pragma_SPARK_Mode;
2607 -- Save flag Ignore_Pragma_SPARK_Mode for restore on exit
2608
2609 Associations : Boolean := True;
2610 New_N : Node_Id;
2611 Parent_Installed : Boolean := False;
2612 Parent_Instance : Entity_Id;
2613 Renaming_In_Par : Entity_Id;
2614
2615 -- Start of processing for Analyze_Formal_Package_Declaration
2616
2617 begin
2618 Check_Text_IO_Special_Unit (Gen_Id);
2619
2620 Init_Env;
2621 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
2622 Gen_Unit := Entity (Gen_Id);
2623
2624 -- Check for a formal package that is a package renaming
2625
2626 if Present (Renamed_Object (Gen_Unit)) then
2627
2628 -- Indicate that unit is used, before replacing it with renamed
2629 -- entity for use below.
2630
2631 if In_Extended_Main_Source_Unit (N) then
2632 Set_Is_Instantiated (Gen_Unit);
2633 Generate_Reference (Gen_Unit, N);
2634 end if;
2635
2636 Gen_Unit := Renamed_Object (Gen_Unit);
2637 end if;
2638
2639 if Ekind (Gen_Unit) /= E_Generic_Package then
2640 Error_Msg_N ("expect generic package name", Gen_Id);
2641 Restore_Env;
2642 goto Leave;
2643
2644 elsif Gen_Unit = Current_Scope then
2645 Error_Msg_N
2646 ("generic package cannot be used as a formal package of itself",
2647 Gen_Id);
2648 Restore_Env;
2649 goto Leave;
2650
2651 elsif In_Open_Scopes (Gen_Unit) then
2652 if Is_Compilation_Unit (Gen_Unit)
2653 and then Is_Child_Unit (Current_Scope)
2654 then
2655 -- Special-case the error when the formal is a parent, and
2656 -- continue analysis to minimize cascaded errors.
2657
2658 Error_Msg_N
2659 ("generic parent cannot be used as formal package "
2660 & "of a child unit", Gen_Id);
2661
2662 else
2663 Error_Msg_N
2664 ("generic package cannot be used as a formal package "
2665 & "within itself", Gen_Id);
2666 Restore_Env;
2667 goto Leave;
2668 end if;
2669 end if;
2670
2671 -- Check that name of formal package does not hide name of generic,
2672 -- or its leading prefix. This check must be done separately because
2673 -- the name of the generic has already been analyzed.
2674
2675 declare
2676 Gen_Name : Entity_Id;
2677
2678 begin
2679 Gen_Name := Gen_Id;
2680 while Nkind (Gen_Name) = N_Expanded_Name loop
2681 Gen_Name := Prefix (Gen_Name);
2682 end loop;
2683
2684 if Chars (Gen_Name) = Chars (Pack_Id) then
2685 Error_Msg_NE
2686 ("& is hidden within declaration of formal package",
2687 Gen_Id, Gen_Name);
2688 end if;
2689 end;
2690
2691 if Box_Present (N)
2692 or else No (Generic_Associations (N))
2693 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2694 then
2695 Associations := False;
2696 end if;
2697
2698 -- If there are no generic associations, the generic parameters appear
2699 -- as local entities and are instantiated like them. We copy the generic
2700 -- package declaration as if it were an instantiation, and analyze it
2701 -- like a regular package, except that we treat the formals as
2702 -- additional visible components.
2703
2704 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
2705
2706 if In_Extended_Main_Source_Unit (N) then
2707 Set_Is_Instantiated (Gen_Unit);
2708 Generate_Reference (Gen_Unit, N);
2709 end if;
2710
2711 Formal := New_Copy (Pack_Id);
2712 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
2713
2714 -- Make local generic without formals. The formals will be replaced with
2715 -- internal declarations.
2716
2717 begin
2718 New_N := Build_Local_Package;
2719
2720 -- If there are errors in the parameter list, Analyze_Associations
2721 -- raises Instantiation_Error. Patch the declaration to prevent further
2722 -- exception propagation.
2723
2724 exception
2725 when Instantiation_Error =>
2726 Enter_Name (Formal);
2727 Set_Ekind (Formal, E_Variable);
2728 Set_Etype (Formal, Any_Type);
2729 Restore_Hidden_Primitives (Vis_Prims_List);
2730
2731 if Parent_Installed then
2732 Remove_Parent;
2733 end if;
2734
2735 goto Leave;
2736 end;
2737
2738 Rewrite (N, New_N);
2739 Set_Defining_Unit_Name (Specification (New_N), Formal);
2740 Set_Generic_Parent (Specification (N), Gen_Unit);
2741 Set_Instance_Env (Gen_Unit, Formal);
2742 Set_Is_Generic_Instance (Formal);
2743
2744 Enter_Name (Formal);
2745 Set_Ekind (Formal, E_Package);
2746 Set_Etype (Formal, Standard_Void_Type);
2747 Set_Inner_Instances (Formal, New_Elmt_List);
2748 Push_Scope (Formal);
2749
2750 -- Manually set the SPARK_Mode from the context because the package
2751 -- declaration is never analyzed.
2752
2753 Set_SPARK_Pragma (Formal, SPARK_Mode_Pragma);
2754 Set_SPARK_Aux_Pragma (Formal, SPARK_Mode_Pragma);
2755 Set_SPARK_Pragma_Inherited (Formal);
2756 Set_SPARK_Aux_Pragma_Inherited (Formal);
2757
2758 if Is_Child_Unit (Gen_Unit) and then Parent_Installed then
2759
2760 -- Similarly, we have to make the name of the formal visible in the
2761 -- parent instance, to resolve properly fully qualified names that
2762 -- may appear in the generic unit. The parent instance has been
2763 -- placed on the scope stack ahead of the current scope.
2764
2765 Parent_Instance := Scope_Stack.Table (Scope_Stack.Last - 1).Entity;
2766
2767 Renaming_In_Par :=
2768 Make_Defining_Identifier (Loc, Chars (Gen_Unit));
2769 Set_Ekind (Renaming_In_Par, E_Package);
2770 Set_Etype (Renaming_In_Par, Standard_Void_Type);
2771 Set_Scope (Renaming_In_Par, Parent_Instance);
2772 Set_Parent (Renaming_In_Par, Parent (Formal));
2773 Set_Renamed_Object (Renaming_In_Par, Formal);
2774 Append_Entity (Renaming_In_Par, Parent_Instance);
2775 end if;
2776
2777 -- A formal package declaration behaves as a package instantiation with
2778 -- respect to SPARK_Mode "off". If the annotation is "off" or altogether
2779 -- missing, set the global flag which signals Analyze_Pragma to ingnore
2780 -- all SPARK_Mode pragmas within the generic_package_name.
2781
2782 if SPARK_Mode /= On then
2783 Ignore_Pragma_SPARK_Mode := True;
2784 end if;
2785
2786 Analyze (Specification (N));
2787
2788 -- The formals for which associations are provided are not visible
2789 -- outside of the formal package. The others are still declared by a
2790 -- formal parameter declaration.
2791
2792 -- If there are no associations, the only local entity to hide is the
2793 -- generated package renaming itself.
2794
2795 declare
2796 E : Entity_Id;
2797
2798 begin
2799 E := First_Entity (Formal);
2800 while Present (E) loop
2801 if Associations and then not Is_Generic_Formal (E) then
2802 Set_Is_Hidden (E);
2803 end if;
2804
2805 if Ekind (E) = E_Package and then Renamed_Entity (E) = Formal then
2806 Set_Is_Hidden (E);
2807 exit;
2808 end if;
2809
2810 Next_Entity (E);
2811 end loop;
2812 end;
2813
2814 End_Package_Scope (Formal);
2815 Restore_Hidden_Primitives (Vis_Prims_List);
2816
2817 if Parent_Installed then
2818 Remove_Parent;
2819 end if;
2820
2821 Restore_Env;
2822
2823 -- Inside the generic unit, the formal package is a regular package, but
2824 -- no body is needed for it. Note that after instantiation, the defining
2825 -- unit name we need is in the new tree and not in the original (see
2826 -- Package_Instantiation). A generic formal package is an instance, and
2827 -- can be used as an actual for an inner instance.
2828
2829 Set_Has_Completion (Formal, True);
2830
2831 -- Add semantic information to the original defining identifier for ASIS
2832 -- use.
2833
2834 Set_Ekind (Pack_Id, E_Package);
2835 Set_Etype (Pack_Id, Standard_Void_Type);
2836 Set_Scope (Pack_Id, Scope (Formal));
2837 Set_Has_Completion (Pack_Id, True);
2838
2839 <<Leave>>
2840 if Has_Aspects (N) then
2841 Analyze_Aspect_Specifications (N, Pack_Id);
2842 end if;
2843
2844 Ignore_Pragma_SPARK_Mode := Save_IPSM;
2845 end Analyze_Formal_Package_Declaration;
2846
2847 ---------------------------------
2848 -- Analyze_Formal_Private_Type --
2849 ---------------------------------
2850
2851 procedure Analyze_Formal_Private_Type
2852 (N : Node_Id;
2853 T : Entity_Id;
2854 Def : Node_Id)
2855 is
2856 begin
2857 New_Private_Type (N, T, Def);
2858
2859 -- Set the size to an arbitrary but legal value
2860
2861 Set_Size_Info (T, Standard_Integer);
2862 Set_RM_Size (T, RM_Size (Standard_Integer));
2863 end Analyze_Formal_Private_Type;
2864
2865 ------------------------------------
2866 -- Analyze_Formal_Incomplete_Type --
2867 ------------------------------------
2868
2869 procedure Analyze_Formal_Incomplete_Type
2870 (T : Entity_Id;
2871 Def : Node_Id)
2872 is
2873 begin
2874 Enter_Name (T);
2875 Set_Ekind (T, E_Incomplete_Type);
2876 Set_Etype (T, T);
2877 Set_Private_Dependents (T, New_Elmt_List);
2878
2879 if Tagged_Present (Def) then
2880 Set_Is_Tagged_Type (T);
2881 Make_Class_Wide_Type (T);
2882 Set_Direct_Primitive_Operations (T, New_Elmt_List);
2883 end if;
2884 end Analyze_Formal_Incomplete_Type;
2885
2886 ----------------------------------------
2887 -- Analyze_Formal_Signed_Integer_Type --
2888 ----------------------------------------
2889
2890 procedure Analyze_Formal_Signed_Integer_Type
2891 (T : Entity_Id;
2892 Def : Node_Id)
2893 is
2894 Base : constant Entity_Id :=
2895 New_Internal_Entity
2896 (E_Signed_Integer_Type,
2897 Current_Scope,
2898 Sloc (Defining_Identifier (Parent (Def))), 'G');
2899
2900 begin
2901 Enter_Name (T);
2902
2903 Set_Ekind (T, E_Signed_Integer_Subtype);
2904 Set_Etype (T, Base);
2905 Set_Size_Info (T, Standard_Integer);
2906 Set_RM_Size (T, RM_Size (Standard_Integer));
2907 Set_Scalar_Range (T, Scalar_Range (Standard_Integer));
2908 Set_Is_Constrained (T);
2909
2910 Set_Is_Generic_Type (Base);
2911 Set_Size_Info (Base, Standard_Integer);
2912 Set_RM_Size (Base, RM_Size (Standard_Integer));
2913 Set_Etype (Base, Base);
2914 Set_Scalar_Range (Base, Scalar_Range (Standard_Integer));
2915 Set_Parent (Base, Parent (Def));
2916 end Analyze_Formal_Signed_Integer_Type;
2917
2918 -------------------------------------------
2919 -- Analyze_Formal_Subprogram_Declaration --
2920 -------------------------------------------
2921
2922 procedure Analyze_Formal_Subprogram_Declaration (N : Node_Id) is
2923 Spec : constant Node_Id := Specification (N);
2924 Def : constant Node_Id := Default_Name (N);
2925 Nam : constant Entity_Id := Defining_Unit_Name (Spec);
2926 Subp : Entity_Id;
2927
2928 begin
2929 if Nam = Error then
2930 return;
2931 end if;
2932
2933 if Nkind (Nam) = N_Defining_Program_Unit_Name then
2934 Error_Msg_N ("name of formal subprogram must be a direct name", Nam);
2935 goto Leave;
2936 end if;
2937
2938 Analyze_Subprogram_Declaration (N);
2939 Set_Is_Formal_Subprogram (Nam);
2940 Set_Has_Completion (Nam);
2941
2942 if Nkind (N) = N_Formal_Abstract_Subprogram_Declaration then
2943 Set_Is_Abstract_Subprogram (Nam);
2944
2945 Set_Is_Dispatching_Operation (Nam);
2946
2947 -- A formal abstract procedure cannot have a null default
2948 -- (RM 12.6(4.1/2)).
2949
2950 if Nkind (Spec) = N_Procedure_Specification
2951 and then Null_Present (Spec)
2952 then
2953 Error_Msg_N
2954 ("a formal abstract subprogram cannot default to null", Spec);
2955 end if;
2956
2957 declare
2958 Ctrl_Type : constant Entity_Id := Find_Dispatching_Type (Nam);
2959 begin
2960 if No (Ctrl_Type) then
2961 Error_Msg_N
2962 ("abstract formal subprogram must have a controlling type",
2963 N);
2964
2965 elsif Ada_Version >= Ada_2012
2966 and then Is_Incomplete_Type (Ctrl_Type)
2967 then
2968 Error_Msg_NE
2969 ("controlling type of abstract formal subprogram cannot "
2970 & "be incomplete type", N, Ctrl_Type);
2971
2972 else
2973 Check_Controlling_Formals (Ctrl_Type, Nam);
2974 end if;
2975 end;
2976 end if;
2977
2978 -- Default name is resolved at the point of instantiation
2979
2980 if Box_Present (N) then
2981 null;
2982
2983 -- Else default is bound at the point of generic declaration
2984
2985 elsif Present (Def) then
2986 if Nkind (Def) = N_Operator_Symbol then
2987 Find_Direct_Name (Def);
2988
2989 elsif Nkind (Def) /= N_Attribute_Reference then
2990 Analyze (Def);
2991
2992 else
2993 -- For an attribute reference, analyze the prefix and verify
2994 -- that it has the proper profile for the subprogram.
2995
2996 Analyze (Prefix (Def));
2997 Valid_Default_Attribute (Nam, Def);
2998 goto Leave;
2999 end if;
3000
3001 -- Default name may be overloaded, in which case the interpretation
3002 -- with the correct profile must be selected, as for a renaming.
3003 -- If the definition is an indexed component, it must denote a
3004 -- member of an entry family. If it is a selected component, it
3005 -- can be a protected operation.
3006
3007 if Etype (Def) = Any_Type then
3008 goto Leave;
3009
3010 elsif Nkind (Def) = N_Selected_Component then
3011 if not Is_Overloadable (Entity (Selector_Name (Def))) then
3012 Error_Msg_N ("expect valid subprogram name as default", Def);
3013 end if;
3014
3015 elsif Nkind (Def) = N_Indexed_Component then
3016 if Is_Entity_Name (Prefix (Def)) then
3017 if Ekind (Entity (Prefix (Def))) /= E_Entry_Family then
3018 Error_Msg_N ("expect valid subprogram name as default", Def);
3019 end if;
3020
3021 elsif Nkind (Prefix (Def)) = N_Selected_Component then
3022 if Ekind (Entity (Selector_Name (Prefix (Def)))) /=
3023 E_Entry_Family
3024 then
3025 Error_Msg_N ("expect valid subprogram name as default", Def);
3026 end if;
3027
3028 else
3029 Error_Msg_N ("expect valid subprogram name as default", Def);
3030 goto Leave;
3031 end if;
3032
3033 elsif Nkind (Def) = N_Character_Literal then
3034
3035 -- Needs some type checks: subprogram should be parameterless???
3036
3037 Resolve (Def, (Etype (Nam)));
3038
3039 elsif not Is_Entity_Name (Def)
3040 or else not Is_Overloadable (Entity (Def))
3041 then
3042 Error_Msg_N ("expect valid subprogram name as default", Def);
3043 goto Leave;
3044
3045 elsif not Is_Overloaded (Def) then
3046 Subp := Entity (Def);
3047
3048 if Subp = Nam then
3049 Error_Msg_N ("premature usage of formal subprogram", Def);
3050
3051 elsif not Entity_Matches_Spec (Subp, Nam) then
3052 Error_Msg_N ("no visible entity matches specification", Def);
3053 end if;
3054
3055 -- More than one interpretation, so disambiguate as for a renaming
3056
3057 else
3058 declare
3059 I : Interp_Index;
3060 I1 : Interp_Index := 0;
3061 It : Interp;
3062 It1 : Interp;
3063
3064 begin
3065 Subp := Any_Id;
3066 Get_First_Interp (Def, I, It);
3067 while Present (It.Nam) loop
3068 if Entity_Matches_Spec (It.Nam, Nam) then
3069 if Subp /= Any_Id then
3070 It1 := Disambiguate (Def, I1, I, Etype (Subp));
3071
3072 if It1 = No_Interp then
3073 Error_Msg_N ("ambiguous default subprogram", Def);
3074 else
3075 Subp := It1.Nam;
3076 end if;
3077
3078 exit;
3079
3080 else
3081 I1 := I;
3082 Subp := It.Nam;
3083 end if;
3084 end if;
3085
3086 Get_Next_Interp (I, It);
3087 end loop;
3088 end;
3089
3090 if Subp /= Any_Id then
3091
3092 -- Subprogram found, generate reference to it
3093
3094 Set_Entity (Def, Subp);
3095 Generate_Reference (Subp, Def);
3096
3097 if Subp = Nam then
3098 Error_Msg_N ("premature usage of formal subprogram", Def);
3099
3100 elsif Ekind (Subp) /= E_Operator then
3101 Check_Mode_Conformant (Subp, Nam);
3102 end if;
3103
3104 else
3105 Error_Msg_N ("no visible subprogram matches specification", N);
3106 end if;
3107 end if;
3108 end if;
3109
3110 <<Leave>>
3111 if Has_Aspects (N) then
3112 Analyze_Aspect_Specifications (N, Nam);
3113 end if;
3114
3115 end Analyze_Formal_Subprogram_Declaration;
3116
3117 -------------------------------------
3118 -- Analyze_Formal_Type_Declaration --
3119 -------------------------------------
3120
3121 procedure Analyze_Formal_Type_Declaration (N : Node_Id) is
3122 Def : constant Node_Id := Formal_Type_Definition (N);
3123 T : Entity_Id;
3124
3125 begin
3126 T := Defining_Identifier (N);
3127
3128 if Present (Discriminant_Specifications (N))
3129 and then Nkind (Def) /= N_Formal_Private_Type_Definition
3130 then
3131 Error_Msg_N
3132 ("discriminants not allowed for this formal type", T);
3133 end if;
3134
3135 -- Enter the new name, and branch to specific routine
3136
3137 case Nkind (Def) is
3138 when N_Formal_Private_Type_Definition =>
3139 Analyze_Formal_Private_Type (N, T, Def);
3140
3141 when N_Formal_Derived_Type_Definition =>
3142 Analyze_Formal_Derived_Type (N, T, Def);
3143
3144 when N_Formal_Incomplete_Type_Definition =>
3145 Analyze_Formal_Incomplete_Type (T, Def);
3146
3147 when N_Formal_Discrete_Type_Definition =>
3148 Analyze_Formal_Discrete_Type (T, Def);
3149
3150 when N_Formal_Signed_Integer_Type_Definition =>
3151 Analyze_Formal_Signed_Integer_Type (T, Def);
3152
3153 when N_Formal_Modular_Type_Definition =>
3154 Analyze_Formal_Modular_Type (T, Def);
3155
3156 when N_Formal_Floating_Point_Definition =>
3157 Analyze_Formal_Floating_Type (T, Def);
3158
3159 when N_Formal_Ordinary_Fixed_Point_Definition =>
3160 Analyze_Formal_Ordinary_Fixed_Point_Type (T, Def);
3161
3162 when N_Formal_Decimal_Fixed_Point_Definition =>
3163 Analyze_Formal_Decimal_Fixed_Point_Type (T, Def);
3164
3165 when N_Array_Type_Definition =>
3166 Analyze_Formal_Array_Type (T, Def);
3167
3168 when N_Access_To_Object_Definition |
3169 N_Access_Function_Definition |
3170 N_Access_Procedure_Definition =>
3171 Analyze_Generic_Access_Type (T, Def);
3172
3173 -- Ada 2005: a interface declaration is encoded as an abstract
3174 -- record declaration or a abstract type derivation.
3175
3176 when N_Record_Definition =>
3177 Analyze_Formal_Interface_Type (N, T, Def);
3178
3179 when N_Derived_Type_Definition =>
3180 Analyze_Formal_Derived_Interface_Type (N, T, Def);
3181
3182 when N_Error =>
3183 null;
3184
3185 when others =>
3186 raise Program_Error;
3187
3188 end case;
3189
3190 Set_Is_Generic_Type (T);
3191
3192 if Has_Aspects (N) then
3193 Analyze_Aspect_Specifications (N, T);
3194 end if;
3195 end Analyze_Formal_Type_Declaration;
3196
3197 ------------------------------------
3198 -- Analyze_Function_Instantiation --
3199 ------------------------------------
3200
3201 procedure Analyze_Function_Instantiation (N : Node_Id) is
3202 begin
3203 Analyze_Subprogram_Instantiation (N, E_Function);
3204 end Analyze_Function_Instantiation;
3205
3206 ---------------------------------
3207 -- Analyze_Generic_Access_Type --
3208 ---------------------------------
3209
3210 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id) is
3211 begin
3212 Enter_Name (T);
3213
3214 if Nkind (Def) = N_Access_To_Object_Definition then
3215 Access_Type_Declaration (T, Def);
3216
3217 if Is_Incomplete_Or_Private_Type (Designated_Type (T))
3218 and then No (Full_View (Designated_Type (T)))
3219 and then not Is_Generic_Type (Designated_Type (T))
3220 then
3221 Error_Msg_N ("premature usage of incomplete type", Def);
3222
3223 elsif not Is_Entity_Name (Subtype_Indication (Def)) then
3224 Error_Msg_N
3225 ("only a subtype mark is allowed in a formal", Def);
3226 end if;
3227
3228 else
3229 Access_Subprogram_Declaration (T, Def);
3230 end if;
3231 end Analyze_Generic_Access_Type;
3232
3233 ---------------------------------
3234 -- Analyze_Generic_Formal_Part --
3235 ---------------------------------
3236
3237 procedure Analyze_Generic_Formal_Part (N : Node_Id) is
3238 Gen_Parm_Decl : Node_Id;
3239
3240 begin
3241 -- The generic formals are processed in the scope of the generic unit,
3242 -- where they are immediately visible. The scope is installed by the
3243 -- caller.
3244
3245 Gen_Parm_Decl := First (Generic_Formal_Declarations (N));
3246 while Present (Gen_Parm_Decl) loop
3247 Analyze (Gen_Parm_Decl);
3248 Next (Gen_Parm_Decl);
3249 end loop;
3250
3251 Generate_Reference_To_Generic_Formals (Current_Scope);
3252 end Analyze_Generic_Formal_Part;
3253
3254 ------------------------------------------
3255 -- Analyze_Generic_Package_Declaration --
3256 ------------------------------------------
3257
3258 procedure Analyze_Generic_Package_Declaration (N : Node_Id) is
3259 Loc : constant Source_Ptr := Sloc (N);
3260 Decls : constant List_Id :=
3261 Visible_Declarations (Specification (N));
3262 Decl : Node_Id;
3263 Id : Entity_Id;
3264 New_N : Node_Id;
3265 Renaming : Node_Id;
3266 Save_Parent : Node_Id;
3267
3268 begin
3269 Check_SPARK_05_Restriction ("generic is not allowed", N);
3270
3271 -- We introduce a renaming of the enclosing package, to have a usable
3272 -- entity as the prefix of an expanded name for a local entity of the
3273 -- form Par.P.Q, where P is the generic package. This is because a local
3274 -- entity named P may hide it, so that the usual visibility rules in
3275 -- the instance will not resolve properly.
3276
3277 Renaming :=
3278 Make_Package_Renaming_Declaration (Loc,
3279 Defining_Unit_Name =>
3280 Make_Defining_Identifier (Loc,
3281 Chars => New_External_Name (Chars (Defining_Entity (N)), "GH")),
3282 Name =>
3283 Make_Identifier (Loc, Chars (Defining_Entity (N))));
3284
3285 if Present (Decls) then
3286 Decl := First (Decls);
3287 while Present (Decl) and then Nkind (Decl) = N_Pragma loop
3288 Next (Decl);
3289 end loop;
3290
3291 if Present (Decl) then
3292 Insert_Before (Decl, Renaming);
3293 else
3294 Append (Renaming, Visible_Declarations (Specification (N)));
3295 end if;
3296
3297 else
3298 Set_Visible_Declarations (Specification (N), New_List (Renaming));
3299 end if;
3300
3301 -- Create copy of generic unit, and save for instantiation. If the unit
3302 -- is a child unit, do not copy the specifications for the parent, which
3303 -- are not part of the generic tree.
3304
3305 Save_Parent := Parent_Spec (N);
3306 Set_Parent_Spec (N, Empty);
3307
3308 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
3309 Set_Parent_Spec (New_N, Save_Parent);
3310 Rewrite (N, New_N);
3311
3312 -- Once the contents of the generic copy and the template are swapped,
3313 -- do the same for their respective aspect specifications.
3314
3315 Exchange_Aspects (N, New_N);
3316
3317 -- Collect all contract-related source pragmas found within the template
3318 -- and attach them to the contract of the package spec. This contract is
3319 -- used in the capture of global references within annotations.
3320
3321 Create_Generic_Contract (N);
3322
3323 Id := Defining_Entity (N);
3324 Generate_Definition (Id);
3325
3326 -- Expansion is not applied to generic units
3327
3328 Start_Generic;
3329
3330 Enter_Name (Id);
3331 Set_Ekind (Id, E_Generic_Package);
3332 Set_Etype (Id, Standard_Void_Type);
3333
3334 -- A generic package declared within a Ghost region is rendered Ghost
3335 -- (SPARK RM 6.9(2)).
3336
3337 if Ghost_Mode > None then
3338 Set_Is_Ghost_Entity (Id);
3339 end if;
3340
3341 -- Analyze aspects now, so that generated pragmas appear in the
3342 -- declarations before building and analyzing the generic copy.
3343
3344 if Has_Aspects (N) then
3345 Analyze_Aspect_Specifications (N, Id);
3346 end if;
3347
3348 Push_Scope (Id);
3349 Enter_Generic_Scope (Id);
3350 Set_Inner_Instances (Id, New_Elmt_List);
3351
3352 Set_Categorization_From_Pragmas (N);
3353 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3354
3355 -- Link the declaration of the generic homonym in the generic copy to
3356 -- the package it renames, so that it is always resolved properly.
3357
3358 Set_Generic_Homonym (Id, Defining_Unit_Name (Renaming));
3359 Set_Entity (Associated_Node (Name (Renaming)), Id);
3360
3361 -- For a library unit, we have reconstructed the entity for the unit,
3362 -- and must reset it in the library tables.
3363
3364 if Nkind (Parent (N)) = N_Compilation_Unit then
3365 Set_Cunit_Entity (Current_Sem_Unit, Id);
3366 end if;
3367
3368 Analyze_Generic_Formal_Part (N);
3369
3370 -- After processing the generic formals, analysis proceeds as for a
3371 -- non-generic package.
3372
3373 Analyze (Specification (N));
3374
3375 Validate_Categorization_Dependency (N, Id);
3376
3377 End_Generic;
3378
3379 End_Package_Scope (Id);
3380 Exit_Generic_Scope (Id);
3381
3382 if Nkind (Parent (N)) /= N_Compilation_Unit then
3383 Move_Freeze_Nodes (Id, N, Visible_Declarations (Specification (N)));
3384 Move_Freeze_Nodes (Id, N, Private_Declarations (Specification (N)));
3385 Move_Freeze_Nodes (Id, N, Generic_Formal_Declarations (N));
3386
3387 else
3388 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
3389 Validate_RT_RAT_Component (N);
3390
3391 -- If this is a spec without a body, check that generic parameters
3392 -- are referenced.
3393
3394 if not Body_Required (Parent (N)) then
3395 Check_References (Id);
3396 end if;
3397 end if;
3398
3399 -- If there is a specified storage pool in the context, create an
3400 -- aspect on the package declaration, so that it is used in any
3401 -- instance that does not override it.
3402
3403 if Present (Default_Pool) then
3404 declare
3405 ASN : Node_Id;
3406
3407 begin
3408 ASN :=
3409 Make_Aspect_Specification (Loc,
3410 Identifier => Make_Identifier (Loc, Name_Default_Storage_Pool),
3411 Expression => New_Copy (Default_Pool));
3412
3413 if No (Aspect_Specifications (Specification (N))) then
3414 Set_Aspect_Specifications (Specification (N), New_List (ASN));
3415 else
3416 Append (ASN, Aspect_Specifications (Specification (N)));
3417 end if;
3418 end;
3419 end if;
3420 end Analyze_Generic_Package_Declaration;
3421
3422 --------------------------------------------
3423 -- Analyze_Generic_Subprogram_Declaration --
3424 --------------------------------------------
3425
3426 procedure Analyze_Generic_Subprogram_Declaration (N : Node_Id) is
3427 Formals : List_Id;
3428 Id : Entity_Id;
3429 New_N : Node_Id;
3430 Result_Type : Entity_Id;
3431 Save_Parent : Node_Id;
3432 Spec : Node_Id;
3433 Typ : Entity_Id;
3434
3435 begin
3436 Check_SPARK_05_Restriction ("generic is not allowed", N);
3437
3438 -- Create copy of generic unit, and save for instantiation. If the unit
3439 -- is a child unit, do not copy the specifications for the parent, which
3440 -- are not part of the generic tree.
3441
3442 Save_Parent := Parent_Spec (N);
3443 Set_Parent_Spec (N, Empty);
3444
3445 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
3446 Set_Parent_Spec (New_N, Save_Parent);
3447 Rewrite (N, New_N);
3448
3449 -- Once the contents of the generic copy and the template are swapped,
3450 -- do the same for their respective aspect specifications.
3451
3452 Exchange_Aspects (N, New_N);
3453
3454 -- Collect all contract-related source pragmas found within the template
3455 -- and attach them to the contract of the subprogram spec. This contract
3456 -- is used in the capture of global references within annotations.
3457
3458 Create_Generic_Contract (N);
3459
3460 Spec := Specification (N);
3461 Id := Defining_Entity (Spec);
3462 Generate_Definition (Id);
3463
3464 if Nkind (Id) = N_Defining_Operator_Symbol then
3465 Error_Msg_N
3466 ("operator symbol not allowed for generic subprogram", Id);
3467 end if;
3468
3469 Start_Generic;
3470
3471 Enter_Name (Id);
3472 Set_Scope_Depth_Value (Id, Scope_Depth (Current_Scope) + 1);
3473
3474 -- Analyze the aspects of the generic copy to ensure that all generated
3475 -- pragmas (if any) perform their semantic effects.
3476
3477 if Has_Aspects (N) then
3478 Analyze_Aspect_Specifications (N, Id);
3479 end if;
3480
3481 Push_Scope (Id);
3482 Enter_Generic_Scope (Id);
3483 Set_Inner_Instances (Id, New_Elmt_List);
3484 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3485
3486 Analyze_Generic_Formal_Part (N);
3487
3488 Formals := Parameter_Specifications (Spec);
3489
3490 if Nkind (Spec) = N_Function_Specification then
3491 Set_Ekind (Id, E_Generic_Function);
3492 else
3493 Set_Ekind (Id, E_Generic_Procedure);
3494 end if;
3495
3496 if Present (Formals) then
3497 Process_Formals (Formals, Spec);
3498 end if;
3499
3500 if Nkind (Spec) = N_Function_Specification then
3501 if Nkind (Result_Definition (Spec)) = N_Access_Definition then
3502 Result_Type := Access_Definition (Spec, Result_Definition (Spec));
3503 Set_Etype (Id, Result_Type);
3504
3505 -- Check restriction imposed by AI05-073: a generic function
3506 -- cannot return an abstract type or an access to such.
3507
3508 -- This is a binding interpretation should it apply to earlier
3509 -- versions of Ada as well as Ada 2012???
3510
3511 if Is_Abstract_Type (Designated_Type (Result_Type))
3512 and then Ada_Version >= Ada_2012
3513 then
3514 Error_Msg_N
3515 ("generic function cannot have an access result "
3516 & "that designates an abstract type", Spec);
3517 end if;
3518
3519 else
3520 Find_Type (Result_Definition (Spec));
3521 Typ := Entity (Result_Definition (Spec));
3522
3523 if Is_Abstract_Type (Typ)
3524 and then Ada_Version >= Ada_2012
3525 then
3526 Error_Msg_N
3527 ("generic function cannot have abstract result type", Spec);
3528 end if;
3529
3530 -- If a null exclusion is imposed on the result type, then create
3531 -- a null-excluding itype (an access subtype) and use it as the
3532 -- function's Etype.
3533
3534 if Is_Access_Type (Typ)
3535 and then Null_Exclusion_Present (Spec)
3536 then
3537 Set_Etype (Id,
3538 Create_Null_Excluding_Itype
3539 (T => Typ,
3540 Related_Nod => Spec,
3541 Scope_Id => Defining_Unit_Name (Spec)));
3542 else
3543 Set_Etype (Id, Typ);
3544 end if;
3545 end if;
3546
3547 else
3548 Set_Etype (Id, Standard_Void_Type);
3549 end if;
3550
3551 -- A generic subprogram declared within a Ghost region is rendered Ghost
3552 -- (SPARK RM 6.9(2)).
3553
3554 if Ghost_Mode > None then
3555 Set_Is_Ghost_Entity (Id);
3556 end if;
3557
3558 -- For a library unit, we have reconstructed the entity for the unit,
3559 -- and must reset it in the library tables. We also make sure that
3560 -- Body_Required is set properly in the original compilation unit node.
3561
3562 if Nkind (Parent (N)) = N_Compilation_Unit then
3563 Set_Cunit_Entity (Current_Sem_Unit, Id);
3564 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
3565 end if;
3566
3567 Set_Categorization_From_Pragmas (N);
3568 Validate_Categorization_Dependency (N, Id);
3569
3570 -- Capture all global references that occur within the profile of the
3571 -- generic subprogram. Aspects are not part of this processing because
3572 -- they must be delayed. If processed now, Save_Global_References will
3573 -- destroy the Associated_Node links and prevent the capture of global
3574 -- references when the contract of the generic subprogram is analyzed.
3575
3576 Save_Global_References (Original_Node (N));
3577
3578 End_Generic;
3579 End_Scope;
3580 Exit_Generic_Scope (Id);
3581 Generate_Reference_To_Formals (Id);
3582
3583 List_Inherited_Pre_Post_Aspects (Id);
3584 end Analyze_Generic_Subprogram_Declaration;
3585
3586 -----------------------------------
3587 -- Analyze_Package_Instantiation --
3588 -----------------------------------
3589
3590 procedure Analyze_Package_Instantiation (N : Node_Id) is
3591 Loc : constant Source_Ptr := Sloc (N);
3592 Gen_Id : constant Node_Id := Name (N);
3593
3594 Act_Decl : Node_Id;
3595 Act_Decl_Name : Node_Id;
3596 Act_Decl_Id : Entity_Id;
3597 Act_Spec : Node_Id;
3598 Act_Tree : Node_Id;
3599
3600 Gen_Decl : Node_Id;
3601 Gen_Spec : Node_Id;
3602 Gen_Unit : Entity_Id;
3603
3604 Is_Actual_Pack : constant Boolean :=
3605 Is_Internal (Defining_Entity (N));
3606
3607 Env_Installed : Boolean := False;
3608 Parent_Installed : Boolean := False;
3609 Renaming_List : List_Id;
3610 Unit_Renaming : Node_Id;
3611 Needs_Body : Boolean;
3612 Inline_Now : Boolean := False;
3613 Has_Inline_Always : Boolean := False;
3614
3615 Save_IPSM : constant Boolean := Ignore_Pragma_SPARK_Mode;
3616 -- Save flag Ignore_Pragma_SPARK_Mode for restore on exit
3617
3618 Save_SM : constant SPARK_Mode_Type := SPARK_Mode;
3619 Save_SMP : constant Node_Id := SPARK_Mode_Pragma;
3620 -- Save the SPARK_Mode-related data for restore on exit
3621
3622 Save_Style_Check : constant Boolean := Style_Check;
3623 -- Save style check mode for restore on exit
3624
3625 procedure Delay_Descriptors (E : Entity_Id);
3626 -- Delay generation of subprogram descriptors for given entity
3627
3628 function Might_Inline_Subp return Boolean;
3629 -- If inlining is active and the generic contains inlined subprograms,
3630 -- we instantiate the body. This may cause superfluous instantiations,
3631 -- but it is simpler than detecting the need for the body at the point
3632 -- of inlining, when the context of the instance is not available.
3633
3634 -----------------------
3635 -- Delay_Descriptors --
3636 -----------------------
3637
3638 procedure Delay_Descriptors (E : Entity_Id) is
3639 begin
3640 if not Delay_Subprogram_Descriptors (E) then
3641 Set_Delay_Subprogram_Descriptors (E);
3642 Pending_Descriptor.Append (E);
3643 end if;
3644 end Delay_Descriptors;
3645
3646 -----------------------
3647 -- Might_Inline_Subp --
3648 -----------------------
3649
3650 function Might_Inline_Subp return Boolean is
3651 E : Entity_Id;
3652
3653 begin
3654 if not Inline_Processing_Required then
3655 return False;
3656
3657 else
3658 E := First_Entity (Gen_Unit);
3659 while Present (E) loop
3660 if Is_Subprogram (E) and then Is_Inlined (E) then
3661 -- Remember if there are any subprograms with Inline_Always
3662
3663 if Has_Pragma_Inline_Always (E) then
3664 Has_Inline_Always := True;
3665 end if;
3666
3667 return True;
3668 end if;
3669
3670 Next_Entity (E);
3671 end loop;
3672 end if;
3673
3674 return False;
3675 end Might_Inline_Subp;
3676
3677 -- Local declarations
3678
3679 Vis_Prims_List : Elist_Id := No_Elist;
3680 -- List of primitives made temporarily visible in the instantiation
3681 -- to match the visibility of the formal type
3682
3683 -- Start of processing for Analyze_Package_Instantiation
3684
3685 begin
3686 Check_SPARK_05_Restriction ("generic is not allowed", N);
3687
3688 -- Very first thing: check for Text_IO special unit in case we are
3689 -- instantiating one of the children of [[Wide_]Wide_]Text_IO.
3690
3691 Check_Text_IO_Special_Unit (Name (N));
3692
3693 -- Make node global for error reporting
3694
3695 Instantiation_Node := N;
3696
3697 -- Turn off style checking in instances. If the check is enabled on the
3698 -- generic unit, a warning in an instance would just be noise. If not
3699 -- enabled on the generic, then a warning in an instance is just wrong.
3700
3701 Style_Check := False;
3702
3703 -- Case of instantiation of a generic package
3704
3705 if Nkind (N) = N_Package_Instantiation then
3706 Act_Decl_Id := New_Copy (Defining_Entity (N));
3707 Set_Comes_From_Source (Act_Decl_Id, True);
3708
3709 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name then
3710 Act_Decl_Name :=
3711 Make_Defining_Program_Unit_Name (Loc,
3712 Name =>
3713 New_Copy_Tree (Name (Defining_Unit_Name (N))),
3714 Defining_Identifier => Act_Decl_Id);
3715 else
3716 Act_Decl_Name := Act_Decl_Id;
3717 end if;
3718
3719 -- Case of instantiation of a formal package
3720
3721 else
3722 Act_Decl_Id := Defining_Identifier (N);
3723 Act_Decl_Name := Act_Decl_Id;
3724 end if;
3725
3726 Generate_Definition (Act_Decl_Id);
3727 Set_Ekind (Act_Decl_Id, E_Package);
3728
3729 -- Initialize list of incomplete actuals before analysis
3730
3731 Set_Incomplete_Actuals (Act_Decl_Id, New_Elmt_List);
3732
3733 Preanalyze_Actuals (N, Act_Decl_Id);
3734
3735 Init_Env;
3736 Env_Installed := True;
3737
3738 -- Reset renaming map for formal types. The mapping is established
3739 -- when analyzing the generic associations, but some mappings are
3740 -- inherited from formal packages of parent units, and these are
3741 -- constructed when the parents are installed.
3742
3743 Generic_Renamings.Set_Last (0);
3744 Generic_Renamings_HTable.Reset;
3745
3746 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
3747 Gen_Unit := Entity (Gen_Id);
3748
3749 -- Verify that it is the name of a generic package
3750
3751 -- A visibility glitch: if the instance is a child unit and the generic
3752 -- is the generic unit of a parent instance (i.e. both the parent and
3753 -- the child units are instances of the same package) the name now
3754 -- denotes the renaming within the parent, not the intended generic
3755 -- unit. See if there is a homonym that is the desired generic. The
3756 -- renaming declaration must be visible inside the instance of the
3757 -- child, but not when analyzing the name in the instantiation itself.
3758
3759 if Ekind (Gen_Unit) = E_Package
3760 and then Present (Renamed_Entity (Gen_Unit))
3761 and then In_Open_Scopes (Renamed_Entity (Gen_Unit))
3762 and then Is_Generic_Instance (Renamed_Entity (Gen_Unit))
3763 and then Present (Homonym (Gen_Unit))
3764 then
3765 Gen_Unit := Homonym (Gen_Unit);
3766 end if;
3767
3768 if Etype (Gen_Unit) = Any_Type then
3769 Restore_Env;
3770 goto Leave;
3771
3772 elsif Ekind (Gen_Unit) /= E_Generic_Package then
3773
3774 -- Ada 2005 (AI-50217): Cannot use instance in limited with_clause
3775
3776 if From_Limited_With (Gen_Unit) then
3777 Error_Msg_N
3778 ("cannot instantiate a limited withed package", Gen_Id);
3779 else
3780 Error_Msg_NE
3781 ("& is not the name of a generic package", Gen_Id, Gen_Unit);
3782 end if;
3783
3784 Restore_Env;
3785 goto Leave;
3786 end if;
3787
3788 if In_Extended_Main_Source_Unit (N) then
3789 Set_Is_Instantiated (Gen_Unit);
3790 Generate_Reference (Gen_Unit, N);
3791
3792 if Present (Renamed_Object (Gen_Unit)) then
3793 Set_Is_Instantiated (Renamed_Object (Gen_Unit));
3794 Generate_Reference (Renamed_Object (Gen_Unit), N);
3795 end if;
3796 end if;
3797
3798 if Nkind (Gen_Id) = N_Identifier
3799 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
3800 then
3801 Error_Msg_NE
3802 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
3803
3804 elsif Nkind (Gen_Id) = N_Expanded_Name
3805 and then Is_Child_Unit (Gen_Unit)
3806 and then Nkind (Prefix (Gen_Id)) = N_Identifier
3807 and then Chars (Act_Decl_Id) = Chars (Prefix (Gen_Id))
3808 then
3809 Error_Msg_N
3810 ("& is hidden within declaration of instance ", Prefix (Gen_Id));
3811 end if;
3812
3813 Set_Entity (Gen_Id, Gen_Unit);
3814
3815 -- If generic is a renaming, get original generic unit
3816
3817 if Present (Renamed_Object (Gen_Unit))
3818 and then Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Package
3819 then
3820 Gen_Unit := Renamed_Object (Gen_Unit);
3821 end if;
3822
3823 -- Verify that there are no circular instantiations
3824
3825 if In_Open_Scopes (Gen_Unit) then
3826 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
3827 Restore_Env;
3828 goto Leave;
3829
3830 elsif Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
3831 Error_Msg_Node_2 := Current_Scope;
3832 Error_Msg_NE
3833 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
3834 Circularity_Detected := True;
3835 Restore_Env;
3836 goto Leave;
3837
3838 else
3839 -- If the context of the instance is subject to SPARK_Mode "off" or
3840 -- the annotation is altogether missing, set the global flag which
3841 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
3842 -- the instance.
3843
3844 if SPARK_Mode /= On then
3845 Ignore_Pragma_SPARK_Mode := True;
3846 end if;
3847
3848 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
3849 Gen_Spec := Specification (Gen_Decl);
3850
3851 -- Initialize renamings map, for error checking, and the list that
3852 -- holds private entities whose views have changed between generic
3853 -- definition and instantiation. If this is the instance created to
3854 -- validate an actual package, the instantiation environment is that
3855 -- of the enclosing instance.
3856
3857 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
3858
3859 -- Copy original generic tree, to produce text for instantiation
3860
3861 Act_Tree :=
3862 Copy_Generic_Node
3863 (Original_Node (Gen_Decl), Empty, Instantiating => True);
3864
3865 Act_Spec := Specification (Act_Tree);
3866
3867 -- If this is the instance created to validate an actual package,
3868 -- only the formals matter, do not examine the package spec itself.
3869
3870 if Is_Actual_Pack then
3871 Set_Visible_Declarations (Act_Spec, New_List);
3872 Set_Private_Declarations (Act_Spec, New_List);
3873 end if;
3874
3875 Renaming_List :=
3876 Analyze_Associations
3877 (I_Node => N,
3878 Formals => Generic_Formal_Declarations (Act_Tree),
3879 F_Copy => Generic_Formal_Declarations (Gen_Decl));
3880
3881 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
3882
3883 Set_Instance_Env (Gen_Unit, Act_Decl_Id);
3884 Set_Defining_Unit_Name (Act_Spec, Act_Decl_Name);
3885 Set_Is_Generic_Instance (Act_Decl_Id);
3886 Set_Generic_Parent (Act_Spec, Gen_Unit);
3887
3888 -- References to the generic in its own declaration or its body are
3889 -- references to the instance. Add a renaming declaration for the
3890 -- generic unit itself. This declaration, as well as the renaming
3891 -- declarations for the generic formals, must remain private to the
3892 -- unit: the formals, because this is the language semantics, and
3893 -- the unit because its use is an artifact of the implementation.
3894
3895 Unit_Renaming :=
3896 Make_Package_Renaming_Declaration (Loc,
3897 Defining_Unit_Name =>
3898 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
3899 Name => New_Occurrence_Of (Act_Decl_Id, Loc));
3900
3901 Append (Unit_Renaming, Renaming_List);
3902
3903 -- The renaming declarations are the first local declarations of the
3904 -- new unit.
3905
3906 if Is_Non_Empty_List (Visible_Declarations (Act_Spec)) then
3907 Insert_List_Before
3908 (First (Visible_Declarations (Act_Spec)), Renaming_List);
3909 else
3910 Set_Visible_Declarations (Act_Spec, Renaming_List);
3911 end if;
3912
3913 Act_Decl := Make_Package_Declaration (Loc, Specification => Act_Spec);
3914
3915 -- Propagate the aspect specifications from the package declaration
3916 -- template to the instantiated version of the package declaration.
3917
3918 if Has_Aspects (Act_Tree) then
3919 Set_Aspect_Specifications (Act_Decl,
3920 New_Copy_List_Tree (Aspect_Specifications (Act_Tree)));
3921 end if;
3922
3923 -- The generic may have a generated Default_Storage_Pool aspect,
3924 -- set at the point of generic declaration. If the instance has
3925 -- that aspect, it overrides the one inherited from the generic.
3926
3927 if Has_Aspects (Gen_Spec) then
3928 if No (Aspect_Specifications (N)) then
3929 Set_Aspect_Specifications (N,
3930 (New_Copy_List_Tree
3931 (Aspect_Specifications (Gen_Spec))));
3932
3933 else
3934 declare
3935 ASN1, ASN2 : Node_Id;
3936
3937 begin
3938 ASN1 := First (Aspect_Specifications (N));
3939 while Present (ASN1) loop
3940 if Chars (Identifier (ASN1)) = Name_Default_Storage_Pool
3941 then
3942 -- If generic carries a default storage pool, remove
3943 -- it in favor of the instance one.
3944
3945 ASN2 := First (Aspect_Specifications (Gen_Spec));
3946 while Present (ASN2) loop
3947 if Chars (Identifier (ASN2)) =
3948 Name_Default_Storage_Pool
3949 then
3950 Remove (ASN2);
3951 exit;
3952 end if;
3953
3954 Next (ASN2);
3955 end loop;
3956 end if;
3957
3958 Next (ASN1);
3959 end loop;
3960
3961 Prepend_List_To (Aspect_Specifications (N),
3962 (New_Copy_List_Tree
3963 (Aspect_Specifications (Gen_Spec))));
3964 end;
3965 end if;
3966 end if;
3967
3968 -- Save the instantiation node, for subsequent instantiation of the
3969 -- body, if there is one and we are generating code for the current
3970 -- unit. Mark unit as having a body (avoids premature error message).
3971
3972 -- We instantiate the body if we are generating code, if we are
3973 -- generating cross-reference information, or if we are building
3974 -- trees for ASIS use or GNATprove use.
3975
3976 declare
3977 Enclosing_Body_Present : Boolean := False;
3978 -- If the generic unit is not a compilation unit, then a body may
3979 -- be present in its parent even if none is required. We create a
3980 -- tentative pending instantiation for the body, which will be
3981 -- discarded if none is actually present.
3982
3983 Scop : Entity_Id;
3984
3985 begin
3986 if Scope (Gen_Unit) /= Standard_Standard
3987 and then not Is_Child_Unit (Gen_Unit)
3988 then
3989 Scop := Scope (Gen_Unit);
3990 while Present (Scop) and then Scop /= Standard_Standard loop
3991 if Unit_Requires_Body (Scop) then
3992 Enclosing_Body_Present := True;
3993 exit;
3994
3995 elsif In_Open_Scopes (Scop)
3996 and then In_Package_Body (Scop)
3997 then
3998 Enclosing_Body_Present := True;
3999 exit;
4000 end if;
4001
4002 exit when Is_Compilation_Unit (Scop);
4003 Scop := Scope (Scop);
4004 end loop;
4005 end if;
4006
4007 -- If front-end inlining is enabled or there are any subprograms
4008 -- marked with Inline_Always, and this is a unit for which code
4009 -- will be generated, we instantiate the body at once.
4010
4011 -- This is done if the instance is not the main unit, and if the
4012 -- generic is not a child unit of another generic, to avoid scope
4013 -- problems and the reinstallation of parent instances.
4014
4015 if Expander_Active
4016 and then (not Is_Child_Unit (Gen_Unit)
4017 or else not Is_Generic_Unit (Scope (Gen_Unit)))
4018 and then Might_Inline_Subp
4019 and then not Is_Actual_Pack
4020 then
4021 if not Back_End_Inlining
4022 and then (Front_End_Inlining or else Has_Inline_Always)
4023 and then (Is_In_Main_Unit (N)
4024 or else In_Main_Context (Current_Scope))
4025 and then Nkind (Parent (N)) /= N_Compilation_Unit
4026 then
4027 Inline_Now := True;
4028
4029 -- In configurable_run_time mode we force the inlining of
4030 -- predefined subprograms marked Inline_Always, to minimize
4031 -- the use of the run-time library.
4032
4033 elsif Is_Predefined_File_Name
4034 (Unit_File_Name (Get_Source_Unit (Gen_Decl)))
4035 and then Configurable_Run_Time_Mode
4036 and then Nkind (Parent (N)) /= N_Compilation_Unit
4037 then
4038 Inline_Now := True;
4039 end if;
4040
4041 -- If the current scope is itself an instance within a child
4042 -- unit, there will be duplications in the scope stack, and the
4043 -- unstacking mechanism in Inline_Instance_Body will fail.
4044 -- This loses some rare cases of optimization, and might be
4045 -- improved some day, if we can find a proper abstraction for
4046 -- "the complete compilation context" that can be saved and
4047 -- restored. ???
4048
4049 if Is_Generic_Instance (Current_Scope) then
4050 declare
4051 Curr_Unit : constant Entity_Id :=
4052 Cunit_Entity (Current_Sem_Unit);
4053 begin
4054 if Curr_Unit /= Current_Scope
4055 and then Is_Child_Unit (Curr_Unit)
4056 then
4057 Inline_Now := False;
4058 end if;
4059 end;
4060 end if;
4061 end if;
4062
4063 Needs_Body :=
4064 (Unit_Requires_Body (Gen_Unit)
4065 or else Enclosing_Body_Present
4066 or else Present (Corresponding_Body (Gen_Decl)))
4067 and then (Is_In_Main_Unit (N) or else Might_Inline_Subp)
4068 and then not Is_Actual_Pack
4069 and then not Inline_Now
4070 and then (Operating_Mode = Generate_Code
4071
4072 -- Need comment for this check ???
4073
4074 or else (Operating_Mode = Check_Semantics
4075 and then (ASIS_Mode or GNATprove_Mode)));
4076
4077 -- If front-end inlining is enabled or there are any subprograms
4078 -- marked with Inline_Always, do not instantiate body when within
4079 -- a generic context.
4080
4081 if ((Front_End_Inlining or else Has_Inline_Always)
4082 and then not Expander_Active)
4083 or else Is_Generic_Unit (Cunit_Entity (Main_Unit))
4084 then
4085 Needs_Body := False;
4086 end if;
4087
4088 -- If the current context is generic, and the package being
4089 -- instantiated is declared within a formal package, there is no
4090 -- body to instantiate until the enclosing generic is instantiated
4091 -- and there is an actual for the formal package. If the formal
4092 -- package has parameters, we build a regular package instance for
4093 -- it, that precedes the original formal package declaration.
4094
4095 if In_Open_Scopes (Scope (Scope (Gen_Unit))) then
4096 declare
4097 Decl : constant Node_Id :=
4098 Original_Node
4099 (Unit_Declaration_Node (Scope (Gen_Unit)));
4100 begin
4101 if Nkind (Decl) = N_Formal_Package_Declaration
4102 or else (Nkind (Decl) = N_Package_Declaration
4103 and then Is_List_Member (Decl)
4104 and then Present (Next (Decl))
4105 and then
4106 Nkind (Next (Decl)) =
4107 N_Formal_Package_Declaration)
4108 then
4109 Needs_Body := False;
4110 end if;
4111 end;
4112 end if;
4113 end;
4114
4115 -- For RCI unit calling stubs, we omit the instance body if the
4116 -- instance is the RCI library unit itself.
4117
4118 -- However there is a special case for nested instances: in this case
4119 -- we do generate the instance body, as it might be required, e.g.
4120 -- because it provides stream attributes for some type used in the
4121 -- profile of a remote subprogram. This is consistent with 12.3(12),
4122 -- which indicates that the instance body occurs at the place of the
4123 -- instantiation, and thus is part of the RCI declaration, which is
4124 -- present on all client partitions (this is E.2.3(18)).
4125
4126 -- Note that AI12-0002 may make it illegal at some point to have
4127 -- stream attributes defined in an RCI unit, in which case this
4128 -- special case will become unnecessary. In the meantime, there
4129 -- is known application code in production that depends on this
4130 -- being possible, so we definitely cannot eliminate the body in
4131 -- the case of nested instances for the time being.
4132
4133 -- When we generate a nested instance body, calling stubs for any
4134 -- relevant subprogram will be be inserted immediately after the
4135 -- subprogram declarations, and will take precedence over the
4136 -- subsequent (original) body. (The stub and original body will be
4137 -- complete homographs, but this is permitted in an instance).
4138 -- (Could we do better and remove the original body???)
4139
4140 if Distribution_Stub_Mode = Generate_Caller_Stub_Body
4141 and then Comes_From_Source (N)
4142 and then Nkind (Parent (N)) = N_Compilation_Unit
4143 then
4144 Needs_Body := False;
4145 end if;
4146
4147 if Needs_Body then
4148
4149 -- Here is a defence against a ludicrous number of instantiations
4150 -- caused by a circular set of instantiation attempts.
4151
4152 if Pending_Instantiations.Last > Maximum_Instantiations then
4153 Error_Msg_Uint_1 := UI_From_Int (Maximum_Instantiations);
4154 Error_Msg_N ("too many instantiations, exceeds max of^", N);
4155 Error_Msg_N ("\limit can be changed using -gnateinn switch", N);
4156 raise Unrecoverable_Error;
4157 end if;
4158
4159 -- Indicate that the enclosing scopes contain an instantiation,
4160 -- and that cleanup actions should be delayed until after the
4161 -- instance body is expanded.
4162
4163 Check_Forward_Instantiation (Gen_Decl);
4164 if Nkind (N) = N_Package_Instantiation then
4165 declare
4166 Enclosing_Master : Entity_Id;
4167
4168 begin
4169 -- Loop to search enclosing masters
4170
4171 Enclosing_Master := Current_Scope;
4172 Scope_Loop : while Enclosing_Master /= Standard_Standard loop
4173 if Ekind (Enclosing_Master) = E_Package then
4174 if Is_Compilation_Unit (Enclosing_Master) then
4175 if In_Package_Body (Enclosing_Master) then
4176 Delay_Descriptors
4177 (Body_Entity (Enclosing_Master));
4178 else
4179 Delay_Descriptors
4180 (Enclosing_Master);
4181 end if;
4182
4183 exit Scope_Loop;
4184
4185 else
4186 Enclosing_Master := Scope (Enclosing_Master);
4187 end if;
4188
4189 elsif Is_Generic_Unit (Enclosing_Master)
4190 or else Ekind (Enclosing_Master) = E_Void
4191 then
4192 -- Cleanup actions will eventually be performed on the
4193 -- enclosing subprogram or package instance, if any.
4194 -- Enclosing scope is void in the formal part of a
4195 -- generic subprogram.
4196
4197 exit Scope_Loop;
4198
4199 else
4200 if Ekind (Enclosing_Master) = E_Entry
4201 and then
4202 Ekind (Scope (Enclosing_Master)) = E_Protected_Type
4203 then
4204 if not Expander_Active then
4205 exit Scope_Loop;
4206 else
4207 Enclosing_Master :=
4208 Protected_Body_Subprogram (Enclosing_Master);
4209 end if;
4210 end if;
4211
4212 Set_Delay_Cleanups (Enclosing_Master);
4213
4214 while Ekind (Enclosing_Master) = E_Block loop
4215 Enclosing_Master := Scope (Enclosing_Master);
4216 end loop;
4217
4218 if Is_Subprogram (Enclosing_Master) then
4219 Delay_Descriptors (Enclosing_Master);
4220
4221 elsif Is_Task_Type (Enclosing_Master) then
4222 declare
4223 TBP : constant Node_Id :=
4224 Get_Task_Body_Procedure
4225 (Enclosing_Master);
4226 begin
4227 if Present (TBP) then
4228 Delay_Descriptors (TBP);
4229 Set_Delay_Cleanups (TBP);
4230 end if;
4231 end;
4232 end if;
4233
4234 exit Scope_Loop;
4235 end if;
4236 end loop Scope_Loop;
4237 end;
4238
4239 -- Make entry in table
4240
4241 Add_Pending_Instantiation (N, Act_Decl);
4242 end if;
4243 end if;
4244
4245 Set_Categorization_From_Pragmas (Act_Decl);
4246
4247 if Parent_Installed then
4248 Hide_Current_Scope;
4249 end if;
4250
4251 Set_Instance_Spec (N, Act_Decl);
4252
4253 -- If not a compilation unit, insert the package declaration before
4254 -- the original instantiation node.
4255
4256 if Nkind (Parent (N)) /= N_Compilation_Unit then
4257 Mark_Rewrite_Insertion (Act_Decl);
4258 Insert_Before (N, Act_Decl);
4259
4260 if Has_Aspects (N) then
4261 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4262
4263 -- The pragma created for a Default_Storage_Pool aspect must
4264 -- appear ahead of the declarations in the instance spec.
4265 -- Analysis has placed it after the instance node, so remove
4266 -- it and reinsert it properly now.
4267
4268 declare
4269 ASN : constant Node_Id := First (Aspect_Specifications (N));
4270 A_Name : constant Name_Id := Chars (Identifier (ASN));
4271 Decl : Node_Id;
4272
4273 begin
4274 if A_Name = Name_Default_Storage_Pool then
4275 if No (Visible_Declarations (Act_Spec)) then
4276 Set_Visible_Declarations (Act_Spec, New_List);
4277 end if;
4278
4279 Decl := Next (N);
4280 while Present (Decl) loop
4281 if Nkind (Decl) = N_Pragma then
4282 Remove (Decl);
4283 Prepend (Decl, Visible_Declarations (Act_Spec));
4284 exit;
4285 end if;
4286
4287 Next (Decl);
4288 end loop;
4289 end if;
4290 end;
4291 end if;
4292
4293 Analyze (Act_Decl);
4294
4295 -- For an instantiation that is a compilation unit, place
4296 -- declaration on current node so context is complete for analysis
4297 -- (including nested instantiations). If this is the main unit,
4298 -- the declaration eventually replaces the instantiation node.
4299 -- If the instance body is created later, it replaces the
4300 -- instance node, and the declaration is attached to it
4301 -- (see Build_Instance_Compilation_Unit_Nodes).
4302
4303 else
4304 if Cunit_Entity (Current_Sem_Unit) = Defining_Entity (N) then
4305
4306 -- The entity for the current unit is the newly created one,
4307 -- and all semantic information is attached to it.
4308
4309 Set_Cunit_Entity (Current_Sem_Unit, Act_Decl_Id);
4310
4311 -- If this is the main unit, replace the main entity as well
4312
4313 if Current_Sem_Unit = Main_Unit then
4314 Main_Unit_Entity := Act_Decl_Id;
4315 end if;
4316 end if;
4317
4318 Set_Unit (Parent (N), Act_Decl);
4319 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
4320 Set_Package_Instantiation (Act_Decl_Id, N);
4321
4322 -- Process aspect specifications of the instance node, if any, to
4323 -- take into account categorization pragmas before analyzing the
4324 -- instance.
4325
4326 if Has_Aspects (N) then
4327 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4328 end if;
4329
4330 Analyze (Act_Decl);
4331 Set_Unit (Parent (N), N);
4332 Set_Body_Required (Parent (N), False);
4333
4334 -- We never need elaboration checks on instantiations, since by
4335 -- definition, the body instantiation is elaborated at the same
4336 -- time as the spec instantiation.
4337
4338 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
4339 Set_Kill_Elaboration_Checks (Act_Decl_Id);
4340 end if;
4341
4342 Check_Elab_Instantiation (N);
4343
4344 if ABE_Is_Certain (N) and then Needs_Body then
4345 Pending_Instantiations.Decrement_Last;
4346 end if;
4347
4348 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
4349
4350 Set_First_Private_Entity (Defining_Unit_Name (Unit_Renaming),
4351 First_Private_Entity (Act_Decl_Id));
4352
4353 -- If the instantiation will receive a body, the unit will be
4354 -- transformed into a package body, and receive its own elaboration
4355 -- entity. Otherwise, the nature of the unit is now a package
4356 -- declaration.
4357
4358 if Nkind (Parent (N)) = N_Compilation_Unit
4359 and then not Needs_Body
4360 then
4361 Rewrite (N, Act_Decl);
4362 end if;
4363
4364 if Present (Corresponding_Body (Gen_Decl))
4365 or else Unit_Requires_Body (Gen_Unit)
4366 then
4367 Set_Has_Completion (Act_Decl_Id);
4368 end if;
4369
4370 Check_Formal_Packages (Act_Decl_Id);
4371
4372 Restore_Hidden_Primitives (Vis_Prims_List);
4373 Restore_Private_Views (Act_Decl_Id);
4374
4375 Inherit_Context (Gen_Decl, N);
4376
4377 if Parent_Installed then
4378 Remove_Parent;
4379 end if;
4380
4381 Restore_Env;
4382 Env_Installed := False;
4383 end if;
4384
4385 Validate_Categorization_Dependency (N, Act_Decl_Id);
4386
4387 -- There used to be a check here to prevent instantiations in local
4388 -- contexts if the No_Local_Allocators restriction was active. This
4389 -- check was removed by a binding interpretation in AI-95-00130/07,
4390 -- but we retain the code for documentation purposes.
4391
4392 -- if Ekind (Act_Decl_Id) /= E_Void
4393 -- and then not Is_Library_Level_Entity (Act_Decl_Id)
4394 -- then
4395 -- Check_Restriction (No_Local_Allocators, N);
4396 -- end if;
4397
4398 if Inline_Now then
4399 Inline_Instance_Body (N, Gen_Unit, Act_Decl);
4400 end if;
4401
4402 -- The following is a tree patch for ASIS: ASIS needs separate nodes to
4403 -- be used as defining identifiers for a formal package and for the
4404 -- corresponding expanded package.
4405
4406 if Nkind (N) = N_Formal_Package_Declaration then
4407 Act_Decl_Id := New_Copy (Defining_Entity (N));
4408 Set_Comes_From_Source (Act_Decl_Id, True);
4409 Set_Is_Generic_Instance (Act_Decl_Id, False);
4410 Set_Defining_Identifier (N, Act_Decl_Id);
4411 end if;
4412
4413 Ignore_Pragma_SPARK_Mode := Save_IPSM;
4414 SPARK_Mode := Save_SM;
4415 SPARK_Mode_Pragma := Save_SMP;
4416 Style_Check := Save_Style_Check;
4417
4418 -- Check that if N is an instantiation of System.Dim_Float_IO or
4419 -- System.Dim_Integer_IO, the formal type has a dimension system.
4420
4421 if Nkind (N) = N_Package_Instantiation
4422 and then Is_Dim_IO_Package_Instantiation (N)
4423 then
4424 declare
4425 Assoc : constant Node_Id := First (Generic_Associations (N));
4426 begin
4427 if not Has_Dimension_System
4428 (Etype (Explicit_Generic_Actual_Parameter (Assoc)))
4429 then
4430 Error_Msg_N ("type with a dimension system expected", Assoc);
4431 end if;
4432 end;
4433 end if;
4434
4435 <<Leave>>
4436 if Has_Aspects (N) and then Nkind (Parent (N)) /= N_Compilation_Unit then
4437 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4438 end if;
4439
4440 exception
4441 when Instantiation_Error =>
4442 if Parent_Installed then
4443 Remove_Parent;
4444 end if;
4445
4446 if Env_Installed then
4447 Restore_Env;
4448 end if;
4449
4450 Ignore_Pragma_SPARK_Mode := Save_IPSM;
4451 SPARK_Mode := Save_SM;
4452 SPARK_Mode_Pragma := Save_SMP;
4453 Style_Check := Save_Style_Check;
4454 end Analyze_Package_Instantiation;
4455
4456 --------------------------
4457 -- Inline_Instance_Body --
4458 --------------------------
4459
4460 procedure Inline_Instance_Body
4461 (N : Node_Id;
4462 Gen_Unit : Entity_Id;
4463 Act_Decl : Node_Id)
4464 is
4465 Curr_Comp : constant Node_Id := Cunit (Current_Sem_Unit);
4466 Curr_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
4467 Gen_Comp : constant Entity_Id :=
4468 Cunit_Entity (Get_Source_Unit (Gen_Unit));
4469
4470 Save_SM : constant SPARK_Mode_Type := SPARK_Mode;
4471 Save_SMP : constant Node_Id := SPARK_Mode_Pragma;
4472 -- Save all SPARK_Mode-related attributes as removing enclosing scopes
4473 -- to provide a clean environment for analysis of the inlined body will
4474 -- eliminate any previously set SPARK_Mode.
4475
4476 Scope_Stack_Depth : constant Pos :=
4477 Scope_Stack.Last - Scope_Stack.First + 1;
4478
4479 Use_Clauses : array (1 .. Scope_Stack_Depth) of Node_Id;
4480 Instances : array (1 .. Scope_Stack_Depth) of Entity_Id;
4481 Inner_Scopes : array (1 .. Scope_Stack_Depth) of Entity_Id;
4482 Curr_Scope : Entity_Id := Empty;
4483 List : Elist_Id;
4484 Num_Inner : Nat := 0;
4485 Num_Scopes : Nat := 0;
4486 N_Instances : Nat := 0;
4487 Removed : Boolean := False;
4488 S : Entity_Id;
4489 Vis : Boolean;
4490
4491 begin
4492 -- Case of generic unit defined in another unit. We must remove the
4493 -- complete context of the current unit to install that of the generic.
4494
4495 if Gen_Comp /= Cunit_Entity (Current_Sem_Unit) then
4496
4497 -- Add some comments for the following two loops ???
4498
4499 S := Current_Scope;
4500 while Present (S) and then S /= Standard_Standard loop
4501 loop
4502 Num_Scopes := Num_Scopes + 1;
4503
4504 Use_Clauses (Num_Scopes) :=
4505 (Scope_Stack.Table
4506 (Scope_Stack.Last - Num_Scopes + 1).
4507 First_Use_Clause);
4508 End_Use_Clauses (Use_Clauses (Num_Scopes));
4509
4510 exit when Scope_Stack.Last - Num_Scopes + 1 = Scope_Stack.First
4511 or else Scope_Stack.Table
4512 (Scope_Stack.Last - Num_Scopes).Entity = Scope (S);
4513 end loop;
4514
4515 exit when Is_Generic_Instance (S)
4516 and then (In_Package_Body (S)
4517 or else Ekind (S) = E_Procedure
4518 or else Ekind (S) = E_Function);
4519 S := Scope (S);
4520 end loop;
4521
4522 Vis := Is_Immediately_Visible (Gen_Comp);
4523
4524 -- Find and save all enclosing instances
4525
4526 S := Current_Scope;
4527
4528 while Present (S)
4529 and then S /= Standard_Standard
4530 loop
4531 if Is_Generic_Instance (S) then
4532 N_Instances := N_Instances + 1;
4533 Instances (N_Instances) := S;
4534
4535 exit when In_Package_Body (S);
4536 end if;
4537
4538 S := Scope (S);
4539 end loop;
4540
4541 -- Remove context of current compilation unit, unless we are within a
4542 -- nested package instantiation, in which case the context has been
4543 -- removed previously.
4544
4545 -- If current scope is the body of a child unit, remove context of
4546 -- spec as well. If an enclosing scope is an instance body, the
4547 -- context has already been removed, but the entities in the body
4548 -- must be made invisible as well.
4549
4550 S := Current_Scope;
4551 while Present (S) and then S /= Standard_Standard loop
4552 if Is_Generic_Instance (S)
4553 and then (In_Package_Body (S)
4554 or else Ekind_In (S, E_Procedure, E_Function))
4555 then
4556 -- We still have to remove the entities of the enclosing
4557 -- instance from direct visibility.
4558
4559 declare
4560 E : Entity_Id;
4561 begin
4562 E := First_Entity (S);
4563 while Present (E) loop
4564 Set_Is_Immediately_Visible (E, False);
4565 Next_Entity (E);
4566 end loop;
4567 end;
4568
4569 exit;
4570 end if;
4571
4572 if S = Curr_Unit
4573 or else (Ekind (Curr_Unit) = E_Package_Body
4574 and then S = Spec_Entity (Curr_Unit))
4575 or else (Ekind (Curr_Unit) = E_Subprogram_Body
4576 and then S = Corresponding_Spec
4577 (Unit_Declaration_Node (Curr_Unit)))
4578 then
4579 Removed := True;
4580
4581 -- Remove entities in current scopes from visibility, so that
4582 -- instance body is compiled in a clean environment.
4583
4584 List := Save_Scope_Stack (Handle_Use => False);
4585
4586 if Is_Child_Unit (S) then
4587
4588 -- Remove child unit from stack, as well as inner scopes.
4589 -- Removing the context of a child unit removes parent units
4590 -- as well.
4591
4592 while Current_Scope /= S loop
4593 Num_Inner := Num_Inner + 1;
4594 Inner_Scopes (Num_Inner) := Current_Scope;
4595 Pop_Scope;
4596 end loop;
4597
4598 Pop_Scope;
4599 Remove_Context (Curr_Comp);
4600 Curr_Scope := S;
4601
4602 else
4603 Remove_Context (Curr_Comp);
4604 end if;
4605
4606 if Ekind (Curr_Unit) = E_Package_Body then
4607 Remove_Context (Library_Unit (Curr_Comp));
4608 end if;
4609 end if;
4610
4611 S := Scope (S);
4612 end loop;
4613
4614 pragma Assert (Num_Inner < Num_Scopes);
4615
4616 -- The inlined package body must be analyzed with the SPARK_Mode of
4617 -- the enclosing context, otherwise the body may cause bogus errors
4618 -- if a configuration SPARK_Mode pragma in in effect.
4619
4620 Push_Scope (Standard_Standard);
4621 Scope_Stack.Table (Scope_Stack.Last).Is_Active_Stack_Base := True;
4622 Instantiate_Package_Body
4623 (Body_Info =>
4624 ((Inst_Node => N,
4625 Act_Decl => Act_Decl,
4626 Expander_Status => Expander_Active,
4627 Current_Sem_Unit => Current_Sem_Unit,
4628 Scope_Suppress => Scope_Suppress,
4629 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
4630 Version => Ada_Version,
4631 Version_Pragma => Ada_Version_Pragma,
4632 Warnings => Save_Warnings,
4633 SPARK_Mode => Save_SM,
4634 SPARK_Mode_Pragma => Save_SMP)),
4635 Inlined_Body => True);
4636
4637 Pop_Scope;
4638
4639 -- Restore context
4640
4641 Set_Is_Immediately_Visible (Gen_Comp, Vis);
4642
4643 -- Reset Generic_Instance flag so that use clauses can be installed
4644 -- in the proper order. (See Use_One_Package for effect of enclosing
4645 -- instances on processing of use clauses).
4646
4647 for J in 1 .. N_Instances loop
4648 Set_Is_Generic_Instance (Instances (J), False);
4649 end loop;
4650
4651 if Removed then
4652 Install_Context (Curr_Comp);
4653
4654 if Present (Curr_Scope)
4655 and then Is_Child_Unit (Curr_Scope)
4656 then
4657 Push_Scope (Curr_Scope);
4658 Set_Is_Immediately_Visible (Curr_Scope);
4659
4660 -- Finally, restore inner scopes as well
4661
4662 for J in reverse 1 .. Num_Inner loop
4663 Push_Scope (Inner_Scopes (J));
4664 end loop;
4665 end if;
4666
4667 Restore_Scope_Stack (List, Handle_Use => False);
4668
4669 if Present (Curr_Scope)
4670 and then
4671 (In_Private_Part (Curr_Scope)
4672 or else In_Package_Body (Curr_Scope))
4673 then
4674 -- Install private declaration of ancestor units, which are
4675 -- currently available. Restore_Scope_Stack and Install_Context
4676 -- only install the visible part of parents.
4677
4678 declare
4679 Par : Entity_Id;
4680 begin
4681 Par := Scope (Curr_Scope);
4682 while (Present (Par)) and then Par /= Standard_Standard loop
4683 Install_Private_Declarations (Par);
4684 Par := Scope (Par);
4685 end loop;
4686 end;
4687 end if;
4688 end if;
4689
4690 -- Restore use clauses. For a child unit, use clauses in the parents
4691 -- are restored when installing the context, so only those in inner
4692 -- scopes (and those local to the child unit itself) need to be
4693 -- installed explicitly.
4694
4695 if Is_Child_Unit (Curr_Unit) and then Removed then
4696 for J in reverse 1 .. Num_Inner + 1 loop
4697 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
4698 Use_Clauses (J);
4699 Install_Use_Clauses (Use_Clauses (J));
4700 end loop;
4701
4702 else
4703 for J in reverse 1 .. Num_Scopes loop
4704 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
4705 Use_Clauses (J);
4706 Install_Use_Clauses (Use_Clauses (J));
4707 end loop;
4708 end if;
4709
4710 -- Restore status of instances. If one of them is a body, make its
4711 -- local entities visible again.
4712
4713 declare
4714 E : Entity_Id;
4715 Inst : Entity_Id;
4716
4717 begin
4718 for J in 1 .. N_Instances loop
4719 Inst := Instances (J);
4720 Set_Is_Generic_Instance (Inst, True);
4721
4722 if In_Package_Body (Inst)
4723 or else Ekind_In (S, E_Procedure, E_Function)
4724 then
4725 E := First_Entity (Instances (J));
4726 while Present (E) loop
4727 Set_Is_Immediately_Visible (E);
4728 Next_Entity (E);
4729 end loop;
4730 end if;
4731 end loop;
4732 end;
4733
4734 -- If generic unit is in current unit, current context is correct. Note
4735 -- that the context is guaranteed to carry the correct SPARK_Mode as no
4736 -- enclosing scopes were removed.
4737
4738 else
4739 Instantiate_Package_Body
4740 (Body_Info =>
4741 ((Inst_Node => N,
4742 Act_Decl => Act_Decl,
4743 Expander_Status => Expander_Active,
4744 Current_Sem_Unit => Current_Sem_Unit,
4745 Scope_Suppress => Scope_Suppress,
4746 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
4747 Version => Ada_Version,
4748 Version_Pragma => Ada_Version_Pragma,
4749 Warnings => Save_Warnings,
4750 SPARK_Mode => SPARK_Mode,
4751 SPARK_Mode_Pragma => SPARK_Mode_Pragma)),
4752 Inlined_Body => True);
4753 end if;
4754 end Inline_Instance_Body;
4755
4756 -------------------------------------
4757 -- Analyze_Procedure_Instantiation --
4758 -------------------------------------
4759
4760 procedure Analyze_Procedure_Instantiation (N : Node_Id) is
4761 begin
4762 Analyze_Subprogram_Instantiation (N, E_Procedure);
4763 end Analyze_Procedure_Instantiation;
4764
4765 -----------------------------------
4766 -- Need_Subprogram_Instance_Body --
4767 -----------------------------------
4768
4769 function Need_Subprogram_Instance_Body
4770 (N : Node_Id;
4771 Subp : Entity_Id) return Boolean
4772 is
4773
4774 function Is_Inlined_Or_Child_Of_Inlined (E : Entity_Id) return Boolean;
4775 -- Return True if E is an inlined subprogram, an inlined renaming or a
4776 -- subprogram nested in an inlined subprogram. The inlining machinery
4777 -- totally disregards nested subprograms since it considers that they
4778 -- will always be compiled if the parent is (see Inline.Is_Nested).
4779
4780 ------------------------------------
4781 -- Is_Inlined_Or_Child_Of_Inlined --
4782 ------------------------------------
4783
4784 function Is_Inlined_Or_Child_Of_Inlined (E : Entity_Id) return Boolean is
4785 Scop : Entity_Id;
4786
4787 begin
4788 if Is_Inlined (E) or else Is_Inlined (Alias (E)) then
4789 return True;
4790 end if;
4791
4792 Scop := Scope (E);
4793 while Scop /= Standard_Standard loop
4794 if Ekind (Scop) in Subprogram_Kind and then Is_Inlined (Scop) then
4795 return True;
4796 end if;
4797
4798 Scop := Scope (Scop);
4799 end loop;
4800
4801 return False;
4802 end Is_Inlined_Or_Child_Of_Inlined;
4803
4804 begin
4805 -- Must be in the main unit or inlined (or child of inlined)
4806
4807 if (Is_In_Main_Unit (N) or else Is_Inlined_Or_Child_Of_Inlined (Subp))
4808
4809 -- Must be generating code or analyzing code in ASIS/GNATprove mode
4810
4811 and then (Operating_Mode = Generate_Code
4812 or else (Operating_Mode = Check_Semantics
4813 and then (ASIS_Mode or GNATprove_Mode)))
4814
4815 -- The body is needed when generating code (full expansion), in ASIS
4816 -- mode for other tools, and in GNATprove mode (special expansion) for
4817 -- formal verification of the body itself.
4818
4819 and then (Expander_Active or ASIS_Mode or GNATprove_Mode)
4820
4821 -- No point in inlining if ABE is inevitable
4822
4823 and then not ABE_Is_Certain (N)
4824
4825 -- Or if subprogram is eliminated
4826
4827 and then not Is_Eliminated (Subp)
4828 then
4829 Add_Pending_Instantiation (N, Unit_Declaration_Node (Subp));
4830 return True;
4831
4832 -- Here if not inlined, or we ignore the inlining
4833
4834 else
4835 return False;
4836 end if;
4837 end Need_Subprogram_Instance_Body;
4838
4839 --------------------------------------
4840 -- Analyze_Subprogram_Instantiation --
4841 --------------------------------------
4842
4843 procedure Analyze_Subprogram_Instantiation
4844 (N : Node_Id;
4845 K : Entity_Kind)
4846 is
4847 Loc : constant Source_Ptr := Sloc (N);
4848 Gen_Id : constant Node_Id := Name (N);
4849
4850 Anon_Id : constant Entity_Id :=
4851 Make_Defining_Identifier (Sloc (Defining_Entity (N)),
4852 Chars => New_External_Name
4853 (Chars (Defining_Entity (N)), 'R'));
4854
4855 Act_Decl_Id : Entity_Id;
4856 Act_Decl : Node_Id;
4857 Act_Spec : Node_Id;
4858 Act_Tree : Node_Id;
4859
4860 Env_Installed : Boolean := False;
4861 Gen_Unit : Entity_Id;
4862 Gen_Decl : Node_Id;
4863 Pack_Id : Entity_Id;
4864 Parent_Installed : Boolean := False;
4865
4866 Renaming_List : List_Id;
4867 -- The list of declarations that link formals and actuals of the
4868 -- instance. These are subtype declarations for formal types, and
4869 -- renaming declarations for other formals. The subprogram declaration
4870 -- for the instance is then appended to the list, and the last item on
4871 -- the list is the renaming declaration for the instance.
4872
4873 procedure Analyze_Instance_And_Renamings;
4874 -- The instance must be analyzed in a context that includes the mappings
4875 -- of generic parameters into actuals. We create a package declaration
4876 -- for this purpose, and a subprogram with an internal name within the
4877 -- package. The subprogram instance is simply an alias for the internal
4878 -- subprogram, declared in the current scope.
4879
4880 procedure Build_Subprogram_Renaming;
4881 -- If the subprogram is recursive, there are occurrences of the name of
4882 -- the generic within the body, which must resolve to the current
4883 -- instance. We add a renaming declaration after the declaration, which
4884 -- is available in the instance body, as well as in the analysis of
4885 -- aspects that appear in the generic. This renaming declaration is
4886 -- inserted after the instance declaration which it renames.
4887
4888 ------------------------------------
4889 -- Analyze_Instance_And_Renamings --
4890 ------------------------------------
4891
4892 procedure Analyze_Instance_And_Renamings is
4893 Def_Ent : constant Entity_Id := Defining_Entity (N);
4894 Pack_Decl : Node_Id;
4895
4896 begin
4897 if Nkind (Parent (N)) = N_Compilation_Unit then
4898
4899 -- For the case of a compilation unit, the container package has
4900 -- the same name as the instantiation, to insure that the binder
4901 -- calls the elaboration procedure with the right name. Copy the
4902 -- entity of the instance, which may have compilation level flags
4903 -- (e.g. Is_Child_Unit) set.
4904
4905 Pack_Id := New_Copy (Def_Ent);
4906
4907 else
4908 -- Otherwise we use the name of the instantiation concatenated
4909 -- with its source position to ensure uniqueness if there are
4910 -- several instantiations with the same name.
4911
4912 Pack_Id :=
4913 Make_Defining_Identifier (Loc,
4914 Chars => New_External_Name
4915 (Related_Id => Chars (Def_Ent),
4916 Suffix => "GP",
4917 Suffix_Index => Source_Offset (Sloc (Def_Ent))));
4918 end if;
4919
4920 Pack_Decl :=
4921 Make_Package_Declaration (Loc,
4922 Specification => Make_Package_Specification (Loc,
4923 Defining_Unit_Name => Pack_Id,
4924 Visible_Declarations => Renaming_List,
4925 End_Label => Empty));
4926
4927 Set_Instance_Spec (N, Pack_Decl);
4928 Set_Is_Generic_Instance (Pack_Id);
4929 Set_Debug_Info_Needed (Pack_Id);
4930
4931 -- Case of not a compilation unit
4932
4933 if Nkind (Parent (N)) /= N_Compilation_Unit then
4934 Mark_Rewrite_Insertion (Pack_Decl);
4935 Insert_Before (N, Pack_Decl);
4936 Set_Has_Completion (Pack_Id);
4937
4938 -- Case of an instantiation that is a compilation unit
4939
4940 -- Place declaration on current node so context is complete for
4941 -- analysis (including nested instantiations), and for use in a
4942 -- context_clause (see Analyze_With_Clause).
4943
4944 else
4945 Set_Unit (Parent (N), Pack_Decl);
4946 Set_Parent_Spec (Pack_Decl, Parent_Spec (N));
4947 end if;
4948
4949 Analyze (Pack_Decl);
4950 Check_Formal_Packages (Pack_Id);
4951 Set_Is_Generic_Instance (Pack_Id, False);
4952
4953 -- Why do we clear Is_Generic_Instance??? We set it 20 lines
4954 -- above???
4955
4956 -- Body of the enclosing package is supplied when instantiating the
4957 -- subprogram body, after semantic analysis is completed.
4958
4959 if Nkind (Parent (N)) = N_Compilation_Unit then
4960
4961 -- Remove package itself from visibility, so it does not
4962 -- conflict with subprogram.
4963
4964 Set_Name_Entity_Id (Chars (Pack_Id), Homonym (Pack_Id));
4965
4966 -- Set name and scope of internal subprogram so that the proper
4967 -- external name will be generated. The proper scope is the scope
4968 -- of the wrapper package. We need to generate debugging info for
4969 -- the internal subprogram, so set flag accordingly.
4970
4971 Set_Chars (Anon_Id, Chars (Defining_Entity (N)));
4972 Set_Scope (Anon_Id, Scope (Pack_Id));
4973
4974 -- Mark wrapper package as referenced, to avoid spurious warnings
4975 -- if the instantiation appears in various with_ clauses of
4976 -- subunits of the main unit.
4977
4978 Set_Referenced (Pack_Id);
4979 end if;
4980
4981 Set_Is_Generic_Instance (Anon_Id);
4982 Set_Debug_Info_Needed (Anon_Id);
4983 Act_Decl_Id := New_Copy (Anon_Id);
4984
4985 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
4986 Set_Chars (Act_Decl_Id, Chars (Defining_Entity (N)));
4987 Set_Sloc (Act_Decl_Id, Sloc (Defining_Entity (N)));
4988
4989 -- Subprogram instance comes from source only if generic does
4990
4991 Set_Comes_From_Source (Act_Decl_Id, Comes_From_Source (Gen_Unit));
4992
4993 -- If the instance is a child unit, mark the Id accordingly. Mark
4994 -- the anonymous entity as well, which is the real subprogram and
4995 -- which is used when the instance appears in a context clause.
4996 -- Similarly, propagate the Is_Eliminated flag to handle properly
4997 -- nested eliminated subprograms.
4998
4999 Set_Is_Child_Unit (Act_Decl_Id, Is_Child_Unit (Defining_Entity (N)));
5000 Set_Is_Child_Unit (Anon_Id, Is_Child_Unit (Defining_Entity (N)));
5001 New_Overloaded_Entity (Act_Decl_Id);
5002 Check_Eliminated (Act_Decl_Id);
5003 Set_Is_Eliminated (Anon_Id, Is_Eliminated (Act_Decl_Id));
5004
5005 -- In compilation unit case, kill elaboration checks on the
5006 -- instantiation, since they are never needed -- the body is
5007 -- instantiated at the same point as the spec.
5008
5009 if Nkind (Parent (N)) = N_Compilation_Unit then
5010 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
5011 Set_Kill_Elaboration_Checks (Act_Decl_Id);
5012 Set_Is_Compilation_Unit (Anon_Id);
5013
5014 Set_Cunit_Entity (Current_Sem_Unit, Pack_Id);
5015 end if;
5016
5017 -- The instance is not a freezing point for the new subprogram.
5018 -- The anonymous subprogram may have a freeze node, created for
5019 -- some delayed aspects. This freeze node must not be inherited
5020 -- by the visible subprogram entity.
5021
5022 Set_Is_Frozen (Act_Decl_Id, False);
5023 Set_Freeze_Node (Act_Decl_Id, Empty);
5024
5025 if Nkind (Defining_Entity (N)) = N_Defining_Operator_Symbol then
5026 Valid_Operator_Definition (Act_Decl_Id);
5027 end if;
5028
5029 Set_Alias (Act_Decl_Id, Anon_Id);
5030 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
5031 Set_Has_Completion (Act_Decl_Id);
5032 Set_Related_Instance (Pack_Id, Act_Decl_Id);
5033
5034 if Nkind (Parent (N)) = N_Compilation_Unit then
5035 Set_Body_Required (Parent (N), False);
5036 end if;
5037 end Analyze_Instance_And_Renamings;
5038
5039 -------------------------------
5040 -- Build_Subprogram_Renaming --
5041 -------------------------------
5042
5043 procedure Build_Subprogram_Renaming is
5044 Renaming_Decl : Node_Id;
5045 Unit_Renaming : Node_Id;
5046
5047 begin
5048 Unit_Renaming :=
5049 Make_Subprogram_Renaming_Declaration (Loc,
5050 Specification =>
5051 Copy_Generic_Node
5052 (Specification (Original_Node (Gen_Decl)),
5053 Empty,
5054 Instantiating => True),
5055 Name => New_Occurrence_Of (Anon_Id, Loc));
5056
5057 -- The generic may be a a child unit. The renaming needs an
5058 -- identifier with the proper name.
5059
5060 Set_Defining_Unit_Name (Specification (Unit_Renaming),
5061 Make_Defining_Identifier (Loc, Chars (Gen_Unit)));
5062
5063 -- If there is a formal subprogram with the same name as the unit
5064 -- itself, do not add this renaming declaration, to prevent
5065 -- ambiguities when there is a call with that name in the body.
5066 -- This is a partial and ugly fix for one ACATS test. ???
5067
5068 Renaming_Decl := First (Renaming_List);
5069 while Present (Renaming_Decl) loop
5070 if Nkind (Renaming_Decl) = N_Subprogram_Renaming_Declaration
5071 and then
5072 Chars (Defining_Entity (Renaming_Decl)) = Chars (Gen_Unit)
5073 then
5074 exit;
5075 end if;
5076
5077 Next (Renaming_Decl);
5078 end loop;
5079
5080 if No (Renaming_Decl) then
5081 Append (Unit_Renaming, Renaming_List);
5082 end if;
5083 end Build_Subprogram_Renaming;
5084
5085 -- Local variables
5086
5087 Save_IPSM : constant Boolean := Ignore_Pragma_SPARK_Mode;
5088 -- Save flag Ignore_Pragma_SPARK_Mode for restore on exit
5089
5090 Save_SM : constant SPARK_Mode_Type := SPARK_Mode;
5091 Save_SMP : constant Node_Id := SPARK_Mode_Pragma;
5092 -- Save the SPARK_Mode-related data for restore on exit
5093
5094 Vis_Prims_List : Elist_Id := No_Elist;
5095 -- List of primitives made temporarily visible in the instantiation
5096 -- to match the visibility of the formal type
5097
5098 -- Start of processing for Analyze_Subprogram_Instantiation
5099
5100 begin
5101 Check_SPARK_05_Restriction ("generic is not allowed", N);
5102
5103 -- Very first thing: check for special Text_IO unit in case we are
5104 -- instantiating one of the children of [[Wide_]Wide_]Text_IO. Of course
5105 -- such an instantiation is bogus (these are packages, not subprograms),
5106 -- but we get a better error message if we do this.
5107
5108 Check_Text_IO_Special_Unit (Gen_Id);
5109
5110 -- Make node global for error reporting
5111
5112 Instantiation_Node := N;
5113
5114 -- For package instantiations we turn off style checks, because they
5115 -- will have been emitted in the generic. For subprogram instantiations
5116 -- we want to apply at least the check on overriding indicators so we
5117 -- do not modify the style check status.
5118
5119 -- The renaming declarations for the actuals do not come from source and
5120 -- will not generate spurious warnings.
5121
5122 Preanalyze_Actuals (N);
5123
5124 Init_Env;
5125 Env_Installed := True;
5126 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
5127 Gen_Unit := Entity (Gen_Id);
5128
5129 Generate_Reference (Gen_Unit, Gen_Id);
5130
5131 if Nkind (Gen_Id) = N_Identifier
5132 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
5133 then
5134 Error_Msg_NE
5135 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
5136 end if;
5137
5138 if Etype (Gen_Unit) = Any_Type then
5139 Restore_Env;
5140 return;
5141 end if;
5142
5143 -- Verify that it is a generic subprogram of the right kind, and that
5144 -- it does not lead to a circular instantiation.
5145
5146 if K = E_Procedure and then Ekind (Gen_Unit) /= E_Generic_Procedure then
5147 Error_Msg_NE
5148 ("& is not the name of a generic procedure", Gen_Id, Gen_Unit);
5149
5150 elsif K = E_Function and then Ekind (Gen_Unit) /= E_Generic_Function then
5151 Error_Msg_NE
5152 ("& is not the name of a generic function", Gen_Id, Gen_Unit);
5153
5154 elsif In_Open_Scopes (Gen_Unit) then
5155 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
5156
5157 else
5158 -- If the context of the instance is subject to SPARK_Mode "off" or
5159 -- the annotation is altogether missing, set the global flag which
5160 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
5161 -- the instance.
5162
5163 if SPARK_Mode /= On then
5164 Ignore_Pragma_SPARK_Mode := True;
5165 end if;
5166
5167 Set_Entity (Gen_Id, Gen_Unit);
5168 Set_Is_Instantiated (Gen_Unit);
5169
5170 if In_Extended_Main_Source_Unit (N) then
5171 Generate_Reference (Gen_Unit, N);
5172 end if;
5173
5174 -- If renaming, get original unit
5175
5176 if Present (Renamed_Object (Gen_Unit))
5177 and then Ekind_In (Renamed_Object (Gen_Unit), E_Generic_Procedure,
5178 E_Generic_Function)
5179 then
5180 Gen_Unit := Renamed_Object (Gen_Unit);
5181 Set_Is_Instantiated (Gen_Unit);
5182 Generate_Reference (Gen_Unit, N);
5183 end if;
5184
5185 if Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
5186 Error_Msg_Node_2 := Current_Scope;
5187 Error_Msg_NE
5188 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
5189 Circularity_Detected := True;
5190 Restore_Hidden_Primitives (Vis_Prims_List);
5191 goto Leave;
5192 end if;
5193
5194 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
5195
5196 -- Initialize renamings map, for error checking
5197
5198 Generic_Renamings.Set_Last (0);
5199 Generic_Renamings_HTable.Reset;
5200
5201 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
5202
5203 -- Copy original generic tree, to produce text for instantiation
5204
5205 Act_Tree :=
5206 Copy_Generic_Node
5207 (Original_Node (Gen_Decl), Empty, Instantiating => True);
5208
5209 -- Inherit overriding indicator from instance node
5210
5211 Act_Spec := Specification (Act_Tree);
5212 Set_Must_Override (Act_Spec, Must_Override (N));
5213 Set_Must_Not_Override (Act_Spec, Must_Not_Override (N));
5214
5215 Renaming_List :=
5216 Analyze_Associations
5217 (I_Node => N,
5218 Formals => Generic_Formal_Declarations (Act_Tree),
5219 F_Copy => Generic_Formal_Declarations (Gen_Decl));
5220
5221 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
5222
5223 -- The subprogram itself cannot contain a nested instance, so the
5224 -- current parent is left empty.
5225
5226 Set_Instance_Env (Gen_Unit, Empty);
5227
5228 -- Build the subprogram declaration, which does not appear in the
5229 -- generic template, and give it a sloc consistent with that of the
5230 -- template.
5231
5232 Set_Defining_Unit_Name (Act_Spec, Anon_Id);
5233 Set_Generic_Parent (Act_Spec, Gen_Unit);
5234 Act_Decl :=
5235 Make_Subprogram_Declaration (Sloc (Act_Spec),
5236 Specification => Act_Spec);
5237
5238 -- The aspects have been copied previously, but they have to be
5239 -- linked explicitly to the new subprogram declaration. Explicit
5240 -- pre/postconditions on the instance are analyzed below, in a
5241 -- separate step.
5242
5243 Move_Aspects (Act_Tree, To => Act_Decl);
5244 Set_Categorization_From_Pragmas (Act_Decl);
5245
5246 if Parent_Installed then
5247 Hide_Current_Scope;
5248 end if;
5249
5250 Append (Act_Decl, Renaming_List);
5251
5252 -- Contract-related source pragmas that follow a generic subprogram
5253 -- must be instantiated explicitly because they are not part of the
5254 -- subprogram template.
5255
5256 Instantiate_Subprogram_Contract
5257 (Original_Node (Gen_Decl), Renaming_List);
5258
5259 Build_Subprogram_Renaming;
5260 Analyze_Instance_And_Renamings;
5261
5262 -- If the generic is marked Import (Intrinsic), then so is the
5263 -- instance. This indicates that there is no body to instantiate. If
5264 -- generic is marked inline, so it the instance, and the anonymous
5265 -- subprogram it renames. If inlined, or else if inlining is enabled
5266 -- for the compilation, we generate the instance body even if it is
5267 -- not within the main unit.
5268
5269 if Is_Intrinsic_Subprogram (Gen_Unit) then
5270 Set_Is_Intrinsic_Subprogram (Anon_Id);
5271 Set_Is_Intrinsic_Subprogram (Act_Decl_Id);
5272
5273 if Chars (Gen_Unit) = Name_Unchecked_Conversion then
5274 Validate_Unchecked_Conversion (N, Act_Decl_Id);
5275 end if;
5276 end if;
5277
5278 -- Inherit convention from generic unit. Intrinsic convention, as for
5279 -- an instance of unchecked conversion, is not inherited because an
5280 -- explicit Ada instance has been created.
5281
5282 if Has_Convention_Pragma (Gen_Unit)
5283 and then Convention (Gen_Unit) /= Convention_Intrinsic
5284 then
5285 Set_Convention (Act_Decl_Id, Convention (Gen_Unit));
5286 Set_Is_Exported (Act_Decl_Id, Is_Exported (Gen_Unit));
5287 end if;
5288
5289 Generate_Definition (Act_Decl_Id);
5290
5291 -- Inherit all inlining-related flags which apply to the generic in
5292 -- the subprogram and its declaration.
5293
5294 Set_Is_Inlined (Act_Decl_Id, Is_Inlined (Gen_Unit));
5295 Set_Is_Inlined (Anon_Id, Is_Inlined (Gen_Unit));
5296
5297 Set_Has_Pragma_Inline (Act_Decl_Id, Has_Pragma_Inline (Gen_Unit));
5298 Set_Has_Pragma_Inline (Anon_Id, Has_Pragma_Inline (Gen_Unit));
5299
5300 Set_Has_Pragma_Inline_Always
5301 (Act_Decl_Id, Has_Pragma_Inline_Always (Gen_Unit));
5302 Set_Has_Pragma_Inline_Always
5303 (Anon_Id, Has_Pragma_Inline_Always (Gen_Unit));
5304
5305 if not Is_Intrinsic_Subprogram (Gen_Unit) then
5306 Check_Elab_Instantiation (N);
5307 end if;
5308
5309 if Is_Dispatching_Operation (Act_Decl_Id)
5310 and then Ada_Version >= Ada_2005
5311 then
5312 declare
5313 Formal : Entity_Id;
5314
5315 begin
5316 Formal := First_Formal (Act_Decl_Id);
5317 while Present (Formal) loop
5318 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type
5319 and then Is_Controlling_Formal (Formal)
5320 and then not Can_Never_Be_Null (Formal)
5321 then
5322 Error_Msg_NE
5323 ("access parameter& is controlling,", N, Formal);
5324 Error_Msg_NE
5325 ("\corresponding parameter of & must be "
5326 & "explicitly null-excluding", N, Gen_Id);
5327 end if;
5328
5329 Next_Formal (Formal);
5330 end loop;
5331 end;
5332 end if;
5333
5334 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
5335
5336 Validate_Categorization_Dependency (N, Act_Decl_Id);
5337
5338 if not Is_Intrinsic_Subprogram (Act_Decl_Id) then
5339 Inherit_Context (Gen_Decl, N);
5340
5341 Restore_Private_Views (Pack_Id, False);
5342
5343 -- If the context requires a full instantiation, mark node for
5344 -- subsequent construction of the body.
5345
5346 if Need_Subprogram_Instance_Body (N, Act_Decl_Id) then
5347 Check_Forward_Instantiation (Gen_Decl);
5348
5349 -- The wrapper package is always delayed, because it does not
5350 -- constitute a freeze point, but to insure that the freeze node
5351 -- is placed properly, it is created directly when instantiating
5352 -- the body (otherwise the freeze node might appear to early for
5353 -- nested instantiations). For ASIS purposes, indicate that the
5354 -- wrapper package has replaced the instantiation node.
5355
5356 elsif Nkind (Parent (N)) = N_Compilation_Unit then
5357 Rewrite (N, Unit (Parent (N)));
5358 Set_Unit (Parent (N), N);
5359 end if;
5360
5361 -- Replace instance node for library-level instantiations of
5362 -- intrinsic subprograms, for ASIS use.
5363
5364 elsif Nkind (Parent (N)) = N_Compilation_Unit then
5365 Rewrite (N, Unit (Parent (N)));
5366 Set_Unit (Parent (N), N);
5367 end if;
5368
5369 if Parent_Installed then
5370 Remove_Parent;
5371 end if;
5372
5373 Restore_Hidden_Primitives (Vis_Prims_List);
5374 Restore_Env;
5375 Env_Installed := False;
5376 Generic_Renamings.Set_Last (0);
5377 Generic_Renamings_HTable.Reset;
5378
5379 Ignore_Pragma_SPARK_Mode := Save_IPSM;
5380 SPARK_Mode := Save_SM;
5381 SPARK_Mode_Pragma := Save_SMP;
5382 end if;
5383
5384 <<Leave>>
5385 if Has_Aspects (N) then
5386 Analyze_Aspect_Specifications (N, Act_Decl_Id);
5387 end if;
5388
5389 exception
5390 when Instantiation_Error =>
5391 if Parent_Installed then
5392 Remove_Parent;
5393 end if;
5394
5395 if Env_Installed then
5396 Restore_Env;
5397 end if;
5398
5399 Ignore_Pragma_SPARK_Mode := Save_IPSM;
5400 SPARK_Mode := Save_SM;
5401 SPARK_Mode_Pragma := Save_SMP;
5402 end Analyze_Subprogram_Instantiation;
5403
5404 -------------------------
5405 -- Get_Associated_Node --
5406 -------------------------
5407
5408 function Get_Associated_Node (N : Node_Id) return Node_Id is
5409 Assoc : Node_Id;
5410
5411 begin
5412 Assoc := Associated_Node (N);
5413
5414 if Nkind (Assoc) /= Nkind (N) then
5415 return Assoc;
5416
5417 elsif Nkind_In (Assoc, N_Aggregate, N_Extension_Aggregate) then
5418 return Assoc;
5419
5420 else
5421 -- If the node is part of an inner generic, it may itself have been
5422 -- remapped into a further generic copy. Associated_Node is otherwise
5423 -- used for the entity of the node, and will be of a different node
5424 -- kind, or else N has been rewritten as a literal or function call.
5425
5426 while Present (Associated_Node (Assoc))
5427 and then Nkind (Associated_Node (Assoc)) = Nkind (Assoc)
5428 loop
5429 Assoc := Associated_Node (Assoc);
5430 end loop;
5431
5432 -- Follow and additional link in case the final node was rewritten.
5433 -- This can only happen with nested generic units.
5434
5435 if (Nkind (Assoc) = N_Identifier or else Nkind (Assoc) in N_Op)
5436 and then Present (Associated_Node (Assoc))
5437 and then (Nkind_In (Associated_Node (Assoc), N_Function_Call,
5438 N_Explicit_Dereference,
5439 N_Integer_Literal,
5440 N_Real_Literal,
5441 N_String_Literal))
5442 then
5443 Assoc := Associated_Node (Assoc);
5444 end if;
5445
5446 -- An additional special case: an unconstrained type in an object
5447 -- declaration may have been rewritten as a local subtype constrained
5448 -- by the expression in the declaration. We need to recover the
5449 -- original entity which may be global.
5450
5451 if Present (Original_Node (Assoc))
5452 and then Nkind (Parent (N)) = N_Object_Declaration
5453 then
5454 Assoc := Original_Node (Assoc);
5455 end if;
5456
5457 return Assoc;
5458 end if;
5459 end Get_Associated_Node;
5460
5461 ----------------------------
5462 -- Build_Function_Wrapper --
5463 ----------------------------
5464
5465 function Build_Function_Wrapper
5466 (Formal_Subp : Entity_Id;
5467 Actual_Subp : Entity_Id) return Node_Id
5468 is
5469 Loc : constant Source_Ptr := Sloc (Current_Scope);
5470 Ret_Type : constant Entity_Id := Get_Instance_Of (Etype (Formal_Subp));
5471 Actuals : List_Id;
5472 Decl : Node_Id;
5473 Func_Name : Node_Id;
5474 Func : Entity_Id;
5475 Parm_Type : Node_Id;
5476 Profile : List_Id := New_List;
5477 Spec : Node_Id;
5478 Act_F : Entity_Id;
5479 Form_F : Entity_Id;
5480 New_F : Entity_Id;
5481
5482 begin
5483 Func_Name := New_Occurrence_Of (Actual_Subp, Loc);
5484
5485 Func := Make_Defining_Identifier (Loc, Chars (Formal_Subp));
5486 Set_Ekind (Func, E_Function);
5487 Set_Is_Generic_Actual_Subprogram (Func);
5488
5489 Actuals := New_List;
5490 Profile := New_List;
5491
5492 Act_F := First_Formal (Actual_Subp);
5493 Form_F := First_Formal (Formal_Subp);
5494 while Present (Form_F) loop
5495
5496 -- Create new formal for profile of wrapper, and add a reference
5497 -- to it in the list of actuals for the enclosing call. The name
5498 -- must be that of the formal in the formal subprogram, because
5499 -- calls to it in the generic body may use named associations.
5500
5501 New_F := Make_Defining_Identifier (Loc, Chars (Form_F));
5502
5503 Parm_Type :=
5504 New_Occurrence_Of (Get_Instance_Of (Etype (Form_F)), Loc);
5505
5506 Append_To (Profile,
5507 Make_Parameter_Specification (Loc,
5508 Defining_Identifier => New_F,
5509 Parameter_Type => Parm_Type));
5510
5511 Append_To (Actuals, New_Occurrence_Of (New_F, Loc));
5512 Next_Formal (Form_F);
5513
5514 if Present (Act_F) then
5515 Next_Formal (Act_F);
5516 end if;
5517 end loop;
5518
5519 Spec :=
5520 Make_Function_Specification (Loc,
5521 Defining_Unit_Name => Func,
5522 Parameter_Specifications => Profile,
5523 Result_Definition => New_Occurrence_Of (Ret_Type, Loc));
5524
5525 Decl :=
5526 Make_Expression_Function (Loc,
5527 Specification => Spec,
5528 Expression =>
5529 Make_Function_Call (Loc,
5530 Name => Func_Name,
5531 Parameter_Associations => Actuals));
5532
5533 return Decl;
5534 end Build_Function_Wrapper;
5535
5536 ----------------------------
5537 -- Build_Operator_Wrapper --
5538 ----------------------------
5539
5540 function Build_Operator_Wrapper
5541 (Formal_Subp : Entity_Id;
5542 Actual_Subp : Entity_Id) return Node_Id
5543 is
5544 Loc : constant Source_Ptr := Sloc (Current_Scope);
5545 Ret_Type : constant Entity_Id :=
5546 Get_Instance_Of (Etype (Formal_Subp));
5547 Op_Type : constant Entity_Id :=
5548 Get_Instance_Of (Etype (First_Formal (Formal_Subp)));
5549 Is_Binary : constant Boolean :=
5550 Present (Next_Formal (First_Formal (Formal_Subp)));
5551
5552 Decl : Node_Id;
5553 Expr : Node_Id;
5554 F1, F2 : Entity_Id;
5555 Func : Entity_Id;
5556 Op_Name : Name_Id;
5557 Spec : Node_Id;
5558 L, R : Node_Id;
5559
5560 begin
5561 Op_Name := Chars (Actual_Subp);
5562
5563 -- Create entities for wrapper function and its formals
5564
5565 F1 := Make_Temporary (Loc, 'A');
5566 F2 := Make_Temporary (Loc, 'B');
5567 L := New_Occurrence_Of (F1, Loc);
5568 R := New_Occurrence_Of (F2, Loc);
5569
5570 Func := Make_Defining_Identifier (Loc, Chars (Formal_Subp));
5571 Set_Ekind (Func, E_Function);
5572 Set_Is_Generic_Actual_Subprogram (Func);
5573
5574 Spec :=
5575 Make_Function_Specification (Loc,
5576 Defining_Unit_Name => Func,
5577 Parameter_Specifications => New_List (
5578 Make_Parameter_Specification (Loc,
5579 Defining_Identifier => F1,
5580 Parameter_Type => New_Occurrence_Of (Op_Type, Loc))),
5581 Result_Definition => New_Occurrence_Of (Ret_Type, Loc));
5582
5583 if Is_Binary then
5584 Append_To (Parameter_Specifications (Spec),
5585 Make_Parameter_Specification (Loc,
5586 Defining_Identifier => F2,
5587 Parameter_Type => New_Occurrence_Of (Op_Type, Loc)));
5588 end if;
5589
5590 -- Build expression as a function call, or as an operator node
5591 -- that corresponds to the name of the actual, starting with
5592 -- binary operators.
5593
5594 if Op_Name not in Any_Operator_Name then
5595 Expr :=
5596 Make_Function_Call (Loc,
5597 Name =>
5598 New_Occurrence_Of (Actual_Subp, Loc),
5599 Parameter_Associations => New_List (L));
5600
5601 if Is_Binary then
5602 Append_To (Parameter_Associations (Expr), R);
5603 end if;
5604
5605 -- Binary operators
5606
5607 elsif Is_Binary then
5608 if Op_Name = Name_Op_And then
5609 Expr := Make_Op_And (Loc, Left_Opnd => L, Right_Opnd => R);
5610 elsif Op_Name = Name_Op_Or then
5611 Expr := Make_Op_Or (Loc, Left_Opnd => L, Right_Opnd => R);
5612 elsif Op_Name = Name_Op_Xor then
5613 Expr := Make_Op_Xor (Loc, Left_Opnd => L, Right_Opnd => R);
5614 elsif Op_Name = Name_Op_Eq then
5615 Expr := Make_Op_Eq (Loc, Left_Opnd => L, Right_Opnd => R);
5616 elsif Op_Name = Name_Op_Ne then
5617 Expr := Make_Op_Ne (Loc, Left_Opnd => L, Right_Opnd => R);
5618 elsif Op_Name = Name_Op_Le then
5619 Expr := Make_Op_Le (Loc, Left_Opnd => L, Right_Opnd => R);
5620 elsif Op_Name = Name_Op_Gt then
5621 Expr := Make_Op_Gt (Loc, Left_Opnd => L, Right_Opnd => R);
5622 elsif Op_Name = Name_Op_Ge then
5623 Expr := Make_Op_Ge (Loc, Left_Opnd => L, Right_Opnd => R);
5624 elsif Op_Name = Name_Op_Lt then
5625 Expr := Make_Op_Lt (Loc, Left_Opnd => L, Right_Opnd => R);
5626 elsif Op_Name = Name_Op_Add then
5627 Expr := Make_Op_Add (Loc, Left_Opnd => L, Right_Opnd => R);
5628 elsif Op_Name = Name_Op_Subtract then
5629 Expr := Make_Op_Subtract (Loc, Left_Opnd => L, Right_Opnd => R);
5630 elsif Op_Name = Name_Op_Concat then
5631 Expr := Make_Op_Concat (Loc, Left_Opnd => L, Right_Opnd => R);
5632 elsif Op_Name = Name_Op_Multiply then
5633 Expr := Make_Op_Multiply (Loc, Left_Opnd => L, Right_Opnd => R);
5634 elsif Op_Name = Name_Op_Divide then
5635 Expr := Make_Op_Divide (Loc, Left_Opnd => L, Right_Opnd => R);
5636 elsif Op_Name = Name_Op_Mod then
5637 Expr := Make_Op_Mod (Loc, Left_Opnd => L, Right_Opnd => R);
5638 elsif Op_Name = Name_Op_Rem then
5639 Expr := Make_Op_Rem (Loc, Left_Opnd => L, Right_Opnd => R);
5640 elsif Op_Name = Name_Op_Expon then
5641 Expr := Make_Op_Expon (Loc, Left_Opnd => L, Right_Opnd => R);
5642 end if;
5643
5644 -- Unary operators
5645
5646 else
5647 if Op_Name = Name_Op_Add then
5648 Expr := Make_Op_Plus (Loc, Right_Opnd => L);
5649 elsif Op_Name = Name_Op_Subtract then
5650 Expr := Make_Op_Minus (Loc, Right_Opnd => L);
5651 elsif Op_Name = Name_Op_Abs then
5652 Expr := Make_Op_Abs (Loc, Right_Opnd => L);
5653 elsif Op_Name = Name_Op_Not then
5654 Expr := Make_Op_Not (Loc, Right_Opnd => L);
5655 end if;
5656 end if;
5657
5658 Decl :=
5659 Make_Expression_Function (Loc,
5660 Specification => Spec,
5661 Expression => Expr);
5662
5663 return Decl;
5664 end Build_Operator_Wrapper;
5665
5666 -------------------------------------------
5667 -- Build_Instance_Compilation_Unit_Nodes --
5668 -------------------------------------------
5669
5670 procedure Build_Instance_Compilation_Unit_Nodes
5671 (N : Node_Id;
5672 Act_Body : Node_Id;
5673 Act_Decl : Node_Id)
5674 is
5675 Decl_Cunit : Node_Id;
5676 Body_Cunit : Node_Id;
5677 Citem : Node_Id;
5678 New_Main : constant Entity_Id := Defining_Entity (Act_Decl);
5679 Old_Main : constant Entity_Id := Cunit_Entity (Main_Unit);
5680
5681 begin
5682 -- A new compilation unit node is built for the instance declaration
5683
5684 Decl_Cunit :=
5685 Make_Compilation_Unit (Sloc (N),
5686 Context_Items => Empty_List,
5687 Unit => Act_Decl,
5688 Aux_Decls_Node => Make_Compilation_Unit_Aux (Sloc (N)));
5689
5690 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
5691
5692 -- The new compilation unit is linked to its body, but both share the
5693 -- same file, so we do not set Body_Required on the new unit so as not
5694 -- to create a spurious dependency on a non-existent body in the ali.
5695 -- This simplifies CodePeer unit traversal.
5696
5697 -- We use the original instantiation compilation unit as the resulting
5698 -- compilation unit of the instance, since this is the main unit.
5699
5700 Rewrite (N, Act_Body);
5701
5702 -- Propagate the aspect specifications from the package body template to
5703 -- the instantiated version of the package body.
5704
5705 if Has_Aspects (Act_Body) then
5706 Set_Aspect_Specifications
5707 (N, New_Copy_List_Tree (Aspect_Specifications (Act_Body)));
5708 end if;
5709
5710 Body_Cunit := Parent (N);
5711
5712 -- The two compilation unit nodes are linked by the Library_Unit field
5713
5714 Set_Library_Unit (Decl_Cunit, Body_Cunit);
5715 Set_Library_Unit (Body_Cunit, Decl_Cunit);
5716
5717 -- Preserve the private nature of the package if needed
5718
5719 Set_Private_Present (Decl_Cunit, Private_Present (Body_Cunit));
5720
5721 -- If the instance is not the main unit, its context, categorization
5722 -- and elaboration entity are not relevant to the compilation.
5723
5724 if Body_Cunit /= Cunit (Main_Unit) then
5725 Make_Instance_Unit (Body_Cunit, In_Main => False);
5726 return;
5727 end if;
5728
5729 -- The context clause items on the instantiation, which are now attached
5730 -- to the body compilation unit (since the body overwrote the original
5731 -- instantiation node), semantically belong on the spec, so copy them
5732 -- there. It's harmless to leave them on the body as well. In fact one
5733 -- could argue that they belong in both places.
5734
5735 Citem := First (Context_Items (Body_Cunit));
5736 while Present (Citem) loop
5737 Append (New_Copy (Citem), Context_Items (Decl_Cunit));
5738 Next (Citem);
5739 end loop;
5740
5741 -- Propagate categorization flags on packages, so that they appear in
5742 -- the ali file for the spec of the unit.
5743
5744 if Ekind (New_Main) = E_Package then
5745 Set_Is_Pure (Old_Main, Is_Pure (New_Main));
5746 Set_Is_Preelaborated (Old_Main, Is_Preelaborated (New_Main));
5747 Set_Is_Remote_Types (Old_Main, Is_Remote_Types (New_Main));
5748 Set_Is_Shared_Passive (Old_Main, Is_Shared_Passive (New_Main));
5749 Set_Is_Remote_Call_Interface
5750 (Old_Main, Is_Remote_Call_Interface (New_Main));
5751 end if;
5752
5753 -- Make entry in Units table, so that binder can generate call to
5754 -- elaboration procedure for body, if any.
5755
5756 Make_Instance_Unit (Body_Cunit, In_Main => True);
5757 Main_Unit_Entity := New_Main;
5758 Set_Cunit_Entity (Main_Unit, Main_Unit_Entity);
5759
5760 -- Build elaboration entity, since the instance may certainly generate
5761 -- elaboration code requiring a flag for protection.
5762
5763 Build_Elaboration_Entity (Decl_Cunit, New_Main);
5764 end Build_Instance_Compilation_Unit_Nodes;
5765
5766 -----------------------------
5767 -- Check_Access_Definition --
5768 -----------------------------
5769
5770 procedure Check_Access_Definition (N : Node_Id) is
5771 begin
5772 pragma Assert
5773 (Ada_Version >= Ada_2005 and then Present (Access_Definition (N)));
5774 null;
5775 end Check_Access_Definition;
5776
5777 -----------------------------------
5778 -- Check_Formal_Package_Instance --
5779 -----------------------------------
5780
5781 -- If the formal has specific parameters, they must match those of the
5782 -- actual. Both of them are instances, and the renaming declarations for
5783 -- their formal parameters appear in the same order in both. The analyzed
5784 -- formal has been analyzed in the context of the current instance.
5785
5786 procedure Check_Formal_Package_Instance
5787 (Formal_Pack : Entity_Id;
5788 Actual_Pack : Entity_Id)
5789 is
5790 E1 : Entity_Id := First_Entity (Actual_Pack);
5791 E2 : Entity_Id := First_Entity (Formal_Pack);
5792 Prev_E1 : Entity_Id;
5793
5794 Expr1 : Node_Id;
5795 Expr2 : Node_Id;
5796
5797 procedure Check_Mismatch (B : Boolean);
5798 -- Common error routine for mismatch between the parameters of the
5799 -- actual instance and those of the formal package.
5800
5801 function Same_Instantiated_Constant (E1, E2 : Entity_Id) return Boolean;
5802 -- The formal may come from a nested formal package, and the actual may
5803 -- have been constant-folded. To determine whether the two denote the
5804 -- same entity we may have to traverse several definitions to recover
5805 -- the ultimate entity that they refer to.
5806
5807 function Same_Instantiated_Function (E1, E2 : Entity_Id) return Boolean;
5808 -- The formal and the actual must be identical, but if both are
5809 -- given by attributes they end up renaming different generated bodies,
5810 -- and we must verify that the attributes themselves match.
5811
5812 function Same_Instantiated_Variable (E1, E2 : Entity_Id) return Boolean;
5813 -- Similarly, if the formal comes from a nested formal package, the
5814 -- actual may designate the formal through multiple renamings, which
5815 -- have to be followed to determine the original variable in question.
5816
5817 --------------------
5818 -- Check_Mismatch --
5819 --------------------
5820
5821 procedure Check_Mismatch (B : Boolean) is
5822 -- A Formal_Type_Declaration for a derived private type is rewritten
5823 -- as a private extension decl. (see Analyze_Formal_Derived_Type),
5824 -- which is why we examine the original node.
5825
5826 Kind : constant Node_Kind := Nkind (Original_Node (Parent (E2)));
5827
5828 begin
5829 if Kind = N_Formal_Type_Declaration then
5830 return;
5831
5832 elsif Nkind_In (Kind, N_Formal_Object_Declaration,
5833 N_Formal_Package_Declaration)
5834 or else Kind in N_Formal_Subprogram_Declaration
5835 then
5836 null;
5837
5838 -- Ada 2012: If both formal and actual are incomplete types they
5839 -- are conformant.
5840
5841 elsif Is_Incomplete_Type (E1) and then Is_Incomplete_Type (E2) then
5842 null;
5843
5844 elsif B then
5845 Error_Msg_NE
5846 ("actual for & in actual instance does not match formal",
5847 Parent (Actual_Pack), E1);
5848 end if;
5849 end Check_Mismatch;
5850
5851 --------------------------------
5852 -- Same_Instantiated_Constant --
5853 --------------------------------
5854
5855 function Same_Instantiated_Constant
5856 (E1, E2 : Entity_Id) return Boolean
5857 is
5858 Ent : Entity_Id;
5859
5860 begin
5861 Ent := E2;
5862 while Present (Ent) loop
5863 if E1 = Ent then
5864 return True;
5865
5866 elsif Ekind (Ent) /= E_Constant then
5867 return False;
5868
5869 elsif Is_Entity_Name (Constant_Value (Ent)) then
5870 if Entity (Constant_Value (Ent)) = E1 then
5871 return True;
5872 else
5873 Ent := Entity (Constant_Value (Ent));
5874 end if;
5875
5876 -- The actual may be a constant that has been folded. Recover
5877 -- original name.
5878
5879 elsif Is_Entity_Name (Original_Node (Constant_Value (Ent))) then
5880 Ent := Entity (Original_Node (Constant_Value (Ent)));
5881
5882 else
5883 return False;
5884 end if;
5885 end loop;
5886
5887 return False;
5888 end Same_Instantiated_Constant;
5889
5890 --------------------------------
5891 -- Same_Instantiated_Function --
5892 --------------------------------
5893
5894 function Same_Instantiated_Function
5895 (E1, E2 : Entity_Id) return Boolean
5896 is
5897 U1, U2 : Node_Id;
5898 begin
5899 if Alias (E1) = Alias (E2) then
5900 return True;
5901
5902 elsif Present (Alias (E2)) then
5903 U1 := Original_Node (Unit_Declaration_Node (E1));
5904 U2 := Original_Node (Unit_Declaration_Node (Alias (E2)));
5905
5906 return Nkind (U1) = N_Subprogram_Renaming_Declaration
5907 and then Nkind (Name (U1)) = N_Attribute_Reference
5908
5909 and then Nkind (U2) = N_Subprogram_Renaming_Declaration
5910 and then Nkind (Name (U2)) = N_Attribute_Reference
5911
5912 and then
5913 Attribute_Name (Name (U1)) = Attribute_Name (Name (U2));
5914 else
5915 return False;
5916 end if;
5917 end Same_Instantiated_Function;
5918
5919 --------------------------------
5920 -- Same_Instantiated_Variable --
5921 --------------------------------
5922
5923 function Same_Instantiated_Variable
5924 (E1, E2 : Entity_Id) return Boolean
5925 is
5926 function Original_Entity (E : Entity_Id) return Entity_Id;
5927 -- Follow chain of renamings to the ultimate ancestor
5928
5929 ---------------------
5930 -- Original_Entity --
5931 ---------------------
5932
5933 function Original_Entity (E : Entity_Id) return Entity_Id is
5934 Orig : Entity_Id;
5935
5936 begin
5937 Orig := E;
5938 while Nkind (Parent (Orig)) = N_Object_Renaming_Declaration
5939 and then Present (Renamed_Object (Orig))
5940 and then Is_Entity_Name (Renamed_Object (Orig))
5941 loop
5942 Orig := Entity (Renamed_Object (Orig));
5943 end loop;
5944
5945 return Orig;
5946 end Original_Entity;
5947
5948 -- Start of processing for Same_Instantiated_Variable
5949
5950 begin
5951 return Ekind (E1) = Ekind (E2)
5952 and then Original_Entity (E1) = Original_Entity (E2);
5953 end Same_Instantiated_Variable;
5954
5955 -- Start of processing for Check_Formal_Package_Instance
5956
5957 begin
5958 Prev_E1 := E1;
5959 while Present (E1) and then Present (E2) loop
5960 exit when Ekind (E1) = E_Package
5961 and then Renamed_Entity (E1) = Renamed_Entity (Actual_Pack);
5962
5963 -- If the formal is the renaming of the formal package, this
5964 -- is the end of its formal part, which may occur before the
5965 -- end of the formal part in the actual in the presence of
5966 -- defaulted parameters in the formal package.
5967
5968 exit when Nkind (Parent (E2)) = N_Package_Renaming_Declaration
5969 and then Renamed_Entity (E2) = Scope (E2);
5970
5971 -- The analysis of the actual may generate additional internal
5972 -- entities. If the formal is defaulted, there is no corresponding
5973 -- analysis and the internal entities must be skipped, until we
5974 -- find corresponding entities again.
5975
5976 if Comes_From_Source (E2)
5977 and then not Comes_From_Source (E1)
5978 and then Chars (E1) /= Chars (E2)
5979 then
5980 while Present (E1) and then Chars (E1) /= Chars (E2) loop
5981 Next_Entity (E1);
5982 end loop;
5983 end if;
5984
5985 if No (E1) then
5986 return;
5987
5988 -- Entities may be declared without full declaration, such as
5989 -- itypes and predefined operators (concatenation for arrays, eg).
5990 -- Skip it and keep the formal entity to find a later match for it.
5991
5992 elsif No (Parent (E2)) then
5993 E1 := Prev_E1;
5994 goto Next_E;
5995
5996 -- If the formal entity comes from a formal declaration, it was
5997 -- defaulted in the formal package, and no check is needed on it.
5998
5999 elsif Nkind_In (Original_Node (Parent (E2)),
6000 N_Formal_Object_Declaration,
6001 N_Formal_Type_Declaration)
6002 then
6003 -- If the formal is a tagged type the corresponding class-wide
6004 -- type has been generated as well, and it must be skipped.
6005
6006 if Is_Type (E2) and then Is_Tagged_Type (E2) then
6007 Next_Entity (E2);
6008 end if;
6009
6010 goto Next_E;
6011
6012 -- Ditto for defaulted formal subprograms.
6013
6014 elsif Is_Overloadable (E1)
6015 and then Nkind (Unit_Declaration_Node (E2)) in
6016 N_Formal_Subprogram_Declaration
6017 then
6018 goto Next_E;
6019
6020 elsif Is_Type (E1) then
6021
6022 -- Subtypes must statically match. E1, E2 are the local entities
6023 -- that are subtypes of the actuals. Itypes generated for other
6024 -- parameters need not be checked, the check will be performed
6025 -- on the parameters themselves.
6026
6027 -- If E2 is a formal type declaration, it is a defaulted parameter
6028 -- and needs no checking.
6029
6030 if not Is_Itype (E1) and then not Is_Itype (E2) then
6031 Check_Mismatch
6032 (not Is_Type (E2)
6033 or else Etype (E1) /= Etype (E2)
6034 or else not Subtypes_Statically_Match (E1, E2));
6035 end if;
6036
6037 elsif Ekind (E1) = E_Constant then
6038
6039 -- IN parameters must denote the same static value, or the same
6040 -- constant, or the literal null.
6041
6042 Expr1 := Expression (Parent (E1));
6043
6044 if Ekind (E2) /= E_Constant then
6045 Check_Mismatch (True);
6046 goto Next_E;
6047 else
6048 Expr2 := Expression (Parent (E2));
6049 end if;
6050
6051 if Is_OK_Static_Expression (Expr1) then
6052 if not Is_OK_Static_Expression (Expr2) then
6053 Check_Mismatch (True);
6054
6055 elsif Is_Discrete_Type (Etype (E1)) then
6056 declare
6057 V1 : constant Uint := Expr_Value (Expr1);
6058 V2 : constant Uint := Expr_Value (Expr2);
6059 begin
6060 Check_Mismatch (V1 /= V2);
6061 end;
6062
6063 elsif Is_Real_Type (Etype (E1)) then
6064 declare
6065 V1 : constant Ureal := Expr_Value_R (Expr1);
6066 V2 : constant Ureal := Expr_Value_R (Expr2);
6067 begin
6068 Check_Mismatch (V1 /= V2);
6069 end;
6070
6071 elsif Is_String_Type (Etype (E1))
6072 and then Nkind (Expr1) = N_String_Literal
6073 then
6074 if Nkind (Expr2) /= N_String_Literal then
6075 Check_Mismatch (True);
6076 else
6077 Check_Mismatch
6078 (not String_Equal (Strval (Expr1), Strval (Expr2)));
6079 end if;
6080 end if;
6081
6082 elsif Is_Entity_Name (Expr1) then
6083 if Is_Entity_Name (Expr2) then
6084 if Entity (Expr1) = Entity (Expr2) then
6085 null;
6086 else
6087 Check_Mismatch
6088 (not Same_Instantiated_Constant
6089 (Entity (Expr1), Entity (Expr2)));
6090 end if;
6091
6092 else
6093 Check_Mismatch (True);
6094 end if;
6095
6096 elsif Is_Entity_Name (Original_Node (Expr1))
6097 and then Is_Entity_Name (Expr2)
6098 and then Same_Instantiated_Constant
6099 (Entity (Original_Node (Expr1)), Entity (Expr2))
6100 then
6101 null;
6102
6103 elsif Nkind (Expr1) = N_Null then
6104 Check_Mismatch (Nkind (Expr1) /= N_Null);
6105
6106 else
6107 Check_Mismatch (True);
6108 end if;
6109
6110 elsif Ekind (E1) = E_Variable then
6111 Check_Mismatch (not Same_Instantiated_Variable (E1, E2));
6112
6113 elsif Ekind (E1) = E_Package then
6114 Check_Mismatch
6115 (Ekind (E1) /= Ekind (E2)
6116 or else Renamed_Object (E1) /= Renamed_Object (E2));
6117
6118 elsif Is_Overloadable (E1) then
6119
6120 -- Verify that the actual subprograms match. Note that actuals
6121 -- that are attributes are rewritten as subprograms. If the
6122 -- subprogram in the formal package is defaulted, no check is
6123 -- needed. Note that this can only happen in Ada 2005 when the
6124 -- formal package can be partially parameterized.
6125
6126 if Nkind (Unit_Declaration_Node (E1)) =
6127 N_Subprogram_Renaming_Declaration
6128 and then From_Default (Unit_Declaration_Node (E1))
6129 then
6130 null;
6131
6132 -- If the formal package has an "others" box association that
6133 -- covers this formal, there is no need for a check either.
6134
6135 elsif Nkind (Unit_Declaration_Node (E2)) in
6136 N_Formal_Subprogram_Declaration
6137 and then Box_Present (Unit_Declaration_Node (E2))
6138 then
6139 null;
6140
6141 -- No check needed if subprogram is a defaulted null procedure
6142
6143 elsif No (Alias (E2))
6144 and then Ekind (E2) = E_Procedure
6145 and then
6146 Null_Present (Specification (Unit_Declaration_Node (E2)))
6147 then
6148 null;
6149
6150 -- Otherwise the actual in the formal and the actual in the
6151 -- instantiation of the formal must match, up to renamings.
6152
6153 else
6154 Check_Mismatch
6155 (Ekind (E2) /= Ekind (E1)
6156 or else not Same_Instantiated_Function (E1, E2));
6157 end if;
6158
6159 else
6160 raise Program_Error;
6161 end if;
6162
6163 <<Next_E>>
6164 Prev_E1 := E1;
6165 Next_Entity (E1);
6166 Next_Entity (E2);
6167 end loop;
6168 end Check_Formal_Package_Instance;
6169
6170 ---------------------------
6171 -- Check_Formal_Packages --
6172 ---------------------------
6173
6174 procedure Check_Formal_Packages (P_Id : Entity_Id) is
6175 E : Entity_Id;
6176 Formal_P : Entity_Id;
6177 Formal_Decl : Node_Id;
6178
6179 begin
6180 -- Iterate through the declarations in the instance, looking for package
6181 -- renaming declarations that denote instances of formal packages. Stop
6182 -- when we find the renaming of the current package itself. The
6183 -- declaration for a formal package without a box is followed by an
6184 -- internal entity that repeats the instantiation.
6185
6186 E := First_Entity (P_Id);
6187 while Present (E) loop
6188 if Ekind (E) = E_Package then
6189 if Renamed_Object (E) = P_Id then
6190 exit;
6191
6192 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
6193 null;
6194
6195 else
6196 Formal_Decl := Parent (Associated_Formal_Package (E));
6197
6198 -- Nothing to check if the formal has a box or an others_clause
6199 -- (necessarily with a box).
6200
6201 if Box_Present (Formal_Decl) then
6202 null;
6203
6204 elsif Nkind (First (Generic_Associations (Formal_Decl))) =
6205 N_Others_Choice
6206 then
6207 -- The internal validating package was generated but formal
6208 -- and instance are known to be compatible.
6209
6210 Formal_P := Next_Entity (E);
6211 Remove (Unit_Declaration_Node (Formal_P));
6212
6213 else
6214 Formal_P := Next_Entity (E);
6215 Check_Formal_Package_Instance (Formal_P, E);
6216
6217 -- After checking, remove the internal validating package.
6218 -- It is only needed for semantic checks, and as it may
6219 -- contain generic formal declarations it should not reach
6220 -- gigi.
6221
6222 Remove (Unit_Declaration_Node (Formal_P));
6223 end if;
6224 end if;
6225 end if;
6226
6227 Next_Entity (E);
6228 end loop;
6229 end Check_Formal_Packages;
6230
6231 ---------------------------------
6232 -- Check_Forward_Instantiation --
6233 ---------------------------------
6234
6235 procedure Check_Forward_Instantiation (Decl : Node_Id) is
6236 S : Entity_Id;
6237 Gen_Comp : Entity_Id := Cunit_Entity (Get_Source_Unit (Decl));
6238
6239 begin
6240 -- The instantiation appears before the generic body if we are in the
6241 -- scope of the unit containing the generic, either in its spec or in
6242 -- the package body, and before the generic body.
6243
6244 if Ekind (Gen_Comp) = E_Package_Body then
6245 Gen_Comp := Spec_Entity (Gen_Comp);
6246 end if;
6247
6248 if In_Open_Scopes (Gen_Comp)
6249 and then No (Corresponding_Body (Decl))
6250 then
6251 S := Current_Scope;
6252
6253 while Present (S)
6254 and then not Is_Compilation_Unit (S)
6255 and then not Is_Child_Unit (S)
6256 loop
6257 if Ekind (S) = E_Package then
6258 Set_Has_Forward_Instantiation (S);
6259 end if;
6260
6261 S := Scope (S);
6262 end loop;
6263 end if;
6264 end Check_Forward_Instantiation;
6265
6266 ---------------------------
6267 -- Check_Generic_Actuals --
6268 ---------------------------
6269
6270 -- The visibility of the actuals may be different between the point of
6271 -- generic instantiation and the instantiation of the body.
6272
6273 procedure Check_Generic_Actuals
6274 (Instance : Entity_Id;
6275 Is_Formal_Box : Boolean)
6276 is
6277 E : Entity_Id;
6278 Astype : Entity_Id;
6279
6280 function Denotes_Previous_Actual (Typ : Entity_Id) return Boolean;
6281 -- For a formal that is an array type, the component type is often a
6282 -- previous formal in the same unit. The privacy status of the component
6283 -- type will have been examined earlier in the traversal of the
6284 -- corresponding actuals, and this status should not be modified for
6285 -- the array (sub)type itself. However, if the base type of the array
6286 -- (sub)type is private, its full view must be restored in the body to
6287 -- be consistent with subsequent index subtypes, etc.
6288 --
6289 -- To detect this case we have to rescan the list of formals, which is
6290 -- usually short enough to ignore the resulting inefficiency.
6291
6292 -----------------------------
6293 -- Denotes_Previous_Actual --
6294 -----------------------------
6295
6296 function Denotes_Previous_Actual (Typ : Entity_Id) return Boolean is
6297 Prev : Entity_Id;
6298
6299 begin
6300 Prev := First_Entity (Instance);
6301 while Present (Prev) loop
6302 if Is_Type (Prev)
6303 and then Nkind (Parent (Prev)) = N_Subtype_Declaration
6304 and then Is_Entity_Name (Subtype_Indication (Parent (Prev)))
6305 and then Entity (Subtype_Indication (Parent (Prev))) = Typ
6306 then
6307 return True;
6308
6309 elsif Prev = E then
6310 return False;
6311
6312 else
6313 Next_Entity (Prev);
6314 end if;
6315 end loop;
6316
6317 return False;
6318 end Denotes_Previous_Actual;
6319
6320 -- Start of processing for Check_Generic_Actuals
6321
6322 begin
6323 E := First_Entity (Instance);
6324 while Present (E) loop
6325 if Is_Type (E)
6326 and then Nkind (Parent (E)) = N_Subtype_Declaration
6327 and then Scope (Etype (E)) /= Instance
6328 and then Is_Entity_Name (Subtype_Indication (Parent (E)))
6329 then
6330 if Is_Array_Type (E)
6331 and then not Is_Private_Type (Etype (E))
6332 and then Denotes_Previous_Actual (Component_Type (E))
6333 then
6334 null;
6335 else
6336 Check_Private_View (Subtype_Indication (Parent (E)));
6337 end if;
6338
6339 Set_Is_Generic_Actual_Type (E, True);
6340 Set_Is_Hidden (E, False);
6341 Set_Is_Potentially_Use_Visible (E,
6342 In_Use (Instance));
6343
6344 -- We constructed the generic actual type as a subtype of the
6345 -- supplied type. This means that it normally would not inherit
6346 -- subtype specific attributes of the actual, which is wrong for
6347 -- the generic case.
6348
6349 Astype := Ancestor_Subtype (E);
6350
6351 if No (Astype) then
6352
6353 -- This can happen when E is an itype that is the full view of
6354 -- a private type completed, e.g. with a constrained array. In
6355 -- that case, use the first subtype, which will carry size
6356 -- information. The base type itself is unconstrained and will
6357 -- not carry it.
6358
6359 Astype := First_Subtype (E);
6360 end if;
6361
6362 Set_Size_Info (E, (Astype));
6363 Set_RM_Size (E, RM_Size (Astype));
6364 Set_First_Rep_Item (E, First_Rep_Item (Astype));
6365
6366 if Is_Discrete_Or_Fixed_Point_Type (E) then
6367 Set_RM_Size (E, RM_Size (Astype));
6368
6369 -- In nested instances, the base type of an access actual may
6370 -- itself be private, and need to be exchanged.
6371
6372 elsif Is_Access_Type (E)
6373 and then Is_Private_Type (Etype (E))
6374 then
6375 Check_Private_View
6376 (New_Occurrence_Of (Etype (E), Sloc (Instance)));
6377 end if;
6378
6379 elsif Ekind (E) = E_Package then
6380
6381 -- If this is the renaming for the current instance, we're done.
6382 -- Otherwise it is a formal package. If the corresponding formal
6383 -- was declared with a box, the (instantiations of the) generic
6384 -- formal part are also visible. Otherwise, ignore the entity
6385 -- created to validate the actuals.
6386
6387 if Renamed_Object (E) = Instance then
6388 exit;
6389
6390 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
6391 null;
6392
6393 -- The visibility of a formal of an enclosing generic is already
6394 -- correct.
6395
6396 elsif Denotes_Formal_Package (E) then
6397 null;
6398
6399 elsif Present (Associated_Formal_Package (E))
6400 and then not Is_Generic_Formal (E)
6401 then
6402 if Box_Present (Parent (Associated_Formal_Package (E))) then
6403 Check_Generic_Actuals (Renamed_Object (E), True);
6404
6405 else
6406 Check_Generic_Actuals (Renamed_Object (E), False);
6407 end if;
6408
6409 Set_Is_Hidden (E, False);
6410 end if;
6411
6412 -- If this is a subprogram instance (in a wrapper package) the
6413 -- actual is fully visible.
6414
6415 elsif Is_Wrapper_Package (Instance) then
6416 Set_Is_Hidden (E, False);
6417
6418 -- If the formal package is declared with a box, or if the formal
6419 -- parameter is defaulted, it is visible in the body.
6420
6421 elsif Is_Formal_Box or else Is_Visible_Formal (E) then
6422 Set_Is_Hidden (E, False);
6423 end if;
6424
6425 if Ekind (E) = E_Constant then
6426
6427 -- If the type of the actual is a private type declared in the
6428 -- enclosing scope of the generic unit, the body of the generic
6429 -- sees the full view of the type (because it has to appear in
6430 -- the corresponding package body). If the type is private now,
6431 -- exchange views to restore the proper visiblity in the instance.
6432
6433 declare
6434 Typ : constant Entity_Id := Base_Type (Etype (E));
6435 -- The type of the actual
6436
6437 Gen_Id : Entity_Id;
6438 -- The generic unit
6439
6440 Parent_Scope : Entity_Id;
6441 -- The enclosing scope of the generic unit
6442
6443 begin
6444 if Is_Wrapper_Package (Instance) then
6445 Gen_Id :=
6446 Generic_Parent
6447 (Specification
6448 (Unit_Declaration_Node
6449 (Related_Instance (Instance))));
6450 else
6451 Gen_Id :=
6452 Generic_Parent (Package_Specification (Instance));
6453 end if;
6454
6455 Parent_Scope := Scope (Gen_Id);
6456
6457 -- The exchange is only needed if the generic is defined
6458 -- within a package which is not a common ancestor of the
6459 -- scope of the instance, and is not already in scope.
6460
6461 if Is_Private_Type (Typ)
6462 and then Scope (Typ) = Parent_Scope
6463 and then Scope (Instance) /= Parent_Scope
6464 and then Ekind (Parent_Scope) = E_Package
6465 and then not Is_Child_Unit (Gen_Id)
6466 then
6467 Switch_View (Typ);
6468
6469 -- If the type of the entity is a subtype, it may also have
6470 -- to be made visible, together with the base type of its
6471 -- full view, after exchange.
6472
6473 if Is_Private_Type (Etype (E)) then
6474 Switch_View (Etype (E));
6475 Switch_View (Base_Type (Etype (E)));
6476 end if;
6477 end if;
6478 end;
6479 end if;
6480
6481 Next_Entity (E);
6482 end loop;
6483 end Check_Generic_Actuals;
6484
6485 ------------------------------
6486 -- Check_Generic_Child_Unit --
6487 ------------------------------
6488
6489 procedure Check_Generic_Child_Unit
6490 (Gen_Id : Node_Id;
6491 Parent_Installed : in out Boolean)
6492 is
6493 Loc : constant Source_Ptr := Sloc (Gen_Id);
6494 Gen_Par : Entity_Id := Empty;
6495 E : Entity_Id;
6496 Inst_Par : Entity_Id;
6497 S : Node_Id;
6498
6499 function Find_Generic_Child
6500 (Scop : Entity_Id;
6501 Id : Node_Id) return Entity_Id;
6502 -- Search generic parent for possible child unit with the given name
6503
6504 function In_Enclosing_Instance return Boolean;
6505 -- Within an instance of the parent, the child unit may be denoted by
6506 -- a simple name, or an abbreviated expanded name. Examine enclosing
6507 -- scopes to locate a possible parent instantiation.
6508
6509 ------------------------
6510 -- Find_Generic_Child --
6511 ------------------------
6512
6513 function Find_Generic_Child
6514 (Scop : Entity_Id;
6515 Id : Node_Id) return Entity_Id
6516 is
6517 E : Entity_Id;
6518
6519 begin
6520 -- If entity of name is already set, instance has already been
6521 -- resolved, e.g. in an enclosing instantiation.
6522
6523 if Present (Entity (Id)) then
6524 if Scope (Entity (Id)) = Scop then
6525 return Entity (Id);
6526 else
6527 return Empty;
6528 end if;
6529
6530 else
6531 E := First_Entity (Scop);
6532 while Present (E) loop
6533 if Chars (E) = Chars (Id)
6534 and then Is_Child_Unit (E)
6535 then
6536 if Is_Child_Unit (E)
6537 and then not Is_Visible_Lib_Unit (E)
6538 then
6539 Error_Msg_NE
6540 ("generic child unit& is not visible", Gen_Id, E);
6541 end if;
6542
6543 Set_Entity (Id, E);
6544 return E;
6545 end if;
6546
6547 Next_Entity (E);
6548 end loop;
6549
6550 return Empty;
6551 end if;
6552 end Find_Generic_Child;
6553
6554 ---------------------------
6555 -- In_Enclosing_Instance --
6556 ---------------------------
6557
6558 function In_Enclosing_Instance return Boolean is
6559 Enclosing_Instance : Node_Id;
6560 Instance_Decl : Node_Id;
6561
6562 begin
6563 -- We do not inline any call that contains instantiations, except
6564 -- for instantiations of Unchecked_Conversion, so if we are within
6565 -- an inlined body the current instance does not require parents.
6566
6567 if In_Inlined_Body then
6568 pragma Assert (Chars (Gen_Id) = Name_Unchecked_Conversion);
6569 return False;
6570 end if;
6571
6572 -- Loop to check enclosing scopes
6573
6574 Enclosing_Instance := Current_Scope;
6575 while Present (Enclosing_Instance) loop
6576 Instance_Decl := Unit_Declaration_Node (Enclosing_Instance);
6577
6578 if Ekind (Enclosing_Instance) = E_Package
6579 and then Is_Generic_Instance (Enclosing_Instance)
6580 and then Present
6581 (Generic_Parent (Specification (Instance_Decl)))
6582 then
6583 -- Check whether the generic we are looking for is a child of
6584 -- this instance.
6585
6586 E := Find_Generic_Child
6587 (Generic_Parent (Specification (Instance_Decl)), Gen_Id);
6588 exit when Present (E);
6589
6590 else
6591 E := Empty;
6592 end if;
6593
6594 Enclosing_Instance := Scope (Enclosing_Instance);
6595 end loop;
6596
6597 if No (E) then
6598
6599 -- Not a child unit
6600
6601 Analyze (Gen_Id);
6602 return False;
6603
6604 else
6605 Rewrite (Gen_Id,
6606 Make_Expanded_Name (Loc,
6607 Chars => Chars (E),
6608 Prefix => New_Occurrence_Of (Enclosing_Instance, Loc),
6609 Selector_Name => New_Occurrence_Of (E, Loc)));
6610
6611 Set_Entity (Gen_Id, E);
6612 Set_Etype (Gen_Id, Etype (E));
6613 Parent_Installed := False; -- Already in scope.
6614 return True;
6615 end if;
6616 end In_Enclosing_Instance;
6617
6618 -- Start of processing for Check_Generic_Child_Unit
6619
6620 begin
6621 -- If the name of the generic is given by a selected component, it may
6622 -- be the name of a generic child unit, and the prefix is the name of an
6623 -- instance of the parent, in which case the child unit must be visible.
6624 -- If this instance is not in scope, it must be placed there and removed
6625 -- after instantiation, because what is being instantiated is not the
6626 -- original child, but the corresponding child present in the instance
6627 -- of the parent.
6628
6629 -- If the child is instantiated within the parent, it can be given by
6630 -- a simple name. In this case the instance is already in scope, but
6631 -- the child generic must be recovered from the generic parent as well.
6632
6633 if Nkind (Gen_Id) = N_Selected_Component then
6634 S := Selector_Name (Gen_Id);
6635 Analyze (Prefix (Gen_Id));
6636 Inst_Par := Entity (Prefix (Gen_Id));
6637
6638 if Ekind (Inst_Par) = E_Package
6639 and then Present (Renamed_Object (Inst_Par))
6640 then
6641 Inst_Par := Renamed_Object (Inst_Par);
6642 end if;
6643
6644 if Ekind (Inst_Par) = E_Package then
6645 if Nkind (Parent (Inst_Par)) = N_Package_Specification then
6646 Gen_Par := Generic_Parent (Parent (Inst_Par));
6647
6648 elsif Nkind (Parent (Inst_Par)) = N_Defining_Program_Unit_Name
6649 and then
6650 Nkind (Parent (Parent (Inst_Par))) = N_Package_Specification
6651 then
6652 Gen_Par := Generic_Parent (Parent (Parent (Inst_Par)));
6653 end if;
6654
6655 elsif Ekind (Inst_Par) = E_Generic_Package
6656 and then Nkind (Parent (Gen_Id)) = N_Formal_Package_Declaration
6657 then
6658 -- A formal package may be a real child package, and not the
6659 -- implicit instance within a parent. In this case the child is
6660 -- not visible and has to be retrieved explicitly as well.
6661
6662 Gen_Par := Inst_Par;
6663 end if;
6664
6665 if Present (Gen_Par) then
6666
6667 -- The prefix denotes an instantiation. The entity itself may be a
6668 -- nested generic, or a child unit.
6669
6670 E := Find_Generic_Child (Gen_Par, S);
6671
6672 if Present (E) then
6673 Change_Selected_Component_To_Expanded_Name (Gen_Id);
6674 Set_Entity (Gen_Id, E);
6675 Set_Etype (Gen_Id, Etype (E));
6676 Set_Entity (S, E);
6677 Set_Etype (S, Etype (E));
6678
6679 -- Indicate that this is a reference to the parent
6680
6681 if In_Extended_Main_Source_Unit (Gen_Id) then
6682 Set_Is_Instantiated (Inst_Par);
6683 end if;
6684
6685 -- A common mistake is to replicate the naming scheme of a
6686 -- hierarchy by instantiating a generic child directly, rather
6687 -- than the implicit child in a parent instance:
6688
6689 -- generic .. package Gpar is ..
6690 -- generic .. package Gpar.Child is ..
6691 -- package Par is new Gpar ();
6692
6693 -- with Gpar.Child;
6694 -- package Par.Child is new Gpar.Child ();
6695 -- rather than Par.Child
6696
6697 -- In this case the instantiation is within Par, which is an
6698 -- instance, but Gpar does not denote Par because we are not IN
6699 -- the instance of Gpar, so this is illegal. The test below
6700 -- recognizes this particular case.
6701
6702 if Is_Child_Unit (E)
6703 and then not Comes_From_Source (Entity (Prefix (Gen_Id)))
6704 and then (not In_Instance
6705 or else Nkind (Parent (Parent (Gen_Id))) =
6706 N_Compilation_Unit)
6707 then
6708 Error_Msg_N
6709 ("prefix of generic child unit must be instance of parent",
6710 Gen_Id);
6711 end if;
6712
6713 if not In_Open_Scopes (Inst_Par)
6714 and then Nkind (Parent (Gen_Id)) not in
6715 N_Generic_Renaming_Declaration
6716 then
6717 Install_Parent (Inst_Par);
6718 Parent_Installed := True;
6719
6720 elsif In_Open_Scopes (Inst_Par) then
6721
6722 -- If the parent is already installed, install the actuals
6723 -- for its formal packages. This is necessary when the child
6724 -- instance is a child of the parent instance: in this case,
6725 -- the parent is placed on the scope stack but the formal
6726 -- packages are not made visible.
6727
6728 Install_Formal_Packages (Inst_Par);
6729 end if;
6730
6731 else
6732 -- If the generic parent does not contain an entity that
6733 -- corresponds to the selector, the instance doesn't either.
6734 -- Analyzing the node will yield the appropriate error message.
6735 -- If the entity is not a child unit, then it is an inner
6736 -- generic in the parent.
6737
6738 Analyze (Gen_Id);
6739 end if;
6740
6741 else
6742 Analyze (Gen_Id);
6743
6744 if Is_Child_Unit (Entity (Gen_Id))
6745 and then
6746 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
6747 and then not In_Open_Scopes (Inst_Par)
6748 then
6749 Install_Parent (Inst_Par);
6750 Parent_Installed := True;
6751
6752 -- The generic unit may be the renaming of the implicit child
6753 -- present in an instance. In that case the parent instance is
6754 -- obtained from the name of the renamed entity.
6755
6756 elsif Ekind (Entity (Gen_Id)) = E_Generic_Package
6757 and then Present (Renamed_Entity (Entity (Gen_Id)))
6758 and then Is_Child_Unit (Renamed_Entity (Entity (Gen_Id)))
6759 then
6760 declare
6761 Renamed_Package : constant Node_Id :=
6762 Name (Parent (Entity (Gen_Id)));
6763 begin
6764 if Nkind (Renamed_Package) = N_Expanded_Name then
6765 Inst_Par := Entity (Prefix (Renamed_Package));
6766 Install_Parent (Inst_Par);
6767 Parent_Installed := True;
6768 end if;
6769 end;
6770 end if;
6771 end if;
6772
6773 elsif Nkind (Gen_Id) = N_Expanded_Name then
6774
6775 -- Entity already present, analyze prefix, whose meaning may be an
6776 -- instance in the current context. If it is an instance of a
6777 -- relative within another, the proper parent may still have to be
6778 -- installed, if they are not of the same generation.
6779
6780 Analyze (Prefix (Gen_Id));
6781
6782 -- Prevent cascaded errors
6783
6784 if Etype (Prefix (Gen_Id)) = Any_Type then
6785 return;
6786 end if;
6787
6788 -- In the unlikely case that a local declaration hides the name of
6789 -- the parent package, locate it on the homonym chain. If the context
6790 -- is an instance of the parent, the renaming entity is flagged as
6791 -- such.
6792
6793 Inst_Par := Entity (Prefix (Gen_Id));
6794 while Present (Inst_Par)
6795 and then not Is_Package_Or_Generic_Package (Inst_Par)
6796 loop
6797 Inst_Par := Homonym (Inst_Par);
6798 end loop;
6799
6800 pragma Assert (Present (Inst_Par));
6801 Set_Entity (Prefix (Gen_Id), Inst_Par);
6802
6803 if In_Enclosing_Instance then
6804 null;
6805
6806 elsif Present (Entity (Gen_Id))
6807 and then Is_Child_Unit (Entity (Gen_Id))
6808 and then not In_Open_Scopes (Inst_Par)
6809 then
6810 Install_Parent (Inst_Par);
6811 Parent_Installed := True;
6812 end if;
6813
6814 elsif In_Enclosing_Instance then
6815
6816 -- The child unit is found in some enclosing scope
6817
6818 null;
6819
6820 else
6821 Analyze (Gen_Id);
6822
6823 -- If this is the renaming of the implicit child in a parent
6824 -- instance, recover the parent name and install it.
6825
6826 if Is_Entity_Name (Gen_Id) then
6827 E := Entity (Gen_Id);
6828
6829 if Is_Generic_Unit (E)
6830 and then Nkind (Parent (E)) in N_Generic_Renaming_Declaration
6831 and then Is_Child_Unit (Renamed_Object (E))
6832 and then Is_Generic_Unit (Scope (Renamed_Object (E)))
6833 and then Nkind (Name (Parent (E))) = N_Expanded_Name
6834 then
6835 Rewrite (Gen_Id, New_Copy_Tree (Name (Parent (E))));
6836 Inst_Par := Entity (Prefix (Gen_Id));
6837
6838 if not In_Open_Scopes (Inst_Par) then
6839 Install_Parent (Inst_Par);
6840 Parent_Installed := True;
6841 end if;
6842
6843 -- If it is a child unit of a non-generic parent, it may be
6844 -- use-visible and given by a direct name. Install parent as
6845 -- for other cases.
6846
6847 elsif Is_Generic_Unit (E)
6848 and then Is_Child_Unit (E)
6849 and then
6850 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
6851 and then not Is_Generic_Unit (Scope (E))
6852 then
6853 if not In_Open_Scopes (Scope (E)) then
6854 Install_Parent (Scope (E));
6855 Parent_Installed := True;
6856 end if;
6857 end if;
6858 end if;
6859 end if;
6860 end Check_Generic_Child_Unit;
6861
6862 -----------------------------
6863 -- Check_Hidden_Child_Unit --
6864 -----------------------------
6865
6866 procedure Check_Hidden_Child_Unit
6867 (N : Node_Id;
6868 Gen_Unit : Entity_Id;
6869 Act_Decl_Id : Entity_Id)
6870 is
6871 Gen_Id : constant Node_Id := Name (N);
6872
6873 begin
6874 if Is_Child_Unit (Gen_Unit)
6875 and then Is_Child_Unit (Act_Decl_Id)
6876 and then Nkind (Gen_Id) = N_Expanded_Name
6877 and then Entity (Prefix (Gen_Id)) = Scope (Act_Decl_Id)
6878 and then Chars (Gen_Unit) = Chars (Act_Decl_Id)
6879 then
6880 Error_Msg_Node_2 := Scope (Act_Decl_Id);
6881 Error_Msg_NE
6882 ("generic unit & is implicitly declared in &",
6883 Defining_Unit_Name (N), Gen_Unit);
6884 Error_Msg_N ("\instance must have different name",
6885 Defining_Unit_Name (N));
6886 end if;
6887 end Check_Hidden_Child_Unit;
6888
6889 ------------------------
6890 -- Check_Private_View --
6891 ------------------------
6892
6893 procedure Check_Private_View (N : Node_Id) is
6894 T : constant Entity_Id := Etype (N);
6895 BT : Entity_Id;
6896
6897 begin
6898 -- Exchange views if the type was not private in the generic but is
6899 -- private at the point of instantiation. Do not exchange views if
6900 -- the scope of the type is in scope. This can happen if both generic
6901 -- and instance are sibling units, or if type is defined in a parent.
6902 -- In this case the visibility of the type will be correct for all
6903 -- semantic checks.
6904
6905 if Present (T) then
6906 BT := Base_Type (T);
6907
6908 if Is_Private_Type (T)
6909 and then not Has_Private_View (N)
6910 and then Present (Full_View (T))
6911 and then not In_Open_Scopes (Scope (T))
6912 then
6913 -- In the generic, the full type was visible. Save the private
6914 -- entity, for subsequent exchange.
6915
6916 Switch_View (T);
6917
6918 elsif Has_Private_View (N)
6919 and then not Is_Private_Type (T)
6920 and then not Has_Been_Exchanged (T)
6921 and then Etype (Get_Associated_Node (N)) /= T
6922 then
6923 -- Only the private declaration was visible in the generic. If
6924 -- the type appears in a subtype declaration, the subtype in the
6925 -- instance must have a view compatible with that of its parent,
6926 -- which must be exchanged (see corresponding code in Restore_
6927 -- Private_Views). Otherwise, if the type is defined in a parent
6928 -- unit, leave full visibility within instance, which is safe.
6929
6930 if In_Open_Scopes (Scope (Base_Type (T)))
6931 and then not Is_Private_Type (Base_Type (T))
6932 and then Comes_From_Source (Base_Type (T))
6933 then
6934 null;
6935
6936 elsif Nkind (Parent (N)) = N_Subtype_Declaration
6937 or else not In_Private_Part (Scope (Base_Type (T)))
6938 then
6939 Prepend_Elmt (T, Exchanged_Views);
6940 Exchange_Declarations (Etype (Get_Associated_Node (N)));
6941 end if;
6942
6943 -- For composite types with inconsistent representation exchange
6944 -- component types accordingly.
6945
6946 elsif Is_Access_Type (T)
6947 and then Is_Private_Type (Designated_Type (T))
6948 and then not Has_Private_View (N)
6949 and then Present (Full_View (Designated_Type (T)))
6950 then
6951 Switch_View (Designated_Type (T));
6952
6953 elsif Is_Array_Type (T) then
6954 if Is_Private_Type (Component_Type (T))
6955 and then not Has_Private_View (N)
6956 and then Present (Full_View (Component_Type (T)))
6957 then
6958 Switch_View (Component_Type (T));
6959 end if;
6960
6961 -- The normal exchange mechanism relies on the setting of a
6962 -- flag on the reference in the generic. However, an additional
6963 -- mechanism is needed for types that are not explicitly
6964 -- mentioned in the generic, but may be needed in expanded code
6965 -- in the instance. This includes component types of arrays and
6966 -- designated types of access types. This processing must also
6967 -- include the index types of arrays which we take care of here.
6968
6969 declare
6970 Indx : Node_Id;
6971 Typ : Entity_Id;
6972
6973 begin
6974 Indx := First_Index (T);
6975 while Present (Indx) loop
6976 Typ := Base_Type (Etype (Indx));
6977
6978 if Is_Private_Type (Typ)
6979 and then Present (Full_View (Typ))
6980 then
6981 Switch_View (Typ);
6982 end if;
6983
6984 Next_Index (Indx);
6985 end loop;
6986 end;
6987
6988 elsif Is_Private_Type (T)
6989 and then Present (Full_View (T))
6990 and then Is_Array_Type (Full_View (T))
6991 and then Is_Private_Type (Component_Type (Full_View (T)))
6992 then
6993 Switch_View (T);
6994
6995 -- Finally, a non-private subtype may have a private base type, which
6996 -- must be exchanged for consistency. This can happen when a package
6997 -- body is instantiated, when the scope stack is empty but in fact
6998 -- the subtype and the base type are declared in an enclosing scope.
6999
7000 -- Note that in this case we introduce an inconsistency in the view
7001 -- set, because we switch the base type BT, but there could be some
7002 -- private dependent subtypes of BT which remain unswitched. Such
7003 -- subtypes might need to be switched at a later point (see specific
7004 -- provision for that case in Switch_View).
7005
7006 elsif not Is_Private_Type (T)
7007 and then not Has_Private_View (N)
7008 and then Is_Private_Type (BT)
7009 and then Present (Full_View (BT))
7010 and then not Is_Generic_Type (BT)
7011 and then not In_Open_Scopes (BT)
7012 then
7013 Prepend_Elmt (Full_View (BT), Exchanged_Views);
7014 Exchange_Declarations (BT);
7015 end if;
7016 end if;
7017 end Check_Private_View;
7018
7019 -----------------------------
7020 -- Check_Hidden_Primitives --
7021 -----------------------------
7022
7023 function Check_Hidden_Primitives (Assoc_List : List_Id) return Elist_Id is
7024 Actual : Node_Id;
7025 Gen_T : Entity_Id;
7026 Result : Elist_Id := No_Elist;
7027
7028 begin
7029 if No (Assoc_List) then
7030 return No_Elist;
7031 end if;
7032
7033 -- Traverse the list of associations between formals and actuals
7034 -- searching for renamings of tagged types
7035
7036 Actual := First (Assoc_List);
7037 while Present (Actual) loop
7038 if Nkind (Actual) = N_Subtype_Declaration then
7039 Gen_T := Generic_Parent_Type (Actual);
7040
7041 if Present (Gen_T) and then Is_Tagged_Type (Gen_T) then
7042
7043 -- Traverse the list of primitives of the actual types
7044 -- searching for hidden primitives that are visible in the
7045 -- corresponding generic formal; leave them visible and
7046 -- append them to Result to restore their decoration later.
7047
7048 Install_Hidden_Primitives
7049 (Prims_List => Result,
7050 Gen_T => Gen_T,
7051 Act_T => Entity (Subtype_Indication (Actual)));
7052 end if;
7053 end if;
7054
7055 Next (Actual);
7056 end loop;
7057
7058 return Result;
7059 end Check_Hidden_Primitives;
7060
7061 --------------------------
7062 -- Contains_Instance_Of --
7063 --------------------------
7064
7065 function Contains_Instance_Of
7066 (Inner : Entity_Id;
7067 Outer : Entity_Id;
7068 N : Node_Id) return Boolean
7069 is
7070 Elmt : Elmt_Id;
7071 Scop : Entity_Id;
7072
7073 begin
7074 Scop := Outer;
7075
7076 -- Verify that there are no circular instantiations. We check whether
7077 -- the unit contains an instance of the current scope or some enclosing
7078 -- scope (in case one of the instances appears in a subunit). Longer
7079 -- circularities involving subunits might seem too pathological to
7080 -- consider, but they were not too pathological for the authors of
7081 -- DEC bc30vsq, so we loop over all enclosing scopes, and mark all
7082 -- enclosing generic scopes as containing an instance.
7083
7084 loop
7085 -- Within a generic subprogram body, the scope is not generic, to
7086 -- allow for recursive subprograms. Use the declaration to determine
7087 -- whether this is a generic unit.
7088
7089 if Ekind (Scop) = E_Generic_Package
7090 or else (Is_Subprogram (Scop)
7091 and then Nkind (Unit_Declaration_Node (Scop)) =
7092 N_Generic_Subprogram_Declaration)
7093 then
7094 Elmt := First_Elmt (Inner_Instances (Inner));
7095
7096 while Present (Elmt) loop
7097 if Node (Elmt) = Scop then
7098 Error_Msg_Node_2 := Inner;
7099 Error_Msg_NE
7100 ("circular Instantiation: & instantiated within &!",
7101 N, Scop);
7102 return True;
7103
7104 elsif Node (Elmt) = Inner then
7105 return True;
7106
7107 elsif Contains_Instance_Of (Node (Elmt), Scop, N) then
7108 Error_Msg_Node_2 := Inner;
7109 Error_Msg_NE
7110 ("circular Instantiation: & instantiated within &!",
7111 N, Node (Elmt));
7112 return True;
7113 end if;
7114
7115 Next_Elmt (Elmt);
7116 end loop;
7117
7118 -- Indicate that Inner is being instantiated within Scop
7119
7120 Append_Elmt (Inner, Inner_Instances (Scop));
7121 end if;
7122
7123 if Scop = Standard_Standard then
7124 exit;
7125 else
7126 Scop := Scope (Scop);
7127 end if;
7128 end loop;
7129
7130 return False;
7131 end Contains_Instance_Of;
7132
7133 -----------------------
7134 -- Copy_Generic_Node --
7135 -----------------------
7136
7137 function Copy_Generic_Node
7138 (N : Node_Id;
7139 Parent_Id : Node_Id;
7140 Instantiating : Boolean) return Node_Id
7141 is
7142 Ent : Entity_Id;
7143 New_N : Node_Id;
7144
7145 function Copy_Generic_Descendant (D : Union_Id) return Union_Id;
7146 -- Check the given value of one of the Fields referenced by the current
7147 -- node to determine whether to copy it recursively. The field may hold
7148 -- a Node_Id, a List_Id, or an Elist_Id, or a plain value (Sloc, Uint,
7149 -- Char) in which case it need not be copied.
7150
7151 procedure Copy_Descendants;
7152 -- Common utility for various nodes
7153
7154 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id;
7155 -- Make copy of element list
7156
7157 function Copy_Generic_List
7158 (L : List_Id;
7159 Parent_Id : Node_Id) return List_Id;
7160 -- Apply Copy_Node recursively to the members of a node list
7161
7162 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean;
7163 -- True if an identifier is part of the defining program unit name of
7164 -- a child unit. The entity of such an identifier must be kept (for
7165 -- ASIS use) even though as the name of an enclosing generic it would
7166 -- otherwise not be preserved in the generic tree.
7167
7168 ----------------------
7169 -- Copy_Descendants --
7170 ----------------------
7171
7172 procedure Copy_Descendants is
7173 use Atree.Unchecked_Access;
7174 -- This code section is part of the implementation of an untyped
7175 -- tree traversal, so it needs direct access to node fields.
7176
7177 begin
7178 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
7179 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
7180 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
7181 Set_Field4 (New_N, Copy_Generic_Descendant (Field4 (N)));
7182 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
7183 end Copy_Descendants;
7184
7185 -----------------------------
7186 -- Copy_Generic_Descendant --
7187 -----------------------------
7188
7189 function Copy_Generic_Descendant (D : Union_Id) return Union_Id is
7190 begin
7191 if D = Union_Id (Empty) then
7192 return D;
7193
7194 elsif D in Node_Range then
7195 return Union_Id
7196 (Copy_Generic_Node (Node_Id (D), New_N, Instantiating));
7197
7198 elsif D in List_Range then
7199 return Union_Id (Copy_Generic_List (List_Id (D), New_N));
7200
7201 elsif D in Elist_Range then
7202 return Union_Id (Copy_Generic_Elist (Elist_Id (D)));
7203
7204 -- Nothing else is copyable (e.g. Uint values), return as is
7205
7206 else
7207 return D;
7208 end if;
7209 end Copy_Generic_Descendant;
7210
7211 ------------------------
7212 -- Copy_Generic_Elist --
7213 ------------------------
7214
7215 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id is
7216 M : Elmt_Id;
7217 L : Elist_Id;
7218
7219 begin
7220 if Present (E) then
7221 L := New_Elmt_List;
7222 M := First_Elmt (E);
7223 while Present (M) loop
7224 Append_Elmt
7225 (Copy_Generic_Node (Node (M), Empty, Instantiating), L);
7226 Next_Elmt (M);
7227 end loop;
7228
7229 return L;
7230
7231 else
7232 return No_Elist;
7233 end if;
7234 end Copy_Generic_Elist;
7235
7236 -----------------------
7237 -- Copy_Generic_List --
7238 -----------------------
7239
7240 function Copy_Generic_List
7241 (L : List_Id;
7242 Parent_Id : Node_Id) return List_Id
7243 is
7244 N : Node_Id;
7245 New_L : List_Id;
7246
7247 begin
7248 if Present (L) then
7249 New_L := New_List;
7250 Set_Parent (New_L, Parent_Id);
7251
7252 N := First (L);
7253 while Present (N) loop
7254 Append (Copy_Generic_Node (N, Empty, Instantiating), New_L);
7255 Next (N);
7256 end loop;
7257
7258 return New_L;
7259
7260 else
7261 return No_List;
7262 end if;
7263 end Copy_Generic_List;
7264
7265 ---------------------------
7266 -- In_Defining_Unit_Name --
7267 ---------------------------
7268
7269 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean is
7270 begin
7271 return
7272 Present (Parent (Nam))
7273 and then (Nkind (Parent (Nam)) = N_Defining_Program_Unit_Name
7274 or else
7275 (Nkind (Parent (Nam)) = N_Expanded_Name
7276 and then In_Defining_Unit_Name (Parent (Nam))));
7277 end In_Defining_Unit_Name;
7278
7279 -- Start of processing for Copy_Generic_Node
7280
7281 begin
7282 if N = Empty then
7283 return N;
7284 end if;
7285
7286 New_N := New_Copy (N);
7287
7288 -- Copy aspects if present
7289
7290 if Has_Aspects (N) then
7291 Set_Has_Aspects (New_N, False);
7292 Set_Aspect_Specifications
7293 (New_N, Copy_Generic_List (Aspect_Specifications (N), Parent_Id));
7294 end if;
7295
7296 if Instantiating then
7297 Adjust_Instantiation_Sloc (New_N, S_Adjustment);
7298 end if;
7299
7300 if not Is_List_Member (N) then
7301 Set_Parent (New_N, Parent_Id);
7302 end if;
7303
7304 -- Special casing for identifiers and other entity names and operators
7305
7306 if Nkind_In (New_N, N_Character_Literal,
7307 N_Expanded_Name,
7308 N_Identifier,
7309 N_Operator_Symbol)
7310 or else Nkind (New_N) in N_Op
7311 then
7312 if not Instantiating then
7313
7314 -- Link both nodes in order to assign subsequently the entity of
7315 -- the copy to the original node, in case this is a global
7316 -- reference.
7317
7318 Set_Associated_Node (N, New_N);
7319
7320 -- If we are within an instantiation, this is a nested generic
7321 -- that has already been analyzed at the point of definition.
7322 -- We must preserve references that were global to the enclosing
7323 -- parent at that point. Other occurrences, whether global or
7324 -- local to the current generic, must be resolved anew, so we
7325 -- reset the entity in the generic copy. A global reference has a
7326 -- smaller depth than the parent, or else the same depth in case
7327 -- both are distinct compilation units.
7328
7329 -- A child unit is implicitly declared within the enclosing parent
7330 -- but is in fact global to it, and must be preserved.
7331
7332 -- It is also possible for Current_Instantiated_Parent to be
7333 -- defined, and for this not to be a nested generic, namely if
7334 -- the unit is loaded through Rtsfind. In that case, the entity of
7335 -- New_N is only a link to the associated node, and not a defining
7336 -- occurrence.
7337
7338 -- The entities for parent units in the defining_program_unit of a
7339 -- generic child unit are established when the context of the unit
7340 -- is first analyzed, before the generic copy is made. They are
7341 -- preserved in the copy for use in ASIS queries.
7342
7343 Ent := Entity (New_N);
7344
7345 if No (Current_Instantiated_Parent.Gen_Id) then
7346 if No (Ent)
7347 or else Nkind (Ent) /= N_Defining_Identifier
7348 or else not In_Defining_Unit_Name (N)
7349 then
7350 Set_Associated_Node (New_N, Empty);
7351 end if;
7352
7353 elsif No (Ent)
7354 or else
7355 not Nkind_In (Ent, N_Defining_Identifier,
7356 N_Defining_Character_Literal,
7357 N_Defining_Operator_Symbol)
7358 or else No (Scope (Ent))
7359 or else
7360 (Scope (Ent) = Current_Instantiated_Parent.Gen_Id
7361 and then not Is_Child_Unit (Ent))
7362 or else
7363 (Scope_Depth (Scope (Ent)) >
7364 Scope_Depth (Current_Instantiated_Parent.Gen_Id)
7365 and then
7366 Get_Source_Unit (Ent) =
7367 Get_Source_Unit (Current_Instantiated_Parent.Gen_Id))
7368 then
7369 Set_Associated_Node (New_N, Empty);
7370 end if;
7371
7372 -- Case of instantiating identifier or some other name or operator
7373
7374 else
7375 -- If the associated node is still defined, the entity in it
7376 -- is global, and must be copied to the instance. If this copy
7377 -- is being made for a body to inline, it is applied to an
7378 -- instantiated tree, and the entity is already present and
7379 -- must be also preserved.
7380
7381 declare
7382 Assoc : constant Node_Id := Get_Associated_Node (N);
7383
7384 begin
7385 if Present (Assoc) then
7386 if Nkind (Assoc) = Nkind (N) then
7387 Set_Entity (New_N, Entity (Assoc));
7388 Check_Private_View (N);
7389
7390 -- The node is a reference to a global type and acts as the
7391 -- subtype mark of a qualified expression created in order
7392 -- to aid resolution of accidental overloading in instances.
7393 -- Since N is a reference to a type, the Associated_Node of
7394 -- N denotes an entity rather than another identifier. See
7395 -- Qualify_Universal_Operands for details.
7396
7397 elsif Nkind (N) = N_Identifier
7398 and then Nkind (Parent (N)) = N_Qualified_Expression
7399 and then Subtype_Mark (Parent (N)) = N
7400 and then Is_Qualified_Universal_Literal (Parent (N))
7401 then
7402 Set_Entity (New_N, Assoc);
7403
7404 -- The name in the call may be a selected component if the
7405 -- call has not been analyzed yet, as may be the case for
7406 -- pre/post conditions in a generic unit.
7407
7408 elsif Nkind (Assoc) = N_Function_Call
7409 and then Is_Entity_Name (Name (Assoc))
7410 then
7411 Set_Entity (New_N, Entity (Name (Assoc)));
7412
7413 elsif Nkind_In (Assoc, N_Defining_Identifier,
7414 N_Defining_Character_Literal,
7415 N_Defining_Operator_Symbol)
7416 and then Expander_Active
7417 then
7418 -- Inlining case: we are copying a tree that contains
7419 -- global entities, which are preserved in the copy to be
7420 -- used for subsequent inlining.
7421
7422 null;
7423
7424 else
7425 Set_Entity (New_N, Empty);
7426 end if;
7427 end if;
7428 end;
7429 end if;
7430
7431 -- For expanded name, we must copy the Prefix and Selector_Name
7432
7433 if Nkind (N) = N_Expanded_Name then
7434 Set_Prefix
7435 (New_N, Copy_Generic_Node (Prefix (N), New_N, Instantiating));
7436
7437 Set_Selector_Name (New_N,
7438 Copy_Generic_Node (Selector_Name (N), New_N, Instantiating));
7439
7440 -- For operators, we must copy the right operand
7441
7442 elsif Nkind (N) in N_Op then
7443 Set_Right_Opnd (New_N,
7444 Copy_Generic_Node (Right_Opnd (N), New_N, Instantiating));
7445
7446 -- And for binary operators, the left operand as well
7447
7448 if Nkind (N) in N_Binary_Op then
7449 Set_Left_Opnd (New_N,
7450 Copy_Generic_Node (Left_Opnd (N), New_N, Instantiating));
7451 end if;
7452 end if;
7453
7454 -- Establish a link between an entity from the generic template and the
7455 -- corresponding entity in the generic copy to be analyzed.
7456
7457 elsif Nkind (N) in N_Entity then
7458 if not Instantiating then
7459 Set_Associated_Entity (N, New_N);
7460 end if;
7461
7462 -- Clear any existing link the copy may inherit from the replicated
7463 -- generic template entity.
7464
7465 Set_Associated_Entity (New_N, Empty);
7466
7467 -- Special casing for stubs
7468
7469 elsif Nkind (N) in N_Body_Stub then
7470
7471 -- In any case, we must copy the specification or defining
7472 -- identifier as appropriate.
7473
7474 if Nkind (N) = N_Subprogram_Body_Stub then
7475 Set_Specification (New_N,
7476 Copy_Generic_Node (Specification (N), New_N, Instantiating));
7477
7478 else
7479 Set_Defining_Identifier (New_N,
7480 Copy_Generic_Node
7481 (Defining_Identifier (N), New_N, Instantiating));
7482 end if;
7483
7484 -- If we are not instantiating, then this is where we load and
7485 -- analyze subunits, i.e. at the point where the stub occurs. A
7486 -- more permissive system might defer this analysis to the point
7487 -- of instantiation, but this seems too complicated for now.
7488
7489 if not Instantiating then
7490 declare
7491 Subunit_Name : constant Unit_Name_Type := Get_Unit_Name (N);
7492 Subunit : Node_Id;
7493 Unum : Unit_Number_Type;
7494 New_Body : Node_Id;
7495
7496 begin
7497 -- Make sure that, if it is a subunit of the main unit that is
7498 -- preprocessed and if -gnateG is specified, the preprocessed
7499 -- file will be written.
7500
7501 Lib.Analysing_Subunit_Of_Main :=
7502 Lib.In_Extended_Main_Source_Unit (N);
7503 Unum :=
7504 Load_Unit
7505 (Load_Name => Subunit_Name,
7506 Required => False,
7507 Subunit => True,
7508 Error_Node => N);
7509 Lib.Analysing_Subunit_Of_Main := False;
7510
7511 -- If the proper body is not found, a warning message will be
7512 -- emitted when analyzing the stub, or later at the point of
7513 -- instantiation. Here we just leave the stub as is.
7514
7515 if Unum = No_Unit then
7516 Subunits_Missing := True;
7517 goto Subunit_Not_Found;
7518 end if;
7519
7520 Subunit := Cunit (Unum);
7521
7522 if Nkind (Unit (Subunit)) /= N_Subunit then
7523 Error_Msg_N
7524 ("found child unit instead of expected SEPARATE subunit",
7525 Subunit);
7526 Error_Msg_Sloc := Sloc (N);
7527 Error_Msg_N ("\to complete stub #", Subunit);
7528 goto Subunit_Not_Found;
7529 end if;
7530
7531 -- We must create a generic copy of the subunit, in order to
7532 -- perform semantic analysis on it, and we must replace the
7533 -- stub in the original generic unit with the subunit, in order
7534 -- to preserve non-local references within.
7535
7536 -- Only the proper body needs to be copied. Library_Unit and
7537 -- context clause are simply inherited by the generic copy.
7538 -- Note that the copy (which may be recursive if there are
7539 -- nested subunits) must be done first, before attaching it to
7540 -- the enclosing generic.
7541
7542 New_Body :=
7543 Copy_Generic_Node
7544 (Proper_Body (Unit (Subunit)),
7545 Empty, Instantiating => False);
7546
7547 -- Now place the original proper body in the original generic
7548 -- unit. This is a body, not a compilation unit.
7549
7550 Rewrite (N, Proper_Body (Unit (Subunit)));
7551 Set_Is_Compilation_Unit (Defining_Entity (N), False);
7552 Set_Was_Originally_Stub (N);
7553
7554 -- Finally replace the body of the subunit with its copy, and
7555 -- make this new subunit into the library unit of the generic
7556 -- copy, which does not have stubs any longer.
7557
7558 Set_Proper_Body (Unit (Subunit), New_Body);
7559 Set_Library_Unit (New_N, Subunit);
7560 Inherit_Context (Unit (Subunit), N);
7561 end;
7562
7563 -- If we are instantiating, this must be an error case, since
7564 -- otherwise we would have replaced the stub node by the proper body
7565 -- that corresponds. So just ignore it in the copy (i.e. we have
7566 -- copied it, and that is good enough).
7567
7568 else
7569 null;
7570 end if;
7571
7572 <<Subunit_Not_Found>> null;
7573
7574 -- If the node is a compilation unit, it is the subunit of a stub, which
7575 -- has been loaded already (see code below). In this case, the library
7576 -- unit field of N points to the parent unit (which is a compilation
7577 -- unit) and need not (and cannot) be copied.
7578
7579 -- When the proper body of the stub is analyzed, the library_unit link
7580 -- is used to establish the proper context (see sem_ch10).
7581
7582 -- The other fields of a compilation unit are copied as usual
7583
7584 elsif Nkind (N) = N_Compilation_Unit then
7585
7586 -- This code can only be executed when not instantiating, because in
7587 -- the copy made for an instantiation, the compilation unit node has
7588 -- disappeared at the point that a stub is replaced by its proper
7589 -- body.
7590
7591 pragma Assert (not Instantiating);
7592
7593 Set_Context_Items (New_N,
7594 Copy_Generic_List (Context_Items (N), New_N));
7595
7596 Set_Unit (New_N,
7597 Copy_Generic_Node (Unit (N), New_N, False));
7598
7599 Set_First_Inlined_Subprogram (New_N,
7600 Copy_Generic_Node
7601 (First_Inlined_Subprogram (N), New_N, False));
7602
7603 Set_Aux_Decls_Node (New_N,
7604 Copy_Generic_Node (Aux_Decls_Node (N), New_N, False));
7605
7606 -- For an assignment node, the assignment is known to be semantically
7607 -- legal if we are instantiating the template. This avoids incorrect
7608 -- diagnostics in generated code.
7609
7610 elsif Nkind (N) = N_Assignment_Statement then
7611
7612 -- Copy name and expression fields in usual manner
7613
7614 Set_Name (New_N,
7615 Copy_Generic_Node (Name (N), New_N, Instantiating));
7616
7617 Set_Expression (New_N,
7618 Copy_Generic_Node (Expression (N), New_N, Instantiating));
7619
7620 if Instantiating then
7621 Set_Assignment_OK (Name (New_N), True);
7622 end if;
7623
7624 elsif Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
7625 if not Instantiating then
7626 Set_Associated_Node (N, New_N);
7627
7628 else
7629 if Present (Get_Associated_Node (N))
7630 and then Nkind (Get_Associated_Node (N)) = Nkind (N)
7631 then
7632 -- In the generic the aggregate has some composite type. If at
7633 -- the point of instantiation the type has a private view,
7634 -- install the full view (and that of its ancestors, if any).
7635
7636 declare
7637 T : Entity_Id := (Etype (Get_Associated_Node (New_N)));
7638 Rt : Entity_Id;
7639
7640 begin
7641 if Present (T) and then Is_Private_Type (T) then
7642 Switch_View (T);
7643 end if;
7644
7645 if Present (T)
7646 and then Is_Tagged_Type (T)
7647 and then Is_Derived_Type (T)
7648 then
7649 Rt := Root_Type (T);
7650
7651 loop
7652 T := Etype (T);
7653
7654 if Is_Private_Type (T) then
7655 Switch_View (T);
7656 end if;
7657
7658 exit when T = Rt;
7659 end loop;
7660 end if;
7661 end;
7662 end if;
7663 end if;
7664
7665 -- Do not copy the associated node, which points to the generic copy
7666 -- of the aggregate.
7667
7668 declare
7669 use Atree.Unchecked_Access;
7670 -- This code section is part of the implementation of an untyped
7671 -- tree traversal, so it needs direct access to node fields.
7672
7673 begin
7674 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
7675 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
7676 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
7677 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
7678 end;
7679
7680 -- Allocators do not have an identifier denoting the access type, so we
7681 -- must locate it through the expression to check whether the views are
7682 -- consistent.
7683
7684 elsif Nkind (N) = N_Allocator
7685 and then Nkind (Expression (N)) = N_Qualified_Expression
7686 and then Is_Entity_Name (Subtype_Mark (Expression (N)))
7687 and then Instantiating
7688 then
7689 declare
7690 T : constant Node_Id :=
7691 Get_Associated_Node (Subtype_Mark (Expression (N)));
7692 Acc_T : Entity_Id;
7693
7694 begin
7695 if Present (T) then
7696
7697 -- Retrieve the allocator node in the generic copy
7698
7699 Acc_T := Etype (Parent (Parent (T)));
7700
7701 if Present (Acc_T) and then Is_Private_Type (Acc_T) then
7702 Switch_View (Acc_T);
7703 end if;
7704 end if;
7705
7706 Copy_Descendants;
7707 end;
7708
7709 -- For a proper body, we must catch the case of a proper body that
7710 -- replaces a stub. This represents the point at which a separate
7711 -- compilation unit, and hence template file, may be referenced, so we
7712 -- must make a new source instantiation entry for the template of the
7713 -- subunit, and ensure that all nodes in the subunit are adjusted using
7714 -- this new source instantiation entry.
7715
7716 elsif Nkind (N) in N_Proper_Body then
7717 declare
7718 Save_Adjustment : constant Sloc_Adjustment := S_Adjustment;
7719
7720 begin
7721 if Instantiating and then Was_Originally_Stub (N) then
7722 Create_Instantiation_Source
7723 (Instantiation_Node,
7724 Defining_Entity (N),
7725 S_Adjustment);
7726 end if;
7727
7728 -- Now copy the fields of the proper body, using the new
7729 -- adjustment factor if one was needed as per test above.
7730
7731 Copy_Descendants;
7732
7733 -- Restore the original adjustment factor in case changed
7734
7735 S_Adjustment := Save_Adjustment;
7736 end;
7737
7738 elsif Nkind (N) = N_Pragma and then Instantiating then
7739
7740 -- Do not copy Comment or Ident pragmas their content is relevant to
7741 -- the generic unit, not to the instantiating unit.
7742
7743 if Nam_In (Pragma_Name (N), Name_Comment, Name_Ident) then
7744 New_N := Make_Null_Statement (Sloc (N));
7745
7746 -- Do not copy pragmas generated from aspects because the pragmas do
7747 -- not carry any semantic information, plus they will be regenerated
7748 -- in the instance.
7749
7750 -- However, generating C we need to copy them since postconditions
7751 -- are inlined by the front end, and the front-end inlining machinery
7752 -- relies on this routine to perform inlining.
7753
7754 elsif From_Aspect_Specification (N)
7755 and then not Modify_Tree_For_C
7756 then
7757 New_N := Make_Null_Statement (Sloc (N));
7758
7759 else
7760 Copy_Descendants;
7761 end if;
7762
7763 elsif Nkind_In (N, N_Integer_Literal, N_Real_Literal) then
7764
7765 -- No descendant fields need traversing
7766
7767 null;
7768
7769 elsif Nkind (N) = N_String_Literal
7770 and then Present (Etype (N))
7771 and then Instantiating
7772 then
7773 -- If the string is declared in an outer scope, the string_literal
7774 -- subtype created for it may have the wrong scope. Force reanalysis
7775 -- of the constant to generate a new itype in the proper context.
7776
7777 Set_Etype (New_N, Empty);
7778 Set_Analyzed (New_N, False);
7779
7780 -- For the remaining nodes, copy their descendants recursively
7781
7782 else
7783 Copy_Descendants;
7784
7785 if Instantiating and then Nkind (N) = N_Subprogram_Body then
7786 Set_Generic_Parent (Specification (New_N), N);
7787
7788 -- Should preserve Corresponding_Spec??? (12.3(14))
7789 end if;
7790 end if;
7791
7792 -- Propagate dimensions if present, so that they are reflected in the
7793 -- instance.
7794
7795 if Nkind (N) in N_Has_Etype
7796 and then (Nkind (N) in N_Op or else Is_Entity_Name (N))
7797 and then Present (Etype (N))
7798 and then Is_Floating_Point_Type (Etype (N))
7799 and then Has_Dimension_System (Etype (N))
7800 then
7801 Copy_Dimensions (N, New_N);
7802 end if;
7803
7804 return New_N;
7805 end Copy_Generic_Node;
7806
7807 ----------------------------
7808 -- Denotes_Formal_Package --
7809 ----------------------------
7810
7811 function Denotes_Formal_Package
7812 (Pack : Entity_Id;
7813 On_Exit : Boolean := False;
7814 Instance : Entity_Id := Empty) return Boolean
7815 is
7816 Par : Entity_Id;
7817 Scop : constant Entity_Id := Scope (Pack);
7818 E : Entity_Id;
7819
7820 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean;
7821 -- The package in question may be an actual for a previous formal
7822 -- package P of the current instance, so examine its actuals as well.
7823 -- This must be recursive over other formal packages.
7824
7825 ----------------------------------
7826 -- Is_Actual_Of_Previous_Formal --
7827 ----------------------------------
7828
7829 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean is
7830 E1 : Entity_Id;
7831
7832 begin
7833 E1 := First_Entity (P);
7834 while Present (E1) and then E1 /= Instance loop
7835 if Ekind (E1) = E_Package
7836 and then Nkind (Parent (E1)) = N_Package_Renaming_Declaration
7837 then
7838 if Renamed_Object (E1) = Pack then
7839 return True;
7840
7841 elsif E1 = P or else Renamed_Object (E1) = P then
7842 return False;
7843
7844 elsif Is_Actual_Of_Previous_Formal (E1) then
7845 return True;
7846 end if;
7847 end if;
7848
7849 Next_Entity (E1);
7850 end loop;
7851
7852 return False;
7853 end Is_Actual_Of_Previous_Formal;
7854
7855 -- Start of processing for Denotes_Formal_Package
7856
7857 begin
7858 if On_Exit then
7859 Par :=
7860 Instance_Envs.Table
7861 (Instance_Envs.Last).Instantiated_Parent.Act_Id;
7862 else
7863 Par := Current_Instantiated_Parent.Act_Id;
7864 end if;
7865
7866 if Ekind (Scop) = E_Generic_Package
7867 or else Nkind (Unit_Declaration_Node (Scop)) =
7868 N_Generic_Subprogram_Declaration
7869 then
7870 return True;
7871
7872 elsif Nkind (Original_Node (Unit_Declaration_Node (Pack))) =
7873 N_Formal_Package_Declaration
7874 then
7875 return True;
7876
7877 elsif No (Par) then
7878 return False;
7879
7880 else
7881 -- Check whether this package is associated with a formal package of
7882 -- the enclosing instantiation. Iterate over the list of renamings.
7883
7884 E := First_Entity (Par);
7885 while Present (E) loop
7886 if Ekind (E) /= E_Package
7887 or else Nkind (Parent (E)) /= N_Package_Renaming_Declaration
7888 then
7889 null;
7890
7891 elsif Renamed_Object (E) = Par then
7892 return False;
7893
7894 elsif Renamed_Object (E) = Pack then
7895 return True;
7896
7897 elsif Is_Actual_Of_Previous_Formal (E) then
7898 return True;
7899
7900 end if;
7901
7902 Next_Entity (E);
7903 end loop;
7904
7905 return False;
7906 end if;
7907 end Denotes_Formal_Package;
7908
7909 -----------------
7910 -- End_Generic --
7911 -----------------
7912
7913 procedure End_Generic is
7914 begin
7915 -- ??? More things could be factored out in this routine. Should
7916 -- probably be done at a later stage.
7917
7918 Inside_A_Generic := Generic_Flags.Table (Generic_Flags.Last);
7919 Generic_Flags.Decrement_Last;
7920
7921 Expander_Mode_Restore;
7922 end End_Generic;
7923
7924 -------------
7925 -- Earlier --
7926 -------------
7927
7928 function Earlier (N1, N2 : Node_Id) return Boolean is
7929 procedure Find_Depth (P : in out Node_Id; D : in out Integer);
7930 -- Find distance from given node to enclosing compilation unit
7931
7932 ----------------
7933 -- Find_Depth --
7934 ----------------
7935
7936 procedure Find_Depth (P : in out Node_Id; D : in out Integer) is
7937 begin
7938 while Present (P)
7939 and then Nkind (P) /= N_Compilation_Unit
7940 loop
7941 P := True_Parent (P);
7942 D := D + 1;
7943 end loop;
7944 end Find_Depth;
7945
7946 -- Local declarations
7947
7948 D1 : Integer := 0;
7949 D2 : Integer := 0;
7950 P1 : Node_Id := N1;
7951 P2 : Node_Id := N2;
7952 T1 : Source_Ptr;
7953 T2 : Source_Ptr;
7954
7955 -- Start of processing for Earlier
7956
7957 begin
7958 Find_Depth (P1, D1);
7959 Find_Depth (P2, D2);
7960
7961 if P1 /= P2 then
7962 return False;
7963 else
7964 P1 := N1;
7965 P2 := N2;
7966 end if;
7967
7968 while D1 > D2 loop
7969 P1 := True_Parent (P1);
7970 D1 := D1 - 1;
7971 end loop;
7972
7973 while D2 > D1 loop
7974 P2 := True_Parent (P2);
7975 D2 := D2 - 1;
7976 end loop;
7977
7978 -- At this point P1 and P2 are at the same distance from the root.
7979 -- We examine their parents until we find a common declarative list.
7980 -- If we reach the root, N1 and N2 do not descend from the same
7981 -- declarative list (e.g. one is nested in the declarative part and
7982 -- the other is in a block in the statement part) and the earlier
7983 -- one is already frozen.
7984
7985 while not Is_List_Member (P1)
7986 or else not Is_List_Member (P2)
7987 or else List_Containing (P1) /= List_Containing (P2)
7988 loop
7989 P1 := True_Parent (P1);
7990 P2 := True_Parent (P2);
7991
7992 if Nkind (Parent (P1)) = N_Subunit then
7993 P1 := Corresponding_Stub (Parent (P1));
7994 end if;
7995
7996 if Nkind (Parent (P2)) = N_Subunit then
7997 P2 := Corresponding_Stub (Parent (P2));
7998 end if;
7999
8000 if P1 = P2 then
8001 return False;
8002 end if;
8003 end loop;
8004
8005 -- Expanded code usually shares the source location of the original
8006 -- construct it was generated for. This however may not necessarily
8007 -- reflect the true location of the code within the tree.
8008
8009 -- Before comparing the slocs of the two nodes, make sure that we are
8010 -- working with correct source locations. Assume that P1 is to the left
8011 -- of P2. If either one does not come from source, traverse the common
8012 -- list heading towards the other node and locate the first source
8013 -- statement.
8014
8015 -- P1 P2
8016 -- ----+===+===+--------------+===+===+----
8017 -- expanded code expanded code
8018
8019 if not Comes_From_Source (P1) then
8020 while Present (P1) loop
8021
8022 -- Neither P2 nor a source statement were located during the
8023 -- search. If we reach the end of the list, then P1 does not
8024 -- occur earlier than P2.
8025
8026 -- ---->
8027 -- start --- P2 ----- P1 --- end
8028
8029 if No (Next (P1)) then
8030 return False;
8031
8032 -- We encounter P2 while going to the right of the list. This
8033 -- means that P1 does indeed appear earlier.
8034
8035 -- ---->
8036 -- start --- P1 ===== P2 --- end
8037 -- expanded code in between
8038
8039 elsif P1 = P2 then
8040 return True;
8041
8042 -- No need to look any further since we have located a source
8043 -- statement.
8044
8045 elsif Comes_From_Source (P1) then
8046 exit;
8047 end if;
8048
8049 -- Keep going right
8050
8051 Next (P1);
8052 end loop;
8053 end if;
8054
8055 if not Comes_From_Source (P2) then
8056 while Present (P2) loop
8057
8058 -- Neither P1 nor a source statement were located during the
8059 -- search. If we reach the start of the list, then P1 does not
8060 -- occur earlier than P2.
8061
8062 -- <----
8063 -- start --- P2 --- P1 --- end
8064
8065 if No (Prev (P2)) then
8066 return False;
8067
8068 -- We encounter P1 while going to the left of the list. This
8069 -- means that P1 does indeed appear earlier.
8070
8071 -- <----
8072 -- start --- P1 ===== P2 --- end
8073 -- expanded code in between
8074
8075 elsif P2 = P1 then
8076 return True;
8077
8078 -- No need to look any further since we have located a source
8079 -- statement.
8080
8081 elsif Comes_From_Source (P2) then
8082 exit;
8083 end if;
8084
8085 -- Keep going left
8086
8087 Prev (P2);
8088 end loop;
8089 end if;
8090
8091 -- At this point either both nodes came from source or we approximated
8092 -- their source locations through neighboring source statements.
8093
8094 T1 := Top_Level_Location (Sloc (P1));
8095 T2 := Top_Level_Location (Sloc (P2));
8096
8097 -- When two nodes come from the same instance, they have identical top
8098 -- level locations. To determine proper relation within the tree, check
8099 -- their locations within the template.
8100
8101 if T1 = T2 then
8102 return Sloc (P1) < Sloc (P2);
8103
8104 -- The two nodes either come from unrelated instances or do not come
8105 -- from instantiated code at all.
8106
8107 else
8108 return T1 < T2;
8109 end if;
8110 end Earlier;
8111
8112 ----------------------
8113 -- Find_Actual_Type --
8114 ----------------------
8115
8116 function Find_Actual_Type
8117 (Typ : Entity_Id;
8118 Gen_Type : Entity_Id) return Entity_Id
8119 is
8120 Gen_Scope : constant Entity_Id := Scope (Gen_Type);
8121 T : Entity_Id;
8122
8123 begin
8124 -- Special processing only applies to child units
8125
8126 if not Is_Child_Unit (Gen_Scope) then
8127 return Get_Instance_Of (Typ);
8128
8129 -- If designated or component type is itself a formal of the child unit,
8130 -- its instance is available.
8131
8132 elsif Scope (Typ) = Gen_Scope then
8133 return Get_Instance_Of (Typ);
8134
8135 -- If the array or access type is not declared in the parent unit,
8136 -- no special processing needed.
8137
8138 elsif not Is_Generic_Type (Typ)
8139 and then Scope (Gen_Scope) /= Scope (Typ)
8140 then
8141 return Get_Instance_Of (Typ);
8142
8143 -- Otherwise, retrieve designated or component type by visibility
8144
8145 else
8146 T := Current_Entity (Typ);
8147 while Present (T) loop
8148 if In_Open_Scopes (Scope (T)) then
8149 return T;
8150 elsif Is_Generic_Actual_Type (T) then
8151 return T;
8152 end if;
8153
8154 T := Homonym (T);
8155 end loop;
8156
8157 return Typ;
8158 end if;
8159 end Find_Actual_Type;
8160
8161 ----------------------------
8162 -- Freeze_Subprogram_Body --
8163 ----------------------------
8164
8165 procedure Freeze_Subprogram_Body
8166 (Inst_Node : Node_Id;
8167 Gen_Body : Node_Id;
8168 Pack_Id : Entity_Id)
8169 is
8170 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
8171 Par : constant Entity_Id := Scope (Gen_Unit);
8172 E_G_Id : Entity_Id;
8173 Enc_G : Entity_Id;
8174 Enc_I : Node_Id;
8175 F_Node : Node_Id;
8176
8177 function Enclosing_Package_Body (N : Node_Id) return Node_Id;
8178 -- Find innermost package body that encloses the given node, and which
8179 -- is not a compilation unit. Freeze nodes for the instance, or for its
8180 -- enclosing body, may be inserted after the enclosing_body of the
8181 -- generic unit. Used to determine proper placement of freeze node for
8182 -- both package and subprogram instances.
8183
8184 function Package_Freeze_Node (B : Node_Id) return Node_Id;
8185 -- Find entity for given package body, and locate or create a freeze
8186 -- node for it.
8187
8188 ----------------------------
8189 -- Enclosing_Package_Body --
8190 ----------------------------
8191
8192 function Enclosing_Package_Body (N : Node_Id) return Node_Id is
8193 P : Node_Id;
8194
8195 begin
8196 P := Parent (N);
8197 while Present (P)
8198 and then Nkind (Parent (P)) /= N_Compilation_Unit
8199 loop
8200 if Nkind (P) = N_Package_Body then
8201 if Nkind (Parent (P)) = N_Subunit then
8202 return Corresponding_Stub (Parent (P));
8203 else
8204 return P;
8205 end if;
8206 end if;
8207
8208 P := True_Parent (P);
8209 end loop;
8210
8211 return Empty;
8212 end Enclosing_Package_Body;
8213
8214 -------------------------
8215 -- Package_Freeze_Node --
8216 -------------------------
8217
8218 function Package_Freeze_Node (B : Node_Id) return Node_Id is
8219 Id : Entity_Id;
8220
8221 begin
8222 if Nkind (B) = N_Package_Body then
8223 Id := Corresponding_Spec (B);
8224 else pragma Assert (Nkind (B) = N_Package_Body_Stub);
8225 Id := Corresponding_Spec (Proper_Body (Unit (Library_Unit (B))));
8226 end if;
8227
8228 Ensure_Freeze_Node (Id);
8229 return Freeze_Node (Id);
8230 end Package_Freeze_Node;
8231
8232 -- Start of processing for Freeze_Subprogram_Body
8233
8234 begin
8235 -- If the instance and the generic body appear within the same unit, and
8236 -- the instance precedes the generic, the freeze node for the instance
8237 -- must appear after that of the generic. If the generic is nested
8238 -- within another instance I2, then current instance must be frozen
8239 -- after I2. In both cases, the freeze nodes are those of enclosing
8240 -- packages. Otherwise, the freeze node is placed at the end of the
8241 -- current declarative part.
8242
8243 Enc_G := Enclosing_Package_Body (Gen_Body);
8244 Enc_I := Enclosing_Package_Body (Inst_Node);
8245 Ensure_Freeze_Node (Pack_Id);
8246 F_Node := Freeze_Node (Pack_Id);
8247
8248 if Is_Generic_Instance (Par)
8249 and then Present (Freeze_Node (Par))
8250 and then In_Same_Declarative_Part (Freeze_Node (Par), Inst_Node)
8251 then
8252 -- The parent was a premature instantiation. Insert freeze node at
8253 -- the end the current declarative part.
8254
8255 if ABE_Is_Certain (Get_Package_Instantiation_Node (Par)) then
8256 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8257
8258 -- Handle the following case:
8259 --
8260 -- package Parent_Inst is new ...
8261 -- Parent_Inst []
8262 --
8263 -- procedure P ... -- this body freezes Parent_Inst
8264 --
8265 -- package Inst is new ...
8266 --
8267 -- In this particular scenario, the freeze node for Inst must be
8268 -- inserted in the same manner as that of Parent_Inst - before the
8269 -- next source body or at the end of the declarative list (body not
8270 -- available). If body P did not exist and Parent_Inst was frozen
8271 -- after Inst, either by a body following Inst or at the end of the
8272 -- declarative region, the freeze node for Inst must be inserted
8273 -- after that of Parent_Inst. This relation is established by
8274 -- comparing the Slocs of Parent_Inst freeze node and Inst.
8275
8276 elsif List_Containing (Get_Package_Instantiation_Node (Par)) =
8277 List_Containing (Inst_Node)
8278 and then Sloc (Freeze_Node (Par)) < Sloc (Inst_Node)
8279 then
8280 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8281
8282 else
8283 Insert_After (Freeze_Node (Par), F_Node);
8284 end if;
8285
8286 -- The body enclosing the instance should be frozen after the body that
8287 -- includes the generic, because the body of the instance may make
8288 -- references to entities therein. If the two are not in the same
8289 -- declarative part, or if the one enclosing the instance is frozen
8290 -- already, freeze the instance at the end of the current declarative
8291 -- part.
8292
8293 elsif Is_Generic_Instance (Par)
8294 and then Present (Freeze_Node (Par))
8295 and then Present (Enc_I)
8296 then
8297 if In_Same_Declarative_Part (Freeze_Node (Par), Enc_I)
8298 or else
8299 (Nkind (Enc_I) = N_Package_Body
8300 and then
8301 In_Same_Declarative_Part (Freeze_Node (Par), Parent (Enc_I)))
8302 then
8303 -- The enclosing package may contain several instances. Rather
8304 -- than computing the earliest point at which to insert its freeze
8305 -- node, we place it at the end of the declarative part of the
8306 -- parent of the generic.
8307
8308 Insert_Freeze_Node_For_Instance
8309 (Freeze_Node (Par), Package_Freeze_Node (Enc_I));
8310 end if;
8311
8312 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8313
8314 elsif Present (Enc_G)
8315 and then Present (Enc_I)
8316 and then Enc_G /= Enc_I
8317 and then Earlier (Inst_Node, Gen_Body)
8318 then
8319 if Nkind (Enc_G) = N_Package_Body then
8320 E_G_Id :=
8321 Corresponding_Spec (Enc_G);
8322 else pragma Assert (Nkind (Enc_G) = N_Package_Body_Stub);
8323 E_G_Id :=
8324 Corresponding_Spec (Proper_Body (Unit (Library_Unit (Enc_G))));
8325 end if;
8326
8327 -- Freeze package that encloses instance, and place node after the
8328 -- package that encloses generic. If enclosing package is already
8329 -- frozen we have to assume it is at the proper place. This may be a
8330 -- potential ABE that requires dynamic checking. Do not add a freeze
8331 -- node if the package that encloses the generic is inside the body
8332 -- that encloses the instance, because the freeze node would be in
8333 -- the wrong scope. Additional contortions needed if the bodies are
8334 -- within a subunit.
8335
8336 declare
8337 Enclosing_Body : Node_Id;
8338
8339 begin
8340 if Nkind (Enc_I) = N_Package_Body_Stub then
8341 Enclosing_Body := Proper_Body (Unit (Library_Unit (Enc_I)));
8342 else
8343 Enclosing_Body := Enc_I;
8344 end if;
8345
8346 if Parent (List_Containing (Enc_G)) /= Enclosing_Body then
8347 Insert_Freeze_Node_For_Instance
8348 (Enc_G, Package_Freeze_Node (Enc_I));
8349 end if;
8350 end;
8351
8352 -- Freeze enclosing subunit before instance
8353
8354 Ensure_Freeze_Node (E_G_Id);
8355
8356 if not Is_List_Member (Freeze_Node (E_G_Id)) then
8357 Insert_After (Enc_G, Freeze_Node (E_G_Id));
8358 end if;
8359
8360 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8361
8362 else
8363 -- If none of the above, insert freeze node at the end of the current
8364 -- declarative part.
8365
8366 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8367 end if;
8368 end Freeze_Subprogram_Body;
8369
8370 ----------------
8371 -- Get_Gen_Id --
8372 ----------------
8373
8374 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id is
8375 begin
8376 return Generic_Renamings.Table (E).Gen_Id;
8377 end Get_Gen_Id;
8378
8379 ---------------------
8380 -- Get_Instance_Of --
8381 ---------------------
8382
8383 function Get_Instance_Of (A : Entity_Id) return Entity_Id is
8384 Res : constant Assoc_Ptr := Generic_Renamings_HTable.Get (A);
8385
8386 begin
8387 if Res /= Assoc_Null then
8388 return Generic_Renamings.Table (Res).Act_Id;
8389
8390 else
8391 -- On exit, entity is not instantiated: not a generic parameter, or
8392 -- else parameter of an inner generic unit.
8393
8394 return A;
8395 end if;
8396 end Get_Instance_Of;
8397
8398 ------------------------------------
8399 -- Get_Package_Instantiation_Node --
8400 ------------------------------------
8401
8402 function Get_Package_Instantiation_Node (A : Entity_Id) return Node_Id is
8403 Decl : Node_Id := Unit_Declaration_Node (A);
8404 Inst : Node_Id;
8405
8406 begin
8407 -- If the Package_Instantiation attribute has been set on the package
8408 -- entity, then use it directly when it (or its Original_Node) refers
8409 -- to an N_Package_Instantiation node. In principle it should be
8410 -- possible to have this field set in all cases, which should be
8411 -- investigated, and would allow this function to be significantly
8412 -- simplified. ???
8413
8414 Inst := Package_Instantiation (A);
8415
8416 if Present (Inst) then
8417 if Nkind (Inst) = N_Package_Instantiation then
8418 return Inst;
8419
8420 elsif Nkind (Original_Node (Inst)) = N_Package_Instantiation then
8421 return Original_Node (Inst);
8422 end if;
8423 end if;
8424
8425 -- If the instantiation is a compilation unit that does not need body
8426 -- then the instantiation node has been rewritten as a package
8427 -- declaration for the instance, and we return the original node.
8428
8429 -- If it is a compilation unit and the instance node has not been
8430 -- rewritten, then it is still the unit of the compilation. Finally, if
8431 -- a body is present, this is a parent of the main unit whose body has
8432 -- been compiled for inlining purposes, and the instantiation node has
8433 -- been rewritten with the instance body.
8434
8435 -- Otherwise the instantiation node appears after the declaration. If
8436 -- the entity is a formal package, the declaration may have been
8437 -- rewritten as a generic declaration (in the case of a formal with box)
8438 -- or left as a formal package declaration if it has actuals, and is
8439 -- found with a forward search.
8440
8441 if Nkind (Parent (Decl)) = N_Compilation_Unit then
8442 if Nkind (Decl) = N_Package_Declaration
8443 and then Present (Corresponding_Body (Decl))
8444 then
8445 Decl := Unit_Declaration_Node (Corresponding_Body (Decl));
8446 end if;
8447
8448 if Nkind (Original_Node (Decl)) = N_Package_Instantiation then
8449 return Original_Node (Decl);
8450 else
8451 return Unit (Parent (Decl));
8452 end if;
8453
8454 elsif Nkind (Decl) = N_Package_Declaration
8455 and then Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration
8456 then
8457 return Original_Node (Decl);
8458
8459 else
8460 Inst := Next (Decl);
8461 while not Nkind_In (Inst, N_Package_Instantiation,
8462 N_Formal_Package_Declaration)
8463 loop
8464 Next (Inst);
8465 end loop;
8466
8467 return Inst;
8468 end if;
8469 end Get_Package_Instantiation_Node;
8470
8471 ------------------------
8472 -- Has_Been_Exchanged --
8473 ------------------------
8474
8475 function Has_Been_Exchanged (E : Entity_Id) return Boolean is
8476 Next : Elmt_Id;
8477
8478 begin
8479 Next := First_Elmt (Exchanged_Views);
8480 while Present (Next) loop
8481 if Full_View (Node (Next)) = E then
8482 return True;
8483 end if;
8484
8485 Next_Elmt (Next);
8486 end loop;
8487
8488 return False;
8489 end Has_Been_Exchanged;
8490
8491 ----------
8492 -- Hash --
8493 ----------
8494
8495 function Hash (F : Entity_Id) return HTable_Range is
8496 begin
8497 return HTable_Range (F mod HTable_Size);
8498 end Hash;
8499
8500 ------------------------
8501 -- Hide_Current_Scope --
8502 ------------------------
8503
8504 procedure Hide_Current_Scope is
8505 C : constant Entity_Id := Current_Scope;
8506 E : Entity_Id;
8507
8508 begin
8509 Set_Is_Hidden_Open_Scope (C);
8510
8511 E := First_Entity (C);
8512 while Present (E) loop
8513 if Is_Immediately_Visible (E) then
8514 Set_Is_Immediately_Visible (E, False);
8515 Append_Elmt (E, Hidden_Entities);
8516 end if;
8517
8518 Next_Entity (E);
8519 end loop;
8520
8521 -- Make the scope name invisible as well. This is necessary, but might
8522 -- conflict with calls to Rtsfind later on, in case the scope is a
8523 -- predefined one. There is no clean solution to this problem, so for
8524 -- now we depend on the user not redefining Standard itself in one of
8525 -- the parent units.
8526
8527 if Is_Immediately_Visible (C) and then C /= Standard_Standard then
8528 Set_Is_Immediately_Visible (C, False);
8529 Append_Elmt (C, Hidden_Entities);
8530 end if;
8531
8532 end Hide_Current_Scope;
8533
8534 --------------
8535 -- Init_Env --
8536 --------------
8537
8538 procedure Init_Env is
8539 Saved : Instance_Env;
8540
8541 begin
8542 Saved.Instantiated_Parent := Current_Instantiated_Parent;
8543 Saved.Exchanged_Views := Exchanged_Views;
8544 Saved.Hidden_Entities := Hidden_Entities;
8545 Saved.Current_Sem_Unit := Current_Sem_Unit;
8546 Saved.Parent_Unit_Visible := Parent_Unit_Visible;
8547 Saved.Instance_Parent_Unit := Instance_Parent_Unit;
8548
8549 -- Save configuration switches. These may be reset if the unit is a
8550 -- predefined unit, and the current mode is not Ada 2005.
8551
8552 Save_Opt_Config_Switches (Saved.Switches);
8553
8554 Instance_Envs.Append (Saved);
8555
8556 Exchanged_Views := New_Elmt_List;
8557 Hidden_Entities := New_Elmt_List;
8558
8559 -- Make dummy entry for Instantiated parent. If generic unit is legal,
8560 -- this is set properly in Set_Instance_Env.
8561
8562 Current_Instantiated_Parent :=
8563 (Current_Scope, Current_Scope, Assoc_Null);
8564 end Init_Env;
8565
8566 ------------------------------
8567 -- In_Same_Declarative_Part --
8568 ------------------------------
8569
8570 function In_Same_Declarative_Part
8571 (F_Node : Node_Id;
8572 Inst : Node_Id) return Boolean
8573 is
8574 Decls : constant Node_Id := Parent (F_Node);
8575 Nod : Node_Id;
8576
8577 begin
8578 Nod := Parent (Inst);
8579 while Present (Nod) loop
8580 if Nod = Decls then
8581 return True;
8582
8583 elsif Nkind_In (Nod, N_Subprogram_Body,
8584 N_Package_Body,
8585 N_Package_Declaration,
8586 N_Task_Body,
8587 N_Protected_Body,
8588 N_Block_Statement)
8589 then
8590 return False;
8591
8592 elsif Nkind (Nod) = N_Subunit then
8593 Nod := Corresponding_Stub (Nod);
8594
8595 elsif Nkind (Nod) = N_Compilation_Unit then
8596 return False;
8597
8598 else
8599 Nod := Parent (Nod);
8600 end if;
8601 end loop;
8602
8603 return False;
8604 end In_Same_Declarative_Part;
8605
8606 ---------------------
8607 -- In_Main_Context --
8608 ---------------------
8609
8610 function In_Main_Context (E : Entity_Id) return Boolean is
8611 Context : List_Id;
8612 Clause : Node_Id;
8613 Nam : Node_Id;
8614
8615 begin
8616 if not Is_Compilation_Unit (E)
8617 or else Ekind (E) /= E_Package
8618 or else In_Private_Part (E)
8619 then
8620 return False;
8621 end if;
8622
8623 Context := Context_Items (Cunit (Main_Unit));
8624
8625 Clause := First (Context);
8626 while Present (Clause) loop
8627 if Nkind (Clause) = N_With_Clause then
8628 Nam := Name (Clause);
8629
8630 -- If the current scope is part of the context of the main unit,
8631 -- analysis of the corresponding with_clause is not complete, and
8632 -- the entity is not set. We use the Chars field directly, which
8633 -- might produce false positives in rare cases, but guarantees
8634 -- that we produce all the instance bodies we will need.
8635
8636 if (Is_Entity_Name (Nam) and then Chars (Nam) = Chars (E))
8637 or else (Nkind (Nam) = N_Selected_Component
8638 and then Chars (Selector_Name (Nam)) = Chars (E))
8639 then
8640 return True;
8641 end if;
8642 end if;
8643
8644 Next (Clause);
8645 end loop;
8646
8647 return False;
8648 end In_Main_Context;
8649
8650 ---------------------
8651 -- Inherit_Context --
8652 ---------------------
8653
8654 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id) is
8655 Current_Context : List_Id;
8656 Current_Unit : Node_Id;
8657 Item : Node_Id;
8658 New_I : Node_Id;
8659
8660 Clause : Node_Id;
8661 OK : Boolean;
8662 Lib_Unit : Node_Id;
8663
8664 begin
8665 if Nkind (Parent (Gen_Decl)) = N_Compilation_Unit then
8666
8667 -- The inherited context is attached to the enclosing compilation
8668 -- unit. This is either the main unit, or the declaration for the
8669 -- main unit (in case the instantiation appears within the package
8670 -- declaration and the main unit is its body).
8671
8672 Current_Unit := Parent (Inst);
8673 while Present (Current_Unit)
8674 and then Nkind (Current_Unit) /= N_Compilation_Unit
8675 loop
8676 Current_Unit := Parent (Current_Unit);
8677 end loop;
8678
8679 Current_Context := Context_Items (Current_Unit);
8680
8681 Item := First (Context_Items (Parent (Gen_Decl)));
8682 while Present (Item) loop
8683 if Nkind (Item) = N_With_Clause then
8684 Lib_Unit := Library_Unit (Item);
8685
8686 -- Take care to prevent direct cyclic with's
8687
8688 if Lib_Unit /= Current_Unit then
8689
8690 -- Do not add a unit if it is already in the context
8691
8692 Clause := First (Current_Context);
8693 OK := True;
8694 while Present (Clause) loop
8695 if Nkind (Clause) = N_With_Clause and then
8696 Library_Unit (Clause) = Lib_Unit
8697 then
8698 OK := False;
8699 exit;
8700 end if;
8701
8702 Next (Clause);
8703 end loop;
8704
8705 if OK then
8706 New_I := New_Copy (Item);
8707 Set_Implicit_With (New_I, True);
8708 Set_Implicit_With_From_Instantiation (New_I, True);
8709 Append (New_I, Current_Context);
8710 end if;
8711 end if;
8712 end if;
8713
8714 Next (Item);
8715 end loop;
8716 end if;
8717 end Inherit_Context;
8718
8719 ----------------
8720 -- Initialize --
8721 ----------------
8722
8723 procedure Initialize is
8724 begin
8725 Generic_Renamings.Init;
8726 Instance_Envs.Init;
8727 Generic_Flags.Init;
8728 Generic_Renamings_HTable.Reset;
8729 Circularity_Detected := False;
8730 Exchanged_Views := No_Elist;
8731 Hidden_Entities := No_Elist;
8732 end Initialize;
8733
8734 -------------------------------------
8735 -- Insert_Freeze_Node_For_Instance --
8736 -------------------------------------
8737
8738 procedure Insert_Freeze_Node_For_Instance
8739 (N : Node_Id;
8740 F_Node : Node_Id)
8741 is
8742 Decl : Node_Id;
8743 Decls : List_Id;
8744 Inst : Entity_Id;
8745 Par_N : Node_Id;
8746
8747 function Enclosing_Body (N : Node_Id) return Node_Id;
8748 -- Find enclosing package or subprogram body, if any. Freeze node may
8749 -- be placed at end of current declarative list if previous instance
8750 -- and current one have different enclosing bodies.
8751
8752 function Previous_Instance (Gen : Entity_Id) return Entity_Id;
8753 -- Find the local instance, if any, that declares the generic that is
8754 -- being instantiated. If present, the freeze node for this instance
8755 -- must follow the freeze node for the previous instance.
8756
8757 --------------------
8758 -- Enclosing_Body --
8759 --------------------
8760
8761 function Enclosing_Body (N : Node_Id) return Node_Id is
8762 P : Node_Id;
8763
8764 begin
8765 P := Parent (N);
8766 while Present (P)
8767 and then Nkind (Parent (P)) /= N_Compilation_Unit
8768 loop
8769 if Nkind_In (P, N_Package_Body, N_Subprogram_Body) then
8770 if Nkind (Parent (P)) = N_Subunit then
8771 return Corresponding_Stub (Parent (P));
8772 else
8773 return P;
8774 end if;
8775 end if;
8776
8777 P := True_Parent (P);
8778 end loop;
8779
8780 return Empty;
8781 end Enclosing_Body;
8782
8783 -----------------------
8784 -- Previous_Instance --
8785 -----------------------
8786
8787 function Previous_Instance (Gen : Entity_Id) return Entity_Id is
8788 S : Entity_Id;
8789
8790 begin
8791 S := Scope (Gen);
8792 while Present (S) and then S /= Standard_Standard loop
8793 if Is_Generic_Instance (S)
8794 and then In_Same_Source_Unit (S, N)
8795 then
8796 return S;
8797 end if;
8798
8799 S := Scope (S);
8800 end loop;
8801
8802 return Empty;
8803 end Previous_Instance;
8804
8805 -- Start of processing for Insert_Freeze_Node_For_Instance
8806
8807 begin
8808 if not Is_List_Member (F_Node) then
8809 Decl := N;
8810 Decls := List_Containing (N);
8811 Inst := Entity (F_Node);
8812 Par_N := Parent (Decls);
8813
8814 -- When processing a subprogram instantiation, utilize the actual
8815 -- subprogram instantiation rather than its package wrapper as it
8816 -- carries all the context information.
8817
8818 if Is_Wrapper_Package (Inst) then
8819 Inst := Related_Instance (Inst);
8820 end if;
8821
8822 -- If this is a package instance, check whether the generic is
8823 -- declared in a previous instance and the current instance is
8824 -- not within the previous one.
8825
8826 if Present (Generic_Parent (Parent (Inst)))
8827 and then Is_In_Main_Unit (N)
8828 then
8829 declare
8830 Enclosing_N : constant Node_Id := Enclosing_Body (N);
8831 Par_I : constant Entity_Id :=
8832 Previous_Instance
8833 (Generic_Parent (Parent (Inst)));
8834 Scop : Entity_Id;
8835
8836 begin
8837 if Present (Par_I)
8838 and then Earlier (N, Freeze_Node (Par_I))
8839 then
8840 Scop := Scope (Inst);
8841
8842 -- If the current instance is within the one that contains
8843 -- the generic, the freeze node for the current one must
8844 -- appear in the current declarative part. Ditto, if the
8845 -- current instance is within another package instance or
8846 -- within a body that does not enclose the current instance.
8847 -- In these three cases the freeze node of the previous
8848 -- instance is not relevant.
8849
8850 while Present (Scop) and then Scop /= Standard_Standard loop
8851 exit when Scop = Par_I
8852 or else
8853 (Is_Generic_Instance (Scop)
8854 and then Scope_Depth (Scop) > Scope_Depth (Par_I));
8855 Scop := Scope (Scop);
8856 end loop;
8857
8858 -- Previous instance encloses current instance
8859
8860 if Scop = Par_I then
8861 null;
8862
8863 -- If the next node is a source body we must freeze in
8864 -- the current scope as well.
8865
8866 elsif Present (Next (N))
8867 and then Nkind_In (Next (N), N_Subprogram_Body,
8868 N_Package_Body)
8869 and then Comes_From_Source (Next (N))
8870 then
8871 null;
8872
8873 -- Current instance is within an unrelated instance
8874
8875 elsif Is_Generic_Instance (Scop) then
8876 null;
8877
8878 -- Current instance is within an unrelated body
8879
8880 elsif Present (Enclosing_N)
8881 and then Enclosing_N /= Enclosing_Body (Par_I)
8882 then
8883 null;
8884
8885 else
8886 Insert_After (Freeze_Node (Par_I), F_Node);
8887 return;
8888 end if;
8889 end if;
8890 end;
8891 end if;
8892
8893 -- When the instantiation occurs in a package declaration, append the
8894 -- freeze node to the private declarations (if any).
8895
8896 if Nkind (Par_N) = N_Package_Specification
8897 and then Decls = Visible_Declarations (Par_N)
8898 and then Present (Private_Declarations (Par_N))
8899 and then not Is_Empty_List (Private_Declarations (Par_N))
8900 then
8901 Decls := Private_Declarations (Par_N);
8902 Decl := First (Decls);
8903 end if;
8904
8905 -- Determine the proper freeze point of a package instantiation. We
8906 -- adhere to the general rule of a package or subprogram body causing
8907 -- freezing of anything before it in the same declarative region. In
8908 -- this case, the proper freeze point of a package instantiation is
8909 -- before the first source body which follows, or before a stub. This
8910 -- ensures that entities coming from the instance are already frozen
8911 -- and usable in source bodies.
8912
8913 if Nkind (Par_N) /= N_Package_Declaration
8914 and then Ekind (Inst) = E_Package
8915 and then Is_Generic_Instance (Inst)
8916 and then
8917 not In_Same_Source_Unit (Generic_Parent (Parent (Inst)), Inst)
8918 then
8919 while Present (Decl) loop
8920 if (Nkind (Decl) in N_Unit_Body
8921 or else
8922 Nkind (Decl) in N_Body_Stub)
8923 and then Comes_From_Source (Decl)
8924 then
8925 Insert_Before (Decl, F_Node);
8926 return;
8927 end if;
8928
8929 Next (Decl);
8930 end loop;
8931 end if;
8932
8933 -- In a package declaration, or if no previous body, insert at end
8934 -- of list.
8935
8936 Set_Sloc (F_Node, Sloc (Last (Decls)));
8937 Insert_After (Last (Decls), F_Node);
8938 end if;
8939 end Insert_Freeze_Node_For_Instance;
8940
8941 ------------------
8942 -- Install_Body --
8943 ------------------
8944
8945 procedure Install_Body
8946 (Act_Body : Node_Id;
8947 N : Node_Id;
8948 Gen_Body : Node_Id;
8949 Gen_Decl : Node_Id)
8950 is
8951
8952 function In_Same_Scope (Gen_Id, Act_Id : Node_Id) return Boolean;
8953 -- Check if the generic definition and the instantiation come from
8954 -- a common scope, in which case the instance must be frozen after
8955 -- the generic body.
8956
8957 function True_Sloc (N, Act_Unit : Node_Id) return Source_Ptr;
8958 -- If the instance is nested inside a generic unit, the Sloc of the
8959 -- instance indicates the place of the original definition, not the
8960 -- point of the current enclosing instance. Pending a better usage of
8961 -- Slocs to indicate instantiation places, we determine the place of
8962 -- origin of a node by finding the maximum sloc of any ancestor node.
8963 -- Why is this not equivalent to Top_Level_Location ???
8964
8965 -------------------
8966 -- In_Same_Scope --
8967 -------------------
8968
8969 function In_Same_Scope (Gen_Id, Act_Id : Node_Id) return Boolean is
8970 Act_Scop : Entity_Id := Scope (Act_Id);
8971 Gen_Scop : Entity_Id := Scope (Gen_Id);
8972
8973 begin
8974 while Act_Scop /= Standard_Standard
8975 and then Gen_Scop /= Standard_Standard
8976 loop
8977 if Act_Scop = Gen_Scop then
8978 return True;
8979 end if;
8980
8981 Act_Scop := Scope (Act_Scop);
8982 Gen_Scop := Scope (Gen_Scop);
8983 end loop;
8984
8985 return False;
8986 end In_Same_Scope;
8987
8988 ---------------
8989 -- True_Sloc --
8990 ---------------
8991
8992 function True_Sloc (N, Act_Unit : Node_Id) return Source_Ptr is
8993 Res : Source_Ptr;
8994 N1 : Node_Id;
8995
8996 begin
8997 Res := Sloc (N);
8998 N1 := N;
8999 while Present (N1) and then N1 /= Act_Unit loop
9000 if Sloc (N1) > Res then
9001 Res := Sloc (N1);
9002 end if;
9003
9004 N1 := Parent (N1);
9005 end loop;
9006
9007 return Res;
9008 end True_Sloc;
9009
9010 Act_Id : constant Entity_Id := Corresponding_Spec (Act_Body);
9011 Act_Unit : constant Node_Id := Unit (Cunit (Get_Source_Unit (N)));
9012 Gen_Id : constant Entity_Id := Corresponding_Spec (Gen_Body);
9013 Par : constant Entity_Id := Scope (Gen_Id);
9014 Gen_Unit : constant Node_Id :=
9015 Unit (Cunit (Get_Source_Unit (Gen_Decl)));
9016 Orig_Body : Node_Id := Gen_Body;
9017 F_Node : Node_Id;
9018 Body_Unit : Node_Id;
9019
9020 Must_Delay : Boolean;
9021
9022 -- Start of processing for Install_Body
9023
9024 begin
9025 -- Handle first the case of an instance with incomplete actual types.
9026 -- The instance body cannot be placed after the declaration because
9027 -- full views have not been seen yet. Any use of the non-limited views
9028 -- in the instance body requires the presence of a regular with_clause
9029 -- in the enclosing unit, and will fail if this with_clause is missing.
9030 -- We place the instance body at the beginning of the enclosing body,
9031 -- which is the unit being compiled. The freeze node for the instance
9032 -- is then placed after the instance body.
9033
9034 if not Is_Empty_Elmt_List (Incomplete_Actuals (Act_Id))
9035 and then Expander_Active
9036 and then Ekind (Scope (Act_Id)) = E_Package
9037 then
9038 declare
9039 Scop : constant Entity_Id := Scope (Act_Id);
9040 Body_Id : constant Node_Id :=
9041 Corresponding_Body (Unit_Declaration_Node (Scop));
9042
9043 begin
9044 Ensure_Freeze_Node (Act_Id);
9045 F_Node := Freeze_Node (Act_Id);
9046 if Present (Body_Id) then
9047 Set_Is_Frozen (Act_Id, False);
9048 Prepend (Act_Body, Declarations (Parent (Body_Id)));
9049 if Is_List_Member (F_Node) then
9050 Remove (F_Node);
9051 end if;
9052
9053 Insert_After (Act_Body, F_Node);
9054 end if;
9055 end;
9056 return;
9057 end if;
9058
9059 -- If the body is a subunit, the freeze point is the corresponding stub
9060 -- in the current compilation, not the subunit itself.
9061
9062 if Nkind (Parent (Gen_Body)) = N_Subunit then
9063 Orig_Body := Corresponding_Stub (Parent (Gen_Body));
9064 else
9065 Orig_Body := Gen_Body;
9066 end if;
9067
9068 Body_Unit := Unit (Cunit (Get_Source_Unit (Orig_Body)));
9069
9070 -- If the instantiation and the generic definition appear in the same
9071 -- package declaration, this is an early instantiation. If they appear
9072 -- in the same declarative part, it is an early instantiation only if
9073 -- the generic body appears textually later, and the generic body is
9074 -- also in the main unit.
9075
9076 -- If instance is nested within a subprogram, and the generic body
9077 -- is not, the instance is delayed because the enclosing body is. If
9078 -- instance and body are within the same scope, or the same subprogram
9079 -- body, indicate explicitly that the instance is delayed.
9080
9081 Must_Delay :=
9082 (Gen_Unit = Act_Unit
9083 and then (Nkind_In (Gen_Unit, N_Package_Declaration,
9084 N_Generic_Package_Declaration)
9085 or else (Gen_Unit = Body_Unit
9086 and then True_Sloc (N, Act_Unit)
9087 < Sloc (Orig_Body)))
9088 and then Is_In_Main_Unit (Original_Node (Gen_Unit))
9089 and then (In_Same_Scope (Gen_Id, Act_Id)));
9090
9091 -- If this is an early instantiation, the freeze node is placed after
9092 -- the generic body. Otherwise, if the generic appears in an instance,
9093 -- we cannot freeze the current instance until the outer one is frozen.
9094 -- This is only relevant if the current instance is nested within some
9095 -- inner scope not itself within the outer instance. If this scope is
9096 -- a package body in the same declarative part as the outer instance,
9097 -- then that body needs to be frozen after the outer instance. Finally,
9098 -- if no delay is needed, we place the freeze node at the end of the
9099 -- current declarative part.
9100
9101 if Expander_Active then
9102 Ensure_Freeze_Node (Act_Id);
9103 F_Node := Freeze_Node (Act_Id);
9104
9105 if Must_Delay then
9106 Insert_After (Orig_Body, F_Node);
9107
9108 elsif Is_Generic_Instance (Par)
9109 and then Present (Freeze_Node (Par))
9110 and then Scope (Act_Id) /= Par
9111 then
9112 -- Freeze instance of inner generic after instance of enclosing
9113 -- generic.
9114
9115 if In_Same_Declarative_Part (Freeze_Node (Par), N) then
9116
9117 -- Handle the following case:
9118
9119 -- package Parent_Inst is new ...
9120 -- Parent_Inst []
9121
9122 -- procedure P ... -- this body freezes Parent_Inst
9123
9124 -- package Inst is new ...
9125
9126 -- In this particular scenario, the freeze node for Inst must
9127 -- be inserted in the same manner as that of Parent_Inst,
9128 -- before the next source body or at the end of the declarative
9129 -- list (body not available). If body P did not exist and
9130 -- Parent_Inst was frozen after Inst, either by a body
9131 -- following Inst or at the end of the declarative region,
9132 -- the freeze node for Inst must be inserted after that of
9133 -- Parent_Inst. This relation is established by comparing
9134 -- the Slocs of Parent_Inst freeze node and Inst.
9135
9136 if List_Containing (Get_Package_Instantiation_Node (Par)) =
9137 List_Containing (N)
9138 and then Sloc (Freeze_Node (Par)) < Sloc (N)
9139 then
9140 Insert_Freeze_Node_For_Instance (N, F_Node);
9141 else
9142 Insert_After (Freeze_Node (Par), F_Node);
9143 end if;
9144
9145 -- Freeze package enclosing instance of inner generic after
9146 -- instance of enclosing generic.
9147
9148 elsif Nkind_In (Parent (N), N_Package_Body, N_Subprogram_Body)
9149 and then In_Same_Declarative_Part (Freeze_Node (Par), Parent (N))
9150 then
9151 declare
9152 Enclosing : Entity_Id;
9153
9154 begin
9155 Enclosing := Corresponding_Spec (Parent (N));
9156
9157 if No (Enclosing) then
9158 Enclosing := Defining_Entity (Parent (N));
9159 end if;
9160
9161 Insert_Freeze_Node_For_Instance (N, F_Node);
9162 Ensure_Freeze_Node (Enclosing);
9163
9164 if not Is_List_Member (Freeze_Node (Enclosing)) then
9165
9166 -- The enclosing context is a subunit, insert the freeze
9167 -- node after the stub.
9168
9169 if Nkind (Parent (Parent (N))) = N_Subunit then
9170 Insert_Freeze_Node_For_Instance
9171 (Corresponding_Stub (Parent (Parent (N))),
9172 Freeze_Node (Enclosing));
9173
9174 -- The enclosing context is a package with a stub body
9175 -- which has already been replaced by the real body.
9176 -- Insert the freeze node after the actual body.
9177
9178 elsif Ekind (Enclosing) = E_Package
9179 and then Present (Body_Entity (Enclosing))
9180 and then Was_Originally_Stub
9181 (Parent (Body_Entity (Enclosing)))
9182 then
9183 Insert_Freeze_Node_For_Instance
9184 (Parent (Body_Entity (Enclosing)),
9185 Freeze_Node (Enclosing));
9186
9187 -- The parent instance has been frozen before the body of
9188 -- the enclosing package, insert the freeze node after
9189 -- the body.
9190
9191 elsif List_Containing (Freeze_Node (Par)) =
9192 List_Containing (Parent (N))
9193 and then Sloc (Freeze_Node (Par)) < Sloc (Parent (N))
9194 then
9195 Insert_Freeze_Node_For_Instance
9196 (Parent (N), Freeze_Node (Enclosing));
9197
9198 else
9199 Insert_After
9200 (Freeze_Node (Par), Freeze_Node (Enclosing));
9201 end if;
9202 end if;
9203 end;
9204
9205 else
9206 Insert_Freeze_Node_For_Instance (N, F_Node);
9207 end if;
9208
9209 else
9210 Insert_Freeze_Node_For_Instance (N, F_Node);
9211 end if;
9212 end if;
9213
9214 Set_Is_Frozen (Act_Id);
9215 Insert_Before (N, Act_Body);
9216 Mark_Rewrite_Insertion (Act_Body);
9217 end Install_Body;
9218
9219 -----------------------------
9220 -- Install_Formal_Packages --
9221 -----------------------------
9222
9223 procedure Install_Formal_Packages (Par : Entity_Id) is
9224 E : Entity_Id;
9225 Gen : Entity_Id;
9226 Gen_E : Entity_Id := Empty;
9227
9228 begin
9229 E := First_Entity (Par);
9230
9231 -- If we are installing an instance parent, locate the formal packages
9232 -- of its generic parent.
9233
9234 if Is_Generic_Instance (Par) then
9235 Gen := Generic_Parent (Package_Specification (Par));
9236 Gen_E := First_Entity (Gen);
9237 end if;
9238
9239 while Present (E) loop
9240 if Ekind (E) = E_Package
9241 and then Nkind (Parent (E)) = N_Package_Renaming_Declaration
9242 then
9243 -- If this is the renaming for the parent instance, done
9244
9245 if Renamed_Object (E) = Par then
9246 exit;
9247
9248 -- The visibility of a formal of an enclosing generic is already
9249 -- correct.
9250
9251 elsif Denotes_Formal_Package (E) then
9252 null;
9253
9254 elsif Present (Associated_Formal_Package (E)) then
9255 Check_Generic_Actuals (Renamed_Object (E), True);
9256 Set_Is_Hidden (E, False);
9257
9258 -- Find formal package in generic unit that corresponds to
9259 -- (instance of) formal package in instance.
9260
9261 while Present (Gen_E) and then Chars (Gen_E) /= Chars (E) loop
9262 Next_Entity (Gen_E);
9263 end loop;
9264
9265 if Present (Gen_E) then
9266 Map_Formal_Package_Entities (Gen_E, E);
9267 end if;
9268 end if;
9269 end if;
9270
9271 Next_Entity (E);
9272
9273 if Present (Gen_E) then
9274 Next_Entity (Gen_E);
9275 end if;
9276 end loop;
9277 end Install_Formal_Packages;
9278
9279 --------------------
9280 -- Install_Parent --
9281 --------------------
9282
9283 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False) is
9284 Ancestors : constant Elist_Id := New_Elmt_List;
9285 S : constant Entity_Id := Current_Scope;
9286 Inst_Par : Entity_Id;
9287 First_Par : Entity_Id;
9288 Inst_Node : Node_Id;
9289 Gen_Par : Entity_Id;
9290 First_Gen : Entity_Id;
9291 Elmt : Elmt_Id;
9292
9293 procedure Install_Noninstance_Specs (Par : Entity_Id);
9294 -- Install the scopes of noninstance parent units ending with Par
9295
9296 procedure Install_Spec (Par : Entity_Id);
9297 -- The child unit is within the declarative part of the parent, so the
9298 -- declarations within the parent are immediately visible.
9299
9300 -------------------------------
9301 -- Install_Noninstance_Specs --
9302 -------------------------------
9303
9304 procedure Install_Noninstance_Specs (Par : Entity_Id) is
9305 begin
9306 if Present (Par)
9307 and then Par /= Standard_Standard
9308 and then not In_Open_Scopes (Par)
9309 then
9310 Install_Noninstance_Specs (Scope (Par));
9311 Install_Spec (Par);
9312 end if;
9313 end Install_Noninstance_Specs;
9314
9315 ------------------
9316 -- Install_Spec --
9317 ------------------
9318
9319 procedure Install_Spec (Par : Entity_Id) is
9320 Spec : constant Node_Id := Package_Specification (Par);
9321
9322 begin
9323 -- If this parent of the child instance is a top-level unit,
9324 -- then record the unit and its visibility for later resetting in
9325 -- Remove_Parent. We exclude units that are generic instances, as we
9326 -- only want to record this information for the ultimate top-level
9327 -- noninstance parent (is that always correct???).
9328
9329 if Scope (Par) = Standard_Standard
9330 and then not Is_Generic_Instance (Par)
9331 then
9332 Parent_Unit_Visible := Is_Immediately_Visible (Par);
9333 Instance_Parent_Unit := Par;
9334 end if;
9335
9336 -- Open the parent scope and make it and its declarations visible.
9337 -- If this point is not within a body, then only the visible
9338 -- declarations should be made visible, and installation of the
9339 -- private declarations is deferred until the appropriate point
9340 -- within analysis of the spec being instantiated (see the handling
9341 -- of parent visibility in Analyze_Package_Specification). This is
9342 -- relaxed in the case where the parent unit is Ada.Tags, to avoid
9343 -- private view problems that occur when compiling instantiations of
9344 -- a generic child of that package (Generic_Dispatching_Constructor).
9345 -- If the instance freezes a tagged type, inlinings of operations
9346 -- from Ada.Tags may need the full view of type Tag. If inlining took
9347 -- proper account of establishing visibility of inlined subprograms'
9348 -- parents then it should be possible to remove this
9349 -- special check. ???
9350
9351 Push_Scope (Par);
9352 Set_Is_Immediately_Visible (Par);
9353 Install_Visible_Declarations (Par);
9354 Set_Use (Visible_Declarations (Spec));
9355
9356 if In_Body or else Is_RTU (Par, Ada_Tags) then
9357 Install_Private_Declarations (Par);
9358 Set_Use (Private_Declarations (Spec));
9359 end if;
9360 end Install_Spec;
9361
9362 -- Start of processing for Install_Parent
9363
9364 begin
9365 -- We need to install the parent instance to compile the instantiation
9366 -- of the child, but the child instance must appear in the current
9367 -- scope. Given that we cannot place the parent above the current scope
9368 -- in the scope stack, we duplicate the current scope and unstack both
9369 -- after the instantiation is complete.
9370
9371 -- If the parent is itself the instantiation of a child unit, we must
9372 -- also stack the instantiation of its parent, and so on. Each such
9373 -- ancestor is the prefix of the name in a prior instantiation.
9374
9375 -- If this is a nested instance, the parent unit itself resolves to
9376 -- a renaming of the parent instance, whose declaration we need.
9377
9378 -- Finally, the parent may be a generic (not an instance) when the
9379 -- child unit appears as a formal package.
9380
9381 Inst_Par := P;
9382
9383 if Present (Renamed_Entity (Inst_Par)) then
9384 Inst_Par := Renamed_Entity (Inst_Par);
9385 end if;
9386
9387 First_Par := Inst_Par;
9388
9389 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
9390
9391 First_Gen := Gen_Par;
9392
9393 while Present (Gen_Par) and then Is_Child_Unit (Gen_Par) loop
9394
9395 -- Load grandparent instance as well
9396
9397 Inst_Node := Get_Package_Instantiation_Node (Inst_Par);
9398
9399 if Nkind (Name (Inst_Node)) = N_Expanded_Name then
9400 Inst_Par := Entity (Prefix (Name (Inst_Node)));
9401
9402 if Present (Renamed_Entity (Inst_Par)) then
9403 Inst_Par := Renamed_Entity (Inst_Par);
9404 end if;
9405
9406 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
9407
9408 if Present (Gen_Par) then
9409 Prepend_Elmt (Inst_Par, Ancestors);
9410
9411 else
9412 -- Parent is not the name of an instantiation
9413
9414 Install_Noninstance_Specs (Inst_Par);
9415 exit;
9416 end if;
9417
9418 else
9419 -- Previous error
9420
9421 exit;
9422 end if;
9423 end loop;
9424
9425 if Present (First_Gen) then
9426 Append_Elmt (First_Par, Ancestors);
9427 else
9428 Install_Noninstance_Specs (First_Par);
9429 end if;
9430
9431 if not Is_Empty_Elmt_List (Ancestors) then
9432 Elmt := First_Elmt (Ancestors);
9433 while Present (Elmt) loop
9434 Install_Spec (Node (Elmt));
9435 Install_Formal_Packages (Node (Elmt));
9436 Next_Elmt (Elmt);
9437 end loop;
9438 end if;
9439
9440 if not In_Body then
9441 Push_Scope (S);
9442 end if;
9443 end Install_Parent;
9444
9445 -------------------------------
9446 -- Install_Hidden_Primitives --
9447 -------------------------------
9448
9449 procedure Install_Hidden_Primitives
9450 (Prims_List : in out Elist_Id;
9451 Gen_T : Entity_Id;
9452 Act_T : Entity_Id)
9453 is
9454 Elmt : Elmt_Id;
9455 List : Elist_Id := No_Elist;
9456 Prim_G_Elmt : Elmt_Id;
9457 Prim_A_Elmt : Elmt_Id;
9458 Prim_G : Node_Id;
9459 Prim_A : Node_Id;
9460
9461 begin
9462 -- No action needed in case of serious errors because we cannot trust
9463 -- in the order of primitives
9464
9465 if Serious_Errors_Detected > 0 then
9466 return;
9467
9468 -- No action possible if we don't have available the list of primitive
9469 -- operations
9470
9471 elsif No (Gen_T)
9472 or else not Is_Record_Type (Gen_T)
9473 or else not Is_Tagged_Type (Gen_T)
9474 or else not Is_Record_Type (Act_T)
9475 or else not Is_Tagged_Type (Act_T)
9476 then
9477 return;
9478
9479 -- There is no need to handle interface types since their primitives
9480 -- cannot be hidden
9481
9482 elsif Is_Interface (Gen_T) then
9483 return;
9484 end if;
9485
9486 Prim_G_Elmt := First_Elmt (Primitive_Operations (Gen_T));
9487
9488 if not Is_Class_Wide_Type (Act_T) then
9489 Prim_A_Elmt := First_Elmt (Primitive_Operations (Act_T));
9490 else
9491 Prim_A_Elmt := First_Elmt (Primitive_Operations (Root_Type (Act_T)));
9492 end if;
9493
9494 loop
9495 -- Skip predefined primitives in the generic formal
9496
9497 while Present (Prim_G_Elmt)
9498 and then Is_Predefined_Dispatching_Operation (Node (Prim_G_Elmt))
9499 loop
9500 Next_Elmt (Prim_G_Elmt);
9501 end loop;
9502
9503 -- Skip predefined primitives in the generic actual
9504
9505 while Present (Prim_A_Elmt)
9506 and then Is_Predefined_Dispatching_Operation (Node (Prim_A_Elmt))
9507 loop
9508 Next_Elmt (Prim_A_Elmt);
9509 end loop;
9510
9511 exit when No (Prim_G_Elmt) or else No (Prim_A_Elmt);
9512
9513 Prim_G := Node (Prim_G_Elmt);
9514 Prim_A := Node (Prim_A_Elmt);
9515
9516 -- There is no need to handle interface primitives because their
9517 -- primitives are not hidden
9518
9519 exit when Present (Interface_Alias (Prim_G));
9520
9521 -- Here we install one hidden primitive
9522
9523 if Chars (Prim_G) /= Chars (Prim_A)
9524 and then Has_Suffix (Prim_A, 'P')
9525 and then Remove_Suffix (Prim_A, 'P') = Chars (Prim_G)
9526 then
9527 Set_Chars (Prim_A, Chars (Prim_G));
9528 Append_New_Elmt (Prim_A, To => List);
9529 end if;
9530
9531 Next_Elmt (Prim_A_Elmt);
9532 Next_Elmt (Prim_G_Elmt);
9533 end loop;
9534
9535 -- Append the elements to the list of temporarily visible primitives
9536 -- avoiding duplicates.
9537
9538 if Present (List) then
9539 if No (Prims_List) then
9540 Prims_List := New_Elmt_List;
9541 end if;
9542
9543 Elmt := First_Elmt (List);
9544 while Present (Elmt) loop
9545 Append_Unique_Elmt (Node (Elmt), Prims_List);
9546 Next_Elmt (Elmt);
9547 end loop;
9548 end if;
9549 end Install_Hidden_Primitives;
9550
9551 -------------------------------
9552 -- Restore_Hidden_Primitives --
9553 -------------------------------
9554
9555 procedure Restore_Hidden_Primitives (Prims_List : in out Elist_Id) is
9556 Prim_Elmt : Elmt_Id;
9557 Prim : Node_Id;
9558
9559 begin
9560 if Prims_List /= No_Elist then
9561 Prim_Elmt := First_Elmt (Prims_List);
9562 while Present (Prim_Elmt) loop
9563 Prim := Node (Prim_Elmt);
9564 Set_Chars (Prim, Add_Suffix (Prim, 'P'));
9565 Next_Elmt (Prim_Elmt);
9566 end loop;
9567
9568 Prims_List := No_Elist;
9569 end if;
9570 end Restore_Hidden_Primitives;
9571
9572 --------------------------------
9573 -- Instantiate_Formal_Package --
9574 --------------------------------
9575
9576 function Instantiate_Formal_Package
9577 (Formal : Node_Id;
9578 Actual : Node_Id;
9579 Analyzed_Formal : Node_Id) return List_Id
9580 is
9581 Loc : constant Source_Ptr := Sloc (Actual);
9582 Actual_Pack : Entity_Id;
9583 Formal_Pack : Entity_Id;
9584 Gen_Parent : Entity_Id;
9585 Decls : List_Id;
9586 Nod : Node_Id;
9587 Parent_Spec : Node_Id;
9588
9589 procedure Find_Matching_Actual
9590 (F : Node_Id;
9591 Act : in out Entity_Id);
9592 -- We need to associate each formal entity in the formal package with
9593 -- the corresponding entity in the actual package. The actual package
9594 -- has been analyzed and possibly expanded, and as a result there is
9595 -- no one-to-one correspondence between the two lists (for example,
9596 -- the actual may include subtypes, itypes, and inherited primitive
9597 -- operations, interspersed among the renaming declarations for the
9598 -- actuals). We retrieve the corresponding actual by name because each
9599 -- actual has the same name as the formal, and they do appear in the
9600 -- same order.
9601
9602 function Get_Formal_Entity (N : Node_Id) return Entity_Id;
9603 -- Retrieve entity of defining entity of generic formal parameter.
9604 -- Only the declarations of formals need to be considered when
9605 -- linking them to actuals, but the declarative list may include
9606 -- internal entities generated during analysis, and those are ignored.
9607
9608 procedure Match_Formal_Entity
9609 (Formal_Node : Node_Id;
9610 Formal_Ent : Entity_Id;
9611 Actual_Ent : Entity_Id);
9612 -- Associates the formal entity with the actual. In the case where
9613 -- Formal_Ent is a formal package, this procedure iterates through all
9614 -- of its formals and enters associations between the actuals occurring
9615 -- in the formal package's corresponding actual package (given by
9616 -- Actual_Ent) and the formal package's formal parameters. This
9617 -- procedure recurses if any of the parameters is itself a package.
9618
9619 function Is_Instance_Of
9620 (Act_Spec : Entity_Id;
9621 Gen_Anc : Entity_Id) return Boolean;
9622 -- The actual can be an instantiation of a generic within another
9623 -- instance, in which case there is no direct link from it to the
9624 -- original generic ancestor. In that case, we recognize that the
9625 -- ultimate ancestor is the same by examining names and scopes.
9626
9627 procedure Process_Nested_Formal (Formal : Entity_Id);
9628 -- If the current formal is declared with a box, its own formals are
9629 -- visible in the instance, as they were in the generic, and their
9630 -- Hidden flag must be reset. If some of these formals are themselves
9631 -- packages declared with a box, the processing must be recursive.
9632
9633 --------------------------
9634 -- Find_Matching_Actual --
9635 --------------------------
9636
9637 procedure Find_Matching_Actual
9638 (F : Node_Id;
9639 Act : in out Entity_Id)
9640 is
9641 Formal_Ent : Entity_Id;
9642
9643 begin
9644 case Nkind (Original_Node (F)) is
9645 when N_Formal_Object_Declaration |
9646 N_Formal_Type_Declaration =>
9647 Formal_Ent := Defining_Identifier (F);
9648
9649 while Chars (Act) /= Chars (Formal_Ent) loop
9650 Next_Entity (Act);
9651 end loop;
9652
9653 when N_Formal_Subprogram_Declaration |
9654 N_Formal_Package_Declaration |
9655 N_Package_Declaration |
9656 N_Generic_Package_Declaration =>
9657 Formal_Ent := Defining_Entity (F);
9658
9659 while Chars (Act) /= Chars (Formal_Ent) loop
9660 Next_Entity (Act);
9661 end loop;
9662
9663 when others =>
9664 raise Program_Error;
9665 end case;
9666 end Find_Matching_Actual;
9667
9668 -------------------------
9669 -- Match_Formal_Entity --
9670 -------------------------
9671
9672 procedure Match_Formal_Entity
9673 (Formal_Node : Node_Id;
9674 Formal_Ent : Entity_Id;
9675 Actual_Ent : Entity_Id)
9676 is
9677 Act_Pkg : Entity_Id;
9678
9679 begin
9680 Set_Instance_Of (Formal_Ent, Actual_Ent);
9681
9682 if Ekind (Actual_Ent) = E_Package then
9683
9684 -- Record associations for each parameter
9685
9686 Act_Pkg := Actual_Ent;
9687
9688 declare
9689 A_Ent : Entity_Id := First_Entity (Act_Pkg);
9690 F_Ent : Entity_Id;
9691 F_Node : Node_Id;
9692
9693 Gen_Decl : Node_Id;
9694 Formals : List_Id;
9695 Actual : Entity_Id;
9696
9697 begin
9698 -- Retrieve the actual given in the formal package declaration
9699
9700 Actual := Entity (Name (Original_Node (Formal_Node)));
9701
9702 -- The actual in the formal package declaration may be a
9703 -- renamed generic package, in which case we want to retrieve
9704 -- the original generic in order to traverse its formal part.
9705
9706 if Present (Renamed_Entity (Actual)) then
9707 Gen_Decl := Unit_Declaration_Node (Renamed_Entity (Actual));
9708 else
9709 Gen_Decl := Unit_Declaration_Node (Actual);
9710 end if;
9711
9712 Formals := Generic_Formal_Declarations (Gen_Decl);
9713
9714 if Present (Formals) then
9715 F_Node := First_Non_Pragma (Formals);
9716 else
9717 F_Node := Empty;
9718 end if;
9719
9720 while Present (A_Ent)
9721 and then Present (F_Node)
9722 and then A_Ent /= First_Private_Entity (Act_Pkg)
9723 loop
9724 F_Ent := Get_Formal_Entity (F_Node);
9725
9726 if Present (F_Ent) then
9727
9728 -- This is a formal of the original package. Record
9729 -- association and recurse.
9730
9731 Find_Matching_Actual (F_Node, A_Ent);
9732 Match_Formal_Entity (F_Node, F_Ent, A_Ent);
9733 Next_Entity (A_Ent);
9734 end if;
9735
9736 Next_Non_Pragma (F_Node);
9737 end loop;
9738 end;
9739 end if;
9740 end Match_Formal_Entity;
9741
9742 -----------------------
9743 -- Get_Formal_Entity --
9744 -----------------------
9745
9746 function Get_Formal_Entity (N : Node_Id) return Entity_Id is
9747 Kind : constant Node_Kind := Nkind (Original_Node (N));
9748 begin
9749 case Kind is
9750 when N_Formal_Object_Declaration =>
9751 return Defining_Identifier (N);
9752
9753 when N_Formal_Type_Declaration =>
9754 return Defining_Identifier (N);
9755
9756 when N_Formal_Subprogram_Declaration =>
9757 return Defining_Unit_Name (Specification (N));
9758
9759 when N_Formal_Package_Declaration =>
9760 return Defining_Identifier (Original_Node (N));
9761
9762 when N_Generic_Package_Declaration =>
9763 return Defining_Identifier (Original_Node (N));
9764
9765 -- All other declarations are introduced by semantic analysis and
9766 -- have no match in the actual.
9767
9768 when others =>
9769 return Empty;
9770 end case;
9771 end Get_Formal_Entity;
9772
9773 --------------------
9774 -- Is_Instance_Of --
9775 --------------------
9776
9777 function Is_Instance_Of
9778 (Act_Spec : Entity_Id;
9779 Gen_Anc : Entity_Id) return Boolean
9780 is
9781 Gen_Par : constant Entity_Id := Generic_Parent (Act_Spec);
9782
9783 begin
9784 if No (Gen_Par) then
9785 return False;
9786
9787 -- Simplest case: the generic parent of the actual is the formal
9788
9789 elsif Gen_Par = Gen_Anc then
9790 return True;
9791
9792 elsif Chars (Gen_Par) /= Chars (Gen_Anc) then
9793 return False;
9794
9795 -- The actual may be obtained through several instantiations. Its
9796 -- scope must itself be an instance of a generic declared in the
9797 -- same scope as the formal. Any other case is detected above.
9798
9799 elsif not Is_Generic_Instance (Scope (Gen_Par)) then
9800 return False;
9801
9802 else
9803 return Generic_Parent (Parent (Scope (Gen_Par))) = Scope (Gen_Anc);
9804 end if;
9805 end Is_Instance_Of;
9806
9807 ---------------------------
9808 -- Process_Nested_Formal --
9809 ---------------------------
9810
9811 procedure Process_Nested_Formal (Formal : Entity_Id) is
9812 Ent : Entity_Id;
9813
9814 begin
9815 if Present (Associated_Formal_Package (Formal))
9816 and then Box_Present (Parent (Associated_Formal_Package (Formal)))
9817 then
9818 Ent := First_Entity (Formal);
9819 while Present (Ent) loop
9820 Set_Is_Hidden (Ent, False);
9821 Set_Is_Visible_Formal (Ent);
9822 Set_Is_Potentially_Use_Visible
9823 (Ent, Is_Potentially_Use_Visible (Formal));
9824
9825 if Ekind (Ent) = E_Package then
9826 exit when Renamed_Entity (Ent) = Renamed_Entity (Formal);
9827 Process_Nested_Formal (Ent);
9828 end if;
9829
9830 Next_Entity (Ent);
9831 end loop;
9832 end if;
9833 end Process_Nested_Formal;
9834
9835 -- Start of processing for Instantiate_Formal_Package
9836
9837 begin
9838 Analyze (Actual);
9839
9840 if not Is_Entity_Name (Actual)
9841 or else Ekind (Entity (Actual)) /= E_Package
9842 then
9843 Error_Msg_N
9844 ("expect package instance to instantiate formal", Actual);
9845 Abandon_Instantiation (Actual);
9846 raise Program_Error;
9847
9848 else
9849 Actual_Pack := Entity (Actual);
9850 Set_Is_Instantiated (Actual_Pack);
9851
9852 -- The actual may be a renamed package, or an outer generic formal
9853 -- package whose instantiation is converted into a renaming.
9854
9855 if Present (Renamed_Object (Actual_Pack)) then
9856 Actual_Pack := Renamed_Object (Actual_Pack);
9857 end if;
9858
9859 if Nkind (Analyzed_Formal) = N_Formal_Package_Declaration then
9860 Gen_Parent := Get_Instance_Of (Entity (Name (Analyzed_Formal)));
9861 Formal_Pack := Defining_Identifier (Analyzed_Formal);
9862 else
9863 Gen_Parent :=
9864 Generic_Parent (Specification (Analyzed_Formal));
9865 Formal_Pack :=
9866 Defining_Unit_Name (Specification (Analyzed_Formal));
9867 end if;
9868
9869 if Nkind (Parent (Actual_Pack)) = N_Defining_Program_Unit_Name then
9870 Parent_Spec := Package_Specification (Actual_Pack);
9871 else
9872 Parent_Spec := Parent (Actual_Pack);
9873 end if;
9874
9875 if Gen_Parent = Any_Id then
9876 Error_Msg_N
9877 ("previous error in declaration of formal package", Actual);
9878 Abandon_Instantiation (Actual);
9879
9880 elsif
9881 Is_Instance_Of (Parent_Spec, Get_Instance_Of (Gen_Parent))
9882 then
9883 null;
9884
9885 else
9886 Error_Msg_NE
9887 ("actual parameter must be instance of&", Actual, Gen_Parent);
9888 Abandon_Instantiation (Actual);
9889 end if;
9890
9891 Set_Instance_Of (Defining_Identifier (Formal), Actual_Pack);
9892 Map_Formal_Package_Entities (Formal_Pack, Actual_Pack);
9893
9894 Nod :=
9895 Make_Package_Renaming_Declaration (Loc,
9896 Defining_Unit_Name => New_Copy (Defining_Identifier (Formal)),
9897 Name => New_Occurrence_Of (Actual_Pack, Loc));
9898
9899 Set_Associated_Formal_Package
9900 (Defining_Unit_Name (Nod), Defining_Identifier (Formal));
9901 Decls := New_List (Nod);
9902
9903 -- If the formal F has a box, then the generic declarations are
9904 -- visible in the generic G. In an instance of G, the corresponding
9905 -- entities in the actual for F (which are the actuals for the
9906 -- instantiation of the generic that F denotes) must also be made
9907 -- visible for analysis of the current instance. On exit from the
9908 -- current instance, those entities are made private again. If the
9909 -- actual is currently in use, these entities are also use-visible.
9910
9911 -- The loop through the actual entities also steps through the formal
9912 -- entities and enters associations from formals to actuals into the
9913 -- renaming map. This is necessary to properly handle checking of
9914 -- actual parameter associations for later formals that depend on
9915 -- actuals declared in the formal package.
9916
9917 -- In Ada 2005, partial parameterization requires that we make
9918 -- visible the actuals corresponding to formals that were defaulted
9919 -- in the formal package. There formals are identified because they
9920 -- remain formal generics within the formal package, rather than
9921 -- being renamings of the actuals supplied.
9922
9923 declare
9924 Gen_Decl : constant Node_Id :=
9925 Unit_Declaration_Node (Gen_Parent);
9926 Formals : constant List_Id :=
9927 Generic_Formal_Declarations (Gen_Decl);
9928
9929 Actual_Ent : Entity_Id;
9930 Actual_Of_Formal : Node_Id;
9931 Formal_Node : Node_Id;
9932 Formal_Ent : Entity_Id;
9933
9934 begin
9935 if Present (Formals) then
9936 Formal_Node := First_Non_Pragma (Formals);
9937 else
9938 Formal_Node := Empty;
9939 end if;
9940
9941 Actual_Ent := First_Entity (Actual_Pack);
9942 Actual_Of_Formal :=
9943 First (Visible_Declarations (Specification (Analyzed_Formal)));
9944 while Present (Actual_Ent)
9945 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
9946 loop
9947 if Present (Formal_Node) then
9948 Formal_Ent := Get_Formal_Entity (Formal_Node);
9949
9950 if Present (Formal_Ent) then
9951 Find_Matching_Actual (Formal_Node, Actual_Ent);
9952 Match_Formal_Entity (Formal_Node, Formal_Ent, Actual_Ent);
9953
9954 -- We iterate at the same time over the actuals of the
9955 -- local package created for the formal, to determine
9956 -- which one of the formals of the original generic were
9957 -- defaulted in the formal. The corresponding actual
9958 -- entities are visible in the enclosing instance.
9959
9960 if Box_Present (Formal)
9961 or else
9962 (Present (Actual_Of_Formal)
9963 and then
9964 Is_Generic_Formal
9965 (Get_Formal_Entity (Actual_Of_Formal)))
9966 then
9967 Set_Is_Hidden (Actual_Ent, False);
9968 Set_Is_Visible_Formal (Actual_Ent);
9969 Set_Is_Potentially_Use_Visible
9970 (Actual_Ent, In_Use (Actual_Pack));
9971
9972 if Ekind (Actual_Ent) = E_Package then
9973 Process_Nested_Formal (Actual_Ent);
9974 end if;
9975
9976 else
9977 Set_Is_Hidden (Actual_Ent);
9978 Set_Is_Potentially_Use_Visible (Actual_Ent, False);
9979 end if;
9980 end if;
9981
9982 Next_Non_Pragma (Formal_Node);
9983 Next (Actual_Of_Formal);
9984
9985 else
9986 -- No further formals to match, but the generic part may
9987 -- contain inherited operation that are not hidden in the
9988 -- enclosing instance.
9989
9990 Next_Entity (Actual_Ent);
9991 end if;
9992 end loop;
9993
9994 -- Inherited subprograms generated by formal derived types are
9995 -- also visible if the types are.
9996
9997 Actual_Ent := First_Entity (Actual_Pack);
9998 while Present (Actual_Ent)
9999 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
10000 loop
10001 if Is_Overloadable (Actual_Ent)
10002 and then
10003 Nkind (Parent (Actual_Ent)) = N_Subtype_Declaration
10004 and then
10005 not Is_Hidden (Defining_Identifier (Parent (Actual_Ent)))
10006 then
10007 Set_Is_Hidden (Actual_Ent, False);
10008 Set_Is_Potentially_Use_Visible
10009 (Actual_Ent, In_Use (Actual_Pack));
10010 end if;
10011
10012 Next_Entity (Actual_Ent);
10013 end loop;
10014 end;
10015
10016 -- If the formal is not declared with a box, reanalyze it as an
10017 -- abbreviated instantiation, to verify the matching rules of 12.7.
10018 -- The actual checks are performed after the generic associations
10019 -- have been analyzed, to guarantee the same visibility for this
10020 -- instantiation and for the actuals.
10021
10022 -- In Ada 2005, the generic associations for the formal can include
10023 -- defaulted parameters. These are ignored during check. This
10024 -- internal instantiation is removed from the tree after conformance
10025 -- checking, because it contains formal declarations for those
10026 -- defaulted parameters, and those should not reach the back-end.
10027
10028 if not Box_Present (Formal) then
10029 declare
10030 I_Pack : constant Entity_Id :=
10031 Make_Temporary (Sloc (Actual), 'P');
10032
10033 begin
10034 Set_Is_Internal (I_Pack);
10035
10036 Append_To (Decls,
10037 Make_Package_Instantiation (Sloc (Actual),
10038 Defining_Unit_Name => I_Pack,
10039 Name =>
10040 New_Occurrence_Of
10041 (Get_Instance_Of (Gen_Parent), Sloc (Actual)),
10042 Generic_Associations => Generic_Associations (Formal)));
10043 end;
10044 end if;
10045
10046 return Decls;
10047 end if;
10048 end Instantiate_Formal_Package;
10049
10050 -----------------------------------
10051 -- Instantiate_Formal_Subprogram --
10052 -----------------------------------
10053
10054 function Instantiate_Formal_Subprogram
10055 (Formal : Node_Id;
10056 Actual : Node_Id;
10057 Analyzed_Formal : Node_Id) return Node_Id
10058 is
10059 Analyzed_S : constant Entity_Id :=
10060 Defining_Unit_Name (Specification (Analyzed_Formal));
10061 Formal_Sub : constant Entity_Id :=
10062 Defining_Unit_Name (Specification (Formal));
10063
10064 function From_Parent_Scope (Subp : Entity_Id) return Boolean;
10065 -- If the generic is a child unit, the parent has been installed on the
10066 -- scope stack, but a default subprogram cannot resolve to something
10067 -- on the parent because that parent is not really part of the visible
10068 -- context (it is there to resolve explicit local entities). If the
10069 -- default has resolved in this way, we remove the entity from immediate
10070 -- visibility and analyze the node again to emit an error message or
10071 -- find another visible candidate.
10072
10073 procedure Valid_Actual_Subprogram (Act : Node_Id);
10074 -- Perform legality check and raise exception on failure
10075
10076 -----------------------
10077 -- From_Parent_Scope --
10078 -----------------------
10079
10080 function From_Parent_Scope (Subp : Entity_Id) return Boolean is
10081 Gen_Scope : Node_Id;
10082
10083 begin
10084 Gen_Scope := Scope (Analyzed_S);
10085 while Present (Gen_Scope) and then Is_Child_Unit (Gen_Scope) loop
10086 if Scope (Subp) = Scope (Gen_Scope) then
10087 return True;
10088 end if;
10089
10090 Gen_Scope := Scope (Gen_Scope);
10091 end loop;
10092
10093 return False;
10094 end From_Parent_Scope;
10095
10096 -----------------------------
10097 -- Valid_Actual_Subprogram --
10098 -----------------------------
10099
10100 procedure Valid_Actual_Subprogram (Act : Node_Id) is
10101 Act_E : Entity_Id;
10102
10103 begin
10104 if Is_Entity_Name (Act) then
10105 Act_E := Entity (Act);
10106
10107 elsif Nkind (Act) = N_Selected_Component
10108 and then Is_Entity_Name (Selector_Name (Act))
10109 then
10110 Act_E := Entity (Selector_Name (Act));
10111
10112 else
10113 Act_E := Empty;
10114 end if;
10115
10116 if (Present (Act_E) and then Is_Overloadable (Act_E))
10117 or else Nkind_In (Act, N_Attribute_Reference,
10118 N_Indexed_Component,
10119 N_Character_Literal,
10120 N_Explicit_Dereference)
10121 then
10122 return;
10123 end if;
10124
10125 Error_Msg_NE
10126 ("expect subprogram or entry name in instantiation of &",
10127 Instantiation_Node, Formal_Sub);
10128 Abandon_Instantiation (Instantiation_Node);
10129 end Valid_Actual_Subprogram;
10130
10131 -- Local variables
10132
10133 Decl_Node : Node_Id;
10134 Loc : Source_Ptr;
10135 Nam : Node_Id;
10136 New_Spec : Node_Id;
10137 New_Subp : Entity_Id;
10138
10139 -- Start of processing for Instantiate_Formal_Subprogram
10140
10141 begin
10142 New_Spec := New_Copy_Tree (Specification (Formal));
10143
10144 -- The tree copy has created the proper instantiation sloc for the
10145 -- new specification. Use this location for all other constructed
10146 -- declarations.
10147
10148 Loc := Sloc (Defining_Unit_Name (New_Spec));
10149
10150 -- Create new entity for the actual (New_Copy_Tree does not), and
10151 -- indicate that it is an actual.
10152
10153 New_Subp := Make_Defining_Identifier (Loc, Chars (Formal_Sub));
10154 Set_Ekind (New_Subp, Ekind (Analyzed_S));
10155 Set_Is_Generic_Actual_Subprogram (New_Subp);
10156 Set_Defining_Unit_Name (New_Spec, New_Subp);
10157
10158 -- Create new entities for the each of the formals in the specification
10159 -- of the renaming declaration built for the actual.
10160
10161 if Present (Parameter_Specifications (New_Spec)) then
10162 declare
10163 F : Node_Id;
10164 F_Id : Entity_Id;
10165
10166 begin
10167 F := First (Parameter_Specifications (New_Spec));
10168 while Present (F) loop
10169 F_Id := Defining_Identifier (F);
10170
10171 Set_Defining_Identifier (F,
10172 Make_Defining_Identifier (Sloc (F_Id), Chars (F_Id)));
10173 Next (F);
10174 end loop;
10175 end;
10176 end if;
10177
10178 -- Find entity of actual. If the actual is an attribute reference, it
10179 -- cannot be resolved here (its formal is missing) but is handled
10180 -- instead in Attribute_Renaming. If the actual is overloaded, it is
10181 -- fully resolved subsequently, when the renaming declaration for the
10182 -- formal is analyzed. If it is an explicit dereference, resolve the
10183 -- prefix but not the actual itself, to prevent interpretation as call.
10184
10185 if Present (Actual) then
10186 Loc := Sloc (Actual);
10187 Set_Sloc (New_Spec, Loc);
10188
10189 if Nkind (Actual) = N_Operator_Symbol then
10190 Find_Direct_Name (Actual);
10191
10192 elsif Nkind (Actual) = N_Explicit_Dereference then
10193 Analyze (Prefix (Actual));
10194
10195 elsif Nkind (Actual) /= N_Attribute_Reference then
10196 Analyze (Actual);
10197 end if;
10198
10199 Valid_Actual_Subprogram (Actual);
10200 Nam := Actual;
10201
10202 elsif Present (Default_Name (Formal)) then
10203 if not Nkind_In (Default_Name (Formal), N_Attribute_Reference,
10204 N_Selected_Component,
10205 N_Indexed_Component,
10206 N_Character_Literal)
10207 and then Present (Entity (Default_Name (Formal)))
10208 then
10209 Nam := New_Occurrence_Of (Entity (Default_Name (Formal)), Loc);
10210 else
10211 Nam := New_Copy (Default_Name (Formal));
10212 Set_Sloc (Nam, Loc);
10213 end if;
10214
10215 elsif Box_Present (Formal) then
10216
10217 -- Actual is resolved at the point of instantiation. Create an
10218 -- identifier or operator with the same name as the formal.
10219
10220 if Nkind (Formal_Sub) = N_Defining_Operator_Symbol then
10221 Nam :=
10222 Make_Operator_Symbol (Loc,
10223 Chars => Chars (Formal_Sub),
10224 Strval => No_String);
10225 else
10226 Nam := Make_Identifier (Loc, Chars (Formal_Sub));
10227 end if;
10228
10229 elsif Nkind (Specification (Formal)) = N_Procedure_Specification
10230 and then Null_Present (Specification (Formal))
10231 then
10232 -- Generate null body for procedure, for use in the instance
10233
10234 Decl_Node :=
10235 Make_Subprogram_Body (Loc,
10236 Specification => New_Spec,
10237 Declarations => New_List,
10238 Handled_Statement_Sequence =>
10239 Make_Handled_Sequence_Of_Statements (Loc,
10240 Statements => New_List (Make_Null_Statement (Loc))));
10241
10242 Set_Is_Intrinsic_Subprogram (Defining_Unit_Name (New_Spec));
10243 return Decl_Node;
10244
10245 else
10246 Error_Msg_Sloc := Sloc (Scope (Analyzed_S));
10247 Error_Msg_NE
10248 ("missing actual&", Instantiation_Node, Formal_Sub);
10249 Error_Msg_NE
10250 ("\in instantiation of & declared#",
10251 Instantiation_Node, Scope (Analyzed_S));
10252 Abandon_Instantiation (Instantiation_Node);
10253 end if;
10254
10255 Decl_Node :=
10256 Make_Subprogram_Renaming_Declaration (Loc,
10257 Specification => New_Spec,
10258 Name => Nam);
10259
10260 -- If we do not have an actual and the formal specified <> then set to
10261 -- get proper default.
10262
10263 if No (Actual) and then Box_Present (Formal) then
10264 Set_From_Default (Decl_Node);
10265 end if;
10266
10267 -- Gather possible interpretations for the actual before analyzing the
10268 -- instance. If overloaded, it will be resolved when analyzing the
10269 -- renaming declaration.
10270
10271 if Box_Present (Formal) and then No (Actual) then
10272 Analyze (Nam);
10273
10274 if Is_Child_Unit (Scope (Analyzed_S))
10275 and then Present (Entity (Nam))
10276 then
10277 if not Is_Overloaded (Nam) then
10278 if From_Parent_Scope (Entity (Nam)) then
10279 Set_Is_Immediately_Visible (Entity (Nam), False);
10280 Set_Entity (Nam, Empty);
10281 Set_Etype (Nam, Empty);
10282
10283 Analyze (Nam);
10284 Set_Is_Immediately_Visible (Entity (Nam));
10285 end if;
10286
10287 else
10288 declare
10289 I : Interp_Index;
10290 It : Interp;
10291
10292 begin
10293 Get_First_Interp (Nam, I, It);
10294 while Present (It.Nam) loop
10295 if From_Parent_Scope (It.Nam) then
10296 Remove_Interp (I);
10297 end if;
10298
10299 Get_Next_Interp (I, It);
10300 end loop;
10301 end;
10302 end if;
10303 end if;
10304 end if;
10305
10306 -- The generic instantiation freezes the actual. This can only be done
10307 -- once the actual is resolved, in the analysis of the renaming
10308 -- declaration. To make the formal subprogram entity available, we set
10309 -- Corresponding_Formal_Spec to point to the formal subprogram entity.
10310 -- This is also needed in Analyze_Subprogram_Renaming for the processing
10311 -- of formal abstract subprograms.
10312
10313 Set_Corresponding_Formal_Spec (Decl_Node, Analyzed_S);
10314
10315 -- We cannot analyze the renaming declaration, and thus find the actual,
10316 -- until all the actuals are assembled in the instance. For subsequent
10317 -- checks of other actuals, indicate the node that will hold the
10318 -- instance of this formal.
10319
10320 Set_Instance_Of (Analyzed_S, Nam);
10321
10322 if Nkind (Actual) = N_Selected_Component
10323 and then Is_Task_Type (Etype (Prefix (Actual)))
10324 and then not Is_Frozen (Etype (Prefix (Actual)))
10325 then
10326 -- The renaming declaration will create a body, which must appear
10327 -- outside of the instantiation, We move the renaming declaration
10328 -- out of the instance, and create an additional renaming inside,
10329 -- to prevent freezing anomalies.
10330
10331 declare
10332 Anon_Id : constant Entity_Id := Make_Temporary (Loc, 'E');
10333
10334 begin
10335 Set_Defining_Unit_Name (New_Spec, Anon_Id);
10336 Insert_Before (Instantiation_Node, Decl_Node);
10337 Analyze (Decl_Node);
10338
10339 -- Now create renaming within the instance
10340
10341 Decl_Node :=
10342 Make_Subprogram_Renaming_Declaration (Loc,
10343 Specification => New_Copy_Tree (New_Spec),
10344 Name => New_Occurrence_Of (Anon_Id, Loc));
10345
10346 Set_Defining_Unit_Name (Specification (Decl_Node),
10347 Make_Defining_Identifier (Loc, Chars (Formal_Sub)));
10348 end;
10349 end if;
10350
10351 return Decl_Node;
10352 end Instantiate_Formal_Subprogram;
10353
10354 ------------------------
10355 -- Instantiate_Object --
10356 ------------------------
10357
10358 function Instantiate_Object
10359 (Formal : Node_Id;
10360 Actual : Node_Id;
10361 Analyzed_Formal : Node_Id) return List_Id
10362 is
10363 Gen_Obj : constant Entity_Id := Defining_Identifier (Formal);
10364 A_Gen_Obj : constant Entity_Id :=
10365 Defining_Identifier (Analyzed_Formal);
10366 Acc_Def : Node_Id := Empty;
10367 Act_Assoc : constant Node_Id := Parent (Actual);
10368 Actual_Decl : Node_Id := Empty;
10369 Decl_Node : Node_Id;
10370 Def : Node_Id;
10371 Ftyp : Entity_Id;
10372 List : constant List_Id := New_List;
10373 Loc : constant Source_Ptr := Sloc (Actual);
10374 Orig_Ftyp : constant Entity_Id := Etype (A_Gen_Obj);
10375 Subt_Decl : Node_Id := Empty;
10376 Subt_Mark : Node_Id := Empty;
10377
10378 function Copy_Access_Def return Node_Id;
10379 -- If formal is an anonymous access, copy access definition of formal
10380 -- for generated object declaration.
10381
10382 ---------------------
10383 -- Copy_Access_Def --
10384 ---------------------
10385
10386 function Copy_Access_Def return Node_Id is
10387 begin
10388 Def := New_Copy_Tree (Acc_Def);
10389
10390 -- In addition, if formal is an access to subprogram we need to
10391 -- generate new formals for the signature of the default, so that
10392 -- the tree is properly formatted for ASIS use.
10393
10394 if Present (Access_To_Subprogram_Definition (Acc_Def)) then
10395 declare
10396 Par_Spec : Node_Id;
10397 begin
10398 Par_Spec :=
10399 First (Parameter_Specifications
10400 (Access_To_Subprogram_Definition (Def)));
10401 while Present (Par_Spec) loop
10402 Set_Defining_Identifier (Par_Spec,
10403 Make_Defining_Identifier (Sloc (Acc_Def),
10404 Chars => Chars (Defining_Identifier (Par_Spec))));
10405 Next (Par_Spec);
10406 end loop;
10407 end;
10408 end if;
10409
10410 return Def;
10411 end Copy_Access_Def;
10412
10413 -- Start of processing for Instantiate_Object
10414
10415 begin
10416 -- Formal may be an anonymous access
10417
10418 if Present (Subtype_Mark (Formal)) then
10419 Subt_Mark := Subtype_Mark (Formal);
10420 else
10421 Check_Access_Definition (Formal);
10422 Acc_Def := Access_Definition (Formal);
10423 end if;
10424
10425 -- Sloc for error message on missing actual
10426
10427 Error_Msg_Sloc := Sloc (Scope (A_Gen_Obj));
10428
10429 if Get_Instance_Of (Gen_Obj) /= Gen_Obj then
10430 Error_Msg_N ("duplicate instantiation of generic parameter", Actual);
10431 end if;
10432
10433 Set_Parent (List, Parent (Actual));
10434
10435 -- OUT present
10436
10437 if Out_Present (Formal) then
10438
10439 -- An IN OUT generic actual must be a name. The instantiation is a
10440 -- renaming declaration. The actual is the name being renamed. We
10441 -- use the actual directly, rather than a copy, because it is not
10442 -- used further in the list of actuals, and because a copy or a use
10443 -- of relocate_node is incorrect if the instance is nested within a
10444 -- generic. In order to simplify ASIS searches, the Generic_Parent
10445 -- field links the declaration to the generic association.
10446
10447 if No (Actual) then
10448 Error_Msg_NE
10449 ("missing actual &",
10450 Instantiation_Node, Gen_Obj);
10451 Error_Msg_NE
10452 ("\in instantiation of & declared#",
10453 Instantiation_Node, Scope (A_Gen_Obj));
10454 Abandon_Instantiation (Instantiation_Node);
10455 end if;
10456
10457 if Present (Subt_Mark) then
10458 Decl_Node :=
10459 Make_Object_Renaming_Declaration (Loc,
10460 Defining_Identifier => New_Copy (Gen_Obj),
10461 Subtype_Mark => New_Copy_Tree (Subt_Mark),
10462 Name => Actual);
10463
10464 else pragma Assert (Present (Acc_Def));
10465 Decl_Node :=
10466 Make_Object_Renaming_Declaration (Loc,
10467 Defining_Identifier => New_Copy (Gen_Obj),
10468 Access_Definition => New_Copy_Tree (Acc_Def),
10469 Name => Actual);
10470 end if;
10471
10472 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
10473
10474 -- The analysis of the actual may produce Insert_Action nodes, so
10475 -- the declaration must have a context in which to attach them.
10476
10477 Append (Decl_Node, List);
10478 Analyze (Actual);
10479
10480 -- Return if the analysis of the actual reported some error
10481
10482 if Etype (Actual) = Any_Type then
10483 return List;
10484 end if;
10485
10486 -- This check is performed here because Analyze_Object_Renaming will
10487 -- not check it when Comes_From_Source is False. Note though that the
10488 -- check for the actual being the name of an object will be performed
10489 -- in Analyze_Object_Renaming.
10490
10491 if Is_Object_Reference (Actual)
10492 and then Is_Dependent_Component_Of_Mutable_Object (Actual)
10493 then
10494 Error_Msg_N
10495 ("illegal discriminant-dependent component for in out parameter",
10496 Actual);
10497 end if;
10498
10499 -- The actual has to be resolved in order to check that it is a
10500 -- variable (due to cases such as F (1), where F returns access to
10501 -- an array, and for overloaded prefixes).
10502
10503 Ftyp := Get_Instance_Of (Etype (A_Gen_Obj));
10504
10505 -- If the type of the formal is not itself a formal, and the current
10506 -- unit is a child unit, the formal type must be declared in a
10507 -- parent, and must be retrieved by visibility.
10508
10509 if Ftyp = Orig_Ftyp
10510 and then Is_Generic_Unit (Scope (Ftyp))
10511 and then Is_Child_Unit (Scope (A_Gen_Obj))
10512 then
10513 declare
10514 Temp : constant Node_Id :=
10515 New_Copy_Tree (Subtype_Mark (Analyzed_Formal));
10516 begin
10517 Set_Entity (Temp, Empty);
10518 Find_Type (Temp);
10519 Ftyp := Entity (Temp);
10520 end;
10521 end if;
10522
10523 if Is_Private_Type (Ftyp)
10524 and then not Is_Private_Type (Etype (Actual))
10525 and then (Base_Type (Full_View (Ftyp)) = Base_Type (Etype (Actual))
10526 or else Base_Type (Etype (Actual)) = Ftyp)
10527 then
10528 -- If the actual has the type of the full view of the formal, or
10529 -- else a non-private subtype of the formal, then the visibility
10530 -- of the formal type has changed. Add to the actuals a subtype
10531 -- declaration that will force the exchange of views in the body
10532 -- of the instance as well.
10533
10534 Subt_Decl :=
10535 Make_Subtype_Declaration (Loc,
10536 Defining_Identifier => Make_Temporary (Loc, 'P'),
10537 Subtype_Indication => New_Occurrence_Of (Ftyp, Loc));
10538
10539 Prepend (Subt_Decl, List);
10540
10541 Prepend_Elmt (Full_View (Ftyp), Exchanged_Views);
10542 Exchange_Declarations (Ftyp);
10543 end if;
10544
10545 Resolve (Actual, Ftyp);
10546
10547 if not Denotes_Variable (Actual) then
10548 Error_Msg_NE ("actual for& must be a variable", Actual, Gen_Obj);
10549
10550 elsif Base_Type (Ftyp) /= Base_Type (Etype (Actual)) then
10551
10552 -- Ada 2005 (AI-423): For a generic formal object of mode in out,
10553 -- the type of the actual shall resolve to a specific anonymous
10554 -- access type.
10555
10556 if Ada_Version < Ada_2005
10557 or else Ekind (Base_Type (Ftyp)) /=
10558 E_Anonymous_Access_Type
10559 or else Ekind (Base_Type (Etype (Actual))) /=
10560 E_Anonymous_Access_Type
10561 then
10562 Error_Msg_NE
10563 ("type of actual does not match type of&", Actual, Gen_Obj);
10564 end if;
10565 end if;
10566
10567 Note_Possible_Modification (Actual, Sure => True);
10568
10569 -- Check for instantiation of atomic/volatile actual for
10570 -- non-atomic/volatile formal (RM C.6 (12)).
10571
10572 if Is_Atomic_Object (Actual) and then not Is_Atomic (Orig_Ftyp) then
10573 Error_Msg_N
10574 ("cannot instantiate non-atomic formal object "
10575 & "with atomic actual", Actual);
10576
10577 elsif Is_Volatile_Object (Actual) and then not Is_Volatile (Orig_Ftyp)
10578 then
10579 Error_Msg_N
10580 ("cannot instantiate non-volatile formal object "
10581 & "with volatile actual", Actual);
10582 end if;
10583
10584 -- Formal in-parameter
10585
10586 else
10587 -- The instantiation of a generic formal in-parameter is constant
10588 -- declaration. The actual is the expression for that declaration.
10589 -- Its type is a full copy of the type of the formal. This may be
10590 -- an access to subprogram, for which we need to generate entities
10591 -- for the formals in the new signature.
10592
10593 if Present (Actual) then
10594 if Present (Subt_Mark) then
10595 Def := New_Copy_Tree (Subt_Mark);
10596 else pragma Assert (Present (Acc_Def));
10597 Def := Copy_Access_Def;
10598 end if;
10599
10600 Decl_Node :=
10601 Make_Object_Declaration (Loc,
10602 Defining_Identifier => New_Copy (Gen_Obj),
10603 Constant_Present => True,
10604 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
10605 Object_Definition => Def,
10606 Expression => Actual);
10607
10608 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
10609
10610 -- A generic formal object of a tagged type is defined to be
10611 -- aliased so the new constant must also be treated as aliased.
10612
10613 if Is_Tagged_Type (Etype (A_Gen_Obj)) then
10614 Set_Aliased_Present (Decl_Node);
10615 end if;
10616
10617 Append (Decl_Node, List);
10618
10619 -- No need to repeat (pre-)analysis of some expression nodes
10620 -- already handled in Preanalyze_Actuals.
10621
10622 if Nkind (Actual) /= N_Allocator then
10623 Analyze (Actual);
10624
10625 -- Return if the analysis of the actual reported some error
10626
10627 if Etype (Actual) = Any_Type then
10628 return List;
10629 end if;
10630 end if;
10631
10632 declare
10633 Formal_Type : constant Entity_Id := Etype (A_Gen_Obj);
10634 Typ : Entity_Id;
10635
10636 begin
10637 Typ := Get_Instance_Of (Formal_Type);
10638
10639 -- If the actual appears in the current or an enclosing scope,
10640 -- use its type directly. This is relevant if it has an actual
10641 -- subtype that is distinct from its nominal one. This cannot
10642 -- be done in general because the type of the actual may
10643 -- depend on other actuals, and only be fully determined when
10644 -- the enclosing instance is analyzed.
10645
10646 if Present (Etype (Actual))
10647 and then Is_Constr_Subt_For_U_Nominal (Etype (Actual))
10648 then
10649 Freeze_Before (Instantiation_Node, Etype (Actual));
10650 else
10651 Freeze_Before (Instantiation_Node, Typ);
10652 end if;
10653
10654 -- If the actual is an aggregate, perform name resolution on
10655 -- its components (the analysis of an aggregate does not do it)
10656 -- to capture local names that may be hidden if the generic is
10657 -- a child unit.
10658
10659 if Nkind (Actual) = N_Aggregate then
10660 Preanalyze_And_Resolve (Actual, Typ);
10661 end if;
10662
10663 if Is_Limited_Type (Typ)
10664 and then not OK_For_Limited_Init (Typ, Actual)
10665 then
10666 Error_Msg_N
10667 ("initialization not allowed for limited types", Actual);
10668 Explain_Limited_Type (Typ, Actual);
10669 end if;
10670 end;
10671
10672 elsif Present (Default_Expression (Formal)) then
10673
10674 -- Use default to construct declaration
10675
10676 if Present (Subt_Mark) then
10677 Def := New_Copy (Subt_Mark);
10678 else pragma Assert (Present (Acc_Def));
10679 Def := Copy_Access_Def;
10680 end if;
10681
10682 Decl_Node :=
10683 Make_Object_Declaration (Sloc (Formal),
10684 Defining_Identifier => New_Copy (Gen_Obj),
10685 Constant_Present => True,
10686 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
10687 Object_Definition => Def,
10688 Expression => New_Copy_Tree
10689 (Default_Expression (Formal)));
10690
10691 Append (Decl_Node, List);
10692 Set_Analyzed (Expression (Decl_Node), False);
10693
10694 else
10695 Error_Msg_NE ("missing actual&", Instantiation_Node, Gen_Obj);
10696 Error_Msg_NE ("\in instantiation of & declared#",
10697 Instantiation_Node, Scope (A_Gen_Obj));
10698
10699 if Is_Scalar_Type (Etype (A_Gen_Obj)) then
10700
10701 -- Create dummy constant declaration so that instance can be
10702 -- analyzed, to minimize cascaded visibility errors.
10703
10704 if Present (Subt_Mark) then
10705 Def := Subt_Mark;
10706 else pragma Assert (Present (Acc_Def));
10707 Def := Acc_Def;
10708 end if;
10709
10710 Decl_Node :=
10711 Make_Object_Declaration (Loc,
10712 Defining_Identifier => New_Copy (Gen_Obj),
10713 Constant_Present => True,
10714 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
10715 Object_Definition => New_Copy (Def),
10716 Expression =>
10717 Make_Attribute_Reference (Sloc (Gen_Obj),
10718 Attribute_Name => Name_First,
10719 Prefix => New_Copy (Def)));
10720
10721 Append (Decl_Node, List);
10722
10723 else
10724 Abandon_Instantiation (Instantiation_Node);
10725 end if;
10726 end if;
10727 end if;
10728
10729 if Nkind (Actual) in N_Has_Entity then
10730 Actual_Decl := Parent (Entity (Actual));
10731 end if;
10732
10733 -- Ada 2005 (AI-423): For a formal object declaration with a null
10734 -- exclusion or an access definition that has a null exclusion: If the
10735 -- actual matching the formal object declaration denotes a generic
10736 -- formal object of another generic unit G, and the instantiation
10737 -- containing the actual occurs within the body of G or within the body
10738 -- of a generic unit declared within the declarative region of G, then
10739 -- the declaration of the formal object of G must have a null exclusion.
10740 -- Otherwise, the subtype of the actual matching the formal object
10741 -- declaration shall exclude null.
10742
10743 if Ada_Version >= Ada_2005
10744 and then Present (Actual_Decl)
10745 and then Nkind_In (Actual_Decl, N_Formal_Object_Declaration,
10746 N_Object_Declaration)
10747 and then Nkind (Analyzed_Formal) = N_Formal_Object_Declaration
10748 and then not Has_Null_Exclusion (Actual_Decl)
10749 and then Has_Null_Exclusion (Analyzed_Formal)
10750 then
10751 Error_Msg_Sloc := Sloc (Analyzed_Formal);
10752 Error_Msg_N
10753 ("actual must exclude null to match generic formal#", Actual);
10754 end if;
10755
10756 -- An effectively volatile object cannot be used as an actual in a
10757 -- generic instantiation (SPARK RM 7.1.3(7)). The following check is
10758 -- relevant only when SPARK_Mode is on as it is not a standard Ada
10759 -- legality rule, and also verifies that the actual is an object.
10760
10761 if SPARK_Mode = On
10762 and then Present (Actual)
10763 and then Is_Object_Reference (Actual)
10764 and then Is_Effectively_Volatile_Object (Actual)
10765 then
10766 Error_Msg_N
10767 ("volatile object cannot act as actual in generic instantiation",
10768 Actual);
10769 end if;
10770
10771 return List;
10772 end Instantiate_Object;
10773
10774 ------------------------------
10775 -- Instantiate_Package_Body --
10776 ------------------------------
10777
10778 procedure Instantiate_Package_Body
10779 (Body_Info : Pending_Body_Info;
10780 Inlined_Body : Boolean := False;
10781 Body_Optional : Boolean := False)
10782 is
10783 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
10784 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
10785 Loc : constant Source_Ptr := Sloc (Inst_Node);
10786
10787 Gen_Id : constant Node_Id := Name (Inst_Node);
10788 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
10789 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
10790 Act_Spec : constant Node_Id := Specification (Act_Decl);
10791 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Spec);
10792
10793 Save_IPSM : constant Boolean := Ignore_Pragma_SPARK_Mode;
10794 Save_Style_Check : constant Boolean := Style_Check;
10795
10796 Act_Body : Node_Id;
10797 Act_Body_Id : Entity_Id;
10798 Act_Body_Name : Node_Id;
10799 Gen_Body : Node_Id;
10800 Gen_Body_Id : Node_Id;
10801 Par_Ent : Entity_Id := Empty;
10802 Par_Vis : Boolean := False;
10803
10804 Parent_Installed : Boolean := False;
10805
10806 Vis_Prims_List : Elist_Id := No_Elist;
10807 -- List of primitives made temporarily visible in the instantiation
10808 -- to match the visibility of the formal type
10809
10810 procedure Check_Initialized_Types;
10811 -- In a generic package body, an entity of a generic private type may
10812 -- appear uninitialized. This is suspicious, unless the actual is a
10813 -- fully initialized type.
10814
10815 -----------------------------
10816 -- Check_Initialized_Types --
10817 -----------------------------
10818
10819 procedure Check_Initialized_Types is
10820 Decl : Node_Id;
10821 Formal : Entity_Id;
10822 Actual : Entity_Id;
10823 Uninit_Var : Entity_Id;
10824
10825 begin
10826 Decl := First (Generic_Formal_Declarations (Gen_Decl));
10827 while Present (Decl) loop
10828 Uninit_Var := Empty;
10829
10830 if Nkind (Decl) = N_Private_Extension_Declaration then
10831 Uninit_Var := Uninitialized_Variable (Decl);
10832
10833 elsif Nkind (Decl) = N_Formal_Type_Declaration
10834 and then Nkind (Formal_Type_Definition (Decl)) =
10835 N_Formal_Private_Type_Definition
10836 then
10837 Uninit_Var :=
10838 Uninitialized_Variable (Formal_Type_Definition (Decl));
10839 end if;
10840
10841 if Present (Uninit_Var) then
10842 Formal := Defining_Identifier (Decl);
10843 Actual := First_Entity (Act_Decl_Id);
10844
10845 -- For each formal there is a subtype declaration that renames
10846 -- the actual and has the same name as the formal. Locate the
10847 -- formal for warning message about uninitialized variables
10848 -- in the generic, for which the actual type should be a fully
10849 -- initialized type.
10850
10851 while Present (Actual) loop
10852 exit when Ekind (Actual) = E_Package
10853 and then Present (Renamed_Object (Actual));
10854
10855 if Chars (Actual) = Chars (Formal)
10856 and then not Is_Scalar_Type (Actual)
10857 and then not Is_Fully_Initialized_Type (Actual)
10858 and then Warn_On_No_Value_Assigned
10859 then
10860 Error_Msg_Node_2 := Formal;
10861 Error_Msg_NE
10862 ("generic unit has uninitialized variable& of "
10863 & "formal private type &?v?", Actual, Uninit_Var);
10864 Error_Msg_NE
10865 ("actual type for& should be fully initialized type?v?",
10866 Actual, Formal);
10867 exit;
10868 end if;
10869
10870 Next_Entity (Actual);
10871 end loop;
10872 end if;
10873
10874 Next (Decl);
10875 end loop;
10876 end Check_Initialized_Types;
10877
10878 -- Start of processing for Instantiate_Package_Body
10879
10880 begin
10881 Gen_Body_Id := Corresponding_Body (Gen_Decl);
10882
10883 -- The instance body may already have been processed, as the parent of
10884 -- another instance that is inlined (Load_Parent_Of_Generic).
10885
10886 if Present (Corresponding_Body (Instance_Spec (Inst_Node))) then
10887 return;
10888 end if;
10889
10890 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
10891
10892 -- Re-establish the state of information on which checks are suppressed.
10893 -- This information was set in Body_Info at the point of instantiation,
10894 -- and now we restore it so that the instance is compiled using the
10895 -- check status at the instantiation (RM 11.5(7.2/2), AI95-00224-01).
10896
10897 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
10898 Scope_Suppress := Body_Info.Scope_Suppress;
10899 Opt.Ada_Version := Body_Info.Version;
10900 Opt.Ada_Version_Pragma := Body_Info.Version_Pragma;
10901 Restore_Warnings (Body_Info.Warnings);
10902 Opt.SPARK_Mode := Body_Info.SPARK_Mode;
10903 Opt.SPARK_Mode_Pragma := Body_Info.SPARK_Mode_Pragma;
10904
10905 if No (Gen_Body_Id) then
10906
10907 -- Do not look for parent of generic body if none is required.
10908 -- This may happen when the routine is called as part of the
10909 -- Pending_Instantiations processing, when nested instances
10910 -- may precede the one generated from the main unit.
10911
10912 if not Unit_Requires_Body (Defining_Entity (Gen_Decl))
10913 and then Body_Optional
10914 then
10915 return;
10916 else
10917 Load_Parent_Of_Generic
10918 (Inst_Node, Specification (Gen_Decl), Body_Optional);
10919 Gen_Body_Id := Corresponding_Body (Gen_Decl);
10920 end if;
10921 end if;
10922
10923 -- Establish global variable for sloc adjustment and for error recovery
10924 -- In the case of an instance body for an instantiation with actuals
10925 -- from a limited view, the instance body is placed at the beginning
10926 -- of the enclosing package body: use the body entity as the source
10927 -- location for nodes of the instance body.
10928
10929 if not Is_Empty_Elmt_List (Incomplete_Actuals (Act_Decl_Id)) then
10930 declare
10931 Scop : constant Entity_Id := Scope (Act_Decl_Id);
10932 Body_Id : constant Node_Id :=
10933 Corresponding_Body (Unit_Declaration_Node (Scop));
10934
10935 begin
10936 Instantiation_Node := Body_Id;
10937 end;
10938 else
10939 Instantiation_Node := Inst_Node;
10940 end if;
10941
10942 if Present (Gen_Body_Id) then
10943 Save_Env (Gen_Unit, Act_Decl_Id);
10944 Style_Check := False;
10945
10946 -- If the context of the instance is subject to SPARK_Mode "off" or
10947 -- the annotation is altogether missing, set the global flag which
10948 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
10949 -- the instance.
10950
10951 if SPARK_Mode /= On then
10952 Ignore_Pragma_SPARK_Mode := True;
10953 end if;
10954
10955 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
10956 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
10957
10958 Create_Instantiation_Source
10959 (Inst_Node, Gen_Body_Id, S_Adjustment);
10960
10961 Act_Body :=
10962 Copy_Generic_Node
10963 (Original_Node (Gen_Body), Empty, Instantiating => True);
10964
10965 -- Create proper (possibly qualified) defining name for the body, to
10966 -- correspond to the one in the spec.
10967
10968 Act_Body_Id :=
10969 Make_Defining_Identifier (Sloc (Act_Decl_Id), Chars (Act_Decl_Id));
10970 Set_Comes_From_Source (Act_Body_Id, Comes_From_Source (Act_Decl_Id));
10971
10972 -- Some attributes of spec entity are not inherited by body entity
10973
10974 Set_Handler_Records (Act_Body_Id, No_List);
10975
10976 if Nkind (Defining_Unit_Name (Act_Spec)) =
10977 N_Defining_Program_Unit_Name
10978 then
10979 Act_Body_Name :=
10980 Make_Defining_Program_Unit_Name (Loc,
10981 Name =>
10982 New_Copy_Tree (Name (Defining_Unit_Name (Act_Spec))),
10983 Defining_Identifier => Act_Body_Id);
10984 else
10985 Act_Body_Name := Act_Body_Id;
10986 end if;
10987
10988 Set_Defining_Unit_Name (Act_Body, Act_Body_Name);
10989
10990 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
10991 Check_Generic_Actuals (Act_Decl_Id, False);
10992 Check_Initialized_Types;
10993
10994 -- Install primitives hidden at the point of the instantiation but
10995 -- visible when processing the generic formals
10996
10997 declare
10998 E : Entity_Id;
10999
11000 begin
11001 E := First_Entity (Act_Decl_Id);
11002 while Present (E) loop
11003 if Is_Type (E)
11004 and then not Is_Itype (E)
11005 and then Is_Generic_Actual_Type (E)
11006 and then Is_Tagged_Type (E)
11007 then
11008 Install_Hidden_Primitives
11009 (Prims_List => Vis_Prims_List,
11010 Gen_T => Generic_Parent_Type (Parent (E)),
11011 Act_T => E);
11012 end if;
11013
11014 Next_Entity (E);
11015 end loop;
11016 end;
11017
11018 -- If it is a child unit, make the parent instance (which is an
11019 -- instance of the parent of the generic) visible. The parent
11020 -- instance is the prefix of the name of the generic unit.
11021
11022 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
11023 and then Nkind (Gen_Id) = N_Expanded_Name
11024 then
11025 Par_Ent := Entity (Prefix (Gen_Id));
11026 Par_Vis := Is_Immediately_Visible (Par_Ent);
11027 Install_Parent (Par_Ent, In_Body => True);
11028 Parent_Installed := True;
11029
11030 elsif Is_Child_Unit (Gen_Unit) then
11031 Par_Ent := Scope (Gen_Unit);
11032 Par_Vis := Is_Immediately_Visible (Par_Ent);
11033 Install_Parent (Par_Ent, In_Body => True);
11034 Parent_Installed := True;
11035 end if;
11036
11037 -- If the instantiation is a library unit, and this is the main unit,
11038 -- then build the resulting compilation unit nodes for the instance.
11039 -- If this is a compilation unit but it is not the main unit, then it
11040 -- is the body of a unit in the context, that is being compiled
11041 -- because it is encloses some inlined unit or another generic unit
11042 -- being instantiated. In that case, this body is not part of the
11043 -- current compilation, and is not attached to the tree, but its
11044 -- parent must be set for analysis.
11045
11046 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
11047
11048 -- Replace instance node with body of instance, and create new
11049 -- node for corresponding instance declaration.
11050
11051 Build_Instance_Compilation_Unit_Nodes
11052 (Inst_Node, Act_Body, Act_Decl);
11053 Analyze (Inst_Node);
11054
11055 if Parent (Inst_Node) = Cunit (Main_Unit) then
11056
11057 -- If the instance is a child unit itself, then set the scope
11058 -- of the expanded body to be the parent of the instantiation
11059 -- (ensuring that the fully qualified name will be generated
11060 -- for the elaboration subprogram).
11061
11062 if Nkind (Defining_Unit_Name (Act_Spec)) =
11063 N_Defining_Program_Unit_Name
11064 then
11065 Set_Scope (Defining_Entity (Inst_Node), Scope (Act_Decl_Id));
11066 end if;
11067 end if;
11068
11069 -- Case where instantiation is not a library unit
11070
11071 else
11072 -- If this is an early instantiation, i.e. appears textually
11073 -- before the corresponding body and must be elaborated first,
11074 -- indicate that the body instance is to be delayed.
11075
11076 Install_Body (Act_Body, Inst_Node, Gen_Body, Gen_Decl);
11077
11078 -- Now analyze the body. We turn off all checks if this is an
11079 -- internal unit, since there is no reason to have checks on for
11080 -- any predefined run-time library code. All such code is designed
11081 -- to be compiled with checks off.
11082
11083 -- Note that we do NOT apply this criterion to children of GNAT
11084 -- The latter units must suppress checks explicitly if needed.
11085
11086 -- We also do not suppress checks in CodePeer mode where we are
11087 -- interested in finding possible runtime errors.
11088
11089 if not CodePeer_Mode
11090 and then Is_Predefined_File_Name
11091 (Unit_File_Name (Get_Source_Unit (Gen_Decl)))
11092 then
11093 Analyze (Act_Body, Suppress => All_Checks);
11094 else
11095 Analyze (Act_Body);
11096 end if;
11097 end if;
11098
11099 Inherit_Context (Gen_Body, Inst_Node);
11100
11101 -- Remove the parent instances if they have been placed on the scope
11102 -- stack to compile the body.
11103
11104 if Parent_Installed then
11105 Remove_Parent (In_Body => True);
11106
11107 -- Restore the previous visibility of the parent
11108
11109 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
11110 end if;
11111
11112 Restore_Hidden_Primitives (Vis_Prims_List);
11113 Restore_Private_Views (Act_Decl_Id);
11114
11115 -- Remove the current unit from visibility if this is an instance
11116 -- that is not elaborated on the fly for inlining purposes.
11117
11118 if not Inlined_Body then
11119 Set_Is_Immediately_Visible (Act_Decl_Id, False);
11120 end if;
11121
11122 Restore_Env;
11123 Ignore_Pragma_SPARK_Mode := Save_IPSM;
11124 Style_Check := Save_Style_Check;
11125
11126 -- If we have no body, and the unit requires a body, then complain. This
11127 -- complaint is suppressed if we have detected other errors (since a
11128 -- common reason for missing the body is that it had errors).
11129 -- In CodePeer mode, a warning has been emitted already, no need for
11130 -- further messages.
11131
11132 elsif Unit_Requires_Body (Gen_Unit)
11133 and then not Body_Optional
11134 then
11135 if CodePeer_Mode then
11136 null;
11137
11138 elsif Serious_Errors_Detected = 0 then
11139 Error_Msg_NE
11140 ("cannot find body of generic package &", Inst_Node, Gen_Unit);
11141
11142 -- Don't attempt to perform any cleanup actions if some other error
11143 -- was already detected, since this can cause blowups.
11144
11145 else
11146 return;
11147 end if;
11148
11149 -- Case of package that does not need a body
11150
11151 else
11152 -- If the instantiation of the declaration is a library unit, rewrite
11153 -- the original package instantiation as a package declaration in the
11154 -- compilation unit node.
11155
11156 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
11157 Set_Parent_Spec (Act_Decl, Parent_Spec (Inst_Node));
11158 Rewrite (Inst_Node, Act_Decl);
11159
11160 -- Generate elaboration entity, in case spec has elaboration code.
11161 -- This cannot be done when the instance is analyzed, because it
11162 -- is not known yet whether the body exists.
11163
11164 Set_Elaboration_Entity_Required (Act_Decl_Id, False);
11165 Build_Elaboration_Entity (Parent (Inst_Node), Act_Decl_Id);
11166
11167 -- If the instantiation is not a library unit, then append the
11168 -- declaration to the list of implicitly generated entities, unless
11169 -- it is already a list member which means that it was already
11170 -- processed
11171
11172 elsif not Is_List_Member (Act_Decl) then
11173 Mark_Rewrite_Insertion (Act_Decl);
11174 Insert_Before (Inst_Node, Act_Decl);
11175 end if;
11176 end if;
11177
11178 Expander_Mode_Restore;
11179 end Instantiate_Package_Body;
11180
11181 ---------------------------------
11182 -- Instantiate_Subprogram_Body --
11183 ---------------------------------
11184
11185 procedure Instantiate_Subprogram_Body
11186 (Body_Info : Pending_Body_Info;
11187 Body_Optional : Boolean := False)
11188 is
11189 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
11190 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
11191 Loc : constant Source_Ptr := Sloc (Inst_Node);
11192 Gen_Id : constant Node_Id := Name (Inst_Node);
11193 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
11194 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
11195 Act_Decl_Id : constant Entity_Id :=
11196 Defining_Unit_Name (Specification (Act_Decl));
11197 Pack_Id : constant Entity_Id :=
11198 Defining_Unit_Name (Parent (Act_Decl));
11199
11200 Saved_IPSM : constant Boolean := Ignore_Pragma_SPARK_Mode;
11201 Saved_Style_Check : constant Boolean := Style_Check;
11202 Saved_Warnings : constant Warning_Record := Save_Warnings;
11203
11204 Act_Body : Node_Id;
11205 Act_Body_Id : Entity_Id;
11206 Gen_Body : Node_Id;
11207 Gen_Body_Id : Node_Id;
11208 Pack_Body : Node_Id;
11209 Par_Ent : Entity_Id := Empty;
11210 Par_Vis : Boolean := False;
11211 Ret_Expr : Node_Id;
11212
11213 Parent_Installed : Boolean := False;
11214
11215 begin
11216 Gen_Body_Id := Corresponding_Body (Gen_Decl);
11217
11218 -- Subprogram body may have been created already because of an inline
11219 -- pragma, or because of multiple elaborations of the enclosing package
11220 -- when several instances of the subprogram appear in the main unit.
11221
11222 if Present (Corresponding_Body (Act_Decl)) then
11223 return;
11224 end if;
11225
11226 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
11227
11228 -- Re-establish the state of information on which checks are suppressed.
11229 -- This information was set in Body_Info at the point of instantiation,
11230 -- and now we restore it so that the instance is compiled using the
11231 -- check status at the instantiation (RM 11.5(7.2/2), AI95-00224-01).
11232
11233 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
11234 Scope_Suppress := Body_Info.Scope_Suppress;
11235 Opt.Ada_Version := Body_Info.Version;
11236 Opt.Ada_Version_Pragma := Body_Info.Version_Pragma;
11237 Restore_Warnings (Body_Info.Warnings);
11238 Opt.SPARK_Mode := Body_Info.SPARK_Mode;
11239 Opt.SPARK_Mode_Pragma := Body_Info.SPARK_Mode_Pragma;
11240
11241 if No (Gen_Body_Id) then
11242
11243 -- For imported generic subprogram, no body to compile, complete
11244 -- the spec entity appropriately.
11245
11246 if Is_Imported (Gen_Unit) then
11247 Set_Is_Imported (Act_Decl_Id);
11248 Set_First_Rep_Item (Act_Decl_Id, First_Rep_Item (Gen_Unit));
11249 Set_Interface_Name (Act_Decl_Id, Interface_Name (Gen_Unit));
11250 Set_Convention (Act_Decl_Id, Convention (Gen_Unit));
11251 Set_Has_Completion (Act_Decl_Id);
11252 return;
11253
11254 -- For other cases, compile the body
11255
11256 else
11257 Load_Parent_Of_Generic
11258 (Inst_Node, Specification (Gen_Decl), Body_Optional);
11259 Gen_Body_Id := Corresponding_Body (Gen_Decl);
11260 end if;
11261 end if;
11262
11263 Instantiation_Node := Inst_Node;
11264
11265 if Present (Gen_Body_Id) then
11266 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
11267
11268 if Nkind (Gen_Body) = N_Subprogram_Body_Stub then
11269
11270 -- Either body is not present, or context is non-expanding, as
11271 -- when compiling a subunit. Mark the instance as completed, and
11272 -- diagnose a missing body when needed.
11273
11274 if Expander_Active
11275 and then Operating_Mode = Generate_Code
11276 then
11277 Error_Msg_N
11278 ("missing proper body for instantiation", Gen_Body);
11279 end if;
11280
11281 Set_Has_Completion (Act_Decl_Id);
11282 return;
11283 end if;
11284
11285 Save_Env (Gen_Unit, Act_Decl_Id);
11286 Style_Check := False;
11287
11288 -- If the context of the instance is subject to SPARK_Mode "off" or
11289 -- the annotation is altogether missing, set the global flag which
11290 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
11291 -- the instance.
11292
11293 if SPARK_Mode /= On then
11294 Ignore_Pragma_SPARK_Mode := True;
11295 end if;
11296
11297 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
11298 Create_Instantiation_Source
11299 (Inst_Node,
11300 Gen_Body_Id,
11301 S_Adjustment);
11302
11303 Act_Body :=
11304 Copy_Generic_Node
11305 (Original_Node (Gen_Body), Empty, Instantiating => True);
11306
11307 -- Create proper defining name for the body, to correspond to the one
11308 -- in the spec.
11309
11310 Act_Body_Id :=
11311 Make_Defining_Identifier (Sloc (Act_Decl_Id), Chars (Act_Decl_Id));
11312
11313 Set_Comes_From_Source (Act_Body_Id, Comes_From_Source (Act_Decl_Id));
11314 Set_Defining_Unit_Name (Specification (Act_Body), Act_Body_Id);
11315
11316 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
11317 Set_Has_Completion (Act_Decl_Id);
11318 Check_Generic_Actuals (Pack_Id, False);
11319
11320 -- Generate a reference to link the visible subprogram instance to
11321 -- the generic body, which for navigation purposes is the only
11322 -- available source for the instance.
11323
11324 Generate_Reference
11325 (Related_Instance (Pack_Id),
11326 Gen_Body_Id, 'b', Set_Ref => False, Force => True);
11327
11328 -- If it is a child unit, make the parent instance (which is an
11329 -- instance of the parent of the generic) visible. The parent
11330 -- instance is the prefix of the name of the generic unit.
11331
11332 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
11333 and then Nkind (Gen_Id) = N_Expanded_Name
11334 then
11335 Par_Ent := Entity (Prefix (Gen_Id));
11336 Par_Vis := Is_Immediately_Visible (Par_Ent);
11337 Install_Parent (Par_Ent, In_Body => True);
11338 Parent_Installed := True;
11339
11340 elsif Is_Child_Unit (Gen_Unit) then
11341 Par_Ent := Scope (Gen_Unit);
11342 Par_Vis := Is_Immediately_Visible (Par_Ent);
11343 Install_Parent (Par_Ent, In_Body => True);
11344 Parent_Installed := True;
11345 end if;
11346
11347 -- Subprogram body is placed in the body of wrapper package,
11348 -- whose spec contains the subprogram declaration as well as
11349 -- the renaming declarations for the generic parameters.
11350
11351 Pack_Body :=
11352 Make_Package_Body (Loc,
11353 Defining_Unit_Name => New_Copy (Pack_Id),
11354 Declarations => New_List (Act_Body));
11355
11356 Set_Corresponding_Spec (Pack_Body, Pack_Id);
11357
11358 -- If the instantiation is a library unit, then build resulting
11359 -- compilation unit nodes for the instance. The declaration of
11360 -- the enclosing package is the grandparent of the subprogram
11361 -- declaration. First replace the instantiation node as the unit
11362 -- of the corresponding compilation.
11363
11364 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
11365 if Parent (Inst_Node) = Cunit (Main_Unit) then
11366 Set_Unit (Parent (Inst_Node), Inst_Node);
11367 Build_Instance_Compilation_Unit_Nodes
11368 (Inst_Node, Pack_Body, Parent (Parent (Act_Decl)));
11369 Analyze (Inst_Node);
11370 else
11371 Set_Parent (Pack_Body, Parent (Inst_Node));
11372 Analyze (Pack_Body);
11373 end if;
11374
11375 else
11376 Insert_Before (Inst_Node, Pack_Body);
11377 Mark_Rewrite_Insertion (Pack_Body);
11378 Analyze (Pack_Body);
11379
11380 if Expander_Active then
11381 Freeze_Subprogram_Body (Inst_Node, Gen_Body, Pack_Id);
11382 end if;
11383 end if;
11384
11385 Inherit_Context (Gen_Body, Inst_Node);
11386
11387 Restore_Private_Views (Pack_Id, False);
11388
11389 if Parent_Installed then
11390 Remove_Parent (In_Body => True);
11391
11392 -- Restore the previous visibility of the parent
11393
11394 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
11395 end if;
11396
11397 Restore_Env;
11398 Ignore_Pragma_SPARK_Mode := Saved_IPSM;
11399 Style_Check := Saved_Style_Check;
11400 Restore_Warnings (Saved_Warnings);
11401
11402 -- Body not found. Error was emitted already. If there were no previous
11403 -- errors, this may be an instance whose scope is a premature instance.
11404 -- In that case we must insure that the (legal) program does raise
11405 -- program error if executed. We generate a subprogram body for this
11406 -- purpose. See DEC ac30vso.
11407
11408 -- Should not reference proprietary DEC tests in comments ???
11409
11410 elsif Serious_Errors_Detected = 0
11411 and then Nkind (Parent (Inst_Node)) /= N_Compilation_Unit
11412 then
11413 if Body_Optional then
11414 return;
11415
11416 elsif Ekind (Act_Decl_Id) = E_Procedure then
11417 Act_Body :=
11418 Make_Subprogram_Body (Loc,
11419 Specification =>
11420 Make_Procedure_Specification (Loc,
11421 Defining_Unit_Name =>
11422 Make_Defining_Identifier (Loc, Chars (Act_Decl_Id)),
11423 Parameter_Specifications =>
11424 New_Copy_List
11425 (Parameter_Specifications (Parent (Act_Decl_Id)))),
11426
11427 Declarations => Empty_List,
11428 Handled_Statement_Sequence =>
11429 Make_Handled_Sequence_Of_Statements (Loc,
11430 Statements =>
11431 New_List (
11432 Make_Raise_Program_Error (Loc,
11433 Reason =>
11434 PE_Access_Before_Elaboration))));
11435
11436 else
11437 Ret_Expr :=
11438 Make_Raise_Program_Error (Loc,
11439 Reason => PE_Access_Before_Elaboration);
11440
11441 Set_Etype (Ret_Expr, (Etype (Act_Decl_Id)));
11442 Set_Analyzed (Ret_Expr);
11443
11444 Act_Body :=
11445 Make_Subprogram_Body (Loc,
11446 Specification =>
11447 Make_Function_Specification (Loc,
11448 Defining_Unit_Name =>
11449 Make_Defining_Identifier (Loc, Chars (Act_Decl_Id)),
11450 Parameter_Specifications =>
11451 New_Copy_List
11452 (Parameter_Specifications (Parent (Act_Decl_Id))),
11453 Result_Definition =>
11454 New_Occurrence_Of (Etype (Act_Decl_Id), Loc)),
11455
11456 Declarations => Empty_List,
11457 Handled_Statement_Sequence =>
11458 Make_Handled_Sequence_Of_Statements (Loc,
11459 Statements =>
11460 New_List
11461 (Make_Simple_Return_Statement (Loc, Ret_Expr))));
11462 end if;
11463
11464 Pack_Body :=
11465 Make_Package_Body (Loc,
11466 Defining_Unit_Name => New_Copy (Pack_Id),
11467 Declarations => New_List (Act_Body));
11468
11469 Insert_After (Inst_Node, Pack_Body);
11470 Set_Corresponding_Spec (Pack_Body, Pack_Id);
11471 Analyze (Pack_Body);
11472 end if;
11473
11474 Expander_Mode_Restore;
11475 end Instantiate_Subprogram_Body;
11476
11477 ----------------------
11478 -- Instantiate_Type --
11479 ----------------------
11480
11481 function Instantiate_Type
11482 (Formal : Node_Id;
11483 Actual : Node_Id;
11484 Analyzed_Formal : Node_Id;
11485 Actual_Decls : List_Id) return List_Id
11486 is
11487 Gen_T : constant Entity_Id := Defining_Identifier (Formal);
11488 A_Gen_T : constant Entity_Id :=
11489 Defining_Identifier (Analyzed_Formal);
11490 Ancestor : Entity_Id := Empty;
11491 Def : constant Node_Id := Formal_Type_Definition (Formal);
11492 Act_T : Entity_Id;
11493 Decl_Node : Node_Id;
11494 Decl_Nodes : List_Id;
11495 Loc : Source_Ptr;
11496 Subt : Entity_Id;
11497
11498 procedure Diagnose_Predicated_Actual;
11499 -- There are a number of constructs in which a discrete type with
11500 -- predicates is illegal, e.g. as an index in an array type declaration.
11501 -- If a generic type is used is such a construct in a generic package
11502 -- declaration, it carries the flag No_Predicate_On_Actual. it is part
11503 -- of the generic contract that the actual cannot have predicates.
11504
11505 procedure Validate_Array_Type_Instance;
11506 procedure Validate_Access_Subprogram_Instance;
11507 procedure Validate_Access_Type_Instance;
11508 procedure Validate_Derived_Type_Instance;
11509 procedure Validate_Derived_Interface_Type_Instance;
11510 procedure Validate_Discriminated_Formal_Type;
11511 procedure Validate_Interface_Type_Instance;
11512 procedure Validate_Private_Type_Instance;
11513 procedure Validate_Incomplete_Type_Instance;
11514 -- These procedures perform validation tests for the named case.
11515 -- Validate_Discriminated_Formal_Type is shared by formal private
11516 -- types and Ada 2012 formal incomplete types.
11517
11518 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean;
11519 -- Check that base types are the same and that the subtypes match
11520 -- statically. Used in several of the above.
11521
11522 ---------------------------------
11523 -- Diagnose_Predicated_Actual --
11524 ---------------------------------
11525
11526 procedure Diagnose_Predicated_Actual is
11527 begin
11528 if No_Predicate_On_Actual (A_Gen_T)
11529 and then Has_Predicates (Act_T)
11530 then
11531 Error_Msg_NE
11532 ("actual for& cannot be a type with predicate",
11533 Instantiation_Node, A_Gen_T);
11534
11535 elsif No_Dynamic_Predicate_On_Actual (A_Gen_T)
11536 and then Has_Predicates (Act_T)
11537 and then not Has_Static_Predicate_Aspect (Act_T)
11538 then
11539 Error_Msg_NE
11540 ("actual for& cannot be a type with a dynamic predicate",
11541 Instantiation_Node, A_Gen_T);
11542 end if;
11543 end Diagnose_Predicated_Actual;
11544
11545 --------------------
11546 -- Subtypes_Match --
11547 --------------------
11548
11549 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean is
11550 T : constant Entity_Id := Get_Instance_Of (Gen_T);
11551
11552 begin
11553 -- Some detailed comments would be useful here ???
11554
11555 return ((Base_Type (T) = Act_T
11556 or else Base_Type (T) = Base_Type (Act_T))
11557 and then Subtypes_Statically_Match (T, Act_T))
11558
11559 or else (Is_Class_Wide_Type (Gen_T)
11560 and then Is_Class_Wide_Type (Act_T)
11561 and then Subtypes_Match
11562 (Get_Instance_Of (Root_Type (Gen_T)),
11563 Root_Type (Act_T)))
11564
11565 or else
11566 (Ekind_In (Gen_T, E_Anonymous_Access_Subprogram_Type,
11567 E_Anonymous_Access_Type)
11568 and then Ekind (Act_T) = Ekind (Gen_T)
11569 and then Subtypes_Statically_Match
11570 (Designated_Type (Gen_T), Designated_Type (Act_T)));
11571 end Subtypes_Match;
11572
11573 -----------------------------------------
11574 -- Validate_Access_Subprogram_Instance --
11575 -----------------------------------------
11576
11577 procedure Validate_Access_Subprogram_Instance is
11578 begin
11579 if not Is_Access_Type (Act_T)
11580 or else Ekind (Designated_Type (Act_T)) /= E_Subprogram_Type
11581 then
11582 Error_Msg_NE
11583 ("expect access type in instantiation of &", Actual, Gen_T);
11584 Abandon_Instantiation (Actual);
11585 end if;
11586
11587 -- According to AI05-288, actuals for access_to_subprograms must be
11588 -- subtype conformant with the generic formal. Previous to AI05-288
11589 -- only mode conformance was required.
11590
11591 -- This is a binding interpretation that applies to previous versions
11592 -- of the language, no need to maintain previous weaker checks.
11593
11594 Check_Subtype_Conformant
11595 (Designated_Type (Act_T),
11596 Designated_Type (A_Gen_T),
11597 Actual,
11598 Get_Inst => True);
11599
11600 if Ekind (Base_Type (Act_T)) = E_Access_Protected_Subprogram_Type then
11601 if Ekind (A_Gen_T) = E_Access_Subprogram_Type then
11602 Error_Msg_NE
11603 ("protected access type not allowed for formal &",
11604 Actual, Gen_T);
11605 end if;
11606
11607 elsif Ekind (A_Gen_T) = E_Access_Protected_Subprogram_Type then
11608 Error_Msg_NE
11609 ("expect protected access type for formal &",
11610 Actual, Gen_T);
11611 end if;
11612
11613 -- If the formal has a specified convention (which in most cases
11614 -- will be StdCall) verify that the actual has the same convention.
11615
11616 if Has_Convention_Pragma (A_Gen_T)
11617 and then Convention (A_Gen_T) /= Convention (Act_T)
11618 then
11619 Error_Msg_Name_1 := Get_Convention_Name (Convention (A_Gen_T));
11620 Error_Msg_NE
11621 ("actual for formal & must have convention %", Actual, Gen_T);
11622 end if;
11623 end Validate_Access_Subprogram_Instance;
11624
11625 -----------------------------------
11626 -- Validate_Access_Type_Instance --
11627 -----------------------------------
11628
11629 procedure Validate_Access_Type_Instance is
11630 Desig_Type : constant Entity_Id :=
11631 Find_Actual_Type (Designated_Type (A_Gen_T), A_Gen_T);
11632 Desig_Act : Entity_Id;
11633
11634 begin
11635 if not Is_Access_Type (Act_T) then
11636 Error_Msg_NE
11637 ("expect access type in instantiation of &", Actual, Gen_T);
11638 Abandon_Instantiation (Actual);
11639 end if;
11640
11641 if Is_Access_Constant (A_Gen_T) then
11642 if not Is_Access_Constant (Act_T) then
11643 Error_Msg_N
11644 ("actual type must be access-to-constant type", Actual);
11645 Abandon_Instantiation (Actual);
11646 end if;
11647 else
11648 if Is_Access_Constant (Act_T) then
11649 Error_Msg_N
11650 ("actual type must be access-to-variable type", Actual);
11651 Abandon_Instantiation (Actual);
11652
11653 elsif Ekind (A_Gen_T) = E_General_Access_Type
11654 and then Ekind (Base_Type (Act_T)) /= E_General_Access_Type
11655 then
11656 Error_Msg_N -- CODEFIX
11657 ("actual must be general access type!", Actual);
11658 Error_Msg_NE -- CODEFIX
11659 ("add ALL to }!", Actual, Act_T);
11660 Abandon_Instantiation (Actual);
11661 end if;
11662 end if;
11663
11664 -- The designated subtypes, that is to say the subtypes introduced
11665 -- by an access type declaration (and not by a subtype declaration)
11666 -- must match.
11667
11668 Desig_Act := Designated_Type (Base_Type (Act_T));
11669
11670 -- The designated type may have been introduced through a limited_
11671 -- with clause, in which case retrieve the non-limited view. This
11672 -- applies to incomplete types as well as to class-wide types.
11673
11674 if From_Limited_With (Desig_Act) then
11675 Desig_Act := Available_View (Desig_Act);
11676 end if;
11677
11678 if not Subtypes_Match (Desig_Type, Desig_Act) then
11679 Error_Msg_NE
11680 ("designated type of actual does not match that of formal &",
11681 Actual, Gen_T);
11682
11683 if not Predicates_Match (Desig_Type, Desig_Act) then
11684 Error_Msg_N ("\predicates do not match", Actual);
11685 end if;
11686
11687 Abandon_Instantiation (Actual);
11688
11689 elsif Is_Access_Type (Designated_Type (Act_T))
11690 and then Is_Constrained (Designated_Type (Designated_Type (Act_T)))
11691 /=
11692 Is_Constrained (Designated_Type (Desig_Type))
11693 then
11694 Error_Msg_NE
11695 ("designated type of actual does not match that of formal &",
11696 Actual, Gen_T);
11697
11698 if not Predicates_Match (Desig_Type, Desig_Act) then
11699 Error_Msg_N ("\predicates do not match", Actual);
11700 end if;
11701
11702 Abandon_Instantiation (Actual);
11703 end if;
11704
11705 -- Ada 2005: null-exclusion indicators of the two types must agree
11706
11707 if Can_Never_Be_Null (A_Gen_T) /= Can_Never_Be_Null (Act_T) then
11708 Error_Msg_NE
11709 ("non null exclusion of actual and formal & do not match",
11710 Actual, Gen_T);
11711 end if;
11712 end Validate_Access_Type_Instance;
11713
11714 ----------------------------------
11715 -- Validate_Array_Type_Instance --
11716 ----------------------------------
11717
11718 procedure Validate_Array_Type_Instance is
11719 I1 : Node_Id;
11720 I2 : Node_Id;
11721 T2 : Entity_Id;
11722
11723 function Formal_Dimensions return Nat;
11724 -- Count number of dimensions in array type formal
11725
11726 -----------------------
11727 -- Formal_Dimensions --
11728 -----------------------
11729
11730 function Formal_Dimensions return Nat is
11731 Num : Nat := 0;
11732 Index : Node_Id;
11733
11734 begin
11735 if Nkind (Def) = N_Constrained_Array_Definition then
11736 Index := First (Discrete_Subtype_Definitions (Def));
11737 else
11738 Index := First (Subtype_Marks (Def));
11739 end if;
11740
11741 while Present (Index) loop
11742 Num := Num + 1;
11743 Next_Index (Index);
11744 end loop;
11745
11746 return Num;
11747 end Formal_Dimensions;
11748
11749 -- Start of processing for Validate_Array_Type_Instance
11750
11751 begin
11752 if not Is_Array_Type (Act_T) then
11753 Error_Msg_NE
11754 ("expect array type in instantiation of &", Actual, Gen_T);
11755 Abandon_Instantiation (Actual);
11756
11757 elsif Nkind (Def) = N_Constrained_Array_Definition then
11758 if not (Is_Constrained (Act_T)) then
11759 Error_Msg_NE
11760 ("expect constrained array in instantiation of &",
11761 Actual, Gen_T);
11762 Abandon_Instantiation (Actual);
11763 end if;
11764
11765 else
11766 if Is_Constrained (Act_T) then
11767 Error_Msg_NE
11768 ("expect unconstrained array in instantiation of &",
11769 Actual, Gen_T);
11770 Abandon_Instantiation (Actual);
11771 end if;
11772 end if;
11773
11774 if Formal_Dimensions /= Number_Dimensions (Act_T) then
11775 Error_Msg_NE
11776 ("dimensions of actual do not match formal &", Actual, Gen_T);
11777 Abandon_Instantiation (Actual);
11778 end if;
11779
11780 I1 := First_Index (A_Gen_T);
11781 I2 := First_Index (Act_T);
11782 for J in 1 .. Formal_Dimensions loop
11783
11784 -- If the indexes of the actual were given by a subtype_mark,
11785 -- the index was transformed into a range attribute. Retrieve
11786 -- the original type mark for checking.
11787
11788 if Is_Entity_Name (Original_Node (I2)) then
11789 T2 := Entity (Original_Node (I2));
11790 else
11791 T2 := Etype (I2);
11792 end if;
11793
11794 if not Subtypes_Match
11795 (Find_Actual_Type (Etype (I1), A_Gen_T), T2)
11796 then
11797 Error_Msg_NE
11798 ("index types of actual do not match those of formal &",
11799 Actual, Gen_T);
11800 Abandon_Instantiation (Actual);
11801 end if;
11802
11803 Next_Index (I1);
11804 Next_Index (I2);
11805 end loop;
11806
11807 -- Check matching subtypes. Note that there are complex visibility
11808 -- issues when the generic is a child unit and some aspect of the
11809 -- generic type is declared in a parent unit of the generic. We do
11810 -- the test to handle this special case only after a direct check
11811 -- for static matching has failed. The case where both the component
11812 -- type and the array type are separate formals, and the component
11813 -- type is a private view may also require special checking in
11814 -- Subtypes_Match.
11815
11816 if Subtypes_Match
11817 (Component_Type (A_Gen_T), Component_Type (Act_T))
11818 or else
11819 Subtypes_Match
11820 (Find_Actual_Type (Component_Type (A_Gen_T), A_Gen_T),
11821 Component_Type (Act_T))
11822 then
11823 null;
11824 else
11825 Error_Msg_NE
11826 ("component subtype of actual does not match that of formal &",
11827 Actual, Gen_T);
11828 Abandon_Instantiation (Actual);
11829 end if;
11830
11831 if Has_Aliased_Components (A_Gen_T)
11832 and then not Has_Aliased_Components (Act_T)
11833 then
11834 Error_Msg_NE
11835 ("actual must have aliased components to match formal type &",
11836 Actual, Gen_T);
11837 end if;
11838 end Validate_Array_Type_Instance;
11839
11840 -----------------------------------------------
11841 -- Validate_Derived_Interface_Type_Instance --
11842 -----------------------------------------------
11843
11844 procedure Validate_Derived_Interface_Type_Instance is
11845 Par : constant Entity_Id := Entity (Subtype_Indication (Def));
11846 Elmt : Elmt_Id;
11847
11848 begin
11849 -- First apply interface instance checks
11850
11851 Validate_Interface_Type_Instance;
11852
11853 -- Verify that immediate parent interface is an ancestor of
11854 -- the actual.
11855
11856 if Present (Par)
11857 and then not Interface_Present_In_Ancestor (Act_T, Par)
11858 then
11859 Error_Msg_NE
11860 ("interface actual must include progenitor&", Actual, Par);
11861 end if;
11862
11863 -- Now verify that the actual includes all other ancestors of
11864 -- the formal.
11865
11866 Elmt := First_Elmt (Interfaces (A_Gen_T));
11867 while Present (Elmt) loop
11868 if not Interface_Present_In_Ancestor
11869 (Act_T, Get_Instance_Of (Node (Elmt)))
11870 then
11871 Error_Msg_NE
11872 ("interface actual must include progenitor&",
11873 Actual, Node (Elmt));
11874 end if;
11875
11876 Next_Elmt (Elmt);
11877 end loop;
11878 end Validate_Derived_Interface_Type_Instance;
11879
11880 ------------------------------------
11881 -- Validate_Derived_Type_Instance --
11882 ------------------------------------
11883
11884 procedure Validate_Derived_Type_Instance is
11885 Actual_Discr : Entity_Id;
11886 Ancestor_Discr : Entity_Id;
11887
11888 begin
11889 -- If the parent type in the generic declaration is itself a previous
11890 -- formal type, then it is local to the generic and absent from the
11891 -- analyzed generic definition. In that case the ancestor is the
11892 -- instance of the formal (which must have been instantiated
11893 -- previously), unless the ancestor is itself a formal derived type.
11894 -- In this latter case (which is the subject of Corrigendum 8652/0038
11895 -- (AI-202) the ancestor of the formals is the ancestor of its
11896 -- parent. Otherwise, the analyzed generic carries the parent type.
11897 -- If the parent type is defined in a previous formal package, then
11898 -- the scope of that formal package is that of the generic type
11899 -- itself, and it has already been mapped into the corresponding type
11900 -- in the actual package.
11901
11902 -- Common case: parent type defined outside of the generic
11903
11904 if Is_Entity_Name (Subtype_Mark (Def))
11905 and then Present (Entity (Subtype_Mark (Def)))
11906 then
11907 Ancestor := Get_Instance_Of (Entity (Subtype_Mark (Def)));
11908
11909 -- Check whether parent is defined in a previous formal package
11910
11911 elsif
11912 Scope (Scope (Base_Type (Etype (A_Gen_T)))) = Scope (A_Gen_T)
11913 then
11914 Ancestor :=
11915 Get_Instance_Of (Base_Type (Etype (A_Gen_T)));
11916
11917 -- The type may be a local derivation, or a type extension of a
11918 -- previous formal, or of a formal of a parent package.
11919
11920 elsif Is_Derived_Type (Get_Instance_Of (A_Gen_T))
11921 or else
11922 Ekind (Get_Instance_Of (A_Gen_T)) = E_Record_Type_With_Private
11923 then
11924 -- Check whether the parent is another derived formal type in the
11925 -- same generic unit.
11926
11927 if Etype (A_Gen_T) /= A_Gen_T
11928 and then Is_Generic_Type (Etype (A_Gen_T))
11929 and then Scope (Etype (A_Gen_T)) = Scope (A_Gen_T)
11930 and then Etype (Etype (A_Gen_T)) /= Etype (A_Gen_T)
11931 then
11932 -- Locate ancestor of parent from the subtype declaration
11933 -- created for the actual.
11934
11935 declare
11936 Decl : Node_Id;
11937
11938 begin
11939 Decl := First (Actual_Decls);
11940 while Present (Decl) loop
11941 if Nkind (Decl) = N_Subtype_Declaration
11942 and then Chars (Defining_Identifier (Decl)) =
11943 Chars (Etype (A_Gen_T))
11944 then
11945 Ancestor := Generic_Parent_Type (Decl);
11946 exit;
11947 else
11948 Next (Decl);
11949 end if;
11950 end loop;
11951 end;
11952
11953 pragma Assert (Present (Ancestor));
11954
11955 -- The ancestor itself may be a previous formal that has been
11956 -- instantiated.
11957
11958 Ancestor := Get_Instance_Of (Ancestor);
11959
11960 else
11961 Ancestor :=
11962 Get_Instance_Of (Base_Type (Get_Instance_Of (A_Gen_T)));
11963 end if;
11964
11965 -- Check whether parent is a previous formal of the current generic
11966
11967 elsif Is_Derived_Type (A_Gen_T)
11968 and then Is_Generic_Type (Etype (A_Gen_T))
11969 and then Scope (A_Gen_T) = Scope (Etype (A_Gen_T))
11970 then
11971 Ancestor := Get_Instance_Of (First_Subtype (Etype (A_Gen_T)));
11972
11973 -- An unusual case: the actual is a type declared in a parent unit,
11974 -- but is not a formal type so there is no instance_of for it.
11975 -- Retrieve it by analyzing the record extension.
11976
11977 elsif Is_Child_Unit (Scope (A_Gen_T))
11978 and then In_Open_Scopes (Scope (Act_T))
11979 and then Is_Generic_Instance (Scope (Act_T))
11980 then
11981 Analyze (Subtype_Mark (Def));
11982 Ancestor := Entity (Subtype_Mark (Def));
11983
11984 else
11985 Ancestor := Get_Instance_Of (Etype (Base_Type (A_Gen_T)));
11986 end if;
11987
11988 -- If the formal derived type has pragma Preelaborable_Initialization
11989 -- then the actual type must have preelaborable initialization.
11990
11991 if Known_To_Have_Preelab_Init (A_Gen_T)
11992 and then not Has_Preelaborable_Initialization (Act_T)
11993 then
11994 Error_Msg_NE
11995 ("actual for & must have preelaborable initialization",
11996 Actual, Gen_T);
11997 end if;
11998
11999 -- Ada 2005 (AI-251)
12000
12001 if Ada_Version >= Ada_2005 and then Is_Interface (Ancestor) then
12002 if not Interface_Present_In_Ancestor (Act_T, Ancestor) then
12003 Error_Msg_NE
12004 ("(Ada 2005) expected type implementing & in instantiation",
12005 Actual, Ancestor);
12006 end if;
12007
12008 -- Finally verify that the (instance of) the ancestor is an ancestor
12009 -- of the actual.
12010
12011 elsif not Is_Ancestor (Base_Type (Ancestor), Act_T) then
12012 Error_Msg_NE
12013 ("expect type derived from & in instantiation",
12014 Actual, First_Subtype (Ancestor));
12015 Abandon_Instantiation (Actual);
12016 end if;
12017
12018 -- Ada 2005 (AI-443): Synchronized formal derived type checks. Note
12019 -- that the formal type declaration has been rewritten as a private
12020 -- extension.
12021
12022 if Ada_Version >= Ada_2005
12023 and then Nkind (Parent (A_Gen_T)) = N_Private_Extension_Declaration
12024 and then Synchronized_Present (Parent (A_Gen_T))
12025 then
12026 -- The actual must be a synchronized tagged type
12027
12028 if not Is_Tagged_Type (Act_T) then
12029 Error_Msg_N
12030 ("actual of synchronized type must be tagged", Actual);
12031 Abandon_Instantiation (Actual);
12032
12033 elsif Nkind (Parent (Act_T)) = N_Full_Type_Declaration
12034 and then Nkind (Type_Definition (Parent (Act_T))) =
12035 N_Derived_Type_Definition
12036 and then not Synchronized_Present
12037 (Type_Definition (Parent (Act_T)))
12038 then
12039 Error_Msg_N
12040 ("actual of synchronized type must be synchronized", Actual);
12041 Abandon_Instantiation (Actual);
12042 end if;
12043 end if;
12044
12045 -- Perform atomic/volatile checks (RM C.6(12)). Note that AI05-0218-1
12046 -- removes the second instance of the phrase "or allow pass by copy".
12047
12048 if Is_Atomic (Act_T) and then not Is_Atomic (Ancestor) then
12049 Error_Msg_N
12050 ("cannot have atomic actual type for non-atomic formal type",
12051 Actual);
12052
12053 elsif Is_Volatile (Act_T) and then not Is_Volatile (Ancestor) then
12054 Error_Msg_N
12055 ("cannot have volatile actual type for non-volatile formal type",
12056 Actual);
12057 end if;
12058
12059 -- It should not be necessary to check for unknown discriminants on
12060 -- Formal, but for some reason Has_Unknown_Discriminants is false for
12061 -- A_Gen_T, so Is_Definite_Subtype incorrectly returns True. This
12062 -- needs fixing. ???
12063
12064 if Is_Definite_Subtype (A_Gen_T)
12065 and then not Unknown_Discriminants_Present (Formal)
12066 and then not Is_Definite_Subtype (Act_T)
12067 then
12068 Error_Msg_N ("actual subtype must be constrained", Actual);
12069 Abandon_Instantiation (Actual);
12070 end if;
12071
12072 if not Unknown_Discriminants_Present (Formal) then
12073 if Is_Constrained (Ancestor) then
12074 if not Is_Constrained (Act_T) then
12075 Error_Msg_N ("actual subtype must be constrained", Actual);
12076 Abandon_Instantiation (Actual);
12077 end if;
12078
12079 -- Ancestor is unconstrained, Check if generic formal and actual
12080 -- agree on constrainedness. The check only applies to array types
12081 -- and discriminated types.
12082
12083 elsif Is_Constrained (Act_T) then
12084 if Ekind (Ancestor) = E_Access_Type
12085 or else (not Is_Constrained (A_Gen_T)
12086 and then Is_Composite_Type (A_Gen_T))
12087 then
12088 Error_Msg_N ("actual subtype must be unconstrained", Actual);
12089 Abandon_Instantiation (Actual);
12090 end if;
12091
12092 -- A class-wide type is only allowed if the formal has unknown
12093 -- discriminants.
12094
12095 elsif Is_Class_Wide_Type (Act_T)
12096 and then not Has_Unknown_Discriminants (Ancestor)
12097 then
12098 Error_Msg_NE
12099 ("actual for & cannot be a class-wide type", Actual, Gen_T);
12100 Abandon_Instantiation (Actual);
12101
12102 -- Otherwise, the formal and actual must have the same number
12103 -- of discriminants and each discriminant of the actual must
12104 -- correspond to a discriminant of the formal.
12105
12106 elsif Has_Discriminants (Act_T)
12107 and then not Has_Unknown_Discriminants (Act_T)
12108 and then Has_Discriminants (Ancestor)
12109 then
12110 Actual_Discr := First_Discriminant (Act_T);
12111 Ancestor_Discr := First_Discriminant (Ancestor);
12112 while Present (Actual_Discr)
12113 and then Present (Ancestor_Discr)
12114 loop
12115 if Base_Type (Act_T) /= Base_Type (Ancestor) and then
12116 No (Corresponding_Discriminant (Actual_Discr))
12117 then
12118 Error_Msg_NE
12119 ("discriminant & does not correspond "
12120 & "to ancestor discriminant", Actual, Actual_Discr);
12121 Abandon_Instantiation (Actual);
12122 end if;
12123
12124 Next_Discriminant (Actual_Discr);
12125 Next_Discriminant (Ancestor_Discr);
12126 end loop;
12127
12128 if Present (Actual_Discr) or else Present (Ancestor_Discr) then
12129 Error_Msg_NE
12130 ("actual for & must have same number of discriminants",
12131 Actual, Gen_T);
12132 Abandon_Instantiation (Actual);
12133 end if;
12134
12135 -- This case should be caught by the earlier check for
12136 -- constrainedness, but the check here is added for completeness.
12137
12138 elsif Has_Discriminants (Act_T)
12139 and then not Has_Unknown_Discriminants (Act_T)
12140 then
12141 Error_Msg_NE
12142 ("actual for & must not have discriminants", Actual, Gen_T);
12143 Abandon_Instantiation (Actual);
12144
12145 elsif Has_Discriminants (Ancestor) then
12146 Error_Msg_NE
12147 ("actual for & must have known discriminants", Actual, Gen_T);
12148 Abandon_Instantiation (Actual);
12149 end if;
12150
12151 if not Subtypes_Statically_Compatible
12152 (Act_T, Ancestor, Formal_Derived_Matching => True)
12153 then
12154 Error_Msg_N
12155 ("constraint on actual is incompatible with formal", Actual);
12156 Abandon_Instantiation (Actual);
12157 end if;
12158 end if;
12159
12160 -- If the formal and actual types are abstract, check that there
12161 -- are no abstract primitives of the actual type that correspond to
12162 -- nonabstract primitives of the formal type (second sentence of
12163 -- RM95 3.9.3(9)).
12164
12165 if Is_Abstract_Type (A_Gen_T) and then Is_Abstract_Type (Act_T) then
12166 Check_Abstract_Primitives : declare
12167 Gen_Prims : constant Elist_Id :=
12168 Primitive_Operations (A_Gen_T);
12169 Gen_Elmt : Elmt_Id;
12170 Gen_Subp : Entity_Id;
12171 Anc_Subp : Entity_Id;
12172 Anc_Formal : Entity_Id;
12173 Anc_F_Type : Entity_Id;
12174
12175 Act_Prims : constant Elist_Id := Primitive_Operations (Act_T);
12176 Act_Elmt : Elmt_Id;
12177 Act_Subp : Entity_Id;
12178 Act_Formal : Entity_Id;
12179 Act_F_Type : Entity_Id;
12180
12181 Subprograms_Correspond : Boolean;
12182
12183 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean;
12184 -- Returns true if T2 is derived directly or indirectly from
12185 -- T1, including derivations from interfaces. T1 and T2 are
12186 -- required to be specific tagged base types.
12187
12188 ------------------------
12189 -- Is_Tagged_Ancestor --
12190 ------------------------
12191
12192 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean
12193 is
12194 Intfc_Elmt : Elmt_Id;
12195
12196 begin
12197 -- The predicate is satisfied if the types are the same
12198
12199 if T1 = T2 then
12200 return True;
12201
12202 -- If we've reached the top of the derivation chain then
12203 -- we know that T1 is not an ancestor of T2.
12204
12205 elsif Etype (T2) = T2 then
12206 return False;
12207
12208 -- Proceed to check T2's immediate parent
12209
12210 elsif Is_Ancestor (T1, Base_Type (Etype (T2))) then
12211 return True;
12212
12213 -- Finally, check to see if T1 is an ancestor of any of T2's
12214 -- progenitors.
12215
12216 else
12217 Intfc_Elmt := First_Elmt (Interfaces (T2));
12218 while Present (Intfc_Elmt) loop
12219 if Is_Ancestor (T1, Node (Intfc_Elmt)) then
12220 return True;
12221 end if;
12222
12223 Next_Elmt (Intfc_Elmt);
12224 end loop;
12225 end if;
12226
12227 return False;
12228 end Is_Tagged_Ancestor;
12229
12230 -- Start of processing for Check_Abstract_Primitives
12231
12232 begin
12233 -- Loop over all of the formal derived type's primitives
12234
12235 Gen_Elmt := First_Elmt (Gen_Prims);
12236 while Present (Gen_Elmt) loop
12237 Gen_Subp := Node (Gen_Elmt);
12238
12239 -- If the primitive of the formal is not abstract, then
12240 -- determine whether there is a corresponding primitive of
12241 -- the actual type that's abstract.
12242
12243 if not Is_Abstract_Subprogram (Gen_Subp) then
12244 Act_Elmt := First_Elmt (Act_Prims);
12245 while Present (Act_Elmt) loop
12246 Act_Subp := Node (Act_Elmt);
12247
12248 -- If we find an abstract primitive of the actual,
12249 -- then we need to test whether it corresponds to the
12250 -- subprogram from which the generic formal primitive
12251 -- is inherited.
12252
12253 if Is_Abstract_Subprogram (Act_Subp) then
12254 Anc_Subp := Alias (Gen_Subp);
12255
12256 -- Test whether we have a corresponding primitive
12257 -- by comparing names, kinds, formal types, and
12258 -- result types.
12259
12260 if Chars (Anc_Subp) = Chars (Act_Subp)
12261 and then Ekind (Anc_Subp) = Ekind (Act_Subp)
12262 then
12263 Anc_Formal := First_Formal (Anc_Subp);
12264 Act_Formal := First_Formal (Act_Subp);
12265 while Present (Anc_Formal)
12266 and then Present (Act_Formal)
12267 loop
12268 Anc_F_Type := Etype (Anc_Formal);
12269 Act_F_Type := Etype (Act_Formal);
12270
12271 if Ekind (Anc_F_Type) =
12272 E_Anonymous_Access_Type
12273 then
12274 Anc_F_Type := Designated_Type (Anc_F_Type);
12275
12276 if Ekind (Act_F_Type) =
12277 E_Anonymous_Access_Type
12278 then
12279 Act_F_Type :=
12280 Designated_Type (Act_F_Type);
12281 else
12282 exit;
12283 end if;
12284
12285 elsif
12286 Ekind (Act_F_Type) = E_Anonymous_Access_Type
12287 then
12288 exit;
12289 end if;
12290
12291 Anc_F_Type := Base_Type (Anc_F_Type);
12292 Act_F_Type := Base_Type (Act_F_Type);
12293
12294 -- If the formal is controlling, then the
12295 -- the type of the actual primitive's formal
12296 -- must be derived directly or indirectly
12297 -- from the type of the ancestor primitive's
12298 -- formal.
12299
12300 if Is_Controlling_Formal (Anc_Formal) then
12301 if not Is_Tagged_Ancestor
12302 (Anc_F_Type, Act_F_Type)
12303 then
12304 exit;
12305 end if;
12306
12307 -- Otherwise the types of the formals must
12308 -- be the same.
12309
12310 elsif Anc_F_Type /= Act_F_Type then
12311 exit;
12312 end if;
12313
12314 Next_Entity (Anc_Formal);
12315 Next_Entity (Act_Formal);
12316 end loop;
12317
12318 -- If we traversed through all of the formals
12319 -- then so far the subprograms correspond, so
12320 -- now check that any result types correspond.
12321
12322 if No (Anc_Formal) and then No (Act_Formal) then
12323 Subprograms_Correspond := True;
12324
12325 if Ekind (Act_Subp) = E_Function then
12326 Anc_F_Type := Etype (Anc_Subp);
12327 Act_F_Type := Etype (Act_Subp);
12328
12329 if Ekind (Anc_F_Type) =
12330 E_Anonymous_Access_Type
12331 then
12332 Anc_F_Type :=
12333 Designated_Type (Anc_F_Type);
12334
12335 if Ekind (Act_F_Type) =
12336 E_Anonymous_Access_Type
12337 then
12338 Act_F_Type :=
12339 Designated_Type (Act_F_Type);
12340 else
12341 Subprograms_Correspond := False;
12342 end if;
12343
12344 elsif
12345 Ekind (Act_F_Type)
12346 = E_Anonymous_Access_Type
12347 then
12348 Subprograms_Correspond := False;
12349 end if;
12350
12351 Anc_F_Type := Base_Type (Anc_F_Type);
12352 Act_F_Type := Base_Type (Act_F_Type);
12353
12354 -- Now either the result types must be
12355 -- the same or, if the result type is
12356 -- controlling, the result type of the
12357 -- actual primitive must descend from the
12358 -- result type of the ancestor primitive.
12359
12360 if Subprograms_Correspond
12361 and then Anc_F_Type /= Act_F_Type
12362 and then
12363 Has_Controlling_Result (Anc_Subp)
12364 and then not Is_Tagged_Ancestor
12365 (Anc_F_Type, Act_F_Type)
12366 then
12367 Subprograms_Correspond := False;
12368 end if;
12369 end if;
12370
12371 -- Found a matching subprogram belonging to
12372 -- formal ancestor type, so actual subprogram
12373 -- corresponds and this violates 3.9.3(9).
12374
12375 if Subprograms_Correspond then
12376 Error_Msg_NE
12377 ("abstract subprogram & overrides "
12378 & "nonabstract subprogram of ancestor",
12379 Actual, Act_Subp);
12380 end if;
12381 end if;
12382 end if;
12383 end if;
12384
12385 Next_Elmt (Act_Elmt);
12386 end loop;
12387 end if;
12388
12389 Next_Elmt (Gen_Elmt);
12390 end loop;
12391 end Check_Abstract_Primitives;
12392 end if;
12393
12394 -- Verify that limitedness matches. If parent is a limited
12395 -- interface then the generic formal is not unless declared
12396 -- explicitly so. If not declared limited, the actual cannot be
12397 -- limited (see AI05-0087).
12398
12399 -- Even though this AI is a binding interpretation, we enable the
12400 -- check only in Ada 2012 mode, because this improper construct
12401 -- shows up in user code and in existing B-tests.
12402
12403 if Is_Limited_Type (Act_T)
12404 and then not Is_Limited_Type (A_Gen_T)
12405 and then Ada_Version >= Ada_2012
12406 then
12407 if In_Instance then
12408 null;
12409 else
12410 Error_Msg_NE
12411 ("actual for non-limited & cannot be a limited type",
12412 Actual, Gen_T);
12413 Explain_Limited_Type (Act_T, Actual);
12414 Abandon_Instantiation (Actual);
12415 end if;
12416 end if;
12417 end Validate_Derived_Type_Instance;
12418
12419 ----------------------------------------
12420 -- Validate_Discriminated_Formal_Type --
12421 ----------------------------------------
12422
12423 procedure Validate_Discriminated_Formal_Type is
12424 Formal_Discr : Entity_Id;
12425 Actual_Discr : Entity_Id;
12426 Formal_Subt : Entity_Id;
12427
12428 begin
12429 if Has_Discriminants (A_Gen_T) then
12430 if not Has_Discriminants (Act_T) then
12431 Error_Msg_NE
12432 ("actual for & must have discriminants", Actual, Gen_T);
12433 Abandon_Instantiation (Actual);
12434
12435 elsif Is_Constrained (Act_T) then
12436 Error_Msg_NE
12437 ("actual for & must be unconstrained", Actual, Gen_T);
12438 Abandon_Instantiation (Actual);
12439
12440 else
12441 Formal_Discr := First_Discriminant (A_Gen_T);
12442 Actual_Discr := First_Discriminant (Act_T);
12443 while Formal_Discr /= Empty loop
12444 if Actual_Discr = Empty then
12445 Error_Msg_NE
12446 ("discriminants on actual do not match formal",
12447 Actual, Gen_T);
12448 Abandon_Instantiation (Actual);
12449 end if;
12450
12451 Formal_Subt := Get_Instance_Of (Etype (Formal_Discr));
12452
12453 -- Access discriminants match if designated types do
12454
12455 if Ekind (Base_Type (Formal_Subt)) = E_Anonymous_Access_Type
12456 and then (Ekind (Base_Type (Etype (Actual_Discr)))) =
12457 E_Anonymous_Access_Type
12458 and then
12459 Get_Instance_Of
12460 (Designated_Type (Base_Type (Formal_Subt))) =
12461 Designated_Type (Base_Type (Etype (Actual_Discr)))
12462 then
12463 null;
12464
12465 elsif Base_Type (Formal_Subt) /=
12466 Base_Type (Etype (Actual_Discr))
12467 then
12468 Error_Msg_NE
12469 ("types of actual discriminants must match formal",
12470 Actual, Gen_T);
12471 Abandon_Instantiation (Actual);
12472
12473 elsif not Subtypes_Statically_Match
12474 (Formal_Subt, Etype (Actual_Discr))
12475 and then Ada_Version >= Ada_95
12476 then
12477 Error_Msg_NE
12478 ("subtypes of actual discriminants must match formal",
12479 Actual, Gen_T);
12480 Abandon_Instantiation (Actual);
12481 end if;
12482
12483 Next_Discriminant (Formal_Discr);
12484 Next_Discriminant (Actual_Discr);
12485 end loop;
12486
12487 if Actual_Discr /= Empty then
12488 Error_Msg_NE
12489 ("discriminants on actual do not match formal",
12490 Actual, Gen_T);
12491 Abandon_Instantiation (Actual);
12492 end if;
12493 end if;
12494 end if;
12495 end Validate_Discriminated_Formal_Type;
12496
12497 ---------------------------------------
12498 -- Validate_Incomplete_Type_Instance --
12499 ---------------------------------------
12500
12501 procedure Validate_Incomplete_Type_Instance is
12502 begin
12503 if not Is_Tagged_Type (Act_T)
12504 and then Is_Tagged_Type (A_Gen_T)
12505 then
12506 Error_Msg_NE
12507 ("actual for & must be a tagged type", Actual, Gen_T);
12508 end if;
12509
12510 Validate_Discriminated_Formal_Type;
12511 end Validate_Incomplete_Type_Instance;
12512
12513 --------------------------------------
12514 -- Validate_Interface_Type_Instance --
12515 --------------------------------------
12516
12517 procedure Validate_Interface_Type_Instance is
12518 begin
12519 if not Is_Interface (Act_T) then
12520 Error_Msg_NE
12521 ("actual for formal interface type must be an interface",
12522 Actual, Gen_T);
12523
12524 elsif Is_Limited_Type (Act_T) /= Is_Limited_Type (A_Gen_T)
12525 or else Is_Task_Interface (A_Gen_T) /= Is_Task_Interface (Act_T)
12526 or else Is_Protected_Interface (A_Gen_T) /=
12527 Is_Protected_Interface (Act_T)
12528 or else Is_Synchronized_Interface (A_Gen_T) /=
12529 Is_Synchronized_Interface (Act_T)
12530 then
12531 Error_Msg_NE
12532 ("actual for interface& does not match (RM 12.5.5(4))",
12533 Actual, Gen_T);
12534 end if;
12535 end Validate_Interface_Type_Instance;
12536
12537 ------------------------------------
12538 -- Validate_Private_Type_Instance --
12539 ------------------------------------
12540
12541 procedure Validate_Private_Type_Instance is
12542 begin
12543 if Is_Limited_Type (Act_T)
12544 and then not Is_Limited_Type (A_Gen_T)
12545 then
12546 if In_Instance then
12547 null;
12548 else
12549 Error_Msg_NE
12550 ("actual for non-limited & cannot be a limited type", Actual,
12551 Gen_T);
12552 Explain_Limited_Type (Act_T, Actual);
12553 Abandon_Instantiation (Actual);
12554 end if;
12555
12556 elsif Known_To_Have_Preelab_Init (A_Gen_T)
12557 and then not Has_Preelaborable_Initialization (Act_T)
12558 then
12559 Error_Msg_NE
12560 ("actual for & must have preelaborable initialization", Actual,
12561 Gen_T);
12562
12563 elsif not Is_Definite_Subtype (Act_T)
12564 and then Is_Definite_Subtype (A_Gen_T)
12565 and then Ada_Version >= Ada_95
12566 then
12567 Error_Msg_NE
12568 ("actual for & must be a definite subtype", Actual, Gen_T);
12569
12570 elsif not Is_Tagged_Type (Act_T)
12571 and then Is_Tagged_Type (A_Gen_T)
12572 then
12573 Error_Msg_NE
12574 ("actual for & must be a tagged type", Actual, Gen_T);
12575 end if;
12576
12577 Validate_Discriminated_Formal_Type;
12578 Ancestor := Gen_T;
12579 end Validate_Private_Type_Instance;
12580
12581 -- Start of processing for Instantiate_Type
12582
12583 begin
12584 if Get_Instance_Of (A_Gen_T) /= A_Gen_T then
12585 Error_Msg_N ("duplicate instantiation of generic type", Actual);
12586 return New_List (Error);
12587
12588 elsif not Is_Entity_Name (Actual)
12589 or else not Is_Type (Entity (Actual))
12590 then
12591 Error_Msg_NE
12592 ("expect valid subtype mark to instantiate &", Actual, Gen_T);
12593 Abandon_Instantiation (Actual);
12594
12595 else
12596 Act_T := Entity (Actual);
12597
12598 -- Ada 2005 (AI-216): An Unchecked_Union subtype shall only be passed
12599 -- as a generic actual parameter if the corresponding formal type
12600 -- does not have a known_discriminant_part, or is a formal derived
12601 -- type that is an Unchecked_Union type.
12602
12603 if Is_Unchecked_Union (Base_Type (Act_T)) then
12604 if not Has_Discriminants (A_Gen_T)
12605 or else (Is_Derived_Type (A_Gen_T)
12606 and then Is_Unchecked_Union (A_Gen_T))
12607 then
12608 null;
12609 else
12610 Error_Msg_N ("unchecked union cannot be the actual for a "
12611 & "discriminated formal type", Act_T);
12612
12613 end if;
12614 end if;
12615
12616 -- Deal with fixed/floating restrictions
12617
12618 if Is_Floating_Point_Type (Act_T) then
12619 Check_Restriction (No_Floating_Point, Actual);
12620 elsif Is_Fixed_Point_Type (Act_T) then
12621 Check_Restriction (No_Fixed_Point, Actual);
12622 end if;
12623
12624 -- Deal with error of using incomplete type as generic actual.
12625 -- This includes limited views of a type, even if the non-limited
12626 -- view may be available.
12627
12628 if Ekind (Act_T) = E_Incomplete_Type
12629 or else (Is_Class_Wide_Type (Act_T)
12630 and then Ekind (Root_Type (Act_T)) = E_Incomplete_Type)
12631 then
12632 -- If the formal is an incomplete type, the actual can be
12633 -- incomplete as well.
12634
12635 if Ekind (A_Gen_T) = E_Incomplete_Type then
12636 null;
12637
12638 elsif Is_Class_Wide_Type (Act_T)
12639 or else No (Full_View (Act_T))
12640 then
12641 Error_Msg_N ("premature use of incomplete type", Actual);
12642 Abandon_Instantiation (Actual);
12643 else
12644 Act_T := Full_View (Act_T);
12645 Set_Entity (Actual, Act_T);
12646
12647 if Has_Private_Component (Act_T) then
12648 Error_Msg_N
12649 ("premature use of type with private component", Actual);
12650 end if;
12651 end if;
12652
12653 -- Deal with error of premature use of private type as generic actual
12654
12655 elsif Is_Private_Type (Act_T)
12656 and then Is_Private_Type (Base_Type (Act_T))
12657 and then not Is_Generic_Type (Act_T)
12658 and then not Is_Derived_Type (Act_T)
12659 and then No (Full_View (Root_Type (Act_T)))
12660 then
12661 -- If the formal is an incomplete type, the actual can be
12662 -- private or incomplete as well.
12663
12664 if Ekind (A_Gen_T) = E_Incomplete_Type then
12665 null;
12666 else
12667 Error_Msg_N ("premature use of private type", Actual);
12668 end if;
12669
12670 elsif Has_Private_Component (Act_T) then
12671 Error_Msg_N
12672 ("premature use of type with private component", Actual);
12673 end if;
12674
12675 Set_Instance_Of (A_Gen_T, Act_T);
12676
12677 -- If the type is generic, the class-wide type may also be used
12678
12679 if Is_Tagged_Type (A_Gen_T)
12680 and then Is_Tagged_Type (Act_T)
12681 and then not Is_Class_Wide_Type (A_Gen_T)
12682 then
12683 Set_Instance_Of (Class_Wide_Type (A_Gen_T),
12684 Class_Wide_Type (Act_T));
12685 end if;
12686
12687 if not Is_Abstract_Type (A_Gen_T)
12688 and then Is_Abstract_Type (Act_T)
12689 then
12690 Error_Msg_N
12691 ("actual of non-abstract formal cannot be abstract", Actual);
12692 end if;
12693
12694 -- A generic scalar type is a first subtype for which we generate
12695 -- an anonymous base type. Indicate that the instance of this base
12696 -- is the base type of the actual.
12697
12698 if Is_Scalar_Type (A_Gen_T) then
12699 Set_Instance_Of (Etype (A_Gen_T), Etype (Act_T));
12700 end if;
12701 end if;
12702
12703 if Error_Posted (Act_T) then
12704 null;
12705 else
12706 case Nkind (Def) is
12707 when N_Formal_Private_Type_Definition =>
12708 Validate_Private_Type_Instance;
12709
12710 when N_Formal_Incomplete_Type_Definition =>
12711 Validate_Incomplete_Type_Instance;
12712
12713 when N_Formal_Derived_Type_Definition =>
12714 Validate_Derived_Type_Instance;
12715
12716 when N_Formal_Discrete_Type_Definition =>
12717 if not Is_Discrete_Type (Act_T) then
12718 Error_Msg_NE
12719 ("expect discrete type in instantiation of&",
12720 Actual, Gen_T);
12721 Abandon_Instantiation (Actual);
12722 end if;
12723
12724 Diagnose_Predicated_Actual;
12725
12726 when N_Formal_Signed_Integer_Type_Definition =>
12727 if not Is_Signed_Integer_Type (Act_T) then
12728 Error_Msg_NE
12729 ("expect signed integer type in instantiation of&",
12730 Actual, Gen_T);
12731 Abandon_Instantiation (Actual);
12732 end if;
12733
12734 Diagnose_Predicated_Actual;
12735
12736 when N_Formal_Modular_Type_Definition =>
12737 if not Is_Modular_Integer_Type (Act_T) then
12738 Error_Msg_NE
12739 ("expect modular type in instantiation of &",
12740 Actual, Gen_T);
12741 Abandon_Instantiation (Actual);
12742 end if;
12743
12744 Diagnose_Predicated_Actual;
12745
12746 when N_Formal_Floating_Point_Definition =>
12747 if not Is_Floating_Point_Type (Act_T) then
12748 Error_Msg_NE
12749 ("expect float type in instantiation of &", Actual, Gen_T);
12750 Abandon_Instantiation (Actual);
12751 end if;
12752
12753 when N_Formal_Ordinary_Fixed_Point_Definition =>
12754 if not Is_Ordinary_Fixed_Point_Type (Act_T) then
12755 Error_Msg_NE
12756 ("expect ordinary fixed point type in instantiation of &",
12757 Actual, Gen_T);
12758 Abandon_Instantiation (Actual);
12759 end if;
12760
12761 when N_Formal_Decimal_Fixed_Point_Definition =>
12762 if not Is_Decimal_Fixed_Point_Type (Act_T) then
12763 Error_Msg_NE
12764 ("expect decimal type in instantiation of &",
12765 Actual, Gen_T);
12766 Abandon_Instantiation (Actual);
12767 end if;
12768
12769 when N_Array_Type_Definition =>
12770 Validate_Array_Type_Instance;
12771
12772 when N_Access_To_Object_Definition =>
12773 Validate_Access_Type_Instance;
12774
12775 when N_Access_Function_Definition |
12776 N_Access_Procedure_Definition =>
12777 Validate_Access_Subprogram_Instance;
12778
12779 when N_Record_Definition =>
12780 Validate_Interface_Type_Instance;
12781
12782 when N_Derived_Type_Definition =>
12783 Validate_Derived_Interface_Type_Instance;
12784
12785 when others =>
12786 raise Program_Error;
12787
12788 end case;
12789 end if;
12790
12791 Subt := New_Copy (Gen_T);
12792
12793 -- Use adjusted sloc of subtype name as the location for other nodes in
12794 -- the subtype declaration.
12795
12796 Loc := Sloc (Subt);
12797
12798 Decl_Node :=
12799 Make_Subtype_Declaration (Loc,
12800 Defining_Identifier => Subt,
12801 Subtype_Indication => New_Occurrence_Of (Act_T, Loc));
12802
12803 if Is_Private_Type (Act_T) then
12804 Set_Has_Private_View (Subtype_Indication (Decl_Node));
12805
12806 elsif Is_Access_Type (Act_T)
12807 and then Is_Private_Type (Designated_Type (Act_T))
12808 then
12809 Set_Has_Private_View (Subtype_Indication (Decl_Node));
12810 end if;
12811
12812 -- In Ada 2012 the actual may be a limited view. Indicate that
12813 -- the local subtype must be treated as such.
12814
12815 if From_Limited_With (Act_T) then
12816 Set_Ekind (Subt, E_Incomplete_Subtype);
12817 Set_From_Limited_With (Subt);
12818 end if;
12819
12820 Decl_Nodes := New_List (Decl_Node);
12821
12822 -- Flag actual derived types so their elaboration produces the
12823 -- appropriate renamings for the primitive operations of the ancestor.
12824 -- Flag actual for formal private types as well, to determine whether
12825 -- operations in the private part may override inherited operations.
12826 -- If the formal has an interface list, the ancestor is not the
12827 -- parent, but the analyzed formal that includes the interface
12828 -- operations of all its progenitors.
12829
12830 -- Same treatment for formal private types, so we can check whether the
12831 -- type is tagged limited when validating derivations in the private
12832 -- part. (See AI05-096).
12833
12834 if Nkind (Def) = N_Formal_Derived_Type_Definition then
12835 if Present (Interface_List (Def)) then
12836 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
12837 else
12838 Set_Generic_Parent_Type (Decl_Node, Ancestor);
12839 end if;
12840
12841 elsif Nkind_In (Def, N_Formal_Private_Type_Definition,
12842 N_Formal_Incomplete_Type_Definition)
12843 then
12844 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
12845 end if;
12846
12847 -- If the actual is a synchronized type that implements an interface,
12848 -- the primitive operations are attached to the corresponding record,
12849 -- and we have to treat it as an additional generic actual, so that its
12850 -- primitive operations become visible in the instance. The task or
12851 -- protected type itself does not carry primitive operations.
12852
12853 if Is_Concurrent_Type (Act_T)
12854 and then Is_Tagged_Type (Act_T)
12855 and then Present (Corresponding_Record_Type (Act_T))
12856 and then Present (Ancestor)
12857 and then Is_Interface (Ancestor)
12858 then
12859 declare
12860 Corr_Rec : constant Entity_Id :=
12861 Corresponding_Record_Type (Act_T);
12862 New_Corr : Entity_Id;
12863 Corr_Decl : Node_Id;
12864
12865 begin
12866 New_Corr := Make_Temporary (Loc, 'S');
12867 Corr_Decl :=
12868 Make_Subtype_Declaration (Loc,
12869 Defining_Identifier => New_Corr,
12870 Subtype_Indication =>
12871 New_Occurrence_Of (Corr_Rec, Loc));
12872 Append_To (Decl_Nodes, Corr_Decl);
12873
12874 if Ekind (Act_T) = E_Task_Type then
12875 Set_Ekind (Subt, E_Task_Subtype);
12876 else
12877 Set_Ekind (Subt, E_Protected_Subtype);
12878 end if;
12879
12880 Set_Corresponding_Record_Type (Subt, Corr_Rec);
12881 Set_Generic_Parent_Type (Corr_Decl, Ancestor);
12882 Set_Generic_Parent_Type (Decl_Node, Empty);
12883 end;
12884 end if;
12885
12886 -- For a floating-point type, capture dimension info if any, because
12887 -- the generated subtype declaration does not come from source and
12888 -- will not process dimensions.
12889
12890 if Is_Floating_Point_Type (Act_T) then
12891 Copy_Dimensions (Act_T, Subt);
12892 end if;
12893
12894 return Decl_Nodes;
12895 end Instantiate_Type;
12896
12897 ---------------------
12898 -- Is_In_Main_Unit --
12899 ---------------------
12900
12901 function Is_In_Main_Unit (N : Node_Id) return Boolean is
12902 Unum : constant Unit_Number_Type := Get_Source_Unit (N);
12903 Current_Unit : Node_Id;
12904
12905 begin
12906 if Unum = Main_Unit then
12907 return True;
12908
12909 -- If the current unit is a subunit then it is either the main unit or
12910 -- is being compiled as part of the main unit.
12911
12912 elsif Nkind (N) = N_Compilation_Unit then
12913 return Nkind (Unit (N)) = N_Subunit;
12914 end if;
12915
12916 Current_Unit := Parent (N);
12917
12918 while Present (Current_Unit)
12919 and then Nkind (Current_Unit) /= N_Compilation_Unit
12920 loop
12921 Current_Unit := Parent (Current_Unit);
12922 end loop;
12923
12924 -- The instantiation node is in the main unit, or else the current node
12925 -- (perhaps as the result of nested instantiations) is in the main unit,
12926 -- or in the declaration of the main unit, which in this last case must
12927 -- be a body.
12928
12929 return
12930 Current_Unit = Cunit (Main_Unit)
12931 or else Current_Unit = Library_Unit (Cunit (Main_Unit))
12932 or else (Present (Current_Unit)
12933 and then Present (Library_Unit (Current_Unit))
12934 and then Is_In_Main_Unit (Library_Unit (Current_Unit)));
12935 end Is_In_Main_Unit;
12936
12937 ----------------------------
12938 -- Load_Parent_Of_Generic --
12939 ----------------------------
12940
12941 procedure Load_Parent_Of_Generic
12942 (N : Node_Id;
12943 Spec : Node_Id;
12944 Body_Optional : Boolean := False)
12945 is
12946 Comp_Unit : constant Node_Id := Cunit (Get_Source_Unit (Spec));
12947 Saved_Style_Check : constant Boolean := Style_Check;
12948 Saved_Warnings : constant Warning_Record := Save_Warnings;
12949 True_Parent : Node_Id;
12950 Inst_Node : Node_Id;
12951 OK : Boolean;
12952 Previous_Instances : constant Elist_Id := New_Elmt_List;
12953
12954 procedure Collect_Previous_Instances (Decls : List_Id);
12955 -- Collect all instantiations in the given list of declarations, that
12956 -- precede the generic that we need to load. If the bodies of these
12957 -- instantiations are available, we must analyze them, to ensure that
12958 -- the public symbols generated are the same when the unit is compiled
12959 -- to generate code, and when it is compiled in the context of a unit
12960 -- that needs a particular nested instance. This process is applied to
12961 -- both package and subprogram instances.
12962
12963 --------------------------------
12964 -- Collect_Previous_Instances --
12965 --------------------------------
12966
12967 procedure Collect_Previous_Instances (Decls : List_Id) is
12968 Decl : Node_Id;
12969
12970 begin
12971 Decl := First (Decls);
12972 while Present (Decl) loop
12973 if Sloc (Decl) >= Sloc (Inst_Node) then
12974 return;
12975
12976 -- If Decl is an instantiation, then record it as requiring
12977 -- instantiation of the corresponding body, except if it is an
12978 -- abbreviated instantiation generated internally for conformance
12979 -- checking purposes only for the case of a formal package
12980 -- declared without a box (see Instantiate_Formal_Package). Such
12981 -- an instantiation does not generate any code (the actual code
12982 -- comes from actual) and thus does not need to be analyzed here.
12983 -- If the instantiation appears with a generic package body it is
12984 -- not analyzed here either.
12985
12986 elsif Nkind (Decl) = N_Package_Instantiation
12987 and then not Is_Internal (Defining_Entity (Decl))
12988 then
12989 Append_Elmt (Decl, Previous_Instances);
12990
12991 -- For a subprogram instantiation, omit instantiations intrinsic
12992 -- operations (Unchecked_Conversions, etc.) that have no bodies.
12993
12994 elsif Nkind_In (Decl, N_Function_Instantiation,
12995 N_Procedure_Instantiation)
12996 and then not Is_Intrinsic_Subprogram (Entity (Name (Decl)))
12997 then
12998 Append_Elmt (Decl, Previous_Instances);
12999
13000 elsif Nkind (Decl) = N_Package_Declaration then
13001 Collect_Previous_Instances
13002 (Visible_Declarations (Specification (Decl)));
13003 Collect_Previous_Instances
13004 (Private_Declarations (Specification (Decl)));
13005
13006 -- Previous non-generic bodies may contain instances as well
13007
13008 elsif Nkind (Decl) = N_Package_Body
13009 and then Ekind (Corresponding_Spec (Decl)) /= E_Generic_Package
13010 then
13011 Collect_Previous_Instances (Declarations (Decl));
13012
13013 elsif Nkind (Decl) = N_Subprogram_Body
13014 and then not Acts_As_Spec (Decl)
13015 and then not Is_Generic_Subprogram (Corresponding_Spec (Decl))
13016 then
13017 Collect_Previous_Instances (Declarations (Decl));
13018 end if;
13019
13020 Next (Decl);
13021 end loop;
13022 end Collect_Previous_Instances;
13023
13024 -- Start of processing for Load_Parent_Of_Generic
13025
13026 begin
13027 if not In_Same_Source_Unit (N, Spec)
13028 or else Nkind (Unit (Comp_Unit)) = N_Package_Declaration
13029 or else (Nkind (Unit (Comp_Unit)) = N_Package_Body
13030 and then not Is_In_Main_Unit (Spec))
13031 then
13032 -- Find body of parent of spec, and analyze it. A special case arises
13033 -- when the parent is an instantiation, that is to say when we are
13034 -- currently instantiating a nested generic. In that case, there is
13035 -- no separate file for the body of the enclosing instance. Instead,
13036 -- the enclosing body must be instantiated as if it were a pending
13037 -- instantiation, in order to produce the body for the nested generic
13038 -- we require now. Note that in that case the generic may be defined
13039 -- in a package body, the instance defined in the same package body,
13040 -- and the original enclosing body may not be in the main unit.
13041
13042 Inst_Node := Empty;
13043
13044 True_Parent := Parent (Spec);
13045 while Present (True_Parent)
13046 and then Nkind (True_Parent) /= N_Compilation_Unit
13047 loop
13048 if Nkind (True_Parent) = N_Package_Declaration
13049 and then
13050 Nkind (Original_Node (True_Parent)) = N_Package_Instantiation
13051 then
13052 -- Parent is a compilation unit that is an instantiation.
13053 -- Instantiation node has been replaced with package decl.
13054
13055 Inst_Node := Original_Node (True_Parent);
13056 exit;
13057
13058 elsif Nkind (True_Parent) = N_Package_Declaration
13059 and then Present (Generic_Parent (Specification (True_Parent)))
13060 and then Nkind (Parent (True_Parent)) /= N_Compilation_Unit
13061 then
13062 -- Parent is an instantiation within another specification.
13063 -- Declaration for instance has been inserted before original
13064 -- instantiation node. A direct link would be preferable?
13065
13066 Inst_Node := Next (True_Parent);
13067 while Present (Inst_Node)
13068 and then Nkind (Inst_Node) /= N_Package_Instantiation
13069 loop
13070 Next (Inst_Node);
13071 end loop;
13072
13073 -- If the instance appears within a generic, and the generic
13074 -- unit is defined within a formal package of the enclosing
13075 -- generic, there is no generic body available, and none
13076 -- needed. A more precise test should be used ???
13077
13078 if No (Inst_Node) then
13079 return;
13080 end if;
13081
13082 exit;
13083
13084 else
13085 True_Parent := Parent (True_Parent);
13086 end if;
13087 end loop;
13088
13089 -- Case where we are currently instantiating a nested generic
13090
13091 if Present (Inst_Node) then
13092 if Nkind (Parent (True_Parent)) = N_Compilation_Unit then
13093
13094 -- Instantiation node and declaration of instantiated package
13095 -- were exchanged when only the declaration was needed.
13096 -- Restore instantiation node before proceeding with body.
13097
13098 Set_Unit (Parent (True_Parent), Inst_Node);
13099 end if;
13100
13101 -- Now complete instantiation of enclosing body, if it appears in
13102 -- some other unit. If it appears in the current unit, the body
13103 -- will have been instantiated already.
13104
13105 if No (Corresponding_Body (Instance_Spec (Inst_Node))) then
13106
13107 -- We need to determine the expander mode to instantiate the
13108 -- enclosing body. Because the generic body we need may use
13109 -- global entities declared in the enclosing package (including
13110 -- aggregates) it is in general necessary to compile this body
13111 -- with expansion enabled, except if we are within a generic
13112 -- package, in which case the usual generic rule applies.
13113
13114 declare
13115 Exp_Status : Boolean := True;
13116 Scop : Entity_Id;
13117
13118 begin
13119 -- Loop through scopes looking for generic package
13120
13121 Scop := Scope (Defining_Entity (Instance_Spec (Inst_Node)));
13122 while Present (Scop)
13123 and then Scop /= Standard_Standard
13124 loop
13125 if Ekind (Scop) = E_Generic_Package then
13126 Exp_Status := False;
13127 exit;
13128 end if;
13129
13130 Scop := Scope (Scop);
13131 end loop;
13132
13133 -- Collect previous instantiations in the unit that contains
13134 -- the desired generic.
13135
13136 if Nkind (Parent (True_Parent)) /= N_Compilation_Unit
13137 and then not Body_Optional
13138 then
13139 declare
13140 Decl : Elmt_Id;
13141 Info : Pending_Body_Info;
13142 Par : Node_Id;
13143
13144 begin
13145 Par := Parent (Inst_Node);
13146 while Present (Par) loop
13147 exit when Nkind (Parent (Par)) = N_Compilation_Unit;
13148 Par := Parent (Par);
13149 end loop;
13150
13151 pragma Assert (Present (Par));
13152
13153 if Nkind (Par) = N_Package_Body then
13154 Collect_Previous_Instances (Declarations (Par));
13155
13156 elsif Nkind (Par) = N_Package_Declaration then
13157 Collect_Previous_Instances
13158 (Visible_Declarations (Specification (Par)));
13159 Collect_Previous_Instances
13160 (Private_Declarations (Specification (Par)));
13161
13162 else
13163 -- Enclosing unit is a subprogram body. In this
13164 -- case all instance bodies are processed in order
13165 -- and there is no need to collect them separately.
13166
13167 null;
13168 end if;
13169
13170 Decl := First_Elmt (Previous_Instances);
13171 while Present (Decl) loop
13172 Info :=
13173 (Inst_Node => Node (Decl),
13174 Act_Decl =>
13175 Instance_Spec (Node (Decl)),
13176 Expander_Status => Exp_Status,
13177 Current_Sem_Unit =>
13178 Get_Code_Unit (Sloc (Node (Decl))),
13179 Scope_Suppress => Scope_Suppress,
13180 Local_Suppress_Stack_Top =>
13181 Local_Suppress_Stack_Top,
13182 Version => Ada_Version,
13183 Version_Pragma => Ada_Version_Pragma,
13184 Warnings => Save_Warnings,
13185 SPARK_Mode => SPARK_Mode,
13186 SPARK_Mode_Pragma => SPARK_Mode_Pragma);
13187
13188 -- Package instance
13189
13190 if
13191 Nkind (Node (Decl)) = N_Package_Instantiation
13192 then
13193 Instantiate_Package_Body
13194 (Info, Body_Optional => True);
13195
13196 -- Subprogram instance
13197
13198 else
13199 -- The instance_spec is in the wrapper package,
13200 -- usually followed by its local renaming
13201 -- declaration. See Build_Subprogram_Renaming
13202 -- for details. If the instance carries aspects,
13203 -- these result in the corresponding pragmas,
13204 -- inserted after the subprogram declaration.
13205 -- They must be skipped as well when retrieving
13206 -- the desired spec. A direct link would be
13207 -- more robust ???
13208
13209 declare
13210 Decl : Node_Id :=
13211 (Last (Visible_Declarations
13212 (Specification (Info.Act_Decl))));
13213 begin
13214 while Nkind_In (Decl,
13215 N_Subprogram_Renaming_Declaration, N_Pragma)
13216 loop
13217 Decl := Prev (Decl);
13218 end loop;
13219
13220 Info.Act_Decl := Decl;
13221 end;
13222
13223 Instantiate_Subprogram_Body
13224 (Info, Body_Optional => True);
13225 end if;
13226
13227 Next_Elmt (Decl);
13228 end loop;
13229 end;
13230 end if;
13231
13232 Instantiate_Package_Body
13233 (Body_Info =>
13234 ((Inst_Node => Inst_Node,
13235 Act_Decl => True_Parent,
13236 Expander_Status => Exp_Status,
13237 Current_Sem_Unit => Get_Code_Unit
13238 (Sloc (Inst_Node)),
13239 Scope_Suppress => Scope_Suppress,
13240 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
13241 Version => Ada_Version,
13242 Version_Pragma => Ada_Version_Pragma,
13243 Warnings => Save_Warnings,
13244 SPARK_Mode => SPARK_Mode,
13245 SPARK_Mode_Pragma => SPARK_Mode_Pragma)),
13246 Body_Optional => Body_Optional);
13247 end;
13248 end if;
13249
13250 -- Case where we are not instantiating a nested generic
13251
13252 else
13253 Opt.Style_Check := False;
13254 Expander_Mode_Save_And_Set (True);
13255 Load_Needed_Body (Comp_Unit, OK);
13256 Opt.Style_Check := Saved_Style_Check;
13257 Restore_Warnings (Saved_Warnings);
13258 Expander_Mode_Restore;
13259
13260 if not OK
13261 and then Unit_Requires_Body (Defining_Entity (Spec))
13262 and then not Body_Optional
13263 then
13264 declare
13265 Bname : constant Unit_Name_Type :=
13266 Get_Body_Name (Get_Unit_Name (Unit (Comp_Unit)));
13267
13268 begin
13269 -- In CodePeer mode, the missing body may make the analysis
13270 -- incomplete, but we do not treat it as fatal.
13271
13272 if CodePeer_Mode then
13273 return;
13274
13275 else
13276 Error_Msg_Unit_1 := Bname;
13277 Error_Msg_N ("this instantiation requires$!", N);
13278 Error_Msg_File_1 :=
13279 Get_File_Name (Bname, Subunit => False);
13280 Error_Msg_N ("\but file{ was not found!", N);
13281 raise Unrecoverable_Error;
13282 end if;
13283 end;
13284 end if;
13285 end if;
13286 end if;
13287
13288 -- If loading parent of the generic caused an instantiation circularity,
13289 -- we abandon compilation at this point, because otherwise in some cases
13290 -- we get into trouble with infinite recursions after this point.
13291
13292 if Circularity_Detected then
13293 raise Unrecoverable_Error;
13294 end if;
13295 end Load_Parent_Of_Generic;
13296
13297 ---------------------------------
13298 -- Map_Formal_Package_Entities --
13299 ---------------------------------
13300
13301 procedure Map_Formal_Package_Entities (Form : Entity_Id; Act : Entity_Id) is
13302 E1 : Entity_Id;
13303 E2 : Entity_Id;
13304
13305 begin
13306 Set_Instance_Of (Form, Act);
13307
13308 -- Traverse formal and actual package to map the corresponding entities.
13309 -- We skip over internal entities that may be generated during semantic
13310 -- analysis, and find the matching entities by name, given that they
13311 -- must appear in the same order.
13312
13313 E1 := First_Entity (Form);
13314 E2 := First_Entity (Act);
13315 while Present (E1) and then E1 /= First_Private_Entity (Form) loop
13316 -- Could this test be a single condition??? Seems like it could, and
13317 -- isn't FPE (Form) a constant anyway???
13318
13319 if not Is_Internal (E1)
13320 and then Present (Parent (E1))
13321 and then not Is_Class_Wide_Type (E1)
13322 and then not Is_Internal_Name (Chars (E1))
13323 then
13324 while Present (E2) and then Chars (E2) /= Chars (E1) loop
13325 Next_Entity (E2);
13326 end loop;
13327
13328 if No (E2) then
13329 exit;
13330 else
13331 Set_Instance_Of (E1, E2);
13332
13333 if Is_Type (E1) and then Is_Tagged_Type (E2) then
13334 Set_Instance_Of (Class_Wide_Type (E1), Class_Wide_Type (E2));
13335 end if;
13336
13337 if Is_Constrained (E1) then
13338 Set_Instance_Of (Base_Type (E1), Base_Type (E2));
13339 end if;
13340
13341 if Ekind (E1) = E_Package and then No (Renamed_Object (E1)) then
13342 Map_Formal_Package_Entities (E1, E2);
13343 end if;
13344 end if;
13345 end if;
13346
13347 Next_Entity (E1);
13348 end loop;
13349 end Map_Formal_Package_Entities;
13350
13351 -----------------------
13352 -- Move_Freeze_Nodes --
13353 -----------------------
13354
13355 procedure Move_Freeze_Nodes
13356 (Out_Of : Entity_Id;
13357 After : Node_Id;
13358 L : List_Id)
13359 is
13360 Decl : Node_Id;
13361 Next_Decl : Node_Id;
13362 Next_Node : Node_Id := After;
13363 Spec : Node_Id;
13364
13365 function Is_Outer_Type (T : Entity_Id) return Boolean;
13366 -- Check whether entity is declared in a scope external to that of the
13367 -- generic unit.
13368
13369 -------------------
13370 -- Is_Outer_Type --
13371 -------------------
13372
13373 function Is_Outer_Type (T : Entity_Id) return Boolean is
13374 Scop : Entity_Id := Scope (T);
13375
13376 begin
13377 if Scope_Depth (Scop) < Scope_Depth (Out_Of) then
13378 return True;
13379
13380 else
13381 while Scop /= Standard_Standard loop
13382 if Scop = Out_Of then
13383 return False;
13384 else
13385 Scop := Scope (Scop);
13386 end if;
13387 end loop;
13388
13389 return True;
13390 end if;
13391 end Is_Outer_Type;
13392
13393 -- Start of processing for Move_Freeze_Nodes
13394
13395 begin
13396 if No (L) then
13397 return;
13398 end if;
13399
13400 -- First remove the freeze nodes that may appear before all other
13401 -- declarations.
13402
13403 Decl := First (L);
13404 while Present (Decl)
13405 and then Nkind (Decl) = N_Freeze_Entity
13406 and then Is_Outer_Type (Entity (Decl))
13407 loop
13408 Decl := Remove_Head (L);
13409 Insert_After (Next_Node, Decl);
13410 Set_Analyzed (Decl, False);
13411 Next_Node := Decl;
13412 Decl := First (L);
13413 end loop;
13414
13415 -- Next scan the list of declarations and remove each freeze node that
13416 -- appears ahead of the current node.
13417
13418 while Present (Decl) loop
13419 while Present (Next (Decl))
13420 and then Nkind (Next (Decl)) = N_Freeze_Entity
13421 and then Is_Outer_Type (Entity (Next (Decl)))
13422 loop
13423 Next_Decl := Remove_Next (Decl);
13424 Insert_After (Next_Node, Next_Decl);
13425 Set_Analyzed (Next_Decl, False);
13426 Next_Node := Next_Decl;
13427 end loop;
13428
13429 -- If the declaration is a nested package or concurrent type, then
13430 -- recurse. Nested generic packages will have been processed from the
13431 -- inside out.
13432
13433 case Nkind (Decl) is
13434 when N_Package_Declaration =>
13435 Spec := Specification (Decl);
13436
13437 when N_Task_Type_Declaration =>
13438 Spec := Task_Definition (Decl);
13439
13440 when N_Protected_Type_Declaration =>
13441 Spec := Protected_Definition (Decl);
13442
13443 when others =>
13444 Spec := Empty;
13445 end case;
13446
13447 if Present (Spec) then
13448 Move_Freeze_Nodes (Out_Of, Next_Node, Visible_Declarations (Spec));
13449 Move_Freeze_Nodes (Out_Of, Next_Node, Private_Declarations (Spec));
13450 end if;
13451
13452 Next (Decl);
13453 end loop;
13454 end Move_Freeze_Nodes;
13455
13456 ----------------
13457 -- Next_Assoc --
13458 ----------------
13459
13460 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr is
13461 begin
13462 return Generic_Renamings.Table (E).Next_In_HTable;
13463 end Next_Assoc;
13464
13465 ------------------------
13466 -- Preanalyze_Actuals --
13467 ------------------------
13468
13469 procedure Preanalyze_Actuals (N : Node_Id; Inst : Entity_Id := Empty) is
13470 Assoc : Node_Id;
13471 Act : Node_Id;
13472 Errs : constant Nat := Serious_Errors_Detected;
13473
13474 Cur : Entity_Id := Empty;
13475 -- Current homograph of the instance name
13476
13477 Vis : Boolean;
13478 -- Saved visibility status of the current homograph
13479
13480 begin
13481 Assoc := First (Generic_Associations (N));
13482
13483 -- If the instance is a child unit, its name may hide an outer homonym,
13484 -- so make it invisible to perform name resolution on the actuals.
13485
13486 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name
13487 and then Present
13488 (Current_Entity (Defining_Identifier (Defining_Unit_Name (N))))
13489 then
13490 Cur := Current_Entity (Defining_Identifier (Defining_Unit_Name (N)));
13491
13492 if Is_Compilation_Unit (Cur) then
13493 Vis := Is_Immediately_Visible (Cur);
13494 Set_Is_Immediately_Visible (Cur, False);
13495 else
13496 Cur := Empty;
13497 end if;
13498 end if;
13499
13500 while Present (Assoc) loop
13501 if Nkind (Assoc) /= N_Others_Choice then
13502 Act := Explicit_Generic_Actual_Parameter (Assoc);
13503
13504 -- Within a nested instantiation, a defaulted actual is an empty
13505 -- association, so nothing to analyze. If the subprogram actual
13506 -- is an attribute, analyze prefix only, because actual is not a
13507 -- complete attribute reference.
13508
13509 -- If actual is an allocator, analyze expression only. The full
13510 -- analysis can generate code, and if instance is a compilation
13511 -- unit we have to wait until the package instance is installed
13512 -- to have a proper place to insert this code.
13513
13514 -- String literals may be operators, but at this point we do not
13515 -- know whether the actual is a formal subprogram or a string.
13516
13517 if No (Act) then
13518 null;
13519
13520 elsif Nkind (Act) = N_Attribute_Reference then
13521 Analyze (Prefix (Act));
13522
13523 elsif Nkind (Act) = N_Explicit_Dereference then
13524 Analyze (Prefix (Act));
13525
13526 elsif Nkind (Act) = N_Allocator then
13527 declare
13528 Expr : constant Node_Id := Expression (Act);
13529
13530 begin
13531 if Nkind (Expr) = N_Subtype_Indication then
13532 Analyze (Subtype_Mark (Expr));
13533
13534 -- Analyze separately each discriminant constraint, when
13535 -- given with a named association.
13536
13537 declare
13538 Constr : Node_Id;
13539
13540 begin
13541 Constr := First (Constraints (Constraint (Expr)));
13542 while Present (Constr) loop
13543 if Nkind (Constr) = N_Discriminant_Association then
13544 Analyze (Expression (Constr));
13545 else
13546 Analyze (Constr);
13547 end if;
13548
13549 Next (Constr);
13550 end loop;
13551 end;
13552
13553 else
13554 Analyze (Expr);
13555 end if;
13556 end;
13557
13558 elsif Nkind (Act) /= N_Operator_Symbol then
13559 Analyze (Act);
13560
13561 -- Within a package instance, mark actuals that are limited
13562 -- views, so their use can be moved to the body of the
13563 -- enclosing unit.
13564
13565 if Is_Entity_Name (Act)
13566 and then Is_Type (Entity (Act))
13567 and then From_Limited_With (Entity (Act))
13568 and then Present (Inst)
13569 then
13570 Append_Elmt (Entity (Act), Incomplete_Actuals (Inst));
13571 end if;
13572 end if;
13573
13574 if Errs /= Serious_Errors_Detected then
13575
13576 -- Do a minimal analysis of the generic, to prevent spurious
13577 -- warnings complaining about the generic being unreferenced,
13578 -- before abandoning the instantiation.
13579
13580 Analyze (Name (N));
13581
13582 if Is_Entity_Name (Name (N))
13583 and then Etype (Name (N)) /= Any_Type
13584 then
13585 Generate_Reference (Entity (Name (N)), Name (N));
13586 Set_Is_Instantiated (Entity (Name (N)));
13587 end if;
13588
13589 if Present (Cur) then
13590
13591 -- For the case of a child instance hiding an outer homonym,
13592 -- provide additional warning which might explain the error.
13593
13594 Set_Is_Immediately_Visible (Cur, Vis);
13595 Error_Msg_NE
13596 ("& hides outer unit with the same name??",
13597 N, Defining_Unit_Name (N));
13598 end if;
13599
13600 Abandon_Instantiation (Act);
13601 end if;
13602 end if;
13603
13604 Next (Assoc);
13605 end loop;
13606
13607 if Present (Cur) then
13608 Set_Is_Immediately_Visible (Cur, Vis);
13609 end if;
13610 end Preanalyze_Actuals;
13611
13612 -------------------
13613 -- Remove_Parent --
13614 -------------------
13615
13616 procedure Remove_Parent (In_Body : Boolean := False) is
13617 S : Entity_Id := Current_Scope;
13618 -- S is the scope containing the instantiation just completed. The scope
13619 -- stack contains the parent instances of the instantiation, followed by
13620 -- the original S.
13621
13622 Cur_P : Entity_Id;
13623 E : Entity_Id;
13624 P : Entity_Id;
13625 Hidden : Elmt_Id;
13626
13627 begin
13628 -- After child instantiation is complete, remove from scope stack the
13629 -- extra copy of the current scope, and then remove parent instances.
13630
13631 if not In_Body then
13632 Pop_Scope;
13633
13634 while Current_Scope /= S loop
13635 P := Current_Scope;
13636 End_Package_Scope (Current_Scope);
13637
13638 if In_Open_Scopes (P) then
13639 E := First_Entity (P);
13640 while Present (E) loop
13641 Set_Is_Immediately_Visible (E, True);
13642 Next_Entity (E);
13643 end loop;
13644
13645 -- If instantiation is declared in a block, it is the enclosing
13646 -- scope that might be a parent instance. Note that only one
13647 -- block can be involved, because the parent instances have
13648 -- been installed within it.
13649
13650 if Ekind (P) = E_Block then
13651 Cur_P := Scope (P);
13652 else
13653 Cur_P := P;
13654 end if;
13655
13656 if Is_Generic_Instance (Cur_P) and then P /= Current_Scope then
13657 -- We are within an instance of some sibling. Retain
13658 -- visibility of parent, for proper subsequent cleanup, and
13659 -- reinstall private declarations as well.
13660
13661 Set_In_Private_Part (P);
13662 Install_Private_Declarations (P);
13663 end if;
13664
13665 -- If the ultimate parent is a top-level unit recorded in
13666 -- Instance_Parent_Unit, then reset its visibility to what it was
13667 -- before instantiation. (It's not clear what the purpose is of
13668 -- testing whether Scope (P) is In_Open_Scopes, but that test was
13669 -- present before the ultimate parent test was added.???)
13670
13671 elsif not In_Open_Scopes (Scope (P))
13672 or else (P = Instance_Parent_Unit
13673 and then not Parent_Unit_Visible)
13674 then
13675 Set_Is_Immediately_Visible (P, False);
13676
13677 -- If the current scope is itself an instantiation of a generic
13678 -- nested within P, and we are in the private part of body of this
13679 -- instantiation, restore the full views of P, that were removed
13680 -- in End_Package_Scope above. This obscure case can occur when a
13681 -- subunit of a generic contains an instance of a child unit of
13682 -- its generic parent unit.
13683
13684 elsif S = Current_Scope and then Is_Generic_Instance (S) then
13685 declare
13686 Par : constant Entity_Id :=
13687 Generic_Parent (Package_Specification (S));
13688 begin
13689 if Present (Par)
13690 and then P = Scope (Par)
13691 and then (In_Package_Body (S) or else In_Private_Part (S))
13692 then
13693 Set_In_Private_Part (P);
13694 Install_Private_Declarations (P);
13695 end if;
13696 end;
13697 end if;
13698 end loop;
13699
13700 -- Reset visibility of entities in the enclosing scope
13701
13702 Set_Is_Hidden_Open_Scope (Current_Scope, False);
13703
13704 Hidden := First_Elmt (Hidden_Entities);
13705 while Present (Hidden) loop
13706 Set_Is_Immediately_Visible (Node (Hidden), True);
13707 Next_Elmt (Hidden);
13708 end loop;
13709
13710 else
13711 -- Each body is analyzed separately, and there is no context that
13712 -- needs preserving from one body instance to the next, so remove all
13713 -- parent scopes that have been installed.
13714
13715 while Present (S) loop
13716 End_Package_Scope (S);
13717 Set_Is_Immediately_Visible (S, False);
13718 S := Current_Scope;
13719 exit when S = Standard_Standard;
13720 end loop;
13721 end if;
13722 end Remove_Parent;
13723
13724 -----------------
13725 -- Restore_Env --
13726 -----------------
13727
13728 procedure Restore_Env is
13729 Saved : Instance_Env renames Instance_Envs.Table (Instance_Envs.Last);
13730
13731 begin
13732 if No (Current_Instantiated_Parent.Act_Id) then
13733 -- Restore environment after subprogram inlining
13734
13735 Restore_Private_Views (Empty);
13736 end if;
13737
13738 Current_Instantiated_Parent := Saved.Instantiated_Parent;
13739 Exchanged_Views := Saved.Exchanged_Views;
13740 Hidden_Entities := Saved.Hidden_Entities;
13741 Current_Sem_Unit := Saved.Current_Sem_Unit;
13742 Parent_Unit_Visible := Saved.Parent_Unit_Visible;
13743 Instance_Parent_Unit := Saved.Instance_Parent_Unit;
13744
13745 Restore_Opt_Config_Switches (Saved.Switches);
13746
13747 Instance_Envs.Decrement_Last;
13748 end Restore_Env;
13749
13750 ---------------------------
13751 -- Restore_Private_Views --
13752 ---------------------------
13753
13754 procedure Restore_Private_Views
13755 (Pack_Id : Entity_Id;
13756 Is_Package : Boolean := True)
13757 is
13758 M : Elmt_Id;
13759 E : Entity_Id;
13760 Typ : Entity_Id;
13761 Dep_Elmt : Elmt_Id;
13762 Dep_Typ : Node_Id;
13763
13764 procedure Restore_Nested_Formal (Formal : Entity_Id);
13765 -- Hide the generic formals of formal packages declared with box which
13766 -- were reachable in the current instantiation.
13767
13768 ---------------------------
13769 -- Restore_Nested_Formal --
13770 ---------------------------
13771
13772 procedure Restore_Nested_Formal (Formal : Entity_Id) is
13773 Ent : Entity_Id;
13774
13775 begin
13776 if Present (Renamed_Object (Formal))
13777 and then Denotes_Formal_Package (Renamed_Object (Formal), True)
13778 then
13779 return;
13780
13781 elsif Present (Associated_Formal_Package (Formal)) then
13782 Ent := First_Entity (Formal);
13783 while Present (Ent) loop
13784 exit when Ekind (Ent) = E_Package
13785 and then Renamed_Entity (Ent) = Renamed_Entity (Formal);
13786
13787 Set_Is_Hidden (Ent);
13788 Set_Is_Potentially_Use_Visible (Ent, False);
13789
13790 -- If package, then recurse
13791
13792 if Ekind (Ent) = E_Package then
13793 Restore_Nested_Formal (Ent);
13794 end if;
13795
13796 Next_Entity (Ent);
13797 end loop;
13798 end if;
13799 end Restore_Nested_Formal;
13800
13801 -- Start of processing for Restore_Private_Views
13802
13803 begin
13804 M := First_Elmt (Exchanged_Views);
13805 while Present (M) loop
13806 Typ := Node (M);
13807
13808 -- Subtypes of types whose views have been exchanged, and that are
13809 -- defined within the instance, were not on the Private_Dependents
13810 -- list on entry to the instance, so they have to be exchanged
13811 -- explicitly now, in order to remain consistent with the view of the
13812 -- parent type.
13813
13814 if Ekind_In (Typ, E_Private_Type,
13815 E_Limited_Private_Type,
13816 E_Record_Type_With_Private)
13817 then
13818 Dep_Elmt := First_Elmt (Private_Dependents (Typ));
13819 while Present (Dep_Elmt) loop
13820 Dep_Typ := Node (Dep_Elmt);
13821
13822 if Scope (Dep_Typ) = Pack_Id
13823 and then Present (Full_View (Dep_Typ))
13824 then
13825 Replace_Elmt (Dep_Elmt, Full_View (Dep_Typ));
13826 Exchange_Declarations (Dep_Typ);
13827 end if;
13828
13829 Next_Elmt (Dep_Elmt);
13830 end loop;
13831 end if;
13832
13833 Exchange_Declarations (Node (M));
13834 Next_Elmt (M);
13835 end loop;
13836
13837 if No (Pack_Id) then
13838 return;
13839 end if;
13840
13841 -- Make the generic formal parameters private, and make the formal types
13842 -- into subtypes of the actuals again.
13843
13844 E := First_Entity (Pack_Id);
13845 while Present (E) loop
13846 Set_Is_Hidden (E, True);
13847
13848 if Is_Type (E)
13849 and then Nkind (Parent (E)) = N_Subtype_Declaration
13850 then
13851 -- If the actual for E is itself a generic actual type from
13852 -- an enclosing instance, E is still a generic actual type
13853 -- outside of the current instance. This matter when resolving
13854 -- an overloaded call that may be ambiguous in the enclosing
13855 -- instance, when two of its actuals coincide.
13856
13857 if Is_Entity_Name (Subtype_Indication (Parent (E)))
13858 and then Is_Generic_Actual_Type
13859 (Entity (Subtype_Indication (Parent (E))))
13860 then
13861 null;
13862 else
13863 Set_Is_Generic_Actual_Type (E, False);
13864 end if;
13865
13866 -- An unusual case of aliasing: the actual may also be directly
13867 -- visible in the generic, and be private there, while it is fully
13868 -- visible in the context of the instance. The internal subtype
13869 -- is private in the instance but has full visibility like its
13870 -- parent in the enclosing scope. This enforces the invariant that
13871 -- the privacy status of all private dependents of a type coincide
13872 -- with that of the parent type. This can only happen when a
13873 -- generic child unit is instantiated within a sibling.
13874
13875 if Is_Private_Type (E)
13876 and then not Is_Private_Type (Etype (E))
13877 then
13878 Exchange_Declarations (E);
13879 end if;
13880
13881 elsif Ekind (E) = E_Package then
13882
13883 -- The end of the renaming list is the renaming of the generic
13884 -- package itself. If the instance is a subprogram, all entities
13885 -- in the corresponding package are renamings. If this entity is
13886 -- a formal package, make its own formals private as well. The
13887 -- actual in this case is itself the renaming of an instantiation.
13888 -- If the entity is not a package renaming, it is the entity
13889 -- created to validate formal package actuals: ignore it.
13890
13891 -- If the actual is itself a formal package for the enclosing
13892 -- generic, or the actual for such a formal package, it remains
13893 -- visible on exit from the instance, and therefore nothing needs
13894 -- to be done either, except to keep it accessible.
13895
13896 if Is_Package and then Renamed_Object (E) = Pack_Id then
13897 exit;
13898
13899 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
13900 null;
13901
13902 elsif
13903 Denotes_Formal_Package (Renamed_Object (E), True, Pack_Id)
13904 then
13905 Set_Is_Hidden (E, False);
13906
13907 else
13908 declare
13909 Act_P : constant Entity_Id := Renamed_Object (E);
13910 Id : Entity_Id;
13911
13912 begin
13913 Id := First_Entity (Act_P);
13914 while Present (Id)
13915 and then Id /= First_Private_Entity (Act_P)
13916 loop
13917 exit when Ekind (Id) = E_Package
13918 and then Renamed_Object (Id) = Act_P;
13919
13920 Set_Is_Hidden (Id, True);
13921 Set_Is_Potentially_Use_Visible (Id, In_Use (Act_P));
13922
13923 if Ekind (Id) = E_Package then
13924 Restore_Nested_Formal (Id);
13925 end if;
13926
13927 Next_Entity (Id);
13928 end loop;
13929 end;
13930 end if;
13931 end if;
13932
13933 Next_Entity (E);
13934 end loop;
13935 end Restore_Private_Views;
13936
13937 --------------
13938 -- Save_Env --
13939 --------------
13940
13941 procedure Save_Env
13942 (Gen_Unit : Entity_Id;
13943 Act_Unit : Entity_Id)
13944 is
13945 begin
13946 Init_Env;
13947 Set_Instance_Env (Gen_Unit, Act_Unit);
13948 end Save_Env;
13949
13950 ----------------------------
13951 -- Save_Global_References --
13952 ----------------------------
13953
13954 procedure Save_Global_References (Templ : Node_Id) is
13955
13956 -- ??? it is horrible to use global variables in highly recursive code
13957
13958 E : Entity_Id;
13959 -- The entity of the current associated node
13960
13961 Gen_Scope : Entity_Id;
13962 -- The scope of the generic for which references are being saved
13963
13964 N2 : Node_Id;
13965 -- The current associated node
13966
13967 function Is_Global (E : Entity_Id) return Boolean;
13968 -- Check whether entity is defined outside of generic unit. Examine the
13969 -- scope of an entity, and the scope of the scope, etc, until we find
13970 -- either Standard, in which case the entity is global, or the generic
13971 -- unit itself, which indicates that the entity is local. If the entity
13972 -- is the generic unit itself, as in the case of a recursive call, or
13973 -- the enclosing generic unit, if different from the current scope, then
13974 -- it is local as well, because it will be replaced at the point of
13975 -- instantiation. On the other hand, if it is a reference to a child
13976 -- unit of a common ancestor, which appears in an instantiation, it is
13977 -- global because it is used to denote a specific compilation unit at
13978 -- the time the instantiations will be analyzed.
13979
13980 procedure Qualify_Universal_Operands
13981 (Op : Node_Id;
13982 Func_Call : Node_Id);
13983 -- Op denotes a binary or unary operator in generic template Templ. Node
13984 -- Func_Call is the function call alternative of the operator within the
13985 -- the analyzed copy of the template. Change each operand which yields a
13986 -- universal type by wrapping it into a qualified expression
13987 --
13988 -- Actual_Typ'(Operand)
13989 --
13990 -- where Actual_Typ is the type of corresponding actual parameter of
13991 -- Operand in Func_Call.
13992
13993 procedure Reset_Entity (N : Node_Id);
13994 -- Save semantic information on global entity so that it is not resolved
13995 -- again at instantiation time.
13996
13997 procedure Save_Entity_Descendants (N : Node_Id);
13998 -- Apply Save_Global_References to the two syntactic descendants of
13999 -- non-terminal nodes that carry an Associated_Node and are processed
14000 -- through Reset_Entity. Once the global entity (if any) has been
14001 -- captured together with its type, only two syntactic descendants need
14002 -- to be traversed to complete the processing of the tree rooted at N.
14003 -- This applies to Selected_Components, Expanded_Names, and to Operator
14004 -- nodes. N can also be a character literal, identifier, or operator
14005 -- symbol node, but the call has no effect in these cases.
14006
14007 procedure Save_Global_Defaults (N1 : Node_Id; N2 : Node_Id);
14008 -- Default actuals in nested instances must be handled specially
14009 -- because there is no link to them from the original tree. When an
14010 -- actual subprogram is given by a default, we add an explicit generic
14011 -- association for it in the instantiation node. When we save the
14012 -- global references on the name of the instance, we recover the list
14013 -- of generic associations, and add an explicit one to the original
14014 -- generic tree, through which a global actual can be preserved.
14015 -- Similarly, if a child unit is instantiated within a sibling, in the
14016 -- context of the parent, we must preserve the identifier of the parent
14017 -- so that it can be properly resolved in a subsequent instantiation.
14018
14019 procedure Save_Global_Descendant (D : Union_Id);
14020 -- Apply Save_References recursively to the descendants of node D
14021
14022 procedure Save_References (N : Node_Id);
14023 -- This is the recursive procedure that does the work, once the
14024 -- enclosing generic scope has been established.
14025
14026 ---------------
14027 -- Is_Global --
14028 ---------------
14029
14030 function Is_Global (E : Entity_Id) return Boolean is
14031 Se : Entity_Id;
14032
14033 function Is_Instance_Node (Decl : Node_Id) return Boolean;
14034 -- Determine whether the parent node of a reference to a child unit
14035 -- denotes an instantiation or a formal package, in which case the
14036 -- reference to the child unit is global, even if it appears within
14037 -- the current scope (e.g. when the instance appears within the body
14038 -- of an ancestor).
14039
14040 ----------------------
14041 -- Is_Instance_Node --
14042 ----------------------
14043
14044 function Is_Instance_Node (Decl : Node_Id) return Boolean is
14045 begin
14046 return Nkind (Decl) in N_Generic_Instantiation
14047 or else
14048 Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration;
14049 end Is_Instance_Node;
14050
14051 -- Start of processing for Is_Global
14052
14053 begin
14054 if E = Gen_Scope then
14055 return False;
14056
14057 elsif E = Standard_Standard then
14058 return True;
14059
14060 elsif Is_Child_Unit (E)
14061 and then (Is_Instance_Node (Parent (N2))
14062 or else (Nkind (Parent (N2)) = N_Expanded_Name
14063 and then N2 = Selector_Name (Parent (N2))
14064 and then
14065 Is_Instance_Node (Parent (Parent (N2)))))
14066 then
14067 return True;
14068
14069 else
14070 Se := Scope (E);
14071 while Se /= Gen_Scope loop
14072 if Se = Standard_Standard then
14073 return True;
14074 else
14075 Se := Scope (Se);
14076 end if;
14077 end loop;
14078
14079 return False;
14080 end if;
14081 end Is_Global;
14082
14083 --------------------------------
14084 -- Qualify_Universal_Operands --
14085 --------------------------------
14086
14087 procedure Qualify_Universal_Operands
14088 (Op : Node_Id;
14089 Func_Call : Node_Id)
14090 is
14091 procedure Qualify_Operand (Opnd : Node_Id; Actual : Node_Id);
14092 -- Rewrite operand Opnd as a qualified expression of the form
14093 --
14094 -- Actual_Typ'(Opnd)
14095 --
14096 -- where Actual is the corresponding actual parameter of Opnd in
14097 -- function call Func_Call.
14098
14099 function Qualify_Type
14100 (Loc : Source_Ptr;
14101 Typ : Entity_Id) return Node_Id;
14102 -- Qualify type Typ by creating a selected component of the form
14103 --
14104 -- Scope_Of_Typ.Typ
14105
14106 ---------------------
14107 -- Qualify_Operand --
14108 ---------------------
14109
14110 procedure Qualify_Operand (Opnd : Node_Id; Actual : Node_Id) is
14111 Loc : constant Source_Ptr := Sloc (Opnd);
14112 Typ : constant Entity_Id := Etype (Actual);
14113 Mark : Node_Id;
14114 Qual : Node_Id;
14115
14116 begin
14117 -- Qualify the operand when it is of a universal type. Note that
14118 -- the template is unanalyzed and it is not possible to directly
14119 -- query the type. This transformation is not done when the type
14120 -- of the actual is internally generated because the type will be
14121 -- regenerated in the instance.
14122
14123 if Yields_Universal_Type (Opnd)
14124 and then Comes_From_Source (Typ)
14125 and then not Is_Hidden (Typ)
14126 then
14127 -- The type of the actual may be a global reference. Save this
14128 -- information by creating a reference to it.
14129
14130 if Is_Global (Typ) then
14131 Mark := New_Occurrence_Of (Typ, Loc);
14132
14133 -- Otherwise rely on resolution to find the proper type within
14134 -- the instance.
14135
14136 else
14137 Mark := Qualify_Type (Loc, Typ);
14138 end if;
14139
14140 Qual :=
14141 Make_Qualified_Expression (Loc,
14142 Subtype_Mark => Mark,
14143 Expression => Relocate_Node (Opnd));
14144
14145 -- Mark the qualification to distinguish it from other source
14146 -- constructs and signal the instantiation mechanism that this
14147 -- node requires special processing. See Copy_Generic_Node for
14148 -- details.
14149
14150 Set_Is_Qualified_Universal_Literal (Qual);
14151
14152 Rewrite (Opnd, Qual);
14153 end if;
14154 end Qualify_Operand;
14155
14156 ------------------
14157 -- Qualify_Type --
14158 ------------------
14159
14160 function Qualify_Type
14161 (Loc : Source_Ptr;
14162 Typ : Entity_Id) return Node_Id
14163 is
14164 Scop : constant Entity_Id := Scope (Typ);
14165 Result : Node_Id;
14166
14167 begin
14168 Result := Make_Identifier (Loc, Chars (Typ));
14169
14170 if Present (Scop) and then not Is_Generic_Unit (Scop) then
14171 Result :=
14172 Make_Selected_Component (Loc,
14173 Prefix => Make_Identifier (Loc, Chars (Scop)),
14174 Selector_Name => Result);
14175 end if;
14176
14177 return Result;
14178 end Qualify_Type;
14179
14180 -- Local variables
14181
14182 Actuals : constant List_Id := Parameter_Associations (Func_Call);
14183
14184 -- Start of processing for Qualify_Universal_Operands
14185
14186 begin
14187 if Nkind (Op) in N_Binary_Op then
14188 Qualify_Operand (Left_Opnd (Op), First (Actuals));
14189 Qualify_Operand (Right_Opnd (Op), Next (First (Actuals)));
14190
14191 elsif Nkind (Op) in N_Unary_Op then
14192 Qualify_Operand (Right_Opnd (Op), First (Actuals));
14193 end if;
14194 end Qualify_Universal_Operands;
14195
14196 ------------------
14197 -- Reset_Entity --
14198 ------------------
14199
14200 procedure Reset_Entity (N : Node_Id) is
14201 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id);
14202 -- If the type of N2 is global to the generic unit, save the type in
14203 -- the generic node. Just as we perform name capture for explicit
14204 -- references within the generic, we must capture the global types
14205 -- of local entities because they may participate in resolution in
14206 -- the instance.
14207
14208 function Top_Ancestor (E : Entity_Id) return Entity_Id;
14209 -- Find the ultimate ancestor of the current unit. If it is not a
14210 -- generic unit, then the name of the current unit in the prefix of
14211 -- an expanded name must be replaced with its generic homonym to
14212 -- ensure that it will be properly resolved in an instance.
14213
14214 ---------------------
14215 -- Set_Global_Type --
14216 ---------------------
14217
14218 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id) is
14219 Typ : constant Entity_Id := Etype (N2);
14220
14221 begin
14222 Set_Etype (N, Typ);
14223
14224 -- If the entity of N is not the associated node, this is a
14225 -- nested generic and it has an associated node as well, whose
14226 -- type is already the full view (see below). Indicate that the
14227 -- original node has a private view.
14228
14229 if Entity (N) /= N2 and then Has_Private_View (Entity (N)) then
14230 Set_Has_Private_View (N);
14231 end if;
14232
14233 -- If not a private type, nothing else to do
14234
14235 if not Is_Private_Type (Typ) then
14236 if Is_Array_Type (Typ)
14237 and then Is_Private_Type (Component_Type (Typ))
14238 then
14239 Set_Has_Private_View (N);
14240 end if;
14241
14242 -- If it is a derivation of a private type in a context where no
14243 -- full view is needed, nothing to do either.
14244
14245 elsif No (Full_View (Typ)) and then Typ /= Etype (Typ) then
14246 null;
14247
14248 -- Otherwise mark the type for flipping and use the full view when
14249 -- available.
14250
14251 else
14252 Set_Has_Private_View (N);
14253
14254 if Present (Full_View (Typ)) then
14255 Set_Etype (N2, Full_View (Typ));
14256 end if;
14257 end if;
14258
14259 if Is_Floating_Point_Type (Typ)
14260 and then Has_Dimension_System (Typ)
14261 then
14262 Copy_Dimensions (N2, N);
14263 end if;
14264
14265 end Set_Global_Type;
14266
14267 ------------------
14268 -- Top_Ancestor --
14269 ------------------
14270
14271 function Top_Ancestor (E : Entity_Id) return Entity_Id is
14272 Par : Entity_Id;
14273
14274 begin
14275 Par := E;
14276 while Is_Child_Unit (Par) loop
14277 Par := Scope (Par);
14278 end loop;
14279
14280 return Par;
14281 end Top_Ancestor;
14282
14283 -- Start of processing for Reset_Entity
14284
14285 begin
14286 N2 := Get_Associated_Node (N);
14287 E := Entity (N2);
14288
14289 if Present (E) then
14290
14291 -- If the node is an entry call to an entry in an enclosing task,
14292 -- it is rewritten as a selected component. No global entity to
14293 -- preserve in this case, since the expansion will be redone in
14294 -- the instance.
14295
14296 if not Nkind_In (E, N_Defining_Character_Literal,
14297 N_Defining_Identifier,
14298 N_Defining_Operator_Symbol)
14299 then
14300 Set_Associated_Node (N, Empty);
14301 Set_Etype (N, Empty);
14302 return;
14303 end if;
14304
14305 -- If the entity is an itype created as a subtype of an access
14306 -- type with a null exclusion restore source entity for proper
14307 -- visibility. The itype will be created anew in the instance.
14308
14309 if Is_Itype (E)
14310 and then Ekind (E) = E_Access_Subtype
14311 and then Is_Entity_Name (N)
14312 and then Chars (Etype (E)) = Chars (N)
14313 then
14314 E := Etype (E);
14315 Set_Entity (N2, E);
14316 Set_Etype (N2, E);
14317 end if;
14318
14319 if Is_Global (E) then
14320
14321 -- If the entity is a package renaming that is the prefix of
14322 -- an expanded name, it has been rewritten as the renamed
14323 -- package, which is necessary semantically but complicates
14324 -- ASIS tree traversal, so we recover the original entity to
14325 -- expose the renaming. Take into account that the context may
14326 -- be a nested generic, that the original node may itself have
14327 -- an associated node that had better be an entity, and that
14328 -- the current node is still a selected component.
14329
14330 if Ekind (E) = E_Package
14331 and then Nkind (N) = N_Selected_Component
14332 and then Nkind (Parent (N)) = N_Expanded_Name
14333 and then Present (Original_Node (N2))
14334 and then Is_Entity_Name (Original_Node (N2))
14335 and then Present (Entity (Original_Node (N2)))
14336 then
14337 if Is_Global (Entity (Original_Node (N2))) then
14338 N2 := Original_Node (N2);
14339 Set_Associated_Node (N, N2);
14340 Set_Global_Type (N, N2);
14341
14342 -- Renaming is local, and will be resolved in instance
14343
14344 else
14345 Set_Associated_Node (N, Empty);
14346 Set_Etype (N, Empty);
14347 end if;
14348
14349 else
14350 Set_Global_Type (N, N2);
14351 end if;
14352
14353 elsif Nkind (N) = N_Op_Concat
14354 and then Is_Generic_Type (Etype (N2))
14355 and then (Base_Type (Etype (Right_Opnd (N2))) = Etype (N2)
14356 or else
14357 Base_Type (Etype (Left_Opnd (N2))) = Etype (N2))
14358 and then Is_Intrinsic_Subprogram (E)
14359 then
14360 null;
14361
14362 -- Entity is local. Mark generic node as unresolved. Note that now
14363 -- it does not have an entity.
14364
14365 else
14366 Set_Associated_Node (N, Empty);
14367 Set_Etype (N, Empty);
14368 end if;
14369
14370 if Nkind (Parent (N)) in N_Generic_Instantiation
14371 and then N = Name (Parent (N))
14372 then
14373 Save_Global_Defaults (Parent (N), Parent (N2));
14374 end if;
14375
14376 elsif Nkind (Parent (N)) = N_Selected_Component
14377 and then Nkind (Parent (N2)) = N_Expanded_Name
14378 then
14379 if Is_Global (Entity (Parent (N2))) then
14380 Change_Selected_Component_To_Expanded_Name (Parent (N));
14381 Set_Associated_Node (Parent (N), Parent (N2));
14382 Set_Global_Type (Parent (N), Parent (N2));
14383 Save_Entity_Descendants (N);
14384
14385 -- If this is a reference to the current generic entity, replace
14386 -- by the name of the generic homonym of the current package. This
14387 -- is because in an instantiation Par.P.Q will not resolve to the
14388 -- name of the instance, whose enclosing scope is not necessarily
14389 -- Par. We use the generic homonym rather that the name of the
14390 -- generic itself because it may be hidden by a local declaration.
14391
14392 elsif In_Open_Scopes (Entity (Parent (N2)))
14393 and then not
14394 Is_Generic_Unit (Top_Ancestor (Entity (Prefix (Parent (N2)))))
14395 then
14396 if Ekind (Entity (Parent (N2))) = E_Generic_Package then
14397 Rewrite (Parent (N),
14398 Make_Identifier (Sloc (N),
14399 Chars =>
14400 Chars (Generic_Homonym (Entity (Parent (N2))))));
14401 else
14402 Rewrite (Parent (N),
14403 Make_Identifier (Sloc (N),
14404 Chars => Chars (Selector_Name (Parent (N2)))));
14405 end if;
14406 end if;
14407
14408 if Nkind (Parent (Parent (N))) in N_Generic_Instantiation
14409 and then Parent (N) = Name (Parent (Parent (N)))
14410 then
14411 Save_Global_Defaults
14412 (Parent (Parent (N)), Parent (Parent (N2)));
14413 end if;
14414
14415 -- A selected component may denote a static constant that has been
14416 -- folded. If the static constant is global to the generic, capture
14417 -- its value. Otherwise the folding will happen in any instantiation.
14418
14419 elsif Nkind (Parent (N)) = N_Selected_Component
14420 and then Nkind_In (Parent (N2), N_Integer_Literal, N_Real_Literal)
14421 then
14422 if Present (Entity (Original_Node (Parent (N2))))
14423 and then Is_Global (Entity (Original_Node (Parent (N2))))
14424 then
14425 Rewrite (Parent (N), New_Copy (Parent (N2)));
14426 Set_Analyzed (Parent (N), False);
14427 end if;
14428
14429 -- A selected component may be transformed into a parameterless
14430 -- function call. If the called entity is global, rewrite the node
14431 -- appropriately, i.e. as an extended name for the global entity.
14432
14433 elsif Nkind (Parent (N)) = N_Selected_Component
14434 and then Nkind (Parent (N2)) = N_Function_Call
14435 and then N = Selector_Name (Parent (N))
14436 then
14437 if No (Parameter_Associations (Parent (N2))) then
14438 if Is_Global (Entity (Name (Parent (N2)))) then
14439 Change_Selected_Component_To_Expanded_Name (Parent (N));
14440 Set_Associated_Node (Parent (N), Name (Parent (N2)));
14441 Set_Global_Type (Parent (N), Name (Parent (N2)));
14442 Save_Entity_Descendants (N);
14443
14444 else
14445 Set_Is_Prefixed_Call (Parent (N));
14446 Set_Associated_Node (N, Empty);
14447 Set_Etype (N, Empty);
14448 end if;
14449
14450 -- In Ada 2005, X.F may be a call to a primitive operation,
14451 -- rewritten as F (X). This rewriting will be done again in an
14452 -- instance, so keep the original node. Global entities will be
14453 -- captured as for other constructs. Indicate that this must
14454 -- resolve as a call, to prevent accidental overloading in the
14455 -- instance, if both a component and a primitive operation appear
14456 -- as candidates.
14457
14458 else
14459 Set_Is_Prefixed_Call (Parent (N));
14460 end if;
14461
14462 -- Entity is local. Reset in generic unit, so that node is resolved
14463 -- anew at the point of instantiation.
14464
14465 else
14466 Set_Associated_Node (N, Empty);
14467 Set_Etype (N, Empty);
14468 end if;
14469 end Reset_Entity;
14470
14471 -----------------------------
14472 -- Save_Entity_Descendants --
14473 -----------------------------
14474
14475 procedure Save_Entity_Descendants (N : Node_Id) is
14476 begin
14477 case Nkind (N) is
14478 when N_Binary_Op =>
14479 Save_Global_Descendant (Union_Id (Left_Opnd (N)));
14480 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
14481
14482 when N_Unary_Op =>
14483 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
14484
14485 when N_Expanded_Name |
14486 N_Selected_Component =>
14487 Save_Global_Descendant (Union_Id (Prefix (N)));
14488 Save_Global_Descendant (Union_Id (Selector_Name (N)));
14489
14490 when N_Identifier |
14491 N_Character_Literal |
14492 N_Operator_Symbol =>
14493 null;
14494
14495 when others =>
14496 raise Program_Error;
14497 end case;
14498 end Save_Entity_Descendants;
14499
14500 --------------------------
14501 -- Save_Global_Defaults --
14502 --------------------------
14503
14504 procedure Save_Global_Defaults (N1 : Node_Id; N2 : Node_Id) is
14505 Loc : constant Source_Ptr := Sloc (N1);
14506 Assoc2 : constant List_Id := Generic_Associations (N2);
14507 Gen_Id : constant Entity_Id := Get_Generic_Entity (N2);
14508 Assoc1 : List_Id;
14509 Act1 : Node_Id;
14510 Act2 : Node_Id;
14511 Def : Node_Id;
14512 Ndec : Node_Id;
14513 Subp : Entity_Id;
14514 Actual : Entity_Id;
14515
14516 begin
14517 Assoc1 := Generic_Associations (N1);
14518
14519 if Present (Assoc1) then
14520 Act1 := First (Assoc1);
14521 else
14522 Act1 := Empty;
14523 Set_Generic_Associations (N1, New_List);
14524 Assoc1 := Generic_Associations (N1);
14525 end if;
14526
14527 if Present (Assoc2) then
14528 Act2 := First (Assoc2);
14529 else
14530 return;
14531 end if;
14532
14533 while Present (Act1) and then Present (Act2) loop
14534 Next (Act1);
14535 Next (Act2);
14536 end loop;
14537
14538 -- Find the associations added for default subprograms
14539
14540 if Present (Act2) then
14541 while Nkind (Act2) /= N_Generic_Association
14542 or else No (Entity (Selector_Name (Act2)))
14543 or else not Is_Overloadable (Entity (Selector_Name (Act2)))
14544 loop
14545 Next (Act2);
14546 end loop;
14547
14548 -- Add a similar association if the default is global. The
14549 -- renaming declaration for the actual has been analyzed, and
14550 -- its alias is the program it renames. Link the actual in the
14551 -- original generic tree with the node in the analyzed tree.
14552
14553 while Present (Act2) loop
14554 Subp := Entity (Selector_Name (Act2));
14555 Def := Explicit_Generic_Actual_Parameter (Act2);
14556
14557 -- Following test is defence against rubbish errors
14558
14559 if No (Alias (Subp)) then
14560 return;
14561 end if;
14562
14563 -- Retrieve the resolved actual from the renaming declaration
14564 -- created for the instantiated formal.
14565
14566 Actual := Entity (Name (Parent (Parent (Subp))));
14567 Set_Entity (Def, Actual);
14568 Set_Etype (Def, Etype (Actual));
14569
14570 if Is_Global (Actual) then
14571 Ndec :=
14572 Make_Generic_Association (Loc,
14573 Selector_Name =>
14574 New_Occurrence_Of (Subp, Loc),
14575 Explicit_Generic_Actual_Parameter =>
14576 New_Occurrence_Of (Actual, Loc));
14577
14578 Set_Associated_Node
14579 (Explicit_Generic_Actual_Parameter (Ndec), Def);
14580
14581 Append (Ndec, Assoc1);
14582
14583 -- If there are other defaults, add a dummy association in case
14584 -- there are other defaulted formals with the same name.
14585
14586 elsif Present (Next (Act2)) then
14587 Ndec :=
14588 Make_Generic_Association (Loc,
14589 Selector_Name =>
14590 New_Occurrence_Of (Subp, Loc),
14591 Explicit_Generic_Actual_Parameter => Empty);
14592
14593 Append (Ndec, Assoc1);
14594 end if;
14595
14596 Next (Act2);
14597 end loop;
14598 end if;
14599
14600 if Nkind (Name (N1)) = N_Identifier
14601 and then Is_Child_Unit (Gen_Id)
14602 and then Is_Global (Gen_Id)
14603 and then Is_Generic_Unit (Scope (Gen_Id))
14604 and then In_Open_Scopes (Scope (Gen_Id))
14605 then
14606 -- This is an instantiation of a child unit within a sibling, so
14607 -- that the generic parent is in scope. An eventual instance must
14608 -- occur within the scope of an instance of the parent. Make name
14609 -- in instance into an expanded name, to preserve the identifier
14610 -- of the parent, so it can be resolved subsequently.
14611
14612 Rewrite (Name (N2),
14613 Make_Expanded_Name (Loc,
14614 Chars => Chars (Gen_Id),
14615 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
14616 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
14617 Set_Entity (Name (N2), Gen_Id);
14618
14619 Rewrite (Name (N1),
14620 Make_Expanded_Name (Loc,
14621 Chars => Chars (Gen_Id),
14622 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
14623 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
14624
14625 Set_Associated_Node (Name (N1), Name (N2));
14626 Set_Associated_Node (Prefix (Name (N1)), Empty);
14627 Set_Associated_Node
14628 (Selector_Name (Name (N1)), Selector_Name (Name (N2)));
14629 Set_Etype (Name (N1), Etype (Gen_Id));
14630 end if;
14631 end Save_Global_Defaults;
14632
14633 ----------------------------
14634 -- Save_Global_Descendant --
14635 ----------------------------
14636
14637 procedure Save_Global_Descendant (D : Union_Id) is
14638 N1 : Node_Id;
14639
14640 begin
14641 if D in Node_Range then
14642 if D = Union_Id (Empty) then
14643 null;
14644
14645 elsif Nkind (Node_Id (D)) /= N_Compilation_Unit then
14646 Save_References (Node_Id (D));
14647 end if;
14648
14649 elsif D in List_Range then
14650 pragma Assert (D /= Union_Id (No_List));
14651 -- Because No_List = Empty, which is in Node_Range above
14652
14653 if Is_Empty_List (List_Id (D)) then
14654 null;
14655
14656 else
14657 N1 := First (List_Id (D));
14658 while Present (N1) loop
14659 Save_References (N1);
14660 Next (N1);
14661 end loop;
14662 end if;
14663
14664 -- Element list or other non-node field, nothing to do
14665
14666 else
14667 null;
14668 end if;
14669 end Save_Global_Descendant;
14670
14671 ---------------------
14672 -- Save_References --
14673 ---------------------
14674
14675 -- This is the recursive procedure that does the work once the enclosing
14676 -- generic scope has been established. We have to treat specially a
14677 -- number of node rewritings that are required by semantic processing
14678 -- and which change the kind of nodes in the generic copy: typically
14679 -- constant-folding, replacing an operator node by a string literal, or
14680 -- a selected component by an expanded name. In each of those cases, the
14681 -- transformation is propagated to the generic unit.
14682
14683 procedure Save_References (N : Node_Id) is
14684 Loc : constant Source_Ptr := Sloc (N);
14685
14686 function Requires_Delayed_Save (Nod : Node_Id) return Boolean;
14687 -- Determine whether arbitrary node Nod requires delayed capture of
14688 -- global references within its aspect specifications.
14689
14690 procedure Save_References_In_Aggregate (N : Node_Id);
14691 -- Save all global references in [extension] aggregate node N
14692
14693 procedure Save_References_In_Char_Lit_Or_Op_Symbol (N : Node_Id);
14694 -- Save all global references in a character literal or operator
14695 -- symbol denoted by N.
14696
14697 procedure Save_References_In_Descendants (N : Node_Id);
14698 -- Save all global references in all descendants of node N
14699
14700 procedure Save_References_In_Identifier (N : Node_Id);
14701 -- Save all global references in identifier node N
14702
14703 procedure Save_References_In_Operator (N : Node_Id);
14704 -- Save all global references in operator node N
14705
14706 procedure Save_References_In_Pragma (Prag : Node_Id);
14707 -- Save all global references found within the expression of pragma
14708 -- Prag.
14709
14710 ---------------------------
14711 -- Requires_Delayed_Save --
14712 ---------------------------
14713
14714 function Requires_Delayed_Save (Nod : Node_Id) return Boolean is
14715 begin
14716 -- Generic packages and subprograms require delayed capture of
14717 -- global references within their aspects due to the timing of
14718 -- annotation analysis.
14719
14720 if Nkind_In (Nod, N_Generic_Package_Declaration,
14721 N_Generic_Subprogram_Declaration,
14722 N_Package_Body,
14723 N_Package_Body_Stub,
14724 N_Subprogram_Body,
14725 N_Subprogram_Body_Stub)
14726 then
14727 -- Since the capture of global references is done on the
14728 -- unanalyzed generic template, there is no information around
14729 -- to infer the context. Use the Associated_Entity linkages to
14730 -- peek into the analyzed generic copy and determine what the
14731 -- template corresponds to.
14732
14733 if Nod = Templ then
14734 return
14735 Is_Generic_Declaration_Or_Body
14736 (Unit_Declaration_Node
14737 (Associated_Entity (Defining_Entity (Nod))));
14738
14739 -- Otherwise the generic unit being processed is not the top
14740 -- level template. It is safe to capture of global references
14741 -- within the generic unit because at this point the top level
14742 -- copy is fully analyzed.
14743
14744 else
14745 return False;
14746 end if;
14747
14748 -- Otherwise capture the global references without interference
14749
14750 else
14751 return False;
14752 end if;
14753 end Requires_Delayed_Save;
14754
14755 ----------------------------------
14756 -- Save_References_In_Aggregate --
14757 ----------------------------------
14758
14759 procedure Save_References_In_Aggregate (N : Node_Id) is
14760 Nam : Node_Id;
14761 Qual : Node_Id := Empty;
14762 Typ : Entity_Id := Empty;
14763
14764 use Atree.Unchecked_Access;
14765 -- This code section is part of implementing an untyped tree
14766 -- traversal, so it needs direct access to node fields.
14767
14768 begin
14769 N2 := Get_Associated_Node (N);
14770
14771 if Present (N2) then
14772 Typ := Etype (N2);
14773
14774 -- In an instance within a generic, use the name of the actual
14775 -- and not the original generic parameter. If the actual is
14776 -- global in the current generic it must be preserved for its
14777 -- instantiation.
14778
14779 if Nkind (Parent (Typ)) = N_Subtype_Declaration
14780 and then Present (Generic_Parent_Type (Parent (Typ)))
14781 then
14782 Typ := Base_Type (Typ);
14783 Set_Etype (N2, Typ);
14784 end if;
14785 end if;
14786
14787 if No (N2) or else No (Typ) or else not Is_Global (Typ) then
14788 Set_Associated_Node (N, Empty);
14789
14790 -- If the aggregate is an actual in a call, it has been
14791 -- resolved in the current context, to some local type. The
14792 -- enclosing call may have been disambiguated by the aggregate,
14793 -- and this disambiguation might fail at instantiation time
14794 -- because the type to which the aggregate did resolve is not
14795 -- preserved. In order to preserve some of this information,
14796 -- wrap the aggregate in a qualified expression, using the id
14797 -- of its type. For further disambiguation we qualify the type
14798 -- name with its scope (if visible) because both id's will have
14799 -- corresponding entities in an instance. This resolves most of
14800 -- the problems with missing type information on aggregates in
14801 -- instances.
14802
14803 if Present (N2)
14804 and then Nkind (N2) = Nkind (N)
14805 and then Nkind (Parent (N2)) in N_Subprogram_Call
14806 and then Present (Typ)
14807 and then Comes_From_Source (Typ)
14808 then
14809 Nam := Make_Identifier (Loc, Chars (Typ));
14810
14811 if Is_Immediately_Visible (Scope (Typ)) then
14812 Nam :=
14813 Make_Selected_Component (Loc,
14814 Prefix =>
14815 Make_Identifier (Loc, Chars (Scope (Typ))),
14816 Selector_Name => Nam);
14817 end if;
14818
14819 Qual :=
14820 Make_Qualified_Expression (Loc,
14821 Subtype_Mark => Nam,
14822 Expression => Relocate_Node (N));
14823 end if;
14824 end if;
14825
14826 Save_Global_Descendant (Field1 (N));
14827 Save_Global_Descendant (Field2 (N));
14828 Save_Global_Descendant (Field3 (N));
14829 Save_Global_Descendant (Field5 (N));
14830
14831 if Present (Qual) then
14832 Rewrite (N, Qual);
14833 end if;
14834 end Save_References_In_Aggregate;
14835
14836 ----------------------------------------------
14837 -- Save_References_In_Char_Lit_Or_Op_Symbol --
14838 ----------------------------------------------
14839
14840 procedure Save_References_In_Char_Lit_Or_Op_Symbol (N : Node_Id) is
14841 begin
14842 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
14843 Reset_Entity (N);
14844
14845 elsif Nkind (N) = N_Operator_Symbol
14846 and then Nkind (Get_Associated_Node (N)) = N_String_Literal
14847 then
14848 Change_Operator_Symbol_To_String_Literal (N);
14849 end if;
14850 end Save_References_In_Char_Lit_Or_Op_Symbol;
14851
14852 ------------------------------------
14853 -- Save_References_In_Descendants --
14854 ------------------------------------
14855
14856 procedure Save_References_In_Descendants (N : Node_Id) is
14857 use Atree.Unchecked_Access;
14858 -- This code section is part of implementing an untyped tree
14859 -- traversal, so it needs direct access to node fields.
14860
14861 begin
14862 Save_Global_Descendant (Field1 (N));
14863 Save_Global_Descendant (Field2 (N));
14864 Save_Global_Descendant (Field3 (N));
14865 Save_Global_Descendant (Field4 (N));
14866 Save_Global_Descendant (Field5 (N));
14867 end Save_References_In_Descendants;
14868
14869 -----------------------------------
14870 -- Save_References_In_Identifier --
14871 -----------------------------------
14872
14873 procedure Save_References_In_Identifier (N : Node_Id) is
14874 begin
14875 -- The node did not undergo a transformation
14876
14877 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
14878 declare
14879 Aux_N2 : constant Node_Id := Get_Associated_Node (N);
14880 Orig_N2_Parent : constant Node_Id :=
14881 Original_Node (Parent (Aux_N2));
14882 begin
14883 -- The parent of this identifier is a selected component
14884 -- which denotes a named number that was constant folded.
14885 -- Preserve the original name for ASIS and link the parent
14886 -- with its expanded name. The constant folding will be
14887 -- repeated in the instance.
14888
14889 if Nkind (Parent (N)) = N_Selected_Component
14890 and then Nkind_In (Parent (Aux_N2), N_Integer_Literal,
14891 N_Real_Literal)
14892 and then Is_Entity_Name (Orig_N2_Parent)
14893 and then Ekind (Entity (Orig_N2_Parent)) in Named_Kind
14894 and then Is_Global (Entity (Orig_N2_Parent))
14895 then
14896 N2 := Aux_N2;
14897 Set_Associated_Node
14898 (Parent (N), Original_Node (Parent (N2)));
14899
14900 -- Common case
14901
14902 else
14903 -- If this is a discriminant reference, always save it.
14904 -- It is used in the instance to find the corresponding
14905 -- discriminant positionally rather than by name.
14906
14907 Set_Original_Discriminant
14908 (N, Original_Discriminant (Get_Associated_Node (N)));
14909 end if;
14910
14911 Reset_Entity (N);
14912 end;
14913
14914 -- The analysis of the generic copy transformed the identifier
14915 -- into another construct. Propagate the changes to the template.
14916
14917 else
14918 N2 := Get_Associated_Node (N);
14919
14920 -- The identifier denotes a call to a parameterless function.
14921 -- Mark the node as resolved when the function is external.
14922
14923 if Nkind (N2) = N_Function_Call then
14924 E := Entity (Name (N2));
14925
14926 if Present (E) and then Is_Global (E) then
14927 Set_Etype (N, Etype (N2));
14928 else
14929 Set_Associated_Node (N, Empty);
14930 Set_Etype (N, Empty);
14931 end if;
14932
14933 -- The identifier denotes a named number that was constant
14934 -- folded. Preserve the original name for ASIS and undo the
14935 -- constant folding which will be repeated in the instance.
14936
14937 elsif Nkind_In (N2, N_Integer_Literal, N_Real_Literal)
14938 and then Is_Entity_Name (Original_Node (N2))
14939 then
14940 Set_Associated_Node (N, Original_Node (N2));
14941 Reset_Entity (N);
14942
14943 -- The identifier resolved to a string literal. Propagate this
14944 -- information to the generic template.
14945
14946 elsif Nkind (N2) = N_String_Literal then
14947 Rewrite (N, New_Copy (N2));
14948
14949 -- The identifier is rewritten as a dereference if it is the
14950 -- prefix of an implicit dereference. Preserve the original
14951 -- tree as the analysis of the instance will expand the node
14952 -- again, but preserve the resolved entity if it is global.
14953
14954 elsif Nkind (N2) = N_Explicit_Dereference then
14955 if Is_Entity_Name (Prefix (N2))
14956 and then Present (Entity (Prefix (N2)))
14957 and then Is_Global (Entity (Prefix (N2)))
14958 then
14959 Set_Associated_Node (N, Prefix (N2));
14960
14961 elsif Nkind (Prefix (N2)) = N_Function_Call
14962 and then Present (Entity (Name (Prefix (N2))))
14963 and then Is_Global (Entity (Name (Prefix (N2))))
14964 then
14965 Rewrite (N,
14966 Make_Explicit_Dereference (Loc,
14967 Prefix =>
14968 Make_Function_Call (Loc,
14969 Name =>
14970 New_Occurrence_Of
14971 (Entity (Name (Prefix (N2))), Loc))));
14972
14973 else
14974 Set_Associated_Node (N, Empty);
14975 Set_Etype (N, Empty);
14976 end if;
14977
14978 -- The subtype mark of a nominally unconstrained object is
14979 -- rewritten as a subtype indication using the bounds of the
14980 -- expression. Recover the original subtype mark.
14981
14982 elsif Nkind (N2) = N_Subtype_Indication
14983 and then Is_Entity_Name (Original_Node (N2))
14984 then
14985 Set_Associated_Node (N, Original_Node (N2));
14986 Reset_Entity (N);
14987 end if;
14988 end if;
14989 end Save_References_In_Identifier;
14990
14991 ---------------------------------
14992 -- Save_References_In_Operator --
14993 ---------------------------------
14994
14995 procedure Save_References_In_Operator (N : Node_Id) is
14996 begin
14997 -- The node did not undergo a transformation
14998
14999 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
15000 if Nkind (N) = N_Op_Concat then
15001 Set_Is_Component_Left_Opnd (N,
15002 Is_Component_Left_Opnd (Get_Associated_Node (N)));
15003
15004 Set_Is_Component_Right_Opnd (N,
15005 Is_Component_Right_Opnd (Get_Associated_Node (N)));
15006 end if;
15007
15008 Reset_Entity (N);
15009
15010 -- The analysis of the generic copy transformed the operator into
15011 -- some other construct. Propagate the changes to the template if
15012 -- applicable.
15013
15014 else
15015 N2 := Get_Associated_Node (N);
15016
15017 -- The operator resoved to a function call
15018
15019 if Nkind (N2) = N_Function_Call then
15020
15021 -- Add explicit qualifications in the generic template for
15022 -- all operands of universal type. This aids resolution by
15023 -- preserving the actual type of a literal or an attribute
15024 -- that yields a universal result.
15025
15026 Qualify_Universal_Operands (N, N2);
15027
15028 E := Entity (Name (N2));
15029
15030 if Present (E) and then Is_Global (E) then
15031 Set_Etype (N, Etype (N2));
15032 else
15033 Set_Associated_Node (N, Empty);
15034 Set_Etype (N, Empty);
15035 end if;
15036
15037 -- The operator was folded into a literal
15038
15039 elsif Nkind_In (N2, N_Integer_Literal,
15040 N_Real_Literal,
15041 N_String_Literal)
15042 then
15043 if Present (Original_Node (N2))
15044 and then Nkind (Original_Node (N2)) = Nkind (N)
15045 then
15046 -- Operation was constant-folded. Whenever possible,
15047 -- recover semantic information from unfolded node,
15048 -- for ASIS use.
15049
15050 Set_Associated_Node (N, Original_Node (N2));
15051
15052 if Nkind (N) = N_Op_Concat then
15053 Set_Is_Component_Left_Opnd (N,
15054 Is_Component_Left_Opnd (Get_Associated_Node (N)));
15055 Set_Is_Component_Right_Opnd (N,
15056 Is_Component_Right_Opnd (Get_Associated_Node (N)));
15057 end if;
15058
15059 Reset_Entity (N);
15060
15061 -- Propagate the constant folding back to the template
15062
15063 else
15064 Rewrite (N, New_Copy (N2));
15065 Set_Analyzed (N, False);
15066 end if;
15067
15068 -- The operator was folded into an enumeration literal. Retain
15069 -- the entity to avoid spurious ambiguities if it is overloaded
15070 -- at the point of instantiation or inlining.
15071
15072 elsif Nkind (N2) = N_Identifier
15073 and then Ekind (Entity (N2)) = E_Enumeration_Literal
15074 then
15075 Rewrite (N, New_Copy (N2));
15076 Set_Analyzed (N, False);
15077 end if;
15078 end if;
15079
15080 -- Complete the operands check if node has not been constant
15081 -- folded.
15082
15083 if Nkind (N) in N_Op then
15084 Save_Entity_Descendants (N);
15085 end if;
15086 end Save_References_In_Operator;
15087
15088 -------------------------------
15089 -- Save_References_In_Pragma --
15090 -------------------------------
15091
15092 procedure Save_References_In_Pragma (Prag : Node_Id) is
15093 Context : Node_Id;
15094 Do_Save : Boolean := True;
15095
15096 use Atree.Unchecked_Access;
15097 -- This code section is part of implementing an untyped tree
15098 -- traversal, so it needs direct access to node fields.
15099
15100 begin
15101 -- Do not save global references in pragmas generated from aspects
15102 -- because the pragmas will be regenerated at instantiation time.
15103
15104 if From_Aspect_Specification (Prag) then
15105 Do_Save := False;
15106
15107 -- The capture of global references within contract-related source
15108 -- pragmas associated with generic packages, subprograms or their
15109 -- respective bodies must be delayed due to timing of annotation
15110 -- analysis. Global references are still captured in routine
15111 -- Save_Global_References_In_Contract.
15112
15113 elsif Is_Generic_Contract_Pragma (Prag) and then Prag /= Templ then
15114 if Is_Package_Contract_Annotation (Prag) then
15115 Context := Find_Related_Package_Or_Body (Prag);
15116 else
15117 pragma Assert (Is_Subprogram_Contract_Annotation (Prag));
15118 Context := Find_Related_Declaration_Or_Body (Prag);
15119 end if;
15120
15121 -- The use of Original_Node accounts for the case when the
15122 -- related context is generic template.
15123
15124 if Requires_Delayed_Save (Original_Node (Context)) then
15125 Do_Save := False;
15126 end if;
15127 end if;
15128
15129 -- For all other cases, save all global references within the
15130 -- descendants, but skip the following semantic fields:
15131
15132 -- Field1 - Next_Pragma
15133 -- Field3 - Corresponding_Aspect
15134 -- Field5 - Next_Rep_Item
15135
15136 if Do_Save then
15137 Save_Global_Descendant (Field2 (Prag));
15138 Save_Global_Descendant (Field4 (Prag));
15139 end if;
15140 end Save_References_In_Pragma;
15141
15142 -- Start of processing for Save_References
15143
15144 begin
15145 if N = Empty then
15146 null;
15147
15148 -- Aggregates
15149
15150 elsif Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
15151 Save_References_In_Aggregate (N);
15152
15153 -- Character literals, operator symbols
15154
15155 elsif Nkind_In (N, N_Character_Literal, N_Operator_Symbol) then
15156 Save_References_In_Char_Lit_Or_Op_Symbol (N);
15157
15158 -- Defining identifiers
15159
15160 elsif Nkind (N) in N_Entity then
15161 null;
15162
15163 -- Identifiers
15164
15165 elsif Nkind (N) = N_Identifier then
15166 Save_References_In_Identifier (N);
15167
15168 -- Operators
15169
15170 elsif Nkind (N) in N_Op then
15171 Save_References_In_Operator (N);
15172
15173 -- Pragmas
15174
15175 elsif Nkind (N) = N_Pragma then
15176 Save_References_In_Pragma (N);
15177
15178 else
15179 Save_References_In_Descendants (N);
15180 end if;
15181
15182 -- Save all global references found within the aspect specifications
15183 -- of the related node.
15184
15185 if Permits_Aspect_Specifications (N) and then Has_Aspects (N) then
15186
15187 -- The capture of global references within aspects associated with
15188 -- generic packages, subprograms or their bodies must be delayed
15189 -- due to timing of annotation analysis. Global references are
15190 -- still captured in routine Save_Global_References_In_Contract.
15191
15192 if Requires_Delayed_Save (N) then
15193 null;
15194
15195 -- Otherwise save all global references within the aspects
15196
15197 else
15198 Save_Global_References_In_Aspects (N);
15199 end if;
15200 end if;
15201 end Save_References;
15202
15203 -- Start of processing for Save_Global_References
15204
15205 begin
15206 Gen_Scope := Current_Scope;
15207
15208 -- If the generic unit is a child unit, references to entities in the
15209 -- parent are treated as local, because they will be resolved anew in
15210 -- the context of the instance of the parent.
15211
15212 while Is_Child_Unit (Gen_Scope)
15213 and then Ekind (Scope (Gen_Scope)) = E_Generic_Package
15214 loop
15215 Gen_Scope := Scope (Gen_Scope);
15216 end loop;
15217
15218 Save_References (Templ);
15219 end Save_Global_References;
15220
15221 ---------------------------------------
15222 -- Save_Global_References_In_Aspects --
15223 ---------------------------------------
15224
15225 procedure Save_Global_References_In_Aspects (N : Node_Id) is
15226 Asp : Node_Id;
15227 Expr : Node_Id;
15228
15229 begin
15230 Asp := First (Aspect_Specifications (N));
15231 while Present (Asp) loop
15232 Expr := Expression (Asp);
15233
15234 if Present (Expr) then
15235 Save_Global_References (Expr);
15236 end if;
15237
15238 Next (Asp);
15239 end loop;
15240 end Save_Global_References_In_Aspects;
15241
15242 ------------------------------------------
15243 -- Set_Copied_Sloc_For_Inherited_Pragma --
15244 ------------------------------------------
15245
15246 procedure Set_Copied_Sloc_For_Inherited_Pragma
15247 (N : Node_Id;
15248 E : Entity_Id)
15249 is
15250 begin
15251 Create_Instantiation_Source (N, E,
15252 Inlined_Body => False,
15253 Inherited_Pragma => True,
15254 Factor => S_Adjustment);
15255 end Set_Copied_Sloc_For_Inherited_Pragma;
15256
15257 --------------------------------------
15258 -- Set_Copied_Sloc_For_Inlined_Body --
15259 --------------------------------------
15260
15261 procedure Set_Copied_Sloc_For_Inlined_Body (N : Node_Id; E : Entity_Id) is
15262 begin
15263 Create_Instantiation_Source (N, E,
15264 Inlined_Body => True,
15265 Inherited_Pragma => False,
15266 Factor => S_Adjustment);
15267 end Set_Copied_Sloc_For_Inlined_Body;
15268
15269 ---------------------
15270 -- Set_Instance_Of --
15271 ---------------------
15272
15273 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id) is
15274 begin
15275 Generic_Renamings.Table (Generic_Renamings.Last) := (A, B, Assoc_Null);
15276 Generic_Renamings_HTable.Set (Generic_Renamings.Last);
15277 Generic_Renamings.Increment_Last;
15278 end Set_Instance_Of;
15279
15280 --------------------
15281 -- Set_Next_Assoc --
15282 --------------------
15283
15284 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr) is
15285 begin
15286 Generic_Renamings.Table (E).Next_In_HTable := Next;
15287 end Set_Next_Assoc;
15288
15289 -------------------
15290 -- Start_Generic --
15291 -------------------
15292
15293 procedure Start_Generic is
15294 begin
15295 -- ??? More things could be factored out in this routine.
15296 -- Should probably be done at a later stage.
15297
15298 Generic_Flags.Append (Inside_A_Generic);
15299 Inside_A_Generic := True;
15300
15301 Expander_Mode_Save_And_Set (False);
15302 end Start_Generic;
15303
15304 ----------------------
15305 -- Set_Instance_Env --
15306 ----------------------
15307
15308 procedure Set_Instance_Env
15309 (Gen_Unit : Entity_Id;
15310 Act_Unit : Entity_Id)
15311 is
15312 Assertion_Status : constant Boolean := Assertions_Enabled;
15313 Save_SPARK_Mode : constant SPARK_Mode_Type := SPARK_Mode;
15314 Save_SPARK_Mode_Pragma : constant Node_Id := SPARK_Mode_Pragma;
15315
15316 begin
15317 -- Regardless of the current mode, predefined units are analyzed in the
15318 -- most current Ada mode, and earlier version Ada checks do not apply
15319 -- to predefined units. Nothing needs to be done for non-internal units.
15320 -- These are always analyzed in the current mode.
15321
15322 if Is_Internal_File_Name
15323 (Fname => Unit_File_Name (Get_Source_Unit (Gen_Unit)),
15324 Renamings_Included => True)
15325 then
15326 Set_Opt_Config_Switches (True, Current_Sem_Unit = Main_Unit);
15327
15328 -- In Ada2012 we may want to enable assertions in an instance of a
15329 -- predefined unit, in which case we need to preserve the current
15330 -- setting for the Assertions_Enabled flag. This will become more
15331 -- critical when pre/postconditions are added to predefined units,
15332 -- as is already the case for some numeric libraries.
15333
15334 if Ada_Version >= Ada_2012 then
15335 Assertions_Enabled := Assertion_Status;
15336 end if;
15337
15338 -- SPARK_Mode for an instance is the one applicable at the point of
15339 -- instantiation.
15340
15341 SPARK_Mode := Save_SPARK_Mode;
15342 SPARK_Mode_Pragma := Save_SPARK_Mode_Pragma;
15343 end if;
15344
15345 Current_Instantiated_Parent :=
15346 (Gen_Id => Gen_Unit,
15347 Act_Id => Act_Unit,
15348 Next_In_HTable => Assoc_Null);
15349 end Set_Instance_Env;
15350
15351 -----------------
15352 -- Switch_View --
15353 -----------------
15354
15355 procedure Switch_View (T : Entity_Id) is
15356 BT : constant Entity_Id := Base_Type (T);
15357 Priv_Elmt : Elmt_Id := No_Elmt;
15358 Priv_Sub : Entity_Id;
15359
15360 begin
15361 -- T may be private but its base type may have been exchanged through
15362 -- some other occurrence, in which case there is nothing to switch
15363 -- besides T itself. Note that a private dependent subtype of a private
15364 -- type might not have been switched even if the base type has been,
15365 -- because of the last branch of Check_Private_View (see comment there).
15366
15367 if not Is_Private_Type (BT) then
15368 Prepend_Elmt (Full_View (T), Exchanged_Views);
15369 Exchange_Declarations (T);
15370 return;
15371 end if;
15372
15373 Priv_Elmt := First_Elmt (Private_Dependents (BT));
15374
15375 if Present (Full_View (BT)) then
15376 Prepend_Elmt (Full_View (BT), Exchanged_Views);
15377 Exchange_Declarations (BT);
15378 end if;
15379
15380 while Present (Priv_Elmt) loop
15381 Priv_Sub := (Node (Priv_Elmt));
15382
15383 -- We avoid flipping the subtype if the Etype of its full view is
15384 -- private because this would result in a malformed subtype. This
15385 -- occurs when the Etype of the subtype full view is the full view of
15386 -- the base type (and since the base types were just switched, the
15387 -- subtype is pointing to the wrong view). This is currently the case
15388 -- for tagged record types, access types (maybe more?) and needs to
15389 -- be resolved. ???
15390
15391 if Present (Full_View (Priv_Sub))
15392 and then not Is_Private_Type (Etype (Full_View (Priv_Sub)))
15393 then
15394 Prepend_Elmt (Full_View (Priv_Sub), Exchanged_Views);
15395 Exchange_Declarations (Priv_Sub);
15396 end if;
15397
15398 Next_Elmt (Priv_Elmt);
15399 end loop;
15400 end Switch_View;
15401
15402 -----------------
15403 -- True_Parent --
15404 -----------------
15405
15406 function True_Parent (N : Node_Id) return Node_Id is
15407 begin
15408 if Nkind (Parent (N)) = N_Subunit then
15409 return Parent (Corresponding_Stub (Parent (N)));
15410 else
15411 return Parent (N);
15412 end if;
15413 end True_Parent;
15414
15415 -----------------------------
15416 -- Valid_Default_Attribute --
15417 -----------------------------
15418
15419 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id) is
15420 Attr_Id : constant Attribute_Id :=
15421 Get_Attribute_Id (Attribute_Name (Def));
15422 T : constant Entity_Id := Entity (Prefix (Def));
15423 Is_Fun : constant Boolean := (Ekind (Nam) = E_Function);
15424 F : Entity_Id;
15425 Num_F : Nat;
15426 OK : Boolean;
15427
15428 begin
15429 if No (T) or else T = Any_Id then
15430 return;
15431 end if;
15432
15433 Num_F := 0;
15434 F := First_Formal (Nam);
15435 while Present (F) loop
15436 Num_F := Num_F + 1;
15437 Next_Formal (F);
15438 end loop;
15439
15440 case Attr_Id is
15441 when Attribute_Adjacent | Attribute_Ceiling | Attribute_Copy_Sign |
15442 Attribute_Floor | Attribute_Fraction | Attribute_Machine |
15443 Attribute_Model | Attribute_Remainder | Attribute_Rounding |
15444 Attribute_Unbiased_Rounding =>
15445 OK := Is_Fun
15446 and then Num_F = 1
15447 and then Is_Floating_Point_Type (T);
15448
15449 when Attribute_Image | Attribute_Pred | Attribute_Succ |
15450 Attribute_Value | Attribute_Wide_Image |
15451 Attribute_Wide_Value =>
15452 OK := (Is_Fun and then Num_F = 1 and then Is_Scalar_Type (T));
15453
15454 when Attribute_Max | Attribute_Min =>
15455 OK := (Is_Fun and then Num_F = 2 and then Is_Scalar_Type (T));
15456
15457 when Attribute_Input =>
15458 OK := (Is_Fun and then Num_F = 1);
15459
15460 when Attribute_Output | Attribute_Read | Attribute_Write =>
15461 OK := (not Is_Fun and then Num_F = 2);
15462
15463 when others =>
15464 OK := False;
15465 end case;
15466
15467 if not OK then
15468 Error_Msg_N
15469 ("attribute reference has wrong profile for subprogram", Def);
15470 end if;
15471 end Valid_Default_Attribute;
15472
15473 end Sem_Ch12;