[Ada] Minor reformatting
[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-2018, 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 Fname; use Fname;
34 with Fname.UF; use Fname.UF;
35 with Freeze; use Freeze;
36 with Ghost; use Ghost;
37 with Itypes; use Itypes;
38 with Lib; use Lib;
39 with Lib.Load; use Lib.Load;
40 with Lib.Xref; use Lib.Xref;
41 with Nlists; use Nlists;
42 with Namet; use Namet;
43 with Nmake; use Nmake;
44 with Opt; use Opt;
45 with Rident; use Rident;
46 with Restrict; use Restrict;
47 with Rtsfind; use Rtsfind;
48 with Sem; use Sem;
49 with Sem_Aux; use Sem_Aux;
50 with Sem_Cat; use Sem_Cat;
51 with Sem_Ch3; use Sem_Ch3;
52 with Sem_Ch6; use Sem_Ch6;
53 with Sem_Ch7; use Sem_Ch7;
54 with Sem_Ch8; use Sem_Ch8;
55 with Sem_Ch10; use Sem_Ch10;
56 with Sem_Ch13; use Sem_Ch13;
57 with Sem_Dim; use Sem_Dim;
58 with Sem_Disp; use Sem_Disp;
59 with Sem_Elab; use Sem_Elab;
60 with Sem_Elim; use Sem_Elim;
61 with Sem_Eval; use Sem_Eval;
62 with Sem_Prag; use Sem_Prag;
63 with Sem_Res; use Sem_Res;
64 with Sem_Type; use Sem_Type;
65 with Sem_Util; use Sem_Util;
66 with Sem_Warn; use Sem_Warn;
67 with Stand; use Stand;
68 with Sinfo; use Sinfo;
69 with Sinfo.CN; use Sinfo.CN;
70 with Sinput; use Sinput;
71 with Sinput.L; use Sinput.L;
72 with Snames; use Snames;
73 with Stringt; use Stringt;
74 with Uname; use Uname;
75 with Table;
76 with Tbuild; use Tbuild;
77 with Uintp; use Uintp;
78 with Urealp; use Urealp;
79 with Warnsw; use Warnsw;
80
81 with GNAT.HTable;
82
83 package body Sem_Ch12 is
84
85 ----------------------------------------------------------
86 -- Implementation of Generic Analysis and Instantiation --
87 ----------------------------------------------------------
88
89 -- GNAT implements generics by macro expansion. No attempt is made to share
90 -- generic instantiations (for now). Analysis of a generic definition does
91 -- not perform any expansion action, but the expander must be called on the
92 -- tree for each instantiation, because the expansion may of course depend
93 -- on the generic actuals. All of this is best achieved as follows:
94 --
95 -- a) Semantic analysis of a generic unit is performed on a copy of the
96 -- tree for the generic unit. All tree modifications that follow analysis
97 -- do not affect the original tree. Links are kept between the original
98 -- tree and the copy, in order to recognize non-local references within
99 -- the generic, and propagate them to each instance (recall that name
100 -- resolution is done on the generic declaration: generics are not really
101 -- macros). This is summarized in the following diagram:
102
103 -- .-----------. .----------.
104 -- | semantic |<--------------| generic |
105 -- | copy | | unit |
106 -- | |==============>| |
107 -- |___________| global |__________|
108 -- references | | |
109 -- | | |
110 -- .-----|--|.
111 -- | .-----|---.
112 -- | | .----------.
113 -- | | | generic |
114 -- |__| | |
115 -- |__| instance |
116 -- |__________|
117
118 -- b) Each instantiation copies the original tree, and inserts into it a
119 -- series of declarations that describe the mapping between generic formals
120 -- and actuals. For example, a generic In OUT parameter is an object
121 -- renaming of the corresponding actual, etc. Generic IN parameters are
122 -- constant declarations.
123
124 -- c) In order to give the right visibility for these renamings, we use
125 -- a different scheme for package and subprogram instantiations. For
126 -- packages, the list of renamings is inserted into the package
127 -- specification, before the visible declarations of the package. The
128 -- renamings are analyzed before any of the text of the instance, and are
129 -- thus visible at the right place. Furthermore, outside of the instance,
130 -- the generic parameters are visible and denote their corresponding
131 -- actuals.
132
133 -- For subprograms, we create a container package to hold the renamings
134 -- and the subprogram instance itself. Analysis of the package makes the
135 -- renaming declarations visible to the subprogram. After analyzing the
136 -- package, the defining entity for the subprogram is touched-up so that
137 -- it appears declared in the current scope, and not inside the container
138 -- package.
139
140 -- If the instantiation is a compilation unit, the container package is
141 -- given the same name as the subprogram instance. This ensures that
142 -- the elaboration procedure called by the binder, using the compilation
143 -- unit name, calls in fact the elaboration procedure for the package.
144
145 -- Not surprisingly, private types complicate this approach. By saving in
146 -- the original generic object the non-local references, we guarantee that
147 -- the proper entities are referenced at the point of instantiation.
148 -- However, for private types, this by itself does not insure that the
149 -- proper VIEW of the entity is used (the full type may be visible at the
150 -- point of generic definition, but not at instantiation, or vice-versa).
151 -- In order to reference the proper view, we special-case any reference
152 -- to private types in the generic object, by saving both views, one in
153 -- the generic and one in the semantic copy. At time of instantiation, we
154 -- check whether the two views are consistent, and exchange declarations if
155 -- necessary, in order to restore the correct visibility. Similarly, if
156 -- the instance view is private when the generic view was not, we perform
157 -- the exchange. After completing the instantiation, we restore the
158 -- current visibility. The flag Has_Private_View marks identifiers in the
159 -- the generic unit that require checking.
160
161 -- Visibility within nested generic units requires special handling.
162 -- Consider the following scheme:
163
164 -- type Global is ... -- outside of generic unit.
165 -- generic ...
166 -- package Outer is
167 -- ...
168 -- type Semi_Global is ... -- global to inner.
169
170 -- generic ... -- 1
171 -- procedure inner (X1 : Global; X2 : Semi_Global);
172
173 -- procedure in2 is new inner (...); -- 4
174 -- end Outer;
175
176 -- package New_Outer is new Outer (...); -- 2
177 -- procedure New_Inner is new New_Outer.Inner (...); -- 3
178
179 -- The semantic analysis of Outer captures all occurrences of Global.
180 -- The semantic analysis of Inner (at 1) captures both occurrences of
181 -- Global and Semi_Global.
182
183 -- At point 2 (instantiation of Outer), we also produce a generic copy
184 -- of Inner, even though Inner is, at that point, not being instantiated.
185 -- (This is just part of the semantic analysis of New_Outer).
186
187 -- Critically, references to Global within Inner must be preserved, while
188 -- references to Semi_Global should not preserved, because they must now
189 -- resolve to an entity within New_Outer. To distinguish between these, we
190 -- use a global variable, Current_Instantiated_Parent, which is set when
191 -- performing a generic copy during instantiation (at 2). This variable is
192 -- used when performing a generic copy that is not an instantiation, but
193 -- that is nested within one, as the occurrence of 1 within 2. The analysis
194 -- of a nested generic only preserves references that are global to the
195 -- enclosing Current_Instantiated_Parent. We use the Scope_Depth value to
196 -- determine whether a reference is external to the given parent.
197
198 -- The instantiation at point 3 requires no special treatment. The method
199 -- works as well for further nestings of generic units, but of course the
200 -- variable Current_Instantiated_Parent must be stacked because nested
201 -- instantiations can occur, e.g. the occurrence of 4 within 2.
202
203 -- The instantiation of package and subprogram bodies is handled in a
204 -- similar manner, except that it is delayed until after semantic
205 -- analysis is complete. In this fashion complex cross-dependencies
206 -- between several package declarations and bodies containing generics
207 -- can be compiled which otherwise would diagnose spurious circularities.
208
209 -- For example, it is possible to compile two packages A and B that
210 -- have the following structure:
211
212 -- package A is package B is
213 -- generic ... generic ...
214 -- package G_A is package G_B is
215
216 -- with B; with A;
217 -- package body A is package body B is
218 -- package N_B is new G_B (..) package N_A is new G_A (..)
219
220 -- The table Pending_Instantiations in package Inline is used to keep
221 -- track of body instantiations that are delayed in this manner. Inline
222 -- handles the actual calls to do the body instantiations. This activity
223 -- is part of Inline, since the processing occurs at the same point, and
224 -- for essentially the same reason, as the handling of inlined routines.
225
226 ----------------------------------------------
227 -- Detection of Instantiation Circularities --
228 ----------------------------------------------
229
230 -- If we have a chain of instantiations that is circular, this is static
231 -- error which must be detected at compile time. The detection of these
232 -- circularities is carried out at the point that we insert a generic
233 -- instance spec or body. If there is a circularity, then the analysis of
234 -- the offending spec or body will eventually result in trying to load the
235 -- same unit again, and we detect this problem as we analyze the package
236 -- instantiation for the second time.
237
238 -- At least in some cases after we have detected the circularity, we get
239 -- into trouble if we try to keep going. The following flag is set if a
240 -- circularity is detected, and used to abandon compilation after the
241 -- messages have been posted.
242
243 -----------------------------------------
244 -- Implementation of Generic Contracts --
245 -----------------------------------------
246
247 -- A "contract" is a collection of aspects and pragmas that either verify a
248 -- property of a construct at runtime or classify the data flow to and from
249 -- the construct in some fashion.
250
251 -- Generic packages, subprograms and their respective bodies may be subject
252 -- to the following contract-related aspects or pragmas collectively known
253 -- as annotations:
254
255 -- package subprogram [body]
256 -- Abstract_State Contract_Cases
257 -- Initial_Condition Depends
258 -- Initializes Extensions_Visible
259 -- Global
260 -- package body Post
261 -- Refined_State Post_Class
262 -- Postcondition
263 -- Pre
264 -- Pre_Class
265 -- Precondition
266 -- Refined_Depends
267 -- Refined_Global
268 -- Refined_Post
269 -- Test_Case
270
271 -- Most package contract annotations utilize forward references to classify
272 -- data declared within the package [body]. Subprogram annotations then use
273 -- the classifications to further refine them. These inter dependencies are
274 -- problematic with respect to the implementation of generics because their
275 -- analysis, capture of global references and instantiation does not mesh
276 -- well with the existing mechanism.
277
278 -- 1) Analysis of generic contracts is carried out the same way non-generic
279 -- contracts are analyzed:
280
281 -- 1.1) General rule - a contract is analyzed after all related aspects
282 -- and pragmas are analyzed. This is done by routines
283
284 -- Analyze_Package_Body_Contract
285 -- Analyze_Package_Contract
286 -- Analyze_Subprogram_Body_Contract
287 -- Analyze_Subprogram_Contract
288
289 -- 1.2) Compilation unit - the contract is analyzed after Pragmas_After
290 -- are processed.
291
292 -- 1.3) Compilation unit body - the contract is analyzed at the end of
293 -- the body declaration list.
294
295 -- 1.4) Package - the contract is analyzed at the end of the private or
296 -- visible declarations, prior to analyzing the contracts of any nested
297 -- packages or subprograms.
298
299 -- 1.5) Package body - the contract is analyzed at the end of the body
300 -- declaration list, prior to analyzing the contracts of any nested
301 -- packages or subprograms.
302
303 -- 1.6) Subprogram - if the subprogram is declared inside a block, a
304 -- package or a subprogram, then its contract is analyzed at the end of
305 -- the enclosing declarations, otherwise the subprogram is a compilation
306 -- unit 1.2).
307
308 -- 1.7) Subprogram body - if the subprogram body is declared inside a
309 -- block, a package body or a subprogram body, then its contract is
310 -- analyzed at the end of the enclosing declarations, otherwise the
311 -- subprogram is a compilation unit 1.3).
312
313 -- 2) Capture of global references within contracts is done after capturing
314 -- global references within the generic template. There are two reasons for
315 -- this delay - pragma annotations are not part of the generic template in
316 -- the case of a generic subprogram declaration, and analysis of contracts
317 -- is delayed.
318
319 -- Contract-related source pragmas within generic templates are prepared
320 -- for delayed capture of global references by routine
321
322 -- Create_Generic_Contract
323
324 -- The routine associates these pragmas with the contract of the template.
325 -- In the case of a generic subprogram declaration, the routine creates
326 -- generic templates for the pragmas declared after the subprogram because
327 -- they are not part of the template.
328
329 -- generic -- template starts
330 -- procedure Gen_Proc (Input : Integer); -- template ends
331 -- pragma Precondition (Input > 0); -- requires own template
332
333 -- 2.1) The capture of global references with aspect specifications and
334 -- source pragmas that apply to a generic unit must be suppressed when
335 -- the generic template is being processed because the contracts have not
336 -- been analyzed yet. Any attempts to capture global references at that
337 -- point will destroy the Associated_Node linkages and leave the template
338 -- undecorated. This delay is controlled by routine
339
340 -- Requires_Delayed_Save
341
342 -- 2.2) The real capture of global references within a contract is done
343 -- after the contract has been analyzed, by routine
344
345 -- Save_Global_References_In_Contract
346
347 -- 3) The instantiation of a generic contract occurs as part of the
348 -- instantiation of the contract owner. Generic subprogram declarations
349 -- require additional processing when the contract is specified by pragmas
350 -- because the pragmas are not part of the generic template. This is done
351 -- by routine
352
353 -- Instantiate_Subprogram_Contract
354
355 Circularity_Detected : Boolean := False;
356 -- This should really be reset on encountering a new main unit, but in
357 -- practice we are not using multiple main units so it is not critical.
358
359 --------------------------------------------------
360 -- Formal packages and partial parameterization --
361 --------------------------------------------------
362
363 -- When compiling a generic, a formal package is a local instantiation. If
364 -- declared with a box, its generic formals are visible in the enclosing
365 -- generic. If declared with a partial list of actuals, those actuals that
366 -- are defaulted (covered by an Others clause, or given an explicit box
367 -- initialization) are also visible in the enclosing generic, while those
368 -- that have a corresponding actual are not.
369
370 -- In our source model of instantiation, the same visibility must be
371 -- present in the spec and body of an instance: the names of the formals
372 -- that are defaulted must be made visible within the instance, and made
373 -- invisible (hidden) after the instantiation is complete, so that they
374 -- are not accessible outside of the instance.
375
376 -- In a generic, a formal package is treated like a special instantiation.
377 -- Our Ada 95 compiler handled formals with and without box in different
378 -- ways. With partial parameterization, we use a single model for both.
379 -- We create a package declaration that consists of the specification of
380 -- the generic package, and a set of declarations that map the actuals
381 -- into local renamings, just as we do for bona fide instantiations. For
382 -- defaulted parameters and formals with a box, we copy directly the
383 -- declarations of the formal into this local package. The result is a
384 -- a package whose visible declarations may include generic formals. This
385 -- package is only used for type checking and visibility analysis, and
386 -- never reaches the back-end, so it can freely violate the placement
387 -- rules for generic formal declarations.
388
389 -- The list of declarations (renamings and copies of formals) is built
390 -- by Analyze_Associations, just as for regular instantiations.
391
392 -- At the point of instantiation, conformance checking must be applied only
393 -- to those parameters that were specified in the formal. We perform this
394 -- checking by creating another internal instantiation, this one including
395 -- only the renamings and the formals (the rest of the package spec is not
396 -- relevant to conformance checking). We can then traverse two lists: the
397 -- list of actuals in the instance that corresponds to the formal package,
398 -- and the list of actuals produced for this bogus instantiation. We apply
399 -- the conformance rules to those actuals that are not defaulted (i.e.
400 -- which still appear as generic formals.
401
402 -- When we compile an instance body we must make the right parameters
403 -- visible again. The predicate Is_Generic_Formal indicates which of the
404 -- formals should have its Is_Hidden flag reset.
405
406 -----------------------
407 -- Local subprograms --
408 -----------------------
409
410 procedure Abandon_Instantiation (N : Node_Id);
411 pragma No_Return (Abandon_Instantiation);
412 -- Posts an error message "instantiation abandoned" at the indicated node
413 -- and then raises the exception Instantiation_Error to do it.
414
415 procedure Analyze_Formal_Array_Type
416 (T : in out Entity_Id;
417 Def : Node_Id);
418 -- A formal array type is treated like an array type declaration, and
419 -- invokes Array_Type_Declaration (sem_ch3) whose first parameter is
420 -- in-out, because in the case of an anonymous type the entity is
421 -- actually created in the procedure.
422
423 -- The following procedures treat other kinds of formal parameters
424
425 procedure Analyze_Formal_Derived_Interface_Type
426 (N : Node_Id;
427 T : Entity_Id;
428 Def : Node_Id);
429
430 procedure Analyze_Formal_Derived_Type
431 (N : Node_Id;
432 T : Entity_Id;
433 Def : Node_Id);
434
435 procedure Analyze_Formal_Interface_Type
436 (N : Node_Id;
437 T : Entity_Id;
438 Def : Node_Id);
439
440 -- The following subprograms create abbreviated declarations for formal
441 -- scalar types. We introduce an anonymous base of the proper class for
442 -- each of them, and define the formals as constrained first subtypes of
443 -- their bases. The bounds are expressions that are non-static in the
444 -- generic.
445
446 procedure Analyze_Formal_Decimal_Fixed_Point_Type
447 (T : Entity_Id; Def : Node_Id);
448 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id);
449 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id);
450 procedure Analyze_Formal_Signed_Integer_Type (T : Entity_Id; Def : Node_Id);
451 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id);
452 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
453 (T : Entity_Id; Def : Node_Id);
454
455 procedure Analyze_Formal_Private_Type
456 (N : Node_Id;
457 T : Entity_Id;
458 Def : Node_Id);
459 -- Creates a new private type, which does not require completion
460
461 procedure Analyze_Formal_Incomplete_Type (T : Entity_Id; Def : Node_Id);
462 -- Ada 2012: Creates a new incomplete type whose actual does not freeze
463
464 procedure Analyze_Generic_Formal_Part (N : Node_Id);
465 -- Analyze generic formal part
466
467 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id);
468 -- Create a new access type with the given designated type
469
470 function Analyze_Associations
471 (I_Node : Node_Id;
472 Formals : List_Id;
473 F_Copy : List_Id) return List_Id;
474 -- At instantiation time, build the list of associations between formals
475 -- and actuals. Each association becomes a renaming declaration for the
476 -- formal entity. F_Copy is the analyzed list of formals in the generic
477 -- copy. It is used to apply legality checks to the actuals. I_Node is the
478 -- instantiation node itself.
479
480 procedure Analyze_Subprogram_Instantiation
481 (N : Node_Id;
482 K : Entity_Kind);
483
484 procedure Build_Instance_Compilation_Unit_Nodes
485 (N : Node_Id;
486 Act_Body : Node_Id;
487 Act_Decl : Node_Id);
488 -- This procedure is used in the case where the generic instance of a
489 -- subprogram body or package body is a library unit. In this case, the
490 -- original library unit node for the generic instantiation must be
491 -- replaced by the resulting generic body, and a link made to a new
492 -- compilation unit node for the generic declaration. The argument N is
493 -- the original generic instantiation. Act_Body and Act_Decl are the body
494 -- and declaration of the instance (either package body and declaration
495 -- nodes or subprogram body and declaration nodes depending on the case).
496 -- On return, the node N has been rewritten with the actual body.
497
498 procedure Check_Access_Definition (N : Node_Id);
499 -- Subsidiary routine to null exclusion processing. Perform an assertion
500 -- check on Ada version and the presence of an access definition in N.
501
502 procedure Check_Formal_Packages (P_Id : Entity_Id);
503 -- Apply the following to all formal packages in generic associations.
504 -- Restore the visibility of the formals of the instance that are not
505 -- defaulted (see RM 12.7 (10)). Remove the anonymous package declaration
506 -- created for formal instances that are not defaulted.
507
508 procedure Check_Formal_Package_Instance
509 (Formal_Pack : Entity_Id;
510 Actual_Pack : Entity_Id);
511 -- Verify that the actuals of the actual instance match the actuals of
512 -- the template for a formal package that is not declared with a box.
513
514 procedure Check_Forward_Instantiation (Decl : Node_Id);
515 -- If the generic is a local entity and the corresponding body has not
516 -- been seen yet, flag enclosing packages to indicate that it will be
517 -- elaborated after the generic body. Subprograms declared in the same
518 -- package cannot be inlined by the front end because front-end inlining
519 -- requires a strict linear order of elaboration.
520
521 function Check_Hidden_Primitives (Assoc_List : List_Id) return Elist_Id;
522 -- Check if some association between formals and actuals requires to make
523 -- visible primitives of a tagged type, and make those primitives visible.
524 -- Return the list of primitives whose visibility is modified (to restore
525 -- their visibility later through Restore_Hidden_Primitives). If no
526 -- candidate is found then return No_Elist.
527
528 procedure Check_Hidden_Child_Unit
529 (N : Node_Id;
530 Gen_Unit : Entity_Id;
531 Act_Decl_Id : Entity_Id);
532 -- If the generic unit is an implicit child instance within a parent
533 -- instance, we need to make an explicit test that it is not hidden by
534 -- a child instance of the same name and parent.
535
536 procedure Check_Generic_Actuals
537 (Instance : Entity_Id;
538 Is_Formal_Box : Boolean);
539 -- Similar to previous one. Check the actuals in the instantiation,
540 -- whose views can change between the point of instantiation and the point
541 -- of instantiation of the body. In addition, mark the generic renamings
542 -- as generic actuals, so that they are not compatible with other actuals.
543 -- Recurse on an actual that is a formal package whose declaration has
544 -- a box.
545
546 function Contains_Instance_Of
547 (Inner : Entity_Id;
548 Outer : Entity_Id;
549 N : Node_Id) return Boolean;
550 -- Inner is instantiated within the generic Outer. Check whether Inner
551 -- directly or indirectly contains an instance of Outer or of one of its
552 -- parents, in the case of a subunit. Each generic unit holds a list of
553 -- the entities instantiated within (at any depth). This procedure
554 -- determines whether the set of such lists contains a cycle, i.e. an
555 -- illegal circular instantiation.
556
557 function Denotes_Formal_Package
558 (Pack : Entity_Id;
559 On_Exit : Boolean := False;
560 Instance : Entity_Id := Empty) return Boolean;
561 -- Returns True if E is a formal package of an enclosing generic, or
562 -- the actual for such a formal in an enclosing instantiation. If such
563 -- a package is used as a formal in an nested generic, or as an actual
564 -- in a nested instantiation, the visibility of ITS formals should not
565 -- be modified. When called from within Restore_Private_Views, the flag
566 -- On_Exit is true, to indicate that the search for a possible enclosing
567 -- instance should ignore the current one. In that case Instance denotes
568 -- the declaration for which this is an actual. This declaration may be
569 -- an instantiation in the source, or the internal instantiation that
570 -- corresponds to the actual for a formal package.
571
572 function Earlier (N1, N2 : Node_Id) return Boolean;
573 -- Yields True if N1 and N2 appear in the same compilation unit,
574 -- ignoring subunits, and if N1 is to the left of N2 in a left-to-right
575 -- traversal of the tree for the unit. Used to determine the placement
576 -- of freeze nodes for instance bodies that may depend on other instances.
577
578 function Find_Actual_Type
579 (Typ : Entity_Id;
580 Gen_Type : Entity_Id) return Entity_Id;
581 -- When validating the actual types of a child instance, check whether
582 -- the formal is a formal type of the parent unit, and retrieve the current
583 -- actual for it. Typ is the entity in the analyzed formal type declaration
584 -- (component or index type of an array type, or designated type of an
585 -- access formal) and Gen_Type is the enclosing analyzed formal array
586 -- or access type. The desired actual may be a formal of a parent, or may
587 -- be declared in a formal package of a parent. In both cases it is a
588 -- generic actual type because it appears within a visible instance.
589 -- Finally, it may be declared in a parent unit without being a formal
590 -- of that unit, in which case it must be retrieved by visibility.
591 -- Ambiguities may still arise if two homonyms are declared in two formal
592 -- packages, and the prefix of the formal type may be needed to resolve
593 -- the ambiguity in the instance ???
594
595 procedure Freeze_Subprogram_Body
596 (Inst_Node : Node_Id;
597 Gen_Body : Node_Id;
598 Pack_Id : Entity_Id);
599 -- The generic body may appear textually after the instance, including
600 -- in the proper body of a stub, or within a different package instance.
601 -- Given that the instance can only be elaborated after the generic, we
602 -- place freeze_nodes for the instance and/or for packages that may enclose
603 -- the instance and the generic, so that the back-end can establish the
604 -- proper order of elaboration.
605
606 function Get_Associated_Node (N : Node_Id) return Node_Id;
607 -- In order to propagate semantic information back from the analyzed copy
608 -- to the original generic, we maintain links between selected nodes in the
609 -- generic and their corresponding copies. At the end of generic analysis,
610 -- the routine Save_Global_References traverses the generic tree, examines
611 -- the semantic information, and preserves the links to those nodes that
612 -- contain global information. At instantiation, the information from the
613 -- associated node is placed on the new copy, so that name resolution is
614 -- not repeated.
615 --
616 -- Three kinds of source nodes have associated nodes:
617 --
618 -- a) those that can reference (denote) entities, that is identifiers,
619 -- character literals, expanded_names, operator symbols, operators,
620 -- and attribute reference nodes. These nodes have an Entity field
621 -- and are the set of nodes that are in N_Has_Entity.
622 --
623 -- b) aggregates (N_Aggregate and N_Extension_Aggregate)
624 --
625 -- c) selected components (N_Selected_Component)
626 --
627 -- For the first class, the associated node preserves the entity if it is
628 -- global. If the generic contains nested instantiations, the associated
629 -- node itself has been recopied, and a chain of them must be followed.
630 --
631 -- For aggregates, the associated node allows retrieval of the type, which
632 -- may otherwise not appear in the generic. The view of this type may be
633 -- different between generic and instantiation, and the full view can be
634 -- installed before the instantiation is analyzed. For aggregates of type
635 -- extensions, the same view exchange may have to be performed for some of
636 -- the ancestor types, if their view is private at the point of
637 -- instantiation.
638 --
639 -- Nodes that are selected components in the parse tree may be rewritten
640 -- as expanded names after resolution, and must be treated as potential
641 -- entity holders, which is why they also have an Associated_Node.
642 --
643 -- Nodes that do not come from source, such as freeze nodes, do not appear
644 -- in the generic tree, and need not have an associated node.
645 --
646 -- The associated node is stored in the Associated_Node field. Note that
647 -- this field overlaps Entity, which is fine, because the whole point is
648 -- that we don't need or want the normal Entity field in this situation.
649
650 function Has_Been_Exchanged (E : Entity_Id) return Boolean;
651 -- Traverse the Exchanged_Views list to see if a type was private
652 -- and has already been flipped during this phase of instantiation.
653
654 procedure Hide_Current_Scope;
655 -- When instantiating a generic child unit, the parent context must be
656 -- present, but the instance and all entities that may be generated
657 -- must be inserted in the current scope. We leave the current scope
658 -- on the stack, but make its entities invisible to avoid visibility
659 -- problems. This is reversed at the end of the instantiation. This is
660 -- not done for the instantiation of the bodies, which only require the
661 -- instances of the generic parents to be in scope.
662
663 function In_Main_Context (E : Entity_Id) return Boolean;
664 -- Check whether an instantiation is in the context of the main unit.
665 -- Used to determine whether its body should be elaborated to allow
666 -- front-end inlining.
667
668 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id);
669 -- Add the context clause of the unit containing a generic unit to a
670 -- compilation unit that is, or contains, an instantiation.
671
672 procedure Init_Env;
673 -- Establish environment for subsequent instantiation. Separated from
674 -- Save_Env because data-structures for visibility handling must be
675 -- initialized before call to Check_Generic_Child_Unit.
676
677 procedure Inline_Instance_Body
678 (N : Node_Id;
679 Gen_Unit : Entity_Id;
680 Act_Decl : Node_Id);
681 -- If front-end inlining is requested, instantiate the package body,
682 -- and preserve the visibility of its compilation unit, to insure
683 -- that successive instantiations succeed.
684
685 procedure Insert_Freeze_Node_For_Instance
686 (N : Node_Id;
687 F_Node : Node_Id);
688 -- N denotes a package or a subprogram instantiation and F_Node is the
689 -- associated freeze node. Insert the freeze node before the first source
690 -- body which follows immediately after N. If no such body is found, the
691 -- freeze node is inserted at the end of the declarative region which
692 -- contains N.
693
694 procedure Install_Body
695 (Act_Body : Node_Id;
696 N : Node_Id;
697 Gen_Body : Node_Id;
698 Gen_Decl : Node_Id);
699 -- If the instantiation happens textually before the body of the generic,
700 -- the instantiation of the body must be analyzed after the generic body,
701 -- and not at the point of instantiation. Such early instantiations can
702 -- happen if the generic and the instance appear in a package declaration
703 -- because the generic body can only appear in the corresponding package
704 -- body. Early instantiations can also appear if generic, instance and
705 -- body are all in the declarative part of a subprogram or entry. Entities
706 -- of packages that are early instantiations are delayed, and their freeze
707 -- node appears after the generic body. This rather complex machinery is
708 -- needed when nested instantiations are present, because the source does
709 -- not carry any indication of where the corresponding instance bodies must
710 -- be installed and frozen.
711
712 procedure Install_Formal_Packages (Par : Entity_Id);
713 -- Install the visible part of any formal of the parent that is a formal
714 -- package. Note that for the case of a formal package with a box, this
715 -- includes the formal part of the formal package (12.7(10/2)).
716
717 procedure Install_Hidden_Primitives
718 (Prims_List : in out Elist_Id;
719 Gen_T : Entity_Id;
720 Act_T : Entity_Id);
721 -- Remove suffix 'P' from hidden primitives of Act_T to match the
722 -- visibility of primitives of Gen_T. The list of primitives to which
723 -- the suffix is removed is added to Prims_List to restore them later.
724
725 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False);
726 -- When compiling an instance of a child unit the parent (which is
727 -- itself an instance) is an enclosing scope that must be made
728 -- immediately visible. This procedure is also used to install the non-
729 -- generic parent of a generic child unit when compiling its body, so
730 -- that full views of types in the parent are made visible.
731
732 -- The functions Instantiate_XXX perform various legality checks and build
733 -- the declarations for instantiated generic parameters. In all of these
734 -- Formal is the entity in the generic unit, Actual is the entity of
735 -- expression in the generic associations, and Analyzed_Formal is the
736 -- formal in the generic copy, which contains the semantic information to
737 -- be used to validate the actual.
738
739 function Instantiate_Object
740 (Formal : Node_Id;
741 Actual : Node_Id;
742 Analyzed_Formal : Node_Id) return List_Id;
743
744 function Instantiate_Type
745 (Formal : Node_Id;
746 Actual : Node_Id;
747 Analyzed_Formal : Node_Id;
748 Actual_Decls : List_Id) return List_Id;
749
750 function Instantiate_Formal_Subprogram
751 (Formal : Node_Id;
752 Actual : Node_Id;
753 Analyzed_Formal : Node_Id) return Node_Id;
754
755 function Instantiate_Formal_Package
756 (Formal : Node_Id;
757 Actual : Node_Id;
758 Analyzed_Formal : Node_Id) return List_Id;
759 -- If the formal package is declared with a box, special visibility rules
760 -- apply to its formals: they are in the visible part of the package. This
761 -- is true in the declarative region of the formal package, that is to say
762 -- in the enclosing generic or instantiation. For an instantiation, the
763 -- parameters of the formal package are made visible in an explicit step.
764 -- Furthermore, if the actual has a visible USE clause, these formals must
765 -- be made potentially use-visible as well. On exit from the enclosing
766 -- instantiation, the reverse must be done.
767
768 -- For a formal package declared without a box, there are conformance rules
769 -- that apply to the actuals in the generic declaration and the actuals of
770 -- the actual package in the enclosing instantiation. The simplest way to
771 -- apply these rules is to repeat the instantiation of the formal package
772 -- in the context of the enclosing instance, and compare the generic
773 -- associations of this instantiation with those of the actual package.
774 -- This internal instantiation only needs to contain the renamings of the
775 -- formals: the visible and private declarations themselves need not be
776 -- created.
777
778 -- In Ada 2005, the formal package may be only partially parameterized.
779 -- In that case the visibility step must make visible those actuals whose
780 -- corresponding formals were given with a box. A final complication
781 -- involves inherited operations from formal derived types, which must
782 -- be visible if the type is.
783
784 function Is_In_Main_Unit (N : Node_Id) return Boolean;
785 -- Test if given node is in the main unit
786
787 procedure Load_Parent_Of_Generic
788 (N : Node_Id;
789 Spec : Node_Id;
790 Body_Optional : Boolean := False);
791 -- If the generic appears in a separate non-generic library unit, load the
792 -- corresponding body to retrieve the body of the generic. N is the node
793 -- for the generic instantiation, Spec is the generic package declaration.
794 --
795 -- Body_Optional is a flag that indicates that the body is being loaded to
796 -- ensure that temporaries are generated consistently when there are other
797 -- instances in the current declarative part that precede the one being
798 -- loaded. In that case a missing body is acceptable.
799
800 procedure Map_Formal_Package_Entities (Form : Entity_Id; Act : Entity_Id);
801 -- Within the generic part, entities in the formal package are
802 -- visible. To validate subsequent type declarations, indicate
803 -- the correspondence between the entities in the analyzed formal,
804 -- and the entities in the actual package. There are three packages
805 -- involved in the instantiation of a formal package: the parent
806 -- generic P1 which appears in the generic declaration, the fake
807 -- instantiation P2 which appears in the analyzed generic, and whose
808 -- visible entities may be used in subsequent formals, and the actual
809 -- P3 in the instance. To validate subsequent formals, me indicate
810 -- that the entities in P2 are mapped into those of P3. The mapping of
811 -- entities has to be done recursively for nested packages.
812
813 procedure Move_Freeze_Nodes
814 (Out_Of : Entity_Id;
815 After : Node_Id;
816 L : List_Id);
817 -- Freeze nodes can be generated in the analysis of a generic unit, but
818 -- will not be seen by the back-end. It is necessary to move those nodes
819 -- to the enclosing scope if they freeze an outer entity. We place them
820 -- at the end of the enclosing generic package, which is semantically
821 -- neutral.
822
823 procedure Preanalyze_Actuals (N : Node_Id; Inst : Entity_Id := Empty);
824 -- Analyze actuals to perform name resolution. Full resolution is done
825 -- later, when the expected types are known, but names have to be captured
826 -- before installing parents of generics, that are not visible for the
827 -- actuals themselves.
828 --
829 -- If Inst is present, it is the entity of the package instance. This
830 -- entity is marked as having a limited_view actual when some actual is
831 -- a limited view. This is used to place the instance body properly.
832
833 procedure Provide_Completing_Bodies (N : Node_Id);
834 -- Generate completing bodies for all subprograms found within package or
835 -- subprogram declaration N.
836
837 procedure Remove_Parent (In_Body : Boolean := False);
838 -- Reverse effect after instantiation of child is complete
839
840 procedure Restore_Hidden_Primitives (Prims_List : in out Elist_Id);
841 -- Restore suffix 'P' to primitives of Prims_List and leave Prims_List
842 -- set to No_Elist.
843
844 procedure Set_Instance_Env
845 (Gen_Unit : Entity_Id;
846 Act_Unit : Entity_Id);
847 -- Save current instance on saved environment, to be used to determine
848 -- the global status of entities in nested instances. Part of Save_Env.
849 -- called after verifying that the generic unit is legal for the instance,
850 -- The procedure also examines whether the generic unit is a predefined
851 -- unit, in order to set configuration switches accordingly. As a result
852 -- the procedure must be called after analyzing and freezing the actuals.
853
854 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id);
855 -- Associate analyzed generic parameter with corresponding instance. Used
856 -- for semantic checks at instantiation time.
857
858 function True_Parent (N : Node_Id) return Node_Id;
859 -- For a subunit, return parent of corresponding stub, else return
860 -- parent of node.
861
862 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id);
863 -- Verify that an attribute that appears as the default for a formal
864 -- subprogram is a function or procedure with the correct profile.
865
866 -------------------------------------------
867 -- Data Structures for Generic Renamings --
868 -------------------------------------------
869
870 -- The map Generic_Renamings associates generic entities with their
871 -- corresponding actuals. Currently used to validate type instances. It
872 -- will eventually be used for all generic parameters to eliminate the
873 -- need for overload resolution in the instance.
874
875 type Assoc_Ptr is new Int;
876
877 Assoc_Null : constant Assoc_Ptr := -1;
878
879 type Assoc is record
880 Gen_Id : Entity_Id;
881 Act_Id : Entity_Id;
882 Next_In_HTable : Assoc_Ptr;
883 end record;
884
885 package Generic_Renamings is new Table.Table
886 (Table_Component_Type => Assoc,
887 Table_Index_Type => Assoc_Ptr,
888 Table_Low_Bound => 0,
889 Table_Initial => 10,
890 Table_Increment => 100,
891 Table_Name => "Generic_Renamings");
892
893 -- Variable to hold enclosing instantiation. When the environment is
894 -- saved for a subprogram inlining, the corresponding Act_Id is empty.
895
896 Current_Instantiated_Parent : Assoc := (Empty, Empty, Assoc_Null);
897
898 -- Hash table for associations
899
900 HTable_Size : constant := 37;
901 type HTable_Range is range 0 .. HTable_Size - 1;
902
903 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr);
904 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr;
905 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id;
906 function Hash (F : Entity_Id) return HTable_Range;
907
908 package Generic_Renamings_HTable is new GNAT.HTable.Static_HTable (
909 Header_Num => HTable_Range,
910 Element => Assoc,
911 Elmt_Ptr => Assoc_Ptr,
912 Null_Ptr => Assoc_Null,
913 Set_Next => Set_Next_Assoc,
914 Next => Next_Assoc,
915 Key => Entity_Id,
916 Get_Key => Get_Gen_Id,
917 Hash => Hash,
918 Equal => "=");
919
920 Exchanged_Views : Elist_Id;
921 -- This list holds the private views that have been exchanged during
922 -- instantiation to restore the visibility of the generic declaration.
923 -- (see comments above). After instantiation, the current visibility is
924 -- reestablished by means of a traversal of this list.
925
926 Hidden_Entities : Elist_Id;
927 -- This list holds the entities of the current scope that are removed
928 -- from immediate visibility when instantiating a child unit. Their
929 -- visibility is restored in Remove_Parent.
930
931 -- Because instantiations can be recursive, the following must be saved
932 -- on entry and restored on exit from an instantiation (spec or body).
933 -- This is done by the two procedures Save_Env and Restore_Env. For
934 -- package and subprogram instantiations (but not for the body instances)
935 -- the action of Save_Env is done in two steps: Init_Env is called before
936 -- Check_Generic_Child_Unit, because setting the parent instances requires
937 -- that the visibility data structures be properly initialized. Once the
938 -- generic is unit is validated, Set_Instance_Env completes Save_Env.
939
940 Parent_Unit_Visible : Boolean := False;
941 -- Parent_Unit_Visible is used when the generic is a child unit, and
942 -- indicates whether the ultimate parent of the generic is visible in the
943 -- instantiation environment. It is used to reset the visibility of the
944 -- parent at the end of the instantiation (see Remove_Parent).
945
946 Instance_Parent_Unit : Entity_Id := Empty;
947 -- This records the ultimate parent unit of an instance of a generic
948 -- child unit and is used in conjunction with Parent_Unit_Visible to
949 -- indicate the unit to which the Parent_Unit_Visible flag corresponds.
950
951 type Instance_Env is record
952 Instantiated_Parent : Assoc;
953 Exchanged_Views : Elist_Id;
954 Hidden_Entities : Elist_Id;
955 Current_Sem_Unit : Unit_Number_Type;
956 Parent_Unit_Visible : Boolean := False;
957 Instance_Parent_Unit : Entity_Id := Empty;
958 Switches : Config_Switches_Type;
959 end record;
960
961 package Instance_Envs is new Table.Table (
962 Table_Component_Type => Instance_Env,
963 Table_Index_Type => Int,
964 Table_Low_Bound => 0,
965 Table_Initial => 32,
966 Table_Increment => 100,
967 Table_Name => "Instance_Envs");
968
969 procedure Restore_Private_Views
970 (Pack_Id : Entity_Id;
971 Is_Package : Boolean := True);
972 -- Restore the private views of external types, and unmark the generic
973 -- renamings of actuals, so that they become compatible subtypes again.
974 -- For subprograms, Pack_Id is the package constructed to hold the
975 -- renamings.
976
977 procedure Switch_View (T : Entity_Id);
978 -- Switch the partial and full views of a type and its private
979 -- dependents (i.e. its subtypes and derived types).
980
981 ------------------------------------
982 -- Structures for Error Reporting --
983 ------------------------------------
984
985 Instantiation_Node : Node_Id;
986 -- Used by subprograms that validate instantiation of formal parameters
987 -- where there might be no actual on which to place the error message.
988 -- Also used to locate the instantiation node for generic subunits.
989
990 Instantiation_Error : exception;
991 -- When there is a semantic error in the generic parameter matching,
992 -- there is no point in continuing the instantiation, because the
993 -- number of cascaded errors is unpredictable. This exception aborts
994 -- the instantiation process altogether.
995
996 S_Adjustment : Sloc_Adjustment;
997 -- Offset created for each node in an instantiation, in order to keep
998 -- track of the source position of the instantiation in each of its nodes.
999 -- A subsequent semantic error or warning on a construct of the instance
1000 -- points to both places: the original generic node, and the point of
1001 -- instantiation. See Sinput and Sinput.L for additional details.
1002
1003 ------------------------------------------------------------
1004 -- Data structure for keeping track when inside a Generic --
1005 ------------------------------------------------------------
1006
1007 -- The following table is used to save values of the Inside_A_Generic
1008 -- flag (see spec of Sem) when they are saved by Start_Generic.
1009
1010 package Generic_Flags is new Table.Table (
1011 Table_Component_Type => Boolean,
1012 Table_Index_Type => Int,
1013 Table_Low_Bound => 0,
1014 Table_Initial => 32,
1015 Table_Increment => 200,
1016 Table_Name => "Generic_Flags");
1017
1018 ---------------------------
1019 -- Abandon_Instantiation --
1020 ---------------------------
1021
1022 procedure Abandon_Instantiation (N : Node_Id) is
1023 begin
1024 Error_Msg_N ("\instantiation abandoned!", N);
1025 raise Instantiation_Error;
1026 end Abandon_Instantiation;
1027
1028 --------------------------------
1029 -- Add_Pending_Instantiation --
1030 --------------------------------
1031
1032 procedure Add_Pending_Instantiation (Inst : Node_Id; Act_Decl : Node_Id) is
1033 begin
1034
1035 -- Add to the instantiation node and the corresponding unit declaration
1036 -- the current values of global flags to be used when analyzing the
1037 -- instance body.
1038
1039 Pending_Instantiations.Append
1040 ((Inst_Node => Inst,
1041 Act_Decl => Act_Decl,
1042 Expander_Status => Expander_Active,
1043 Current_Sem_Unit => Current_Sem_Unit,
1044 Scope_Suppress => Scope_Suppress,
1045 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
1046 Version => Ada_Version,
1047 Version_Pragma => Ada_Version_Pragma,
1048 Warnings => Save_Warnings,
1049 SPARK_Mode => SPARK_Mode,
1050 SPARK_Mode_Pragma => SPARK_Mode_Pragma));
1051 end Add_Pending_Instantiation;
1052
1053 ----------------------------------
1054 -- Adjust_Inherited_Pragma_Sloc --
1055 ----------------------------------
1056
1057 procedure Adjust_Inherited_Pragma_Sloc (N : Node_Id) is
1058 begin
1059 Adjust_Instantiation_Sloc (N, S_Adjustment);
1060 end Adjust_Inherited_Pragma_Sloc;
1061
1062 --------------------------
1063 -- Analyze_Associations --
1064 --------------------------
1065
1066 function Analyze_Associations
1067 (I_Node : Node_Id;
1068 Formals : List_Id;
1069 F_Copy : List_Id) return List_Id
1070 is
1071 Actuals_To_Freeze : constant Elist_Id := New_Elmt_List;
1072 Assoc_List : constant List_Id := New_List;
1073 Default_Actuals : constant List_Id := New_List;
1074 Gen_Unit : constant Entity_Id :=
1075 Defining_Entity (Parent (F_Copy));
1076
1077 Actuals : List_Id;
1078 Actual : Node_Id;
1079 Analyzed_Formal : Node_Id;
1080 First_Named : Node_Id := Empty;
1081 Formal : Node_Id;
1082 Match : Node_Id;
1083 Named : Node_Id;
1084 Saved_Formal : Node_Id;
1085
1086 Default_Formals : constant List_Id := New_List;
1087 -- If an Others_Choice is present, some of the formals may be defaulted.
1088 -- To simplify the treatment of visibility in an instance, we introduce
1089 -- individual defaults for each such formal. These defaults are
1090 -- appended to the list of associations and replace the Others_Choice.
1091
1092 Found_Assoc : Node_Id;
1093 -- Association for the current formal being match. Empty if there are
1094 -- no remaining actuals, or if there is no named association with the
1095 -- name of the formal.
1096
1097 Is_Named_Assoc : Boolean;
1098 Num_Matched : Nat := 0;
1099 Num_Actuals : Nat := 0;
1100
1101 Others_Present : Boolean := False;
1102 Others_Choice : Node_Id := Empty;
1103 -- In Ada 2005, indicates partial parameterization of a formal
1104 -- package. As usual an other association must be last in the list.
1105
1106 procedure Check_Fixed_Point_Actual (Actual : Node_Id);
1107 -- Warn if an actual fixed-point type has user-defined arithmetic
1108 -- operations, but there is no corresponding formal in the generic,
1109 -- in which case the predefined operations will be used. This merits
1110 -- a warning because of the special semantics of fixed point ops.
1111
1112 procedure Check_Overloaded_Formal_Subprogram (Formal : Entity_Id);
1113 -- Apply RM 12.3(9): if a formal subprogram is overloaded, the instance
1114 -- cannot have a named association for it. AI05-0025 extends this rule
1115 -- to formals of formal packages by AI05-0025, and it also applies to
1116 -- box-initialized formals.
1117
1118 function Has_Fully_Defined_Profile (Subp : Entity_Id) return Boolean;
1119 -- Determine whether the parameter types and the return type of Subp
1120 -- are fully defined at the point of instantiation.
1121
1122 function Matching_Actual
1123 (F : Entity_Id;
1124 A_F : Entity_Id) return Node_Id;
1125 -- Find actual that corresponds to a given a formal parameter. If the
1126 -- actuals are positional, return the next one, if any. If the actuals
1127 -- are named, scan the parameter associations to find the right one.
1128 -- A_F is the corresponding entity in the analyzed generic, which is
1129 -- placed on the selector name for ASIS use.
1130 --
1131 -- In Ada 2005, a named association may be given with a box, in which
1132 -- case Matching_Actual sets Found_Assoc to the generic association,
1133 -- but return Empty for the actual itself. In this case the code below
1134 -- creates a corresponding declaration for the formal.
1135
1136 function Partial_Parameterization return Boolean;
1137 -- Ada 2005: if no match is found for a given formal, check if the
1138 -- association for it includes a box, or whether the associations
1139 -- include an Others clause.
1140
1141 procedure Process_Default (F : Entity_Id);
1142 -- Add a copy of the declaration of generic formal F to the list of
1143 -- associations, and add an explicit box association for F if there
1144 -- is none yet, and the default comes from an Others_Choice.
1145
1146 function Renames_Standard_Subprogram (Subp : Entity_Id) return Boolean;
1147 -- Determine whether Subp renames one of the subprograms defined in the
1148 -- generated package Standard.
1149
1150 procedure Set_Analyzed_Formal;
1151 -- Find the node in the generic copy that corresponds to a given formal.
1152 -- The semantic information on this node is used to perform legality
1153 -- checks on the actuals. Because semantic analysis can introduce some
1154 -- anonymous entities or modify the declaration node itself, the
1155 -- correspondence between the two lists is not one-one. In addition to
1156 -- anonymous types, the presence a formal equality will introduce an
1157 -- implicit declaration for the corresponding inequality.
1158
1159 ----------------------------------------
1160 -- Check_Overloaded_Formal_Subprogram --
1161 ----------------------------------------
1162
1163 procedure Check_Overloaded_Formal_Subprogram (Formal : Entity_Id) is
1164 Temp_Formal : Entity_Id;
1165
1166 begin
1167 Temp_Formal := First (Formals);
1168 while Present (Temp_Formal) loop
1169 if Nkind (Temp_Formal) in N_Formal_Subprogram_Declaration
1170 and then Temp_Formal /= Formal
1171 and then
1172 Chars (Defining_Unit_Name (Specification (Formal))) =
1173 Chars (Defining_Unit_Name (Specification (Temp_Formal)))
1174 then
1175 if Present (Found_Assoc) then
1176 Error_Msg_N
1177 ("named association not allowed for overloaded formal",
1178 Found_Assoc);
1179
1180 else
1181 Error_Msg_N
1182 ("named association not allowed for overloaded formal",
1183 Others_Choice);
1184 end if;
1185
1186 Abandon_Instantiation (Instantiation_Node);
1187 end if;
1188
1189 Next (Temp_Formal);
1190 end loop;
1191 end Check_Overloaded_Formal_Subprogram;
1192
1193 -------------------------------
1194 -- Check_Fixed_Point_Actual --
1195 -------------------------------
1196
1197 procedure Check_Fixed_Point_Actual (Actual : Node_Id) is
1198 Typ : constant Entity_Id := Entity (Actual);
1199 Prims : constant Elist_Id := Collect_Primitive_Operations (Typ);
1200 Elem : Elmt_Id;
1201 Formal : Node_Id;
1202 Op : Entity_Id;
1203
1204 begin
1205 -- Locate primitive operations of the type that are arithmetic
1206 -- operations.
1207
1208 Elem := First_Elmt (Prims);
1209 while Present (Elem) loop
1210 if Nkind (Node (Elem)) = N_Defining_Operator_Symbol then
1211
1212 -- Check whether the generic unit has a formal subprogram of
1213 -- the same name. This does not check types but is good enough
1214 -- to justify a warning.
1215
1216 Formal := First_Non_Pragma (Formals);
1217 Op := Alias (Node (Elem));
1218
1219 while Present (Formal) loop
1220 if Nkind (Formal) = N_Formal_Concrete_Subprogram_Declaration
1221 and then Chars (Defining_Entity (Formal)) =
1222 Chars (Node (Elem))
1223 then
1224 exit;
1225
1226 elsif Nkind (Formal) = N_Formal_Package_Declaration then
1227 declare
1228 Assoc : Node_Id;
1229 Ent : Entity_Id;
1230
1231 begin
1232 -- Locate corresponding actual, and check whether it
1233 -- includes a fixed-point type.
1234
1235 Assoc := First (Assoc_List);
1236 while Present (Assoc) loop
1237 exit when
1238 Nkind (Assoc) = N_Package_Renaming_Declaration
1239 and then Chars (Defining_Unit_Name (Assoc)) =
1240 Chars (Defining_Identifier (Formal));
1241
1242 Next (Assoc);
1243 end loop;
1244
1245 if Present (Assoc) then
1246
1247 -- If formal package declares a fixed-point type,
1248 -- and the user-defined operator is derived from
1249 -- a generic instance package, the fixed-point type
1250 -- does not use the corresponding predefined op.
1251
1252 Ent := First_Entity (Entity (Name (Assoc)));
1253 while Present (Ent) loop
1254 if Is_Fixed_Point_Type (Ent)
1255 and then Present (Op)
1256 and then Is_Generic_Instance (Scope (Op))
1257 then
1258 return;
1259 end if;
1260
1261 Next_Entity (Ent);
1262 end loop;
1263 end if;
1264 end;
1265 end if;
1266
1267 Next (Formal);
1268 end loop;
1269
1270 if No (Formal) then
1271 Error_Msg_Sloc := Sloc (Node (Elem));
1272 Error_Msg_NE
1273 ("?instance uses predefined operation, not primitive "
1274 & "operation&#", Actual, Node (Elem));
1275 end if;
1276 end if;
1277
1278 Next_Elmt (Elem);
1279 end loop;
1280 end Check_Fixed_Point_Actual;
1281
1282 -------------------------------
1283 -- Has_Fully_Defined_Profile --
1284 -------------------------------
1285
1286 function Has_Fully_Defined_Profile (Subp : Entity_Id) return Boolean is
1287 function Is_Fully_Defined_Type (Typ : Entity_Id) return Boolean;
1288 -- Determine whethet type Typ is fully defined
1289
1290 ---------------------------
1291 -- Is_Fully_Defined_Type --
1292 ---------------------------
1293
1294 function Is_Fully_Defined_Type (Typ : Entity_Id) return Boolean is
1295 begin
1296 -- A private type without a full view is not fully defined
1297
1298 if Is_Private_Type (Typ)
1299 and then No (Full_View (Typ))
1300 then
1301 return False;
1302
1303 -- An incomplete type is never fully defined
1304
1305 elsif Is_Incomplete_Type (Typ) then
1306 return False;
1307
1308 -- All other types are fully defined
1309
1310 else
1311 return True;
1312 end if;
1313 end Is_Fully_Defined_Type;
1314
1315 -- Local declarations
1316
1317 Param : Entity_Id;
1318
1319 -- Start of processing for Has_Fully_Defined_Profile
1320
1321 begin
1322 -- Check the parameters
1323
1324 Param := First_Formal (Subp);
1325 while Present (Param) loop
1326 if not Is_Fully_Defined_Type (Etype (Param)) then
1327 return False;
1328 end if;
1329
1330 Next_Formal (Param);
1331 end loop;
1332
1333 -- Check the return type
1334
1335 return Is_Fully_Defined_Type (Etype (Subp));
1336 end Has_Fully_Defined_Profile;
1337
1338 ---------------------
1339 -- Matching_Actual --
1340 ---------------------
1341
1342 function Matching_Actual
1343 (F : Entity_Id;
1344 A_F : Entity_Id) return Node_Id
1345 is
1346 Prev : Node_Id;
1347 Act : Node_Id;
1348
1349 begin
1350 Is_Named_Assoc := False;
1351
1352 -- End of list of purely positional parameters
1353
1354 if No (Actual) or else Nkind (Actual) = N_Others_Choice then
1355 Found_Assoc := Empty;
1356 Act := Empty;
1357
1358 -- Case of positional parameter corresponding to current formal
1359
1360 elsif No (Selector_Name (Actual)) then
1361 Found_Assoc := Actual;
1362 Act := Explicit_Generic_Actual_Parameter (Actual);
1363 Num_Matched := Num_Matched + 1;
1364 Next (Actual);
1365
1366 -- Otherwise scan list of named actuals to find the one with the
1367 -- desired name. All remaining actuals have explicit names.
1368
1369 else
1370 Is_Named_Assoc := True;
1371 Found_Assoc := Empty;
1372 Act := Empty;
1373 Prev := Empty;
1374
1375 while Present (Actual) loop
1376 if Nkind (Actual) = N_Others_Choice then
1377 Found_Assoc := Empty;
1378 Act := Empty;
1379
1380 elsif Chars (Selector_Name (Actual)) = Chars (F) then
1381 Set_Entity (Selector_Name (Actual), A_F);
1382 Set_Etype (Selector_Name (Actual), Etype (A_F));
1383 Generate_Reference (A_F, Selector_Name (Actual));
1384
1385 Found_Assoc := Actual;
1386 Act := Explicit_Generic_Actual_Parameter (Actual);
1387 Num_Matched := Num_Matched + 1;
1388 exit;
1389 end if;
1390
1391 Prev := Actual;
1392 Next (Actual);
1393 end loop;
1394
1395 -- Reset for subsequent searches. In most cases the named
1396 -- associations are in order. If they are not, we reorder them
1397 -- to avoid scanning twice the same actual. This is not just a
1398 -- question of efficiency: there may be multiple defaults with
1399 -- boxes that have the same name. In a nested instantiation we
1400 -- insert actuals for those defaults, and cannot rely on their
1401 -- names to disambiguate them.
1402
1403 if Actual = First_Named then
1404 Next (First_Named);
1405
1406 elsif Present (Actual) then
1407 Insert_Before (First_Named, Remove_Next (Prev));
1408 end if;
1409
1410 Actual := First_Named;
1411 end if;
1412
1413 if Is_Entity_Name (Act) and then Present (Entity (Act)) then
1414 Set_Used_As_Generic_Actual (Entity (Act));
1415 end if;
1416
1417 return Act;
1418 end Matching_Actual;
1419
1420 ------------------------------
1421 -- Partial_Parameterization --
1422 ------------------------------
1423
1424 function Partial_Parameterization return Boolean is
1425 begin
1426 return Others_Present
1427 or else (Present (Found_Assoc) and then Box_Present (Found_Assoc));
1428 end Partial_Parameterization;
1429
1430 ---------------------
1431 -- Process_Default --
1432 ---------------------
1433
1434 procedure Process_Default (F : Entity_Id) is
1435 Loc : constant Source_Ptr := Sloc (I_Node);
1436 F_Id : constant Entity_Id := Defining_Entity (F);
1437 Decl : Node_Id;
1438 Default : Node_Id;
1439 Id : Entity_Id;
1440
1441 begin
1442 -- Append copy of formal declaration to associations, and create new
1443 -- defining identifier for it.
1444
1445 Decl := New_Copy_Tree (F);
1446 Id := Make_Defining_Identifier (Sloc (F_Id), Chars (F_Id));
1447
1448 if Nkind (F) in N_Formal_Subprogram_Declaration then
1449 Set_Defining_Unit_Name (Specification (Decl), Id);
1450
1451 else
1452 Set_Defining_Identifier (Decl, Id);
1453 end if;
1454
1455 Append (Decl, Assoc_List);
1456
1457 if No (Found_Assoc) then
1458 Default :=
1459 Make_Generic_Association (Loc,
1460 Selector_Name =>
1461 New_Occurrence_Of (Id, Loc),
1462 Explicit_Generic_Actual_Parameter => Empty);
1463 Set_Box_Present (Default);
1464 Append (Default, Default_Formals);
1465 end if;
1466 end Process_Default;
1467
1468 ---------------------------------
1469 -- Renames_Standard_Subprogram --
1470 ---------------------------------
1471
1472 function Renames_Standard_Subprogram (Subp : Entity_Id) return Boolean is
1473 Id : Entity_Id;
1474
1475 begin
1476 Id := Alias (Subp);
1477 while Present (Id) loop
1478 if Scope (Id) = Standard_Standard then
1479 return True;
1480 end if;
1481
1482 Id := Alias (Id);
1483 end loop;
1484
1485 return False;
1486 end Renames_Standard_Subprogram;
1487
1488 -------------------------
1489 -- Set_Analyzed_Formal --
1490 -------------------------
1491
1492 procedure Set_Analyzed_Formal is
1493 Kind : Node_Kind;
1494
1495 begin
1496 while Present (Analyzed_Formal) loop
1497 Kind := Nkind (Analyzed_Formal);
1498
1499 case Nkind (Formal) is
1500 when N_Formal_Subprogram_Declaration =>
1501 exit when Kind in N_Formal_Subprogram_Declaration
1502 and then
1503 Chars
1504 (Defining_Unit_Name (Specification (Formal))) =
1505 Chars
1506 (Defining_Unit_Name (Specification (Analyzed_Formal)));
1507
1508 when N_Formal_Package_Declaration =>
1509 exit when Nkind_In (Kind, N_Formal_Package_Declaration,
1510 N_Generic_Package_Declaration,
1511 N_Package_Declaration);
1512
1513 when N_Use_Package_Clause
1514 | N_Use_Type_Clause
1515 =>
1516 exit;
1517
1518 when others =>
1519
1520 -- Skip freeze nodes, and nodes inserted to replace
1521 -- unrecognized pragmas.
1522
1523 exit when
1524 Kind not in N_Formal_Subprogram_Declaration
1525 and then not Nkind_In (Kind, N_Subprogram_Declaration,
1526 N_Freeze_Entity,
1527 N_Null_Statement,
1528 N_Itype_Reference)
1529 and then Chars (Defining_Identifier (Formal)) =
1530 Chars (Defining_Identifier (Analyzed_Formal));
1531 end case;
1532
1533 Next (Analyzed_Formal);
1534 end loop;
1535 end Set_Analyzed_Formal;
1536
1537 -- Start of processing for Analyze_Associations
1538
1539 begin
1540 Actuals := Generic_Associations (I_Node);
1541
1542 if Present (Actuals) then
1543
1544 -- Check for an Others choice, indicating a partial parameterization
1545 -- for a formal package.
1546
1547 Actual := First (Actuals);
1548 while Present (Actual) loop
1549 if Nkind (Actual) = N_Others_Choice then
1550 Others_Present := True;
1551 Others_Choice := Actual;
1552
1553 if Present (Next (Actual)) then
1554 Error_Msg_N ("others must be last association", Actual);
1555 end if;
1556
1557 -- This subprogram is used both for formal packages and for
1558 -- instantiations. For the latter, associations must all be
1559 -- explicit.
1560
1561 if Nkind (I_Node) /= N_Formal_Package_Declaration
1562 and then Comes_From_Source (I_Node)
1563 then
1564 Error_Msg_N
1565 ("others association not allowed in an instance",
1566 Actual);
1567 end if;
1568
1569 -- In any case, nothing to do after the others association
1570
1571 exit;
1572
1573 elsif Box_Present (Actual)
1574 and then Comes_From_Source (I_Node)
1575 and then Nkind (I_Node) /= N_Formal_Package_Declaration
1576 then
1577 Error_Msg_N
1578 ("box association not allowed in an instance", Actual);
1579 end if;
1580
1581 Next (Actual);
1582 end loop;
1583
1584 -- If named associations are present, save first named association
1585 -- (it may of course be Empty) to facilitate subsequent name search.
1586
1587 First_Named := First (Actuals);
1588 while Present (First_Named)
1589 and then Nkind (First_Named) /= N_Others_Choice
1590 and then No (Selector_Name (First_Named))
1591 loop
1592 Num_Actuals := Num_Actuals + 1;
1593 Next (First_Named);
1594 end loop;
1595 end if;
1596
1597 Named := First_Named;
1598 while Present (Named) loop
1599 if Nkind (Named) /= N_Others_Choice
1600 and then No (Selector_Name (Named))
1601 then
1602 Error_Msg_N ("invalid positional actual after named one", Named);
1603 Abandon_Instantiation (Named);
1604 end if;
1605
1606 -- A named association may lack an actual parameter, if it was
1607 -- introduced for a default subprogram that turns out to be local
1608 -- to the outer instantiation. If it has a box association it must
1609 -- correspond to some formal in the generic.
1610
1611 if Nkind (Named) /= N_Others_Choice
1612 and then (Present (Explicit_Generic_Actual_Parameter (Named))
1613 or else Box_Present (Named))
1614 then
1615 Num_Actuals := Num_Actuals + 1;
1616 end if;
1617
1618 Next (Named);
1619 end loop;
1620
1621 if Present (Formals) then
1622 Formal := First_Non_Pragma (Formals);
1623 Analyzed_Formal := First_Non_Pragma (F_Copy);
1624
1625 if Present (Actuals) then
1626 Actual := First (Actuals);
1627
1628 -- All formals should have default values
1629
1630 else
1631 Actual := Empty;
1632 end if;
1633
1634 while Present (Formal) loop
1635 Set_Analyzed_Formal;
1636 Saved_Formal := Next_Non_Pragma (Formal);
1637
1638 case Nkind (Formal) is
1639 when N_Formal_Object_Declaration =>
1640 Match :=
1641 Matching_Actual
1642 (Defining_Identifier (Formal),
1643 Defining_Identifier (Analyzed_Formal));
1644
1645 if No (Match) and then Partial_Parameterization then
1646 Process_Default (Formal);
1647
1648 else
1649 Append_List
1650 (Instantiate_Object (Formal, Match, Analyzed_Formal),
1651 Assoc_List);
1652
1653 -- For a defaulted in_parameter, create an entry in the
1654 -- the list of defaulted actuals, for GNATProve use. Do
1655 -- not included these defaults for an instance nested
1656 -- within a generic, because the defaults are also used
1657 -- in the analysis of the enclosing generic, and only
1658 -- defaulted subprograms are relevant there.
1659
1660 if No (Match) and then not Inside_A_Generic then
1661 Append_To (Default_Actuals,
1662 Make_Generic_Association (Sloc (I_Node),
1663 Selector_Name =>
1664 New_Occurrence_Of
1665 (Defining_Identifier (Formal), Sloc (I_Node)),
1666 Explicit_Generic_Actual_Parameter =>
1667 New_Copy_Tree (Default_Expression (Formal))));
1668 end if;
1669 end if;
1670
1671 -- If the object is a call to an expression function, this
1672 -- is a freezing point for it.
1673
1674 if Is_Entity_Name (Match)
1675 and then Present (Entity (Match))
1676 and then Nkind
1677 (Original_Node (Unit_Declaration_Node (Entity (Match))))
1678 = N_Expression_Function
1679 then
1680 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1681 end if;
1682
1683 when N_Formal_Type_Declaration =>
1684 Match :=
1685 Matching_Actual
1686 (Defining_Identifier (Formal),
1687 Defining_Identifier (Analyzed_Formal));
1688
1689 if No (Match) then
1690 if Partial_Parameterization then
1691 Process_Default (Formal);
1692
1693 else
1694 Error_Msg_Sloc := Sloc (Gen_Unit);
1695 Error_Msg_NE
1696 ("missing actual&",
1697 Instantiation_Node, Defining_Identifier (Formal));
1698 Error_Msg_NE
1699 ("\in instantiation of & declared#",
1700 Instantiation_Node, Gen_Unit);
1701 Abandon_Instantiation (Instantiation_Node);
1702 end if;
1703
1704 else
1705 Analyze (Match);
1706 Append_List
1707 (Instantiate_Type
1708 (Formal, Match, Analyzed_Formal, Assoc_List),
1709 Assoc_List);
1710
1711 -- Warn when an actual is a fixed-point with user-
1712 -- defined promitives. The warning is superfluous
1713 -- if the fornal is private, because there can be
1714 -- no arithmetic operations in the generic so there
1715 -- no danger of confusion.
1716
1717 if Is_Fixed_Point_Type (Entity (Match))
1718 and then not Is_Private_Type
1719 (Defining_Identifier (Analyzed_Formal))
1720 then
1721 Check_Fixed_Point_Actual (Match);
1722 end if;
1723
1724 -- An instantiation is a freeze point for the actuals,
1725 -- unless this is a rewritten formal package, or the
1726 -- formal is an Ada 2012 formal incomplete type.
1727
1728 if Nkind (I_Node) = N_Formal_Package_Declaration
1729 or else
1730 (Ada_Version >= Ada_2012
1731 and then
1732 Ekind (Defining_Identifier (Analyzed_Formal)) =
1733 E_Incomplete_Type)
1734 then
1735 null;
1736
1737 else
1738 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1739 end if;
1740 end if;
1741
1742 -- A remote access-to-class-wide type is not a legal actual
1743 -- for a generic formal of an access type (E.2.2(17/2)).
1744 -- In GNAT an exception to this rule is introduced when
1745 -- the formal is marked as remote using implementation
1746 -- defined aspect/pragma Remote_Access_Type. In that case
1747 -- the actual must be remote as well.
1748
1749 -- If the current instantiation is the construction of a
1750 -- local copy for a formal package the actuals may be
1751 -- defaulted, and there is no matching actual to check.
1752
1753 if Nkind (Analyzed_Formal) = N_Formal_Type_Declaration
1754 and then
1755 Nkind (Formal_Type_Definition (Analyzed_Formal)) =
1756 N_Access_To_Object_Definition
1757 and then Present (Match)
1758 then
1759 declare
1760 Formal_Ent : constant Entity_Id :=
1761 Defining_Identifier (Analyzed_Formal);
1762 begin
1763 if Is_Remote_Access_To_Class_Wide_Type (Entity (Match))
1764 = Is_Remote_Types (Formal_Ent)
1765 then
1766 -- Remoteness of formal and actual match
1767
1768 null;
1769
1770 elsif Is_Remote_Types (Formal_Ent) then
1771
1772 -- Remote formal, non-remote actual
1773
1774 Error_Msg_NE
1775 ("actual for& must be remote", Match, Formal_Ent);
1776
1777 else
1778 -- Non-remote formal, remote actual
1779
1780 Error_Msg_NE
1781 ("actual for& may not be remote",
1782 Match, Formal_Ent);
1783 end if;
1784 end;
1785 end if;
1786
1787 when N_Formal_Subprogram_Declaration =>
1788 Match :=
1789 Matching_Actual
1790 (Defining_Unit_Name (Specification (Formal)),
1791 Defining_Unit_Name (Specification (Analyzed_Formal)));
1792
1793 -- If the formal subprogram has the same name as another
1794 -- formal subprogram of the generic, then a named
1795 -- association is illegal (12.3(9)). Exclude named
1796 -- associations that are generated for a nested instance.
1797
1798 if Present (Match)
1799 and then Is_Named_Assoc
1800 and then Comes_From_Source (Found_Assoc)
1801 then
1802 Check_Overloaded_Formal_Subprogram (Formal);
1803 end if;
1804
1805 -- If there is no corresponding actual, this may be case
1806 -- of partial parameterization, or else the formal has a
1807 -- default or a box.
1808
1809 if No (Match) and then Partial_Parameterization then
1810 Process_Default (Formal);
1811
1812 if Nkind (I_Node) = N_Formal_Package_Declaration then
1813 Check_Overloaded_Formal_Subprogram (Formal);
1814 end if;
1815
1816 else
1817 Append_To (Assoc_List,
1818 Instantiate_Formal_Subprogram
1819 (Formal, Match, Analyzed_Formal));
1820
1821 -- An instantiation is a freeze point for the actuals,
1822 -- unless this is a rewritten formal package.
1823
1824 if Nkind (I_Node) /= N_Formal_Package_Declaration
1825 and then Nkind (Match) = N_Identifier
1826 and then Is_Subprogram (Entity (Match))
1827
1828 -- The actual subprogram may rename a routine defined
1829 -- in Standard. Avoid freezing such renamings because
1830 -- subprograms coming from Standard cannot be frozen.
1831
1832 and then
1833 not Renames_Standard_Subprogram (Entity (Match))
1834
1835 -- If the actual subprogram comes from a different
1836 -- unit, it is already frozen, either by a body in
1837 -- that unit or by the end of the declarative part
1838 -- of the unit. This check avoids the freezing of
1839 -- subprograms defined in Standard which are used
1840 -- as generic actuals.
1841
1842 and then In_Same_Code_Unit (Entity (Match), I_Node)
1843 and then Has_Fully_Defined_Profile (Entity (Match))
1844 then
1845 -- Mark the subprogram as having a delayed freeze
1846 -- since this may be an out-of-order action.
1847
1848 Set_Has_Delayed_Freeze (Entity (Match));
1849 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1850 end if;
1851 end if;
1852
1853 -- If this is a nested generic, preserve default for later
1854 -- instantiations. We do this as well for GNATProve use,
1855 -- so that the list of generic associations is complete.
1856
1857 if No (Match) and then Box_Present (Formal) then
1858 declare
1859 Subp : constant Entity_Id :=
1860 Defining_Unit_Name
1861 (Specification (Last (Assoc_List)));
1862
1863 begin
1864 Append_To (Default_Actuals,
1865 Make_Generic_Association (Sloc (I_Node),
1866 Selector_Name =>
1867 New_Occurrence_Of (Subp, Sloc (I_Node)),
1868 Explicit_Generic_Actual_Parameter =>
1869 New_Occurrence_Of (Subp, Sloc (I_Node))));
1870 end;
1871 end if;
1872
1873 when N_Formal_Package_Declaration =>
1874 Match :=
1875 Matching_Actual
1876 (Defining_Identifier (Formal),
1877 Defining_Identifier (Original_Node (Analyzed_Formal)));
1878
1879 if No (Match) then
1880 if Partial_Parameterization then
1881 Process_Default (Formal);
1882
1883 else
1884 Error_Msg_Sloc := Sloc (Gen_Unit);
1885 Error_Msg_NE
1886 ("missing actual&",
1887 Instantiation_Node, Defining_Identifier (Formal));
1888 Error_Msg_NE
1889 ("\in instantiation of & declared#",
1890 Instantiation_Node, Gen_Unit);
1891
1892 Abandon_Instantiation (Instantiation_Node);
1893 end if;
1894
1895 else
1896 Analyze (Match);
1897 Append_List
1898 (Instantiate_Formal_Package
1899 (Formal, Match, Analyzed_Formal),
1900 Assoc_List);
1901
1902 -- Determine whether the actual package needs an explicit
1903 -- freeze node. This is only the case if the actual is
1904 -- declared in the same unit and has a body. Normally
1905 -- packages do not have explicit freeze nodes, and gigi
1906 -- only uses them to elaborate entities in a package
1907 -- body.
1908
1909 Explicit_Freeze_Check : declare
1910 Actual : constant Entity_Id := Entity (Match);
1911 Gen_Par : Entity_Id;
1912
1913 Needs_Freezing : Boolean;
1914 S : Entity_Id;
1915
1916 procedure Check_Generic_Parent;
1917 -- The actual may be an instantiation of a unit
1918 -- declared in a previous instantiation. If that
1919 -- one is also in the current compilation, it must
1920 -- itself be frozen before the actual. The actual
1921 -- may be an instantiation of a generic child unit,
1922 -- in which case the same applies to the instance
1923 -- of the parent which must be frozen before the
1924 -- actual.
1925 -- Should this itself be recursive ???
1926
1927 --------------------------
1928 -- Check_Generic_Parent --
1929 --------------------------
1930
1931 procedure Check_Generic_Parent is
1932 Inst : constant Node_Id :=
1933 Next (Unit_Declaration_Node (Actual));
1934 Par : Entity_Id;
1935
1936 begin
1937 Par := Empty;
1938
1939 if Nkind (Parent (Actual)) = N_Package_Specification
1940 then
1941 Par := Scope (Generic_Parent (Parent (Actual)));
1942
1943 if Is_Generic_Instance (Par) then
1944 null;
1945
1946 -- If the actual is a child generic unit, check
1947 -- whether the instantiation of the parent is
1948 -- also local and must also be frozen now. We
1949 -- must retrieve the instance node to locate the
1950 -- parent instance if any.
1951
1952 elsif Ekind (Par) = E_Generic_Package
1953 and then Is_Child_Unit (Gen_Par)
1954 and then Ekind (Scope (Gen_Par)) =
1955 E_Generic_Package
1956 then
1957 if Nkind (Inst) = N_Package_Instantiation
1958 and then Nkind (Name (Inst)) =
1959 N_Expanded_Name
1960 then
1961 -- Retrieve entity of parent instance
1962
1963 Par := Entity (Prefix (Name (Inst)));
1964 end if;
1965
1966 else
1967 Par := Empty;
1968 end if;
1969 end if;
1970
1971 if Present (Par)
1972 and then Is_Generic_Instance (Par)
1973 and then Scope (Par) = Current_Scope
1974 and then
1975 (No (Freeze_Node (Par))
1976 or else
1977 not Is_List_Member (Freeze_Node (Par)))
1978 then
1979 Set_Has_Delayed_Freeze (Par);
1980 Append_Elmt (Par, Actuals_To_Freeze);
1981 end if;
1982 end Check_Generic_Parent;
1983
1984 -- Start of processing for Explicit_Freeze_Check
1985
1986 begin
1987 if Present (Renamed_Entity (Actual)) then
1988 Gen_Par :=
1989 Generic_Parent (Specification
1990 (Unit_Declaration_Node
1991 (Renamed_Entity (Actual))));
1992 else
1993 Gen_Par :=
1994 Generic_Parent (Specification
1995 (Unit_Declaration_Node (Actual)));
1996 end if;
1997
1998 if not Expander_Active
1999 or else not Has_Completion (Actual)
2000 or else not In_Same_Source_Unit (I_Node, Actual)
2001 or else Is_Frozen (Actual)
2002 or else
2003 (Present (Renamed_Entity (Actual))
2004 and then
2005 not In_Same_Source_Unit
2006 (I_Node, (Renamed_Entity (Actual))))
2007 then
2008 null;
2009
2010 else
2011 -- Finally we want to exclude such freeze nodes
2012 -- from statement sequences, which freeze
2013 -- everything before them.
2014 -- Is this strictly necessary ???
2015
2016 Needs_Freezing := True;
2017
2018 S := Current_Scope;
2019 while Present (S) loop
2020 if Ekind_In (S, E_Block,
2021 E_Function,
2022 E_Loop,
2023 E_Procedure)
2024 then
2025 Needs_Freezing := False;
2026 exit;
2027 end if;
2028
2029 S := Scope (S);
2030 end loop;
2031
2032 if Needs_Freezing then
2033 Check_Generic_Parent;
2034
2035 -- If the actual is a renaming of a proper
2036 -- instance of the formal package, indicate
2037 -- that it is the instance that must be frozen.
2038
2039 if Nkind (Parent (Actual)) =
2040 N_Package_Renaming_Declaration
2041 then
2042 Set_Has_Delayed_Freeze
2043 (Renamed_Entity (Actual));
2044 Append_Elmt
2045 (Renamed_Entity (Actual),
2046 Actuals_To_Freeze);
2047 else
2048 Set_Has_Delayed_Freeze (Actual);
2049 Append_Elmt (Actual, Actuals_To_Freeze);
2050 end if;
2051 end if;
2052 end if;
2053 end Explicit_Freeze_Check;
2054 end if;
2055
2056 -- For use type and use package appearing in the generic part,
2057 -- we have already copied them, so we can just move them where
2058 -- they belong (we mustn't recopy them since this would mess up
2059 -- the Sloc values).
2060
2061 when N_Use_Package_Clause
2062 | N_Use_Type_Clause
2063 =>
2064 if Nkind (Original_Node (I_Node)) =
2065 N_Formal_Package_Declaration
2066 then
2067 Append (New_Copy_Tree (Formal), Assoc_List);
2068 else
2069 Remove (Formal);
2070 Append (Formal, Assoc_List);
2071 end if;
2072
2073 when others =>
2074 raise Program_Error;
2075 end case;
2076
2077 Formal := Saved_Formal;
2078 Next_Non_Pragma (Analyzed_Formal);
2079 end loop;
2080
2081 if Num_Actuals > Num_Matched then
2082 Error_Msg_Sloc := Sloc (Gen_Unit);
2083
2084 if Present (Selector_Name (Actual)) then
2085 Error_Msg_NE
2086 ("unmatched actual &", Actual, Selector_Name (Actual));
2087 Error_Msg_NE
2088 ("\in instantiation of & declared#", Actual, Gen_Unit);
2089 else
2090 Error_Msg_NE
2091 ("unmatched actual in instantiation of & declared#",
2092 Actual, Gen_Unit);
2093 end if;
2094 end if;
2095
2096 elsif Present (Actuals) then
2097 Error_Msg_N
2098 ("too many actuals in generic instantiation", Instantiation_Node);
2099 end if;
2100
2101 -- An instantiation freezes all generic actuals. The only exceptions
2102 -- to this are incomplete types and subprograms which are not fully
2103 -- defined at the point of instantiation.
2104
2105 declare
2106 Elmt : Elmt_Id := First_Elmt (Actuals_To_Freeze);
2107 begin
2108 while Present (Elmt) loop
2109 Freeze_Before (I_Node, Node (Elmt));
2110 Next_Elmt (Elmt);
2111 end loop;
2112 end;
2113
2114 -- If there are default subprograms, normalize the tree by adding
2115 -- explicit associations for them. This is required if the instance
2116 -- appears within a generic.
2117
2118 if not Is_Empty_List (Default_Actuals) then
2119 declare
2120 Default : Node_Id;
2121
2122 begin
2123 Default := First (Default_Actuals);
2124 while Present (Default) loop
2125 Mark_Rewrite_Insertion (Default);
2126 Next (Default);
2127 end loop;
2128
2129 if No (Actuals) then
2130 Set_Generic_Associations (I_Node, Default_Actuals);
2131 else
2132 Append_List_To (Actuals, Default_Actuals);
2133 end if;
2134 end;
2135 end if;
2136
2137 -- If this is a formal package, normalize the parameter list by adding
2138 -- explicit box associations for the formals that are covered by an
2139 -- Others_Choice.
2140
2141 if not Is_Empty_List (Default_Formals) then
2142 Append_List (Default_Formals, Formals);
2143 end if;
2144
2145 return Assoc_List;
2146 end Analyze_Associations;
2147
2148 -------------------------------
2149 -- Analyze_Formal_Array_Type --
2150 -------------------------------
2151
2152 procedure Analyze_Formal_Array_Type
2153 (T : in out Entity_Id;
2154 Def : Node_Id)
2155 is
2156 DSS : Node_Id;
2157
2158 begin
2159 -- Treated like a non-generic array declaration, with additional
2160 -- semantic checks.
2161
2162 Enter_Name (T);
2163
2164 if Nkind (Def) = N_Constrained_Array_Definition then
2165 DSS := First (Discrete_Subtype_Definitions (Def));
2166 while Present (DSS) loop
2167 if Nkind_In (DSS, N_Subtype_Indication,
2168 N_Range,
2169 N_Attribute_Reference)
2170 then
2171 Error_Msg_N ("only a subtype mark is allowed in a formal", DSS);
2172 end if;
2173
2174 Next (DSS);
2175 end loop;
2176 end if;
2177
2178 Array_Type_Declaration (T, Def);
2179 Set_Is_Generic_Type (Base_Type (T));
2180
2181 if Ekind (Component_Type (T)) = E_Incomplete_Type
2182 and then No (Full_View (Component_Type (T)))
2183 then
2184 Error_Msg_N ("premature usage of incomplete type", Def);
2185
2186 -- Check that range constraint is not allowed on the component type
2187 -- of a generic formal array type (AARM 12.5.3(3))
2188
2189 elsif Is_Internal (Component_Type (T))
2190 and then Present (Subtype_Indication (Component_Definition (Def)))
2191 and then Nkind (Original_Node
2192 (Subtype_Indication (Component_Definition (Def)))) =
2193 N_Subtype_Indication
2194 then
2195 Error_Msg_N
2196 ("in a formal, a subtype indication can only be "
2197 & "a subtype mark (RM 12.5.3(3))",
2198 Subtype_Indication (Component_Definition (Def)));
2199 end if;
2200
2201 end Analyze_Formal_Array_Type;
2202
2203 ---------------------------------------------
2204 -- Analyze_Formal_Decimal_Fixed_Point_Type --
2205 ---------------------------------------------
2206
2207 -- As for other generic types, we create a valid type representation with
2208 -- legal but arbitrary attributes, whose values are never considered
2209 -- static. For all scalar types we introduce an anonymous base type, with
2210 -- the same attributes. We choose the corresponding integer type to be
2211 -- Standard_Integer.
2212 -- Here and in other similar routines, the Sloc of the generated internal
2213 -- type must be the same as the sloc of the defining identifier of the
2214 -- formal type declaration, to provide proper source navigation.
2215
2216 procedure Analyze_Formal_Decimal_Fixed_Point_Type
2217 (T : Entity_Id;
2218 Def : Node_Id)
2219 is
2220 Loc : constant Source_Ptr := Sloc (Def);
2221
2222 Base : constant Entity_Id :=
2223 New_Internal_Entity
2224 (E_Decimal_Fixed_Point_Type,
2225 Current_Scope,
2226 Sloc (Defining_Identifier (Parent (Def))), 'G');
2227
2228 Int_Base : constant Entity_Id := Standard_Integer;
2229 Delta_Val : constant Ureal := Ureal_1;
2230 Digs_Val : constant Uint := Uint_6;
2231
2232 function Make_Dummy_Bound return Node_Id;
2233 -- Return a properly typed universal real literal to use as a bound
2234
2235 ----------------------
2236 -- Make_Dummy_Bound --
2237 ----------------------
2238
2239 function Make_Dummy_Bound return Node_Id is
2240 Bound : constant Node_Id := Make_Real_Literal (Loc, Ureal_1);
2241 begin
2242 Set_Etype (Bound, Universal_Real);
2243 return Bound;
2244 end Make_Dummy_Bound;
2245
2246 -- Start of processing for Analyze_Formal_Decimal_Fixed_Point_Type
2247
2248 begin
2249 Enter_Name (T);
2250
2251 Set_Etype (Base, Base);
2252 Set_Size_Info (Base, Int_Base);
2253 Set_RM_Size (Base, RM_Size (Int_Base));
2254 Set_First_Rep_Item (Base, First_Rep_Item (Int_Base));
2255 Set_Digits_Value (Base, Digs_Val);
2256 Set_Delta_Value (Base, Delta_Val);
2257 Set_Small_Value (Base, Delta_Val);
2258 Set_Scalar_Range (Base,
2259 Make_Range (Loc,
2260 Low_Bound => Make_Dummy_Bound,
2261 High_Bound => Make_Dummy_Bound));
2262
2263 Set_Is_Generic_Type (Base);
2264 Set_Parent (Base, Parent (Def));
2265
2266 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
2267 Set_Etype (T, Base);
2268 Set_Size_Info (T, Int_Base);
2269 Set_RM_Size (T, RM_Size (Int_Base));
2270 Set_First_Rep_Item (T, First_Rep_Item (Int_Base));
2271 Set_Digits_Value (T, Digs_Val);
2272 Set_Delta_Value (T, Delta_Val);
2273 Set_Small_Value (T, Delta_Val);
2274 Set_Scalar_Range (T, Scalar_Range (Base));
2275 Set_Is_Constrained (T);
2276
2277 Check_Restriction (No_Fixed_Point, Def);
2278 end Analyze_Formal_Decimal_Fixed_Point_Type;
2279
2280 -------------------------------------------
2281 -- Analyze_Formal_Derived_Interface_Type --
2282 -------------------------------------------
2283
2284 procedure Analyze_Formal_Derived_Interface_Type
2285 (N : Node_Id;
2286 T : Entity_Id;
2287 Def : Node_Id)
2288 is
2289 Loc : constant Source_Ptr := Sloc (Def);
2290
2291 begin
2292 -- Rewrite as a type declaration of a derived type. This ensures that
2293 -- the interface list and primitive operations are properly captured.
2294
2295 Rewrite (N,
2296 Make_Full_Type_Declaration (Loc,
2297 Defining_Identifier => T,
2298 Type_Definition => Def));
2299 Analyze (N);
2300 Set_Is_Generic_Type (T);
2301 end Analyze_Formal_Derived_Interface_Type;
2302
2303 ---------------------------------
2304 -- Analyze_Formal_Derived_Type --
2305 ---------------------------------
2306
2307 procedure Analyze_Formal_Derived_Type
2308 (N : Node_Id;
2309 T : Entity_Id;
2310 Def : Node_Id)
2311 is
2312 Loc : constant Source_Ptr := Sloc (Def);
2313 Unk_Disc : constant Boolean := Unknown_Discriminants_Present (N);
2314 New_N : Node_Id;
2315
2316 begin
2317 Set_Is_Generic_Type (T);
2318
2319 if Private_Present (Def) then
2320 New_N :=
2321 Make_Private_Extension_Declaration (Loc,
2322 Defining_Identifier => T,
2323 Discriminant_Specifications => Discriminant_Specifications (N),
2324 Unknown_Discriminants_Present => Unk_Disc,
2325 Subtype_Indication => Subtype_Mark (Def),
2326 Interface_List => Interface_List (Def));
2327
2328 Set_Abstract_Present (New_N, Abstract_Present (Def));
2329 Set_Limited_Present (New_N, Limited_Present (Def));
2330 Set_Synchronized_Present (New_N, Synchronized_Present (Def));
2331
2332 else
2333 New_N :=
2334 Make_Full_Type_Declaration (Loc,
2335 Defining_Identifier => T,
2336 Discriminant_Specifications =>
2337 Discriminant_Specifications (Parent (T)),
2338 Type_Definition =>
2339 Make_Derived_Type_Definition (Loc,
2340 Subtype_Indication => Subtype_Mark (Def)));
2341
2342 Set_Abstract_Present
2343 (Type_Definition (New_N), Abstract_Present (Def));
2344 Set_Limited_Present
2345 (Type_Definition (New_N), Limited_Present (Def));
2346 end if;
2347
2348 Rewrite (N, New_N);
2349 Analyze (N);
2350
2351 if Unk_Disc then
2352 if not Is_Composite_Type (T) then
2353 Error_Msg_N
2354 ("unknown discriminants not allowed for elementary types", N);
2355 else
2356 Set_Has_Unknown_Discriminants (T);
2357 Set_Is_Constrained (T, False);
2358 end if;
2359 end if;
2360
2361 -- If the parent type has a known size, so does the formal, which makes
2362 -- legal representation clauses that involve the formal.
2363
2364 Set_Size_Known_At_Compile_Time
2365 (T, Size_Known_At_Compile_Time (Entity (Subtype_Mark (Def))));
2366 end Analyze_Formal_Derived_Type;
2367
2368 ----------------------------------
2369 -- Analyze_Formal_Discrete_Type --
2370 ----------------------------------
2371
2372 -- The operations defined for a discrete types are those of an enumeration
2373 -- type. The size is set to an arbitrary value, for use in analyzing the
2374 -- generic unit.
2375
2376 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id) is
2377 Loc : constant Source_Ptr := Sloc (Def);
2378 Lo : Node_Id;
2379 Hi : Node_Id;
2380
2381 Base : constant Entity_Id :=
2382 New_Internal_Entity
2383 (E_Floating_Point_Type, Current_Scope,
2384 Sloc (Defining_Identifier (Parent (Def))), 'G');
2385
2386 begin
2387 Enter_Name (T);
2388 Set_Ekind (T, E_Enumeration_Subtype);
2389 Set_Etype (T, Base);
2390 Init_Size (T, 8);
2391 Init_Alignment (T);
2392 Set_Is_Generic_Type (T);
2393 Set_Is_Constrained (T);
2394
2395 -- For semantic analysis, the bounds of the type must be set to some
2396 -- non-static value. The simplest is to create attribute nodes for those
2397 -- bounds, that refer to the type itself. These bounds are never
2398 -- analyzed but serve as place-holders.
2399
2400 Lo :=
2401 Make_Attribute_Reference (Loc,
2402 Attribute_Name => Name_First,
2403 Prefix => New_Occurrence_Of (T, Loc));
2404 Set_Etype (Lo, T);
2405
2406 Hi :=
2407 Make_Attribute_Reference (Loc,
2408 Attribute_Name => Name_Last,
2409 Prefix => New_Occurrence_Of (T, Loc));
2410 Set_Etype (Hi, T);
2411
2412 Set_Scalar_Range (T,
2413 Make_Range (Loc,
2414 Low_Bound => Lo,
2415 High_Bound => Hi));
2416
2417 Set_Ekind (Base, E_Enumeration_Type);
2418 Set_Etype (Base, Base);
2419 Init_Size (Base, 8);
2420 Init_Alignment (Base);
2421 Set_Is_Generic_Type (Base);
2422 Set_Scalar_Range (Base, Scalar_Range (T));
2423 Set_Parent (Base, Parent (Def));
2424 end Analyze_Formal_Discrete_Type;
2425
2426 ----------------------------------
2427 -- Analyze_Formal_Floating_Type --
2428 ---------------------------------
2429
2430 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id) is
2431 Base : constant Entity_Id :=
2432 New_Internal_Entity
2433 (E_Floating_Point_Type, Current_Scope,
2434 Sloc (Defining_Identifier (Parent (Def))), 'G');
2435
2436 begin
2437 -- The various semantic attributes are taken from the predefined type
2438 -- Float, just so that all of them are initialized. Their values are
2439 -- never used because no constant folding or expansion takes place in
2440 -- the generic itself.
2441
2442 Enter_Name (T);
2443 Set_Ekind (T, E_Floating_Point_Subtype);
2444 Set_Etype (T, Base);
2445 Set_Size_Info (T, (Standard_Float));
2446 Set_RM_Size (T, RM_Size (Standard_Float));
2447 Set_Digits_Value (T, Digits_Value (Standard_Float));
2448 Set_Scalar_Range (T, Scalar_Range (Standard_Float));
2449 Set_Is_Constrained (T);
2450
2451 Set_Is_Generic_Type (Base);
2452 Set_Etype (Base, Base);
2453 Set_Size_Info (Base, (Standard_Float));
2454 Set_RM_Size (Base, RM_Size (Standard_Float));
2455 Set_Digits_Value (Base, Digits_Value (Standard_Float));
2456 Set_Scalar_Range (Base, Scalar_Range (Standard_Float));
2457 Set_Parent (Base, Parent (Def));
2458
2459 Check_Restriction (No_Floating_Point, Def);
2460 end Analyze_Formal_Floating_Type;
2461
2462 -----------------------------------
2463 -- Analyze_Formal_Interface_Type;--
2464 -----------------------------------
2465
2466 procedure Analyze_Formal_Interface_Type
2467 (N : Node_Id;
2468 T : Entity_Id;
2469 Def : Node_Id)
2470 is
2471 Loc : constant Source_Ptr := Sloc (N);
2472 New_N : Node_Id;
2473
2474 begin
2475 New_N :=
2476 Make_Full_Type_Declaration (Loc,
2477 Defining_Identifier => T,
2478 Type_Definition => Def);
2479
2480 Rewrite (N, New_N);
2481 Analyze (N);
2482 Set_Is_Generic_Type (T);
2483 end Analyze_Formal_Interface_Type;
2484
2485 ---------------------------------
2486 -- Analyze_Formal_Modular_Type --
2487 ---------------------------------
2488
2489 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id) is
2490 begin
2491 -- Apart from their entity kind, generic modular types are treated like
2492 -- signed integer types, and have the same attributes.
2493
2494 Analyze_Formal_Signed_Integer_Type (T, Def);
2495 Set_Ekind (T, E_Modular_Integer_Subtype);
2496 Set_Ekind (Etype (T), E_Modular_Integer_Type);
2497
2498 end Analyze_Formal_Modular_Type;
2499
2500 ---------------------------------------
2501 -- Analyze_Formal_Object_Declaration --
2502 ---------------------------------------
2503
2504 procedure Analyze_Formal_Object_Declaration (N : Node_Id) is
2505 E : constant Node_Id := Default_Expression (N);
2506 Id : constant Node_Id := Defining_Identifier (N);
2507 K : Entity_Kind;
2508 T : Node_Id;
2509
2510 begin
2511 Enter_Name (Id);
2512
2513 -- Determine the mode of the formal object
2514
2515 if Out_Present (N) then
2516 K := E_Generic_In_Out_Parameter;
2517
2518 if not In_Present (N) then
2519 Error_Msg_N ("formal generic objects cannot have mode OUT", N);
2520 end if;
2521
2522 else
2523 K := E_Generic_In_Parameter;
2524 end if;
2525
2526 if Present (Subtype_Mark (N)) then
2527 Find_Type (Subtype_Mark (N));
2528 T := Entity (Subtype_Mark (N));
2529
2530 -- Verify that there is no redundant null exclusion
2531
2532 if Null_Exclusion_Present (N) then
2533 if not Is_Access_Type (T) then
2534 Error_Msg_N
2535 ("null exclusion can only apply to an access type", N);
2536
2537 elsif Can_Never_Be_Null (T) then
2538 Error_Msg_NE
2539 ("`NOT NULL` not allowed (& already excludes null)", N, T);
2540 end if;
2541 end if;
2542
2543 -- Ada 2005 (AI-423): Formal object with an access definition
2544
2545 else
2546 Check_Access_Definition (N);
2547 T := Access_Definition
2548 (Related_Nod => N,
2549 N => Access_Definition (N));
2550 end if;
2551
2552 if Ekind (T) = E_Incomplete_Type then
2553 declare
2554 Error_Node : Node_Id;
2555
2556 begin
2557 if Present (Subtype_Mark (N)) then
2558 Error_Node := Subtype_Mark (N);
2559 else
2560 Check_Access_Definition (N);
2561 Error_Node := Access_Definition (N);
2562 end if;
2563
2564 Error_Msg_N ("premature usage of incomplete type", Error_Node);
2565 end;
2566 end if;
2567
2568 if K = E_Generic_In_Parameter then
2569
2570 -- Ada 2005 (AI-287): Limited aggregates allowed in generic formals
2571
2572 if Ada_Version < Ada_2005 and then Is_Limited_Type (T) then
2573 Error_Msg_N
2574 ("generic formal of mode IN must not be of limited type", N);
2575 Explain_Limited_Type (T, N);
2576 end if;
2577
2578 if Is_Abstract_Type (T) then
2579 Error_Msg_N
2580 ("generic formal of mode IN must not be of abstract type", N);
2581 end if;
2582
2583 if Present (E) then
2584 Preanalyze_Spec_Expression (E, T);
2585
2586 if Is_Limited_Type (T) and then not OK_For_Limited_Init (T, E) then
2587 Error_Msg_N
2588 ("initialization not allowed for limited types", E);
2589 Explain_Limited_Type (T, E);
2590 end if;
2591 end if;
2592
2593 Set_Ekind (Id, K);
2594 Set_Etype (Id, T);
2595
2596 -- Case of generic IN OUT parameter
2597
2598 else
2599 -- If the formal has an unconstrained type, construct its actual
2600 -- subtype, as is done for subprogram formals. In this fashion, all
2601 -- its uses can refer to specific bounds.
2602
2603 Set_Ekind (Id, K);
2604 Set_Etype (Id, T);
2605
2606 if (Is_Array_Type (T) and then not Is_Constrained (T))
2607 or else (Ekind (T) = E_Record_Type and then Has_Discriminants (T))
2608 then
2609 declare
2610 Non_Freezing_Ref : constant Node_Id :=
2611 New_Occurrence_Of (Id, Sloc (Id));
2612 Decl : Node_Id;
2613
2614 begin
2615 -- Make sure the actual subtype doesn't generate bogus freezing
2616
2617 Set_Must_Not_Freeze (Non_Freezing_Ref);
2618 Decl := Build_Actual_Subtype (T, Non_Freezing_Ref);
2619 Insert_Before_And_Analyze (N, Decl);
2620 Set_Actual_Subtype (Id, Defining_Identifier (Decl));
2621 end;
2622 else
2623 Set_Actual_Subtype (Id, T);
2624 end if;
2625
2626 if Present (E) then
2627 Error_Msg_N
2628 ("initialization not allowed for `IN OUT` formals", N);
2629 end if;
2630 end if;
2631
2632 if Has_Aspects (N) then
2633 Analyze_Aspect_Specifications (N, Id);
2634 end if;
2635 end Analyze_Formal_Object_Declaration;
2636
2637 ----------------------------------------------
2638 -- Analyze_Formal_Ordinary_Fixed_Point_Type --
2639 ----------------------------------------------
2640
2641 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
2642 (T : Entity_Id;
2643 Def : Node_Id)
2644 is
2645 Loc : constant Source_Ptr := Sloc (Def);
2646 Base : constant Entity_Id :=
2647 New_Internal_Entity
2648 (E_Ordinary_Fixed_Point_Type, Current_Scope,
2649 Sloc (Defining_Identifier (Parent (Def))), 'G');
2650
2651 begin
2652 -- The semantic attributes are set for completeness only, their values
2653 -- will never be used, since all properties of the type are non-static.
2654
2655 Enter_Name (T);
2656 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
2657 Set_Etype (T, Base);
2658 Set_Size_Info (T, Standard_Integer);
2659 Set_RM_Size (T, RM_Size (Standard_Integer));
2660 Set_Small_Value (T, Ureal_1);
2661 Set_Delta_Value (T, Ureal_1);
2662 Set_Scalar_Range (T,
2663 Make_Range (Loc,
2664 Low_Bound => Make_Real_Literal (Loc, Ureal_1),
2665 High_Bound => Make_Real_Literal (Loc, Ureal_1)));
2666 Set_Is_Constrained (T);
2667
2668 Set_Is_Generic_Type (Base);
2669 Set_Etype (Base, Base);
2670 Set_Size_Info (Base, Standard_Integer);
2671 Set_RM_Size (Base, RM_Size (Standard_Integer));
2672 Set_Small_Value (Base, Ureal_1);
2673 Set_Delta_Value (Base, Ureal_1);
2674 Set_Scalar_Range (Base, Scalar_Range (T));
2675 Set_Parent (Base, Parent (Def));
2676
2677 Check_Restriction (No_Fixed_Point, Def);
2678 end Analyze_Formal_Ordinary_Fixed_Point_Type;
2679
2680 ----------------------------------------
2681 -- Analyze_Formal_Package_Declaration --
2682 ----------------------------------------
2683
2684 procedure Analyze_Formal_Package_Declaration (N : Node_Id) is
2685 Gen_Id : constant Node_Id := Name (N);
2686 Loc : constant Source_Ptr := Sloc (N);
2687 Pack_Id : constant Entity_Id := Defining_Identifier (N);
2688 Formal : Entity_Id;
2689 Gen_Decl : Node_Id;
2690 Gen_Unit : Entity_Id;
2691 Renaming : Node_Id;
2692
2693 Vis_Prims_List : Elist_Id := No_Elist;
2694 -- List of primitives made temporarily visible in the instantiation
2695 -- to match the visibility of the formal type.
2696
2697 function Build_Local_Package return Node_Id;
2698 -- The formal package is rewritten so that its parameters are replaced
2699 -- with corresponding declarations. For parameters with bona fide
2700 -- associations these declarations are created by Analyze_Associations
2701 -- as for a regular instantiation. For boxed parameters, we preserve
2702 -- the formal declarations and analyze them, in order to introduce
2703 -- entities of the right kind in the environment of the formal.
2704
2705 -------------------------
2706 -- Build_Local_Package --
2707 -------------------------
2708
2709 function Build_Local_Package return Node_Id is
2710 Decls : List_Id;
2711 Pack_Decl : Node_Id;
2712
2713 begin
2714 -- Within the formal, the name of the generic package is a renaming
2715 -- of the formal (as for a regular instantiation).
2716
2717 Pack_Decl :=
2718 Make_Package_Declaration (Loc,
2719 Specification =>
2720 Copy_Generic_Node
2721 (Specification (Original_Node (Gen_Decl)),
2722 Empty, Instantiating => True));
2723
2724 Renaming :=
2725 Make_Package_Renaming_Declaration (Loc,
2726 Defining_Unit_Name =>
2727 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
2728 Name => New_Occurrence_Of (Formal, Loc));
2729
2730 if Nkind (Gen_Id) = N_Identifier
2731 and then Chars (Gen_Id) = Chars (Pack_Id)
2732 then
2733 Error_Msg_NE
2734 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
2735 end if;
2736
2737 -- If the formal is declared with a box, or with an others choice,
2738 -- create corresponding declarations for all entities in the formal
2739 -- part, so that names with the proper types are available in the
2740 -- specification of the formal package.
2741
2742 -- On the other hand, if there are no associations, then all the
2743 -- formals must have defaults, and this will be checked by the
2744 -- call to Analyze_Associations.
2745
2746 if Box_Present (N)
2747 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2748 then
2749 declare
2750 Formal_Decl : Node_Id;
2751
2752 begin
2753 -- TBA : for a formal package, need to recurse ???
2754
2755 Decls := New_List;
2756 Formal_Decl :=
2757 First
2758 (Generic_Formal_Declarations (Original_Node (Gen_Decl)));
2759 while Present (Formal_Decl) loop
2760 Append_To
2761 (Decls,
2762 Copy_Generic_Node
2763 (Formal_Decl, Empty, Instantiating => True));
2764 Next (Formal_Decl);
2765 end loop;
2766 end;
2767
2768 -- If generic associations are present, use Analyze_Associations to
2769 -- create the proper renaming declarations.
2770
2771 else
2772 declare
2773 Act_Tree : constant Node_Id :=
2774 Copy_Generic_Node
2775 (Original_Node (Gen_Decl), Empty,
2776 Instantiating => True);
2777
2778 begin
2779 Generic_Renamings.Set_Last (0);
2780 Generic_Renamings_HTable.Reset;
2781 Instantiation_Node := N;
2782
2783 Decls :=
2784 Analyze_Associations
2785 (I_Node => Original_Node (N),
2786 Formals => Generic_Formal_Declarations (Act_Tree),
2787 F_Copy => Generic_Formal_Declarations (Gen_Decl));
2788
2789 Vis_Prims_List := Check_Hidden_Primitives (Decls);
2790 end;
2791 end if;
2792
2793 Append (Renaming, To => Decls);
2794
2795 -- Add generated declarations ahead of local declarations in
2796 -- the package.
2797
2798 if No (Visible_Declarations (Specification (Pack_Decl))) then
2799 Set_Visible_Declarations (Specification (Pack_Decl), Decls);
2800 else
2801 Insert_List_Before
2802 (First (Visible_Declarations (Specification (Pack_Decl))),
2803 Decls);
2804 end if;
2805
2806 return Pack_Decl;
2807 end Build_Local_Package;
2808
2809 -- Local variables
2810
2811 Save_ISMP : constant Boolean := Ignore_SPARK_Mode_Pragmas_In_Instance;
2812 -- Save flag Ignore_SPARK_Mode_Pragmas_In_Instance for restore on exit
2813
2814 Associations : Boolean := True;
2815 New_N : Node_Id;
2816 Parent_Installed : Boolean := False;
2817 Parent_Instance : Entity_Id;
2818 Renaming_In_Par : Entity_Id;
2819
2820 -- Start of processing for Analyze_Formal_Package_Declaration
2821
2822 begin
2823 Check_Text_IO_Special_Unit (Gen_Id);
2824
2825 Init_Env;
2826 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
2827 Gen_Unit := Entity (Gen_Id);
2828
2829 -- Check for a formal package that is a package renaming
2830
2831 if Present (Renamed_Object (Gen_Unit)) then
2832
2833 -- Indicate that unit is used, before replacing it with renamed
2834 -- entity for use below.
2835
2836 if In_Extended_Main_Source_Unit (N) then
2837 Set_Is_Instantiated (Gen_Unit);
2838 Generate_Reference (Gen_Unit, N);
2839 end if;
2840
2841 Gen_Unit := Renamed_Object (Gen_Unit);
2842 end if;
2843
2844 if Ekind (Gen_Unit) /= E_Generic_Package then
2845 Error_Msg_N ("expect generic package name", Gen_Id);
2846 Restore_Env;
2847 goto Leave;
2848
2849 elsif Gen_Unit = Current_Scope then
2850 Error_Msg_N
2851 ("generic package cannot be used as a formal package of itself",
2852 Gen_Id);
2853 Restore_Env;
2854 goto Leave;
2855
2856 elsif In_Open_Scopes (Gen_Unit) then
2857 if Is_Compilation_Unit (Gen_Unit)
2858 and then Is_Child_Unit (Current_Scope)
2859 then
2860 -- Special-case the error when the formal is a parent, and
2861 -- continue analysis to minimize cascaded errors.
2862
2863 Error_Msg_N
2864 ("generic parent cannot be used as formal package of a child "
2865 & "unit", Gen_Id);
2866
2867 else
2868 Error_Msg_N
2869 ("generic package cannot be used as a formal package within "
2870 & "itself", Gen_Id);
2871 Restore_Env;
2872 goto Leave;
2873 end if;
2874 end if;
2875
2876 -- Check that name of formal package does not hide name of generic,
2877 -- or its leading prefix. This check must be done separately because
2878 -- the name of the generic has already been analyzed.
2879
2880 declare
2881 Gen_Name : Entity_Id;
2882
2883 begin
2884 Gen_Name := Gen_Id;
2885 while Nkind (Gen_Name) = N_Expanded_Name loop
2886 Gen_Name := Prefix (Gen_Name);
2887 end loop;
2888
2889 if Chars (Gen_Name) = Chars (Pack_Id) then
2890 Error_Msg_NE
2891 ("& is hidden within declaration of formal package",
2892 Gen_Id, Gen_Name);
2893 end if;
2894 end;
2895
2896 if Box_Present (N)
2897 or else No (Generic_Associations (N))
2898 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2899 then
2900 Associations := False;
2901 end if;
2902
2903 -- If there are no generic associations, the generic parameters appear
2904 -- as local entities and are instantiated like them. We copy the generic
2905 -- package declaration as if it were an instantiation, and analyze it
2906 -- like a regular package, except that we treat the formals as
2907 -- additional visible components.
2908
2909 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
2910
2911 if In_Extended_Main_Source_Unit (N) then
2912 Set_Is_Instantiated (Gen_Unit);
2913 Generate_Reference (Gen_Unit, N);
2914 end if;
2915
2916 Formal := New_Copy (Pack_Id);
2917 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
2918
2919 -- Make local generic without formals. The formals will be replaced with
2920 -- internal declarations.
2921
2922 begin
2923 New_N := Build_Local_Package;
2924
2925 -- If there are errors in the parameter list, Analyze_Associations
2926 -- raises Instantiation_Error. Patch the declaration to prevent further
2927 -- exception propagation.
2928
2929 exception
2930 when Instantiation_Error =>
2931 Enter_Name (Formal);
2932 Set_Ekind (Formal, E_Variable);
2933 Set_Etype (Formal, Any_Type);
2934 Restore_Hidden_Primitives (Vis_Prims_List);
2935
2936 if Parent_Installed then
2937 Remove_Parent;
2938 end if;
2939
2940 goto Leave;
2941 end;
2942
2943 Rewrite (N, New_N);
2944 Set_Defining_Unit_Name (Specification (New_N), Formal);
2945 Set_Generic_Parent (Specification (N), Gen_Unit);
2946 Set_Instance_Env (Gen_Unit, Formal);
2947 Set_Is_Generic_Instance (Formal);
2948
2949 Enter_Name (Formal);
2950 Set_Ekind (Formal, E_Package);
2951 Set_Etype (Formal, Standard_Void_Type);
2952 Set_Inner_Instances (Formal, New_Elmt_List);
2953 Push_Scope (Formal);
2954
2955 -- Manually set the SPARK_Mode from the context because the package
2956 -- declaration is never analyzed.
2957
2958 Set_SPARK_Pragma (Formal, SPARK_Mode_Pragma);
2959 Set_SPARK_Aux_Pragma (Formal, SPARK_Mode_Pragma);
2960 Set_SPARK_Pragma_Inherited (Formal);
2961 Set_SPARK_Aux_Pragma_Inherited (Formal);
2962
2963 if Is_Child_Unit (Gen_Unit) and then Parent_Installed then
2964
2965 -- Similarly, we have to make the name of the formal visible in the
2966 -- parent instance, to resolve properly fully qualified names that
2967 -- may appear in the generic unit. The parent instance has been
2968 -- placed on the scope stack ahead of the current scope.
2969
2970 Parent_Instance := Scope_Stack.Table (Scope_Stack.Last - 1).Entity;
2971
2972 Renaming_In_Par :=
2973 Make_Defining_Identifier (Loc, Chars (Gen_Unit));
2974 Set_Ekind (Renaming_In_Par, E_Package);
2975 Set_Etype (Renaming_In_Par, Standard_Void_Type);
2976 Set_Scope (Renaming_In_Par, Parent_Instance);
2977 Set_Parent (Renaming_In_Par, Parent (Formal));
2978 Set_Renamed_Object (Renaming_In_Par, Formal);
2979 Append_Entity (Renaming_In_Par, Parent_Instance);
2980 end if;
2981
2982 -- A formal package declaration behaves as a package instantiation with
2983 -- respect to SPARK_Mode "off". If the annotation is "off" or altogether
2984 -- missing, set the global flag which signals Analyze_Pragma to ingnore
2985 -- all SPARK_Mode pragmas within the generic_package_name.
2986
2987 if SPARK_Mode /= On then
2988 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
2989
2990 -- Mark the formal spec in case the body is instantiated at a later
2991 -- pass. This preserves the original context in effect for the body.
2992
2993 Set_Ignore_SPARK_Mode_Pragmas (Formal);
2994 end if;
2995
2996 Analyze (Specification (N));
2997
2998 -- The formals for which associations are provided are not visible
2999 -- outside of the formal package. The others are still declared by a
3000 -- formal parameter declaration.
3001
3002 -- If there are no associations, the only local entity to hide is the
3003 -- generated package renaming itself.
3004
3005 declare
3006 E : Entity_Id;
3007
3008 begin
3009 E := First_Entity (Formal);
3010 while Present (E) loop
3011 if Associations and then not Is_Generic_Formal (E) then
3012 Set_Is_Hidden (E);
3013 end if;
3014
3015 if Ekind (E) = E_Package and then Renamed_Entity (E) = Formal then
3016 Set_Is_Hidden (E);
3017 exit;
3018 end if;
3019
3020 Next_Entity (E);
3021 end loop;
3022 end;
3023
3024 End_Package_Scope (Formal);
3025 Restore_Hidden_Primitives (Vis_Prims_List);
3026
3027 if Parent_Installed then
3028 Remove_Parent;
3029 end if;
3030
3031 Restore_Env;
3032
3033 -- Inside the generic unit, the formal package is a regular package, but
3034 -- no body is needed for it. Note that after instantiation, the defining
3035 -- unit name we need is in the new tree and not in the original (see
3036 -- Package_Instantiation). A generic formal package is an instance, and
3037 -- can be used as an actual for an inner instance.
3038
3039 Set_Has_Completion (Formal, True);
3040
3041 -- Add semantic information to the original defining identifier for ASIS
3042 -- use.
3043
3044 Set_Ekind (Pack_Id, E_Package);
3045 Set_Etype (Pack_Id, Standard_Void_Type);
3046 Set_Scope (Pack_Id, Scope (Formal));
3047 Set_Has_Completion (Pack_Id, True);
3048
3049 <<Leave>>
3050 if Has_Aspects (N) then
3051 Analyze_Aspect_Specifications (N, Pack_Id);
3052 end if;
3053
3054 Ignore_SPARK_Mode_Pragmas_In_Instance := Save_ISMP;
3055 end Analyze_Formal_Package_Declaration;
3056
3057 ---------------------------------
3058 -- Analyze_Formal_Private_Type --
3059 ---------------------------------
3060
3061 procedure Analyze_Formal_Private_Type
3062 (N : Node_Id;
3063 T : Entity_Id;
3064 Def : Node_Id)
3065 is
3066 begin
3067 New_Private_Type (N, T, Def);
3068
3069 -- Set the size to an arbitrary but legal value
3070
3071 Set_Size_Info (T, Standard_Integer);
3072 Set_RM_Size (T, RM_Size (Standard_Integer));
3073 end Analyze_Formal_Private_Type;
3074
3075 ------------------------------------
3076 -- Analyze_Formal_Incomplete_Type --
3077 ------------------------------------
3078
3079 procedure Analyze_Formal_Incomplete_Type
3080 (T : Entity_Id;
3081 Def : Node_Id)
3082 is
3083 begin
3084 Enter_Name (T);
3085 Set_Ekind (T, E_Incomplete_Type);
3086 Set_Etype (T, T);
3087 Set_Private_Dependents (T, New_Elmt_List);
3088
3089 if Tagged_Present (Def) then
3090 Set_Is_Tagged_Type (T);
3091 Make_Class_Wide_Type (T);
3092 Set_Direct_Primitive_Operations (T, New_Elmt_List);
3093 end if;
3094 end Analyze_Formal_Incomplete_Type;
3095
3096 ----------------------------------------
3097 -- Analyze_Formal_Signed_Integer_Type --
3098 ----------------------------------------
3099
3100 procedure Analyze_Formal_Signed_Integer_Type
3101 (T : Entity_Id;
3102 Def : Node_Id)
3103 is
3104 Base : constant Entity_Id :=
3105 New_Internal_Entity
3106 (E_Signed_Integer_Type,
3107 Current_Scope,
3108 Sloc (Defining_Identifier (Parent (Def))), 'G');
3109
3110 begin
3111 Enter_Name (T);
3112
3113 Set_Ekind (T, E_Signed_Integer_Subtype);
3114 Set_Etype (T, Base);
3115 Set_Size_Info (T, Standard_Integer);
3116 Set_RM_Size (T, RM_Size (Standard_Integer));
3117 Set_Scalar_Range (T, Scalar_Range (Standard_Integer));
3118 Set_Is_Constrained (T);
3119
3120 Set_Is_Generic_Type (Base);
3121 Set_Size_Info (Base, Standard_Integer);
3122 Set_RM_Size (Base, RM_Size (Standard_Integer));
3123 Set_Etype (Base, Base);
3124 Set_Scalar_Range (Base, Scalar_Range (Standard_Integer));
3125 Set_Parent (Base, Parent (Def));
3126 end Analyze_Formal_Signed_Integer_Type;
3127
3128 -------------------------------------------
3129 -- Analyze_Formal_Subprogram_Declaration --
3130 -------------------------------------------
3131
3132 procedure Analyze_Formal_Subprogram_Declaration (N : Node_Id) is
3133 Spec : constant Node_Id := Specification (N);
3134 Def : constant Node_Id := Default_Name (N);
3135 Nam : constant Entity_Id := Defining_Unit_Name (Spec);
3136 Subp : Entity_Id;
3137
3138 begin
3139 if Nam = Error then
3140 return;
3141 end if;
3142
3143 if Nkind (Nam) = N_Defining_Program_Unit_Name then
3144 Error_Msg_N ("name of formal subprogram must be a direct name", Nam);
3145 goto Leave;
3146 end if;
3147
3148 Analyze_Subprogram_Declaration (N);
3149 Set_Is_Formal_Subprogram (Nam);
3150 Set_Has_Completion (Nam);
3151
3152 if Nkind (N) = N_Formal_Abstract_Subprogram_Declaration then
3153 Set_Is_Abstract_Subprogram (Nam);
3154
3155 Set_Is_Dispatching_Operation (Nam);
3156
3157 -- A formal abstract procedure cannot have a null default
3158 -- (RM 12.6(4.1/2)).
3159
3160 if Nkind (Spec) = N_Procedure_Specification
3161 and then Null_Present (Spec)
3162 then
3163 Error_Msg_N
3164 ("a formal abstract subprogram cannot default to null", Spec);
3165 end if;
3166
3167 declare
3168 Ctrl_Type : constant Entity_Id := Find_Dispatching_Type (Nam);
3169 begin
3170 if No (Ctrl_Type) then
3171 Error_Msg_N
3172 ("abstract formal subprogram must have a controlling type",
3173 N);
3174
3175 elsif Ada_Version >= Ada_2012
3176 and then Is_Incomplete_Type (Ctrl_Type)
3177 then
3178 Error_Msg_NE
3179 ("controlling type of abstract formal subprogram cannot "
3180 & "be incomplete type", N, Ctrl_Type);
3181
3182 else
3183 Check_Controlling_Formals (Ctrl_Type, Nam);
3184 end if;
3185 end;
3186 end if;
3187
3188 -- Default name is resolved at the point of instantiation
3189
3190 if Box_Present (N) then
3191 null;
3192
3193 -- Else default is bound at the point of generic declaration
3194
3195 elsif Present (Def) then
3196 if Nkind (Def) = N_Operator_Symbol then
3197 Find_Direct_Name (Def);
3198
3199 elsif Nkind (Def) /= N_Attribute_Reference then
3200 Analyze (Def);
3201
3202 else
3203 -- For an attribute reference, analyze the prefix and verify
3204 -- that it has the proper profile for the subprogram.
3205
3206 Analyze (Prefix (Def));
3207 Valid_Default_Attribute (Nam, Def);
3208 goto Leave;
3209 end if;
3210
3211 -- Default name may be overloaded, in which case the interpretation
3212 -- with the correct profile must be selected, as for a renaming.
3213 -- If the definition is an indexed component, it must denote a
3214 -- member of an entry family. If it is a selected component, it
3215 -- can be a protected operation.
3216
3217 if Etype (Def) = Any_Type then
3218 goto Leave;
3219
3220 elsif Nkind (Def) = N_Selected_Component then
3221 if not Is_Overloadable (Entity (Selector_Name (Def))) then
3222 Error_Msg_N ("expect valid subprogram name as default", Def);
3223 end if;
3224
3225 elsif Nkind (Def) = N_Indexed_Component then
3226 if Is_Entity_Name (Prefix (Def)) then
3227 if Ekind (Entity (Prefix (Def))) /= E_Entry_Family then
3228 Error_Msg_N ("expect valid subprogram name as default", Def);
3229 end if;
3230
3231 elsif Nkind (Prefix (Def)) = N_Selected_Component then
3232 if Ekind (Entity (Selector_Name (Prefix (Def)))) /=
3233 E_Entry_Family
3234 then
3235 Error_Msg_N ("expect valid subprogram name as default", Def);
3236 end if;
3237
3238 else
3239 Error_Msg_N ("expect valid subprogram name as default", Def);
3240 goto Leave;
3241 end if;
3242
3243 elsif Nkind (Def) = N_Character_Literal then
3244
3245 -- Needs some type checks: subprogram should be parameterless???
3246
3247 Resolve (Def, (Etype (Nam)));
3248
3249 elsif not Is_Entity_Name (Def)
3250 or else not Is_Overloadable (Entity (Def))
3251 then
3252 Error_Msg_N ("expect valid subprogram name as default", Def);
3253 goto Leave;
3254
3255 elsif not Is_Overloaded (Def) then
3256 Subp := Entity (Def);
3257
3258 if Subp = Nam then
3259 Error_Msg_N ("premature usage of formal subprogram", Def);
3260
3261 elsif not Entity_Matches_Spec (Subp, Nam) then
3262 Error_Msg_N ("no visible entity matches specification", Def);
3263 end if;
3264
3265 -- More than one interpretation, so disambiguate as for a renaming
3266
3267 else
3268 declare
3269 I : Interp_Index;
3270 I1 : Interp_Index := 0;
3271 It : Interp;
3272 It1 : Interp;
3273
3274 begin
3275 Subp := Any_Id;
3276 Get_First_Interp (Def, I, It);
3277 while Present (It.Nam) loop
3278 if Entity_Matches_Spec (It.Nam, Nam) then
3279 if Subp /= Any_Id then
3280 It1 := Disambiguate (Def, I1, I, Etype (Subp));
3281
3282 if It1 = No_Interp then
3283 Error_Msg_N ("ambiguous default subprogram", Def);
3284 else
3285 Subp := It1.Nam;
3286 end if;
3287
3288 exit;
3289
3290 else
3291 I1 := I;
3292 Subp := It.Nam;
3293 end if;
3294 end if;
3295
3296 Get_Next_Interp (I, It);
3297 end loop;
3298 end;
3299
3300 if Subp /= Any_Id then
3301
3302 -- Subprogram found, generate reference to it
3303
3304 Set_Entity (Def, Subp);
3305 Generate_Reference (Subp, Def);
3306
3307 if Subp = Nam then
3308 Error_Msg_N ("premature usage of formal subprogram", Def);
3309
3310 elsif Ekind (Subp) /= E_Operator then
3311 Check_Mode_Conformant (Subp, Nam);
3312 end if;
3313
3314 else
3315 Error_Msg_N ("no visible subprogram matches specification", N);
3316 end if;
3317 end if;
3318 end if;
3319
3320 <<Leave>>
3321 if Has_Aspects (N) then
3322 Analyze_Aspect_Specifications (N, Nam);
3323 end if;
3324
3325 end Analyze_Formal_Subprogram_Declaration;
3326
3327 -------------------------------------
3328 -- Analyze_Formal_Type_Declaration --
3329 -------------------------------------
3330
3331 procedure Analyze_Formal_Type_Declaration (N : Node_Id) is
3332 Def : constant Node_Id := Formal_Type_Definition (N);
3333 T : Entity_Id;
3334
3335 begin
3336 T := Defining_Identifier (N);
3337
3338 if Present (Discriminant_Specifications (N))
3339 and then Nkind (Def) /= N_Formal_Private_Type_Definition
3340 then
3341 Error_Msg_N
3342 ("discriminants not allowed for this formal type", T);
3343 end if;
3344
3345 -- Enter the new name, and branch to specific routine
3346
3347 case Nkind (Def) is
3348 when N_Formal_Private_Type_Definition =>
3349 Analyze_Formal_Private_Type (N, T, Def);
3350
3351 when N_Formal_Derived_Type_Definition =>
3352 Analyze_Formal_Derived_Type (N, T, Def);
3353
3354 when N_Formal_Incomplete_Type_Definition =>
3355 Analyze_Formal_Incomplete_Type (T, Def);
3356
3357 when N_Formal_Discrete_Type_Definition =>
3358 Analyze_Formal_Discrete_Type (T, Def);
3359
3360 when N_Formal_Signed_Integer_Type_Definition =>
3361 Analyze_Formal_Signed_Integer_Type (T, Def);
3362
3363 when N_Formal_Modular_Type_Definition =>
3364 Analyze_Formal_Modular_Type (T, Def);
3365
3366 when N_Formal_Floating_Point_Definition =>
3367 Analyze_Formal_Floating_Type (T, Def);
3368
3369 when N_Formal_Ordinary_Fixed_Point_Definition =>
3370 Analyze_Formal_Ordinary_Fixed_Point_Type (T, Def);
3371
3372 when N_Formal_Decimal_Fixed_Point_Definition =>
3373 Analyze_Formal_Decimal_Fixed_Point_Type (T, Def);
3374
3375 when N_Array_Type_Definition =>
3376 Analyze_Formal_Array_Type (T, Def);
3377
3378 when N_Access_Function_Definition
3379 | N_Access_Procedure_Definition
3380 | N_Access_To_Object_Definition
3381 =>
3382 Analyze_Generic_Access_Type (T, Def);
3383
3384 -- Ada 2005: a interface declaration is encoded as an abstract
3385 -- record declaration or a abstract type derivation.
3386
3387 when N_Record_Definition =>
3388 Analyze_Formal_Interface_Type (N, T, Def);
3389
3390 when N_Derived_Type_Definition =>
3391 Analyze_Formal_Derived_Interface_Type (N, T, Def);
3392
3393 when N_Error =>
3394 null;
3395
3396 when others =>
3397 raise Program_Error;
3398 end case;
3399
3400 Set_Is_Generic_Type (T);
3401
3402 if Has_Aspects (N) then
3403 Analyze_Aspect_Specifications (N, T);
3404 end if;
3405 end Analyze_Formal_Type_Declaration;
3406
3407 ------------------------------------
3408 -- Analyze_Function_Instantiation --
3409 ------------------------------------
3410
3411 procedure Analyze_Function_Instantiation (N : Node_Id) is
3412 begin
3413 Analyze_Subprogram_Instantiation (N, E_Function);
3414 end Analyze_Function_Instantiation;
3415
3416 ---------------------------------
3417 -- Analyze_Generic_Access_Type --
3418 ---------------------------------
3419
3420 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id) is
3421 begin
3422 Enter_Name (T);
3423
3424 if Nkind (Def) = N_Access_To_Object_Definition then
3425 Access_Type_Declaration (T, Def);
3426
3427 if Is_Incomplete_Or_Private_Type (Designated_Type (T))
3428 and then No (Full_View (Designated_Type (T)))
3429 and then not Is_Generic_Type (Designated_Type (T))
3430 then
3431 Error_Msg_N ("premature usage of incomplete type", Def);
3432
3433 elsif not Is_Entity_Name (Subtype_Indication (Def)) then
3434 Error_Msg_N
3435 ("only a subtype mark is allowed in a formal", Def);
3436 end if;
3437
3438 else
3439 Access_Subprogram_Declaration (T, Def);
3440 end if;
3441 end Analyze_Generic_Access_Type;
3442
3443 ---------------------------------
3444 -- Analyze_Generic_Formal_Part --
3445 ---------------------------------
3446
3447 procedure Analyze_Generic_Formal_Part (N : Node_Id) is
3448 Gen_Parm_Decl : Node_Id;
3449
3450 begin
3451 -- The generic formals are processed in the scope of the generic unit,
3452 -- where they are immediately visible. The scope is installed by the
3453 -- caller.
3454
3455 Gen_Parm_Decl := First (Generic_Formal_Declarations (N));
3456 while Present (Gen_Parm_Decl) loop
3457 Analyze (Gen_Parm_Decl);
3458 Next (Gen_Parm_Decl);
3459 end loop;
3460
3461 Generate_Reference_To_Generic_Formals (Current_Scope);
3462 end Analyze_Generic_Formal_Part;
3463
3464 ------------------------------------------
3465 -- Analyze_Generic_Package_Declaration --
3466 ------------------------------------------
3467
3468 procedure Analyze_Generic_Package_Declaration (N : Node_Id) is
3469 Decls : constant List_Id := Visible_Declarations (Specification (N));
3470 Loc : constant Source_Ptr := Sloc (N);
3471
3472 Decl : Node_Id;
3473 Id : Entity_Id;
3474 New_N : Node_Id;
3475 Renaming : Node_Id;
3476 Save_Parent : Node_Id;
3477
3478 begin
3479 Check_SPARK_05_Restriction ("generic is not allowed", N);
3480
3481 -- We introduce a renaming of the enclosing package, to have a usable
3482 -- entity as the prefix of an expanded name for a local entity of the
3483 -- form Par.P.Q, where P is the generic package. This is because a local
3484 -- entity named P may hide it, so that the usual visibility rules in
3485 -- the instance will not resolve properly.
3486
3487 Renaming :=
3488 Make_Package_Renaming_Declaration (Loc,
3489 Defining_Unit_Name =>
3490 Make_Defining_Identifier (Loc,
3491 Chars => New_External_Name (Chars (Defining_Entity (N)), "GH")),
3492 Name =>
3493 Make_Identifier (Loc, Chars (Defining_Entity (N))));
3494
3495 -- The declaration is inserted before other declarations, but before
3496 -- pragmas that may be library-unit pragmas and must appear before other
3497 -- declarations. The pragma Compile_Time_Error is not in this class, and
3498 -- may contain an expression that includes such a qualified name, so the
3499 -- renaming declaration must appear before it.
3500
3501 -- Are there other pragmas that require this special handling ???
3502
3503 if Present (Decls) then
3504 Decl := First (Decls);
3505 while Present (Decl)
3506 and then Nkind (Decl) = N_Pragma
3507 and then Get_Pragma_Id (Decl) /= Pragma_Compile_Time_Error
3508 loop
3509 Next (Decl);
3510 end loop;
3511
3512 if Present (Decl) then
3513 Insert_Before (Decl, Renaming);
3514 else
3515 Append (Renaming, Visible_Declarations (Specification (N)));
3516 end if;
3517
3518 else
3519 Set_Visible_Declarations (Specification (N), New_List (Renaming));
3520 end if;
3521
3522 -- Create copy of generic unit, and save for instantiation. If the unit
3523 -- is a child unit, do not copy the specifications for the parent, which
3524 -- are not part of the generic tree.
3525
3526 Save_Parent := Parent_Spec (N);
3527 Set_Parent_Spec (N, Empty);
3528
3529 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
3530 Set_Parent_Spec (New_N, Save_Parent);
3531 Rewrite (N, New_N);
3532
3533 -- Once the contents of the generic copy and the template are swapped,
3534 -- do the same for their respective aspect specifications.
3535
3536 Exchange_Aspects (N, New_N);
3537
3538 -- Collect all contract-related source pragmas found within the template
3539 -- and attach them to the contract of the package spec. This contract is
3540 -- used in the capture of global references within annotations.
3541
3542 Create_Generic_Contract (N);
3543
3544 Id := Defining_Entity (N);
3545 Generate_Definition (Id);
3546
3547 -- Expansion is not applied to generic units
3548
3549 Start_Generic;
3550
3551 Enter_Name (Id);
3552 Set_Ekind (Id, E_Generic_Package);
3553 Set_Etype (Id, Standard_Void_Type);
3554
3555 -- Set SPARK_Mode from context
3556
3557 Set_SPARK_Pragma (Id, SPARK_Mode_Pragma);
3558 Set_SPARK_Aux_Pragma (Id, SPARK_Mode_Pragma);
3559 Set_SPARK_Pragma_Inherited (Id);
3560 Set_SPARK_Aux_Pragma_Inherited (Id);
3561
3562 -- Preserve relevant elaboration-related attributes of the context which
3563 -- are no longer available or very expensive to recompute once analysis,
3564 -- resolution, and expansion are over.
3565
3566 Mark_Elaboration_Attributes
3567 (N_Id => Id,
3568 Checks => True,
3569 Warnings => True);
3570
3571 -- Analyze aspects now, so that generated pragmas appear in the
3572 -- declarations before building and analyzing the generic copy.
3573
3574 if Has_Aspects (N) then
3575 Analyze_Aspect_Specifications (N, Id);
3576 end if;
3577
3578 Push_Scope (Id);
3579 Enter_Generic_Scope (Id);
3580 Set_Inner_Instances (Id, New_Elmt_List);
3581
3582 Set_Categorization_From_Pragmas (N);
3583 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3584
3585 -- Link the declaration of the generic homonym in the generic copy to
3586 -- the package it renames, so that it is always resolved properly.
3587
3588 Set_Generic_Homonym (Id, Defining_Unit_Name (Renaming));
3589 Set_Entity (Associated_Node (Name (Renaming)), Id);
3590
3591 -- For a library unit, we have reconstructed the entity for the unit,
3592 -- and must reset it in the library tables.
3593
3594 if Nkind (Parent (N)) = N_Compilation_Unit then
3595 Set_Cunit_Entity (Current_Sem_Unit, Id);
3596 end if;
3597
3598 Analyze_Generic_Formal_Part (N);
3599
3600 -- After processing the generic formals, analysis proceeds as for a
3601 -- non-generic package.
3602
3603 Analyze (Specification (N));
3604
3605 Validate_Categorization_Dependency (N, Id);
3606
3607 End_Generic;
3608
3609 End_Package_Scope (Id);
3610 Exit_Generic_Scope (Id);
3611
3612 -- If the generic appears within a package unit, the body of that unit
3613 -- has to be present for instantiation and inlining.
3614
3615 if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Declaration then
3616 Set_Body_Needed_For_Inlining
3617 (Defining_Entity (Unit (Cunit (Current_Sem_Unit))));
3618 end if;
3619
3620 if Nkind (Parent (N)) /= N_Compilation_Unit then
3621 Move_Freeze_Nodes (Id, N, Visible_Declarations (Specification (N)));
3622 Move_Freeze_Nodes (Id, N, Private_Declarations (Specification (N)));
3623 Move_Freeze_Nodes (Id, N, Generic_Formal_Declarations (N));
3624
3625 else
3626 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
3627 Validate_RT_RAT_Component (N);
3628
3629 -- If this is a spec without a body, check that generic parameters
3630 -- are referenced.
3631
3632 if not Body_Required (Parent (N)) then
3633 Check_References (Id);
3634 end if;
3635 end if;
3636
3637 -- If there is a specified storage pool in the context, create an
3638 -- aspect on the package declaration, so that it is used in any
3639 -- instance that does not override it.
3640
3641 if Present (Default_Pool) then
3642 declare
3643 ASN : Node_Id;
3644
3645 begin
3646 ASN :=
3647 Make_Aspect_Specification (Loc,
3648 Identifier => Make_Identifier (Loc, Name_Default_Storage_Pool),
3649 Expression => New_Copy (Default_Pool));
3650
3651 if No (Aspect_Specifications (Specification (N))) then
3652 Set_Aspect_Specifications (Specification (N), New_List (ASN));
3653 else
3654 Append (ASN, Aspect_Specifications (Specification (N)));
3655 end if;
3656 end;
3657 end if;
3658 end Analyze_Generic_Package_Declaration;
3659
3660 --------------------------------------------
3661 -- Analyze_Generic_Subprogram_Declaration --
3662 --------------------------------------------
3663
3664 procedure Analyze_Generic_Subprogram_Declaration (N : Node_Id) is
3665 Formals : List_Id;
3666 Id : Entity_Id;
3667 New_N : Node_Id;
3668 Result_Type : Entity_Id;
3669 Save_Parent : Node_Id;
3670 Spec : Node_Id;
3671 Typ : Entity_Id;
3672
3673 begin
3674 Check_SPARK_05_Restriction ("generic is not allowed", N);
3675
3676 -- Create copy of generic unit, and save for instantiation. If the unit
3677 -- is a child unit, do not copy the specifications for the parent, which
3678 -- are not part of the generic tree.
3679
3680 Save_Parent := Parent_Spec (N);
3681 Set_Parent_Spec (N, Empty);
3682
3683 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
3684 Set_Parent_Spec (New_N, Save_Parent);
3685 Rewrite (N, New_N);
3686
3687 -- Once the contents of the generic copy and the template are swapped,
3688 -- do the same for their respective aspect specifications.
3689
3690 Exchange_Aspects (N, New_N);
3691
3692 -- Collect all contract-related source pragmas found within the template
3693 -- and attach them to the contract of the subprogram spec. This contract
3694 -- is used in the capture of global references within annotations.
3695
3696 Create_Generic_Contract (N);
3697
3698 Spec := Specification (N);
3699 Id := Defining_Entity (Spec);
3700 Generate_Definition (Id);
3701
3702 if Nkind (Id) = N_Defining_Operator_Symbol then
3703 Error_Msg_N
3704 ("operator symbol not allowed for generic subprogram", Id);
3705 end if;
3706
3707 Start_Generic;
3708
3709 Enter_Name (Id);
3710 Set_Scope_Depth_Value (Id, Scope_Depth (Current_Scope) + 1);
3711
3712 -- Analyze the aspects of the generic copy to ensure that all generated
3713 -- pragmas (if any) perform their semantic effects.
3714
3715 if Has_Aspects (N) then
3716 Analyze_Aspect_Specifications (N, Id);
3717 end if;
3718
3719 Push_Scope (Id);
3720 Enter_Generic_Scope (Id);
3721 Set_Inner_Instances (Id, New_Elmt_List);
3722 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3723
3724 Analyze_Generic_Formal_Part (N);
3725
3726 if Nkind (Spec) = N_Function_Specification then
3727 Set_Ekind (Id, E_Generic_Function);
3728 else
3729 Set_Ekind (Id, E_Generic_Procedure);
3730 end if;
3731
3732 -- Set SPARK_Mode from context
3733
3734 Set_SPARK_Pragma (Id, SPARK_Mode_Pragma);
3735 Set_SPARK_Pragma_Inherited (Id);
3736
3737 -- Preserve relevant elaboration-related attributes of the context which
3738 -- are no longer available or very expensive to recompute once analysis,
3739 -- resolution, and expansion are over.
3740
3741 Mark_Elaboration_Attributes
3742 (N_Id => Id,
3743 Checks => True,
3744 Warnings => True);
3745
3746 Formals := Parameter_Specifications (Spec);
3747
3748 if Present (Formals) then
3749 Process_Formals (Formals, Spec);
3750 end if;
3751
3752 if Nkind (Spec) = N_Function_Specification then
3753 if Nkind (Result_Definition (Spec)) = N_Access_Definition then
3754 Result_Type := Access_Definition (Spec, Result_Definition (Spec));
3755 Set_Etype (Id, Result_Type);
3756
3757 -- Check restriction imposed by AI05-073: a generic function
3758 -- cannot return an abstract type or an access to such.
3759
3760 -- This is a binding interpretation should it apply to earlier
3761 -- versions of Ada as well as Ada 2012???
3762
3763 if Is_Abstract_Type (Designated_Type (Result_Type))
3764 and then Ada_Version >= Ada_2012
3765 then
3766 Error_Msg_N
3767 ("generic function cannot have an access result "
3768 & "that designates an abstract type", Spec);
3769 end if;
3770
3771 else
3772 Find_Type (Result_Definition (Spec));
3773 Typ := Entity (Result_Definition (Spec));
3774
3775 if Is_Abstract_Type (Typ)
3776 and then Ada_Version >= Ada_2012
3777 then
3778 Error_Msg_N
3779 ("generic function cannot have abstract result type", Spec);
3780 end if;
3781
3782 -- If a null exclusion is imposed on the result type, then create
3783 -- a null-excluding itype (an access subtype) and use it as the
3784 -- function's Etype.
3785
3786 if Is_Access_Type (Typ)
3787 and then Null_Exclusion_Present (Spec)
3788 then
3789 Set_Etype (Id,
3790 Create_Null_Excluding_Itype
3791 (T => Typ,
3792 Related_Nod => Spec,
3793 Scope_Id => Defining_Unit_Name (Spec)));
3794 else
3795 Set_Etype (Id, Typ);
3796 end if;
3797 end if;
3798
3799 else
3800 Set_Etype (Id, Standard_Void_Type);
3801 end if;
3802
3803 -- For a library unit, we have reconstructed the entity for the unit,
3804 -- and must reset it in the library tables. We also make sure that
3805 -- Body_Required is set properly in the original compilation unit node.
3806
3807 if Nkind (Parent (N)) = N_Compilation_Unit then
3808 Set_Cunit_Entity (Current_Sem_Unit, Id);
3809 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
3810 end if;
3811
3812 -- If the generic appears within a package unit, the body of that unit
3813 -- has to be present for instantiation and inlining.
3814
3815 if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Declaration
3816 and then Unit_Requires_Body (Id)
3817 then
3818 Set_Body_Needed_For_Inlining
3819 (Defining_Entity (Unit (Cunit (Current_Sem_Unit))));
3820 end if;
3821
3822 Set_Categorization_From_Pragmas (N);
3823 Validate_Categorization_Dependency (N, Id);
3824
3825 -- Capture all global references that occur within the profile of the
3826 -- generic subprogram. Aspects are not part of this processing because
3827 -- they must be delayed. If processed now, Save_Global_References will
3828 -- destroy the Associated_Node links and prevent the capture of global
3829 -- references when the contract of the generic subprogram is analyzed.
3830
3831 Save_Global_References (Original_Node (N));
3832
3833 End_Generic;
3834 End_Scope;
3835 Exit_Generic_Scope (Id);
3836 Generate_Reference_To_Formals (Id);
3837
3838 List_Inherited_Pre_Post_Aspects (Id);
3839 end Analyze_Generic_Subprogram_Declaration;
3840
3841 -----------------------------------
3842 -- Analyze_Package_Instantiation --
3843 -----------------------------------
3844
3845 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
3846 -- must be replaced by gotos which jump to the end of the routine in order
3847 -- to restore the Ghost and SPARK modes.
3848
3849 procedure Analyze_Package_Instantiation (N : Node_Id) is
3850 Has_Inline_Always : Boolean := False;
3851
3852 procedure Delay_Descriptors (E : Entity_Id);
3853 -- Delay generation of subprogram descriptors for given entity
3854
3855 function Might_Inline_Subp (Gen_Unit : Entity_Id) return Boolean;
3856 -- If inlining is active and the generic contains inlined subprograms,
3857 -- we instantiate the body. This may cause superfluous instantiations,
3858 -- but it is simpler than detecting the need for the body at the point
3859 -- of inlining, when the context of the instance is not available.
3860
3861 -----------------------
3862 -- Delay_Descriptors --
3863 -----------------------
3864
3865 procedure Delay_Descriptors (E : Entity_Id) is
3866 begin
3867 if not Delay_Subprogram_Descriptors (E) then
3868 Set_Delay_Subprogram_Descriptors (E);
3869 Pending_Descriptor.Append (E);
3870 end if;
3871 end Delay_Descriptors;
3872
3873 -----------------------
3874 -- Might_Inline_Subp --
3875 -----------------------
3876
3877 function Might_Inline_Subp (Gen_Unit : Entity_Id) return Boolean is
3878 E : Entity_Id;
3879
3880 begin
3881 if not Inline_Processing_Required then
3882 return False;
3883
3884 else
3885 E := First_Entity (Gen_Unit);
3886 while Present (E) loop
3887 if Is_Subprogram (E) and then Is_Inlined (E) then
3888 -- Remember if there are any subprograms with Inline_Always
3889
3890 if Has_Pragma_Inline_Always (E) then
3891 Has_Inline_Always := True;
3892 end if;
3893
3894 return True;
3895 end if;
3896
3897 Next_Entity (E);
3898 end loop;
3899 end if;
3900
3901 return False;
3902 end Might_Inline_Subp;
3903
3904 -- Local declarations
3905
3906 Gen_Id : constant Node_Id := Name (N);
3907 Is_Actual_Pack : constant Boolean :=
3908 Is_Internal (Defining_Entity (N));
3909 Loc : constant Source_Ptr := Sloc (N);
3910
3911 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
3912 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
3913 Saved_ISMP : constant Boolean :=
3914 Ignore_SPARK_Mode_Pragmas_In_Instance;
3915 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
3916 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
3917 -- Save the Ghost and SPARK mode-related data to restore on exit
3918
3919 Saved_Style_Check : constant Boolean := Style_Check;
3920 -- Save style check mode for restore on exit
3921
3922 Act_Decl : Node_Id;
3923 Act_Decl_Name : Node_Id;
3924 Act_Decl_Id : Entity_Id;
3925 Act_Spec : Node_Id;
3926 Act_Tree : Node_Id;
3927 Env_Installed : Boolean := False;
3928 Gen_Decl : Node_Id;
3929 Gen_Spec : Node_Id;
3930 Gen_Unit : Entity_Id;
3931 Inline_Now : Boolean := False;
3932 Needs_Body : Boolean;
3933 Parent_Installed : Boolean := False;
3934 Renaming_List : List_Id;
3935 Unit_Renaming : Node_Id;
3936
3937 Vis_Prims_List : Elist_Id := No_Elist;
3938 -- List of primitives made temporarily visible in the instantiation
3939 -- to match the visibility of the formal type
3940
3941 -- Start of processing for Analyze_Package_Instantiation
3942
3943 begin
3944 -- Preserve relevant elaboration-related attributes of the context which
3945 -- are no longer available or very expensive to recompute once analysis,
3946 -- resolution, and expansion are over.
3947
3948 Mark_Elaboration_Attributes
3949 (N_Id => N,
3950 Checks => True,
3951 Level => True,
3952 Modes => True,
3953 Warnings => True);
3954
3955 Check_SPARK_05_Restriction ("generic is not allowed", N);
3956
3957 -- Very first thing: check for Text_IO special unit in case we are
3958 -- instantiating one of the children of [[Wide_]Wide_]Text_IO.
3959
3960 Check_Text_IO_Special_Unit (Name (N));
3961
3962 -- Make node global for error reporting
3963
3964 Instantiation_Node := N;
3965
3966 -- Case of instantiation of a generic package
3967
3968 if Nkind (N) = N_Package_Instantiation then
3969 Act_Decl_Id := New_Copy (Defining_Entity (N));
3970 Set_Comes_From_Source (Act_Decl_Id, True);
3971
3972 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name then
3973 Act_Decl_Name :=
3974 Make_Defining_Program_Unit_Name (Loc,
3975 Name =>
3976 New_Copy_Tree (Name (Defining_Unit_Name (N))),
3977 Defining_Identifier => Act_Decl_Id);
3978 else
3979 Act_Decl_Name := Act_Decl_Id;
3980 end if;
3981
3982 -- Case of instantiation of a formal package
3983
3984 else
3985 Act_Decl_Id := Defining_Identifier (N);
3986 Act_Decl_Name := Act_Decl_Id;
3987 end if;
3988
3989 Generate_Definition (Act_Decl_Id);
3990 Set_Ekind (Act_Decl_Id, E_Package);
3991
3992 -- Initialize list of incomplete actuals before analysis
3993
3994 Set_Incomplete_Actuals (Act_Decl_Id, New_Elmt_List);
3995
3996 Preanalyze_Actuals (N, Act_Decl_Id);
3997
3998 -- Turn off style checking in instances. If the check is enabled on the
3999 -- generic unit, a warning in an instance would just be noise. If not
4000 -- enabled on the generic, then a warning in an instance is just wrong.
4001 -- This must be done after analyzing the actuals, which do come from
4002 -- source and are subject to style checking.
4003
4004 Style_Check := False;
4005
4006 Init_Env;
4007 Env_Installed := True;
4008
4009 -- Reset renaming map for formal types. The mapping is established
4010 -- when analyzing the generic associations, but some mappings are
4011 -- inherited from formal packages of parent units, and these are
4012 -- constructed when the parents are installed.
4013
4014 Generic_Renamings.Set_Last (0);
4015 Generic_Renamings_HTable.Reset;
4016
4017 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
4018 Gen_Unit := Entity (Gen_Id);
4019
4020 -- A package instantiation is Ghost when it is subject to pragma Ghost
4021 -- or the generic template is Ghost. Set the mode now to ensure that
4022 -- any nodes generated during analysis and expansion are marked as
4023 -- Ghost.
4024
4025 Mark_And_Set_Ghost_Instantiation (N, Gen_Unit);
4026
4027 -- Verify that it is the name of a generic package
4028
4029 -- A visibility glitch: if the instance is a child unit and the generic
4030 -- is the generic unit of a parent instance (i.e. both the parent and
4031 -- the child units are instances of the same package) the name now
4032 -- denotes the renaming within the parent, not the intended generic
4033 -- unit. See if there is a homonym that is the desired generic. The
4034 -- renaming declaration must be visible inside the instance of the
4035 -- child, but not when analyzing the name in the instantiation itself.
4036
4037 if Ekind (Gen_Unit) = E_Package
4038 and then Present (Renamed_Entity (Gen_Unit))
4039 and then In_Open_Scopes (Renamed_Entity (Gen_Unit))
4040 and then Is_Generic_Instance (Renamed_Entity (Gen_Unit))
4041 and then Present (Homonym (Gen_Unit))
4042 then
4043 Gen_Unit := Homonym (Gen_Unit);
4044 end if;
4045
4046 if Etype (Gen_Unit) = Any_Type then
4047 Restore_Env;
4048 goto Leave;
4049
4050 elsif Ekind (Gen_Unit) /= E_Generic_Package then
4051
4052 -- Ada 2005 (AI-50217): Cannot use instance in limited with_clause
4053
4054 if From_Limited_With (Gen_Unit) then
4055 Error_Msg_N
4056 ("cannot instantiate a limited withed package", Gen_Id);
4057 else
4058 Error_Msg_NE
4059 ("& is not the name of a generic package", Gen_Id, Gen_Unit);
4060 end if;
4061
4062 Restore_Env;
4063 goto Leave;
4064 end if;
4065
4066 if In_Extended_Main_Source_Unit (N) then
4067 Set_Is_Instantiated (Gen_Unit);
4068 Generate_Reference (Gen_Unit, N);
4069
4070 if Present (Renamed_Object (Gen_Unit)) then
4071 Set_Is_Instantiated (Renamed_Object (Gen_Unit));
4072 Generate_Reference (Renamed_Object (Gen_Unit), N);
4073 end if;
4074 end if;
4075
4076 if Nkind (Gen_Id) = N_Identifier
4077 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
4078 then
4079 Error_Msg_NE
4080 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
4081
4082 elsif Nkind (Gen_Id) = N_Expanded_Name
4083 and then Is_Child_Unit (Gen_Unit)
4084 and then Nkind (Prefix (Gen_Id)) = N_Identifier
4085 and then Chars (Act_Decl_Id) = Chars (Prefix (Gen_Id))
4086 then
4087 Error_Msg_N
4088 ("& is hidden within declaration of instance ", Prefix (Gen_Id));
4089 end if;
4090
4091 Set_Entity (Gen_Id, Gen_Unit);
4092
4093 -- If generic is a renaming, get original generic unit
4094
4095 if Present (Renamed_Object (Gen_Unit))
4096 and then Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Package
4097 then
4098 Gen_Unit := Renamed_Object (Gen_Unit);
4099 end if;
4100
4101 -- Verify that there are no circular instantiations
4102
4103 if In_Open_Scopes (Gen_Unit) then
4104 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
4105 Restore_Env;
4106 goto Leave;
4107
4108 elsif Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
4109 Error_Msg_Node_2 := Current_Scope;
4110 Error_Msg_NE
4111 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
4112 Circularity_Detected := True;
4113 Restore_Env;
4114 goto Leave;
4115
4116 else
4117 -- If the context of the instance is subject to SPARK_Mode "off" or
4118 -- the annotation is altogether missing, set the global flag which
4119 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
4120 -- the instance.
4121
4122 if SPARK_Mode /= On then
4123 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
4124
4125 -- Mark the instance spec in case the body is instantiated at a
4126 -- later pass. This preserves the original context in effect for
4127 -- the body.
4128
4129 Set_Ignore_SPARK_Mode_Pragmas (Act_Decl_Id);
4130 end if;
4131
4132 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
4133 Gen_Spec := Specification (Gen_Decl);
4134
4135 -- Initialize renamings map, for error checking, and the list that
4136 -- holds private entities whose views have changed between generic
4137 -- definition and instantiation. If this is the instance created to
4138 -- validate an actual package, the instantiation environment is that
4139 -- of the enclosing instance.
4140
4141 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
4142
4143 -- Copy original generic tree, to produce text for instantiation
4144
4145 Act_Tree :=
4146 Copy_Generic_Node
4147 (Original_Node (Gen_Decl), Empty, Instantiating => True);
4148
4149 Act_Spec := Specification (Act_Tree);
4150
4151 -- If this is the instance created to validate an actual package,
4152 -- only the formals matter, do not examine the package spec itself.
4153
4154 if Is_Actual_Pack then
4155 Set_Visible_Declarations (Act_Spec, New_List);
4156 Set_Private_Declarations (Act_Spec, New_List);
4157 end if;
4158
4159 Renaming_List :=
4160 Analyze_Associations
4161 (I_Node => N,
4162 Formals => Generic_Formal_Declarations (Act_Tree),
4163 F_Copy => Generic_Formal_Declarations (Gen_Decl));
4164
4165 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
4166
4167 Set_Instance_Env (Gen_Unit, Act_Decl_Id);
4168 Set_Defining_Unit_Name (Act_Spec, Act_Decl_Name);
4169 Set_Is_Generic_Instance (Act_Decl_Id);
4170 Set_Generic_Parent (Act_Spec, Gen_Unit);
4171
4172 -- References to the generic in its own declaration or its body are
4173 -- references to the instance. Add a renaming declaration for the
4174 -- generic unit itself. This declaration, as well as the renaming
4175 -- declarations for the generic formals, must remain private to the
4176 -- unit: the formals, because this is the language semantics, and
4177 -- the unit because its use is an artifact of the implementation.
4178
4179 Unit_Renaming :=
4180 Make_Package_Renaming_Declaration (Loc,
4181 Defining_Unit_Name =>
4182 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
4183 Name => New_Occurrence_Of (Act_Decl_Id, Loc));
4184
4185 Append (Unit_Renaming, Renaming_List);
4186
4187 -- The renaming declarations are the first local declarations of the
4188 -- new unit.
4189
4190 if Is_Non_Empty_List (Visible_Declarations (Act_Spec)) then
4191 Insert_List_Before
4192 (First (Visible_Declarations (Act_Spec)), Renaming_List);
4193 else
4194 Set_Visible_Declarations (Act_Spec, Renaming_List);
4195 end if;
4196
4197 Act_Decl := Make_Package_Declaration (Loc, Specification => Act_Spec);
4198
4199 -- Propagate the aspect specifications from the package declaration
4200 -- template to the instantiated version of the package declaration.
4201
4202 if Has_Aspects (Act_Tree) then
4203 Set_Aspect_Specifications (Act_Decl,
4204 New_Copy_List_Tree (Aspect_Specifications (Act_Tree)));
4205 end if;
4206
4207 -- The generic may have a generated Default_Storage_Pool aspect,
4208 -- set at the point of generic declaration. If the instance has
4209 -- that aspect, it overrides the one inherited from the generic.
4210
4211 if Has_Aspects (Gen_Spec) then
4212 if No (Aspect_Specifications (N)) then
4213 Set_Aspect_Specifications (N,
4214 (New_Copy_List_Tree
4215 (Aspect_Specifications (Gen_Spec))));
4216
4217 else
4218 declare
4219 Inherited_Aspects : constant List_Id :=
4220 New_Copy_List_Tree
4221 (Aspect_Specifications (Gen_Spec));
4222
4223 ASN1 : Node_Id;
4224 ASN2 : Node_Id;
4225 Pool_Present : Boolean := False;
4226
4227 begin
4228 ASN1 := First (Aspect_Specifications (N));
4229 while Present (ASN1) loop
4230 if Chars (Identifier (ASN1)) =
4231 Name_Default_Storage_Pool
4232 then
4233 Pool_Present := True;
4234 exit;
4235 end if;
4236
4237 Next (ASN1);
4238 end loop;
4239
4240 if Pool_Present then
4241
4242 -- If generic carries a default storage pool, remove it
4243 -- in favor of the instance one.
4244
4245 ASN2 := First (Inherited_Aspects);
4246 while Present (ASN2) loop
4247 if Chars (Identifier (ASN2)) =
4248 Name_Default_Storage_Pool
4249 then
4250 Remove (ASN2);
4251 exit;
4252 end if;
4253
4254 Next (ASN2);
4255 end loop;
4256 end if;
4257
4258 Prepend_List_To
4259 (Aspect_Specifications (N), Inherited_Aspects);
4260 end;
4261 end if;
4262 end if;
4263
4264 -- Save the instantiation node, for subsequent instantiation of the
4265 -- body, if there is one and we are generating code for the current
4266 -- unit. Mark unit as having a body (avoids premature error message).
4267
4268 -- We instantiate the body if we are generating code, if we are
4269 -- generating cross-reference information, or if we are building
4270 -- trees for ASIS use or GNATprove use.
4271
4272 declare
4273 Enclosing_Body_Present : Boolean := False;
4274 -- If the generic unit is not a compilation unit, then a body may
4275 -- be present in its parent even if none is required. We create a
4276 -- tentative pending instantiation for the body, which will be
4277 -- discarded if none is actually present.
4278
4279 Scop : Entity_Id;
4280
4281 begin
4282 if Scope (Gen_Unit) /= Standard_Standard
4283 and then not Is_Child_Unit (Gen_Unit)
4284 then
4285 Scop := Scope (Gen_Unit);
4286 while Present (Scop) and then Scop /= Standard_Standard loop
4287 if Unit_Requires_Body (Scop) then
4288 Enclosing_Body_Present := True;
4289 exit;
4290
4291 elsif In_Open_Scopes (Scop)
4292 and then In_Package_Body (Scop)
4293 then
4294 Enclosing_Body_Present := True;
4295 exit;
4296 end if;
4297
4298 exit when Is_Compilation_Unit (Scop);
4299 Scop := Scope (Scop);
4300 end loop;
4301 end if;
4302
4303 -- If front-end inlining is enabled or there are any subprograms
4304 -- marked with Inline_Always, and this is a unit for which code
4305 -- will be generated, we instantiate the body at once.
4306
4307 -- This is done if the instance is not the main unit, and if the
4308 -- generic is not a child unit of another generic, to avoid scope
4309 -- problems and the reinstallation of parent instances.
4310
4311 if Expander_Active
4312 and then (not Is_Child_Unit (Gen_Unit)
4313 or else not Is_Generic_Unit (Scope (Gen_Unit)))
4314 and then Might_Inline_Subp (Gen_Unit)
4315 and then not Is_Actual_Pack
4316 then
4317 if not Back_End_Inlining
4318 and then (Front_End_Inlining or else Has_Inline_Always)
4319 and then (Is_In_Main_Unit (N)
4320 or else In_Main_Context (Current_Scope))
4321 and then Nkind (Parent (N)) /= N_Compilation_Unit
4322 then
4323 Inline_Now := True;
4324
4325 -- In configurable_run_time mode we force the inlining of
4326 -- predefined subprograms marked Inline_Always, to minimize
4327 -- the use of the run-time library.
4328
4329 elsif In_Predefined_Unit (Gen_Decl)
4330 and then Configurable_Run_Time_Mode
4331 and then Nkind (Parent (N)) /= N_Compilation_Unit
4332 then
4333 Inline_Now := True;
4334 end if;
4335
4336 -- If the current scope is itself an instance within a child
4337 -- unit, there will be duplications in the scope stack, and the
4338 -- unstacking mechanism in Inline_Instance_Body will fail.
4339 -- This loses some rare cases of optimization, and might be
4340 -- improved some day, if we can find a proper abstraction for
4341 -- "the complete compilation context" that can be saved and
4342 -- restored. ???
4343
4344 if Is_Generic_Instance (Current_Scope) then
4345 declare
4346 Curr_Unit : constant Entity_Id :=
4347 Cunit_Entity (Current_Sem_Unit);
4348 begin
4349 if Curr_Unit /= Current_Scope
4350 and then Is_Child_Unit (Curr_Unit)
4351 then
4352 Inline_Now := False;
4353 end if;
4354 end;
4355 end if;
4356 end if;
4357
4358 Needs_Body :=
4359 (Unit_Requires_Body (Gen_Unit)
4360 or else Enclosing_Body_Present
4361 or else Present (Corresponding_Body (Gen_Decl)))
4362 and then (Is_In_Main_Unit (N)
4363 or else Might_Inline_Subp (Gen_Unit))
4364 and then not Is_Actual_Pack
4365 and then not Inline_Now
4366 and then (Operating_Mode = Generate_Code
4367
4368 -- Need comment for this check ???
4369
4370 or else (Operating_Mode = Check_Semantics
4371 and then (ASIS_Mode or GNATprove_Mode)));
4372
4373 -- If front-end inlining is enabled or there are any subprograms
4374 -- marked with Inline_Always, do not instantiate body when within
4375 -- a generic context.
4376
4377 if ((Front_End_Inlining or else Has_Inline_Always)
4378 and then not Expander_Active)
4379 or else Is_Generic_Unit (Cunit_Entity (Main_Unit))
4380 then
4381 Needs_Body := False;
4382 end if;
4383
4384 -- If the current context is generic, and the package being
4385 -- instantiated is declared within a formal package, there is no
4386 -- body to instantiate until the enclosing generic is instantiated
4387 -- and there is an actual for the formal package. If the formal
4388 -- package has parameters, we build a regular package instance for
4389 -- it, that precedes the original formal package declaration.
4390
4391 if In_Open_Scopes (Scope (Scope (Gen_Unit))) then
4392 declare
4393 Decl : constant Node_Id :=
4394 Original_Node
4395 (Unit_Declaration_Node (Scope (Gen_Unit)));
4396 begin
4397 if Nkind (Decl) = N_Formal_Package_Declaration
4398 or else (Nkind (Decl) = N_Package_Declaration
4399 and then Is_List_Member (Decl)
4400 and then Present (Next (Decl))
4401 and then
4402 Nkind (Next (Decl)) =
4403 N_Formal_Package_Declaration)
4404 then
4405 Needs_Body := False;
4406 end if;
4407 end;
4408 end if;
4409 end;
4410
4411 -- For RCI unit calling stubs, we omit the instance body if the
4412 -- instance is the RCI library unit itself.
4413
4414 -- However there is a special case for nested instances: in this case
4415 -- we do generate the instance body, as it might be required, e.g.
4416 -- because it provides stream attributes for some type used in the
4417 -- profile of a remote subprogram. This is consistent with 12.3(12),
4418 -- which indicates that the instance body occurs at the place of the
4419 -- instantiation, and thus is part of the RCI declaration, which is
4420 -- present on all client partitions (this is E.2.3(18)).
4421
4422 -- Note that AI12-0002 may make it illegal at some point to have
4423 -- stream attributes defined in an RCI unit, in which case this
4424 -- special case will become unnecessary. In the meantime, there
4425 -- is known application code in production that depends on this
4426 -- being possible, so we definitely cannot eliminate the body in
4427 -- the case of nested instances for the time being.
4428
4429 -- When we generate a nested instance body, calling stubs for any
4430 -- relevant subprogram will be be inserted immediately after the
4431 -- subprogram declarations, and will take precedence over the
4432 -- subsequent (original) body. (The stub and original body will be
4433 -- complete homographs, but this is permitted in an instance).
4434 -- (Could we do better and remove the original body???)
4435
4436 if Distribution_Stub_Mode = Generate_Caller_Stub_Body
4437 and then Comes_From_Source (N)
4438 and then Nkind (Parent (N)) = N_Compilation_Unit
4439 then
4440 Needs_Body := False;
4441 end if;
4442
4443 if Needs_Body then
4444
4445 -- Here is a defence against a ludicrous number of instantiations
4446 -- caused by a circular set of instantiation attempts.
4447
4448 if Pending_Instantiations.Last > Maximum_Instantiations then
4449 Error_Msg_Uint_1 := UI_From_Int (Maximum_Instantiations);
4450 Error_Msg_N ("too many instantiations, exceeds max of^", N);
4451 Error_Msg_N ("\limit can be changed using -gnateinn switch", N);
4452 raise Unrecoverable_Error;
4453 end if;
4454
4455 -- Indicate that the enclosing scopes contain an instantiation,
4456 -- and that cleanup actions should be delayed until after the
4457 -- instance body is expanded.
4458
4459 Check_Forward_Instantiation (Gen_Decl);
4460 if Nkind (N) = N_Package_Instantiation then
4461 declare
4462 Enclosing_Master : Entity_Id;
4463
4464 begin
4465 -- Loop to search enclosing masters
4466
4467 Enclosing_Master := Current_Scope;
4468 Scope_Loop : while Enclosing_Master /= Standard_Standard loop
4469 if Ekind (Enclosing_Master) = E_Package then
4470 if Is_Compilation_Unit (Enclosing_Master) then
4471 if In_Package_Body (Enclosing_Master) then
4472 Delay_Descriptors
4473 (Body_Entity (Enclosing_Master));
4474 else
4475 Delay_Descriptors
4476 (Enclosing_Master);
4477 end if;
4478
4479 exit Scope_Loop;
4480
4481 else
4482 Enclosing_Master := Scope (Enclosing_Master);
4483 end if;
4484
4485 elsif Is_Generic_Unit (Enclosing_Master)
4486 or else Ekind (Enclosing_Master) = E_Void
4487 then
4488 -- Cleanup actions will eventually be performed on the
4489 -- enclosing subprogram or package instance, if any.
4490 -- Enclosing scope is void in the formal part of a
4491 -- generic subprogram.
4492
4493 exit Scope_Loop;
4494
4495 else
4496 if Ekind (Enclosing_Master) = E_Entry
4497 and then
4498 Ekind (Scope (Enclosing_Master)) = E_Protected_Type
4499 then
4500 if not Expander_Active then
4501 exit Scope_Loop;
4502 else
4503 Enclosing_Master :=
4504 Protected_Body_Subprogram (Enclosing_Master);
4505 end if;
4506 end if;
4507
4508 Set_Delay_Cleanups (Enclosing_Master);
4509
4510 while Ekind (Enclosing_Master) = E_Block loop
4511 Enclosing_Master := Scope (Enclosing_Master);
4512 end loop;
4513
4514 if Is_Subprogram (Enclosing_Master) then
4515 Delay_Descriptors (Enclosing_Master);
4516
4517 elsif Is_Task_Type (Enclosing_Master) then
4518 declare
4519 TBP : constant Node_Id :=
4520 Get_Task_Body_Procedure
4521 (Enclosing_Master);
4522 begin
4523 if Present (TBP) then
4524 Delay_Descriptors (TBP);
4525 Set_Delay_Cleanups (TBP);
4526 end if;
4527 end;
4528 end if;
4529
4530 exit Scope_Loop;
4531 end if;
4532 end loop Scope_Loop;
4533 end;
4534
4535 -- Make entry in table
4536
4537 Add_Pending_Instantiation (N, Act_Decl);
4538 end if;
4539 end if;
4540
4541 Set_Categorization_From_Pragmas (Act_Decl);
4542
4543 if Parent_Installed then
4544 Hide_Current_Scope;
4545 end if;
4546
4547 Set_Instance_Spec (N, Act_Decl);
4548
4549 -- If not a compilation unit, insert the package declaration before
4550 -- the original instantiation node.
4551
4552 if Nkind (Parent (N)) /= N_Compilation_Unit then
4553 Mark_Rewrite_Insertion (Act_Decl);
4554 Insert_Before (N, Act_Decl);
4555
4556 if Has_Aspects (N) then
4557 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4558
4559 -- The pragma created for a Default_Storage_Pool aspect must
4560 -- appear ahead of the declarations in the instance spec.
4561 -- Analysis has placed it after the instance node, so remove
4562 -- it and reinsert it properly now.
4563
4564 declare
4565 ASN : constant Node_Id := First (Aspect_Specifications (N));
4566 A_Name : constant Name_Id := Chars (Identifier (ASN));
4567 Decl : Node_Id;
4568
4569 begin
4570 if A_Name = Name_Default_Storage_Pool then
4571 if No (Visible_Declarations (Act_Spec)) then
4572 Set_Visible_Declarations (Act_Spec, New_List);
4573 end if;
4574
4575 Decl := Next (N);
4576 while Present (Decl) loop
4577 if Nkind (Decl) = N_Pragma then
4578 Remove (Decl);
4579 Prepend (Decl, Visible_Declarations (Act_Spec));
4580 exit;
4581 end if;
4582
4583 Next (Decl);
4584 end loop;
4585 end if;
4586 end;
4587 end if;
4588
4589 Analyze (Act_Decl);
4590
4591 -- For an instantiation that is a compilation unit, place
4592 -- declaration on current node so context is complete for analysis
4593 -- (including nested instantiations). If this is the main unit,
4594 -- the declaration eventually replaces the instantiation node.
4595 -- If the instance body is created later, it replaces the
4596 -- instance node, and the declaration is attached to it
4597 -- (see Build_Instance_Compilation_Unit_Nodes).
4598
4599 else
4600 if Cunit_Entity (Current_Sem_Unit) = Defining_Entity (N) then
4601
4602 -- The entity for the current unit is the newly created one,
4603 -- and all semantic information is attached to it.
4604
4605 Set_Cunit_Entity (Current_Sem_Unit, Act_Decl_Id);
4606
4607 -- If this is the main unit, replace the main entity as well
4608
4609 if Current_Sem_Unit = Main_Unit then
4610 Main_Unit_Entity := Act_Decl_Id;
4611 end if;
4612 end if;
4613
4614 Set_Unit (Parent (N), Act_Decl);
4615 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
4616 Set_Package_Instantiation (Act_Decl_Id, N);
4617
4618 -- Process aspect specifications of the instance node, if any, to
4619 -- take into account categorization pragmas before analyzing the
4620 -- instance.
4621
4622 if Has_Aspects (N) then
4623 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4624 end if;
4625
4626 Analyze (Act_Decl);
4627 Set_Unit (Parent (N), N);
4628 Set_Body_Required (Parent (N), False);
4629
4630 -- We never need elaboration checks on instantiations, since by
4631 -- definition, the body instantiation is elaborated at the same
4632 -- time as the spec instantiation.
4633
4634 if Legacy_Elaboration_Checks then
4635 Set_Kill_Elaboration_Checks (Act_Decl_Id);
4636 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
4637 end if;
4638 end if;
4639
4640 if Legacy_Elaboration_Checks then
4641 Check_Elab_Instantiation (N);
4642 end if;
4643
4644 -- Save the scenario for later examination by the ABE Processing
4645 -- phase.
4646
4647 Record_Elaboration_Scenario (N);
4648
4649 -- The instantiation results in a guaranteed ABE
4650
4651 if Is_Known_Guaranteed_ABE (N) and then Needs_Body then
4652
4653 -- Do not instantiate the corresponding body because gigi cannot
4654 -- handle certain types of premature instantiations.
4655
4656 Pending_Instantiations.Decrement_Last;
4657
4658 -- Create completing bodies for all subprogram declarations since
4659 -- their real bodies will not be instantiated.
4660
4661 Provide_Completing_Bodies (Instance_Spec (N));
4662 end if;
4663
4664 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
4665
4666 Set_First_Private_Entity (Defining_Unit_Name (Unit_Renaming),
4667 First_Private_Entity (Act_Decl_Id));
4668
4669 -- If the instantiation will receive a body, the unit will be
4670 -- transformed into a package body, and receive its own elaboration
4671 -- entity. Otherwise, the nature of the unit is now a package
4672 -- declaration.
4673
4674 if Nkind (Parent (N)) = N_Compilation_Unit
4675 and then not Needs_Body
4676 then
4677 Rewrite (N, Act_Decl);
4678 end if;
4679
4680 if Present (Corresponding_Body (Gen_Decl))
4681 or else Unit_Requires_Body (Gen_Unit)
4682 then
4683 Set_Has_Completion (Act_Decl_Id);
4684 end if;
4685
4686 Check_Formal_Packages (Act_Decl_Id);
4687
4688 Restore_Hidden_Primitives (Vis_Prims_List);
4689 Restore_Private_Views (Act_Decl_Id);
4690
4691 Inherit_Context (Gen_Decl, N);
4692
4693 if Parent_Installed then
4694 Remove_Parent;
4695 end if;
4696
4697 Restore_Env;
4698 Env_Installed := False;
4699 end if;
4700
4701 Validate_Categorization_Dependency (N, Act_Decl_Id);
4702
4703 -- There used to be a check here to prevent instantiations in local
4704 -- contexts if the No_Local_Allocators restriction was active. This
4705 -- check was removed by a binding interpretation in AI-95-00130/07,
4706 -- but we retain the code for documentation purposes.
4707
4708 -- if Ekind (Act_Decl_Id) /= E_Void
4709 -- and then not Is_Library_Level_Entity (Act_Decl_Id)
4710 -- then
4711 -- Check_Restriction (No_Local_Allocators, N);
4712 -- end if;
4713
4714 if Inline_Now then
4715 Inline_Instance_Body (N, Gen_Unit, Act_Decl);
4716 end if;
4717
4718 -- The following is a tree patch for ASIS: ASIS needs separate nodes to
4719 -- be used as defining identifiers for a formal package and for the
4720 -- corresponding expanded package.
4721
4722 if Nkind (N) = N_Formal_Package_Declaration then
4723 Act_Decl_Id := New_Copy (Defining_Entity (N));
4724 Set_Comes_From_Source (Act_Decl_Id, True);
4725 Set_Is_Generic_Instance (Act_Decl_Id, False);
4726 Set_Defining_Identifier (N, Act_Decl_Id);
4727 end if;
4728
4729 -- Check that if N is an instantiation of System.Dim_Float_IO or
4730 -- System.Dim_Integer_IO, the formal type has a dimension system.
4731
4732 if Nkind (N) = N_Package_Instantiation
4733 and then Is_Dim_IO_Package_Instantiation (N)
4734 then
4735 declare
4736 Assoc : constant Node_Id := First (Generic_Associations (N));
4737 begin
4738 if not Has_Dimension_System
4739 (Etype (Explicit_Generic_Actual_Parameter (Assoc)))
4740 then
4741 Error_Msg_N ("type with a dimension system expected", Assoc);
4742 end if;
4743 end;
4744 end if;
4745
4746 <<Leave>>
4747 if Has_Aspects (N) and then Nkind (Parent (N)) /= N_Compilation_Unit then
4748 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4749 end if;
4750
4751 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
4752 Restore_Ghost_Region (Saved_GM, Saved_IGR);
4753 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
4754 Style_Check := Saved_Style_Check;
4755
4756 exception
4757 when Instantiation_Error =>
4758 if Parent_Installed then
4759 Remove_Parent;
4760 end if;
4761
4762 if Env_Installed then
4763 Restore_Env;
4764 end if;
4765
4766 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
4767 Restore_Ghost_Region (Saved_GM, Saved_IGR);
4768 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
4769 Style_Check := Saved_Style_Check;
4770 end Analyze_Package_Instantiation;
4771
4772 --------------------------
4773 -- Inline_Instance_Body --
4774 --------------------------
4775
4776 -- WARNING: This routine manages SPARK regions. Return statements must be
4777 -- replaced by gotos which jump to the end of the routine and restore the
4778 -- SPARK mode.
4779
4780 procedure Inline_Instance_Body
4781 (N : Node_Id;
4782 Gen_Unit : Entity_Id;
4783 Act_Decl : Node_Id)
4784 is
4785 Curr_Comp : constant Node_Id := Cunit (Current_Sem_Unit);
4786 Curr_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
4787 Gen_Comp : constant Entity_Id :=
4788 Cunit_Entity (Get_Source_Unit (Gen_Unit));
4789
4790 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
4791 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
4792 -- Save the SPARK mode-related data to restore on exit. Removing
4793 -- enclosing scopes to provide a clean environment for analysis of
4794 -- the inlined body will eliminate any previously set SPARK_Mode.
4795
4796 Scope_Stack_Depth : constant Pos :=
4797 Scope_Stack.Last - Scope_Stack.First + 1;
4798
4799 Inner_Scopes : array (1 .. Scope_Stack_Depth) of Entity_Id;
4800 Instances : array (1 .. Scope_Stack_Depth) of Entity_Id;
4801 Use_Clauses : array (1 .. Scope_Stack_Depth) of Node_Id;
4802
4803 Curr_Scope : Entity_Id := Empty;
4804 List : Elist_Id := No_Elist; -- init to avoid warning
4805 N_Instances : Nat := 0;
4806 Num_Inner : Nat := 0;
4807 Num_Scopes : Nat := 0;
4808 Removed : Boolean := False;
4809 S : Entity_Id;
4810 Vis : Boolean;
4811
4812 begin
4813 -- Case of generic unit defined in another unit. We must remove the
4814 -- complete context of the current unit to install that of the generic.
4815
4816 if Gen_Comp /= Cunit_Entity (Current_Sem_Unit) then
4817
4818 -- Add some comments for the following two loops ???
4819
4820 S := Current_Scope;
4821 while Present (S) and then S /= Standard_Standard loop
4822 loop
4823 Num_Scopes := Num_Scopes + 1;
4824
4825 Use_Clauses (Num_Scopes) :=
4826 (Scope_Stack.Table
4827 (Scope_Stack.Last - Num_Scopes + 1).
4828 First_Use_Clause);
4829 End_Use_Clauses (Use_Clauses (Num_Scopes));
4830
4831 exit when Scope_Stack.Last - Num_Scopes + 1 = Scope_Stack.First
4832 or else Scope_Stack.Table
4833 (Scope_Stack.Last - Num_Scopes).Entity = Scope (S);
4834 end loop;
4835
4836 exit when Is_Generic_Instance (S)
4837 and then (In_Package_Body (S)
4838 or else Ekind (S) = E_Procedure
4839 or else Ekind (S) = E_Function);
4840 S := Scope (S);
4841 end loop;
4842
4843 Vis := Is_Immediately_Visible (Gen_Comp);
4844
4845 -- Find and save all enclosing instances
4846
4847 S := Current_Scope;
4848
4849 while Present (S)
4850 and then S /= Standard_Standard
4851 loop
4852 if Is_Generic_Instance (S) then
4853 N_Instances := N_Instances + 1;
4854 Instances (N_Instances) := S;
4855
4856 exit when In_Package_Body (S);
4857 end if;
4858
4859 S := Scope (S);
4860 end loop;
4861
4862 -- Remove context of current compilation unit, unless we are within a
4863 -- nested package instantiation, in which case the context has been
4864 -- removed previously.
4865
4866 -- If current scope is the body of a child unit, remove context of
4867 -- spec as well. If an enclosing scope is an instance body, the
4868 -- context has already been removed, but the entities in the body
4869 -- must be made invisible as well.
4870
4871 S := Current_Scope;
4872 while Present (S) and then S /= Standard_Standard loop
4873 if Is_Generic_Instance (S)
4874 and then (In_Package_Body (S)
4875 or else Ekind_In (S, E_Procedure, E_Function))
4876 then
4877 -- We still have to remove the entities of the enclosing
4878 -- instance from direct visibility.
4879
4880 declare
4881 E : Entity_Id;
4882 begin
4883 E := First_Entity (S);
4884 while Present (E) loop
4885 Set_Is_Immediately_Visible (E, False);
4886 Next_Entity (E);
4887 end loop;
4888 end;
4889
4890 exit;
4891 end if;
4892
4893 if S = Curr_Unit
4894 or else (Ekind (Curr_Unit) = E_Package_Body
4895 and then S = Spec_Entity (Curr_Unit))
4896 or else (Ekind (Curr_Unit) = E_Subprogram_Body
4897 and then S = Corresponding_Spec
4898 (Unit_Declaration_Node (Curr_Unit)))
4899 then
4900 Removed := True;
4901
4902 -- Remove entities in current scopes from visibility, so that
4903 -- instance body is compiled in a clean environment.
4904
4905 List := Save_Scope_Stack (Handle_Use => False);
4906
4907 if Is_Child_Unit (S) then
4908
4909 -- Remove child unit from stack, as well as inner scopes.
4910 -- Removing the context of a child unit removes parent units
4911 -- as well.
4912
4913 while Current_Scope /= S loop
4914 Num_Inner := Num_Inner + 1;
4915 Inner_Scopes (Num_Inner) := Current_Scope;
4916 Pop_Scope;
4917 end loop;
4918
4919 Pop_Scope;
4920 Remove_Context (Curr_Comp);
4921 Curr_Scope := S;
4922
4923 else
4924 Remove_Context (Curr_Comp);
4925 end if;
4926
4927 if Ekind (Curr_Unit) = E_Package_Body then
4928 Remove_Context (Library_Unit (Curr_Comp));
4929 end if;
4930 end if;
4931
4932 S := Scope (S);
4933 end loop;
4934
4935 pragma Assert (Num_Inner < Num_Scopes);
4936
4937 -- The inlined package body must be analyzed with the SPARK_Mode of
4938 -- the enclosing context, otherwise the body may cause bogus errors
4939 -- if a configuration SPARK_Mode pragma in in effect.
4940
4941 Push_Scope (Standard_Standard);
4942 Scope_Stack.Table (Scope_Stack.Last).Is_Active_Stack_Base := True;
4943 Instantiate_Package_Body
4944 (Body_Info =>
4945 ((Inst_Node => N,
4946 Act_Decl => Act_Decl,
4947 Expander_Status => Expander_Active,
4948 Current_Sem_Unit => Current_Sem_Unit,
4949 Scope_Suppress => Scope_Suppress,
4950 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
4951 Version => Ada_Version,
4952 Version_Pragma => Ada_Version_Pragma,
4953 Warnings => Save_Warnings,
4954 SPARK_Mode => Saved_SM,
4955 SPARK_Mode_Pragma => Saved_SMP)),
4956 Inlined_Body => True);
4957
4958 Pop_Scope;
4959
4960 -- Restore context
4961
4962 Set_Is_Immediately_Visible (Gen_Comp, Vis);
4963
4964 -- Reset Generic_Instance flag so that use clauses can be installed
4965 -- in the proper order. (See Use_One_Package for effect of enclosing
4966 -- instances on processing of use clauses).
4967
4968 for J in 1 .. N_Instances loop
4969 Set_Is_Generic_Instance (Instances (J), False);
4970 end loop;
4971
4972 if Removed then
4973 Install_Context (Curr_Comp, Chain => False);
4974
4975 if Present (Curr_Scope)
4976 and then Is_Child_Unit (Curr_Scope)
4977 then
4978 Push_Scope (Curr_Scope);
4979 Set_Is_Immediately_Visible (Curr_Scope);
4980
4981 -- Finally, restore inner scopes as well
4982
4983 for J in reverse 1 .. Num_Inner loop
4984 Push_Scope (Inner_Scopes (J));
4985 end loop;
4986 end if;
4987
4988 Restore_Scope_Stack (List, Handle_Use => False);
4989
4990 if Present (Curr_Scope)
4991 and then
4992 (In_Private_Part (Curr_Scope)
4993 or else In_Package_Body (Curr_Scope))
4994 then
4995 -- Install private declaration of ancestor units, which are
4996 -- currently available. Restore_Scope_Stack and Install_Context
4997 -- only install the visible part of parents.
4998
4999 declare
5000 Par : Entity_Id;
5001 begin
5002 Par := Scope (Curr_Scope);
5003 while (Present (Par)) and then Par /= Standard_Standard loop
5004 Install_Private_Declarations (Par);
5005 Par := Scope (Par);
5006 end loop;
5007 end;
5008 end if;
5009 end if;
5010
5011 -- Restore use clauses. For a child unit, use clauses in the parents
5012 -- are restored when installing the context, so only those in inner
5013 -- scopes (and those local to the child unit itself) need to be
5014 -- installed explicitly.
5015
5016 if Is_Child_Unit (Curr_Unit) and then Removed then
5017 for J in reverse 1 .. Num_Inner + 1 loop
5018 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
5019 Use_Clauses (J);
5020 Install_Use_Clauses (Use_Clauses (J));
5021 end loop;
5022
5023 else
5024 for J in reverse 1 .. Num_Scopes loop
5025 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
5026 Use_Clauses (J);
5027 Install_Use_Clauses (Use_Clauses (J));
5028 end loop;
5029 end if;
5030
5031 -- Restore status of instances. If one of them is a body, make its
5032 -- local entities visible again.
5033
5034 declare
5035 E : Entity_Id;
5036 Inst : Entity_Id;
5037
5038 begin
5039 for J in 1 .. N_Instances loop
5040 Inst := Instances (J);
5041 Set_Is_Generic_Instance (Inst, True);
5042
5043 if In_Package_Body (Inst)
5044 or else Ekind_In (S, E_Procedure, E_Function)
5045 then
5046 E := First_Entity (Instances (J));
5047 while Present (E) loop
5048 Set_Is_Immediately_Visible (E);
5049 Next_Entity (E);
5050 end loop;
5051 end if;
5052 end loop;
5053 end;
5054
5055 -- If generic unit is in current unit, current context is correct. Note
5056 -- that the context is guaranteed to carry the correct SPARK_Mode as no
5057 -- enclosing scopes were removed.
5058
5059 else
5060 Instantiate_Package_Body
5061 (Body_Info =>
5062 ((Inst_Node => N,
5063 Act_Decl => Act_Decl,
5064 Expander_Status => Expander_Active,
5065 Current_Sem_Unit => Current_Sem_Unit,
5066 Scope_Suppress => Scope_Suppress,
5067 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
5068 Version => Ada_Version,
5069 Version_Pragma => Ada_Version_Pragma,
5070 Warnings => Save_Warnings,
5071 SPARK_Mode => SPARK_Mode,
5072 SPARK_Mode_Pragma => SPARK_Mode_Pragma)),
5073 Inlined_Body => True);
5074 end if;
5075 end Inline_Instance_Body;
5076
5077 -------------------------------------
5078 -- Analyze_Procedure_Instantiation --
5079 -------------------------------------
5080
5081 procedure Analyze_Procedure_Instantiation (N : Node_Id) is
5082 begin
5083 Analyze_Subprogram_Instantiation (N, E_Procedure);
5084 end Analyze_Procedure_Instantiation;
5085
5086 -----------------------------------
5087 -- Need_Subprogram_Instance_Body --
5088 -----------------------------------
5089
5090 function Need_Subprogram_Instance_Body
5091 (N : Node_Id;
5092 Subp : Entity_Id) return Boolean
5093 is
5094 function Is_Inlined_Or_Child_Of_Inlined (E : Entity_Id) return Boolean;
5095 -- Return True if E is an inlined subprogram, an inlined renaming or a
5096 -- subprogram nested in an inlined subprogram. The inlining machinery
5097 -- totally disregards nested subprograms since it considers that they
5098 -- will always be compiled if the parent is (see Inline.Is_Nested).
5099
5100 ------------------------------------
5101 -- Is_Inlined_Or_Child_Of_Inlined --
5102 ------------------------------------
5103
5104 function Is_Inlined_Or_Child_Of_Inlined (E : Entity_Id) return Boolean is
5105 Scop : Entity_Id;
5106
5107 begin
5108 if Is_Inlined (E) or else Is_Inlined (Alias (E)) then
5109 return True;
5110 end if;
5111
5112 Scop := Scope (E);
5113 while Scop /= Standard_Standard loop
5114 if Ekind (Scop) in Subprogram_Kind and then Is_Inlined (Scop) then
5115 return True;
5116 end if;
5117
5118 Scop := Scope (Scop);
5119 end loop;
5120
5121 return False;
5122 end Is_Inlined_Or_Child_Of_Inlined;
5123
5124 begin
5125 -- Must be in the main unit or inlined (or child of inlined)
5126
5127 if (Is_In_Main_Unit (N) or else Is_Inlined_Or_Child_Of_Inlined (Subp))
5128
5129 -- Must be generating code or analyzing code in ASIS/GNATprove mode
5130
5131 and then (Operating_Mode = Generate_Code
5132 or else (Operating_Mode = Check_Semantics
5133 and then (ASIS_Mode or GNATprove_Mode)))
5134
5135 -- The body is needed when generating code (full expansion), in ASIS
5136 -- mode for other tools, and in GNATprove mode (special expansion) for
5137 -- formal verification of the body itself.
5138
5139 and then (Expander_Active or ASIS_Mode or GNATprove_Mode)
5140
5141 -- No point in inlining if ABE is inevitable
5142
5143 and then not Is_Known_Guaranteed_ABE (N)
5144
5145 -- Or if subprogram is eliminated
5146
5147 and then not Is_Eliminated (Subp)
5148 then
5149 Add_Pending_Instantiation (N, Unit_Declaration_Node (Subp));
5150 return True;
5151
5152 -- Here if not inlined, or we ignore the inlining
5153
5154 else
5155 return False;
5156 end if;
5157 end Need_Subprogram_Instance_Body;
5158
5159 --------------------------------------
5160 -- Analyze_Subprogram_Instantiation --
5161 --------------------------------------
5162
5163 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
5164 -- must be replaced by gotos which jump to the end of the routine in order
5165 -- to restore the Ghost and SPARK modes.
5166
5167 procedure Analyze_Subprogram_Instantiation
5168 (N : Node_Id;
5169 K : Entity_Kind)
5170 is
5171 Loc : constant Source_Ptr := Sloc (N);
5172 Gen_Id : constant Node_Id := Name (N);
5173 Errs : constant Nat := Serious_Errors_Detected;
5174
5175 Anon_Id : constant Entity_Id :=
5176 Make_Defining_Identifier (Sloc (Defining_Entity (N)),
5177 Chars => New_External_Name
5178 (Chars (Defining_Entity (N)), 'R'));
5179
5180 Act_Decl_Id : Entity_Id := Empty; -- init to avoid warning
5181 Act_Decl : Node_Id;
5182 Act_Spec : Node_Id;
5183 Act_Tree : Node_Id;
5184
5185 Env_Installed : Boolean := False;
5186 Gen_Unit : Entity_Id;
5187 Gen_Decl : Node_Id;
5188 Pack_Id : Entity_Id;
5189 Parent_Installed : Boolean := False;
5190
5191 Renaming_List : List_Id;
5192 -- The list of declarations that link formals and actuals of the
5193 -- instance. These are subtype declarations for formal types, and
5194 -- renaming declarations for other formals. The subprogram declaration
5195 -- for the instance is then appended to the list, and the last item on
5196 -- the list is the renaming declaration for the instance.
5197
5198 procedure Analyze_Instance_And_Renamings;
5199 -- The instance must be analyzed in a context that includes the mappings
5200 -- of generic parameters into actuals. We create a package declaration
5201 -- for this purpose, and a subprogram with an internal name within the
5202 -- package. The subprogram instance is simply an alias for the internal
5203 -- subprogram, declared in the current scope.
5204
5205 procedure Build_Subprogram_Renaming;
5206 -- If the subprogram is recursive, there are occurrences of the name of
5207 -- the generic within the body, which must resolve to the current
5208 -- instance. We add a renaming declaration after the declaration, which
5209 -- is available in the instance body, as well as in the analysis of
5210 -- aspects that appear in the generic. This renaming declaration is
5211 -- inserted after the instance declaration which it renames.
5212
5213 ------------------------------------
5214 -- Analyze_Instance_And_Renamings --
5215 ------------------------------------
5216
5217 procedure Analyze_Instance_And_Renamings is
5218 Def_Ent : constant Entity_Id := Defining_Entity (N);
5219 Pack_Decl : Node_Id;
5220
5221 begin
5222 if Nkind (Parent (N)) = N_Compilation_Unit then
5223
5224 -- For the case of a compilation unit, the container package has
5225 -- the same name as the instantiation, to insure that the binder
5226 -- calls the elaboration procedure with the right name. Copy the
5227 -- entity of the instance, which may have compilation level flags
5228 -- (e.g. Is_Child_Unit) set.
5229
5230 Pack_Id := New_Copy (Def_Ent);
5231
5232 else
5233 -- Otherwise we use the name of the instantiation concatenated
5234 -- with its source position to ensure uniqueness if there are
5235 -- several instantiations with the same name.
5236
5237 Pack_Id :=
5238 Make_Defining_Identifier (Loc,
5239 Chars => New_External_Name
5240 (Related_Id => Chars (Def_Ent),
5241 Suffix => "GP",
5242 Suffix_Index => Source_Offset (Sloc (Def_Ent))));
5243 end if;
5244
5245 Pack_Decl :=
5246 Make_Package_Declaration (Loc,
5247 Specification => Make_Package_Specification (Loc,
5248 Defining_Unit_Name => Pack_Id,
5249 Visible_Declarations => Renaming_List,
5250 End_Label => Empty));
5251
5252 Set_Instance_Spec (N, Pack_Decl);
5253 Set_Is_Generic_Instance (Pack_Id);
5254 Set_Debug_Info_Needed (Pack_Id);
5255
5256 -- Case of not a compilation unit
5257
5258 if Nkind (Parent (N)) /= N_Compilation_Unit then
5259 Mark_Rewrite_Insertion (Pack_Decl);
5260 Insert_Before (N, Pack_Decl);
5261 Set_Has_Completion (Pack_Id);
5262
5263 -- Case of an instantiation that is a compilation unit
5264
5265 -- Place declaration on current node so context is complete for
5266 -- analysis (including nested instantiations), and for use in a
5267 -- context_clause (see Analyze_With_Clause).
5268
5269 else
5270 Set_Unit (Parent (N), Pack_Decl);
5271 Set_Parent_Spec (Pack_Decl, Parent_Spec (N));
5272 end if;
5273
5274 Analyze (Pack_Decl);
5275 Check_Formal_Packages (Pack_Id);
5276 Set_Is_Generic_Instance (Pack_Id, False);
5277
5278 -- Why do we clear Is_Generic_Instance??? We set it 20 lines
5279 -- above???
5280
5281 -- Body of the enclosing package is supplied when instantiating the
5282 -- subprogram body, after semantic analysis is completed.
5283
5284 if Nkind (Parent (N)) = N_Compilation_Unit then
5285
5286 -- Remove package itself from visibility, so it does not
5287 -- conflict with subprogram.
5288
5289 Set_Name_Entity_Id (Chars (Pack_Id), Homonym (Pack_Id));
5290
5291 -- Set name and scope of internal subprogram so that the proper
5292 -- external name will be generated. The proper scope is the scope
5293 -- of the wrapper package. We need to generate debugging info for
5294 -- the internal subprogram, so set flag accordingly.
5295
5296 Set_Chars (Anon_Id, Chars (Defining_Entity (N)));
5297 Set_Scope (Anon_Id, Scope (Pack_Id));
5298
5299 -- Mark wrapper package as referenced, to avoid spurious warnings
5300 -- if the instantiation appears in various with_ clauses of
5301 -- subunits of the main unit.
5302
5303 Set_Referenced (Pack_Id);
5304 end if;
5305
5306 Set_Is_Generic_Instance (Anon_Id);
5307 Set_Debug_Info_Needed (Anon_Id);
5308 Act_Decl_Id := New_Copy (Anon_Id);
5309
5310 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
5311 Set_Chars (Act_Decl_Id, Chars (Defining_Entity (N)));
5312 Set_Sloc (Act_Decl_Id, Sloc (Defining_Entity (N)));
5313
5314 -- Subprogram instance comes from source only if generic does
5315
5316 Set_Comes_From_Source (Act_Decl_Id, Comes_From_Source (Gen_Unit));
5317
5318 -- If the instance is a child unit, mark the Id accordingly. Mark
5319 -- the anonymous entity as well, which is the real subprogram and
5320 -- which is used when the instance appears in a context clause.
5321 -- Similarly, propagate the Is_Eliminated flag to handle properly
5322 -- nested eliminated subprograms.
5323
5324 Set_Is_Child_Unit (Act_Decl_Id, Is_Child_Unit (Defining_Entity (N)));
5325 Set_Is_Child_Unit (Anon_Id, Is_Child_Unit (Defining_Entity (N)));
5326 New_Overloaded_Entity (Act_Decl_Id);
5327 Check_Eliminated (Act_Decl_Id);
5328 Set_Is_Eliminated (Anon_Id, Is_Eliminated (Act_Decl_Id));
5329
5330 if Nkind (Parent (N)) = N_Compilation_Unit then
5331
5332 -- In compilation unit case, kill elaboration checks on the
5333 -- instantiation, since they are never needed - the body is
5334 -- instantiated at the same point as the spec.
5335
5336 if Legacy_Elaboration_Checks then
5337 Set_Kill_Elaboration_Checks (Act_Decl_Id);
5338 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
5339 end if;
5340
5341 Set_Is_Compilation_Unit (Anon_Id);
5342 Set_Cunit_Entity (Current_Sem_Unit, Pack_Id);
5343 end if;
5344
5345 -- The instance is not a freezing point for the new subprogram.
5346 -- The anonymous subprogram may have a freeze node, created for
5347 -- some delayed aspects. This freeze node must not be inherited
5348 -- by the visible subprogram entity.
5349
5350 Set_Is_Frozen (Act_Decl_Id, False);
5351 Set_Freeze_Node (Act_Decl_Id, Empty);
5352
5353 if Nkind (Defining_Entity (N)) = N_Defining_Operator_Symbol then
5354 Valid_Operator_Definition (Act_Decl_Id);
5355 end if;
5356
5357 Set_Alias (Act_Decl_Id, Anon_Id);
5358 Set_Has_Completion (Act_Decl_Id);
5359 Set_Related_Instance (Pack_Id, Act_Decl_Id);
5360
5361 if Nkind (Parent (N)) = N_Compilation_Unit then
5362 Set_Body_Required (Parent (N), False);
5363 end if;
5364 end Analyze_Instance_And_Renamings;
5365
5366 -------------------------------
5367 -- Build_Subprogram_Renaming --
5368 -------------------------------
5369
5370 procedure Build_Subprogram_Renaming is
5371 Renaming_Decl : Node_Id;
5372 Unit_Renaming : Node_Id;
5373
5374 begin
5375 Unit_Renaming :=
5376 Make_Subprogram_Renaming_Declaration (Loc,
5377 Specification =>
5378 Copy_Generic_Node
5379 (Specification (Original_Node (Gen_Decl)),
5380 Empty,
5381 Instantiating => True),
5382 Name => New_Occurrence_Of (Anon_Id, Loc));
5383
5384 -- The generic may be a a child unit. The renaming needs an
5385 -- identifier with the proper name.
5386
5387 Set_Defining_Unit_Name (Specification (Unit_Renaming),
5388 Make_Defining_Identifier (Loc, Chars (Gen_Unit)));
5389
5390 -- If there is a formal subprogram with the same name as the unit
5391 -- itself, do not add this renaming declaration, to prevent
5392 -- ambiguities when there is a call with that name in the body.
5393 -- This is a partial and ugly fix for one ACATS test. ???
5394
5395 Renaming_Decl := First (Renaming_List);
5396 while Present (Renaming_Decl) loop
5397 if Nkind (Renaming_Decl) = N_Subprogram_Renaming_Declaration
5398 and then
5399 Chars (Defining_Entity (Renaming_Decl)) = Chars (Gen_Unit)
5400 then
5401 exit;
5402 end if;
5403
5404 Next (Renaming_Decl);
5405 end loop;
5406
5407 if No (Renaming_Decl) then
5408 Append (Unit_Renaming, Renaming_List);
5409 end if;
5410 end Build_Subprogram_Renaming;
5411
5412 -- Local variables
5413
5414 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
5415 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
5416 Saved_ISMP : constant Boolean :=
5417 Ignore_SPARK_Mode_Pragmas_In_Instance;
5418 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
5419 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
5420 -- Save the Ghost and SPARK mode-related data to restore on exit
5421
5422 Vis_Prims_List : Elist_Id := No_Elist;
5423 -- List of primitives made temporarily visible in the instantiation
5424 -- to match the visibility of the formal type
5425
5426 -- Start of processing for Analyze_Subprogram_Instantiation
5427
5428 begin
5429 -- Preserve relevant elaboration-related attributes of the context which
5430 -- are no longer available or very expensive to recompute once analysis,
5431 -- resolution, and expansion are over.
5432
5433 Mark_Elaboration_Attributes
5434 (N_Id => N,
5435 Checks => True,
5436 Level => True,
5437 Modes => True,
5438 Warnings => True);
5439
5440 Check_SPARK_05_Restriction ("generic is not allowed", N);
5441
5442 -- Very first thing: check for special Text_IO unit in case we are
5443 -- instantiating one of the children of [[Wide_]Wide_]Text_IO. Of course
5444 -- such an instantiation is bogus (these are packages, not subprograms),
5445 -- but we get a better error message if we do this.
5446
5447 Check_Text_IO_Special_Unit (Gen_Id);
5448
5449 -- Make node global for error reporting
5450
5451 Instantiation_Node := N;
5452
5453 -- For package instantiations we turn off style checks, because they
5454 -- will have been emitted in the generic. For subprogram instantiations
5455 -- we want to apply at least the check on overriding indicators so we
5456 -- do not modify the style check status.
5457
5458 -- The renaming declarations for the actuals do not come from source and
5459 -- will not generate spurious warnings.
5460
5461 Preanalyze_Actuals (N);
5462
5463 Init_Env;
5464 Env_Installed := True;
5465 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
5466 Gen_Unit := Entity (Gen_Id);
5467
5468 -- A subprogram instantiation is Ghost when it is subject to pragma
5469 -- Ghost or the generic template is Ghost. Set the mode now to ensure
5470 -- that any nodes generated during analysis and expansion are marked as
5471 -- Ghost.
5472
5473 Mark_And_Set_Ghost_Instantiation (N, Gen_Unit);
5474
5475 Generate_Reference (Gen_Unit, Gen_Id);
5476
5477 if Nkind (Gen_Id) = N_Identifier
5478 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
5479 then
5480 Error_Msg_NE
5481 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
5482 end if;
5483
5484 if Etype (Gen_Unit) = Any_Type then
5485 Restore_Env;
5486 goto Leave;
5487 end if;
5488
5489 -- Verify that it is a generic subprogram of the right kind, and that
5490 -- it does not lead to a circular instantiation.
5491
5492 if K = E_Procedure and then Ekind (Gen_Unit) /= E_Generic_Procedure then
5493 Error_Msg_NE
5494 ("& is not the name of a generic procedure", Gen_Id, Gen_Unit);
5495
5496 elsif K = E_Function and then Ekind (Gen_Unit) /= E_Generic_Function then
5497 Error_Msg_NE
5498 ("& is not the name of a generic function", Gen_Id, Gen_Unit);
5499
5500 elsif In_Open_Scopes (Gen_Unit) then
5501 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
5502
5503 else
5504 Set_Entity (Gen_Id, Gen_Unit);
5505 Set_Is_Instantiated (Gen_Unit);
5506
5507 if In_Extended_Main_Source_Unit (N) then
5508 Generate_Reference (Gen_Unit, N);
5509 end if;
5510
5511 -- If renaming, get original unit
5512
5513 if Present (Renamed_Object (Gen_Unit))
5514 and then Ekind_In (Renamed_Object (Gen_Unit), E_Generic_Procedure,
5515 E_Generic_Function)
5516 then
5517 Gen_Unit := Renamed_Object (Gen_Unit);
5518 Set_Is_Instantiated (Gen_Unit);
5519 Generate_Reference (Gen_Unit, N);
5520 end if;
5521
5522 if Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
5523 Error_Msg_Node_2 := Current_Scope;
5524 Error_Msg_NE
5525 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
5526 Circularity_Detected := True;
5527 Restore_Hidden_Primitives (Vis_Prims_List);
5528 goto Leave;
5529 end if;
5530
5531 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
5532
5533 -- Initialize renamings map, for error checking
5534
5535 Generic_Renamings.Set_Last (0);
5536 Generic_Renamings_HTable.Reset;
5537
5538 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
5539
5540 -- Copy original generic tree, to produce text for instantiation
5541
5542 Act_Tree :=
5543 Copy_Generic_Node
5544 (Original_Node (Gen_Decl), Empty, Instantiating => True);
5545
5546 -- Inherit overriding indicator from instance node
5547
5548 Act_Spec := Specification (Act_Tree);
5549 Set_Must_Override (Act_Spec, Must_Override (N));
5550 Set_Must_Not_Override (Act_Spec, Must_Not_Override (N));
5551
5552 Renaming_List :=
5553 Analyze_Associations
5554 (I_Node => N,
5555 Formals => Generic_Formal_Declarations (Act_Tree),
5556 F_Copy => Generic_Formal_Declarations (Gen_Decl));
5557
5558 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
5559
5560 -- The subprogram itself cannot contain a nested instance, so the
5561 -- current parent is left empty.
5562
5563 Set_Instance_Env (Gen_Unit, Empty);
5564
5565 -- Build the subprogram declaration, which does not appear in the
5566 -- generic template, and give it a sloc consistent with that of the
5567 -- template.
5568
5569 Set_Defining_Unit_Name (Act_Spec, Anon_Id);
5570 Set_Generic_Parent (Act_Spec, Gen_Unit);
5571 Act_Decl :=
5572 Make_Subprogram_Declaration (Sloc (Act_Spec),
5573 Specification => Act_Spec);
5574
5575 -- The aspects have been copied previously, but they have to be
5576 -- linked explicitly to the new subprogram declaration. Explicit
5577 -- pre/postconditions on the instance are analyzed below, in a
5578 -- separate step.
5579
5580 Move_Aspects (Act_Tree, To => Act_Decl);
5581 Set_Categorization_From_Pragmas (Act_Decl);
5582
5583 if Parent_Installed then
5584 Hide_Current_Scope;
5585 end if;
5586
5587 Append (Act_Decl, Renaming_List);
5588
5589 -- Contract-related source pragmas that follow a generic subprogram
5590 -- must be instantiated explicitly because they are not part of the
5591 -- subprogram template.
5592
5593 Instantiate_Subprogram_Contract
5594 (Original_Node (Gen_Decl), Renaming_List);
5595
5596 Build_Subprogram_Renaming;
5597
5598 -- If the context of the instance is subject to SPARK_Mode "off" or
5599 -- the annotation is altogether missing, set the global flag which
5600 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
5601 -- the instance. This should be done prior to analyzing the instance.
5602
5603 if SPARK_Mode /= On then
5604 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
5605 end if;
5606
5607 -- If the context of an instance is not subject to SPARK_Mode "off",
5608 -- and the generic spec is subject to an explicit SPARK_Mode pragma,
5609 -- the latter should be the one applicable to the instance.
5610
5611 if not Ignore_SPARK_Mode_Pragmas_In_Instance
5612 and then Saved_SM /= Off
5613 and then Present (SPARK_Pragma (Gen_Unit))
5614 then
5615 Set_SPARK_Mode (Gen_Unit);
5616 end if;
5617
5618 Analyze_Instance_And_Renamings;
5619
5620 -- Restore SPARK_Mode from the context after analysis of the package
5621 -- declaration, so that the SPARK_Mode on the generic spec does not
5622 -- apply to the pending instance for the instance body.
5623
5624 if not Ignore_SPARK_Mode_Pragmas_In_Instance
5625 and then Saved_SM /= Off
5626 and then Present (SPARK_Pragma (Gen_Unit))
5627 then
5628 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
5629 end if;
5630
5631 -- If the generic is marked Import (Intrinsic), then so is the
5632 -- instance. This indicates that there is no body to instantiate. If
5633 -- generic is marked inline, so it the instance, and the anonymous
5634 -- subprogram it renames. If inlined, or else if inlining is enabled
5635 -- for the compilation, we generate the instance body even if it is
5636 -- not within the main unit.
5637
5638 if Is_Intrinsic_Subprogram (Gen_Unit) then
5639 Set_Is_Intrinsic_Subprogram (Anon_Id);
5640 Set_Is_Intrinsic_Subprogram (Act_Decl_Id);
5641
5642 if Chars (Gen_Unit) = Name_Unchecked_Conversion then
5643 Validate_Unchecked_Conversion (N, Act_Decl_Id);
5644 end if;
5645 end if;
5646
5647 -- Inherit convention from generic unit. Intrinsic convention, as for
5648 -- an instance of unchecked conversion, is not inherited because an
5649 -- explicit Ada instance has been created.
5650
5651 if Has_Convention_Pragma (Gen_Unit)
5652 and then Convention (Gen_Unit) /= Convention_Intrinsic
5653 then
5654 Set_Convention (Act_Decl_Id, Convention (Gen_Unit));
5655 Set_Is_Exported (Act_Decl_Id, Is_Exported (Gen_Unit));
5656 end if;
5657
5658 Generate_Definition (Act_Decl_Id);
5659
5660 -- Inherit all inlining-related flags which apply to the generic in
5661 -- the subprogram and its declaration.
5662
5663 Set_Is_Inlined (Act_Decl_Id, Is_Inlined (Gen_Unit));
5664 Set_Is_Inlined (Anon_Id, Is_Inlined (Gen_Unit));
5665
5666 Set_Has_Pragma_Inline (Act_Decl_Id, Has_Pragma_Inline (Gen_Unit));
5667 Set_Has_Pragma_Inline (Anon_Id, Has_Pragma_Inline (Gen_Unit));
5668
5669 -- Propagate No_Return if pragma applied to generic unit. This must
5670 -- be done explicitly because pragma does not appear in generic
5671 -- declaration (unlike the aspect case).
5672
5673 if No_Return (Gen_Unit) then
5674 Set_No_Return (Act_Decl_Id);
5675 Set_No_Return (Anon_Id);
5676 end if;
5677
5678 Set_Has_Pragma_Inline_Always
5679 (Act_Decl_Id, Has_Pragma_Inline_Always (Gen_Unit));
5680 Set_Has_Pragma_Inline_Always
5681 (Anon_Id, Has_Pragma_Inline_Always (Gen_Unit));
5682
5683 -- Mark both the instance spec and the anonymous package in case the
5684 -- body is instantiated at a later pass. This preserves the original
5685 -- context in effect for the body.
5686
5687 if SPARK_Mode /= On then
5688 Set_Ignore_SPARK_Mode_Pragmas (Act_Decl_Id);
5689 Set_Ignore_SPARK_Mode_Pragmas (Anon_Id);
5690 end if;
5691
5692 if Legacy_Elaboration_Checks
5693 and then not Is_Intrinsic_Subprogram (Gen_Unit)
5694 then
5695 Check_Elab_Instantiation (N);
5696 end if;
5697
5698 -- Save the scenario for later examination by the ABE Processing
5699 -- phase.
5700
5701 Record_Elaboration_Scenario (N);
5702
5703 -- The instantiation results in a guaranteed ABE. Create a completing
5704 -- body for the subprogram declaration because the real body will not
5705 -- be instantiated.
5706
5707 if Is_Known_Guaranteed_ABE (N) then
5708 Provide_Completing_Bodies (Instance_Spec (N));
5709 end if;
5710
5711 if Is_Dispatching_Operation (Act_Decl_Id)
5712 and then Ada_Version >= Ada_2005
5713 then
5714 declare
5715 Formal : Entity_Id;
5716
5717 begin
5718 Formal := First_Formal (Act_Decl_Id);
5719 while Present (Formal) loop
5720 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type
5721 and then Is_Controlling_Formal (Formal)
5722 and then not Can_Never_Be_Null (Formal)
5723 then
5724 Error_Msg_NE
5725 ("access parameter& is controlling,", N, Formal);
5726 Error_Msg_NE
5727 ("\corresponding parameter of & must be explicitly "
5728 & "null-excluding", N, Gen_Id);
5729 end if;
5730
5731 Next_Formal (Formal);
5732 end loop;
5733 end;
5734 end if;
5735
5736 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
5737
5738 Validate_Categorization_Dependency (N, Act_Decl_Id);
5739
5740 if not Is_Intrinsic_Subprogram (Act_Decl_Id) then
5741 Inherit_Context (Gen_Decl, N);
5742
5743 Restore_Private_Views (Pack_Id, False);
5744
5745 -- If the context requires a full instantiation, mark node for
5746 -- subsequent construction of the body.
5747
5748 if Need_Subprogram_Instance_Body (N, Act_Decl_Id) then
5749 Check_Forward_Instantiation (Gen_Decl);
5750
5751 -- The wrapper package is always delayed, because it does not
5752 -- constitute a freeze point, but to insure that the freeze node
5753 -- is placed properly, it is created directly when instantiating
5754 -- the body (otherwise the freeze node might appear to early for
5755 -- nested instantiations). For ASIS purposes, indicate that the
5756 -- wrapper package has replaced the instantiation node.
5757
5758 elsif Nkind (Parent (N)) = N_Compilation_Unit then
5759 Rewrite (N, Unit (Parent (N)));
5760 Set_Unit (Parent (N), N);
5761 end if;
5762
5763 -- Replace instance node for library-level instantiations of
5764 -- intrinsic subprograms, for ASIS use.
5765
5766 elsif Nkind (Parent (N)) = N_Compilation_Unit then
5767 Rewrite (N, Unit (Parent (N)));
5768 Set_Unit (Parent (N), N);
5769 end if;
5770
5771 if Parent_Installed then
5772 Remove_Parent;
5773 end if;
5774
5775 Restore_Hidden_Primitives (Vis_Prims_List);
5776 Restore_Env;
5777 Env_Installed := False;
5778 Generic_Renamings.Set_Last (0);
5779 Generic_Renamings_HTable.Reset;
5780 end if;
5781
5782 <<Leave>>
5783 -- Analyze aspects in declaration if no errors appear in the instance.
5784
5785 if Has_Aspects (N) and then Serious_Errors_Detected = Errs then
5786 Analyze_Aspect_Specifications (N, Act_Decl_Id);
5787 end if;
5788
5789 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
5790 Restore_Ghost_Region (Saved_GM, Saved_IGR);
5791 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
5792
5793 exception
5794 when Instantiation_Error =>
5795 if Parent_Installed then
5796 Remove_Parent;
5797 end if;
5798
5799 if Env_Installed then
5800 Restore_Env;
5801 end if;
5802
5803 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
5804 Restore_Ghost_Region (Saved_GM, Saved_IGR);
5805 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
5806 end Analyze_Subprogram_Instantiation;
5807
5808 -------------------------
5809 -- Get_Associated_Node --
5810 -------------------------
5811
5812 function Get_Associated_Node (N : Node_Id) return Node_Id is
5813 Assoc : Node_Id;
5814
5815 begin
5816 Assoc := Associated_Node (N);
5817
5818 if Nkind (Assoc) /= Nkind (N) then
5819 return Assoc;
5820
5821 elsif Nkind_In (Assoc, N_Aggregate, N_Extension_Aggregate) then
5822 return Assoc;
5823
5824 else
5825 -- If the node is part of an inner generic, it may itself have been
5826 -- remapped into a further generic copy. Associated_Node is otherwise
5827 -- used for the entity of the node, and will be of a different node
5828 -- kind, or else N has been rewritten as a literal or function call.
5829
5830 while Present (Associated_Node (Assoc))
5831 and then Nkind (Associated_Node (Assoc)) = Nkind (Assoc)
5832 loop
5833 Assoc := Associated_Node (Assoc);
5834 end loop;
5835
5836 -- Follow an additional link in case the final node was rewritten.
5837 -- This can only happen with nested generic units.
5838
5839 if (Nkind (Assoc) = N_Identifier or else Nkind (Assoc) in N_Op)
5840 and then Present (Associated_Node (Assoc))
5841 and then (Nkind_In (Associated_Node (Assoc), N_Function_Call,
5842 N_Explicit_Dereference,
5843 N_Integer_Literal,
5844 N_Real_Literal,
5845 N_String_Literal))
5846 then
5847 Assoc := Associated_Node (Assoc);
5848 end if;
5849
5850 -- An additional special case: an unconstrained type in an object
5851 -- declaration may have been rewritten as a local subtype constrained
5852 -- by the expression in the declaration. We need to recover the
5853 -- original entity, which may be global.
5854
5855 if Present (Original_Node (Assoc))
5856 and then Nkind (Parent (N)) = N_Object_Declaration
5857 then
5858 Assoc := Original_Node (Assoc);
5859 end if;
5860
5861 return Assoc;
5862 end if;
5863 end Get_Associated_Node;
5864
5865 ----------------------------
5866 -- Build_Function_Wrapper --
5867 ----------------------------
5868
5869 function Build_Function_Wrapper
5870 (Formal_Subp : Entity_Id;
5871 Actual_Subp : Entity_Id) return Node_Id
5872 is
5873 Loc : constant Source_Ptr := Sloc (Current_Scope);
5874 Ret_Type : constant Entity_Id := Get_Instance_Of (Etype (Formal_Subp));
5875 Actuals : List_Id;
5876 Decl : Node_Id;
5877 Func_Name : Node_Id;
5878 Func : Entity_Id;
5879 Parm_Type : Node_Id;
5880 Profile : List_Id := New_List;
5881 Spec : Node_Id;
5882 Act_F : Entity_Id;
5883 Form_F : Entity_Id;
5884 New_F : Entity_Id;
5885
5886 begin
5887 Func_Name := New_Occurrence_Of (Actual_Subp, Loc);
5888
5889 Func := Make_Defining_Identifier (Loc, Chars (Formal_Subp));
5890 Set_Ekind (Func, E_Function);
5891 Set_Is_Generic_Actual_Subprogram (Func);
5892
5893 Actuals := New_List;
5894 Profile := New_List;
5895
5896 Act_F := First_Formal (Actual_Subp);
5897 Form_F := First_Formal (Formal_Subp);
5898 while Present (Form_F) loop
5899
5900 -- Create new formal for profile of wrapper, and add a reference
5901 -- to it in the list of actuals for the enclosing call. The name
5902 -- must be that of the formal in the formal subprogram, because
5903 -- calls to it in the generic body may use named associations.
5904
5905 New_F := Make_Defining_Identifier (Loc, Chars (Form_F));
5906
5907 Parm_Type :=
5908 New_Occurrence_Of (Get_Instance_Of (Etype (Form_F)), Loc);
5909
5910 Append_To (Profile,
5911 Make_Parameter_Specification (Loc,
5912 Defining_Identifier => New_F,
5913 Parameter_Type => Parm_Type));
5914
5915 Append_To (Actuals, New_Occurrence_Of (New_F, Loc));
5916 Next_Formal (Form_F);
5917
5918 if Present (Act_F) then
5919 Next_Formal (Act_F);
5920 end if;
5921 end loop;
5922
5923 Spec :=
5924 Make_Function_Specification (Loc,
5925 Defining_Unit_Name => Func,
5926 Parameter_Specifications => Profile,
5927 Result_Definition => New_Occurrence_Of (Ret_Type, Loc));
5928
5929 Decl :=
5930 Make_Expression_Function (Loc,
5931 Specification => Spec,
5932 Expression =>
5933 Make_Function_Call (Loc,
5934 Name => Func_Name,
5935 Parameter_Associations => Actuals));
5936
5937 return Decl;
5938 end Build_Function_Wrapper;
5939
5940 ----------------------------
5941 -- Build_Operator_Wrapper --
5942 ----------------------------
5943
5944 function Build_Operator_Wrapper
5945 (Formal_Subp : Entity_Id;
5946 Actual_Subp : Entity_Id) return Node_Id
5947 is
5948 Loc : constant Source_Ptr := Sloc (Current_Scope);
5949 Ret_Type : constant Entity_Id :=
5950 Get_Instance_Of (Etype (Formal_Subp));
5951 Op_Type : constant Entity_Id :=
5952 Get_Instance_Of (Etype (First_Formal (Formal_Subp)));
5953 Is_Binary : constant Boolean :=
5954 Present (Next_Formal (First_Formal (Formal_Subp)));
5955
5956 Decl : Node_Id;
5957 Expr : Node_Id := Empty;
5958 F1, F2 : Entity_Id;
5959 Func : Entity_Id;
5960 Op_Name : Name_Id;
5961 Spec : Node_Id;
5962 L, R : Node_Id;
5963
5964 begin
5965 Op_Name := Chars (Actual_Subp);
5966
5967 -- Create entities for wrapper function and its formals
5968
5969 F1 := Make_Temporary (Loc, 'A');
5970 F2 := Make_Temporary (Loc, 'B');
5971 L := New_Occurrence_Of (F1, Loc);
5972 R := New_Occurrence_Of (F2, Loc);
5973
5974 Func := Make_Defining_Identifier (Loc, Chars (Formal_Subp));
5975 Set_Ekind (Func, E_Function);
5976 Set_Is_Generic_Actual_Subprogram (Func);
5977
5978 Spec :=
5979 Make_Function_Specification (Loc,
5980 Defining_Unit_Name => Func,
5981 Parameter_Specifications => New_List (
5982 Make_Parameter_Specification (Loc,
5983 Defining_Identifier => F1,
5984 Parameter_Type => New_Occurrence_Of (Op_Type, Loc))),
5985 Result_Definition => New_Occurrence_Of (Ret_Type, Loc));
5986
5987 if Is_Binary then
5988 Append_To (Parameter_Specifications (Spec),
5989 Make_Parameter_Specification (Loc,
5990 Defining_Identifier => F2,
5991 Parameter_Type => New_Occurrence_Of (Op_Type, Loc)));
5992 end if;
5993
5994 -- Build expression as a function call, or as an operator node
5995 -- that corresponds to the name of the actual, starting with
5996 -- binary operators.
5997
5998 if Op_Name not in Any_Operator_Name then
5999 Expr :=
6000 Make_Function_Call (Loc,
6001 Name =>
6002 New_Occurrence_Of (Actual_Subp, Loc),
6003 Parameter_Associations => New_List (L));
6004
6005 if Is_Binary then
6006 Append_To (Parameter_Associations (Expr), R);
6007 end if;
6008
6009 -- Binary operators
6010
6011 elsif Is_Binary then
6012 if Op_Name = Name_Op_And then
6013 Expr := Make_Op_And (Loc, Left_Opnd => L, Right_Opnd => R);
6014 elsif Op_Name = Name_Op_Or then
6015 Expr := Make_Op_Or (Loc, Left_Opnd => L, Right_Opnd => R);
6016 elsif Op_Name = Name_Op_Xor then
6017 Expr := Make_Op_Xor (Loc, Left_Opnd => L, Right_Opnd => R);
6018 elsif Op_Name = Name_Op_Eq then
6019 Expr := Make_Op_Eq (Loc, Left_Opnd => L, Right_Opnd => R);
6020 elsif Op_Name = Name_Op_Ne then
6021 Expr := Make_Op_Ne (Loc, Left_Opnd => L, Right_Opnd => R);
6022 elsif Op_Name = Name_Op_Le then
6023 Expr := Make_Op_Le (Loc, Left_Opnd => L, Right_Opnd => R);
6024 elsif Op_Name = Name_Op_Gt then
6025 Expr := Make_Op_Gt (Loc, Left_Opnd => L, Right_Opnd => R);
6026 elsif Op_Name = Name_Op_Ge then
6027 Expr := Make_Op_Ge (Loc, Left_Opnd => L, Right_Opnd => R);
6028 elsif Op_Name = Name_Op_Lt then
6029 Expr := Make_Op_Lt (Loc, Left_Opnd => L, Right_Opnd => R);
6030 elsif Op_Name = Name_Op_Add then
6031 Expr := Make_Op_Add (Loc, Left_Opnd => L, Right_Opnd => R);
6032 elsif Op_Name = Name_Op_Subtract then
6033 Expr := Make_Op_Subtract (Loc, Left_Opnd => L, Right_Opnd => R);
6034 elsif Op_Name = Name_Op_Concat then
6035 Expr := Make_Op_Concat (Loc, Left_Opnd => L, Right_Opnd => R);
6036 elsif Op_Name = Name_Op_Multiply then
6037 Expr := Make_Op_Multiply (Loc, Left_Opnd => L, Right_Opnd => R);
6038 elsif Op_Name = Name_Op_Divide then
6039 Expr := Make_Op_Divide (Loc, Left_Opnd => L, Right_Opnd => R);
6040 elsif Op_Name = Name_Op_Mod then
6041 Expr := Make_Op_Mod (Loc, Left_Opnd => L, Right_Opnd => R);
6042 elsif Op_Name = Name_Op_Rem then
6043 Expr := Make_Op_Rem (Loc, Left_Opnd => L, Right_Opnd => R);
6044 elsif Op_Name = Name_Op_Expon then
6045 Expr := Make_Op_Expon (Loc, Left_Opnd => L, Right_Opnd => R);
6046 end if;
6047
6048 -- Unary operators
6049
6050 else
6051 if Op_Name = Name_Op_Add then
6052 Expr := Make_Op_Plus (Loc, Right_Opnd => L);
6053 elsif Op_Name = Name_Op_Subtract then
6054 Expr := Make_Op_Minus (Loc, Right_Opnd => L);
6055 elsif Op_Name = Name_Op_Abs then
6056 Expr := Make_Op_Abs (Loc, Right_Opnd => L);
6057 elsif Op_Name = Name_Op_Not then
6058 Expr := Make_Op_Not (Loc, Right_Opnd => L);
6059 end if;
6060 end if;
6061
6062 Decl :=
6063 Make_Expression_Function (Loc,
6064 Specification => Spec,
6065 Expression => Expr);
6066
6067 return Decl;
6068 end Build_Operator_Wrapper;
6069
6070 -------------------------------------------
6071 -- Build_Instance_Compilation_Unit_Nodes --
6072 -------------------------------------------
6073
6074 procedure Build_Instance_Compilation_Unit_Nodes
6075 (N : Node_Id;
6076 Act_Body : Node_Id;
6077 Act_Decl : Node_Id)
6078 is
6079 Decl_Cunit : Node_Id;
6080 Body_Cunit : Node_Id;
6081 Citem : Node_Id;
6082 New_Main : constant Entity_Id := Defining_Entity (Act_Decl);
6083 Old_Main : constant Entity_Id := Cunit_Entity (Main_Unit);
6084
6085 begin
6086 -- A new compilation unit node is built for the instance declaration
6087
6088 Decl_Cunit :=
6089 Make_Compilation_Unit (Sloc (N),
6090 Context_Items => Empty_List,
6091 Unit => Act_Decl,
6092 Aux_Decls_Node => Make_Compilation_Unit_Aux (Sloc (N)));
6093
6094 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
6095
6096 -- The new compilation unit is linked to its body, but both share the
6097 -- same file, so we do not set Body_Required on the new unit so as not
6098 -- to create a spurious dependency on a non-existent body in the ali.
6099 -- This simplifies CodePeer unit traversal.
6100
6101 -- We use the original instantiation compilation unit as the resulting
6102 -- compilation unit of the instance, since this is the main unit.
6103
6104 Rewrite (N, Act_Body);
6105
6106 -- Propagate the aspect specifications from the package body template to
6107 -- the instantiated version of the package body.
6108
6109 if Has_Aspects (Act_Body) then
6110 Set_Aspect_Specifications
6111 (N, New_Copy_List_Tree (Aspect_Specifications (Act_Body)));
6112 end if;
6113
6114 Body_Cunit := Parent (N);
6115
6116 -- The two compilation unit nodes are linked by the Library_Unit field
6117
6118 Set_Library_Unit (Decl_Cunit, Body_Cunit);
6119 Set_Library_Unit (Body_Cunit, Decl_Cunit);
6120
6121 -- Preserve the private nature of the package if needed
6122
6123 Set_Private_Present (Decl_Cunit, Private_Present (Body_Cunit));
6124
6125 -- If the instance is not the main unit, its context, categorization
6126 -- and elaboration entity are not relevant to the compilation.
6127
6128 if Body_Cunit /= Cunit (Main_Unit) then
6129 Make_Instance_Unit (Body_Cunit, In_Main => False);
6130 return;
6131 end if;
6132
6133 -- The context clause items on the instantiation, which are now attached
6134 -- to the body compilation unit (since the body overwrote the original
6135 -- instantiation node), semantically belong on the spec, so copy them
6136 -- there. It's harmless to leave them on the body as well. In fact one
6137 -- could argue that they belong in both places.
6138
6139 Citem := First (Context_Items (Body_Cunit));
6140 while Present (Citem) loop
6141 Append (New_Copy (Citem), Context_Items (Decl_Cunit));
6142 Next (Citem);
6143 end loop;
6144
6145 -- Propagate categorization flags on packages, so that they appear in
6146 -- the ali file for the spec of the unit.
6147
6148 if Ekind (New_Main) = E_Package then
6149 Set_Is_Pure (Old_Main, Is_Pure (New_Main));
6150 Set_Is_Preelaborated (Old_Main, Is_Preelaborated (New_Main));
6151 Set_Is_Remote_Types (Old_Main, Is_Remote_Types (New_Main));
6152 Set_Is_Shared_Passive (Old_Main, Is_Shared_Passive (New_Main));
6153 Set_Is_Remote_Call_Interface
6154 (Old_Main, Is_Remote_Call_Interface (New_Main));
6155 end if;
6156
6157 -- Make entry in Units table, so that binder can generate call to
6158 -- elaboration procedure for body, if any.
6159
6160 Make_Instance_Unit (Body_Cunit, In_Main => True);
6161 Main_Unit_Entity := New_Main;
6162 Set_Cunit_Entity (Main_Unit, Main_Unit_Entity);
6163
6164 -- Build elaboration entity, since the instance may certainly generate
6165 -- elaboration code requiring a flag for protection.
6166
6167 Build_Elaboration_Entity (Decl_Cunit, New_Main);
6168 end Build_Instance_Compilation_Unit_Nodes;
6169
6170 -----------------------------
6171 -- Check_Access_Definition --
6172 -----------------------------
6173
6174 procedure Check_Access_Definition (N : Node_Id) is
6175 begin
6176 pragma Assert
6177 (Ada_Version >= Ada_2005 and then Present (Access_Definition (N)));
6178 null;
6179 end Check_Access_Definition;
6180
6181 -----------------------------------
6182 -- Check_Formal_Package_Instance --
6183 -----------------------------------
6184
6185 -- If the formal has specific parameters, they must match those of the
6186 -- actual. Both of them are instances, and the renaming declarations for
6187 -- their formal parameters appear in the same order in both. The analyzed
6188 -- formal has been analyzed in the context of the current instance.
6189
6190 procedure Check_Formal_Package_Instance
6191 (Formal_Pack : Entity_Id;
6192 Actual_Pack : Entity_Id)
6193 is
6194 E1 : Entity_Id := First_Entity (Actual_Pack);
6195 E2 : Entity_Id := First_Entity (Formal_Pack);
6196 Prev_E1 : Entity_Id;
6197
6198 Expr1 : Node_Id;
6199 Expr2 : Node_Id;
6200
6201 procedure Check_Mismatch (B : Boolean);
6202 -- Common error routine for mismatch between the parameters of the
6203 -- actual instance and those of the formal package.
6204
6205 function Same_Instantiated_Constant (E1, E2 : Entity_Id) return Boolean;
6206 -- The formal may come from a nested formal package, and the actual may
6207 -- have been constant-folded. To determine whether the two denote the
6208 -- same entity we may have to traverse several definitions to recover
6209 -- the ultimate entity that they refer to.
6210
6211 function Same_Instantiated_Function (E1, E2 : Entity_Id) return Boolean;
6212 -- The formal and the actual must be identical, but if both are
6213 -- given by attributes they end up renaming different generated bodies,
6214 -- and we must verify that the attributes themselves match.
6215
6216 function Same_Instantiated_Variable (E1, E2 : Entity_Id) return Boolean;
6217 -- Similarly, if the formal comes from a nested formal package, the
6218 -- actual may designate the formal through multiple renamings, which
6219 -- have to be followed to determine the original variable in question.
6220
6221 --------------------
6222 -- Check_Mismatch --
6223 --------------------
6224
6225 procedure Check_Mismatch (B : Boolean) is
6226 -- A Formal_Type_Declaration for a derived private type is rewritten
6227 -- as a private extension decl. (see Analyze_Formal_Derived_Type),
6228 -- which is why we examine the original node.
6229
6230 Kind : constant Node_Kind := Nkind (Original_Node (Parent (E2)));
6231
6232 begin
6233 if Kind = N_Formal_Type_Declaration then
6234 return;
6235
6236 elsif Nkind_In (Kind, N_Formal_Object_Declaration,
6237 N_Formal_Package_Declaration)
6238 or else Kind in N_Formal_Subprogram_Declaration
6239 then
6240 null;
6241
6242 -- Ada 2012: If both formal and actual are incomplete types they
6243 -- are conformant.
6244
6245 elsif Is_Incomplete_Type (E1) and then Is_Incomplete_Type (E2) then
6246 null;
6247
6248 elsif B then
6249 Error_Msg_NE
6250 ("actual for & in actual instance does not match formal",
6251 Parent (Actual_Pack), E1);
6252 end if;
6253 end Check_Mismatch;
6254
6255 --------------------------------
6256 -- Same_Instantiated_Constant --
6257 --------------------------------
6258
6259 function Same_Instantiated_Constant
6260 (E1, E2 : Entity_Id) return Boolean
6261 is
6262 Ent : Entity_Id;
6263
6264 begin
6265 Ent := E2;
6266 while Present (Ent) loop
6267 if E1 = Ent then
6268 return True;
6269
6270 elsif Ekind (Ent) /= E_Constant then
6271 return False;
6272
6273 elsif Is_Entity_Name (Constant_Value (Ent)) then
6274 if Entity (Constant_Value (Ent)) = E1 then
6275 return True;
6276 else
6277 Ent := Entity (Constant_Value (Ent));
6278 end if;
6279
6280 -- The actual may be a constant that has been folded. Recover
6281 -- original name.
6282
6283 elsif Is_Entity_Name (Original_Node (Constant_Value (Ent))) then
6284 Ent := Entity (Original_Node (Constant_Value (Ent)));
6285
6286 else
6287 return False;
6288 end if;
6289 end loop;
6290
6291 return False;
6292 end Same_Instantiated_Constant;
6293
6294 --------------------------------
6295 -- Same_Instantiated_Function --
6296 --------------------------------
6297
6298 function Same_Instantiated_Function
6299 (E1, E2 : Entity_Id) return Boolean
6300 is
6301 U1, U2 : Node_Id;
6302 begin
6303 if Alias (E1) = Alias (E2) then
6304 return True;
6305
6306 elsif Present (Alias (E2)) then
6307 U1 := Original_Node (Unit_Declaration_Node (E1));
6308 U2 := Original_Node (Unit_Declaration_Node (Alias (E2)));
6309
6310 return Nkind (U1) = N_Subprogram_Renaming_Declaration
6311 and then Nkind (Name (U1)) = N_Attribute_Reference
6312
6313 and then Nkind (U2) = N_Subprogram_Renaming_Declaration
6314 and then Nkind (Name (U2)) = N_Attribute_Reference
6315
6316 and then
6317 Attribute_Name (Name (U1)) = Attribute_Name (Name (U2));
6318 else
6319 return False;
6320 end if;
6321 end Same_Instantiated_Function;
6322
6323 --------------------------------
6324 -- Same_Instantiated_Variable --
6325 --------------------------------
6326
6327 function Same_Instantiated_Variable
6328 (E1, E2 : Entity_Id) return Boolean
6329 is
6330 function Original_Entity (E : Entity_Id) return Entity_Id;
6331 -- Follow chain of renamings to the ultimate ancestor
6332
6333 ---------------------
6334 -- Original_Entity --
6335 ---------------------
6336
6337 function Original_Entity (E : Entity_Id) return Entity_Id is
6338 Orig : Entity_Id;
6339
6340 begin
6341 Orig := E;
6342 while Nkind (Parent (Orig)) = N_Object_Renaming_Declaration
6343 and then Present (Renamed_Object (Orig))
6344 and then Is_Entity_Name (Renamed_Object (Orig))
6345 loop
6346 Orig := Entity (Renamed_Object (Orig));
6347 end loop;
6348
6349 return Orig;
6350 end Original_Entity;
6351
6352 -- Start of processing for Same_Instantiated_Variable
6353
6354 begin
6355 return Ekind (E1) = Ekind (E2)
6356 and then Original_Entity (E1) = Original_Entity (E2);
6357 end Same_Instantiated_Variable;
6358
6359 -- Start of processing for Check_Formal_Package_Instance
6360
6361 begin
6362 Prev_E1 := E1;
6363 while Present (E1) and then Present (E2) loop
6364 exit when Ekind (E1) = E_Package
6365 and then Renamed_Entity (E1) = Renamed_Entity (Actual_Pack);
6366
6367 -- If the formal is the renaming of the formal package, this
6368 -- is the end of its formal part, which may occur before the
6369 -- end of the formal part in the actual in the presence of
6370 -- defaulted parameters in the formal package.
6371
6372 exit when Nkind (Parent (E2)) = N_Package_Renaming_Declaration
6373 and then Renamed_Entity (E2) = Scope (E2);
6374
6375 -- The analysis of the actual may generate additional internal
6376 -- entities. If the formal is defaulted, there is no corresponding
6377 -- analysis and the internal entities must be skipped, until we
6378 -- find corresponding entities again.
6379
6380 if Comes_From_Source (E2)
6381 and then not Comes_From_Source (E1)
6382 and then Chars (E1) /= Chars (E2)
6383 then
6384 while Present (E1) and then Chars (E1) /= Chars (E2) loop
6385 Next_Entity (E1);
6386 end loop;
6387 end if;
6388
6389 if No (E1) then
6390 return;
6391
6392 -- Entities may be declared without full declaration, such as
6393 -- itypes and predefined operators (concatenation for arrays, eg).
6394 -- Skip it and keep the formal entity to find a later match for it.
6395
6396 elsif No (Parent (E2)) and then Ekind (E1) /= Ekind (E2) then
6397 E1 := Prev_E1;
6398 goto Next_E;
6399
6400 -- If the formal entity comes from a formal declaration, it was
6401 -- defaulted in the formal package, and no check is needed on it.
6402
6403 elsif Nkind_In (Original_Node (Parent (E2)),
6404 N_Formal_Object_Declaration,
6405 N_Formal_Type_Declaration)
6406 then
6407 -- If the formal is a tagged type the corresponding class-wide
6408 -- type has been generated as well, and it must be skipped.
6409
6410 if Is_Type (E2) and then Is_Tagged_Type (E2) then
6411 Next_Entity (E2);
6412 end if;
6413
6414 goto Next_E;
6415
6416 -- Ditto for defaulted formal subprograms.
6417
6418 elsif Is_Overloadable (E1)
6419 and then Nkind (Unit_Declaration_Node (E2)) in
6420 N_Formal_Subprogram_Declaration
6421 then
6422 goto Next_E;
6423
6424 elsif Is_Type (E1) then
6425
6426 -- Subtypes must statically match. E1, E2 are the local entities
6427 -- that are subtypes of the actuals. Itypes generated for other
6428 -- parameters need not be checked, the check will be performed
6429 -- on the parameters themselves.
6430
6431 -- If E2 is a formal type declaration, it is a defaulted parameter
6432 -- and needs no checking.
6433
6434 if not Is_Itype (E1) and then not Is_Itype (E2) then
6435 Check_Mismatch
6436 (not Is_Type (E2)
6437 or else Etype (E1) /= Etype (E2)
6438 or else not Subtypes_Statically_Match (E1, E2));
6439 end if;
6440
6441 elsif Ekind (E1) = E_Constant then
6442
6443 -- IN parameters must denote the same static value, or the same
6444 -- constant, or the literal null.
6445
6446 Expr1 := Expression (Parent (E1));
6447
6448 if Ekind (E2) /= E_Constant then
6449 Check_Mismatch (True);
6450 goto Next_E;
6451 else
6452 Expr2 := Expression (Parent (E2));
6453 end if;
6454
6455 if Is_OK_Static_Expression (Expr1) then
6456 if not Is_OK_Static_Expression (Expr2) then
6457 Check_Mismatch (True);
6458
6459 elsif Is_Discrete_Type (Etype (E1)) then
6460 declare
6461 V1 : constant Uint := Expr_Value (Expr1);
6462 V2 : constant Uint := Expr_Value (Expr2);
6463 begin
6464 Check_Mismatch (V1 /= V2);
6465 end;
6466
6467 elsif Is_Real_Type (Etype (E1)) then
6468 declare
6469 V1 : constant Ureal := Expr_Value_R (Expr1);
6470 V2 : constant Ureal := Expr_Value_R (Expr2);
6471 begin
6472 Check_Mismatch (V1 /= V2);
6473 end;
6474
6475 elsif Is_String_Type (Etype (E1))
6476 and then Nkind (Expr1) = N_String_Literal
6477 then
6478 if Nkind (Expr2) /= N_String_Literal then
6479 Check_Mismatch (True);
6480 else
6481 Check_Mismatch
6482 (not String_Equal (Strval (Expr1), Strval (Expr2)));
6483 end if;
6484 end if;
6485
6486 elsif Is_Entity_Name (Expr1) then
6487 if Is_Entity_Name (Expr2) then
6488 if Entity (Expr1) = Entity (Expr2) then
6489 null;
6490 else
6491 Check_Mismatch
6492 (not Same_Instantiated_Constant
6493 (Entity (Expr1), Entity (Expr2)));
6494 end if;
6495
6496 else
6497 Check_Mismatch (True);
6498 end if;
6499
6500 elsif Is_Entity_Name (Original_Node (Expr1))
6501 and then Is_Entity_Name (Expr2)
6502 and then Same_Instantiated_Constant
6503 (Entity (Original_Node (Expr1)), Entity (Expr2))
6504 then
6505 null;
6506
6507 elsif Nkind (Expr1) = N_Null then
6508 Check_Mismatch (Nkind (Expr1) /= N_Null);
6509
6510 else
6511 Check_Mismatch (True);
6512 end if;
6513
6514 elsif Ekind (E1) = E_Variable then
6515 Check_Mismatch (not Same_Instantiated_Variable (E1, E2));
6516
6517 elsif Ekind (E1) = E_Package then
6518 Check_Mismatch
6519 (Ekind (E1) /= Ekind (E2)
6520 or else (Present (Renamed_Object (E2))
6521 and then Renamed_Object (E1) /=
6522 Renamed_Object (E2)));
6523
6524 elsif Is_Overloadable (E1) then
6525 -- Verify that the actual subprograms match. Note that actuals
6526 -- that are attributes are rewritten as subprograms. If the
6527 -- subprogram in the formal package is defaulted, no check is
6528 -- needed. Note that this can only happen in Ada 2005 when the
6529 -- formal package can be partially parameterized.
6530
6531 if Nkind (Unit_Declaration_Node (E1)) =
6532 N_Subprogram_Renaming_Declaration
6533 and then From_Default (Unit_Declaration_Node (E1))
6534 then
6535 null;
6536
6537 -- If the formal package has an "others" box association that
6538 -- covers this formal, there is no need for a check either.
6539
6540 elsif Nkind (Unit_Declaration_Node (E2)) in
6541 N_Formal_Subprogram_Declaration
6542 and then Box_Present (Unit_Declaration_Node (E2))
6543 then
6544 null;
6545
6546 -- No check needed if subprogram is a defaulted null procedure
6547
6548 elsif No (Alias (E2))
6549 and then Ekind (E2) = E_Procedure
6550 and then
6551 Null_Present (Specification (Unit_Declaration_Node (E2)))
6552 then
6553 null;
6554
6555 -- Otherwise the actual in the formal and the actual in the
6556 -- instantiation of the formal must match, up to renamings.
6557
6558 else
6559 Check_Mismatch
6560 (Ekind (E2) /= Ekind (E1)
6561 or else not Same_Instantiated_Function (E1, E2));
6562 end if;
6563
6564 else
6565 raise Program_Error;
6566 end if;
6567
6568 <<Next_E>>
6569 Prev_E1 := E1;
6570 Next_Entity (E1);
6571 Next_Entity (E2);
6572 end loop;
6573 end Check_Formal_Package_Instance;
6574
6575 ---------------------------
6576 -- Check_Formal_Packages --
6577 ---------------------------
6578
6579 procedure Check_Formal_Packages (P_Id : Entity_Id) is
6580 E : Entity_Id;
6581 Formal_P : Entity_Id;
6582 Formal_Decl : Node_Id;
6583 begin
6584 -- Iterate through the declarations in the instance, looking for package
6585 -- renaming declarations that denote instances of formal packages. Stop
6586 -- when we find the renaming of the current package itself. The
6587 -- declaration for a formal package without a box is followed by an
6588 -- internal entity that repeats the instantiation.
6589
6590 E := First_Entity (P_Id);
6591 while Present (E) loop
6592 if Ekind (E) = E_Package then
6593 if Renamed_Object (E) = P_Id then
6594 exit;
6595
6596 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
6597 null;
6598
6599 else
6600 Formal_Decl := Parent (Associated_Formal_Package (E));
6601
6602 -- Nothing to check if the formal has a box or an others_clause
6603 -- (necessarily with a box).
6604
6605 if Box_Present (Formal_Decl) then
6606 null;
6607
6608 elsif Nkind (First (Generic_Associations (Formal_Decl))) =
6609 N_Others_Choice
6610 then
6611 -- The internal validating package was generated but formal
6612 -- and instance are known to be compatible.
6613
6614 Formal_P := Next_Entity (E);
6615 Remove (Unit_Declaration_Node (Formal_P));
6616
6617 else
6618 Formal_P := Next_Entity (E);
6619
6620 -- If the instance is within an enclosing instance body
6621 -- there is no need to verify the legality of current formal
6622 -- packages because they were legal in the generic body.
6623 -- This optimization may be applicable elsewhere, and it
6624 -- also removes spurious errors that may arise with
6625 -- on-the-fly inlining and confusion between private and
6626 -- full views.
6627
6628 if not In_Instance_Body then
6629 Check_Formal_Package_Instance (Formal_P, E);
6630 end if;
6631
6632 -- Restore the visibility of formals of the formal instance
6633 -- that are not defaulted, and are hidden within the current
6634 -- generic. These formals may be visible within an enclosing
6635 -- generic.
6636
6637 declare
6638 Elmt : Elmt_Id;
6639 begin
6640 Elmt := First_Elmt (Hidden_In_Formal_Instance (Formal_P));
6641 while Present (Elmt) loop
6642 Set_Is_Hidden (Node (Elmt), False);
6643 Next_Elmt (Elmt);
6644 end loop;
6645 end;
6646
6647 -- After checking, remove the internal validating package.
6648 -- It is only needed for semantic checks, and as it may
6649 -- contain generic formal declarations it should not reach
6650 -- gigi.
6651
6652 Remove (Unit_Declaration_Node (Formal_P));
6653 end if;
6654 end if;
6655 end if;
6656
6657 Next_Entity (E);
6658 end loop;
6659 end Check_Formal_Packages;
6660
6661 ---------------------------------
6662 -- Check_Forward_Instantiation --
6663 ---------------------------------
6664
6665 procedure Check_Forward_Instantiation (Decl : Node_Id) is
6666 S : Entity_Id;
6667 Gen_Comp : Entity_Id := Cunit_Entity (Get_Source_Unit (Decl));
6668
6669 begin
6670 -- The instantiation appears before the generic body if we are in the
6671 -- scope of the unit containing the generic, either in its spec or in
6672 -- the package body, and before the generic body.
6673
6674 if Ekind (Gen_Comp) = E_Package_Body then
6675 Gen_Comp := Spec_Entity (Gen_Comp);
6676 end if;
6677
6678 if In_Open_Scopes (Gen_Comp)
6679 and then No (Corresponding_Body (Decl))
6680 then
6681 S := Current_Scope;
6682
6683 while Present (S)
6684 and then not Is_Compilation_Unit (S)
6685 and then not Is_Child_Unit (S)
6686 loop
6687 if Ekind (S) = E_Package then
6688 Set_Has_Forward_Instantiation (S);
6689 end if;
6690
6691 S := Scope (S);
6692 end loop;
6693 end if;
6694 end Check_Forward_Instantiation;
6695
6696 ---------------------------
6697 -- Check_Generic_Actuals --
6698 ---------------------------
6699
6700 -- The visibility of the actuals may be different between the point of
6701 -- generic instantiation and the instantiation of the body.
6702
6703 procedure Check_Generic_Actuals
6704 (Instance : Entity_Id;
6705 Is_Formal_Box : Boolean)
6706 is
6707 E : Entity_Id;
6708 Astype : Entity_Id;
6709
6710 function Denotes_Previous_Actual (Typ : Entity_Id) return Boolean;
6711 -- For a formal that is an array type, the component type is often a
6712 -- previous formal in the same unit. The privacy status of the component
6713 -- type will have been examined earlier in the traversal of the
6714 -- corresponding actuals, and this status should not be modified for
6715 -- the array (sub)type itself. However, if the base type of the array
6716 -- (sub)type is private, its full view must be restored in the body to
6717 -- be consistent with subsequent index subtypes, etc.
6718 --
6719 -- To detect this case we have to rescan the list of formals, which is
6720 -- usually short enough to ignore the resulting inefficiency.
6721
6722 -----------------------------
6723 -- Denotes_Previous_Actual --
6724 -----------------------------
6725
6726 function Denotes_Previous_Actual (Typ : Entity_Id) return Boolean is
6727 Prev : Entity_Id;
6728
6729 begin
6730 Prev := First_Entity (Instance);
6731 while Present (Prev) loop
6732 if Is_Type (Prev)
6733 and then Nkind (Parent (Prev)) = N_Subtype_Declaration
6734 and then Is_Entity_Name (Subtype_Indication (Parent (Prev)))
6735 and then Entity (Subtype_Indication (Parent (Prev))) = Typ
6736 then
6737 return True;
6738
6739 elsif Prev = E then
6740 return False;
6741
6742 else
6743 Next_Entity (Prev);
6744 end if;
6745 end loop;
6746
6747 return False;
6748 end Denotes_Previous_Actual;
6749
6750 -- Start of processing for Check_Generic_Actuals
6751
6752 begin
6753 E := First_Entity (Instance);
6754 while Present (E) loop
6755 if Is_Type (E)
6756 and then Nkind (Parent (E)) = N_Subtype_Declaration
6757 and then Scope (Etype (E)) /= Instance
6758 and then Is_Entity_Name (Subtype_Indication (Parent (E)))
6759 then
6760 if Is_Array_Type (E)
6761 and then not Is_Private_Type (Etype (E))
6762 and then Denotes_Previous_Actual (Component_Type (E))
6763 then
6764 null;
6765 else
6766 Check_Private_View (Subtype_Indication (Parent (E)));
6767 end if;
6768
6769 Set_Is_Generic_Actual_Type (E, True);
6770 Set_Is_Hidden (E, False);
6771 Set_Is_Potentially_Use_Visible (E, In_Use (Instance));
6772
6773 -- We constructed the generic actual type as a subtype of the
6774 -- supplied type. This means that it normally would not inherit
6775 -- subtype specific attributes of the actual, which is wrong for
6776 -- the generic case.
6777
6778 Astype := Ancestor_Subtype (E);
6779
6780 if No (Astype) then
6781
6782 -- This can happen when E is an itype that is the full view of
6783 -- a private type completed, e.g. with a constrained array. In
6784 -- that case, use the first subtype, which will carry size
6785 -- information. The base type itself is unconstrained and will
6786 -- not carry it.
6787
6788 Astype := First_Subtype (E);
6789 end if;
6790
6791 Set_Size_Info (E, (Astype));
6792 Set_RM_Size (E, RM_Size (Astype));
6793 Set_First_Rep_Item (E, First_Rep_Item (Astype));
6794
6795 if Is_Discrete_Or_Fixed_Point_Type (E) then
6796 Set_RM_Size (E, RM_Size (Astype));
6797
6798 -- In nested instances, the base type of an access actual may
6799 -- itself be private, and need to be exchanged.
6800
6801 elsif Is_Access_Type (E)
6802 and then Is_Private_Type (Etype (E))
6803 then
6804 Check_Private_View
6805 (New_Occurrence_Of (Etype (E), Sloc (Instance)));
6806 end if;
6807
6808 elsif Ekind (E) = E_Package then
6809
6810 -- If this is the renaming for the current instance, we're done.
6811 -- Otherwise it is a formal package. If the corresponding formal
6812 -- was declared with a box, the (instantiations of the) generic
6813 -- formal part are also visible. Otherwise, ignore the entity
6814 -- created to validate the actuals.
6815
6816 if Renamed_Object (E) = Instance then
6817 exit;
6818
6819 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
6820 null;
6821
6822 -- The visibility of a formal of an enclosing generic is already
6823 -- correct.
6824
6825 elsif Denotes_Formal_Package (E) then
6826 null;
6827
6828 elsif Present (Associated_Formal_Package (E))
6829 and then not Is_Generic_Formal (E)
6830 then
6831 if Box_Present (Parent (Associated_Formal_Package (E))) then
6832 Check_Generic_Actuals (Renamed_Object (E), True);
6833
6834 else
6835 Check_Generic_Actuals (Renamed_Object (E), False);
6836 end if;
6837
6838 Set_Is_Hidden (E, False);
6839 end if;
6840
6841 -- If this is a subprogram instance (in a wrapper package) the
6842 -- actual is fully visible.
6843
6844 elsif Is_Wrapper_Package (Instance) then
6845 Set_Is_Hidden (E, False);
6846
6847 -- If the formal package is declared with a box, or if the formal
6848 -- parameter is defaulted, it is visible in the body.
6849
6850 elsif Is_Formal_Box or else Is_Visible_Formal (E) then
6851 Set_Is_Hidden (E, False);
6852 end if;
6853
6854 if Ekind (E) = E_Constant then
6855
6856 -- If the type of the actual is a private type declared in the
6857 -- enclosing scope of the generic unit, the body of the generic
6858 -- sees the full view of the type (because it has to appear in
6859 -- the corresponding package body). If the type is private now,
6860 -- exchange views to restore the proper visiblity in the instance.
6861
6862 declare
6863 Typ : constant Entity_Id := Base_Type (Etype (E));
6864 -- The type of the actual
6865
6866 Gen_Id : Entity_Id;
6867 -- The generic unit
6868
6869 Parent_Scope : Entity_Id;
6870 -- The enclosing scope of the generic unit
6871
6872 begin
6873 if Is_Wrapper_Package (Instance) then
6874 Gen_Id :=
6875 Generic_Parent
6876 (Specification
6877 (Unit_Declaration_Node
6878 (Related_Instance (Instance))));
6879 else
6880 Gen_Id :=
6881 Generic_Parent (Package_Specification (Instance));
6882 end if;
6883
6884 Parent_Scope := Scope (Gen_Id);
6885
6886 -- The exchange is only needed if the generic is defined
6887 -- within a package which is not a common ancestor of the
6888 -- scope of the instance, and is not already in scope.
6889
6890 if Is_Private_Type (Typ)
6891 and then Scope (Typ) = Parent_Scope
6892 and then Scope (Instance) /= Parent_Scope
6893 and then Ekind (Parent_Scope) = E_Package
6894 and then not Is_Child_Unit (Gen_Id)
6895 then
6896 Switch_View (Typ);
6897
6898 -- If the type of the entity is a subtype, it may also have
6899 -- to be made visible, together with the base type of its
6900 -- full view, after exchange.
6901
6902 if Is_Private_Type (Etype (E)) then
6903 Switch_View (Etype (E));
6904 Switch_View (Base_Type (Etype (E)));
6905 end if;
6906 end if;
6907 end;
6908 end if;
6909
6910 Next_Entity (E);
6911 end loop;
6912 end Check_Generic_Actuals;
6913
6914 ------------------------------
6915 -- Check_Generic_Child_Unit --
6916 ------------------------------
6917
6918 procedure Check_Generic_Child_Unit
6919 (Gen_Id : Node_Id;
6920 Parent_Installed : in out Boolean)
6921 is
6922 Loc : constant Source_Ptr := Sloc (Gen_Id);
6923 Gen_Par : Entity_Id := Empty;
6924 E : Entity_Id;
6925 Inst_Par : Entity_Id;
6926 S : Node_Id;
6927
6928 function Find_Generic_Child
6929 (Scop : Entity_Id;
6930 Id : Node_Id) return Entity_Id;
6931 -- Search generic parent for possible child unit with the given name
6932
6933 function In_Enclosing_Instance return Boolean;
6934 -- Within an instance of the parent, the child unit may be denoted by
6935 -- a simple name, or an abbreviated expanded name. Examine enclosing
6936 -- scopes to locate a possible parent instantiation.
6937
6938 ------------------------
6939 -- Find_Generic_Child --
6940 ------------------------
6941
6942 function Find_Generic_Child
6943 (Scop : Entity_Id;
6944 Id : Node_Id) return Entity_Id
6945 is
6946 E : Entity_Id;
6947
6948 begin
6949 -- If entity of name is already set, instance has already been
6950 -- resolved, e.g. in an enclosing instantiation.
6951
6952 if Present (Entity (Id)) then
6953 if Scope (Entity (Id)) = Scop then
6954 return Entity (Id);
6955 else
6956 return Empty;
6957 end if;
6958
6959 else
6960 E := First_Entity (Scop);
6961 while Present (E) loop
6962 if Chars (E) = Chars (Id)
6963 and then Is_Child_Unit (E)
6964 then
6965 if Is_Child_Unit (E)
6966 and then not Is_Visible_Lib_Unit (E)
6967 then
6968 Error_Msg_NE
6969 ("generic child unit& is not visible", Gen_Id, E);
6970 end if;
6971
6972 Set_Entity (Id, E);
6973 return E;
6974 end if;
6975
6976 Next_Entity (E);
6977 end loop;
6978
6979 return Empty;
6980 end if;
6981 end Find_Generic_Child;
6982
6983 ---------------------------
6984 -- In_Enclosing_Instance --
6985 ---------------------------
6986
6987 function In_Enclosing_Instance return Boolean is
6988 Enclosing_Instance : Node_Id;
6989 Instance_Decl : Node_Id;
6990
6991 begin
6992 -- We do not inline any call that contains instantiations, except
6993 -- for instantiations of Unchecked_Conversion, so if we are within
6994 -- an inlined body the current instance does not require parents.
6995
6996 if In_Inlined_Body then
6997 pragma Assert (Chars (Gen_Id) = Name_Unchecked_Conversion);
6998 return False;
6999 end if;
7000
7001 -- Loop to check enclosing scopes
7002
7003 Enclosing_Instance := Current_Scope;
7004 while Present (Enclosing_Instance) loop
7005 Instance_Decl := Unit_Declaration_Node (Enclosing_Instance);
7006
7007 if Ekind (Enclosing_Instance) = E_Package
7008 and then Is_Generic_Instance (Enclosing_Instance)
7009 and then Present
7010 (Generic_Parent (Specification (Instance_Decl)))
7011 then
7012 -- Check whether the generic we are looking for is a child of
7013 -- this instance.
7014
7015 E := Find_Generic_Child
7016 (Generic_Parent (Specification (Instance_Decl)), Gen_Id);
7017 exit when Present (E);
7018
7019 else
7020 E := Empty;
7021 end if;
7022
7023 Enclosing_Instance := Scope (Enclosing_Instance);
7024 end loop;
7025
7026 if No (E) then
7027
7028 -- Not a child unit
7029
7030 Analyze (Gen_Id);
7031 return False;
7032
7033 else
7034 Rewrite (Gen_Id,
7035 Make_Expanded_Name (Loc,
7036 Chars => Chars (E),
7037 Prefix => New_Occurrence_Of (Enclosing_Instance, Loc),
7038 Selector_Name => New_Occurrence_Of (E, Loc)));
7039
7040 Set_Entity (Gen_Id, E);
7041 Set_Etype (Gen_Id, Etype (E));
7042 Parent_Installed := False; -- Already in scope.
7043 return True;
7044 end if;
7045 end In_Enclosing_Instance;
7046
7047 -- Start of processing for Check_Generic_Child_Unit
7048
7049 begin
7050 -- If the name of the generic is given by a selected component, it may
7051 -- be the name of a generic child unit, and the prefix is the name of an
7052 -- instance of the parent, in which case the child unit must be visible.
7053 -- If this instance is not in scope, it must be placed there and removed
7054 -- after instantiation, because what is being instantiated is not the
7055 -- original child, but the corresponding child present in the instance
7056 -- of the parent.
7057
7058 -- If the child is instantiated within the parent, it can be given by
7059 -- a simple name. In this case the instance is already in scope, but
7060 -- the child generic must be recovered from the generic parent as well.
7061
7062 if Nkind (Gen_Id) = N_Selected_Component then
7063 S := Selector_Name (Gen_Id);
7064 Analyze (Prefix (Gen_Id));
7065 Inst_Par := Entity (Prefix (Gen_Id));
7066
7067 if Ekind (Inst_Par) = E_Package
7068 and then Present (Renamed_Object (Inst_Par))
7069 then
7070 Inst_Par := Renamed_Object (Inst_Par);
7071 end if;
7072
7073 if Ekind (Inst_Par) = E_Package then
7074 if Nkind (Parent (Inst_Par)) = N_Package_Specification then
7075 Gen_Par := Generic_Parent (Parent (Inst_Par));
7076
7077 elsif Nkind (Parent (Inst_Par)) = N_Defining_Program_Unit_Name
7078 and then
7079 Nkind (Parent (Parent (Inst_Par))) = N_Package_Specification
7080 then
7081 Gen_Par := Generic_Parent (Parent (Parent (Inst_Par)));
7082 end if;
7083
7084 elsif Ekind (Inst_Par) = E_Generic_Package
7085 and then Nkind (Parent (Gen_Id)) = N_Formal_Package_Declaration
7086 then
7087 -- A formal package may be a real child package, and not the
7088 -- implicit instance within a parent. In this case the child is
7089 -- not visible and has to be retrieved explicitly as well.
7090
7091 Gen_Par := Inst_Par;
7092 end if;
7093
7094 if Present (Gen_Par) then
7095
7096 -- The prefix denotes an instantiation. The entity itself may be a
7097 -- nested generic, or a child unit.
7098
7099 E := Find_Generic_Child (Gen_Par, S);
7100
7101 if Present (E) then
7102 Change_Selected_Component_To_Expanded_Name (Gen_Id);
7103 Set_Entity (Gen_Id, E);
7104 Set_Etype (Gen_Id, Etype (E));
7105 Set_Entity (S, E);
7106 Set_Etype (S, Etype (E));
7107
7108 -- Indicate that this is a reference to the parent
7109
7110 if In_Extended_Main_Source_Unit (Gen_Id) then
7111 Set_Is_Instantiated (Inst_Par);
7112 end if;
7113
7114 -- A common mistake is to replicate the naming scheme of a
7115 -- hierarchy by instantiating a generic child directly, rather
7116 -- than the implicit child in a parent instance:
7117
7118 -- generic .. package Gpar is ..
7119 -- generic .. package Gpar.Child is ..
7120 -- package Par is new Gpar ();
7121
7122 -- with Gpar.Child;
7123 -- package Par.Child is new Gpar.Child ();
7124 -- rather than Par.Child
7125
7126 -- In this case the instantiation is within Par, which is an
7127 -- instance, but Gpar does not denote Par because we are not IN
7128 -- the instance of Gpar, so this is illegal. The test below
7129 -- recognizes this particular case.
7130
7131 if Is_Child_Unit (E)
7132 and then not Comes_From_Source (Entity (Prefix (Gen_Id)))
7133 and then (not In_Instance
7134 or else Nkind (Parent (Parent (Gen_Id))) =
7135 N_Compilation_Unit)
7136 then
7137 Error_Msg_N
7138 ("prefix of generic child unit must be instance of parent",
7139 Gen_Id);
7140 end if;
7141
7142 if not In_Open_Scopes (Inst_Par)
7143 and then Nkind (Parent (Gen_Id)) not in
7144 N_Generic_Renaming_Declaration
7145 then
7146 Install_Parent (Inst_Par);
7147 Parent_Installed := True;
7148
7149 elsif In_Open_Scopes (Inst_Par) then
7150
7151 -- If the parent is already installed, install the actuals
7152 -- for its formal packages. This is necessary when the child
7153 -- instance is a child of the parent instance: in this case,
7154 -- the parent is placed on the scope stack but the formal
7155 -- packages are not made visible.
7156
7157 Install_Formal_Packages (Inst_Par);
7158 end if;
7159
7160 else
7161 -- If the generic parent does not contain an entity that
7162 -- corresponds to the selector, the instance doesn't either.
7163 -- Analyzing the node will yield the appropriate error message.
7164 -- If the entity is not a child unit, then it is an inner
7165 -- generic in the parent.
7166
7167 Analyze (Gen_Id);
7168 end if;
7169
7170 else
7171 Analyze (Gen_Id);
7172
7173 if Is_Child_Unit (Entity (Gen_Id))
7174 and then
7175 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
7176 and then not In_Open_Scopes (Inst_Par)
7177 then
7178 Install_Parent (Inst_Par);
7179 Parent_Installed := True;
7180
7181 -- The generic unit may be the renaming of the implicit child
7182 -- present in an instance. In that case the parent instance is
7183 -- obtained from the name of the renamed entity.
7184
7185 elsif Ekind (Entity (Gen_Id)) = E_Generic_Package
7186 and then Present (Renamed_Entity (Entity (Gen_Id)))
7187 and then Is_Child_Unit (Renamed_Entity (Entity (Gen_Id)))
7188 then
7189 declare
7190 Renamed_Package : constant Node_Id :=
7191 Name (Parent (Entity (Gen_Id)));
7192 begin
7193 if Nkind (Renamed_Package) = N_Expanded_Name then
7194 Inst_Par := Entity (Prefix (Renamed_Package));
7195 Install_Parent (Inst_Par);
7196 Parent_Installed := True;
7197 end if;
7198 end;
7199 end if;
7200 end if;
7201
7202 elsif Nkind (Gen_Id) = N_Expanded_Name then
7203
7204 -- Entity already present, analyze prefix, whose meaning may be an
7205 -- instance in the current context. If it is an instance of a
7206 -- relative within another, the proper parent may still have to be
7207 -- installed, if they are not of the same generation.
7208
7209 Analyze (Prefix (Gen_Id));
7210
7211 -- Prevent cascaded errors
7212
7213 if Etype (Prefix (Gen_Id)) = Any_Type then
7214 return;
7215 end if;
7216
7217 -- In the unlikely case that a local declaration hides the name of
7218 -- the parent package, locate it on the homonym chain. If the context
7219 -- is an instance of the parent, the renaming entity is flagged as
7220 -- such.
7221
7222 Inst_Par := Entity (Prefix (Gen_Id));
7223 while Present (Inst_Par)
7224 and then not Is_Package_Or_Generic_Package (Inst_Par)
7225 loop
7226 Inst_Par := Homonym (Inst_Par);
7227 end loop;
7228
7229 pragma Assert (Present (Inst_Par));
7230 Set_Entity (Prefix (Gen_Id), Inst_Par);
7231
7232 if In_Enclosing_Instance then
7233 null;
7234
7235 elsif Present (Entity (Gen_Id))
7236 and then Is_Child_Unit (Entity (Gen_Id))
7237 and then not In_Open_Scopes (Inst_Par)
7238 then
7239 Install_Parent (Inst_Par);
7240 Parent_Installed := True;
7241 end if;
7242
7243 elsif In_Enclosing_Instance then
7244
7245 -- The child unit is found in some enclosing scope
7246
7247 null;
7248
7249 else
7250 Analyze (Gen_Id);
7251
7252 -- If this is the renaming of the implicit child in a parent
7253 -- instance, recover the parent name and install it.
7254
7255 if Is_Entity_Name (Gen_Id) then
7256 E := Entity (Gen_Id);
7257
7258 if Is_Generic_Unit (E)
7259 and then Nkind (Parent (E)) in N_Generic_Renaming_Declaration
7260 and then Is_Child_Unit (Renamed_Object (E))
7261 and then Is_Generic_Unit (Scope (Renamed_Object (E)))
7262 and then Nkind (Name (Parent (E))) = N_Expanded_Name
7263 then
7264 Rewrite (Gen_Id, New_Copy_Tree (Name (Parent (E))));
7265 Inst_Par := Entity (Prefix (Gen_Id));
7266
7267 if not In_Open_Scopes (Inst_Par) then
7268 Install_Parent (Inst_Par);
7269 Parent_Installed := True;
7270 end if;
7271
7272 -- If it is a child unit of a non-generic parent, it may be
7273 -- use-visible and given by a direct name. Install parent as
7274 -- for other cases.
7275
7276 elsif Is_Generic_Unit (E)
7277 and then Is_Child_Unit (E)
7278 and then
7279 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
7280 and then not Is_Generic_Unit (Scope (E))
7281 then
7282 if not In_Open_Scopes (Scope (E)) then
7283 Install_Parent (Scope (E));
7284 Parent_Installed := True;
7285 end if;
7286 end if;
7287 end if;
7288 end if;
7289 end Check_Generic_Child_Unit;
7290
7291 -----------------------------
7292 -- Check_Hidden_Child_Unit --
7293 -----------------------------
7294
7295 procedure Check_Hidden_Child_Unit
7296 (N : Node_Id;
7297 Gen_Unit : Entity_Id;
7298 Act_Decl_Id : Entity_Id)
7299 is
7300 Gen_Id : constant Node_Id := Name (N);
7301
7302 begin
7303 if Is_Child_Unit (Gen_Unit)
7304 and then Is_Child_Unit (Act_Decl_Id)
7305 and then Nkind (Gen_Id) = N_Expanded_Name
7306 and then Entity (Prefix (Gen_Id)) = Scope (Act_Decl_Id)
7307 and then Chars (Gen_Unit) = Chars (Act_Decl_Id)
7308 then
7309 Error_Msg_Node_2 := Scope (Act_Decl_Id);
7310 Error_Msg_NE
7311 ("generic unit & is implicitly declared in &",
7312 Defining_Unit_Name (N), Gen_Unit);
7313 Error_Msg_N ("\instance must have different name",
7314 Defining_Unit_Name (N));
7315 end if;
7316 end Check_Hidden_Child_Unit;
7317
7318 ------------------------
7319 -- Check_Private_View --
7320 ------------------------
7321
7322 procedure Check_Private_View (N : Node_Id) is
7323 T : constant Entity_Id := Etype (N);
7324 BT : Entity_Id;
7325
7326 begin
7327 -- Exchange views if the type was not private in the generic but is
7328 -- private at the point of instantiation. Do not exchange views if
7329 -- the scope of the type is in scope. This can happen if both generic
7330 -- and instance are sibling units, or if type is defined in a parent.
7331 -- In this case the visibility of the type will be correct for all
7332 -- semantic checks.
7333
7334 if Present (T) then
7335 BT := Base_Type (T);
7336
7337 if Is_Private_Type (T)
7338 and then not Has_Private_View (N)
7339 and then Present (Full_View (T))
7340 and then not In_Open_Scopes (Scope (T))
7341 then
7342 -- In the generic, the full type was visible. Save the private
7343 -- entity, for subsequent exchange.
7344
7345 Switch_View (T);
7346
7347 elsif Has_Private_View (N)
7348 and then not Is_Private_Type (T)
7349 and then not Has_Been_Exchanged (T)
7350 and then Etype (Get_Associated_Node (N)) /= T
7351 then
7352 -- Only the private declaration was visible in the generic. If
7353 -- the type appears in a subtype declaration, the subtype in the
7354 -- instance must have a view compatible with that of its parent,
7355 -- which must be exchanged (see corresponding code in Restore_
7356 -- Private_Views). Otherwise, if the type is defined in a parent
7357 -- unit, leave full visibility within instance, which is safe.
7358
7359 if In_Open_Scopes (Scope (Base_Type (T)))
7360 and then not Is_Private_Type (Base_Type (T))
7361 and then Comes_From_Source (Base_Type (T))
7362 then
7363 null;
7364
7365 elsif Nkind (Parent (N)) = N_Subtype_Declaration
7366 or else not In_Private_Part (Scope (Base_Type (T)))
7367 then
7368 Prepend_Elmt (T, Exchanged_Views);
7369 Exchange_Declarations (Etype (Get_Associated_Node (N)));
7370 end if;
7371
7372 -- For composite types with inconsistent representation exchange
7373 -- component types accordingly.
7374
7375 elsif Is_Access_Type (T)
7376 and then Is_Private_Type (Designated_Type (T))
7377 and then not Has_Private_View (N)
7378 and then Present (Full_View (Designated_Type (T)))
7379 then
7380 Switch_View (Designated_Type (T));
7381
7382 elsif Is_Array_Type (T) then
7383 if Is_Private_Type (Component_Type (T))
7384 and then not Has_Private_View (N)
7385 and then Present (Full_View (Component_Type (T)))
7386 then
7387 Switch_View (Component_Type (T));
7388 end if;
7389
7390 -- The normal exchange mechanism relies on the setting of a
7391 -- flag on the reference in the generic. However, an additional
7392 -- mechanism is needed for types that are not explicitly
7393 -- mentioned in the generic, but may be needed in expanded code
7394 -- in the instance. This includes component types of arrays and
7395 -- designated types of access types. This processing must also
7396 -- include the index types of arrays which we take care of here.
7397
7398 declare
7399 Indx : Node_Id;
7400 Typ : Entity_Id;
7401
7402 begin
7403 Indx := First_Index (T);
7404 while Present (Indx) loop
7405 Typ := Base_Type (Etype (Indx));
7406
7407 if Is_Private_Type (Typ)
7408 and then Present (Full_View (Typ))
7409 then
7410 Switch_View (Typ);
7411 end if;
7412
7413 Next_Index (Indx);
7414 end loop;
7415 end;
7416
7417 elsif Is_Private_Type (T)
7418 and then Present (Full_View (T))
7419 and then Is_Array_Type (Full_View (T))
7420 and then Is_Private_Type (Component_Type (Full_View (T)))
7421 then
7422 Switch_View (T);
7423
7424 -- Finally, a non-private subtype may have a private base type, which
7425 -- must be exchanged for consistency. This can happen when a package
7426 -- body is instantiated, when the scope stack is empty but in fact
7427 -- the subtype and the base type are declared in an enclosing scope.
7428
7429 -- Note that in this case we introduce an inconsistency in the view
7430 -- set, because we switch the base type BT, but there could be some
7431 -- private dependent subtypes of BT which remain unswitched. Such
7432 -- subtypes might need to be switched at a later point (see specific
7433 -- provision for that case in Switch_View).
7434
7435 elsif not Is_Private_Type (T)
7436 and then not Has_Private_View (N)
7437 and then Is_Private_Type (BT)
7438 and then Present (Full_View (BT))
7439 and then not Is_Generic_Type (BT)
7440 and then not In_Open_Scopes (BT)
7441 then
7442 Prepend_Elmt (Full_View (BT), Exchanged_Views);
7443 Exchange_Declarations (BT);
7444 end if;
7445 end if;
7446 end Check_Private_View;
7447
7448 -----------------------------
7449 -- Check_Hidden_Primitives --
7450 -----------------------------
7451
7452 function Check_Hidden_Primitives (Assoc_List : List_Id) return Elist_Id is
7453 Actual : Node_Id;
7454 Gen_T : Entity_Id;
7455 Result : Elist_Id := No_Elist;
7456
7457 begin
7458 if No (Assoc_List) then
7459 return No_Elist;
7460 end if;
7461
7462 -- Traverse the list of associations between formals and actuals
7463 -- searching for renamings of tagged types
7464
7465 Actual := First (Assoc_List);
7466 while Present (Actual) loop
7467 if Nkind (Actual) = N_Subtype_Declaration then
7468 Gen_T := Generic_Parent_Type (Actual);
7469
7470 if Present (Gen_T) and then Is_Tagged_Type (Gen_T) then
7471
7472 -- Traverse the list of primitives of the actual types
7473 -- searching for hidden primitives that are visible in the
7474 -- corresponding generic formal; leave them visible and
7475 -- append them to Result to restore their decoration later.
7476
7477 Install_Hidden_Primitives
7478 (Prims_List => Result,
7479 Gen_T => Gen_T,
7480 Act_T => Entity (Subtype_Indication (Actual)));
7481 end if;
7482 end if;
7483
7484 Next (Actual);
7485 end loop;
7486
7487 return Result;
7488 end Check_Hidden_Primitives;
7489
7490 --------------------------
7491 -- Contains_Instance_Of --
7492 --------------------------
7493
7494 function Contains_Instance_Of
7495 (Inner : Entity_Id;
7496 Outer : Entity_Id;
7497 N : Node_Id) return Boolean
7498 is
7499 Elmt : Elmt_Id;
7500 Scop : Entity_Id;
7501
7502 begin
7503 Scop := Outer;
7504
7505 -- Verify that there are no circular instantiations. We check whether
7506 -- the unit contains an instance of the current scope or some enclosing
7507 -- scope (in case one of the instances appears in a subunit). Longer
7508 -- circularities involving subunits might seem too pathological to
7509 -- consider, but they were not too pathological for the authors of
7510 -- DEC bc30vsq, so we loop over all enclosing scopes, and mark all
7511 -- enclosing generic scopes as containing an instance.
7512
7513 loop
7514 -- Within a generic subprogram body, the scope is not generic, to
7515 -- allow for recursive subprograms. Use the declaration to determine
7516 -- whether this is a generic unit.
7517
7518 if Ekind (Scop) = E_Generic_Package
7519 or else (Is_Subprogram (Scop)
7520 and then Nkind (Unit_Declaration_Node (Scop)) =
7521 N_Generic_Subprogram_Declaration)
7522 then
7523 Elmt := First_Elmt (Inner_Instances (Inner));
7524
7525 while Present (Elmt) loop
7526 if Node (Elmt) = Scop then
7527 Error_Msg_Node_2 := Inner;
7528 Error_Msg_NE
7529 ("circular Instantiation: & instantiated within &!",
7530 N, Scop);
7531 return True;
7532
7533 elsif Node (Elmt) = Inner then
7534 return True;
7535
7536 elsif Contains_Instance_Of (Node (Elmt), Scop, N) then
7537 Error_Msg_Node_2 := Inner;
7538 Error_Msg_NE
7539 ("circular Instantiation: & instantiated within &!",
7540 N, Node (Elmt));
7541 return True;
7542 end if;
7543
7544 Next_Elmt (Elmt);
7545 end loop;
7546
7547 -- Indicate that Inner is being instantiated within Scop
7548
7549 Append_Elmt (Inner, Inner_Instances (Scop));
7550 end if;
7551
7552 if Scop = Standard_Standard then
7553 exit;
7554 else
7555 Scop := Scope (Scop);
7556 end if;
7557 end loop;
7558
7559 return False;
7560 end Contains_Instance_Of;
7561
7562 -----------------------
7563 -- Copy_Generic_Node --
7564 -----------------------
7565
7566 function Copy_Generic_Node
7567 (N : Node_Id;
7568 Parent_Id : Node_Id;
7569 Instantiating : Boolean) return Node_Id
7570 is
7571 Ent : Entity_Id;
7572 New_N : Node_Id;
7573
7574 function Copy_Generic_Descendant (D : Union_Id) return Union_Id;
7575 -- Check the given value of one of the Fields referenced by the current
7576 -- node to determine whether to copy it recursively. The field may hold
7577 -- a Node_Id, a List_Id, or an Elist_Id, or a plain value (Sloc, Uint,
7578 -- Char) in which case it need not be copied.
7579
7580 procedure Copy_Descendants;
7581 -- Common utility for various nodes
7582
7583 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id;
7584 -- Make copy of element list
7585
7586 function Copy_Generic_List
7587 (L : List_Id;
7588 Parent_Id : Node_Id) return List_Id;
7589 -- Apply Copy_Node recursively to the members of a node list
7590
7591 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean;
7592 -- True if an identifier is part of the defining program unit name of
7593 -- a child unit. The entity of such an identifier must be kept (for
7594 -- ASIS use) even though as the name of an enclosing generic it would
7595 -- otherwise not be preserved in the generic tree.
7596
7597 ----------------------
7598 -- Copy_Descendants --
7599 ----------------------
7600
7601 procedure Copy_Descendants is
7602 use Atree.Unchecked_Access;
7603 -- This code section is part of the implementation of an untyped
7604 -- tree traversal, so it needs direct access to node fields.
7605
7606 begin
7607 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
7608 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
7609 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
7610 Set_Field4 (New_N, Copy_Generic_Descendant (Field4 (N)));
7611 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
7612 end Copy_Descendants;
7613
7614 -----------------------------
7615 -- Copy_Generic_Descendant --
7616 -----------------------------
7617
7618 function Copy_Generic_Descendant (D : Union_Id) return Union_Id is
7619 begin
7620 if D = Union_Id (Empty) then
7621 return D;
7622
7623 elsif D in Node_Range then
7624 return Union_Id
7625 (Copy_Generic_Node (Node_Id (D), New_N, Instantiating));
7626
7627 elsif D in List_Range then
7628 return Union_Id (Copy_Generic_List (List_Id (D), New_N));
7629
7630 elsif D in Elist_Range then
7631 return Union_Id (Copy_Generic_Elist (Elist_Id (D)));
7632
7633 -- Nothing else is copyable (e.g. Uint values), return as is
7634
7635 else
7636 return D;
7637 end if;
7638 end Copy_Generic_Descendant;
7639
7640 ------------------------
7641 -- Copy_Generic_Elist --
7642 ------------------------
7643
7644 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id is
7645 M : Elmt_Id;
7646 L : Elist_Id;
7647
7648 begin
7649 if Present (E) then
7650 L := New_Elmt_List;
7651 M := First_Elmt (E);
7652 while Present (M) loop
7653 Append_Elmt
7654 (Copy_Generic_Node (Node (M), Empty, Instantiating), L);
7655 Next_Elmt (M);
7656 end loop;
7657
7658 return L;
7659
7660 else
7661 return No_Elist;
7662 end if;
7663 end Copy_Generic_Elist;
7664
7665 -----------------------
7666 -- Copy_Generic_List --
7667 -----------------------
7668
7669 function Copy_Generic_List
7670 (L : List_Id;
7671 Parent_Id : Node_Id) return List_Id
7672 is
7673 N : Node_Id;
7674 New_L : List_Id;
7675
7676 begin
7677 if Present (L) then
7678 New_L := New_List;
7679 Set_Parent (New_L, Parent_Id);
7680
7681 N := First (L);
7682 while Present (N) loop
7683 Append (Copy_Generic_Node (N, Empty, Instantiating), New_L);
7684 Next (N);
7685 end loop;
7686
7687 return New_L;
7688
7689 else
7690 return No_List;
7691 end if;
7692 end Copy_Generic_List;
7693
7694 ---------------------------
7695 -- In_Defining_Unit_Name --
7696 ---------------------------
7697
7698 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean is
7699 begin
7700 return
7701 Present (Parent (Nam))
7702 and then (Nkind (Parent (Nam)) = N_Defining_Program_Unit_Name
7703 or else
7704 (Nkind (Parent (Nam)) = N_Expanded_Name
7705 and then In_Defining_Unit_Name (Parent (Nam))));
7706 end In_Defining_Unit_Name;
7707
7708 -- Start of processing for Copy_Generic_Node
7709
7710 begin
7711 if N = Empty then
7712 return N;
7713 end if;
7714
7715 New_N := New_Copy (N);
7716
7717 -- Copy aspects if present
7718
7719 if Has_Aspects (N) then
7720 Set_Has_Aspects (New_N, False);
7721 Set_Aspect_Specifications
7722 (New_N, Copy_Generic_List (Aspect_Specifications (N), Parent_Id));
7723 end if;
7724
7725 -- If we are instantiating, we want to adjust the sloc based on the
7726 -- current S_Adjustment. However, if this is the root node of a subunit,
7727 -- we need to defer that adjustment to below (see "elsif Instantiating
7728 -- and Was_Stub"), so it comes after Create_Instantiation_Source has
7729 -- computed the adjustment.
7730
7731 if Instantiating
7732 and then not (Nkind (N) in N_Proper_Body
7733 and then Was_Originally_Stub (N))
7734 then
7735 Adjust_Instantiation_Sloc (New_N, S_Adjustment);
7736 end if;
7737
7738 if not Is_List_Member (N) then
7739 Set_Parent (New_N, Parent_Id);
7740 end if;
7741
7742 -- Special casing for identifiers and other entity names and operators
7743
7744 if Nkind_In (New_N, N_Character_Literal,
7745 N_Expanded_Name,
7746 N_Identifier,
7747 N_Operator_Symbol)
7748 or else Nkind (New_N) in N_Op
7749 then
7750 if not Instantiating then
7751
7752 -- Link both nodes in order to assign subsequently the entity of
7753 -- the copy to the original node, in case this is a global
7754 -- reference.
7755
7756 Set_Associated_Node (N, New_N);
7757
7758 -- If we are within an instantiation, this is a nested generic
7759 -- that has already been analyzed at the point of definition.
7760 -- We must preserve references that were global to the enclosing
7761 -- parent at that point. Other occurrences, whether global or
7762 -- local to the current generic, must be resolved anew, so we
7763 -- reset the entity in the generic copy. A global reference has a
7764 -- smaller depth than the parent, or else the same depth in case
7765 -- both are distinct compilation units.
7766
7767 -- A child unit is implicitly declared within the enclosing parent
7768 -- but is in fact global to it, and must be preserved.
7769
7770 -- It is also possible for Current_Instantiated_Parent to be
7771 -- defined, and for this not to be a nested generic, namely if
7772 -- the unit is loaded through Rtsfind. In that case, the entity of
7773 -- New_N is only a link to the associated node, and not a defining
7774 -- occurrence.
7775
7776 -- The entities for parent units in the defining_program_unit of a
7777 -- generic child unit are established when the context of the unit
7778 -- is first analyzed, before the generic copy is made. They are
7779 -- preserved in the copy for use in ASIS queries.
7780
7781 Ent := Entity (New_N);
7782
7783 if No (Current_Instantiated_Parent.Gen_Id) then
7784 if No (Ent)
7785 or else Nkind (Ent) /= N_Defining_Identifier
7786 or else not In_Defining_Unit_Name (N)
7787 then
7788 Set_Associated_Node (New_N, Empty);
7789 end if;
7790
7791 elsif No (Ent)
7792 or else
7793 not Nkind_In (Ent, N_Defining_Identifier,
7794 N_Defining_Character_Literal,
7795 N_Defining_Operator_Symbol)
7796 or else No (Scope (Ent))
7797 or else
7798 (Scope (Ent) = Current_Instantiated_Parent.Gen_Id
7799 and then not Is_Child_Unit (Ent))
7800 or else
7801 (Scope_Depth (Scope (Ent)) >
7802 Scope_Depth (Current_Instantiated_Parent.Gen_Id)
7803 and then
7804 Get_Source_Unit (Ent) =
7805 Get_Source_Unit (Current_Instantiated_Parent.Gen_Id))
7806 then
7807 Set_Associated_Node (New_N, Empty);
7808 end if;
7809
7810 -- Case of instantiating identifier or some other name or operator
7811
7812 else
7813 -- If the associated node is still defined, the entity in it
7814 -- is global, and must be copied to the instance. If this copy
7815 -- is being made for a body to inline, it is applied to an
7816 -- instantiated tree, and the entity is already present and
7817 -- must be also preserved.
7818
7819 declare
7820 Assoc : constant Node_Id := Get_Associated_Node (N);
7821
7822 begin
7823 if Present (Assoc) then
7824 if Nkind (Assoc) = Nkind (N) then
7825 Set_Entity (New_N, Entity (Assoc));
7826 Check_Private_View (N);
7827
7828 -- The node is a reference to a global type and acts as the
7829 -- subtype mark of a qualified expression created in order
7830 -- to aid resolution of accidental overloading in instances.
7831 -- Since N is a reference to a type, the Associated_Node of
7832 -- N denotes an entity rather than another identifier. See
7833 -- Qualify_Universal_Operands for details.
7834
7835 elsif Nkind (N) = N_Identifier
7836 and then Nkind (Parent (N)) = N_Qualified_Expression
7837 and then Subtype_Mark (Parent (N)) = N
7838 and then Is_Qualified_Universal_Literal (Parent (N))
7839 then
7840 Set_Entity (New_N, Assoc);
7841
7842 -- The name in the call may be a selected component if the
7843 -- call has not been analyzed yet, as may be the case for
7844 -- pre/post conditions in a generic unit.
7845
7846 elsif Nkind (Assoc) = N_Function_Call
7847 and then Is_Entity_Name (Name (Assoc))
7848 then
7849 Set_Entity (New_N, Entity (Name (Assoc)));
7850
7851 elsif Nkind_In (Assoc, N_Defining_Identifier,
7852 N_Defining_Character_Literal,
7853 N_Defining_Operator_Symbol)
7854 and then Expander_Active
7855 then
7856 -- Inlining case: we are copying a tree that contains
7857 -- global entities, which are preserved in the copy to be
7858 -- used for subsequent inlining.
7859
7860 null;
7861
7862 else
7863 Set_Entity (New_N, Empty);
7864 end if;
7865 end if;
7866 end;
7867 end if;
7868
7869 -- For expanded name, we must copy the Prefix and Selector_Name
7870
7871 if Nkind (N) = N_Expanded_Name then
7872 Set_Prefix
7873 (New_N, Copy_Generic_Node (Prefix (N), New_N, Instantiating));
7874
7875 Set_Selector_Name (New_N,
7876 Copy_Generic_Node (Selector_Name (N), New_N, Instantiating));
7877
7878 -- For operators, copy the operands
7879
7880 elsif Nkind (N) in N_Op then
7881 if Nkind (N) in N_Binary_Op then
7882 Set_Left_Opnd (New_N,
7883 Copy_Generic_Node (Left_Opnd (N), New_N, Instantiating));
7884 end if;
7885
7886 Set_Right_Opnd (New_N,
7887 Copy_Generic_Node (Right_Opnd (N), New_N, Instantiating));
7888 end if;
7889
7890 -- Establish a link between an entity from the generic template and the
7891 -- corresponding entity in the generic copy to be analyzed.
7892
7893 elsif Nkind (N) in N_Entity then
7894 if not Instantiating then
7895 Set_Associated_Entity (N, New_N);
7896 end if;
7897
7898 -- Clear any existing link the copy may inherit from the replicated
7899 -- generic template entity.
7900
7901 Set_Associated_Entity (New_N, Empty);
7902
7903 -- Special casing for stubs
7904
7905 elsif Nkind (N) in N_Body_Stub then
7906
7907 -- In any case, we must copy the specification or defining
7908 -- identifier as appropriate.
7909
7910 if Nkind (N) = N_Subprogram_Body_Stub then
7911 Set_Specification (New_N,
7912 Copy_Generic_Node (Specification (N), New_N, Instantiating));
7913
7914 else
7915 Set_Defining_Identifier (New_N,
7916 Copy_Generic_Node
7917 (Defining_Identifier (N), New_N, Instantiating));
7918 end if;
7919
7920 -- If we are not instantiating, then this is where we load and
7921 -- analyze subunits, i.e. at the point where the stub occurs. A
7922 -- more permissive system might defer this analysis to the point
7923 -- of instantiation, but this seems too complicated for now.
7924
7925 if not Instantiating then
7926 declare
7927 Subunit_Name : constant Unit_Name_Type := Get_Unit_Name (N);
7928 Subunit : Node_Id;
7929 Unum : Unit_Number_Type;
7930 New_Body : Node_Id;
7931
7932 begin
7933 -- Make sure that, if it is a subunit of the main unit that is
7934 -- preprocessed and if -gnateG is specified, the preprocessed
7935 -- file will be written.
7936
7937 Lib.Analysing_Subunit_Of_Main :=
7938 Lib.In_Extended_Main_Source_Unit (N);
7939 Unum :=
7940 Load_Unit
7941 (Load_Name => Subunit_Name,
7942 Required => False,
7943 Subunit => True,
7944 Error_Node => N);
7945 Lib.Analysing_Subunit_Of_Main := False;
7946
7947 -- If the proper body is not found, a warning message will be
7948 -- emitted when analyzing the stub, or later at the point of
7949 -- instantiation. Here we just leave the stub as is.
7950
7951 if Unum = No_Unit then
7952 Subunits_Missing := True;
7953 goto Subunit_Not_Found;
7954 end if;
7955
7956 Subunit := Cunit (Unum);
7957
7958 if Nkind (Unit (Subunit)) /= N_Subunit then
7959 Error_Msg_N
7960 ("found child unit instead of expected SEPARATE subunit",
7961 Subunit);
7962 Error_Msg_Sloc := Sloc (N);
7963 Error_Msg_N ("\to complete stub #", Subunit);
7964 goto Subunit_Not_Found;
7965 end if;
7966
7967 -- We must create a generic copy of the subunit, in order to
7968 -- perform semantic analysis on it, and we must replace the
7969 -- stub in the original generic unit with the subunit, in order
7970 -- to preserve non-local references within.
7971
7972 -- Only the proper body needs to be copied. Library_Unit and
7973 -- context clause are simply inherited by the generic copy.
7974 -- Note that the copy (which may be recursive if there are
7975 -- nested subunits) must be done first, before attaching it to
7976 -- the enclosing generic.
7977
7978 New_Body :=
7979 Copy_Generic_Node
7980 (Proper_Body (Unit (Subunit)),
7981 Empty, Instantiating => False);
7982
7983 -- Now place the original proper body in the original generic
7984 -- unit. This is a body, not a compilation unit.
7985
7986 Rewrite (N, Proper_Body (Unit (Subunit)));
7987 Set_Is_Compilation_Unit (Defining_Entity (N), False);
7988 Set_Was_Originally_Stub (N);
7989
7990 -- Finally replace the body of the subunit with its copy, and
7991 -- make this new subunit into the library unit of the generic
7992 -- copy, which does not have stubs any longer.
7993
7994 Set_Proper_Body (Unit (Subunit), New_Body);
7995 Set_Library_Unit (New_N, Subunit);
7996 Inherit_Context (Unit (Subunit), N);
7997 end;
7998
7999 -- If we are instantiating, this must be an error case, since
8000 -- otherwise we would have replaced the stub node by the proper body
8001 -- that corresponds. So just ignore it in the copy (i.e. we have
8002 -- copied it, and that is good enough).
8003
8004 else
8005 null;
8006 end if;
8007
8008 <<Subunit_Not_Found>> null;
8009
8010 -- If the node is a compilation unit, it is the subunit of a stub, which
8011 -- has been loaded already (see code below). In this case, the library
8012 -- unit field of N points to the parent unit (which is a compilation
8013 -- unit) and need not (and cannot) be copied.
8014
8015 -- When the proper body of the stub is analyzed, the library_unit link
8016 -- is used to establish the proper context (see sem_ch10).
8017
8018 -- The other fields of a compilation unit are copied as usual
8019
8020 elsif Nkind (N) = N_Compilation_Unit then
8021
8022 -- This code can only be executed when not instantiating, because in
8023 -- the copy made for an instantiation, the compilation unit node has
8024 -- disappeared at the point that a stub is replaced by its proper
8025 -- body.
8026
8027 pragma Assert (not Instantiating);
8028
8029 Set_Context_Items (New_N,
8030 Copy_Generic_List (Context_Items (N), New_N));
8031
8032 Set_Unit (New_N,
8033 Copy_Generic_Node (Unit (N), New_N, Instantiating => False));
8034
8035 Set_First_Inlined_Subprogram (New_N,
8036 Copy_Generic_Node
8037 (First_Inlined_Subprogram (N), New_N, Instantiating => False));
8038
8039 Set_Aux_Decls_Node
8040 (New_N,
8041 Copy_Generic_Node
8042 (Aux_Decls_Node (N), New_N, Instantiating => False));
8043
8044 -- For an assignment node, the assignment is known to be semantically
8045 -- legal if we are instantiating the template. This avoids incorrect
8046 -- diagnostics in generated code.
8047
8048 elsif Nkind (N) = N_Assignment_Statement then
8049
8050 -- Copy name and expression fields in usual manner
8051
8052 Set_Name (New_N,
8053 Copy_Generic_Node (Name (N), New_N, Instantiating));
8054
8055 Set_Expression (New_N,
8056 Copy_Generic_Node (Expression (N), New_N, Instantiating));
8057
8058 if Instantiating then
8059 Set_Assignment_OK (Name (New_N), True);
8060 end if;
8061
8062 elsif Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
8063 if not Instantiating then
8064 Set_Associated_Node (N, New_N);
8065
8066 else
8067 if Present (Get_Associated_Node (N))
8068 and then Nkind (Get_Associated_Node (N)) = Nkind (N)
8069 then
8070 -- In the generic the aggregate has some composite type. If at
8071 -- the point of instantiation the type has a private view,
8072 -- install the full view (and that of its ancestors, if any).
8073
8074 declare
8075 T : Entity_Id := (Etype (Get_Associated_Node (New_N)));
8076 Rt : Entity_Id;
8077
8078 begin
8079 if Present (T) and then Is_Private_Type (T) then
8080 Switch_View (T);
8081 end if;
8082
8083 if Present (T)
8084 and then Is_Tagged_Type (T)
8085 and then Is_Derived_Type (T)
8086 then
8087 Rt := Root_Type (T);
8088
8089 loop
8090 T := Etype (T);
8091
8092 if Is_Private_Type (T) then
8093 Switch_View (T);
8094 end if;
8095
8096 exit when T = Rt;
8097 end loop;
8098 end if;
8099 end;
8100 end if;
8101 end if;
8102
8103 -- Do not copy the associated node, which points to the generic copy
8104 -- of the aggregate.
8105
8106 declare
8107 use Atree.Unchecked_Access;
8108 -- This code section is part of the implementation of an untyped
8109 -- tree traversal, so it needs direct access to node fields.
8110
8111 begin
8112 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
8113 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
8114 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
8115 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
8116 end;
8117
8118 -- Allocators do not have an identifier denoting the access type, so we
8119 -- must locate it through the expression to check whether the views are
8120 -- consistent.
8121
8122 elsif Nkind (N) = N_Allocator
8123 and then Nkind (Expression (N)) = N_Qualified_Expression
8124 and then Is_Entity_Name (Subtype_Mark (Expression (N)))
8125 and then Instantiating
8126 then
8127 declare
8128 T : constant Node_Id :=
8129 Get_Associated_Node (Subtype_Mark (Expression (N)));
8130 Acc_T : Entity_Id;
8131
8132 begin
8133 if Present (T) then
8134
8135 -- Retrieve the allocator node in the generic copy
8136
8137 Acc_T := Etype (Parent (Parent (T)));
8138
8139 if Present (Acc_T) and then Is_Private_Type (Acc_T) then
8140 Switch_View (Acc_T);
8141 end if;
8142 end if;
8143
8144 Copy_Descendants;
8145 end;
8146
8147 -- For a proper body, we must catch the case of a proper body that
8148 -- replaces a stub. This represents the point at which a separate
8149 -- compilation unit, and hence template file, may be referenced, so we
8150 -- must make a new source instantiation entry for the template of the
8151 -- subunit, and ensure that all nodes in the subunit are adjusted using
8152 -- this new source instantiation entry.
8153
8154 elsif Nkind (N) in N_Proper_Body then
8155 declare
8156 Save_Adjustment : constant Sloc_Adjustment := S_Adjustment;
8157 begin
8158 if Instantiating and then Was_Originally_Stub (N) then
8159 Create_Instantiation_Source
8160 (Instantiation_Node,
8161 Defining_Entity (N),
8162 S_Adjustment);
8163
8164 Adjust_Instantiation_Sloc (New_N, S_Adjustment);
8165 end if;
8166
8167 -- Now copy the fields of the proper body, using the new
8168 -- adjustment factor if one was needed as per test above.
8169
8170 Copy_Descendants;
8171
8172 -- Restore the original adjustment factor
8173
8174 S_Adjustment := Save_Adjustment;
8175 end;
8176
8177 elsif Nkind (N) = N_Pragma and then Instantiating then
8178
8179 -- Do not copy Comment or Ident pragmas their content is relevant to
8180 -- the generic unit, not to the instantiating unit.
8181
8182 if Nam_In (Pragma_Name_Unmapped (N), Name_Comment, Name_Ident) then
8183 New_N := Make_Null_Statement (Sloc (N));
8184
8185 -- Do not copy pragmas generated from aspects because the pragmas do
8186 -- not carry any semantic information, plus they will be regenerated
8187 -- in the instance.
8188
8189 -- However, generating C we need to copy them since postconditions
8190 -- are inlined by the front end, and the front-end inlining machinery
8191 -- relies on this routine to perform inlining.
8192
8193 elsif From_Aspect_Specification (N)
8194 and then not Modify_Tree_For_C
8195 then
8196 New_N := Make_Null_Statement (Sloc (N));
8197
8198 else
8199 Copy_Descendants;
8200 end if;
8201
8202 elsif Nkind_In (N, N_Integer_Literal, N_Real_Literal) then
8203
8204 -- No descendant fields need traversing
8205
8206 null;
8207
8208 elsif Nkind (N) = N_String_Literal
8209 and then Present (Etype (N))
8210 and then Instantiating
8211 then
8212 -- If the string is declared in an outer scope, the string_literal
8213 -- subtype created for it may have the wrong scope. Force reanalysis
8214 -- of the constant to generate a new itype in the proper context.
8215
8216 Set_Etype (New_N, Empty);
8217 Set_Analyzed (New_N, False);
8218
8219 -- For the remaining nodes, copy their descendants recursively
8220
8221 else
8222 Copy_Descendants;
8223
8224 if Instantiating and then Nkind (N) = N_Subprogram_Body then
8225 Set_Generic_Parent (Specification (New_N), N);
8226
8227 -- Should preserve Corresponding_Spec??? (12.3(14))
8228 end if;
8229 end if;
8230
8231 -- Propagate dimensions if present, so that they are reflected in the
8232 -- instance.
8233
8234 if Nkind (N) in N_Has_Etype
8235 and then (Nkind (N) in N_Op or else Is_Entity_Name (N))
8236 and then Present (Etype (N))
8237 and then Is_Floating_Point_Type (Etype (N))
8238 and then Has_Dimension_System (Etype (N))
8239 then
8240 Copy_Dimensions (N, New_N);
8241 end if;
8242
8243 return New_N;
8244 end Copy_Generic_Node;
8245
8246 ----------------------------
8247 -- Denotes_Formal_Package --
8248 ----------------------------
8249
8250 function Denotes_Formal_Package
8251 (Pack : Entity_Id;
8252 On_Exit : Boolean := False;
8253 Instance : Entity_Id := Empty) return Boolean
8254 is
8255 Par : Entity_Id;
8256 Scop : constant Entity_Id := Scope (Pack);
8257 E : Entity_Id;
8258
8259 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean;
8260 -- The package in question may be an actual for a previous formal
8261 -- package P of the current instance, so examine its actuals as well.
8262 -- This must be recursive over other formal packages.
8263
8264 ----------------------------------
8265 -- Is_Actual_Of_Previous_Formal --
8266 ----------------------------------
8267
8268 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean is
8269 E1 : Entity_Id;
8270
8271 begin
8272 E1 := First_Entity (P);
8273 while Present (E1) and then E1 /= Instance loop
8274 if Ekind (E1) = E_Package
8275 and then Nkind (Parent (E1)) = N_Package_Renaming_Declaration
8276 then
8277 if Renamed_Object (E1) = Pack then
8278 return True;
8279
8280 elsif E1 = P or else Renamed_Object (E1) = P then
8281 return False;
8282
8283 elsif Is_Actual_Of_Previous_Formal (E1) then
8284 return True;
8285 end if;
8286 end if;
8287
8288 Next_Entity (E1);
8289 end loop;
8290
8291 return False;
8292 end Is_Actual_Of_Previous_Formal;
8293
8294 -- Start of processing for Denotes_Formal_Package
8295
8296 begin
8297 if On_Exit then
8298 Par :=
8299 Instance_Envs.Table
8300 (Instance_Envs.Last).Instantiated_Parent.Act_Id;
8301 else
8302 Par := Current_Instantiated_Parent.Act_Id;
8303 end if;
8304
8305 if Ekind (Scop) = E_Generic_Package
8306 or else Nkind (Unit_Declaration_Node (Scop)) =
8307 N_Generic_Subprogram_Declaration
8308 then
8309 return True;
8310
8311 elsif Nkind (Original_Node (Unit_Declaration_Node (Pack))) =
8312 N_Formal_Package_Declaration
8313 then
8314 return True;
8315
8316 elsif No (Par) then
8317 return False;
8318
8319 else
8320 -- Check whether this package is associated with a formal package of
8321 -- the enclosing instantiation. Iterate over the list of renamings.
8322
8323 E := First_Entity (Par);
8324 while Present (E) loop
8325 if Ekind (E) /= E_Package
8326 or else Nkind (Parent (E)) /= N_Package_Renaming_Declaration
8327 then
8328 null;
8329
8330 elsif Renamed_Object (E) = Par then
8331 return False;
8332
8333 elsif Renamed_Object (E) = Pack then
8334 return True;
8335
8336 elsif Is_Actual_Of_Previous_Formal (E) then
8337 return True;
8338
8339 end if;
8340
8341 Next_Entity (E);
8342 end loop;
8343
8344 return False;
8345 end if;
8346 end Denotes_Formal_Package;
8347
8348 -----------------
8349 -- End_Generic --
8350 -----------------
8351
8352 procedure End_Generic is
8353 begin
8354 -- ??? More things could be factored out in this routine. Should
8355 -- probably be done at a later stage.
8356
8357 Inside_A_Generic := Generic_Flags.Table (Generic_Flags.Last);
8358 Generic_Flags.Decrement_Last;
8359
8360 Expander_Mode_Restore;
8361 end End_Generic;
8362
8363 -------------
8364 -- Earlier --
8365 -------------
8366
8367 function Earlier (N1, N2 : Node_Id) return Boolean is
8368 procedure Find_Depth (P : in out Node_Id; D : in out Integer);
8369 -- Find distance from given node to enclosing compilation unit
8370
8371 ----------------
8372 -- Find_Depth --
8373 ----------------
8374
8375 procedure Find_Depth (P : in out Node_Id; D : in out Integer) is
8376 begin
8377 while Present (P)
8378 and then Nkind (P) /= N_Compilation_Unit
8379 loop
8380 P := True_Parent (P);
8381 D := D + 1;
8382 end loop;
8383 end Find_Depth;
8384
8385 -- Local declarations
8386
8387 D1 : Integer := 0;
8388 D2 : Integer := 0;
8389 P1 : Node_Id := N1;
8390 P2 : Node_Id := N2;
8391 T1 : Source_Ptr;
8392 T2 : Source_Ptr;
8393
8394 -- Start of processing for Earlier
8395
8396 begin
8397 Find_Depth (P1, D1);
8398 Find_Depth (P2, D2);
8399
8400 if P1 /= P2 then
8401 return False;
8402 else
8403 P1 := N1;
8404 P2 := N2;
8405 end if;
8406
8407 while D1 > D2 loop
8408 P1 := True_Parent (P1);
8409 D1 := D1 - 1;
8410 end loop;
8411
8412 while D2 > D1 loop
8413 P2 := True_Parent (P2);
8414 D2 := D2 - 1;
8415 end loop;
8416
8417 -- At this point P1 and P2 are at the same distance from the root.
8418 -- We examine their parents until we find a common declarative list.
8419 -- If we reach the root, N1 and N2 do not descend from the same
8420 -- declarative list (e.g. one is nested in the declarative part and
8421 -- the other is in a block in the statement part) and the earlier
8422 -- one is already frozen.
8423
8424 while not Is_List_Member (P1)
8425 or else not Is_List_Member (P2)
8426 or else List_Containing (P1) /= List_Containing (P2)
8427 loop
8428 P1 := True_Parent (P1);
8429 P2 := True_Parent (P2);
8430
8431 if Nkind (Parent (P1)) = N_Subunit then
8432 P1 := Corresponding_Stub (Parent (P1));
8433 end if;
8434
8435 if Nkind (Parent (P2)) = N_Subunit then
8436 P2 := Corresponding_Stub (Parent (P2));
8437 end if;
8438
8439 if P1 = P2 then
8440 return False;
8441 end if;
8442 end loop;
8443
8444 -- Expanded code usually shares the source location of the original
8445 -- construct it was generated for. This however may not necessarily
8446 -- reflect the true location of the code within the tree.
8447
8448 -- Before comparing the slocs of the two nodes, make sure that we are
8449 -- working with correct source locations. Assume that P1 is to the left
8450 -- of P2. If either one does not come from source, traverse the common
8451 -- list heading towards the other node and locate the first source
8452 -- statement.
8453
8454 -- P1 P2
8455 -- ----+===+===+--------------+===+===+----
8456 -- expanded code expanded code
8457
8458 if not Comes_From_Source (P1) then
8459 while Present (P1) loop
8460
8461 -- Neither P2 nor a source statement were located during the
8462 -- search. If we reach the end of the list, then P1 does not
8463 -- occur earlier than P2.
8464
8465 -- ---->
8466 -- start --- P2 ----- P1 --- end
8467
8468 if No (Next (P1)) then
8469 return False;
8470
8471 -- We encounter P2 while going to the right of the list. This
8472 -- means that P1 does indeed appear earlier.
8473
8474 -- ---->
8475 -- start --- P1 ===== P2 --- end
8476 -- expanded code in between
8477
8478 elsif P1 = P2 then
8479 return True;
8480
8481 -- No need to look any further since we have located a source
8482 -- statement.
8483
8484 elsif Comes_From_Source (P1) then
8485 exit;
8486 end if;
8487
8488 -- Keep going right
8489
8490 Next (P1);
8491 end loop;
8492 end if;
8493
8494 if not Comes_From_Source (P2) then
8495 while Present (P2) loop
8496
8497 -- Neither P1 nor a source statement were located during the
8498 -- search. If we reach the start of the list, then P1 does not
8499 -- occur earlier than P2.
8500
8501 -- <----
8502 -- start --- P2 --- P1 --- end
8503
8504 if No (Prev (P2)) then
8505 return False;
8506
8507 -- We encounter P1 while going to the left of the list. This
8508 -- means that P1 does indeed appear earlier.
8509
8510 -- <----
8511 -- start --- P1 ===== P2 --- end
8512 -- expanded code in between
8513
8514 elsif P2 = P1 then
8515 return True;
8516
8517 -- No need to look any further since we have located a source
8518 -- statement.
8519
8520 elsif Comes_From_Source (P2) then
8521 exit;
8522 end if;
8523
8524 -- Keep going left
8525
8526 Prev (P2);
8527 end loop;
8528 end if;
8529
8530 -- At this point either both nodes came from source or we approximated
8531 -- their source locations through neighboring source statements.
8532
8533 T1 := Top_Level_Location (Sloc (P1));
8534 T2 := Top_Level_Location (Sloc (P2));
8535
8536 -- When two nodes come from the same instance, they have identical top
8537 -- level locations. To determine proper relation within the tree, check
8538 -- their locations within the template.
8539
8540 if T1 = T2 then
8541 return Sloc (P1) < Sloc (P2);
8542
8543 -- The two nodes either come from unrelated instances or do not come
8544 -- from instantiated code at all.
8545
8546 else
8547 return T1 < T2;
8548 end if;
8549 end Earlier;
8550
8551 ----------------------
8552 -- Find_Actual_Type --
8553 ----------------------
8554
8555 function Find_Actual_Type
8556 (Typ : Entity_Id;
8557 Gen_Type : Entity_Id) return Entity_Id
8558 is
8559 Gen_Scope : constant Entity_Id := Scope (Gen_Type);
8560 T : Entity_Id;
8561
8562 begin
8563 -- Special processing only applies to child units
8564
8565 if not Is_Child_Unit (Gen_Scope) then
8566 return Get_Instance_Of (Typ);
8567
8568 -- If designated or component type is itself a formal of the child unit,
8569 -- its instance is available.
8570
8571 elsif Scope (Typ) = Gen_Scope then
8572 return Get_Instance_Of (Typ);
8573
8574 -- If the array or access type is not declared in the parent unit,
8575 -- no special processing needed.
8576
8577 elsif not Is_Generic_Type (Typ)
8578 and then Scope (Gen_Scope) /= Scope (Typ)
8579 then
8580 return Get_Instance_Of (Typ);
8581
8582 -- Otherwise, retrieve designated or component type by visibility
8583
8584 else
8585 T := Current_Entity (Typ);
8586 while Present (T) loop
8587 if In_Open_Scopes (Scope (T)) then
8588 return T;
8589 elsif Is_Generic_Actual_Type (T) then
8590 return T;
8591 end if;
8592
8593 T := Homonym (T);
8594 end loop;
8595
8596 return Typ;
8597 end if;
8598 end Find_Actual_Type;
8599
8600 ----------------------------
8601 -- Freeze_Subprogram_Body --
8602 ----------------------------
8603
8604 procedure Freeze_Subprogram_Body
8605 (Inst_Node : Node_Id;
8606 Gen_Body : Node_Id;
8607 Pack_Id : Entity_Id)
8608 is
8609 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
8610 Par : constant Entity_Id := Scope (Gen_Unit);
8611 E_G_Id : Entity_Id;
8612 Enc_G : Entity_Id;
8613 Enc_I : Node_Id;
8614 F_Node : Node_Id;
8615
8616 function Enclosing_Package_Body (N : Node_Id) return Node_Id;
8617 -- Find innermost package body that encloses the given node, and which
8618 -- is not a compilation unit. Freeze nodes for the instance, or for its
8619 -- enclosing body, may be inserted after the enclosing_body of the
8620 -- generic unit. Used to determine proper placement of freeze node for
8621 -- both package and subprogram instances.
8622
8623 function Package_Freeze_Node (B : Node_Id) return Node_Id;
8624 -- Find entity for given package body, and locate or create a freeze
8625 -- node for it.
8626
8627 ----------------------------
8628 -- Enclosing_Package_Body --
8629 ----------------------------
8630
8631 function Enclosing_Package_Body (N : Node_Id) return Node_Id is
8632 P : Node_Id;
8633
8634 begin
8635 P := Parent (N);
8636 while Present (P)
8637 and then Nkind (Parent (P)) /= N_Compilation_Unit
8638 loop
8639 if Nkind (P) = N_Package_Body then
8640 if Nkind (Parent (P)) = N_Subunit then
8641 return Corresponding_Stub (Parent (P));
8642 else
8643 return P;
8644 end if;
8645 end if;
8646
8647 P := True_Parent (P);
8648 end loop;
8649
8650 return Empty;
8651 end Enclosing_Package_Body;
8652
8653 -------------------------
8654 -- Package_Freeze_Node --
8655 -------------------------
8656
8657 function Package_Freeze_Node (B : Node_Id) return Node_Id is
8658 Id : Entity_Id;
8659
8660 begin
8661 if Nkind (B) = N_Package_Body then
8662 Id := Corresponding_Spec (B);
8663 else pragma Assert (Nkind (B) = N_Package_Body_Stub);
8664 Id := Corresponding_Spec (Proper_Body (Unit (Library_Unit (B))));
8665 end if;
8666
8667 Ensure_Freeze_Node (Id);
8668 return Freeze_Node (Id);
8669 end Package_Freeze_Node;
8670
8671 -- Start of processing for Freeze_Subprogram_Body
8672
8673 begin
8674 -- If the instance and the generic body appear within the same unit, and
8675 -- the instance precedes the generic, the freeze node for the instance
8676 -- must appear after that of the generic. If the generic is nested
8677 -- within another instance I2, then current instance must be frozen
8678 -- after I2. In both cases, the freeze nodes are those of enclosing
8679 -- packages. Otherwise, the freeze node is placed at the end of the
8680 -- current declarative part.
8681
8682 Enc_G := Enclosing_Package_Body (Gen_Body);
8683 Enc_I := Enclosing_Package_Body (Inst_Node);
8684 Ensure_Freeze_Node (Pack_Id);
8685 F_Node := Freeze_Node (Pack_Id);
8686
8687 if Is_Generic_Instance (Par)
8688 and then Present (Freeze_Node (Par))
8689 and then In_Same_Declarative_Part
8690 (Parent (Freeze_Node (Par)), Inst_Node)
8691 then
8692 -- The parent was a premature instantiation. Insert freeze node at
8693 -- the end the current declarative part.
8694
8695 if Is_Known_Guaranteed_ABE (Get_Unit_Instantiation_Node (Par)) then
8696 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8697
8698 -- Handle the following case:
8699 --
8700 -- package Parent_Inst is new ...
8701 -- Parent_Inst []
8702 --
8703 -- procedure P ... -- this body freezes Parent_Inst
8704 --
8705 -- package Inst is new ...
8706 --
8707 -- In this particular scenario, the freeze node for Inst must be
8708 -- inserted in the same manner as that of Parent_Inst - before the
8709 -- next source body or at the end of the declarative list (body not
8710 -- available). If body P did not exist and Parent_Inst was frozen
8711 -- after Inst, either by a body following Inst or at the end of the
8712 -- declarative region, the freeze node for Inst must be inserted
8713 -- after that of Parent_Inst. This relation is established by
8714 -- comparing the Slocs of Parent_Inst freeze node and Inst.
8715
8716 elsif List_Containing (Get_Unit_Instantiation_Node (Par)) =
8717 List_Containing (Inst_Node)
8718 and then Sloc (Freeze_Node (Par)) < Sloc (Inst_Node)
8719 then
8720 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8721
8722 else
8723 Insert_After (Freeze_Node (Par), F_Node);
8724 end if;
8725
8726 -- The body enclosing the instance should be frozen after the body that
8727 -- includes the generic, because the body of the instance may make
8728 -- references to entities therein. If the two are not in the same
8729 -- declarative part, or if the one enclosing the instance is frozen
8730 -- already, freeze the instance at the end of the current declarative
8731 -- part.
8732
8733 elsif Is_Generic_Instance (Par)
8734 and then Present (Freeze_Node (Par))
8735 and then Present (Enc_I)
8736 then
8737 if In_Same_Declarative_Part (Parent (Freeze_Node (Par)), Enc_I)
8738 or else
8739 (Nkind (Enc_I) = N_Package_Body
8740 and then In_Same_Declarative_Part
8741 (Parent (Freeze_Node (Par)), Parent (Enc_I)))
8742 then
8743 -- The enclosing package may contain several instances. Rather
8744 -- than computing the earliest point at which to insert its freeze
8745 -- node, we place it at the end of the declarative part of the
8746 -- parent of the generic.
8747
8748 Insert_Freeze_Node_For_Instance
8749 (Freeze_Node (Par), Package_Freeze_Node (Enc_I));
8750 end if;
8751
8752 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8753
8754 elsif Present (Enc_G)
8755 and then Present (Enc_I)
8756 and then Enc_G /= Enc_I
8757 and then Earlier (Inst_Node, Gen_Body)
8758 then
8759 if Nkind (Enc_G) = N_Package_Body then
8760 E_G_Id :=
8761 Corresponding_Spec (Enc_G);
8762 else pragma Assert (Nkind (Enc_G) = N_Package_Body_Stub);
8763 E_G_Id :=
8764 Corresponding_Spec (Proper_Body (Unit (Library_Unit (Enc_G))));
8765 end if;
8766
8767 -- Freeze package that encloses instance, and place node after the
8768 -- package that encloses generic. If enclosing package is already
8769 -- frozen we have to assume it is at the proper place. This may be a
8770 -- potential ABE that requires dynamic checking. Do not add a freeze
8771 -- node if the package that encloses the generic is inside the body
8772 -- that encloses the instance, because the freeze node would be in
8773 -- the wrong scope. Additional contortions needed if the bodies are
8774 -- within a subunit.
8775
8776 declare
8777 Enclosing_Body : Node_Id;
8778
8779 begin
8780 if Nkind (Enc_I) = N_Package_Body_Stub then
8781 Enclosing_Body := Proper_Body (Unit (Library_Unit (Enc_I)));
8782 else
8783 Enclosing_Body := Enc_I;
8784 end if;
8785
8786 if Parent (List_Containing (Enc_G)) /= Enclosing_Body then
8787 Insert_Freeze_Node_For_Instance
8788 (Enc_G, Package_Freeze_Node (Enc_I));
8789 end if;
8790 end;
8791
8792 -- Freeze enclosing subunit before instance
8793
8794 Ensure_Freeze_Node (E_G_Id);
8795
8796 if not Is_List_Member (Freeze_Node (E_G_Id)) then
8797 Insert_After (Enc_G, Freeze_Node (E_G_Id));
8798 end if;
8799
8800 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8801
8802 else
8803 -- If none of the above, insert freeze node at the end of the current
8804 -- declarative part.
8805
8806 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8807 end if;
8808 end Freeze_Subprogram_Body;
8809
8810 ----------------
8811 -- Get_Gen_Id --
8812 ----------------
8813
8814 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id is
8815 begin
8816 return Generic_Renamings.Table (E).Gen_Id;
8817 end Get_Gen_Id;
8818
8819 ---------------------
8820 -- Get_Instance_Of --
8821 ---------------------
8822
8823 function Get_Instance_Of (A : Entity_Id) return Entity_Id is
8824 Res : constant Assoc_Ptr := Generic_Renamings_HTable.Get (A);
8825
8826 begin
8827 if Res /= Assoc_Null then
8828 return Generic_Renamings.Table (Res).Act_Id;
8829
8830 else
8831 -- On exit, entity is not instantiated: not a generic parameter, or
8832 -- else parameter of an inner generic unit.
8833
8834 return A;
8835 end if;
8836 end Get_Instance_Of;
8837
8838 ---------------------------------
8839 -- Get_Unit_Instantiation_Node --
8840 ---------------------------------
8841
8842 function Get_Unit_Instantiation_Node (A : Entity_Id) return Node_Id is
8843 Decl : Node_Id := Unit_Declaration_Node (A);
8844 Inst : Node_Id;
8845
8846 begin
8847 -- If the Package_Instantiation attribute has been set on the package
8848 -- entity, then use it directly when it (or its Original_Node) refers
8849 -- to an N_Package_Instantiation node. In principle it should be
8850 -- possible to have this field set in all cases, which should be
8851 -- investigated, and would allow this function to be significantly
8852 -- simplified. ???
8853
8854 Inst := Package_Instantiation (A);
8855
8856 if Present (Inst) then
8857 if Nkind (Inst) = N_Package_Instantiation then
8858 return Inst;
8859
8860 elsif Nkind (Original_Node (Inst)) = N_Package_Instantiation then
8861 return Original_Node (Inst);
8862 end if;
8863 end if;
8864
8865 -- If the instantiation is a compilation unit that does not need body
8866 -- then the instantiation node has been rewritten as a package
8867 -- declaration for the instance, and we return the original node.
8868
8869 -- If it is a compilation unit and the instance node has not been
8870 -- rewritten, then it is still the unit of the compilation. Finally, if
8871 -- a body is present, this is a parent of the main unit whose body has
8872 -- been compiled for inlining purposes, and the instantiation node has
8873 -- been rewritten with the instance body.
8874
8875 -- Otherwise the instantiation node appears after the declaration. If
8876 -- the entity is a formal package, the declaration may have been
8877 -- rewritten as a generic declaration (in the case of a formal with box)
8878 -- or left as a formal package declaration if it has actuals, and is
8879 -- found with a forward search.
8880
8881 if Nkind (Parent (Decl)) = N_Compilation_Unit then
8882 if Nkind (Decl) = N_Package_Declaration
8883 and then Present (Corresponding_Body (Decl))
8884 then
8885 Decl := Unit_Declaration_Node (Corresponding_Body (Decl));
8886 end if;
8887
8888 if Nkind_In (Original_Node (Decl), N_Function_Instantiation,
8889 N_Package_Instantiation,
8890 N_Procedure_Instantiation)
8891 then
8892 return Original_Node (Decl);
8893 else
8894 return Unit (Parent (Decl));
8895 end if;
8896
8897 elsif Nkind (Decl) = N_Package_Declaration
8898 and then Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration
8899 then
8900 return Original_Node (Decl);
8901
8902 else
8903 Inst := Next (Decl);
8904 while not Nkind_In (Inst, N_Formal_Package_Declaration,
8905 N_Function_Instantiation,
8906 N_Package_Instantiation,
8907 N_Procedure_Instantiation)
8908 loop
8909 Next (Inst);
8910 end loop;
8911
8912 return Inst;
8913 end if;
8914 end Get_Unit_Instantiation_Node;
8915
8916 ------------------------
8917 -- Has_Been_Exchanged --
8918 ------------------------
8919
8920 function Has_Been_Exchanged (E : Entity_Id) return Boolean is
8921 Next : Elmt_Id;
8922
8923 begin
8924 Next := First_Elmt (Exchanged_Views);
8925 while Present (Next) loop
8926 if Full_View (Node (Next)) = E then
8927 return True;
8928 end if;
8929
8930 Next_Elmt (Next);
8931 end loop;
8932
8933 return False;
8934 end Has_Been_Exchanged;
8935
8936 ----------
8937 -- Hash --
8938 ----------
8939
8940 function Hash (F : Entity_Id) return HTable_Range is
8941 begin
8942 return HTable_Range (F mod HTable_Size);
8943 end Hash;
8944
8945 ------------------------
8946 -- Hide_Current_Scope --
8947 ------------------------
8948
8949 procedure Hide_Current_Scope is
8950 C : constant Entity_Id := Current_Scope;
8951 E : Entity_Id;
8952
8953 begin
8954 Set_Is_Hidden_Open_Scope (C);
8955
8956 E := First_Entity (C);
8957 while Present (E) loop
8958 if Is_Immediately_Visible (E) then
8959 Set_Is_Immediately_Visible (E, False);
8960 Append_Elmt (E, Hidden_Entities);
8961 end if;
8962
8963 Next_Entity (E);
8964 end loop;
8965
8966 -- Make the scope name invisible as well. This is necessary, but might
8967 -- conflict with calls to Rtsfind later on, in case the scope is a
8968 -- predefined one. There is no clean solution to this problem, so for
8969 -- now we depend on the user not redefining Standard itself in one of
8970 -- the parent units.
8971
8972 if Is_Immediately_Visible (C) and then C /= Standard_Standard then
8973 Set_Is_Immediately_Visible (C, False);
8974 Append_Elmt (C, Hidden_Entities);
8975 end if;
8976
8977 end Hide_Current_Scope;
8978
8979 --------------
8980 -- Init_Env --
8981 --------------
8982
8983 procedure Init_Env is
8984 Saved : Instance_Env;
8985
8986 begin
8987 Saved.Instantiated_Parent := Current_Instantiated_Parent;
8988 Saved.Exchanged_Views := Exchanged_Views;
8989 Saved.Hidden_Entities := Hidden_Entities;
8990 Saved.Current_Sem_Unit := Current_Sem_Unit;
8991 Saved.Parent_Unit_Visible := Parent_Unit_Visible;
8992 Saved.Instance_Parent_Unit := Instance_Parent_Unit;
8993
8994 -- Save configuration switches. These may be reset if the unit is a
8995 -- predefined unit, and the current mode is not Ada 2005.
8996
8997 Save_Opt_Config_Switches (Saved.Switches);
8998
8999 Instance_Envs.Append (Saved);
9000
9001 Exchanged_Views := New_Elmt_List;
9002 Hidden_Entities := New_Elmt_List;
9003
9004 -- Make dummy entry for Instantiated parent. If generic unit is legal,
9005 -- this is set properly in Set_Instance_Env.
9006
9007 Current_Instantiated_Parent :=
9008 (Current_Scope, Current_Scope, Assoc_Null);
9009 end Init_Env;
9010
9011 ---------------------
9012 -- In_Main_Context --
9013 ---------------------
9014
9015 function In_Main_Context (E : Entity_Id) return Boolean is
9016 Context : List_Id;
9017 Clause : Node_Id;
9018 Nam : Node_Id;
9019
9020 begin
9021 if not Is_Compilation_Unit (E)
9022 or else Ekind (E) /= E_Package
9023 or else In_Private_Part (E)
9024 then
9025 return False;
9026 end if;
9027
9028 Context := Context_Items (Cunit (Main_Unit));
9029
9030 Clause := First (Context);
9031 while Present (Clause) loop
9032 if Nkind (Clause) = N_With_Clause then
9033 Nam := Name (Clause);
9034
9035 -- If the current scope is part of the context of the main unit,
9036 -- analysis of the corresponding with_clause is not complete, and
9037 -- the entity is not set. We use the Chars field directly, which
9038 -- might produce false positives in rare cases, but guarantees
9039 -- that we produce all the instance bodies we will need.
9040
9041 if (Is_Entity_Name (Nam) and then Chars (Nam) = Chars (E))
9042 or else (Nkind (Nam) = N_Selected_Component
9043 and then Chars (Selector_Name (Nam)) = Chars (E))
9044 then
9045 return True;
9046 end if;
9047 end if;
9048
9049 Next (Clause);
9050 end loop;
9051
9052 return False;
9053 end In_Main_Context;
9054
9055 ---------------------
9056 -- Inherit_Context --
9057 ---------------------
9058
9059 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id) is
9060 Current_Context : List_Id;
9061 Current_Unit : Node_Id;
9062 Item : Node_Id;
9063 New_I : Node_Id;
9064
9065 Clause : Node_Id;
9066 OK : Boolean;
9067 Lib_Unit : Node_Id;
9068
9069 begin
9070 if Nkind (Parent (Gen_Decl)) = N_Compilation_Unit then
9071
9072 -- The inherited context is attached to the enclosing compilation
9073 -- unit. This is either the main unit, or the declaration for the
9074 -- main unit (in case the instantiation appears within the package
9075 -- declaration and the main unit is its body).
9076
9077 Current_Unit := Parent (Inst);
9078 while Present (Current_Unit)
9079 and then Nkind (Current_Unit) /= N_Compilation_Unit
9080 loop
9081 Current_Unit := Parent (Current_Unit);
9082 end loop;
9083
9084 Current_Context := Context_Items (Current_Unit);
9085
9086 Item := First (Context_Items (Parent (Gen_Decl)));
9087 while Present (Item) loop
9088 if Nkind (Item) = N_With_Clause then
9089 Lib_Unit := Library_Unit (Item);
9090
9091 -- Take care to prevent direct cyclic with's
9092
9093 if Lib_Unit /= Current_Unit then
9094
9095 -- Do not add a unit if it is already in the context
9096
9097 Clause := First (Current_Context);
9098 OK := True;
9099 while Present (Clause) loop
9100 if Nkind (Clause) = N_With_Clause
9101 and then Library_Unit (Clause) = Lib_Unit
9102 then
9103 OK := False;
9104 exit;
9105 end if;
9106
9107 Next (Clause);
9108 end loop;
9109
9110 if OK then
9111 New_I := New_Copy (Item);
9112 Set_Implicit_With (New_I);
9113
9114 Append (New_I, Current_Context);
9115 end if;
9116 end if;
9117 end if;
9118
9119 Next (Item);
9120 end loop;
9121 end if;
9122 end Inherit_Context;
9123
9124 ----------------
9125 -- Initialize --
9126 ----------------
9127
9128 procedure Initialize is
9129 begin
9130 Generic_Renamings.Init;
9131 Instance_Envs.Init;
9132 Generic_Flags.Init;
9133 Generic_Renamings_HTable.Reset;
9134 Circularity_Detected := False;
9135 Exchanged_Views := No_Elist;
9136 Hidden_Entities := No_Elist;
9137 end Initialize;
9138
9139 -------------------------------------
9140 -- Insert_Freeze_Node_For_Instance --
9141 -------------------------------------
9142
9143 procedure Insert_Freeze_Node_For_Instance
9144 (N : Node_Id;
9145 F_Node : Node_Id)
9146 is
9147 Decl : Node_Id;
9148 Decls : List_Id;
9149 Inst : Entity_Id;
9150 Par_N : Node_Id;
9151
9152 function Enclosing_Body (N : Node_Id) return Node_Id;
9153 -- Find enclosing package or subprogram body, if any. Freeze node may
9154 -- be placed at end of current declarative list if previous instance
9155 -- and current one have different enclosing bodies.
9156
9157 function Previous_Instance (Gen : Entity_Id) return Entity_Id;
9158 -- Find the local instance, if any, that declares the generic that is
9159 -- being instantiated. If present, the freeze node for this instance
9160 -- must follow the freeze node for the previous instance.
9161
9162 --------------------
9163 -- Enclosing_Body --
9164 --------------------
9165
9166 function Enclosing_Body (N : Node_Id) return Node_Id is
9167 P : Node_Id;
9168
9169 begin
9170 P := Parent (N);
9171 while Present (P)
9172 and then Nkind (Parent (P)) /= N_Compilation_Unit
9173 loop
9174 if Nkind_In (P, N_Package_Body, N_Subprogram_Body) then
9175 if Nkind (Parent (P)) = N_Subunit then
9176 return Corresponding_Stub (Parent (P));
9177 else
9178 return P;
9179 end if;
9180 end if;
9181
9182 P := True_Parent (P);
9183 end loop;
9184
9185 return Empty;
9186 end Enclosing_Body;
9187
9188 -----------------------
9189 -- Previous_Instance --
9190 -----------------------
9191
9192 function Previous_Instance (Gen : Entity_Id) return Entity_Id is
9193 S : Entity_Id;
9194
9195 begin
9196 S := Scope (Gen);
9197 while Present (S) and then S /= Standard_Standard loop
9198 if Is_Generic_Instance (S)
9199 and then In_Same_Source_Unit (S, N)
9200 then
9201 return S;
9202 end if;
9203
9204 S := Scope (S);
9205 end loop;
9206
9207 return Empty;
9208 end Previous_Instance;
9209
9210 -- Start of processing for Insert_Freeze_Node_For_Instance
9211
9212 begin
9213 if not Is_List_Member (F_Node) then
9214 Decl := N;
9215 Decls := List_Containing (N);
9216 Inst := Entity (F_Node);
9217 Par_N := Parent (Decls);
9218
9219 -- When processing a subprogram instantiation, utilize the actual
9220 -- subprogram instantiation rather than its package wrapper as it
9221 -- carries all the context information.
9222
9223 if Is_Wrapper_Package (Inst) then
9224 Inst := Related_Instance (Inst);
9225 end if;
9226
9227 -- If this is a package instance, check whether the generic is
9228 -- declared in a previous instance and the current instance is
9229 -- not within the previous one.
9230
9231 if Present (Generic_Parent (Parent (Inst)))
9232 and then Is_In_Main_Unit (N)
9233 then
9234 declare
9235 Enclosing_N : constant Node_Id := Enclosing_Body (N);
9236 Par_I : constant Entity_Id :=
9237 Previous_Instance
9238 (Generic_Parent (Parent (Inst)));
9239 Scop : Entity_Id;
9240
9241 begin
9242 if Present (Par_I)
9243 and then Earlier (N, Freeze_Node (Par_I))
9244 then
9245 Scop := Scope (Inst);
9246
9247 -- If the current instance is within the one that contains
9248 -- the generic, the freeze node for the current one must
9249 -- appear in the current declarative part. Ditto, if the
9250 -- current instance is within another package instance or
9251 -- within a body that does not enclose the current instance.
9252 -- In these three cases the freeze node of the previous
9253 -- instance is not relevant.
9254
9255 while Present (Scop) and then Scop /= Standard_Standard loop
9256 exit when Scop = Par_I
9257 or else
9258 (Is_Generic_Instance (Scop)
9259 and then Scope_Depth (Scop) > Scope_Depth (Par_I));
9260 Scop := Scope (Scop);
9261 end loop;
9262
9263 -- Previous instance encloses current instance
9264
9265 if Scop = Par_I then
9266 null;
9267
9268 -- If the next node is a source body we must freeze in
9269 -- the current scope as well.
9270
9271 elsif Present (Next (N))
9272 and then Nkind_In (Next (N), N_Subprogram_Body,
9273 N_Package_Body)
9274 and then Comes_From_Source (Next (N))
9275 then
9276 null;
9277
9278 -- Current instance is within an unrelated instance
9279
9280 elsif Is_Generic_Instance (Scop) then
9281 null;
9282
9283 -- Current instance is within an unrelated body
9284
9285 elsif Present (Enclosing_N)
9286 and then Enclosing_N /= Enclosing_Body (Par_I)
9287 then
9288 null;
9289
9290 else
9291 Insert_After (Freeze_Node (Par_I), F_Node);
9292 return;
9293 end if;
9294 end if;
9295 end;
9296 end if;
9297
9298 -- When the instantiation occurs in a package declaration, append the
9299 -- freeze node to the private declarations (if any).
9300
9301 if Nkind (Par_N) = N_Package_Specification
9302 and then Decls = Visible_Declarations (Par_N)
9303 and then Present (Private_Declarations (Par_N))
9304 and then not Is_Empty_List (Private_Declarations (Par_N))
9305 then
9306 Decls := Private_Declarations (Par_N);
9307 Decl := First (Decls);
9308 end if;
9309
9310 -- Determine the proper freeze point of a package instantiation. We
9311 -- adhere to the general rule of a package or subprogram body causing
9312 -- freezing of anything before it in the same declarative region. In
9313 -- this case, the proper freeze point of a package instantiation is
9314 -- before the first source body which follows, or before a stub. This
9315 -- ensures that entities coming from the instance are already frozen
9316 -- and usable in source bodies.
9317
9318 if Nkind (Par_N) /= N_Package_Declaration
9319 and then Ekind (Inst) = E_Package
9320 and then Is_Generic_Instance (Inst)
9321 and then
9322 not In_Same_Source_Unit (Generic_Parent (Parent (Inst)), Inst)
9323 then
9324 while Present (Decl) loop
9325 if (Nkind (Decl) in N_Unit_Body
9326 or else
9327 Nkind (Decl) in N_Body_Stub)
9328 and then Comes_From_Source (Decl)
9329 then
9330 Insert_Before (Decl, F_Node);
9331 return;
9332 end if;
9333
9334 Next (Decl);
9335 end loop;
9336 end if;
9337
9338 -- In a package declaration, or if no previous body, insert at end
9339 -- of list.
9340
9341 Set_Sloc (F_Node, Sloc (Last (Decls)));
9342 Insert_After (Last (Decls), F_Node);
9343 end if;
9344 end Insert_Freeze_Node_For_Instance;
9345
9346 ------------------
9347 -- Install_Body --
9348 ------------------
9349
9350 procedure Install_Body
9351 (Act_Body : Node_Id;
9352 N : Node_Id;
9353 Gen_Body : Node_Id;
9354 Gen_Decl : Node_Id)
9355 is
9356 function In_Same_Scope (Gen_Id, Act_Id : Node_Id) return Boolean;
9357 -- Check if the generic definition and the instantiation come from
9358 -- a common scope, in which case the instance must be frozen after
9359 -- the generic body.
9360
9361 function True_Sloc (N, Act_Unit : Node_Id) return Source_Ptr;
9362 -- If the instance is nested inside a generic unit, the Sloc of the
9363 -- instance indicates the place of the original definition, not the
9364 -- point of the current enclosing instance. Pending a better usage of
9365 -- Slocs to indicate instantiation places, we determine the place of
9366 -- origin of a node by finding the maximum sloc of any ancestor node.
9367 -- Why is this not equivalent to Top_Level_Location ???
9368
9369 -------------------
9370 -- In_Same_Scope --
9371 -------------------
9372
9373 function In_Same_Scope (Gen_Id, Act_Id : Node_Id) return Boolean is
9374 Act_Scop : Entity_Id := Scope (Act_Id);
9375 Gen_Scop : Entity_Id := Scope (Gen_Id);
9376
9377 begin
9378 while Act_Scop /= Standard_Standard
9379 and then Gen_Scop /= Standard_Standard
9380 loop
9381 if Act_Scop = Gen_Scop then
9382 return True;
9383 end if;
9384
9385 Act_Scop := Scope (Act_Scop);
9386 Gen_Scop := Scope (Gen_Scop);
9387 end loop;
9388
9389 return False;
9390 end In_Same_Scope;
9391
9392 ---------------
9393 -- True_Sloc --
9394 ---------------
9395
9396 function True_Sloc (N, Act_Unit : Node_Id) return Source_Ptr is
9397 N1 : Node_Id;
9398 Res : Source_Ptr;
9399
9400 begin
9401 Res := Sloc (N);
9402 N1 := N;
9403 while Present (N1) and then N1 /= Act_Unit loop
9404 if Sloc (N1) > Res then
9405 Res := Sloc (N1);
9406 end if;
9407
9408 N1 := Parent (N1);
9409 end loop;
9410
9411 return Res;
9412 end True_Sloc;
9413
9414 Act_Id : constant Entity_Id := Corresponding_Spec (Act_Body);
9415 Act_Unit : constant Node_Id := Unit (Cunit (Get_Source_Unit (N)));
9416 Gen_Id : constant Entity_Id := Corresponding_Spec (Gen_Body);
9417 Par : constant Entity_Id := Scope (Gen_Id);
9418 Gen_Unit : constant Node_Id :=
9419 Unit (Cunit (Get_Source_Unit (Gen_Decl)));
9420
9421 Body_Unit : Node_Id;
9422 F_Node : Node_Id;
9423 Must_Delay : Boolean;
9424 Orig_Body : Node_Id := Gen_Body;
9425
9426 -- Start of processing for Install_Body
9427
9428 begin
9429 -- Handle first the case of an instance with incomplete actual types.
9430 -- The instance body cannot be placed after the declaration because
9431 -- full views have not been seen yet. Any use of the non-limited views
9432 -- in the instance body requires the presence of a regular with_clause
9433 -- in the enclosing unit, and will fail if this with_clause is missing.
9434 -- We place the instance body at the beginning of the enclosing body,
9435 -- which is the unit being compiled. The freeze node for the instance
9436 -- is then placed after the instance body.
9437
9438 if not Is_Empty_Elmt_List (Incomplete_Actuals (Act_Id))
9439 and then Expander_Active
9440 and then Ekind (Scope (Act_Id)) = E_Package
9441 then
9442 declare
9443 Scop : constant Entity_Id := Scope (Act_Id);
9444 Body_Id : constant Node_Id :=
9445 Corresponding_Body (Unit_Declaration_Node (Scop));
9446
9447 begin
9448 Ensure_Freeze_Node (Act_Id);
9449 F_Node := Freeze_Node (Act_Id);
9450 if Present (Body_Id) then
9451 Set_Is_Frozen (Act_Id, False);
9452 Prepend (Act_Body, Declarations (Parent (Body_Id)));
9453 if Is_List_Member (F_Node) then
9454 Remove (F_Node);
9455 end if;
9456
9457 Insert_After (Act_Body, F_Node);
9458 end if;
9459 end;
9460 return;
9461 end if;
9462
9463 -- If the body is a subunit, the freeze point is the corresponding stub
9464 -- in the current compilation, not the subunit itself.
9465
9466 if Nkind (Parent (Gen_Body)) = N_Subunit then
9467 Orig_Body := Corresponding_Stub (Parent (Gen_Body));
9468 else
9469 Orig_Body := Gen_Body;
9470 end if;
9471
9472 Body_Unit := Unit (Cunit (Get_Source_Unit (Orig_Body)));
9473
9474 -- If the instantiation and the generic definition appear in the same
9475 -- package declaration, this is an early instantiation. If they appear
9476 -- in the same declarative part, it is an early instantiation only if
9477 -- the generic body appears textually later, and the generic body is
9478 -- also in the main unit.
9479
9480 -- If instance is nested within a subprogram, and the generic body
9481 -- is not, the instance is delayed because the enclosing body is. If
9482 -- instance and body are within the same scope, or the same subprogram
9483 -- body, indicate explicitly that the instance is delayed.
9484
9485 Must_Delay :=
9486 (Gen_Unit = Act_Unit
9487 and then (Nkind_In (Gen_Unit, N_Generic_Package_Declaration,
9488 N_Package_Declaration)
9489 or else (Gen_Unit = Body_Unit
9490 and then True_Sloc (N, Act_Unit) <
9491 Sloc (Orig_Body)))
9492 and then Is_In_Main_Unit (Original_Node (Gen_Unit))
9493 and then In_Same_Scope (Gen_Id, Act_Id));
9494
9495 -- If this is an early instantiation, the freeze node is placed after
9496 -- the generic body. Otherwise, if the generic appears in an instance,
9497 -- we cannot freeze the current instance until the outer one is frozen.
9498 -- This is only relevant if the current instance is nested within some
9499 -- inner scope not itself within the outer instance. If this scope is
9500 -- a package body in the same declarative part as the outer instance,
9501 -- then that body needs to be frozen after the outer instance. Finally,
9502 -- if no delay is needed, we place the freeze node at the end of the
9503 -- current declarative part.
9504
9505 if Expander_Active
9506 and then (No (Freeze_Node (Act_Id))
9507 or else not Is_List_Member (Freeze_Node (Act_Id)))
9508 then
9509 Ensure_Freeze_Node (Act_Id);
9510 F_Node := Freeze_Node (Act_Id);
9511
9512 if Must_Delay then
9513 Insert_After (Orig_Body, F_Node);
9514
9515 elsif Is_Generic_Instance (Par)
9516 and then Present (Freeze_Node (Par))
9517 and then Scope (Act_Id) /= Par
9518 then
9519 -- Freeze instance of inner generic after instance of enclosing
9520 -- generic.
9521
9522 if In_Same_Declarative_Part (Parent (Freeze_Node (Par)), N) then
9523
9524 -- Handle the following case:
9525
9526 -- package Parent_Inst is new ...
9527 -- Parent_Inst []
9528
9529 -- procedure P ... -- this body freezes Parent_Inst
9530
9531 -- package Inst is new ...
9532
9533 -- In this particular scenario, the freeze node for Inst must
9534 -- be inserted in the same manner as that of Parent_Inst,
9535 -- before the next source body or at the end of the declarative
9536 -- list (body not available). If body P did not exist and
9537 -- Parent_Inst was frozen after Inst, either by a body
9538 -- following Inst or at the end of the declarative region,
9539 -- the freeze node for Inst must be inserted after that of
9540 -- Parent_Inst. This relation is established by comparing
9541 -- the Slocs of Parent_Inst freeze node and Inst.
9542 -- We examine the parents of the enclosing lists to handle
9543 -- the case where the parent instance is in the visible part
9544 -- of a package declaration, and the inner instance is in
9545 -- the corresponding private part.
9546
9547 if Parent (List_Containing (Get_Unit_Instantiation_Node (Par)))
9548 = Parent (List_Containing (N))
9549 and then Sloc (Freeze_Node (Par)) < Sloc (N)
9550 then
9551 Insert_Freeze_Node_For_Instance (N, F_Node);
9552 else
9553 Insert_After (Freeze_Node (Par), F_Node);
9554 end if;
9555
9556 -- Freeze package enclosing instance of inner generic after
9557 -- instance of enclosing generic.
9558
9559 elsif Nkind_In (Parent (N), N_Package_Body, N_Subprogram_Body)
9560 and then In_Same_Declarative_Part
9561 (Parent (Freeze_Node (Par)), Parent (N))
9562 then
9563 declare
9564 Enclosing : Entity_Id;
9565
9566 begin
9567 Enclosing := Corresponding_Spec (Parent (N));
9568
9569 if No (Enclosing) then
9570 Enclosing := Defining_Entity (Parent (N));
9571 end if;
9572
9573 Insert_Freeze_Node_For_Instance (N, F_Node);
9574 Ensure_Freeze_Node (Enclosing);
9575
9576 if not Is_List_Member (Freeze_Node (Enclosing)) then
9577
9578 -- The enclosing context is a subunit, insert the freeze
9579 -- node after the stub.
9580
9581 if Nkind (Parent (Parent (N))) = N_Subunit then
9582 Insert_Freeze_Node_For_Instance
9583 (Corresponding_Stub (Parent (Parent (N))),
9584 Freeze_Node (Enclosing));
9585
9586 -- The enclosing context is a package with a stub body
9587 -- which has already been replaced by the real body.
9588 -- Insert the freeze node after the actual body.
9589
9590 elsif Ekind (Enclosing) = E_Package
9591 and then Present (Body_Entity (Enclosing))
9592 and then Was_Originally_Stub
9593 (Parent (Body_Entity (Enclosing)))
9594 then
9595 Insert_Freeze_Node_For_Instance
9596 (Parent (Body_Entity (Enclosing)),
9597 Freeze_Node (Enclosing));
9598
9599 -- The parent instance has been frozen before the body of
9600 -- the enclosing package, insert the freeze node after
9601 -- the body.
9602
9603 elsif List_Containing (Freeze_Node (Par)) =
9604 List_Containing (Parent (N))
9605 and then Sloc (Freeze_Node (Par)) < Sloc (Parent (N))
9606 then
9607 Insert_Freeze_Node_For_Instance
9608 (Parent (N), Freeze_Node (Enclosing));
9609
9610 else
9611 Insert_After
9612 (Freeze_Node (Par), Freeze_Node (Enclosing));
9613 end if;
9614 end if;
9615 end;
9616
9617 else
9618 Insert_Freeze_Node_For_Instance (N, F_Node);
9619 end if;
9620
9621 else
9622 Insert_Freeze_Node_For_Instance (N, F_Node);
9623 end if;
9624 end if;
9625
9626 Set_Is_Frozen (Act_Id);
9627 Insert_Before (N, Act_Body);
9628 Mark_Rewrite_Insertion (Act_Body);
9629 end Install_Body;
9630
9631 -----------------------------
9632 -- Install_Formal_Packages --
9633 -----------------------------
9634
9635 procedure Install_Formal_Packages (Par : Entity_Id) is
9636 E : Entity_Id;
9637 Gen : Entity_Id;
9638 Gen_E : Entity_Id := Empty;
9639
9640 begin
9641 E := First_Entity (Par);
9642
9643 -- If we are installing an instance parent, locate the formal packages
9644 -- of its generic parent.
9645
9646 if Is_Generic_Instance (Par) then
9647 Gen := Generic_Parent (Package_Specification (Par));
9648 Gen_E := First_Entity (Gen);
9649 end if;
9650
9651 while Present (E) loop
9652 if Ekind (E) = E_Package
9653 and then Nkind (Parent (E)) = N_Package_Renaming_Declaration
9654 then
9655 -- If this is the renaming for the parent instance, done
9656
9657 if Renamed_Object (E) = Par then
9658 exit;
9659
9660 -- The visibility of a formal of an enclosing generic is already
9661 -- correct.
9662
9663 elsif Denotes_Formal_Package (E) then
9664 null;
9665
9666 elsif Present (Associated_Formal_Package (E)) then
9667 Check_Generic_Actuals (Renamed_Object (E), True);
9668 Set_Is_Hidden (E, False);
9669
9670 -- Find formal package in generic unit that corresponds to
9671 -- (instance of) formal package in instance.
9672
9673 while Present (Gen_E) and then Chars (Gen_E) /= Chars (E) loop
9674 Next_Entity (Gen_E);
9675 end loop;
9676
9677 if Present (Gen_E) then
9678 Map_Formal_Package_Entities (Gen_E, E);
9679 end if;
9680 end if;
9681 end if;
9682
9683 Next_Entity (E);
9684
9685 if Present (Gen_E) then
9686 Next_Entity (Gen_E);
9687 end if;
9688 end loop;
9689 end Install_Formal_Packages;
9690
9691 --------------------
9692 -- Install_Parent --
9693 --------------------
9694
9695 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False) is
9696 Ancestors : constant Elist_Id := New_Elmt_List;
9697 S : constant Entity_Id := Current_Scope;
9698 Inst_Par : Entity_Id;
9699 First_Par : Entity_Id;
9700 Inst_Node : Node_Id;
9701 Gen_Par : Entity_Id;
9702 First_Gen : Entity_Id;
9703 Elmt : Elmt_Id;
9704
9705 procedure Install_Noninstance_Specs (Par : Entity_Id);
9706 -- Install the scopes of noninstance parent units ending with Par
9707
9708 procedure Install_Spec (Par : Entity_Id);
9709 -- The child unit is within the declarative part of the parent, so the
9710 -- declarations within the parent are immediately visible.
9711
9712 -------------------------------
9713 -- Install_Noninstance_Specs --
9714 -------------------------------
9715
9716 procedure Install_Noninstance_Specs (Par : Entity_Id) is
9717 begin
9718 if Present (Par)
9719 and then Par /= Standard_Standard
9720 and then not In_Open_Scopes (Par)
9721 then
9722 Install_Noninstance_Specs (Scope (Par));
9723 Install_Spec (Par);
9724 end if;
9725 end Install_Noninstance_Specs;
9726
9727 ------------------
9728 -- Install_Spec --
9729 ------------------
9730
9731 procedure Install_Spec (Par : Entity_Id) is
9732 Spec : constant Node_Id := Package_Specification (Par);
9733
9734 begin
9735 -- If this parent of the child instance is a top-level unit,
9736 -- then record the unit and its visibility for later resetting in
9737 -- Remove_Parent. We exclude units that are generic instances, as we
9738 -- only want to record this information for the ultimate top-level
9739 -- noninstance parent (is that always correct???).
9740
9741 if Scope (Par) = Standard_Standard
9742 and then not Is_Generic_Instance (Par)
9743 then
9744 Parent_Unit_Visible := Is_Immediately_Visible (Par);
9745 Instance_Parent_Unit := Par;
9746 end if;
9747
9748 -- Open the parent scope and make it and its declarations visible.
9749 -- If this point is not within a body, then only the visible
9750 -- declarations should be made visible, and installation of the
9751 -- private declarations is deferred until the appropriate point
9752 -- within analysis of the spec being instantiated (see the handling
9753 -- of parent visibility in Analyze_Package_Specification). This is
9754 -- relaxed in the case where the parent unit is Ada.Tags, to avoid
9755 -- private view problems that occur when compiling instantiations of
9756 -- a generic child of that package (Generic_Dispatching_Constructor).
9757 -- If the instance freezes a tagged type, inlinings of operations
9758 -- from Ada.Tags may need the full view of type Tag. If inlining took
9759 -- proper account of establishing visibility of inlined subprograms'
9760 -- parents then it should be possible to remove this
9761 -- special check. ???
9762
9763 Push_Scope (Par);
9764 Set_Is_Immediately_Visible (Par);
9765 Install_Visible_Declarations (Par);
9766 Set_Use (Visible_Declarations (Spec));
9767
9768 if In_Body or else Is_RTU (Par, Ada_Tags) then
9769 Install_Private_Declarations (Par);
9770 Set_Use (Private_Declarations (Spec));
9771 end if;
9772 end Install_Spec;
9773
9774 -- Start of processing for Install_Parent
9775
9776 begin
9777 -- We need to install the parent instance to compile the instantiation
9778 -- of the child, but the child instance must appear in the current
9779 -- scope. Given that we cannot place the parent above the current scope
9780 -- in the scope stack, we duplicate the current scope and unstack both
9781 -- after the instantiation is complete.
9782
9783 -- If the parent is itself the instantiation of a child unit, we must
9784 -- also stack the instantiation of its parent, and so on. Each such
9785 -- ancestor is the prefix of the name in a prior instantiation.
9786
9787 -- If this is a nested instance, the parent unit itself resolves to
9788 -- a renaming of the parent instance, whose declaration we need.
9789
9790 -- Finally, the parent may be a generic (not an instance) when the
9791 -- child unit appears as a formal package.
9792
9793 Inst_Par := P;
9794
9795 if Present (Renamed_Entity (Inst_Par)) then
9796 Inst_Par := Renamed_Entity (Inst_Par);
9797 end if;
9798
9799 First_Par := Inst_Par;
9800
9801 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
9802
9803 First_Gen := Gen_Par;
9804
9805 while Present (Gen_Par) and then Is_Child_Unit (Gen_Par) loop
9806
9807 -- Load grandparent instance as well
9808
9809 Inst_Node := Get_Unit_Instantiation_Node (Inst_Par);
9810
9811 if Nkind (Name (Inst_Node)) = N_Expanded_Name then
9812 Inst_Par := Entity (Prefix (Name (Inst_Node)));
9813
9814 if Present (Renamed_Entity (Inst_Par)) then
9815 Inst_Par := Renamed_Entity (Inst_Par);
9816 end if;
9817
9818 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
9819
9820 if Present (Gen_Par) then
9821 Prepend_Elmt (Inst_Par, Ancestors);
9822
9823 else
9824 -- Parent is not the name of an instantiation
9825
9826 Install_Noninstance_Specs (Inst_Par);
9827 exit;
9828 end if;
9829
9830 else
9831 -- Previous error
9832
9833 exit;
9834 end if;
9835 end loop;
9836
9837 if Present (First_Gen) then
9838 Append_Elmt (First_Par, Ancestors);
9839 else
9840 Install_Noninstance_Specs (First_Par);
9841 end if;
9842
9843 if not Is_Empty_Elmt_List (Ancestors) then
9844 Elmt := First_Elmt (Ancestors);
9845 while Present (Elmt) loop
9846 Install_Spec (Node (Elmt));
9847 Install_Formal_Packages (Node (Elmt));
9848 Next_Elmt (Elmt);
9849 end loop;
9850 end if;
9851
9852 if not In_Body then
9853 Push_Scope (S);
9854 end if;
9855 end Install_Parent;
9856
9857 -------------------------------
9858 -- Install_Hidden_Primitives --
9859 -------------------------------
9860
9861 procedure Install_Hidden_Primitives
9862 (Prims_List : in out Elist_Id;
9863 Gen_T : Entity_Id;
9864 Act_T : Entity_Id)
9865 is
9866 Elmt : Elmt_Id;
9867 List : Elist_Id := No_Elist;
9868 Prim_G_Elmt : Elmt_Id;
9869 Prim_A_Elmt : Elmt_Id;
9870 Prim_G : Node_Id;
9871 Prim_A : Node_Id;
9872
9873 begin
9874 -- No action needed in case of serious errors because we cannot trust
9875 -- in the order of primitives
9876
9877 if Serious_Errors_Detected > 0 then
9878 return;
9879
9880 -- No action possible if we don't have available the list of primitive
9881 -- operations
9882
9883 elsif No (Gen_T)
9884 or else not Is_Record_Type (Gen_T)
9885 or else not Is_Tagged_Type (Gen_T)
9886 or else not Is_Record_Type (Act_T)
9887 or else not Is_Tagged_Type (Act_T)
9888 then
9889 return;
9890
9891 -- There is no need to handle interface types since their primitives
9892 -- cannot be hidden
9893
9894 elsif Is_Interface (Gen_T) then
9895 return;
9896 end if;
9897
9898 Prim_G_Elmt := First_Elmt (Primitive_Operations (Gen_T));
9899
9900 if not Is_Class_Wide_Type (Act_T) then
9901 Prim_A_Elmt := First_Elmt (Primitive_Operations (Act_T));
9902 else
9903 Prim_A_Elmt := First_Elmt (Primitive_Operations (Root_Type (Act_T)));
9904 end if;
9905
9906 loop
9907 -- Skip predefined primitives in the generic formal
9908
9909 while Present (Prim_G_Elmt)
9910 and then Is_Predefined_Dispatching_Operation (Node (Prim_G_Elmt))
9911 loop
9912 Next_Elmt (Prim_G_Elmt);
9913 end loop;
9914
9915 -- Skip predefined primitives in the generic actual
9916
9917 while Present (Prim_A_Elmt)
9918 and then Is_Predefined_Dispatching_Operation (Node (Prim_A_Elmt))
9919 loop
9920 Next_Elmt (Prim_A_Elmt);
9921 end loop;
9922
9923 exit when No (Prim_G_Elmt) or else No (Prim_A_Elmt);
9924
9925 Prim_G := Node (Prim_G_Elmt);
9926 Prim_A := Node (Prim_A_Elmt);
9927
9928 -- There is no need to handle interface primitives because their
9929 -- primitives are not hidden
9930
9931 exit when Present (Interface_Alias (Prim_G));
9932
9933 -- Here we install one hidden primitive
9934
9935 if Chars (Prim_G) /= Chars (Prim_A)
9936 and then Has_Suffix (Prim_A, 'P')
9937 and then Remove_Suffix (Prim_A, 'P') = Chars (Prim_G)
9938 then
9939 Set_Chars (Prim_A, Chars (Prim_G));
9940 Append_New_Elmt (Prim_A, To => List);
9941 end if;
9942
9943 Next_Elmt (Prim_A_Elmt);
9944 Next_Elmt (Prim_G_Elmt);
9945 end loop;
9946
9947 -- Append the elements to the list of temporarily visible primitives
9948 -- avoiding duplicates.
9949
9950 if Present (List) then
9951 if No (Prims_List) then
9952 Prims_List := New_Elmt_List;
9953 end if;
9954
9955 Elmt := First_Elmt (List);
9956 while Present (Elmt) loop
9957 Append_Unique_Elmt (Node (Elmt), Prims_List);
9958 Next_Elmt (Elmt);
9959 end loop;
9960 end if;
9961 end Install_Hidden_Primitives;
9962
9963 -------------------------------
9964 -- Restore_Hidden_Primitives --
9965 -------------------------------
9966
9967 procedure Restore_Hidden_Primitives (Prims_List : in out Elist_Id) is
9968 Prim_Elmt : Elmt_Id;
9969 Prim : Node_Id;
9970
9971 begin
9972 if Prims_List /= No_Elist then
9973 Prim_Elmt := First_Elmt (Prims_List);
9974 while Present (Prim_Elmt) loop
9975 Prim := Node (Prim_Elmt);
9976 Set_Chars (Prim, Add_Suffix (Prim, 'P'));
9977 Next_Elmt (Prim_Elmt);
9978 end loop;
9979
9980 Prims_List := No_Elist;
9981 end if;
9982 end Restore_Hidden_Primitives;
9983
9984 --------------------------------
9985 -- Instantiate_Formal_Package --
9986 --------------------------------
9987
9988 function Instantiate_Formal_Package
9989 (Formal : Node_Id;
9990 Actual : Node_Id;
9991 Analyzed_Formal : Node_Id) return List_Id
9992 is
9993 Loc : constant Source_Ptr := Sloc (Actual);
9994 Hidden_Formals : constant Elist_Id := New_Elmt_List;
9995 Actual_Pack : Entity_Id;
9996 Formal_Pack : Entity_Id;
9997 Gen_Parent : Entity_Id;
9998 Decls : List_Id;
9999 Nod : Node_Id;
10000 Parent_Spec : Node_Id;
10001
10002 procedure Find_Matching_Actual
10003 (F : Node_Id;
10004 Act : in out Entity_Id);
10005 -- We need to associate each formal entity in the formal package with
10006 -- the corresponding entity in the actual package. The actual package
10007 -- has been analyzed and possibly expanded, and as a result there is
10008 -- no one-to-one correspondence between the two lists (for example,
10009 -- the actual may include subtypes, itypes, and inherited primitive
10010 -- operations, interspersed among the renaming declarations for the
10011 -- actuals). We retrieve the corresponding actual by name because each
10012 -- actual has the same name as the formal, and they do appear in the
10013 -- same order.
10014
10015 function Get_Formal_Entity (N : Node_Id) return Entity_Id;
10016 -- Retrieve entity of defining entity of generic formal parameter.
10017 -- Only the declarations of formals need to be considered when
10018 -- linking them to actuals, but the declarative list may include
10019 -- internal entities generated during analysis, and those are ignored.
10020
10021 procedure Match_Formal_Entity
10022 (Formal_Node : Node_Id;
10023 Formal_Ent : Entity_Id;
10024 Actual_Ent : Entity_Id);
10025 -- Associates the formal entity with the actual. In the case where
10026 -- Formal_Ent is a formal package, this procedure iterates through all
10027 -- of its formals and enters associations between the actuals occurring
10028 -- in the formal package's corresponding actual package (given by
10029 -- Actual_Ent) and the formal package's formal parameters. This
10030 -- procedure recurses if any of the parameters is itself a package.
10031
10032 function Is_Instance_Of
10033 (Act_Spec : Entity_Id;
10034 Gen_Anc : Entity_Id) return Boolean;
10035 -- The actual can be an instantiation of a generic within another
10036 -- instance, in which case there is no direct link from it to the
10037 -- original generic ancestor. In that case, we recognize that the
10038 -- ultimate ancestor is the same by examining names and scopes.
10039
10040 procedure Process_Nested_Formal (Formal : Entity_Id);
10041 -- If the current formal is declared with a box, its own formals are
10042 -- visible in the instance, as they were in the generic, and their
10043 -- Hidden flag must be reset. If some of these formals are themselves
10044 -- packages declared with a box, the processing must be recursive.
10045
10046 --------------------------
10047 -- Find_Matching_Actual --
10048 --------------------------
10049
10050 procedure Find_Matching_Actual
10051 (F : Node_Id;
10052 Act : in out Entity_Id)
10053 is
10054 Formal_Ent : Entity_Id;
10055
10056 begin
10057 case Nkind (Original_Node (F)) is
10058 when N_Formal_Object_Declaration
10059 | N_Formal_Type_Declaration
10060 =>
10061 Formal_Ent := Defining_Identifier (F);
10062
10063 while Chars (Act) /= Chars (Formal_Ent) loop
10064 Next_Entity (Act);
10065 end loop;
10066
10067 when N_Formal_Package_Declaration
10068 | N_Formal_Subprogram_Declaration
10069 | N_Generic_Package_Declaration
10070 | N_Package_Declaration
10071 =>
10072 Formal_Ent := Defining_Entity (F);
10073
10074 while Chars (Act) /= Chars (Formal_Ent) loop
10075 Next_Entity (Act);
10076 end loop;
10077
10078 when others =>
10079 raise Program_Error;
10080 end case;
10081 end Find_Matching_Actual;
10082
10083 -------------------------
10084 -- Match_Formal_Entity --
10085 -------------------------
10086
10087 procedure Match_Formal_Entity
10088 (Formal_Node : Node_Id;
10089 Formal_Ent : Entity_Id;
10090 Actual_Ent : Entity_Id)
10091 is
10092 Act_Pkg : Entity_Id;
10093
10094 begin
10095 Set_Instance_Of (Formal_Ent, Actual_Ent);
10096
10097 if Ekind (Actual_Ent) = E_Package then
10098
10099 -- Record associations for each parameter
10100
10101 Act_Pkg := Actual_Ent;
10102
10103 declare
10104 A_Ent : Entity_Id := First_Entity (Act_Pkg);
10105 F_Ent : Entity_Id;
10106 F_Node : Node_Id;
10107
10108 Gen_Decl : Node_Id;
10109 Formals : List_Id;
10110 Actual : Entity_Id;
10111
10112 begin
10113 -- Retrieve the actual given in the formal package declaration
10114
10115 Actual := Entity (Name (Original_Node (Formal_Node)));
10116
10117 -- The actual in the formal package declaration may be a
10118 -- renamed generic package, in which case we want to retrieve
10119 -- the original generic in order to traverse its formal part.
10120
10121 if Present (Renamed_Entity (Actual)) then
10122 Gen_Decl := Unit_Declaration_Node (Renamed_Entity (Actual));
10123 else
10124 Gen_Decl := Unit_Declaration_Node (Actual);
10125 end if;
10126
10127 Formals := Generic_Formal_Declarations (Gen_Decl);
10128
10129 if Present (Formals) then
10130 F_Node := First_Non_Pragma (Formals);
10131 else
10132 F_Node := Empty;
10133 end if;
10134
10135 while Present (A_Ent)
10136 and then Present (F_Node)
10137 and then A_Ent /= First_Private_Entity (Act_Pkg)
10138 loop
10139 F_Ent := Get_Formal_Entity (F_Node);
10140
10141 if Present (F_Ent) then
10142
10143 -- This is a formal of the original package. Record
10144 -- association and recurse.
10145
10146 Find_Matching_Actual (F_Node, A_Ent);
10147 Match_Formal_Entity (F_Node, F_Ent, A_Ent);
10148 Next_Entity (A_Ent);
10149 end if;
10150
10151 Next_Non_Pragma (F_Node);
10152 end loop;
10153 end;
10154 end if;
10155 end Match_Formal_Entity;
10156
10157 -----------------------
10158 -- Get_Formal_Entity --
10159 -----------------------
10160
10161 function Get_Formal_Entity (N : Node_Id) return Entity_Id is
10162 Kind : constant Node_Kind := Nkind (Original_Node (N));
10163 begin
10164 case Kind is
10165 when N_Formal_Object_Declaration =>
10166 return Defining_Identifier (N);
10167
10168 when N_Formal_Type_Declaration =>
10169 return Defining_Identifier (N);
10170
10171 when N_Formal_Subprogram_Declaration =>
10172 return Defining_Unit_Name (Specification (N));
10173
10174 when N_Formal_Package_Declaration =>
10175 return Defining_Identifier (Original_Node (N));
10176
10177 when N_Generic_Package_Declaration =>
10178 return Defining_Identifier (Original_Node (N));
10179
10180 -- All other declarations are introduced by semantic analysis and
10181 -- have no match in the actual.
10182
10183 when others =>
10184 return Empty;
10185 end case;
10186 end Get_Formal_Entity;
10187
10188 --------------------
10189 -- Is_Instance_Of --
10190 --------------------
10191
10192 function Is_Instance_Of
10193 (Act_Spec : Entity_Id;
10194 Gen_Anc : Entity_Id) return Boolean
10195 is
10196 Gen_Par : constant Entity_Id := Generic_Parent (Act_Spec);
10197
10198 begin
10199 if No (Gen_Par) then
10200 return False;
10201
10202 -- Simplest case: the generic parent of the actual is the formal
10203
10204 elsif Gen_Par = Gen_Anc then
10205 return True;
10206
10207 elsif Chars (Gen_Par) /= Chars (Gen_Anc) then
10208 return False;
10209
10210 -- The actual may be obtained through several instantiations. Its
10211 -- scope must itself be an instance of a generic declared in the
10212 -- same scope as the formal. Any other case is detected above.
10213
10214 elsif not Is_Generic_Instance (Scope (Gen_Par)) then
10215 return False;
10216
10217 else
10218 return Generic_Parent (Parent (Scope (Gen_Par))) = Scope (Gen_Anc);
10219 end if;
10220 end Is_Instance_Of;
10221
10222 ---------------------------
10223 -- Process_Nested_Formal --
10224 ---------------------------
10225
10226 procedure Process_Nested_Formal (Formal : Entity_Id) is
10227 Ent : Entity_Id;
10228
10229 begin
10230 if Present (Associated_Formal_Package (Formal))
10231 and then Box_Present (Parent (Associated_Formal_Package (Formal)))
10232 then
10233 Ent := First_Entity (Formal);
10234 while Present (Ent) loop
10235 Set_Is_Hidden (Ent, False);
10236 Set_Is_Visible_Formal (Ent);
10237 Set_Is_Potentially_Use_Visible
10238 (Ent, Is_Potentially_Use_Visible (Formal));
10239
10240 if Ekind (Ent) = E_Package then
10241 exit when Renamed_Entity (Ent) = Renamed_Entity (Formal);
10242 Process_Nested_Formal (Ent);
10243 end if;
10244
10245 Next_Entity (Ent);
10246 end loop;
10247 end if;
10248 end Process_Nested_Formal;
10249
10250 -- Start of processing for Instantiate_Formal_Package
10251
10252 begin
10253 Analyze (Actual);
10254
10255 if not Is_Entity_Name (Actual)
10256 or else Ekind (Entity (Actual)) /= E_Package
10257 then
10258 Error_Msg_N
10259 ("expect package instance to instantiate formal", Actual);
10260 Abandon_Instantiation (Actual);
10261 raise Program_Error;
10262
10263 else
10264 Actual_Pack := Entity (Actual);
10265 Set_Is_Instantiated (Actual_Pack);
10266
10267 -- The actual may be a renamed package, or an outer generic formal
10268 -- package whose instantiation is converted into a renaming.
10269
10270 if Present (Renamed_Object (Actual_Pack)) then
10271 Actual_Pack := Renamed_Object (Actual_Pack);
10272 end if;
10273
10274 if Nkind (Analyzed_Formal) = N_Formal_Package_Declaration then
10275 Gen_Parent := Get_Instance_Of (Entity (Name (Analyzed_Formal)));
10276 Formal_Pack := Defining_Identifier (Analyzed_Formal);
10277 else
10278 Gen_Parent :=
10279 Generic_Parent (Specification (Analyzed_Formal));
10280 Formal_Pack :=
10281 Defining_Unit_Name (Specification (Analyzed_Formal));
10282 end if;
10283
10284 if Nkind (Parent (Actual_Pack)) = N_Defining_Program_Unit_Name then
10285 Parent_Spec := Package_Specification (Actual_Pack);
10286 else
10287 Parent_Spec := Parent (Actual_Pack);
10288 end if;
10289
10290 if Gen_Parent = Any_Id then
10291 Error_Msg_N
10292 ("previous error in declaration of formal package", Actual);
10293 Abandon_Instantiation (Actual);
10294
10295 elsif
10296 Is_Instance_Of (Parent_Spec, Get_Instance_Of (Gen_Parent))
10297 then
10298 null;
10299
10300 else
10301 Error_Msg_NE
10302 ("actual parameter must be instance of&", Actual, Gen_Parent);
10303 Abandon_Instantiation (Actual);
10304 end if;
10305
10306 Set_Instance_Of (Defining_Identifier (Formal), Actual_Pack);
10307 Map_Formal_Package_Entities (Formal_Pack, Actual_Pack);
10308
10309 Nod :=
10310 Make_Package_Renaming_Declaration (Loc,
10311 Defining_Unit_Name => New_Copy (Defining_Identifier (Formal)),
10312 Name => New_Occurrence_Of (Actual_Pack, Loc));
10313
10314 Set_Associated_Formal_Package
10315 (Defining_Unit_Name (Nod), Defining_Identifier (Formal));
10316 Decls := New_List (Nod);
10317
10318 -- If the formal F has a box, then the generic declarations are
10319 -- visible in the generic G. In an instance of G, the corresponding
10320 -- entities in the actual for F (which are the actuals for the
10321 -- instantiation of the generic that F denotes) must also be made
10322 -- visible for analysis of the current instance. On exit from the
10323 -- current instance, those entities are made private again. If the
10324 -- actual is currently in use, these entities are also use-visible.
10325
10326 -- The loop through the actual entities also steps through the formal
10327 -- entities and enters associations from formals to actuals into the
10328 -- renaming map. This is necessary to properly handle checking of
10329 -- actual parameter associations for later formals that depend on
10330 -- actuals declared in the formal package.
10331
10332 -- In Ada 2005, partial parameterization requires that we make
10333 -- visible the actuals corresponding to formals that were defaulted
10334 -- in the formal package. There formals are identified because they
10335 -- remain formal generics within the formal package, rather than
10336 -- being renamings of the actuals supplied.
10337
10338 declare
10339 Gen_Decl : constant Node_Id :=
10340 Unit_Declaration_Node (Gen_Parent);
10341 Formals : constant List_Id :=
10342 Generic_Formal_Declarations (Gen_Decl);
10343
10344 Actual_Ent : Entity_Id;
10345 Actual_Of_Formal : Node_Id;
10346 Formal_Node : Node_Id;
10347 Formal_Ent : Entity_Id;
10348
10349 begin
10350 if Present (Formals) then
10351 Formal_Node := First_Non_Pragma (Formals);
10352 else
10353 Formal_Node := Empty;
10354 end if;
10355
10356 Actual_Ent := First_Entity (Actual_Pack);
10357 Actual_Of_Formal :=
10358 First (Visible_Declarations (Specification (Analyzed_Formal)));
10359 while Present (Actual_Ent)
10360 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
10361 loop
10362 if Present (Formal_Node) then
10363 Formal_Ent := Get_Formal_Entity (Formal_Node);
10364
10365 if Present (Formal_Ent) then
10366 Find_Matching_Actual (Formal_Node, Actual_Ent);
10367 Match_Formal_Entity (Formal_Node, Formal_Ent, Actual_Ent);
10368
10369 -- We iterate at the same time over the actuals of the
10370 -- local package created for the formal, to determine
10371 -- which one of the formals of the original generic were
10372 -- defaulted in the formal. The corresponding actual
10373 -- entities are visible in the enclosing instance.
10374
10375 if Box_Present (Formal)
10376 or else
10377 (Present (Actual_Of_Formal)
10378 and then
10379 Is_Generic_Formal
10380 (Get_Formal_Entity (Actual_Of_Formal)))
10381 then
10382 Set_Is_Hidden (Actual_Ent, False);
10383 Set_Is_Visible_Formal (Actual_Ent);
10384 Set_Is_Potentially_Use_Visible
10385 (Actual_Ent, In_Use (Actual_Pack));
10386
10387 if Ekind (Actual_Ent) = E_Package then
10388 Process_Nested_Formal (Actual_Ent);
10389 end if;
10390
10391 else
10392 if not Is_Hidden (Actual_Ent) then
10393 Append_Elmt (Actual_Ent, Hidden_Formals);
10394 end if;
10395
10396 Set_Is_Hidden (Actual_Ent);
10397 Set_Is_Potentially_Use_Visible (Actual_Ent, False);
10398 end if;
10399 end if;
10400
10401 Next_Non_Pragma (Formal_Node);
10402 Next (Actual_Of_Formal);
10403
10404 else
10405 -- No further formals to match, but the generic part may
10406 -- contain inherited operation that are not hidden in the
10407 -- enclosing instance.
10408
10409 Next_Entity (Actual_Ent);
10410 end if;
10411 end loop;
10412
10413 -- Inherited subprograms generated by formal derived types are
10414 -- also visible if the types are.
10415
10416 Actual_Ent := First_Entity (Actual_Pack);
10417 while Present (Actual_Ent)
10418 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
10419 loop
10420 if Is_Overloadable (Actual_Ent)
10421 and then
10422 Nkind (Parent (Actual_Ent)) = N_Subtype_Declaration
10423 and then
10424 not Is_Hidden (Defining_Identifier (Parent (Actual_Ent)))
10425 then
10426 Set_Is_Hidden (Actual_Ent, False);
10427 Set_Is_Potentially_Use_Visible
10428 (Actual_Ent, In_Use (Actual_Pack));
10429 end if;
10430
10431 Next_Entity (Actual_Ent);
10432 end loop;
10433 end;
10434
10435 -- If the formal is not declared with a box, reanalyze it as an
10436 -- abbreviated instantiation, to verify the matching rules of 12.7.
10437 -- The actual checks are performed after the generic associations
10438 -- have been analyzed, to guarantee the same visibility for this
10439 -- instantiation and for the actuals.
10440
10441 -- In Ada 2005, the generic associations for the formal can include
10442 -- defaulted parameters. These are ignored during check. This
10443 -- internal instantiation is removed from the tree after conformance
10444 -- checking, because it contains formal declarations for those
10445 -- defaulted parameters, and those should not reach the back-end.
10446
10447 if not Box_Present (Formal) then
10448 declare
10449 I_Pack : constant Entity_Id :=
10450 Make_Temporary (Sloc (Actual), 'P');
10451
10452 begin
10453 Set_Is_Internal (I_Pack);
10454 Set_Ekind (I_Pack, E_Package);
10455 Set_Hidden_In_Formal_Instance (I_Pack, Hidden_Formals);
10456
10457 Append_To (Decls,
10458 Make_Package_Instantiation (Sloc (Actual),
10459 Defining_Unit_Name => I_Pack,
10460 Name =>
10461 New_Occurrence_Of
10462 (Get_Instance_Of (Gen_Parent), Sloc (Actual)),
10463 Generic_Associations => Generic_Associations (Formal)));
10464 end;
10465 end if;
10466
10467 return Decls;
10468 end if;
10469 end Instantiate_Formal_Package;
10470
10471 -----------------------------------
10472 -- Instantiate_Formal_Subprogram --
10473 -----------------------------------
10474
10475 function Instantiate_Formal_Subprogram
10476 (Formal : Node_Id;
10477 Actual : Node_Id;
10478 Analyzed_Formal : Node_Id) return Node_Id
10479 is
10480 Analyzed_S : constant Entity_Id :=
10481 Defining_Unit_Name (Specification (Analyzed_Formal));
10482 Formal_Sub : constant Entity_Id :=
10483 Defining_Unit_Name (Specification (Formal));
10484
10485 function From_Parent_Scope (Subp : Entity_Id) return Boolean;
10486 -- If the generic is a child unit, the parent has been installed on the
10487 -- scope stack, but a default subprogram cannot resolve to something
10488 -- on the parent because that parent is not really part of the visible
10489 -- context (it is there to resolve explicit local entities). If the
10490 -- default has resolved in this way, we remove the entity from immediate
10491 -- visibility and analyze the node again to emit an error message or
10492 -- find another visible candidate.
10493
10494 procedure Valid_Actual_Subprogram (Act : Node_Id);
10495 -- Perform legality check and raise exception on failure
10496
10497 -----------------------
10498 -- From_Parent_Scope --
10499 -----------------------
10500
10501 function From_Parent_Scope (Subp : Entity_Id) return Boolean is
10502 Gen_Scope : Node_Id;
10503
10504 begin
10505 Gen_Scope := Scope (Analyzed_S);
10506 while Present (Gen_Scope) and then Is_Child_Unit (Gen_Scope) loop
10507 if Scope (Subp) = Scope (Gen_Scope) then
10508 return True;
10509 end if;
10510
10511 Gen_Scope := Scope (Gen_Scope);
10512 end loop;
10513
10514 return False;
10515 end From_Parent_Scope;
10516
10517 -----------------------------
10518 -- Valid_Actual_Subprogram --
10519 -----------------------------
10520
10521 procedure Valid_Actual_Subprogram (Act : Node_Id) is
10522 Act_E : Entity_Id;
10523
10524 begin
10525 if Is_Entity_Name (Act) then
10526 Act_E := Entity (Act);
10527
10528 elsif Nkind (Act) = N_Selected_Component
10529 and then Is_Entity_Name (Selector_Name (Act))
10530 then
10531 Act_E := Entity (Selector_Name (Act));
10532
10533 else
10534 Act_E := Empty;
10535 end if;
10536
10537 if (Present (Act_E) and then Is_Overloadable (Act_E))
10538 or else Nkind_In (Act, N_Attribute_Reference,
10539 N_Indexed_Component,
10540 N_Character_Literal,
10541 N_Explicit_Dereference)
10542 then
10543 return;
10544 end if;
10545
10546 Error_Msg_NE
10547 ("expect subprogram or entry name in instantiation of &",
10548 Instantiation_Node, Formal_Sub);
10549 Abandon_Instantiation (Instantiation_Node);
10550 end Valid_Actual_Subprogram;
10551
10552 -- Local variables
10553
10554 Decl_Node : Node_Id;
10555 Loc : Source_Ptr;
10556 Nam : Node_Id;
10557 New_Spec : Node_Id;
10558 New_Subp : Entity_Id;
10559
10560 -- Start of processing for Instantiate_Formal_Subprogram
10561
10562 begin
10563 New_Spec := New_Copy_Tree (Specification (Formal));
10564
10565 -- The tree copy has created the proper instantiation sloc for the
10566 -- new specification. Use this location for all other constructed
10567 -- declarations.
10568
10569 Loc := Sloc (Defining_Unit_Name (New_Spec));
10570
10571 -- Create new entity for the actual (New_Copy_Tree does not), and
10572 -- indicate that it is an actual.
10573
10574 New_Subp := Make_Defining_Identifier (Loc, Chars (Formal_Sub));
10575 Set_Ekind (New_Subp, Ekind (Analyzed_S));
10576 Set_Is_Generic_Actual_Subprogram (New_Subp);
10577 Set_Defining_Unit_Name (New_Spec, New_Subp);
10578
10579 -- Create new entities for the each of the formals in the specification
10580 -- of the renaming declaration built for the actual.
10581
10582 if Present (Parameter_Specifications (New_Spec)) then
10583 declare
10584 F : Node_Id;
10585 F_Id : Entity_Id;
10586
10587 begin
10588 F := First (Parameter_Specifications (New_Spec));
10589 while Present (F) loop
10590 F_Id := Defining_Identifier (F);
10591
10592 Set_Defining_Identifier (F,
10593 Make_Defining_Identifier (Sloc (F_Id), Chars (F_Id)));
10594 Next (F);
10595 end loop;
10596 end;
10597 end if;
10598
10599 -- Find entity of actual. If the actual is an attribute reference, it
10600 -- cannot be resolved here (its formal is missing) but is handled
10601 -- instead in Attribute_Renaming. If the actual is overloaded, it is
10602 -- fully resolved subsequently, when the renaming declaration for the
10603 -- formal is analyzed. If it is an explicit dereference, resolve the
10604 -- prefix but not the actual itself, to prevent interpretation as call.
10605
10606 if Present (Actual) then
10607 Loc := Sloc (Actual);
10608 Set_Sloc (New_Spec, Loc);
10609
10610 if Nkind (Actual) = N_Operator_Symbol then
10611 Find_Direct_Name (Actual);
10612
10613 elsif Nkind (Actual) = N_Explicit_Dereference then
10614 Analyze (Prefix (Actual));
10615
10616 elsif Nkind (Actual) /= N_Attribute_Reference then
10617 Analyze (Actual);
10618 end if;
10619
10620 Valid_Actual_Subprogram (Actual);
10621 Nam := Actual;
10622
10623 elsif Present (Default_Name (Formal)) then
10624 if not Nkind_In (Default_Name (Formal), N_Attribute_Reference,
10625 N_Selected_Component,
10626 N_Indexed_Component,
10627 N_Character_Literal)
10628 and then Present (Entity (Default_Name (Formal)))
10629 then
10630 Nam := New_Occurrence_Of (Entity (Default_Name (Formal)), Loc);
10631 else
10632 Nam := New_Copy (Default_Name (Formal));
10633 Set_Sloc (Nam, Loc);
10634 end if;
10635
10636 elsif Box_Present (Formal) then
10637
10638 -- Actual is resolved at the point of instantiation. Create an
10639 -- identifier or operator with the same name as the formal.
10640
10641 if Nkind (Formal_Sub) = N_Defining_Operator_Symbol then
10642 Nam :=
10643 Make_Operator_Symbol (Loc,
10644 Chars => Chars (Formal_Sub),
10645 Strval => No_String);
10646 else
10647 Nam := Make_Identifier (Loc, Chars (Formal_Sub));
10648 end if;
10649
10650 elsif Nkind (Specification (Formal)) = N_Procedure_Specification
10651 and then Null_Present (Specification (Formal))
10652 then
10653 -- Generate null body for procedure, for use in the instance
10654
10655 Decl_Node :=
10656 Make_Subprogram_Body (Loc,
10657 Specification => New_Spec,
10658 Declarations => New_List,
10659 Handled_Statement_Sequence =>
10660 Make_Handled_Sequence_Of_Statements (Loc,
10661 Statements => New_List (Make_Null_Statement (Loc))));
10662
10663 Set_Is_Intrinsic_Subprogram (Defining_Unit_Name (New_Spec));
10664 return Decl_Node;
10665
10666 else
10667 Error_Msg_Sloc := Sloc (Scope (Analyzed_S));
10668 Error_Msg_NE
10669 ("missing actual&", Instantiation_Node, Formal_Sub);
10670 Error_Msg_NE
10671 ("\in instantiation of & declared#",
10672 Instantiation_Node, Scope (Analyzed_S));
10673 Abandon_Instantiation (Instantiation_Node);
10674 end if;
10675
10676 Decl_Node :=
10677 Make_Subprogram_Renaming_Declaration (Loc,
10678 Specification => New_Spec,
10679 Name => Nam);
10680
10681 -- If we do not have an actual and the formal specified <> then set to
10682 -- get proper default.
10683
10684 if No (Actual) and then Box_Present (Formal) then
10685 Set_From_Default (Decl_Node);
10686 end if;
10687
10688 -- Gather possible interpretations for the actual before analyzing the
10689 -- instance. If overloaded, it will be resolved when analyzing the
10690 -- renaming declaration.
10691
10692 if Box_Present (Formal) and then No (Actual) then
10693 Analyze (Nam);
10694
10695 if Is_Child_Unit (Scope (Analyzed_S))
10696 and then Present (Entity (Nam))
10697 then
10698 if not Is_Overloaded (Nam) then
10699 if From_Parent_Scope (Entity (Nam)) then
10700 Set_Is_Immediately_Visible (Entity (Nam), False);
10701 Set_Entity (Nam, Empty);
10702 Set_Etype (Nam, Empty);
10703
10704 Analyze (Nam);
10705 Set_Is_Immediately_Visible (Entity (Nam));
10706 end if;
10707
10708 else
10709 declare
10710 I : Interp_Index;
10711 It : Interp;
10712
10713 begin
10714 Get_First_Interp (Nam, I, It);
10715 while Present (It.Nam) loop
10716 if From_Parent_Scope (It.Nam) then
10717 Remove_Interp (I);
10718 end if;
10719
10720 Get_Next_Interp (I, It);
10721 end loop;
10722 end;
10723 end if;
10724 end if;
10725 end if;
10726
10727 -- The generic instantiation freezes the actual. This can only be done
10728 -- once the actual is resolved, in the analysis of the renaming
10729 -- declaration. To make the formal subprogram entity available, we set
10730 -- Corresponding_Formal_Spec to point to the formal subprogram entity.
10731 -- This is also needed in Analyze_Subprogram_Renaming for the processing
10732 -- of formal abstract subprograms.
10733
10734 Set_Corresponding_Formal_Spec (Decl_Node, Analyzed_S);
10735
10736 -- We cannot analyze the renaming declaration, and thus find the actual,
10737 -- until all the actuals are assembled in the instance. For subsequent
10738 -- checks of other actuals, indicate the node that will hold the
10739 -- instance of this formal.
10740
10741 Set_Instance_Of (Analyzed_S, Nam);
10742
10743 if Nkind (Actual) = N_Selected_Component
10744 and then Is_Task_Type (Etype (Prefix (Actual)))
10745 and then not Is_Frozen (Etype (Prefix (Actual)))
10746 then
10747 -- The renaming declaration will create a body, which must appear
10748 -- outside of the instantiation, We move the renaming declaration
10749 -- out of the instance, and create an additional renaming inside,
10750 -- to prevent freezing anomalies.
10751
10752 declare
10753 Anon_Id : constant Entity_Id := Make_Temporary (Loc, 'E');
10754
10755 begin
10756 Set_Defining_Unit_Name (New_Spec, Anon_Id);
10757 Insert_Before (Instantiation_Node, Decl_Node);
10758 Analyze (Decl_Node);
10759
10760 -- Now create renaming within the instance
10761
10762 Decl_Node :=
10763 Make_Subprogram_Renaming_Declaration (Loc,
10764 Specification => New_Copy_Tree (New_Spec),
10765 Name => New_Occurrence_Of (Anon_Id, Loc));
10766
10767 Set_Defining_Unit_Name (Specification (Decl_Node),
10768 Make_Defining_Identifier (Loc, Chars (Formal_Sub)));
10769 end;
10770 end if;
10771
10772 return Decl_Node;
10773 end Instantiate_Formal_Subprogram;
10774
10775 ------------------------
10776 -- Instantiate_Object --
10777 ------------------------
10778
10779 function Instantiate_Object
10780 (Formal : Node_Id;
10781 Actual : Node_Id;
10782 Analyzed_Formal : Node_Id) return List_Id
10783 is
10784 Gen_Obj : constant Entity_Id := Defining_Identifier (Formal);
10785 A_Gen_Obj : constant Entity_Id :=
10786 Defining_Identifier (Analyzed_Formal);
10787 Acc_Def : Node_Id := Empty;
10788 Act_Assoc : constant Node_Id := Parent (Actual);
10789 Actual_Decl : Node_Id := Empty;
10790 Decl_Node : Node_Id;
10791 Def : Node_Id;
10792 Ftyp : Entity_Id;
10793 List : constant List_Id := New_List;
10794 Loc : constant Source_Ptr := Sloc (Actual);
10795 Orig_Ftyp : constant Entity_Id := Etype (A_Gen_Obj);
10796 Subt_Decl : Node_Id := Empty;
10797 Subt_Mark : Node_Id := Empty;
10798
10799 function Copy_Access_Def return Node_Id;
10800 -- If formal is an anonymous access, copy access definition of formal
10801 -- for generated object declaration.
10802
10803 ---------------------
10804 -- Copy_Access_Def --
10805 ---------------------
10806
10807 function Copy_Access_Def return Node_Id is
10808 begin
10809 Def := New_Copy_Tree (Acc_Def);
10810
10811 -- In addition, if formal is an access to subprogram we need to
10812 -- generate new formals for the signature of the default, so that
10813 -- the tree is properly formatted for ASIS use.
10814
10815 if Present (Access_To_Subprogram_Definition (Acc_Def)) then
10816 declare
10817 Par_Spec : Node_Id;
10818 begin
10819 Par_Spec :=
10820 First (Parameter_Specifications
10821 (Access_To_Subprogram_Definition (Def)));
10822 while Present (Par_Spec) loop
10823 Set_Defining_Identifier (Par_Spec,
10824 Make_Defining_Identifier (Sloc (Acc_Def),
10825 Chars => Chars (Defining_Identifier (Par_Spec))));
10826 Next (Par_Spec);
10827 end loop;
10828 end;
10829 end if;
10830
10831 return Def;
10832 end Copy_Access_Def;
10833
10834 -- Start of processing for Instantiate_Object
10835
10836 begin
10837 -- Formal may be an anonymous access
10838
10839 if Present (Subtype_Mark (Formal)) then
10840 Subt_Mark := Subtype_Mark (Formal);
10841 else
10842 Check_Access_Definition (Formal);
10843 Acc_Def := Access_Definition (Formal);
10844 end if;
10845
10846 -- Sloc for error message on missing actual
10847
10848 Error_Msg_Sloc := Sloc (Scope (A_Gen_Obj));
10849
10850 if Get_Instance_Of (Gen_Obj) /= Gen_Obj then
10851 Error_Msg_N ("duplicate instantiation of generic parameter", Actual);
10852 end if;
10853
10854 Set_Parent (List, Parent (Actual));
10855
10856 -- OUT present
10857
10858 if Out_Present (Formal) then
10859
10860 -- An IN OUT generic actual must be a name. The instantiation is a
10861 -- renaming declaration. The actual is the name being renamed. We
10862 -- use the actual directly, rather than a copy, because it is not
10863 -- used further in the list of actuals, and because a copy or a use
10864 -- of relocate_node is incorrect if the instance is nested within a
10865 -- generic. In order to simplify ASIS searches, the Generic_Parent
10866 -- field links the declaration to the generic association.
10867
10868 if No (Actual) then
10869 Error_Msg_NE
10870 ("missing actual &",
10871 Instantiation_Node, Gen_Obj);
10872 Error_Msg_NE
10873 ("\in instantiation of & declared#",
10874 Instantiation_Node, Scope (A_Gen_Obj));
10875 Abandon_Instantiation (Instantiation_Node);
10876 end if;
10877
10878 if Present (Subt_Mark) then
10879 Decl_Node :=
10880 Make_Object_Renaming_Declaration (Loc,
10881 Defining_Identifier => New_Copy (Gen_Obj),
10882 Subtype_Mark => New_Copy_Tree (Subt_Mark),
10883 Name => Actual);
10884
10885 else pragma Assert (Present (Acc_Def));
10886 Decl_Node :=
10887 Make_Object_Renaming_Declaration (Loc,
10888 Defining_Identifier => New_Copy (Gen_Obj),
10889 Access_Definition => New_Copy_Tree (Acc_Def),
10890 Name => Actual);
10891 end if;
10892
10893 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
10894
10895 -- The analysis of the actual may produce Insert_Action nodes, so
10896 -- the declaration must have a context in which to attach them.
10897
10898 Append (Decl_Node, List);
10899 Analyze (Actual);
10900
10901 -- Return if the analysis of the actual reported some error
10902
10903 if Etype (Actual) = Any_Type then
10904 return List;
10905 end if;
10906
10907 -- This check is performed here because Analyze_Object_Renaming will
10908 -- not check it when Comes_From_Source is False. Note though that the
10909 -- check for the actual being the name of an object will be performed
10910 -- in Analyze_Object_Renaming.
10911
10912 if Is_Object_Reference (Actual)
10913 and then Is_Dependent_Component_Of_Mutable_Object (Actual)
10914 then
10915 Error_Msg_N
10916 ("illegal discriminant-dependent component for in out parameter",
10917 Actual);
10918 end if;
10919
10920 -- The actual has to be resolved in order to check that it is a
10921 -- variable (due to cases such as F (1), where F returns access to
10922 -- an array, and for overloaded prefixes).
10923
10924 Ftyp := Get_Instance_Of (Etype (A_Gen_Obj));
10925
10926 -- If the type of the formal is not itself a formal, and the current
10927 -- unit is a child unit, the formal type must be declared in a
10928 -- parent, and must be retrieved by visibility.
10929
10930 if Ftyp = Orig_Ftyp
10931 and then Is_Generic_Unit (Scope (Ftyp))
10932 and then Is_Child_Unit (Scope (A_Gen_Obj))
10933 then
10934 declare
10935 Temp : constant Node_Id :=
10936 New_Copy_Tree (Subtype_Mark (Analyzed_Formal));
10937 begin
10938 Set_Entity (Temp, Empty);
10939 Find_Type (Temp);
10940 Ftyp := Entity (Temp);
10941 end;
10942 end if;
10943
10944 if Is_Private_Type (Ftyp)
10945 and then not Is_Private_Type (Etype (Actual))
10946 and then (Base_Type (Full_View (Ftyp)) = Base_Type (Etype (Actual))
10947 or else Base_Type (Etype (Actual)) = Ftyp)
10948 then
10949 -- If the actual has the type of the full view of the formal, or
10950 -- else a non-private subtype of the formal, then the visibility
10951 -- of the formal type has changed. Add to the actuals a subtype
10952 -- declaration that will force the exchange of views in the body
10953 -- of the instance as well.
10954
10955 Subt_Decl :=
10956 Make_Subtype_Declaration (Loc,
10957 Defining_Identifier => Make_Temporary (Loc, 'P'),
10958 Subtype_Indication => New_Occurrence_Of (Ftyp, Loc));
10959
10960 Prepend (Subt_Decl, List);
10961
10962 Prepend_Elmt (Full_View (Ftyp), Exchanged_Views);
10963 Exchange_Declarations (Ftyp);
10964 end if;
10965
10966 Resolve (Actual, Ftyp);
10967
10968 if not Denotes_Variable (Actual) then
10969 Error_Msg_NE ("actual for& must be a variable", Actual, Gen_Obj);
10970
10971 elsif Base_Type (Ftyp) /= Base_Type (Etype (Actual)) then
10972
10973 -- Ada 2005 (AI-423): For a generic formal object of mode in out,
10974 -- the type of the actual shall resolve to a specific anonymous
10975 -- access type.
10976
10977 if Ada_Version < Ada_2005
10978 or else Ekind (Base_Type (Ftyp)) /=
10979 E_Anonymous_Access_Type
10980 or else Ekind (Base_Type (Etype (Actual))) /=
10981 E_Anonymous_Access_Type
10982 then
10983 Error_Msg_NE
10984 ("type of actual does not match type of&", Actual, Gen_Obj);
10985 end if;
10986 end if;
10987
10988 Note_Possible_Modification (Actual, Sure => True);
10989
10990 -- Check for instantiation of atomic/volatile actual for
10991 -- non-atomic/volatile formal (RM C.6 (12)).
10992
10993 if Is_Atomic_Object (Actual) and then not Is_Atomic (Orig_Ftyp) then
10994 Error_Msg_N
10995 ("cannot instantiate non-atomic formal object "
10996 & "with atomic actual", Actual);
10997
10998 elsif Is_Volatile_Object (Actual) and then not Is_Volatile (Orig_Ftyp)
10999 then
11000 Error_Msg_N
11001 ("cannot instantiate non-volatile formal object "
11002 & "with volatile actual", Actual);
11003 end if;
11004
11005 -- Formal in-parameter
11006
11007 else
11008 -- The instantiation of a generic formal in-parameter is constant
11009 -- declaration. The actual is the expression for that declaration.
11010 -- Its type is a full copy of the type of the formal. This may be
11011 -- an access to subprogram, for which we need to generate entities
11012 -- for the formals in the new signature.
11013
11014 if Present (Actual) then
11015 if Present (Subt_Mark) then
11016 Def := New_Copy_Tree (Subt_Mark);
11017 else pragma Assert (Present (Acc_Def));
11018 Def := Copy_Access_Def;
11019 end if;
11020
11021 Decl_Node :=
11022 Make_Object_Declaration (Loc,
11023 Defining_Identifier => New_Copy (Gen_Obj),
11024 Constant_Present => True,
11025 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
11026 Object_Definition => Def,
11027 Expression => Actual);
11028
11029 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
11030
11031 -- A generic formal object of a tagged type is defined to be
11032 -- aliased so the new constant must also be treated as aliased.
11033
11034 if Is_Tagged_Type (Etype (A_Gen_Obj)) then
11035 Set_Aliased_Present (Decl_Node);
11036 end if;
11037
11038 Append (Decl_Node, List);
11039
11040 -- No need to repeat (pre-)analysis of some expression nodes
11041 -- already handled in Preanalyze_Actuals.
11042
11043 if Nkind (Actual) /= N_Allocator then
11044 Analyze (Actual);
11045
11046 -- Return if the analysis of the actual reported some error
11047
11048 if Etype (Actual) = Any_Type then
11049 return List;
11050 end if;
11051 end if;
11052
11053 declare
11054 Formal_Type : constant Entity_Id := Etype (A_Gen_Obj);
11055 Typ : Entity_Id;
11056
11057 begin
11058 Typ := Get_Instance_Of (Formal_Type);
11059
11060 -- If the actual appears in the current or an enclosing scope,
11061 -- use its type directly. This is relevant if it has an actual
11062 -- subtype that is distinct from its nominal one. This cannot
11063 -- be done in general because the type of the actual may
11064 -- depend on other actuals, and only be fully determined when
11065 -- the enclosing instance is analyzed.
11066
11067 if Present (Etype (Actual))
11068 and then Is_Constr_Subt_For_U_Nominal (Etype (Actual))
11069 then
11070 Freeze_Before (Instantiation_Node, Etype (Actual));
11071 else
11072 Freeze_Before (Instantiation_Node, Typ);
11073 end if;
11074
11075 -- If the actual is an aggregate, perform name resolution on
11076 -- its components (the analysis of an aggregate does not do it)
11077 -- to capture local names that may be hidden if the generic is
11078 -- a child unit.
11079
11080 if Nkind (Actual) = N_Aggregate then
11081 Preanalyze_And_Resolve (Actual, Typ);
11082 end if;
11083
11084 if Is_Limited_Type (Typ)
11085 and then not OK_For_Limited_Init (Typ, Actual)
11086 then
11087 Error_Msg_N
11088 ("initialization not allowed for limited types", Actual);
11089 Explain_Limited_Type (Typ, Actual);
11090 end if;
11091 end;
11092
11093 elsif Present (Default_Expression (Formal)) then
11094
11095 -- Use default to construct declaration
11096
11097 if Present (Subt_Mark) then
11098 Def := New_Copy (Subt_Mark);
11099 else pragma Assert (Present (Acc_Def));
11100 Def := Copy_Access_Def;
11101 end if;
11102
11103 Decl_Node :=
11104 Make_Object_Declaration (Sloc (Formal),
11105 Defining_Identifier => New_Copy (Gen_Obj),
11106 Constant_Present => True,
11107 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
11108 Object_Definition => Def,
11109 Expression => New_Copy_Tree
11110 (Default_Expression (Formal)));
11111
11112 Append (Decl_Node, List);
11113 Set_Analyzed (Expression (Decl_Node), False);
11114
11115 else
11116 Error_Msg_NE ("missing actual&", Instantiation_Node, Gen_Obj);
11117 Error_Msg_NE ("\in instantiation of & declared#",
11118 Instantiation_Node, Scope (A_Gen_Obj));
11119
11120 if Is_Scalar_Type (Etype (A_Gen_Obj)) then
11121
11122 -- Create dummy constant declaration so that instance can be
11123 -- analyzed, to minimize cascaded visibility errors.
11124
11125 if Present (Subt_Mark) then
11126 Def := Subt_Mark;
11127 else pragma Assert (Present (Acc_Def));
11128 Def := Acc_Def;
11129 end if;
11130
11131 Decl_Node :=
11132 Make_Object_Declaration (Loc,
11133 Defining_Identifier => New_Copy (Gen_Obj),
11134 Constant_Present => True,
11135 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
11136 Object_Definition => New_Copy (Def),
11137 Expression =>
11138 Make_Attribute_Reference (Sloc (Gen_Obj),
11139 Attribute_Name => Name_First,
11140 Prefix => New_Copy (Def)));
11141
11142 Append (Decl_Node, List);
11143
11144 else
11145 Abandon_Instantiation (Instantiation_Node);
11146 end if;
11147 end if;
11148 end if;
11149
11150 if Nkind (Actual) in N_Has_Entity then
11151 Actual_Decl := Parent (Entity (Actual));
11152 end if;
11153
11154 -- Ada 2005 (AI-423): For a formal object declaration with a null
11155 -- exclusion or an access definition that has a null exclusion: If the
11156 -- actual matching the formal object declaration denotes a generic
11157 -- formal object of another generic unit G, and the instantiation
11158 -- containing the actual occurs within the body of G or within the body
11159 -- of a generic unit declared within the declarative region of G, then
11160 -- the declaration of the formal object of G must have a null exclusion.
11161 -- Otherwise, the subtype of the actual matching the formal object
11162 -- declaration shall exclude null.
11163
11164 if Ada_Version >= Ada_2005
11165 and then Present (Actual_Decl)
11166 and then Nkind_In (Actual_Decl, N_Formal_Object_Declaration,
11167 N_Object_Declaration)
11168 and then Nkind (Analyzed_Formal) = N_Formal_Object_Declaration
11169 and then not Has_Null_Exclusion (Actual_Decl)
11170 and then Has_Null_Exclusion (Analyzed_Formal)
11171 then
11172 Error_Msg_Sloc := Sloc (Analyzed_Formal);
11173 Error_Msg_N
11174 ("actual must exclude null to match generic formal#", Actual);
11175 end if;
11176
11177 -- An effectively volatile object cannot be used as an actual in a
11178 -- generic instantiation (SPARK RM 7.1.3(7)). The following check is
11179 -- relevant only when SPARK_Mode is on as it is not a standard Ada
11180 -- legality rule, and also verifies that the actual is an object.
11181
11182 if SPARK_Mode = On
11183 and then Present (Actual)
11184 and then Is_Object_Reference (Actual)
11185 and then Is_Effectively_Volatile_Object (Actual)
11186 then
11187 Error_Msg_N
11188 ("volatile object cannot act as actual in generic instantiation",
11189 Actual);
11190 end if;
11191
11192 return List;
11193 end Instantiate_Object;
11194
11195 ------------------------------
11196 -- Instantiate_Package_Body --
11197 ------------------------------
11198
11199 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
11200 -- must be replaced by gotos which jump to the end of the routine in order
11201 -- to restore the Ghost and SPARK modes.
11202
11203 procedure Instantiate_Package_Body
11204 (Body_Info : Pending_Body_Info;
11205 Inlined_Body : Boolean := False;
11206 Body_Optional : Boolean := False)
11207 is
11208 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
11209 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Decl);
11210 Act_Spec : constant Node_Id := Specification (Act_Decl);
11211 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
11212 Gen_Id : constant Node_Id := Name (Inst_Node);
11213 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
11214 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
11215 Loc : constant Source_Ptr := Sloc (Inst_Node);
11216
11217 Saved_ISMP : constant Boolean :=
11218 Ignore_SPARK_Mode_Pragmas_In_Instance;
11219 Saved_Style_Check : constant Boolean := Style_Check;
11220
11221 procedure Check_Initialized_Types;
11222 -- In a generic package body, an entity of a generic private type may
11223 -- appear uninitialized. This is suspicious, unless the actual is a
11224 -- fully initialized type.
11225
11226 -----------------------------
11227 -- Check_Initialized_Types --
11228 -----------------------------
11229
11230 procedure Check_Initialized_Types is
11231 Decl : Node_Id;
11232 Formal : Entity_Id;
11233 Actual : Entity_Id;
11234 Uninit_Var : Entity_Id;
11235
11236 begin
11237 Decl := First (Generic_Formal_Declarations (Gen_Decl));
11238 while Present (Decl) loop
11239 Uninit_Var := Empty;
11240
11241 if Nkind (Decl) = N_Private_Extension_Declaration then
11242 Uninit_Var := Uninitialized_Variable (Decl);
11243
11244 elsif Nkind (Decl) = N_Formal_Type_Declaration
11245 and then Nkind (Formal_Type_Definition (Decl)) =
11246 N_Formal_Private_Type_Definition
11247 then
11248 Uninit_Var :=
11249 Uninitialized_Variable (Formal_Type_Definition (Decl));
11250 end if;
11251
11252 if Present (Uninit_Var) then
11253 Formal := Defining_Identifier (Decl);
11254 Actual := First_Entity (Act_Decl_Id);
11255
11256 -- For each formal there is a subtype declaration that renames
11257 -- the actual and has the same name as the formal. Locate the
11258 -- formal for warning message about uninitialized variables
11259 -- in the generic, for which the actual type should be a fully
11260 -- initialized type.
11261
11262 while Present (Actual) loop
11263 exit when Ekind (Actual) = E_Package
11264 and then Present (Renamed_Object (Actual));
11265
11266 if Chars (Actual) = Chars (Formal)
11267 and then not Is_Scalar_Type (Actual)
11268 and then not Is_Fully_Initialized_Type (Actual)
11269 and then Warn_On_No_Value_Assigned
11270 then
11271 Error_Msg_Node_2 := Formal;
11272 Error_Msg_NE
11273 ("generic unit has uninitialized variable& of "
11274 & "formal private type &?v?", Actual, Uninit_Var);
11275 Error_Msg_NE
11276 ("actual type for& should be fully initialized type?v?",
11277 Actual, Formal);
11278 exit;
11279 end if;
11280
11281 Next_Entity (Actual);
11282 end loop;
11283 end if;
11284
11285 Next (Decl);
11286 end loop;
11287 end Check_Initialized_Types;
11288
11289 -- Local variables
11290
11291 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
11292 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
11293 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
11294 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
11295 -- Save the Ghost and SPARK mode-related data to restore on exit
11296
11297 Act_Body : Node_Id;
11298 Act_Body_Id : Entity_Id;
11299 Act_Body_Name : Node_Id;
11300 Gen_Body : Node_Id;
11301 Gen_Body_Id : Node_Id;
11302 Par_Ent : Entity_Id := Empty;
11303 Par_Vis : Boolean := False;
11304 Parent_Installed : Boolean := False;
11305
11306 Vis_Prims_List : Elist_Id := No_Elist;
11307 -- List of primitives made temporarily visible in the instantiation
11308 -- to match the visibility of the formal type.
11309
11310 -- Start of processing for Instantiate_Package_Body
11311
11312 begin
11313 Gen_Body_Id := Corresponding_Body (Gen_Decl);
11314
11315 -- The instance body may already have been processed, as the parent of
11316 -- another instance that is inlined (Load_Parent_Of_Generic).
11317
11318 if Present (Corresponding_Body (Instance_Spec (Inst_Node))) then
11319 return;
11320 end if;
11321
11322 -- The package being instantiated may be subject to pragma Ghost. Set
11323 -- the mode now to ensure that any nodes generated during instantiation
11324 -- are properly marked as Ghost.
11325
11326 Set_Ghost_Mode (Act_Decl_Id);
11327
11328 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
11329
11330 -- Re-establish the state of information on which checks are suppressed.
11331 -- This information was set in Body_Info at the point of instantiation,
11332 -- and now we restore it so that the instance is compiled using the
11333 -- check status at the instantiation (RM 11.5(7.2/2), AI95-00224-01).
11334
11335 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
11336 Scope_Suppress := Body_Info.Scope_Suppress;
11337 Opt.Ada_Version := Body_Info.Version;
11338 Opt.Ada_Version_Pragma := Body_Info.Version_Pragma;
11339 Restore_Warnings (Body_Info.Warnings);
11340
11341 -- Install the SPARK mode which applies to the package body
11342
11343 Install_SPARK_Mode (Body_Info.SPARK_Mode, Body_Info.SPARK_Mode_Pragma);
11344
11345 if No (Gen_Body_Id) then
11346
11347 -- Do not look for parent of generic body if none is required.
11348 -- This may happen when the routine is called as part of the
11349 -- Pending_Instantiations processing, when nested instances
11350 -- may precede the one generated from the main unit.
11351
11352 if not Unit_Requires_Body (Defining_Entity (Gen_Decl))
11353 and then Body_Optional
11354 then
11355 goto Leave;
11356 else
11357 Load_Parent_Of_Generic
11358 (Inst_Node, Specification (Gen_Decl), Body_Optional);
11359 Gen_Body_Id := Corresponding_Body (Gen_Decl);
11360 end if;
11361 end if;
11362
11363 -- Establish global variable for sloc adjustment and for error recovery
11364 -- In the case of an instance body for an instantiation with actuals
11365 -- from a limited view, the instance body is placed at the beginning
11366 -- of the enclosing package body: use the body entity as the source
11367 -- location for nodes of the instance body.
11368
11369 if not Is_Empty_Elmt_List (Incomplete_Actuals (Act_Decl_Id)) then
11370 declare
11371 Scop : constant Entity_Id := Scope (Act_Decl_Id);
11372 Body_Id : constant Node_Id :=
11373 Corresponding_Body (Unit_Declaration_Node (Scop));
11374
11375 begin
11376 Instantiation_Node := Body_Id;
11377 end;
11378 else
11379 Instantiation_Node := Inst_Node;
11380 end if;
11381
11382 if Present (Gen_Body_Id) then
11383 Save_Env (Gen_Unit, Act_Decl_Id);
11384 Style_Check := False;
11385
11386 -- If the context of the instance is subject to SPARK_Mode "off", the
11387 -- annotation is missing, or the body is instantiated at a later pass
11388 -- and its spec ignored SPARK_Mode pragma, set the global flag which
11389 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within the
11390 -- instance.
11391
11392 if SPARK_Mode /= On
11393 or else Ignore_SPARK_Mode_Pragmas (Act_Decl_Id)
11394 then
11395 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
11396 end if;
11397
11398 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
11399 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
11400
11401 Create_Instantiation_Source
11402 (Inst_Node, Gen_Body_Id, S_Adjustment);
11403
11404 Act_Body :=
11405 Copy_Generic_Node
11406 (Original_Node (Gen_Body), Empty, Instantiating => True);
11407
11408 -- Create proper (possibly qualified) defining name for the body, to
11409 -- correspond to the one in the spec.
11410
11411 Act_Body_Id :=
11412 Make_Defining_Identifier (Sloc (Act_Decl_Id), Chars (Act_Decl_Id));
11413 Set_Comes_From_Source (Act_Body_Id, Comes_From_Source (Act_Decl_Id));
11414
11415 -- Some attributes of spec entity are not inherited by body entity
11416
11417 Set_Handler_Records (Act_Body_Id, No_List);
11418
11419 if Nkind (Defining_Unit_Name (Act_Spec)) =
11420 N_Defining_Program_Unit_Name
11421 then
11422 Act_Body_Name :=
11423 Make_Defining_Program_Unit_Name (Loc,
11424 Name =>
11425 New_Copy_Tree (Name (Defining_Unit_Name (Act_Spec))),
11426 Defining_Identifier => Act_Body_Id);
11427 else
11428 Act_Body_Name := Act_Body_Id;
11429 end if;
11430
11431 Set_Defining_Unit_Name (Act_Body, Act_Body_Name);
11432
11433 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
11434 Check_Generic_Actuals (Act_Decl_Id, False);
11435 Check_Initialized_Types;
11436
11437 -- Install primitives hidden at the point of the instantiation but
11438 -- visible when processing the generic formals
11439
11440 declare
11441 E : Entity_Id;
11442
11443 begin
11444 E := First_Entity (Act_Decl_Id);
11445 while Present (E) loop
11446 if Is_Type (E)
11447 and then not Is_Itype (E)
11448 and then Is_Generic_Actual_Type (E)
11449 and then Is_Tagged_Type (E)
11450 then
11451 Install_Hidden_Primitives
11452 (Prims_List => Vis_Prims_List,
11453 Gen_T => Generic_Parent_Type (Parent (E)),
11454 Act_T => E);
11455 end if;
11456
11457 Next_Entity (E);
11458 end loop;
11459 end;
11460
11461 -- If it is a child unit, make the parent instance (which is an
11462 -- instance of the parent of the generic) visible. The parent
11463 -- instance is the prefix of the name of the generic unit.
11464
11465 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
11466 and then Nkind (Gen_Id) = N_Expanded_Name
11467 then
11468 Par_Ent := Entity (Prefix (Gen_Id));
11469 Par_Vis := Is_Immediately_Visible (Par_Ent);
11470 Install_Parent (Par_Ent, In_Body => True);
11471 Parent_Installed := True;
11472
11473 elsif Is_Child_Unit (Gen_Unit) then
11474 Par_Ent := Scope (Gen_Unit);
11475 Par_Vis := Is_Immediately_Visible (Par_Ent);
11476 Install_Parent (Par_Ent, In_Body => True);
11477 Parent_Installed := True;
11478 end if;
11479
11480 -- If the instantiation is a library unit, and this is the main unit,
11481 -- then build the resulting compilation unit nodes for the instance.
11482 -- If this is a compilation unit but it is not the main unit, then it
11483 -- is the body of a unit in the context, that is being compiled
11484 -- because it is encloses some inlined unit or another generic unit
11485 -- being instantiated. In that case, this body is not part of the
11486 -- current compilation, and is not attached to the tree, but its
11487 -- parent must be set for analysis.
11488
11489 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
11490
11491 -- Replace instance node with body of instance, and create new
11492 -- node for corresponding instance declaration.
11493
11494 Build_Instance_Compilation_Unit_Nodes
11495 (Inst_Node, Act_Body, Act_Decl);
11496 Analyze (Inst_Node);
11497
11498 if Parent (Inst_Node) = Cunit (Main_Unit) then
11499
11500 -- If the instance is a child unit itself, then set the scope
11501 -- of the expanded body to be the parent of the instantiation
11502 -- (ensuring that the fully qualified name will be generated
11503 -- for the elaboration subprogram).
11504
11505 if Nkind (Defining_Unit_Name (Act_Spec)) =
11506 N_Defining_Program_Unit_Name
11507 then
11508 Set_Scope (Defining_Entity (Inst_Node), Scope (Act_Decl_Id));
11509 end if;
11510 end if;
11511
11512 -- Case where instantiation is not a library unit
11513
11514 else
11515 -- If this is an early instantiation, i.e. appears textually
11516 -- before the corresponding body and must be elaborated first,
11517 -- indicate that the body instance is to be delayed.
11518
11519 Install_Body (Act_Body, Inst_Node, Gen_Body, Gen_Decl);
11520
11521 -- Now analyze the body. We turn off all checks if this is an
11522 -- internal unit, since there is no reason to have checks on for
11523 -- any predefined run-time library code. All such code is designed
11524 -- to be compiled with checks off.
11525
11526 -- Note that we do NOT apply this criterion to children of GNAT
11527 -- The latter units must suppress checks explicitly if needed.
11528
11529 -- We also do not suppress checks in CodePeer mode where we are
11530 -- interested in finding possible runtime errors.
11531
11532 if not CodePeer_Mode
11533 and then In_Predefined_Unit (Gen_Decl)
11534 then
11535 Analyze (Act_Body, Suppress => All_Checks);
11536 else
11537 Analyze (Act_Body);
11538 end if;
11539 end if;
11540
11541 Inherit_Context (Gen_Body, Inst_Node);
11542
11543 -- Remove the parent instances if they have been placed on the scope
11544 -- stack to compile the body.
11545
11546 if Parent_Installed then
11547 Remove_Parent (In_Body => True);
11548
11549 -- Restore the previous visibility of the parent
11550
11551 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
11552 end if;
11553
11554 Restore_Hidden_Primitives (Vis_Prims_List);
11555 Restore_Private_Views (Act_Decl_Id);
11556
11557 -- Remove the current unit from visibility if this is an instance
11558 -- that is not elaborated on the fly for inlining purposes.
11559
11560 if not Inlined_Body then
11561 Set_Is_Immediately_Visible (Act_Decl_Id, False);
11562 end if;
11563
11564 Restore_Env;
11565
11566 -- If we have no body, and the unit requires a body, then complain. This
11567 -- complaint is suppressed if we have detected other errors (since a
11568 -- common reason for missing the body is that it had errors).
11569 -- In CodePeer mode, a warning has been emitted already, no need for
11570 -- further messages.
11571
11572 elsif Unit_Requires_Body (Gen_Unit)
11573 and then not Body_Optional
11574 then
11575 if CodePeer_Mode then
11576 null;
11577
11578 elsif Serious_Errors_Detected = 0 then
11579 Error_Msg_NE
11580 ("cannot find body of generic package &", Inst_Node, Gen_Unit);
11581
11582 -- Don't attempt to perform any cleanup actions if some other error
11583 -- was already detected, since this can cause blowups.
11584
11585 else
11586 goto Leave;
11587 end if;
11588
11589 -- Case of package that does not need a body
11590
11591 else
11592 -- If the instantiation of the declaration is a library unit, rewrite
11593 -- the original package instantiation as a package declaration in the
11594 -- compilation unit node.
11595
11596 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
11597 Set_Parent_Spec (Act_Decl, Parent_Spec (Inst_Node));
11598 Rewrite (Inst_Node, Act_Decl);
11599
11600 -- Generate elaboration entity, in case spec has elaboration code.
11601 -- This cannot be done when the instance is analyzed, because it
11602 -- is not known yet whether the body exists.
11603
11604 Set_Elaboration_Entity_Required (Act_Decl_Id, False);
11605 Build_Elaboration_Entity (Parent (Inst_Node), Act_Decl_Id);
11606
11607 -- If the instantiation is not a library unit, then append the
11608 -- declaration to the list of implicitly generated entities, unless
11609 -- it is already a list member which means that it was already
11610 -- processed
11611
11612 elsif not Is_List_Member (Act_Decl) then
11613 Mark_Rewrite_Insertion (Act_Decl);
11614 Insert_Before (Inst_Node, Act_Decl);
11615 end if;
11616 end if;
11617
11618 Expander_Mode_Restore;
11619
11620 <<Leave>>
11621 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
11622 Restore_Ghost_Region (Saved_GM, Saved_IGR);
11623 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
11624 Style_Check := Saved_Style_Check;
11625 end Instantiate_Package_Body;
11626
11627 ---------------------------------
11628 -- Instantiate_Subprogram_Body --
11629 ---------------------------------
11630
11631 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
11632 -- must be replaced by gotos which jump to the end of the routine in order
11633 -- to restore the Ghost and SPARK modes.
11634
11635 procedure Instantiate_Subprogram_Body
11636 (Body_Info : Pending_Body_Info;
11637 Body_Optional : Boolean := False)
11638 is
11639 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
11640 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Decl);
11641 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
11642 Gen_Id : constant Node_Id := Name (Inst_Node);
11643 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
11644 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
11645 Loc : constant Source_Ptr := Sloc (Inst_Node);
11646 Pack_Id : constant Entity_Id :=
11647 Defining_Unit_Name (Parent (Act_Decl));
11648
11649 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
11650 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
11651 Saved_ISMP : constant Boolean :=
11652 Ignore_SPARK_Mode_Pragmas_In_Instance;
11653 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
11654 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
11655 -- Save the Ghost and SPARK mode-related data to restore on exit
11656
11657 Saved_Style_Check : constant Boolean := Style_Check;
11658 Saved_Warnings : constant Warning_Record := Save_Warnings;
11659
11660 Act_Body : Node_Id;
11661 Act_Body_Id : Entity_Id;
11662 Gen_Body : Node_Id;
11663 Gen_Body_Id : Node_Id;
11664 Pack_Body : Node_Id;
11665 Par_Ent : Entity_Id := Empty;
11666 Par_Vis : Boolean := False;
11667 Ret_Expr : Node_Id;
11668
11669 Parent_Installed : Boolean := False;
11670
11671 begin
11672 Gen_Body_Id := Corresponding_Body (Gen_Decl);
11673
11674 -- Subprogram body may have been created already because of an inline
11675 -- pragma, or because of multiple elaborations of the enclosing package
11676 -- when several instances of the subprogram appear in the main unit.
11677
11678 if Present (Corresponding_Body (Act_Decl)) then
11679 return;
11680 end if;
11681
11682 -- The subprogram being instantiated may be subject to pragma Ghost. Set
11683 -- the mode now to ensure that any nodes generated during instantiation
11684 -- are properly marked as Ghost.
11685
11686 Set_Ghost_Mode (Act_Decl_Id);
11687
11688 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
11689
11690 -- Re-establish the state of information on which checks are suppressed.
11691 -- This information was set in Body_Info at the point of instantiation,
11692 -- and now we restore it so that the instance is compiled using the
11693 -- check status at the instantiation (RM 11.5(7.2/2), AI95-00224-01).
11694
11695 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
11696 Scope_Suppress := Body_Info.Scope_Suppress;
11697 Opt.Ada_Version := Body_Info.Version;
11698 Opt.Ada_Version_Pragma := Body_Info.Version_Pragma;
11699 Restore_Warnings (Body_Info.Warnings);
11700
11701 -- Install the SPARK mode which applies to the subprogram body from the
11702 -- instantiation context. This may be refined further if an explicit
11703 -- SPARK_Mode pragma applies to the generic body.
11704
11705 Install_SPARK_Mode (Body_Info.SPARK_Mode, Body_Info.SPARK_Mode_Pragma);
11706
11707 if No (Gen_Body_Id) then
11708
11709 -- For imported generic subprogram, no body to compile, complete
11710 -- the spec entity appropriately.
11711
11712 if Is_Imported (Gen_Unit) then
11713 Set_Is_Imported (Act_Decl_Id);
11714 Set_First_Rep_Item (Act_Decl_Id, First_Rep_Item (Gen_Unit));
11715 Set_Interface_Name (Act_Decl_Id, Interface_Name (Gen_Unit));
11716 Set_Convention (Act_Decl_Id, Convention (Gen_Unit));
11717 Set_Has_Completion (Act_Decl_Id);
11718 goto Leave;
11719
11720 -- For other cases, compile the body
11721
11722 else
11723 Load_Parent_Of_Generic
11724 (Inst_Node, Specification (Gen_Decl), Body_Optional);
11725 Gen_Body_Id := Corresponding_Body (Gen_Decl);
11726 end if;
11727 end if;
11728
11729 Instantiation_Node := Inst_Node;
11730
11731 if Present (Gen_Body_Id) then
11732 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
11733
11734 if Nkind (Gen_Body) = N_Subprogram_Body_Stub then
11735
11736 -- Either body is not present, or context is non-expanding, as
11737 -- when compiling a subunit. Mark the instance as completed, and
11738 -- diagnose a missing body when needed.
11739
11740 if Expander_Active
11741 and then Operating_Mode = Generate_Code
11742 then
11743 Error_Msg_N ("missing proper body for instantiation", Gen_Body);
11744 end if;
11745
11746 Set_Has_Completion (Act_Decl_Id);
11747 goto Leave;
11748 end if;
11749
11750 Save_Env (Gen_Unit, Act_Decl_Id);
11751 Style_Check := False;
11752
11753 -- If the context of the instance is subject to SPARK_Mode "off", the
11754 -- annotation is missing, or the body is instantiated at a later pass
11755 -- and its spec ignored SPARK_Mode pragma, set the global flag which
11756 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within the
11757 -- instance.
11758
11759 if SPARK_Mode /= On
11760 or else Ignore_SPARK_Mode_Pragmas (Act_Decl_Id)
11761 then
11762 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
11763 end if;
11764
11765 -- If the context of an instance is not subject to SPARK_Mode "off",
11766 -- and the generic body is subject to an explicit SPARK_Mode pragma,
11767 -- the latter should be the one applicable to the instance.
11768
11769 if not Ignore_SPARK_Mode_Pragmas_In_Instance
11770 and then SPARK_Mode /= Off
11771 and then Present (SPARK_Pragma (Gen_Body_Id))
11772 then
11773 Set_SPARK_Mode (Gen_Body_Id);
11774 end if;
11775
11776 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
11777 Create_Instantiation_Source
11778 (Inst_Node,
11779 Gen_Body_Id,
11780 S_Adjustment);
11781
11782 Act_Body :=
11783 Copy_Generic_Node
11784 (Original_Node (Gen_Body), Empty, Instantiating => True);
11785
11786 -- Create proper defining name for the body, to correspond to the one
11787 -- in the spec.
11788
11789 Act_Body_Id :=
11790 Make_Defining_Identifier (Sloc (Act_Decl_Id), Chars (Act_Decl_Id));
11791
11792 Set_Comes_From_Source (Act_Body_Id, Comes_From_Source (Act_Decl_Id));
11793 Set_Defining_Unit_Name (Specification (Act_Body), Act_Body_Id);
11794
11795 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
11796 Set_Has_Completion (Act_Decl_Id);
11797 Check_Generic_Actuals (Pack_Id, False);
11798
11799 -- Generate a reference to link the visible subprogram instance to
11800 -- the generic body, which for navigation purposes is the only
11801 -- available source for the instance.
11802
11803 Generate_Reference
11804 (Related_Instance (Pack_Id),
11805 Gen_Body_Id, 'b', Set_Ref => False, Force => True);
11806
11807 -- If it is a child unit, make the parent instance (which is an
11808 -- instance of the parent of the generic) visible. The parent
11809 -- instance is the prefix of the name of the generic unit.
11810
11811 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
11812 and then Nkind (Gen_Id) = N_Expanded_Name
11813 then
11814 Par_Ent := Entity (Prefix (Gen_Id));
11815 Par_Vis := Is_Immediately_Visible (Par_Ent);
11816 Install_Parent (Par_Ent, In_Body => True);
11817 Parent_Installed := True;
11818
11819 elsif Is_Child_Unit (Gen_Unit) then
11820 Par_Ent := Scope (Gen_Unit);
11821 Par_Vis := Is_Immediately_Visible (Par_Ent);
11822 Install_Parent (Par_Ent, In_Body => True);
11823 Parent_Installed := True;
11824 end if;
11825
11826 -- Subprogram body is placed in the body of wrapper package,
11827 -- whose spec contains the subprogram declaration as well as
11828 -- the renaming declarations for the generic parameters.
11829
11830 Pack_Body :=
11831 Make_Package_Body (Loc,
11832 Defining_Unit_Name => New_Copy (Pack_Id),
11833 Declarations => New_List (Act_Body));
11834
11835 Set_Corresponding_Spec (Pack_Body, Pack_Id);
11836
11837 -- If the instantiation is a library unit, then build resulting
11838 -- compilation unit nodes for the instance. The declaration of
11839 -- the enclosing package is the grandparent of the subprogram
11840 -- declaration. First replace the instantiation node as the unit
11841 -- of the corresponding compilation.
11842
11843 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
11844 if Parent (Inst_Node) = Cunit (Main_Unit) then
11845 Set_Unit (Parent (Inst_Node), Inst_Node);
11846 Build_Instance_Compilation_Unit_Nodes
11847 (Inst_Node, Pack_Body, Parent (Parent (Act_Decl)));
11848 Analyze (Inst_Node);
11849 else
11850 Set_Parent (Pack_Body, Parent (Inst_Node));
11851 Analyze (Pack_Body);
11852 end if;
11853
11854 else
11855 Insert_Before (Inst_Node, Pack_Body);
11856 Mark_Rewrite_Insertion (Pack_Body);
11857 Analyze (Pack_Body);
11858
11859 if Expander_Active then
11860 Freeze_Subprogram_Body (Inst_Node, Gen_Body, Pack_Id);
11861 end if;
11862 end if;
11863
11864 Inherit_Context (Gen_Body, Inst_Node);
11865
11866 Restore_Private_Views (Pack_Id, False);
11867
11868 if Parent_Installed then
11869 Remove_Parent (In_Body => True);
11870
11871 -- Restore the previous visibility of the parent
11872
11873 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
11874 end if;
11875
11876 Restore_Env;
11877 Restore_Warnings (Saved_Warnings);
11878
11879 -- Body not found. Error was emitted already. If there were no previous
11880 -- errors, this may be an instance whose scope is a premature instance.
11881 -- In that case we must insure that the (legal) program does raise
11882 -- program error if executed. We generate a subprogram body for this
11883 -- purpose. See DEC ac30vso.
11884
11885 -- Should not reference proprietary DEC tests in comments ???
11886
11887 elsif Serious_Errors_Detected = 0
11888 and then Nkind (Parent (Inst_Node)) /= N_Compilation_Unit
11889 then
11890 if Body_Optional then
11891 goto Leave;
11892
11893 elsif Ekind (Act_Decl_Id) = E_Procedure then
11894 Act_Body :=
11895 Make_Subprogram_Body (Loc,
11896 Specification =>
11897 Make_Procedure_Specification (Loc,
11898 Defining_Unit_Name =>
11899 Make_Defining_Identifier (Loc, Chars (Act_Decl_Id)),
11900 Parameter_Specifications =>
11901 New_Copy_List
11902 (Parameter_Specifications (Parent (Act_Decl_Id)))),
11903
11904 Declarations => Empty_List,
11905 Handled_Statement_Sequence =>
11906 Make_Handled_Sequence_Of_Statements (Loc,
11907 Statements => New_List (
11908 Make_Raise_Program_Error (Loc,
11909 Reason => PE_Access_Before_Elaboration))));
11910
11911 else
11912 Ret_Expr :=
11913 Make_Raise_Program_Error (Loc,
11914 Reason => PE_Access_Before_Elaboration);
11915
11916 Set_Etype (Ret_Expr, (Etype (Act_Decl_Id)));
11917 Set_Analyzed (Ret_Expr);
11918
11919 Act_Body :=
11920 Make_Subprogram_Body (Loc,
11921 Specification =>
11922 Make_Function_Specification (Loc,
11923 Defining_Unit_Name =>
11924 Make_Defining_Identifier (Loc, Chars (Act_Decl_Id)),
11925 Parameter_Specifications =>
11926 New_Copy_List
11927 (Parameter_Specifications (Parent (Act_Decl_Id))),
11928 Result_Definition =>
11929 New_Occurrence_Of (Etype (Act_Decl_Id), Loc)),
11930
11931 Declarations => Empty_List,
11932 Handled_Statement_Sequence =>
11933 Make_Handled_Sequence_Of_Statements (Loc,
11934 Statements => New_List (
11935 Make_Simple_Return_Statement (Loc, Ret_Expr))));
11936 end if;
11937
11938 Pack_Body :=
11939 Make_Package_Body (Loc,
11940 Defining_Unit_Name => New_Copy (Pack_Id),
11941 Declarations => New_List (Act_Body));
11942
11943 Insert_After (Inst_Node, Pack_Body);
11944 Set_Corresponding_Spec (Pack_Body, Pack_Id);
11945 Analyze (Pack_Body);
11946 end if;
11947
11948 Expander_Mode_Restore;
11949
11950 <<Leave>>
11951 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
11952 Restore_Ghost_Region (Saved_GM, Saved_IGR);
11953 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
11954 Style_Check := Saved_Style_Check;
11955 end Instantiate_Subprogram_Body;
11956
11957 ----------------------
11958 -- Instantiate_Type --
11959 ----------------------
11960
11961 function Instantiate_Type
11962 (Formal : Node_Id;
11963 Actual : Node_Id;
11964 Analyzed_Formal : Node_Id;
11965 Actual_Decls : List_Id) return List_Id
11966 is
11967 A_Gen_T : constant Entity_Id :=
11968 Defining_Identifier (Analyzed_Formal);
11969 Def : constant Node_Id := Formal_Type_Definition (Formal);
11970 Gen_T : constant Entity_Id := Defining_Identifier (Formal);
11971 Act_T : Entity_Id;
11972 Ancestor : Entity_Id := Empty;
11973 Decl_Node : Node_Id;
11974 Decl_Nodes : List_Id;
11975 Loc : Source_Ptr;
11976 Subt : Entity_Id;
11977
11978 procedure Diagnose_Predicated_Actual;
11979 -- There are a number of constructs in which a discrete type with
11980 -- predicates is illegal, e.g. as an index in an array type declaration.
11981 -- If a generic type is used is such a construct in a generic package
11982 -- declaration, it carries the flag No_Predicate_On_Actual. it is part
11983 -- of the generic contract that the actual cannot have predicates.
11984
11985 procedure Validate_Array_Type_Instance;
11986 procedure Validate_Access_Subprogram_Instance;
11987 procedure Validate_Access_Type_Instance;
11988 procedure Validate_Derived_Type_Instance;
11989 procedure Validate_Derived_Interface_Type_Instance;
11990 procedure Validate_Discriminated_Formal_Type;
11991 procedure Validate_Interface_Type_Instance;
11992 procedure Validate_Private_Type_Instance;
11993 procedure Validate_Incomplete_Type_Instance;
11994 -- These procedures perform validation tests for the named case.
11995 -- Validate_Discriminated_Formal_Type is shared by formal private
11996 -- types and Ada 2012 formal incomplete types.
11997
11998 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean;
11999 -- Check that base types are the same and that the subtypes match
12000 -- statically. Used in several of the above.
12001
12002 ---------------------------------
12003 -- Diagnose_Predicated_Actual --
12004 ---------------------------------
12005
12006 procedure Diagnose_Predicated_Actual is
12007 begin
12008 if No_Predicate_On_Actual (A_Gen_T)
12009 and then Has_Predicates (Act_T)
12010 then
12011 Error_Msg_NE
12012 ("actual for& cannot be a type with predicate",
12013 Instantiation_Node, A_Gen_T);
12014
12015 elsif No_Dynamic_Predicate_On_Actual (A_Gen_T)
12016 and then Has_Predicates (Act_T)
12017 and then not Has_Static_Predicate_Aspect (Act_T)
12018 then
12019 Error_Msg_NE
12020 ("actual for& cannot be a type with a dynamic predicate",
12021 Instantiation_Node, A_Gen_T);
12022 end if;
12023 end Diagnose_Predicated_Actual;
12024
12025 --------------------
12026 -- Subtypes_Match --
12027 --------------------
12028
12029 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean is
12030 T : constant Entity_Id := Get_Instance_Of (Gen_T);
12031
12032 begin
12033 -- Some detailed comments would be useful here ???
12034
12035 return ((Base_Type (T) = Act_T
12036 or else Base_Type (T) = Base_Type (Act_T))
12037 and then Subtypes_Statically_Match (T, Act_T))
12038
12039 or else (Is_Class_Wide_Type (Gen_T)
12040 and then Is_Class_Wide_Type (Act_T)
12041 and then Subtypes_Match
12042 (Get_Instance_Of (Root_Type (Gen_T)),
12043 Root_Type (Act_T)))
12044
12045 or else
12046 (Ekind_In (Gen_T, E_Anonymous_Access_Subprogram_Type,
12047 E_Anonymous_Access_Type)
12048 and then Ekind (Act_T) = Ekind (Gen_T)
12049 and then Subtypes_Statically_Match
12050 (Designated_Type (Gen_T), Designated_Type (Act_T)));
12051 end Subtypes_Match;
12052
12053 -----------------------------------------
12054 -- Validate_Access_Subprogram_Instance --
12055 -----------------------------------------
12056
12057 procedure Validate_Access_Subprogram_Instance is
12058 begin
12059 if not Is_Access_Type (Act_T)
12060 or else Ekind (Designated_Type (Act_T)) /= E_Subprogram_Type
12061 then
12062 Error_Msg_NE
12063 ("expect access type in instantiation of &", Actual, Gen_T);
12064 Abandon_Instantiation (Actual);
12065 end if;
12066
12067 -- According to AI05-288, actuals for access_to_subprograms must be
12068 -- subtype conformant with the generic formal. Previous to AI05-288
12069 -- only mode conformance was required.
12070
12071 -- This is a binding interpretation that applies to previous versions
12072 -- of the language, no need to maintain previous weaker checks.
12073
12074 Check_Subtype_Conformant
12075 (Designated_Type (Act_T),
12076 Designated_Type (A_Gen_T),
12077 Actual,
12078 Get_Inst => True);
12079
12080 if Ekind (Base_Type (Act_T)) = E_Access_Protected_Subprogram_Type then
12081 if Ekind (A_Gen_T) = E_Access_Subprogram_Type then
12082 Error_Msg_NE
12083 ("protected access type not allowed for formal &",
12084 Actual, Gen_T);
12085 end if;
12086
12087 elsif Ekind (A_Gen_T) = E_Access_Protected_Subprogram_Type then
12088 Error_Msg_NE
12089 ("expect protected access type for formal &",
12090 Actual, Gen_T);
12091 end if;
12092
12093 -- If the formal has a specified convention (which in most cases
12094 -- will be StdCall) verify that the actual has the same convention.
12095
12096 if Has_Convention_Pragma (A_Gen_T)
12097 and then Convention (A_Gen_T) /= Convention (Act_T)
12098 then
12099 Error_Msg_Name_1 := Get_Convention_Name (Convention (A_Gen_T));
12100 Error_Msg_NE
12101 ("actual for formal & must have convention %", Actual, Gen_T);
12102 end if;
12103 end Validate_Access_Subprogram_Instance;
12104
12105 -----------------------------------
12106 -- Validate_Access_Type_Instance --
12107 -----------------------------------
12108
12109 procedure Validate_Access_Type_Instance is
12110 Desig_Type : constant Entity_Id :=
12111 Find_Actual_Type (Designated_Type (A_Gen_T), A_Gen_T);
12112 Desig_Act : Entity_Id;
12113
12114 begin
12115 if not Is_Access_Type (Act_T) then
12116 Error_Msg_NE
12117 ("expect access type in instantiation of &", Actual, Gen_T);
12118 Abandon_Instantiation (Actual);
12119 end if;
12120
12121 if Is_Access_Constant (A_Gen_T) then
12122 if not Is_Access_Constant (Act_T) then
12123 Error_Msg_N
12124 ("actual type must be access-to-constant type", Actual);
12125 Abandon_Instantiation (Actual);
12126 end if;
12127 else
12128 if Is_Access_Constant (Act_T) then
12129 Error_Msg_N
12130 ("actual type must be access-to-variable type", Actual);
12131 Abandon_Instantiation (Actual);
12132
12133 elsif Ekind (A_Gen_T) = E_General_Access_Type
12134 and then Ekind (Base_Type (Act_T)) /= E_General_Access_Type
12135 then
12136 Error_Msg_N -- CODEFIX
12137 ("actual must be general access type!", Actual);
12138 Error_Msg_NE -- CODEFIX
12139 ("add ALL to }!", Actual, Act_T);
12140 Abandon_Instantiation (Actual);
12141 end if;
12142 end if;
12143
12144 -- The designated subtypes, that is to say the subtypes introduced
12145 -- by an access type declaration (and not by a subtype declaration)
12146 -- must match.
12147
12148 Desig_Act := Designated_Type (Base_Type (Act_T));
12149
12150 -- The designated type may have been introduced through a limited_
12151 -- with clause, in which case retrieve the non-limited view. This
12152 -- applies to incomplete types as well as to class-wide types.
12153
12154 if From_Limited_With (Desig_Act) then
12155 Desig_Act := Available_View (Desig_Act);
12156 end if;
12157
12158 if not Subtypes_Match (Desig_Type, Desig_Act) then
12159 Error_Msg_NE
12160 ("designated type of actual does not match that of formal &",
12161 Actual, Gen_T);
12162
12163 if not Predicates_Match (Desig_Type, Desig_Act) then
12164 Error_Msg_N ("\predicates do not match", Actual);
12165 end if;
12166
12167 Abandon_Instantiation (Actual);
12168
12169 elsif Is_Access_Type (Designated_Type (Act_T))
12170 and then Is_Constrained (Designated_Type (Designated_Type (Act_T)))
12171 /=
12172 Is_Constrained (Designated_Type (Desig_Type))
12173 then
12174 Error_Msg_NE
12175 ("designated type of actual does not match that of formal &",
12176 Actual, Gen_T);
12177
12178 if not Predicates_Match (Desig_Type, Desig_Act) then
12179 Error_Msg_N ("\predicates do not match", Actual);
12180 end if;
12181
12182 Abandon_Instantiation (Actual);
12183 end if;
12184
12185 -- Ada 2005: null-exclusion indicators of the two types must agree
12186
12187 if Can_Never_Be_Null (A_Gen_T) /= Can_Never_Be_Null (Act_T) then
12188 Error_Msg_NE
12189 ("non null exclusion of actual and formal & do not match",
12190 Actual, Gen_T);
12191 end if;
12192 end Validate_Access_Type_Instance;
12193
12194 ----------------------------------
12195 -- Validate_Array_Type_Instance --
12196 ----------------------------------
12197
12198 procedure Validate_Array_Type_Instance is
12199 I1 : Node_Id;
12200 I2 : Node_Id;
12201 T2 : Entity_Id;
12202
12203 function Formal_Dimensions return Nat;
12204 -- Count number of dimensions in array type formal
12205
12206 -----------------------
12207 -- Formal_Dimensions --
12208 -----------------------
12209
12210 function Formal_Dimensions return Nat is
12211 Num : Nat := 0;
12212 Index : Node_Id;
12213
12214 begin
12215 if Nkind (Def) = N_Constrained_Array_Definition then
12216 Index := First (Discrete_Subtype_Definitions (Def));
12217 else
12218 Index := First (Subtype_Marks (Def));
12219 end if;
12220
12221 while Present (Index) loop
12222 Num := Num + 1;
12223 Next_Index (Index);
12224 end loop;
12225
12226 return Num;
12227 end Formal_Dimensions;
12228
12229 -- Start of processing for Validate_Array_Type_Instance
12230
12231 begin
12232 if not Is_Array_Type (Act_T) then
12233 Error_Msg_NE
12234 ("expect array type in instantiation of &", Actual, Gen_T);
12235 Abandon_Instantiation (Actual);
12236
12237 elsif Nkind (Def) = N_Constrained_Array_Definition then
12238 if not (Is_Constrained (Act_T)) then
12239 Error_Msg_NE
12240 ("expect constrained array in instantiation of &",
12241 Actual, Gen_T);
12242 Abandon_Instantiation (Actual);
12243 end if;
12244
12245 else
12246 if Is_Constrained (Act_T) then
12247 Error_Msg_NE
12248 ("expect unconstrained array in instantiation of &",
12249 Actual, Gen_T);
12250 Abandon_Instantiation (Actual);
12251 end if;
12252 end if;
12253
12254 if Formal_Dimensions /= Number_Dimensions (Act_T) then
12255 Error_Msg_NE
12256 ("dimensions of actual do not match formal &", Actual, Gen_T);
12257 Abandon_Instantiation (Actual);
12258 end if;
12259
12260 I1 := First_Index (A_Gen_T);
12261 I2 := First_Index (Act_T);
12262 for J in 1 .. Formal_Dimensions loop
12263
12264 -- If the indexes of the actual were given by a subtype_mark,
12265 -- the index was transformed into a range attribute. Retrieve
12266 -- the original type mark for checking.
12267
12268 if Is_Entity_Name (Original_Node (I2)) then
12269 T2 := Entity (Original_Node (I2));
12270 else
12271 T2 := Etype (I2);
12272 end if;
12273
12274 if not Subtypes_Match
12275 (Find_Actual_Type (Etype (I1), A_Gen_T), T2)
12276 then
12277 Error_Msg_NE
12278 ("index types of actual do not match those of formal &",
12279 Actual, Gen_T);
12280 Abandon_Instantiation (Actual);
12281 end if;
12282
12283 Next_Index (I1);
12284 Next_Index (I2);
12285 end loop;
12286
12287 -- Check matching subtypes. Note that there are complex visibility
12288 -- issues when the generic is a child unit and some aspect of the
12289 -- generic type is declared in a parent unit of the generic. We do
12290 -- the test to handle this special case only after a direct check
12291 -- for static matching has failed. The case where both the component
12292 -- type and the array type are separate formals, and the component
12293 -- type is a private view may also require special checking in
12294 -- Subtypes_Match. Finally, we assume that a child instance where
12295 -- the component type comes from a formal of a parent instance is
12296 -- correct because the generic was correct. A more precise check
12297 -- seems too complex to install???
12298
12299 if Subtypes_Match
12300 (Component_Type (A_Gen_T), Component_Type (Act_T))
12301 or else
12302 Subtypes_Match
12303 (Find_Actual_Type (Component_Type (A_Gen_T), A_Gen_T),
12304 Component_Type (Act_T))
12305 or else
12306 (not Inside_A_Generic
12307 and then Is_Child_Unit (Scope (Component_Type (A_Gen_T))))
12308 then
12309 null;
12310 else
12311 Error_Msg_NE
12312 ("component subtype of actual does not match that of formal &",
12313 Actual, Gen_T);
12314 Abandon_Instantiation (Actual);
12315 end if;
12316
12317 if Has_Aliased_Components (A_Gen_T)
12318 and then not Has_Aliased_Components (Act_T)
12319 then
12320 Error_Msg_NE
12321 ("actual must have aliased components to match formal type &",
12322 Actual, Gen_T);
12323 end if;
12324 end Validate_Array_Type_Instance;
12325
12326 -----------------------------------------------
12327 -- Validate_Derived_Interface_Type_Instance --
12328 -----------------------------------------------
12329
12330 procedure Validate_Derived_Interface_Type_Instance is
12331 Par : constant Entity_Id := Entity (Subtype_Indication (Def));
12332 Elmt : Elmt_Id;
12333
12334 begin
12335 -- First apply interface instance checks
12336
12337 Validate_Interface_Type_Instance;
12338
12339 -- Verify that immediate parent interface is an ancestor of
12340 -- the actual.
12341
12342 if Present (Par)
12343 and then not Interface_Present_In_Ancestor (Act_T, Par)
12344 then
12345 Error_Msg_NE
12346 ("interface actual must include progenitor&", Actual, Par);
12347 end if;
12348
12349 -- Now verify that the actual includes all other ancestors of
12350 -- the formal.
12351
12352 Elmt := First_Elmt (Interfaces (A_Gen_T));
12353 while Present (Elmt) loop
12354 if not Interface_Present_In_Ancestor
12355 (Act_T, Get_Instance_Of (Node (Elmt)))
12356 then
12357 Error_Msg_NE
12358 ("interface actual must include progenitor&",
12359 Actual, Node (Elmt));
12360 end if;
12361
12362 Next_Elmt (Elmt);
12363 end loop;
12364 end Validate_Derived_Interface_Type_Instance;
12365
12366 ------------------------------------
12367 -- Validate_Derived_Type_Instance --
12368 ------------------------------------
12369
12370 procedure Validate_Derived_Type_Instance is
12371 Actual_Discr : Entity_Id;
12372 Ancestor_Discr : Entity_Id;
12373
12374 begin
12375 -- Verify that the actual includes the progenitors of the formal,
12376 -- if any. The formal may depend on previous formals and their
12377 -- instance, so we must examine instance of interfaces if present.
12378 -- The actual may be an extension of an interface, in which case
12379 -- it does not appear in the interface list, so this must be
12380 -- checked separately.
12381
12382 if Present (Interface_List (Def)) then
12383 if not Has_Interfaces (Act_T) then
12384 Error_Msg_NE
12385 ("actual must implement all interfaces of formal&",
12386 Actual, A_Gen_T);
12387
12388 else
12389 declare
12390 Act_Iface_List : Elist_Id;
12391 Iface : Node_Id;
12392 Iface_Ent : Entity_Id;
12393
12394 function Instance_Exists (I : Entity_Id) return Boolean;
12395 -- If the interface entity is declared in a generic unit,
12396 -- this can only be legal if we are within an instantiation
12397 -- of a child of that generic. There is currently no
12398 -- mechanism to relate an interface declared within a
12399 -- generic to the corresponding interface in an instance,
12400 -- so we traverse the list of interfaces of the actual,
12401 -- looking for a name match.
12402
12403 ---------------------
12404 -- Instance_Exists --
12405 ---------------------
12406
12407 function Instance_Exists (I : Entity_Id) return Boolean is
12408 Iface_Elmt : Elmt_Id;
12409
12410 begin
12411 Iface_Elmt := First_Elmt (Act_Iface_List);
12412 while Present (Iface_Elmt) loop
12413 if Is_Generic_Instance (Scope (Node (Iface_Elmt)))
12414 and then Chars (Node (Iface_Elmt)) = Chars (I)
12415 then
12416 return True;
12417 end if;
12418
12419 Next_Elmt (Iface_Elmt);
12420 end loop;
12421
12422 return False;
12423 end Instance_Exists;
12424
12425 begin
12426 Iface := First (Abstract_Interface_List (A_Gen_T));
12427 Collect_Interfaces (Act_T, Act_Iface_List);
12428
12429 while Present (Iface) loop
12430 Iface_Ent := Get_Instance_Of (Entity (Iface));
12431
12432 if Is_Ancestor (Iface_Ent, Act_T)
12433 or else Is_Progenitor (Iface_Ent, Act_T)
12434 then
12435 null;
12436
12437 elsif Ekind (Scope (Iface_Ent)) = E_Generic_Package
12438 and then Instance_Exists (Iface_Ent)
12439 then
12440 null;
12441
12442 else
12443 Error_Msg_Name_1 := Chars (Act_T);
12444 Error_Msg_NE
12445 ("Actual% must implement interface&",
12446 Actual, Etype (Iface));
12447 end if;
12448
12449 Next (Iface);
12450 end loop;
12451 end;
12452 end if;
12453 end if;
12454
12455 -- If the parent type in the generic declaration is itself a previous
12456 -- formal type, then it is local to the generic and absent from the
12457 -- analyzed generic definition. In that case the ancestor is the
12458 -- instance of the formal (which must have been instantiated
12459 -- previously), unless the ancestor is itself a formal derived type.
12460 -- In this latter case (which is the subject of Corrigendum 8652/0038
12461 -- (AI-202) the ancestor of the formals is the ancestor of its
12462 -- parent. Otherwise, the analyzed generic carries the parent type.
12463 -- If the parent type is defined in a previous formal package, then
12464 -- the scope of that formal package is that of the generic type
12465 -- itself, and it has already been mapped into the corresponding type
12466 -- in the actual package.
12467
12468 -- Common case: parent type defined outside of the generic
12469
12470 if Is_Entity_Name (Subtype_Mark (Def))
12471 and then Present (Entity (Subtype_Mark (Def)))
12472 then
12473 Ancestor := Get_Instance_Of (Entity (Subtype_Mark (Def)));
12474
12475 -- Check whether parent is defined in a previous formal package
12476
12477 elsif
12478 Scope (Scope (Base_Type (Etype (A_Gen_T)))) = Scope (A_Gen_T)
12479 then
12480 Ancestor :=
12481 Get_Instance_Of (Base_Type (Etype (A_Gen_T)));
12482
12483 -- The type may be a local derivation, or a type extension of a
12484 -- previous formal, or of a formal of a parent package.
12485
12486 elsif Is_Derived_Type (Get_Instance_Of (A_Gen_T))
12487 or else
12488 Ekind (Get_Instance_Of (A_Gen_T)) = E_Record_Type_With_Private
12489 then
12490 -- Check whether the parent is another derived formal type in the
12491 -- same generic unit.
12492
12493 if Etype (A_Gen_T) /= A_Gen_T
12494 and then Is_Generic_Type (Etype (A_Gen_T))
12495 and then Scope (Etype (A_Gen_T)) = Scope (A_Gen_T)
12496 and then Etype (Etype (A_Gen_T)) /= Etype (A_Gen_T)
12497 then
12498 -- Locate ancestor of parent from the subtype declaration
12499 -- created for the actual.
12500
12501 declare
12502 Decl : Node_Id;
12503
12504 begin
12505 Decl := First (Actual_Decls);
12506 while Present (Decl) loop
12507 if Nkind (Decl) = N_Subtype_Declaration
12508 and then Chars (Defining_Identifier (Decl)) =
12509 Chars (Etype (A_Gen_T))
12510 then
12511 Ancestor := Generic_Parent_Type (Decl);
12512 exit;
12513 else
12514 Next (Decl);
12515 end if;
12516 end loop;
12517 end;
12518
12519 pragma Assert (Present (Ancestor));
12520
12521 -- The ancestor itself may be a previous formal that has been
12522 -- instantiated.
12523
12524 Ancestor := Get_Instance_Of (Ancestor);
12525
12526 else
12527 Ancestor :=
12528 Get_Instance_Of (Base_Type (Get_Instance_Of (A_Gen_T)));
12529 end if;
12530
12531 -- Check whether parent is a previous formal of the current generic
12532
12533 elsif Is_Derived_Type (A_Gen_T)
12534 and then Is_Generic_Type (Etype (A_Gen_T))
12535 and then Scope (A_Gen_T) = Scope (Etype (A_Gen_T))
12536 then
12537 Ancestor := Get_Instance_Of (First_Subtype (Etype (A_Gen_T)));
12538
12539 -- An unusual case: the actual is a type declared in a parent unit,
12540 -- but is not a formal type so there is no instance_of for it.
12541 -- Retrieve it by analyzing the record extension.
12542
12543 elsif Is_Child_Unit (Scope (A_Gen_T))
12544 and then In_Open_Scopes (Scope (Act_T))
12545 and then Is_Generic_Instance (Scope (Act_T))
12546 then
12547 Analyze (Subtype_Mark (Def));
12548 Ancestor := Entity (Subtype_Mark (Def));
12549
12550 else
12551 Ancestor := Get_Instance_Of (Etype (Base_Type (A_Gen_T)));
12552 end if;
12553
12554 -- If the formal derived type has pragma Preelaborable_Initialization
12555 -- then the actual type must have preelaborable initialization.
12556
12557 if Known_To_Have_Preelab_Init (A_Gen_T)
12558 and then not Has_Preelaborable_Initialization (Act_T)
12559 then
12560 Error_Msg_NE
12561 ("actual for & must have preelaborable initialization",
12562 Actual, Gen_T);
12563 end if;
12564
12565 -- Ada 2005 (AI-251)
12566
12567 if Ada_Version >= Ada_2005 and then Is_Interface (Ancestor) then
12568 if not Interface_Present_In_Ancestor (Act_T, Ancestor) then
12569 Error_Msg_NE
12570 ("(Ada 2005) expected type implementing & in instantiation",
12571 Actual, Ancestor);
12572 end if;
12573
12574 -- Finally verify that the (instance of) the ancestor is an ancestor
12575 -- of the actual.
12576
12577 elsif not Is_Ancestor (Base_Type (Ancestor), Act_T) then
12578 Error_Msg_NE
12579 ("expect type derived from & in instantiation",
12580 Actual, First_Subtype (Ancestor));
12581 Abandon_Instantiation (Actual);
12582 end if;
12583
12584 -- Ada 2005 (AI-443): Synchronized formal derived type checks. Note
12585 -- that the formal type declaration has been rewritten as a private
12586 -- extension.
12587
12588 if Ada_Version >= Ada_2005
12589 and then Nkind (Parent (A_Gen_T)) = N_Private_Extension_Declaration
12590 and then Synchronized_Present (Parent (A_Gen_T))
12591 then
12592 -- The actual must be a synchronized tagged type
12593
12594 if not Is_Tagged_Type (Act_T) then
12595 Error_Msg_N
12596 ("actual of synchronized type must be tagged", Actual);
12597 Abandon_Instantiation (Actual);
12598
12599 elsif Nkind (Parent (Act_T)) = N_Full_Type_Declaration
12600 and then Nkind (Type_Definition (Parent (Act_T))) =
12601 N_Derived_Type_Definition
12602 and then not Synchronized_Present
12603 (Type_Definition (Parent (Act_T)))
12604 then
12605 Error_Msg_N
12606 ("actual of synchronized type must be synchronized", Actual);
12607 Abandon_Instantiation (Actual);
12608 end if;
12609 end if;
12610
12611 -- Perform atomic/volatile checks (RM C.6(12)). Note that AI05-0218-1
12612 -- removes the second instance of the phrase "or allow pass by copy".
12613
12614 if Is_Atomic (Act_T) and then not Is_Atomic (Ancestor) then
12615 Error_Msg_N
12616 ("cannot have atomic actual type for non-atomic formal type",
12617 Actual);
12618
12619 elsif Is_Volatile (Act_T) and then not Is_Volatile (Ancestor) then
12620 Error_Msg_N
12621 ("cannot have volatile actual type for non-volatile formal type",
12622 Actual);
12623 end if;
12624
12625 -- It should not be necessary to check for unknown discriminants on
12626 -- Formal, but for some reason Has_Unknown_Discriminants is false for
12627 -- A_Gen_T, so Is_Definite_Subtype incorrectly returns True. This
12628 -- needs fixing. ???
12629
12630 if Is_Definite_Subtype (A_Gen_T)
12631 and then not Unknown_Discriminants_Present (Formal)
12632 and then not Is_Definite_Subtype (Act_T)
12633 then
12634 Error_Msg_N ("actual subtype must be constrained", Actual);
12635 Abandon_Instantiation (Actual);
12636 end if;
12637
12638 if not Unknown_Discriminants_Present (Formal) then
12639 if Is_Constrained (Ancestor) then
12640 if not Is_Constrained (Act_T) then
12641 Error_Msg_N ("actual subtype must be constrained", Actual);
12642 Abandon_Instantiation (Actual);
12643 end if;
12644
12645 -- Ancestor is unconstrained, Check if generic formal and actual
12646 -- agree on constrainedness. The check only applies to array types
12647 -- and discriminated types.
12648
12649 elsif Is_Constrained (Act_T) then
12650 if Ekind (Ancestor) = E_Access_Type
12651 or else (not Is_Constrained (A_Gen_T)
12652 and then Is_Composite_Type (A_Gen_T))
12653 then
12654 Error_Msg_N ("actual subtype must be unconstrained", Actual);
12655 Abandon_Instantiation (Actual);
12656 end if;
12657
12658 -- A class-wide type is only allowed if the formal has unknown
12659 -- discriminants.
12660
12661 elsif Is_Class_Wide_Type (Act_T)
12662 and then not Has_Unknown_Discriminants (Ancestor)
12663 then
12664 Error_Msg_NE
12665 ("actual for & cannot be a class-wide type", Actual, Gen_T);
12666 Abandon_Instantiation (Actual);
12667
12668 -- Otherwise, the formal and actual must have the same number
12669 -- of discriminants and each discriminant of the actual must
12670 -- correspond to a discriminant of the formal.
12671
12672 elsif Has_Discriminants (Act_T)
12673 and then not Has_Unknown_Discriminants (Act_T)
12674 and then Has_Discriminants (Ancestor)
12675 then
12676 Actual_Discr := First_Discriminant (Act_T);
12677 Ancestor_Discr := First_Discriminant (Ancestor);
12678 while Present (Actual_Discr)
12679 and then Present (Ancestor_Discr)
12680 loop
12681 if Base_Type (Act_T) /= Base_Type (Ancestor) and then
12682 No (Corresponding_Discriminant (Actual_Discr))
12683 then
12684 Error_Msg_NE
12685 ("discriminant & does not correspond "
12686 & "to ancestor discriminant", Actual, Actual_Discr);
12687 Abandon_Instantiation (Actual);
12688 end if;
12689
12690 Next_Discriminant (Actual_Discr);
12691 Next_Discriminant (Ancestor_Discr);
12692 end loop;
12693
12694 if Present (Actual_Discr) or else Present (Ancestor_Discr) then
12695 Error_Msg_NE
12696 ("actual for & must have same number of discriminants",
12697 Actual, Gen_T);
12698 Abandon_Instantiation (Actual);
12699 end if;
12700
12701 -- This case should be caught by the earlier check for
12702 -- constrainedness, but the check here is added for completeness.
12703
12704 elsif Has_Discriminants (Act_T)
12705 and then not Has_Unknown_Discriminants (Act_T)
12706 then
12707 Error_Msg_NE
12708 ("actual for & must not have discriminants", Actual, Gen_T);
12709 Abandon_Instantiation (Actual);
12710
12711 elsif Has_Discriminants (Ancestor) then
12712 Error_Msg_NE
12713 ("actual for & must have known discriminants", Actual, Gen_T);
12714 Abandon_Instantiation (Actual);
12715 end if;
12716
12717 if not Subtypes_Statically_Compatible
12718 (Act_T, Ancestor, Formal_Derived_Matching => True)
12719 then
12720 Error_Msg_N
12721 ("constraint on actual is incompatible with formal", Actual);
12722 Abandon_Instantiation (Actual);
12723 end if;
12724 end if;
12725
12726 -- If the formal and actual types are abstract, check that there
12727 -- are no abstract primitives of the actual type that correspond to
12728 -- nonabstract primitives of the formal type (second sentence of
12729 -- RM95 3.9.3(9)).
12730
12731 if Is_Abstract_Type (A_Gen_T) and then Is_Abstract_Type (Act_T) then
12732 Check_Abstract_Primitives : declare
12733 Gen_Prims : constant Elist_Id :=
12734 Primitive_Operations (A_Gen_T);
12735 Gen_Elmt : Elmt_Id;
12736 Gen_Subp : Entity_Id;
12737 Anc_Subp : Entity_Id;
12738 Anc_Formal : Entity_Id;
12739 Anc_F_Type : Entity_Id;
12740
12741 Act_Prims : constant Elist_Id := Primitive_Operations (Act_T);
12742 Act_Elmt : Elmt_Id;
12743 Act_Subp : Entity_Id;
12744 Act_Formal : Entity_Id;
12745 Act_F_Type : Entity_Id;
12746
12747 Subprograms_Correspond : Boolean;
12748
12749 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean;
12750 -- Returns true if T2 is derived directly or indirectly from
12751 -- T1, including derivations from interfaces. T1 and T2 are
12752 -- required to be specific tagged base types.
12753
12754 ------------------------
12755 -- Is_Tagged_Ancestor --
12756 ------------------------
12757
12758 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean
12759 is
12760 Intfc_Elmt : Elmt_Id;
12761
12762 begin
12763 -- The predicate is satisfied if the types are the same
12764
12765 if T1 = T2 then
12766 return True;
12767
12768 -- If we've reached the top of the derivation chain then
12769 -- we know that T1 is not an ancestor of T2.
12770
12771 elsif Etype (T2) = T2 then
12772 return False;
12773
12774 -- Proceed to check T2's immediate parent
12775
12776 elsif Is_Ancestor (T1, Base_Type (Etype (T2))) then
12777 return True;
12778
12779 -- Finally, check to see if T1 is an ancestor of any of T2's
12780 -- progenitors.
12781
12782 else
12783 Intfc_Elmt := First_Elmt (Interfaces (T2));
12784 while Present (Intfc_Elmt) loop
12785 if Is_Ancestor (T1, Node (Intfc_Elmt)) then
12786 return True;
12787 end if;
12788
12789 Next_Elmt (Intfc_Elmt);
12790 end loop;
12791 end if;
12792
12793 return False;
12794 end Is_Tagged_Ancestor;
12795
12796 -- Start of processing for Check_Abstract_Primitives
12797
12798 begin
12799 -- Loop over all of the formal derived type's primitives
12800
12801 Gen_Elmt := First_Elmt (Gen_Prims);
12802 while Present (Gen_Elmt) loop
12803 Gen_Subp := Node (Gen_Elmt);
12804
12805 -- If the primitive of the formal is not abstract, then
12806 -- determine whether there is a corresponding primitive of
12807 -- the actual type that's abstract.
12808
12809 if not Is_Abstract_Subprogram (Gen_Subp) then
12810 Act_Elmt := First_Elmt (Act_Prims);
12811 while Present (Act_Elmt) loop
12812 Act_Subp := Node (Act_Elmt);
12813
12814 -- If we find an abstract primitive of the actual,
12815 -- then we need to test whether it corresponds to the
12816 -- subprogram from which the generic formal primitive
12817 -- is inherited.
12818
12819 if Is_Abstract_Subprogram (Act_Subp) then
12820 Anc_Subp := Alias (Gen_Subp);
12821
12822 -- Test whether we have a corresponding primitive
12823 -- by comparing names, kinds, formal types, and
12824 -- result types.
12825
12826 if Chars (Anc_Subp) = Chars (Act_Subp)
12827 and then Ekind (Anc_Subp) = Ekind (Act_Subp)
12828 then
12829 Anc_Formal := First_Formal (Anc_Subp);
12830 Act_Formal := First_Formal (Act_Subp);
12831 while Present (Anc_Formal)
12832 and then Present (Act_Formal)
12833 loop
12834 Anc_F_Type := Etype (Anc_Formal);
12835 Act_F_Type := Etype (Act_Formal);
12836
12837 if Ekind (Anc_F_Type) =
12838 E_Anonymous_Access_Type
12839 then
12840 Anc_F_Type := Designated_Type (Anc_F_Type);
12841
12842 if Ekind (Act_F_Type) =
12843 E_Anonymous_Access_Type
12844 then
12845 Act_F_Type :=
12846 Designated_Type (Act_F_Type);
12847 else
12848 exit;
12849 end if;
12850
12851 elsif
12852 Ekind (Act_F_Type) = E_Anonymous_Access_Type
12853 then
12854 exit;
12855 end if;
12856
12857 Anc_F_Type := Base_Type (Anc_F_Type);
12858 Act_F_Type := Base_Type (Act_F_Type);
12859
12860 -- If the formal is controlling, then the
12861 -- the type of the actual primitive's formal
12862 -- must be derived directly or indirectly
12863 -- from the type of the ancestor primitive's
12864 -- formal.
12865
12866 if Is_Controlling_Formal (Anc_Formal) then
12867 if not Is_Tagged_Ancestor
12868 (Anc_F_Type, Act_F_Type)
12869 then
12870 exit;
12871 end if;
12872
12873 -- Otherwise the types of the formals must
12874 -- be the same.
12875
12876 elsif Anc_F_Type /= Act_F_Type then
12877 exit;
12878 end if;
12879
12880 Next_Entity (Anc_Formal);
12881 Next_Entity (Act_Formal);
12882 end loop;
12883
12884 -- If we traversed through all of the formals
12885 -- then so far the subprograms correspond, so
12886 -- now check that any result types correspond.
12887
12888 if No (Anc_Formal) and then No (Act_Formal) then
12889 Subprograms_Correspond := True;
12890
12891 if Ekind (Act_Subp) = E_Function then
12892 Anc_F_Type := Etype (Anc_Subp);
12893 Act_F_Type := Etype (Act_Subp);
12894
12895 if Ekind (Anc_F_Type) =
12896 E_Anonymous_Access_Type
12897 then
12898 Anc_F_Type :=
12899 Designated_Type (Anc_F_Type);
12900
12901 if Ekind (Act_F_Type) =
12902 E_Anonymous_Access_Type
12903 then
12904 Act_F_Type :=
12905 Designated_Type (Act_F_Type);
12906 else
12907 Subprograms_Correspond := False;
12908 end if;
12909
12910 elsif
12911 Ekind (Act_F_Type)
12912 = E_Anonymous_Access_Type
12913 then
12914 Subprograms_Correspond := False;
12915 end if;
12916
12917 Anc_F_Type := Base_Type (Anc_F_Type);
12918 Act_F_Type := Base_Type (Act_F_Type);
12919
12920 -- Now either the result types must be
12921 -- the same or, if the result type is
12922 -- controlling, the result type of the
12923 -- actual primitive must descend from the
12924 -- result type of the ancestor primitive.
12925
12926 if Subprograms_Correspond
12927 and then Anc_F_Type /= Act_F_Type
12928 and then
12929 Has_Controlling_Result (Anc_Subp)
12930 and then not Is_Tagged_Ancestor
12931 (Anc_F_Type, Act_F_Type)
12932 then
12933 Subprograms_Correspond := False;
12934 end if;
12935 end if;
12936
12937 -- Found a matching subprogram belonging to
12938 -- formal ancestor type, so actual subprogram
12939 -- corresponds and this violates 3.9.3(9).
12940
12941 if Subprograms_Correspond then
12942 Error_Msg_NE
12943 ("abstract subprogram & overrides "
12944 & "nonabstract subprogram of ancestor",
12945 Actual, Act_Subp);
12946 end if;
12947 end if;
12948 end if;
12949 end if;
12950
12951 Next_Elmt (Act_Elmt);
12952 end loop;
12953 end if;
12954
12955 Next_Elmt (Gen_Elmt);
12956 end loop;
12957 end Check_Abstract_Primitives;
12958 end if;
12959
12960 -- Verify that limitedness matches. If parent is a limited
12961 -- interface then the generic formal is not unless declared
12962 -- explicitly so. If not declared limited, the actual cannot be
12963 -- limited (see AI05-0087).
12964
12965 -- Even though this AI is a binding interpretation, we enable the
12966 -- check only in Ada 2012 mode, because this improper construct
12967 -- shows up in user code and in existing B-tests.
12968
12969 if Is_Limited_Type (Act_T)
12970 and then not Is_Limited_Type (A_Gen_T)
12971 and then Ada_Version >= Ada_2012
12972 then
12973 if In_Instance then
12974 null;
12975 else
12976 Error_Msg_NE
12977 ("actual for non-limited & cannot be a limited type",
12978 Actual, Gen_T);
12979 Explain_Limited_Type (Act_T, Actual);
12980 Abandon_Instantiation (Actual);
12981 end if;
12982 end if;
12983 end Validate_Derived_Type_Instance;
12984
12985 ----------------------------------------
12986 -- Validate_Discriminated_Formal_Type --
12987 ----------------------------------------
12988
12989 procedure Validate_Discriminated_Formal_Type is
12990 Formal_Discr : Entity_Id;
12991 Actual_Discr : Entity_Id;
12992 Formal_Subt : Entity_Id;
12993
12994 begin
12995 if Has_Discriminants (A_Gen_T) then
12996 if not Has_Discriminants (Act_T) then
12997 Error_Msg_NE
12998 ("actual for & must have discriminants", Actual, Gen_T);
12999 Abandon_Instantiation (Actual);
13000
13001 elsif Is_Constrained (Act_T) then
13002 Error_Msg_NE
13003 ("actual for & must be unconstrained", Actual, Gen_T);
13004 Abandon_Instantiation (Actual);
13005
13006 else
13007 Formal_Discr := First_Discriminant (A_Gen_T);
13008 Actual_Discr := First_Discriminant (Act_T);
13009 while Formal_Discr /= Empty loop
13010 if Actual_Discr = Empty then
13011 Error_Msg_NE
13012 ("discriminants on actual do not match formal",
13013 Actual, Gen_T);
13014 Abandon_Instantiation (Actual);
13015 end if;
13016
13017 Formal_Subt := Get_Instance_Of (Etype (Formal_Discr));
13018
13019 -- Access discriminants match if designated types do
13020
13021 if Ekind (Base_Type (Formal_Subt)) = E_Anonymous_Access_Type
13022 and then (Ekind (Base_Type (Etype (Actual_Discr)))) =
13023 E_Anonymous_Access_Type
13024 and then
13025 Get_Instance_Of
13026 (Designated_Type (Base_Type (Formal_Subt))) =
13027 Designated_Type (Base_Type (Etype (Actual_Discr)))
13028 then
13029 null;
13030
13031 elsif Base_Type (Formal_Subt) /=
13032 Base_Type (Etype (Actual_Discr))
13033 then
13034 Error_Msg_NE
13035 ("types of actual discriminants must match formal",
13036 Actual, Gen_T);
13037 Abandon_Instantiation (Actual);
13038
13039 elsif not Subtypes_Statically_Match
13040 (Formal_Subt, Etype (Actual_Discr))
13041 and then Ada_Version >= Ada_95
13042 then
13043 Error_Msg_NE
13044 ("subtypes of actual discriminants must match formal",
13045 Actual, Gen_T);
13046 Abandon_Instantiation (Actual);
13047 end if;
13048
13049 Next_Discriminant (Formal_Discr);
13050 Next_Discriminant (Actual_Discr);
13051 end loop;
13052
13053 if Actual_Discr /= Empty then
13054 Error_Msg_NE
13055 ("discriminants on actual do not match formal",
13056 Actual, Gen_T);
13057 Abandon_Instantiation (Actual);
13058 end if;
13059 end if;
13060 end if;
13061 end Validate_Discriminated_Formal_Type;
13062
13063 ---------------------------------------
13064 -- Validate_Incomplete_Type_Instance --
13065 ---------------------------------------
13066
13067 procedure Validate_Incomplete_Type_Instance is
13068 begin
13069 if not Is_Tagged_Type (Act_T)
13070 and then Is_Tagged_Type (A_Gen_T)
13071 then
13072 Error_Msg_NE
13073 ("actual for & must be a tagged type", Actual, Gen_T);
13074 end if;
13075
13076 Validate_Discriminated_Formal_Type;
13077 end Validate_Incomplete_Type_Instance;
13078
13079 --------------------------------------
13080 -- Validate_Interface_Type_Instance --
13081 --------------------------------------
13082
13083 procedure Validate_Interface_Type_Instance is
13084 begin
13085 if not Is_Interface (Act_T) then
13086 Error_Msg_NE
13087 ("actual for formal interface type must be an interface",
13088 Actual, Gen_T);
13089
13090 elsif Is_Limited_Type (Act_T) /= Is_Limited_Type (A_Gen_T)
13091 or else Is_Task_Interface (A_Gen_T) /= Is_Task_Interface (Act_T)
13092 or else Is_Protected_Interface (A_Gen_T) /=
13093 Is_Protected_Interface (Act_T)
13094 or else Is_Synchronized_Interface (A_Gen_T) /=
13095 Is_Synchronized_Interface (Act_T)
13096 then
13097 Error_Msg_NE
13098 ("actual for interface& does not match (RM 12.5.5(4))",
13099 Actual, Gen_T);
13100 end if;
13101 end Validate_Interface_Type_Instance;
13102
13103 ------------------------------------
13104 -- Validate_Private_Type_Instance --
13105 ------------------------------------
13106
13107 procedure Validate_Private_Type_Instance is
13108 begin
13109 if Is_Limited_Type (Act_T)
13110 and then not Is_Limited_Type (A_Gen_T)
13111 then
13112 if In_Instance then
13113 null;
13114 else
13115 Error_Msg_NE
13116 ("actual for non-limited & cannot be a limited type", Actual,
13117 Gen_T);
13118 Explain_Limited_Type (Act_T, Actual);
13119 Abandon_Instantiation (Actual);
13120 end if;
13121
13122 elsif Known_To_Have_Preelab_Init (A_Gen_T)
13123 and then not Has_Preelaborable_Initialization (Act_T)
13124 then
13125 Error_Msg_NE
13126 ("actual for & must have preelaborable initialization", Actual,
13127 Gen_T);
13128
13129 elsif not Is_Definite_Subtype (Act_T)
13130 and then Is_Definite_Subtype (A_Gen_T)
13131 and then Ada_Version >= Ada_95
13132 then
13133 Error_Msg_NE
13134 ("actual for & must be a definite subtype", Actual, Gen_T);
13135
13136 elsif not Is_Tagged_Type (Act_T)
13137 and then Is_Tagged_Type (A_Gen_T)
13138 then
13139 Error_Msg_NE
13140 ("actual for & must be a tagged type", Actual, Gen_T);
13141 end if;
13142
13143 Validate_Discriminated_Formal_Type;
13144 Ancestor := Gen_T;
13145 end Validate_Private_Type_Instance;
13146
13147 -- Start of processing for Instantiate_Type
13148
13149 begin
13150 if Get_Instance_Of (A_Gen_T) /= A_Gen_T then
13151 Error_Msg_N ("duplicate instantiation of generic type", Actual);
13152 return New_List (Error);
13153
13154 elsif not Is_Entity_Name (Actual)
13155 or else not Is_Type (Entity (Actual))
13156 then
13157 Error_Msg_NE
13158 ("expect valid subtype mark to instantiate &", Actual, Gen_T);
13159 Abandon_Instantiation (Actual);
13160
13161 else
13162 Act_T := Entity (Actual);
13163
13164 -- Ada 2005 (AI-216): An Unchecked_Union subtype shall only be passed
13165 -- as a generic actual parameter if the corresponding formal type
13166 -- does not have a known_discriminant_part, or is a formal derived
13167 -- type that is an Unchecked_Union type.
13168
13169 if Is_Unchecked_Union (Base_Type (Act_T)) then
13170 if not Has_Discriminants (A_Gen_T)
13171 or else (Is_Derived_Type (A_Gen_T)
13172 and then Is_Unchecked_Union (A_Gen_T))
13173 then
13174 null;
13175 else
13176 Error_Msg_N ("unchecked union cannot be the actual for a "
13177 & "discriminated formal type", Act_T);
13178
13179 end if;
13180 end if;
13181
13182 -- Deal with fixed/floating restrictions
13183
13184 if Is_Floating_Point_Type (Act_T) then
13185 Check_Restriction (No_Floating_Point, Actual);
13186 elsif Is_Fixed_Point_Type (Act_T) then
13187 Check_Restriction (No_Fixed_Point, Actual);
13188 end if;
13189
13190 -- Deal with error of using incomplete type as generic actual.
13191 -- This includes limited views of a type, even if the non-limited
13192 -- view may be available.
13193
13194 if Ekind (Act_T) = E_Incomplete_Type
13195 or else (Is_Class_Wide_Type (Act_T)
13196 and then Ekind (Root_Type (Act_T)) = E_Incomplete_Type)
13197 then
13198 -- If the formal is an incomplete type, the actual can be
13199 -- incomplete as well.
13200
13201 if Ekind (A_Gen_T) = E_Incomplete_Type then
13202 null;
13203
13204 elsif Is_Class_Wide_Type (Act_T)
13205 or else No (Full_View (Act_T))
13206 then
13207 Error_Msg_N ("premature use of incomplete type", Actual);
13208 Abandon_Instantiation (Actual);
13209 else
13210 Act_T := Full_View (Act_T);
13211 Set_Entity (Actual, Act_T);
13212
13213 if Has_Private_Component (Act_T) then
13214 Error_Msg_N
13215 ("premature use of type with private component", Actual);
13216 end if;
13217 end if;
13218
13219 -- Deal with error of premature use of private type as generic actual
13220
13221 elsif Is_Private_Type (Act_T)
13222 and then Is_Private_Type (Base_Type (Act_T))
13223 and then not Is_Generic_Type (Act_T)
13224 and then not Is_Derived_Type (Act_T)
13225 and then No (Full_View (Root_Type (Act_T)))
13226 then
13227 -- If the formal is an incomplete type, the actual can be
13228 -- private or incomplete as well.
13229
13230 if Ekind (A_Gen_T) = E_Incomplete_Type then
13231 null;
13232 else
13233 Error_Msg_N ("premature use of private type", Actual);
13234 end if;
13235
13236 elsif Has_Private_Component (Act_T) then
13237 Error_Msg_N
13238 ("premature use of type with private component", Actual);
13239 end if;
13240
13241 Set_Instance_Of (A_Gen_T, Act_T);
13242
13243 -- If the type is generic, the class-wide type may also be used
13244
13245 if Is_Tagged_Type (A_Gen_T)
13246 and then Is_Tagged_Type (Act_T)
13247 and then not Is_Class_Wide_Type (A_Gen_T)
13248 then
13249 Set_Instance_Of (Class_Wide_Type (A_Gen_T),
13250 Class_Wide_Type (Act_T));
13251 end if;
13252
13253 if not Is_Abstract_Type (A_Gen_T)
13254 and then Is_Abstract_Type (Act_T)
13255 then
13256 Error_Msg_N
13257 ("actual of non-abstract formal cannot be abstract", Actual);
13258 end if;
13259
13260 -- A generic scalar type is a first subtype for which we generate
13261 -- an anonymous base type. Indicate that the instance of this base
13262 -- is the base type of the actual.
13263
13264 if Is_Scalar_Type (A_Gen_T) then
13265 Set_Instance_Of (Etype (A_Gen_T), Etype (Act_T));
13266 end if;
13267 end if;
13268
13269 if Error_Posted (Act_T) then
13270 null;
13271 else
13272 case Nkind (Def) is
13273 when N_Formal_Private_Type_Definition =>
13274 Validate_Private_Type_Instance;
13275
13276 when N_Formal_Incomplete_Type_Definition =>
13277 Validate_Incomplete_Type_Instance;
13278
13279 when N_Formal_Derived_Type_Definition =>
13280 Validate_Derived_Type_Instance;
13281
13282 when N_Formal_Discrete_Type_Definition =>
13283 if not Is_Discrete_Type (Act_T) then
13284 Error_Msg_NE
13285 ("expect discrete type in instantiation of&",
13286 Actual, Gen_T);
13287 Abandon_Instantiation (Actual);
13288 end if;
13289
13290 Diagnose_Predicated_Actual;
13291
13292 when N_Formal_Signed_Integer_Type_Definition =>
13293 if not Is_Signed_Integer_Type (Act_T) then
13294 Error_Msg_NE
13295 ("expect signed integer type in instantiation of&",
13296 Actual, Gen_T);
13297 Abandon_Instantiation (Actual);
13298 end if;
13299
13300 Diagnose_Predicated_Actual;
13301
13302 when N_Formal_Modular_Type_Definition =>
13303 if not Is_Modular_Integer_Type (Act_T) then
13304 Error_Msg_NE
13305 ("expect modular type in instantiation of &",
13306 Actual, Gen_T);
13307 Abandon_Instantiation (Actual);
13308 end if;
13309
13310 Diagnose_Predicated_Actual;
13311
13312 when N_Formal_Floating_Point_Definition =>
13313 if not Is_Floating_Point_Type (Act_T) then
13314 Error_Msg_NE
13315 ("expect float type in instantiation of &", Actual, Gen_T);
13316 Abandon_Instantiation (Actual);
13317 end if;
13318
13319 when N_Formal_Ordinary_Fixed_Point_Definition =>
13320 if not Is_Ordinary_Fixed_Point_Type (Act_T) then
13321 Error_Msg_NE
13322 ("expect ordinary fixed point type in instantiation of &",
13323 Actual, Gen_T);
13324 Abandon_Instantiation (Actual);
13325 end if;
13326
13327 when N_Formal_Decimal_Fixed_Point_Definition =>
13328 if not Is_Decimal_Fixed_Point_Type (Act_T) then
13329 Error_Msg_NE
13330 ("expect decimal type in instantiation of &",
13331 Actual, Gen_T);
13332 Abandon_Instantiation (Actual);
13333 end if;
13334
13335 when N_Array_Type_Definition =>
13336 Validate_Array_Type_Instance;
13337
13338 when N_Access_To_Object_Definition =>
13339 Validate_Access_Type_Instance;
13340
13341 when N_Access_Function_Definition
13342 | N_Access_Procedure_Definition
13343 =>
13344 Validate_Access_Subprogram_Instance;
13345
13346 when N_Record_Definition =>
13347 Validate_Interface_Type_Instance;
13348
13349 when N_Derived_Type_Definition =>
13350 Validate_Derived_Interface_Type_Instance;
13351
13352 when others =>
13353 raise Program_Error;
13354 end case;
13355 end if;
13356
13357 Subt := New_Copy (Gen_T);
13358
13359 -- Use adjusted sloc of subtype name as the location for other nodes in
13360 -- the subtype declaration.
13361
13362 Loc := Sloc (Subt);
13363
13364 Decl_Node :=
13365 Make_Subtype_Declaration (Loc,
13366 Defining_Identifier => Subt,
13367 Subtype_Indication => New_Occurrence_Of (Act_T, Loc));
13368
13369 if Is_Private_Type (Act_T) then
13370 Set_Has_Private_View (Subtype_Indication (Decl_Node));
13371
13372 elsif Is_Access_Type (Act_T)
13373 and then Is_Private_Type (Designated_Type (Act_T))
13374 then
13375 Set_Has_Private_View (Subtype_Indication (Decl_Node));
13376 end if;
13377
13378 -- In Ada 2012 the actual may be a limited view. Indicate that
13379 -- the local subtype must be treated as such.
13380
13381 if From_Limited_With (Act_T) then
13382 Set_Ekind (Subt, E_Incomplete_Subtype);
13383 Set_From_Limited_With (Subt);
13384 end if;
13385
13386 Decl_Nodes := New_List (Decl_Node);
13387
13388 -- Flag actual derived types so their elaboration produces the
13389 -- appropriate renamings for the primitive operations of the ancestor.
13390 -- Flag actual for formal private types as well, to determine whether
13391 -- operations in the private part may override inherited operations.
13392 -- If the formal has an interface list, the ancestor is not the
13393 -- parent, but the analyzed formal that includes the interface
13394 -- operations of all its progenitors.
13395
13396 -- Same treatment for formal private types, so we can check whether the
13397 -- type is tagged limited when validating derivations in the private
13398 -- part. (See AI05-096).
13399
13400 if Nkind (Def) = N_Formal_Derived_Type_Definition then
13401 if Present (Interface_List (Def)) then
13402 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
13403 else
13404 Set_Generic_Parent_Type (Decl_Node, Ancestor);
13405 end if;
13406
13407 elsif Nkind_In (Def, N_Formal_Private_Type_Definition,
13408 N_Formal_Incomplete_Type_Definition)
13409 then
13410 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
13411 end if;
13412
13413 -- If the actual is a synchronized type that implements an interface,
13414 -- the primitive operations are attached to the corresponding record,
13415 -- and we have to treat it as an additional generic actual, so that its
13416 -- primitive operations become visible in the instance. The task or
13417 -- protected type itself does not carry primitive operations.
13418
13419 if Is_Concurrent_Type (Act_T)
13420 and then Is_Tagged_Type (Act_T)
13421 and then Present (Corresponding_Record_Type (Act_T))
13422 and then Present (Ancestor)
13423 and then Is_Interface (Ancestor)
13424 then
13425 declare
13426 Corr_Rec : constant Entity_Id :=
13427 Corresponding_Record_Type (Act_T);
13428 New_Corr : Entity_Id;
13429 Corr_Decl : Node_Id;
13430
13431 begin
13432 New_Corr := Make_Temporary (Loc, 'S');
13433 Corr_Decl :=
13434 Make_Subtype_Declaration (Loc,
13435 Defining_Identifier => New_Corr,
13436 Subtype_Indication =>
13437 New_Occurrence_Of (Corr_Rec, Loc));
13438 Append_To (Decl_Nodes, Corr_Decl);
13439
13440 if Ekind (Act_T) = E_Task_Type then
13441 Set_Ekind (Subt, E_Task_Subtype);
13442 else
13443 Set_Ekind (Subt, E_Protected_Subtype);
13444 end if;
13445
13446 Set_Corresponding_Record_Type (Subt, Corr_Rec);
13447 Set_Generic_Parent_Type (Corr_Decl, Ancestor);
13448 Set_Generic_Parent_Type (Decl_Node, Empty);
13449 end;
13450 end if;
13451
13452 -- For a floating-point type, capture dimension info if any, because
13453 -- the generated subtype declaration does not come from source and
13454 -- will not process dimensions.
13455
13456 if Is_Floating_Point_Type (Act_T) then
13457 Copy_Dimensions (Act_T, Subt);
13458 end if;
13459
13460 return Decl_Nodes;
13461 end Instantiate_Type;
13462
13463 ---------------------
13464 -- Is_In_Main_Unit --
13465 ---------------------
13466
13467 function Is_In_Main_Unit (N : Node_Id) return Boolean is
13468 Unum : constant Unit_Number_Type := Get_Source_Unit (N);
13469 Current_Unit : Node_Id;
13470
13471 begin
13472 if Unum = Main_Unit then
13473 return True;
13474
13475 -- If the current unit is a subunit then it is either the main unit or
13476 -- is being compiled as part of the main unit.
13477
13478 elsif Nkind (N) = N_Compilation_Unit then
13479 return Nkind (Unit (N)) = N_Subunit;
13480 end if;
13481
13482 Current_Unit := Parent (N);
13483 while Present (Current_Unit)
13484 and then Nkind (Current_Unit) /= N_Compilation_Unit
13485 loop
13486 Current_Unit := Parent (Current_Unit);
13487 end loop;
13488
13489 -- The instantiation node is in the main unit, or else the current node
13490 -- (perhaps as the result of nested instantiations) is in the main unit,
13491 -- or in the declaration of the main unit, which in this last case must
13492 -- be a body.
13493
13494 return
13495 Current_Unit = Cunit (Main_Unit)
13496 or else Current_Unit = Library_Unit (Cunit (Main_Unit))
13497 or else (Present (Current_Unit)
13498 and then Present (Library_Unit (Current_Unit))
13499 and then Is_In_Main_Unit (Library_Unit (Current_Unit)));
13500 end Is_In_Main_Unit;
13501
13502 ----------------------------
13503 -- Load_Parent_Of_Generic --
13504 ----------------------------
13505
13506 procedure Load_Parent_Of_Generic
13507 (N : Node_Id;
13508 Spec : Node_Id;
13509 Body_Optional : Boolean := False)
13510 is
13511 Comp_Unit : constant Node_Id := Cunit (Get_Source_Unit (Spec));
13512 Saved_Style_Check : constant Boolean := Style_Check;
13513 Saved_Warnings : constant Warning_Record := Save_Warnings;
13514 True_Parent : Node_Id;
13515 Inst_Node : Node_Id;
13516 OK : Boolean;
13517 Previous_Instances : constant Elist_Id := New_Elmt_List;
13518
13519 procedure Collect_Previous_Instances (Decls : List_Id);
13520 -- Collect all instantiations in the given list of declarations, that
13521 -- precede the generic that we need to load. If the bodies of these
13522 -- instantiations are available, we must analyze them, to ensure that
13523 -- the public symbols generated are the same when the unit is compiled
13524 -- to generate code, and when it is compiled in the context of a unit
13525 -- that needs a particular nested instance. This process is applied to
13526 -- both package and subprogram instances.
13527
13528 --------------------------------
13529 -- Collect_Previous_Instances --
13530 --------------------------------
13531
13532 procedure Collect_Previous_Instances (Decls : List_Id) is
13533 Decl : Node_Id;
13534
13535 begin
13536 Decl := First (Decls);
13537 while Present (Decl) loop
13538 if Sloc (Decl) >= Sloc (Inst_Node) then
13539 return;
13540
13541 -- If Decl is an instantiation, then record it as requiring
13542 -- instantiation of the corresponding body, except if it is an
13543 -- abbreviated instantiation generated internally for conformance
13544 -- checking purposes only for the case of a formal package
13545 -- declared without a box (see Instantiate_Formal_Package). Such
13546 -- an instantiation does not generate any code (the actual code
13547 -- comes from actual) and thus does not need to be analyzed here.
13548 -- If the instantiation appears with a generic package body it is
13549 -- not analyzed here either.
13550
13551 elsif Nkind (Decl) = N_Package_Instantiation
13552 and then not Is_Internal (Defining_Entity (Decl))
13553 then
13554 Append_Elmt (Decl, Previous_Instances);
13555
13556 -- For a subprogram instantiation, omit instantiations intrinsic
13557 -- operations (Unchecked_Conversions, etc.) that have no bodies.
13558
13559 elsif Nkind_In (Decl, N_Function_Instantiation,
13560 N_Procedure_Instantiation)
13561 and then not Is_Intrinsic_Subprogram (Entity (Name (Decl)))
13562 then
13563 Append_Elmt (Decl, Previous_Instances);
13564
13565 elsif Nkind (Decl) = N_Package_Declaration then
13566 Collect_Previous_Instances
13567 (Visible_Declarations (Specification (Decl)));
13568 Collect_Previous_Instances
13569 (Private_Declarations (Specification (Decl)));
13570
13571 -- Previous non-generic bodies may contain instances as well
13572
13573 elsif Nkind (Decl) = N_Package_Body
13574 and then Ekind (Corresponding_Spec (Decl)) /= E_Generic_Package
13575 then
13576 Collect_Previous_Instances (Declarations (Decl));
13577
13578 elsif Nkind (Decl) = N_Subprogram_Body
13579 and then not Acts_As_Spec (Decl)
13580 and then not Is_Generic_Subprogram (Corresponding_Spec (Decl))
13581 then
13582 Collect_Previous_Instances (Declarations (Decl));
13583 end if;
13584
13585 Next (Decl);
13586 end loop;
13587 end Collect_Previous_Instances;
13588
13589 -- Start of processing for Load_Parent_Of_Generic
13590
13591 begin
13592 if not In_Same_Source_Unit (N, Spec)
13593 or else Nkind (Unit (Comp_Unit)) = N_Package_Declaration
13594 or else (Nkind (Unit (Comp_Unit)) = N_Package_Body
13595 and then not Is_In_Main_Unit (Spec))
13596 then
13597 -- Find body of parent of spec, and analyze it. A special case arises
13598 -- when the parent is an instantiation, that is to say when we are
13599 -- currently instantiating a nested generic. In that case, there is
13600 -- no separate file for the body of the enclosing instance. Instead,
13601 -- the enclosing body must be instantiated as if it were a pending
13602 -- instantiation, in order to produce the body for the nested generic
13603 -- we require now. Note that in that case the generic may be defined
13604 -- in a package body, the instance defined in the same package body,
13605 -- and the original enclosing body may not be in the main unit.
13606
13607 Inst_Node := Empty;
13608
13609 True_Parent := Parent (Spec);
13610 while Present (True_Parent)
13611 and then Nkind (True_Parent) /= N_Compilation_Unit
13612 loop
13613 if Nkind (True_Parent) = N_Package_Declaration
13614 and then
13615 Nkind (Original_Node (True_Parent)) = N_Package_Instantiation
13616 then
13617 -- Parent is a compilation unit that is an instantiation.
13618 -- Instantiation node has been replaced with package decl.
13619
13620 Inst_Node := Original_Node (True_Parent);
13621 exit;
13622
13623 elsif Nkind (True_Parent) = N_Package_Declaration
13624 and then Present (Generic_Parent (Specification (True_Parent)))
13625 and then Nkind (Parent (True_Parent)) /= N_Compilation_Unit
13626 then
13627 -- Parent is an instantiation within another specification.
13628 -- Declaration for instance has been inserted before original
13629 -- instantiation node. A direct link would be preferable?
13630
13631 Inst_Node := Next (True_Parent);
13632 while Present (Inst_Node)
13633 and then Nkind (Inst_Node) /= N_Package_Instantiation
13634 loop
13635 Next (Inst_Node);
13636 end loop;
13637
13638 -- If the instance appears within a generic, and the generic
13639 -- unit is defined within a formal package of the enclosing
13640 -- generic, there is no generic body available, and none
13641 -- needed. A more precise test should be used ???
13642
13643 if No (Inst_Node) then
13644 return;
13645 end if;
13646
13647 exit;
13648
13649 else
13650 True_Parent := Parent (True_Parent);
13651 end if;
13652 end loop;
13653
13654 -- Case where we are currently instantiating a nested generic
13655
13656 if Present (Inst_Node) then
13657 if Nkind (Parent (True_Parent)) = N_Compilation_Unit then
13658
13659 -- Instantiation node and declaration of instantiated package
13660 -- were exchanged when only the declaration was needed.
13661 -- Restore instantiation node before proceeding with body.
13662
13663 Set_Unit (Parent (True_Parent), Inst_Node);
13664 end if;
13665
13666 -- Now complete instantiation of enclosing body, if it appears in
13667 -- some other unit. If it appears in the current unit, the body
13668 -- will have been instantiated already.
13669
13670 if No (Corresponding_Body (Instance_Spec (Inst_Node))) then
13671
13672 -- We need to determine the expander mode to instantiate the
13673 -- enclosing body. Because the generic body we need may use
13674 -- global entities declared in the enclosing package (including
13675 -- aggregates) it is in general necessary to compile this body
13676 -- with expansion enabled, except if we are within a generic
13677 -- package, in which case the usual generic rule applies.
13678
13679 declare
13680 Exp_Status : Boolean := True;
13681 Scop : Entity_Id;
13682
13683 begin
13684 -- Loop through scopes looking for generic package
13685
13686 Scop := Scope (Defining_Entity (Instance_Spec (Inst_Node)));
13687 while Present (Scop)
13688 and then Scop /= Standard_Standard
13689 loop
13690 if Ekind (Scop) = E_Generic_Package then
13691 Exp_Status := False;
13692 exit;
13693 end if;
13694
13695 Scop := Scope (Scop);
13696 end loop;
13697
13698 -- Collect previous instantiations in the unit that contains
13699 -- the desired generic.
13700
13701 if Nkind (Parent (True_Parent)) /= N_Compilation_Unit
13702 and then not Body_Optional
13703 then
13704 declare
13705 Decl : Elmt_Id;
13706 Info : Pending_Body_Info;
13707 Par : Node_Id;
13708
13709 begin
13710 Par := Parent (Inst_Node);
13711 while Present (Par) loop
13712 exit when Nkind (Parent (Par)) = N_Compilation_Unit;
13713 Par := Parent (Par);
13714 end loop;
13715
13716 pragma Assert (Present (Par));
13717
13718 if Nkind (Par) = N_Package_Body then
13719 Collect_Previous_Instances (Declarations (Par));
13720
13721 elsif Nkind (Par) = N_Package_Declaration then
13722 Collect_Previous_Instances
13723 (Visible_Declarations (Specification (Par)));
13724 Collect_Previous_Instances
13725 (Private_Declarations (Specification (Par)));
13726
13727 else
13728 -- Enclosing unit is a subprogram body. In this
13729 -- case all instance bodies are processed in order
13730 -- and there is no need to collect them separately.
13731
13732 null;
13733 end if;
13734
13735 Decl := First_Elmt (Previous_Instances);
13736 while Present (Decl) loop
13737 Info :=
13738 (Inst_Node => Node (Decl),
13739 Act_Decl =>
13740 Instance_Spec (Node (Decl)),
13741 Expander_Status => Exp_Status,
13742 Current_Sem_Unit =>
13743 Get_Code_Unit (Sloc (Node (Decl))),
13744 Scope_Suppress => Scope_Suppress,
13745 Local_Suppress_Stack_Top =>
13746 Local_Suppress_Stack_Top,
13747 Version => Ada_Version,
13748 Version_Pragma => Ada_Version_Pragma,
13749 Warnings => Save_Warnings,
13750 SPARK_Mode => SPARK_Mode,
13751 SPARK_Mode_Pragma => SPARK_Mode_Pragma);
13752
13753 -- Package instance
13754
13755 if Nkind (Node (Decl)) = N_Package_Instantiation
13756 then
13757 Instantiate_Package_Body
13758 (Info, Body_Optional => True);
13759
13760 -- Subprogram instance
13761
13762 else
13763 -- The instance_spec is in the wrapper package,
13764 -- usually followed by its local renaming
13765 -- declaration. See Build_Subprogram_Renaming
13766 -- for details. If the instance carries aspects,
13767 -- these result in the corresponding pragmas,
13768 -- inserted after the subprogram declaration.
13769 -- They must be skipped as well when retrieving
13770 -- the desired spec. Some of them may have been
13771 -- rewritten as null statements.
13772 -- A direct link would be more robust ???
13773
13774 declare
13775 Decl : Node_Id :=
13776 (Last (Visible_Declarations
13777 (Specification (Info.Act_Decl))));
13778 begin
13779 while Nkind_In (Decl,
13780 N_Null_Statement,
13781 N_Pragma,
13782 N_Subprogram_Renaming_Declaration)
13783 loop
13784 Decl := Prev (Decl);
13785 end loop;
13786
13787 Info.Act_Decl := Decl;
13788 end;
13789
13790 Instantiate_Subprogram_Body
13791 (Info, Body_Optional => True);
13792 end if;
13793
13794 Next_Elmt (Decl);
13795 end loop;
13796 end;
13797 end if;
13798
13799 Instantiate_Package_Body
13800 (Body_Info =>
13801 ((Inst_Node => Inst_Node,
13802 Act_Decl => True_Parent,
13803 Expander_Status => Exp_Status,
13804 Current_Sem_Unit => Get_Code_Unit
13805 (Sloc (Inst_Node)),
13806 Scope_Suppress => Scope_Suppress,
13807 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
13808 Version => Ada_Version,
13809 Version_Pragma => Ada_Version_Pragma,
13810 Warnings => Save_Warnings,
13811 SPARK_Mode => SPARK_Mode,
13812 SPARK_Mode_Pragma => SPARK_Mode_Pragma)),
13813 Body_Optional => Body_Optional);
13814 end;
13815 end if;
13816
13817 -- Case where we are not instantiating a nested generic
13818
13819 else
13820 Opt.Style_Check := False;
13821 Expander_Mode_Save_And_Set (True);
13822 Load_Needed_Body (Comp_Unit, OK);
13823 Opt.Style_Check := Saved_Style_Check;
13824 Restore_Warnings (Saved_Warnings);
13825 Expander_Mode_Restore;
13826
13827 if not OK
13828 and then Unit_Requires_Body (Defining_Entity (Spec))
13829 and then not Body_Optional
13830 then
13831 declare
13832 Bname : constant Unit_Name_Type :=
13833 Get_Body_Name (Get_Unit_Name (Unit (Comp_Unit)));
13834
13835 begin
13836 -- In CodePeer mode, the missing body may make the analysis
13837 -- incomplete, but we do not treat it as fatal.
13838
13839 if CodePeer_Mode then
13840 return;
13841
13842 else
13843 Error_Msg_Unit_1 := Bname;
13844 Error_Msg_N ("this instantiation requires$!", N);
13845 Error_Msg_File_1 :=
13846 Get_File_Name (Bname, Subunit => False);
13847 Error_Msg_N ("\but file{ was not found!", N);
13848 raise Unrecoverable_Error;
13849 end if;
13850 end;
13851 end if;
13852 end if;
13853 end if;
13854
13855 -- If loading parent of the generic caused an instantiation circularity,
13856 -- we abandon compilation at this point, because otherwise in some cases
13857 -- we get into trouble with infinite recursions after this point.
13858
13859 if Circularity_Detected then
13860 raise Unrecoverable_Error;
13861 end if;
13862 end Load_Parent_Of_Generic;
13863
13864 ---------------------------------
13865 -- Map_Formal_Package_Entities --
13866 ---------------------------------
13867
13868 procedure Map_Formal_Package_Entities (Form : Entity_Id; Act : Entity_Id) is
13869 E1 : Entity_Id;
13870 E2 : Entity_Id;
13871
13872 begin
13873 Set_Instance_Of (Form, Act);
13874
13875 -- Traverse formal and actual package to map the corresponding entities.
13876 -- We skip over internal entities that may be generated during semantic
13877 -- analysis, and find the matching entities by name, given that they
13878 -- must appear in the same order.
13879
13880 E1 := First_Entity (Form);
13881 E2 := First_Entity (Act);
13882 while Present (E1) and then E1 /= First_Private_Entity (Form) loop
13883 -- Could this test be a single condition??? Seems like it could, and
13884 -- isn't FPE (Form) a constant anyway???
13885
13886 if not Is_Internal (E1)
13887 and then Present (Parent (E1))
13888 and then not Is_Class_Wide_Type (E1)
13889 and then not Is_Internal_Name (Chars (E1))
13890 then
13891 while Present (E2) and then Chars (E2) /= Chars (E1) loop
13892 Next_Entity (E2);
13893 end loop;
13894
13895 if No (E2) then
13896 exit;
13897 else
13898 Set_Instance_Of (E1, E2);
13899
13900 if Is_Type (E1) and then Is_Tagged_Type (E2) then
13901 Set_Instance_Of (Class_Wide_Type (E1), Class_Wide_Type (E2));
13902 end if;
13903
13904 if Is_Constrained (E1) then
13905 Set_Instance_Of (Base_Type (E1), Base_Type (E2));
13906 end if;
13907
13908 if Ekind (E1) = E_Package and then No (Renamed_Object (E1)) then
13909 Map_Formal_Package_Entities (E1, E2);
13910 end if;
13911 end if;
13912 end if;
13913
13914 Next_Entity (E1);
13915 end loop;
13916 end Map_Formal_Package_Entities;
13917
13918 -----------------------
13919 -- Move_Freeze_Nodes --
13920 -----------------------
13921
13922 procedure Move_Freeze_Nodes
13923 (Out_Of : Entity_Id;
13924 After : Node_Id;
13925 L : List_Id)
13926 is
13927 Decl : Node_Id;
13928 Next_Decl : Node_Id;
13929 Next_Node : Node_Id := After;
13930 Spec : Node_Id;
13931
13932 function Is_Outer_Type (T : Entity_Id) return Boolean;
13933 -- Check whether entity is declared in a scope external to that of the
13934 -- generic unit.
13935
13936 -------------------
13937 -- Is_Outer_Type --
13938 -------------------
13939
13940 function Is_Outer_Type (T : Entity_Id) return Boolean is
13941 Scop : Entity_Id := Scope (T);
13942
13943 begin
13944 if Scope_Depth (Scop) < Scope_Depth (Out_Of) then
13945 return True;
13946
13947 else
13948 while Scop /= Standard_Standard loop
13949 if Scop = Out_Of then
13950 return False;
13951 else
13952 Scop := Scope (Scop);
13953 end if;
13954 end loop;
13955
13956 return True;
13957 end if;
13958 end Is_Outer_Type;
13959
13960 -- Start of processing for Move_Freeze_Nodes
13961
13962 begin
13963 if No (L) then
13964 return;
13965 end if;
13966
13967 -- First remove the freeze nodes that may appear before all other
13968 -- declarations.
13969
13970 Decl := First (L);
13971 while Present (Decl)
13972 and then Nkind (Decl) = N_Freeze_Entity
13973 and then Is_Outer_Type (Entity (Decl))
13974 loop
13975 Decl := Remove_Head (L);
13976 Insert_After (Next_Node, Decl);
13977 Set_Analyzed (Decl, False);
13978 Next_Node := Decl;
13979 Decl := First (L);
13980 end loop;
13981
13982 -- Next scan the list of declarations and remove each freeze node that
13983 -- appears ahead of the current node.
13984
13985 while Present (Decl) loop
13986 while Present (Next (Decl))
13987 and then Nkind (Next (Decl)) = N_Freeze_Entity
13988 and then Is_Outer_Type (Entity (Next (Decl)))
13989 loop
13990 Next_Decl := Remove_Next (Decl);
13991 Insert_After (Next_Node, Next_Decl);
13992 Set_Analyzed (Next_Decl, False);
13993 Next_Node := Next_Decl;
13994 end loop;
13995
13996 -- If the declaration is a nested package or concurrent type, then
13997 -- recurse. Nested generic packages will have been processed from the
13998 -- inside out.
13999
14000 case Nkind (Decl) is
14001 when N_Package_Declaration =>
14002 Spec := Specification (Decl);
14003
14004 when N_Task_Type_Declaration =>
14005 Spec := Task_Definition (Decl);
14006
14007 when N_Protected_Type_Declaration =>
14008 Spec := Protected_Definition (Decl);
14009
14010 when others =>
14011 Spec := Empty;
14012 end case;
14013
14014 if Present (Spec) then
14015 Move_Freeze_Nodes (Out_Of, Next_Node, Visible_Declarations (Spec));
14016 Move_Freeze_Nodes (Out_Of, Next_Node, Private_Declarations (Spec));
14017 end if;
14018
14019 Next (Decl);
14020 end loop;
14021 end Move_Freeze_Nodes;
14022
14023 ----------------
14024 -- Next_Assoc --
14025 ----------------
14026
14027 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr is
14028 begin
14029 return Generic_Renamings.Table (E).Next_In_HTable;
14030 end Next_Assoc;
14031
14032 ------------------------
14033 -- Preanalyze_Actuals --
14034 ------------------------
14035
14036 procedure Preanalyze_Actuals (N : Node_Id; Inst : Entity_Id := Empty) is
14037 Assoc : Node_Id;
14038 Act : Node_Id;
14039 Errs : constant Nat := Serious_Errors_Detected;
14040
14041 Cur : Entity_Id := Empty;
14042 -- Current homograph of the instance name
14043
14044 Vis : Boolean := False;
14045 -- Saved visibility status of the current homograph
14046
14047 begin
14048 Assoc := First (Generic_Associations (N));
14049
14050 -- If the instance is a child unit, its name may hide an outer homonym,
14051 -- so make it invisible to perform name resolution on the actuals.
14052
14053 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name
14054 and then Present
14055 (Current_Entity (Defining_Identifier (Defining_Unit_Name (N))))
14056 then
14057 Cur := Current_Entity (Defining_Identifier (Defining_Unit_Name (N)));
14058
14059 if Is_Compilation_Unit (Cur) then
14060 Vis := Is_Immediately_Visible (Cur);
14061 Set_Is_Immediately_Visible (Cur, False);
14062 else
14063 Cur := Empty;
14064 end if;
14065 end if;
14066
14067 while Present (Assoc) loop
14068 if Nkind (Assoc) /= N_Others_Choice then
14069 Act := Explicit_Generic_Actual_Parameter (Assoc);
14070
14071 -- Within a nested instantiation, a defaulted actual is an empty
14072 -- association, so nothing to analyze. If the subprogram actual
14073 -- is an attribute, analyze prefix only, because actual is not a
14074 -- complete attribute reference.
14075
14076 -- If actual is an allocator, analyze expression only. The full
14077 -- analysis can generate code, and if instance is a compilation
14078 -- unit we have to wait until the package instance is installed
14079 -- to have a proper place to insert this code.
14080
14081 -- String literals may be operators, but at this point we do not
14082 -- know whether the actual is a formal subprogram or a string.
14083
14084 if No (Act) then
14085 null;
14086
14087 elsif Nkind (Act) = N_Attribute_Reference then
14088 Analyze (Prefix (Act));
14089
14090 elsif Nkind (Act) = N_Explicit_Dereference then
14091 Analyze (Prefix (Act));
14092
14093 elsif Nkind (Act) = N_Allocator then
14094 declare
14095 Expr : constant Node_Id := Expression (Act);
14096
14097 begin
14098 if Nkind (Expr) = N_Subtype_Indication then
14099 Analyze (Subtype_Mark (Expr));
14100
14101 -- Analyze separately each discriminant constraint, when
14102 -- given with a named association.
14103
14104 declare
14105 Constr : Node_Id;
14106
14107 begin
14108 Constr := First (Constraints (Constraint (Expr)));
14109 while Present (Constr) loop
14110 if Nkind (Constr) = N_Discriminant_Association then
14111 Analyze (Expression (Constr));
14112 else
14113 Analyze (Constr);
14114 end if;
14115
14116 Next (Constr);
14117 end loop;
14118 end;
14119
14120 else
14121 Analyze (Expr);
14122 end if;
14123 end;
14124
14125 elsif Nkind (Act) /= N_Operator_Symbol then
14126 Analyze (Act);
14127
14128 -- Within a package instance, mark actuals that are limited
14129 -- views, so their use can be moved to the body of the
14130 -- enclosing unit.
14131
14132 if Is_Entity_Name (Act)
14133 and then Is_Type (Entity (Act))
14134 and then From_Limited_With (Entity (Act))
14135 and then Present (Inst)
14136 then
14137 Append_Elmt (Entity (Act), Incomplete_Actuals (Inst));
14138 end if;
14139 end if;
14140
14141 if Errs /= Serious_Errors_Detected then
14142
14143 -- Do a minimal analysis of the generic, to prevent spurious
14144 -- warnings complaining about the generic being unreferenced,
14145 -- before abandoning the instantiation.
14146
14147 Analyze (Name (N));
14148
14149 if Is_Entity_Name (Name (N))
14150 and then Etype (Name (N)) /= Any_Type
14151 then
14152 Generate_Reference (Entity (Name (N)), Name (N));
14153 Set_Is_Instantiated (Entity (Name (N)));
14154 end if;
14155
14156 if Present (Cur) then
14157
14158 -- For the case of a child instance hiding an outer homonym,
14159 -- provide additional warning which might explain the error.
14160
14161 Set_Is_Immediately_Visible (Cur, Vis);
14162 Error_Msg_NE
14163 ("& hides outer unit with the same name??",
14164 N, Defining_Unit_Name (N));
14165 end if;
14166
14167 Abandon_Instantiation (Act);
14168 end if;
14169 end if;
14170
14171 Next (Assoc);
14172 end loop;
14173
14174 if Present (Cur) then
14175 Set_Is_Immediately_Visible (Cur, Vis);
14176 end if;
14177 end Preanalyze_Actuals;
14178
14179 -------------------------------
14180 -- Provide_Completing_Bodies --
14181 -------------------------------
14182
14183 procedure Provide_Completing_Bodies (N : Node_Id) is
14184 procedure Build_Completing_Body (Subp_Decl : Node_Id);
14185 -- Generate the completing body for subprogram declaration Subp_Decl
14186
14187 procedure Provide_Completing_Bodies_In (Decls : List_Id);
14188 -- Generating completing bodies for all subprograms found in declarative
14189 -- list Decls.
14190
14191 ---------------------------
14192 -- Build_Completing_Body --
14193 ---------------------------
14194
14195 procedure Build_Completing_Body (Subp_Decl : Node_Id) is
14196 Loc : constant Source_Ptr := Sloc (Subp_Decl);
14197 Subp_Id : constant Entity_Id := Defining_Entity (Subp_Decl);
14198 Spec : Node_Id;
14199
14200 begin
14201 -- Nothing to do if the subprogram already has a completing body
14202
14203 if Present (Corresponding_Body (Subp_Decl)) then
14204 return;
14205
14206 -- Mark the function as having a valid return statement even though
14207 -- the body contains a single raise statement.
14208
14209 elsif Ekind (Subp_Id) = E_Function then
14210 Set_Return_Present (Subp_Id);
14211 end if;
14212
14213 -- Clone the specification to obtain new entities and reset the only
14214 -- semantic field.
14215
14216 Spec := Copy_Subprogram_Spec (Specification (Subp_Decl));
14217 Set_Generic_Parent (Spec, Empty);
14218
14219 -- Generate:
14220 -- function Func ... return ... is
14221 -- <or>
14222 -- procedure Proc ... is
14223 -- begin
14224 -- raise Program_Error with "access before elaboration";
14225 -- edn Proc;
14226
14227 Insert_After_And_Analyze (Subp_Decl,
14228 Make_Subprogram_Body (Loc,
14229 Specification => Spec,
14230 Declarations => New_List,
14231 Handled_Statement_Sequence =>
14232 Make_Handled_Sequence_Of_Statements (Loc,
14233 Statements => New_List (
14234 Make_Raise_Program_Error (Loc,
14235 Reason => PE_Access_Before_Elaboration)))));
14236 end Build_Completing_Body;
14237
14238 ----------------------------------
14239 -- Provide_Completing_Bodies_In --
14240 ----------------------------------
14241
14242 procedure Provide_Completing_Bodies_In (Decls : List_Id) is
14243 Decl : Node_Id;
14244
14245 begin
14246 if Present (Decls) then
14247 Decl := First (Decls);
14248 while Present (Decl) loop
14249 Provide_Completing_Bodies (Decl);
14250 Next (Decl);
14251 end loop;
14252 end if;
14253 end Provide_Completing_Bodies_In;
14254
14255 -- Local variables
14256
14257 Spec : Node_Id;
14258
14259 -- Start of processing for Provide_Completing_Bodies
14260
14261 begin
14262 if Nkind (N) = N_Package_Declaration then
14263 Spec := Specification (N);
14264
14265 Push_Scope (Defining_Entity (N));
14266 Provide_Completing_Bodies_In (Visible_Declarations (Spec));
14267 Provide_Completing_Bodies_In (Private_Declarations (Spec));
14268 Pop_Scope;
14269
14270 elsif Nkind (N) = N_Subprogram_Declaration then
14271 Build_Completing_Body (N);
14272 end if;
14273 end Provide_Completing_Bodies;
14274
14275 -------------------
14276 -- Remove_Parent --
14277 -------------------
14278
14279 procedure Remove_Parent (In_Body : Boolean := False) is
14280 S : Entity_Id := Current_Scope;
14281 -- S is the scope containing the instantiation just completed. The scope
14282 -- stack contains the parent instances of the instantiation, followed by
14283 -- the original S.
14284
14285 Cur_P : Entity_Id;
14286 E : Entity_Id;
14287 P : Entity_Id;
14288 Hidden : Elmt_Id;
14289
14290 begin
14291 -- After child instantiation is complete, remove from scope stack the
14292 -- extra copy of the current scope, and then remove parent instances.
14293
14294 if not In_Body then
14295 Pop_Scope;
14296
14297 while Current_Scope /= S loop
14298 P := Current_Scope;
14299 End_Package_Scope (Current_Scope);
14300
14301 if In_Open_Scopes (P) then
14302 E := First_Entity (P);
14303 while Present (E) loop
14304 Set_Is_Immediately_Visible (E, True);
14305 Next_Entity (E);
14306 end loop;
14307
14308 -- If instantiation is declared in a block, it is the enclosing
14309 -- scope that might be a parent instance. Note that only one
14310 -- block can be involved, because the parent instances have
14311 -- been installed within it.
14312
14313 if Ekind (P) = E_Block then
14314 Cur_P := Scope (P);
14315 else
14316 Cur_P := P;
14317 end if;
14318
14319 if Is_Generic_Instance (Cur_P) and then P /= Current_Scope then
14320 -- We are within an instance of some sibling. Retain
14321 -- visibility of parent, for proper subsequent cleanup, and
14322 -- reinstall private declarations as well.
14323
14324 Set_In_Private_Part (P);
14325 Install_Private_Declarations (P);
14326 end if;
14327
14328 -- If the ultimate parent is a top-level unit recorded in
14329 -- Instance_Parent_Unit, then reset its visibility to what it was
14330 -- before instantiation. (It's not clear what the purpose is of
14331 -- testing whether Scope (P) is In_Open_Scopes, but that test was
14332 -- present before the ultimate parent test was added.???)
14333
14334 elsif not In_Open_Scopes (Scope (P))
14335 or else (P = Instance_Parent_Unit
14336 and then not Parent_Unit_Visible)
14337 then
14338 Set_Is_Immediately_Visible (P, False);
14339
14340 -- If the current scope is itself an instantiation of a generic
14341 -- nested within P, and we are in the private part of body of this
14342 -- instantiation, restore the full views of P, that were removed
14343 -- in End_Package_Scope above. This obscure case can occur when a
14344 -- subunit of a generic contains an instance of a child unit of
14345 -- its generic parent unit.
14346
14347 elsif S = Current_Scope and then Is_Generic_Instance (S) then
14348 declare
14349 Par : constant Entity_Id :=
14350 Generic_Parent (Package_Specification (S));
14351 begin
14352 if Present (Par)
14353 and then P = Scope (Par)
14354 and then (In_Package_Body (S) or else In_Private_Part (S))
14355 then
14356 Set_In_Private_Part (P);
14357 Install_Private_Declarations (P);
14358 end if;
14359 end;
14360 end if;
14361 end loop;
14362
14363 -- Reset visibility of entities in the enclosing scope
14364
14365 Set_Is_Hidden_Open_Scope (Current_Scope, False);
14366
14367 Hidden := First_Elmt (Hidden_Entities);
14368 while Present (Hidden) loop
14369 Set_Is_Immediately_Visible (Node (Hidden), True);
14370 Next_Elmt (Hidden);
14371 end loop;
14372
14373 else
14374 -- Each body is analyzed separately, and there is no context that
14375 -- needs preserving from one body instance to the next, so remove all
14376 -- parent scopes that have been installed.
14377
14378 while Present (S) loop
14379 End_Package_Scope (S);
14380 Set_Is_Immediately_Visible (S, False);
14381 S := Current_Scope;
14382 exit when S = Standard_Standard;
14383 end loop;
14384 end if;
14385 end Remove_Parent;
14386
14387 -----------------
14388 -- Restore_Env --
14389 -----------------
14390
14391 procedure Restore_Env is
14392 Saved : Instance_Env renames Instance_Envs.Table (Instance_Envs.Last);
14393
14394 begin
14395 if No (Current_Instantiated_Parent.Act_Id) then
14396 -- Restore environment after subprogram inlining
14397
14398 Restore_Private_Views (Empty);
14399 end if;
14400
14401 Current_Instantiated_Parent := Saved.Instantiated_Parent;
14402 Exchanged_Views := Saved.Exchanged_Views;
14403 Hidden_Entities := Saved.Hidden_Entities;
14404 Current_Sem_Unit := Saved.Current_Sem_Unit;
14405 Parent_Unit_Visible := Saved.Parent_Unit_Visible;
14406 Instance_Parent_Unit := Saved.Instance_Parent_Unit;
14407
14408 Restore_Opt_Config_Switches (Saved.Switches);
14409
14410 Instance_Envs.Decrement_Last;
14411 end Restore_Env;
14412
14413 ---------------------------
14414 -- Restore_Private_Views --
14415 ---------------------------
14416
14417 procedure Restore_Private_Views
14418 (Pack_Id : Entity_Id;
14419 Is_Package : Boolean := True)
14420 is
14421 M : Elmt_Id;
14422 E : Entity_Id;
14423 Typ : Entity_Id;
14424 Dep_Elmt : Elmt_Id;
14425 Dep_Typ : Node_Id;
14426
14427 procedure Restore_Nested_Formal (Formal : Entity_Id);
14428 -- Hide the generic formals of formal packages declared with box which
14429 -- were reachable in the current instantiation.
14430
14431 ---------------------------
14432 -- Restore_Nested_Formal --
14433 ---------------------------
14434
14435 procedure Restore_Nested_Formal (Formal : Entity_Id) is
14436 Ent : Entity_Id;
14437
14438 begin
14439 if Present (Renamed_Object (Formal))
14440 and then Denotes_Formal_Package (Renamed_Object (Formal), True)
14441 then
14442 return;
14443
14444 elsif Present (Associated_Formal_Package (Formal)) then
14445 Ent := First_Entity (Formal);
14446 while Present (Ent) loop
14447 exit when Ekind (Ent) = E_Package
14448 and then Renamed_Entity (Ent) = Renamed_Entity (Formal);
14449
14450 Set_Is_Hidden (Ent);
14451 Set_Is_Potentially_Use_Visible (Ent, False);
14452
14453 -- If package, then recurse
14454
14455 if Ekind (Ent) = E_Package then
14456 Restore_Nested_Formal (Ent);
14457 end if;
14458
14459 Next_Entity (Ent);
14460 end loop;
14461 end if;
14462 end Restore_Nested_Formal;
14463
14464 -- Start of processing for Restore_Private_Views
14465
14466 begin
14467 M := First_Elmt (Exchanged_Views);
14468 while Present (M) loop
14469 Typ := Node (M);
14470
14471 -- Subtypes of types whose views have been exchanged, and that are
14472 -- defined within the instance, were not on the Private_Dependents
14473 -- list on entry to the instance, so they have to be exchanged
14474 -- explicitly now, in order to remain consistent with the view of the
14475 -- parent type.
14476
14477 if Ekind_In (Typ, E_Private_Type,
14478 E_Limited_Private_Type,
14479 E_Record_Type_With_Private)
14480 then
14481 Dep_Elmt := First_Elmt (Private_Dependents (Typ));
14482 while Present (Dep_Elmt) loop
14483 Dep_Typ := Node (Dep_Elmt);
14484
14485 if Scope (Dep_Typ) = Pack_Id
14486 and then Present (Full_View (Dep_Typ))
14487 then
14488 Replace_Elmt (Dep_Elmt, Full_View (Dep_Typ));
14489 Exchange_Declarations (Dep_Typ);
14490 end if;
14491
14492 Next_Elmt (Dep_Elmt);
14493 end loop;
14494 end if;
14495
14496 Exchange_Declarations (Node (M));
14497 Next_Elmt (M);
14498 end loop;
14499
14500 if No (Pack_Id) then
14501 return;
14502 end if;
14503
14504 -- Make the generic formal parameters private, and make the formal types
14505 -- into subtypes of the actuals again.
14506
14507 E := First_Entity (Pack_Id);
14508 while Present (E) loop
14509 Set_Is_Hidden (E, True);
14510
14511 if Is_Type (E)
14512 and then Nkind (Parent (E)) = N_Subtype_Declaration
14513 then
14514 -- If the actual for E is itself a generic actual type from
14515 -- an enclosing instance, E is still a generic actual type
14516 -- outside of the current instance. This matter when resolving
14517 -- an overloaded call that may be ambiguous in the enclosing
14518 -- instance, when two of its actuals coincide.
14519
14520 if Is_Entity_Name (Subtype_Indication (Parent (E)))
14521 and then Is_Generic_Actual_Type
14522 (Entity (Subtype_Indication (Parent (E))))
14523 then
14524 null;
14525 else
14526 Set_Is_Generic_Actual_Type (E, False);
14527 end if;
14528
14529 -- An unusual case of aliasing: the actual may also be directly
14530 -- visible in the generic, and be private there, while it is fully
14531 -- visible in the context of the instance. The internal subtype
14532 -- is private in the instance but has full visibility like its
14533 -- parent in the enclosing scope. This enforces the invariant that
14534 -- the privacy status of all private dependents of a type coincide
14535 -- with that of the parent type. This can only happen when a
14536 -- generic child unit is instantiated within a sibling.
14537
14538 if Is_Private_Type (E)
14539 and then not Is_Private_Type (Etype (E))
14540 then
14541 Exchange_Declarations (E);
14542 end if;
14543
14544 elsif Ekind (E) = E_Package then
14545
14546 -- The end of the renaming list is the renaming of the generic
14547 -- package itself. If the instance is a subprogram, all entities
14548 -- in the corresponding package are renamings. If this entity is
14549 -- a formal package, make its own formals private as well. The
14550 -- actual in this case is itself the renaming of an instantiation.
14551 -- If the entity is not a package renaming, it is the entity
14552 -- created to validate formal package actuals: ignore it.
14553
14554 -- If the actual is itself a formal package for the enclosing
14555 -- generic, or the actual for such a formal package, it remains
14556 -- visible on exit from the instance, and therefore nothing needs
14557 -- to be done either, except to keep it accessible.
14558
14559 if Is_Package and then Renamed_Object (E) = Pack_Id then
14560 exit;
14561
14562 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
14563 null;
14564
14565 elsif
14566 Denotes_Formal_Package (Renamed_Object (E), True, Pack_Id)
14567 then
14568 Set_Is_Hidden (E, False);
14569
14570 else
14571 declare
14572 Act_P : constant Entity_Id := Renamed_Object (E);
14573 Id : Entity_Id;
14574
14575 begin
14576 Id := First_Entity (Act_P);
14577 while Present (Id)
14578 and then Id /= First_Private_Entity (Act_P)
14579 loop
14580 exit when Ekind (Id) = E_Package
14581 and then Renamed_Object (Id) = Act_P;
14582
14583 Set_Is_Hidden (Id, True);
14584 Set_Is_Potentially_Use_Visible (Id, In_Use (Act_P));
14585
14586 if Ekind (Id) = E_Package then
14587 Restore_Nested_Formal (Id);
14588 end if;
14589
14590 Next_Entity (Id);
14591 end loop;
14592 end;
14593 end if;
14594 end if;
14595
14596 Next_Entity (E);
14597 end loop;
14598 end Restore_Private_Views;
14599
14600 --------------
14601 -- Save_Env --
14602 --------------
14603
14604 procedure Save_Env
14605 (Gen_Unit : Entity_Id;
14606 Act_Unit : Entity_Id)
14607 is
14608 begin
14609 Init_Env;
14610 Set_Instance_Env (Gen_Unit, Act_Unit);
14611 end Save_Env;
14612
14613 ----------------------------
14614 -- Save_Global_References --
14615 ----------------------------
14616
14617 procedure Save_Global_References (Templ : Node_Id) is
14618
14619 -- ??? it is horrible to use global variables in highly recursive code
14620
14621 E : Entity_Id;
14622 -- The entity of the current associated node
14623
14624 Gen_Scope : Entity_Id;
14625 -- The scope of the generic for which references are being saved
14626
14627 N2 : Node_Id;
14628 -- The current associated node
14629
14630 function Is_Global (E : Entity_Id) return Boolean;
14631 -- Check whether entity is defined outside of generic unit. Examine the
14632 -- scope of an entity, and the scope of the scope, etc, until we find
14633 -- either Standard, in which case the entity is global, or the generic
14634 -- unit itself, which indicates that the entity is local. If the entity
14635 -- is the generic unit itself, as in the case of a recursive call, or
14636 -- the enclosing generic unit, if different from the current scope, then
14637 -- it is local as well, because it will be replaced at the point of
14638 -- instantiation. On the other hand, if it is a reference to a child
14639 -- unit of a common ancestor, which appears in an instantiation, it is
14640 -- global because it is used to denote a specific compilation unit at
14641 -- the time the instantiations will be analyzed.
14642
14643 procedure Qualify_Universal_Operands
14644 (Op : Node_Id;
14645 Func_Call : Node_Id);
14646 -- Op denotes a binary or unary operator in generic template Templ. Node
14647 -- Func_Call is the function call alternative of the operator within the
14648 -- the analyzed copy of the template. Change each operand which yields a
14649 -- universal type by wrapping it into a qualified expression
14650 --
14651 -- Actual_Typ'(Operand)
14652 --
14653 -- where Actual_Typ is the type of corresponding actual parameter of
14654 -- Operand in Func_Call.
14655
14656 procedure Reset_Entity (N : Node_Id);
14657 -- Save semantic information on global entity so that it is not resolved
14658 -- again at instantiation time.
14659
14660 procedure Save_Entity_Descendants (N : Node_Id);
14661 -- Apply Save_Global_References to the two syntactic descendants of
14662 -- non-terminal nodes that carry an Associated_Node and are processed
14663 -- through Reset_Entity. Once the global entity (if any) has been
14664 -- captured together with its type, only two syntactic descendants need
14665 -- to be traversed to complete the processing of the tree rooted at N.
14666 -- This applies to Selected_Components, Expanded_Names, and to Operator
14667 -- nodes. N can also be a character literal, identifier, or operator
14668 -- symbol node, but the call has no effect in these cases.
14669
14670 procedure Save_Global_Defaults (N1 : Node_Id; N2 : Node_Id);
14671 -- Default actuals in nested instances must be handled specially
14672 -- because there is no link to them from the original tree. When an
14673 -- actual subprogram is given by a default, we add an explicit generic
14674 -- association for it in the instantiation node. When we save the
14675 -- global references on the name of the instance, we recover the list
14676 -- of generic associations, and add an explicit one to the original
14677 -- generic tree, through which a global actual can be preserved.
14678 -- Similarly, if a child unit is instantiated within a sibling, in the
14679 -- context of the parent, we must preserve the identifier of the parent
14680 -- so that it can be properly resolved in a subsequent instantiation.
14681
14682 procedure Save_Global_Descendant (D : Union_Id);
14683 -- Apply Save_References recursively to the descendants of node D
14684
14685 procedure Save_References (N : Node_Id);
14686 -- This is the recursive procedure that does the work, once the
14687 -- enclosing generic scope has been established.
14688
14689 ---------------
14690 -- Is_Global --
14691 ---------------
14692
14693 function Is_Global (E : Entity_Id) return Boolean is
14694 Se : Entity_Id;
14695
14696 function Is_Instance_Node (Decl : Node_Id) return Boolean;
14697 -- Determine whether the parent node of a reference to a child unit
14698 -- denotes an instantiation or a formal package, in which case the
14699 -- reference to the child unit is global, even if it appears within
14700 -- the current scope (e.g. when the instance appears within the body
14701 -- of an ancestor).
14702
14703 ----------------------
14704 -- Is_Instance_Node --
14705 ----------------------
14706
14707 function Is_Instance_Node (Decl : Node_Id) return Boolean is
14708 begin
14709 return Nkind (Decl) in N_Generic_Instantiation
14710 or else
14711 Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration;
14712 end Is_Instance_Node;
14713
14714 -- Start of processing for Is_Global
14715
14716 begin
14717 if E = Gen_Scope then
14718 return False;
14719
14720 elsif E = Standard_Standard then
14721 return True;
14722
14723 elsif Is_Child_Unit (E)
14724 and then (Is_Instance_Node (Parent (N2))
14725 or else (Nkind (Parent (N2)) = N_Expanded_Name
14726 and then N2 = Selector_Name (Parent (N2))
14727 and then
14728 Is_Instance_Node (Parent (Parent (N2)))))
14729 then
14730 return True;
14731
14732 else
14733 Se := Scope (E);
14734 while Se /= Gen_Scope loop
14735 if Se = Standard_Standard then
14736 return True;
14737 else
14738 Se := Scope (Se);
14739 end if;
14740 end loop;
14741
14742 return False;
14743 end if;
14744 end Is_Global;
14745
14746 --------------------------------
14747 -- Qualify_Universal_Operands --
14748 --------------------------------
14749
14750 procedure Qualify_Universal_Operands
14751 (Op : Node_Id;
14752 Func_Call : Node_Id)
14753 is
14754 procedure Qualify_Operand (Opnd : Node_Id; Actual : Node_Id);
14755 -- Rewrite operand Opnd as a qualified expression of the form
14756 --
14757 -- Actual_Typ'(Opnd)
14758 --
14759 -- where Actual is the corresponding actual parameter of Opnd in
14760 -- function call Func_Call.
14761
14762 function Qualify_Type
14763 (Loc : Source_Ptr;
14764 Typ : Entity_Id) return Node_Id;
14765 -- Qualify type Typ by creating a selected component of the form
14766 --
14767 -- Scope_Of_Typ.Typ
14768
14769 ---------------------
14770 -- Qualify_Operand --
14771 ---------------------
14772
14773 procedure Qualify_Operand (Opnd : Node_Id; Actual : Node_Id) is
14774 Loc : constant Source_Ptr := Sloc (Opnd);
14775 Typ : constant Entity_Id := Etype (Actual);
14776 Mark : Node_Id;
14777 Qual : Node_Id;
14778
14779 begin
14780 -- Qualify the operand when it is of a universal type. Note that
14781 -- the template is unanalyzed and it is not possible to directly
14782 -- query the type. This transformation is not done when the type
14783 -- of the actual is internally generated because the type will be
14784 -- regenerated in the instance.
14785
14786 if Yields_Universal_Type (Opnd)
14787 and then Comes_From_Source (Typ)
14788 and then not Is_Hidden (Typ)
14789 then
14790 -- The type of the actual may be a global reference. Save this
14791 -- information by creating a reference to it.
14792
14793 if Is_Global (Typ) then
14794 Mark := New_Occurrence_Of (Typ, Loc);
14795
14796 -- Otherwise rely on resolution to find the proper type within
14797 -- the instance.
14798
14799 else
14800 Mark := Qualify_Type (Loc, Typ);
14801 end if;
14802
14803 Qual :=
14804 Make_Qualified_Expression (Loc,
14805 Subtype_Mark => Mark,
14806 Expression => Relocate_Node (Opnd));
14807
14808 -- Mark the qualification to distinguish it from other source
14809 -- constructs and signal the instantiation mechanism that this
14810 -- node requires special processing. See Copy_Generic_Node for
14811 -- details.
14812
14813 Set_Is_Qualified_Universal_Literal (Qual);
14814
14815 Rewrite (Opnd, Qual);
14816 end if;
14817 end Qualify_Operand;
14818
14819 ------------------
14820 -- Qualify_Type --
14821 ------------------
14822
14823 function Qualify_Type
14824 (Loc : Source_Ptr;
14825 Typ : Entity_Id) return Node_Id
14826 is
14827 Scop : constant Entity_Id := Scope (Typ);
14828 Result : Node_Id;
14829
14830 begin
14831 Result := Make_Identifier (Loc, Chars (Typ));
14832
14833 if Present (Scop) and then not Is_Generic_Unit (Scop) then
14834 Result :=
14835 Make_Selected_Component (Loc,
14836 Prefix => Make_Identifier (Loc, Chars (Scop)),
14837 Selector_Name => Result);
14838 end if;
14839
14840 return Result;
14841 end Qualify_Type;
14842
14843 -- Local variables
14844
14845 Actuals : constant List_Id := Parameter_Associations (Func_Call);
14846
14847 -- Start of processing for Qualify_Universal_Operands
14848
14849 begin
14850 if Nkind (Op) in N_Binary_Op then
14851 Qualify_Operand (Left_Opnd (Op), First (Actuals));
14852 Qualify_Operand (Right_Opnd (Op), Next (First (Actuals)));
14853
14854 elsif Nkind (Op) in N_Unary_Op then
14855 Qualify_Operand (Right_Opnd (Op), First (Actuals));
14856 end if;
14857 end Qualify_Universal_Operands;
14858
14859 ------------------
14860 -- Reset_Entity --
14861 ------------------
14862
14863 procedure Reset_Entity (N : Node_Id) is
14864 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id);
14865 -- If the type of N2 is global to the generic unit, save the type in
14866 -- the generic node. Just as we perform name capture for explicit
14867 -- references within the generic, we must capture the global types
14868 -- of local entities because they may participate in resolution in
14869 -- the instance.
14870
14871 function Top_Ancestor (E : Entity_Id) return Entity_Id;
14872 -- Find the ultimate ancestor of the current unit. If it is not a
14873 -- generic unit, then the name of the current unit in the prefix of
14874 -- an expanded name must be replaced with its generic homonym to
14875 -- ensure that it will be properly resolved in an instance.
14876
14877 ---------------------
14878 -- Set_Global_Type --
14879 ---------------------
14880
14881 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id) is
14882 Typ : constant Entity_Id := Etype (N2);
14883
14884 begin
14885 Set_Etype (N, Typ);
14886
14887 -- If the entity of N is not the associated node, this is a
14888 -- nested generic and it has an associated node as well, whose
14889 -- type is already the full view (see below). Indicate that the
14890 -- original node has a private view.
14891
14892 if Entity (N) /= N2 and then Has_Private_View (Entity (N)) then
14893 Set_Has_Private_View (N);
14894 end if;
14895
14896 -- If not a private type, nothing else to do
14897
14898 if not Is_Private_Type (Typ) then
14899 if Is_Array_Type (Typ)
14900 and then Is_Private_Type (Component_Type (Typ))
14901 then
14902 Set_Has_Private_View (N);
14903 end if;
14904
14905 -- If it is a derivation of a private type in a context where no
14906 -- full view is needed, nothing to do either.
14907
14908 elsif No (Full_View (Typ)) and then Typ /= Etype (Typ) then
14909 null;
14910
14911 -- Otherwise mark the type for flipping and use the full view when
14912 -- available.
14913
14914 else
14915 Set_Has_Private_View (N);
14916
14917 if Present (Full_View (Typ)) then
14918 Set_Etype (N2, Full_View (Typ));
14919 end if;
14920 end if;
14921
14922 if Is_Floating_Point_Type (Typ)
14923 and then Has_Dimension_System (Typ)
14924 then
14925 Copy_Dimensions (N2, N);
14926 end if;
14927 end Set_Global_Type;
14928
14929 ------------------
14930 -- Top_Ancestor --
14931 ------------------
14932
14933 function Top_Ancestor (E : Entity_Id) return Entity_Id is
14934 Par : Entity_Id;
14935
14936 begin
14937 Par := E;
14938 while Is_Child_Unit (Par) loop
14939 Par := Scope (Par);
14940 end loop;
14941
14942 return Par;
14943 end Top_Ancestor;
14944
14945 -- Start of processing for Reset_Entity
14946
14947 begin
14948 N2 := Get_Associated_Node (N);
14949 E := Entity (N2);
14950
14951 if Present (E) then
14952
14953 -- If the node is an entry call to an entry in an enclosing task,
14954 -- it is rewritten as a selected component. No global entity to
14955 -- preserve in this case, since the expansion will be redone in
14956 -- the instance.
14957
14958 if not Nkind_In (E, N_Defining_Character_Literal,
14959 N_Defining_Identifier,
14960 N_Defining_Operator_Symbol)
14961 then
14962 Set_Associated_Node (N, Empty);
14963 Set_Etype (N, Empty);
14964 return;
14965 end if;
14966
14967 -- If the entity is an itype created as a subtype of an access
14968 -- type with a null exclusion restore source entity for proper
14969 -- visibility. The itype will be created anew in the instance.
14970
14971 if Is_Itype (E)
14972 and then Ekind (E) = E_Access_Subtype
14973 and then Is_Entity_Name (N)
14974 and then Chars (Etype (E)) = Chars (N)
14975 then
14976 E := Etype (E);
14977 Set_Entity (N2, E);
14978 Set_Etype (N2, E);
14979 end if;
14980
14981 if Is_Global (E) then
14982
14983 -- If the entity is a package renaming that is the prefix of
14984 -- an expanded name, it has been rewritten as the renamed
14985 -- package, which is necessary semantically but complicates
14986 -- ASIS tree traversal, so we recover the original entity to
14987 -- expose the renaming. Take into account that the context may
14988 -- be a nested generic, that the original node may itself have
14989 -- an associated node that had better be an entity, and that
14990 -- the current node is still a selected component.
14991
14992 if Ekind (E) = E_Package
14993 and then Nkind (N) = N_Selected_Component
14994 and then Nkind (Parent (N)) = N_Expanded_Name
14995 and then Present (Original_Node (N2))
14996 and then Is_Entity_Name (Original_Node (N2))
14997 and then Present (Entity (Original_Node (N2)))
14998 then
14999 if Is_Global (Entity (Original_Node (N2))) then
15000 N2 := Original_Node (N2);
15001 Set_Associated_Node (N, N2);
15002 Set_Global_Type (N, N2);
15003
15004 -- Renaming is local, and will be resolved in instance
15005
15006 else
15007 Set_Associated_Node (N, Empty);
15008 Set_Etype (N, Empty);
15009 end if;
15010
15011 else
15012 Set_Global_Type (N, N2);
15013 end if;
15014
15015 elsif Nkind (N) = N_Op_Concat
15016 and then Is_Generic_Type (Etype (N2))
15017 and then (Base_Type (Etype (Right_Opnd (N2))) = Etype (N2)
15018 or else
15019 Base_Type (Etype (Left_Opnd (N2))) = Etype (N2))
15020 and then Is_Intrinsic_Subprogram (E)
15021 then
15022 null;
15023
15024 -- Entity is local. Mark generic node as unresolved. Note that now
15025 -- it does not have an entity.
15026
15027 else
15028 Set_Associated_Node (N, Empty);
15029 Set_Etype (N, Empty);
15030 end if;
15031
15032 if Nkind (Parent (N)) in N_Generic_Instantiation
15033 and then N = Name (Parent (N))
15034 then
15035 Save_Global_Defaults (Parent (N), Parent (N2));
15036 end if;
15037
15038 elsif Nkind (Parent (N)) = N_Selected_Component
15039 and then Nkind (Parent (N2)) = N_Expanded_Name
15040 then
15041 if Is_Global (Entity (Parent (N2))) then
15042 Change_Selected_Component_To_Expanded_Name (Parent (N));
15043 Set_Associated_Node (Parent (N), Parent (N2));
15044 Set_Global_Type (Parent (N), Parent (N2));
15045 Save_Entity_Descendants (N);
15046
15047 -- If this is a reference to the current generic entity, replace
15048 -- by the name of the generic homonym of the current package. This
15049 -- is because in an instantiation Par.P.Q will not resolve to the
15050 -- name of the instance, whose enclosing scope is not necessarily
15051 -- Par. We use the generic homonym rather that the name of the
15052 -- generic itself because it may be hidden by a local declaration.
15053
15054 elsif In_Open_Scopes (Entity (Parent (N2)))
15055 and then not
15056 Is_Generic_Unit (Top_Ancestor (Entity (Prefix (Parent (N2)))))
15057 then
15058 if Ekind (Entity (Parent (N2))) = E_Generic_Package then
15059 Rewrite (Parent (N),
15060 Make_Identifier (Sloc (N),
15061 Chars =>
15062 Chars (Generic_Homonym (Entity (Parent (N2))))));
15063 else
15064 Rewrite (Parent (N),
15065 Make_Identifier (Sloc (N),
15066 Chars => Chars (Selector_Name (Parent (N2)))));
15067 end if;
15068 end if;
15069
15070 if Nkind (Parent (Parent (N))) in N_Generic_Instantiation
15071 and then Parent (N) = Name (Parent (Parent (N)))
15072 then
15073 Save_Global_Defaults
15074 (Parent (Parent (N)), Parent (Parent (N2)));
15075 end if;
15076
15077 -- A selected component may denote a static constant that has been
15078 -- folded. If the static constant is global to the generic, capture
15079 -- its value. Otherwise the folding will happen in any instantiation.
15080
15081 elsif Nkind (Parent (N)) = N_Selected_Component
15082 and then Nkind_In (Parent (N2), N_Integer_Literal, N_Real_Literal)
15083 then
15084 if Present (Entity (Original_Node (Parent (N2))))
15085 and then Is_Global (Entity (Original_Node (Parent (N2))))
15086 then
15087 Rewrite (Parent (N), New_Copy (Parent (N2)));
15088 Set_Analyzed (Parent (N), False);
15089 end if;
15090
15091 -- A selected component may be transformed into a parameterless
15092 -- function call. If the called entity is global, rewrite the node
15093 -- appropriately, i.e. as an extended name for the global entity.
15094
15095 elsif Nkind (Parent (N)) = N_Selected_Component
15096 and then Nkind (Parent (N2)) = N_Function_Call
15097 and then N = Selector_Name (Parent (N))
15098 then
15099 if No (Parameter_Associations (Parent (N2))) then
15100 if Is_Global (Entity (Name (Parent (N2)))) then
15101 Change_Selected_Component_To_Expanded_Name (Parent (N));
15102 Set_Associated_Node (Parent (N), Name (Parent (N2)));
15103 Set_Global_Type (Parent (N), Name (Parent (N2)));
15104 Save_Entity_Descendants (N);
15105
15106 else
15107 Set_Is_Prefixed_Call (Parent (N));
15108 Set_Associated_Node (N, Empty);
15109 Set_Etype (N, Empty);
15110 end if;
15111
15112 -- In Ada 2005, X.F may be a call to a primitive operation,
15113 -- rewritten as F (X). This rewriting will be done again in an
15114 -- instance, so keep the original node. Global entities will be
15115 -- captured as for other constructs. Indicate that this must
15116 -- resolve as a call, to prevent accidental overloading in the
15117 -- instance, if both a component and a primitive operation appear
15118 -- as candidates.
15119
15120 else
15121 Set_Is_Prefixed_Call (Parent (N));
15122 end if;
15123
15124 -- Entity is local. Reset in generic unit, so that node is resolved
15125 -- anew at the point of instantiation.
15126
15127 else
15128 Set_Associated_Node (N, Empty);
15129 Set_Etype (N, Empty);
15130 end if;
15131 end Reset_Entity;
15132
15133 -----------------------------
15134 -- Save_Entity_Descendants --
15135 -----------------------------
15136
15137 procedure Save_Entity_Descendants (N : Node_Id) is
15138 begin
15139 case Nkind (N) is
15140 when N_Binary_Op =>
15141 Save_Global_Descendant (Union_Id (Left_Opnd (N)));
15142 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
15143
15144 when N_Unary_Op =>
15145 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
15146
15147 when N_Expanded_Name
15148 | N_Selected_Component
15149 =>
15150 Save_Global_Descendant (Union_Id (Prefix (N)));
15151 Save_Global_Descendant (Union_Id (Selector_Name (N)));
15152
15153 when N_Character_Literal
15154 | N_Identifier
15155 | N_Operator_Symbol
15156 =>
15157 null;
15158
15159 when others =>
15160 raise Program_Error;
15161 end case;
15162 end Save_Entity_Descendants;
15163
15164 --------------------------
15165 -- Save_Global_Defaults --
15166 --------------------------
15167
15168 procedure Save_Global_Defaults (N1 : Node_Id; N2 : Node_Id) is
15169 Loc : constant Source_Ptr := Sloc (N1);
15170 Assoc2 : constant List_Id := Generic_Associations (N2);
15171 Gen_Id : constant Entity_Id := Get_Generic_Entity (N2);
15172 Assoc1 : List_Id;
15173 Act1 : Node_Id;
15174 Act2 : Node_Id;
15175 Def : Node_Id;
15176 Ndec : Node_Id;
15177 Subp : Entity_Id;
15178 Actual : Entity_Id;
15179
15180 begin
15181 Assoc1 := Generic_Associations (N1);
15182
15183 if Present (Assoc1) then
15184 Act1 := First (Assoc1);
15185 else
15186 Act1 := Empty;
15187 Set_Generic_Associations (N1, New_List);
15188 Assoc1 := Generic_Associations (N1);
15189 end if;
15190
15191 if Present (Assoc2) then
15192 Act2 := First (Assoc2);
15193 else
15194 return;
15195 end if;
15196
15197 while Present (Act1) and then Present (Act2) loop
15198 Next (Act1);
15199 Next (Act2);
15200 end loop;
15201
15202 -- Find the associations added for default subprograms
15203
15204 if Present (Act2) then
15205 while Nkind (Act2) /= N_Generic_Association
15206 or else No (Entity (Selector_Name (Act2)))
15207 or else not Is_Overloadable (Entity (Selector_Name (Act2)))
15208 loop
15209 Next (Act2);
15210 end loop;
15211
15212 -- Add a similar association if the default is global. The
15213 -- renaming declaration for the actual has been analyzed, and
15214 -- its alias is the program it renames. Link the actual in the
15215 -- original generic tree with the node in the analyzed tree.
15216
15217 while Present (Act2) loop
15218 Subp := Entity (Selector_Name (Act2));
15219 Def := Explicit_Generic_Actual_Parameter (Act2);
15220
15221 -- Following test is defence against rubbish errors
15222
15223 if No (Alias (Subp)) then
15224 return;
15225 end if;
15226
15227 -- Retrieve the resolved actual from the renaming declaration
15228 -- created for the instantiated formal.
15229
15230 Actual := Entity (Name (Parent (Parent (Subp))));
15231 Set_Entity (Def, Actual);
15232 Set_Etype (Def, Etype (Actual));
15233
15234 if Is_Global (Actual) then
15235 Ndec :=
15236 Make_Generic_Association (Loc,
15237 Selector_Name =>
15238 New_Occurrence_Of (Subp, Loc),
15239 Explicit_Generic_Actual_Parameter =>
15240 New_Occurrence_Of (Actual, Loc));
15241
15242 Set_Associated_Node
15243 (Explicit_Generic_Actual_Parameter (Ndec), Def);
15244
15245 Append (Ndec, Assoc1);
15246
15247 -- If there are other defaults, add a dummy association in case
15248 -- there are other defaulted formals with the same name.
15249
15250 elsif Present (Next (Act2)) then
15251 Ndec :=
15252 Make_Generic_Association (Loc,
15253 Selector_Name =>
15254 New_Occurrence_Of (Subp, Loc),
15255 Explicit_Generic_Actual_Parameter => Empty);
15256
15257 Append (Ndec, Assoc1);
15258 end if;
15259
15260 Next (Act2);
15261 end loop;
15262 end if;
15263
15264 if Nkind (Name (N1)) = N_Identifier
15265 and then Is_Child_Unit (Gen_Id)
15266 and then Is_Global (Gen_Id)
15267 and then Is_Generic_Unit (Scope (Gen_Id))
15268 and then In_Open_Scopes (Scope (Gen_Id))
15269 then
15270 -- This is an instantiation of a child unit within a sibling, so
15271 -- that the generic parent is in scope. An eventual instance must
15272 -- occur within the scope of an instance of the parent. Make name
15273 -- in instance into an expanded name, to preserve the identifier
15274 -- of the parent, so it can be resolved subsequently.
15275
15276 Rewrite (Name (N2),
15277 Make_Expanded_Name (Loc,
15278 Chars => Chars (Gen_Id),
15279 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
15280 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
15281 Set_Entity (Name (N2), Gen_Id);
15282
15283 Rewrite (Name (N1),
15284 Make_Expanded_Name (Loc,
15285 Chars => Chars (Gen_Id),
15286 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
15287 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
15288
15289 Set_Associated_Node (Name (N1), Name (N2));
15290 Set_Associated_Node (Prefix (Name (N1)), Empty);
15291 Set_Associated_Node
15292 (Selector_Name (Name (N1)), Selector_Name (Name (N2)));
15293 Set_Etype (Name (N1), Etype (Gen_Id));
15294 end if;
15295 end Save_Global_Defaults;
15296
15297 ----------------------------
15298 -- Save_Global_Descendant --
15299 ----------------------------
15300
15301 procedure Save_Global_Descendant (D : Union_Id) is
15302 N1 : Node_Id;
15303
15304 begin
15305 if D in Node_Range then
15306 if D = Union_Id (Empty) then
15307 null;
15308
15309 elsif Nkind (Node_Id (D)) /= N_Compilation_Unit then
15310 Save_References (Node_Id (D));
15311 end if;
15312
15313 elsif D in List_Range then
15314 pragma Assert (D /= Union_Id (No_List));
15315 -- Because No_List = Empty, which is in Node_Range above
15316
15317 if Is_Empty_List (List_Id (D)) then
15318 null;
15319
15320 else
15321 N1 := First (List_Id (D));
15322 while Present (N1) loop
15323 Save_References (N1);
15324 Next (N1);
15325 end loop;
15326 end if;
15327
15328 -- Element list or other non-node field, nothing to do
15329
15330 else
15331 null;
15332 end if;
15333 end Save_Global_Descendant;
15334
15335 ---------------------
15336 -- Save_References --
15337 ---------------------
15338
15339 -- This is the recursive procedure that does the work once the enclosing
15340 -- generic scope has been established. We have to treat specially a
15341 -- number of node rewritings that are required by semantic processing
15342 -- and which change the kind of nodes in the generic copy: typically
15343 -- constant-folding, replacing an operator node by a string literal, or
15344 -- a selected component by an expanded name. In each of those cases, the
15345 -- transformation is propagated to the generic unit.
15346
15347 procedure Save_References (N : Node_Id) is
15348 Loc : constant Source_Ptr := Sloc (N);
15349
15350 function Requires_Delayed_Save (Nod : Node_Id) return Boolean;
15351 -- Determine whether arbitrary node Nod requires delayed capture of
15352 -- global references within its aspect specifications.
15353
15354 procedure Save_References_In_Aggregate (N : Node_Id);
15355 -- Save all global references in [extension] aggregate node N
15356
15357 procedure Save_References_In_Char_Lit_Or_Op_Symbol (N : Node_Id);
15358 -- Save all global references in a character literal or operator
15359 -- symbol denoted by N.
15360
15361 procedure Save_References_In_Descendants (N : Node_Id);
15362 -- Save all global references in all descendants of node N
15363
15364 procedure Save_References_In_Identifier (N : Node_Id);
15365 -- Save all global references in identifier node N
15366
15367 procedure Save_References_In_Operator (N : Node_Id);
15368 -- Save all global references in operator node N
15369
15370 procedure Save_References_In_Pragma (Prag : Node_Id);
15371 -- Save all global references found within the expression of pragma
15372 -- Prag.
15373
15374 ---------------------------
15375 -- Requires_Delayed_Save --
15376 ---------------------------
15377
15378 function Requires_Delayed_Save (Nod : Node_Id) return Boolean is
15379 begin
15380 -- Generic packages and subprograms require delayed capture of
15381 -- global references within their aspects due to the timing of
15382 -- annotation analysis.
15383
15384 if Nkind_In (Nod, N_Generic_Package_Declaration,
15385 N_Generic_Subprogram_Declaration,
15386 N_Package_Body,
15387 N_Package_Body_Stub,
15388 N_Subprogram_Body,
15389 N_Subprogram_Body_Stub)
15390 then
15391 -- Since the capture of global references is done on the
15392 -- unanalyzed generic template, there is no information around
15393 -- to infer the context. Use the Associated_Entity linkages to
15394 -- peek into the analyzed generic copy and determine what the
15395 -- template corresponds to.
15396
15397 if Nod = Templ then
15398 return
15399 Is_Generic_Declaration_Or_Body
15400 (Unit_Declaration_Node
15401 (Associated_Entity (Defining_Entity (Nod))));
15402
15403 -- Otherwise the generic unit being processed is not the top
15404 -- level template. It is safe to capture of global references
15405 -- within the generic unit because at this point the top level
15406 -- copy is fully analyzed.
15407
15408 else
15409 return False;
15410 end if;
15411
15412 -- Otherwise capture the global references without interference
15413
15414 else
15415 return False;
15416 end if;
15417 end Requires_Delayed_Save;
15418
15419 ----------------------------------
15420 -- Save_References_In_Aggregate --
15421 ----------------------------------
15422
15423 procedure Save_References_In_Aggregate (N : Node_Id) is
15424 Nam : Node_Id;
15425 Qual : Node_Id := Empty;
15426 Typ : Entity_Id := Empty;
15427
15428 use Atree.Unchecked_Access;
15429 -- This code section is part of implementing an untyped tree
15430 -- traversal, so it needs direct access to node fields.
15431
15432 begin
15433 N2 := Get_Associated_Node (N);
15434
15435 if Present (N2) then
15436 Typ := Etype (N2);
15437
15438 -- In an instance within a generic, use the name of the actual
15439 -- and not the original generic parameter. If the actual is
15440 -- global in the current generic it must be preserved for its
15441 -- instantiation.
15442
15443 if Nkind (Parent (Typ)) = N_Subtype_Declaration
15444 and then Present (Generic_Parent_Type (Parent (Typ)))
15445 then
15446 Typ := Base_Type (Typ);
15447 Set_Etype (N2, Typ);
15448 end if;
15449 end if;
15450
15451 if No (N2) or else No (Typ) or else not Is_Global (Typ) then
15452 Set_Associated_Node (N, Empty);
15453
15454 -- If the aggregate is an actual in a call, it has been
15455 -- resolved in the current context, to some local type. The
15456 -- enclosing call may have been disambiguated by the aggregate,
15457 -- and this disambiguation might fail at instantiation time
15458 -- because the type to which the aggregate did resolve is not
15459 -- preserved. In order to preserve some of this information,
15460 -- wrap the aggregate in a qualified expression, using the id
15461 -- of its type. For further disambiguation we qualify the type
15462 -- name with its scope (if visible and not hidden by a local
15463 -- homograph) because both id's will have corresponding
15464 -- entities in an instance. This resolves most of the problems
15465 -- with missing type information on aggregates in instances.
15466
15467 if Present (N2)
15468 and then Nkind (N2) = Nkind (N)
15469 and then Nkind (Parent (N2)) in N_Subprogram_Call
15470 and then Present (Typ)
15471 and then Comes_From_Source (Typ)
15472 then
15473 Nam := Make_Identifier (Loc, Chars (Typ));
15474
15475 if Is_Immediately_Visible (Scope (Typ))
15476 and then
15477 (not In_Open_Scopes (Scope (Typ))
15478 or else Current_Entity (Scope (Typ)) = Scope (Typ))
15479 then
15480 Nam :=
15481 Make_Selected_Component (Loc,
15482 Prefix =>
15483 Make_Identifier (Loc, Chars (Scope (Typ))),
15484 Selector_Name => Nam);
15485 end if;
15486
15487 Qual :=
15488 Make_Qualified_Expression (Loc,
15489 Subtype_Mark => Nam,
15490 Expression => Relocate_Node (N));
15491 end if;
15492 end if;
15493
15494 Save_Global_Descendant (Field1 (N));
15495 Save_Global_Descendant (Field2 (N));
15496 Save_Global_Descendant (Field3 (N));
15497 Save_Global_Descendant (Field5 (N));
15498
15499 if Present (Qual) then
15500 Rewrite (N, Qual);
15501 end if;
15502 end Save_References_In_Aggregate;
15503
15504 ----------------------------------------------
15505 -- Save_References_In_Char_Lit_Or_Op_Symbol --
15506 ----------------------------------------------
15507
15508 procedure Save_References_In_Char_Lit_Or_Op_Symbol (N : Node_Id) is
15509 begin
15510 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
15511 Reset_Entity (N);
15512
15513 elsif Nkind (N) = N_Operator_Symbol
15514 and then Nkind (Get_Associated_Node (N)) = N_String_Literal
15515 then
15516 Change_Operator_Symbol_To_String_Literal (N);
15517 end if;
15518 end Save_References_In_Char_Lit_Or_Op_Symbol;
15519
15520 ------------------------------------
15521 -- Save_References_In_Descendants --
15522 ------------------------------------
15523
15524 procedure Save_References_In_Descendants (N : Node_Id) is
15525 use Atree.Unchecked_Access;
15526 -- This code section is part of implementing an untyped tree
15527 -- traversal, so it needs direct access to node fields.
15528
15529 begin
15530 Save_Global_Descendant (Field1 (N));
15531 Save_Global_Descendant (Field2 (N));
15532 Save_Global_Descendant (Field3 (N));
15533 Save_Global_Descendant (Field4 (N));
15534 Save_Global_Descendant (Field5 (N));
15535 end Save_References_In_Descendants;
15536
15537 -----------------------------------
15538 -- Save_References_In_Identifier --
15539 -----------------------------------
15540
15541 procedure Save_References_In_Identifier (N : Node_Id) is
15542 begin
15543 -- The node did not undergo a transformation
15544
15545 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
15546 declare
15547 Aux_N2 : constant Node_Id := Get_Associated_Node (N);
15548 Orig_N2_Parent : constant Node_Id :=
15549 Original_Node (Parent (Aux_N2));
15550 begin
15551 -- The parent of this identifier is a selected component
15552 -- which denotes a named number that was constant folded.
15553 -- Preserve the original name for ASIS and link the parent
15554 -- with its expanded name. The constant folding will be
15555 -- repeated in the instance.
15556
15557 if Nkind (Parent (N)) = N_Selected_Component
15558 and then Nkind_In (Parent (Aux_N2), N_Integer_Literal,
15559 N_Real_Literal)
15560 and then Is_Entity_Name (Orig_N2_Parent)
15561 and then Ekind (Entity (Orig_N2_Parent)) in Named_Kind
15562 and then Is_Global (Entity (Orig_N2_Parent))
15563 then
15564 N2 := Aux_N2;
15565 Set_Associated_Node
15566 (Parent (N), Original_Node (Parent (N2)));
15567
15568 -- Common case
15569
15570 else
15571 -- If this is a discriminant reference, always save it.
15572 -- It is used in the instance to find the corresponding
15573 -- discriminant positionally rather than by name.
15574
15575 Set_Original_Discriminant
15576 (N, Original_Discriminant (Get_Associated_Node (N)));
15577 end if;
15578
15579 Reset_Entity (N);
15580 end;
15581
15582 -- The analysis of the generic copy transformed the identifier
15583 -- into another construct. Propagate the changes to the template.
15584
15585 else
15586 N2 := Get_Associated_Node (N);
15587
15588 -- The identifier denotes a call to a parameterless function.
15589 -- Mark the node as resolved when the function is external.
15590
15591 if Nkind (N2) = N_Function_Call then
15592 E := Entity (Name (N2));
15593
15594 if Present (E) and then Is_Global (E) then
15595 Set_Etype (N, Etype (N2));
15596 else
15597 Set_Associated_Node (N, Empty);
15598 Set_Etype (N, Empty);
15599 end if;
15600
15601 -- The identifier denotes a named number that was constant
15602 -- folded. Preserve the original name for ASIS and undo the
15603 -- constant folding which will be repeated in the instance.
15604
15605 elsif Nkind_In (N2, N_Integer_Literal, N_Real_Literal)
15606 and then Is_Entity_Name (Original_Node (N2))
15607 then
15608 Set_Associated_Node (N, Original_Node (N2));
15609 Reset_Entity (N);
15610
15611 -- The identifier resolved to a string literal. Propagate this
15612 -- information to the generic template.
15613
15614 elsif Nkind (N2) = N_String_Literal then
15615 Rewrite (N, New_Copy (N2));
15616
15617 -- The identifier is rewritten as a dereference if it is the
15618 -- prefix of an implicit dereference. Preserve the original
15619 -- tree as the analysis of the instance will expand the node
15620 -- again, but preserve the resolved entity if it is global.
15621
15622 elsif Nkind (N2) = N_Explicit_Dereference then
15623 if Is_Entity_Name (Prefix (N2))
15624 and then Present (Entity (Prefix (N2)))
15625 and then Is_Global (Entity (Prefix (N2)))
15626 then
15627 Set_Associated_Node (N, Prefix (N2));
15628
15629 elsif Nkind (Prefix (N2)) = N_Function_Call
15630 and then Present (Entity (Name (Prefix (N2))))
15631 and then Is_Global (Entity (Name (Prefix (N2))))
15632 then
15633 Rewrite (N,
15634 Make_Explicit_Dereference (Loc,
15635 Prefix =>
15636 Make_Function_Call (Loc,
15637 Name =>
15638 New_Occurrence_Of
15639 (Entity (Name (Prefix (N2))), Loc))));
15640
15641 else
15642 Set_Associated_Node (N, Empty);
15643 Set_Etype (N, Empty);
15644 end if;
15645
15646 -- The subtype mark of a nominally unconstrained object is
15647 -- rewritten as a subtype indication using the bounds of the
15648 -- expression. Recover the original subtype mark.
15649
15650 elsif Nkind (N2) = N_Subtype_Indication
15651 and then Is_Entity_Name (Original_Node (N2))
15652 then
15653 Set_Associated_Node (N, Original_Node (N2));
15654 Reset_Entity (N);
15655 end if;
15656 end if;
15657 end Save_References_In_Identifier;
15658
15659 ---------------------------------
15660 -- Save_References_In_Operator --
15661 ---------------------------------
15662
15663 procedure Save_References_In_Operator (N : Node_Id) is
15664 begin
15665 -- The node did not undergo a transformation
15666
15667 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
15668 if Nkind (N) = N_Op_Concat then
15669 Set_Is_Component_Left_Opnd (N,
15670 Is_Component_Left_Opnd (Get_Associated_Node (N)));
15671
15672 Set_Is_Component_Right_Opnd (N,
15673 Is_Component_Right_Opnd (Get_Associated_Node (N)));
15674 end if;
15675
15676 Reset_Entity (N);
15677
15678 -- The analysis of the generic copy transformed the operator into
15679 -- some other construct. Propagate the changes to the template if
15680 -- applicable.
15681
15682 else
15683 N2 := Get_Associated_Node (N);
15684
15685 -- The operator resoved to a function call
15686
15687 if Nkind (N2) = N_Function_Call then
15688
15689 -- Add explicit qualifications in the generic template for
15690 -- all operands of universal type. This aids resolution by
15691 -- preserving the actual type of a literal or an attribute
15692 -- that yields a universal result.
15693
15694 Qualify_Universal_Operands (N, N2);
15695
15696 E := Entity (Name (N2));
15697
15698 if Present (E) and then Is_Global (E) then
15699 Set_Etype (N, Etype (N2));
15700 else
15701 Set_Associated_Node (N, Empty);
15702 Set_Etype (N, Empty);
15703 end if;
15704
15705 -- The operator was folded into a literal
15706
15707 elsif Nkind_In (N2, N_Integer_Literal,
15708 N_Real_Literal,
15709 N_String_Literal)
15710 then
15711 if Present (Original_Node (N2))
15712 and then Nkind (Original_Node (N2)) = Nkind (N)
15713 then
15714 -- Operation was constant-folded. Whenever possible,
15715 -- recover semantic information from unfolded node,
15716 -- for ASIS use.
15717
15718 Set_Associated_Node (N, Original_Node (N2));
15719
15720 if Nkind (N) = N_Op_Concat then
15721 Set_Is_Component_Left_Opnd (N,
15722 Is_Component_Left_Opnd (Get_Associated_Node (N)));
15723 Set_Is_Component_Right_Opnd (N,
15724 Is_Component_Right_Opnd (Get_Associated_Node (N)));
15725 end if;
15726
15727 Reset_Entity (N);
15728
15729 -- Propagate the constant folding back to the template
15730
15731 else
15732 Rewrite (N, New_Copy (N2));
15733 Set_Analyzed (N, False);
15734 end if;
15735
15736 -- The operator was folded into an enumeration literal. Retain
15737 -- the entity to avoid spurious ambiguities if it is overloaded
15738 -- at the point of instantiation or inlining.
15739
15740 elsif Nkind (N2) = N_Identifier
15741 and then Ekind (Entity (N2)) = E_Enumeration_Literal
15742 then
15743 Rewrite (N, New_Copy (N2));
15744 Set_Analyzed (N, False);
15745 end if;
15746 end if;
15747
15748 -- Complete the operands check if node has not been constant
15749 -- folded.
15750
15751 if Nkind (N) in N_Op then
15752 Save_Entity_Descendants (N);
15753 end if;
15754 end Save_References_In_Operator;
15755
15756 -------------------------------
15757 -- Save_References_In_Pragma --
15758 -------------------------------
15759
15760 procedure Save_References_In_Pragma (Prag : Node_Id) is
15761 Context : Node_Id;
15762 Do_Save : Boolean := True;
15763
15764 use Atree.Unchecked_Access;
15765 -- This code section is part of implementing an untyped tree
15766 -- traversal, so it needs direct access to node fields.
15767
15768 begin
15769 -- Do not save global references in pragmas generated from aspects
15770 -- because the pragmas will be regenerated at instantiation time.
15771
15772 if From_Aspect_Specification (Prag) then
15773 Do_Save := False;
15774
15775 -- The capture of global references within contract-related source
15776 -- pragmas associated with generic packages, subprograms or their
15777 -- respective bodies must be delayed due to timing of annotation
15778 -- analysis. Global references are still captured in routine
15779 -- Save_Global_References_In_Contract.
15780
15781 elsif Is_Generic_Contract_Pragma (Prag) and then Prag /= Templ then
15782 if Is_Package_Contract_Annotation (Prag) then
15783 Context := Find_Related_Package_Or_Body (Prag);
15784 else
15785 pragma Assert (Is_Subprogram_Contract_Annotation (Prag));
15786 Context := Find_Related_Declaration_Or_Body (Prag);
15787 end if;
15788
15789 -- The use of Original_Node accounts for the case when the
15790 -- related context is generic template.
15791
15792 if Requires_Delayed_Save (Original_Node (Context)) then
15793 Do_Save := False;
15794 end if;
15795 end if;
15796
15797 -- For all other cases, save all global references within the
15798 -- descendants, but skip the following semantic fields:
15799
15800 -- Field1 - Next_Pragma
15801 -- Field3 - Corresponding_Aspect
15802 -- Field5 - Next_Rep_Item
15803
15804 if Do_Save then
15805 Save_Global_Descendant (Field2 (Prag));
15806 Save_Global_Descendant (Field4 (Prag));
15807 end if;
15808 end Save_References_In_Pragma;
15809
15810 -- Start of processing for Save_References
15811
15812 begin
15813 if N = Empty then
15814 null;
15815
15816 -- Aggregates
15817
15818 elsif Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
15819 Save_References_In_Aggregate (N);
15820
15821 -- Character literals, operator symbols
15822
15823 elsif Nkind_In (N, N_Character_Literal, N_Operator_Symbol) then
15824 Save_References_In_Char_Lit_Or_Op_Symbol (N);
15825
15826 -- Defining identifiers
15827
15828 elsif Nkind (N) in N_Entity then
15829 null;
15830
15831 -- Identifiers
15832
15833 elsif Nkind (N) = N_Identifier then
15834 Save_References_In_Identifier (N);
15835
15836 -- Operators
15837
15838 elsif Nkind (N) in N_Op then
15839 Save_References_In_Operator (N);
15840
15841 -- Pragmas
15842
15843 elsif Nkind (N) = N_Pragma then
15844 Save_References_In_Pragma (N);
15845
15846 else
15847 Save_References_In_Descendants (N);
15848 end if;
15849
15850 -- Save all global references found within the aspect specifications
15851 -- of the related node.
15852
15853 if Permits_Aspect_Specifications (N) and then Has_Aspects (N) then
15854
15855 -- The capture of global references within aspects associated with
15856 -- generic packages, subprograms or their bodies must be delayed
15857 -- due to timing of annotation analysis. Global references are
15858 -- still captured in routine Save_Global_References_In_Contract.
15859
15860 if Requires_Delayed_Save (N) then
15861 null;
15862
15863 -- Otherwise save all global references within the aspects
15864
15865 else
15866 Save_Global_References_In_Aspects (N);
15867 end if;
15868 end if;
15869 end Save_References;
15870
15871 -- Start of processing for Save_Global_References
15872
15873 begin
15874 Gen_Scope := Current_Scope;
15875
15876 -- If the generic unit is a child unit, references to entities in the
15877 -- parent are treated as local, because they will be resolved anew in
15878 -- the context of the instance of the parent.
15879
15880 while Is_Child_Unit (Gen_Scope)
15881 and then Ekind (Scope (Gen_Scope)) = E_Generic_Package
15882 loop
15883 Gen_Scope := Scope (Gen_Scope);
15884 end loop;
15885
15886 Save_References (Templ);
15887 end Save_Global_References;
15888
15889 ---------------------------------------
15890 -- Save_Global_References_In_Aspects --
15891 ---------------------------------------
15892
15893 procedure Save_Global_References_In_Aspects (N : Node_Id) is
15894 Asp : Node_Id;
15895 Expr : Node_Id;
15896
15897 begin
15898 Asp := First (Aspect_Specifications (N));
15899 while Present (Asp) loop
15900 Expr := Expression (Asp);
15901
15902 if Present (Expr) then
15903 Save_Global_References (Expr);
15904 end if;
15905
15906 Next (Asp);
15907 end loop;
15908 end Save_Global_References_In_Aspects;
15909
15910 ------------------------------------------
15911 -- Set_Copied_Sloc_For_Inherited_Pragma --
15912 ------------------------------------------
15913
15914 procedure Set_Copied_Sloc_For_Inherited_Pragma
15915 (N : Node_Id;
15916 E : Entity_Id)
15917 is
15918 begin
15919 Create_Instantiation_Source (N, E,
15920 Inlined_Body => False,
15921 Inherited_Pragma => True,
15922 Factor => S_Adjustment);
15923 end Set_Copied_Sloc_For_Inherited_Pragma;
15924
15925 --------------------------------------
15926 -- Set_Copied_Sloc_For_Inlined_Body --
15927 --------------------------------------
15928
15929 procedure Set_Copied_Sloc_For_Inlined_Body (N : Node_Id; E : Entity_Id) is
15930 begin
15931 Create_Instantiation_Source (N, E,
15932 Inlined_Body => True,
15933 Inherited_Pragma => False,
15934 Factor => S_Adjustment);
15935 end Set_Copied_Sloc_For_Inlined_Body;
15936
15937 ---------------------
15938 -- Set_Instance_Of --
15939 ---------------------
15940
15941 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id) is
15942 begin
15943 Generic_Renamings.Table (Generic_Renamings.Last) := (A, B, Assoc_Null);
15944 Generic_Renamings_HTable.Set (Generic_Renamings.Last);
15945 Generic_Renamings.Increment_Last;
15946 end Set_Instance_Of;
15947
15948 --------------------
15949 -- Set_Next_Assoc --
15950 --------------------
15951
15952 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr) is
15953 begin
15954 Generic_Renamings.Table (E).Next_In_HTable := Next;
15955 end Set_Next_Assoc;
15956
15957 -------------------
15958 -- Start_Generic --
15959 -------------------
15960
15961 procedure Start_Generic is
15962 begin
15963 -- ??? More things could be factored out in this routine.
15964 -- Should probably be done at a later stage.
15965
15966 Generic_Flags.Append (Inside_A_Generic);
15967 Inside_A_Generic := True;
15968
15969 Expander_Mode_Save_And_Set (False);
15970 end Start_Generic;
15971
15972 ----------------------
15973 -- Set_Instance_Env --
15974 ----------------------
15975
15976 -- WARNING: This routine manages SPARK regions
15977
15978 procedure Set_Instance_Env
15979 (Gen_Unit : Entity_Id;
15980 Act_Unit : Entity_Id)
15981 is
15982 Saved_AE : constant Boolean := Assertions_Enabled;
15983 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
15984 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
15985 -- Save the SPARK mode-related data because utilizing the configuration
15986 -- values of pragmas and switches will eliminate any previously set
15987 -- SPARK_Mode.
15988
15989 begin
15990 -- Regardless of the current mode, predefined units are analyzed in the
15991 -- most current Ada mode, and earlier version Ada checks do not apply
15992 -- to predefined units. Nothing needs to be done for non-internal units.
15993 -- These are always analyzed in the current mode.
15994
15995 if In_Internal_Unit (Gen_Unit) then
15996 Set_Opt_Config_Switches (True, Current_Sem_Unit = Main_Unit);
15997
15998 -- In Ada2012 we may want to enable assertions in an instance of a
15999 -- predefined unit, in which case we need to preserve the current
16000 -- setting for the Assertions_Enabled flag. This will become more
16001 -- critical when pre/postconditions are added to predefined units,
16002 -- as is already the case for some numeric libraries.
16003
16004 if Ada_Version >= Ada_2012 then
16005 Assertions_Enabled := Saved_AE;
16006 end if;
16007
16008 -- Reinstall the SPARK_Mode which was in effect at the point of
16009 -- instantiation.
16010
16011 Install_SPARK_Mode (Saved_SM, Saved_SMP);
16012 end if;
16013
16014 Current_Instantiated_Parent :=
16015 (Gen_Id => Gen_Unit,
16016 Act_Id => Act_Unit,
16017 Next_In_HTable => Assoc_Null);
16018 end Set_Instance_Env;
16019
16020 -----------------
16021 -- Switch_View --
16022 -----------------
16023
16024 procedure Switch_View (T : Entity_Id) is
16025 BT : constant Entity_Id := Base_Type (T);
16026 Priv_Elmt : Elmt_Id := No_Elmt;
16027 Priv_Sub : Entity_Id;
16028
16029 begin
16030 -- T may be private but its base type may have been exchanged through
16031 -- some other occurrence, in which case there is nothing to switch
16032 -- besides T itself. Note that a private dependent subtype of a private
16033 -- type might not have been switched even if the base type has been,
16034 -- because of the last branch of Check_Private_View (see comment there).
16035
16036 if not Is_Private_Type (BT) then
16037 Prepend_Elmt (Full_View (T), Exchanged_Views);
16038 Exchange_Declarations (T);
16039 return;
16040 end if;
16041
16042 Priv_Elmt := First_Elmt (Private_Dependents (BT));
16043
16044 if Present (Full_View (BT)) then
16045 Prepend_Elmt (Full_View (BT), Exchanged_Views);
16046 Exchange_Declarations (BT);
16047 end if;
16048
16049 while Present (Priv_Elmt) loop
16050 Priv_Sub := (Node (Priv_Elmt));
16051
16052 -- We avoid flipping the subtype if the Etype of its full view is
16053 -- private because this would result in a malformed subtype. This
16054 -- occurs when the Etype of the subtype full view is the full view of
16055 -- the base type (and since the base types were just switched, the
16056 -- subtype is pointing to the wrong view). This is currently the case
16057 -- for tagged record types, access types (maybe more?) and needs to
16058 -- be resolved. ???
16059
16060 if Present (Full_View (Priv_Sub))
16061 and then not Is_Private_Type (Etype (Full_View (Priv_Sub)))
16062 then
16063 Prepend_Elmt (Full_View (Priv_Sub), Exchanged_Views);
16064 Exchange_Declarations (Priv_Sub);
16065 end if;
16066
16067 Next_Elmt (Priv_Elmt);
16068 end loop;
16069 end Switch_View;
16070
16071 -----------------
16072 -- True_Parent --
16073 -----------------
16074
16075 function True_Parent (N : Node_Id) return Node_Id is
16076 begin
16077 if Nkind (Parent (N)) = N_Subunit then
16078 return Parent (Corresponding_Stub (Parent (N)));
16079 else
16080 return Parent (N);
16081 end if;
16082 end True_Parent;
16083
16084 -----------------------------
16085 -- Valid_Default_Attribute --
16086 -----------------------------
16087
16088 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id) is
16089 Attr_Id : constant Attribute_Id :=
16090 Get_Attribute_Id (Attribute_Name (Def));
16091 T : constant Entity_Id := Entity (Prefix (Def));
16092 Is_Fun : constant Boolean := (Ekind (Nam) = E_Function);
16093 F : Entity_Id;
16094 Num_F : Nat;
16095 OK : Boolean;
16096
16097 begin
16098 if No (T) or else T = Any_Id then
16099 return;
16100 end if;
16101
16102 Num_F := 0;
16103 F := First_Formal (Nam);
16104 while Present (F) loop
16105 Num_F := Num_F + 1;
16106 Next_Formal (F);
16107 end loop;
16108
16109 case Attr_Id is
16110 when Attribute_Adjacent
16111 | Attribute_Ceiling
16112 | Attribute_Copy_Sign
16113 | Attribute_Floor
16114 | Attribute_Fraction
16115 | Attribute_Machine
16116 | Attribute_Model
16117 | Attribute_Remainder
16118 | Attribute_Rounding
16119 | Attribute_Unbiased_Rounding
16120 =>
16121 OK := Is_Fun
16122 and then Num_F = 1
16123 and then Is_Floating_Point_Type (T);
16124
16125 when Attribute_Image
16126 | Attribute_Pred
16127 | Attribute_Succ
16128 | Attribute_Value
16129 | Attribute_Wide_Image
16130 | Attribute_Wide_Value
16131 =>
16132 OK := Is_Fun and then Num_F = 1 and then Is_Scalar_Type (T);
16133
16134 when Attribute_Max
16135 | Attribute_Min
16136 =>
16137 OK := Is_Fun and then Num_F = 2 and then Is_Scalar_Type (T);
16138
16139 when Attribute_Input =>
16140 OK := (Is_Fun and then Num_F = 1);
16141
16142 when Attribute_Output
16143 | Attribute_Read
16144 | Attribute_Write
16145 =>
16146 OK := not Is_Fun and then Num_F = 2;
16147
16148 when others =>
16149 OK := False;
16150 end case;
16151
16152 if not OK then
16153 Error_Msg_N
16154 ("attribute reference has wrong profile for subprogram", Def);
16155 end if;
16156 end Valid_Default_Attribute;
16157
16158 end Sem_Ch12;