sem_aggr.adb, [...]: Update all eligible case statements to reflect the new style...
[gcc.git] / gcc / ada / sem_ch12.adb
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S E M _ C H 1 2 --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2016, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
25
26 with Aspects; use Aspects;
27 with Atree; use Atree;
28 with Contracts; use Contracts;
29 with Einfo; use Einfo;
30 with Elists; use Elists;
31 with Errout; use Errout;
32 with Expander; use Expander;
33 with Exp_Disp; use Exp_Disp;
34 with Fname; use Fname;
35 with Fname.UF; use Fname.UF;
36 with Freeze; use Freeze;
37 with Ghost; use Ghost;
38 with Itypes; use Itypes;
39 with Lib; use Lib;
40 with Lib.Load; use Lib.Load;
41 with Lib.Xref; use Lib.Xref;
42 with Nlists; use Nlists;
43 with Namet; use Namet;
44 with Nmake; use Nmake;
45 with Opt; use Opt;
46 with Rident; use Rident;
47 with Restrict; use Restrict;
48 with Rtsfind; use Rtsfind;
49 with Sem; use Sem;
50 with Sem_Aux; use Sem_Aux;
51 with Sem_Cat; use Sem_Cat;
52 with Sem_Ch3; use Sem_Ch3;
53 with Sem_Ch6; use Sem_Ch6;
54 with Sem_Ch7; use Sem_Ch7;
55 with Sem_Ch8; use Sem_Ch8;
56 with Sem_Ch10; use Sem_Ch10;
57 with Sem_Ch13; use Sem_Ch13;
58 with Sem_Dim; use Sem_Dim;
59 with Sem_Disp; use Sem_Disp;
60 with Sem_Elab; use Sem_Elab;
61 with Sem_Elim; use Sem_Elim;
62 with Sem_Eval; use Sem_Eval;
63 with Sem_Prag; use Sem_Prag;
64 with Sem_Res; use Sem_Res;
65 with Sem_Type; use Sem_Type;
66 with Sem_Util; use Sem_Util;
67 with Sem_Warn; use Sem_Warn;
68 with Stand; use Stand;
69 with Sinfo; use Sinfo;
70 with Sinfo.CN; use Sinfo.CN;
71 with Sinput; use Sinput;
72 with Sinput.L; use Sinput.L;
73 with Snames; use Snames;
74 with Stringt; use Stringt;
75 with Uname; use Uname;
76 with Table;
77 with Tbuild; use Tbuild;
78 with Uintp; use Uintp;
79 with Urealp; use Urealp;
80 with Warnsw; use Warnsw;
81
82 with GNAT.HTable;
83
84 package body Sem_Ch12 is
85
86 ----------------------------------------------------------
87 -- Implementation of Generic Analysis and Instantiation --
88 ----------------------------------------------------------
89
90 -- GNAT implements generics by macro expansion. No attempt is made to share
91 -- generic instantiations (for now). Analysis of a generic definition does
92 -- not perform any expansion action, but the expander must be called on the
93 -- tree for each instantiation, because the expansion may of course depend
94 -- on the generic actuals. All of this is best achieved as follows:
95 --
96 -- a) Semantic analysis of a generic unit is performed on a copy of the
97 -- tree for the generic unit. All tree modifications that follow analysis
98 -- do not affect the original tree. Links are kept between the original
99 -- tree and the copy, in order to recognize non-local references within
100 -- the generic, and propagate them to each instance (recall that name
101 -- resolution is done on the generic declaration: generics are not really
102 -- macros). This is summarized in the following diagram:
103
104 -- .-----------. .----------.
105 -- | semantic |<--------------| generic |
106 -- | copy | | unit |
107 -- | |==============>| |
108 -- |___________| global |__________|
109 -- references | | |
110 -- | | |
111 -- .-----|--|.
112 -- | .-----|---.
113 -- | | .----------.
114 -- | | | generic |
115 -- |__| | |
116 -- |__| instance |
117 -- |__________|
118
119 -- b) Each instantiation copies the original tree, and inserts into it a
120 -- series of declarations that describe the mapping between generic formals
121 -- and actuals. For example, a generic In OUT parameter is an object
122 -- renaming of the corresponding actual, etc. Generic IN parameters are
123 -- constant declarations.
124
125 -- c) In order to give the right visibility for these renamings, we use
126 -- a different scheme for package and subprogram instantiations. For
127 -- packages, the list of renamings is inserted into the package
128 -- specification, before the visible declarations of the package. The
129 -- renamings are analyzed before any of the text of the instance, and are
130 -- thus visible at the right place. Furthermore, outside of the instance,
131 -- the generic parameters are visible and denote their corresponding
132 -- actuals.
133
134 -- For subprograms, we create a container package to hold the renamings
135 -- and the subprogram instance itself. Analysis of the package makes the
136 -- renaming declarations visible to the subprogram. After analyzing the
137 -- package, the defining entity for the subprogram is touched-up so that
138 -- it appears declared in the current scope, and not inside the container
139 -- package.
140
141 -- If the instantiation is a compilation unit, the container package is
142 -- given the same name as the subprogram instance. This ensures that
143 -- the elaboration procedure called by the binder, using the compilation
144 -- unit name, calls in fact the elaboration procedure for the package.
145
146 -- Not surprisingly, private types complicate this approach. By saving in
147 -- the original generic object the non-local references, we guarantee that
148 -- the proper entities are referenced at the point of instantiation.
149 -- However, for private types, this by itself does not insure that the
150 -- proper VIEW of the entity is used (the full type may be visible at the
151 -- point of generic definition, but not at instantiation, or vice-versa).
152 -- In order to reference the proper view, we special-case any reference
153 -- to private types in the generic object, by saving both views, one in
154 -- the generic and one in the semantic copy. At time of instantiation, we
155 -- check whether the two views are consistent, and exchange declarations if
156 -- necessary, in order to restore the correct visibility. Similarly, if
157 -- the instance view is private when the generic view was not, we perform
158 -- the exchange. After completing the instantiation, we restore the
159 -- current visibility. The flag Has_Private_View marks identifiers in the
160 -- the generic unit that require checking.
161
162 -- Visibility within nested generic units requires special handling.
163 -- Consider the following scheme:
164
165 -- type Global is ... -- outside of generic unit.
166 -- generic ...
167 -- package Outer is
168 -- ...
169 -- type Semi_Global is ... -- global to inner.
170
171 -- generic ... -- 1
172 -- procedure inner (X1 : Global; X2 : Semi_Global);
173
174 -- procedure in2 is new inner (...); -- 4
175 -- end Outer;
176
177 -- package New_Outer is new Outer (...); -- 2
178 -- procedure New_Inner is new New_Outer.Inner (...); -- 3
179
180 -- The semantic analysis of Outer captures all occurrences of Global.
181 -- The semantic analysis of Inner (at 1) captures both occurrences of
182 -- Global and Semi_Global.
183
184 -- At point 2 (instantiation of Outer), we also produce a generic copy
185 -- of Inner, even though Inner is, at that point, not being instantiated.
186 -- (This is just part of the semantic analysis of New_Outer).
187
188 -- Critically, references to Global within Inner must be preserved, while
189 -- references to Semi_Global should not preserved, because they must now
190 -- resolve to an entity within New_Outer. To distinguish between these, we
191 -- use a global variable, Current_Instantiated_Parent, which is set when
192 -- performing a generic copy during instantiation (at 2). This variable is
193 -- used when performing a generic copy that is not an instantiation, but
194 -- that is nested within one, as the occurrence of 1 within 2. The analysis
195 -- of a nested generic only preserves references that are global to the
196 -- enclosing Current_Instantiated_Parent. We use the Scope_Depth value to
197 -- determine whether a reference is external to the given parent.
198
199 -- The instantiation at point 3 requires no special treatment. The method
200 -- works as well for further nestings of generic units, but of course the
201 -- variable Current_Instantiated_Parent must be stacked because nested
202 -- instantiations can occur, e.g. the occurrence of 4 within 2.
203
204 -- The instantiation of package and subprogram bodies is handled in a
205 -- similar manner, except that it is delayed until after semantic
206 -- analysis is complete. In this fashion complex cross-dependencies
207 -- between several package declarations and bodies containing generics
208 -- can be compiled which otherwise would diagnose spurious circularities.
209
210 -- For example, it is possible to compile two packages A and B that
211 -- have the following structure:
212
213 -- package A is package B is
214 -- generic ... generic ...
215 -- package G_A is package G_B is
216
217 -- with B; with A;
218 -- package body A is package body B is
219 -- package N_B is new G_B (..) package N_A is new G_A (..)
220
221 -- The table Pending_Instantiations in package Inline is used to keep
222 -- track of body instantiations that are delayed in this manner. Inline
223 -- handles the actual calls to do the body instantiations. This activity
224 -- is part of Inline, since the processing occurs at the same point, and
225 -- for essentially the same reason, as the handling of inlined routines.
226
227 ----------------------------------------------
228 -- Detection of Instantiation Circularities --
229 ----------------------------------------------
230
231 -- If we have a chain of instantiations that is circular, this is static
232 -- error which must be detected at compile time. The detection of these
233 -- circularities is carried out at the point that we insert a generic
234 -- instance spec or body. If there is a circularity, then the analysis of
235 -- the offending spec or body will eventually result in trying to load the
236 -- same unit again, and we detect this problem as we analyze the package
237 -- instantiation for the second time.
238
239 -- At least in some cases after we have detected the circularity, we get
240 -- into trouble if we try to keep going. The following flag is set if a
241 -- circularity is detected, and used to abandon compilation after the
242 -- messages have been posted.
243
244 -----------------------------------------
245 -- Implementation of Generic Contracts --
246 -----------------------------------------
247
248 -- A "contract" is a collection of aspects and pragmas that either verify a
249 -- property of a construct at runtime or classify the data flow to and from
250 -- the construct in some fashion.
251
252 -- Generic packages, subprograms and their respective bodies may be subject
253 -- to the following contract-related aspects or pragmas collectively known
254 -- as annotations:
255
256 -- package subprogram [body]
257 -- Abstract_State Contract_Cases
258 -- Initial_Condition Depends
259 -- Initializes Extensions_Visible
260 -- Global
261 -- package body Post
262 -- Refined_State Post_Class
263 -- Postcondition
264 -- Pre
265 -- Pre_Class
266 -- Precondition
267 -- Refined_Depends
268 -- Refined_Global
269 -- Refined_Post
270 -- Test_Case
271
272 -- Most package contract annotations utilize forward references to classify
273 -- data declared within the package [body]. Subprogram annotations then use
274 -- the classifications to further refine them. These inter dependencies are
275 -- problematic with respect to the implementation of generics because their
276 -- analysis, capture of global references and instantiation does not mesh
277 -- well with the existing mechanism.
278
279 -- 1) Analysis of generic contracts is carried out the same way non-generic
280 -- contracts are analyzed:
281
282 -- 1.1) General rule - a contract is analyzed after all related aspects
283 -- and pragmas are analyzed. This is done by routines
284
285 -- Analyze_Package_Body_Contract
286 -- Analyze_Package_Contract
287 -- Analyze_Subprogram_Body_Contract
288 -- Analyze_Subprogram_Contract
289
290 -- 1.2) Compilation unit - the contract is analyzed after Pragmas_After
291 -- are processed.
292
293 -- 1.3) Compilation unit body - the contract is analyzed at the end of
294 -- the body declaration list.
295
296 -- 1.4) Package - the contract is analyzed at the end of the private or
297 -- visible declarations, prior to analyzing the contracts of any nested
298 -- packages or subprograms.
299
300 -- 1.5) Package body - the contract is analyzed at the end of the body
301 -- declaration list, prior to analyzing the contracts of any nested
302 -- packages or subprograms.
303
304 -- 1.6) Subprogram - if the subprogram is declared inside a block, a
305 -- package or a subprogram, then its contract is analyzed at the end of
306 -- the enclosing declarations, otherwise the subprogram is a compilation
307 -- unit 1.2).
308
309 -- 1.7) Subprogram body - if the subprogram body is declared inside a
310 -- block, a package body or a subprogram body, then its contract is
311 -- analyzed at the end of the enclosing declarations, otherwise the
312 -- subprogram is a compilation unit 1.3).
313
314 -- 2) Capture of global references within contracts is done after capturing
315 -- global references within the generic template. There are two reasons for
316 -- this delay - pragma annotations are not part of the generic template in
317 -- the case of a generic subprogram declaration, and analysis of contracts
318 -- is delayed.
319
320 -- Contract-related source pragmas within generic templates are prepared
321 -- for delayed capture of global references by routine
322
323 -- Create_Generic_Contract
324
325 -- The routine associates these pragmas with the contract of the template.
326 -- In the case of a generic subprogram declaration, the routine creates
327 -- generic templates for the pragmas declared after the subprogram because
328 -- they are not part of the template.
329
330 -- generic -- template starts
331 -- procedure Gen_Proc (Input : Integer); -- template ends
332 -- pragma Precondition (Input > 0); -- requires own template
333
334 -- 2.1) The capture of global references with aspect specifications and
335 -- source pragmas that apply to a generic unit must be suppressed when
336 -- the generic template is being processed because the contracts have not
337 -- been analyzed yet. Any attempts to capture global references at that
338 -- point will destroy the Associated_Node linkages and leave the template
339 -- undecorated. This delay is controlled by routine
340
341 -- Requires_Delayed_Save
342
343 -- 2.2) The real capture of global references within a contract is done
344 -- after the contract has been analyzed, by routine
345
346 -- Save_Global_References_In_Contract
347
348 -- 3) The instantiation of a generic contract occurs as part of the
349 -- instantiation of the contract owner. Generic subprogram declarations
350 -- require additional processing when the contract is specified by pragmas
351 -- because the pragmas are not part of the generic template. This is done
352 -- by routine
353
354 -- Instantiate_Subprogram_Contract
355
356 Circularity_Detected : Boolean := False;
357 -- This should really be reset on encountering a new main unit, but in
358 -- practice we are not using multiple main units so it is not critical.
359
360 --------------------------------------------------
361 -- Formal packages and partial parameterization --
362 --------------------------------------------------
363
364 -- When compiling a generic, a formal package is a local instantiation. If
365 -- declared with a box, its generic formals are visible in the enclosing
366 -- generic. If declared with a partial list of actuals, those actuals that
367 -- are defaulted (covered by an Others clause, or given an explicit box
368 -- initialization) are also visible in the enclosing generic, while those
369 -- that have a corresponding actual are not.
370
371 -- In our source model of instantiation, the same visibility must be
372 -- present in the spec and body of an instance: the names of the formals
373 -- that are defaulted must be made visible within the instance, and made
374 -- invisible (hidden) after the instantiation is complete, so that they
375 -- are not accessible outside of the instance.
376
377 -- In a generic, a formal package is treated like a special instantiation.
378 -- Our Ada 95 compiler handled formals with and without box in different
379 -- ways. With partial parameterization, we use a single model for both.
380 -- We create a package declaration that consists of the specification of
381 -- the generic package, and a set of declarations that map the actuals
382 -- into local renamings, just as we do for bona fide instantiations. For
383 -- defaulted parameters and formals with a box, we copy directly the
384 -- declarations of the formal into this local package. The result is a
385 -- a package whose visible declarations may include generic formals. This
386 -- package is only used for type checking and visibility analysis, and
387 -- never reaches the back-end, so it can freely violate the placement
388 -- rules for generic formal declarations.
389
390 -- The list of declarations (renamings and copies of formals) is built
391 -- by Analyze_Associations, just as for regular instantiations.
392
393 -- At the point of instantiation, conformance checking must be applied only
394 -- to those parameters that were specified in the formal. We perform this
395 -- checking by creating another internal instantiation, this one including
396 -- only the renamings and the formals (the rest of the package spec is not
397 -- relevant to conformance checking). We can then traverse two lists: the
398 -- list of actuals in the instance that corresponds to the formal package,
399 -- and the list of actuals produced for this bogus instantiation. We apply
400 -- the conformance rules to those actuals that are not defaulted (i.e.
401 -- which still appear as generic formals.
402
403 -- When we compile an instance body we must make the right parameters
404 -- visible again. The predicate Is_Generic_Formal indicates which of the
405 -- formals should have its Is_Hidden flag reset.
406
407 -----------------------
408 -- Local subprograms --
409 -----------------------
410
411 procedure Abandon_Instantiation (N : Node_Id);
412 pragma No_Return (Abandon_Instantiation);
413 -- Posts an error message "instantiation abandoned" at the indicated node
414 -- and then raises the exception Instantiation_Error to do it.
415
416 procedure Analyze_Formal_Array_Type
417 (T : in out Entity_Id;
418 Def : Node_Id);
419 -- A formal array type is treated like an array type declaration, and
420 -- invokes Array_Type_Declaration (sem_ch3) whose first parameter is
421 -- in-out, because in the case of an anonymous type the entity is
422 -- actually created in the procedure.
423
424 -- The following procedures treat other kinds of formal parameters
425
426 procedure Analyze_Formal_Derived_Interface_Type
427 (N : Node_Id;
428 T : Entity_Id;
429 Def : Node_Id);
430
431 procedure Analyze_Formal_Derived_Type
432 (N : Node_Id;
433 T : Entity_Id;
434 Def : Node_Id);
435
436 procedure Analyze_Formal_Interface_Type
437 (N : Node_Id;
438 T : Entity_Id;
439 Def : Node_Id);
440
441 -- The following subprograms create abbreviated declarations for formal
442 -- scalar types. We introduce an anonymous base of the proper class for
443 -- each of them, and define the formals as constrained first subtypes of
444 -- their bases. The bounds are expressions that are non-static in the
445 -- generic.
446
447 procedure Analyze_Formal_Decimal_Fixed_Point_Type
448 (T : Entity_Id; Def : Node_Id);
449 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id);
450 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id);
451 procedure Analyze_Formal_Signed_Integer_Type (T : Entity_Id; Def : Node_Id);
452 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id);
453 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
454 (T : Entity_Id; Def : Node_Id);
455
456 procedure Analyze_Formal_Private_Type
457 (N : Node_Id;
458 T : Entity_Id;
459 Def : Node_Id);
460 -- Creates a new private type, which does not require completion
461
462 procedure Analyze_Formal_Incomplete_Type (T : Entity_Id; Def : Node_Id);
463 -- Ada 2012: Creates a new incomplete type whose actual does not freeze
464
465 procedure Analyze_Generic_Formal_Part (N : Node_Id);
466 -- Analyze generic formal part
467
468 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id);
469 -- Create a new access type with the given designated type
470
471 function Analyze_Associations
472 (I_Node : Node_Id;
473 Formals : List_Id;
474 F_Copy : List_Id) return List_Id;
475 -- At instantiation time, build the list of associations between formals
476 -- and actuals. Each association becomes a renaming declaration for the
477 -- formal entity. F_Copy is the analyzed list of formals in the generic
478 -- copy. It is used to apply legality checks to the actuals. I_Node is the
479 -- instantiation node itself.
480
481 procedure Analyze_Subprogram_Instantiation
482 (N : Node_Id;
483 K : Entity_Kind);
484
485 procedure Build_Instance_Compilation_Unit_Nodes
486 (N : Node_Id;
487 Act_Body : Node_Id;
488 Act_Decl : Node_Id);
489 -- This procedure is used in the case where the generic instance of a
490 -- subprogram body or package body is a library unit. In this case, the
491 -- original library unit node for the generic instantiation must be
492 -- replaced by the resulting generic body, and a link made to a new
493 -- compilation unit node for the generic declaration. The argument N is
494 -- the original generic instantiation. Act_Body and Act_Decl are the body
495 -- and declaration of the instance (either package body and declaration
496 -- nodes or subprogram body and declaration nodes depending on the case).
497 -- On return, the node N has been rewritten with the actual body.
498
499 procedure Check_Access_Definition (N : Node_Id);
500 -- Subsidiary routine to null exclusion processing. Perform an assertion
501 -- check on Ada version and the presence of an access definition in N.
502
503 procedure Check_Formal_Packages (P_Id : Entity_Id);
504 -- Apply the following to all formal packages in generic associations
505
506 procedure Check_Formal_Package_Instance
507 (Formal_Pack : Entity_Id;
508 Actual_Pack : Entity_Id);
509 -- Verify that the actuals of the actual instance match the actuals of
510 -- the template for a formal package that is not declared with a box.
511
512 procedure Check_Forward_Instantiation (Decl : Node_Id);
513 -- If the generic is a local entity and the corresponding body has not
514 -- been seen yet, flag enclosing packages to indicate that it will be
515 -- elaborated after the generic body. Subprograms declared in the same
516 -- package cannot be inlined by the front end because front-end inlining
517 -- requires a strict linear order of elaboration.
518
519 function Check_Hidden_Primitives (Assoc_List : List_Id) return Elist_Id;
520 -- Check if some association between formals and actuals requires to make
521 -- visible primitives of a tagged type, and make those primitives visible.
522 -- Return the list of primitives whose visibility is modified (to restore
523 -- their visibility later through Restore_Hidden_Primitives). If no
524 -- candidate is found then return No_Elist.
525
526 procedure Check_Hidden_Child_Unit
527 (N : Node_Id;
528 Gen_Unit : Entity_Id;
529 Act_Decl_Id : Entity_Id);
530 -- If the generic unit is an implicit child instance within a parent
531 -- instance, we need to make an explicit test that it is not hidden by
532 -- a child instance of the same name and parent.
533
534 procedure Check_Generic_Actuals
535 (Instance : Entity_Id;
536 Is_Formal_Box : Boolean);
537 -- Similar to previous one. Check the actuals in the instantiation,
538 -- whose views can change between the point of instantiation and the point
539 -- of instantiation of the body. In addition, mark the generic renamings
540 -- as generic actuals, so that they are not compatible with other actuals.
541 -- Recurse on an actual that is a formal package whose declaration has
542 -- a box.
543
544 function Contains_Instance_Of
545 (Inner : Entity_Id;
546 Outer : Entity_Id;
547 N : Node_Id) return Boolean;
548 -- Inner is instantiated within the generic Outer. Check whether Inner
549 -- directly or indirectly contains an instance of Outer or of one of its
550 -- parents, in the case of a subunit. Each generic unit holds a list of
551 -- the entities instantiated within (at any depth). This procedure
552 -- determines whether the set of such lists contains a cycle, i.e. an
553 -- illegal circular instantiation.
554
555 function Denotes_Formal_Package
556 (Pack : Entity_Id;
557 On_Exit : Boolean := False;
558 Instance : Entity_Id := Empty) return Boolean;
559 -- Returns True if E is a formal package of an enclosing generic, or
560 -- the actual for such a formal in an enclosing instantiation. If such
561 -- a package is used as a formal in an nested generic, or as an actual
562 -- in a nested instantiation, the visibility of ITS formals should not
563 -- be modified. When called from within Restore_Private_Views, the flag
564 -- On_Exit is true, to indicate that the search for a possible enclosing
565 -- instance should ignore the current one. In that case Instance denotes
566 -- the declaration for which this is an actual. This declaration may be
567 -- an instantiation in the source, or the internal instantiation that
568 -- corresponds to the actual for a formal package.
569
570 function Earlier (N1, N2 : Node_Id) return Boolean;
571 -- Yields True if N1 and N2 appear in the same compilation unit,
572 -- ignoring subunits, and if N1 is to the left of N2 in a left-to-right
573 -- traversal of the tree for the unit. Used to determine the placement
574 -- of freeze nodes for instance bodies that may depend on other instances.
575
576 function Find_Actual_Type
577 (Typ : Entity_Id;
578 Gen_Type : Entity_Id) return Entity_Id;
579 -- When validating the actual types of a child instance, check whether
580 -- the formal is a formal type of the parent unit, and retrieve the current
581 -- actual for it. Typ is the entity in the analyzed formal type declaration
582 -- (component or index type of an array type, or designated type of an
583 -- access formal) and Gen_Type is the enclosing analyzed formal array
584 -- or access type. The desired actual may be a formal of a parent, or may
585 -- be declared in a formal package of a parent. In both cases it is a
586 -- generic actual type because it appears within a visible instance.
587 -- Finally, it may be declared in a parent unit without being a formal
588 -- of that unit, in which case it must be retrieved by visibility.
589 -- Ambiguities may still arise if two homonyms are declared in two formal
590 -- packages, and the prefix of the formal type may be needed to resolve
591 -- the ambiguity in the instance ???
592
593 procedure Freeze_Subprogram_Body
594 (Inst_Node : Node_Id;
595 Gen_Body : Node_Id;
596 Pack_Id : Entity_Id);
597 -- The generic body may appear textually after the instance, including
598 -- in the proper body of a stub, or within a different package instance.
599 -- Given that the instance can only be elaborated after the generic, we
600 -- place freeze_nodes for the instance and/or for packages that may enclose
601 -- the instance and the generic, so that the back-end can establish the
602 -- proper order of elaboration.
603
604 function Get_Associated_Node (N : Node_Id) return Node_Id;
605 -- In order to propagate semantic information back from the analyzed copy
606 -- to the original generic, we maintain links between selected nodes in the
607 -- generic and their corresponding copies. At the end of generic analysis,
608 -- the routine Save_Global_References traverses the generic tree, examines
609 -- the semantic information, and preserves the links to those nodes that
610 -- contain global information. At instantiation, the information from the
611 -- associated node is placed on the new copy, so that name resolution is
612 -- not repeated.
613 --
614 -- Three kinds of source nodes have associated nodes:
615 --
616 -- a) those that can reference (denote) entities, that is identifiers,
617 -- character literals, expanded_names, operator symbols, operators,
618 -- and attribute reference nodes. These nodes have an Entity field
619 -- and are the set of nodes that are in N_Has_Entity.
620 --
621 -- b) aggregates (N_Aggregate and N_Extension_Aggregate)
622 --
623 -- c) selected components (N_Selected_Component)
624 --
625 -- For the first class, the associated node preserves the entity if it is
626 -- global. If the generic contains nested instantiations, the associated
627 -- node itself has been recopied, and a chain of them must be followed.
628 --
629 -- For aggregates, the associated node allows retrieval of the type, which
630 -- may otherwise not appear in the generic. The view of this type may be
631 -- different between generic and instantiation, and the full view can be
632 -- installed before the instantiation is analyzed. For aggregates of type
633 -- extensions, the same view exchange may have to be performed for some of
634 -- the ancestor types, if their view is private at the point of
635 -- instantiation.
636 --
637 -- Nodes that are selected components in the parse tree may be rewritten
638 -- as expanded names after resolution, and must be treated as potential
639 -- entity holders, which is why they also have an Associated_Node.
640 --
641 -- Nodes that do not come from source, such as freeze nodes, do not appear
642 -- in the generic tree, and need not have an associated node.
643 --
644 -- The associated node is stored in the Associated_Node field. Note that
645 -- this field overlaps Entity, which is fine, because the whole point is
646 -- that we don't need or want the normal Entity field in this situation.
647
648 function Has_Been_Exchanged (E : Entity_Id) return Boolean;
649 -- Traverse the Exchanged_Views list to see if a type was private
650 -- and has already been flipped during this phase of instantiation.
651
652 procedure Hide_Current_Scope;
653 -- When instantiating a generic child unit, the parent context must be
654 -- present, but the instance and all entities that may be generated
655 -- must be inserted in the current scope. We leave the current scope
656 -- on the stack, but make its entities invisible to avoid visibility
657 -- problems. This is reversed at the end of the instantiation. This is
658 -- not done for the instantiation of the bodies, which only require the
659 -- instances of the generic parents to be in scope.
660
661 function In_Same_Declarative_Part
662 (F_Node : Node_Id;
663 Inst : Node_Id) return Boolean;
664 -- True if the instantiation Inst and the given freeze_node F_Node appear
665 -- within the same declarative part, ignoring subunits, but with no inter-
666 -- vening subprograms or concurrent units. Used to find the proper plave
667 -- for the freeze node of an instance, when the generic is declared in a
668 -- previous instance. If predicate is true, the freeze node of the instance
669 -- can be placed after the freeze node of the previous instance, Otherwise
670 -- it has to be placed at the end of the current declarative part.
671
672 function In_Main_Context (E : Entity_Id) return Boolean;
673 -- Check whether an instantiation is in the context of the main unit.
674 -- Used to determine whether its body should be elaborated to allow
675 -- front-end inlining.
676
677 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id);
678 -- Add the context clause of the unit containing a generic unit to a
679 -- compilation unit that is, or contains, an instantiation.
680
681 procedure Init_Env;
682 -- Establish environment for subsequent instantiation. Separated from
683 -- Save_Env because data-structures for visibility handling must be
684 -- initialized before call to Check_Generic_Child_Unit.
685
686 procedure Inline_Instance_Body
687 (N : Node_Id;
688 Gen_Unit : Entity_Id;
689 Act_Decl : Node_Id);
690 -- If front-end inlining is requested, instantiate the package body,
691 -- and preserve the visibility of its compilation unit, to insure
692 -- that successive instantiations succeed.
693
694 procedure Insert_Freeze_Node_For_Instance
695 (N : Node_Id;
696 F_Node : Node_Id);
697 -- N denotes a package or a subprogram instantiation and F_Node is the
698 -- associated freeze node. Insert the freeze node before the first source
699 -- body which follows immediately after N. If no such body is found, the
700 -- freeze node is inserted at the end of the declarative region which
701 -- contains N.
702
703 procedure Install_Body
704 (Act_Body : Node_Id;
705 N : Node_Id;
706 Gen_Body : Node_Id;
707 Gen_Decl : Node_Id);
708 -- If the instantiation happens textually before the body of the generic,
709 -- the instantiation of the body must be analyzed after the generic body,
710 -- and not at the point of instantiation. Such early instantiations can
711 -- happen if the generic and the instance appear in a package declaration
712 -- because the generic body can only appear in the corresponding package
713 -- body. Early instantiations can also appear if generic, instance and
714 -- body are all in the declarative part of a subprogram or entry. Entities
715 -- of packages that are early instantiations are delayed, and their freeze
716 -- node appears after the generic body. This rather complex machinery is
717 -- needed when nested instantiations are present, because the source does
718 -- not carry any indication of where the corresponding instance bodies must
719 -- be installed and frozen.
720
721 procedure Install_Formal_Packages (Par : Entity_Id);
722 -- Install the visible part of any formal of the parent that is a formal
723 -- package. Note that for the case of a formal package with a box, this
724 -- includes the formal part of the formal package (12.7(10/2)).
725
726 procedure Install_Hidden_Primitives
727 (Prims_List : in out Elist_Id;
728 Gen_T : Entity_Id;
729 Act_T : Entity_Id);
730 -- Remove suffix 'P' from hidden primitives of Act_T to match the
731 -- visibility of primitives of Gen_T. The list of primitives to which
732 -- the suffix is removed is added to Prims_List to restore them later.
733
734 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False);
735 -- When compiling an instance of a child unit the parent (which is
736 -- itself an instance) is an enclosing scope that must be made
737 -- immediately visible. This procedure is also used to install the non-
738 -- generic parent of a generic child unit when compiling its body, so
739 -- that full views of types in the parent are made visible.
740
741 -- The functions Instantiate_XXX perform various legality checks and build
742 -- the declarations for instantiated generic parameters. In all of these
743 -- Formal is the entity in the generic unit, Actual is the entity of
744 -- expression in the generic associations, and Analyzed_Formal is the
745 -- formal in the generic copy, which contains the semantic information to
746 -- be used to validate the actual.
747
748 function Instantiate_Object
749 (Formal : Node_Id;
750 Actual : Node_Id;
751 Analyzed_Formal : Node_Id) return List_Id;
752
753 function Instantiate_Type
754 (Formal : Node_Id;
755 Actual : Node_Id;
756 Analyzed_Formal : Node_Id;
757 Actual_Decls : List_Id) return List_Id;
758
759 function Instantiate_Formal_Subprogram
760 (Formal : Node_Id;
761 Actual : Node_Id;
762 Analyzed_Formal : Node_Id) return Node_Id;
763
764 function Instantiate_Formal_Package
765 (Formal : Node_Id;
766 Actual : Node_Id;
767 Analyzed_Formal : Node_Id) return List_Id;
768 -- If the formal package is declared with a box, special visibility rules
769 -- apply to its formals: they are in the visible part of the package. This
770 -- is true in the declarative region of the formal package, that is to say
771 -- in the enclosing generic or instantiation. For an instantiation, the
772 -- parameters of the formal package are made visible in an explicit step.
773 -- Furthermore, if the actual has a visible USE clause, these formals must
774 -- be made potentially use-visible as well. On exit from the enclosing
775 -- instantiation, the reverse must be done.
776
777 -- For a formal package declared without a box, there are conformance rules
778 -- that apply to the actuals in the generic declaration and the actuals of
779 -- the actual package in the enclosing instantiation. The simplest way to
780 -- apply these rules is to repeat the instantiation of the formal package
781 -- in the context of the enclosing instance, and compare the generic
782 -- associations of this instantiation with those of the actual package.
783 -- This internal instantiation only needs to contain the renamings of the
784 -- formals: the visible and private declarations themselves need not be
785 -- created.
786
787 -- In Ada 2005, the formal package may be only partially parameterized.
788 -- In that case the visibility step must make visible those actuals whose
789 -- corresponding formals were given with a box. A final complication
790 -- involves inherited operations from formal derived types, which must
791 -- be visible if the type is.
792
793 function Is_In_Main_Unit (N : Node_Id) return Boolean;
794 -- Test if given node is in the main unit
795
796 procedure Load_Parent_Of_Generic
797 (N : Node_Id;
798 Spec : Node_Id;
799 Body_Optional : Boolean := False);
800 -- If the generic appears in a separate non-generic library unit, load the
801 -- corresponding body to retrieve the body of the generic. N is the node
802 -- for the generic instantiation, Spec is the generic package declaration.
803 --
804 -- Body_Optional is a flag that indicates that the body is being loaded to
805 -- ensure that temporaries are generated consistently when there are other
806 -- instances in the current declarative part that precede the one being
807 -- loaded. In that case a missing body is acceptable.
808
809 procedure Map_Formal_Package_Entities (Form : Entity_Id; Act : Entity_Id);
810 -- Within the generic part, entities in the formal package are
811 -- visible. To validate subsequent type declarations, indicate
812 -- the correspondence between the entities in the analyzed formal,
813 -- and the entities in the actual package. There are three packages
814 -- involved in the instantiation of a formal package: the parent
815 -- generic P1 which appears in the generic declaration, the fake
816 -- instantiation P2 which appears in the analyzed generic, and whose
817 -- visible entities may be used in subsequent formals, and the actual
818 -- P3 in the instance. To validate subsequent formals, me indicate
819 -- that the entities in P2 are mapped into those of P3. The mapping of
820 -- entities has to be done recursively for nested packages.
821
822 procedure Move_Freeze_Nodes
823 (Out_Of : Entity_Id;
824 After : Node_Id;
825 L : List_Id);
826 -- Freeze nodes can be generated in the analysis of a generic unit, but
827 -- will not be seen by the back-end. It is necessary to move those nodes
828 -- to the enclosing scope if they freeze an outer entity. We place them
829 -- at the end of the enclosing generic package, which is semantically
830 -- neutral.
831
832 procedure Preanalyze_Actuals (N : Node_Id; Inst : Entity_Id := Empty);
833 -- Analyze actuals to perform name resolution. Full resolution is done
834 -- later, when the expected types are known, but names have to be captured
835 -- before installing parents of generics, that are not visible for the
836 -- actuals themselves.
837 --
838 -- If Inst is present, it is the entity of the package instance. This
839 -- entity is marked as having a limited_view actual when some actual is
840 -- a limited view. This is used to place the instance body properly.
841
842 procedure Remove_Parent (In_Body : Boolean := False);
843 -- Reverse effect after instantiation of child is complete
844
845 procedure Restore_Hidden_Primitives (Prims_List : in out Elist_Id);
846 -- Restore suffix 'P' to primitives of Prims_List and leave Prims_List
847 -- set to No_Elist.
848
849 procedure Set_Instance_Env
850 (Gen_Unit : Entity_Id;
851 Act_Unit : Entity_Id);
852 -- Save current instance on saved environment, to be used to determine
853 -- the global status of entities in nested instances. Part of Save_Env.
854 -- called after verifying that the generic unit is legal for the instance,
855 -- The procedure also examines whether the generic unit is a predefined
856 -- unit, in order to set configuration switches accordingly. As a result
857 -- the procedure must be called after analyzing and freezing the actuals.
858
859 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id);
860 -- Associate analyzed generic parameter with corresponding instance. Used
861 -- for semantic checks at instantiation time.
862
863 function True_Parent (N : Node_Id) return Node_Id;
864 -- For a subunit, return parent of corresponding stub, else return
865 -- parent of node.
866
867 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id);
868 -- Verify that an attribute that appears as the default for a formal
869 -- subprogram is a function or procedure with the correct profile.
870
871 -------------------------------------------
872 -- Data Structures for Generic Renamings --
873 -------------------------------------------
874
875 -- The map Generic_Renamings associates generic entities with their
876 -- corresponding actuals. Currently used to validate type instances. It
877 -- will eventually be used for all generic parameters to eliminate the
878 -- need for overload resolution in the instance.
879
880 type Assoc_Ptr is new Int;
881
882 Assoc_Null : constant Assoc_Ptr := -1;
883
884 type Assoc is record
885 Gen_Id : Entity_Id;
886 Act_Id : Entity_Id;
887 Next_In_HTable : Assoc_Ptr;
888 end record;
889
890 package Generic_Renamings is new Table.Table
891 (Table_Component_Type => Assoc,
892 Table_Index_Type => Assoc_Ptr,
893 Table_Low_Bound => 0,
894 Table_Initial => 10,
895 Table_Increment => 100,
896 Table_Name => "Generic_Renamings");
897
898 -- Variable to hold enclosing instantiation. When the environment is
899 -- saved for a subprogram inlining, the corresponding Act_Id is empty.
900
901 Current_Instantiated_Parent : Assoc := (Empty, Empty, Assoc_Null);
902
903 -- Hash table for associations
904
905 HTable_Size : constant := 37;
906 type HTable_Range is range 0 .. HTable_Size - 1;
907
908 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr);
909 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr;
910 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id;
911 function Hash (F : Entity_Id) return HTable_Range;
912
913 package Generic_Renamings_HTable is new GNAT.HTable.Static_HTable (
914 Header_Num => HTable_Range,
915 Element => Assoc,
916 Elmt_Ptr => Assoc_Ptr,
917 Null_Ptr => Assoc_Null,
918 Set_Next => Set_Next_Assoc,
919 Next => Next_Assoc,
920 Key => Entity_Id,
921 Get_Key => Get_Gen_Id,
922 Hash => Hash,
923 Equal => "=");
924
925 Exchanged_Views : Elist_Id;
926 -- This list holds the private views that have been exchanged during
927 -- instantiation to restore the visibility of the generic declaration.
928 -- (see comments above). After instantiation, the current visibility is
929 -- reestablished by means of a traversal of this list.
930
931 Hidden_Entities : Elist_Id;
932 -- This list holds the entities of the current scope that are removed
933 -- from immediate visibility when instantiating a child unit. Their
934 -- visibility is restored in Remove_Parent.
935
936 -- Because instantiations can be recursive, the following must be saved
937 -- on entry and restored on exit from an instantiation (spec or body).
938 -- This is done by the two procedures Save_Env and Restore_Env. For
939 -- package and subprogram instantiations (but not for the body instances)
940 -- the action of Save_Env is done in two steps: Init_Env is called before
941 -- Check_Generic_Child_Unit, because setting the parent instances requires
942 -- that the visibility data structures be properly initialized. Once the
943 -- generic is unit is validated, Set_Instance_Env completes Save_Env.
944
945 Parent_Unit_Visible : Boolean := False;
946 -- Parent_Unit_Visible is used when the generic is a child unit, and
947 -- indicates whether the ultimate parent of the generic is visible in the
948 -- instantiation environment. It is used to reset the visibility of the
949 -- parent at the end of the instantiation (see Remove_Parent).
950
951 Instance_Parent_Unit : Entity_Id := Empty;
952 -- This records the ultimate parent unit of an instance of a generic
953 -- child unit and is used in conjunction with Parent_Unit_Visible to
954 -- indicate the unit to which the Parent_Unit_Visible flag corresponds.
955
956 type Instance_Env is record
957 Instantiated_Parent : Assoc;
958 Exchanged_Views : Elist_Id;
959 Hidden_Entities : Elist_Id;
960 Current_Sem_Unit : Unit_Number_Type;
961 Parent_Unit_Visible : Boolean := False;
962 Instance_Parent_Unit : Entity_Id := Empty;
963 Switches : Config_Switches_Type;
964 end record;
965
966 package Instance_Envs is new Table.Table (
967 Table_Component_Type => Instance_Env,
968 Table_Index_Type => Int,
969 Table_Low_Bound => 0,
970 Table_Initial => 32,
971 Table_Increment => 100,
972 Table_Name => "Instance_Envs");
973
974 procedure Restore_Private_Views
975 (Pack_Id : Entity_Id;
976 Is_Package : Boolean := True);
977 -- Restore the private views of external types, and unmark the generic
978 -- renamings of actuals, so that they become compatible subtypes again.
979 -- For subprograms, Pack_Id is the package constructed to hold the
980 -- renamings.
981
982 procedure Switch_View (T : Entity_Id);
983 -- Switch the partial and full views of a type and its private
984 -- dependents (i.e. its subtypes and derived types).
985
986 ------------------------------------
987 -- Structures for Error Reporting --
988 ------------------------------------
989
990 Instantiation_Node : Node_Id;
991 -- Used by subprograms that validate instantiation of formal parameters
992 -- where there might be no actual on which to place the error message.
993 -- Also used to locate the instantiation node for generic subunits.
994
995 Instantiation_Error : exception;
996 -- When there is a semantic error in the generic parameter matching,
997 -- there is no point in continuing the instantiation, because the
998 -- number of cascaded errors is unpredictable. This exception aborts
999 -- the instantiation process altogether.
1000
1001 S_Adjustment : Sloc_Adjustment;
1002 -- Offset created for each node in an instantiation, in order to keep
1003 -- track of the source position of the instantiation in each of its nodes.
1004 -- A subsequent semantic error or warning on a construct of the instance
1005 -- points to both places: the original generic node, and the point of
1006 -- instantiation. See Sinput and Sinput.L for additional details.
1007
1008 ------------------------------------------------------------
1009 -- Data structure for keeping track when inside a Generic --
1010 ------------------------------------------------------------
1011
1012 -- The following table is used to save values of the Inside_A_Generic
1013 -- flag (see spec of Sem) when they are saved by Start_Generic.
1014
1015 package Generic_Flags is new Table.Table (
1016 Table_Component_Type => Boolean,
1017 Table_Index_Type => Int,
1018 Table_Low_Bound => 0,
1019 Table_Initial => 32,
1020 Table_Increment => 200,
1021 Table_Name => "Generic_Flags");
1022
1023 ---------------------------
1024 -- Abandon_Instantiation --
1025 ---------------------------
1026
1027 procedure Abandon_Instantiation (N : Node_Id) is
1028 begin
1029 Error_Msg_N ("\instantiation abandoned!", N);
1030 raise Instantiation_Error;
1031 end Abandon_Instantiation;
1032
1033 --------------------------------
1034 -- Add_Pending_Instantiation --
1035 --------------------------------
1036
1037 procedure Add_Pending_Instantiation (Inst : Node_Id; Act_Decl : Node_Id) is
1038 begin
1039
1040 -- Add to the instantiation node and the corresponding unit declaration
1041 -- the current values of global flags to be used when analyzing the
1042 -- instance body.
1043
1044 Pending_Instantiations.Append
1045 ((Inst_Node => Inst,
1046 Act_Decl => Act_Decl,
1047 Expander_Status => Expander_Active,
1048 Current_Sem_Unit => Current_Sem_Unit,
1049 Scope_Suppress => Scope_Suppress,
1050 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
1051 Version => Ada_Version,
1052 Version_Pragma => Ada_Version_Pragma,
1053 Warnings => Save_Warnings,
1054 SPARK_Mode => SPARK_Mode,
1055 SPARK_Mode_Pragma => SPARK_Mode_Pragma));
1056 end Add_Pending_Instantiation;
1057
1058 ----------------------------------
1059 -- Adjust_Inherited_Pragma_Sloc --
1060 ----------------------------------
1061
1062 procedure Adjust_Inherited_Pragma_Sloc (N : Node_Id) is
1063 begin
1064 Adjust_Instantiation_Sloc (N, S_Adjustment);
1065 end Adjust_Inherited_Pragma_Sloc;
1066
1067 --------------------------
1068 -- Analyze_Associations --
1069 --------------------------
1070
1071 function Analyze_Associations
1072 (I_Node : Node_Id;
1073 Formals : List_Id;
1074 F_Copy : List_Id) return List_Id
1075 is
1076 Actuals_To_Freeze : constant Elist_Id := New_Elmt_List;
1077 Assoc : constant List_Id := New_List;
1078 Default_Actuals : constant List_Id := New_List;
1079 Gen_Unit : constant Entity_Id :=
1080 Defining_Entity (Parent (F_Copy));
1081
1082 Actuals : List_Id;
1083 Actual : Node_Id;
1084 Analyzed_Formal : Node_Id;
1085 First_Named : Node_Id := Empty;
1086 Formal : Node_Id;
1087 Match : Node_Id;
1088 Named : Node_Id;
1089 Saved_Formal : Node_Id;
1090
1091 Default_Formals : constant List_Id := New_List;
1092 -- If an Others_Choice is present, some of the formals may be defaulted.
1093 -- To simplify the treatment of visibility in an instance, we introduce
1094 -- individual defaults for each such formal. These defaults are
1095 -- appended to the list of associations and replace the Others_Choice.
1096
1097 Found_Assoc : Node_Id;
1098 -- Association for the current formal being match. Empty if there are
1099 -- no remaining actuals, or if there is no named association with the
1100 -- name of the formal.
1101
1102 Is_Named_Assoc : Boolean;
1103 Num_Matched : Nat := 0;
1104 Num_Actuals : Nat := 0;
1105
1106 Others_Present : Boolean := False;
1107 Others_Choice : Node_Id := Empty;
1108 -- In Ada 2005, indicates partial parameterization of a formal
1109 -- package. As usual an other association must be last in the list.
1110
1111 procedure Check_Fixed_Point_Actual (Actual : Node_Id);
1112 -- Warn if an actual fixed-point type has user-defined arithmetic
1113 -- operations, but there is no corresponding formal in the generic,
1114 -- in which case the predefined operations will be used. This merits
1115 -- a warning because of the special semantics of fixed point ops.
1116
1117 procedure Check_Overloaded_Formal_Subprogram (Formal : Entity_Id);
1118 -- Apply RM 12.3(9): if a formal subprogram is overloaded, the instance
1119 -- cannot have a named association for it. AI05-0025 extends this rule
1120 -- to formals of formal packages by AI05-0025, and it also applies to
1121 -- box-initialized formals.
1122
1123 function Has_Fully_Defined_Profile (Subp : Entity_Id) return Boolean;
1124 -- Determine whether the parameter types and the return type of Subp
1125 -- are fully defined at the point of instantiation.
1126
1127 function Matching_Actual
1128 (F : Entity_Id;
1129 A_F : Entity_Id) return Node_Id;
1130 -- Find actual that corresponds to a given a formal parameter. If the
1131 -- actuals are positional, return the next one, if any. If the actuals
1132 -- are named, scan the parameter associations to find the right one.
1133 -- A_F is the corresponding entity in the analyzed generic, which is
1134 -- placed on the selector name for ASIS use.
1135 --
1136 -- In Ada 2005, a named association may be given with a box, in which
1137 -- case Matching_Actual sets Found_Assoc to the generic association,
1138 -- but return Empty for the actual itself. In this case the code below
1139 -- creates a corresponding declaration for the formal.
1140
1141 function Partial_Parameterization return Boolean;
1142 -- Ada 2005: if no match is found for a given formal, check if the
1143 -- association for it includes a box, or whether the associations
1144 -- include an Others clause.
1145
1146 procedure Process_Default (F : Entity_Id);
1147 -- Add a copy of the declaration of generic formal F to the list of
1148 -- associations, and add an explicit box association for F if there
1149 -- is none yet, and the default comes from an Others_Choice.
1150
1151 function Renames_Standard_Subprogram (Subp : Entity_Id) return Boolean;
1152 -- Determine whether Subp renames one of the subprograms defined in the
1153 -- generated package Standard.
1154
1155 procedure Set_Analyzed_Formal;
1156 -- Find the node in the generic copy that corresponds to a given formal.
1157 -- The semantic information on this node is used to perform legality
1158 -- checks on the actuals. Because semantic analysis can introduce some
1159 -- anonymous entities or modify the declaration node itself, the
1160 -- correspondence between the two lists is not one-one. In addition to
1161 -- anonymous types, the presence a formal equality will introduce an
1162 -- implicit declaration for the corresponding inequality.
1163
1164 ----------------------------------------
1165 -- Check_Overloaded_Formal_Subprogram --
1166 ----------------------------------------
1167
1168 procedure Check_Overloaded_Formal_Subprogram (Formal : Entity_Id) is
1169 Temp_Formal : Entity_Id;
1170
1171 begin
1172 Temp_Formal := First (Formals);
1173 while Present (Temp_Formal) loop
1174 if Nkind (Temp_Formal) in N_Formal_Subprogram_Declaration
1175 and then Temp_Formal /= Formal
1176 and then
1177 Chars (Defining_Unit_Name (Specification (Formal))) =
1178 Chars (Defining_Unit_Name (Specification (Temp_Formal)))
1179 then
1180 if Present (Found_Assoc) then
1181 Error_Msg_N
1182 ("named association not allowed for overloaded formal",
1183 Found_Assoc);
1184
1185 else
1186 Error_Msg_N
1187 ("named association not allowed for overloaded formal",
1188 Others_Choice);
1189 end if;
1190
1191 Abandon_Instantiation (Instantiation_Node);
1192 end if;
1193
1194 Next (Temp_Formal);
1195 end loop;
1196 end Check_Overloaded_Formal_Subprogram;
1197
1198 -------------------------------
1199 -- Check_Fixed_Point_Actual --
1200 -------------------------------
1201
1202 procedure Check_Fixed_Point_Actual (Actual : Node_Id) is
1203 Typ : constant Entity_Id := Entity (Actual);
1204 Prims : constant Elist_Id := Collect_Primitive_Operations (Typ);
1205 Elem : Elmt_Id;
1206 Formal : Node_Id;
1207
1208 begin
1209 -- Locate primitive operations of the type that are arithmetic
1210 -- operations.
1211
1212 Elem := First_Elmt (Prims);
1213 while Present (Elem) loop
1214 if Nkind (Node (Elem)) = N_Defining_Operator_Symbol then
1215
1216 -- Check whether the generic unit has a formal subprogram of
1217 -- the same name. This does not check types but is good enough
1218 -- to justify a warning.
1219
1220 Formal := First_Non_Pragma (Formals);
1221 while Present (Formal) loop
1222 if Nkind (Formal) = N_Formal_Concrete_Subprogram_Declaration
1223 and then Chars (Defining_Entity (Formal)) =
1224 Chars (Node (Elem))
1225 then
1226 exit;
1227 end if;
1228
1229 Next (Formal);
1230 end loop;
1231
1232 if No (Formal) then
1233 Error_Msg_Sloc := Sloc (Node (Elem));
1234 Error_Msg_NE
1235 ("?instance does not use primitive operation&#",
1236 Actual, Node (Elem));
1237 end if;
1238 end if;
1239
1240 Next_Elmt (Elem);
1241 end loop;
1242 end Check_Fixed_Point_Actual;
1243
1244 -------------------------------
1245 -- Has_Fully_Defined_Profile --
1246 -------------------------------
1247
1248 function Has_Fully_Defined_Profile (Subp : Entity_Id) return Boolean is
1249 function Is_Fully_Defined_Type (Typ : Entity_Id) return Boolean;
1250 -- Determine whethet type Typ is fully defined
1251
1252 ---------------------------
1253 -- Is_Fully_Defined_Type --
1254 ---------------------------
1255
1256 function Is_Fully_Defined_Type (Typ : Entity_Id) return Boolean is
1257 begin
1258 -- A private type without a full view is not fully defined
1259
1260 if Is_Private_Type (Typ)
1261 and then No (Full_View (Typ))
1262 then
1263 return False;
1264
1265 -- An incomplete type is never fully defined
1266
1267 elsif Is_Incomplete_Type (Typ) then
1268 return False;
1269
1270 -- All other types are fully defined
1271
1272 else
1273 return True;
1274 end if;
1275 end Is_Fully_Defined_Type;
1276
1277 -- Local declarations
1278
1279 Param : Entity_Id;
1280
1281 -- Start of processing for Has_Fully_Defined_Profile
1282
1283 begin
1284 -- Check the parameters
1285
1286 Param := First_Formal (Subp);
1287 while Present (Param) loop
1288 if not Is_Fully_Defined_Type (Etype (Param)) then
1289 return False;
1290 end if;
1291
1292 Next_Formal (Param);
1293 end loop;
1294
1295 -- Check the return type
1296
1297 return Is_Fully_Defined_Type (Etype (Subp));
1298 end Has_Fully_Defined_Profile;
1299
1300 ---------------------
1301 -- Matching_Actual --
1302 ---------------------
1303
1304 function Matching_Actual
1305 (F : Entity_Id;
1306 A_F : Entity_Id) return Node_Id
1307 is
1308 Prev : Node_Id;
1309 Act : Node_Id;
1310
1311 begin
1312 Is_Named_Assoc := False;
1313
1314 -- End of list of purely positional parameters
1315
1316 if No (Actual) or else Nkind (Actual) = N_Others_Choice then
1317 Found_Assoc := Empty;
1318 Act := Empty;
1319
1320 -- Case of positional parameter corresponding to current formal
1321
1322 elsif No (Selector_Name (Actual)) then
1323 Found_Assoc := Actual;
1324 Act := Explicit_Generic_Actual_Parameter (Actual);
1325 Num_Matched := Num_Matched + 1;
1326 Next (Actual);
1327
1328 -- Otherwise scan list of named actuals to find the one with the
1329 -- desired name. All remaining actuals have explicit names.
1330
1331 else
1332 Is_Named_Assoc := True;
1333 Found_Assoc := Empty;
1334 Act := Empty;
1335 Prev := Empty;
1336
1337 while Present (Actual) loop
1338 if Nkind (Actual) = N_Others_Choice then
1339 Found_Assoc := Empty;
1340 Act := Empty;
1341
1342 elsif Chars (Selector_Name (Actual)) = Chars (F) then
1343 Set_Entity (Selector_Name (Actual), A_F);
1344 Set_Etype (Selector_Name (Actual), Etype (A_F));
1345 Generate_Reference (A_F, Selector_Name (Actual));
1346
1347 Found_Assoc := Actual;
1348 Act := Explicit_Generic_Actual_Parameter (Actual);
1349 Num_Matched := Num_Matched + 1;
1350 exit;
1351 end if;
1352
1353 Prev := Actual;
1354 Next (Actual);
1355 end loop;
1356
1357 -- Reset for subsequent searches. In most cases the named
1358 -- associations are in order. If they are not, we reorder them
1359 -- to avoid scanning twice the same actual. This is not just a
1360 -- question of efficiency: there may be multiple defaults with
1361 -- boxes that have the same name. In a nested instantiation we
1362 -- insert actuals for those defaults, and cannot rely on their
1363 -- names to disambiguate them.
1364
1365 if Actual = First_Named then
1366 Next (First_Named);
1367
1368 elsif Present (Actual) then
1369 Insert_Before (First_Named, Remove_Next (Prev));
1370 end if;
1371
1372 Actual := First_Named;
1373 end if;
1374
1375 if Is_Entity_Name (Act) and then Present (Entity (Act)) then
1376 Set_Used_As_Generic_Actual (Entity (Act));
1377 end if;
1378
1379 return Act;
1380 end Matching_Actual;
1381
1382 ------------------------------
1383 -- Partial_Parameterization --
1384 ------------------------------
1385
1386 function Partial_Parameterization return Boolean is
1387 begin
1388 return Others_Present
1389 or else (Present (Found_Assoc) and then Box_Present (Found_Assoc));
1390 end Partial_Parameterization;
1391
1392 ---------------------
1393 -- Process_Default --
1394 ---------------------
1395
1396 procedure Process_Default (F : Entity_Id) is
1397 Loc : constant Source_Ptr := Sloc (I_Node);
1398 F_Id : constant Entity_Id := Defining_Entity (F);
1399 Decl : Node_Id;
1400 Default : Node_Id;
1401 Id : Entity_Id;
1402
1403 begin
1404 -- Append copy of formal declaration to associations, and create new
1405 -- defining identifier for it.
1406
1407 Decl := New_Copy_Tree (F);
1408 Id := Make_Defining_Identifier (Sloc (F_Id), Chars (F_Id));
1409
1410 if Nkind (F) in N_Formal_Subprogram_Declaration then
1411 Set_Defining_Unit_Name (Specification (Decl), Id);
1412
1413 else
1414 Set_Defining_Identifier (Decl, Id);
1415 end if;
1416
1417 Append (Decl, Assoc);
1418
1419 if No (Found_Assoc) then
1420 Default :=
1421 Make_Generic_Association (Loc,
1422 Selector_Name =>
1423 New_Occurrence_Of (Id, Loc),
1424 Explicit_Generic_Actual_Parameter => Empty);
1425 Set_Box_Present (Default);
1426 Append (Default, Default_Formals);
1427 end if;
1428 end Process_Default;
1429
1430 ---------------------------------
1431 -- Renames_Standard_Subprogram --
1432 ---------------------------------
1433
1434 function Renames_Standard_Subprogram (Subp : Entity_Id) return Boolean is
1435 Id : Entity_Id;
1436
1437 begin
1438 Id := Alias (Subp);
1439 while Present (Id) loop
1440 if Scope (Id) = Standard_Standard then
1441 return True;
1442 end if;
1443
1444 Id := Alias (Id);
1445 end loop;
1446
1447 return False;
1448 end Renames_Standard_Subprogram;
1449
1450 -------------------------
1451 -- Set_Analyzed_Formal --
1452 -------------------------
1453
1454 procedure Set_Analyzed_Formal is
1455 Kind : Node_Kind;
1456
1457 begin
1458 while Present (Analyzed_Formal) loop
1459 Kind := Nkind (Analyzed_Formal);
1460
1461 case Nkind (Formal) is
1462 when N_Formal_Subprogram_Declaration =>
1463 exit when Kind in N_Formal_Subprogram_Declaration
1464 and then
1465 Chars
1466 (Defining_Unit_Name (Specification (Formal))) =
1467 Chars
1468 (Defining_Unit_Name (Specification (Analyzed_Formal)));
1469
1470 when N_Formal_Package_Declaration =>
1471 exit when Nkind_In (Kind, N_Formal_Package_Declaration,
1472 N_Generic_Package_Declaration,
1473 N_Package_Declaration);
1474
1475 when N_Use_Package_Clause
1476 | N_Use_Type_Clause
1477 =>
1478 exit;
1479
1480 when others =>
1481
1482 -- Skip freeze nodes, and nodes inserted to replace
1483 -- unrecognized pragmas.
1484
1485 exit when
1486 Kind not in N_Formal_Subprogram_Declaration
1487 and then not Nkind_In (Kind, N_Subprogram_Declaration,
1488 N_Freeze_Entity,
1489 N_Null_Statement,
1490 N_Itype_Reference)
1491 and then Chars (Defining_Identifier (Formal)) =
1492 Chars (Defining_Identifier (Analyzed_Formal));
1493 end case;
1494
1495 Next (Analyzed_Formal);
1496 end loop;
1497 end Set_Analyzed_Formal;
1498
1499 -- Start of processing for Analyze_Associations
1500
1501 begin
1502 Actuals := Generic_Associations (I_Node);
1503
1504 if Present (Actuals) then
1505
1506 -- Check for an Others choice, indicating a partial parameterization
1507 -- for a formal package.
1508
1509 Actual := First (Actuals);
1510 while Present (Actual) loop
1511 if Nkind (Actual) = N_Others_Choice then
1512 Others_Present := True;
1513 Others_Choice := Actual;
1514
1515 if Present (Next (Actual)) then
1516 Error_Msg_N ("others must be last association", Actual);
1517 end if;
1518
1519 -- This subprogram is used both for formal packages and for
1520 -- instantiations. For the latter, associations must all be
1521 -- explicit.
1522
1523 if Nkind (I_Node) /= N_Formal_Package_Declaration
1524 and then Comes_From_Source (I_Node)
1525 then
1526 Error_Msg_N
1527 ("others association not allowed in an instance",
1528 Actual);
1529 end if;
1530
1531 -- In any case, nothing to do after the others association
1532
1533 exit;
1534
1535 elsif Box_Present (Actual)
1536 and then Comes_From_Source (I_Node)
1537 and then Nkind (I_Node) /= N_Formal_Package_Declaration
1538 then
1539 Error_Msg_N
1540 ("box association not allowed in an instance", Actual);
1541 end if;
1542
1543 Next (Actual);
1544 end loop;
1545
1546 -- If named associations are present, save first named association
1547 -- (it may of course be Empty) to facilitate subsequent name search.
1548
1549 First_Named := First (Actuals);
1550 while Present (First_Named)
1551 and then Nkind (First_Named) /= N_Others_Choice
1552 and then No (Selector_Name (First_Named))
1553 loop
1554 Num_Actuals := Num_Actuals + 1;
1555 Next (First_Named);
1556 end loop;
1557 end if;
1558
1559 Named := First_Named;
1560 while Present (Named) loop
1561 if Nkind (Named) /= N_Others_Choice
1562 and then No (Selector_Name (Named))
1563 then
1564 Error_Msg_N ("invalid positional actual after named one", Named);
1565 Abandon_Instantiation (Named);
1566 end if;
1567
1568 -- A named association may lack an actual parameter, if it was
1569 -- introduced for a default subprogram that turns out to be local
1570 -- to the outer instantiation. If it has a box association it must
1571 -- correspond to some formal in the generic.
1572
1573 if Nkind (Named) /= N_Others_Choice
1574 and then (Present (Explicit_Generic_Actual_Parameter (Named))
1575 or else Box_Present (Named))
1576 then
1577 Num_Actuals := Num_Actuals + 1;
1578 end if;
1579
1580 Next (Named);
1581 end loop;
1582
1583 if Present (Formals) then
1584 Formal := First_Non_Pragma (Formals);
1585 Analyzed_Formal := First_Non_Pragma (F_Copy);
1586
1587 if Present (Actuals) then
1588 Actual := First (Actuals);
1589
1590 -- All formals should have default values
1591
1592 else
1593 Actual := Empty;
1594 end if;
1595
1596 while Present (Formal) loop
1597 Set_Analyzed_Formal;
1598 Saved_Formal := Next_Non_Pragma (Formal);
1599
1600 case Nkind (Formal) is
1601 when N_Formal_Object_Declaration =>
1602 Match :=
1603 Matching_Actual
1604 (Defining_Identifier (Formal),
1605 Defining_Identifier (Analyzed_Formal));
1606
1607 if No (Match) and then Partial_Parameterization then
1608 Process_Default (Formal);
1609
1610 else
1611 Append_List
1612 (Instantiate_Object (Formal, Match, Analyzed_Formal),
1613 Assoc);
1614
1615 -- For a defaulted in_parameter, create an entry in the
1616 -- the list of defaulted actuals, for GNATProve use. Do
1617 -- not included these defaults for an instance nested
1618 -- within a generic, because the defaults are also used
1619 -- in the analysis of the enclosing generic, and only
1620 -- defaulted subprograms are relevant there.
1621
1622 if No (Match) and then not Inside_A_Generic then
1623 Append_To (Default_Actuals,
1624 Make_Generic_Association (Sloc (I_Node),
1625 Selector_Name =>
1626 New_Occurrence_Of
1627 (Defining_Identifier (Formal), Sloc (I_Node)),
1628 Explicit_Generic_Actual_Parameter =>
1629 New_Copy_Tree (Default_Expression (Formal))));
1630 end if;
1631 end if;
1632
1633 -- If the object is a call to an expression function, this
1634 -- is a freezing point for it.
1635
1636 if Is_Entity_Name (Match)
1637 and then Present (Entity (Match))
1638 and then Nkind
1639 (Original_Node (Unit_Declaration_Node (Entity (Match))))
1640 = N_Expression_Function
1641 then
1642 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1643 end if;
1644
1645 when N_Formal_Type_Declaration =>
1646 Match :=
1647 Matching_Actual
1648 (Defining_Identifier (Formal),
1649 Defining_Identifier (Analyzed_Formal));
1650
1651 if No (Match) then
1652 if Partial_Parameterization then
1653 Process_Default (Formal);
1654
1655 else
1656 Error_Msg_Sloc := Sloc (Gen_Unit);
1657 Error_Msg_NE
1658 ("missing actual&",
1659 Instantiation_Node, Defining_Identifier (Formal));
1660 Error_Msg_NE
1661 ("\in instantiation of & declared#",
1662 Instantiation_Node, Gen_Unit);
1663 Abandon_Instantiation (Instantiation_Node);
1664 end if;
1665
1666 else
1667 Analyze (Match);
1668 Append_List
1669 (Instantiate_Type
1670 (Formal, Match, Analyzed_Formal, Assoc),
1671 Assoc);
1672
1673 if Is_Fixed_Point_Type (Entity (Match)) then
1674 Check_Fixed_Point_Actual (Match);
1675 end if;
1676
1677 -- An instantiation is a freeze point for the actuals,
1678 -- unless this is a rewritten formal package, or the
1679 -- formal is an Ada 2012 formal incomplete type.
1680
1681 if Nkind (I_Node) = N_Formal_Package_Declaration
1682 or else
1683 (Ada_Version >= Ada_2012
1684 and then
1685 Ekind (Defining_Identifier (Analyzed_Formal)) =
1686 E_Incomplete_Type)
1687 then
1688 null;
1689
1690 else
1691 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1692 end if;
1693 end if;
1694
1695 -- A remote access-to-class-wide type is not a legal actual
1696 -- for a generic formal of an access type (E.2.2(17/2)).
1697 -- In GNAT an exception to this rule is introduced when
1698 -- the formal is marked as remote using implementation
1699 -- defined aspect/pragma Remote_Access_Type. In that case
1700 -- the actual must be remote as well.
1701
1702 -- If the current instantiation is the construction of a
1703 -- local copy for a formal package the actuals may be
1704 -- defaulted, and there is no matching actual to check.
1705
1706 if Nkind (Analyzed_Formal) = N_Formal_Type_Declaration
1707 and then
1708 Nkind (Formal_Type_Definition (Analyzed_Formal)) =
1709 N_Access_To_Object_Definition
1710 and then Present (Match)
1711 then
1712 declare
1713 Formal_Ent : constant Entity_Id :=
1714 Defining_Identifier (Analyzed_Formal);
1715 begin
1716 if Is_Remote_Access_To_Class_Wide_Type (Entity (Match))
1717 = Is_Remote_Types (Formal_Ent)
1718 then
1719 -- Remoteness of formal and actual match
1720
1721 null;
1722
1723 elsif Is_Remote_Types (Formal_Ent) then
1724
1725 -- Remote formal, non-remote actual
1726
1727 Error_Msg_NE
1728 ("actual for& must be remote", Match, Formal_Ent);
1729
1730 else
1731 -- Non-remote formal, remote actual
1732
1733 Error_Msg_NE
1734 ("actual for& may not be remote",
1735 Match, Formal_Ent);
1736 end if;
1737 end;
1738 end if;
1739
1740 when N_Formal_Subprogram_Declaration =>
1741 Match :=
1742 Matching_Actual
1743 (Defining_Unit_Name (Specification (Formal)),
1744 Defining_Unit_Name (Specification (Analyzed_Formal)));
1745
1746 -- If the formal subprogram has the same name as another
1747 -- formal subprogram of the generic, then a named
1748 -- association is illegal (12.3(9)). Exclude named
1749 -- associations that are generated for a nested instance.
1750
1751 if Present (Match)
1752 and then Is_Named_Assoc
1753 and then Comes_From_Source (Found_Assoc)
1754 then
1755 Check_Overloaded_Formal_Subprogram (Formal);
1756 end if;
1757
1758 -- If there is no corresponding actual, this may be case
1759 -- of partial parameterization, or else the formal has a
1760 -- default or a box.
1761
1762 if No (Match) and then Partial_Parameterization then
1763 Process_Default (Formal);
1764
1765 if Nkind (I_Node) = N_Formal_Package_Declaration then
1766 Check_Overloaded_Formal_Subprogram (Formal);
1767 end if;
1768
1769 else
1770 Append_To (Assoc,
1771 Instantiate_Formal_Subprogram
1772 (Formal, Match, Analyzed_Formal));
1773
1774 -- An instantiation is a freeze point for the actuals,
1775 -- unless this is a rewritten formal package.
1776
1777 if Nkind (I_Node) /= N_Formal_Package_Declaration
1778 and then Nkind (Match) = N_Identifier
1779 and then Is_Subprogram (Entity (Match))
1780
1781 -- The actual subprogram may rename a routine defined
1782 -- in Standard. Avoid freezing such renamings because
1783 -- subprograms coming from Standard cannot be frozen.
1784
1785 and then
1786 not Renames_Standard_Subprogram (Entity (Match))
1787
1788 -- If the actual subprogram comes from a different
1789 -- unit, it is already frozen, either by a body in
1790 -- that unit or by the end of the declarative part
1791 -- of the unit. This check avoids the freezing of
1792 -- subprograms defined in Standard which are used
1793 -- as generic actuals.
1794
1795 and then In_Same_Code_Unit (Entity (Match), I_Node)
1796 and then Has_Fully_Defined_Profile (Entity (Match))
1797 then
1798 -- Mark the subprogram as having a delayed freeze
1799 -- since this may be an out-of-order action.
1800
1801 Set_Has_Delayed_Freeze (Entity (Match));
1802 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1803 end if;
1804 end if;
1805
1806 -- If this is a nested generic, preserve default for later
1807 -- instantiations. We do this as well for GNATProve use,
1808 -- so that the list of generic associations is complete.
1809
1810 if No (Match) and then Box_Present (Formal) then
1811 declare
1812 Subp : constant Entity_Id :=
1813 Defining_Unit_Name (Specification (Last (Assoc)));
1814
1815 begin
1816 Append_To (Default_Actuals,
1817 Make_Generic_Association (Sloc (I_Node),
1818 Selector_Name =>
1819 New_Occurrence_Of (Subp, Sloc (I_Node)),
1820 Explicit_Generic_Actual_Parameter =>
1821 New_Occurrence_Of (Subp, Sloc (I_Node))));
1822 end;
1823 end if;
1824
1825 when N_Formal_Package_Declaration =>
1826 Match :=
1827 Matching_Actual
1828 (Defining_Identifier (Formal),
1829 Defining_Identifier (Original_Node (Analyzed_Formal)));
1830
1831 if No (Match) then
1832 if Partial_Parameterization then
1833 Process_Default (Formal);
1834
1835 else
1836 Error_Msg_Sloc := Sloc (Gen_Unit);
1837 Error_Msg_NE
1838 ("missing actual&",
1839 Instantiation_Node, Defining_Identifier (Formal));
1840 Error_Msg_NE
1841 ("\in instantiation of & declared#",
1842 Instantiation_Node, Gen_Unit);
1843
1844 Abandon_Instantiation (Instantiation_Node);
1845 end if;
1846
1847 else
1848 Analyze (Match);
1849 Append_List
1850 (Instantiate_Formal_Package
1851 (Formal, Match, Analyzed_Formal),
1852 Assoc);
1853 end if;
1854
1855 -- For use type and use package appearing in the generic part,
1856 -- we have already copied them, so we can just move them where
1857 -- they belong (we mustn't recopy them since this would mess up
1858 -- the Sloc values).
1859
1860 when N_Use_Package_Clause
1861 | N_Use_Type_Clause
1862 =>
1863 if Nkind (Original_Node (I_Node)) =
1864 N_Formal_Package_Declaration
1865 then
1866 Append (New_Copy_Tree (Formal), Assoc);
1867 else
1868 Remove (Formal);
1869 Append (Formal, Assoc);
1870 end if;
1871
1872 when others =>
1873 raise Program_Error;
1874 end case;
1875
1876 Formal := Saved_Formal;
1877 Next_Non_Pragma (Analyzed_Formal);
1878 end loop;
1879
1880 if Num_Actuals > Num_Matched then
1881 Error_Msg_Sloc := Sloc (Gen_Unit);
1882
1883 if Present (Selector_Name (Actual)) then
1884 Error_Msg_NE
1885 ("unmatched actual &", Actual, Selector_Name (Actual));
1886 Error_Msg_NE
1887 ("\in instantiation of & declared#", Actual, Gen_Unit);
1888 else
1889 Error_Msg_NE
1890 ("unmatched actual in instantiation of & declared#",
1891 Actual, Gen_Unit);
1892 end if;
1893 end if;
1894
1895 elsif Present (Actuals) then
1896 Error_Msg_N
1897 ("too many actuals in generic instantiation", Instantiation_Node);
1898 end if;
1899
1900 -- An instantiation freezes all generic actuals. The only exceptions
1901 -- to this are incomplete types and subprograms which are not fully
1902 -- defined at the point of instantiation.
1903
1904 declare
1905 Elmt : Elmt_Id := First_Elmt (Actuals_To_Freeze);
1906 begin
1907 while Present (Elmt) loop
1908 Freeze_Before (I_Node, Node (Elmt));
1909 Next_Elmt (Elmt);
1910 end loop;
1911 end;
1912
1913 -- If there are default subprograms, normalize the tree by adding
1914 -- explicit associations for them. This is required if the instance
1915 -- appears within a generic.
1916
1917 if not Is_Empty_List (Default_Actuals) then
1918 declare
1919 Default : Node_Id;
1920
1921 begin
1922 Default := First (Default_Actuals);
1923 while Present (Default) loop
1924 Mark_Rewrite_Insertion (Default);
1925 Next (Default);
1926 end loop;
1927
1928 if No (Actuals) then
1929 Set_Generic_Associations (I_Node, Default_Actuals);
1930 else
1931 Append_List_To (Actuals, Default_Actuals);
1932 end if;
1933 end;
1934 end if;
1935
1936 -- If this is a formal package, normalize the parameter list by adding
1937 -- explicit box associations for the formals that are covered by an
1938 -- Others_Choice.
1939
1940 if not Is_Empty_List (Default_Formals) then
1941 Append_List (Default_Formals, Formals);
1942 end if;
1943
1944 return Assoc;
1945 end Analyze_Associations;
1946
1947 -------------------------------
1948 -- Analyze_Formal_Array_Type --
1949 -------------------------------
1950
1951 procedure Analyze_Formal_Array_Type
1952 (T : in out Entity_Id;
1953 Def : Node_Id)
1954 is
1955 DSS : Node_Id;
1956
1957 begin
1958 -- Treated like a non-generic array declaration, with additional
1959 -- semantic checks.
1960
1961 Enter_Name (T);
1962
1963 if Nkind (Def) = N_Constrained_Array_Definition then
1964 DSS := First (Discrete_Subtype_Definitions (Def));
1965 while Present (DSS) loop
1966 if Nkind_In (DSS, N_Subtype_Indication,
1967 N_Range,
1968 N_Attribute_Reference)
1969 then
1970 Error_Msg_N ("only a subtype mark is allowed in a formal", DSS);
1971 end if;
1972
1973 Next (DSS);
1974 end loop;
1975 end if;
1976
1977 Array_Type_Declaration (T, Def);
1978 Set_Is_Generic_Type (Base_Type (T));
1979
1980 if Ekind (Component_Type (T)) = E_Incomplete_Type
1981 and then No (Full_View (Component_Type (T)))
1982 then
1983 Error_Msg_N ("premature usage of incomplete type", Def);
1984
1985 -- Check that range constraint is not allowed on the component type
1986 -- of a generic formal array type (AARM 12.5.3(3))
1987
1988 elsif Is_Internal (Component_Type (T))
1989 and then Present (Subtype_Indication (Component_Definition (Def)))
1990 and then Nkind (Original_Node
1991 (Subtype_Indication (Component_Definition (Def)))) =
1992 N_Subtype_Indication
1993 then
1994 Error_Msg_N
1995 ("in a formal, a subtype indication can only be "
1996 & "a subtype mark (RM 12.5.3(3))",
1997 Subtype_Indication (Component_Definition (Def)));
1998 end if;
1999
2000 end Analyze_Formal_Array_Type;
2001
2002 ---------------------------------------------
2003 -- Analyze_Formal_Decimal_Fixed_Point_Type --
2004 ---------------------------------------------
2005
2006 -- As for other generic types, we create a valid type representation with
2007 -- legal but arbitrary attributes, whose values are never considered
2008 -- static. For all scalar types we introduce an anonymous base type, with
2009 -- the same attributes. We choose the corresponding integer type to be
2010 -- Standard_Integer.
2011 -- Here and in other similar routines, the Sloc of the generated internal
2012 -- type must be the same as the sloc of the defining identifier of the
2013 -- formal type declaration, to provide proper source navigation.
2014
2015 procedure Analyze_Formal_Decimal_Fixed_Point_Type
2016 (T : Entity_Id;
2017 Def : Node_Id)
2018 is
2019 Loc : constant Source_Ptr := Sloc (Def);
2020
2021 Base : constant Entity_Id :=
2022 New_Internal_Entity
2023 (E_Decimal_Fixed_Point_Type,
2024 Current_Scope,
2025 Sloc (Defining_Identifier (Parent (Def))), 'G');
2026
2027 Int_Base : constant Entity_Id := Standard_Integer;
2028 Delta_Val : constant Ureal := Ureal_1;
2029 Digs_Val : constant Uint := Uint_6;
2030
2031 function Make_Dummy_Bound return Node_Id;
2032 -- Return a properly typed universal real literal to use as a bound
2033
2034 ----------------------
2035 -- Make_Dummy_Bound --
2036 ----------------------
2037
2038 function Make_Dummy_Bound return Node_Id is
2039 Bound : constant Node_Id := Make_Real_Literal (Loc, Ureal_1);
2040 begin
2041 Set_Etype (Bound, Universal_Real);
2042 return Bound;
2043 end Make_Dummy_Bound;
2044
2045 -- Start of processing for Analyze_Formal_Decimal_Fixed_Point_Type
2046
2047 begin
2048 Enter_Name (T);
2049
2050 Set_Etype (Base, Base);
2051 Set_Size_Info (Base, Int_Base);
2052 Set_RM_Size (Base, RM_Size (Int_Base));
2053 Set_First_Rep_Item (Base, First_Rep_Item (Int_Base));
2054 Set_Digits_Value (Base, Digs_Val);
2055 Set_Delta_Value (Base, Delta_Val);
2056 Set_Small_Value (Base, Delta_Val);
2057 Set_Scalar_Range (Base,
2058 Make_Range (Loc,
2059 Low_Bound => Make_Dummy_Bound,
2060 High_Bound => Make_Dummy_Bound));
2061
2062 Set_Is_Generic_Type (Base);
2063 Set_Parent (Base, Parent (Def));
2064
2065 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
2066 Set_Etype (T, Base);
2067 Set_Size_Info (T, Int_Base);
2068 Set_RM_Size (T, RM_Size (Int_Base));
2069 Set_First_Rep_Item (T, First_Rep_Item (Int_Base));
2070 Set_Digits_Value (T, Digs_Val);
2071 Set_Delta_Value (T, Delta_Val);
2072 Set_Small_Value (T, Delta_Val);
2073 Set_Scalar_Range (T, Scalar_Range (Base));
2074 Set_Is_Constrained (T);
2075
2076 Check_Restriction (No_Fixed_Point, Def);
2077 end Analyze_Formal_Decimal_Fixed_Point_Type;
2078
2079 -------------------------------------------
2080 -- Analyze_Formal_Derived_Interface_Type --
2081 -------------------------------------------
2082
2083 procedure Analyze_Formal_Derived_Interface_Type
2084 (N : Node_Id;
2085 T : Entity_Id;
2086 Def : Node_Id)
2087 is
2088 Loc : constant Source_Ptr := Sloc (Def);
2089
2090 begin
2091 -- Rewrite as a type declaration of a derived type. This ensures that
2092 -- the interface list and primitive operations are properly captured.
2093
2094 Rewrite (N,
2095 Make_Full_Type_Declaration (Loc,
2096 Defining_Identifier => T,
2097 Type_Definition => Def));
2098 Analyze (N);
2099 Set_Is_Generic_Type (T);
2100 end Analyze_Formal_Derived_Interface_Type;
2101
2102 ---------------------------------
2103 -- Analyze_Formal_Derived_Type --
2104 ---------------------------------
2105
2106 procedure Analyze_Formal_Derived_Type
2107 (N : Node_Id;
2108 T : Entity_Id;
2109 Def : Node_Id)
2110 is
2111 Loc : constant Source_Ptr := Sloc (Def);
2112 Unk_Disc : constant Boolean := Unknown_Discriminants_Present (N);
2113 New_N : Node_Id;
2114
2115 begin
2116 Set_Is_Generic_Type (T);
2117
2118 if Private_Present (Def) then
2119 New_N :=
2120 Make_Private_Extension_Declaration (Loc,
2121 Defining_Identifier => T,
2122 Discriminant_Specifications => Discriminant_Specifications (N),
2123 Unknown_Discriminants_Present => Unk_Disc,
2124 Subtype_Indication => Subtype_Mark (Def),
2125 Interface_List => Interface_List (Def));
2126
2127 Set_Abstract_Present (New_N, Abstract_Present (Def));
2128 Set_Limited_Present (New_N, Limited_Present (Def));
2129 Set_Synchronized_Present (New_N, Synchronized_Present (Def));
2130
2131 else
2132 New_N :=
2133 Make_Full_Type_Declaration (Loc,
2134 Defining_Identifier => T,
2135 Discriminant_Specifications =>
2136 Discriminant_Specifications (Parent (T)),
2137 Type_Definition =>
2138 Make_Derived_Type_Definition (Loc,
2139 Subtype_Indication => Subtype_Mark (Def)));
2140
2141 Set_Abstract_Present
2142 (Type_Definition (New_N), Abstract_Present (Def));
2143 Set_Limited_Present
2144 (Type_Definition (New_N), Limited_Present (Def));
2145 end if;
2146
2147 Rewrite (N, New_N);
2148 Analyze (N);
2149
2150 if Unk_Disc then
2151 if not Is_Composite_Type (T) then
2152 Error_Msg_N
2153 ("unknown discriminants not allowed for elementary types", N);
2154 else
2155 Set_Has_Unknown_Discriminants (T);
2156 Set_Is_Constrained (T, False);
2157 end if;
2158 end if;
2159
2160 -- If the parent type has a known size, so does the formal, which makes
2161 -- legal representation clauses that involve the formal.
2162
2163 Set_Size_Known_At_Compile_Time
2164 (T, Size_Known_At_Compile_Time (Entity (Subtype_Mark (Def))));
2165 end Analyze_Formal_Derived_Type;
2166
2167 ----------------------------------
2168 -- Analyze_Formal_Discrete_Type --
2169 ----------------------------------
2170
2171 -- The operations defined for a discrete types are those of an enumeration
2172 -- type. The size is set to an arbitrary value, for use in analyzing the
2173 -- generic unit.
2174
2175 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id) is
2176 Loc : constant Source_Ptr := Sloc (Def);
2177 Lo : Node_Id;
2178 Hi : Node_Id;
2179
2180 Base : constant Entity_Id :=
2181 New_Internal_Entity
2182 (E_Floating_Point_Type, Current_Scope,
2183 Sloc (Defining_Identifier (Parent (Def))), 'G');
2184
2185 begin
2186 Enter_Name (T);
2187 Set_Ekind (T, E_Enumeration_Subtype);
2188 Set_Etype (T, Base);
2189 Init_Size (T, 8);
2190 Init_Alignment (T);
2191 Set_Is_Generic_Type (T);
2192 Set_Is_Constrained (T);
2193
2194 -- For semantic analysis, the bounds of the type must be set to some
2195 -- non-static value. The simplest is to create attribute nodes for those
2196 -- bounds, that refer to the type itself. These bounds are never
2197 -- analyzed but serve as place-holders.
2198
2199 Lo :=
2200 Make_Attribute_Reference (Loc,
2201 Attribute_Name => Name_First,
2202 Prefix => New_Occurrence_Of (T, Loc));
2203 Set_Etype (Lo, T);
2204
2205 Hi :=
2206 Make_Attribute_Reference (Loc,
2207 Attribute_Name => Name_Last,
2208 Prefix => New_Occurrence_Of (T, Loc));
2209 Set_Etype (Hi, T);
2210
2211 Set_Scalar_Range (T,
2212 Make_Range (Loc,
2213 Low_Bound => Lo,
2214 High_Bound => Hi));
2215
2216 Set_Ekind (Base, E_Enumeration_Type);
2217 Set_Etype (Base, Base);
2218 Init_Size (Base, 8);
2219 Init_Alignment (Base);
2220 Set_Is_Generic_Type (Base);
2221 Set_Scalar_Range (Base, Scalar_Range (T));
2222 Set_Parent (Base, Parent (Def));
2223 end Analyze_Formal_Discrete_Type;
2224
2225 ----------------------------------
2226 -- Analyze_Formal_Floating_Type --
2227 ---------------------------------
2228
2229 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id) is
2230 Base : constant Entity_Id :=
2231 New_Internal_Entity
2232 (E_Floating_Point_Type, Current_Scope,
2233 Sloc (Defining_Identifier (Parent (Def))), 'G');
2234
2235 begin
2236 -- The various semantic attributes are taken from the predefined type
2237 -- Float, just so that all of them are initialized. Their values are
2238 -- never used because no constant folding or expansion takes place in
2239 -- the generic itself.
2240
2241 Enter_Name (T);
2242 Set_Ekind (T, E_Floating_Point_Subtype);
2243 Set_Etype (T, Base);
2244 Set_Size_Info (T, (Standard_Float));
2245 Set_RM_Size (T, RM_Size (Standard_Float));
2246 Set_Digits_Value (T, Digits_Value (Standard_Float));
2247 Set_Scalar_Range (T, Scalar_Range (Standard_Float));
2248 Set_Is_Constrained (T);
2249
2250 Set_Is_Generic_Type (Base);
2251 Set_Etype (Base, Base);
2252 Set_Size_Info (Base, (Standard_Float));
2253 Set_RM_Size (Base, RM_Size (Standard_Float));
2254 Set_Digits_Value (Base, Digits_Value (Standard_Float));
2255 Set_Scalar_Range (Base, Scalar_Range (Standard_Float));
2256 Set_Parent (Base, Parent (Def));
2257
2258 Check_Restriction (No_Floating_Point, Def);
2259 end Analyze_Formal_Floating_Type;
2260
2261 -----------------------------------
2262 -- Analyze_Formal_Interface_Type;--
2263 -----------------------------------
2264
2265 procedure Analyze_Formal_Interface_Type
2266 (N : Node_Id;
2267 T : Entity_Id;
2268 Def : Node_Id)
2269 is
2270 Loc : constant Source_Ptr := Sloc (N);
2271 New_N : Node_Id;
2272
2273 begin
2274 New_N :=
2275 Make_Full_Type_Declaration (Loc,
2276 Defining_Identifier => T,
2277 Type_Definition => Def);
2278
2279 Rewrite (N, New_N);
2280 Analyze (N);
2281 Set_Is_Generic_Type (T);
2282 end Analyze_Formal_Interface_Type;
2283
2284 ---------------------------------
2285 -- Analyze_Formal_Modular_Type --
2286 ---------------------------------
2287
2288 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id) is
2289 begin
2290 -- Apart from their entity kind, generic modular types are treated like
2291 -- signed integer types, and have the same attributes.
2292
2293 Analyze_Formal_Signed_Integer_Type (T, Def);
2294 Set_Ekind (T, E_Modular_Integer_Subtype);
2295 Set_Ekind (Etype (T), E_Modular_Integer_Type);
2296
2297 end Analyze_Formal_Modular_Type;
2298
2299 ---------------------------------------
2300 -- Analyze_Formal_Object_Declaration --
2301 ---------------------------------------
2302
2303 procedure Analyze_Formal_Object_Declaration (N : Node_Id) is
2304 E : constant Node_Id := Default_Expression (N);
2305 Id : constant Node_Id := Defining_Identifier (N);
2306 K : Entity_Kind;
2307 T : Node_Id;
2308
2309 begin
2310 Enter_Name (Id);
2311
2312 -- Determine the mode of the formal object
2313
2314 if Out_Present (N) then
2315 K := E_Generic_In_Out_Parameter;
2316
2317 if not In_Present (N) then
2318 Error_Msg_N ("formal generic objects cannot have mode OUT", N);
2319 end if;
2320
2321 else
2322 K := E_Generic_In_Parameter;
2323 end if;
2324
2325 if Present (Subtype_Mark (N)) then
2326 Find_Type (Subtype_Mark (N));
2327 T := Entity (Subtype_Mark (N));
2328
2329 -- Verify that there is no redundant null exclusion
2330
2331 if Null_Exclusion_Present (N) then
2332 if not Is_Access_Type (T) then
2333 Error_Msg_N
2334 ("null exclusion can only apply to an access type", N);
2335
2336 elsif Can_Never_Be_Null (T) then
2337 Error_Msg_NE
2338 ("`NOT NULL` not allowed (& already excludes null)", N, T);
2339 end if;
2340 end if;
2341
2342 -- Ada 2005 (AI-423): Formal object with an access definition
2343
2344 else
2345 Check_Access_Definition (N);
2346 T := Access_Definition
2347 (Related_Nod => N,
2348 N => Access_Definition (N));
2349 end if;
2350
2351 if Ekind (T) = E_Incomplete_Type then
2352 declare
2353 Error_Node : Node_Id;
2354
2355 begin
2356 if Present (Subtype_Mark (N)) then
2357 Error_Node := Subtype_Mark (N);
2358 else
2359 Check_Access_Definition (N);
2360 Error_Node := Access_Definition (N);
2361 end if;
2362
2363 Error_Msg_N ("premature usage of incomplete type", Error_Node);
2364 end;
2365 end if;
2366
2367 if K = E_Generic_In_Parameter then
2368
2369 -- Ada 2005 (AI-287): Limited aggregates allowed in generic formals
2370
2371 if Ada_Version < Ada_2005 and then Is_Limited_Type (T) then
2372 Error_Msg_N
2373 ("generic formal of mode IN must not be of limited type", N);
2374 Explain_Limited_Type (T, N);
2375 end if;
2376
2377 if Is_Abstract_Type (T) then
2378 Error_Msg_N
2379 ("generic formal of mode IN must not be of abstract type", N);
2380 end if;
2381
2382 if Present (E) then
2383 Preanalyze_Spec_Expression (E, T);
2384
2385 if Is_Limited_Type (T) and then not OK_For_Limited_Init (T, E) then
2386 Error_Msg_N
2387 ("initialization not allowed for limited types", E);
2388 Explain_Limited_Type (T, E);
2389 end if;
2390 end if;
2391
2392 Set_Ekind (Id, K);
2393 Set_Etype (Id, T);
2394
2395 -- Case of generic IN OUT parameter
2396
2397 else
2398 -- If the formal has an unconstrained type, construct its actual
2399 -- subtype, as is done for subprogram formals. In this fashion, all
2400 -- its uses can refer to specific bounds.
2401
2402 Set_Ekind (Id, K);
2403 Set_Etype (Id, T);
2404
2405 if (Is_Array_Type (T) and then not Is_Constrained (T))
2406 or else (Ekind (T) = E_Record_Type and then Has_Discriminants (T))
2407 then
2408 declare
2409 Non_Freezing_Ref : constant Node_Id :=
2410 New_Occurrence_Of (Id, Sloc (Id));
2411 Decl : Node_Id;
2412
2413 begin
2414 -- Make sure the actual subtype doesn't generate bogus freezing
2415
2416 Set_Must_Not_Freeze (Non_Freezing_Ref);
2417 Decl := Build_Actual_Subtype (T, Non_Freezing_Ref);
2418 Insert_Before_And_Analyze (N, Decl);
2419 Set_Actual_Subtype (Id, Defining_Identifier (Decl));
2420 end;
2421 else
2422 Set_Actual_Subtype (Id, T);
2423 end if;
2424
2425 if Present (E) then
2426 Error_Msg_N
2427 ("initialization not allowed for `IN OUT` formals", N);
2428 end if;
2429 end if;
2430
2431 if Has_Aspects (N) then
2432 Analyze_Aspect_Specifications (N, Id);
2433 end if;
2434 end Analyze_Formal_Object_Declaration;
2435
2436 ----------------------------------------------
2437 -- Analyze_Formal_Ordinary_Fixed_Point_Type --
2438 ----------------------------------------------
2439
2440 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
2441 (T : Entity_Id;
2442 Def : Node_Id)
2443 is
2444 Loc : constant Source_Ptr := Sloc (Def);
2445 Base : constant Entity_Id :=
2446 New_Internal_Entity
2447 (E_Ordinary_Fixed_Point_Type, Current_Scope,
2448 Sloc (Defining_Identifier (Parent (Def))), 'G');
2449
2450 begin
2451 -- The semantic attributes are set for completeness only, their values
2452 -- will never be used, since all properties of the type are non-static.
2453
2454 Enter_Name (T);
2455 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
2456 Set_Etype (T, Base);
2457 Set_Size_Info (T, Standard_Integer);
2458 Set_RM_Size (T, RM_Size (Standard_Integer));
2459 Set_Small_Value (T, Ureal_1);
2460 Set_Delta_Value (T, Ureal_1);
2461 Set_Scalar_Range (T,
2462 Make_Range (Loc,
2463 Low_Bound => Make_Real_Literal (Loc, Ureal_1),
2464 High_Bound => Make_Real_Literal (Loc, Ureal_1)));
2465 Set_Is_Constrained (T);
2466
2467 Set_Is_Generic_Type (Base);
2468 Set_Etype (Base, Base);
2469 Set_Size_Info (Base, Standard_Integer);
2470 Set_RM_Size (Base, RM_Size (Standard_Integer));
2471 Set_Small_Value (Base, Ureal_1);
2472 Set_Delta_Value (Base, Ureal_1);
2473 Set_Scalar_Range (Base, Scalar_Range (T));
2474 Set_Parent (Base, Parent (Def));
2475
2476 Check_Restriction (No_Fixed_Point, Def);
2477 end Analyze_Formal_Ordinary_Fixed_Point_Type;
2478
2479 ----------------------------------------
2480 -- Analyze_Formal_Package_Declaration --
2481 ----------------------------------------
2482
2483 procedure Analyze_Formal_Package_Declaration (N : Node_Id) is
2484 Gen_Id : constant Node_Id := Name (N);
2485 Loc : constant Source_Ptr := Sloc (N);
2486 Pack_Id : constant Entity_Id := Defining_Identifier (N);
2487 Formal : Entity_Id;
2488 Gen_Decl : Node_Id;
2489 Gen_Unit : Entity_Id;
2490 Renaming : Node_Id;
2491
2492 Vis_Prims_List : Elist_Id := No_Elist;
2493 -- List of primitives made temporarily visible in the instantiation
2494 -- to match the visibility of the formal type.
2495
2496 function Build_Local_Package return Node_Id;
2497 -- The formal package is rewritten so that its parameters are replaced
2498 -- with corresponding declarations. For parameters with bona fide
2499 -- associations these declarations are created by Analyze_Associations
2500 -- as for a regular instantiation. For boxed parameters, we preserve
2501 -- the formal declarations and analyze them, in order to introduce
2502 -- entities of the right kind in the environment of the formal.
2503
2504 -------------------------
2505 -- Build_Local_Package --
2506 -------------------------
2507
2508 function Build_Local_Package return Node_Id is
2509 Decls : List_Id;
2510 Pack_Decl : Node_Id;
2511
2512 begin
2513 -- Within the formal, the name of the generic package is a renaming
2514 -- of the formal (as for a regular instantiation).
2515
2516 Pack_Decl :=
2517 Make_Package_Declaration (Loc,
2518 Specification =>
2519 Copy_Generic_Node
2520 (Specification (Original_Node (Gen_Decl)),
2521 Empty, Instantiating => True));
2522
2523 Renaming :=
2524 Make_Package_Renaming_Declaration (Loc,
2525 Defining_Unit_Name =>
2526 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
2527 Name => New_Occurrence_Of (Formal, Loc));
2528
2529 if Nkind (Gen_Id) = N_Identifier
2530 and then Chars (Gen_Id) = Chars (Pack_Id)
2531 then
2532 Error_Msg_NE
2533 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
2534 end if;
2535
2536 -- If the formal is declared with a box, or with an others choice,
2537 -- create corresponding declarations for all entities in the formal
2538 -- part, so that names with the proper types are available in the
2539 -- specification of the formal package.
2540
2541 -- On the other hand, if there are no associations, then all the
2542 -- formals must have defaults, and this will be checked by the
2543 -- call to Analyze_Associations.
2544
2545 if Box_Present (N)
2546 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2547 then
2548 declare
2549 Formal_Decl : Node_Id;
2550
2551 begin
2552 -- TBA : for a formal package, need to recurse ???
2553
2554 Decls := New_List;
2555 Formal_Decl :=
2556 First
2557 (Generic_Formal_Declarations (Original_Node (Gen_Decl)));
2558 while Present (Formal_Decl) loop
2559 Append_To
2560 (Decls, Copy_Generic_Node (Formal_Decl, Empty, True));
2561 Next (Formal_Decl);
2562 end loop;
2563 end;
2564
2565 -- If generic associations are present, use Analyze_Associations to
2566 -- create the proper renaming declarations.
2567
2568 else
2569 declare
2570 Act_Tree : constant Node_Id :=
2571 Copy_Generic_Node
2572 (Original_Node (Gen_Decl), Empty,
2573 Instantiating => True);
2574
2575 begin
2576 Generic_Renamings.Set_Last (0);
2577 Generic_Renamings_HTable.Reset;
2578 Instantiation_Node := N;
2579
2580 Decls :=
2581 Analyze_Associations
2582 (I_Node => Original_Node (N),
2583 Formals => Generic_Formal_Declarations (Act_Tree),
2584 F_Copy => Generic_Formal_Declarations (Gen_Decl));
2585
2586 Vis_Prims_List := Check_Hidden_Primitives (Decls);
2587 end;
2588 end if;
2589
2590 Append (Renaming, To => Decls);
2591
2592 -- Add generated declarations ahead of local declarations in
2593 -- the package.
2594
2595 if No (Visible_Declarations (Specification (Pack_Decl))) then
2596 Set_Visible_Declarations (Specification (Pack_Decl), Decls);
2597 else
2598 Insert_List_Before
2599 (First (Visible_Declarations (Specification (Pack_Decl))),
2600 Decls);
2601 end if;
2602
2603 return Pack_Decl;
2604 end Build_Local_Package;
2605
2606 -- Local variables
2607
2608 Save_IPSM : constant Boolean := Ignore_Pragma_SPARK_Mode;
2609 -- Save flag Ignore_Pragma_SPARK_Mode for restore on exit
2610
2611 Associations : Boolean := True;
2612 New_N : Node_Id;
2613 Parent_Installed : Boolean := False;
2614 Parent_Instance : Entity_Id;
2615 Renaming_In_Par : Entity_Id;
2616
2617 -- Start of processing for Analyze_Formal_Package_Declaration
2618
2619 begin
2620 Check_Text_IO_Special_Unit (Gen_Id);
2621
2622 Init_Env;
2623 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
2624 Gen_Unit := Entity (Gen_Id);
2625
2626 -- Check for a formal package that is a package renaming
2627
2628 if Present (Renamed_Object (Gen_Unit)) then
2629
2630 -- Indicate that unit is used, before replacing it with renamed
2631 -- entity for use below.
2632
2633 if In_Extended_Main_Source_Unit (N) then
2634 Set_Is_Instantiated (Gen_Unit);
2635 Generate_Reference (Gen_Unit, N);
2636 end if;
2637
2638 Gen_Unit := Renamed_Object (Gen_Unit);
2639 end if;
2640
2641 if Ekind (Gen_Unit) /= E_Generic_Package then
2642 Error_Msg_N ("expect generic package name", Gen_Id);
2643 Restore_Env;
2644 goto Leave;
2645
2646 elsif Gen_Unit = Current_Scope then
2647 Error_Msg_N
2648 ("generic package cannot be used as a formal package of itself",
2649 Gen_Id);
2650 Restore_Env;
2651 goto Leave;
2652
2653 elsif In_Open_Scopes (Gen_Unit) then
2654 if Is_Compilation_Unit (Gen_Unit)
2655 and then Is_Child_Unit (Current_Scope)
2656 then
2657 -- Special-case the error when the formal is a parent, and
2658 -- continue analysis to minimize cascaded errors.
2659
2660 Error_Msg_N
2661 ("generic parent cannot be used as formal package of a child "
2662 & "unit", Gen_Id);
2663
2664 else
2665 Error_Msg_N
2666 ("generic package cannot be used as a formal package within "
2667 & "itself", Gen_Id);
2668 Restore_Env;
2669 goto Leave;
2670 end if;
2671 end if;
2672
2673 -- Check that name of formal package does not hide name of generic,
2674 -- or its leading prefix. This check must be done separately because
2675 -- the name of the generic has already been analyzed.
2676
2677 declare
2678 Gen_Name : Entity_Id;
2679
2680 begin
2681 Gen_Name := Gen_Id;
2682 while Nkind (Gen_Name) = N_Expanded_Name loop
2683 Gen_Name := Prefix (Gen_Name);
2684 end loop;
2685
2686 if Chars (Gen_Name) = Chars (Pack_Id) then
2687 Error_Msg_NE
2688 ("& is hidden within declaration of formal package",
2689 Gen_Id, Gen_Name);
2690 end if;
2691 end;
2692
2693 if Box_Present (N)
2694 or else No (Generic_Associations (N))
2695 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2696 then
2697 Associations := False;
2698 end if;
2699
2700 -- If there are no generic associations, the generic parameters appear
2701 -- as local entities and are instantiated like them. We copy the generic
2702 -- package declaration as if it were an instantiation, and analyze it
2703 -- like a regular package, except that we treat the formals as
2704 -- additional visible components.
2705
2706 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
2707
2708 if In_Extended_Main_Source_Unit (N) then
2709 Set_Is_Instantiated (Gen_Unit);
2710 Generate_Reference (Gen_Unit, N);
2711 end if;
2712
2713 Formal := New_Copy (Pack_Id);
2714 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
2715
2716 -- Make local generic without formals. The formals will be replaced with
2717 -- internal declarations.
2718
2719 begin
2720 New_N := Build_Local_Package;
2721
2722 -- If there are errors in the parameter list, Analyze_Associations
2723 -- raises Instantiation_Error. Patch the declaration to prevent further
2724 -- exception propagation.
2725
2726 exception
2727 when Instantiation_Error =>
2728 Enter_Name (Formal);
2729 Set_Ekind (Formal, E_Variable);
2730 Set_Etype (Formal, Any_Type);
2731 Restore_Hidden_Primitives (Vis_Prims_List);
2732
2733 if Parent_Installed then
2734 Remove_Parent;
2735 end if;
2736
2737 goto Leave;
2738 end;
2739
2740 Rewrite (N, New_N);
2741 Set_Defining_Unit_Name (Specification (New_N), Formal);
2742 Set_Generic_Parent (Specification (N), Gen_Unit);
2743 Set_Instance_Env (Gen_Unit, Formal);
2744 Set_Is_Generic_Instance (Formal);
2745
2746 Enter_Name (Formal);
2747 Set_Ekind (Formal, E_Package);
2748 Set_Etype (Formal, Standard_Void_Type);
2749 Set_Inner_Instances (Formal, New_Elmt_List);
2750 Push_Scope (Formal);
2751
2752 -- Manually set the SPARK_Mode from the context because the package
2753 -- declaration is never analyzed.
2754
2755 Set_SPARK_Pragma (Formal, SPARK_Mode_Pragma);
2756 Set_SPARK_Aux_Pragma (Formal, SPARK_Mode_Pragma);
2757 Set_SPARK_Pragma_Inherited (Formal);
2758 Set_SPARK_Aux_Pragma_Inherited (Formal);
2759
2760 if Is_Child_Unit (Gen_Unit) and then Parent_Installed then
2761
2762 -- Similarly, we have to make the name of the formal visible in the
2763 -- parent instance, to resolve properly fully qualified names that
2764 -- may appear in the generic unit. The parent instance has been
2765 -- placed on the scope stack ahead of the current scope.
2766
2767 Parent_Instance := Scope_Stack.Table (Scope_Stack.Last - 1).Entity;
2768
2769 Renaming_In_Par :=
2770 Make_Defining_Identifier (Loc, Chars (Gen_Unit));
2771 Set_Ekind (Renaming_In_Par, E_Package);
2772 Set_Etype (Renaming_In_Par, Standard_Void_Type);
2773 Set_Scope (Renaming_In_Par, Parent_Instance);
2774 Set_Parent (Renaming_In_Par, Parent (Formal));
2775 Set_Renamed_Object (Renaming_In_Par, Formal);
2776 Append_Entity (Renaming_In_Par, Parent_Instance);
2777 end if;
2778
2779 -- A formal package declaration behaves as a package instantiation with
2780 -- respect to SPARK_Mode "off". If the annotation is "off" or altogether
2781 -- missing, set the global flag which signals Analyze_Pragma to ingnore
2782 -- all SPARK_Mode pragmas within the generic_package_name.
2783
2784 if SPARK_Mode /= On then
2785 Ignore_Pragma_SPARK_Mode := True;
2786 end if;
2787
2788 Analyze (Specification (N));
2789
2790 -- The formals for which associations are provided are not visible
2791 -- outside of the formal package. The others are still declared by a
2792 -- formal parameter declaration.
2793
2794 -- If there are no associations, the only local entity to hide is the
2795 -- generated package renaming itself.
2796
2797 declare
2798 E : Entity_Id;
2799
2800 begin
2801 E := First_Entity (Formal);
2802 while Present (E) loop
2803 if Associations and then not Is_Generic_Formal (E) then
2804 Set_Is_Hidden (E);
2805 end if;
2806
2807 if Ekind (E) = E_Package and then Renamed_Entity (E) = Formal then
2808 Set_Is_Hidden (E);
2809 exit;
2810 end if;
2811
2812 Next_Entity (E);
2813 end loop;
2814 end;
2815
2816 End_Package_Scope (Formal);
2817 Restore_Hidden_Primitives (Vis_Prims_List);
2818
2819 if Parent_Installed then
2820 Remove_Parent;
2821 end if;
2822
2823 Restore_Env;
2824
2825 -- Inside the generic unit, the formal package is a regular package, but
2826 -- no body is needed for it. Note that after instantiation, the defining
2827 -- unit name we need is in the new tree and not in the original (see
2828 -- Package_Instantiation). A generic formal package is an instance, and
2829 -- can be used as an actual for an inner instance.
2830
2831 Set_Has_Completion (Formal, True);
2832
2833 -- Add semantic information to the original defining identifier for ASIS
2834 -- use.
2835
2836 Set_Ekind (Pack_Id, E_Package);
2837 Set_Etype (Pack_Id, Standard_Void_Type);
2838 Set_Scope (Pack_Id, Scope (Formal));
2839 Set_Has_Completion (Pack_Id, True);
2840
2841 <<Leave>>
2842 if Has_Aspects (N) then
2843 Analyze_Aspect_Specifications (N, Pack_Id);
2844 end if;
2845
2846 Ignore_Pragma_SPARK_Mode := Save_IPSM;
2847 end Analyze_Formal_Package_Declaration;
2848
2849 ---------------------------------
2850 -- Analyze_Formal_Private_Type --
2851 ---------------------------------
2852
2853 procedure Analyze_Formal_Private_Type
2854 (N : Node_Id;
2855 T : Entity_Id;
2856 Def : Node_Id)
2857 is
2858 begin
2859 New_Private_Type (N, T, Def);
2860
2861 -- Set the size to an arbitrary but legal value
2862
2863 Set_Size_Info (T, Standard_Integer);
2864 Set_RM_Size (T, RM_Size (Standard_Integer));
2865 end Analyze_Formal_Private_Type;
2866
2867 ------------------------------------
2868 -- Analyze_Formal_Incomplete_Type --
2869 ------------------------------------
2870
2871 procedure Analyze_Formal_Incomplete_Type
2872 (T : Entity_Id;
2873 Def : Node_Id)
2874 is
2875 begin
2876 Enter_Name (T);
2877 Set_Ekind (T, E_Incomplete_Type);
2878 Set_Etype (T, T);
2879 Set_Private_Dependents (T, New_Elmt_List);
2880
2881 if Tagged_Present (Def) then
2882 Set_Is_Tagged_Type (T);
2883 Make_Class_Wide_Type (T);
2884 Set_Direct_Primitive_Operations (T, New_Elmt_List);
2885 end if;
2886 end Analyze_Formal_Incomplete_Type;
2887
2888 ----------------------------------------
2889 -- Analyze_Formal_Signed_Integer_Type --
2890 ----------------------------------------
2891
2892 procedure Analyze_Formal_Signed_Integer_Type
2893 (T : Entity_Id;
2894 Def : Node_Id)
2895 is
2896 Base : constant Entity_Id :=
2897 New_Internal_Entity
2898 (E_Signed_Integer_Type,
2899 Current_Scope,
2900 Sloc (Defining_Identifier (Parent (Def))), 'G');
2901
2902 begin
2903 Enter_Name (T);
2904
2905 Set_Ekind (T, E_Signed_Integer_Subtype);
2906 Set_Etype (T, Base);
2907 Set_Size_Info (T, Standard_Integer);
2908 Set_RM_Size (T, RM_Size (Standard_Integer));
2909 Set_Scalar_Range (T, Scalar_Range (Standard_Integer));
2910 Set_Is_Constrained (T);
2911
2912 Set_Is_Generic_Type (Base);
2913 Set_Size_Info (Base, Standard_Integer);
2914 Set_RM_Size (Base, RM_Size (Standard_Integer));
2915 Set_Etype (Base, Base);
2916 Set_Scalar_Range (Base, Scalar_Range (Standard_Integer));
2917 Set_Parent (Base, Parent (Def));
2918 end Analyze_Formal_Signed_Integer_Type;
2919
2920 -------------------------------------------
2921 -- Analyze_Formal_Subprogram_Declaration --
2922 -------------------------------------------
2923
2924 procedure Analyze_Formal_Subprogram_Declaration (N : Node_Id) is
2925 Spec : constant Node_Id := Specification (N);
2926 Def : constant Node_Id := Default_Name (N);
2927 Nam : constant Entity_Id := Defining_Unit_Name (Spec);
2928 Subp : Entity_Id;
2929
2930 begin
2931 if Nam = Error then
2932 return;
2933 end if;
2934
2935 if Nkind (Nam) = N_Defining_Program_Unit_Name then
2936 Error_Msg_N ("name of formal subprogram must be a direct name", Nam);
2937 goto Leave;
2938 end if;
2939
2940 Analyze_Subprogram_Declaration (N);
2941 Set_Is_Formal_Subprogram (Nam);
2942 Set_Has_Completion (Nam);
2943
2944 if Nkind (N) = N_Formal_Abstract_Subprogram_Declaration then
2945 Set_Is_Abstract_Subprogram (Nam);
2946
2947 Set_Is_Dispatching_Operation (Nam);
2948
2949 -- A formal abstract procedure cannot have a null default
2950 -- (RM 12.6(4.1/2)).
2951
2952 if Nkind (Spec) = N_Procedure_Specification
2953 and then Null_Present (Spec)
2954 then
2955 Error_Msg_N
2956 ("a formal abstract subprogram cannot default to null", Spec);
2957 end if;
2958
2959 declare
2960 Ctrl_Type : constant Entity_Id := Find_Dispatching_Type (Nam);
2961 begin
2962 if No (Ctrl_Type) then
2963 Error_Msg_N
2964 ("abstract formal subprogram must have a controlling type",
2965 N);
2966
2967 elsif Ada_Version >= Ada_2012
2968 and then Is_Incomplete_Type (Ctrl_Type)
2969 then
2970 Error_Msg_NE
2971 ("controlling type of abstract formal subprogram cannot "
2972 & "be incomplete type", N, Ctrl_Type);
2973
2974 else
2975 Check_Controlling_Formals (Ctrl_Type, Nam);
2976 end if;
2977 end;
2978 end if;
2979
2980 -- Default name is resolved at the point of instantiation
2981
2982 if Box_Present (N) then
2983 null;
2984
2985 -- Else default is bound at the point of generic declaration
2986
2987 elsif Present (Def) then
2988 if Nkind (Def) = N_Operator_Symbol then
2989 Find_Direct_Name (Def);
2990
2991 elsif Nkind (Def) /= N_Attribute_Reference then
2992 Analyze (Def);
2993
2994 else
2995 -- For an attribute reference, analyze the prefix and verify
2996 -- that it has the proper profile for the subprogram.
2997
2998 Analyze (Prefix (Def));
2999 Valid_Default_Attribute (Nam, Def);
3000 goto Leave;
3001 end if;
3002
3003 -- Default name may be overloaded, in which case the interpretation
3004 -- with the correct profile must be selected, as for a renaming.
3005 -- If the definition is an indexed component, it must denote a
3006 -- member of an entry family. If it is a selected component, it
3007 -- can be a protected operation.
3008
3009 if Etype (Def) = Any_Type then
3010 goto Leave;
3011
3012 elsif Nkind (Def) = N_Selected_Component then
3013 if not Is_Overloadable (Entity (Selector_Name (Def))) then
3014 Error_Msg_N ("expect valid subprogram name as default", Def);
3015 end if;
3016
3017 elsif Nkind (Def) = N_Indexed_Component then
3018 if Is_Entity_Name (Prefix (Def)) then
3019 if Ekind (Entity (Prefix (Def))) /= E_Entry_Family then
3020 Error_Msg_N ("expect valid subprogram name as default", Def);
3021 end if;
3022
3023 elsif Nkind (Prefix (Def)) = N_Selected_Component then
3024 if Ekind (Entity (Selector_Name (Prefix (Def)))) /=
3025 E_Entry_Family
3026 then
3027 Error_Msg_N ("expect valid subprogram name as default", Def);
3028 end if;
3029
3030 else
3031 Error_Msg_N ("expect valid subprogram name as default", Def);
3032 goto Leave;
3033 end if;
3034
3035 elsif Nkind (Def) = N_Character_Literal then
3036
3037 -- Needs some type checks: subprogram should be parameterless???
3038
3039 Resolve (Def, (Etype (Nam)));
3040
3041 elsif not Is_Entity_Name (Def)
3042 or else not Is_Overloadable (Entity (Def))
3043 then
3044 Error_Msg_N ("expect valid subprogram name as default", Def);
3045 goto Leave;
3046
3047 elsif not Is_Overloaded (Def) then
3048 Subp := Entity (Def);
3049
3050 if Subp = Nam then
3051 Error_Msg_N ("premature usage of formal subprogram", Def);
3052
3053 elsif not Entity_Matches_Spec (Subp, Nam) then
3054 Error_Msg_N ("no visible entity matches specification", Def);
3055 end if;
3056
3057 -- More than one interpretation, so disambiguate as for a renaming
3058
3059 else
3060 declare
3061 I : Interp_Index;
3062 I1 : Interp_Index := 0;
3063 It : Interp;
3064 It1 : Interp;
3065
3066 begin
3067 Subp := Any_Id;
3068 Get_First_Interp (Def, I, It);
3069 while Present (It.Nam) loop
3070 if Entity_Matches_Spec (It.Nam, Nam) then
3071 if Subp /= Any_Id then
3072 It1 := Disambiguate (Def, I1, I, Etype (Subp));
3073
3074 if It1 = No_Interp then
3075 Error_Msg_N ("ambiguous default subprogram", Def);
3076 else
3077 Subp := It1.Nam;
3078 end if;
3079
3080 exit;
3081
3082 else
3083 I1 := I;
3084 Subp := It.Nam;
3085 end if;
3086 end if;
3087
3088 Get_Next_Interp (I, It);
3089 end loop;
3090 end;
3091
3092 if Subp /= Any_Id then
3093
3094 -- Subprogram found, generate reference to it
3095
3096 Set_Entity (Def, Subp);
3097 Generate_Reference (Subp, Def);
3098
3099 if Subp = Nam then
3100 Error_Msg_N ("premature usage of formal subprogram", Def);
3101
3102 elsif Ekind (Subp) /= E_Operator then
3103 Check_Mode_Conformant (Subp, Nam);
3104 end if;
3105
3106 else
3107 Error_Msg_N ("no visible subprogram matches specification", N);
3108 end if;
3109 end if;
3110 end if;
3111
3112 <<Leave>>
3113 if Has_Aspects (N) then
3114 Analyze_Aspect_Specifications (N, Nam);
3115 end if;
3116
3117 end Analyze_Formal_Subprogram_Declaration;
3118
3119 -------------------------------------
3120 -- Analyze_Formal_Type_Declaration --
3121 -------------------------------------
3122
3123 procedure Analyze_Formal_Type_Declaration (N : Node_Id) is
3124 Def : constant Node_Id := Formal_Type_Definition (N);
3125 T : Entity_Id;
3126
3127 begin
3128 T := Defining_Identifier (N);
3129
3130 if Present (Discriminant_Specifications (N))
3131 and then Nkind (Def) /= N_Formal_Private_Type_Definition
3132 then
3133 Error_Msg_N
3134 ("discriminants not allowed for this formal type", T);
3135 end if;
3136
3137 -- Enter the new name, and branch to specific routine
3138
3139 case Nkind (Def) is
3140 when N_Formal_Private_Type_Definition =>
3141 Analyze_Formal_Private_Type (N, T, Def);
3142
3143 when N_Formal_Derived_Type_Definition =>
3144 Analyze_Formal_Derived_Type (N, T, Def);
3145
3146 when N_Formal_Incomplete_Type_Definition =>
3147 Analyze_Formal_Incomplete_Type (T, Def);
3148
3149 when N_Formal_Discrete_Type_Definition =>
3150 Analyze_Formal_Discrete_Type (T, Def);
3151
3152 when N_Formal_Signed_Integer_Type_Definition =>
3153 Analyze_Formal_Signed_Integer_Type (T, Def);
3154
3155 when N_Formal_Modular_Type_Definition =>
3156 Analyze_Formal_Modular_Type (T, Def);
3157
3158 when N_Formal_Floating_Point_Definition =>
3159 Analyze_Formal_Floating_Type (T, Def);
3160
3161 when N_Formal_Ordinary_Fixed_Point_Definition =>
3162 Analyze_Formal_Ordinary_Fixed_Point_Type (T, Def);
3163
3164 when N_Formal_Decimal_Fixed_Point_Definition =>
3165 Analyze_Formal_Decimal_Fixed_Point_Type (T, Def);
3166
3167 when N_Array_Type_Definition =>
3168 Analyze_Formal_Array_Type (T, Def);
3169
3170 when N_Access_Function_Definition
3171 | N_Access_Procedure_Definition
3172 | N_Access_To_Object_Definition
3173 =>
3174 Analyze_Generic_Access_Type (T, Def);
3175
3176 -- Ada 2005: a interface declaration is encoded as an abstract
3177 -- record declaration or a abstract type derivation.
3178
3179 when N_Record_Definition =>
3180 Analyze_Formal_Interface_Type (N, T, Def);
3181
3182 when N_Derived_Type_Definition =>
3183 Analyze_Formal_Derived_Interface_Type (N, T, Def);
3184
3185 when N_Error =>
3186 null;
3187
3188 when others =>
3189 raise Program_Error;
3190 end case;
3191
3192 Set_Is_Generic_Type (T);
3193
3194 if Has_Aspects (N) then
3195 Analyze_Aspect_Specifications (N, T);
3196 end if;
3197 end Analyze_Formal_Type_Declaration;
3198
3199 ------------------------------------
3200 -- Analyze_Function_Instantiation --
3201 ------------------------------------
3202
3203 procedure Analyze_Function_Instantiation (N : Node_Id) is
3204 begin
3205 Analyze_Subprogram_Instantiation (N, E_Function);
3206 end Analyze_Function_Instantiation;
3207
3208 ---------------------------------
3209 -- Analyze_Generic_Access_Type --
3210 ---------------------------------
3211
3212 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id) is
3213 begin
3214 Enter_Name (T);
3215
3216 if Nkind (Def) = N_Access_To_Object_Definition then
3217 Access_Type_Declaration (T, Def);
3218
3219 if Is_Incomplete_Or_Private_Type (Designated_Type (T))
3220 and then No (Full_View (Designated_Type (T)))
3221 and then not Is_Generic_Type (Designated_Type (T))
3222 then
3223 Error_Msg_N ("premature usage of incomplete type", Def);
3224
3225 elsif not Is_Entity_Name (Subtype_Indication (Def)) then
3226 Error_Msg_N
3227 ("only a subtype mark is allowed in a formal", Def);
3228 end if;
3229
3230 else
3231 Access_Subprogram_Declaration (T, Def);
3232 end if;
3233 end Analyze_Generic_Access_Type;
3234
3235 ---------------------------------
3236 -- Analyze_Generic_Formal_Part --
3237 ---------------------------------
3238
3239 procedure Analyze_Generic_Formal_Part (N : Node_Id) is
3240 Gen_Parm_Decl : Node_Id;
3241
3242 begin
3243 -- The generic formals are processed in the scope of the generic unit,
3244 -- where they are immediately visible. The scope is installed by the
3245 -- caller.
3246
3247 Gen_Parm_Decl := First (Generic_Formal_Declarations (N));
3248 while Present (Gen_Parm_Decl) loop
3249 Analyze (Gen_Parm_Decl);
3250 Next (Gen_Parm_Decl);
3251 end loop;
3252
3253 Generate_Reference_To_Generic_Formals (Current_Scope);
3254 end Analyze_Generic_Formal_Part;
3255
3256 ------------------------------------------
3257 -- Analyze_Generic_Package_Declaration --
3258 ------------------------------------------
3259
3260 procedure Analyze_Generic_Package_Declaration (N : Node_Id) is
3261 Loc : constant Source_Ptr := Sloc (N);
3262 Decls : constant List_Id :=
3263 Visible_Declarations (Specification (N));
3264 Decl : Node_Id;
3265 Id : Entity_Id;
3266 New_N : Node_Id;
3267 Renaming : Node_Id;
3268 Save_Parent : Node_Id;
3269
3270 begin
3271 Check_SPARK_05_Restriction ("generic is not allowed", N);
3272
3273 -- We introduce a renaming of the enclosing package, to have a usable
3274 -- entity as the prefix of an expanded name for a local entity of the
3275 -- form Par.P.Q, where P is the generic package. This is because a local
3276 -- entity named P may hide it, so that the usual visibility rules in
3277 -- the instance will not resolve properly.
3278
3279 Renaming :=
3280 Make_Package_Renaming_Declaration (Loc,
3281 Defining_Unit_Name =>
3282 Make_Defining_Identifier (Loc,
3283 Chars => New_External_Name (Chars (Defining_Entity (N)), "GH")),
3284 Name =>
3285 Make_Identifier (Loc, Chars (Defining_Entity (N))));
3286
3287 if Present (Decls) then
3288 Decl := First (Decls);
3289 while Present (Decl) and then Nkind (Decl) = N_Pragma loop
3290 Next (Decl);
3291 end loop;
3292
3293 if Present (Decl) then
3294 Insert_Before (Decl, Renaming);
3295 else
3296 Append (Renaming, Visible_Declarations (Specification (N)));
3297 end if;
3298
3299 else
3300 Set_Visible_Declarations (Specification (N), New_List (Renaming));
3301 end if;
3302
3303 -- Create copy of generic unit, and save for instantiation. If the unit
3304 -- is a child unit, do not copy the specifications for the parent, which
3305 -- are not part of the generic tree.
3306
3307 Save_Parent := Parent_Spec (N);
3308 Set_Parent_Spec (N, Empty);
3309
3310 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
3311 Set_Parent_Spec (New_N, Save_Parent);
3312 Rewrite (N, New_N);
3313
3314 -- Once the contents of the generic copy and the template are swapped,
3315 -- do the same for their respective aspect specifications.
3316
3317 Exchange_Aspects (N, New_N);
3318
3319 -- Collect all contract-related source pragmas found within the template
3320 -- and attach them to the contract of the package spec. This contract is
3321 -- used in the capture of global references within annotations.
3322
3323 Create_Generic_Contract (N);
3324
3325 Id := Defining_Entity (N);
3326 Generate_Definition (Id);
3327
3328 -- Expansion is not applied to generic units
3329
3330 Start_Generic;
3331
3332 Enter_Name (Id);
3333 Set_Ekind (Id, E_Generic_Package);
3334 Set_Etype (Id, Standard_Void_Type);
3335
3336 -- Analyze aspects now, so that generated pragmas appear in the
3337 -- declarations before building and analyzing the generic copy.
3338
3339 if Has_Aspects (N) then
3340 Analyze_Aspect_Specifications (N, Id);
3341 end if;
3342
3343 Push_Scope (Id);
3344 Enter_Generic_Scope (Id);
3345 Set_Inner_Instances (Id, New_Elmt_List);
3346
3347 Set_Categorization_From_Pragmas (N);
3348 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3349
3350 -- Link the declaration of the generic homonym in the generic copy to
3351 -- the package it renames, so that it is always resolved properly.
3352
3353 Set_Generic_Homonym (Id, Defining_Unit_Name (Renaming));
3354 Set_Entity (Associated_Node (Name (Renaming)), Id);
3355
3356 -- For a library unit, we have reconstructed the entity for the unit,
3357 -- and must reset it in the library tables.
3358
3359 if Nkind (Parent (N)) = N_Compilation_Unit then
3360 Set_Cunit_Entity (Current_Sem_Unit, Id);
3361 end if;
3362
3363 Analyze_Generic_Formal_Part (N);
3364
3365 -- After processing the generic formals, analysis proceeds as for a
3366 -- non-generic package.
3367
3368 Analyze (Specification (N));
3369
3370 Validate_Categorization_Dependency (N, Id);
3371
3372 End_Generic;
3373
3374 End_Package_Scope (Id);
3375 Exit_Generic_Scope (Id);
3376
3377 if Nkind (Parent (N)) /= N_Compilation_Unit then
3378 Move_Freeze_Nodes (Id, N, Visible_Declarations (Specification (N)));
3379 Move_Freeze_Nodes (Id, N, Private_Declarations (Specification (N)));
3380 Move_Freeze_Nodes (Id, N, Generic_Formal_Declarations (N));
3381
3382 else
3383 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
3384 Validate_RT_RAT_Component (N);
3385
3386 -- If this is a spec without a body, check that generic parameters
3387 -- are referenced.
3388
3389 if not Body_Required (Parent (N)) then
3390 Check_References (Id);
3391 end if;
3392 end if;
3393
3394 -- If there is a specified storage pool in the context, create an
3395 -- aspect on the package declaration, so that it is used in any
3396 -- instance that does not override it.
3397
3398 if Present (Default_Pool) then
3399 declare
3400 ASN : Node_Id;
3401
3402 begin
3403 ASN :=
3404 Make_Aspect_Specification (Loc,
3405 Identifier => Make_Identifier (Loc, Name_Default_Storage_Pool),
3406 Expression => New_Copy (Default_Pool));
3407
3408 if No (Aspect_Specifications (Specification (N))) then
3409 Set_Aspect_Specifications (Specification (N), New_List (ASN));
3410 else
3411 Append (ASN, Aspect_Specifications (Specification (N)));
3412 end if;
3413 end;
3414 end if;
3415 end Analyze_Generic_Package_Declaration;
3416
3417 --------------------------------------------
3418 -- Analyze_Generic_Subprogram_Declaration --
3419 --------------------------------------------
3420
3421 procedure Analyze_Generic_Subprogram_Declaration (N : Node_Id) is
3422 Formals : List_Id;
3423 Id : Entity_Id;
3424 New_N : Node_Id;
3425 Result_Type : Entity_Id;
3426 Save_Parent : Node_Id;
3427 Spec : Node_Id;
3428 Typ : Entity_Id;
3429
3430 begin
3431 Check_SPARK_05_Restriction ("generic is not allowed", N);
3432
3433 -- Create copy of generic unit, and save for instantiation. If the unit
3434 -- is a child unit, do not copy the specifications for the parent, which
3435 -- are not part of the generic tree.
3436
3437 Save_Parent := Parent_Spec (N);
3438 Set_Parent_Spec (N, Empty);
3439
3440 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
3441 Set_Parent_Spec (New_N, Save_Parent);
3442 Rewrite (N, New_N);
3443
3444 -- Once the contents of the generic copy and the template are swapped,
3445 -- do the same for their respective aspect specifications.
3446
3447 Exchange_Aspects (N, New_N);
3448
3449 -- Collect all contract-related source pragmas found within the template
3450 -- and attach them to the contract of the subprogram spec. This contract
3451 -- is used in the capture of global references within annotations.
3452
3453 Create_Generic_Contract (N);
3454
3455 Spec := Specification (N);
3456 Id := Defining_Entity (Spec);
3457 Generate_Definition (Id);
3458
3459 if Nkind (Id) = N_Defining_Operator_Symbol then
3460 Error_Msg_N
3461 ("operator symbol not allowed for generic subprogram", Id);
3462 end if;
3463
3464 Start_Generic;
3465
3466 Enter_Name (Id);
3467 Set_Scope_Depth_Value (Id, Scope_Depth (Current_Scope) + 1);
3468
3469 -- Analyze the aspects of the generic copy to ensure that all generated
3470 -- pragmas (if any) perform their semantic effects.
3471
3472 if Has_Aspects (N) then
3473 Analyze_Aspect_Specifications (N, Id);
3474 end if;
3475
3476 Push_Scope (Id);
3477 Enter_Generic_Scope (Id);
3478 Set_Inner_Instances (Id, New_Elmt_List);
3479 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3480
3481 Analyze_Generic_Formal_Part (N);
3482
3483 Formals := Parameter_Specifications (Spec);
3484
3485 if Nkind (Spec) = N_Function_Specification then
3486 Set_Ekind (Id, E_Generic_Function);
3487 else
3488 Set_Ekind (Id, E_Generic_Procedure);
3489 end if;
3490
3491 if Present (Formals) then
3492 Process_Formals (Formals, Spec);
3493 end if;
3494
3495 if Nkind (Spec) = N_Function_Specification then
3496 if Nkind (Result_Definition (Spec)) = N_Access_Definition then
3497 Result_Type := Access_Definition (Spec, Result_Definition (Spec));
3498 Set_Etype (Id, Result_Type);
3499
3500 -- Check restriction imposed by AI05-073: a generic function
3501 -- cannot return an abstract type or an access to such.
3502
3503 -- This is a binding interpretation should it apply to earlier
3504 -- versions of Ada as well as Ada 2012???
3505
3506 if Is_Abstract_Type (Designated_Type (Result_Type))
3507 and then Ada_Version >= Ada_2012
3508 then
3509 Error_Msg_N
3510 ("generic function cannot have an access result "
3511 & "that designates an abstract type", Spec);
3512 end if;
3513
3514 else
3515 Find_Type (Result_Definition (Spec));
3516 Typ := Entity (Result_Definition (Spec));
3517
3518 if Is_Abstract_Type (Typ)
3519 and then Ada_Version >= Ada_2012
3520 then
3521 Error_Msg_N
3522 ("generic function cannot have abstract result type", Spec);
3523 end if;
3524
3525 -- If a null exclusion is imposed on the result type, then create
3526 -- a null-excluding itype (an access subtype) and use it as the
3527 -- function's Etype.
3528
3529 if Is_Access_Type (Typ)
3530 and then Null_Exclusion_Present (Spec)
3531 then
3532 Set_Etype (Id,
3533 Create_Null_Excluding_Itype
3534 (T => Typ,
3535 Related_Nod => Spec,
3536 Scope_Id => Defining_Unit_Name (Spec)));
3537 else
3538 Set_Etype (Id, Typ);
3539 end if;
3540 end if;
3541
3542 else
3543 Set_Etype (Id, Standard_Void_Type);
3544 end if;
3545
3546 -- For a library unit, we have reconstructed the entity for the unit,
3547 -- and must reset it in the library tables. We also make sure that
3548 -- Body_Required is set properly in the original compilation unit node.
3549
3550 if Nkind (Parent (N)) = N_Compilation_Unit then
3551 Set_Cunit_Entity (Current_Sem_Unit, Id);
3552 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
3553 end if;
3554
3555 Set_Categorization_From_Pragmas (N);
3556 Validate_Categorization_Dependency (N, Id);
3557
3558 -- Capture all global references that occur within the profile of the
3559 -- generic subprogram. Aspects are not part of this processing because
3560 -- they must be delayed. If processed now, Save_Global_References will
3561 -- destroy the Associated_Node links and prevent the capture of global
3562 -- references when the contract of the generic subprogram is analyzed.
3563
3564 Save_Global_References (Original_Node (N));
3565
3566 End_Generic;
3567 End_Scope;
3568 Exit_Generic_Scope (Id);
3569 Generate_Reference_To_Formals (Id);
3570
3571 List_Inherited_Pre_Post_Aspects (Id);
3572 end Analyze_Generic_Subprogram_Declaration;
3573
3574 -----------------------------------
3575 -- Analyze_Package_Instantiation --
3576 -----------------------------------
3577
3578 -- WARNING: This routine manages Ghost regions. Return statements must be
3579 -- replaced by gotos which jump to the end of the routine and restore the
3580 -- Ghost mode.
3581
3582 procedure Analyze_Package_Instantiation (N : Node_Id) is
3583 Loc : constant Source_Ptr := Sloc (N);
3584 Gen_Id : constant Node_Id := Name (N);
3585
3586 Act_Decl : Node_Id;
3587 Act_Decl_Name : Node_Id;
3588 Act_Decl_Id : Entity_Id;
3589 Act_Spec : Node_Id;
3590 Act_Tree : Node_Id;
3591
3592 Gen_Decl : Node_Id;
3593 Gen_Spec : Node_Id;
3594 Gen_Unit : Entity_Id;
3595
3596 Is_Actual_Pack : constant Boolean :=
3597 Is_Internal (Defining_Entity (N));
3598
3599 Env_Installed : Boolean := False;
3600 Parent_Installed : Boolean := False;
3601 Renaming_List : List_Id;
3602 Unit_Renaming : Node_Id;
3603 Needs_Body : Boolean;
3604 Inline_Now : Boolean := False;
3605 Has_Inline_Always : Boolean := False;
3606
3607 Save_IPSM : constant Boolean := Ignore_Pragma_SPARK_Mode;
3608 -- Save flag Ignore_Pragma_SPARK_Mode for restore on exit
3609
3610 Save_SM : constant SPARK_Mode_Type := SPARK_Mode;
3611 Save_SMP : constant Node_Id := SPARK_Mode_Pragma;
3612 -- Save the SPARK_Mode-related data for restore on exit
3613
3614 Save_Style_Check : constant Boolean := Style_Check;
3615 -- Save style check mode for restore on exit
3616
3617 procedure Delay_Descriptors (E : Entity_Id);
3618 -- Delay generation of subprogram descriptors for given entity
3619
3620 function Might_Inline_Subp return Boolean;
3621 -- If inlining is active and the generic contains inlined subprograms,
3622 -- we instantiate the body. This may cause superfluous instantiations,
3623 -- but it is simpler than detecting the need for the body at the point
3624 -- of inlining, when the context of the instance is not available.
3625
3626 -----------------------
3627 -- Delay_Descriptors --
3628 -----------------------
3629
3630 procedure Delay_Descriptors (E : Entity_Id) is
3631 begin
3632 if not Delay_Subprogram_Descriptors (E) then
3633 Set_Delay_Subprogram_Descriptors (E);
3634 Pending_Descriptor.Append (E);
3635 end if;
3636 end Delay_Descriptors;
3637
3638 -----------------------
3639 -- Might_Inline_Subp --
3640 -----------------------
3641
3642 function Might_Inline_Subp return Boolean is
3643 E : Entity_Id;
3644
3645 begin
3646 if not Inline_Processing_Required then
3647 return False;
3648
3649 else
3650 E := First_Entity (Gen_Unit);
3651 while Present (E) loop
3652 if Is_Subprogram (E) and then Is_Inlined (E) then
3653 -- Remember if there are any subprograms with Inline_Always
3654
3655 if Has_Pragma_Inline_Always (E) then
3656 Has_Inline_Always := True;
3657 end if;
3658
3659 return True;
3660 end if;
3661
3662 Next_Entity (E);
3663 end loop;
3664 end if;
3665
3666 return False;
3667 end Might_Inline_Subp;
3668
3669 -- Local declarations
3670
3671 Mode : Ghost_Mode_Type;
3672
3673 Vis_Prims_List : Elist_Id := No_Elist;
3674 -- List of primitives made temporarily visible in the instantiation
3675 -- to match the visibility of the formal type
3676
3677 -- Start of processing for Analyze_Package_Instantiation
3678
3679 begin
3680 Check_SPARK_05_Restriction ("generic is not allowed", N);
3681
3682 -- Very first thing: check for Text_IO special unit in case we are
3683 -- instantiating one of the children of [[Wide_]Wide_]Text_IO.
3684
3685 Check_Text_IO_Special_Unit (Name (N));
3686
3687 -- Make node global for error reporting
3688
3689 Instantiation_Node := N;
3690
3691 -- Case of instantiation of a generic package
3692
3693 if Nkind (N) = N_Package_Instantiation then
3694 Act_Decl_Id := New_Copy (Defining_Entity (N));
3695 Set_Comes_From_Source (Act_Decl_Id, True);
3696
3697 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name then
3698 Act_Decl_Name :=
3699 Make_Defining_Program_Unit_Name (Loc,
3700 Name =>
3701 New_Copy_Tree (Name (Defining_Unit_Name (N))),
3702 Defining_Identifier => Act_Decl_Id);
3703 else
3704 Act_Decl_Name := Act_Decl_Id;
3705 end if;
3706
3707 -- Case of instantiation of a formal package
3708
3709 else
3710 Act_Decl_Id := Defining_Identifier (N);
3711 Act_Decl_Name := Act_Decl_Id;
3712 end if;
3713
3714 Generate_Definition (Act_Decl_Id);
3715 Set_Ekind (Act_Decl_Id, E_Package);
3716
3717 -- Initialize list of incomplete actuals before analysis
3718
3719 Set_Incomplete_Actuals (Act_Decl_Id, New_Elmt_List);
3720
3721 Preanalyze_Actuals (N, Act_Decl_Id);
3722
3723 -- Turn off style checking in instances. If the check is enabled on the
3724 -- generic unit, a warning in an instance would just be noise. If not
3725 -- enabled on the generic, then a warning in an instance is just wrong.
3726
3727 Style_Check := False;
3728
3729 Init_Env;
3730 Env_Installed := True;
3731
3732 -- Reset renaming map for formal types. The mapping is established
3733 -- when analyzing the generic associations, but some mappings are
3734 -- inherited from formal packages of parent units, and these are
3735 -- constructed when the parents are installed.
3736
3737 Generic_Renamings.Set_Last (0);
3738 Generic_Renamings_HTable.Reset;
3739
3740 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
3741 Gen_Unit := Entity (Gen_Id);
3742
3743 -- A package instantiation is Ghost when it is subject to pragma Ghost
3744 -- or the generic template is Ghost. Set the mode now to ensure that
3745 -- any nodes generated during analysis and expansion are marked as
3746 -- Ghost.
3747
3748 Mark_And_Set_Ghost_Instantiation (N, Gen_Unit, Mode);
3749
3750 -- Verify that it is the name of a generic package
3751
3752 -- A visibility glitch: if the instance is a child unit and the generic
3753 -- is the generic unit of a parent instance (i.e. both the parent and
3754 -- the child units are instances of the same package) the name now
3755 -- denotes the renaming within the parent, not the intended generic
3756 -- unit. See if there is a homonym that is the desired generic. The
3757 -- renaming declaration must be visible inside the instance of the
3758 -- child, but not when analyzing the name in the instantiation itself.
3759
3760 if Ekind (Gen_Unit) = E_Package
3761 and then Present (Renamed_Entity (Gen_Unit))
3762 and then In_Open_Scopes (Renamed_Entity (Gen_Unit))
3763 and then Is_Generic_Instance (Renamed_Entity (Gen_Unit))
3764 and then Present (Homonym (Gen_Unit))
3765 then
3766 Gen_Unit := Homonym (Gen_Unit);
3767 end if;
3768
3769 if Etype (Gen_Unit) = Any_Type then
3770 Restore_Env;
3771 goto Leave;
3772
3773 elsif Ekind (Gen_Unit) /= E_Generic_Package then
3774
3775 -- Ada 2005 (AI-50217): Cannot use instance in limited with_clause
3776
3777 if From_Limited_With (Gen_Unit) then
3778 Error_Msg_N
3779 ("cannot instantiate a limited withed package", Gen_Id);
3780 else
3781 Error_Msg_NE
3782 ("& is not the name of a generic package", Gen_Id, Gen_Unit);
3783 end if;
3784
3785 Restore_Env;
3786 goto Leave;
3787 end if;
3788
3789 if In_Extended_Main_Source_Unit (N) then
3790 Set_Is_Instantiated (Gen_Unit);
3791 Generate_Reference (Gen_Unit, N);
3792
3793 if Present (Renamed_Object (Gen_Unit)) then
3794 Set_Is_Instantiated (Renamed_Object (Gen_Unit));
3795 Generate_Reference (Renamed_Object (Gen_Unit), N);
3796 end if;
3797 end if;
3798
3799 if Nkind (Gen_Id) = N_Identifier
3800 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
3801 then
3802 Error_Msg_NE
3803 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
3804
3805 elsif Nkind (Gen_Id) = N_Expanded_Name
3806 and then Is_Child_Unit (Gen_Unit)
3807 and then Nkind (Prefix (Gen_Id)) = N_Identifier
3808 and then Chars (Act_Decl_Id) = Chars (Prefix (Gen_Id))
3809 then
3810 Error_Msg_N
3811 ("& is hidden within declaration of instance ", Prefix (Gen_Id));
3812 end if;
3813
3814 Set_Entity (Gen_Id, Gen_Unit);
3815
3816 -- If generic is a renaming, get original generic unit
3817
3818 if Present (Renamed_Object (Gen_Unit))
3819 and then Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Package
3820 then
3821 Gen_Unit := Renamed_Object (Gen_Unit);
3822 end if;
3823
3824 -- Verify that there are no circular instantiations
3825
3826 if In_Open_Scopes (Gen_Unit) then
3827 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
3828 Restore_Env;
3829 goto Leave;
3830
3831 elsif Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
3832 Error_Msg_Node_2 := Current_Scope;
3833 Error_Msg_NE
3834 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
3835 Circularity_Detected := True;
3836 Restore_Env;
3837 goto Leave;
3838
3839 else
3840 -- If the context of the instance is subject to SPARK_Mode "off" or
3841 -- the annotation is altogether missing, set the global flag which
3842 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
3843 -- the instance.
3844
3845 if SPARK_Mode /= On then
3846 Ignore_Pragma_SPARK_Mode := True;
3847 end if;
3848
3849 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
3850 Gen_Spec := Specification (Gen_Decl);
3851
3852 -- Initialize renamings map, for error checking, and the list that
3853 -- holds private entities whose views have changed between generic
3854 -- definition and instantiation. If this is the instance created to
3855 -- validate an actual package, the instantiation environment is that
3856 -- of the enclosing instance.
3857
3858 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
3859
3860 -- Copy original generic tree, to produce text for instantiation
3861
3862 Act_Tree :=
3863 Copy_Generic_Node
3864 (Original_Node (Gen_Decl), Empty, Instantiating => True);
3865
3866 Act_Spec := Specification (Act_Tree);
3867
3868 -- If this is the instance created to validate an actual package,
3869 -- only the formals matter, do not examine the package spec itself.
3870
3871 if Is_Actual_Pack then
3872 Set_Visible_Declarations (Act_Spec, New_List);
3873 Set_Private_Declarations (Act_Spec, New_List);
3874 end if;
3875
3876 Renaming_List :=
3877 Analyze_Associations
3878 (I_Node => N,
3879 Formals => Generic_Formal_Declarations (Act_Tree),
3880 F_Copy => Generic_Formal_Declarations (Gen_Decl));
3881
3882 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
3883
3884 Set_Instance_Env (Gen_Unit, Act_Decl_Id);
3885 Set_Defining_Unit_Name (Act_Spec, Act_Decl_Name);
3886 Set_Is_Generic_Instance (Act_Decl_Id);
3887 Set_Generic_Parent (Act_Spec, Gen_Unit);
3888
3889 -- References to the generic in its own declaration or its body are
3890 -- references to the instance. Add a renaming declaration for the
3891 -- generic unit itself. This declaration, as well as the renaming
3892 -- declarations for the generic formals, must remain private to the
3893 -- unit: the formals, because this is the language semantics, and
3894 -- the unit because its use is an artifact of the implementation.
3895
3896 Unit_Renaming :=
3897 Make_Package_Renaming_Declaration (Loc,
3898 Defining_Unit_Name =>
3899 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
3900 Name => New_Occurrence_Of (Act_Decl_Id, Loc));
3901
3902 Append (Unit_Renaming, Renaming_List);
3903
3904 -- The renaming declarations are the first local declarations of the
3905 -- new unit.
3906
3907 if Is_Non_Empty_List (Visible_Declarations (Act_Spec)) then
3908 Insert_List_Before
3909 (First (Visible_Declarations (Act_Spec)), Renaming_List);
3910 else
3911 Set_Visible_Declarations (Act_Spec, Renaming_List);
3912 end if;
3913
3914 Act_Decl := Make_Package_Declaration (Loc, Specification => Act_Spec);
3915
3916 -- Propagate the aspect specifications from the package declaration
3917 -- template to the instantiated version of the package declaration.
3918
3919 if Has_Aspects (Act_Tree) then
3920 Set_Aspect_Specifications (Act_Decl,
3921 New_Copy_List_Tree (Aspect_Specifications (Act_Tree)));
3922 end if;
3923
3924 -- The generic may have a generated Default_Storage_Pool aspect,
3925 -- set at the point of generic declaration. If the instance has
3926 -- that aspect, it overrides the one inherited from the generic.
3927
3928 if Has_Aspects (Gen_Spec) then
3929 if No (Aspect_Specifications (N)) then
3930 Set_Aspect_Specifications (N,
3931 (New_Copy_List_Tree
3932 (Aspect_Specifications (Gen_Spec))));
3933
3934 else
3935 declare
3936 ASN1, ASN2 : Node_Id;
3937
3938 begin
3939 ASN1 := First (Aspect_Specifications (N));
3940 while Present (ASN1) loop
3941 if Chars (Identifier (ASN1)) = Name_Default_Storage_Pool
3942 then
3943 -- If generic carries a default storage pool, remove
3944 -- it in favor of the instance one.
3945
3946 ASN2 := First (Aspect_Specifications (Gen_Spec));
3947 while Present (ASN2) loop
3948 if Chars (Identifier (ASN2)) =
3949 Name_Default_Storage_Pool
3950 then
3951 Remove (ASN2);
3952 exit;
3953 end if;
3954
3955 Next (ASN2);
3956 end loop;
3957 end if;
3958
3959 Next (ASN1);
3960 end loop;
3961
3962 Prepend_List_To (Aspect_Specifications (N),
3963 (New_Copy_List_Tree
3964 (Aspect_Specifications (Gen_Spec))));
3965 end;
3966 end if;
3967 end if;
3968
3969 -- Save the instantiation node, for subsequent instantiation of the
3970 -- body, if there is one and we are generating code for the current
3971 -- unit. Mark unit as having a body (avoids premature error message).
3972
3973 -- We instantiate the body if we are generating code, if we are
3974 -- generating cross-reference information, or if we are building
3975 -- trees for ASIS use or GNATprove use.
3976
3977 declare
3978 Enclosing_Body_Present : Boolean := False;
3979 -- If the generic unit is not a compilation unit, then a body may
3980 -- be present in its parent even if none is required. We create a
3981 -- tentative pending instantiation for the body, which will be
3982 -- discarded if none is actually present.
3983
3984 Scop : Entity_Id;
3985
3986 begin
3987 if Scope (Gen_Unit) /= Standard_Standard
3988 and then not Is_Child_Unit (Gen_Unit)
3989 then
3990 Scop := Scope (Gen_Unit);
3991 while Present (Scop) and then Scop /= Standard_Standard loop
3992 if Unit_Requires_Body (Scop) then
3993 Enclosing_Body_Present := True;
3994 exit;
3995
3996 elsif In_Open_Scopes (Scop)
3997 and then In_Package_Body (Scop)
3998 then
3999 Enclosing_Body_Present := True;
4000 exit;
4001 end if;
4002
4003 exit when Is_Compilation_Unit (Scop);
4004 Scop := Scope (Scop);
4005 end loop;
4006 end if;
4007
4008 -- If front-end inlining is enabled or there are any subprograms
4009 -- marked with Inline_Always, and this is a unit for which code
4010 -- will be generated, we instantiate the body at once.
4011
4012 -- This is done if the instance is not the main unit, and if the
4013 -- generic is not a child unit of another generic, to avoid scope
4014 -- problems and the reinstallation of parent instances.
4015
4016 if Expander_Active
4017 and then (not Is_Child_Unit (Gen_Unit)
4018 or else not Is_Generic_Unit (Scope (Gen_Unit)))
4019 and then Might_Inline_Subp
4020 and then not Is_Actual_Pack
4021 then
4022 if not Back_End_Inlining
4023 and then (Front_End_Inlining or else Has_Inline_Always)
4024 and then (Is_In_Main_Unit (N)
4025 or else In_Main_Context (Current_Scope))
4026 and then Nkind (Parent (N)) /= N_Compilation_Unit
4027 then
4028 Inline_Now := True;
4029
4030 -- In configurable_run_time mode we force the inlining of
4031 -- predefined subprograms marked Inline_Always, to minimize
4032 -- the use of the run-time library.
4033
4034 elsif Is_Predefined_File_Name
4035 (Unit_File_Name (Get_Source_Unit (Gen_Decl)))
4036 and then Configurable_Run_Time_Mode
4037 and then Nkind (Parent (N)) /= N_Compilation_Unit
4038 then
4039 Inline_Now := True;
4040 end if;
4041
4042 -- If the current scope is itself an instance within a child
4043 -- unit, there will be duplications in the scope stack, and the
4044 -- unstacking mechanism in Inline_Instance_Body will fail.
4045 -- This loses some rare cases of optimization, and might be
4046 -- improved some day, if we can find a proper abstraction for
4047 -- "the complete compilation context" that can be saved and
4048 -- restored. ???
4049
4050 if Is_Generic_Instance (Current_Scope) then
4051 declare
4052 Curr_Unit : constant Entity_Id :=
4053 Cunit_Entity (Current_Sem_Unit);
4054 begin
4055 if Curr_Unit /= Current_Scope
4056 and then Is_Child_Unit (Curr_Unit)
4057 then
4058 Inline_Now := False;
4059 end if;
4060 end;
4061 end if;
4062 end if;
4063
4064 Needs_Body :=
4065 (Unit_Requires_Body (Gen_Unit)
4066 or else Enclosing_Body_Present
4067 or else Present (Corresponding_Body (Gen_Decl)))
4068 and then (Is_In_Main_Unit (N) or else Might_Inline_Subp)
4069 and then not Is_Actual_Pack
4070 and then not Inline_Now
4071 and then (Operating_Mode = Generate_Code
4072
4073 -- Need comment for this check ???
4074
4075 or else (Operating_Mode = Check_Semantics
4076 and then (ASIS_Mode or GNATprove_Mode)));
4077
4078 -- If front-end inlining is enabled or there are any subprograms
4079 -- marked with Inline_Always, do not instantiate body when within
4080 -- a generic context.
4081
4082 if ((Front_End_Inlining or else Has_Inline_Always)
4083 and then not Expander_Active)
4084 or else Is_Generic_Unit (Cunit_Entity (Main_Unit))
4085 then
4086 Needs_Body := False;
4087 end if;
4088
4089 -- If the current context is generic, and the package being
4090 -- instantiated is declared within a formal package, there is no
4091 -- body to instantiate until the enclosing generic is instantiated
4092 -- and there is an actual for the formal package. If the formal
4093 -- package has parameters, we build a regular package instance for
4094 -- it, that precedes the original formal package declaration.
4095
4096 if In_Open_Scopes (Scope (Scope (Gen_Unit))) then
4097 declare
4098 Decl : constant Node_Id :=
4099 Original_Node
4100 (Unit_Declaration_Node (Scope (Gen_Unit)));
4101 begin
4102 if Nkind (Decl) = N_Formal_Package_Declaration
4103 or else (Nkind (Decl) = N_Package_Declaration
4104 and then Is_List_Member (Decl)
4105 and then Present (Next (Decl))
4106 and then
4107 Nkind (Next (Decl)) =
4108 N_Formal_Package_Declaration)
4109 then
4110 Needs_Body := False;
4111 end if;
4112 end;
4113 end if;
4114 end;
4115
4116 -- For RCI unit calling stubs, we omit the instance body if the
4117 -- instance is the RCI library unit itself.
4118
4119 -- However there is a special case for nested instances: in this case
4120 -- we do generate the instance body, as it might be required, e.g.
4121 -- because it provides stream attributes for some type used in the
4122 -- profile of a remote subprogram. This is consistent with 12.3(12),
4123 -- which indicates that the instance body occurs at the place of the
4124 -- instantiation, and thus is part of the RCI declaration, which is
4125 -- present on all client partitions (this is E.2.3(18)).
4126
4127 -- Note that AI12-0002 may make it illegal at some point to have
4128 -- stream attributes defined in an RCI unit, in which case this
4129 -- special case will become unnecessary. In the meantime, there
4130 -- is known application code in production that depends on this
4131 -- being possible, so we definitely cannot eliminate the body in
4132 -- the case of nested instances for the time being.
4133
4134 -- When we generate a nested instance body, calling stubs for any
4135 -- relevant subprogram will be be inserted immediately after the
4136 -- subprogram declarations, and will take precedence over the
4137 -- subsequent (original) body. (The stub and original body will be
4138 -- complete homographs, but this is permitted in an instance).
4139 -- (Could we do better and remove the original body???)
4140
4141 if Distribution_Stub_Mode = Generate_Caller_Stub_Body
4142 and then Comes_From_Source (N)
4143 and then Nkind (Parent (N)) = N_Compilation_Unit
4144 then
4145 Needs_Body := False;
4146 end if;
4147
4148 if Needs_Body then
4149
4150 -- Here is a defence against a ludicrous number of instantiations
4151 -- caused by a circular set of instantiation attempts.
4152
4153 if Pending_Instantiations.Last > Maximum_Instantiations then
4154 Error_Msg_Uint_1 := UI_From_Int (Maximum_Instantiations);
4155 Error_Msg_N ("too many instantiations, exceeds max of^", N);
4156 Error_Msg_N ("\limit can be changed using -gnateinn switch", N);
4157 raise Unrecoverable_Error;
4158 end if;
4159
4160 -- Indicate that the enclosing scopes contain an instantiation,
4161 -- and that cleanup actions should be delayed until after the
4162 -- instance body is expanded.
4163
4164 Check_Forward_Instantiation (Gen_Decl);
4165 if Nkind (N) = N_Package_Instantiation then
4166 declare
4167 Enclosing_Master : Entity_Id;
4168
4169 begin
4170 -- Loop to search enclosing masters
4171
4172 Enclosing_Master := Current_Scope;
4173 Scope_Loop : while Enclosing_Master /= Standard_Standard loop
4174 if Ekind (Enclosing_Master) = E_Package then
4175 if Is_Compilation_Unit (Enclosing_Master) then
4176 if In_Package_Body (Enclosing_Master) then
4177 Delay_Descriptors
4178 (Body_Entity (Enclosing_Master));
4179 else
4180 Delay_Descriptors
4181 (Enclosing_Master);
4182 end if;
4183
4184 exit Scope_Loop;
4185
4186 else
4187 Enclosing_Master := Scope (Enclosing_Master);
4188 end if;
4189
4190 elsif Is_Generic_Unit (Enclosing_Master)
4191 or else Ekind (Enclosing_Master) = E_Void
4192 then
4193 -- Cleanup actions will eventually be performed on the
4194 -- enclosing subprogram or package instance, if any.
4195 -- Enclosing scope is void in the formal part of a
4196 -- generic subprogram.
4197
4198 exit Scope_Loop;
4199
4200 else
4201 if Ekind (Enclosing_Master) = E_Entry
4202 and then
4203 Ekind (Scope (Enclosing_Master)) = E_Protected_Type
4204 then
4205 if not Expander_Active then
4206 exit Scope_Loop;
4207 else
4208 Enclosing_Master :=
4209 Protected_Body_Subprogram (Enclosing_Master);
4210 end if;
4211 end if;
4212
4213 Set_Delay_Cleanups (Enclosing_Master);
4214
4215 while Ekind (Enclosing_Master) = E_Block loop
4216 Enclosing_Master := Scope (Enclosing_Master);
4217 end loop;
4218
4219 if Is_Subprogram (Enclosing_Master) then
4220 Delay_Descriptors (Enclosing_Master);
4221
4222 elsif Is_Task_Type (Enclosing_Master) then
4223 declare
4224 TBP : constant Node_Id :=
4225 Get_Task_Body_Procedure
4226 (Enclosing_Master);
4227 begin
4228 if Present (TBP) then
4229 Delay_Descriptors (TBP);
4230 Set_Delay_Cleanups (TBP);
4231 end if;
4232 end;
4233 end if;
4234
4235 exit Scope_Loop;
4236 end if;
4237 end loop Scope_Loop;
4238 end;
4239
4240 -- Make entry in table
4241
4242 Add_Pending_Instantiation (N, Act_Decl);
4243 end if;
4244 end if;
4245
4246 Set_Categorization_From_Pragmas (Act_Decl);
4247
4248 if Parent_Installed then
4249 Hide_Current_Scope;
4250 end if;
4251
4252 Set_Instance_Spec (N, Act_Decl);
4253
4254 -- If not a compilation unit, insert the package declaration before
4255 -- the original instantiation node.
4256
4257 if Nkind (Parent (N)) /= N_Compilation_Unit then
4258 Mark_Rewrite_Insertion (Act_Decl);
4259 Insert_Before (N, Act_Decl);
4260
4261 if Has_Aspects (N) then
4262 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4263
4264 -- The pragma created for a Default_Storage_Pool aspect must
4265 -- appear ahead of the declarations in the instance spec.
4266 -- Analysis has placed it after the instance node, so remove
4267 -- it and reinsert it properly now.
4268
4269 declare
4270 ASN : constant Node_Id := First (Aspect_Specifications (N));
4271 A_Name : constant Name_Id := Chars (Identifier (ASN));
4272 Decl : Node_Id;
4273
4274 begin
4275 if A_Name = Name_Default_Storage_Pool then
4276 if No (Visible_Declarations (Act_Spec)) then
4277 Set_Visible_Declarations (Act_Spec, New_List);
4278 end if;
4279
4280 Decl := Next (N);
4281 while Present (Decl) loop
4282 if Nkind (Decl) = N_Pragma then
4283 Remove (Decl);
4284 Prepend (Decl, Visible_Declarations (Act_Spec));
4285 exit;
4286 end if;
4287
4288 Next (Decl);
4289 end loop;
4290 end if;
4291 end;
4292 end if;
4293
4294 Analyze (Act_Decl);
4295
4296 -- For an instantiation that is a compilation unit, place
4297 -- declaration on current node so context is complete for analysis
4298 -- (including nested instantiations). If this is the main unit,
4299 -- the declaration eventually replaces the instantiation node.
4300 -- If the instance body is created later, it replaces the
4301 -- instance node, and the declaration is attached to it
4302 -- (see Build_Instance_Compilation_Unit_Nodes).
4303
4304 else
4305 if Cunit_Entity (Current_Sem_Unit) = Defining_Entity (N) then
4306
4307 -- The entity for the current unit is the newly created one,
4308 -- and all semantic information is attached to it.
4309
4310 Set_Cunit_Entity (Current_Sem_Unit, Act_Decl_Id);
4311
4312 -- If this is the main unit, replace the main entity as well
4313
4314 if Current_Sem_Unit = Main_Unit then
4315 Main_Unit_Entity := Act_Decl_Id;
4316 end if;
4317 end if;
4318
4319 Set_Unit (Parent (N), Act_Decl);
4320 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
4321 Set_Package_Instantiation (Act_Decl_Id, N);
4322
4323 -- Process aspect specifications of the instance node, if any, to
4324 -- take into account categorization pragmas before analyzing the
4325 -- instance.
4326
4327 if Has_Aspects (N) then
4328 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4329 end if;
4330
4331 Analyze (Act_Decl);
4332 Set_Unit (Parent (N), N);
4333 Set_Body_Required (Parent (N), False);
4334
4335 -- We never need elaboration checks on instantiations, since by
4336 -- definition, the body instantiation is elaborated at the same
4337 -- time as the spec instantiation.
4338
4339 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
4340 Set_Kill_Elaboration_Checks (Act_Decl_Id);
4341 end if;
4342
4343 Check_Elab_Instantiation (N);
4344
4345 if ABE_Is_Certain (N) and then Needs_Body then
4346 Pending_Instantiations.Decrement_Last;
4347 end if;
4348
4349 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
4350
4351 Set_First_Private_Entity (Defining_Unit_Name (Unit_Renaming),
4352 First_Private_Entity (Act_Decl_Id));
4353
4354 -- If the instantiation will receive a body, the unit will be
4355 -- transformed into a package body, and receive its own elaboration
4356 -- entity. Otherwise, the nature of the unit is now a package
4357 -- declaration.
4358
4359 if Nkind (Parent (N)) = N_Compilation_Unit
4360 and then not Needs_Body
4361 then
4362 Rewrite (N, Act_Decl);
4363 end if;
4364
4365 if Present (Corresponding_Body (Gen_Decl))
4366 or else Unit_Requires_Body (Gen_Unit)
4367 then
4368 Set_Has_Completion (Act_Decl_Id);
4369 end if;
4370
4371 Check_Formal_Packages (Act_Decl_Id);
4372
4373 Restore_Hidden_Primitives (Vis_Prims_List);
4374 Restore_Private_Views (Act_Decl_Id);
4375
4376 Inherit_Context (Gen_Decl, N);
4377
4378 if Parent_Installed then
4379 Remove_Parent;
4380 end if;
4381
4382 Restore_Env;
4383 Env_Installed := False;
4384 end if;
4385
4386 Validate_Categorization_Dependency (N, Act_Decl_Id);
4387
4388 -- There used to be a check here to prevent instantiations in local
4389 -- contexts if the No_Local_Allocators restriction was active. This
4390 -- check was removed by a binding interpretation in AI-95-00130/07,
4391 -- but we retain the code for documentation purposes.
4392
4393 -- if Ekind (Act_Decl_Id) /= E_Void
4394 -- and then not Is_Library_Level_Entity (Act_Decl_Id)
4395 -- then
4396 -- Check_Restriction (No_Local_Allocators, N);
4397 -- end if;
4398
4399 if Inline_Now then
4400 Inline_Instance_Body (N, Gen_Unit, Act_Decl);
4401 end if;
4402
4403 -- The following is a tree patch for ASIS: ASIS needs separate nodes to
4404 -- be used as defining identifiers for a formal package and for the
4405 -- corresponding expanded package.
4406
4407 if Nkind (N) = N_Formal_Package_Declaration then
4408 Act_Decl_Id := New_Copy (Defining_Entity (N));
4409 Set_Comes_From_Source (Act_Decl_Id, True);
4410 Set_Is_Generic_Instance (Act_Decl_Id, False);
4411 Set_Defining_Identifier (N, Act_Decl_Id);
4412 end if;
4413
4414 Ignore_Pragma_SPARK_Mode := Save_IPSM;
4415 SPARK_Mode := Save_SM;
4416 SPARK_Mode_Pragma := Save_SMP;
4417 Style_Check := Save_Style_Check;
4418
4419 -- Check that if N is an instantiation of System.Dim_Float_IO or
4420 -- System.Dim_Integer_IO, the formal type has a dimension system.
4421
4422 if Nkind (N) = N_Package_Instantiation
4423 and then Is_Dim_IO_Package_Instantiation (N)
4424 then
4425 declare
4426 Assoc : constant Node_Id := First (Generic_Associations (N));
4427 begin
4428 if not Has_Dimension_System
4429 (Etype (Explicit_Generic_Actual_Parameter (Assoc)))
4430 then
4431 Error_Msg_N ("type with a dimension system expected", Assoc);
4432 end if;
4433 end;
4434 end if;
4435
4436 <<Leave>>
4437 if Has_Aspects (N) and then Nkind (Parent (N)) /= N_Compilation_Unit then
4438 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4439 end if;
4440
4441 Restore_Ghost_Mode (Mode);
4442
4443 exception
4444 when Instantiation_Error =>
4445 if Parent_Installed then
4446 Remove_Parent;
4447 end if;
4448
4449 if Env_Installed then
4450 Restore_Env;
4451 end if;
4452
4453 Ignore_Pragma_SPARK_Mode := Save_IPSM;
4454 SPARK_Mode := Save_SM;
4455 SPARK_Mode_Pragma := Save_SMP;
4456 Style_Check := Save_Style_Check;
4457
4458 Restore_Ghost_Mode (Mode);
4459 end Analyze_Package_Instantiation;
4460
4461 --------------------------
4462 -- Inline_Instance_Body --
4463 --------------------------
4464
4465 procedure Inline_Instance_Body
4466 (N : Node_Id;
4467 Gen_Unit : Entity_Id;
4468 Act_Decl : Node_Id)
4469 is
4470 Curr_Comp : constant Node_Id := Cunit (Current_Sem_Unit);
4471 Curr_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
4472 Gen_Comp : constant Entity_Id :=
4473 Cunit_Entity (Get_Source_Unit (Gen_Unit));
4474
4475 Save_SM : constant SPARK_Mode_Type := SPARK_Mode;
4476 Save_SMP : constant Node_Id := SPARK_Mode_Pragma;
4477 -- Save all SPARK_Mode-related attributes as removing enclosing scopes
4478 -- to provide a clean environment for analysis of the inlined body will
4479 -- eliminate any previously set SPARK_Mode.
4480
4481 Scope_Stack_Depth : constant Pos :=
4482 Scope_Stack.Last - Scope_Stack.First + 1;
4483
4484 Use_Clauses : array (1 .. Scope_Stack_Depth) of Node_Id;
4485 Instances : array (1 .. Scope_Stack_Depth) of Entity_Id;
4486 Inner_Scopes : array (1 .. Scope_Stack_Depth) of Entity_Id;
4487 Curr_Scope : Entity_Id := Empty;
4488 List : Elist_Id;
4489 Num_Inner : Nat := 0;
4490 Num_Scopes : Nat := 0;
4491 N_Instances : Nat := 0;
4492 Removed : Boolean := False;
4493 S : Entity_Id;
4494 Vis : Boolean;
4495
4496 begin
4497 -- Case of generic unit defined in another unit. We must remove the
4498 -- complete context of the current unit to install that of the generic.
4499
4500 if Gen_Comp /= Cunit_Entity (Current_Sem_Unit) then
4501
4502 -- Add some comments for the following two loops ???
4503
4504 S := Current_Scope;
4505 while Present (S) and then S /= Standard_Standard loop
4506 loop
4507 Num_Scopes := Num_Scopes + 1;
4508
4509 Use_Clauses (Num_Scopes) :=
4510 (Scope_Stack.Table
4511 (Scope_Stack.Last - Num_Scopes + 1).
4512 First_Use_Clause);
4513 End_Use_Clauses (Use_Clauses (Num_Scopes));
4514
4515 exit when Scope_Stack.Last - Num_Scopes + 1 = Scope_Stack.First
4516 or else Scope_Stack.Table
4517 (Scope_Stack.Last - Num_Scopes).Entity = Scope (S);
4518 end loop;
4519
4520 exit when Is_Generic_Instance (S)
4521 and then (In_Package_Body (S)
4522 or else Ekind (S) = E_Procedure
4523 or else Ekind (S) = E_Function);
4524 S := Scope (S);
4525 end loop;
4526
4527 Vis := Is_Immediately_Visible (Gen_Comp);
4528
4529 -- Find and save all enclosing instances
4530
4531 S := Current_Scope;
4532
4533 while Present (S)
4534 and then S /= Standard_Standard
4535 loop
4536 if Is_Generic_Instance (S) then
4537 N_Instances := N_Instances + 1;
4538 Instances (N_Instances) := S;
4539
4540 exit when In_Package_Body (S);
4541 end if;
4542
4543 S := Scope (S);
4544 end loop;
4545
4546 -- Remove context of current compilation unit, unless we are within a
4547 -- nested package instantiation, in which case the context has been
4548 -- removed previously.
4549
4550 -- If current scope is the body of a child unit, remove context of
4551 -- spec as well. If an enclosing scope is an instance body, the
4552 -- context has already been removed, but the entities in the body
4553 -- must be made invisible as well.
4554
4555 S := Current_Scope;
4556 while Present (S) and then S /= Standard_Standard loop
4557 if Is_Generic_Instance (S)
4558 and then (In_Package_Body (S)
4559 or else Ekind_In (S, E_Procedure, E_Function))
4560 then
4561 -- We still have to remove the entities of the enclosing
4562 -- instance from direct visibility.
4563
4564 declare
4565 E : Entity_Id;
4566 begin
4567 E := First_Entity (S);
4568 while Present (E) loop
4569 Set_Is_Immediately_Visible (E, False);
4570 Next_Entity (E);
4571 end loop;
4572 end;
4573
4574 exit;
4575 end if;
4576
4577 if S = Curr_Unit
4578 or else (Ekind (Curr_Unit) = E_Package_Body
4579 and then S = Spec_Entity (Curr_Unit))
4580 or else (Ekind (Curr_Unit) = E_Subprogram_Body
4581 and then S = Corresponding_Spec
4582 (Unit_Declaration_Node (Curr_Unit)))
4583 then
4584 Removed := True;
4585
4586 -- Remove entities in current scopes from visibility, so that
4587 -- instance body is compiled in a clean environment.
4588
4589 List := Save_Scope_Stack (Handle_Use => False);
4590
4591 if Is_Child_Unit (S) then
4592
4593 -- Remove child unit from stack, as well as inner scopes.
4594 -- Removing the context of a child unit removes parent units
4595 -- as well.
4596
4597 while Current_Scope /= S loop
4598 Num_Inner := Num_Inner + 1;
4599 Inner_Scopes (Num_Inner) := Current_Scope;
4600 Pop_Scope;
4601 end loop;
4602
4603 Pop_Scope;
4604 Remove_Context (Curr_Comp);
4605 Curr_Scope := S;
4606
4607 else
4608 Remove_Context (Curr_Comp);
4609 end if;
4610
4611 if Ekind (Curr_Unit) = E_Package_Body then
4612 Remove_Context (Library_Unit (Curr_Comp));
4613 end if;
4614 end if;
4615
4616 S := Scope (S);
4617 end loop;
4618
4619 pragma Assert (Num_Inner < Num_Scopes);
4620
4621 -- The inlined package body must be analyzed with the SPARK_Mode of
4622 -- the enclosing context, otherwise the body may cause bogus errors
4623 -- if a configuration SPARK_Mode pragma in in effect.
4624
4625 Push_Scope (Standard_Standard);
4626 Scope_Stack.Table (Scope_Stack.Last).Is_Active_Stack_Base := True;
4627 Instantiate_Package_Body
4628 (Body_Info =>
4629 ((Inst_Node => N,
4630 Act_Decl => Act_Decl,
4631 Expander_Status => Expander_Active,
4632 Current_Sem_Unit => Current_Sem_Unit,
4633 Scope_Suppress => Scope_Suppress,
4634 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
4635 Version => Ada_Version,
4636 Version_Pragma => Ada_Version_Pragma,
4637 Warnings => Save_Warnings,
4638 SPARK_Mode => Save_SM,
4639 SPARK_Mode_Pragma => Save_SMP)),
4640 Inlined_Body => True);
4641
4642 Pop_Scope;
4643
4644 -- Restore context
4645
4646 Set_Is_Immediately_Visible (Gen_Comp, Vis);
4647
4648 -- Reset Generic_Instance flag so that use clauses can be installed
4649 -- in the proper order. (See Use_One_Package for effect of enclosing
4650 -- instances on processing of use clauses).
4651
4652 for J in 1 .. N_Instances loop
4653 Set_Is_Generic_Instance (Instances (J), False);
4654 end loop;
4655
4656 if Removed then
4657 Install_Context (Curr_Comp);
4658
4659 if Present (Curr_Scope)
4660 and then Is_Child_Unit (Curr_Scope)
4661 then
4662 Push_Scope (Curr_Scope);
4663 Set_Is_Immediately_Visible (Curr_Scope);
4664
4665 -- Finally, restore inner scopes as well
4666
4667 for J in reverse 1 .. Num_Inner loop
4668 Push_Scope (Inner_Scopes (J));
4669 end loop;
4670 end if;
4671
4672 Restore_Scope_Stack (List, Handle_Use => False);
4673
4674 if Present (Curr_Scope)
4675 and then
4676 (In_Private_Part (Curr_Scope)
4677 or else In_Package_Body (Curr_Scope))
4678 then
4679 -- Install private declaration of ancestor units, which are
4680 -- currently available. Restore_Scope_Stack and Install_Context
4681 -- only install the visible part of parents.
4682
4683 declare
4684 Par : Entity_Id;
4685 begin
4686 Par := Scope (Curr_Scope);
4687 while (Present (Par)) and then Par /= Standard_Standard loop
4688 Install_Private_Declarations (Par);
4689 Par := Scope (Par);
4690 end loop;
4691 end;
4692 end if;
4693 end if;
4694
4695 -- Restore use clauses. For a child unit, use clauses in the parents
4696 -- are restored when installing the context, so only those in inner
4697 -- scopes (and those local to the child unit itself) need to be
4698 -- installed explicitly.
4699
4700 if Is_Child_Unit (Curr_Unit) and then Removed then
4701 for J in reverse 1 .. Num_Inner + 1 loop
4702 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
4703 Use_Clauses (J);
4704 Install_Use_Clauses (Use_Clauses (J));
4705 end loop;
4706
4707 else
4708 for J in reverse 1 .. Num_Scopes loop
4709 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
4710 Use_Clauses (J);
4711 Install_Use_Clauses (Use_Clauses (J));
4712 end loop;
4713 end if;
4714
4715 -- Restore status of instances. If one of them is a body, make its
4716 -- local entities visible again.
4717
4718 declare
4719 E : Entity_Id;
4720 Inst : Entity_Id;
4721
4722 begin
4723 for J in 1 .. N_Instances loop
4724 Inst := Instances (J);
4725 Set_Is_Generic_Instance (Inst, True);
4726
4727 if In_Package_Body (Inst)
4728 or else Ekind_In (S, E_Procedure, E_Function)
4729 then
4730 E := First_Entity (Instances (J));
4731 while Present (E) loop
4732 Set_Is_Immediately_Visible (E);
4733 Next_Entity (E);
4734 end loop;
4735 end if;
4736 end loop;
4737 end;
4738
4739 -- If generic unit is in current unit, current context is correct. Note
4740 -- that the context is guaranteed to carry the correct SPARK_Mode as no
4741 -- enclosing scopes were removed.
4742
4743 else
4744 Instantiate_Package_Body
4745 (Body_Info =>
4746 ((Inst_Node => N,
4747 Act_Decl => Act_Decl,
4748 Expander_Status => Expander_Active,
4749 Current_Sem_Unit => Current_Sem_Unit,
4750 Scope_Suppress => Scope_Suppress,
4751 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
4752 Version => Ada_Version,
4753 Version_Pragma => Ada_Version_Pragma,
4754 Warnings => Save_Warnings,
4755 SPARK_Mode => SPARK_Mode,
4756 SPARK_Mode_Pragma => SPARK_Mode_Pragma)),
4757 Inlined_Body => True);
4758 end if;
4759 end Inline_Instance_Body;
4760
4761 -------------------------------------
4762 -- Analyze_Procedure_Instantiation --
4763 -------------------------------------
4764
4765 procedure Analyze_Procedure_Instantiation (N : Node_Id) is
4766 begin
4767 Analyze_Subprogram_Instantiation (N, E_Procedure);
4768 end Analyze_Procedure_Instantiation;
4769
4770 -----------------------------------
4771 -- Need_Subprogram_Instance_Body --
4772 -----------------------------------
4773
4774 function Need_Subprogram_Instance_Body
4775 (N : Node_Id;
4776 Subp : Entity_Id) return Boolean
4777 is
4778
4779 function Is_Inlined_Or_Child_Of_Inlined (E : Entity_Id) return Boolean;
4780 -- Return True if E is an inlined subprogram, an inlined renaming or a
4781 -- subprogram nested in an inlined subprogram. The inlining machinery
4782 -- totally disregards nested subprograms since it considers that they
4783 -- will always be compiled if the parent is (see Inline.Is_Nested).
4784
4785 ------------------------------------
4786 -- Is_Inlined_Or_Child_Of_Inlined --
4787 ------------------------------------
4788
4789 function Is_Inlined_Or_Child_Of_Inlined (E : Entity_Id) return Boolean is
4790 Scop : Entity_Id;
4791
4792 begin
4793 if Is_Inlined (E) or else Is_Inlined (Alias (E)) then
4794 return True;
4795 end if;
4796
4797 Scop := Scope (E);
4798 while Scop /= Standard_Standard loop
4799 if Ekind (Scop) in Subprogram_Kind and then Is_Inlined (Scop) then
4800 return True;
4801 end if;
4802
4803 Scop := Scope (Scop);
4804 end loop;
4805
4806 return False;
4807 end Is_Inlined_Or_Child_Of_Inlined;
4808
4809 begin
4810 -- Must be in the main unit or inlined (or child of inlined)
4811
4812 if (Is_In_Main_Unit (N) or else Is_Inlined_Or_Child_Of_Inlined (Subp))
4813
4814 -- Must be generating code or analyzing code in ASIS/GNATprove mode
4815
4816 and then (Operating_Mode = Generate_Code
4817 or else (Operating_Mode = Check_Semantics
4818 and then (ASIS_Mode or GNATprove_Mode)))
4819
4820 -- The body is needed when generating code (full expansion), in ASIS
4821 -- mode for other tools, and in GNATprove mode (special expansion) for
4822 -- formal verification of the body itself.
4823
4824 and then (Expander_Active or ASIS_Mode or GNATprove_Mode)
4825
4826 -- No point in inlining if ABE is inevitable
4827
4828 and then not ABE_Is_Certain (N)
4829
4830 -- Or if subprogram is eliminated
4831
4832 and then not Is_Eliminated (Subp)
4833 then
4834 Add_Pending_Instantiation (N, Unit_Declaration_Node (Subp));
4835 return True;
4836
4837 -- Here if not inlined, or we ignore the inlining
4838
4839 else
4840 return False;
4841 end if;
4842 end Need_Subprogram_Instance_Body;
4843
4844 --------------------------------------
4845 -- Analyze_Subprogram_Instantiation --
4846 --------------------------------------
4847
4848 -- WARNING: This routine manages Ghost regions. Return statements must be
4849 -- replaced by gotos which jump to the end of the routine and restore the
4850 -- Ghost mode.
4851
4852 procedure Analyze_Subprogram_Instantiation
4853 (N : Node_Id;
4854 K : Entity_Kind)
4855 is
4856 Loc : constant Source_Ptr := Sloc (N);
4857 Gen_Id : constant Node_Id := Name (N);
4858
4859 Anon_Id : constant Entity_Id :=
4860 Make_Defining_Identifier (Sloc (Defining_Entity (N)),
4861 Chars => New_External_Name
4862 (Chars (Defining_Entity (N)), 'R'));
4863
4864 Act_Decl_Id : Entity_Id;
4865 Act_Decl : Node_Id;
4866 Act_Spec : Node_Id;
4867 Act_Tree : Node_Id;
4868
4869 Env_Installed : Boolean := False;
4870 Gen_Unit : Entity_Id;
4871 Gen_Decl : Node_Id;
4872 Pack_Id : Entity_Id;
4873 Parent_Installed : Boolean := False;
4874
4875 Renaming_List : List_Id;
4876 -- The list of declarations that link formals and actuals of the
4877 -- instance. These are subtype declarations for formal types, and
4878 -- renaming declarations for other formals. The subprogram declaration
4879 -- for the instance is then appended to the list, and the last item on
4880 -- the list is the renaming declaration for the instance.
4881
4882 procedure Analyze_Instance_And_Renamings;
4883 -- The instance must be analyzed in a context that includes the mappings
4884 -- of generic parameters into actuals. We create a package declaration
4885 -- for this purpose, and a subprogram with an internal name within the
4886 -- package. The subprogram instance is simply an alias for the internal
4887 -- subprogram, declared in the current scope.
4888
4889 procedure Build_Subprogram_Renaming;
4890 -- If the subprogram is recursive, there are occurrences of the name of
4891 -- the generic within the body, which must resolve to the current
4892 -- instance. We add a renaming declaration after the declaration, which
4893 -- is available in the instance body, as well as in the analysis of
4894 -- aspects that appear in the generic. This renaming declaration is
4895 -- inserted after the instance declaration which it renames.
4896
4897 ------------------------------------
4898 -- Analyze_Instance_And_Renamings --
4899 ------------------------------------
4900
4901 procedure Analyze_Instance_And_Renamings is
4902 Def_Ent : constant Entity_Id := Defining_Entity (N);
4903 Pack_Decl : Node_Id;
4904
4905 begin
4906 if Nkind (Parent (N)) = N_Compilation_Unit then
4907
4908 -- For the case of a compilation unit, the container package has
4909 -- the same name as the instantiation, to insure that the binder
4910 -- calls the elaboration procedure with the right name. Copy the
4911 -- entity of the instance, which may have compilation level flags
4912 -- (e.g. Is_Child_Unit) set.
4913
4914 Pack_Id := New_Copy (Def_Ent);
4915
4916 else
4917 -- Otherwise we use the name of the instantiation concatenated
4918 -- with its source position to ensure uniqueness if there are
4919 -- several instantiations with the same name.
4920
4921 Pack_Id :=
4922 Make_Defining_Identifier (Loc,
4923 Chars => New_External_Name
4924 (Related_Id => Chars (Def_Ent),
4925 Suffix => "GP",
4926 Suffix_Index => Source_Offset (Sloc (Def_Ent))));
4927 end if;
4928
4929 Pack_Decl :=
4930 Make_Package_Declaration (Loc,
4931 Specification => Make_Package_Specification (Loc,
4932 Defining_Unit_Name => Pack_Id,
4933 Visible_Declarations => Renaming_List,
4934 End_Label => Empty));
4935
4936 Set_Instance_Spec (N, Pack_Decl);
4937 Set_Is_Generic_Instance (Pack_Id);
4938 Set_Debug_Info_Needed (Pack_Id);
4939
4940 -- Case of not a compilation unit
4941
4942 if Nkind (Parent (N)) /= N_Compilation_Unit then
4943 Mark_Rewrite_Insertion (Pack_Decl);
4944 Insert_Before (N, Pack_Decl);
4945 Set_Has_Completion (Pack_Id);
4946
4947 -- Case of an instantiation that is a compilation unit
4948
4949 -- Place declaration on current node so context is complete for
4950 -- analysis (including nested instantiations), and for use in a
4951 -- context_clause (see Analyze_With_Clause).
4952
4953 else
4954 Set_Unit (Parent (N), Pack_Decl);
4955 Set_Parent_Spec (Pack_Decl, Parent_Spec (N));
4956 end if;
4957
4958 Analyze (Pack_Decl);
4959 Check_Formal_Packages (Pack_Id);
4960 Set_Is_Generic_Instance (Pack_Id, False);
4961
4962 -- Why do we clear Is_Generic_Instance??? We set it 20 lines
4963 -- above???
4964
4965 -- Body of the enclosing package is supplied when instantiating the
4966 -- subprogram body, after semantic analysis is completed.
4967
4968 if Nkind (Parent (N)) = N_Compilation_Unit then
4969
4970 -- Remove package itself from visibility, so it does not
4971 -- conflict with subprogram.
4972
4973 Set_Name_Entity_Id (Chars (Pack_Id), Homonym (Pack_Id));
4974
4975 -- Set name and scope of internal subprogram so that the proper
4976 -- external name will be generated. The proper scope is the scope
4977 -- of the wrapper package. We need to generate debugging info for
4978 -- the internal subprogram, so set flag accordingly.
4979
4980 Set_Chars (Anon_Id, Chars (Defining_Entity (N)));
4981 Set_Scope (Anon_Id, Scope (Pack_Id));
4982
4983 -- Mark wrapper package as referenced, to avoid spurious warnings
4984 -- if the instantiation appears in various with_ clauses of
4985 -- subunits of the main unit.
4986
4987 Set_Referenced (Pack_Id);
4988 end if;
4989
4990 Set_Is_Generic_Instance (Anon_Id);
4991 Set_Debug_Info_Needed (Anon_Id);
4992 Act_Decl_Id := New_Copy (Anon_Id);
4993
4994 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
4995 Set_Chars (Act_Decl_Id, Chars (Defining_Entity (N)));
4996 Set_Sloc (Act_Decl_Id, Sloc (Defining_Entity (N)));
4997
4998 -- Subprogram instance comes from source only if generic does
4999
5000 Set_Comes_From_Source (Act_Decl_Id, Comes_From_Source (Gen_Unit));
5001
5002 -- If the instance is a child unit, mark the Id accordingly. Mark
5003 -- the anonymous entity as well, which is the real subprogram and
5004 -- which is used when the instance appears in a context clause.
5005 -- Similarly, propagate the Is_Eliminated flag to handle properly
5006 -- nested eliminated subprograms.
5007
5008 Set_Is_Child_Unit (Act_Decl_Id, Is_Child_Unit (Defining_Entity (N)));
5009 Set_Is_Child_Unit (Anon_Id, Is_Child_Unit (Defining_Entity (N)));
5010 New_Overloaded_Entity (Act_Decl_Id);
5011 Check_Eliminated (Act_Decl_Id);
5012 Set_Is_Eliminated (Anon_Id, Is_Eliminated (Act_Decl_Id));
5013
5014 -- In compilation unit case, kill elaboration checks on the
5015 -- instantiation, since they are never needed -- the body is
5016 -- instantiated at the same point as the spec.
5017
5018 if Nkind (Parent (N)) = N_Compilation_Unit then
5019 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
5020 Set_Kill_Elaboration_Checks (Act_Decl_Id);
5021 Set_Is_Compilation_Unit (Anon_Id);
5022
5023 Set_Cunit_Entity (Current_Sem_Unit, Pack_Id);
5024 end if;
5025
5026 -- The instance is not a freezing point for the new subprogram.
5027 -- The anonymous subprogram may have a freeze node, created for
5028 -- some delayed aspects. This freeze node must not be inherited
5029 -- by the visible subprogram entity.
5030
5031 Set_Is_Frozen (Act_Decl_Id, False);
5032 Set_Freeze_Node (Act_Decl_Id, Empty);
5033
5034 if Nkind (Defining_Entity (N)) = N_Defining_Operator_Symbol then
5035 Valid_Operator_Definition (Act_Decl_Id);
5036 end if;
5037
5038 Set_Alias (Act_Decl_Id, Anon_Id);
5039 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
5040 Set_Has_Completion (Act_Decl_Id);
5041 Set_Related_Instance (Pack_Id, Act_Decl_Id);
5042
5043 if Nkind (Parent (N)) = N_Compilation_Unit then
5044 Set_Body_Required (Parent (N), False);
5045 end if;
5046 end Analyze_Instance_And_Renamings;
5047
5048 -------------------------------
5049 -- Build_Subprogram_Renaming --
5050 -------------------------------
5051
5052 procedure Build_Subprogram_Renaming is
5053 Renaming_Decl : Node_Id;
5054 Unit_Renaming : Node_Id;
5055
5056 begin
5057 Unit_Renaming :=
5058 Make_Subprogram_Renaming_Declaration (Loc,
5059 Specification =>
5060 Copy_Generic_Node
5061 (Specification (Original_Node (Gen_Decl)),
5062 Empty,
5063 Instantiating => True),
5064 Name => New_Occurrence_Of (Anon_Id, Loc));
5065
5066 -- The generic may be a a child unit. The renaming needs an
5067 -- identifier with the proper name.
5068
5069 Set_Defining_Unit_Name (Specification (Unit_Renaming),
5070 Make_Defining_Identifier (Loc, Chars (Gen_Unit)));
5071
5072 -- If there is a formal subprogram with the same name as the unit
5073 -- itself, do not add this renaming declaration, to prevent
5074 -- ambiguities when there is a call with that name in the body.
5075 -- This is a partial and ugly fix for one ACATS test. ???
5076
5077 Renaming_Decl := First (Renaming_List);
5078 while Present (Renaming_Decl) loop
5079 if Nkind (Renaming_Decl) = N_Subprogram_Renaming_Declaration
5080 and then
5081 Chars (Defining_Entity (Renaming_Decl)) = Chars (Gen_Unit)
5082 then
5083 exit;
5084 end if;
5085
5086 Next (Renaming_Decl);
5087 end loop;
5088
5089 if No (Renaming_Decl) then
5090 Append (Unit_Renaming, Renaming_List);
5091 end if;
5092 end Build_Subprogram_Renaming;
5093
5094 -- Local variables
5095
5096 Mode : Ghost_Mode_Type;
5097
5098 Save_IPSM : constant Boolean := Ignore_Pragma_SPARK_Mode;
5099 -- Save flag Ignore_Pragma_SPARK_Mode for restore on exit
5100
5101 Save_SM : constant SPARK_Mode_Type := SPARK_Mode;
5102 Save_SMP : constant Node_Id := SPARK_Mode_Pragma;
5103 -- Save the SPARK_Mode-related data for restore on exit
5104
5105 Vis_Prims_List : Elist_Id := No_Elist;
5106 -- List of primitives made temporarily visible in the instantiation
5107 -- to match the visibility of the formal type
5108
5109 -- Start of processing for Analyze_Subprogram_Instantiation
5110
5111 begin
5112 Check_SPARK_05_Restriction ("generic is not allowed", N);
5113
5114 -- Very first thing: check for special Text_IO unit in case we are
5115 -- instantiating one of the children of [[Wide_]Wide_]Text_IO. Of course
5116 -- such an instantiation is bogus (these are packages, not subprograms),
5117 -- but we get a better error message if we do this.
5118
5119 Check_Text_IO_Special_Unit (Gen_Id);
5120
5121 -- Make node global for error reporting
5122
5123 Instantiation_Node := N;
5124
5125 -- For package instantiations we turn off style checks, because they
5126 -- will have been emitted in the generic. For subprogram instantiations
5127 -- we want to apply at least the check on overriding indicators so we
5128 -- do not modify the style check status.
5129
5130 -- The renaming declarations for the actuals do not come from source and
5131 -- will not generate spurious warnings.
5132
5133 Preanalyze_Actuals (N);
5134
5135 Init_Env;
5136 Env_Installed := True;
5137 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
5138 Gen_Unit := Entity (Gen_Id);
5139
5140 -- A subprogram instantiation is Ghost when it is subject to pragma
5141 -- Ghost or the generic template is Ghost. Set the mode now to ensure
5142 -- that any nodes generated during analysis and expansion are marked as
5143 -- Ghost.
5144
5145 Mark_And_Set_Ghost_Instantiation (N, Gen_Unit, Mode);
5146
5147 Generate_Reference (Gen_Unit, Gen_Id);
5148
5149 if Nkind (Gen_Id) = N_Identifier
5150 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
5151 then
5152 Error_Msg_NE
5153 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
5154 end if;
5155
5156 if Etype (Gen_Unit) = Any_Type then
5157 Restore_Env;
5158 goto Leave;
5159 end if;
5160
5161 -- Verify that it is a generic subprogram of the right kind, and that
5162 -- it does not lead to a circular instantiation.
5163
5164 if K = E_Procedure and then Ekind (Gen_Unit) /= E_Generic_Procedure then
5165 Error_Msg_NE
5166 ("& is not the name of a generic procedure", Gen_Id, Gen_Unit);
5167
5168 elsif K = E_Function and then Ekind (Gen_Unit) /= E_Generic_Function then
5169 Error_Msg_NE
5170 ("& is not the name of a generic function", Gen_Id, Gen_Unit);
5171
5172 elsif In_Open_Scopes (Gen_Unit) then
5173 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
5174
5175 else
5176 -- If the context of the instance is subject to SPARK_Mode "off" or
5177 -- the annotation is altogether missing, set the global flag which
5178 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
5179 -- the instance.
5180
5181 if SPARK_Mode /= On then
5182 Ignore_Pragma_SPARK_Mode := True;
5183 end if;
5184
5185 Set_Entity (Gen_Id, Gen_Unit);
5186 Set_Is_Instantiated (Gen_Unit);
5187
5188 if In_Extended_Main_Source_Unit (N) then
5189 Generate_Reference (Gen_Unit, N);
5190 end if;
5191
5192 -- If renaming, get original unit
5193
5194 if Present (Renamed_Object (Gen_Unit))
5195 and then Ekind_In (Renamed_Object (Gen_Unit), E_Generic_Procedure,
5196 E_Generic_Function)
5197 then
5198 Gen_Unit := Renamed_Object (Gen_Unit);
5199 Set_Is_Instantiated (Gen_Unit);
5200 Generate_Reference (Gen_Unit, N);
5201 end if;
5202
5203 if Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
5204 Error_Msg_Node_2 := Current_Scope;
5205 Error_Msg_NE
5206 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
5207 Circularity_Detected := True;
5208 Restore_Hidden_Primitives (Vis_Prims_List);
5209 goto Leave;
5210 end if;
5211
5212 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
5213
5214 -- Initialize renamings map, for error checking
5215
5216 Generic_Renamings.Set_Last (0);
5217 Generic_Renamings_HTable.Reset;
5218
5219 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
5220
5221 -- Copy original generic tree, to produce text for instantiation
5222
5223 Act_Tree :=
5224 Copy_Generic_Node
5225 (Original_Node (Gen_Decl), Empty, Instantiating => True);
5226
5227 -- Inherit overriding indicator from instance node
5228
5229 Act_Spec := Specification (Act_Tree);
5230 Set_Must_Override (Act_Spec, Must_Override (N));
5231 Set_Must_Not_Override (Act_Spec, Must_Not_Override (N));
5232
5233 Renaming_List :=
5234 Analyze_Associations
5235 (I_Node => N,
5236 Formals => Generic_Formal_Declarations (Act_Tree),
5237 F_Copy => Generic_Formal_Declarations (Gen_Decl));
5238
5239 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
5240
5241 -- The subprogram itself cannot contain a nested instance, so the
5242 -- current parent is left empty.
5243
5244 Set_Instance_Env (Gen_Unit, Empty);
5245
5246 -- Build the subprogram declaration, which does not appear in the
5247 -- generic template, and give it a sloc consistent with that of the
5248 -- template.
5249
5250 Set_Defining_Unit_Name (Act_Spec, Anon_Id);
5251 Set_Generic_Parent (Act_Spec, Gen_Unit);
5252 Act_Decl :=
5253 Make_Subprogram_Declaration (Sloc (Act_Spec),
5254 Specification => Act_Spec);
5255
5256 -- The aspects have been copied previously, but they have to be
5257 -- linked explicitly to the new subprogram declaration. Explicit
5258 -- pre/postconditions on the instance are analyzed below, in a
5259 -- separate step.
5260
5261 Move_Aspects (Act_Tree, To => Act_Decl);
5262 Set_Categorization_From_Pragmas (Act_Decl);
5263
5264 if Parent_Installed then
5265 Hide_Current_Scope;
5266 end if;
5267
5268 Append (Act_Decl, Renaming_List);
5269
5270 -- Contract-related source pragmas that follow a generic subprogram
5271 -- must be instantiated explicitly because they are not part of the
5272 -- subprogram template.
5273
5274 Instantiate_Subprogram_Contract
5275 (Original_Node (Gen_Decl), Renaming_List);
5276
5277 Build_Subprogram_Renaming;
5278 Analyze_Instance_And_Renamings;
5279
5280 -- If the generic is marked Import (Intrinsic), then so is the
5281 -- instance. This indicates that there is no body to instantiate. If
5282 -- generic is marked inline, so it the instance, and the anonymous
5283 -- subprogram it renames. If inlined, or else if inlining is enabled
5284 -- for the compilation, we generate the instance body even if it is
5285 -- not within the main unit.
5286
5287 if Is_Intrinsic_Subprogram (Gen_Unit) then
5288 Set_Is_Intrinsic_Subprogram (Anon_Id);
5289 Set_Is_Intrinsic_Subprogram (Act_Decl_Id);
5290
5291 if Chars (Gen_Unit) = Name_Unchecked_Conversion then
5292 Validate_Unchecked_Conversion (N, Act_Decl_Id);
5293 end if;
5294 end if;
5295
5296 -- Inherit convention from generic unit. Intrinsic convention, as for
5297 -- an instance of unchecked conversion, is not inherited because an
5298 -- explicit Ada instance has been created.
5299
5300 if Has_Convention_Pragma (Gen_Unit)
5301 and then Convention (Gen_Unit) /= Convention_Intrinsic
5302 then
5303 Set_Convention (Act_Decl_Id, Convention (Gen_Unit));
5304 Set_Is_Exported (Act_Decl_Id, Is_Exported (Gen_Unit));
5305 end if;
5306
5307 Generate_Definition (Act_Decl_Id);
5308
5309 -- Inherit all inlining-related flags which apply to the generic in
5310 -- the subprogram and its declaration.
5311
5312 Set_Is_Inlined (Act_Decl_Id, Is_Inlined (Gen_Unit));
5313 Set_Is_Inlined (Anon_Id, Is_Inlined (Gen_Unit));
5314
5315 Set_Has_Pragma_Inline (Act_Decl_Id, Has_Pragma_Inline (Gen_Unit));
5316 Set_Has_Pragma_Inline (Anon_Id, Has_Pragma_Inline (Gen_Unit));
5317
5318 Set_Has_Pragma_Inline_Always
5319 (Act_Decl_Id, Has_Pragma_Inline_Always (Gen_Unit));
5320 Set_Has_Pragma_Inline_Always
5321 (Anon_Id, Has_Pragma_Inline_Always (Gen_Unit));
5322
5323 if not Is_Intrinsic_Subprogram (Gen_Unit) then
5324 Check_Elab_Instantiation (N);
5325 end if;
5326
5327 if Is_Dispatching_Operation (Act_Decl_Id)
5328 and then Ada_Version >= Ada_2005
5329 then
5330 declare
5331 Formal : Entity_Id;
5332
5333 begin
5334 Formal := First_Formal (Act_Decl_Id);
5335 while Present (Formal) loop
5336 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type
5337 and then Is_Controlling_Formal (Formal)
5338 and then not Can_Never_Be_Null (Formal)
5339 then
5340 Error_Msg_NE
5341 ("access parameter& is controlling,", N, Formal);
5342 Error_Msg_NE
5343 ("\corresponding parameter of & must be explicitly "
5344 & "null-excluding", N, Gen_Id);
5345 end if;
5346
5347 Next_Formal (Formal);
5348 end loop;
5349 end;
5350 end if;
5351
5352 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
5353
5354 Validate_Categorization_Dependency (N, Act_Decl_Id);
5355
5356 if not Is_Intrinsic_Subprogram (Act_Decl_Id) then
5357 Inherit_Context (Gen_Decl, N);
5358
5359 Restore_Private_Views (Pack_Id, False);
5360
5361 -- If the context requires a full instantiation, mark node for
5362 -- subsequent construction of the body.
5363
5364 if Need_Subprogram_Instance_Body (N, Act_Decl_Id) then
5365 Check_Forward_Instantiation (Gen_Decl);
5366
5367 -- The wrapper package is always delayed, because it does not
5368 -- constitute a freeze point, but to insure that the freeze node
5369 -- is placed properly, it is created directly when instantiating
5370 -- the body (otherwise the freeze node might appear to early for
5371 -- nested instantiations). For ASIS purposes, indicate that the
5372 -- wrapper package has replaced the instantiation node.
5373
5374 elsif Nkind (Parent (N)) = N_Compilation_Unit then
5375 Rewrite (N, Unit (Parent (N)));
5376 Set_Unit (Parent (N), N);
5377 end if;
5378
5379 -- Replace instance node for library-level instantiations of
5380 -- intrinsic subprograms, for ASIS use.
5381
5382 elsif Nkind (Parent (N)) = N_Compilation_Unit then
5383 Rewrite (N, Unit (Parent (N)));
5384 Set_Unit (Parent (N), N);
5385 end if;
5386
5387 if Parent_Installed then
5388 Remove_Parent;
5389 end if;
5390
5391 Restore_Hidden_Primitives (Vis_Prims_List);
5392 Restore_Env;
5393 Env_Installed := False;
5394 Generic_Renamings.Set_Last (0);
5395 Generic_Renamings_HTable.Reset;
5396
5397 Ignore_Pragma_SPARK_Mode := Save_IPSM;
5398 SPARK_Mode := Save_SM;
5399 SPARK_Mode_Pragma := Save_SMP;
5400 end if;
5401
5402 <<Leave>>
5403 if Has_Aspects (N) then
5404 Analyze_Aspect_Specifications (N, Act_Decl_Id);
5405 end if;
5406
5407 Restore_Ghost_Mode (Mode);
5408
5409 exception
5410 when Instantiation_Error =>
5411 if Parent_Installed then
5412 Remove_Parent;
5413 end if;
5414
5415 if Env_Installed then
5416 Restore_Env;
5417 end if;
5418
5419 Ignore_Pragma_SPARK_Mode := Save_IPSM;
5420 SPARK_Mode := Save_SM;
5421 SPARK_Mode_Pragma := Save_SMP;
5422
5423 Restore_Ghost_Mode (Mode);
5424 end Analyze_Subprogram_Instantiation;
5425
5426 -------------------------
5427 -- Get_Associated_Node --
5428 -------------------------
5429
5430 function Get_Associated_Node (N : Node_Id) return Node_Id is
5431 Assoc : Node_Id;
5432
5433 begin
5434 Assoc := Associated_Node (N);
5435
5436 if Nkind (Assoc) /= Nkind (N) then
5437 return Assoc;
5438
5439 elsif Nkind_In (Assoc, N_Aggregate, N_Extension_Aggregate) then
5440 return Assoc;
5441
5442 else
5443 -- If the node is part of an inner generic, it may itself have been
5444 -- remapped into a further generic copy. Associated_Node is otherwise
5445 -- used for the entity of the node, and will be of a different node
5446 -- kind, or else N has been rewritten as a literal or function call.
5447
5448 while Present (Associated_Node (Assoc))
5449 and then Nkind (Associated_Node (Assoc)) = Nkind (Assoc)
5450 loop
5451 Assoc := Associated_Node (Assoc);
5452 end loop;
5453
5454 -- Follow and additional link in case the final node was rewritten.
5455 -- This can only happen with nested generic units.
5456
5457 if (Nkind (Assoc) = N_Identifier or else Nkind (Assoc) in N_Op)
5458 and then Present (Associated_Node (Assoc))
5459 and then (Nkind_In (Associated_Node (Assoc), N_Function_Call,
5460 N_Explicit_Dereference,
5461 N_Integer_Literal,
5462 N_Real_Literal,
5463 N_String_Literal))
5464 then
5465 Assoc := Associated_Node (Assoc);
5466 end if;
5467
5468 -- An additional special case: an unconstrained type in an object
5469 -- declaration may have been rewritten as a local subtype constrained
5470 -- by the expression in the declaration. We need to recover the
5471 -- original entity which may be global.
5472
5473 if Present (Original_Node (Assoc))
5474 and then Nkind (Parent (N)) = N_Object_Declaration
5475 then
5476 Assoc := Original_Node (Assoc);
5477 end if;
5478
5479 return Assoc;
5480 end if;
5481 end Get_Associated_Node;
5482
5483 ----------------------------
5484 -- Build_Function_Wrapper --
5485 ----------------------------
5486
5487 function Build_Function_Wrapper
5488 (Formal_Subp : Entity_Id;
5489 Actual_Subp : Entity_Id) return Node_Id
5490 is
5491 Loc : constant Source_Ptr := Sloc (Current_Scope);
5492 Ret_Type : constant Entity_Id := Get_Instance_Of (Etype (Formal_Subp));
5493 Actuals : List_Id;
5494 Decl : Node_Id;
5495 Func_Name : Node_Id;
5496 Func : Entity_Id;
5497 Parm_Type : Node_Id;
5498 Profile : List_Id := New_List;
5499 Spec : Node_Id;
5500 Act_F : Entity_Id;
5501 Form_F : Entity_Id;
5502 New_F : Entity_Id;
5503
5504 begin
5505 Func_Name := New_Occurrence_Of (Actual_Subp, Loc);
5506
5507 Func := Make_Defining_Identifier (Loc, Chars (Formal_Subp));
5508 Set_Ekind (Func, E_Function);
5509 Set_Is_Generic_Actual_Subprogram (Func);
5510
5511 Actuals := New_List;
5512 Profile := New_List;
5513
5514 Act_F := First_Formal (Actual_Subp);
5515 Form_F := First_Formal (Formal_Subp);
5516 while Present (Form_F) loop
5517
5518 -- Create new formal for profile of wrapper, and add a reference
5519 -- to it in the list of actuals for the enclosing call. The name
5520 -- must be that of the formal in the formal subprogram, because
5521 -- calls to it in the generic body may use named associations.
5522
5523 New_F := Make_Defining_Identifier (Loc, Chars (Form_F));
5524
5525 Parm_Type :=
5526 New_Occurrence_Of (Get_Instance_Of (Etype (Form_F)), Loc);
5527
5528 Append_To (Profile,
5529 Make_Parameter_Specification (Loc,
5530 Defining_Identifier => New_F,
5531 Parameter_Type => Parm_Type));
5532
5533 Append_To (Actuals, New_Occurrence_Of (New_F, Loc));
5534 Next_Formal (Form_F);
5535
5536 if Present (Act_F) then
5537 Next_Formal (Act_F);
5538 end if;
5539 end loop;
5540
5541 Spec :=
5542 Make_Function_Specification (Loc,
5543 Defining_Unit_Name => Func,
5544 Parameter_Specifications => Profile,
5545 Result_Definition => New_Occurrence_Of (Ret_Type, Loc));
5546
5547 Decl :=
5548 Make_Expression_Function (Loc,
5549 Specification => Spec,
5550 Expression =>
5551 Make_Function_Call (Loc,
5552 Name => Func_Name,
5553 Parameter_Associations => Actuals));
5554
5555 return Decl;
5556 end Build_Function_Wrapper;
5557
5558 ----------------------------
5559 -- Build_Operator_Wrapper --
5560 ----------------------------
5561
5562 function Build_Operator_Wrapper
5563 (Formal_Subp : Entity_Id;
5564 Actual_Subp : Entity_Id) return Node_Id
5565 is
5566 Loc : constant Source_Ptr := Sloc (Current_Scope);
5567 Ret_Type : constant Entity_Id :=
5568 Get_Instance_Of (Etype (Formal_Subp));
5569 Op_Type : constant Entity_Id :=
5570 Get_Instance_Of (Etype (First_Formal (Formal_Subp)));
5571 Is_Binary : constant Boolean :=
5572 Present (Next_Formal (First_Formal (Formal_Subp)));
5573
5574 Decl : Node_Id;
5575 Expr : Node_Id;
5576 F1, F2 : Entity_Id;
5577 Func : Entity_Id;
5578 Op_Name : Name_Id;
5579 Spec : Node_Id;
5580 L, R : Node_Id;
5581
5582 begin
5583 Op_Name := Chars (Actual_Subp);
5584
5585 -- Create entities for wrapper function and its formals
5586
5587 F1 := Make_Temporary (Loc, 'A');
5588 F2 := Make_Temporary (Loc, 'B');
5589 L := New_Occurrence_Of (F1, Loc);
5590 R := New_Occurrence_Of (F2, Loc);
5591
5592 Func := Make_Defining_Identifier (Loc, Chars (Formal_Subp));
5593 Set_Ekind (Func, E_Function);
5594 Set_Is_Generic_Actual_Subprogram (Func);
5595
5596 Spec :=
5597 Make_Function_Specification (Loc,
5598 Defining_Unit_Name => Func,
5599 Parameter_Specifications => New_List (
5600 Make_Parameter_Specification (Loc,
5601 Defining_Identifier => F1,
5602 Parameter_Type => New_Occurrence_Of (Op_Type, Loc))),
5603 Result_Definition => New_Occurrence_Of (Ret_Type, Loc));
5604
5605 if Is_Binary then
5606 Append_To (Parameter_Specifications (Spec),
5607 Make_Parameter_Specification (Loc,
5608 Defining_Identifier => F2,
5609 Parameter_Type => New_Occurrence_Of (Op_Type, Loc)));
5610 end if;
5611
5612 -- Build expression as a function call, or as an operator node
5613 -- that corresponds to the name of the actual, starting with
5614 -- binary operators.
5615
5616 if Op_Name not in Any_Operator_Name then
5617 Expr :=
5618 Make_Function_Call (Loc,
5619 Name =>
5620 New_Occurrence_Of (Actual_Subp, Loc),
5621 Parameter_Associations => New_List (L));
5622
5623 if Is_Binary then
5624 Append_To (Parameter_Associations (Expr), R);
5625 end if;
5626
5627 -- Binary operators
5628
5629 elsif Is_Binary then
5630 if Op_Name = Name_Op_And then
5631 Expr := Make_Op_And (Loc, Left_Opnd => L, Right_Opnd => R);
5632 elsif Op_Name = Name_Op_Or then
5633 Expr := Make_Op_Or (Loc, Left_Opnd => L, Right_Opnd => R);
5634 elsif Op_Name = Name_Op_Xor then
5635 Expr := Make_Op_Xor (Loc, Left_Opnd => L, Right_Opnd => R);
5636 elsif Op_Name = Name_Op_Eq then
5637 Expr := Make_Op_Eq (Loc, Left_Opnd => L, Right_Opnd => R);
5638 elsif Op_Name = Name_Op_Ne then
5639 Expr := Make_Op_Ne (Loc, Left_Opnd => L, Right_Opnd => R);
5640 elsif Op_Name = Name_Op_Le then
5641 Expr := Make_Op_Le (Loc, Left_Opnd => L, Right_Opnd => R);
5642 elsif Op_Name = Name_Op_Gt then
5643 Expr := Make_Op_Gt (Loc, Left_Opnd => L, Right_Opnd => R);
5644 elsif Op_Name = Name_Op_Ge then
5645 Expr := Make_Op_Ge (Loc, Left_Opnd => L, Right_Opnd => R);
5646 elsif Op_Name = Name_Op_Lt then
5647 Expr := Make_Op_Lt (Loc, Left_Opnd => L, Right_Opnd => R);
5648 elsif Op_Name = Name_Op_Add then
5649 Expr := Make_Op_Add (Loc, Left_Opnd => L, Right_Opnd => R);
5650 elsif Op_Name = Name_Op_Subtract then
5651 Expr := Make_Op_Subtract (Loc, Left_Opnd => L, Right_Opnd => R);
5652 elsif Op_Name = Name_Op_Concat then
5653 Expr := Make_Op_Concat (Loc, Left_Opnd => L, Right_Opnd => R);
5654 elsif Op_Name = Name_Op_Multiply then
5655 Expr := Make_Op_Multiply (Loc, Left_Opnd => L, Right_Opnd => R);
5656 elsif Op_Name = Name_Op_Divide then
5657 Expr := Make_Op_Divide (Loc, Left_Opnd => L, Right_Opnd => R);
5658 elsif Op_Name = Name_Op_Mod then
5659 Expr := Make_Op_Mod (Loc, Left_Opnd => L, Right_Opnd => R);
5660 elsif Op_Name = Name_Op_Rem then
5661 Expr := Make_Op_Rem (Loc, Left_Opnd => L, Right_Opnd => R);
5662 elsif Op_Name = Name_Op_Expon then
5663 Expr := Make_Op_Expon (Loc, Left_Opnd => L, Right_Opnd => R);
5664 end if;
5665
5666 -- Unary operators
5667
5668 else
5669 if Op_Name = Name_Op_Add then
5670 Expr := Make_Op_Plus (Loc, Right_Opnd => L);
5671 elsif Op_Name = Name_Op_Subtract then
5672 Expr := Make_Op_Minus (Loc, Right_Opnd => L);
5673 elsif Op_Name = Name_Op_Abs then
5674 Expr := Make_Op_Abs (Loc, Right_Opnd => L);
5675 elsif Op_Name = Name_Op_Not then
5676 Expr := Make_Op_Not (Loc, Right_Opnd => L);
5677 end if;
5678 end if;
5679
5680 Decl :=
5681 Make_Expression_Function (Loc,
5682 Specification => Spec,
5683 Expression => Expr);
5684
5685 return Decl;
5686 end Build_Operator_Wrapper;
5687
5688 -------------------------------------------
5689 -- Build_Instance_Compilation_Unit_Nodes --
5690 -------------------------------------------
5691
5692 procedure Build_Instance_Compilation_Unit_Nodes
5693 (N : Node_Id;
5694 Act_Body : Node_Id;
5695 Act_Decl : Node_Id)
5696 is
5697 Decl_Cunit : Node_Id;
5698 Body_Cunit : Node_Id;
5699 Citem : Node_Id;
5700 New_Main : constant Entity_Id := Defining_Entity (Act_Decl);
5701 Old_Main : constant Entity_Id := Cunit_Entity (Main_Unit);
5702
5703 begin
5704 -- A new compilation unit node is built for the instance declaration
5705
5706 Decl_Cunit :=
5707 Make_Compilation_Unit (Sloc (N),
5708 Context_Items => Empty_List,
5709 Unit => Act_Decl,
5710 Aux_Decls_Node => Make_Compilation_Unit_Aux (Sloc (N)));
5711
5712 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
5713
5714 -- The new compilation unit is linked to its body, but both share the
5715 -- same file, so we do not set Body_Required on the new unit so as not
5716 -- to create a spurious dependency on a non-existent body in the ali.
5717 -- This simplifies CodePeer unit traversal.
5718
5719 -- We use the original instantiation compilation unit as the resulting
5720 -- compilation unit of the instance, since this is the main unit.
5721
5722 Rewrite (N, Act_Body);
5723
5724 -- Propagate the aspect specifications from the package body template to
5725 -- the instantiated version of the package body.
5726
5727 if Has_Aspects (Act_Body) then
5728 Set_Aspect_Specifications
5729 (N, New_Copy_List_Tree (Aspect_Specifications (Act_Body)));
5730 end if;
5731
5732 Body_Cunit := Parent (N);
5733
5734 -- The two compilation unit nodes are linked by the Library_Unit field
5735
5736 Set_Library_Unit (Decl_Cunit, Body_Cunit);
5737 Set_Library_Unit (Body_Cunit, Decl_Cunit);
5738
5739 -- Preserve the private nature of the package if needed
5740
5741 Set_Private_Present (Decl_Cunit, Private_Present (Body_Cunit));
5742
5743 -- If the instance is not the main unit, its context, categorization
5744 -- and elaboration entity are not relevant to the compilation.
5745
5746 if Body_Cunit /= Cunit (Main_Unit) then
5747 Make_Instance_Unit (Body_Cunit, In_Main => False);
5748 return;
5749 end if;
5750
5751 -- The context clause items on the instantiation, which are now attached
5752 -- to the body compilation unit (since the body overwrote the original
5753 -- instantiation node), semantically belong on the spec, so copy them
5754 -- there. It's harmless to leave them on the body as well. In fact one
5755 -- could argue that they belong in both places.
5756
5757 Citem := First (Context_Items (Body_Cunit));
5758 while Present (Citem) loop
5759 Append (New_Copy (Citem), Context_Items (Decl_Cunit));
5760 Next (Citem);
5761 end loop;
5762
5763 -- Propagate categorization flags on packages, so that they appear in
5764 -- the ali file for the spec of the unit.
5765
5766 if Ekind (New_Main) = E_Package then
5767 Set_Is_Pure (Old_Main, Is_Pure (New_Main));
5768 Set_Is_Preelaborated (Old_Main, Is_Preelaborated (New_Main));
5769 Set_Is_Remote_Types (Old_Main, Is_Remote_Types (New_Main));
5770 Set_Is_Shared_Passive (Old_Main, Is_Shared_Passive (New_Main));
5771 Set_Is_Remote_Call_Interface
5772 (Old_Main, Is_Remote_Call_Interface (New_Main));
5773 end if;
5774
5775 -- Make entry in Units table, so that binder can generate call to
5776 -- elaboration procedure for body, if any.
5777
5778 Make_Instance_Unit (Body_Cunit, In_Main => True);
5779 Main_Unit_Entity := New_Main;
5780 Set_Cunit_Entity (Main_Unit, Main_Unit_Entity);
5781
5782 -- Build elaboration entity, since the instance may certainly generate
5783 -- elaboration code requiring a flag for protection.
5784
5785 Build_Elaboration_Entity (Decl_Cunit, New_Main);
5786 end Build_Instance_Compilation_Unit_Nodes;
5787
5788 -----------------------------
5789 -- Check_Access_Definition --
5790 -----------------------------
5791
5792 procedure Check_Access_Definition (N : Node_Id) is
5793 begin
5794 pragma Assert
5795 (Ada_Version >= Ada_2005 and then Present (Access_Definition (N)));
5796 null;
5797 end Check_Access_Definition;
5798
5799 -----------------------------------
5800 -- Check_Formal_Package_Instance --
5801 -----------------------------------
5802
5803 -- If the formal has specific parameters, they must match those of the
5804 -- actual. Both of them are instances, and the renaming declarations for
5805 -- their formal parameters appear in the same order in both. The analyzed
5806 -- formal has been analyzed in the context of the current instance.
5807
5808 procedure Check_Formal_Package_Instance
5809 (Formal_Pack : Entity_Id;
5810 Actual_Pack : Entity_Id)
5811 is
5812 E1 : Entity_Id := First_Entity (Actual_Pack);
5813 E2 : Entity_Id := First_Entity (Formal_Pack);
5814 Prev_E1 : Entity_Id;
5815
5816 Expr1 : Node_Id;
5817 Expr2 : Node_Id;
5818
5819 procedure Check_Mismatch (B : Boolean);
5820 -- Common error routine for mismatch between the parameters of the
5821 -- actual instance and those of the formal package.
5822
5823 function Same_Instantiated_Constant (E1, E2 : Entity_Id) return Boolean;
5824 -- The formal may come from a nested formal package, and the actual may
5825 -- have been constant-folded. To determine whether the two denote the
5826 -- same entity we may have to traverse several definitions to recover
5827 -- the ultimate entity that they refer to.
5828
5829 function Same_Instantiated_Function (E1, E2 : Entity_Id) return Boolean;
5830 -- The formal and the actual must be identical, but if both are
5831 -- given by attributes they end up renaming different generated bodies,
5832 -- and we must verify that the attributes themselves match.
5833
5834 function Same_Instantiated_Variable (E1, E2 : Entity_Id) return Boolean;
5835 -- Similarly, if the formal comes from a nested formal package, the
5836 -- actual may designate the formal through multiple renamings, which
5837 -- have to be followed to determine the original variable in question.
5838
5839 --------------------
5840 -- Check_Mismatch --
5841 --------------------
5842
5843 procedure Check_Mismatch (B : Boolean) is
5844 -- A Formal_Type_Declaration for a derived private type is rewritten
5845 -- as a private extension decl. (see Analyze_Formal_Derived_Type),
5846 -- which is why we examine the original node.
5847
5848 Kind : constant Node_Kind := Nkind (Original_Node (Parent (E2)));
5849
5850 begin
5851 if Kind = N_Formal_Type_Declaration then
5852 return;
5853
5854 elsif Nkind_In (Kind, N_Formal_Object_Declaration,
5855 N_Formal_Package_Declaration)
5856 or else Kind in N_Formal_Subprogram_Declaration
5857 then
5858 null;
5859
5860 -- Ada 2012: If both formal and actual are incomplete types they
5861 -- are conformant.
5862
5863 elsif Is_Incomplete_Type (E1) and then Is_Incomplete_Type (E2) then
5864 null;
5865
5866 elsif B then
5867 Error_Msg_NE
5868 ("actual for & in actual instance does not match formal",
5869 Parent (Actual_Pack), E1);
5870 end if;
5871 end Check_Mismatch;
5872
5873 --------------------------------
5874 -- Same_Instantiated_Constant --
5875 --------------------------------
5876
5877 function Same_Instantiated_Constant
5878 (E1, E2 : Entity_Id) return Boolean
5879 is
5880 Ent : Entity_Id;
5881
5882 begin
5883 Ent := E2;
5884 while Present (Ent) loop
5885 if E1 = Ent then
5886 return True;
5887
5888 elsif Ekind (Ent) /= E_Constant then
5889 return False;
5890
5891 elsif Is_Entity_Name (Constant_Value (Ent)) then
5892 if Entity (Constant_Value (Ent)) = E1 then
5893 return True;
5894 else
5895 Ent := Entity (Constant_Value (Ent));
5896 end if;
5897
5898 -- The actual may be a constant that has been folded. Recover
5899 -- original name.
5900
5901 elsif Is_Entity_Name (Original_Node (Constant_Value (Ent))) then
5902 Ent := Entity (Original_Node (Constant_Value (Ent)));
5903
5904 else
5905 return False;
5906 end if;
5907 end loop;
5908
5909 return False;
5910 end Same_Instantiated_Constant;
5911
5912 --------------------------------
5913 -- Same_Instantiated_Function --
5914 --------------------------------
5915
5916 function Same_Instantiated_Function
5917 (E1, E2 : Entity_Id) return Boolean
5918 is
5919 U1, U2 : Node_Id;
5920 begin
5921 if Alias (E1) = Alias (E2) then
5922 return True;
5923
5924 elsif Present (Alias (E2)) then
5925 U1 := Original_Node (Unit_Declaration_Node (E1));
5926 U2 := Original_Node (Unit_Declaration_Node (Alias (E2)));
5927
5928 return Nkind (U1) = N_Subprogram_Renaming_Declaration
5929 and then Nkind (Name (U1)) = N_Attribute_Reference
5930
5931 and then Nkind (U2) = N_Subprogram_Renaming_Declaration
5932 and then Nkind (Name (U2)) = N_Attribute_Reference
5933
5934 and then
5935 Attribute_Name (Name (U1)) = Attribute_Name (Name (U2));
5936 else
5937 return False;
5938 end if;
5939 end Same_Instantiated_Function;
5940
5941 --------------------------------
5942 -- Same_Instantiated_Variable --
5943 --------------------------------
5944
5945 function Same_Instantiated_Variable
5946 (E1, E2 : Entity_Id) return Boolean
5947 is
5948 function Original_Entity (E : Entity_Id) return Entity_Id;
5949 -- Follow chain of renamings to the ultimate ancestor
5950
5951 ---------------------
5952 -- Original_Entity --
5953 ---------------------
5954
5955 function Original_Entity (E : Entity_Id) return Entity_Id is
5956 Orig : Entity_Id;
5957
5958 begin
5959 Orig := E;
5960 while Nkind (Parent (Orig)) = N_Object_Renaming_Declaration
5961 and then Present (Renamed_Object (Orig))
5962 and then Is_Entity_Name (Renamed_Object (Orig))
5963 loop
5964 Orig := Entity (Renamed_Object (Orig));
5965 end loop;
5966
5967 return Orig;
5968 end Original_Entity;
5969
5970 -- Start of processing for Same_Instantiated_Variable
5971
5972 begin
5973 return Ekind (E1) = Ekind (E2)
5974 and then Original_Entity (E1) = Original_Entity (E2);
5975 end Same_Instantiated_Variable;
5976
5977 -- Start of processing for Check_Formal_Package_Instance
5978
5979 begin
5980 Prev_E1 := E1;
5981 while Present (E1) and then Present (E2) loop
5982 exit when Ekind (E1) = E_Package
5983 and then Renamed_Entity (E1) = Renamed_Entity (Actual_Pack);
5984
5985 -- If the formal is the renaming of the formal package, this
5986 -- is the end of its formal part, which may occur before the
5987 -- end of the formal part in the actual in the presence of
5988 -- defaulted parameters in the formal package.
5989
5990 exit when Nkind (Parent (E2)) = N_Package_Renaming_Declaration
5991 and then Renamed_Entity (E2) = Scope (E2);
5992
5993 -- The analysis of the actual may generate additional internal
5994 -- entities. If the formal is defaulted, there is no corresponding
5995 -- analysis and the internal entities must be skipped, until we
5996 -- find corresponding entities again.
5997
5998 if Comes_From_Source (E2)
5999 and then not Comes_From_Source (E1)
6000 and then Chars (E1) /= Chars (E2)
6001 then
6002 while Present (E1) and then Chars (E1) /= Chars (E2) loop
6003 Next_Entity (E1);
6004 end loop;
6005 end if;
6006
6007 if No (E1) then
6008 return;
6009
6010 -- Entities may be declared without full declaration, such as
6011 -- itypes and predefined operators (concatenation for arrays, eg).
6012 -- Skip it and keep the formal entity to find a later match for it.
6013
6014 elsif No (Parent (E2)) and then Ekind (E1) /= Ekind (E2) then
6015 E1 := Prev_E1;
6016 goto Next_E;
6017
6018 -- If the formal entity comes from a formal declaration, it was
6019 -- defaulted in the formal package, and no check is needed on it.
6020
6021 elsif Nkind_In (Original_Node (Parent (E2)),
6022 N_Formal_Object_Declaration,
6023 N_Formal_Type_Declaration)
6024 then
6025 -- If the formal is a tagged type the corresponding class-wide
6026 -- type has been generated as well, and it must be skipped.
6027
6028 if Is_Type (E2) and then Is_Tagged_Type (E2) then
6029 Next_Entity (E2);
6030 end if;
6031
6032 goto Next_E;
6033
6034 -- Ditto for defaulted formal subprograms.
6035
6036 elsif Is_Overloadable (E1)
6037 and then Nkind (Unit_Declaration_Node (E2)) in
6038 N_Formal_Subprogram_Declaration
6039 then
6040 goto Next_E;
6041
6042 elsif Is_Type (E1) then
6043
6044 -- Subtypes must statically match. E1, E2 are the local entities
6045 -- that are subtypes of the actuals. Itypes generated for other
6046 -- parameters need not be checked, the check will be performed
6047 -- on the parameters themselves.
6048
6049 -- If E2 is a formal type declaration, it is a defaulted parameter
6050 -- and needs no checking.
6051
6052 if not Is_Itype (E1) and then not Is_Itype (E2) then
6053 Check_Mismatch
6054 (not Is_Type (E2)
6055 or else Etype (E1) /= Etype (E2)
6056 or else not Subtypes_Statically_Match (E1, E2));
6057 end if;
6058
6059 elsif Ekind (E1) = E_Constant then
6060
6061 -- IN parameters must denote the same static value, or the same
6062 -- constant, or the literal null.
6063
6064 Expr1 := Expression (Parent (E1));
6065
6066 if Ekind (E2) /= E_Constant then
6067 Check_Mismatch (True);
6068 goto Next_E;
6069 else
6070 Expr2 := Expression (Parent (E2));
6071 end if;
6072
6073 if Is_OK_Static_Expression (Expr1) then
6074 if not Is_OK_Static_Expression (Expr2) then
6075 Check_Mismatch (True);
6076
6077 elsif Is_Discrete_Type (Etype (E1)) then
6078 declare
6079 V1 : constant Uint := Expr_Value (Expr1);
6080 V2 : constant Uint := Expr_Value (Expr2);
6081 begin
6082 Check_Mismatch (V1 /= V2);
6083 end;
6084
6085 elsif Is_Real_Type (Etype (E1)) then
6086 declare
6087 V1 : constant Ureal := Expr_Value_R (Expr1);
6088 V2 : constant Ureal := Expr_Value_R (Expr2);
6089 begin
6090 Check_Mismatch (V1 /= V2);
6091 end;
6092
6093 elsif Is_String_Type (Etype (E1))
6094 and then Nkind (Expr1) = N_String_Literal
6095 then
6096 if Nkind (Expr2) /= N_String_Literal then
6097 Check_Mismatch (True);
6098 else
6099 Check_Mismatch
6100 (not String_Equal (Strval (Expr1), Strval (Expr2)));
6101 end if;
6102 end if;
6103
6104 elsif Is_Entity_Name (Expr1) then
6105 if Is_Entity_Name (Expr2) then
6106 if Entity (Expr1) = Entity (Expr2) then
6107 null;
6108 else
6109 Check_Mismatch
6110 (not Same_Instantiated_Constant
6111 (Entity (Expr1), Entity (Expr2)));
6112 end if;
6113
6114 else
6115 Check_Mismatch (True);
6116 end if;
6117
6118 elsif Is_Entity_Name (Original_Node (Expr1))
6119 and then Is_Entity_Name (Expr2)
6120 and then Same_Instantiated_Constant
6121 (Entity (Original_Node (Expr1)), Entity (Expr2))
6122 then
6123 null;
6124
6125 elsif Nkind (Expr1) = N_Null then
6126 Check_Mismatch (Nkind (Expr1) /= N_Null);
6127
6128 else
6129 Check_Mismatch (True);
6130 end if;
6131
6132 elsif Ekind (E1) = E_Variable then
6133 Check_Mismatch (not Same_Instantiated_Variable (E1, E2));
6134
6135 elsif Ekind (E1) = E_Package then
6136 Check_Mismatch
6137 (Ekind (E1) /= Ekind (E2)
6138 or else Renamed_Object (E1) /= Renamed_Object (E2));
6139
6140 elsif Is_Overloadable (E1) then
6141
6142 -- Verify that the actual subprograms match. Note that actuals
6143 -- that are attributes are rewritten as subprograms. If the
6144 -- subprogram in the formal package is defaulted, no check is
6145 -- needed. Note that this can only happen in Ada 2005 when the
6146 -- formal package can be partially parameterized.
6147
6148 if Nkind (Unit_Declaration_Node (E1)) =
6149 N_Subprogram_Renaming_Declaration
6150 and then From_Default (Unit_Declaration_Node (E1))
6151 then
6152 null;
6153
6154 -- If the formal package has an "others" box association that
6155 -- covers this formal, there is no need for a check either.
6156
6157 elsif Nkind (Unit_Declaration_Node (E2)) in
6158 N_Formal_Subprogram_Declaration
6159 and then Box_Present (Unit_Declaration_Node (E2))
6160 then
6161 null;
6162
6163 -- No check needed if subprogram is a defaulted null procedure
6164
6165 elsif No (Alias (E2))
6166 and then Ekind (E2) = E_Procedure
6167 and then
6168 Null_Present (Specification (Unit_Declaration_Node (E2)))
6169 then
6170 null;
6171
6172 -- Otherwise the actual in the formal and the actual in the
6173 -- instantiation of the formal must match, up to renamings.
6174
6175 else
6176 Check_Mismatch
6177 (Ekind (E2) /= Ekind (E1)
6178 or else not Same_Instantiated_Function (E1, E2));
6179 end if;
6180
6181 else
6182 raise Program_Error;
6183 end if;
6184
6185 <<Next_E>>
6186 Prev_E1 := E1;
6187 Next_Entity (E1);
6188 Next_Entity (E2);
6189 end loop;
6190 end Check_Formal_Package_Instance;
6191
6192 ---------------------------
6193 -- Check_Formal_Packages --
6194 ---------------------------
6195
6196 procedure Check_Formal_Packages (P_Id : Entity_Id) is
6197 E : Entity_Id;
6198 Formal_P : Entity_Id;
6199 Formal_Decl : Node_Id;
6200
6201 begin
6202 -- Iterate through the declarations in the instance, looking for package
6203 -- renaming declarations that denote instances of formal packages. Stop
6204 -- when we find the renaming of the current package itself. The
6205 -- declaration for a formal package without a box is followed by an
6206 -- internal entity that repeats the instantiation.
6207
6208 E := First_Entity (P_Id);
6209 while Present (E) loop
6210 if Ekind (E) = E_Package then
6211 if Renamed_Object (E) = P_Id then
6212 exit;
6213
6214 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
6215 null;
6216
6217 else
6218 Formal_Decl := Parent (Associated_Formal_Package (E));
6219
6220 -- Nothing to check if the formal has a box or an others_clause
6221 -- (necessarily with a box).
6222
6223 if Box_Present (Formal_Decl) then
6224 null;
6225
6226 elsif Nkind (First (Generic_Associations (Formal_Decl))) =
6227 N_Others_Choice
6228 then
6229 -- The internal validating package was generated but formal
6230 -- and instance are known to be compatible.
6231
6232 Formal_P := Next_Entity (E);
6233 Remove (Unit_Declaration_Node (Formal_P));
6234
6235 else
6236 Formal_P := Next_Entity (E);
6237 Check_Formal_Package_Instance (Formal_P, E);
6238
6239 -- After checking, remove the internal validating package.
6240 -- It is only needed for semantic checks, and as it may
6241 -- contain generic formal declarations it should not reach
6242 -- gigi.
6243
6244 Remove (Unit_Declaration_Node (Formal_P));
6245 end if;
6246 end if;
6247 end if;
6248
6249 Next_Entity (E);
6250 end loop;
6251 end Check_Formal_Packages;
6252
6253 ---------------------------------
6254 -- Check_Forward_Instantiation --
6255 ---------------------------------
6256
6257 procedure Check_Forward_Instantiation (Decl : Node_Id) is
6258 S : Entity_Id;
6259 Gen_Comp : Entity_Id := Cunit_Entity (Get_Source_Unit (Decl));
6260
6261 begin
6262 -- The instantiation appears before the generic body if we are in the
6263 -- scope of the unit containing the generic, either in its spec or in
6264 -- the package body, and before the generic body.
6265
6266 if Ekind (Gen_Comp) = E_Package_Body then
6267 Gen_Comp := Spec_Entity (Gen_Comp);
6268 end if;
6269
6270 if In_Open_Scopes (Gen_Comp)
6271 and then No (Corresponding_Body (Decl))
6272 then
6273 S := Current_Scope;
6274
6275 while Present (S)
6276 and then not Is_Compilation_Unit (S)
6277 and then not Is_Child_Unit (S)
6278 loop
6279 if Ekind (S) = E_Package then
6280 Set_Has_Forward_Instantiation (S);
6281 end if;
6282
6283 S := Scope (S);
6284 end loop;
6285 end if;
6286 end Check_Forward_Instantiation;
6287
6288 ---------------------------
6289 -- Check_Generic_Actuals --
6290 ---------------------------
6291
6292 -- The visibility of the actuals may be different between the point of
6293 -- generic instantiation and the instantiation of the body.
6294
6295 procedure Check_Generic_Actuals
6296 (Instance : Entity_Id;
6297 Is_Formal_Box : Boolean)
6298 is
6299 E : Entity_Id;
6300 Astype : Entity_Id;
6301
6302 function Denotes_Previous_Actual (Typ : Entity_Id) return Boolean;
6303 -- For a formal that is an array type, the component type is often a
6304 -- previous formal in the same unit. The privacy status of the component
6305 -- type will have been examined earlier in the traversal of the
6306 -- corresponding actuals, and this status should not be modified for
6307 -- the array (sub)type itself. However, if the base type of the array
6308 -- (sub)type is private, its full view must be restored in the body to
6309 -- be consistent with subsequent index subtypes, etc.
6310 --
6311 -- To detect this case we have to rescan the list of formals, which is
6312 -- usually short enough to ignore the resulting inefficiency.
6313
6314 -----------------------------
6315 -- Denotes_Previous_Actual --
6316 -----------------------------
6317
6318 function Denotes_Previous_Actual (Typ : Entity_Id) return Boolean is
6319 Prev : Entity_Id;
6320
6321 begin
6322 Prev := First_Entity (Instance);
6323 while Present (Prev) loop
6324 if Is_Type (Prev)
6325 and then Nkind (Parent (Prev)) = N_Subtype_Declaration
6326 and then Is_Entity_Name (Subtype_Indication (Parent (Prev)))
6327 and then Entity (Subtype_Indication (Parent (Prev))) = Typ
6328 then
6329 return True;
6330
6331 elsif Prev = E then
6332 return False;
6333
6334 else
6335 Next_Entity (Prev);
6336 end if;
6337 end loop;
6338
6339 return False;
6340 end Denotes_Previous_Actual;
6341
6342 -- Start of processing for Check_Generic_Actuals
6343
6344 begin
6345 E := First_Entity (Instance);
6346 while Present (E) loop
6347 if Is_Type (E)
6348 and then Nkind (Parent (E)) = N_Subtype_Declaration
6349 and then Scope (Etype (E)) /= Instance
6350 and then Is_Entity_Name (Subtype_Indication (Parent (E)))
6351 then
6352 if Is_Array_Type (E)
6353 and then not Is_Private_Type (Etype (E))
6354 and then Denotes_Previous_Actual (Component_Type (E))
6355 then
6356 null;
6357 else
6358 Check_Private_View (Subtype_Indication (Parent (E)));
6359 end if;
6360
6361 Set_Is_Generic_Actual_Type (E, True);
6362 Set_Is_Hidden (E, False);
6363 Set_Is_Potentially_Use_Visible (E,
6364 In_Use (Instance));
6365
6366 -- We constructed the generic actual type as a subtype of the
6367 -- supplied type. This means that it normally would not inherit
6368 -- subtype specific attributes of the actual, which is wrong for
6369 -- the generic case.
6370
6371 Astype := Ancestor_Subtype (E);
6372
6373 if No (Astype) then
6374
6375 -- This can happen when E is an itype that is the full view of
6376 -- a private type completed, e.g. with a constrained array. In
6377 -- that case, use the first subtype, which will carry size
6378 -- information. The base type itself is unconstrained and will
6379 -- not carry it.
6380
6381 Astype := First_Subtype (E);
6382 end if;
6383
6384 Set_Size_Info (E, (Astype));
6385 Set_RM_Size (E, RM_Size (Astype));
6386 Set_First_Rep_Item (E, First_Rep_Item (Astype));
6387
6388 if Is_Discrete_Or_Fixed_Point_Type (E) then
6389 Set_RM_Size (E, RM_Size (Astype));
6390
6391 -- In nested instances, the base type of an access actual may
6392 -- itself be private, and need to be exchanged.
6393
6394 elsif Is_Access_Type (E)
6395 and then Is_Private_Type (Etype (E))
6396 then
6397 Check_Private_View
6398 (New_Occurrence_Of (Etype (E), Sloc (Instance)));
6399 end if;
6400
6401 elsif Ekind (E) = E_Package then
6402
6403 -- If this is the renaming for the current instance, we're done.
6404 -- Otherwise it is a formal package. If the corresponding formal
6405 -- was declared with a box, the (instantiations of the) generic
6406 -- formal part are also visible. Otherwise, ignore the entity
6407 -- created to validate the actuals.
6408
6409 if Renamed_Object (E) = Instance then
6410 exit;
6411
6412 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
6413 null;
6414
6415 -- The visibility of a formal of an enclosing generic is already
6416 -- correct.
6417
6418 elsif Denotes_Formal_Package (E) then
6419 null;
6420
6421 elsif Present (Associated_Formal_Package (E))
6422 and then not Is_Generic_Formal (E)
6423 then
6424 if Box_Present (Parent (Associated_Formal_Package (E))) then
6425 Check_Generic_Actuals (Renamed_Object (E), True);
6426
6427 else
6428 Check_Generic_Actuals (Renamed_Object (E), False);
6429 end if;
6430
6431 Set_Is_Hidden (E, False);
6432 end if;
6433
6434 -- If this is a subprogram instance (in a wrapper package) the
6435 -- actual is fully visible.
6436
6437 elsif Is_Wrapper_Package (Instance) then
6438 Set_Is_Hidden (E, False);
6439
6440 -- If the formal package is declared with a box, or if the formal
6441 -- parameter is defaulted, it is visible in the body.
6442
6443 elsif Is_Formal_Box or else Is_Visible_Formal (E) then
6444 Set_Is_Hidden (E, False);
6445 end if;
6446
6447 if Ekind (E) = E_Constant then
6448
6449 -- If the type of the actual is a private type declared in the
6450 -- enclosing scope of the generic unit, the body of the generic
6451 -- sees the full view of the type (because it has to appear in
6452 -- the corresponding package body). If the type is private now,
6453 -- exchange views to restore the proper visiblity in the instance.
6454
6455 declare
6456 Typ : constant Entity_Id := Base_Type (Etype (E));
6457 -- The type of the actual
6458
6459 Gen_Id : Entity_Id;
6460 -- The generic unit
6461
6462 Parent_Scope : Entity_Id;
6463 -- The enclosing scope of the generic unit
6464
6465 begin
6466 if Is_Wrapper_Package (Instance) then
6467 Gen_Id :=
6468 Generic_Parent
6469 (Specification
6470 (Unit_Declaration_Node
6471 (Related_Instance (Instance))));
6472 else
6473 Gen_Id :=
6474 Generic_Parent (Package_Specification (Instance));
6475 end if;
6476
6477 Parent_Scope := Scope (Gen_Id);
6478
6479 -- The exchange is only needed if the generic is defined
6480 -- within a package which is not a common ancestor of the
6481 -- scope of the instance, and is not already in scope.
6482
6483 if Is_Private_Type (Typ)
6484 and then Scope (Typ) = Parent_Scope
6485 and then Scope (Instance) /= Parent_Scope
6486 and then Ekind (Parent_Scope) = E_Package
6487 and then not Is_Child_Unit (Gen_Id)
6488 then
6489 Switch_View (Typ);
6490
6491 -- If the type of the entity is a subtype, it may also have
6492 -- to be made visible, together with the base type of its
6493 -- full view, after exchange.
6494
6495 if Is_Private_Type (Etype (E)) then
6496 Switch_View (Etype (E));
6497 Switch_View (Base_Type (Etype (E)));
6498 end if;
6499 end if;
6500 end;
6501 end if;
6502
6503 Next_Entity (E);
6504 end loop;
6505 end Check_Generic_Actuals;
6506
6507 ------------------------------
6508 -- Check_Generic_Child_Unit --
6509 ------------------------------
6510
6511 procedure Check_Generic_Child_Unit
6512 (Gen_Id : Node_Id;
6513 Parent_Installed : in out Boolean)
6514 is
6515 Loc : constant Source_Ptr := Sloc (Gen_Id);
6516 Gen_Par : Entity_Id := Empty;
6517 E : Entity_Id;
6518 Inst_Par : Entity_Id;
6519 S : Node_Id;
6520
6521 function Find_Generic_Child
6522 (Scop : Entity_Id;
6523 Id : Node_Id) return Entity_Id;
6524 -- Search generic parent for possible child unit with the given name
6525
6526 function In_Enclosing_Instance return Boolean;
6527 -- Within an instance of the parent, the child unit may be denoted by
6528 -- a simple name, or an abbreviated expanded name. Examine enclosing
6529 -- scopes to locate a possible parent instantiation.
6530
6531 ------------------------
6532 -- Find_Generic_Child --
6533 ------------------------
6534
6535 function Find_Generic_Child
6536 (Scop : Entity_Id;
6537 Id : Node_Id) return Entity_Id
6538 is
6539 E : Entity_Id;
6540
6541 begin
6542 -- If entity of name is already set, instance has already been
6543 -- resolved, e.g. in an enclosing instantiation.
6544
6545 if Present (Entity (Id)) then
6546 if Scope (Entity (Id)) = Scop then
6547 return Entity (Id);
6548 else
6549 return Empty;
6550 end if;
6551
6552 else
6553 E := First_Entity (Scop);
6554 while Present (E) loop
6555 if Chars (E) = Chars (Id)
6556 and then Is_Child_Unit (E)
6557 then
6558 if Is_Child_Unit (E)
6559 and then not Is_Visible_Lib_Unit (E)
6560 then
6561 Error_Msg_NE
6562 ("generic child unit& is not visible", Gen_Id, E);
6563 end if;
6564
6565 Set_Entity (Id, E);
6566 return E;
6567 end if;
6568
6569 Next_Entity (E);
6570 end loop;
6571
6572 return Empty;
6573 end if;
6574 end Find_Generic_Child;
6575
6576 ---------------------------
6577 -- In_Enclosing_Instance --
6578 ---------------------------
6579
6580 function In_Enclosing_Instance return Boolean is
6581 Enclosing_Instance : Node_Id;
6582 Instance_Decl : Node_Id;
6583
6584 begin
6585 -- We do not inline any call that contains instantiations, except
6586 -- for instantiations of Unchecked_Conversion, so if we are within
6587 -- an inlined body the current instance does not require parents.
6588
6589 if In_Inlined_Body then
6590 pragma Assert (Chars (Gen_Id) = Name_Unchecked_Conversion);
6591 return False;
6592 end if;
6593
6594 -- Loop to check enclosing scopes
6595
6596 Enclosing_Instance := Current_Scope;
6597 while Present (Enclosing_Instance) loop
6598 Instance_Decl := Unit_Declaration_Node (Enclosing_Instance);
6599
6600 if Ekind (Enclosing_Instance) = E_Package
6601 and then Is_Generic_Instance (Enclosing_Instance)
6602 and then Present
6603 (Generic_Parent (Specification (Instance_Decl)))
6604 then
6605 -- Check whether the generic we are looking for is a child of
6606 -- this instance.
6607
6608 E := Find_Generic_Child
6609 (Generic_Parent (Specification (Instance_Decl)), Gen_Id);
6610 exit when Present (E);
6611
6612 else
6613 E := Empty;
6614 end if;
6615
6616 Enclosing_Instance := Scope (Enclosing_Instance);
6617 end loop;
6618
6619 if No (E) then
6620
6621 -- Not a child unit
6622
6623 Analyze (Gen_Id);
6624 return False;
6625
6626 else
6627 Rewrite (Gen_Id,
6628 Make_Expanded_Name (Loc,
6629 Chars => Chars (E),
6630 Prefix => New_Occurrence_Of (Enclosing_Instance, Loc),
6631 Selector_Name => New_Occurrence_Of (E, Loc)));
6632
6633 Set_Entity (Gen_Id, E);
6634 Set_Etype (Gen_Id, Etype (E));
6635 Parent_Installed := False; -- Already in scope.
6636 return True;
6637 end if;
6638 end In_Enclosing_Instance;
6639
6640 -- Start of processing for Check_Generic_Child_Unit
6641
6642 begin
6643 -- If the name of the generic is given by a selected component, it may
6644 -- be the name of a generic child unit, and the prefix is the name of an
6645 -- instance of the parent, in which case the child unit must be visible.
6646 -- If this instance is not in scope, it must be placed there and removed
6647 -- after instantiation, because what is being instantiated is not the
6648 -- original child, but the corresponding child present in the instance
6649 -- of the parent.
6650
6651 -- If the child is instantiated within the parent, it can be given by
6652 -- a simple name. In this case the instance is already in scope, but
6653 -- the child generic must be recovered from the generic parent as well.
6654
6655 if Nkind (Gen_Id) = N_Selected_Component then
6656 S := Selector_Name (Gen_Id);
6657 Analyze (Prefix (Gen_Id));
6658 Inst_Par := Entity (Prefix (Gen_Id));
6659
6660 if Ekind (Inst_Par) = E_Package
6661 and then Present (Renamed_Object (Inst_Par))
6662 then
6663 Inst_Par := Renamed_Object (Inst_Par);
6664 end if;
6665
6666 if Ekind (Inst_Par) = E_Package then
6667 if Nkind (Parent (Inst_Par)) = N_Package_Specification then
6668 Gen_Par := Generic_Parent (Parent (Inst_Par));
6669
6670 elsif Nkind (Parent (Inst_Par)) = N_Defining_Program_Unit_Name
6671 and then
6672 Nkind (Parent (Parent (Inst_Par))) = N_Package_Specification
6673 then
6674 Gen_Par := Generic_Parent (Parent (Parent (Inst_Par)));
6675 end if;
6676
6677 elsif Ekind (Inst_Par) = E_Generic_Package
6678 and then Nkind (Parent (Gen_Id)) = N_Formal_Package_Declaration
6679 then
6680 -- A formal package may be a real child package, and not the
6681 -- implicit instance within a parent. In this case the child is
6682 -- not visible and has to be retrieved explicitly as well.
6683
6684 Gen_Par := Inst_Par;
6685 end if;
6686
6687 if Present (Gen_Par) then
6688
6689 -- The prefix denotes an instantiation. The entity itself may be a
6690 -- nested generic, or a child unit.
6691
6692 E := Find_Generic_Child (Gen_Par, S);
6693
6694 if Present (E) then
6695 Change_Selected_Component_To_Expanded_Name (Gen_Id);
6696 Set_Entity (Gen_Id, E);
6697 Set_Etype (Gen_Id, Etype (E));
6698 Set_Entity (S, E);
6699 Set_Etype (S, Etype (E));
6700
6701 -- Indicate that this is a reference to the parent
6702
6703 if In_Extended_Main_Source_Unit (Gen_Id) then
6704 Set_Is_Instantiated (Inst_Par);
6705 end if;
6706
6707 -- A common mistake is to replicate the naming scheme of a
6708 -- hierarchy by instantiating a generic child directly, rather
6709 -- than the implicit child in a parent instance:
6710
6711 -- generic .. package Gpar is ..
6712 -- generic .. package Gpar.Child is ..
6713 -- package Par is new Gpar ();
6714
6715 -- with Gpar.Child;
6716 -- package Par.Child is new Gpar.Child ();
6717 -- rather than Par.Child
6718
6719 -- In this case the instantiation is within Par, which is an
6720 -- instance, but Gpar does not denote Par because we are not IN
6721 -- the instance of Gpar, so this is illegal. The test below
6722 -- recognizes this particular case.
6723
6724 if Is_Child_Unit (E)
6725 and then not Comes_From_Source (Entity (Prefix (Gen_Id)))
6726 and then (not In_Instance
6727 or else Nkind (Parent (Parent (Gen_Id))) =
6728 N_Compilation_Unit)
6729 then
6730 Error_Msg_N
6731 ("prefix of generic child unit must be instance of parent",
6732 Gen_Id);
6733 end if;
6734
6735 if not In_Open_Scopes (Inst_Par)
6736 and then Nkind (Parent (Gen_Id)) not in
6737 N_Generic_Renaming_Declaration
6738 then
6739 Install_Parent (Inst_Par);
6740 Parent_Installed := True;
6741
6742 elsif In_Open_Scopes (Inst_Par) then
6743
6744 -- If the parent is already installed, install the actuals
6745 -- for its formal packages. This is necessary when the child
6746 -- instance is a child of the parent instance: in this case,
6747 -- the parent is placed on the scope stack but the formal
6748 -- packages are not made visible.
6749
6750 Install_Formal_Packages (Inst_Par);
6751 end if;
6752
6753 else
6754 -- If the generic parent does not contain an entity that
6755 -- corresponds to the selector, the instance doesn't either.
6756 -- Analyzing the node will yield the appropriate error message.
6757 -- If the entity is not a child unit, then it is an inner
6758 -- generic in the parent.
6759
6760 Analyze (Gen_Id);
6761 end if;
6762
6763 else
6764 Analyze (Gen_Id);
6765
6766 if Is_Child_Unit (Entity (Gen_Id))
6767 and then
6768 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
6769 and then not In_Open_Scopes (Inst_Par)
6770 then
6771 Install_Parent (Inst_Par);
6772 Parent_Installed := True;
6773
6774 -- The generic unit may be the renaming of the implicit child
6775 -- present in an instance. In that case the parent instance is
6776 -- obtained from the name of the renamed entity.
6777
6778 elsif Ekind (Entity (Gen_Id)) = E_Generic_Package
6779 and then Present (Renamed_Entity (Entity (Gen_Id)))
6780 and then Is_Child_Unit (Renamed_Entity (Entity (Gen_Id)))
6781 then
6782 declare
6783 Renamed_Package : constant Node_Id :=
6784 Name (Parent (Entity (Gen_Id)));
6785 begin
6786 if Nkind (Renamed_Package) = N_Expanded_Name then
6787 Inst_Par := Entity (Prefix (Renamed_Package));
6788 Install_Parent (Inst_Par);
6789 Parent_Installed := True;
6790 end if;
6791 end;
6792 end if;
6793 end if;
6794
6795 elsif Nkind (Gen_Id) = N_Expanded_Name then
6796
6797 -- Entity already present, analyze prefix, whose meaning may be an
6798 -- instance in the current context. If it is an instance of a
6799 -- relative within another, the proper parent may still have to be
6800 -- installed, if they are not of the same generation.
6801
6802 Analyze (Prefix (Gen_Id));
6803
6804 -- Prevent cascaded errors
6805
6806 if Etype (Prefix (Gen_Id)) = Any_Type then
6807 return;
6808 end if;
6809
6810 -- In the unlikely case that a local declaration hides the name of
6811 -- the parent package, locate it on the homonym chain. If the context
6812 -- is an instance of the parent, the renaming entity is flagged as
6813 -- such.
6814
6815 Inst_Par := Entity (Prefix (Gen_Id));
6816 while Present (Inst_Par)
6817 and then not Is_Package_Or_Generic_Package (Inst_Par)
6818 loop
6819 Inst_Par := Homonym (Inst_Par);
6820 end loop;
6821
6822 pragma Assert (Present (Inst_Par));
6823 Set_Entity (Prefix (Gen_Id), Inst_Par);
6824
6825 if In_Enclosing_Instance then
6826 null;
6827
6828 elsif Present (Entity (Gen_Id))
6829 and then Is_Child_Unit (Entity (Gen_Id))
6830 and then not In_Open_Scopes (Inst_Par)
6831 then
6832 Install_Parent (Inst_Par);
6833 Parent_Installed := True;
6834 end if;
6835
6836 elsif In_Enclosing_Instance then
6837
6838 -- The child unit is found in some enclosing scope
6839
6840 null;
6841
6842 else
6843 Analyze (Gen_Id);
6844
6845 -- If this is the renaming of the implicit child in a parent
6846 -- instance, recover the parent name and install it.
6847
6848 if Is_Entity_Name (Gen_Id) then
6849 E := Entity (Gen_Id);
6850
6851 if Is_Generic_Unit (E)
6852 and then Nkind (Parent (E)) in N_Generic_Renaming_Declaration
6853 and then Is_Child_Unit (Renamed_Object (E))
6854 and then Is_Generic_Unit (Scope (Renamed_Object (E)))
6855 and then Nkind (Name (Parent (E))) = N_Expanded_Name
6856 then
6857 Rewrite (Gen_Id, New_Copy_Tree (Name (Parent (E))));
6858 Inst_Par := Entity (Prefix (Gen_Id));
6859
6860 if not In_Open_Scopes (Inst_Par) then
6861 Install_Parent (Inst_Par);
6862 Parent_Installed := True;
6863 end if;
6864
6865 -- If it is a child unit of a non-generic parent, it may be
6866 -- use-visible and given by a direct name. Install parent as
6867 -- for other cases.
6868
6869 elsif Is_Generic_Unit (E)
6870 and then Is_Child_Unit (E)
6871 and then
6872 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
6873 and then not Is_Generic_Unit (Scope (E))
6874 then
6875 if not In_Open_Scopes (Scope (E)) then
6876 Install_Parent (Scope (E));
6877 Parent_Installed := True;
6878 end if;
6879 end if;
6880 end if;
6881 end if;
6882 end Check_Generic_Child_Unit;
6883
6884 -----------------------------
6885 -- Check_Hidden_Child_Unit --
6886 -----------------------------
6887
6888 procedure Check_Hidden_Child_Unit
6889 (N : Node_Id;
6890 Gen_Unit : Entity_Id;
6891 Act_Decl_Id : Entity_Id)
6892 is
6893 Gen_Id : constant Node_Id := Name (N);
6894
6895 begin
6896 if Is_Child_Unit (Gen_Unit)
6897 and then Is_Child_Unit (Act_Decl_Id)
6898 and then Nkind (Gen_Id) = N_Expanded_Name
6899 and then Entity (Prefix (Gen_Id)) = Scope (Act_Decl_Id)
6900 and then Chars (Gen_Unit) = Chars (Act_Decl_Id)
6901 then
6902 Error_Msg_Node_2 := Scope (Act_Decl_Id);
6903 Error_Msg_NE
6904 ("generic unit & is implicitly declared in &",
6905 Defining_Unit_Name (N), Gen_Unit);
6906 Error_Msg_N ("\instance must have different name",
6907 Defining_Unit_Name (N));
6908 end if;
6909 end Check_Hidden_Child_Unit;
6910
6911 ------------------------
6912 -- Check_Private_View --
6913 ------------------------
6914
6915 procedure Check_Private_View (N : Node_Id) is
6916 T : constant Entity_Id := Etype (N);
6917 BT : Entity_Id;
6918
6919 begin
6920 -- Exchange views if the type was not private in the generic but is
6921 -- private at the point of instantiation. Do not exchange views if
6922 -- the scope of the type is in scope. This can happen if both generic
6923 -- and instance are sibling units, or if type is defined in a parent.
6924 -- In this case the visibility of the type will be correct for all
6925 -- semantic checks.
6926
6927 if Present (T) then
6928 BT := Base_Type (T);
6929
6930 if Is_Private_Type (T)
6931 and then not Has_Private_View (N)
6932 and then Present (Full_View (T))
6933 and then not In_Open_Scopes (Scope (T))
6934 then
6935 -- In the generic, the full type was visible. Save the private
6936 -- entity, for subsequent exchange.
6937
6938 Switch_View (T);
6939
6940 elsif Has_Private_View (N)
6941 and then not Is_Private_Type (T)
6942 and then not Has_Been_Exchanged (T)
6943 and then Etype (Get_Associated_Node (N)) /= T
6944 then
6945 -- Only the private declaration was visible in the generic. If
6946 -- the type appears in a subtype declaration, the subtype in the
6947 -- instance must have a view compatible with that of its parent,
6948 -- which must be exchanged (see corresponding code in Restore_
6949 -- Private_Views). Otherwise, if the type is defined in a parent
6950 -- unit, leave full visibility within instance, which is safe.
6951
6952 if In_Open_Scopes (Scope (Base_Type (T)))
6953 and then not Is_Private_Type (Base_Type (T))
6954 and then Comes_From_Source (Base_Type (T))
6955 then
6956 null;
6957
6958 elsif Nkind (Parent (N)) = N_Subtype_Declaration
6959 or else not In_Private_Part (Scope (Base_Type (T)))
6960 then
6961 Prepend_Elmt (T, Exchanged_Views);
6962 Exchange_Declarations (Etype (Get_Associated_Node (N)));
6963 end if;
6964
6965 -- For composite types with inconsistent representation exchange
6966 -- component types accordingly.
6967
6968 elsif Is_Access_Type (T)
6969 and then Is_Private_Type (Designated_Type (T))
6970 and then not Has_Private_View (N)
6971 and then Present (Full_View (Designated_Type (T)))
6972 then
6973 Switch_View (Designated_Type (T));
6974
6975 elsif Is_Array_Type (T) then
6976 if Is_Private_Type (Component_Type (T))
6977 and then not Has_Private_View (N)
6978 and then Present (Full_View (Component_Type (T)))
6979 then
6980 Switch_View (Component_Type (T));
6981 end if;
6982
6983 -- The normal exchange mechanism relies on the setting of a
6984 -- flag on the reference in the generic. However, an additional
6985 -- mechanism is needed for types that are not explicitly
6986 -- mentioned in the generic, but may be needed in expanded code
6987 -- in the instance. This includes component types of arrays and
6988 -- designated types of access types. This processing must also
6989 -- include the index types of arrays which we take care of here.
6990
6991 declare
6992 Indx : Node_Id;
6993 Typ : Entity_Id;
6994
6995 begin
6996 Indx := First_Index (T);
6997 while Present (Indx) loop
6998 Typ := Base_Type (Etype (Indx));
6999
7000 if Is_Private_Type (Typ)
7001 and then Present (Full_View (Typ))
7002 then
7003 Switch_View (Typ);
7004 end if;
7005
7006 Next_Index (Indx);
7007 end loop;
7008 end;
7009
7010 elsif Is_Private_Type (T)
7011 and then Present (Full_View (T))
7012 and then Is_Array_Type (Full_View (T))
7013 and then Is_Private_Type (Component_Type (Full_View (T)))
7014 then
7015 Switch_View (T);
7016
7017 -- Finally, a non-private subtype may have a private base type, which
7018 -- must be exchanged for consistency. This can happen when a package
7019 -- body is instantiated, when the scope stack is empty but in fact
7020 -- the subtype and the base type are declared in an enclosing scope.
7021
7022 -- Note that in this case we introduce an inconsistency in the view
7023 -- set, because we switch the base type BT, but there could be some
7024 -- private dependent subtypes of BT which remain unswitched. Such
7025 -- subtypes might need to be switched at a later point (see specific
7026 -- provision for that case in Switch_View).
7027
7028 elsif not Is_Private_Type (T)
7029 and then not Has_Private_View (N)
7030 and then Is_Private_Type (BT)
7031 and then Present (Full_View (BT))
7032 and then not Is_Generic_Type (BT)
7033 and then not In_Open_Scopes (BT)
7034 then
7035 Prepend_Elmt (Full_View (BT), Exchanged_Views);
7036 Exchange_Declarations (BT);
7037 end if;
7038 end if;
7039 end Check_Private_View;
7040
7041 -----------------------------
7042 -- Check_Hidden_Primitives --
7043 -----------------------------
7044
7045 function Check_Hidden_Primitives (Assoc_List : List_Id) return Elist_Id is
7046 Actual : Node_Id;
7047 Gen_T : Entity_Id;
7048 Result : Elist_Id := No_Elist;
7049
7050 begin
7051 if No (Assoc_List) then
7052 return No_Elist;
7053 end if;
7054
7055 -- Traverse the list of associations between formals and actuals
7056 -- searching for renamings of tagged types
7057
7058 Actual := First (Assoc_List);
7059 while Present (Actual) loop
7060 if Nkind (Actual) = N_Subtype_Declaration then
7061 Gen_T := Generic_Parent_Type (Actual);
7062
7063 if Present (Gen_T) and then Is_Tagged_Type (Gen_T) then
7064
7065 -- Traverse the list of primitives of the actual types
7066 -- searching for hidden primitives that are visible in the
7067 -- corresponding generic formal; leave them visible and
7068 -- append them to Result to restore their decoration later.
7069
7070 Install_Hidden_Primitives
7071 (Prims_List => Result,
7072 Gen_T => Gen_T,
7073 Act_T => Entity (Subtype_Indication (Actual)));
7074 end if;
7075 end if;
7076
7077 Next (Actual);
7078 end loop;
7079
7080 return Result;
7081 end Check_Hidden_Primitives;
7082
7083 --------------------------
7084 -- Contains_Instance_Of --
7085 --------------------------
7086
7087 function Contains_Instance_Of
7088 (Inner : Entity_Id;
7089 Outer : Entity_Id;
7090 N : Node_Id) return Boolean
7091 is
7092 Elmt : Elmt_Id;
7093 Scop : Entity_Id;
7094
7095 begin
7096 Scop := Outer;
7097
7098 -- Verify that there are no circular instantiations. We check whether
7099 -- the unit contains an instance of the current scope or some enclosing
7100 -- scope (in case one of the instances appears in a subunit). Longer
7101 -- circularities involving subunits might seem too pathological to
7102 -- consider, but they were not too pathological for the authors of
7103 -- DEC bc30vsq, so we loop over all enclosing scopes, and mark all
7104 -- enclosing generic scopes as containing an instance.
7105
7106 loop
7107 -- Within a generic subprogram body, the scope is not generic, to
7108 -- allow for recursive subprograms. Use the declaration to determine
7109 -- whether this is a generic unit.
7110
7111 if Ekind (Scop) = E_Generic_Package
7112 or else (Is_Subprogram (Scop)
7113 and then Nkind (Unit_Declaration_Node (Scop)) =
7114 N_Generic_Subprogram_Declaration)
7115 then
7116 Elmt := First_Elmt (Inner_Instances (Inner));
7117
7118 while Present (Elmt) loop
7119 if Node (Elmt) = Scop then
7120 Error_Msg_Node_2 := Inner;
7121 Error_Msg_NE
7122 ("circular Instantiation: & instantiated within &!",
7123 N, Scop);
7124 return True;
7125
7126 elsif Node (Elmt) = Inner then
7127 return True;
7128
7129 elsif Contains_Instance_Of (Node (Elmt), Scop, N) then
7130 Error_Msg_Node_2 := Inner;
7131 Error_Msg_NE
7132 ("circular Instantiation: & instantiated within &!",
7133 N, Node (Elmt));
7134 return True;
7135 end if;
7136
7137 Next_Elmt (Elmt);
7138 end loop;
7139
7140 -- Indicate that Inner is being instantiated within Scop
7141
7142 Append_Elmt (Inner, Inner_Instances (Scop));
7143 end if;
7144
7145 if Scop = Standard_Standard then
7146 exit;
7147 else
7148 Scop := Scope (Scop);
7149 end if;
7150 end loop;
7151
7152 return False;
7153 end Contains_Instance_Of;
7154
7155 -----------------------
7156 -- Copy_Generic_Node --
7157 -----------------------
7158
7159 function Copy_Generic_Node
7160 (N : Node_Id;
7161 Parent_Id : Node_Id;
7162 Instantiating : Boolean) return Node_Id
7163 is
7164 Ent : Entity_Id;
7165 New_N : Node_Id;
7166
7167 function Copy_Generic_Descendant (D : Union_Id) return Union_Id;
7168 -- Check the given value of one of the Fields referenced by the current
7169 -- node to determine whether to copy it recursively. The field may hold
7170 -- a Node_Id, a List_Id, or an Elist_Id, or a plain value (Sloc, Uint,
7171 -- Char) in which case it need not be copied.
7172
7173 procedure Copy_Descendants;
7174 -- Common utility for various nodes
7175
7176 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id;
7177 -- Make copy of element list
7178
7179 function Copy_Generic_List
7180 (L : List_Id;
7181 Parent_Id : Node_Id) return List_Id;
7182 -- Apply Copy_Node recursively to the members of a node list
7183
7184 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean;
7185 -- True if an identifier is part of the defining program unit name of
7186 -- a child unit. The entity of such an identifier must be kept (for
7187 -- ASIS use) even though as the name of an enclosing generic it would
7188 -- otherwise not be preserved in the generic tree.
7189
7190 ----------------------
7191 -- Copy_Descendants --
7192 ----------------------
7193
7194 procedure Copy_Descendants is
7195 use Atree.Unchecked_Access;
7196 -- This code section is part of the implementation of an untyped
7197 -- tree traversal, so it needs direct access to node fields.
7198
7199 begin
7200 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
7201 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
7202 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
7203 Set_Field4 (New_N, Copy_Generic_Descendant (Field4 (N)));
7204 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
7205 end Copy_Descendants;
7206
7207 -----------------------------
7208 -- Copy_Generic_Descendant --
7209 -----------------------------
7210
7211 function Copy_Generic_Descendant (D : Union_Id) return Union_Id is
7212 begin
7213 if D = Union_Id (Empty) then
7214 return D;
7215
7216 elsif D in Node_Range then
7217 return Union_Id
7218 (Copy_Generic_Node (Node_Id (D), New_N, Instantiating));
7219
7220 elsif D in List_Range then
7221 return Union_Id (Copy_Generic_List (List_Id (D), New_N));
7222
7223 elsif D in Elist_Range then
7224 return Union_Id (Copy_Generic_Elist (Elist_Id (D)));
7225
7226 -- Nothing else is copyable (e.g. Uint values), return as is
7227
7228 else
7229 return D;
7230 end if;
7231 end Copy_Generic_Descendant;
7232
7233 ------------------------
7234 -- Copy_Generic_Elist --
7235 ------------------------
7236
7237 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id is
7238 M : Elmt_Id;
7239 L : Elist_Id;
7240
7241 begin
7242 if Present (E) then
7243 L := New_Elmt_List;
7244 M := First_Elmt (E);
7245 while Present (M) loop
7246 Append_Elmt
7247 (Copy_Generic_Node (Node (M), Empty, Instantiating), L);
7248 Next_Elmt (M);
7249 end loop;
7250
7251 return L;
7252
7253 else
7254 return No_Elist;
7255 end if;
7256 end Copy_Generic_Elist;
7257
7258 -----------------------
7259 -- Copy_Generic_List --
7260 -----------------------
7261
7262 function Copy_Generic_List
7263 (L : List_Id;
7264 Parent_Id : Node_Id) return List_Id
7265 is
7266 N : Node_Id;
7267 New_L : List_Id;
7268
7269 begin
7270 if Present (L) then
7271 New_L := New_List;
7272 Set_Parent (New_L, Parent_Id);
7273
7274 N := First (L);
7275 while Present (N) loop
7276 Append (Copy_Generic_Node (N, Empty, Instantiating), New_L);
7277 Next (N);
7278 end loop;
7279
7280 return New_L;
7281
7282 else
7283 return No_List;
7284 end if;
7285 end Copy_Generic_List;
7286
7287 ---------------------------
7288 -- In_Defining_Unit_Name --
7289 ---------------------------
7290
7291 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean is
7292 begin
7293 return
7294 Present (Parent (Nam))
7295 and then (Nkind (Parent (Nam)) = N_Defining_Program_Unit_Name
7296 or else
7297 (Nkind (Parent (Nam)) = N_Expanded_Name
7298 and then In_Defining_Unit_Name (Parent (Nam))));
7299 end In_Defining_Unit_Name;
7300
7301 -- Start of processing for Copy_Generic_Node
7302
7303 begin
7304 if N = Empty then
7305 return N;
7306 end if;
7307
7308 New_N := New_Copy (N);
7309
7310 -- Copy aspects if present
7311
7312 if Has_Aspects (N) then
7313 Set_Has_Aspects (New_N, False);
7314 Set_Aspect_Specifications
7315 (New_N, Copy_Generic_List (Aspect_Specifications (N), Parent_Id));
7316 end if;
7317
7318 if Instantiating then
7319 Adjust_Instantiation_Sloc (New_N, S_Adjustment);
7320 end if;
7321
7322 if not Is_List_Member (N) then
7323 Set_Parent (New_N, Parent_Id);
7324 end if;
7325
7326 -- Special casing for identifiers and other entity names and operators
7327
7328 if Nkind_In (New_N, N_Character_Literal,
7329 N_Expanded_Name,
7330 N_Identifier,
7331 N_Operator_Symbol)
7332 or else Nkind (New_N) in N_Op
7333 then
7334 if not Instantiating then
7335
7336 -- Link both nodes in order to assign subsequently the entity of
7337 -- the copy to the original node, in case this is a global
7338 -- reference.
7339
7340 Set_Associated_Node (N, New_N);
7341
7342 -- If we are within an instantiation, this is a nested generic
7343 -- that has already been analyzed at the point of definition.
7344 -- We must preserve references that were global to the enclosing
7345 -- parent at that point. Other occurrences, whether global or
7346 -- local to the current generic, must be resolved anew, so we
7347 -- reset the entity in the generic copy. A global reference has a
7348 -- smaller depth than the parent, or else the same depth in case
7349 -- both are distinct compilation units.
7350
7351 -- A child unit is implicitly declared within the enclosing parent
7352 -- but is in fact global to it, and must be preserved.
7353
7354 -- It is also possible for Current_Instantiated_Parent to be
7355 -- defined, and for this not to be a nested generic, namely if
7356 -- the unit is loaded through Rtsfind. In that case, the entity of
7357 -- New_N is only a link to the associated node, and not a defining
7358 -- occurrence.
7359
7360 -- The entities for parent units in the defining_program_unit of a
7361 -- generic child unit are established when the context of the unit
7362 -- is first analyzed, before the generic copy is made. They are
7363 -- preserved in the copy for use in ASIS queries.
7364
7365 Ent := Entity (New_N);
7366
7367 if No (Current_Instantiated_Parent.Gen_Id) then
7368 if No (Ent)
7369 or else Nkind (Ent) /= N_Defining_Identifier
7370 or else not In_Defining_Unit_Name (N)
7371 then
7372 Set_Associated_Node (New_N, Empty);
7373 end if;
7374
7375 elsif No (Ent)
7376 or else
7377 not Nkind_In (Ent, N_Defining_Identifier,
7378 N_Defining_Character_Literal,
7379 N_Defining_Operator_Symbol)
7380 or else No (Scope (Ent))
7381 or else
7382 (Scope (Ent) = Current_Instantiated_Parent.Gen_Id
7383 and then not Is_Child_Unit (Ent))
7384 or else
7385 (Scope_Depth (Scope (Ent)) >
7386 Scope_Depth (Current_Instantiated_Parent.Gen_Id)
7387 and then
7388 Get_Source_Unit (Ent) =
7389 Get_Source_Unit (Current_Instantiated_Parent.Gen_Id))
7390 then
7391 Set_Associated_Node (New_N, Empty);
7392 end if;
7393
7394 -- Case of instantiating identifier or some other name or operator
7395
7396 else
7397 -- If the associated node is still defined, the entity in it
7398 -- is global, and must be copied to the instance. If this copy
7399 -- is being made for a body to inline, it is applied to an
7400 -- instantiated tree, and the entity is already present and
7401 -- must be also preserved.
7402
7403 declare
7404 Assoc : constant Node_Id := Get_Associated_Node (N);
7405
7406 begin
7407 if Present (Assoc) then
7408 if Nkind (Assoc) = Nkind (N) then
7409 Set_Entity (New_N, Entity (Assoc));
7410 Check_Private_View (N);
7411
7412 -- The node is a reference to a global type and acts as the
7413 -- subtype mark of a qualified expression created in order
7414 -- to aid resolution of accidental overloading in instances.
7415 -- Since N is a reference to a type, the Associated_Node of
7416 -- N denotes an entity rather than another identifier. See
7417 -- Qualify_Universal_Operands for details.
7418
7419 elsif Nkind (N) = N_Identifier
7420 and then Nkind (Parent (N)) = N_Qualified_Expression
7421 and then Subtype_Mark (Parent (N)) = N
7422 and then Is_Qualified_Universal_Literal (Parent (N))
7423 then
7424 Set_Entity (New_N, Assoc);
7425
7426 -- The name in the call may be a selected component if the
7427 -- call has not been analyzed yet, as may be the case for
7428 -- pre/post conditions in a generic unit.
7429
7430 elsif Nkind (Assoc) = N_Function_Call
7431 and then Is_Entity_Name (Name (Assoc))
7432 then
7433 Set_Entity (New_N, Entity (Name (Assoc)));
7434
7435 elsif Nkind_In (Assoc, N_Defining_Identifier,
7436 N_Defining_Character_Literal,
7437 N_Defining_Operator_Symbol)
7438 and then Expander_Active
7439 then
7440 -- Inlining case: we are copying a tree that contains
7441 -- global entities, which are preserved in the copy to be
7442 -- used for subsequent inlining.
7443
7444 null;
7445
7446 else
7447 Set_Entity (New_N, Empty);
7448 end if;
7449 end if;
7450 end;
7451 end if;
7452
7453 -- For expanded name, we must copy the Prefix and Selector_Name
7454
7455 if Nkind (N) = N_Expanded_Name then
7456 Set_Prefix
7457 (New_N, Copy_Generic_Node (Prefix (N), New_N, Instantiating));
7458
7459 Set_Selector_Name (New_N,
7460 Copy_Generic_Node (Selector_Name (N), New_N, Instantiating));
7461
7462 -- For operators, we must copy the right operand
7463
7464 elsif Nkind (N) in N_Op then
7465 Set_Right_Opnd (New_N,
7466 Copy_Generic_Node (Right_Opnd (N), New_N, Instantiating));
7467
7468 -- And for binary operators, the left operand as well
7469
7470 if Nkind (N) in N_Binary_Op then
7471 Set_Left_Opnd (New_N,
7472 Copy_Generic_Node (Left_Opnd (N), New_N, Instantiating));
7473 end if;
7474 end if;
7475
7476 -- Establish a link between an entity from the generic template and the
7477 -- corresponding entity in the generic copy to be analyzed.
7478
7479 elsif Nkind (N) in N_Entity then
7480 if not Instantiating then
7481 Set_Associated_Entity (N, New_N);
7482 end if;
7483
7484 -- Clear any existing link the copy may inherit from the replicated
7485 -- generic template entity.
7486
7487 Set_Associated_Entity (New_N, Empty);
7488
7489 -- Special casing for stubs
7490
7491 elsif Nkind (N) in N_Body_Stub then
7492
7493 -- In any case, we must copy the specification or defining
7494 -- identifier as appropriate.
7495
7496 if Nkind (N) = N_Subprogram_Body_Stub then
7497 Set_Specification (New_N,
7498 Copy_Generic_Node (Specification (N), New_N, Instantiating));
7499
7500 else
7501 Set_Defining_Identifier (New_N,
7502 Copy_Generic_Node
7503 (Defining_Identifier (N), New_N, Instantiating));
7504 end if;
7505
7506 -- If we are not instantiating, then this is where we load and
7507 -- analyze subunits, i.e. at the point where the stub occurs. A
7508 -- more permissive system might defer this analysis to the point
7509 -- of instantiation, but this seems too complicated for now.
7510
7511 if not Instantiating then
7512 declare
7513 Subunit_Name : constant Unit_Name_Type := Get_Unit_Name (N);
7514 Subunit : Node_Id;
7515 Unum : Unit_Number_Type;
7516 New_Body : Node_Id;
7517
7518 begin
7519 -- Make sure that, if it is a subunit of the main unit that is
7520 -- preprocessed and if -gnateG is specified, the preprocessed
7521 -- file will be written.
7522
7523 Lib.Analysing_Subunit_Of_Main :=
7524 Lib.In_Extended_Main_Source_Unit (N);
7525 Unum :=
7526 Load_Unit
7527 (Load_Name => Subunit_Name,
7528 Required => False,
7529 Subunit => True,
7530 Error_Node => N);
7531 Lib.Analysing_Subunit_Of_Main := False;
7532
7533 -- If the proper body is not found, a warning message will be
7534 -- emitted when analyzing the stub, or later at the point of
7535 -- instantiation. Here we just leave the stub as is.
7536
7537 if Unum = No_Unit then
7538 Subunits_Missing := True;
7539 goto Subunit_Not_Found;
7540 end if;
7541
7542 Subunit := Cunit (Unum);
7543
7544 if Nkind (Unit (Subunit)) /= N_Subunit then
7545 Error_Msg_N
7546 ("found child unit instead of expected SEPARATE subunit",
7547 Subunit);
7548 Error_Msg_Sloc := Sloc (N);
7549 Error_Msg_N ("\to complete stub #", Subunit);
7550 goto Subunit_Not_Found;
7551 end if;
7552
7553 -- We must create a generic copy of the subunit, in order to
7554 -- perform semantic analysis on it, and we must replace the
7555 -- stub in the original generic unit with the subunit, in order
7556 -- to preserve non-local references within.
7557
7558 -- Only the proper body needs to be copied. Library_Unit and
7559 -- context clause are simply inherited by the generic copy.
7560 -- Note that the copy (which may be recursive if there are
7561 -- nested subunits) must be done first, before attaching it to
7562 -- the enclosing generic.
7563
7564 New_Body :=
7565 Copy_Generic_Node
7566 (Proper_Body (Unit (Subunit)),
7567 Empty, Instantiating => False);
7568
7569 -- Now place the original proper body in the original generic
7570 -- unit. This is a body, not a compilation unit.
7571
7572 Rewrite (N, Proper_Body (Unit (Subunit)));
7573 Set_Is_Compilation_Unit (Defining_Entity (N), False);
7574 Set_Was_Originally_Stub (N);
7575
7576 -- Finally replace the body of the subunit with its copy, and
7577 -- make this new subunit into the library unit of the generic
7578 -- copy, which does not have stubs any longer.
7579
7580 Set_Proper_Body (Unit (Subunit), New_Body);
7581 Set_Library_Unit (New_N, Subunit);
7582 Inherit_Context (Unit (Subunit), N);
7583 end;
7584
7585 -- If we are instantiating, this must be an error case, since
7586 -- otherwise we would have replaced the stub node by the proper body
7587 -- that corresponds. So just ignore it in the copy (i.e. we have
7588 -- copied it, and that is good enough).
7589
7590 else
7591 null;
7592 end if;
7593
7594 <<Subunit_Not_Found>> null;
7595
7596 -- If the node is a compilation unit, it is the subunit of a stub, which
7597 -- has been loaded already (see code below). In this case, the library
7598 -- unit field of N points to the parent unit (which is a compilation
7599 -- unit) and need not (and cannot) be copied.
7600
7601 -- When the proper body of the stub is analyzed, the library_unit link
7602 -- is used to establish the proper context (see sem_ch10).
7603
7604 -- The other fields of a compilation unit are copied as usual
7605
7606 elsif Nkind (N) = N_Compilation_Unit then
7607
7608 -- This code can only be executed when not instantiating, because in
7609 -- the copy made for an instantiation, the compilation unit node has
7610 -- disappeared at the point that a stub is replaced by its proper
7611 -- body.
7612
7613 pragma Assert (not Instantiating);
7614
7615 Set_Context_Items (New_N,
7616 Copy_Generic_List (Context_Items (N), New_N));
7617
7618 Set_Unit (New_N,
7619 Copy_Generic_Node (Unit (N), New_N, False));
7620
7621 Set_First_Inlined_Subprogram (New_N,
7622 Copy_Generic_Node
7623 (First_Inlined_Subprogram (N), New_N, False));
7624
7625 Set_Aux_Decls_Node (New_N,
7626 Copy_Generic_Node (Aux_Decls_Node (N), New_N, False));
7627
7628 -- For an assignment node, the assignment is known to be semantically
7629 -- legal if we are instantiating the template. This avoids incorrect
7630 -- diagnostics in generated code.
7631
7632 elsif Nkind (N) = N_Assignment_Statement then
7633
7634 -- Copy name and expression fields in usual manner
7635
7636 Set_Name (New_N,
7637 Copy_Generic_Node (Name (N), New_N, Instantiating));
7638
7639 Set_Expression (New_N,
7640 Copy_Generic_Node (Expression (N), New_N, Instantiating));
7641
7642 if Instantiating then
7643 Set_Assignment_OK (Name (New_N), True);
7644 end if;
7645
7646 elsif Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
7647 if not Instantiating then
7648 Set_Associated_Node (N, New_N);
7649
7650 else
7651 if Present (Get_Associated_Node (N))
7652 and then Nkind (Get_Associated_Node (N)) = Nkind (N)
7653 then
7654 -- In the generic the aggregate has some composite type. If at
7655 -- the point of instantiation the type has a private view,
7656 -- install the full view (and that of its ancestors, if any).
7657
7658 declare
7659 T : Entity_Id := (Etype (Get_Associated_Node (New_N)));
7660 Rt : Entity_Id;
7661
7662 begin
7663 if Present (T) and then Is_Private_Type (T) then
7664 Switch_View (T);
7665 end if;
7666
7667 if Present (T)
7668 and then Is_Tagged_Type (T)
7669 and then Is_Derived_Type (T)
7670 then
7671 Rt := Root_Type (T);
7672
7673 loop
7674 T := Etype (T);
7675
7676 if Is_Private_Type (T) then
7677 Switch_View (T);
7678 end if;
7679
7680 exit when T = Rt;
7681 end loop;
7682 end if;
7683 end;
7684 end if;
7685 end if;
7686
7687 -- Do not copy the associated node, which points to the generic copy
7688 -- of the aggregate.
7689
7690 declare
7691 use Atree.Unchecked_Access;
7692 -- This code section is part of the implementation of an untyped
7693 -- tree traversal, so it needs direct access to node fields.
7694
7695 begin
7696 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
7697 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
7698 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
7699 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
7700 end;
7701
7702 -- Allocators do not have an identifier denoting the access type, so we
7703 -- must locate it through the expression to check whether the views are
7704 -- consistent.
7705
7706 elsif Nkind (N) = N_Allocator
7707 and then Nkind (Expression (N)) = N_Qualified_Expression
7708 and then Is_Entity_Name (Subtype_Mark (Expression (N)))
7709 and then Instantiating
7710 then
7711 declare
7712 T : constant Node_Id :=
7713 Get_Associated_Node (Subtype_Mark (Expression (N)));
7714 Acc_T : Entity_Id;
7715
7716 begin
7717 if Present (T) then
7718
7719 -- Retrieve the allocator node in the generic copy
7720
7721 Acc_T := Etype (Parent (Parent (T)));
7722
7723 if Present (Acc_T) and then Is_Private_Type (Acc_T) then
7724 Switch_View (Acc_T);
7725 end if;
7726 end if;
7727
7728 Copy_Descendants;
7729 end;
7730
7731 -- For a proper body, we must catch the case of a proper body that
7732 -- replaces a stub. This represents the point at which a separate
7733 -- compilation unit, and hence template file, may be referenced, so we
7734 -- must make a new source instantiation entry for the template of the
7735 -- subunit, and ensure that all nodes in the subunit are adjusted using
7736 -- this new source instantiation entry.
7737
7738 elsif Nkind (N) in N_Proper_Body then
7739 declare
7740 Save_Adjustment : constant Sloc_Adjustment := S_Adjustment;
7741
7742 begin
7743 if Instantiating and then Was_Originally_Stub (N) then
7744 Create_Instantiation_Source
7745 (Instantiation_Node,
7746 Defining_Entity (N),
7747 S_Adjustment);
7748 end if;
7749
7750 -- Now copy the fields of the proper body, using the new
7751 -- adjustment factor if one was needed as per test above.
7752
7753 Copy_Descendants;
7754
7755 -- Restore the original adjustment factor in case changed
7756
7757 S_Adjustment := Save_Adjustment;
7758 end;
7759
7760 elsif Nkind (N) = N_Pragma and then Instantiating then
7761
7762 -- Do not copy Comment or Ident pragmas their content is relevant to
7763 -- the generic unit, not to the instantiating unit.
7764
7765 if Nam_In (Pragma_Name_Unmapped (N), Name_Comment, Name_Ident) then
7766 New_N := Make_Null_Statement (Sloc (N));
7767
7768 -- Do not copy pragmas generated from aspects because the pragmas do
7769 -- not carry any semantic information, plus they will be regenerated
7770 -- in the instance.
7771
7772 -- However, generating C we need to copy them since postconditions
7773 -- are inlined by the front end, and the front-end inlining machinery
7774 -- relies on this routine to perform inlining.
7775
7776 elsif From_Aspect_Specification (N)
7777 and then not Modify_Tree_For_C
7778 then
7779 New_N := Make_Null_Statement (Sloc (N));
7780
7781 else
7782 Copy_Descendants;
7783 end if;
7784
7785 elsif Nkind_In (N, N_Integer_Literal, N_Real_Literal) then
7786
7787 -- No descendant fields need traversing
7788
7789 null;
7790
7791 elsif Nkind (N) = N_String_Literal
7792 and then Present (Etype (N))
7793 and then Instantiating
7794 then
7795 -- If the string is declared in an outer scope, the string_literal
7796 -- subtype created for it may have the wrong scope. Force reanalysis
7797 -- of the constant to generate a new itype in the proper context.
7798
7799 Set_Etype (New_N, Empty);
7800 Set_Analyzed (New_N, False);
7801
7802 -- For the remaining nodes, copy their descendants recursively
7803
7804 else
7805 Copy_Descendants;
7806
7807 if Instantiating and then Nkind (N) = N_Subprogram_Body then
7808 Set_Generic_Parent (Specification (New_N), N);
7809
7810 -- Should preserve Corresponding_Spec??? (12.3(14))
7811 end if;
7812 end if;
7813
7814 -- Propagate dimensions if present, so that they are reflected in the
7815 -- instance.
7816
7817 if Nkind (N) in N_Has_Etype
7818 and then (Nkind (N) in N_Op or else Is_Entity_Name (N))
7819 and then Present (Etype (N))
7820 and then Is_Floating_Point_Type (Etype (N))
7821 and then Has_Dimension_System (Etype (N))
7822 then
7823 Copy_Dimensions (N, New_N);
7824 end if;
7825
7826 return New_N;
7827 end Copy_Generic_Node;
7828
7829 ----------------------------
7830 -- Denotes_Formal_Package --
7831 ----------------------------
7832
7833 function Denotes_Formal_Package
7834 (Pack : Entity_Id;
7835 On_Exit : Boolean := False;
7836 Instance : Entity_Id := Empty) return Boolean
7837 is
7838 Par : Entity_Id;
7839 Scop : constant Entity_Id := Scope (Pack);
7840 E : Entity_Id;
7841
7842 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean;
7843 -- The package in question may be an actual for a previous formal
7844 -- package P of the current instance, so examine its actuals as well.
7845 -- This must be recursive over other formal packages.
7846
7847 ----------------------------------
7848 -- Is_Actual_Of_Previous_Formal --
7849 ----------------------------------
7850
7851 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean is
7852 E1 : Entity_Id;
7853
7854 begin
7855 E1 := First_Entity (P);
7856 while Present (E1) and then E1 /= Instance loop
7857 if Ekind (E1) = E_Package
7858 and then Nkind (Parent (E1)) = N_Package_Renaming_Declaration
7859 then
7860 if Renamed_Object (E1) = Pack then
7861 return True;
7862
7863 elsif E1 = P or else Renamed_Object (E1) = P then
7864 return False;
7865
7866 elsif Is_Actual_Of_Previous_Formal (E1) then
7867 return True;
7868 end if;
7869 end if;
7870
7871 Next_Entity (E1);
7872 end loop;
7873
7874 return False;
7875 end Is_Actual_Of_Previous_Formal;
7876
7877 -- Start of processing for Denotes_Formal_Package
7878
7879 begin
7880 if On_Exit then
7881 Par :=
7882 Instance_Envs.Table
7883 (Instance_Envs.Last).Instantiated_Parent.Act_Id;
7884 else
7885 Par := Current_Instantiated_Parent.Act_Id;
7886 end if;
7887
7888 if Ekind (Scop) = E_Generic_Package
7889 or else Nkind (Unit_Declaration_Node (Scop)) =
7890 N_Generic_Subprogram_Declaration
7891 then
7892 return True;
7893
7894 elsif Nkind (Original_Node (Unit_Declaration_Node (Pack))) =
7895 N_Formal_Package_Declaration
7896 then
7897 return True;
7898
7899 elsif No (Par) then
7900 return False;
7901
7902 else
7903 -- Check whether this package is associated with a formal package of
7904 -- the enclosing instantiation. Iterate over the list of renamings.
7905
7906 E := First_Entity (Par);
7907 while Present (E) loop
7908 if Ekind (E) /= E_Package
7909 or else Nkind (Parent (E)) /= N_Package_Renaming_Declaration
7910 then
7911 null;
7912
7913 elsif Renamed_Object (E) = Par then
7914 return False;
7915
7916 elsif Renamed_Object (E) = Pack then
7917 return True;
7918
7919 elsif Is_Actual_Of_Previous_Formal (E) then
7920 return True;
7921
7922 end if;
7923
7924 Next_Entity (E);
7925 end loop;
7926
7927 return False;
7928 end if;
7929 end Denotes_Formal_Package;
7930
7931 -----------------
7932 -- End_Generic --
7933 -----------------
7934
7935 procedure End_Generic is
7936 begin
7937 -- ??? More things could be factored out in this routine. Should
7938 -- probably be done at a later stage.
7939
7940 Inside_A_Generic := Generic_Flags.Table (Generic_Flags.Last);
7941 Generic_Flags.Decrement_Last;
7942
7943 Expander_Mode_Restore;
7944 end End_Generic;
7945
7946 -------------
7947 -- Earlier --
7948 -------------
7949
7950 function Earlier (N1, N2 : Node_Id) return Boolean is
7951 procedure Find_Depth (P : in out Node_Id; D : in out Integer);
7952 -- Find distance from given node to enclosing compilation unit
7953
7954 ----------------
7955 -- Find_Depth --
7956 ----------------
7957
7958 procedure Find_Depth (P : in out Node_Id; D : in out Integer) is
7959 begin
7960 while Present (P)
7961 and then Nkind (P) /= N_Compilation_Unit
7962 loop
7963 P := True_Parent (P);
7964 D := D + 1;
7965 end loop;
7966 end Find_Depth;
7967
7968 -- Local declarations
7969
7970 D1 : Integer := 0;
7971 D2 : Integer := 0;
7972 P1 : Node_Id := N1;
7973 P2 : Node_Id := N2;
7974 T1 : Source_Ptr;
7975 T2 : Source_Ptr;
7976
7977 -- Start of processing for Earlier
7978
7979 begin
7980 Find_Depth (P1, D1);
7981 Find_Depth (P2, D2);
7982
7983 if P1 /= P2 then
7984 return False;
7985 else
7986 P1 := N1;
7987 P2 := N2;
7988 end if;
7989
7990 while D1 > D2 loop
7991 P1 := True_Parent (P1);
7992 D1 := D1 - 1;
7993 end loop;
7994
7995 while D2 > D1 loop
7996 P2 := True_Parent (P2);
7997 D2 := D2 - 1;
7998 end loop;
7999
8000 -- At this point P1 and P2 are at the same distance from the root.
8001 -- We examine their parents until we find a common declarative list.
8002 -- If we reach the root, N1 and N2 do not descend from the same
8003 -- declarative list (e.g. one is nested in the declarative part and
8004 -- the other is in a block in the statement part) and the earlier
8005 -- one is already frozen.
8006
8007 while not Is_List_Member (P1)
8008 or else not Is_List_Member (P2)
8009 or else List_Containing (P1) /= List_Containing (P2)
8010 loop
8011 P1 := True_Parent (P1);
8012 P2 := True_Parent (P2);
8013
8014 if Nkind (Parent (P1)) = N_Subunit then
8015 P1 := Corresponding_Stub (Parent (P1));
8016 end if;
8017
8018 if Nkind (Parent (P2)) = N_Subunit then
8019 P2 := Corresponding_Stub (Parent (P2));
8020 end if;
8021
8022 if P1 = P2 then
8023 return False;
8024 end if;
8025 end loop;
8026
8027 -- Expanded code usually shares the source location of the original
8028 -- construct it was generated for. This however may not necessarily
8029 -- reflect the true location of the code within the tree.
8030
8031 -- Before comparing the slocs of the two nodes, make sure that we are
8032 -- working with correct source locations. Assume that P1 is to the left
8033 -- of P2. If either one does not come from source, traverse the common
8034 -- list heading towards the other node and locate the first source
8035 -- statement.
8036
8037 -- P1 P2
8038 -- ----+===+===+--------------+===+===+----
8039 -- expanded code expanded code
8040
8041 if not Comes_From_Source (P1) then
8042 while Present (P1) loop
8043
8044 -- Neither P2 nor a source statement were located during the
8045 -- search. If we reach the end of the list, then P1 does not
8046 -- occur earlier than P2.
8047
8048 -- ---->
8049 -- start --- P2 ----- P1 --- end
8050
8051 if No (Next (P1)) then
8052 return False;
8053
8054 -- We encounter P2 while going to the right of the list. This
8055 -- means that P1 does indeed appear earlier.
8056
8057 -- ---->
8058 -- start --- P1 ===== P2 --- end
8059 -- expanded code in between
8060
8061 elsif P1 = P2 then
8062 return True;
8063
8064 -- No need to look any further since we have located a source
8065 -- statement.
8066
8067 elsif Comes_From_Source (P1) then
8068 exit;
8069 end if;
8070
8071 -- Keep going right
8072
8073 Next (P1);
8074 end loop;
8075 end if;
8076
8077 if not Comes_From_Source (P2) then
8078 while Present (P2) loop
8079
8080 -- Neither P1 nor a source statement were located during the
8081 -- search. If we reach the start of the list, then P1 does not
8082 -- occur earlier than P2.
8083
8084 -- <----
8085 -- start --- P2 --- P1 --- end
8086
8087 if No (Prev (P2)) then
8088 return False;
8089
8090 -- We encounter P1 while going to the left of the list. This
8091 -- means that P1 does indeed appear earlier.
8092
8093 -- <----
8094 -- start --- P1 ===== P2 --- end
8095 -- expanded code in between
8096
8097 elsif P2 = P1 then
8098 return True;
8099
8100 -- No need to look any further since we have located a source
8101 -- statement.
8102
8103 elsif Comes_From_Source (P2) then
8104 exit;
8105 end if;
8106
8107 -- Keep going left
8108
8109 Prev (P2);
8110 end loop;
8111 end if;
8112
8113 -- At this point either both nodes came from source or we approximated
8114 -- their source locations through neighboring source statements.
8115
8116 T1 := Top_Level_Location (Sloc (P1));
8117 T2 := Top_Level_Location (Sloc (P2));
8118
8119 -- When two nodes come from the same instance, they have identical top
8120 -- level locations. To determine proper relation within the tree, check
8121 -- their locations within the template.
8122
8123 if T1 = T2 then
8124 return Sloc (P1) < Sloc (P2);
8125
8126 -- The two nodes either come from unrelated instances or do not come
8127 -- from instantiated code at all.
8128
8129 else
8130 return T1 < T2;
8131 end if;
8132 end Earlier;
8133
8134 ----------------------
8135 -- Find_Actual_Type --
8136 ----------------------
8137
8138 function Find_Actual_Type
8139 (Typ : Entity_Id;
8140 Gen_Type : Entity_Id) return Entity_Id
8141 is
8142 Gen_Scope : constant Entity_Id := Scope (Gen_Type);
8143 T : Entity_Id;
8144
8145 begin
8146 -- Special processing only applies to child units
8147
8148 if not Is_Child_Unit (Gen_Scope) then
8149 return Get_Instance_Of (Typ);
8150
8151 -- If designated or component type is itself a formal of the child unit,
8152 -- its instance is available.
8153
8154 elsif Scope (Typ) = Gen_Scope then
8155 return Get_Instance_Of (Typ);
8156
8157 -- If the array or access type is not declared in the parent unit,
8158 -- no special processing needed.
8159
8160 elsif not Is_Generic_Type (Typ)
8161 and then Scope (Gen_Scope) /= Scope (Typ)
8162 then
8163 return Get_Instance_Of (Typ);
8164
8165 -- Otherwise, retrieve designated or component type by visibility
8166
8167 else
8168 T := Current_Entity (Typ);
8169 while Present (T) loop
8170 if In_Open_Scopes (Scope (T)) then
8171 return T;
8172 elsif Is_Generic_Actual_Type (T) then
8173 return T;
8174 end if;
8175
8176 T := Homonym (T);
8177 end loop;
8178
8179 return Typ;
8180 end if;
8181 end Find_Actual_Type;
8182
8183 ----------------------------
8184 -- Freeze_Subprogram_Body --
8185 ----------------------------
8186
8187 procedure Freeze_Subprogram_Body
8188 (Inst_Node : Node_Id;
8189 Gen_Body : Node_Id;
8190 Pack_Id : Entity_Id)
8191 is
8192 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
8193 Par : constant Entity_Id := Scope (Gen_Unit);
8194 E_G_Id : Entity_Id;
8195 Enc_G : Entity_Id;
8196 Enc_I : Node_Id;
8197 F_Node : Node_Id;
8198
8199 function Enclosing_Package_Body (N : Node_Id) return Node_Id;
8200 -- Find innermost package body that encloses the given node, and which
8201 -- is not a compilation unit. Freeze nodes for the instance, or for its
8202 -- enclosing body, may be inserted after the enclosing_body of the
8203 -- generic unit. Used to determine proper placement of freeze node for
8204 -- both package and subprogram instances.
8205
8206 function Package_Freeze_Node (B : Node_Id) return Node_Id;
8207 -- Find entity for given package body, and locate or create a freeze
8208 -- node for it.
8209
8210 ----------------------------
8211 -- Enclosing_Package_Body --
8212 ----------------------------
8213
8214 function Enclosing_Package_Body (N : Node_Id) return Node_Id is
8215 P : Node_Id;
8216
8217 begin
8218 P := Parent (N);
8219 while Present (P)
8220 and then Nkind (Parent (P)) /= N_Compilation_Unit
8221 loop
8222 if Nkind (P) = N_Package_Body then
8223 if Nkind (Parent (P)) = N_Subunit then
8224 return Corresponding_Stub (Parent (P));
8225 else
8226 return P;
8227 end if;
8228 end if;
8229
8230 P := True_Parent (P);
8231 end loop;
8232
8233 return Empty;
8234 end Enclosing_Package_Body;
8235
8236 -------------------------
8237 -- Package_Freeze_Node --
8238 -------------------------
8239
8240 function Package_Freeze_Node (B : Node_Id) return Node_Id is
8241 Id : Entity_Id;
8242
8243 begin
8244 if Nkind (B) = N_Package_Body then
8245 Id := Corresponding_Spec (B);
8246 else pragma Assert (Nkind (B) = N_Package_Body_Stub);
8247 Id := Corresponding_Spec (Proper_Body (Unit (Library_Unit (B))));
8248 end if;
8249
8250 Ensure_Freeze_Node (Id);
8251 return Freeze_Node (Id);
8252 end Package_Freeze_Node;
8253
8254 -- Start of processing for Freeze_Subprogram_Body
8255
8256 begin
8257 -- If the instance and the generic body appear within the same unit, and
8258 -- the instance precedes the generic, the freeze node for the instance
8259 -- must appear after that of the generic. If the generic is nested
8260 -- within another instance I2, then current instance must be frozen
8261 -- after I2. In both cases, the freeze nodes are those of enclosing
8262 -- packages. Otherwise, the freeze node is placed at the end of the
8263 -- current declarative part.
8264
8265 Enc_G := Enclosing_Package_Body (Gen_Body);
8266 Enc_I := Enclosing_Package_Body (Inst_Node);
8267 Ensure_Freeze_Node (Pack_Id);
8268 F_Node := Freeze_Node (Pack_Id);
8269
8270 if Is_Generic_Instance (Par)
8271 and then Present (Freeze_Node (Par))
8272 and then In_Same_Declarative_Part (Freeze_Node (Par), Inst_Node)
8273 then
8274 -- The parent was a premature instantiation. Insert freeze node at
8275 -- the end the current declarative part.
8276
8277 if ABE_Is_Certain (Get_Package_Instantiation_Node (Par)) then
8278 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8279
8280 -- Handle the following case:
8281 --
8282 -- package Parent_Inst is new ...
8283 -- Parent_Inst []
8284 --
8285 -- procedure P ... -- this body freezes Parent_Inst
8286 --
8287 -- package Inst is new ...
8288 --
8289 -- In this particular scenario, the freeze node for Inst must be
8290 -- inserted in the same manner as that of Parent_Inst - before the
8291 -- next source body or at the end of the declarative list (body not
8292 -- available). If body P did not exist and Parent_Inst was frozen
8293 -- after Inst, either by a body following Inst or at the end of the
8294 -- declarative region, the freeze node for Inst must be inserted
8295 -- after that of Parent_Inst. This relation is established by
8296 -- comparing the Slocs of Parent_Inst freeze node and Inst.
8297
8298 elsif List_Containing (Get_Package_Instantiation_Node (Par)) =
8299 List_Containing (Inst_Node)
8300 and then Sloc (Freeze_Node (Par)) < Sloc (Inst_Node)
8301 then
8302 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8303
8304 else
8305 Insert_After (Freeze_Node (Par), F_Node);
8306 end if;
8307
8308 -- The body enclosing the instance should be frozen after the body that
8309 -- includes the generic, because the body of the instance may make
8310 -- references to entities therein. If the two are not in the same
8311 -- declarative part, or if the one enclosing the instance is frozen
8312 -- already, freeze the instance at the end of the current declarative
8313 -- part.
8314
8315 elsif Is_Generic_Instance (Par)
8316 and then Present (Freeze_Node (Par))
8317 and then Present (Enc_I)
8318 then
8319 if In_Same_Declarative_Part (Freeze_Node (Par), Enc_I)
8320 or else
8321 (Nkind (Enc_I) = N_Package_Body
8322 and then
8323 In_Same_Declarative_Part (Freeze_Node (Par), Parent (Enc_I)))
8324 then
8325 -- The enclosing package may contain several instances. Rather
8326 -- than computing the earliest point at which to insert its freeze
8327 -- node, we place it at the end of the declarative part of the
8328 -- parent of the generic.
8329
8330 Insert_Freeze_Node_For_Instance
8331 (Freeze_Node (Par), Package_Freeze_Node (Enc_I));
8332 end if;
8333
8334 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8335
8336 elsif Present (Enc_G)
8337 and then Present (Enc_I)
8338 and then Enc_G /= Enc_I
8339 and then Earlier (Inst_Node, Gen_Body)
8340 then
8341 if Nkind (Enc_G) = N_Package_Body then
8342 E_G_Id :=
8343 Corresponding_Spec (Enc_G);
8344 else pragma Assert (Nkind (Enc_G) = N_Package_Body_Stub);
8345 E_G_Id :=
8346 Corresponding_Spec (Proper_Body (Unit (Library_Unit (Enc_G))));
8347 end if;
8348
8349 -- Freeze package that encloses instance, and place node after the
8350 -- package that encloses generic. If enclosing package is already
8351 -- frozen we have to assume it is at the proper place. This may be a
8352 -- potential ABE that requires dynamic checking. Do not add a freeze
8353 -- node if the package that encloses the generic is inside the body
8354 -- that encloses the instance, because the freeze node would be in
8355 -- the wrong scope. Additional contortions needed if the bodies are
8356 -- within a subunit.
8357
8358 declare
8359 Enclosing_Body : Node_Id;
8360
8361 begin
8362 if Nkind (Enc_I) = N_Package_Body_Stub then
8363 Enclosing_Body := Proper_Body (Unit (Library_Unit (Enc_I)));
8364 else
8365 Enclosing_Body := Enc_I;
8366 end if;
8367
8368 if Parent (List_Containing (Enc_G)) /= Enclosing_Body then
8369 Insert_Freeze_Node_For_Instance
8370 (Enc_G, Package_Freeze_Node (Enc_I));
8371 end if;
8372 end;
8373
8374 -- Freeze enclosing subunit before instance
8375
8376 Ensure_Freeze_Node (E_G_Id);
8377
8378 if not Is_List_Member (Freeze_Node (E_G_Id)) then
8379 Insert_After (Enc_G, Freeze_Node (E_G_Id));
8380 end if;
8381
8382 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8383
8384 else
8385 -- If none of the above, insert freeze node at the end of the current
8386 -- declarative part.
8387
8388 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8389 end if;
8390 end Freeze_Subprogram_Body;
8391
8392 ----------------
8393 -- Get_Gen_Id --
8394 ----------------
8395
8396 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id is
8397 begin
8398 return Generic_Renamings.Table (E).Gen_Id;
8399 end Get_Gen_Id;
8400
8401 ---------------------
8402 -- Get_Instance_Of --
8403 ---------------------
8404
8405 function Get_Instance_Of (A : Entity_Id) return Entity_Id is
8406 Res : constant Assoc_Ptr := Generic_Renamings_HTable.Get (A);
8407
8408 begin
8409 if Res /= Assoc_Null then
8410 return Generic_Renamings.Table (Res).Act_Id;
8411
8412 else
8413 -- On exit, entity is not instantiated: not a generic parameter, or
8414 -- else parameter of an inner generic unit.
8415
8416 return A;
8417 end if;
8418 end Get_Instance_Of;
8419
8420 ------------------------------------
8421 -- Get_Package_Instantiation_Node --
8422 ------------------------------------
8423
8424 function Get_Package_Instantiation_Node (A : Entity_Id) return Node_Id is
8425 Decl : Node_Id := Unit_Declaration_Node (A);
8426 Inst : Node_Id;
8427
8428 begin
8429 -- If the Package_Instantiation attribute has been set on the package
8430 -- entity, then use it directly when it (or its Original_Node) refers
8431 -- to an N_Package_Instantiation node. In principle it should be
8432 -- possible to have this field set in all cases, which should be
8433 -- investigated, and would allow this function to be significantly
8434 -- simplified. ???
8435
8436 Inst := Package_Instantiation (A);
8437
8438 if Present (Inst) then
8439 if Nkind (Inst) = N_Package_Instantiation then
8440 return Inst;
8441
8442 elsif Nkind (Original_Node (Inst)) = N_Package_Instantiation then
8443 return Original_Node (Inst);
8444 end if;
8445 end if;
8446
8447 -- If the instantiation is a compilation unit that does not need body
8448 -- then the instantiation node has been rewritten as a package
8449 -- declaration for the instance, and we return the original node.
8450
8451 -- If it is a compilation unit and the instance node has not been
8452 -- rewritten, then it is still the unit of the compilation. Finally, if
8453 -- a body is present, this is a parent of the main unit whose body has
8454 -- been compiled for inlining purposes, and the instantiation node has
8455 -- been rewritten with the instance body.
8456
8457 -- Otherwise the instantiation node appears after the declaration. If
8458 -- the entity is a formal package, the declaration may have been
8459 -- rewritten as a generic declaration (in the case of a formal with box)
8460 -- or left as a formal package declaration if it has actuals, and is
8461 -- found with a forward search.
8462
8463 if Nkind (Parent (Decl)) = N_Compilation_Unit then
8464 if Nkind (Decl) = N_Package_Declaration
8465 and then Present (Corresponding_Body (Decl))
8466 then
8467 Decl := Unit_Declaration_Node (Corresponding_Body (Decl));
8468 end if;
8469
8470 if Nkind (Original_Node (Decl)) = N_Package_Instantiation then
8471 return Original_Node (Decl);
8472 else
8473 return Unit (Parent (Decl));
8474 end if;
8475
8476 elsif Nkind (Decl) = N_Package_Declaration
8477 and then Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration
8478 then
8479 return Original_Node (Decl);
8480
8481 else
8482 Inst := Next (Decl);
8483 while not Nkind_In (Inst, N_Package_Instantiation,
8484 N_Formal_Package_Declaration)
8485 loop
8486 Next (Inst);
8487 end loop;
8488
8489 return Inst;
8490 end if;
8491 end Get_Package_Instantiation_Node;
8492
8493 ------------------------
8494 -- Has_Been_Exchanged --
8495 ------------------------
8496
8497 function Has_Been_Exchanged (E : Entity_Id) return Boolean is
8498 Next : Elmt_Id;
8499
8500 begin
8501 Next := First_Elmt (Exchanged_Views);
8502 while Present (Next) loop
8503 if Full_View (Node (Next)) = E then
8504 return True;
8505 end if;
8506
8507 Next_Elmt (Next);
8508 end loop;
8509
8510 return False;
8511 end Has_Been_Exchanged;
8512
8513 ----------
8514 -- Hash --
8515 ----------
8516
8517 function Hash (F : Entity_Id) return HTable_Range is
8518 begin
8519 return HTable_Range (F mod HTable_Size);
8520 end Hash;
8521
8522 ------------------------
8523 -- Hide_Current_Scope --
8524 ------------------------
8525
8526 procedure Hide_Current_Scope is
8527 C : constant Entity_Id := Current_Scope;
8528 E : Entity_Id;
8529
8530 begin
8531 Set_Is_Hidden_Open_Scope (C);
8532
8533 E := First_Entity (C);
8534 while Present (E) loop
8535 if Is_Immediately_Visible (E) then
8536 Set_Is_Immediately_Visible (E, False);
8537 Append_Elmt (E, Hidden_Entities);
8538 end if;
8539
8540 Next_Entity (E);
8541 end loop;
8542
8543 -- Make the scope name invisible as well. This is necessary, but might
8544 -- conflict with calls to Rtsfind later on, in case the scope is a
8545 -- predefined one. There is no clean solution to this problem, so for
8546 -- now we depend on the user not redefining Standard itself in one of
8547 -- the parent units.
8548
8549 if Is_Immediately_Visible (C) and then C /= Standard_Standard then
8550 Set_Is_Immediately_Visible (C, False);
8551 Append_Elmt (C, Hidden_Entities);
8552 end if;
8553
8554 end Hide_Current_Scope;
8555
8556 --------------
8557 -- Init_Env --
8558 --------------
8559
8560 procedure Init_Env is
8561 Saved : Instance_Env;
8562
8563 begin
8564 Saved.Instantiated_Parent := Current_Instantiated_Parent;
8565 Saved.Exchanged_Views := Exchanged_Views;
8566 Saved.Hidden_Entities := Hidden_Entities;
8567 Saved.Current_Sem_Unit := Current_Sem_Unit;
8568 Saved.Parent_Unit_Visible := Parent_Unit_Visible;
8569 Saved.Instance_Parent_Unit := Instance_Parent_Unit;
8570
8571 -- Save configuration switches. These may be reset if the unit is a
8572 -- predefined unit, and the current mode is not Ada 2005.
8573
8574 Save_Opt_Config_Switches (Saved.Switches);
8575
8576 Instance_Envs.Append (Saved);
8577
8578 Exchanged_Views := New_Elmt_List;
8579 Hidden_Entities := New_Elmt_List;
8580
8581 -- Make dummy entry for Instantiated parent. If generic unit is legal,
8582 -- this is set properly in Set_Instance_Env.
8583
8584 Current_Instantiated_Parent :=
8585 (Current_Scope, Current_Scope, Assoc_Null);
8586 end Init_Env;
8587
8588 ------------------------------
8589 -- In_Same_Declarative_Part --
8590 ------------------------------
8591
8592 function In_Same_Declarative_Part
8593 (F_Node : Node_Id;
8594 Inst : Node_Id) return Boolean
8595 is
8596 Decls : constant Node_Id := Parent (F_Node);
8597 Nod : Node_Id;
8598
8599 begin
8600 Nod := Parent (Inst);
8601 while Present (Nod) loop
8602 if Nod = Decls then
8603 return True;
8604
8605 elsif Nkind_In (Nod, N_Subprogram_Body,
8606 N_Package_Body,
8607 N_Package_Declaration,
8608 N_Task_Body,
8609 N_Protected_Body,
8610 N_Block_Statement)
8611 then
8612 return False;
8613
8614 elsif Nkind (Nod) = N_Subunit then
8615 Nod := Corresponding_Stub (Nod);
8616
8617 elsif Nkind (Nod) = N_Compilation_Unit then
8618 return False;
8619
8620 else
8621 Nod := Parent (Nod);
8622 end if;
8623 end loop;
8624
8625 return False;
8626 end In_Same_Declarative_Part;
8627
8628 ---------------------
8629 -- In_Main_Context --
8630 ---------------------
8631
8632 function In_Main_Context (E : Entity_Id) return Boolean is
8633 Context : List_Id;
8634 Clause : Node_Id;
8635 Nam : Node_Id;
8636
8637 begin
8638 if not Is_Compilation_Unit (E)
8639 or else Ekind (E) /= E_Package
8640 or else In_Private_Part (E)
8641 then
8642 return False;
8643 end if;
8644
8645 Context := Context_Items (Cunit (Main_Unit));
8646
8647 Clause := First (Context);
8648 while Present (Clause) loop
8649 if Nkind (Clause) = N_With_Clause then
8650 Nam := Name (Clause);
8651
8652 -- If the current scope is part of the context of the main unit,
8653 -- analysis of the corresponding with_clause is not complete, and
8654 -- the entity is not set. We use the Chars field directly, which
8655 -- might produce false positives in rare cases, but guarantees
8656 -- that we produce all the instance bodies we will need.
8657
8658 if (Is_Entity_Name (Nam) and then Chars (Nam) = Chars (E))
8659 or else (Nkind (Nam) = N_Selected_Component
8660 and then Chars (Selector_Name (Nam)) = Chars (E))
8661 then
8662 return True;
8663 end if;
8664 end if;
8665
8666 Next (Clause);
8667 end loop;
8668
8669 return False;
8670 end In_Main_Context;
8671
8672 ---------------------
8673 -- Inherit_Context --
8674 ---------------------
8675
8676 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id) is
8677 Current_Context : List_Id;
8678 Current_Unit : Node_Id;
8679 Item : Node_Id;
8680 New_I : Node_Id;
8681
8682 Clause : Node_Id;
8683 OK : Boolean;
8684 Lib_Unit : Node_Id;
8685
8686 begin
8687 if Nkind (Parent (Gen_Decl)) = N_Compilation_Unit then
8688
8689 -- The inherited context is attached to the enclosing compilation
8690 -- unit. This is either the main unit, or the declaration for the
8691 -- main unit (in case the instantiation appears within the package
8692 -- declaration and the main unit is its body).
8693
8694 Current_Unit := Parent (Inst);
8695 while Present (Current_Unit)
8696 and then Nkind (Current_Unit) /= N_Compilation_Unit
8697 loop
8698 Current_Unit := Parent (Current_Unit);
8699 end loop;
8700
8701 Current_Context := Context_Items (Current_Unit);
8702
8703 Item := First (Context_Items (Parent (Gen_Decl)));
8704 while Present (Item) loop
8705 if Nkind (Item) = N_With_Clause then
8706 Lib_Unit := Library_Unit (Item);
8707
8708 -- Take care to prevent direct cyclic with's
8709
8710 if Lib_Unit /= Current_Unit then
8711
8712 -- Do not add a unit if it is already in the context
8713
8714 Clause := First (Current_Context);
8715 OK := True;
8716 while Present (Clause) loop
8717 if Nkind (Clause) = N_With_Clause and then
8718 Library_Unit (Clause) = Lib_Unit
8719 then
8720 OK := False;
8721 exit;
8722 end if;
8723
8724 Next (Clause);
8725 end loop;
8726
8727 if OK then
8728 New_I := New_Copy (Item);
8729 Set_Implicit_With (New_I, True);
8730 Set_Implicit_With_From_Instantiation (New_I, True);
8731 Append (New_I, Current_Context);
8732 end if;
8733 end if;
8734 end if;
8735
8736 Next (Item);
8737 end loop;
8738 end if;
8739 end Inherit_Context;
8740
8741 ----------------
8742 -- Initialize --
8743 ----------------
8744
8745 procedure Initialize is
8746 begin
8747 Generic_Renamings.Init;
8748 Instance_Envs.Init;
8749 Generic_Flags.Init;
8750 Generic_Renamings_HTable.Reset;
8751 Circularity_Detected := False;
8752 Exchanged_Views := No_Elist;
8753 Hidden_Entities := No_Elist;
8754 end Initialize;
8755
8756 -------------------------------------
8757 -- Insert_Freeze_Node_For_Instance --
8758 -------------------------------------
8759
8760 procedure Insert_Freeze_Node_For_Instance
8761 (N : Node_Id;
8762 F_Node : Node_Id)
8763 is
8764 Decl : Node_Id;
8765 Decls : List_Id;
8766 Inst : Entity_Id;
8767 Par_N : Node_Id;
8768
8769 function Enclosing_Body (N : Node_Id) return Node_Id;
8770 -- Find enclosing package or subprogram body, if any. Freeze node may
8771 -- be placed at end of current declarative list if previous instance
8772 -- and current one have different enclosing bodies.
8773
8774 function Previous_Instance (Gen : Entity_Id) return Entity_Id;
8775 -- Find the local instance, if any, that declares the generic that is
8776 -- being instantiated. If present, the freeze node for this instance
8777 -- must follow the freeze node for the previous instance.
8778
8779 --------------------
8780 -- Enclosing_Body --
8781 --------------------
8782
8783 function Enclosing_Body (N : Node_Id) return Node_Id is
8784 P : Node_Id;
8785
8786 begin
8787 P := Parent (N);
8788 while Present (P)
8789 and then Nkind (Parent (P)) /= N_Compilation_Unit
8790 loop
8791 if Nkind_In (P, N_Package_Body, N_Subprogram_Body) then
8792 if Nkind (Parent (P)) = N_Subunit then
8793 return Corresponding_Stub (Parent (P));
8794 else
8795 return P;
8796 end if;
8797 end if;
8798
8799 P := True_Parent (P);
8800 end loop;
8801
8802 return Empty;
8803 end Enclosing_Body;
8804
8805 -----------------------
8806 -- Previous_Instance --
8807 -----------------------
8808
8809 function Previous_Instance (Gen : Entity_Id) return Entity_Id is
8810 S : Entity_Id;
8811
8812 begin
8813 S := Scope (Gen);
8814 while Present (S) and then S /= Standard_Standard loop
8815 if Is_Generic_Instance (S)
8816 and then In_Same_Source_Unit (S, N)
8817 then
8818 return S;
8819 end if;
8820
8821 S := Scope (S);
8822 end loop;
8823
8824 return Empty;
8825 end Previous_Instance;
8826
8827 -- Start of processing for Insert_Freeze_Node_For_Instance
8828
8829 begin
8830 if not Is_List_Member (F_Node) then
8831 Decl := N;
8832 Decls := List_Containing (N);
8833 Inst := Entity (F_Node);
8834 Par_N := Parent (Decls);
8835
8836 -- When processing a subprogram instantiation, utilize the actual
8837 -- subprogram instantiation rather than its package wrapper as it
8838 -- carries all the context information.
8839
8840 if Is_Wrapper_Package (Inst) then
8841 Inst := Related_Instance (Inst);
8842 end if;
8843
8844 -- If this is a package instance, check whether the generic is
8845 -- declared in a previous instance and the current instance is
8846 -- not within the previous one.
8847
8848 if Present (Generic_Parent (Parent (Inst)))
8849 and then Is_In_Main_Unit (N)
8850 then
8851 declare
8852 Enclosing_N : constant Node_Id := Enclosing_Body (N);
8853 Par_I : constant Entity_Id :=
8854 Previous_Instance
8855 (Generic_Parent (Parent (Inst)));
8856 Scop : Entity_Id;
8857
8858 begin
8859 if Present (Par_I)
8860 and then Earlier (N, Freeze_Node (Par_I))
8861 then
8862 Scop := Scope (Inst);
8863
8864 -- If the current instance is within the one that contains
8865 -- the generic, the freeze node for the current one must
8866 -- appear in the current declarative part. Ditto, if the
8867 -- current instance is within another package instance or
8868 -- within a body that does not enclose the current instance.
8869 -- In these three cases the freeze node of the previous
8870 -- instance is not relevant.
8871
8872 while Present (Scop) and then Scop /= Standard_Standard loop
8873 exit when Scop = Par_I
8874 or else
8875 (Is_Generic_Instance (Scop)
8876 and then Scope_Depth (Scop) > Scope_Depth (Par_I));
8877 Scop := Scope (Scop);
8878 end loop;
8879
8880 -- Previous instance encloses current instance
8881
8882 if Scop = Par_I then
8883 null;
8884
8885 -- If the next node is a source body we must freeze in
8886 -- the current scope as well.
8887
8888 elsif Present (Next (N))
8889 and then Nkind_In (Next (N), N_Subprogram_Body,
8890 N_Package_Body)
8891 and then Comes_From_Source (Next (N))
8892 then
8893 null;
8894
8895 -- Current instance is within an unrelated instance
8896
8897 elsif Is_Generic_Instance (Scop) then
8898 null;
8899
8900 -- Current instance is within an unrelated body
8901
8902 elsif Present (Enclosing_N)
8903 and then Enclosing_N /= Enclosing_Body (Par_I)
8904 then
8905 null;
8906
8907 else
8908 Insert_After (Freeze_Node (Par_I), F_Node);
8909 return;
8910 end if;
8911 end if;
8912 end;
8913 end if;
8914
8915 -- When the instantiation occurs in a package declaration, append the
8916 -- freeze node to the private declarations (if any).
8917
8918 if Nkind (Par_N) = N_Package_Specification
8919 and then Decls = Visible_Declarations (Par_N)
8920 and then Present (Private_Declarations (Par_N))
8921 and then not Is_Empty_List (Private_Declarations (Par_N))
8922 then
8923 Decls := Private_Declarations (Par_N);
8924 Decl := First (Decls);
8925 end if;
8926
8927 -- Determine the proper freeze point of a package instantiation. We
8928 -- adhere to the general rule of a package or subprogram body causing
8929 -- freezing of anything before it in the same declarative region. In
8930 -- this case, the proper freeze point of a package instantiation is
8931 -- before the first source body which follows, or before a stub. This
8932 -- ensures that entities coming from the instance are already frozen
8933 -- and usable in source bodies.
8934
8935 if Nkind (Par_N) /= N_Package_Declaration
8936 and then Ekind (Inst) = E_Package
8937 and then Is_Generic_Instance (Inst)
8938 and then
8939 not In_Same_Source_Unit (Generic_Parent (Parent (Inst)), Inst)
8940 then
8941 while Present (Decl) loop
8942 if (Nkind (Decl) in N_Unit_Body
8943 or else
8944 Nkind (Decl) in N_Body_Stub)
8945 and then Comes_From_Source (Decl)
8946 then
8947 Insert_Before (Decl, F_Node);
8948 return;
8949 end if;
8950
8951 Next (Decl);
8952 end loop;
8953 end if;
8954
8955 -- In a package declaration, or if no previous body, insert at end
8956 -- of list.
8957
8958 Set_Sloc (F_Node, Sloc (Last (Decls)));
8959 Insert_After (Last (Decls), F_Node);
8960 end if;
8961 end Insert_Freeze_Node_For_Instance;
8962
8963 ------------------
8964 -- Install_Body --
8965 ------------------
8966
8967 procedure Install_Body
8968 (Act_Body : Node_Id;
8969 N : Node_Id;
8970 Gen_Body : Node_Id;
8971 Gen_Decl : Node_Id)
8972 is
8973 function In_Same_Scope (Gen_Id, Act_Id : Node_Id) return Boolean;
8974 -- Check if the generic definition and the instantiation come from
8975 -- a common scope, in which case the instance must be frozen after
8976 -- the generic body.
8977
8978 function True_Sloc (N, Act_Unit : Node_Id) return Source_Ptr;
8979 -- If the instance is nested inside a generic unit, the Sloc of the
8980 -- instance indicates the place of the original definition, not the
8981 -- point of the current enclosing instance. Pending a better usage of
8982 -- Slocs to indicate instantiation places, we determine the place of
8983 -- origin of a node by finding the maximum sloc of any ancestor node.
8984 -- Why is this not equivalent to Top_Level_Location ???
8985
8986 -------------------
8987 -- In_Same_Scope --
8988 -------------------
8989
8990 function In_Same_Scope (Gen_Id, Act_Id : Node_Id) return Boolean is
8991 Act_Scop : Entity_Id := Scope (Act_Id);
8992 Gen_Scop : Entity_Id := Scope (Gen_Id);
8993
8994 begin
8995 while Act_Scop /= Standard_Standard
8996 and then Gen_Scop /= Standard_Standard
8997 loop
8998 if Act_Scop = Gen_Scop then
8999 return True;
9000 end if;
9001
9002 Act_Scop := Scope (Act_Scop);
9003 Gen_Scop := Scope (Gen_Scop);
9004 end loop;
9005
9006 return False;
9007 end In_Same_Scope;
9008
9009 ---------------
9010 -- True_Sloc --
9011 ---------------
9012
9013 function True_Sloc (N, Act_Unit : Node_Id) return Source_Ptr is
9014 N1 : Node_Id;
9015 Res : Source_Ptr;
9016
9017 begin
9018 Res := Sloc (N);
9019 N1 := N;
9020 while Present (N1) and then N1 /= Act_Unit loop
9021 if Sloc (N1) > Res then
9022 Res := Sloc (N1);
9023 end if;
9024
9025 N1 := Parent (N1);
9026 end loop;
9027
9028 return Res;
9029 end True_Sloc;
9030
9031 Act_Id : constant Entity_Id := Corresponding_Spec (Act_Body);
9032 Act_Unit : constant Node_Id := Unit (Cunit (Get_Source_Unit (N)));
9033 Gen_Id : constant Entity_Id := Corresponding_Spec (Gen_Body);
9034 Par : constant Entity_Id := Scope (Gen_Id);
9035 Gen_Unit : constant Node_Id :=
9036 Unit (Cunit (Get_Source_Unit (Gen_Decl)));
9037
9038 Body_Unit : Node_Id;
9039 F_Node : Node_Id;
9040 Must_Delay : Boolean;
9041 Orig_Body : Node_Id := Gen_Body;
9042
9043 -- Start of processing for Install_Body
9044
9045 begin
9046 -- Handle first the case of an instance with incomplete actual types.
9047 -- The instance body cannot be placed after the declaration because
9048 -- full views have not been seen yet. Any use of the non-limited views
9049 -- in the instance body requires the presence of a regular with_clause
9050 -- in the enclosing unit, and will fail if this with_clause is missing.
9051 -- We place the instance body at the beginning of the enclosing body,
9052 -- which is the unit being compiled. The freeze node for the instance
9053 -- is then placed after the instance body.
9054
9055 if not Is_Empty_Elmt_List (Incomplete_Actuals (Act_Id))
9056 and then Expander_Active
9057 and then Ekind (Scope (Act_Id)) = E_Package
9058 then
9059 declare
9060 Scop : constant Entity_Id := Scope (Act_Id);
9061 Body_Id : constant Node_Id :=
9062 Corresponding_Body (Unit_Declaration_Node (Scop));
9063
9064 begin
9065 Ensure_Freeze_Node (Act_Id);
9066 F_Node := Freeze_Node (Act_Id);
9067 if Present (Body_Id) then
9068 Set_Is_Frozen (Act_Id, False);
9069 Prepend (Act_Body, Declarations (Parent (Body_Id)));
9070 if Is_List_Member (F_Node) then
9071 Remove (F_Node);
9072 end if;
9073
9074 Insert_After (Act_Body, F_Node);
9075 end if;
9076 end;
9077 return;
9078 end if;
9079
9080 -- If the body is a subunit, the freeze point is the corresponding stub
9081 -- in the current compilation, not the subunit itself.
9082
9083 if Nkind (Parent (Gen_Body)) = N_Subunit then
9084 Orig_Body := Corresponding_Stub (Parent (Gen_Body));
9085 else
9086 Orig_Body := Gen_Body;
9087 end if;
9088
9089 Body_Unit := Unit (Cunit (Get_Source_Unit (Orig_Body)));
9090
9091 -- If the instantiation and the generic definition appear in the same
9092 -- package declaration, this is an early instantiation. If they appear
9093 -- in the same declarative part, it is an early instantiation only if
9094 -- the generic body appears textually later, and the generic body is
9095 -- also in the main unit.
9096
9097 -- If instance is nested within a subprogram, and the generic body
9098 -- is not, the instance is delayed because the enclosing body is. If
9099 -- instance and body are within the same scope, or the same subprogram
9100 -- body, indicate explicitly that the instance is delayed.
9101
9102 Must_Delay :=
9103 (Gen_Unit = Act_Unit
9104 and then (Nkind_In (Gen_Unit, N_Generic_Package_Declaration,
9105 N_Package_Declaration)
9106 or else (Gen_Unit = Body_Unit
9107 and then True_Sloc (N, Act_Unit)
9108 < Sloc (Orig_Body)))
9109 and then Is_In_Main_Unit (Original_Node (Gen_Unit))
9110 and then In_Same_Scope (Gen_Id, Act_Id));
9111
9112 -- If this is an early instantiation, the freeze node is placed after
9113 -- the generic body. Otherwise, if the generic appears in an instance,
9114 -- we cannot freeze the current instance until the outer one is frozen.
9115 -- This is only relevant if the current instance is nested within some
9116 -- inner scope not itself within the outer instance. If this scope is
9117 -- a package body in the same declarative part as the outer instance,
9118 -- then that body needs to be frozen after the outer instance. Finally,
9119 -- if no delay is needed, we place the freeze node at the end of the
9120 -- current declarative part.
9121
9122 if Expander_Active then
9123 Ensure_Freeze_Node (Act_Id);
9124 F_Node := Freeze_Node (Act_Id);
9125
9126 if Must_Delay then
9127 Insert_After (Orig_Body, F_Node);
9128
9129 elsif Is_Generic_Instance (Par)
9130 and then Present (Freeze_Node (Par))
9131 and then Scope (Act_Id) /= Par
9132 then
9133 -- Freeze instance of inner generic after instance of enclosing
9134 -- generic.
9135
9136 if In_Same_Declarative_Part (Freeze_Node (Par), N) then
9137
9138 -- Handle the following case:
9139
9140 -- package Parent_Inst is new ...
9141 -- Parent_Inst []
9142
9143 -- procedure P ... -- this body freezes Parent_Inst
9144
9145 -- package Inst is new ...
9146
9147 -- In this particular scenario, the freeze node for Inst must
9148 -- be inserted in the same manner as that of Parent_Inst,
9149 -- before the next source body or at the end of the declarative
9150 -- list (body not available). If body P did not exist and
9151 -- Parent_Inst was frozen after Inst, either by a body
9152 -- following Inst or at the end of the declarative region,
9153 -- the freeze node for Inst must be inserted after that of
9154 -- Parent_Inst. This relation is established by comparing
9155 -- the Slocs of Parent_Inst freeze node and Inst.
9156
9157 if List_Containing (Get_Package_Instantiation_Node (Par)) =
9158 List_Containing (N)
9159 and then Sloc (Freeze_Node (Par)) < Sloc (N)
9160 then
9161 Insert_Freeze_Node_For_Instance (N, F_Node);
9162 else
9163 Insert_After (Freeze_Node (Par), F_Node);
9164 end if;
9165
9166 -- Freeze package enclosing instance of inner generic after
9167 -- instance of enclosing generic.
9168
9169 elsif Nkind_In (Parent (N), N_Package_Body, N_Subprogram_Body)
9170 and then In_Same_Declarative_Part (Freeze_Node (Par), Parent (N))
9171 then
9172 declare
9173 Enclosing : Entity_Id;
9174
9175 begin
9176 Enclosing := Corresponding_Spec (Parent (N));
9177
9178 if No (Enclosing) then
9179 Enclosing := Defining_Entity (Parent (N));
9180 end if;
9181
9182 Insert_Freeze_Node_For_Instance (N, F_Node);
9183 Ensure_Freeze_Node (Enclosing);
9184
9185 if not Is_List_Member (Freeze_Node (Enclosing)) then
9186
9187 -- The enclosing context is a subunit, insert the freeze
9188 -- node after the stub.
9189
9190 if Nkind (Parent (Parent (N))) = N_Subunit then
9191 Insert_Freeze_Node_For_Instance
9192 (Corresponding_Stub (Parent (Parent (N))),
9193 Freeze_Node (Enclosing));
9194
9195 -- The enclosing context is a package with a stub body
9196 -- which has already been replaced by the real body.
9197 -- Insert the freeze node after the actual body.
9198
9199 elsif Ekind (Enclosing) = E_Package
9200 and then Present (Body_Entity (Enclosing))
9201 and then Was_Originally_Stub
9202 (Parent (Body_Entity (Enclosing)))
9203 then
9204 Insert_Freeze_Node_For_Instance
9205 (Parent (Body_Entity (Enclosing)),
9206 Freeze_Node (Enclosing));
9207
9208 -- The parent instance has been frozen before the body of
9209 -- the enclosing package, insert the freeze node after
9210 -- the body.
9211
9212 elsif List_Containing (Freeze_Node (Par)) =
9213 List_Containing (Parent (N))
9214 and then Sloc (Freeze_Node (Par)) < Sloc (Parent (N))
9215 then
9216 Insert_Freeze_Node_For_Instance
9217 (Parent (N), Freeze_Node (Enclosing));
9218
9219 else
9220 Insert_After
9221 (Freeze_Node (Par), Freeze_Node (Enclosing));
9222 end if;
9223 end if;
9224 end;
9225
9226 else
9227 Insert_Freeze_Node_For_Instance (N, F_Node);
9228 end if;
9229
9230 else
9231 Insert_Freeze_Node_For_Instance (N, F_Node);
9232 end if;
9233 end if;
9234
9235 Set_Is_Frozen (Act_Id);
9236 Insert_Before (N, Act_Body);
9237 Mark_Rewrite_Insertion (Act_Body);
9238 end Install_Body;
9239
9240 -----------------------------
9241 -- Install_Formal_Packages --
9242 -----------------------------
9243
9244 procedure Install_Formal_Packages (Par : Entity_Id) is
9245 E : Entity_Id;
9246 Gen : Entity_Id;
9247 Gen_E : Entity_Id := Empty;
9248
9249 begin
9250 E := First_Entity (Par);
9251
9252 -- If we are installing an instance parent, locate the formal packages
9253 -- of its generic parent.
9254
9255 if Is_Generic_Instance (Par) then
9256 Gen := Generic_Parent (Package_Specification (Par));
9257 Gen_E := First_Entity (Gen);
9258 end if;
9259
9260 while Present (E) loop
9261 if Ekind (E) = E_Package
9262 and then Nkind (Parent (E)) = N_Package_Renaming_Declaration
9263 then
9264 -- If this is the renaming for the parent instance, done
9265
9266 if Renamed_Object (E) = Par then
9267 exit;
9268
9269 -- The visibility of a formal of an enclosing generic is already
9270 -- correct.
9271
9272 elsif Denotes_Formal_Package (E) then
9273 null;
9274
9275 elsif Present (Associated_Formal_Package (E)) then
9276 Check_Generic_Actuals (Renamed_Object (E), True);
9277 Set_Is_Hidden (E, False);
9278
9279 -- Find formal package in generic unit that corresponds to
9280 -- (instance of) formal package in instance.
9281
9282 while Present (Gen_E) and then Chars (Gen_E) /= Chars (E) loop
9283 Next_Entity (Gen_E);
9284 end loop;
9285
9286 if Present (Gen_E) then
9287 Map_Formal_Package_Entities (Gen_E, E);
9288 end if;
9289 end if;
9290 end if;
9291
9292 Next_Entity (E);
9293
9294 if Present (Gen_E) then
9295 Next_Entity (Gen_E);
9296 end if;
9297 end loop;
9298 end Install_Formal_Packages;
9299
9300 --------------------
9301 -- Install_Parent --
9302 --------------------
9303
9304 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False) is
9305 Ancestors : constant Elist_Id := New_Elmt_List;
9306 S : constant Entity_Id := Current_Scope;
9307 Inst_Par : Entity_Id;
9308 First_Par : Entity_Id;
9309 Inst_Node : Node_Id;
9310 Gen_Par : Entity_Id;
9311 First_Gen : Entity_Id;
9312 Elmt : Elmt_Id;
9313
9314 procedure Install_Noninstance_Specs (Par : Entity_Id);
9315 -- Install the scopes of noninstance parent units ending with Par
9316
9317 procedure Install_Spec (Par : Entity_Id);
9318 -- The child unit is within the declarative part of the parent, so the
9319 -- declarations within the parent are immediately visible.
9320
9321 -------------------------------
9322 -- Install_Noninstance_Specs --
9323 -------------------------------
9324
9325 procedure Install_Noninstance_Specs (Par : Entity_Id) is
9326 begin
9327 if Present (Par)
9328 and then Par /= Standard_Standard
9329 and then not In_Open_Scopes (Par)
9330 then
9331 Install_Noninstance_Specs (Scope (Par));
9332 Install_Spec (Par);
9333 end if;
9334 end Install_Noninstance_Specs;
9335
9336 ------------------
9337 -- Install_Spec --
9338 ------------------
9339
9340 procedure Install_Spec (Par : Entity_Id) is
9341 Spec : constant Node_Id := Package_Specification (Par);
9342
9343 begin
9344 -- If this parent of the child instance is a top-level unit,
9345 -- then record the unit and its visibility for later resetting in
9346 -- Remove_Parent. We exclude units that are generic instances, as we
9347 -- only want to record this information for the ultimate top-level
9348 -- noninstance parent (is that always correct???).
9349
9350 if Scope (Par) = Standard_Standard
9351 and then not Is_Generic_Instance (Par)
9352 then
9353 Parent_Unit_Visible := Is_Immediately_Visible (Par);
9354 Instance_Parent_Unit := Par;
9355 end if;
9356
9357 -- Open the parent scope and make it and its declarations visible.
9358 -- If this point is not within a body, then only the visible
9359 -- declarations should be made visible, and installation of the
9360 -- private declarations is deferred until the appropriate point
9361 -- within analysis of the spec being instantiated (see the handling
9362 -- of parent visibility in Analyze_Package_Specification). This is
9363 -- relaxed in the case where the parent unit is Ada.Tags, to avoid
9364 -- private view problems that occur when compiling instantiations of
9365 -- a generic child of that package (Generic_Dispatching_Constructor).
9366 -- If the instance freezes a tagged type, inlinings of operations
9367 -- from Ada.Tags may need the full view of type Tag. If inlining took
9368 -- proper account of establishing visibility of inlined subprograms'
9369 -- parents then it should be possible to remove this
9370 -- special check. ???
9371
9372 Push_Scope (Par);
9373 Set_Is_Immediately_Visible (Par);
9374 Install_Visible_Declarations (Par);
9375 Set_Use (Visible_Declarations (Spec));
9376
9377 if In_Body or else Is_RTU (Par, Ada_Tags) then
9378 Install_Private_Declarations (Par);
9379 Set_Use (Private_Declarations (Spec));
9380 end if;
9381 end Install_Spec;
9382
9383 -- Start of processing for Install_Parent
9384
9385 begin
9386 -- We need to install the parent instance to compile the instantiation
9387 -- of the child, but the child instance must appear in the current
9388 -- scope. Given that we cannot place the parent above the current scope
9389 -- in the scope stack, we duplicate the current scope and unstack both
9390 -- after the instantiation is complete.
9391
9392 -- If the parent is itself the instantiation of a child unit, we must
9393 -- also stack the instantiation of its parent, and so on. Each such
9394 -- ancestor is the prefix of the name in a prior instantiation.
9395
9396 -- If this is a nested instance, the parent unit itself resolves to
9397 -- a renaming of the parent instance, whose declaration we need.
9398
9399 -- Finally, the parent may be a generic (not an instance) when the
9400 -- child unit appears as a formal package.
9401
9402 Inst_Par := P;
9403
9404 if Present (Renamed_Entity (Inst_Par)) then
9405 Inst_Par := Renamed_Entity (Inst_Par);
9406 end if;
9407
9408 First_Par := Inst_Par;
9409
9410 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
9411
9412 First_Gen := Gen_Par;
9413
9414 while Present (Gen_Par) and then Is_Child_Unit (Gen_Par) loop
9415
9416 -- Load grandparent instance as well
9417
9418 Inst_Node := Get_Package_Instantiation_Node (Inst_Par);
9419
9420 if Nkind (Name (Inst_Node)) = N_Expanded_Name then
9421 Inst_Par := Entity (Prefix (Name (Inst_Node)));
9422
9423 if Present (Renamed_Entity (Inst_Par)) then
9424 Inst_Par := Renamed_Entity (Inst_Par);
9425 end if;
9426
9427 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
9428
9429 if Present (Gen_Par) then
9430 Prepend_Elmt (Inst_Par, Ancestors);
9431
9432 else
9433 -- Parent is not the name of an instantiation
9434
9435 Install_Noninstance_Specs (Inst_Par);
9436 exit;
9437 end if;
9438
9439 else
9440 -- Previous error
9441
9442 exit;
9443 end if;
9444 end loop;
9445
9446 if Present (First_Gen) then
9447 Append_Elmt (First_Par, Ancestors);
9448 else
9449 Install_Noninstance_Specs (First_Par);
9450 end if;
9451
9452 if not Is_Empty_Elmt_List (Ancestors) then
9453 Elmt := First_Elmt (Ancestors);
9454 while Present (Elmt) loop
9455 Install_Spec (Node (Elmt));
9456 Install_Formal_Packages (Node (Elmt));
9457 Next_Elmt (Elmt);
9458 end loop;
9459 end if;
9460
9461 if not In_Body then
9462 Push_Scope (S);
9463 end if;
9464 end Install_Parent;
9465
9466 -------------------------------
9467 -- Install_Hidden_Primitives --
9468 -------------------------------
9469
9470 procedure Install_Hidden_Primitives
9471 (Prims_List : in out Elist_Id;
9472 Gen_T : Entity_Id;
9473 Act_T : Entity_Id)
9474 is
9475 Elmt : Elmt_Id;
9476 List : Elist_Id := No_Elist;
9477 Prim_G_Elmt : Elmt_Id;
9478 Prim_A_Elmt : Elmt_Id;
9479 Prim_G : Node_Id;
9480 Prim_A : Node_Id;
9481
9482 begin
9483 -- No action needed in case of serious errors because we cannot trust
9484 -- in the order of primitives
9485
9486 if Serious_Errors_Detected > 0 then
9487 return;
9488
9489 -- No action possible if we don't have available the list of primitive
9490 -- operations
9491
9492 elsif No (Gen_T)
9493 or else not Is_Record_Type (Gen_T)
9494 or else not Is_Tagged_Type (Gen_T)
9495 or else not Is_Record_Type (Act_T)
9496 or else not Is_Tagged_Type (Act_T)
9497 then
9498 return;
9499
9500 -- There is no need to handle interface types since their primitives
9501 -- cannot be hidden
9502
9503 elsif Is_Interface (Gen_T) then
9504 return;
9505 end if;
9506
9507 Prim_G_Elmt := First_Elmt (Primitive_Operations (Gen_T));
9508
9509 if not Is_Class_Wide_Type (Act_T) then
9510 Prim_A_Elmt := First_Elmt (Primitive_Operations (Act_T));
9511 else
9512 Prim_A_Elmt := First_Elmt (Primitive_Operations (Root_Type (Act_T)));
9513 end if;
9514
9515 loop
9516 -- Skip predefined primitives in the generic formal
9517
9518 while Present (Prim_G_Elmt)
9519 and then Is_Predefined_Dispatching_Operation (Node (Prim_G_Elmt))
9520 loop
9521 Next_Elmt (Prim_G_Elmt);
9522 end loop;
9523
9524 -- Skip predefined primitives in the generic actual
9525
9526 while Present (Prim_A_Elmt)
9527 and then Is_Predefined_Dispatching_Operation (Node (Prim_A_Elmt))
9528 loop
9529 Next_Elmt (Prim_A_Elmt);
9530 end loop;
9531
9532 exit when No (Prim_G_Elmt) or else No (Prim_A_Elmt);
9533
9534 Prim_G := Node (Prim_G_Elmt);
9535 Prim_A := Node (Prim_A_Elmt);
9536
9537 -- There is no need to handle interface primitives because their
9538 -- primitives are not hidden
9539
9540 exit when Present (Interface_Alias (Prim_G));
9541
9542 -- Here we install one hidden primitive
9543
9544 if Chars (Prim_G) /= Chars (Prim_A)
9545 and then Has_Suffix (Prim_A, 'P')
9546 and then Remove_Suffix (Prim_A, 'P') = Chars (Prim_G)
9547 then
9548 Set_Chars (Prim_A, Chars (Prim_G));
9549 Append_New_Elmt (Prim_A, To => List);
9550 end if;
9551
9552 Next_Elmt (Prim_A_Elmt);
9553 Next_Elmt (Prim_G_Elmt);
9554 end loop;
9555
9556 -- Append the elements to the list of temporarily visible primitives
9557 -- avoiding duplicates.
9558
9559 if Present (List) then
9560 if No (Prims_List) then
9561 Prims_List := New_Elmt_List;
9562 end if;
9563
9564 Elmt := First_Elmt (List);
9565 while Present (Elmt) loop
9566 Append_Unique_Elmt (Node (Elmt), Prims_List);
9567 Next_Elmt (Elmt);
9568 end loop;
9569 end if;
9570 end Install_Hidden_Primitives;
9571
9572 -------------------------------
9573 -- Restore_Hidden_Primitives --
9574 -------------------------------
9575
9576 procedure Restore_Hidden_Primitives (Prims_List : in out Elist_Id) is
9577 Prim_Elmt : Elmt_Id;
9578 Prim : Node_Id;
9579
9580 begin
9581 if Prims_List /= No_Elist then
9582 Prim_Elmt := First_Elmt (Prims_List);
9583 while Present (Prim_Elmt) loop
9584 Prim := Node (Prim_Elmt);
9585 Set_Chars (Prim, Add_Suffix (Prim, 'P'));
9586 Next_Elmt (Prim_Elmt);
9587 end loop;
9588
9589 Prims_List := No_Elist;
9590 end if;
9591 end Restore_Hidden_Primitives;
9592
9593 --------------------------------
9594 -- Instantiate_Formal_Package --
9595 --------------------------------
9596
9597 function Instantiate_Formal_Package
9598 (Formal : Node_Id;
9599 Actual : Node_Id;
9600 Analyzed_Formal : Node_Id) return List_Id
9601 is
9602 Loc : constant Source_Ptr := Sloc (Actual);
9603 Actual_Pack : Entity_Id;
9604 Formal_Pack : Entity_Id;
9605 Gen_Parent : Entity_Id;
9606 Decls : List_Id;
9607 Nod : Node_Id;
9608 Parent_Spec : Node_Id;
9609
9610 procedure Find_Matching_Actual
9611 (F : Node_Id;
9612 Act : in out Entity_Id);
9613 -- We need to associate each formal entity in the formal package with
9614 -- the corresponding entity in the actual package. The actual package
9615 -- has been analyzed and possibly expanded, and as a result there is
9616 -- no one-to-one correspondence between the two lists (for example,
9617 -- the actual may include subtypes, itypes, and inherited primitive
9618 -- operations, interspersed among the renaming declarations for the
9619 -- actuals). We retrieve the corresponding actual by name because each
9620 -- actual has the same name as the formal, and they do appear in the
9621 -- same order.
9622
9623 function Get_Formal_Entity (N : Node_Id) return Entity_Id;
9624 -- Retrieve entity of defining entity of generic formal parameter.
9625 -- Only the declarations of formals need to be considered when
9626 -- linking them to actuals, but the declarative list may include
9627 -- internal entities generated during analysis, and those are ignored.
9628
9629 procedure Match_Formal_Entity
9630 (Formal_Node : Node_Id;
9631 Formal_Ent : Entity_Id;
9632 Actual_Ent : Entity_Id);
9633 -- Associates the formal entity with the actual. In the case where
9634 -- Formal_Ent is a formal package, this procedure iterates through all
9635 -- of its formals and enters associations between the actuals occurring
9636 -- in the formal package's corresponding actual package (given by
9637 -- Actual_Ent) and the formal package's formal parameters. This
9638 -- procedure recurses if any of the parameters is itself a package.
9639
9640 function Is_Instance_Of
9641 (Act_Spec : Entity_Id;
9642 Gen_Anc : Entity_Id) return Boolean;
9643 -- The actual can be an instantiation of a generic within another
9644 -- instance, in which case there is no direct link from it to the
9645 -- original generic ancestor. In that case, we recognize that the
9646 -- ultimate ancestor is the same by examining names and scopes.
9647
9648 procedure Process_Nested_Formal (Formal : Entity_Id);
9649 -- If the current formal is declared with a box, its own formals are
9650 -- visible in the instance, as they were in the generic, and their
9651 -- Hidden flag must be reset. If some of these formals are themselves
9652 -- packages declared with a box, the processing must be recursive.
9653
9654 --------------------------
9655 -- Find_Matching_Actual --
9656 --------------------------
9657
9658 procedure Find_Matching_Actual
9659 (F : Node_Id;
9660 Act : in out Entity_Id)
9661 is
9662 Formal_Ent : Entity_Id;
9663
9664 begin
9665 case Nkind (Original_Node (F)) is
9666 when N_Formal_Object_Declaration
9667 | N_Formal_Type_Declaration
9668 =>
9669 Formal_Ent := Defining_Identifier (F);
9670
9671 while Chars (Act) /= Chars (Formal_Ent) loop
9672 Next_Entity (Act);
9673 end loop;
9674
9675 when N_Formal_Package_Declaration
9676 | N_Formal_Subprogram_Declaration
9677 | N_Generic_Package_Declaration
9678 | N_Package_Declaration
9679 =>
9680 Formal_Ent := Defining_Entity (F);
9681
9682 while Chars (Act) /= Chars (Formal_Ent) loop
9683 Next_Entity (Act);
9684 end loop;
9685
9686 when others =>
9687 raise Program_Error;
9688 end case;
9689 end Find_Matching_Actual;
9690
9691 -------------------------
9692 -- Match_Formal_Entity --
9693 -------------------------
9694
9695 procedure Match_Formal_Entity
9696 (Formal_Node : Node_Id;
9697 Formal_Ent : Entity_Id;
9698 Actual_Ent : Entity_Id)
9699 is
9700 Act_Pkg : Entity_Id;
9701
9702 begin
9703 Set_Instance_Of (Formal_Ent, Actual_Ent);
9704
9705 if Ekind (Actual_Ent) = E_Package then
9706
9707 -- Record associations for each parameter
9708
9709 Act_Pkg := Actual_Ent;
9710
9711 declare
9712 A_Ent : Entity_Id := First_Entity (Act_Pkg);
9713 F_Ent : Entity_Id;
9714 F_Node : Node_Id;
9715
9716 Gen_Decl : Node_Id;
9717 Formals : List_Id;
9718 Actual : Entity_Id;
9719
9720 begin
9721 -- Retrieve the actual given in the formal package declaration
9722
9723 Actual := Entity (Name (Original_Node (Formal_Node)));
9724
9725 -- The actual in the formal package declaration may be a
9726 -- renamed generic package, in which case we want to retrieve
9727 -- the original generic in order to traverse its formal part.
9728
9729 if Present (Renamed_Entity (Actual)) then
9730 Gen_Decl := Unit_Declaration_Node (Renamed_Entity (Actual));
9731 else
9732 Gen_Decl := Unit_Declaration_Node (Actual);
9733 end if;
9734
9735 Formals := Generic_Formal_Declarations (Gen_Decl);
9736
9737 if Present (Formals) then
9738 F_Node := First_Non_Pragma (Formals);
9739 else
9740 F_Node := Empty;
9741 end if;
9742
9743 while Present (A_Ent)
9744 and then Present (F_Node)
9745 and then A_Ent /= First_Private_Entity (Act_Pkg)
9746 loop
9747 F_Ent := Get_Formal_Entity (F_Node);
9748
9749 if Present (F_Ent) then
9750
9751 -- This is a formal of the original package. Record
9752 -- association and recurse.
9753
9754 Find_Matching_Actual (F_Node, A_Ent);
9755 Match_Formal_Entity (F_Node, F_Ent, A_Ent);
9756 Next_Entity (A_Ent);
9757 end if;
9758
9759 Next_Non_Pragma (F_Node);
9760 end loop;
9761 end;
9762 end if;
9763 end Match_Formal_Entity;
9764
9765 -----------------------
9766 -- Get_Formal_Entity --
9767 -----------------------
9768
9769 function Get_Formal_Entity (N : Node_Id) return Entity_Id is
9770 Kind : constant Node_Kind := Nkind (Original_Node (N));
9771 begin
9772 case Kind is
9773 when N_Formal_Object_Declaration =>
9774 return Defining_Identifier (N);
9775
9776 when N_Formal_Type_Declaration =>
9777 return Defining_Identifier (N);
9778
9779 when N_Formal_Subprogram_Declaration =>
9780 return Defining_Unit_Name (Specification (N));
9781
9782 when N_Formal_Package_Declaration =>
9783 return Defining_Identifier (Original_Node (N));
9784
9785 when N_Generic_Package_Declaration =>
9786 return Defining_Identifier (Original_Node (N));
9787
9788 -- All other declarations are introduced by semantic analysis and
9789 -- have no match in the actual.
9790
9791 when others =>
9792 return Empty;
9793 end case;
9794 end Get_Formal_Entity;
9795
9796 --------------------
9797 -- Is_Instance_Of --
9798 --------------------
9799
9800 function Is_Instance_Of
9801 (Act_Spec : Entity_Id;
9802 Gen_Anc : Entity_Id) return Boolean
9803 is
9804 Gen_Par : constant Entity_Id := Generic_Parent (Act_Spec);
9805
9806 begin
9807 if No (Gen_Par) then
9808 return False;
9809
9810 -- Simplest case: the generic parent of the actual is the formal
9811
9812 elsif Gen_Par = Gen_Anc then
9813 return True;
9814
9815 elsif Chars (Gen_Par) /= Chars (Gen_Anc) then
9816 return False;
9817
9818 -- The actual may be obtained through several instantiations. Its
9819 -- scope must itself be an instance of a generic declared in the
9820 -- same scope as the formal. Any other case is detected above.
9821
9822 elsif not Is_Generic_Instance (Scope (Gen_Par)) then
9823 return False;
9824
9825 else
9826 return Generic_Parent (Parent (Scope (Gen_Par))) = Scope (Gen_Anc);
9827 end if;
9828 end Is_Instance_Of;
9829
9830 ---------------------------
9831 -- Process_Nested_Formal --
9832 ---------------------------
9833
9834 procedure Process_Nested_Formal (Formal : Entity_Id) is
9835 Ent : Entity_Id;
9836
9837 begin
9838 if Present (Associated_Formal_Package (Formal))
9839 and then Box_Present (Parent (Associated_Formal_Package (Formal)))
9840 then
9841 Ent := First_Entity (Formal);
9842 while Present (Ent) loop
9843 Set_Is_Hidden (Ent, False);
9844 Set_Is_Visible_Formal (Ent);
9845 Set_Is_Potentially_Use_Visible
9846 (Ent, Is_Potentially_Use_Visible (Formal));
9847
9848 if Ekind (Ent) = E_Package then
9849 exit when Renamed_Entity (Ent) = Renamed_Entity (Formal);
9850 Process_Nested_Formal (Ent);
9851 end if;
9852
9853 Next_Entity (Ent);
9854 end loop;
9855 end if;
9856 end Process_Nested_Formal;
9857
9858 -- Start of processing for Instantiate_Formal_Package
9859
9860 begin
9861 Analyze (Actual);
9862
9863 if not Is_Entity_Name (Actual)
9864 or else Ekind (Entity (Actual)) /= E_Package
9865 then
9866 Error_Msg_N
9867 ("expect package instance to instantiate formal", Actual);
9868 Abandon_Instantiation (Actual);
9869 raise Program_Error;
9870
9871 else
9872 Actual_Pack := Entity (Actual);
9873 Set_Is_Instantiated (Actual_Pack);
9874
9875 -- The actual may be a renamed package, or an outer generic formal
9876 -- package whose instantiation is converted into a renaming.
9877
9878 if Present (Renamed_Object (Actual_Pack)) then
9879 Actual_Pack := Renamed_Object (Actual_Pack);
9880 end if;
9881
9882 if Nkind (Analyzed_Formal) = N_Formal_Package_Declaration then
9883 Gen_Parent := Get_Instance_Of (Entity (Name (Analyzed_Formal)));
9884 Formal_Pack := Defining_Identifier (Analyzed_Formal);
9885 else
9886 Gen_Parent :=
9887 Generic_Parent (Specification (Analyzed_Formal));
9888 Formal_Pack :=
9889 Defining_Unit_Name (Specification (Analyzed_Formal));
9890 end if;
9891
9892 if Nkind (Parent (Actual_Pack)) = N_Defining_Program_Unit_Name then
9893 Parent_Spec := Package_Specification (Actual_Pack);
9894 else
9895 Parent_Spec := Parent (Actual_Pack);
9896 end if;
9897
9898 if Gen_Parent = Any_Id then
9899 Error_Msg_N
9900 ("previous error in declaration of formal package", Actual);
9901 Abandon_Instantiation (Actual);
9902
9903 elsif
9904 Is_Instance_Of (Parent_Spec, Get_Instance_Of (Gen_Parent))
9905 then
9906 null;
9907
9908 else
9909 Error_Msg_NE
9910 ("actual parameter must be instance of&", Actual, Gen_Parent);
9911 Abandon_Instantiation (Actual);
9912 end if;
9913
9914 Set_Instance_Of (Defining_Identifier (Formal), Actual_Pack);
9915 Map_Formal_Package_Entities (Formal_Pack, Actual_Pack);
9916
9917 Nod :=
9918 Make_Package_Renaming_Declaration (Loc,
9919 Defining_Unit_Name => New_Copy (Defining_Identifier (Formal)),
9920 Name => New_Occurrence_Of (Actual_Pack, Loc));
9921
9922 Set_Associated_Formal_Package
9923 (Defining_Unit_Name (Nod), Defining_Identifier (Formal));
9924 Decls := New_List (Nod);
9925
9926 -- If the formal F has a box, then the generic declarations are
9927 -- visible in the generic G. In an instance of G, the corresponding
9928 -- entities in the actual for F (which are the actuals for the
9929 -- instantiation of the generic that F denotes) must also be made
9930 -- visible for analysis of the current instance. On exit from the
9931 -- current instance, those entities are made private again. If the
9932 -- actual is currently in use, these entities are also use-visible.
9933
9934 -- The loop through the actual entities also steps through the formal
9935 -- entities and enters associations from formals to actuals into the
9936 -- renaming map. This is necessary to properly handle checking of
9937 -- actual parameter associations for later formals that depend on
9938 -- actuals declared in the formal package.
9939
9940 -- In Ada 2005, partial parameterization requires that we make
9941 -- visible the actuals corresponding to formals that were defaulted
9942 -- in the formal package. There formals are identified because they
9943 -- remain formal generics within the formal package, rather than
9944 -- being renamings of the actuals supplied.
9945
9946 declare
9947 Gen_Decl : constant Node_Id :=
9948 Unit_Declaration_Node (Gen_Parent);
9949 Formals : constant List_Id :=
9950 Generic_Formal_Declarations (Gen_Decl);
9951
9952 Actual_Ent : Entity_Id;
9953 Actual_Of_Formal : Node_Id;
9954 Formal_Node : Node_Id;
9955 Formal_Ent : Entity_Id;
9956
9957 begin
9958 if Present (Formals) then
9959 Formal_Node := First_Non_Pragma (Formals);
9960 else
9961 Formal_Node := Empty;
9962 end if;
9963
9964 Actual_Ent := First_Entity (Actual_Pack);
9965 Actual_Of_Formal :=
9966 First (Visible_Declarations (Specification (Analyzed_Formal)));
9967 while Present (Actual_Ent)
9968 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
9969 loop
9970 if Present (Formal_Node) then
9971 Formal_Ent := Get_Formal_Entity (Formal_Node);
9972
9973 if Present (Formal_Ent) then
9974 Find_Matching_Actual (Formal_Node, Actual_Ent);
9975 Match_Formal_Entity (Formal_Node, Formal_Ent, Actual_Ent);
9976
9977 -- We iterate at the same time over the actuals of the
9978 -- local package created for the formal, to determine
9979 -- which one of the formals of the original generic were
9980 -- defaulted in the formal. The corresponding actual
9981 -- entities are visible in the enclosing instance.
9982
9983 if Box_Present (Formal)
9984 or else
9985 (Present (Actual_Of_Formal)
9986 and then
9987 Is_Generic_Formal
9988 (Get_Formal_Entity (Actual_Of_Formal)))
9989 then
9990 Set_Is_Hidden (Actual_Ent, False);
9991 Set_Is_Visible_Formal (Actual_Ent);
9992 Set_Is_Potentially_Use_Visible
9993 (Actual_Ent, In_Use (Actual_Pack));
9994
9995 if Ekind (Actual_Ent) = E_Package then
9996 Process_Nested_Formal (Actual_Ent);
9997 end if;
9998
9999 else
10000 Set_Is_Hidden (Actual_Ent);
10001 Set_Is_Potentially_Use_Visible (Actual_Ent, False);
10002 end if;
10003 end if;
10004
10005 Next_Non_Pragma (Formal_Node);
10006 Next (Actual_Of_Formal);
10007
10008 else
10009 -- No further formals to match, but the generic part may
10010 -- contain inherited operation that are not hidden in the
10011 -- enclosing instance.
10012
10013 Next_Entity (Actual_Ent);
10014 end if;
10015 end loop;
10016
10017 -- Inherited subprograms generated by formal derived types are
10018 -- also visible if the types are.
10019
10020 Actual_Ent := First_Entity (Actual_Pack);
10021 while Present (Actual_Ent)
10022 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
10023 loop
10024 if Is_Overloadable (Actual_Ent)
10025 and then
10026 Nkind (Parent (Actual_Ent)) = N_Subtype_Declaration
10027 and then
10028 not Is_Hidden (Defining_Identifier (Parent (Actual_Ent)))
10029 then
10030 Set_Is_Hidden (Actual_Ent, False);
10031 Set_Is_Potentially_Use_Visible
10032 (Actual_Ent, In_Use (Actual_Pack));
10033 end if;
10034
10035 Next_Entity (Actual_Ent);
10036 end loop;
10037 end;
10038
10039 -- If the formal is not declared with a box, reanalyze it as an
10040 -- abbreviated instantiation, to verify the matching rules of 12.7.
10041 -- The actual checks are performed after the generic associations
10042 -- have been analyzed, to guarantee the same visibility for this
10043 -- instantiation and for the actuals.
10044
10045 -- In Ada 2005, the generic associations for the formal can include
10046 -- defaulted parameters. These are ignored during check. This
10047 -- internal instantiation is removed from the tree after conformance
10048 -- checking, because it contains formal declarations for those
10049 -- defaulted parameters, and those should not reach the back-end.
10050
10051 if not Box_Present (Formal) then
10052 declare
10053 I_Pack : constant Entity_Id :=
10054 Make_Temporary (Sloc (Actual), 'P');
10055
10056 begin
10057 Set_Is_Internal (I_Pack);
10058
10059 Append_To (Decls,
10060 Make_Package_Instantiation (Sloc (Actual),
10061 Defining_Unit_Name => I_Pack,
10062 Name =>
10063 New_Occurrence_Of
10064 (Get_Instance_Of (Gen_Parent), Sloc (Actual)),
10065 Generic_Associations => Generic_Associations (Formal)));
10066 end;
10067 end if;
10068
10069 return Decls;
10070 end if;
10071 end Instantiate_Formal_Package;
10072
10073 -----------------------------------
10074 -- Instantiate_Formal_Subprogram --
10075 -----------------------------------
10076
10077 function Instantiate_Formal_Subprogram
10078 (Formal : Node_Id;
10079 Actual : Node_Id;
10080 Analyzed_Formal : Node_Id) return Node_Id
10081 is
10082 Analyzed_S : constant Entity_Id :=
10083 Defining_Unit_Name (Specification (Analyzed_Formal));
10084 Formal_Sub : constant Entity_Id :=
10085 Defining_Unit_Name (Specification (Formal));
10086
10087 function From_Parent_Scope (Subp : Entity_Id) return Boolean;
10088 -- If the generic is a child unit, the parent has been installed on the
10089 -- scope stack, but a default subprogram cannot resolve to something
10090 -- on the parent because that parent is not really part of the visible
10091 -- context (it is there to resolve explicit local entities). If the
10092 -- default has resolved in this way, we remove the entity from immediate
10093 -- visibility and analyze the node again to emit an error message or
10094 -- find another visible candidate.
10095
10096 procedure Valid_Actual_Subprogram (Act : Node_Id);
10097 -- Perform legality check and raise exception on failure
10098
10099 -----------------------
10100 -- From_Parent_Scope --
10101 -----------------------
10102
10103 function From_Parent_Scope (Subp : Entity_Id) return Boolean is
10104 Gen_Scope : Node_Id;
10105
10106 begin
10107 Gen_Scope := Scope (Analyzed_S);
10108 while Present (Gen_Scope) and then Is_Child_Unit (Gen_Scope) loop
10109 if Scope (Subp) = Scope (Gen_Scope) then
10110 return True;
10111 end if;
10112
10113 Gen_Scope := Scope (Gen_Scope);
10114 end loop;
10115
10116 return False;
10117 end From_Parent_Scope;
10118
10119 -----------------------------
10120 -- Valid_Actual_Subprogram --
10121 -----------------------------
10122
10123 procedure Valid_Actual_Subprogram (Act : Node_Id) is
10124 Act_E : Entity_Id;
10125
10126 begin
10127 if Is_Entity_Name (Act) then
10128 Act_E := Entity (Act);
10129
10130 elsif Nkind (Act) = N_Selected_Component
10131 and then Is_Entity_Name (Selector_Name (Act))
10132 then
10133 Act_E := Entity (Selector_Name (Act));
10134
10135 else
10136 Act_E := Empty;
10137 end if;
10138
10139 if (Present (Act_E) and then Is_Overloadable (Act_E))
10140 or else Nkind_In (Act, N_Attribute_Reference,
10141 N_Indexed_Component,
10142 N_Character_Literal,
10143 N_Explicit_Dereference)
10144 then
10145 return;
10146 end if;
10147
10148 Error_Msg_NE
10149 ("expect subprogram or entry name in instantiation of &",
10150 Instantiation_Node, Formal_Sub);
10151 Abandon_Instantiation (Instantiation_Node);
10152 end Valid_Actual_Subprogram;
10153
10154 -- Local variables
10155
10156 Decl_Node : Node_Id;
10157 Loc : Source_Ptr;
10158 Nam : Node_Id;
10159 New_Spec : Node_Id;
10160 New_Subp : Entity_Id;
10161
10162 -- Start of processing for Instantiate_Formal_Subprogram
10163
10164 begin
10165 New_Spec := New_Copy_Tree (Specification (Formal));
10166
10167 -- The tree copy has created the proper instantiation sloc for the
10168 -- new specification. Use this location for all other constructed
10169 -- declarations.
10170
10171 Loc := Sloc (Defining_Unit_Name (New_Spec));
10172
10173 -- Create new entity for the actual (New_Copy_Tree does not), and
10174 -- indicate that it is an actual.
10175
10176 New_Subp := Make_Defining_Identifier (Loc, Chars (Formal_Sub));
10177 Set_Ekind (New_Subp, Ekind (Analyzed_S));
10178 Set_Is_Generic_Actual_Subprogram (New_Subp);
10179 Set_Defining_Unit_Name (New_Spec, New_Subp);
10180
10181 -- Create new entities for the each of the formals in the specification
10182 -- of the renaming declaration built for the actual.
10183
10184 if Present (Parameter_Specifications (New_Spec)) then
10185 declare
10186 F : Node_Id;
10187 F_Id : Entity_Id;
10188
10189 begin
10190 F := First (Parameter_Specifications (New_Spec));
10191 while Present (F) loop
10192 F_Id := Defining_Identifier (F);
10193
10194 Set_Defining_Identifier (F,
10195 Make_Defining_Identifier (Sloc (F_Id), Chars (F_Id)));
10196 Next (F);
10197 end loop;
10198 end;
10199 end if;
10200
10201 -- Find entity of actual. If the actual is an attribute reference, it
10202 -- cannot be resolved here (its formal is missing) but is handled
10203 -- instead in Attribute_Renaming. If the actual is overloaded, it is
10204 -- fully resolved subsequently, when the renaming declaration for the
10205 -- formal is analyzed. If it is an explicit dereference, resolve the
10206 -- prefix but not the actual itself, to prevent interpretation as call.
10207
10208 if Present (Actual) then
10209 Loc := Sloc (Actual);
10210 Set_Sloc (New_Spec, Loc);
10211
10212 if Nkind (Actual) = N_Operator_Symbol then
10213 Find_Direct_Name (Actual);
10214
10215 elsif Nkind (Actual) = N_Explicit_Dereference then
10216 Analyze (Prefix (Actual));
10217
10218 elsif Nkind (Actual) /= N_Attribute_Reference then
10219 Analyze (Actual);
10220 end if;
10221
10222 Valid_Actual_Subprogram (Actual);
10223 Nam := Actual;
10224
10225 elsif Present (Default_Name (Formal)) then
10226 if not Nkind_In (Default_Name (Formal), N_Attribute_Reference,
10227 N_Selected_Component,
10228 N_Indexed_Component,
10229 N_Character_Literal)
10230 and then Present (Entity (Default_Name (Formal)))
10231 then
10232 Nam := New_Occurrence_Of (Entity (Default_Name (Formal)), Loc);
10233 else
10234 Nam := New_Copy (Default_Name (Formal));
10235 Set_Sloc (Nam, Loc);
10236 end if;
10237
10238 elsif Box_Present (Formal) then
10239
10240 -- Actual is resolved at the point of instantiation. Create an
10241 -- identifier or operator with the same name as the formal.
10242
10243 if Nkind (Formal_Sub) = N_Defining_Operator_Symbol then
10244 Nam :=
10245 Make_Operator_Symbol (Loc,
10246 Chars => Chars (Formal_Sub),
10247 Strval => No_String);
10248 else
10249 Nam := Make_Identifier (Loc, Chars (Formal_Sub));
10250 end if;
10251
10252 elsif Nkind (Specification (Formal)) = N_Procedure_Specification
10253 and then Null_Present (Specification (Formal))
10254 then
10255 -- Generate null body for procedure, for use in the instance
10256
10257 Decl_Node :=
10258 Make_Subprogram_Body (Loc,
10259 Specification => New_Spec,
10260 Declarations => New_List,
10261 Handled_Statement_Sequence =>
10262 Make_Handled_Sequence_Of_Statements (Loc,
10263 Statements => New_List (Make_Null_Statement (Loc))));
10264
10265 Set_Is_Intrinsic_Subprogram (Defining_Unit_Name (New_Spec));
10266 return Decl_Node;
10267
10268 else
10269 Error_Msg_Sloc := Sloc (Scope (Analyzed_S));
10270 Error_Msg_NE
10271 ("missing actual&", Instantiation_Node, Formal_Sub);
10272 Error_Msg_NE
10273 ("\in instantiation of & declared#",
10274 Instantiation_Node, Scope (Analyzed_S));
10275 Abandon_Instantiation (Instantiation_Node);
10276 end if;
10277
10278 Decl_Node :=
10279 Make_Subprogram_Renaming_Declaration (Loc,
10280 Specification => New_Spec,
10281 Name => Nam);
10282
10283 -- If we do not have an actual and the formal specified <> then set to
10284 -- get proper default.
10285
10286 if No (Actual) and then Box_Present (Formal) then
10287 Set_From_Default (Decl_Node);
10288 end if;
10289
10290 -- Gather possible interpretations for the actual before analyzing the
10291 -- instance. If overloaded, it will be resolved when analyzing the
10292 -- renaming declaration.
10293
10294 if Box_Present (Formal) and then No (Actual) then
10295 Analyze (Nam);
10296
10297 if Is_Child_Unit (Scope (Analyzed_S))
10298 and then Present (Entity (Nam))
10299 then
10300 if not Is_Overloaded (Nam) then
10301 if From_Parent_Scope (Entity (Nam)) then
10302 Set_Is_Immediately_Visible (Entity (Nam), False);
10303 Set_Entity (Nam, Empty);
10304 Set_Etype (Nam, Empty);
10305
10306 Analyze (Nam);
10307 Set_Is_Immediately_Visible (Entity (Nam));
10308 end if;
10309
10310 else
10311 declare
10312 I : Interp_Index;
10313 It : Interp;
10314
10315 begin
10316 Get_First_Interp (Nam, I, It);
10317 while Present (It.Nam) loop
10318 if From_Parent_Scope (It.Nam) then
10319 Remove_Interp (I);
10320 end if;
10321
10322 Get_Next_Interp (I, It);
10323 end loop;
10324 end;
10325 end if;
10326 end if;
10327 end if;
10328
10329 -- The generic instantiation freezes the actual. This can only be done
10330 -- once the actual is resolved, in the analysis of the renaming
10331 -- declaration. To make the formal subprogram entity available, we set
10332 -- Corresponding_Formal_Spec to point to the formal subprogram entity.
10333 -- This is also needed in Analyze_Subprogram_Renaming for the processing
10334 -- of formal abstract subprograms.
10335
10336 Set_Corresponding_Formal_Spec (Decl_Node, Analyzed_S);
10337
10338 -- We cannot analyze the renaming declaration, and thus find the actual,
10339 -- until all the actuals are assembled in the instance. For subsequent
10340 -- checks of other actuals, indicate the node that will hold the
10341 -- instance of this formal.
10342
10343 Set_Instance_Of (Analyzed_S, Nam);
10344
10345 if Nkind (Actual) = N_Selected_Component
10346 and then Is_Task_Type (Etype (Prefix (Actual)))
10347 and then not Is_Frozen (Etype (Prefix (Actual)))
10348 then
10349 -- The renaming declaration will create a body, which must appear
10350 -- outside of the instantiation, We move the renaming declaration
10351 -- out of the instance, and create an additional renaming inside,
10352 -- to prevent freezing anomalies.
10353
10354 declare
10355 Anon_Id : constant Entity_Id := Make_Temporary (Loc, 'E');
10356
10357 begin
10358 Set_Defining_Unit_Name (New_Spec, Anon_Id);
10359 Insert_Before (Instantiation_Node, Decl_Node);
10360 Analyze (Decl_Node);
10361
10362 -- Now create renaming within the instance
10363
10364 Decl_Node :=
10365 Make_Subprogram_Renaming_Declaration (Loc,
10366 Specification => New_Copy_Tree (New_Spec),
10367 Name => New_Occurrence_Of (Anon_Id, Loc));
10368
10369 Set_Defining_Unit_Name (Specification (Decl_Node),
10370 Make_Defining_Identifier (Loc, Chars (Formal_Sub)));
10371 end;
10372 end if;
10373
10374 return Decl_Node;
10375 end Instantiate_Formal_Subprogram;
10376
10377 ------------------------
10378 -- Instantiate_Object --
10379 ------------------------
10380
10381 function Instantiate_Object
10382 (Formal : Node_Id;
10383 Actual : Node_Id;
10384 Analyzed_Formal : Node_Id) return List_Id
10385 is
10386 Gen_Obj : constant Entity_Id := Defining_Identifier (Formal);
10387 A_Gen_Obj : constant Entity_Id :=
10388 Defining_Identifier (Analyzed_Formal);
10389 Acc_Def : Node_Id := Empty;
10390 Act_Assoc : constant Node_Id := Parent (Actual);
10391 Actual_Decl : Node_Id := Empty;
10392 Decl_Node : Node_Id;
10393 Def : Node_Id;
10394 Ftyp : Entity_Id;
10395 List : constant List_Id := New_List;
10396 Loc : constant Source_Ptr := Sloc (Actual);
10397 Orig_Ftyp : constant Entity_Id := Etype (A_Gen_Obj);
10398 Subt_Decl : Node_Id := Empty;
10399 Subt_Mark : Node_Id := Empty;
10400
10401 function Copy_Access_Def return Node_Id;
10402 -- If formal is an anonymous access, copy access definition of formal
10403 -- for generated object declaration.
10404
10405 ---------------------
10406 -- Copy_Access_Def --
10407 ---------------------
10408
10409 function Copy_Access_Def return Node_Id is
10410 begin
10411 Def := New_Copy_Tree (Acc_Def);
10412
10413 -- In addition, if formal is an access to subprogram we need to
10414 -- generate new formals for the signature of the default, so that
10415 -- the tree is properly formatted for ASIS use.
10416
10417 if Present (Access_To_Subprogram_Definition (Acc_Def)) then
10418 declare
10419 Par_Spec : Node_Id;
10420 begin
10421 Par_Spec :=
10422 First (Parameter_Specifications
10423 (Access_To_Subprogram_Definition (Def)));
10424 while Present (Par_Spec) loop
10425 Set_Defining_Identifier (Par_Spec,
10426 Make_Defining_Identifier (Sloc (Acc_Def),
10427 Chars => Chars (Defining_Identifier (Par_Spec))));
10428 Next (Par_Spec);
10429 end loop;
10430 end;
10431 end if;
10432
10433 return Def;
10434 end Copy_Access_Def;
10435
10436 -- Start of processing for Instantiate_Object
10437
10438 begin
10439 -- Formal may be an anonymous access
10440
10441 if Present (Subtype_Mark (Formal)) then
10442 Subt_Mark := Subtype_Mark (Formal);
10443 else
10444 Check_Access_Definition (Formal);
10445 Acc_Def := Access_Definition (Formal);
10446 end if;
10447
10448 -- Sloc for error message on missing actual
10449
10450 Error_Msg_Sloc := Sloc (Scope (A_Gen_Obj));
10451
10452 if Get_Instance_Of (Gen_Obj) /= Gen_Obj then
10453 Error_Msg_N ("duplicate instantiation of generic parameter", Actual);
10454 end if;
10455
10456 Set_Parent (List, Parent (Actual));
10457
10458 -- OUT present
10459
10460 if Out_Present (Formal) then
10461
10462 -- An IN OUT generic actual must be a name. The instantiation is a
10463 -- renaming declaration. The actual is the name being renamed. We
10464 -- use the actual directly, rather than a copy, because it is not
10465 -- used further in the list of actuals, and because a copy or a use
10466 -- of relocate_node is incorrect if the instance is nested within a
10467 -- generic. In order to simplify ASIS searches, the Generic_Parent
10468 -- field links the declaration to the generic association.
10469
10470 if No (Actual) then
10471 Error_Msg_NE
10472 ("missing actual &",
10473 Instantiation_Node, Gen_Obj);
10474 Error_Msg_NE
10475 ("\in instantiation of & declared#",
10476 Instantiation_Node, Scope (A_Gen_Obj));
10477 Abandon_Instantiation (Instantiation_Node);
10478 end if;
10479
10480 if Present (Subt_Mark) then
10481 Decl_Node :=
10482 Make_Object_Renaming_Declaration (Loc,
10483 Defining_Identifier => New_Copy (Gen_Obj),
10484 Subtype_Mark => New_Copy_Tree (Subt_Mark),
10485 Name => Actual);
10486
10487 else pragma Assert (Present (Acc_Def));
10488 Decl_Node :=
10489 Make_Object_Renaming_Declaration (Loc,
10490 Defining_Identifier => New_Copy (Gen_Obj),
10491 Access_Definition => New_Copy_Tree (Acc_Def),
10492 Name => Actual);
10493 end if;
10494
10495 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
10496
10497 -- The analysis of the actual may produce Insert_Action nodes, so
10498 -- the declaration must have a context in which to attach them.
10499
10500 Append (Decl_Node, List);
10501 Analyze (Actual);
10502
10503 -- Return if the analysis of the actual reported some error
10504
10505 if Etype (Actual) = Any_Type then
10506 return List;
10507 end if;
10508
10509 -- This check is performed here because Analyze_Object_Renaming will
10510 -- not check it when Comes_From_Source is False. Note though that the
10511 -- check for the actual being the name of an object will be performed
10512 -- in Analyze_Object_Renaming.
10513
10514 if Is_Object_Reference (Actual)
10515 and then Is_Dependent_Component_Of_Mutable_Object (Actual)
10516 then
10517 Error_Msg_N
10518 ("illegal discriminant-dependent component for in out parameter",
10519 Actual);
10520 end if;
10521
10522 -- The actual has to be resolved in order to check that it is a
10523 -- variable (due to cases such as F (1), where F returns access to
10524 -- an array, and for overloaded prefixes).
10525
10526 Ftyp := Get_Instance_Of (Etype (A_Gen_Obj));
10527
10528 -- If the type of the formal is not itself a formal, and the current
10529 -- unit is a child unit, the formal type must be declared in a
10530 -- parent, and must be retrieved by visibility.
10531
10532 if Ftyp = Orig_Ftyp
10533 and then Is_Generic_Unit (Scope (Ftyp))
10534 and then Is_Child_Unit (Scope (A_Gen_Obj))
10535 then
10536 declare
10537 Temp : constant Node_Id :=
10538 New_Copy_Tree (Subtype_Mark (Analyzed_Formal));
10539 begin
10540 Set_Entity (Temp, Empty);
10541 Find_Type (Temp);
10542 Ftyp := Entity (Temp);
10543 end;
10544 end if;
10545
10546 if Is_Private_Type (Ftyp)
10547 and then not Is_Private_Type (Etype (Actual))
10548 and then (Base_Type (Full_View (Ftyp)) = Base_Type (Etype (Actual))
10549 or else Base_Type (Etype (Actual)) = Ftyp)
10550 then
10551 -- If the actual has the type of the full view of the formal, or
10552 -- else a non-private subtype of the formal, then the visibility
10553 -- of the formal type has changed. Add to the actuals a subtype
10554 -- declaration that will force the exchange of views in the body
10555 -- of the instance as well.
10556
10557 Subt_Decl :=
10558 Make_Subtype_Declaration (Loc,
10559 Defining_Identifier => Make_Temporary (Loc, 'P'),
10560 Subtype_Indication => New_Occurrence_Of (Ftyp, Loc));
10561
10562 Prepend (Subt_Decl, List);
10563
10564 Prepend_Elmt (Full_View (Ftyp), Exchanged_Views);
10565 Exchange_Declarations (Ftyp);
10566 end if;
10567
10568 Resolve (Actual, Ftyp);
10569
10570 if not Denotes_Variable (Actual) then
10571 Error_Msg_NE ("actual for& must be a variable", Actual, Gen_Obj);
10572
10573 elsif Base_Type (Ftyp) /= Base_Type (Etype (Actual)) then
10574
10575 -- Ada 2005 (AI-423): For a generic formal object of mode in out,
10576 -- the type of the actual shall resolve to a specific anonymous
10577 -- access type.
10578
10579 if Ada_Version < Ada_2005
10580 or else Ekind (Base_Type (Ftyp)) /=
10581 E_Anonymous_Access_Type
10582 or else Ekind (Base_Type (Etype (Actual))) /=
10583 E_Anonymous_Access_Type
10584 then
10585 Error_Msg_NE
10586 ("type of actual does not match type of&", Actual, Gen_Obj);
10587 end if;
10588 end if;
10589
10590 Note_Possible_Modification (Actual, Sure => True);
10591
10592 -- Check for instantiation of atomic/volatile actual for
10593 -- non-atomic/volatile formal (RM C.6 (12)).
10594
10595 if Is_Atomic_Object (Actual) and then not Is_Atomic (Orig_Ftyp) then
10596 Error_Msg_N
10597 ("cannot instantiate non-atomic formal object "
10598 & "with atomic actual", Actual);
10599
10600 elsif Is_Volatile_Object (Actual) and then not Is_Volatile (Orig_Ftyp)
10601 then
10602 Error_Msg_N
10603 ("cannot instantiate non-volatile formal object "
10604 & "with volatile actual", Actual);
10605 end if;
10606
10607 -- Formal in-parameter
10608
10609 else
10610 -- The instantiation of a generic formal in-parameter is constant
10611 -- declaration. The actual is the expression for that declaration.
10612 -- Its type is a full copy of the type of the formal. This may be
10613 -- an access to subprogram, for which we need to generate entities
10614 -- for the formals in the new signature.
10615
10616 if Present (Actual) then
10617 if Present (Subt_Mark) then
10618 Def := New_Copy_Tree (Subt_Mark);
10619 else pragma Assert (Present (Acc_Def));
10620 Def := Copy_Access_Def;
10621 end if;
10622
10623 Decl_Node :=
10624 Make_Object_Declaration (Loc,
10625 Defining_Identifier => New_Copy (Gen_Obj),
10626 Constant_Present => True,
10627 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
10628 Object_Definition => Def,
10629 Expression => Actual);
10630
10631 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
10632
10633 -- A generic formal object of a tagged type is defined to be
10634 -- aliased so the new constant must also be treated as aliased.
10635
10636 if Is_Tagged_Type (Etype (A_Gen_Obj)) then
10637 Set_Aliased_Present (Decl_Node);
10638 end if;
10639
10640 Append (Decl_Node, List);
10641
10642 -- No need to repeat (pre-)analysis of some expression nodes
10643 -- already handled in Preanalyze_Actuals.
10644
10645 if Nkind (Actual) /= N_Allocator then
10646 Analyze (Actual);
10647
10648 -- Return if the analysis of the actual reported some error
10649
10650 if Etype (Actual) = Any_Type then
10651 return List;
10652 end if;
10653 end if;
10654
10655 declare
10656 Formal_Type : constant Entity_Id := Etype (A_Gen_Obj);
10657 Typ : Entity_Id;
10658
10659 begin
10660 Typ := Get_Instance_Of (Formal_Type);
10661
10662 -- If the actual appears in the current or an enclosing scope,
10663 -- use its type directly. This is relevant if it has an actual
10664 -- subtype that is distinct from its nominal one. This cannot
10665 -- be done in general because the type of the actual may
10666 -- depend on other actuals, and only be fully determined when
10667 -- the enclosing instance is analyzed.
10668
10669 if Present (Etype (Actual))
10670 and then Is_Constr_Subt_For_U_Nominal (Etype (Actual))
10671 then
10672 Freeze_Before (Instantiation_Node, Etype (Actual));
10673 else
10674 Freeze_Before (Instantiation_Node, Typ);
10675 end if;
10676
10677 -- If the actual is an aggregate, perform name resolution on
10678 -- its components (the analysis of an aggregate does not do it)
10679 -- to capture local names that may be hidden if the generic is
10680 -- a child unit.
10681
10682 if Nkind (Actual) = N_Aggregate then
10683 Preanalyze_And_Resolve (Actual, Typ);
10684 end if;
10685
10686 if Is_Limited_Type (Typ)
10687 and then not OK_For_Limited_Init (Typ, Actual)
10688 then
10689 Error_Msg_N
10690 ("initialization not allowed for limited types", Actual);
10691 Explain_Limited_Type (Typ, Actual);
10692 end if;
10693 end;
10694
10695 elsif Present (Default_Expression (Formal)) then
10696
10697 -- Use default to construct declaration
10698
10699 if Present (Subt_Mark) then
10700 Def := New_Copy (Subt_Mark);
10701 else pragma Assert (Present (Acc_Def));
10702 Def := Copy_Access_Def;
10703 end if;
10704
10705 Decl_Node :=
10706 Make_Object_Declaration (Sloc (Formal),
10707 Defining_Identifier => New_Copy (Gen_Obj),
10708 Constant_Present => True,
10709 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
10710 Object_Definition => Def,
10711 Expression => New_Copy_Tree
10712 (Default_Expression (Formal)));
10713
10714 Append (Decl_Node, List);
10715 Set_Analyzed (Expression (Decl_Node), False);
10716
10717 else
10718 Error_Msg_NE ("missing actual&", Instantiation_Node, Gen_Obj);
10719 Error_Msg_NE ("\in instantiation of & declared#",
10720 Instantiation_Node, Scope (A_Gen_Obj));
10721
10722 if Is_Scalar_Type (Etype (A_Gen_Obj)) then
10723
10724 -- Create dummy constant declaration so that instance can be
10725 -- analyzed, to minimize cascaded visibility errors.
10726
10727 if Present (Subt_Mark) then
10728 Def := Subt_Mark;
10729 else pragma Assert (Present (Acc_Def));
10730 Def := Acc_Def;
10731 end if;
10732
10733 Decl_Node :=
10734 Make_Object_Declaration (Loc,
10735 Defining_Identifier => New_Copy (Gen_Obj),
10736 Constant_Present => True,
10737 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
10738 Object_Definition => New_Copy (Def),
10739 Expression =>
10740 Make_Attribute_Reference (Sloc (Gen_Obj),
10741 Attribute_Name => Name_First,
10742 Prefix => New_Copy (Def)));
10743
10744 Append (Decl_Node, List);
10745
10746 else
10747 Abandon_Instantiation (Instantiation_Node);
10748 end if;
10749 end if;
10750 end if;
10751
10752 if Nkind (Actual) in N_Has_Entity then
10753 Actual_Decl := Parent (Entity (Actual));
10754 end if;
10755
10756 -- Ada 2005 (AI-423): For a formal object declaration with a null
10757 -- exclusion or an access definition that has a null exclusion: If the
10758 -- actual matching the formal object declaration denotes a generic
10759 -- formal object of another generic unit G, and the instantiation
10760 -- containing the actual occurs within the body of G or within the body
10761 -- of a generic unit declared within the declarative region of G, then
10762 -- the declaration of the formal object of G must have a null exclusion.
10763 -- Otherwise, the subtype of the actual matching the formal object
10764 -- declaration shall exclude null.
10765
10766 if Ada_Version >= Ada_2005
10767 and then Present (Actual_Decl)
10768 and then Nkind_In (Actual_Decl, N_Formal_Object_Declaration,
10769 N_Object_Declaration)
10770 and then Nkind (Analyzed_Formal) = N_Formal_Object_Declaration
10771 and then not Has_Null_Exclusion (Actual_Decl)
10772 and then Has_Null_Exclusion (Analyzed_Formal)
10773 then
10774 Error_Msg_Sloc := Sloc (Analyzed_Formal);
10775 Error_Msg_N
10776 ("actual must exclude null to match generic formal#", Actual);
10777 end if;
10778
10779 -- An effectively volatile object cannot be used as an actual in a
10780 -- generic instantiation (SPARK RM 7.1.3(7)). The following check is
10781 -- relevant only when SPARK_Mode is on as it is not a standard Ada
10782 -- legality rule, and also verifies that the actual is an object.
10783
10784 if SPARK_Mode = On
10785 and then Present (Actual)
10786 and then Is_Object_Reference (Actual)
10787 and then Is_Effectively_Volatile_Object (Actual)
10788 then
10789 Error_Msg_N
10790 ("volatile object cannot act as actual in generic instantiation",
10791 Actual);
10792 end if;
10793
10794 return List;
10795 end Instantiate_Object;
10796
10797 ------------------------------
10798 -- Instantiate_Package_Body --
10799 ------------------------------
10800
10801 -- WARNING: This routine manages Ghost regions. Return statements must be
10802 -- replaced by gotos which jump to the end of the routine and restore the
10803 -- Ghost mode.
10804
10805 procedure Instantiate_Package_Body
10806 (Body_Info : Pending_Body_Info;
10807 Inlined_Body : Boolean := False;
10808 Body_Optional : Boolean := False)
10809 is
10810 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
10811 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Decl);
10812 Act_Spec : constant Node_Id := Specification (Act_Decl);
10813 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
10814 Gen_Id : constant Node_Id := Name (Inst_Node);
10815 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
10816 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
10817 Loc : constant Source_Ptr := Sloc (Inst_Node);
10818
10819 Save_IPSM : constant Boolean := Ignore_Pragma_SPARK_Mode;
10820 Save_Style_Check : constant Boolean := Style_Check;
10821
10822 procedure Check_Initialized_Types;
10823 -- In a generic package body, an entity of a generic private type may
10824 -- appear uninitialized. This is suspicious, unless the actual is a
10825 -- fully initialized type.
10826
10827 -----------------------------
10828 -- Check_Initialized_Types --
10829 -----------------------------
10830
10831 procedure Check_Initialized_Types is
10832 Decl : Node_Id;
10833 Formal : Entity_Id;
10834 Actual : Entity_Id;
10835 Uninit_Var : Entity_Id;
10836
10837 begin
10838 Decl := First (Generic_Formal_Declarations (Gen_Decl));
10839 while Present (Decl) loop
10840 Uninit_Var := Empty;
10841
10842 if Nkind (Decl) = N_Private_Extension_Declaration then
10843 Uninit_Var := Uninitialized_Variable (Decl);
10844
10845 elsif Nkind (Decl) = N_Formal_Type_Declaration
10846 and then Nkind (Formal_Type_Definition (Decl)) =
10847 N_Formal_Private_Type_Definition
10848 then
10849 Uninit_Var :=
10850 Uninitialized_Variable (Formal_Type_Definition (Decl));
10851 end if;
10852
10853 if Present (Uninit_Var) then
10854 Formal := Defining_Identifier (Decl);
10855 Actual := First_Entity (Act_Decl_Id);
10856
10857 -- For each formal there is a subtype declaration that renames
10858 -- the actual and has the same name as the formal. Locate the
10859 -- formal for warning message about uninitialized variables
10860 -- in the generic, for which the actual type should be a fully
10861 -- initialized type.
10862
10863 while Present (Actual) loop
10864 exit when Ekind (Actual) = E_Package
10865 and then Present (Renamed_Object (Actual));
10866
10867 if Chars (Actual) = Chars (Formal)
10868 and then not Is_Scalar_Type (Actual)
10869 and then not Is_Fully_Initialized_Type (Actual)
10870 and then Warn_On_No_Value_Assigned
10871 then
10872 Error_Msg_Node_2 := Formal;
10873 Error_Msg_NE
10874 ("generic unit has uninitialized variable& of "
10875 & "formal private type &?v?", Actual, Uninit_Var);
10876 Error_Msg_NE
10877 ("actual type for& should be fully initialized type?v?",
10878 Actual, Formal);
10879 exit;
10880 end if;
10881
10882 Next_Entity (Actual);
10883 end loop;
10884 end if;
10885
10886 Next (Decl);
10887 end loop;
10888 end Check_Initialized_Types;
10889
10890 -- Local variables
10891
10892 Act_Body : Node_Id;
10893 Act_Body_Id : Entity_Id;
10894 Act_Body_Name : Node_Id;
10895 Gen_Body : Node_Id;
10896 Gen_Body_Id : Node_Id;
10897 Mode : Ghost_Mode_Type;
10898 Par_Ent : Entity_Id := Empty;
10899 Par_Vis : Boolean := False;
10900
10901 Parent_Installed : Boolean := False;
10902
10903 Vis_Prims_List : Elist_Id := No_Elist;
10904 -- List of primitives made temporarily visible in the instantiation
10905 -- to match the visibility of the formal type.
10906
10907 -- Start of processing for Instantiate_Package_Body
10908
10909 begin
10910 Gen_Body_Id := Corresponding_Body (Gen_Decl);
10911
10912 -- The instance body may already have been processed, as the parent of
10913 -- another instance that is inlined (Load_Parent_Of_Generic).
10914
10915 if Present (Corresponding_Body (Instance_Spec (Inst_Node))) then
10916 return;
10917 end if;
10918
10919 -- The package being instantiated may be subject to pragma Ghost. Set
10920 -- the mode now to ensure that any nodes generated during instantiation
10921 -- are properly marked as Ghost.
10922
10923 Set_Ghost_Mode (Act_Decl_Id, Mode);
10924
10925 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
10926
10927 -- Re-establish the state of information on which checks are suppressed.
10928 -- This information was set in Body_Info at the point of instantiation,
10929 -- and now we restore it so that the instance is compiled using the
10930 -- check status at the instantiation (RM 11.5(7.2/2), AI95-00224-01).
10931
10932 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
10933 Scope_Suppress := Body_Info.Scope_Suppress;
10934 Opt.Ada_Version := Body_Info.Version;
10935 Opt.Ada_Version_Pragma := Body_Info.Version_Pragma;
10936 Restore_Warnings (Body_Info.Warnings);
10937 Opt.SPARK_Mode := Body_Info.SPARK_Mode;
10938 Opt.SPARK_Mode_Pragma := Body_Info.SPARK_Mode_Pragma;
10939
10940 if No (Gen_Body_Id) then
10941
10942 -- Do not look for parent of generic body if none is required.
10943 -- This may happen when the routine is called as part of the
10944 -- Pending_Instantiations processing, when nested instances
10945 -- may precede the one generated from the main unit.
10946
10947 if not Unit_Requires_Body (Defining_Entity (Gen_Decl))
10948 and then Body_Optional
10949 then
10950 goto Leave;
10951 else
10952 Load_Parent_Of_Generic
10953 (Inst_Node, Specification (Gen_Decl), Body_Optional);
10954 Gen_Body_Id := Corresponding_Body (Gen_Decl);
10955 end if;
10956 end if;
10957
10958 -- Establish global variable for sloc adjustment and for error recovery
10959 -- In the case of an instance body for an instantiation with actuals
10960 -- from a limited view, the instance body is placed at the beginning
10961 -- of the enclosing package body: use the body entity as the source
10962 -- location for nodes of the instance body.
10963
10964 if not Is_Empty_Elmt_List (Incomplete_Actuals (Act_Decl_Id)) then
10965 declare
10966 Scop : constant Entity_Id := Scope (Act_Decl_Id);
10967 Body_Id : constant Node_Id :=
10968 Corresponding_Body (Unit_Declaration_Node (Scop));
10969
10970 begin
10971 Instantiation_Node := Body_Id;
10972 end;
10973 else
10974 Instantiation_Node := Inst_Node;
10975 end if;
10976
10977 if Present (Gen_Body_Id) then
10978 Save_Env (Gen_Unit, Act_Decl_Id);
10979 Style_Check := False;
10980
10981 -- If the context of the instance is subject to SPARK_Mode "off" or
10982 -- the annotation is altogether missing, set the global flag which
10983 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
10984 -- the instance.
10985
10986 if SPARK_Mode /= On then
10987 Ignore_Pragma_SPARK_Mode := True;
10988 end if;
10989
10990 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
10991 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
10992
10993 Create_Instantiation_Source
10994 (Inst_Node, Gen_Body_Id, S_Adjustment);
10995
10996 Act_Body :=
10997 Copy_Generic_Node
10998 (Original_Node (Gen_Body), Empty, Instantiating => True);
10999
11000 -- Create proper (possibly qualified) defining name for the body, to
11001 -- correspond to the one in the spec.
11002
11003 Act_Body_Id :=
11004 Make_Defining_Identifier (Sloc (Act_Decl_Id), Chars (Act_Decl_Id));
11005 Set_Comes_From_Source (Act_Body_Id, Comes_From_Source (Act_Decl_Id));
11006
11007 -- Some attributes of spec entity are not inherited by body entity
11008
11009 Set_Handler_Records (Act_Body_Id, No_List);
11010
11011 if Nkind (Defining_Unit_Name (Act_Spec)) =
11012 N_Defining_Program_Unit_Name
11013 then
11014 Act_Body_Name :=
11015 Make_Defining_Program_Unit_Name (Loc,
11016 Name =>
11017 New_Copy_Tree (Name (Defining_Unit_Name (Act_Spec))),
11018 Defining_Identifier => Act_Body_Id);
11019 else
11020 Act_Body_Name := Act_Body_Id;
11021 end if;
11022
11023 Set_Defining_Unit_Name (Act_Body, Act_Body_Name);
11024
11025 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
11026 Check_Generic_Actuals (Act_Decl_Id, False);
11027 Check_Initialized_Types;
11028
11029 -- Install primitives hidden at the point of the instantiation but
11030 -- visible when processing the generic formals
11031
11032 declare
11033 E : Entity_Id;
11034
11035 begin
11036 E := First_Entity (Act_Decl_Id);
11037 while Present (E) loop
11038 if Is_Type (E)
11039 and then not Is_Itype (E)
11040 and then Is_Generic_Actual_Type (E)
11041 and then Is_Tagged_Type (E)
11042 then
11043 Install_Hidden_Primitives
11044 (Prims_List => Vis_Prims_List,
11045 Gen_T => Generic_Parent_Type (Parent (E)),
11046 Act_T => E);
11047 end if;
11048
11049 Next_Entity (E);
11050 end loop;
11051 end;
11052
11053 -- If it is a child unit, make the parent instance (which is an
11054 -- instance of the parent of the generic) visible. The parent
11055 -- instance is the prefix of the name of the generic unit.
11056
11057 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
11058 and then Nkind (Gen_Id) = N_Expanded_Name
11059 then
11060 Par_Ent := Entity (Prefix (Gen_Id));
11061 Par_Vis := Is_Immediately_Visible (Par_Ent);
11062 Install_Parent (Par_Ent, In_Body => True);
11063 Parent_Installed := True;
11064
11065 elsif Is_Child_Unit (Gen_Unit) then
11066 Par_Ent := Scope (Gen_Unit);
11067 Par_Vis := Is_Immediately_Visible (Par_Ent);
11068 Install_Parent (Par_Ent, In_Body => True);
11069 Parent_Installed := True;
11070 end if;
11071
11072 -- If the instantiation is a library unit, and this is the main unit,
11073 -- then build the resulting compilation unit nodes for the instance.
11074 -- If this is a compilation unit but it is not the main unit, then it
11075 -- is the body of a unit in the context, that is being compiled
11076 -- because it is encloses some inlined unit or another generic unit
11077 -- being instantiated. In that case, this body is not part of the
11078 -- current compilation, and is not attached to the tree, but its
11079 -- parent must be set for analysis.
11080
11081 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
11082
11083 -- Replace instance node with body of instance, and create new
11084 -- node for corresponding instance declaration.
11085
11086 Build_Instance_Compilation_Unit_Nodes
11087 (Inst_Node, Act_Body, Act_Decl);
11088 Analyze (Inst_Node);
11089
11090 if Parent (Inst_Node) = Cunit (Main_Unit) then
11091
11092 -- If the instance is a child unit itself, then set the scope
11093 -- of the expanded body to be the parent of the instantiation
11094 -- (ensuring that the fully qualified name will be generated
11095 -- for the elaboration subprogram).
11096
11097 if Nkind (Defining_Unit_Name (Act_Spec)) =
11098 N_Defining_Program_Unit_Name
11099 then
11100 Set_Scope (Defining_Entity (Inst_Node), Scope (Act_Decl_Id));
11101 end if;
11102 end if;
11103
11104 -- Case where instantiation is not a library unit
11105
11106 else
11107 -- If this is an early instantiation, i.e. appears textually
11108 -- before the corresponding body and must be elaborated first,
11109 -- indicate that the body instance is to be delayed.
11110
11111 Install_Body (Act_Body, Inst_Node, Gen_Body, Gen_Decl);
11112
11113 -- Now analyze the body. We turn off all checks if this is an
11114 -- internal unit, since there is no reason to have checks on for
11115 -- any predefined run-time library code. All such code is designed
11116 -- to be compiled with checks off.
11117
11118 -- Note that we do NOT apply this criterion to children of GNAT
11119 -- The latter units must suppress checks explicitly if needed.
11120
11121 -- We also do not suppress checks in CodePeer mode where we are
11122 -- interested in finding possible runtime errors.
11123
11124 if not CodePeer_Mode
11125 and then Is_Predefined_File_Name
11126 (Unit_File_Name (Get_Source_Unit (Gen_Decl)))
11127 then
11128 Analyze (Act_Body, Suppress => All_Checks);
11129 else
11130 Analyze (Act_Body);
11131 end if;
11132 end if;
11133
11134 Inherit_Context (Gen_Body, Inst_Node);
11135
11136 -- Remove the parent instances if they have been placed on the scope
11137 -- stack to compile the body.
11138
11139 if Parent_Installed then
11140 Remove_Parent (In_Body => True);
11141
11142 -- Restore the previous visibility of the parent
11143
11144 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
11145 end if;
11146
11147 Restore_Hidden_Primitives (Vis_Prims_List);
11148 Restore_Private_Views (Act_Decl_Id);
11149
11150 -- Remove the current unit from visibility if this is an instance
11151 -- that is not elaborated on the fly for inlining purposes.
11152
11153 if not Inlined_Body then
11154 Set_Is_Immediately_Visible (Act_Decl_Id, False);
11155 end if;
11156
11157 Restore_Env;
11158 Ignore_Pragma_SPARK_Mode := Save_IPSM;
11159 Style_Check := Save_Style_Check;
11160
11161 -- If we have no body, and the unit requires a body, then complain. This
11162 -- complaint is suppressed if we have detected other errors (since a
11163 -- common reason for missing the body is that it had errors).
11164 -- In CodePeer mode, a warning has been emitted already, no need for
11165 -- further messages.
11166
11167 elsif Unit_Requires_Body (Gen_Unit)
11168 and then not Body_Optional
11169 then
11170 if CodePeer_Mode then
11171 null;
11172
11173 elsif Serious_Errors_Detected = 0 then
11174 Error_Msg_NE
11175 ("cannot find body of generic package &", Inst_Node, Gen_Unit);
11176
11177 -- Don't attempt to perform any cleanup actions if some other error
11178 -- was already detected, since this can cause blowups.
11179
11180 else
11181 return;
11182 end if;
11183
11184 -- Case of package that does not need a body
11185
11186 else
11187 -- If the instantiation of the declaration is a library unit, rewrite
11188 -- the original package instantiation as a package declaration in the
11189 -- compilation unit node.
11190
11191 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
11192 Set_Parent_Spec (Act_Decl, Parent_Spec (Inst_Node));
11193 Rewrite (Inst_Node, Act_Decl);
11194
11195 -- Generate elaboration entity, in case spec has elaboration code.
11196 -- This cannot be done when the instance is analyzed, because it
11197 -- is not known yet whether the body exists.
11198
11199 Set_Elaboration_Entity_Required (Act_Decl_Id, False);
11200 Build_Elaboration_Entity (Parent (Inst_Node), Act_Decl_Id);
11201
11202 -- If the instantiation is not a library unit, then append the
11203 -- declaration to the list of implicitly generated entities, unless
11204 -- it is already a list member which means that it was already
11205 -- processed
11206
11207 elsif not Is_List_Member (Act_Decl) then
11208 Mark_Rewrite_Insertion (Act_Decl);
11209 Insert_Before (Inst_Node, Act_Decl);
11210 end if;
11211 end if;
11212
11213 Expander_Mode_Restore;
11214
11215 <<Leave>>
11216 Restore_Ghost_Mode (Mode);
11217 end Instantiate_Package_Body;
11218
11219 ---------------------------------
11220 -- Instantiate_Subprogram_Body --
11221 ---------------------------------
11222
11223 -- WARNING: This routine manages Ghost regions. Return statements must be
11224 -- replaced by gotos which jump to the end of the routine and restore the
11225 -- Ghost mode.
11226
11227 procedure Instantiate_Subprogram_Body
11228 (Body_Info : Pending_Body_Info;
11229 Body_Optional : Boolean := False)
11230 is
11231 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
11232 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Decl);
11233 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
11234 Gen_Id : constant Node_Id := Name (Inst_Node);
11235 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
11236 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
11237 Loc : constant Source_Ptr := Sloc (Inst_Node);
11238 Pack_Id : constant Entity_Id :=
11239 Defining_Unit_Name (Parent (Act_Decl));
11240
11241 Saved_IPSM : constant Boolean := Ignore_Pragma_SPARK_Mode;
11242 Saved_Style_Check : constant Boolean := Style_Check;
11243 Saved_Warnings : constant Warning_Record := Save_Warnings;
11244
11245 Act_Body : Node_Id;
11246 Act_Body_Id : Entity_Id;
11247 Gen_Body : Node_Id;
11248 Gen_Body_Id : Node_Id;
11249 Mode : Ghost_Mode_Type;
11250 Pack_Body : Node_Id;
11251 Par_Ent : Entity_Id := Empty;
11252 Par_Vis : Boolean := False;
11253 Ret_Expr : Node_Id;
11254
11255 Parent_Installed : Boolean := False;
11256
11257 begin
11258 Gen_Body_Id := Corresponding_Body (Gen_Decl);
11259
11260 -- Subprogram body may have been created already because of an inline
11261 -- pragma, or because of multiple elaborations of the enclosing package
11262 -- when several instances of the subprogram appear in the main unit.
11263
11264 if Present (Corresponding_Body (Act_Decl)) then
11265 return;
11266 end if;
11267
11268 -- The subprogram being instantiated may be subject to pragma Ghost. Set
11269 -- the mode now to ensure that any nodes generated during instantiation
11270 -- are properly marked as Ghost.
11271
11272 Set_Ghost_Mode (Act_Decl_Id, Mode);
11273
11274 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
11275
11276 -- Re-establish the state of information on which checks are suppressed.
11277 -- This information was set in Body_Info at the point of instantiation,
11278 -- and now we restore it so that the instance is compiled using the
11279 -- check status at the instantiation (RM 11.5(7.2/2), AI95-00224-01).
11280
11281 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
11282 Scope_Suppress := Body_Info.Scope_Suppress;
11283 Opt.Ada_Version := Body_Info.Version;
11284 Opt.Ada_Version_Pragma := Body_Info.Version_Pragma;
11285 Restore_Warnings (Body_Info.Warnings);
11286 Opt.SPARK_Mode := Body_Info.SPARK_Mode;
11287 Opt.SPARK_Mode_Pragma := Body_Info.SPARK_Mode_Pragma;
11288
11289 if No (Gen_Body_Id) then
11290
11291 -- For imported generic subprogram, no body to compile, complete
11292 -- the spec entity appropriately.
11293
11294 if Is_Imported (Gen_Unit) then
11295 Set_Is_Imported (Act_Decl_Id);
11296 Set_First_Rep_Item (Act_Decl_Id, First_Rep_Item (Gen_Unit));
11297 Set_Interface_Name (Act_Decl_Id, Interface_Name (Gen_Unit));
11298 Set_Convention (Act_Decl_Id, Convention (Gen_Unit));
11299 Set_Has_Completion (Act_Decl_Id);
11300 goto Leave;
11301
11302 -- For other cases, compile the body
11303
11304 else
11305 Load_Parent_Of_Generic
11306 (Inst_Node, Specification (Gen_Decl), Body_Optional);
11307 Gen_Body_Id := Corresponding_Body (Gen_Decl);
11308 end if;
11309 end if;
11310
11311 Instantiation_Node := Inst_Node;
11312
11313 if Present (Gen_Body_Id) then
11314 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
11315
11316 if Nkind (Gen_Body) = N_Subprogram_Body_Stub then
11317
11318 -- Either body is not present, or context is non-expanding, as
11319 -- when compiling a subunit. Mark the instance as completed, and
11320 -- diagnose a missing body when needed.
11321
11322 if Expander_Active
11323 and then Operating_Mode = Generate_Code
11324 then
11325 Error_Msg_N ("missing proper body for instantiation", Gen_Body);
11326 end if;
11327
11328 Set_Has_Completion (Act_Decl_Id);
11329 goto Leave;
11330 end if;
11331
11332 Save_Env (Gen_Unit, Act_Decl_Id);
11333 Style_Check := False;
11334
11335 -- If the context of the instance is subject to SPARK_Mode "off" or
11336 -- the annotation is altogether missing, set the global flag which
11337 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
11338 -- the instance.
11339
11340 if SPARK_Mode /= On then
11341 Ignore_Pragma_SPARK_Mode := True;
11342 end if;
11343
11344 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
11345 Create_Instantiation_Source
11346 (Inst_Node,
11347 Gen_Body_Id,
11348 S_Adjustment);
11349
11350 Act_Body :=
11351 Copy_Generic_Node
11352 (Original_Node (Gen_Body), Empty, Instantiating => True);
11353
11354 -- Create proper defining name for the body, to correspond to the one
11355 -- in the spec.
11356
11357 Act_Body_Id :=
11358 Make_Defining_Identifier (Sloc (Act_Decl_Id), Chars (Act_Decl_Id));
11359
11360 Set_Comes_From_Source (Act_Body_Id, Comes_From_Source (Act_Decl_Id));
11361 Set_Defining_Unit_Name (Specification (Act_Body), Act_Body_Id);
11362
11363 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
11364 Set_Has_Completion (Act_Decl_Id);
11365 Check_Generic_Actuals (Pack_Id, False);
11366
11367 -- Generate a reference to link the visible subprogram instance to
11368 -- the generic body, which for navigation purposes is the only
11369 -- available source for the instance.
11370
11371 Generate_Reference
11372 (Related_Instance (Pack_Id),
11373 Gen_Body_Id, 'b', Set_Ref => False, Force => True);
11374
11375 -- If it is a child unit, make the parent instance (which is an
11376 -- instance of the parent of the generic) visible. The parent
11377 -- instance is the prefix of the name of the generic unit.
11378
11379 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
11380 and then Nkind (Gen_Id) = N_Expanded_Name
11381 then
11382 Par_Ent := Entity (Prefix (Gen_Id));
11383 Par_Vis := Is_Immediately_Visible (Par_Ent);
11384 Install_Parent (Par_Ent, In_Body => True);
11385 Parent_Installed := True;
11386
11387 elsif Is_Child_Unit (Gen_Unit) then
11388 Par_Ent := Scope (Gen_Unit);
11389 Par_Vis := Is_Immediately_Visible (Par_Ent);
11390 Install_Parent (Par_Ent, In_Body => True);
11391 Parent_Installed := True;
11392 end if;
11393
11394 -- Subprogram body is placed in the body of wrapper package,
11395 -- whose spec contains the subprogram declaration as well as
11396 -- the renaming declarations for the generic parameters.
11397
11398 Pack_Body :=
11399 Make_Package_Body (Loc,
11400 Defining_Unit_Name => New_Copy (Pack_Id),
11401 Declarations => New_List (Act_Body));
11402
11403 Set_Corresponding_Spec (Pack_Body, Pack_Id);
11404
11405 -- If the instantiation is a library unit, then build resulting
11406 -- compilation unit nodes for the instance. The declaration of
11407 -- the enclosing package is the grandparent of the subprogram
11408 -- declaration. First replace the instantiation node as the unit
11409 -- of the corresponding compilation.
11410
11411 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
11412 if Parent (Inst_Node) = Cunit (Main_Unit) then
11413 Set_Unit (Parent (Inst_Node), Inst_Node);
11414 Build_Instance_Compilation_Unit_Nodes
11415 (Inst_Node, Pack_Body, Parent (Parent (Act_Decl)));
11416 Analyze (Inst_Node);
11417 else
11418 Set_Parent (Pack_Body, Parent (Inst_Node));
11419 Analyze (Pack_Body);
11420 end if;
11421
11422 else
11423 Insert_Before (Inst_Node, Pack_Body);
11424 Mark_Rewrite_Insertion (Pack_Body);
11425 Analyze (Pack_Body);
11426
11427 if Expander_Active then
11428 Freeze_Subprogram_Body (Inst_Node, Gen_Body, Pack_Id);
11429 end if;
11430 end if;
11431
11432 Inherit_Context (Gen_Body, Inst_Node);
11433
11434 Restore_Private_Views (Pack_Id, False);
11435
11436 if Parent_Installed then
11437 Remove_Parent (In_Body => True);
11438
11439 -- Restore the previous visibility of the parent
11440
11441 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
11442 end if;
11443
11444 Restore_Env;
11445 Ignore_Pragma_SPARK_Mode := Saved_IPSM;
11446 Style_Check := Saved_Style_Check;
11447 Restore_Warnings (Saved_Warnings);
11448
11449 -- Body not found. Error was emitted already. If there were no previous
11450 -- errors, this may be an instance whose scope is a premature instance.
11451 -- In that case we must insure that the (legal) program does raise
11452 -- program error if executed. We generate a subprogram body for this
11453 -- purpose. See DEC ac30vso.
11454
11455 -- Should not reference proprietary DEC tests in comments ???
11456
11457 elsif Serious_Errors_Detected = 0
11458 and then Nkind (Parent (Inst_Node)) /= N_Compilation_Unit
11459 then
11460 if Body_Optional then
11461 goto Leave;
11462
11463 elsif Ekind (Act_Decl_Id) = E_Procedure then
11464 Act_Body :=
11465 Make_Subprogram_Body (Loc,
11466 Specification =>
11467 Make_Procedure_Specification (Loc,
11468 Defining_Unit_Name =>
11469 Make_Defining_Identifier (Loc, Chars (Act_Decl_Id)),
11470 Parameter_Specifications =>
11471 New_Copy_List
11472 (Parameter_Specifications (Parent (Act_Decl_Id)))),
11473
11474 Declarations => Empty_List,
11475 Handled_Statement_Sequence =>
11476 Make_Handled_Sequence_Of_Statements (Loc,
11477 Statements => New_List (
11478 Make_Raise_Program_Error (Loc,
11479 Reason => PE_Access_Before_Elaboration))));
11480
11481 else
11482 Ret_Expr :=
11483 Make_Raise_Program_Error (Loc,
11484 Reason => PE_Access_Before_Elaboration);
11485
11486 Set_Etype (Ret_Expr, (Etype (Act_Decl_Id)));
11487 Set_Analyzed (Ret_Expr);
11488
11489 Act_Body :=
11490 Make_Subprogram_Body (Loc,
11491 Specification =>
11492 Make_Function_Specification (Loc,
11493 Defining_Unit_Name =>
11494 Make_Defining_Identifier (Loc, Chars (Act_Decl_Id)),
11495 Parameter_Specifications =>
11496 New_Copy_List
11497 (Parameter_Specifications (Parent (Act_Decl_Id))),
11498 Result_Definition =>
11499 New_Occurrence_Of (Etype (Act_Decl_Id), Loc)),
11500
11501 Declarations => Empty_List,
11502 Handled_Statement_Sequence =>
11503 Make_Handled_Sequence_Of_Statements (Loc,
11504 Statements => New_List (
11505 Make_Simple_Return_Statement (Loc, Ret_Expr))));
11506 end if;
11507
11508 Pack_Body :=
11509 Make_Package_Body (Loc,
11510 Defining_Unit_Name => New_Copy (Pack_Id),
11511 Declarations => New_List (Act_Body));
11512
11513 Insert_After (Inst_Node, Pack_Body);
11514 Set_Corresponding_Spec (Pack_Body, Pack_Id);
11515 Analyze (Pack_Body);
11516 end if;
11517
11518 Expander_Mode_Restore;
11519
11520 <<Leave>>
11521 Restore_Ghost_Mode (Mode);
11522 end Instantiate_Subprogram_Body;
11523
11524 ----------------------
11525 -- Instantiate_Type --
11526 ----------------------
11527
11528 function Instantiate_Type
11529 (Formal : Node_Id;
11530 Actual : Node_Id;
11531 Analyzed_Formal : Node_Id;
11532 Actual_Decls : List_Id) return List_Id
11533 is
11534 Gen_T : constant Entity_Id := Defining_Identifier (Formal);
11535 A_Gen_T : constant Entity_Id :=
11536 Defining_Identifier (Analyzed_Formal);
11537 Ancestor : Entity_Id := Empty;
11538 Def : constant Node_Id := Formal_Type_Definition (Formal);
11539 Act_T : Entity_Id;
11540 Decl_Node : Node_Id;
11541 Decl_Nodes : List_Id;
11542 Loc : Source_Ptr;
11543 Subt : Entity_Id;
11544
11545 procedure Diagnose_Predicated_Actual;
11546 -- There are a number of constructs in which a discrete type with
11547 -- predicates is illegal, e.g. as an index in an array type declaration.
11548 -- If a generic type is used is such a construct in a generic package
11549 -- declaration, it carries the flag No_Predicate_On_Actual. it is part
11550 -- of the generic contract that the actual cannot have predicates.
11551
11552 procedure Validate_Array_Type_Instance;
11553 procedure Validate_Access_Subprogram_Instance;
11554 procedure Validate_Access_Type_Instance;
11555 procedure Validate_Derived_Type_Instance;
11556 procedure Validate_Derived_Interface_Type_Instance;
11557 procedure Validate_Discriminated_Formal_Type;
11558 procedure Validate_Interface_Type_Instance;
11559 procedure Validate_Private_Type_Instance;
11560 procedure Validate_Incomplete_Type_Instance;
11561 -- These procedures perform validation tests for the named case.
11562 -- Validate_Discriminated_Formal_Type is shared by formal private
11563 -- types and Ada 2012 formal incomplete types.
11564
11565 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean;
11566 -- Check that base types are the same and that the subtypes match
11567 -- statically. Used in several of the above.
11568
11569 ---------------------------------
11570 -- Diagnose_Predicated_Actual --
11571 ---------------------------------
11572
11573 procedure Diagnose_Predicated_Actual is
11574 begin
11575 if No_Predicate_On_Actual (A_Gen_T)
11576 and then Has_Predicates (Act_T)
11577 then
11578 Error_Msg_NE
11579 ("actual for& cannot be a type with predicate",
11580 Instantiation_Node, A_Gen_T);
11581
11582 elsif No_Dynamic_Predicate_On_Actual (A_Gen_T)
11583 and then Has_Predicates (Act_T)
11584 and then not Has_Static_Predicate_Aspect (Act_T)
11585 then
11586 Error_Msg_NE
11587 ("actual for& cannot be a type with a dynamic predicate",
11588 Instantiation_Node, A_Gen_T);
11589 end if;
11590 end Diagnose_Predicated_Actual;
11591
11592 --------------------
11593 -- Subtypes_Match --
11594 --------------------
11595
11596 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean is
11597 T : constant Entity_Id := Get_Instance_Of (Gen_T);
11598
11599 begin
11600 -- Some detailed comments would be useful here ???
11601
11602 return ((Base_Type (T) = Act_T
11603 or else Base_Type (T) = Base_Type (Act_T))
11604 and then Subtypes_Statically_Match (T, Act_T))
11605
11606 or else (Is_Class_Wide_Type (Gen_T)
11607 and then Is_Class_Wide_Type (Act_T)
11608 and then Subtypes_Match
11609 (Get_Instance_Of (Root_Type (Gen_T)),
11610 Root_Type (Act_T)))
11611
11612 or else
11613 (Ekind_In (Gen_T, E_Anonymous_Access_Subprogram_Type,
11614 E_Anonymous_Access_Type)
11615 and then Ekind (Act_T) = Ekind (Gen_T)
11616 and then Subtypes_Statically_Match
11617 (Designated_Type (Gen_T), Designated_Type (Act_T)));
11618 end Subtypes_Match;
11619
11620 -----------------------------------------
11621 -- Validate_Access_Subprogram_Instance --
11622 -----------------------------------------
11623
11624 procedure Validate_Access_Subprogram_Instance is
11625 begin
11626 if not Is_Access_Type (Act_T)
11627 or else Ekind (Designated_Type (Act_T)) /= E_Subprogram_Type
11628 then
11629 Error_Msg_NE
11630 ("expect access type in instantiation of &", Actual, Gen_T);
11631 Abandon_Instantiation (Actual);
11632 end if;
11633
11634 -- According to AI05-288, actuals for access_to_subprograms must be
11635 -- subtype conformant with the generic formal. Previous to AI05-288
11636 -- only mode conformance was required.
11637
11638 -- This is a binding interpretation that applies to previous versions
11639 -- of the language, no need to maintain previous weaker checks.
11640
11641 Check_Subtype_Conformant
11642 (Designated_Type (Act_T),
11643 Designated_Type (A_Gen_T),
11644 Actual,
11645 Get_Inst => True);
11646
11647 if Ekind (Base_Type (Act_T)) = E_Access_Protected_Subprogram_Type then
11648 if Ekind (A_Gen_T) = E_Access_Subprogram_Type then
11649 Error_Msg_NE
11650 ("protected access type not allowed for formal &",
11651 Actual, Gen_T);
11652 end if;
11653
11654 elsif Ekind (A_Gen_T) = E_Access_Protected_Subprogram_Type then
11655 Error_Msg_NE
11656 ("expect protected access type for formal &",
11657 Actual, Gen_T);
11658 end if;
11659
11660 -- If the formal has a specified convention (which in most cases
11661 -- will be StdCall) verify that the actual has the same convention.
11662
11663 if Has_Convention_Pragma (A_Gen_T)
11664 and then Convention (A_Gen_T) /= Convention (Act_T)
11665 then
11666 Error_Msg_Name_1 := Get_Convention_Name (Convention (A_Gen_T));
11667 Error_Msg_NE
11668 ("actual for formal & must have convention %", Actual, Gen_T);
11669 end if;
11670 end Validate_Access_Subprogram_Instance;
11671
11672 -----------------------------------
11673 -- Validate_Access_Type_Instance --
11674 -----------------------------------
11675
11676 procedure Validate_Access_Type_Instance is
11677 Desig_Type : constant Entity_Id :=
11678 Find_Actual_Type (Designated_Type (A_Gen_T), A_Gen_T);
11679 Desig_Act : Entity_Id;
11680
11681 begin
11682 if not Is_Access_Type (Act_T) then
11683 Error_Msg_NE
11684 ("expect access type in instantiation of &", Actual, Gen_T);
11685 Abandon_Instantiation (Actual);
11686 end if;
11687
11688 if Is_Access_Constant (A_Gen_T) then
11689 if not Is_Access_Constant (Act_T) then
11690 Error_Msg_N
11691 ("actual type must be access-to-constant type", Actual);
11692 Abandon_Instantiation (Actual);
11693 end if;
11694 else
11695 if Is_Access_Constant (Act_T) then
11696 Error_Msg_N
11697 ("actual type must be access-to-variable type", Actual);
11698 Abandon_Instantiation (Actual);
11699
11700 elsif Ekind (A_Gen_T) = E_General_Access_Type
11701 and then Ekind (Base_Type (Act_T)) /= E_General_Access_Type
11702 then
11703 Error_Msg_N -- CODEFIX
11704 ("actual must be general access type!", Actual);
11705 Error_Msg_NE -- CODEFIX
11706 ("add ALL to }!", Actual, Act_T);
11707 Abandon_Instantiation (Actual);
11708 end if;
11709 end if;
11710
11711 -- The designated subtypes, that is to say the subtypes introduced
11712 -- by an access type declaration (and not by a subtype declaration)
11713 -- must match.
11714
11715 Desig_Act := Designated_Type (Base_Type (Act_T));
11716
11717 -- The designated type may have been introduced through a limited_
11718 -- with clause, in which case retrieve the non-limited view. This
11719 -- applies to incomplete types as well as to class-wide types.
11720
11721 if From_Limited_With (Desig_Act) then
11722 Desig_Act := Available_View (Desig_Act);
11723 end if;
11724
11725 if not Subtypes_Match (Desig_Type, Desig_Act) then
11726 Error_Msg_NE
11727 ("designated type of actual does not match that of formal &",
11728 Actual, Gen_T);
11729
11730 if not Predicates_Match (Desig_Type, Desig_Act) then
11731 Error_Msg_N ("\predicates do not match", Actual);
11732 end if;
11733
11734 Abandon_Instantiation (Actual);
11735
11736 elsif Is_Access_Type (Designated_Type (Act_T))
11737 and then Is_Constrained (Designated_Type (Designated_Type (Act_T)))
11738 /=
11739 Is_Constrained (Designated_Type (Desig_Type))
11740 then
11741 Error_Msg_NE
11742 ("designated type of actual does not match that of formal &",
11743 Actual, Gen_T);
11744
11745 if not Predicates_Match (Desig_Type, Desig_Act) then
11746 Error_Msg_N ("\predicates do not match", Actual);
11747 end if;
11748
11749 Abandon_Instantiation (Actual);
11750 end if;
11751
11752 -- Ada 2005: null-exclusion indicators of the two types must agree
11753
11754 if Can_Never_Be_Null (A_Gen_T) /= Can_Never_Be_Null (Act_T) then
11755 Error_Msg_NE
11756 ("non null exclusion of actual and formal & do not match",
11757 Actual, Gen_T);
11758 end if;
11759 end Validate_Access_Type_Instance;
11760
11761 ----------------------------------
11762 -- Validate_Array_Type_Instance --
11763 ----------------------------------
11764
11765 procedure Validate_Array_Type_Instance is
11766 I1 : Node_Id;
11767 I2 : Node_Id;
11768 T2 : Entity_Id;
11769
11770 function Formal_Dimensions return Nat;
11771 -- Count number of dimensions in array type formal
11772
11773 -----------------------
11774 -- Formal_Dimensions --
11775 -----------------------
11776
11777 function Formal_Dimensions return Nat is
11778 Num : Nat := 0;
11779 Index : Node_Id;
11780
11781 begin
11782 if Nkind (Def) = N_Constrained_Array_Definition then
11783 Index := First (Discrete_Subtype_Definitions (Def));
11784 else
11785 Index := First (Subtype_Marks (Def));
11786 end if;
11787
11788 while Present (Index) loop
11789 Num := Num + 1;
11790 Next_Index (Index);
11791 end loop;
11792
11793 return Num;
11794 end Formal_Dimensions;
11795
11796 -- Start of processing for Validate_Array_Type_Instance
11797
11798 begin
11799 if not Is_Array_Type (Act_T) then
11800 Error_Msg_NE
11801 ("expect array type in instantiation of &", Actual, Gen_T);
11802 Abandon_Instantiation (Actual);
11803
11804 elsif Nkind (Def) = N_Constrained_Array_Definition then
11805 if not (Is_Constrained (Act_T)) then
11806 Error_Msg_NE
11807 ("expect constrained array in instantiation of &",
11808 Actual, Gen_T);
11809 Abandon_Instantiation (Actual);
11810 end if;
11811
11812 else
11813 if Is_Constrained (Act_T) then
11814 Error_Msg_NE
11815 ("expect unconstrained array in instantiation of &",
11816 Actual, Gen_T);
11817 Abandon_Instantiation (Actual);
11818 end if;
11819 end if;
11820
11821 if Formal_Dimensions /= Number_Dimensions (Act_T) then
11822 Error_Msg_NE
11823 ("dimensions of actual do not match formal &", Actual, Gen_T);
11824 Abandon_Instantiation (Actual);
11825 end if;
11826
11827 I1 := First_Index (A_Gen_T);
11828 I2 := First_Index (Act_T);
11829 for J in 1 .. Formal_Dimensions loop
11830
11831 -- If the indexes of the actual were given by a subtype_mark,
11832 -- the index was transformed into a range attribute. Retrieve
11833 -- the original type mark for checking.
11834
11835 if Is_Entity_Name (Original_Node (I2)) then
11836 T2 := Entity (Original_Node (I2));
11837 else
11838 T2 := Etype (I2);
11839 end if;
11840
11841 if not Subtypes_Match
11842 (Find_Actual_Type (Etype (I1), A_Gen_T), T2)
11843 then
11844 Error_Msg_NE
11845 ("index types of actual do not match those of formal &",
11846 Actual, Gen_T);
11847 Abandon_Instantiation (Actual);
11848 end if;
11849
11850 Next_Index (I1);
11851 Next_Index (I2);
11852 end loop;
11853
11854 -- Check matching subtypes. Note that there are complex visibility
11855 -- issues when the generic is a child unit and some aspect of the
11856 -- generic type is declared in a parent unit of the generic. We do
11857 -- the test to handle this special case only after a direct check
11858 -- for static matching has failed. The case where both the component
11859 -- type and the array type are separate formals, and the component
11860 -- type is a private view may also require special checking in
11861 -- Subtypes_Match.
11862
11863 if Subtypes_Match
11864 (Component_Type (A_Gen_T), Component_Type (Act_T))
11865 or else
11866 Subtypes_Match
11867 (Find_Actual_Type (Component_Type (A_Gen_T), A_Gen_T),
11868 Component_Type (Act_T))
11869 then
11870 null;
11871 else
11872 Error_Msg_NE
11873 ("component subtype of actual does not match that of formal &",
11874 Actual, Gen_T);
11875 Abandon_Instantiation (Actual);
11876 end if;
11877
11878 if Has_Aliased_Components (A_Gen_T)
11879 and then not Has_Aliased_Components (Act_T)
11880 then
11881 Error_Msg_NE
11882 ("actual must have aliased components to match formal type &",
11883 Actual, Gen_T);
11884 end if;
11885 end Validate_Array_Type_Instance;
11886
11887 -----------------------------------------------
11888 -- Validate_Derived_Interface_Type_Instance --
11889 -----------------------------------------------
11890
11891 procedure Validate_Derived_Interface_Type_Instance is
11892 Par : constant Entity_Id := Entity (Subtype_Indication (Def));
11893 Elmt : Elmt_Id;
11894
11895 begin
11896 -- First apply interface instance checks
11897
11898 Validate_Interface_Type_Instance;
11899
11900 -- Verify that immediate parent interface is an ancestor of
11901 -- the actual.
11902
11903 if Present (Par)
11904 and then not Interface_Present_In_Ancestor (Act_T, Par)
11905 then
11906 Error_Msg_NE
11907 ("interface actual must include progenitor&", Actual, Par);
11908 end if;
11909
11910 -- Now verify that the actual includes all other ancestors of
11911 -- the formal.
11912
11913 Elmt := First_Elmt (Interfaces (A_Gen_T));
11914 while Present (Elmt) loop
11915 if not Interface_Present_In_Ancestor
11916 (Act_T, Get_Instance_Of (Node (Elmt)))
11917 then
11918 Error_Msg_NE
11919 ("interface actual must include progenitor&",
11920 Actual, Node (Elmt));
11921 end if;
11922
11923 Next_Elmt (Elmt);
11924 end loop;
11925 end Validate_Derived_Interface_Type_Instance;
11926
11927 ------------------------------------
11928 -- Validate_Derived_Type_Instance --
11929 ------------------------------------
11930
11931 procedure Validate_Derived_Type_Instance is
11932 Actual_Discr : Entity_Id;
11933 Ancestor_Discr : Entity_Id;
11934
11935 begin
11936 -- If the parent type in the generic declaration is itself a previous
11937 -- formal type, then it is local to the generic and absent from the
11938 -- analyzed generic definition. In that case the ancestor is the
11939 -- instance of the formal (which must have been instantiated
11940 -- previously), unless the ancestor is itself a formal derived type.
11941 -- In this latter case (which is the subject of Corrigendum 8652/0038
11942 -- (AI-202) the ancestor of the formals is the ancestor of its
11943 -- parent. Otherwise, the analyzed generic carries the parent type.
11944 -- If the parent type is defined in a previous formal package, then
11945 -- the scope of that formal package is that of the generic type
11946 -- itself, and it has already been mapped into the corresponding type
11947 -- in the actual package.
11948
11949 -- Common case: parent type defined outside of the generic
11950
11951 if Is_Entity_Name (Subtype_Mark (Def))
11952 and then Present (Entity (Subtype_Mark (Def)))
11953 then
11954 Ancestor := Get_Instance_Of (Entity (Subtype_Mark (Def)));
11955
11956 -- Check whether parent is defined in a previous formal package
11957
11958 elsif
11959 Scope (Scope (Base_Type (Etype (A_Gen_T)))) = Scope (A_Gen_T)
11960 then
11961 Ancestor :=
11962 Get_Instance_Of (Base_Type (Etype (A_Gen_T)));
11963
11964 -- The type may be a local derivation, or a type extension of a
11965 -- previous formal, or of a formal of a parent package.
11966
11967 elsif Is_Derived_Type (Get_Instance_Of (A_Gen_T))
11968 or else
11969 Ekind (Get_Instance_Of (A_Gen_T)) = E_Record_Type_With_Private
11970 then
11971 -- Check whether the parent is another derived formal type in the
11972 -- same generic unit.
11973
11974 if Etype (A_Gen_T) /= A_Gen_T
11975 and then Is_Generic_Type (Etype (A_Gen_T))
11976 and then Scope (Etype (A_Gen_T)) = Scope (A_Gen_T)
11977 and then Etype (Etype (A_Gen_T)) /= Etype (A_Gen_T)
11978 then
11979 -- Locate ancestor of parent from the subtype declaration
11980 -- created for the actual.
11981
11982 declare
11983 Decl : Node_Id;
11984
11985 begin
11986 Decl := First (Actual_Decls);
11987 while Present (Decl) loop
11988 if Nkind (Decl) = N_Subtype_Declaration
11989 and then Chars (Defining_Identifier (Decl)) =
11990 Chars (Etype (A_Gen_T))
11991 then
11992 Ancestor := Generic_Parent_Type (Decl);
11993 exit;
11994 else
11995 Next (Decl);
11996 end if;
11997 end loop;
11998 end;
11999
12000 pragma Assert (Present (Ancestor));
12001
12002 -- The ancestor itself may be a previous formal that has been
12003 -- instantiated.
12004
12005 Ancestor := Get_Instance_Of (Ancestor);
12006
12007 else
12008 Ancestor :=
12009 Get_Instance_Of (Base_Type (Get_Instance_Of (A_Gen_T)));
12010 end if;
12011
12012 -- Check whether parent is a previous formal of the current generic
12013
12014 elsif Is_Derived_Type (A_Gen_T)
12015 and then Is_Generic_Type (Etype (A_Gen_T))
12016 and then Scope (A_Gen_T) = Scope (Etype (A_Gen_T))
12017 then
12018 Ancestor := Get_Instance_Of (First_Subtype (Etype (A_Gen_T)));
12019
12020 -- An unusual case: the actual is a type declared in a parent unit,
12021 -- but is not a formal type so there is no instance_of for it.
12022 -- Retrieve it by analyzing the record extension.
12023
12024 elsif Is_Child_Unit (Scope (A_Gen_T))
12025 and then In_Open_Scopes (Scope (Act_T))
12026 and then Is_Generic_Instance (Scope (Act_T))
12027 then
12028 Analyze (Subtype_Mark (Def));
12029 Ancestor := Entity (Subtype_Mark (Def));
12030
12031 else
12032 Ancestor := Get_Instance_Of (Etype (Base_Type (A_Gen_T)));
12033 end if;
12034
12035 -- If the formal derived type has pragma Preelaborable_Initialization
12036 -- then the actual type must have preelaborable initialization.
12037
12038 if Known_To_Have_Preelab_Init (A_Gen_T)
12039 and then not Has_Preelaborable_Initialization (Act_T)
12040 then
12041 Error_Msg_NE
12042 ("actual for & must have preelaborable initialization",
12043 Actual, Gen_T);
12044 end if;
12045
12046 -- Ada 2005 (AI-251)
12047
12048 if Ada_Version >= Ada_2005 and then Is_Interface (Ancestor) then
12049 if not Interface_Present_In_Ancestor (Act_T, Ancestor) then
12050 Error_Msg_NE
12051 ("(Ada 2005) expected type implementing & in instantiation",
12052 Actual, Ancestor);
12053 end if;
12054
12055 -- Finally verify that the (instance of) the ancestor is an ancestor
12056 -- of the actual.
12057
12058 elsif not Is_Ancestor (Base_Type (Ancestor), Act_T) then
12059 Error_Msg_NE
12060 ("expect type derived from & in instantiation",
12061 Actual, First_Subtype (Ancestor));
12062 Abandon_Instantiation (Actual);
12063 end if;
12064
12065 -- Ada 2005 (AI-443): Synchronized formal derived type checks. Note
12066 -- that the formal type declaration has been rewritten as a private
12067 -- extension.
12068
12069 if Ada_Version >= Ada_2005
12070 and then Nkind (Parent (A_Gen_T)) = N_Private_Extension_Declaration
12071 and then Synchronized_Present (Parent (A_Gen_T))
12072 then
12073 -- The actual must be a synchronized tagged type
12074
12075 if not Is_Tagged_Type (Act_T) then
12076 Error_Msg_N
12077 ("actual of synchronized type must be tagged", Actual);
12078 Abandon_Instantiation (Actual);
12079
12080 elsif Nkind (Parent (Act_T)) = N_Full_Type_Declaration
12081 and then Nkind (Type_Definition (Parent (Act_T))) =
12082 N_Derived_Type_Definition
12083 and then not Synchronized_Present
12084 (Type_Definition (Parent (Act_T)))
12085 then
12086 Error_Msg_N
12087 ("actual of synchronized type must be synchronized", Actual);
12088 Abandon_Instantiation (Actual);
12089 end if;
12090 end if;
12091
12092 -- Perform atomic/volatile checks (RM C.6(12)). Note that AI05-0218-1
12093 -- removes the second instance of the phrase "or allow pass by copy".
12094
12095 if Is_Atomic (Act_T) and then not Is_Atomic (Ancestor) then
12096 Error_Msg_N
12097 ("cannot have atomic actual type for non-atomic formal type",
12098 Actual);
12099
12100 elsif Is_Volatile (Act_T) and then not Is_Volatile (Ancestor) then
12101 Error_Msg_N
12102 ("cannot have volatile actual type for non-volatile formal type",
12103 Actual);
12104 end if;
12105
12106 -- It should not be necessary to check for unknown discriminants on
12107 -- Formal, but for some reason Has_Unknown_Discriminants is false for
12108 -- A_Gen_T, so Is_Definite_Subtype incorrectly returns True. This
12109 -- needs fixing. ???
12110
12111 if Is_Definite_Subtype (A_Gen_T)
12112 and then not Unknown_Discriminants_Present (Formal)
12113 and then not Is_Definite_Subtype (Act_T)
12114 then
12115 Error_Msg_N ("actual subtype must be constrained", Actual);
12116 Abandon_Instantiation (Actual);
12117 end if;
12118
12119 if not Unknown_Discriminants_Present (Formal) then
12120 if Is_Constrained (Ancestor) then
12121 if not Is_Constrained (Act_T) then
12122 Error_Msg_N ("actual subtype must be constrained", Actual);
12123 Abandon_Instantiation (Actual);
12124 end if;
12125
12126 -- Ancestor is unconstrained, Check if generic formal and actual
12127 -- agree on constrainedness. The check only applies to array types
12128 -- and discriminated types.
12129
12130 elsif Is_Constrained (Act_T) then
12131 if Ekind (Ancestor) = E_Access_Type
12132 or else (not Is_Constrained (A_Gen_T)
12133 and then Is_Composite_Type (A_Gen_T))
12134 then
12135 Error_Msg_N ("actual subtype must be unconstrained", Actual);
12136 Abandon_Instantiation (Actual);
12137 end if;
12138
12139 -- A class-wide type is only allowed if the formal has unknown
12140 -- discriminants.
12141
12142 elsif Is_Class_Wide_Type (Act_T)
12143 and then not Has_Unknown_Discriminants (Ancestor)
12144 then
12145 Error_Msg_NE
12146 ("actual for & cannot be a class-wide type", Actual, Gen_T);
12147 Abandon_Instantiation (Actual);
12148
12149 -- Otherwise, the formal and actual must have the same number
12150 -- of discriminants and each discriminant of the actual must
12151 -- correspond to a discriminant of the formal.
12152
12153 elsif Has_Discriminants (Act_T)
12154 and then not Has_Unknown_Discriminants (Act_T)
12155 and then Has_Discriminants (Ancestor)
12156 then
12157 Actual_Discr := First_Discriminant (Act_T);
12158 Ancestor_Discr := First_Discriminant (Ancestor);
12159 while Present (Actual_Discr)
12160 and then Present (Ancestor_Discr)
12161 loop
12162 if Base_Type (Act_T) /= Base_Type (Ancestor) and then
12163 No (Corresponding_Discriminant (Actual_Discr))
12164 then
12165 Error_Msg_NE
12166 ("discriminant & does not correspond "
12167 & "to ancestor discriminant", Actual, Actual_Discr);
12168 Abandon_Instantiation (Actual);
12169 end if;
12170
12171 Next_Discriminant (Actual_Discr);
12172 Next_Discriminant (Ancestor_Discr);
12173 end loop;
12174
12175 if Present (Actual_Discr) or else Present (Ancestor_Discr) then
12176 Error_Msg_NE
12177 ("actual for & must have same number of discriminants",
12178 Actual, Gen_T);
12179 Abandon_Instantiation (Actual);
12180 end if;
12181
12182 -- This case should be caught by the earlier check for
12183 -- constrainedness, but the check here is added for completeness.
12184
12185 elsif Has_Discriminants (Act_T)
12186 and then not Has_Unknown_Discriminants (Act_T)
12187 then
12188 Error_Msg_NE
12189 ("actual for & must not have discriminants", Actual, Gen_T);
12190 Abandon_Instantiation (Actual);
12191
12192 elsif Has_Discriminants (Ancestor) then
12193 Error_Msg_NE
12194 ("actual for & must have known discriminants", Actual, Gen_T);
12195 Abandon_Instantiation (Actual);
12196 end if;
12197
12198 if not Subtypes_Statically_Compatible
12199 (Act_T, Ancestor, Formal_Derived_Matching => True)
12200 then
12201 Error_Msg_N
12202 ("constraint on actual is incompatible with formal", Actual);
12203 Abandon_Instantiation (Actual);
12204 end if;
12205 end if;
12206
12207 -- If the formal and actual types are abstract, check that there
12208 -- are no abstract primitives of the actual type that correspond to
12209 -- nonabstract primitives of the formal type (second sentence of
12210 -- RM95 3.9.3(9)).
12211
12212 if Is_Abstract_Type (A_Gen_T) and then Is_Abstract_Type (Act_T) then
12213 Check_Abstract_Primitives : declare
12214 Gen_Prims : constant Elist_Id :=
12215 Primitive_Operations (A_Gen_T);
12216 Gen_Elmt : Elmt_Id;
12217 Gen_Subp : Entity_Id;
12218 Anc_Subp : Entity_Id;
12219 Anc_Formal : Entity_Id;
12220 Anc_F_Type : Entity_Id;
12221
12222 Act_Prims : constant Elist_Id := Primitive_Operations (Act_T);
12223 Act_Elmt : Elmt_Id;
12224 Act_Subp : Entity_Id;
12225 Act_Formal : Entity_Id;
12226 Act_F_Type : Entity_Id;
12227
12228 Subprograms_Correspond : Boolean;
12229
12230 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean;
12231 -- Returns true if T2 is derived directly or indirectly from
12232 -- T1, including derivations from interfaces. T1 and T2 are
12233 -- required to be specific tagged base types.
12234
12235 ------------------------
12236 -- Is_Tagged_Ancestor --
12237 ------------------------
12238
12239 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean
12240 is
12241 Intfc_Elmt : Elmt_Id;
12242
12243 begin
12244 -- The predicate is satisfied if the types are the same
12245
12246 if T1 = T2 then
12247 return True;
12248
12249 -- If we've reached the top of the derivation chain then
12250 -- we know that T1 is not an ancestor of T2.
12251
12252 elsif Etype (T2) = T2 then
12253 return False;
12254
12255 -- Proceed to check T2's immediate parent
12256
12257 elsif Is_Ancestor (T1, Base_Type (Etype (T2))) then
12258 return True;
12259
12260 -- Finally, check to see if T1 is an ancestor of any of T2's
12261 -- progenitors.
12262
12263 else
12264 Intfc_Elmt := First_Elmt (Interfaces (T2));
12265 while Present (Intfc_Elmt) loop
12266 if Is_Ancestor (T1, Node (Intfc_Elmt)) then
12267 return True;
12268 end if;
12269
12270 Next_Elmt (Intfc_Elmt);
12271 end loop;
12272 end if;
12273
12274 return False;
12275 end Is_Tagged_Ancestor;
12276
12277 -- Start of processing for Check_Abstract_Primitives
12278
12279 begin
12280 -- Loop over all of the formal derived type's primitives
12281
12282 Gen_Elmt := First_Elmt (Gen_Prims);
12283 while Present (Gen_Elmt) loop
12284 Gen_Subp := Node (Gen_Elmt);
12285
12286 -- If the primitive of the formal is not abstract, then
12287 -- determine whether there is a corresponding primitive of
12288 -- the actual type that's abstract.
12289
12290 if not Is_Abstract_Subprogram (Gen_Subp) then
12291 Act_Elmt := First_Elmt (Act_Prims);
12292 while Present (Act_Elmt) loop
12293 Act_Subp := Node (Act_Elmt);
12294
12295 -- If we find an abstract primitive of the actual,
12296 -- then we need to test whether it corresponds to the
12297 -- subprogram from which the generic formal primitive
12298 -- is inherited.
12299
12300 if Is_Abstract_Subprogram (Act_Subp) then
12301 Anc_Subp := Alias (Gen_Subp);
12302
12303 -- Test whether we have a corresponding primitive
12304 -- by comparing names, kinds, formal types, and
12305 -- result types.
12306
12307 if Chars (Anc_Subp) = Chars (Act_Subp)
12308 and then Ekind (Anc_Subp) = Ekind (Act_Subp)
12309 then
12310 Anc_Formal := First_Formal (Anc_Subp);
12311 Act_Formal := First_Formal (Act_Subp);
12312 while Present (Anc_Formal)
12313 and then Present (Act_Formal)
12314 loop
12315 Anc_F_Type := Etype (Anc_Formal);
12316 Act_F_Type := Etype (Act_Formal);
12317
12318 if Ekind (Anc_F_Type) =
12319 E_Anonymous_Access_Type
12320 then
12321 Anc_F_Type := Designated_Type (Anc_F_Type);
12322
12323 if Ekind (Act_F_Type) =
12324 E_Anonymous_Access_Type
12325 then
12326 Act_F_Type :=
12327 Designated_Type (Act_F_Type);
12328 else
12329 exit;
12330 end if;
12331
12332 elsif
12333 Ekind (Act_F_Type) = E_Anonymous_Access_Type
12334 then
12335 exit;
12336 end if;
12337
12338 Anc_F_Type := Base_Type (Anc_F_Type);
12339 Act_F_Type := Base_Type (Act_F_Type);
12340
12341 -- If the formal is controlling, then the
12342 -- the type of the actual primitive's formal
12343 -- must be derived directly or indirectly
12344 -- from the type of the ancestor primitive's
12345 -- formal.
12346
12347 if Is_Controlling_Formal (Anc_Formal) then
12348 if not Is_Tagged_Ancestor
12349 (Anc_F_Type, Act_F_Type)
12350 then
12351 exit;
12352 end if;
12353
12354 -- Otherwise the types of the formals must
12355 -- be the same.
12356
12357 elsif Anc_F_Type /= Act_F_Type then
12358 exit;
12359 end if;
12360
12361 Next_Entity (Anc_Formal);
12362 Next_Entity (Act_Formal);
12363 end loop;
12364
12365 -- If we traversed through all of the formals
12366 -- then so far the subprograms correspond, so
12367 -- now check that any result types correspond.
12368
12369 if No (Anc_Formal) and then No (Act_Formal) then
12370 Subprograms_Correspond := True;
12371
12372 if Ekind (Act_Subp) = E_Function then
12373 Anc_F_Type := Etype (Anc_Subp);
12374 Act_F_Type := Etype (Act_Subp);
12375
12376 if Ekind (Anc_F_Type) =
12377 E_Anonymous_Access_Type
12378 then
12379 Anc_F_Type :=
12380 Designated_Type (Anc_F_Type);
12381
12382 if Ekind (Act_F_Type) =
12383 E_Anonymous_Access_Type
12384 then
12385 Act_F_Type :=
12386 Designated_Type (Act_F_Type);
12387 else
12388 Subprograms_Correspond := False;
12389 end if;
12390
12391 elsif
12392 Ekind (Act_F_Type)
12393 = E_Anonymous_Access_Type
12394 then
12395 Subprograms_Correspond := False;
12396 end if;
12397
12398 Anc_F_Type := Base_Type (Anc_F_Type);
12399 Act_F_Type := Base_Type (Act_F_Type);
12400
12401 -- Now either the result types must be
12402 -- the same or, if the result type is
12403 -- controlling, the result type of the
12404 -- actual primitive must descend from the
12405 -- result type of the ancestor primitive.
12406
12407 if Subprograms_Correspond
12408 and then Anc_F_Type /= Act_F_Type
12409 and then
12410 Has_Controlling_Result (Anc_Subp)
12411 and then not Is_Tagged_Ancestor
12412 (Anc_F_Type, Act_F_Type)
12413 then
12414 Subprograms_Correspond := False;
12415 end if;
12416 end if;
12417
12418 -- Found a matching subprogram belonging to
12419 -- formal ancestor type, so actual subprogram
12420 -- corresponds and this violates 3.9.3(9).
12421
12422 if Subprograms_Correspond then
12423 Error_Msg_NE
12424 ("abstract subprogram & overrides "
12425 & "nonabstract subprogram of ancestor",
12426 Actual, Act_Subp);
12427 end if;
12428 end if;
12429 end if;
12430 end if;
12431
12432 Next_Elmt (Act_Elmt);
12433 end loop;
12434 end if;
12435
12436 Next_Elmt (Gen_Elmt);
12437 end loop;
12438 end Check_Abstract_Primitives;
12439 end if;
12440
12441 -- Verify that limitedness matches. If parent is a limited
12442 -- interface then the generic formal is not unless declared
12443 -- explicitly so. If not declared limited, the actual cannot be
12444 -- limited (see AI05-0087).
12445
12446 -- Even though this AI is a binding interpretation, we enable the
12447 -- check only in Ada 2012 mode, because this improper construct
12448 -- shows up in user code and in existing B-tests.
12449
12450 if Is_Limited_Type (Act_T)
12451 and then not Is_Limited_Type (A_Gen_T)
12452 and then Ada_Version >= Ada_2012
12453 then
12454 if In_Instance then
12455 null;
12456 else
12457 Error_Msg_NE
12458 ("actual for non-limited & cannot be a limited type",
12459 Actual, Gen_T);
12460 Explain_Limited_Type (Act_T, Actual);
12461 Abandon_Instantiation (Actual);
12462 end if;
12463 end if;
12464 end Validate_Derived_Type_Instance;
12465
12466 ----------------------------------------
12467 -- Validate_Discriminated_Formal_Type --
12468 ----------------------------------------
12469
12470 procedure Validate_Discriminated_Formal_Type is
12471 Formal_Discr : Entity_Id;
12472 Actual_Discr : Entity_Id;
12473 Formal_Subt : Entity_Id;
12474
12475 begin
12476 if Has_Discriminants (A_Gen_T) then
12477 if not Has_Discriminants (Act_T) then
12478 Error_Msg_NE
12479 ("actual for & must have discriminants", Actual, Gen_T);
12480 Abandon_Instantiation (Actual);
12481
12482 elsif Is_Constrained (Act_T) then
12483 Error_Msg_NE
12484 ("actual for & must be unconstrained", Actual, Gen_T);
12485 Abandon_Instantiation (Actual);
12486
12487 else
12488 Formal_Discr := First_Discriminant (A_Gen_T);
12489 Actual_Discr := First_Discriminant (Act_T);
12490 while Formal_Discr /= Empty loop
12491 if Actual_Discr = Empty then
12492 Error_Msg_NE
12493 ("discriminants on actual do not match formal",
12494 Actual, Gen_T);
12495 Abandon_Instantiation (Actual);
12496 end if;
12497
12498 Formal_Subt := Get_Instance_Of (Etype (Formal_Discr));
12499
12500 -- Access discriminants match if designated types do
12501
12502 if Ekind (Base_Type (Formal_Subt)) = E_Anonymous_Access_Type
12503 and then (Ekind (Base_Type (Etype (Actual_Discr)))) =
12504 E_Anonymous_Access_Type
12505 and then
12506 Get_Instance_Of
12507 (Designated_Type (Base_Type (Formal_Subt))) =
12508 Designated_Type (Base_Type (Etype (Actual_Discr)))
12509 then
12510 null;
12511
12512 elsif Base_Type (Formal_Subt) /=
12513 Base_Type (Etype (Actual_Discr))
12514 then
12515 Error_Msg_NE
12516 ("types of actual discriminants must match formal",
12517 Actual, Gen_T);
12518 Abandon_Instantiation (Actual);
12519
12520 elsif not Subtypes_Statically_Match
12521 (Formal_Subt, Etype (Actual_Discr))
12522 and then Ada_Version >= Ada_95
12523 then
12524 Error_Msg_NE
12525 ("subtypes of actual discriminants must match formal",
12526 Actual, Gen_T);
12527 Abandon_Instantiation (Actual);
12528 end if;
12529
12530 Next_Discriminant (Formal_Discr);
12531 Next_Discriminant (Actual_Discr);
12532 end loop;
12533
12534 if Actual_Discr /= Empty then
12535 Error_Msg_NE
12536 ("discriminants on actual do not match formal",
12537 Actual, Gen_T);
12538 Abandon_Instantiation (Actual);
12539 end if;
12540 end if;
12541 end if;
12542 end Validate_Discriminated_Formal_Type;
12543
12544 ---------------------------------------
12545 -- Validate_Incomplete_Type_Instance --
12546 ---------------------------------------
12547
12548 procedure Validate_Incomplete_Type_Instance is
12549 begin
12550 if not Is_Tagged_Type (Act_T)
12551 and then Is_Tagged_Type (A_Gen_T)
12552 then
12553 Error_Msg_NE
12554 ("actual for & must be a tagged type", Actual, Gen_T);
12555 end if;
12556
12557 Validate_Discriminated_Formal_Type;
12558 end Validate_Incomplete_Type_Instance;
12559
12560 --------------------------------------
12561 -- Validate_Interface_Type_Instance --
12562 --------------------------------------
12563
12564 procedure Validate_Interface_Type_Instance is
12565 begin
12566 if not Is_Interface (Act_T) then
12567 Error_Msg_NE
12568 ("actual for formal interface type must be an interface",
12569 Actual, Gen_T);
12570
12571 elsif Is_Limited_Type (Act_T) /= Is_Limited_Type (A_Gen_T)
12572 or else Is_Task_Interface (A_Gen_T) /= Is_Task_Interface (Act_T)
12573 or else Is_Protected_Interface (A_Gen_T) /=
12574 Is_Protected_Interface (Act_T)
12575 or else Is_Synchronized_Interface (A_Gen_T) /=
12576 Is_Synchronized_Interface (Act_T)
12577 then
12578 Error_Msg_NE
12579 ("actual for interface& does not match (RM 12.5.5(4))",
12580 Actual, Gen_T);
12581 end if;
12582 end Validate_Interface_Type_Instance;
12583
12584 ------------------------------------
12585 -- Validate_Private_Type_Instance --
12586 ------------------------------------
12587
12588 procedure Validate_Private_Type_Instance is
12589 begin
12590 if Is_Limited_Type (Act_T)
12591 and then not Is_Limited_Type (A_Gen_T)
12592 then
12593 if In_Instance then
12594 null;
12595 else
12596 Error_Msg_NE
12597 ("actual for non-limited & cannot be a limited type", Actual,
12598 Gen_T);
12599 Explain_Limited_Type (Act_T, Actual);
12600 Abandon_Instantiation (Actual);
12601 end if;
12602
12603 elsif Known_To_Have_Preelab_Init (A_Gen_T)
12604 and then not Has_Preelaborable_Initialization (Act_T)
12605 then
12606 Error_Msg_NE
12607 ("actual for & must have preelaborable initialization", Actual,
12608 Gen_T);
12609
12610 elsif not Is_Definite_Subtype (Act_T)
12611 and then Is_Definite_Subtype (A_Gen_T)
12612 and then Ada_Version >= Ada_95
12613 then
12614 Error_Msg_NE
12615 ("actual for & must be a definite subtype", Actual, Gen_T);
12616
12617 elsif not Is_Tagged_Type (Act_T)
12618 and then Is_Tagged_Type (A_Gen_T)
12619 then
12620 Error_Msg_NE
12621 ("actual for & must be a tagged type", Actual, Gen_T);
12622 end if;
12623
12624 Validate_Discriminated_Formal_Type;
12625 Ancestor := Gen_T;
12626 end Validate_Private_Type_Instance;
12627
12628 -- Start of processing for Instantiate_Type
12629
12630 begin
12631 if Get_Instance_Of (A_Gen_T) /= A_Gen_T then
12632 Error_Msg_N ("duplicate instantiation of generic type", Actual);
12633 return New_List (Error);
12634
12635 elsif not Is_Entity_Name (Actual)
12636 or else not Is_Type (Entity (Actual))
12637 then
12638 Error_Msg_NE
12639 ("expect valid subtype mark to instantiate &", Actual, Gen_T);
12640 Abandon_Instantiation (Actual);
12641
12642 else
12643 Act_T := Entity (Actual);
12644
12645 -- Ada 2005 (AI-216): An Unchecked_Union subtype shall only be passed
12646 -- as a generic actual parameter if the corresponding formal type
12647 -- does not have a known_discriminant_part, or is a formal derived
12648 -- type that is an Unchecked_Union type.
12649
12650 if Is_Unchecked_Union (Base_Type (Act_T)) then
12651 if not Has_Discriminants (A_Gen_T)
12652 or else (Is_Derived_Type (A_Gen_T)
12653 and then Is_Unchecked_Union (A_Gen_T))
12654 then
12655 null;
12656 else
12657 Error_Msg_N ("unchecked union cannot be the actual for a "
12658 & "discriminated formal type", Act_T);
12659
12660 end if;
12661 end if;
12662
12663 -- Deal with fixed/floating restrictions
12664
12665 if Is_Floating_Point_Type (Act_T) then
12666 Check_Restriction (No_Floating_Point, Actual);
12667 elsif Is_Fixed_Point_Type (Act_T) then
12668 Check_Restriction (No_Fixed_Point, Actual);
12669 end if;
12670
12671 -- Deal with error of using incomplete type as generic actual.
12672 -- This includes limited views of a type, even if the non-limited
12673 -- view may be available.
12674
12675 if Ekind (Act_T) = E_Incomplete_Type
12676 or else (Is_Class_Wide_Type (Act_T)
12677 and then Ekind (Root_Type (Act_T)) = E_Incomplete_Type)
12678 then
12679 -- If the formal is an incomplete type, the actual can be
12680 -- incomplete as well.
12681
12682 if Ekind (A_Gen_T) = E_Incomplete_Type then
12683 null;
12684
12685 elsif Is_Class_Wide_Type (Act_T)
12686 or else No (Full_View (Act_T))
12687 then
12688 Error_Msg_N ("premature use of incomplete type", Actual);
12689 Abandon_Instantiation (Actual);
12690 else
12691 Act_T := Full_View (Act_T);
12692 Set_Entity (Actual, Act_T);
12693
12694 if Has_Private_Component (Act_T) then
12695 Error_Msg_N
12696 ("premature use of type with private component", Actual);
12697 end if;
12698 end if;
12699
12700 -- Deal with error of premature use of private type as generic actual
12701
12702 elsif Is_Private_Type (Act_T)
12703 and then Is_Private_Type (Base_Type (Act_T))
12704 and then not Is_Generic_Type (Act_T)
12705 and then not Is_Derived_Type (Act_T)
12706 and then No (Full_View (Root_Type (Act_T)))
12707 then
12708 -- If the formal is an incomplete type, the actual can be
12709 -- private or incomplete as well.
12710
12711 if Ekind (A_Gen_T) = E_Incomplete_Type then
12712 null;
12713 else
12714 Error_Msg_N ("premature use of private type", Actual);
12715 end if;
12716
12717 elsif Has_Private_Component (Act_T) then
12718 Error_Msg_N
12719 ("premature use of type with private component", Actual);
12720 end if;
12721
12722 Set_Instance_Of (A_Gen_T, Act_T);
12723
12724 -- If the type is generic, the class-wide type may also be used
12725
12726 if Is_Tagged_Type (A_Gen_T)
12727 and then Is_Tagged_Type (Act_T)
12728 and then not Is_Class_Wide_Type (A_Gen_T)
12729 then
12730 Set_Instance_Of (Class_Wide_Type (A_Gen_T),
12731 Class_Wide_Type (Act_T));
12732 end if;
12733
12734 if not Is_Abstract_Type (A_Gen_T)
12735 and then Is_Abstract_Type (Act_T)
12736 then
12737 Error_Msg_N
12738 ("actual of non-abstract formal cannot be abstract", Actual);
12739 end if;
12740
12741 -- A generic scalar type is a first subtype for which we generate
12742 -- an anonymous base type. Indicate that the instance of this base
12743 -- is the base type of the actual.
12744
12745 if Is_Scalar_Type (A_Gen_T) then
12746 Set_Instance_Of (Etype (A_Gen_T), Etype (Act_T));
12747 end if;
12748 end if;
12749
12750 if Error_Posted (Act_T) then
12751 null;
12752 else
12753 case Nkind (Def) is
12754 when N_Formal_Private_Type_Definition =>
12755 Validate_Private_Type_Instance;
12756
12757 when N_Formal_Incomplete_Type_Definition =>
12758 Validate_Incomplete_Type_Instance;
12759
12760 when N_Formal_Derived_Type_Definition =>
12761 Validate_Derived_Type_Instance;
12762
12763 when N_Formal_Discrete_Type_Definition =>
12764 if not Is_Discrete_Type (Act_T) then
12765 Error_Msg_NE
12766 ("expect discrete type in instantiation of&",
12767 Actual, Gen_T);
12768 Abandon_Instantiation (Actual);
12769 end if;
12770
12771 Diagnose_Predicated_Actual;
12772
12773 when N_Formal_Signed_Integer_Type_Definition =>
12774 if not Is_Signed_Integer_Type (Act_T) then
12775 Error_Msg_NE
12776 ("expect signed integer type in instantiation of&",
12777 Actual, Gen_T);
12778 Abandon_Instantiation (Actual);
12779 end if;
12780
12781 Diagnose_Predicated_Actual;
12782
12783 when N_Formal_Modular_Type_Definition =>
12784 if not Is_Modular_Integer_Type (Act_T) then
12785 Error_Msg_NE
12786 ("expect modular type in instantiation of &",
12787 Actual, Gen_T);
12788 Abandon_Instantiation (Actual);
12789 end if;
12790
12791 Diagnose_Predicated_Actual;
12792
12793 when N_Formal_Floating_Point_Definition =>
12794 if not Is_Floating_Point_Type (Act_T) then
12795 Error_Msg_NE
12796 ("expect float type in instantiation of &", Actual, Gen_T);
12797 Abandon_Instantiation (Actual);
12798 end if;
12799
12800 when N_Formal_Ordinary_Fixed_Point_Definition =>
12801 if not Is_Ordinary_Fixed_Point_Type (Act_T) then
12802 Error_Msg_NE
12803 ("expect ordinary fixed point type in instantiation of &",
12804 Actual, Gen_T);
12805 Abandon_Instantiation (Actual);
12806 end if;
12807
12808 when N_Formal_Decimal_Fixed_Point_Definition =>
12809 if not Is_Decimal_Fixed_Point_Type (Act_T) then
12810 Error_Msg_NE
12811 ("expect decimal type in instantiation of &",
12812 Actual, Gen_T);
12813 Abandon_Instantiation (Actual);
12814 end if;
12815
12816 when N_Array_Type_Definition =>
12817 Validate_Array_Type_Instance;
12818
12819 when N_Access_To_Object_Definition =>
12820 Validate_Access_Type_Instance;
12821
12822 when N_Access_Function_Definition
12823 | N_Access_Procedure_Definition
12824 =>
12825 Validate_Access_Subprogram_Instance;
12826
12827 when N_Record_Definition =>
12828 Validate_Interface_Type_Instance;
12829
12830 when N_Derived_Type_Definition =>
12831 Validate_Derived_Interface_Type_Instance;
12832
12833 when others =>
12834 raise Program_Error;
12835 end case;
12836 end if;
12837
12838 Subt := New_Copy (Gen_T);
12839
12840 -- Use adjusted sloc of subtype name as the location for other nodes in
12841 -- the subtype declaration.
12842
12843 Loc := Sloc (Subt);
12844
12845 Decl_Node :=
12846 Make_Subtype_Declaration (Loc,
12847 Defining_Identifier => Subt,
12848 Subtype_Indication => New_Occurrence_Of (Act_T, Loc));
12849
12850 if Is_Private_Type (Act_T) then
12851 Set_Has_Private_View (Subtype_Indication (Decl_Node));
12852
12853 elsif Is_Access_Type (Act_T)
12854 and then Is_Private_Type (Designated_Type (Act_T))
12855 then
12856 Set_Has_Private_View (Subtype_Indication (Decl_Node));
12857 end if;
12858
12859 -- In Ada 2012 the actual may be a limited view. Indicate that
12860 -- the local subtype must be treated as such.
12861
12862 if From_Limited_With (Act_T) then
12863 Set_Ekind (Subt, E_Incomplete_Subtype);
12864 Set_From_Limited_With (Subt);
12865 end if;
12866
12867 Decl_Nodes := New_List (Decl_Node);
12868
12869 -- Flag actual derived types so their elaboration produces the
12870 -- appropriate renamings for the primitive operations of the ancestor.
12871 -- Flag actual for formal private types as well, to determine whether
12872 -- operations in the private part may override inherited operations.
12873 -- If the formal has an interface list, the ancestor is not the
12874 -- parent, but the analyzed formal that includes the interface
12875 -- operations of all its progenitors.
12876
12877 -- Same treatment for formal private types, so we can check whether the
12878 -- type is tagged limited when validating derivations in the private
12879 -- part. (See AI05-096).
12880
12881 if Nkind (Def) = N_Formal_Derived_Type_Definition then
12882 if Present (Interface_List (Def)) then
12883 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
12884 else
12885 Set_Generic_Parent_Type (Decl_Node, Ancestor);
12886 end if;
12887
12888 elsif Nkind_In (Def, N_Formal_Private_Type_Definition,
12889 N_Formal_Incomplete_Type_Definition)
12890 then
12891 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
12892 end if;
12893
12894 -- If the actual is a synchronized type that implements an interface,
12895 -- the primitive operations are attached to the corresponding record,
12896 -- and we have to treat it as an additional generic actual, so that its
12897 -- primitive operations become visible in the instance. The task or
12898 -- protected type itself does not carry primitive operations.
12899
12900 if Is_Concurrent_Type (Act_T)
12901 and then Is_Tagged_Type (Act_T)
12902 and then Present (Corresponding_Record_Type (Act_T))
12903 and then Present (Ancestor)
12904 and then Is_Interface (Ancestor)
12905 then
12906 declare
12907 Corr_Rec : constant Entity_Id :=
12908 Corresponding_Record_Type (Act_T);
12909 New_Corr : Entity_Id;
12910 Corr_Decl : Node_Id;
12911
12912 begin
12913 New_Corr := Make_Temporary (Loc, 'S');
12914 Corr_Decl :=
12915 Make_Subtype_Declaration (Loc,
12916 Defining_Identifier => New_Corr,
12917 Subtype_Indication =>
12918 New_Occurrence_Of (Corr_Rec, Loc));
12919 Append_To (Decl_Nodes, Corr_Decl);
12920
12921 if Ekind (Act_T) = E_Task_Type then
12922 Set_Ekind (Subt, E_Task_Subtype);
12923 else
12924 Set_Ekind (Subt, E_Protected_Subtype);
12925 end if;
12926
12927 Set_Corresponding_Record_Type (Subt, Corr_Rec);
12928 Set_Generic_Parent_Type (Corr_Decl, Ancestor);
12929 Set_Generic_Parent_Type (Decl_Node, Empty);
12930 end;
12931 end if;
12932
12933 -- For a floating-point type, capture dimension info if any, because
12934 -- the generated subtype declaration does not come from source and
12935 -- will not process dimensions.
12936
12937 if Is_Floating_Point_Type (Act_T) then
12938 Copy_Dimensions (Act_T, Subt);
12939 end if;
12940
12941 return Decl_Nodes;
12942 end Instantiate_Type;
12943
12944 ---------------------
12945 -- Is_In_Main_Unit --
12946 ---------------------
12947
12948 function Is_In_Main_Unit (N : Node_Id) return Boolean is
12949 Unum : constant Unit_Number_Type := Get_Source_Unit (N);
12950 Current_Unit : Node_Id;
12951
12952 begin
12953 if Unum = Main_Unit then
12954 return True;
12955
12956 -- If the current unit is a subunit then it is either the main unit or
12957 -- is being compiled as part of the main unit.
12958
12959 elsif Nkind (N) = N_Compilation_Unit then
12960 return Nkind (Unit (N)) = N_Subunit;
12961 end if;
12962
12963 Current_Unit := Parent (N);
12964 while Present (Current_Unit)
12965 and then Nkind (Current_Unit) /= N_Compilation_Unit
12966 loop
12967 Current_Unit := Parent (Current_Unit);
12968 end loop;
12969
12970 -- The instantiation node is in the main unit, or else the current node
12971 -- (perhaps as the result of nested instantiations) is in the main unit,
12972 -- or in the declaration of the main unit, which in this last case must
12973 -- be a body.
12974
12975 return
12976 Current_Unit = Cunit (Main_Unit)
12977 or else Current_Unit = Library_Unit (Cunit (Main_Unit))
12978 or else (Present (Current_Unit)
12979 and then Present (Library_Unit (Current_Unit))
12980 and then Is_In_Main_Unit (Library_Unit (Current_Unit)));
12981 end Is_In_Main_Unit;
12982
12983 ----------------------------
12984 -- Load_Parent_Of_Generic --
12985 ----------------------------
12986
12987 procedure Load_Parent_Of_Generic
12988 (N : Node_Id;
12989 Spec : Node_Id;
12990 Body_Optional : Boolean := False)
12991 is
12992 Comp_Unit : constant Node_Id := Cunit (Get_Source_Unit (Spec));
12993 Saved_Style_Check : constant Boolean := Style_Check;
12994 Saved_Warnings : constant Warning_Record := Save_Warnings;
12995 True_Parent : Node_Id;
12996 Inst_Node : Node_Id;
12997 OK : Boolean;
12998 Previous_Instances : constant Elist_Id := New_Elmt_List;
12999
13000 procedure Collect_Previous_Instances (Decls : List_Id);
13001 -- Collect all instantiations in the given list of declarations, that
13002 -- precede the generic that we need to load. If the bodies of these
13003 -- instantiations are available, we must analyze them, to ensure that
13004 -- the public symbols generated are the same when the unit is compiled
13005 -- to generate code, and when it is compiled in the context of a unit
13006 -- that needs a particular nested instance. This process is applied to
13007 -- both package and subprogram instances.
13008
13009 --------------------------------
13010 -- Collect_Previous_Instances --
13011 --------------------------------
13012
13013 procedure Collect_Previous_Instances (Decls : List_Id) is
13014 Decl : Node_Id;
13015
13016 begin
13017 Decl := First (Decls);
13018 while Present (Decl) loop
13019 if Sloc (Decl) >= Sloc (Inst_Node) then
13020 return;
13021
13022 -- If Decl is an instantiation, then record it as requiring
13023 -- instantiation of the corresponding body, except if it is an
13024 -- abbreviated instantiation generated internally for conformance
13025 -- checking purposes only for the case of a formal package
13026 -- declared without a box (see Instantiate_Formal_Package). Such
13027 -- an instantiation does not generate any code (the actual code
13028 -- comes from actual) and thus does not need to be analyzed here.
13029 -- If the instantiation appears with a generic package body it is
13030 -- not analyzed here either.
13031
13032 elsif Nkind (Decl) = N_Package_Instantiation
13033 and then not Is_Internal (Defining_Entity (Decl))
13034 then
13035 Append_Elmt (Decl, Previous_Instances);
13036
13037 -- For a subprogram instantiation, omit instantiations intrinsic
13038 -- operations (Unchecked_Conversions, etc.) that have no bodies.
13039
13040 elsif Nkind_In (Decl, N_Function_Instantiation,
13041 N_Procedure_Instantiation)
13042 and then not Is_Intrinsic_Subprogram (Entity (Name (Decl)))
13043 then
13044 Append_Elmt (Decl, Previous_Instances);
13045
13046 elsif Nkind (Decl) = N_Package_Declaration then
13047 Collect_Previous_Instances
13048 (Visible_Declarations (Specification (Decl)));
13049 Collect_Previous_Instances
13050 (Private_Declarations (Specification (Decl)));
13051
13052 -- Previous non-generic bodies may contain instances as well
13053
13054 elsif Nkind (Decl) = N_Package_Body
13055 and then Ekind (Corresponding_Spec (Decl)) /= E_Generic_Package
13056 then
13057 Collect_Previous_Instances (Declarations (Decl));
13058
13059 elsif Nkind (Decl) = N_Subprogram_Body
13060 and then not Acts_As_Spec (Decl)
13061 and then not Is_Generic_Subprogram (Corresponding_Spec (Decl))
13062 then
13063 Collect_Previous_Instances (Declarations (Decl));
13064 end if;
13065
13066 Next (Decl);
13067 end loop;
13068 end Collect_Previous_Instances;
13069
13070 -- Start of processing for Load_Parent_Of_Generic
13071
13072 begin
13073 if not In_Same_Source_Unit (N, Spec)
13074 or else Nkind (Unit (Comp_Unit)) = N_Package_Declaration
13075 or else (Nkind (Unit (Comp_Unit)) = N_Package_Body
13076 and then not Is_In_Main_Unit (Spec))
13077 then
13078 -- Find body of parent of spec, and analyze it. A special case arises
13079 -- when the parent is an instantiation, that is to say when we are
13080 -- currently instantiating a nested generic. In that case, there is
13081 -- no separate file for the body of the enclosing instance. Instead,
13082 -- the enclosing body must be instantiated as if it were a pending
13083 -- instantiation, in order to produce the body for the nested generic
13084 -- we require now. Note that in that case the generic may be defined
13085 -- in a package body, the instance defined in the same package body,
13086 -- and the original enclosing body may not be in the main unit.
13087
13088 Inst_Node := Empty;
13089
13090 True_Parent := Parent (Spec);
13091 while Present (True_Parent)
13092 and then Nkind (True_Parent) /= N_Compilation_Unit
13093 loop
13094 if Nkind (True_Parent) = N_Package_Declaration
13095 and then
13096 Nkind (Original_Node (True_Parent)) = N_Package_Instantiation
13097 then
13098 -- Parent is a compilation unit that is an instantiation.
13099 -- Instantiation node has been replaced with package decl.
13100
13101 Inst_Node := Original_Node (True_Parent);
13102 exit;
13103
13104 elsif Nkind (True_Parent) = N_Package_Declaration
13105 and then Present (Generic_Parent (Specification (True_Parent)))
13106 and then Nkind (Parent (True_Parent)) /= N_Compilation_Unit
13107 then
13108 -- Parent is an instantiation within another specification.
13109 -- Declaration for instance has been inserted before original
13110 -- instantiation node. A direct link would be preferable?
13111
13112 Inst_Node := Next (True_Parent);
13113 while Present (Inst_Node)
13114 and then Nkind (Inst_Node) /= N_Package_Instantiation
13115 loop
13116 Next (Inst_Node);
13117 end loop;
13118
13119 -- If the instance appears within a generic, and the generic
13120 -- unit is defined within a formal package of the enclosing
13121 -- generic, there is no generic body available, and none
13122 -- needed. A more precise test should be used ???
13123
13124 if No (Inst_Node) then
13125 return;
13126 end if;
13127
13128 exit;
13129
13130 else
13131 True_Parent := Parent (True_Parent);
13132 end if;
13133 end loop;
13134
13135 -- Case where we are currently instantiating a nested generic
13136
13137 if Present (Inst_Node) then
13138 if Nkind (Parent (True_Parent)) = N_Compilation_Unit then
13139
13140 -- Instantiation node and declaration of instantiated package
13141 -- were exchanged when only the declaration was needed.
13142 -- Restore instantiation node before proceeding with body.
13143
13144 Set_Unit (Parent (True_Parent), Inst_Node);
13145 end if;
13146
13147 -- Now complete instantiation of enclosing body, if it appears in
13148 -- some other unit. If it appears in the current unit, the body
13149 -- will have been instantiated already.
13150
13151 if No (Corresponding_Body (Instance_Spec (Inst_Node))) then
13152
13153 -- We need to determine the expander mode to instantiate the
13154 -- enclosing body. Because the generic body we need may use
13155 -- global entities declared in the enclosing package (including
13156 -- aggregates) it is in general necessary to compile this body
13157 -- with expansion enabled, except if we are within a generic
13158 -- package, in which case the usual generic rule applies.
13159
13160 declare
13161 Exp_Status : Boolean := True;
13162 Scop : Entity_Id;
13163
13164 begin
13165 -- Loop through scopes looking for generic package
13166
13167 Scop := Scope (Defining_Entity (Instance_Spec (Inst_Node)));
13168 while Present (Scop)
13169 and then Scop /= Standard_Standard
13170 loop
13171 if Ekind (Scop) = E_Generic_Package then
13172 Exp_Status := False;
13173 exit;
13174 end if;
13175
13176 Scop := Scope (Scop);
13177 end loop;
13178
13179 -- Collect previous instantiations in the unit that contains
13180 -- the desired generic.
13181
13182 if Nkind (Parent (True_Parent)) /= N_Compilation_Unit
13183 and then not Body_Optional
13184 then
13185 declare
13186 Decl : Elmt_Id;
13187 Info : Pending_Body_Info;
13188 Par : Node_Id;
13189
13190 begin
13191 Par := Parent (Inst_Node);
13192 while Present (Par) loop
13193 exit when Nkind (Parent (Par)) = N_Compilation_Unit;
13194 Par := Parent (Par);
13195 end loop;
13196
13197 pragma Assert (Present (Par));
13198
13199 if Nkind (Par) = N_Package_Body then
13200 Collect_Previous_Instances (Declarations (Par));
13201
13202 elsif Nkind (Par) = N_Package_Declaration then
13203 Collect_Previous_Instances
13204 (Visible_Declarations (Specification (Par)));
13205 Collect_Previous_Instances
13206 (Private_Declarations (Specification (Par)));
13207
13208 else
13209 -- Enclosing unit is a subprogram body. In this
13210 -- case all instance bodies are processed in order
13211 -- and there is no need to collect them separately.
13212
13213 null;
13214 end if;
13215
13216 Decl := First_Elmt (Previous_Instances);
13217 while Present (Decl) loop
13218 Info :=
13219 (Inst_Node => Node (Decl),
13220 Act_Decl =>
13221 Instance_Spec (Node (Decl)),
13222 Expander_Status => Exp_Status,
13223 Current_Sem_Unit =>
13224 Get_Code_Unit (Sloc (Node (Decl))),
13225 Scope_Suppress => Scope_Suppress,
13226 Local_Suppress_Stack_Top =>
13227 Local_Suppress_Stack_Top,
13228 Version => Ada_Version,
13229 Version_Pragma => Ada_Version_Pragma,
13230 Warnings => Save_Warnings,
13231 SPARK_Mode => SPARK_Mode,
13232 SPARK_Mode_Pragma => SPARK_Mode_Pragma);
13233
13234 -- Package instance
13235
13236 if
13237 Nkind (Node (Decl)) = N_Package_Instantiation
13238 then
13239 Instantiate_Package_Body
13240 (Info, Body_Optional => True);
13241
13242 -- Subprogram instance
13243
13244 else
13245 -- The instance_spec is in the wrapper package,
13246 -- usually followed by its local renaming
13247 -- declaration. See Build_Subprogram_Renaming
13248 -- for details. If the instance carries aspects,
13249 -- these result in the corresponding pragmas,
13250 -- inserted after the subprogram declaration.
13251 -- They must be skipped as well when retrieving
13252 -- the desired spec. A direct link would be
13253 -- more robust ???
13254
13255 declare
13256 Decl : Node_Id :=
13257 (Last (Visible_Declarations
13258 (Specification (Info.Act_Decl))));
13259 begin
13260 while Nkind_In (Decl,
13261 N_Subprogram_Renaming_Declaration, N_Pragma)
13262 loop
13263 Decl := Prev (Decl);
13264 end loop;
13265
13266 Info.Act_Decl := Decl;
13267 end;
13268
13269 Instantiate_Subprogram_Body
13270 (Info, Body_Optional => True);
13271 end if;
13272
13273 Next_Elmt (Decl);
13274 end loop;
13275 end;
13276 end if;
13277
13278 Instantiate_Package_Body
13279 (Body_Info =>
13280 ((Inst_Node => Inst_Node,
13281 Act_Decl => True_Parent,
13282 Expander_Status => Exp_Status,
13283 Current_Sem_Unit => Get_Code_Unit
13284 (Sloc (Inst_Node)),
13285 Scope_Suppress => Scope_Suppress,
13286 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
13287 Version => Ada_Version,
13288 Version_Pragma => Ada_Version_Pragma,
13289 Warnings => Save_Warnings,
13290 SPARK_Mode => SPARK_Mode,
13291 SPARK_Mode_Pragma => SPARK_Mode_Pragma)),
13292 Body_Optional => Body_Optional);
13293 end;
13294 end if;
13295
13296 -- Case where we are not instantiating a nested generic
13297
13298 else
13299 Opt.Style_Check := False;
13300 Expander_Mode_Save_And_Set (True);
13301 Load_Needed_Body (Comp_Unit, OK);
13302 Opt.Style_Check := Saved_Style_Check;
13303 Restore_Warnings (Saved_Warnings);
13304 Expander_Mode_Restore;
13305
13306 if not OK
13307 and then Unit_Requires_Body (Defining_Entity (Spec))
13308 and then not Body_Optional
13309 then
13310 declare
13311 Bname : constant Unit_Name_Type :=
13312 Get_Body_Name (Get_Unit_Name (Unit (Comp_Unit)));
13313
13314 begin
13315 -- In CodePeer mode, the missing body may make the analysis
13316 -- incomplete, but we do not treat it as fatal.
13317
13318 if CodePeer_Mode then
13319 return;
13320
13321 else
13322 Error_Msg_Unit_1 := Bname;
13323 Error_Msg_N ("this instantiation requires$!", N);
13324 Error_Msg_File_1 :=
13325 Get_File_Name (Bname, Subunit => False);
13326 Error_Msg_N ("\but file{ was not found!", N);
13327 raise Unrecoverable_Error;
13328 end if;
13329 end;
13330 end if;
13331 end if;
13332 end if;
13333
13334 -- If loading parent of the generic caused an instantiation circularity,
13335 -- we abandon compilation at this point, because otherwise in some cases
13336 -- we get into trouble with infinite recursions after this point.
13337
13338 if Circularity_Detected then
13339 raise Unrecoverable_Error;
13340 end if;
13341 end Load_Parent_Of_Generic;
13342
13343 ---------------------------------
13344 -- Map_Formal_Package_Entities --
13345 ---------------------------------
13346
13347 procedure Map_Formal_Package_Entities (Form : Entity_Id; Act : Entity_Id) is
13348 E1 : Entity_Id;
13349 E2 : Entity_Id;
13350
13351 begin
13352 Set_Instance_Of (Form, Act);
13353
13354 -- Traverse formal and actual package to map the corresponding entities.
13355 -- We skip over internal entities that may be generated during semantic
13356 -- analysis, and find the matching entities by name, given that they
13357 -- must appear in the same order.
13358
13359 E1 := First_Entity (Form);
13360 E2 := First_Entity (Act);
13361 while Present (E1) and then E1 /= First_Private_Entity (Form) loop
13362 -- Could this test be a single condition??? Seems like it could, and
13363 -- isn't FPE (Form) a constant anyway???
13364
13365 if not Is_Internal (E1)
13366 and then Present (Parent (E1))
13367 and then not Is_Class_Wide_Type (E1)
13368 and then not Is_Internal_Name (Chars (E1))
13369 then
13370 while Present (E2) and then Chars (E2) /= Chars (E1) loop
13371 Next_Entity (E2);
13372 end loop;
13373
13374 if No (E2) then
13375 exit;
13376 else
13377 Set_Instance_Of (E1, E2);
13378
13379 if Is_Type (E1) and then Is_Tagged_Type (E2) then
13380 Set_Instance_Of (Class_Wide_Type (E1), Class_Wide_Type (E2));
13381 end if;
13382
13383 if Is_Constrained (E1) then
13384 Set_Instance_Of (Base_Type (E1), Base_Type (E2));
13385 end if;
13386
13387 if Ekind (E1) = E_Package and then No (Renamed_Object (E1)) then
13388 Map_Formal_Package_Entities (E1, E2);
13389 end if;
13390 end if;
13391 end if;
13392
13393 Next_Entity (E1);
13394 end loop;
13395 end Map_Formal_Package_Entities;
13396
13397 -----------------------
13398 -- Move_Freeze_Nodes --
13399 -----------------------
13400
13401 procedure Move_Freeze_Nodes
13402 (Out_Of : Entity_Id;
13403 After : Node_Id;
13404 L : List_Id)
13405 is
13406 Decl : Node_Id;
13407 Next_Decl : Node_Id;
13408 Next_Node : Node_Id := After;
13409 Spec : Node_Id;
13410
13411 function Is_Outer_Type (T : Entity_Id) return Boolean;
13412 -- Check whether entity is declared in a scope external to that of the
13413 -- generic unit.
13414
13415 -------------------
13416 -- Is_Outer_Type --
13417 -------------------
13418
13419 function Is_Outer_Type (T : Entity_Id) return Boolean is
13420 Scop : Entity_Id := Scope (T);
13421
13422 begin
13423 if Scope_Depth (Scop) < Scope_Depth (Out_Of) then
13424 return True;
13425
13426 else
13427 while Scop /= Standard_Standard loop
13428 if Scop = Out_Of then
13429 return False;
13430 else
13431 Scop := Scope (Scop);
13432 end if;
13433 end loop;
13434
13435 return True;
13436 end if;
13437 end Is_Outer_Type;
13438
13439 -- Start of processing for Move_Freeze_Nodes
13440
13441 begin
13442 if No (L) then
13443 return;
13444 end if;
13445
13446 -- First remove the freeze nodes that may appear before all other
13447 -- declarations.
13448
13449 Decl := First (L);
13450 while Present (Decl)
13451 and then Nkind (Decl) = N_Freeze_Entity
13452 and then Is_Outer_Type (Entity (Decl))
13453 loop
13454 Decl := Remove_Head (L);
13455 Insert_After (Next_Node, Decl);
13456 Set_Analyzed (Decl, False);
13457 Next_Node := Decl;
13458 Decl := First (L);
13459 end loop;
13460
13461 -- Next scan the list of declarations and remove each freeze node that
13462 -- appears ahead of the current node.
13463
13464 while Present (Decl) loop
13465 while Present (Next (Decl))
13466 and then Nkind (Next (Decl)) = N_Freeze_Entity
13467 and then Is_Outer_Type (Entity (Next (Decl)))
13468 loop
13469 Next_Decl := Remove_Next (Decl);
13470 Insert_After (Next_Node, Next_Decl);
13471 Set_Analyzed (Next_Decl, False);
13472 Next_Node := Next_Decl;
13473 end loop;
13474
13475 -- If the declaration is a nested package or concurrent type, then
13476 -- recurse. Nested generic packages will have been processed from the
13477 -- inside out.
13478
13479 case Nkind (Decl) is
13480 when N_Package_Declaration =>
13481 Spec := Specification (Decl);
13482
13483 when N_Task_Type_Declaration =>
13484 Spec := Task_Definition (Decl);
13485
13486 when N_Protected_Type_Declaration =>
13487 Spec := Protected_Definition (Decl);
13488
13489 when others =>
13490 Spec := Empty;
13491 end case;
13492
13493 if Present (Spec) then
13494 Move_Freeze_Nodes (Out_Of, Next_Node, Visible_Declarations (Spec));
13495 Move_Freeze_Nodes (Out_Of, Next_Node, Private_Declarations (Spec));
13496 end if;
13497
13498 Next (Decl);
13499 end loop;
13500 end Move_Freeze_Nodes;
13501
13502 ----------------
13503 -- Next_Assoc --
13504 ----------------
13505
13506 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr is
13507 begin
13508 return Generic_Renamings.Table (E).Next_In_HTable;
13509 end Next_Assoc;
13510
13511 ------------------------
13512 -- Preanalyze_Actuals --
13513 ------------------------
13514
13515 procedure Preanalyze_Actuals (N : Node_Id; Inst : Entity_Id := Empty) is
13516 Assoc : Node_Id;
13517 Act : Node_Id;
13518 Errs : constant Nat := Serious_Errors_Detected;
13519
13520 Cur : Entity_Id := Empty;
13521 -- Current homograph of the instance name
13522
13523 Vis : Boolean;
13524 -- Saved visibility status of the current homograph
13525
13526 begin
13527 Assoc := First (Generic_Associations (N));
13528
13529 -- If the instance is a child unit, its name may hide an outer homonym,
13530 -- so make it invisible to perform name resolution on the actuals.
13531
13532 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name
13533 and then Present
13534 (Current_Entity (Defining_Identifier (Defining_Unit_Name (N))))
13535 then
13536 Cur := Current_Entity (Defining_Identifier (Defining_Unit_Name (N)));
13537
13538 if Is_Compilation_Unit (Cur) then
13539 Vis := Is_Immediately_Visible (Cur);
13540 Set_Is_Immediately_Visible (Cur, False);
13541 else
13542 Cur := Empty;
13543 end if;
13544 end if;
13545
13546 while Present (Assoc) loop
13547 if Nkind (Assoc) /= N_Others_Choice then
13548 Act := Explicit_Generic_Actual_Parameter (Assoc);
13549
13550 -- Within a nested instantiation, a defaulted actual is an empty
13551 -- association, so nothing to analyze. If the subprogram actual
13552 -- is an attribute, analyze prefix only, because actual is not a
13553 -- complete attribute reference.
13554
13555 -- If actual is an allocator, analyze expression only. The full
13556 -- analysis can generate code, and if instance is a compilation
13557 -- unit we have to wait until the package instance is installed
13558 -- to have a proper place to insert this code.
13559
13560 -- String literals may be operators, but at this point we do not
13561 -- know whether the actual is a formal subprogram or a string.
13562
13563 if No (Act) then
13564 null;
13565
13566 elsif Nkind (Act) = N_Attribute_Reference then
13567 Analyze (Prefix (Act));
13568
13569 elsif Nkind (Act) = N_Explicit_Dereference then
13570 Analyze (Prefix (Act));
13571
13572 elsif Nkind (Act) = N_Allocator then
13573 declare
13574 Expr : constant Node_Id := Expression (Act);
13575
13576 begin
13577 if Nkind (Expr) = N_Subtype_Indication then
13578 Analyze (Subtype_Mark (Expr));
13579
13580 -- Analyze separately each discriminant constraint, when
13581 -- given with a named association.
13582
13583 declare
13584 Constr : Node_Id;
13585
13586 begin
13587 Constr := First (Constraints (Constraint (Expr)));
13588 while Present (Constr) loop
13589 if Nkind (Constr) = N_Discriminant_Association then
13590 Analyze (Expression (Constr));
13591 else
13592 Analyze (Constr);
13593 end if;
13594
13595 Next (Constr);
13596 end loop;
13597 end;
13598
13599 else
13600 Analyze (Expr);
13601 end if;
13602 end;
13603
13604 elsif Nkind (Act) /= N_Operator_Symbol then
13605 Analyze (Act);
13606
13607 -- Within a package instance, mark actuals that are limited
13608 -- views, so their use can be moved to the body of the
13609 -- enclosing unit.
13610
13611 if Is_Entity_Name (Act)
13612 and then Is_Type (Entity (Act))
13613 and then From_Limited_With (Entity (Act))
13614 and then Present (Inst)
13615 then
13616 Append_Elmt (Entity (Act), Incomplete_Actuals (Inst));
13617 end if;
13618 end if;
13619
13620 if Errs /= Serious_Errors_Detected then
13621
13622 -- Do a minimal analysis of the generic, to prevent spurious
13623 -- warnings complaining about the generic being unreferenced,
13624 -- before abandoning the instantiation.
13625
13626 Analyze (Name (N));
13627
13628 if Is_Entity_Name (Name (N))
13629 and then Etype (Name (N)) /= Any_Type
13630 then
13631 Generate_Reference (Entity (Name (N)), Name (N));
13632 Set_Is_Instantiated (Entity (Name (N)));
13633 end if;
13634
13635 if Present (Cur) then
13636
13637 -- For the case of a child instance hiding an outer homonym,
13638 -- provide additional warning which might explain the error.
13639
13640 Set_Is_Immediately_Visible (Cur, Vis);
13641 Error_Msg_NE
13642 ("& hides outer unit with the same name??",
13643 N, Defining_Unit_Name (N));
13644 end if;
13645
13646 Abandon_Instantiation (Act);
13647 end if;
13648 end if;
13649
13650 Next (Assoc);
13651 end loop;
13652
13653 if Present (Cur) then
13654 Set_Is_Immediately_Visible (Cur, Vis);
13655 end if;
13656 end Preanalyze_Actuals;
13657
13658 -------------------
13659 -- Remove_Parent --
13660 -------------------
13661
13662 procedure Remove_Parent (In_Body : Boolean := False) is
13663 S : Entity_Id := Current_Scope;
13664 -- S is the scope containing the instantiation just completed. The scope
13665 -- stack contains the parent instances of the instantiation, followed by
13666 -- the original S.
13667
13668 Cur_P : Entity_Id;
13669 E : Entity_Id;
13670 P : Entity_Id;
13671 Hidden : Elmt_Id;
13672
13673 begin
13674 -- After child instantiation is complete, remove from scope stack the
13675 -- extra copy of the current scope, and then remove parent instances.
13676
13677 if not In_Body then
13678 Pop_Scope;
13679
13680 while Current_Scope /= S loop
13681 P := Current_Scope;
13682 End_Package_Scope (Current_Scope);
13683
13684 if In_Open_Scopes (P) then
13685 E := First_Entity (P);
13686 while Present (E) loop
13687 Set_Is_Immediately_Visible (E, True);
13688 Next_Entity (E);
13689 end loop;
13690
13691 -- If instantiation is declared in a block, it is the enclosing
13692 -- scope that might be a parent instance. Note that only one
13693 -- block can be involved, because the parent instances have
13694 -- been installed within it.
13695
13696 if Ekind (P) = E_Block then
13697 Cur_P := Scope (P);
13698 else
13699 Cur_P := P;
13700 end if;
13701
13702 if Is_Generic_Instance (Cur_P) and then P /= Current_Scope then
13703 -- We are within an instance of some sibling. Retain
13704 -- visibility of parent, for proper subsequent cleanup, and
13705 -- reinstall private declarations as well.
13706
13707 Set_In_Private_Part (P);
13708 Install_Private_Declarations (P);
13709 end if;
13710
13711 -- If the ultimate parent is a top-level unit recorded in
13712 -- Instance_Parent_Unit, then reset its visibility to what it was
13713 -- before instantiation. (It's not clear what the purpose is of
13714 -- testing whether Scope (P) is In_Open_Scopes, but that test was
13715 -- present before the ultimate parent test was added.???)
13716
13717 elsif not In_Open_Scopes (Scope (P))
13718 or else (P = Instance_Parent_Unit
13719 and then not Parent_Unit_Visible)
13720 then
13721 Set_Is_Immediately_Visible (P, False);
13722
13723 -- If the current scope is itself an instantiation of a generic
13724 -- nested within P, and we are in the private part of body of this
13725 -- instantiation, restore the full views of P, that were removed
13726 -- in End_Package_Scope above. This obscure case can occur when a
13727 -- subunit of a generic contains an instance of a child unit of
13728 -- its generic parent unit.
13729
13730 elsif S = Current_Scope and then Is_Generic_Instance (S) then
13731 declare
13732 Par : constant Entity_Id :=
13733 Generic_Parent (Package_Specification (S));
13734 begin
13735 if Present (Par)
13736 and then P = Scope (Par)
13737 and then (In_Package_Body (S) or else In_Private_Part (S))
13738 then
13739 Set_In_Private_Part (P);
13740 Install_Private_Declarations (P);
13741 end if;
13742 end;
13743 end if;
13744 end loop;
13745
13746 -- Reset visibility of entities in the enclosing scope
13747
13748 Set_Is_Hidden_Open_Scope (Current_Scope, False);
13749
13750 Hidden := First_Elmt (Hidden_Entities);
13751 while Present (Hidden) loop
13752 Set_Is_Immediately_Visible (Node (Hidden), True);
13753 Next_Elmt (Hidden);
13754 end loop;
13755
13756 else
13757 -- Each body is analyzed separately, and there is no context that
13758 -- needs preserving from one body instance to the next, so remove all
13759 -- parent scopes that have been installed.
13760
13761 while Present (S) loop
13762 End_Package_Scope (S);
13763 Set_Is_Immediately_Visible (S, False);
13764 S := Current_Scope;
13765 exit when S = Standard_Standard;
13766 end loop;
13767 end if;
13768 end Remove_Parent;
13769
13770 -----------------
13771 -- Restore_Env --
13772 -----------------
13773
13774 procedure Restore_Env is
13775 Saved : Instance_Env renames Instance_Envs.Table (Instance_Envs.Last);
13776
13777 begin
13778 if No (Current_Instantiated_Parent.Act_Id) then
13779 -- Restore environment after subprogram inlining
13780
13781 Restore_Private_Views (Empty);
13782 end if;
13783
13784 Current_Instantiated_Parent := Saved.Instantiated_Parent;
13785 Exchanged_Views := Saved.Exchanged_Views;
13786 Hidden_Entities := Saved.Hidden_Entities;
13787 Current_Sem_Unit := Saved.Current_Sem_Unit;
13788 Parent_Unit_Visible := Saved.Parent_Unit_Visible;
13789 Instance_Parent_Unit := Saved.Instance_Parent_Unit;
13790
13791 Restore_Opt_Config_Switches (Saved.Switches);
13792
13793 Instance_Envs.Decrement_Last;
13794 end Restore_Env;
13795
13796 ---------------------------
13797 -- Restore_Private_Views --
13798 ---------------------------
13799
13800 procedure Restore_Private_Views
13801 (Pack_Id : Entity_Id;
13802 Is_Package : Boolean := True)
13803 is
13804 M : Elmt_Id;
13805 E : Entity_Id;
13806 Typ : Entity_Id;
13807 Dep_Elmt : Elmt_Id;
13808 Dep_Typ : Node_Id;
13809
13810 procedure Restore_Nested_Formal (Formal : Entity_Id);
13811 -- Hide the generic formals of formal packages declared with box which
13812 -- were reachable in the current instantiation.
13813
13814 ---------------------------
13815 -- Restore_Nested_Formal --
13816 ---------------------------
13817
13818 procedure Restore_Nested_Formal (Formal : Entity_Id) is
13819 Ent : Entity_Id;
13820
13821 begin
13822 if Present (Renamed_Object (Formal))
13823 and then Denotes_Formal_Package (Renamed_Object (Formal), True)
13824 then
13825 return;
13826
13827 elsif Present (Associated_Formal_Package (Formal)) then
13828 Ent := First_Entity (Formal);
13829 while Present (Ent) loop
13830 exit when Ekind (Ent) = E_Package
13831 and then Renamed_Entity (Ent) = Renamed_Entity (Formal);
13832
13833 Set_Is_Hidden (Ent);
13834 Set_Is_Potentially_Use_Visible (Ent, False);
13835
13836 -- If package, then recurse
13837
13838 if Ekind (Ent) = E_Package then
13839 Restore_Nested_Formal (Ent);
13840 end if;
13841
13842 Next_Entity (Ent);
13843 end loop;
13844 end if;
13845 end Restore_Nested_Formal;
13846
13847 -- Start of processing for Restore_Private_Views
13848
13849 begin
13850 M := First_Elmt (Exchanged_Views);
13851 while Present (M) loop
13852 Typ := Node (M);
13853
13854 -- Subtypes of types whose views have been exchanged, and that are
13855 -- defined within the instance, were not on the Private_Dependents
13856 -- list on entry to the instance, so they have to be exchanged
13857 -- explicitly now, in order to remain consistent with the view of the
13858 -- parent type.
13859
13860 if Ekind_In (Typ, E_Private_Type,
13861 E_Limited_Private_Type,
13862 E_Record_Type_With_Private)
13863 then
13864 Dep_Elmt := First_Elmt (Private_Dependents (Typ));
13865 while Present (Dep_Elmt) loop
13866 Dep_Typ := Node (Dep_Elmt);
13867
13868 if Scope (Dep_Typ) = Pack_Id
13869 and then Present (Full_View (Dep_Typ))
13870 then
13871 Replace_Elmt (Dep_Elmt, Full_View (Dep_Typ));
13872 Exchange_Declarations (Dep_Typ);
13873 end if;
13874
13875 Next_Elmt (Dep_Elmt);
13876 end loop;
13877 end if;
13878
13879 Exchange_Declarations (Node (M));
13880 Next_Elmt (M);
13881 end loop;
13882
13883 if No (Pack_Id) then
13884 return;
13885 end if;
13886
13887 -- Make the generic formal parameters private, and make the formal types
13888 -- into subtypes of the actuals again.
13889
13890 E := First_Entity (Pack_Id);
13891 while Present (E) loop
13892 Set_Is_Hidden (E, True);
13893
13894 if Is_Type (E)
13895 and then Nkind (Parent (E)) = N_Subtype_Declaration
13896 then
13897 -- If the actual for E is itself a generic actual type from
13898 -- an enclosing instance, E is still a generic actual type
13899 -- outside of the current instance. This matter when resolving
13900 -- an overloaded call that may be ambiguous in the enclosing
13901 -- instance, when two of its actuals coincide.
13902
13903 if Is_Entity_Name (Subtype_Indication (Parent (E)))
13904 and then Is_Generic_Actual_Type
13905 (Entity (Subtype_Indication (Parent (E))))
13906 then
13907 null;
13908 else
13909 Set_Is_Generic_Actual_Type (E, False);
13910 end if;
13911
13912 -- An unusual case of aliasing: the actual may also be directly
13913 -- visible in the generic, and be private there, while it is fully
13914 -- visible in the context of the instance. The internal subtype
13915 -- is private in the instance but has full visibility like its
13916 -- parent in the enclosing scope. This enforces the invariant that
13917 -- the privacy status of all private dependents of a type coincide
13918 -- with that of the parent type. This can only happen when a
13919 -- generic child unit is instantiated within a sibling.
13920
13921 if Is_Private_Type (E)
13922 and then not Is_Private_Type (Etype (E))
13923 then
13924 Exchange_Declarations (E);
13925 end if;
13926
13927 elsif Ekind (E) = E_Package then
13928
13929 -- The end of the renaming list is the renaming of the generic
13930 -- package itself. If the instance is a subprogram, all entities
13931 -- in the corresponding package are renamings. If this entity is
13932 -- a formal package, make its own formals private as well. The
13933 -- actual in this case is itself the renaming of an instantiation.
13934 -- If the entity is not a package renaming, it is the entity
13935 -- created to validate formal package actuals: ignore it.
13936
13937 -- If the actual is itself a formal package for the enclosing
13938 -- generic, or the actual for such a formal package, it remains
13939 -- visible on exit from the instance, and therefore nothing needs
13940 -- to be done either, except to keep it accessible.
13941
13942 if Is_Package and then Renamed_Object (E) = Pack_Id then
13943 exit;
13944
13945 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
13946 null;
13947
13948 elsif
13949 Denotes_Formal_Package (Renamed_Object (E), True, Pack_Id)
13950 then
13951 Set_Is_Hidden (E, False);
13952
13953 else
13954 declare
13955 Act_P : constant Entity_Id := Renamed_Object (E);
13956 Id : Entity_Id;
13957
13958 begin
13959 Id := First_Entity (Act_P);
13960 while Present (Id)
13961 and then Id /= First_Private_Entity (Act_P)
13962 loop
13963 exit when Ekind (Id) = E_Package
13964 and then Renamed_Object (Id) = Act_P;
13965
13966 Set_Is_Hidden (Id, True);
13967 Set_Is_Potentially_Use_Visible (Id, In_Use (Act_P));
13968
13969 if Ekind (Id) = E_Package then
13970 Restore_Nested_Formal (Id);
13971 end if;
13972
13973 Next_Entity (Id);
13974 end loop;
13975 end;
13976 end if;
13977 end if;
13978
13979 Next_Entity (E);
13980 end loop;
13981 end Restore_Private_Views;
13982
13983 --------------
13984 -- Save_Env --
13985 --------------
13986
13987 procedure Save_Env
13988 (Gen_Unit : Entity_Id;
13989 Act_Unit : Entity_Id)
13990 is
13991 begin
13992 Init_Env;
13993 Set_Instance_Env (Gen_Unit, Act_Unit);
13994 end Save_Env;
13995
13996 ----------------------------
13997 -- Save_Global_References --
13998 ----------------------------
13999
14000 procedure Save_Global_References (Templ : Node_Id) is
14001
14002 -- ??? it is horrible to use global variables in highly recursive code
14003
14004 E : Entity_Id;
14005 -- The entity of the current associated node
14006
14007 Gen_Scope : Entity_Id;
14008 -- The scope of the generic for which references are being saved
14009
14010 N2 : Node_Id;
14011 -- The current associated node
14012
14013 function Is_Global (E : Entity_Id) return Boolean;
14014 -- Check whether entity is defined outside of generic unit. Examine the
14015 -- scope of an entity, and the scope of the scope, etc, until we find
14016 -- either Standard, in which case the entity is global, or the generic
14017 -- unit itself, which indicates that the entity is local. If the entity
14018 -- is the generic unit itself, as in the case of a recursive call, or
14019 -- the enclosing generic unit, if different from the current scope, then
14020 -- it is local as well, because it will be replaced at the point of
14021 -- instantiation. On the other hand, if it is a reference to a child
14022 -- unit of a common ancestor, which appears in an instantiation, it is
14023 -- global because it is used to denote a specific compilation unit at
14024 -- the time the instantiations will be analyzed.
14025
14026 procedure Qualify_Universal_Operands
14027 (Op : Node_Id;
14028 Func_Call : Node_Id);
14029 -- Op denotes a binary or unary operator in generic template Templ. Node
14030 -- Func_Call is the function call alternative of the operator within the
14031 -- the analyzed copy of the template. Change each operand which yields a
14032 -- universal type by wrapping it into a qualified expression
14033 --
14034 -- Actual_Typ'(Operand)
14035 --
14036 -- where Actual_Typ is the type of corresponding actual parameter of
14037 -- Operand in Func_Call.
14038
14039 procedure Reset_Entity (N : Node_Id);
14040 -- Save semantic information on global entity so that it is not resolved
14041 -- again at instantiation time.
14042
14043 procedure Save_Entity_Descendants (N : Node_Id);
14044 -- Apply Save_Global_References to the two syntactic descendants of
14045 -- non-terminal nodes that carry an Associated_Node and are processed
14046 -- through Reset_Entity. Once the global entity (if any) has been
14047 -- captured together with its type, only two syntactic descendants need
14048 -- to be traversed to complete the processing of the tree rooted at N.
14049 -- This applies to Selected_Components, Expanded_Names, and to Operator
14050 -- nodes. N can also be a character literal, identifier, or operator
14051 -- symbol node, but the call has no effect in these cases.
14052
14053 procedure Save_Global_Defaults (N1 : Node_Id; N2 : Node_Id);
14054 -- Default actuals in nested instances must be handled specially
14055 -- because there is no link to them from the original tree. When an
14056 -- actual subprogram is given by a default, we add an explicit generic
14057 -- association for it in the instantiation node. When we save the
14058 -- global references on the name of the instance, we recover the list
14059 -- of generic associations, and add an explicit one to the original
14060 -- generic tree, through which a global actual can be preserved.
14061 -- Similarly, if a child unit is instantiated within a sibling, in the
14062 -- context of the parent, we must preserve the identifier of the parent
14063 -- so that it can be properly resolved in a subsequent instantiation.
14064
14065 procedure Save_Global_Descendant (D : Union_Id);
14066 -- Apply Save_References recursively to the descendants of node D
14067
14068 procedure Save_References (N : Node_Id);
14069 -- This is the recursive procedure that does the work, once the
14070 -- enclosing generic scope has been established.
14071
14072 ---------------
14073 -- Is_Global --
14074 ---------------
14075
14076 function Is_Global (E : Entity_Id) return Boolean is
14077 Se : Entity_Id;
14078
14079 function Is_Instance_Node (Decl : Node_Id) return Boolean;
14080 -- Determine whether the parent node of a reference to a child unit
14081 -- denotes an instantiation or a formal package, in which case the
14082 -- reference to the child unit is global, even if it appears within
14083 -- the current scope (e.g. when the instance appears within the body
14084 -- of an ancestor).
14085
14086 ----------------------
14087 -- Is_Instance_Node --
14088 ----------------------
14089
14090 function Is_Instance_Node (Decl : Node_Id) return Boolean is
14091 begin
14092 return Nkind (Decl) in N_Generic_Instantiation
14093 or else
14094 Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration;
14095 end Is_Instance_Node;
14096
14097 -- Start of processing for Is_Global
14098
14099 begin
14100 if E = Gen_Scope then
14101 return False;
14102
14103 elsif E = Standard_Standard then
14104 return True;
14105
14106 elsif Is_Child_Unit (E)
14107 and then (Is_Instance_Node (Parent (N2))
14108 or else (Nkind (Parent (N2)) = N_Expanded_Name
14109 and then N2 = Selector_Name (Parent (N2))
14110 and then
14111 Is_Instance_Node (Parent (Parent (N2)))))
14112 then
14113 return True;
14114
14115 else
14116 Se := Scope (E);
14117 while Se /= Gen_Scope loop
14118 if Se = Standard_Standard then
14119 return True;
14120 else
14121 Se := Scope (Se);
14122 end if;
14123 end loop;
14124
14125 return False;
14126 end if;
14127 end Is_Global;
14128
14129 --------------------------------
14130 -- Qualify_Universal_Operands --
14131 --------------------------------
14132
14133 procedure Qualify_Universal_Operands
14134 (Op : Node_Id;
14135 Func_Call : Node_Id)
14136 is
14137 procedure Qualify_Operand (Opnd : Node_Id; Actual : Node_Id);
14138 -- Rewrite operand Opnd as a qualified expression of the form
14139 --
14140 -- Actual_Typ'(Opnd)
14141 --
14142 -- where Actual is the corresponding actual parameter of Opnd in
14143 -- function call Func_Call.
14144
14145 function Qualify_Type
14146 (Loc : Source_Ptr;
14147 Typ : Entity_Id) return Node_Id;
14148 -- Qualify type Typ by creating a selected component of the form
14149 --
14150 -- Scope_Of_Typ.Typ
14151
14152 ---------------------
14153 -- Qualify_Operand --
14154 ---------------------
14155
14156 procedure Qualify_Operand (Opnd : Node_Id; Actual : Node_Id) is
14157 Loc : constant Source_Ptr := Sloc (Opnd);
14158 Typ : constant Entity_Id := Etype (Actual);
14159 Mark : Node_Id;
14160 Qual : Node_Id;
14161
14162 begin
14163 -- Qualify the operand when it is of a universal type. Note that
14164 -- the template is unanalyzed and it is not possible to directly
14165 -- query the type. This transformation is not done when the type
14166 -- of the actual is internally generated because the type will be
14167 -- regenerated in the instance.
14168
14169 if Yields_Universal_Type (Opnd)
14170 and then Comes_From_Source (Typ)
14171 and then not Is_Hidden (Typ)
14172 then
14173 -- The type of the actual may be a global reference. Save this
14174 -- information by creating a reference to it.
14175
14176 if Is_Global (Typ) then
14177 Mark := New_Occurrence_Of (Typ, Loc);
14178
14179 -- Otherwise rely on resolution to find the proper type within
14180 -- the instance.
14181
14182 else
14183 Mark := Qualify_Type (Loc, Typ);
14184 end if;
14185
14186 Qual :=
14187 Make_Qualified_Expression (Loc,
14188 Subtype_Mark => Mark,
14189 Expression => Relocate_Node (Opnd));
14190
14191 -- Mark the qualification to distinguish it from other source
14192 -- constructs and signal the instantiation mechanism that this
14193 -- node requires special processing. See Copy_Generic_Node for
14194 -- details.
14195
14196 Set_Is_Qualified_Universal_Literal (Qual);
14197
14198 Rewrite (Opnd, Qual);
14199 end if;
14200 end Qualify_Operand;
14201
14202 ------------------
14203 -- Qualify_Type --
14204 ------------------
14205
14206 function Qualify_Type
14207 (Loc : Source_Ptr;
14208 Typ : Entity_Id) return Node_Id
14209 is
14210 Scop : constant Entity_Id := Scope (Typ);
14211 Result : Node_Id;
14212
14213 begin
14214 Result := Make_Identifier (Loc, Chars (Typ));
14215
14216 if Present (Scop) and then not Is_Generic_Unit (Scop) then
14217 Result :=
14218 Make_Selected_Component (Loc,
14219 Prefix => Make_Identifier (Loc, Chars (Scop)),
14220 Selector_Name => Result);
14221 end if;
14222
14223 return Result;
14224 end Qualify_Type;
14225
14226 -- Local variables
14227
14228 Actuals : constant List_Id := Parameter_Associations (Func_Call);
14229
14230 -- Start of processing for Qualify_Universal_Operands
14231
14232 begin
14233 if Nkind (Op) in N_Binary_Op then
14234 Qualify_Operand (Left_Opnd (Op), First (Actuals));
14235 Qualify_Operand (Right_Opnd (Op), Next (First (Actuals)));
14236
14237 elsif Nkind (Op) in N_Unary_Op then
14238 Qualify_Operand (Right_Opnd (Op), First (Actuals));
14239 end if;
14240 end Qualify_Universal_Operands;
14241
14242 ------------------
14243 -- Reset_Entity --
14244 ------------------
14245
14246 procedure Reset_Entity (N : Node_Id) is
14247 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id);
14248 -- If the type of N2 is global to the generic unit, save the type in
14249 -- the generic node. Just as we perform name capture for explicit
14250 -- references within the generic, we must capture the global types
14251 -- of local entities because they may participate in resolution in
14252 -- the instance.
14253
14254 function Top_Ancestor (E : Entity_Id) return Entity_Id;
14255 -- Find the ultimate ancestor of the current unit. If it is not a
14256 -- generic unit, then the name of the current unit in the prefix of
14257 -- an expanded name must be replaced with its generic homonym to
14258 -- ensure that it will be properly resolved in an instance.
14259
14260 ---------------------
14261 -- Set_Global_Type --
14262 ---------------------
14263
14264 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id) is
14265 Typ : constant Entity_Id := Etype (N2);
14266
14267 begin
14268 Set_Etype (N, Typ);
14269
14270 -- If the entity of N is not the associated node, this is a
14271 -- nested generic and it has an associated node as well, whose
14272 -- type is already the full view (see below). Indicate that the
14273 -- original node has a private view.
14274
14275 if Entity (N) /= N2 and then Has_Private_View (Entity (N)) then
14276 Set_Has_Private_View (N);
14277 end if;
14278
14279 -- If not a private type, nothing else to do
14280
14281 if not Is_Private_Type (Typ) then
14282 if Is_Array_Type (Typ)
14283 and then Is_Private_Type (Component_Type (Typ))
14284 then
14285 Set_Has_Private_View (N);
14286 end if;
14287
14288 -- If it is a derivation of a private type in a context where no
14289 -- full view is needed, nothing to do either.
14290
14291 elsif No (Full_View (Typ)) and then Typ /= Etype (Typ) then
14292 null;
14293
14294 -- Otherwise mark the type for flipping and use the full view when
14295 -- available.
14296
14297 else
14298 Set_Has_Private_View (N);
14299
14300 if Present (Full_View (Typ)) then
14301 Set_Etype (N2, Full_View (Typ));
14302 end if;
14303 end if;
14304
14305 if Is_Floating_Point_Type (Typ)
14306 and then Has_Dimension_System (Typ)
14307 then
14308 Copy_Dimensions (N2, N);
14309 end if;
14310 end Set_Global_Type;
14311
14312 ------------------
14313 -- Top_Ancestor --
14314 ------------------
14315
14316 function Top_Ancestor (E : Entity_Id) return Entity_Id is
14317 Par : Entity_Id;
14318
14319 begin
14320 Par := E;
14321 while Is_Child_Unit (Par) loop
14322 Par := Scope (Par);
14323 end loop;
14324
14325 return Par;
14326 end Top_Ancestor;
14327
14328 -- Start of processing for Reset_Entity
14329
14330 begin
14331 N2 := Get_Associated_Node (N);
14332 E := Entity (N2);
14333
14334 if Present (E) then
14335
14336 -- If the node is an entry call to an entry in an enclosing task,
14337 -- it is rewritten as a selected component. No global entity to
14338 -- preserve in this case, since the expansion will be redone in
14339 -- the instance.
14340
14341 if not Nkind_In (E, N_Defining_Character_Literal,
14342 N_Defining_Identifier,
14343 N_Defining_Operator_Symbol)
14344 then
14345 Set_Associated_Node (N, Empty);
14346 Set_Etype (N, Empty);
14347 return;
14348 end if;
14349
14350 -- If the entity is an itype created as a subtype of an access
14351 -- type with a null exclusion restore source entity for proper
14352 -- visibility. The itype will be created anew in the instance.
14353
14354 if Is_Itype (E)
14355 and then Ekind (E) = E_Access_Subtype
14356 and then Is_Entity_Name (N)
14357 and then Chars (Etype (E)) = Chars (N)
14358 then
14359 E := Etype (E);
14360 Set_Entity (N2, E);
14361 Set_Etype (N2, E);
14362 end if;
14363
14364 if Is_Global (E) then
14365
14366 -- If the entity is a package renaming that is the prefix of
14367 -- an expanded name, it has been rewritten as the renamed
14368 -- package, which is necessary semantically but complicates
14369 -- ASIS tree traversal, so we recover the original entity to
14370 -- expose the renaming. Take into account that the context may
14371 -- be a nested generic, that the original node may itself have
14372 -- an associated node that had better be an entity, and that
14373 -- the current node is still a selected component.
14374
14375 if Ekind (E) = E_Package
14376 and then Nkind (N) = N_Selected_Component
14377 and then Nkind (Parent (N)) = N_Expanded_Name
14378 and then Present (Original_Node (N2))
14379 and then Is_Entity_Name (Original_Node (N2))
14380 and then Present (Entity (Original_Node (N2)))
14381 then
14382 if Is_Global (Entity (Original_Node (N2))) then
14383 N2 := Original_Node (N2);
14384 Set_Associated_Node (N, N2);
14385 Set_Global_Type (N, N2);
14386
14387 -- Renaming is local, and will be resolved in instance
14388
14389 else
14390 Set_Associated_Node (N, Empty);
14391 Set_Etype (N, Empty);
14392 end if;
14393
14394 else
14395 Set_Global_Type (N, N2);
14396 end if;
14397
14398 elsif Nkind (N) = N_Op_Concat
14399 and then Is_Generic_Type (Etype (N2))
14400 and then (Base_Type (Etype (Right_Opnd (N2))) = Etype (N2)
14401 or else
14402 Base_Type (Etype (Left_Opnd (N2))) = Etype (N2))
14403 and then Is_Intrinsic_Subprogram (E)
14404 then
14405 null;
14406
14407 -- Entity is local. Mark generic node as unresolved. Note that now
14408 -- it does not have an entity.
14409
14410 else
14411 Set_Associated_Node (N, Empty);
14412 Set_Etype (N, Empty);
14413 end if;
14414
14415 if Nkind (Parent (N)) in N_Generic_Instantiation
14416 and then N = Name (Parent (N))
14417 then
14418 Save_Global_Defaults (Parent (N), Parent (N2));
14419 end if;
14420
14421 elsif Nkind (Parent (N)) = N_Selected_Component
14422 and then Nkind (Parent (N2)) = N_Expanded_Name
14423 then
14424 if Is_Global (Entity (Parent (N2))) then
14425 Change_Selected_Component_To_Expanded_Name (Parent (N));
14426 Set_Associated_Node (Parent (N), Parent (N2));
14427 Set_Global_Type (Parent (N), Parent (N2));
14428 Save_Entity_Descendants (N);
14429
14430 -- If this is a reference to the current generic entity, replace
14431 -- by the name of the generic homonym of the current package. This
14432 -- is because in an instantiation Par.P.Q will not resolve to the
14433 -- name of the instance, whose enclosing scope is not necessarily
14434 -- Par. We use the generic homonym rather that the name of the
14435 -- generic itself because it may be hidden by a local declaration.
14436
14437 elsif In_Open_Scopes (Entity (Parent (N2)))
14438 and then not
14439 Is_Generic_Unit (Top_Ancestor (Entity (Prefix (Parent (N2)))))
14440 then
14441 if Ekind (Entity (Parent (N2))) = E_Generic_Package then
14442 Rewrite (Parent (N),
14443 Make_Identifier (Sloc (N),
14444 Chars =>
14445 Chars (Generic_Homonym (Entity (Parent (N2))))));
14446 else
14447 Rewrite (Parent (N),
14448 Make_Identifier (Sloc (N),
14449 Chars => Chars (Selector_Name (Parent (N2)))));
14450 end if;
14451 end if;
14452
14453 if Nkind (Parent (Parent (N))) in N_Generic_Instantiation
14454 and then Parent (N) = Name (Parent (Parent (N)))
14455 then
14456 Save_Global_Defaults
14457 (Parent (Parent (N)), Parent (Parent (N2)));
14458 end if;
14459
14460 -- A selected component may denote a static constant that has been
14461 -- folded. If the static constant is global to the generic, capture
14462 -- its value. Otherwise the folding will happen in any instantiation.
14463
14464 elsif Nkind (Parent (N)) = N_Selected_Component
14465 and then Nkind_In (Parent (N2), N_Integer_Literal, N_Real_Literal)
14466 then
14467 if Present (Entity (Original_Node (Parent (N2))))
14468 and then Is_Global (Entity (Original_Node (Parent (N2))))
14469 then
14470 Rewrite (Parent (N), New_Copy (Parent (N2)));
14471 Set_Analyzed (Parent (N), False);
14472 end if;
14473
14474 -- A selected component may be transformed into a parameterless
14475 -- function call. If the called entity is global, rewrite the node
14476 -- appropriately, i.e. as an extended name for the global entity.
14477
14478 elsif Nkind (Parent (N)) = N_Selected_Component
14479 and then Nkind (Parent (N2)) = N_Function_Call
14480 and then N = Selector_Name (Parent (N))
14481 then
14482 if No (Parameter_Associations (Parent (N2))) then
14483 if Is_Global (Entity (Name (Parent (N2)))) then
14484 Change_Selected_Component_To_Expanded_Name (Parent (N));
14485 Set_Associated_Node (Parent (N), Name (Parent (N2)));
14486 Set_Global_Type (Parent (N), Name (Parent (N2)));
14487 Save_Entity_Descendants (N);
14488
14489 else
14490 Set_Is_Prefixed_Call (Parent (N));
14491 Set_Associated_Node (N, Empty);
14492 Set_Etype (N, Empty);
14493 end if;
14494
14495 -- In Ada 2005, X.F may be a call to a primitive operation,
14496 -- rewritten as F (X). This rewriting will be done again in an
14497 -- instance, so keep the original node. Global entities will be
14498 -- captured as for other constructs. Indicate that this must
14499 -- resolve as a call, to prevent accidental overloading in the
14500 -- instance, if both a component and a primitive operation appear
14501 -- as candidates.
14502
14503 else
14504 Set_Is_Prefixed_Call (Parent (N));
14505 end if;
14506
14507 -- Entity is local. Reset in generic unit, so that node is resolved
14508 -- anew at the point of instantiation.
14509
14510 else
14511 Set_Associated_Node (N, Empty);
14512 Set_Etype (N, Empty);
14513 end if;
14514 end Reset_Entity;
14515
14516 -----------------------------
14517 -- Save_Entity_Descendants --
14518 -----------------------------
14519
14520 procedure Save_Entity_Descendants (N : Node_Id) is
14521 begin
14522 case Nkind (N) is
14523 when N_Binary_Op =>
14524 Save_Global_Descendant (Union_Id (Left_Opnd (N)));
14525 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
14526
14527 when N_Unary_Op =>
14528 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
14529
14530 when N_Expanded_Name
14531 | N_Selected_Component
14532 =>
14533 Save_Global_Descendant (Union_Id (Prefix (N)));
14534 Save_Global_Descendant (Union_Id (Selector_Name (N)));
14535
14536 when N_Character_Literal
14537 | N_Identifier
14538 | N_Operator_Symbol
14539 =>
14540 null;
14541
14542 when others =>
14543 raise Program_Error;
14544 end case;
14545 end Save_Entity_Descendants;
14546
14547 --------------------------
14548 -- Save_Global_Defaults --
14549 --------------------------
14550
14551 procedure Save_Global_Defaults (N1 : Node_Id; N2 : Node_Id) is
14552 Loc : constant Source_Ptr := Sloc (N1);
14553 Assoc2 : constant List_Id := Generic_Associations (N2);
14554 Gen_Id : constant Entity_Id := Get_Generic_Entity (N2);
14555 Assoc1 : List_Id;
14556 Act1 : Node_Id;
14557 Act2 : Node_Id;
14558 Def : Node_Id;
14559 Ndec : Node_Id;
14560 Subp : Entity_Id;
14561 Actual : Entity_Id;
14562
14563 begin
14564 Assoc1 := Generic_Associations (N1);
14565
14566 if Present (Assoc1) then
14567 Act1 := First (Assoc1);
14568 else
14569 Act1 := Empty;
14570 Set_Generic_Associations (N1, New_List);
14571 Assoc1 := Generic_Associations (N1);
14572 end if;
14573
14574 if Present (Assoc2) then
14575 Act2 := First (Assoc2);
14576 else
14577 return;
14578 end if;
14579
14580 while Present (Act1) and then Present (Act2) loop
14581 Next (Act1);
14582 Next (Act2);
14583 end loop;
14584
14585 -- Find the associations added for default subprograms
14586
14587 if Present (Act2) then
14588 while Nkind (Act2) /= N_Generic_Association
14589 or else No (Entity (Selector_Name (Act2)))
14590 or else not Is_Overloadable (Entity (Selector_Name (Act2)))
14591 loop
14592 Next (Act2);
14593 end loop;
14594
14595 -- Add a similar association if the default is global. The
14596 -- renaming declaration for the actual has been analyzed, and
14597 -- its alias is the program it renames. Link the actual in the
14598 -- original generic tree with the node in the analyzed tree.
14599
14600 while Present (Act2) loop
14601 Subp := Entity (Selector_Name (Act2));
14602 Def := Explicit_Generic_Actual_Parameter (Act2);
14603
14604 -- Following test is defence against rubbish errors
14605
14606 if No (Alias (Subp)) then
14607 return;
14608 end if;
14609
14610 -- Retrieve the resolved actual from the renaming declaration
14611 -- created for the instantiated formal.
14612
14613 Actual := Entity (Name (Parent (Parent (Subp))));
14614 Set_Entity (Def, Actual);
14615 Set_Etype (Def, Etype (Actual));
14616
14617 if Is_Global (Actual) then
14618 Ndec :=
14619 Make_Generic_Association (Loc,
14620 Selector_Name =>
14621 New_Occurrence_Of (Subp, Loc),
14622 Explicit_Generic_Actual_Parameter =>
14623 New_Occurrence_Of (Actual, Loc));
14624
14625 Set_Associated_Node
14626 (Explicit_Generic_Actual_Parameter (Ndec), Def);
14627
14628 Append (Ndec, Assoc1);
14629
14630 -- If there are other defaults, add a dummy association in case
14631 -- there are other defaulted formals with the same name.
14632
14633 elsif Present (Next (Act2)) then
14634 Ndec :=
14635 Make_Generic_Association (Loc,
14636 Selector_Name =>
14637 New_Occurrence_Of (Subp, Loc),
14638 Explicit_Generic_Actual_Parameter => Empty);
14639
14640 Append (Ndec, Assoc1);
14641 end if;
14642
14643 Next (Act2);
14644 end loop;
14645 end if;
14646
14647 if Nkind (Name (N1)) = N_Identifier
14648 and then Is_Child_Unit (Gen_Id)
14649 and then Is_Global (Gen_Id)
14650 and then Is_Generic_Unit (Scope (Gen_Id))
14651 and then In_Open_Scopes (Scope (Gen_Id))
14652 then
14653 -- This is an instantiation of a child unit within a sibling, so
14654 -- that the generic parent is in scope. An eventual instance must
14655 -- occur within the scope of an instance of the parent. Make name
14656 -- in instance into an expanded name, to preserve the identifier
14657 -- of the parent, so it can be resolved subsequently.
14658
14659 Rewrite (Name (N2),
14660 Make_Expanded_Name (Loc,
14661 Chars => Chars (Gen_Id),
14662 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
14663 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
14664 Set_Entity (Name (N2), Gen_Id);
14665
14666 Rewrite (Name (N1),
14667 Make_Expanded_Name (Loc,
14668 Chars => Chars (Gen_Id),
14669 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
14670 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
14671
14672 Set_Associated_Node (Name (N1), Name (N2));
14673 Set_Associated_Node (Prefix (Name (N1)), Empty);
14674 Set_Associated_Node
14675 (Selector_Name (Name (N1)), Selector_Name (Name (N2)));
14676 Set_Etype (Name (N1), Etype (Gen_Id));
14677 end if;
14678 end Save_Global_Defaults;
14679
14680 ----------------------------
14681 -- Save_Global_Descendant --
14682 ----------------------------
14683
14684 procedure Save_Global_Descendant (D : Union_Id) is
14685 N1 : Node_Id;
14686
14687 begin
14688 if D in Node_Range then
14689 if D = Union_Id (Empty) then
14690 null;
14691
14692 elsif Nkind (Node_Id (D)) /= N_Compilation_Unit then
14693 Save_References (Node_Id (D));
14694 end if;
14695
14696 elsif D in List_Range then
14697 pragma Assert (D /= Union_Id (No_List));
14698 -- Because No_List = Empty, which is in Node_Range above
14699
14700 if Is_Empty_List (List_Id (D)) then
14701 null;
14702
14703 else
14704 N1 := First (List_Id (D));
14705 while Present (N1) loop
14706 Save_References (N1);
14707 Next (N1);
14708 end loop;
14709 end if;
14710
14711 -- Element list or other non-node field, nothing to do
14712
14713 else
14714 null;
14715 end if;
14716 end Save_Global_Descendant;
14717
14718 ---------------------
14719 -- Save_References --
14720 ---------------------
14721
14722 -- This is the recursive procedure that does the work once the enclosing
14723 -- generic scope has been established. We have to treat specially a
14724 -- number of node rewritings that are required by semantic processing
14725 -- and which change the kind of nodes in the generic copy: typically
14726 -- constant-folding, replacing an operator node by a string literal, or
14727 -- a selected component by an expanded name. In each of those cases, the
14728 -- transformation is propagated to the generic unit.
14729
14730 procedure Save_References (N : Node_Id) is
14731 Loc : constant Source_Ptr := Sloc (N);
14732
14733 function Requires_Delayed_Save (Nod : Node_Id) return Boolean;
14734 -- Determine whether arbitrary node Nod requires delayed capture of
14735 -- global references within its aspect specifications.
14736
14737 procedure Save_References_In_Aggregate (N : Node_Id);
14738 -- Save all global references in [extension] aggregate node N
14739
14740 procedure Save_References_In_Char_Lit_Or_Op_Symbol (N : Node_Id);
14741 -- Save all global references in a character literal or operator
14742 -- symbol denoted by N.
14743
14744 procedure Save_References_In_Descendants (N : Node_Id);
14745 -- Save all global references in all descendants of node N
14746
14747 procedure Save_References_In_Identifier (N : Node_Id);
14748 -- Save all global references in identifier node N
14749
14750 procedure Save_References_In_Operator (N : Node_Id);
14751 -- Save all global references in operator node N
14752
14753 procedure Save_References_In_Pragma (Prag : Node_Id);
14754 -- Save all global references found within the expression of pragma
14755 -- Prag.
14756
14757 ---------------------------
14758 -- Requires_Delayed_Save --
14759 ---------------------------
14760
14761 function Requires_Delayed_Save (Nod : Node_Id) return Boolean is
14762 begin
14763 -- Generic packages and subprograms require delayed capture of
14764 -- global references within their aspects due to the timing of
14765 -- annotation analysis.
14766
14767 if Nkind_In (Nod, N_Generic_Package_Declaration,
14768 N_Generic_Subprogram_Declaration,
14769 N_Package_Body,
14770 N_Package_Body_Stub,
14771 N_Subprogram_Body,
14772 N_Subprogram_Body_Stub)
14773 then
14774 -- Since the capture of global references is done on the
14775 -- unanalyzed generic template, there is no information around
14776 -- to infer the context. Use the Associated_Entity linkages to
14777 -- peek into the analyzed generic copy and determine what the
14778 -- template corresponds to.
14779
14780 if Nod = Templ then
14781 return
14782 Is_Generic_Declaration_Or_Body
14783 (Unit_Declaration_Node
14784 (Associated_Entity (Defining_Entity (Nod))));
14785
14786 -- Otherwise the generic unit being processed is not the top
14787 -- level template. It is safe to capture of global references
14788 -- within the generic unit because at this point the top level
14789 -- copy is fully analyzed.
14790
14791 else
14792 return False;
14793 end if;
14794
14795 -- Otherwise capture the global references without interference
14796
14797 else
14798 return False;
14799 end if;
14800 end Requires_Delayed_Save;
14801
14802 ----------------------------------
14803 -- Save_References_In_Aggregate --
14804 ----------------------------------
14805
14806 procedure Save_References_In_Aggregate (N : Node_Id) is
14807 Nam : Node_Id;
14808 Qual : Node_Id := Empty;
14809 Typ : Entity_Id := Empty;
14810
14811 use Atree.Unchecked_Access;
14812 -- This code section is part of implementing an untyped tree
14813 -- traversal, so it needs direct access to node fields.
14814
14815 begin
14816 N2 := Get_Associated_Node (N);
14817
14818 if Present (N2) then
14819 Typ := Etype (N2);
14820
14821 -- In an instance within a generic, use the name of the actual
14822 -- and not the original generic parameter. If the actual is
14823 -- global in the current generic it must be preserved for its
14824 -- instantiation.
14825
14826 if Nkind (Parent (Typ)) = N_Subtype_Declaration
14827 and then Present (Generic_Parent_Type (Parent (Typ)))
14828 then
14829 Typ := Base_Type (Typ);
14830 Set_Etype (N2, Typ);
14831 end if;
14832 end if;
14833
14834 if No (N2) or else No (Typ) or else not Is_Global (Typ) then
14835 Set_Associated_Node (N, Empty);
14836
14837 -- If the aggregate is an actual in a call, it has been
14838 -- resolved in the current context, to some local type. The
14839 -- enclosing call may have been disambiguated by the aggregate,
14840 -- and this disambiguation might fail at instantiation time
14841 -- because the type to which the aggregate did resolve is not
14842 -- preserved. In order to preserve some of this information,
14843 -- wrap the aggregate in a qualified expression, using the id
14844 -- of its type. For further disambiguation we qualify the type
14845 -- name with its scope (if visible) because both id's will have
14846 -- corresponding entities in an instance. This resolves most of
14847 -- the problems with missing type information on aggregates in
14848 -- instances.
14849
14850 if Present (N2)
14851 and then Nkind (N2) = Nkind (N)
14852 and then Nkind (Parent (N2)) in N_Subprogram_Call
14853 and then Present (Typ)
14854 and then Comes_From_Source (Typ)
14855 then
14856 Nam := Make_Identifier (Loc, Chars (Typ));
14857
14858 if Is_Immediately_Visible (Scope (Typ)) then
14859 Nam :=
14860 Make_Selected_Component (Loc,
14861 Prefix =>
14862 Make_Identifier (Loc, Chars (Scope (Typ))),
14863 Selector_Name => Nam);
14864 end if;
14865
14866 Qual :=
14867 Make_Qualified_Expression (Loc,
14868 Subtype_Mark => Nam,
14869 Expression => Relocate_Node (N));
14870 end if;
14871 end if;
14872
14873 Save_Global_Descendant (Field1 (N));
14874 Save_Global_Descendant (Field2 (N));
14875 Save_Global_Descendant (Field3 (N));
14876 Save_Global_Descendant (Field5 (N));
14877
14878 if Present (Qual) then
14879 Rewrite (N, Qual);
14880 end if;
14881 end Save_References_In_Aggregate;
14882
14883 ----------------------------------------------
14884 -- Save_References_In_Char_Lit_Or_Op_Symbol --
14885 ----------------------------------------------
14886
14887 procedure Save_References_In_Char_Lit_Or_Op_Symbol (N : Node_Id) is
14888 begin
14889 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
14890 Reset_Entity (N);
14891
14892 elsif Nkind (N) = N_Operator_Symbol
14893 and then Nkind (Get_Associated_Node (N)) = N_String_Literal
14894 then
14895 Change_Operator_Symbol_To_String_Literal (N);
14896 end if;
14897 end Save_References_In_Char_Lit_Or_Op_Symbol;
14898
14899 ------------------------------------
14900 -- Save_References_In_Descendants --
14901 ------------------------------------
14902
14903 procedure Save_References_In_Descendants (N : Node_Id) is
14904 use Atree.Unchecked_Access;
14905 -- This code section is part of implementing an untyped tree
14906 -- traversal, so it needs direct access to node fields.
14907
14908 begin
14909 Save_Global_Descendant (Field1 (N));
14910 Save_Global_Descendant (Field2 (N));
14911 Save_Global_Descendant (Field3 (N));
14912 Save_Global_Descendant (Field4 (N));
14913 Save_Global_Descendant (Field5 (N));
14914 end Save_References_In_Descendants;
14915
14916 -----------------------------------
14917 -- Save_References_In_Identifier --
14918 -----------------------------------
14919
14920 procedure Save_References_In_Identifier (N : Node_Id) is
14921 begin
14922 -- The node did not undergo a transformation
14923
14924 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
14925 declare
14926 Aux_N2 : constant Node_Id := Get_Associated_Node (N);
14927 Orig_N2_Parent : constant Node_Id :=
14928 Original_Node (Parent (Aux_N2));
14929 begin
14930 -- The parent of this identifier is a selected component
14931 -- which denotes a named number that was constant folded.
14932 -- Preserve the original name for ASIS and link the parent
14933 -- with its expanded name. The constant folding will be
14934 -- repeated in the instance.
14935
14936 if Nkind (Parent (N)) = N_Selected_Component
14937 and then Nkind_In (Parent (Aux_N2), N_Integer_Literal,
14938 N_Real_Literal)
14939 and then Is_Entity_Name (Orig_N2_Parent)
14940 and then Ekind (Entity (Orig_N2_Parent)) in Named_Kind
14941 and then Is_Global (Entity (Orig_N2_Parent))
14942 then
14943 N2 := Aux_N2;
14944 Set_Associated_Node
14945 (Parent (N), Original_Node (Parent (N2)));
14946
14947 -- Common case
14948
14949 else
14950 -- If this is a discriminant reference, always save it.
14951 -- It is used in the instance to find the corresponding
14952 -- discriminant positionally rather than by name.
14953
14954 Set_Original_Discriminant
14955 (N, Original_Discriminant (Get_Associated_Node (N)));
14956 end if;
14957
14958 Reset_Entity (N);
14959 end;
14960
14961 -- The analysis of the generic copy transformed the identifier
14962 -- into another construct. Propagate the changes to the template.
14963
14964 else
14965 N2 := Get_Associated_Node (N);
14966
14967 -- The identifier denotes a call to a parameterless function.
14968 -- Mark the node as resolved when the function is external.
14969
14970 if Nkind (N2) = N_Function_Call then
14971 E := Entity (Name (N2));
14972
14973 if Present (E) and then Is_Global (E) then
14974 Set_Etype (N, Etype (N2));
14975 else
14976 Set_Associated_Node (N, Empty);
14977 Set_Etype (N, Empty);
14978 end if;
14979
14980 -- The identifier denotes a named number that was constant
14981 -- folded. Preserve the original name for ASIS and undo the
14982 -- constant folding which will be repeated in the instance.
14983
14984 elsif Nkind_In (N2, N_Integer_Literal, N_Real_Literal)
14985 and then Is_Entity_Name (Original_Node (N2))
14986 then
14987 Set_Associated_Node (N, Original_Node (N2));
14988 Reset_Entity (N);
14989
14990 -- The identifier resolved to a string literal. Propagate this
14991 -- information to the generic template.
14992
14993 elsif Nkind (N2) = N_String_Literal then
14994 Rewrite (N, New_Copy (N2));
14995
14996 -- The identifier is rewritten as a dereference if it is the
14997 -- prefix of an implicit dereference. Preserve the original
14998 -- tree as the analysis of the instance will expand the node
14999 -- again, but preserve the resolved entity if it is global.
15000
15001 elsif Nkind (N2) = N_Explicit_Dereference then
15002 if Is_Entity_Name (Prefix (N2))
15003 and then Present (Entity (Prefix (N2)))
15004 and then Is_Global (Entity (Prefix (N2)))
15005 then
15006 Set_Associated_Node (N, Prefix (N2));
15007
15008 elsif Nkind (Prefix (N2)) = N_Function_Call
15009 and then Present (Entity (Name (Prefix (N2))))
15010 and then Is_Global (Entity (Name (Prefix (N2))))
15011 then
15012 Rewrite (N,
15013 Make_Explicit_Dereference (Loc,
15014 Prefix =>
15015 Make_Function_Call (Loc,
15016 Name =>
15017 New_Occurrence_Of
15018 (Entity (Name (Prefix (N2))), Loc))));
15019
15020 else
15021 Set_Associated_Node (N, Empty);
15022 Set_Etype (N, Empty);
15023 end if;
15024
15025 -- The subtype mark of a nominally unconstrained object is
15026 -- rewritten as a subtype indication using the bounds of the
15027 -- expression. Recover the original subtype mark.
15028
15029 elsif Nkind (N2) = N_Subtype_Indication
15030 and then Is_Entity_Name (Original_Node (N2))
15031 then
15032 Set_Associated_Node (N, Original_Node (N2));
15033 Reset_Entity (N);
15034 end if;
15035 end if;
15036 end Save_References_In_Identifier;
15037
15038 ---------------------------------
15039 -- Save_References_In_Operator --
15040 ---------------------------------
15041
15042 procedure Save_References_In_Operator (N : Node_Id) is
15043 begin
15044 -- The node did not undergo a transformation
15045
15046 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
15047 if Nkind (N) = N_Op_Concat then
15048 Set_Is_Component_Left_Opnd (N,
15049 Is_Component_Left_Opnd (Get_Associated_Node (N)));
15050
15051 Set_Is_Component_Right_Opnd (N,
15052 Is_Component_Right_Opnd (Get_Associated_Node (N)));
15053 end if;
15054
15055 Reset_Entity (N);
15056
15057 -- The analysis of the generic copy transformed the operator into
15058 -- some other construct. Propagate the changes to the template if
15059 -- applicable.
15060
15061 else
15062 N2 := Get_Associated_Node (N);
15063
15064 -- The operator resoved to a function call
15065
15066 if Nkind (N2) = N_Function_Call then
15067
15068 -- Add explicit qualifications in the generic template for
15069 -- all operands of universal type. This aids resolution by
15070 -- preserving the actual type of a literal or an attribute
15071 -- that yields a universal result.
15072
15073 Qualify_Universal_Operands (N, N2);
15074
15075 E := Entity (Name (N2));
15076
15077 if Present (E) and then Is_Global (E) then
15078 Set_Etype (N, Etype (N2));
15079 else
15080 Set_Associated_Node (N, Empty);
15081 Set_Etype (N, Empty);
15082 end if;
15083
15084 -- The operator was folded into a literal
15085
15086 elsif Nkind_In (N2, N_Integer_Literal,
15087 N_Real_Literal,
15088 N_String_Literal)
15089 then
15090 if Present (Original_Node (N2))
15091 and then Nkind (Original_Node (N2)) = Nkind (N)
15092 then
15093 -- Operation was constant-folded. Whenever possible,
15094 -- recover semantic information from unfolded node,
15095 -- for ASIS use.
15096
15097 Set_Associated_Node (N, Original_Node (N2));
15098
15099 if Nkind (N) = N_Op_Concat then
15100 Set_Is_Component_Left_Opnd (N,
15101 Is_Component_Left_Opnd (Get_Associated_Node (N)));
15102 Set_Is_Component_Right_Opnd (N,
15103 Is_Component_Right_Opnd (Get_Associated_Node (N)));
15104 end if;
15105
15106 Reset_Entity (N);
15107
15108 -- Propagate the constant folding back to the template
15109
15110 else
15111 Rewrite (N, New_Copy (N2));
15112 Set_Analyzed (N, False);
15113 end if;
15114
15115 -- The operator was folded into an enumeration literal. Retain
15116 -- the entity to avoid spurious ambiguities if it is overloaded
15117 -- at the point of instantiation or inlining.
15118
15119 elsif Nkind (N2) = N_Identifier
15120 and then Ekind (Entity (N2)) = E_Enumeration_Literal
15121 then
15122 Rewrite (N, New_Copy (N2));
15123 Set_Analyzed (N, False);
15124 end if;
15125 end if;
15126
15127 -- Complete the operands check if node has not been constant
15128 -- folded.
15129
15130 if Nkind (N) in N_Op then
15131 Save_Entity_Descendants (N);
15132 end if;
15133 end Save_References_In_Operator;
15134
15135 -------------------------------
15136 -- Save_References_In_Pragma --
15137 -------------------------------
15138
15139 procedure Save_References_In_Pragma (Prag : Node_Id) is
15140 Context : Node_Id;
15141 Do_Save : Boolean := True;
15142
15143 use Atree.Unchecked_Access;
15144 -- This code section is part of implementing an untyped tree
15145 -- traversal, so it needs direct access to node fields.
15146
15147 begin
15148 -- Do not save global references in pragmas generated from aspects
15149 -- because the pragmas will be regenerated at instantiation time.
15150
15151 if From_Aspect_Specification (Prag) then
15152 Do_Save := False;
15153
15154 -- The capture of global references within contract-related source
15155 -- pragmas associated with generic packages, subprograms or their
15156 -- respective bodies must be delayed due to timing of annotation
15157 -- analysis. Global references are still captured in routine
15158 -- Save_Global_References_In_Contract.
15159
15160 elsif Is_Generic_Contract_Pragma (Prag) and then Prag /= Templ then
15161 if Is_Package_Contract_Annotation (Prag) then
15162 Context := Find_Related_Package_Or_Body (Prag);
15163 else
15164 pragma Assert (Is_Subprogram_Contract_Annotation (Prag));
15165 Context := Find_Related_Declaration_Or_Body (Prag);
15166 end if;
15167
15168 -- The use of Original_Node accounts for the case when the
15169 -- related context is generic template.
15170
15171 if Requires_Delayed_Save (Original_Node (Context)) then
15172 Do_Save := False;
15173 end if;
15174 end if;
15175
15176 -- For all other cases, save all global references within the
15177 -- descendants, but skip the following semantic fields:
15178
15179 -- Field1 - Next_Pragma
15180 -- Field3 - Corresponding_Aspect
15181 -- Field5 - Next_Rep_Item
15182
15183 if Do_Save then
15184 Save_Global_Descendant (Field2 (Prag));
15185 Save_Global_Descendant (Field4 (Prag));
15186 end if;
15187 end Save_References_In_Pragma;
15188
15189 -- Start of processing for Save_References
15190
15191 begin
15192 if N = Empty then
15193 null;
15194
15195 -- Aggregates
15196
15197 elsif Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
15198 Save_References_In_Aggregate (N);
15199
15200 -- Character literals, operator symbols
15201
15202 elsif Nkind_In (N, N_Character_Literal, N_Operator_Symbol) then
15203 Save_References_In_Char_Lit_Or_Op_Symbol (N);
15204
15205 -- Defining identifiers
15206
15207 elsif Nkind (N) in N_Entity then
15208 null;
15209
15210 -- Identifiers
15211
15212 elsif Nkind (N) = N_Identifier then
15213 Save_References_In_Identifier (N);
15214
15215 -- Operators
15216
15217 elsif Nkind (N) in N_Op then
15218 Save_References_In_Operator (N);
15219
15220 -- Pragmas
15221
15222 elsif Nkind (N) = N_Pragma then
15223 Save_References_In_Pragma (N);
15224
15225 else
15226 Save_References_In_Descendants (N);
15227 end if;
15228
15229 -- Save all global references found within the aspect specifications
15230 -- of the related node.
15231
15232 if Permits_Aspect_Specifications (N) and then Has_Aspects (N) then
15233
15234 -- The capture of global references within aspects associated with
15235 -- generic packages, subprograms or their bodies must be delayed
15236 -- due to timing of annotation analysis. Global references are
15237 -- still captured in routine Save_Global_References_In_Contract.
15238
15239 if Requires_Delayed_Save (N) then
15240 null;
15241
15242 -- Otherwise save all global references within the aspects
15243
15244 else
15245 Save_Global_References_In_Aspects (N);
15246 end if;
15247 end if;
15248 end Save_References;
15249
15250 -- Start of processing for Save_Global_References
15251
15252 begin
15253 Gen_Scope := Current_Scope;
15254
15255 -- If the generic unit is a child unit, references to entities in the
15256 -- parent are treated as local, because they will be resolved anew in
15257 -- the context of the instance of the parent.
15258
15259 while Is_Child_Unit (Gen_Scope)
15260 and then Ekind (Scope (Gen_Scope)) = E_Generic_Package
15261 loop
15262 Gen_Scope := Scope (Gen_Scope);
15263 end loop;
15264
15265 Save_References (Templ);
15266 end Save_Global_References;
15267
15268 ---------------------------------------
15269 -- Save_Global_References_In_Aspects --
15270 ---------------------------------------
15271
15272 procedure Save_Global_References_In_Aspects (N : Node_Id) is
15273 Asp : Node_Id;
15274 Expr : Node_Id;
15275
15276 begin
15277 Asp := First (Aspect_Specifications (N));
15278 while Present (Asp) loop
15279 Expr := Expression (Asp);
15280
15281 if Present (Expr) then
15282 Save_Global_References (Expr);
15283 end if;
15284
15285 Next (Asp);
15286 end loop;
15287 end Save_Global_References_In_Aspects;
15288
15289 ------------------------------------------
15290 -- Set_Copied_Sloc_For_Inherited_Pragma --
15291 ------------------------------------------
15292
15293 procedure Set_Copied_Sloc_For_Inherited_Pragma
15294 (N : Node_Id;
15295 E : Entity_Id)
15296 is
15297 begin
15298 Create_Instantiation_Source (N, E,
15299 Inlined_Body => False,
15300 Inherited_Pragma => True,
15301 Factor => S_Adjustment);
15302 end Set_Copied_Sloc_For_Inherited_Pragma;
15303
15304 --------------------------------------
15305 -- Set_Copied_Sloc_For_Inlined_Body --
15306 --------------------------------------
15307
15308 procedure Set_Copied_Sloc_For_Inlined_Body (N : Node_Id; E : Entity_Id) is
15309 begin
15310 Create_Instantiation_Source (N, E,
15311 Inlined_Body => True,
15312 Inherited_Pragma => False,
15313 Factor => S_Adjustment);
15314 end Set_Copied_Sloc_For_Inlined_Body;
15315
15316 ---------------------
15317 -- Set_Instance_Of --
15318 ---------------------
15319
15320 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id) is
15321 begin
15322 Generic_Renamings.Table (Generic_Renamings.Last) := (A, B, Assoc_Null);
15323 Generic_Renamings_HTable.Set (Generic_Renamings.Last);
15324 Generic_Renamings.Increment_Last;
15325 end Set_Instance_Of;
15326
15327 --------------------
15328 -- Set_Next_Assoc --
15329 --------------------
15330
15331 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr) is
15332 begin
15333 Generic_Renamings.Table (E).Next_In_HTable := Next;
15334 end Set_Next_Assoc;
15335
15336 -------------------
15337 -- Start_Generic --
15338 -------------------
15339
15340 procedure Start_Generic is
15341 begin
15342 -- ??? More things could be factored out in this routine.
15343 -- Should probably be done at a later stage.
15344
15345 Generic_Flags.Append (Inside_A_Generic);
15346 Inside_A_Generic := True;
15347
15348 Expander_Mode_Save_And_Set (False);
15349 end Start_Generic;
15350
15351 ----------------------
15352 -- Set_Instance_Env --
15353 ----------------------
15354
15355 procedure Set_Instance_Env
15356 (Gen_Unit : Entity_Id;
15357 Act_Unit : Entity_Id)
15358 is
15359 Assertion_Status : constant Boolean := Assertions_Enabled;
15360 Save_SPARK_Mode : constant SPARK_Mode_Type := SPARK_Mode;
15361 Save_SPARK_Mode_Pragma : constant Node_Id := SPARK_Mode_Pragma;
15362
15363 begin
15364 -- Regardless of the current mode, predefined units are analyzed in the
15365 -- most current Ada mode, and earlier version Ada checks do not apply
15366 -- to predefined units. Nothing needs to be done for non-internal units.
15367 -- These are always analyzed in the current mode.
15368
15369 if Is_Internal_File_Name
15370 (Fname => Unit_File_Name (Get_Source_Unit (Gen_Unit)),
15371 Renamings_Included => True)
15372 then
15373 Set_Opt_Config_Switches (True, Current_Sem_Unit = Main_Unit);
15374
15375 -- In Ada2012 we may want to enable assertions in an instance of a
15376 -- predefined unit, in which case we need to preserve the current
15377 -- setting for the Assertions_Enabled flag. This will become more
15378 -- critical when pre/postconditions are added to predefined units,
15379 -- as is already the case for some numeric libraries.
15380
15381 if Ada_Version >= Ada_2012 then
15382 Assertions_Enabled := Assertion_Status;
15383 end if;
15384
15385 -- SPARK_Mode for an instance is the one applicable at the point of
15386 -- instantiation.
15387
15388 SPARK_Mode := Save_SPARK_Mode;
15389 SPARK_Mode_Pragma := Save_SPARK_Mode_Pragma;
15390 end if;
15391
15392 Current_Instantiated_Parent :=
15393 (Gen_Id => Gen_Unit,
15394 Act_Id => Act_Unit,
15395 Next_In_HTable => Assoc_Null);
15396 end Set_Instance_Env;
15397
15398 -----------------
15399 -- Switch_View --
15400 -----------------
15401
15402 procedure Switch_View (T : Entity_Id) is
15403 BT : constant Entity_Id := Base_Type (T);
15404 Priv_Elmt : Elmt_Id := No_Elmt;
15405 Priv_Sub : Entity_Id;
15406
15407 begin
15408 -- T may be private but its base type may have been exchanged through
15409 -- some other occurrence, in which case there is nothing to switch
15410 -- besides T itself. Note that a private dependent subtype of a private
15411 -- type might not have been switched even if the base type has been,
15412 -- because of the last branch of Check_Private_View (see comment there).
15413
15414 if not Is_Private_Type (BT) then
15415 Prepend_Elmt (Full_View (T), Exchanged_Views);
15416 Exchange_Declarations (T);
15417 return;
15418 end if;
15419
15420 Priv_Elmt := First_Elmt (Private_Dependents (BT));
15421
15422 if Present (Full_View (BT)) then
15423 Prepend_Elmt (Full_View (BT), Exchanged_Views);
15424 Exchange_Declarations (BT);
15425 end if;
15426
15427 while Present (Priv_Elmt) loop
15428 Priv_Sub := (Node (Priv_Elmt));
15429
15430 -- We avoid flipping the subtype if the Etype of its full view is
15431 -- private because this would result in a malformed subtype. This
15432 -- occurs when the Etype of the subtype full view is the full view of
15433 -- the base type (and since the base types were just switched, the
15434 -- subtype is pointing to the wrong view). This is currently the case
15435 -- for tagged record types, access types (maybe more?) and needs to
15436 -- be resolved. ???
15437
15438 if Present (Full_View (Priv_Sub))
15439 and then not Is_Private_Type (Etype (Full_View (Priv_Sub)))
15440 then
15441 Prepend_Elmt (Full_View (Priv_Sub), Exchanged_Views);
15442 Exchange_Declarations (Priv_Sub);
15443 end if;
15444
15445 Next_Elmt (Priv_Elmt);
15446 end loop;
15447 end Switch_View;
15448
15449 -----------------
15450 -- True_Parent --
15451 -----------------
15452
15453 function True_Parent (N : Node_Id) return Node_Id is
15454 begin
15455 if Nkind (Parent (N)) = N_Subunit then
15456 return Parent (Corresponding_Stub (Parent (N)));
15457 else
15458 return Parent (N);
15459 end if;
15460 end True_Parent;
15461
15462 -----------------------------
15463 -- Valid_Default_Attribute --
15464 -----------------------------
15465
15466 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id) is
15467 Attr_Id : constant Attribute_Id :=
15468 Get_Attribute_Id (Attribute_Name (Def));
15469 T : constant Entity_Id := Entity (Prefix (Def));
15470 Is_Fun : constant Boolean := (Ekind (Nam) = E_Function);
15471 F : Entity_Id;
15472 Num_F : Nat;
15473 OK : Boolean;
15474
15475 begin
15476 if No (T) or else T = Any_Id then
15477 return;
15478 end if;
15479
15480 Num_F := 0;
15481 F := First_Formal (Nam);
15482 while Present (F) loop
15483 Num_F := Num_F + 1;
15484 Next_Formal (F);
15485 end loop;
15486
15487 case Attr_Id is
15488 when Attribute_Adjacent
15489 | Attribute_Ceiling
15490 | Attribute_Copy_Sign
15491 | Attribute_Floor
15492 | Attribute_Fraction
15493 | Attribute_Machine
15494 | Attribute_Model
15495 | Attribute_Remainder
15496 | Attribute_Rounding
15497 | Attribute_Unbiased_Rounding
15498 =>
15499 OK := Is_Fun
15500 and then Num_F = 1
15501 and then Is_Floating_Point_Type (T);
15502
15503 when Attribute_Image
15504 | Attribute_Pred
15505 | Attribute_Succ
15506 | Attribute_Value
15507 | Attribute_Wide_Image
15508 | Attribute_Wide_Value
15509 =>
15510 OK := Is_Fun and then Num_F = 1 and then Is_Scalar_Type (T);
15511
15512 when Attribute_Max
15513 | Attribute_Min
15514 =>
15515 OK := Is_Fun and then Num_F = 2 and then Is_Scalar_Type (T);
15516
15517 when Attribute_Input =>
15518 OK := (Is_Fun and then Num_F = 1);
15519
15520 when Attribute_Output
15521 | Attribute_Read
15522 | Attribute_Write
15523 =>
15524 OK := not Is_Fun and then Num_F = 2;
15525
15526 when others =>
15527 OK := False;
15528 end case;
15529
15530 if not OK then
15531 Error_Msg_N
15532 ("attribute reference has wrong profile for subprogram", Def);
15533 end if;
15534 end Valid_Default_Attribute;
15535
15536 end Sem_Ch12;