[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.
717
718 procedure Install_Formal_Packages (Par : Entity_Id);
719 -- Install the visible part of any formal of the parent that is a formal
720 -- package. Note that for the case of a formal package with a box, this
721 -- includes the formal part of the formal package (12.7(10/2)).
722
723 procedure Install_Hidden_Primitives
724 (Prims_List : in out Elist_Id;
725 Gen_T : Entity_Id;
726 Act_T : Entity_Id);
727 -- Remove suffix 'P' from hidden primitives of Act_T to match the
728 -- visibility of primitives of Gen_T. The list of primitives to which
729 -- the suffix is removed is added to Prims_List to restore them later.
730
731 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False);
732 -- When compiling an instance of a child unit the parent (which is
733 -- itself an instance) is an enclosing scope that must be made
734 -- immediately visible. This procedure is also used to install the non-
735 -- generic parent of a generic child unit when compiling its body, so
736 -- that full views of types in the parent are made visible.
737
738 -- The functions Instantiate_XXX perform various legality checks and build
739 -- the declarations for instantiated generic parameters. In all of these
740 -- Formal is the entity in the generic unit, Actual is the entity of
741 -- expression in the generic associations, and Analyzed_Formal is the
742 -- formal in the generic copy, which contains the semantic information to
743 -- be used to validate the actual.
744
745 function Instantiate_Object
746 (Formal : Node_Id;
747 Actual : Node_Id;
748 Analyzed_Formal : Node_Id) return List_Id;
749
750 function Instantiate_Type
751 (Formal : Node_Id;
752 Actual : Node_Id;
753 Analyzed_Formal : Node_Id;
754 Actual_Decls : List_Id) return List_Id;
755
756 function Instantiate_Formal_Subprogram
757 (Formal : Node_Id;
758 Actual : Node_Id;
759 Analyzed_Formal : Node_Id) return Node_Id;
760
761 function Instantiate_Formal_Package
762 (Formal : Node_Id;
763 Actual : Node_Id;
764 Analyzed_Formal : Node_Id) return List_Id;
765 -- If the formal package is declared with a box, special visibility rules
766 -- apply to its formals: they are in the visible part of the package. This
767 -- is true in the declarative region of the formal package, that is to say
768 -- in the enclosing generic or instantiation. For an instantiation, the
769 -- parameters of the formal package are made visible in an explicit step.
770 -- Furthermore, if the actual has a visible USE clause, these formals must
771 -- be made potentially use-visible as well. On exit from the enclosing
772 -- instantiation, the reverse must be done.
773
774 -- For a formal package declared without a box, there are conformance rules
775 -- that apply to the actuals in the generic declaration and the actuals of
776 -- the actual package in the enclosing instantiation. The simplest way to
777 -- apply these rules is to repeat the instantiation of the formal package
778 -- in the context of the enclosing instance, and compare the generic
779 -- associations of this instantiation with those of the actual package.
780 -- This internal instantiation only needs to contain the renamings of the
781 -- formals: the visible and private declarations themselves need not be
782 -- created.
783
784 -- In Ada 2005, the formal package may be only partially parameterized.
785 -- In that case the visibility step must make visible those actuals whose
786 -- corresponding formals were given with a box. A final complication
787 -- involves inherited operations from formal derived types, which must
788 -- be visible if the type is.
789
790 function Is_In_Main_Unit (N : Node_Id) return Boolean;
791 -- Test if given node is in the main unit
792
793 procedure Load_Parent_Of_Generic
794 (N : Node_Id;
795 Spec : Node_Id;
796 Body_Optional : Boolean := False);
797 -- If the generic appears in a separate non-generic library unit, load the
798 -- corresponding body to retrieve the body of the generic. N is the node
799 -- for the generic instantiation, Spec is the generic package declaration.
800 --
801 -- Body_Optional is a flag that indicates that the body is being loaded to
802 -- ensure that temporaries are generated consistently when there are other
803 -- instances in the current declarative part that precede the one being
804 -- loaded. In that case a missing body is acceptable.
805
806 procedure Map_Formal_Package_Entities (Form : Entity_Id; Act : Entity_Id);
807 -- Within the generic part, entities in the formal package are
808 -- visible. To validate subsequent type declarations, indicate
809 -- the correspondence between the entities in the analyzed formal,
810 -- and the entities in the actual package. There are three packages
811 -- involved in the instantiation of a formal package: the parent
812 -- generic P1 which appears in the generic declaration, the fake
813 -- instantiation P2 which appears in the analyzed generic, and whose
814 -- visible entities may be used in subsequent formals, and the actual
815 -- P3 in the instance. To validate subsequent formals, me indicate
816 -- that the entities in P2 are mapped into those of P3. The mapping of
817 -- entities has to be done recursively for nested packages.
818
819 procedure Move_Freeze_Nodes
820 (Out_Of : Entity_Id;
821 After : Node_Id;
822 L : List_Id);
823 -- Freeze nodes can be generated in the analysis of a generic unit, but
824 -- will not be seen by the back-end. It is necessary to move those nodes
825 -- to the enclosing scope if they freeze an outer entity. We place them
826 -- at the end of the enclosing generic package, which is semantically
827 -- neutral.
828
829 procedure Preanalyze_Actuals (N : Node_Id; Inst : Entity_Id := Empty);
830 -- Analyze actuals to perform name resolution. Full resolution is done
831 -- later, when the expected types are known, but names have to be captured
832 -- before installing parents of generics, that are not visible for the
833 -- actuals themselves.
834 --
835 -- If Inst is present, it is the entity of the package instance. This
836 -- entity is marked as having a limited_view actual when some actual is
837 -- a limited view. This is used to place the instance body properly.
838
839 procedure Remove_Parent (In_Body : Boolean := False);
840 -- Reverse effect after instantiation of child is complete
841
842 procedure Restore_Hidden_Primitives (Prims_List : in out Elist_Id);
843 -- Restore suffix 'P' to primitives of Prims_List and leave Prims_List
844 -- set to No_Elist.
845
846 procedure Set_Instance_Env
847 (Gen_Unit : Entity_Id;
848 Act_Unit : Entity_Id);
849 -- Save current instance on saved environment, to be used to determine
850 -- the global status of entities in nested instances. Part of Save_Env.
851 -- called after verifying that the generic unit is legal for the instance,
852 -- The procedure also examines whether the generic unit is a predefined
853 -- unit, in order to set configuration switches accordingly. As a result
854 -- the procedure must be called after analyzing and freezing the actuals.
855
856 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id);
857 -- Associate analyzed generic parameter with corresponding instance. Used
858 -- for semantic checks at instantiation time.
859
860 function True_Parent (N : Node_Id) return Node_Id;
861 -- For a subunit, return parent of corresponding stub, else return
862 -- parent of node.
863
864 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id);
865 -- Verify that an attribute that appears as the default for a formal
866 -- subprogram is a function or procedure with the correct profile.
867
868 -------------------------------------------
869 -- Data Structures for Generic Renamings --
870 -------------------------------------------
871
872 -- The map Generic_Renamings associates generic entities with their
873 -- corresponding actuals. Currently used to validate type instances. It
874 -- will eventually be used for all generic parameters to eliminate the
875 -- need for overload resolution in the instance.
876
877 type Assoc_Ptr is new Int;
878
879 Assoc_Null : constant Assoc_Ptr := -1;
880
881 type Assoc is record
882 Gen_Id : Entity_Id;
883 Act_Id : Entity_Id;
884 Next_In_HTable : Assoc_Ptr;
885 end record;
886
887 package Generic_Renamings is new Table.Table
888 (Table_Component_Type => Assoc,
889 Table_Index_Type => Assoc_Ptr,
890 Table_Low_Bound => 0,
891 Table_Initial => 10,
892 Table_Increment => 100,
893 Table_Name => "Generic_Renamings");
894
895 -- Variable to hold enclosing instantiation. When the environment is
896 -- saved for a subprogram inlining, the corresponding Act_Id is empty.
897
898 Current_Instantiated_Parent : Assoc := (Empty, Empty, Assoc_Null);
899
900 -- Hash table for associations
901
902 HTable_Size : constant := 37;
903 type HTable_Range is range 0 .. HTable_Size - 1;
904
905 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr);
906 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr;
907 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id;
908 function Hash (F : Entity_Id) return HTable_Range;
909
910 package Generic_Renamings_HTable is new GNAT.HTable.Static_HTable (
911 Header_Num => HTable_Range,
912 Element => Assoc,
913 Elmt_Ptr => Assoc_Ptr,
914 Null_Ptr => Assoc_Null,
915 Set_Next => Set_Next_Assoc,
916 Next => Next_Assoc,
917 Key => Entity_Id,
918 Get_Key => Get_Gen_Id,
919 Hash => Hash,
920 Equal => "=");
921
922 Exchanged_Views : Elist_Id;
923 -- This list holds the private views that have been exchanged during
924 -- instantiation to restore the visibility of the generic declaration.
925 -- (see comments above). After instantiation, the current visibility is
926 -- reestablished by means of a traversal of this list.
927
928 Hidden_Entities : Elist_Id;
929 -- This list holds the entities of the current scope that are removed
930 -- from immediate visibility when instantiating a child unit. Their
931 -- visibility is restored in Remove_Parent.
932
933 -- Because instantiations can be recursive, the following must be saved
934 -- on entry and restored on exit from an instantiation (spec or body).
935 -- This is done by the two procedures Save_Env and Restore_Env. For
936 -- package and subprogram instantiations (but not for the body instances)
937 -- the action of Save_Env is done in two steps: Init_Env is called before
938 -- Check_Generic_Child_Unit, because setting the parent instances requires
939 -- that the visibility data structures be properly initialized. Once the
940 -- generic is unit is validated, Set_Instance_Env completes Save_Env.
941
942 Parent_Unit_Visible : Boolean := False;
943 -- Parent_Unit_Visible is used when the generic is a child unit, and
944 -- indicates whether the ultimate parent of the generic is visible in the
945 -- instantiation environment. It is used to reset the visibility of the
946 -- parent at the end of the instantiation (see Remove_Parent).
947
948 Instance_Parent_Unit : Entity_Id := Empty;
949 -- This records the ultimate parent unit of an instance of a generic
950 -- child unit and is used in conjunction with Parent_Unit_Visible to
951 -- indicate the unit to which the Parent_Unit_Visible flag corresponds.
952
953 type Instance_Env is record
954 Instantiated_Parent : Assoc;
955 Exchanged_Views : Elist_Id;
956 Hidden_Entities : Elist_Id;
957 Current_Sem_Unit : Unit_Number_Type;
958 Parent_Unit_Visible : Boolean := False;
959 Instance_Parent_Unit : Entity_Id := Empty;
960 Switches : Config_Switches_Type;
961 end record;
962
963 package Instance_Envs is new Table.Table (
964 Table_Component_Type => Instance_Env,
965 Table_Index_Type => Int,
966 Table_Low_Bound => 0,
967 Table_Initial => 32,
968 Table_Increment => 100,
969 Table_Name => "Instance_Envs");
970
971 procedure Restore_Private_Views
972 (Pack_Id : Entity_Id;
973 Is_Package : Boolean := True);
974 -- Restore the private views of external types, and unmark the generic
975 -- renamings of actuals, so that they become compatible subtypes again.
976 -- For subprograms, Pack_Id is the package constructed to hold the
977 -- renamings.
978
979 procedure Switch_View (T : Entity_Id);
980 -- Switch the partial and full views of a type and its private
981 -- dependents (i.e. its subtypes and derived types).
982
983 ------------------------------------
984 -- Structures for Error Reporting --
985 ------------------------------------
986
987 Instantiation_Node : Node_Id;
988 -- Used by subprograms that validate instantiation of formal parameters
989 -- where there might be no actual on which to place the error message.
990 -- Also used to locate the instantiation node for generic subunits.
991
992 Instantiation_Error : exception;
993 -- When there is a semantic error in the generic parameter matching,
994 -- there is no point in continuing the instantiation, because the
995 -- number of cascaded errors is unpredictable. This exception aborts
996 -- the instantiation process altogether.
997
998 S_Adjustment : Sloc_Adjustment;
999 -- Offset created for each node in an instantiation, in order to keep
1000 -- track of the source position of the instantiation in each of its nodes.
1001 -- A subsequent semantic error or warning on a construct of the instance
1002 -- points to both places: the original generic node, and the point of
1003 -- instantiation. See Sinput and Sinput.L for additional details.
1004
1005 ------------------------------------------------------------
1006 -- Data structure for keeping track when inside a Generic --
1007 ------------------------------------------------------------
1008
1009 -- The following table is used to save values of the Inside_A_Generic
1010 -- flag (see spec of Sem) when they are saved by Start_Generic.
1011
1012 package Generic_Flags is new Table.Table (
1013 Table_Component_Type => Boolean,
1014 Table_Index_Type => Int,
1015 Table_Low_Bound => 0,
1016 Table_Initial => 32,
1017 Table_Increment => 200,
1018 Table_Name => "Generic_Flags");
1019
1020 ---------------------------
1021 -- Abandon_Instantiation --
1022 ---------------------------
1023
1024 procedure Abandon_Instantiation (N : Node_Id) is
1025 begin
1026 Error_Msg_N ("\instantiation abandoned!", N);
1027 raise Instantiation_Error;
1028 end Abandon_Instantiation;
1029
1030 --------------------------------
1031 -- Add_Pending_Instantiation --
1032 --------------------------------
1033
1034 procedure Add_Pending_Instantiation (Inst : Node_Id; Act_Decl : Node_Id) is
1035 begin
1036
1037 -- Add to the instantiation node and the corresponding unit declaration
1038 -- the current values of global flags to be used when analyzing the
1039 -- instance body.
1040
1041 Pending_Instantiations.Append
1042 ((Inst_Node => Inst,
1043 Act_Decl => Act_Decl,
1044 Expander_Status => Expander_Active,
1045 Current_Sem_Unit => Current_Sem_Unit,
1046 Scope_Suppress => Scope_Suppress,
1047 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
1048 Version => Ada_Version,
1049 Version_Pragma => Ada_Version_Pragma,
1050 Warnings => Save_Warnings,
1051 SPARK_Mode => SPARK_Mode,
1052 SPARK_Mode_Pragma => SPARK_Mode_Pragma));
1053 end Add_Pending_Instantiation;
1054
1055 --------------------------
1056 -- Analyze_Associations --
1057 --------------------------
1058
1059 function Analyze_Associations
1060 (I_Node : Node_Id;
1061 Formals : List_Id;
1062 F_Copy : List_Id) return List_Id
1063 is
1064 Actuals_To_Freeze : constant Elist_Id := New_Elmt_List;
1065 Assoc : constant List_Id := New_List;
1066 Default_Actuals : constant List_Id := New_List;
1067 Gen_Unit : constant Entity_Id :=
1068 Defining_Entity (Parent (F_Copy));
1069
1070 Actuals : List_Id;
1071 Actual : Node_Id;
1072 Analyzed_Formal : Node_Id;
1073 First_Named : Node_Id := Empty;
1074 Formal : Node_Id;
1075 Match : Node_Id;
1076 Named : Node_Id;
1077 Saved_Formal : Node_Id;
1078
1079 Default_Formals : constant List_Id := New_List;
1080 -- If an Others_Choice is present, some of the formals may be defaulted.
1081 -- To simplify the treatment of visibility in an instance, we introduce
1082 -- individual defaults for each such formal. These defaults are
1083 -- appended to the list of associations and replace the Others_Choice.
1084
1085 Found_Assoc : Node_Id;
1086 -- Association for the current formal being match. Empty if there are
1087 -- no remaining actuals, or if there is no named association with the
1088 -- name of the formal.
1089
1090 Is_Named_Assoc : Boolean;
1091 Num_Matched : Nat := 0;
1092 Num_Actuals : Nat := 0;
1093
1094 Others_Present : Boolean := False;
1095 Others_Choice : Node_Id := Empty;
1096 -- In Ada 2005, indicates partial parameterization of a formal
1097 -- package. As usual an other association must be last in the list.
1098
1099 procedure Check_Overloaded_Formal_Subprogram (Formal : Entity_Id);
1100 -- Apply RM 12.3(9): if a formal subprogram is overloaded, the instance
1101 -- cannot have a named association for it. AI05-0025 extends this rule
1102 -- to formals of formal packages by AI05-0025, and it also applies to
1103 -- box-initialized formals.
1104
1105 function Has_Fully_Defined_Profile (Subp : Entity_Id) return Boolean;
1106 -- Determine whether the parameter types and the return type of Subp
1107 -- are fully defined at the point of instantiation.
1108
1109 function Matching_Actual
1110 (F : Entity_Id;
1111 A_F : Entity_Id) return Node_Id;
1112 -- Find actual that corresponds to a given a formal parameter. If the
1113 -- actuals are positional, return the next one, if any. If the actuals
1114 -- are named, scan the parameter associations to find the right one.
1115 -- A_F is the corresponding entity in the analyzed generic,which is
1116 -- placed on the selector name for ASIS use.
1117 --
1118 -- In Ada 2005, a named association may be given with a box, in which
1119 -- case Matching_Actual sets Found_Assoc to the generic association,
1120 -- but return Empty for the actual itself. In this case the code below
1121 -- creates a corresponding declaration for the formal.
1122
1123 function Partial_Parameterization return Boolean;
1124 -- Ada 2005: if no match is found for a given formal, check if the
1125 -- association for it includes a box, or whether the associations
1126 -- include an Others clause.
1127
1128 procedure Process_Default (F : Entity_Id);
1129 -- Add a copy of the declaration of generic formal F to the list of
1130 -- associations, and add an explicit box association for F if there
1131 -- is none yet, and the default comes from an Others_Choice.
1132
1133 function Renames_Standard_Subprogram (Subp : Entity_Id) return Boolean;
1134 -- Determine whether Subp renames one of the subprograms defined in the
1135 -- generated package Standard.
1136
1137 procedure Set_Analyzed_Formal;
1138 -- Find the node in the generic copy that corresponds to a given formal.
1139 -- The semantic information on this node is used to perform legality
1140 -- checks on the actuals. Because semantic analysis can introduce some
1141 -- anonymous entities or modify the declaration node itself, the
1142 -- correspondence between the two lists is not one-one. In addition to
1143 -- anonymous types, the presence a formal equality will introduce an
1144 -- implicit declaration for the corresponding inequality.
1145
1146 ----------------------------------------
1147 -- Check_Overloaded_Formal_Subprogram --
1148 ----------------------------------------
1149
1150 procedure Check_Overloaded_Formal_Subprogram (Formal : Entity_Id) is
1151 Temp_Formal : Entity_Id;
1152
1153 begin
1154 Temp_Formal := First (Formals);
1155 while Present (Temp_Formal) loop
1156 if Nkind (Temp_Formal) in N_Formal_Subprogram_Declaration
1157 and then Temp_Formal /= Formal
1158 and then
1159 Chars (Defining_Unit_Name (Specification (Formal))) =
1160 Chars (Defining_Unit_Name (Specification (Temp_Formal)))
1161 then
1162 if Present (Found_Assoc) then
1163 Error_Msg_N
1164 ("named association not allowed for overloaded formal",
1165 Found_Assoc);
1166
1167 else
1168 Error_Msg_N
1169 ("named association not allowed for overloaded formal",
1170 Others_Choice);
1171 end if;
1172
1173 Abandon_Instantiation (Instantiation_Node);
1174 end if;
1175
1176 Next (Temp_Formal);
1177 end loop;
1178 end Check_Overloaded_Formal_Subprogram;
1179
1180 -------------------------------
1181 -- Has_Fully_Defined_Profile --
1182 -------------------------------
1183
1184 function Has_Fully_Defined_Profile (Subp : Entity_Id) return Boolean is
1185 function Is_Fully_Defined_Type (Typ : Entity_Id) return Boolean;
1186 -- Determine whethet type Typ is fully defined
1187
1188 ---------------------------
1189 -- Is_Fully_Defined_Type --
1190 ---------------------------
1191
1192 function Is_Fully_Defined_Type (Typ : Entity_Id) return Boolean is
1193 begin
1194 -- A private type without a full view is not fully defined
1195
1196 if Is_Private_Type (Typ)
1197 and then No (Full_View (Typ))
1198 then
1199 return False;
1200
1201 -- An incomplete type is never fully defined
1202
1203 elsif Is_Incomplete_Type (Typ) then
1204 return False;
1205
1206 -- All other types are fully defined
1207
1208 else
1209 return True;
1210 end if;
1211 end Is_Fully_Defined_Type;
1212
1213 -- Local declarations
1214
1215 Param : Entity_Id;
1216
1217 -- Start of processing for Has_Fully_Defined_Profile
1218
1219 begin
1220 -- Check the parameters
1221
1222 Param := First_Formal (Subp);
1223 while Present (Param) loop
1224 if not Is_Fully_Defined_Type (Etype (Param)) then
1225 return False;
1226 end if;
1227
1228 Next_Formal (Param);
1229 end loop;
1230
1231 -- Check the return type
1232
1233 return Is_Fully_Defined_Type (Etype (Subp));
1234 end Has_Fully_Defined_Profile;
1235
1236 ---------------------
1237 -- Matching_Actual --
1238 ---------------------
1239
1240 function Matching_Actual
1241 (F : Entity_Id;
1242 A_F : Entity_Id) return Node_Id
1243 is
1244 Prev : Node_Id;
1245 Act : Node_Id;
1246
1247 begin
1248 Is_Named_Assoc := False;
1249
1250 -- End of list of purely positional parameters
1251
1252 if No (Actual) or else Nkind (Actual) = N_Others_Choice then
1253 Found_Assoc := Empty;
1254 Act := Empty;
1255
1256 -- Case of positional parameter corresponding to current formal
1257
1258 elsif No (Selector_Name (Actual)) then
1259 Found_Assoc := Actual;
1260 Act := Explicit_Generic_Actual_Parameter (Actual);
1261 Num_Matched := Num_Matched + 1;
1262 Next (Actual);
1263
1264 -- Otherwise scan list of named actuals to find the one with the
1265 -- desired name. All remaining actuals have explicit names.
1266
1267 else
1268 Is_Named_Assoc := True;
1269 Found_Assoc := Empty;
1270 Act := Empty;
1271 Prev := Empty;
1272
1273 while Present (Actual) loop
1274 if Chars (Selector_Name (Actual)) = Chars (F) then
1275 Set_Entity (Selector_Name (Actual), A_F);
1276 Set_Etype (Selector_Name (Actual), Etype (A_F));
1277 Generate_Reference (A_F, Selector_Name (Actual));
1278 Found_Assoc := Actual;
1279 Act := Explicit_Generic_Actual_Parameter (Actual);
1280 Num_Matched := Num_Matched + 1;
1281 exit;
1282 end if;
1283
1284 Prev := Actual;
1285 Next (Actual);
1286 end loop;
1287
1288 -- Reset for subsequent searches. In most cases the named
1289 -- associations are in order. If they are not, we reorder them
1290 -- to avoid scanning twice the same actual. This is not just a
1291 -- question of efficiency: there may be multiple defaults with
1292 -- boxes that have the same name. In a nested instantiation we
1293 -- insert actuals for those defaults, and cannot rely on their
1294 -- names to disambiguate them.
1295
1296 if Actual = First_Named then
1297 Next (First_Named);
1298
1299 elsif Present (Actual) then
1300 Insert_Before (First_Named, Remove_Next (Prev));
1301 end if;
1302
1303 Actual := First_Named;
1304 end if;
1305
1306 if Is_Entity_Name (Act) and then Present (Entity (Act)) then
1307 Set_Used_As_Generic_Actual (Entity (Act));
1308 end if;
1309
1310 return Act;
1311 end Matching_Actual;
1312
1313 ------------------------------
1314 -- Partial_Parameterization --
1315 ------------------------------
1316
1317 function Partial_Parameterization return Boolean is
1318 begin
1319 return Others_Present
1320 or else (Present (Found_Assoc) and then Box_Present (Found_Assoc));
1321 end Partial_Parameterization;
1322
1323 ---------------------
1324 -- Process_Default --
1325 ---------------------
1326
1327 procedure Process_Default (F : Entity_Id) is
1328 Loc : constant Source_Ptr := Sloc (I_Node);
1329 F_Id : constant Entity_Id := Defining_Entity (F);
1330 Decl : Node_Id;
1331 Default : Node_Id;
1332 Id : Entity_Id;
1333
1334 begin
1335 -- Append copy of formal declaration to associations, and create new
1336 -- defining identifier for it.
1337
1338 Decl := New_Copy_Tree (F);
1339 Id := Make_Defining_Identifier (Sloc (F_Id), Chars (F_Id));
1340
1341 if Nkind (F) in N_Formal_Subprogram_Declaration then
1342 Set_Defining_Unit_Name (Specification (Decl), Id);
1343
1344 else
1345 Set_Defining_Identifier (Decl, Id);
1346 end if;
1347
1348 Append (Decl, Assoc);
1349
1350 if No (Found_Assoc) then
1351 Default :=
1352 Make_Generic_Association (Loc,
1353 Selector_Name =>
1354 New_Occurrence_Of (Id, Loc),
1355 Explicit_Generic_Actual_Parameter => Empty);
1356 Set_Box_Present (Default);
1357 Append (Default, Default_Formals);
1358 end if;
1359 end Process_Default;
1360
1361 ---------------------------------
1362 -- Renames_Standard_Subprogram --
1363 ---------------------------------
1364
1365 function Renames_Standard_Subprogram (Subp : Entity_Id) return Boolean is
1366 Id : Entity_Id;
1367
1368 begin
1369 Id := Alias (Subp);
1370 while Present (Id) loop
1371 if Scope (Id) = Standard_Standard then
1372 return True;
1373 end if;
1374
1375 Id := Alias (Id);
1376 end loop;
1377
1378 return False;
1379 end Renames_Standard_Subprogram;
1380
1381 -------------------------
1382 -- Set_Analyzed_Formal --
1383 -------------------------
1384
1385 procedure Set_Analyzed_Formal is
1386 Kind : Node_Kind;
1387
1388 begin
1389 while Present (Analyzed_Formal) loop
1390 Kind := Nkind (Analyzed_Formal);
1391
1392 case Nkind (Formal) is
1393
1394 when N_Formal_Subprogram_Declaration =>
1395 exit when Kind in N_Formal_Subprogram_Declaration
1396 and then
1397 Chars
1398 (Defining_Unit_Name (Specification (Formal))) =
1399 Chars
1400 (Defining_Unit_Name (Specification (Analyzed_Formal)));
1401
1402 when N_Formal_Package_Declaration =>
1403 exit when Nkind_In (Kind, N_Formal_Package_Declaration,
1404 N_Generic_Package_Declaration,
1405 N_Package_Declaration);
1406
1407 when N_Use_Package_Clause | N_Use_Type_Clause => exit;
1408
1409 when others =>
1410
1411 -- Skip freeze nodes, and nodes inserted to replace
1412 -- unrecognized pragmas.
1413
1414 exit when
1415 Kind not in N_Formal_Subprogram_Declaration
1416 and then not Nkind_In (Kind, N_Subprogram_Declaration,
1417 N_Freeze_Entity,
1418 N_Null_Statement,
1419 N_Itype_Reference)
1420 and then Chars (Defining_Identifier (Formal)) =
1421 Chars (Defining_Identifier (Analyzed_Formal));
1422 end case;
1423
1424 Next (Analyzed_Formal);
1425 end loop;
1426 end Set_Analyzed_Formal;
1427
1428 -- Start of processing for Analyze_Associations
1429
1430 begin
1431 Actuals := Generic_Associations (I_Node);
1432
1433 if Present (Actuals) then
1434
1435 -- Check for an Others choice, indicating a partial parameterization
1436 -- for a formal package.
1437
1438 Actual := First (Actuals);
1439 while Present (Actual) loop
1440 if Nkind (Actual) = N_Others_Choice then
1441 Others_Present := True;
1442 Others_Choice := Actual;
1443
1444 if Present (Next (Actual)) then
1445 Error_Msg_N ("others must be last association", Actual);
1446 end if;
1447
1448 -- This subprogram is used both for formal packages and for
1449 -- instantiations. For the latter, associations must all be
1450 -- explicit.
1451
1452 if Nkind (I_Node) /= N_Formal_Package_Declaration
1453 and then Comes_From_Source (I_Node)
1454 then
1455 Error_Msg_N
1456 ("others association not allowed in an instance",
1457 Actual);
1458 end if;
1459
1460 -- In any case, nothing to do after the others association
1461
1462 exit;
1463
1464 elsif Box_Present (Actual)
1465 and then Comes_From_Source (I_Node)
1466 and then Nkind (I_Node) /= N_Formal_Package_Declaration
1467 then
1468 Error_Msg_N
1469 ("box association not allowed in an instance", Actual);
1470 end if;
1471
1472 Next (Actual);
1473 end loop;
1474
1475 -- If named associations are present, save first named association
1476 -- (it may of course be Empty) to facilitate subsequent name search.
1477
1478 First_Named := First (Actuals);
1479 while Present (First_Named)
1480 and then Nkind (First_Named) /= N_Others_Choice
1481 and then No (Selector_Name (First_Named))
1482 loop
1483 Num_Actuals := Num_Actuals + 1;
1484 Next (First_Named);
1485 end loop;
1486 end if;
1487
1488 Named := First_Named;
1489 while Present (Named) loop
1490 if Nkind (Named) /= N_Others_Choice
1491 and then No (Selector_Name (Named))
1492 then
1493 Error_Msg_N ("invalid positional actual after named one", Named);
1494 Abandon_Instantiation (Named);
1495 end if;
1496
1497 -- A named association may lack an actual parameter, if it was
1498 -- introduced for a default subprogram that turns out to be local
1499 -- to the outer instantiation. If it has a box association it must
1500 -- correspond to some formal in the generic.
1501
1502 if Nkind (Named) /= N_Others_Choice
1503 and then
1504 (Present (Explicit_Generic_Actual_Parameter (Named))
1505 or else Box_Present (Named))
1506 then
1507 Num_Actuals := Num_Actuals + 1;
1508 end if;
1509
1510 Next (Named);
1511 end loop;
1512
1513 if Present (Formals) then
1514 Formal := First_Non_Pragma (Formals);
1515 Analyzed_Formal := First_Non_Pragma (F_Copy);
1516
1517 if Present (Actuals) then
1518 Actual := First (Actuals);
1519
1520 -- All formals should have default values
1521
1522 else
1523 Actual := Empty;
1524 end if;
1525
1526 while Present (Formal) loop
1527 Set_Analyzed_Formal;
1528 Saved_Formal := Next_Non_Pragma (Formal);
1529
1530 case Nkind (Formal) is
1531 when N_Formal_Object_Declaration =>
1532 Match :=
1533 Matching_Actual
1534 (Defining_Identifier (Formal),
1535 Defining_Identifier (Analyzed_Formal));
1536
1537 if No (Match) and then Partial_Parameterization then
1538 Process_Default (Formal);
1539
1540 else
1541 Append_List
1542 (Instantiate_Object (Formal, Match, Analyzed_Formal),
1543 Assoc);
1544
1545 -- For a defaulted in_parameter, create an entry in the
1546 -- the list of defaulted actuals, for GNATProve use. Do
1547 -- not included these defaults for an instance nested
1548 -- within a generic, because the defaults are also used
1549 -- in the analysis of the enclosing generic, and only
1550 -- defaulted subprograms are relevant there.
1551
1552 if No (Match) and then not Inside_A_Generic then
1553 Append_To (Default_Actuals,
1554 Make_Generic_Association (Sloc (I_Node),
1555 Selector_Name =>
1556 New_Occurrence_Of
1557 (Defining_Identifier (Formal), Sloc (I_Node)),
1558 Explicit_Generic_Actual_Parameter =>
1559 New_Copy_Tree (Default_Expression (Formal))));
1560 end if;
1561 end if;
1562
1563 -- If the object is a call to an expression function, this
1564 -- is a freezing point for it.
1565
1566 if Is_Entity_Name (Match)
1567 and then Present (Entity (Match))
1568 and then Nkind
1569 (Original_Node (Unit_Declaration_Node (Entity (Match))))
1570 = N_Expression_Function
1571 then
1572 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1573 end if;
1574
1575 when N_Formal_Type_Declaration =>
1576 Match :=
1577 Matching_Actual
1578 (Defining_Identifier (Formal),
1579 Defining_Identifier (Analyzed_Formal));
1580
1581 if No (Match) then
1582 if Partial_Parameterization then
1583 Process_Default (Formal);
1584
1585 else
1586 Error_Msg_Sloc := Sloc (Gen_Unit);
1587 Error_Msg_NE
1588 ("missing actual&",
1589 Instantiation_Node, Defining_Identifier (Formal));
1590 Error_Msg_NE
1591 ("\in instantiation of & declared#",
1592 Instantiation_Node, Gen_Unit);
1593 Abandon_Instantiation (Instantiation_Node);
1594 end if;
1595
1596 else
1597 Analyze (Match);
1598 Append_List
1599 (Instantiate_Type
1600 (Formal, Match, Analyzed_Formal, Assoc),
1601 Assoc);
1602
1603 -- An instantiation is a freeze point for the actuals,
1604 -- unless this is a rewritten formal package, or the
1605 -- formal is an Ada 2012 formal incomplete type.
1606
1607 if Nkind (I_Node) = N_Formal_Package_Declaration
1608 or else
1609 (Ada_Version >= Ada_2012
1610 and then
1611 Ekind (Defining_Identifier (Analyzed_Formal)) =
1612 E_Incomplete_Type)
1613 then
1614 null;
1615
1616 else
1617 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1618 end if;
1619 end if;
1620
1621 -- A remote access-to-class-wide type is not a legal actual
1622 -- for a generic formal of an access type (E.2.2(17/2)).
1623 -- In GNAT an exception to this rule is introduced when
1624 -- the formal is marked as remote using implementation
1625 -- defined aspect/pragma Remote_Access_Type. In that case
1626 -- the actual must be remote as well.
1627
1628 -- If the current instantiation is the construction of a
1629 -- local copy for a formal package the actuals may be
1630 -- defaulted, and there is no matching actual to check.
1631
1632 if Nkind (Analyzed_Formal) = N_Formal_Type_Declaration
1633 and then
1634 Nkind (Formal_Type_Definition (Analyzed_Formal)) =
1635 N_Access_To_Object_Definition
1636 and then Present (Match)
1637 then
1638 declare
1639 Formal_Ent : constant Entity_Id :=
1640 Defining_Identifier (Analyzed_Formal);
1641 begin
1642 if Is_Remote_Access_To_Class_Wide_Type (Entity (Match))
1643 = Is_Remote_Types (Formal_Ent)
1644 then
1645 -- Remoteness of formal and actual match
1646
1647 null;
1648
1649 elsif Is_Remote_Types (Formal_Ent) then
1650
1651 -- Remote formal, non-remote actual
1652
1653 Error_Msg_NE
1654 ("actual for& must be remote", Match, Formal_Ent);
1655
1656 else
1657 -- Non-remote formal, remote actual
1658
1659 Error_Msg_NE
1660 ("actual for& may not be remote",
1661 Match, Formal_Ent);
1662 end if;
1663 end;
1664 end if;
1665
1666 when N_Formal_Subprogram_Declaration =>
1667 Match :=
1668 Matching_Actual
1669 (Defining_Unit_Name (Specification (Formal)),
1670 Defining_Unit_Name (Specification (Analyzed_Formal)));
1671
1672 -- If the formal subprogram has the same name as another
1673 -- formal subprogram of the generic, then a named
1674 -- association is illegal (12.3(9)). Exclude named
1675 -- associations that are generated for a nested instance.
1676
1677 if Present (Match)
1678 and then Is_Named_Assoc
1679 and then Comes_From_Source (Found_Assoc)
1680 then
1681 Check_Overloaded_Formal_Subprogram (Formal);
1682 end if;
1683
1684 -- If there is no corresponding actual, this may be case
1685 -- of partial parameterization, or else the formal has a
1686 -- default or a box.
1687
1688 if No (Match) and then Partial_Parameterization then
1689 Process_Default (Formal);
1690
1691 if Nkind (I_Node) = N_Formal_Package_Declaration then
1692 Check_Overloaded_Formal_Subprogram (Formal);
1693 end if;
1694
1695 else
1696 Append_To (Assoc,
1697 Instantiate_Formal_Subprogram
1698 (Formal, Match, Analyzed_Formal));
1699
1700 -- An instantiation is a freeze point for the actuals,
1701 -- unless this is a rewritten formal package.
1702
1703 if Nkind (I_Node) /= N_Formal_Package_Declaration
1704 and then Nkind (Match) = N_Identifier
1705 and then Is_Subprogram (Entity (Match))
1706
1707 -- The actual subprogram may rename a routine defined
1708 -- in Standard. Avoid freezing such renamings because
1709 -- subprograms coming from Standard cannot be frozen.
1710
1711 and then
1712 not Renames_Standard_Subprogram (Entity (Match))
1713
1714 -- If the actual subprogram comes from a different
1715 -- unit, it is already frozen, either by a body in
1716 -- that unit or by the end of the declarative part
1717 -- of the unit. This check avoids the freezing of
1718 -- subprograms defined in Standard which are used
1719 -- as generic actuals.
1720
1721 and then In_Same_Code_Unit (Entity (Match), I_Node)
1722 and then Has_Fully_Defined_Profile (Entity (Match))
1723 then
1724 -- Mark the subprogram as having a delayed freeze
1725 -- since this may be an out-of-order action.
1726
1727 Set_Has_Delayed_Freeze (Entity (Match));
1728 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1729 end if;
1730 end if;
1731
1732 -- If this is a nested generic, preserve default for later
1733 -- instantiations. We do this as well for GNATProve use,
1734 -- so that the list of generic associations is complete.
1735
1736 if No (Match) and then Box_Present (Formal) then
1737 declare
1738 Subp : constant Entity_Id :=
1739 Defining_Unit_Name (Specification (Last (Assoc)));
1740
1741 begin
1742 Append_To (Default_Actuals,
1743 Make_Generic_Association (Sloc (I_Node),
1744 Selector_Name =>
1745 New_Occurrence_Of (Subp, Sloc (I_Node)),
1746 Explicit_Generic_Actual_Parameter =>
1747 New_Occurrence_Of (Subp, Sloc (I_Node))));
1748 end;
1749 end if;
1750
1751 when N_Formal_Package_Declaration =>
1752 Match :=
1753 Matching_Actual
1754 (Defining_Identifier (Formal),
1755 Defining_Identifier (Original_Node (Analyzed_Formal)));
1756
1757 if No (Match) then
1758 if Partial_Parameterization then
1759 Process_Default (Formal);
1760
1761 else
1762 Error_Msg_Sloc := Sloc (Gen_Unit);
1763 Error_Msg_NE
1764 ("missing actual&",
1765 Instantiation_Node, Defining_Identifier (Formal));
1766 Error_Msg_NE
1767 ("\in instantiation of & declared#",
1768 Instantiation_Node, Gen_Unit);
1769
1770 Abandon_Instantiation (Instantiation_Node);
1771 end if;
1772
1773 else
1774 Analyze (Match);
1775 Append_List
1776 (Instantiate_Formal_Package
1777 (Formal, Match, Analyzed_Formal),
1778 Assoc);
1779 end if;
1780
1781 -- For use type and use package appearing in the generic part,
1782 -- we have already copied them, so we can just move them where
1783 -- they belong (we mustn't recopy them since this would mess up
1784 -- the Sloc values).
1785
1786 when N_Use_Package_Clause |
1787 N_Use_Type_Clause =>
1788 if Nkind (Original_Node (I_Node)) =
1789 N_Formal_Package_Declaration
1790 then
1791 Append (New_Copy_Tree (Formal), Assoc);
1792 else
1793 Remove (Formal);
1794 Append (Formal, Assoc);
1795 end if;
1796
1797 when others =>
1798 raise Program_Error;
1799
1800 end case;
1801
1802 Formal := Saved_Formal;
1803 Next_Non_Pragma (Analyzed_Formal);
1804 end loop;
1805
1806 if Num_Actuals > Num_Matched then
1807 Error_Msg_Sloc := Sloc (Gen_Unit);
1808
1809 if Present (Selector_Name (Actual)) then
1810 Error_Msg_NE
1811 ("unmatched actual &", Actual, Selector_Name (Actual));
1812 Error_Msg_NE
1813 ("\in instantiation of & declared#", Actual, Gen_Unit);
1814 else
1815 Error_Msg_NE
1816 ("unmatched actual in instantiation of & declared#",
1817 Actual, Gen_Unit);
1818 end if;
1819 end if;
1820
1821 elsif Present (Actuals) then
1822 Error_Msg_N
1823 ("too many actuals in generic instantiation", Instantiation_Node);
1824 end if;
1825
1826 -- An instantiation freezes all generic actuals. The only exceptions
1827 -- to this are incomplete types and subprograms which are not fully
1828 -- defined at the point of instantiation.
1829
1830 declare
1831 Elmt : Elmt_Id := First_Elmt (Actuals_To_Freeze);
1832 begin
1833 while Present (Elmt) loop
1834 Freeze_Before (I_Node, Node (Elmt));
1835 Next_Elmt (Elmt);
1836 end loop;
1837 end;
1838
1839 -- If there are default subprograms, normalize the tree by adding
1840 -- explicit associations for them. This is required if the instance
1841 -- appears within a generic.
1842
1843 if not Is_Empty_List (Default_Actuals) then
1844 declare
1845 Default : Node_Id;
1846
1847 begin
1848 Default := First (Default_Actuals);
1849 while Present (Default) loop
1850 Mark_Rewrite_Insertion (Default);
1851 Next (Default);
1852 end loop;
1853
1854 if No (Actuals) then
1855 Set_Generic_Associations (I_Node, Default_Actuals);
1856 else
1857 Append_List_To (Actuals, Default_Actuals);
1858 end if;
1859 end;
1860 end if;
1861
1862 -- If this is a formal package, normalize the parameter list by adding
1863 -- explicit box associations for the formals that are covered by an
1864 -- Others_Choice.
1865
1866 if not Is_Empty_List (Default_Formals) then
1867 Append_List (Default_Formals, Formals);
1868 end if;
1869
1870 return Assoc;
1871 end Analyze_Associations;
1872
1873 -------------------------------
1874 -- Analyze_Formal_Array_Type --
1875 -------------------------------
1876
1877 procedure Analyze_Formal_Array_Type
1878 (T : in out Entity_Id;
1879 Def : Node_Id)
1880 is
1881 DSS : Node_Id;
1882
1883 begin
1884 -- Treated like a non-generic array declaration, with additional
1885 -- semantic checks.
1886
1887 Enter_Name (T);
1888
1889 if Nkind (Def) = N_Constrained_Array_Definition then
1890 DSS := First (Discrete_Subtype_Definitions (Def));
1891 while Present (DSS) loop
1892 if Nkind_In (DSS, N_Subtype_Indication,
1893 N_Range,
1894 N_Attribute_Reference)
1895 then
1896 Error_Msg_N ("only a subtype mark is allowed in a formal", DSS);
1897 end if;
1898
1899 Next (DSS);
1900 end loop;
1901 end if;
1902
1903 Array_Type_Declaration (T, Def);
1904 Set_Is_Generic_Type (Base_Type (T));
1905
1906 if Ekind (Component_Type (T)) = E_Incomplete_Type
1907 and then No (Full_View (Component_Type (T)))
1908 then
1909 Error_Msg_N ("premature usage of incomplete type", Def);
1910
1911 -- Check that range constraint is not allowed on the component type
1912 -- of a generic formal array type (AARM 12.5.3(3))
1913
1914 elsif Is_Internal (Component_Type (T))
1915 and then Present (Subtype_Indication (Component_Definition (Def)))
1916 and then Nkind (Original_Node
1917 (Subtype_Indication (Component_Definition (Def)))) =
1918 N_Subtype_Indication
1919 then
1920 Error_Msg_N
1921 ("in a formal, a subtype indication can only be "
1922 & "a subtype mark (RM 12.5.3(3))",
1923 Subtype_Indication (Component_Definition (Def)));
1924 end if;
1925
1926 end Analyze_Formal_Array_Type;
1927
1928 ---------------------------------------------
1929 -- Analyze_Formal_Decimal_Fixed_Point_Type --
1930 ---------------------------------------------
1931
1932 -- As for other generic types, we create a valid type representation with
1933 -- legal but arbitrary attributes, whose values are never considered
1934 -- static. For all scalar types we introduce an anonymous base type, with
1935 -- the same attributes. We choose the corresponding integer type to be
1936 -- Standard_Integer.
1937 -- Here and in other similar routines, the Sloc of the generated internal
1938 -- type must be the same as the sloc of the defining identifier of the
1939 -- formal type declaration, to provide proper source navigation.
1940
1941 procedure Analyze_Formal_Decimal_Fixed_Point_Type
1942 (T : Entity_Id;
1943 Def : Node_Id)
1944 is
1945 Loc : constant Source_Ptr := Sloc (Def);
1946
1947 Base : constant Entity_Id :=
1948 New_Internal_Entity
1949 (E_Decimal_Fixed_Point_Type,
1950 Current_Scope,
1951 Sloc (Defining_Identifier (Parent (Def))), 'G');
1952
1953 Int_Base : constant Entity_Id := Standard_Integer;
1954 Delta_Val : constant Ureal := Ureal_1;
1955 Digs_Val : constant Uint := Uint_6;
1956
1957 function Make_Dummy_Bound return Node_Id;
1958 -- Return a properly typed universal real literal to use as a bound
1959
1960 ----------------------
1961 -- Make_Dummy_Bound --
1962 ----------------------
1963
1964 function Make_Dummy_Bound return Node_Id is
1965 Bound : constant Node_Id := Make_Real_Literal (Loc, Ureal_1);
1966 begin
1967 Set_Etype (Bound, Universal_Real);
1968 return Bound;
1969 end Make_Dummy_Bound;
1970
1971 -- Start of processing for Analyze_Formal_Decimal_Fixed_Point_Type
1972
1973 begin
1974 Enter_Name (T);
1975
1976 Set_Etype (Base, Base);
1977 Set_Size_Info (Base, Int_Base);
1978 Set_RM_Size (Base, RM_Size (Int_Base));
1979 Set_First_Rep_Item (Base, First_Rep_Item (Int_Base));
1980 Set_Digits_Value (Base, Digs_Val);
1981 Set_Delta_Value (Base, Delta_Val);
1982 Set_Small_Value (Base, Delta_Val);
1983 Set_Scalar_Range (Base,
1984 Make_Range (Loc,
1985 Low_Bound => Make_Dummy_Bound,
1986 High_Bound => Make_Dummy_Bound));
1987
1988 Set_Is_Generic_Type (Base);
1989 Set_Parent (Base, Parent (Def));
1990
1991 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
1992 Set_Etype (T, Base);
1993 Set_Size_Info (T, Int_Base);
1994 Set_RM_Size (T, RM_Size (Int_Base));
1995 Set_First_Rep_Item (T, First_Rep_Item (Int_Base));
1996 Set_Digits_Value (T, Digs_Val);
1997 Set_Delta_Value (T, Delta_Val);
1998 Set_Small_Value (T, Delta_Val);
1999 Set_Scalar_Range (T, Scalar_Range (Base));
2000 Set_Is_Constrained (T);
2001
2002 Check_Restriction (No_Fixed_Point, Def);
2003 end Analyze_Formal_Decimal_Fixed_Point_Type;
2004
2005 -------------------------------------------
2006 -- Analyze_Formal_Derived_Interface_Type --
2007 -------------------------------------------
2008
2009 procedure Analyze_Formal_Derived_Interface_Type
2010 (N : Node_Id;
2011 T : Entity_Id;
2012 Def : Node_Id)
2013 is
2014 Loc : constant Source_Ptr := Sloc (Def);
2015
2016 begin
2017 -- Rewrite as a type declaration of a derived type. This ensures that
2018 -- the interface list and primitive operations are properly captured.
2019
2020 Rewrite (N,
2021 Make_Full_Type_Declaration (Loc,
2022 Defining_Identifier => T,
2023 Type_Definition => Def));
2024 Analyze (N);
2025 Set_Is_Generic_Type (T);
2026 end Analyze_Formal_Derived_Interface_Type;
2027
2028 ---------------------------------
2029 -- Analyze_Formal_Derived_Type --
2030 ---------------------------------
2031
2032 procedure Analyze_Formal_Derived_Type
2033 (N : Node_Id;
2034 T : Entity_Id;
2035 Def : Node_Id)
2036 is
2037 Loc : constant Source_Ptr := Sloc (Def);
2038 Unk_Disc : constant Boolean := Unknown_Discriminants_Present (N);
2039 New_N : Node_Id;
2040
2041 begin
2042 Set_Is_Generic_Type (T);
2043
2044 if Private_Present (Def) then
2045 New_N :=
2046 Make_Private_Extension_Declaration (Loc,
2047 Defining_Identifier => T,
2048 Discriminant_Specifications => Discriminant_Specifications (N),
2049 Unknown_Discriminants_Present => Unk_Disc,
2050 Subtype_Indication => Subtype_Mark (Def),
2051 Interface_List => Interface_List (Def));
2052
2053 Set_Abstract_Present (New_N, Abstract_Present (Def));
2054 Set_Limited_Present (New_N, Limited_Present (Def));
2055 Set_Synchronized_Present (New_N, Synchronized_Present (Def));
2056
2057 else
2058 New_N :=
2059 Make_Full_Type_Declaration (Loc,
2060 Defining_Identifier => T,
2061 Discriminant_Specifications =>
2062 Discriminant_Specifications (Parent (T)),
2063 Type_Definition =>
2064 Make_Derived_Type_Definition (Loc,
2065 Subtype_Indication => Subtype_Mark (Def)));
2066
2067 Set_Abstract_Present
2068 (Type_Definition (New_N), Abstract_Present (Def));
2069 Set_Limited_Present
2070 (Type_Definition (New_N), Limited_Present (Def));
2071 end if;
2072
2073 Rewrite (N, New_N);
2074 Analyze (N);
2075
2076 if Unk_Disc then
2077 if not Is_Composite_Type (T) then
2078 Error_Msg_N
2079 ("unknown discriminants not allowed for elementary types", N);
2080 else
2081 Set_Has_Unknown_Discriminants (T);
2082 Set_Is_Constrained (T, False);
2083 end if;
2084 end if;
2085
2086 -- If the parent type has a known size, so does the formal, which makes
2087 -- legal representation clauses that involve the formal.
2088
2089 Set_Size_Known_At_Compile_Time
2090 (T, Size_Known_At_Compile_Time (Entity (Subtype_Mark (Def))));
2091 end Analyze_Formal_Derived_Type;
2092
2093 ----------------------------------
2094 -- Analyze_Formal_Discrete_Type --
2095 ----------------------------------
2096
2097 -- The operations defined for a discrete types are those of an enumeration
2098 -- type. The size is set to an arbitrary value, for use in analyzing the
2099 -- generic unit.
2100
2101 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id) is
2102 Loc : constant Source_Ptr := Sloc (Def);
2103 Lo : Node_Id;
2104 Hi : Node_Id;
2105
2106 Base : constant Entity_Id :=
2107 New_Internal_Entity
2108 (E_Floating_Point_Type, Current_Scope,
2109 Sloc (Defining_Identifier (Parent (Def))), 'G');
2110
2111 begin
2112 Enter_Name (T);
2113 Set_Ekind (T, E_Enumeration_Subtype);
2114 Set_Etype (T, Base);
2115 Init_Size (T, 8);
2116 Init_Alignment (T);
2117 Set_Is_Generic_Type (T);
2118 Set_Is_Constrained (T);
2119
2120 -- For semantic analysis, the bounds of the type must be set to some
2121 -- non-static value. The simplest is to create attribute nodes for those
2122 -- bounds, that refer to the type itself. These bounds are never
2123 -- analyzed but serve as place-holders.
2124
2125 Lo :=
2126 Make_Attribute_Reference (Loc,
2127 Attribute_Name => Name_First,
2128 Prefix => New_Occurrence_Of (T, Loc));
2129 Set_Etype (Lo, T);
2130
2131 Hi :=
2132 Make_Attribute_Reference (Loc,
2133 Attribute_Name => Name_Last,
2134 Prefix => New_Occurrence_Of (T, Loc));
2135 Set_Etype (Hi, T);
2136
2137 Set_Scalar_Range (T,
2138 Make_Range (Loc,
2139 Low_Bound => Lo,
2140 High_Bound => Hi));
2141
2142 Set_Ekind (Base, E_Enumeration_Type);
2143 Set_Etype (Base, Base);
2144 Init_Size (Base, 8);
2145 Init_Alignment (Base);
2146 Set_Is_Generic_Type (Base);
2147 Set_Scalar_Range (Base, Scalar_Range (T));
2148 Set_Parent (Base, Parent (Def));
2149 end Analyze_Formal_Discrete_Type;
2150
2151 ----------------------------------
2152 -- Analyze_Formal_Floating_Type --
2153 ---------------------------------
2154
2155 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id) is
2156 Base : constant Entity_Id :=
2157 New_Internal_Entity
2158 (E_Floating_Point_Type, Current_Scope,
2159 Sloc (Defining_Identifier (Parent (Def))), 'G');
2160
2161 begin
2162 -- The various semantic attributes are taken from the predefined type
2163 -- Float, just so that all of them are initialized. Their values are
2164 -- never used because no constant folding or expansion takes place in
2165 -- the generic itself.
2166
2167 Enter_Name (T);
2168 Set_Ekind (T, E_Floating_Point_Subtype);
2169 Set_Etype (T, Base);
2170 Set_Size_Info (T, (Standard_Float));
2171 Set_RM_Size (T, RM_Size (Standard_Float));
2172 Set_Digits_Value (T, Digits_Value (Standard_Float));
2173 Set_Scalar_Range (T, Scalar_Range (Standard_Float));
2174 Set_Is_Constrained (T);
2175
2176 Set_Is_Generic_Type (Base);
2177 Set_Etype (Base, Base);
2178 Set_Size_Info (Base, (Standard_Float));
2179 Set_RM_Size (Base, RM_Size (Standard_Float));
2180 Set_Digits_Value (Base, Digits_Value (Standard_Float));
2181 Set_Scalar_Range (Base, Scalar_Range (Standard_Float));
2182 Set_Parent (Base, Parent (Def));
2183
2184 Check_Restriction (No_Floating_Point, Def);
2185 end Analyze_Formal_Floating_Type;
2186
2187 -----------------------------------
2188 -- Analyze_Formal_Interface_Type;--
2189 -----------------------------------
2190
2191 procedure Analyze_Formal_Interface_Type
2192 (N : Node_Id;
2193 T : Entity_Id;
2194 Def : Node_Id)
2195 is
2196 Loc : constant Source_Ptr := Sloc (N);
2197 New_N : Node_Id;
2198
2199 begin
2200 New_N :=
2201 Make_Full_Type_Declaration (Loc,
2202 Defining_Identifier => T,
2203 Type_Definition => Def);
2204
2205 Rewrite (N, New_N);
2206 Analyze (N);
2207 Set_Is_Generic_Type (T);
2208 end Analyze_Formal_Interface_Type;
2209
2210 ---------------------------------
2211 -- Analyze_Formal_Modular_Type --
2212 ---------------------------------
2213
2214 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id) is
2215 begin
2216 -- Apart from their entity kind, generic modular types are treated like
2217 -- signed integer types, and have the same attributes.
2218
2219 Analyze_Formal_Signed_Integer_Type (T, Def);
2220 Set_Ekind (T, E_Modular_Integer_Subtype);
2221 Set_Ekind (Etype (T), E_Modular_Integer_Type);
2222
2223 end Analyze_Formal_Modular_Type;
2224
2225 ---------------------------------------
2226 -- Analyze_Formal_Object_Declaration --
2227 ---------------------------------------
2228
2229 procedure Analyze_Formal_Object_Declaration (N : Node_Id) is
2230 E : constant Node_Id := Default_Expression (N);
2231 Id : constant Node_Id := Defining_Identifier (N);
2232 K : Entity_Kind;
2233 T : Node_Id;
2234
2235 begin
2236 Enter_Name (Id);
2237
2238 -- Determine the mode of the formal object
2239
2240 if Out_Present (N) then
2241 K := E_Generic_In_Out_Parameter;
2242
2243 if not In_Present (N) then
2244 Error_Msg_N ("formal generic objects cannot have mode OUT", N);
2245 end if;
2246
2247 else
2248 K := E_Generic_In_Parameter;
2249 end if;
2250
2251 if Present (Subtype_Mark (N)) then
2252 Find_Type (Subtype_Mark (N));
2253 T := Entity (Subtype_Mark (N));
2254
2255 -- Verify that there is no redundant null exclusion
2256
2257 if Null_Exclusion_Present (N) then
2258 if not Is_Access_Type (T) then
2259 Error_Msg_N
2260 ("null exclusion can only apply to an access type", N);
2261
2262 elsif Can_Never_Be_Null (T) then
2263 Error_Msg_NE
2264 ("`NOT NULL` not allowed (& already excludes null)", N, T);
2265 end if;
2266 end if;
2267
2268 -- Ada 2005 (AI-423): Formal object with an access definition
2269
2270 else
2271 Check_Access_Definition (N);
2272 T := Access_Definition
2273 (Related_Nod => N,
2274 N => Access_Definition (N));
2275 end if;
2276
2277 if Ekind (T) = E_Incomplete_Type then
2278 declare
2279 Error_Node : Node_Id;
2280
2281 begin
2282 if Present (Subtype_Mark (N)) then
2283 Error_Node := Subtype_Mark (N);
2284 else
2285 Check_Access_Definition (N);
2286 Error_Node := Access_Definition (N);
2287 end if;
2288
2289 Error_Msg_N ("premature usage of incomplete type", Error_Node);
2290 end;
2291 end if;
2292
2293 if K = E_Generic_In_Parameter then
2294
2295 -- Ada 2005 (AI-287): Limited aggregates allowed in generic formals
2296
2297 if Ada_Version < Ada_2005 and then Is_Limited_Type (T) then
2298 Error_Msg_N
2299 ("generic formal of mode IN must not be of limited type", N);
2300 Explain_Limited_Type (T, N);
2301 end if;
2302
2303 if Is_Abstract_Type (T) then
2304 Error_Msg_N
2305 ("generic formal of mode IN must not be of abstract type", N);
2306 end if;
2307
2308 if Present (E) then
2309 Preanalyze_Spec_Expression (E, T);
2310
2311 if Is_Limited_Type (T) and then not OK_For_Limited_Init (T, E) then
2312 Error_Msg_N
2313 ("initialization not allowed for limited types", E);
2314 Explain_Limited_Type (T, E);
2315 end if;
2316 end if;
2317
2318 Set_Ekind (Id, K);
2319 Set_Etype (Id, T);
2320
2321 -- Case of generic IN OUT parameter
2322
2323 else
2324 -- If the formal has an unconstrained type, construct its actual
2325 -- subtype, as is done for subprogram formals. In this fashion, all
2326 -- its uses can refer to specific bounds.
2327
2328 Set_Ekind (Id, K);
2329 Set_Etype (Id, T);
2330
2331 if (Is_Array_Type (T) and then not Is_Constrained (T))
2332 or else (Ekind (T) = E_Record_Type and then Has_Discriminants (T))
2333 then
2334 declare
2335 Non_Freezing_Ref : constant Node_Id :=
2336 New_Occurrence_Of (Id, Sloc (Id));
2337 Decl : Node_Id;
2338
2339 begin
2340 -- Make sure the actual subtype doesn't generate bogus freezing
2341
2342 Set_Must_Not_Freeze (Non_Freezing_Ref);
2343 Decl := Build_Actual_Subtype (T, Non_Freezing_Ref);
2344 Insert_Before_And_Analyze (N, Decl);
2345 Set_Actual_Subtype (Id, Defining_Identifier (Decl));
2346 end;
2347 else
2348 Set_Actual_Subtype (Id, T);
2349 end if;
2350
2351 if Present (E) then
2352 Error_Msg_N
2353 ("initialization not allowed for `IN OUT` formals", N);
2354 end if;
2355 end if;
2356
2357 if Has_Aspects (N) then
2358 Analyze_Aspect_Specifications (N, Id);
2359 end if;
2360 end Analyze_Formal_Object_Declaration;
2361
2362 ----------------------------------------------
2363 -- Analyze_Formal_Ordinary_Fixed_Point_Type --
2364 ----------------------------------------------
2365
2366 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
2367 (T : Entity_Id;
2368 Def : Node_Id)
2369 is
2370 Loc : constant Source_Ptr := Sloc (Def);
2371 Base : constant Entity_Id :=
2372 New_Internal_Entity
2373 (E_Ordinary_Fixed_Point_Type, Current_Scope,
2374 Sloc (Defining_Identifier (Parent (Def))), 'G');
2375
2376 begin
2377 -- The semantic attributes are set for completeness only, their values
2378 -- will never be used, since all properties of the type are non-static.
2379
2380 Enter_Name (T);
2381 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
2382 Set_Etype (T, Base);
2383 Set_Size_Info (T, Standard_Integer);
2384 Set_RM_Size (T, RM_Size (Standard_Integer));
2385 Set_Small_Value (T, Ureal_1);
2386 Set_Delta_Value (T, Ureal_1);
2387 Set_Scalar_Range (T,
2388 Make_Range (Loc,
2389 Low_Bound => Make_Real_Literal (Loc, Ureal_1),
2390 High_Bound => Make_Real_Literal (Loc, Ureal_1)));
2391 Set_Is_Constrained (T);
2392
2393 Set_Is_Generic_Type (Base);
2394 Set_Etype (Base, Base);
2395 Set_Size_Info (Base, Standard_Integer);
2396 Set_RM_Size (Base, RM_Size (Standard_Integer));
2397 Set_Small_Value (Base, Ureal_1);
2398 Set_Delta_Value (Base, Ureal_1);
2399 Set_Scalar_Range (Base, Scalar_Range (T));
2400 Set_Parent (Base, Parent (Def));
2401
2402 Check_Restriction (No_Fixed_Point, Def);
2403 end Analyze_Formal_Ordinary_Fixed_Point_Type;
2404
2405 ----------------------------------------
2406 -- Analyze_Formal_Package_Declaration --
2407 ----------------------------------------
2408
2409 procedure Analyze_Formal_Package_Declaration (N : Node_Id) is
2410 Gen_Id : constant Node_Id := Name (N);
2411 Loc : constant Source_Ptr := Sloc (N);
2412 Pack_Id : constant Entity_Id := Defining_Identifier (N);
2413 Formal : Entity_Id;
2414 Gen_Decl : Node_Id;
2415 Gen_Unit : Entity_Id;
2416 Renaming : Node_Id;
2417
2418 Vis_Prims_List : Elist_Id := No_Elist;
2419 -- List of primitives made temporarily visible in the instantiation
2420 -- to match the visibility of the formal type.
2421
2422 function Build_Local_Package return Node_Id;
2423 -- The formal package is rewritten so that its parameters are replaced
2424 -- with corresponding declarations. For parameters with bona fide
2425 -- associations these declarations are created by Analyze_Associations
2426 -- as for a regular instantiation. For boxed parameters, we preserve
2427 -- the formal declarations and analyze them, in order to introduce
2428 -- entities of the right kind in the environment of the formal.
2429
2430 -------------------------
2431 -- Build_Local_Package --
2432 -------------------------
2433
2434 function Build_Local_Package return Node_Id is
2435 Decls : List_Id;
2436 Pack_Decl : Node_Id;
2437
2438 begin
2439 -- Within the formal, the name of the generic package is a renaming
2440 -- of the formal (as for a regular instantiation).
2441
2442 Pack_Decl :=
2443 Make_Package_Declaration (Loc,
2444 Specification =>
2445 Copy_Generic_Node
2446 (Specification (Original_Node (Gen_Decl)),
2447 Empty, Instantiating => True));
2448
2449 Renaming :=
2450 Make_Package_Renaming_Declaration (Loc,
2451 Defining_Unit_Name =>
2452 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
2453 Name => New_Occurrence_Of (Formal, Loc));
2454
2455 if Nkind (Gen_Id) = N_Identifier
2456 and then Chars (Gen_Id) = Chars (Pack_Id)
2457 then
2458 Error_Msg_NE
2459 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
2460 end if;
2461
2462 -- If the formal is declared with a box, or with an others choice,
2463 -- create corresponding declarations for all entities in the formal
2464 -- part, so that names with the proper types are available in the
2465 -- specification of the formal package.
2466
2467 -- On the other hand, if there are no associations, then all the
2468 -- formals must have defaults, and this will be checked by the
2469 -- call to Analyze_Associations.
2470
2471 if Box_Present (N)
2472 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2473 then
2474 declare
2475 Formal_Decl : Node_Id;
2476
2477 begin
2478 -- TBA : for a formal package, need to recurse ???
2479
2480 Decls := New_List;
2481 Formal_Decl :=
2482 First
2483 (Generic_Formal_Declarations (Original_Node (Gen_Decl)));
2484 while Present (Formal_Decl) loop
2485 Append_To
2486 (Decls, Copy_Generic_Node (Formal_Decl, Empty, True));
2487 Next (Formal_Decl);
2488 end loop;
2489 end;
2490
2491 -- If generic associations are present, use Analyze_Associations to
2492 -- create the proper renaming declarations.
2493
2494 else
2495 declare
2496 Act_Tree : constant Node_Id :=
2497 Copy_Generic_Node
2498 (Original_Node (Gen_Decl), Empty,
2499 Instantiating => True);
2500
2501 begin
2502 Generic_Renamings.Set_Last (0);
2503 Generic_Renamings_HTable.Reset;
2504 Instantiation_Node := N;
2505
2506 Decls :=
2507 Analyze_Associations
2508 (I_Node => Original_Node (N),
2509 Formals => Generic_Formal_Declarations (Act_Tree),
2510 F_Copy => Generic_Formal_Declarations (Gen_Decl));
2511
2512 Vis_Prims_List := Check_Hidden_Primitives (Decls);
2513 end;
2514 end if;
2515
2516 Append (Renaming, To => Decls);
2517
2518 -- Add generated declarations ahead of local declarations in
2519 -- the package.
2520
2521 if No (Visible_Declarations (Specification (Pack_Decl))) then
2522 Set_Visible_Declarations (Specification (Pack_Decl), Decls);
2523 else
2524 Insert_List_Before
2525 (First (Visible_Declarations (Specification (Pack_Decl))),
2526 Decls);
2527 end if;
2528
2529 return Pack_Decl;
2530 end Build_Local_Package;
2531
2532 -- Local variables
2533
2534 Save_IPSM : constant Boolean := Ignore_Pragma_SPARK_Mode;
2535 -- Save flag Ignore_Pragma_SPARK_Mode for restore on exit
2536
2537 Associations : Boolean := True;
2538 New_N : Node_Id;
2539 Parent_Installed : Boolean := False;
2540 Parent_Instance : Entity_Id;
2541 Renaming_In_Par : Entity_Id;
2542
2543 -- Start of processing for Analyze_Formal_Package_Declaration
2544
2545 begin
2546 Check_Text_IO_Special_Unit (Gen_Id);
2547
2548 Init_Env;
2549 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
2550 Gen_Unit := Entity (Gen_Id);
2551
2552 -- Check for a formal package that is a package renaming
2553
2554 if Present (Renamed_Object (Gen_Unit)) then
2555
2556 -- Indicate that unit is used, before replacing it with renamed
2557 -- entity for use below.
2558
2559 if In_Extended_Main_Source_Unit (N) then
2560 Set_Is_Instantiated (Gen_Unit);
2561 Generate_Reference (Gen_Unit, N);
2562 end if;
2563
2564 Gen_Unit := Renamed_Object (Gen_Unit);
2565 end if;
2566
2567 if Ekind (Gen_Unit) /= E_Generic_Package then
2568 Error_Msg_N ("expect generic package name", Gen_Id);
2569 Restore_Env;
2570 goto Leave;
2571
2572 elsif Gen_Unit = Current_Scope then
2573 Error_Msg_N
2574 ("generic package cannot be used as a formal package of itself",
2575 Gen_Id);
2576 Restore_Env;
2577 goto Leave;
2578
2579 elsif In_Open_Scopes (Gen_Unit) then
2580 if Is_Compilation_Unit (Gen_Unit)
2581 and then Is_Child_Unit (Current_Scope)
2582 then
2583 -- Special-case the error when the formal is a parent, and
2584 -- continue analysis to minimize cascaded errors.
2585
2586 Error_Msg_N
2587 ("generic parent cannot be used as formal package "
2588 & "of a child unit", Gen_Id);
2589
2590 else
2591 Error_Msg_N
2592 ("generic package cannot be used as a formal package "
2593 & "within itself", Gen_Id);
2594 Restore_Env;
2595 goto Leave;
2596 end if;
2597 end if;
2598
2599 -- Check that name of formal package does not hide name of generic,
2600 -- or its leading prefix. This check must be done separately because
2601 -- the name of the generic has already been analyzed.
2602
2603 declare
2604 Gen_Name : Entity_Id;
2605
2606 begin
2607 Gen_Name := Gen_Id;
2608 while Nkind (Gen_Name) = N_Expanded_Name loop
2609 Gen_Name := Prefix (Gen_Name);
2610 end loop;
2611
2612 if Chars (Gen_Name) = Chars (Pack_Id) then
2613 Error_Msg_NE
2614 ("& is hidden within declaration of formal package",
2615 Gen_Id, Gen_Name);
2616 end if;
2617 end;
2618
2619 if Box_Present (N)
2620 or else No (Generic_Associations (N))
2621 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2622 then
2623 Associations := False;
2624 end if;
2625
2626 -- If there are no generic associations, the generic parameters appear
2627 -- as local entities and are instantiated like them. We copy the generic
2628 -- package declaration as if it were an instantiation, and analyze it
2629 -- like a regular package, except that we treat the formals as
2630 -- additional visible components.
2631
2632 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
2633
2634 if In_Extended_Main_Source_Unit (N) then
2635 Set_Is_Instantiated (Gen_Unit);
2636 Generate_Reference (Gen_Unit, N);
2637 end if;
2638
2639 Formal := New_Copy (Pack_Id);
2640 Create_Instantiation_Source (N, Gen_Unit, False, S_Adjustment);
2641
2642 -- Make local generic without formals. The formals will be replaced with
2643 -- internal declarations.
2644
2645 begin
2646 New_N := Build_Local_Package;
2647
2648 -- If there are errors in the parameter list, Analyze_Associations
2649 -- raises Instantiation_Error. Patch the declaration to prevent further
2650 -- exception propagation.
2651
2652 exception
2653 when Instantiation_Error =>
2654 Enter_Name (Formal);
2655 Set_Ekind (Formal, E_Variable);
2656 Set_Etype (Formal, Any_Type);
2657 Restore_Hidden_Primitives (Vis_Prims_List);
2658
2659 if Parent_Installed then
2660 Remove_Parent;
2661 end if;
2662
2663 goto Leave;
2664 end;
2665
2666 Rewrite (N, New_N);
2667 Set_Defining_Unit_Name (Specification (New_N), Formal);
2668 Set_Generic_Parent (Specification (N), Gen_Unit);
2669 Set_Instance_Env (Gen_Unit, Formal);
2670 Set_Is_Generic_Instance (Formal);
2671
2672 Enter_Name (Formal);
2673 Set_Ekind (Formal, E_Package);
2674 Set_Etype (Formal, Standard_Void_Type);
2675 Set_Inner_Instances (Formal, New_Elmt_List);
2676 Push_Scope (Formal);
2677
2678 -- Manually set the SPARK_Mode from the context because the package
2679 -- declaration is never analyzed.
2680
2681 Set_SPARK_Pragma (Formal, SPARK_Mode_Pragma);
2682 Set_SPARK_Aux_Pragma (Formal, SPARK_Mode_Pragma);
2683 Set_SPARK_Pragma_Inherited (Formal);
2684 Set_SPARK_Aux_Pragma_Inherited (Formal);
2685
2686 if Is_Child_Unit (Gen_Unit) and then Parent_Installed then
2687
2688 -- Similarly, we have to make the name of the formal visible in the
2689 -- parent instance, to resolve properly fully qualified names that
2690 -- may appear in the generic unit. The parent instance has been
2691 -- placed on the scope stack ahead of the current scope.
2692
2693 Parent_Instance := Scope_Stack.Table (Scope_Stack.Last - 1).Entity;
2694
2695 Renaming_In_Par :=
2696 Make_Defining_Identifier (Loc, Chars (Gen_Unit));
2697 Set_Ekind (Renaming_In_Par, E_Package);
2698 Set_Etype (Renaming_In_Par, Standard_Void_Type);
2699 Set_Scope (Renaming_In_Par, Parent_Instance);
2700 Set_Parent (Renaming_In_Par, Parent (Formal));
2701 Set_Renamed_Object (Renaming_In_Par, Formal);
2702 Append_Entity (Renaming_In_Par, Parent_Instance);
2703 end if;
2704
2705 -- A formal package declaration behaves as a package instantiation with
2706 -- respect to SPARK_Mode "off". If the annotation is "off" or altogether
2707 -- missing, set the global flag which signals Analyze_Pragma to ingnore
2708 -- all SPARK_Mode pragmas within the generic_package_name.
2709
2710 if SPARK_Mode /= On then
2711 Ignore_Pragma_SPARK_Mode := True;
2712 end if;
2713
2714 Analyze (Specification (N));
2715
2716 -- The formals for which associations are provided are not visible
2717 -- outside of the formal package. The others are still declared by a
2718 -- formal parameter declaration.
2719
2720 -- If there are no associations, the only local entity to hide is the
2721 -- generated package renaming itself.
2722
2723 declare
2724 E : Entity_Id;
2725
2726 begin
2727 E := First_Entity (Formal);
2728 while Present (E) loop
2729 if Associations and then not Is_Generic_Formal (E) then
2730 Set_Is_Hidden (E);
2731 end if;
2732
2733 if Ekind (E) = E_Package and then Renamed_Entity (E) = Formal then
2734 Set_Is_Hidden (E);
2735 exit;
2736 end if;
2737
2738 Next_Entity (E);
2739 end loop;
2740 end;
2741
2742 End_Package_Scope (Formal);
2743 Restore_Hidden_Primitives (Vis_Prims_List);
2744
2745 if Parent_Installed then
2746 Remove_Parent;
2747 end if;
2748
2749 Restore_Env;
2750
2751 -- Inside the generic unit, the formal package is a regular package, but
2752 -- no body is needed for it. Note that after instantiation, the defining
2753 -- unit name we need is in the new tree and not in the original (see
2754 -- Package_Instantiation). A generic formal package is an instance, and
2755 -- can be used as an actual for an inner instance.
2756
2757 Set_Has_Completion (Formal, True);
2758
2759 -- Add semantic information to the original defining identifier for ASIS
2760 -- use.
2761
2762 Set_Ekind (Pack_Id, E_Package);
2763 Set_Etype (Pack_Id, Standard_Void_Type);
2764 Set_Scope (Pack_Id, Scope (Formal));
2765 Set_Has_Completion (Pack_Id, True);
2766
2767 <<Leave>>
2768 if Has_Aspects (N) then
2769 Analyze_Aspect_Specifications (N, Pack_Id);
2770 end if;
2771
2772 Ignore_Pragma_SPARK_Mode := Save_IPSM;
2773 end Analyze_Formal_Package_Declaration;
2774
2775 ---------------------------------
2776 -- Analyze_Formal_Private_Type --
2777 ---------------------------------
2778
2779 procedure Analyze_Formal_Private_Type
2780 (N : Node_Id;
2781 T : Entity_Id;
2782 Def : Node_Id)
2783 is
2784 begin
2785 New_Private_Type (N, T, Def);
2786
2787 -- Set the size to an arbitrary but legal value
2788
2789 Set_Size_Info (T, Standard_Integer);
2790 Set_RM_Size (T, RM_Size (Standard_Integer));
2791 end Analyze_Formal_Private_Type;
2792
2793 ------------------------------------
2794 -- Analyze_Formal_Incomplete_Type --
2795 ------------------------------------
2796
2797 procedure Analyze_Formal_Incomplete_Type
2798 (T : Entity_Id;
2799 Def : Node_Id)
2800 is
2801 begin
2802 Enter_Name (T);
2803 Set_Ekind (T, E_Incomplete_Type);
2804 Set_Etype (T, T);
2805 Set_Private_Dependents (T, New_Elmt_List);
2806
2807 if Tagged_Present (Def) then
2808 Set_Is_Tagged_Type (T);
2809 Make_Class_Wide_Type (T);
2810 Set_Direct_Primitive_Operations (T, New_Elmt_List);
2811 end if;
2812 end Analyze_Formal_Incomplete_Type;
2813
2814 ----------------------------------------
2815 -- Analyze_Formal_Signed_Integer_Type --
2816 ----------------------------------------
2817
2818 procedure Analyze_Formal_Signed_Integer_Type
2819 (T : Entity_Id;
2820 Def : Node_Id)
2821 is
2822 Base : constant Entity_Id :=
2823 New_Internal_Entity
2824 (E_Signed_Integer_Type,
2825 Current_Scope,
2826 Sloc (Defining_Identifier (Parent (Def))), 'G');
2827
2828 begin
2829 Enter_Name (T);
2830
2831 Set_Ekind (T, E_Signed_Integer_Subtype);
2832 Set_Etype (T, Base);
2833 Set_Size_Info (T, Standard_Integer);
2834 Set_RM_Size (T, RM_Size (Standard_Integer));
2835 Set_Scalar_Range (T, Scalar_Range (Standard_Integer));
2836 Set_Is_Constrained (T);
2837
2838 Set_Is_Generic_Type (Base);
2839 Set_Size_Info (Base, Standard_Integer);
2840 Set_RM_Size (Base, RM_Size (Standard_Integer));
2841 Set_Etype (Base, Base);
2842 Set_Scalar_Range (Base, Scalar_Range (Standard_Integer));
2843 Set_Parent (Base, Parent (Def));
2844 end Analyze_Formal_Signed_Integer_Type;
2845
2846 -------------------------------------------
2847 -- Analyze_Formal_Subprogram_Declaration --
2848 -------------------------------------------
2849
2850 procedure Analyze_Formal_Subprogram_Declaration (N : Node_Id) is
2851 Spec : constant Node_Id := Specification (N);
2852 Def : constant Node_Id := Default_Name (N);
2853 Nam : constant Entity_Id := Defining_Unit_Name (Spec);
2854 Subp : Entity_Id;
2855
2856 begin
2857 if Nam = Error then
2858 return;
2859 end if;
2860
2861 if Nkind (Nam) = N_Defining_Program_Unit_Name then
2862 Error_Msg_N ("name of formal subprogram must be a direct name", Nam);
2863 goto Leave;
2864 end if;
2865
2866 Analyze_Subprogram_Declaration (N);
2867 Set_Is_Formal_Subprogram (Nam);
2868 Set_Has_Completion (Nam);
2869
2870 if Nkind (N) = N_Formal_Abstract_Subprogram_Declaration then
2871 Set_Is_Abstract_Subprogram (Nam);
2872
2873 Set_Is_Dispatching_Operation (Nam);
2874
2875 -- A formal abstract procedure cannot have a null default
2876 -- (RM 12.6(4.1/2)).
2877
2878 if Nkind (Spec) = N_Procedure_Specification
2879 and then Null_Present (Spec)
2880 then
2881 Error_Msg_N
2882 ("a formal abstract subprogram cannot default to null", Spec);
2883 end if;
2884
2885 declare
2886 Ctrl_Type : constant Entity_Id := Find_Dispatching_Type (Nam);
2887 begin
2888 if No (Ctrl_Type) then
2889 Error_Msg_N
2890 ("abstract formal subprogram must have a controlling type",
2891 N);
2892
2893 elsif Ada_Version >= Ada_2012
2894 and then Is_Incomplete_Type (Ctrl_Type)
2895 then
2896 Error_Msg_NE
2897 ("controlling type of abstract formal subprogram cannot "
2898 & "be incomplete type", N, Ctrl_Type);
2899
2900 else
2901 Check_Controlling_Formals (Ctrl_Type, Nam);
2902 end if;
2903 end;
2904 end if;
2905
2906 -- Default name is resolved at the point of instantiation
2907
2908 if Box_Present (N) then
2909 null;
2910
2911 -- Else default is bound at the point of generic declaration
2912
2913 elsif Present (Def) then
2914 if Nkind (Def) = N_Operator_Symbol then
2915 Find_Direct_Name (Def);
2916
2917 elsif Nkind (Def) /= N_Attribute_Reference then
2918 Analyze (Def);
2919
2920 else
2921 -- For an attribute reference, analyze the prefix and verify
2922 -- that it has the proper profile for the subprogram.
2923
2924 Analyze (Prefix (Def));
2925 Valid_Default_Attribute (Nam, Def);
2926 goto Leave;
2927 end if;
2928
2929 -- Default name may be overloaded, in which case the interpretation
2930 -- with the correct profile must be selected, as for a renaming.
2931 -- If the definition is an indexed component, it must denote a
2932 -- member of an entry family. If it is a selected component, it
2933 -- can be a protected operation.
2934
2935 if Etype (Def) = Any_Type then
2936 goto Leave;
2937
2938 elsif Nkind (Def) = N_Selected_Component then
2939 if not Is_Overloadable (Entity (Selector_Name (Def))) then
2940 Error_Msg_N ("expect valid subprogram name as default", Def);
2941 end if;
2942
2943 elsif Nkind (Def) = N_Indexed_Component then
2944 if Is_Entity_Name (Prefix (Def)) then
2945 if Ekind (Entity (Prefix (Def))) /= E_Entry_Family then
2946 Error_Msg_N ("expect valid subprogram name as default", Def);
2947 end if;
2948
2949 elsif Nkind (Prefix (Def)) = N_Selected_Component then
2950 if Ekind (Entity (Selector_Name (Prefix (Def)))) /=
2951 E_Entry_Family
2952 then
2953 Error_Msg_N ("expect valid subprogram name as default", Def);
2954 end if;
2955
2956 else
2957 Error_Msg_N ("expect valid subprogram name as default", Def);
2958 goto Leave;
2959 end if;
2960
2961 elsif Nkind (Def) = N_Character_Literal then
2962
2963 -- Needs some type checks: subprogram should be parameterless???
2964
2965 Resolve (Def, (Etype (Nam)));
2966
2967 elsif not Is_Entity_Name (Def)
2968 or else not Is_Overloadable (Entity (Def))
2969 then
2970 Error_Msg_N ("expect valid subprogram name as default", Def);
2971 goto Leave;
2972
2973 elsif not Is_Overloaded (Def) then
2974 Subp := Entity (Def);
2975
2976 if Subp = Nam then
2977 Error_Msg_N ("premature usage of formal subprogram", Def);
2978
2979 elsif not Entity_Matches_Spec (Subp, Nam) then
2980 Error_Msg_N ("no visible entity matches specification", Def);
2981 end if;
2982
2983 -- More than one interpretation, so disambiguate as for a renaming
2984
2985 else
2986 declare
2987 I : Interp_Index;
2988 I1 : Interp_Index := 0;
2989 It : Interp;
2990 It1 : Interp;
2991
2992 begin
2993 Subp := Any_Id;
2994 Get_First_Interp (Def, I, It);
2995 while Present (It.Nam) loop
2996 if Entity_Matches_Spec (It.Nam, Nam) then
2997 if Subp /= Any_Id then
2998 It1 := Disambiguate (Def, I1, I, Etype (Subp));
2999
3000 if It1 = No_Interp then
3001 Error_Msg_N ("ambiguous default subprogram", Def);
3002 else
3003 Subp := It1.Nam;
3004 end if;
3005
3006 exit;
3007
3008 else
3009 I1 := I;
3010 Subp := It.Nam;
3011 end if;
3012 end if;
3013
3014 Get_Next_Interp (I, It);
3015 end loop;
3016 end;
3017
3018 if Subp /= Any_Id then
3019
3020 -- Subprogram found, generate reference to it
3021
3022 Set_Entity (Def, Subp);
3023 Generate_Reference (Subp, Def);
3024
3025 if Subp = Nam then
3026 Error_Msg_N ("premature usage of formal subprogram", Def);
3027
3028 elsif Ekind (Subp) /= E_Operator then
3029 Check_Mode_Conformant (Subp, Nam);
3030 end if;
3031
3032 else
3033 Error_Msg_N ("no visible subprogram matches specification", N);
3034 end if;
3035 end if;
3036 end if;
3037
3038 <<Leave>>
3039 if Has_Aspects (N) then
3040 Analyze_Aspect_Specifications (N, Nam);
3041 end if;
3042
3043 end Analyze_Formal_Subprogram_Declaration;
3044
3045 -------------------------------------
3046 -- Analyze_Formal_Type_Declaration --
3047 -------------------------------------
3048
3049 procedure Analyze_Formal_Type_Declaration (N : Node_Id) is
3050 Def : constant Node_Id := Formal_Type_Definition (N);
3051 T : Entity_Id;
3052
3053 begin
3054 T := Defining_Identifier (N);
3055
3056 if Present (Discriminant_Specifications (N))
3057 and then Nkind (Def) /= N_Formal_Private_Type_Definition
3058 then
3059 Error_Msg_N
3060 ("discriminants not allowed for this formal type", T);
3061 end if;
3062
3063 -- Enter the new name, and branch to specific routine
3064
3065 case Nkind (Def) is
3066 when N_Formal_Private_Type_Definition =>
3067 Analyze_Formal_Private_Type (N, T, Def);
3068
3069 when N_Formal_Derived_Type_Definition =>
3070 Analyze_Formal_Derived_Type (N, T, Def);
3071
3072 when N_Formal_Incomplete_Type_Definition =>
3073 Analyze_Formal_Incomplete_Type (T, Def);
3074
3075 when N_Formal_Discrete_Type_Definition =>
3076 Analyze_Formal_Discrete_Type (T, Def);
3077
3078 when N_Formal_Signed_Integer_Type_Definition =>
3079 Analyze_Formal_Signed_Integer_Type (T, Def);
3080
3081 when N_Formal_Modular_Type_Definition =>
3082 Analyze_Formal_Modular_Type (T, Def);
3083
3084 when N_Formal_Floating_Point_Definition =>
3085 Analyze_Formal_Floating_Type (T, Def);
3086
3087 when N_Formal_Ordinary_Fixed_Point_Definition =>
3088 Analyze_Formal_Ordinary_Fixed_Point_Type (T, Def);
3089
3090 when N_Formal_Decimal_Fixed_Point_Definition =>
3091 Analyze_Formal_Decimal_Fixed_Point_Type (T, Def);
3092
3093 when N_Array_Type_Definition =>
3094 Analyze_Formal_Array_Type (T, Def);
3095
3096 when N_Access_To_Object_Definition |
3097 N_Access_Function_Definition |
3098 N_Access_Procedure_Definition =>
3099 Analyze_Generic_Access_Type (T, Def);
3100
3101 -- Ada 2005: a interface declaration is encoded as an abstract
3102 -- record declaration or a abstract type derivation.
3103
3104 when N_Record_Definition =>
3105 Analyze_Formal_Interface_Type (N, T, Def);
3106
3107 when N_Derived_Type_Definition =>
3108 Analyze_Formal_Derived_Interface_Type (N, T, Def);
3109
3110 when N_Error =>
3111 null;
3112
3113 when others =>
3114 raise Program_Error;
3115
3116 end case;
3117
3118 Set_Is_Generic_Type (T);
3119
3120 if Has_Aspects (N) then
3121 Analyze_Aspect_Specifications (N, T);
3122 end if;
3123 end Analyze_Formal_Type_Declaration;
3124
3125 ------------------------------------
3126 -- Analyze_Function_Instantiation --
3127 ------------------------------------
3128
3129 procedure Analyze_Function_Instantiation (N : Node_Id) is
3130 begin
3131 Analyze_Subprogram_Instantiation (N, E_Function);
3132 end Analyze_Function_Instantiation;
3133
3134 ---------------------------------
3135 -- Analyze_Generic_Access_Type --
3136 ---------------------------------
3137
3138 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id) is
3139 begin
3140 Enter_Name (T);
3141
3142 if Nkind (Def) = N_Access_To_Object_Definition then
3143 Access_Type_Declaration (T, Def);
3144
3145 if Is_Incomplete_Or_Private_Type (Designated_Type (T))
3146 and then No (Full_View (Designated_Type (T)))
3147 and then not Is_Generic_Type (Designated_Type (T))
3148 then
3149 Error_Msg_N ("premature usage of incomplete type", Def);
3150
3151 elsif not Is_Entity_Name (Subtype_Indication (Def)) then
3152 Error_Msg_N
3153 ("only a subtype mark is allowed in a formal", Def);
3154 end if;
3155
3156 else
3157 Access_Subprogram_Declaration (T, Def);
3158 end if;
3159 end Analyze_Generic_Access_Type;
3160
3161 ---------------------------------
3162 -- Analyze_Generic_Formal_Part --
3163 ---------------------------------
3164
3165 procedure Analyze_Generic_Formal_Part (N : Node_Id) is
3166 Gen_Parm_Decl : Node_Id;
3167
3168 begin
3169 -- The generic formals are processed in the scope of the generic unit,
3170 -- where they are immediately visible. The scope is installed by the
3171 -- caller.
3172
3173 Gen_Parm_Decl := First (Generic_Formal_Declarations (N));
3174 while Present (Gen_Parm_Decl) loop
3175 Analyze (Gen_Parm_Decl);
3176 Next (Gen_Parm_Decl);
3177 end loop;
3178
3179 Generate_Reference_To_Generic_Formals (Current_Scope);
3180 end Analyze_Generic_Formal_Part;
3181
3182 ------------------------------------------
3183 -- Analyze_Generic_Package_Declaration --
3184 ------------------------------------------
3185
3186 procedure Analyze_Generic_Package_Declaration (N : Node_Id) is
3187 Loc : constant Source_Ptr := Sloc (N);
3188 Decls : constant List_Id :=
3189 Visible_Declarations (Specification (N));
3190 Decl : Node_Id;
3191 Id : Entity_Id;
3192 New_N : Node_Id;
3193 Renaming : Node_Id;
3194 Save_Parent : Node_Id;
3195
3196 begin
3197 Check_SPARK_05_Restriction ("generic is not allowed", N);
3198
3199 -- We introduce a renaming of the enclosing package, to have a usable
3200 -- entity as the prefix of an expanded name for a local entity of the
3201 -- form Par.P.Q, where P is the generic package. This is because a local
3202 -- entity named P may hide it, so that the usual visibility rules in
3203 -- the instance will not resolve properly.
3204
3205 Renaming :=
3206 Make_Package_Renaming_Declaration (Loc,
3207 Defining_Unit_Name =>
3208 Make_Defining_Identifier (Loc,
3209 Chars => New_External_Name (Chars (Defining_Entity (N)), "GH")),
3210 Name =>
3211 Make_Identifier (Loc, Chars (Defining_Entity (N))));
3212
3213 if Present (Decls) then
3214 Decl := First (Decls);
3215 while Present (Decl) and then Nkind (Decl) = N_Pragma loop
3216 Next (Decl);
3217 end loop;
3218
3219 if Present (Decl) then
3220 Insert_Before (Decl, Renaming);
3221 else
3222 Append (Renaming, Visible_Declarations (Specification (N)));
3223 end if;
3224
3225 else
3226 Set_Visible_Declarations (Specification (N), New_List (Renaming));
3227 end if;
3228
3229 -- Create copy of generic unit, and save for instantiation. If the unit
3230 -- is a child unit, do not copy the specifications for the parent, which
3231 -- are not part of the generic tree.
3232
3233 Save_Parent := Parent_Spec (N);
3234 Set_Parent_Spec (N, Empty);
3235
3236 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
3237 Set_Parent_Spec (New_N, Save_Parent);
3238 Rewrite (N, New_N);
3239
3240 -- Once the contents of the generic copy and the template are swapped,
3241 -- do the same for their respective aspect specifications.
3242
3243 Exchange_Aspects (N, New_N);
3244
3245 -- Collect all contract-related source pragmas found within the template
3246 -- and attach them to the contract of the package spec. This contract is
3247 -- used in the capture of global references within annotations.
3248
3249 Create_Generic_Contract (N);
3250
3251 Id := Defining_Entity (N);
3252 Generate_Definition (Id);
3253
3254 -- Expansion is not applied to generic units
3255
3256 Start_Generic;
3257
3258 Enter_Name (Id);
3259 Set_Ekind (Id, E_Generic_Package);
3260 Set_Etype (Id, Standard_Void_Type);
3261
3262 -- A generic package declared within a Ghost region is rendered Ghost
3263 -- (SPARK RM 6.9(2)).
3264
3265 if Ghost_Mode > None then
3266 Set_Is_Ghost_Entity (Id);
3267 end if;
3268
3269 -- Analyze aspects now, so that generated pragmas appear in the
3270 -- declarations before building and analyzing the generic copy.
3271
3272 if Has_Aspects (N) then
3273 Analyze_Aspect_Specifications (N, Id);
3274 end if;
3275
3276 Push_Scope (Id);
3277 Enter_Generic_Scope (Id);
3278 Set_Inner_Instances (Id, New_Elmt_List);
3279
3280 Set_Categorization_From_Pragmas (N);
3281 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3282
3283 -- Link the declaration of the generic homonym in the generic copy to
3284 -- the package it renames, so that it is always resolved properly.
3285
3286 Set_Generic_Homonym (Id, Defining_Unit_Name (Renaming));
3287 Set_Entity (Associated_Node (Name (Renaming)), Id);
3288
3289 -- For a library unit, we have reconstructed the entity for the unit,
3290 -- and must reset it in the library tables.
3291
3292 if Nkind (Parent (N)) = N_Compilation_Unit then
3293 Set_Cunit_Entity (Current_Sem_Unit, Id);
3294 end if;
3295
3296 Analyze_Generic_Formal_Part (N);
3297
3298 -- After processing the generic formals, analysis proceeds as for a
3299 -- non-generic package.
3300
3301 Analyze (Specification (N));
3302
3303 Validate_Categorization_Dependency (N, Id);
3304
3305 End_Generic;
3306
3307 End_Package_Scope (Id);
3308 Exit_Generic_Scope (Id);
3309
3310 if Nkind (Parent (N)) /= N_Compilation_Unit then
3311 Move_Freeze_Nodes (Id, N, Visible_Declarations (Specification (N)));
3312 Move_Freeze_Nodes (Id, N, Private_Declarations (Specification (N)));
3313 Move_Freeze_Nodes (Id, N, Generic_Formal_Declarations (N));
3314
3315 else
3316 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
3317 Validate_RT_RAT_Component (N);
3318
3319 -- If this is a spec without a body, check that generic parameters
3320 -- are referenced.
3321
3322 if not Body_Required (Parent (N)) then
3323 Check_References (Id);
3324 end if;
3325 end if;
3326
3327 -- If there is a specified storage pool in the context, create an
3328 -- aspect on the package declaration, so that it is used in any
3329 -- instance that does not override it.
3330
3331 if Present (Default_Pool) then
3332 declare
3333 ASN : Node_Id;
3334
3335 begin
3336 ASN :=
3337 Make_Aspect_Specification (Loc,
3338 Identifier => Make_Identifier (Loc, Name_Default_Storage_Pool),
3339 Expression => New_Copy (Default_Pool));
3340
3341 if No (Aspect_Specifications (Specification (N))) then
3342 Set_Aspect_Specifications (Specification (N), New_List (ASN));
3343 else
3344 Append (ASN, Aspect_Specifications (Specification (N)));
3345 end if;
3346 end;
3347 end if;
3348 end Analyze_Generic_Package_Declaration;
3349
3350 --------------------------------------------
3351 -- Analyze_Generic_Subprogram_Declaration --
3352 --------------------------------------------
3353
3354 procedure Analyze_Generic_Subprogram_Declaration (N : Node_Id) is
3355 Formals : List_Id;
3356 Id : Entity_Id;
3357 New_N : Node_Id;
3358 Result_Type : Entity_Id;
3359 Save_Parent : Node_Id;
3360 Spec : Node_Id;
3361 Typ : Entity_Id;
3362
3363 begin
3364 Check_SPARK_05_Restriction ("generic is not allowed", N);
3365
3366 -- Create copy of generic unit, and save for instantiation. If the unit
3367 -- is a child unit, do not copy the specifications for the parent, which
3368 -- are not part of the generic tree.
3369
3370 Save_Parent := Parent_Spec (N);
3371 Set_Parent_Spec (N, Empty);
3372
3373 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
3374 Set_Parent_Spec (New_N, Save_Parent);
3375 Rewrite (N, New_N);
3376
3377 -- Once the contents of the generic copy and the template are swapped,
3378 -- do the same for their respective aspect specifications.
3379
3380 Exchange_Aspects (N, New_N);
3381
3382 -- Collect all contract-related source pragmas found within the template
3383 -- and attach them to the contract of the subprogram spec. This contract
3384 -- is used in the capture of global references within annotations.
3385
3386 Create_Generic_Contract (N);
3387
3388 Spec := Specification (N);
3389 Id := Defining_Entity (Spec);
3390 Generate_Definition (Id);
3391
3392 if Nkind (Id) = N_Defining_Operator_Symbol then
3393 Error_Msg_N
3394 ("operator symbol not allowed for generic subprogram", Id);
3395 end if;
3396
3397 Start_Generic;
3398
3399 Enter_Name (Id);
3400 Set_Scope_Depth_Value (Id, Scope_Depth (Current_Scope) + 1);
3401
3402 -- Analyze the aspects of the generic copy to ensure that all generated
3403 -- pragmas (if any) perform their semantic effects.
3404
3405 if Has_Aspects (N) then
3406 Analyze_Aspect_Specifications (N, Id);
3407 end if;
3408
3409 Push_Scope (Id);
3410 Enter_Generic_Scope (Id);
3411 Set_Inner_Instances (Id, New_Elmt_List);
3412 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3413
3414 Analyze_Generic_Formal_Part (N);
3415
3416 Formals := Parameter_Specifications (Spec);
3417
3418 if Nkind (Spec) = N_Function_Specification then
3419 Set_Ekind (Id, E_Generic_Function);
3420 else
3421 Set_Ekind (Id, E_Generic_Procedure);
3422 end if;
3423
3424 if Present (Formals) then
3425 Process_Formals (Formals, Spec);
3426 end if;
3427
3428 if Nkind (Spec) = N_Function_Specification then
3429 if Nkind (Result_Definition (Spec)) = N_Access_Definition then
3430 Result_Type := Access_Definition (Spec, Result_Definition (Spec));
3431 Set_Etype (Id, Result_Type);
3432
3433 -- Check restriction imposed by AI05-073: a generic function
3434 -- cannot return an abstract type or an access to such.
3435
3436 -- This is a binding interpretation should it apply to earlier
3437 -- versions of Ada as well as Ada 2012???
3438
3439 if Is_Abstract_Type (Designated_Type (Result_Type))
3440 and then Ada_Version >= Ada_2012
3441 then
3442 Error_Msg_N
3443 ("generic function cannot have an access result "
3444 & "that designates an abstract type", Spec);
3445 end if;
3446
3447 else
3448 Find_Type (Result_Definition (Spec));
3449 Typ := Entity (Result_Definition (Spec));
3450
3451 if Is_Abstract_Type (Typ)
3452 and then Ada_Version >= Ada_2012
3453 then
3454 Error_Msg_N
3455 ("generic function cannot have abstract result type", Spec);
3456 end if;
3457
3458 -- If a null exclusion is imposed on the result type, then create
3459 -- a null-excluding itype (an access subtype) and use it as the
3460 -- function's Etype.
3461
3462 if Is_Access_Type (Typ)
3463 and then Null_Exclusion_Present (Spec)
3464 then
3465 Set_Etype (Id,
3466 Create_Null_Excluding_Itype
3467 (T => Typ,
3468 Related_Nod => Spec,
3469 Scope_Id => Defining_Unit_Name (Spec)));
3470 else
3471 Set_Etype (Id, Typ);
3472 end if;
3473 end if;
3474
3475 else
3476 Set_Etype (Id, Standard_Void_Type);
3477 end if;
3478
3479 -- A generic subprogram declared within a Ghost region is rendered Ghost
3480 -- (SPARK RM 6.9(2)).
3481
3482 if Ghost_Mode > None then
3483 Set_Is_Ghost_Entity (Id);
3484 end if;
3485
3486 -- For a library unit, we have reconstructed the entity for the unit,
3487 -- and must reset it in the library tables. We also make sure that
3488 -- Body_Required is set properly in the original compilation unit node.
3489
3490 if Nkind (Parent (N)) = N_Compilation_Unit then
3491 Set_Cunit_Entity (Current_Sem_Unit, Id);
3492 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
3493 end if;
3494
3495 Set_Categorization_From_Pragmas (N);
3496 Validate_Categorization_Dependency (N, Id);
3497
3498 -- Capture all global references that occur within the profile of the
3499 -- generic subprogram. Aspects are not part of this processing because
3500 -- they must be delayed. If processed now, Save_Global_References will
3501 -- destroy the Associated_Node links and prevent the capture of global
3502 -- references when the contract of the generic subprogram is analyzed.
3503
3504 Save_Global_References (Original_Node (N));
3505
3506 End_Generic;
3507 End_Scope;
3508 Exit_Generic_Scope (Id);
3509 Generate_Reference_To_Formals (Id);
3510
3511 List_Inherited_Pre_Post_Aspects (Id);
3512 end Analyze_Generic_Subprogram_Declaration;
3513
3514 -----------------------------------
3515 -- Analyze_Package_Instantiation --
3516 -----------------------------------
3517
3518 procedure Analyze_Package_Instantiation (N : Node_Id) is
3519 Loc : constant Source_Ptr := Sloc (N);
3520 Gen_Id : constant Node_Id := Name (N);
3521
3522 Act_Decl : Node_Id;
3523 Act_Decl_Name : Node_Id;
3524 Act_Decl_Id : Entity_Id;
3525 Act_Spec : Node_Id;
3526 Act_Tree : Node_Id;
3527
3528 Gen_Decl : Node_Id;
3529 Gen_Spec : Node_Id;
3530 Gen_Unit : Entity_Id;
3531
3532 Is_Actual_Pack : constant Boolean :=
3533 Is_Internal (Defining_Entity (N));
3534
3535 Env_Installed : Boolean := False;
3536 Parent_Installed : Boolean := False;
3537 Renaming_List : List_Id;
3538 Unit_Renaming : Node_Id;
3539 Needs_Body : Boolean;
3540 Inline_Now : Boolean := False;
3541 Has_Inline_Always : Boolean := False;
3542
3543 Save_IPSM : constant Boolean := Ignore_Pragma_SPARK_Mode;
3544 -- Save flag Ignore_Pragma_SPARK_Mode for restore on exit
3545
3546 Save_SM : constant SPARK_Mode_Type := SPARK_Mode;
3547 Save_SMP : constant Node_Id := SPARK_Mode_Pragma;
3548 -- Save the SPARK_Mode-related data for restore on exit
3549
3550 Save_Style_Check : constant Boolean := Style_Check;
3551 -- Save style check mode for restore on exit
3552
3553 procedure Delay_Descriptors (E : Entity_Id);
3554 -- Delay generation of subprogram descriptors for given entity
3555
3556 function Might_Inline_Subp return Boolean;
3557 -- If inlining is active and the generic contains inlined subprograms,
3558 -- we instantiate the body. This may cause superfluous instantiations,
3559 -- but it is simpler than detecting the need for the body at the point
3560 -- of inlining, when the context of the instance is not available.
3561
3562 -----------------------
3563 -- Delay_Descriptors --
3564 -----------------------
3565
3566 procedure Delay_Descriptors (E : Entity_Id) is
3567 begin
3568 if not Delay_Subprogram_Descriptors (E) then
3569 Set_Delay_Subprogram_Descriptors (E);
3570 Pending_Descriptor.Append (E);
3571 end if;
3572 end Delay_Descriptors;
3573
3574 -----------------------
3575 -- Might_Inline_Subp --
3576 -----------------------
3577
3578 function Might_Inline_Subp return Boolean is
3579 E : Entity_Id;
3580
3581 begin
3582 if not Inline_Processing_Required then
3583 return False;
3584
3585 else
3586 E := First_Entity (Gen_Unit);
3587 while Present (E) loop
3588 if Is_Subprogram (E) and then Is_Inlined (E) then
3589 -- Remember if there are any subprograms with Inline_Always
3590
3591 if Has_Pragma_Inline_Always (E) then
3592 Has_Inline_Always := True;
3593 end if;
3594
3595 return True;
3596 end if;
3597
3598 Next_Entity (E);
3599 end loop;
3600 end if;
3601
3602 return False;
3603 end Might_Inline_Subp;
3604
3605 -- Local declarations
3606
3607 Vis_Prims_List : Elist_Id := No_Elist;
3608 -- List of primitives made temporarily visible in the instantiation
3609 -- to match the visibility of the formal type
3610
3611 -- Start of processing for Analyze_Package_Instantiation
3612
3613 begin
3614 Check_SPARK_05_Restriction ("generic is not allowed", N);
3615
3616 -- Very first thing: check for Text_IO special unit in case we are
3617 -- instantiating one of the children of [[Wide_]Wide_]Text_IO.
3618
3619 Check_Text_IO_Special_Unit (Name (N));
3620
3621 -- Make node global for error reporting
3622
3623 Instantiation_Node := N;
3624
3625 -- Turn off style checking in instances. If the check is enabled on the
3626 -- generic unit, a warning in an instance would just be noise. If not
3627 -- enabled on the generic, then a warning in an instance is just wrong.
3628
3629 Style_Check := False;
3630
3631 -- Case of instantiation of a generic package
3632
3633 if Nkind (N) = N_Package_Instantiation then
3634 Act_Decl_Id := New_Copy (Defining_Entity (N));
3635 Set_Comes_From_Source (Act_Decl_Id, True);
3636
3637 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name then
3638 Act_Decl_Name :=
3639 Make_Defining_Program_Unit_Name (Loc,
3640 Name =>
3641 New_Copy_Tree (Name (Defining_Unit_Name (N))),
3642 Defining_Identifier => Act_Decl_Id);
3643 else
3644 Act_Decl_Name := Act_Decl_Id;
3645 end if;
3646
3647 -- Case of instantiation of a formal package
3648
3649 else
3650 Act_Decl_Id := Defining_Identifier (N);
3651 Act_Decl_Name := Act_Decl_Id;
3652 end if;
3653
3654 Generate_Definition (Act_Decl_Id);
3655 Set_Ekind (Act_Decl_Id, E_Package);
3656
3657 -- Initialize list of incomplete actuals before analysis
3658
3659 Set_Incomplete_Actuals (Act_Decl_Id, New_Elmt_List);
3660
3661 Preanalyze_Actuals (N, Act_Decl_Id);
3662
3663 Init_Env;
3664 Env_Installed := True;
3665
3666 -- Reset renaming map for formal types. The mapping is established
3667 -- when analyzing the generic associations, but some mappings are
3668 -- inherited from formal packages of parent units, and these are
3669 -- constructed when the parents are installed.
3670
3671 Generic_Renamings.Set_Last (0);
3672 Generic_Renamings_HTable.Reset;
3673
3674 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
3675 Gen_Unit := Entity (Gen_Id);
3676
3677 -- Verify that it is the name of a generic package
3678
3679 -- A visibility glitch: if the instance is a child unit and the generic
3680 -- is the generic unit of a parent instance (i.e. both the parent and
3681 -- the child units are instances of the same package) the name now
3682 -- denotes the renaming within the parent, not the intended generic
3683 -- unit. See if there is a homonym that is the desired generic. The
3684 -- renaming declaration must be visible inside the instance of the
3685 -- child, but not when analyzing the name in the instantiation itself.
3686
3687 if Ekind (Gen_Unit) = E_Package
3688 and then Present (Renamed_Entity (Gen_Unit))
3689 and then In_Open_Scopes (Renamed_Entity (Gen_Unit))
3690 and then Is_Generic_Instance (Renamed_Entity (Gen_Unit))
3691 and then Present (Homonym (Gen_Unit))
3692 then
3693 Gen_Unit := Homonym (Gen_Unit);
3694 end if;
3695
3696 if Etype (Gen_Unit) = Any_Type then
3697 Restore_Env;
3698 goto Leave;
3699
3700 elsif Ekind (Gen_Unit) /= E_Generic_Package then
3701
3702 -- Ada 2005 (AI-50217): Cannot use instance in limited with_clause
3703
3704 if From_Limited_With (Gen_Unit) then
3705 Error_Msg_N
3706 ("cannot instantiate a limited withed package", Gen_Id);
3707 else
3708 Error_Msg_NE
3709 ("& is not the name of a generic package", Gen_Id, Gen_Unit);
3710 end if;
3711
3712 Restore_Env;
3713 goto Leave;
3714 end if;
3715
3716 if In_Extended_Main_Source_Unit (N) then
3717 Set_Is_Instantiated (Gen_Unit);
3718 Generate_Reference (Gen_Unit, N);
3719
3720 if Present (Renamed_Object (Gen_Unit)) then
3721 Set_Is_Instantiated (Renamed_Object (Gen_Unit));
3722 Generate_Reference (Renamed_Object (Gen_Unit), N);
3723 end if;
3724 end if;
3725
3726 if Nkind (Gen_Id) = N_Identifier
3727 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
3728 then
3729 Error_Msg_NE
3730 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
3731
3732 elsif Nkind (Gen_Id) = N_Expanded_Name
3733 and then Is_Child_Unit (Gen_Unit)
3734 and then Nkind (Prefix (Gen_Id)) = N_Identifier
3735 and then Chars (Act_Decl_Id) = Chars (Prefix (Gen_Id))
3736 then
3737 Error_Msg_N
3738 ("& is hidden within declaration of instance ", Prefix (Gen_Id));
3739 end if;
3740
3741 Set_Entity (Gen_Id, Gen_Unit);
3742
3743 -- If generic is a renaming, get original generic unit
3744
3745 if Present (Renamed_Object (Gen_Unit))
3746 and then Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Package
3747 then
3748 Gen_Unit := Renamed_Object (Gen_Unit);
3749 end if;
3750
3751 -- Verify that there are no circular instantiations
3752
3753 if In_Open_Scopes (Gen_Unit) then
3754 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
3755 Restore_Env;
3756 goto Leave;
3757
3758 elsif Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
3759 Error_Msg_Node_2 := Current_Scope;
3760 Error_Msg_NE
3761 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
3762 Circularity_Detected := True;
3763 Restore_Env;
3764 goto Leave;
3765
3766 else
3767 -- If the context of the instance is subject to SPARK_Mode "off" or
3768 -- the annotation is altogether missing, set the global flag which
3769 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
3770 -- the instance.
3771
3772 if SPARK_Mode /= On then
3773 Ignore_Pragma_SPARK_Mode := True;
3774 end if;
3775
3776 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
3777 Gen_Spec := Specification (Gen_Decl);
3778
3779 -- Initialize renamings map, for error checking, and the list that
3780 -- holds private entities whose views have changed between generic
3781 -- definition and instantiation. If this is the instance created to
3782 -- validate an actual package, the instantiation environment is that
3783 -- of the enclosing instance.
3784
3785 Create_Instantiation_Source (N, Gen_Unit, False, S_Adjustment);
3786
3787 -- Copy original generic tree, to produce text for instantiation
3788
3789 Act_Tree :=
3790 Copy_Generic_Node
3791 (Original_Node (Gen_Decl), Empty, Instantiating => True);
3792
3793 Act_Spec := Specification (Act_Tree);
3794
3795 -- If this is the instance created to validate an actual package,
3796 -- only the formals matter, do not examine the package spec itself.
3797
3798 if Is_Actual_Pack then
3799 Set_Visible_Declarations (Act_Spec, New_List);
3800 Set_Private_Declarations (Act_Spec, New_List);
3801 end if;
3802
3803 Renaming_List :=
3804 Analyze_Associations
3805 (I_Node => N,
3806 Formals => Generic_Formal_Declarations (Act_Tree),
3807 F_Copy => Generic_Formal_Declarations (Gen_Decl));
3808
3809 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
3810
3811 Set_Instance_Env (Gen_Unit, Act_Decl_Id);
3812 Set_Defining_Unit_Name (Act_Spec, Act_Decl_Name);
3813 Set_Is_Generic_Instance (Act_Decl_Id);
3814 Set_Generic_Parent (Act_Spec, Gen_Unit);
3815
3816 -- References to the generic in its own declaration or its body are
3817 -- references to the instance. Add a renaming declaration for the
3818 -- generic unit itself. This declaration, as well as the renaming
3819 -- declarations for the generic formals, must remain private to the
3820 -- unit: the formals, because this is the language semantics, and
3821 -- the unit because its use is an artifact of the implementation.
3822
3823 Unit_Renaming :=
3824 Make_Package_Renaming_Declaration (Loc,
3825 Defining_Unit_Name =>
3826 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
3827 Name => New_Occurrence_Of (Act_Decl_Id, Loc));
3828
3829 Append (Unit_Renaming, Renaming_List);
3830
3831 -- The renaming declarations are the first local declarations of the
3832 -- new unit.
3833
3834 if Is_Non_Empty_List (Visible_Declarations (Act_Spec)) then
3835 Insert_List_Before
3836 (First (Visible_Declarations (Act_Spec)), Renaming_List);
3837 else
3838 Set_Visible_Declarations (Act_Spec, Renaming_List);
3839 end if;
3840
3841 Act_Decl := Make_Package_Declaration (Loc, Specification => Act_Spec);
3842
3843 -- Propagate the aspect specifications from the package declaration
3844 -- template to the instantiated version of the package declaration.
3845
3846 if Has_Aspects (Act_Tree) then
3847 Set_Aspect_Specifications (Act_Decl,
3848 New_Copy_List_Tree (Aspect_Specifications (Act_Tree)));
3849 end if;
3850
3851 -- The generic may have a generated Default_Storage_Pool aspect,
3852 -- set at the point of generic declaration. If the instance has
3853 -- that aspect, it overrides the one inherited from the generic.
3854
3855 if Has_Aspects (Gen_Spec) then
3856 if No (Aspect_Specifications (N)) then
3857 Set_Aspect_Specifications (N,
3858 (New_Copy_List_Tree
3859 (Aspect_Specifications (Gen_Spec))));
3860
3861 else
3862 declare
3863 ASN1, ASN2 : Node_Id;
3864
3865 begin
3866 ASN1 := First (Aspect_Specifications (N));
3867 while Present (ASN1) loop
3868 if Chars (Identifier (ASN1)) = Name_Default_Storage_Pool
3869 then
3870 -- If generic carries a default storage pool, remove
3871 -- it in favor of the instance one.
3872
3873 ASN2 := First (Aspect_Specifications (Gen_Spec));
3874 while Present (ASN2) loop
3875 if Chars (Identifier (ASN2)) =
3876 Name_Default_Storage_Pool
3877 then
3878 Remove (ASN2);
3879 exit;
3880 end if;
3881
3882 Next (ASN2);
3883 end loop;
3884 end if;
3885
3886 Next (ASN1);
3887 end loop;
3888
3889 Prepend_List_To (Aspect_Specifications (N),
3890 (New_Copy_List_Tree
3891 (Aspect_Specifications (Gen_Spec))));
3892 end;
3893 end if;
3894 end if;
3895
3896 -- Save the instantiation node, for subsequent instantiation of the
3897 -- body, if there is one and we are generating code for the current
3898 -- unit. Mark unit as having a body (avoids premature error message).
3899
3900 -- We instantiate the body if we are generating code, if we are
3901 -- generating cross-reference information, or if we are building
3902 -- trees for ASIS use or GNATprove use.
3903
3904 declare
3905 Enclosing_Body_Present : Boolean := False;
3906 -- If the generic unit is not a compilation unit, then a body may
3907 -- be present in its parent even if none is required. We create a
3908 -- tentative pending instantiation for the body, which will be
3909 -- discarded if none is actually present.
3910
3911 Scop : Entity_Id;
3912
3913 begin
3914 if Scope (Gen_Unit) /= Standard_Standard
3915 and then not Is_Child_Unit (Gen_Unit)
3916 then
3917 Scop := Scope (Gen_Unit);
3918 while Present (Scop) and then Scop /= Standard_Standard loop
3919 if Unit_Requires_Body (Scop) then
3920 Enclosing_Body_Present := True;
3921 exit;
3922
3923 elsif In_Open_Scopes (Scop)
3924 and then In_Package_Body (Scop)
3925 then
3926 Enclosing_Body_Present := True;
3927 exit;
3928 end if;
3929
3930 exit when Is_Compilation_Unit (Scop);
3931 Scop := Scope (Scop);
3932 end loop;
3933 end if;
3934
3935 -- If front-end inlining is enabled or there are any subprograms
3936 -- marked with Inline_Always, and this is a unit for which code
3937 -- will be generated, we instantiate the body at once.
3938
3939 -- This is done if the instance is not the main unit, and if the
3940 -- generic is not a child unit of another generic, to avoid scope
3941 -- problems and the reinstallation of parent instances.
3942
3943 if Expander_Active
3944 and then (not Is_Child_Unit (Gen_Unit)
3945 or else not Is_Generic_Unit (Scope (Gen_Unit)))
3946 and then Might_Inline_Subp
3947 and then not Is_Actual_Pack
3948 then
3949 if not Back_End_Inlining
3950 and then (Front_End_Inlining or else Has_Inline_Always)
3951 and then (Is_In_Main_Unit (N)
3952 or else In_Main_Context (Current_Scope))
3953 and then Nkind (Parent (N)) /= N_Compilation_Unit
3954 then
3955 Inline_Now := True;
3956
3957 -- In configurable_run_time mode we force the inlining of
3958 -- predefined subprograms marked Inline_Always, to minimize
3959 -- the use of the run-time library.
3960
3961 elsif Is_Predefined_File_Name
3962 (Unit_File_Name (Get_Source_Unit (Gen_Decl)))
3963 and then Configurable_Run_Time_Mode
3964 and then Nkind (Parent (N)) /= N_Compilation_Unit
3965 then
3966 Inline_Now := True;
3967 end if;
3968
3969 -- If the current scope is itself an instance within a child
3970 -- unit, there will be duplications in the scope stack, and the
3971 -- unstacking mechanism in Inline_Instance_Body will fail.
3972 -- This loses some rare cases of optimization, and might be
3973 -- improved some day, if we can find a proper abstraction for
3974 -- "the complete compilation context" that can be saved and
3975 -- restored. ???
3976
3977 if Is_Generic_Instance (Current_Scope) then
3978 declare
3979 Curr_Unit : constant Entity_Id :=
3980 Cunit_Entity (Current_Sem_Unit);
3981 begin
3982 if Curr_Unit /= Current_Scope
3983 and then Is_Child_Unit (Curr_Unit)
3984 then
3985 Inline_Now := False;
3986 end if;
3987 end;
3988 end if;
3989 end if;
3990
3991 Needs_Body :=
3992 (Unit_Requires_Body (Gen_Unit)
3993 or else Enclosing_Body_Present
3994 or else Present (Corresponding_Body (Gen_Decl)))
3995 and then (Is_In_Main_Unit (N) or else Might_Inline_Subp)
3996 and then not Is_Actual_Pack
3997 and then not Inline_Now
3998 and then (Operating_Mode = Generate_Code
3999
4000 -- Need comment for this check ???
4001
4002 or else (Operating_Mode = Check_Semantics
4003 and then (ASIS_Mode or GNATprove_Mode)));
4004
4005 -- If front-end inlining is enabled or there are any subprograms
4006 -- marked with Inline_Always, do not instantiate body when within
4007 -- a generic context.
4008
4009 if ((Front_End_Inlining or else Has_Inline_Always)
4010 and then not Expander_Active)
4011 or else Is_Generic_Unit (Cunit_Entity (Main_Unit))
4012 then
4013 Needs_Body := False;
4014 end if;
4015
4016 -- If the current context is generic, and the package being
4017 -- instantiated is declared within a formal package, there is no
4018 -- body to instantiate until the enclosing generic is instantiated
4019 -- and there is an actual for the formal package. If the formal
4020 -- package has parameters, we build a regular package instance for
4021 -- it, that precedes the original formal package declaration.
4022
4023 if In_Open_Scopes (Scope (Scope (Gen_Unit))) then
4024 declare
4025 Decl : constant Node_Id :=
4026 Original_Node
4027 (Unit_Declaration_Node (Scope (Gen_Unit)));
4028 begin
4029 if Nkind (Decl) = N_Formal_Package_Declaration
4030 or else (Nkind (Decl) = N_Package_Declaration
4031 and then Is_List_Member (Decl)
4032 and then Present (Next (Decl))
4033 and then
4034 Nkind (Next (Decl)) =
4035 N_Formal_Package_Declaration)
4036 then
4037 Needs_Body := False;
4038 end if;
4039 end;
4040 end if;
4041 end;
4042
4043 -- For RCI unit calling stubs, we omit the instance body if the
4044 -- instance is the RCI library unit itself.
4045
4046 -- However there is a special case for nested instances: in this case
4047 -- we do generate the instance body, as it might be required, e.g.
4048 -- because it provides stream attributes for some type used in the
4049 -- profile of a remote subprogram. This is consistent with 12.3(12),
4050 -- which indicates that the instance body occurs at the place of the
4051 -- instantiation, and thus is part of the RCI declaration, which is
4052 -- present on all client partitions (this is E.2.3(18)).
4053
4054 -- Note that AI12-0002 may make it illegal at some point to have
4055 -- stream attributes defined in an RCI unit, in which case this
4056 -- special case will become unnecessary. In the meantime, there
4057 -- is known application code in production that depends on this
4058 -- being possible, so we definitely cannot eliminate the body in
4059 -- the case of nested instances for the time being.
4060
4061 -- When we generate a nested instance body, calling stubs for any
4062 -- relevant subprogram will be be inserted immediately after the
4063 -- subprogram declarations, and will take precedence over the
4064 -- subsequent (original) body. (The stub and original body will be
4065 -- complete homographs, but this is permitted in an instance).
4066 -- (Could we do better and remove the original body???)
4067
4068 if Distribution_Stub_Mode = Generate_Caller_Stub_Body
4069 and then Comes_From_Source (N)
4070 and then Nkind (Parent (N)) = N_Compilation_Unit
4071 then
4072 Needs_Body := False;
4073 end if;
4074
4075 if Needs_Body then
4076
4077 -- Here is a defence against a ludicrous number of instantiations
4078 -- caused by a circular set of instantiation attempts.
4079
4080 if Pending_Instantiations.Last > Maximum_Instantiations then
4081 Error_Msg_Uint_1 := UI_From_Int (Maximum_Instantiations);
4082 Error_Msg_N ("too many instantiations, exceeds max of^", N);
4083 Error_Msg_N ("\limit can be changed using -gnateinn switch", N);
4084 raise Unrecoverable_Error;
4085 end if;
4086
4087 -- Indicate that the enclosing scopes contain an instantiation,
4088 -- and that cleanup actions should be delayed until after the
4089 -- instance body is expanded.
4090
4091 Check_Forward_Instantiation (Gen_Decl);
4092 if Nkind (N) = N_Package_Instantiation then
4093 declare
4094 Enclosing_Master : Entity_Id;
4095
4096 begin
4097 -- Loop to search enclosing masters
4098
4099 Enclosing_Master := Current_Scope;
4100 Scope_Loop : while Enclosing_Master /= Standard_Standard loop
4101 if Ekind (Enclosing_Master) = E_Package then
4102 if Is_Compilation_Unit (Enclosing_Master) then
4103 if In_Package_Body (Enclosing_Master) then
4104 Delay_Descriptors
4105 (Body_Entity (Enclosing_Master));
4106 else
4107 Delay_Descriptors
4108 (Enclosing_Master);
4109 end if;
4110
4111 exit Scope_Loop;
4112
4113 else
4114 Enclosing_Master := Scope (Enclosing_Master);
4115 end if;
4116
4117 elsif Is_Generic_Unit (Enclosing_Master)
4118 or else Ekind (Enclosing_Master) = E_Void
4119 then
4120 -- Cleanup actions will eventually be performed on the
4121 -- enclosing subprogram or package instance, if any.
4122 -- Enclosing scope is void in the formal part of a
4123 -- generic subprogram.
4124
4125 exit Scope_Loop;
4126
4127 else
4128 if Ekind (Enclosing_Master) = E_Entry
4129 and then
4130 Ekind (Scope (Enclosing_Master)) = E_Protected_Type
4131 then
4132 if not Expander_Active then
4133 exit Scope_Loop;
4134 else
4135 Enclosing_Master :=
4136 Protected_Body_Subprogram (Enclosing_Master);
4137 end if;
4138 end if;
4139
4140 Set_Delay_Cleanups (Enclosing_Master);
4141
4142 while Ekind (Enclosing_Master) = E_Block loop
4143 Enclosing_Master := Scope (Enclosing_Master);
4144 end loop;
4145
4146 if Is_Subprogram (Enclosing_Master) then
4147 Delay_Descriptors (Enclosing_Master);
4148
4149 elsif Is_Task_Type (Enclosing_Master) then
4150 declare
4151 TBP : constant Node_Id :=
4152 Get_Task_Body_Procedure
4153 (Enclosing_Master);
4154 begin
4155 if Present (TBP) then
4156 Delay_Descriptors (TBP);
4157 Set_Delay_Cleanups (TBP);
4158 end if;
4159 end;
4160 end if;
4161
4162 exit Scope_Loop;
4163 end if;
4164 end loop Scope_Loop;
4165 end;
4166
4167 -- Make entry in table
4168
4169 Add_Pending_Instantiation (N, Act_Decl);
4170 end if;
4171 end if;
4172
4173 Set_Categorization_From_Pragmas (Act_Decl);
4174
4175 if Parent_Installed then
4176 Hide_Current_Scope;
4177 end if;
4178
4179 Set_Instance_Spec (N, Act_Decl);
4180
4181 -- If not a compilation unit, insert the package declaration before
4182 -- the original instantiation node.
4183
4184 if Nkind (Parent (N)) /= N_Compilation_Unit then
4185 Mark_Rewrite_Insertion (Act_Decl);
4186 Insert_Before (N, Act_Decl);
4187
4188 if Has_Aspects (N) then
4189 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4190
4191 -- The pragma created for a Default_Storage_Pool aspect must
4192 -- appear ahead of the declarations in the instance spec.
4193 -- Analysis has placed it after the instance node, so remove
4194 -- it and reinsert it properly now.
4195
4196 declare
4197 ASN : constant Node_Id := First (Aspect_Specifications (N));
4198 A_Name : constant Name_Id := Chars (Identifier (ASN));
4199 Decl : Node_Id;
4200
4201 begin
4202 if A_Name = Name_Default_Storage_Pool then
4203 if No (Visible_Declarations (Act_Spec)) then
4204 Set_Visible_Declarations (Act_Spec, New_List);
4205 end if;
4206
4207 Decl := Next (N);
4208 while Present (Decl) loop
4209 if Nkind (Decl) = N_Pragma then
4210 Remove (Decl);
4211 Prepend (Decl, Visible_Declarations (Act_Spec));
4212 exit;
4213 end if;
4214
4215 Next (Decl);
4216 end loop;
4217 end if;
4218 end;
4219 end if;
4220
4221 Analyze (Act_Decl);
4222
4223 -- For an instantiation that is a compilation unit, place
4224 -- declaration on current node so context is complete for analysis
4225 -- (including nested instantiations). If this is the main unit,
4226 -- the declaration eventually replaces the instantiation node.
4227 -- If the instance body is created later, it replaces the
4228 -- instance node, and the declaration is attached to it
4229 -- (see Build_Instance_Compilation_Unit_Nodes).
4230
4231 else
4232 if Cunit_Entity (Current_Sem_Unit) = Defining_Entity (N) then
4233
4234 -- The entity for the current unit is the newly created one,
4235 -- and all semantic information is attached to it.
4236
4237 Set_Cunit_Entity (Current_Sem_Unit, Act_Decl_Id);
4238
4239 -- If this is the main unit, replace the main entity as well
4240
4241 if Current_Sem_Unit = Main_Unit then
4242 Main_Unit_Entity := Act_Decl_Id;
4243 end if;
4244 end if;
4245
4246 Set_Unit (Parent (N), Act_Decl);
4247 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
4248 Set_Package_Instantiation (Act_Decl_Id, N);
4249
4250 -- Process aspect specifications of the instance node, if any, to
4251 -- take into account categorization pragmas before analyzing the
4252 -- instance.
4253
4254 if Has_Aspects (N) then
4255 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4256 end if;
4257
4258 Analyze (Act_Decl);
4259 Set_Unit (Parent (N), N);
4260 Set_Body_Required (Parent (N), False);
4261
4262 -- We never need elaboration checks on instantiations, since by
4263 -- definition, the body instantiation is elaborated at the same
4264 -- time as the spec instantiation.
4265
4266 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
4267 Set_Kill_Elaboration_Checks (Act_Decl_Id);
4268 end if;
4269
4270 Check_Elab_Instantiation (N);
4271
4272 if ABE_Is_Certain (N) and then Needs_Body then
4273 Pending_Instantiations.Decrement_Last;
4274 end if;
4275
4276 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
4277
4278 Set_First_Private_Entity (Defining_Unit_Name (Unit_Renaming),
4279 First_Private_Entity (Act_Decl_Id));
4280
4281 -- If the instantiation will receive a body, the unit will be
4282 -- transformed into a package body, and receive its own elaboration
4283 -- entity. Otherwise, the nature of the unit is now a package
4284 -- declaration.
4285
4286 if Nkind (Parent (N)) = N_Compilation_Unit
4287 and then not Needs_Body
4288 then
4289 Rewrite (N, Act_Decl);
4290 end if;
4291
4292 if Present (Corresponding_Body (Gen_Decl))
4293 or else Unit_Requires_Body (Gen_Unit)
4294 then
4295 Set_Has_Completion (Act_Decl_Id);
4296 end if;
4297
4298 Check_Formal_Packages (Act_Decl_Id);
4299
4300 Restore_Hidden_Primitives (Vis_Prims_List);
4301 Restore_Private_Views (Act_Decl_Id);
4302
4303 Inherit_Context (Gen_Decl, N);
4304
4305 if Parent_Installed then
4306 Remove_Parent;
4307 end if;
4308
4309 Restore_Env;
4310 Env_Installed := False;
4311 end if;
4312
4313 Validate_Categorization_Dependency (N, Act_Decl_Id);
4314
4315 -- There used to be a check here to prevent instantiations in local
4316 -- contexts if the No_Local_Allocators restriction was active. This
4317 -- check was removed by a binding interpretation in AI-95-00130/07,
4318 -- but we retain the code for documentation purposes.
4319
4320 -- if Ekind (Act_Decl_Id) /= E_Void
4321 -- and then not Is_Library_Level_Entity (Act_Decl_Id)
4322 -- then
4323 -- Check_Restriction (No_Local_Allocators, N);
4324 -- end if;
4325
4326 if Inline_Now then
4327 Inline_Instance_Body (N, Gen_Unit, Act_Decl);
4328 end if;
4329
4330 -- The following is a tree patch for ASIS: ASIS needs separate nodes to
4331 -- be used as defining identifiers for a formal package and for the
4332 -- corresponding expanded package.
4333
4334 if Nkind (N) = N_Formal_Package_Declaration then
4335 Act_Decl_Id := New_Copy (Defining_Entity (N));
4336 Set_Comes_From_Source (Act_Decl_Id, True);
4337 Set_Is_Generic_Instance (Act_Decl_Id, False);
4338 Set_Defining_Identifier (N, Act_Decl_Id);
4339 end if;
4340
4341 Ignore_Pragma_SPARK_Mode := Save_IPSM;
4342 SPARK_Mode := Save_SM;
4343 SPARK_Mode_Pragma := Save_SMP;
4344 Style_Check := Save_Style_Check;
4345
4346 if SPARK_Mode = On then
4347 Dynamic_Elaboration_Checks := False;
4348 end if;
4349
4350 -- Check that if N is an instantiation of System.Dim_Float_IO or
4351 -- System.Dim_Integer_IO, the formal type has a dimension system.
4352
4353 if Nkind (N) = N_Package_Instantiation
4354 and then Is_Dim_IO_Package_Instantiation (N)
4355 then
4356 declare
4357 Assoc : constant Node_Id := First (Generic_Associations (N));
4358 begin
4359 if not Has_Dimension_System
4360 (Etype (Explicit_Generic_Actual_Parameter (Assoc)))
4361 then
4362 Error_Msg_N ("type with a dimension system expected", Assoc);
4363 end if;
4364 end;
4365 end if;
4366
4367 <<Leave>>
4368 if Has_Aspects (N) and then Nkind (Parent (N)) /= N_Compilation_Unit then
4369 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4370 end if;
4371
4372 exception
4373 when Instantiation_Error =>
4374 if Parent_Installed then
4375 Remove_Parent;
4376 end if;
4377
4378 if Env_Installed then
4379 Restore_Env;
4380 end if;
4381
4382 Ignore_Pragma_SPARK_Mode := Save_IPSM;
4383 SPARK_Mode := Save_SM;
4384 SPARK_Mode_Pragma := Save_SMP;
4385 Style_Check := Save_Style_Check;
4386
4387 if SPARK_Mode = On then
4388 Dynamic_Elaboration_Checks := False;
4389 end if;
4390 end Analyze_Package_Instantiation;
4391
4392 --------------------------
4393 -- Inline_Instance_Body --
4394 --------------------------
4395
4396 procedure Inline_Instance_Body
4397 (N : Node_Id;
4398 Gen_Unit : Entity_Id;
4399 Act_Decl : Node_Id)
4400 is
4401 Curr_Comp : constant Node_Id := Cunit (Current_Sem_Unit);
4402 Curr_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
4403 Gen_Comp : constant Entity_Id :=
4404 Cunit_Entity (Get_Source_Unit (Gen_Unit));
4405
4406 Save_SM : constant SPARK_Mode_Type := SPARK_Mode;
4407 Save_SMP : constant Node_Id := SPARK_Mode_Pragma;
4408 -- Save all SPARK_Mode-related attributes as removing enclosing scopes
4409 -- to provide a clean environment for analysis of the inlined body will
4410 -- eliminate any previously set SPARK_Mode.
4411
4412 Scope_Stack_Depth : constant Pos :=
4413 Scope_Stack.Last - Scope_Stack.First + 1;
4414
4415 Use_Clauses : array (1 .. Scope_Stack_Depth) of Node_Id;
4416 Instances : array (1 .. Scope_Stack_Depth) of Entity_Id;
4417 Inner_Scopes : array (1 .. Scope_Stack_Depth) of Entity_Id;
4418 Curr_Scope : Entity_Id := Empty;
4419 List : Elist_Id;
4420 Num_Inner : Nat := 0;
4421 Num_Scopes : Nat := 0;
4422 N_Instances : Nat := 0;
4423 Removed : Boolean := False;
4424 S : Entity_Id;
4425 Vis : Boolean;
4426
4427 begin
4428 -- Case of generic unit defined in another unit. We must remove the
4429 -- complete context of the current unit to install that of the generic.
4430
4431 if Gen_Comp /= Cunit_Entity (Current_Sem_Unit) then
4432
4433 -- Add some comments for the following two loops ???
4434
4435 S := Current_Scope;
4436 while Present (S) and then S /= Standard_Standard loop
4437 loop
4438 Num_Scopes := Num_Scopes + 1;
4439
4440 Use_Clauses (Num_Scopes) :=
4441 (Scope_Stack.Table
4442 (Scope_Stack.Last - Num_Scopes + 1).
4443 First_Use_Clause);
4444 End_Use_Clauses (Use_Clauses (Num_Scopes));
4445
4446 exit when Scope_Stack.Last - Num_Scopes + 1 = Scope_Stack.First
4447 or else Scope_Stack.Table
4448 (Scope_Stack.Last - Num_Scopes).Entity = Scope (S);
4449 end loop;
4450
4451 exit when Is_Generic_Instance (S)
4452 and then (In_Package_Body (S)
4453 or else Ekind (S) = E_Procedure
4454 or else Ekind (S) = E_Function);
4455 S := Scope (S);
4456 end loop;
4457
4458 Vis := Is_Immediately_Visible (Gen_Comp);
4459
4460 -- Find and save all enclosing instances
4461
4462 S := Current_Scope;
4463
4464 while Present (S)
4465 and then S /= Standard_Standard
4466 loop
4467 if Is_Generic_Instance (S) then
4468 N_Instances := N_Instances + 1;
4469 Instances (N_Instances) := S;
4470
4471 exit when In_Package_Body (S);
4472 end if;
4473
4474 S := Scope (S);
4475 end loop;
4476
4477 -- Remove context of current compilation unit, unless we are within a
4478 -- nested package instantiation, in which case the context has been
4479 -- removed previously.
4480
4481 -- If current scope is the body of a child unit, remove context of
4482 -- spec as well. If an enclosing scope is an instance body, the
4483 -- context has already been removed, but the entities in the body
4484 -- must be made invisible as well.
4485
4486 S := Current_Scope;
4487 while Present (S) and then S /= Standard_Standard loop
4488 if Is_Generic_Instance (S)
4489 and then (In_Package_Body (S)
4490 or else Ekind_In (S, E_Procedure, E_Function))
4491 then
4492 -- We still have to remove the entities of the enclosing
4493 -- instance from direct visibility.
4494
4495 declare
4496 E : Entity_Id;
4497 begin
4498 E := First_Entity (S);
4499 while Present (E) loop
4500 Set_Is_Immediately_Visible (E, False);
4501 Next_Entity (E);
4502 end loop;
4503 end;
4504
4505 exit;
4506 end if;
4507
4508 if S = Curr_Unit
4509 or else (Ekind (Curr_Unit) = E_Package_Body
4510 and then S = Spec_Entity (Curr_Unit))
4511 or else (Ekind (Curr_Unit) = E_Subprogram_Body
4512 and then S = Corresponding_Spec
4513 (Unit_Declaration_Node (Curr_Unit)))
4514 then
4515 Removed := True;
4516
4517 -- Remove entities in current scopes from visibility, so that
4518 -- instance body is compiled in a clean environment.
4519
4520 List := Save_Scope_Stack (Handle_Use => False);
4521
4522 if Is_Child_Unit (S) then
4523
4524 -- Remove child unit from stack, as well as inner scopes.
4525 -- Removing the context of a child unit removes parent units
4526 -- as well.
4527
4528 while Current_Scope /= S loop
4529 Num_Inner := Num_Inner + 1;
4530 Inner_Scopes (Num_Inner) := Current_Scope;
4531 Pop_Scope;
4532 end loop;
4533
4534 Pop_Scope;
4535 Remove_Context (Curr_Comp);
4536 Curr_Scope := S;
4537
4538 else
4539 Remove_Context (Curr_Comp);
4540 end if;
4541
4542 if Ekind (Curr_Unit) = E_Package_Body then
4543 Remove_Context (Library_Unit (Curr_Comp));
4544 end if;
4545 end if;
4546
4547 S := Scope (S);
4548 end loop;
4549
4550 pragma Assert (Num_Inner < Num_Scopes);
4551
4552 -- The inlined package body must be analyzed with the SPARK_Mode of
4553 -- the enclosing context, otherwise the body may cause bogus errors
4554 -- if a configuration SPARK_Mode pragma in in effect.
4555
4556 Push_Scope (Standard_Standard);
4557 Scope_Stack.Table (Scope_Stack.Last).Is_Active_Stack_Base := True;
4558 Instantiate_Package_Body
4559 (Body_Info =>
4560 ((Inst_Node => N,
4561 Act_Decl => Act_Decl,
4562 Expander_Status => Expander_Active,
4563 Current_Sem_Unit => Current_Sem_Unit,
4564 Scope_Suppress => Scope_Suppress,
4565 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
4566 Version => Ada_Version,
4567 Version_Pragma => Ada_Version_Pragma,
4568 Warnings => Save_Warnings,
4569 SPARK_Mode => Save_SM,
4570 SPARK_Mode_Pragma => Save_SMP)),
4571 Inlined_Body => True);
4572
4573 Pop_Scope;
4574
4575 -- Restore context
4576
4577 Set_Is_Immediately_Visible (Gen_Comp, Vis);
4578
4579 -- Reset Generic_Instance flag so that use clauses can be installed
4580 -- in the proper order. (See Use_One_Package for effect of enclosing
4581 -- instances on processing of use clauses).
4582
4583 for J in 1 .. N_Instances loop
4584 Set_Is_Generic_Instance (Instances (J), False);
4585 end loop;
4586
4587 if Removed then
4588 Install_Context (Curr_Comp);
4589
4590 if Present (Curr_Scope)
4591 and then Is_Child_Unit (Curr_Scope)
4592 then
4593 Push_Scope (Curr_Scope);
4594 Set_Is_Immediately_Visible (Curr_Scope);
4595
4596 -- Finally, restore inner scopes as well
4597
4598 for J in reverse 1 .. Num_Inner loop
4599 Push_Scope (Inner_Scopes (J));
4600 end loop;
4601 end if;
4602
4603 Restore_Scope_Stack (List, Handle_Use => False);
4604
4605 if Present (Curr_Scope)
4606 and then
4607 (In_Private_Part (Curr_Scope)
4608 or else In_Package_Body (Curr_Scope))
4609 then
4610 -- Install private declaration of ancestor units, which are
4611 -- currently available. Restore_Scope_Stack and Install_Context
4612 -- only install the visible part of parents.
4613
4614 declare
4615 Par : Entity_Id;
4616 begin
4617 Par := Scope (Curr_Scope);
4618 while (Present (Par)) and then Par /= Standard_Standard loop
4619 Install_Private_Declarations (Par);
4620 Par := Scope (Par);
4621 end loop;
4622 end;
4623 end if;
4624 end if;
4625
4626 -- Restore use clauses. For a child unit, use clauses in the parents
4627 -- are restored when installing the context, so only those in inner
4628 -- scopes (and those local to the child unit itself) need to be
4629 -- installed explicitly.
4630
4631 if Is_Child_Unit (Curr_Unit) and then Removed then
4632 for J in reverse 1 .. Num_Inner + 1 loop
4633 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
4634 Use_Clauses (J);
4635 Install_Use_Clauses (Use_Clauses (J));
4636 end loop;
4637
4638 else
4639 for J in reverse 1 .. Num_Scopes loop
4640 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
4641 Use_Clauses (J);
4642 Install_Use_Clauses (Use_Clauses (J));
4643 end loop;
4644 end if;
4645
4646 -- Restore status of instances. If one of them is a body, make its
4647 -- local entities visible again.
4648
4649 declare
4650 E : Entity_Id;
4651 Inst : Entity_Id;
4652
4653 begin
4654 for J in 1 .. N_Instances loop
4655 Inst := Instances (J);
4656 Set_Is_Generic_Instance (Inst, True);
4657
4658 if In_Package_Body (Inst)
4659 or else Ekind_In (S, E_Procedure, E_Function)
4660 then
4661 E := First_Entity (Instances (J));
4662 while Present (E) loop
4663 Set_Is_Immediately_Visible (E);
4664 Next_Entity (E);
4665 end loop;
4666 end if;
4667 end loop;
4668 end;
4669
4670 -- If generic unit is in current unit, current context is correct. Note
4671 -- that the context is guaranteed to carry the correct SPARK_Mode as no
4672 -- enclosing scopes were removed.
4673
4674 else
4675 Instantiate_Package_Body
4676 (Body_Info =>
4677 ((Inst_Node => N,
4678 Act_Decl => Act_Decl,
4679 Expander_Status => Expander_Active,
4680 Current_Sem_Unit => Current_Sem_Unit,
4681 Scope_Suppress => Scope_Suppress,
4682 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
4683 Version => Ada_Version,
4684 Version_Pragma => Ada_Version_Pragma,
4685 Warnings => Save_Warnings,
4686 SPARK_Mode => SPARK_Mode,
4687 SPARK_Mode_Pragma => SPARK_Mode_Pragma)),
4688 Inlined_Body => True);
4689 end if;
4690 end Inline_Instance_Body;
4691
4692 -------------------------------------
4693 -- Analyze_Procedure_Instantiation --
4694 -------------------------------------
4695
4696 procedure Analyze_Procedure_Instantiation (N : Node_Id) is
4697 begin
4698 Analyze_Subprogram_Instantiation (N, E_Procedure);
4699 end Analyze_Procedure_Instantiation;
4700
4701 -----------------------------------
4702 -- Need_Subprogram_Instance_Body --
4703 -----------------------------------
4704
4705 function Need_Subprogram_Instance_Body
4706 (N : Node_Id;
4707 Subp : Entity_Id) return Boolean
4708 is
4709
4710 function Is_Inlined_Or_Child_Of_Inlined (E : Entity_Id) return Boolean;
4711 -- Return True if E is an inlined subprogram, an inlined renaming or a
4712 -- subprogram nested in an inlined subprogram. The inlining machinery
4713 -- totally disregards nested subprograms since it considers that they
4714 -- will always be compiled if the parent is (see Inline.Is_Nested).
4715
4716 ------------------------------------
4717 -- Is_Inlined_Or_Child_Of_Inlined --
4718 ------------------------------------
4719
4720 function Is_Inlined_Or_Child_Of_Inlined (E : Entity_Id) return Boolean is
4721 Scop : Entity_Id;
4722
4723 begin
4724 if Is_Inlined (E) or else Is_Inlined (Alias (E)) then
4725 return True;
4726 end if;
4727
4728 Scop := Scope (E);
4729 while Scop /= Standard_Standard loop
4730 if Ekind (Scop) in Subprogram_Kind and then Is_Inlined (Scop) then
4731 return True;
4732 end if;
4733
4734 Scop := Scope (Scop);
4735 end loop;
4736
4737 return False;
4738 end Is_Inlined_Or_Child_Of_Inlined;
4739
4740 begin
4741 -- Must be in the main unit or inlined (or child of inlined)
4742
4743 if (Is_In_Main_Unit (N) or else Is_Inlined_Or_Child_Of_Inlined (Subp))
4744
4745 -- Must be generating code or analyzing code in ASIS/GNATprove mode
4746
4747 and then (Operating_Mode = Generate_Code
4748 or else (Operating_Mode = Check_Semantics
4749 and then (ASIS_Mode or GNATprove_Mode)))
4750
4751 -- The body is needed when generating code (full expansion), in ASIS
4752 -- mode for other tools, and in GNATprove mode (special expansion) for
4753 -- formal verification of the body itself.
4754
4755 and then (Expander_Active or ASIS_Mode or GNATprove_Mode)
4756
4757 -- No point in inlining if ABE is inevitable
4758
4759 and then not ABE_Is_Certain (N)
4760
4761 -- Or if subprogram is eliminated
4762
4763 and then not Is_Eliminated (Subp)
4764 then
4765 Add_Pending_Instantiation (N, Unit_Declaration_Node (Subp));
4766 return True;
4767
4768 -- Here if not inlined, or we ignore the inlining
4769
4770 else
4771 return False;
4772 end if;
4773 end Need_Subprogram_Instance_Body;
4774
4775 --------------------------------------
4776 -- Analyze_Subprogram_Instantiation --
4777 --------------------------------------
4778
4779 procedure Analyze_Subprogram_Instantiation
4780 (N : Node_Id;
4781 K : Entity_Kind)
4782 is
4783 Loc : constant Source_Ptr := Sloc (N);
4784 Gen_Id : constant Node_Id := Name (N);
4785
4786 Anon_Id : constant Entity_Id :=
4787 Make_Defining_Identifier (Sloc (Defining_Entity (N)),
4788 Chars => New_External_Name
4789 (Chars (Defining_Entity (N)), 'R'));
4790
4791 Act_Decl_Id : Entity_Id;
4792 Act_Decl : Node_Id;
4793 Act_Spec : Node_Id;
4794 Act_Tree : Node_Id;
4795
4796 Env_Installed : Boolean := False;
4797 Gen_Unit : Entity_Id;
4798 Gen_Decl : Node_Id;
4799 Pack_Id : Entity_Id;
4800 Parent_Installed : Boolean := False;
4801
4802 Renaming_List : List_Id;
4803 -- The list of declarations that link formals and actuals of the
4804 -- instance. These are subtype declarations for formal types, and
4805 -- renaming declarations for other formals. The subprogram declaration
4806 -- for the instance is then appended to the list, and the last item on
4807 -- the list is the renaming declaration for the instance.
4808
4809 procedure Analyze_Instance_And_Renamings;
4810 -- The instance must be analyzed in a context that includes the mappings
4811 -- of generic parameters into actuals. We create a package declaration
4812 -- for this purpose, and a subprogram with an internal name within the
4813 -- package. The subprogram instance is simply an alias for the internal
4814 -- subprogram, declared in the current scope.
4815
4816 procedure Build_Subprogram_Renaming;
4817 -- If the subprogram is recursive, there are occurrences of the name of
4818 -- the generic within the body, which must resolve to the current
4819 -- instance. We add a renaming declaration after the declaration, which
4820 -- is available in the instance body, as well as in the analysis of
4821 -- aspects that appear in the generic. This renaming declaration is
4822 -- inserted after the instance declaration which it renames.
4823
4824 ------------------------------------
4825 -- Analyze_Instance_And_Renamings --
4826 ------------------------------------
4827
4828 procedure Analyze_Instance_And_Renamings is
4829 Def_Ent : constant Entity_Id := Defining_Entity (N);
4830 Pack_Decl : Node_Id;
4831
4832 begin
4833 if Nkind (Parent (N)) = N_Compilation_Unit then
4834
4835 -- For the case of a compilation unit, the container package has
4836 -- the same name as the instantiation, to insure that the binder
4837 -- calls the elaboration procedure with the right name. Copy the
4838 -- entity of the instance, which may have compilation level flags
4839 -- (e.g. Is_Child_Unit) set.
4840
4841 Pack_Id := New_Copy (Def_Ent);
4842
4843 else
4844 -- Otherwise we use the name of the instantiation concatenated
4845 -- with its source position to ensure uniqueness if there are
4846 -- several instantiations with the same name.
4847
4848 Pack_Id :=
4849 Make_Defining_Identifier (Loc,
4850 Chars => New_External_Name
4851 (Related_Id => Chars (Def_Ent),
4852 Suffix => "GP",
4853 Suffix_Index => Source_Offset (Sloc (Def_Ent))));
4854 end if;
4855
4856 Pack_Decl :=
4857 Make_Package_Declaration (Loc,
4858 Specification => Make_Package_Specification (Loc,
4859 Defining_Unit_Name => Pack_Id,
4860 Visible_Declarations => Renaming_List,
4861 End_Label => Empty));
4862
4863 Set_Instance_Spec (N, Pack_Decl);
4864 Set_Is_Generic_Instance (Pack_Id);
4865 Set_Debug_Info_Needed (Pack_Id);
4866
4867 -- Case of not a compilation unit
4868
4869 if Nkind (Parent (N)) /= N_Compilation_Unit then
4870 Mark_Rewrite_Insertion (Pack_Decl);
4871 Insert_Before (N, Pack_Decl);
4872 Set_Has_Completion (Pack_Id);
4873
4874 -- Case of an instantiation that is a compilation unit
4875
4876 -- Place declaration on current node so context is complete for
4877 -- analysis (including nested instantiations), and for use in a
4878 -- context_clause (see Analyze_With_Clause).
4879
4880 else
4881 Set_Unit (Parent (N), Pack_Decl);
4882 Set_Parent_Spec (Pack_Decl, Parent_Spec (N));
4883 end if;
4884
4885 Analyze (Pack_Decl);
4886 Check_Formal_Packages (Pack_Id);
4887 Set_Is_Generic_Instance (Pack_Id, False);
4888
4889 -- Why do we clear Is_Generic_Instance??? We set it 20 lines
4890 -- above???
4891
4892 -- Body of the enclosing package is supplied when instantiating the
4893 -- subprogram body, after semantic analysis is completed.
4894
4895 if Nkind (Parent (N)) = N_Compilation_Unit then
4896
4897 -- Remove package itself from visibility, so it does not
4898 -- conflict with subprogram.
4899
4900 Set_Name_Entity_Id (Chars (Pack_Id), Homonym (Pack_Id));
4901
4902 -- Set name and scope of internal subprogram so that the proper
4903 -- external name will be generated. The proper scope is the scope
4904 -- of the wrapper package. We need to generate debugging info for
4905 -- the internal subprogram, so set flag accordingly.
4906
4907 Set_Chars (Anon_Id, Chars (Defining_Entity (N)));
4908 Set_Scope (Anon_Id, Scope (Pack_Id));
4909
4910 -- Mark wrapper package as referenced, to avoid spurious warnings
4911 -- if the instantiation appears in various with_ clauses of
4912 -- subunits of the main unit.
4913
4914 Set_Referenced (Pack_Id);
4915 end if;
4916
4917 Set_Is_Generic_Instance (Anon_Id);
4918 Set_Debug_Info_Needed (Anon_Id);
4919 Act_Decl_Id := New_Copy (Anon_Id);
4920
4921 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
4922 Set_Chars (Act_Decl_Id, Chars (Defining_Entity (N)));
4923 Set_Sloc (Act_Decl_Id, Sloc (Defining_Entity (N)));
4924
4925 -- Subprogram instance comes from source only if generic does
4926
4927 Set_Comes_From_Source (Act_Decl_Id, Comes_From_Source (Gen_Unit));
4928
4929 -- If the instance is a child unit, mark the Id accordingly. Mark
4930 -- the anonymous entity as well, which is the real subprogram and
4931 -- which is used when the instance appears in a context clause.
4932 -- Similarly, propagate the Is_Eliminated flag to handle properly
4933 -- nested eliminated subprograms.
4934
4935 Set_Is_Child_Unit (Act_Decl_Id, Is_Child_Unit (Defining_Entity (N)));
4936 Set_Is_Child_Unit (Anon_Id, Is_Child_Unit (Defining_Entity (N)));
4937 New_Overloaded_Entity (Act_Decl_Id);
4938 Check_Eliminated (Act_Decl_Id);
4939 Set_Is_Eliminated (Anon_Id, Is_Eliminated (Act_Decl_Id));
4940
4941 -- In compilation unit case, kill elaboration checks on the
4942 -- instantiation, since they are never needed -- the body is
4943 -- instantiated at the same point as the spec.
4944
4945 if Nkind (Parent (N)) = N_Compilation_Unit then
4946 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
4947 Set_Kill_Elaboration_Checks (Act_Decl_Id);
4948 Set_Is_Compilation_Unit (Anon_Id);
4949
4950 Set_Cunit_Entity (Current_Sem_Unit, Pack_Id);
4951 end if;
4952
4953 -- The instance is not a freezing point for the new subprogram.
4954 -- The anonymous subprogram may have a freeze node, created for
4955 -- some delayed aspects. This freeze node must not be inherited
4956 -- by the visible subprogram entity.
4957
4958 Set_Is_Frozen (Act_Decl_Id, False);
4959 Set_Freeze_Node (Act_Decl_Id, Empty);
4960
4961 if Nkind (Defining_Entity (N)) = N_Defining_Operator_Symbol then
4962 Valid_Operator_Definition (Act_Decl_Id);
4963 end if;
4964
4965 Set_Alias (Act_Decl_Id, Anon_Id);
4966 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
4967 Set_Has_Completion (Act_Decl_Id);
4968 Set_Related_Instance (Pack_Id, Act_Decl_Id);
4969
4970 if Nkind (Parent (N)) = N_Compilation_Unit then
4971 Set_Body_Required (Parent (N), False);
4972 end if;
4973 end Analyze_Instance_And_Renamings;
4974
4975 -------------------------------
4976 -- Build_Subprogram_Renaming --
4977 -------------------------------
4978
4979 procedure Build_Subprogram_Renaming is
4980 Renaming_Decl : Node_Id;
4981 Unit_Renaming : Node_Id;
4982
4983 begin
4984 Unit_Renaming :=
4985 Make_Subprogram_Renaming_Declaration (Loc,
4986 Specification =>
4987 Copy_Generic_Node
4988 (Specification (Original_Node (Gen_Decl)),
4989 Empty,
4990 Instantiating => True),
4991 Name => New_Occurrence_Of (Anon_Id, Loc));
4992
4993 -- The generic may be a a child unit. The renaming needs an
4994 -- identifier with the proper name.
4995
4996 Set_Defining_Unit_Name (Specification (Unit_Renaming),
4997 Make_Defining_Identifier (Loc, Chars (Gen_Unit)));
4998
4999 -- If there is a formal subprogram with the same name as the unit
5000 -- itself, do not add this renaming declaration, to prevent
5001 -- ambiguities when there is a call with that name in the body.
5002 -- This is a partial and ugly fix for one ACATS test. ???
5003
5004 Renaming_Decl := First (Renaming_List);
5005 while Present (Renaming_Decl) loop
5006 if Nkind (Renaming_Decl) = N_Subprogram_Renaming_Declaration
5007 and then
5008 Chars (Defining_Entity (Renaming_Decl)) = Chars (Gen_Unit)
5009 then
5010 exit;
5011 end if;
5012
5013 Next (Renaming_Decl);
5014 end loop;
5015
5016 if No (Renaming_Decl) then
5017 Append (Unit_Renaming, Renaming_List);
5018 end if;
5019 end Build_Subprogram_Renaming;
5020
5021 -- Local variables
5022
5023 Save_IPSM : constant Boolean := Ignore_Pragma_SPARK_Mode;
5024 -- Save flag Ignore_Pragma_SPARK_Mode for restore on exit
5025
5026 Save_SM : constant SPARK_Mode_Type := SPARK_Mode;
5027 Save_SMP : constant Node_Id := SPARK_Mode_Pragma;
5028 -- Save the SPARK_Mode-related data for restore on exit
5029
5030 Vis_Prims_List : Elist_Id := No_Elist;
5031 -- List of primitives made temporarily visible in the instantiation
5032 -- to match the visibility of the formal type
5033
5034 -- Start of processing for Analyze_Subprogram_Instantiation
5035
5036 begin
5037 Check_SPARK_05_Restriction ("generic is not allowed", N);
5038
5039 -- Very first thing: check for special Text_IO unit in case we are
5040 -- instantiating one of the children of [[Wide_]Wide_]Text_IO. Of course
5041 -- such an instantiation is bogus (these are packages, not subprograms),
5042 -- but we get a better error message if we do this.
5043
5044 Check_Text_IO_Special_Unit (Gen_Id);
5045
5046 -- Make node global for error reporting
5047
5048 Instantiation_Node := N;
5049
5050 -- For package instantiations we turn off style checks, because they
5051 -- will have been emitted in the generic. For subprogram instantiations
5052 -- we want to apply at least the check on overriding indicators so we
5053 -- do not modify the style check status.
5054
5055 -- The renaming declarations for the actuals do not come from source and
5056 -- will not generate spurious warnings.
5057
5058 Preanalyze_Actuals (N);
5059
5060 Init_Env;
5061 Env_Installed := True;
5062 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
5063 Gen_Unit := Entity (Gen_Id);
5064
5065 Generate_Reference (Gen_Unit, Gen_Id);
5066
5067 if Nkind (Gen_Id) = N_Identifier
5068 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
5069 then
5070 Error_Msg_NE
5071 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
5072 end if;
5073
5074 if Etype (Gen_Unit) = Any_Type then
5075 Restore_Env;
5076 return;
5077 end if;
5078
5079 -- Verify that it is a generic subprogram of the right kind, and that
5080 -- it does not lead to a circular instantiation.
5081
5082 if K = E_Procedure and then Ekind (Gen_Unit) /= E_Generic_Procedure then
5083 Error_Msg_NE
5084 ("& is not the name of a generic procedure", Gen_Id, Gen_Unit);
5085
5086 elsif K = E_Function and then Ekind (Gen_Unit) /= E_Generic_Function then
5087 Error_Msg_NE
5088 ("& is not the name of a generic function", Gen_Id, Gen_Unit);
5089
5090 elsif In_Open_Scopes (Gen_Unit) then
5091 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
5092
5093 else
5094 -- If the context of the instance is subject to SPARK_Mode "off" or
5095 -- the annotation is altogether missing, set the global flag which
5096 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
5097 -- the instance.
5098
5099 if SPARK_Mode /= On then
5100 Ignore_Pragma_SPARK_Mode := True;
5101 end if;
5102
5103 Set_Entity (Gen_Id, Gen_Unit);
5104 Set_Is_Instantiated (Gen_Unit);
5105
5106 if In_Extended_Main_Source_Unit (N) then
5107 Generate_Reference (Gen_Unit, N);
5108 end if;
5109
5110 -- If renaming, get original unit
5111
5112 if Present (Renamed_Object (Gen_Unit))
5113 and then Ekind_In (Renamed_Object (Gen_Unit), E_Generic_Procedure,
5114 E_Generic_Function)
5115 then
5116 Gen_Unit := Renamed_Object (Gen_Unit);
5117 Set_Is_Instantiated (Gen_Unit);
5118 Generate_Reference (Gen_Unit, N);
5119 end if;
5120
5121 if Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
5122 Error_Msg_Node_2 := Current_Scope;
5123 Error_Msg_NE
5124 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
5125 Circularity_Detected := True;
5126 Restore_Hidden_Primitives (Vis_Prims_List);
5127 goto Leave;
5128 end if;
5129
5130 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
5131
5132 -- Initialize renamings map, for error checking
5133
5134 Generic_Renamings.Set_Last (0);
5135 Generic_Renamings_HTable.Reset;
5136
5137 Create_Instantiation_Source (N, Gen_Unit, False, S_Adjustment);
5138
5139 -- Copy original generic tree, to produce text for instantiation
5140
5141 Act_Tree :=
5142 Copy_Generic_Node
5143 (Original_Node (Gen_Decl), Empty, Instantiating => True);
5144
5145 -- Inherit overriding indicator from instance node
5146
5147 Act_Spec := Specification (Act_Tree);
5148 Set_Must_Override (Act_Spec, Must_Override (N));
5149 Set_Must_Not_Override (Act_Spec, Must_Not_Override (N));
5150
5151 Renaming_List :=
5152 Analyze_Associations
5153 (I_Node => N,
5154 Formals => Generic_Formal_Declarations (Act_Tree),
5155 F_Copy => Generic_Formal_Declarations (Gen_Decl));
5156
5157 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
5158
5159 -- The subprogram itself cannot contain a nested instance, so the
5160 -- current parent is left empty.
5161
5162 Set_Instance_Env (Gen_Unit, Empty);
5163
5164 -- Build the subprogram declaration, which does not appear in the
5165 -- generic template, and give it a sloc consistent with that of the
5166 -- template.
5167
5168 Set_Defining_Unit_Name (Act_Spec, Anon_Id);
5169 Set_Generic_Parent (Act_Spec, Gen_Unit);
5170 Act_Decl :=
5171 Make_Subprogram_Declaration (Sloc (Act_Spec),
5172 Specification => Act_Spec);
5173
5174 -- The aspects have been copied previously, but they have to be
5175 -- linked explicitly to the new subprogram declaration. Explicit
5176 -- pre/postconditions on the instance are analyzed below, in a
5177 -- separate step.
5178
5179 Move_Aspects (Act_Tree, To => Act_Decl);
5180 Set_Categorization_From_Pragmas (Act_Decl);
5181
5182 if Parent_Installed then
5183 Hide_Current_Scope;
5184 end if;
5185
5186 Append (Act_Decl, Renaming_List);
5187
5188 -- Contract-related source pragmas that follow a generic subprogram
5189 -- must be instantiated explicitly because they are not part of the
5190 -- subprogram template.
5191
5192 Instantiate_Subprogram_Contract
5193 (Original_Node (Gen_Decl), Renaming_List);
5194
5195 Build_Subprogram_Renaming;
5196 Analyze_Instance_And_Renamings;
5197
5198 -- If the generic is marked Import (Intrinsic), then so is the
5199 -- instance. This indicates that there is no body to instantiate. If
5200 -- generic is marked inline, so it the instance, and the anonymous
5201 -- subprogram it renames. If inlined, or else if inlining is enabled
5202 -- for the compilation, we generate the instance body even if it is
5203 -- not within the main unit.
5204
5205 if Is_Intrinsic_Subprogram (Gen_Unit) then
5206 Set_Is_Intrinsic_Subprogram (Anon_Id);
5207 Set_Is_Intrinsic_Subprogram (Act_Decl_Id);
5208
5209 if Chars (Gen_Unit) = Name_Unchecked_Conversion then
5210 Validate_Unchecked_Conversion (N, Act_Decl_Id);
5211 end if;
5212 end if;
5213
5214 -- Inherit convention from generic unit. Intrinsic convention, as for
5215 -- an instance of unchecked conversion, is not inherited because an
5216 -- explicit Ada instance has been created.
5217
5218 if Has_Convention_Pragma (Gen_Unit)
5219 and then Convention (Gen_Unit) /= Convention_Intrinsic
5220 then
5221 Set_Convention (Act_Decl_Id, Convention (Gen_Unit));
5222 Set_Is_Exported (Act_Decl_Id, Is_Exported (Gen_Unit));
5223 end if;
5224
5225 Generate_Definition (Act_Decl_Id);
5226
5227 -- Inherit all inlining-related flags which apply to the generic in
5228 -- the subprogram and its declaration.
5229
5230 Set_Is_Inlined (Act_Decl_Id, Is_Inlined (Gen_Unit));
5231 Set_Is_Inlined (Anon_Id, Is_Inlined (Gen_Unit));
5232
5233 Set_Has_Pragma_Inline (Act_Decl_Id, Has_Pragma_Inline (Gen_Unit));
5234 Set_Has_Pragma_Inline (Anon_Id, Has_Pragma_Inline (Gen_Unit));
5235
5236 Set_Has_Pragma_Inline_Always
5237 (Act_Decl_Id, Has_Pragma_Inline_Always (Gen_Unit));
5238 Set_Has_Pragma_Inline_Always
5239 (Anon_Id, Has_Pragma_Inline_Always (Gen_Unit));
5240
5241 if not Is_Intrinsic_Subprogram (Gen_Unit) then
5242 Check_Elab_Instantiation (N);
5243 end if;
5244
5245 if Is_Dispatching_Operation (Act_Decl_Id)
5246 and then Ada_Version >= Ada_2005
5247 then
5248 declare
5249 Formal : Entity_Id;
5250
5251 begin
5252 Formal := First_Formal (Act_Decl_Id);
5253 while Present (Formal) loop
5254 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type
5255 and then Is_Controlling_Formal (Formal)
5256 and then not Can_Never_Be_Null (Formal)
5257 then
5258 Error_Msg_NE
5259 ("access parameter& is controlling,", N, Formal);
5260 Error_Msg_NE
5261 ("\corresponding parameter of & must be "
5262 & "explicitly null-excluding", N, Gen_Id);
5263 end if;
5264
5265 Next_Formal (Formal);
5266 end loop;
5267 end;
5268 end if;
5269
5270 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
5271
5272 Validate_Categorization_Dependency (N, Act_Decl_Id);
5273
5274 if not Is_Intrinsic_Subprogram (Act_Decl_Id) then
5275 Inherit_Context (Gen_Decl, N);
5276
5277 Restore_Private_Views (Pack_Id, False);
5278
5279 -- If the context requires a full instantiation, mark node for
5280 -- subsequent construction of the body.
5281
5282 if Need_Subprogram_Instance_Body (N, Act_Decl_Id) then
5283 Check_Forward_Instantiation (Gen_Decl);
5284
5285 -- The wrapper package is always delayed, because it does not
5286 -- constitute a freeze point, but to insure that the freeze node
5287 -- is placed properly, it is created directly when instantiating
5288 -- the body (otherwise the freeze node might appear to early for
5289 -- nested instantiations). For ASIS purposes, indicate that the
5290 -- wrapper package has replaced the instantiation node.
5291
5292 elsif Nkind (Parent (N)) = N_Compilation_Unit then
5293 Rewrite (N, Unit (Parent (N)));
5294 Set_Unit (Parent (N), N);
5295 end if;
5296
5297 -- Replace instance node for library-level instantiations of
5298 -- intrinsic subprograms, for ASIS use.
5299
5300 elsif Nkind (Parent (N)) = N_Compilation_Unit then
5301 Rewrite (N, Unit (Parent (N)));
5302 Set_Unit (Parent (N), N);
5303 end if;
5304
5305 if Parent_Installed then
5306 Remove_Parent;
5307 end if;
5308
5309 Restore_Hidden_Primitives (Vis_Prims_List);
5310 Restore_Env;
5311 Env_Installed := False;
5312 Generic_Renamings.Set_Last (0);
5313 Generic_Renamings_HTable.Reset;
5314
5315 Ignore_Pragma_SPARK_Mode := Save_IPSM;
5316 SPARK_Mode := Save_SM;
5317 SPARK_Mode_Pragma := Save_SMP;
5318
5319 if SPARK_Mode = On then
5320 Dynamic_Elaboration_Checks := False;
5321 end if;
5322 end if;
5323
5324 <<Leave>>
5325 if Has_Aspects (N) then
5326 Analyze_Aspect_Specifications (N, Act_Decl_Id);
5327 end if;
5328
5329 exception
5330 when Instantiation_Error =>
5331 if Parent_Installed then
5332 Remove_Parent;
5333 end if;
5334
5335 if Env_Installed then
5336 Restore_Env;
5337 end if;
5338
5339 Ignore_Pragma_SPARK_Mode := Save_IPSM;
5340 SPARK_Mode := Save_SM;
5341 SPARK_Mode_Pragma := Save_SMP;
5342
5343 if SPARK_Mode = On then
5344 Dynamic_Elaboration_Checks := False;
5345 end if;
5346 end Analyze_Subprogram_Instantiation;
5347
5348 -------------------------
5349 -- Get_Associated_Node --
5350 -------------------------
5351
5352 function Get_Associated_Node (N : Node_Id) return Node_Id is
5353 Assoc : Node_Id;
5354
5355 begin
5356 Assoc := Associated_Node (N);
5357
5358 if Nkind (Assoc) /= Nkind (N) then
5359 return Assoc;
5360
5361 elsif Nkind_In (Assoc, N_Aggregate, N_Extension_Aggregate) then
5362 return Assoc;
5363
5364 else
5365 -- If the node is part of an inner generic, it may itself have been
5366 -- remapped into a further generic copy. Associated_Node is otherwise
5367 -- used for the entity of the node, and will be of a different node
5368 -- kind, or else N has been rewritten as a literal or function call.
5369
5370 while Present (Associated_Node (Assoc))
5371 and then Nkind (Associated_Node (Assoc)) = Nkind (Assoc)
5372 loop
5373 Assoc := Associated_Node (Assoc);
5374 end loop;
5375
5376 -- Follow and additional link in case the final node was rewritten.
5377 -- This can only happen with nested generic units.
5378
5379 if (Nkind (Assoc) = N_Identifier or else Nkind (Assoc) in N_Op)
5380 and then Present (Associated_Node (Assoc))
5381 and then (Nkind_In (Associated_Node (Assoc), N_Function_Call,
5382 N_Explicit_Dereference,
5383 N_Integer_Literal,
5384 N_Real_Literal,
5385 N_String_Literal))
5386 then
5387 Assoc := Associated_Node (Assoc);
5388 end if;
5389
5390 -- An additional special case: an unconstrained type in an object
5391 -- declaration may have been rewritten as a local subtype constrained
5392 -- by the expression in the declaration. We need to recover the
5393 -- original entity which may be global.
5394
5395 if Present (Original_Node (Assoc))
5396 and then Nkind (Parent (N)) = N_Object_Declaration
5397 then
5398 Assoc := Original_Node (Assoc);
5399 end if;
5400
5401 return Assoc;
5402 end if;
5403 end Get_Associated_Node;
5404
5405 ----------------------------
5406 -- Build_Function_Wrapper --
5407 ----------------------------
5408
5409 function Build_Function_Wrapper
5410 (Formal_Subp : Entity_Id;
5411 Actual_Subp : Entity_Id) return Node_Id
5412 is
5413 Loc : constant Source_Ptr := Sloc (Current_Scope);
5414 Ret_Type : constant Entity_Id := Get_Instance_Of (Etype (Formal_Subp));
5415 Actuals : List_Id;
5416 Decl : Node_Id;
5417 Func_Name : Node_Id;
5418 Func : Entity_Id;
5419 Parm_Type : Node_Id;
5420 Profile : List_Id := New_List;
5421 Spec : Node_Id;
5422 Act_F : Entity_Id;
5423 Form_F : Entity_Id;
5424 New_F : Entity_Id;
5425
5426 begin
5427 Func_Name := New_Occurrence_Of (Actual_Subp, Loc);
5428
5429 Func := Make_Defining_Identifier (Loc, Chars (Formal_Subp));
5430 Set_Ekind (Func, E_Function);
5431 Set_Is_Generic_Actual_Subprogram (Func);
5432
5433 Actuals := New_List;
5434 Profile := New_List;
5435
5436 Act_F := First_Formal (Actual_Subp);
5437 Form_F := First_Formal (Formal_Subp);
5438 while Present (Form_F) loop
5439
5440 -- Create new formal for profile of wrapper, and add a reference
5441 -- to it in the list of actuals for the enclosing call. The name
5442 -- must be that of the formal in the formal subprogram, because
5443 -- calls to it in the generic body may use named associations.
5444
5445 New_F := Make_Defining_Identifier (Loc, Chars (Form_F));
5446
5447 Parm_Type :=
5448 New_Occurrence_Of (Get_Instance_Of (Etype (Form_F)), Loc);
5449
5450 Append_To (Profile,
5451 Make_Parameter_Specification (Loc,
5452 Defining_Identifier => New_F,
5453 Parameter_Type => Parm_Type));
5454
5455 Append_To (Actuals, New_Occurrence_Of (New_F, Loc));
5456 Next_Formal (Form_F);
5457
5458 if Present (Act_F) then
5459 Next_Formal (Act_F);
5460 end if;
5461 end loop;
5462
5463 Spec :=
5464 Make_Function_Specification (Loc,
5465 Defining_Unit_Name => Func,
5466 Parameter_Specifications => Profile,
5467 Result_Definition => New_Occurrence_Of (Ret_Type, Loc));
5468
5469 Decl :=
5470 Make_Expression_Function (Loc,
5471 Specification => Spec,
5472 Expression =>
5473 Make_Function_Call (Loc,
5474 Name => Func_Name,
5475 Parameter_Associations => Actuals));
5476
5477 return Decl;
5478 end Build_Function_Wrapper;
5479
5480 ----------------------------
5481 -- Build_Operator_Wrapper --
5482 ----------------------------
5483
5484 function Build_Operator_Wrapper
5485 (Formal_Subp : Entity_Id;
5486 Actual_Subp : Entity_Id) return Node_Id
5487 is
5488 Loc : constant Source_Ptr := Sloc (Current_Scope);
5489 Ret_Type : constant Entity_Id :=
5490 Get_Instance_Of (Etype (Formal_Subp));
5491 Op_Type : constant Entity_Id :=
5492 Get_Instance_Of (Etype (First_Formal (Formal_Subp)));
5493 Is_Binary : constant Boolean :=
5494 Present (Next_Formal (First_Formal (Formal_Subp)));
5495
5496 Decl : Node_Id;
5497 Expr : Node_Id;
5498 F1, F2 : Entity_Id;
5499 Func : Entity_Id;
5500 Op_Name : Name_Id;
5501 Spec : Node_Id;
5502 L, R : Node_Id;
5503
5504 begin
5505 Op_Name := Chars (Actual_Subp);
5506
5507 -- Create entities for wrapper function and its formals
5508
5509 F1 := Make_Temporary (Loc, 'A');
5510 F2 := Make_Temporary (Loc, 'B');
5511 L := New_Occurrence_Of (F1, Loc);
5512 R := New_Occurrence_Of (F2, Loc);
5513
5514 Func := Make_Defining_Identifier (Loc, Chars (Formal_Subp));
5515 Set_Ekind (Func, E_Function);
5516 Set_Is_Generic_Actual_Subprogram (Func);
5517
5518 Spec :=
5519 Make_Function_Specification (Loc,
5520 Defining_Unit_Name => Func,
5521 Parameter_Specifications => New_List (
5522 Make_Parameter_Specification (Loc,
5523 Defining_Identifier => F1,
5524 Parameter_Type => New_Occurrence_Of (Op_Type, Loc))),
5525 Result_Definition => New_Occurrence_Of (Ret_Type, Loc));
5526
5527 if Is_Binary then
5528 Append_To (Parameter_Specifications (Spec),
5529 Make_Parameter_Specification (Loc,
5530 Defining_Identifier => F2,
5531 Parameter_Type => New_Occurrence_Of (Op_Type, Loc)));
5532 end if;
5533
5534 -- Build expression as a function call, or as an operator node
5535 -- that corresponds to the name of the actual, starting with
5536 -- binary operators.
5537
5538 if Op_Name not in Any_Operator_Name then
5539 Expr :=
5540 Make_Function_Call (Loc,
5541 Name =>
5542 New_Occurrence_Of (Actual_Subp, Loc),
5543 Parameter_Associations => New_List (L));
5544
5545 if Is_Binary then
5546 Append_To (Parameter_Associations (Expr), R);
5547 end if;
5548
5549 -- Binary operators
5550
5551 elsif Is_Binary then
5552 if Op_Name = Name_Op_And then
5553 Expr := Make_Op_And (Loc, Left_Opnd => L, Right_Opnd => R);
5554 elsif Op_Name = Name_Op_Or then
5555 Expr := Make_Op_Or (Loc, Left_Opnd => L, Right_Opnd => R);
5556 elsif Op_Name = Name_Op_Xor then
5557 Expr := Make_Op_Xor (Loc, Left_Opnd => L, Right_Opnd => R);
5558 elsif Op_Name = Name_Op_Eq then
5559 Expr := Make_Op_Eq (Loc, Left_Opnd => L, Right_Opnd => R);
5560 elsif Op_Name = Name_Op_Ne then
5561 Expr := Make_Op_Ne (Loc, Left_Opnd => L, Right_Opnd => R);
5562 elsif Op_Name = Name_Op_Le then
5563 Expr := Make_Op_Le (Loc, Left_Opnd => L, Right_Opnd => R);
5564 elsif Op_Name = Name_Op_Gt then
5565 Expr := Make_Op_Gt (Loc, Left_Opnd => L, Right_Opnd => R);
5566 elsif Op_Name = Name_Op_Ge then
5567 Expr := Make_Op_Ge (Loc, Left_Opnd => L, Right_Opnd => R);
5568 elsif Op_Name = Name_Op_Lt then
5569 Expr := Make_Op_Lt (Loc, Left_Opnd => L, Right_Opnd => R);
5570 elsif Op_Name = Name_Op_Add then
5571 Expr := Make_Op_Add (Loc, Left_Opnd => L, Right_Opnd => R);
5572 elsif Op_Name = Name_Op_Subtract then
5573 Expr := Make_Op_Subtract (Loc, Left_Opnd => L, Right_Opnd => R);
5574 elsif Op_Name = Name_Op_Concat then
5575 Expr := Make_Op_Concat (Loc, Left_Opnd => L, Right_Opnd => R);
5576 elsif Op_Name = Name_Op_Multiply then
5577 Expr := Make_Op_Multiply (Loc, Left_Opnd => L, Right_Opnd => R);
5578 elsif Op_Name = Name_Op_Divide then
5579 Expr := Make_Op_Divide (Loc, Left_Opnd => L, Right_Opnd => R);
5580 elsif Op_Name = Name_Op_Mod then
5581 Expr := Make_Op_Mod (Loc, Left_Opnd => L, Right_Opnd => R);
5582 elsif Op_Name = Name_Op_Rem then
5583 Expr := Make_Op_Rem (Loc, Left_Opnd => L, Right_Opnd => R);
5584 elsif Op_Name = Name_Op_Expon then
5585 Expr := Make_Op_Expon (Loc, Left_Opnd => L, Right_Opnd => R);
5586 end if;
5587
5588 -- Unary operators
5589
5590 else
5591 if Op_Name = Name_Op_Add then
5592 Expr := Make_Op_Plus (Loc, Right_Opnd => L);
5593 elsif Op_Name = Name_Op_Subtract then
5594 Expr := Make_Op_Minus (Loc, Right_Opnd => L);
5595 elsif Op_Name = Name_Op_Abs then
5596 Expr := Make_Op_Abs (Loc, Right_Opnd => L);
5597 elsif Op_Name = Name_Op_Not then
5598 Expr := Make_Op_Not (Loc, Right_Opnd => L);
5599 end if;
5600 end if;
5601
5602 Decl :=
5603 Make_Expression_Function (Loc,
5604 Specification => Spec,
5605 Expression => Expr);
5606
5607 return Decl;
5608 end Build_Operator_Wrapper;
5609
5610 -------------------------------------------
5611 -- Build_Instance_Compilation_Unit_Nodes --
5612 -------------------------------------------
5613
5614 procedure Build_Instance_Compilation_Unit_Nodes
5615 (N : Node_Id;
5616 Act_Body : Node_Id;
5617 Act_Decl : Node_Id)
5618 is
5619 Decl_Cunit : Node_Id;
5620 Body_Cunit : Node_Id;
5621 Citem : Node_Id;
5622 New_Main : constant Entity_Id := Defining_Entity (Act_Decl);
5623 Old_Main : constant Entity_Id := Cunit_Entity (Main_Unit);
5624
5625 begin
5626 -- A new compilation unit node is built for the instance declaration
5627
5628 Decl_Cunit :=
5629 Make_Compilation_Unit (Sloc (N),
5630 Context_Items => Empty_List,
5631 Unit => Act_Decl,
5632 Aux_Decls_Node => Make_Compilation_Unit_Aux (Sloc (N)));
5633
5634 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
5635
5636 -- The new compilation unit is linked to its body, but both share the
5637 -- same file, so we do not set Body_Required on the new unit so as not
5638 -- to create a spurious dependency on a non-existent body in the ali.
5639 -- This simplifies CodePeer unit traversal.
5640
5641 -- We use the original instantiation compilation unit as the resulting
5642 -- compilation unit of the instance, since this is the main unit.
5643
5644 Rewrite (N, Act_Body);
5645
5646 -- Propagate the aspect specifications from the package body template to
5647 -- the instantiated version of the package body.
5648
5649 if Has_Aspects (Act_Body) then
5650 Set_Aspect_Specifications
5651 (N, New_Copy_List_Tree (Aspect_Specifications (Act_Body)));
5652 end if;
5653
5654 Body_Cunit := Parent (N);
5655
5656 -- The two compilation unit nodes are linked by the Library_Unit field
5657
5658 Set_Library_Unit (Decl_Cunit, Body_Cunit);
5659 Set_Library_Unit (Body_Cunit, Decl_Cunit);
5660
5661 -- Preserve the private nature of the package if needed
5662
5663 Set_Private_Present (Decl_Cunit, Private_Present (Body_Cunit));
5664
5665 -- If the instance is not the main unit, its context, categorization
5666 -- and elaboration entity are not relevant to the compilation.
5667
5668 if Body_Cunit /= Cunit (Main_Unit) then
5669 Make_Instance_Unit (Body_Cunit, In_Main => False);
5670 return;
5671 end if;
5672
5673 -- The context clause items on the instantiation, which are now attached
5674 -- to the body compilation unit (since the body overwrote the original
5675 -- instantiation node), semantically belong on the spec, so copy them
5676 -- there. It's harmless to leave them on the body as well. In fact one
5677 -- could argue that they belong in both places.
5678
5679 Citem := First (Context_Items (Body_Cunit));
5680 while Present (Citem) loop
5681 Append (New_Copy (Citem), Context_Items (Decl_Cunit));
5682 Next (Citem);
5683 end loop;
5684
5685 -- Propagate categorization flags on packages, so that they appear in
5686 -- the ali file for the spec of the unit.
5687
5688 if Ekind (New_Main) = E_Package then
5689 Set_Is_Pure (Old_Main, Is_Pure (New_Main));
5690 Set_Is_Preelaborated (Old_Main, Is_Preelaborated (New_Main));
5691 Set_Is_Remote_Types (Old_Main, Is_Remote_Types (New_Main));
5692 Set_Is_Shared_Passive (Old_Main, Is_Shared_Passive (New_Main));
5693 Set_Is_Remote_Call_Interface
5694 (Old_Main, Is_Remote_Call_Interface (New_Main));
5695 end if;
5696
5697 -- Make entry in Units table, so that binder can generate call to
5698 -- elaboration procedure for body, if any.
5699
5700 Make_Instance_Unit (Body_Cunit, In_Main => True);
5701 Main_Unit_Entity := New_Main;
5702 Set_Cunit_Entity (Main_Unit, Main_Unit_Entity);
5703
5704 -- Build elaboration entity, since the instance may certainly generate
5705 -- elaboration code requiring a flag for protection.
5706
5707 Build_Elaboration_Entity (Decl_Cunit, New_Main);
5708 end Build_Instance_Compilation_Unit_Nodes;
5709
5710 -----------------------------
5711 -- Check_Access_Definition --
5712 -----------------------------
5713
5714 procedure Check_Access_Definition (N : Node_Id) is
5715 begin
5716 pragma Assert
5717 (Ada_Version >= Ada_2005 and then Present (Access_Definition (N)));
5718 null;
5719 end Check_Access_Definition;
5720
5721 -----------------------------------
5722 -- Check_Formal_Package_Instance --
5723 -----------------------------------
5724
5725 -- If the formal has specific parameters, they must match those of the
5726 -- actual. Both of them are instances, and the renaming declarations for
5727 -- their formal parameters appear in the same order in both. The analyzed
5728 -- formal has been analyzed in the context of the current instance.
5729
5730 procedure Check_Formal_Package_Instance
5731 (Formal_Pack : Entity_Id;
5732 Actual_Pack : Entity_Id)
5733 is
5734 E1 : Entity_Id := First_Entity (Actual_Pack);
5735 E2 : Entity_Id := First_Entity (Formal_Pack);
5736
5737 Expr1 : Node_Id;
5738 Expr2 : Node_Id;
5739
5740 procedure Check_Mismatch (B : Boolean);
5741 -- Common error routine for mismatch between the parameters of the
5742 -- actual instance and those of the formal package.
5743
5744 function Same_Instantiated_Constant (E1, E2 : Entity_Id) return Boolean;
5745 -- The formal may come from a nested formal package, and the actual may
5746 -- have been constant-folded. To determine whether the two denote the
5747 -- same entity we may have to traverse several definitions to recover
5748 -- the ultimate entity that they refer to.
5749
5750 function Same_Instantiated_Function (E1, E2 : Entity_Id) return Boolean;
5751 -- The formal and the actual must be identical, but if both are
5752 -- given by attributes they end up renaming different generated bodies,
5753 -- and we must verify that the attributes themselves match.
5754
5755 function Same_Instantiated_Variable (E1, E2 : Entity_Id) return Boolean;
5756 -- Similarly, if the formal comes from a nested formal package, the
5757 -- actual may designate the formal through multiple renamings, which
5758 -- have to be followed to determine the original variable in question.
5759
5760 --------------------
5761 -- Check_Mismatch --
5762 --------------------
5763
5764 procedure Check_Mismatch (B : Boolean) is
5765 -- A Formal_Type_Declaration for a derived private type is rewritten
5766 -- as a private extension decl. (see Analyze_Formal_Derived_Type),
5767 -- which is why we examine the original node.
5768
5769 Kind : constant Node_Kind := Nkind (Original_Node (Parent (E2)));
5770
5771 begin
5772 if Kind = N_Formal_Type_Declaration then
5773 return;
5774
5775 elsif Nkind_In (Kind, N_Formal_Object_Declaration,
5776 N_Formal_Package_Declaration)
5777 or else Kind in N_Formal_Subprogram_Declaration
5778 then
5779 null;
5780
5781 -- Ada 2012: If both formal and actual are incomplete types they
5782 -- are conformant.
5783
5784 elsif Is_Incomplete_Type (E1) and then Is_Incomplete_Type (E2) then
5785 null;
5786
5787 elsif B then
5788 Error_Msg_NE
5789 ("actual for & in actual instance does not match formal",
5790 Parent (Actual_Pack), E1);
5791 end if;
5792 end Check_Mismatch;
5793
5794 --------------------------------
5795 -- Same_Instantiated_Constant --
5796 --------------------------------
5797
5798 function Same_Instantiated_Constant
5799 (E1, E2 : Entity_Id) return Boolean
5800 is
5801 Ent : Entity_Id;
5802
5803 begin
5804 Ent := E2;
5805 while Present (Ent) loop
5806 if E1 = Ent then
5807 return True;
5808
5809 elsif Ekind (Ent) /= E_Constant then
5810 return False;
5811
5812 elsif Is_Entity_Name (Constant_Value (Ent)) then
5813 if Entity (Constant_Value (Ent)) = E1 then
5814 return True;
5815 else
5816 Ent := Entity (Constant_Value (Ent));
5817 end if;
5818
5819 -- The actual may be a constant that has been folded. Recover
5820 -- original name.
5821
5822 elsif Is_Entity_Name (Original_Node (Constant_Value (Ent))) then
5823 Ent := Entity (Original_Node (Constant_Value (Ent)));
5824
5825 else
5826 return False;
5827 end if;
5828 end loop;
5829
5830 return False;
5831 end Same_Instantiated_Constant;
5832
5833 --------------------------------
5834 -- Same_Instantiated_Function --
5835 --------------------------------
5836
5837 function Same_Instantiated_Function
5838 (E1, E2 : Entity_Id) return Boolean
5839 is
5840 U1, U2 : Node_Id;
5841 begin
5842 if Alias (E1) = Alias (E2) then
5843 return True;
5844
5845 elsif Present (Alias (E2)) then
5846 U1 := Original_Node (Unit_Declaration_Node (E1));
5847 U2 := Original_Node (Unit_Declaration_Node (Alias (E2)));
5848
5849 return Nkind (U1) = N_Subprogram_Renaming_Declaration
5850 and then Nkind (Name (U1)) = N_Attribute_Reference
5851
5852 and then Nkind (U2) = N_Subprogram_Renaming_Declaration
5853 and then Nkind (Name (U2)) = N_Attribute_Reference
5854
5855 and then
5856 Attribute_Name (Name (U1)) = Attribute_Name (Name (U2));
5857 else
5858 return False;
5859 end if;
5860 end Same_Instantiated_Function;
5861
5862 --------------------------------
5863 -- Same_Instantiated_Variable --
5864 --------------------------------
5865
5866 function Same_Instantiated_Variable
5867 (E1, E2 : Entity_Id) return Boolean
5868 is
5869 function Original_Entity (E : Entity_Id) return Entity_Id;
5870 -- Follow chain of renamings to the ultimate ancestor
5871
5872 ---------------------
5873 -- Original_Entity --
5874 ---------------------
5875
5876 function Original_Entity (E : Entity_Id) return Entity_Id is
5877 Orig : Entity_Id;
5878
5879 begin
5880 Orig := E;
5881 while Nkind (Parent (Orig)) = N_Object_Renaming_Declaration
5882 and then Present (Renamed_Object (Orig))
5883 and then Is_Entity_Name (Renamed_Object (Orig))
5884 loop
5885 Orig := Entity (Renamed_Object (Orig));
5886 end loop;
5887
5888 return Orig;
5889 end Original_Entity;
5890
5891 -- Start of processing for Same_Instantiated_Variable
5892
5893 begin
5894 return Ekind (E1) = Ekind (E2)
5895 and then Original_Entity (E1) = Original_Entity (E2);
5896 end Same_Instantiated_Variable;
5897
5898 -- Start of processing for Check_Formal_Package_Instance
5899
5900 begin
5901 while Present (E1) and then Present (E2) loop
5902 exit when Ekind (E1) = E_Package
5903 and then Renamed_Entity (E1) = Renamed_Entity (Actual_Pack);
5904
5905 -- If the formal is the renaming of the formal package, this
5906 -- is the end of its formal part, which may occur before the
5907 -- end of the formal part in the actual in the presence of
5908 -- defaulted parameters in the formal package.
5909
5910 exit when Nkind (Parent (E2)) = N_Package_Renaming_Declaration
5911 and then Renamed_Entity (E2) = Scope (E2);
5912
5913 -- The analysis of the actual may generate additional internal
5914 -- entities. If the formal is defaulted, there is no corresponding
5915 -- analysis and the internal entities must be skipped, until we
5916 -- find corresponding entities again.
5917
5918 if Comes_From_Source (E2)
5919 and then not Comes_From_Source (E1)
5920 and then Chars (E1) /= Chars (E2)
5921 then
5922 while Present (E1) and then Chars (E1) /= Chars (E2) loop
5923 Next_Entity (E1);
5924 end loop;
5925 end if;
5926
5927 if No (E1) then
5928 return;
5929
5930 -- If the formal entity comes from a formal declaration, it was
5931 -- defaulted in the formal package, and no check is needed on it.
5932
5933 elsif Nkind_In (Original_Node (Parent (E2)),
5934 N_Formal_Object_Declaration,
5935 N_Formal_Type_Declaration)
5936 then
5937 goto Next_E;
5938
5939 -- Ditto for defaulted formal subprograms.
5940
5941 elsif Is_Overloadable (E1)
5942 and then Nkind (Unit_Declaration_Node (E2)) in
5943 N_Formal_Subprogram_Declaration
5944 then
5945 goto Next_E;
5946
5947 elsif Is_Type (E1) then
5948
5949 -- Subtypes must statically match. E1, E2 are the local entities
5950 -- that are subtypes of the actuals. Itypes generated for other
5951 -- parameters need not be checked, the check will be performed
5952 -- on the parameters themselves.
5953
5954 -- If E2 is a formal type declaration, it is a defaulted parameter
5955 -- and needs no checking.
5956
5957 if not Is_Itype (E1) and then not Is_Itype (E2) then
5958 Check_Mismatch
5959 (not Is_Type (E2)
5960 or else Etype (E1) /= Etype (E2)
5961 or else not Subtypes_Statically_Match (E1, E2));
5962 end if;
5963
5964 elsif Ekind (E1) = E_Constant then
5965
5966 -- IN parameters must denote the same static value, or the same
5967 -- constant, or the literal null.
5968
5969 Expr1 := Expression (Parent (E1));
5970
5971 if Ekind (E2) /= E_Constant then
5972 Check_Mismatch (True);
5973 goto Next_E;
5974 else
5975 Expr2 := Expression (Parent (E2));
5976 end if;
5977
5978 if Is_OK_Static_Expression (Expr1) then
5979 if not Is_OK_Static_Expression (Expr2) then
5980 Check_Mismatch (True);
5981
5982 elsif Is_Discrete_Type (Etype (E1)) then
5983 declare
5984 V1 : constant Uint := Expr_Value (Expr1);
5985 V2 : constant Uint := Expr_Value (Expr2);
5986 begin
5987 Check_Mismatch (V1 /= V2);
5988 end;
5989
5990 elsif Is_Real_Type (Etype (E1)) then
5991 declare
5992 V1 : constant Ureal := Expr_Value_R (Expr1);
5993 V2 : constant Ureal := Expr_Value_R (Expr2);
5994 begin
5995 Check_Mismatch (V1 /= V2);
5996 end;
5997
5998 elsif Is_String_Type (Etype (E1))
5999 and then Nkind (Expr1) = N_String_Literal
6000 then
6001 if Nkind (Expr2) /= N_String_Literal then
6002 Check_Mismatch (True);
6003 else
6004 Check_Mismatch
6005 (not String_Equal (Strval (Expr1), Strval (Expr2)));
6006 end if;
6007 end if;
6008
6009 elsif Is_Entity_Name (Expr1) then
6010 if Is_Entity_Name (Expr2) then
6011 if Entity (Expr1) = Entity (Expr2) then
6012 null;
6013 else
6014 Check_Mismatch
6015 (not Same_Instantiated_Constant
6016 (Entity (Expr1), Entity (Expr2)));
6017 end if;
6018
6019 else
6020 Check_Mismatch (True);
6021 end if;
6022
6023 elsif Is_Entity_Name (Original_Node (Expr1))
6024 and then Is_Entity_Name (Expr2)
6025 and then Same_Instantiated_Constant
6026 (Entity (Original_Node (Expr1)), Entity (Expr2))
6027 then
6028 null;
6029
6030 elsif Nkind (Expr1) = N_Null then
6031 Check_Mismatch (Nkind (Expr1) /= N_Null);
6032
6033 else
6034 Check_Mismatch (True);
6035 end if;
6036
6037 elsif Ekind (E1) = E_Variable then
6038 Check_Mismatch (not Same_Instantiated_Variable (E1, E2));
6039
6040 elsif Ekind (E1) = E_Package then
6041 Check_Mismatch
6042 (Ekind (E1) /= Ekind (E2)
6043 or else Renamed_Object (E1) /= Renamed_Object (E2));
6044
6045 elsif Is_Overloadable (E1) then
6046
6047 -- Verify that the actual subprograms match. Note that actuals
6048 -- that are attributes are rewritten as subprograms. If the
6049 -- subprogram in the formal package is defaulted, no check is
6050 -- needed. Note that this can only happen in Ada 2005 when the
6051 -- formal package can be partially parameterized.
6052
6053 if Nkind (Unit_Declaration_Node (E1)) =
6054 N_Subprogram_Renaming_Declaration
6055 and then From_Default (Unit_Declaration_Node (E1))
6056 then
6057 null;
6058
6059 -- If the formal package has an "others" box association that
6060 -- covers this formal, there is no need for a check either.
6061
6062 elsif Nkind (Unit_Declaration_Node (E2)) in
6063 N_Formal_Subprogram_Declaration
6064 and then Box_Present (Unit_Declaration_Node (E2))
6065 then
6066 null;
6067
6068 -- No check needed if subprogram is a defaulted null procedure
6069
6070 elsif No (Alias (E2))
6071 and then Ekind (E2) = E_Procedure
6072 and then
6073 Null_Present (Specification (Unit_Declaration_Node (E2)))
6074 then
6075 null;
6076
6077 -- Otherwise the actual in the formal and the actual in the
6078 -- instantiation of the formal must match, up to renamings.
6079
6080 else
6081 Check_Mismatch
6082 (Ekind (E2) /= Ekind (E1)
6083 or else not Same_Instantiated_Function (E1, E2));
6084 end if;
6085
6086 else
6087 raise Program_Error;
6088 end if;
6089
6090 <<Next_E>>
6091 Next_Entity (E1);
6092 Next_Entity (E2);
6093 end loop;
6094 end Check_Formal_Package_Instance;
6095
6096 ---------------------------
6097 -- Check_Formal_Packages --
6098 ---------------------------
6099
6100 procedure Check_Formal_Packages (P_Id : Entity_Id) is
6101 E : Entity_Id;
6102 Formal_P : Entity_Id;
6103 Formal_Decl : Node_Id;
6104
6105 begin
6106 -- Iterate through the declarations in the instance, looking for package
6107 -- renaming declarations that denote instances of formal packages. Stop
6108 -- when we find the renaming of the current package itself. The
6109 -- declaration for a formal package without a box is followed by an
6110 -- internal entity that repeats the instantiation.
6111
6112 E := First_Entity (P_Id);
6113 while Present (E) loop
6114 if Ekind (E) = E_Package then
6115 if Renamed_Object (E) = P_Id then
6116 exit;
6117
6118 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
6119 null;
6120
6121 else
6122 Formal_Decl := Parent (Associated_Formal_Package (E));
6123
6124 -- Nothing to check if the formal has a box or an others_clause
6125 -- (necessarily with a box).
6126
6127 if Box_Present (Formal_Decl) then
6128 null;
6129
6130 elsif Nkind (First (Generic_Associations (Formal_Decl))) =
6131 N_Others_Choice
6132 then
6133 -- The internal validating package was generated but formal
6134 -- and instance are known to be compatible.
6135
6136 Formal_P := Next_Entity (E);
6137 Remove (Unit_Declaration_Node (Formal_P));
6138
6139 else
6140 Formal_P := Next_Entity (E);
6141 Check_Formal_Package_Instance (Formal_P, E);
6142
6143 -- After checking, remove the internal validating package.
6144 -- It is only needed for semantic checks, and as it may
6145 -- contain generic formal declarations it should not reach
6146 -- gigi.
6147
6148 Remove (Unit_Declaration_Node (Formal_P));
6149 end if;
6150 end if;
6151 end if;
6152
6153 Next_Entity (E);
6154 end loop;
6155 end Check_Formal_Packages;
6156
6157 ---------------------------------
6158 -- Check_Forward_Instantiation --
6159 ---------------------------------
6160
6161 procedure Check_Forward_Instantiation (Decl : Node_Id) is
6162 S : Entity_Id;
6163 Gen_Comp : Entity_Id := Cunit_Entity (Get_Source_Unit (Decl));
6164
6165 begin
6166 -- The instantiation appears before the generic body if we are in the
6167 -- scope of the unit containing the generic, either in its spec or in
6168 -- the package body, and before the generic body.
6169
6170 if Ekind (Gen_Comp) = E_Package_Body then
6171 Gen_Comp := Spec_Entity (Gen_Comp);
6172 end if;
6173
6174 if In_Open_Scopes (Gen_Comp)
6175 and then No (Corresponding_Body (Decl))
6176 then
6177 S := Current_Scope;
6178
6179 while Present (S)
6180 and then not Is_Compilation_Unit (S)
6181 and then not Is_Child_Unit (S)
6182 loop
6183 if Ekind (S) = E_Package then
6184 Set_Has_Forward_Instantiation (S);
6185 end if;
6186
6187 S := Scope (S);
6188 end loop;
6189 end if;
6190 end Check_Forward_Instantiation;
6191
6192 ---------------------------
6193 -- Check_Generic_Actuals --
6194 ---------------------------
6195
6196 -- The visibility of the actuals may be different between the point of
6197 -- generic instantiation and the instantiation of the body.
6198
6199 procedure Check_Generic_Actuals
6200 (Instance : Entity_Id;
6201 Is_Formal_Box : Boolean)
6202 is
6203 E : Entity_Id;
6204 Astype : Entity_Id;
6205
6206 function Denotes_Previous_Actual (Typ : Entity_Id) return Boolean;
6207 -- For a formal that is an array type, the component type is often a
6208 -- previous formal in the same unit. The privacy status of the component
6209 -- type will have been examined earlier in the traversal of the
6210 -- corresponding actuals, and this status should not be modified for
6211 -- the array (sub)type itself. However, if the base type of the array
6212 -- (sub)type is private, its full view must be restored in the body to
6213 -- be consistent with subsequent index subtypes, etc.
6214 --
6215 -- To detect this case we have to rescan the list of formals, which is
6216 -- usually short enough to ignore the resulting inefficiency.
6217
6218 -----------------------------
6219 -- Denotes_Previous_Actual --
6220 -----------------------------
6221
6222 function Denotes_Previous_Actual (Typ : Entity_Id) return Boolean is
6223 Prev : Entity_Id;
6224
6225 begin
6226 Prev := First_Entity (Instance);
6227 while Present (Prev) loop
6228 if Is_Type (Prev)
6229 and then Nkind (Parent (Prev)) = N_Subtype_Declaration
6230 and then Is_Entity_Name (Subtype_Indication (Parent (Prev)))
6231 and then Entity (Subtype_Indication (Parent (Prev))) = Typ
6232 then
6233 return True;
6234
6235 elsif Prev = E then
6236 return False;
6237
6238 else
6239 Next_Entity (Prev);
6240 end if;
6241 end loop;
6242
6243 return False;
6244 end Denotes_Previous_Actual;
6245
6246 -- Start of processing for Check_Generic_Actuals
6247
6248 begin
6249 E := First_Entity (Instance);
6250 while Present (E) loop
6251 if Is_Type (E)
6252 and then Nkind (Parent (E)) = N_Subtype_Declaration
6253 and then Scope (Etype (E)) /= Instance
6254 and then Is_Entity_Name (Subtype_Indication (Parent (E)))
6255 then
6256 if Is_Array_Type (E)
6257 and then not Is_Private_Type (Etype (E))
6258 and then Denotes_Previous_Actual (Component_Type (E))
6259 then
6260 null;
6261 else
6262 Check_Private_View (Subtype_Indication (Parent (E)));
6263 end if;
6264
6265 Set_Is_Generic_Actual_Type (E, True);
6266 Set_Is_Hidden (E, False);
6267 Set_Is_Potentially_Use_Visible (E,
6268 In_Use (Instance));
6269
6270 -- We constructed the generic actual type as a subtype of the
6271 -- supplied type. This means that it normally would not inherit
6272 -- subtype specific attributes of the actual, which is wrong for
6273 -- the generic case.
6274
6275 Astype := Ancestor_Subtype (E);
6276
6277 if No (Astype) then
6278
6279 -- This can happen when E is an itype that is the full view of
6280 -- a private type completed, e.g. with a constrained array. In
6281 -- that case, use the first subtype, which will carry size
6282 -- information. The base type itself is unconstrained and will
6283 -- not carry it.
6284
6285 Astype := First_Subtype (E);
6286 end if;
6287
6288 Set_Size_Info (E, (Astype));
6289 Set_RM_Size (E, RM_Size (Astype));
6290 Set_First_Rep_Item (E, First_Rep_Item (Astype));
6291
6292 if Is_Discrete_Or_Fixed_Point_Type (E) then
6293 Set_RM_Size (E, RM_Size (Astype));
6294
6295 -- In nested instances, the base type of an access actual may
6296 -- itself be private, and need to be exchanged.
6297
6298 elsif Is_Access_Type (E)
6299 and then Is_Private_Type (Etype (E))
6300 then
6301 Check_Private_View
6302 (New_Occurrence_Of (Etype (E), Sloc (Instance)));
6303 end if;
6304
6305 elsif Ekind (E) = E_Package then
6306
6307 -- If this is the renaming for the current instance, we're done.
6308 -- Otherwise it is a formal package. If the corresponding formal
6309 -- was declared with a box, the (instantiations of the) generic
6310 -- formal part are also visible. Otherwise, ignore the entity
6311 -- created to validate the actuals.
6312
6313 if Renamed_Object (E) = Instance then
6314 exit;
6315
6316 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
6317 null;
6318
6319 -- The visibility of a formal of an enclosing generic is already
6320 -- correct.
6321
6322 elsif Denotes_Formal_Package (E) then
6323 null;
6324
6325 elsif Present (Associated_Formal_Package (E))
6326 and then not Is_Generic_Formal (E)
6327 then
6328 if Box_Present (Parent (Associated_Formal_Package (E))) then
6329 Check_Generic_Actuals (Renamed_Object (E), True);
6330
6331 else
6332 Check_Generic_Actuals (Renamed_Object (E), False);
6333 end if;
6334
6335 Set_Is_Hidden (E, False);
6336 end if;
6337
6338 -- If this is a subprogram instance (in a wrapper package) the
6339 -- actual is fully visible.
6340
6341 elsif Is_Wrapper_Package (Instance) then
6342 Set_Is_Hidden (E, False);
6343
6344 -- If the formal package is declared with a box, or if the formal
6345 -- parameter is defaulted, it is visible in the body.
6346
6347 elsif Is_Formal_Box or else Is_Visible_Formal (E) then
6348 Set_Is_Hidden (E, False);
6349 end if;
6350
6351 if Ekind (E) = E_Constant then
6352
6353 -- If the type of the actual is a private type declared in the
6354 -- enclosing scope of the generic unit, the body of the generic
6355 -- sees the full view of the type (because it has to appear in
6356 -- the corresponding package body). If the type is private now,
6357 -- exchange views to restore the proper visiblity in the instance.
6358
6359 declare
6360 Typ : constant Entity_Id := Base_Type (Etype (E));
6361 -- The type of the actual
6362
6363 Gen_Id : Entity_Id;
6364 -- The generic unit
6365
6366 Parent_Scope : Entity_Id;
6367 -- The enclosing scope of the generic unit
6368
6369 begin
6370 if Is_Wrapper_Package (Instance) then
6371 Gen_Id :=
6372 Generic_Parent
6373 (Specification
6374 (Unit_Declaration_Node
6375 (Related_Instance (Instance))));
6376 else
6377 Gen_Id :=
6378 Generic_Parent (Package_Specification (Instance));
6379 end if;
6380
6381 Parent_Scope := Scope (Gen_Id);
6382
6383 -- The exchange is only needed if the generic is defined
6384 -- within a package which is not a common ancestor of the
6385 -- scope of the instance, and is not already in scope.
6386
6387 if Is_Private_Type (Typ)
6388 and then Scope (Typ) = Parent_Scope
6389 and then Scope (Instance) /= Parent_Scope
6390 and then Ekind (Parent_Scope) = E_Package
6391 and then not Is_Child_Unit (Gen_Id)
6392 then
6393 Switch_View (Typ);
6394
6395 -- If the type of the entity is a subtype, it may also have
6396 -- to be made visible, together with the base type of its
6397 -- full view, after exchange.
6398
6399 if Is_Private_Type (Etype (E)) then
6400 Switch_View (Etype (E));
6401 Switch_View (Base_Type (Etype (E)));
6402 end if;
6403 end if;
6404 end;
6405 end if;
6406
6407 Next_Entity (E);
6408 end loop;
6409 end Check_Generic_Actuals;
6410
6411 ------------------------------
6412 -- Check_Generic_Child_Unit --
6413 ------------------------------
6414
6415 procedure Check_Generic_Child_Unit
6416 (Gen_Id : Node_Id;
6417 Parent_Installed : in out Boolean)
6418 is
6419 Loc : constant Source_Ptr := Sloc (Gen_Id);
6420 Gen_Par : Entity_Id := Empty;
6421 E : Entity_Id;
6422 Inst_Par : Entity_Id;
6423 S : Node_Id;
6424
6425 function Find_Generic_Child
6426 (Scop : Entity_Id;
6427 Id : Node_Id) return Entity_Id;
6428 -- Search generic parent for possible child unit with the given name
6429
6430 function In_Enclosing_Instance return Boolean;
6431 -- Within an instance of the parent, the child unit may be denoted by
6432 -- a simple name, or an abbreviated expanded name. Examine enclosing
6433 -- scopes to locate a possible parent instantiation.
6434
6435 ------------------------
6436 -- Find_Generic_Child --
6437 ------------------------
6438
6439 function Find_Generic_Child
6440 (Scop : Entity_Id;
6441 Id : Node_Id) return Entity_Id
6442 is
6443 E : Entity_Id;
6444
6445 begin
6446 -- If entity of name is already set, instance has already been
6447 -- resolved, e.g. in an enclosing instantiation.
6448
6449 if Present (Entity (Id)) then
6450 if Scope (Entity (Id)) = Scop then
6451 return Entity (Id);
6452 else
6453 return Empty;
6454 end if;
6455
6456 else
6457 E := First_Entity (Scop);
6458 while Present (E) loop
6459 if Chars (E) = Chars (Id)
6460 and then Is_Child_Unit (E)
6461 then
6462 if Is_Child_Unit (E)
6463 and then not Is_Visible_Lib_Unit (E)
6464 then
6465 Error_Msg_NE
6466 ("generic child unit& is not visible", Gen_Id, E);
6467 end if;
6468
6469 Set_Entity (Id, E);
6470 return E;
6471 end if;
6472
6473 Next_Entity (E);
6474 end loop;
6475
6476 return Empty;
6477 end if;
6478 end Find_Generic_Child;
6479
6480 ---------------------------
6481 -- In_Enclosing_Instance --
6482 ---------------------------
6483
6484 function In_Enclosing_Instance return Boolean is
6485 Enclosing_Instance : Node_Id;
6486 Instance_Decl : Node_Id;
6487
6488 begin
6489 -- We do not inline any call that contains instantiations, except
6490 -- for instantiations of Unchecked_Conversion, so if we are within
6491 -- an inlined body the current instance does not require parents.
6492
6493 if In_Inlined_Body then
6494 pragma Assert (Chars (Gen_Id) = Name_Unchecked_Conversion);
6495 return False;
6496 end if;
6497
6498 -- Loop to check enclosing scopes
6499
6500 Enclosing_Instance := Current_Scope;
6501 while Present (Enclosing_Instance) loop
6502 Instance_Decl := Unit_Declaration_Node (Enclosing_Instance);
6503
6504 if Ekind (Enclosing_Instance) = E_Package
6505 and then Is_Generic_Instance (Enclosing_Instance)
6506 and then Present
6507 (Generic_Parent (Specification (Instance_Decl)))
6508 then
6509 -- Check whether the generic we are looking for is a child of
6510 -- this instance.
6511
6512 E := Find_Generic_Child
6513 (Generic_Parent (Specification (Instance_Decl)), Gen_Id);
6514 exit when Present (E);
6515
6516 else
6517 E := Empty;
6518 end if;
6519
6520 Enclosing_Instance := Scope (Enclosing_Instance);
6521 end loop;
6522
6523 if No (E) then
6524
6525 -- Not a child unit
6526
6527 Analyze (Gen_Id);
6528 return False;
6529
6530 else
6531 Rewrite (Gen_Id,
6532 Make_Expanded_Name (Loc,
6533 Chars => Chars (E),
6534 Prefix => New_Occurrence_Of (Enclosing_Instance, Loc),
6535 Selector_Name => New_Occurrence_Of (E, Loc)));
6536
6537 Set_Entity (Gen_Id, E);
6538 Set_Etype (Gen_Id, Etype (E));
6539 Parent_Installed := False; -- Already in scope.
6540 return True;
6541 end if;
6542 end In_Enclosing_Instance;
6543
6544 -- Start of processing for Check_Generic_Child_Unit
6545
6546 begin
6547 -- If the name of the generic is given by a selected component, it may
6548 -- be the name of a generic child unit, and the prefix is the name of an
6549 -- instance of the parent, in which case the child unit must be visible.
6550 -- If this instance is not in scope, it must be placed there and removed
6551 -- after instantiation, because what is being instantiated is not the
6552 -- original child, but the corresponding child present in the instance
6553 -- of the parent.
6554
6555 -- If the child is instantiated within the parent, it can be given by
6556 -- a simple name. In this case the instance is already in scope, but
6557 -- the child generic must be recovered from the generic parent as well.
6558
6559 if Nkind (Gen_Id) = N_Selected_Component then
6560 S := Selector_Name (Gen_Id);
6561 Analyze (Prefix (Gen_Id));
6562 Inst_Par := Entity (Prefix (Gen_Id));
6563
6564 if Ekind (Inst_Par) = E_Package
6565 and then Present (Renamed_Object (Inst_Par))
6566 then
6567 Inst_Par := Renamed_Object (Inst_Par);
6568 end if;
6569
6570 if Ekind (Inst_Par) = E_Package then
6571 if Nkind (Parent (Inst_Par)) = N_Package_Specification then
6572 Gen_Par := Generic_Parent (Parent (Inst_Par));
6573
6574 elsif Nkind (Parent (Inst_Par)) = N_Defining_Program_Unit_Name
6575 and then
6576 Nkind (Parent (Parent (Inst_Par))) = N_Package_Specification
6577 then
6578 Gen_Par := Generic_Parent (Parent (Parent (Inst_Par)));
6579 end if;
6580
6581 elsif Ekind (Inst_Par) = E_Generic_Package
6582 and then Nkind (Parent (Gen_Id)) = N_Formal_Package_Declaration
6583 then
6584 -- A formal package may be a real child package, and not the
6585 -- implicit instance within a parent. In this case the child is
6586 -- not visible and has to be retrieved explicitly as well.
6587
6588 Gen_Par := Inst_Par;
6589 end if;
6590
6591 if Present (Gen_Par) then
6592
6593 -- The prefix denotes an instantiation. The entity itself may be a
6594 -- nested generic, or a child unit.
6595
6596 E := Find_Generic_Child (Gen_Par, S);
6597
6598 if Present (E) then
6599 Change_Selected_Component_To_Expanded_Name (Gen_Id);
6600 Set_Entity (Gen_Id, E);
6601 Set_Etype (Gen_Id, Etype (E));
6602 Set_Entity (S, E);
6603 Set_Etype (S, Etype (E));
6604
6605 -- Indicate that this is a reference to the parent
6606
6607 if In_Extended_Main_Source_Unit (Gen_Id) then
6608 Set_Is_Instantiated (Inst_Par);
6609 end if;
6610
6611 -- A common mistake is to replicate the naming scheme of a
6612 -- hierarchy by instantiating a generic child directly, rather
6613 -- than the implicit child in a parent instance:
6614
6615 -- generic .. package Gpar is ..
6616 -- generic .. package Gpar.Child is ..
6617 -- package Par is new Gpar ();
6618
6619 -- with Gpar.Child;
6620 -- package Par.Child is new Gpar.Child ();
6621 -- rather than Par.Child
6622
6623 -- In this case the instantiation is within Par, which is an
6624 -- instance, but Gpar does not denote Par because we are not IN
6625 -- the instance of Gpar, so this is illegal. The test below
6626 -- recognizes this particular case.
6627
6628 if Is_Child_Unit (E)
6629 and then not Comes_From_Source (Entity (Prefix (Gen_Id)))
6630 and then (not In_Instance
6631 or else Nkind (Parent (Parent (Gen_Id))) =
6632 N_Compilation_Unit)
6633 then
6634 Error_Msg_N
6635 ("prefix of generic child unit must be instance of parent",
6636 Gen_Id);
6637 end if;
6638
6639 if not In_Open_Scopes (Inst_Par)
6640 and then Nkind (Parent (Gen_Id)) not in
6641 N_Generic_Renaming_Declaration
6642 then
6643 Install_Parent (Inst_Par);
6644 Parent_Installed := True;
6645
6646 elsif In_Open_Scopes (Inst_Par) then
6647
6648 -- If the parent is already installed, install the actuals
6649 -- for its formal packages. This is necessary when the child
6650 -- instance is a child of the parent instance: in this case,
6651 -- the parent is placed on the scope stack but the formal
6652 -- packages are not made visible.
6653
6654 Install_Formal_Packages (Inst_Par);
6655 end if;
6656
6657 else
6658 -- If the generic parent does not contain an entity that
6659 -- corresponds to the selector, the instance doesn't either.
6660 -- Analyzing the node will yield the appropriate error message.
6661 -- If the entity is not a child unit, then it is an inner
6662 -- generic in the parent.
6663
6664 Analyze (Gen_Id);
6665 end if;
6666
6667 else
6668 Analyze (Gen_Id);
6669
6670 if Is_Child_Unit (Entity (Gen_Id))
6671 and then
6672 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
6673 and then not In_Open_Scopes (Inst_Par)
6674 then
6675 Install_Parent (Inst_Par);
6676 Parent_Installed := True;
6677
6678 -- The generic unit may be the renaming of the implicit child
6679 -- present in an instance. In that case the parent instance is
6680 -- obtained from the name of the renamed entity.
6681
6682 elsif Ekind (Entity (Gen_Id)) = E_Generic_Package
6683 and then Present (Renamed_Entity (Entity (Gen_Id)))
6684 and then Is_Child_Unit (Renamed_Entity (Entity (Gen_Id)))
6685 then
6686 declare
6687 Renamed_Package : constant Node_Id :=
6688 Name (Parent (Entity (Gen_Id)));
6689 begin
6690 if Nkind (Renamed_Package) = N_Expanded_Name then
6691 Inst_Par := Entity (Prefix (Renamed_Package));
6692 Install_Parent (Inst_Par);
6693 Parent_Installed := True;
6694 end if;
6695 end;
6696 end if;
6697 end if;
6698
6699 elsif Nkind (Gen_Id) = N_Expanded_Name then
6700
6701 -- Entity already present, analyze prefix, whose meaning may be
6702 -- an instance in the current context. If it is an instance of
6703 -- a relative within another, the proper parent may still have
6704 -- to be installed, if they are not of the same generation.
6705
6706 Analyze (Prefix (Gen_Id));
6707
6708 -- In the unlikely case that a local declaration hides the name
6709 -- of the parent package, locate it on the homonym chain. If the
6710 -- context is an instance of the parent, the renaming entity is
6711 -- flagged as such.
6712
6713 Inst_Par := Entity (Prefix (Gen_Id));
6714 while Present (Inst_Par)
6715 and then not Is_Package_Or_Generic_Package (Inst_Par)
6716 loop
6717 Inst_Par := Homonym (Inst_Par);
6718 end loop;
6719
6720 pragma Assert (Present (Inst_Par));
6721 Set_Entity (Prefix (Gen_Id), Inst_Par);
6722
6723 if In_Enclosing_Instance then
6724 null;
6725
6726 elsif Present (Entity (Gen_Id))
6727 and then Is_Child_Unit (Entity (Gen_Id))
6728 and then not In_Open_Scopes (Inst_Par)
6729 then
6730 Install_Parent (Inst_Par);
6731 Parent_Installed := True;
6732 end if;
6733
6734 elsif In_Enclosing_Instance then
6735
6736 -- The child unit is found in some enclosing scope
6737
6738 null;
6739
6740 else
6741 Analyze (Gen_Id);
6742
6743 -- If this is the renaming of the implicit child in a parent
6744 -- instance, recover the parent name and install it.
6745
6746 if Is_Entity_Name (Gen_Id) then
6747 E := Entity (Gen_Id);
6748
6749 if Is_Generic_Unit (E)
6750 and then Nkind (Parent (E)) in N_Generic_Renaming_Declaration
6751 and then Is_Child_Unit (Renamed_Object (E))
6752 and then Is_Generic_Unit (Scope (Renamed_Object (E)))
6753 and then Nkind (Name (Parent (E))) = N_Expanded_Name
6754 then
6755 Rewrite (Gen_Id, New_Copy_Tree (Name (Parent (E))));
6756 Inst_Par := Entity (Prefix (Gen_Id));
6757
6758 if not In_Open_Scopes (Inst_Par) then
6759 Install_Parent (Inst_Par);
6760 Parent_Installed := True;
6761 end if;
6762
6763 -- If it is a child unit of a non-generic parent, it may be
6764 -- use-visible and given by a direct name. Install parent as
6765 -- for other cases.
6766
6767 elsif Is_Generic_Unit (E)
6768 and then Is_Child_Unit (E)
6769 and then
6770 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
6771 and then not Is_Generic_Unit (Scope (E))
6772 then
6773 if not In_Open_Scopes (Scope (E)) then
6774 Install_Parent (Scope (E));
6775 Parent_Installed := True;
6776 end if;
6777 end if;
6778 end if;
6779 end if;
6780 end Check_Generic_Child_Unit;
6781
6782 -----------------------------
6783 -- Check_Hidden_Child_Unit --
6784 -----------------------------
6785
6786 procedure Check_Hidden_Child_Unit
6787 (N : Node_Id;
6788 Gen_Unit : Entity_Id;
6789 Act_Decl_Id : Entity_Id)
6790 is
6791 Gen_Id : constant Node_Id := Name (N);
6792
6793 begin
6794 if Is_Child_Unit (Gen_Unit)
6795 and then Is_Child_Unit (Act_Decl_Id)
6796 and then Nkind (Gen_Id) = N_Expanded_Name
6797 and then Entity (Prefix (Gen_Id)) = Scope (Act_Decl_Id)
6798 and then Chars (Gen_Unit) = Chars (Act_Decl_Id)
6799 then
6800 Error_Msg_Node_2 := Scope (Act_Decl_Id);
6801 Error_Msg_NE
6802 ("generic unit & is implicitly declared in &",
6803 Defining_Unit_Name (N), Gen_Unit);
6804 Error_Msg_N ("\instance must have different name",
6805 Defining_Unit_Name (N));
6806 end if;
6807 end Check_Hidden_Child_Unit;
6808
6809 ------------------------
6810 -- Check_Private_View --
6811 ------------------------
6812
6813 procedure Check_Private_View (N : Node_Id) is
6814 T : constant Entity_Id := Etype (N);
6815 BT : Entity_Id;
6816
6817 begin
6818 -- Exchange views if the type was not private in the generic but is
6819 -- private at the point of instantiation. Do not exchange views if
6820 -- the scope of the type is in scope. This can happen if both generic
6821 -- and instance are sibling units, or if type is defined in a parent.
6822 -- In this case the visibility of the type will be correct for all
6823 -- semantic checks.
6824
6825 if Present (T) then
6826 BT := Base_Type (T);
6827
6828 if Is_Private_Type (T)
6829 and then not Has_Private_View (N)
6830 and then Present (Full_View (T))
6831 and then not In_Open_Scopes (Scope (T))
6832 then
6833 -- In the generic, the full type was visible. Save the private
6834 -- entity, for subsequent exchange.
6835
6836 Switch_View (T);
6837
6838 elsif Has_Private_View (N)
6839 and then not Is_Private_Type (T)
6840 and then not Has_Been_Exchanged (T)
6841 and then Etype (Get_Associated_Node (N)) /= T
6842 then
6843 -- Only the private declaration was visible in the generic. If
6844 -- the type appears in a subtype declaration, the subtype in the
6845 -- instance must have a view compatible with that of its parent,
6846 -- which must be exchanged (see corresponding code in Restore_
6847 -- Private_Views). Otherwise, if the type is defined in a parent
6848 -- unit, leave full visibility within instance, which is safe.
6849
6850 if In_Open_Scopes (Scope (Base_Type (T)))
6851 and then not Is_Private_Type (Base_Type (T))
6852 and then Comes_From_Source (Base_Type (T))
6853 then
6854 null;
6855
6856 elsif Nkind (Parent (N)) = N_Subtype_Declaration
6857 or else not In_Private_Part (Scope (Base_Type (T)))
6858 then
6859 Prepend_Elmt (T, Exchanged_Views);
6860 Exchange_Declarations (Etype (Get_Associated_Node (N)));
6861 end if;
6862
6863 -- For composite types with inconsistent representation exchange
6864 -- component types accordingly.
6865
6866 elsif Is_Access_Type (T)
6867 and then Is_Private_Type (Designated_Type (T))
6868 and then not Has_Private_View (N)
6869 and then Present (Full_View (Designated_Type (T)))
6870 then
6871 Switch_View (Designated_Type (T));
6872
6873 elsif Is_Array_Type (T) then
6874 if Is_Private_Type (Component_Type (T))
6875 and then not Has_Private_View (N)
6876 and then Present (Full_View (Component_Type (T)))
6877 then
6878 Switch_View (Component_Type (T));
6879 end if;
6880
6881 -- The normal exchange mechanism relies on the setting of a
6882 -- flag on the reference in the generic. However, an additional
6883 -- mechanism is needed for types that are not explicitly
6884 -- mentioned in the generic, but may be needed in expanded code
6885 -- in the instance. This includes component types of arrays and
6886 -- designated types of access types. This processing must also
6887 -- include the index types of arrays which we take care of here.
6888
6889 declare
6890 Indx : Node_Id;
6891 Typ : Entity_Id;
6892
6893 begin
6894 Indx := First_Index (T);
6895 while Present (Indx) loop
6896 Typ := Base_Type (Etype (Indx));
6897
6898 if Is_Private_Type (Typ)
6899 and then Present (Full_View (Typ))
6900 then
6901 Switch_View (Typ);
6902 end if;
6903
6904 Next_Index (Indx);
6905 end loop;
6906 end;
6907
6908 elsif Is_Private_Type (T)
6909 and then Present (Full_View (T))
6910 and then Is_Array_Type (Full_View (T))
6911 and then Is_Private_Type (Component_Type (Full_View (T)))
6912 then
6913 Switch_View (T);
6914
6915 -- Finally, a non-private subtype may have a private base type, which
6916 -- must be exchanged for consistency. This can happen when a package
6917 -- body is instantiated, when the scope stack is empty but in fact
6918 -- the subtype and the base type are declared in an enclosing scope.
6919
6920 -- Note that in this case we introduce an inconsistency in the view
6921 -- set, because we switch the base type BT, but there could be some
6922 -- private dependent subtypes of BT which remain unswitched. Such
6923 -- subtypes might need to be switched at a later point (see specific
6924 -- provision for that case in Switch_View).
6925
6926 elsif not Is_Private_Type (T)
6927 and then not Has_Private_View (N)
6928 and then Is_Private_Type (BT)
6929 and then Present (Full_View (BT))
6930 and then not Is_Generic_Type (BT)
6931 and then not In_Open_Scopes (BT)
6932 then
6933 Prepend_Elmt (Full_View (BT), Exchanged_Views);
6934 Exchange_Declarations (BT);
6935 end if;
6936 end if;
6937 end Check_Private_View;
6938
6939 -----------------------------
6940 -- Check_Hidden_Primitives --
6941 -----------------------------
6942
6943 function Check_Hidden_Primitives (Assoc_List : List_Id) return Elist_Id is
6944 Actual : Node_Id;
6945 Gen_T : Entity_Id;
6946 Result : Elist_Id := No_Elist;
6947
6948 begin
6949 if No (Assoc_List) then
6950 return No_Elist;
6951 end if;
6952
6953 -- Traverse the list of associations between formals and actuals
6954 -- searching for renamings of tagged types
6955
6956 Actual := First (Assoc_List);
6957 while Present (Actual) loop
6958 if Nkind (Actual) = N_Subtype_Declaration then
6959 Gen_T := Generic_Parent_Type (Actual);
6960
6961 if Present (Gen_T) and then Is_Tagged_Type (Gen_T) then
6962
6963 -- Traverse the list of primitives of the actual types
6964 -- searching for hidden primitives that are visible in the
6965 -- corresponding generic formal; leave them visible and
6966 -- append them to Result to restore their decoration later.
6967
6968 Install_Hidden_Primitives
6969 (Prims_List => Result,
6970 Gen_T => Gen_T,
6971 Act_T => Entity (Subtype_Indication (Actual)));
6972 end if;
6973 end if;
6974
6975 Next (Actual);
6976 end loop;
6977
6978 return Result;
6979 end Check_Hidden_Primitives;
6980
6981 --------------------------
6982 -- Contains_Instance_Of --
6983 --------------------------
6984
6985 function Contains_Instance_Of
6986 (Inner : Entity_Id;
6987 Outer : Entity_Id;
6988 N : Node_Id) return Boolean
6989 is
6990 Elmt : Elmt_Id;
6991 Scop : Entity_Id;
6992
6993 begin
6994 Scop := Outer;
6995
6996 -- Verify that there are no circular instantiations. We check whether
6997 -- the unit contains an instance of the current scope or some enclosing
6998 -- scope (in case one of the instances appears in a subunit). Longer
6999 -- circularities involving subunits might seem too pathological to
7000 -- consider, but they were not too pathological for the authors of
7001 -- DEC bc30vsq, so we loop over all enclosing scopes, and mark all
7002 -- enclosing generic scopes as containing an instance.
7003
7004 loop
7005 -- Within a generic subprogram body, the scope is not generic, to
7006 -- allow for recursive subprograms. Use the declaration to determine
7007 -- whether this is a generic unit.
7008
7009 if Ekind (Scop) = E_Generic_Package
7010 or else (Is_Subprogram (Scop)
7011 and then Nkind (Unit_Declaration_Node (Scop)) =
7012 N_Generic_Subprogram_Declaration)
7013 then
7014 Elmt := First_Elmt (Inner_Instances (Inner));
7015
7016 while Present (Elmt) loop
7017 if Node (Elmt) = Scop then
7018 Error_Msg_Node_2 := Inner;
7019 Error_Msg_NE
7020 ("circular Instantiation: & instantiated within &!",
7021 N, Scop);
7022 return True;
7023
7024 elsif Node (Elmt) = Inner then
7025 return True;
7026
7027 elsif Contains_Instance_Of (Node (Elmt), Scop, N) then
7028 Error_Msg_Node_2 := Inner;
7029 Error_Msg_NE
7030 ("circular Instantiation: & instantiated within &!",
7031 N, Node (Elmt));
7032 return True;
7033 end if;
7034
7035 Next_Elmt (Elmt);
7036 end loop;
7037
7038 -- Indicate that Inner is being instantiated within Scop
7039
7040 Append_Elmt (Inner, Inner_Instances (Scop));
7041 end if;
7042
7043 if Scop = Standard_Standard then
7044 exit;
7045 else
7046 Scop := Scope (Scop);
7047 end if;
7048 end loop;
7049
7050 return False;
7051 end Contains_Instance_Of;
7052
7053 -----------------------
7054 -- Copy_Generic_Node --
7055 -----------------------
7056
7057 function Copy_Generic_Node
7058 (N : Node_Id;
7059 Parent_Id : Node_Id;
7060 Instantiating : Boolean) return Node_Id
7061 is
7062 Ent : Entity_Id;
7063 New_N : Node_Id;
7064
7065 function Copy_Generic_Descendant (D : Union_Id) return Union_Id;
7066 -- Check the given value of one of the Fields referenced by the current
7067 -- node to determine whether to copy it recursively. The field may hold
7068 -- a Node_Id, a List_Id, or an Elist_Id, or a plain value (Sloc, Uint,
7069 -- Char) in which case it need not be copied.
7070
7071 procedure Copy_Descendants;
7072 -- Common utility for various nodes
7073
7074 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id;
7075 -- Make copy of element list
7076
7077 function Copy_Generic_List
7078 (L : List_Id;
7079 Parent_Id : Node_Id) return List_Id;
7080 -- Apply Copy_Node recursively to the members of a node list
7081
7082 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean;
7083 -- True if an identifier is part of the defining program unit name of
7084 -- a child unit. The entity of such an identifier must be kept (for
7085 -- ASIS use) even though as the name of an enclosing generic it would
7086 -- otherwise not be preserved in the generic tree.
7087
7088 ----------------------
7089 -- Copy_Descendants --
7090 ----------------------
7091
7092 procedure Copy_Descendants is
7093 use Atree.Unchecked_Access;
7094 -- This code section is part of the implementation of an untyped
7095 -- tree traversal, so it needs direct access to node fields.
7096
7097 begin
7098 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
7099 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
7100 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
7101 Set_Field4 (New_N, Copy_Generic_Descendant (Field4 (N)));
7102 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
7103 end Copy_Descendants;
7104
7105 -----------------------------
7106 -- Copy_Generic_Descendant --
7107 -----------------------------
7108
7109 function Copy_Generic_Descendant (D : Union_Id) return Union_Id is
7110 begin
7111 if D = Union_Id (Empty) then
7112 return D;
7113
7114 elsif D in Node_Range then
7115 return Union_Id
7116 (Copy_Generic_Node (Node_Id (D), New_N, Instantiating));
7117
7118 elsif D in List_Range then
7119 return Union_Id (Copy_Generic_List (List_Id (D), New_N));
7120
7121 elsif D in Elist_Range then
7122 return Union_Id (Copy_Generic_Elist (Elist_Id (D)));
7123
7124 -- Nothing else is copyable (e.g. Uint values), return as is
7125
7126 else
7127 return D;
7128 end if;
7129 end Copy_Generic_Descendant;
7130
7131 ------------------------
7132 -- Copy_Generic_Elist --
7133 ------------------------
7134
7135 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id is
7136 M : Elmt_Id;
7137 L : Elist_Id;
7138
7139 begin
7140 if Present (E) then
7141 L := New_Elmt_List;
7142 M := First_Elmt (E);
7143 while Present (M) loop
7144 Append_Elmt
7145 (Copy_Generic_Node (Node (M), Empty, Instantiating), L);
7146 Next_Elmt (M);
7147 end loop;
7148
7149 return L;
7150
7151 else
7152 return No_Elist;
7153 end if;
7154 end Copy_Generic_Elist;
7155
7156 -----------------------
7157 -- Copy_Generic_List --
7158 -----------------------
7159
7160 function Copy_Generic_List
7161 (L : List_Id;
7162 Parent_Id : Node_Id) return List_Id
7163 is
7164 N : Node_Id;
7165 New_L : List_Id;
7166
7167 begin
7168 if Present (L) then
7169 New_L := New_List;
7170 Set_Parent (New_L, Parent_Id);
7171
7172 N := First (L);
7173 while Present (N) loop
7174 Append (Copy_Generic_Node (N, Empty, Instantiating), New_L);
7175 Next (N);
7176 end loop;
7177
7178 return New_L;
7179
7180 else
7181 return No_List;
7182 end if;
7183 end Copy_Generic_List;
7184
7185 ---------------------------
7186 -- In_Defining_Unit_Name --
7187 ---------------------------
7188
7189 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean is
7190 begin
7191 return
7192 Present (Parent (Nam))
7193 and then (Nkind (Parent (Nam)) = N_Defining_Program_Unit_Name
7194 or else
7195 (Nkind (Parent (Nam)) = N_Expanded_Name
7196 and then In_Defining_Unit_Name (Parent (Nam))));
7197 end In_Defining_Unit_Name;
7198
7199 -- Start of processing for Copy_Generic_Node
7200
7201 begin
7202 if N = Empty then
7203 return N;
7204 end if;
7205
7206 New_N := New_Copy (N);
7207
7208 -- Copy aspects if present
7209
7210 if Has_Aspects (N) then
7211 Set_Has_Aspects (New_N, False);
7212 Set_Aspect_Specifications
7213 (New_N, Copy_Generic_List (Aspect_Specifications (N), Parent_Id));
7214 end if;
7215
7216 if Instantiating then
7217 Adjust_Instantiation_Sloc (New_N, S_Adjustment);
7218 end if;
7219
7220 if not Is_List_Member (N) then
7221 Set_Parent (New_N, Parent_Id);
7222 end if;
7223
7224 -- Special casing for identifiers and other entity names and operators
7225
7226 if Nkind_In (New_N, N_Character_Literal,
7227 N_Expanded_Name,
7228 N_Identifier,
7229 N_Operator_Symbol)
7230 or else Nkind (New_N) in N_Op
7231 then
7232 if not Instantiating then
7233
7234 -- Link both nodes in order to assign subsequently the entity of
7235 -- the copy to the original node, in case this is a global
7236 -- reference.
7237
7238 Set_Associated_Node (N, New_N);
7239
7240 -- If we are within an instantiation, this is a nested generic
7241 -- that has already been analyzed at the point of definition.
7242 -- We must preserve references that were global to the enclosing
7243 -- parent at that point. Other occurrences, whether global or
7244 -- local to the current generic, must be resolved anew, so we
7245 -- reset the entity in the generic copy. A global reference has a
7246 -- smaller depth than the parent, or else the same depth in case
7247 -- both are distinct compilation units.
7248
7249 -- A child unit is implicitly declared within the enclosing parent
7250 -- but is in fact global to it, and must be preserved.
7251
7252 -- It is also possible for Current_Instantiated_Parent to be
7253 -- defined, and for this not to be a nested generic, namely if
7254 -- the unit is loaded through Rtsfind. In that case, the entity of
7255 -- New_N is only a link to the associated node, and not a defining
7256 -- occurrence.
7257
7258 -- The entities for parent units in the defining_program_unit of a
7259 -- generic child unit are established when the context of the unit
7260 -- is first analyzed, before the generic copy is made. They are
7261 -- preserved in the copy for use in ASIS queries.
7262
7263 Ent := Entity (New_N);
7264
7265 if No (Current_Instantiated_Parent.Gen_Id) then
7266 if No (Ent)
7267 or else Nkind (Ent) /= N_Defining_Identifier
7268 or else not In_Defining_Unit_Name (N)
7269 then
7270 Set_Associated_Node (New_N, Empty);
7271 end if;
7272
7273 elsif No (Ent)
7274 or else
7275 not Nkind_In (Ent, N_Defining_Identifier,
7276 N_Defining_Character_Literal,
7277 N_Defining_Operator_Symbol)
7278 or else No (Scope (Ent))
7279 or else
7280 (Scope (Ent) = Current_Instantiated_Parent.Gen_Id
7281 and then not Is_Child_Unit (Ent))
7282 or else
7283 (Scope_Depth (Scope (Ent)) >
7284 Scope_Depth (Current_Instantiated_Parent.Gen_Id)
7285 and then
7286 Get_Source_Unit (Ent) =
7287 Get_Source_Unit (Current_Instantiated_Parent.Gen_Id))
7288 then
7289 Set_Associated_Node (New_N, Empty);
7290 end if;
7291
7292 -- Case of instantiating identifier or some other name or operator
7293
7294 else
7295 -- If the associated node is still defined, the entity in it
7296 -- is global, and must be copied to the instance. If this copy
7297 -- is being made for a body to inline, it is applied to an
7298 -- instantiated tree, and the entity is already present and
7299 -- must be also preserved.
7300
7301 declare
7302 Assoc : constant Node_Id := Get_Associated_Node (N);
7303
7304 begin
7305 if Present (Assoc) then
7306 if Nkind (Assoc) = Nkind (N) then
7307 Set_Entity (New_N, Entity (Assoc));
7308 Check_Private_View (N);
7309
7310 -- The node is a reference to a global type and acts as the
7311 -- subtype mark of a qualified expression created in order
7312 -- to aid resolution of accidental overloading in instances.
7313 -- Since N is a reference to a type, the Associated_Node of
7314 -- N denotes an entity rather than another identifier. See
7315 -- Qualify_Universal_Operands for details.
7316
7317 elsif Nkind (N) = N_Identifier
7318 and then Nkind (Parent (N)) = N_Qualified_Expression
7319 and then Subtype_Mark (Parent (N)) = N
7320 and then Is_Qualified_Universal_Literal (Parent (N))
7321 then
7322 Set_Entity (New_N, Assoc);
7323
7324 -- The name in the call may be a selected component if the
7325 -- call has not been analyzed yet, as may be the case for
7326 -- pre/post conditions in a generic unit.
7327
7328 elsif Nkind (Assoc) = N_Function_Call
7329 and then Is_Entity_Name (Name (Assoc))
7330 then
7331 Set_Entity (New_N, Entity (Name (Assoc)));
7332
7333 elsif Nkind_In (Assoc, N_Defining_Identifier,
7334 N_Defining_Character_Literal,
7335 N_Defining_Operator_Symbol)
7336 and then Expander_Active
7337 then
7338 -- Inlining case: we are copying a tree that contains
7339 -- global entities, which are preserved in the copy to be
7340 -- used for subsequent inlining.
7341
7342 null;
7343
7344 else
7345 Set_Entity (New_N, Empty);
7346 end if;
7347 end if;
7348 end;
7349 end if;
7350
7351 -- For expanded name, we must copy the Prefix and Selector_Name
7352
7353 if Nkind (N) = N_Expanded_Name then
7354 Set_Prefix
7355 (New_N, Copy_Generic_Node (Prefix (N), New_N, Instantiating));
7356
7357 Set_Selector_Name (New_N,
7358 Copy_Generic_Node (Selector_Name (N), New_N, Instantiating));
7359
7360 -- For operators, we must copy the right operand
7361
7362 elsif Nkind (N) in N_Op then
7363 Set_Right_Opnd (New_N,
7364 Copy_Generic_Node (Right_Opnd (N), New_N, Instantiating));
7365
7366 -- And for binary operators, the left operand as well
7367
7368 if Nkind (N) in N_Binary_Op then
7369 Set_Left_Opnd (New_N,
7370 Copy_Generic_Node (Left_Opnd (N), New_N, Instantiating));
7371 end if;
7372 end if;
7373
7374 -- Establish a link between an entity from the generic template and the
7375 -- corresponding entity in the generic copy to be analyzed.
7376
7377 elsif Nkind (N) in N_Entity then
7378 if not Instantiating then
7379 Set_Associated_Entity (N, New_N);
7380 end if;
7381
7382 -- Clear any existing link the copy may inherit from the replicated
7383 -- generic template entity.
7384
7385 Set_Associated_Entity (New_N, Empty);
7386
7387 -- Special casing for stubs
7388
7389 elsif Nkind (N) in N_Body_Stub then
7390
7391 -- In any case, we must copy the specification or defining
7392 -- identifier as appropriate.
7393
7394 if Nkind (N) = N_Subprogram_Body_Stub then
7395 Set_Specification (New_N,
7396 Copy_Generic_Node (Specification (N), New_N, Instantiating));
7397
7398 else
7399 Set_Defining_Identifier (New_N,
7400 Copy_Generic_Node
7401 (Defining_Identifier (N), New_N, Instantiating));
7402 end if;
7403
7404 -- If we are not instantiating, then this is where we load and
7405 -- analyze subunits, i.e. at the point where the stub occurs. A
7406 -- more permissive system might defer this analysis to the point
7407 -- of instantiation, but this seems too complicated for now.
7408
7409 if not Instantiating then
7410 declare
7411 Subunit_Name : constant Unit_Name_Type := Get_Unit_Name (N);
7412 Subunit : Node_Id;
7413 Unum : Unit_Number_Type;
7414 New_Body : Node_Id;
7415
7416 begin
7417 -- Make sure that, if it is a subunit of the main unit that is
7418 -- preprocessed and if -gnateG is specified, the preprocessed
7419 -- file will be written.
7420
7421 Lib.Analysing_Subunit_Of_Main :=
7422 Lib.In_Extended_Main_Source_Unit (N);
7423 Unum :=
7424 Load_Unit
7425 (Load_Name => Subunit_Name,
7426 Required => False,
7427 Subunit => True,
7428 Error_Node => N);
7429 Lib.Analysing_Subunit_Of_Main := False;
7430
7431 -- If the proper body is not found, a warning message will be
7432 -- emitted when analyzing the stub, or later at the point of
7433 -- instantiation. Here we just leave the stub as is.
7434
7435 if Unum = No_Unit then
7436 Subunits_Missing := True;
7437 goto Subunit_Not_Found;
7438 end if;
7439
7440 Subunit := Cunit (Unum);
7441
7442 if Nkind (Unit (Subunit)) /= N_Subunit then
7443 Error_Msg_N
7444 ("found child unit instead of expected SEPARATE subunit",
7445 Subunit);
7446 Error_Msg_Sloc := Sloc (N);
7447 Error_Msg_N ("\to complete stub #", Subunit);
7448 goto Subunit_Not_Found;
7449 end if;
7450
7451 -- We must create a generic copy of the subunit, in order to
7452 -- perform semantic analysis on it, and we must replace the
7453 -- stub in the original generic unit with the subunit, in order
7454 -- to preserve non-local references within.
7455
7456 -- Only the proper body needs to be copied. Library_Unit and
7457 -- context clause are simply inherited by the generic copy.
7458 -- Note that the copy (which may be recursive if there are
7459 -- nested subunits) must be done first, before attaching it to
7460 -- the enclosing generic.
7461
7462 New_Body :=
7463 Copy_Generic_Node
7464 (Proper_Body (Unit (Subunit)),
7465 Empty, Instantiating => False);
7466
7467 -- Now place the original proper body in the original generic
7468 -- unit. This is a body, not a compilation unit.
7469
7470 Rewrite (N, Proper_Body (Unit (Subunit)));
7471 Set_Is_Compilation_Unit (Defining_Entity (N), False);
7472 Set_Was_Originally_Stub (N);
7473
7474 -- Finally replace the body of the subunit with its copy, and
7475 -- make this new subunit into the library unit of the generic
7476 -- copy, which does not have stubs any longer.
7477
7478 Set_Proper_Body (Unit (Subunit), New_Body);
7479 Set_Library_Unit (New_N, Subunit);
7480 Inherit_Context (Unit (Subunit), N);
7481 end;
7482
7483 -- If we are instantiating, this must be an error case, since
7484 -- otherwise we would have replaced the stub node by the proper body
7485 -- that corresponds. So just ignore it in the copy (i.e. we have
7486 -- copied it, and that is good enough).
7487
7488 else
7489 null;
7490 end if;
7491
7492 <<Subunit_Not_Found>> null;
7493
7494 -- If the node is a compilation unit, it is the subunit of a stub, which
7495 -- has been loaded already (see code below). In this case, the library
7496 -- unit field of N points to the parent unit (which is a compilation
7497 -- unit) and need not (and cannot) be copied.
7498
7499 -- When the proper body of the stub is analyzed, the library_unit link
7500 -- is used to establish the proper context (see sem_ch10).
7501
7502 -- The other fields of a compilation unit are copied as usual
7503
7504 elsif Nkind (N) = N_Compilation_Unit then
7505
7506 -- This code can only be executed when not instantiating, because in
7507 -- the copy made for an instantiation, the compilation unit node has
7508 -- disappeared at the point that a stub is replaced by its proper
7509 -- body.
7510
7511 pragma Assert (not Instantiating);
7512
7513 Set_Context_Items (New_N,
7514 Copy_Generic_List (Context_Items (N), New_N));
7515
7516 Set_Unit (New_N,
7517 Copy_Generic_Node (Unit (N), New_N, False));
7518
7519 Set_First_Inlined_Subprogram (New_N,
7520 Copy_Generic_Node
7521 (First_Inlined_Subprogram (N), New_N, False));
7522
7523 Set_Aux_Decls_Node (New_N,
7524 Copy_Generic_Node (Aux_Decls_Node (N), New_N, False));
7525
7526 -- For an assignment node, the assignment is known to be semantically
7527 -- legal if we are instantiating the template. This avoids incorrect
7528 -- diagnostics in generated code.
7529
7530 elsif Nkind (N) = N_Assignment_Statement then
7531
7532 -- Copy name and expression fields in usual manner
7533
7534 Set_Name (New_N,
7535 Copy_Generic_Node (Name (N), New_N, Instantiating));
7536
7537 Set_Expression (New_N,
7538 Copy_Generic_Node (Expression (N), New_N, Instantiating));
7539
7540 if Instantiating then
7541 Set_Assignment_OK (Name (New_N), True);
7542 end if;
7543
7544 elsif Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
7545 if not Instantiating then
7546 Set_Associated_Node (N, New_N);
7547
7548 else
7549 if Present (Get_Associated_Node (N))
7550 and then Nkind (Get_Associated_Node (N)) = Nkind (N)
7551 then
7552 -- In the generic the aggregate has some composite type. If at
7553 -- the point of instantiation the type has a private view,
7554 -- install the full view (and that of its ancestors, if any).
7555
7556 declare
7557 T : Entity_Id := (Etype (Get_Associated_Node (New_N)));
7558 Rt : Entity_Id;
7559
7560 begin
7561 if Present (T) and then Is_Private_Type (T) then
7562 Switch_View (T);
7563 end if;
7564
7565 if Present (T)
7566 and then Is_Tagged_Type (T)
7567 and then Is_Derived_Type (T)
7568 then
7569 Rt := Root_Type (T);
7570
7571 loop
7572 T := Etype (T);
7573
7574 if Is_Private_Type (T) then
7575 Switch_View (T);
7576 end if;
7577
7578 exit when T = Rt;
7579 end loop;
7580 end if;
7581 end;
7582 end if;
7583 end if;
7584
7585 -- Do not copy the associated node, which points to the generic copy
7586 -- of the aggregate.
7587
7588 declare
7589 use Atree.Unchecked_Access;
7590 -- This code section is part of the implementation of an untyped
7591 -- tree traversal, so it needs direct access to node fields.
7592
7593 begin
7594 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
7595 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
7596 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
7597 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
7598 end;
7599
7600 -- Allocators do not have an identifier denoting the access type, so we
7601 -- must locate it through the expression to check whether the views are
7602 -- consistent.
7603
7604 elsif Nkind (N) = N_Allocator
7605 and then Nkind (Expression (N)) = N_Qualified_Expression
7606 and then Is_Entity_Name (Subtype_Mark (Expression (N)))
7607 and then Instantiating
7608 then
7609 declare
7610 T : constant Node_Id :=
7611 Get_Associated_Node (Subtype_Mark (Expression (N)));
7612 Acc_T : Entity_Id;
7613
7614 begin
7615 if Present (T) then
7616
7617 -- Retrieve the allocator node in the generic copy
7618
7619 Acc_T := Etype (Parent (Parent (T)));
7620
7621 if Present (Acc_T) and then Is_Private_Type (Acc_T) then
7622 Switch_View (Acc_T);
7623 end if;
7624 end if;
7625
7626 Copy_Descendants;
7627 end;
7628
7629 -- For a proper body, we must catch the case of a proper body that
7630 -- replaces a stub. This represents the point at which a separate
7631 -- compilation unit, and hence template file, may be referenced, so we
7632 -- must make a new source instantiation entry for the template of the
7633 -- subunit, and ensure that all nodes in the subunit are adjusted using
7634 -- this new source instantiation entry.
7635
7636 elsif Nkind (N) in N_Proper_Body then
7637 declare
7638 Save_Adjustment : constant Sloc_Adjustment := S_Adjustment;
7639
7640 begin
7641 if Instantiating and then Was_Originally_Stub (N) then
7642 Create_Instantiation_Source
7643 (Instantiation_Node,
7644 Defining_Entity (N),
7645 False,
7646 S_Adjustment);
7647 end if;
7648
7649 -- Now copy the fields of the proper body, using the new
7650 -- adjustment factor if one was needed as per test above.
7651
7652 Copy_Descendants;
7653
7654 -- Restore the original adjustment factor in case changed
7655
7656 S_Adjustment := Save_Adjustment;
7657 end;
7658
7659 elsif Nkind (N) = N_Pragma and then Instantiating then
7660
7661 -- Do not copy Comment or Ident pragmas their content is relevant to
7662 -- the generic unit, not to the instantiating unit.
7663
7664 if Nam_In (Pragma_Name (N), Name_Comment, Name_Ident) then
7665 New_N := Make_Null_Statement (Sloc (N));
7666
7667 -- Do not copy pragmas generated from aspects because the pragmas do
7668 -- not carry any semantic information, plus they will be regenerated
7669 -- in the instance.
7670
7671 elsif From_Aspect_Specification (N) then
7672 New_N := Make_Null_Statement (Sloc (N));
7673
7674 else
7675 Copy_Descendants;
7676 end if;
7677
7678 elsif Nkind_In (N, N_Integer_Literal, N_Real_Literal) then
7679
7680 -- No descendant fields need traversing
7681
7682 null;
7683
7684 elsif Nkind (N) = N_String_Literal
7685 and then Present (Etype (N))
7686 and then Instantiating
7687 then
7688 -- If the string is declared in an outer scope, the string_literal
7689 -- subtype created for it may have the wrong scope. Force reanalysis
7690 -- of the constant to generate a new itype in the proper context.
7691
7692 Set_Etype (New_N, Empty);
7693 Set_Analyzed (New_N, False);
7694
7695 -- For the remaining nodes, copy their descendants recursively
7696
7697 else
7698 Copy_Descendants;
7699
7700 if Instantiating and then Nkind (N) = N_Subprogram_Body then
7701 Set_Generic_Parent (Specification (New_N), N);
7702
7703 -- Should preserve Corresponding_Spec??? (12.3(14))
7704 end if;
7705 end if;
7706
7707 -- Propagate dimensions if present, so that they are reflected in the
7708 -- instance.
7709
7710 if Nkind (N) in N_Has_Etype
7711 and then (Nkind (N) in N_Op or else Is_Entity_Name (N))
7712 and then Present (Etype (N))
7713 and then Is_Floating_Point_Type (Etype (N))
7714 and then Has_Dimension_System (Etype (N))
7715 then
7716 Copy_Dimensions (N, New_N);
7717 end if;
7718
7719 return New_N;
7720 end Copy_Generic_Node;
7721
7722 ----------------------------
7723 -- Denotes_Formal_Package --
7724 ----------------------------
7725
7726 function Denotes_Formal_Package
7727 (Pack : Entity_Id;
7728 On_Exit : Boolean := False;
7729 Instance : Entity_Id := Empty) return Boolean
7730 is
7731 Par : Entity_Id;
7732 Scop : constant Entity_Id := Scope (Pack);
7733 E : Entity_Id;
7734
7735 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean;
7736 -- The package in question may be an actual for a previous formal
7737 -- package P of the current instance, so examine its actuals as well.
7738 -- This must be recursive over other formal packages.
7739
7740 ----------------------------------
7741 -- Is_Actual_Of_Previous_Formal --
7742 ----------------------------------
7743
7744 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean is
7745 E1 : Entity_Id;
7746
7747 begin
7748 E1 := First_Entity (P);
7749 while Present (E1) and then E1 /= Instance loop
7750 if Ekind (E1) = E_Package
7751 and then Nkind (Parent (E1)) = N_Package_Renaming_Declaration
7752 then
7753 if Renamed_Object (E1) = Pack then
7754 return True;
7755
7756 elsif E1 = P or else Renamed_Object (E1) = P then
7757 return False;
7758
7759 elsif Is_Actual_Of_Previous_Formal (E1) then
7760 return True;
7761 end if;
7762 end if;
7763
7764 Next_Entity (E1);
7765 end loop;
7766
7767 return False;
7768 end Is_Actual_Of_Previous_Formal;
7769
7770 -- Start of processing for Denotes_Formal_Package
7771
7772 begin
7773 if On_Exit then
7774 Par :=
7775 Instance_Envs.Table
7776 (Instance_Envs.Last).Instantiated_Parent.Act_Id;
7777 else
7778 Par := Current_Instantiated_Parent.Act_Id;
7779 end if;
7780
7781 if Ekind (Scop) = E_Generic_Package
7782 or else Nkind (Unit_Declaration_Node (Scop)) =
7783 N_Generic_Subprogram_Declaration
7784 then
7785 return True;
7786
7787 elsif Nkind (Original_Node (Unit_Declaration_Node (Pack))) =
7788 N_Formal_Package_Declaration
7789 then
7790 return True;
7791
7792 elsif No (Par) then
7793 return False;
7794
7795 else
7796 -- Check whether this package is associated with a formal package of
7797 -- the enclosing instantiation. Iterate over the list of renamings.
7798
7799 E := First_Entity (Par);
7800 while Present (E) loop
7801 if Ekind (E) /= E_Package
7802 or else Nkind (Parent (E)) /= N_Package_Renaming_Declaration
7803 then
7804 null;
7805
7806 elsif Renamed_Object (E) = Par then
7807 return False;
7808
7809 elsif Renamed_Object (E) = Pack then
7810 return True;
7811
7812 elsif Is_Actual_Of_Previous_Formal (E) then
7813 return True;
7814
7815 end if;
7816
7817 Next_Entity (E);
7818 end loop;
7819
7820 return False;
7821 end if;
7822 end Denotes_Formal_Package;
7823
7824 -----------------
7825 -- End_Generic --
7826 -----------------
7827
7828 procedure End_Generic is
7829 begin
7830 -- ??? More things could be factored out in this routine. Should
7831 -- probably be done at a later stage.
7832
7833 Inside_A_Generic := Generic_Flags.Table (Generic_Flags.Last);
7834 Generic_Flags.Decrement_Last;
7835
7836 Expander_Mode_Restore;
7837 end End_Generic;
7838
7839 -------------
7840 -- Earlier --
7841 -------------
7842
7843 function Earlier (N1, N2 : Node_Id) return Boolean is
7844 procedure Find_Depth (P : in out Node_Id; D : in out Integer);
7845 -- Find distance from given node to enclosing compilation unit
7846
7847 ----------------
7848 -- Find_Depth --
7849 ----------------
7850
7851 procedure Find_Depth (P : in out Node_Id; D : in out Integer) is
7852 begin
7853 while Present (P)
7854 and then Nkind (P) /= N_Compilation_Unit
7855 loop
7856 P := True_Parent (P);
7857 D := D + 1;
7858 end loop;
7859 end Find_Depth;
7860
7861 -- Local declarations
7862
7863 D1 : Integer := 0;
7864 D2 : Integer := 0;
7865 P1 : Node_Id := N1;
7866 P2 : Node_Id := N2;
7867 T1 : Source_Ptr;
7868 T2 : Source_Ptr;
7869
7870 -- Start of processing for Earlier
7871
7872 begin
7873 Find_Depth (P1, D1);
7874 Find_Depth (P2, D2);
7875
7876 if P1 /= P2 then
7877 return False;
7878 else
7879 P1 := N1;
7880 P2 := N2;
7881 end if;
7882
7883 while D1 > D2 loop
7884 P1 := True_Parent (P1);
7885 D1 := D1 - 1;
7886 end loop;
7887
7888 while D2 > D1 loop
7889 P2 := True_Parent (P2);
7890 D2 := D2 - 1;
7891 end loop;
7892
7893 -- At this point P1 and P2 are at the same distance from the root.
7894 -- We examine their parents until we find a common declarative list.
7895 -- If we reach the root, N1 and N2 do not descend from the same
7896 -- declarative list (e.g. one is nested in the declarative part and
7897 -- the other is in a block in the statement part) and the earlier
7898 -- one is already frozen.
7899
7900 while not Is_List_Member (P1)
7901 or else not Is_List_Member (P2)
7902 or else List_Containing (P1) /= List_Containing (P2)
7903 loop
7904 P1 := True_Parent (P1);
7905 P2 := True_Parent (P2);
7906
7907 if Nkind (Parent (P1)) = N_Subunit then
7908 P1 := Corresponding_Stub (Parent (P1));
7909 end if;
7910
7911 if Nkind (Parent (P2)) = N_Subunit then
7912 P2 := Corresponding_Stub (Parent (P2));
7913 end if;
7914
7915 if P1 = P2 then
7916 return False;
7917 end if;
7918 end loop;
7919
7920 -- Expanded code usually shares the source location of the original
7921 -- construct it was generated for. This however may not necessarily
7922 -- reflect the true location of the code within the tree.
7923
7924 -- Before comparing the slocs of the two nodes, make sure that we are
7925 -- working with correct source locations. Assume that P1 is to the left
7926 -- of P2. If either one does not come from source, traverse the common
7927 -- list heading towards the other node and locate the first source
7928 -- statement.
7929
7930 -- P1 P2
7931 -- ----+===+===+--------------+===+===+----
7932 -- expanded code expanded code
7933
7934 if not Comes_From_Source (P1) then
7935 while Present (P1) loop
7936
7937 -- Neither P2 nor a source statement were located during the
7938 -- search. If we reach the end of the list, then P1 does not
7939 -- occur earlier than P2.
7940
7941 -- ---->
7942 -- start --- P2 ----- P1 --- end
7943
7944 if No (Next (P1)) then
7945 return False;
7946
7947 -- We encounter P2 while going to the right of the list. This
7948 -- means that P1 does indeed appear earlier.
7949
7950 -- ---->
7951 -- start --- P1 ===== P2 --- end
7952 -- expanded code in between
7953
7954 elsif P1 = P2 then
7955 return True;
7956
7957 -- No need to look any further since we have located a source
7958 -- statement.
7959
7960 elsif Comes_From_Source (P1) then
7961 exit;
7962 end if;
7963
7964 -- Keep going right
7965
7966 Next (P1);
7967 end loop;
7968 end if;
7969
7970 if not Comes_From_Source (P2) then
7971 while Present (P2) loop
7972
7973 -- Neither P1 nor a source statement were located during the
7974 -- search. If we reach the start of the list, then P1 does not
7975 -- occur earlier than P2.
7976
7977 -- <----
7978 -- start --- P2 --- P1 --- end
7979
7980 if No (Prev (P2)) then
7981 return False;
7982
7983 -- We encounter P1 while going to the left of the list. This
7984 -- means that P1 does indeed appear earlier.
7985
7986 -- <----
7987 -- start --- P1 ===== P2 --- end
7988 -- expanded code in between
7989
7990 elsif P2 = P1 then
7991 return True;
7992
7993 -- No need to look any further since we have located a source
7994 -- statement.
7995
7996 elsif Comes_From_Source (P2) then
7997 exit;
7998 end if;
7999
8000 -- Keep going left
8001
8002 Prev (P2);
8003 end loop;
8004 end if;
8005
8006 -- At this point either both nodes came from source or we approximated
8007 -- their source locations through neighboring source statements.
8008
8009 T1 := Top_Level_Location (Sloc (P1));
8010 T2 := Top_Level_Location (Sloc (P2));
8011
8012 -- When two nodes come from the same instance, they have identical top
8013 -- level locations. To determine proper relation within the tree, check
8014 -- their locations within the template.
8015
8016 if T1 = T2 then
8017 return Sloc (P1) < Sloc (P2);
8018
8019 -- The two nodes either come from unrelated instances or do not come
8020 -- from instantiated code at all.
8021
8022 else
8023 return T1 < T2;
8024 end if;
8025 end Earlier;
8026
8027 ----------------------
8028 -- Find_Actual_Type --
8029 ----------------------
8030
8031 function Find_Actual_Type
8032 (Typ : Entity_Id;
8033 Gen_Type : Entity_Id) return Entity_Id
8034 is
8035 Gen_Scope : constant Entity_Id := Scope (Gen_Type);
8036 T : Entity_Id;
8037
8038 begin
8039 -- Special processing only applies to child units
8040
8041 if not Is_Child_Unit (Gen_Scope) then
8042 return Get_Instance_Of (Typ);
8043
8044 -- If designated or component type is itself a formal of the child unit,
8045 -- its instance is available.
8046
8047 elsif Scope (Typ) = Gen_Scope then
8048 return Get_Instance_Of (Typ);
8049
8050 -- If the array or access type is not declared in the parent unit,
8051 -- no special processing needed.
8052
8053 elsif not Is_Generic_Type (Typ)
8054 and then Scope (Gen_Scope) /= Scope (Typ)
8055 then
8056 return Get_Instance_Of (Typ);
8057
8058 -- Otherwise, retrieve designated or component type by visibility
8059
8060 else
8061 T := Current_Entity (Typ);
8062 while Present (T) loop
8063 if In_Open_Scopes (Scope (T)) then
8064 return T;
8065 elsif Is_Generic_Actual_Type (T) then
8066 return T;
8067 end if;
8068
8069 T := Homonym (T);
8070 end loop;
8071
8072 return Typ;
8073 end if;
8074 end Find_Actual_Type;
8075
8076 ----------------------------
8077 -- Freeze_Subprogram_Body --
8078 ----------------------------
8079
8080 procedure Freeze_Subprogram_Body
8081 (Inst_Node : Node_Id;
8082 Gen_Body : Node_Id;
8083 Pack_Id : Entity_Id)
8084 is
8085 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
8086 Par : constant Entity_Id := Scope (Gen_Unit);
8087 E_G_Id : Entity_Id;
8088 Enc_G : Entity_Id;
8089 Enc_I : Node_Id;
8090 F_Node : Node_Id;
8091
8092 function Enclosing_Package_Body (N : Node_Id) return Node_Id;
8093 -- Find innermost package body that encloses the given node, and which
8094 -- is not a compilation unit. Freeze nodes for the instance, or for its
8095 -- enclosing body, may be inserted after the enclosing_body of the
8096 -- generic unit. Used to determine proper placement of freeze node for
8097 -- both package and subprogram instances.
8098
8099 function Package_Freeze_Node (B : Node_Id) return Node_Id;
8100 -- Find entity for given package body, and locate or create a freeze
8101 -- node for it.
8102
8103 ----------------------------
8104 -- Enclosing_Package_Body --
8105 ----------------------------
8106
8107 function Enclosing_Package_Body (N : Node_Id) return Node_Id is
8108 P : Node_Id;
8109
8110 begin
8111 P := Parent (N);
8112 while Present (P)
8113 and then Nkind (Parent (P)) /= N_Compilation_Unit
8114 loop
8115 if Nkind (P) = N_Package_Body then
8116 if Nkind (Parent (P)) = N_Subunit then
8117 return Corresponding_Stub (Parent (P));
8118 else
8119 return P;
8120 end if;
8121 end if;
8122
8123 P := True_Parent (P);
8124 end loop;
8125
8126 return Empty;
8127 end Enclosing_Package_Body;
8128
8129 -------------------------
8130 -- Package_Freeze_Node --
8131 -------------------------
8132
8133 function Package_Freeze_Node (B : Node_Id) return Node_Id is
8134 Id : Entity_Id;
8135
8136 begin
8137 if Nkind (B) = N_Package_Body then
8138 Id := Corresponding_Spec (B);
8139 else pragma Assert (Nkind (B) = N_Package_Body_Stub);
8140 Id := Corresponding_Spec (Proper_Body (Unit (Library_Unit (B))));
8141 end if;
8142
8143 Ensure_Freeze_Node (Id);
8144 return Freeze_Node (Id);
8145 end Package_Freeze_Node;
8146
8147 -- Start of processing for Freeze_Subprogram_Body
8148
8149 begin
8150 -- If the instance and the generic body appear within the same unit, and
8151 -- the instance precedes the generic, the freeze node for the instance
8152 -- must appear after that of the generic. If the generic is nested
8153 -- within another instance I2, then current instance must be frozen
8154 -- after I2. In both cases, the freeze nodes are those of enclosing
8155 -- packages. Otherwise, the freeze node is placed at the end of the
8156 -- current declarative part.
8157
8158 Enc_G := Enclosing_Package_Body (Gen_Body);
8159 Enc_I := Enclosing_Package_Body (Inst_Node);
8160 Ensure_Freeze_Node (Pack_Id);
8161 F_Node := Freeze_Node (Pack_Id);
8162
8163 if Is_Generic_Instance (Par)
8164 and then Present (Freeze_Node (Par))
8165 and then In_Same_Declarative_Part (Freeze_Node (Par), Inst_Node)
8166 then
8167 -- The parent was a premature instantiation. Insert freeze node at
8168 -- the end the current declarative part.
8169
8170 if ABE_Is_Certain (Get_Package_Instantiation_Node (Par)) then
8171 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8172
8173 -- Handle the following case:
8174 --
8175 -- package Parent_Inst is new ...
8176 -- Parent_Inst []
8177 --
8178 -- procedure P ... -- this body freezes Parent_Inst
8179 --
8180 -- package Inst is new ...
8181 --
8182 -- In this particular scenario, the freeze node for Inst must be
8183 -- inserted in the same manner as that of Parent_Inst - before the
8184 -- next source body or at the end of the declarative list (body not
8185 -- available). If body P did not exist and Parent_Inst was frozen
8186 -- after Inst, either by a body following Inst or at the end of the
8187 -- declarative region, the freeze node for Inst must be inserted
8188 -- after that of Parent_Inst. This relation is established by
8189 -- comparing the Slocs of Parent_Inst freeze node and Inst.
8190
8191 elsif List_Containing (Get_Package_Instantiation_Node (Par)) =
8192 List_Containing (Inst_Node)
8193 and then Sloc (Freeze_Node (Par)) < Sloc (Inst_Node)
8194 then
8195 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8196
8197 else
8198 Insert_After (Freeze_Node (Par), F_Node);
8199 end if;
8200
8201 -- The body enclosing the instance should be frozen after the body that
8202 -- includes the generic, because the body of the instance may make
8203 -- references to entities therein. If the two are not in the same
8204 -- declarative part, or if the one enclosing the instance is frozen
8205 -- already, freeze the instance at the end of the current declarative
8206 -- part.
8207
8208 elsif Is_Generic_Instance (Par)
8209 and then Present (Freeze_Node (Par))
8210 and then Present (Enc_I)
8211 then
8212 if In_Same_Declarative_Part (Freeze_Node (Par), Enc_I)
8213 or else
8214 (Nkind (Enc_I) = N_Package_Body
8215 and then
8216 In_Same_Declarative_Part (Freeze_Node (Par), Parent (Enc_I)))
8217 then
8218 -- The enclosing package may contain several instances. Rather
8219 -- than computing the earliest point at which to insert its freeze
8220 -- node, we place it at the end of the declarative part of the
8221 -- parent of the generic.
8222
8223 Insert_Freeze_Node_For_Instance
8224 (Freeze_Node (Par), Package_Freeze_Node (Enc_I));
8225 end if;
8226
8227 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8228
8229 elsif Present (Enc_G)
8230 and then Present (Enc_I)
8231 and then Enc_G /= Enc_I
8232 and then Earlier (Inst_Node, Gen_Body)
8233 then
8234 if Nkind (Enc_G) = N_Package_Body then
8235 E_G_Id :=
8236 Corresponding_Spec (Enc_G);
8237 else pragma Assert (Nkind (Enc_G) = N_Package_Body_Stub);
8238 E_G_Id :=
8239 Corresponding_Spec (Proper_Body (Unit (Library_Unit (Enc_G))));
8240 end if;
8241
8242 -- Freeze package that encloses instance, and place node after the
8243 -- package that encloses generic. If enclosing package is already
8244 -- frozen we have to assume it is at the proper place. This may be a
8245 -- potential ABE that requires dynamic checking. Do not add a freeze
8246 -- node if the package that encloses the generic is inside the body
8247 -- that encloses the instance, because the freeze node would be in
8248 -- the wrong scope. Additional contortions needed if the bodies are
8249 -- within a subunit.
8250
8251 declare
8252 Enclosing_Body : Node_Id;
8253
8254 begin
8255 if Nkind (Enc_I) = N_Package_Body_Stub then
8256 Enclosing_Body := Proper_Body (Unit (Library_Unit (Enc_I)));
8257 else
8258 Enclosing_Body := Enc_I;
8259 end if;
8260
8261 if Parent (List_Containing (Enc_G)) /= Enclosing_Body then
8262 Insert_Freeze_Node_For_Instance
8263 (Enc_G, Package_Freeze_Node (Enc_I));
8264 end if;
8265 end;
8266
8267 -- Freeze enclosing subunit before instance
8268
8269 Ensure_Freeze_Node (E_G_Id);
8270
8271 if not Is_List_Member (Freeze_Node (E_G_Id)) then
8272 Insert_After (Enc_G, Freeze_Node (E_G_Id));
8273 end if;
8274
8275 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8276
8277 else
8278 -- If none of the above, insert freeze node at the end of the current
8279 -- declarative part.
8280
8281 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8282 end if;
8283 end Freeze_Subprogram_Body;
8284
8285 ----------------
8286 -- Get_Gen_Id --
8287 ----------------
8288
8289 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id is
8290 begin
8291 return Generic_Renamings.Table (E).Gen_Id;
8292 end Get_Gen_Id;
8293
8294 ---------------------
8295 -- Get_Instance_Of --
8296 ---------------------
8297
8298 function Get_Instance_Of (A : Entity_Id) return Entity_Id is
8299 Res : constant Assoc_Ptr := Generic_Renamings_HTable.Get (A);
8300
8301 begin
8302 if Res /= Assoc_Null then
8303 return Generic_Renamings.Table (Res).Act_Id;
8304
8305 else
8306 -- On exit, entity is not instantiated: not a generic parameter, or
8307 -- else parameter of an inner generic unit.
8308
8309 return A;
8310 end if;
8311 end Get_Instance_Of;
8312
8313 ------------------------------------
8314 -- Get_Package_Instantiation_Node --
8315 ------------------------------------
8316
8317 function Get_Package_Instantiation_Node (A : Entity_Id) return Node_Id is
8318 Decl : Node_Id := Unit_Declaration_Node (A);
8319 Inst : Node_Id;
8320
8321 begin
8322 -- If the Package_Instantiation attribute has been set on the package
8323 -- entity, then use it directly when it (or its Original_Node) refers
8324 -- to an N_Package_Instantiation node. In principle it should be
8325 -- possible to have this field set in all cases, which should be
8326 -- investigated, and would allow this function to be significantly
8327 -- simplified. ???
8328
8329 Inst := Package_Instantiation (A);
8330
8331 if Present (Inst) then
8332 if Nkind (Inst) = N_Package_Instantiation then
8333 return Inst;
8334
8335 elsif Nkind (Original_Node (Inst)) = N_Package_Instantiation then
8336 return Original_Node (Inst);
8337 end if;
8338 end if;
8339
8340 -- If the instantiation is a compilation unit that does not need body
8341 -- then the instantiation node has been rewritten as a package
8342 -- declaration for the instance, and we return the original node.
8343
8344 -- If it is a compilation unit and the instance node has not been
8345 -- rewritten, then it is still the unit of the compilation. Finally, if
8346 -- a body is present, this is a parent of the main unit whose body has
8347 -- been compiled for inlining purposes, and the instantiation node has
8348 -- been rewritten with the instance body.
8349
8350 -- Otherwise the instantiation node appears after the declaration. If
8351 -- the entity is a formal package, the declaration may have been
8352 -- rewritten as a generic declaration (in the case of a formal with box)
8353 -- or left as a formal package declaration if it has actuals, and is
8354 -- found with a forward search.
8355
8356 if Nkind (Parent (Decl)) = N_Compilation_Unit then
8357 if Nkind (Decl) = N_Package_Declaration
8358 and then Present (Corresponding_Body (Decl))
8359 then
8360 Decl := Unit_Declaration_Node (Corresponding_Body (Decl));
8361 end if;
8362
8363 if Nkind (Original_Node (Decl)) = N_Package_Instantiation then
8364 return Original_Node (Decl);
8365 else
8366 return Unit (Parent (Decl));
8367 end if;
8368
8369 elsif Nkind (Decl) = N_Package_Declaration
8370 and then Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration
8371 then
8372 return Original_Node (Decl);
8373
8374 else
8375 Inst := Next (Decl);
8376 while not Nkind_In (Inst, N_Package_Instantiation,
8377 N_Formal_Package_Declaration)
8378 loop
8379 Next (Inst);
8380 end loop;
8381
8382 return Inst;
8383 end if;
8384 end Get_Package_Instantiation_Node;
8385
8386 ------------------------
8387 -- Has_Been_Exchanged --
8388 ------------------------
8389
8390 function Has_Been_Exchanged (E : Entity_Id) return Boolean is
8391 Next : Elmt_Id;
8392
8393 begin
8394 Next := First_Elmt (Exchanged_Views);
8395 while Present (Next) loop
8396 if Full_View (Node (Next)) = E then
8397 return True;
8398 end if;
8399
8400 Next_Elmt (Next);
8401 end loop;
8402
8403 return False;
8404 end Has_Been_Exchanged;
8405
8406 ----------
8407 -- Hash --
8408 ----------
8409
8410 function Hash (F : Entity_Id) return HTable_Range is
8411 begin
8412 return HTable_Range (F mod HTable_Size);
8413 end Hash;
8414
8415 ------------------------
8416 -- Hide_Current_Scope --
8417 ------------------------
8418
8419 procedure Hide_Current_Scope is
8420 C : constant Entity_Id := Current_Scope;
8421 E : Entity_Id;
8422
8423 begin
8424 Set_Is_Hidden_Open_Scope (C);
8425
8426 E := First_Entity (C);
8427 while Present (E) loop
8428 if Is_Immediately_Visible (E) then
8429 Set_Is_Immediately_Visible (E, False);
8430 Append_Elmt (E, Hidden_Entities);
8431 end if;
8432
8433 Next_Entity (E);
8434 end loop;
8435
8436 -- Make the scope name invisible as well. This is necessary, but might
8437 -- conflict with calls to Rtsfind later on, in case the scope is a
8438 -- predefined one. There is no clean solution to this problem, so for
8439 -- now we depend on the user not redefining Standard itself in one of
8440 -- the parent units.
8441
8442 if Is_Immediately_Visible (C) and then C /= Standard_Standard then
8443 Set_Is_Immediately_Visible (C, False);
8444 Append_Elmt (C, Hidden_Entities);
8445 end if;
8446
8447 end Hide_Current_Scope;
8448
8449 --------------
8450 -- Init_Env --
8451 --------------
8452
8453 procedure Init_Env is
8454 Saved : Instance_Env;
8455
8456 begin
8457 Saved.Instantiated_Parent := Current_Instantiated_Parent;
8458 Saved.Exchanged_Views := Exchanged_Views;
8459 Saved.Hidden_Entities := Hidden_Entities;
8460 Saved.Current_Sem_Unit := Current_Sem_Unit;
8461 Saved.Parent_Unit_Visible := Parent_Unit_Visible;
8462 Saved.Instance_Parent_Unit := Instance_Parent_Unit;
8463
8464 -- Save configuration switches. These may be reset if the unit is a
8465 -- predefined unit, and the current mode is not Ada 2005.
8466
8467 Save_Opt_Config_Switches (Saved.Switches);
8468
8469 Instance_Envs.Append (Saved);
8470
8471 Exchanged_Views := New_Elmt_List;
8472 Hidden_Entities := New_Elmt_List;
8473
8474 -- Make dummy entry for Instantiated parent. If generic unit is legal,
8475 -- this is set properly in Set_Instance_Env.
8476
8477 Current_Instantiated_Parent :=
8478 (Current_Scope, Current_Scope, Assoc_Null);
8479 end Init_Env;
8480
8481 ------------------------------
8482 -- In_Same_Declarative_Part --
8483 ------------------------------
8484
8485 function In_Same_Declarative_Part
8486 (F_Node : Node_Id;
8487 Inst : Node_Id) return Boolean
8488 is
8489 Decls : constant Node_Id := Parent (F_Node);
8490 Nod : Node_Id;
8491
8492 begin
8493 Nod := Parent (Inst);
8494 while Present (Nod) loop
8495 if Nod = Decls then
8496 return True;
8497
8498 elsif Nkind_In (Nod, N_Subprogram_Body,
8499 N_Package_Body,
8500 N_Package_Declaration,
8501 N_Task_Body,
8502 N_Protected_Body,
8503 N_Block_Statement)
8504 then
8505 return False;
8506
8507 elsif Nkind (Nod) = N_Subunit then
8508 Nod := Corresponding_Stub (Nod);
8509
8510 elsif Nkind (Nod) = N_Compilation_Unit then
8511 return False;
8512
8513 else
8514 Nod := Parent (Nod);
8515 end if;
8516 end loop;
8517
8518 return False;
8519 end In_Same_Declarative_Part;
8520
8521 ---------------------
8522 -- In_Main_Context --
8523 ---------------------
8524
8525 function In_Main_Context (E : Entity_Id) return Boolean is
8526 Context : List_Id;
8527 Clause : Node_Id;
8528 Nam : Node_Id;
8529
8530 begin
8531 if not Is_Compilation_Unit (E)
8532 or else Ekind (E) /= E_Package
8533 or else In_Private_Part (E)
8534 then
8535 return False;
8536 end if;
8537
8538 Context := Context_Items (Cunit (Main_Unit));
8539
8540 Clause := First (Context);
8541 while Present (Clause) loop
8542 if Nkind (Clause) = N_With_Clause then
8543 Nam := Name (Clause);
8544
8545 -- If the current scope is part of the context of the main unit,
8546 -- analysis of the corresponding with_clause is not complete, and
8547 -- the entity is not set. We use the Chars field directly, which
8548 -- might produce false positives in rare cases, but guarantees
8549 -- that we produce all the instance bodies we will need.
8550
8551 if (Is_Entity_Name (Nam) and then Chars (Nam) = Chars (E))
8552 or else (Nkind (Nam) = N_Selected_Component
8553 and then Chars (Selector_Name (Nam)) = Chars (E))
8554 then
8555 return True;
8556 end if;
8557 end if;
8558
8559 Next (Clause);
8560 end loop;
8561
8562 return False;
8563 end In_Main_Context;
8564
8565 ---------------------
8566 -- Inherit_Context --
8567 ---------------------
8568
8569 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id) is
8570 Current_Context : List_Id;
8571 Current_Unit : Node_Id;
8572 Item : Node_Id;
8573 New_I : Node_Id;
8574
8575 Clause : Node_Id;
8576 OK : Boolean;
8577 Lib_Unit : Node_Id;
8578
8579 begin
8580 if Nkind (Parent (Gen_Decl)) = N_Compilation_Unit then
8581
8582 -- The inherited context is attached to the enclosing compilation
8583 -- unit. This is either the main unit, or the declaration for the
8584 -- main unit (in case the instantiation appears within the package
8585 -- declaration and the main unit is its body).
8586
8587 Current_Unit := Parent (Inst);
8588 while Present (Current_Unit)
8589 and then Nkind (Current_Unit) /= N_Compilation_Unit
8590 loop
8591 Current_Unit := Parent (Current_Unit);
8592 end loop;
8593
8594 Current_Context := Context_Items (Current_Unit);
8595
8596 Item := First (Context_Items (Parent (Gen_Decl)));
8597 while Present (Item) loop
8598 if Nkind (Item) = N_With_Clause then
8599 Lib_Unit := Library_Unit (Item);
8600
8601 -- Take care to prevent direct cyclic with's
8602
8603 if Lib_Unit /= Current_Unit then
8604
8605 -- Do not add a unit if it is already in the context
8606
8607 Clause := First (Current_Context);
8608 OK := True;
8609 while Present (Clause) loop
8610 if Nkind (Clause) = N_With_Clause and then
8611 Library_Unit (Clause) = Lib_Unit
8612 then
8613 OK := False;
8614 exit;
8615 end if;
8616
8617 Next (Clause);
8618 end loop;
8619
8620 if OK then
8621 New_I := New_Copy (Item);
8622 Set_Implicit_With (New_I, True);
8623 Set_Implicit_With_From_Instantiation (New_I, True);
8624 Append (New_I, Current_Context);
8625 end if;
8626 end if;
8627 end if;
8628
8629 Next (Item);
8630 end loop;
8631 end if;
8632 end Inherit_Context;
8633
8634 ----------------
8635 -- Initialize --
8636 ----------------
8637
8638 procedure Initialize is
8639 begin
8640 Generic_Renamings.Init;
8641 Instance_Envs.Init;
8642 Generic_Flags.Init;
8643 Generic_Renamings_HTable.Reset;
8644 Circularity_Detected := False;
8645 Exchanged_Views := No_Elist;
8646 Hidden_Entities := No_Elist;
8647 end Initialize;
8648
8649 -------------------------------------
8650 -- Insert_Freeze_Node_For_Instance --
8651 -------------------------------------
8652
8653 procedure Insert_Freeze_Node_For_Instance
8654 (N : Node_Id;
8655 F_Node : Node_Id)
8656 is
8657 Decl : Node_Id;
8658 Decls : List_Id;
8659 Inst : Entity_Id;
8660 Par_N : Node_Id;
8661
8662 function Enclosing_Body (N : Node_Id) return Node_Id;
8663 -- Find enclosing package or subprogram body, if any. Freeze node may
8664 -- be placed at end of current declarative list if previous instance
8665 -- and current one have different enclosing bodies.
8666
8667 function Previous_Instance (Gen : Entity_Id) return Entity_Id;
8668 -- Find the local instance, if any, that declares the generic that is
8669 -- being instantiated. If present, the freeze node for this instance
8670 -- must follow the freeze node for the previous instance.
8671
8672 --------------------
8673 -- Enclosing_Body --
8674 --------------------
8675
8676 function Enclosing_Body (N : Node_Id) return Node_Id is
8677 P : Node_Id;
8678
8679 begin
8680 P := Parent (N);
8681 while Present (P)
8682 and then Nkind (Parent (P)) /= N_Compilation_Unit
8683 loop
8684 if Nkind_In (P, N_Package_Body, N_Subprogram_Body) then
8685 if Nkind (Parent (P)) = N_Subunit then
8686 return Corresponding_Stub (Parent (P));
8687 else
8688 return P;
8689 end if;
8690 end if;
8691
8692 P := True_Parent (P);
8693 end loop;
8694
8695 return Empty;
8696 end Enclosing_Body;
8697
8698 -----------------------
8699 -- Previous_Instance --
8700 -----------------------
8701
8702 function Previous_Instance (Gen : Entity_Id) return Entity_Id is
8703 S : Entity_Id;
8704
8705 begin
8706 S := Scope (Gen);
8707 while Present (S) and then S /= Standard_Standard loop
8708 if Is_Generic_Instance (S)
8709 and then In_Same_Source_Unit (S, N)
8710 then
8711 return S;
8712 end if;
8713
8714 S := Scope (S);
8715 end loop;
8716
8717 return Empty;
8718 end Previous_Instance;
8719
8720 -- Start of processing for Insert_Freeze_Node_For_Instance
8721
8722 begin
8723 if not Is_List_Member (F_Node) then
8724 Decl := N;
8725 Decls := List_Containing (N);
8726 Inst := Entity (F_Node);
8727 Par_N := Parent (Decls);
8728
8729 -- When processing a subprogram instantiation, utilize the actual
8730 -- subprogram instantiation rather than its package wrapper as it
8731 -- carries all the context information.
8732
8733 if Is_Wrapper_Package (Inst) then
8734 Inst := Related_Instance (Inst);
8735 end if;
8736
8737 -- If this is a package instance, check whether the generic is
8738 -- declared in a previous instance and the current instance is
8739 -- not within the previous one.
8740
8741 if Present (Generic_Parent (Parent (Inst)))
8742 and then Is_In_Main_Unit (N)
8743 then
8744 declare
8745 Enclosing_N : constant Node_Id := Enclosing_Body (N);
8746 Par_I : constant Entity_Id :=
8747 Previous_Instance
8748 (Generic_Parent (Parent (Inst)));
8749 Scop : Entity_Id;
8750
8751 begin
8752 if Present (Par_I)
8753 and then Earlier (N, Freeze_Node (Par_I))
8754 then
8755 Scop := Scope (Inst);
8756
8757 -- If the current instance is within the one that contains
8758 -- the generic, the freeze node for the current one must
8759 -- appear in the current declarative part. Ditto, if the
8760 -- current instance is within another package instance or
8761 -- within a body that does not enclose the current instance.
8762 -- In these three cases the freeze node of the previous
8763 -- instance is not relevant.
8764
8765 while Present (Scop) and then Scop /= Standard_Standard loop
8766 exit when Scop = Par_I
8767 or else
8768 (Is_Generic_Instance (Scop)
8769 and then Scope_Depth (Scop) > Scope_Depth (Par_I));
8770 Scop := Scope (Scop);
8771 end loop;
8772
8773 -- Previous instance encloses current instance
8774
8775 if Scop = Par_I then
8776 null;
8777
8778 -- If the next node is a source body we must freeze in
8779 -- the current scope as well.
8780
8781 elsif Present (Next (N))
8782 and then Nkind_In (Next (N), N_Subprogram_Body,
8783 N_Package_Body)
8784 and then Comes_From_Source (Next (N))
8785 then
8786 null;
8787
8788 -- Current instance is within an unrelated instance
8789
8790 elsif Is_Generic_Instance (Scop) then
8791 null;
8792
8793 -- Current instance is within an unrelated body
8794
8795 elsif Present (Enclosing_N)
8796 and then Enclosing_N /= Enclosing_Body (Par_I)
8797 then
8798 null;
8799
8800 else
8801 Insert_After (Freeze_Node (Par_I), F_Node);
8802 return;
8803 end if;
8804 end if;
8805 end;
8806 end if;
8807
8808 -- When the instantiation occurs in a package declaration, append the
8809 -- freeze node to the private declarations (if any).
8810
8811 if Nkind (Par_N) = N_Package_Specification
8812 and then Decls = Visible_Declarations (Par_N)
8813 and then Present (Private_Declarations (Par_N))
8814 and then not Is_Empty_List (Private_Declarations (Par_N))
8815 then
8816 Decls := Private_Declarations (Par_N);
8817 Decl := First (Decls);
8818 end if;
8819
8820 -- Determine the proper freeze point of a package instantiation. We
8821 -- adhere to the general rule of a package or subprogram body causing
8822 -- freezing of anything before it in the same declarative region. In
8823 -- this case, the proper freeze point of a package instantiation is
8824 -- before the first source body which follows, or before a stub. This
8825 -- ensures that entities coming from the instance are already frozen
8826 -- and usable in source bodies.
8827
8828 if Nkind (Par_N) /= N_Package_Declaration
8829 and then Ekind (Inst) = E_Package
8830 and then Is_Generic_Instance (Inst)
8831 and then
8832 not In_Same_Source_Unit (Generic_Parent (Parent (Inst)), Inst)
8833 then
8834 while Present (Decl) loop
8835 if (Nkind (Decl) in N_Unit_Body
8836 or else
8837 Nkind (Decl) in N_Body_Stub)
8838 and then Comes_From_Source (Decl)
8839 then
8840 Insert_Before (Decl, F_Node);
8841 return;
8842 end if;
8843
8844 Next (Decl);
8845 end loop;
8846 end if;
8847
8848 -- In a package declaration, or if no previous body, insert at end
8849 -- of list.
8850
8851 Set_Sloc (F_Node, Sloc (Last (Decls)));
8852 Insert_After (Last (Decls), F_Node);
8853 end if;
8854 end Insert_Freeze_Node_For_Instance;
8855
8856 ------------------
8857 -- Install_Body --
8858 ------------------
8859
8860 procedure Install_Body
8861 (Act_Body : Node_Id;
8862 N : Node_Id;
8863 Gen_Body : Node_Id;
8864 Gen_Decl : Node_Id)
8865 is
8866 Act_Id : constant Entity_Id := Corresponding_Spec (Act_Body);
8867 Act_Unit : constant Node_Id := Unit (Cunit (Get_Source_Unit (N)));
8868 Gen_Id : constant Entity_Id := Corresponding_Spec (Gen_Body);
8869 Par : constant Entity_Id := Scope (Gen_Id);
8870 Gen_Unit : constant Node_Id :=
8871 Unit (Cunit (Get_Source_Unit (Gen_Decl)));
8872 Orig_Body : Node_Id := Gen_Body;
8873 F_Node : Node_Id;
8874 Body_Unit : Node_Id;
8875
8876 Must_Delay : Boolean;
8877
8878 function In_Same_Enclosing_Subp return Boolean;
8879 -- Check whether instance and generic body are within same subprogram.
8880
8881 function True_Sloc (N : Node_Id) return Source_Ptr;
8882 -- If the instance is nested inside a generic unit, the Sloc of the
8883 -- instance indicates the place of the original definition, not the
8884 -- point of the current enclosing instance. Pending a better usage of
8885 -- Slocs to indicate instantiation places, we determine the place of
8886 -- origin of a node by finding the maximum sloc of any ancestor node.
8887 -- Why is this not equivalent to Top_Level_Location ???
8888
8889 ----------------------------
8890 -- In_Same_Enclosing_Subp --
8891 ----------------------------
8892
8893 function In_Same_Enclosing_Subp return Boolean is
8894 Scop : Entity_Id;
8895 Subp : Entity_Id;
8896
8897 begin
8898 Scop := Scope (Act_Id);
8899 while Scop /= Standard_Standard
8900 and then not Is_Overloadable (Scop)
8901 loop
8902 Scop := Scope (Scop);
8903 end loop;
8904
8905 if Scop = Standard_Standard then
8906 return False;
8907 else
8908 Subp := Scop;
8909 end if;
8910
8911 Scop := Scope (Gen_Id);
8912 while Scop /= Standard_Standard loop
8913 if Scop = Subp then
8914 return True;
8915 else
8916 Scop := Scope (Scop);
8917 end if;
8918 end loop;
8919
8920 return False;
8921 end In_Same_Enclosing_Subp;
8922
8923 ---------------
8924 -- True_Sloc --
8925 ---------------
8926
8927 function True_Sloc (N : Node_Id) return Source_Ptr is
8928 Res : Source_Ptr;
8929 N1 : Node_Id;
8930
8931 begin
8932 Res := Sloc (N);
8933 N1 := N;
8934 while Present (N1) and then N1 /= Act_Unit loop
8935 if Sloc (N1) > Res then
8936 Res := Sloc (N1);
8937 end if;
8938
8939 N1 := Parent (N1);
8940 end loop;
8941
8942 return Res;
8943 end True_Sloc;
8944
8945 -- Start of processing for Install_Body
8946
8947 begin
8948 -- Handle first the case of an instance with incomplete actual types.
8949 -- The instance body cannot be placed after the declaration because
8950 -- full views have not been seen yet. Any use of the non-limited views
8951 -- in the instance body requires the presence of a regular with_clause
8952 -- in the enclosing unit, and will fail if this with_clause is missing.
8953 -- We place the instance body at the beginning of the enclosing body,
8954 -- which is the unit being compiled. The freeze node for the instance
8955 -- is then placed after the instance body.
8956
8957 if not Is_Empty_Elmt_List (Incomplete_Actuals (Act_Id))
8958 and then Expander_Active
8959 and then Ekind (Scope (Act_Id)) = E_Package
8960 then
8961 declare
8962 Scop : constant Entity_Id := Scope (Act_Id);
8963 Body_Id : constant Node_Id :=
8964 Corresponding_Body (Unit_Declaration_Node (Scop));
8965
8966 begin
8967 Ensure_Freeze_Node (Act_Id);
8968 F_Node := Freeze_Node (Act_Id);
8969 if Present (Body_Id) then
8970 Set_Is_Frozen (Act_Id, False);
8971 Prepend (Act_Body, Declarations (Parent (Body_Id)));
8972 if Is_List_Member (F_Node) then
8973 Remove (F_Node);
8974 end if;
8975
8976 Insert_After (Act_Body, F_Node);
8977 end if;
8978 end;
8979 return;
8980 end if;
8981
8982 -- If the body is a subunit, the freeze point is the corresponding stub
8983 -- in the current compilation, not the subunit itself.
8984
8985 if Nkind (Parent (Gen_Body)) = N_Subunit then
8986 Orig_Body := Corresponding_Stub (Parent (Gen_Body));
8987 else
8988 Orig_Body := Gen_Body;
8989 end if;
8990
8991 Body_Unit := Unit (Cunit (Get_Source_Unit (Orig_Body)));
8992
8993 -- If the instantiation and the generic definition appear in the same
8994 -- package declaration, this is an early instantiation. If they appear
8995 -- in the same declarative part, it is an early instantiation only if
8996 -- the generic body appears textually later, and the generic body is
8997 -- also in the main unit.
8998
8999 -- If instance is nested within a subprogram, and the generic body
9000 -- is not, the instance is delayed because the enclosing body is. If
9001 -- instance and body are within the same scope, or the same subprogram
9002 -- body, indicate explicitly that the instance is delayed.
9003
9004 Must_Delay :=
9005 (Gen_Unit = Act_Unit
9006 and then (Nkind_In (Gen_Unit, N_Package_Declaration,
9007 N_Generic_Package_Declaration)
9008 or else (Gen_Unit = Body_Unit
9009 and then True_Sloc (N) < Sloc (Orig_Body)))
9010 and then Is_In_Main_Unit (Gen_Unit)
9011 and then (Scope (Act_Id) = Scope (Gen_Id)
9012 or else In_Same_Enclosing_Subp));
9013
9014 -- If this is an early instantiation, the freeze node is placed after
9015 -- the generic body. Otherwise, if the generic appears in an instance,
9016 -- we cannot freeze the current instance until the outer one is frozen.
9017 -- This is only relevant if the current instance is nested within some
9018 -- inner scope not itself within the outer instance. If this scope is
9019 -- a package body in the same declarative part as the outer instance,
9020 -- then that body needs to be frozen after the outer instance. Finally,
9021 -- if no delay is needed, we place the freeze node at the end of the
9022 -- current declarative part.
9023
9024 if Expander_Active then
9025 Ensure_Freeze_Node (Act_Id);
9026 F_Node := Freeze_Node (Act_Id);
9027
9028 if Must_Delay then
9029 Insert_After (Orig_Body, F_Node);
9030
9031 elsif Is_Generic_Instance (Par)
9032 and then Present (Freeze_Node (Par))
9033 and then Scope (Act_Id) /= Par
9034 then
9035 -- Freeze instance of inner generic after instance of enclosing
9036 -- generic.
9037
9038 if In_Same_Declarative_Part (Freeze_Node (Par), N) then
9039
9040 -- Handle the following case:
9041
9042 -- package Parent_Inst is new ...
9043 -- Parent_Inst []
9044
9045 -- procedure P ... -- this body freezes Parent_Inst
9046
9047 -- package Inst is new ...
9048
9049 -- In this particular scenario, the freeze node for Inst must
9050 -- be inserted in the same manner as that of Parent_Inst,
9051 -- before the next source body or at the end of the declarative
9052 -- list (body not available). If body P did not exist and
9053 -- Parent_Inst was frozen after Inst, either by a body
9054 -- following Inst or at the end of the declarative region,
9055 -- the freeze node for Inst must be inserted after that of
9056 -- Parent_Inst. This relation is established by comparing
9057 -- the Slocs of Parent_Inst freeze node and Inst.
9058
9059 if List_Containing (Get_Package_Instantiation_Node (Par)) =
9060 List_Containing (N)
9061 and then Sloc (Freeze_Node (Par)) < Sloc (N)
9062 then
9063 Insert_Freeze_Node_For_Instance (N, F_Node);
9064 else
9065 Insert_After (Freeze_Node (Par), F_Node);
9066 end if;
9067
9068 -- Freeze package enclosing instance of inner generic after
9069 -- instance of enclosing generic.
9070
9071 elsif Nkind_In (Parent (N), N_Package_Body, N_Subprogram_Body)
9072 and then In_Same_Declarative_Part (Freeze_Node (Par), Parent (N))
9073 then
9074 declare
9075 Enclosing : Entity_Id;
9076
9077 begin
9078 Enclosing := Corresponding_Spec (Parent (N));
9079
9080 if No (Enclosing) then
9081 Enclosing := Defining_Entity (Parent (N));
9082 end if;
9083
9084 Insert_Freeze_Node_For_Instance (N, F_Node);
9085 Ensure_Freeze_Node (Enclosing);
9086
9087 if not Is_List_Member (Freeze_Node (Enclosing)) then
9088
9089 -- The enclosing context is a subunit, insert the freeze
9090 -- node after the stub.
9091
9092 if Nkind (Parent (Parent (N))) = N_Subunit then
9093 Insert_Freeze_Node_For_Instance
9094 (Corresponding_Stub (Parent (Parent (N))),
9095 Freeze_Node (Enclosing));
9096
9097 -- The enclosing context is a package with a stub body
9098 -- which has already been replaced by the real body.
9099 -- Insert the freeze node after the actual body.
9100
9101 elsif Ekind (Enclosing) = E_Package
9102 and then Present (Body_Entity (Enclosing))
9103 and then Was_Originally_Stub
9104 (Parent (Body_Entity (Enclosing)))
9105 then
9106 Insert_Freeze_Node_For_Instance
9107 (Parent (Body_Entity (Enclosing)),
9108 Freeze_Node (Enclosing));
9109
9110 -- The parent instance has been frozen before the body of
9111 -- the enclosing package, insert the freeze node after
9112 -- the body.
9113
9114 elsif List_Containing (Freeze_Node (Par)) =
9115 List_Containing (Parent (N))
9116 and then Sloc (Freeze_Node (Par)) < Sloc (Parent (N))
9117 then
9118 Insert_Freeze_Node_For_Instance
9119 (Parent (N), Freeze_Node (Enclosing));
9120
9121 else
9122 Insert_After
9123 (Freeze_Node (Par), Freeze_Node (Enclosing));
9124 end if;
9125 end if;
9126 end;
9127
9128 else
9129 Insert_Freeze_Node_For_Instance (N, F_Node);
9130 end if;
9131
9132 else
9133 Insert_Freeze_Node_For_Instance (N, F_Node);
9134 end if;
9135 end if;
9136
9137 Set_Is_Frozen (Act_Id);
9138 Insert_Before (N, Act_Body);
9139 Mark_Rewrite_Insertion (Act_Body);
9140 end Install_Body;
9141
9142 -----------------------------
9143 -- Install_Formal_Packages --
9144 -----------------------------
9145
9146 procedure Install_Formal_Packages (Par : Entity_Id) is
9147 E : Entity_Id;
9148 Gen : Entity_Id;
9149 Gen_E : Entity_Id := Empty;
9150
9151 begin
9152 E := First_Entity (Par);
9153
9154 -- If we are installing an instance parent, locate the formal packages
9155 -- of its generic parent.
9156
9157 if Is_Generic_Instance (Par) then
9158 Gen := Generic_Parent (Package_Specification (Par));
9159 Gen_E := First_Entity (Gen);
9160 end if;
9161
9162 while Present (E) loop
9163 if Ekind (E) = E_Package
9164 and then Nkind (Parent (E)) = N_Package_Renaming_Declaration
9165 then
9166 -- If this is the renaming for the parent instance, done
9167
9168 if Renamed_Object (E) = Par then
9169 exit;
9170
9171 -- The visibility of a formal of an enclosing generic is already
9172 -- correct.
9173
9174 elsif Denotes_Formal_Package (E) then
9175 null;
9176
9177 elsif Present (Associated_Formal_Package (E)) then
9178 Check_Generic_Actuals (Renamed_Object (E), True);
9179 Set_Is_Hidden (E, False);
9180
9181 -- Find formal package in generic unit that corresponds to
9182 -- (instance of) formal package in instance.
9183
9184 while Present (Gen_E) and then Chars (Gen_E) /= Chars (E) loop
9185 Next_Entity (Gen_E);
9186 end loop;
9187
9188 if Present (Gen_E) then
9189 Map_Formal_Package_Entities (Gen_E, E);
9190 end if;
9191 end if;
9192 end if;
9193
9194 Next_Entity (E);
9195
9196 if Present (Gen_E) then
9197 Next_Entity (Gen_E);
9198 end if;
9199 end loop;
9200 end Install_Formal_Packages;
9201
9202 --------------------
9203 -- Install_Parent --
9204 --------------------
9205
9206 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False) is
9207 Ancestors : constant Elist_Id := New_Elmt_List;
9208 S : constant Entity_Id := Current_Scope;
9209 Inst_Par : Entity_Id;
9210 First_Par : Entity_Id;
9211 Inst_Node : Node_Id;
9212 Gen_Par : Entity_Id;
9213 First_Gen : Entity_Id;
9214 Elmt : Elmt_Id;
9215
9216 procedure Install_Noninstance_Specs (Par : Entity_Id);
9217 -- Install the scopes of noninstance parent units ending with Par
9218
9219 procedure Install_Spec (Par : Entity_Id);
9220 -- The child unit is within the declarative part of the parent, so the
9221 -- declarations within the parent are immediately visible.
9222
9223 -------------------------------
9224 -- Install_Noninstance_Specs --
9225 -------------------------------
9226
9227 procedure Install_Noninstance_Specs (Par : Entity_Id) is
9228 begin
9229 if Present (Par)
9230 and then Par /= Standard_Standard
9231 and then not In_Open_Scopes (Par)
9232 then
9233 Install_Noninstance_Specs (Scope (Par));
9234 Install_Spec (Par);
9235 end if;
9236 end Install_Noninstance_Specs;
9237
9238 ------------------
9239 -- Install_Spec --
9240 ------------------
9241
9242 procedure Install_Spec (Par : Entity_Id) is
9243 Spec : constant Node_Id := Package_Specification (Par);
9244
9245 begin
9246 -- If this parent of the child instance is a top-level unit,
9247 -- then record the unit and its visibility for later resetting in
9248 -- Remove_Parent. We exclude units that are generic instances, as we
9249 -- only want to record this information for the ultimate top-level
9250 -- noninstance parent (is that always correct???).
9251
9252 if Scope (Par) = Standard_Standard
9253 and then not Is_Generic_Instance (Par)
9254 then
9255 Parent_Unit_Visible := Is_Immediately_Visible (Par);
9256 Instance_Parent_Unit := Par;
9257 end if;
9258
9259 -- Open the parent scope and make it and its declarations visible.
9260 -- If this point is not within a body, then only the visible
9261 -- declarations should be made visible, and installation of the
9262 -- private declarations is deferred until the appropriate point
9263 -- within analysis of the spec being instantiated (see the handling
9264 -- of parent visibility in Analyze_Package_Specification). This is
9265 -- relaxed in the case where the parent unit is Ada.Tags, to avoid
9266 -- private view problems that occur when compiling instantiations of
9267 -- a generic child of that package (Generic_Dispatching_Constructor).
9268 -- If the instance freezes a tagged type, inlinings of operations
9269 -- from Ada.Tags may need the full view of type Tag. If inlining took
9270 -- proper account of establishing visibility of inlined subprograms'
9271 -- parents then it should be possible to remove this
9272 -- special check. ???
9273
9274 Push_Scope (Par);
9275 Set_Is_Immediately_Visible (Par);
9276 Install_Visible_Declarations (Par);
9277 Set_Use (Visible_Declarations (Spec));
9278
9279 if In_Body or else Is_RTU (Par, Ada_Tags) then
9280 Install_Private_Declarations (Par);
9281 Set_Use (Private_Declarations (Spec));
9282 end if;
9283 end Install_Spec;
9284
9285 -- Start of processing for Install_Parent
9286
9287 begin
9288 -- We need to install the parent instance to compile the instantiation
9289 -- of the child, but the child instance must appear in the current
9290 -- scope. Given that we cannot place the parent above the current scope
9291 -- in the scope stack, we duplicate the current scope and unstack both
9292 -- after the instantiation is complete.
9293
9294 -- If the parent is itself the instantiation of a child unit, we must
9295 -- also stack the instantiation of its parent, and so on. Each such
9296 -- ancestor is the prefix of the name in a prior instantiation.
9297
9298 -- If this is a nested instance, the parent unit itself resolves to
9299 -- a renaming of the parent instance, whose declaration we need.
9300
9301 -- Finally, the parent may be a generic (not an instance) when the
9302 -- child unit appears as a formal package.
9303
9304 Inst_Par := P;
9305
9306 if Present (Renamed_Entity (Inst_Par)) then
9307 Inst_Par := Renamed_Entity (Inst_Par);
9308 end if;
9309
9310 First_Par := Inst_Par;
9311
9312 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
9313
9314 First_Gen := Gen_Par;
9315
9316 while Present (Gen_Par) and then Is_Child_Unit (Gen_Par) loop
9317
9318 -- Load grandparent instance as well
9319
9320 Inst_Node := Get_Package_Instantiation_Node (Inst_Par);
9321
9322 if Nkind (Name (Inst_Node)) = N_Expanded_Name then
9323 Inst_Par := Entity (Prefix (Name (Inst_Node)));
9324
9325 if Present (Renamed_Entity (Inst_Par)) then
9326 Inst_Par := Renamed_Entity (Inst_Par);
9327 end if;
9328
9329 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
9330
9331 if Present (Gen_Par) then
9332 Prepend_Elmt (Inst_Par, Ancestors);
9333
9334 else
9335 -- Parent is not the name of an instantiation
9336
9337 Install_Noninstance_Specs (Inst_Par);
9338 exit;
9339 end if;
9340
9341 else
9342 -- Previous error
9343
9344 exit;
9345 end if;
9346 end loop;
9347
9348 if Present (First_Gen) then
9349 Append_Elmt (First_Par, Ancestors);
9350 else
9351 Install_Noninstance_Specs (First_Par);
9352 end if;
9353
9354 if not Is_Empty_Elmt_List (Ancestors) then
9355 Elmt := First_Elmt (Ancestors);
9356 while Present (Elmt) loop
9357 Install_Spec (Node (Elmt));
9358 Install_Formal_Packages (Node (Elmt));
9359 Next_Elmt (Elmt);
9360 end loop;
9361 end if;
9362
9363 if not In_Body then
9364 Push_Scope (S);
9365 end if;
9366 end Install_Parent;
9367
9368 -------------------------------
9369 -- Install_Hidden_Primitives --
9370 -------------------------------
9371
9372 procedure Install_Hidden_Primitives
9373 (Prims_List : in out Elist_Id;
9374 Gen_T : Entity_Id;
9375 Act_T : Entity_Id)
9376 is
9377 Elmt : Elmt_Id;
9378 List : Elist_Id := No_Elist;
9379 Prim_G_Elmt : Elmt_Id;
9380 Prim_A_Elmt : Elmt_Id;
9381 Prim_G : Node_Id;
9382 Prim_A : Node_Id;
9383
9384 begin
9385 -- No action needed in case of serious errors because we cannot trust
9386 -- in the order of primitives
9387
9388 if Serious_Errors_Detected > 0 then
9389 return;
9390
9391 -- No action possible if we don't have available the list of primitive
9392 -- operations
9393
9394 elsif No (Gen_T)
9395 or else not Is_Record_Type (Gen_T)
9396 or else not Is_Tagged_Type (Gen_T)
9397 or else not Is_Record_Type (Act_T)
9398 or else not Is_Tagged_Type (Act_T)
9399 then
9400 return;
9401
9402 -- There is no need to handle interface types since their primitives
9403 -- cannot be hidden
9404
9405 elsif Is_Interface (Gen_T) then
9406 return;
9407 end if;
9408
9409 Prim_G_Elmt := First_Elmt (Primitive_Operations (Gen_T));
9410
9411 if not Is_Class_Wide_Type (Act_T) then
9412 Prim_A_Elmt := First_Elmt (Primitive_Operations (Act_T));
9413 else
9414 Prim_A_Elmt := First_Elmt (Primitive_Operations (Root_Type (Act_T)));
9415 end if;
9416
9417 loop
9418 -- Skip predefined primitives in the generic formal
9419
9420 while Present (Prim_G_Elmt)
9421 and then Is_Predefined_Dispatching_Operation (Node (Prim_G_Elmt))
9422 loop
9423 Next_Elmt (Prim_G_Elmt);
9424 end loop;
9425
9426 -- Skip predefined primitives in the generic actual
9427
9428 while Present (Prim_A_Elmt)
9429 and then Is_Predefined_Dispatching_Operation (Node (Prim_A_Elmt))
9430 loop
9431 Next_Elmt (Prim_A_Elmt);
9432 end loop;
9433
9434 exit when No (Prim_G_Elmt) or else No (Prim_A_Elmt);
9435
9436 Prim_G := Node (Prim_G_Elmt);
9437 Prim_A := Node (Prim_A_Elmt);
9438
9439 -- There is no need to handle interface primitives because their
9440 -- primitives are not hidden
9441
9442 exit when Present (Interface_Alias (Prim_G));
9443
9444 -- Here we install one hidden primitive
9445
9446 if Chars (Prim_G) /= Chars (Prim_A)
9447 and then Has_Suffix (Prim_A, 'P')
9448 and then Remove_Suffix (Prim_A, 'P') = Chars (Prim_G)
9449 then
9450 Set_Chars (Prim_A, Chars (Prim_G));
9451 Append_New_Elmt (Prim_A, To => List);
9452 end if;
9453
9454 Next_Elmt (Prim_A_Elmt);
9455 Next_Elmt (Prim_G_Elmt);
9456 end loop;
9457
9458 -- Append the elements to the list of temporarily visible primitives
9459 -- avoiding duplicates.
9460
9461 if Present (List) then
9462 if No (Prims_List) then
9463 Prims_List := New_Elmt_List;
9464 end if;
9465
9466 Elmt := First_Elmt (List);
9467 while Present (Elmt) loop
9468 Append_Unique_Elmt (Node (Elmt), Prims_List);
9469 Next_Elmt (Elmt);
9470 end loop;
9471 end if;
9472 end Install_Hidden_Primitives;
9473
9474 -------------------------------
9475 -- Restore_Hidden_Primitives --
9476 -------------------------------
9477
9478 procedure Restore_Hidden_Primitives (Prims_List : in out Elist_Id) is
9479 Prim_Elmt : Elmt_Id;
9480 Prim : Node_Id;
9481
9482 begin
9483 if Prims_List /= No_Elist then
9484 Prim_Elmt := First_Elmt (Prims_List);
9485 while Present (Prim_Elmt) loop
9486 Prim := Node (Prim_Elmt);
9487 Set_Chars (Prim, Add_Suffix (Prim, 'P'));
9488 Next_Elmt (Prim_Elmt);
9489 end loop;
9490
9491 Prims_List := No_Elist;
9492 end if;
9493 end Restore_Hidden_Primitives;
9494
9495 --------------------------------
9496 -- Instantiate_Formal_Package --
9497 --------------------------------
9498
9499 function Instantiate_Formal_Package
9500 (Formal : Node_Id;
9501 Actual : Node_Id;
9502 Analyzed_Formal : Node_Id) return List_Id
9503 is
9504 Loc : constant Source_Ptr := Sloc (Actual);
9505 Actual_Pack : Entity_Id;
9506 Formal_Pack : Entity_Id;
9507 Gen_Parent : Entity_Id;
9508 Decls : List_Id;
9509 Nod : Node_Id;
9510 Parent_Spec : Node_Id;
9511
9512 procedure Find_Matching_Actual
9513 (F : Node_Id;
9514 Act : in out Entity_Id);
9515 -- We need to associate each formal entity in the formal package with
9516 -- the corresponding entity in the actual package. The actual package
9517 -- has been analyzed and possibly expanded, and as a result there is
9518 -- no one-to-one correspondence between the two lists (for example,
9519 -- the actual may include subtypes, itypes, and inherited primitive
9520 -- operations, interspersed among the renaming declarations for the
9521 -- actuals). We retrieve the corresponding actual by name because each
9522 -- actual has the same name as the formal, and they do appear in the
9523 -- same order.
9524
9525 function Get_Formal_Entity (N : Node_Id) return Entity_Id;
9526 -- Retrieve entity of defining entity of generic formal parameter.
9527 -- Only the declarations of formals need to be considered when
9528 -- linking them to actuals, but the declarative list may include
9529 -- internal entities generated during analysis, and those are ignored.
9530
9531 procedure Match_Formal_Entity
9532 (Formal_Node : Node_Id;
9533 Formal_Ent : Entity_Id;
9534 Actual_Ent : Entity_Id);
9535 -- Associates the formal entity with the actual. In the case where
9536 -- Formal_Ent is a formal package, this procedure iterates through all
9537 -- of its formals and enters associations between the actuals occurring
9538 -- in the formal package's corresponding actual package (given by
9539 -- Actual_Ent) and the formal package's formal parameters. This
9540 -- procedure recurses if any of the parameters is itself a package.
9541
9542 function Is_Instance_Of
9543 (Act_Spec : Entity_Id;
9544 Gen_Anc : Entity_Id) return Boolean;
9545 -- The actual can be an instantiation of a generic within another
9546 -- instance, in which case there is no direct link from it to the
9547 -- original generic ancestor. In that case, we recognize that the
9548 -- ultimate ancestor is the same by examining names and scopes.
9549
9550 procedure Process_Nested_Formal (Formal : Entity_Id);
9551 -- If the current formal is declared with a box, its own formals are
9552 -- visible in the instance, as they were in the generic, and their
9553 -- Hidden flag must be reset. If some of these formals are themselves
9554 -- packages declared with a box, the processing must be recursive.
9555
9556 --------------------------
9557 -- Find_Matching_Actual --
9558 --------------------------
9559
9560 procedure Find_Matching_Actual
9561 (F : Node_Id;
9562 Act : in out Entity_Id)
9563 is
9564 Formal_Ent : Entity_Id;
9565
9566 begin
9567 case Nkind (Original_Node (F)) is
9568 when N_Formal_Object_Declaration |
9569 N_Formal_Type_Declaration =>
9570 Formal_Ent := Defining_Identifier (F);
9571
9572 while Chars (Act) /= Chars (Formal_Ent) loop
9573 Next_Entity (Act);
9574 end loop;
9575
9576 when N_Formal_Subprogram_Declaration |
9577 N_Formal_Package_Declaration |
9578 N_Package_Declaration |
9579 N_Generic_Package_Declaration =>
9580 Formal_Ent := Defining_Entity (F);
9581
9582 while Chars (Act) /= Chars (Formal_Ent) loop
9583 Next_Entity (Act);
9584 end loop;
9585
9586 when others =>
9587 raise Program_Error;
9588 end case;
9589 end Find_Matching_Actual;
9590
9591 -------------------------
9592 -- Match_Formal_Entity --
9593 -------------------------
9594
9595 procedure Match_Formal_Entity
9596 (Formal_Node : Node_Id;
9597 Formal_Ent : Entity_Id;
9598 Actual_Ent : Entity_Id)
9599 is
9600 Act_Pkg : Entity_Id;
9601
9602 begin
9603 Set_Instance_Of (Formal_Ent, Actual_Ent);
9604
9605 if Ekind (Actual_Ent) = E_Package then
9606
9607 -- Record associations for each parameter
9608
9609 Act_Pkg := Actual_Ent;
9610
9611 declare
9612 A_Ent : Entity_Id := First_Entity (Act_Pkg);
9613 F_Ent : Entity_Id;
9614 F_Node : Node_Id;
9615
9616 Gen_Decl : Node_Id;
9617 Formals : List_Id;
9618 Actual : Entity_Id;
9619
9620 begin
9621 -- Retrieve the actual given in the formal package declaration
9622
9623 Actual := Entity (Name (Original_Node (Formal_Node)));
9624
9625 -- The actual in the formal package declaration may be a
9626 -- renamed generic package, in which case we want to retrieve
9627 -- the original generic in order to traverse its formal part.
9628
9629 if Present (Renamed_Entity (Actual)) then
9630 Gen_Decl := Unit_Declaration_Node (Renamed_Entity (Actual));
9631 else
9632 Gen_Decl := Unit_Declaration_Node (Actual);
9633 end if;
9634
9635 Formals := Generic_Formal_Declarations (Gen_Decl);
9636
9637 if Present (Formals) then
9638 F_Node := First_Non_Pragma (Formals);
9639 else
9640 F_Node := Empty;
9641 end if;
9642
9643 while Present (A_Ent)
9644 and then Present (F_Node)
9645 and then A_Ent /= First_Private_Entity (Act_Pkg)
9646 loop
9647 F_Ent := Get_Formal_Entity (F_Node);
9648
9649 if Present (F_Ent) then
9650
9651 -- This is a formal of the original package. Record
9652 -- association and recurse.
9653
9654 Find_Matching_Actual (F_Node, A_Ent);
9655 Match_Formal_Entity (F_Node, F_Ent, A_Ent);
9656 Next_Entity (A_Ent);
9657 end if;
9658
9659 Next_Non_Pragma (F_Node);
9660 end loop;
9661 end;
9662 end if;
9663 end Match_Formal_Entity;
9664
9665 -----------------------
9666 -- Get_Formal_Entity --
9667 -----------------------
9668
9669 function Get_Formal_Entity (N : Node_Id) return Entity_Id is
9670 Kind : constant Node_Kind := Nkind (Original_Node (N));
9671 begin
9672 case Kind is
9673 when N_Formal_Object_Declaration =>
9674 return Defining_Identifier (N);
9675
9676 when N_Formal_Type_Declaration =>
9677 return Defining_Identifier (N);
9678
9679 when N_Formal_Subprogram_Declaration =>
9680 return Defining_Unit_Name (Specification (N));
9681
9682 when N_Formal_Package_Declaration =>
9683 return Defining_Identifier (Original_Node (N));
9684
9685 when N_Generic_Package_Declaration =>
9686 return Defining_Identifier (Original_Node (N));
9687
9688 -- All other declarations are introduced by semantic analysis and
9689 -- have no match in the actual.
9690
9691 when others =>
9692 return Empty;
9693 end case;
9694 end Get_Formal_Entity;
9695
9696 --------------------
9697 -- Is_Instance_Of --
9698 --------------------
9699
9700 function Is_Instance_Of
9701 (Act_Spec : Entity_Id;
9702 Gen_Anc : Entity_Id) return Boolean
9703 is
9704 Gen_Par : constant Entity_Id := Generic_Parent (Act_Spec);
9705
9706 begin
9707 if No (Gen_Par) then
9708 return False;
9709
9710 -- Simplest case: the generic parent of the actual is the formal
9711
9712 elsif Gen_Par = Gen_Anc then
9713 return True;
9714
9715 elsif Chars (Gen_Par) /= Chars (Gen_Anc) then
9716 return False;
9717
9718 -- The actual may be obtained through several instantiations. Its
9719 -- scope must itself be an instance of a generic declared in the
9720 -- same scope as the formal. Any other case is detected above.
9721
9722 elsif not Is_Generic_Instance (Scope (Gen_Par)) then
9723 return False;
9724
9725 else
9726 return Generic_Parent (Parent (Scope (Gen_Par))) = Scope (Gen_Anc);
9727 end if;
9728 end Is_Instance_Of;
9729
9730 ---------------------------
9731 -- Process_Nested_Formal --
9732 ---------------------------
9733
9734 procedure Process_Nested_Formal (Formal : Entity_Id) is
9735 Ent : Entity_Id;
9736
9737 begin
9738 if Present (Associated_Formal_Package (Formal))
9739 and then Box_Present (Parent (Associated_Formal_Package (Formal)))
9740 then
9741 Ent := First_Entity (Formal);
9742 while Present (Ent) loop
9743 Set_Is_Hidden (Ent, False);
9744 Set_Is_Visible_Formal (Ent);
9745 Set_Is_Potentially_Use_Visible
9746 (Ent, Is_Potentially_Use_Visible (Formal));
9747
9748 if Ekind (Ent) = E_Package then
9749 exit when Renamed_Entity (Ent) = Renamed_Entity (Formal);
9750 Process_Nested_Formal (Ent);
9751 end if;
9752
9753 Next_Entity (Ent);
9754 end loop;
9755 end if;
9756 end Process_Nested_Formal;
9757
9758 -- Start of processing for Instantiate_Formal_Package
9759
9760 begin
9761 Analyze (Actual);
9762
9763 if not Is_Entity_Name (Actual)
9764 or else Ekind (Entity (Actual)) /= E_Package
9765 then
9766 Error_Msg_N
9767 ("expect package instance to instantiate formal", Actual);
9768 Abandon_Instantiation (Actual);
9769 raise Program_Error;
9770
9771 else
9772 Actual_Pack := Entity (Actual);
9773 Set_Is_Instantiated (Actual_Pack);
9774
9775 -- The actual may be a renamed package, or an outer generic formal
9776 -- package whose instantiation is converted into a renaming.
9777
9778 if Present (Renamed_Object (Actual_Pack)) then
9779 Actual_Pack := Renamed_Object (Actual_Pack);
9780 end if;
9781
9782 if Nkind (Analyzed_Formal) = N_Formal_Package_Declaration then
9783 Gen_Parent := Get_Instance_Of (Entity (Name (Analyzed_Formal)));
9784 Formal_Pack := Defining_Identifier (Analyzed_Formal);
9785 else
9786 Gen_Parent :=
9787 Generic_Parent (Specification (Analyzed_Formal));
9788 Formal_Pack :=
9789 Defining_Unit_Name (Specification (Analyzed_Formal));
9790 end if;
9791
9792 if Nkind (Parent (Actual_Pack)) = N_Defining_Program_Unit_Name then
9793 Parent_Spec := Package_Specification (Actual_Pack);
9794 else
9795 Parent_Spec := Parent (Actual_Pack);
9796 end if;
9797
9798 if Gen_Parent = Any_Id then
9799 Error_Msg_N
9800 ("previous error in declaration of formal package", Actual);
9801 Abandon_Instantiation (Actual);
9802
9803 elsif
9804 Is_Instance_Of (Parent_Spec, Get_Instance_Of (Gen_Parent))
9805 then
9806 null;
9807
9808 else
9809 Error_Msg_NE
9810 ("actual parameter must be instance of&", Actual, Gen_Parent);
9811 Abandon_Instantiation (Actual);
9812 end if;
9813
9814 Set_Instance_Of (Defining_Identifier (Formal), Actual_Pack);
9815 Map_Formal_Package_Entities (Formal_Pack, Actual_Pack);
9816
9817 Nod :=
9818 Make_Package_Renaming_Declaration (Loc,
9819 Defining_Unit_Name => New_Copy (Defining_Identifier (Formal)),
9820 Name => New_Occurrence_Of (Actual_Pack, Loc));
9821
9822 Set_Associated_Formal_Package
9823 (Defining_Unit_Name (Nod), Defining_Identifier (Formal));
9824 Decls := New_List (Nod);
9825
9826 -- If the formal F has a box, then the generic declarations are
9827 -- visible in the generic G. In an instance of G, the corresponding
9828 -- entities in the actual for F (which are the actuals for the
9829 -- instantiation of the generic that F denotes) must also be made
9830 -- visible for analysis of the current instance. On exit from the
9831 -- current instance, those entities are made private again. If the
9832 -- actual is currently in use, these entities are also use-visible.
9833
9834 -- The loop through the actual entities also steps through the formal
9835 -- entities and enters associations from formals to actuals into the
9836 -- renaming map. This is necessary to properly handle checking of
9837 -- actual parameter associations for later formals that depend on
9838 -- actuals declared in the formal package.
9839
9840 -- In Ada 2005, partial parameterization requires that we make
9841 -- visible the actuals corresponding to formals that were defaulted
9842 -- in the formal package. There formals are identified because they
9843 -- remain formal generics within the formal package, rather than
9844 -- being renamings of the actuals supplied.
9845
9846 declare
9847 Gen_Decl : constant Node_Id :=
9848 Unit_Declaration_Node (Gen_Parent);
9849 Formals : constant List_Id :=
9850 Generic_Formal_Declarations (Gen_Decl);
9851
9852 Actual_Ent : Entity_Id;
9853 Actual_Of_Formal : Node_Id;
9854 Formal_Node : Node_Id;
9855 Formal_Ent : Entity_Id;
9856
9857 begin
9858 if Present (Formals) then
9859 Formal_Node := First_Non_Pragma (Formals);
9860 else
9861 Formal_Node := Empty;
9862 end if;
9863
9864 Actual_Ent := First_Entity (Actual_Pack);
9865 Actual_Of_Formal :=
9866 First (Visible_Declarations (Specification (Analyzed_Formal)));
9867 while Present (Actual_Ent)
9868 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
9869 loop
9870 if Present (Formal_Node) then
9871 Formal_Ent := Get_Formal_Entity (Formal_Node);
9872
9873 if Present (Formal_Ent) then
9874 Find_Matching_Actual (Formal_Node, Actual_Ent);
9875 Match_Formal_Entity (Formal_Node, Formal_Ent, Actual_Ent);
9876
9877 -- We iterate at the same time over the actuals of the
9878 -- local package created for the formal, to determine
9879 -- which one of the formals of the original generic were
9880 -- defaulted in the formal. The corresponding actual
9881 -- entities are visible in the enclosing instance.
9882
9883 if Box_Present (Formal)
9884 or else
9885 (Present (Actual_Of_Formal)
9886 and then
9887 Is_Generic_Formal
9888 (Get_Formal_Entity (Actual_Of_Formal)))
9889 then
9890 Set_Is_Hidden (Actual_Ent, False);
9891 Set_Is_Visible_Formal (Actual_Ent);
9892 Set_Is_Potentially_Use_Visible
9893 (Actual_Ent, In_Use (Actual_Pack));
9894
9895 if Ekind (Actual_Ent) = E_Package then
9896 Process_Nested_Formal (Actual_Ent);
9897 end if;
9898
9899 else
9900 Set_Is_Hidden (Actual_Ent);
9901 Set_Is_Potentially_Use_Visible (Actual_Ent, False);
9902 end if;
9903 end if;
9904
9905 Next_Non_Pragma (Formal_Node);
9906 Next (Actual_Of_Formal);
9907
9908 else
9909 -- No further formals to match, but the generic part may
9910 -- contain inherited operation that are not hidden in the
9911 -- enclosing instance.
9912
9913 Next_Entity (Actual_Ent);
9914 end if;
9915 end loop;
9916
9917 -- Inherited subprograms generated by formal derived types are
9918 -- also visible if the types are.
9919
9920 Actual_Ent := First_Entity (Actual_Pack);
9921 while Present (Actual_Ent)
9922 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
9923 loop
9924 if Is_Overloadable (Actual_Ent)
9925 and then
9926 Nkind (Parent (Actual_Ent)) = N_Subtype_Declaration
9927 and then
9928 not Is_Hidden (Defining_Identifier (Parent (Actual_Ent)))
9929 then
9930 Set_Is_Hidden (Actual_Ent, False);
9931 Set_Is_Potentially_Use_Visible
9932 (Actual_Ent, In_Use (Actual_Pack));
9933 end if;
9934
9935 Next_Entity (Actual_Ent);
9936 end loop;
9937 end;
9938
9939 -- If the formal is not declared with a box, reanalyze it as an
9940 -- abbreviated instantiation, to verify the matching rules of 12.7.
9941 -- The actual checks are performed after the generic associations
9942 -- have been analyzed, to guarantee the same visibility for this
9943 -- instantiation and for the actuals.
9944
9945 -- In Ada 2005, the generic associations for the formal can include
9946 -- defaulted parameters. These are ignored during check. This
9947 -- internal instantiation is removed from the tree after conformance
9948 -- checking, because it contains formal declarations for those
9949 -- defaulted parameters, and those should not reach the back-end.
9950
9951 if not Box_Present (Formal) then
9952 declare
9953 I_Pack : constant Entity_Id :=
9954 Make_Temporary (Sloc (Actual), 'P');
9955
9956 begin
9957 Set_Is_Internal (I_Pack);
9958
9959 Append_To (Decls,
9960 Make_Package_Instantiation (Sloc (Actual),
9961 Defining_Unit_Name => I_Pack,
9962 Name =>
9963 New_Occurrence_Of
9964 (Get_Instance_Of (Gen_Parent), Sloc (Actual)),
9965 Generic_Associations => Generic_Associations (Formal)));
9966 end;
9967 end if;
9968
9969 return Decls;
9970 end if;
9971 end Instantiate_Formal_Package;
9972
9973 -----------------------------------
9974 -- Instantiate_Formal_Subprogram --
9975 -----------------------------------
9976
9977 function Instantiate_Formal_Subprogram
9978 (Formal : Node_Id;
9979 Actual : Node_Id;
9980 Analyzed_Formal : Node_Id) return Node_Id
9981 is
9982 Analyzed_S : constant Entity_Id :=
9983 Defining_Unit_Name (Specification (Analyzed_Formal));
9984 Formal_Sub : constant Entity_Id :=
9985 Defining_Unit_Name (Specification (Formal));
9986
9987 function From_Parent_Scope (Subp : Entity_Id) return Boolean;
9988 -- If the generic is a child unit, the parent has been installed on the
9989 -- scope stack, but a default subprogram cannot resolve to something
9990 -- on the parent because that parent is not really part of the visible
9991 -- context (it is there to resolve explicit local entities). If the
9992 -- default has resolved in this way, we remove the entity from immediate
9993 -- visibility and analyze the node again to emit an error message or
9994 -- find another visible candidate.
9995
9996 procedure Valid_Actual_Subprogram (Act : Node_Id);
9997 -- Perform legality check and raise exception on failure
9998
9999 -----------------------
10000 -- From_Parent_Scope --
10001 -----------------------
10002
10003 function From_Parent_Scope (Subp : Entity_Id) return Boolean is
10004 Gen_Scope : Node_Id;
10005
10006 begin
10007 Gen_Scope := Scope (Analyzed_S);
10008 while Present (Gen_Scope) and then Is_Child_Unit (Gen_Scope) loop
10009 if Scope (Subp) = Scope (Gen_Scope) then
10010 return True;
10011 end if;
10012
10013 Gen_Scope := Scope (Gen_Scope);
10014 end loop;
10015
10016 return False;
10017 end From_Parent_Scope;
10018
10019 -----------------------------
10020 -- Valid_Actual_Subprogram --
10021 -----------------------------
10022
10023 procedure Valid_Actual_Subprogram (Act : Node_Id) is
10024 Act_E : Entity_Id;
10025
10026 begin
10027 if Is_Entity_Name (Act) then
10028 Act_E := Entity (Act);
10029
10030 elsif Nkind (Act) = N_Selected_Component
10031 and then Is_Entity_Name (Selector_Name (Act))
10032 then
10033 Act_E := Entity (Selector_Name (Act));
10034
10035 else
10036 Act_E := Empty;
10037 end if;
10038
10039 if (Present (Act_E) and then Is_Overloadable (Act_E))
10040 or else Nkind_In (Act, N_Attribute_Reference,
10041 N_Indexed_Component,
10042 N_Character_Literal,
10043 N_Explicit_Dereference)
10044 then
10045 return;
10046 end if;
10047
10048 Error_Msg_NE
10049 ("expect subprogram or entry name in instantiation of &",
10050 Instantiation_Node, Formal_Sub);
10051 Abandon_Instantiation (Instantiation_Node);
10052 end Valid_Actual_Subprogram;
10053
10054 -- Local variables
10055
10056 Decl_Node : Node_Id;
10057 Loc : Source_Ptr;
10058 Nam : Node_Id;
10059 New_Spec : Node_Id;
10060 New_Subp : Entity_Id;
10061
10062 -- Start of processing for Instantiate_Formal_Subprogram
10063
10064 begin
10065 New_Spec := New_Copy_Tree (Specification (Formal));
10066
10067 -- The tree copy has created the proper instantiation sloc for the
10068 -- new specification. Use this location for all other constructed
10069 -- declarations.
10070
10071 Loc := Sloc (Defining_Unit_Name (New_Spec));
10072
10073 -- Create new entity for the actual (New_Copy_Tree does not), and
10074 -- indicate that it is an actual.
10075
10076 New_Subp := Make_Defining_Identifier (Loc, Chars (Formal_Sub));
10077 Set_Ekind (New_Subp, Ekind (Analyzed_S));
10078 Set_Is_Generic_Actual_Subprogram (New_Subp);
10079 Set_Defining_Unit_Name (New_Spec, New_Subp);
10080
10081 -- Create new entities for the each of the formals in the specification
10082 -- of the renaming declaration built for the actual.
10083
10084 if Present (Parameter_Specifications (New_Spec)) then
10085 declare
10086 F : Node_Id;
10087 F_Id : Entity_Id;
10088
10089 begin
10090 F := First (Parameter_Specifications (New_Spec));
10091 while Present (F) loop
10092 F_Id := Defining_Identifier (F);
10093
10094 Set_Defining_Identifier (F,
10095 Make_Defining_Identifier (Sloc (F_Id), Chars (F_Id)));
10096 Next (F);
10097 end loop;
10098 end;
10099 end if;
10100
10101 -- Find entity of actual. If the actual is an attribute reference, it
10102 -- cannot be resolved here (its formal is missing) but is handled
10103 -- instead in Attribute_Renaming. If the actual is overloaded, it is
10104 -- fully resolved subsequently, when the renaming declaration for the
10105 -- formal is analyzed. If it is an explicit dereference, resolve the
10106 -- prefix but not the actual itself, to prevent interpretation as call.
10107
10108 if Present (Actual) then
10109 Loc := Sloc (Actual);
10110 Set_Sloc (New_Spec, Loc);
10111
10112 if Nkind (Actual) = N_Operator_Symbol then
10113 Find_Direct_Name (Actual);
10114
10115 elsif Nkind (Actual) = N_Explicit_Dereference then
10116 Analyze (Prefix (Actual));
10117
10118 elsif Nkind (Actual) /= N_Attribute_Reference then
10119 Analyze (Actual);
10120 end if;
10121
10122 Valid_Actual_Subprogram (Actual);
10123 Nam := Actual;
10124
10125 elsif Present (Default_Name (Formal)) then
10126 if not Nkind_In (Default_Name (Formal), N_Attribute_Reference,
10127 N_Selected_Component,
10128 N_Indexed_Component,
10129 N_Character_Literal)
10130 and then Present (Entity (Default_Name (Formal)))
10131 then
10132 Nam := New_Occurrence_Of (Entity (Default_Name (Formal)), Loc);
10133 else
10134 Nam := New_Copy (Default_Name (Formal));
10135 Set_Sloc (Nam, Loc);
10136 end if;
10137
10138 elsif Box_Present (Formal) then
10139
10140 -- Actual is resolved at the point of instantiation. Create an
10141 -- identifier or operator with the same name as the formal.
10142
10143 if Nkind (Formal_Sub) = N_Defining_Operator_Symbol then
10144 Nam :=
10145 Make_Operator_Symbol (Loc,
10146 Chars => Chars (Formal_Sub),
10147 Strval => No_String);
10148 else
10149 Nam := Make_Identifier (Loc, Chars (Formal_Sub));
10150 end if;
10151
10152 elsif Nkind (Specification (Formal)) = N_Procedure_Specification
10153 and then Null_Present (Specification (Formal))
10154 then
10155 -- Generate null body for procedure, for use in the instance
10156
10157 Decl_Node :=
10158 Make_Subprogram_Body (Loc,
10159 Specification => New_Spec,
10160 Declarations => New_List,
10161 Handled_Statement_Sequence =>
10162 Make_Handled_Sequence_Of_Statements (Loc,
10163 Statements => New_List (Make_Null_Statement (Loc))));
10164
10165 Set_Is_Intrinsic_Subprogram (Defining_Unit_Name (New_Spec));
10166 return Decl_Node;
10167
10168 else
10169 Error_Msg_Sloc := Sloc (Scope (Analyzed_S));
10170 Error_Msg_NE
10171 ("missing actual&", Instantiation_Node, Formal_Sub);
10172 Error_Msg_NE
10173 ("\in instantiation of & declared#",
10174 Instantiation_Node, Scope (Analyzed_S));
10175 Abandon_Instantiation (Instantiation_Node);
10176 end if;
10177
10178 Decl_Node :=
10179 Make_Subprogram_Renaming_Declaration (Loc,
10180 Specification => New_Spec,
10181 Name => Nam);
10182
10183 -- If we do not have an actual and the formal specified <> then set to
10184 -- get proper default.
10185
10186 if No (Actual) and then Box_Present (Formal) then
10187 Set_From_Default (Decl_Node);
10188 end if;
10189
10190 -- Gather possible interpretations for the actual before analyzing the
10191 -- instance. If overloaded, it will be resolved when analyzing the
10192 -- renaming declaration.
10193
10194 if Box_Present (Formal) and then No (Actual) then
10195 Analyze (Nam);
10196
10197 if Is_Child_Unit (Scope (Analyzed_S))
10198 and then Present (Entity (Nam))
10199 then
10200 if not Is_Overloaded (Nam) then
10201 if From_Parent_Scope (Entity (Nam)) then
10202 Set_Is_Immediately_Visible (Entity (Nam), False);
10203 Set_Entity (Nam, Empty);
10204 Set_Etype (Nam, Empty);
10205
10206 Analyze (Nam);
10207 Set_Is_Immediately_Visible (Entity (Nam));
10208 end if;
10209
10210 else
10211 declare
10212 I : Interp_Index;
10213 It : Interp;
10214
10215 begin
10216 Get_First_Interp (Nam, I, It);
10217 while Present (It.Nam) loop
10218 if From_Parent_Scope (It.Nam) then
10219 Remove_Interp (I);
10220 end if;
10221
10222 Get_Next_Interp (I, It);
10223 end loop;
10224 end;
10225 end if;
10226 end if;
10227 end if;
10228
10229 -- The generic instantiation freezes the actual. This can only be done
10230 -- once the actual is resolved, in the analysis of the renaming
10231 -- declaration. To make the formal subprogram entity available, we set
10232 -- Corresponding_Formal_Spec to point to the formal subprogram entity.
10233 -- This is also needed in Analyze_Subprogram_Renaming for the processing
10234 -- of formal abstract subprograms.
10235
10236 Set_Corresponding_Formal_Spec (Decl_Node, Analyzed_S);
10237
10238 -- We cannot analyze the renaming declaration, and thus find the actual,
10239 -- until all the actuals are assembled in the instance. For subsequent
10240 -- checks of other actuals, indicate the node that will hold the
10241 -- instance of this formal.
10242
10243 Set_Instance_Of (Analyzed_S, Nam);
10244
10245 if Nkind (Actual) = N_Selected_Component
10246 and then Is_Task_Type (Etype (Prefix (Actual)))
10247 and then not Is_Frozen (Etype (Prefix (Actual)))
10248 then
10249 -- The renaming declaration will create a body, which must appear
10250 -- outside of the instantiation, We move the renaming declaration
10251 -- out of the instance, and create an additional renaming inside,
10252 -- to prevent freezing anomalies.
10253
10254 declare
10255 Anon_Id : constant Entity_Id := Make_Temporary (Loc, 'E');
10256
10257 begin
10258 Set_Defining_Unit_Name (New_Spec, Anon_Id);
10259 Insert_Before (Instantiation_Node, Decl_Node);
10260 Analyze (Decl_Node);
10261
10262 -- Now create renaming within the instance
10263
10264 Decl_Node :=
10265 Make_Subprogram_Renaming_Declaration (Loc,
10266 Specification => New_Copy_Tree (New_Spec),
10267 Name => New_Occurrence_Of (Anon_Id, Loc));
10268
10269 Set_Defining_Unit_Name (Specification (Decl_Node),
10270 Make_Defining_Identifier (Loc, Chars (Formal_Sub)));
10271 end;
10272 end if;
10273
10274 return Decl_Node;
10275 end Instantiate_Formal_Subprogram;
10276
10277 ------------------------
10278 -- Instantiate_Object --
10279 ------------------------
10280
10281 function Instantiate_Object
10282 (Formal : Node_Id;
10283 Actual : Node_Id;
10284 Analyzed_Formal : Node_Id) return List_Id
10285 is
10286 Gen_Obj : constant Entity_Id := Defining_Identifier (Formal);
10287 A_Gen_Obj : constant Entity_Id :=
10288 Defining_Identifier (Analyzed_Formal);
10289 Acc_Def : Node_Id := Empty;
10290 Act_Assoc : constant Node_Id := Parent (Actual);
10291 Actual_Decl : Node_Id := Empty;
10292 Decl_Node : Node_Id;
10293 Def : Node_Id;
10294 Ftyp : Entity_Id;
10295 List : constant List_Id := New_List;
10296 Loc : constant Source_Ptr := Sloc (Actual);
10297 Orig_Ftyp : constant Entity_Id := Etype (A_Gen_Obj);
10298 Subt_Decl : Node_Id := Empty;
10299 Subt_Mark : Node_Id := Empty;
10300
10301 function Copy_Access_Def return Node_Id;
10302 -- If formal is an anonymous access, copy access definition of formal
10303 -- for generated object declaration.
10304
10305 ---------------------
10306 -- Copy_Access_Def --
10307 ---------------------
10308
10309 function Copy_Access_Def return Node_Id is
10310 begin
10311 Def := New_Copy_Tree (Acc_Def);
10312
10313 -- In addition, if formal is an access to subprogram we need to
10314 -- generate new formals for the signature of the default, so that
10315 -- the tree is properly formatted for ASIS use.
10316
10317 if Present (Access_To_Subprogram_Definition (Acc_Def)) then
10318 declare
10319 Par_Spec : Node_Id;
10320 begin
10321 Par_Spec :=
10322 First (Parameter_Specifications
10323 (Access_To_Subprogram_Definition (Def)));
10324 while Present (Par_Spec) loop
10325 Set_Defining_Identifier (Par_Spec,
10326 Make_Defining_Identifier (Sloc (Acc_Def),
10327 Chars => Chars (Defining_Identifier (Par_Spec))));
10328 Next (Par_Spec);
10329 end loop;
10330 end;
10331 end if;
10332
10333 return Def;
10334 end Copy_Access_Def;
10335
10336 -- Start of processing for Instantiate_Object
10337
10338 begin
10339 -- Formal may be an anonymous access
10340
10341 if Present (Subtype_Mark (Formal)) then
10342 Subt_Mark := Subtype_Mark (Formal);
10343 else
10344 Check_Access_Definition (Formal);
10345 Acc_Def := Access_Definition (Formal);
10346 end if;
10347
10348 -- Sloc for error message on missing actual
10349
10350 Error_Msg_Sloc := Sloc (Scope (A_Gen_Obj));
10351
10352 if Get_Instance_Of (Gen_Obj) /= Gen_Obj then
10353 Error_Msg_N ("duplicate instantiation of generic parameter", Actual);
10354 end if;
10355
10356 Set_Parent (List, Parent (Actual));
10357
10358 -- OUT present
10359
10360 if Out_Present (Formal) then
10361
10362 -- An IN OUT generic actual must be a name. The instantiation is a
10363 -- renaming declaration. The actual is the name being renamed. We
10364 -- use the actual directly, rather than a copy, because it is not
10365 -- used further in the list of actuals, and because a copy or a use
10366 -- of relocate_node is incorrect if the instance is nested within a
10367 -- generic. In order to simplify ASIS searches, the Generic_Parent
10368 -- field links the declaration to the generic association.
10369
10370 if No (Actual) then
10371 Error_Msg_NE
10372 ("missing actual &",
10373 Instantiation_Node, Gen_Obj);
10374 Error_Msg_NE
10375 ("\in instantiation of & declared#",
10376 Instantiation_Node, Scope (A_Gen_Obj));
10377 Abandon_Instantiation (Instantiation_Node);
10378 end if;
10379
10380 if Present (Subt_Mark) then
10381 Decl_Node :=
10382 Make_Object_Renaming_Declaration (Loc,
10383 Defining_Identifier => New_Copy (Gen_Obj),
10384 Subtype_Mark => New_Copy_Tree (Subt_Mark),
10385 Name => Actual);
10386
10387 else pragma Assert (Present (Acc_Def));
10388 Decl_Node :=
10389 Make_Object_Renaming_Declaration (Loc,
10390 Defining_Identifier => New_Copy (Gen_Obj),
10391 Access_Definition => New_Copy_Tree (Acc_Def),
10392 Name => Actual);
10393 end if;
10394
10395 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
10396
10397 -- The analysis of the actual may produce Insert_Action nodes, so
10398 -- the declaration must have a context in which to attach them.
10399
10400 Append (Decl_Node, List);
10401 Analyze (Actual);
10402
10403 -- Return if the analysis of the actual reported some error
10404
10405 if Etype (Actual) = Any_Type then
10406 return List;
10407 end if;
10408
10409 -- This check is performed here because Analyze_Object_Renaming will
10410 -- not check it when Comes_From_Source is False. Note though that the
10411 -- check for the actual being the name of an object will be performed
10412 -- in Analyze_Object_Renaming.
10413
10414 if Is_Object_Reference (Actual)
10415 and then Is_Dependent_Component_Of_Mutable_Object (Actual)
10416 then
10417 Error_Msg_N
10418 ("illegal discriminant-dependent component for in out parameter",
10419 Actual);
10420 end if;
10421
10422 -- The actual has to be resolved in order to check that it is a
10423 -- variable (due to cases such as F (1), where F returns access to
10424 -- an array, and for overloaded prefixes).
10425
10426 Ftyp := Get_Instance_Of (Etype (A_Gen_Obj));
10427
10428 -- If the type of the formal is not itself a formal, and the current
10429 -- unit is a child unit, the formal type must be declared in a
10430 -- parent, and must be retrieved by visibility.
10431
10432 if Ftyp = Orig_Ftyp
10433 and then Is_Generic_Unit (Scope (Ftyp))
10434 and then Is_Child_Unit (Scope (A_Gen_Obj))
10435 then
10436 declare
10437 Temp : constant Node_Id :=
10438 New_Copy_Tree (Subtype_Mark (Analyzed_Formal));
10439 begin
10440 Set_Entity (Temp, Empty);
10441 Find_Type (Temp);
10442 Ftyp := Entity (Temp);
10443 end;
10444 end if;
10445
10446 if Is_Private_Type (Ftyp)
10447 and then not Is_Private_Type (Etype (Actual))
10448 and then (Base_Type (Full_View (Ftyp)) = Base_Type (Etype (Actual))
10449 or else Base_Type (Etype (Actual)) = Ftyp)
10450 then
10451 -- If the actual has the type of the full view of the formal, or
10452 -- else a non-private subtype of the formal, then the visibility
10453 -- of the formal type has changed. Add to the actuals a subtype
10454 -- declaration that will force the exchange of views in the body
10455 -- of the instance as well.
10456
10457 Subt_Decl :=
10458 Make_Subtype_Declaration (Loc,
10459 Defining_Identifier => Make_Temporary (Loc, 'P'),
10460 Subtype_Indication => New_Occurrence_Of (Ftyp, Loc));
10461
10462 Prepend (Subt_Decl, List);
10463
10464 Prepend_Elmt (Full_View (Ftyp), Exchanged_Views);
10465 Exchange_Declarations (Ftyp);
10466 end if;
10467
10468 Resolve (Actual, Ftyp);
10469
10470 if not Denotes_Variable (Actual) then
10471 Error_Msg_NE ("actual for& must be a variable", Actual, Gen_Obj);
10472
10473 elsif Base_Type (Ftyp) /= Base_Type (Etype (Actual)) then
10474
10475 -- Ada 2005 (AI-423): For a generic formal object of mode in out,
10476 -- the type of the actual shall resolve to a specific anonymous
10477 -- access type.
10478
10479 if Ada_Version < Ada_2005
10480 or else Ekind (Base_Type (Ftyp)) /=
10481 E_Anonymous_Access_Type
10482 or else Ekind (Base_Type (Etype (Actual))) /=
10483 E_Anonymous_Access_Type
10484 then
10485 Error_Msg_NE
10486 ("type of actual does not match type of&", Actual, Gen_Obj);
10487 end if;
10488 end if;
10489
10490 Note_Possible_Modification (Actual, Sure => True);
10491
10492 -- Check for instantiation of atomic/volatile actual for
10493 -- non-atomic/volatile formal (RM C.6 (12)).
10494
10495 if Is_Atomic_Object (Actual) and then not Is_Atomic (Orig_Ftyp) then
10496 Error_Msg_N
10497 ("cannot instantiate non-atomic formal object "
10498 & "with atomic actual", Actual);
10499
10500 elsif Is_Volatile_Object (Actual) and then not Is_Volatile (Orig_Ftyp)
10501 then
10502 Error_Msg_N
10503 ("cannot instantiate non-volatile formal object "
10504 & "with volatile actual", Actual);
10505 end if;
10506
10507 -- Formal in-parameter
10508
10509 else
10510 -- The instantiation of a generic formal in-parameter is constant
10511 -- declaration. The actual is the expression for that declaration.
10512 -- Its type is a full copy of the type of the formal. This may be
10513 -- an access to subprogram, for which we need to generate entities
10514 -- for the formals in the new signature.
10515
10516 if Present (Actual) then
10517 if Present (Subt_Mark) then
10518 Def := New_Copy_Tree (Subt_Mark);
10519 else pragma Assert (Present (Acc_Def));
10520 Def := Copy_Access_Def;
10521 end if;
10522
10523 Decl_Node :=
10524 Make_Object_Declaration (Loc,
10525 Defining_Identifier => New_Copy (Gen_Obj),
10526 Constant_Present => True,
10527 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
10528 Object_Definition => Def,
10529 Expression => Actual);
10530
10531 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
10532
10533 -- A generic formal object of a tagged type is defined to be
10534 -- aliased so the new constant must also be treated as aliased.
10535
10536 if Is_Tagged_Type (Etype (A_Gen_Obj)) then
10537 Set_Aliased_Present (Decl_Node);
10538 end if;
10539
10540 Append (Decl_Node, List);
10541
10542 -- No need to repeat (pre-)analysis of some expression nodes
10543 -- already handled in Preanalyze_Actuals.
10544
10545 if Nkind (Actual) /= N_Allocator then
10546 Analyze (Actual);
10547
10548 -- Return if the analysis of the actual reported some error
10549
10550 if Etype (Actual) = Any_Type then
10551 return List;
10552 end if;
10553 end if;
10554
10555 declare
10556 Formal_Type : constant Entity_Id := Etype (A_Gen_Obj);
10557 Typ : Entity_Id;
10558
10559 begin
10560 Typ := Get_Instance_Of (Formal_Type);
10561
10562 -- If the actual appears in the current or an enclosing scope,
10563 -- use its type directly. This is relevant if it has an actual
10564 -- subtype that is distinct from its nominal one. This cannot
10565 -- be done in general because the type of the actual may
10566 -- depend on other actuals, and only be fully determined when
10567 -- the enclosing instance is analyzed.
10568
10569 if Present (Etype (Actual))
10570 and then Is_Constr_Subt_For_U_Nominal (Etype (Actual))
10571 then
10572 Freeze_Before (Instantiation_Node, Etype (Actual));
10573 else
10574 Freeze_Before (Instantiation_Node, Typ);
10575 end if;
10576
10577 -- If the actual is an aggregate, perform name resolution on
10578 -- its components (the analysis of an aggregate does not do it)
10579 -- to capture local names that may be hidden if the generic is
10580 -- a child unit.
10581
10582 if Nkind (Actual) = N_Aggregate then
10583 Preanalyze_And_Resolve (Actual, Typ);
10584 end if;
10585
10586 if Is_Limited_Type (Typ)
10587 and then not OK_For_Limited_Init (Typ, Actual)
10588 then
10589 Error_Msg_N
10590 ("initialization not allowed for limited types", Actual);
10591 Explain_Limited_Type (Typ, Actual);
10592 end if;
10593 end;
10594
10595 elsif Present (Default_Expression (Formal)) then
10596
10597 -- Use default to construct declaration
10598
10599 if Present (Subt_Mark) then
10600 Def := New_Copy (Subt_Mark);
10601 else pragma Assert (Present (Acc_Def));
10602 Def := Copy_Access_Def;
10603 end if;
10604
10605 Decl_Node :=
10606 Make_Object_Declaration (Sloc (Formal),
10607 Defining_Identifier => New_Copy (Gen_Obj),
10608 Constant_Present => True,
10609 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
10610 Object_Definition => Def,
10611 Expression => New_Copy_Tree
10612 (Default_Expression (Formal)));
10613
10614 Append (Decl_Node, List);
10615 Set_Analyzed (Expression (Decl_Node), False);
10616
10617 else
10618 Error_Msg_NE ("missing actual&", Instantiation_Node, Gen_Obj);
10619 Error_Msg_NE ("\in instantiation of & declared#",
10620 Instantiation_Node, Scope (A_Gen_Obj));
10621
10622 if Is_Scalar_Type (Etype (A_Gen_Obj)) then
10623
10624 -- Create dummy constant declaration so that instance can be
10625 -- analyzed, to minimize cascaded visibility errors.
10626
10627 if Present (Subt_Mark) then
10628 Def := Subt_Mark;
10629 else pragma Assert (Present (Acc_Def));
10630 Def := Acc_Def;
10631 end if;
10632
10633 Decl_Node :=
10634 Make_Object_Declaration (Loc,
10635 Defining_Identifier => New_Copy (Gen_Obj),
10636 Constant_Present => True,
10637 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
10638 Object_Definition => New_Copy (Def),
10639 Expression =>
10640 Make_Attribute_Reference (Sloc (Gen_Obj),
10641 Attribute_Name => Name_First,
10642 Prefix => New_Copy (Def)));
10643
10644 Append (Decl_Node, List);
10645
10646 else
10647 Abandon_Instantiation (Instantiation_Node);
10648 end if;
10649 end if;
10650 end if;
10651
10652 if Nkind (Actual) in N_Has_Entity then
10653 Actual_Decl := Parent (Entity (Actual));
10654 end if;
10655
10656 -- Ada 2005 (AI-423): For a formal object declaration with a null
10657 -- exclusion or an access definition that has a null exclusion: If the
10658 -- actual matching the formal object declaration denotes a generic
10659 -- formal object of another generic unit G, and the instantiation
10660 -- containing the actual occurs within the body of G or within the body
10661 -- of a generic unit declared within the declarative region of G, then
10662 -- the declaration of the formal object of G must have a null exclusion.
10663 -- Otherwise, the subtype of the actual matching the formal object
10664 -- declaration shall exclude null.
10665
10666 if Ada_Version >= Ada_2005
10667 and then Present (Actual_Decl)
10668 and then Nkind_In (Actual_Decl, N_Formal_Object_Declaration,
10669 N_Object_Declaration)
10670 and then Nkind (Analyzed_Formal) = N_Formal_Object_Declaration
10671 and then not Has_Null_Exclusion (Actual_Decl)
10672 and then Has_Null_Exclusion (Analyzed_Formal)
10673 then
10674 Error_Msg_Sloc := Sloc (Analyzed_Formal);
10675 Error_Msg_N
10676 ("actual must exclude null to match generic formal#", Actual);
10677 end if;
10678
10679 -- An effectively volatile object cannot be used as an actual in a
10680 -- generic instantiation (SPARK RM 7.1.3(7)). The following check is
10681 -- relevant only when SPARK_Mode is on as it is not a standard Ada
10682 -- legality rule.
10683
10684 if SPARK_Mode = On
10685 and then Present (Actual)
10686 and then Is_Effectively_Volatile_Object (Actual)
10687 then
10688 Error_Msg_N
10689 ("volatile object cannot act as actual in generic instantiation",
10690 Actual);
10691 end if;
10692
10693 return List;
10694 end Instantiate_Object;
10695
10696 ------------------------------
10697 -- Instantiate_Package_Body --
10698 ------------------------------
10699
10700 procedure Instantiate_Package_Body
10701 (Body_Info : Pending_Body_Info;
10702 Inlined_Body : Boolean := False;
10703 Body_Optional : Boolean := False)
10704 is
10705 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
10706 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
10707 Loc : constant Source_Ptr := Sloc (Inst_Node);
10708
10709 Gen_Id : constant Node_Id := Name (Inst_Node);
10710 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
10711 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
10712 Act_Spec : constant Node_Id := Specification (Act_Decl);
10713 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Spec);
10714
10715 Save_IPSM : constant Boolean := Ignore_Pragma_SPARK_Mode;
10716 Save_Style_Check : constant Boolean := Style_Check;
10717
10718 Act_Body : Node_Id;
10719 Act_Body_Id : Entity_Id;
10720 Act_Body_Name : Node_Id;
10721 Gen_Body : Node_Id;
10722 Gen_Body_Id : Node_Id;
10723 Par_Ent : Entity_Id := Empty;
10724 Par_Vis : Boolean := False;
10725
10726 Parent_Installed : Boolean := False;
10727
10728 Vis_Prims_List : Elist_Id := No_Elist;
10729 -- List of primitives made temporarily visible in the instantiation
10730 -- to match the visibility of the formal type
10731
10732 procedure Check_Initialized_Types;
10733 -- In a generic package body, an entity of a generic private type may
10734 -- appear uninitialized. This is suspicious, unless the actual is a
10735 -- fully initialized type.
10736
10737 -----------------------------
10738 -- Check_Initialized_Types --
10739 -----------------------------
10740
10741 procedure Check_Initialized_Types is
10742 Decl : Node_Id;
10743 Formal : Entity_Id;
10744 Actual : Entity_Id;
10745 Uninit_Var : Entity_Id;
10746
10747 begin
10748 Decl := First (Generic_Formal_Declarations (Gen_Decl));
10749 while Present (Decl) loop
10750 Uninit_Var := Empty;
10751
10752 if Nkind (Decl) = N_Private_Extension_Declaration then
10753 Uninit_Var := Uninitialized_Variable (Decl);
10754
10755 elsif Nkind (Decl) = N_Formal_Type_Declaration
10756 and then Nkind (Formal_Type_Definition (Decl)) =
10757 N_Formal_Private_Type_Definition
10758 then
10759 Uninit_Var :=
10760 Uninitialized_Variable (Formal_Type_Definition (Decl));
10761 end if;
10762
10763 if Present (Uninit_Var) then
10764 Formal := Defining_Identifier (Decl);
10765 Actual := First_Entity (Act_Decl_Id);
10766
10767 -- For each formal there is a subtype declaration that renames
10768 -- the actual and has the same name as the formal. Locate the
10769 -- formal for warning message about uninitialized variables
10770 -- in the generic, for which the actual type should be a fully
10771 -- initialized type.
10772
10773 while Present (Actual) loop
10774 exit when Ekind (Actual) = E_Package
10775 and then Present (Renamed_Object (Actual));
10776
10777 if Chars (Actual) = Chars (Formal)
10778 and then not Is_Scalar_Type (Actual)
10779 and then not Is_Fully_Initialized_Type (Actual)
10780 and then Warn_On_No_Value_Assigned
10781 then
10782 Error_Msg_Node_2 := Formal;
10783 Error_Msg_NE
10784 ("generic unit has uninitialized variable& of "
10785 & "formal private type &?v?", Actual, Uninit_Var);
10786 Error_Msg_NE
10787 ("actual type for& should be fully initialized type?v?",
10788 Actual, Formal);
10789 exit;
10790 end if;
10791
10792 Next_Entity (Actual);
10793 end loop;
10794 end if;
10795
10796 Next (Decl);
10797 end loop;
10798 end Check_Initialized_Types;
10799
10800 -- Start of processing for Instantiate_Package_Body
10801
10802 begin
10803 Gen_Body_Id := Corresponding_Body (Gen_Decl);
10804
10805 -- The instance body may already have been processed, as the parent of
10806 -- another instance that is inlined (Load_Parent_Of_Generic).
10807
10808 if Present (Corresponding_Body (Instance_Spec (Inst_Node))) then
10809 return;
10810 end if;
10811
10812 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
10813
10814 -- Re-establish the state of information on which checks are suppressed.
10815 -- This information was set in Body_Info at the point of instantiation,
10816 -- and now we restore it so that the instance is compiled using the
10817 -- check status at the instantiation (RM 11.5(7.2/2), AI95-00224-01).
10818
10819 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
10820 Scope_Suppress := Body_Info.Scope_Suppress;
10821 Opt.Ada_Version := Body_Info.Version;
10822 Opt.Ada_Version_Pragma := Body_Info.Version_Pragma;
10823 Restore_Warnings (Body_Info.Warnings);
10824 Opt.SPARK_Mode := Body_Info.SPARK_Mode;
10825 Opt.SPARK_Mode_Pragma := Body_Info.SPARK_Mode_Pragma;
10826
10827 if No (Gen_Body_Id) then
10828
10829 -- Do not look for parent of generic body if none is required.
10830 -- This may happen when the routine is called as part of the
10831 -- Pending_Instantiations processing, when nested instances
10832 -- may precede the one generated from the main unit.
10833
10834 if not Unit_Requires_Body (Defining_Entity (Gen_Decl))
10835 and then Body_Optional
10836 then
10837 return;
10838 else
10839 Load_Parent_Of_Generic
10840 (Inst_Node, Specification (Gen_Decl), Body_Optional);
10841 Gen_Body_Id := Corresponding_Body (Gen_Decl);
10842 end if;
10843 end if;
10844
10845 -- Establish global variable for sloc adjustment and for error recovery
10846 -- In the case of an instance body for an instantiation with actuals
10847 -- from a limited view, the instance body is placed at the beginning
10848 -- of the enclosing package body: use the body entity as the source
10849 -- location for nodes of the instance body.
10850
10851 if not Is_Empty_Elmt_List (Incomplete_Actuals (Act_Decl_Id)) then
10852 declare
10853 Scop : constant Entity_Id := Scope (Act_Decl_Id);
10854 Body_Id : constant Node_Id :=
10855 Corresponding_Body (Unit_Declaration_Node (Scop));
10856
10857 begin
10858 Instantiation_Node := Body_Id;
10859 end;
10860 else
10861 Instantiation_Node := Inst_Node;
10862 end if;
10863
10864 if Present (Gen_Body_Id) then
10865 Save_Env (Gen_Unit, Act_Decl_Id);
10866 Style_Check := False;
10867
10868 -- If the context of the instance is subject to SPARK_Mode "off" or
10869 -- the annotation is altogether missing, set the global flag which
10870 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
10871 -- the instance.
10872
10873 if SPARK_Mode /= On then
10874 Ignore_Pragma_SPARK_Mode := True;
10875 end if;
10876
10877 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
10878 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
10879
10880 Create_Instantiation_Source
10881 (Inst_Node, Gen_Body_Id, False, S_Adjustment);
10882
10883 Act_Body :=
10884 Copy_Generic_Node
10885 (Original_Node (Gen_Body), Empty, Instantiating => True);
10886
10887 -- Create proper (possibly qualified) defining name for the body, to
10888 -- correspond to the one in the spec.
10889
10890 Act_Body_Id :=
10891 Make_Defining_Identifier (Sloc (Act_Decl_Id), Chars (Act_Decl_Id));
10892 Set_Comes_From_Source (Act_Body_Id, Comes_From_Source (Act_Decl_Id));
10893
10894 -- Some attributes of spec entity are not inherited by body entity
10895
10896 Set_Handler_Records (Act_Body_Id, No_List);
10897
10898 if Nkind (Defining_Unit_Name (Act_Spec)) =
10899 N_Defining_Program_Unit_Name
10900 then
10901 Act_Body_Name :=
10902 Make_Defining_Program_Unit_Name (Loc,
10903 Name =>
10904 New_Copy_Tree (Name (Defining_Unit_Name (Act_Spec))),
10905 Defining_Identifier => Act_Body_Id);
10906 else
10907 Act_Body_Name := Act_Body_Id;
10908 end if;
10909
10910 Set_Defining_Unit_Name (Act_Body, Act_Body_Name);
10911
10912 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
10913 Check_Generic_Actuals (Act_Decl_Id, False);
10914 Check_Initialized_Types;
10915
10916 -- Install primitives hidden at the point of the instantiation but
10917 -- visible when processing the generic formals
10918
10919 declare
10920 E : Entity_Id;
10921
10922 begin
10923 E := First_Entity (Act_Decl_Id);
10924 while Present (E) loop
10925 if Is_Type (E)
10926 and then Is_Generic_Actual_Type (E)
10927 and then Is_Tagged_Type (E)
10928 then
10929 Install_Hidden_Primitives
10930 (Prims_List => Vis_Prims_List,
10931 Gen_T => Generic_Parent_Type (Parent (E)),
10932 Act_T => E);
10933 end if;
10934
10935 Next_Entity (E);
10936 end loop;
10937 end;
10938
10939 -- If it is a child unit, make the parent instance (which is an
10940 -- instance of the parent of the generic) visible. The parent
10941 -- instance is the prefix of the name of the generic unit.
10942
10943 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
10944 and then Nkind (Gen_Id) = N_Expanded_Name
10945 then
10946 Par_Ent := Entity (Prefix (Gen_Id));
10947 Par_Vis := Is_Immediately_Visible (Par_Ent);
10948 Install_Parent (Par_Ent, In_Body => True);
10949 Parent_Installed := True;
10950
10951 elsif Is_Child_Unit (Gen_Unit) then
10952 Par_Ent := Scope (Gen_Unit);
10953 Par_Vis := Is_Immediately_Visible (Par_Ent);
10954 Install_Parent (Par_Ent, In_Body => True);
10955 Parent_Installed := True;
10956 end if;
10957
10958 -- If the instantiation is a library unit, and this is the main unit,
10959 -- then build the resulting compilation unit nodes for the instance.
10960 -- If this is a compilation unit but it is not the main unit, then it
10961 -- is the body of a unit in the context, that is being compiled
10962 -- because it is encloses some inlined unit or another generic unit
10963 -- being instantiated. In that case, this body is not part of the
10964 -- current compilation, and is not attached to the tree, but its
10965 -- parent must be set for analysis.
10966
10967 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
10968
10969 -- Replace instance node with body of instance, and create new
10970 -- node for corresponding instance declaration.
10971
10972 Build_Instance_Compilation_Unit_Nodes
10973 (Inst_Node, Act_Body, Act_Decl);
10974 Analyze (Inst_Node);
10975
10976 if Parent (Inst_Node) = Cunit (Main_Unit) then
10977
10978 -- If the instance is a child unit itself, then set the scope
10979 -- of the expanded body to be the parent of the instantiation
10980 -- (ensuring that the fully qualified name will be generated
10981 -- for the elaboration subprogram).
10982
10983 if Nkind (Defining_Unit_Name (Act_Spec)) =
10984 N_Defining_Program_Unit_Name
10985 then
10986 Set_Scope (Defining_Entity (Inst_Node), Scope (Act_Decl_Id));
10987 end if;
10988 end if;
10989
10990 -- Case where instantiation is not a library unit
10991
10992 else
10993 -- If this is an early instantiation, i.e. appears textually
10994 -- before the corresponding body and must be elaborated first,
10995 -- indicate that the body instance is to be delayed.
10996
10997 Install_Body (Act_Body, Inst_Node, Gen_Body, Gen_Decl);
10998
10999 -- Now analyze the body. We turn off all checks if this is an
11000 -- internal unit, since there is no reason to have checks on for
11001 -- any predefined run-time library code. All such code is designed
11002 -- to be compiled with checks off.
11003
11004 -- Note that we do NOT apply this criterion to children of GNAT
11005 -- The latter units must suppress checks explicitly if needed.
11006
11007 -- We also do not suppress checks in CodePeer mode where we are
11008 -- interested in finding possible runtime errors.
11009
11010 if not CodePeer_Mode
11011 and then Is_Predefined_File_Name
11012 (Unit_File_Name (Get_Source_Unit (Gen_Decl)))
11013 then
11014 Analyze (Act_Body, Suppress => All_Checks);
11015 else
11016 Analyze (Act_Body);
11017 end if;
11018 end if;
11019
11020 Inherit_Context (Gen_Body, Inst_Node);
11021
11022 -- Remove the parent instances if they have been placed on the scope
11023 -- stack to compile the body.
11024
11025 if Parent_Installed then
11026 Remove_Parent (In_Body => True);
11027
11028 -- Restore the previous visibility of the parent
11029
11030 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
11031 end if;
11032
11033 Restore_Hidden_Primitives (Vis_Prims_List);
11034 Restore_Private_Views (Act_Decl_Id);
11035
11036 -- Remove the current unit from visibility if this is an instance
11037 -- that is not elaborated on the fly for inlining purposes.
11038
11039 if not Inlined_Body then
11040 Set_Is_Immediately_Visible (Act_Decl_Id, False);
11041 end if;
11042
11043 Restore_Env;
11044 Ignore_Pragma_SPARK_Mode := Save_IPSM;
11045 Style_Check := Save_Style_Check;
11046
11047 -- If we have no body, and the unit requires a body, then complain. This
11048 -- complaint is suppressed if we have detected other errors (since a
11049 -- common reason for missing the body is that it had errors).
11050 -- In CodePeer mode, a warning has been emitted already, no need for
11051 -- further messages.
11052
11053 elsif Unit_Requires_Body (Gen_Unit)
11054 and then not Body_Optional
11055 then
11056 if CodePeer_Mode then
11057 null;
11058
11059 elsif Serious_Errors_Detected = 0 then
11060 Error_Msg_NE
11061 ("cannot find body of generic package &", Inst_Node, Gen_Unit);
11062
11063 -- Don't attempt to perform any cleanup actions if some other error
11064 -- was already detected, since this can cause blowups.
11065
11066 else
11067 return;
11068 end if;
11069
11070 -- Case of package that does not need a body
11071
11072 else
11073 -- If the instantiation of the declaration is a library unit, rewrite
11074 -- the original package instantiation as a package declaration in the
11075 -- compilation unit node.
11076
11077 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
11078 Set_Parent_Spec (Act_Decl, Parent_Spec (Inst_Node));
11079 Rewrite (Inst_Node, Act_Decl);
11080
11081 -- Generate elaboration entity, in case spec has elaboration code.
11082 -- This cannot be done when the instance is analyzed, because it
11083 -- is not known yet whether the body exists.
11084
11085 Set_Elaboration_Entity_Required (Act_Decl_Id, False);
11086 Build_Elaboration_Entity (Parent (Inst_Node), Act_Decl_Id);
11087
11088 -- If the instantiation is not a library unit, then append the
11089 -- declaration to the list of implicitly generated entities, unless
11090 -- it is already a list member which means that it was already
11091 -- processed
11092
11093 elsif not Is_List_Member (Act_Decl) then
11094 Mark_Rewrite_Insertion (Act_Decl);
11095 Insert_Before (Inst_Node, Act_Decl);
11096 end if;
11097 end if;
11098
11099 Expander_Mode_Restore;
11100 end Instantiate_Package_Body;
11101
11102 ---------------------------------
11103 -- Instantiate_Subprogram_Body --
11104 ---------------------------------
11105
11106 procedure Instantiate_Subprogram_Body
11107 (Body_Info : Pending_Body_Info;
11108 Body_Optional : Boolean := False)
11109 is
11110 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
11111 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
11112 Loc : constant Source_Ptr := Sloc (Inst_Node);
11113 Gen_Id : constant Node_Id := Name (Inst_Node);
11114 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
11115 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
11116 Act_Decl_Id : constant Entity_Id :=
11117 Defining_Unit_Name (Specification (Act_Decl));
11118 Pack_Id : constant Entity_Id :=
11119 Defining_Unit_Name (Parent (Act_Decl));
11120
11121 Saved_IPSM : constant Boolean := Ignore_Pragma_SPARK_Mode;
11122 Saved_Style_Check : constant Boolean := Style_Check;
11123 Saved_Warnings : constant Warning_Record := Save_Warnings;
11124
11125 Act_Body : Node_Id;
11126 Act_Body_Id : Entity_Id;
11127 Gen_Body : Node_Id;
11128 Gen_Body_Id : Node_Id;
11129 Pack_Body : Node_Id;
11130 Par_Ent : Entity_Id := Empty;
11131 Par_Vis : Boolean := False;
11132 Ret_Expr : Node_Id;
11133
11134 Parent_Installed : Boolean := False;
11135
11136 begin
11137 Gen_Body_Id := Corresponding_Body (Gen_Decl);
11138
11139 -- Subprogram body may have been created already because of an inline
11140 -- pragma, or because of multiple elaborations of the enclosing package
11141 -- when several instances of the subprogram appear in the main unit.
11142
11143 if Present (Corresponding_Body (Act_Decl)) then
11144 return;
11145 end if;
11146
11147 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
11148
11149 -- Re-establish the state of information on which checks are suppressed.
11150 -- This information was set in Body_Info at the point of instantiation,
11151 -- and now we restore it so that the instance is compiled using the
11152 -- check status at the instantiation (RM 11.5(7.2/2), AI95-00224-01).
11153
11154 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
11155 Scope_Suppress := Body_Info.Scope_Suppress;
11156 Opt.Ada_Version := Body_Info.Version;
11157 Opt.Ada_Version_Pragma := Body_Info.Version_Pragma;
11158 Restore_Warnings (Body_Info.Warnings);
11159 Opt.SPARK_Mode := Body_Info.SPARK_Mode;
11160 Opt.SPARK_Mode_Pragma := Body_Info.SPARK_Mode_Pragma;
11161
11162 if No (Gen_Body_Id) then
11163
11164 -- For imported generic subprogram, no body to compile, complete
11165 -- the spec entity appropriately.
11166
11167 if Is_Imported (Gen_Unit) then
11168 Set_Is_Imported (Act_Decl_Id);
11169 Set_First_Rep_Item (Act_Decl_Id, First_Rep_Item (Gen_Unit));
11170 Set_Interface_Name (Act_Decl_Id, Interface_Name (Gen_Unit));
11171 Set_Convention (Act_Decl_Id, Convention (Gen_Unit));
11172 Set_Has_Completion (Act_Decl_Id);
11173 return;
11174
11175 -- For other cases, compile the body
11176
11177 else
11178 Load_Parent_Of_Generic
11179 (Inst_Node, Specification (Gen_Decl), Body_Optional);
11180 Gen_Body_Id := Corresponding_Body (Gen_Decl);
11181 end if;
11182 end if;
11183
11184 Instantiation_Node := Inst_Node;
11185
11186 if Present (Gen_Body_Id) then
11187 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
11188
11189 if Nkind (Gen_Body) = N_Subprogram_Body_Stub then
11190
11191 -- Either body is not present, or context is non-expanding, as
11192 -- when compiling a subunit. Mark the instance as completed, and
11193 -- diagnose a missing body when needed.
11194
11195 if Expander_Active
11196 and then Operating_Mode = Generate_Code
11197 then
11198 Error_Msg_N
11199 ("missing proper body for instantiation", Gen_Body);
11200 end if;
11201
11202 Set_Has_Completion (Act_Decl_Id);
11203 return;
11204 end if;
11205
11206 Save_Env (Gen_Unit, Act_Decl_Id);
11207 Style_Check := False;
11208
11209 -- If the context of the instance is subject to SPARK_Mode "off" or
11210 -- the annotation is altogether missing, set the global flag which
11211 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
11212 -- the instance.
11213
11214 if SPARK_Mode /= On then
11215 Ignore_Pragma_SPARK_Mode := True;
11216 end if;
11217
11218 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
11219 Create_Instantiation_Source
11220 (Inst_Node,
11221 Gen_Body_Id,
11222 False,
11223 S_Adjustment);
11224
11225 Act_Body :=
11226 Copy_Generic_Node
11227 (Original_Node (Gen_Body), Empty, Instantiating => True);
11228
11229 -- Create proper defining name for the body, to correspond to the one
11230 -- in the spec.
11231
11232 Act_Body_Id :=
11233 Make_Defining_Identifier (Sloc (Act_Decl_Id), Chars (Act_Decl_Id));
11234
11235 Set_Comes_From_Source (Act_Body_Id, Comes_From_Source (Act_Decl_Id));
11236 Set_Defining_Unit_Name (Specification (Act_Body), Act_Body_Id);
11237
11238 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
11239 Set_Has_Completion (Act_Decl_Id);
11240 Check_Generic_Actuals (Pack_Id, False);
11241
11242 -- Generate a reference to link the visible subprogram instance to
11243 -- the generic body, which for navigation purposes is the only
11244 -- available source for the instance.
11245
11246 Generate_Reference
11247 (Related_Instance (Pack_Id),
11248 Gen_Body_Id, 'b', Set_Ref => False, Force => True);
11249
11250 -- If it is a child unit, make the parent instance (which is an
11251 -- instance of the parent of the generic) visible. The parent
11252 -- instance is the prefix of the name of the generic unit.
11253
11254 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
11255 and then Nkind (Gen_Id) = N_Expanded_Name
11256 then
11257 Par_Ent := Entity (Prefix (Gen_Id));
11258 Par_Vis := Is_Immediately_Visible (Par_Ent);
11259 Install_Parent (Par_Ent, In_Body => True);
11260 Parent_Installed := True;
11261
11262 elsif Is_Child_Unit (Gen_Unit) then
11263 Par_Ent := Scope (Gen_Unit);
11264 Par_Vis := Is_Immediately_Visible (Par_Ent);
11265 Install_Parent (Par_Ent, In_Body => True);
11266 Parent_Installed := True;
11267 end if;
11268
11269 -- Subprogram body is placed in the body of wrapper package,
11270 -- whose spec contains the subprogram declaration as well as
11271 -- the renaming declarations for the generic parameters.
11272
11273 Pack_Body :=
11274 Make_Package_Body (Loc,
11275 Defining_Unit_Name => New_Copy (Pack_Id),
11276 Declarations => New_List (Act_Body));
11277
11278 Set_Corresponding_Spec (Pack_Body, Pack_Id);
11279
11280 -- If the instantiation is a library unit, then build resulting
11281 -- compilation unit nodes for the instance. The declaration of
11282 -- the enclosing package is the grandparent of the subprogram
11283 -- declaration. First replace the instantiation node as the unit
11284 -- of the corresponding compilation.
11285
11286 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
11287 if Parent (Inst_Node) = Cunit (Main_Unit) then
11288 Set_Unit (Parent (Inst_Node), Inst_Node);
11289 Build_Instance_Compilation_Unit_Nodes
11290 (Inst_Node, Pack_Body, Parent (Parent (Act_Decl)));
11291 Analyze (Inst_Node);
11292 else
11293 Set_Parent (Pack_Body, Parent (Inst_Node));
11294 Analyze (Pack_Body);
11295 end if;
11296
11297 else
11298 Insert_Before (Inst_Node, Pack_Body);
11299 Mark_Rewrite_Insertion (Pack_Body);
11300 Analyze (Pack_Body);
11301
11302 if Expander_Active then
11303 Freeze_Subprogram_Body (Inst_Node, Gen_Body, Pack_Id);
11304 end if;
11305 end if;
11306
11307 Inherit_Context (Gen_Body, Inst_Node);
11308
11309 Restore_Private_Views (Pack_Id, False);
11310
11311 if Parent_Installed then
11312 Remove_Parent (In_Body => True);
11313
11314 -- Restore the previous visibility of the parent
11315
11316 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
11317 end if;
11318
11319 Restore_Env;
11320 Ignore_Pragma_SPARK_Mode := Saved_IPSM;
11321 Style_Check := Saved_Style_Check;
11322 Restore_Warnings (Saved_Warnings);
11323
11324 -- Body not found. Error was emitted already. If there were no previous
11325 -- errors, this may be an instance whose scope is a premature instance.
11326 -- In that case we must insure that the (legal) program does raise
11327 -- program error if executed. We generate a subprogram body for this
11328 -- purpose. See DEC ac30vso.
11329
11330 -- Should not reference proprietary DEC tests in comments ???
11331
11332 elsif Serious_Errors_Detected = 0
11333 and then Nkind (Parent (Inst_Node)) /= N_Compilation_Unit
11334 then
11335 if Body_Optional then
11336 return;
11337
11338 elsif Ekind (Act_Decl_Id) = E_Procedure then
11339 Act_Body :=
11340 Make_Subprogram_Body (Loc,
11341 Specification =>
11342 Make_Procedure_Specification (Loc,
11343 Defining_Unit_Name =>
11344 Make_Defining_Identifier (Loc, Chars (Act_Decl_Id)),
11345 Parameter_Specifications =>
11346 New_Copy_List
11347 (Parameter_Specifications (Parent (Act_Decl_Id)))),
11348
11349 Declarations => Empty_List,
11350 Handled_Statement_Sequence =>
11351 Make_Handled_Sequence_Of_Statements (Loc,
11352 Statements =>
11353 New_List (
11354 Make_Raise_Program_Error (Loc,
11355 Reason =>
11356 PE_Access_Before_Elaboration))));
11357
11358 else
11359 Ret_Expr :=
11360 Make_Raise_Program_Error (Loc,
11361 Reason => PE_Access_Before_Elaboration);
11362
11363 Set_Etype (Ret_Expr, (Etype (Act_Decl_Id)));
11364 Set_Analyzed (Ret_Expr);
11365
11366 Act_Body :=
11367 Make_Subprogram_Body (Loc,
11368 Specification =>
11369 Make_Function_Specification (Loc,
11370 Defining_Unit_Name =>
11371 Make_Defining_Identifier (Loc, Chars (Act_Decl_Id)),
11372 Parameter_Specifications =>
11373 New_Copy_List
11374 (Parameter_Specifications (Parent (Act_Decl_Id))),
11375 Result_Definition =>
11376 New_Occurrence_Of (Etype (Act_Decl_Id), Loc)),
11377
11378 Declarations => Empty_List,
11379 Handled_Statement_Sequence =>
11380 Make_Handled_Sequence_Of_Statements (Loc,
11381 Statements =>
11382 New_List
11383 (Make_Simple_Return_Statement (Loc, Ret_Expr))));
11384 end if;
11385
11386 Pack_Body :=
11387 Make_Package_Body (Loc,
11388 Defining_Unit_Name => New_Copy (Pack_Id),
11389 Declarations => New_List (Act_Body));
11390
11391 Insert_After (Inst_Node, Pack_Body);
11392 Set_Corresponding_Spec (Pack_Body, Pack_Id);
11393 Analyze (Pack_Body);
11394 end if;
11395
11396 Expander_Mode_Restore;
11397 end Instantiate_Subprogram_Body;
11398
11399 ----------------------
11400 -- Instantiate_Type --
11401 ----------------------
11402
11403 function Instantiate_Type
11404 (Formal : Node_Id;
11405 Actual : Node_Id;
11406 Analyzed_Formal : Node_Id;
11407 Actual_Decls : List_Id) return List_Id
11408 is
11409 Gen_T : constant Entity_Id := Defining_Identifier (Formal);
11410 A_Gen_T : constant Entity_Id :=
11411 Defining_Identifier (Analyzed_Formal);
11412 Ancestor : Entity_Id := Empty;
11413 Def : constant Node_Id := Formal_Type_Definition (Formal);
11414 Act_T : Entity_Id;
11415 Decl_Node : Node_Id;
11416 Decl_Nodes : List_Id;
11417 Loc : Source_Ptr;
11418 Subt : Entity_Id;
11419
11420 procedure Diagnose_Predicated_Actual;
11421 -- There are a number of constructs in which a discrete type with
11422 -- predicates is illegal, e.g. as an index in an array type declaration.
11423 -- If a generic type is used is such a construct in a generic package
11424 -- declaration, it carries the flag No_Predicate_On_Actual. it is part
11425 -- of the generic contract that the actual cannot have predicates.
11426
11427 procedure Validate_Array_Type_Instance;
11428 procedure Validate_Access_Subprogram_Instance;
11429 procedure Validate_Access_Type_Instance;
11430 procedure Validate_Derived_Type_Instance;
11431 procedure Validate_Derived_Interface_Type_Instance;
11432 procedure Validate_Discriminated_Formal_Type;
11433 procedure Validate_Interface_Type_Instance;
11434 procedure Validate_Private_Type_Instance;
11435 procedure Validate_Incomplete_Type_Instance;
11436 -- These procedures perform validation tests for the named case.
11437 -- Validate_Discriminated_Formal_Type is shared by formal private
11438 -- types and Ada 2012 formal incomplete types.
11439
11440 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean;
11441 -- Check that base types are the same and that the subtypes match
11442 -- statically. Used in several of the above.
11443
11444 ---------------------------------
11445 -- Diagnose_Predicated_Actual --
11446 ---------------------------------
11447
11448 procedure Diagnose_Predicated_Actual is
11449 begin
11450 if No_Predicate_On_Actual (A_Gen_T)
11451 and then Has_Predicates (Act_T)
11452 then
11453 Error_Msg_NE
11454 ("actual for& cannot be a type with predicate",
11455 Instantiation_Node, A_Gen_T);
11456
11457 elsif No_Dynamic_Predicate_On_Actual (A_Gen_T)
11458 and then Has_Predicates (Act_T)
11459 and then not Has_Static_Predicate_Aspect (Act_T)
11460 then
11461 Error_Msg_NE
11462 ("actual for& cannot be a type with a dynamic predicate",
11463 Instantiation_Node, A_Gen_T);
11464 end if;
11465 end Diagnose_Predicated_Actual;
11466
11467 --------------------
11468 -- Subtypes_Match --
11469 --------------------
11470
11471 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean is
11472 T : constant Entity_Id := Get_Instance_Of (Gen_T);
11473
11474 begin
11475 -- Some detailed comments would be useful here ???
11476
11477 return ((Base_Type (T) = Act_T
11478 or else Base_Type (T) = Base_Type (Act_T))
11479 and then Subtypes_Statically_Match (T, Act_T))
11480
11481 or else (Is_Class_Wide_Type (Gen_T)
11482 and then Is_Class_Wide_Type (Act_T)
11483 and then Subtypes_Match
11484 (Get_Instance_Of (Root_Type (Gen_T)),
11485 Root_Type (Act_T)))
11486
11487 or else
11488 (Ekind_In (Gen_T, E_Anonymous_Access_Subprogram_Type,
11489 E_Anonymous_Access_Type)
11490 and then Ekind (Act_T) = Ekind (Gen_T)
11491 and then Subtypes_Statically_Match
11492 (Designated_Type (Gen_T), Designated_Type (Act_T)));
11493 end Subtypes_Match;
11494
11495 -----------------------------------------
11496 -- Validate_Access_Subprogram_Instance --
11497 -----------------------------------------
11498
11499 procedure Validate_Access_Subprogram_Instance is
11500 begin
11501 if not Is_Access_Type (Act_T)
11502 or else Ekind (Designated_Type (Act_T)) /= E_Subprogram_Type
11503 then
11504 Error_Msg_NE
11505 ("expect access type in instantiation of &", Actual, Gen_T);
11506 Abandon_Instantiation (Actual);
11507 end if;
11508
11509 -- According to AI05-288, actuals for access_to_subprograms must be
11510 -- subtype conformant with the generic formal. Previous to AI05-288
11511 -- only mode conformance was required.
11512
11513 -- This is a binding interpretation that applies to previous versions
11514 -- of the language, no need to maintain previous weaker checks.
11515
11516 Check_Subtype_Conformant
11517 (Designated_Type (Act_T),
11518 Designated_Type (A_Gen_T),
11519 Actual,
11520 Get_Inst => True);
11521
11522 if Ekind (Base_Type (Act_T)) = E_Access_Protected_Subprogram_Type then
11523 if Ekind (A_Gen_T) = E_Access_Subprogram_Type then
11524 Error_Msg_NE
11525 ("protected access type not allowed for formal &",
11526 Actual, Gen_T);
11527 end if;
11528
11529 elsif Ekind (A_Gen_T) = E_Access_Protected_Subprogram_Type then
11530 Error_Msg_NE
11531 ("expect protected access type for formal &",
11532 Actual, Gen_T);
11533 end if;
11534
11535 -- If the formal has a specified convention (which in most cases
11536 -- will be StdCall) verify that the actual has the same convention.
11537
11538 if Has_Convention_Pragma (A_Gen_T)
11539 and then Convention (A_Gen_T) /= Convention (Act_T)
11540 then
11541 Error_Msg_Name_1 := Get_Convention_Name (Convention (A_Gen_T));
11542 Error_Msg_NE
11543 ("actual for formal & must have convention %", Actual, Gen_T);
11544 end if;
11545 end Validate_Access_Subprogram_Instance;
11546
11547 -----------------------------------
11548 -- Validate_Access_Type_Instance --
11549 -----------------------------------
11550
11551 procedure Validate_Access_Type_Instance is
11552 Desig_Type : constant Entity_Id :=
11553 Find_Actual_Type (Designated_Type (A_Gen_T), A_Gen_T);
11554 Desig_Act : Entity_Id;
11555
11556 begin
11557 if not Is_Access_Type (Act_T) then
11558 Error_Msg_NE
11559 ("expect access type in instantiation of &", Actual, Gen_T);
11560 Abandon_Instantiation (Actual);
11561 end if;
11562
11563 if Is_Access_Constant (A_Gen_T) then
11564 if not Is_Access_Constant (Act_T) then
11565 Error_Msg_N
11566 ("actual type must be access-to-constant type", Actual);
11567 Abandon_Instantiation (Actual);
11568 end if;
11569 else
11570 if Is_Access_Constant (Act_T) then
11571 Error_Msg_N
11572 ("actual type must be access-to-variable type", Actual);
11573 Abandon_Instantiation (Actual);
11574
11575 elsif Ekind (A_Gen_T) = E_General_Access_Type
11576 and then Ekind (Base_Type (Act_T)) /= E_General_Access_Type
11577 then
11578 Error_Msg_N -- CODEFIX
11579 ("actual must be general access type!", Actual);
11580 Error_Msg_NE -- CODEFIX
11581 ("add ALL to }!", Actual, Act_T);
11582 Abandon_Instantiation (Actual);
11583 end if;
11584 end if;
11585
11586 -- The designated subtypes, that is to say the subtypes introduced
11587 -- by an access type declaration (and not by a subtype declaration)
11588 -- must match.
11589
11590 Desig_Act := Designated_Type (Base_Type (Act_T));
11591
11592 -- The designated type may have been introduced through a limited_
11593 -- with clause, in which case retrieve the non-limited view. This
11594 -- applies to incomplete types as well as to class-wide types.
11595
11596 if From_Limited_With (Desig_Act) then
11597 Desig_Act := Available_View (Desig_Act);
11598 end if;
11599
11600 if not Subtypes_Match (Desig_Type, Desig_Act) then
11601 Error_Msg_NE
11602 ("designated type of actual does not match that of formal &",
11603 Actual, Gen_T);
11604
11605 if not Predicates_Match (Desig_Type, Desig_Act) then
11606 Error_Msg_N ("\predicates do not match", Actual);
11607 end if;
11608
11609 Abandon_Instantiation (Actual);
11610
11611 elsif Is_Access_Type (Designated_Type (Act_T))
11612 and then Is_Constrained (Designated_Type (Designated_Type (Act_T)))
11613 /=
11614 Is_Constrained (Designated_Type (Desig_Type))
11615 then
11616 Error_Msg_NE
11617 ("designated type of actual does not match that of formal &",
11618 Actual, Gen_T);
11619
11620 if not Predicates_Match (Desig_Type, Desig_Act) then
11621 Error_Msg_N ("\predicates do not match", Actual);
11622 end if;
11623
11624 Abandon_Instantiation (Actual);
11625 end if;
11626
11627 -- Ada 2005: null-exclusion indicators of the two types must agree
11628
11629 if Can_Never_Be_Null (A_Gen_T) /= Can_Never_Be_Null (Act_T) then
11630 Error_Msg_NE
11631 ("non null exclusion of actual and formal & do not match",
11632 Actual, Gen_T);
11633 end if;
11634 end Validate_Access_Type_Instance;
11635
11636 ----------------------------------
11637 -- Validate_Array_Type_Instance --
11638 ----------------------------------
11639
11640 procedure Validate_Array_Type_Instance is
11641 I1 : Node_Id;
11642 I2 : Node_Id;
11643 T2 : Entity_Id;
11644
11645 function Formal_Dimensions return Nat;
11646 -- Count number of dimensions in array type formal
11647
11648 -----------------------
11649 -- Formal_Dimensions --
11650 -----------------------
11651
11652 function Formal_Dimensions return Nat is
11653 Num : Nat := 0;
11654 Index : Node_Id;
11655
11656 begin
11657 if Nkind (Def) = N_Constrained_Array_Definition then
11658 Index := First (Discrete_Subtype_Definitions (Def));
11659 else
11660 Index := First (Subtype_Marks (Def));
11661 end if;
11662
11663 while Present (Index) loop
11664 Num := Num + 1;
11665 Next_Index (Index);
11666 end loop;
11667
11668 return Num;
11669 end Formal_Dimensions;
11670
11671 -- Start of processing for Validate_Array_Type_Instance
11672
11673 begin
11674 if not Is_Array_Type (Act_T) then
11675 Error_Msg_NE
11676 ("expect array type in instantiation of &", Actual, Gen_T);
11677 Abandon_Instantiation (Actual);
11678
11679 elsif Nkind (Def) = N_Constrained_Array_Definition then
11680 if not (Is_Constrained (Act_T)) then
11681 Error_Msg_NE
11682 ("expect constrained array in instantiation of &",
11683 Actual, Gen_T);
11684 Abandon_Instantiation (Actual);
11685 end if;
11686
11687 else
11688 if Is_Constrained (Act_T) then
11689 Error_Msg_NE
11690 ("expect unconstrained array in instantiation of &",
11691 Actual, Gen_T);
11692 Abandon_Instantiation (Actual);
11693 end if;
11694 end if;
11695
11696 if Formal_Dimensions /= Number_Dimensions (Act_T) then
11697 Error_Msg_NE
11698 ("dimensions of actual do not match formal &", Actual, Gen_T);
11699 Abandon_Instantiation (Actual);
11700 end if;
11701
11702 I1 := First_Index (A_Gen_T);
11703 I2 := First_Index (Act_T);
11704 for J in 1 .. Formal_Dimensions loop
11705
11706 -- If the indexes of the actual were given by a subtype_mark,
11707 -- the index was transformed into a range attribute. Retrieve
11708 -- the original type mark for checking.
11709
11710 if Is_Entity_Name (Original_Node (I2)) then
11711 T2 := Entity (Original_Node (I2));
11712 else
11713 T2 := Etype (I2);
11714 end if;
11715
11716 if not Subtypes_Match
11717 (Find_Actual_Type (Etype (I1), A_Gen_T), T2)
11718 then
11719 Error_Msg_NE
11720 ("index types of actual do not match those of formal &",
11721 Actual, Gen_T);
11722 Abandon_Instantiation (Actual);
11723 end if;
11724
11725 Next_Index (I1);
11726 Next_Index (I2);
11727 end loop;
11728
11729 -- Check matching subtypes. Note that there are complex visibility
11730 -- issues when the generic is a child unit and some aspect of the
11731 -- generic type is declared in a parent unit of the generic. We do
11732 -- the test to handle this special case only after a direct check
11733 -- for static matching has failed. The case where both the component
11734 -- type and the array type are separate formals, and the component
11735 -- type is a private view may also require special checking in
11736 -- Subtypes_Match.
11737
11738 if Subtypes_Match
11739 (Component_Type (A_Gen_T), Component_Type (Act_T))
11740 or else
11741 Subtypes_Match
11742 (Find_Actual_Type (Component_Type (A_Gen_T), A_Gen_T),
11743 Component_Type (Act_T))
11744 then
11745 null;
11746 else
11747 Error_Msg_NE
11748 ("component subtype of actual does not match that of formal &",
11749 Actual, Gen_T);
11750 Abandon_Instantiation (Actual);
11751 end if;
11752
11753 if Has_Aliased_Components (A_Gen_T)
11754 and then not Has_Aliased_Components (Act_T)
11755 then
11756 Error_Msg_NE
11757 ("actual must have aliased components to match formal type &",
11758 Actual, Gen_T);
11759 end if;
11760 end Validate_Array_Type_Instance;
11761
11762 -----------------------------------------------
11763 -- Validate_Derived_Interface_Type_Instance --
11764 -----------------------------------------------
11765
11766 procedure Validate_Derived_Interface_Type_Instance is
11767 Par : constant Entity_Id := Entity (Subtype_Indication (Def));
11768 Elmt : Elmt_Id;
11769
11770 begin
11771 -- First apply interface instance checks
11772
11773 Validate_Interface_Type_Instance;
11774
11775 -- Verify that immediate parent interface is an ancestor of
11776 -- the actual.
11777
11778 if Present (Par)
11779 and then not Interface_Present_In_Ancestor (Act_T, Par)
11780 then
11781 Error_Msg_NE
11782 ("interface actual must include progenitor&", Actual, Par);
11783 end if;
11784
11785 -- Now verify that the actual includes all other ancestors of
11786 -- the formal.
11787
11788 Elmt := First_Elmt (Interfaces (A_Gen_T));
11789 while Present (Elmt) loop
11790 if not Interface_Present_In_Ancestor
11791 (Act_T, Get_Instance_Of (Node (Elmt)))
11792 then
11793 Error_Msg_NE
11794 ("interface actual must include progenitor&",
11795 Actual, Node (Elmt));
11796 end if;
11797
11798 Next_Elmt (Elmt);
11799 end loop;
11800 end Validate_Derived_Interface_Type_Instance;
11801
11802 ------------------------------------
11803 -- Validate_Derived_Type_Instance --
11804 ------------------------------------
11805
11806 procedure Validate_Derived_Type_Instance is
11807 Actual_Discr : Entity_Id;
11808 Ancestor_Discr : Entity_Id;
11809
11810 begin
11811 -- If the parent type in the generic declaration is itself a previous
11812 -- formal type, then it is local to the generic and absent from the
11813 -- analyzed generic definition. In that case the ancestor is the
11814 -- instance of the formal (which must have been instantiated
11815 -- previously), unless the ancestor is itself a formal derived type.
11816 -- In this latter case (which is the subject of Corrigendum 8652/0038
11817 -- (AI-202) the ancestor of the formals is the ancestor of its
11818 -- parent. Otherwise, the analyzed generic carries the parent type.
11819 -- If the parent type is defined in a previous formal package, then
11820 -- the scope of that formal package is that of the generic type
11821 -- itself, and it has already been mapped into the corresponding type
11822 -- in the actual package.
11823
11824 -- Common case: parent type defined outside of the generic
11825
11826 if Is_Entity_Name (Subtype_Mark (Def))
11827 and then Present (Entity (Subtype_Mark (Def)))
11828 then
11829 Ancestor := Get_Instance_Of (Entity (Subtype_Mark (Def)));
11830
11831 -- Check whether parent is defined in a previous formal package
11832
11833 elsif
11834 Scope (Scope (Base_Type (Etype (A_Gen_T)))) = Scope (A_Gen_T)
11835 then
11836 Ancestor :=
11837 Get_Instance_Of (Base_Type (Etype (A_Gen_T)));
11838
11839 -- The type may be a local derivation, or a type extension of a
11840 -- previous formal, or of a formal of a parent package.
11841
11842 elsif Is_Derived_Type (Get_Instance_Of (A_Gen_T))
11843 or else
11844 Ekind (Get_Instance_Of (A_Gen_T)) = E_Record_Type_With_Private
11845 then
11846 -- Check whether the parent is another derived formal type in the
11847 -- same generic unit.
11848
11849 if Etype (A_Gen_T) /= A_Gen_T
11850 and then Is_Generic_Type (Etype (A_Gen_T))
11851 and then Scope (Etype (A_Gen_T)) = Scope (A_Gen_T)
11852 and then Etype (Etype (A_Gen_T)) /= Etype (A_Gen_T)
11853 then
11854 -- Locate ancestor of parent from the subtype declaration
11855 -- created for the actual.
11856
11857 declare
11858 Decl : Node_Id;
11859
11860 begin
11861 Decl := First (Actual_Decls);
11862 while Present (Decl) loop
11863 if Nkind (Decl) = N_Subtype_Declaration
11864 and then Chars (Defining_Identifier (Decl)) =
11865 Chars (Etype (A_Gen_T))
11866 then
11867 Ancestor := Generic_Parent_Type (Decl);
11868 exit;
11869 else
11870 Next (Decl);
11871 end if;
11872 end loop;
11873 end;
11874
11875 pragma Assert (Present (Ancestor));
11876
11877 -- The ancestor itself may be a previous formal that has been
11878 -- instantiated.
11879
11880 Ancestor := Get_Instance_Of (Ancestor);
11881
11882 else
11883 Ancestor :=
11884 Get_Instance_Of (Base_Type (Get_Instance_Of (A_Gen_T)));
11885 end if;
11886
11887 -- Check whether parent is a previous formal of the current generic
11888
11889 elsif Is_Derived_Type (A_Gen_T)
11890 and then Is_Generic_Type (Etype (A_Gen_T))
11891 and then Scope (A_Gen_T) = Scope (Etype (A_Gen_T))
11892 then
11893 Ancestor := Get_Instance_Of (First_Subtype (Etype (A_Gen_T)));
11894
11895 -- An unusual case: the actual is a type declared in a parent unit,
11896 -- but is not a formal type so there is no instance_of for it.
11897 -- Retrieve it by analyzing the record extension.
11898
11899 elsif Is_Child_Unit (Scope (A_Gen_T))
11900 and then In_Open_Scopes (Scope (Act_T))
11901 and then Is_Generic_Instance (Scope (Act_T))
11902 then
11903 Analyze (Subtype_Mark (Def));
11904 Ancestor := Entity (Subtype_Mark (Def));
11905
11906 else
11907 Ancestor := Get_Instance_Of (Etype (Base_Type (A_Gen_T)));
11908 end if;
11909
11910 -- If the formal derived type has pragma Preelaborable_Initialization
11911 -- then the actual type must have preelaborable initialization.
11912
11913 if Known_To_Have_Preelab_Init (A_Gen_T)
11914 and then not Has_Preelaborable_Initialization (Act_T)
11915 then
11916 Error_Msg_NE
11917 ("actual for & must have preelaborable initialization",
11918 Actual, Gen_T);
11919 end if;
11920
11921 -- Ada 2005 (AI-251)
11922
11923 if Ada_Version >= Ada_2005 and then Is_Interface (Ancestor) then
11924 if not Interface_Present_In_Ancestor (Act_T, Ancestor) then
11925 Error_Msg_NE
11926 ("(Ada 2005) expected type implementing & in instantiation",
11927 Actual, Ancestor);
11928 end if;
11929
11930 -- Finally verify that the (instance of) the ancestor is an ancestor
11931 -- of the actual.
11932
11933 elsif not Is_Ancestor (Base_Type (Ancestor), Act_T) then
11934 Error_Msg_NE
11935 ("expect type derived from & in instantiation",
11936 Actual, First_Subtype (Ancestor));
11937 Abandon_Instantiation (Actual);
11938 end if;
11939
11940 -- Ada 2005 (AI-443): Synchronized formal derived type checks. Note
11941 -- that the formal type declaration has been rewritten as a private
11942 -- extension.
11943
11944 if Ada_Version >= Ada_2005
11945 and then Nkind (Parent (A_Gen_T)) = N_Private_Extension_Declaration
11946 and then Synchronized_Present (Parent (A_Gen_T))
11947 then
11948 -- The actual must be a synchronized tagged type
11949
11950 if not Is_Tagged_Type (Act_T) then
11951 Error_Msg_N
11952 ("actual of synchronized type must be tagged", Actual);
11953 Abandon_Instantiation (Actual);
11954
11955 elsif Nkind (Parent (Act_T)) = N_Full_Type_Declaration
11956 and then Nkind (Type_Definition (Parent (Act_T))) =
11957 N_Derived_Type_Definition
11958 and then not Synchronized_Present
11959 (Type_Definition (Parent (Act_T)))
11960 then
11961 Error_Msg_N
11962 ("actual of synchronized type must be synchronized", Actual);
11963 Abandon_Instantiation (Actual);
11964 end if;
11965 end if;
11966
11967 -- Perform atomic/volatile checks (RM C.6(12)). Note that AI05-0218-1
11968 -- removes the second instance of the phrase "or allow pass by copy".
11969
11970 if Is_Atomic (Act_T) and then not Is_Atomic (Ancestor) then
11971 Error_Msg_N
11972 ("cannot have atomic actual type for non-atomic formal type",
11973 Actual);
11974
11975 elsif Is_Volatile (Act_T) and then not Is_Volatile (Ancestor) then
11976 Error_Msg_N
11977 ("cannot have volatile actual type for non-volatile formal type",
11978 Actual);
11979 end if;
11980
11981 -- It should not be necessary to check for unknown discriminants on
11982 -- Formal, but for some reason Has_Unknown_Discriminants is false for
11983 -- A_Gen_T, so Is_Definite_Subtype incorrectly returns True. This
11984 -- needs fixing. ???
11985
11986 if Is_Definite_Subtype (A_Gen_T)
11987 and then not Unknown_Discriminants_Present (Formal)
11988 and then not Is_Definite_Subtype (Act_T)
11989 then
11990 Error_Msg_N ("actual subtype must be constrained", Actual);
11991 Abandon_Instantiation (Actual);
11992 end if;
11993
11994 if not Unknown_Discriminants_Present (Formal) then
11995 if Is_Constrained (Ancestor) then
11996 if not Is_Constrained (Act_T) then
11997 Error_Msg_N ("actual subtype must be constrained", Actual);
11998 Abandon_Instantiation (Actual);
11999 end if;
12000
12001 -- Ancestor is unconstrained, Check if generic formal and actual
12002 -- agree on constrainedness. The check only applies to array types
12003 -- and discriminated types.
12004
12005 elsif Is_Constrained (Act_T) then
12006 if Ekind (Ancestor) = E_Access_Type
12007 or else (not Is_Constrained (A_Gen_T)
12008 and then Is_Composite_Type (A_Gen_T))
12009 then
12010 Error_Msg_N ("actual subtype must be unconstrained", Actual);
12011 Abandon_Instantiation (Actual);
12012 end if;
12013
12014 -- A class-wide type is only allowed if the formal has unknown
12015 -- discriminants.
12016
12017 elsif Is_Class_Wide_Type (Act_T)
12018 and then not Has_Unknown_Discriminants (Ancestor)
12019 then
12020 Error_Msg_NE
12021 ("actual for & cannot be a class-wide type", Actual, Gen_T);
12022 Abandon_Instantiation (Actual);
12023
12024 -- Otherwise, the formal and actual must have the same number
12025 -- of discriminants and each discriminant of the actual must
12026 -- correspond to a discriminant of the formal.
12027
12028 elsif Has_Discriminants (Act_T)
12029 and then not Has_Unknown_Discriminants (Act_T)
12030 and then Has_Discriminants (Ancestor)
12031 then
12032 Actual_Discr := First_Discriminant (Act_T);
12033 Ancestor_Discr := First_Discriminant (Ancestor);
12034 while Present (Actual_Discr)
12035 and then Present (Ancestor_Discr)
12036 loop
12037 if Base_Type (Act_T) /= Base_Type (Ancestor) and then
12038 No (Corresponding_Discriminant (Actual_Discr))
12039 then
12040 Error_Msg_NE
12041 ("discriminant & does not correspond "
12042 & "to ancestor discriminant", Actual, Actual_Discr);
12043 Abandon_Instantiation (Actual);
12044 end if;
12045
12046 Next_Discriminant (Actual_Discr);
12047 Next_Discriminant (Ancestor_Discr);
12048 end loop;
12049
12050 if Present (Actual_Discr) or else Present (Ancestor_Discr) then
12051 Error_Msg_NE
12052 ("actual for & must have same number of discriminants",
12053 Actual, Gen_T);
12054 Abandon_Instantiation (Actual);
12055 end if;
12056
12057 -- This case should be caught by the earlier check for
12058 -- constrainedness, but the check here is added for completeness.
12059
12060 elsif Has_Discriminants (Act_T)
12061 and then not Has_Unknown_Discriminants (Act_T)
12062 then
12063 Error_Msg_NE
12064 ("actual for & must not have discriminants", Actual, Gen_T);
12065 Abandon_Instantiation (Actual);
12066
12067 elsif Has_Discriminants (Ancestor) then
12068 Error_Msg_NE
12069 ("actual for & must have known discriminants", Actual, Gen_T);
12070 Abandon_Instantiation (Actual);
12071 end if;
12072
12073 if not Subtypes_Statically_Compatible
12074 (Act_T, Ancestor, Formal_Derived_Matching => True)
12075 then
12076 Error_Msg_N
12077 ("constraint on actual is incompatible with formal", Actual);
12078 Abandon_Instantiation (Actual);
12079 end if;
12080 end if;
12081
12082 -- If the formal and actual types are abstract, check that there
12083 -- are no abstract primitives of the actual type that correspond to
12084 -- nonabstract primitives of the formal type (second sentence of
12085 -- RM95 3.9.3(9)).
12086
12087 if Is_Abstract_Type (A_Gen_T) and then Is_Abstract_Type (Act_T) then
12088 Check_Abstract_Primitives : declare
12089 Gen_Prims : constant Elist_Id :=
12090 Primitive_Operations (A_Gen_T);
12091 Gen_Elmt : Elmt_Id;
12092 Gen_Subp : Entity_Id;
12093 Anc_Subp : Entity_Id;
12094 Anc_Formal : Entity_Id;
12095 Anc_F_Type : Entity_Id;
12096
12097 Act_Prims : constant Elist_Id := Primitive_Operations (Act_T);
12098 Act_Elmt : Elmt_Id;
12099 Act_Subp : Entity_Id;
12100 Act_Formal : Entity_Id;
12101 Act_F_Type : Entity_Id;
12102
12103 Subprograms_Correspond : Boolean;
12104
12105 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean;
12106 -- Returns true if T2 is derived directly or indirectly from
12107 -- T1, including derivations from interfaces. T1 and T2 are
12108 -- required to be specific tagged base types.
12109
12110 ------------------------
12111 -- Is_Tagged_Ancestor --
12112 ------------------------
12113
12114 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean
12115 is
12116 Intfc_Elmt : Elmt_Id;
12117
12118 begin
12119 -- The predicate is satisfied if the types are the same
12120
12121 if T1 = T2 then
12122 return True;
12123
12124 -- If we've reached the top of the derivation chain then
12125 -- we know that T1 is not an ancestor of T2.
12126
12127 elsif Etype (T2) = T2 then
12128 return False;
12129
12130 -- Proceed to check T2's immediate parent
12131
12132 elsif Is_Ancestor (T1, Base_Type (Etype (T2))) then
12133 return True;
12134
12135 -- Finally, check to see if T1 is an ancestor of any of T2's
12136 -- progenitors.
12137
12138 else
12139 Intfc_Elmt := First_Elmt (Interfaces (T2));
12140 while Present (Intfc_Elmt) loop
12141 if Is_Ancestor (T1, Node (Intfc_Elmt)) then
12142 return True;
12143 end if;
12144
12145 Next_Elmt (Intfc_Elmt);
12146 end loop;
12147 end if;
12148
12149 return False;
12150 end Is_Tagged_Ancestor;
12151
12152 -- Start of processing for Check_Abstract_Primitives
12153
12154 begin
12155 -- Loop over all of the formal derived type's primitives
12156
12157 Gen_Elmt := First_Elmt (Gen_Prims);
12158 while Present (Gen_Elmt) loop
12159 Gen_Subp := Node (Gen_Elmt);
12160
12161 -- If the primitive of the formal is not abstract, then
12162 -- determine whether there is a corresponding primitive of
12163 -- the actual type that's abstract.
12164
12165 if not Is_Abstract_Subprogram (Gen_Subp) then
12166 Act_Elmt := First_Elmt (Act_Prims);
12167 while Present (Act_Elmt) loop
12168 Act_Subp := Node (Act_Elmt);
12169
12170 -- If we find an abstract primitive of the actual,
12171 -- then we need to test whether it corresponds to the
12172 -- subprogram from which the generic formal primitive
12173 -- is inherited.
12174
12175 if Is_Abstract_Subprogram (Act_Subp) then
12176 Anc_Subp := Alias (Gen_Subp);
12177
12178 -- Test whether we have a corresponding primitive
12179 -- by comparing names, kinds, formal types, and
12180 -- result types.
12181
12182 if Chars (Anc_Subp) = Chars (Act_Subp)
12183 and then Ekind (Anc_Subp) = Ekind (Act_Subp)
12184 then
12185 Anc_Formal := First_Formal (Anc_Subp);
12186 Act_Formal := First_Formal (Act_Subp);
12187 while Present (Anc_Formal)
12188 and then Present (Act_Formal)
12189 loop
12190 Anc_F_Type := Etype (Anc_Formal);
12191 Act_F_Type := Etype (Act_Formal);
12192
12193 if Ekind (Anc_F_Type) =
12194 E_Anonymous_Access_Type
12195 then
12196 Anc_F_Type := Designated_Type (Anc_F_Type);
12197
12198 if Ekind (Act_F_Type) =
12199 E_Anonymous_Access_Type
12200 then
12201 Act_F_Type :=
12202 Designated_Type (Act_F_Type);
12203 else
12204 exit;
12205 end if;
12206
12207 elsif
12208 Ekind (Act_F_Type) = E_Anonymous_Access_Type
12209 then
12210 exit;
12211 end if;
12212
12213 Anc_F_Type := Base_Type (Anc_F_Type);
12214 Act_F_Type := Base_Type (Act_F_Type);
12215
12216 -- If the formal is controlling, then the
12217 -- the type of the actual primitive's formal
12218 -- must be derived directly or indirectly
12219 -- from the type of the ancestor primitive's
12220 -- formal.
12221
12222 if Is_Controlling_Formal (Anc_Formal) then
12223 if not Is_Tagged_Ancestor
12224 (Anc_F_Type, Act_F_Type)
12225 then
12226 exit;
12227 end if;
12228
12229 -- Otherwise the types of the formals must
12230 -- be the same.
12231
12232 elsif Anc_F_Type /= Act_F_Type then
12233 exit;
12234 end if;
12235
12236 Next_Entity (Anc_Formal);
12237 Next_Entity (Act_Formal);
12238 end loop;
12239
12240 -- If we traversed through all of the formals
12241 -- then so far the subprograms correspond, so
12242 -- now check that any result types correspond.
12243
12244 if No (Anc_Formal) and then No (Act_Formal) then
12245 Subprograms_Correspond := True;
12246
12247 if Ekind (Act_Subp) = E_Function then
12248 Anc_F_Type := Etype (Anc_Subp);
12249 Act_F_Type := Etype (Act_Subp);
12250
12251 if Ekind (Anc_F_Type) =
12252 E_Anonymous_Access_Type
12253 then
12254 Anc_F_Type :=
12255 Designated_Type (Anc_F_Type);
12256
12257 if Ekind (Act_F_Type) =
12258 E_Anonymous_Access_Type
12259 then
12260 Act_F_Type :=
12261 Designated_Type (Act_F_Type);
12262 else
12263 Subprograms_Correspond := False;
12264 end if;
12265
12266 elsif
12267 Ekind (Act_F_Type)
12268 = E_Anonymous_Access_Type
12269 then
12270 Subprograms_Correspond := False;
12271 end if;
12272
12273 Anc_F_Type := Base_Type (Anc_F_Type);
12274 Act_F_Type := Base_Type (Act_F_Type);
12275
12276 -- Now either the result types must be
12277 -- the same or, if the result type is
12278 -- controlling, the result type of the
12279 -- actual primitive must descend from the
12280 -- result type of the ancestor primitive.
12281
12282 if Subprograms_Correspond
12283 and then Anc_F_Type /= Act_F_Type
12284 and then
12285 Has_Controlling_Result (Anc_Subp)
12286 and then not Is_Tagged_Ancestor
12287 (Anc_F_Type, Act_F_Type)
12288 then
12289 Subprograms_Correspond := False;
12290 end if;
12291 end if;
12292
12293 -- Found a matching subprogram belonging to
12294 -- formal ancestor type, so actual subprogram
12295 -- corresponds and this violates 3.9.3(9).
12296
12297 if Subprograms_Correspond then
12298 Error_Msg_NE
12299 ("abstract subprogram & overrides "
12300 & "nonabstract subprogram of ancestor",
12301 Actual, Act_Subp);
12302 end if;
12303 end if;
12304 end if;
12305 end if;
12306
12307 Next_Elmt (Act_Elmt);
12308 end loop;
12309 end if;
12310
12311 Next_Elmt (Gen_Elmt);
12312 end loop;
12313 end Check_Abstract_Primitives;
12314 end if;
12315
12316 -- Verify that limitedness matches. If parent is a limited
12317 -- interface then the generic formal is not unless declared
12318 -- explicitly so. If not declared limited, the actual cannot be
12319 -- limited (see AI05-0087).
12320
12321 -- Even though this AI is a binding interpretation, we enable the
12322 -- check only in Ada 2012 mode, because this improper construct
12323 -- shows up in user code and in existing B-tests.
12324
12325 if Is_Limited_Type (Act_T)
12326 and then not Is_Limited_Type (A_Gen_T)
12327 and then Ada_Version >= Ada_2012
12328 then
12329 if In_Instance then
12330 null;
12331 else
12332 Error_Msg_NE
12333 ("actual for non-limited & cannot be a limited type",
12334 Actual, Gen_T);
12335 Explain_Limited_Type (Act_T, Actual);
12336 Abandon_Instantiation (Actual);
12337 end if;
12338 end if;
12339 end Validate_Derived_Type_Instance;
12340
12341 ----------------------------------------
12342 -- Validate_Discriminated_Formal_Type --
12343 ----------------------------------------
12344
12345 procedure Validate_Discriminated_Formal_Type is
12346 Formal_Discr : Entity_Id;
12347 Actual_Discr : Entity_Id;
12348 Formal_Subt : Entity_Id;
12349
12350 begin
12351 if Has_Discriminants (A_Gen_T) then
12352 if not Has_Discriminants (Act_T) then
12353 Error_Msg_NE
12354 ("actual for & must have discriminants", Actual, Gen_T);
12355 Abandon_Instantiation (Actual);
12356
12357 elsif Is_Constrained (Act_T) then
12358 Error_Msg_NE
12359 ("actual for & must be unconstrained", Actual, Gen_T);
12360 Abandon_Instantiation (Actual);
12361
12362 else
12363 Formal_Discr := First_Discriminant (A_Gen_T);
12364 Actual_Discr := First_Discriminant (Act_T);
12365 while Formal_Discr /= Empty loop
12366 if Actual_Discr = Empty then
12367 Error_Msg_NE
12368 ("discriminants on actual do not match formal",
12369 Actual, Gen_T);
12370 Abandon_Instantiation (Actual);
12371 end if;
12372
12373 Formal_Subt := Get_Instance_Of (Etype (Formal_Discr));
12374
12375 -- Access discriminants match if designated types do
12376
12377 if Ekind (Base_Type (Formal_Subt)) = E_Anonymous_Access_Type
12378 and then (Ekind (Base_Type (Etype (Actual_Discr)))) =
12379 E_Anonymous_Access_Type
12380 and then
12381 Get_Instance_Of
12382 (Designated_Type (Base_Type (Formal_Subt))) =
12383 Designated_Type (Base_Type (Etype (Actual_Discr)))
12384 then
12385 null;
12386
12387 elsif Base_Type (Formal_Subt) /=
12388 Base_Type (Etype (Actual_Discr))
12389 then
12390 Error_Msg_NE
12391 ("types of actual discriminants must match formal",
12392 Actual, Gen_T);
12393 Abandon_Instantiation (Actual);
12394
12395 elsif not Subtypes_Statically_Match
12396 (Formal_Subt, Etype (Actual_Discr))
12397 and then Ada_Version >= Ada_95
12398 then
12399 Error_Msg_NE
12400 ("subtypes of actual discriminants must match formal",
12401 Actual, Gen_T);
12402 Abandon_Instantiation (Actual);
12403 end if;
12404
12405 Next_Discriminant (Formal_Discr);
12406 Next_Discriminant (Actual_Discr);
12407 end loop;
12408
12409 if Actual_Discr /= Empty then
12410 Error_Msg_NE
12411 ("discriminants on actual do not match formal",
12412 Actual, Gen_T);
12413 Abandon_Instantiation (Actual);
12414 end if;
12415 end if;
12416 end if;
12417 end Validate_Discriminated_Formal_Type;
12418
12419 ---------------------------------------
12420 -- Validate_Incomplete_Type_Instance --
12421 ---------------------------------------
12422
12423 procedure Validate_Incomplete_Type_Instance is
12424 begin
12425 if not Is_Tagged_Type (Act_T)
12426 and then Is_Tagged_Type (A_Gen_T)
12427 then
12428 Error_Msg_NE
12429 ("actual for & must be a tagged type", Actual, Gen_T);
12430 end if;
12431
12432 Validate_Discriminated_Formal_Type;
12433 end Validate_Incomplete_Type_Instance;
12434
12435 --------------------------------------
12436 -- Validate_Interface_Type_Instance --
12437 --------------------------------------
12438
12439 procedure Validate_Interface_Type_Instance is
12440 begin
12441 if not Is_Interface (Act_T) then
12442 Error_Msg_NE
12443 ("actual for formal interface type must be an interface",
12444 Actual, Gen_T);
12445
12446 elsif Is_Limited_Type (Act_T) /= Is_Limited_Type (A_Gen_T)
12447 or else Is_Task_Interface (A_Gen_T) /= Is_Task_Interface (Act_T)
12448 or else Is_Protected_Interface (A_Gen_T) /=
12449 Is_Protected_Interface (Act_T)
12450 or else Is_Synchronized_Interface (A_Gen_T) /=
12451 Is_Synchronized_Interface (Act_T)
12452 then
12453 Error_Msg_NE
12454 ("actual for interface& does not match (RM 12.5.5(4))",
12455 Actual, Gen_T);
12456 end if;
12457 end Validate_Interface_Type_Instance;
12458
12459 ------------------------------------
12460 -- Validate_Private_Type_Instance --
12461 ------------------------------------
12462
12463 procedure Validate_Private_Type_Instance is
12464 begin
12465 if Is_Limited_Type (Act_T)
12466 and then not Is_Limited_Type (A_Gen_T)
12467 then
12468 if In_Instance then
12469 null;
12470 else
12471 Error_Msg_NE
12472 ("actual for non-limited & cannot be a limited type", Actual,
12473 Gen_T);
12474 Explain_Limited_Type (Act_T, Actual);
12475 Abandon_Instantiation (Actual);
12476 end if;
12477
12478 elsif Known_To_Have_Preelab_Init (A_Gen_T)
12479 and then not Has_Preelaborable_Initialization (Act_T)
12480 then
12481 Error_Msg_NE
12482 ("actual for & must have preelaborable initialization", Actual,
12483 Gen_T);
12484
12485 elsif not Is_Definite_Subtype (Act_T)
12486 and then Is_Definite_Subtype (A_Gen_T)
12487 and then Ada_Version >= Ada_95
12488 then
12489 Error_Msg_NE
12490 ("actual for & must be a definite subtype", Actual, Gen_T);
12491
12492 elsif not Is_Tagged_Type (Act_T)
12493 and then Is_Tagged_Type (A_Gen_T)
12494 then
12495 Error_Msg_NE
12496 ("actual for & must be a tagged type", Actual, Gen_T);
12497 end if;
12498
12499 Validate_Discriminated_Formal_Type;
12500 Ancestor := Gen_T;
12501 end Validate_Private_Type_Instance;
12502
12503 -- Start of processing for Instantiate_Type
12504
12505 begin
12506 if Get_Instance_Of (A_Gen_T) /= A_Gen_T then
12507 Error_Msg_N ("duplicate instantiation of generic type", Actual);
12508 return New_List (Error);
12509
12510 elsif not Is_Entity_Name (Actual)
12511 or else not Is_Type (Entity (Actual))
12512 then
12513 Error_Msg_NE
12514 ("expect valid subtype mark to instantiate &", Actual, Gen_T);
12515 Abandon_Instantiation (Actual);
12516
12517 else
12518 Act_T := Entity (Actual);
12519
12520 -- Ada 2005 (AI-216): An Unchecked_Union subtype shall only be passed
12521 -- as a generic actual parameter if the corresponding formal type
12522 -- does not have a known_discriminant_part, or is a formal derived
12523 -- type that is an Unchecked_Union type.
12524
12525 if Is_Unchecked_Union (Base_Type (Act_T)) then
12526 if not Has_Discriminants (A_Gen_T)
12527 or else (Is_Derived_Type (A_Gen_T)
12528 and then Is_Unchecked_Union (A_Gen_T))
12529 then
12530 null;
12531 else
12532 Error_Msg_N ("unchecked union cannot be the actual for a "
12533 & "discriminated formal type", Act_T);
12534
12535 end if;
12536 end if;
12537
12538 -- Deal with fixed/floating restrictions
12539
12540 if Is_Floating_Point_Type (Act_T) then
12541 Check_Restriction (No_Floating_Point, Actual);
12542 elsif Is_Fixed_Point_Type (Act_T) then
12543 Check_Restriction (No_Fixed_Point, Actual);
12544 end if;
12545
12546 -- Deal with error of using incomplete type as generic actual.
12547 -- This includes limited views of a type, even if the non-limited
12548 -- view may be available.
12549
12550 if Ekind (Act_T) = E_Incomplete_Type
12551 or else (Is_Class_Wide_Type (Act_T)
12552 and then Ekind (Root_Type (Act_T)) = E_Incomplete_Type)
12553 then
12554 -- If the formal is an incomplete type, the actual can be
12555 -- incomplete as well.
12556
12557 if Ekind (A_Gen_T) = E_Incomplete_Type then
12558 null;
12559
12560 elsif Is_Class_Wide_Type (Act_T)
12561 or else No (Full_View (Act_T))
12562 then
12563 Error_Msg_N ("premature use of incomplete type", Actual);
12564 Abandon_Instantiation (Actual);
12565 else
12566 Act_T := Full_View (Act_T);
12567 Set_Entity (Actual, Act_T);
12568
12569 if Has_Private_Component (Act_T) then
12570 Error_Msg_N
12571 ("premature use of type with private component", Actual);
12572 end if;
12573 end if;
12574
12575 -- Deal with error of premature use of private type as generic actual
12576
12577 elsif Is_Private_Type (Act_T)
12578 and then Is_Private_Type (Base_Type (Act_T))
12579 and then not Is_Generic_Type (Act_T)
12580 and then not Is_Derived_Type (Act_T)
12581 and then No (Full_View (Root_Type (Act_T)))
12582 then
12583 -- If the formal is an incomplete type, the actual can be
12584 -- private or incomplete as well.
12585
12586 if Ekind (A_Gen_T) = E_Incomplete_Type then
12587 null;
12588 else
12589 Error_Msg_N ("premature use of private type", Actual);
12590 end if;
12591
12592 elsif Has_Private_Component (Act_T) then
12593 Error_Msg_N
12594 ("premature use of type with private component", Actual);
12595 end if;
12596
12597 Set_Instance_Of (A_Gen_T, Act_T);
12598
12599 -- If the type is generic, the class-wide type may also be used
12600
12601 if Is_Tagged_Type (A_Gen_T)
12602 and then Is_Tagged_Type (Act_T)
12603 and then not Is_Class_Wide_Type (A_Gen_T)
12604 then
12605 Set_Instance_Of (Class_Wide_Type (A_Gen_T),
12606 Class_Wide_Type (Act_T));
12607 end if;
12608
12609 if not Is_Abstract_Type (A_Gen_T)
12610 and then Is_Abstract_Type (Act_T)
12611 then
12612 Error_Msg_N
12613 ("actual of non-abstract formal cannot be abstract", Actual);
12614 end if;
12615
12616 -- A generic scalar type is a first subtype for which we generate
12617 -- an anonymous base type. Indicate that the instance of this base
12618 -- is the base type of the actual.
12619
12620 if Is_Scalar_Type (A_Gen_T) then
12621 Set_Instance_Of (Etype (A_Gen_T), Etype (Act_T));
12622 end if;
12623 end if;
12624
12625 if Error_Posted (Act_T) then
12626 null;
12627 else
12628 case Nkind (Def) is
12629 when N_Formal_Private_Type_Definition =>
12630 Validate_Private_Type_Instance;
12631
12632 when N_Formal_Incomplete_Type_Definition =>
12633 Validate_Incomplete_Type_Instance;
12634
12635 when N_Formal_Derived_Type_Definition =>
12636 Validate_Derived_Type_Instance;
12637
12638 when N_Formal_Discrete_Type_Definition =>
12639 if not Is_Discrete_Type (Act_T) then
12640 Error_Msg_NE
12641 ("expect discrete type in instantiation of&",
12642 Actual, Gen_T);
12643 Abandon_Instantiation (Actual);
12644 end if;
12645
12646 Diagnose_Predicated_Actual;
12647
12648 when N_Formal_Signed_Integer_Type_Definition =>
12649 if not Is_Signed_Integer_Type (Act_T) then
12650 Error_Msg_NE
12651 ("expect signed integer type in instantiation of&",
12652 Actual, Gen_T);
12653 Abandon_Instantiation (Actual);
12654 end if;
12655
12656 Diagnose_Predicated_Actual;
12657
12658 when N_Formal_Modular_Type_Definition =>
12659 if not Is_Modular_Integer_Type (Act_T) then
12660 Error_Msg_NE
12661 ("expect modular type in instantiation of &",
12662 Actual, Gen_T);
12663 Abandon_Instantiation (Actual);
12664 end if;
12665
12666 Diagnose_Predicated_Actual;
12667
12668 when N_Formal_Floating_Point_Definition =>
12669 if not Is_Floating_Point_Type (Act_T) then
12670 Error_Msg_NE
12671 ("expect float type in instantiation of &", Actual, Gen_T);
12672 Abandon_Instantiation (Actual);
12673 end if;
12674
12675 when N_Formal_Ordinary_Fixed_Point_Definition =>
12676 if not Is_Ordinary_Fixed_Point_Type (Act_T) then
12677 Error_Msg_NE
12678 ("expect ordinary fixed point type in instantiation of &",
12679 Actual, Gen_T);
12680 Abandon_Instantiation (Actual);
12681 end if;
12682
12683 when N_Formal_Decimal_Fixed_Point_Definition =>
12684 if not Is_Decimal_Fixed_Point_Type (Act_T) then
12685 Error_Msg_NE
12686 ("expect decimal type in instantiation of &",
12687 Actual, Gen_T);
12688 Abandon_Instantiation (Actual);
12689 end if;
12690
12691 when N_Array_Type_Definition =>
12692 Validate_Array_Type_Instance;
12693
12694 when N_Access_To_Object_Definition =>
12695 Validate_Access_Type_Instance;
12696
12697 when N_Access_Function_Definition |
12698 N_Access_Procedure_Definition =>
12699 Validate_Access_Subprogram_Instance;
12700
12701 when N_Record_Definition =>
12702 Validate_Interface_Type_Instance;
12703
12704 when N_Derived_Type_Definition =>
12705 Validate_Derived_Interface_Type_Instance;
12706
12707 when others =>
12708 raise Program_Error;
12709
12710 end case;
12711 end if;
12712
12713 Subt := New_Copy (Gen_T);
12714
12715 -- Use adjusted sloc of subtype name as the location for other nodes in
12716 -- the subtype declaration.
12717
12718 Loc := Sloc (Subt);
12719
12720 Decl_Node :=
12721 Make_Subtype_Declaration (Loc,
12722 Defining_Identifier => Subt,
12723 Subtype_Indication => New_Occurrence_Of (Act_T, Loc));
12724
12725 if Is_Private_Type (Act_T) then
12726 Set_Has_Private_View (Subtype_Indication (Decl_Node));
12727
12728 elsif Is_Access_Type (Act_T)
12729 and then Is_Private_Type (Designated_Type (Act_T))
12730 then
12731 Set_Has_Private_View (Subtype_Indication (Decl_Node));
12732 end if;
12733
12734 -- In Ada 2012 the actual may be a limited view. Indicate that
12735 -- the local subtype must be treated as such.
12736
12737 if From_Limited_With (Act_T) then
12738 Set_Ekind (Subt, E_Incomplete_Subtype);
12739 Set_From_Limited_With (Subt);
12740 end if;
12741
12742 Decl_Nodes := New_List (Decl_Node);
12743
12744 -- Flag actual derived types so their elaboration produces the
12745 -- appropriate renamings for the primitive operations of the ancestor.
12746 -- Flag actual for formal private types as well, to determine whether
12747 -- operations in the private part may override inherited operations.
12748 -- If the formal has an interface list, the ancestor is not the
12749 -- parent, but the analyzed formal that includes the interface
12750 -- operations of all its progenitors.
12751
12752 -- Same treatment for formal private types, so we can check whether the
12753 -- type is tagged limited when validating derivations in the private
12754 -- part. (See AI05-096).
12755
12756 if Nkind (Def) = N_Formal_Derived_Type_Definition then
12757 if Present (Interface_List (Def)) then
12758 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
12759 else
12760 Set_Generic_Parent_Type (Decl_Node, Ancestor);
12761 end if;
12762
12763 elsif Nkind_In (Def, N_Formal_Private_Type_Definition,
12764 N_Formal_Incomplete_Type_Definition)
12765 then
12766 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
12767 end if;
12768
12769 -- If the actual is a synchronized type that implements an interface,
12770 -- the primitive operations are attached to the corresponding record,
12771 -- and we have to treat it as an additional generic actual, so that its
12772 -- primitive operations become visible in the instance. The task or
12773 -- protected type itself does not carry primitive operations.
12774
12775 if Is_Concurrent_Type (Act_T)
12776 and then Is_Tagged_Type (Act_T)
12777 and then Present (Corresponding_Record_Type (Act_T))
12778 and then Present (Ancestor)
12779 and then Is_Interface (Ancestor)
12780 then
12781 declare
12782 Corr_Rec : constant Entity_Id :=
12783 Corresponding_Record_Type (Act_T);
12784 New_Corr : Entity_Id;
12785 Corr_Decl : Node_Id;
12786
12787 begin
12788 New_Corr := Make_Temporary (Loc, 'S');
12789 Corr_Decl :=
12790 Make_Subtype_Declaration (Loc,
12791 Defining_Identifier => New_Corr,
12792 Subtype_Indication =>
12793 New_Occurrence_Of (Corr_Rec, Loc));
12794 Append_To (Decl_Nodes, Corr_Decl);
12795
12796 if Ekind (Act_T) = E_Task_Type then
12797 Set_Ekind (Subt, E_Task_Subtype);
12798 else
12799 Set_Ekind (Subt, E_Protected_Subtype);
12800 end if;
12801
12802 Set_Corresponding_Record_Type (Subt, Corr_Rec);
12803 Set_Generic_Parent_Type (Corr_Decl, Ancestor);
12804 Set_Generic_Parent_Type (Decl_Node, Empty);
12805 end;
12806 end if;
12807
12808 -- For a floating-point type, capture dimension info if any, because
12809 -- the generated subtype declaration does not come from source and
12810 -- will not process dimensions.
12811
12812 if Is_Floating_Point_Type (Act_T) then
12813 Copy_Dimensions (Act_T, Subt);
12814 end if;
12815
12816 return Decl_Nodes;
12817 end Instantiate_Type;
12818
12819 ---------------------
12820 -- Is_In_Main_Unit --
12821 ---------------------
12822
12823 function Is_In_Main_Unit (N : Node_Id) return Boolean is
12824 Unum : constant Unit_Number_Type := Get_Source_Unit (N);
12825 Current_Unit : Node_Id;
12826
12827 begin
12828 if Unum = Main_Unit then
12829 return True;
12830
12831 -- If the current unit is a subunit then it is either the main unit or
12832 -- is being compiled as part of the main unit.
12833
12834 elsif Nkind (N) = N_Compilation_Unit then
12835 return Nkind (Unit (N)) = N_Subunit;
12836 end if;
12837
12838 Current_Unit := Parent (N);
12839 while Present (Current_Unit)
12840 and then Nkind (Current_Unit) /= N_Compilation_Unit
12841 loop
12842 Current_Unit := Parent (Current_Unit);
12843 end loop;
12844
12845 -- The instantiation node is in the main unit, or else the current node
12846 -- (perhaps as the result of nested instantiations) is in the main unit,
12847 -- or in the declaration of the main unit, which in this last case must
12848 -- be a body.
12849
12850 return Unum = Main_Unit
12851 or else Current_Unit = Cunit (Main_Unit)
12852 or else Current_Unit = Library_Unit (Cunit (Main_Unit))
12853 or else (Present (Library_Unit (Current_Unit))
12854 and then Is_In_Main_Unit (Library_Unit (Current_Unit)));
12855 end Is_In_Main_Unit;
12856
12857 ----------------------------
12858 -- Load_Parent_Of_Generic --
12859 ----------------------------
12860
12861 procedure Load_Parent_Of_Generic
12862 (N : Node_Id;
12863 Spec : Node_Id;
12864 Body_Optional : Boolean := False)
12865 is
12866 Comp_Unit : constant Node_Id := Cunit (Get_Source_Unit (Spec));
12867 Saved_Style_Check : constant Boolean := Style_Check;
12868 Saved_Warnings : constant Warning_Record := Save_Warnings;
12869 True_Parent : Node_Id;
12870 Inst_Node : Node_Id;
12871 OK : Boolean;
12872 Previous_Instances : constant Elist_Id := New_Elmt_List;
12873
12874 procedure Collect_Previous_Instances (Decls : List_Id);
12875 -- Collect all instantiations in the given list of declarations, that
12876 -- precede the generic that we need to load. If the bodies of these
12877 -- instantiations are available, we must analyze them, to ensure that
12878 -- the public symbols generated are the same when the unit is compiled
12879 -- to generate code, and when it is compiled in the context of a unit
12880 -- that needs a particular nested instance. This process is applied to
12881 -- both package and subprogram instances.
12882
12883 --------------------------------
12884 -- Collect_Previous_Instances --
12885 --------------------------------
12886
12887 procedure Collect_Previous_Instances (Decls : List_Id) is
12888 Decl : Node_Id;
12889
12890 begin
12891 Decl := First (Decls);
12892 while Present (Decl) loop
12893 if Sloc (Decl) >= Sloc (Inst_Node) then
12894 return;
12895
12896 -- If Decl is an instantiation, then record it as requiring
12897 -- instantiation of the corresponding body, except if it is an
12898 -- abbreviated instantiation generated internally for conformance
12899 -- checking purposes only for the case of a formal package
12900 -- declared without a box (see Instantiate_Formal_Package). Such
12901 -- an instantiation does not generate any code (the actual code
12902 -- comes from actual) and thus does not need to be analyzed here.
12903 -- If the instantiation appears with a generic package body it is
12904 -- not analyzed here either.
12905
12906 elsif Nkind (Decl) = N_Package_Instantiation
12907 and then not Is_Internal (Defining_Entity (Decl))
12908 then
12909 Append_Elmt (Decl, Previous_Instances);
12910
12911 -- For a subprogram instantiation, omit instantiations intrinsic
12912 -- operations (Unchecked_Conversions, etc.) that have no bodies.
12913
12914 elsif Nkind_In (Decl, N_Function_Instantiation,
12915 N_Procedure_Instantiation)
12916 and then not Is_Intrinsic_Subprogram (Entity (Name (Decl)))
12917 then
12918 Append_Elmt (Decl, Previous_Instances);
12919
12920 elsif Nkind (Decl) = N_Package_Declaration then
12921 Collect_Previous_Instances
12922 (Visible_Declarations (Specification (Decl)));
12923 Collect_Previous_Instances
12924 (Private_Declarations (Specification (Decl)));
12925
12926 -- Previous non-generic bodies may contain instances as well
12927
12928 elsif Nkind (Decl) = N_Package_Body
12929 and then Ekind (Corresponding_Spec (Decl)) /= E_Generic_Package
12930 then
12931 Collect_Previous_Instances (Declarations (Decl));
12932
12933 elsif Nkind (Decl) = N_Subprogram_Body
12934 and then not Acts_As_Spec (Decl)
12935 and then not Is_Generic_Subprogram (Corresponding_Spec (Decl))
12936 then
12937 Collect_Previous_Instances (Declarations (Decl));
12938 end if;
12939
12940 Next (Decl);
12941 end loop;
12942 end Collect_Previous_Instances;
12943
12944 -- Start of processing for Load_Parent_Of_Generic
12945
12946 begin
12947 if not In_Same_Source_Unit (N, Spec)
12948 or else Nkind (Unit (Comp_Unit)) = N_Package_Declaration
12949 or else (Nkind (Unit (Comp_Unit)) = N_Package_Body
12950 and then not Is_In_Main_Unit (Spec))
12951 then
12952 -- Find body of parent of spec, and analyze it. A special case arises
12953 -- when the parent is an instantiation, that is to say when we are
12954 -- currently instantiating a nested generic. In that case, there is
12955 -- no separate file for the body of the enclosing instance. Instead,
12956 -- the enclosing body must be instantiated as if it were a pending
12957 -- instantiation, in order to produce the body for the nested generic
12958 -- we require now. Note that in that case the generic may be defined
12959 -- in a package body, the instance defined in the same package body,
12960 -- and the original enclosing body may not be in the main unit.
12961
12962 Inst_Node := Empty;
12963
12964 True_Parent := Parent (Spec);
12965 while Present (True_Parent)
12966 and then Nkind (True_Parent) /= N_Compilation_Unit
12967 loop
12968 if Nkind (True_Parent) = N_Package_Declaration
12969 and then
12970 Nkind (Original_Node (True_Parent)) = N_Package_Instantiation
12971 then
12972 -- Parent is a compilation unit that is an instantiation.
12973 -- Instantiation node has been replaced with package decl.
12974
12975 Inst_Node := Original_Node (True_Parent);
12976 exit;
12977
12978 elsif Nkind (True_Parent) = N_Package_Declaration
12979 and then Present (Generic_Parent (Specification (True_Parent)))
12980 and then Nkind (Parent (True_Parent)) /= N_Compilation_Unit
12981 then
12982 -- Parent is an instantiation within another specification.
12983 -- Declaration for instance has been inserted before original
12984 -- instantiation node. A direct link would be preferable?
12985
12986 Inst_Node := Next (True_Parent);
12987 while Present (Inst_Node)
12988 and then Nkind (Inst_Node) /= N_Package_Instantiation
12989 loop
12990 Next (Inst_Node);
12991 end loop;
12992
12993 -- If the instance appears within a generic, and the generic
12994 -- unit is defined within a formal package of the enclosing
12995 -- generic, there is no generic body available, and none
12996 -- needed. A more precise test should be used ???
12997
12998 if No (Inst_Node) then
12999 return;
13000 end if;
13001
13002 exit;
13003
13004 else
13005 True_Parent := Parent (True_Parent);
13006 end if;
13007 end loop;
13008
13009 -- Case where we are currently instantiating a nested generic
13010
13011 if Present (Inst_Node) then
13012 if Nkind (Parent (True_Parent)) = N_Compilation_Unit then
13013
13014 -- Instantiation node and declaration of instantiated package
13015 -- were exchanged when only the declaration was needed.
13016 -- Restore instantiation node before proceeding with body.
13017
13018 Set_Unit (Parent (True_Parent), Inst_Node);
13019 end if;
13020
13021 -- Now complete instantiation of enclosing body, if it appears in
13022 -- some other unit. If it appears in the current unit, the body
13023 -- will have been instantiated already.
13024
13025 if No (Corresponding_Body (Instance_Spec (Inst_Node))) then
13026
13027 -- We need to determine the expander mode to instantiate the
13028 -- enclosing body. Because the generic body we need may use
13029 -- global entities declared in the enclosing package (including
13030 -- aggregates) it is in general necessary to compile this body
13031 -- with expansion enabled, except if we are within a generic
13032 -- package, in which case the usual generic rule applies.
13033
13034 declare
13035 Exp_Status : Boolean := True;
13036 Scop : Entity_Id;
13037
13038 begin
13039 -- Loop through scopes looking for generic package
13040
13041 Scop := Scope (Defining_Entity (Instance_Spec (Inst_Node)));
13042 while Present (Scop)
13043 and then Scop /= Standard_Standard
13044 loop
13045 if Ekind (Scop) = E_Generic_Package then
13046 Exp_Status := False;
13047 exit;
13048 end if;
13049
13050 Scop := Scope (Scop);
13051 end loop;
13052
13053 -- Collect previous instantiations in the unit that contains
13054 -- the desired generic.
13055
13056 if Nkind (Parent (True_Parent)) /= N_Compilation_Unit
13057 and then not Body_Optional
13058 then
13059 declare
13060 Decl : Elmt_Id;
13061 Info : Pending_Body_Info;
13062 Par : Node_Id;
13063
13064 begin
13065 Par := Parent (Inst_Node);
13066 while Present (Par) loop
13067 exit when Nkind (Parent (Par)) = N_Compilation_Unit;
13068 Par := Parent (Par);
13069 end loop;
13070
13071 pragma Assert (Present (Par));
13072
13073 if Nkind (Par) = N_Package_Body then
13074 Collect_Previous_Instances (Declarations (Par));
13075
13076 elsif Nkind (Par) = N_Package_Declaration then
13077 Collect_Previous_Instances
13078 (Visible_Declarations (Specification (Par)));
13079 Collect_Previous_Instances
13080 (Private_Declarations (Specification (Par)));
13081
13082 else
13083 -- Enclosing unit is a subprogram body. In this
13084 -- case all instance bodies are processed in order
13085 -- and there is no need to collect them separately.
13086
13087 null;
13088 end if;
13089
13090 Decl := First_Elmt (Previous_Instances);
13091 while Present (Decl) loop
13092 Info :=
13093 (Inst_Node => Node (Decl),
13094 Act_Decl =>
13095 Instance_Spec (Node (Decl)),
13096 Expander_Status => Exp_Status,
13097 Current_Sem_Unit =>
13098 Get_Code_Unit (Sloc (Node (Decl))),
13099 Scope_Suppress => Scope_Suppress,
13100 Local_Suppress_Stack_Top =>
13101 Local_Suppress_Stack_Top,
13102 Version => Ada_Version,
13103 Version_Pragma => Ada_Version_Pragma,
13104 Warnings => Save_Warnings,
13105 SPARK_Mode => SPARK_Mode,
13106 SPARK_Mode_Pragma => SPARK_Mode_Pragma);
13107
13108 -- Package instance
13109
13110 if
13111 Nkind (Node (Decl)) = N_Package_Instantiation
13112 then
13113 Instantiate_Package_Body
13114 (Info, Body_Optional => True);
13115
13116 -- Subprogram instance
13117
13118 else
13119 -- The instance_spec is in the wrapper package,
13120 -- usually followed by its local renaming
13121 -- declaration. See Build_Subprogram_Renaming
13122 -- for details. If the instance carries aspects,
13123 -- these result in the corresponding pragmas,
13124 -- inserted after the subprogram declaration.
13125 -- They must be skipped as well when retrieving
13126 -- the desired spec. A direct link would be
13127 -- more robust ???
13128
13129 declare
13130 Decl : Node_Id :=
13131 (Last (Visible_Declarations
13132 (Specification (Info.Act_Decl))));
13133 begin
13134 while Nkind_In (Decl,
13135 N_Subprogram_Renaming_Declaration, N_Pragma)
13136 loop
13137 Decl := Prev (Decl);
13138 end loop;
13139
13140 Info.Act_Decl := Decl;
13141 end;
13142
13143 Instantiate_Subprogram_Body
13144 (Info, Body_Optional => True);
13145 end if;
13146
13147 Next_Elmt (Decl);
13148 end loop;
13149 end;
13150 end if;
13151
13152 Instantiate_Package_Body
13153 (Body_Info =>
13154 ((Inst_Node => Inst_Node,
13155 Act_Decl => True_Parent,
13156 Expander_Status => Exp_Status,
13157 Current_Sem_Unit => Get_Code_Unit
13158 (Sloc (Inst_Node)),
13159 Scope_Suppress => Scope_Suppress,
13160 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
13161 Version => Ada_Version,
13162 Version_Pragma => Ada_Version_Pragma,
13163 Warnings => Save_Warnings,
13164 SPARK_Mode => SPARK_Mode,
13165 SPARK_Mode_Pragma => SPARK_Mode_Pragma)),
13166 Body_Optional => Body_Optional);
13167 end;
13168 end if;
13169
13170 -- Case where we are not instantiating a nested generic
13171
13172 else
13173 Opt.Style_Check := False;
13174 Expander_Mode_Save_And_Set (True);
13175 Load_Needed_Body (Comp_Unit, OK);
13176 Opt.Style_Check := Saved_Style_Check;
13177 Restore_Warnings (Saved_Warnings);
13178 Expander_Mode_Restore;
13179
13180 if not OK
13181 and then Unit_Requires_Body (Defining_Entity (Spec))
13182 and then not Body_Optional
13183 then
13184 declare
13185 Bname : constant Unit_Name_Type :=
13186 Get_Body_Name (Get_Unit_Name (Unit (Comp_Unit)));
13187
13188 begin
13189 -- In CodePeer mode, the missing body may make the analysis
13190 -- incomplete, but we do not treat it as fatal.
13191
13192 if CodePeer_Mode then
13193 return;
13194
13195 else
13196 Error_Msg_Unit_1 := Bname;
13197 Error_Msg_N ("this instantiation requires$!", N);
13198 Error_Msg_File_1 :=
13199 Get_File_Name (Bname, Subunit => False);
13200 Error_Msg_N ("\but file{ was not found!", N);
13201 raise Unrecoverable_Error;
13202 end if;
13203 end;
13204 end if;
13205 end if;
13206 end if;
13207
13208 -- If loading parent of the generic caused an instantiation circularity,
13209 -- we abandon compilation at this point, because otherwise in some cases
13210 -- we get into trouble with infinite recursions after this point.
13211
13212 if Circularity_Detected then
13213 raise Unrecoverable_Error;
13214 end if;
13215 end Load_Parent_Of_Generic;
13216
13217 ---------------------------------
13218 -- Map_Formal_Package_Entities --
13219 ---------------------------------
13220
13221 procedure Map_Formal_Package_Entities (Form : Entity_Id; Act : Entity_Id) is
13222 E1 : Entity_Id;
13223 E2 : Entity_Id;
13224
13225 begin
13226 Set_Instance_Of (Form, Act);
13227
13228 -- Traverse formal and actual package to map the corresponding entities.
13229 -- We skip over internal entities that may be generated during semantic
13230 -- analysis, and find the matching entities by name, given that they
13231 -- must appear in the same order.
13232
13233 E1 := First_Entity (Form);
13234 E2 := First_Entity (Act);
13235 while Present (E1) and then E1 /= First_Private_Entity (Form) loop
13236 -- Could this test be a single condition??? Seems like it could, and
13237 -- isn't FPE (Form) a constant anyway???
13238
13239 if not Is_Internal (E1)
13240 and then Present (Parent (E1))
13241 and then not Is_Class_Wide_Type (E1)
13242 and then not Is_Internal_Name (Chars (E1))
13243 then
13244 while Present (E2) and then Chars (E2) /= Chars (E1) loop
13245 Next_Entity (E2);
13246 end loop;
13247
13248 if No (E2) then
13249 exit;
13250 else
13251 Set_Instance_Of (E1, E2);
13252
13253 if Is_Type (E1) and then Is_Tagged_Type (E2) then
13254 Set_Instance_Of (Class_Wide_Type (E1), Class_Wide_Type (E2));
13255 end if;
13256
13257 if Is_Constrained (E1) then
13258 Set_Instance_Of (Base_Type (E1), Base_Type (E2));
13259 end if;
13260
13261 if Ekind (E1) = E_Package and then No (Renamed_Object (E1)) then
13262 Map_Formal_Package_Entities (E1, E2);
13263 end if;
13264 end if;
13265 end if;
13266
13267 Next_Entity (E1);
13268 end loop;
13269 end Map_Formal_Package_Entities;
13270
13271 -----------------------
13272 -- Move_Freeze_Nodes --
13273 -----------------------
13274
13275 procedure Move_Freeze_Nodes
13276 (Out_Of : Entity_Id;
13277 After : Node_Id;
13278 L : List_Id)
13279 is
13280 Decl : Node_Id;
13281 Next_Decl : Node_Id;
13282 Next_Node : Node_Id := After;
13283 Spec : Node_Id;
13284
13285 function Is_Outer_Type (T : Entity_Id) return Boolean;
13286 -- Check whether entity is declared in a scope external to that of the
13287 -- generic unit.
13288
13289 -------------------
13290 -- Is_Outer_Type --
13291 -------------------
13292
13293 function Is_Outer_Type (T : Entity_Id) return Boolean is
13294 Scop : Entity_Id := Scope (T);
13295
13296 begin
13297 if Scope_Depth (Scop) < Scope_Depth (Out_Of) then
13298 return True;
13299
13300 else
13301 while Scop /= Standard_Standard loop
13302 if Scop = Out_Of then
13303 return False;
13304 else
13305 Scop := Scope (Scop);
13306 end if;
13307 end loop;
13308
13309 return True;
13310 end if;
13311 end Is_Outer_Type;
13312
13313 -- Start of processing for Move_Freeze_Nodes
13314
13315 begin
13316 if No (L) then
13317 return;
13318 end if;
13319
13320 -- First remove the freeze nodes that may appear before all other
13321 -- declarations.
13322
13323 Decl := First (L);
13324 while Present (Decl)
13325 and then Nkind (Decl) = N_Freeze_Entity
13326 and then Is_Outer_Type (Entity (Decl))
13327 loop
13328 Decl := Remove_Head (L);
13329 Insert_After (Next_Node, Decl);
13330 Set_Analyzed (Decl, False);
13331 Next_Node := Decl;
13332 Decl := First (L);
13333 end loop;
13334
13335 -- Next scan the list of declarations and remove each freeze node that
13336 -- appears ahead of the current node.
13337
13338 while Present (Decl) loop
13339 while Present (Next (Decl))
13340 and then Nkind (Next (Decl)) = N_Freeze_Entity
13341 and then Is_Outer_Type (Entity (Next (Decl)))
13342 loop
13343 Next_Decl := Remove_Next (Decl);
13344 Insert_After (Next_Node, Next_Decl);
13345 Set_Analyzed (Next_Decl, False);
13346 Next_Node := Next_Decl;
13347 end loop;
13348
13349 -- If the declaration is a nested package or concurrent type, then
13350 -- recurse. Nested generic packages will have been processed from the
13351 -- inside out.
13352
13353 case Nkind (Decl) is
13354 when N_Package_Declaration =>
13355 Spec := Specification (Decl);
13356
13357 when N_Task_Type_Declaration =>
13358 Spec := Task_Definition (Decl);
13359
13360 when N_Protected_Type_Declaration =>
13361 Spec := Protected_Definition (Decl);
13362
13363 when others =>
13364 Spec := Empty;
13365 end case;
13366
13367 if Present (Spec) then
13368 Move_Freeze_Nodes (Out_Of, Next_Node, Visible_Declarations (Spec));
13369 Move_Freeze_Nodes (Out_Of, Next_Node, Private_Declarations (Spec));
13370 end if;
13371
13372 Next (Decl);
13373 end loop;
13374 end Move_Freeze_Nodes;
13375
13376 ----------------
13377 -- Next_Assoc --
13378 ----------------
13379
13380 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr is
13381 begin
13382 return Generic_Renamings.Table (E).Next_In_HTable;
13383 end Next_Assoc;
13384
13385 ------------------------
13386 -- Preanalyze_Actuals --
13387 ------------------------
13388
13389 procedure Preanalyze_Actuals (N : Node_Id; Inst : Entity_Id := Empty) is
13390 Assoc : Node_Id;
13391 Act : Node_Id;
13392 Errs : constant Nat := Serious_Errors_Detected;
13393
13394 Cur : Entity_Id := Empty;
13395 -- Current homograph of the instance name
13396
13397 Vis : Boolean;
13398 -- Saved visibility status of the current homograph
13399
13400 begin
13401 Assoc := First (Generic_Associations (N));
13402
13403 -- If the instance is a child unit, its name may hide an outer homonym,
13404 -- so make it invisible to perform name resolution on the actuals.
13405
13406 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name
13407 and then Present
13408 (Current_Entity (Defining_Identifier (Defining_Unit_Name (N))))
13409 then
13410 Cur := Current_Entity (Defining_Identifier (Defining_Unit_Name (N)));
13411
13412 if Is_Compilation_Unit (Cur) then
13413 Vis := Is_Immediately_Visible (Cur);
13414 Set_Is_Immediately_Visible (Cur, False);
13415 else
13416 Cur := Empty;
13417 end if;
13418 end if;
13419
13420 while Present (Assoc) loop
13421 if Nkind (Assoc) /= N_Others_Choice then
13422 Act := Explicit_Generic_Actual_Parameter (Assoc);
13423
13424 -- Within a nested instantiation, a defaulted actual is an empty
13425 -- association, so nothing to analyze. If the subprogram actual
13426 -- is an attribute, analyze prefix only, because actual is not a
13427 -- complete attribute reference.
13428
13429 -- If actual is an allocator, analyze expression only. The full
13430 -- analysis can generate code, and if instance is a compilation
13431 -- unit we have to wait until the package instance is installed
13432 -- to have a proper place to insert this code.
13433
13434 -- String literals may be operators, but at this point we do not
13435 -- know whether the actual is a formal subprogram or a string.
13436
13437 if No (Act) then
13438 null;
13439
13440 elsif Nkind (Act) = N_Attribute_Reference then
13441 Analyze (Prefix (Act));
13442
13443 elsif Nkind (Act) = N_Explicit_Dereference then
13444 Analyze (Prefix (Act));
13445
13446 elsif Nkind (Act) = N_Allocator then
13447 declare
13448 Expr : constant Node_Id := Expression (Act);
13449
13450 begin
13451 if Nkind (Expr) = N_Subtype_Indication then
13452 Analyze (Subtype_Mark (Expr));
13453
13454 -- Analyze separately each discriminant constraint, when
13455 -- given with a named association.
13456
13457 declare
13458 Constr : Node_Id;
13459
13460 begin
13461 Constr := First (Constraints (Constraint (Expr)));
13462 while Present (Constr) loop
13463 if Nkind (Constr) = N_Discriminant_Association then
13464 Analyze (Expression (Constr));
13465 else
13466 Analyze (Constr);
13467 end if;
13468
13469 Next (Constr);
13470 end loop;
13471 end;
13472
13473 else
13474 Analyze (Expr);
13475 end if;
13476 end;
13477
13478 elsif Nkind (Act) /= N_Operator_Symbol then
13479 Analyze (Act);
13480
13481 -- Within a package instance, mark actuals that are limited
13482 -- views, so their use can be moved to the body of the
13483 -- enclosing unit.
13484
13485 if Is_Entity_Name (Act)
13486 and then Is_Type (Entity (Act))
13487 and then From_Limited_With (Entity (Act))
13488 and then Present (Inst)
13489 then
13490 Append_Elmt (Entity (Act), Incomplete_Actuals (Inst));
13491 end if;
13492 end if;
13493
13494 if Errs /= Serious_Errors_Detected then
13495
13496 -- Do a minimal analysis of the generic, to prevent spurious
13497 -- warnings complaining about the generic being unreferenced,
13498 -- before abandoning the instantiation.
13499
13500 Analyze (Name (N));
13501
13502 if Is_Entity_Name (Name (N))
13503 and then Etype (Name (N)) /= Any_Type
13504 then
13505 Generate_Reference (Entity (Name (N)), Name (N));
13506 Set_Is_Instantiated (Entity (Name (N)));
13507 end if;
13508
13509 if Present (Cur) then
13510
13511 -- For the case of a child instance hiding an outer homonym,
13512 -- provide additional warning which might explain the error.
13513
13514 Set_Is_Immediately_Visible (Cur, Vis);
13515 Error_Msg_NE
13516 ("& hides outer unit with the same name??",
13517 N, Defining_Unit_Name (N));
13518 end if;
13519
13520 Abandon_Instantiation (Act);
13521 end if;
13522 end if;
13523
13524 Next (Assoc);
13525 end loop;
13526
13527 if Present (Cur) then
13528 Set_Is_Immediately_Visible (Cur, Vis);
13529 end if;
13530 end Preanalyze_Actuals;
13531
13532 -------------------
13533 -- Remove_Parent --
13534 -------------------
13535
13536 procedure Remove_Parent (In_Body : Boolean := False) is
13537 S : Entity_Id := Current_Scope;
13538 -- S is the scope containing the instantiation just completed. The scope
13539 -- stack contains the parent instances of the instantiation, followed by
13540 -- the original S.
13541
13542 Cur_P : Entity_Id;
13543 E : Entity_Id;
13544 P : Entity_Id;
13545 Hidden : Elmt_Id;
13546
13547 begin
13548 -- After child instantiation is complete, remove from scope stack the
13549 -- extra copy of the current scope, and then remove parent instances.
13550
13551 if not In_Body then
13552 Pop_Scope;
13553
13554 while Current_Scope /= S loop
13555 P := Current_Scope;
13556 End_Package_Scope (Current_Scope);
13557
13558 if In_Open_Scopes (P) then
13559 E := First_Entity (P);
13560 while Present (E) loop
13561 Set_Is_Immediately_Visible (E, True);
13562 Next_Entity (E);
13563 end loop;
13564
13565 -- If instantiation is declared in a block, it is the enclosing
13566 -- scope that might be a parent instance. Note that only one
13567 -- block can be involved, because the parent instances have
13568 -- been installed within it.
13569
13570 if Ekind (P) = E_Block then
13571 Cur_P := Scope (P);
13572 else
13573 Cur_P := P;
13574 end if;
13575
13576 if Is_Generic_Instance (Cur_P) and then P /= Current_Scope then
13577 -- We are within an instance of some sibling. Retain
13578 -- visibility of parent, for proper subsequent cleanup, and
13579 -- reinstall private declarations as well.
13580
13581 Set_In_Private_Part (P);
13582 Install_Private_Declarations (P);
13583 end if;
13584
13585 -- If the ultimate parent is a top-level unit recorded in
13586 -- Instance_Parent_Unit, then reset its visibility to what it was
13587 -- before instantiation. (It's not clear what the purpose is of
13588 -- testing whether Scope (P) is In_Open_Scopes, but that test was
13589 -- present before the ultimate parent test was added.???)
13590
13591 elsif not In_Open_Scopes (Scope (P))
13592 or else (P = Instance_Parent_Unit
13593 and then not Parent_Unit_Visible)
13594 then
13595 Set_Is_Immediately_Visible (P, False);
13596
13597 -- If the current scope is itself an instantiation of a generic
13598 -- nested within P, and we are in the private part of body of this
13599 -- instantiation, restore the full views of P, that were removed
13600 -- in End_Package_Scope above. This obscure case can occur when a
13601 -- subunit of a generic contains an instance of a child unit of
13602 -- its generic parent unit.
13603
13604 elsif S = Current_Scope and then Is_Generic_Instance (S) then
13605 declare
13606 Par : constant Entity_Id :=
13607 Generic_Parent (Package_Specification (S));
13608 begin
13609 if Present (Par)
13610 and then P = Scope (Par)
13611 and then (In_Package_Body (S) or else In_Private_Part (S))
13612 then
13613 Set_In_Private_Part (P);
13614 Install_Private_Declarations (P);
13615 end if;
13616 end;
13617 end if;
13618 end loop;
13619
13620 -- Reset visibility of entities in the enclosing scope
13621
13622 Set_Is_Hidden_Open_Scope (Current_Scope, False);
13623
13624 Hidden := First_Elmt (Hidden_Entities);
13625 while Present (Hidden) loop
13626 Set_Is_Immediately_Visible (Node (Hidden), True);
13627 Next_Elmt (Hidden);
13628 end loop;
13629
13630 else
13631 -- Each body is analyzed separately, and there is no context that
13632 -- needs preserving from one body instance to the next, so remove all
13633 -- parent scopes that have been installed.
13634
13635 while Present (S) loop
13636 End_Package_Scope (S);
13637 Set_Is_Immediately_Visible (S, False);
13638 S := Current_Scope;
13639 exit when S = Standard_Standard;
13640 end loop;
13641 end if;
13642 end Remove_Parent;
13643
13644 -----------------
13645 -- Restore_Env --
13646 -----------------
13647
13648 procedure Restore_Env is
13649 Saved : Instance_Env renames Instance_Envs.Table (Instance_Envs.Last);
13650
13651 begin
13652 if No (Current_Instantiated_Parent.Act_Id) then
13653 -- Restore environment after subprogram inlining
13654
13655 Restore_Private_Views (Empty);
13656 end if;
13657
13658 Current_Instantiated_Parent := Saved.Instantiated_Parent;
13659 Exchanged_Views := Saved.Exchanged_Views;
13660 Hidden_Entities := Saved.Hidden_Entities;
13661 Current_Sem_Unit := Saved.Current_Sem_Unit;
13662 Parent_Unit_Visible := Saved.Parent_Unit_Visible;
13663 Instance_Parent_Unit := Saved.Instance_Parent_Unit;
13664
13665 Restore_Opt_Config_Switches (Saved.Switches);
13666
13667 Instance_Envs.Decrement_Last;
13668 end Restore_Env;
13669
13670 ---------------------------
13671 -- Restore_Private_Views --
13672 ---------------------------
13673
13674 procedure Restore_Private_Views
13675 (Pack_Id : Entity_Id;
13676 Is_Package : Boolean := True)
13677 is
13678 M : Elmt_Id;
13679 E : Entity_Id;
13680 Typ : Entity_Id;
13681 Dep_Elmt : Elmt_Id;
13682 Dep_Typ : Node_Id;
13683
13684 procedure Restore_Nested_Formal (Formal : Entity_Id);
13685 -- Hide the generic formals of formal packages declared with box which
13686 -- were reachable in the current instantiation.
13687
13688 ---------------------------
13689 -- Restore_Nested_Formal --
13690 ---------------------------
13691
13692 procedure Restore_Nested_Formal (Formal : Entity_Id) is
13693 Ent : Entity_Id;
13694
13695 begin
13696 if Present (Renamed_Object (Formal))
13697 and then Denotes_Formal_Package (Renamed_Object (Formal), True)
13698 then
13699 return;
13700
13701 elsif Present (Associated_Formal_Package (Formal)) then
13702 Ent := First_Entity (Formal);
13703 while Present (Ent) loop
13704 exit when Ekind (Ent) = E_Package
13705 and then Renamed_Entity (Ent) = Renamed_Entity (Formal);
13706
13707 Set_Is_Hidden (Ent);
13708 Set_Is_Potentially_Use_Visible (Ent, False);
13709
13710 -- If package, then recurse
13711
13712 if Ekind (Ent) = E_Package then
13713 Restore_Nested_Formal (Ent);
13714 end if;
13715
13716 Next_Entity (Ent);
13717 end loop;
13718 end if;
13719 end Restore_Nested_Formal;
13720
13721 -- Start of processing for Restore_Private_Views
13722
13723 begin
13724 M := First_Elmt (Exchanged_Views);
13725 while Present (M) loop
13726 Typ := Node (M);
13727
13728 -- Subtypes of types whose views have been exchanged, and that are
13729 -- defined within the instance, were not on the Private_Dependents
13730 -- list on entry to the instance, so they have to be exchanged
13731 -- explicitly now, in order to remain consistent with the view of the
13732 -- parent type.
13733
13734 if Ekind_In (Typ, E_Private_Type,
13735 E_Limited_Private_Type,
13736 E_Record_Type_With_Private)
13737 then
13738 Dep_Elmt := First_Elmt (Private_Dependents (Typ));
13739 while Present (Dep_Elmt) loop
13740 Dep_Typ := Node (Dep_Elmt);
13741
13742 if Scope (Dep_Typ) = Pack_Id
13743 and then Present (Full_View (Dep_Typ))
13744 then
13745 Replace_Elmt (Dep_Elmt, Full_View (Dep_Typ));
13746 Exchange_Declarations (Dep_Typ);
13747 end if;
13748
13749 Next_Elmt (Dep_Elmt);
13750 end loop;
13751 end if;
13752
13753 Exchange_Declarations (Node (M));
13754 Next_Elmt (M);
13755 end loop;
13756
13757 if No (Pack_Id) then
13758 return;
13759 end if;
13760
13761 -- Make the generic formal parameters private, and make the formal types
13762 -- into subtypes of the actuals again.
13763
13764 E := First_Entity (Pack_Id);
13765 while Present (E) loop
13766 Set_Is_Hidden (E, True);
13767
13768 if Is_Type (E)
13769 and then Nkind (Parent (E)) = N_Subtype_Declaration
13770 then
13771 -- If the actual for E is itself a generic actual type from
13772 -- an enclosing instance, E is still a generic actual type
13773 -- outside of the current instance. This matter when resolving
13774 -- an overloaded call that may be ambiguous in the enclosing
13775 -- instance, when two of its actuals coincide.
13776
13777 if Is_Entity_Name (Subtype_Indication (Parent (E)))
13778 and then Is_Generic_Actual_Type
13779 (Entity (Subtype_Indication (Parent (E))))
13780 then
13781 null;
13782 else
13783 Set_Is_Generic_Actual_Type (E, False);
13784 end if;
13785
13786 -- An unusual case of aliasing: the actual may also be directly
13787 -- visible in the generic, and be private there, while it is fully
13788 -- visible in the context of the instance. The internal subtype
13789 -- is private in the instance but has full visibility like its
13790 -- parent in the enclosing scope. This enforces the invariant that
13791 -- the privacy status of all private dependents of a type coincide
13792 -- with that of the parent type. This can only happen when a
13793 -- generic child unit is instantiated within a sibling.
13794
13795 if Is_Private_Type (E)
13796 and then not Is_Private_Type (Etype (E))
13797 then
13798 Exchange_Declarations (E);
13799 end if;
13800
13801 elsif Ekind (E) = E_Package then
13802
13803 -- The end of the renaming list is the renaming of the generic
13804 -- package itself. If the instance is a subprogram, all entities
13805 -- in the corresponding package are renamings. If this entity is
13806 -- a formal package, make its own formals private as well. The
13807 -- actual in this case is itself the renaming of an instantiation.
13808 -- If the entity is not a package renaming, it is the entity
13809 -- created to validate formal package actuals: ignore it.
13810
13811 -- If the actual is itself a formal package for the enclosing
13812 -- generic, or the actual for such a formal package, it remains
13813 -- visible on exit from the instance, and therefore nothing needs
13814 -- to be done either, except to keep it accessible.
13815
13816 if Is_Package and then Renamed_Object (E) = Pack_Id then
13817 exit;
13818
13819 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
13820 null;
13821
13822 elsif
13823 Denotes_Formal_Package (Renamed_Object (E), True, Pack_Id)
13824 then
13825 Set_Is_Hidden (E, False);
13826
13827 else
13828 declare
13829 Act_P : constant Entity_Id := Renamed_Object (E);
13830 Id : Entity_Id;
13831
13832 begin
13833 Id := First_Entity (Act_P);
13834 while Present (Id)
13835 and then Id /= First_Private_Entity (Act_P)
13836 loop
13837 exit when Ekind (Id) = E_Package
13838 and then Renamed_Object (Id) = Act_P;
13839
13840 Set_Is_Hidden (Id, True);
13841 Set_Is_Potentially_Use_Visible (Id, In_Use (Act_P));
13842
13843 if Ekind (Id) = E_Package then
13844 Restore_Nested_Formal (Id);
13845 end if;
13846
13847 Next_Entity (Id);
13848 end loop;
13849 end;
13850 end if;
13851 end if;
13852
13853 Next_Entity (E);
13854 end loop;
13855 end Restore_Private_Views;
13856
13857 --------------
13858 -- Save_Env --
13859 --------------
13860
13861 procedure Save_Env
13862 (Gen_Unit : Entity_Id;
13863 Act_Unit : Entity_Id)
13864 is
13865 begin
13866 Init_Env;
13867 Set_Instance_Env (Gen_Unit, Act_Unit);
13868 end Save_Env;
13869
13870 ----------------------------
13871 -- Save_Global_References --
13872 ----------------------------
13873
13874 procedure Save_Global_References (Templ : Node_Id) is
13875
13876 -- ??? it is horrible to use global variables in highly recursive code
13877
13878 E : Entity_Id;
13879 -- The entity of the current associated node
13880
13881 Gen_Scope : Entity_Id;
13882 -- The scope of the generic for which references are being saved
13883
13884 N2 : Node_Id;
13885 -- The current associated node
13886
13887 function Is_Global (E : Entity_Id) return Boolean;
13888 -- Check whether entity is defined outside of generic unit. Examine the
13889 -- scope of an entity, and the scope of the scope, etc, until we find
13890 -- either Standard, in which case the entity is global, or the generic
13891 -- unit itself, which indicates that the entity is local. If the entity
13892 -- is the generic unit itself, as in the case of a recursive call, or
13893 -- the enclosing generic unit, if different from the current scope, then
13894 -- it is local as well, because it will be replaced at the point of
13895 -- instantiation. On the other hand, if it is a reference to a child
13896 -- unit of a common ancestor, which appears in an instantiation, it is
13897 -- global because it is used to denote a specific compilation unit at
13898 -- the time the instantiations will be analyzed.
13899
13900 procedure Qualify_Universal_Operands
13901 (Op : Node_Id;
13902 Func_Call : Node_Id);
13903 -- Op denotes a binary or unary operator in generic template Templ. Node
13904 -- Func_Call is the function call alternative of the operator within the
13905 -- the analyzed copy of the template. Change each operand which yields a
13906 -- universal type by wrapping it into a qualified expression
13907 --
13908 -- Actual_Typ'(Operand)
13909 --
13910 -- where Actual_Typ is the type of corresponding actual parameter of
13911 -- Operand in Func_Call.
13912
13913 procedure Reset_Entity (N : Node_Id);
13914 -- Save semantic information on global entity so that it is not resolved
13915 -- again at instantiation time.
13916
13917 procedure Save_Entity_Descendants (N : Node_Id);
13918 -- Apply Save_Global_References to the two syntactic descendants of
13919 -- non-terminal nodes that carry an Associated_Node and are processed
13920 -- through Reset_Entity. Once the global entity (if any) has been
13921 -- captured together with its type, only two syntactic descendants need
13922 -- to be traversed to complete the processing of the tree rooted at N.
13923 -- This applies to Selected_Components, Expanded_Names, and to Operator
13924 -- nodes. N can also be a character literal, identifier, or operator
13925 -- symbol node, but the call has no effect in these cases.
13926
13927 procedure Save_Global_Defaults (N1 : Node_Id; N2 : Node_Id);
13928 -- Default actuals in nested instances must be handled specially
13929 -- because there is no link to them from the original tree. When an
13930 -- actual subprogram is given by a default, we add an explicit generic
13931 -- association for it in the instantiation node. When we save the
13932 -- global references on the name of the instance, we recover the list
13933 -- of generic associations, and add an explicit one to the original
13934 -- generic tree, through which a global actual can be preserved.
13935 -- Similarly, if a child unit is instantiated within a sibling, in the
13936 -- context of the parent, we must preserve the identifier of the parent
13937 -- so that it can be properly resolved in a subsequent instantiation.
13938
13939 procedure Save_Global_Descendant (D : Union_Id);
13940 -- Apply Save_References recursively to the descendants of node D
13941
13942 procedure Save_References (N : Node_Id);
13943 -- This is the recursive procedure that does the work, once the
13944 -- enclosing generic scope has been established.
13945
13946 ---------------
13947 -- Is_Global --
13948 ---------------
13949
13950 function Is_Global (E : Entity_Id) return Boolean is
13951 Se : Entity_Id;
13952
13953 function Is_Instance_Node (Decl : Node_Id) return Boolean;
13954 -- Determine whether the parent node of a reference to a child unit
13955 -- denotes an instantiation or a formal package, in which case the
13956 -- reference to the child unit is global, even if it appears within
13957 -- the current scope (e.g. when the instance appears within the body
13958 -- of an ancestor).
13959
13960 ----------------------
13961 -- Is_Instance_Node --
13962 ----------------------
13963
13964 function Is_Instance_Node (Decl : Node_Id) return Boolean is
13965 begin
13966 return Nkind (Decl) in N_Generic_Instantiation
13967 or else
13968 Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration;
13969 end Is_Instance_Node;
13970
13971 -- Start of processing for Is_Global
13972
13973 begin
13974 if E = Gen_Scope then
13975 return False;
13976
13977 elsif E = Standard_Standard then
13978 return True;
13979
13980 elsif Is_Child_Unit (E)
13981 and then (Is_Instance_Node (Parent (N2))
13982 or else (Nkind (Parent (N2)) = N_Expanded_Name
13983 and then N2 = Selector_Name (Parent (N2))
13984 and then
13985 Is_Instance_Node (Parent (Parent (N2)))))
13986 then
13987 return True;
13988
13989 else
13990 Se := Scope (E);
13991 while Se /= Gen_Scope loop
13992 if Se = Standard_Standard then
13993 return True;
13994 else
13995 Se := Scope (Se);
13996 end if;
13997 end loop;
13998
13999 return False;
14000 end if;
14001 end Is_Global;
14002
14003 --------------------------------
14004 -- Qualify_Universal_Operands --
14005 --------------------------------
14006
14007 procedure Qualify_Universal_Operands
14008 (Op : Node_Id;
14009 Func_Call : Node_Id)
14010 is
14011 procedure Qualify_Operand (Opnd : Node_Id; Actual : Node_Id);
14012 -- Rewrite operand Opnd as a qualified expression of the form
14013 --
14014 -- Actual_Typ'(Opnd)
14015 --
14016 -- where Actual is the corresponding actual parameter of Opnd in
14017 -- function call Func_Call.
14018
14019 function Qualify_Type
14020 (Loc : Source_Ptr;
14021 Typ : Entity_Id) return Node_Id;
14022 -- Qualify type Typ by creating a selected component of the form
14023 --
14024 -- Scope_Of_Typ.Typ
14025
14026 ---------------------
14027 -- Qualify_Operand --
14028 ---------------------
14029
14030 procedure Qualify_Operand (Opnd : Node_Id; Actual : Node_Id) is
14031 Loc : constant Source_Ptr := Sloc (Opnd);
14032 Typ : constant Entity_Id := Etype (Actual);
14033 Mark : Node_Id;
14034 Qual : Node_Id;
14035
14036 begin
14037 -- Qualify the operand when it is of a universal type. Note that
14038 -- the template is unanalyzed and it is not possible to directly
14039 -- query the type. This transformation is not done when the type
14040 -- of the actual is internally generated because the type will be
14041 -- regenerated in the instance.
14042
14043 if Yields_Universal_Type (Opnd)
14044 and then Comes_From_Source (Typ)
14045 and then not Is_Hidden (Typ)
14046 then
14047 -- The type of the actual may be a global reference. Save this
14048 -- information by creating a reference to it.
14049
14050 if Is_Global (Typ) then
14051 Mark := New_Occurrence_Of (Typ, Loc);
14052
14053 -- Otherwise rely on resolution to find the proper type within
14054 -- the instance.
14055
14056 else
14057 Mark := Qualify_Type (Loc, Typ);
14058 end if;
14059
14060 Qual :=
14061 Make_Qualified_Expression (Loc,
14062 Subtype_Mark => Mark,
14063 Expression => Relocate_Node (Opnd));
14064
14065 -- Mark the qualification to distinguish it from other source
14066 -- constructs and signal the instantiation mechanism that this
14067 -- node requires special processing. See Copy_Generic_Node for
14068 -- details.
14069
14070 Set_Is_Qualified_Universal_Literal (Qual);
14071
14072 Rewrite (Opnd, Qual);
14073 end if;
14074 end Qualify_Operand;
14075
14076 ------------------
14077 -- Qualify_Type --
14078 ------------------
14079
14080 function Qualify_Type
14081 (Loc : Source_Ptr;
14082 Typ : Entity_Id) return Node_Id
14083 is
14084 Scop : constant Entity_Id := Scope (Typ);
14085 Result : Node_Id;
14086
14087 begin
14088 Result := Make_Identifier (Loc, Chars (Typ));
14089
14090 if Present (Scop) and then not Is_Generic_Unit (Scop) then
14091 Result :=
14092 Make_Selected_Component (Loc,
14093 Prefix => Make_Identifier (Loc, Chars (Scop)),
14094 Selector_Name => Result);
14095 end if;
14096
14097 return Result;
14098 end Qualify_Type;
14099
14100 -- Local variables
14101
14102 Actuals : constant List_Id := Parameter_Associations (Func_Call);
14103
14104 -- Start of processing for Qualify_Universal_Operands
14105
14106 begin
14107 if Nkind (Op) in N_Binary_Op then
14108 Qualify_Operand (Left_Opnd (Op), First (Actuals));
14109 Qualify_Operand (Right_Opnd (Op), Next (First (Actuals)));
14110
14111 elsif Nkind (Op) in N_Unary_Op then
14112 Qualify_Operand (Right_Opnd (Op), First (Actuals));
14113 end if;
14114 end Qualify_Universal_Operands;
14115
14116 ------------------
14117 -- Reset_Entity --
14118 ------------------
14119
14120 procedure Reset_Entity (N : Node_Id) is
14121 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id);
14122 -- If the type of N2 is global to the generic unit, save the type in
14123 -- the generic node. Just as we perform name capture for explicit
14124 -- references within the generic, we must capture the global types
14125 -- of local entities because they may participate in resolution in
14126 -- the instance.
14127
14128 function Top_Ancestor (E : Entity_Id) return Entity_Id;
14129 -- Find the ultimate ancestor of the current unit. If it is not a
14130 -- generic unit, then the name of the current unit in the prefix of
14131 -- an expanded name must be replaced with its generic homonym to
14132 -- ensure that it will be properly resolved in an instance.
14133
14134 ---------------------
14135 -- Set_Global_Type --
14136 ---------------------
14137
14138 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id) is
14139 Typ : constant Entity_Id := Etype (N2);
14140
14141 begin
14142 Set_Etype (N, Typ);
14143
14144 -- If the entity of N is not the associated node, this is a
14145 -- nested generic and it has an associated node as well, whose
14146 -- type is already the full view (see below). Indicate that the
14147 -- original node has a private view.
14148
14149 if Entity (N) /= N2 and then Has_Private_View (Entity (N)) then
14150 Set_Has_Private_View (N);
14151 end if;
14152
14153 -- If not a private type, nothing else to do
14154
14155 if not Is_Private_Type (Typ) then
14156 if Is_Array_Type (Typ)
14157 and then Is_Private_Type (Component_Type (Typ))
14158 then
14159 Set_Has_Private_View (N);
14160 end if;
14161
14162 -- If it is a derivation of a private type in a context where no
14163 -- full view is needed, nothing to do either.
14164
14165 elsif No (Full_View (Typ)) and then Typ /= Etype (Typ) then
14166 null;
14167
14168 -- Otherwise mark the type for flipping and use the full view when
14169 -- available.
14170
14171 else
14172 Set_Has_Private_View (N);
14173
14174 if Present (Full_View (Typ)) then
14175 Set_Etype (N2, Full_View (Typ));
14176 end if;
14177 end if;
14178
14179 if Is_Floating_Point_Type (Typ)
14180 and then Has_Dimension_System (Typ)
14181 then
14182 Copy_Dimensions (N2, N);
14183 end if;
14184
14185 end Set_Global_Type;
14186
14187 ------------------
14188 -- Top_Ancestor --
14189 ------------------
14190
14191 function Top_Ancestor (E : Entity_Id) return Entity_Id is
14192 Par : Entity_Id;
14193
14194 begin
14195 Par := E;
14196 while Is_Child_Unit (Par) loop
14197 Par := Scope (Par);
14198 end loop;
14199
14200 return Par;
14201 end Top_Ancestor;
14202
14203 -- Start of processing for Reset_Entity
14204
14205 begin
14206 N2 := Get_Associated_Node (N);
14207 E := Entity (N2);
14208
14209 if Present (E) then
14210
14211 -- If the node is an entry call to an entry in an enclosing task,
14212 -- it is rewritten as a selected component. No global entity to
14213 -- preserve in this case, since the expansion will be redone in
14214 -- the instance.
14215
14216 if not Nkind_In (E, N_Defining_Character_Literal,
14217 N_Defining_Identifier,
14218 N_Defining_Operator_Symbol)
14219 then
14220 Set_Associated_Node (N, Empty);
14221 Set_Etype (N, Empty);
14222 return;
14223 end if;
14224
14225 -- If the entity is an itype created as a subtype of an access
14226 -- type with a null exclusion restore source entity for proper
14227 -- visibility. The itype will be created anew in the instance.
14228
14229 if Is_Itype (E)
14230 and then Ekind (E) = E_Access_Subtype
14231 and then Is_Entity_Name (N)
14232 and then Chars (Etype (E)) = Chars (N)
14233 then
14234 E := Etype (E);
14235 Set_Entity (N2, E);
14236 Set_Etype (N2, E);
14237 end if;
14238
14239 if Is_Global (E) then
14240
14241 -- If the entity is a package renaming that is the prefix of
14242 -- an expanded name, it has been rewritten as the renamed
14243 -- package, which is necessary semantically but complicates
14244 -- ASIS tree traversal, so we recover the original entity to
14245 -- expose the renaming. Take into account that the context may
14246 -- be a nested generic, that the original node may itself have
14247 -- an associated node that had better be an entity, and that
14248 -- the current node is still a selected component.
14249
14250 if Ekind (E) = E_Package
14251 and then Nkind (N) = N_Selected_Component
14252 and then Nkind (Parent (N)) = N_Expanded_Name
14253 and then Present (Original_Node (N2))
14254 and then Is_Entity_Name (Original_Node (N2))
14255 and then Present (Entity (Original_Node (N2)))
14256 then
14257 if Is_Global (Entity (Original_Node (N2))) then
14258 N2 := Original_Node (N2);
14259 Set_Associated_Node (N, N2);
14260 Set_Global_Type (N, N2);
14261
14262 -- Renaming is local, and will be resolved in instance
14263
14264 else
14265 Set_Associated_Node (N, Empty);
14266 Set_Etype (N, Empty);
14267 end if;
14268
14269 else
14270 Set_Global_Type (N, N2);
14271 end if;
14272
14273 elsif Nkind (N) = N_Op_Concat
14274 and then Is_Generic_Type (Etype (N2))
14275 and then (Base_Type (Etype (Right_Opnd (N2))) = Etype (N2)
14276 or else
14277 Base_Type (Etype (Left_Opnd (N2))) = Etype (N2))
14278 and then Is_Intrinsic_Subprogram (E)
14279 then
14280 null;
14281
14282 -- Entity is local. Mark generic node as unresolved. Note that now
14283 -- it does not have an entity.
14284
14285 else
14286 Set_Associated_Node (N, Empty);
14287 Set_Etype (N, Empty);
14288 end if;
14289
14290 if Nkind (Parent (N)) in N_Generic_Instantiation
14291 and then N = Name (Parent (N))
14292 then
14293 Save_Global_Defaults (Parent (N), Parent (N2));
14294 end if;
14295
14296 elsif Nkind (Parent (N)) = N_Selected_Component
14297 and then Nkind (Parent (N2)) = N_Expanded_Name
14298 then
14299 if Is_Global (Entity (Parent (N2))) then
14300 Change_Selected_Component_To_Expanded_Name (Parent (N));
14301 Set_Associated_Node (Parent (N), Parent (N2));
14302 Set_Global_Type (Parent (N), Parent (N2));
14303 Save_Entity_Descendants (N);
14304
14305 -- If this is a reference to the current generic entity, replace
14306 -- by the name of the generic homonym of the current package. This
14307 -- is because in an instantiation Par.P.Q will not resolve to the
14308 -- name of the instance, whose enclosing scope is not necessarily
14309 -- Par. We use the generic homonym rather that the name of the
14310 -- generic itself because it may be hidden by a local declaration.
14311
14312 elsif In_Open_Scopes (Entity (Parent (N2)))
14313 and then not
14314 Is_Generic_Unit (Top_Ancestor (Entity (Prefix (Parent (N2)))))
14315 then
14316 if Ekind (Entity (Parent (N2))) = E_Generic_Package then
14317 Rewrite (Parent (N),
14318 Make_Identifier (Sloc (N),
14319 Chars =>
14320 Chars (Generic_Homonym (Entity (Parent (N2))))));
14321 else
14322 Rewrite (Parent (N),
14323 Make_Identifier (Sloc (N),
14324 Chars => Chars (Selector_Name (Parent (N2)))));
14325 end if;
14326 end if;
14327
14328 if Nkind (Parent (Parent (N))) in N_Generic_Instantiation
14329 and then Parent (N) = Name (Parent (Parent (N)))
14330 then
14331 Save_Global_Defaults
14332 (Parent (Parent (N)), Parent (Parent (N2)));
14333 end if;
14334
14335 -- A selected component may denote a static constant that has been
14336 -- folded. If the static constant is global to the generic, capture
14337 -- its value. Otherwise the folding will happen in any instantiation.
14338
14339 elsif Nkind (Parent (N)) = N_Selected_Component
14340 and then Nkind_In (Parent (N2), N_Integer_Literal, N_Real_Literal)
14341 then
14342 if Present (Entity (Original_Node (Parent (N2))))
14343 and then Is_Global (Entity (Original_Node (Parent (N2))))
14344 then
14345 Rewrite (Parent (N), New_Copy (Parent (N2)));
14346 Set_Analyzed (Parent (N), False);
14347 end if;
14348
14349 -- A selected component may be transformed into a parameterless
14350 -- function call. If the called entity is global, rewrite the node
14351 -- appropriately, i.e. as an extended name for the global entity.
14352
14353 elsif Nkind (Parent (N)) = N_Selected_Component
14354 and then Nkind (Parent (N2)) = N_Function_Call
14355 and then N = Selector_Name (Parent (N))
14356 then
14357 if No (Parameter_Associations (Parent (N2))) then
14358 if Is_Global (Entity (Name (Parent (N2)))) then
14359 Change_Selected_Component_To_Expanded_Name (Parent (N));
14360 Set_Associated_Node (Parent (N), Name (Parent (N2)));
14361 Set_Global_Type (Parent (N), Name (Parent (N2)));
14362 Save_Entity_Descendants (N);
14363
14364 else
14365 Set_Is_Prefixed_Call (Parent (N));
14366 Set_Associated_Node (N, Empty);
14367 Set_Etype (N, Empty);
14368 end if;
14369
14370 -- In Ada 2005, X.F may be a call to a primitive operation,
14371 -- rewritten as F (X). This rewriting will be done again in an
14372 -- instance, so keep the original node. Global entities will be
14373 -- captured as for other constructs. Indicate that this must
14374 -- resolve as a call, to prevent accidental overloading in the
14375 -- instance, if both a component and a primitive operation appear
14376 -- as candidates.
14377
14378 else
14379 Set_Is_Prefixed_Call (Parent (N));
14380 end if;
14381
14382 -- Entity is local. Reset in generic unit, so that node is resolved
14383 -- anew at the point of instantiation.
14384
14385 else
14386 Set_Associated_Node (N, Empty);
14387 Set_Etype (N, Empty);
14388 end if;
14389 end Reset_Entity;
14390
14391 -----------------------------
14392 -- Save_Entity_Descendants --
14393 -----------------------------
14394
14395 procedure Save_Entity_Descendants (N : Node_Id) is
14396 begin
14397 case Nkind (N) is
14398 when N_Binary_Op =>
14399 Save_Global_Descendant (Union_Id (Left_Opnd (N)));
14400 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
14401
14402 when N_Unary_Op =>
14403 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
14404
14405 when N_Expanded_Name |
14406 N_Selected_Component =>
14407 Save_Global_Descendant (Union_Id (Prefix (N)));
14408 Save_Global_Descendant (Union_Id (Selector_Name (N)));
14409
14410 when N_Identifier |
14411 N_Character_Literal |
14412 N_Operator_Symbol =>
14413 null;
14414
14415 when others =>
14416 raise Program_Error;
14417 end case;
14418 end Save_Entity_Descendants;
14419
14420 --------------------------
14421 -- Save_Global_Defaults --
14422 --------------------------
14423
14424 procedure Save_Global_Defaults (N1 : Node_Id; N2 : Node_Id) is
14425 Loc : constant Source_Ptr := Sloc (N1);
14426 Assoc2 : constant List_Id := Generic_Associations (N2);
14427 Gen_Id : constant Entity_Id := Get_Generic_Entity (N2);
14428 Assoc1 : List_Id;
14429 Act1 : Node_Id;
14430 Act2 : Node_Id;
14431 Def : Node_Id;
14432 Ndec : Node_Id;
14433 Subp : Entity_Id;
14434 Actual : Entity_Id;
14435
14436 begin
14437 Assoc1 := Generic_Associations (N1);
14438
14439 if Present (Assoc1) then
14440 Act1 := First (Assoc1);
14441 else
14442 Act1 := Empty;
14443 Set_Generic_Associations (N1, New_List);
14444 Assoc1 := Generic_Associations (N1);
14445 end if;
14446
14447 if Present (Assoc2) then
14448 Act2 := First (Assoc2);
14449 else
14450 return;
14451 end if;
14452
14453 while Present (Act1) and then Present (Act2) loop
14454 Next (Act1);
14455 Next (Act2);
14456 end loop;
14457
14458 -- Find the associations added for default subprograms
14459
14460 if Present (Act2) then
14461 while Nkind (Act2) /= N_Generic_Association
14462 or else No (Entity (Selector_Name (Act2)))
14463 or else not Is_Overloadable (Entity (Selector_Name (Act2)))
14464 loop
14465 Next (Act2);
14466 end loop;
14467
14468 -- Add a similar association if the default is global. The
14469 -- renaming declaration for the actual has been analyzed, and
14470 -- its alias is the program it renames. Link the actual in the
14471 -- original generic tree with the node in the analyzed tree.
14472
14473 while Present (Act2) loop
14474 Subp := Entity (Selector_Name (Act2));
14475 Def := Explicit_Generic_Actual_Parameter (Act2);
14476
14477 -- Following test is defence against rubbish errors
14478
14479 if No (Alias (Subp)) then
14480 return;
14481 end if;
14482
14483 -- Retrieve the resolved actual from the renaming declaration
14484 -- created for the instantiated formal.
14485
14486 Actual := Entity (Name (Parent (Parent (Subp))));
14487 Set_Entity (Def, Actual);
14488 Set_Etype (Def, Etype (Actual));
14489
14490 if Is_Global (Actual) then
14491 Ndec :=
14492 Make_Generic_Association (Loc,
14493 Selector_Name =>
14494 New_Occurrence_Of (Subp, Loc),
14495 Explicit_Generic_Actual_Parameter =>
14496 New_Occurrence_Of (Actual, Loc));
14497
14498 Set_Associated_Node
14499 (Explicit_Generic_Actual_Parameter (Ndec), Def);
14500
14501 Append (Ndec, Assoc1);
14502
14503 -- If there are other defaults, add a dummy association in case
14504 -- there are other defaulted formals with the same name.
14505
14506 elsif Present (Next (Act2)) then
14507 Ndec :=
14508 Make_Generic_Association (Loc,
14509 Selector_Name =>
14510 New_Occurrence_Of (Subp, Loc),
14511 Explicit_Generic_Actual_Parameter => Empty);
14512
14513 Append (Ndec, Assoc1);
14514 end if;
14515
14516 Next (Act2);
14517 end loop;
14518 end if;
14519
14520 if Nkind (Name (N1)) = N_Identifier
14521 and then Is_Child_Unit (Gen_Id)
14522 and then Is_Global (Gen_Id)
14523 and then Is_Generic_Unit (Scope (Gen_Id))
14524 and then In_Open_Scopes (Scope (Gen_Id))
14525 then
14526 -- This is an instantiation of a child unit within a sibling, so
14527 -- that the generic parent is in scope. An eventual instance must
14528 -- occur within the scope of an instance of the parent. Make name
14529 -- in instance into an expanded name, to preserve the identifier
14530 -- of the parent, so it can be resolved subsequently.
14531
14532 Rewrite (Name (N2),
14533 Make_Expanded_Name (Loc,
14534 Chars => Chars (Gen_Id),
14535 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
14536 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
14537 Set_Entity (Name (N2), Gen_Id);
14538
14539 Rewrite (Name (N1),
14540 Make_Expanded_Name (Loc,
14541 Chars => Chars (Gen_Id),
14542 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
14543 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
14544
14545 Set_Associated_Node (Name (N1), Name (N2));
14546 Set_Associated_Node (Prefix (Name (N1)), Empty);
14547 Set_Associated_Node
14548 (Selector_Name (Name (N1)), Selector_Name (Name (N2)));
14549 Set_Etype (Name (N1), Etype (Gen_Id));
14550 end if;
14551 end Save_Global_Defaults;
14552
14553 ----------------------------
14554 -- Save_Global_Descendant --
14555 ----------------------------
14556
14557 procedure Save_Global_Descendant (D : Union_Id) is
14558 N1 : Node_Id;
14559
14560 begin
14561 if D in Node_Range then
14562 if D = Union_Id (Empty) then
14563 null;
14564
14565 elsif Nkind (Node_Id (D)) /= N_Compilation_Unit then
14566 Save_References (Node_Id (D));
14567 end if;
14568
14569 elsif D in List_Range then
14570 if D = Union_Id (No_List) or else Is_Empty_List (List_Id (D)) then
14571 null;
14572
14573 else
14574 N1 := First (List_Id (D));
14575 while Present (N1) loop
14576 Save_References (N1);
14577 Next (N1);
14578 end loop;
14579 end if;
14580
14581 -- Element list or other non-node field, nothing to do
14582
14583 else
14584 null;
14585 end if;
14586 end Save_Global_Descendant;
14587
14588 ---------------------
14589 -- Save_References --
14590 ---------------------
14591
14592 -- This is the recursive procedure that does the work once the enclosing
14593 -- generic scope has been established. We have to treat specially a
14594 -- number of node rewritings that are required by semantic processing
14595 -- and which change the kind of nodes in the generic copy: typically
14596 -- constant-folding, replacing an operator node by a string literal, or
14597 -- a selected component by an expanded name. In each of those cases, the
14598 -- transformation is propagated to the generic unit.
14599
14600 procedure Save_References (N : Node_Id) is
14601 Loc : constant Source_Ptr := Sloc (N);
14602
14603 function Requires_Delayed_Save (Nod : Node_Id) return Boolean;
14604 -- Determine whether arbitrary node Nod requires delayed capture of
14605 -- global references within its aspect specifications.
14606
14607 procedure Save_References_In_Aggregate (N : Node_Id);
14608 -- Save all global references in [extension] aggregate node N
14609
14610 procedure Save_References_In_Char_Lit_Or_Op_Symbol (N : Node_Id);
14611 -- Save all global references in a character literal or operator
14612 -- symbol denoted by N.
14613
14614 procedure Save_References_In_Descendants (N : Node_Id);
14615 -- Save all global references in all descendants of node N
14616
14617 procedure Save_References_In_Identifier (N : Node_Id);
14618 -- Save all global references in identifier node N
14619
14620 procedure Save_References_In_Operator (N : Node_Id);
14621 -- Save all global references in operator node N
14622
14623 procedure Save_References_In_Pragma (Prag : Node_Id);
14624 -- Save all global references found within the expression of pragma
14625 -- Prag.
14626
14627 ---------------------------
14628 -- Requires_Delayed_Save --
14629 ---------------------------
14630
14631 function Requires_Delayed_Save (Nod : Node_Id) return Boolean is
14632 begin
14633 -- Generic packages and subprograms require delayed capture of
14634 -- global references within their aspects due to the timing of
14635 -- annotation analysis.
14636
14637 if Nkind_In (Nod, N_Generic_Package_Declaration,
14638 N_Generic_Subprogram_Declaration,
14639 N_Package_Body,
14640 N_Package_Body_Stub,
14641 N_Subprogram_Body,
14642 N_Subprogram_Body_Stub)
14643 then
14644 -- Since the capture of global references is done on the
14645 -- unanalyzed generic template, there is no information around
14646 -- to infer the context. Use the Associated_Entity linkages to
14647 -- peek into the analyzed generic copy and determine what the
14648 -- template corresponds to.
14649
14650 if Nod = Templ then
14651 return
14652 Is_Generic_Declaration_Or_Body
14653 (Unit_Declaration_Node
14654 (Associated_Entity (Defining_Entity (Nod))));
14655
14656 -- Otherwise the generic unit being processed is not the top
14657 -- level template. It is safe to capture of global references
14658 -- within the generic unit because at this point the top level
14659 -- copy is fully analyzed.
14660
14661 else
14662 return False;
14663 end if;
14664
14665 -- Otherwise capture the global references without interference
14666
14667 else
14668 return False;
14669 end if;
14670 end Requires_Delayed_Save;
14671
14672 ----------------------------------
14673 -- Save_References_In_Aggregate --
14674 ----------------------------------
14675
14676 procedure Save_References_In_Aggregate (N : Node_Id) is
14677 Nam : Node_Id;
14678 Qual : Node_Id := Empty;
14679 Typ : Entity_Id := Empty;
14680
14681 use Atree.Unchecked_Access;
14682 -- This code section is part of implementing an untyped tree
14683 -- traversal, so it needs direct access to node fields.
14684
14685 begin
14686 N2 := Get_Associated_Node (N);
14687
14688 if Present (N2) then
14689 Typ := Etype (N2);
14690
14691 -- In an instance within a generic, use the name of the actual
14692 -- and not the original generic parameter. If the actual is
14693 -- global in the current generic it must be preserved for its
14694 -- instantiation.
14695
14696 if Nkind (Parent (Typ)) = N_Subtype_Declaration
14697 and then Present (Generic_Parent_Type (Parent (Typ)))
14698 then
14699 Typ := Base_Type (Typ);
14700 Set_Etype (N2, Typ);
14701 end if;
14702 end if;
14703
14704 if No (N2) or else No (Typ) or else not Is_Global (Typ) then
14705 Set_Associated_Node (N, Empty);
14706
14707 -- If the aggregate is an actual in a call, it has been
14708 -- resolved in the current context, to some local type. The
14709 -- enclosing call may have been disambiguated by the aggregate,
14710 -- and this disambiguation might fail at instantiation time
14711 -- because the type to which the aggregate did resolve is not
14712 -- preserved. In order to preserve some of this information,
14713 -- wrap the aggregate in a qualified expression, using the id
14714 -- of its type. For further disambiguation we qualify the type
14715 -- name with its scope (if visible) because both id's will have
14716 -- corresponding entities in an instance. This resolves most of
14717 -- the problems with missing type information on aggregates in
14718 -- instances.
14719
14720 if Present (N2)
14721 and then Nkind (N2) = Nkind (N)
14722 and then Nkind (Parent (N2)) in N_Subprogram_Call
14723 and then Present (Typ)
14724 and then Comes_From_Source (Typ)
14725 then
14726 Nam := Make_Identifier (Loc, Chars (Typ));
14727
14728 if Is_Immediately_Visible (Scope (Typ)) then
14729 Nam :=
14730 Make_Selected_Component (Loc,
14731 Prefix =>
14732 Make_Identifier (Loc, Chars (Scope (Typ))),
14733 Selector_Name => Nam);
14734 end if;
14735
14736 Qual :=
14737 Make_Qualified_Expression (Loc,
14738 Subtype_Mark => Nam,
14739 Expression => Relocate_Node (N));
14740 end if;
14741 end if;
14742
14743 Save_Global_Descendant (Field1 (N));
14744 Save_Global_Descendant (Field2 (N));
14745 Save_Global_Descendant (Field3 (N));
14746 Save_Global_Descendant (Field5 (N));
14747
14748 if Present (Qual) then
14749 Rewrite (N, Qual);
14750 end if;
14751 end Save_References_In_Aggregate;
14752
14753 ----------------------------------------------
14754 -- Save_References_In_Char_Lit_Or_Op_Symbol --
14755 ----------------------------------------------
14756
14757 procedure Save_References_In_Char_Lit_Or_Op_Symbol (N : Node_Id) is
14758 begin
14759 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
14760 Reset_Entity (N);
14761
14762 elsif Nkind (N) = N_Operator_Symbol
14763 and then Nkind (Get_Associated_Node (N)) = N_String_Literal
14764 then
14765 Change_Operator_Symbol_To_String_Literal (N);
14766 end if;
14767 end Save_References_In_Char_Lit_Or_Op_Symbol;
14768
14769 ------------------------------------
14770 -- Save_References_In_Descendants --
14771 ------------------------------------
14772
14773 procedure Save_References_In_Descendants (N : Node_Id) is
14774 use Atree.Unchecked_Access;
14775 -- This code section is part of implementing an untyped tree
14776 -- traversal, so it needs direct access to node fields.
14777
14778 begin
14779 Save_Global_Descendant (Field1 (N));
14780 Save_Global_Descendant (Field2 (N));
14781 Save_Global_Descendant (Field3 (N));
14782 Save_Global_Descendant (Field4 (N));
14783 Save_Global_Descendant (Field5 (N));
14784 end Save_References_In_Descendants;
14785
14786 -----------------------------------
14787 -- Save_References_In_Identifier --
14788 -----------------------------------
14789
14790 procedure Save_References_In_Identifier (N : Node_Id) is
14791 begin
14792 -- The node did not undergo a transformation
14793
14794 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
14795
14796 -- If this is a discriminant reference, always save it. It is
14797 -- used in the instance to find the corresponding discriminant
14798 -- positionally rather than by name.
14799
14800 Set_Original_Discriminant
14801 (N, Original_Discriminant (Get_Associated_Node (N)));
14802 Reset_Entity (N);
14803
14804 -- The analysis of the generic copy transformed the identifier
14805 -- into another construct. Propagate the changes to the template.
14806
14807 else
14808 N2 := Get_Associated_Node (N);
14809
14810 -- The identifier denotes a call to a parameterless function.
14811 -- Mark the node as resolved when the function is external.
14812
14813 if Nkind (N2) = N_Function_Call then
14814 E := Entity (Name (N2));
14815
14816 if Present (E) and then Is_Global (E) then
14817 Set_Etype (N, Etype (N2));
14818 else
14819 Set_Associated_Node (N, Empty);
14820 Set_Etype (N, Empty);
14821 end if;
14822
14823 -- The identifier denotes a named number that was constant
14824 -- folded. Preserve the original name for ASIS and undo the
14825 -- constant folding which will be repeated in the instance.
14826
14827 elsif Nkind_In (N2, N_Integer_Literal, N_Real_Literal)
14828 and then Is_Entity_Name (Original_Node (N2))
14829 then
14830 Set_Associated_Node (N, Original_Node (N2));
14831 Reset_Entity (N);
14832
14833 -- The identifier resolved to a string literal. Propagate this
14834 -- information to the generic template.
14835
14836 elsif Nkind (N2) = N_String_Literal then
14837 Rewrite (N, New_Copy (N2));
14838
14839 -- The identifier is rewritten as a dereference if it is the
14840 -- prefix of an implicit dereference. Preserve the original
14841 -- tree as the analysis of the instance will expand the node
14842 -- again, but preserve the resolved entity if it is global.
14843
14844 elsif Nkind (N2) = N_Explicit_Dereference then
14845 if Is_Entity_Name (Prefix (N2))
14846 and then Present (Entity (Prefix (N2)))
14847 and then Is_Global (Entity (Prefix (N2)))
14848 then
14849 Set_Associated_Node (N, Prefix (N2));
14850
14851 elsif Nkind (Prefix (N2)) = N_Function_Call
14852 and then Present (Entity (Name (Prefix (N2))))
14853 and then Is_Global (Entity (Name (Prefix (N2))))
14854 then
14855 Rewrite (N,
14856 Make_Explicit_Dereference (Loc,
14857 Prefix =>
14858 Make_Function_Call (Loc,
14859 Name =>
14860 New_Occurrence_Of
14861 (Entity (Name (Prefix (N2))), Loc))));
14862
14863 else
14864 Set_Associated_Node (N, Empty);
14865 Set_Etype (N, Empty);
14866 end if;
14867
14868 -- The subtype mark of a nominally unconstrained object is
14869 -- rewritten as a subtype indication using the bounds of the
14870 -- expression. Recover the original subtype mark.
14871
14872 elsif Nkind (N2) = N_Subtype_Indication
14873 and then Is_Entity_Name (Original_Node (N2))
14874 then
14875 Set_Associated_Node (N, Original_Node (N2));
14876 Reset_Entity (N);
14877 end if;
14878 end if;
14879 end Save_References_In_Identifier;
14880
14881 ---------------------------------
14882 -- Save_References_In_Operator --
14883 ---------------------------------
14884
14885 procedure Save_References_In_Operator (N : Node_Id) is
14886 begin
14887 -- The node did not undergo a transformation
14888
14889 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
14890 if Nkind (N) = N_Op_Concat then
14891 Set_Is_Component_Left_Opnd (N,
14892 Is_Component_Left_Opnd (Get_Associated_Node (N)));
14893
14894 Set_Is_Component_Right_Opnd (N,
14895 Is_Component_Right_Opnd (Get_Associated_Node (N)));
14896 end if;
14897
14898 Reset_Entity (N);
14899
14900 -- The analysis of the generic copy transformed the operator into
14901 -- some other construct. Propagate the changes to the template if
14902 -- applicable.
14903
14904 else
14905 N2 := Get_Associated_Node (N);
14906
14907 -- The operator resoved to a function call
14908
14909 if Nkind (N2) = N_Function_Call then
14910
14911 -- Add explicit qualifications in the generic template for
14912 -- all operands of universal type. This aids resolution by
14913 -- preserving the actual type of a literal or an attribute
14914 -- that yields a universal result.
14915
14916 Qualify_Universal_Operands (N, N2);
14917
14918 E := Entity (Name (N2));
14919
14920 if Present (E) and then Is_Global (E) then
14921 Set_Etype (N, Etype (N2));
14922 else
14923 Set_Associated_Node (N, Empty);
14924 Set_Etype (N, Empty);
14925 end if;
14926
14927 -- The operator was folded into a literal
14928
14929 elsif Nkind_In (N2, N_Integer_Literal,
14930 N_Real_Literal,
14931 N_String_Literal)
14932 then
14933 if Present (Original_Node (N2))
14934 and then Nkind (Original_Node (N2)) = Nkind (N)
14935 then
14936 -- Operation was constant-folded. Whenever possible,
14937 -- recover semantic information from unfolded node,
14938 -- for ASIS use.
14939
14940 Set_Associated_Node (N, Original_Node (N2));
14941
14942 if Nkind (N) = N_Op_Concat then
14943 Set_Is_Component_Left_Opnd (N,
14944 Is_Component_Left_Opnd (Get_Associated_Node (N)));
14945 Set_Is_Component_Right_Opnd (N,
14946 Is_Component_Right_Opnd (Get_Associated_Node (N)));
14947 end if;
14948
14949 Reset_Entity (N);
14950
14951 -- Propagate the constant folding back to the template
14952
14953 else
14954 Rewrite (N, New_Copy (N2));
14955 Set_Analyzed (N, False);
14956 end if;
14957
14958 -- The operator was folded into an enumeration literal. Retain
14959 -- the entity to avoid spurious ambiguities if it is overloaded
14960 -- at the point of instantiation or inlining.
14961
14962 elsif Nkind (N2) = N_Identifier
14963 and then Ekind (Entity (N2)) = E_Enumeration_Literal
14964 then
14965 Rewrite (N, New_Copy (N2));
14966 Set_Analyzed (N, False);
14967 end if;
14968 end if;
14969
14970 -- Complete the operands check if node has not been constant
14971 -- folded.
14972
14973 if Nkind (N) in N_Op then
14974 Save_Entity_Descendants (N);
14975 end if;
14976 end Save_References_In_Operator;
14977
14978 -------------------------------
14979 -- Save_References_In_Pragma --
14980 -------------------------------
14981
14982 procedure Save_References_In_Pragma (Prag : Node_Id) is
14983 Context : Node_Id;
14984 Do_Save : Boolean := True;
14985
14986 use Atree.Unchecked_Access;
14987 -- This code section is part of implementing an untyped tree
14988 -- traversal, so it needs direct access to node fields.
14989
14990 begin
14991 -- Do not save global references in pragmas generated from aspects
14992 -- because the pragmas will be regenerated at instantiation time.
14993
14994 if From_Aspect_Specification (Prag) then
14995 Do_Save := False;
14996
14997 -- The capture of global references within contract-related source
14998 -- pragmas associated with generic packages, subprograms or their
14999 -- respective bodies must be delayed due to timing of annotation
15000 -- analysis. Global references are still captured in routine
15001 -- Save_Global_References_In_Contract.
15002
15003 elsif Is_Generic_Contract_Pragma (Prag) and then Prag /= Templ then
15004 if Is_Package_Contract_Annotation (Prag) then
15005 Context := Find_Related_Package_Or_Body (Prag);
15006 else
15007 pragma Assert (Is_Subprogram_Contract_Annotation (Prag));
15008 Context := Find_Related_Declaration_Or_Body (Prag);
15009 end if;
15010
15011 -- The use of Original_Node accounts for the case when the
15012 -- related context is generic template.
15013
15014 if Requires_Delayed_Save (Original_Node (Context)) then
15015 Do_Save := False;
15016 end if;
15017 end if;
15018
15019 -- For all other cases, save all global references within the
15020 -- descendants, but skip the following semantic fields:
15021
15022 -- Field1 - Next_Pragma
15023 -- Field3 - Corresponding_Aspect
15024 -- Field5 - Next_Rep_Item
15025
15026 if Do_Save then
15027 Save_Global_Descendant (Field2 (Prag));
15028 Save_Global_Descendant (Field4 (Prag));
15029 end if;
15030 end Save_References_In_Pragma;
15031
15032 -- Start of processing for Save_References
15033
15034 begin
15035 if N = Empty then
15036 null;
15037
15038 -- Aggregates
15039
15040 elsif Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
15041 Save_References_In_Aggregate (N);
15042
15043 -- Character literals, operator symbols
15044
15045 elsif Nkind_In (N, N_Character_Literal, N_Operator_Symbol) then
15046 Save_References_In_Char_Lit_Or_Op_Symbol (N);
15047
15048 -- Defining identifiers
15049
15050 elsif Nkind (N) in N_Entity then
15051 null;
15052
15053 -- Identifiers
15054
15055 elsif Nkind (N) = N_Identifier then
15056 Save_References_In_Identifier (N);
15057
15058 -- Operators
15059
15060 elsif Nkind (N) in N_Op then
15061 Save_References_In_Operator (N);
15062
15063 -- Pragmas
15064
15065 elsif Nkind (N) = N_Pragma then
15066 Save_References_In_Pragma (N);
15067
15068 else
15069 Save_References_In_Descendants (N);
15070 end if;
15071
15072 -- Save all global references found within the aspect specifications
15073 -- of the related node.
15074
15075 if Permits_Aspect_Specifications (N) and then Has_Aspects (N) then
15076
15077 -- The capture of global references within aspects associated with
15078 -- generic packages, subprograms or their bodies must be delayed
15079 -- due to timing of annotation analysis. Global references are
15080 -- still captured in routine Save_Global_References_In_Contract.
15081
15082 if Requires_Delayed_Save (N) then
15083 null;
15084
15085 -- Otherwise save all global references within the aspects
15086
15087 else
15088 Save_Global_References_In_Aspects (N);
15089 end if;
15090 end if;
15091 end Save_References;
15092
15093 -- Start of processing for Save_Global_References
15094
15095 begin
15096 Gen_Scope := Current_Scope;
15097
15098 -- If the generic unit is a child unit, references to entities in the
15099 -- parent are treated as local, because they will be resolved anew in
15100 -- the context of the instance of the parent.
15101
15102 while Is_Child_Unit (Gen_Scope)
15103 and then Ekind (Scope (Gen_Scope)) = E_Generic_Package
15104 loop
15105 Gen_Scope := Scope (Gen_Scope);
15106 end loop;
15107
15108 Save_References (Templ);
15109 end Save_Global_References;
15110
15111 ---------------------------------------
15112 -- Save_Global_References_In_Aspects --
15113 ---------------------------------------
15114
15115 procedure Save_Global_References_In_Aspects (N : Node_Id) is
15116 Asp : Node_Id;
15117 Expr : Node_Id;
15118
15119 begin
15120 Asp := First (Aspect_Specifications (N));
15121 while Present (Asp) loop
15122 Expr := Expression (Asp);
15123
15124 if Present (Expr) then
15125 Save_Global_References (Expr);
15126 end if;
15127
15128 Next (Asp);
15129 end loop;
15130 end Save_Global_References_In_Aspects;
15131
15132 --------------------------------------
15133 -- Set_Copied_Sloc_For_Inlined_Body --
15134 --------------------------------------
15135
15136 procedure Set_Copied_Sloc_For_Inlined_Body (N : Node_Id; E : Entity_Id) is
15137 begin
15138 Create_Instantiation_Source (N, E, True, S_Adjustment);
15139 end Set_Copied_Sloc_For_Inlined_Body;
15140
15141 ---------------------
15142 -- Set_Instance_Of --
15143 ---------------------
15144
15145 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id) is
15146 begin
15147 Generic_Renamings.Table (Generic_Renamings.Last) := (A, B, Assoc_Null);
15148 Generic_Renamings_HTable.Set (Generic_Renamings.Last);
15149 Generic_Renamings.Increment_Last;
15150 end Set_Instance_Of;
15151
15152 --------------------
15153 -- Set_Next_Assoc --
15154 --------------------
15155
15156 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr) is
15157 begin
15158 Generic_Renamings.Table (E).Next_In_HTable := Next;
15159 end Set_Next_Assoc;
15160
15161 -------------------
15162 -- Start_Generic --
15163 -------------------
15164
15165 procedure Start_Generic is
15166 begin
15167 -- ??? More things could be factored out in this routine.
15168 -- Should probably be done at a later stage.
15169
15170 Generic_Flags.Append (Inside_A_Generic);
15171 Inside_A_Generic := True;
15172
15173 Expander_Mode_Save_And_Set (False);
15174 end Start_Generic;
15175
15176 ----------------------
15177 -- Set_Instance_Env --
15178 ----------------------
15179
15180 procedure Set_Instance_Env
15181 (Gen_Unit : Entity_Id;
15182 Act_Unit : Entity_Id)
15183 is
15184 Assertion_Status : constant Boolean := Assertions_Enabled;
15185 Save_SPARK_Mode : constant SPARK_Mode_Type := SPARK_Mode;
15186 Save_SPARK_Mode_Pragma : constant Node_Id := SPARK_Mode_Pragma;
15187
15188 begin
15189 -- Regardless of the current mode, predefined units are analyzed in the
15190 -- most current Ada mode, and earlier version Ada checks do not apply
15191 -- to predefined units. Nothing needs to be done for non-internal units.
15192 -- These are always analyzed in the current mode.
15193
15194 if Is_Internal_File_Name
15195 (Fname => Unit_File_Name (Get_Source_Unit (Gen_Unit)),
15196 Renamings_Included => True)
15197 then
15198 Set_Opt_Config_Switches (True, Current_Sem_Unit = Main_Unit);
15199
15200 -- In Ada2012 we may want to enable assertions in an instance of a
15201 -- predefined unit, in which case we need to preserve the current
15202 -- setting for the Assertions_Enabled flag. This will become more
15203 -- critical when pre/postconditions are added to predefined units,
15204 -- as is already the case for some numeric libraries.
15205
15206 if Ada_Version >= Ada_2012 then
15207 Assertions_Enabled := Assertion_Status;
15208 end if;
15209
15210 -- SPARK_Mode for an instance is the one applicable at the point of
15211 -- instantiation.
15212
15213 SPARK_Mode := Save_SPARK_Mode;
15214 SPARK_Mode_Pragma := Save_SPARK_Mode_Pragma;
15215
15216 -- Make sure dynamic elaboration checks are off in SPARK Mode
15217
15218 if SPARK_Mode = On then
15219 Dynamic_Elaboration_Checks := False;
15220 end if;
15221 end if;
15222
15223 Current_Instantiated_Parent :=
15224 (Gen_Id => Gen_Unit,
15225 Act_Id => Act_Unit,
15226 Next_In_HTable => Assoc_Null);
15227 end Set_Instance_Env;
15228
15229 -----------------
15230 -- Switch_View --
15231 -----------------
15232
15233 procedure Switch_View (T : Entity_Id) is
15234 BT : constant Entity_Id := Base_Type (T);
15235 Priv_Elmt : Elmt_Id := No_Elmt;
15236 Priv_Sub : Entity_Id;
15237
15238 begin
15239 -- T may be private but its base type may have been exchanged through
15240 -- some other occurrence, in which case there is nothing to switch
15241 -- besides T itself. Note that a private dependent subtype of a private
15242 -- type might not have been switched even if the base type has been,
15243 -- because of the last branch of Check_Private_View (see comment there).
15244
15245 if not Is_Private_Type (BT) then
15246 Prepend_Elmt (Full_View (T), Exchanged_Views);
15247 Exchange_Declarations (T);
15248 return;
15249 end if;
15250
15251 Priv_Elmt := First_Elmt (Private_Dependents (BT));
15252
15253 if Present (Full_View (BT)) then
15254 Prepend_Elmt (Full_View (BT), Exchanged_Views);
15255 Exchange_Declarations (BT);
15256 end if;
15257
15258 while Present (Priv_Elmt) loop
15259 Priv_Sub := (Node (Priv_Elmt));
15260
15261 -- We avoid flipping the subtype if the Etype of its full view is
15262 -- private because this would result in a malformed subtype. This
15263 -- occurs when the Etype of the subtype full view is the full view of
15264 -- the base type (and since the base types were just switched, the
15265 -- subtype is pointing to the wrong view). This is currently the case
15266 -- for tagged record types, access types (maybe more?) and needs to
15267 -- be resolved. ???
15268
15269 if Present (Full_View (Priv_Sub))
15270 and then not Is_Private_Type (Etype (Full_View (Priv_Sub)))
15271 then
15272 Prepend_Elmt (Full_View (Priv_Sub), Exchanged_Views);
15273 Exchange_Declarations (Priv_Sub);
15274 end if;
15275
15276 Next_Elmt (Priv_Elmt);
15277 end loop;
15278 end Switch_View;
15279
15280 -----------------
15281 -- True_Parent --
15282 -----------------
15283
15284 function True_Parent (N : Node_Id) return Node_Id is
15285 begin
15286 if Nkind (Parent (N)) = N_Subunit then
15287 return Parent (Corresponding_Stub (Parent (N)));
15288 else
15289 return Parent (N);
15290 end if;
15291 end True_Parent;
15292
15293 -----------------------------
15294 -- Valid_Default_Attribute --
15295 -----------------------------
15296
15297 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id) is
15298 Attr_Id : constant Attribute_Id :=
15299 Get_Attribute_Id (Attribute_Name (Def));
15300 T : constant Entity_Id := Entity (Prefix (Def));
15301 Is_Fun : constant Boolean := (Ekind (Nam) = E_Function);
15302 F : Entity_Id;
15303 Num_F : Nat;
15304 OK : Boolean;
15305
15306 begin
15307 if No (T) or else T = Any_Id then
15308 return;
15309 end if;
15310
15311 Num_F := 0;
15312 F := First_Formal (Nam);
15313 while Present (F) loop
15314 Num_F := Num_F + 1;
15315 Next_Formal (F);
15316 end loop;
15317
15318 case Attr_Id is
15319 when Attribute_Adjacent | Attribute_Ceiling | Attribute_Copy_Sign |
15320 Attribute_Floor | Attribute_Fraction | Attribute_Machine |
15321 Attribute_Model | Attribute_Remainder | Attribute_Rounding |
15322 Attribute_Unbiased_Rounding =>
15323 OK := Is_Fun
15324 and then Num_F = 1
15325 and then Is_Floating_Point_Type (T);
15326
15327 when Attribute_Image | Attribute_Pred | Attribute_Succ |
15328 Attribute_Value | Attribute_Wide_Image |
15329 Attribute_Wide_Value =>
15330 OK := (Is_Fun and then Num_F = 1 and then Is_Scalar_Type (T));
15331
15332 when Attribute_Max | Attribute_Min =>
15333 OK := (Is_Fun and then Num_F = 2 and then Is_Scalar_Type (T));
15334
15335 when Attribute_Input =>
15336 OK := (Is_Fun and then Num_F = 1);
15337
15338 when Attribute_Output | Attribute_Read | Attribute_Write =>
15339 OK := (not Is_Fun and then Num_F = 2);
15340
15341 when others =>
15342 OK := False;
15343 end case;
15344
15345 if not OK then
15346 Error_Msg_N
15347 ("attribute reference has wrong profile for subprogram", Def);
15348 end if;
15349 end Valid_Default_Attribute;
15350
15351 end Sem_Ch12;