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