[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 when N_Formal_Subprogram_Declaration =>
1463 exit when Kind in N_Formal_Subprogram_Declaration
1464 and then
1465 Chars
1466 (Defining_Unit_Name (Specification (Formal))) =
1467 Chars
1468 (Defining_Unit_Name (Specification (Analyzed_Formal)));
1469
1470 when N_Formal_Package_Declaration =>
1471 exit when Nkind_In (Kind, N_Formal_Package_Declaration,
1472 N_Generic_Package_Declaration,
1473 N_Package_Declaration);
1474
1475 when N_Use_Package_Clause
1476 | N_Use_Type_Clause
1477 =>
1478 exit;
1479
1480 when others =>
1481
1482 -- Skip freeze nodes, and nodes inserted to replace
1483 -- unrecognized pragmas.
1484
1485 exit when
1486 Kind not in N_Formal_Subprogram_Declaration
1487 and then not Nkind_In (Kind, N_Subprogram_Declaration,
1488 N_Freeze_Entity,
1489 N_Null_Statement,
1490 N_Itype_Reference)
1491 and then Chars (Defining_Identifier (Formal)) =
1492 Chars (Defining_Identifier (Analyzed_Formal));
1493 end case;
1494
1495 Next (Analyzed_Formal);
1496 end loop;
1497 end Set_Analyzed_Formal;
1498
1499 -- Start of processing for Analyze_Associations
1500
1501 begin
1502 Actuals := Generic_Associations (I_Node);
1503
1504 if Present (Actuals) then
1505
1506 -- Check for an Others choice, indicating a partial parameterization
1507 -- for a formal package.
1508
1509 Actual := First (Actuals);
1510 while Present (Actual) loop
1511 if Nkind (Actual) = N_Others_Choice then
1512 Others_Present := True;
1513 Others_Choice := Actual;
1514
1515 if Present (Next (Actual)) then
1516 Error_Msg_N ("others must be last association", Actual);
1517 end if;
1518
1519 -- This subprogram is used both for formal packages and for
1520 -- instantiations. For the latter, associations must all be
1521 -- explicit.
1522
1523 if Nkind (I_Node) /= N_Formal_Package_Declaration
1524 and then Comes_From_Source (I_Node)
1525 then
1526 Error_Msg_N
1527 ("others association not allowed in an instance",
1528 Actual);
1529 end if;
1530
1531 -- In any case, nothing to do after the others association
1532
1533 exit;
1534
1535 elsif Box_Present (Actual)
1536 and then Comes_From_Source (I_Node)
1537 and then Nkind (I_Node) /= N_Formal_Package_Declaration
1538 then
1539 Error_Msg_N
1540 ("box association not allowed in an instance", Actual);
1541 end if;
1542
1543 Next (Actual);
1544 end loop;
1545
1546 -- If named associations are present, save first named association
1547 -- (it may of course be Empty) to facilitate subsequent name search.
1548
1549 First_Named := First (Actuals);
1550 while Present (First_Named)
1551 and then Nkind (First_Named) /= N_Others_Choice
1552 and then No (Selector_Name (First_Named))
1553 loop
1554 Num_Actuals := Num_Actuals + 1;
1555 Next (First_Named);
1556 end loop;
1557 end if;
1558
1559 Named := First_Named;
1560 while Present (Named) loop
1561 if Nkind (Named) /= N_Others_Choice
1562 and then No (Selector_Name (Named))
1563 then
1564 Error_Msg_N ("invalid positional actual after named one", Named);
1565 Abandon_Instantiation (Named);
1566 end if;
1567
1568 -- A named association may lack an actual parameter, if it was
1569 -- introduced for a default subprogram that turns out to be local
1570 -- to the outer instantiation. If it has a box association it must
1571 -- correspond to some formal in the generic.
1572
1573 if Nkind (Named) /= N_Others_Choice
1574 and then (Present (Explicit_Generic_Actual_Parameter (Named))
1575 or else Box_Present (Named))
1576 then
1577 Num_Actuals := Num_Actuals + 1;
1578 end if;
1579
1580 Next (Named);
1581 end loop;
1582
1583 if Present (Formals) then
1584 Formal := First_Non_Pragma (Formals);
1585 Analyzed_Formal := First_Non_Pragma (F_Copy);
1586
1587 if Present (Actuals) then
1588 Actual := First (Actuals);
1589
1590 -- All formals should have default values
1591
1592 else
1593 Actual := Empty;
1594 end if;
1595
1596 while Present (Formal) loop
1597 Set_Analyzed_Formal;
1598 Saved_Formal := Next_Non_Pragma (Formal);
1599
1600 case Nkind (Formal) is
1601 when N_Formal_Object_Declaration =>
1602 Match :=
1603 Matching_Actual
1604 (Defining_Identifier (Formal),
1605 Defining_Identifier (Analyzed_Formal));
1606
1607 if No (Match) and then Partial_Parameterization then
1608 Process_Default (Formal);
1609
1610 else
1611 Append_List
1612 (Instantiate_Object (Formal, Match, Analyzed_Formal),
1613 Assoc);
1614
1615 -- For a defaulted in_parameter, create an entry in the
1616 -- the list of defaulted actuals, for GNATProve use. Do
1617 -- not included these defaults for an instance nested
1618 -- within a generic, because the defaults are also used
1619 -- in the analysis of the enclosing generic, and only
1620 -- defaulted subprograms are relevant there.
1621
1622 if No (Match) and then not Inside_A_Generic then
1623 Append_To (Default_Actuals,
1624 Make_Generic_Association (Sloc (I_Node),
1625 Selector_Name =>
1626 New_Occurrence_Of
1627 (Defining_Identifier (Formal), Sloc (I_Node)),
1628 Explicit_Generic_Actual_Parameter =>
1629 New_Copy_Tree (Default_Expression (Formal))));
1630 end if;
1631 end if;
1632
1633 -- If the object is a call to an expression function, this
1634 -- is a freezing point for it.
1635
1636 if Is_Entity_Name (Match)
1637 and then Present (Entity (Match))
1638 and then Nkind
1639 (Original_Node (Unit_Declaration_Node (Entity (Match))))
1640 = N_Expression_Function
1641 then
1642 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1643 end if;
1644
1645 when N_Formal_Type_Declaration =>
1646 Match :=
1647 Matching_Actual
1648 (Defining_Identifier (Formal),
1649 Defining_Identifier (Analyzed_Formal));
1650
1651 if No (Match) then
1652 if Partial_Parameterization then
1653 Process_Default (Formal);
1654
1655 else
1656 Error_Msg_Sloc := Sloc (Gen_Unit);
1657 Error_Msg_NE
1658 ("missing actual&",
1659 Instantiation_Node, Defining_Identifier (Formal));
1660 Error_Msg_NE
1661 ("\in instantiation of & declared#",
1662 Instantiation_Node, Gen_Unit);
1663 Abandon_Instantiation (Instantiation_Node);
1664 end if;
1665
1666 else
1667 Analyze (Match);
1668 Append_List
1669 (Instantiate_Type
1670 (Formal, Match, Analyzed_Formal, Assoc),
1671 Assoc);
1672
1673 if Is_Fixed_Point_Type (Entity (Match)) then
1674 Check_Fixed_Point_Actual (Match);
1675 end if;
1676
1677 -- An instantiation is a freeze point for the actuals,
1678 -- unless this is a rewritten formal package, or the
1679 -- formal is an Ada 2012 formal incomplete type.
1680
1681 if Nkind (I_Node) = N_Formal_Package_Declaration
1682 or else
1683 (Ada_Version >= Ada_2012
1684 and then
1685 Ekind (Defining_Identifier (Analyzed_Formal)) =
1686 E_Incomplete_Type)
1687 then
1688 null;
1689
1690 else
1691 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1692 end if;
1693 end if;
1694
1695 -- A remote access-to-class-wide type is not a legal actual
1696 -- for a generic formal of an access type (E.2.2(17/2)).
1697 -- In GNAT an exception to this rule is introduced when
1698 -- the formal is marked as remote using implementation
1699 -- defined aspect/pragma Remote_Access_Type. In that case
1700 -- the actual must be remote as well.
1701
1702 -- If the current instantiation is the construction of a
1703 -- local copy for a formal package the actuals may be
1704 -- defaulted, and there is no matching actual to check.
1705
1706 if Nkind (Analyzed_Formal) = N_Formal_Type_Declaration
1707 and then
1708 Nkind (Formal_Type_Definition (Analyzed_Formal)) =
1709 N_Access_To_Object_Definition
1710 and then Present (Match)
1711 then
1712 declare
1713 Formal_Ent : constant Entity_Id :=
1714 Defining_Identifier (Analyzed_Formal);
1715 begin
1716 if Is_Remote_Access_To_Class_Wide_Type (Entity (Match))
1717 = Is_Remote_Types (Formal_Ent)
1718 then
1719 -- Remoteness of formal and actual match
1720
1721 null;
1722
1723 elsif Is_Remote_Types (Formal_Ent) then
1724
1725 -- Remote formal, non-remote actual
1726
1727 Error_Msg_NE
1728 ("actual for& must be remote", Match, Formal_Ent);
1729
1730 else
1731 -- Non-remote formal, remote actual
1732
1733 Error_Msg_NE
1734 ("actual for& may not be remote",
1735 Match, Formal_Ent);
1736 end if;
1737 end;
1738 end if;
1739
1740 when N_Formal_Subprogram_Declaration =>
1741 Match :=
1742 Matching_Actual
1743 (Defining_Unit_Name (Specification (Formal)),
1744 Defining_Unit_Name (Specification (Analyzed_Formal)));
1745
1746 -- If the formal subprogram has the same name as another
1747 -- formal subprogram of the generic, then a named
1748 -- association is illegal (12.3(9)). Exclude named
1749 -- associations that are generated for a nested instance.
1750
1751 if Present (Match)
1752 and then Is_Named_Assoc
1753 and then Comes_From_Source (Found_Assoc)
1754 then
1755 Check_Overloaded_Formal_Subprogram (Formal);
1756 end if;
1757
1758 -- If there is no corresponding actual, this may be case
1759 -- of partial parameterization, or else the formal has a
1760 -- default or a box.
1761
1762 if No (Match) and then Partial_Parameterization then
1763 Process_Default (Formal);
1764
1765 if Nkind (I_Node) = N_Formal_Package_Declaration then
1766 Check_Overloaded_Formal_Subprogram (Formal);
1767 end if;
1768
1769 else
1770 Append_To (Assoc,
1771 Instantiate_Formal_Subprogram
1772 (Formal, Match, Analyzed_Formal));
1773
1774 -- An instantiation is a freeze point for the actuals,
1775 -- unless this is a rewritten formal package.
1776
1777 if Nkind (I_Node) /= N_Formal_Package_Declaration
1778 and then Nkind (Match) = N_Identifier
1779 and then Is_Subprogram (Entity (Match))
1780
1781 -- The actual subprogram may rename a routine defined
1782 -- in Standard. Avoid freezing such renamings because
1783 -- subprograms coming from Standard cannot be frozen.
1784
1785 and then
1786 not Renames_Standard_Subprogram (Entity (Match))
1787
1788 -- If the actual subprogram comes from a different
1789 -- unit, it is already frozen, either by a body in
1790 -- that unit or by the end of the declarative part
1791 -- of the unit. This check avoids the freezing of
1792 -- subprograms defined in Standard which are used
1793 -- as generic actuals.
1794
1795 and then In_Same_Code_Unit (Entity (Match), I_Node)
1796 and then Has_Fully_Defined_Profile (Entity (Match))
1797 then
1798 -- Mark the subprogram as having a delayed freeze
1799 -- since this may be an out-of-order action.
1800
1801 Set_Has_Delayed_Freeze (Entity (Match));
1802 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1803 end if;
1804 end if;
1805
1806 -- If this is a nested generic, preserve default for later
1807 -- instantiations. We do this as well for GNATProve use,
1808 -- so that the list of generic associations is complete.
1809
1810 if No (Match) and then Box_Present (Formal) then
1811 declare
1812 Subp : constant Entity_Id :=
1813 Defining_Unit_Name (Specification (Last (Assoc)));
1814
1815 begin
1816 Append_To (Default_Actuals,
1817 Make_Generic_Association (Sloc (I_Node),
1818 Selector_Name =>
1819 New_Occurrence_Of (Subp, Sloc (I_Node)),
1820 Explicit_Generic_Actual_Parameter =>
1821 New_Occurrence_Of (Subp, Sloc (I_Node))));
1822 end;
1823 end if;
1824
1825 when N_Formal_Package_Declaration =>
1826 Match :=
1827 Matching_Actual
1828 (Defining_Identifier (Formal),
1829 Defining_Identifier (Original_Node (Analyzed_Formal)));
1830
1831 if No (Match) then
1832 if Partial_Parameterization then
1833 Process_Default (Formal);
1834
1835 else
1836 Error_Msg_Sloc := Sloc (Gen_Unit);
1837 Error_Msg_NE
1838 ("missing actual&",
1839 Instantiation_Node, Defining_Identifier (Formal));
1840 Error_Msg_NE
1841 ("\in instantiation of & declared#",
1842 Instantiation_Node, Gen_Unit);
1843
1844 Abandon_Instantiation (Instantiation_Node);
1845 end if;
1846
1847 else
1848 Analyze (Match);
1849 Append_List
1850 (Instantiate_Formal_Package
1851 (Formal, Match, Analyzed_Formal),
1852 Assoc);
1853 end if;
1854
1855 -- For use type and use package appearing in the generic part,
1856 -- we have already copied them, so we can just move them where
1857 -- they belong (we mustn't recopy them since this would mess up
1858 -- the Sloc values).
1859
1860 when N_Use_Package_Clause
1861 | N_Use_Type_Clause
1862 =>
1863 if Nkind (Original_Node (I_Node)) =
1864 N_Formal_Package_Declaration
1865 then
1866 Append (New_Copy_Tree (Formal), Assoc);
1867 else
1868 Remove (Formal);
1869 Append (Formal, Assoc);
1870 end if;
1871
1872 when others =>
1873 raise Program_Error;
1874 end case;
1875
1876 Formal := Saved_Formal;
1877 Next_Non_Pragma (Analyzed_Formal);
1878 end loop;
1879
1880 if Num_Actuals > Num_Matched then
1881 Error_Msg_Sloc := Sloc (Gen_Unit);
1882
1883 if Present (Selector_Name (Actual)) then
1884 Error_Msg_NE
1885 ("unmatched actual &", Actual, Selector_Name (Actual));
1886 Error_Msg_NE
1887 ("\in instantiation of & declared#", Actual, Gen_Unit);
1888 else
1889 Error_Msg_NE
1890 ("unmatched actual in instantiation of & declared#",
1891 Actual, Gen_Unit);
1892 end if;
1893 end if;
1894
1895 elsif Present (Actuals) then
1896 Error_Msg_N
1897 ("too many actuals in generic instantiation", Instantiation_Node);
1898 end if;
1899
1900 -- An instantiation freezes all generic actuals. The only exceptions
1901 -- to this are incomplete types and subprograms which are not fully
1902 -- defined at the point of instantiation.
1903
1904 declare
1905 Elmt : Elmt_Id := First_Elmt (Actuals_To_Freeze);
1906 begin
1907 while Present (Elmt) loop
1908 Freeze_Before (I_Node, Node (Elmt));
1909 Next_Elmt (Elmt);
1910 end loop;
1911 end;
1912
1913 -- If there are default subprograms, normalize the tree by adding
1914 -- explicit associations for them. This is required if the instance
1915 -- appears within a generic.
1916
1917 if not Is_Empty_List (Default_Actuals) then
1918 declare
1919 Default : Node_Id;
1920
1921 begin
1922 Default := First (Default_Actuals);
1923 while Present (Default) loop
1924 Mark_Rewrite_Insertion (Default);
1925 Next (Default);
1926 end loop;
1927
1928 if No (Actuals) then
1929 Set_Generic_Associations (I_Node, Default_Actuals);
1930 else
1931 Append_List_To (Actuals, Default_Actuals);
1932 end if;
1933 end;
1934 end if;
1935
1936 -- If this is a formal package, normalize the parameter list by adding
1937 -- explicit box associations for the formals that are covered by an
1938 -- Others_Choice.
1939
1940 if not Is_Empty_List (Default_Formals) then
1941 Append_List (Default_Formals, Formals);
1942 end if;
1943
1944 return Assoc;
1945 end Analyze_Associations;
1946
1947 -------------------------------
1948 -- Analyze_Formal_Array_Type --
1949 -------------------------------
1950
1951 procedure Analyze_Formal_Array_Type
1952 (T : in out Entity_Id;
1953 Def : Node_Id)
1954 is
1955 DSS : Node_Id;
1956
1957 begin
1958 -- Treated like a non-generic array declaration, with additional
1959 -- semantic checks.
1960
1961 Enter_Name (T);
1962
1963 if Nkind (Def) = N_Constrained_Array_Definition then
1964 DSS := First (Discrete_Subtype_Definitions (Def));
1965 while Present (DSS) loop
1966 if Nkind_In (DSS, N_Subtype_Indication,
1967 N_Range,
1968 N_Attribute_Reference)
1969 then
1970 Error_Msg_N ("only a subtype mark is allowed in a formal", DSS);
1971 end if;
1972
1973 Next (DSS);
1974 end loop;
1975 end if;
1976
1977 Array_Type_Declaration (T, Def);
1978 Set_Is_Generic_Type (Base_Type (T));
1979
1980 if Ekind (Component_Type (T)) = E_Incomplete_Type
1981 and then No (Full_View (Component_Type (T)))
1982 then
1983 Error_Msg_N ("premature usage of incomplete type", Def);
1984
1985 -- Check that range constraint is not allowed on the component type
1986 -- of a generic formal array type (AARM 12.5.3(3))
1987
1988 elsif Is_Internal (Component_Type (T))
1989 and then Present (Subtype_Indication (Component_Definition (Def)))
1990 and then Nkind (Original_Node
1991 (Subtype_Indication (Component_Definition (Def)))) =
1992 N_Subtype_Indication
1993 then
1994 Error_Msg_N
1995 ("in a formal, a subtype indication can only be "
1996 & "a subtype mark (RM 12.5.3(3))",
1997 Subtype_Indication (Component_Definition (Def)));
1998 end if;
1999
2000 end Analyze_Formal_Array_Type;
2001
2002 ---------------------------------------------
2003 -- Analyze_Formal_Decimal_Fixed_Point_Type --
2004 ---------------------------------------------
2005
2006 -- As for other generic types, we create a valid type representation with
2007 -- legal but arbitrary attributes, whose values are never considered
2008 -- static. For all scalar types we introduce an anonymous base type, with
2009 -- the same attributes. We choose the corresponding integer type to be
2010 -- Standard_Integer.
2011 -- Here and in other similar routines, the Sloc of the generated internal
2012 -- type must be the same as the sloc of the defining identifier of the
2013 -- formal type declaration, to provide proper source navigation.
2014
2015 procedure Analyze_Formal_Decimal_Fixed_Point_Type
2016 (T : Entity_Id;
2017 Def : Node_Id)
2018 is
2019 Loc : constant Source_Ptr := Sloc (Def);
2020
2021 Base : constant Entity_Id :=
2022 New_Internal_Entity
2023 (E_Decimal_Fixed_Point_Type,
2024 Current_Scope,
2025 Sloc (Defining_Identifier (Parent (Def))), 'G');
2026
2027 Int_Base : constant Entity_Id := Standard_Integer;
2028 Delta_Val : constant Ureal := Ureal_1;
2029 Digs_Val : constant Uint := Uint_6;
2030
2031 function Make_Dummy_Bound return Node_Id;
2032 -- Return a properly typed universal real literal to use as a bound
2033
2034 ----------------------
2035 -- Make_Dummy_Bound --
2036 ----------------------
2037
2038 function Make_Dummy_Bound return Node_Id is
2039 Bound : constant Node_Id := Make_Real_Literal (Loc, Ureal_1);
2040 begin
2041 Set_Etype (Bound, Universal_Real);
2042 return Bound;
2043 end Make_Dummy_Bound;
2044
2045 -- Start of processing for Analyze_Formal_Decimal_Fixed_Point_Type
2046
2047 begin
2048 Enter_Name (T);
2049
2050 Set_Etype (Base, Base);
2051 Set_Size_Info (Base, Int_Base);
2052 Set_RM_Size (Base, RM_Size (Int_Base));
2053 Set_First_Rep_Item (Base, First_Rep_Item (Int_Base));
2054 Set_Digits_Value (Base, Digs_Val);
2055 Set_Delta_Value (Base, Delta_Val);
2056 Set_Small_Value (Base, Delta_Val);
2057 Set_Scalar_Range (Base,
2058 Make_Range (Loc,
2059 Low_Bound => Make_Dummy_Bound,
2060 High_Bound => Make_Dummy_Bound));
2061
2062 Set_Is_Generic_Type (Base);
2063 Set_Parent (Base, Parent (Def));
2064
2065 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
2066 Set_Etype (T, Base);
2067 Set_Size_Info (T, Int_Base);
2068 Set_RM_Size (T, RM_Size (Int_Base));
2069 Set_First_Rep_Item (T, First_Rep_Item (Int_Base));
2070 Set_Digits_Value (T, Digs_Val);
2071 Set_Delta_Value (T, Delta_Val);
2072 Set_Small_Value (T, Delta_Val);
2073 Set_Scalar_Range (T, Scalar_Range (Base));
2074 Set_Is_Constrained (T);
2075
2076 Check_Restriction (No_Fixed_Point, Def);
2077 end Analyze_Formal_Decimal_Fixed_Point_Type;
2078
2079 -------------------------------------------
2080 -- Analyze_Formal_Derived_Interface_Type --
2081 -------------------------------------------
2082
2083 procedure Analyze_Formal_Derived_Interface_Type
2084 (N : Node_Id;
2085 T : Entity_Id;
2086 Def : Node_Id)
2087 is
2088 Loc : constant Source_Ptr := Sloc (Def);
2089
2090 begin
2091 -- Rewrite as a type declaration of a derived type. This ensures that
2092 -- the interface list and primitive operations are properly captured.
2093
2094 Rewrite (N,
2095 Make_Full_Type_Declaration (Loc,
2096 Defining_Identifier => T,
2097 Type_Definition => Def));
2098 Analyze (N);
2099 Set_Is_Generic_Type (T);
2100 end Analyze_Formal_Derived_Interface_Type;
2101
2102 ---------------------------------
2103 -- Analyze_Formal_Derived_Type --
2104 ---------------------------------
2105
2106 procedure Analyze_Formal_Derived_Type
2107 (N : Node_Id;
2108 T : Entity_Id;
2109 Def : Node_Id)
2110 is
2111 Loc : constant Source_Ptr := Sloc (Def);
2112 Unk_Disc : constant Boolean := Unknown_Discriminants_Present (N);
2113 New_N : Node_Id;
2114
2115 begin
2116 Set_Is_Generic_Type (T);
2117
2118 if Private_Present (Def) then
2119 New_N :=
2120 Make_Private_Extension_Declaration (Loc,
2121 Defining_Identifier => T,
2122 Discriminant_Specifications => Discriminant_Specifications (N),
2123 Unknown_Discriminants_Present => Unk_Disc,
2124 Subtype_Indication => Subtype_Mark (Def),
2125 Interface_List => Interface_List (Def));
2126
2127 Set_Abstract_Present (New_N, Abstract_Present (Def));
2128 Set_Limited_Present (New_N, Limited_Present (Def));
2129 Set_Synchronized_Present (New_N, Synchronized_Present (Def));
2130
2131 else
2132 New_N :=
2133 Make_Full_Type_Declaration (Loc,
2134 Defining_Identifier => T,
2135 Discriminant_Specifications =>
2136 Discriminant_Specifications (Parent (T)),
2137 Type_Definition =>
2138 Make_Derived_Type_Definition (Loc,
2139 Subtype_Indication => Subtype_Mark (Def)));
2140
2141 Set_Abstract_Present
2142 (Type_Definition (New_N), Abstract_Present (Def));
2143 Set_Limited_Present
2144 (Type_Definition (New_N), Limited_Present (Def));
2145 end if;
2146
2147 Rewrite (N, New_N);
2148 Analyze (N);
2149
2150 if Unk_Disc then
2151 if not Is_Composite_Type (T) then
2152 Error_Msg_N
2153 ("unknown discriminants not allowed for elementary types", N);
2154 else
2155 Set_Has_Unknown_Discriminants (T);
2156 Set_Is_Constrained (T, False);
2157 end if;
2158 end if;
2159
2160 -- If the parent type has a known size, so does the formal, which makes
2161 -- legal representation clauses that involve the formal.
2162
2163 Set_Size_Known_At_Compile_Time
2164 (T, Size_Known_At_Compile_Time (Entity (Subtype_Mark (Def))));
2165 end Analyze_Formal_Derived_Type;
2166
2167 ----------------------------------
2168 -- Analyze_Formal_Discrete_Type --
2169 ----------------------------------
2170
2171 -- The operations defined for a discrete types are those of an enumeration
2172 -- type. The size is set to an arbitrary value, for use in analyzing the
2173 -- generic unit.
2174
2175 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id) is
2176 Loc : constant Source_Ptr := Sloc (Def);
2177 Lo : Node_Id;
2178 Hi : Node_Id;
2179
2180 Base : constant Entity_Id :=
2181 New_Internal_Entity
2182 (E_Floating_Point_Type, Current_Scope,
2183 Sloc (Defining_Identifier (Parent (Def))), 'G');
2184
2185 begin
2186 Enter_Name (T);
2187 Set_Ekind (T, E_Enumeration_Subtype);
2188 Set_Etype (T, Base);
2189 Init_Size (T, 8);
2190 Init_Alignment (T);
2191 Set_Is_Generic_Type (T);
2192 Set_Is_Constrained (T);
2193
2194 -- For semantic analysis, the bounds of the type must be set to some
2195 -- non-static value. The simplest is to create attribute nodes for those
2196 -- bounds, that refer to the type itself. These bounds are never
2197 -- analyzed but serve as place-holders.
2198
2199 Lo :=
2200 Make_Attribute_Reference (Loc,
2201 Attribute_Name => Name_First,
2202 Prefix => New_Occurrence_Of (T, Loc));
2203 Set_Etype (Lo, T);
2204
2205 Hi :=
2206 Make_Attribute_Reference (Loc,
2207 Attribute_Name => Name_Last,
2208 Prefix => New_Occurrence_Of (T, Loc));
2209 Set_Etype (Hi, T);
2210
2211 Set_Scalar_Range (T,
2212 Make_Range (Loc,
2213 Low_Bound => Lo,
2214 High_Bound => Hi));
2215
2216 Set_Ekind (Base, E_Enumeration_Type);
2217 Set_Etype (Base, Base);
2218 Init_Size (Base, 8);
2219 Init_Alignment (Base);
2220 Set_Is_Generic_Type (Base);
2221 Set_Scalar_Range (Base, Scalar_Range (T));
2222 Set_Parent (Base, Parent (Def));
2223 end Analyze_Formal_Discrete_Type;
2224
2225 ----------------------------------
2226 -- Analyze_Formal_Floating_Type --
2227 ---------------------------------
2228
2229 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id) is
2230 Base : constant Entity_Id :=
2231 New_Internal_Entity
2232 (E_Floating_Point_Type, Current_Scope,
2233 Sloc (Defining_Identifier (Parent (Def))), 'G');
2234
2235 begin
2236 -- The various semantic attributes are taken from the predefined type
2237 -- Float, just so that all of them are initialized. Their values are
2238 -- never used because no constant folding or expansion takes place in
2239 -- the generic itself.
2240
2241 Enter_Name (T);
2242 Set_Ekind (T, E_Floating_Point_Subtype);
2243 Set_Etype (T, Base);
2244 Set_Size_Info (T, (Standard_Float));
2245 Set_RM_Size (T, RM_Size (Standard_Float));
2246 Set_Digits_Value (T, Digits_Value (Standard_Float));
2247 Set_Scalar_Range (T, Scalar_Range (Standard_Float));
2248 Set_Is_Constrained (T);
2249
2250 Set_Is_Generic_Type (Base);
2251 Set_Etype (Base, Base);
2252 Set_Size_Info (Base, (Standard_Float));
2253 Set_RM_Size (Base, RM_Size (Standard_Float));
2254 Set_Digits_Value (Base, Digits_Value (Standard_Float));
2255 Set_Scalar_Range (Base, Scalar_Range (Standard_Float));
2256 Set_Parent (Base, Parent (Def));
2257
2258 Check_Restriction (No_Floating_Point, Def);
2259 end Analyze_Formal_Floating_Type;
2260
2261 -----------------------------------
2262 -- Analyze_Formal_Interface_Type;--
2263 -----------------------------------
2264
2265 procedure Analyze_Formal_Interface_Type
2266 (N : Node_Id;
2267 T : Entity_Id;
2268 Def : Node_Id)
2269 is
2270 Loc : constant Source_Ptr := Sloc (N);
2271 New_N : Node_Id;
2272
2273 begin
2274 New_N :=
2275 Make_Full_Type_Declaration (Loc,
2276 Defining_Identifier => T,
2277 Type_Definition => Def);
2278
2279 Rewrite (N, New_N);
2280 Analyze (N);
2281 Set_Is_Generic_Type (T);
2282 end Analyze_Formal_Interface_Type;
2283
2284 ---------------------------------
2285 -- Analyze_Formal_Modular_Type --
2286 ---------------------------------
2287
2288 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id) is
2289 begin
2290 -- Apart from their entity kind, generic modular types are treated like
2291 -- signed integer types, and have the same attributes.
2292
2293 Analyze_Formal_Signed_Integer_Type (T, Def);
2294 Set_Ekind (T, E_Modular_Integer_Subtype);
2295 Set_Ekind (Etype (T), E_Modular_Integer_Type);
2296
2297 end Analyze_Formal_Modular_Type;
2298
2299 ---------------------------------------
2300 -- Analyze_Formal_Object_Declaration --
2301 ---------------------------------------
2302
2303 procedure Analyze_Formal_Object_Declaration (N : Node_Id) is
2304 E : constant Node_Id := Default_Expression (N);
2305 Id : constant Node_Id := Defining_Identifier (N);
2306 K : Entity_Kind;
2307 T : Node_Id;
2308
2309 begin
2310 Enter_Name (Id);
2311
2312 -- Determine the mode of the formal object
2313
2314 if Out_Present (N) then
2315 K := E_Generic_In_Out_Parameter;
2316
2317 if not In_Present (N) then
2318 Error_Msg_N ("formal generic objects cannot have mode OUT", N);
2319 end if;
2320
2321 else
2322 K := E_Generic_In_Parameter;
2323 end if;
2324
2325 if Present (Subtype_Mark (N)) then
2326 Find_Type (Subtype_Mark (N));
2327 T := Entity (Subtype_Mark (N));
2328
2329 -- Verify that there is no redundant null exclusion
2330
2331 if Null_Exclusion_Present (N) then
2332 if not Is_Access_Type (T) then
2333 Error_Msg_N
2334 ("null exclusion can only apply to an access type", N);
2335
2336 elsif Can_Never_Be_Null (T) then
2337 Error_Msg_NE
2338 ("`NOT NULL` not allowed (& already excludes null)", N, T);
2339 end if;
2340 end if;
2341
2342 -- Ada 2005 (AI-423): Formal object with an access definition
2343
2344 else
2345 Check_Access_Definition (N);
2346 T := Access_Definition
2347 (Related_Nod => N,
2348 N => Access_Definition (N));
2349 end if;
2350
2351 if Ekind (T) = E_Incomplete_Type then
2352 declare
2353 Error_Node : Node_Id;
2354
2355 begin
2356 if Present (Subtype_Mark (N)) then
2357 Error_Node := Subtype_Mark (N);
2358 else
2359 Check_Access_Definition (N);
2360 Error_Node := Access_Definition (N);
2361 end if;
2362
2363 Error_Msg_N ("premature usage of incomplete type", Error_Node);
2364 end;
2365 end if;
2366
2367 if K = E_Generic_In_Parameter then
2368
2369 -- Ada 2005 (AI-287): Limited aggregates allowed in generic formals
2370
2371 if Ada_Version < Ada_2005 and then Is_Limited_Type (T) then
2372 Error_Msg_N
2373 ("generic formal of mode IN must not be of limited type", N);
2374 Explain_Limited_Type (T, N);
2375 end if;
2376
2377 if Is_Abstract_Type (T) then
2378 Error_Msg_N
2379 ("generic formal of mode IN must not be of abstract type", N);
2380 end if;
2381
2382 if Present (E) then
2383 Preanalyze_Spec_Expression (E, T);
2384
2385 if Is_Limited_Type (T) and then not OK_For_Limited_Init (T, E) then
2386 Error_Msg_N
2387 ("initialization not allowed for limited types", E);
2388 Explain_Limited_Type (T, E);
2389 end if;
2390 end if;
2391
2392 Set_Ekind (Id, K);
2393 Set_Etype (Id, T);
2394
2395 -- Case of generic IN OUT parameter
2396
2397 else
2398 -- If the formal has an unconstrained type, construct its actual
2399 -- subtype, as is done for subprogram formals. In this fashion, all
2400 -- its uses can refer to specific bounds.
2401
2402 Set_Ekind (Id, K);
2403 Set_Etype (Id, T);
2404
2405 if (Is_Array_Type (T) and then not Is_Constrained (T))
2406 or else (Ekind (T) = E_Record_Type and then Has_Discriminants (T))
2407 then
2408 declare
2409 Non_Freezing_Ref : constant Node_Id :=
2410 New_Occurrence_Of (Id, Sloc (Id));
2411 Decl : Node_Id;
2412
2413 begin
2414 -- Make sure the actual subtype doesn't generate bogus freezing
2415
2416 Set_Must_Not_Freeze (Non_Freezing_Ref);
2417 Decl := Build_Actual_Subtype (T, Non_Freezing_Ref);
2418 Insert_Before_And_Analyze (N, Decl);
2419 Set_Actual_Subtype (Id, Defining_Identifier (Decl));
2420 end;
2421 else
2422 Set_Actual_Subtype (Id, T);
2423 end if;
2424
2425 if Present (E) then
2426 Error_Msg_N
2427 ("initialization not allowed for `IN OUT` formals", N);
2428 end if;
2429 end if;
2430
2431 if Has_Aspects (N) then
2432 Analyze_Aspect_Specifications (N, Id);
2433 end if;
2434 end Analyze_Formal_Object_Declaration;
2435
2436 ----------------------------------------------
2437 -- Analyze_Formal_Ordinary_Fixed_Point_Type --
2438 ----------------------------------------------
2439
2440 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
2441 (T : Entity_Id;
2442 Def : Node_Id)
2443 is
2444 Loc : constant Source_Ptr := Sloc (Def);
2445 Base : constant Entity_Id :=
2446 New_Internal_Entity
2447 (E_Ordinary_Fixed_Point_Type, Current_Scope,
2448 Sloc (Defining_Identifier (Parent (Def))), 'G');
2449
2450 begin
2451 -- The semantic attributes are set for completeness only, their values
2452 -- will never be used, since all properties of the type are non-static.
2453
2454 Enter_Name (T);
2455 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
2456 Set_Etype (T, Base);
2457 Set_Size_Info (T, Standard_Integer);
2458 Set_RM_Size (T, RM_Size (Standard_Integer));
2459 Set_Small_Value (T, Ureal_1);
2460 Set_Delta_Value (T, Ureal_1);
2461 Set_Scalar_Range (T,
2462 Make_Range (Loc,
2463 Low_Bound => Make_Real_Literal (Loc, Ureal_1),
2464 High_Bound => Make_Real_Literal (Loc, Ureal_1)));
2465 Set_Is_Constrained (T);
2466
2467 Set_Is_Generic_Type (Base);
2468 Set_Etype (Base, Base);
2469 Set_Size_Info (Base, Standard_Integer);
2470 Set_RM_Size (Base, RM_Size (Standard_Integer));
2471 Set_Small_Value (Base, Ureal_1);
2472 Set_Delta_Value (Base, Ureal_1);
2473 Set_Scalar_Range (Base, Scalar_Range (T));
2474 Set_Parent (Base, Parent (Def));
2475
2476 Check_Restriction (No_Fixed_Point, Def);
2477 end Analyze_Formal_Ordinary_Fixed_Point_Type;
2478
2479 ----------------------------------------
2480 -- Analyze_Formal_Package_Declaration --
2481 ----------------------------------------
2482
2483 procedure Analyze_Formal_Package_Declaration (N : Node_Id) is
2484 Gen_Id : constant Node_Id := Name (N);
2485 Loc : constant Source_Ptr := Sloc (N);
2486 Pack_Id : constant Entity_Id := Defining_Identifier (N);
2487 Formal : Entity_Id;
2488 Gen_Decl : Node_Id;
2489 Gen_Unit : Entity_Id;
2490 Renaming : Node_Id;
2491
2492 Vis_Prims_List : Elist_Id := No_Elist;
2493 -- List of primitives made temporarily visible in the instantiation
2494 -- to match the visibility of the formal type.
2495
2496 function Build_Local_Package return Node_Id;
2497 -- The formal package is rewritten so that its parameters are replaced
2498 -- with corresponding declarations. For parameters with bona fide
2499 -- associations these declarations are created by Analyze_Associations
2500 -- as for a regular instantiation. For boxed parameters, we preserve
2501 -- the formal declarations and analyze them, in order to introduce
2502 -- entities of the right kind in the environment of the formal.
2503
2504 -------------------------
2505 -- Build_Local_Package --
2506 -------------------------
2507
2508 function Build_Local_Package return Node_Id is
2509 Decls : List_Id;
2510 Pack_Decl : Node_Id;
2511
2512 begin
2513 -- Within the formal, the name of the generic package is a renaming
2514 -- of the formal (as for a regular instantiation).
2515
2516 Pack_Decl :=
2517 Make_Package_Declaration (Loc,
2518 Specification =>
2519 Copy_Generic_Node
2520 (Specification (Original_Node (Gen_Decl)),
2521 Empty, Instantiating => True));
2522
2523 Renaming :=
2524 Make_Package_Renaming_Declaration (Loc,
2525 Defining_Unit_Name =>
2526 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
2527 Name => New_Occurrence_Of (Formal, Loc));
2528
2529 if Nkind (Gen_Id) = N_Identifier
2530 and then Chars (Gen_Id) = Chars (Pack_Id)
2531 then
2532 Error_Msg_NE
2533 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
2534 end if;
2535
2536 -- If the formal is declared with a box, or with an others choice,
2537 -- create corresponding declarations for all entities in the formal
2538 -- part, so that names with the proper types are available in the
2539 -- specification of the formal package.
2540
2541 -- On the other hand, if there are no associations, then all the
2542 -- formals must have defaults, and this will be checked by the
2543 -- call to Analyze_Associations.
2544
2545 if Box_Present (N)
2546 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2547 then
2548 declare
2549 Formal_Decl : Node_Id;
2550
2551 begin
2552 -- TBA : for a formal package, need to recurse ???
2553
2554 Decls := New_List;
2555 Formal_Decl :=
2556 First
2557 (Generic_Formal_Declarations (Original_Node (Gen_Decl)));
2558 while Present (Formal_Decl) loop
2559 Append_To
2560 (Decls, Copy_Generic_Node (Formal_Decl, Empty, True));
2561 Next (Formal_Decl);
2562 end loop;
2563 end;
2564
2565 -- If generic associations are present, use Analyze_Associations to
2566 -- create the proper renaming declarations.
2567
2568 else
2569 declare
2570 Act_Tree : constant Node_Id :=
2571 Copy_Generic_Node
2572 (Original_Node (Gen_Decl), Empty,
2573 Instantiating => True);
2574
2575 begin
2576 Generic_Renamings.Set_Last (0);
2577 Generic_Renamings_HTable.Reset;
2578 Instantiation_Node := N;
2579
2580 Decls :=
2581 Analyze_Associations
2582 (I_Node => Original_Node (N),
2583 Formals => Generic_Formal_Declarations (Act_Tree),
2584 F_Copy => Generic_Formal_Declarations (Gen_Decl));
2585
2586 Vis_Prims_List := Check_Hidden_Primitives (Decls);
2587 end;
2588 end if;
2589
2590 Append (Renaming, To => Decls);
2591
2592 -- Add generated declarations ahead of local declarations in
2593 -- the package.
2594
2595 if No (Visible_Declarations (Specification (Pack_Decl))) then
2596 Set_Visible_Declarations (Specification (Pack_Decl), Decls);
2597 else
2598 Insert_List_Before
2599 (First (Visible_Declarations (Specification (Pack_Decl))),
2600 Decls);
2601 end if;
2602
2603 return Pack_Decl;
2604 end Build_Local_Package;
2605
2606 -- Local variables
2607
2608 Save_IPSM : constant Boolean := Ignore_Pragma_SPARK_Mode;
2609 -- Save flag Ignore_Pragma_SPARK_Mode for restore on exit
2610
2611 Associations : Boolean := True;
2612 New_N : Node_Id;
2613 Parent_Installed : Boolean := False;
2614 Parent_Instance : Entity_Id;
2615 Renaming_In_Par : Entity_Id;
2616
2617 -- Start of processing for Analyze_Formal_Package_Declaration
2618
2619 begin
2620 Check_Text_IO_Special_Unit (Gen_Id);
2621
2622 Init_Env;
2623 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
2624 Gen_Unit := Entity (Gen_Id);
2625
2626 -- Check for a formal package that is a package renaming
2627
2628 if Present (Renamed_Object (Gen_Unit)) then
2629
2630 -- Indicate that unit is used, before replacing it with renamed
2631 -- entity for use below.
2632
2633 if In_Extended_Main_Source_Unit (N) then
2634 Set_Is_Instantiated (Gen_Unit);
2635 Generate_Reference (Gen_Unit, N);
2636 end if;
2637
2638 Gen_Unit := Renamed_Object (Gen_Unit);
2639 end if;
2640
2641 if Ekind (Gen_Unit) /= E_Generic_Package then
2642 Error_Msg_N ("expect generic package name", Gen_Id);
2643 Restore_Env;
2644 goto Leave;
2645
2646 elsif Gen_Unit = Current_Scope then
2647 Error_Msg_N
2648 ("generic package cannot be used as a formal package of itself",
2649 Gen_Id);
2650 Restore_Env;
2651 goto Leave;
2652
2653 elsif In_Open_Scopes (Gen_Unit) then
2654 if Is_Compilation_Unit (Gen_Unit)
2655 and then Is_Child_Unit (Current_Scope)
2656 then
2657 -- Special-case the error when the formal is a parent, and
2658 -- continue analysis to minimize cascaded errors.
2659
2660 Error_Msg_N
2661 ("generic parent cannot be used as formal package of a child "
2662 & "unit", Gen_Id);
2663
2664 else
2665 Error_Msg_N
2666 ("generic package cannot be used as a formal package within "
2667 & "itself", Gen_Id);
2668 Restore_Env;
2669 goto Leave;
2670 end if;
2671 end if;
2672
2673 -- Check that name of formal package does not hide name of generic,
2674 -- or its leading prefix. This check must be done separately because
2675 -- the name of the generic has already been analyzed.
2676
2677 declare
2678 Gen_Name : Entity_Id;
2679
2680 begin
2681 Gen_Name := Gen_Id;
2682 while Nkind (Gen_Name) = N_Expanded_Name loop
2683 Gen_Name := Prefix (Gen_Name);
2684 end loop;
2685
2686 if Chars (Gen_Name) = Chars (Pack_Id) then
2687 Error_Msg_NE
2688 ("& is hidden within declaration of formal package",
2689 Gen_Id, Gen_Name);
2690 end if;
2691 end;
2692
2693 if Box_Present (N)
2694 or else No (Generic_Associations (N))
2695 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2696 then
2697 Associations := False;
2698 end if;
2699
2700 -- If there are no generic associations, the generic parameters appear
2701 -- as local entities and are instantiated like them. We copy the generic
2702 -- package declaration as if it were an instantiation, and analyze it
2703 -- like a regular package, except that we treat the formals as
2704 -- additional visible components.
2705
2706 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
2707
2708 if In_Extended_Main_Source_Unit (N) then
2709 Set_Is_Instantiated (Gen_Unit);
2710 Generate_Reference (Gen_Unit, N);
2711 end if;
2712
2713 Formal := New_Copy (Pack_Id);
2714 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
2715
2716 -- Make local generic without formals. The formals will be replaced with
2717 -- internal declarations.
2718
2719 begin
2720 New_N := Build_Local_Package;
2721
2722 -- If there are errors in the parameter list, Analyze_Associations
2723 -- raises Instantiation_Error. Patch the declaration to prevent further
2724 -- exception propagation.
2725
2726 exception
2727 when Instantiation_Error =>
2728 Enter_Name (Formal);
2729 Set_Ekind (Formal, E_Variable);
2730 Set_Etype (Formal, Any_Type);
2731 Restore_Hidden_Primitives (Vis_Prims_List);
2732
2733 if Parent_Installed then
2734 Remove_Parent;
2735 end if;
2736
2737 goto Leave;
2738 end;
2739
2740 Rewrite (N, New_N);
2741 Set_Defining_Unit_Name (Specification (New_N), Formal);
2742 Set_Generic_Parent (Specification (N), Gen_Unit);
2743 Set_Instance_Env (Gen_Unit, Formal);
2744 Set_Is_Generic_Instance (Formal);
2745
2746 Enter_Name (Formal);
2747 Set_Ekind (Formal, E_Package);
2748 Set_Etype (Formal, Standard_Void_Type);
2749 Set_Inner_Instances (Formal, New_Elmt_List);
2750 Push_Scope (Formal);
2751
2752 -- Manually set the SPARK_Mode from the context because the package
2753 -- declaration is never analyzed.
2754
2755 Set_SPARK_Pragma (Formal, SPARK_Mode_Pragma);
2756 Set_SPARK_Aux_Pragma (Formal, SPARK_Mode_Pragma);
2757 Set_SPARK_Pragma_Inherited (Formal);
2758 Set_SPARK_Aux_Pragma_Inherited (Formal);
2759
2760 if Is_Child_Unit (Gen_Unit) and then Parent_Installed then
2761
2762 -- Similarly, we have to make the name of the formal visible in the
2763 -- parent instance, to resolve properly fully qualified names that
2764 -- may appear in the generic unit. The parent instance has been
2765 -- placed on the scope stack ahead of the current scope.
2766
2767 Parent_Instance := Scope_Stack.Table (Scope_Stack.Last - 1).Entity;
2768
2769 Renaming_In_Par :=
2770 Make_Defining_Identifier (Loc, Chars (Gen_Unit));
2771 Set_Ekind (Renaming_In_Par, E_Package);
2772 Set_Etype (Renaming_In_Par, Standard_Void_Type);
2773 Set_Scope (Renaming_In_Par, Parent_Instance);
2774 Set_Parent (Renaming_In_Par, Parent (Formal));
2775 Set_Renamed_Object (Renaming_In_Par, Formal);
2776 Append_Entity (Renaming_In_Par, Parent_Instance);
2777 end if;
2778
2779 -- A formal package declaration behaves as a package instantiation with
2780 -- respect to SPARK_Mode "off". If the annotation is "off" or altogether
2781 -- missing, set the global flag which signals Analyze_Pragma to ingnore
2782 -- all SPARK_Mode pragmas within the generic_package_name.
2783
2784 if SPARK_Mode /= On then
2785 Ignore_Pragma_SPARK_Mode := True;
2786 end if;
2787
2788 Analyze (Specification (N));
2789
2790 -- The formals for which associations are provided are not visible
2791 -- outside of the formal package. The others are still declared by a
2792 -- formal parameter declaration.
2793
2794 -- If there are no associations, the only local entity to hide is the
2795 -- generated package renaming itself.
2796
2797 declare
2798 E : Entity_Id;
2799
2800 begin
2801 E := First_Entity (Formal);
2802 while Present (E) loop
2803 if Associations and then not Is_Generic_Formal (E) then
2804 Set_Is_Hidden (E);
2805 end if;
2806
2807 if Ekind (E) = E_Package and then Renamed_Entity (E) = Formal then
2808 Set_Is_Hidden (E);
2809 exit;
2810 end if;
2811
2812 Next_Entity (E);
2813 end loop;
2814 end;
2815
2816 End_Package_Scope (Formal);
2817 Restore_Hidden_Primitives (Vis_Prims_List);
2818
2819 if Parent_Installed then
2820 Remove_Parent;
2821 end if;
2822
2823 Restore_Env;
2824
2825 -- Inside the generic unit, the formal package is a regular package, but
2826 -- no body is needed for it. Note that after instantiation, the defining
2827 -- unit name we need is in the new tree and not in the original (see
2828 -- Package_Instantiation). A generic formal package is an instance, and
2829 -- can be used as an actual for an inner instance.
2830
2831 Set_Has_Completion (Formal, True);
2832
2833 -- Add semantic information to the original defining identifier for ASIS
2834 -- use.
2835
2836 Set_Ekind (Pack_Id, E_Package);
2837 Set_Etype (Pack_Id, Standard_Void_Type);
2838 Set_Scope (Pack_Id, Scope (Formal));
2839 Set_Has_Completion (Pack_Id, True);
2840
2841 <<Leave>>
2842 if Has_Aspects (N) then
2843 Analyze_Aspect_Specifications (N, Pack_Id);
2844 end if;
2845
2846 Ignore_Pragma_SPARK_Mode := Save_IPSM;
2847 end Analyze_Formal_Package_Declaration;
2848
2849 ---------------------------------
2850 -- Analyze_Formal_Private_Type --
2851 ---------------------------------
2852
2853 procedure Analyze_Formal_Private_Type
2854 (N : Node_Id;
2855 T : Entity_Id;
2856 Def : Node_Id)
2857 is
2858 begin
2859 New_Private_Type (N, T, Def);
2860
2861 -- Set the size to an arbitrary but legal value
2862
2863 Set_Size_Info (T, Standard_Integer);
2864 Set_RM_Size (T, RM_Size (Standard_Integer));
2865 end Analyze_Formal_Private_Type;
2866
2867 ------------------------------------
2868 -- Analyze_Formal_Incomplete_Type --
2869 ------------------------------------
2870
2871 procedure Analyze_Formal_Incomplete_Type
2872 (T : Entity_Id;
2873 Def : Node_Id)
2874 is
2875 begin
2876 Enter_Name (T);
2877 Set_Ekind (T, E_Incomplete_Type);
2878 Set_Etype (T, T);
2879 Set_Private_Dependents (T, New_Elmt_List);
2880
2881 if Tagged_Present (Def) then
2882 Set_Is_Tagged_Type (T);
2883 Make_Class_Wide_Type (T);
2884 Set_Direct_Primitive_Operations (T, New_Elmt_List);
2885 end if;
2886 end Analyze_Formal_Incomplete_Type;
2887
2888 ----------------------------------------
2889 -- Analyze_Formal_Signed_Integer_Type --
2890 ----------------------------------------
2891
2892 procedure Analyze_Formal_Signed_Integer_Type
2893 (T : Entity_Id;
2894 Def : Node_Id)
2895 is
2896 Base : constant Entity_Id :=
2897 New_Internal_Entity
2898 (E_Signed_Integer_Type,
2899 Current_Scope,
2900 Sloc (Defining_Identifier (Parent (Def))), 'G');
2901
2902 begin
2903 Enter_Name (T);
2904
2905 Set_Ekind (T, E_Signed_Integer_Subtype);
2906 Set_Etype (T, Base);
2907 Set_Size_Info (T, Standard_Integer);
2908 Set_RM_Size (T, RM_Size (Standard_Integer));
2909 Set_Scalar_Range (T, Scalar_Range (Standard_Integer));
2910 Set_Is_Constrained (T);
2911
2912 Set_Is_Generic_Type (Base);
2913 Set_Size_Info (Base, Standard_Integer);
2914 Set_RM_Size (Base, RM_Size (Standard_Integer));
2915 Set_Etype (Base, Base);
2916 Set_Scalar_Range (Base, Scalar_Range (Standard_Integer));
2917 Set_Parent (Base, Parent (Def));
2918 end Analyze_Formal_Signed_Integer_Type;
2919
2920 -------------------------------------------
2921 -- Analyze_Formal_Subprogram_Declaration --
2922 -------------------------------------------
2923
2924 procedure Analyze_Formal_Subprogram_Declaration (N : Node_Id) is
2925 Spec : constant Node_Id := Specification (N);
2926 Def : constant Node_Id := Default_Name (N);
2927 Nam : constant Entity_Id := Defining_Unit_Name (Spec);
2928 Subp : Entity_Id;
2929
2930 begin
2931 if Nam = Error then
2932 return;
2933 end if;
2934
2935 if Nkind (Nam) = N_Defining_Program_Unit_Name then
2936 Error_Msg_N ("name of formal subprogram must be a direct name", Nam);
2937 goto Leave;
2938 end if;
2939
2940 Analyze_Subprogram_Declaration (N);
2941 Set_Is_Formal_Subprogram (Nam);
2942 Set_Has_Completion (Nam);
2943
2944 if Nkind (N) = N_Formal_Abstract_Subprogram_Declaration then
2945 Set_Is_Abstract_Subprogram (Nam);
2946
2947 Set_Is_Dispatching_Operation (Nam);
2948
2949 -- A formal abstract procedure cannot have a null default
2950 -- (RM 12.6(4.1/2)).
2951
2952 if Nkind (Spec) = N_Procedure_Specification
2953 and then Null_Present (Spec)
2954 then
2955 Error_Msg_N
2956 ("a formal abstract subprogram cannot default to null", Spec);
2957 end if;
2958
2959 declare
2960 Ctrl_Type : constant Entity_Id := Find_Dispatching_Type (Nam);
2961 begin
2962 if No (Ctrl_Type) then
2963 Error_Msg_N
2964 ("abstract formal subprogram must have a controlling type",
2965 N);
2966
2967 elsif Ada_Version >= Ada_2012
2968 and then Is_Incomplete_Type (Ctrl_Type)
2969 then
2970 Error_Msg_NE
2971 ("controlling type of abstract formal subprogram cannot "
2972 & "be incomplete type", N, Ctrl_Type);
2973
2974 else
2975 Check_Controlling_Formals (Ctrl_Type, Nam);
2976 end if;
2977 end;
2978 end if;
2979
2980 -- Default name is resolved at the point of instantiation
2981
2982 if Box_Present (N) then
2983 null;
2984
2985 -- Else default is bound at the point of generic declaration
2986
2987 elsif Present (Def) then
2988 if Nkind (Def) = N_Operator_Symbol then
2989 Find_Direct_Name (Def);
2990
2991 elsif Nkind (Def) /= N_Attribute_Reference then
2992 Analyze (Def);
2993
2994 else
2995 -- For an attribute reference, analyze the prefix and verify
2996 -- that it has the proper profile for the subprogram.
2997
2998 Analyze (Prefix (Def));
2999 Valid_Default_Attribute (Nam, Def);
3000 goto Leave;
3001 end if;
3002
3003 -- Default name may be overloaded, in which case the interpretation
3004 -- with the correct profile must be selected, as for a renaming.
3005 -- If the definition is an indexed component, it must denote a
3006 -- member of an entry family. If it is a selected component, it
3007 -- can be a protected operation.
3008
3009 if Etype (Def) = Any_Type then
3010 goto Leave;
3011
3012 elsif Nkind (Def) = N_Selected_Component then
3013 if not Is_Overloadable (Entity (Selector_Name (Def))) then
3014 Error_Msg_N ("expect valid subprogram name as default", Def);
3015 end if;
3016
3017 elsif Nkind (Def) = N_Indexed_Component then
3018 if Is_Entity_Name (Prefix (Def)) then
3019 if Ekind (Entity (Prefix (Def))) /= E_Entry_Family then
3020 Error_Msg_N ("expect valid subprogram name as default", Def);
3021 end if;
3022
3023 elsif Nkind (Prefix (Def)) = N_Selected_Component then
3024 if Ekind (Entity (Selector_Name (Prefix (Def)))) /=
3025 E_Entry_Family
3026 then
3027 Error_Msg_N ("expect valid subprogram name as default", Def);
3028 end if;
3029
3030 else
3031 Error_Msg_N ("expect valid subprogram name as default", Def);
3032 goto Leave;
3033 end if;
3034
3035 elsif Nkind (Def) = N_Character_Literal then
3036
3037 -- Needs some type checks: subprogram should be parameterless???
3038
3039 Resolve (Def, (Etype (Nam)));
3040
3041 elsif not Is_Entity_Name (Def)
3042 or else not Is_Overloadable (Entity (Def))
3043 then
3044 Error_Msg_N ("expect valid subprogram name as default", Def);
3045 goto Leave;
3046
3047 elsif not Is_Overloaded (Def) then
3048 Subp := Entity (Def);
3049
3050 if Subp = Nam then
3051 Error_Msg_N ("premature usage of formal subprogram", Def);
3052
3053 elsif not Entity_Matches_Spec (Subp, Nam) then
3054 Error_Msg_N ("no visible entity matches specification", Def);
3055 end if;
3056
3057 -- More than one interpretation, so disambiguate as for a renaming
3058
3059 else
3060 declare
3061 I : Interp_Index;
3062 I1 : Interp_Index := 0;
3063 It : Interp;
3064 It1 : Interp;
3065
3066 begin
3067 Subp := Any_Id;
3068 Get_First_Interp (Def, I, It);
3069 while Present (It.Nam) loop
3070 if Entity_Matches_Spec (It.Nam, Nam) then
3071 if Subp /= Any_Id then
3072 It1 := Disambiguate (Def, I1, I, Etype (Subp));
3073
3074 if It1 = No_Interp then
3075 Error_Msg_N ("ambiguous default subprogram", Def);
3076 else
3077 Subp := It1.Nam;
3078 end if;
3079
3080 exit;
3081
3082 else
3083 I1 := I;
3084 Subp := It.Nam;
3085 end if;
3086 end if;
3087
3088 Get_Next_Interp (I, It);
3089 end loop;
3090 end;
3091
3092 if Subp /= Any_Id then
3093
3094 -- Subprogram found, generate reference to it
3095
3096 Set_Entity (Def, Subp);
3097 Generate_Reference (Subp, Def);
3098
3099 if Subp = Nam then
3100 Error_Msg_N ("premature usage of formal subprogram", Def);
3101
3102 elsif Ekind (Subp) /= E_Operator then
3103 Check_Mode_Conformant (Subp, Nam);
3104 end if;
3105
3106 else
3107 Error_Msg_N ("no visible subprogram matches specification", N);
3108 end if;
3109 end if;
3110 end if;
3111
3112 <<Leave>>
3113 if Has_Aspects (N) then
3114 Analyze_Aspect_Specifications (N, Nam);
3115 end if;
3116
3117 end Analyze_Formal_Subprogram_Declaration;
3118
3119 -------------------------------------
3120 -- Analyze_Formal_Type_Declaration --
3121 -------------------------------------
3122
3123 procedure Analyze_Formal_Type_Declaration (N : Node_Id) is
3124 Def : constant Node_Id := Formal_Type_Definition (N);
3125 T : Entity_Id;
3126
3127 begin
3128 T := Defining_Identifier (N);
3129
3130 if Present (Discriminant_Specifications (N))
3131 and then Nkind (Def) /= N_Formal_Private_Type_Definition
3132 then
3133 Error_Msg_N
3134 ("discriminants not allowed for this formal type", T);
3135 end if;
3136
3137 -- Enter the new name, and branch to specific routine
3138
3139 case Nkind (Def) is
3140 when N_Formal_Private_Type_Definition =>
3141 Analyze_Formal_Private_Type (N, T, Def);
3142
3143 when N_Formal_Derived_Type_Definition =>
3144 Analyze_Formal_Derived_Type (N, T, Def);
3145
3146 when N_Formal_Incomplete_Type_Definition =>
3147 Analyze_Formal_Incomplete_Type (T, Def);
3148
3149 when N_Formal_Discrete_Type_Definition =>
3150 Analyze_Formal_Discrete_Type (T, Def);
3151
3152 when N_Formal_Signed_Integer_Type_Definition =>
3153 Analyze_Formal_Signed_Integer_Type (T, Def);
3154
3155 when N_Formal_Modular_Type_Definition =>
3156 Analyze_Formal_Modular_Type (T, Def);
3157
3158 when N_Formal_Floating_Point_Definition =>
3159 Analyze_Formal_Floating_Type (T, Def);
3160
3161 when N_Formal_Ordinary_Fixed_Point_Definition =>
3162 Analyze_Formal_Ordinary_Fixed_Point_Type (T, Def);
3163
3164 when N_Formal_Decimal_Fixed_Point_Definition =>
3165 Analyze_Formal_Decimal_Fixed_Point_Type (T, Def);
3166
3167 when N_Array_Type_Definition =>
3168 Analyze_Formal_Array_Type (T, Def);
3169
3170 when N_Access_Function_Definition
3171 | N_Access_Procedure_Definition
3172 | N_Access_To_Object_Definition
3173 =>
3174 Analyze_Generic_Access_Type (T, Def);
3175
3176 -- Ada 2005: a interface declaration is encoded as an abstract
3177 -- record declaration or a abstract type derivation.
3178
3179 when N_Record_Definition =>
3180 Analyze_Formal_Interface_Type (N, T, Def);
3181
3182 when N_Derived_Type_Definition =>
3183 Analyze_Formal_Derived_Interface_Type (N, T, Def);
3184
3185 when N_Error =>
3186 null;
3187
3188 when others =>
3189 raise Program_Error;
3190 end case;
3191
3192 Set_Is_Generic_Type (T);
3193
3194 if Has_Aspects (N) then
3195 Analyze_Aspect_Specifications (N, T);
3196 end if;
3197 end Analyze_Formal_Type_Declaration;
3198
3199 ------------------------------------
3200 -- Analyze_Function_Instantiation --
3201 ------------------------------------
3202
3203 procedure Analyze_Function_Instantiation (N : Node_Id) is
3204 begin
3205 Analyze_Subprogram_Instantiation (N, E_Function);
3206 end Analyze_Function_Instantiation;
3207
3208 ---------------------------------
3209 -- Analyze_Generic_Access_Type --
3210 ---------------------------------
3211
3212 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id) is
3213 begin
3214 Enter_Name (T);
3215
3216 if Nkind (Def) = N_Access_To_Object_Definition then
3217 Access_Type_Declaration (T, Def);
3218
3219 if Is_Incomplete_Or_Private_Type (Designated_Type (T))
3220 and then No (Full_View (Designated_Type (T)))
3221 and then not Is_Generic_Type (Designated_Type (T))
3222 then
3223 Error_Msg_N ("premature usage of incomplete type", Def);
3224
3225 elsif not Is_Entity_Name (Subtype_Indication (Def)) then
3226 Error_Msg_N
3227 ("only a subtype mark is allowed in a formal", Def);
3228 end if;
3229
3230 else
3231 Access_Subprogram_Declaration (T, Def);
3232 end if;
3233 end Analyze_Generic_Access_Type;
3234
3235 ---------------------------------
3236 -- Analyze_Generic_Formal_Part --
3237 ---------------------------------
3238
3239 procedure Analyze_Generic_Formal_Part (N : Node_Id) is
3240 Gen_Parm_Decl : Node_Id;
3241
3242 begin
3243 -- The generic formals are processed in the scope of the generic unit,
3244 -- where they are immediately visible. The scope is installed by the
3245 -- caller.
3246
3247 Gen_Parm_Decl := First (Generic_Formal_Declarations (N));
3248 while Present (Gen_Parm_Decl) loop
3249 Analyze (Gen_Parm_Decl);
3250 Next (Gen_Parm_Decl);
3251 end loop;
3252
3253 Generate_Reference_To_Generic_Formals (Current_Scope);
3254 end Analyze_Generic_Formal_Part;
3255
3256 ------------------------------------------
3257 -- Analyze_Generic_Package_Declaration --
3258 ------------------------------------------
3259
3260 procedure Analyze_Generic_Package_Declaration (N : Node_Id) is
3261 Loc : constant Source_Ptr := Sloc (N);
3262 Decls : constant List_Id :=
3263 Visible_Declarations (Specification (N));
3264 Decl : Node_Id;
3265 Id : Entity_Id;
3266 New_N : Node_Id;
3267 Renaming : Node_Id;
3268 Save_Parent : Node_Id;
3269
3270 begin
3271 Check_SPARK_05_Restriction ("generic is not allowed", N);
3272
3273 -- We introduce a renaming of the enclosing package, to have a usable
3274 -- entity as the prefix of an expanded name for a local entity of the
3275 -- form Par.P.Q, where P is the generic package. This is because a local
3276 -- entity named P may hide it, so that the usual visibility rules in
3277 -- the instance will not resolve properly.
3278
3279 Renaming :=
3280 Make_Package_Renaming_Declaration (Loc,
3281 Defining_Unit_Name =>
3282 Make_Defining_Identifier (Loc,
3283 Chars => New_External_Name (Chars (Defining_Entity (N)), "GH")),
3284 Name =>
3285 Make_Identifier (Loc, Chars (Defining_Entity (N))));
3286
3287 if Present (Decls) then
3288 Decl := First (Decls);
3289 while Present (Decl) and then Nkind (Decl) = N_Pragma loop
3290 Next (Decl);
3291 end loop;
3292
3293 if Present (Decl) then
3294 Insert_Before (Decl, Renaming);
3295 else
3296 Append (Renaming, Visible_Declarations (Specification (N)));
3297 end if;
3298
3299 else
3300 Set_Visible_Declarations (Specification (N), New_List (Renaming));
3301 end if;
3302
3303 -- Create copy of generic unit, and save for instantiation. If the unit
3304 -- is a child unit, do not copy the specifications for the parent, which
3305 -- are not part of the generic tree.
3306
3307 Save_Parent := Parent_Spec (N);
3308 Set_Parent_Spec (N, Empty);
3309
3310 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
3311 Set_Parent_Spec (New_N, Save_Parent);
3312 Rewrite (N, New_N);
3313
3314 -- Once the contents of the generic copy and the template are swapped,
3315 -- do the same for their respective aspect specifications.
3316
3317 Exchange_Aspects (N, New_N);
3318
3319 -- Collect all contract-related source pragmas found within the template
3320 -- and attach them to the contract of the package spec. This contract is
3321 -- used in the capture of global references within annotations.
3322
3323 Create_Generic_Contract (N);
3324
3325 Id := Defining_Entity (N);
3326 Generate_Definition (Id);
3327
3328 -- Expansion is not applied to generic units
3329
3330 Start_Generic;
3331
3332 Enter_Name (Id);
3333 Set_Ekind (Id, E_Generic_Package);
3334 Set_Etype (Id, Standard_Void_Type);
3335
3336 -- Analyze aspects now, so that generated pragmas appear in the
3337 -- declarations before building and analyzing the generic copy.
3338
3339 if Has_Aspects (N) then
3340 Analyze_Aspect_Specifications (N, Id);
3341 end if;
3342
3343 Push_Scope (Id);
3344 Enter_Generic_Scope (Id);
3345 Set_Inner_Instances (Id, New_Elmt_List);
3346
3347 Set_Categorization_From_Pragmas (N);
3348 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3349
3350 -- Link the declaration of the generic homonym in the generic copy to
3351 -- the package it renames, so that it is always resolved properly.
3352
3353 Set_Generic_Homonym (Id, Defining_Unit_Name (Renaming));
3354 Set_Entity (Associated_Node (Name (Renaming)), Id);
3355
3356 -- For a library unit, we have reconstructed the entity for the unit,
3357 -- and must reset it in the library tables.
3358
3359 if Nkind (Parent (N)) = N_Compilation_Unit then
3360 Set_Cunit_Entity (Current_Sem_Unit, Id);
3361 end if;
3362
3363 Analyze_Generic_Formal_Part (N);
3364
3365 -- After processing the generic formals, analysis proceeds as for a
3366 -- non-generic package.
3367
3368 Analyze (Specification (N));
3369
3370 Validate_Categorization_Dependency (N, Id);
3371
3372 End_Generic;
3373
3374 End_Package_Scope (Id);
3375 Exit_Generic_Scope (Id);
3376
3377 -- If the generic appears within a package unit, the body of that unit
3378 -- has to be present for instantiation and inlining.
3379
3380 if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Declaration then
3381 Set_Body_Needed_For_Inlining
3382 (Defining_Entity (Unit (Cunit (Current_Sem_Unit))));
3383 end if;
3384
3385 if Nkind (Parent (N)) /= N_Compilation_Unit then
3386 Move_Freeze_Nodes (Id, N, Visible_Declarations (Specification (N)));
3387 Move_Freeze_Nodes (Id, N, Private_Declarations (Specification (N)));
3388 Move_Freeze_Nodes (Id, N, Generic_Formal_Declarations (N));
3389
3390 else
3391 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
3392 Validate_RT_RAT_Component (N);
3393
3394 -- If this is a spec without a body, check that generic parameters
3395 -- are referenced.
3396
3397 if not Body_Required (Parent (N)) then
3398 Check_References (Id);
3399 end if;
3400 end if;
3401
3402 -- If there is a specified storage pool in the context, create an
3403 -- aspect on the package declaration, so that it is used in any
3404 -- instance that does not override it.
3405
3406 if Present (Default_Pool) then
3407 declare
3408 ASN : Node_Id;
3409
3410 begin
3411 ASN :=
3412 Make_Aspect_Specification (Loc,
3413 Identifier => Make_Identifier (Loc, Name_Default_Storage_Pool),
3414 Expression => New_Copy (Default_Pool));
3415
3416 if No (Aspect_Specifications (Specification (N))) then
3417 Set_Aspect_Specifications (Specification (N), New_List (ASN));
3418 else
3419 Append (ASN, Aspect_Specifications (Specification (N)));
3420 end if;
3421 end;
3422 end if;
3423 end Analyze_Generic_Package_Declaration;
3424
3425 --------------------------------------------
3426 -- Analyze_Generic_Subprogram_Declaration --
3427 --------------------------------------------
3428
3429 procedure Analyze_Generic_Subprogram_Declaration (N : Node_Id) is
3430 Formals : List_Id;
3431 Id : Entity_Id;
3432 New_N : Node_Id;
3433 Result_Type : Entity_Id;
3434 Save_Parent : Node_Id;
3435 Spec : Node_Id;
3436 Typ : Entity_Id;
3437
3438 begin
3439 Check_SPARK_05_Restriction ("generic is not allowed", N);
3440
3441 -- Create copy of generic unit, and save for instantiation. If the unit
3442 -- is a child unit, do not copy the specifications for the parent, which
3443 -- are not part of the generic tree.
3444
3445 Save_Parent := Parent_Spec (N);
3446 Set_Parent_Spec (N, Empty);
3447
3448 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
3449 Set_Parent_Spec (New_N, Save_Parent);
3450 Rewrite (N, New_N);
3451
3452 -- Once the contents of the generic copy and the template are swapped,
3453 -- do the same for their respective aspect specifications.
3454
3455 Exchange_Aspects (N, New_N);
3456
3457 -- Collect all contract-related source pragmas found within the template
3458 -- and attach them to the contract of the subprogram spec. This contract
3459 -- is used in the capture of global references within annotations.
3460
3461 Create_Generic_Contract (N);
3462
3463 Spec := Specification (N);
3464 Id := Defining_Entity (Spec);
3465 Generate_Definition (Id);
3466
3467 if Nkind (Id) = N_Defining_Operator_Symbol then
3468 Error_Msg_N
3469 ("operator symbol not allowed for generic subprogram", Id);
3470 end if;
3471
3472 Start_Generic;
3473
3474 Enter_Name (Id);
3475 Set_Scope_Depth_Value (Id, Scope_Depth (Current_Scope) + 1);
3476
3477 -- Analyze the aspects of the generic copy to ensure that all generated
3478 -- pragmas (if any) perform their semantic effects.
3479
3480 if Has_Aspects (N) then
3481 Analyze_Aspect_Specifications (N, Id);
3482 end if;
3483
3484 Push_Scope (Id);
3485 Enter_Generic_Scope (Id);
3486 Set_Inner_Instances (Id, New_Elmt_List);
3487 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3488
3489 Analyze_Generic_Formal_Part (N);
3490
3491 Formals := Parameter_Specifications (Spec);
3492
3493 if Nkind (Spec) = N_Function_Specification then
3494 Set_Ekind (Id, E_Generic_Function);
3495 else
3496 Set_Ekind (Id, E_Generic_Procedure);
3497 end if;
3498
3499 if Present (Formals) then
3500 Process_Formals (Formals, Spec);
3501 end if;
3502
3503 if Nkind (Spec) = N_Function_Specification then
3504 if Nkind (Result_Definition (Spec)) = N_Access_Definition then
3505 Result_Type := Access_Definition (Spec, Result_Definition (Spec));
3506 Set_Etype (Id, Result_Type);
3507
3508 -- Check restriction imposed by AI05-073: a generic function
3509 -- cannot return an abstract type or an access to such.
3510
3511 -- This is a binding interpretation should it apply to earlier
3512 -- versions of Ada as well as Ada 2012???
3513
3514 if Is_Abstract_Type (Designated_Type (Result_Type))
3515 and then Ada_Version >= Ada_2012
3516 then
3517 Error_Msg_N
3518 ("generic function cannot have an access result "
3519 & "that designates an abstract type", Spec);
3520 end if;
3521
3522 else
3523 Find_Type (Result_Definition (Spec));
3524 Typ := Entity (Result_Definition (Spec));
3525
3526 if Is_Abstract_Type (Typ)
3527 and then Ada_Version >= Ada_2012
3528 then
3529 Error_Msg_N
3530 ("generic function cannot have abstract result type", Spec);
3531 end if;
3532
3533 -- If a null exclusion is imposed on the result type, then create
3534 -- a null-excluding itype (an access subtype) and use it as the
3535 -- function's Etype.
3536
3537 if Is_Access_Type (Typ)
3538 and then Null_Exclusion_Present (Spec)
3539 then
3540 Set_Etype (Id,
3541 Create_Null_Excluding_Itype
3542 (T => Typ,
3543 Related_Nod => Spec,
3544 Scope_Id => Defining_Unit_Name (Spec)));
3545 else
3546 Set_Etype (Id, Typ);
3547 end if;
3548 end if;
3549
3550 else
3551 Set_Etype (Id, Standard_Void_Type);
3552 end if;
3553
3554 -- For a library unit, we have reconstructed the entity for the unit,
3555 -- and must reset it in the library tables. We also make sure that
3556 -- Body_Required is set properly in the original compilation unit node.
3557
3558 if Nkind (Parent (N)) = N_Compilation_Unit then
3559 Set_Cunit_Entity (Current_Sem_Unit, Id);
3560 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
3561 end if;
3562
3563 -- If the generic appears within a package unit, the body of that unit
3564 -- has to be present for instantiation and inlining.
3565
3566 if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Declaration
3567 and then Unit_Requires_Body (Id)
3568 then
3569 Set_Body_Needed_For_Inlining
3570 (Defining_Entity (Unit (Cunit (Current_Sem_Unit))));
3571 end if;
3572
3573 Set_Categorization_From_Pragmas (N);
3574 Validate_Categorization_Dependency (N, Id);
3575
3576 -- Capture all global references that occur within the profile of the
3577 -- generic subprogram. Aspects are not part of this processing because
3578 -- they must be delayed. If processed now, Save_Global_References will
3579 -- destroy the Associated_Node links and prevent the capture of global
3580 -- references when the contract of the generic subprogram is analyzed.
3581
3582 Save_Global_References (Original_Node (N));
3583
3584 End_Generic;
3585 End_Scope;
3586 Exit_Generic_Scope (Id);
3587 Generate_Reference_To_Formals (Id);
3588
3589 List_Inherited_Pre_Post_Aspects (Id);
3590 end Analyze_Generic_Subprogram_Declaration;
3591
3592 -----------------------------------
3593 -- Analyze_Package_Instantiation --
3594 -----------------------------------
3595
3596 -- WARNING: This routine manages Ghost regions. Return statements must be
3597 -- replaced by gotos which jump to the end of the routine and restore the
3598 -- Ghost mode.
3599
3600 procedure Analyze_Package_Instantiation (N : Node_Id) is
3601 Loc : constant Source_Ptr := Sloc (N);
3602 Gen_Id : constant Node_Id := Name (N);
3603
3604 Act_Decl : Node_Id;
3605 Act_Decl_Name : Node_Id;
3606 Act_Decl_Id : Entity_Id;
3607 Act_Spec : Node_Id;
3608 Act_Tree : Node_Id;
3609
3610 Gen_Decl : Node_Id;
3611 Gen_Spec : Node_Id;
3612 Gen_Unit : Entity_Id;
3613
3614 Is_Actual_Pack : constant Boolean :=
3615 Is_Internal (Defining_Entity (N));
3616
3617 Env_Installed : Boolean := False;
3618 Parent_Installed : Boolean := False;
3619 Renaming_List : List_Id;
3620 Unit_Renaming : Node_Id;
3621 Needs_Body : Boolean;
3622 Inline_Now : Boolean := False;
3623 Has_Inline_Always : Boolean := False;
3624
3625 Save_IPSM : constant Boolean := Ignore_Pragma_SPARK_Mode;
3626 -- Save flag Ignore_Pragma_SPARK_Mode for restore on exit
3627
3628 Save_SM : constant SPARK_Mode_Type := SPARK_Mode;
3629 Save_SMP : constant Node_Id := SPARK_Mode_Pragma;
3630 -- Save the SPARK_Mode-related data for restore on exit
3631
3632 Save_Style_Check : constant Boolean := Style_Check;
3633 -- Save style check mode for restore on exit
3634
3635 procedure Delay_Descriptors (E : Entity_Id);
3636 -- Delay generation of subprogram descriptors for given entity
3637
3638 function Might_Inline_Subp return Boolean;
3639 -- If inlining is active and the generic contains inlined subprograms,
3640 -- we instantiate the body. This may cause superfluous instantiations,
3641 -- but it is simpler than detecting the need for the body at the point
3642 -- of inlining, when the context of the instance is not available.
3643
3644 -----------------------
3645 -- Delay_Descriptors --
3646 -----------------------
3647
3648 procedure Delay_Descriptors (E : Entity_Id) is
3649 begin
3650 if not Delay_Subprogram_Descriptors (E) then
3651 Set_Delay_Subprogram_Descriptors (E);
3652 Pending_Descriptor.Append (E);
3653 end if;
3654 end Delay_Descriptors;
3655
3656 -----------------------
3657 -- Might_Inline_Subp --
3658 -----------------------
3659
3660 function Might_Inline_Subp return Boolean is
3661 E : Entity_Id;
3662
3663 begin
3664 if not Inline_Processing_Required then
3665 return False;
3666
3667 else
3668 E := First_Entity (Gen_Unit);
3669 while Present (E) loop
3670 if Is_Subprogram (E) and then Is_Inlined (E) then
3671 -- Remember if there are any subprograms with Inline_Always
3672
3673 if Has_Pragma_Inline_Always (E) then
3674 Has_Inline_Always := True;
3675 end if;
3676
3677 return True;
3678 end if;
3679
3680 Next_Entity (E);
3681 end loop;
3682 end if;
3683
3684 return False;
3685 end Might_Inline_Subp;
3686
3687 -- Local declarations
3688
3689 Mode : Ghost_Mode_Type;
3690 Mode_Set : Boolean := False;
3691
3692 Vis_Prims_List : Elist_Id := No_Elist;
3693 -- List of primitives made temporarily visible in the instantiation
3694 -- to match the visibility of the formal type
3695
3696 -- Start of processing for Analyze_Package_Instantiation
3697
3698 begin
3699 Check_SPARK_05_Restriction ("generic is not allowed", N);
3700
3701 -- Very first thing: check for Text_IO special unit in case we are
3702 -- instantiating one of the children of [[Wide_]Wide_]Text_IO.
3703
3704 Check_Text_IO_Special_Unit (Name (N));
3705
3706 -- Make node global for error reporting
3707
3708 Instantiation_Node := N;
3709
3710 -- Case of instantiation of a generic package
3711
3712 if Nkind (N) = N_Package_Instantiation then
3713 Act_Decl_Id := New_Copy (Defining_Entity (N));
3714 Set_Comes_From_Source (Act_Decl_Id, True);
3715
3716 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name then
3717 Act_Decl_Name :=
3718 Make_Defining_Program_Unit_Name (Loc,
3719 Name =>
3720 New_Copy_Tree (Name (Defining_Unit_Name (N))),
3721 Defining_Identifier => Act_Decl_Id);
3722 else
3723 Act_Decl_Name := Act_Decl_Id;
3724 end if;
3725
3726 -- Case of instantiation of a formal package
3727
3728 else
3729 Act_Decl_Id := Defining_Identifier (N);
3730 Act_Decl_Name := Act_Decl_Id;
3731 end if;
3732
3733 Generate_Definition (Act_Decl_Id);
3734 Set_Ekind (Act_Decl_Id, E_Package);
3735
3736 -- Initialize list of incomplete actuals before analysis
3737
3738 Set_Incomplete_Actuals (Act_Decl_Id, New_Elmt_List);
3739
3740 Preanalyze_Actuals (N, Act_Decl_Id);
3741
3742 -- Turn off style checking in instances. If the check is enabled on the
3743 -- generic unit, a warning in an instance would just be noise. If not
3744 -- enabled on the generic, then a warning in an instance is just wrong.
3745 -- This must be done after analyzing the actuals, which do come from
3746 -- source and are subject to style checking.
3747
3748 Style_Check := False;
3749
3750 Init_Env;
3751 Env_Installed := True;
3752
3753 -- Reset renaming map for formal types. The mapping is established
3754 -- when analyzing the generic associations, but some mappings are
3755 -- inherited from formal packages of parent units, and these are
3756 -- constructed when the parents are installed.
3757
3758 Generic_Renamings.Set_Last (0);
3759 Generic_Renamings_HTable.Reset;
3760
3761 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
3762 Gen_Unit := Entity (Gen_Id);
3763
3764 -- A package instantiation is Ghost when it is subject to pragma Ghost
3765 -- or the generic template is Ghost. Set the mode now to ensure that
3766 -- any nodes generated during analysis and expansion are marked as
3767 -- Ghost.
3768
3769 Mark_And_Set_Ghost_Instantiation (N, Gen_Unit, Mode);
3770 Mode_Set := True;
3771
3772 -- Verify that it is the name of a generic package
3773
3774 -- A visibility glitch: if the instance is a child unit and the generic
3775 -- is the generic unit of a parent instance (i.e. both the parent and
3776 -- the child units are instances of the same package) the name now
3777 -- denotes the renaming within the parent, not the intended generic
3778 -- unit. See if there is a homonym that is the desired generic. The
3779 -- renaming declaration must be visible inside the instance of the
3780 -- child, but not when analyzing the name in the instantiation itself.
3781
3782 if Ekind (Gen_Unit) = E_Package
3783 and then Present (Renamed_Entity (Gen_Unit))
3784 and then In_Open_Scopes (Renamed_Entity (Gen_Unit))
3785 and then Is_Generic_Instance (Renamed_Entity (Gen_Unit))
3786 and then Present (Homonym (Gen_Unit))
3787 then
3788 Gen_Unit := Homonym (Gen_Unit);
3789 end if;
3790
3791 if Etype (Gen_Unit) = Any_Type then
3792 Restore_Env;
3793 goto Leave;
3794
3795 elsif Ekind (Gen_Unit) /= E_Generic_Package then
3796
3797 -- Ada 2005 (AI-50217): Cannot use instance in limited with_clause
3798
3799 if From_Limited_With (Gen_Unit) then
3800 Error_Msg_N
3801 ("cannot instantiate a limited withed package", Gen_Id);
3802 else
3803 Error_Msg_NE
3804 ("& is not the name of a generic package", Gen_Id, Gen_Unit);
3805 end if;
3806
3807 Restore_Env;
3808 goto Leave;
3809 end if;
3810
3811 if In_Extended_Main_Source_Unit (N) then
3812 Set_Is_Instantiated (Gen_Unit);
3813 Generate_Reference (Gen_Unit, N);
3814
3815 if Present (Renamed_Object (Gen_Unit)) then
3816 Set_Is_Instantiated (Renamed_Object (Gen_Unit));
3817 Generate_Reference (Renamed_Object (Gen_Unit), N);
3818 end if;
3819 end if;
3820
3821 if Nkind (Gen_Id) = N_Identifier
3822 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
3823 then
3824 Error_Msg_NE
3825 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
3826
3827 elsif Nkind (Gen_Id) = N_Expanded_Name
3828 and then Is_Child_Unit (Gen_Unit)
3829 and then Nkind (Prefix (Gen_Id)) = N_Identifier
3830 and then Chars (Act_Decl_Id) = Chars (Prefix (Gen_Id))
3831 then
3832 Error_Msg_N
3833 ("& is hidden within declaration of instance ", Prefix (Gen_Id));
3834 end if;
3835
3836 Set_Entity (Gen_Id, Gen_Unit);
3837
3838 -- If generic is a renaming, get original generic unit
3839
3840 if Present (Renamed_Object (Gen_Unit))
3841 and then Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Package
3842 then
3843 Gen_Unit := Renamed_Object (Gen_Unit);
3844 end if;
3845
3846 -- Verify that there are no circular instantiations
3847
3848 if In_Open_Scopes (Gen_Unit) then
3849 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
3850 Restore_Env;
3851 goto Leave;
3852
3853 elsif Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
3854 Error_Msg_Node_2 := Current_Scope;
3855 Error_Msg_NE
3856 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
3857 Circularity_Detected := True;
3858 Restore_Env;
3859 goto Leave;
3860
3861 else
3862 -- If the context of the instance is subject to SPARK_Mode "off" or
3863 -- the annotation is altogether missing, set the global flag which
3864 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
3865 -- the instance.
3866
3867 if SPARK_Mode /= On then
3868 Ignore_Pragma_SPARK_Mode := True;
3869 end if;
3870
3871 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
3872 Gen_Spec := Specification (Gen_Decl);
3873
3874 -- Initialize renamings map, for error checking, and the list that
3875 -- holds private entities whose views have changed between generic
3876 -- definition and instantiation. If this is the instance created to
3877 -- validate an actual package, the instantiation environment is that
3878 -- of the enclosing instance.
3879
3880 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
3881
3882 -- Copy original generic tree, to produce text for instantiation
3883
3884 Act_Tree :=
3885 Copy_Generic_Node
3886 (Original_Node (Gen_Decl), Empty, Instantiating => True);
3887
3888 Act_Spec := Specification (Act_Tree);
3889
3890 -- If this is the instance created to validate an actual package,
3891 -- only the formals matter, do not examine the package spec itself.
3892
3893 if Is_Actual_Pack then
3894 Set_Visible_Declarations (Act_Spec, New_List);
3895 Set_Private_Declarations (Act_Spec, New_List);
3896 end if;
3897
3898 Renaming_List :=
3899 Analyze_Associations
3900 (I_Node => N,
3901 Formals => Generic_Formal_Declarations (Act_Tree),
3902 F_Copy => Generic_Formal_Declarations (Gen_Decl));
3903
3904 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
3905
3906 Set_Instance_Env (Gen_Unit, Act_Decl_Id);
3907 Set_Defining_Unit_Name (Act_Spec, Act_Decl_Name);
3908 Set_Is_Generic_Instance (Act_Decl_Id);
3909 Set_Generic_Parent (Act_Spec, Gen_Unit);
3910
3911 -- References to the generic in its own declaration or its body are
3912 -- references to the instance. Add a renaming declaration for the
3913 -- generic unit itself. This declaration, as well as the renaming
3914 -- declarations for the generic formals, must remain private to the
3915 -- unit: the formals, because this is the language semantics, and
3916 -- the unit because its use is an artifact of the implementation.
3917
3918 Unit_Renaming :=
3919 Make_Package_Renaming_Declaration (Loc,
3920 Defining_Unit_Name =>
3921 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
3922 Name => New_Occurrence_Of (Act_Decl_Id, Loc));
3923
3924 Append (Unit_Renaming, Renaming_List);
3925
3926 -- The renaming declarations are the first local declarations of the
3927 -- new unit.
3928
3929 if Is_Non_Empty_List (Visible_Declarations (Act_Spec)) then
3930 Insert_List_Before
3931 (First (Visible_Declarations (Act_Spec)), Renaming_List);
3932 else
3933 Set_Visible_Declarations (Act_Spec, Renaming_List);
3934 end if;
3935
3936 Act_Decl := Make_Package_Declaration (Loc, Specification => Act_Spec);
3937
3938 -- Propagate the aspect specifications from the package declaration
3939 -- template to the instantiated version of the package declaration.
3940
3941 if Has_Aspects (Act_Tree) then
3942 Set_Aspect_Specifications (Act_Decl,
3943 New_Copy_List_Tree (Aspect_Specifications (Act_Tree)));
3944 end if;
3945
3946 -- The generic may have a generated Default_Storage_Pool aspect,
3947 -- set at the point of generic declaration. If the instance has
3948 -- that aspect, it overrides the one inherited from the generic.
3949
3950 if Has_Aspects (Gen_Spec) then
3951 if No (Aspect_Specifications (N)) then
3952 Set_Aspect_Specifications (N,
3953 (New_Copy_List_Tree
3954 (Aspect_Specifications (Gen_Spec))));
3955
3956 else
3957 declare
3958 ASN1, ASN2 : Node_Id;
3959
3960 begin
3961 ASN1 := First (Aspect_Specifications (N));
3962 while Present (ASN1) loop
3963 if Chars (Identifier (ASN1)) = Name_Default_Storage_Pool
3964 then
3965 -- If generic carries a default storage pool, remove
3966 -- it in favor of the instance one.
3967
3968 ASN2 := First (Aspect_Specifications (Gen_Spec));
3969 while Present (ASN2) loop
3970 if Chars (Identifier (ASN2)) =
3971 Name_Default_Storage_Pool
3972 then
3973 Remove (ASN2);
3974 exit;
3975 end if;
3976
3977 Next (ASN2);
3978 end loop;
3979 end if;
3980
3981 Next (ASN1);
3982 end loop;
3983
3984 Prepend_List_To (Aspect_Specifications (N),
3985 (New_Copy_List_Tree
3986 (Aspect_Specifications (Gen_Spec))));
3987 end;
3988 end if;
3989 end if;
3990
3991 -- Save the instantiation node, for subsequent instantiation of the
3992 -- body, if there is one and we are generating code for the current
3993 -- unit. Mark unit as having a body (avoids premature error message).
3994
3995 -- We instantiate the body if we are generating code, if we are
3996 -- generating cross-reference information, or if we are building
3997 -- trees for ASIS use or GNATprove use.
3998
3999 declare
4000 Enclosing_Body_Present : Boolean := False;
4001 -- If the generic unit is not a compilation unit, then a body may
4002 -- be present in its parent even if none is required. We create a
4003 -- tentative pending instantiation for the body, which will be
4004 -- discarded if none is actually present.
4005
4006 Scop : Entity_Id;
4007
4008 begin
4009 if Scope (Gen_Unit) /= Standard_Standard
4010 and then not Is_Child_Unit (Gen_Unit)
4011 then
4012 Scop := Scope (Gen_Unit);
4013 while Present (Scop) and then Scop /= Standard_Standard loop
4014 if Unit_Requires_Body (Scop) then
4015 Enclosing_Body_Present := True;
4016 exit;
4017
4018 elsif In_Open_Scopes (Scop)
4019 and then In_Package_Body (Scop)
4020 then
4021 Enclosing_Body_Present := True;
4022 exit;
4023 end if;
4024
4025 exit when Is_Compilation_Unit (Scop);
4026 Scop := Scope (Scop);
4027 end loop;
4028 end if;
4029
4030 -- If front-end inlining is enabled or there are any subprograms
4031 -- marked with Inline_Always, and this is a unit for which code
4032 -- will be generated, we instantiate the body at once.
4033
4034 -- This is done if the instance is not the main unit, and if the
4035 -- generic is not a child unit of another generic, to avoid scope
4036 -- problems and the reinstallation of parent instances.
4037
4038 if Expander_Active
4039 and then (not Is_Child_Unit (Gen_Unit)
4040 or else not Is_Generic_Unit (Scope (Gen_Unit)))
4041 and then Might_Inline_Subp
4042 and then not Is_Actual_Pack
4043 then
4044 if not Back_End_Inlining
4045 and then (Front_End_Inlining or else Has_Inline_Always)
4046 and then (Is_In_Main_Unit (N)
4047 or else In_Main_Context (Current_Scope))
4048 and then Nkind (Parent (N)) /= N_Compilation_Unit
4049 then
4050 Inline_Now := True;
4051
4052 -- In configurable_run_time mode we force the inlining of
4053 -- predefined subprograms marked Inline_Always, to minimize
4054 -- the use of the run-time library.
4055
4056 elsif Is_Predefined_File_Name
4057 (Unit_File_Name (Get_Source_Unit (Gen_Decl)))
4058 and then Configurable_Run_Time_Mode
4059 and then Nkind (Parent (N)) /= N_Compilation_Unit
4060 then
4061 Inline_Now := True;
4062 end if;
4063
4064 -- If the current scope is itself an instance within a child
4065 -- unit, there will be duplications in the scope stack, and the
4066 -- unstacking mechanism in Inline_Instance_Body will fail.
4067 -- This loses some rare cases of optimization, and might be
4068 -- improved some day, if we can find a proper abstraction for
4069 -- "the complete compilation context" that can be saved and
4070 -- restored. ???
4071
4072 if Is_Generic_Instance (Current_Scope) then
4073 declare
4074 Curr_Unit : constant Entity_Id :=
4075 Cunit_Entity (Current_Sem_Unit);
4076 begin
4077 if Curr_Unit /= Current_Scope
4078 and then Is_Child_Unit (Curr_Unit)
4079 then
4080 Inline_Now := False;
4081 end if;
4082 end;
4083 end if;
4084 end if;
4085
4086 Needs_Body :=
4087 (Unit_Requires_Body (Gen_Unit)
4088 or else Enclosing_Body_Present
4089 or else Present (Corresponding_Body (Gen_Decl)))
4090 and then (Is_In_Main_Unit (N) or else Might_Inline_Subp)
4091 and then not Is_Actual_Pack
4092 and then not Inline_Now
4093 and then (Operating_Mode = Generate_Code
4094
4095 -- Need comment for this check ???
4096
4097 or else (Operating_Mode = Check_Semantics
4098 and then (ASIS_Mode or GNATprove_Mode)));
4099
4100 -- If front-end inlining is enabled or there are any subprograms
4101 -- marked with Inline_Always, do not instantiate body when within
4102 -- a generic context.
4103
4104 if ((Front_End_Inlining or else Has_Inline_Always)
4105 and then not Expander_Active)
4106 or else Is_Generic_Unit (Cunit_Entity (Main_Unit))
4107 then
4108 Needs_Body := False;
4109 end if;
4110
4111 -- If the current context is generic, and the package being
4112 -- instantiated is declared within a formal package, there is no
4113 -- body to instantiate until the enclosing generic is instantiated
4114 -- and there is an actual for the formal package. If the formal
4115 -- package has parameters, we build a regular package instance for
4116 -- it, that precedes the original formal package declaration.
4117
4118 if In_Open_Scopes (Scope (Scope (Gen_Unit))) then
4119 declare
4120 Decl : constant Node_Id :=
4121 Original_Node
4122 (Unit_Declaration_Node (Scope (Gen_Unit)));
4123 begin
4124 if Nkind (Decl) = N_Formal_Package_Declaration
4125 or else (Nkind (Decl) = N_Package_Declaration
4126 and then Is_List_Member (Decl)
4127 and then Present (Next (Decl))
4128 and then
4129 Nkind (Next (Decl)) =
4130 N_Formal_Package_Declaration)
4131 then
4132 Needs_Body := False;
4133 end if;
4134 end;
4135 end if;
4136 end;
4137
4138 -- For RCI unit calling stubs, we omit the instance body if the
4139 -- instance is the RCI library unit itself.
4140
4141 -- However there is a special case for nested instances: in this case
4142 -- we do generate the instance body, as it might be required, e.g.
4143 -- because it provides stream attributes for some type used in the
4144 -- profile of a remote subprogram. This is consistent with 12.3(12),
4145 -- which indicates that the instance body occurs at the place of the
4146 -- instantiation, and thus is part of the RCI declaration, which is
4147 -- present on all client partitions (this is E.2.3(18)).
4148
4149 -- Note that AI12-0002 may make it illegal at some point to have
4150 -- stream attributes defined in an RCI unit, in which case this
4151 -- special case will become unnecessary. In the meantime, there
4152 -- is known application code in production that depends on this
4153 -- being possible, so we definitely cannot eliminate the body in
4154 -- the case of nested instances for the time being.
4155
4156 -- When we generate a nested instance body, calling stubs for any
4157 -- relevant subprogram will be be inserted immediately after the
4158 -- subprogram declarations, and will take precedence over the
4159 -- subsequent (original) body. (The stub and original body will be
4160 -- complete homographs, but this is permitted in an instance).
4161 -- (Could we do better and remove the original body???)
4162
4163 if Distribution_Stub_Mode = Generate_Caller_Stub_Body
4164 and then Comes_From_Source (N)
4165 and then Nkind (Parent (N)) = N_Compilation_Unit
4166 then
4167 Needs_Body := False;
4168 end if;
4169
4170 if Needs_Body then
4171
4172 -- Here is a defence against a ludicrous number of instantiations
4173 -- caused by a circular set of instantiation attempts.
4174
4175 if Pending_Instantiations.Last > Maximum_Instantiations then
4176 Error_Msg_Uint_1 := UI_From_Int (Maximum_Instantiations);
4177 Error_Msg_N ("too many instantiations, exceeds max of^", N);
4178 Error_Msg_N ("\limit can be changed using -gnateinn switch", N);
4179 raise Unrecoverable_Error;
4180 end if;
4181
4182 -- Indicate that the enclosing scopes contain an instantiation,
4183 -- and that cleanup actions should be delayed until after the
4184 -- instance body is expanded.
4185
4186 Check_Forward_Instantiation (Gen_Decl);
4187 if Nkind (N) = N_Package_Instantiation then
4188 declare
4189 Enclosing_Master : Entity_Id;
4190
4191 begin
4192 -- Loop to search enclosing masters
4193
4194 Enclosing_Master := Current_Scope;
4195 Scope_Loop : while Enclosing_Master /= Standard_Standard loop
4196 if Ekind (Enclosing_Master) = E_Package then
4197 if Is_Compilation_Unit (Enclosing_Master) then
4198 if In_Package_Body (Enclosing_Master) then
4199 Delay_Descriptors
4200 (Body_Entity (Enclosing_Master));
4201 else
4202 Delay_Descriptors
4203 (Enclosing_Master);
4204 end if;
4205
4206 exit Scope_Loop;
4207
4208 else
4209 Enclosing_Master := Scope (Enclosing_Master);
4210 end if;
4211
4212 elsif Is_Generic_Unit (Enclosing_Master)
4213 or else Ekind (Enclosing_Master) = E_Void
4214 then
4215 -- Cleanup actions will eventually be performed on the
4216 -- enclosing subprogram or package instance, if any.
4217 -- Enclosing scope is void in the formal part of a
4218 -- generic subprogram.
4219
4220 exit Scope_Loop;
4221
4222 else
4223 if Ekind (Enclosing_Master) = E_Entry
4224 and then
4225 Ekind (Scope (Enclosing_Master)) = E_Protected_Type
4226 then
4227 if not Expander_Active then
4228 exit Scope_Loop;
4229 else
4230 Enclosing_Master :=
4231 Protected_Body_Subprogram (Enclosing_Master);
4232 end if;
4233 end if;
4234
4235 Set_Delay_Cleanups (Enclosing_Master);
4236
4237 while Ekind (Enclosing_Master) = E_Block loop
4238 Enclosing_Master := Scope (Enclosing_Master);
4239 end loop;
4240
4241 if Is_Subprogram (Enclosing_Master) then
4242 Delay_Descriptors (Enclosing_Master);
4243
4244 elsif Is_Task_Type (Enclosing_Master) then
4245 declare
4246 TBP : constant Node_Id :=
4247 Get_Task_Body_Procedure
4248 (Enclosing_Master);
4249 begin
4250 if Present (TBP) then
4251 Delay_Descriptors (TBP);
4252 Set_Delay_Cleanups (TBP);
4253 end if;
4254 end;
4255 end if;
4256
4257 exit Scope_Loop;
4258 end if;
4259 end loop Scope_Loop;
4260 end;
4261
4262 -- Make entry in table
4263
4264 Add_Pending_Instantiation (N, Act_Decl);
4265 end if;
4266 end if;
4267
4268 Set_Categorization_From_Pragmas (Act_Decl);
4269
4270 if Parent_Installed then
4271 Hide_Current_Scope;
4272 end if;
4273
4274 Set_Instance_Spec (N, Act_Decl);
4275
4276 -- If not a compilation unit, insert the package declaration before
4277 -- the original instantiation node.
4278
4279 if Nkind (Parent (N)) /= N_Compilation_Unit then
4280 Mark_Rewrite_Insertion (Act_Decl);
4281 Insert_Before (N, Act_Decl);
4282
4283 if Has_Aspects (N) then
4284 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4285
4286 -- The pragma created for a Default_Storage_Pool aspect must
4287 -- appear ahead of the declarations in the instance spec.
4288 -- Analysis has placed it after the instance node, so remove
4289 -- it and reinsert it properly now.
4290
4291 declare
4292 ASN : constant Node_Id := First (Aspect_Specifications (N));
4293 A_Name : constant Name_Id := Chars (Identifier (ASN));
4294 Decl : Node_Id;
4295
4296 begin
4297 if A_Name = Name_Default_Storage_Pool then
4298 if No (Visible_Declarations (Act_Spec)) then
4299 Set_Visible_Declarations (Act_Spec, New_List);
4300 end if;
4301
4302 Decl := Next (N);
4303 while Present (Decl) loop
4304 if Nkind (Decl) = N_Pragma then
4305 Remove (Decl);
4306 Prepend (Decl, Visible_Declarations (Act_Spec));
4307 exit;
4308 end if;
4309
4310 Next (Decl);
4311 end loop;
4312 end if;
4313 end;
4314 end if;
4315
4316 Analyze (Act_Decl);
4317
4318 -- For an instantiation that is a compilation unit, place
4319 -- declaration on current node so context is complete for analysis
4320 -- (including nested instantiations). If this is the main unit,
4321 -- the declaration eventually replaces the instantiation node.
4322 -- If the instance body is created later, it replaces the
4323 -- instance node, and the declaration is attached to it
4324 -- (see Build_Instance_Compilation_Unit_Nodes).
4325
4326 else
4327 if Cunit_Entity (Current_Sem_Unit) = Defining_Entity (N) then
4328
4329 -- The entity for the current unit is the newly created one,
4330 -- and all semantic information is attached to it.
4331
4332 Set_Cunit_Entity (Current_Sem_Unit, Act_Decl_Id);
4333
4334 -- If this is the main unit, replace the main entity as well
4335
4336 if Current_Sem_Unit = Main_Unit then
4337 Main_Unit_Entity := Act_Decl_Id;
4338 end if;
4339 end if;
4340
4341 Set_Unit (Parent (N), Act_Decl);
4342 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
4343 Set_Package_Instantiation (Act_Decl_Id, N);
4344
4345 -- Process aspect specifications of the instance node, if any, to
4346 -- take into account categorization pragmas before analyzing the
4347 -- instance.
4348
4349 if Has_Aspects (N) then
4350 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4351 end if;
4352
4353 Analyze (Act_Decl);
4354 Set_Unit (Parent (N), N);
4355 Set_Body_Required (Parent (N), False);
4356
4357 -- We never need elaboration checks on instantiations, since by
4358 -- definition, the body instantiation is elaborated at the same
4359 -- time as the spec instantiation.
4360
4361 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
4362 Set_Kill_Elaboration_Checks (Act_Decl_Id);
4363 end if;
4364
4365 Check_Elab_Instantiation (N);
4366
4367 if ABE_Is_Certain (N) and then Needs_Body then
4368 Pending_Instantiations.Decrement_Last;
4369 end if;
4370
4371 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
4372
4373 Set_First_Private_Entity (Defining_Unit_Name (Unit_Renaming),
4374 First_Private_Entity (Act_Decl_Id));
4375
4376 -- If the instantiation will receive a body, the unit will be
4377 -- transformed into a package body, and receive its own elaboration
4378 -- entity. Otherwise, the nature of the unit is now a package
4379 -- declaration.
4380
4381 if Nkind (Parent (N)) = N_Compilation_Unit
4382 and then not Needs_Body
4383 then
4384 Rewrite (N, Act_Decl);
4385 end if;
4386
4387 if Present (Corresponding_Body (Gen_Decl))
4388 or else Unit_Requires_Body (Gen_Unit)
4389 then
4390 Set_Has_Completion (Act_Decl_Id);
4391 end if;
4392
4393 Check_Formal_Packages (Act_Decl_Id);
4394
4395 Restore_Hidden_Primitives (Vis_Prims_List);
4396 Restore_Private_Views (Act_Decl_Id);
4397
4398 Inherit_Context (Gen_Decl, N);
4399
4400 if Parent_Installed then
4401 Remove_Parent;
4402 end if;
4403
4404 Restore_Env;
4405 Env_Installed := False;
4406 end if;
4407
4408 Validate_Categorization_Dependency (N, Act_Decl_Id);
4409
4410 -- There used to be a check here to prevent instantiations in local
4411 -- contexts if the No_Local_Allocators restriction was active. This
4412 -- check was removed by a binding interpretation in AI-95-00130/07,
4413 -- but we retain the code for documentation purposes.
4414
4415 -- if Ekind (Act_Decl_Id) /= E_Void
4416 -- and then not Is_Library_Level_Entity (Act_Decl_Id)
4417 -- then
4418 -- Check_Restriction (No_Local_Allocators, N);
4419 -- end if;
4420
4421 if Inline_Now then
4422 Inline_Instance_Body (N, Gen_Unit, Act_Decl);
4423 end if;
4424
4425 -- The following is a tree patch for ASIS: ASIS needs separate nodes to
4426 -- be used as defining identifiers for a formal package and for the
4427 -- corresponding expanded package.
4428
4429 if Nkind (N) = N_Formal_Package_Declaration then
4430 Act_Decl_Id := New_Copy (Defining_Entity (N));
4431 Set_Comes_From_Source (Act_Decl_Id, True);
4432 Set_Is_Generic_Instance (Act_Decl_Id, False);
4433 Set_Defining_Identifier (N, Act_Decl_Id);
4434 end if;
4435
4436 Ignore_Pragma_SPARK_Mode := Save_IPSM;
4437 SPARK_Mode := Save_SM;
4438 SPARK_Mode_Pragma := Save_SMP;
4439 Style_Check := Save_Style_Check;
4440
4441 -- Check that if N is an instantiation of System.Dim_Float_IO or
4442 -- System.Dim_Integer_IO, the formal type has a dimension system.
4443
4444 if Nkind (N) = N_Package_Instantiation
4445 and then Is_Dim_IO_Package_Instantiation (N)
4446 then
4447 declare
4448 Assoc : constant Node_Id := First (Generic_Associations (N));
4449 begin
4450 if not Has_Dimension_System
4451 (Etype (Explicit_Generic_Actual_Parameter (Assoc)))
4452 then
4453 Error_Msg_N ("type with a dimension system expected", Assoc);
4454 end if;
4455 end;
4456 end if;
4457
4458 <<Leave>>
4459 if Has_Aspects (N) and then Nkind (Parent (N)) /= N_Compilation_Unit then
4460 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4461 end if;
4462
4463 if Mode_Set then
4464 Restore_Ghost_Mode (Mode);
4465 end if;
4466
4467 exception
4468 when Instantiation_Error =>
4469 if Parent_Installed then
4470 Remove_Parent;
4471 end if;
4472
4473 if Env_Installed then
4474 Restore_Env;
4475 end if;
4476
4477 Ignore_Pragma_SPARK_Mode := Save_IPSM;
4478 SPARK_Mode := Save_SM;
4479 SPARK_Mode_Pragma := Save_SMP;
4480 Style_Check := Save_Style_Check;
4481
4482 if Mode_Set then
4483 Restore_Ghost_Mode (Mode);
4484 end if;
4485 end Analyze_Package_Instantiation;
4486
4487 --------------------------
4488 -- Inline_Instance_Body --
4489 --------------------------
4490
4491 procedure Inline_Instance_Body
4492 (N : Node_Id;
4493 Gen_Unit : Entity_Id;
4494 Act_Decl : Node_Id)
4495 is
4496 Curr_Comp : constant Node_Id := Cunit (Current_Sem_Unit);
4497 Curr_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
4498 Gen_Comp : constant Entity_Id :=
4499 Cunit_Entity (Get_Source_Unit (Gen_Unit));
4500
4501 Save_SM : constant SPARK_Mode_Type := SPARK_Mode;
4502 Save_SMP : constant Node_Id := SPARK_Mode_Pragma;
4503 -- Save all SPARK_Mode-related attributes as removing enclosing scopes
4504 -- to provide a clean environment for analysis of the inlined body will
4505 -- eliminate any previously set SPARK_Mode.
4506
4507 Scope_Stack_Depth : constant Pos :=
4508 Scope_Stack.Last - Scope_Stack.First + 1;
4509
4510 Use_Clauses : array (1 .. Scope_Stack_Depth) of Node_Id;
4511 Instances : array (1 .. Scope_Stack_Depth) of Entity_Id;
4512 Inner_Scopes : array (1 .. Scope_Stack_Depth) of Entity_Id;
4513 Curr_Scope : Entity_Id := Empty;
4514 List : Elist_Id;
4515 Num_Inner : Nat := 0;
4516 Num_Scopes : Nat := 0;
4517 N_Instances : Nat := 0;
4518 Removed : Boolean := False;
4519 S : Entity_Id;
4520 Vis : Boolean;
4521
4522 begin
4523 -- Case of generic unit defined in another unit. We must remove the
4524 -- complete context of the current unit to install that of the generic.
4525
4526 if Gen_Comp /= Cunit_Entity (Current_Sem_Unit) then
4527
4528 -- Add some comments for the following two loops ???
4529
4530 S := Current_Scope;
4531 while Present (S) and then S /= Standard_Standard loop
4532 loop
4533 Num_Scopes := Num_Scopes + 1;
4534
4535 Use_Clauses (Num_Scopes) :=
4536 (Scope_Stack.Table
4537 (Scope_Stack.Last - Num_Scopes + 1).
4538 First_Use_Clause);
4539 End_Use_Clauses (Use_Clauses (Num_Scopes));
4540
4541 exit when Scope_Stack.Last - Num_Scopes + 1 = Scope_Stack.First
4542 or else Scope_Stack.Table
4543 (Scope_Stack.Last - Num_Scopes).Entity = Scope (S);
4544 end loop;
4545
4546 exit when Is_Generic_Instance (S)
4547 and then (In_Package_Body (S)
4548 or else Ekind (S) = E_Procedure
4549 or else Ekind (S) = E_Function);
4550 S := Scope (S);
4551 end loop;
4552
4553 Vis := Is_Immediately_Visible (Gen_Comp);
4554
4555 -- Find and save all enclosing instances
4556
4557 S := Current_Scope;
4558
4559 while Present (S)
4560 and then S /= Standard_Standard
4561 loop
4562 if Is_Generic_Instance (S) then
4563 N_Instances := N_Instances + 1;
4564 Instances (N_Instances) := S;
4565
4566 exit when In_Package_Body (S);
4567 end if;
4568
4569 S := Scope (S);
4570 end loop;
4571
4572 -- Remove context of current compilation unit, unless we are within a
4573 -- nested package instantiation, in which case the context has been
4574 -- removed previously.
4575
4576 -- If current scope is the body of a child unit, remove context of
4577 -- spec as well. If an enclosing scope is an instance body, the
4578 -- context has already been removed, but the entities in the body
4579 -- must be made invisible as well.
4580
4581 S := Current_Scope;
4582 while Present (S) and then S /= Standard_Standard loop
4583 if Is_Generic_Instance (S)
4584 and then (In_Package_Body (S)
4585 or else Ekind_In (S, E_Procedure, E_Function))
4586 then
4587 -- We still have to remove the entities of the enclosing
4588 -- instance from direct visibility.
4589
4590 declare
4591 E : Entity_Id;
4592 begin
4593 E := First_Entity (S);
4594 while Present (E) loop
4595 Set_Is_Immediately_Visible (E, False);
4596 Next_Entity (E);
4597 end loop;
4598 end;
4599
4600 exit;
4601 end if;
4602
4603 if S = Curr_Unit
4604 or else (Ekind (Curr_Unit) = E_Package_Body
4605 and then S = Spec_Entity (Curr_Unit))
4606 or else (Ekind (Curr_Unit) = E_Subprogram_Body
4607 and then S = Corresponding_Spec
4608 (Unit_Declaration_Node (Curr_Unit)))
4609 then
4610 Removed := True;
4611
4612 -- Remove entities in current scopes from visibility, so that
4613 -- instance body is compiled in a clean environment.
4614
4615 List := Save_Scope_Stack (Handle_Use => False);
4616
4617 if Is_Child_Unit (S) then
4618
4619 -- Remove child unit from stack, as well as inner scopes.
4620 -- Removing the context of a child unit removes parent units
4621 -- as well.
4622
4623 while Current_Scope /= S loop
4624 Num_Inner := Num_Inner + 1;
4625 Inner_Scopes (Num_Inner) := Current_Scope;
4626 Pop_Scope;
4627 end loop;
4628
4629 Pop_Scope;
4630 Remove_Context (Curr_Comp);
4631 Curr_Scope := S;
4632
4633 else
4634 Remove_Context (Curr_Comp);
4635 end if;
4636
4637 if Ekind (Curr_Unit) = E_Package_Body then
4638 Remove_Context (Library_Unit (Curr_Comp));
4639 end if;
4640 end if;
4641
4642 S := Scope (S);
4643 end loop;
4644
4645 pragma Assert (Num_Inner < Num_Scopes);
4646
4647 -- The inlined package body must be analyzed with the SPARK_Mode of
4648 -- the enclosing context, otherwise the body may cause bogus errors
4649 -- if a configuration SPARK_Mode pragma in in effect.
4650
4651 Push_Scope (Standard_Standard);
4652 Scope_Stack.Table (Scope_Stack.Last).Is_Active_Stack_Base := True;
4653 Instantiate_Package_Body
4654 (Body_Info =>
4655 ((Inst_Node => N,
4656 Act_Decl => Act_Decl,
4657 Expander_Status => Expander_Active,
4658 Current_Sem_Unit => Current_Sem_Unit,
4659 Scope_Suppress => Scope_Suppress,
4660 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
4661 Version => Ada_Version,
4662 Version_Pragma => Ada_Version_Pragma,
4663 Warnings => Save_Warnings,
4664 SPARK_Mode => Save_SM,
4665 SPARK_Mode_Pragma => Save_SMP)),
4666 Inlined_Body => True);
4667
4668 Pop_Scope;
4669
4670 -- Restore context
4671
4672 Set_Is_Immediately_Visible (Gen_Comp, Vis);
4673
4674 -- Reset Generic_Instance flag so that use clauses can be installed
4675 -- in the proper order. (See Use_One_Package for effect of enclosing
4676 -- instances on processing of use clauses).
4677
4678 for J in 1 .. N_Instances loop
4679 Set_Is_Generic_Instance (Instances (J), False);
4680 end loop;
4681
4682 if Removed then
4683 Install_Context (Curr_Comp);
4684
4685 if Present (Curr_Scope)
4686 and then Is_Child_Unit (Curr_Scope)
4687 then
4688 Push_Scope (Curr_Scope);
4689 Set_Is_Immediately_Visible (Curr_Scope);
4690
4691 -- Finally, restore inner scopes as well
4692
4693 for J in reverse 1 .. Num_Inner loop
4694 Push_Scope (Inner_Scopes (J));
4695 end loop;
4696 end if;
4697
4698 Restore_Scope_Stack (List, Handle_Use => False);
4699
4700 if Present (Curr_Scope)
4701 and then
4702 (In_Private_Part (Curr_Scope)
4703 or else In_Package_Body (Curr_Scope))
4704 then
4705 -- Install private declaration of ancestor units, which are
4706 -- currently available. Restore_Scope_Stack and Install_Context
4707 -- only install the visible part of parents.
4708
4709 declare
4710 Par : Entity_Id;
4711 begin
4712 Par := Scope (Curr_Scope);
4713 while (Present (Par)) and then Par /= Standard_Standard loop
4714 Install_Private_Declarations (Par);
4715 Par := Scope (Par);
4716 end loop;
4717 end;
4718 end if;
4719 end if;
4720
4721 -- Restore use clauses. For a child unit, use clauses in the parents
4722 -- are restored when installing the context, so only those in inner
4723 -- scopes (and those local to the child unit itself) need to be
4724 -- installed explicitly.
4725
4726 if Is_Child_Unit (Curr_Unit) and then Removed then
4727 for J in reverse 1 .. Num_Inner + 1 loop
4728 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
4729 Use_Clauses (J);
4730 Install_Use_Clauses (Use_Clauses (J));
4731 end loop;
4732
4733 else
4734 for J in reverse 1 .. Num_Scopes loop
4735 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
4736 Use_Clauses (J);
4737 Install_Use_Clauses (Use_Clauses (J));
4738 end loop;
4739 end if;
4740
4741 -- Restore status of instances. If one of them is a body, make its
4742 -- local entities visible again.
4743
4744 declare
4745 E : Entity_Id;
4746 Inst : Entity_Id;
4747
4748 begin
4749 for J in 1 .. N_Instances loop
4750 Inst := Instances (J);
4751 Set_Is_Generic_Instance (Inst, True);
4752
4753 if In_Package_Body (Inst)
4754 or else Ekind_In (S, E_Procedure, E_Function)
4755 then
4756 E := First_Entity (Instances (J));
4757 while Present (E) loop
4758 Set_Is_Immediately_Visible (E);
4759 Next_Entity (E);
4760 end loop;
4761 end if;
4762 end loop;
4763 end;
4764
4765 -- If generic unit is in current unit, current context is correct. Note
4766 -- that the context is guaranteed to carry the correct SPARK_Mode as no
4767 -- enclosing scopes were removed.
4768
4769 else
4770 Instantiate_Package_Body
4771 (Body_Info =>
4772 ((Inst_Node => N,
4773 Act_Decl => Act_Decl,
4774 Expander_Status => Expander_Active,
4775 Current_Sem_Unit => Current_Sem_Unit,
4776 Scope_Suppress => Scope_Suppress,
4777 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
4778 Version => Ada_Version,
4779 Version_Pragma => Ada_Version_Pragma,
4780 Warnings => Save_Warnings,
4781 SPARK_Mode => SPARK_Mode,
4782 SPARK_Mode_Pragma => SPARK_Mode_Pragma)),
4783 Inlined_Body => True);
4784 end if;
4785 end Inline_Instance_Body;
4786
4787 -------------------------------------
4788 -- Analyze_Procedure_Instantiation --
4789 -------------------------------------
4790
4791 procedure Analyze_Procedure_Instantiation (N : Node_Id) is
4792 begin
4793 Analyze_Subprogram_Instantiation (N, E_Procedure);
4794 end Analyze_Procedure_Instantiation;
4795
4796 -----------------------------------
4797 -- Need_Subprogram_Instance_Body --
4798 -----------------------------------
4799
4800 function Need_Subprogram_Instance_Body
4801 (N : Node_Id;
4802 Subp : Entity_Id) return Boolean
4803 is
4804
4805 function Is_Inlined_Or_Child_Of_Inlined (E : Entity_Id) return Boolean;
4806 -- Return True if E is an inlined subprogram, an inlined renaming or a
4807 -- subprogram nested in an inlined subprogram. The inlining machinery
4808 -- totally disregards nested subprograms since it considers that they
4809 -- will always be compiled if the parent is (see Inline.Is_Nested).
4810
4811 ------------------------------------
4812 -- Is_Inlined_Or_Child_Of_Inlined --
4813 ------------------------------------
4814
4815 function Is_Inlined_Or_Child_Of_Inlined (E : Entity_Id) return Boolean is
4816 Scop : Entity_Id;
4817
4818 begin
4819 if Is_Inlined (E) or else Is_Inlined (Alias (E)) then
4820 return True;
4821 end if;
4822
4823 Scop := Scope (E);
4824 while Scop /= Standard_Standard loop
4825 if Ekind (Scop) in Subprogram_Kind and then Is_Inlined (Scop) then
4826 return True;
4827 end if;
4828
4829 Scop := Scope (Scop);
4830 end loop;
4831
4832 return False;
4833 end Is_Inlined_Or_Child_Of_Inlined;
4834
4835 begin
4836 -- Must be in the main unit or inlined (or child of inlined)
4837
4838 if (Is_In_Main_Unit (N) or else Is_Inlined_Or_Child_Of_Inlined (Subp))
4839
4840 -- Must be generating code or analyzing code in ASIS/GNATprove mode
4841
4842 and then (Operating_Mode = Generate_Code
4843 or else (Operating_Mode = Check_Semantics
4844 and then (ASIS_Mode or GNATprove_Mode)))
4845
4846 -- The body is needed when generating code (full expansion), in ASIS
4847 -- mode for other tools, and in GNATprove mode (special expansion) for
4848 -- formal verification of the body itself.
4849
4850 and then (Expander_Active or ASIS_Mode or GNATprove_Mode)
4851
4852 -- No point in inlining if ABE is inevitable
4853
4854 and then not ABE_Is_Certain (N)
4855
4856 -- Or if subprogram is eliminated
4857
4858 and then not Is_Eliminated (Subp)
4859 then
4860 Add_Pending_Instantiation (N, Unit_Declaration_Node (Subp));
4861 return True;
4862
4863 -- Here if not inlined, or we ignore the inlining
4864
4865 else
4866 return False;
4867 end if;
4868 end Need_Subprogram_Instance_Body;
4869
4870 --------------------------------------
4871 -- Analyze_Subprogram_Instantiation --
4872 --------------------------------------
4873
4874 -- WARNING: This routine manages Ghost regions. Return statements must be
4875 -- replaced by gotos which jump to the end of the routine and restore the
4876 -- Ghost mode.
4877
4878 procedure Analyze_Subprogram_Instantiation
4879 (N : Node_Id;
4880 K : Entity_Kind)
4881 is
4882 Loc : constant Source_Ptr := Sloc (N);
4883 Gen_Id : constant Node_Id := Name (N);
4884
4885 Anon_Id : constant Entity_Id :=
4886 Make_Defining_Identifier (Sloc (Defining_Entity (N)),
4887 Chars => New_External_Name
4888 (Chars (Defining_Entity (N)), 'R'));
4889
4890 Act_Decl_Id : Entity_Id;
4891 Act_Decl : Node_Id;
4892 Act_Spec : Node_Id;
4893 Act_Tree : Node_Id;
4894
4895 Env_Installed : Boolean := False;
4896 Gen_Unit : Entity_Id;
4897 Gen_Decl : Node_Id;
4898 Pack_Id : Entity_Id;
4899 Parent_Installed : Boolean := False;
4900
4901 Renaming_List : List_Id;
4902 -- The list of declarations that link formals and actuals of the
4903 -- instance. These are subtype declarations for formal types, and
4904 -- renaming declarations for other formals. The subprogram declaration
4905 -- for the instance is then appended to the list, and the last item on
4906 -- the list is the renaming declaration for the instance.
4907
4908 procedure Analyze_Instance_And_Renamings;
4909 -- The instance must be analyzed in a context that includes the mappings
4910 -- of generic parameters into actuals. We create a package declaration
4911 -- for this purpose, and a subprogram with an internal name within the
4912 -- package. The subprogram instance is simply an alias for the internal
4913 -- subprogram, declared in the current scope.
4914
4915 procedure Build_Subprogram_Renaming;
4916 -- If the subprogram is recursive, there are occurrences of the name of
4917 -- the generic within the body, which must resolve to the current
4918 -- instance. We add a renaming declaration after the declaration, which
4919 -- is available in the instance body, as well as in the analysis of
4920 -- aspects that appear in the generic. This renaming declaration is
4921 -- inserted after the instance declaration which it renames.
4922
4923 ------------------------------------
4924 -- Analyze_Instance_And_Renamings --
4925 ------------------------------------
4926
4927 procedure Analyze_Instance_And_Renamings is
4928 Def_Ent : constant Entity_Id := Defining_Entity (N);
4929 Pack_Decl : Node_Id;
4930
4931 begin
4932 if Nkind (Parent (N)) = N_Compilation_Unit then
4933
4934 -- For the case of a compilation unit, the container package has
4935 -- the same name as the instantiation, to insure that the binder
4936 -- calls the elaboration procedure with the right name. Copy the
4937 -- entity of the instance, which may have compilation level flags
4938 -- (e.g. Is_Child_Unit) set.
4939
4940 Pack_Id := New_Copy (Def_Ent);
4941
4942 else
4943 -- Otherwise we use the name of the instantiation concatenated
4944 -- with its source position to ensure uniqueness if there are
4945 -- several instantiations with the same name.
4946
4947 Pack_Id :=
4948 Make_Defining_Identifier (Loc,
4949 Chars => New_External_Name
4950 (Related_Id => Chars (Def_Ent),
4951 Suffix => "GP",
4952 Suffix_Index => Source_Offset (Sloc (Def_Ent))));
4953 end if;
4954
4955 Pack_Decl :=
4956 Make_Package_Declaration (Loc,
4957 Specification => Make_Package_Specification (Loc,
4958 Defining_Unit_Name => Pack_Id,
4959 Visible_Declarations => Renaming_List,
4960 End_Label => Empty));
4961
4962 Set_Instance_Spec (N, Pack_Decl);
4963 Set_Is_Generic_Instance (Pack_Id);
4964 Set_Debug_Info_Needed (Pack_Id);
4965
4966 -- Case of not a compilation unit
4967
4968 if Nkind (Parent (N)) /= N_Compilation_Unit then
4969 Mark_Rewrite_Insertion (Pack_Decl);
4970 Insert_Before (N, Pack_Decl);
4971 Set_Has_Completion (Pack_Id);
4972
4973 -- Case of an instantiation that is a compilation unit
4974
4975 -- Place declaration on current node so context is complete for
4976 -- analysis (including nested instantiations), and for use in a
4977 -- context_clause (see Analyze_With_Clause).
4978
4979 else
4980 Set_Unit (Parent (N), Pack_Decl);
4981 Set_Parent_Spec (Pack_Decl, Parent_Spec (N));
4982 end if;
4983
4984 Analyze (Pack_Decl);
4985 Check_Formal_Packages (Pack_Id);
4986 Set_Is_Generic_Instance (Pack_Id, False);
4987
4988 -- Why do we clear Is_Generic_Instance??? We set it 20 lines
4989 -- above???
4990
4991 -- Body of the enclosing package is supplied when instantiating the
4992 -- subprogram body, after semantic analysis is completed.
4993
4994 if Nkind (Parent (N)) = N_Compilation_Unit then
4995
4996 -- Remove package itself from visibility, so it does not
4997 -- conflict with subprogram.
4998
4999 Set_Name_Entity_Id (Chars (Pack_Id), Homonym (Pack_Id));
5000
5001 -- Set name and scope of internal subprogram so that the proper
5002 -- external name will be generated. The proper scope is the scope
5003 -- of the wrapper package. We need to generate debugging info for
5004 -- the internal subprogram, so set flag accordingly.
5005
5006 Set_Chars (Anon_Id, Chars (Defining_Entity (N)));
5007 Set_Scope (Anon_Id, Scope (Pack_Id));
5008
5009 -- Mark wrapper package as referenced, to avoid spurious warnings
5010 -- if the instantiation appears in various with_ clauses of
5011 -- subunits of the main unit.
5012
5013 Set_Referenced (Pack_Id);
5014 end if;
5015
5016 Set_Is_Generic_Instance (Anon_Id);
5017 Set_Debug_Info_Needed (Anon_Id);
5018 Act_Decl_Id := New_Copy (Anon_Id);
5019
5020 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
5021 Set_Chars (Act_Decl_Id, Chars (Defining_Entity (N)));
5022 Set_Sloc (Act_Decl_Id, Sloc (Defining_Entity (N)));
5023
5024 -- Subprogram instance comes from source only if generic does
5025
5026 Set_Comes_From_Source (Act_Decl_Id, Comes_From_Source (Gen_Unit));
5027
5028 -- If the instance is a child unit, mark the Id accordingly. Mark
5029 -- the anonymous entity as well, which is the real subprogram and
5030 -- which is used when the instance appears in a context clause.
5031 -- Similarly, propagate the Is_Eliminated flag to handle properly
5032 -- nested eliminated subprograms.
5033
5034 Set_Is_Child_Unit (Act_Decl_Id, Is_Child_Unit (Defining_Entity (N)));
5035 Set_Is_Child_Unit (Anon_Id, Is_Child_Unit (Defining_Entity (N)));
5036 New_Overloaded_Entity (Act_Decl_Id);
5037 Check_Eliminated (Act_Decl_Id);
5038 Set_Is_Eliminated (Anon_Id, Is_Eliminated (Act_Decl_Id));
5039
5040 -- In compilation unit case, kill elaboration checks on the
5041 -- instantiation, since they are never needed -- the body is
5042 -- instantiated at the same point as the spec.
5043
5044 if Nkind (Parent (N)) = N_Compilation_Unit then
5045 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
5046 Set_Kill_Elaboration_Checks (Act_Decl_Id);
5047 Set_Is_Compilation_Unit (Anon_Id);
5048
5049 Set_Cunit_Entity (Current_Sem_Unit, Pack_Id);
5050 end if;
5051
5052 -- The instance is not a freezing point for the new subprogram.
5053 -- The anonymous subprogram may have a freeze node, created for
5054 -- some delayed aspects. This freeze node must not be inherited
5055 -- by the visible subprogram entity.
5056
5057 Set_Is_Frozen (Act_Decl_Id, False);
5058 Set_Freeze_Node (Act_Decl_Id, Empty);
5059
5060 if Nkind (Defining_Entity (N)) = N_Defining_Operator_Symbol then
5061 Valid_Operator_Definition (Act_Decl_Id);
5062 end if;
5063
5064 Set_Alias (Act_Decl_Id, Anon_Id);
5065 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
5066 Set_Has_Completion (Act_Decl_Id);
5067 Set_Related_Instance (Pack_Id, Act_Decl_Id);
5068
5069 if Nkind (Parent (N)) = N_Compilation_Unit then
5070 Set_Body_Required (Parent (N), False);
5071 end if;
5072 end Analyze_Instance_And_Renamings;
5073
5074 -------------------------------
5075 -- Build_Subprogram_Renaming --
5076 -------------------------------
5077
5078 procedure Build_Subprogram_Renaming is
5079 Renaming_Decl : Node_Id;
5080 Unit_Renaming : Node_Id;
5081
5082 begin
5083 Unit_Renaming :=
5084 Make_Subprogram_Renaming_Declaration (Loc,
5085 Specification =>
5086 Copy_Generic_Node
5087 (Specification (Original_Node (Gen_Decl)),
5088 Empty,
5089 Instantiating => True),
5090 Name => New_Occurrence_Of (Anon_Id, Loc));
5091
5092 -- The generic may be a a child unit. The renaming needs an
5093 -- identifier with the proper name.
5094
5095 Set_Defining_Unit_Name (Specification (Unit_Renaming),
5096 Make_Defining_Identifier (Loc, Chars (Gen_Unit)));
5097
5098 -- If there is a formal subprogram with the same name as the unit
5099 -- itself, do not add this renaming declaration, to prevent
5100 -- ambiguities when there is a call with that name in the body.
5101 -- This is a partial and ugly fix for one ACATS test. ???
5102
5103 Renaming_Decl := First (Renaming_List);
5104 while Present (Renaming_Decl) loop
5105 if Nkind (Renaming_Decl) = N_Subprogram_Renaming_Declaration
5106 and then
5107 Chars (Defining_Entity (Renaming_Decl)) = Chars (Gen_Unit)
5108 then
5109 exit;
5110 end if;
5111
5112 Next (Renaming_Decl);
5113 end loop;
5114
5115 if No (Renaming_Decl) then
5116 Append (Unit_Renaming, Renaming_List);
5117 end if;
5118 end Build_Subprogram_Renaming;
5119
5120 -- Local variables
5121
5122 Save_IPSM : constant Boolean := Ignore_Pragma_SPARK_Mode;
5123 -- Save flag Ignore_Pragma_SPARK_Mode for restore on exit
5124
5125 Save_SM : constant SPARK_Mode_Type := SPARK_Mode;
5126 Save_SMP : constant Node_Id := SPARK_Mode_Pragma;
5127 -- Save the SPARK_Mode-related data for restore on exit
5128
5129 Mode : Ghost_Mode_Type;
5130 Mode_Set : Boolean := False;
5131
5132 Vis_Prims_List : Elist_Id := No_Elist;
5133 -- List of primitives made temporarily visible in the instantiation
5134 -- to match the visibility of the formal type
5135
5136 -- Start of processing for Analyze_Subprogram_Instantiation
5137
5138 begin
5139 Check_SPARK_05_Restriction ("generic is not allowed", N);
5140
5141 -- Very first thing: check for special Text_IO unit in case we are
5142 -- instantiating one of the children of [[Wide_]Wide_]Text_IO. Of course
5143 -- such an instantiation is bogus (these are packages, not subprograms),
5144 -- but we get a better error message if we do this.
5145
5146 Check_Text_IO_Special_Unit (Gen_Id);
5147
5148 -- Make node global for error reporting
5149
5150 Instantiation_Node := N;
5151
5152 -- For package instantiations we turn off style checks, because they
5153 -- will have been emitted in the generic. For subprogram instantiations
5154 -- we want to apply at least the check on overriding indicators so we
5155 -- do not modify the style check status.
5156
5157 -- The renaming declarations for the actuals do not come from source and
5158 -- will not generate spurious warnings.
5159
5160 Preanalyze_Actuals (N);
5161
5162 Init_Env;
5163 Env_Installed := True;
5164 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
5165 Gen_Unit := Entity (Gen_Id);
5166
5167 -- A subprogram instantiation is Ghost when it is subject to pragma
5168 -- Ghost or the generic template is Ghost. Set the mode now to ensure
5169 -- that any nodes generated during analysis and expansion are marked as
5170 -- Ghost.
5171
5172 Mark_And_Set_Ghost_Instantiation (N, Gen_Unit, Mode);
5173 Mode_Set := True;
5174
5175 Generate_Reference (Gen_Unit, Gen_Id);
5176
5177 if Nkind (Gen_Id) = N_Identifier
5178 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
5179 then
5180 Error_Msg_NE
5181 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
5182 end if;
5183
5184 if Etype (Gen_Unit) = Any_Type then
5185 Restore_Env;
5186 goto Leave;
5187 end if;
5188
5189 -- Verify that it is a generic subprogram of the right kind, and that
5190 -- it does not lead to a circular instantiation.
5191
5192 if K = E_Procedure and then Ekind (Gen_Unit) /= E_Generic_Procedure then
5193 Error_Msg_NE
5194 ("& is not the name of a generic procedure", Gen_Id, Gen_Unit);
5195
5196 elsif K = E_Function and then Ekind (Gen_Unit) /= E_Generic_Function then
5197 Error_Msg_NE
5198 ("& is not the name of a generic function", Gen_Id, Gen_Unit);
5199
5200 elsif In_Open_Scopes (Gen_Unit) then
5201 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
5202
5203 else
5204 -- If the context of the instance is subject to SPARK_Mode "off" or
5205 -- the annotation is altogether missing, set the global flag which
5206 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
5207 -- the instance.
5208
5209 if SPARK_Mode /= On then
5210 Ignore_Pragma_SPARK_Mode := True;
5211 end if;
5212
5213 Set_Entity (Gen_Id, Gen_Unit);
5214 Set_Is_Instantiated (Gen_Unit);
5215
5216 if In_Extended_Main_Source_Unit (N) then
5217 Generate_Reference (Gen_Unit, N);
5218 end if;
5219
5220 -- If renaming, get original unit
5221
5222 if Present (Renamed_Object (Gen_Unit))
5223 and then Ekind_In (Renamed_Object (Gen_Unit), E_Generic_Procedure,
5224 E_Generic_Function)
5225 then
5226 Gen_Unit := Renamed_Object (Gen_Unit);
5227 Set_Is_Instantiated (Gen_Unit);
5228 Generate_Reference (Gen_Unit, N);
5229 end if;
5230
5231 if Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
5232 Error_Msg_Node_2 := Current_Scope;
5233 Error_Msg_NE
5234 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
5235 Circularity_Detected := True;
5236 Restore_Hidden_Primitives (Vis_Prims_List);
5237 goto Leave;
5238 end if;
5239
5240 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
5241
5242 -- Initialize renamings map, for error checking
5243
5244 Generic_Renamings.Set_Last (0);
5245 Generic_Renamings_HTable.Reset;
5246
5247 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
5248
5249 -- Copy original generic tree, to produce text for instantiation
5250
5251 Act_Tree :=
5252 Copy_Generic_Node
5253 (Original_Node (Gen_Decl), Empty, Instantiating => True);
5254
5255 -- Inherit overriding indicator from instance node
5256
5257 Act_Spec := Specification (Act_Tree);
5258 Set_Must_Override (Act_Spec, Must_Override (N));
5259 Set_Must_Not_Override (Act_Spec, Must_Not_Override (N));
5260
5261 Renaming_List :=
5262 Analyze_Associations
5263 (I_Node => N,
5264 Formals => Generic_Formal_Declarations (Act_Tree),
5265 F_Copy => Generic_Formal_Declarations (Gen_Decl));
5266
5267 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
5268
5269 -- The subprogram itself cannot contain a nested instance, so the
5270 -- current parent is left empty.
5271
5272 Set_Instance_Env (Gen_Unit, Empty);
5273
5274 -- Build the subprogram declaration, which does not appear in the
5275 -- generic template, and give it a sloc consistent with that of the
5276 -- template.
5277
5278 Set_Defining_Unit_Name (Act_Spec, Anon_Id);
5279 Set_Generic_Parent (Act_Spec, Gen_Unit);
5280 Act_Decl :=
5281 Make_Subprogram_Declaration (Sloc (Act_Spec),
5282 Specification => Act_Spec);
5283
5284 -- The aspects have been copied previously, but they have to be
5285 -- linked explicitly to the new subprogram declaration. Explicit
5286 -- pre/postconditions on the instance are analyzed below, in a
5287 -- separate step.
5288
5289 Move_Aspects (Act_Tree, To => Act_Decl);
5290 Set_Categorization_From_Pragmas (Act_Decl);
5291
5292 if Parent_Installed then
5293 Hide_Current_Scope;
5294 end if;
5295
5296 Append (Act_Decl, Renaming_List);
5297
5298 -- Contract-related source pragmas that follow a generic subprogram
5299 -- must be instantiated explicitly because they are not part of the
5300 -- subprogram template.
5301
5302 Instantiate_Subprogram_Contract
5303 (Original_Node (Gen_Decl), Renaming_List);
5304
5305 Build_Subprogram_Renaming;
5306 Analyze_Instance_And_Renamings;
5307
5308 -- If the generic is marked Import (Intrinsic), then so is the
5309 -- instance. This indicates that there is no body to instantiate. If
5310 -- generic is marked inline, so it the instance, and the anonymous
5311 -- subprogram it renames. If inlined, or else if inlining is enabled
5312 -- for the compilation, we generate the instance body even if it is
5313 -- not within the main unit.
5314
5315 if Is_Intrinsic_Subprogram (Gen_Unit) then
5316 Set_Is_Intrinsic_Subprogram (Anon_Id);
5317 Set_Is_Intrinsic_Subprogram (Act_Decl_Id);
5318
5319 if Chars (Gen_Unit) = Name_Unchecked_Conversion then
5320 Validate_Unchecked_Conversion (N, Act_Decl_Id);
5321 end if;
5322 end if;
5323
5324 -- Inherit convention from generic unit. Intrinsic convention, as for
5325 -- an instance of unchecked conversion, is not inherited because an
5326 -- explicit Ada instance has been created.
5327
5328 if Has_Convention_Pragma (Gen_Unit)
5329 and then Convention (Gen_Unit) /= Convention_Intrinsic
5330 then
5331 Set_Convention (Act_Decl_Id, Convention (Gen_Unit));
5332 Set_Is_Exported (Act_Decl_Id, Is_Exported (Gen_Unit));
5333 end if;
5334
5335 Generate_Definition (Act_Decl_Id);
5336
5337 -- Inherit all inlining-related flags which apply to the generic in
5338 -- the subprogram and its declaration.
5339
5340 Set_Is_Inlined (Act_Decl_Id, Is_Inlined (Gen_Unit));
5341 Set_Is_Inlined (Anon_Id, Is_Inlined (Gen_Unit));
5342
5343 Set_Has_Pragma_Inline (Act_Decl_Id, Has_Pragma_Inline (Gen_Unit));
5344 Set_Has_Pragma_Inline (Anon_Id, Has_Pragma_Inline (Gen_Unit));
5345
5346 Set_Has_Pragma_Inline_Always
5347 (Act_Decl_Id, Has_Pragma_Inline_Always (Gen_Unit));
5348 Set_Has_Pragma_Inline_Always
5349 (Anon_Id, Has_Pragma_Inline_Always (Gen_Unit));
5350
5351 if not Is_Intrinsic_Subprogram (Gen_Unit) then
5352 Check_Elab_Instantiation (N);
5353 end if;
5354
5355 if Is_Dispatching_Operation (Act_Decl_Id)
5356 and then Ada_Version >= Ada_2005
5357 then
5358 declare
5359 Formal : Entity_Id;
5360
5361 begin
5362 Formal := First_Formal (Act_Decl_Id);
5363 while Present (Formal) loop
5364 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type
5365 and then Is_Controlling_Formal (Formal)
5366 and then not Can_Never_Be_Null (Formal)
5367 then
5368 Error_Msg_NE
5369 ("access parameter& is controlling,", N, Formal);
5370 Error_Msg_NE
5371 ("\corresponding parameter of & must be explicitly "
5372 & "null-excluding", N, Gen_Id);
5373 end if;
5374
5375 Next_Formal (Formal);
5376 end loop;
5377 end;
5378 end if;
5379
5380 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
5381
5382 Validate_Categorization_Dependency (N, Act_Decl_Id);
5383
5384 if not Is_Intrinsic_Subprogram (Act_Decl_Id) then
5385 Inherit_Context (Gen_Decl, N);
5386
5387 Restore_Private_Views (Pack_Id, False);
5388
5389 -- If the context requires a full instantiation, mark node for
5390 -- subsequent construction of the body.
5391
5392 if Need_Subprogram_Instance_Body (N, Act_Decl_Id) then
5393 Check_Forward_Instantiation (Gen_Decl);
5394
5395 -- The wrapper package is always delayed, because it does not
5396 -- constitute a freeze point, but to insure that the freeze node
5397 -- is placed properly, it is created directly when instantiating
5398 -- the body (otherwise the freeze node might appear to early for
5399 -- nested instantiations). For ASIS purposes, indicate that the
5400 -- wrapper package has replaced the instantiation node.
5401
5402 elsif Nkind (Parent (N)) = N_Compilation_Unit then
5403 Rewrite (N, Unit (Parent (N)));
5404 Set_Unit (Parent (N), N);
5405 end if;
5406
5407 -- Replace instance node for library-level instantiations of
5408 -- intrinsic subprograms, for ASIS use.
5409
5410 elsif Nkind (Parent (N)) = N_Compilation_Unit then
5411 Rewrite (N, Unit (Parent (N)));
5412 Set_Unit (Parent (N), N);
5413 end if;
5414
5415 if Parent_Installed then
5416 Remove_Parent;
5417 end if;
5418
5419 Restore_Hidden_Primitives (Vis_Prims_List);
5420 Restore_Env;
5421 Env_Installed := False;
5422 Generic_Renamings.Set_Last (0);
5423 Generic_Renamings_HTable.Reset;
5424
5425 Ignore_Pragma_SPARK_Mode := Save_IPSM;
5426 SPARK_Mode := Save_SM;
5427 SPARK_Mode_Pragma := Save_SMP;
5428 end if;
5429
5430 <<Leave>>
5431 if Has_Aspects (N) then
5432 Analyze_Aspect_Specifications (N, Act_Decl_Id);
5433 end if;
5434
5435 if Mode_Set then
5436 Restore_Ghost_Mode (Mode);
5437 end if;
5438
5439 exception
5440 when Instantiation_Error =>
5441 if Parent_Installed then
5442 Remove_Parent;
5443 end if;
5444
5445 if Env_Installed then
5446 Restore_Env;
5447 end if;
5448
5449 Ignore_Pragma_SPARK_Mode := Save_IPSM;
5450 SPARK_Mode := Save_SM;
5451 SPARK_Mode_Pragma := Save_SMP;
5452
5453 if Mode_Set then
5454 Restore_Ghost_Mode (Mode);
5455 end if;
5456 end Analyze_Subprogram_Instantiation;
5457
5458 -------------------------
5459 -- Get_Associated_Node --
5460 -------------------------
5461
5462 function Get_Associated_Node (N : Node_Id) return Node_Id is
5463 Assoc : Node_Id;
5464
5465 begin
5466 Assoc := Associated_Node (N);
5467
5468 if Nkind (Assoc) /= Nkind (N) then
5469 return Assoc;
5470
5471 elsif Nkind_In (Assoc, N_Aggregate, N_Extension_Aggregate) then
5472 return Assoc;
5473
5474 else
5475 -- If the node is part of an inner generic, it may itself have been
5476 -- remapped into a further generic copy. Associated_Node is otherwise
5477 -- used for the entity of the node, and will be of a different node
5478 -- kind, or else N has been rewritten as a literal or function call.
5479
5480 while Present (Associated_Node (Assoc))
5481 and then Nkind (Associated_Node (Assoc)) = Nkind (Assoc)
5482 loop
5483 Assoc := Associated_Node (Assoc);
5484 end loop;
5485
5486 -- Follow and additional link in case the final node was rewritten.
5487 -- This can only happen with nested generic units.
5488
5489 if (Nkind (Assoc) = N_Identifier or else Nkind (Assoc) in N_Op)
5490 and then Present (Associated_Node (Assoc))
5491 and then (Nkind_In (Associated_Node (Assoc), N_Function_Call,
5492 N_Explicit_Dereference,
5493 N_Integer_Literal,
5494 N_Real_Literal,
5495 N_String_Literal))
5496 then
5497 Assoc := Associated_Node (Assoc);
5498 end if;
5499
5500 -- An additional special case: an unconstrained type in an object
5501 -- declaration may have been rewritten as a local subtype constrained
5502 -- by the expression in the declaration. We need to recover the
5503 -- original entity which may be global.
5504
5505 if Present (Original_Node (Assoc))
5506 and then Nkind (Parent (N)) = N_Object_Declaration
5507 then
5508 Assoc := Original_Node (Assoc);
5509 end if;
5510
5511 return Assoc;
5512 end if;
5513 end Get_Associated_Node;
5514
5515 ----------------------------
5516 -- Build_Function_Wrapper --
5517 ----------------------------
5518
5519 function Build_Function_Wrapper
5520 (Formal_Subp : Entity_Id;
5521 Actual_Subp : Entity_Id) return Node_Id
5522 is
5523 Loc : constant Source_Ptr := Sloc (Current_Scope);
5524 Ret_Type : constant Entity_Id := Get_Instance_Of (Etype (Formal_Subp));
5525 Actuals : List_Id;
5526 Decl : Node_Id;
5527 Func_Name : Node_Id;
5528 Func : Entity_Id;
5529 Parm_Type : Node_Id;
5530 Profile : List_Id := New_List;
5531 Spec : Node_Id;
5532 Act_F : Entity_Id;
5533 Form_F : Entity_Id;
5534 New_F : Entity_Id;
5535
5536 begin
5537 Func_Name := New_Occurrence_Of (Actual_Subp, Loc);
5538
5539 Func := Make_Defining_Identifier (Loc, Chars (Formal_Subp));
5540 Set_Ekind (Func, E_Function);
5541 Set_Is_Generic_Actual_Subprogram (Func);
5542
5543 Actuals := New_List;
5544 Profile := New_List;
5545
5546 Act_F := First_Formal (Actual_Subp);
5547 Form_F := First_Formal (Formal_Subp);
5548 while Present (Form_F) loop
5549
5550 -- Create new formal for profile of wrapper, and add a reference
5551 -- to it in the list of actuals for the enclosing call. The name
5552 -- must be that of the formal in the formal subprogram, because
5553 -- calls to it in the generic body may use named associations.
5554
5555 New_F := Make_Defining_Identifier (Loc, Chars (Form_F));
5556
5557 Parm_Type :=
5558 New_Occurrence_Of (Get_Instance_Of (Etype (Form_F)), Loc);
5559
5560 Append_To (Profile,
5561 Make_Parameter_Specification (Loc,
5562 Defining_Identifier => New_F,
5563 Parameter_Type => Parm_Type));
5564
5565 Append_To (Actuals, New_Occurrence_Of (New_F, Loc));
5566 Next_Formal (Form_F);
5567
5568 if Present (Act_F) then
5569 Next_Formal (Act_F);
5570 end if;
5571 end loop;
5572
5573 Spec :=
5574 Make_Function_Specification (Loc,
5575 Defining_Unit_Name => Func,
5576 Parameter_Specifications => Profile,
5577 Result_Definition => New_Occurrence_Of (Ret_Type, Loc));
5578
5579 Decl :=
5580 Make_Expression_Function (Loc,
5581 Specification => Spec,
5582 Expression =>
5583 Make_Function_Call (Loc,
5584 Name => Func_Name,
5585 Parameter_Associations => Actuals));
5586
5587 return Decl;
5588 end Build_Function_Wrapper;
5589
5590 ----------------------------
5591 -- Build_Operator_Wrapper --
5592 ----------------------------
5593
5594 function Build_Operator_Wrapper
5595 (Formal_Subp : Entity_Id;
5596 Actual_Subp : Entity_Id) return Node_Id
5597 is
5598 Loc : constant Source_Ptr := Sloc (Current_Scope);
5599 Ret_Type : constant Entity_Id :=
5600 Get_Instance_Of (Etype (Formal_Subp));
5601 Op_Type : constant Entity_Id :=
5602 Get_Instance_Of (Etype (First_Formal (Formal_Subp)));
5603 Is_Binary : constant Boolean :=
5604 Present (Next_Formal (First_Formal (Formal_Subp)));
5605
5606 Decl : Node_Id;
5607 Expr : Node_Id;
5608 F1, F2 : Entity_Id;
5609 Func : Entity_Id;
5610 Op_Name : Name_Id;
5611 Spec : Node_Id;
5612 L, R : Node_Id;
5613
5614 begin
5615 Op_Name := Chars (Actual_Subp);
5616
5617 -- Create entities for wrapper function and its formals
5618
5619 F1 := Make_Temporary (Loc, 'A');
5620 F2 := Make_Temporary (Loc, 'B');
5621 L := New_Occurrence_Of (F1, Loc);
5622 R := New_Occurrence_Of (F2, Loc);
5623
5624 Func := Make_Defining_Identifier (Loc, Chars (Formal_Subp));
5625 Set_Ekind (Func, E_Function);
5626 Set_Is_Generic_Actual_Subprogram (Func);
5627
5628 Spec :=
5629 Make_Function_Specification (Loc,
5630 Defining_Unit_Name => Func,
5631 Parameter_Specifications => New_List (
5632 Make_Parameter_Specification (Loc,
5633 Defining_Identifier => F1,
5634 Parameter_Type => New_Occurrence_Of (Op_Type, Loc))),
5635 Result_Definition => New_Occurrence_Of (Ret_Type, Loc));
5636
5637 if Is_Binary then
5638 Append_To (Parameter_Specifications (Spec),
5639 Make_Parameter_Specification (Loc,
5640 Defining_Identifier => F2,
5641 Parameter_Type => New_Occurrence_Of (Op_Type, Loc)));
5642 end if;
5643
5644 -- Build expression as a function call, or as an operator node
5645 -- that corresponds to the name of the actual, starting with
5646 -- binary operators.
5647
5648 if Op_Name not in Any_Operator_Name then
5649 Expr :=
5650 Make_Function_Call (Loc,
5651 Name =>
5652 New_Occurrence_Of (Actual_Subp, Loc),
5653 Parameter_Associations => New_List (L));
5654
5655 if Is_Binary then
5656 Append_To (Parameter_Associations (Expr), R);
5657 end if;
5658
5659 -- Binary operators
5660
5661 elsif Is_Binary then
5662 if Op_Name = Name_Op_And then
5663 Expr := Make_Op_And (Loc, Left_Opnd => L, Right_Opnd => R);
5664 elsif Op_Name = Name_Op_Or then
5665 Expr := Make_Op_Or (Loc, Left_Opnd => L, Right_Opnd => R);
5666 elsif Op_Name = Name_Op_Xor then
5667 Expr := Make_Op_Xor (Loc, Left_Opnd => L, Right_Opnd => R);
5668 elsif Op_Name = Name_Op_Eq then
5669 Expr := Make_Op_Eq (Loc, Left_Opnd => L, Right_Opnd => R);
5670 elsif Op_Name = Name_Op_Ne then
5671 Expr := Make_Op_Ne (Loc, Left_Opnd => L, Right_Opnd => R);
5672 elsif Op_Name = Name_Op_Le then
5673 Expr := Make_Op_Le (Loc, Left_Opnd => L, Right_Opnd => R);
5674 elsif Op_Name = Name_Op_Gt then
5675 Expr := Make_Op_Gt (Loc, Left_Opnd => L, Right_Opnd => R);
5676 elsif Op_Name = Name_Op_Ge then
5677 Expr := Make_Op_Ge (Loc, Left_Opnd => L, Right_Opnd => R);
5678 elsif Op_Name = Name_Op_Lt then
5679 Expr := Make_Op_Lt (Loc, Left_Opnd => L, Right_Opnd => R);
5680 elsif Op_Name = Name_Op_Add then
5681 Expr := Make_Op_Add (Loc, Left_Opnd => L, Right_Opnd => R);
5682 elsif Op_Name = Name_Op_Subtract then
5683 Expr := Make_Op_Subtract (Loc, Left_Opnd => L, Right_Opnd => R);
5684 elsif Op_Name = Name_Op_Concat then
5685 Expr := Make_Op_Concat (Loc, Left_Opnd => L, Right_Opnd => R);
5686 elsif Op_Name = Name_Op_Multiply then
5687 Expr := Make_Op_Multiply (Loc, Left_Opnd => L, Right_Opnd => R);
5688 elsif Op_Name = Name_Op_Divide then
5689 Expr := Make_Op_Divide (Loc, Left_Opnd => L, Right_Opnd => R);
5690 elsif Op_Name = Name_Op_Mod then
5691 Expr := Make_Op_Mod (Loc, Left_Opnd => L, Right_Opnd => R);
5692 elsif Op_Name = Name_Op_Rem then
5693 Expr := Make_Op_Rem (Loc, Left_Opnd => L, Right_Opnd => R);
5694 elsif Op_Name = Name_Op_Expon then
5695 Expr := Make_Op_Expon (Loc, Left_Opnd => L, Right_Opnd => R);
5696 end if;
5697
5698 -- Unary operators
5699
5700 else
5701 if Op_Name = Name_Op_Add then
5702 Expr := Make_Op_Plus (Loc, Right_Opnd => L);
5703 elsif Op_Name = Name_Op_Subtract then
5704 Expr := Make_Op_Minus (Loc, Right_Opnd => L);
5705 elsif Op_Name = Name_Op_Abs then
5706 Expr := Make_Op_Abs (Loc, Right_Opnd => L);
5707 elsif Op_Name = Name_Op_Not then
5708 Expr := Make_Op_Not (Loc, Right_Opnd => L);
5709 end if;
5710 end if;
5711
5712 Decl :=
5713 Make_Expression_Function (Loc,
5714 Specification => Spec,
5715 Expression => Expr);
5716
5717 return Decl;
5718 end Build_Operator_Wrapper;
5719
5720 -------------------------------------------
5721 -- Build_Instance_Compilation_Unit_Nodes --
5722 -------------------------------------------
5723
5724 procedure Build_Instance_Compilation_Unit_Nodes
5725 (N : Node_Id;
5726 Act_Body : Node_Id;
5727 Act_Decl : Node_Id)
5728 is
5729 Decl_Cunit : Node_Id;
5730 Body_Cunit : Node_Id;
5731 Citem : Node_Id;
5732 New_Main : constant Entity_Id := Defining_Entity (Act_Decl);
5733 Old_Main : constant Entity_Id := Cunit_Entity (Main_Unit);
5734
5735 begin
5736 -- A new compilation unit node is built for the instance declaration
5737
5738 Decl_Cunit :=
5739 Make_Compilation_Unit (Sloc (N),
5740 Context_Items => Empty_List,
5741 Unit => Act_Decl,
5742 Aux_Decls_Node => Make_Compilation_Unit_Aux (Sloc (N)));
5743
5744 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
5745
5746 -- The new compilation unit is linked to its body, but both share the
5747 -- same file, so we do not set Body_Required on the new unit so as not
5748 -- to create a spurious dependency on a non-existent body in the ali.
5749 -- This simplifies CodePeer unit traversal.
5750
5751 -- We use the original instantiation compilation unit as the resulting
5752 -- compilation unit of the instance, since this is the main unit.
5753
5754 Rewrite (N, Act_Body);
5755
5756 -- Propagate the aspect specifications from the package body template to
5757 -- the instantiated version of the package body.
5758
5759 if Has_Aspects (Act_Body) then
5760 Set_Aspect_Specifications
5761 (N, New_Copy_List_Tree (Aspect_Specifications (Act_Body)));
5762 end if;
5763
5764 Body_Cunit := Parent (N);
5765
5766 -- The two compilation unit nodes are linked by the Library_Unit field
5767
5768 Set_Library_Unit (Decl_Cunit, Body_Cunit);
5769 Set_Library_Unit (Body_Cunit, Decl_Cunit);
5770
5771 -- Preserve the private nature of the package if needed
5772
5773 Set_Private_Present (Decl_Cunit, Private_Present (Body_Cunit));
5774
5775 -- If the instance is not the main unit, its context, categorization
5776 -- and elaboration entity are not relevant to the compilation.
5777
5778 if Body_Cunit /= Cunit (Main_Unit) then
5779 Make_Instance_Unit (Body_Cunit, In_Main => False);
5780 return;
5781 end if;
5782
5783 -- The context clause items on the instantiation, which are now attached
5784 -- to the body compilation unit (since the body overwrote the original
5785 -- instantiation node), semantically belong on the spec, so copy them
5786 -- there. It's harmless to leave them on the body as well. In fact one
5787 -- could argue that they belong in both places.
5788
5789 Citem := First (Context_Items (Body_Cunit));
5790 while Present (Citem) loop
5791 Append (New_Copy (Citem), Context_Items (Decl_Cunit));
5792 Next (Citem);
5793 end loop;
5794
5795 -- Propagate categorization flags on packages, so that they appear in
5796 -- the ali file for the spec of the unit.
5797
5798 if Ekind (New_Main) = E_Package then
5799 Set_Is_Pure (Old_Main, Is_Pure (New_Main));
5800 Set_Is_Preelaborated (Old_Main, Is_Preelaborated (New_Main));
5801 Set_Is_Remote_Types (Old_Main, Is_Remote_Types (New_Main));
5802 Set_Is_Shared_Passive (Old_Main, Is_Shared_Passive (New_Main));
5803 Set_Is_Remote_Call_Interface
5804 (Old_Main, Is_Remote_Call_Interface (New_Main));
5805 end if;
5806
5807 -- Make entry in Units table, so that binder can generate call to
5808 -- elaboration procedure for body, if any.
5809
5810 Make_Instance_Unit (Body_Cunit, In_Main => True);
5811 Main_Unit_Entity := New_Main;
5812 Set_Cunit_Entity (Main_Unit, Main_Unit_Entity);
5813
5814 -- Build elaboration entity, since the instance may certainly generate
5815 -- elaboration code requiring a flag for protection.
5816
5817 Build_Elaboration_Entity (Decl_Cunit, New_Main);
5818 end Build_Instance_Compilation_Unit_Nodes;
5819
5820 -----------------------------
5821 -- Check_Access_Definition --
5822 -----------------------------
5823
5824 procedure Check_Access_Definition (N : Node_Id) is
5825 begin
5826 pragma Assert
5827 (Ada_Version >= Ada_2005 and then Present (Access_Definition (N)));
5828 null;
5829 end Check_Access_Definition;
5830
5831 -----------------------------------
5832 -- Check_Formal_Package_Instance --
5833 -----------------------------------
5834
5835 -- If the formal has specific parameters, they must match those of the
5836 -- actual. Both of them are instances, and the renaming declarations for
5837 -- their formal parameters appear in the same order in both. The analyzed
5838 -- formal has been analyzed in the context of the current instance.
5839
5840 procedure Check_Formal_Package_Instance
5841 (Formal_Pack : Entity_Id;
5842 Actual_Pack : Entity_Id)
5843 is
5844 E1 : Entity_Id := First_Entity (Actual_Pack);
5845 E2 : Entity_Id := First_Entity (Formal_Pack);
5846 Prev_E1 : Entity_Id;
5847
5848 Expr1 : Node_Id;
5849 Expr2 : Node_Id;
5850
5851 procedure Check_Mismatch (B : Boolean);
5852 -- Common error routine for mismatch between the parameters of the
5853 -- actual instance and those of the formal package.
5854
5855 function Same_Instantiated_Constant (E1, E2 : Entity_Id) return Boolean;
5856 -- The formal may come from a nested formal package, and the actual may
5857 -- have been constant-folded. To determine whether the two denote the
5858 -- same entity we may have to traverse several definitions to recover
5859 -- the ultimate entity that they refer to.
5860
5861 function Same_Instantiated_Function (E1, E2 : Entity_Id) return Boolean;
5862 -- The formal and the actual must be identical, but if both are
5863 -- given by attributes they end up renaming different generated bodies,
5864 -- and we must verify that the attributes themselves match.
5865
5866 function Same_Instantiated_Variable (E1, E2 : Entity_Id) return Boolean;
5867 -- Similarly, if the formal comes from a nested formal package, the
5868 -- actual may designate the formal through multiple renamings, which
5869 -- have to be followed to determine the original variable in question.
5870
5871 --------------------
5872 -- Check_Mismatch --
5873 --------------------
5874
5875 procedure Check_Mismatch (B : Boolean) is
5876 -- A Formal_Type_Declaration for a derived private type is rewritten
5877 -- as a private extension decl. (see Analyze_Formal_Derived_Type),
5878 -- which is why we examine the original node.
5879
5880 Kind : constant Node_Kind := Nkind (Original_Node (Parent (E2)));
5881
5882 begin
5883 if Kind = N_Formal_Type_Declaration then
5884 return;
5885
5886 elsif Nkind_In (Kind, N_Formal_Object_Declaration,
5887 N_Formal_Package_Declaration)
5888 or else Kind in N_Formal_Subprogram_Declaration
5889 then
5890 null;
5891
5892 -- Ada 2012: If both formal and actual are incomplete types they
5893 -- are conformant.
5894
5895 elsif Is_Incomplete_Type (E1) and then Is_Incomplete_Type (E2) then
5896 null;
5897
5898 elsif B then
5899 Error_Msg_NE
5900 ("actual for & in actual instance does not match formal",
5901 Parent (Actual_Pack), E1);
5902 end if;
5903 end Check_Mismatch;
5904
5905 --------------------------------
5906 -- Same_Instantiated_Constant --
5907 --------------------------------
5908
5909 function Same_Instantiated_Constant
5910 (E1, E2 : Entity_Id) return Boolean
5911 is
5912 Ent : Entity_Id;
5913
5914 begin
5915 Ent := E2;
5916 while Present (Ent) loop
5917 if E1 = Ent then
5918 return True;
5919
5920 elsif Ekind (Ent) /= E_Constant then
5921 return False;
5922
5923 elsif Is_Entity_Name (Constant_Value (Ent)) then
5924 if Entity (Constant_Value (Ent)) = E1 then
5925 return True;
5926 else
5927 Ent := Entity (Constant_Value (Ent));
5928 end if;
5929
5930 -- The actual may be a constant that has been folded. Recover
5931 -- original name.
5932
5933 elsif Is_Entity_Name (Original_Node (Constant_Value (Ent))) then
5934 Ent := Entity (Original_Node (Constant_Value (Ent)));
5935
5936 else
5937 return False;
5938 end if;
5939 end loop;
5940
5941 return False;
5942 end Same_Instantiated_Constant;
5943
5944 --------------------------------
5945 -- Same_Instantiated_Function --
5946 --------------------------------
5947
5948 function Same_Instantiated_Function
5949 (E1, E2 : Entity_Id) return Boolean
5950 is
5951 U1, U2 : Node_Id;
5952 begin
5953 if Alias (E1) = Alias (E2) then
5954 return True;
5955
5956 elsif Present (Alias (E2)) then
5957 U1 := Original_Node (Unit_Declaration_Node (E1));
5958 U2 := Original_Node (Unit_Declaration_Node (Alias (E2)));
5959
5960 return Nkind (U1) = N_Subprogram_Renaming_Declaration
5961 and then Nkind (Name (U1)) = N_Attribute_Reference
5962
5963 and then Nkind (U2) = N_Subprogram_Renaming_Declaration
5964 and then Nkind (Name (U2)) = N_Attribute_Reference
5965
5966 and then
5967 Attribute_Name (Name (U1)) = Attribute_Name (Name (U2));
5968 else
5969 return False;
5970 end if;
5971 end Same_Instantiated_Function;
5972
5973 --------------------------------
5974 -- Same_Instantiated_Variable --
5975 --------------------------------
5976
5977 function Same_Instantiated_Variable
5978 (E1, E2 : Entity_Id) return Boolean
5979 is
5980 function Original_Entity (E : Entity_Id) return Entity_Id;
5981 -- Follow chain of renamings to the ultimate ancestor
5982
5983 ---------------------
5984 -- Original_Entity --
5985 ---------------------
5986
5987 function Original_Entity (E : Entity_Id) return Entity_Id is
5988 Orig : Entity_Id;
5989
5990 begin
5991 Orig := E;
5992 while Nkind (Parent (Orig)) = N_Object_Renaming_Declaration
5993 and then Present (Renamed_Object (Orig))
5994 and then Is_Entity_Name (Renamed_Object (Orig))
5995 loop
5996 Orig := Entity (Renamed_Object (Orig));
5997 end loop;
5998
5999 return Orig;
6000 end Original_Entity;
6001
6002 -- Start of processing for Same_Instantiated_Variable
6003
6004 begin
6005 return Ekind (E1) = Ekind (E2)
6006 and then Original_Entity (E1) = Original_Entity (E2);
6007 end Same_Instantiated_Variable;
6008
6009 -- Start of processing for Check_Formal_Package_Instance
6010
6011 begin
6012 Prev_E1 := E1;
6013 while Present (E1) and then Present (E2) loop
6014 exit when Ekind (E1) = E_Package
6015 and then Renamed_Entity (E1) = Renamed_Entity (Actual_Pack);
6016
6017 -- If the formal is the renaming of the formal package, this
6018 -- is the end of its formal part, which may occur before the
6019 -- end of the formal part in the actual in the presence of
6020 -- defaulted parameters in the formal package.
6021
6022 exit when Nkind (Parent (E2)) = N_Package_Renaming_Declaration
6023 and then Renamed_Entity (E2) = Scope (E2);
6024
6025 -- The analysis of the actual may generate additional internal
6026 -- entities. If the formal is defaulted, there is no corresponding
6027 -- analysis and the internal entities must be skipped, until we
6028 -- find corresponding entities again.
6029
6030 if Comes_From_Source (E2)
6031 and then not Comes_From_Source (E1)
6032 and then Chars (E1) /= Chars (E2)
6033 then
6034 while Present (E1) and then Chars (E1) /= Chars (E2) loop
6035 Next_Entity (E1);
6036 end loop;
6037 end if;
6038
6039 if No (E1) then
6040 return;
6041
6042 -- Entities may be declared without full declaration, such as
6043 -- itypes and predefined operators (concatenation for arrays, eg).
6044 -- Skip it and keep the formal entity to find a later match for it.
6045
6046 elsif No (Parent (E2)) and then Ekind (E1) /= Ekind (E2) then
6047 E1 := Prev_E1;
6048 goto Next_E;
6049
6050 -- If the formal entity comes from a formal declaration, it was
6051 -- defaulted in the formal package, and no check is needed on it.
6052
6053 elsif Nkind_In (Original_Node (Parent (E2)),
6054 N_Formal_Object_Declaration,
6055 N_Formal_Type_Declaration)
6056 then
6057 -- If the formal is a tagged type the corresponding class-wide
6058 -- type has been generated as well, and it must be skipped.
6059
6060 if Is_Type (E2) and then Is_Tagged_Type (E2) then
6061 Next_Entity (E2);
6062 end if;
6063
6064 goto Next_E;
6065
6066 -- Ditto for defaulted formal subprograms.
6067
6068 elsif Is_Overloadable (E1)
6069 and then Nkind (Unit_Declaration_Node (E2)) in
6070 N_Formal_Subprogram_Declaration
6071 then
6072 goto Next_E;
6073
6074 elsif Is_Type (E1) then
6075
6076 -- Subtypes must statically match. E1, E2 are the local entities
6077 -- that are subtypes of the actuals. Itypes generated for other
6078 -- parameters need not be checked, the check will be performed
6079 -- on the parameters themselves.
6080
6081 -- If E2 is a formal type declaration, it is a defaulted parameter
6082 -- and needs no checking.
6083
6084 if not Is_Itype (E1) and then not Is_Itype (E2) then
6085 Check_Mismatch
6086 (not Is_Type (E2)
6087 or else Etype (E1) /= Etype (E2)
6088 or else not Subtypes_Statically_Match (E1, E2));
6089 end if;
6090
6091 elsif Ekind (E1) = E_Constant then
6092
6093 -- IN parameters must denote the same static value, or the same
6094 -- constant, or the literal null.
6095
6096 Expr1 := Expression (Parent (E1));
6097
6098 if Ekind (E2) /= E_Constant then
6099 Check_Mismatch (True);
6100 goto Next_E;
6101 else
6102 Expr2 := Expression (Parent (E2));
6103 end if;
6104
6105 if Is_OK_Static_Expression (Expr1) then
6106 if not Is_OK_Static_Expression (Expr2) then
6107 Check_Mismatch (True);
6108
6109 elsif Is_Discrete_Type (Etype (E1)) then
6110 declare
6111 V1 : constant Uint := Expr_Value (Expr1);
6112 V2 : constant Uint := Expr_Value (Expr2);
6113 begin
6114 Check_Mismatch (V1 /= V2);
6115 end;
6116
6117 elsif Is_Real_Type (Etype (E1)) then
6118 declare
6119 V1 : constant Ureal := Expr_Value_R (Expr1);
6120 V2 : constant Ureal := Expr_Value_R (Expr2);
6121 begin
6122 Check_Mismatch (V1 /= V2);
6123 end;
6124
6125 elsif Is_String_Type (Etype (E1))
6126 and then Nkind (Expr1) = N_String_Literal
6127 then
6128 if Nkind (Expr2) /= N_String_Literal then
6129 Check_Mismatch (True);
6130 else
6131 Check_Mismatch
6132 (not String_Equal (Strval (Expr1), Strval (Expr2)));
6133 end if;
6134 end if;
6135
6136 elsif Is_Entity_Name (Expr1) then
6137 if Is_Entity_Name (Expr2) then
6138 if Entity (Expr1) = Entity (Expr2) then
6139 null;
6140 else
6141 Check_Mismatch
6142 (not Same_Instantiated_Constant
6143 (Entity (Expr1), Entity (Expr2)));
6144 end if;
6145
6146 else
6147 Check_Mismatch (True);
6148 end if;
6149
6150 elsif Is_Entity_Name (Original_Node (Expr1))
6151 and then Is_Entity_Name (Expr2)
6152 and then Same_Instantiated_Constant
6153 (Entity (Original_Node (Expr1)), Entity (Expr2))
6154 then
6155 null;
6156
6157 elsif Nkind (Expr1) = N_Null then
6158 Check_Mismatch (Nkind (Expr1) /= N_Null);
6159
6160 else
6161 Check_Mismatch (True);
6162 end if;
6163
6164 elsif Ekind (E1) = E_Variable then
6165 Check_Mismatch (not Same_Instantiated_Variable (E1, E2));
6166
6167 elsif Ekind (E1) = E_Package then
6168 Check_Mismatch
6169 (Ekind (E1) /= Ekind (E2)
6170 or else Renamed_Object (E1) /= Renamed_Object (E2));
6171
6172 elsif Is_Overloadable (E1) then
6173
6174 -- Verify that the actual subprograms match. Note that actuals
6175 -- that are attributes are rewritten as subprograms. If the
6176 -- subprogram in the formal package is defaulted, no check is
6177 -- needed. Note that this can only happen in Ada 2005 when the
6178 -- formal package can be partially parameterized.
6179
6180 if Nkind (Unit_Declaration_Node (E1)) =
6181 N_Subprogram_Renaming_Declaration
6182 and then From_Default (Unit_Declaration_Node (E1))
6183 then
6184 null;
6185
6186 -- If the formal package has an "others" box association that
6187 -- covers this formal, there is no need for a check either.
6188
6189 elsif Nkind (Unit_Declaration_Node (E2)) in
6190 N_Formal_Subprogram_Declaration
6191 and then Box_Present (Unit_Declaration_Node (E2))
6192 then
6193 null;
6194
6195 -- No check needed if subprogram is a defaulted null procedure
6196
6197 elsif No (Alias (E2))
6198 and then Ekind (E2) = E_Procedure
6199 and then
6200 Null_Present (Specification (Unit_Declaration_Node (E2)))
6201 then
6202 null;
6203
6204 -- Otherwise the actual in the formal and the actual in the
6205 -- instantiation of the formal must match, up to renamings.
6206
6207 else
6208 Check_Mismatch
6209 (Ekind (E2) /= Ekind (E1)
6210 or else not Same_Instantiated_Function (E1, E2));
6211 end if;
6212
6213 else
6214 raise Program_Error;
6215 end if;
6216
6217 <<Next_E>>
6218 Prev_E1 := E1;
6219 Next_Entity (E1);
6220 Next_Entity (E2);
6221 end loop;
6222 end Check_Formal_Package_Instance;
6223
6224 ---------------------------
6225 -- Check_Formal_Packages --
6226 ---------------------------
6227
6228 procedure Check_Formal_Packages (P_Id : Entity_Id) is
6229 E : Entity_Id;
6230 Formal_P : Entity_Id;
6231 Formal_Decl : Node_Id;
6232
6233 begin
6234 -- Iterate through the declarations in the instance, looking for package
6235 -- renaming declarations that denote instances of formal packages. Stop
6236 -- when we find the renaming of the current package itself. The
6237 -- declaration for a formal package without a box is followed by an
6238 -- internal entity that repeats the instantiation.
6239
6240 E := First_Entity (P_Id);
6241 while Present (E) loop
6242 if Ekind (E) = E_Package then
6243 if Renamed_Object (E) = P_Id then
6244 exit;
6245
6246 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
6247 null;
6248
6249 else
6250 Formal_Decl := Parent (Associated_Formal_Package (E));
6251
6252 -- Nothing to check if the formal has a box or an others_clause
6253 -- (necessarily with a box).
6254
6255 if Box_Present (Formal_Decl) then
6256 null;
6257
6258 elsif Nkind (First (Generic_Associations (Formal_Decl))) =
6259 N_Others_Choice
6260 then
6261 -- The internal validating package was generated but formal
6262 -- and instance are known to be compatible.
6263
6264 Formal_P := Next_Entity (E);
6265 Remove (Unit_Declaration_Node (Formal_P));
6266
6267 else
6268 Formal_P := Next_Entity (E);
6269 Check_Formal_Package_Instance (Formal_P, E);
6270
6271 -- After checking, remove the internal validating package.
6272 -- It is only needed for semantic checks, and as it may
6273 -- contain generic formal declarations it should not reach
6274 -- gigi.
6275
6276 Remove (Unit_Declaration_Node (Formal_P));
6277 end if;
6278 end if;
6279 end if;
6280
6281 Next_Entity (E);
6282 end loop;
6283 end Check_Formal_Packages;
6284
6285 ---------------------------------
6286 -- Check_Forward_Instantiation --
6287 ---------------------------------
6288
6289 procedure Check_Forward_Instantiation (Decl : Node_Id) is
6290 S : Entity_Id;
6291 Gen_Comp : Entity_Id := Cunit_Entity (Get_Source_Unit (Decl));
6292
6293 begin
6294 -- The instantiation appears before the generic body if we are in the
6295 -- scope of the unit containing the generic, either in its spec or in
6296 -- the package body, and before the generic body.
6297
6298 if Ekind (Gen_Comp) = E_Package_Body then
6299 Gen_Comp := Spec_Entity (Gen_Comp);
6300 end if;
6301
6302 if In_Open_Scopes (Gen_Comp)
6303 and then No (Corresponding_Body (Decl))
6304 then
6305 S := Current_Scope;
6306
6307 while Present (S)
6308 and then not Is_Compilation_Unit (S)
6309 and then not Is_Child_Unit (S)
6310 loop
6311 if Ekind (S) = E_Package then
6312 Set_Has_Forward_Instantiation (S);
6313 end if;
6314
6315 S := Scope (S);
6316 end loop;
6317 end if;
6318 end Check_Forward_Instantiation;
6319
6320 ---------------------------
6321 -- Check_Generic_Actuals --
6322 ---------------------------
6323
6324 -- The visibility of the actuals may be different between the point of
6325 -- generic instantiation and the instantiation of the body.
6326
6327 procedure Check_Generic_Actuals
6328 (Instance : Entity_Id;
6329 Is_Formal_Box : Boolean)
6330 is
6331 E : Entity_Id;
6332 Astype : Entity_Id;
6333
6334 function Denotes_Previous_Actual (Typ : Entity_Id) return Boolean;
6335 -- For a formal that is an array type, the component type is often a
6336 -- previous formal in the same unit. The privacy status of the component
6337 -- type will have been examined earlier in the traversal of the
6338 -- corresponding actuals, and this status should not be modified for
6339 -- the array (sub)type itself. However, if the base type of the array
6340 -- (sub)type is private, its full view must be restored in the body to
6341 -- be consistent with subsequent index subtypes, etc.
6342 --
6343 -- To detect this case we have to rescan the list of formals, which is
6344 -- usually short enough to ignore the resulting inefficiency.
6345
6346 -----------------------------
6347 -- Denotes_Previous_Actual --
6348 -----------------------------
6349
6350 function Denotes_Previous_Actual (Typ : Entity_Id) return Boolean is
6351 Prev : Entity_Id;
6352
6353 begin
6354 Prev := First_Entity (Instance);
6355 while Present (Prev) loop
6356 if Is_Type (Prev)
6357 and then Nkind (Parent (Prev)) = N_Subtype_Declaration
6358 and then Is_Entity_Name (Subtype_Indication (Parent (Prev)))
6359 and then Entity (Subtype_Indication (Parent (Prev))) = Typ
6360 then
6361 return True;
6362
6363 elsif Prev = E then
6364 return False;
6365
6366 else
6367 Next_Entity (Prev);
6368 end if;
6369 end loop;
6370
6371 return False;
6372 end Denotes_Previous_Actual;
6373
6374 -- Start of processing for Check_Generic_Actuals
6375
6376 begin
6377 E := First_Entity (Instance);
6378 while Present (E) loop
6379 if Is_Type (E)
6380 and then Nkind (Parent (E)) = N_Subtype_Declaration
6381 and then Scope (Etype (E)) /= Instance
6382 and then Is_Entity_Name (Subtype_Indication (Parent (E)))
6383 then
6384 if Is_Array_Type (E)
6385 and then not Is_Private_Type (Etype (E))
6386 and then Denotes_Previous_Actual (Component_Type (E))
6387 then
6388 null;
6389 else
6390 Check_Private_View (Subtype_Indication (Parent (E)));
6391 end if;
6392
6393 Set_Is_Generic_Actual_Type (E, True);
6394 Set_Is_Hidden (E, False);
6395 Set_Is_Potentially_Use_Visible (E, In_Use (Instance));
6396
6397 -- We constructed the generic actual type as a subtype of the
6398 -- supplied type. This means that it normally would not inherit
6399 -- subtype specific attributes of the actual, which is wrong for
6400 -- the generic case.
6401
6402 Astype := Ancestor_Subtype (E);
6403
6404 if No (Astype) then
6405
6406 -- This can happen when E is an itype that is the full view of
6407 -- a private type completed, e.g. with a constrained array. In
6408 -- that case, use the first subtype, which will carry size
6409 -- information. The base type itself is unconstrained and will
6410 -- not carry it.
6411
6412 Astype := First_Subtype (E);
6413 end if;
6414
6415 Set_Size_Info (E, (Astype));
6416 Set_RM_Size (E, RM_Size (Astype));
6417 Set_First_Rep_Item (E, First_Rep_Item (Astype));
6418
6419 if Is_Discrete_Or_Fixed_Point_Type (E) then
6420 Set_RM_Size (E, RM_Size (Astype));
6421
6422 -- In nested instances, the base type of an access actual may
6423 -- itself be private, and need to be exchanged.
6424
6425 elsif Is_Access_Type (E)
6426 and then Is_Private_Type (Etype (E))
6427 then
6428 Check_Private_View
6429 (New_Occurrence_Of (Etype (E), Sloc (Instance)));
6430 end if;
6431
6432 elsif Ekind (E) = E_Package then
6433
6434 -- If this is the renaming for the current instance, we're done.
6435 -- Otherwise it is a formal package. If the corresponding formal
6436 -- was declared with a box, the (instantiations of the) generic
6437 -- formal part are also visible. Otherwise, ignore the entity
6438 -- created to validate the actuals.
6439
6440 if Renamed_Object (E) = Instance then
6441 exit;
6442
6443 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
6444 null;
6445
6446 -- The visibility of a formal of an enclosing generic is already
6447 -- correct.
6448
6449 elsif Denotes_Formal_Package (E) then
6450 null;
6451
6452 elsif Present (Associated_Formal_Package (E))
6453 and then not Is_Generic_Formal (E)
6454 then
6455 if Box_Present (Parent (Associated_Formal_Package (E))) then
6456 Check_Generic_Actuals (Renamed_Object (E), True);
6457
6458 else
6459 Check_Generic_Actuals (Renamed_Object (E), False);
6460 end if;
6461
6462 Set_Is_Hidden (E, False);
6463 end if;
6464
6465 -- If this is a subprogram instance (in a wrapper package) the
6466 -- actual is fully visible.
6467
6468 elsif Is_Wrapper_Package (Instance) then
6469 Set_Is_Hidden (E, False);
6470
6471 -- If the formal package is declared with a box, or if the formal
6472 -- parameter is defaulted, it is visible in the body.
6473
6474 elsif Is_Formal_Box or else Is_Visible_Formal (E) then
6475 Set_Is_Hidden (E, False);
6476 end if;
6477
6478 if Ekind (E) = E_Constant then
6479
6480 -- If the type of the actual is a private type declared in the
6481 -- enclosing scope of the generic unit, the body of the generic
6482 -- sees the full view of the type (because it has to appear in
6483 -- the corresponding package body). If the type is private now,
6484 -- exchange views to restore the proper visiblity in the instance.
6485
6486 declare
6487 Typ : constant Entity_Id := Base_Type (Etype (E));
6488 -- The type of the actual
6489
6490 Gen_Id : Entity_Id;
6491 -- The generic unit
6492
6493 Parent_Scope : Entity_Id;
6494 -- The enclosing scope of the generic unit
6495
6496 begin
6497 if Is_Wrapper_Package (Instance) then
6498 Gen_Id :=
6499 Generic_Parent
6500 (Specification
6501 (Unit_Declaration_Node
6502 (Related_Instance (Instance))));
6503 else
6504 Gen_Id :=
6505 Generic_Parent (Package_Specification (Instance));
6506 end if;
6507
6508 Parent_Scope := Scope (Gen_Id);
6509
6510 -- The exchange is only needed if the generic is defined
6511 -- within a package which is not a common ancestor of the
6512 -- scope of the instance, and is not already in scope.
6513
6514 if Is_Private_Type (Typ)
6515 and then Scope (Typ) = Parent_Scope
6516 and then Scope (Instance) /= Parent_Scope
6517 and then Ekind (Parent_Scope) = E_Package
6518 and then not Is_Child_Unit (Gen_Id)
6519 then
6520 Switch_View (Typ);
6521
6522 -- If the type of the entity is a subtype, it may also have
6523 -- to be made visible, together with the base type of its
6524 -- full view, after exchange.
6525
6526 if Is_Private_Type (Etype (E)) then
6527 Switch_View (Etype (E));
6528 Switch_View (Base_Type (Etype (E)));
6529 end if;
6530 end if;
6531 end;
6532 end if;
6533
6534 Next_Entity (E);
6535 end loop;
6536 end Check_Generic_Actuals;
6537
6538 ------------------------------
6539 -- Check_Generic_Child_Unit --
6540 ------------------------------
6541
6542 procedure Check_Generic_Child_Unit
6543 (Gen_Id : Node_Id;
6544 Parent_Installed : in out Boolean)
6545 is
6546 Loc : constant Source_Ptr := Sloc (Gen_Id);
6547 Gen_Par : Entity_Id := Empty;
6548 E : Entity_Id;
6549 Inst_Par : Entity_Id;
6550 S : Node_Id;
6551
6552 function Find_Generic_Child
6553 (Scop : Entity_Id;
6554 Id : Node_Id) return Entity_Id;
6555 -- Search generic parent for possible child unit with the given name
6556
6557 function In_Enclosing_Instance return Boolean;
6558 -- Within an instance of the parent, the child unit may be denoted by
6559 -- a simple name, or an abbreviated expanded name. Examine enclosing
6560 -- scopes to locate a possible parent instantiation.
6561
6562 ------------------------
6563 -- Find_Generic_Child --
6564 ------------------------
6565
6566 function Find_Generic_Child
6567 (Scop : Entity_Id;
6568 Id : Node_Id) return Entity_Id
6569 is
6570 E : Entity_Id;
6571
6572 begin
6573 -- If entity of name is already set, instance has already been
6574 -- resolved, e.g. in an enclosing instantiation.
6575
6576 if Present (Entity (Id)) then
6577 if Scope (Entity (Id)) = Scop then
6578 return Entity (Id);
6579 else
6580 return Empty;
6581 end if;
6582
6583 else
6584 E := First_Entity (Scop);
6585 while Present (E) loop
6586 if Chars (E) = Chars (Id)
6587 and then Is_Child_Unit (E)
6588 then
6589 if Is_Child_Unit (E)
6590 and then not Is_Visible_Lib_Unit (E)
6591 then
6592 Error_Msg_NE
6593 ("generic child unit& is not visible", Gen_Id, E);
6594 end if;
6595
6596 Set_Entity (Id, E);
6597 return E;
6598 end if;
6599
6600 Next_Entity (E);
6601 end loop;
6602
6603 return Empty;
6604 end if;
6605 end Find_Generic_Child;
6606
6607 ---------------------------
6608 -- In_Enclosing_Instance --
6609 ---------------------------
6610
6611 function In_Enclosing_Instance return Boolean is
6612 Enclosing_Instance : Node_Id;
6613 Instance_Decl : Node_Id;
6614
6615 begin
6616 -- We do not inline any call that contains instantiations, except
6617 -- for instantiations of Unchecked_Conversion, so if we are within
6618 -- an inlined body the current instance does not require parents.
6619
6620 if In_Inlined_Body then
6621 pragma Assert (Chars (Gen_Id) = Name_Unchecked_Conversion);
6622 return False;
6623 end if;
6624
6625 -- Loop to check enclosing scopes
6626
6627 Enclosing_Instance := Current_Scope;
6628 while Present (Enclosing_Instance) loop
6629 Instance_Decl := Unit_Declaration_Node (Enclosing_Instance);
6630
6631 if Ekind (Enclosing_Instance) = E_Package
6632 and then Is_Generic_Instance (Enclosing_Instance)
6633 and then Present
6634 (Generic_Parent (Specification (Instance_Decl)))
6635 then
6636 -- Check whether the generic we are looking for is a child of
6637 -- this instance.
6638
6639 E := Find_Generic_Child
6640 (Generic_Parent (Specification (Instance_Decl)), Gen_Id);
6641 exit when Present (E);
6642
6643 else
6644 E := Empty;
6645 end if;
6646
6647 Enclosing_Instance := Scope (Enclosing_Instance);
6648 end loop;
6649
6650 if No (E) then
6651
6652 -- Not a child unit
6653
6654 Analyze (Gen_Id);
6655 return False;
6656
6657 else
6658 Rewrite (Gen_Id,
6659 Make_Expanded_Name (Loc,
6660 Chars => Chars (E),
6661 Prefix => New_Occurrence_Of (Enclosing_Instance, Loc),
6662 Selector_Name => New_Occurrence_Of (E, Loc)));
6663
6664 Set_Entity (Gen_Id, E);
6665 Set_Etype (Gen_Id, Etype (E));
6666 Parent_Installed := False; -- Already in scope.
6667 return True;
6668 end if;
6669 end In_Enclosing_Instance;
6670
6671 -- Start of processing for Check_Generic_Child_Unit
6672
6673 begin
6674 -- If the name of the generic is given by a selected component, it may
6675 -- be the name of a generic child unit, and the prefix is the name of an
6676 -- instance of the parent, in which case the child unit must be visible.
6677 -- If this instance is not in scope, it must be placed there and removed
6678 -- after instantiation, because what is being instantiated is not the
6679 -- original child, but the corresponding child present in the instance
6680 -- of the parent.
6681
6682 -- If the child is instantiated within the parent, it can be given by
6683 -- a simple name. In this case the instance is already in scope, but
6684 -- the child generic must be recovered from the generic parent as well.
6685
6686 if Nkind (Gen_Id) = N_Selected_Component then
6687 S := Selector_Name (Gen_Id);
6688 Analyze (Prefix (Gen_Id));
6689 Inst_Par := Entity (Prefix (Gen_Id));
6690
6691 if Ekind (Inst_Par) = E_Package
6692 and then Present (Renamed_Object (Inst_Par))
6693 then
6694 Inst_Par := Renamed_Object (Inst_Par);
6695 end if;
6696
6697 if Ekind (Inst_Par) = E_Package then
6698 if Nkind (Parent (Inst_Par)) = N_Package_Specification then
6699 Gen_Par := Generic_Parent (Parent (Inst_Par));
6700
6701 elsif Nkind (Parent (Inst_Par)) = N_Defining_Program_Unit_Name
6702 and then
6703 Nkind (Parent (Parent (Inst_Par))) = N_Package_Specification
6704 then
6705 Gen_Par := Generic_Parent (Parent (Parent (Inst_Par)));
6706 end if;
6707
6708 elsif Ekind (Inst_Par) = E_Generic_Package
6709 and then Nkind (Parent (Gen_Id)) = N_Formal_Package_Declaration
6710 then
6711 -- A formal package may be a real child package, and not the
6712 -- implicit instance within a parent. In this case the child is
6713 -- not visible and has to be retrieved explicitly as well.
6714
6715 Gen_Par := Inst_Par;
6716 end if;
6717
6718 if Present (Gen_Par) then
6719
6720 -- The prefix denotes an instantiation. The entity itself may be a
6721 -- nested generic, or a child unit.
6722
6723 E := Find_Generic_Child (Gen_Par, S);
6724
6725 if Present (E) then
6726 Change_Selected_Component_To_Expanded_Name (Gen_Id);
6727 Set_Entity (Gen_Id, E);
6728 Set_Etype (Gen_Id, Etype (E));
6729 Set_Entity (S, E);
6730 Set_Etype (S, Etype (E));
6731
6732 -- Indicate that this is a reference to the parent
6733
6734 if In_Extended_Main_Source_Unit (Gen_Id) then
6735 Set_Is_Instantiated (Inst_Par);
6736 end if;
6737
6738 -- A common mistake is to replicate the naming scheme of a
6739 -- hierarchy by instantiating a generic child directly, rather
6740 -- than the implicit child in a parent instance:
6741
6742 -- generic .. package Gpar is ..
6743 -- generic .. package Gpar.Child is ..
6744 -- package Par is new Gpar ();
6745
6746 -- with Gpar.Child;
6747 -- package Par.Child is new Gpar.Child ();
6748 -- rather than Par.Child
6749
6750 -- In this case the instantiation is within Par, which is an
6751 -- instance, but Gpar does not denote Par because we are not IN
6752 -- the instance of Gpar, so this is illegal. The test below
6753 -- recognizes this particular case.
6754
6755 if Is_Child_Unit (E)
6756 and then not Comes_From_Source (Entity (Prefix (Gen_Id)))
6757 and then (not In_Instance
6758 or else Nkind (Parent (Parent (Gen_Id))) =
6759 N_Compilation_Unit)
6760 then
6761 Error_Msg_N
6762 ("prefix of generic child unit must be instance of parent",
6763 Gen_Id);
6764 end if;
6765
6766 if not In_Open_Scopes (Inst_Par)
6767 and then Nkind (Parent (Gen_Id)) not in
6768 N_Generic_Renaming_Declaration
6769 then
6770 Install_Parent (Inst_Par);
6771 Parent_Installed := True;
6772
6773 elsif In_Open_Scopes (Inst_Par) then
6774
6775 -- If the parent is already installed, install the actuals
6776 -- for its formal packages. This is necessary when the child
6777 -- instance is a child of the parent instance: in this case,
6778 -- the parent is placed on the scope stack but the formal
6779 -- packages are not made visible.
6780
6781 Install_Formal_Packages (Inst_Par);
6782 end if;
6783
6784 else
6785 -- If the generic parent does not contain an entity that
6786 -- corresponds to the selector, the instance doesn't either.
6787 -- Analyzing the node will yield the appropriate error message.
6788 -- If the entity is not a child unit, then it is an inner
6789 -- generic in the parent.
6790
6791 Analyze (Gen_Id);
6792 end if;
6793
6794 else
6795 Analyze (Gen_Id);
6796
6797 if Is_Child_Unit (Entity (Gen_Id))
6798 and then
6799 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
6800 and then not In_Open_Scopes (Inst_Par)
6801 then
6802 Install_Parent (Inst_Par);
6803 Parent_Installed := True;
6804
6805 -- The generic unit may be the renaming of the implicit child
6806 -- present in an instance. In that case the parent instance is
6807 -- obtained from the name of the renamed entity.
6808
6809 elsif Ekind (Entity (Gen_Id)) = E_Generic_Package
6810 and then Present (Renamed_Entity (Entity (Gen_Id)))
6811 and then Is_Child_Unit (Renamed_Entity (Entity (Gen_Id)))
6812 then
6813 declare
6814 Renamed_Package : constant Node_Id :=
6815 Name (Parent (Entity (Gen_Id)));
6816 begin
6817 if Nkind (Renamed_Package) = N_Expanded_Name then
6818 Inst_Par := Entity (Prefix (Renamed_Package));
6819 Install_Parent (Inst_Par);
6820 Parent_Installed := True;
6821 end if;
6822 end;
6823 end if;
6824 end if;
6825
6826 elsif Nkind (Gen_Id) = N_Expanded_Name then
6827
6828 -- Entity already present, analyze prefix, whose meaning may be an
6829 -- instance in the current context. If it is an instance of a
6830 -- relative within another, the proper parent may still have to be
6831 -- installed, if they are not of the same generation.
6832
6833 Analyze (Prefix (Gen_Id));
6834
6835 -- Prevent cascaded errors
6836
6837 if Etype (Prefix (Gen_Id)) = Any_Type then
6838 return;
6839 end if;
6840
6841 -- In the unlikely case that a local declaration hides the name of
6842 -- the parent package, locate it on the homonym chain. If the context
6843 -- is an instance of the parent, the renaming entity is flagged as
6844 -- such.
6845
6846 Inst_Par := Entity (Prefix (Gen_Id));
6847 while Present (Inst_Par)
6848 and then not Is_Package_Or_Generic_Package (Inst_Par)
6849 loop
6850 Inst_Par := Homonym (Inst_Par);
6851 end loop;
6852
6853 pragma Assert (Present (Inst_Par));
6854 Set_Entity (Prefix (Gen_Id), Inst_Par);
6855
6856 if In_Enclosing_Instance then
6857 null;
6858
6859 elsif Present (Entity (Gen_Id))
6860 and then Is_Child_Unit (Entity (Gen_Id))
6861 and then not In_Open_Scopes (Inst_Par)
6862 then
6863 Install_Parent (Inst_Par);
6864 Parent_Installed := True;
6865 end if;
6866
6867 elsif In_Enclosing_Instance then
6868
6869 -- The child unit is found in some enclosing scope
6870
6871 null;
6872
6873 else
6874 Analyze (Gen_Id);
6875
6876 -- If this is the renaming of the implicit child in a parent
6877 -- instance, recover the parent name and install it.
6878
6879 if Is_Entity_Name (Gen_Id) then
6880 E := Entity (Gen_Id);
6881
6882 if Is_Generic_Unit (E)
6883 and then Nkind (Parent (E)) in N_Generic_Renaming_Declaration
6884 and then Is_Child_Unit (Renamed_Object (E))
6885 and then Is_Generic_Unit (Scope (Renamed_Object (E)))
6886 and then Nkind (Name (Parent (E))) = N_Expanded_Name
6887 then
6888 Rewrite (Gen_Id, New_Copy_Tree (Name (Parent (E))));
6889 Inst_Par := Entity (Prefix (Gen_Id));
6890
6891 if not In_Open_Scopes (Inst_Par) then
6892 Install_Parent (Inst_Par);
6893 Parent_Installed := True;
6894 end if;
6895
6896 -- If it is a child unit of a non-generic parent, it may be
6897 -- use-visible and given by a direct name. Install parent as
6898 -- for other cases.
6899
6900 elsif Is_Generic_Unit (E)
6901 and then Is_Child_Unit (E)
6902 and then
6903 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
6904 and then not Is_Generic_Unit (Scope (E))
6905 then
6906 if not In_Open_Scopes (Scope (E)) then
6907 Install_Parent (Scope (E));
6908 Parent_Installed := True;
6909 end if;
6910 end if;
6911 end if;
6912 end if;
6913 end Check_Generic_Child_Unit;
6914
6915 -----------------------------
6916 -- Check_Hidden_Child_Unit --
6917 -----------------------------
6918
6919 procedure Check_Hidden_Child_Unit
6920 (N : Node_Id;
6921 Gen_Unit : Entity_Id;
6922 Act_Decl_Id : Entity_Id)
6923 is
6924 Gen_Id : constant Node_Id := Name (N);
6925
6926 begin
6927 if Is_Child_Unit (Gen_Unit)
6928 and then Is_Child_Unit (Act_Decl_Id)
6929 and then Nkind (Gen_Id) = N_Expanded_Name
6930 and then Entity (Prefix (Gen_Id)) = Scope (Act_Decl_Id)
6931 and then Chars (Gen_Unit) = Chars (Act_Decl_Id)
6932 then
6933 Error_Msg_Node_2 := Scope (Act_Decl_Id);
6934 Error_Msg_NE
6935 ("generic unit & is implicitly declared in &",
6936 Defining_Unit_Name (N), Gen_Unit);
6937 Error_Msg_N ("\instance must have different name",
6938 Defining_Unit_Name (N));
6939 end if;
6940 end Check_Hidden_Child_Unit;
6941
6942 ------------------------
6943 -- Check_Private_View --
6944 ------------------------
6945
6946 procedure Check_Private_View (N : Node_Id) is
6947 T : constant Entity_Id := Etype (N);
6948 BT : Entity_Id;
6949
6950 begin
6951 -- Exchange views if the type was not private in the generic but is
6952 -- private at the point of instantiation. Do not exchange views if
6953 -- the scope of the type is in scope. This can happen if both generic
6954 -- and instance are sibling units, or if type is defined in a parent.
6955 -- In this case the visibility of the type will be correct for all
6956 -- semantic checks.
6957
6958 if Present (T) then
6959 BT := Base_Type (T);
6960
6961 if Is_Private_Type (T)
6962 and then not Has_Private_View (N)
6963 and then Present (Full_View (T))
6964 and then not In_Open_Scopes (Scope (T))
6965 then
6966 -- In the generic, the full type was visible. Save the private
6967 -- entity, for subsequent exchange.
6968
6969 Switch_View (T);
6970
6971 elsif Has_Private_View (N)
6972 and then not Is_Private_Type (T)
6973 and then not Has_Been_Exchanged (T)
6974 and then Etype (Get_Associated_Node (N)) /= T
6975 then
6976 -- Only the private declaration was visible in the generic. If
6977 -- the type appears in a subtype declaration, the subtype in the
6978 -- instance must have a view compatible with that of its parent,
6979 -- which must be exchanged (see corresponding code in Restore_
6980 -- Private_Views). Otherwise, if the type is defined in a parent
6981 -- unit, leave full visibility within instance, which is safe.
6982
6983 if In_Open_Scopes (Scope (Base_Type (T)))
6984 and then not Is_Private_Type (Base_Type (T))
6985 and then Comes_From_Source (Base_Type (T))
6986 then
6987 null;
6988
6989 elsif Nkind (Parent (N)) = N_Subtype_Declaration
6990 or else not In_Private_Part (Scope (Base_Type (T)))
6991 then
6992 Prepend_Elmt (T, Exchanged_Views);
6993 Exchange_Declarations (Etype (Get_Associated_Node (N)));
6994 end if;
6995
6996 -- For composite types with inconsistent representation exchange
6997 -- component types accordingly.
6998
6999 elsif Is_Access_Type (T)
7000 and then Is_Private_Type (Designated_Type (T))
7001 and then not Has_Private_View (N)
7002 and then Present (Full_View (Designated_Type (T)))
7003 then
7004 Switch_View (Designated_Type (T));
7005
7006 elsif Is_Array_Type (T) then
7007 if Is_Private_Type (Component_Type (T))
7008 and then not Has_Private_View (N)
7009 and then Present (Full_View (Component_Type (T)))
7010 then
7011 Switch_View (Component_Type (T));
7012 end if;
7013
7014 -- The normal exchange mechanism relies on the setting of a
7015 -- flag on the reference in the generic. However, an additional
7016 -- mechanism is needed for types that are not explicitly
7017 -- mentioned in the generic, but may be needed in expanded code
7018 -- in the instance. This includes component types of arrays and
7019 -- designated types of access types. This processing must also
7020 -- include the index types of arrays which we take care of here.
7021
7022 declare
7023 Indx : Node_Id;
7024 Typ : Entity_Id;
7025
7026 begin
7027 Indx := First_Index (T);
7028 while Present (Indx) loop
7029 Typ := Base_Type (Etype (Indx));
7030
7031 if Is_Private_Type (Typ)
7032 and then Present (Full_View (Typ))
7033 then
7034 Switch_View (Typ);
7035 end if;
7036
7037 Next_Index (Indx);
7038 end loop;
7039 end;
7040
7041 elsif Is_Private_Type (T)
7042 and then Present (Full_View (T))
7043 and then Is_Array_Type (Full_View (T))
7044 and then Is_Private_Type (Component_Type (Full_View (T)))
7045 then
7046 Switch_View (T);
7047
7048 -- Finally, a non-private subtype may have a private base type, which
7049 -- must be exchanged for consistency. This can happen when a package
7050 -- body is instantiated, when the scope stack is empty but in fact
7051 -- the subtype and the base type are declared in an enclosing scope.
7052
7053 -- Note that in this case we introduce an inconsistency in the view
7054 -- set, because we switch the base type BT, but there could be some
7055 -- private dependent subtypes of BT which remain unswitched. Such
7056 -- subtypes might need to be switched at a later point (see specific
7057 -- provision for that case in Switch_View).
7058
7059 elsif not Is_Private_Type (T)
7060 and then not Has_Private_View (N)
7061 and then Is_Private_Type (BT)
7062 and then Present (Full_View (BT))
7063 and then not Is_Generic_Type (BT)
7064 and then not In_Open_Scopes (BT)
7065 then
7066 Prepend_Elmt (Full_View (BT), Exchanged_Views);
7067 Exchange_Declarations (BT);
7068 end if;
7069 end if;
7070 end Check_Private_View;
7071
7072 -----------------------------
7073 -- Check_Hidden_Primitives --
7074 -----------------------------
7075
7076 function Check_Hidden_Primitives (Assoc_List : List_Id) return Elist_Id is
7077 Actual : Node_Id;
7078 Gen_T : Entity_Id;
7079 Result : Elist_Id := No_Elist;
7080
7081 begin
7082 if No (Assoc_List) then
7083 return No_Elist;
7084 end if;
7085
7086 -- Traverse the list of associations between formals and actuals
7087 -- searching for renamings of tagged types
7088
7089 Actual := First (Assoc_List);
7090 while Present (Actual) loop
7091 if Nkind (Actual) = N_Subtype_Declaration then
7092 Gen_T := Generic_Parent_Type (Actual);
7093
7094 if Present (Gen_T) and then Is_Tagged_Type (Gen_T) then
7095
7096 -- Traverse the list of primitives of the actual types
7097 -- searching for hidden primitives that are visible in the
7098 -- corresponding generic formal; leave them visible and
7099 -- append them to Result to restore their decoration later.
7100
7101 Install_Hidden_Primitives
7102 (Prims_List => Result,
7103 Gen_T => Gen_T,
7104 Act_T => Entity (Subtype_Indication (Actual)));
7105 end if;
7106 end if;
7107
7108 Next (Actual);
7109 end loop;
7110
7111 return Result;
7112 end Check_Hidden_Primitives;
7113
7114 --------------------------
7115 -- Contains_Instance_Of --
7116 --------------------------
7117
7118 function Contains_Instance_Of
7119 (Inner : Entity_Id;
7120 Outer : Entity_Id;
7121 N : Node_Id) return Boolean
7122 is
7123 Elmt : Elmt_Id;
7124 Scop : Entity_Id;
7125
7126 begin
7127 Scop := Outer;
7128
7129 -- Verify that there are no circular instantiations. We check whether
7130 -- the unit contains an instance of the current scope or some enclosing
7131 -- scope (in case one of the instances appears in a subunit). Longer
7132 -- circularities involving subunits might seem too pathological to
7133 -- consider, but they were not too pathological for the authors of
7134 -- DEC bc30vsq, so we loop over all enclosing scopes, and mark all
7135 -- enclosing generic scopes as containing an instance.
7136
7137 loop
7138 -- Within a generic subprogram body, the scope is not generic, to
7139 -- allow for recursive subprograms. Use the declaration to determine
7140 -- whether this is a generic unit.
7141
7142 if Ekind (Scop) = E_Generic_Package
7143 or else (Is_Subprogram (Scop)
7144 and then Nkind (Unit_Declaration_Node (Scop)) =
7145 N_Generic_Subprogram_Declaration)
7146 then
7147 Elmt := First_Elmt (Inner_Instances (Inner));
7148
7149 while Present (Elmt) loop
7150 if Node (Elmt) = Scop then
7151 Error_Msg_Node_2 := Inner;
7152 Error_Msg_NE
7153 ("circular Instantiation: & instantiated within &!",
7154 N, Scop);
7155 return True;
7156
7157 elsif Node (Elmt) = Inner then
7158 return True;
7159
7160 elsif Contains_Instance_Of (Node (Elmt), Scop, N) then
7161 Error_Msg_Node_2 := Inner;
7162 Error_Msg_NE
7163 ("circular Instantiation: & instantiated within &!",
7164 N, Node (Elmt));
7165 return True;
7166 end if;
7167
7168 Next_Elmt (Elmt);
7169 end loop;
7170
7171 -- Indicate that Inner is being instantiated within Scop
7172
7173 Append_Elmt (Inner, Inner_Instances (Scop));
7174 end if;
7175
7176 if Scop = Standard_Standard then
7177 exit;
7178 else
7179 Scop := Scope (Scop);
7180 end if;
7181 end loop;
7182
7183 return False;
7184 end Contains_Instance_Of;
7185
7186 -----------------------
7187 -- Copy_Generic_Node --
7188 -----------------------
7189
7190 function Copy_Generic_Node
7191 (N : Node_Id;
7192 Parent_Id : Node_Id;
7193 Instantiating : Boolean) return Node_Id
7194 is
7195 Ent : Entity_Id;
7196 New_N : Node_Id;
7197
7198 function Copy_Generic_Descendant (D : Union_Id) return Union_Id;
7199 -- Check the given value of one of the Fields referenced by the current
7200 -- node to determine whether to copy it recursively. The field may hold
7201 -- a Node_Id, a List_Id, or an Elist_Id, or a plain value (Sloc, Uint,
7202 -- Char) in which case it need not be copied.
7203
7204 procedure Copy_Descendants;
7205 -- Common utility for various nodes
7206
7207 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id;
7208 -- Make copy of element list
7209
7210 function Copy_Generic_List
7211 (L : List_Id;
7212 Parent_Id : Node_Id) return List_Id;
7213 -- Apply Copy_Node recursively to the members of a node list
7214
7215 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean;
7216 -- True if an identifier is part of the defining program unit name of
7217 -- a child unit. The entity of such an identifier must be kept (for
7218 -- ASIS use) even though as the name of an enclosing generic it would
7219 -- otherwise not be preserved in the generic tree.
7220
7221 ----------------------
7222 -- Copy_Descendants --
7223 ----------------------
7224
7225 procedure Copy_Descendants is
7226 use Atree.Unchecked_Access;
7227 -- This code section is part of the implementation of an untyped
7228 -- tree traversal, so it needs direct access to node fields.
7229
7230 begin
7231 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
7232 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
7233 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
7234 Set_Field4 (New_N, Copy_Generic_Descendant (Field4 (N)));
7235 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
7236 end Copy_Descendants;
7237
7238 -----------------------------
7239 -- Copy_Generic_Descendant --
7240 -----------------------------
7241
7242 function Copy_Generic_Descendant (D : Union_Id) return Union_Id is
7243 begin
7244 if D = Union_Id (Empty) then
7245 return D;
7246
7247 elsif D in Node_Range then
7248 return Union_Id
7249 (Copy_Generic_Node (Node_Id (D), New_N, Instantiating));
7250
7251 elsif D in List_Range then
7252 return Union_Id (Copy_Generic_List (List_Id (D), New_N));
7253
7254 elsif D in Elist_Range then
7255 return Union_Id (Copy_Generic_Elist (Elist_Id (D)));
7256
7257 -- Nothing else is copyable (e.g. Uint values), return as is
7258
7259 else
7260 return D;
7261 end if;
7262 end Copy_Generic_Descendant;
7263
7264 ------------------------
7265 -- Copy_Generic_Elist --
7266 ------------------------
7267
7268 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id is
7269 M : Elmt_Id;
7270 L : Elist_Id;
7271
7272 begin
7273 if Present (E) then
7274 L := New_Elmt_List;
7275 M := First_Elmt (E);
7276 while Present (M) loop
7277 Append_Elmt
7278 (Copy_Generic_Node (Node (M), Empty, Instantiating), L);
7279 Next_Elmt (M);
7280 end loop;
7281
7282 return L;
7283
7284 else
7285 return No_Elist;
7286 end if;
7287 end Copy_Generic_Elist;
7288
7289 -----------------------
7290 -- Copy_Generic_List --
7291 -----------------------
7292
7293 function Copy_Generic_List
7294 (L : List_Id;
7295 Parent_Id : Node_Id) return List_Id
7296 is
7297 N : Node_Id;
7298 New_L : List_Id;
7299
7300 begin
7301 if Present (L) then
7302 New_L := New_List;
7303 Set_Parent (New_L, Parent_Id);
7304
7305 N := First (L);
7306 while Present (N) loop
7307 Append (Copy_Generic_Node (N, Empty, Instantiating), New_L);
7308 Next (N);
7309 end loop;
7310
7311 return New_L;
7312
7313 else
7314 return No_List;
7315 end if;
7316 end Copy_Generic_List;
7317
7318 ---------------------------
7319 -- In_Defining_Unit_Name --
7320 ---------------------------
7321
7322 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean is
7323 begin
7324 return
7325 Present (Parent (Nam))
7326 and then (Nkind (Parent (Nam)) = N_Defining_Program_Unit_Name
7327 or else
7328 (Nkind (Parent (Nam)) = N_Expanded_Name
7329 and then In_Defining_Unit_Name (Parent (Nam))));
7330 end In_Defining_Unit_Name;
7331
7332 -- Start of processing for Copy_Generic_Node
7333
7334 begin
7335 if N = Empty then
7336 return N;
7337 end if;
7338
7339 New_N := New_Copy (N);
7340
7341 -- Copy aspects if present
7342
7343 if Has_Aspects (N) then
7344 Set_Has_Aspects (New_N, False);
7345 Set_Aspect_Specifications
7346 (New_N, Copy_Generic_List (Aspect_Specifications (N), Parent_Id));
7347 end if;
7348
7349 if Instantiating then
7350 Adjust_Instantiation_Sloc (New_N, S_Adjustment);
7351 end if;
7352
7353 if not Is_List_Member (N) then
7354 Set_Parent (New_N, Parent_Id);
7355 end if;
7356
7357 -- Special casing for identifiers and other entity names and operators
7358
7359 if Nkind_In (New_N, N_Character_Literal,
7360 N_Expanded_Name,
7361 N_Identifier,
7362 N_Operator_Symbol)
7363 or else Nkind (New_N) in N_Op
7364 then
7365 if not Instantiating then
7366
7367 -- Link both nodes in order to assign subsequently the entity of
7368 -- the copy to the original node, in case this is a global
7369 -- reference.
7370
7371 Set_Associated_Node (N, New_N);
7372
7373 -- If we are within an instantiation, this is a nested generic
7374 -- that has already been analyzed at the point of definition.
7375 -- We must preserve references that were global to the enclosing
7376 -- parent at that point. Other occurrences, whether global or
7377 -- local to the current generic, must be resolved anew, so we
7378 -- reset the entity in the generic copy. A global reference has a
7379 -- smaller depth than the parent, or else the same depth in case
7380 -- both are distinct compilation units.
7381
7382 -- A child unit is implicitly declared within the enclosing parent
7383 -- but is in fact global to it, and must be preserved.
7384
7385 -- It is also possible for Current_Instantiated_Parent to be
7386 -- defined, and for this not to be a nested generic, namely if
7387 -- the unit is loaded through Rtsfind. In that case, the entity of
7388 -- New_N is only a link to the associated node, and not a defining
7389 -- occurrence.
7390
7391 -- The entities for parent units in the defining_program_unit of a
7392 -- generic child unit are established when the context of the unit
7393 -- is first analyzed, before the generic copy is made. They are
7394 -- preserved in the copy for use in ASIS queries.
7395
7396 Ent := Entity (New_N);
7397
7398 if No (Current_Instantiated_Parent.Gen_Id) then
7399 if No (Ent)
7400 or else Nkind (Ent) /= N_Defining_Identifier
7401 or else not In_Defining_Unit_Name (N)
7402 then
7403 Set_Associated_Node (New_N, Empty);
7404 end if;
7405
7406 elsif No (Ent)
7407 or else
7408 not Nkind_In (Ent, N_Defining_Identifier,
7409 N_Defining_Character_Literal,
7410 N_Defining_Operator_Symbol)
7411 or else No (Scope (Ent))
7412 or else
7413 (Scope (Ent) = Current_Instantiated_Parent.Gen_Id
7414 and then not Is_Child_Unit (Ent))
7415 or else
7416 (Scope_Depth (Scope (Ent)) >
7417 Scope_Depth (Current_Instantiated_Parent.Gen_Id)
7418 and then
7419 Get_Source_Unit (Ent) =
7420 Get_Source_Unit (Current_Instantiated_Parent.Gen_Id))
7421 then
7422 Set_Associated_Node (New_N, Empty);
7423 end if;
7424
7425 -- Case of instantiating identifier or some other name or operator
7426
7427 else
7428 -- If the associated node is still defined, the entity in it
7429 -- is global, and must be copied to the instance. If this copy
7430 -- is being made for a body to inline, it is applied to an
7431 -- instantiated tree, and the entity is already present and
7432 -- must be also preserved.
7433
7434 declare
7435 Assoc : constant Node_Id := Get_Associated_Node (N);
7436
7437 begin
7438 if Present (Assoc) then
7439 if Nkind (Assoc) = Nkind (N) then
7440 Set_Entity (New_N, Entity (Assoc));
7441 Check_Private_View (N);
7442
7443 -- The node is a reference to a global type and acts as the
7444 -- subtype mark of a qualified expression created in order
7445 -- to aid resolution of accidental overloading in instances.
7446 -- Since N is a reference to a type, the Associated_Node of
7447 -- N denotes an entity rather than another identifier. See
7448 -- Qualify_Universal_Operands for details.
7449
7450 elsif Nkind (N) = N_Identifier
7451 and then Nkind (Parent (N)) = N_Qualified_Expression
7452 and then Subtype_Mark (Parent (N)) = N
7453 and then Is_Qualified_Universal_Literal (Parent (N))
7454 then
7455 Set_Entity (New_N, Assoc);
7456
7457 -- The name in the call may be a selected component if the
7458 -- call has not been analyzed yet, as may be the case for
7459 -- pre/post conditions in a generic unit.
7460
7461 elsif Nkind (Assoc) = N_Function_Call
7462 and then Is_Entity_Name (Name (Assoc))
7463 then
7464 Set_Entity (New_N, Entity (Name (Assoc)));
7465
7466 elsif Nkind_In (Assoc, N_Defining_Identifier,
7467 N_Defining_Character_Literal,
7468 N_Defining_Operator_Symbol)
7469 and then Expander_Active
7470 then
7471 -- Inlining case: we are copying a tree that contains
7472 -- global entities, which are preserved in the copy to be
7473 -- used for subsequent inlining.
7474
7475 null;
7476
7477 else
7478 Set_Entity (New_N, Empty);
7479 end if;
7480 end if;
7481 end;
7482 end if;
7483
7484 -- For expanded name, we must copy the Prefix and Selector_Name
7485
7486 if Nkind (N) = N_Expanded_Name then
7487 Set_Prefix
7488 (New_N, Copy_Generic_Node (Prefix (N), New_N, Instantiating));
7489
7490 Set_Selector_Name (New_N,
7491 Copy_Generic_Node (Selector_Name (N), New_N, Instantiating));
7492
7493 -- For operators, we must copy the right operand
7494
7495 elsif Nkind (N) in N_Op then
7496 Set_Right_Opnd (New_N,
7497 Copy_Generic_Node (Right_Opnd (N), New_N, Instantiating));
7498
7499 -- And for binary operators, the left operand as well
7500
7501 if Nkind (N) in N_Binary_Op then
7502 Set_Left_Opnd (New_N,
7503 Copy_Generic_Node (Left_Opnd (N), New_N, Instantiating));
7504 end if;
7505 end if;
7506
7507 -- Establish a link between an entity from the generic template and the
7508 -- corresponding entity in the generic copy to be analyzed.
7509
7510 elsif Nkind (N) in N_Entity then
7511 if not Instantiating then
7512 Set_Associated_Entity (N, New_N);
7513 end if;
7514
7515 -- Clear any existing link the copy may inherit from the replicated
7516 -- generic template entity.
7517
7518 Set_Associated_Entity (New_N, Empty);
7519
7520 -- Special casing for stubs
7521
7522 elsif Nkind (N) in N_Body_Stub then
7523
7524 -- In any case, we must copy the specification or defining
7525 -- identifier as appropriate.
7526
7527 if Nkind (N) = N_Subprogram_Body_Stub then
7528 Set_Specification (New_N,
7529 Copy_Generic_Node (Specification (N), New_N, Instantiating));
7530
7531 else
7532 Set_Defining_Identifier (New_N,
7533 Copy_Generic_Node
7534 (Defining_Identifier (N), New_N, Instantiating));
7535 end if;
7536
7537 -- If we are not instantiating, then this is where we load and
7538 -- analyze subunits, i.e. at the point where the stub occurs. A
7539 -- more permissive system might defer this analysis to the point
7540 -- of instantiation, but this seems too complicated for now.
7541
7542 if not Instantiating then
7543 declare
7544 Subunit_Name : constant Unit_Name_Type := Get_Unit_Name (N);
7545 Subunit : Node_Id;
7546 Unum : Unit_Number_Type;
7547 New_Body : Node_Id;
7548
7549 begin
7550 -- Make sure that, if it is a subunit of the main unit that is
7551 -- preprocessed and if -gnateG is specified, the preprocessed
7552 -- file will be written.
7553
7554 Lib.Analysing_Subunit_Of_Main :=
7555 Lib.In_Extended_Main_Source_Unit (N);
7556 Unum :=
7557 Load_Unit
7558 (Load_Name => Subunit_Name,
7559 Required => False,
7560 Subunit => True,
7561 Error_Node => N);
7562 Lib.Analysing_Subunit_Of_Main := False;
7563
7564 -- If the proper body is not found, a warning message will be
7565 -- emitted when analyzing the stub, or later at the point of
7566 -- instantiation. Here we just leave the stub as is.
7567
7568 if Unum = No_Unit then
7569 Subunits_Missing := True;
7570 goto Subunit_Not_Found;
7571 end if;
7572
7573 Subunit := Cunit (Unum);
7574
7575 if Nkind (Unit (Subunit)) /= N_Subunit then
7576 Error_Msg_N
7577 ("found child unit instead of expected SEPARATE subunit",
7578 Subunit);
7579 Error_Msg_Sloc := Sloc (N);
7580 Error_Msg_N ("\to complete stub #", Subunit);
7581 goto Subunit_Not_Found;
7582 end if;
7583
7584 -- We must create a generic copy of the subunit, in order to
7585 -- perform semantic analysis on it, and we must replace the
7586 -- stub in the original generic unit with the subunit, in order
7587 -- to preserve non-local references within.
7588
7589 -- Only the proper body needs to be copied. Library_Unit and
7590 -- context clause are simply inherited by the generic copy.
7591 -- Note that the copy (which may be recursive if there are
7592 -- nested subunits) must be done first, before attaching it to
7593 -- the enclosing generic.
7594
7595 New_Body :=
7596 Copy_Generic_Node
7597 (Proper_Body (Unit (Subunit)),
7598 Empty, Instantiating => False);
7599
7600 -- Now place the original proper body in the original generic
7601 -- unit. This is a body, not a compilation unit.
7602
7603 Rewrite (N, Proper_Body (Unit (Subunit)));
7604 Set_Is_Compilation_Unit (Defining_Entity (N), False);
7605 Set_Was_Originally_Stub (N);
7606
7607 -- Finally replace the body of the subunit with its copy, and
7608 -- make this new subunit into the library unit of the generic
7609 -- copy, which does not have stubs any longer.
7610
7611 Set_Proper_Body (Unit (Subunit), New_Body);
7612 Set_Library_Unit (New_N, Subunit);
7613 Inherit_Context (Unit (Subunit), N);
7614 end;
7615
7616 -- If we are instantiating, this must be an error case, since
7617 -- otherwise we would have replaced the stub node by the proper body
7618 -- that corresponds. So just ignore it in the copy (i.e. we have
7619 -- copied it, and that is good enough).
7620
7621 else
7622 null;
7623 end if;
7624
7625 <<Subunit_Not_Found>> null;
7626
7627 -- If the node is a compilation unit, it is the subunit of a stub, which
7628 -- has been loaded already (see code below). In this case, the library
7629 -- unit field of N points to the parent unit (which is a compilation
7630 -- unit) and need not (and cannot) be copied.
7631
7632 -- When the proper body of the stub is analyzed, the library_unit link
7633 -- is used to establish the proper context (see sem_ch10).
7634
7635 -- The other fields of a compilation unit are copied as usual
7636
7637 elsif Nkind (N) = N_Compilation_Unit then
7638
7639 -- This code can only be executed when not instantiating, because in
7640 -- the copy made for an instantiation, the compilation unit node has
7641 -- disappeared at the point that a stub is replaced by its proper
7642 -- body.
7643
7644 pragma Assert (not Instantiating);
7645
7646 Set_Context_Items (New_N,
7647 Copy_Generic_List (Context_Items (N), New_N));
7648
7649 Set_Unit (New_N,
7650 Copy_Generic_Node (Unit (N), New_N, False));
7651
7652 Set_First_Inlined_Subprogram (New_N,
7653 Copy_Generic_Node
7654 (First_Inlined_Subprogram (N), New_N, False));
7655
7656 Set_Aux_Decls_Node (New_N,
7657 Copy_Generic_Node (Aux_Decls_Node (N), New_N, False));
7658
7659 -- For an assignment node, the assignment is known to be semantically
7660 -- legal if we are instantiating the template. This avoids incorrect
7661 -- diagnostics in generated code.
7662
7663 elsif Nkind (N) = N_Assignment_Statement then
7664
7665 -- Copy name and expression fields in usual manner
7666
7667 Set_Name (New_N,
7668 Copy_Generic_Node (Name (N), New_N, Instantiating));
7669
7670 Set_Expression (New_N,
7671 Copy_Generic_Node (Expression (N), New_N, Instantiating));
7672
7673 if Instantiating then
7674 Set_Assignment_OK (Name (New_N), True);
7675 end if;
7676
7677 elsif Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
7678 if not Instantiating then
7679 Set_Associated_Node (N, New_N);
7680
7681 else
7682 if Present (Get_Associated_Node (N))
7683 and then Nkind (Get_Associated_Node (N)) = Nkind (N)
7684 then
7685 -- In the generic the aggregate has some composite type. If at
7686 -- the point of instantiation the type has a private view,
7687 -- install the full view (and that of its ancestors, if any).
7688
7689 declare
7690 T : Entity_Id := (Etype (Get_Associated_Node (New_N)));
7691 Rt : Entity_Id;
7692
7693 begin
7694 if Present (T) and then Is_Private_Type (T) then
7695 Switch_View (T);
7696 end if;
7697
7698 if Present (T)
7699 and then Is_Tagged_Type (T)
7700 and then Is_Derived_Type (T)
7701 then
7702 Rt := Root_Type (T);
7703
7704 loop
7705 T := Etype (T);
7706
7707 if Is_Private_Type (T) then
7708 Switch_View (T);
7709 end if;
7710
7711 exit when T = Rt;
7712 end loop;
7713 end if;
7714 end;
7715 end if;
7716 end if;
7717
7718 -- Do not copy the associated node, which points to the generic copy
7719 -- of the aggregate.
7720
7721 declare
7722 use Atree.Unchecked_Access;
7723 -- This code section is part of the implementation of an untyped
7724 -- tree traversal, so it needs direct access to node fields.
7725
7726 begin
7727 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
7728 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
7729 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
7730 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
7731 end;
7732
7733 -- Allocators do not have an identifier denoting the access type, so we
7734 -- must locate it through the expression to check whether the views are
7735 -- consistent.
7736
7737 elsif Nkind (N) = N_Allocator
7738 and then Nkind (Expression (N)) = N_Qualified_Expression
7739 and then Is_Entity_Name (Subtype_Mark (Expression (N)))
7740 and then Instantiating
7741 then
7742 declare
7743 T : constant Node_Id :=
7744 Get_Associated_Node (Subtype_Mark (Expression (N)));
7745 Acc_T : Entity_Id;
7746
7747 begin
7748 if Present (T) then
7749
7750 -- Retrieve the allocator node in the generic copy
7751
7752 Acc_T := Etype (Parent (Parent (T)));
7753
7754 if Present (Acc_T) and then Is_Private_Type (Acc_T) then
7755 Switch_View (Acc_T);
7756 end if;
7757 end if;
7758
7759 Copy_Descendants;
7760 end;
7761
7762 -- For a proper body, we must catch the case of a proper body that
7763 -- replaces a stub. This represents the point at which a separate
7764 -- compilation unit, and hence template file, may be referenced, so we
7765 -- must make a new source instantiation entry for the template of the
7766 -- subunit, and ensure that all nodes in the subunit are adjusted using
7767 -- this new source instantiation entry.
7768
7769 elsif Nkind (N) in N_Proper_Body then
7770 declare
7771 Save_Adjustment : constant Sloc_Adjustment := S_Adjustment;
7772
7773 begin
7774 if Instantiating and then Was_Originally_Stub (N) then
7775 Create_Instantiation_Source
7776 (Instantiation_Node,
7777 Defining_Entity (N),
7778 S_Adjustment);
7779 end if;
7780
7781 -- Now copy the fields of the proper body, using the new
7782 -- adjustment factor if one was needed as per test above.
7783
7784 Copy_Descendants;
7785
7786 -- Restore the original adjustment factor in case changed
7787
7788 S_Adjustment := Save_Adjustment;
7789 end;
7790
7791 elsif Nkind (N) = N_Pragma and then Instantiating then
7792
7793 -- Do not copy Comment or Ident pragmas their content is relevant to
7794 -- the generic unit, not to the instantiating unit.
7795
7796 if Nam_In (Pragma_Name_Unmapped (N), Name_Comment, Name_Ident) then
7797 New_N := Make_Null_Statement (Sloc (N));
7798
7799 -- Do not copy pragmas generated from aspects because the pragmas do
7800 -- not carry any semantic information, plus they will be regenerated
7801 -- in the instance.
7802
7803 -- However, generating C we need to copy them since postconditions
7804 -- are inlined by the front end, and the front-end inlining machinery
7805 -- relies on this routine to perform inlining.
7806
7807 elsif From_Aspect_Specification (N)
7808 and then not Modify_Tree_For_C
7809 then
7810 New_N := Make_Null_Statement (Sloc (N));
7811
7812 else
7813 Copy_Descendants;
7814 end if;
7815
7816 elsif Nkind_In (N, N_Integer_Literal, N_Real_Literal) then
7817
7818 -- No descendant fields need traversing
7819
7820 null;
7821
7822 elsif Nkind (N) = N_String_Literal
7823 and then Present (Etype (N))
7824 and then Instantiating
7825 then
7826 -- If the string is declared in an outer scope, the string_literal
7827 -- subtype created for it may have the wrong scope. Force reanalysis
7828 -- of the constant to generate a new itype in the proper context.
7829
7830 Set_Etype (New_N, Empty);
7831 Set_Analyzed (New_N, False);
7832
7833 -- For the remaining nodes, copy their descendants recursively
7834
7835 else
7836 Copy_Descendants;
7837
7838 if Instantiating and then Nkind (N) = N_Subprogram_Body then
7839 Set_Generic_Parent (Specification (New_N), N);
7840
7841 -- Should preserve Corresponding_Spec??? (12.3(14))
7842 end if;
7843 end if;
7844
7845 -- Propagate dimensions if present, so that they are reflected in the
7846 -- instance.
7847
7848 if Nkind (N) in N_Has_Etype
7849 and then (Nkind (N) in N_Op or else Is_Entity_Name (N))
7850 and then Present (Etype (N))
7851 and then Is_Floating_Point_Type (Etype (N))
7852 and then Has_Dimension_System (Etype (N))
7853 then
7854 Copy_Dimensions (N, New_N);
7855 end if;
7856
7857 return New_N;
7858 end Copy_Generic_Node;
7859
7860 ----------------------------
7861 -- Denotes_Formal_Package --
7862 ----------------------------
7863
7864 function Denotes_Formal_Package
7865 (Pack : Entity_Id;
7866 On_Exit : Boolean := False;
7867 Instance : Entity_Id := Empty) return Boolean
7868 is
7869 Par : Entity_Id;
7870 Scop : constant Entity_Id := Scope (Pack);
7871 E : Entity_Id;
7872
7873 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean;
7874 -- The package in question may be an actual for a previous formal
7875 -- package P of the current instance, so examine its actuals as well.
7876 -- This must be recursive over other formal packages.
7877
7878 ----------------------------------
7879 -- Is_Actual_Of_Previous_Formal --
7880 ----------------------------------
7881
7882 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean is
7883 E1 : Entity_Id;
7884
7885 begin
7886 E1 := First_Entity (P);
7887 while Present (E1) and then E1 /= Instance loop
7888 if Ekind (E1) = E_Package
7889 and then Nkind (Parent (E1)) = N_Package_Renaming_Declaration
7890 then
7891 if Renamed_Object (E1) = Pack then
7892 return True;
7893
7894 elsif E1 = P or else Renamed_Object (E1) = P then
7895 return False;
7896
7897 elsif Is_Actual_Of_Previous_Formal (E1) then
7898 return True;
7899 end if;
7900 end if;
7901
7902 Next_Entity (E1);
7903 end loop;
7904
7905 return False;
7906 end Is_Actual_Of_Previous_Formal;
7907
7908 -- Start of processing for Denotes_Formal_Package
7909
7910 begin
7911 if On_Exit then
7912 Par :=
7913 Instance_Envs.Table
7914 (Instance_Envs.Last).Instantiated_Parent.Act_Id;
7915 else
7916 Par := Current_Instantiated_Parent.Act_Id;
7917 end if;
7918
7919 if Ekind (Scop) = E_Generic_Package
7920 or else Nkind (Unit_Declaration_Node (Scop)) =
7921 N_Generic_Subprogram_Declaration
7922 then
7923 return True;
7924
7925 elsif Nkind (Original_Node (Unit_Declaration_Node (Pack))) =
7926 N_Formal_Package_Declaration
7927 then
7928 return True;
7929
7930 elsif No (Par) then
7931 return False;
7932
7933 else
7934 -- Check whether this package is associated with a formal package of
7935 -- the enclosing instantiation. Iterate over the list of renamings.
7936
7937 E := First_Entity (Par);
7938 while Present (E) loop
7939 if Ekind (E) /= E_Package
7940 or else Nkind (Parent (E)) /= N_Package_Renaming_Declaration
7941 then
7942 null;
7943
7944 elsif Renamed_Object (E) = Par then
7945 return False;
7946
7947 elsif Renamed_Object (E) = Pack then
7948 return True;
7949
7950 elsif Is_Actual_Of_Previous_Formal (E) then
7951 return True;
7952
7953 end if;
7954
7955 Next_Entity (E);
7956 end loop;
7957
7958 return False;
7959 end if;
7960 end Denotes_Formal_Package;
7961
7962 -----------------
7963 -- End_Generic --
7964 -----------------
7965
7966 procedure End_Generic is
7967 begin
7968 -- ??? More things could be factored out in this routine. Should
7969 -- probably be done at a later stage.
7970
7971 Inside_A_Generic := Generic_Flags.Table (Generic_Flags.Last);
7972 Generic_Flags.Decrement_Last;
7973
7974 Expander_Mode_Restore;
7975 end End_Generic;
7976
7977 -------------
7978 -- Earlier --
7979 -------------
7980
7981 function Earlier (N1, N2 : Node_Id) return Boolean is
7982 procedure Find_Depth (P : in out Node_Id; D : in out Integer);
7983 -- Find distance from given node to enclosing compilation unit
7984
7985 ----------------
7986 -- Find_Depth --
7987 ----------------
7988
7989 procedure Find_Depth (P : in out Node_Id; D : in out Integer) is
7990 begin
7991 while Present (P)
7992 and then Nkind (P) /= N_Compilation_Unit
7993 loop
7994 P := True_Parent (P);
7995 D := D + 1;
7996 end loop;
7997 end Find_Depth;
7998
7999 -- Local declarations
8000
8001 D1 : Integer := 0;
8002 D2 : Integer := 0;
8003 P1 : Node_Id := N1;
8004 P2 : Node_Id := N2;
8005 T1 : Source_Ptr;
8006 T2 : Source_Ptr;
8007
8008 -- Start of processing for Earlier
8009
8010 begin
8011 Find_Depth (P1, D1);
8012 Find_Depth (P2, D2);
8013
8014 if P1 /= P2 then
8015 return False;
8016 else
8017 P1 := N1;
8018 P2 := N2;
8019 end if;
8020
8021 while D1 > D2 loop
8022 P1 := True_Parent (P1);
8023 D1 := D1 - 1;
8024 end loop;
8025
8026 while D2 > D1 loop
8027 P2 := True_Parent (P2);
8028 D2 := D2 - 1;
8029 end loop;
8030
8031 -- At this point P1 and P2 are at the same distance from the root.
8032 -- We examine their parents until we find a common declarative list.
8033 -- If we reach the root, N1 and N2 do not descend from the same
8034 -- declarative list (e.g. one is nested in the declarative part and
8035 -- the other is in a block in the statement part) and the earlier
8036 -- one is already frozen.
8037
8038 while not Is_List_Member (P1)
8039 or else not Is_List_Member (P2)
8040 or else List_Containing (P1) /= List_Containing (P2)
8041 loop
8042 P1 := True_Parent (P1);
8043 P2 := True_Parent (P2);
8044
8045 if Nkind (Parent (P1)) = N_Subunit then
8046 P1 := Corresponding_Stub (Parent (P1));
8047 end if;
8048
8049 if Nkind (Parent (P2)) = N_Subunit then
8050 P2 := Corresponding_Stub (Parent (P2));
8051 end if;
8052
8053 if P1 = P2 then
8054 return False;
8055 end if;
8056 end loop;
8057
8058 -- Expanded code usually shares the source location of the original
8059 -- construct it was generated for. This however may not necessarily
8060 -- reflect the true location of the code within the tree.
8061
8062 -- Before comparing the slocs of the two nodes, make sure that we are
8063 -- working with correct source locations. Assume that P1 is to the left
8064 -- of P2. If either one does not come from source, traverse the common
8065 -- list heading towards the other node and locate the first source
8066 -- statement.
8067
8068 -- P1 P2
8069 -- ----+===+===+--------------+===+===+----
8070 -- expanded code expanded code
8071
8072 if not Comes_From_Source (P1) then
8073 while Present (P1) loop
8074
8075 -- Neither P2 nor a source statement were located during the
8076 -- search. If we reach the end of the list, then P1 does not
8077 -- occur earlier than P2.
8078
8079 -- ---->
8080 -- start --- P2 ----- P1 --- end
8081
8082 if No (Next (P1)) then
8083 return False;
8084
8085 -- We encounter P2 while going to the right of the list. This
8086 -- means that P1 does indeed appear earlier.
8087
8088 -- ---->
8089 -- start --- P1 ===== P2 --- end
8090 -- expanded code in between
8091
8092 elsif P1 = P2 then
8093 return True;
8094
8095 -- No need to look any further since we have located a source
8096 -- statement.
8097
8098 elsif Comes_From_Source (P1) then
8099 exit;
8100 end if;
8101
8102 -- Keep going right
8103
8104 Next (P1);
8105 end loop;
8106 end if;
8107
8108 if not Comes_From_Source (P2) then
8109 while Present (P2) loop
8110
8111 -- Neither P1 nor a source statement were located during the
8112 -- search. If we reach the start of the list, then P1 does not
8113 -- occur earlier than P2.
8114
8115 -- <----
8116 -- start --- P2 --- P1 --- end
8117
8118 if No (Prev (P2)) then
8119 return False;
8120
8121 -- We encounter P1 while going to the left of the list. This
8122 -- means that P1 does indeed appear earlier.
8123
8124 -- <----
8125 -- start --- P1 ===== P2 --- end
8126 -- expanded code in between
8127
8128 elsif P2 = P1 then
8129 return True;
8130
8131 -- No need to look any further since we have located a source
8132 -- statement.
8133
8134 elsif Comes_From_Source (P2) then
8135 exit;
8136 end if;
8137
8138 -- Keep going left
8139
8140 Prev (P2);
8141 end loop;
8142 end if;
8143
8144 -- At this point either both nodes came from source or we approximated
8145 -- their source locations through neighboring source statements.
8146
8147 T1 := Top_Level_Location (Sloc (P1));
8148 T2 := Top_Level_Location (Sloc (P2));
8149
8150 -- When two nodes come from the same instance, they have identical top
8151 -- level locations. To determine proper relation within the tree, check
8152 -- their locations within the template.
8153
8154 if T1 = T2 then
8155 return Sloc (P1) < Sloc (P2);
8156
8157 -- The two nodes either come from unrelated instances or do not come
8158 -- from instantiated code at all.
8159
8160 else
8161 return T1 < T2;
8162 end if;
8163 end Earlier;
8164
8165 ----------------------
8166 -- Find_Actual_Type --
8167 ----------------------
8168
8169 function Find_Actual_Type
8170 (Typ : Entity_Id;
8171 Gen_Type : Entity_Id) return Entity_Id
8172 is
8173 Gen_Scope : constant Entity_Id := Scope (Gen_Type);
8174 T : Entity_Id;
8175
8176 begin
8177 -- Special processing only applies to child units
8178
8179 if not Is_Child_Unit (Gen_Scope) then
8180 return Get_Instance_Of (Typ);
8181
8182 -- If designated or component type is itself a formal of the child unit,
8183 -- its instance is available.
8184
8185 elsif Scope (Typ) = Gen_Scope then
8186 return Get_Instance_Of (Typ);
8187
8188 -- If the array or access type is not declared in the parent unit,
8189 -- no special processing needed.
8190
8191 elsif not Is_Generic_Type (Typ)
8192 and then Scope (Gen_Scope) /= Scope (Typ)
8193 then
8194 return Get_Instance_Of (Typ);
8195
8196 -- Otherwise, retrieve designated or component type by visibility
8197
8198 else
8199 T := Current_Entity (Typ);
8200 while Present (T) loop
8201 if In_Open_Scopes (Scope (T)) then
8202 return T;
8203 elsif Is_Generic_Actual_Type (T) then
8204 return T;
8205 end if;
8206
8207 T := Homonym (T);
8208 end loop;
8209
8210 return Typ;
8211 end if;
8212 end Find_Actual_Type;
8213
8214 ----------------------------
8215 -- Freeze_Subprogram_Body --
8216 ----------------------------
8217
8218 procedure Freeze_Subprogram_Body
8219 (Inst_Node : Node_Id;
8220 Gen_Body : Node_Id;
8221 Pack_Id : Entity_Id)
8222 is
8223 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
8224 Par : constant Entity_Id := Scope (Gen_Unit);
8225 E_G_Id : Entity_Id;
8226 Enc_G : Entity_Id;
8227 Enc_I : Node_Id;
8228 F_Node : Node_Id;
8229
8230 function Enclosing_Package_Body (N : Node_Id) return Node_Id;
8231 -- Find innermost package body that encloses the given node, and which
8232 -- is not a compilation unit. Freeze nodes for the instance, or for its
8233 -- enclosing body, may be inserted after the enclosing_body of the
8234 -- generic unit. Used to determine proper placement of freeze node for
8235 -- both package and subprogram instances.
8236
8237 function Package_Freeze_Node (B : Node_Id) return Node_Id;
8238 -- Find entity for given package body, and locate or create a freeze
8239 -- node for it.
8240
8241 ----------------------------
8242 -- Enclosing_Package_Body --
8243 ----------------------------
8244
8245 function Enclosing_Package_Body (N : Node_Id) return Node_Id is
8246 P : Node_Id;
8247
8248 begin
8249 P := Parent (N);
8250 while Present (P)
8251 and then Nkind (Parent (P)) /= N_Compilation_Unit
8252 loop
8253 if Nkind (P) = N_Package_Body then
8254 if Nkind (Parent (P)) = N_Subunit then
8255 return Corresponding_Stub (Parent (P));
8256 else
8257 return P;
8258 end if;
8259 end if;
8260
8261 P := True_Parent (P);
8262 end loop;
8263
8264 return Empty;
8265 end Enclosing_Package_Body;
8266
8267 -------------------------
8268 -- Package_Freeze_Node --
8269 -------------------------
8270
8271 function Package_Freeze_Node (B : Node_Id) return Node_Id is
8272 Id : Entity_Id;
8273
8274 begin
8275 if Nkind (B) = N_Package_Body then
8276 Id := Corresponding_Spec (B);
8277 else pragma Assert (Nkind (B) = N_Package_Body_Stub);
8278 Id := Corresponding_Spec (Proper_Body (Unit (Library_Unit (B))));
8279 end if;
8280
8281 Ensure_Freeze_Node (Id);
8282 return Freeze_Node (Id);
8283 end Package_Freeze_Node;
8284
8285 -- Start of processing for Freeze_Subprogram_Body
8286
8287 begin
8288 -- If the instance and the generic body appear within the same unit, and
8289 -- the instance precedes the generic, the freeze node for the instance
8290 -- must appear after that of the generic. If the generic is nested
8291 -- within another instance I2, then current instance must be frozen
8292 -- after I2. In both cases, the freeze nodes are those of enclosing
8293 -- packages. Otherwise, the freeze node is placed at the end of the
8294 -- current declarative part.
8295
8296 Enc_G := Enclosing_Package_Body (Gen_Body);
8297 Enc_I := Enclosing_Package_Body (Inst_Node);
8298 Ensure_Freeze_Node (Pack_Id);
8299 F_Node := Freeze_Node (Pack_Id);
8300
8301 if Is_Generic_Instance (Par)
8302 and then Present (Freeze_Node (Par))
8303 and then In_Same_Declarative_Part (Freeze_Node (Par), Inst_Node)
8304 then
8305 -- The parent was a premature instantiation. Insert freeze node at
8306 -- the end the current declarative part.
8307
8308 if ABE_Is_Certain (Get_Package_Instantiation_Node (Par)) then
8309 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8310
8311 -- Handle the following case:
8312 --
8313 -- package Parent_Inst is new ...
8314 -- Parent_Inst []
8315 --
8316 -- procedure P ... -- this body freezes Parent_Inst
8317 --
8318 -- package Inst is new ...
8319 --
8320 -- In this particular scenario, the freeze node for Inst must be
8321 -- inserted in the same manner as that of Parent_Inst - before the
8322 -- next source body or at the end of the declarative list (body not
8323 -- available). If body P did not exist and Parent_Inst was frozen
8324 -- after Inst, either by a body following Inst or at the end of the
8325 -- declarative region, the freeze node for Inst must be inserted
8326 -- after that of Parent_Inst. This relation is established by
8327 -- comparing the Slocs of Parent_Inst freeze node and Inst.
8328
8329 elsif List_Containing (Get_Package_Instantiation_Node (Par)) =
8330 List_Containing (Inst_Node)
8331 and then Sloc (Freeze_Node (Par)) < Sloc (Inst_Node)
8332 then
8333 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8334
8335 else
8336 Insert_After (Freeze_Node (Par), F_Node);
8337 end if;
8338
8339 -- The body enclosing the instance should be frozen after the body that
8340 -- includes the generic, because the body of the instance may make
8341 -- references to entities therein. If the two are not in the same
8342 -- declarative part, or if the one enclosing the instance is frozen
8343 -- already, freeze the instance at the end of the current declarative
8344 -- part.
8345
8346 elsif Is_Generic_Instance (Par)
8347 and then Present (Freeze_Node (Par))
8348 and then Present (Enc_I)
8349 then
8350 if In_Same_Declarative_Part (Freeze_Node (Par), Enc_I)
8351 or else
8352 (Nkind (Enc_I) = N_Package_Body
8353 and then
8354 In_Same_Declarative_Part (Freeze_Node (Par), Parent (Enc_I)))
8355 then
8356 -- The enclosing package may contain several instances. Rather
8357 -- than computing the earliest point at which to insert its freeze
8358 -- node, we place it at the end of the declarative part of the
8359 -- parent of the generic.
8360
8361 Insert_Freeze_Node_For_Instance
8362 (Freeze_Node (Par), Package_Freeze_Node (Enc_I));
8363 end if;
8364
8365 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8366
8367 elsif Present (Enc_G)
8368 and then Present (Enc_I)
8369 and then Enc_G /= Enc_I
8370 and then Earlier (Inst_Node, Gen_Body)
8371 then
8372 if Nkind (Enc_G) = N_Package_Body then
8373 E_G_Id :=
8374 Corresponding_Spec (Enc_G);
8375 else pragma Assert (Nkind (Enc_G) = N_Package_Body_Stub);
8376 E_G_Id :=
8377 Corresponding_Spec (Proper_Body (Unit (Library_Unit (Enc_G))));
8378 end if;
8379
8380 -- Freeze package that encloses instance, and place node after the
8381 -- package that encloses generic. If enclosing package is already
8382 -- frozen we have to assume it is at the proper place. This may be a
8383 -- potential ABE that requires dynamic checking. Do not add a freeze
8384 -- node if the package that encloses the generic is inside the body
8385 -- that encloses the instance, because the freeze node would be in
8386 -- the wrong scope. Additional contortions needed if the bodies are
8387 -- within a subunit.
8388
8389 declare
8390 Enclosing_Body : Node_Id;
8391
8392 begin
8393 if Nkind (Enc_I) = N_Package_Body_Stub then
8394 Enclosing_Body := Proper_Body (Unit (Library_Unit (Enc_I)));
8395 else
8396 Enclosing_Body := Enc_I;
8397 end if;
8398
8399 if Parent (List_Containing (Enc_G)) /= Enclosing_Body then
8400 Insert_Freeze_Node_For_Instance
8401 (Enc_G, Package_Freeze_Node (Enc_I));
8402 end if;
8403 end;
8404
8405 -- Freeze enclosing subunit before instance
8406
8407 Ensure_Freeze_Node (E_G_Id);
8408
8409 if not Is_List_Member (Freeze_Node (E_G_Id)) then
8410 Insert_After (Enc_G, Freeze_Node (E_G_Id));
8411 end if;
8412
8413 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8414
8415 else
8416 -- If none of the above, insert freeze node at the end of the current
8417 -- declarative part.
8418
8419 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8420 end if;
8421 end Freeze_Subprogram_Body;
8422
8423 ----------------
8424 -- Get_Gen_Id --
8425 ----------------
8426
8427 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id is
8428 begin
8429 return Generic_Renamings.Table (E).Gen_Id;
8430 end Get_Gen_Id;
8431
8432 ---------------------
8433 -- Get_Instance_Of --
8434 ---------------------
8435
8436 function Get_Instance_Of (A : Entity_Id) return Entity_Id is
8437 Res : constant Assoc_Ptr := Generic_Renamings_HTable.Get (A);
8438
8439 begin
8440 if Res /= Assoc_Null then
8441 return Generic_Renamings.Table (Res).Act_Id;
8442
8443 else
8444 -- On exit, entity is not instantiated: not a generic parameter, or
8445 -- else parameter of an inner generic unit.
8446
8447 return A;
8448 end if;
8449 end Get_Instance_Of;
8450
8451 ------------------------------------
8452 -- Get_Package_Instantiation_Node --
8453 ------------------------------------
8454
8455 function Get_Package_Instantiation_Node (A : Entity_Id) return Node_Id is
8456 Decl : Node_Id := Unit_Declaration_Node (A);
8457 Inst : Node_Id;
8458
8459 begin
8460 -- If the Package_Instantiation attribute has been set on the package
8461 -- entity, then use it directly when it (or its Original_Node) refers
8462 -- to an N_Package_Instantiation node. In principle it should be
8463 -- possible to have this field set in all cases, which should be
8464 -- investigated, and would allow this function to be significantly
8465 -- simplified. ???
8466
8467 Inst := Package_Instantiation (A);
8468
8469 if Present (Inst) then
8470 if Nkind (Inst) = N_Package_Instantiation then
8471 return Inst;
8472
8473 elsif Nkind (Original_Node (Inst)) = N_Package_Instantiation then
8474 return Original_Node (Inst);
8475 end if;
8476 end if;
8477
8478 -- If the instantiation is a compilation unit that does not need body
8479 -- then the instantiation node has been rewritten as a package
8480 -- declaration for the instance, and we return the original node.
8481
8482 -- If it is a compilation unit and the instance node has not been
8483 -- rewritten, then it is still the unit of the compilation. Finally, if
8484 -- a body is present, this is a parent of the main unit whose body has
8485 -- been compiled for inlining purposes, and the instantiation node has
8486 -- been rewritten with the instance body.
8487
8488 -- Otherwise the instantiation node appears after the declaration. If
8489 -- the entity is a formal package, the declaration may have been
8490 -- rewritten as a generic declaration (in the case of a formal with box)
8491 -- or left as a formal package declaration if it has actuals, and is
8492 -- found with a forward search.
8493
8494 if Nkind (Parent (Decl)) = N_Compilation_Unit then
8495 if Nkind (Decl) = N_Package_Declaration
8496 and then Present (Corresponding_Body (Decl))
8497 then
8498 Decl := Unit_Declaration_Node (Corresponding_Body (Decl));
8499 end if;
8500
8501 if Nkind (Original_Node (Decl)) = N_Package_Instantiation then
8502 return Original_Node (Decl);
8503 else
8504 return Unit (Parent (Decl));
8505 end if;
8506
8507 elsif Nkind (Decl) = N_Package_Declaration
8508 and then Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration
8509 then
8510 return Original_Node (Decl);
8511
8512 else
8513 Inst := Next (Decl);
8514 while not Nkind_In (Inst, N_Package_Instantiation,
8515 N_Formal_Package_Declaration)
8516 loop
8517 Next (Inst);
8518 end loop;
8519
8520 return Inst;
8521 end if;
8522 end Get_Package_Instantiation_Node;
8523
8524 ------------------------
8525 -- Has_Been_Exchanged --
8526 ------------------------
8527
8528 function Has_Been_Exchanged (E : Entity_Id) return Boolean is
8529 Next : Elmt_Id;
8530
8531 begin
8532 Next := First_Elmt (Exchanged_Views);
8533 while Present (Next) loop
8534 if Full_View (Node (Next)) = E then
8535 return True;
8536 end if;
8537
8538 Next_Elmt (Next);
8539 end loop;
8540
8541 return False;
8542 end Has_Been_Exchanged;
8543
8544 ----------
8545 -- Hash --
8546 ----------
8547
8548 function Hash (F : Entity_Id) return HTable_Range is
8549 begin
8550 return HTable_Range (F mod HTable_Size);
8551 end Hash;
8552
8553 ------------------------
8554 -- Hide_Current_Scope --
8555 ------------------------
8556
8557 procedure Hide_Current_Scope is
8558 C : constant Entity_Id := Current_Scope;
8559 E : Entity_Id;
8560
8561 begin
8562 Set_Is_Hidden_Open_Scope (C);
8563
8564 E := First_Entity (C);
8565 while Present (E) loop
8566 if Is_Immediately_Visible (E) then
8567 Set_Is_Immediately_Visible (E, False);
8568 Append_Elmt (E, Hidden_Entities);
8569 end if;
8570
8571 Next_Entity (E);
8572 end loop;
8573
8574 -- Make the scope name invisible as well. This is necessary, but might
8575 -- conflict with calls to Rtsfind later on, in case the scope is a
8576 -- predefined one. There is no clean solution to this problem, so for
8577 -- now we depend on the user not redefining Standard itself in one of
8578 -- the parent units.
8579
8580 if Is_Immediately_Visible (C) and then C /= Standard_Standard then
8581 Set_Is_Immediately_Visible (C, False);
8582 Append_Elmt (C, Hidden_Entities);
8583 end if;
8584
8585 end Hide_Current_Scope;
8586
8587 --------------
8588 -- Init_Env --
8589 --------------
8590
8591 procedure Init_Env is
8592 Saved : Instance_Env;
8593
8594 begin
8595 Saved.Instantiated_Parent := Current_Instantiated_Parent;
8596 Saved.Exchanged_Views := Exchanged_Views;
8597 Saved.Hidden_Entities := Hidden_Entities;
8598 Saved.Current_Sem_Unit := Current_Sem_Unit;
8599 Saved.Parent_Unit_Visible := Parent_Unit_Visible;
8600 Saved.Instance_Parent_Unit := Instance_Parent_Unit;
8601
8602 -- Save configuration switches. These may be reset if the unit is a
8603 -- predefined unit, and the current mode is not Ada 2005.
8604
8605 Save_Opt_Config_Switches (Saved.Switches);
8606
8607 Instance_Envs.Append (Saved);
8608
8609 Exchanged_Views := New_Elmt_List;
8610 Hidden_Entities := New_Elmt_List;
8611
8612 -- Make dummy entry for Instantiated parent. If generic unit is legal,
8613 -- this is set properly in Set_Instance_Env.
8614
8615 Current_Instantiated_Parent :=
8616 (Current_Scope, Current_Scope, Assoc_Null);
8617 end Init_Env;
8618
8619 ------------------------------
8620 -- In_Same_Declarative_Part --
8621 ------------------------------
8622
8623 function In_Same_Declarative_Part
8624 (F_Node : Node_Id;
8625 Inst : Node_Id) return Boolean
8626 is
8627 Decls : constant Node_Id := Parent (F_Node);
8628 Nod : Node_Id;
8629
8630 begin
8631 Nod := Parent (Inst);
8632 while Present (Nod) loop
8633 if Nod = Decls then
8634 return True;
8635
8636 elsif Nkind_In (Nod, N_Subprogram_Body,
8637 N_Package_Body,
8638 N_Package_Declaration,
8639 N_Task_Body,
8640 N_Protected_Body,
8641 N_Block_Statement)
8642 then
8643 return False;
8644
8645 elsif Nkind (Nod) = N_Subunit then
8646 Nod := Corresponding_Stub (Nod);
8647
8648 elsif Nkind (Nod) = N_Compilation_Unit then
8649 return False;
8650
8651 else
8652 Nod := Parent (Nod);
8653 end if;
8654 end loop;
8655
8656 return False;
8657 end In_Same_Declarative_Part;
8658
8659 ---------------------
8660 -- In_Main_Context --
8661 ---------------------
8662
8663 function In_Main_Context (E : Entity_Id) return Boolean is
8664 Context : List_Id;
8665 Clause : Node_Id;
8666 Nam : Node_Id;
8667
8668 begin
8669 if not Is_Compilation_Unit (E)
8670 or else Ekind (E) /= E_Package
8671 or else In_Private_Part (E)
8672 then
8673 return False;
8674 end if;
8675
8676 Context := Context_Items (Cunit (Main_Unit));
8677
8678 Clause := First (Context);
8679 while Present (Clause) loop
8680 if Nkind (Clause) = N_With_Clause then
8681 Nam := Name (Clause);
8682
8683 -- If the current scope is part of the context of the main unit,
8684 -- analysis of the corresponding with_clause is not complete, and
8685 -- the entity is not set. We use the Chars field directly, which
8686 -- might produce false positives in rare cases, but guarantees
8687 -- that we produce all the instance bodies we will need.
8688
8689 if (Is_Entity_Name (Nam) and then Chars (Nam) = Chars (E))
8690 or else (Nkind (Nam) = N_Selected_Component
8691 and then Chars (Selector_Name (Nam)) = Chars (E))
8692 then
8693 return True;
8694 end if;
8695 end if;
8696
8697 Next (Clause);
8698 end loop;
8699
8700 return False;
8701 end In_Main_Context;
8702
8703 ---------------------
8704 -- Inherit_Context --
8705 ---------------------
8706
8707 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id) is
8708 Current_Context : List_Id;
8709 Current_Unit : Node_Id;
8710 Item : Node_Id;
8711 New_I : Node_Id;
8712
8713 Clause : Node_Id;
8714 OK : Boolean;
8715 Lib_Unit : Node_Id;
8716
8717 begin
8718 if Nkind (Parent (Gen_Decl)) = N_Compilation_Unit then
8719
8720 -- The inherited context is attached to the enclosing compilation
8721 -- unit. This is either the main unit, or the declaration for the
8722 -- main unit (in case the instantiation appears within the package
8723 -- declaration and the main unit is its body).
8724
8725 Current_Unit := Parent (Inst);
8726 while Present (Current_Unit)
8727 and then Nkind (Current_Unit) /= N_Compilation_Unit
8728 loop
8729 Current_Unit := Parent (Current_Unit);
8730 end loop;
8731
8732 Current_Context := Context_Items (Current_Unit);
8733
8734 Item := First (Context_Items (Parent (Gen_Decl)));
8735 while Present (Item) loop
8736 if Nkind (Item) = N_With_Clause then
8737 Lib_Unit := Library_Unit (Item);
8738
8739 -- Take care to prevent direct cyclic with's
8740
8741 if Lib_Unit /= Current_Unit then
8742
8743 -- Do not add a unit if it is already in the context
8744
8745 Clause := First (Current_Context);
8746 OK := True;
8747 while Present (Clause) loop
8748 if Nkind (Clause) = N_With_Clause and then
8749 Library_Unit (Clause) = Lib_Unit
8750 then
8751 OK := False;
8752 exit;
8753 end if;
8754
8755 Next (Clause);
8756 end loop;
8757
8758 if OK then
8759 New_I := New_Copy (Item);
8760 Set_Implicit_With (New_I, True);
8761 Set_Implicit_With_From_Instantiation (New_I, True);
8762 Append (New_I, Current_Context);
8763 end if;
8764 end if;
8765 end if;
8766
8767 Next (Item);
8768 end loop;
8769 end if;
8770 end Inherit_Context;
8771
8772 ----------------
8773 -- Initialize --
8774 ----------------
8775
8776 procedure Initialize is
8777 begin
8778 Generic_Renamings.Init;
8779 Instance_Envs.Init;
8780 Generic_Flags.Init;
8781 Generic_Renamings_HTable.Reset;
8782 Circularity_Detected := False;
8783 Exchanged_Views := No_Elist;
8784 Hidden_Entities := No_Elist;
8785 end Initialize;
8786
8787 -------------------------------------
8788 -- Insert_Freeze_Node_For_Instance --
8789 -------------------------------------
8790
8791 procedure Insert_Freeze_Node_For_Instance
8792 (N : Node_Id;
8793 F_Node : Node_Id)
8794 is
8795 Decl : Node_Id;
8796 Decls : List_Id;
8797 Inst : Entity_Id;
8798 Par_N : Node_Id;
8799
8800 function Enclosing_Body (N : Node_Id) return Node_Id;
8801 -- Find enclosing package or subprogram body, if any. Freeze node may
8802 -- be placed at end of current declarative list if previous instance
8803 -- and current one have different enclosing bodies.
8804
8805 function Previous_Instance (Gen : Entity_Id) return Entity_Id;
8806 -- Find the local instance, if any, that declares the generic that is
8807 -- being instantiated. If present, the freeze node for this instance
8808 -- must follow the freeze node for the previous instance.
8809
8810 --------------------
8811 -- Enclosing_Body --
8812 --------------------
8813
8814 function Enclosing_Body (N : Node_Id) return Node_Id is
8815 P : Node_Id;
8816
8817 begin
8818 P := Parent (N);
8819 while Present (P)
8820 and then Nkind (Parent (P)) /= N_Compilation_Unit
8821 loop
8822 if Nkind_In (P, N_Package_Body, N_Subprogram_Body) then
8823 if Nkind (Parent (P)) = N_Subunit then
8824 return Corresponding_Stub (Parent (P));
8825 else
8826 return P;
8827 end if;
8828 end if;
8829
8830 P := True_Parent (P);
8831 end loop;
8832
8833 return Empty;
8834 end Enclosing_Body;
8835
8836 -----------------------
8837 -- Previous_Instance --
8838 -----------------------
8839
8840 function Previous_Instance (Gen : Entity_Id) return Entity_Id is
8841 S : Entity_Id;
8842
8843 begin
8844 S := Scope (Gen);
8845 while Present (S) and then S /= Standard_Standard loop
8846 if Is_Generic_Instance (S)
8847 and then In_Same_Source_Unit (S, N)
8848 then
8849 return S;
8850 end if;
8851
8852 S := Scope (S);
8853 end loop;
8854
8855 return Empty;
8856 end Previous_Instance;
8857
8858 -- Start of processing for Insert_Freeze_Node_For_Instance
8859
8860 begin
8861 if not Is_List_Member (F_Node) then
8862 Decl := N;
8863 Decls := List_Containing (N);
8864 Inst := Entity (F_Node);
8865 Par_N := Parent (Decls);
8866
8867 -- When processing a subprogram instantiation, utilize the actual
8868 -- subprogram instantiation rather than its package wrapper as it
8869 -- carries all the context information.
8870
8871 if Is_Wrapper_Package (Inst) then
8872 Inst := Related_Instance (Inst);
8873 end if;
8874
8875 -- If this is a package instance, check whether the generic is
8876 -- declared in a previous instance and the current instance is
8877 -- not within the previous one.
8878
8879 if Present (Generic_Parent (Parent (Inst)))
8880 and then Is_In_Main_Unit (N)
8881 then
8882 declare
8883 Enclosing_N : constant Node_Id := Enclosing_Body (N);
8884 Par_I : constant Entity_Id :=
8885 Previous_Instance
8886 (Generic_Parent (Parent (Inst)));
8887 Scop : Entity_Id;
8888
8889 begin
8890 if Present (Par_I)
8891 and then Earlier (N, Freeze_Node (Par_I))
8892 then
8893 Scop := Scope (Inst);
8894
8895 -- If the current instance is within the one that contains
8896 -- the generic, the freeze node for the current one must
8897 -- appear in the current declarative part. Ditto, if the
8898 -- current instance is within another package instance or
8899 -- within a body that does not enclose the current instance.
8900 -- In these three cases the freeze node of the previous
8901 -- instance is not relevant.
8902
8903 while Present (Scop) and then Scop /= Standard_Standard loop
8904 exit when Scop = Par_I
8905 or else
8906 (Is_Generic_Instance (Scop)
8907 and then Scope_Depth (Scop) > Scope_Depth (Par_I));
8908 Scop := Scope (Scop);
8909 end loop;
8910
8911 -- Previous instance encloses current instance
8912
8913 if Scop = Par_I then
8914 null;
8915
8916 -- If the next node is a source body we must freeze in
8917 -- the current scope as well.
8918
8919 elsif Present (Next (N))
8920 and then Nkind_In (Next (N), N_Subprogram_Body,
8921 N_Package_Body)
8922 and then Comes_From_Source (Next (N))
8923 then
8924 null;
8925
8926 -- Current instance is within an unrelated instance
8927
8928 elsif Is_Generic_Instance (Scop) then
8929 null;
8930
8931 -- Current instance is within an unrelated body
8932
8933 elsif Present (Enclosing_N)
8934 and then Enclosing_N /= Enclosing_Body (Par_I)
8935 then
8936 null;
8937
8938 else
8939 Insert_After (Freeze_Node (Par_I), F_Node);
8940 return;
8941 end if;
8942 end if;
8943 end;
8944 end if;
8945
8946 -- When the instantiation occurs in a package declaration, append the
8947 -- freeze node to the private declarations (if any).
8948
8949 if Nkind (Par_N) = N_Package_Specification
8950 and then Decls = Visible_Declarations (Par_N)
8951 and then Present (Private_Declarations (Par_N))
8952 and then not Is_Empty_List (Private_Declarations (Par_N))
8953 then
8954 Decls := Private_Declarations (Par_N);
8955 Decl := First (Decls);
8956 end if;
8957
8958 -- Determine the proper freeze point of a package instantiation. We
8959 -- adhere to the general rule of a package or subprogram body causing
8960 -- freezing of anything before it in the same declarative region. In
8961 -- this case, the proper freeze point of a package instantiation is
8962 -- before the first source body which follows, or before a stub. This
8963 -- ensures that entities coming from the instance are already frozen
8964 -- and usable in source bodies.
8965
8966 if Nkind (Par_N) /= N_Package_Declaration
8967 and then Ekind (Inst) = E_Package
8968 and then Is_Generic_Instance (Inst)
8969 and then
8970 not In_Same_Source_Unit (Generic_Parent (Parent (Inst)), Inst)
8971 then
8972 while Present (Decl) loop
8973 if (Nkind (Decl) in N_Unit_Body
8974 or else
8975 Nkind (Decl) in N_Body_Stub)
8976 and then Comes_From_Source (Decl)
8977 then
8978 Insert_Before (Decl, F_Node);
8979 return;
8980 end if;
8981
8982 Next (Decl);
8983 end loop;
8984 end if;
8985
8986 -- In a package declaration, or if no previous body, insert at end
8987 -- of list.
8988
8989 Set_Sloc (F_Node, Sloc (Last (Decls)));
8990 Insert_After (Last (Decls), F_Node);
8991 end if;
8992 end Insert_Freeze_Node_For_Instance;
8993
8994 ------------------
8995 -- Install_Body --
8996 ------------------
8997
8998 procedure Install_Body
8999 (Act_Body : Node_Id;
9000 N : Node_Id;
9001 Gen_Body : Node_Id;
9002 Gen_Decl : Node_Id)
9003 is
9004 function In_Same_Scope (Gen_Id, Act_Id : Node_Id) return Boolean;
9005 -- Check if the generic definition and the instantiation come from
9006 -- a common scope, in which case the instance must be frozen after
9007 -- the generic body.
9008
9009 function True_Sloc (N, Act_Unit : Node_Id) return Source_Ptr;
9010 -- If the instance is nested inside a generic unit, the Sloc of the
9011 -- instance indicates the place of the original definition, not the
9012 -- point of the current enclosing instance. Pending a better usage of
9013 -- Slocs to indicate instantiation places, we determine the place of
9014 -- origin of a node by finding the maximum sloc of any ancestor node.
9015 -- Why is this not equivalent to Top_Level_Location ???
9016
9017 -------------------
9018 -- In_Same_Scope --
9019 -------------------
9020
9021 function In_Same_Scope (Gen_Id, Act_Id : Node_Id) return Boolean is
9022 Act_Scop : Entity_Id := Scope (Act_Id);
9023 Gen_Scop : Entity_Id := Scope (Gen_Id);
9024
9025 begin
9026 while Act_Scop /= Standard_Standard
9027 and then Gen_Scop /= Standard_Standard
9028 loop
9029 if Act_Scop = Gen_Scop then
9030 return True;
9031 end if;
9032
9033 Act_Scop := Scope (Act_Scop);
9034 Gen_Scop := Scope (Gen_Scop);
9035 end loop;
9036
9037 return False;
9038 end In_Same_Scope;
9039
9040 ---------------
9041 -- True_Sloc --
9042 ---------------
9043
9044 function True_Sloc (N, Act_Unit : Node_Id) return Source_Ptr is
9045 N1 : Node_Id;
9046 Res : Source_Ptr;
9047
9048 begin
9049 Res := Sloc (N);
9050 N1 := N;
9051 while Present (N1) and then N1 /= Act_Unit loop
9052 if Sloc (N1) > Res then
9053 Res := Sloc (N1);
9054 end if;
9055
9056 N1 := Parent (N1);
9057 end loop;
9058
9059 return Res;
9060 end True_Sloc;
9061
9062 Act_Id : constant Entity_Id := Corresponding_Spec (Act_Body);
9063 Act_Unit : constant Node_Id := Unit (Cunit (Get_Source_Unit (N)));
9064 Gen_Id : constant Entity_Id := Corresponding_Spec (Gen_Body);
9065 Par : constant Entity_Id := Scope (Gen_Id);
9066 Gen_Unit : constant Node_Id :=
9067 Unit (Cunit (Get_Source_Unit (Gen_Decl)));
9068
9069 Body_Unit : Node_Id;
9070 F_Node : Node_Id;
9071 Must_Delay : Boolean;
9072 Orig_Body : Node_Id := Gen_Body;
9073
9074 -- Start of processing for Install_Body
9075
9076 begin
9077 -- Handle first the case of an instance with incomplete actual types.
9078 -- The instance body cannot be placed after the declaration because
9079 -- full views have not been seen yet. Any use of the non-limited views
9080 -- in the instance body requires the presence of a regular with_clause
9081 -- in the enclosing unit, and will fail if this with_clause is missing.
9082 -- We place the instance body at the beginning of the enclosing body,
9083 -- which is the unit being compiled. The freeze node for the instance
9084 -- is then placed after the instance body.
9085
9086 if not Is_Empty_Elmt_List (Incomplete_Actuals (Act_Id))
9087 and then Expander_Active
9088 and then Ekind (Scope (Act_Id)) = E_Package
9089 then
9090 declare
9091 Scop : constant Entity_Id := Scope (Act_Id);
9092 Body_Id : constant Node_Id :=
9093 Corresponding_Body (Unit_Declaration_Node (Scop));
9094
9095 begin
9096 Ensure_Freeze_Node (Act_Id);
9097 F_Node := Freeze_Node (Act_Id);
9098 if Present (Body_Id) then
9099 Set_Is_Frozen (Act_Id, False);
9100 Prepend (Act_Body, Declarations (Parent (Body_Id)));
9101 if Is_List_Member (F_Node) then
9102 Remove (F_Node);
9103 end if;
9104
9105 Insert_After (Act_Body, F_Node);
9106 end if;
9107 end;
9108 return;
9109 end if;
9110
9111 -- If the body is a subunit, the freeze point is the corresponding stub
9112 -- in the current compilation, not the subunit itself.
9113
9114 if Nkind (Parent (Gen_Body)) = N_Subunit then
9115 Orig_Body := Corresponding_Stub (Parent (Gen_Body));
9116 else
9117 Orig_Body := Gen_Body;
9118 end if;
9119
9120 Body_Unit := Unit (Cunit (Get_Source_Unit (Orig_Body)));
9121
9122 -- If the instantiation and the generic definition appear in the same
9123 -- package declaration, this is an early instantiation. If they appear
9124 -- in the same declarative part, it is an early instantiation only if
9125 -- the generic body appears textually later, and the generic body is
9126 -- also in the main unit.
9127
9128 -- If instance is nested within a subprogram, and the generic body
9129 -- is not, the instance is delayed because the enclosing body is. If
9130 -- instance and body are within the same scope, or the same subprogram
9131 -- body, indicate explicitly that the instance is delayed.
9132
9133 Must_Delay :=
9134 (Gen_Unit = Act_Unit
9135 and then (Nkind_In (Gen_Unit, N_Generic_Package_Declaration,
9136 N_Package_Declaration)
9137 or else (Gen_Unit = Body_Unit
9138 and then True_Sloc (N, Act_Unit)
9139 < Sloc (Orig_Body)))
9140 and then Is_In_Main_Unit (Original_Node (Gen_Unit))
9141 and then In_Same_Scope (Gen_Id, Act_Id));
9142
9143 -- If this is an early instantiation, the freeze node is placed after
9144 -- the generic body. Otherwise, if the generic appears in an instance,
9145 -- we cannot freeze the current instance until the outer one is frozen.
9146 -- This is only relevant if the current instance is nested within some
9147 -- inner scope not itself within the outer instance. If this scope is
9148 -- a package body in the same declarative part as the outer instance,
9149 -- then that body needs to be frozen after the outer instance. Finally,
9150 -- if no delay is needed, we place the freeze node at the end of the
9151 -- current declarative part.
9152
9153 if Expander_Active then
9154 Ensure_Freeze_Node (Act_Id);
9155 F_Node := Freeze_Node (Act_Id);
9156
9157 if Must_Delay then
9158 Insert_After (Orig_Body, F_Node);
9159
9160 elsif Is_Generic_Instance (Par)
9161 and then Present (Freeze_Node (Par))
9162 and then Scope (Act_Id) /= Par
9163 then
9164 -- Freeze instance of inner generic after instance of enclosing
9165 -- generic.
9166
9167 if In_Same_Declarative_Part (Freeze_Node (Par), N) then
9168
9169 -- Handle the following case:
9170
9171 -- package Parent_Inst is new ...
9172 -- Parent_Inst []
9173
9174 -- procedure P ... -- this body freezes Parent_Inst
9175
9176 -- package Inst is new ...
9177
9178 -- In this particular scenario, the freeze node for Inst must
9179 -- be inserted in the same manner as that of Parent_Inst,
9180 -- before the next source body or at the end of the declarative
9181 -- list (body not available). If body P did not exist and
9182 -- Parent_Inst was frozen after Inst, either by a body
9183 -- following Inst or at the end of the declarative region,
9184 -- the freeze node for Inst must be inserted after that of
9185 -- Parent_Inst. This relation is established by comparing
9186 -- the Slocs of Parent_Inst freeze node and Inst.
9187
9188 if List_Containing (Get_Package_Instantiation_Node (Par)) =
9189 List_Containing (N)
9190 and then Sloc (Freeze_Node (Par)) < Sloc (N)
9191 then
9192 Insert_Freeze_Node_For_Instance (N, F_Node);
9193 else
9194 Insert_After (Freeze_Node (Par), F_Node);
9195 end if;
9196
9197 -- Freeze package enclosing instance of inner generic after
9198 -- instance of enclosing generic.
9199
9200 elsif Nkind_In (Parent (N), N_Package_Body, N_Subprogram_Body)
9201 and then In_Same_Declarative_Part (Freeze_Node (Par), Parent (N))
9202 then
9203 declare
9204 Enclosing : Entity_Id;
9205
9206 begin
9207 Enclosing := Corresponding_Spec (Parent (N));
9208
9209 if No (Enclosing) then
9210 Enclosing := Defining_Entity (Parent (N));
9211 end if;
9212
9213 Insert_Freeze_Node_For_Instance (N, F_Node);
9214 Ensure_Freeze_Node (Enclosing);
9215
9216 if not Is_List_Member (Freeze_Node (Enclosing)) then
9217
9218 -- The enclosing context is a subunit, insert the freeze
9219 -- node after the stub.
9220
9221 if Nkind (Parent (Parent (N))) = N_Subunit then
9222 Insert_Freeze_Node_For_Instance
9223 (Corresponding_Stub (Parent (Parent (N))),
9224 Freeze_Node (Enclosing));
9225
9226 -- The enclosing context is a package with a stub body
9227 -- which has already been replaced by the real body.
9228 -- Insert the freeze node after the actual body.
9229
9230 elsif Ekind (Enclosing) = E_Package
9231 and then Present (Body_Entity (Enclosing))
9232 and then Was_Originally_Stub
9233 (Parent (Body_Entity (Enclosing)))
9234 then
9235 Insert_Freeze_Node_For_Instance
9236 (Parent (Body_Entity (Enclosing)),
9237 Freeze_Node (Enclosing));
9238
9239 -- The parent instance has been frozen before the body of
9240 -- the enclosing package, insert the freeze node after
9241 -- the body.
9242
9243 elsif List_Containing (Freeze_Node (Par)) =
9244 List_Containing (Parent (N))
9245 and then Sloc (Freeze_Node (Par)) < Sloc (Parent (N))
9246 then
9247 Insert_Freeze_Node_For_Instance
9248 (Parent (N), Freeze_Node (Enclosing));
9249
9250 else
9251 Insert_After
9252 (Freeze_Node (Par), Freeze_Node (Enclosing));
9253 end if;
9254 end if;
9255 end;
9256
9257 else
9258 Insert_Freeze_Node_For_Instance (N, F_Node);
9259 end if;
9260
9261 else
9262 Insert_Freeze_Node_For_Instance (N, F_Node);
9263 end if;
9264 end if;
9265
9266 Set_Is_Frozen (Act_Id);
9267 Insert_Before (N, Act_Body);
9268 Mark_Rewrite_Insertion (Act_Body);
9269 end Install_Body;
9270
9271 -----------------------------
9272 -- Install_Formal_Packages --
9273 -----------------------------
9274
9275 procedure Install_Formal_Packages (Par : Entity_Id) is
9276 E : Entity_Id;
9277 Gen : Entity_Id;
9278 Gen_E : Entity_Id := Empty;
9279
9280 begin
9281 E := First_Entity (Par);
9282
9283 -- If we are installing an instance parent, locate the formal packages
9284 -- of its generic parent.
9285
9286 if Is_Generic_Instance (Par) then
9287 Gen := Generic_Parent (Package_Specification (Par));
9288 Gen_E := First_Entity (Gen);
9289 end if;
9290
9291 while Present (E) loop
9292 if Ekind (E) = E_Package
9293 and then Nkind (Parent (E)) = N_Package_Renaming_Declaration
9294 then
9295 -- If this is the renaming for the parent instance, done
9296
9297 if Renamed_Object (E) = Par then
9298 exit;
9299
9300 -- The visibility of a formal of an enclosing generic is already
9301 -- correct.
9302
9303 elsif Denotes_Formal_Package (E) then
9304 null;
9305
9306 elsif Present (Associated_Formal_Package (E)) then
9307 Check_Generic_Actuals (Renamed_Object (E), True);
9308 Set_Is_Hidden (E, False);
9309
9310 -- Find formal package in generic unit that corresponds to
9311 -- (instance of) formal package in instance.
9312
9313 while Present (Gen_E) and then Chars (Gen_E) /= Chars (E) loop
9314 Next_Entity (Gen_E);
9315 end loop;
9316
9317 if Present (Gen_E) then
9318 Map_Formal_Package_Entities (Gen_E, E);
9319 end if;
9320 end if;
9321 end if;
9322
9323 Next_Entity (E);
9324
9325 if Present (Gen_E) then
9326 Next_Entity (Gen_E);
9327 end if;
9328 end loop;
9329 end Install_Formal_Packages;
9330
9331 --------------------
9332 -- Install_Parent --
9333 --------------------
9334
9335 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False) is
9336 Ancestors : constant Elist_Id := New_Elmt_List;
9337 S : constant Entity_Id := Current_Scope;
9338 Inst_Par : Entity_Id;
9339 First_Par : Entity_Id;
9340 Inst_Node : Node_Id;
9341 Gen_Par : Entity_Id;
9342 First_Gen : Entity_Id;
9343 Elmt : Elmt_Id;
9344
9345 procedure Install_Noninstance_Specs (Par : Entity_Id);
9346 -- Install the scopes of noninstance parent units ending with Par
9347
9348 procedure Install_Spec (Par : Entity_Id);
9349 -- The child unit is within the declarative part of the parent, so the
9350 -- declarations within the parent are immediately visible.
9351
9352 -------------------------------
9353 -- Install_Noninstance_Specs --
9354 -------------------------------
9355
9356 procedure Install_Noninstance_Specs (Par : Entity_Id) is
9357 begin
9358 if Present (Par)
9359 and then Par /= Standard_Standard
9360 and then not In_Open_Scopes (Par)
9361 then
9362 Install_Noninstance_Specs (Scope (Par));
9363 Install_Spec (Par);
9364 end if;
9365 end Install_Noninstance_Specs;
9366
9367 ------------------
9368 -- Install_Spec --
9369 ------------------
9370
9371 procedure Install_Spec (Par : Entity_Id) is
9372 Spec : constant Node_Id := Package_Specification (Par);
9373
9374 begin
9375 -- If this parent of the child instance is a top-level unit,
9376 -- then record the unit and its visibility for later resetting in
9377 -- Remove_Parent. We exclude units that are generic instances, as we
9378 -- only want to record this information for the ultimate top-level
9379 -- noninstance parent (is that always correct???).
9380
9381 if Scope (Par) = Standard_Standard
9382 and then not Is_Generic_Instance (Par)
9383 then
9384 Parent_Unit_Visible := Is_Immediately_Visible (Par);
9385 Instance_Parent_Unit := Par;
9386 end if;
9387
9388 -- Open the parent scope and make it and its declarations visible.
9389 -- If this point is not within a body, then only the visible
9390 -- declarations should be made visible, and installation of the
9391 -- private declarations is deferred until the appropriate point
9392 -- within analysis of the spec being instantiated (see the handling
9393 -- of parent visibility in Analyze_Package_Specification). This is
9394 -- relaxed in the case where the parent unit is Ada.Tags, to avoid
9395 -- private view problems that occur when compiling instantiations of
9396 -- a generic child of that package (Generic_Dispatching_Constructor).
9397 -- If the instance freezes a tagged type, inlinings of operations
9398 -- from Ada.Tags may need the full view of type Tag. If inlining took
9399 -- proper account of establishing visibility of inlined subprograms'
9400 -- parents then it should be possible to remove this
9401 -- special check. ???
9402
9403 Push_Scope (Par);
9404 Set_Is_Immediately_Visible (Par);
9405 Install_Visible_Declarations (Par);
9406 Set_Use (Visible_Declarations (Spec));
9407
9408 if In_Body or else Is_RTU (Par, Ada_Tags) then
9409 Install_Private_Declarations (Par);
9410 Set_Use (Private_Declarations (Spec));
9411 end if;
9412 end Install_Spec;
9413
9414 -- Start of processing for Install_Parent
9415
9416 begin
9417 -- We need to install the parent instance to compile the instantiation
9418 -- of the child, but the child instance must appear in the current
9419 -- scope. Given that we cannot place the parent above the current scope
9420 -- in the scope stack, we duplicate the current scope and unstack both
9421 -- after the instantiation is complete.
9422
9423 -- If the parent is itself the instantiation of a child unit, we must
9424 -- also stack the instantiation of its parent, and so on. Each such
9425 -- ancestor is the prefix of the name in a prior instantiation.
9426
9427 -- If this is a nested instance, the parent unit itself resolves to
9428 -- a renaming of the parent instance, whose declaration we need.
9429
9430 -- Finally, the parent may be a generic (not an instance) when the
9431 -- child unit appears as a formal package.
9432
9433 Inst_Par := P;
9434
9435 if Present (Renamed_Entity (Inst_Par)) then
9436 Inst_Par := Renamed_Entity (Inst_Par);
9437 end if;
9438
9439 First_Par := Inst_Par;
9440
9441 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
9442
9443 First_Gen := Gen_Par;
9444
9445 while Present (Gen_Par) and then Is_Child_Unit (Gen_Par) loop
9446
9447 -- Load grandparent instance as well
9448
9449 Inst_Node := Get_Package_Instantiation_Node (Inst_Par);
9450
9451 if Nkind (Name (Inst_Node)) = N_Expanded_Name then
9452 Inst_Par := Entity (Prefix (Name (Inst_Node)));
9453
9454 if Present (Renamed_Entity (Inst_Par)) then
9455 Inst_Par := Renamed_Entity (Inst_Par);
9456 end if;
9457
9458 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
9459
9460 if Present (Gen_Par) then
9461 Prepend_Elmt (Inst_Par, Ancestors);
9462
9463 else
9464 -- Parent is not the name of an instantiation
9465
9466 Install_Noninstance_Specs (Inst_Par);
9467 exit;
9468 end if;
9469
9470 else
9471 -- Previous error
9472
9473 exit;
9474 end if;
9475 end loop;
9476
9477 if Present (First_Gen) then
9478 Append_Elmt (First_Par, Ancestors);
9479 else
9480 Install_Noninstance_Specs (First_Par);
9481 end if;
9482
9483 if not Is_Empty_Elmt_List (Ancestors) then
9484 Elmt := First_Elmt (Ancestors);
9485 while Present (Elmt) loop
9486 Install_Spec (Node (Elmt));
9487 Install_Formal_Packages (Node (Elmt));
9488 Next_Elmt (Elmt);
9489 end loop;
9490 end if;
9491
9492 if not In_Body then
9493 Push_Scope (S);
9494 end if;
9495 end Install_Parent;
9496
9497 -------------------------------
9498 -- Install_Hidden_Primitives --
9499 -------------------------------
9500
9501 procedure Install_Hidden_Primitives
9502 (Prims_List : in out Elist_Id;
9503 Gen_T : Entity_Id;
9504 Act_T : Entity_Id)
9505 is
9506 Elmt : Elmt_Id;
9507 List : Elist_Id := No_Elist;
9508 Prim_G_Elmt : Elmt_Id;
9509 Prim_A_Elmt : Elmt_Id;
9510 Prim_G : Node_Id;
9511 Prim_A : Node_Id;
9512
9513 begin
9514 -- No action needed in case of serious errors because we cannot trust
9515 -- in the order of primitives
9516
9517 if Serious_Errors_Detected > 0 then
9518 return;
9519
9520 -- No action possible if we don't have available the list of primitive
9521 -- operations
9522
9523 elsif No (Gen_T)
9524 or else not Is_Record_Type (Gen_T)
9525 or else not Is_Tagged_Type (Gen_T)
9526 or else not Is_Record_Type (Act_T)
9527 or else not Is_Tagged_Type (Act_T)
9528 then
9529 return;
9530
9531 -- There is no need to handle interface types since their primitives
9532 -- cannot be hidden
9533
9534 elsif Is_Interface (Gen_T) then
9535 return;
9536 end if;
9537
9538 Prim_G_Elmt := First_Elmt (Primitive_Operations (Gen_T));
9539
9540 if not Is_Class_Wide_Type (Act_T) then
9541 Prim_A_Elmt := First_Elmt (Primitive_Operations (Act_T));
9542 else
9543 Prim_A_Elmt := First_Elmt (Primitive_Operations (Root_Type (Act_T)));
9544 end if;
9545
9546 loop
9547 -- Skip predefined primitives in the generic formal
9548
9549 while Present (Prim_G_Elmt)
9550 and then Is_Predefined_Dispatching_Operation (Node (Prim_G_Elmt))
9551 loop
9552 Next_Elmt (Prim_G_Elmt);
9553 end loop;
9554
9555 -- Skip predefined primitives in the generic actual
9556
9557 while Present (Prim_A_Elmt)
9558 and then Is_Predefined_Dispatching_Operation (Node (Prim_A_Elmt))
9559 loop
9560 Next_Elmt (Prim_A_Elmt);
9561 end loop;
9562
9563 exit when No (Prim_G_Elmt) or else No (Prim_A_Elmt);
9564
9565 Prim_G := Node (Prim_G_Elmt);
9566 Prim_A := Node (Prim_A_Elmt);
9567
9568 -- There is no need to handle interface primitives because their
9569 -- primitives are not hidden
9570
9571 exit when Present (Interface_Alias (Prim_G));
9572
9573 -- Here we install one hidden primitive
9574
9575 if Chars (Prim_G) /= Chars (Prim_A)
9576 and then Has_Suffix (Prim_A, 'P')
9577 and then Remove_Suffix (Prim_A, 'P') = Chars (Prim_G)
9578 then
9579 Set_Chars (Prim_A, Chars (Prim_G));
9580 Append_New_Elmt (Prim_A, To => List);
9581 end if;
9582
9583 Next_Elmt (Prim_A_Elmt);
9584 Next_Elmt (Prim_G_Elmt);
9585 end loop;
9586
9587 -- Append the elements to the list of temporarily visible primitives
9588 -- avoiding duplicates.
9589
9590 if Present (List) then
9591 if No (Prims_List) then
9592 Prims_List := New_Elmt_List;
9593 end if;
9594
9595 Elmt := First_Elmt (List);
9596 while Present (Elmt) loop
9597 Append_Unique_Elmt (Node (Elmt), Prims_List);
9598 Next_Elmt (Elmt);
9599 end loop;
9600 end if;
9601 end Install_Hidden_Primitives;
9602
9603 -------------------------------
9604 -- Restore_Hidden_Primitives --
9605 -------------------------------
9606
9607 procedure Restore_Hidden_Primitives (Prims_List : in out Elist_Id) is
9608 Prim_Elmt : Elmt_Id;
9609 Prim : Node_Id;
9610
9611 begin
9612 if Prims_List /= No_Elist then
9613 Prim_Elmt := First_Elmt (Prims_List);
9614 while Present (Prim_Elmt) loop
9615 Prim := Node (Prim_Elmt);
9616 Set_Chars (Prim, Add_Suffix (Prim, 'P'));
9617 Next_Elmt (Prim_Elmt);
9618 end loop;
9619
9620 Prims_List := No_Elist;
9621 end if;
9622 end Restore_Hidden_Primitives;
9623
9624 --------------------------------
9625 -- Instantiate_Formal_Package --
9626 --------------------------------
9627
9628 function Instantiate_Formal_Package
9629 (Formal : Node_Id;
9630 Actual : Node_Id;
9631 Analyzed_Formal : Node_Id) return List_Id
9632 is
9633 Loc : constant Source_Ptr := Sloc (Actual);
9634 Actual_Pack : Entity_Id;
9635 Formal_Pack : Entity_Id;
9636 Gen_Parent : Entity_Id;
9637 Decls : List_Id;
9638 Nod : Node_Id;
9639 Parent_Spec : Node_Id;
9640
9641 procedure Find_Matching_Actual
9642 (F : Node_Id;
9643 Act : in out Entity_Id);
9644 -- We need to associate each formal entity in the formal package with
9645 -- the corresponding entity in the actual package. The actual package
9646 -- has been analyzed and possibly expanded, and as a result there is
9647 -- no one-to-one correspondence between the two lists (for example,
9648 -- the actual may include subtypes, itypes, and inherited primitive
9649 -- operations, interspersed among the renaming declarations for the
9650 -- actuals). We retrieve the corresponding actual by name because each
9651 -- actual has the same name as the formal, and they do appear in the
9652 -- same order.
9653
9654 function Get_Formal_Entity (N : Node_Id) return Entity_Id;
9655 -- Retrieve entity of defining entity of generic formal parameter.
9656 -- Only the declarations of formals need to be considered when
9657 -- linking them to actuals, but the declarative list may include
9658 -- internal entities generated during analysis, and those are ignored.
9659
9660 procedure Match_Formal_Entity
9661 (Formal_Node : Node_Id;
9662 Formal_Ent : Entity_Id;
9663 Actual_Ent : Entity_Id);
9664 -- Associates the formal entity with the actual. In the case where
9665 -- Formal_Ent is a formal package, this procedure iterates through all
9666 -- of its formals and enters associations between the actuals occurring
9667 -- in the formal package's corresponding actual package (given by
9668 -- Actual_Ent) and the formal package's formal parameters. This
9669 -- procedure recurses if any of the parameters is itself a package.
9670
9671 function Is_Instance_Of
9672 (Act_Spec : Entity_Id;
9673 Gen_Anc : Entity_Id) return Boolean;
9674 -- The actual can be an instantiation of a generic within another
9675 -- instance, in which case there is no direct link from it to the
9676 -- original generic ancestor. In that case, we recognize that the
9677 -- ultimate ancestor is the same by examining names and scopes.
9678
9679 procedure Process_Nested_Formal (Formal : Entity_Id);
9680 -- If the current formal is declared with a box, its own formals are
9681 -- visible in the instance, as they were in the generic, and their
9682 -- Hidden flag must be reset. If some of these formals are themselves
9683 -- packages declared with a box, the processing must be recursive.
9684
9685 --------------------------
9686 -- Find_Matching_Actual --
9687 --------------------------
9688
9689 procedure Find_Matching_Actual
9690 (F : Node_Id;
9691 Act : in out Entity_Id)
9692 is
9693 Formal_Ent : Entity_Id;
9694
9695 begin
9696 case Nkind (Original_Node (F)) is
9697 when N_Formal_Object_Declaration
9698 | N_Formal_Type_Declaration
9699 =>
9700 Formal_Ent := Defining_Identifier (F);
9701
9702 while Chars (Act) /= Chars (Formal_Ent) loop
9703 Next_Entity (Act);
9704 end loop;
9705
9706 when N_Formal_Package_Declaration
9707 | N_Formal_Subprogram_Declaration
9708 | N_Generic_Package_Declaration
9709 | N_Package_Declaration
9710 =>
9711 Formal_Ent := Defining_Entity (F);
9712
9713 while Chars (Act) /= Chars (Formal_Ent) loop
9714 Next_Entity (Act);
9715 end loop;
9716
9717 when others =>
9718 raise Program_Error;
9719 end case;
9720 end Find_Matching_Actual;
9721
9722 -------------------------
9723 -- Match_Formal_Entity --
9724 -------------------------
9725
9726 procedure Match_Formal_Entity
9727 (Formal_Node : Node_Id;
9728 Formal_Ent : Entity_Id;
9729 Actual_Ent : Entity_Id)
9730 is
9731 Act_Pkg : Entity_Id;
9732
9733 begin
9734 Set_Instance_Of (Formal_Ent, Actual_Ent);
9735
9736 if Ekind (Actual_Ent) = E_Package then
9737
9738 -- Record associations for each parameter
9739
9740 Act_Pkg := Actual_Ent;
9741
9742 declare
9743 A_Ent : Entity_Id := First_Entity (Act_Pkg);
9744 F_Ent : Entity_Id;
9745 F_Node : Node_Id;
9746
9747 Gen_Decl : Node_Id;
9748 Formals : List_Id;
9749 Actual : Entity_Id;
9750
9751 begin
9752 -- Retrieve the actual given in the formal package declaration
9753
9754 Actual := Entity (Name (Original_Node (Formal_Node)));
9755
9756 -- The actual in the formal package declaration may be a
9757 -- renamed generic package, in which case we want to retrieve
9758 -- the original generic in order to traverse its formal part.
9759
9760 if Present (Renamed_Entity (Actual)) then
9761 Gen_Decl := Unit_Declaration_Node (Renamed_Entity (Actual));
9762 else
9763 Gen_Decl := Unit_Declaration_Node (Actual);
9764 end if;
9765
9766 Formals := Generic_Formal_Declarations (Gen_Decl);
9767
9768 if Present (Formals) then
9769 F_Node := First_Non_Pragma (Formals);
9770 else
9771 F_Node := Empty;
9772 end if;
9773
9774 while Present (A_Ent)
9775 and then Present (F_Node)
9776 and then A_Ent /= First_Private_Entity (Act_Pkg)
9777 loop
9778 F_Ent := Get_Formal_Entity (F_Node);
9779
9780 if Present (F_Ent) then
9781
9782 -- This is a formal of the original package. Record
9783 -- association and recurse.
9784
9785 Find_Matching_Actual (F_Node, A_Ent);
9786 Match_Formal_Entity (F_Node, F_Ent, A_Ent);
9787 Next_Entity (A_Ent);
9788 end if;
9789
9790 Next_Non_Pragma (F_Node);
9791 end loop;
9792 end;
9793 end if;
9794 end Match_Formal_Entity;
9795
9796 -----------------------
9797 -- Get_Formal_Entity --
9798 -----------------------
9799
9800 function Get_Formal_Entity (N : Node_Id) return Entity_Id is
9801 Kind : constant Node_Kind := Nkind (Original_Node (N));
9802 begin
9803 case Kind is
9804 when N_Formal_Object_Declaration =>
9805 return Defining_Identifier (N);
9806
9807 when N_Formal_Type_Declaration =>
9808 return Defining_Identifier (N);
9809
9810 when N_Formal_Subprogram_Declaration =>
9811 return Defining_Unit_Name (Specification (N));
9812
9813 when N_Formal_Package_Declaration =>
9814 return Defining_Identifier (Original_Node (N));
9815
9816 when N_Generic_Package_Declaration =>
9817 return Defining_Identifier (Original_Node (N));
9818
9819 -- All other declarations are introduced by semantic analysis and
9820 -- have no match in the actual.
9821
9822 when others =>
9823 return Empty;
9824 end case;
9825 end Get_Formal_Entity;
9826
9827 --------------------
9828 -- Is_Instance_Of --
9829 --------------------
9830
9831 function Is_Instance_Of
9832 (Act_Spec : Entity_Id;
9833 Gen_Anc : Entity_Id) return Boolean
9834 is
9835 Gen_Par : constant Entity_Id := Generic_Parent (Act_Spec);
9836
9837 begin
9838 if No (Gen_Par) then
9839 return False;
9840
9841 -- Simplest case: the generic parent of the actual is the formal
9842
9843 elsif Gen_Par = Gen_Anc then
9844 return True;
9845
9846 elsif Chars (Gen_Par) /= Chars (Gen_Anc) then
9847 return False;
9848
9849 -- The actual may be obtained through several instantiations. Its
9850 -- scope must itself be an instance of a generic declared in the
9851 -- same scope as the formal. Any other case is detected above.
9852
9853 elsif not Is_Generic_Instance (Scope (Gen_Par)) then
9854 return False;
9855
9856 else
9857 return Generic_Parent (Parent (Scope (Gen_Par))) = Scope (Gen_Anc);
9858 end if;
9859 end Is_Instance_Of;
9860
9861 ---------------------------
9862 -- Process_Nested_Formal --
9863 ---------------------------
9864
9865 procedure Process_Nested_Formal (Formal : Entity_Id) is
9866 Ent : Entity_Id;
9867
9868 begin
9869 if Present (Associated_Formal_Package (Formal))
9870 and then Box_Present (Parent (Associated_Formal_Package (Formal)))
9871 then
9872 Ent := First_Entity (Formal);
9873 while Present (Ent) loop
9874 Set_Is_Hidden (Ent, False);
9875 Set_Is_Visible_Formal (Ent);
9876 Set_Is_Potentially_Use_Visible
9877 (Ent, Is_Potentially_Use_Visible (Formal));
9878
9879 if Ekind (Ent) = E_Package then
9880 exit when Renamed_Entity (Ent) = Renamed_Entity (Formal);
9881 Process_Nested_Formal (Ent);
9882 end if;
9883
9884 Next_Entity (Ent);
9885 end loop;
9886 end if;
9887 end Process_Nested_Formal;
9888
9889 -- Start of processing for Instantiate_Formal_Package
9890
9891 begin
9892 Analyze (Actual);
9893
9894 if not Is_Entity_Name (Actual)
9895 or else Ekind (Entity (Actual)) /= E_Package
9896 then
9897 Error_Msg_N
9898 ("expect package instance to instantiate formal", Actual);
9899 Abandon_Instantiation (Actual);
9900 raise Program_Error;
9901
9902 else
9903 Actual_Pack := Entity (Actual);
9904 Set_Is_Instantiated (Actual_Pack);
9905
9906 -- The actual may be a renamed package, or an outer generic formal
9907 -- package whose instantiation is converted into a renaming.
9908
9909 if Present (Renamed_Object (Actual_Pack)) then
9910 Actual_Pack := Renamed_Object (Actual_Pack);
9911 end if;
9912
9913 if Nkind (Analyzed_Formal) = N_Formal_Package_Declaration then
9914 Gen_Parent := Get_Instance_Of (Entity (Name (Analyzed_Formal)));
9915 Formal_Pack := Defining_Identifier (Analyzed_Formal);
9916 else
9917 Gen_Parent :=
9918 Generic_Parent (Specification (Analyzed_Formal));
9919 Formal_Pack :=
9920 Defining_Unit_Name (Specification (Analyzed_Formal));
9921 end if;
9922
9923 if Nkind (Parent (Actual_Pack)) = N_Defining_Program_Unit_Name then
9924 Parent_Spec := Package_Specification (Actual_Pack);
9925 else
9926 Parent_Spec := Parent (Actual_Pack);
9927 end if;
9928
9929 if Gen_Parent = Any_Id then
9930 Error_Msg_N
9931 ("previous error in declaration of formal package", Actual);
9932 Abandon_Instantiation (Actual);
9933
9934 elsif
9935 Is_Instance_Of (Parent_Spec, Get_Instance_Of (Gen_Parent))
9936 then
9937 null;
9938
9939 else
9940 Error_Msg_NE
9941 ("actual parameter must be instance of&", Actual, Gen_Parent);
9942 Abandon_Instantiation (Actual);
9943 end if;
9944
9945 Set_Instance_Of (Defining_Identifier (Formal), Actual_Pack);
9946 Map_Formal_Package_Entities (Formal_Pack, Actual_Pack);
9947
9948 Nod :=
9949 Make_Package_Renaming_Declaration (Loc,
9950 Defining_Unit_Name => New_Copy (Defining_Identifier (Formal)),
9951 Name => New_Occurrence_Of (Actual_Pack, Loc));
9952
9953 Set_Associated_Formal_Package
9954 (Defining_Unit_Name (Nod), Defining_Identifier (Formal));
9955 Decls := New_List (Nod);
9956
9957 -- If the formal F has a box, then the generic declarations are
9958 -- visible in the generic G. In an instance of G, the corresponding
9959 -- entities in the actual for F (which are the actuals for the
9960 -- instantiation of the generic that F denotes) must also be made
9961 -- visible for analysis of the current instance. On exit from the
9962 -- current instance, those entities are made private again. If the
9963 -- actual is currently in use, these entities are also use-visible.
9964
9965 -- The loop through the actual entities also steps through the formal
9966 -- entities and enters associations from formals to actuals into the
9967 -- renaming map. This is necessary to properly handle checking of
9968 -- actual parameter associations for later formals that depend on
9969 -- actuals declared in the formal package.
9970
9971 -- In Ada 2005, partial parameterization requires that we make
9972 -- visible the actuals corresponding to formals that were defaulted
9973 -- in the formal package. There formals are identified because they
9974 -- remain formal generics within the formal package, rather than
9975 -- being renamings of the actuals supplied.
9976
9977 declare
9978 Gen_Decl : constant Node_Id :=
9979 Unit_Declaration_Node (Gen_Parent);
9980 Formals : constant List_Id :=
9981 Generic_Formal_Declarations (Gen_Decl);
9982
9983 Actual_Ent : Entity_Id;
9984 Actual_Of_Formal : Node_Id;
9985 Formal_Node : Node_Id;
9986 Formal_Ent : Entity_Id;
9987
9988 begin
9989 if Present (Formals) then
9990 Formal_Node := First_Non_Pragma (Formals);
9991 else
9992 Formal_Node := Empty;
9993 end if;
9994
9995 Actual_Ent := First_Entity (Actual_Pack);
9996 Actual_Of_Formal :=
9997 First (Visible_Declarations (Specification (Analyzed_Formal)));
9998 while Present (Actual_Ent)
9999 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
10000 loop
10001 if Present (Formal_Node) then
10002 Formal_Ent := Get_Formal_Entity (Formal_Node);
10003
10004 if Present (Formal_Ent) then
10005 Find_Matching_Actual (Formal_Node, Actual_Ent);
10006 Match_Formal_Entity (Formal_Node, Formal_Ent, Actual_Ent);
10007
10008 -- We iterate at the same time over the actuals of the
10009 -- local package created for the formal, to determine
10010 -- which one of the formals of the original generic were
10011 -- defaulted in the formal. The corresponding actual
10012 -- entities are visible in the enclosing instance.
10013
10014 if Box_Present (Formal)
10015 or else
10016 (Present (Actual_Of_Formal)
10017 and then
10018 Is_Generic_Formal
10019 (Get_Formal_Entity (Actual_Of_Formal)))
10020 then
10021 Set_Is_Hidden (Actual_Ent, False);
10022 Set_Is_Visible_Formal (Actual_Ent);
10023 Set_Is_Potentially_Use_Visible
10024 (Actual_Ent, In_Use (Actual_Pack));
10025
10026 if Ekind (Actual_Ent) = E_Package then
10027 Process_Nested_Formal (Actual_Ent);
10028 end if;
10029
10030 else
10031 Set_Is_Hidden (Actual_Ent);
10032 Set_Is_Potentially_Use_Visible (Actual_Ent, False);
10033 end if;
10034 end if;
10035
10036 Next_Non_Pragma (Formal_Node);
10037 Next (Actual_Of_Formal);
10038
10039 else
10040 -- No further formals to match, but the generic part may
10041 -- contain inherited operation that are not hidden in the
10042 -- enclosing instance.
10043
10044 Next_Entity (Actual_Ent);
10045 end if;
10046 end loop;
10047
10048 -- Inherited subprograms generated by formal derived types are
10049 -- also visible if the types are.
10050
10051 Actual_Ent := First_Entity (Actual_Pack);
10052 while Present (Actual_Ent)
10053 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
10054 loop
10055 if Is_Overloadable (Actual_Ent)
10056 and then
10057 Nkind (Parent (Actual_Ent)) = N_Subtype_Declaration
10058 and then
10059 not Is_Hidden (Defining_Identifier (Parent (Actual_Ent)))
10060 then
10061 Set_Is_Hidden (Actual_Ent, False);
10062 Set_Is_Potentially_Use_Visible
10063 (Actual_Ent, In_Use (Actual_Pack));
10064 end if;
10065
10066 Next_Entity (Actual_Ent);
10067 end loop;
10068 end;
10069
10070 -- If the formal is not declared with a box, reanalyze it as an
10071 -- abbreviated instantiation, to verify the matching rules of 12.7.
10072 -- The actual checks are performed after the generic associations
10073 -- have been analyzed, to guarantee the same visibility for this
10074 -- instantiation and for the actuals.
10075
10076 -- In Ada 2005, the generic associations for the formal can include
10077 -- defaulted parameters. These are ignored during check. This
10078 -- internal instantiation is removed from the tree after conformance
10079 -- checking, because it contains formal declarations for those
10080 -- defaulted parameters, and those should not reach the back-end.
10081
10082 if not Box_Present (Formal) then
10083 declare
10084 I_Pack : constant Entity_Id :=
10085 Make_Temporary (Sloc (Actual), 'P');
10086
10087 begin
10088 Set_Is_Internal (I_Pack);
10089
10090 Append_To (Decls,
10091 Make_Package_Instantiation (Sloc (Actual),
10092 Defining_Unit_Name => I_Pack,
10093 Name =>
10094 New_Occurrence_Of
10095 (Get_Instance_Of (Gen_Parent), Sloc (Actual)),
10096 Generic_Associations => Generic_Associations (Formal)));
10097 end;
10098 end if;
10099
10100 return Decls;
10101 end if;
10102 end Instantiate_Formal_Package;
10103
10104 -----------------------------------
10105 -- Instantiate_Formal_Subprogram --
10106 -----------------------------------
10107
10108 function Instantiate_Formal_Subprogram
10109 (Formal : Node_Id;
10110 Actual : Node_Id;
10111 Analyzed_Formal : Node_Id) return Node_Id
10112 is
10113 Analyzed_S : constant Entity_Id :=
10114 Defining_Unit_Name (Specification (Analyzed_Formal));
10115 Formal_Sub : constant Entity_Id :=
10116 Defining_Unit_Name (Specification (Formal));
10117
10118 function From_Parent_Scope (Subp : Entity_Id) return Boolean;
10119 -- If the generic is a child unit, the parent has been installed on the
10120 -- scope stack, but a default subprogram cannot resolve to something
10121 -- on the parent because that parent is not really part of the visible
10122 -- context (it is there to resolve explicit local entities). If the
10123 -- default has resolved in this way, we remove the entity from immediate
10124 -- visibility and analyze the node again to emit an error message or
10125 -- find another visible candidate.
10126
10127 procedure Valid_Actual_Subprogram (Act : Node_Id);
10128 -- Perform legality check and raise exception on failure
10129
10130 -----------------------
10131 -- From_Parent_Scope --
10132 -----------------------
10133
10134 function From_Parent_Scope (Subp : Entity_Id) return Boolean is
10135 Gen_Scope : Node_Id;
10136
10137 begin
10138 Gen_Scope := Scope (Analyzed_S);
10139 while Present (Gen_Scope) and then Is_Child_Unit (Gen_Scope) loop
10140 if Scope (Subp) = Scope (Gen_Scope) then
10141 return True;
10142 end if;
10143
10144 Gen_Scope := Scope (Gen_Scope);
10145 end loop;
10146
10147 return False;
10148 end From_Parent_Scope;
10149
10150 -----------------------------
10151 -- Valid_Actual_Subprogram --
10152 -----------------------------
10153
10154 procedure Valid_Actual_Subprogram (Act : Node_Id) is
10155 Act_E : Entity_Id;
10156
10157 begin
10158 if Is_Entity_Name (Act) then
10159 Act_E := Entity (Act);
10160
10161 elsif Nkind (Act) = N_Selected_Component
10162 and then Is_Entity_Name (Selector_Name (Act))
10163 then
10164 Act_E := Entity (Selector_Name (Act));
10165
10166 else
10167 Act_E := Empty;
10168 end if;
10169
10170 if (Present (Act_E) and then Is_Overloadable (Act_E))
10171 or else Nkind_In (Act, N_Attribute_Reference,
10172 N_Indexed_Component,
10173 N_Character_Literal,
10174 N_Explicit_Dereference)
10175 then
10176 return;
10177 end if;
10178
10179 Error_Msg_NE
10180 ("expect subprogram or entry name in instantiation of &",
10181 Instantiation_Node, Formal_Sub);
10182 Abandon_Instantiation (Instantiation_Node);
10183 end Valid_Actual_Subprogram;
10184
10185 -- Local variables
10186
10187 Decl_Node : Node_Id;
10188 Loc : Source_Ptr;
10189 Nam : Node_Id;
10190 New_Spec : Node_Id;
10191 New_Subp : Entity_Id;
10192
10193 -- Start of processing for Instantiate_Formal_Subprogram
10194
10195 begin
10196 New_Spec := New_Copy_Tree (Specification (Formal));
10197
10198 -- The tree copy has created the proper instantiation sloc for the
10199 -- new specification. Use this location for all other constructed
10200 -- declarations.
10201
10202 Loc := Sloc (Defining_Unit_Name (New_Spec));
10203
10204 -- Create new entity for the actual (New_Copy_Tree does not), and
10205 -- indicate that it is an actual.
10206
10207 New_Subp := Make_Defining_Identifier (Loc, Chars (Formal_Sub));
10208 Set_Ekind (New_Subp, Ekind (Analyzed_S));
10209 Set_Is_Generic_Actual_Subprogram (New_Subp);
10210 Set_Defining_Unit_Name (New_Spec, New_Subp);
10211
10212 -- Create new entities for the each of the formals in the specification
10213 -- of the renaming declaration built for the actual.
10214
10215 if Present (Parameter_Specifications (New_Spec)) then
10216 declare
10217 F : Node_Id;
10218 F_Id : Entity_Id;
10219
10220 begin
10221 F := First (Parameter_Specifications (New_Spec));
10222 while Present (F) loop
10223 F_Id := Defining_Identifier (F);
10224
10225 Set_Defining_Identifier (F,
10226 Make_Defining_Identifier (Sloc (F_Id), Chars (F_Id)));
10227 Next (F);
10228 end loop;
10229 end;
10230 end if;
10231
10232 -- Find entity of actual. If the actual is an attribute reference, it
10233 -- cannot be resolved here (its formal is missing) but is handled
10234 -- instead in Attribute_Renaming. If the actual is overloaded, it is
10235 -- fully resolved subsequently, when the renaming declaration for the
10236 -- formal is analyzed. If it is an explicit dereference, resolve the
10237 -- prefix but not the actual itself, to prevent interpretation as call.
10238
10239 if Present (Actual) then
10240 Loc := Sloc (Actual);
10241 Set_Sloc (New_Spec, Loc);
10242
10243 if Nkind (Actual) = N_Operator_Symbol then
10244 Find_Direct_Name (Actual);
10245
10246 elsif Nkind (Actual) = N_Explicit_Dereference then
10247 Analyze (Prefix (Actual));
10248
10249 elsif Nkind (Actual) /= N_Attribute_Reference then
10250 Analyze (Actual);
10251 end if;
10252
10253 Valid_Actual_Subprogram (Actual);
10254 Nam := Actual;
10255
10256 elsif Present (Default_Name (Formal)) then
10257 if not Nkind_In (Default_Name (Formal), N_Attribute_Reference,
10258 N_Selected_Component,
10259 N_Indexed_Component,
10260 N_Character_Literal)
10261 and then Present (Entity (Default_Name (Formal)))
10262 then
10263 Nam := New_Occurrence_Of (Entity (Default_Name (Formal)), Loc);
10264 else
10265 Nam := New_Copy (Default_Name (Formal));
10266 Set_Sloc (Nam, Loc);
10267 end if;
10268
10269 elsif Box_Present (Formal) then
10270
10271 -- Actual is resolved at the point of instantiation. Create an
10272 -- identifier or operator with the same name as the formal.
10273
10274 if Nkind (Formal_Sub) = N_Defining_Operator_Symbol then
10275 Nam :=
10276 Make_Operator_Symbol (Loc,
10277 Chars => Chars (Formal_Sub),
10278 Strval => No_String);
10279 else
10280 Nam := Make_Identifier (Loc, Chars (Formal_Sub));
10281 end if;
10282
10283 elsif Nkind (Specification (Formal)) = N_Procedure_Specification
10284 and then Null_Present (Specification (Formal))
10285 then
10286 -- Generate null body for procedure, for use in the instance
10287
10288 Decl_Node :=
10289 Make_Subprogram_Body (Loc,
10290 Specification => New_Spec,
10291 Declarations => New_List,
10292 Handled_Statement_Sequence =>
10293 Make_Handled_Sequence_Of_Statements (Loc,
10294 Statements => New_List (Make_Null_Statement (Loc))));
10295
10296 Set_Is_Intrinsic_Subprogram (Defining_Unit_Name (New_Spec));
10297 return Decl_Node;
10298
10299 else
10300 Error_Msg_Sloc := Sloc (Scope (Analyzed_S));
10301 Error_Msg_NE
10302 ("missing actual&", Instantiation_Node, Formal_Sub);
10303 Error_Msg_NE
10304 ("\in instantiation of & declared#",
10305 Instantiation_Node, Scope (Analyzed_S));
10306 Abandon_Instantiation (Instantiation_Node);
10307 end if;
10308
10309 Decl_Node :=
10310 Make_Subprogram_Renaming_Declaration (Loc,
10311 Specification => New_Spec,
10312 Name => Nam);
10313
10314 -- If we do not have an actual and the formal specified <> then set to
10315 -- get proper default.
10316
10317 if No (Actual) and then Box_Present (Formal) then
10318 Set_From_Default (Decl_Node);
10319 end if;
10320
10321 -- Gather possible interpretations for the actual before analyzing the
10322 -- instance. If overloaded, it will be resolved when analyzing the
10323 -- renaming declaration.
10324
10325 if Box_Present (Formal) and then No (Actual) then
10326 Analyze (Nam);
10327
10328 if Is_Child_Unit (Scope (Analyzed_S))
10329 and then Present (Entity (Nam))
10330 then
10331 if not Is_Overloaded (Nam) then
10332 if From_Parent_Scope (Entity (Nam)) then
10333 Set_Is_Immediately_Visible (Entity (Nam), False);
10334 Set_Entity (Nam, Empty);
10335 Set_Etype (Nam, Empty);
10336
10337 Analyze (Nam);
10338 Set_Is_Immediately_Visible (Entity (Nam));
10339 end if;
10340
10341 else
10342 declare
10343 I : Interp_Index;
10344 It : Interp;
10345
10346 begin
10347 Get_First_Interp (Nam, I, It);
10348 while Present (It.Nam) loop
10349 if From_Parent_Scope (It.Nam) then
10350 Remove_Interp (I);
10351 end if;
10352
10353 Get_Next_Interp (I, It);
10354 end loop;
10355 end;
10356 end if;
10357 end if;
10358 end if;
10359
10360 -- The generic instantiation freezes the actual. This can only be done
10361 -- once the actual is resolved, in the analysis of the renaming
10362 -- declaration. To make the formal subprogram entity available, we set
10363 -- Corresponding_Formal_Spec to point to the formal subprogram entity.
10364 -- This is also needed in Analyze_Subprogram_Renaming for the processing
10365 -- of formal abstract subprograms.
10366
10367 Set_Corresponding_Formal_Spec (Decl_Node, Analyzed_S);
10368
10369 -- We cannot analyze the renaming declaration, and thus find the actual,
10370 -- until all the actuals are assembled in the instance. For subsequent
10371 -- checks of other actuals, indicate the node that will hold the
10372 -- instance of this formal.
10373
10374 Set_Instance_Of (Analyzed_S, Nam);
10375
10376 if Nkind (Actual) = N_Selected_Component
10377 and then Is_Task_Type (Etype (Prefix (Actual)))
10378 and then not Is_Frozen (Etype (Prefix (Actual)))
10379 then
10380 -- The renaming declaration will create a body, which must appear
10381 -- outside of the instantiation, We move the renaming declaration
10382 -- out of the instance, and create an additional renaming inside,
10383 -- to prevent freezing anomalies.
10384
10385 declare
10386 Anon_Id : constant Entity_Id := Make_Temporary (Loc, 'E');
10387
10388 begin
10389 Set_Defining_Unit_Name (New_Spec, Anon_Id);
10390 Insert_Before (Instantiation_Node, Decl_Node);
10391 Analyze (Decl_Node);
10392
10393 -- Now create renaming within the instance
10394
10395 Decl_Node :=
10396 Make_Subprogram_Renaming_Declaration (Loc,
10397 Specification => New_Copy_Tree (New_Spec),
10398 Name => New_Occurrence_Of (Anon_Id, Loc));
10399
10400 Set_Defining_Unit_Name (Specification (Decl_Node),
10401 Make_Defining_Identifier (Loc, Chars (Formal_Sub)));
10402 end;
10403 end if;
10404
10405 return Decl_Node;
10406 end Instantiate_Formal_Subprogram;
10407
10408 ------------------------
10409 -- Instantiate_Object --
10410 ------------------------
10411
10412 function Instantiate_Object
10413 (Formal : Node_Id;
10414 Actual : Node_Id;
10415 Analyzed_Formal : Node_Id) return List_Id
10416 is
10417 Gen_Obj : constant Entity_Id := Defining_Identifier (Formal);
10418 A_Gen_Obj : constant Entity_Id :=
10419 Defining_Identifier (Analyzed_Formal);
10420 Acc_Def : Node_Id := Empty;
10421 Act_Assoc : constant Node_Id := Parent (Actual);
10422 Actual_Decl : Node_Id := Empty;
10423 Decl_Node : Node_Id;
10424 Def : Node_Id;
10425 Ftyp : Entity_Id;
10426 List : constant List_Id := New_List;
10427 Loc : constant Source_Ptr := Sloc (Actual);
10428 Orig_Ftyp : constant Entity_Id := Etype (A_Gen_Obj);
10429 Subt_Decl : Node_Id := Empty;
10430 Subt_Mark : Node_Id := Empty;
10431
10432 function Copy_Access_Def return Node_Id;
10433 -- If formal is an anonymous access, copy access definition of formal
10434 -- for generated object declaration.
10435
10436 ---------------------
10437 -- Copy_Access_Def --
10438 ---------------------
10439
10440 function Copy_Access_Def return Node_Id is
10441 begin
10442 Def := New_Copy_Tree (Acc_Def);
10443
10444 -- In addition, if formal is an access to subprogram we need to
10445 -- generate new formals for the signature of the default, so that
10446 -- the tree is properly formatted for ASIS use.
10447
10448 if Present (Access_To_Subprogram_Definition (Acc_Def)) then
10449 declare
10450 Par_Spec : Node_Id;
10451 begin
10452 Par_Spec :=
10453 First (Parameter_Specifications
10454 (Access_To_Subprogram_Definition (Def)));
10455 while Present (Par_Spec) loop
10456 Set_Defining_Identifier (Par_Spec,
10457 Make_Defining_Identifier (Sloc (Acc_Def),
10458 Chars => Chars (Defining_Identifier (Par_Spec))));
10459 Next (Par_Spec);
10460 end loop;
10461 end;
10462 end if;
10463
10464 return Def;
10465 end Copy_Access_Def;
10466
10467 -- Start of processing for Instantiate_Object
10468
10469 begin
10470 -- Formal may be an anonymous access
10471
10472 if Present (Subtype_Mark (Formal)) then
10473 Subt_Mark := Subtype_Mark (Formal);
10474 else
10475 Check_Access_Definition (Formal);
10476 Acc_Def := Access_Definition (Formal);
10477 end if;
10478
10479 -- Sloc for error message on missing actual
10480
10481 Error_Msg_Sloc := Sloc (Scope (A_Gen_Obj));
10482
10483 if Get_Instance_Of (Gen_Obj) /= Gen_Obj then
10484 Error_Msg_N ("duplicate instantiation of generic parameter", Actual);
10485 end if;
10486
10487 Set_Parent (List, Parent (Actual));
10488
10489 -- OUT present
10490
10491 if Out_Present (Formal) then
10492
10493 -- An IN OUT generic actual must be a name. The instantiation is a
10494 -- renaming declaration. The actual is the name being renamed. We
10495 -- use the actual directly, rather than a copy, because it is not
10496 -- used further in the list of actuals, and because a copy or a use
10497 -- of relocate_node is incorrect if the instance is nested within a
10498 -- generic. In order to simplify ASIS searches, the Generic_Parent
10499 -- field links the declaration to the generic association.
10500
10501 if No (Actual) then
10502 Error_Msg_NE
10503 ("missing actual &",
10504 Instantiation_Node, Gen_Obj);
10505 Error_Msg_NE
10506 ("\in instantiation of & declared#",
10507 Instantiation_Node, Scope (A_Gen_Obj));
10508 Abandon_Instantiation (Instantiation_Node);
10509 end if;
10510
10511 if Present (Subt_Mark) then
10512 Decl_Node :=
10513 Make_Object_Renaming_Declaration (Loc,
10514 Defining_Identifier => New_Copy (Gen_Obj),
10515 Subtype_Mark => New_Copy_Tree (Subt_Mark),
10516 Name => Actual);
10517
10518 else pragma Assert (Present (Acc_Def));
10519 Decl_Node :=
10520 Make_Object_Renaming_Declaration (Loc,
10521 Defining_Identifier => New_Copy (Gen_Obj),
10522 Access_Definition => New_Copy_Tree (Acc_Def),
10523 Name => Actual);
10524 end if;
10525
10526 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
10527
10528 -- The analysis of the actual may produce Insert_Action nodes, so
10529 -- the declaration must have a context in which to attach them.
10530
10531 Append (Decl_Node, List);
10532 Analyze (Actual);
10533
10534 -- Return if the analysis of the actual reported some error
10535
10536 if Etype (Actual) = Any_Type then
10537 return List;
10538 end if;
10539
10540 -- This check is performed here because Analyze_Object_Renaming will
10541 -- not check it when Comes_From_Source is False. Note though that the
10542 -- check for the actual being the name of an object will be performed
10543 -- in Analyze_Object_Renaming.
10544
10545 if Is_Object_Reference (Actual)
10546 and then Is_Dependent_Component_Of_Mutable_Object (Actual)
10547 then
10548 Error_Msg_N
10549 ("illegal discriminant-dependent component for in out parameter",
10550 Actual);
10551 end if;
10552
10553 -- The actual has to be resolved in order to check that it is a
10554 -- variable (due to cases such as F (1), where F returns access to
10555 -- an array, and for overloaded prefixes).
10556
10557 Ftyp := Get_Instance_Of (Etype (A_Gen_Obj));
10558
10559 -- If the type of the formal is not itself a formal, and the current
10560 -- unit is a child unit, the formal type must be declared in a
10561 -- parent, and must be retrieved by visibility.
10562
10563 if Ftyp = Orig_Ftyp
10564 and then Is_Generic_Unit (Scope (Ftyp))
10565 and then Is_Child_Unit (Scope (A_Gen_Obj))
10566 then
10567 declare
10568 Temp : constant Node_Id :=
10569 New_Copy_Tree (Subtype_Mark (Analyzed_Formal));
10570 begin
10571 Set_Entity (Temp, Empty);
10572 Find_Type (Temp);
10573 Ftyp := Entity (Temp);
10574 end;
10575 end if;
10576
10577 if Is_Private_Type (Ftyp)
10578 and then not Is_Private_Type (Etype (Actual))
10579 and then (Base_Type (Full_View (Ftyp)) = Base_Type (Etype (Actual))
10580 or else Base_Type (Etype (Actual)) = Ftyp)
10581 then
10582 -- If the actual has the type of the full view of the formal, or
10583 -- else a non-private subtype of the formal, then the visibility
10584 -- of the formal type has changed. Add to the actuals a subtype
10585 -- declaration that will force the exchange of views in the body
10586 -- of the instance as well.
10587
10588 Subt_Decl :=
10589 Make_Subtype_Declaration (Loc,
10590 Defining_Identifier => Make_Temporary (Loc, 'P'),
10591 Subtype_Indication => New_Occurrence_Of (Ftyp, Loc));
10592
10593 Prepend (Subt_Decl, List);
10594
10595 Prepend_Elmt (Full_View (Ftyp), Exchanged_Views);
10596 Exchange_Declarations (Ftyp);
10597 end if;
10598
10599 Resolve (Actual, Ftyp);
10600
10601 if not Denotes_Variable (Actual) then
10602 Error_Msg_NE ("actual for& must be a variable", Actual, Gen_Obj);
10603
10604 elsif Base_Type (Ftyp) /= Base_Type (Etype (Actual)) then
10605
10606 -- Ada 2005 (AI-423): For a generic formal object of mode in out,
10607 -- the type of the actual shall resolve to a specific anonymous
10608 -- access type.
10609
10610 if Ada_Version < Ada_2005
10611 or else Ekind (Base_Type (Ftyp)) /=
10612 E_Anonymous_Access_Type
10613 or else Ekind (Base_Type (Etype (Actual))) /=
10614 E_Anonymous_Access_Type
10615 then
10616 Error_Msg_NE
10617 ("type of actual does not match type of&", Actual, Gen_Obj);
10618 end if;
10619 end if;
10620
10621 Note_Possible_Modification (Actual, Sure => True);
10622
10623 -- Check for instantiation of atomic/volatile actual for
10624 -- non-atomic/volatile formal (RM C.6 (12)).
10625
10626 if Is_Atomic_Object (Actual) and then not Is_Atomic (Orig_Ftyp) then
10627 Error_Msg_N
10628 ("cannot instantiate non-atomic formal object "
10629 & "with atomic actual", Actual);
10630
10631 elsif Is_Volatile_Object (Actual) and then not Is_Volatile (Orig_Ftyp)
10632 then
10633 Error_Msg_N
10634 ("cannot instantiate non-volatile formal object "
10635 & "with volatile actual", Actual);
10636 end if;
10637
10638 -- Formal in-parameter
10639
10640 else
10641 -- The instantiation of a generic formal in-parameter is constant
10642 -- declaration. The actual is the expression for that declaration.
10643 -- Its type is a full copy of the type of the formal. This may be
10644 -- an access to subprogram, for which we need to generate entities
10645 -- for the formals in the new signature.
10646
10647 if Present (Actual) then
10648 if Present (Subt_Mark) then
10649 Def := New_Copy_Tree (Subt_Mark);
10650 else pragma Assert (Present (Acc_Def));
10651 Def := Copy_Access_Def;
10652 end if;
10653
10654 Decl_Node :=
10655 Make_Object_Declaration (Loc,
10656 Defining_Identifier => New_Copy (Gen_Obj),
10657 Constant_Present => True,
10658 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
10659 Object_Definition => Def,
10660 Expression => Actual);
10661
10662 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
10663
10664 -- A generic formal object of a tagged type is defined to be
10665 -- aliased so the new constant must also be treated as aliased.
10666
10667 if Is_Tagged_Type (Etype (A_Gen_Obj)) then
10668 Set_Aliased_Present (Decl_Node);
10669 end if;
10670
10671 Append (Decl_Node, List);
10672
10673 -- No need to repeat (pre-)analysis of some expression nodes
10674 -- already handled in Preanalyze_Actuals.
10675
10676 if Nkind (Actual) /= N_Allocator then
10677 Analyze (Actual);
10678
10679 -- Return if the analysis of the actual reported some error
10680
10681 if Etype (Actual) = Any_Type then
10682 return List;
10683 end if;
10684 end if;
10685
10686 declare
10687 Formal_Type : constant Entity_Id := Etype (A_Gen_Obj);
10688 Typ : Entity_Id;
10689
10690 begin
10691 Typ := Get_Instance_Of (Formal_Type);
10692
10693 -- If the actual appears in the current or an enclosing scope,
10694 -- use its type directly. This is relevant if it has an actual
10695 -- subtype that is distinct from its nominal one. This cannot
10696 -- be done in general because the type of the actual may
10697 -- depend on other actuals, and only be fully determined when
10698 -- the enclosing instance is analyzed.
10699
10700 if Present (Etype (Actual))
10701 and then Is_Constr_Subt_For_U_Nominal (Etype (Actual))
10702 then
10703 Freeze_Before (Instantiation_Node, Etype (Actual));
10704 else
10705 Freeze_Before (Instantiation_Node, Typ);
10706 end if;
10707
10708 -- If the actual is an aggregate, perform name resolution on
10709 -- its components (the analysis of an aggregate does not do it)
10710 -- to capture local names that may be hidden if the generic is
10711 -- a child unit.
10712
10713 if Nkind (Actual) = N_Aggregate then
10714 Preanalyze_And_Resolve (Actual, Typ);
10715 end if;
10716
10717 if Is_Limited_Type (Typ)
10718 and then not OK_For_Limited_Init (Typ, Actual)
10719 then
10720 Error_Msg_N
10721 ("initialization not allowed for limited types", Actual);
10722 Explain_Limited_Type (Typ, Actual);
10723 end if;
10724 end;
10725
10726 elsif Present (Default_Expression (Formal)) then
10727
10728 -- Use default to construct declaration
10729
10730 if Present (Subt_Mark) then
10731 Def := New_Copy (Subt_Mark);
10732 else pragma Assert (Present (Acc_Def));
10733 Def := Copy_Access_Def;
10734 end if;
10735
10736 Decl_Node :=
10737 Make_Object_Declaration (Sloc (Formal),
10738 Defining_Identifier => New_Copy (Gen_Obj),
10739 Constant_Present => True,
10740 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
10741 Object_Definition => Def,
10742 Expression => New_Copy_Tree
10743 (Default_Expression (Formal)));
10744
10745 Append (Decl_Node, List);
10746 Set_Analyzed (Expression (Decl_Node), False);
10747
10748 else
10749 Error_Msg_NE ("missing actual&", Instantiation_Node, Gen_Obj);
10750 Error_Msg_NE ("\in instantiation of & declared#",
10751 Instantiation_Node, Scope (A_Gen_Obj));
10752
10753 if Is_Scalar_Type (Etype (A_Gen_Obj)) then
10754
10755 -- Create dummy constant declaration so that instance can be
10756 -- analyzed, to minimize cascaded visibility errors.
10757
10758 if Present (Subt_Mark) then
10759 Def := Subt_Mark;
10760 else pragma Assert (Present (Acc_Def));
10761 Def := Acc_Def;
10762 end if;
10763
10764 Decl_Node :=
10765 Make_Object_Declaration (Loc,
10766 Defining_Identifier => New_Copy (Gen_Obj),
10767 Constant_Present => True,
10768 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
10769 Object_Definition => New_Copy (Def),
10770 Expression =>
10771 Make_Attribute_Reference (Sloc (Gen_Obj),
10772 Attribute_Name => Name_First,
10773 Prefix => New_Copy (Def)));
10774
10775 Append (Decl_Node, List);
10776
10777 else
10778 Abandon_Instantiation (Instantiation_Node);
10779 end if;
10780 end if;
10781 end if;
10782
10783 if Nkind (Actual) in N_Has_Entity then
10784 Actual_Decl := Parent (Entity (Actual));
10785 end if;
10786
10787 -- Ada 2005 (AI-423): For a formal object declaration with a null
10788 -- exclusion or an access definition that has a null exclusion: If the
10789 -- actual matching the formal object declaration denotes a generic
10790 -- formal object of another generic unit G, and the instantiation
10791 -- containing the actual occurs within the body of G or within the body
10792 -- of a generic unit declared within the declarative region of G, then
10793 -- the declaration of the formal object of G must have a null exclusion.
10794 -- Otherwise, the subtype of the actual matching the formal object
10795 -- declaration shall exclude null.
10796
10797 if Ada_Version >= Ada_2005
10798 and then Present (Actual_Decl)
10799 and then Nkind_In (Actual_Decl, N_Formal_Object_Declaration,
10800 N_Object_Declaration)
10801 and then Nkind (Analyzed_Formal) = N_Formal_Object_Declaration
10802 and then not Has_Null_Exclusion (Actual_Decl)
10803 and then Has_Null_Exclusion (Analyzed_Formal)
10804 then
10805 Error_Msg_Sloc := Sloc (Analyzed_Formal);
10806 Error_Msg_N
10807 ("actual must exclude null to match generic formal#", Actual);
10808 end if;
10809
10810 -- An effectively volatile object cannot be used as an actual in a
10811 -- generic instantiation (SPARK RM 7.1.3(7)). The following check is
10812 -- relevant only when SPARK_Mode is on as it is not a standard Ada
10813 -- legality rule, and also verifies that the actual is an object.
10814
10815 if SPARK_Mode = On
10816 and then Present (Actual)
10817 and then Is_Object_Reference (Actual)
10818 and then Is_Effectively_Volatile_Object (Actual)
10819 then
10820 Error_Msg_N
10821 ("volatile object cannot act as actual in generic instantiation",
10822 Actual);
10823 end if;
10824
10825 return List;
10826 end Instantiate_Object;
10827
10828 ------------------------------
10829 -- Instantiate_Package_Body --
10830 ------------------------------
10831
10832 -- WARNING: This routine manages Ghost regions. Return statements must be
10833 -- replaced by gotos which jump to the end of the routine and restore the
10834 -- Ghost mode.
10835
10836 procedure Instantiate_Package_Body
10837 (Body_Info : Pending_Body_Info;
10838 Inlined_Body : Boolean := False;
10839 Body_Optional : Boolean := False)
10840 is
10841 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
10842 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Decl);
10843 Act_Spec : constant Node_Id := Specification (Act_Decl);
10844 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
10845 Gen_Id : constant Node_Id := Name (Inst_Node);
10846 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
10847 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
10848 Loc : constant Source_Ptr := Sloc (Inst_Node);
10849
10850 Save_IPSM : constant Boolean := Ignore_Pragma_SPARK_Mode;
10851 Save_Style_Check : constant Boolean := Style_Check;
10852
10853 procedure Check_Initialized_Types;
10854 -- In a generic package body, an entity of a generic private type may
10855 -- appear uninitialized. This is suspicious, unless the actual is a
10856 -- fully initialized type.
10857
10858 -----------------------------
10859 -- Check_Initialized_Types --
10860 -----------------------------
10861
10862 procedure Check_Initialized_Types is
10863 Decl : Node_Id;
10864 Formal : Entity_Id;
10865 Actual : Entity_Id;
10866 Uninit_Var : Entity_Id;
10867
10868 begin
10869 Decl := First (Generic_Formal_Declarations (Gen_Decl));
10870 while Present (Decl) loop
10871 Uninit_Var := Empty;
10872
10873 if Nkind (Decl) = N_Private_Extension_Declaration then
10874 Uninit_Var := Uninitialized_Variable (Decl);
10875
10876 elsif Nkind (Decl) = N_Formal_Type_Declaration
10877 and then Nkind (Formal_Type_Definition (Decl)) =
10878 N_Formal_Private_Type_Definition
10879 then
10880 Uninit_Var :=
10881 Uninitialized_Variable (Formal_Type_Definition (Decl));
10882 end if;
10883
10884 if Present (Uninit_Var) then
10885 Formal := Defining_Identifier (Decl);
10886 Actual := First_Entity (Act_Decl_Id);
10887
10888 -- For each formal there is a subtype declaration that renames
10889 -- the actual and has the same name as the formal. Locate the
10890 -- formal for warning message about uninitialized variables
10891 -- in the generic, for which the actual type should be a fully
10892 -- initialized type.
10893
10894 while Present (Actual) loop
10895 exit when Ekind (Actual) = E_Package
10896 and then Present (Renamed_Object (Actual));
10897
10898 if Chars (Actual) = Chars (Formal)
10899 and then not Is_Scalar_Type (Actual)
10900 and then not Is_Fully_Initialized_Type (Actual)
10901 and then Warn_On_No_Value_Assigned
10902 then
10903 Error_Msg_Node_2 := Formal;
10904 Error_Msg_NE
10905 ("generic unit has uninitialized variable& of "
10906 & "formal private type &?v?", Actual, Uninit_Var);
10907 Error_Msg_NE
10908 ("actual type for& should be fully initialized type?v?",
10909 Actual, Formal);
10910 exit;
10911 end if;
10912
10913 Next_Entity (Actual);
10914 end loop;
10915 end if;
10916
10917 Next (Decl);
10918 end loop;
10919 end Check_Initialized_Types;
10920
10921 -- Local variables
10922
10923 Act_Body : Node_Id;
10924 Act_Body_Id : Entity_Id;
10925 Act_Body_Name : Node_Id;
10926 Gen_Body : Node_Id;
10927 Gen_Body_Id : Node_Id;
10928 Mode : Ghost_Mode_Type;
10929 Par_Ent : Entity_Id := Empty;
10930 Par_Vis : Boolean := False;
10931
10932 Parent_Installed : Boolean := False;
10933
10934 Vis_Prims_List : Elist_Id := No_Elist;
10935 -- List of primitives made temporarily visible in the instantiation
10936 -- to match the visibility of the formal type.
10937
10938 -- Start of processing for Instantiate_Package_Body
10939
10940 begin
10941 Gen_Body_Id := Corresponding_Body (Gen_Decl);
10942
10943 -- The instance body may already have been processed, as the parent of
10944 -- another instance that is inlined (Load_Parent_Of_Generic).
10945
10946 if Present (Corresponding_Body (Instance_Spec (Inst_Node))) then
10947 return;
10948 end if;
10949
10950 -- The package being instantiated may be subject to pragma Ghost. Set
10951 -- the mode now to ensure that any nodes generated during instantiation
10952 -- are properly marked as Ghost.
10953
10954 Set_Ghost_Mode (Act_Decl_Id, Mode);
10955
10956 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
10957
10958 -- Re-establish the state of information on which checks are suppressed.
10959 -- This information was set in Body_Info at the point of instantiation,
10960 -- and now we restore it so that the instance is compiled using the
10961 -- check status at the instantiation (RM 11.5(7.2/2), AI95-00224-01).
10962
10963 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
10964 Scope_Suppress := Body_Info.Scope_Suppress;
10965 Opt.Ada_Version := Body_Info.Version;
10966 Opt.Ada_Version_Pragma := Body_Info.Version_Pragma;
10967 Restore_Warnings (Body_Info.Warnings);
10968 Opt.SPARK_Mode := Body_Info.SPARK_Mode;
10969 Opt.SPARK_Mode_Pragma := Body_Info.SPARK_Mode_Pragma;
10970
10971 if No (Gen_Body_Id) then
10972
10973 -- Do not look for parent of generic body if none is required.
10974 -- This may happen when the routine is called as part of the
10975 -- Pending_Instantiations processing, when nested instances
10976 -- may precede the one generated from the main unit.
10977
10978 if not Unit_Requires_Body (Defining_Entity (Gen_Decl))
10979 and then Body_Optional
10980 then
10981 goto Leave;
10982 else
10983 Load_Parent_Of_Generic
10984 (Inst_Node, Specification (Gen_Decl), Body_Optional);
10985 Gen_Body_Id := Corresponding_Body (Gen_Decl);
10986 end if;
10987 end if;
10988
10989 -- Establish global variable for sloc adjustment and for error recovery
10990 -- In the case of an instance body for an instantiation with actuals
10991 -- from a limited view, the instance body is placed at the beginning
10992 -- of the enclosing package body: use the body entity as the source
10993 -- location for nodes of the instance body.
10994
10995 if not Is_Empty_Elmt_List (Incomplete_Actuals (Act_Decl_Id)) then
10996 declare
10997 Scop : constant Entity_Id := Scope (Act_Decl_Id);
10998 Body_Id : constant Node_Id :=
10999 Corresponding_Body (Unit_Declaration_Node (Scop));
11000
11001 begin
11002 Instantiation_Node := Body_Id;
11003 end;
11004 else
11005 Instantiation_Node := Inst_Node;
11006 end if;
11007
11008 if Present (Gen_Body_Id) then
11009 Save_Env (Gen_Unit, Act_Decl_Id);
11010 Style_Check := False;
11011
11012 -- If the context of the instance is subject to SPARK_Mode "off" or
11013 -- the annotation is altogether missing, set the global flag which
11014 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
11015 -- the instance.
11016
11017 if SPARK_Mode /= On then
11018 Ignore_Pragma_SPARK_Mode := True;
11019 end if;
11020
11021 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
11022 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
11023
11024 Create_Instantiation_Source
11025 (Inst_Node, Gen_Body_Id, S_Adjustment);
11026
11027 Act_Body :=
11028 Copy_Generic_Node
11029 (Original_Node (Gen_Body), Empty, Instantiating => True);
11030
11031 -- Create proper (possibly qualified) defining name for the body, to
11032 -- correspond to the one in the spec.
11033
11034 Act_Body_Id :=
11035 Make_Defining_Identifier (Sloc (Act_Decl_Id), Chars (Act_Decl_Id));
11036 Set_Comes_From_Source (Act_Body_Id, Comes_From_Source (Act_Decl_Id));
11037
11038 -- Some attributes of spec entity are not inherited by body entity
11039
11040 Set_Handler_Records (Act_Body_Id, No_List);
11041
11042 if Nkind (Defining_Unit_Name (Act_Spec)) =
11043 N_Defining_Program_Unit_Name
11044 then
11045 Act_Body_Name :=
11046 Make_Defining_Program_Unit_Name (Loc,
11047 Name =>
11048 New_Copy_Tree (Name (Defining_Unit_Name (Act_Spec))),
11049 Defining_Identifier => Act_Body_Id);
11050 else
11051 Act_Body_Name := Act_Body_Id;
11052 end if;
11053
11054 Set_Defining_Unit_Name (Act_Body, Act_Body_Name);
11055
11056 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
11057 Check_Generic_Actuals (Act_Decl_Id, False);
11058 Check_Initialized_Types;
11059
11060 -- Install primitives hidden at the point of the instantiation but
11061 -- visible when processing the generic formals
11062
11063 declare
11064 E : Entity_Id;
11065
11066 begin
11067 E := First_Entity (Act_Decl_Id);
11068 while Present (E) loop
11069 if Is_Type (E)
11070 and then not Is_Itype (E)
11071 and then Is_Generic_Actual_Type (E)
11072 and then Is_Tagged_Type (E)
11073 then
11074 Install_Hidden_Primitives
11075 (Prims_List => Vis_Prims_List,
11076 Gen_T => Generic_Parent_Type (Parent (E)),
11077 Act_T => E);
11078 end if;
11079
11080 Next_Entity (E);
11081 end loop;
11082 end;
11083
11084 -- If it is a child unit, make the parent instance (which is an
11085 -- instance of the parent of the generic) visible. The parent
11086 -- instance is the prefix of the name of the generic unit.
11087
11088 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
11089 and then Nkind (Gen_Id) = N_Expanded_Name
11090 then
11091 Par_Ent := Entity (Prefix (Gen_Id));
11092 Par_Vis := Is_Immediately_Visible (Par_Ent);
11093 Install_Parent (Par_Ent, In_Body => True);
11094 Parent_Installed := True;
11095
11096 elsif Is_Child_Unit (Gen_Unit) then
11097 Par_Ent := Scope (Gen_Unit);
11098 Par_Vis := Is_Immediately_Visible (Par_Ent);
11099 Install_Parent (Par_Ent, In_Body => True);
11100 Parent_Installed := True;
11101 end if;
11102
11103 -- If the instantiation is a library unit, and this is the main unit,
11104 -- then build the resulting compilation unit nodes for the instance.
11105 -- If this is a compilation unit but it is not the main unit, then it
11106 -- is the body of a unit in the context, that is being compiled
11107 -- because it is encloses some inlined unit or another generic unit
11108 -- being instantiated. In that case, this body is not part of the
11109 -- current compilation, and is not attached to the tree, but its
11110 -- parent must be set for analysis.
11111
11112 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
11113
11114 -- Replace instance node with body of instance, and create new
11115 -- node for corresponding instance declaration.
11116
11117 Build_Instance_Compilation_Unit_Nodes
11118 (Inst_Node, Act_Body, Act_Decl);
11119 Analyze (Inst_Node);
11120
11121 if Parent (Inst_Node) = Cunit (Main_Unit) then
11122
11123 -- If the instance is a child unit itself, then set the scope
11124 -- of the expanded body to be the parent of the instantiation
11125 -- (ensuring that the fully qualified name will be generated
11126 -- for the elaboration subprogram).
11127
11128 if Nkind (Defining_Unit_Name (Act_Spec)) =
11129 N_Defining_Program_Unit_Name
11130 then
11131 Set_Scope (Defining_Entity (Inst_Node), Scope (Act_Decl_Id));
11132 end if;
11133 end if;
11134
11135 -- Case where instantiation is not a library unit
11136
11137 else
11138 -- If this is an early instantiation, i.e. appears textually
11139 -- before the corresponding body and must be elaborated first,
11140 -- indicate that the body instance is to be delayed.
11141
11142 Install_Body (Act_Body, Inst_Node, Gen_Body, Gen_Decl);
11143
11144 -- Now analyze the body. We turn off all checks if this is an
11145 -- internal unit, since there is no reason to have checks on for
11146 -- any predefined run-time library code. All such code is designed
11147 -- to be compiled with checks off.
11148
11149 -- Note that we do NOT apply this criterion to children of GNAT
11150 -- The latter units must suppress checks explicitly if needed.
11151
11152 -- We also do not suppress checks in CodePeer mode where we are
11153 -- interested in finding possible runtime errors.
11154
11155 if not CodePeer_Mode
11156 and then Is_Predefined_File_Name
11157 (Unit_File_Name (Get_Source_Unit (Gen_Decl)))
11158 then
11159 Analyze (Act_Body, Suppress => All_Checks);
11160 else
11161 Analyze (Act_Body);
11162 end if;
11163 end if;
11164
11165 Inherit_Context (Gen_Body, Inst_Node);
11166
11167 -- Remove the parent instances if they have been placed on the scope
11168 -- stack to compile the body.
11169
11170 if Parent_Installed then
11171 Remove_Parent (In_Body => True);
11172
11173 -- Restore the previous visibility of the parent
11174
11175 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
11176 end if;
11177
11178 Restore_Hidden_Primitives (Vis_Prims_List);
11179 Restore_Private_Views (Act_Decl_Id);
11180
11181 -- Remove the current unit from visibility if this is an instance
11182 -- that is not elaborated on the fly for inlining purposes.
11183
11184 if not Inlined_Body then
11185 Set_Is_Immediately_Visible (Act_Decl_Id, False);
11186 end if;
11187
11188 Restore_Env;
11189 Ignore_Pragma_SPARK_Mode := Save_IPSM;
11190 Style_Check := Save_Style_Check;
11191
11192 -- If we have no body, and the unit requires a body, then complain. This
11193 -- complaint is suppressed if we have detected other errors (since a
11194 -- common reason for missing the body is that it had errors).
11195 -- In CodePeer mode, a warning has been emitted already, no need for
11196 -- further messages.
11197
11198 elsif Unit_Requires_Body (Gen_Unit)
11199 and then not Body_Optional
11200 then
11201 if CodePeer_Mode then
11202 null;
11203
11204 elsif Serious_Errors_Detected = 0 then
11205 Error_Msg_NE
11206 ("cannot find body of generic package &", Inst_Node, Gen_Unit);
11207
11208 -- Don't attempt to perform any cleanup actions if some other error
11209 -- was already detected, since this can cause blowups.
11210
11211 else
11212 return;
11213 end if;
11214
11215 -- Case of package that does not need a body
11216
11217 else
11218 -- If the instantiation of the declaration is a library unit, rewrite
11219 -- the original package instantiation as a package declaration in the
11220 -- compilation unit node.
11221
11222 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
11223 Set_Parent_Spec (Act_Decl, Parent_Spec (Inst_Node));
11224 Rewrite (Inst_Node, Act_Decl);
11225
11226 -- Generate elaboration entity, in case spec has elaboration code.
11227 -- This cannot be done when the instance is analyzed, because it
11228 -- is not known yet whether the body exists.
11229
11230 Set_Elaboration_Entity_Required (Act_Decl_Id, False);
11231 Build_Elaboration_Entity (Parent (Inst_Node), Act_Decl_Id);
11232
11233 -- If the instantiation is not a library unit, then append the
11234 -- declaration to the list of implicitly generated entities, unless
11235 -- it is already a list member which means that it was already
11236 -- processed
11237
11238 elsif not Is_List_Member (Act_Decl) then
11239 Mark_Rewrite_Insertion (Act_Decl);
11240 Insert_Before (Inst_Node, Act_Decl);
11241 end if;
11242 end if;
11243
11244 Expander_Mode_Restore;
11245
11246 <<Leave>>
11247 Restore_Ghost_Mode (Mode);
11248 end Instantiate_Package_Body;
11249
11250 ---------------------------------
11251 -- Instantiate_Subprogram_Body --
11252 ---------------------------------
11253
11254 -- WARNING: This routine manages Ghost regions. Return statements must be
11255 -- replaced by gotos which jump to the end of the routine and restore the
11256 -- Ghost mode.
11257
11258 procedure Instantiate_Subprogram_Body
11259 (Body_Info : Pending_Body_Info;
11260 Body_Optional : Boolean := False)
11261 is
11262 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
11263 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Decl);
11264 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
11265 Gen_Id : constant Node_Id := Name (Inst_Node);
11266 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
11267 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
11268 Loc : constant Source_Ptr := Sloc (Inst_Node);
11269 Pack_Id : constant Entity_Id :=
11270 Defining_Unit_Name (Parent (Act_Decl));
11271
11272 Saved_IPSM : constant Boolean := Ignore_Pragma_SPARK_Mode;
11273 Saved_Style_Check : constant Boolean := Style_Check;
11274 Saved_Warnings : constant Warning_Record := Save_Warnings;
11275
11276 Act_Body : Node_Id;
11277 Act_Body_Id : Entity_Id;
11278 Gen_Body : Node_Id;
11279 Gen_Body_Id : Node_Id;
11280 Mode : Ghost_Mode_Type;
11281 Pack_Body : Node_Id;
11282 Par_Ent : Entity_Id := Empty;
11283 Par_Vis : Boolean := False;
11284 Ret_Expr : Node_Id;
11285
11286 Parent_Installed : Boolean := False;
11287
11288 begin
11289 Gen_Body_Id := Corresponding_Body (Gen_Decl);
11290
11291 -- Subprogram body may have been created already because of an inline
11292 -- pragma, or because of multiple elaborations of the enclosing package
11293 -- when several instances of the subprogram appear in the main unit.
11294
11295 if Present (Corresponding_Body (Act_Decl)) then
11296 return;
11297 end if;
11298
11299 -- The subprogram being instantiated may be subject to pragma Ghost. Set
11300 -- the mode now to ensure that any nodes generated during instantiation
11301 -- are properly marked as Ghost.
11302
11303 Set_Ghost_Mode (Act_Decl_Id, Mode);
11304
11305 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
11306
11307 -- Re-establish the state of information on which checks are suppressed.
11308 -- This information was set in Body_Info at the point of instantiation,
11309 -- and now we restore it so that the instance is compiled using the
11310 -- check status at the instantiation (RM 11.5(7.2/2), AI95-00224-01).
11311
11312 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
11313 Scope_Suppress := Body_Info.Scope_Suppress;
11314 Opt.Ada_Version := Body_Info.Version;
11315 Opt.Ada_Version_Pragma := Body_Info.Version_Pragma;
11316 Restore_Warnings (Body_Info.Warnings);
11317 Opt.SPARK_Mode := Body_Info.SPARK_Mode;
11318 Opt.SPARK_Mode_Pragma := Body_Info.SPARK_Mode_Pragma;
11319
11320 if No (Gen_Body_Id) then
11321
11322 -- For imported generic subprogram, no body to compile, complete
11323 -- the spec entity appropriately.
11324
11325 if Is_Imported (Gen_Unit) then
11326 Set_Is_Imported (Act_Decl_Id);
11327 Set_First_Rep_Item (Act_Decl_Id, First_Rep_Item (Gen_Unit));
11328 Set_Interface_Name (Act_Decl_Id, Interface_Name (Gen_Unit));
11329 Set_Convention (Act_Decl_Id, Convention (Gen_Unit));
11330 Set_Has_Completion (Act_Decl_Id);
11331 goto Leave;
11332
11333 -- For other cases, compile the body
11334
11335 else
11336 Load_Parent_Of_Generic
11337 (Inst_Node, Specification (Gen_Decl), Body_Optional);
11338 Gen_Body_Id := Corresponding_Body (Gen_Decl);
11339 end if;
11340 end if;
11341
11342 Instantiation_Node := Inst_Node;
11343
11344 if Present (Gen_Body_Id) then
11345 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
11346
11347 if Nkind (Gen_Body) = N_Subprogram_Body_Stub then
11348
11349 -- Either body is not present, or context is non-expanding, as
11350 -- when compiling a subunit. Mark the instance as completed, and
11351 -- diagnose a missing body when needed.
11352
11353 if Expander_Active
11354 and then Operating_Mode = Generate_Code
11355 then
11356 Error_Msg_N ("missing proper body for instantiation", Gen_Body);
11357 end if;
11358
11359 Set_Has_Completion (Act_Decl_Id);
11360 goto Leave;
11361 end if;
11362
11363 Save_Env (Gen_Unit, Act_Decl_Id);
11364 Style_Check := False;
11365
11366 -- If the context of the instance is subject to SPARK_Mode "off" or
11367 -- the annotation is altogether missing, set the global flag which
11368 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
11369 -- the instance.
11370
11371 if SPARK_Mode /= On then
11372 Ignore_Pragma_SPARK_Mode := True;
11373 end if;
11374
11375 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
11376 Create_Instantiation_Source
11377 (Inst_Node,
11378 Gen_Body_Id,
11379 S_Adjustment);
11380
11381 Act_Body :=
11382 Copy_Generic_Node
11383 (Original_Node (Gen_Body), Empty, Instantiating => True);
11384
11385 -- Create proper defining name for the body, to correspond to the one
11386 -- in the spec.
11387
11388 Act_Body_Id :=
11389 Make_Defining_Identifier (Sloc (Act_Decl_Id), Chars (Act_Decl_Id));
11390
11391 Set_Comes_From_Source (Act_Body_Id, Comes_From_Source (Act_Decl_Id));
11392 Set_Defining_Unit_Name (Specification (Act_Body), Act_Body_Id);
11393
11394 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
11395 Set_Has_Completion (Act_Decl_Id);
11396 Check_Generic_Actuals (Pack_Id, False);
11397
11398 -- Generate a reference to link the visible subprogram instance to
11399 -- the generic body, which for navigation purposes is the only
11400 -- available source for the instance.
11401
11402 Generate_Reference
11403 (Related_Instance (Pack_Id),
11404 Gen_Body_Id, 'b', Set_Ref => False, Force => True);
11405
11406 -- If it is a child unit, make the parent instance (which is an
11407 -- instance of the parent of the generic) visible. The parent
11408 -- instance is the prefix of the name of the generic unit.
11409
11410 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
11411 and then Nkind (Gen_Id) = N_Expanded_Name
11412 then
11413 Par_Ent := Entity (Prefix (Gen_Id));
11414 Par_Vis := Is_Immediately_Visible (Par_Ent);
11415 Install_Parent (Par_Ent, In_Body => True);
11416 Parent_Installed := True;
11417
11418 elsif Is_Child_Unit (Gen_Unit) then
11419 Par_Ent := Scope (Gen_Unit);
11420 Par_Vis := Is_Immediately_Visible (Par_Ent);
11421 Install_Parent (Par_Ent, In_Body => True);
11422 Parent_Installed := True;
11423 end if;
11424
11425 -- Subprogram body is placed in the body of wrapper package,
11426 -- whose spec contains the subprogram declaration as well as
11427 -- the renaming declarations for the generic parameters.
11428
11429 Pack_Body :=
11430 Make_Package_Body (Loc,
11431 Defining_Unit_Name => New_Copy (Pack_Id),
11432 Declarations => New_List (Act_Body));
11433
11434 Set_Corresponding_Spec (Pack_Body, Pack_Id);
11435
11436 -- If the instantiation is a library unit, then build resulting
11437 -- compilation unit nodes for the instance. The declaration of
11438 -- the enclosing package is the grandparent of the subprogram
11439 -- declaration. First replace the instantiation node as the unit
11440 -- of the corresponding compilation.
11441
11442 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
11443 if Parent (Inst_Node) = Cunit (Main_Unit) then
11444 Set_Unit (Parent (Inst_Node), Inst_Node);
11445 Build_Instance_Compilation_Unit_Nodes
11446 (Inst_Node, Pack_Body, Parent (Parent (Act_Decl)));
11447 Analyze (Inst_Node);
11448 else
11449 Set_Parent (Pack_Body, Parent (Inst_Node));
11450 Analyze (Pack_Body);
11451 end if;
11452
11453 else
11454 Insert_Before (Inst_Node, Pack_Body);
11455 Mark_Rewrite_Insertion (Pack_Body);
11456 Analyze (Pack_Body);
11457
11458 if Expander_Active then
11459 Freeze_Subprogram_Body (Inst_Node, Gen_Body, Pack_Id);
11460 end if;
11461 end if;
11462
11463 Inherit_Context (Gen_Body, Inst_Node);
11464
11465 Restore_Private_Views (Pack_Id, False);
11466
11467 if Parent_Installed then
11468 Remove_Parent (In_Body => True);
11469
11470 -- Restore the previous visibility of the parent
11471
11472 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
11473 end if;
11474
11475 Restore_Env;
11476 Ignore_Pragma_SPARK_Mode := Saved_IPSM;
11477 Style_Check := Saved_Style_Check;
11478 Restore_Warnings (Saved_Warnings);
11479
11480 -- Body not found. Error was emitted already. If there were no previous
11481 -- errors, this may be an instance whose scope is a premature instance.
11482 -- In that case we must insure that the (legal) program does raise
11483 -- program error if executed. We generate a subprogram body for this
11484 -- purpose. See DEC ac30vso.
11485
11486 -- Should not reference proprietary DEC tests in comments ???
11487
11488 elsif Serious_Errors_Detected = 0
11489 and then Nkind (Parent (Inst_Node)) /= N_Compilation_Unit
11490 then
11491 if Body_Optional then
11492 goto Leave;
11493
11494 elsif Ekind (Act_Decl_Id) = E_Procedure then
11495 Act_Body :=
11496 Make_Subprogram_Body (Loc,
11497 Specification =>
11498 Make_Procedure_Specification (Loc,
11499 Defining_Unit_Name =>
11500 Make_Defining_Identifier (Loc, Chars (Act_Decl_Id)),
11501 Parameter_Specifications =>
11502 New_Copy_List
11503 (Parameter_Specifications (Parent (Act_Decl_Id)))),
11504
11505 Declarations => Empty_List,
11506 Handled_Statement_Sequence =>
11507 Make_Handled_Sequence_Of_Statements (Loc,
11508 Statements => New_List (
11509 Make_Raise_Program_Error (Loc,
11510 Reason => PE_Access_Before_Elaboration))));
11511
11512 else
11513 Ret_Expr :=
11514 Make_Raise_Program_Error (Loc,
11515 Reason => PE_Access_Before_Elaboration);
11516
11517 Set_Etype (Ret_Expr, (Etype (Act_Decl_Id)));
11518 Set_Analyzed (Ret_Expr);
11519
11520 Act_Body :=
11521 Make_Subprogram_Body (Loc,
11522 Specification =>
11523 Make_Function_Specification (Loc,
11524 Defining_Unit_Name =>
11525 Make_Defining_Identifier (Loc, Chars (Act_Decl_Id)),
11526 Parameter_Specifications =>
11527 New_Copy_List
11528 (Parameter_Specifications (Parent (Act_Decl_Id))),
11529 Result_Definition =>
11530 New_Occurrence_Of (Etype (Act_Decl_Id), Loc)),
11531
11532 Declarations => Empty_List,
11533 Handled_Statement_Sequence =>
11534 Make_Handled_Sequence_Of_Statements (Loc,
11535 Statements => New_List (
11536 Make_Simple_Return_Statement (Loc, Ret_Expr))));
11537 end if;
11538
11539 Pack_Body :=
11540 Make_Package_Body (Loc,
11541 Defining_Unit_Name => New_Copy (Pack_Id),
11542 Declarations => New_List (Act_Body));
11543
11544 Insert_After (Inst_Node, Pack_Body);
11545 Set_Corresponding_Spec (Pack_Body, Pack_Id);
11546 Analyze (Pack_Body);
11547 end if;
11548
11549 Expander_Mode_Restore;
11550
11551 <<Leave>>
11552 Restore_Ghost_Mode (Mode);
11553 end Instantiate_Subprogram_Body;
11554
11555 ----------------------
11556 -- Instantiate_Type --
11557 ----------------------
11558
11559 function Instantiate_Type
11560 (Formal : Node_Id;
11561 Actual : Node_Id;
11562 Analyzed_Formal : Node_Id;
11563 Actual_Decls : List_Id) return List_Id
11564 is
11565 Gen_T : constant Entity_Id := Defining_Identifier (Formal);
11566 A_Gen_T : constant Entity_Id :=
11567 Defining_Identifier (Analyzed_Formal);
11568 Ancestor : Entity_Id := Empty;
11569 Def : constant Node_Id := Formal_Type_Definition (Formal);
11570 Act_T : Entity_Id;
11571 Decl_Node : Node_Id;
11572 Decl_Nodes : List_Id;
11573 Loc : Source_Ptr;
11574 Subt : Entity_Id;
11575
11576 procedure Diagnose_Predicated_Actual;
11577 -- There are a number of constructs in which a discrete type with
11578 -- predicates is illegal, e.g. as an index in an array type declaration.
11579 -- If a generic type is used is such a construct in a generic package
11580 -- declaration, it carries the flag No_Predicate_On_Actual. it is part
11581 -- of the generic contract that the actual cannot have predicates.
11582
11583 procedure Validate_Array_Type_Instance;
11584 procedure Validate_Access_Subprogram_Instance;
11585 procedure Validate_Access_Type_Instance;
11586 procedure Validate_Derived_Type_Instance;
11587 procedure Validate_Derived_Interface_Type_Instance;
11588 procedure Validate_Discriminated_Formal_Type;
11589 procedure Validate_Interface_Type_Instance;
11590 procedure Validate_Private_Type_Instance;
11591 procedure Validate_Incomplete_Type_Instance;
11592 -- These procedures perform validation tests for the named case.
11593 -- Validate_Discriminated_Formal_Type is shared by formal private
11594 -- types and Ada 2012 formal incomplete types.
11595
11596 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean;
11597 -- Check that base types are the same and that the subtypes match
11598 -- statically. Used in several of the above.
11599
11600 ---------------------------------
11601 -- Diagnose_Predicated_Actual --
11602 ---------------------------------
11603
11604 procedure Diagnose_Predicated_Actual is
11605 begin
11606 if No_Predicate_On_Actual (A_Gen_T)
11607 and then Has_Predicates (Act_T)
11608 then
11609 Error_Msg_NE
11610 ("actual for& cannot be a type with predicate",
11611 Instantiation_Node, A_Gen_T);
11612
11613 elsif No_Dynamic_Predicate_On_Actual (A_Gen_T)
11614 and then Has_Predicates (Act_T)
11615 and then not Has_Static_Predicate_Aspect (Act_T)
11616 then
11617 Error_Msg_NE
11618 ("actual for& cannot be a type with a dynamic predicate",
11619 Instantiation_Node, A_Gen_T);
11620 end if;
11621 end Diagnose_Predicated_Actual;
11622
11623 --------------------
11624 -- Subtypes_Match --
11625 --------------------
11626
11627 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean is
11628 T : constant Entity_Id := Get_Instance_Of (Gen_T);
11629
11630 begin
11631 -- Some detailed comments would be useful here ???
11632
11633 return ((Base_Type (T) = Act_T
11634 or else Base_Type (T) = Base_Type (Act_T))
11635 and then Subtypes_Statically_Match (T, Act_T))
11636
11637 or else (Is_Class_Wide_Type (Gen_T)
11638 and then Is_Class_Wide_Type (Act_T)
11639 and then Subtypes_Match
11640 (Get_Instance_Of (Root_Type (Gen_T)),
11641 Root_Type (Act_T)))
11642
11643 or else
11644 (Ekind_In (Gen_T, E_Anonymous_Access_Subprogram_Type,
11645 E_Anonymous_Access_Type)
11646 and then Ekind (Act_T) = Ekind (Gen_T)
11647 and then Subtypes_Statically_Match
11648 (Designated_Type (Gen_T), Designated_Type (Act_T)));
11649 end Subtypes_Match;
11650
11651 -----------------------------------------
11652 -- Validate_Access_Subprogram_Instance --
11653 -----------------------------------------
11654
11655 procedure Validate_Access_Subprogram_Instance is
11656 begin
11657 if not Is_Access_Type (Act_T)
11658 or else Ekind (Designated_Type (Act_T)) /= E_Subprogram_Type
11659 then
11660 Error_Msg_NE
11661 ("expect access type in instantiation of &", Actual, Gen_T);
11662 Abandon_Instantiation (Actual);
11663 end if;
11664
11665 -- According to AI05-288, actuals for access_to_subprograms must be
11666 -- subtype conformant with the generic formal. Previous to AI05-288
11667 -- only mode conformance was required.
11668
11669 -- This is a binding interpretation that applies to previous versions
11670 -- of the language, no need to maintain previous weaker checks.
11671
11672 Check_Subtype_Conformant
11673 (Designated_Type (Act_T),
11674 Designated_Type (A_Gen_T),
11675 Actual,
11676 Get_Inst => True);
11677
11678 if Ekind (Base_Type (Act_T)) = E_Access_Protected_Subprogram_Type then
11679 if Ekind (A_Gen_T) = E_Access_Subprogram_Type then
11680 Error_Msg_NE
11681 ("protected access type not allowed for formal &",
11682 Actual, Gen_T);
11683 end if;
11684
11685 elsif Ekind (A_Gen_T) = E_Access_Protected_Subprogram_Type then
11686 Error_Msg_NE
11687 ("expect protected access type for formal &",
11688 Actual, Gen_T);
11689 end if;
11690
11691 -- If the formal has a specified convention (which in most cases
11692 -- will be StdCall) verify that the actual has the same convention.
11693
11694 if Has_Convention_Pragma (A_Gen_T)
11695 and then Convention (A_Gen_T) /= Convention (Act_T)
11696 then
11697 Error_Msg_Name_1 := Get_Convention_Name (Convention (A_Gen_T));
11698 Error_Msg_NE
11699 ("actual for formal & must have convention %", Actual, Gen_T);
11700 end if;
11701 end Validate_Access_Subprogram_Instance;
11702
11703 -----------------------------------
11704 -- Validate_Access_Type_Instance --
11705 -----------------------------------
11706
11707 procedure Validate_Access_Type_Instance is
11708 Desig_Type : constant Entity_Id :=
11709 Find_Actual_Type (Designated_Type (A_Gen_T), A_Gen_T);
11710 Desig_Act : Entity_Id;
11711
11712 begin
11713 if not Is_Access_Type (Act_T) then
11714 Error_Msg_NE
11715 ("expect access type in instantiation of &", Actual, Gen_T);
11716 Abandon_Instantiation (Actual);
11717 end if;
11718
11719 if Is_Access_Constant (A_Gen_T) then
11720 if not Is_Access_Constant (Act_T) then
11721 Error_Msg_N
11722 ("actual type must be access-to-constant type", Actual);
11723 Abandon_Instantiation (Actual);
11724 end if;
11725 else
11726 if Is_Access_Constant (Act_T) then
11727 Error_Msg_N
11728 ("actual type must be access-to-variable type", Actual);
11729 Abandon_Instantiation (Actual);
11730
11731 elsif Ekind (A_Gen_T) = E_General_Access_Type
11732 and then Ekind (Base_Type (Act_T)) /= E_General_Access_Type
11733 then
11734 Error_Msg_N -- CODEFIX
11735 ("actual must be general access type!", Actual);
11736 Error_Msg_NE -- CODEFIX
11737 ("add ALL to }!", Actual, Act_T);
11738 Abandon_Instantiation (Actual);
11739 end if;
11740 end if;
11741
11742 -- The designated subtypes, that is to say the subtypes introduced
11743 -- by an access type declaration (and not by a subtype declaration)
11744 -- must match.
11745
11746 Desig_Act := Designated_Type (Base_Type (Act_T));
11747
11748 -- The designated type may have been introduced through a limited_
11749 -- with clause, in which case retrieve the non-limited view. This
11750 -- applies to incomplete types as well as to class-wide types.
11751
11752 if From_Limited_With (Desig_Act) then
11753 Desig_Act := Available_View (Desig_Act);
11754 end if;
11755
11756 if not Subtypes_Match (Desig_Type, Desig_Act) then
11757 Error_Msg_NE
11758 ("designated type of actual does not match that of formal &",
11759 Actual, Gen_T);
11760
11761 if not Predicates_Match (Desig_Type, Desig_Act) then
11762 Error_Msg_N ("\predicates do not match", Actual);
11763 end if;
11764
11765 Abandon_Instantiation (Actual);
11766
11767 elsif Is_Access_Type (Designated_Type (Act_T))
11768 and then Is_Constrained (Designated_Type (Designated_Type (Act_T)))
11769 /=
11770 Is_Constrained (Designated_Type (Desig_Type))
11771 then
11772 Error_Msg_NE
11773 ("designated type of actual does not match that of formal &",
11774 Actual, Gen_T);
11775
11776 if not Predicates_Match (Desig_Type, Desig_Act) then
11777 Error_Msg_N ("\predicates do not match", Actual);
11778 end if;
11779
11780 Abandon_Instantiation (Actual);
11781 end if;
11782
11783 -- Ada 2005: null-exclusion indicators of the two types must agree
11784
11785 if Can_Never_Be_Null (A_Gen_T) /= Can_Never_Be_Null (Act_T) then
11786 Error_Msg_NE
11787 ("non null exclusion of actual and formal & do not match",
11788 Actual, Gen_T);
11789 end if;
11790 end Validate_Access_Type_Instance;
11791
11792 ----------------------------------
11793 -- Validate_Array_Type_Instance --
11794 ----------------------------------
11795
11796 procedure Validate_Array_Type_Instance is
11797 I1 : Node_Id;
11798 I2 : Node_Id;
11799 T2 : Entity_Id;
11800
11801 function Formal_Dimensions return Nat;
11802 -- Count number of dimensions in array type formal
11803
11804 -----------------------
11805 -- Formal_Dimensions --
11806 -----------------------
11807
11808 function Formal_Dimensions return Nat is
11809 Num : Nat := 0;
11810 Index : Node_Id;
11811
11812 begin
11813 if Nkind (Def) = N_Constrained_Array_Definition then
11814 Index := First (Discrete_Subtype_Definitions (Def));
11815 else
11816 Index := First (Subtype_Marks (Def));
11817 end if;
11818
11819 while Present (Index) loop
11820 Num := Num + 1;
11821 Next_Index (Index);
11822 end loop;
11823
11824 return Num;
11825 end Formal_Dimensions;
11826
11827 -- Start of processing for Validate_Array_Type_Instance
11828
11829 begin
11830 if not Is_Array_Type (Act_T) then
11831 Error_Msg_NE
11832 ("expect array type in instantiation of &", Actual, Gen_T);
11833 Abandon_Instantiation (Actual);
11834
11835 elsif Nkind (Def) = N_Constrained_Array_Definition then
11836 if not (Is_Constrained (Act_T)) then
11837 Error_Msg_NE
11838 ("expect constrained array in instantiation of &",
11839 Actual, Gen_T);
11840 Abandon_Instantiation (Actual);
11841 end if;
11842
11843 else
11844 if Is_Constrained (Act_T) then
11845 Error_Msg_NE
11846 ("expect unconstrained array in instantiation of &",
11847 Actual, Gen_T);
11848 Abandon_Instantiation (Actual);
11849 end if;
11850 end if;
11851
11852 if Formal_Dimensions /= Number_Dimensions (Act_T) then
11853 Error_Msg_NE
11854 ("dimensions of actual do not match formal &", Actual, Gen_T);
11855 Abandon_Instantiation (Actual);
11856 end if;
11857
11858 I1 := First_Index (A_Gen_T);
11859 I2 := First_Index (Act_T);
11860 for J in 1 .. Formal_Dimensions loop
11861
11862 -- If the indexes of the actual were given by a subtype_mark,
11863 -- the index was transformed into a range attribute. Retrieve
11864 -- the original type mark for checking.
11865
11866 if Is_Entity_Name (Original_Node (I2)) then
11867 T2 := Entity (Original_Node (I2));
11868 else
11869 T2 := Etype (I2);
11870 end if;
11871
11872 if not Subtypes_Match
11873 (Find_Actual_Type (Etype (I1), A_Gen_T), T2)
11874 then
11875 Error_Msg_NE
11876 ("index types of actual do not match those of formal &",
11877 Actual, Gen_T);
11878 Abandon_Instantiation (Actual);
11879 end if;
11880
11881 Next_Index (I1);
11882 Next_Index (I2);
11883 end loop;
11884
11885 -- Check matching subtypes. Note that there are complex visibility
11886 -- issues when the generic is a child unit and some aspect of the
11887 -- generic type is declared in a parent unit of the generic. We do
11888 -- the test to handle this special case only after a direct check
11889 -- for static matching has failed. The case where both the component
11890 -- type and the array type are separate formals, and the component
11891 -- type is a private view may also require special checking in
11892 -- Subtypes_Match.
11893
11894 if Subtypes_Match
11895 (Component_Type (A_Gen_T), Component_Type (Act_T))
11896 or else
11897 Subtypes_Match
11898 (Find_Actual_Type (Component_Type (A_Gen_T), A_Gen_T),
11899 Component_Type (Act_T))
11900 then
11901 null;
11902 else
11903 Error_Msg_NE
11904 ("component subtype of actual does not match that of formal &",
11905 Actual, Gen_T);
11906 Abandon_Instantiation (Actual);
11907 end if;
11908
11909 if Has_Aliased_Components (A_Gen_T)
11910 and then not Has_Aliased_Components (Act_T)
11911 then
11912 Error_Msg_NE
11913 ("actual must have aliased components to match formal type &",
11914 Actual, Gen_T);
11915 end if;
11916 end Validate_Array_Type_Instance;
11917
11918 -----------------------------------------------
11919 -- Validate_Derived_Interface_Type_Instance --
11920 -----------------------------------------------
11921
11922 procedure Validate_Derived_Interface_Type_Instance is
11923 Par : constant Entity_Id := Entity (Subtype_Indication (Def));
11924 Elmt : Elmt_Id;
11925
11926 begin
11927 -- First apply interface instance checks
11928
11929 Validate_Interface_Type_Instance;
11930
11931 -- Verify that immediate parent interface is an ancestor of
11932 -- the actual.
11933
11934 if Present (Par)
11935 and then not Interface_Present_In_Ancestor (Act_T, Par)
11936 then
11937 Error_Msg_NE
11938 ("interface actual must include progenitor&", Actual, Par);
11939 end if;
11940
11941 -- Now verify that the actual includes all other ancestors of
11942 -- the formal.
11943
11944 Elmt := First_Elmt (Interfaces (A_Gen_T));
11945 while Present (Elmt) loop
11946 if not Interface_Present_In_Ancestor
11947 (Act_T, Get_Instance_Of (Node (Elmt)))
11948 then
11949 Error_Msg_NE
11950 ("interface actual must include progenitor&",
11951 Actual, Node (Elmt));
11952 end if;
11953
11954 Next_Elmt (Elmt);
11955 end loop;
11956 end Validate_Derived_Interface_Type_Instance;
11957
11958 ------------------------------------
11959 -- Validate_Derived_Type_Instance --
11960 ------------------------------------
11961
11962 procedure Validate_Derived_Type_Instance is
11963 Actual_Discr : Entity_Id;
11964 Ancestor_Discr : Entity_Id;
11965
11966 begin
11967 -- If the parent type in the generic declaration is itself a previous
11968 -- formal type, then it is local to the generic and absent from the
11969 -- analyzed generic definition. In that case the ancestor is the
11970 -- instance of the formal (which must have been instantiated
11971 -- previously), unless the ancestor is itself a formal derived type.
11972 -- In this latter case (which is the subject of Corrigendum 8652/0038
11973 -- (AI-202) the ancestor of the formals is the ancestor of its
11974 -- parent. Otherwise, the analyzed generic carries the parent type.
11975 -- If the parent type is defined in a previous formal package, then
11976 -- the scope of that formal package is that of the generic type
11977 -- itself, and it has already been mapped into the corresponding type
11978 -- in the actual package.
11979
11980 -- Common case: parent type defined outside of the generic
11981
11982 if Is_Entity_Name (Subtype_Mark (Def))
11983 and then Present (Entity (Subtype_Mark (Def)))
11984 then
11985 Ancestor := Get_Instance_Of (Entity (Subtype_Mark (Def)));
11986
11987 -- Check whether parent is defined in a previous formal package
11988
11989 elsif
11990 Scope (Scope (Base_Type (Etype (A_Gen_T)))) = Scope (A_Gen_T)
11991 then
11992 Ancestor :=
11993 Get_Instance_Of (Base_Type (Etype (A_Gen_T)));
11994
11995 -- The type may be a local derivation, or a type extension of a
11996 -- previous formal, or of a formal of a parent package.
11997
11998 elsif Is_Derived_Type (Get_Instance_Of (A_Gen_T))
11999 or else
12000 Ekind (Get_Instance_Of (A_Gen_T)) = E_Record_Type_With_Private
12001 then
12002 -- Check whether the parent is another derived formal type in the
12003 -- same generic unit.
12004
12005 if Etype (A_Gen_T) /= A_Gen_T
12006 and then Is_Generic_Type (Etype (A_Gen_T))
12007 and then Scope (Etype (A_Gen_T)) = Scope (A_Gen_T)
12008 and then Etype (Etype (A_Gen_T)) /= Etype (A_Gen_T)
12009 then
12010 -- Locate ancestor of parent from the subtype declaration
12011 -- created for the actual.
12012
12013 declare
12014 Decl : Node_Id;
12015
12016 begin
12017 Decl := First (Actual_Decls);
12018 while Present (Decl) loop
12019 if Nkind (Decl) = N_Subtype_Declaration
12020 and then Chars (Defining_Identifier (Decl)) =
12021 Chars (Etype (A_Gen_T))
12022 then
12023 Ancestor := Generic_Parent_Type (Decl);
12024 exit;
12025 else
12026 Next (Decl);
12027 end if;
12028 end loop;
12029 end;
12030
12031 pragma Assert (Present (Ancestor));
12032
12033 -- The ancestor itself may be a previous formal that has been
12034 -- instantiated.
12035
12036 Ancestor := Get_Instance_Of (Ancestor);
12037
12038 else
12039 Ancestor :=
12040 Get_Instance_Of (Base_Type (Get_Instance_Of (A_Gen_T)));
12041 end if;
12042
12043 -- Check whether parent is a previous formal of the current generic
12044
12045 elsif Is_Derived_Type (A_Gen_T)
12046 and then Is_Generic_Type (Etype (A_Gen_T))
12047 and then Scope (A_Gen_T) = Scope (Etype (A_Gen_T))
12048 then
12049 Ancestor := Get_Instance_Of (First_Subtype (Etype (A_Gen_T)));
12050
12051 -- An unusual case: the actual is a type declared in a parent unit,
12052 -- but is not a formal type so there is no instance_of for it.
12053 -- Retrieve it by analyzing the record extension.
12054
12055 elsif Is_Child_Unit (Scope (A_Gen_T))
12056 and then In_Open_Scopes (Scope (Act_T))
12057 and then Is_Generic_Instance (Scope (Act_T))
12058 then
12059 Analyze (Subtype_Mark (Def));
12060 Ancestor := Entity (Subtype_Mark (Def));
12061
12062 else
12063 Ancestor := Get_Instance_Of (Etype (Base_Type (A_Gen_T)));
12064 end if;
12065
12066 -- If the formal derived type has pragma Preelaborable_Initialization
12067 -- then the actual type must have preelaborable initialization.
12068
12069 if Known_To_Have_Preelab_Init (A_Gen_T)
12070 and then not Has_Preelaborable_Initialization (Act_T)
12071 then
12072 Error_Msg_NE
12073 ("actual for & must have preelaborable initialization",
12074 Actual, Gen_T);
12075 end if;
12076
12077 -- Ada 2005 (AI-251)
12078
12079 if Ada_Version >= Ada_2005 and then Is_Interface (Ancestor) then
12080 if not Interface_Present_In_Ancestor (Act_T, Ancestor) then
12081 Error_Msg_NE
12082 ("(Ada 2005) expected type implementing & in instantiation",
12083 Actual, Ancestor);
12084 end if;
12085
12086 -- Finally verify that the (instance of) the ancestor is an ancestor
12087 -- of the actual.
12088
12089 elsif not Is_Ancestor (Base_Type (Ancestor), Act_T) then
12090 Error_Msg_NE
12091 ("expect type derived from & in instantiation",
12092 Actual, First_Subtype (Ancestor));
12093 Abandon_Instantiation (Actual);
12094 end if;
12095
12096 -- Ada 2005 (AI-443): Synchronized formal derived type checks. Note
12097 -- that the formal type declaration has been rewritten as a private
12098 -- extension.
12099
12100 if Ada_Version >= Ada_2005
12101 and then Nkind (Parent (A_Gen_T)) = N_Private_Extension_Declaration
12102 and then Synchronized_Present (Parent (A_Gen_T))
12103 then
12104 -- The actual must be a synchronized tagged type
12105
12106 if not Is_Tagged_Type (Act_T) then
12107 Error_Msg_N
12108 ("actual of synchronized type must be tagged", Actual);
12109 Abandon_Instantiation (Actual);
12110
12111 elsif Nkind (Parent (Act_T)) = N_Full_Type_Declaration
12112 and then Nkind (Type_Definition (Parent (Act_T))) =
12113 N_Derived_Type_Definition
12114 and then not Synchronized_Present
12115 (Type_Definition (Parent (Act_T)))
12116 then
12117 Error_Msg_N
12118 ("actual of synchronized type must be synchronized", Actual);
12119 Abandon_Instantiation (Actual);
12120 end if;
12121 end if;
12122
12123 -- Perform atomic/volatile checks (RM C.6(12)). Note that AI05-0218-1
12124 -- removes the second instance of the phrase "or allow pass by copy".
12125
12126 if Is_Atomic (Act_T) and then not Is_Atomic (Ancestor) then
12127 Error_Msg_N
12128 ("cannot have atomic actual type for non-atomic formal type",
12129 Actual);
12130
12131 elsif Is_Volatile (Act_T) and then not Is_Volatile (Ancestor) then
12132 Error_Msg_N
12133 ("cannot have volatile actual type for non-volatile formal type",
12134 Actual);
12135 end if;
12136
12137 -- It should not be necessary to check for unknown discriminants on
12138 -- Formal, but for some reason Has_Unknown_Discriminants is false for
12139 -- A_Gen_T, so Is_Definite_Subtype incorrectly returns True. This
12140 -- needs fixing. ???
12141
12142 if Is_Definite_Subtype (A_Gen_T)
12143 and then not Unknown_Discriminants_Present (Formal)
12144 and then not Is_Definite_Subtype (Act_T)
12145 then
12146 Error_Msg_N ("actual subtype must be constrained", Actual);
12147 Abandon_Instantiation (Actual);
12148 end if;
12149
12150 if not Unknown_Discriminants_Present (Formal) then
12151 if Is_Constrained (Ancestor) then
12152 if not Is_Constrained (Act_T) then
12153 Error_Msg_N ("actual subtype must be constrained", Actual);
12154 Abandon_Instantiation (Actual);
12155 end if;
12156
12157 -- Ancestor is unconstrained, Check if generic formal and actual
12158 -- agree on constrainedness. The check only applies to array types
12159 -- and discriminated types.
12160
12161 elsif Is_Constrained (Act_T) then
12162 if Ekind (Ancestor) = E_Access_Type
12163 or else (not Is_Constrained (A_Gen_T)
12164 and then Is_Composite_Type (A_Gen_T))
12165 then
12166 Error_Msg_N ("actual subtype must be unconstrained", Actual);
12167 Abandon_Instantiation (Actual);
12168 end if;
12169
12170 -- A class-wide type is only allowed if the formal has unknown
12171 -- discriminants.
12172
12173 elsif Is_Class_Wide_Type (Act_T)
12174 and then not Has_Unknown_Discriminants (Ancestor)
12175 then
12176 Error_Msg_NE
12177 ("actual for & cannot be a class-wide type", Actual, Gen_T);
12178 Abandon_Instantiation (Actual);
12179
12180 -- Otherwise, the formal and actual must have the same number
12181 -- of discriminants and each discriminant of the actual must
12182 -- correspond to a discriminant of the formal.
12183
12184 elsif Has_Discriminants (Act_T)
12185 and then not Has_Unknown_Discriminants (Act_T)
12186 and then Has_Discriminants (Ancestor)
12187 then
12188 Actual_Discr := First_Discriminant (Act_T);
12189 Ancestor_Discr := First_Discriminant (Ancestor);
12190 while Present (Actual_Discr)
12191 and then Present (Ancestor_Discr)
12192 loop
12193 if Base_Type (Act_T) /= Base_Type (Ancestor) and then
12194 No (Corresponding_Discriminant (Actual_Discr))
12195 then
12196 Error_Msg_NE
12197 ("discriminant & does not correspond "
12198 & "to ancestor discriminant", Actual, Actual_Discr);
12199 Abandon_Instantiation (Actual);
12200 end if;
12201
12202 Next_Discriminant (Actual_Discr);
12203 Next_Discriminant (Ancestor_Discr);
12204 end loop;
12205
12206 if Present (Actual_Discr) or else Present (Ancestor_Discr) then
12207 Error_Msg_NE
12208 ("actual for & must have same number of discriminants",
12209 Actual, Gen_T);
12210 Abandon_Instantiation (Actual);
12211 end if;
12212
12213 -- This case should be caught by the earlier check for
12214 -- constrainedness, but the check here is added for completeness.
12215
12216 elsif Has_Discriminants (Act_T)
12217 and then not Has_Unknown_Discriminants (Act_T)
12218 then
12219 Error_Msg_NE
12220 ("actual for & must not have discriminants", Actual, Gen_T);
12221 Abandon_Instantiation (Actual);
12222
12223 elsif Has_Discriminants (Ancestor) then
12224 Error_Msg_NE
12225 ("actual for & must have known discriminants", Actual, Gen_T);
12226 Abandon_Instantiation (Actual);
12227 end if;
12228
12229 if not Subtypes_Statically_Compatible
12230 (Act_T, Ancestor, Formal_Derived_Matching => True)
12231 then
12232 Error_Msg_N
12233 ("constraint on actual is incompatible with formal", Actual);
12234 Abandon_Instantiation (Actual);
12235 end if;
12236 end if;
12237
12238 -- If the formal and actual types are abstract, check that there
12239 -- are no abstract primitives of the actual type that correspond to
12240 -- nonabstract primitives of the formal type (second sentence of
12241 -- RM95 3.9.3(9)).
12242
12243 if Is_Abstract_Type (A_Gen_T) and then Is_Abstract_Type (Act_T) then
12244 Check_Abstract_Primitives : declare
12245 Gen_Prims : constant Elist_Id :=
12246 Primitive_Operations (A_Gen_T);
12247 Gen_Elmt : Elmt_Id;
12248 Gen_Subp : Entity_Id;
12249 Anc_Subp : Entity_Id;
12250 Anc_Formal : Entity_Id;
12251 Anc_F_Type : Entity_Id;
12252
12253 Act_Prims : constant Elist_Id := Primitive_Operations (Act_T);
12254 Act_Elmt : Elmt_Id;
12255 Act_Subp : Entity_Id;
12256 Act_Formal : Entity_Id;
12257 Act_F_Type : Entity_Id;
12258
12259 Subprograms_Correspond : Boolean;
12260
12261 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean;
12262 -- Returns true if T2 is derived directly or indirectly from
12263 -- T1, including derivations from interfaces. T1 and T2 are
12264 -- required to be specific tagged base types.
12265
12266 ------------------------
12267 -- Is_Tagged_Ancestor --
12268 ------------------------
12269
12270 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean
12271 is
12272 Intfc_Elmt : Elmt_Id;
12273
12274 begin
12275 -- The predicate is satisfied if the types are the same
12276
12277 if T1 = T2 then
12278 return True;
12279
12280 -- If we've reached the top of the derivation chain then
12281 -- we know that T1 is not an ancestor of T2.
12282
12283 elsif Etype (T2) = T2 then
12284 return False;
12285
12286 -- Proceed to check T2's immediate parent
12287
12288 elsif Is_Ancestor (T1, Base_Type (Etype (T2))) then
12289 return True;
12290
12291 -- Finally, check to see if T1 is an ancestor of any of T2's
12292 -- progenitors.
12293
12294 else
12295 Intfc_Elmt := First_Elmt (Interfaces (T2));
12296 while Present (Intfc_Elmt) loop
12297 if Is_Ancestor (T1, Node (Intfc_Elmt)) then
12298 return True;
12299 end if;
12300
12301 Next_Elmt (Intfc_Elmt);
12302 end loop;
12303 end if;
12304
12305 return False;
12306 end Is_Tagged_Ancestor;
12307
12308 -- Start of processing for Check_Abstract_Primitives
12309
12310 begin
12311 -- Loop over all of the formal derived type's primitives
12312
12313 Gen_Elmt := First_Elmt (Gen_Prims);
12314 while Present (Gen_Elmt) loop
12315 Gen_Subp := Node (Gen_Elmt);
12316
12317 -- If the primitive of the formal is not abstract, then
12318 -- determine whether there is a corresponding primitive of
12319 -- the actual type that's abstract.
12320
12321 if not Is_Abstract_Subprogram (Gen_Subp) then
12322 Act_Elmt := First_Elmt (Act_Prims);
12323 while Present (Act_Elmt) loop
12324 Act_Subp := Node (Act_Elmt);
12325
12326 -- If we find an abstract primitive of the actual,
12327 -- then we need to test whether it corresponds to the
12328 -- subprogram from which the generic formal primitive
12329 -- is inherited.
12330
12331 if Is_Abstract_Subprogram (Act_Subp) then
12332 Anc_Subp := Alias (Gen_Subp);
12333
12334 -- Test whether we have a corresponding primitive
12335 -- by comparing names, kinds, formal types, and
12336 -- result types.
12337
12338 if Chars (Anc_Subp) = Chars (Act_Subp)
12339 and then Ekind (Anc_Subp) = Ekind (Act_Subp)
12340 then
12341 Anc_Formal := First_Formal (Anc_Subp);
12342 Act_Formal := First_Formal (Act_Subp);
12343 while Present (Anc_Formal)
12344 and then Present (Act_Formal)
12345 loop
12346 Anc_F_Type := Etype (Anc_Formal);
12347 Act_F_Type := Etype (Act_Formal);
12348
12349 if Ekind (Anc_F_Type) =
12350 E_Anonymous_Access_Type
12351 then
12352 Anc_F_Type := Designated_Type (Anc_F_Type);
12353
12354 if Ekind (Act_F_Type) =
12355 E_Anonymous_Access_Type
12356 then
12357 Act_F_Type :=
12358 Designated_Type (Act_F_Type);
12359 else
12360 exit;
12361 end if;
12362
12363 elsif
12364 Ekind (Act_F_Type) = E_Anonymous_Access_Type
12365 then
12366 exit;
12367 end if;
12368
12369 Anc_F_Type := Base_Type (Anc_F_Type);
12370 Act_F_Type := Base_Type (Act_F_Type);
12371
12372 -- If the formal is controlling, then the
12373 -- the type of the actual primitive's formal
12374 -- must be derived directly or indirectly
12375 -- from the type of the ancestor primitive's
12376 -- formal.
12377
12378 if Is_Controlling_Formal (Anc_Formal) then
12379 if not Is_Tagged_Ancestor
12380 (Anc_F_Type, Act_F_Type)
12381 then
12382 exit;
12383 end if;
12384
12385 -- Otherwise the types of the formals must
12386 -- be the same.
12387
12388 elsif Anc_F_Type /= Act_F_Type then
12389 exit;
12390 end if;
12391
12392 Next_Entity (Anc_Formal);
12393 Next_Entity (Act_Formal);
12394 end loop;
12395
12396 -- If we traversed through all of the formals
12397 -- then so far the subprograms correspond, so
12398 -- now check that any result types correspond.
12399
12400 if No (Anc_Formal) and then No (Act_Formal) then
12401 Subprograms_Correspond := True;
12402
12403 if Ekind (Act_Subp) = E_Function then
12404 Anc_F_Type := Etype (Anc_Subp);
12405 Act_F_Type := Etype (Act_Subp);
12406
12407 if Ekind (Anc_F_Type) =
12408 E_Anonymous_Access_Type
12409 then
12410 Anc_F_Type :=
12411 Designated_Type (Anc_F_Type);
12412
12413 if Ekind (Act_F_Type) =
12414 E_Anonymous_Access_Type
12415 then
12416 Act_F_Type :=
12417 Designated_Type (Act_F_Type);
12418 else
12419 Subprograms_Correspond := False;
12420 end if;
12421
12422 elsif
12423 Ekind (Act_F_Type)
12424 = E_Anonymous_Access_Type
12425 then
12426 Subprograms_Correspond := False;
12427 end if;
12428
12429 Anc_F_Type := Base_Type (Anc_F_Type);
12430 Act_F_Type := Base_Type (Act_F_Type);
12431
12432 -- Now either the result types must be
12433 -- the same or, if the result type is
12434 -- controlling, the result type of the
12435 -- actual primitive must descend from the
12436 -- result type of the ancestor primitive.
12437
12438 if Subprograms_Correspond
12439 and then Anc_F_Type /= Act_F_Type
12440 and then
12441 Has_Controlling_Result (Anc_Subp)
12442 and then not Is_Tagged_Ancestor
12443 (Anc_F_Type, Act_F_Type)
12444 then
12445 Subprograms_Correspond := False;
12446 end if;
12447 end if;
12448
12449 -- Found a matching subprogram belonging to
12450 -- formal ancestor type, so actual subprogram
12451 -- corresponds and this violates 3.9.3(9).
12452
12453 if Subprograms_Correspond then
12454 Error_Msg_NE
12455 ("abstract subprogram & overrides "
12456 & "nonabstract subprogram of ancestor",
12457 Actual, Act_Subp);
12458 end if;
12459 end if;
12460 end if;
12461 end if;
12462
12463 Next_Elmt (Act_Elmt);
12464 end loop;
12465 end if;
12466
12467 Next_Elmt (Gen_Elmt);
12468 end loop;
12469 end Check_Abstract_Primitives;
12470 end if;
12471
12472 -- Verify that limitedness matches. If parent is a limited
12473 -- interface then the generic formal is not unless declared
12474 -- explicitly so. If not declared limited, the actual cannot be
12475 -- limited (see AI05-0087).
12476
12477 -- Even though this AI is a binding interpretation, we enable the
12478 -- check only in Ada 2012 mode, because this improper construct
12479 -- shows up in user code and in existing B-tests.
12480
12481 if Is_Limited_Type (Act_T)
12482 and then not Is_Limited_Type (A_Gen_T)
12483 and then Ada_Version >= Ada_2012
12484 then
12485 if In_Instance then
12486 null;
12487 else
12488 Error_Msg_NE
12489 ("actual for non-limited & cannot be a limited type",
12490 Actual, Gen_T);
12491 Explain_Limited_Type (Act_T, Actual);
12492 Abandon_Instantiation (Actual);
12493 end if;
12494 end if;
12495 end Validate_Derived_Type_Instance;
12496
12497 ----------------------------------------
12498 -- Validate_Discriminated_Formal_Type --
12499 ----------------------------------------
12500
12501 procedure Validate_Discriminated_Formal_Type is
12502 Formal_Discr : Entity_Id;
12503 Actual_Discr : Entity_Id;
12504 Formal_Subt : Entity_Id;
12505
12506 begin
12507 if Has_Discriminants (A_Gen_T) then
12508 if not Has_Discriminants (Act_T) then
12509 Error_Msg_NE
12510 ("actual for & must have discriminants", Actual, Gen_T);
12511 Abandon_Instantiation (Actual);
12512
12513 elsif Is_Constrained (Act_T) then
12514 Error_Msg_NE
12515 ("actual for & must be unconstrained", Actual, Gen_T);
12516 Abandon_Instantiation (Actual);
12517
12518 else
12519 Formal_Discr := First_Discriminant (A_Gen_T);
12520 Actual_Discr := First_Discriminant (Act_T);
12521 while Formal_Discr /= Empty loop
12522 if Actual_Discr = Empty then
12523 Error_Msg_NE
12524 ("discriminants on actual do not match formal",
12525 Actual, Gen_T);
12526 Abandon_Instantiation (Actual);
12527 end if;
12528
12529 Formal_Subt := Get_Instance_Of (Etype (Formal_Discr));
12530
12531 -- Access discriminants match if designated types do
12532
12533 if Ekind (Base_Type (Formal_Subt)) = E_Anonymous_Access_Type
12534 and then (Ekind (Base_Type (Etype (Actual_Discr)))) =
12535 E_Anonymous_Access_Type
12536 and then
12537 Get_Instance_Of
12538 (Designated_Type (Base_Type (Formal_Subt))) =
12539 Designated_Type (Base_Type (Etype (Actual_Discr)))
12540 then
12541 null;
12542
12543 elsif Base_Type (Formal_Subt) /=
12544 Base_Type (Etype (Actual_Discr))
12545 then
12546 Error_Msg_NE
12547 ("types of actual discriminants must match formal",
12548 Actual, Gen_T);
12549 Abandon_Instantiation (Actual);
12550
12551 elsif not Subtypes_Statically_Match
12552 (Formal_Subt, Etype (Actual_Discr))
12553 and then Ada_Version >= Ada_95
12554 then
12555 Error_Msg_NE
12556 ("subtypes of actual discriminants must match formal",
12557 Actual, Gen_T);
12558 Abandon_Instantiation (Actual);
12559 end if;
12560
12561 Next_Discriminant (Formal_Discr);
12562 Next_Discriminant (Actual_Discr);
12563 end loop;
12564
12565 if Actual_Discr /= Empty then
12566 Error_Msg_NE
12567 ("discriminants on actual do not match formal",
12568 Actual, Gen_T);
12569 Abandon_Instantiation (Actual);
12570 end if;
12571 end if;
12572 end if;
12573 end Validate_Discriminated_Formal_Type;
12574
12575 ---------------------------------------
12576 -- Validate_Incomplete_Type_Instance --
12577 ---------------------------------------
12578
12579 procedure Validate_Incomplete_Type_Instance is
12580 begin
12581 if not Is_Tagged_Type (Act_T)
12582 and then Is_Tagged_Type (A_Gen_T)
12583 then
12584 Error_Msg_NE
12585 ("actual for & must be a tagged type", Actual, Gen_T);
12586 end if;
12587
12588 Validate_Discriminated_Formal_Type;
12589 end Validate_Incomplete_Type_Instance;
12590
12591 --------------------------------------
12592 -- Validate_Interface_Type_Instance --
12593 --------------------------------------
12594
12595 procedure Validate_Interface_Type_Instance is
12596 begin
12597 if not Is_Interface (Act_T) then
12598 Error_Msg_NE
12599 ("actual for formal interface type must be an interface",
12600 Actual, Gen_T);
12601
12602 elsif Is_Limited_Type (Act_T) /= Is_Limited_Type (A_Gen_T)
12603 or else Is_Task_Interface (A_Gen_T) /= Is_Task_Interface (Act_T)
12604 or else Is_Protected_Interface (A_Gen_T) /=
12605 Is_Protected_Interface (Act_T)
12606 or else Is_Synchronized_Interface (A_Gen_T) /=
12607 Is_Synchronized_Interface (Act_T)
12608 then
12609 Error_Msg_NE
12610 ("actual for interface& does not match (RM 12.5.5(4))",
12611 Actual, Gen_T);
12612 end if;
12613 end Validate_Interface_Type_Instance;
12614
12615 ------------------------------------
12616 -- Validate_Private_Type_Instance --
12617 ------------------------------------
12618
12619 procedure Validate_Private_Type_Instance is
12620 begin
12621 if Is_Limited_Type (Act_T)
12622 and then not Is_Limited_Type (A_Gen_T)
12623 then
12624 if In_Instance then
12625 null;
12626 else
12627 Error_Msg_NE
12628 ("actual for non-limited & cannot be a limited type", Actual,
12629 Gen_T);
12630 Explain_Limited_Type (Act_T, Actual);
12631 Abandon_Instantiation (Actual);
12632 end if;
12633
12634 elsif Known_To_Have_Preelab_Init (A_Gen_T)
12635 and then not Has_Preelaborable_Initialization (Act_T)
12636 then
12637 Error_Msg_NE
12638 ("actual for & must have preelaborable initialization", Actual,
12639 Gen_T);
12640
12641 elsif not Is_Definite_Subtype (Act_T)
12642 and then Is_Definite_Subtype (A_Gen_T)
12643 and then Ada_Version >= Ada_95
12644 then
12645 Error_Msg_NE
12646 ("actual for & must be a definite subtype", Actual, Gen_T);
12647
12648 elsif not Is_Tagged_Type (Act_T)
12649 and then Is_Tagged_Type (A_Gen_T)
12650 then
12651 Error_Msg_NE
12652 ("actual for & must be a tagged type", Actual, Gen_T);
12653 end if;
12654
12655 Validate_Discriminated_Formal_Type;
12656 Ancestor := Gen_T;
12657 end Validate_Private_Type_Instance;
12658
12659 -- Start of processing for Instantiate_Type
12660
12661 begin
12662 if Get_Instance_Of (A_Gen_T) /= A_Gen_T then
12663 Error_Msg_N ("duplicate instantiation of generic type", Actual);
12664 return New_List (Error);
12665
12666 elsif not Is_Entity_Name (Actual)
12667 or else not Is_Type (Entity (Actual))
12668 then
12669 Error_Msg_NE
12670 ("expect valid subtype mark to instantiate &", Actual, Gen_T);
12671 Abandon_Instantiation (Actual);
12672
12673 else
12674 Act_T := Entity (Actual);
12675
12676 -- Ada 2005 (AI-216): An Unchecked_Union subtype shall only be passed
12677 -- as a generic actual parameter if the corresponding formal type
12678 -- does not have a known_discriminant_part, or is a formal derived
12679 -- type that is an Unchecked_Union type.
12680
12681 if Is_Unchecked_Union (Base_Type (Act_T)) then
12682 if not Has_Discriminants (A_Gen_T)
12683 or else (Is_Derived_Type (A_Gen_T)
12684 and then Is_Unchecked_Union (A_Gen_T))
12685 then
12686 null;
12687 else
12688 Error_Msg_N ("unchecked union cannot be the actual for a "
12689 & "discriminated formal type", Act_T);
12690
12691 end if;
12692 end if;
12693
12694 -- Deal with fixed/floating restrictions
12695
12696 if Is_Floating_Point_Type (Act_T) then
12697 Check_Restriction (No_Floating_Point, Actual);
12698 elsif Is_Fixed_Point_Type (Act_T) then
12699 Check_Restriction (No_Fixed_Point, Actual);
12700 end if;
12701
12702 -- Deal with error of using incomplete type as generic actual.
12703 -- This includes limited views of a type, even if the non-limited
12704 -- view may be available.
12705
12706 if Ekind (Act_T) = E_Incomplete_Type
12707 or else (Is_Class_Wide_Type (Act_T)
12708 and then Ekind (Root_Type (Act_T)) = E_Incomplete_Type)
12709 then
12710 -- If the formal is an incomplete type, the actual can be
12711 -- incomplete as well.
12712
12713 if Ekind (A_Gen_T) = E_Incomplete_Type then
12714 null;
12715
12716 elsif Is_Class_Wide_Type (Act_T)
12717 or else No (Full_View (Act_T))
12718 then
12719 Error_Msg_N ("premature use of incomplete type", Actual);
12720 Abandon_Instantiation (Actual);
12721 else
12722 Act_T := Full_View (Act_T);
12723 Set_Entity (Actual, Act_T);
12724
12725 if Has_Private_Component (Act_T) then
12726 Error_Msg_N
12727 ("premature use of type with private component", Actual);
12728 end if;
12729 end if;
12730
12731 -- Deal with error of premature use of private type as generic actual
12732
12733 elsif Is_Private_Type (Act_T)
12734 and then Is_Private_Type (Base_Type (Act_T))
12735 and then not Is_Generic_Type (Act_T)
12736 and then not Is_Derived_Type (Act_T)
12737 and then No (Full_View (Root_Type (Act_T)))
12738 then
12739 -- If the formal is an incomplete type, the actual can be
12740 -- private or incomplete as well.
12741
12742 if Ekind (A_Gen_T) = E_Incomplete_Type then
12743 null;
12744 else
12745 Error_Msg_N ("premature use of private type", Actual);
12746 end if;
12747
12748 elsif Has_Private_Component (Act_T) then
12749 Error_Msg_N
12750 ("premature use of type with private component", Actual);
12751 end if;
12752
12753 Set_Instance_Of (A_Gen_T, Act_T);
12754
12755 -- If the type is generic, the class-wide type may also be used
12756
12757 if Is_Tagged_Type (A_Gen_T)
12758 and then Is_Tagged_Type (Act_T)
12759 and then not Is_Class_Wide_Type (A_Gen_T)
12760 then
12761 Set_Instance_Of (Class_Wide_Type (A_Gen_T),
12762 Class_Wide_Type (Act_T));
12763 end if;
12764
12765 if not Is_Abstract_Type (A_Gen_T)
12766 and then Is_Abstract_Type (Act_T)
12767 then
12768 Error_Msg_N
12769 ("actual of non-abstract formal cannot be abstract", Actual);
12770 end if;
12771
12772 -- A generic scalar type is a first subtype for which we generate
12773 -- an anonymous base type. Indicate that the instance of this base
12774 -- is the base type of the actual.
12775
12776 if Is_Scalar_Type (A_Gen_T) then
12777 Set_Instance_Of (Etype (A_Gen_T), Etype (Act_T));
12778 end if;
12779 end if;
12780
12781 if Error_Posted (Act_T) then
12782 null;
12783 else
12784 case Nkind (Def) is
12785 when N_Formal_Private_Type_Definition =>
12786 Validate_Private_Type_Instance;
12787
12788 when N_Formal_Incomplete_Type_Definition =>
12789 Validate_Incomplete_Type_Instance;
12790
12791 when N_Formal_Derived_Type_Definition =>
12792 Validate_Derived_Type_Instance;
12793
12794 when N_Formal_Discrete_Type_Definition =>
12795 if not Is_Discrete_Type (Act_T) then
12796 Error_Msg_NE
12797 ("expect discrete type in instantiation of&",
12798 Actual, Gen_T);
12799 Abandon_Instantiation (Actual);
12800 end if;
12801
12802 Diagnose_Predicated_Actual;
12803
12804 when N_Formal_Signed_Integer_Type_Definition =>
12805 if not Is_Signed_Integer_Type (Act_T) then
12806 Error_Msg_NE
12807 ("expect signed integer type in instantiation of&",
12808 Actual, Gen_T);
12809 Abandon_Instantiation (Actual);
12810 end if;
12811
12812 Diagnose_Predicated_Actual;
12813
12814 when N_Formal_Modular_Type_Definition =>
12815 if not Is_Modular_Integer_Type (Act_T) then
12816 Error_Msg_NE
12817 ("expect modular type in instantiation of &",
12818 Actual, Gen_T);
12819 Abandon_Instantiation (Actual);
12820 end if;
12821
12822 Diagnose_Predicated_Actual;
12823
12824 when N_Formal_Floating_Point_Definition =>
12825 if not Is_Floating_Point_Type (Act_T) then
12826 Error_Msg_NE
12827 ("expect float type in instantiation of &", Actual, Gen_T);
12828 Abandon_Instantiation (Actual);
12829 end if;
12830
12831 when N_Formal_Ordinary_Fixed_Point_Definition =>
12832 if not Is_Ordinary_Fixed_Point_Type (Act_T) then
12833 Error_Msg_NE
12834 ("expect ordinary fixed point type in instantiation of &",
12835 Actual, Gen_T);
12836 Abandon_Instantiation (Actual);
12837 end if;
12838
12839 when N_Formal_Decimal_Fixed_Point_Definition =>
12840 if not Is_Decimal_Fixed_Point_Type (Act_T) then
12841 Error_Msg_NE
12842 ("expect decimal type in instantiation of &",
12843 Actual, Gen_T);
12844 Abandon_Instantiation (Actual);
12845 end if;
12846
12847 when N_Array_Type_Definition =>
12848 Validate_Array_Type_Instance;
12849
12850 when N_Access_To_Object_Definition =>
12851 Validate_Access_Type_Instance;
12852
12853 when N_Access_Function_Definition
12854 | N_Access_Procedure_Definition
12855 =>
12856 Validate_Access_Subprogram_Instance;
12857
12858 when N_Record_Definition =>
12859 Validate_Interface_Type_Instance;
12860
12861 when N_Derived_Type_Definition =>
12862 Validate_Derived_Interface_Type_Instance;
12863
12864 when others =>
12865 raise Program_Error;
12866 end case;
12867 end if;
12868
12869 Subt := New_Copy (Gen_T);
12870
12871 -- Use adjusted sloc of subtype name as the location for other nodes in
12872 -- the subtype declaration.
12873
12874 Loc := Sloc (Subt);
12875
12876 Decl_Node :=
12877 Make_Subtype_Declaration (Loc,
12878 Defining_Identifier => Subt,
12879 Subtype_Indication => New_Occurrence_Of (Act_T, Loc));
12880
12881 if Is_Private_Type (Act_T) then
12882 Set_Has_Private_View (Subtype_Indication (Decl_Node));
12883
12884 elsif Is_Access_Type (Act_T)
12885 and then Is_Private_Type (Designated_Type (Act_T))
12886 then
12887 Set_Has_Private_View (Subtype_Indication (Decl_Node));
12888 end if;
12889
12890 -- In Ada 2012 the actual may be a limited view. Indicate that
12891 -- the local subtype must be treated as such.
12892
12893 if From_Limited_With (Act_T) then
12894 Set_Ekind (Subt, E_Incomplete_Subtype);
12895 Set_From_Limited_With (Subt);
12896 end if;
12897
12898 Decl_Nodes := New_List (Decl_Node);
12899
12900 -- Flag actual derived types so their elaboration produces the
12901 -- appropriate renamings for the primitive operations of the ancestor.
12902 -- Flag actual for formal private types as well, to determine whether
12903 -- operations in the private part may override inherited operations.
12904 -- If the formal has an interface list, the ancestor is not the
12905 -- parent, but the analyzed formal that includes the interface
12906 -- operations of all its progenitors.
12907
12908 -- Same treatment for formal private types, so we can check whether the
12909 -- type is tagged limited when validating derivations in the private
12910 -- part. (See AI05-096).
12911
12912 if Nkind (Def) = N_Formal_Derived_Type_Definition then
12913 if Present (Interface_List (Def)) then
12914 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
12915 else
12916 Set_Generic_Parent_Type (Decl_Node, Ancestor);
12917 end if;
12918
12919 elsif Nkind_In (Def, N_Formal_Private_Type_Definition,
12920 N_Formal_Incomplete_Type_Definition)
12921 then
12922 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
12923 end if;
12924
12925 -- If the actual is a synchronized type that implements an interface,
12926 -- the primitive operations are attached to the corresponding record,
12927 -- and we have to treat it as an additional generic actual, so that its
12928 -- primitive operations become visible in the instance. The task or
12929 -- protected type itself does not carry primitive operations.
12930
12931 if Is_Concurrent_Type (Act_T)
12932 and then Is_Tagged_Type (Act_T)
12933 and then Present (Corresponding_Record_Type (Act_T))
12934 and then Present (Ancestor)
12935 and then Is_Interface (Ancestor)
12936 then
12937 declare
12938 Corr_Rec : constant Entity_Id :=
12939 Corresponding_Record_Type (Act_T);
12940 New_Corr : Entity_Id;
12941 Corr_Decl : Node_Id;
12942
12943 begin
12944 New_Corr := Make_Temporary (Loc, 'S');
12945 Corr_Decl :=
12946 Make_Subtype_Declaration (Loc,
12947 Defining_Identifier => New_Corr,
12948 Subtype_Indication =>
12949 New_Occurrence_Of (Corr_Rec, Loc));
12950 Append_To (Decl_Nodes, Corr_Decl);
12951
12952 if Ekind (Act_T) = E_Task_Type then
12953 Set_Ekind (Subt, E_Task_Subtype);
12954 else
12955 Set_Ekind (Subt, E_Protected_Subtype);
12956 end if;
12957
12958 Set_Corresponding_Record_Type (Subt, Corr_Rec);
12959 Set_Generic_Parent_Type (Corr_Decl, Ancestor);
12960 Set_Generic_Parent_Type (Decl_Node, Empty);
12961 end;
12962 end if;
12963
12964 -- For a floating-point type, capture dimension info if any, because
12965 -- the generated subtype declaration does not come from source and
12966 -- will not process dimensions.
12967
12968 if Is_Floating_Point_Type (Act_T) then
12969 Copy_Dimensions (Act_T, Subt);
12970 end if;
12971
12972 return Decl_Nodes;
12973 end Instantiate_Type;
12974
12975 ---------------------
12976 -- Is_In_Main_Unit --
12977 ---------------------
12978
12979 function Is_In_Main_Unit (N : Node_Id) return Boolean is
12980 Unum : constant Unit_Number_Type := Get_Source_Unit (N);
12981 Current_Unit : Node_Id;
12982
12983 begin
12984 if Unum = Main_Unit then
12985 return True;
12986
12987 -- If the current unit is a subunit then it is either the main unit or
12988 -- is being compiled as part of the main unit.
12989
12990 elsif Nkind (N) = N_Compilation_Unit then
12991 return Nkind (Unit (N)) = N_Subunit;
12992 end if;
12993
12994 Current_Unit := Parent (N);
12995 while Present (Current_Unit)
12996 and then Nkind (Current_Unit) /= N_Compilation_Unit
12997 loop
12998 Current_Unit := Parent (Current_Unit);
12999 end loop;
13000
13001 -- The instantiation node is in the main unit, or else the current node
13002 -- (perhaps as the result of nested instantiations) is in the main unit,
13003 -- or in the declaration of the main unit, which in this last case must
13004 -- be a body.
13005
13006 return
13007 Current_Unit = Cunit (Main_Unit)
13008 or else Current_Unit = Library_Unit (Cunit (Main_Unit))
13009 or else (Present (Current_Unit)
13010 and then Present (Library_Unit (Current_Unit))
13011 and then Is_In_Main_Unit (Library_Unit (Current_Unit)));
13012 end Is_In_Main_Unit;
13013
13014 ----------------------------
13015 -- Load_Parent_Of_Generic --
13016 ----------------------------
13017
13018 procedure Load_Parent_Of_Generic
13019 (N : Node_Id;
13020 Spec : Node_Id;
13021 Body_Optional : Boolean := False)
13022 is
13023 Comp_Unit : constant Node_Id := Cunit (Get_Source_Unit (Spec));
13024 Saved_Style_Check : constant Boolean := Style_Check;
13025 Saved_Warnings : constant Warning_Record := Save_Warnings;
13026 True_Parent : Node_Id;
13027 Inst_Node : Node_Id;
13028 OK : Boolean;
13029 Previous_Instances : constant Elist_Id := New_Elmt_List;
13030
13031 procedure Collect_Previous_Instances (Decls : List_Id);
13032 -- Collect all instantiations in the given list of declarations, that
13033 -- precede the generic that we need to load. If the bodies of these
13034 -- instantiations are available, we must analyze them, to ensure that
13035 -- the public symbols generated are the same when the unit is compiled
13036 -- to generate code, and when it is compiled in the context of a unit
13037 -- that needs a particular nested instance. This process is applied to
13038 -- both package and subprogram instances.
13039
13040 --------------------------------
13041 -- Collect_Previous_Instances --
13042 --------------------------------
13043
13044 procedure Collect_Previous_Instances (Decls : List_Id) is
13045 Decl : Node_Id;
13046
13047 begin
13048 Decl := First (Decls);
13049 while Present (Decl) loop
13050 if Sloc (Decl) >= Sloc (Inst_Node) then
13051 return;
13052
13053 -- If Decl is an instantiation, then record it as requiring
13054 -- instantiation of the corresponding body, except if it is an
13055 -- abbreviated instantiation generated internally for conformance
13056 -- checking purposes only for the case of a formal package
13057 -- declared without a box (see Instantiate_Formal_Package). Such
13058 -- an instantiation does not generate any code (the actual code
13059 -- comes from actual) and thus does not need to be analyzed here.
13060 -- If the instantiation appears with a generic package body it is
13061 -- not analyzed here either.
13062
13063 elsif Nkind (Decl) = N_Package_Instantiation
13064 and then not Is_Internal (Defining_Entity (Decl))
13065 then
13066 Append_Elmt (Decl, Previous_Instances);
13067
13068 -- For a subprogram instantiation, omit instantiations intrinsic
13069 -- operations (Unchecked_Conversions, etc.) that have no bodies.
13070
13071 elsif Nkind_In (Decl, N_Function_Instantiation,
13072 N_Procedure_Instantiation)
13073 and then not Is_Intrinsic_Subprogram (Entity (Name (Decl)))
13074 then
13075 Append_Elmt (Decl, Previous_Instances);
13076
13077 elsif Nkind (Decl) = N_Package_Declaration then
13078 Collect_Previous_Instances
13079 (Visible_Declarations (Specification (Decl)));
13080 Collect_Previous_Instances
13081 (Private_Declarations (Specification (Decl)));
13082
13083 -- Previous non-generic bodies may contain instances as well
13084
13085 elsif Nkind (Decl) = N_Package_Body
13086 and then Ekind (Corresponding_Spec (Decl)) /= E_Generic_Package
13087 then
13088 Collect_Previous_Instances (Declarations (Decl));
13089
13090 elsif Nkind (Decl) = N_Subprogram_Body
13091 and then not Acts_As_Spec (Decl)
13092 and then not Is_Generic_Subprogram (Corresponding_Spec (Decl))
13093 then
13094 Collect_Previous_Instances (Declarations (Decl));
13095 end if;
13096
13097 Next (Decl);
13098 end loop;
13099 end Collect_Previous_Instances;
13100
13101 -- Start of processing for Load_Parent_Of_Generic
13102
13103 begin
13104 if not In_Same_Source_Unit (N, Spec)
13105 or else Nkind (Unit (Comp_Unit)) = N_Package_Declaration
13106 or else (Nkind (Unit (Comp_Unit)) = N_Package_Body
13107 and then not Is_In_Main_Unit (Spec))
13108 then
13109 -- Find body of parent of spec, and analyze it. A special case arises
13110 -- when the parent is an instantiation, that is to say when we are
13111 -- currently instantiating a nested generic. In that case, there is
13112 -- no separate file for the body of the enclosing instance. Instead,
13113 -- the enclosing body must be instantiated as if it were a pending
13114 -- instantiation, in order to produce the body for the nested generic
13115 -- we require now. Note that in that case the generic may be defined
13116 -- in a package body, the instance defined in the same package body,
13117 -- and the original enclosing body may not be in the main unit.
13118
13119 Inst_Node := Empty;
13120
13121 True_Parent := Parent (Spec);
13122 while Present (True_Parent)
13123 and then Nkind (True_Parent) /= N_Compilation_Unit
13124 loop
13125 if Nkind (True_Parent) = N_Package_Declaration
13126 and then
13127 Nkind (Original_Node (True_Parent)) = N_Package_Instantiation
13128 then
13129 -- Parent is a compilation unit that is an instantiation.
13130 -- Instantiation node has been replaced with package decl.
13131
13132 Inst_Node := Original_Node (True_Parent);
13133 exit;
13134
13135 elsif Nkind (True_Parent) = N_Package_Declaration
13136 and then Present (Generic_Parent (Specification (True_Parent)))
13137 and then Nkind (Parent (True_Parent)) /= N_Compilation_Unit
13138 then
13139 -- Parent is an instantiation within another specification.
13140 -- Declaration for instance has been inserted before original
13141 -- instantiation node. A direct link would be preferable?
13142
13143 Inst_Node := Next (True_Parent);
13144 while Present (Inst_Node)
13145 and then Nkind (Inst_Node) /= N_Package_Instantiation
13146 loop
13147 Next (Inst_Node);
13148 end loop;
13149
13150 -- If the instance appears within a generic, and the generic
13151 -- unit is defined within a formal package of the enclosing
13152 -- generic, there is no generic body available, and none
13153 -- needed. A more precise test should be used ???
13154
13155 if No (Inst_Node) then
13156 return;
13157 end if;
13158
13159 exit;
13160
13161 else
13162 True_Parent := Parent (True_Parent);
13163 end if;
13164 end loop;
13165
13166 -- Case where we are currently instantiating a nested generic
13167
13168 if Present (Inst_Node) then
13169 if Nkind (Parent (True_Parent)) = N_Compilation_Unit then
13170
13171 -- Instantiation node and declaration of instantiated package
13172 -- were exchanged when only the declaration was needed.
13173 -- Restore instantiation node before proceeding with body.
13174
13175 Set_Unit (Parent (True_Parent), Inst_Node);
13176 end if;
13177
13178 -- Now complete instantiation of enclosing body, if it appears in
13179 -- some other unit. If it appears in the current unit, the body
13180 -- will have been instantiated already.
13181
13182 if No (Corresponding_Body (Instance_Spec (Inst_Node))) then
13183
13184 -- We need to determine the expander mode to instantiate the
13185 -- enclosing body. Because the generic body we need may use
13186 -- global entities declared in the enclosing package (including
13187 -- aggregates) it is in general necessary to compile this body
13188 -- with expansion enabled, except if we are within a generic
13189 -- package, in which case the usual generic rule applies.
13190
13191 declare
13192 Exp_Status : Boolean := True;
13193 Scop : Entity_Id;
13194
13195 begin
13196 -- Loop through scopes looking for generic package
13197
13198 Scop := Scope (Defining_Entity (Instance_Spec (Inst_Node)));
13199 while Present (Scop)
13200 and then Scop /= Standard_Standard
13201 loop
13202 if Ekind (Scop) = E_Generic_Package then
13203 Exp_Status := False;
13204 exit;
13205 end if;
13206
13207 Scop := Scope (Scop);
13208 end loop;
13209
13210 -- Collect previous instantiations in the unit that contains
13211 -- the desired generic.
13212
13213 if Nkind (Parent (True_Parent)) /= N_Compilation_Unit
13214 and then not Body_Optional
13215 then
13216 declare
13217 Decl : Elmt_Id;
13218 Info : Pending_Body_Info;
13219 Par : Node_Id;
13220
13221 begin
13222 Par := Parent (Inst_Node);
13223 while Present (Par) loop
13224 exit when Nkind (Parent (Par)) = N_Compilation_Unit;
13225 Par := Parent (Par);
13226 end loop;
13227
13228 pragma Assert (Present (Par));
13229
13230 if Nkind (Par) = N_Package_Body then
13231 Collect_Previous_Instances (Declarations (Par));
13232
13233 elsif Nkind (Par) = N_Package_Declaration then
13234 Collect_Previous_Instances
13235 (Visible_Declarations (Specification (Par)));
13236 Collect_Previous_Instances
13237 (Private_Declarations (Specification (Par)));
13238
13239 else
13240 -- Enclosing unit is a subprogram body. In this
13241 -- case all instance bodies are processed in order
13242 -- and there is no need to collect them separately.
13243
13244 null;
13245 end if;
13246
13247 Decl := First_Elmt (Previous_Instances);
13248 while Present (Decl) loop
13249 Info :=
13250 (Inst_Node => Node (Decl),
13251 Act_Decl =>
13252 Instance_Spec (Node (Decl)),
13253 Expander_Status => Exp_Status,
13254 Current_Sem_Unit =>
13255 Get_Code_Unit (Sloc (Node (Decl))),
13256 Scope_Suppress => Scope_Suppress,
13257 Local_Suppress_Stack_Top =>
13258 Local_Suppress_Stack_Top,
13259 Version => Ada_Version,
13260 Version_Pragma => Ada_Version_Pragma,
13261 Warnings => Save_Warnings,
13262 SPARK_Mode => SPARK_Mode,
13263 SPARK_Mode_Pragma => SPARK_Mode_Pragma);
13264
13265 -- Package instance
13266
13267 if
13268 Nkind (Node (Decl)) = N_Package_Instantiation
13269 then
13270 Instantiate_Package_Body
13271 (Info, Body_Optional => True);
13272
13273 -- Subprogram instance
13274
13275 else
13276 -- The instance_spec is in the wrapper package,
13277 -- usually followed by its local renaming
13278 -- declaration. See Build_Subprogram_Renaming
13279 -- for details. If the instance carries aspects,
13280 -- these result in the corresponding pragmas,
13281 -- inserted after the subprogram declaration.
13282 -- They must be skipped as well when retrieving
13283 -- the desired spec. A direct link would be
13284 -- more robust ???
13285
13286 declare
13287 Decl : Node_Id :=
13288 (Last (Visible_Declarations
13289 (Specification (Info.Act_Decl))));
13290 begin
13291 while Nkind_In (Decl,
13292 N_Subprogram_Renaming_Declaration, N_Pragma)
13293 loop
13294 Decl := Prev (Decl);
13295 end loop;
13296
13297 Info.Act_Decl := Decl;
13298 end;
13299
13300 Instantiate_Subprogram_Body
13301 (Info, Body_Optional => True);
13302 end if;
13303
13304 Next_Elmt (Decl);
13305 end loop;
13306 end;
13307 end if;
13308
13309 Instantiate_Package_Body
13310 (Body_Info =>
13311 ((Inst_Node => Inst_Node,
13312 Act_Decl => True_Parent,
13313 Expander_Status => Exp_Status,
13314 Current_Sem_Unit => Get_Code_Unit
13315 (Sloc (Inst_Node)),
13316 Scope_Suppress => Scope_Suppress,
13317 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
13318 Version => Ada_Version,
13319 Version_Pragma => Ada_Version_Pragma,
13320 Warnings => Save_Warnings,
13321 SPARK_Mode => SPARK_Mode,
13322 SPARK_Mode_Pragma => SPARK_Mode_Pragma)),
13323 Body_Optional => Body_Optional);
13324 end;
13325 end if;
13326
13327 -- Case where we are not instantiating a nested generic
13328
13329 else
13330 Opt.Style_Check := False;
13331 Expander_Mode_Save_And_Set (True);
13332 Load_Needed_Body (Comp_Unit, OK);
13333 Opt.Style_Check := Saved_Style_Check;
13334 Restore_Warnings (Saved_Warnings);
13335 Expander_Mode_Restore;
13336
13337 if not OK
13338 and then Unit_Requires_Body (Defining_Entity (Spec))
13339 and then not Body_Optional
13340 then
13341 declare
13342 Bname : constant Unit_Name_Type :=
13343 Get_Body_Name (Get_Unit_Name (Unit (Comp_Unit)));
13344
13345 begin
13346 -- In CodePeer mode, the missing body may make the analysis
13347 -- incomplete, but we do not treat it as fatal.
13348
13349 if CodePeer_Mode then
13350 return;
13351
13352 else
13353 Error_Msg_Unit_1 := Bname;
13354 Error_Msg_N ("this instantiation requires$!", N);
13355 Error_Msg_File_1 :=
13356 Get_File_Name (Bname, Subunit => False);
13357 Error_Msg_N ("\but file{ was not found!", N);
13358 raise Unrecoverable_Error;
13359 end if;
13360 end;
13361 end if;
13362 end if;
13363 end if;
13364
13365 -- If loading parent of the generic caused an instantiation circularity,
13366 -- we abandon compilation at this point, because otherwise in some cases
13367 -- we get into trouble with infinite recursions after this point.
13368
13369 if Circularity_Detected then
13370 raise Unrecoverable_Error;
13371 end if;
13372 end Load_Parent_Of_Generic;
13373
13374 ---------------------------------
13375 -- Map_Formal_Package_Entities --
13376 ---------------------------------
13377
13378 procedure Map_Formal_Package_Entities (Form : Entity_Id; Act : Entity_Id) is
13379 E1 : Entity_Id;
13380 E2 : Entity_Id;
13381
13382 begin
13383 Set_Instance_Of (Form, Act);
13384
13385 -- Traverse formal and actual package to map the corresponding entities.
13386 -- We skip over internal entities that may be generated during semantic
13387 -- analysis, and find the matching entities by name, given that they
13388 -- must appear in the same order.
13389
13390 E1 := First_Entity (Form);
13391 E2 := First_Entity (Act);
13392 while Present (E1) and then E1 /= First_Private_Entity (Form) loop
13393 -- Could this test be a single condition??? Seems like it could, and
13394 -- isn't FPE (Form) a constant anyway???
13395
13396 if not Is_Internal (E1)
13397 and then Present (Parent (E1))
13398 and then not Is_Class_Wide_Type (E1)
13399 and then not Is_Internal_Name (Chars (E1))
13400 then
13401 while Present (E2) and then Chars (E2) /= Chars (E1) loop
13402 Next_Entity (E2);
13403 end loop;
13404
13405 if No (E2) then
13406 exit;
13407 else
13408 Set_Instance_Of (E1, E2);
13409
13410 if Is_Type (E1) and then Is_Tagged_Type (E2) then
13411 Set_Instance_Of (Class_Wide_Type (E1), Class_Wide_Type (E2));
13412 end if;
13413
13414 if Is_Constrained (E1) then
13415 Set_Instance_Of (Base_Type (E1), Base_Type (E2));
13416 end if;
13417
13418 if Ekind (E1) = E_Package and then No (Renamed_Object (E1)) then
13419 Map_Formal_Package_Entities (E1, E2);
13420 end if;
13421 end if;
13422 end if;
13423
13424 Next_Entity (E1);
13425 end loop;
13426 end Map_Formal_Package_Entities;
13427
13428 -----------------------
13429 -- Move_Freeze_Nodes --
13430 -----------------------
13431
13432 procedure Move_Freeze_Nodes
13433 (Out_Of : Entity_Id;
13434 After : Node_Id;
13435 L : List_Id)
13436 is
13437 Decl : Node_Id;
13438 Next_Decl : Node_Id;
13439 Next_Node : Node_Id := After;
13440 Spec : Node_Id;
13441
13442 function Is_Outer_Type (T : Entity_Id) return Boolean;
13443 -- Check whether entity is declared in a scope external to that of the
13444 -- generic unit.
13445
13446 -------------------
13447 -- Is_Outer_Type --
13448 -------------------
13449
13450 function Is_Outer_Type (T : Entity_Id) return Boolean is
13451 Scop : Entity_Id := Scope (T);
13452
13453 begin
13454 if Scope_Depth (Scop) < Scope_Depth (Out_Of) then
13455 return True;
13456
13457 else
13458 while Scop /= Standard_Standard loop
13459 if Scop = Out_Of then
13460 return False;
13461 else
13462 Scop := Scope (Scop);
13463 end if;
13464 end loop;
13465
13466 return True;
13467 end if;
13468 end Is_Outer_Type;
13469
13470 -- Start of processing for Move_Freeze_Nodes
13471
13472 begin
13473 if No (L) then
13474 return;
13475 end if;
13476
13477 -- First remove the freeze nodes that may appear before all other
13478 -- declarations.
13479
13480 Decl := First (L);
13481 while Present (Decl)
13482 and then Nkind (Decl) = N_Freeze_Entity
13483 and then Is_Outer_Type (Entity (Decl))
13484 loop
13485 Decl := Remove_Head (L);
13486 Insert_After (Next_Node, Decl);
13487 Set_Analyzed (Decl, False);
13488 Next_Node := Decl;
13489 Decl := First (L);
13490 end loop;
13491
13492 -- Next scan the list of declarations and remove each freeze node that
13493 -- appears ahead of the current node.
13494
13495 while Present (Decl) loop
13496 while Present (Next (Decl))
13497 and then Nkind (Next (Decl)) = N_Freeze_Entity
13498 and then Is_Outer_Type (Entity (Next (Decl)))
13499 loop
13500 Next_Decl := Remove_Next (Decl);
13501 Insert_After (Next_Node, Next_Decl);
13502 Set_Analyzed (Next_Decl, False);
13503 Next_Node := Next_Decl;
13504 end loop;
13505
13506 -- If the declaration is a nested package or concurrent type, then
13507 -- recurse. Nested generic packages will have been processed from the
13508 -- inside out.
13509
13510 case Nkind (Decl) is
13511 when N_Package_Declaration =>
13512 Spec := Specification (Decl);
13513
13514 when N_Task_Type_Declaration =>
13515 Spec := Task_Definition (Decl);
13516
13517 when N_Protected_Type_Declaration =>
13518 Spec := Protected_Definition (Decl);
13519
13520 when others =>
13521 Spec := Empty;
13522 end case;
13523
13524 if Present (Spec) then
13525 Move_Freeze_Nodes (Out_Of, Next_Node, Visible_Declarations (Spec));
13526 Move_Freeze_Nodes (Out_Of, Next_Node, Private_Declarations (Spec));
13527 end if;
13528
13529 Next (Decl);
13530 end loop;
13531 end Move_Freeze_Nodes;
13532
13533 ----------------
13534 -- Next_Assoc --
13535 ----------------
13536
13537 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr is
13538 begin
13539 return Generic_Renamings.Table (E).Next_In_HTable;
13540 end Next_Assoc;
13541
13542 ------------------------
13543 -- Preanalyze_Actuals --
13544 ------------------------
13545
13546 procedure Preanalyze_Actuals (N : Node_Id; Inst : Entity_Id := Empty) is
13547 Assoc : Node_Id;
13548 Act : Node_Id;
13549 Errs : constant Nat := Serious_Errors_Detected;
13550
13551 Cur : Entity_Id := Empty;
13552 -- Current homograph of the instance name
13553
13554 Vis : Boolean;
13555 -- Saved visibility status of the current homograph
13556
13557 begin
13558 Assoc := First (Generic_Associations (N));
13559
13560 -- If the instance is a child unit, its name may hide an outer homonym,
13561 -- so make it invisible to perform name resolution on the actuals.
13562
13563 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name
13564 and then Present
13565 (Current_Entity (Defining_Identifier (Defining_Unit_Name (N))))
13566 then
13567 Cur := Current_Entity (Defining_Identifier (Defining_Unit_Name (N)));
13568
13569 if Is_Compilation_Unit (Cur) then
13570 Vis := Is_Immediately_Visible (Cur);
13571 Set_Is_Immediately_Visible (Cur, False);
13572 else
13573 Cur := Empty;
13574 end if;
13575 end if;
13576
13577 while Present (Assoc) loop
13578 if Nkind (Assoc) /= N_Others_Choice then
13579 Act := Explicit_Generic_Actual_Parameter (Assoc);
13580
13581 -- Within a nested instantiation, a defaulted actual is an empty
13582 -- association, so nothing to analyze. If the subprogram actual
13583 -- is an attribute, analyze prefix only, because actual is not a
13584 -- complete attribute reference.
13585
13586 -- If actual is an allocator, analyze expression only. The full
13587 -- analysis can generate code, and if instance is a compilation
13588 -- unit we have to wait until the package instance is installed
13589 -- to have a proper place to insert this code.
13590
13591 -- String literals may be operators, but at this point we do not
13592 -- know whether the actual is a formal subprogram or a string.
13593
13594 if No (Act) then
13595 null;
13596
13597 elsif Nkind (Act) = N_Attribute_Reference then
13598 Analyze (Prefix (Act));
13599
13600 elsif Nkind (Act) = N_Explicit_Dereference then
13601 Analyze (Prefix (Act));
13602
13603 elsif Nkind (Act) = N_Allocator then
13604 declare
13605 Expr : constant Node_Id := Expression (Act);
13606
13607 begin
13608 if Nkind (Expr) = N_Subtype_Indication then
13609 Analyze (Subtype_Mark (Expr));
13610
13611 -- Analyze separately each discriminant constraint, when
13612 -- given with a named association.
13613
13614 declare
13615 Constr : Node_Id;
13616
13617 begin
13618 Constr := First (Constraints (Constraint (Expr)));
13619 while Present (Constr) loop
13620 if Nkind (Constr) = N_Discriminant_Association then
13621 Analyze (Expression (Constr));
13622 else
13623 Analyze (Constr);
13624 end if;
13625
13626 Next (Constr);
13627 end loop;
13628 end;
13629
13630 else
13631 Analyze (Expr);
13632 end if;
13633 end;
13634
13635 elsif Nkind (Act) /= N_Operator_Symbol then
13636 Analyze (Act);
13637
13638 -- Within a package instance, mark actuals that are limited
13639 -- views, so their use can be moved to the body of the
13640 -- enclosing unit.
13641
13642 if Is_Entity_Name (Act)
13643 and then Is_Type (Entity (Act))
13644 and then From_Limited_With (Entity (Act))
13645 and then Present (Inst)
13646 then
13647 Append_Elmt (Entity (Act), Incomplete_Actuals (Inst));
13648 end if;
13649 end if;
13650
13651 if Errs /= Serious_Errors_Detected then
13652
13653 -- Do a minimal analysis of the generic, to prevent spurious
13654 -- warnings complaining about the generic being unreferenced,
13655 -- before abandoning the instantiation.
13656
13657 Analyze (Name (N));
13658
13659 if Is_Entity_Name (Name (N))
13660 and then Etype (Name (N)) /= Any_Type
13661 then
13662 Generate_Reference (Entity (Name (N)), Name (N));
13663 Set_Is_Instantiated (Entity (Name (N)));
13664 end if;
13665
13666 if Present (Cur) then
13667
13668 -- For the case of a child instance hiding an outer homonym,
13669 -- provide additional warning which might explain the error.
13670
13671 Set_Is_Immediately_Visible (Cur, Vis);
13672 Error_Msg_NE
13673 ("& hides outer unit with the same name??",
13674 N, Defining_Unit_Name (N));
13675 end if;
13676
13677 Abandon_Instantiation (Act);
13678 end if;
13679 end if;
13680
13681 Next (Assoc);
13682 end loop;
13683
13684 if Present (Cur) then
13685 Set_Is_Immediately_Visible (Cur, Vis);
13686 end if;
13687 end Preanalyze_Actuals;
13688
13689 -------------------
13690 -- Remove_Parent --
13691 -------------------
13692
13693 procedure Remove_Parent (In_Body : Boolean := False) is
13694 S : Entity_Id := Current_Scope;
13695 -- S is the scope containing the instantiation just completed. The scope
13696 -- stack contains the parent instances of the instantiation, followed by
13697 -- the original S.
13698
13699 Cur_P : Entity_Id;
13700 E : Entity_Id;
13701 P : Entity_Id;
13702 Hidden : Elmt_Id;
13703
13704 begin
13705 -- After child instantiation is complete, remove from scope stack the
13706 -- extra copy of the current scope, and then remove parent instances.
13707
13708 if not In_Body then
13709 Pop_Scope;
13710
13711 while Current_Scope /= S loop
13712 P := Current_Scope;
13713 End_Package_Scope (Current_Scope);
13714
13715 if In_Open_Scopes (P) then
13716 E := First_Entity (P);
13717 while Present (E) loop
13718 Set_Is_Immediately_Visible (E, True);
13719 Next_Entity (E);
13720 end loop;
13721
13722 -- If instantiation is declared in a block, it is the enclosing
13723 -- scope that might be a parent instance. Note that only one
13724 -- block can be involved, because the parent instances have
13725 -- been installed within it.
13726
13727 if Ekind (P) = E_Block then
13728 Cur_P := Scope (P);
13729 else
13730 Cur_P := P;
13731 end if;
13732
13733 if Is_Generic_Instance (Cur_P) and then P /= Current_Scope then
13734 -- We are within an instance of some sibling. Retain
13735 -- visibility of parent, for proper subsequent cleanup, and
13736 -- reinstall private declarations as well.
13737
13738 Set_In_Private_Part (P);
13739 Install_Private_Declarations (P);
13740 end if;
13741
13742 -- If the ultimate parent is a top-level unit recorded in
13743 -- Instance_Parent_Unit, then reset its visibility to what it was
13744 -- before instantiation. (It's not clear what the purpose is of
13745 -- testing whether Scope (P) is In_Open_Scopes, but that test was
13746 -- present before the ultimate parent test was added.???)
13747
13748 elsif not In_Open_Scopes (Scope (P))
13749 or else (P = Instance_Parent_Unit
13750 and then not Parent_Unit_Visible)
13751 then
13752 Set_Is_Immediately_Visible (P, False);
13753
13754 -- If the current scope is itself an instantiation of a generic
13755 -- nested within P, and we are in the private part of body of this
13756 -- instantiation, restore the full views of P, that were removed
13757 -- in End_Package_Scope above. This obscure case can occur when a
13758 -- subunit of a generic contains an instance of a child unit of
13759 -- its generic parent unit.
13760
13761 elsif S = Current_Scope and then Is_Generic_Instance (S) then
13762 declare
13763 Par : constant Entity_Id :=
13764 Generic_Parent (Package_Specification (S));
13765 begin
13766 if Present (Par)
13767 and then P = Scope (Par)
13768 and then (In_Package_Body (S) or else In_Private_Part (S))
13769 then
13770 Set_In_Private_Part (P);
13771 Install_Private_Declarations (P);
13772 end if;
13773 end;
13774 end if;
13775 end loop;
13776
13777 -- Reset visibility of entities in the enclosing scope
13778
13779 Set_Is_Hidden_Open_Scope (Current_Scope, False);
13780
13781 Hidden := First_Elmt (Hidden_Entities);
13782 while Present (Hidden) loop
13783 Set_Is_Immediately_Visible (Node (Hidden), True);
13784 Next_Elmt (Hidden);
13785 end loop;
13786
13787 else
13788 -- Each body is analyzed separately, and there is no context that
13789 -- needs preserving from one body instance to the next, so remove all
13790 -- parent scopes that have been installed.
13791
13792 while Present (S) loop
13793 End_Package_Scope (S);
13794 Set_Is_Immediately_Visible (S, False);
13795 S := Current_Scope;
13796 exit when S = Standard_Standard;
13797 end loop;
13798 end if;
13799 end Remove_Parent;
13800
13801 -----------------
13802 -- Restore_Env --
13803 -----------------
13804
13805 procedure Restore_Env is
13806 Saved : Instance_Env renames Instance_Envs.Table (Instance_Envs.Last);
13807
13808 begin
13809 if No (Current_Instantiated_Parent.Act_Id) then
13810 -- Restore environment after subprogram inlining
13811
13812 Restore_Private_Views (Empty);
13813 end if;
13814
13815 Current_Instantiated_Parent := Saved.Instantiated_Parent;
13816 Exchanged_Views := Saved.Exchanged_Views;
13817 Hidden_Entities := Saved.Hidden_Entities;
13818 Current_Sem_Unit := Saved.Current_Sem_Unit;
13819 Parent_Unit_Visible := Saved.Parent_Unit_Visible;
13820 Instance_Parent_Unit := Saved.Instance_Parent_Unit;
13821
13822 Restore_Opt_Config_Switches (Saved.Switches);
13823
13824 Instance_Envs.Decrement_Last;
13825 end Restore_Env;
13826
13827 ---------------------------
13828 -- Restore_Private_Views --
13829 ---------------------------
13830
13831 procedure Restore_Private_Views
13832 (Pack_Id : Entity_Id;
13833 Is_Package : Boolean := True)
13834 is
13835 M : Elmt_Id;
13836 E : Entity_Id;
13837 Typ : Entity_Id;
13838 Dep_Elmt : Elmt_Id;
13839 Dep_Typ : Node_Id;
13840
13841 procedure Restore_Nested_Formal (Formal : Entity_Id);
13842 -- Hide the generic formals of formal packages declared with box which
13843 -- were reachable in the current instantiation.
13844
13845 ---------------------------
13846 -- Restore_Nested_Formal --
13847 ---------------------------
13848
13849 procedure Restore_Nested_Formal (Formal : Entity_Id) is
13850 Ent : Entity_Id;
13851
13852 begin
13853 if Present (Renamed_Object (Formal))
13854 and then Denotes_Formal_Package (Renamed_Object (Formal), True)
13855 then
13856 return;
13857
13858 elsif Present (Associated_Formal_Package (Formal)) then
13859 Ent := First_Entity (Formal);
13860 while Present (Ent) loop
13861 exit when Ekind (Ent) = E_Package
13862 and then Renamed_Entity (Ent) = Renamed_Entity (Formal);
13863
13864 Set_Is_Hidden (Ent);
13865 Set_Is_Potentially_Use_Visible (Ent, False);
13866
13867 -- If package, then recurse
13868
13869 if Ekind (Ent) = E_Package then
13870 Restore_Nested_Formal (Ent);
13871 end if;
13872
13873 Next_Entity (Ent);
13874 end loop;
13875 end if;
13876 end Restore_Nested_Formal;
13877
13878 -- Start of processing for Restore_Private_Views
13879
13880 begin
13881 M := First_Elmt (Exchanged_Views);
13882 while Present (M) loop
13883 Typ := Node (M);
13884
13885 -- Subtypes of types whose views have been exchanged, and that are
13886 -- defined within the instance, were not on the Private_Dependents
13887 -- list on entry to the instance, so they have to be exchanged
13888 -- explicitly now, in order to remain consistent with the view of the
13889 -- parent type.
13890
13891 if Ekind_In (Typ, E_Private_Type,
13892 E_Limited_Private_Type,
13893 E_Record_Type_With_Private)
13894 then
13895 Dep_Elmt := First_Elmt (Private_Dependents (Typ));
13896 while Present (Dep_Elmt) loop
13897 Dep_Typ := Node (Dep_Elmt);
13898
13899 if Scope (Dep_Typ) = Pack_Id
13900 and then Present (Full_View (Dep_Typ))
13901 then
13902 Replace_Elmt (Dep_Elmt, Full_View (Dep_Typ));
13903 Exchange_Declarations (Dep_Typ);
13904 end if;
13905
13906 Next_Elmt (Dep_Elmt);
13907 end loop;
13908 end if;
13909
13910 Exchange_Declarations (Node (M));
13911 Next_Elmt (M);
13912 end loop;
13913
13914 if No (Pack_Id) then
13915 return;
13916 end if;
13917
13918 -- Make the generic formal parameters private, and make the formal types
13919 -- into subtypes of the actuals again.
13920
13921 E := First_Entity (Pack_Id);
13922 while Present (E) loop
13923 Set_Is_Hidden (E, True);
13924
13925 if Is_Type (E)
13926 and then Nkind (Parent (E)) = N_Subtype_Declaration
13927 then
13928 -- If the actual for E is itself a generic actual type from
13929 -- an enclosing instance, E is still a generic actual type
13930 -- outside of the current instance. This matter when resolving
13931 -- an overloaded call that may be ambiguous in the enclosing
13932 -- instance, when two of its actuals coincide.
13933
13934 if Is_Entity_Name (Subtype_Indication (Parent (E)))
13935 and then Is_Generic_Actual_Type
13936 (Entity (Subtype_Indication (Parent (E))))
13937 then
13938 null;
13939 else
13940 Set_Is_Generic_Actual_Type (E, False);
13941 end if;
13942
13943 -- An unusual case of aliasing: the actual may also be directly
13944 -- visible in the generic, and be private there, while it is fully
13945 -- visible in the context of the instance. The internal subtype
13946 -- is private in the instance but has full visibility like its
13947 -- parent in the enclosing scope. This enforces the invariant that
13948 -- the privacy status of all private dependents of a type coincide
13949 -- with that of the parent type. This can only happen when a
13950 -- generic child unit is instantiated within a sibling.
13951
13952 if Is_Private_Type (E)
13953 and then not Is_Private_Type (Etype (E))
13954 then
13955 Exchange_Declarations (E);
13956 end if;
13957
13958 elsif Ekind (E) = E_Package then
13959
13960 -- The end of the renaming list is the renaming of the generic
13961 -- package itself. If the instance is a subprogram, all entities
13962 -- in the corresponding package are renamings. If this entity is
13963 -- a formal package, make its own formals private as well. The
13964 -- actual in this case is itself the renaming of an instantiation.
13965 -- If the entity is not a package renaming, it is the entity
13966 -- created to validate formal package actuals: ignore it.
13967
13968 -- If the actual is itself a formal package for the enclosing
13969 -- generic, or the actual for such a formal package, it remains
13970 -- visible on exit from the instance, and therefore nothing needs
13971 -- to be done either, except to keep it accessible.
13972
13973 if Is_Package and then Renamed_Object (E) = Pack_Id then
13974 exit;
13975
13976 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
13977 null;
13978
13979 elsif
13980 Denotes_Formal_Package (Renamed_Object (E), True, Pack_Id)
13981 then
13982 Set_Is_Hidden (E, False);
13983
13984 else
13985 declare
13986 Act_P : constant Entity_Id := Renamed_Object (E);
13987 Id : Entity_Id;
13988
13989 begin
13990 Id := First_Entity (Act_P);
13991 while Present (Id)
13992 and then Id /= First_Private_Entity (Act_P)
13993 loop
13994 exit when Ekind (Id) = E_Package
13995 and then Renamed_Object (Id) = Act_P;
13996
13997 Set_Is_Hidden (Id, True);
13998 Set_Is_Potentially_Use_Visible (Id, In_Use (Act_P));
13999
14000 if Ekind (Id) = E_Package then
14001 Restore_Nested_Formal (Id);
14002 end if;
14003
14004 Next_Entity (Id);
14005 end loop;
14006 end;
14007 end if;
14008 end if;
14009
14010 Next_Entity (E);
14011 end loop;
14012 end Restore_Private_Views;
14013
14014 --------------
14015 -- Save_Env --
14016 --------------
14017
14018 procedure Save_Env
14019 (Gen_Unit : Entity_Id;
14020 Act_Unit : Entity_Id)
14021 is
14022 begin
14023 Init_Env;
14024 Set_Instance_Env (Gen_Unit, Act_Unit);
14025 end Save_Env;
14026
14027 ----------------------------
14028 -- Save_Global_References --
14029 ----------------------------
14030
14031 procedure Save_Global_References (Templ : Node_Id) is
14032
14033 -- ??? it is horrible to use global variables in highly recursive code
14034
14035 E : Entity_Id;
14036 -- The entity of the current associated node
14037
14038 Gen_Scope : Entity_Id;
14039 -- The scope of the generic for which references are being saved
14040
14041 N2 : Node_Id;
14042 -- The current associated node
14043
14044 function Is_Global (E : Entity_Id) return Boolean;
14045 -- Check whether entity is defined outside of generic unit. Examine the
14046 -- scope of an entity, and the scope of the scope, etc, until we find
14047 -- either Standard, in which case the entity is global, or the generic
14048 -- unit itself, which indicates that the entity is local. If the entity
14049 -- is the generic unit itself, as in the case of a recursive call, or
14050 -- the enclosing generic unit, if different from the current scope, then
14051 -- it is local as well, because it will be replaced at the point of
14052 -- instantiation. On the other hand, if it is a reference to a child
14053 -- unit of a common ancestor, which appears in an instantiation, it is
14054 -- global because it is used to denote a specific compilation unit at
14055 -- the time the instantiations will be analyzed.
14056
14057 procedure Qualify_Universal_Operands
14058 (Op : Node_Id;
14059 Func_Call : Node_Id);
14060 -- Op denotes a binary or unary operator in generic template Templ. Node
14061 -- Func_Call is the function call alternative of the operator within the
14062 -- the analyzed copy of the template. Change each operand which yields a
14063 -- universal type by wrapping it into a qualified expression
14064 --
14065 -- Actual_Typ'(Operand)
14066 --
14067 -- where Actual_Typ is the type of corresponding actual parameter of
14068 -- Operand in Func_Call.
14069
14070 procedure Reset_Entity (N : Node_Id);
14071 -- Save semantic information on global entity so that it is not resolved
14072 -- again at instantiation time.
14073
14074 procedure Save_Entity_Descendants (N : Node_Id);
14075 -- Apply Save_Global_References to the two syntactic descendants of
14076 -- non-terminal nodes that carry an Associated_Node and are processed
14077 -- through Reset_Entity. Once the global entity (if any) has been
14078 -- captured together with its type, only two syntactic descendants need
14079 -- to be traversed to complete the processing of the tree rooted at N.
14080 -- This applies to Selected_Components, Expanded_Names, and to Operator
14081 -- nodes. N can also be a character literal, identifier, or operator
14082 -- symbol node, but the call has no effect in these cases.
14083
14084 procedure Save_Global_Defaults (N1 : Node_Id; N2 : Node_Id);
14085 -- Default actuals in nested instances must be handled specially
14086 -- because there is no link to them from the original tree. When an
14087 -- actual subprogram is given by a default, we add an explicit generic
14088 -- association for it in the instantiation node. When we save the
14089 -- global references on the name of the instance, we recover the list
14090 -- of generic associations, and add an explicit one to the original
14091 -- generic tree, through which a global actual can be preserved.
14092 -- Similarly, if a child unit is instantiated within a sibling, in the
14093 -- context of the parent, we must preserve the identifier of the parent
14094 -- so that it can be properly resolved in a subsequent instantiation.
14095
14096 procedure Save_Global_Descendant (D : Union_Id);
14097 -- Apply Save_References recursively to the descendants of node D
14098
14099 procedure Save_References (N : Node_Id);
14100 -- This is the recursive procedure that does the work, once the
14101 -- enclosing generic scope has been established.
14102
14103 ---------------
14104 -- Is_Global --
14105 ---------------
14106
14107 function Is_Global (E : Entity_Id) return Boolean is
14108 Se : Entity_Id;
14109
14110 function Is_Instance_Node (Decl : Node_Id) return Boolean;
14111 -- Determine whether the parent node of a reference to a child unit
14112 -- denotes an instantiation or a formal package, in which case the
14113 -- reference to the child unit is global, even if it appears within
14114 -- the current scope (e.g. when the instance appears within the body
14115 -- of an ancestor).
14116
14117 ----------------------
14118 -- Is_Instance_Node --
14119 ----------------------
14120
14121 function Is_Instance_Node (Decl : Node_Id) return Boolean is
14122 begin
14123 return Nkind (Decl) in N_Generic_Instantiation
14124 or else
14125 Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration;
14126 end Is_Instance_Node;
14127
14128 -- Start of processing for Is_Global
14129
14130 begin
14131 if E = Gen_Scope then
14132 return False;
14133
14134 elsif E = Standard_Standard then
14135 return True;
14136
14137 elsif Is_Child_Unit (E)
14138 and then (Is_Instance_Node (Parent (N2))
14139 or else (Nkind (Parent (N2)) = N_Expanded_Name
14140 and then N2 = Selector_Name (Parent (N2))
14141 and then
14142 Is_Instance_Node (Parent (Parent (N2)))))
14143 then
14144 return True;
14145
14146 else
14147 Se := Scope (E);
14148 while Se /= Gen_Scope loop
14149 if Se = Standard_Standard then
14150 return True;
14151 else
14152 Se := Scope (Se);
14153 end if;
14154 end loop;
14155
14156 return False;
14157 end if;
14158 end Is_Global;
14159
14160 --------------------------------
14161 -- Qualify_Universal_Operands --
14162 --------------------------------
14163
14164 procedure Qualify_Universal_Operands
14165 (Op : Node_Id;
14166 Func_Call : Node_Id)
14167 is
14168 procedure Qualify_Operand (Opnd : Node_Id; Actual : Node_Id);
14169 -- Rewrite operand Opnd as a qualified expression of the form
14170 --
14171 -- Actual_Typ'(Opnd)
14172 --
14173 -- where Actual is the corresponding actual parameter of Opnd in
14174 -- function call Func_Call.
14175
14176 function Qualify_Type
14177 (Loc : Source_Ptr;
14178 Typ : Entity_Id) return Node_Id;
14179 -- Qualify type Typ by creating a selected component of the form
14180 --
14181 -- Scope_Of_Typ.Typ
14182
14183 ---------------------
14184 -- Qualify_Operand --
14185 ---------------------
14186
14187 procedure Qualify_Operand (Opnd : Node_Id; Actual : Node_Id) is
14188 Loc : constant Source_Ptr := Sloc (Opnd);
14189 Typ : constant Entity_Id := Etype (Actual);
14190 Mark : Node_Id;
14191 Qual : Node_Id;
14192
14193 begin
14194 -- Qualify the operand when it is of a universal type. Note that
14195 -- the template is unanalyzed and it is not possible to directly
14196 -- query the type. This transformation is not done when the type
14197 -- of the actual is internally generated because the type will be
14198 -- regenerated in the instance.
14199
14200 if Yields_Universal_Type (Opnd)
14201 and then Comes_From_Source (Typ)
14202 and then not Is_Hidden (Typ)
14203 then
14204 -- The type of the actual may be a global reference. Save this
14205 -- information by creating a reference to it.
14206
14207 if Is_Global (Typ) then
14208 Mark := New_Occurrence_Of (Typ, Loc);
14209
14210 -- Otherwise rely on resolution to find the proper type within
14211 -- the instance.
14212
14213 else
14214 Mark := Qualify_Type (Loc, Typ);
14215 end if;
14216
14217 Qual :=
14218 Make_Qualified_Expression (Loc,
14219 Subtype_Mark => Mark,
14220 Expression => Relocate_Node (Opnd));
14221
14222 -- Mark the qualification to distinguish it from other source
14223 -- constructs and signal the instantiation mechanism that this
14224 -- node requires special processing. See Copy_Generic_Node for
14225 -- details.
14226
14227 Set_Is_Qualified_Universal_Literal (Qual);
14228
14229 Rewrite (Opnd, Qual);
14230 end if;
14231 end Qualify_Operand;
14232
14233 ------------------
14234 -- Qualify_Type --
14235 ------------------
14236
14237 function Qualify_Type
14238 (Loc : Source_Ptr;
14239 Typ : Entity_Id) return Node_Id
14240 is
14241 Scop : constant Entity_Id := Scope (Typ);
14242 Result : Node_Id;
14243
14244 begin
14245 Result := Make_Identifier (Loc, Chars (Typ));
14246
14247 if Present (Scop) and then not Is_Generic_Unit (Scop) then
14248 Result :=
14249 Make_Selected_Component (Loc,
14250 Prefix => Make_Identifier (Loc, Chars (Scop)),
14251 Selector_Name => Result);
14252 end if;
14253
14254 return Result;
14255 end Qualify_Type;
14256
14257 -- Local variables
14258
14259 Actuals : constant List_Id := Parameter_Associations (Func_Call);
14260
14261 -- Start of processing for Qualify_Universal_Operands
14262
14263 begin
14264 if Nkind (Op) in N_Binary_Op then
14265 Qualify_Operand (Left_Opnd (Op), First (Actuals));
14266 Qualify_Operand (Right_Opnd (Op), Next (First (Actuals)));
14267
14268 elsif Nkind (Op) in N_Unary_Op then
14269 Qualify_Operand (Right_Opnd (Op), First (Actuals));
14270 end if;
14271 end Qualify_Universal_Operands;
14272
14273 ------------------
14274 -- Reset_Entity --
14275 ------------------
14276
14277 procedure Reset_Entity (N : Node_Id) is
14278 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id);
14279 -- If the type of N2 is global to the generic unit, save the type in
14280 -- the generic node. Just as we perform name capture for explicit
14281 -- references within the generic, we must capture the global types
14282 -- of local entities because they may participate in resolution in
14283 -- the instance.
14284
14285 function Top_Ancestor (E : Entity_Id) return Entity_Id;
14286 -- Find the ultimate ancestor of the current unit. If it is not a
14287 -- generic unit, then the name of the current unit in the prefix of
14288 -- an expanded name must be replaced with its generic homonym to
14289 -- ensure that it will be properly resolved in an instance.
14290
14291 ---------------------
14292 -- Set_Global_Type --
14293 ---------------------
14294
14295 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id) is
14296 Typ : constant Entity_Id := Etype (N2);
14297
14298 begin
14299 Set_Etype (N, Typ);
14300
14301 -- If the entity of N is not the associated node, this is a
14302 -- nested generic and it has an associated node as well, whose
14303 -- type is already the full view (see below). Indicate that the
14304 -- original node has a private view.
14305
14306 if Entity (N) /= N2 and then Has_Private_View (Entity (N)) then
14307 Set_Has_Private_View (N);
14308 end if;
14309
14310 -- If not a private type, nothing else to do
14311
14312 if not Is_Private_Type (Typ) then
14313 if Is_Array_Type (Typ)
14314 and then Is_Private_Type (Component_Type (Typ))
14315 then
14316 Set_Has_Private_View (N);
14317 end if;
14318
14319 -- If it is a derivation of a private type in a context where no
14320 -- full view is needed, nothing to do either.
14321
14322 elsif No (Full_View (Typ)) and then Typ /= Etype (Typ) then
14323 null;
14324
14325 -- Otherwise mark the type for flipping and use the full view when
14326 -- available.
14327
14328 else
14329 Set_Has_Private_View (N);
14330
14331 if Present (Full_View (Typ)) then
14332 Set_Etype (N2, Full_View (Typ));
14333 end if;
14334 end if;
14335
14336 if Is_Floating_Point_Type (Typ)
14337 and then Has_Dimension_System (Typ)
14338 then
14339 Copy_Dimensions (N2, N);
14340 end if;
14341 end Set_Global_Type;
14342
14343 ------------------
14344 -- Top_Ancestor --
14345 ------------------
14346
14347 function Top_Ancestor (E : Entity_Id) return Entity_Id is
14348 Par : Entity_Id;
14349
14350 begin
14351 Par := E;
14352 while Is_Child_Unit (Par) loop
14353 Par := Scope (Par);
14354 end loop;
14355
14356 return Par;
14357 end Top_Ancestor;
14358
14359 -- Start of processing for Reset_Entity
14360
14361 begin
14362 N2 := Get_Associated_Node (N);
14363 E := Entity (N2);
14364
14365 if Present (E) then
14366
14367 -- If the node is an entry call to an entry in an enclosing task,
14368 -- it is rewritten as a selected component. No global entity to
14369 -- preserve in this case, since the expansion will be redone in
14370 -- the instance.
14371
14372 if not Nkind_In (E, N_Defining_Character_Literal,
14373 N_Defining_Identifier,
14374 N_Defining_Operator_Symbol)
14375 then
14376 Set_Associated_Node (N, Empty);
14377 Set_Etype (N, Empty);
14378 return;
14379 end if;
14380
14381 -- If the entity is an itype created as a subtype of an access
14382 -- type with a null exclusion restore source entity for proper
14383 -- visibility. The itype will be created anew in the instance.
14384
14385 if Is_Itype (E)
14386 and then Ekind (E) = E_Access_Subtype
14387 and then Is_Entity_Name (N)
14388 and then Chars (Etype (E)) = Chars (N)
14389 then
14390 E := Etype (E);
14391 Set_Entity (N2, E);
14392 Set_Etype (N2, E);
14393 end if;
14394
14395 if Is_Global (E) then
14396
14397 -- If the entity is a package renaming that is the prefix of
14398 -- an expanded name, it has been rewritten as the renamed
14399 -- package, which is necessary semantically but complicates
14400 -- ASIS tree traversal, so we recover the original entity to
14401 -- expose the renaming. Take into account that the context may
14402 -- be a nested generic, that the original node may itself have
14403 -- an associated node that had better be an entity, and that
14404 -- the current node is still a selected component.
14405
14406 if Ekind (E) = E_Package
14407 and then Nkind (N) = N_Selected_Component
14408 and then Nkind (Parent (N)) = N_Expanded_Name
14409 and then Present (Original_Node (N2))
14410 and then Is_Entity_Name (Original_Node (N2))
14411 and then Present (Entity (Original_Node (N2)))
14412 then
14413 if Is_Global (Entity (Original_Node (N2))) then
14414 N2 := Original_Node (N2);
14415 Set_Associated_Node (N, N2);
14416 Set_Global_Type (N, N2);
14417
14418 -- Renaming is local, and will be resolved in instance
14419
14420 else
14421 Set_Associated_Node (N, Empty);
14422 Set_Etype (N, Empty);
14423 end if;
14424
14425 else
14426 Set_Global_Type (N, N2);
14427 end if;
14428
14429 elsif Nkind (N) = N_Op_Concat
14430 and then Is_Generic_Type (Etype (N2))
14431 and then (Base_Type (Etype (Right_Opnd (N2))) = Etype (N2)
14432 or else
14433 Base_Type (Etype (Left_Opnd (N2))) = Etype (N2))
14434 and then Is_Intrinsic_Subprogram (E)
14435 then
14436 null;
14437
14438 -- Entity is local. Mark generic node as unresolved. Note that now
14439 -- it does not have an entity.
14440
14441 else
14442 Set_Associated_Node (N, Empty);
14443 Set_Etype (N, Empty);
14444 end if;
14445
14446 if Nkind (Parent (N)) in N_Generic_Instantiation
14447 and then N = Name (Parent (N))
14448 then
14449 Save_Global_Defaults (Parent (N), Parent (N2));
14450 end if;
14451
14452 elsif Nkind (Parent (N)) = N_Selected_Component
14453 and then Nkind (Parent (N2)) = N_Expanded_Name
14454 then
14455 if Is_Global (Entity (Parent (N2))) then
14456 Change_Selected_Component_To_Expanded_Name (Parent (N));
14457 Set_Associated_Node (Parent (N), Parent (N2));
14458 Set_Global_Type (Parent (N), Parent (N2));
14459 Save_Entity_Descendants (N);
14460
14461 -- If this is a reference to the current generic entity, replace
14462 -- by the name of the generic homonym of the current package. This
14463 -- is because in an instantiation Par.P.Q will not resolve to the
14464 -- name of the instance, whose enclosing scope is not necessarily
14465 -- Par. We use the generic homonym rather that the name of the
14466 -- generic itself because it may be hidden by a local declaration.
14467
14468 elsif In_Open_Scopes (Entity (Parent (N2)))
14469 and then not
14470 Is_Generic_Unit (Top_Ancestor (Entity (Prefix (Parent (N2)))))
14471 then
14472 if Ekind (Entity (Parent (N2))) = E_Generic_Package then
14473 Rewrite (Parent (N),
14474 Make_Identifier (Sloc (N),
14475 Chars =>
14476 Chars (Generic_Homonym (Entity (Parent (N2))))));
14477 else
14478 Rewrite (Parent (N),
14479 Make_Identifier (Sloc (N),
14480 Chars => Chars (Selector_Name (Parent (N2)))));
14481 end if;
14482 end if;
14483
14484 if Nkind (Parent (Parent (N))) in N_Generic_Instantiation
14485 and then Parent (N) = Name (Parent (Parent (N)))
14486 then
14487 Save_Global_Defaults
14488 (Parent (Parent (N)), Parent (Parent (N2)));
14489 end if;
14490
14491 -- A selected component may denote a static constant that has been
14492 -- folded. If the static constant is global to the generic, capture
14493 -- its value. Otherwise the folding will happen in any instantiation.
14494
14495 elsif Nkind (Parent (N)) = N_Selected_Component
14496 and then Nkind_In (Parent (N2), N_Integer_Literal, N_Real_Literal)
14497 then
14498 if Present (Entity (Original_Node (Parent (N2))))
14499 and then Is_Global (Entity (Original_Node (Parent (N2))))
14500 then
14501 Rewrite (Parent (N), New_Copy (Parent (N2)));
14502 Set_Analyzed (Parent (N), False);
14503 end if;
14504
14505 -- A selected component may be transformed into a parameterless
14506 -- function call. If the called entity is global, rewrite the node
14507 -- appropriately, i.e. as an extended name for the global entity.
14508
14509 elsif Nkind (Parent (N)) = N_Selected_Component
14510 and then Nkind (Parent (N2)) = N_Function_Call
14511 and then N = Selector_Name (Parent (N))
14512 then
14513 if No (Parameter_Associations (Parent (N2))) then
14514 if Is_Global (Entity (Name (Parent (N2)))) then
14515 Change_Selected_Component_To_Expanded_Name (Parent (N));
14516 Set_Associated_Node (Parent (N), Name (Parent (N2)));
14517 Set_Global_Type (Parent (N), Name (Parent (N2)));
14518 Save_Entity_Descendants (N);
14519
14520 else
14521 Set_Is_Prefixed_Call (Parent (N));
14522 Set_Associated_Node (N, Empty);
14523 Set_Etype (N, Empty);
14524 end if;
14525
14526 -- In Ada 2005, X.F may be a call to a primitive operation,
14527 -- rewritten as F (X). This rewriting will be done again in an
14528 -- instance, so keep the original node. Global entities will be
14529 -- captured as for other constructs. Indicate that this must
14530 -- resolve as a call, to prevent accidental overloading in the
14531 -- instance, if both a component and a primitive operation appear
14532 -- as candidates.
14533
14534 else
14535 Set_Is_Prefixed_Call (Parent (N));
14536 end if;
14537
14538 -- Entity is local. Reset in generic unit, so that node is resolved
14539 -- anew at the point of instantiation.
14540
14541 else
14542 Set_Associated_Node (N, Empty);
14543 Set_Etype (N, Empty);
14544 end if;
14545 end Reset_Entity;
14546
14547 -----------------------------
14548 -- Save_Entity_Descendants --
14549 -----------------------------
14550
14551 procedure Save_Entity_Descendants (N : Node_Id) is
14552 begin
14553 case Nkind (N) is
14554 when N_Binary_Op =>
14555 Save_Global_Descendant (Union_Id (Left_Opnd (N)));
14556 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
14557
14558 when N_Unary_Op =>
14559 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
14560
14561 when N_Expanded_Name
14562 | N_Selected_Component
14563 =>
14564 Save_Global_Descendant (Union_Id (Prefix (N)));
14565 Save_Global_Descendant (Union_Id (Selector_Name (N)));
14566
14567 when N_Character_Literal
14568 | N_Identifier
14569 | N_Operator_Symbol
14570 =>
14571 null;
14572
14573 when others =>
14574 raise Program_Error;
14575 end case;
14576 end Save_Entity_Descendants;
14577
14578 --------------------------
14579 -- Save_Global_Defaults --
14580 --------------------------
14581
14582 procedure Save_Global_Defaults (N1 : Node_Id; N2 : Node_Id) is
14583 Loc : constant Source_Ptr := Sloc (N1);
14584 Assoc2 : constant List_Id := Generic_Associations (N2);
14585 Gen_Id : constant Entity_Id := Get_Generic_Entity (N2);
14586 Assoc1 : List_Id;
14587 Act1 : Node_Id;
14588 Act2 : Node_Id;
14589 Def : Node_Id;
14590 Ndec : Node_Id;
14591 Subp : Entity_Id;
14592 Actual : Entity_Id;
14593
14594 begin
14595 Assoc1 := Generic_Associations (N1);
14596
14597 if Present (Assoc1) then
14598 Act1 := First (Assoc1);
14599 else
14600 Act1 := Empty;
14601 Set_Generic_Associations (N1, New_List);
14602 Assoc1 := Generic_Associations (N1);
14603 end if;
14604
14605 if Present (Assoc2) then
14606 Act2 := First (Assoc2);
14607 else
14608 return;
14609 end if;
14610
14611 while Present (Act1) and then Present (Act2) loop
14612 Next (Act1);
14613 Next (Act2);
14614 end loop;
14615
14616 -- Find the associations added for default subprograms
14617
14618 if Present (Act2) then
14619 while Nkind (Act2) /= N_Generic_Association
14620 or else No (Entity (Selector_Name (Act2)))
14621 or else not Is_Overloadable (Entity (Selector_Name (Act2)))
14622 loop
14623 Next (Act2);
14624 end loop;
14625
14626 -- Add a similar association if the default is global. The
14627 -- renaming declaration for the actual has been analyzed, and
14628 -- its alias is the program it renames. Link the actual in the
14629 -- original generic tree with the node in the analyzed tree.
14630
14631 while Present (Act2) loop
14632 Subp := Entity (Selector_Name (Act2));
14633 Def := Explicit_Generic_Actual_Parameter (Act2);
14634
14635 -- Following test is defence against rubbish errors
14636
14637 if No (Alias (Subp)) then
14638 return;
14639 end if;
14640
14641 -- Retrieve the resolved actual from the renaming declaration
14642 -- created for the instantiated formal.
14643
14644 Actual := Entity (Name (Parent (Parent (Subp))));
14645 Set_Entity (Def, Actual);
14646 Set_Etype (Def, Etype (Actual));
14647
14648 if Is_Global (Actual) then
14649 Ndec :=
14650 Make_Generic_Association (Loc,
14651 Selector_Name =>
14652 New_Occurrence_Of (Subp, Loc),
14653 Explicit_Generic_Actual_Parameter =>
14654 New_Occurrence_Of (Actual, Loc));
14655
14656 Set_Associated_Node
14657 (Explicit_Generic_Actual_Parameter (Ndec), Def);
14658
14659 Append (Ndec, Assoc1);
14660
14661 -- If there are other defaults, add a dummy association in case
14662 -- there are other defaulted formals with the same name.
14663
14664 elsif Present (Next (Act2)) then
14665 Ndec :=
14666 Make_Generic_Association (Loc,
14667 Selector_Name =>
14668 New_Occurrence_Of (Subp, Loc),
14669 Explicit_Generic_Actual_Parameter => Empty);
14670
14671 Append (Ndec, Assoc1);
14672 end if;
14673
14674 Next (Act2);
14675 end loop;
14676 end if;
14677
14678 if Nkind (Name (N1)) = N_Identifier
14679 and then Is_Child_Unit (Gen_Id)
14680 and then Is_Global (Gen_Id)
14681 and then Is_Generic_Unit (Scope (Gen_Id))
14682 and then In_Open_Scopes (Scope (Gen_Id))
14683 then
14684 -- This is an instantiation of a child unit within a sibling, so
14685 -- that the generic parent is in scope. An eventual instance must
14686 -- occur within the scope of an instance of the parent. Make name
14687 -- in instance into an expanded name, to preserve the identifier
14688 -- of the parent, so it can be resolved subsequently.
14689
14690 Rewrite (Name (N2),
14691 Make_Expanded_Name (Loc,
14692 Chars => Chars (Gen_Id),
14693 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
14694 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
14695 Set_Entity (Name (N2), Gen_Id);
14696
14697 Rewrite (Name (N1),
14698 Make_Expanded_Name (Loc,
14699 Chars => Chars (Gen_Id),
14700 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
14701 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
14702
14703 Set_Associated_Node (Name (N1), Name (N2));
14704 Set_Associated_Node (Prefix (Name (N1)), Empty);
14705 Set_Associated_Node
14706 (Selector_Name (Name (N1)), Selector_Name (Name (N2)));
14707 Set_Etype (Name (N1), Etype (Gen_Id));
14708 end if;
14709 end Save_Global_Defaults;
14710
14711 ----------------------------
14712 -- Save_Global_Descendant --
14713 ----------------------------
14714
14715 procedure Save_Global_Descendant (D : Union_Id) is
14716 N1 : Node_Id;
14717
14718 begin
14719 if D in Node_Range then
14720 if D = Union_Id (Empty) then
14721 null;
14722
14723 elsif Nkind (Node_Id (D)) /= N_Compilation_Unit then
14724 Save_References (Node_Id (D));
14725 end if;
14726
14727 elsif D in List_Range then
14728 pragma Assert (D /= Union_Id (No_List));
14729 -- Because No_List = Empty, which is in Node_Range above
14730
14731 if Is_Empty_List (List_Id (D)) then
14732 null;
14733
14734 else
14735 N1 := First (List_Id (D));
14736 while Present (N1) loop
14737 Save_References (N1);
14738 Next (N1);
14739 end loop;
14740 end if;
14741
14742 -- Element list or other non-node field, nothing to do
14743
14744 else
14745 null;
14746 end if;
14747 end Save_Global_Descendant;
14748
14749 ---------------------
14750 -- Save_References --
14751 ---------------------
14752
14753 -- This is the recursive procedure that does the work once the enclosing
14754 -- generic scope has been established. We have to treat specially a
14755 -- number of node rewritings that are required by semantic processing
14756 -- and which change the kind of nodes in the generic copy: typically
14757 -- constant-folding, replacing an operator node by a string literal, or
14758 -- a selected component by an expanded name. In each of those cases, the
14759 -- transformation is propagated to the generic unit.
14760
14761 procedure Save_References (N : Node_Id) is
14762 Loc : constant Source_Ptr := Sloc (N);
14763
14764 function Requires_Delayed_Save (Nod : Node_Id) return Boolean;
14765 -- Determine whether arbitrary node Nod requires delayed capture of
14766 -- global references within its aspect specifications.
14767
14768 procedure Save_References_In_Aggregate (N : Node_Id);
14769 -- Save all global references in [extension] aggregate node N
14770
14771 procedure Save_References_In_Char_Lit_Or_Op_Symbol (N : Node_Id);
14772 -- Save all global references in a character literal or operator
14773 -- symbol denoted by N.
14774
14775 procedure Save_References_In_Descendants (N : Node_Id);
14776 -- Save all global references in all descendants of node N
14777
14778 procedure Save_References_In_Identifier (N : Node_Id);
14779 -- Save all global references in identifier node N
14780
14781 procedure Save_References_In_Operator (N : Node_Id);
14782 -- Save all global references in operator node N
14783
14784 procedure Save_References_In_Pragma (Prag : Node_Id);
14785 -- Save all global references found within the expression of pragma
14786 -- Prag.
14787
14788 ---------------------------
14789 -- Requires_Delayed_Save --
14790 ---------------------------
14791
14792 function Requires_Delayed_Save (Nod : Node_Id) return Boolean is
14793 begin
14794 -- Generic packages and subprograms require delayed capture of
14795 -- global references within their aspects due to the timing of
14796 -- annotation analysis.
14797
14798 if Nkind_In (Nod, N_Generic_Package_Declaration,
14799 N_Generic_Subprogram_Declaration,
14800 N_Package_Body,
14801 N_Package_Body_Stub,
14802 N_Subprogram_Body,
14803 N_Subprogram_Body_Stub)
14804 then
14805 -- Since the capture of global references is done on the
14806 -- unanalyzed generic template, there is no information around
14807 -- to infer the context. Use the Associated_Entity linkages to
14808 -- peek into the analyzed generic copy and determine what the
14809 -- template corresponds to.
14810
14811 if Nod = Templ then
14812 return
14813 Is_Generic_Declaration_Or_Body
14814 (Unit_Declaration_Node
14815 (Associated_Entity (Defining_Entity (Nod))));
14816
14817 -- Otherwise the generic unit being processed is not the top
14818 -- level template. It is safe to capture of global references
14819 -- within the generic unit because at this point the top level
14820 -- copy is fully analyzed.
14821
14822 else
14823 return False;
14824 end if;
14825
14826 -- Otherwise capture the global references without interference
14827
14828 else
14829 return False;
14830 end if;
14831 end Requires_Delayed_Save;
14832
14833 ----------------------------------
14834 -- Save_References_In_Aggregate --
14835 ----------------------------------
14836
14837 procedure Save_References_In_Aggregate (N : Node_Id) is
14838 Nam : Node_Id;
14839 Qual : Node_Id := Empty;
14840 Typ : Entity_Id := Empty;
14841
14842 use Atree.Unchecked_Access;
14843 -- This code section is part of implementing an untyped tree
14844 -- traversal, so it needs direct access to node fields.
14845
14846 begin
14847 N2 := Get_Associated_Node (N);
14848
14849 if Present (N2) then
14850 Typ := Etype (N2);
14851
14852 -- In an instance within a generic, use the name of the actual
14853 -- and not the original generic parameter. If the actual is
14854 -- global in the current generic it must be preserved for its
14855 -- instantiation.
14856
14857 if Nkind (Parent (Typ)) = N_Subtype_Declaration
14858 and then Present (Generic_Parent_Type (Parent (Typ)))
14859 then
14860 Typ := Base_Type (Typ);
14861 Set_Etype (N2, Typ);
14862 end if;
14863 end if;
14864
14865 if No (N2) or else No (Typ) or else not Is_Global (Typ) then
14866 Set_Associated_Node (N, Empty);
14867
14868 -- If the aggregate is an actual in a call, it has been
14869 -- resolved in the current context, to some local type. The
14870 -- enclosing call may have been disambiguated by the aggregate,
14871 -- and this disambiguation might fail at instantiation time
14872 -- because the type to which the aggregate did resolve is not
14873 -- preserved. In order to preserve some of this information,
14874 -- wrap the aggregate in a qualified expression, using the id
14875 -- of its type. For further disambiguation we qualify the type
14876 -- name with its scope (if visible) because both id's will have
14877 -- corresponding entities in an instance. This resolves most of
14878 -- the problems with missing type information on aggregates in
14879 -- instances.
14880
14881 if Present (N2)
14882 and then Nkind (N2) = Nkind (N)
14883 and then Nkind (Parent (N2)) in N_Subprogram_Call
14884 and then Present (Typ)
14885 and then Comes_From_Source (Typ)
14886 then
14887 Nam := Make_Identifier (Loc, Chars (Typ));
14888
14889 if Is_Immediately_Visible (Scope (Typ)) then
14890 Nam :=
14891 Make_Selected_Component (Loc,
14892 Prefix =>
14893 Make_Identifier (Loc, Chars (Scope (Typ))),
14894 Selector_Name => Nam);
14895 end if;
14896
14897 Qual :=
14898 Make_Qualified_Expression (Loc,
14899 Subtype_Mark => Nam,
14900 Expression => Relocate_Node (N));
14901 end if;
14902 end if;
14903
14904 Save_Global_Descendant (Field1 (N));
14905 Save_Global_Descendant (Field2 (N));
14906 Save_Global_Descendant (Field3 (N));
14907 Save_Global_Descendant (Field5 (N));
14908
14909 if Present (Qual) then
14910 Rewrite (N, Qual);
14911 end if;
14912 end Save_References_In_Aggregate;
14913
14914 ----------------------------------------------
14915 -- Save_References_In_Char_Lit_Or_Op_Symbol --
14916 ----------------------------------------------
14917
14918 procedure Save_References_In_Char_Lit_Or_Op_Symbol (N : Node_Id) is
14919 begin
14920 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
14921 Reset_Entity (N);
14922
14923 elsif Nkind (N) = N_Operator_Symbol
14924 and then Nkind (Get_Associated_Node (N)) = N_String_Literal
14925 then
14926 Change_Operator_Symbol_To_String_Literal (N);
14927 end if;
14928 end Save_References_In_Char_Lit_Or_Op_Symbol;
14929
14930 ------------------------------------
14931 -- Save_References_In_Descendants --
14932 ------------------------------------
14933
14934 procedure Save_References_In_Descendants (N : Node_Id) is
14935 use Atree.Unchecked_Access;
14936 -- This code section is part of implementing an untyped tree
14937 -- traversal, so it needs direct access to node fields.
14938
14939 begin
14940 Save_Global_Descendant (Field1 (N));
14941 Save_Global_Descendant (Field2 (N));
14942 Save_Global_Descendant (Field3 (N));
14943 Save_Global_Descendant (Field4 (N));
14944 Save_Global_Descendant (Field5 (N));
14945 end Save_References_In_Descendants;
14946
14947 -----------------------------------
14948 -- Save_References_In_Identifier --
14949 -----------------------------------
14950
14951 procedure Save_References_In_Identifier (N : Node_Id) is
14952 begin
14953 -- The node did not undergo a transformation
14954
14955 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
14956 declare
14957 Aux_N2 : constant Node_Id := Get_Associated_Node (N);
14958 Orig_N2_Parent : constant Node_Id :=
14959 Original_Node (Parent (Aux_N2));
14960 begin
14961 -- The parent of this identifier is a selected component
14962 -- which denotes a named number that was constant folded.
14963 -- Preserve the original name for ASIS and link the parent
14964 -- with its expanded name. The constant folding will be
14965 -- repeated in the instance.
14966
14967 if Nkind (Parent (N)) = N_Selected_Component
14968 and then Nkind_In (Parent (Aux_N2), N_Integer_Literal,
14969 N_Real_Literal)
14970 and then Is_Entity_Name (Orig_N2_Parent)
14971 and then Ekind (Entity (Orig_N2_Parent)) in Named_Kind
14972 and then Is_Global (Entity (Orig_N2_Parent))
14973 then
14974 N2 := Aux_N2;
14975 Set_Associated_Node
14976 (Parent (N), Original_Node (Parent (N2)));
14977
14978 -- Common case
14979
14980 else
14981 -- If this is a discriminant reference, always save it.
14982 -- It is used in the instance to find the corresponding
14983 -- discriminant positionally rather than by name.
14984
14985 Set_Original_Discriminant
14986 (N, Original_Discriminant (Get_Associated_Node (N)));
14987 end if;
14988
14989 Reset_Entity (N);
14990 end;
14991
14992 -- The analysis of the generic copy transformed the identifier
14993 -- into another construct. Propagate the changes to the template.
14994
14995 else
14996 N2 := Get_Associated_Node (N);
14997
14998 -- The identifier denotes a call to a parameterless function.
14999 -- Mark the node as resolved when the function is external.
15000
15001 if Nkind (N2) = N_Function_Call then
15002 E := Entity (Name (N2));
15003
15004 if Present (E) and then Is_Global (E) then
15005 Set_Etype (N, Etype (N2));
15006 else
15007 Set_Associated_Node (N, Empty);
15008 Set_Etype (N, Empty);
15009 end if;
15010
15011 -- The identifier denotes a named number that was constant
15012 -- folded. Preserve the original name for ASIS and undo the
15013 -- constant folding which will be repeated in the instance.
15014
15015 elsif Nkind_In (N2, N_Integer_Literal, N_Real_Literal)
15016 and then Is_Entity_Name (Original_Node (N2))
15017 then
15018 Set_Associated_Node (N, Original_Node (N2));
15019 Reset_Entity (N);
15020
15021 -- The identifier resolved to a string literal. Propagate this
15022 -- information to the generic template.
15023
15024 elsif Nkind (N2) = N_String_Literal then
15025 Rewrite (N, New_Copy (N2));
15026
15027 -- The identifier is rewritten as a dereference if it is the
15028 -- prefix of an implicit dereference. Preserve the original
15029 -- tree as the analysis of the instance will expand the node
15030 -- again, but preserve the resolved entity if it is global.
15031
15032 elsif Nkind (N2) = N_Explicit_Dereference then
15033 if Is_Entity_Name (Prefix (N2))
15034 and then Present (Entity (Prefix (N2)))
15035 and then Is_Global (Entity (Prefix (N2)))
15036 then
15037 Set_Associated_Node (N, Prefix (N2));
15038
15039 elsif Nkind (Prefix (N2)) = N_Function_Call
15040 and then Present (Entity (Name (Prefix (N2))))
15041 and then Is_Global (Entity (Name (Prefix (N2))))
15042 then
15043 Rewrite (N,
15044 Make_Explicit_Dereference (Loc,
15045 Prefix =>
15046 Make_Function_Call (Loc,
15047 Name =>
15048 New_Occurrence_Of
15049 (Entity (Name (Prefix (N2))), Loc))));
15050
15051 else
15052 Set_Associated_Node (N, Empty);
15053 Set_Etype (N, Empty);
15054 end if;
15055
15056 -- The subtype mark of a nominally unconstrained object is
15057 -- rewritten as a subtype indication using the bounds of the
15058 -- expression. Recover the original subtype mark.
15059
15060 elsif Nkind (N2) = N_Subtype_Indication
15061 and then Is_Entity_Name (Original_Node (N2))
15062 then
15063 Set_Associated_Node (N, Original_Node (N2));
15064 Reset_Entity (N);
15065 end if;
15066 end if;
15067 end Save_References_In_Identifier;
15068
15069 ---------------------------------
15070 -- Save_References_In_Operator --
15071 ---------------------------------
15072
15073 procedure Save_References_In_Operator (N : Node_Id) is
15074 begin
15075 -- The node did not undergo a transformation
15076
15077 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
15078 if Nkind (N) = N_Op_Concat then
15079 Set_Is_Component_Left_Opnd (N,
15080 Is_Component_Left_Opnd (Get_Associated_Node (N)));
15081
15082 Set_Is_Component_Right_Opnd (N,
15083 Is_Component_Right_Opnd (Get_Associated_Node (N)));
15084 end if;
15085
15086 Reset_Entity (N);
15087
15088 -- The analysis of the generic copy transformed the operator into
15089 -- some other construct. Propagate the changes to the template if
15090 -- applicable.
15091
15092 else
15093 N2 := Get_Associated_Node (N);
15094
15095 -- The operator resoved to a function call
15096
15097 if Nkind (N2) = N_Function_Call then
15098
15099 -- Add explicit qualifications in the generic template for
15100 -- all operands of universal type. This aids resolution by
15101 -- preserving the actual type of a literal or an attribute
15102 -- that yields a universal result.
15103
15104 Qualify_Universal_Operands (N, N2);
15105
15106 E := Entity (Name (N2));
15107
15108 if Present (E) and then Is_Global (E) then
15109 Set_Etype (N, Etype (N2));
15110 else
15111 Set_Associated_Node (N, Empty);
15112 Set_Etype (N, Empty);
15113 end if;
15114
15115 -- The operator was folded into a literal
15116
15117 elsif Nkind_In (N2, N_Integer_Literal,
15118 N_Real_Literal,
15119 N_String_Literal)
15120 then
15121 if Present (Original_Node (N2))
15122 and then Nkind (Original_Node (N2)) = Nkind (N)
15123 then
15124 -- Operation was constant-folded. Whenever possible,
15125 -- recover semantic information from unfolded node,
15126 -- for ASIS use.
15127
15128 Set_Associated_Node (N, Original_Node (N2));
15129
15130 if Nkind (N) = N_Op_Concat then
15131 Set_Is_Component_Left_Opnd (N,
15132 Is_Component_Left_Opnd (Get_Associated_Node (N)));
15133 Set_Is_Component_Right_Opnd (N,
15134 Is_Component_Right_Opnd (Get_Associated_Node (N)));
15135 end if;
15136
15137 Reset_Entity (N);
15138
15139 -- Propagate the constant folding back to the template
15140
15141 else
15142 Rewrite (N, New_Copy (N2));
15143 Set_Analyzed (N, False);
15144 end if;
15145
15146 -- The operator was folded into an enumeration literal. Retain
15147 -- the entity to avoid spurious ambiguities if it is overloaded
15148 -- at the point of instantiation or inlining.
15149
15150 elsif Nkind (N2) = N_Identifier
15151 and then Ekind (Entity (N2)) = E_Enumeration_Literal
15152 then
15153 Rewrite (N, New_Copy (N2));
15154 Set_Analyzed (N, False);
15155 end if;
15156 end if;
15157
15158 -- Complete the operands check if node has not been constant
15159 -- folded.
15160
15161 if Nkind (N) in N_Op then
15162 Save_Entity_Descendants (N);
15163 end if;
15164 end Save_References_In_Operator;
15165
15166 -------------------------------
15167 -- Save_References_In_Pragma --
15168 -------------------------------
15169
15170 procedure Save_References_In_Pragma (Prag : Node_Id) is
15171 Context : Node_Id;
15172 Do_Save : Boolean := True;
15173
15174 use Atree.Unchecked_Access;
15175 -- This code section is part of implementing an untyped tree
15176 -- traversal, so it needs direct access to node fields.
15177
15178 begin
15179 -- Do not save global references in pragmas generated from aspects
15180 -- because the pragmas will be regenerated at instantiation time.
15181
15182 if From_Aspect_Specification (Prag) then
15183 Do_Save := False;
15184
15185 -- The capture of global references within contract-related source
15186 -- pragmas associated with generic packages, subprograms or their
15187 -- respective bodies must be delayed due to timing of annotation
15188 -- analysis. Global references are still captured in routine
15189 -- Save_Global_References_In_Contract.
15190
15191 elsif Is_Generic_Contract_Pragma (Prag) and then Prag /= Templ then
15192 if Is_Package_Contract_Annotation (Prag) then
15193 Context := Find_Related_Package_Or_Body (Prag);
15194 else
15195 pragma Assert (Is_Subprogram_Contract_Annotation (Prag));
15196 Context := Find_Related_Declaration_Or_Body (Prag);
15197 end if;
15198
15199 -- The use of Original_Node accounts for the case when the
15200 -- related context is generic template.
15201
15202 if Requires_Delayed_Save (Original_Node (Context)) then
15203 Do_Save := False;
15204 end if;
15205 end if;
15206
15207 -- For all other cases, save all global references within the
15208 -- descendants, but skip the following semantic fields:
15209
15210 -- Field1 - Next_Pragma
15211 -- Field3 - Corresponding_Aspect
15212 -- Field5 - Next_Rep_Item
15213
15214 if Do_Save then
15215 Save_Global_Descendant (Field2 (Prag));
15216 Save_Global_Descendant (Field4 (Prag));
15217 end if;
15218 end Save_References_In_Pragma;
15219
15220 -- Start of processing for Save_References
15221
15222 begin
15223 if N = Empty then
15224 null;
15225
15226 -- Aggregates
15227
15228 elsif Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
15229 Save_References_In_Aggregate (N);
15230
15231 -- Character literals, operator symbols
15232
15233 elsif Nkind_In (N, N_Character_Literal, N_Operator_Symbol) then
15234 Save_References_In_Char_Lit_Or_Op_Symbol (N);
15235
15236 -- Defining identifiers
15237
15238 elsif Nkind (N) in N_Entity then
15239 null;
15240
15241 -- Identifiers
15242
15243 elsif Nkind (N) = N_Identifier then
15244 Save_References_In_Identifier (N);
15245
15246 -- Operators
15247
15248 elsif Nkind (N) in N_Op then
15249 Save_References_In_Operator (N);
15250
15251 -- Pragmas
15252
15253 elsif Nkind (N) = N_Pragma then
15254 Save_References_In_Pragma (N);
15255
15256 else
15257 Save_References_In_Descendants (N);
15258 end if;
15259
15260 -- Save all global references found within the aspect specifications
15261 -- of the related node.
15262
15263 if Permits_Aspect_Specifications (N) and then Has_Aspects (N) then
15264
15265 -- The capture of global references within aspects associated with
15266 -- generic packages, subprograms or their bodies must be delayed
15267 -- due to timing of annotation analysis. Global references are
15268 -- still captured in routine Save_Global_References_In_Contract.
15269
15270 if Requires_Delayed_Save (N) then
15271 null;
15272
15273 -- Otherwise save all global references within the aspects
15274
15275 else
15276 Save_Global_References_In_Aspects (N);
15277 end if;
15278 end if;
15279 end Save_References;
15280
15281 -- Start of processing for Save_Global_References
15282
15283 begin
15284 Gen_Scope := Current_Scope;
15285
15286 -- If the generic unit is a child unit, references to entities in the
15287 -- parent are treated as local, because they will be resolved anew in
15288 -- the context of the instance of the parent.
15289
15290 while Is_Child_Unit (Gen_Scope)
15291 and then Ekind (Scope (Gen_Scope)) = E_Generic_Package
15292 loop
15293 Gen_Scope := Scope (Gen_Scope);
15294 end loop;
15295
15296 Save_References (Templ);
15297 end Save_Global_References;
15298
15299 ---------------------------------------
15300 -- Save_Global_References_In_Aspects --
15301 ---------------------------------------
15302
15303 procedure Save_Global_References_In_Aspects (N : Node_Id) is
15304 Asp : Node_Id;
15305 Expr : Node_Id;
15306
15307 begin
15308 Asp := First (Aspect_Specifications (N));
15309 while Present (Asp) loop
15310 Expr := Expression (Asp);
15311
15312 if Present (Expr) then
15313 Save_Global_References (Expr);
15314 end if;
15315
15316 Next (Asp);
15317 end loop;
15318 end Save_Global_References_In_Aspects;
15319
15320 ------------------------------------------
15321 -- Set_Copied_Sloc_For_Inherited_Pragma --
15322 ------------------------------------------
15323
15324 procedure Set_Copied_Sloc_For_Inherited_Pragma
15325 (N : Node_Id;
15326 E : Entity_Id)
15327 is
15328 begin
15329 Create_Instantiation_Source (N, E,
15330 Inlined_Body => False,
15331 Inherited_Pragma => True,
15332 Factor => S_Adjustment);
15333 end Set_Copied_Sloc_For_Inherited_Pragma;
15334
15335 --------------------------------------
15336 -- Set_Copied_Sloc_For_Inlined_Body --
15337 --------------------------------------
15338
15339 procedure Set_Copied_Sloc_For_Inlined_Body (N : Node_Id; E : Entity_Id) is
15340 begin
15341 Create_Instantiation_Source (N, E,
15342 Inlined_Body => True,
15343 Inherited_Pragma => False,
15344 Factor => S_Adjustment);
15345 end Set_Copied_Sloc_For_Inlined_Body;
15346
15347 ---------------------
15348 -- Set_Instance_Of --
15349 ---------------------
15350
15351 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id) is
15352 begin
15353 Generic_Renamings.Table (Generic_Renamings.Last) := (A, B, Assoc_Null);
15354 Generic_Renamings_HTable.Set (Generic_Renamings.Last);
15355 Generic_Renamings.Increment_Last;
15356 end Set_Instance_Of;
15357
15358 --------------------
15359 -- Set_Next_Assoc --
15360 --------------------
15361
15362 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr) is
15363 begin
15364 Generic_Renamings.Table (E).Next_In_HTable := Next;
15365 end Set_Next_Assoc;
15366
15367 -------------------
15368 -- Start_Generic --
15369 -------------------
15370
15371 procedure Start_Generic is
15372 begin
15373 -- ??? More things could be factored out in this routine.
15374 -- Should probably be done at a later stage.
15375
15376 Generic_Flags.Append (Inside_A_Generic);
15377 Inside_A_Generic := True;
15378
15379 Expander_Mode_Save_And_Set (False);
15380 end Start_Generic;
15381
15382 ----------------------
15383 -- Set_Instance_Env --
15384 ----------------------
15385
15386 procedure Set_Instance_Env
15387 (Gen_Unit : Entity_Id;
15388 Act_Unit : Entity_Id)
15389 is
15390 Assertion_Status : constant Boolean := Assertions_Enabled;
15391 Save_SPARK_Mode : constant SPARK_Mode_Type := SPARK_Mode;
15392 Save_SPARK_Mode_Pragma : constant Node_Id := SPARK_Mode_Pragma;
15393
15394 begin
15395 -- Regardless of the current mode, predefined units are analyzed in the
15396 -- most current Ada mode, and earlier version Ada checks do not apply
15397 -- to predefined units. Nothing needs to be done for non-internal units.
15398 -- These are always analyzed in the current mode.
15399
15400 if Is_Internal_File_Name
15401 (Fname => Unit_File_Name (Get_Source_Unit (Gen_Unit)),
15402 Renamings_Included => True)
15403 then
15404 Set_Opt_Config_Switches (True, Current_Sem_Unit = Main_Unit);
15405
15406 -- In Ada2012 we may want to enable assertions in an instance of a
15407 -- predefined unit, in which case we need to preserve the current
15408 -- setting for the Assertions_Enabled flag. This will become more
15409 -- critical when pre/postconditions are added to predefined units,
15410 -- as is already the case for some numeric libraries.
15411
15412 if Ada_Version >= Ada_2012 then
15413 Assertions_Enabled := Assertion_Status;
15414 end if;
15415
15416 -- SPARK_Mode for an instance is the one applicable at the point of
15417 -- instantiation.
15418
15419 SPARK_Mode := Save_SPARK_Mode;
15420 SPARK_Mode_Pragma := Save_SPARK_Mode_Pragma;
15421 end if;
15422
15423 Current_Instantiated_Parent :=
15424 (Gen_Id => Gen_Unit,
15425 Act_Id => Act_Unit,
15426 Next_In_HTable => Assoc_Null);
15427 end Set_Instance_Env;
15428
15429 -----------------
15430 -- Switch_View --
15431 -----------------
15432
15433 procedure Switch_View (T : Entity_Id) is
15434 BT : constant Entity_Id := Base_Type (T);
15435 Priv_Elmt : Elmt_Id := No_Elmt;
15436 Priv_Sub : Entity_Id;
15437
15438 begin
15439 -- T may be private but its base type may have been exchanged through
15440 -- some other occurrence, in which case there is nothing to switch
15441 -- besides T itself. Note that a private dependent subtype of a private
15442 -- type might not have been switched even if the base type has been,
15443 -- because of the last branch of Check_Private_View (see comment there).
15444
15445 if not Is_Private_Type (BT) then
15446 Prepend_Elmt (Full_View (T), Exchanged_Views);
15447 Exchange_Declarations (T);
15448 return;
15449 end if;
15450
15451 Priv_Elmt := First_Elmt (Private_Dependents (BT));
15452
15453 if Present (Full_View (BT)) then
15454 Prepend_Elmt (Full_View (BT), Exchanged_Views);
15455 Exchange_Declarations (BT);
15456 end if;
15457
15458 while Present (Priv_Elmt) loop
15459 Priv_Sub := (Node (Priv_Elmt));
15460
15461 -- We avoid flipping the subtype if the Etype of its full view is
15462 -- private because this would result in a malformed subtype. This
15463 -- occurs when the Etype of the subtype full view is the full view of
15464 -- the base type (and since the base types were just switched, the
15465 -- subtype is pointing to the wrong view). This is currently the case
15466 -- for tagged record types, access types (maybe more?) and needs to
15467 -- be resolved. ???
15468
15469 if Present (Full_View (Priv_Sub))
15470 and then not Is_Private_Type (Etype (Full_View (Priv_Sub)))
15471 then
15472 Prepend_Elmt (Full_View (Priv_Sub), Exchanged_Views);
15473 Exchange_Declarations (Priv_Sub);
15474 end if;
15475
15476 Next_Elmt (Priv_Elmt);
15477 end loop;
15478 end Switch_View;
15479
15480 -----------------
15481 -- True_Parent --
15482 -----------------
15483
15484 function True_Parent (N : Node_Id) return Node_Id is
15485 begin
15486 if Nkind (Parent (N)) = N_Subunit then
15487 return Parent (Corresponding_Stub (Parent (N)));
15488 else
15489 return Parent (N);
15490 end if;
15491 end True_Parent;
15492
15493 -----------------------------
15494 -- Valid_Default_Attribute --
15495 -----------------------------
15496
15497 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id) is
15498 Attr_Id : constant Attribute_Id :=
15499 Get_Attribute_Id (Attribute_Name (Def));
15500 T : constant Entity_Id := Entity (Prefix (Def));
15501 Is_Fun : constant Boolean := (Ekind (Nam) = E_Function);
15502 F : Entity_Id;
15503 Num_F : Nat;
15504 OK : Boolean;
15505
15506 begin
15507 if No (T) or else T = Any_Id then
15508 return;
15509 end if;
15510
15511 Num_F := 0;
15512 F := First_Formal (Nam);
15513 while Present (F) loop
15514 Num_F := Num_F + 1;
15515 Next_Formal (F);
15516 end loop;
15517
15518 case Attr_Id is
15519 when Attribute_Adjacent
15520 | Attribute_Ceiling
15521 | Attribute_Copy_Sign
15522 | Attribute_Floor
15523 | Attribute_Fraction
15524 | Attribute_Machine
15525 | Attribute_Model
15526 | Attribute_Remainder
15527 | Attribute_Rounding
15528 | Attribute_Unbiased_Rounding
15529 =>
15530 OK := Is_Fun
15531 and then Num_F = 1
15532 and then Is_Floating_Point_Type (T);
15533
15534 when Attribute_Image
15535 | Attribute_Pred
15536 | Attribute_Succ
15537 | Attribute_Value
15538 | Attribute_Wide_Image
15539 | Attribute_Wide_Value
15540 =>
15541 OK := Is_Fun and then Num_F = 1 and then Is_Scalar_Type (T);
15542
15543 when Attribute_Max
15544 | Attribute_Min
15545 =>
15546 OK := Is_Fun and then Num_F = 2 and then Is_Scalar_Type (T);
15547
15548 when Attribute_Input =>
15549 OK := (Is_Fun and then Num_F = 1);
15550
15551 when Attribute_Output
15552 | Attribute_Read
15553 | Attribute_Write
15554 =>
15555 OK := not Is_Fun and then Num_F = 2;
15556
15557 when others =>
15558 OK := False;
15559 end case;
15560
15561 if not OK then
15562 Error_Msg_N
15563 ("attribute reference has wrong profile for subprogram", Def);
15564 end if;
15565 end Valid_Default_Attribute;
15566
15567 end Sem_Ch12;