[Ada] Fix fallout of cleanup to Has_Private_View mechanism
[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-2020, 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 Circularity_Detected : Boolean := False;
244 -- It should really be reset upon encountering a new main unit, but in
245 -- practice we do not use multiple main units so this is not critical.
246
247 -----------------------------------------
248 -- Implementation of Generic Contracts --
249 -----------------------------------------
250
251 -- A "contract" is a collection of aspects and pragmas that either verify a
252 -- property of a construct at runtime or classify the data flow to and from
253 -- the construct in some fashion.
254
255 -- Generic packages, subprograms and their respective bodies may be subject
256 -- to the following contract-related aspects or pragmas collectively known
257 -- as annotations:
258
259 -- package subprogram [body]
260 -- Abstract_State Contract_Cases
261 -- Initial_Condition Depends
262 -- Initializes Extensions_Visible
263 -- Global
264 -- package body Post
265 -- Refined_State Post_Class
266 -- Postcondition
267 -- Pre
268 -- Pre_Class
269 -- Precondition
270 -- Refined_Depends
271 -- Refined_Global
272 -- Refined_Post
273 -- Test_Case
274
275 -- Most package contract annotations utilize forward references to classify
276 -- data declared within the package [body]. Subprogram annotations then use
277 -- the classifications to further refine them. These inter dependencies are
278 -- problematic with respect to the implementation of generics because their
279 -- analysis, capture of global references and instantiation does not mesh
280 -- well with the existing mechanism.
281
282 -- 1) Analysis of generic contracts is carried out the same way non-generic
283 -- contracts are analyzed:
284
285 -- 1.1) General rule - a contract is analyzed after all related aspects
286 -- and pragmas are analyzed. This is done by routines
287
288 -- Analyze_Package_Body_Contract
289 -- Analyze_Package_Contract
290 -- Analyze_Subprogram_Body_Contract
291 -- Analyze_Subprogram_Contract
292
293 -- 1.2) Compilation unit - the contract is analyzed after Pragmas_After
294 -- are processed.
295
296 -- 1.3) Compilation unit body - the contract is analyzed at the end of
297 -- the body declaration list.
298
299 -- 1.4) Package - the contract is analyzed at the end of the private or
300 -- visible declarations, prior to analyzing the contracts of any nested
301 -- packages or subprograms.
302
303 -- 1.5) Package body - the contract is analyzed at the end of the body
304 -- declaration list, prior to analyzing the contracts of any nested
305 -- packages or subprograms.
306
307 -- 1.6) Subprogram - if the subprogram is declared inside a block, a
308 -- package or a subprogram, then its contract is analyzed at the end of
309 -- the enclosing declarations, otherwise the subprogram is a compilation
310 -- unit 1.2).
311
312 -- 1.7) Subprogram body - if the subprogram body is declared inside a
313 -- block, a package body or a subprogram body, then its contract is
314 -- analyzed at the end of the enclosing declarations, otherwise the
315 -- subprogram is a compilation unit 1.3).
316
317 -- 2) Capture of global references within contracts is done after capturing
318 -- global references within the generic template. There are two reasons for
319 -- this delay - pragma annotations are not part of the generic template in
320 -- the case of a generic subprogram declaration, and analysis of contracts
321 -- is delayed.
322
323 -- Contract-related source pragmas within generic templates are prepared
324 -- for delayed capture of global references by routine
325
326 -- Create_Generic_Contract
327
328 -- The routine associates these pragmas with the contract of the template.
329 -- In the case of a generic subprogram declaration, the routine creates
330 -- generic templates for the pragmas declared after the subprogram because
331 -- they are not part of the template.
332
333 -- generic -- template starts
334 -- procedure Gen_Proc (Input : Integer); -- template ends
335 -- pragma Precondition (Input > 0); -- requires own template
336
337 -- 2.1) The capture of global references with aspect specifications and
338 -- source pragmas that apply to a generic unit must be suppressed when
339 -- the generic template is being processed because the contracts have not
340 -- been analyzed yet. Any attempts to capture global references at that
341 -- point will destroy the Associated_Node linkages and leave the template
342 -- undecorated. This delay is controlled by routine
343
344 -- Requires_Delayed_Save
345
346 -- 2.2) The real capture of global references within a contract is done
347 -- after the contract has been analyzed, by routine
348
349 -- Save_Global_References_In_Contract
350
351 -- 3) The instantiation of a generic contract occurs as part of the
352 -- instantiation of the contract owner. Generic subprogram declarations
353 -- require additional processing when the contract is specified by pragmas
354 -- because the pragmas are not part of the generic template. This is done
355 -- by routine
356
357 -- Instantiate_Subprogram_Contract
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 formals into this local package. The result is a
384 -- 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 formals. 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 -- Adjust_Inherited_Pragma_Sloc --
1030 ----------------------------------
1031
1032 procedure Adjust_Inherited_Pragma_Sloc (N : Node_Id) is
1033 begin
1034 Adjust_Instantiation_Sloc (N, S_Adjustment);
1035 end Adjust_Inherited_Pragma_Sloc;
1036
1037 --------------------------
1038 -- Analyze_Associations --
1039 --------------------------
1040
1041 function Analyze_Associations
1042 (I_Node : Node_Id;
1043 Formals : List_Id;
1044 F_Copy : List_Id) return List_Id
1045 is
1046 Actuals_To_Freeze : constant Elist_Id := New_Elmt_List;
1047 Assoc_List : constant List_Id := New_List;
1048 Default_Actuals : constant List_Id := New_List;
1049 Gen_Unit : constant Entity_Id :=
1050 Defining_Entity (Parent (F_Copy));
1051
1052 Actuals : List_Id;
1053 Actual : Node_Id;
1054 Analyzed_Formal : Node_Id;
1055 First_Named : Node_Id := Empty;
1056 Formal : Node_Id;
1057 Match : Node_Id;
1058 Named : Node_Id;
1059 Saved_Formal : Node_Id;
1060
1061 Default_Formals : constant List_Id := New_List;
1062 -- If an Others_Choice is present, some of the formals may be defaulted.
1063 -- To simplify the treatment of visibility in an instance, we introduce
1064 -- individual defaults for each such formal. These defaults are
1065 -- appended to the list of associations and replace the Others_Choice.
1066
1067 Found_Assoc : Node_Id;
1068 -- Association for the current formal being match. Empty if there are
1069 -- no remaining actuals, or if there is no named association with the
1070 -- name of the formal.
1071
1072 Is_Named_Assoc : Boolean;
1073 Num_Matched : Nat := 0;
1074 Num_Actuals : Nat := 0;
1075
1076 Others_Present : Boolean := False;
1077 Others_Choice : Node_Id := Empty;
1078 -- In Ada 2005, indicates partial parameterization of a formal
1079 -- package. As usual an other association must be last in the list.
1080
1081 procedure Check_Fixed_Point_Actual (Actual : Node_Id);
1082 -- Warn if an actual fixed-point type has user-defined arithmetic
1083 -- operations, but there is no corresponding formal in the generic,
1084 -- in which case the predefined operations will be used. This merits
1085 -- a warning because of the special semantics of fixed point ops.
1086
1087 procedure Check_Overloaded_Formal_Subprogram (Formal : Entity_Id);
1088 -- Apply RM 12.3(9): if a formal subprogram is overloaded, the instance
1089 -- cannot have a named association for it. AI05-0025 extends this rule
1090 -- to formals of formal packages by AI05-0025, and it also applies to
1091 -- box-initialized formals.
1092
1093 function Has_Fully_Defined_Profile (Subp : Entity_Id) return Boolean;
1094 -- Determine whether the parameter types and the return type of Subp
1095 -- are fully defined at the point of instantiation.
1096
1097 function Matching_Actual
1098 (F : Entity_Id;
1099 A_F : Entity_Id) return Node_Id;
1100 -- Find actual that corresponds to a given a formal parameter. If the
1101 -- actuals are positional, return the next one, if any. If the actuals
1102 -- are named, scan the parameter associations to find the right one.
1103 -- A_F is the corresponding entity in the analyzed generic, which is
1104 -- placed on the selector name.
1105 --
1106 -- In Ada 2005, a named association may be given with a box, in which
1107 -- case Matching_Actual sets Found_Assoc to the generic association,
1108 -- but return Empty for the actual itself. In this case the code below
1109 -- creates a corresponding declaration for the formal.
1110
1111 function Partial_Parameterization return Boolean;
1112 -- Ada 2005: if no match is found for a given formal, check if the
1113 -- association for it includes a box, or whether the associations
1114 -- include an Others clause.
1115
1116 procedure Process_Default (F : Entity_Id);
1117 -- Add a copy of the declaration of generic formal F to the list of
1118 -- associations, and add an explicit box association for F if there
1119 -- is none yet, and the default comes from an Others_Choice.
1120
1121 function Renames_Standard_Subprogram (Subp : Entity_Id) return Boolean;
1122 -- Determine whether Subp renames one of the subprograms defined in the
1123 -- generated package Standard.
1124
1125 procedure Set_Analyzed_Formal;
1126 -- Find the node in the generic copy that corresponds to a given formal.
1127 -- The semantic information on this node is used to perform legality
1128 -- checks on the actuals. Because semantic analysis can introduce some
1129 -- anonymous entities or modify the declaration node itself, the
1130 -- correspondence between the two lists is not one-one. In addition to
1131 -- anonymous types, the presence a formal equality will introduce an
1132 -- implicit declaration for the corresponding inequality.
1133
1134 ----------------------------------------
1135 -- Check_Overloaded_Formal_Subprogram --
1136 ----------------------------------------
1137
1138 procedure Check_Overloaded_Formal_Subprogram (Formal : Entity_Id) is
1139 Temp_Formal : Entity_Id;
1140
1141 begin
1142 Temp_Formal := First (Formals);
1143 while Present (Temp_Formal) loop
1144 if Nkind (Temp_Formal) in N_Formal_Subprogram_Declaration
1145 and then Temp_Formal /= Formal
1146 and then
1147 Chars (Defining_Unit_Name (Specification (Formal))) =
1148 Chars (Defining_Unit_Name (Specification (Temp_Formal)))
1149 then
1150 if Present (Found_Assoc) then
1151 Error_Msg_N
1152 ("named association not allowed for overloaded formal",
1153 Found_Assoc);
1154
1155 else
1156 Error_Msg_N
1157 ("named association not allowed for overloaded formal",
1158 Others_Choice);
1159 end if;
1160
1161 Abandon_Instantiation (Instantiation_Node);
1162 end if;
1163
1164 Next (Temp_Formal);
1165 end loop;
1166 end Check_Overloaded_Formal_Subprogram;
1167
1168 -------------------------------
1169 -- Check_Fixed_Point_Actual --
1170 -------------------------------
1171
1172 procedure Check_Fixed_Point_Actual (Actual : Node_Id) is
1173 Typ : constant Entity_Id := Entity (Actual);
1174 Prims : constant Elist_Id := Collect_Primitive_Operations (Typ);
1175 Elem : Elmt_Id;
1176 Formal : Node_Id;
1177 Op : Entity_Id;
1178
1179 begin
1180 -- Locate primitive operations of the type that are arithmetic
1181 -- operations.
1182
1183 Elem := First_Elmt (Prims);
1184 while Present (Elem) loop
1185 if Nkind (Node (Elem)) = N_Defining_Operator_Symbol then
1186
1187 -- Check whether the generic unit has a formal subprogram of
1188 -- the same name. This does not check types but is good enough
1189 -- to justify a warning.
1190
1191 Formal := First_Non_Pragma (Formals);
1192 Op := Alias (Node (Elem));
1193
1194 while Present (Formal) loop
1195 if Nkind (Formal) = N_Formal_Concrete_Subprogram_Declaration
1196 and then Chars (Defining_Entity (Formal)) =
1197 Chars (Node (Elem))
1198 then
1199 exit;
1200
1201 elsif Nkind (Formal) = N_Formal_Package_Declaration then
1202 declare
1203 Assoc : Node_Id;
1204 Ent : Entity_Id;
1205
1206 begin
1207 -- Locate corresponding actual, and check whether it
1208 -- includes a fixed-point type.
1209
1210 Assoc := First (Assoc_List);
1211 while Present (Assoc) loop
1212 exit when
1213 Nkind (Assoc) = N_Package_Renaming_Declaration
1214 and then Chars (Defining_Unit_Name (Assoc)) =
1215 Chars (Defining_Identifier (Formal));
1216
1217 Next (Assoc);
1218 end loop;
1219
1220 if Present (Assoc) then
1221
1222 -- If formal package declares a fixed-point type,
1223 -- and the user-defined operator is derived from
1224 -- a generic instance package, the fixed-point type
1225 -- does not use the corresponding predefined op.
1226
1227 Ent := First_Entity (Entity (Name (Assoc)));
1228 while Present (Ent) loop
1229 if Is_Fixed_Point_Type (Ent)
1230 and then Present (Op)
1231 and then Is_Generic_Instance (Scope (Op))
1232 then
1233 return;
1234 end if;
1235
1236 Next_Entity (Ent);
1237 end loop;
1238 end if;
1239 end;
1240 end if;
1241
1242 Next (Formal);
1243 end loop;
1244
1245 if No (Formal) then
1246 Error_Msg_Sloc := Sloc (Node (Elem));
1247 Error_Msg_NE
1248 ("?instance uses predefined operation, not primitive "
1249 & "operation&#", Actual, Node (Elem));
1250 end if;
1251 end if;
1252
1253 Next_Elmt (Elem);
1254 end loop;
1255 end Check_Fixed_Point_Actual;
1256
1257 -------------------------------
1258 -- Has_Fully_Defined_Profile --
1259 -------------------------------
1260
1261 function Has_Fully_Defined_Profile (Subp : Entity_Id) return Boolean is
1262 function Is_Fully_Defined_Type (Typ : Entity_Id) return Boolean;
1263 -- Determine whethet type Typ is fully defined
1264
1265 ---------------------------
1266 -- Is_Fully_Defined_Type --
1267 ---------------------------
1268
1269 function Is_Fully_Defined_Type (Typ : Entity_Id) return Boolean is
1270 begin
1271 -- A private type without a full view is not fully defined
1272
1273 if Is_Private_Type (Typ)
1274 and then No (Full_View (Typ))
1275 then
1276 return False;
1277
1278 -- An incomplete type is never fully defined
1279
1280 elsif Is_Incomplete_Type (Typ) then
1281 return False;
1282
1283 -- All other types are fully defined
1284
1285 else
1286 return True;
1287 end if;
1288 end Is_Fully_Defined_Type;
1289
1290 -- Local declarations
1291
1292 Param : Entity_Id;
1293
1294 -- Start of processing for Has_Fully_Defined_Profile
1295
1296 begin
1297 -- Check the parameters
1298
1299 Param := First_Formal (Subp);
1300 while Present (Param) loop
1301 if not Is_Fully_Defined_Type (Etype (Param)) then
1302 return False;
1303 end if;
1304
1305 Next_Formal (Param);
1306 end loop;
1307
1308 -- Check the return type
1309
1310 return Is_Fully_Defined_Type (Etype (Subp));
1311 end Has_Fully_Defined_Profile;
1312
1313 ---------------------
1314 -- Matching_Actual --
1315 ---------------------
1316
1317 function Matching_Actual
1318 (F : Entity_Id;
1319 A_F : Entity_Id) return Node_Id
1320 is
1321 Prev : Node_Id;
1322 Act : Node_Id;
1323
1324 begin
1325 Is_Named_Assoc := False;
1326
1327 -- End of list of purely positional parameters
1328
1329 if No (Actual) or else Nkind (Actual) = N_Others_Choice then
1330 Found_Assoc := Empty;
1331 Act := Empty;
1332
1333 -- Case of positional parameter corresponding to current formal
1334
1335 elsif No (Selector_Name (Actual)) then
1336 Found_Assoc := Actual;
1337 Act := Explicit_Generic_Actual_Parameter (Actual);
1338 Num_Matched := Num_Matched + 1;
1339 Next (Actual);
1340
1341 -- Otherwise scan list of named actuals to find the one with the
1342 -- desired name. All remaining actuals have explicit names.
1343
1344 else
1345 Is_Named_Assoc := True;
1346 Found_Assoc := Empty;
1347 Act := Empty;
1348 Prev := Empty;
1349
1350 while Present (Actual) loop
1351 if Nkind (Actual) = N_Others_Choice then
1352 Found_Assoc := Empty;
1353 Act := Empty;
1354
1355 elsif Chars (Selector_Name (Actual)) = Chars (F) then
1356 Set_Entity (Selector_Name (Actual), A_F);
1357 Set_Etype (Selector_Name (Actual), Etype (A_F));
1358 Generate_Reference (A_F, Selector_Name (Actual));
1359
1360 Found_Assoc := Actual;
1361 Act := Explicit_Generic_Actual_Parameter (Actual);
1362 Num_Matched := Num_Matched + 1;
1363 exit;
1364 end if;
1365
1366 Prev := Actual;
1367 Next (Actual);
1368 end loop;
1369
1370 -- Reset for subsequent searches. In most cases the named
1371 -- associations are in order. If they are not, we reorder them
1372 -- to avoid scanning twice the same actual. This is not just a
1373 -- question of efficiency: there may be multiple defaults with
1374 -- boxes that have the same name. In a nested instantiation we
1375 -- insert actuals for those defaults, and cannot rely on their
1376 -- names to disambiguate them.
1377
1378 if Actual = First_Named then
1379 Next (First_Named);
1380
1381 elsif Present (Actual) then
1382 Insert_Before (First_Named, Remove_Next (Prev));
1383 end if;
1384
1385 Actual := First_Named;
1386 end if;
1387
1388 if Is_Entity_Name (Act) and then Present (Entity (Act)) then
1389 Set_Used_As_Generic_Actual (Entity (Act));
1390 end if;
1391
1392 return Act;
1393 end Matching_Actual;
1394
1395 ------------------------------
1396 -- Partial_Parameterization --
1397 ------------------------------
1398
1399 function Partial_Parameterization return Boolean is
1400 begin
1401 return Others_Present
1402 or else (Present (Found_Assoc) and then Box_Present (Found_Assoc));
1403 end Partial_Parameterization;
1404
1405 ---------------------
1406 -- Process_Default --
1407 ---------------------
1408
1409 procedure Process_Default (F : Entity_Id) is
1410 Loc : constant Source_Ptr := Sloc (I_Node);
1411 F_Id : constant Entity_Id := Defining_Entity (F);
1412 Decl : Node_Id;
1413 Default : Node_Id;
1414 Id : Entity_Id;
1415
1416 begin
1417 -- Append copy of formal declaration to associations, and create new
1418 -- defining identifier for it.
1419
1420 Decl := New_Copy_Tree (F);
1421 Id := Make_Defining_Identifier (Sloc (F_Id), Chars (F_Id));
1422
1423 if Nkind (F) in N_Formal_Subprogram_Declaration then
1424 Set_Defining_Unit_Name (Specification (Decl), Id);
1425
1426 else
1427 Set_Defining_Identifier (Decl, Id);
1428 end if;
1429
1430 Append (Decl, Assoc_List);
1431
1432 if No (Found_Assoc) then
1433 Default :=
1434 Make_Generic_Association (Loc,
1435 Selector_Name =>
1436 New_Occurrence_Of (Id, Loc),
1437 Explicit_Generic_Actual_Parameter => Empty);
1438 Set_Box_Present (Default);
1439 Append (Default, Default_Formals);
1440 end if;
1441 end Process_Default;
1442
1443 ---------------------------------
1444 -- Renames_Standard_Subprogram --
1445 ---------------------------------
1446
1447 function Renames_Standard_Subprogram (Subp : Entity_Id) return Boolean is
1448 Id : Entity_Id;
1449
1450 begin
1451 Id := Alias (Subp);
1452 while Present (Id) loop
1453 if Scope (Id) = Standard_Standard then
1454 return True;
1455 end if;
1456
1457 Id := Alias (Id);
1458 end loop;
1459
1460 return False;
1461 end Renames_Standard_Subprogram;
1462
1463 -------------------------
1464 -- Set_Analyzed_Formal --
1465 -------------------------
1466
1467 procedure Set_Analyzed_Formal is
1468 Kind : Node_Kind;
1469
1470 begin
1471 while Present (Analyzed_Formal) loop
1472 Kind := Nkind (Analyzed_Formal);
1473
1474 case Nkind (Formal) is
1475 when N_Formal_Subprogram_Declaration =>
1476 exit when Kind in N_Formal_Subprogram_Declaration
1477 and then
1478 Chars
1479 (Defining_Unit_Name (Specification (Formal))) =
1480 Chars
1481 (Defining_Unit_Name (Specification (Analyzed_Formal)));
1482
1483 when N_Formal_Package_Declaration =>
1484 exit when Nkind_In (Kind, N_Formal_Package_Declaration,
1485 N_Generic_Package_Declaration,
1486 N_Package_Declaration);
1487
1488 when N_Use_Package_Clause
1489 | N_Use_Type_Clause
1490 =>
1491 exit;
1492
1493 when others =>
1494
1495 -- Skip freeze nodes, and nodes inserted to replace
1496 -- unrecognized pragmas.
1497
1498 exit when
1499 Kind not in N_Formal_Subprogram_Declaration
1500 and then not Nkind_In (Kind, N_Subprogram_Declaration,
1501 N_Freeze_Entity,
1502 N_Null_Statement,
1503 N_Itype_Reference)
1504 and then Chars (Defining_Identifier (Formal)) =
1505 Chars (Defining_Identifier (Analyzed_Formal));
1506 end case;
1507
1508 Next (Analyzed_Formal);
1509 end loop;
1510 end Set_Analyzed_Formal;
1511
1512 -- Start of processing for Analyze_Associations
1513
1514 begin
1515 Actuals := Generic_Associations (I_Node);
1516
1517 if Present (Actuals) then
1518
1519 -- Check for an Others choice, indicating a partial parameterization
1520 -- for a formal package.
1521
1522 Actual := First (Actuals);
1523 while Present (Actual) loop
1524 if Nkind (Actual) = N_Others_Choice then
1525 Others_Present := True;
1526 Others_Choice := Actual;
1527
1528 if Present (Next (Actual)) then
1529 Error_Msg_N ("others must be last association", Actual);
1530 end if;
1531
1532 -- This subprogram is used both for formal packages and for
1533 -- instantiations. For the latter, associations must all be
1534 -- explicit.
1535
1536 if Nkind (I_Node) /= N_Formal_Package_Declaration
1537 and then Comes_From_Source (I_Node)
1538 then
1539 Error_Msg_N
1540 ("others association not allowed in an instance",
1541 Actual);
1542 end if;
1543
1544 -- In any case, nothing to do after the others association
1545
1546 exit;
1547
1548 elsif Box_Present (Actual)
1549 and then Comes_From_Source (I_Node)
1550 and then Nkind (I_Node) /= N_Formal_Package_Declaration
1551 then
1552 Error_Msg_N
1553 ("box association not allowed in an instance", Actual);
1554 end if;
1555
1556 Next (Actual);
1557 end loop;
1558
1559 -- If named associations are present, save first named association
1560 -- (it may of course be Empty) to facilitate subsequent name search.
1561
1562 First_Named := First (Actuals);
1563 while Present (First_Named)
1564 and then Nkind (First_Named) /= N_Others_Choice
1565 and then No (Selector_Name (First_Named))
1566 loop
1567 Num_Actuals := Num_Actuals + 1;
1568 Next (First_Named);
1569 end loop;
1570 end if;
1571
1572 Named := First_Named;
1573 while Present (Named) loop
1574 if Nkind (Named) /= N_Others_Choice
1575 and then No (Selector_Name (Named))
1576 then
1577 Error_Msg_N ("invalid positional actual after named one", Named);
1578 Abandon_Instantiation (Named);
1579 end if;
1580
1581 -- A named association may lack an actual parameter, if it was
1582 -- introduced for a default subprogram that turns out to be local
1583 -- to the outer instantiation. If it has a box association it must
1584 -- correspond to some formal in the generic.
1585
1586 if Nkind (Named) /= N_Others_Choice
1587 and then (Present (Explicit_Generic_Actual_Parameter (Named))
1588 or else Box_Present (Named))
1589 then
1590 Num_Actuals := Num_Actuals + 1;
1591 end if;
1592
1593 Next (Named);
1594 end loop;
1595
1596 if Present (Formals) then
1597 Formal := First_Non_Pragma (Formals);
1598 Analyzed_Formal := First_Non_Pragma (F_Copy);
1599
1600 if Present (Actuals) then
1601 Actual := First (Actuals);
1602
1603 -- All formals should have default values
1604
1605 else
1606 Actual := Empty;
1607 end if;
1608
1609 while Present (Formal) loop
1610 Set_Analyzed_Formal;
1611 Saved_Formal := Next_Non_Pragma (Formal);
1612
1613 case Nkind (Formal) is
1614 when N_Formal_Object_Declaration =>
1615 Match :=
1616 Matching_Actual
1617 (Defining_Identifier (Formal),
1618 Defining_Identifier (Analyzed_Formal));
1619
1620 if No (Match) and then Partial_Parameterization then
1621 Process_Default (Formal);
1622
1623 else
1624 Append_List
1625 (Instantiate_Object (Formal, Match, Analyzed_Formal),
1626 Assoc_List);
1627
1628 -- For a defaulted in_parameter, create an entry in the
1629 -- the list of defaulted actuals, for GNATProve use. Do
1630 -- not included these defaults for an instance nested
1631 -- within a generic, because the defaults are also used
1632 -- in the analysis of the enclosing generic, and only
1633 -- defaulted subprograms are relevant there.
1634
1635 if No (Match) and then not Inside_A_Generic then
1636 Append_To (Default_Actuals,
1637 Make_Generic_Association (Sloc (I_Node),
1638 Selector_Name =>
1639 New_Occurrence_Of
1640 (Defining_Identifier (Formal), Sloc (I_Node)),
1641 Explicit_Generic_Actual_Parameter =>
1642 New_Copy_Tree (Default_Expression (Formal))));
1643 end if;
1644 end if;
1645
1646 -- If the object is a call to an expression function, this
1647 -- is a freezing point for it.
1648
1649 if Is_Entity_Name (Match)
1650 and then Present (Entity (Match))
1651 and then Nkind
1652 (Original_Node (Unit_Declaration_Node (Entity (Match))))
1653 = N_Expression_Function
1654 then
1655 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1656 end if;
1657
1658 when N_Formal_Type_Declaration =>
1659 Match :=
1660 Matching_Actual
1661 (Defining_Identifier (Formal),
1662 Defining_Identifier (Analyzed_Formal));
1663
1664 if No (Match) then
1665 if Partial_Parameterization then
1666 Process_Default (Formal);
1667
1668 else
1669 Error_Msg_Sloc := Sloc (Gen_Unit);
1670 Error_Msg_NE
1671 ("missing actual&",
1672 Instantiation_Node, Defining_Identifier (Formal));
1673 Error_Msg_NE
1674 ("\in instantiation of & declared#",
1675 Instantiation_Node, Gen_Unit);
1676 Abandon_Instantiation (Instantiation_Node);
1677 end if;
1678
1679 else
1680 Analyze (Match);
1681 Append_List
1682 (Instantiate_Type
1683 (Formal, Match, Analyzed_Formal, Assoc_List),
1684 Assoc_List);
1685
1686 -- Warn when an actual is a fixed-point with user-
1687 -- defined promitives. The warning is superfluous
1688 -- if the fornal is private, because there can be
1689 -- no arithmetic operations in the generic so there
1690 -- no danger of confusion.
1691
1692 if Is_Fixed_Point_Type (Entity (Match))
1693 and then not Is_Private_Type
1694 (Defining_Identifier (Analyzed_Formal))
1695 then
1696 Check_Fixed_Point_Actual (Match);
1697 end if;
1698
1699 -- An instantiation is a freeze point for the actuals,
1700 -- unless this is a rewritten formal package, or the
1701 -- formal is an Ada 2012 formal incomplete type.
1702
1703 if Nkind (I_Node) = N_Formal_Package_Declaration
1704 or else
1705 (Ada_Version >= Ada_2012
1706 and then
1707 Ekind (Defining_Identifier (Analyzed_Formal)) =
1708 E_Incomplete_Type)
1709 then
1710 null;
1711
1712 else
1713 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1714 end if;
1715 end if;
1716
1717 -- A remote access-to-class-wide type is not a legal actual
1718 -- for a generic formal of an access type (E.2.2(17/2)).
1719 -- In GNAT an exception to this rule is introduced when
1720 -- the formal is marked as remote using implementation
1721 -- defined aspect/pragma Remote_Access_Type. In that case
1722 -- the actual must be remote as well.
1723
1724 -- If the current instantiation is the construction of a
1725 -- local copy for a formal package the actuals may be
1726 -- defaulted, and there is no matching actual to check.
1727
1728 if Nkind (Analyzed_Formal) = N_Formal_Type_Declaration
1729 and then
1730 Nkind (Formal_Type_Definition (Analyzed_Formal)) =
1731 N_Access_To_Object_Definition
1732 and then Present (Match)
1733 then
1734 declare
1735 Formal_Ent : constant Entity_Id :=
1736 Defining_Identifier (Analyzed_Formal);
1737 begin
1738 if Is_Remote_Access_To_Class_Wide_Type (Entity (Match))
1739 = Is_Remote_Types (Formal_Ent)
1740 then
1741 -- Remoteness of formal and actual match
1742
1743 null;
1744
1745 elsif Is_Remote_Types (Formal_Ent) then
1746
1747 -- Remote formal, non-remote actual
1748
1749 Error_Msg_NE
1750 ("actual for& must be remote", Match, Formal_Ent);
1751
1752 else
1753 -- Non-remote formal, remote actual
1754
1755 Error_Msg_NE
1756 ("actual for& may not be remote",
1757 Match, Formal_Ent);
1758 end if;
1759 end;
1760 end if;
1761
1762 when N_Formal_Subprogram_Declaration =>
1763 Match :=
1764 Matching_Actual
1765 (Defining_Unit_Name (Specification (Formal)),
1766 Defining_Unit_Name (Specification (Analyzed_Formal)));
1767
1768 -- If the formal subprogram has the same name as another
1769 -- formal subprogram of the generic, then a named
1770 -- association is illegal (12.3(9)). Exclude named
1771 -- associations that are generated for a nested instance.
1772
1773 if Present (Match)
1774 and then Is_Named_Assoc
1775 and then Comes_From_Source (Found_Assoc)
1776 then
1777 Check_Overloaded_Formal_Subprogram (Formal);
1778 end if;
1779
1780 -- If there is no corresponding actual, this may be case
1781 -- of partial parameterization, or else the formal has a
1782 -- default or a box.
1783
1784 if No (Match) and then Partial_Parameterization then
1785 Process_Default (Formal);
1786
1787 if Nkind (I_Node) = N_Formal_Package_Declaration then
1788 Check_Overloaded_Formal_Subprogram (Formal);
1789 end if;
1790
1791 else
1792 Append_To (Assoc_List,
1793 Instantiate_Formal_Subprogram
1794 (Formal, Match, Analyzed_Formal));
1795
1796 -- An instantiation is a freeze point for the actuals,
1797 -- unless this is a rewritten formal package.
1798
1799 if Nkind (I_Node) /= N_Formal_Package_Declaration
1800 and then Nkind (Match) = N_Identifier
1801 and then Is_Subprogram (Entity (Match))
1802
1803 -- The actual subprogram may rename a routine defined
1804 -- in Standard. Avoid freezing such renamings because
1805 -- subprograms coming from Standard cannot be frozen.
1806
1807 and then
1808 not Renames_Standard_Subprogram (Entity (Match))
1809
1810 -- If the actual subprogram comes from a different
1811 -- unit, it is already frozen, either by a body in
1812 -- that unit or by the end of the declarative part
1813 -- of the unit. This check avoids the freezing of
1814 -- subprograms defined in Standard which are used
1815 -- as generic actuals.
1816
1817 and then In_Same_Code_Unit (Entity (Match), I_Node)
1818 and then Has_Fully_Defined_Profile (Entity (Match))
1819 then
1820 -- Mark the subprogram as having a delayed freeze
1821 -- since this may be an out-of-order action.
1822
1823 Set_Has_Delayed_Freeze (Entity (Match));
1824 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1825 end if;
1826 end if;
1827
1828 -- If this is a nested generic, preserve default for later
1829 -- instantiations. We do this as well for GNATProve use,
1830 -- so that the list of generic associations is complete.
1831
1832 if No (Match) and then Box_Present (Formal) then
1833 declare
1834 Subp : constant Entity_Id :=
1835 Defining_Unit_Name
1836 (Specification (Last (Assoc_List)));
1837
1838 begin
1839 Append_To (Default_Actuals,
1840 Make_Generic_Association (Sloc (I_Node),
1841 Selector_Name =>
1842 New_Occurrence_Of (Subp, Sloc (I_Node)),
1843 Explicit_Generic_Actual_Parameter =>
1844 New_Occurrence_Of (Subp, Sloc (I_Node))));
1845 end;
1846 end if;
1847
1848 when N_Formal_Package_Declaration =>
1849 Match :=
1850 Matching_Actual
1851 (Defining_Identifier (Formal),
1852 Defining_Identifier (Original_Node (Analyzed_Formal)));
1853
1854 if No (Match) then
1855 if Partial_Parameterization then
1856 Process_Default (Formal);
1857
1858 else
1859 Error_Msg_Sloc := Sloc (Gen_Unit);
1860 Error_Msg_NE
1861 ("missing actual&",
1862 Instantiation_Node, Defining_Identifier (Formal));
1863 Error_Msg_NE
1864 ("\in instantiation of & declared#",
1865 Instantiation_Node, Gen_Unit);
1866
1867 Abandon_Instantiation (Instantiation_Node);
1868 end if;
1869
1870 else
1871 Analyze (Match);
1872 Append_List
1873 (Instantiate_Formal_Package
1874 (Formal, Match, Analyzed_Formal),
1875 Assoc_List);
1876
1877 -- Determine whether the actual package needs an explicit
1878 -- freeze node. This is only the case if the actual is
1879 -- declared in the same unit and has a body. Normally
1880 -- packages do not have explicit freeze nodes, and gigi
1881 -- only uses them to elaborate entities in a package
1882 -- body.
1883
1884 Explicit_Freeze_Check : declare
1885 Actual : constant Entity_Id := Entity (Match);
1886 Gen_Par : Entity_Id;
1887
1888 Needs_Freezing : Boolean;
1889 S : Entity_Id;
1890
1891 procedure Check_Generic_Parent;
1892 -- The actual may be an instantiation of a unit
1893 -- declared in a previous instantiation. If that
1894 -- one is also in the current compilation, it must
1895 -- itself be frozen before the actual. The actual
1896 -- may be an instantiation of a generic child unit,
1897 -- in which case the same applies to the instance
1898 -- of the parent which must be frozen before the
1899 -- actual.
1900 -- Should this itself be recursive ???
1901
1902 --------------------------
1903 -- Check_Generic_Parent --
1904 --------------------------
1905
1906 procedure Check_Generic_Parent is
1907 Inst : constant Node_Id :=
1908 Next (Unit_Declaration_Node (Actual));
1909 Par : Entity_Id;
1910
1911 begin
1912 Par := Empty;
1913
1914 if Nkind (Parent (Actual)) = N_Package_Specification
1915 then
1916 Par := Scope (Generic_Parent (Parent (Actual)));
1917
1918 if Is_Generic_Instance (Par) then
1919 null;
1920
1921 -- If the actual is a child generic unit, check
1922 -- whether the instantiation of the parent is
1923 -- also local and must also be frozen now. We
1924 -- must retrieve the instance node to locate the
1925 -- parent instance if any.
1926
1927 elsif Ekind (Par) = E_Generic_Package
1928 and then Is_Child_Unit (Gen_Par)
1929 and then Ekind (Scope (Gen_Par)) =
1930 E_Generic_Package
1931 then
1932 if Nkind (Inst) = N_Package_Instantiation
1933 and then Nkind (Name (Inst)) =
1934 N_Expanded_Name
1935 then
1936 -- Retrieve entity of parent instance
1937
1938 Par := Entity (Prefix (Name (Inst)));
1939 end if;
1940
1941 else
1942 Par := Empty;
1943 end if;
1944 end if;
1945
1946 if Present (Par)
1947 and then Is_Generic_Instance (Par)
1948 and then Scope (Par) = Current_Scope
1949 and then
1950 (No (Freeze_Node (Par))
1951 or else
1952 not Is_List_Member (Freeze_Node (Par)))
1953 then
1954 Set_Has_Delayed_Freeze (Par);
1955 Append_Elmt (Par, Actuals_To_Freeze);
1956 end if;
1957 end Check_Generic_Parent;
1958
1959 -- Start of processing for Explicit_Freeze_Check
1960
1961 begin
1962 if Present (Renamed_Entity (Actual)) then
1963 Gen_Par :=
1964 Generic_Parent (Specification
1965 (Unit_Declaration_Node
1966 (Renamed_Entity (Actual))));
1967 else
1968 Gen_Par :=
1969 Generic_Parent (Specification
1970 (Unit_Declaration_Node (Actual)));
1971 end if;
1972
1973 if not Expander_Active
1974 or else not Has_Completion (Actual)
1975 or else not In_Same_Source_Unit (I_Node, Actual)
1976 or else Is_Frozen (Actual)
1977 or else
1978 (Present (Renamed_Entity (Actual))
1979 and then
1980 not In_Same_Source_Unit
1981 (I_Node, (Renamed_Entity (Actual))))
1982 then
1983 null;
1984
1985 else
1986 -- Finally we want to exclude such freeze nodes
1987 -- from statement sequences, which freeze
1988 -- everything before them.
1989 -- Is this strictly necessary ???
1990
1991 Needs_Freezing := True;
1992
1993 S := Current_Scope;
1994 while Present (S) loop
1995 if Ekind_In (S, E_Block,
1996 E_Function,
1997 E_Loop,
1998 E_Procedure)
1999 then
2000 Needs_Freezing := False;
2001 exit;
2002 end if;
2003
2004 S := Scope (S);
2005 end loop;
2006
2007 if Needs_Freezing then
2008 Check_Generic_Parent;
2009
2010 -- If the actual is a renaming of a proper
2011 -- instance of the formal package, indicate
2012 -- that it is the instance that must be frozen.
2013
2014 if Nkind (Parent (Actual)) =
2015 N_Package_Renaming_Declaration
2016 then
2017 Set_Has_Delayed_Freeze
2018 (Renamed_Entity (Actual));
2019 Append_Elmt
2020 (Renamed_Entity (Actual),
2021 Actuals_To_Freeze);
2022 else
2023 Set_Has_Delayed_Freeze (Actual);
2024 Append_Elmt (Actual, Actuals_To_Freeze);
2025 end if;
2026 end if;
2027 end if;
2028 end Explicit_Freeze_Check;
2029 end if;
2030
2031 -- For use type and use package appearing in the generic part,
2032 -- we have already copied them, so we can just move them where
2033 -- they belong (we mustn't recopy them since this would mess up
2034 -- the Sloc values).
2035
2036 when N_Use_Package_Clause
2037 | N_Use_Type_Clause
2038 =>
2039 if Nkind (Original_Node (I_Node)) =
2040 N_Formal_Package_Declaration
2041 then
2042 Append (New_Copy_Tree (Formal), Assoc_List);
2043 else
2044 Remove (Formal);
2045 Append (Formal, Assoc_List);
2046 end if;
2047
2048 when others =>
2049 raise Program_Error;
2050 end case;
2051
2052 Formal := Saved_Formal;
2053 Next_Non_Pragma (Analyzed_Formal);
2054 end loop;
2055
2056 if Num_Actuals > Num_Matched then
2057 Error_Msg_Sloc := Sloc (Gen_Unit);
2058
2059 if Present (Selector_Name (Actual)) then
2060 Error_Msg_NE
2061 ("unmatched actual &", Actual, Selector_Name (Actual));
2062 Error_Msg_NE
2063 ("\in instantiation of & declared#", Actual, Gen_Unit);
2064 else
2065 Error_Msg_NE
2066 ("unmatched actual in instantiation of & declared#",
2067 Actual, Gen_Unit);
2068 end if;
2069 end if;
2070
2071 elsif Present (Actuals) then
2072 Error_Msg_N
2073 ("too many actuals in generic instantiation", Instantiation_Node);
2074 end if;
2075
2076 -- An instantiation freezes all generic actuals. The only exceptions
2077 -- to this are incomplete types and subprograms which are not fully
2078 -- defined at the point of instantiation.
2079
2080 declare
2081 Elmt : Elmt_Id := First_Elmt (Actuals_To_Freeze);
2082 begin
2083 while Present (Elmt) loop
2084 Freeze_Before (I_Node, Node (Elmt));
2085 Next_Elmt (Elmt);
2086 end loop;
2087 end;
2088
2089 -- If there are default subprograms, normalize the tree by adding
2090 -- explicit associations for them. This is required if the instance
2091 -- appears within a generic.
2092
2093 if not Is_Empty_List (Default_Actuals) then
2094 declare
2095 Default : Node_Id;
2096
2097 begin
2098 Default := First (Default_Actuals);
2099 while Present (Default) loop
2100 Mark_Rewrite_Insertion (Default);
2101 Next (Default);
2102 end loop;
2103
2104 if No (Actuals) then
2105 Set_Generic_Associations (I_Node, Default_Actuals);
2106 else
2107 Append_List_To (Actuals, Default_Actuals);
2108 end if;
2109 end;
2110 end if;
2111
2112 -- If this is a formal package, normalize the parameter list by adding
2113 -- explicit box associations for the formals that are covered by an
2114 -- Others_Choice.
2115
2116 if not Is_Empty_List (Default_Formals) then
2117 Append_List (Default_Formals, Formals);
2118 end if;
2119
2120 return Assoc_List;
2121 end Analyze_Associations;
2122
2123 -------------------------------
2124 -- Analyze_Formal_Array_Type --
2125 -------------------------------
2126
2127 procedure Analyze_Formal_Array_Type
2128 (T : in out Entity_Id;
2129 Def : Node_Id)
2130 is
2131 DSS : Node_Id;
2132
2133 begin
2134 -- Treated like a non-generic array declaration, with additional
2135 -- semantic checks.
2136
2137 Enter_Name (T);
2138
2139 if Nkind (Def) = N_Constrained_Array_Definition then
2140 DSS := First (Discrete_Subtype_Definitions (Def));
2141 while Present (DSS) loop
2142 if Nkind_In (DSS, N_Subtype_Indication,
2143 N_Range,
2144 N_Attribute_Reference)
2145 then
2146 Error_Msg_N ("only a subtype mark is allowed in a formal", DSS);
2147 end if;
2148
2149 Next (DSS);
2150 end loop;
2151 end if;
2152
2153 Array_Type_Declaration (T, Def);
2154 Set_Is_Generic_Type (Base_Type (T));
2155
2156 if Ekind (Component_Type (T)) = E_Incomplete_Type
2157 and then No (Full_View (Component_Type (T)))
2158 then
2159 Error_Msg_N ("premature usage of incomplete type", Def);
2160
2161 -- Check that range constraint is not allowed on the component type
2162 -- of a generic formal array type (AARM 12.5.3(3))
2163
2164 elsif Is_Internal (Component_Type (T))
2165 and then Present (Subtype_Indication (Component_Definition (Def)))
2166 and then Nkind (Original_Node
2167 (Subtype_Indication (Component_Definition (Def)))) =
2168 N_Subtype_Indication
2169 then
2170 Error_Msg_N
2171 ("in a formal, a subtype indication can only be "
2172 & "a subtype mark (RM 12.5.3(3))",
2173 Subtype_Indication (Component_Definition (Def)));
2174 end if;
2175
2176 end Analyze_Formal_Array_Type;
2177
2178 ---------------------------------------------
2179 -- Analyze_Formal_Decimal_Fixed_Point_Type --
2180 ---------------------------------------------
2181
2182 -- As for other generic types, we create a valid type representation with
2183 -- legal but arbitrary attributes, whose values are never considered
2184 -- static. For all scalar types we introduce an anonymous base type, with
2185 -- the same attributes. We choose the corresponding integer type to be
2186 -- Standard_Integer.
2187 -- Here and in other similar routines, the Sloc of the generated internal
2188 -- type must be the same as the sloc of the defining identifier of the
2189 -- formal type declaration, to provide proper source navigation.
2190
2191 procedure Analyze_Formal_Decimal_Fixed_Point_Type
2192 (T : Entity_Id;
2193 Def : Node_Id)
2194 is
2195 Loc : constant Source_Ptr := Sloc (Def);
2196
2197 Base : constant Entity_Id :=
2198 New_Internal_Entity
2199 (E_Decimal_Fixed_Point_Type,
2200 Current_Scope,
2201 Sloc (Defining_Identifier (Parent (Def))), 'G');
2202
2203 Int_Base : constant Entity_Id := Standard_Integer;
2204 Delta_Val : constant Ureal := Ureal_1;
2205 Digs_Val : constant Uint := Uint_6;
2206
2207 function Make_Dummy_Bound return Node_Id;
2208 -- Return a properly typed universal real literal to use as a bound
2209
2210 ----------------------
2211 -- Make_Dummy_Bound --
2212 ----------------------
2213
2214 function Make_Dummy_Bound return Node_Id is
2215 Bound : constant Node_Id := Make_Real_Literal (Loc, Ureal_1);
2216 begin
2217 Set_Etype (Bound, Universal_Real);
2218 return Bound;
2219 end Make_Dummy_Bound;
2220
2221 -- Start of processing for Analyze_Formal_Decimal_Fixed_Point_Type
2222
2223 begin
2224 Enter_Name (T);
2225
2226 Set_Etype (Base, Base);
2227 Set_Size_Info (Base, Int_Base);
2228 Set_RM_Size (Base, RM_Size (Int_Base));
2229 Set_First_Rep_Item (Base, First_Rep_Item (Int_Base));
2230 Set_Digits_Value (Base, Digs_Val);
2231 Set_Delta_Value (Base, Delta_Val);
2232 Set_Small_Value (Base, Delta_Val);
2233 Set_Scalar_Range (Base,
2234 Make_Range (Loc,
2235 Low_Bound => Make_Dummy_Bound,
2236 High_Bound => Make_Dummy_Bound));
2237
2238 Set_Is_Generic_Type (Base);
2239 Set_Parent (Base, Parent (Def));
2240
2241 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
2242 Set_Etype (T, Base);
2243 Set_Size_Info (T, Int_Base);
2244 Set_RM_Size (T, RM_Size (Int_Base));
2245 Set_First_Rep_Item (T, First_Rep_Item (Int_Base));
2246 Set_Digits_Value (T, Digs_Val);
2247 Set_Delta_Value (T, Delta_Val);
2248 Set_Small_Value (T, Delta_Val);
2249 Set_Scalar_Range (T, Scalar_Range (Base));
2250 Set_Is_Constrained (T);
2251
2252 Check_Restriction (No_Fixed_Point, Def);
2253 end Analyze_Formal_Decimal_Fixed_Point_Type;
2254
2255 -------------------------------------------
2256 -- Analyze_Formal_Derived_Interface_Type --
2257 -------------------------------------------
2258
2259 procedure Analyze_Formal_Derived_Interface_Type
2260 (N : Node_Id;
2261 T : Entity_Id;
2262 Def : Node_Id)
2263 is
2264 Loc : constant Source_Ptr := Sloc (Def);
2265
2266 begin
2267 -- Rewrite as a type declaration of a derived type. This ensures that
2268 -- the interface list and primitive operations are properly captured.
2269
2270 Rewrite (N,
2271 Make_Full_Type_Declaration (Loc,
2272 Defining_Identifier => T,
2273 Type_Definition => Def));
2274 Analyze (N);
2275 Set_Is_Generic_Type (T);
2276 end Analyze_Formal_Derived_Interface_Type;
2277
2278 ---------------------------------
2279 -- Analyze_Formal_Derived_Type --
2280 ---------------------------------
2281
2282 procedure Analyze_Formal_Derived_Type
2283 (N : Node_Id;
2284 T : Entity_Id;
2285 Def : Node_Id)
2286 is
2287 Loc : constant Source_Ptr := Sloc (Def);
2288 Unk_Disc : constant Boolean := Unknown_Discriminants_Present (N);
2289 New_N : Node_Id;
2290
2291 begin
2292 Set_Is_Generic_Type (T);
2293
2294 if Private_Present (Def) then
2295 New_N :=
2296 Make_Private_Extension_Declaration (Loc,
2297 Defining_Identifier => T,
2298 Discriminant_Specifications => Discriminant_Specifications (N),
2299 Unknown_Discriminants_Present => Unk_Disc,
2300 Subtype_Indication => Subtype_Mark (Def),
2301 Interface_List => Interface_List (Def));
2302
2303 Set_Abstract_Present (New_N, Abstract_Present (Def));
2304 Set_Limited_Present (New_N, Limited_Present (Def));
2305 Set_Synchronized_Present (New_N, Synchronized_Present (Def));
2306
2307 else
2308 New_N :=
2309 Make_Full_Type_Declaration (Loc,
2310 Defining_Identifier => T,
2311 Discriminant_Specifications =>
2312 Discriminant_Specifications (Parent (T)),
2313 Type_Definition =>
2314 Make_Derived_Type_Definition (Loc,
2315 Subtype_Indication => Subtype_Mark (Def)));
2316
2317 Set_Abstract_Present
2318 (Type_Definition (New_N), Abstract_Present (Def));
2319 Set_Limited_Present
2320 (Type_Definition (New_N), Limited_Present (Def));
2321 end if;
2322
2323 Rewrite (N, New_N);
2324 Analyze (N);
2325
2326 if Unk_Disc then
2327 if not Is_Composite_Type (T) then
2328 Error_Msg_N
2329 ("unknown discriminants not allowed for elementary types", N);
2330 else
2331 Set_Has_Unknown_Discriminants (T);
2332 Set_Is_Constrained (T, False);
2333 end if;
2334 end if;
2335
2336 -- If the parent type has a known size, so does the formal, which makes
2337 -- legal representation clauses that involve the formal.
2338
2339 Set_Size_Known_At_Compile_Time
2340 (T, Size_Known_At_Compile_Time (Entity (Subtype_Mark (Def))));
2341 end Analyze_Formal_Derived_Type;
2342
2343 ----------------------------------
2344 -- Analyze_Formal_Discrete_Type --
2345 ----------------------------------
2346
2347 -- The operations defined for a discrete types are those of an enumeration
2348 -- type. The size is set to an arbitrary value, for use in analyzing the
2349 -- generic unit.
2350
2351 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id) is
2352 Loc : constant Source_Ptr := Sloc (Def);
2353 Lo : Node_Id;
2354 Hi : Node_Id;
2355
2356 Base : constant Entity_Id :=
2357 New_Internal_Entity
2358 (E_Floating_Point_Type, Current_Scope,
2359 Sloc (Defining_Identifier (Parent (Def))), 'G');
2360
2361 begin
2362 Enter_Name (T);
2363 Set_Ekind (T, E_Enumeration_Subtype);
2364 Set_Etype (T, Base);
2365 Init_Size (T, 8);
2366 Init_Alignment (T);
2367 Set_Is_Generic_Type (T);
2368 Set_Is_Constrained (T);
2369
2370 -- For semantic analysis, the bounds of the type must be set to some
2371 -- non-static value. The simplest is to create attribute nodes for those
2372 -- bounds, that refer to the type itself. These bounds are never
2373 -- analyzed but serve as place-holders.
2374
2375 Lo :=
2376 Make_Attribute_Reference (Loc,
2377 Attribute_Name => Name_First,
2378 Prefix => New_Occurrence_Of (T, Loc));
2379 Set_Etype (Lo, T);
2380
2381 Hi :=
2382 Make_Attribute_Reference (Loc,
2383 Attribute_Name => Name_Last,
2384 Prefix => New_Occurrence_Of (T, Loc));
2385 Set_Etype (Hi, T);
2386
2387 Set_Scalar_Range (T,
2388 Make_Range (Loc,
2389 Low_Bound => Lo,
2390 High_Bound => Hi));
2391
2392 Set_Ekind (Base, E_Enumeration_Type);
2393 Set_Etype (Base, Base);
2394 Init_Size (Base, 8);
2395 Init_Alignment (Base);
2396 Set_Is_Generic_Type (Base);
2397 Set_Scalar_Range (Base, Scalar_Range (T));
2398 Set_Parent (Base, Parent (Def));
2399 end Analyze_Formal_Discrete_Type;
2400
2401 ----------------------------------
2402 -- Analyze_Formal_Floating_Type --
2403 ---------------------------------
2404
2405 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id) is
2406 Base : constant Entity_Id :=
2407 New_Internal_Entity
2408 (E_Floating_Point_Type, Current_Scope,
2409 Sloc (Defining_Identifier (Parent (Def))), 'G');
2410
2411 begin
2412 -- The various semantic attributes are taken from the predefined type
2413 -- Float, just so that all of them are initialized. Their values are
2414 -- never used because no constant folding or expansion takes place in
2415 -- the generic itself.
2416
2417 Enter_Name (T);
2418 Set_Ekind (T, E_Floating_Point_Subtype);
2419 Set_Etype (T, Base);
2420 Set_Size_Info (T, (Standard_Float));
2421 Set_RM_Size (T, RM_Size (Standard_Float));
2422 Set_Digits_Value (T, Digits_Value (Standard_Float));
2423 Set_Scalar_Range (T, Scalar_Range (Standard_Float));
2424 Set_Is_Constrained (T);
2425
2426 Set_Is_Generic_Type (Base);
2427 Set_Etype (Base, Base);
2428 Set_Size_Info (Base, (Standard_Float));
2429 Set_RM_Size (Base, RM_Size (Standard_Float));
2430 Set_Digits_Value (Base, Digits_Value (Standard_Float));
2431 Set_Scalar_Range (Base, Scalar_Range (Standard_Float));
2432 Set_Parent (Base, Parent (Def));
2433
2434 Check_Restriction (No_Floating_Point, Def);
2435 end Analyze_Formal_Floating_Type;
2436
2437 -----------------------------------
2438 -- Analyze_Formal_Interface_Type;--
2439 -----------------------------------
2440
2441 procedure Analyze_Formal_Interface_Type
2442 (N : Node_Id;
2443 T : Entity_Id;
2444 Def : Node_Id)
2445 is
2446 Loc : constant Source_Ptr := Sloc (N);
2447 New_N : Node_Id;
2448
2449 begin
2450 New_N :=
2451 Make_Full_Type_Declaration (Loc,
2452 Defining_Identifier => T,
2453 Type_Definition => Def);
2454
2455 Rewrite (N, New_N);
2456 Analyze (N);
2457 Set_Is_Generic_Type (T);
2458 end Analyze_Formal_Interface_Type;
2459
2460 ---------------------------------
2461 -- Analyze_Formal_Modular_Type --
2462 ---------------------------------
2463
2464 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id) is
2465 begin
2466 -- Apart from their entity kind, generic modular types are treated like
2467 -- signed integer types, and have the same attributes.
2468
2469 Analyze_Formal_Signed_Integer_Type (T, Def);
2470 Set_Ekind (T, E_Modular_Integer_Subtype);
2471 Set_Ekind (Etype (T), E_Modular_Integer_Type);
2472
2473 end Analyze_Formal_Modular_Type;
2474
2475 ---------------------------------------
2476 -- Analyze_Formal_Object_Declaration --
2477 ---------------------------------------
2478
2479 procedure Analyze_Formal_Object_Declaration (N : Node_Id) is
2480 E : constant Node_Id := Default_Expression (N);
2481 Id : constant Node_Id := Defining_Identifier (N);
2482 K : Entity_Kind;
2483 T : Node_Id;
2484
2485 begin
2486 Enter_Name (Id);
2487
2488 -- Determine the mode of the formal object
2489
2490 if Out_Present (N) then
2491 K := E_Generic_In_Out_Parameter;
2492
2493 if not In_Present (N) then
2494 Error_Msg_N ("formal generic objects cannot have mode OUT", N);
2495 end if;
2496
2497 else
2498 K := E_Generic_In_Parameter;
2499 end if;
2500
2501 if Present (Subtype_Mark (N)) then
2502 Find_Type (Subtype_Mark (N));
2503 T := Entity (Subtype_Mark (N));
2504
2505 -- Verify that there is no redundant null exclusion
2506
2507 if Null_Exclusion_Present (N) then
2508 if not Is_Access_Type (T) then
2509 Error_Msg_N
2510 ("null exclusion can only apply to an access type", N);
2511
2512 elsif Can_Never_Be_Null (T) then
2513 Error_Msg_NE
2514 ("`NOT NULL` not allowed (& already excludes null)", N, T);
2515 end if;
2516 end if;
2517
2518 -- Ada 2005 (AI-423): Formal object with an access definition
2519
2520 else
2521 Check_Access_Definition (N);
2522 T := Access_Definition
2523 (Related_Nod => N,
2524 N => Access_Definition (N));
2525 end if;
2526
2527 if Ekind (T) = E_Incomplete_Type then
2528 declare
2529 Error_Node : Node_Id;
2530
2531 begin
2532 if Present (Subtype_Mark (N)) then
2533 Error_Node := Subtype_Mark (N);
2534 else
2535 Check_Access_Definition (N);
2536 Error_Node := Access_Definition (N);
2537 end if;
2538
2539 Error_Msg_N ("premature usage of incomplete type", Error_Node);
2540 end;
2541 end if;
2542
2543 if K = E_Generic_In_Parameter then
2544
2545 -- Ada 2005 (AI-287): Limited aggregates allowed in generic formals
2546
2547 if Ada_Version < Ada_2005 and then Is_Limited_Type (T) then
2548 Error_Msg_N
2549 ("generic formal of mode IN must not be of limited type", N);
2550 Explain_Limited_Type (T, N);
2551 end if;
2552
2553 if Is_Abstract_Type (T) then
2554 Error_Msg_N
2555 ("generic formal of mode IN must not be of abstract type", N);
2556 end if;
2557
2558 if Present (E) then
2559 Preanalyze_Spec_Expression (E, T);
2560
2561 if Is_Limited_Type (T) and then not OK_For_Limited_Init (T, E) then
2562 Error_Msg_N
2563 ("initialization not allowed for limited types", E);
2564 Explain_Limited_Type (T, E);
2565 end if;
2566 end if;
2567
2568 Set_Ekind (Id, K);
2569 Set_Etype (Id, T);
2570
2571 -- Case of generic IN OUT parameter
2572
2573 else
2574 -- If the formal has an unconstrained type, construct its actual
2575 -- subtype, as is done for subprogram formals. In this fashion, all
2576 -- its uses can refer to specific bounds.
2577
2578 Set_Ekind (Id, K);
2579 Set_Etype (Id, T);
2580
2581 if (Is_Array_Type (T) and then not Is_Constrained (T))
2582 or else (Ekind (T) = E_Record_Type and then Has_Discriminants (T))
2583 then
2584 declare
2585 Non_Freezing_Ref : constant Node_Id :=
2586 New_Occurrence_Of (Id, Sloc (Id));
2587 Decl : Node_Id;
2588
2589 begin
2590 -- Make sure the actual subtype doesn't generate bogus freezing
2591
2592 Set_Must_Not_Freeze (Non_Freezing_Ref);
2593 Decl := Build_Actual_Subtype (T, Non_Freezing_Ref);
2594 Insert_Before_And_Analyze (N, Decl);
2595 Set_Actual_Subtype (Id, Defining_Identifier (Decl));
2596 end;
2597 else
2598 Set_Actual_Subtype (Id, T);
2599 end if;
2600
2601 if Present (E) then
2602 Error_Msg_N
2603 ("initialization not allowed for `IN OUT` formals", N);
2604 end if;
2605 end if;
2606
2607 if Has_Aspects (N) then
2608 Analyze_Aspect_Specifications (N, Id);
2609 end if;
2610 end Analyze_Formal_Object_Declaration;
2611
2612 ----------------------------------------------
2613 -- Analyze_Formal_Ordinary_Fixed_Point_Type --
2614 ----------------------------------------------
2615
2616 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
2617 (T : Entity_Id;
2618 Def : Node_Id)
2619 is
2620 Loc : constant Source_Ptr := Sloc (Def);
2621 Base : constant Entity_Id :=
2622 New_Internal_Entity
2623 (E_Ordinary_Fixed_Point_Type, Current_Scope,
2624 Sloc (Defining_Identifier (Parent (Def))), 'G');
2625
2626 begin
2627 -- The semantic attributes are set for completeness only, their values
2628 -- will never be used, since all properties of the type are non-static.
2629
2630 Enter_Name (T);
2631 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
2632 Set_Etype (T, Base);
2633 Set_Size_Info (T, Standard_Integer);
2634 Set_RM_Size (T, RM_Size (Standard_Integer));
2635 Set_Small_Value (T, Ureal_1);
2636 Set_Delta_Value (T, Ureal_1);
2637 Set_Scalar_Range (T,
2638 Make_Range (Loc,
2639 Low_Bound => Make_Real_Literal (Loc, Ureal_1),
2640 High_Bound => Make_Real_Literal (Loc, Ureal_1)));
2641 Set_Is_Constrained (T);
2642
2643 Set_Is_Generic_Type (Base);
2644 Set_Etype (Base, Base);
2645 Set_Size_Info (Base, Standard_Integer);
2646 Set_RM_Size (Base, RM_Size (Standard_Integer));
2647 Set_Small_Value (Base, Ureal_1);
2648 Set_Delta_Value (Base, Ureal_1);
2649 Set_Scalar_Range (Base, Scalar_Range (T));
2650 Set_Parent (Base, Parent (Def));
2651
2652 Check_Restriction (No_Fixed_Point, Def);
2653 end Analyze_Formal_Ordinary_Fixed_Point_Type;
2654
2655 ----------------------------------------
2656 -- Analyze_Formal_Package_Declaration --
2657 ----------------------------------------
2658
2659 procedure Analyze_Formal_Package_Declaration (N : Node_Id) is
2660 Gen_Id : constant Node_Id := Name (N);
2661 Loc : constant Source_Ptr := Sloc (N);
2662 Pack_Id : constant Entity_Id := Defining_Identifier (N);
2663 Formal : Entity_Id;
2664 Gen_Decl : Node_Id;
2665 Gen_Unit : Entity_Id;
2666 Renaming : Node_Id;
2667
2668 Vis_Prims_List : Elist_Id := No_Elist;
2669 -- List of primitives made temporarily visible in the instantiation
2670 -- to match the visibility of the formal type.
2671
2672 function Build_Local_Package return Node_Id;
2673 -- The formal package is rewritten so that its parameters are replaced
2674 -- with corresponding declarations. For parameters with bona fide
2675 -- associations these declarations are created by Analyze_Associations
2676 -- as for a regular instantiation. For boxed parameters, we preserve
2677 -- the formal declarations and analyze them, in order to introduce
2678 -- entities of the right kind in the environment of the formal.
2679
2680 -------------------------
2681 -- Build_Local_Package --
2682 -------------------------
2683
2684 function Build_Local_Package return Node_Id is
2685 Decls : List_Id;
2686 Pack_Decl : Node_Id;
2687
2688 begin
2689 -- Within the formal, the name of the generic package is a renaming
2690 -- of the formal (as for a regular instantiation).
2691
2692 Pack_Decl :=
2693 Make_Package_Declaration (Loc,
2694 Specification =>
2695 Copy_Generic_Node
2696 (Specification (Original_Node (Gen_Decl)),
2697 Empty, Instantiating => True));
2698
2699 Renaming :=
2700 Make_Package_Renaming_Declaration (Loc,
2701 Defining_Unit_Name =>
2702 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
2703 Name => New_Occurrence_Of (Formal, Loc));
2704
2705 if Nkind (Gen_Id) = N_Identifier
2706 and then Chars (Gen_Id) = Chars (Pack_Id)
2707 then
2708 Error_Msg_NE
2709 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
2710 end if;
2711
2712 -- If the formal is declared with a box, or with an others choice,
2713 -- create corresponding declarations for all entities in the formal
2714 -- part, so that names with the proper types are available in the
2715 -- specification of the formal package.
2716
2717 -- On the other hand, if there are no associations, then all the
2718 -- formals must have defaults, and this will be checked by the
2719 -- call to Analyze_Associations.
2720
2721 if Box_Present (N)
2722 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2723 then
2724 declare
2725 Formal_Decl : Node_Id;
2726
2727 begin
2728 -- TBA : for a formal package, need to recurse ???
2729
2730 Decls := New_List;
2731 Formal_Decl :=
2732 First
2733 (Generic_Formal_Declarations (Original_Node (Gen_Decl)));
2734 while Present (Formal_Decl) loop
2735 Append_To
2736 (Decls,
2737 Copy_Generic_Node
2738 (Formal_Decl, Empty, Instantiating => True));
2739 Next (Formal_Decl);
2740 end loop;
2741 end;
2742
2743 -- If generic associations are present, use Analyze_Associations to
2744 -- create the proper renaming declarations.
2745
2746 else
2747 declare
2748 Act_Tree : constant Node_Id :=
2749 Copy_Generic_Node
2750 (Original_Node (Gen_Decl), Empty,
2751 Instantiating => True);
2752
2753 begin
2754 Generic_Renamings.Set_Last (0);
2755 Generic_Renamings_HTable.Reset;
2756 Instantiation_Node := N;
2757
2758 Decls :=
2759 Analyze_Associations
2760 (I_Node => Original_Node (N),
2761 Formals => Generic_Formal_Declarations (Act_Tree),
2762 F_Copy => Generic_Formal_Declarations (Gen_Decl));
2763
2764 Vis_Prims_List := Check_Hidden_Primitives (Decls);
2765 end;
2766 end if;
2767
2768 Append (Renaming, To => Decls);
2769
2770 -- Add generated declarations ahead of local declarations in
2771 -- the package.
2772
2773 if No (Visible_Declarations (Specification (Pack_Decl))) then
2774 Set_Visible_Declarations (Specification (Pack_Decl), Decls);
2775 else
2776 Insert_List_Before
2777 (First (Visible_Declarations (Specification (Pack_Decl))),
2778 Decls);
2779 end if;
2780
2781 return Pack_Decl;
2782 end Build_Local_Package;
2783
2784 -- Local variables
2785
2786 Save_ISMP : constant Boolean := Ignore_SPARK_Mode_Pragmas_In_Instance;
2787 -- Save flag Ignore_SPARK_Mode_Pragmas_In_Instance for restore on exit
2788
2789 Associations : Boolean := True;
2790 New_N : Node_Id;
2791 Parent_Installed : Boolean := False;
2792 Parent_Instance : Entity_Id;
2793 Renaming_In_Par : Entity_Id;
2794
2795 -- Start of processing for Analyze_Formal_Package_Declaration
2796
2797 begin
2798 Check_Text_IO_Special_Unit (Gen_Id);
2799
2800 Init_Env;
2801 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
2802 Gen_Unit := Entity (Gen_Id);
2803
2804 -- Check for a formal package that is a package renaming
2805
2806 if Present (Renamed_Object (Gen_Unit)) then
2807
2808 -- Indicate that unit is used, before replacing it with renamed
2809 -- entity for use below.
2810
2811 if In_Extended_Main_Source_Unit (N) then
2812 Set_Is_Instantiated (Gen_Unit);
2813 Generate_Reference (Gen_Unit, N);
2814 end if;
2815
2816 Gen_Unit := Renamed_Object (Gen_Unit);
2817 end if;
2818
2819 if Ekind (Gen_Unit) /= E_Generic_Package then
2820 Error_Msg_N ("expect generic package name", Gen_Id);
2821 Restore_Env;
2822 goto Leave;
2823
2824 elsif Gen_Unit = Current_Scope then
2825 Error_Msg_N
2826 ("generic package cannot be used as a formal package of itself",
2827 Gen_Id);
2828 Restore_Env;
2829 goto Leave;
2830
2831 elsif In_Open_Scopes (Gen_Unit) then
2832 if Is_Compilation_Unit (Gen_Unit)
2833 and then Is_Child_Unit (Current_Scope)
2834 then
2835 -- Special-case the error when the formal is a parent, and
2836 -- continue analysis to minimize cascaded errors.
2837
2838 Error_Msg_N
2839 ("generic parent cannot be used as formal package of a child "
2840 & "unit", Gen_Id);
2841
2842 else
2843 Error_Msg_N
2844 ("generic package cannot be used as a formal package within "
2845 & "itself", Gen_Id);
2846 Restore_Env;
2847 goto Leave;
2848 end if;
2849 end if;
2850
2851 -- Check that name of formal package does not hide name of generic,
2852 -- or its leading prefix. This check must be done separately because
2853 -- the name of the generic has already been analyzed.
2854
2855 declare
2856 Gen_Name : Entity_Id;
2857
2858 begin
2859 Gen_Name := Gen_Id;
2860 while Nkind (Gen_Name) = N_Expanded_Name loop
2861 Gen_Name := Prefix (Gen_Name);
2862 end loop;
2863
2864 if Chars (Gen_Name) = Chars (Pack_Id) then
2865 Error_Msg_NE
2866 ("& is hidden within declaration of formal package",
2867 Gen_Id, Gen_Name);
2868 end if;
2869 end;
2870
2871 if Box_Present (N)
2872 or else No (Generic_Associations (N))
2873 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2874 then
2875 Associations := False;
2876 end if;
2877
2878 -- If there are no generic associations, the generic parameters appear
2879 -- as local entities and are instantiated like them. We copy the generic
2880 -- package declaration as if it were an instantiation, and analyze it
2881 -- like a regular package, except that we treat the formals as
2882 -- additional visible components.
2883
2884 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
2885
2886 if In_Extended_Main_Source_Unit (N) then
2887 Set_Is_Instantiated (Gen_Unit);
2888 Generate_Reference (Gen_Unit, N);
2889 end if;
2890
2891 Formal := New_Copy (Pack_Id);
2892 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
2893
2894 -- Make local generic without formals. The formals will be replaced with
2895 -- internal declarations.
2896
2897 begin
2898 New_N := Build_Local_Package;
2899
2900 -- If there are errors in the parameter list, Analyze_Associations
2901 -- raises Instantiation_Error. Patch the declaration to prevent further
2902 -- exception propagation.
2903
2904 exception
2905 when Instantiation_Error =>
2906 Enter_Name (Formal);
2907 Set_Ekind (Formal, E_Variable);
2908 Set_Etype (Formal, Any_Type);
2909 Restore_Hidden_Primitives (Vis_Prims_List);
2910
2911 if Parent_Installed then
2912 Remove_Parent;
2913 end if;
2914
2915 goto Leave;
2916 end;
2917
2918 Rewrite (N, New_N);
2919 Set_Defining_Unit_Name (Specification (New_N), Formal);
2920 Set_Generic_Parent (Specification (N), Gen_Unit);
2921 Set_Instance_Env (Gen_Unit, Formal);
2922 Set_Is_Generic_Instance (Formal);
2923
2924 Enter_Name (Formal);
2925 Set_Ekind (Formal, E_Package);
2926 Set_Etype (Formal, Standard_Void_Type);
2927 Set_Inner_Instances (Formal, New_Elmt_List);
2928
2929 -- It is unclear that any aspects can apply to a formal package
2930 -- declaration, given that they look like a hidden conformance
2931 -- requirement on the corresponding actual. However, Abstract_State
2932 -- must be treated specially because it generates declarations that
2933 -- must appear before other declarations in the specification and
2934 -- must be analyzed at once.
2935
2936 if Present (Aspect_Specifications (Gen_Decl)) then
2937 if No (Aspect_Specifications (N)) then
2938 Set_Aspect_Specifications (N, New_List);
2939 Set_Has_Aspects (N);
2940 end if;
2941
2942 declare
2943 ASN : Node_Id := First (Aspect_Specifications (Gen_Decl));
2944 New_A : Node_Id;
2945
2946 begin
2947 while Present (ASN) loop
2948 if Get_Aspect_Id (ASN) = Aspect_Abstract_State then
2949 New_A :=
2950 Copy_Generic_Node (ASN, Empty, Instantiating => True);
2951 Set_Entity (New_A, Formal);
2952 Set_Analyzed (New_A, False);
2953 Append (New_A, Aspect_Specifications (N));
2954 Analyze_Aspect_Specifications (N, Formal);
2955 exit;
2956 end if;
2957
2958 Next (ASN);
2959 end loop;
2960 end;
2961 end if;
2962
2963 Push_Scope (Formal);
2964
2965 -- Manually set the SPARK_Mode from the context because the package
2966 -- declaration is never analyzed.
2967
2968 Set_SPARK_Pragma (Formal, SPARK_Mode_Pragma);
2969 Set_SPARK_Aux_Pragma (Formal, SPARK_Mode_Pragma);
2970 Set_SPARK_Pragma_Inherited (Formal);
2971 Set_SPARK_Aux_Pragma_Inherited (Formal);
2972
2973 if Is_Child_Unit (Gen_Unit) and then Parent_Installed then
2974
2975 -- Similarly, we have to make the name of the formal visible in the
2976 -- parent instance, to resolve properly fully qualified names that
2977 -- may appear in the generic unit. The parent instance has been
2978 -- placed on the scope stack ahead of the current scope.
2979
2980 Parent_Instance := Scope_Stack.Table (Scope_Stack.Last - 1).Entity;
2981
2982 Renaming_In_Par :=
2983 Make_Defining_Identifier (Loc, Chars (Gen_Unit));
2984 Set_Ekind (Renaming_In_Par, E_Package);
2985 Set_Etype (Renaming_In_Par, Standard_Void_Type);
2986 Set_Scope (Renaming_In_Par, Parent_Instance);
2987 Set_Parent (Renaming_In_Par, Parent (Formal));
2988 Set_Renamed_Object (Renaming_In_Par, Formal);
2989 Append_Entity (Renaming_In_Par, Parent_Instance);
2990 end if;
2991
2992 -- A formal package declaration behaves as a package instantiation with
2993 -- respect to SPARK_Mode "off". If the annotation is "off" or altogether
2994 -- missing, set the global flag which signals Analyze_Pragma to ingnore
2995 -- all SPARK_Mode pragmas within the generic_package_name.
2996
2997 if SPARK_Mode /= On then
2998 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
2999
3000 -- Mark the formal spec in case the body is instantiated at a later
3001 -- pass. This preserves the original context in effect for the body.
3002
3003 Set_Ignore_SPARK_Mode_Pragmas (Formal);
3004 end if;
3005
3006 Analyze (Specification (N));
3007
3008 -- The formals for which associations are provided are not visible
3009 -- outside of the formal package. The others are still declared by a
3010 -- formal parameter declaration.
3011
3012 -- If there are no associations, the only local entity to hide is the
3013 -- generated package renaming itself.
3014
3015 declare
3016 E : Entity_Id;
3017
3018 begin
3019 E := First_Entity (Formal);
3020 while Present (E) loop
3021 if Associations and then not Is_Generic_Formal (E) then
3022 Set_Is_Hidden (E);
3023 end if;
3024
3025 if Ekind (E) = E_Package and then Renamed_Entity (E) = Formal then
3026 Set_Is_Hidden (E);
3027 exit;
3028 end if;
3029
3030 Next_Entity (E);
3031 end loop;
3032 end;
3033
3034 End_Package_Scope (Formal);
3035 Restore_Hidden_Primitives (Vis_Prims_List);
3036
3037 if Parent_Installed then
3038 Remove_Parent;
3039 end if;
3040
3041 Restore_Env;
3042
3043 -- Inside the generic unit, the formal package is a regular package, but
3044 -- no body is needed for it. Note that after instantiation, the defining
3045 -- unit name we need is in the new tree and not in the original (see
3046 -- Package_Instantiation). A generic formal package is an instance, and
3047 -- can be used as an actual for an inner instance.
3048
3049 Set_Has_Completion (Formal, True);
3050
3051 -- Add semantic information to the original defining identifier.
3052
3053 Set_Ekind (Pack_Id, E_Package);
3054 Set_Etype (Pack_Id, Standard_Void_Type);
3055 Set_Scope (Pack_Id, Scope (Formal));
3056 Set_Has_Completion (Pack_Id, True);
3057
3058 <<Leave>>
3059 if Has_Aspects (N) then
3060 -- Unclear that any other aspects may appear here, snalyze them
3061 -- for completion, given that the grammar allows their appearance.
3062
3063 Analyze_Aspect_Specifications (N, Pack_Id);
3064 end if;
3065
3066 Ignore_SPARK_Mode_Pragmas_In_Instance := Save_ISMP;
3067 end Analyze_Formal_Package_Declaration;
3068
3069 ---------------------------------
3070 -- Analyze_Formal_Private_Type --
3071 ---------------------------------
3072
3073 procedure Analyze_Formal_Private_Type
3074 (N : Node_Id;
3075 T : Entity_Id;
3076 Def : Node_Id)
3077 is
3078 begin
3079 New_Private_Type (N, T, Def);
3080
3081 -- Set the size to an arbitrary but legal value
3082
3083 Set_Size_Info (T, Standard_Integer);
3084 Set_RM_Size (T, RM_Size (Standard_Integer));
3085 end Analyze_Formal_Private_Type;
3086
3087 ------------------------------------
3088 -- Analyze_Formal_Incomplete_Type --
3089 ------------------------------------
3090
3091 procedure Analyze_Formal_Incomplete_Type
3092 (T : Entity_Id;
3093 Def : Node_Id)
3094 is
3095 begin
3096 Enter_Name (T);
3097 Set_Ekind (T, E_Incomplete_Type);
3098 Set_Etype (T, T);
3099 Set_Private_Dependents (T, New_Elmt_List);
3100
3101 if Tagged_Present (Def) then
3102 Set_Is_Tagged_Type (T);
3103 Make_Class_Wide_Type (T);
3104 Set_Direct_Primitive_Operations (T, New_Elmt_List);
3105 end if;
3106 end Analyze_Formal_Incomplete_Type;
3107
3108 ----------------------------------------
3109 -- Analyze_Formal_Signed_Integer_Type --
3110 ----------------------------------------
3111
3112 procedure Analyze_Formal_Signed_Integer_Type
3113 (T : Entity_Id;
3114 Def : Node_Id)
3115 is
3116 Base : constant Entity_Id :=
3117 New_Internal_Entity
3118 (E_Signed_Integer_Type,
3119 Current_Scope,
3120 Sloc (Defining_Identifier (Parent (Def))), 'G');
3121
3122 begin
3123 Enter_Name (T);
3124
3125 Set_Ekind (T, E_Signed_Integer_Subtype);
3126 Set_Etype (T, Base);
3127 Set_Size_Info (T, Standard_Integer);
3128 Set_RM_Size (T, RM_Size (Standard_Integer));
3129 Set_Scalar_Range (T, Scalar_Range (Standard_Integer));
3130 Set_Is_Constrained (T);
3131
3132 Set_Is_Generic_Type (Base);
3133 Set_Size_Info (Base, Standard_Integer);
3134 Set_RM_Size (Base, RM_Size (Standard_Integer));
3135 Set_Etype (Base, Base);
3136 Set_Scalar_Range (Base, Scalar_Range (Standard_Integer));
3137 Set_Parent (Base, Parent (Def));
3138 end Analyze_Formal_Signed_Integer_Type;
3139
3140 -------------------------------------------
3141 -- Analyze_Formal_Subprogram_Declaration --
3142 -------------------------------------------
3143
3144 procedure Analyze_Formal_Subprogram_Declaration (N : Node_Id) is
3145 Spec : constant Node_Id := Specification (N);
3146 Def : constant Node_Id := Default_Name (N);
3147 Nam : constant Entity_Id := Defining_Unit_Name (Spec);
3148 Subp : Entity_Id;
3149
3150 begin
3151 if Nam = Error then
3152 return;
3153 end if;
3154
3155 if Nkind (Nam) = N_Defining_Program_Unit_Name then
3156 Error_Msg_N ("name of formal subprogram must be a direct name", Nam);
3157 goto Leave;
3158 end if;
3159
3160 Analyze_Subprogram_Declaration (N);
3161 Set_Is_Formal_Subprogram (Nam);
3162 Set_Has_Completion (Nam);
3163
3164 if Nkind (N) = N_Formal_Abstract_Subprogram_Declaration then
3165 Set_Is_Abstract_Subprogram (Nam);
3166
3167 Set_Is_Dispatching_Operation (Nam);
3168
3169 -- A formal abstract procedure cannot have a null default
3170 -- (RM 12.6(4.1/2)).
3171
3172 if Nkind (Spec) = N_Procedure_Specification
3173 and then Null_Present (Spec)
3174 then
3175 Error_Msg_N
3176 ("a formal abstract subprogram cannot default to null", Spec);
3177 end if;
3178
3179 declare
3180 Ctrl_Type : constant Entity_Id := Find_Dispatching_Type (Nam);
3181 begin
3182 if No (Ctrl_Type) then
3183 Error_Msg_N
3184 ("abstract formal subprogram must have a controlling type",
3185 N);
3186
3187 elsif Ada_Version >= Ada_2012
3188 and then Is_Incomplete_Type (Ctrl_Type)
3189 then
3190 Error_Msg_NE
3191 ("controlling type of abstract formal subprogram cannot "
3192 & "be incomplete type", N, Ctrl_Type);
3193
3194 else
3195 Check_Controlling_Formals (Ctrl_Type, Nam);
3196 end if;
3197 end;
3198 end if;
3199
3200 -- Default name is resolved at the point of instantiation
3201
3202 if Box_Present (N) then
3203 null;
3204
3205 -- Else default is bound at the point of generic declaration
3206
3207 elsif Present (Def) then
3208 if Nkind (Def) = N_Operator_Symbol then
3209 Find_Direct_Name (Def);
3210
3211 elsif Nkind (Def) /= N_Attribute_Reference then
3212 Analyze (Def);
3213
3214 else
3215 -- For an attribute reference, analyze the prefix and verify
3216 -- that it has the proper profile for the subprogram.
3217
3218 Analyze (Prefix (Def));
3219 Valid_Default_Attribute (Nam, Def);
3220 goto Leave;
3221 end if;
3222
3223 -- Default name may be overloaded, in which case the interpretation
3224 -- with the correct profile must be selected, as for a renaming.
3225 -- If the definition is an indexed component, it must denote a
3226 -- member of an entry family. If it is a selected component, it
3227 -- can be a protected operation.
3228
3229 if Etype (Def) = Any_Type then
3230 goto Leave;
3231
3232 elsif Nkind (Def) = N_Selected_Component then
3233 if not Is_Overloadable (Entity (Selector_Name (Def))) then
3234 Error_Msg_N ("expect valid subprogram name as default", Def);
3235 end if;
3236
3237 elsif Nkind (Def) = N_Indexed_Component then
3238 if Is_Entity_Name (Prefix (Def)) then
3239 if Ekind (Entity (Prefix (Def))) /= E_Entry_Family then
3240 Error_Msg_N ("expect valid subprogram name as default", Def);
3241 end if;
3242
3243 elsif Nkind (Prefix (Def)) = N_Selected_Component then
3244 if Ekind (Entity (Selector_Name (Prefix (Def)))) /=
3245 E_Entry_Family
3246 then
3247 Error_Msg_N ("expect valid subprogram name as default", Def);
3248 end if;
3249
3250 else
3251 Error_Msg_N ("expect valid subprogram name as default", Def);
3252 goto Leave;
3253 end if;
3254
3255 elsif Nkind (Def) = N_Character_Literal then
3256
3257 -- Needs some type checks: subprogram should be parameterless???
3258
3259 Resolve (Def, (Etype (Nam)));
3260
3261 elsif not Is_Entity_Name (Def)
3262 or else not Is_Overloadable (Entity (Def))
3263 then
3264 Error_Msg_N ("expect valid subprogram name as default", Def);
3265 goto Leave;
3266
3267 elsif not Is_Overloaded (Def) then
3268 Subp := Entity (Def);
3269
3270 if Subp = Nam then
3271 Error_Msg_N ("premature usage of formal subprogram", Def);
3272
3273 elsif not Entity_Matches_Spec (Subp, Nam) then
3274 Error_Msg_N ("no visible entity matches specification", Def);
3275 end if;
3276
3277 -- More than one interpretation, so disambiguate as for a renaming
3278
3279 else
3280 declare
3281 I : Interp_Index;
3282 I1 : Interp_Index := 0;
3283 It : Interp;
3284 It1 : Interp;
3285
3286 begin
3287 Subp := Any_Id;
3288 Get_First_Interp (Def, I, It);
3289 while Present (It.Nam) loop
3290 if Entity_Matches_Spec (It.Nam, Nam) then
3291 if Subp /= Any_Id then
3292 It1 := Disambiguate (Def, I1, I, Etype (Subp));
3293
3294 if It1 = No_Interp then
3295 Error_Msg_N ("ambiguous default subprogram", Def);
3296 else
3297 Subp := It1.Nam;
3298 end if;
3299
3300 exit;
3301
3302 else
3303 I1 := I;
3304 Subp := It.Nam;
3305 end if;
3306 end if;
3307
3308 Get_Next_Interp (I, It);
3309 end loop;
3310 end;
3311
3312 if Subp /= Any_Id then
3313
3314 -- Subprogram found, generate reference to it
3315
3316 Set_Entity (Def, Subp);
3317 Generate_Reference (Subp, Def);
3318
3319 if Subp = Nam then
3320 Error_Msg_N ("premature usage of formal subprogram", Def);
3321
3322 elsif Ekind (Subp) /= E_Operator then
3323 Check_Mode_Conformant (Subp, Nam);
3324 end if;
3325
3326 else
3327 Error_Msg_N ("no visible subprogram matches specification", N);
3328 end if;
3329 end if;
3330 end if;
3331
3332 <<Leave>>
3333 if Has_Aspects (N) then
3334 Analyze_Aspect_Specifications (N, Nam);
3335 end if;
3336
3337 end Analyze_Formal_Subprogram_Declaration;
3338
3339 -------------------------------------
3340 -- Analyze_Formal_Type_Declaration --
3341 -------------------------------------
3342
3343 procedure Analyze_Formal_Type_Declaration (N : Node_Id) is
3344 Def : constant Node_Id := Formal_Type_Definition (N);
3345 T : Entity_Id;
3346
3347 begin
3348 T := Defining_Identifier (N);
3349
3350 if Present (Discriminant_Specifications (N))
3351 and then Nkind (Def) /= N_Formal_Private_Type_Definition
3352 then
3353 Error_Msg_N
3354 ("discriminants not allowed for this formal type", T);
3355 end if;
3356
3357 -- Enter the new name, and branch to specific routine
3358
3359 case Nkind (Def) is
3360 when N_Formal_Private_Type_Definition =>
3361 Analyze_Formal_Private_Type (N, T, Def);
3362
3363 when N_Formal_Derived_Type_Definition =>
3364 Analyze_Formal_Derived_Type (N, T, Def);
3365
3366 when N_Formal_Incomplete_Type_Definition =>
3367 Analyze_Formal_Incomplete_Type (T, Def);
3368
3369 when N_Formal_Discrete_Type_Definition =>
3370 Analyze_Formal_Discrete_Type (T, Def);
3371
3372 when N_Formal_Signed_Integer_Type_Definition =>
3373 Analyze_Formal_Signed_Integer_Type (T, Def);
3374
3375 when N_Formal_Modular_Type_Definition =>
3376 Analyze_Formal_Modular_Type (T, Def);
3377
3378 when N_Formal_Floating_Point_Definition =>
3379 Analyze_Formal_Floating_Type (T, Def);
3380
3381 when N_Formal_Ordinary_Fixed_Point_Definition =>
3382 Analyze_Formal_Ordinary_Fixed_Point_Type (T, Def);
3383
3384 when N_Formal_Decimal_Fixed_Point_Definition =>
3385 Analyze_Formal_Decimal_Fixed_Point_Type (T, Def);
3386
3387 when N_Array_Type_Definition =>
3388 Analyze_Formal_Array_Type (T, Def);
3389
3390 when N_Access_Function_Definition
3391 | N_Access_Procedure_Definition
3392 | N_Access_To_Object_Definition
3393 =>
3394 Analyze_Generic_Access_Type (T, Def);
3395
3396 -- Ada 2005: a interface declaration is encoded as an abstract
3397 -- record declaration or a abstract type derivation.
3398
3399 when N_Record_Definition =>
3400 Analyze_Formal_Interface_Type (N, T, Def);
3401
3402 when N_Derived_Type_Definition =>
3403 Analyze_Formal_Derived_Interface_Type (N, T, Def);
3404
3405 when N_Error =>
3406 null;
3407
3408 when others =>
3409 raise Program_Error;
3410 end case;
3411
3412 -- A formal type declaration declares a type and its first
3413 -- subtype.
3414
3415 Set_Is_Generic_Type (T);
3416 Set_Is_First_Subtype (T);
3417
3418 if Has_Aspects (N) then
3419 Analyze_Aspect_Specifications (N, T);
3420 end if;
3421 end Analyze_Formal_Type_Declaration;
3422
3423 ------------------------------------
3424 -- Analyze_Function_Instantiation --
3425 ------------------------------------
3426
3427 procedure Analyze_Function_Instantiation (N : Node_Id) is
3428 begin
3429 Analyze_Subprogram_Instantiation (N, E_Function);
3430 end Analyze_Function_Instantiation;
3431
3432 ---------------------------------
3433 -- Analyze_Generic_Access_Type --
3434 ---------------------------------
3435
3436 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id) is
3437 begin
3438 Enter_Name (T);
3439
3440 if Nkind (Def) = N_Access_To_Object_Definition then
3441 Access_Type_Declaration (T, Def);
3442
3443 if Is_Incomplete_Or_Private_Type (Designated_Type (T))
3444 and then No (Full_View (Designated_Type (T)))
3445 and then not Is_Generic_Type (Designated_Type (T))
3446 then
3447 Error_Msg_N ("premature usage of incomplete type", Def);
3448
3449 elsif not Is_Entity_Name (Subtype_Indication (Def)) then
3450 Error_Msg_N
3451 ("only a subtype mark is allowed in a formal", Def);
3452 end if;
3453
3454 else
3455 Access_Subprogram_Declaration (T, Def);
3456 end if;
3457 end Analyze_Generic_Access_Type;
3458
3459 ---------------------------------
3460 -- Analyze_Generic_Formal_Part --
3461 ---------------------------------
3462
3463 procedure Analyze_Generic_Formal_Part (N : Node_Id) is
3464 Gen_Parm_Decl : Node_Id;
3465
3466 begin
3467 -- The generic formals are processed in the scope of the generic unit,
3468 -- where they are immediately visible. The scope is installed by the
3469 -- caller.
3470
3471 Gen_Parm_Decl := First (Generic_Formal_Declarations (N));
3472 while Present (Gen_Parm_Decl) loop
3473 Analyze (Gen_Parm_Decl);
3474 Next (Gen_Parm_Decl);
3475 end loop;
3476
3477 Generate_Reference_To_Generic_Formals (Current_Scope);
3478 end Analyze_Generic_Formal_Part;
3479
3480 ------------------------------------------
3481 -- Analyze_Generic_Package_Declaration --
3482 ------------------------------------------
3483
3484 procedure Analyze_Generic_Package_Declaration (N : Node_Id) is
3485 Decls : constant List_Id := Visible_Declarations (Specification (N));
3486 Loc : constant Source_Ptr := Sloc (N);
3487
3488 Decl : Node_Id;
3489 Id : Entity_Id;
3490 New_N : Node_Id;
3491 Renaming : Node_Id;
3492 Save_Parent : Node_Id;
3493
3494 begin
3495 Check_SPARK_05_Restriction ("generic is not allowed", N);
3496
3497 -- A generic may grant access to its private enclosing context depending
3498 -- on the placement of its corresponding body. From elaboration point of
3499 -- view, the flow of execution may enter this private context, and then
3500 -- reach an external unit, thus producing a dependency on that external
3501 -- unit. For such a path to be properly discovered and encoded in the
3502 -- ALI file of the main unit, let the ABE mechanism process the body of
3503 -- the main unit, and encode all relevant invocation constructs and the
3504 -- relations between them.
3505
3506 Mark_Save_Invocation_Graph_Of_Body;
3507
3508 -- We introduce a renaming of the enclosing package, to have a usable
3509 -- entity as the prefix of an expanded name for a local entity of the
3510 -- form Par.P.Q, where P is the generic package. This is because a local
3511 -- entity named P may hide it, so that the usual visibility rules in
3512 -- the instance will not resolve properly.
3513
3514 Renaming :=
3515 Make_Package_Renaming_Declaration (Loc,
3516 Defining_Unit_Name =>
3517 Make_Defining_Identifier (Loc,
3518 Chars => New_External_Name (Chars (Defining_Entity (N)), "GH")),
3519 Name =>
3520 Make_Identifier (Loc, Chars (Defining_Entity (N))));
3521
3522 -- The declaration is inserted before other declarations, but before
3523 -- pragmas that may be library-unit pragmas and must appear before other
3524 -- declarations. The pragma Compile_Time_Error is not in this class, and
3525 -- may contain an expression that includes such a qualified name, so the
3526 -- renaming declaration must appear before it.
3527
3528 -- Are there other pragmas that require this special handling ???
3529
3530 if Present (Decls) then
3531 Decl := First (Decls);
3532 while Present (Decl)
3533 and then Nkind (Decl) = N_Pragma
3534 and then Get_Pragma_Id (Decl) /= Pragma_Compile_Time_Error
3535 loop
3536 Next (Decl);
3537 end loop;
3538
3539 if Present (Decl) then
3540 Insert_Before (Decl, Renaming);
3541 else
3542 Append (Renaming, Visible_Declarations (Specification (N)));
3543 end if;
3544
3545 else
3546 Set_Visible_Declarations (Specification (N), New_List (Renaming));
3547 end if;
3548
3549 -- Create copy of generic unit, and save for instantiation. If the unit
3550 -- is a child unit, do not copy the specifications for the parent, which
3551 -- are not part of the generic tree.
3552
3553 Save_Parent := Parent_Spec (N);
3554 Set_Parent_Spec (N, Empty);
3555
3556 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
3557 Set_Parent_Spec (New_N, Save_Parent);
3558 Rewrite (N, New_N);
3559
3560 -- Once the contents of the generic copy and the template are swapped,
3561 -- do the same for their respective aspect specifications.
3562
3563 Exchange_Aspects (N, New_N);
3564
3565 -- Collect all contract-related source pragmas found within the template
3566 -- and attach them to the contract of the package spec. This contract is
3567 -- used in the capture of global references within annotations.
3568
3569 Create_Generic_Contract (N);
3570
3571 Id := Defining_Entity (N);
3572 Generate_Definition (Id);
3573
3574 -- Expansion is not applied to generic units
3575
3576 Start_Generic;
3577
3578 Enter_Name (Id);
3579 Set_Ekind (Id, E_Generic_Package);
3580 Set_Etype (Id, Standard_Void_Type);
3581
3582 -- Set SPARK_Mode from context
3583
3584 Set_SPARK_Pragma (Id, SPARK_Mode_Pragma);
3585 Set_SPARK_Aux_Pragma (Id, SPARK_Mode_Pragma);
3586 Set_SPARK_Pragma_Inherited (Id);
3587 Set_SPARK_Aux_Pragma_Inherited (Id);
3588
3589 -- Preserve relevant elaboration-related attributes of the context which
3590 -- are no longer available or very expensive to recompute once analysis,
3591 -- resolution, and expansion are over.
3592
3593 Mark_Elaboration_Attributes
3594 (N_Id => Id,
3595 Checks => True,
3596 Warnings => True);
3597
3598 -- Analyze aspects now, so that generated pragmas appear in the
3599 -- declarations before building and analyzing the generic copy.
3600
3601 if Has_Aspects (N) then
3602 Analyze_Aspect_Specifications (N, Id);
3603 end if;
3604
3605 Push_Scope (Id);
3606 Enter_Generic_Scope (Id);
3607 Set_Inner_Instances (Id, New_Elmt_List);
3608
3609 Set_Categorization_From_Pragmas (N);
3610 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3611
3612 -- Link the declaration of the generic homonym in the generic copy to
3613 -- the package it renames, so that it is always resolved properly.
3614
3615 Set_Generic_Homonym (Id, Defining_Unit_Name (Renaming));
3616 Set_Entity (Associated_Node (Name (Renaming)), Id);
3617
3618 -- For a library unit, we have reconstructed the entity for the unit,
3619 -- and must reset it in the library tables.
3620
3621 if Nkind (Parent (N)) = N_Compilation_Unit then
3622 Set_Cunit_Entity (Current_Sem_Unit, Id);
3623 end if;
3624
3625 Analyze_Generic_Formal_Part (N);
3626
3627 -- After processing the generic formals, analysis proceeds as for a
3628 -- non-generic package.
3629
3630 Analyze (Specification (N));
3631
3632 Validate_Categorization_Dependency (N, Id);
3633
3634 End_Generic;
3635
3636 End_Package_Scope (Id);
3637 Exit_Generic_Scope (Id);
3638
3639 -- If the generic appears within a package unit, the body of that unit
3640 -- has to be present for instantiation and inlining.
3641
3642 if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Declaration then
3643 Set_Body_Needed_For_Inlining
3644 (Defining_Entity (Unit (Cunit (Current_Sem_Unit))));
3645 end if;
3646
3647 if Nkind (Parent (N)) /= N_Compilation_Unit then
3648 Move_Freeze_Nodes (Id, N, Visible_Declarations (Specification (N)));
3649 Move_Freeze_Nodes (Id, N, Private_Declarations (Specification (N)));
3650 Move_Freeze_Nodes (Id, N, Generic_Formal_Declarations (N));
3651
3652 else
3653 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
3654 Validate_RT_RAT_Component (N);
3655
3656 -- If this is a spec without a body, check that generic parameters
3657 -- are referenced.
3658
3659 if not Body_Required (Parent (N)) then
3660 Check_References (Id);
3661 end if;
3662 end if;
3663
3664 -- If there is a specified storage pool in the context, create an
3665 -- aspect on the package declaration, so that it is used in any
3666 -- instance that does not override it.
3667
3668 if Present (Default_Pool) then
3669 declare
3670 ASN : Node_Id;
3671
3672 begin
3673 ASN :=
3674 Make_Aspect_Specification (Loc,
3675 Identifier => Make_Identifier (Loc, Name_Default_Storage_Pool),
3676 Expression => New_Copy (Default_Pool));
3677
3678 if No (Aspect_Specifications (Specification (N))) then
3679 Set_Aspect_Specifications (Specification (N), New_List (ASN));
3680 else
3681 Append (ASN, Aspect_Specifications (Specification (N)));
3682 end if;
3683 end;
3684 end if;
3685 end Analyze_Generic_Package_Declaration;
3686
3687 --------------------------------------------
3688 -- Analyze_Generic_Subprogram_Declaration --
3689 --------------------------------------------
3690
3691 procedure Analyze_Generic_Subprogram_Declaration (N : Node_Id) is
3692 Formals : List_Id;
3693 Id : Entity_Id;
3694 New_N : Node_Id;
3695 Result_Type : Entity_Id;
3696 Save_Parent : Node_Id;
3697 Spec : Node_Id;
3698 Typ : Entity_Id;
3699
3700 begin
3701 Check_SPARK_05_Restriction ("generic is not allowed", N);
3702
3703 -- A generic may grant access to its private enclosing context depending
3704 -- on the placement of its corresponding body. From elaboration point of
3705 -- view, the flow of execution may enter this private context, and then
3706 -- reach an external unit, thus producing a dependency on that external
3707 -- unit. For such a path to be properly discovered and encoded in the
3708 -- ALI file of the main unit, let the ABE mechanism process the body of
3709 -- the main unit, and encode all relevant invocation constructs and the
3710 -- relations between them.
3711
3712 Mark_Save_Invocation_Graph_Of_Body;
3713
3714 -- Create copy of generic unit, and save for instantiation. If the unit
3715 -- is a child unit, do not copy the specifications for the parent, which
3716 -- are not part of the generic tree.
3717
3718 Save_Parent := Parent_Spec (N);
3719 Set_Parent_Spec (N, Empty);
3720
3721 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
3722 Set_Parent_Spec (New_N, Save_Parent);
3723 Rewrite (N, New_N);
3724
3725 -- Once the contents of the generic copy and the template are swapped,
3726 -- do the same for their respective aspect specifications.
3727
3728 Exchange_Aspects (N, New_N);
3729
3730 -- Collect all contract-related source pragmas found within the template
3731 -- and attach them to the contract of the subprogram spec. This contract
3732 -- is used in the capture of global references within annotations.
3733
3734 Create_Generic_Contract (N);
3735
3736 Spec := Specification (N);
3737 Id := Defining_Entity (Spec);
3738 Generate_Definition (Id);
3739
3740 if Nkind (Id) = N_Defining_Operator_Symbol then
3741 Error_Msg_N
3742 ("operator symbol not allowed for generic subprogram", Id);
3743 end if;
3744
3745 Start_Generic;
3746
3747 Enter_Name (Id);
3748 Set_Scope_Depth_Value (Id, Scope_Depth (Current_Scope) + 1);
3749
3750 -- Analyze the aspects of the generic copy to ensure that all generated
3751 -- pragmas (if any) perform their semantic effects.
3752
3753 if Has_Aspects (N) then
3754 Analyze_Aspect_Specifications (N, Id);
3755 end if;
3756
3757 Push_Scope (Id);
3758 Enter_Generic_Scope (Id);
3759 Set_Inner_Instances (Id, New_Elmt_List);
3760 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3761
3762 Analyze_Generic_Formal_Part (N);
3763
3764 if Nkind (Spec) = N_Function_Specification then
3765 Set_Ekind (Id, E_Generic_Function);
3766 else
3767 Set_Ekind (Id, E_Generic_Procedure);
3768 end if;
3769
3770 -- Set SPARK_Mode from context
3771
3772 Set_SPARK_Pragma (Id, SPARK_Mode_Pragma);
3773 Set_SPARK_Pragma_Inherited (Id);
3774
3775 -- Preserve relevant elaboration-related attributes of the context which
3776 -- are no longer available or very expensive to recompute once analysis,
3777 -- resolution, and expansion are over.
3778
3779 Mark_Elaboration_Attributes
3780 (N_Id => Id,
3781 Checks => True,
3782 Warnings => True);
3783
3784 Formals := Parameter_Specifications (Spec);
3785
3786 if Present (Formals) then
3787 Process_Formals (Formals, Spec);
3788 end if;
3789
3790 if Nkind (Spec) = N_Function_Specification then
3791 if Nkind (Result_Definition (Spec)) = N_Access_Definition then
3792 Result_Type := Access_Definition (Spec, Result_Definition (Spec));
3793 Set_Etype (Id, Result_Type);
3794
3795 -- Check restriction imposed by AI05-073: a generic function
3796 -- cannot return an abstract type or an access to such.
3797
3798 -- This is a binding interpretation should it apply to earlier
3799 -- versions of Ada as well as Ada 2012???
3800
3801 if Is_Abstract_Type (Designated_Type (Result_Type))
3802 and then Ada_Version >= Ada_2012
3803 then
3804 Error_Msg_N
3805 ("generic function cannot have an access result "
3806 & "that designates an abstract type", Spec);
3807 end if;
3808
3809 else
3810 Find_Type (Result_Definition (Spec));
3811 Typ := Entity (Result_Definition (Spec));
3812
3813 if Is_Abstract_Type (Typ)
3814 and then Ada_Version >= Ada_2012
3815 then
3816 Error_Msg_N
3817 ("generic function cannot have abstract result type", Spec);
3818 end if;
3819
3820 -- If a null exclusion is imposed on the result type, then create
3821 -- a null-excluding itype (an access subtype) and use it as the
3822 -- function's Etype.
3823
3824 if Is_Access_Type (Typ)
3825 and then Null_Exclusion_Present (Spec)
3826 then
3827 Set_Etype (Id,
3828 Create_Null_Excluding_Itype
3829 (T => Typ,
3830 Related_Nod => Spec,
3831 Scope_Id => Defining_Unit_Name (Spec)));
3832 else
3833 Set_Etype (Id, Typ);
3834 end if;
3835 end if;
3836
3837 else
3838 Set_Etype (Id, Standard_Void_Type);
3839 end if;
3840
3841 -- For a library unit, we have reconstructed the entity for the unit,
3842 -- and must reset it in the library tables. We also make sure that
3843 -- Body_Required is set properly in the original compilation unit node.
3844
3845 if Nkind (Parent (N)) = N_Compilation_Unit then
3846 Set_Cunit_Entity (Current_Sem_Unit, Id);
3847 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
3848 end if;
3849
3850 -- If the generic appears within a package unit, the body of that unit
3851 -- has to be present for instantiation and inlining.
3852
3853 if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Declaration
3854 and then Unit_Requires_Body (Id)
3855 then
3856 Set_Body_Needed_For_Inlining
3857 (Defining_Entity (Unit (Cunit (Current_Sem_Unit))));
3858 end if;
3859
3860 Set_Categorization_From_Pragmas (N);
3861 Validate_Categorization_Dependency (N, Id);
3862
3863 -- Capture all global references that occur within the profile of the
3864 -- generic subprogram. Aspects are not part of this processing because
3865 -- they must be delayed. If processed now, Save_Global_References will
3866 -- destroy the Associated_Node links and prevent the capture of global
3867 -- references when the contract of the generic subprogram is analyzed.
3868
3869 Save_Global_References (Original_Node (N));
3870
3871 End_Generic;
3872 End_Scope;
3873 Exit_Generic_Scope (Id);
3874 Generate_Reference_To_Formals (Id);
3875
3876 List_Inherited_Pre_Post_Aspects (Id);
3877 end Analyze_Generic_Subprogram_Declaration;
3878
3879 -----------------------------------
3880 -- Analyze_Package_Instantiation --
3881 -----------------------------------
3882
3883 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
3884 -- must be replaced by gotos which jump to the end of the routine in order
3885 -- to restore the Ghost and SPARK modes.
3886
3887 procedure Analyze_Package_Instantiation (N : Node_Id) is
3888 Has_Inline_Always : Boolean := False;
3889 -- Set if the generic unit contains any subprograms with Inline_Always.
3890 -- Only relevant when back-end inlining is not enabled.
3891
3892 function Might_Inline_Subp (Gen_Unit : Entity_Id) return Boolean;
3893 -- Return True if inlining is active and Gen_Unit contains inlined
3894 -- subprograms. In this case, we may either instantiate the body when
3895 -- front-end inlining is enabled, or add a pending instantiation when
3896 -- back-end inlining is enabled. In the former case, this may cause
3897 -- superfluous instantiations, but in either case we need to perform
3898 -- the instantiation of the body in the context of the instance and
3899 -- not in that of the point of inlining.
3900
3901 function Needs_Body_Instantiated (Gen_Unit : Entity_Id) return Boolean;
3902 -- Return True if Gen_Unit needs to have its body instantiated in the
3903 -- context of N. This in particular excludes generic contexts.
3904
3905 -----------------------
3906 -- Might_Inline_Subp --
3907 -----------------------
3908
3909 function Might_Inline_Subp (Gen_Unit : Entity_Id) return Boolean is
3910 E : Entity_Id;
3911
3912 begin
3913 if Inline_Processing_Required then
3914 -- No need to recompute the answer if we know it is positive
3915 -- and back-end inlining is enabled.
3916
3917 if Is_Inlined (Gen_Unit) and then Back_End_Inlining then
3918 return True;
3919 end if;
3920
3921 E := First_Entity (Gen_Unit);
3922 while Present (E) loop
3923 if Is_Subprogram (E) and then Is_Inlined (E) then
3924 -- Remember if there are any subprograms with Inline_Always
3925
3926 if Has_Pragma_Inline_Always (E) then
3927 Has_Inline_Always := True;
3928 end if;
3929
3930 Set_Is_Inlined (Gen_Unit);
3931 return True;
3932 end if;
3933
3934 Next_Entity (E);
3935 end loop;
3936 end if;
3937
3938 return False;
3939 end Might_Inline_Subp;
3940
3941 -------------------------------
3942 -- Needs_Body_Instantiated --
3943 -------------------------------
3944
3945 function Needs_Body_Instantiated (Gen_Unit : Entity_Id) return Boolean is
3946 begin
3947 -- No need to instantiate bodies in generic units
3948
3949 if Is_Generic_Unit (Cunit_Entity (Main_Unit)) then
3950 return False;
3951 end if;
3952
3953 -- If the instantiation is in the main unit, then the body is needed
3954
3955 if Is_In_Main_Unit (N) then
3956 return True;
3957 end if;
3958
3959 -- If not, then again no need to instantiate bodies in generic units
3960
3961 if Is_Generic_Unit (Cunit_Entity (Get_Code_Unit (N))) then
3962 return False;
3963 end if;
3964
3965 -- Here we have a special handling for back-end inlining: if inline
3966 -- processing is required, then we unconditionally want to have the
3967 -- body instantiated. The reason is that Might_Inline_Subp does not
3968 -- catch all the cases (as it does not recurse into nested packages)
3969 -- so this avoids the need to patch things up afterwards. Moreover,
3970 -- these instantiations are only performed on demand when back-end
3971 -- inlining is enabled, so this causes very little extra work.
3972
3973 if Inline_Processing_Required and then Back_End_Inlining then
3974 return True;
3975 end if;
3976
3977 -- We want to have the bodies instantiated in non-main units if
3978 -- they might contribute inlined subprograms.
3979
3980 return Might_Inline_Subp (Gen_Unit);
3981 end Needs_Body_Instantiated;
3982
3983 -- Local declarations
3984
3985 Gen_Id : constant Node_Id := Name (N);
3986 Inst_Id : constant Entity_Id := Defining_Entity (N);
3987 Is_Actual_Pack : constant Boolean := Is_Internal (Inst_Id);
3988 Loc : constant Source_Ptr := Sloc (N);
3989
3990 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
3991 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
3992 Saved_ISMP : constant Boolean :=
3993 Ignore_SPARK_Mode_Pragmas_In_Instance;
3994 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
3995 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
3996 -- Save the Ghost and SPARK mode-related data to restore on exit
3997
3998 Saved_Style_Check : constant Boolean := Style_Check;
3999 -- Save style check mode for restore on exit
4000
4001 Act_Decl : Node_Id;
4002 Act_Decl_Name : Node_Id;
4003 Act_Decl_Id : Entity_Id;
4004 Act_Spec : Node_Id;
4005 Act_Tree : Node_Id;
4006 Env_Installed : Boolean := False;
4007 Gen_Decl : Node_Id;
4008 Gen_Spec : Node_Id;
4009 Gen_Unit : Entity_Id;
4010 Inline_Now : Boolean := False;
4011 Needs_Body : Boolean;
4012 Parent_Installed : Boolean := False;
4013 Renaming_List : List_Id;
4014 Unit_Renaming : Node_Id;
4015
4016 Vis_Prims_List : Elist_Id := No_Elist;
4017 -- List of primitives made temporarily visible in the instantiation
4018 -- to match the visibility of the formal type
4019
4020 -- Start of processing for Analyze_Package_Instantiation
4021
4022 begin
4023 -- Preserve relevant elaboration-related attributes of the context which
4024 -- are no longer available or very expensive to recompute once analysis,
4025 -- resolution, and expansion are over.
4026
4027 Mark_Elaboration_Attributes
4028 (N_Id => N,
4029 Checks => True,
4030 Level => True,
4031 Modes => True,
4032 Warnings => True);
4033
4034 Check_SPARK_05_Restriction ("generic is not allowed", N);
4035
4036 -- Very first thing: check for Text_IO special unit in case we are
4037 -- instantiating one of the children of [[Wide_]Wide_]Text_IO.
4038
4039 Check_Text_IO_Special_Unit (Name (N));
4040
4041 -- Make node global for error reporting
4042
4043 Instantiation_Node := N;
4044
4045 -- Case of instantiation of a generic package
4046
4047 if Nkind (N) = N_Package_Instantiation then
4048 Act_Decl_Id := New_Copy (Defining_Entity (N));
4049 Set_Comes_From_Source (Act_Decl_Id, True);
4050
4051 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name then
4052 Act_Decl_Name :=
4053 Make_Defining_Program_Unit_Name (Loc,
4054 Name =>
4055 New_Copy_Tree (Name (Defining_Unit_Name (N))),
4056 Defining_Identifier => Act_Decl_Id);
4057 else
4058 Act_Decl_Name := Act_Decl_Id;
4059 end if;
4060
4061 -- Case of instantiation of a formal package
4062
4063 else
4064 Act_Decl_Id := Defining_Identifier (N);
4065 Act_Decl_Name := Act_Decl_Id;
4066 end if;
4067
4068 Generate_Definition (Act_Decl_Id);
4069 Set_Ekind (Act_Decl_Id, E_Package);
4070
4071 -- Initialize list of incomplete actuals before analysis
4072
4073 Set_Incomplete_Actuals (Act_Decl_Id, New_Elmt_List);
4074
4075 Preanalyze_Actuals (N, Act_Decl_Id);
4076
4077 -- Turn off style checking in instances. If the check is enabled on the
4078 -- generic unit, a warning in an instance would just be noise. If not
4079 -- enabled on the generic, then a warning in an instance is just wrong.
4080 -- This must be done after analyzing the actuals, which do come from
4081 -- source and are subject to style checking.
4082
4083 Style_Check := False;
4084
4085 Init_Env;
4086 Env_Installed := True;
4087
4088 -- Reset renaming map for formal types. The mapping is established
4089 -- when analyzing the generic associations, but some mappings are
4090 -- inherited from formal packages of parent units, and these are
4091 -- constructed when the parents are installed.
4092
4093 Generic_Renamings.Set_Last (0);
4094 Generic_Renamings_HTable.Reset;
4095
4096 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
4097 Gen_Unit := Entity (Gen_Id);
4098
4099 -- A package instantiation is Ghost when it is subject to pragma Ghost
4100 -- or the generic template is Ghost. Set the mode now to ensure that
4101 -- any nodes generated during analysis and expansion are marked as
4102 -- Ghost.
4103
4104 Mark_And_Set_Ghost_Instantiation (N, Gen_Unit);
4105
4106 -- Verify that it is the name of a generic package
4107
4108 -- A visibility glitch: if the instance is a child unit and the generic
4109 -- is the generic unit of a parent instance (i.e. both the parent and
4110 -- the child units are instances of the same package) the name now
4111 -- denotes the renaming within the parent, not the intended generic
4112 -- unit. See if there is a homonym that is the desired generic. The
4113 -- renaming declaration must be visible inside the instance of the
4114 -- child, but not when analyzing the name in the instantiation itself.
4115
4116 if Ekind (Gen_Unit) = E_Package
4117 and then Present (Renamed_Entity (Gen_Unit))
4118 and then In_Open_Scopes (Renamed_Entity (Gen_Unit))
4119 and then Is_Generic_Instance (Renamed_Entity (Gen_Unit))
4120 and then Present (Homonym (Gen_Unit))
4121 then
4122 Gen_Unit := Homonym (Gen_Unit);
4123 end if;
4124
4125 if Etype (Gen_Unit) = Any_Type then
4126 Restore_Env;
4127 goto Leave;
4128
4129 elsif Ekind (Gen_Unit) /= E_Generic_Package then
4130
4131 -- Ada 2005 (AI-50217): Cannot use instance in limited with_clause
4132
4133 if From_Limited_With (Gen_Unit) then
4134 Error_Msg_N
4135 ("cannot instantiate a limited withed package", Gen_Id);
4136 else
4137 Error_Msg_NE
4138 ("& is not the name of a generic package", Gen_Id, Gen_Unit);
4139 end if;
4140
4141 Restore_Env;
4142 goto Leave;
4143 end if;
4144
4145 if In_Extended_Main_Source_Unit (N) then
4146 Set_Is_Instantiated (Gen_Unit);
4147 Generate_Reference (Gen_Unit, N);
4148
4149 if Present (Renamed_Object (Gen_Unit)) then
4150 Set_Is_Instantiated (Renamed_Object (Gen_Unit));
4151 Generate_Reference (Renamed_Object (Gen_Unit), N);
4152 end if;
4153 end if;
4154
4155 if Nkind (Gen_Id) = N_Identifier
4156 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
4157 then
4158 Error_Msg_NE
4159 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
4160
4161 elsif Nkind (Gen_Id) = N_Expanded_Name
4162 and then Is_Child_Unit (Gen_Unit)
4163 and then Nkind (Prefix (Gen_Id)) = N_Identifier
4164 and then Chars (Act_Decl_Id) = Chars (Prefix (Gen_Id))
4165 then
4166 Error_Msg_N
4167 ("& is hidden within declaration of instance ", Prefix (Gen_Id));
4168 end if;
4169
4170 Set_Entity (Gen_Id, Gen_Unit);
4171
4172 -- If generic is a renaming, get original generic unit
4173
4174 if Present (Renamed_Object (Gen_Unit))
4175 and then Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Package
4176 then
4177 Gen_Unit := Renamed_Object (Gen_Unit);
4178 end if;
4179
4180 -- Verify that there are no circular instantiations
4181
4182 if In_Open_Scopes (Gen_Unit) then
4183 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
4184 Restore_Env;
4185 goto Leave;
4186
4187 elsif Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
4188 Error_Msg_Node_2 := Current_Scope;
4189 Error_Msg_NE
4190 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
4191 Circularity_Detected := True;
4192 Restore_Env;
4193 goto Leave;
4194
4195 else
4196 Set_Ekind (Inst_Id, E_Package);
4197 Set_Scope (Inst_Id, Current_Scope);
4198
4199 -- If the context of the instance is subject to SPARK_Mode "off" or
4200 -- the annotation is altogether missing, set the global flag which
4201 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
4202 -- the instance.
4203
4204 if SPARK_Mode /= On then
4205 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
4206
4207 -- Mark the instance spec in case the body is instantiated at a
4208 -- later pass. This preserves the original context in effect for
4209 -- the body.
4210
4211 Set_Ignore_SPARK_Mode_Pragmas (Act_Decl_Id);
4212 end if;
4213
4214 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
4215 Gen_Spec := Specification (Gen_Decl);
4216
4217 -- Initialize renamings map, for error checking, and the list that
4218 -- holds private entities whose views have changed between generic
4219 -- definition and instantiation. If this is the instance created to
4220 -- validate an actual package, the instantiation environment is that
4221 -- of the enclosing instance.
4222
4223 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
4224
4225 -- Copy original generic tree, to produce text for instantiation
4226
4227 Act_Tree :=
4228 Copy_Generic_Node
4229 (Original_Node (Gen_Decl), Empty, Instantiating => True);
4230
4231 Act_Spec := Specification (Act_Tree);
4232
4233 -- If this is the instance created to validate an actual package,
4234 -- only the formals matter, do not examine the package spec itself.
4235
4236 if Is_Actual_Pack then
4237 Set_Visible_Declarations (Act_Spec, New_List);
4238 Set_Private_Declarations (Act_Spec, New_List);
4239 end if;
4240
4241 Renaming_List :=
4242 Analyze_Associations
4243 (I_Node => N,
4244 Formals => Generic_Formal_Declarations (Act_Tree),
4245 F_Copy => Generic_Formal_Declarations (Gen_Decl));
4246
4247 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
4248
4249 Set_Instance_Env (Gen_Unit, Act_Decl_Id);
4250 Set_Defining_Unit_Name (Act_Spec, Act_Decl_Name);
4251 Set_Is_Generic_Instance (Act_Decl_Id);
4252 Set_Generic_Parent (Act_Spec, Gen_Unit);
4253
4254 -- References to the generic in its own declaration or its body are
4255 -- references to the instance. Add a renaming declaration for the
4256 -- generic unit itself. This declaration, as well as the renaming
4257 -- declarations for the generic formals, must remain private to the
4258 -- unit: the formals, because this is the language semantics, and
4259 -- the unit because its use is an artifact of the implementation.
4260
4261 Unit_Renaming :=
4262 Make_Package_Renaming_Declaration (Loc,
4263 Defining_Unit_Name =>
4264 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
4265 Name => New_Occurrence_Of (Act_Decl_Id, Loc));
4266
4267 Append (Unit_Renaming, Renaming_List);
4268
4269 -- The renaming declarations are the first local declarations of the
4270 -- new unit.
4271
4272 if Is_Non_Empty_List (Visible_Declarations (Act_Spec)) then
4273 Insert_List_Before
4274 (First (Visible_Declarations (Act_Spec)), Renaming_List);
4275 else
4276 Set_Visible_Declarations (Act_Spec, Renaming_List);
4277 end if;
4278
4279 Act_Decl := Make_Package_Declaration (Loc, Specification => Act_Spec);
4280
4281 -- Propagate the aspect specifications from the package declaration
4282 -- template to the instantiated version of the package declaration.
4283
4284 if Has_Aspects (Act_Tree) then
4285 Set_Aspect_Specifications (Act_Decl,
4286 New_Copy_List_Tree (Aspect_Specifications (Act_Tree)));
4287 end if;
4288
4289 -- The generic may have a generated Default_Storage_Pool aspect,
4290 -- set at the point of generic declaration. If the instance has
4291 -- that aspect, it overrides the one inherited from the generic.
4292
4293 if Has_Aspects (Gen_Spec) then
4294 if No (Aspect_Specifications (N)) then
4295 Set_Aspect_Specifications (N,
4296 (New_Copy_List_Tree
4297 (Aspect_Specifications (Gen_Spec))));
4298
4299 else
4300 declare
4301 Inherited_Aspects : constant List_Id :=
4302 New_Copy_List_Tree
4303 (Aspect_Specifications (Gen_Spec));
4304
4305 ASN1 : Node_Id;
4306 ASN2 : Node_Id;
4307 Pool_Present : Boolean := False;
4308
4309 begin
4310 ASN1 := First (Aspect_Specifications (N));
4311 while Present (ASN1) loop
4312 if Chars (Identifier (ASN1)) =
4313 Name_Default_Storage_Pool
4314 then
4315 Pool_Present := True;
4316 exit;
4317 end if;
4318
4319 Next (ASN1);
4320 end loop;
4321
4322 if Pool_Present then
4323
4324 -- If generic carries a default storage pool, remove it
4325 -- in favor of the instance one.
4326
4327 ASN2 := First (Inherited_Aspects);
4328 while Present (ASN2) loop
4329 if Chars (Identifier (ASN2)) =
4330 Name_Default_Storage_Pool
4331 then
4332 Remove (ASN2);
4333 exit;
4334 end if;
4335
4336 Next (ASN2);
4337 end loop;
4338 end if;
4339
4340 Prepend_List_To
4341 (Aspect_Specifications (N), Inherited_Aspects);
4342 end;
4343 end if;
4344 end if;
4345
4346 -- Save the instantiation node for a subsequent instantiation of the
4347 -- body if there is one and it needs to be instantiated here.
4348
4349 -- We instantiate the body only if we are generating code, or if we
4350 -- are generating cross-reference information, or for GNATprove use.
4351
4352 declare
4353 Enclosing_Body_Present : Boolean := False;
4354 -- If the generic unit is not a compilation unit, then a body may
4355 -- be present in its parent even if none is required. We create a
4356 -- tentative pending instantiation for the body, which will be
4357 -- discarded if none is actually present.
4358
4359 Scop : Entity_Id;
4360
4361 begin
4362 if Scope (Gen_Unit) /= Standard_Standard
4363 and then not Is_Child_Unit (Gen_Unit)
4364 then
4365 Scop := Scope (Gen_Unit);
4366 while Present (Scop) and then Scop /= Standard_Standard loop
4367 if Unit_Requires_Body (Scop) then
4368 Enclosing_Body_Present := True;
4369 exit;
4370
4371 elsif In_Open_Scopes (Scop)
4372 and then In_Package_Body (Scop)
4373 then
4374 Enclosing_Body_Present := True;
4375 exit;
4376 end if;
4377
4378 exit when Is_Compilation_Unit (Scop);
4379 Scop := Scope (Scop);
4380 end loop;
4381 end if;
4382
4383 -- If front-end inlining is enabled or there are any subprograms
4384 -- marked with Inline_Always, and this is a unit for which code
4385 -- will be generated, we instantiate the body at once.
4386
4387 -- This is done if the instance is not the main unit, and if the
4388 -- generic is not a child unit of another generic, to avoid scope
4389 -- problems and the reinstallation of parent instances.
4390
4391 if Expander_Active
4392 and then (not Is_Child_Unit (Gen_Unit)
4393 or else not Is_Generic_Unit (Scope (Gen_Unit)))
4394 and then Might_Inline_Subp (Gen_Unit)
4395 and then not Is_Actual_Pack
4396 then
4397 if not Back_End_Inlining
4398 and then (Front_End_Inlining or else Has_Inline_Always)
4399 and then (Is_In_Main_Unit (N)
4400 or else In_Main_Context (Current_Scope))
4401 and then Nkind (Parent (N)) /= N_Compilation_Unit
4402 then
4403 Inline_Now := True;
4404
4405 -- In configurable_run_time mode we force the inlining of
4406 -- predefined subprograms marked Inline_Always, to minimize
4407 -- the use of the run-time library.
4408
4409 elsif In_Predefined_Unit (Gen_Decl)
4410 and then Configurable_Run_Time_Mode
4411 and then Nkind (Parent (N)) /= N_Compilation_Unit
4412 then
4413 Inline_Now := True;
4414 end if;
4415
4416 -- If the current scope is itself an instance within a child
4417 -- unit, there will be duplications in the scope stack, and the
4418 -- unstacking mechanism in Inline_Instance_Body will fail.
4419 -- This loses some rare cases of optimization, and might be
4420 -- improved some day, if we can find a proper abstraction for
4421 -- "the complete compilation context" that can be saved and
4422 -- restored. ???
4423
4424 if Is_Generic_Instance (Current_Scope) then
4425 declare
4426 Curr_Unit : constant Entity_Id :=
4427 Cunit_Entity (Current_Sem_Unit);
4428 begin
4429 if Curr_Unit /= Current_Scope
4430 and then Is_Child_Unit (Curr_Unit)
4431 then
4432 Inline_Now := False;
4433 end if;
4434 end;
4435 end if;
4436 end if;
4437
4438 Needs_Body :=
4439 (Unit_Requires_Body (Gen_Unit)
4440 or else Enclosing_Body_Present
4441 or else Present (Corresponding_Body (Gen_Decl)))
4442 and then Needs_Body_Instantiated (Gen_Unit)
4443 and then not Is_Actual_Pack
4444 and then not Inline_Now
4445 and then (Operating_Mode = Generate_Code
4446 or else (Operating_Mode = Check_Semantics
4447 and then GNATprove_Mode));
4448
4449 -- If front-end inlining is enabled or there are any subprograms
4450 -- marked with Inline_Always, do not instantiate body when within
4451 -- a generic context.
4452
4453 if not Back_End_Inlining
4454 and then (Front_End_Inlining or else Has_Inline_Always)
4455 and then not Expander_Active
4456 then
4457 Needs_Body := False;
4458 end if;
4459
4460 -- If the current context is generic, and the package being
4461 -- instantiated is declared within a formal package, there is no
4462 -- body to instantiate until the enclosing generic is instantiated
4463 -- and there is an actual for the formal package. If the formal
4464 -- package has parameters, we build a regular package instance for
4465 -- it, that precedes the original formal package declaration.
4466
4467 if In_Open_Scopes (Scope (Scope (Gen_Unit))) then
4468 declare
4469 Decl : constant Node_Id :=
4470 Original_Node
4471 (Unit_Declaration_Node (Scope (Gen_Unit)));
4472 begin
4473 if Nkind (Decl) = N_Formal_Package_Declaration
4474 or else (Nkind (Decl) = N_Package_Declaration
4475 and then Is_List_Member (Decl)
4476 and then Present (Next (Decl))
4477 and then
4478 Nkind (Next (Decl)) =
4479 N_Formal_Package_Declaration)
4480 then
4481 Needs_Body := False;
4482 end if;
4483 end;
4484 end if;
4485 end;
4486
4487 -- For RCI unit calling stubs, we omit the instance body if the
4488 -- instance is the RCI library unit itself.
4489
4490 -- However there is a special case for nested instances: in this case
4491 -- we do generate the instance body, as it might be required, e.g.
4492 -- because it provides stream attributes for some type used in the
4493 -- profile of a remote subprogram. This is consistent with 12.3(12),
4494 -- which indicates that the instance body occurs at the place of the
4495 -- instantiation, and thus is part of the RCI declaration, which is
4496 -- present on all client partitions (this is E.2.3(18)).
4497
4498 -- Note that AI12-0002 may make it illegal at some point to have
4499 -- stream attributes defined in an RCI unit, in which case this
4500 -- special case will become unnecessary. In the meantime, there
4501 -- is known application code in production that depends on this
4502 -- being possible, so we definitely cannot eliminate the body in
4503 -- the case of nested instances for the time being.
4504
4505 -- When we generate a nested instance body, calling stubs for any
4506 -- relevant subprogram will be inserted immediately after the
4507 -- subprogram declarations, and will take precedence over the
4508 -- subsequent (original) body. (The stub and original body will be
4509 -- complete homographs, but this is permitted in an instance).
4510 -- (Could we do better and remove the original body???)
4511
4512 if Distribution_Stub_Mode = Generate_Caller_Stub_Body
4513 and then Comes_From_Source (N)
4514 and then Nkind (Parent (N)) = N_Compilation_Unit
4515 then
4516 Needs_Body := False;
4517 end if;
4518
4519 if Needs_Body then
4520 -- Indicate that the enclosing scopes contain an instantiation,
4521 -- and that cleanup actions should be delayed until after the
4522 -- instance body is expanded.
4523
4524 Check_Forward_Instantiation (Gen_Decl);
4525 if Nkind (N) = N_Package_Instantiation then
4526 declare
4527 Enclosing_Master : Entity_Id;
4528
4529 begin
4530 -- Loop to search enclosing masters
4531
4532 Enclosing_Master := Current_Scope;
4533 Scope_Loop : while Enclosing_Master /= Standard_Standard loop
4534 if Ekind (Enclosing_Master) = E_Package then
4535 if Is_Compilation_Unit (Enclosing_Master) then
4536 if In_Package_Body (Enclosing_Master) then
4537 Set_Delay_Subprogram_Descriptors
4538 (Body_Entity (Enclosing_Master));
4539 else
4540 Set_Delay_Subprogram_Descriptors
4541 (Enclosing_Master);
4542 end if;
4543
4544 exit Scope_Loop;
4545
4546 else
4547 Enclosing_Master := Scope (Enclosing_Master);
4548 end if;
4549
4550 elsif Is_Generic_Unit (Enclosing_Master)
4551 or else Ekind (Enclosing_Master) = E_Void
4552 then
4553 -- Cleanup actions will eventually be performed on the
4554 -- enclosing subprogram or package instance, if any.
4555 -- Enclosing scope is void in the formal part of a
4556 -- generic subprogram.
4557
4558 exit Scope_Loop;
4559
4560 else
4561 if Ekind (Enclosing_Master) = E_Entry
4562 and then
4563 Ekind (Scope (Enclosing_Master)) = E_Protected_Type
4564 then
4565 if not Expander_Active then
4566 exit Scope_Loop;
4567 else
4568 Enclosing_Master :=
4569 Protected_Body_Subprogram (Enclosing_Master);
4570 end if;
4571 end if;
4572
4573 Set_Delay_Cleanups (Enclosing_Master);
4574
4575 while Ekind (Enclosing_Master) = E_Block loop
4576 Enclosing_Master := Scope (Enclosing_Master);
4577 end loop;
4578
4579 if Is_Subprogram (Enclosing_Master) then
4580 Set_Delay_Subprogram_Descriptors (Enclosing_Master);
4581
4582 elsif Is_Task_Type (Enclosing_Master) then
4583 declare
4584 TBP : constant Node_Id :=
4585 Get_Task_Body_Procedure
4586 (Enclosing_Master);
4587 begin
4588 if Present (TBP) then
4589 Set_Delay_Subprogram_Descriptors (TBP);
4590 Set_Delay_Cleanups (TBP);
4591 end if;
4592 end;
4593 end if;
4594
4595 exit Scope_Loop;
4596 end if;
4597 end loop Scope_Loop;
4598 end;
4599
4600 -- Make entry in table
4601
4602 Add_Pending_Instantiation (N, Act_Decl);
4603 end if;
4604 end if;
4605
4606 Set_Categorization_From_Pragmas (Act_Decl);
4607
4608 if Parent_Installed then
4609 Hide_Current_Scope;
4610 end if;
4611
4612 Set_Instance_Spec (N, Act_Decl);
4613
4614 -- If not a compilation unit, insert the package declaration before
4615 -- the original instantiation node.
4616
4617 if Nkind (Parent (N)) /= N_Compilation_Unit then
4618 Mark_Rewrite_Insertion (Act_Decl);
4619 Insert_Before (N, Act_Decl);
4620
4621 if Has_Aspects (N) then
4622 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4623
4624 -- The pragma created for a Default_Storage_Pool aspect must
4625 -- appear ahead of the declarations in the instance spec.
4626 -- Analysis has placed it after the instance node, so remove
4627 -- it and reinsert it properly now.
4628
4629 declare
4630 ASN : constant Node_Id := First (Aspect_Specifications (N));
4631 A_Name : constant Name_Id := Chars (Identifier (ASN));
4632 Decl : Node_Id;
4633
4634 begin
4635 if A_Name = Name_Default_Storage_Pool then
4636 if No (Visible_Declarations (Act_Spec)) then
4637 Set_Visible_Declarations (Act_Spec, New_List);
4638 end if;
4639
4640 Decl := Next (N);
4641 while Present (Decl) loop
4642 if Nkind (Decl) = N_Pragma then
4643 Remove (Decl);
4644 Prepend (Decl, Visible_Declarations (Act_Spec));
4645 exit;
4646 end if;
4647
4648 Next (Decl);
4649 end loop;
4650 end if;
4651 end;
4652 end if;
4653
4654 Analyze (Act_Decl);
4655
4656 -- For an instantiation that is a compilation unit, place
4657 -- declaration on current node so context is complete for analysis
4658 -- (including nested instantiations). If this is the main unit,
4659 -- the declaration eventually replaces the instantiation node.
4660 -- If the instance body is created later, it replaces the
4661 -- instance node, and the declaration is attached to it
4662 -- (see Build_Instance_Compilation_Unit_Nodes).
4663
4664 else
4665 if Cunit_Entity (Current_Sem_Unit) = Defining_Entity (N) then
4666
4667 -- The entity for the current unit is the newly created one,
4668 -- and all semantic information is attached to it.
4669
4670 Set_Cunit_Entity (Current_Sem_Unit, Act_Decl_Id);
4671
4672 -- If this is the main unit, replace the main entity as well
4673
4674 if Current_Sem_Unit = Main_Unit then
4675 Main_Unit_Entity := Act_Decl_Id;
4676 end if;
4677 end if;
4678
4679 Set_Unit (Parent (N), Act_Decl);
4680 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
4681 Set_Package_Instantiation (Act_Decl_Id, N);
4682
4683 -- Process aspect specifications of the instance node, if any, to
4684 -- take into account categorization pragmas before analyzing the
4685 -- instance.
4686
4687 if Has_Aspects (N) then
4688 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4689 end if;
4690
4691 Analyze (Act_Decl);
4692 Set_Unit (Parent (N), N);
4693 Set_Body_Required (Parent (N), False);
4694
4695 -- We never need elaboration checks on instantiations, since by
4696 -- definition, the body instantiation is elaborated at the same
4697 -- time as the spec instantiation.
4698
4699 if Legacy_Elaboration_Checks then
4700 Set_Kill_Elaboration_Checks (Act_Decl_Id);
4701 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
4702 end if;
4703 end if;
4704
4705 if Legacy_Elaboration_Checks then
4706 Check_Elab_Instantiation (N);
4707 end if;
4708
4709 -- Save the scenario for later examination by the ABE Processing
4710 -- phase.
4711
4712 Record_Elaboration_Scenario (N);
4713
4714 -- The instantiation results in a guaranteed ABE
4715
4716 if Is_Known_Guaranteed_ABE (N) and then Needs_Body then
4717 -- Do not instantiate the corresponding body because gigi cannot
4718 -- handle certain types of premature instantiations.
4719
4720 Remove_Dead_Instance (N);
4721
4722 -- Create completing bodies for all subprogram declarations since
4723 -- their real bodies will not be instantiated.
4724
4725 Provide_Completing_Bodies (Instance_Spec (N));
4726 end if;
4727
4728 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
4729
4730 Set_First_Private_Entity (Defining_Unit_Name (Unit_Renaming),
4731 First_Private_Entity (Act_Decl_Id));
4732
4733 -- If the instantiation will receive a body, the unit will be
4734 -- transformed into a package body, and receive its own elaboration
4735 -- entity. Otherwise, the nature of the unit is now a package
4736 -- declaration.
4737
4738 if Nkind (Parent (N)) = N_Compilation_Unit
4739 and then not Needs_Body
4740 then
4741 Rewrite (N, Act_Decl);
4742 end if;
4743
4744 if Present (Corresponding_Body (Gen_Decl))
4745 or else Unit_Requires_Body (Gen_Unit)
4746 then
4747 Set_Has_Completion (Act_Decl_Id);
4748 end if;
4749
4750 Check_Formal_Packages (Act_Decl_Id);
4751
4752 Restore_Hidden_Primitives (Vis_Prims_List);
4753 Restore_Private_Views (Act_Decl_Id);
4754
4755 Inherit_Context (Gen_Decl, N);
4756
4757 if Parent_Installed then
4758 Remove_Parent;
4759 end if;
4760
4761 Restore_Env;
4762 Env_Installed := False;
4763 end if;
4764
4765 Validate_Categorization_Dependency (N, Act_Decl_Id);
4766
4767 -- There used to be a check here to prevent instantiations in local
4768 -- contexts if the No_Local_Allocators restriction was active. This
4769 -- check was removed by a binding interpretation in AI-95-00130/07,
4770 -- but we retain the code for documentation purposes.
4771
4772 -- if Ekind (Act_Decl_Id) /= E_Void
4773 -- and then not Is_Library_Level_Entity (Act_Decl_Id)
4774 -- then
4775 -- Check_Restriction (No_Local_Allocators, N);
4776 -- end if;
4777
4778 if Inline_Now then
4779 Inline_Instance_Body (N, Gen_Unit, Act_Decl);
4780 end if;
4781
4782 -- Check that if N is an instantiation of System.Dim_Float_IO or
4783 -- System.Dim_Integer_IO, the formal type has a dimension system.
4784
4785 if Nkind (N) = N_Package_Instantiation
4786 and then Is_Dim_IO_Package_Instantiation (N)
4787 then
4788 declare
4789 Assoc : constant Node_Id := First (Generic_Associations (N));
4790 begin
4791 if not Has_Dimension_System
4792 (Etype (Explicit_Generic_Actual_Parameter (Assoc)))
4793 then
4794 Error_Msg_N ("type with a dimension system expected", Assoc);
4795 end if;
4796 end;
4797 end if;
4798
4799 <<Leave>>
4800 if Has_Aspects (N) and then Nkind (Parent (N)) /= N_Compilation_Unit then
4801 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4802 end if;
4803
4804 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
4805 Restore_Ghost_Region (Saved_GM, Saved_IGR);
4806 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
4807 Style_Check := Saved_Style_Check;
4808
4809 exception
4810 when Instantiation_Error =>
4811 if Parent_Installed then
4812 Remove_Parent;
4813 end if;
4814
4815 if Env_Installed then
4816 Restore_Env;
4817 end if;
4818
4819 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
4820 Restore_Ghost_Region (Saved_GM, Saved_IGR);
4821 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
4822 Style_Check := Saved_Style_Check;
4823 end Analyze_Package_Instantiation;
4824
4825 --------------------------
4826 -- Inline_Instance_Body --
4827 --------------------------
4828
4829 -- WARNING: This routine manages SPARK regions. Return statements must be
4830 -- replaced by gotos which jump to the end of the routine and restore the
4831 -- SPARK mode.
4832
4833 procedure Inline_Instance_Body
4834 (N : Node_Id;
4835 Gen_Unit : Entity_Id;
4836 Act_Decl : Node_Id)
4837 is
4838 Config_Attrs : constant Config_Switches_Type := Save_Config_Switches;
4839
4840 Curr_Comp : constant Node_Id := Cunit (Current_Sem_Unit);
4841 Curr_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
4842 Gen_Comp : constant Entity_Id :=
4843 Cunit_Entity (Get_Source_Unit (Gen_Unit));
4844
4845 Scope_Stack_Depth : constant Pos :=
4846 Scope_Stack.Last - Scope_Stack.First + 1;
4847
4848 Inner_Scopes : array (1 .. Scope_Stack_Depth) of Entity_Id;
4849 Instances : array (1 .. Scope_Stack_Depth) of Entity_Id;
4850 Use_Clauses : array (1 .. Scope_Stack_Depth) of Node_Id;
4851
4852 Curr_Scope : Entity_Id := Empty;
4853 List : Elist_Id := No_Elist; -- init to avoid warning
4854 N_Instances : Nat := 0;
4855 Num_Inner : Nat := 0;
4856 Num_Scopes : Nat := 0;
4857 Removed : Boolean := False;
4858 S : Entity_Id;
4859 Vis : Boolean;
4860
4861 begin
4862 -- Case of generic unit defined in another unit. We must remove the
4863 -- complete context of the current unit to install that of the generic.
4864
4865 if Gen_Comp /= Cunit_Entity (Current_Sem_Unit) then
4866
4867 -- Add some comments for the following two loops ???
4868
4869 S := Current_Scope;
4870 while Present (S) and then S /= Standard_Standard loop
4871 loop
4872 Num_Scopes := Num_Scopes + 1;
4873
4874 Use_Clauses (Num_Scopes) :=
4875 (Scope_Stack.Table
4876 (Scope_Stack.Last - Num_Scopes + 1).
4877 First_Use_Clause);
4878 End_Use_Clauses (Use_Clauses (Num_Scopes));
4879
4880 exit when Scope_Stack.Last - Num_Scopes + 1 = Scope_Stack.First
4881 or else Scope_Stack.Table
4882 (Scope_Stack.Last - Num_Scopes).Entity = Scope (S);
4883 end loop;
4884
4885 exit when Is_Generic_Instance (S)
4886 and then (In_Package_Body (S)
4887 or else Ekind (S) = E_Procedure
4888 or else Ekind (S) = E_Function);
4889 S := Scope (S);
4890 end loop;
4891
4892 Vis := Is_Immediately_Visible (Gen_Comp);
4893
4894 -- Find and save all enclosing instances
4895
4896 S := Current_Scope;
4897
4898 while Present (S)
4899 and then S /= Standard_Standard
4900 loop
4901 if Is_Generic_Instance (S) then
4902 N_Instances := N_Instances + 1;
4903 Instances (N_Instances) := S;
4904
4905 exit when In_Package_Body (S);
4906 end if;
4907
4908 S := Scope (S);
4909 end loop;
4910
4911 -- Remove context of current compilation unit, unless we are within a
4912 -- nested package instantiation, in which case the context has been
4913 -- removed previously.
4914
4915 -- If current scope is the body of a child unit, remove context of
4916 -- spec as well. If an enclosing scope is an instance body, the
4917 -- context has already been removed, but the entities in the body
4918 -- must be made invisible as well.
4919
4920 S := Current_Scope;
4921 while Present (S) and then S /= Standard_Standard loop
4922 if Is_Generic_Instance (S)
4923 and then (In_Package_Body (S)
4924 or else Ekind_In (S, E_Procedure, E_Function))
4925 then
4926 -- We still have to remove the entities of the enclosing
4927 -- instance from direct visibility.
4928
4929 declare
4930 E : Entity_Id;
4931 begin
4932 E := First_Entity (S);
4933 while Present (E) loop
4934 Set_Is_Immediately_Visible (E, False);
4935 Next_Entity (E);
4936 end loop;
4937 end;
4938
4939 exit;
4940 end if;
4941
4942 if S = Curr_Unit
4943 or else (Ekind (Curr_Unit) = E_Package_Body
4944 and then S = Spec_Entity (Curr_Unit))
4945 or else (Ekind (Curr_Unit) = E_Subprogram_Body
4946 and then S = Corresponding_Spec
4947 (Unit_Declaration_Node (Curr_Unit)))
4948 then
4949 Removed := True;
4950
4951 -- Remove entities in current scopes from visibility, so that
4952 -- instance body is compiled in a clean environment.
4953
4954 List := Save_Scope_Stack (Handle_Use => False);
4955
4956 if Is_Child_Unit (S) then
4957
4958 -- Remove child unit from stack, as well as inner scopes.
4959 -- Removing the context of a child unit removes parent units
4960 -- as well.
4961
4962 while Current_Scope /= S loop
4963 Num_Inner := Num_Inner + 1;
4964 Inner_Scopes (Num_Inner) := Current_Scope;
4965 Pop_Scope;
4966 end loop;
4967
4968 Pop_Scope;
4969 Remove_Context (Curr_Comp);
4970 Curr_Scope := S;
4971
4972 else
4973 Remove_Context (Curr_Comp);
4974 end if;
4975
4976 if Ekind (Curr_Unit) = E_Package_Body then
4977 Remove_Context (Library_Unit (Curr_Comp));
4978 end if;
4979 end if;
4980
4981 S := Scope (S);
4982 end loop;
4983
4984 pragma Assert (Num_Inner < Num_Scopes);
4985
4986 Push_Scope (Standard_Standard);
4987 Scope_Stack.Table (Scope_Stack.Last).Is_Active_Stack_Base := True;
4988
4989 -- The inlined package body is analyzed with the configuration state
4990 -- of the context prior to the scope manipulations performed above.
4991
4992 -- ??? shouldn't this also use the warning state of the context prior
4993 -- to the scope manipulations?
4994
4995 Instantiate_Package_Body
4996 (Body_Info =>
4997 ((Act_Decl => Act_Decl,
4998 Config_Switches => Config_Attrs,
4999 Current_Sem_Unit => Current_Sem_Unit,
5000 Expander_Status => Expander_Active,
5001 Inst_Node => N,
5002 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
5003 Scope_Suppress => Scope_Suppress,
5004 Warnings => Save_Warnings)),
5005 Inlined_Body => True);
5006
5007 Pop_Scope;
5008
5009 -- Restore context
5010
5011 Set_Is_Immediately_Visible (Gen_Comp, Vis);
5012
5013 -- Reset Generic_Instance flag so that use clauses can be installed
5014 -- in the proper order. (See Use_One_Package for effect of enclosing
5015 -- instances on processing of use clauses).
5016
5017 for J in 1 .. N_Instances loop
5018 Set_Is_Generic_Instance (Instances (J), False);
5019 end loop;
5020
5021 if Removed then
5022 Install_Context (Curr_Comp, Chain => False);
5023
5024 if Present (Curr_Scope)
5025 and then Is_Child_Unit (Curr_Scope)
5026 then
5027 Push_Scope (Curr_Scope);
5028 Set_Is_Immediately_Visible (Curr_Scope);
5029
5030 -- Finally, restore inner scopes as well
5031
5032 for J in reverse 1 .. Num_Inner loop
5033 Push_Scope (Inner_Scopes (J));
5034 end loop;
5035 end if;
5036
5037 Restore_Scope_Stack (List, Handle_Use => False);
5038
5039 if Present (Curr_Scope)
5040 and then
5041 (In_Private_Part (Curr_Scope)
5042 or else In_Package_Body (Curr_Scope))
5043 then
5044 -- Install private declaration of ancestor units, which are
5045 -- currently available. Restore_Scope_Stack and Install_Context
5046 -- only install the visible part of parents.
5047
5048 declare
5049 Par : Entity_Id;
5050 begin
5051 Par := Scope (Curr_Scope);
5052 while (Present (Par)) and then Par /= Standard_Standard loop
5053 Install_Private_Declarations (Par);
5054 Par := Scope (Par);
5055 end loop;
5056 end;
5057 end if;
5058 end if;
5059
5060 -- Restore use clauses. For a child unit, use clauses in the parents
5061 -- are restored when installing the context, so only those in inner
5062 -- scopes (and those local to the child unit itself) need to be
5063 -- installed explicitly.
5064
5065 if Is_Child_Unit (Curr_Unit) and then Removed then
5066 for J in reverse 1 .. Num_Inner + 1 loop
5067 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
5068 Use_Clauses (J);
5069 Install_Use_Clauses (Use_Clauses (J));
5070 end loop;
5071
5072 else
5073 for J in reverse 1 .. Num_Scopes loop
5074 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
5075 Use_Clauses (J);
5076 Install_Use_Clauses (Use_Clauses (J));
5077 end loop;
5078 end if;
5079
5080 -- Restore status of instances. If one of them is a body, make its
5081 -- local entities visible again.
5082
5083 declare
5084 E : Entity_Id;
5085 Inst : Entity_Id;
5086
5087 begin
5088 for J in 1 .. N_Instances loop
5089 Inst := Instances (J);
5090 Set_Is_Generic_Instance (Inst, True);
5091
5092 if In_Package_Body (Inst)
5093 or else Ekind_In (S, E_Procedure, E_Function)
5094 then
5095 E := First_Entity (Instances (J));
5096 while Present (E) loop
5097 Set_Is_Immediately_Visible (E);
5098 Next_Entity (E);
5099 end loop;
5100 end if;
5101 end loop;
5102 end;
5103
5104 -- If generic unit is in current unit, current context is correct. Note
5105 -- that the context is guaranteed to carry the correct SPARK_Mode as no
5106 -- enclosing scopes were removed.
5107
5108 else
5109 Instantiate_Package_Body
5110 (Body_Info =>
5111 ((Act_Decl => Act_Decl,
5112 Config_Switches => Save_Config_Switches,
5113 Current_Sem_Unit => Current_Sem_Unit,
5114 Expander_Status => Expander_Active,
5115 Inst_Node => N,
5116 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
5117 Scope_Suppress => Scope_Suppress,
5118 Warnings => Save_Warnings)),
5119 Inlined_Body => True);
5120 end if;
5121 end Inline_Instance_Body;
5122
5123 -------------------------------------
5124 -- Analyze_Procedure_Instantiation --
5125 -------------------------------------
5126
5127 procedure Analyze_Procedure_Instantiation (N : Node_Id) is
5128 begin
5129 Analyze_Subprogram_Instantiation (N, E_Procedure);
5130 end Analyze_Procedure_Instantiation;
5131
5132 -----------------------------------
5133 -- Need_Subprogram_Instance_Body --
5134 -----------------------------------
5135
5136 function Need_Subprogram_Instance_Body
5137 (N : Node_Id;
5138 Subp : Entity_Id) return Boolean
5139 is
5140 function Is_Inlined_Or_Child_Of_Inlined (E : Entity_Id) return Boolean;
5141 -- Return True if E is an inlined subprogram, an inlined renaming or a
5142 -- subprogram nested in an inlined subprogram. The inlining machinery
5143 -- totally disregards nested subprograms since it considers that they
5144 -- will always be compiled if the parent is (see Inline.Is_Nested).
5145
5146 ------------------------------------
5147 -- Is_Inlined_Or_Child_Of_Inlined --
5148 ------------------------------------
5149
5150 function Is_Inlined_Or_Child_Of_Inlined (E : Entity_Id) return Boolean is
5151 Scop : Entity_Id;
5152
5153 begin
5154 if Is_Inlined (E) or else Is_Inlined (Alias (E)) then
5155 return True;
5156 end if;
5157
5158 Scop := Scope (E);
5159 while Scop /= Standard_Standard loop
5160 if Ekind (Scop) in Subprogram_Kind and then Is_Inlined (Scop) then
5161 return True;
5162 end if;
5163
5164 Scop := Scope (Scop);
5165 end loop;
5166
5167 return False;
5168 end Is_Inlined_Or_Child_Of_Inlined;
5169
5170 begin
5171 -- Must be in the main unit or inlined (or child of inlined)
5172
5173 if (Is_In_Main_Unit (N) or else Is_Inlined_Or_Child_Of_Inlined (Subp))
5174
5175 -- Must be generating code or analyzing code in GNATprove mode
5176
5177 and then (Operating_Mode = Generate_Code
5178 or else (Operating_Mode = Check_Semantics
5179 and then GNATprove_Mode))
5180
5181 -- The body is needed when generating code (full expansion) and in
5182 -- in GNATprove mode (special expansion) for formal verification of
5183 -- the body itself.
5184
5185 and then (Expander_Active or GNATprove_Mode)
5186
5187 -- No point in inlining if ABE is inevitable
5188
5189 and then not Is_Known_Guaranteed_ABE (N)
5190
5191 -- Or if subprogram is eliminated
5192
5193 and then not Is_Eliminated (Subp)
5194 then
5195 Add_Pending_Instantiation (N, Unit_Declaration_Node (Subp));
5196 return True;
5197
5198 -- Here if not inlined, or we ignore the inlining
5199
5200 else
5201 return False;
5202 end if;
5203 end Need_Subprogram_Instance_Body;
5204
5205 --------------------------------------
5206 -- Analyze_Subprogram_Instantiation --
5207 --------------------------------------
5208
5209 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
5210 -- must be replaced by gotos which jump to the end of the routine in order
5211 -- to restore the Ghost and SPARK modes.
5212
5213 procedure Analyze_Subprogram_Instantiation
5214 (N : Node_Id;
5215 K : Entity_Kind)
5216 is
5217 Errs : constant Nat := Serious_Errors_Detected;
5218 Gen_Id : constant Node_Id := Name (N);
5219 Inst_Id : constant Entity_Id := Defining_Entity (N);
5220 Anon_Id : constant Entity_Id :=
5221 Make_Defining_Identifier (Sloc (Inst_Id),
5222 Chars => New_External_Name (Chars (Inst_Id), 'R'));
5223 Loc : constant Source_Ptr := Sloc (N);
5224
5225 Act_Decl_Id : Entity_Id := Empty; -- init to avoid warning
5226 Act_Decl : Node_Id;
5227 Act_Spec : Node_Id;
5228 Act_Tree : Node_Id;
5229
5230 Env_Installed : Boolean := False;
5231 Gen_Unit : Entity_Id;
5232 Gen_Decl : Node_Id;
5233 Pack_Id : Entity_Id;
5234 Parent_Installed : Boolean := False;
5235
5236 Renaming_List : List_Id;
5237 -- The list of declarations that link formals and actuals of the
5238 -- instance. These are subtype declarations for formal types, and
5239 -- renaming declarations for other formals. The subprogram declaration
5240 -- for the instance is then appended to the list, and the last item on
5241 -- the list is the renaming declaration for the instance.
5242
5243 procedure Analyze_Instance_And_Renamings;
5244 -- The instance must be analyzed in a context that includes the mappings
5245 -- of generic parameters into actuals. We create a package declaration
5246 -- for this purpose, and a subprogram with an internal name within the
5247 -- package. The subprogram instance is simply an alias for the internal
5248 -- subprogram, declared in the current scope.
5249
5250 procedure Build_Subprogram_Renaming;
5251 -- If the subprogram is recursive, there are occurrences of the name of
5252 -- the generic within the body, which must resolve to the current
5253 -- instance. We add a renaming declaration after the declaration, which
5254 -- is available in the instance body, as well as in the analysis of
5255 -- aspects that appear in the generic. This renaming declaration is
5256 -- inserted after the instance declaration which it renames.
5257
5258 ------------------------------------
5259 -- Analyze_Instance_And_Renamings --
5260 ------------------------------------
5261
5262 procedure Analyze_Instance_And_Renamings is
5263 Def_Ent : constant Entity_Id := Defining_Entity (N);
5264 Pack_Decl : Node_Id;
5265
5266 begin
5267 if Nkind (Parent (N)) = N_Compilation_Unit then
5268
5269 -- For the case of a compilation unit, the container package has
5270 -- the same name as the instantiation, to insure that the binder
5271 -- calls the elaboration procedure with the right name. Copy the
5272 -- entity of the instance, which may have compilation level flags
5273 -- (e.g. Is_Child_Unit) set.
5274
5275 Pack_Id := New_Copy (Def_Ent);
5276
5277 else
5278 -- Otherwise we use the name of the instantiation concatenated
5279 -- with its source position to ensure uniqueness if there are
5280 -- several instantiations with the same name.
5281
5282 Pack_Id :=
5283 Make_Defining_Identifier (Loc,
5284 Chars => New_External_Name
5285 (Related_Id => Chars (Def_Ent),
5286 Suffix => "GP",
5287 Suffix_Index => Source_Offset (Sloc (Def_Ent))));
5288 end if;
5289
5290 Pack_Decl :=
5291 Make_Package_Declaration (Loc,
5292 Specification => Make_Package_Specification (Loc,
5293 Defining_Unit_Name => Pack_Id,
5294 Visible_Declarations => Renaming_List,
5295 End_Label => Empty));
5296
5297 Set_Instance_Spec (N, Pack_Decl);
5298 Set_Is_Generic_Instance (Pack_Id);
5299 Set_Debug_Info_Needed (Pack_Id);
5300
5301 -- Case of not a compilation unit
5302
5303 if Nkind (Parent (N)) /= N_Compilation_Unit then
5304 Mark_Rewrite_Insertion (Pack_Decl);
5305 Insert_Before (N, Pack_Decl);
5306 Set_Has_Completion (Pack_Id);
5307
5308 -- Case of an instantiation that is a compilation unit
5309
5310 -- Place declaration on current node so context is complete for
5311 -- analysis (including nested instantiations), and for use in a
5312 -- context_clause (see Analyze_With_Clause).
5313
5314 else
5315 Set_Unit (Parent (N), Pack_Decl);
5316 Set_Parent_Spec (Pack_Decl, Parent_Spec (N));
5317 end if;
5318
5319 Analyze (Pack_Decl);
5320 Check_Formal_Packages (Pack_Id);
5321
5322 -- Body of the enclosing package is supplied when instantiating the
5323 -- subprogram body, after semantic analysis is completed.
5324
5325 if Nkind (Parent (N)) = N_Compilation_Unit then
5326
5327 -- Remove package itself from visibility, so it does not
5328 -- conflict with subprogram.
5329
5330 Set_Name_Entity_Id (Chars (Pack_Id), Homonym (Pack_Id));
5331
5332 -- Set name and scope of internal subprogram so that the proper
5333 -- external name will be generated. The proper scope is the scope
5334 -- of the wrapper package. We need to generate debugging info for
5335 -- the internal subprogram, so set flag accordingly.
5336
5337 Set_Chars (Anon_Id, Chars (Defining_Entity (N)));
5338 Set_Scope (Anon_Id, Scope (Pack_Id));
5339
5340 -- Mark wrapper package as referenced, to avoid spurious warnings
5341 -- if the instantiation appears in various with_ clauses of
5342 -- subunits of the main unit.
5343
5344 Set_Referenced (Pack_Id);
5345 end if;
5346
5347 Set_Is_Generic_Instance (Anon_Id);
5348 Set_Debug_Info_Needed (Anon_Id);
5349 Act_Decl_Id := New_Copy (Anon_Id);
5350
5351 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
5352 Set_Chars (Act_Decl_Id, Chars (Defining_Entity (N)));
5353 Set_Sloc (Act_Decl_Id, Sloc (Defining_Entity (N)));
5354
5355 -- Subprogram instance comes from source only if generic does
5356
5357 Set_Comes_From_Source (Act_Decl_Id, Comes_From_Source (Gen_Unit));
5358
5359 -- If the instance is a child unit, mark the Id accordingly. Mark
5360 -- the anonymous entity as well, which is the real subprogram and
5361 -- which is used when the instance appears in a context clause.
5362 -- Similarly, propagate the Is_Eliminated flag to handle properly
5363 -- nested eliminated subprograms.
5364
5365 Set_Is_Child_Unit (Act_Decl_Id, Is_Child_Unit (Defining_Entity (N)));
5366 Set_Is_Child_Unit (Anon_Id, Is_Child_Unit (Defining_Entity (N)));
5367 New_Overloaded_Entity (Act_Decl_Id);
5368 Check_Eliminated (Act_Decl_Id);
5369 Set_Is_Eliminated (Anon_Id, Is_Eliminated (Act_Decl_Id));
5370
5371 if Nkind (Parent (N)) = N_Compilation_Unit then
5372
5373 -- In compilation unit case, kill elaboration checks on the
5374 -- instantiation, since they are never needed - the body is
5375 -- instantiated at the same point as the spec.
5376
5377 if Legacy_Elaboration_Checks then
5378 Set_Kill_Elaboration_Checks (Act_Decl_Id);
5379 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
5380 end if;
5381
5382 Set_Is_Compilation_Unit (Anon_Id);
5383 Set_Cunit_Entity (Current_Sem_Unit, Pack_Id);
5384 end if;
5385
5386 -- The instance is not a freezing point for the new subprogram.
5387 -- The anonymous subprogram may have a freeze node, created for
5388 -- some delayed aspects. This freeze node must not be inherited
5389 -- by the visible subprogram entity.
5390
5391 Set_Is_Frozen (Act_Decl_Id, False);
5392 Set_Freeze_Node (Act_Decl_Id, Empty);
5393
5394 if Nkind (Defining_Entity (N)) = N_Defining_Operator_Symbol then
5395 Valid_Operator_Definition (Act_Decl_Id);
5396 end if;
5397
5398 Set_Alias (Act_Decl_Id, Anon_Id);
5399 Set_Has_Completion (Act_Decl_Id);
5400 Set_Related_Instance (Pack_Id, Act_Decl_Id);
5401
5402 if Nkind (Parent (N)) = N_Compilation_Unit then
5403 Set_Body_Required (Parent (N), False);
5404 end if;
5405 end Analyze_Instance_And_Renamings;
5406
5407 -------------------------------
5408 -- Build_Subprogram_Renaming --
5409 -------------------------------
5410
5411 procedure Build_Subprogram_Renaming is
5412 Renaming_Decl : Node_Id;
5413 Unit_Renaming : Node_Id;
5414
5415 begin
5416 Unit_Renaming :=
5417 Make_Subprogram_Renaming_Declaration (Loc,
5418 Specification =>
5419 Copy_Generic_Node
5420 (Specification (Original_Node (Gen_Decl)),
5421 Empty,
5422 Instantiating => True),
5423 Name => New_Occurrence_Of (Anon_Id, Loc));
5424
5425 -- The generic may be a child unit. The renaming needs an identifier
5426 -- with the proper name.
5427
5428 Set_Defining_Unit_Name (Specification (Unit_Renaming),
5429 Make_Defining_Identifier (Loc, Chars (Gen_Unit)));
5430
5431 -- If there is a formal subprogram with the same name as the unit
5432 -- itself, do not add this renaming declaration, to prevent
5433 -- ambiguities when there is a call with that name in the body.
5434 -- This is a partial and ugly fix for one ACATS test. ???
5435
5436 Renaming_Decl := First (Renaming_List);
5437 while Present (Renaming_Decl) loop
5438 if Nkind (Renaming_Decl) = N_Subprogram_Renaming_Declaration
5439 and then
5440 Chars (Defining_Entity (Renaming_Decl)) = Chars (Gen_Unit)
5441 then
5442 exit;
5443 end if;
5444
5445 Next (Renaming_Decl);
5446 end loop;
5447
5448 if No (Renaming_Decl) then
5449 Append (Unit_Renaming, Renaming_List);
5450 end if;
5451 end Build_Subprogram_Renaming;
5452
5453 -- Local variables
5454
5455 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
5456 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
5457 Saved_ISMP : constant Boolean :=
5458 Ignore_SPARK_Mode_Pragmas_In_Instance;
5459 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
5460 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
5461 -- Save the Ghost and SPARK mode-related data to restore on exit
5462
5463 Vis_Prims_List : Elist_Id := No_Elist;
5464 -- List of primitives made temporarily visible in the instantiation
5465 -- to match the visibility of the formal type
5466
5467 -- Start of processing for Analyze_Subprogram_Instantiation
5468
5469 begin
5470 -- Preserve relevant elaboration-related attributes of the context which
5471 -- are no longer available or very expensive to recompute once analysis,
5472 -- resolution, and expansion are over.
5473
5474 Mark_Elaboration_Attributes
5475 (N_Id => N,
5476 Checks => True,
5477 Level => True,
5478 Modes => True,
5479 Warnings => True);
5480
5481 Check_SPARK_05_Restriction ("generic is not allowed", N);
5482
5483 -- Very first thing: check for special Text_IO unit in case we are
5484 -- instantiating one of the children of [[Wide_]Wide_]Text_IO. Of course
5485 -- such an instantiation is bogus (these are packages, not subprograms),
5486 -- but we get a better error message if we do this.
5487
5488 Check_Text_IO_Special_Unit (Gen_Id);
5489
5490 -- Make node global for error reporting
5491
5492 Instantiation_Node := N;
5493
5494 -- For package instantiations we turn off style checks, because they
5495 -- will have been emitted in the generic. For subprogram instantiations
5496 -- we want to apply at least the check on overriding indicators so we
5497 -- do not modify the style check status.
5498
5499 -- The renaming declarations for the actuals do not come from source and
5500 -- will not generate spurious warnings.
5501
5502 Preanalyze_Actuals (N);
5503
5504 Init_Env;
5505 Env_Installed := True;
5506 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
5507 Gen_Unit := Entity (Gen_Id);
5508
5509 -- A subprogram instantiation is Ghost when it is subject to pragma
5510 -- Ghost or the generic template is Ghost. Set the mode now to ensure
5511 -- that any nodes generated during analysis and expansion are marked as
5512 -- Ghost.
5513
5514 Mark_And_Set_Ghost_Instantiation (N, Gen_Unit);
5515
5516 Generate_Reference (Gen_Unit, Gen_Id);
5517
5518 if Nkind (Gen_Id) = N_Identifier
5519 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
5520 then
5521 Error_Msg_NE
5522 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
5523 end if;
5524
5525 if Etype (Gen_Unit) = Any_Type then
5526 Restore_Env;
5527 goto Leave;
5528 end if;
5529
5530 -- Verify that it is a generic subprogram of the right kind, and that
5531 -- it does not lead to a circular instantiation.
5532
5533 if K = E_Procedure and then Ekind (Gen_Unit) /= E_Generic_Procedure then
5534 Error_Msg_NE
5535 ("& is not the name of a generic procedure", Gen_Id, Gen_Unit);
5536
5537 elsif K = E_Function and then Ekind (Gen_Unit) /= E_Generic_Function then
5538 Error_Msg_NE
5539 ("& is not the name of a generic function", Gen_Id, Gen_Unit);
5540
5541 elsif In_Open_Scopes (Gen_Unit) then
5542 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
5543
5544 else
5545 Set_Ekind (Inst_Id, K);
5546 Set_Scope (Inst_Id, Current_Scope);
5547
5548 Set_Entity (Gen_Id, Gen_Unit);
5549 Set_Is_Instantiated (Gen_Unit);
5550
5551 if In_Extended_Main_Source_Unit (N) then
5552 Generate_Reference (Gen_Unit, N);
5553 end if;
5554
5555 -- If renaming, get original unit
5556
5557 if Present (Renamed_Object (Gen_Unit))
5558 and then Ekind_In (Renamed_Object (Gen_Unit), E_Generic_Procedure,
5559 E_Generic_Function)
5560 then
5561 Gen_Unit := Renamed_Object (Gen_Unit);
5562 Set_Is_Instantiated (Gen_Unit);
5563 Generate_Reference (Gen_Unit, N);
5564 end if;
5565
5566 if Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
5567 Error_Msg_Node_2 := Current_Scope;
5568 Error_Msg_NE
5569 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
5570 Circularity_Detected := True;
5571 Restore_Hidden_Primitives (Vis_Prims_List);
5572 goto Leave;
5573 end if;
5574
5575 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
5576
5577 -- Initialize renamings map, for error checking
5578
5579 Generic_Renamings.Set_Last (0);
5580 Generic_Renamings_HTable.Reset;
5581
5582 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
5583
5584 -- Copy original generic tree, to produce text for instantiation
5585
5586 Act_Tree :=
5587 Copy_Generic_Node
5588 (Original_Node (Gen_Decl), Empty, Instantiating => True);
5589
5590 -- Inherit overriding indicator from instance node
5591
5592 Act_Spec := Specification (Act_Tree);
5593 Set_Must_Override (Act_Spec, Must_Override (N));
5594 Set_Must_Not_Override (Act_Spec, Must_Not_Override (N));
5595
5596 Renaming_List :=
5597 Analyze_Associations
5598 (I_Node => N,
5599 Formals => Generic_Formal_Declarations (Act_Tree),
5600 F_Copy => Generic_Formal_Declarations (Gen_Decl));
5601
5602 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
5603
5604 -- The subprogram itself cannot contain a nested instance, so the
5605 -- current parent is left empty.
5606
5607 Set_Instance_Env (Gen_Unit, Empty);
5608
5609 -- Build the subprogram declaration, which does not appear in the
5610 -- generic template, and give it a sloc consistent with that of the
5611 -- template.
5612
5613 Set_Defining_Unit_Name (Act_Spec, Anon_Id);
5614 Set_Generic_Parent (Act_Spec, Gen_Unit);
5615 Act_Decl :=
5616 Make_Subprogram_Declaration (Sloc (Act_Spec),
5617 Specification => Act_Spec);
5618
5619 -- The aspects have been copied previously, but they have to be
5620 -- linked explicitly to the new subprogram declaration. Explicit
5621 -- pre/postconditions on the instance are analyzed below, in a
5622 -- separate step.
5623
5624 Move_Aspects (Act_Tree, To => Act_Decl);
5625 Set_Categorization_From_Pragmas (Act_Decl);
5626
5627 if Parent_Installed then
5628 Hide_Current_Scope;
5629 end if;
5630
5631 Append (Act_Decl, Renaming_List);
5632
5633 -- Contract-related source pragmas that follow a generic subprogram
5634 -- must be instantiated explicitly because they are not part of the
5635 -- subprogram template.
5636
5637 Instantiate_Subprogram_Contract
5638 (Original_Node (Gen_Decl), Renaming_List);
5639
5640 Build_Subprogram_Renaming;
5641
5642 -- If the context of the instance is subject to SPARK_Mode "off" or
5643 -- the annotation is altogether missing, set the global flag which
5644 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
5645 -- the instance. This should be done prior to analyzing the instance.
5646
5647 if SPARK_Mode /= On then
5648 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
5649 end if;
5650
5651 -- If the context of an instance is not subject to SPARK_Mode "off",
5652 -- and the generic spec is subject to an explicit SPARK_Mode pragma,
5653 -- the latter should be the one applicable to the instance.
5654
5655 if not Ignore_SPARK_Mode_Pragmas_In_Instance
5656 and then Saved_SM /= Off
5657 and then Present (SPARK_Pragma (Gen_Unit))
5658 then
5659 Set_SPARK_Mode (Gen_Unit);
5660 end if;
5661
5662 Analyze_Instance_And_Renamings;
5663
5664 -- Restore SPARK_Mode from the context after analysis of the package
5665 -- declaration, so that the SPARK_Mode on the generic spec does not
5666 -- apply to the pending instance for the instance body.
5667
5668 if not Ignore_SPARK_Mode_Pragmas_In_Instance
5669 and then Saved_SM /= Off
5670 and then Present (SPARK_Pragma (Gen_Unit))
5671 then
5672 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
5673 end if;
5674
5675 -- If the generic is marked Import (Intrinsic), then so is the
5676 -- instance. This indicates that there is no body to instantiate. If
5677 -- generic is marked inline, so it the instance, and the anonymous
5678 -- subprogram it renames. If inlined, or else if inlining is enabled
5679 -- for the compilation, we generate the instance body even if it is
5680 -- not within the main unit.
5681
5682 if Is_Intrinsic_Subprogram (Gen_Unit) then
5683 Set_Is_Intrinsic_Subprogram (Anon_Id);
5684 Set_Is_Intrinsic_Subprogram (Act_Decl_Id);
5685
5686 if Chars (Gen_Unit) = Name_Unchecked_Conversion then
5687 Validate_Unchecked_Conversion (N, Act_Decl_Id);
5688 end if;
5689 end if;
5690
5691 -- Inherit convention from generic unit. Intrinsic convention, as for
5692 -- an instance of unchecked conversion, is not inherited because an
5693 -- explicit Ada instance has been created.
5694
5695 if Has_Convention_Pragma (Gen_Unit)
5696 and then Convention (Gen_Unit) /= Convention_Intrinsic
5697 then
5698 Set_Convention (Act_Decl_Id, Convention (Gen_Unit));
5699 Set_Is_Exported (Act_Decl_Id, Is_Exported (Gen_Unit));
5700 end if;
5701
5702 Generate_Definition (Act_Decl_Id);
5703
5704 -- Inherit all inlining-related flags which apply to the generic in
5705 -- the subprogram and its declaration.
5706
5707 Set_Is_Inlined (Act_Decl_Id, Is_Inlined (Gen_Unit));
5708 Set_Is_Inlined (Anon_Id, Is_Inlined (Gen_Unit));
5709
5710 Set_Has_Pragma_Inline (Act_Decl_Id, Has_Pragma_Inline (Gen_Unit));
5711 Set_Has_Pragma_Inline (Anon_Id, Has_Pragma_Inline (Gen_Unit));
5712
5713 Set_Has_Pragma_Inline_Always
5714 (Act_Decl_Id, Has_Pragma_Inline_Always (Gen_Unit));
5715 Set_Has_Pragma_Inline_Always
5716 (Anon_Id, Has_Pragma_Inline_Always (Gen_Unit));
5717
5718 Set_Has_Pragma_No_Inline
5719 (Act_Decl_Id, Has_Pragma_No_Inline (Gen_Unit));
5720 Set_Has_Pragma_No_Inline
5721 (Anon_Id, Has_Pragma_No_Inline (Gen_Unit));
5722
5723 -- Propagate No_Return if pragma applied to generic unit. This must
5724 -- be done explicitly because pragma does not appear in generic
5725 -- declaration (unlike the aspect case).
5726
5727 if No_Return (Gen_Unit) then
5728 Set_No_Return (Act_Decl_Id);
5729 Set_No_Return (Anon_Id);
5730 end if;
5731
5732 -- Mark both the instance spec and the anonymous package in case the
5733 -- body is instantiated at a later pass. This preserves the original
5734 -- context in effect for the body.
5735
5736 if SPARK_Mode /= On then
5737 Set_Ignore_SPARK_Mode_Pragmas (Act_Decl_Id);
5738 Set_Ignore_SPARK_Mode_Pragmas (Anon_Id);
5739 end if;
5740
5741 if Legacy_Elaboration_Checks
5742 and then not Is_Intrinsic_Subprogram (Gen_Unit)
5743 then
5744 Check_Elab_Instantiation (N);
5745 end if;
5746
5747 -- Save the scenario for later examination by the ABE Processing
5748 -- phase.
5749
5750 Record_Elaboration_Scenario (N);
5751
5752 -- The instantiation results in a guaranteed ABE. Create a completing
5753 -- body for the subprogram declaration because the real body will not
5754 -- be instantiated.
5755
5756 if Is_Known_Guaranteed_ABE (N) then
5757 Provide_Completing_Bodies (Instance_Spec (N));
5758 end if;
5759
5760 if Is_Dispatching_Operation (Act_Decl_Id)
5761 and then Ada_Version >= Ada_2005
5762 then
5763 declare
5764 Formal : Entity_Id;
5765
5766 begin
5767 Formal := First_Formal (Act_Decl_Id);
5768 while Present (Formal) loop
5769 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type
5770 and then Is_Controlling_Formal (Formal)
5771 and then not Can_Never_Be_Null (Formal)
5772 then
5773 Error_Msg_NE
5774 ("access parameter& is controlling,", N, Formal);
5775 Error_Msg_NE
5776 ("\corresponding parameter of & must be explicitly "
5777 & "null-excluding", N, Gen_Id);
5778 end if;
5779
5780 Next_Formal (Formal);
5781 end loop;
5782 end;
5783 end if;
5784
5785 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
5786
5787 Validate_Categorization_Dependency (N, Act_Decl_Id);
5788
5789 if not Is_Intrinsic_Subprogram (Act_Decl_Id) then
5790 Inherit_Context (Gen_Decl, N);
5791
5792 Restore_Private_Views (Pack_Id, False);
5793
5794 -- If the context requires a full instantiation, mark node for
5795 -- subsequent construction of the body.
5796
5797 if Need_Subprogram_Instance_Body (N, Act_Decl_Id) then
5798 Check_Forward_Instantiation (Gen_Decl);
5799
5800 -- The wrapper package is always delayed, because it does not
5801 -- constitute a freeze point, but to insure that the freeze node
5802 -- is placed properly, it is created directly when instantiating
5803 -- the body (otherwise the freeze node might appear to early for
5804 -- nested instantiations).
5805
5806 elsif Nkind (Parent (N)) = N_Compilation_Unit then
5807 Rewrite (N, Unit (Parent (N)));
5808 Set_Unit (Parent (N), N);
5809 end if;
5810
5811 -- Replace instance node for library-level instantiations of
5812 -- intrinsic subprograms.
5813
5814 elsif Nkind (Parent (N)) = N_Compilation_Unit then
5815 Rewrite (N, Unit (Parent (N)));
5816 Set_Unit (Parent (N), N);
5817 end if;
5818
5819 if Parent_Installed then
5820 Remove_Parent;
5821 end if;
5822
5823 Restore_Hidden_Primitives (Vis_Prims_List);
5824 Restore_Env;
5825 Env_Installed := False;
5826 Generic_Renamings.Set_Last (0);
5827 Generic_Renamings_HTable.Reset;
5828 end if;
5829
5830 <<Leave>>
5831 -- Analyze aspects in declaration if no errors appear in the instance.
5832
5833 if Has_Aspects (N) and then Serious_Errors_Detected = Errs then
5834 Analyze_Aspect_Specifications (N, Act_Decl_Id);
5835 end if;
5836
5837 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
5838 Restore_Ghost_Region (Saved_GM, Saved_IGR);
5839 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
5840
5841 exception
5842 when Instantiation_Error =>
5843 if Parent_Installed then
5844 Remove_Parent;
5845 end if;
5846
5847 if Env_Installed then
5848 Restore_Env;
5849 end if;
5850
5851 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
5852 Restore_Ghost_Region (Saved_GM, Saved_IGR);
5853 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
5854 end Analyze_Subprogram_Instantiation;
5855
5856 -------------------------
5857 -- Get_Associated_Node --
5858 -------------------------
5859
5860 function Get_Associated_Node (N : Node_Id) return Node_Id is
5861 Assoc : Node_Id;
5862
5863 begin
5864 Assoc := Associated_Node (N);
5865
5866 if Nkind (Assoc) /= Nkind (N) then
5867 return Assoc;
5868
5869 elsif Nkind_In (Assoc, N_Aggregate, N_Extension_Aggregate) then
5870 return Assoc;
5871
5872 else
5873 -- If the node is part of an inner generic, it may itself have been
5874 -- remapped into a further generic copy. Associated_Node is otherwise
5875 -- used for the entity of the node, and will be of a different node
5876 -- kind, or else N has been rewritten as a literal or function call.
5877
5878 while Present (Associated_Node (Assoc))
5879 and then Nkind (Associated_Node (Assoc)) = Nkind (Assoc)
5880 loop
5881 Assoc := Associated_Node (Assoc);
5882 end loop;
5883
5884 -- Follow an additional link in case the final node was rewritten.
5885 -- This can only happen with nested generic units.
5886
5887 if (Nkind (Assoc) = N_Identifier or else Nkind (Assoc) in N_Op)
5888 and then Present (Associated_Node (Assoc))
5889 and then (Nkind_In (Associated_Node (Assoc), N_Function_Call,
5890 N_Explicit_Dereference,
5891 N_Integer_Literal,
5892 N_Real_Literal,
5893 N_String_Literal))
5894 then
5895 Assoc := Associated_Node (Assoc);
5896 end if;
5897
5898 -- An additional special case: an unconstrained type in an object
5899 -- declaration may have been rewritten as a local subtype constrained
5900 -- by the expression in the declaration. We need to recover the
5901 -- original entity, which may be global.
5902
5903 if Present (Original_Node (Assoc))
5904 and then Nkind (Parent (N)) = N_Object_Declaration
5905 then
5906 Assoc := Original_Node (Assoc);
5907 end if;
5908
5909 return Assoc;
5910 end if;
5911 end Get_Associated_Node;
5912
5913 ----------------------------
5914 -- Build_Function_Wrapper --
5915 ----------------------------
5916
5917 function Build_Function_Wrapper
5918 (Formal_Subp : Entity_Id;
5919 Actual_Subp : Entity_Id) return Node_Id
5920 is
5921 Loc : constant Source_Ptr := Sloc (Current_Scope);
5922 Ret_Type : constant Entity_Id := Get_Instance_Of (Etype (Formal_Subp));
5923 Actuals : List_Id;
5924 Decl : Node_Id;
5925 Func_Name : Node_Id;
5926 Func : Entity_Id;
5927 Parm_Type : Node_Id;
5928 Profile : List_Id := New_List;
5929 Spec : Node_Id;
5930 Act_F : Entity_Id;
5931 Form_F : Entity_Id;
5932 New_F : Entity_Id;
5933
5934 begin
5935 Func_Name := New_Occurrence_Of (Actual_Subp, Loc);
5936
5937 Func := Make_Defining_Identifier (Loc, Chars (Formal_Subp));
5938 Set_Ekind (Func, E_Function);
5939 Set_Is_Generic_Actual_Subprogram (Func);
5940
5941 Actuals := New_List;
5942 Profile := New_List;
5943
5944 Act_F := First_Formal (Actual_Subp);
5945 Form_F := First_Formal (Formal_Subp);
5946 while Present (Form_F) loop
5947
5948 -- Create new formal for profile of wrapper, and add a reference
5949 -- to it in the list of actuals for the enclosing call. The name
5950 -- must be that of the formal in the formal subprogram, because
5951 -- calls to it in the generic body may use named associations.
5952
5953 New_F := Make_Defining_Identifier (Loc, Chars (Form_F));
5954
5955 Parm_Type :=
5956 New_Occurrence_Of (Get_Instance_Of (Etype (Form_F)), Loc);
5957
5958 Append_To (Profile,
5959 Make_Parameter_Specification (Loc,
5960 Defining_Identifier => New_F,
5961 Parameter_Type => Parm_Type));
5962
5963 Append_To (Actuals, New_Occurrence_Of (New_F, Loc));
5964 Next_Formal (Form_F);
5965
5966 if Present (Act_F) then
5967 Next_Formal (Act_F);
5968 end if;
5969 end loop;
5970
5971 Spec :=
5972 Make_Function_Specification (Loc,
5973 Defining_Unit_Name => Func,
5974 Parameter_Specifications => Profile,
5975 Result_Definition => New_Occurrence_Of (Ret_Type, Loc));
5976
5977 Decl :=
5978 Make_Expression_Function (Loc,
5979 Specification => Spec,
5980 Expression =>
5981 Make_Function_Call (Loc,
5982 Name => Func_Name,
5983 Parameter_Associations => Actuals));
5984
5985 return Decl;
5986 end Build_Function_Wrapper;
5987
5988 ----------------------------
5989 -- Build_Operator_Wrapper --
5990 ----------------------------
5991
5992 function Build_Operator_Wrapper
5993 (Formal_Subp : Entity_Id;
5994 Actual_Subp : Entity_Id) return Node_Id
5995 is
5996 Loc : constant Source_Ptr := Sloc (Current_Scope);
5997 Ret_Type : constant Entity_Id :=
5998 Get_Instance_Of (Etype (Formal_Subp));
5999 Op_Type : constant Entity_Id :=
6000 Get_Instance_Of (Etype (First_Formal (Formal_Subp)));
6001 Is_Binary : constant Boolean :=
6002 Present (Next_Formal (First_Formal (Formal_Subp)));
6003
6004 Decl : Node_Id;
6005 Expr : Node_Id := Empty;
6006 F1, F2 : Entity_Id;
6007 Func : Entity_Id;
6008 Op_Name : Name_Id;
6009 Spec : Node_Id;
6010 L, R : Node_Id;
6011
6012 begin
6013 Op_Name := Chars (Actual_Subp);
6014
6015 -- Create entities for wrapper function and its formals
6016
6017 F1 := Make_Temporary (Loc, 'A');
6018 F2 := Make_Temporary (Loc, 'B');
6019 L := New_Occurrence_Of (F1, Loc);
6020 R := New_Occurrence_Of (F2, Loc);
6021
6022 Func := Make_Defining_Identifier (Loc, Chars (Formal_Subp));
6023 Set_Ekind (Func, E_Function);
6024 Set_Is_Generic_Actual_Subprogram (Func);
6025
6026 Spec :=
6027 Make_Function_Specification (Loc,
6028 Defining_Unit_Name => Func,
6029 Parameter_Specifications => New_List (
6030 Make_Parameter_Specification (Loc,
6031 Defining_Identifier => F1,
6032 Parameter_Type => New_Occurrence_Of (Op_Type, Loc))),
6033 Result_Definition => New_Occurrence_Of (Ret_Type, Loc));
6034
6035 if Is_Binary then
6036 Append_To (Parameter_Specifications (Spec),
6037 Make_Parameter_Specification (Loc,
6038 Defining_Identifier => F2,
6039 Parameter_Type => New_Occurrence_Of (Op_Type, Loc)));
6040 end if;
6041
6042 -- Build expression as a function call, or as an operator node
6043 -- that corresponds to the name of the actual, starting with
6044 -- binary operators.
6045
6046 if Op_Name not in Any_Operator_Name then
6047 Expr :=
6048 Make_Function_Call (Loc,
6049 Name =>
6050 New_Occurrence_Of (Actual_Subp, Loc),
6051 Parameter_Associations => New_List (L));
6052
6053 if Is_Binary then
6054 Append_To (Parameter_Associations (Expr), R);
6055 end if;
6056
6057 -- Binary operators
6058
6059 elsif Is_Binary then
6060 if Op_Name = Name_Op_And then
6061 Expr := Make_Op_And (Loc, Left_Opnd => L, Right_Opnd => R);
6062 elsif Op_Name = Name_Op_Or then
6063 Expr := Make_Op_Or (Loc, Left_Opnd => L, Right_Opnd => R);
6064 elsif Op_Name = Name_Op_Xor then
6065 Expr := Make_Op_Xor (Loc, Left_Opnd => L, Right_Opnd => R);
6066 elsif Op_Name = Name_Op_Eq then
6067 Expr := Make_Op_Eq (Loc, Left_Opnd => L, Right_Opnd => R);
6068 elsif Op_Name = Name_Op_Ne then
6069 Expr := Make_Op_Ne (Loc, Left_Opnd => L, Right_Opnd => R);
6070 elsif Op_Name = Name_Op_Le then
6071 Expr := Make_Op_Le (Loc, Left_Opnd => L, Right_Opnd => R);
6072 elsif Op_Name = Name_Op_Gt then
6073 Expr := Make_Op_Gt (Loc, Left_Opnd => L, Right_Opnd => R);
6074 elsif Op_Name = Name_Op_Ge then
6075 Expr := Make_Op_Ge (Loc, Left_Opnd => L, Right_Opnd => R);
6076 elsif Op_Name = Name_Op_Lt then
6077 Expr := Make_Op_Lt (Loc, Left_Opnd => L, Right_Opnd => R);
6078 elsif Op_Name = Name_Op_Add then
6079 Expr := Make_Op_Add (Loc, Left_Opnd => L, Right_Opnd => R);
6080 elsif Op_Name = Name_Op_Subtract then
6081 Expr := Make_Op_Subtract (Loc, Left_Opnd => L, Right_Opnd => R);
6082 elsif Op_Name = Name_Op_Concat then
6083 Expr := Make_Op_Concat (Loc, Left_Opnd => L, Right_Opnd => R);
6084 elsif Op_Name = Name_Op_Multiply then
6085 Expr := Make_Op_Multiply (Loc, Left_Opnd => L, Right_Opnd => R);
6086 elsif Op_Name = Name_Op_Divide then
6087 Expr := Make_Op_Divide (Loc, Left_Opnd => L, Right_Opnd => R);
6088 elsif Op_Name = Name_Op_Mod then
6089 Expr := Make_Op_Mod (Loc, Left_Opnd => L, Right_Opnd => R);
6090 elsif Op_Name = Name_Op_Rem then
6091 Expr := Make_Op_Rem (Loc, Left_Opnd => L, Right_Opnd => R);
6092 elsif Op_Name = Name_Op_Expon then
6093 Expr := Make_Op_Expon (Loc, Left_Opnd => L, Right_Opnd => R);
6094 end if;
6095
6096 -- Unary operators
6097
6098 else
6099 if Op_Name = Name_Op_Add then
6100 Expr := Make_Op_Plus (Loc, Right_Opnd => L);
6101 elsif Op_Name = Name_Op_Subtract then
6102 Expr := Make_Op_Minus (Loc, Right_Opnd => L);
6103 elsif Op_Name = Name_Op_Abs then
6104 Expr := Make_Op_Abs (Loc, Right_Opnd => L);
6105 elsif Op_Name = Name_Op_Not then
6106 Expr := Make_Op_Not (Loc, Right_Opnd => L);
6107 end if;
6108 end if;
6109
6110 Decl :=
6111 Make_Expression_Function (Loc,
6112 Specification => Spec,
6113 Expression => Expr);
6114
6115 return Decl;
6116 end Build_Operator_Wrapper;
6117
6118 -------------------------------------------
6119 -- Build_Instance_Compilation_Unit_Nodes --
6120 -------------------------------------------
6121
6122 procedure Build_Instance_Compilation_Unit_Nodes
6123 (N : Node_Id;
6124 Act_Body : Node_Id;
6125 Act_Decl : Node_Id)
6126 is
6127 Decl_Cunit : Node_Id;
6128 Body_Cunit : Node_Id;
6129 Citem : Node_Id;
6130 New_Main : constant Entity_Id := Defining_Entity (Act_Decl);
6131 Old_Main : constant Entity_Id := Cunit_Entity (Main_Unit);
6132
6133 begin
6134 -- A new compilation unit node is built for the instance declaration
6135
6136 Decl_Cunit :=
6137 Make_Compilation_Unit (Sloc (N),
6138 Context_Items => Empty_List,
6139 Unit => Act_Decl,
6140 Aux_Decls_Node => Make_Compilation_Unit_Aux (Sloc (N)));
6141
6142 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
6143
6144 -- The new compilation unit is linked to its body, but both share the
6145 -- same file, so we do not set Body_Required on the new unit so as not
6146 -- to create a spurious dependency on a non-existent body in the ali.
6147 -- This simplifies CodePeer unit traversal.
6148
6149 -- We use the original instantiation compilation unit as the resulting
6150 -- compilation unit of the instance, since this is the main unit.
6151
6152 Rewrite (N, Act_Body);
6153
6154 -- Propagate the aspect specifications from the package body template to
6155 -- the instantiated version of the package body.
6156
6157 if Has_Aspects (Act_Body) then
6158 Set_Aspect_Specifications
6159 (N, New_Copy_List_Tree (Aspect_Specifications (Act_Body)));
6160 end if;
6161
6162 Body_Cunit := Parent (N);
6163
6164 -- The two compilation unit nodes are linked by the Library_Unit field
6165
6166 Set_Library_Unit (Decl_Cunit, Body_Cunit);
6167 Set_Library_Unit (Body_Cunit, Decl_Cunit);
6168
6169 -- Preserve the private nature of the package if needed
6170
6171 Set_Private_Present (Decl_Cunit, Private_Present (Body_Cunit));
6172
6173 -- If the instance is not the main unit, its context, categorization
6174 -- and elaboration entity are not relevant to the compilation.
6175
6176 if Body_Cunit /= Cunit (Main_Unit) then
6177 Make_Instance_Unit (Body_Cunit, In_Main => False);
6178 return;
6179 end if;
6180
6181 -- The context clause items on the instantiation, which are now attached
6182 -- to the body compilation unit (since the body overwrote the original
6183 -- instantiation node), semantically belong on the spec, so copy them
6184 -- there. It's harmless to leave them on the body as well. In fact one
6185 -- could argue that they belong in both places.
6186
6187 Citem := First (Context_Items (Body_Cunit));
6188 while Present (Citem) loop
6189 Append (New_Copy (Citem), Context_Items (Decl_Cunit));
6190 Next (Citem);
6191 end loop;
6192
6193 -- Propagate categorization flags on packages, so that they appear in
6194 -- the ali file for the spec of the unit.
6195
6196 if Ekind (New_Main) = E_Package then
6197 Set_Is_Pure (Old_Main, Is_Pure (New_Main));
6198 Set_Is_Preelaborated (Old_Main, Is_Preelaborated (New_Main));
6199 Set_Is_Remote_Types (Old_Main, Is_Remote_Types (New_Main));
6200 Set_Is_Shared_Passive (Old_Main, Is_Shared_Passive (New_Main));
6201 Set_Is_Remote_Call_Interface
6202 (Old_Main, Is_Remote_Call_Interface (New_Main));
6203 end if;
6204
6205 -- Make entry in Units table, so that binder can generate call to
6206 -- elaboration procedure for body, if any.
6207
6208 Make_Instance_Unit (Body_Cunit, In_Main => True);
6209 Main_Unit_Entity := New_Main;
6210 Set_Cunit_Entity (Main_Unit, Main_Unit_Entity);
6211
6212 -- Build elaboration entity, since the instance may certainly generate
6213 -- elaboration code requiring a flag for protection.
6214
6215 Build_Elaboration_Entity (Decl_Cunit, New_Main);
6216 end Build_Instance_Compilation_Unit_Nodes;
6217
6218 -----------------------------
6219 -- Check_Access_Definition --
6220 -----------------------------
6221
6222 procedure Check_Access_Definition (N : Node_Id) is
6223 begin
6224 pragma Assert
6225 (Ada_Version >= Ada_2005 and then Present (Access_Definition (N)));
6226 null;
6227 end Check_Access_Definition;
6228
6229 -----------------------------------
6230 -- Check_Formal_Package_Instance --
6231 -----------------------------------
6232
6233 -- If the formal has specific parameters, they must match those of the
6234 -- actual. Both of them are instances, and the renaming declarations for
6235 -- their formal parameters appear in the same order in both. The analyzed
6236 -- formal has been analyzed in the context of the current instance.
6237
6238 procedure Check_Formal_Package_Instance
6239 (Formal_Pack : Entity_Id;
6240 Actual_Pack : Entity_Id)
6241 is
6242 E1 : Entity_Id := First_Entity (Actual_Pack);
6243 E2 : Entity_Id := First_Entity (Formal_Pack);
6244 Prev_E1 : Entity_Id;
6245
6246 Expr1 : Node_Id;
6247 Expr2 : Node_Id;
6248
6249 procedure Check_Mismatch (B : Boolean);
6250 -- Common error routine for mismatch between the parameters of the
6251 -- actual instance and those of the formal package.
6252
6253 function Is_Defaulted (Param : Entity_Id) return Boolean;
6254 -- If the formal package has partly box-initialized formals, skip
6255 -- conformance check for these formals. Previously the code assumed
6256 -- that box initialization for a formal package applied to all its
6257 -- formal parameters.
6258
6259 function Same_Instantiated_Constant (E1, E2 : Entity_Id) return Boolean;
6260 -- The formal may come from a nested formal package, and the actual may
6261 -- have been constant-folded. To determine whether the two denote the
6262 -- same entity we may have to traverse several definitions to recover
6263 -- the ultimate entity that they refer to.
6264
6265 function Same_Instantiated_Function (E1, E2 : Entity_Id) return Boolean;
6266 -- The formal and the actual must be identical, but if both are
6267 -- given by attributes they end up renaming different generated bodies,
6268 -- and we must verify that the attributes themselves match.
6269
6270 function Same_Instantiated_Variable (E1, E2 : Entity_Id) return Boolean;
6271 -- Similarly, if the formal comes from a nested formal package, the
6272 -- actual may designate the formal through multiple renamings, which
6273 -- have to be followed to determine the original variable in question.
6274
6275 --------------------
6276 -- Check_Mismatch --
6277 --------------------
6278
6279 procedure Check_Mismatch (B : Boolean) is
6280 -- A Formal_Type_Declaration for a derived private type is rewritten
6281 -- as a private extension decl. (see Analyze_Formal_Derived_Type),
6282 -- which is why we examine the original node.
6283
6284 Kind : constant Node_Kind := Nkind (Original_Node (Parent (E2)));
6285
6286 begin
6287 if Kind = N_Formal_Type_Declaration then
6288 return;
6289
6290 elsif Nkind_In (Kind, N_Formal_Object_Declaration,
6291 N_Formal_Package_Declaration)
6292 or else Kind in N_Formal_Subprogram_Declaration
6293 then
6294 null;
6295
6296 -- Ada 2012: If both formal and actual are incomplete types they
6297 -- are conformant.
6298
6299 elsif Is_Incomplete_Type (E1) and then Is_Incomplete_Type (E2) then
6300 null;
6301
6302 elsif B then
6303 Error_Msg_NE
6304 ("actual for & in actual instance does not match formal",
6305 Parent (Actual_Pack), E1);
6306 end if;
6307 end Check_Mismatch;
6308
6309 ------------------
6310 -- Is_Defaulted --
6311 ------------------
6312
6313 function Is_Defaulted (Param : Entity_Id) return Boolean is
6314 Assoc : Node_Id;
6315
6316 begin
6317 Assoc :=
6318 First (Generic_Associations (Parent
6319 (Associated_Formal_Package (Actual_Pack))));
6320
6321 while Present (Assoc) loop
6322 if Nkind (Assoc) = N_Others_Choice then
6323 return True;
6324
6325 elsif Nkind (Assoc) = N_Generic_Association
6326 and then Chars (Selector_Name (Assoc)) = Chars (Param)
6327 then
6328 return Box_Present (Assoc);
6329 end if;
6330
6331 Next (Assoc);
6332 end loop;
6333
6334 return False;
6335 end Is_Defaulted;
6336
6337 --------------------------------
6338 -- Same_Instantiated_Constant --
6339 --------------------------------
6340
6341 function Same_Instantiated_Constant
6342 (E1, E2 : Entity_Id) return Boolean
6343 is
6344 Ent : Entity_Id;
6345
6346 begin
6347 Ent := E2;
6348 while Present (Ent) loop
6349 if E1 = Ent then
6350 return True;
6351
6352 elsif Ekind (Ent) /= E_Constant then
6353 return False;
6354
6355 elsif Is_Entity_Name (Constant_Value (Ent)) then
6356 if Entity (Constant_Value (Ent)) = E1 then
6357 return True;
6358 else
6359 Ent := Entity (Constant_Value (Ent));
6360 end if;
6361
6362 -- The actual may be a constant that has been folded. Recover
6363 -- original name.
6364
6365 elsif Is_Entity_Name (Original_Node (Constant_Value (Ent))) then
6366 Ent := Entity (Original_Node (Constant_Value (Ent)));
6367
6368 else
6369 return False;
6370 end if;
6371 end loop;
6372
6373 return False;
6374 end Same_Instantiated_Constant;
6375
6376 --------------------------------
6377 -- Same_Instantiated_Function --
6378 --------------------------------
6379
6380 function Same_Instantiated_Function
6381 (E1, E2 : Entity_Id) return Boolean
6382 is
6383 U1, U2 : Node_Id;
6384 begin
6385 if Alias (E1) = Alias (E2) then
6386 return True;
6387
6388 elsif Present (Alias (E2)) then
6389 U1 := Original_Node (Unit_Declaration_Node (E1));
6390 U2 := Original_Node (Unit_Declaration_Node (Alias (E2)));
6391
6392 return Nkind (U1) = N_Subprogram_Renaming_Declaration
6393 and then Nkind (Name (U1)) = N_Attribute_Reference
6394
6395 and then Nkind (U2) = N_Subprogram_Renaming_Declaration
6396 and then Nkind (Name (U2)) = N_Attribute_Reference
6397
6398 and then
6399 Attribute_Name (Name (U1)) = Attribute_Name (Name (U2));
6400 else
6401 return False;
6402 end if;
6403 end Same_Instantiated_Function;
6404
6405 --------------------------------
6406 -- Same_Instantiated_Variable --
6407 --------------------------------
6408
6409 function Same_Instantiated_Variable
6410 (E1, E2 : Entity_Id) return Boolean
6411 is
6412 function Original_Entity (E : Entity_Id) return Entity_Id;
6413 -- Follow chain of renamings to the ultimate ancestor
6414
6415 ---------------------
6416 -- Original_Entity --
6417 ---------------------
6418
6419 function Original_Entity (E : Entity_Id) return Entity_Id is
6420 Orig : Entity_Id;
6421
6422 begin
6423 Orig := E;
6424 while Nkind (Parent (Orig)) = N_Object_Renaming_Declaration
6425 and then Present (Renamed_Object (Orig))
6426 and then Is_Entity_Name (Renamed_Object (Orig))
6427 loop
6428 Orig := Entity (Renamed_Object (Orig));
6429 end loop;
6430
6431 return Orig;
6432 end Original_Entity;
6433
6434 -- Start of processing for Same_Instantiated_Variable
6435
6436 begin
6437 return Ekind (E1) = Ekind (E2)
6438 and then Original_Entity (E1) = Original_Entity (E2);
6439 end Same_Instantiated_Variable;
6440
6441 -- Start of processing for Check_Formal_Package_Instance
6442
6443 begin
6444 Prev_E1 := E1;
6445 while Present (E1) and then Present (E2) loop
6446 exit when Ekind (E1) = E_Package
6447 and then Renamed_Entity (E1) = Renamed_Entity (Actual_Pack);
6448
6449 -- If the formal is the renaming of the formal package, this
6450 -- is the end of its formal part, which may occur before the
6451 -- end of the formal part in the actual in the presence of
6452 -- defaulted parameters in the formal package.
6453
6454 exit when Nkind (Parent (E2)) = N_Package_Renaming_Declaration
6455 and then Renamed_Entity (E2) = Scope (E2);
6456
6457 -- The analysis of the actual may generate additional internal
6458 -- entities. If the formal is defaulted, there is no corresponding
6459 -- analysis and the internal entities must be skipped, until we
6460 -- find corresponding entities again.
6461
6462 if Comes_From_Source (E2)
6463 and then not Comes_From_Source (E1)
6464 and then Chars (E1) /= Chars (E2)
6465 then
6466 while Present (E1) and then Chars (E1) /= Chars (E2) loop
6467 Next_Entity (E1);
6468 end loop;
6469 end if;
6470
6471 if No (E1) then
6472 return;
6473
6474 -- Entities may be declared without full declaration, such as
6475 -- itypes and predefined operators (concatenation for arrays, eg).
6476 -- Skip it and keep the formal entity to find a later match for it.
6477
6478 elsif No (Parent (E2)) and then Ekind (E1) /= Ekind (E2) then
6479 E1 := Prev_E1;
6480 goto Next_E;
6481
6482 -- If the formal entity comes from a formal declaration, it was
6483 -- defaulted in the formal package, and no check is needed on it.
6484
6485 elsif Nkind_In (Original_Node (Parent (E2)),
6486 N_Formal_Object_Declaration,
6487 N_Formal_Type_Declaration)
6488 then
6489 -- If the formal is a tagged type the corresponding class-wide
6490 -- type has been generated as well, and it must be skipped.
6491
6492 if Is_Type (E2) and then Is_Tagged_Type (E2) then
6493 Next_Entity (E2);
6494 end if;
6495
6496 goto Next_E;
6497
6498 -- Ditto for defaulted formal subprograms.
6499
6500 elsif Is_Overloadable (E1)
6501 and then Nkind (Unit_Declaration_Node (E2)) in
6502 N_Formal_Subprogram_Declaration
6503 then
6504 goto Next_E;
6505
6506 elsif Is_Defaulted (E1) then
6507 goto Next_E;
6508
6509 elsif Is_Type (E1) then
6510
6511 -- Subtypes must statically match. E1, E2 are the local entities
6512 -- that are subtypes of the actuals. Itypes generated for other
6513 -- parameters need not be checked, the check will be performed
6514 -- on the parameters themselves.
6515
6516 -- If E2 is a formal type declaration, it is a defaulted parameter
6517 -- and needs no checking.
6518
6519 if not Is_Itype (E1) and then not Is_Itype (E2) then
6520 Check_Mismatch
6521 (not Is_Type (E2)
6522 or else Etype (E1) /= Etype (E2)
6523 or else not Subtypes_Statically_Match (E1, E2));
6524 end if;
6525
6526 elsif Ekind (E1) = E_Constant then
6527
6528 -- IN parameters must denote the same static value, or the same
6529 -- constant, or the literal null.
6530
6531 Expr1 := Expression (Parent (E1));
6532
6533 if Ekind (E2) /= E_Constant then
6534 Check_Mismatch (True);
6535 goto Next_E;
6536 else
6537 Expr2 := Expression (Parent (E2));
6538 end if;
6539
6540 if Is_OK_Static_Expression (Expr1) then
6541 if not Is_OK_Static_Expression (Expr2) then
6542 Check_Mismatch (True);
6543
6544 elsif Is_Discrete_Type (Etype (E1)) then
6545 declare
6546 V1 : constant Uint := Expr_Value (Expr1);
6547 V2 : constant Uint := Expr_Value (Expr2);
6548 begin
6549 Check_Mismatch (V1 /= V2);
6550 end;
6551
6552 elsif Is_Real_Type (Etype (E1)) then
6553 declare
6554 V1 : constant Ureal := Expr_Value_R (Expr1);
6555 V2 : constant Ureal := Expr_Value_R (Expr2);
6556 begin
6557 Check_Mismatch (V1 /= V2);
6558 end;
6559
6560 elsif Is_String_Type (Etype (E1))
6561 and then Nkind (Expr1) = N_String_Literal
6562 then
6563 if Nkind (Expr2) /= N_String_Literal then
6564 Check_Mismatch (True);
6565 else
6566 Check_Mismatch
6567 (not String_Equal (Strval (Expr1), Strval (Expr2)));
6568 end if;
6569 end if;
6570
6571 elsif Is_Entity_Name (Expr1) then
6572 if Is_Entity_Name (Expr2) then
6573 if Entity (Expr1) = Entity (Expr2) then
6574 null;
6575 else
6576 Check_Mismatch
6577 (not Same_Instantiated_Constant
6578 (Entity (Expr1), Entity (Expr2)));
6579 end if;
6580
6581 else
6582 Check_Mismatch (True);
6583 end if;
6584
6585 elsif Is_Entity_Name (Original_Node (Expr1))
6586 and then Is_Entity_Name (Expr2)
6587 and then Same_Instantiated_Constant
6588 (Entity (Original_Node (Expr1)), Entity (Expr2))
6589 then
6590 null;
6591
6592 elsif Nkind (Expr1) = N_Null then
6593 Check_Mismatch (Nkind (Expr1) /= N_Null);
6594
6595 else
6596 Check_Mismatch (True);
6597 end if;
6598
6599 elsif Ekind (E1) = E_Variable then
6600 Check_Mismatch (not Same_Instantiated_Variable (E1, E2));
6601
6602 elsif Ekind (E1) = E_Package then
6603 Check_Mismatch
6604 (Ekind (E1) /= Ekind (E2)
6605 or else (Present (Renamed_Object (E2))
6606 and then Renamed_Object (E1) /=
6607 Renamed_Object (E2)));
6608
6609 elsif Is_Overloadable (E1) then
6610 -- Verify that the actual subprograms match. Note that actuals
6611 -- that are attributes are rewritten as subprograms. If the
6612 -- subprogram in the formal package is defaulted, no check is
6613 -- needed. Note that this can only happen in Ada 2005 when the
6614 -- formal package can be partially parameterized.
6615
6616 if Nkind (Unit_Declaration_Node (E1)) =
6617 N_Subprogram_Renaming_Declaration
6618 and then From_Default (Unit_Declaration_Node (E1))
6619 then
6620 null;
6621
6622 -- If the formal package has an "others" box association that
6623 -- covers this formal, there is no need for a check either.
6624
6625 elsif Nkind (Unit_Declaration_Node (E2)) in
6626 N_Formal_Subprogram_Declaration
6627 and then Box_Present (Unit_Declaration_Node (E2))
6628 then
6629 null;
6630
6631 -- No check needed if subprogram is a defaulted null procedure
6632
6633 elsif No (Alias (E2))
6634 and then Ekind (E2) = E_Procedure
6635 and then
6636 Null_Present (Specification (Unit_Declaration_Node (E2)))
6637 then
6638 null;
6639
6640 -- Otherwise the actual in the formal and the actual in the
6641 -- instantiation of the formal must match, up to renamings.
6642
6643 else
6644 Check_Mismatch
6645 (Ekind (E2) /= Ekind (E1)
6646 or else not Same_Instantiated_Function (E1, E2));
6647 end if;
6648
6649 else
6650 raise Program_Error;
6651 end if;
6652
6653 <<Next_E>>
6654 Prev_E1 := E1;
6655 Next_Entity (E1);
6656 Next_Entity (E2);
6657 end loop;
6658 end Check_Formal_Package_Instance;
6659
6660 ---------------------------
6661 -- Check_Formal_Packages --
6662 ---------------------------
6663
6664 procedure Check_Formal_Packages (P_Id : Entity_Id) is
6665 E : Entity_Id;
6666 Formal_P : Entity_Id;
6667 Formal_Decl : Node_Id;
6668 begin
6669 -- Iterate through the declarations in the instance, looking for package
6670 -- renaming declarations that denote instances of formal packages. Stop
6671 -- when we find the renaming of the current package itself. The
6672 -- declaration for a formal package without a box is followed by an
6673 -- internal entity that repeats the instantiation.
6674
6675 E := First_Entity (P_Id);
6676 while Present (E) loop
6677 if Ekind (E) = E_Package then
6678 if Renamed_Object (E) = P_Id then
6679 exit;
6680
6681 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
6682 null;
6683
6684 else
6685 Formal_Decl := Parent (Associated_Formal_Package (E));
6686
6687 -- Nothing to check if the formal has a box or an others_clause
6688 -- (necessarily with a box), or no associations altogether
6689
6690 if Box_Present (Formal_Decl)
6691 or else No (Generic_Associations (Formal_Decl))
6692 then
6693 null;
6694
6695 elsif Nkind (First (Generic_Associations (Formal_Decl))) =
6696 N_Others_Choice
6697 then
6698 -- The internal validating package was generated but formal
6699 -- and instance are known to be compatible.
6700
6701 Formal_P := Next_Entity (E);
6702 Remove (Unit_Declaration_Node (Formal_P));
6703
6704 else
6705 Formal_P := Next_Entity (E);
6706
6707 -- If the instance is within an enclosing instance body
6708 -- there is no need to verify the legality of current formal
6709 -- packages because they were legal in the generic body.
6710 -- This optimization may be applicable elsewhere, and it
6711 -- also removes spurious errors that may arise with
6712 -- on-the-fly inlining and confusion between private and
6713 -- full views.
6714
6715 if not In_Instance_Body then
6716 Check_Formal_Package_Instance (Formal_P, E);
6717 end if;
6718
6719 -- Restore the visibility of formals of the formal instance
6720 -- that are not defaulted, and are hidden within the current
6721 -- generic. These formals may be visible within an enclosing
6722 -- generic.
6723
6724 declare
6725 Elmt : Elmt_Id;
6726 begin
6727 Elmt := First_Elmt (Hidden_In_Formal_Instance (Formal_P));
6728 while Present (Elmt) loop
6729 Set_Is_Hidden (Node (Elmt), False);
6730 Next_Elmt (Elmt);
6731 end loop;
6732 end;
6733
6734 -- After checking, remove the internal validating package.
6735 -- It is only needed for semantic checks, and as it may
6736 -- contain generic formal declarations it should not reach
6737 -- gigi.
6738
6739 Remove (Unit_Declaration_Node (Formal_P));
6740 end if;
6741 end if;
6742 end if;
6743
6744 Next_Entity (E);
6745 end loop;
6746 end Check_Formal_Packages;
6747
6748 ---------------------------------
6749 -- Check_Forward_Instantiation --
6750 ---------------------------------
6751
6752 procedure Check_Forward_Instantiation (Decl : Node_Id) is
6753 S : Entity_Id;
6754 Gen_Comp : Entity_Id := Cunit_Entity (Get_Source_Unit (Decl));
6755
6756 begin
6757 -- The instantiation appears before the generic body if we are in the
6758 -- scope of the unit containing the generic, either in its spec or in
6759 -- the package body, and before the generic body.
6760
6761 if Ekind (Gen_Comp) = E_Package_Body then
6762 Gen_Comp := Spec_Entity (Gen_Comp);
6763 end if;
6764
6765 if In_Open_Scopes (Gen_Comp)
6766 and then No (Corresponding_Body (Decl))
6767 then
6768 S := Current_Scope;
6769
6770 while Present (S)
6771 and then not Is_Compilation_Unit (S)
6772 and then not Is_Child_Unit (S)
6773 loop
6774 if Ekind (S) = E_Package then
6775 Set_Has_Forward_Instantiation (S);
6776 end if;
6777
6778 S := Scope (S);
6779 end loop;
6780 end if;
6781 end Check_Forward_Instantiation;
6782
6783 ---------------------------
6784 -- Check_Generic_Actuals --
6785 ---------------------------
6786
6787 -- The visibility of the actuals may be different between the point of
6788 -- generic instantiation and the instantiation of the body.
6789
6790 procedure Check_Generic_Actuals
6791 (Instance : Entity_Id;
6792 Is_Formal_Box : Boolean)
6793 is
6794 E : Entity_Id;
6795 Astype : Entity_Id;
6796
6797 begin
6798 E := First_Entity (Instance);
6799 while Present (E) loop
6800 if Is_Type (E)
6801 and then Nkind (Parent (E)) = N_Subtype_Declaration
6802 and then Scope (Etype (E)) /= Instance
6803 and then Is_Entity_Name (Subtype_Indication (Parent (E)))
6804 then
6805 -- Restore the proper view of the actual from the information
6806 -- saved earlier by Instantiate_Type.
6807
6808 Check_Private_View (Subtype_Indication (Parent (E)));
6809
6810 -- If the actual is itself the formal of a parent instance,
6811 -- then also restore the proper view of its actual and so on.
6812 -- That's necessary for nested instantiations of the form
6813
6814 -- generic
6815 -- type Component is private;
6816 -- type Array_Type is array (Positive range <>) of Component;
6817 -- procedure Proc;
6818
6819 -- when the outermost actuals have inconsistent views, because
6820 -- the Component_Type of Array_Type of the inner instantiations
6821 -- is the actual of Component of the outermost one and not that
6822 -- of the corresponding inner instantiations.
6823
6824 Astype := Ancestor_Subtype (E);
6825 while Present (Astype)
6826 and then Nkind (Parent (Astype)) = N_Subtype_Declaration
6827 and then Present (Generic_Parent_Type (Parent (Astype)))
6828 and then Is_Entity_Name (Subtype_Indication (Parent (Astype)))
6829 loop
6830 Check_Private_View (Subtype_Indication (Parent (Astype)));
6831 Astype := Ancestor_Subtype (Astype);
6832 end loop;
6833
6834 Set_Is_Generic_Actual_Type (E);
6835
6836 if Is_Private_Type (E) and then Present (Full_View (E)) then
6837 Set_Is_Generic_Actual_Type (Full_View (E));
6838 end if;
6839
6840 Set_Is_Hidden (E, False);
6841 Set_Is_Potentially_Use_Visible (E, In_Use (Instance));
6842
6843 -- We constructed the generic actual type as a subtype of the
6844 -- supplied type. This means that it normally would not inherit
6845 -- subtype specific attributes of the actual, which is wrong for
6846 -- the generic case.
6847
6848 Astype := Ancestor_Subtype (E);
6849
6850 if No (Astype) then
6851
6852 -- This can happen when E is an itype that is the full view of
6853 -- a private type completed, e.g. with a constrained array. In
6854 -- that case, use the first subtype, which will carry size
6855 -- information. The base type itself is unconstrained and will
6856 -- not carry it.
6857
6858 Astype := First_Subtype (E);
6859 end if;
6860
6861 Set_Size_Info (E, (Astype));
6862 Set_RM_Size (E, RM_Size (Astype));
6863 Set_First_Rep_Item (E, First_Rep_Item (Astype));
6864
6865 if Is_Discrete_Or_Fixed_Point_Type (E) then
6866 Set_RM_Size (E, RM_Size (Astype));
6867 end if;
6868
6869 elsif Ekind (E) = E_Package then
6870
6871 -- If this is the renaming for the current instance, we're done.
6872 -- Otherwise it is a formal package. If the corresponding formal
6873 -- was declared with a box, the (instantiations of the) generic
6874 -- formal part are also visible. Otherwise, ignore the entity
6875 -- created to validate the actuals.
6876
6877 if Renamed_Object (E) = Instance then
6878 exit;
6879
6880 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
6881 null;
6882
6883 -- The visibility of a formal of an enclosing generic is already
6884 -- correct.
6885
6886 elsif Denotes_Formal_Package (E) then
6887 null;
6888
6889 elsif Present (Associated_Formal_Package (E))
6890 and then not Is_Generic_Formal (E)
6891 then
6892 if Box_Present (Parent (Associated_Formal_Package (E))) then
6893 Check_Generic_Actuals (Renamed_Object (E), True);
6894
6895 else
6896 Check_Generic_Actuals (Renamed_Object (E), False);
6897 end if;
6898
6899 Set_Is_Hidden (E, False);
6900 end if;
6901
6902 -- If this is a subprogram instance (in a wrapper package) the
6903 -- actual is fully visible.
6904
6905 elsif Is_Wrapper_Package (Instance) then
6906 Set_Is_Hidden (E, False);
6907
6908 -- If the formal package is declared with a box, or if the formal
6909 -- parameter is defaulted, it is visible in the body.
6910
6911 elsif Is_Formal_Box or else Is_Visible_Formal (E) then
6912 Set_Is_Hidden (E, False);
6913 end if;
6914
6915 if Ekind (E) = E_Constant then
6916
6917 -- If the type of the actual is a private type declared in the
6918 -- enclosing scope of the generic unit, the body of the generic
6919 -- sees the full view of the type (because it has to appear in
6920 -- the corresponding package body). If the type is private now,
6921 -- exchange views to restore the proper visiblity in the instance.
6922
6923 declare
6924 Typ : constant Entity_Id := Base_Type (Etype (E));
6925 -- The type of the actual
6926
6927 Gen_Id : Entity_Id;
6928 -- The generic unit
6929
6930 Parent_Scope : Entity_Id;
6931 -- The enclosing scope of the generic unit
6932
6933 begin
6934 if Is_Wrapper_Package (Instance) then
6935 Gen_Id :=
6936 Generic_Parent
6937 (Specification
6938 (Unit_Declaration_Node
6939 (Related_Instance (Instance))));
6940 else
6941 Gen_Id :=
6942 Generic_Parent (Package_Specification (Instance));
6943 end if;
6944
6945 Parent_Scope := Scope (Gen_Id);
6946
6947 -- The exchange is only needed if the generic is defined
6948 -- within a package which is not a common ancestor of the
6949 -- scope of the instance, and is not already in scope.
6950
6951 if Is_Private_Type (Typ)
6952 and then Scope (Typ) = Parent_Scope
6953 and then Scope (Instance) /= Parent_Scope
6954 and then Ekind (Parent_Scope) = E_Package
6955 and then not Is_Child_Unit (Gen_Id)
6956 then
6957 Switch_View (Typ);
6958
6959 -- If the type of the entity is a subtype, it may also have
6960 -- to be made visible, together with the base type of its
6961 -- full view, after exchange.
6962
6963 if Is_Private_Type (Etype (E)) then
6964 Switch_View (Etype (E));
6965 Switch_View (Base_Type (Etype (E)));
6966 end if;
6967 end if;
6968 end;
6969 end if;
6970
6971 Next_Entity (E);
6972 end loop;
6973 end Check_Generic_Actuals;
6974
6975 ------------------------------
6976 -- Check_Generic_Child_Unit --
6977 ------------------------------
6978
6979 procedure Check_Generic_Child_Unit
6980 (Gen_Id : Node_Id;
6981 Parent_Installed : in out Boolean)
6982 is
6983 Loc : constant Source_Ptr := Sloc (Gen_Id);
6984 Gen_Par : Entity_Id := Empty;
6985 E : Entity_Id;
6986 Inst_Par : Entity_Id;
6987 S : Node_Id;
6988
6989 function Find_Generic_Child
6990 (Scop : Entity_Id;
6991 Id : Node_Id) return Entity_Id;
6992 -- Search generic parent for possible child unit with the given name
6993
6994 function In_Enclosing_Instance return Boolean;
6995 -- Within an instance of the parent, the child unit may be denoted by
6996 -- a simple name, or an abbreviated expanded name. Examine enclosing
6997 -- scopes to locate a possible parent instantiation.
6998
6999 ------------------------
7000 -- Find_Generic_Child --
7001 ------------------------
7002
7003 function Find_Generic_Child
7004 (Scop : Entity_Id;
7005 Id : Node_Id) return Entity_Id
7006 is
7007 E : Entity_Id;
7008
7009 begin
7010 -- If entity of name is already set, instance has already been
7011 -- resolved, e.g. in an enclosing instantiation.
7012
7013 if Present (Entity (Id)) then
7014 if Scope (Entity (Id)) = Scop then
7015 return Entity (Id);
7016 else
7017 return Empty;
7018 end if;
7019
7020 else
7021 E := First_Entity (Scop);
7022 while Present (E) loop
7023 if Chars (E) = Chars (Id)
7024 and then Is_Child_Unit (E)
7025 then
7026 if Is_Child_Unit (E)
7027 and then not Is_Visible_Lib_Unit (E)
7028 then
7029 Error_Msg_NE
7030 ("generic child unit& is not visible", Gen_Id, E);
7031 end if;
7032
7033 Set_Entity (Id, E);
7034 return E;
7035 end if;
7036
7037 Next_Entity (E);
7038 end loop;
7039
7040 return Empty;
7041 end if;
7042 end Find_Generic_Child;
7043
7044 ---------------------------
7045 -- In_Enclosing_Instance --
7046 ---------------------------
7047
7048 function In_Enclosing_Instance return Boolean is
7049 Enclosing_Instance : Node_Id;
7050 Instance_Decl : Node_Id;
7051
7052 begin
7053 -- We do not inline any call that contains instantiations, except
7054 -- for instantiations of Unchecked_Conversion, so if we are within
7055 -- an inlined body the current instance does not require parents.
7056
7057 if In_Inlined_Body then
7058 pragma Assert (Chars (Gen_Id) = Name_Unchecked_Conversion);
7059 return False;
7060 end if;
7061
7062 -- Loop to check enclosing scopes
7063
7064 Enclosing_Instance := Current_Scope;
7065 while Present (Enclosing_Instance) loop
7066 Instance_Decl := Unit_Declaration_Node (Enclosing_Instance);
7067
7068 if Ekind (Enclosing_Instance) = E_Package
7069 and then Is_Generic_Instance (Enclosing_Instance)
7070 and then Present
7071 (Generic_Parent (Specification (Instance_Decl)))
7072 then
7073 -- Check whether the generic we are looking for is a child of
7074 -- this instance.
7075
7076 E := Find_Generic_Child
7077 (Generic_Parent (Specification (Instance_Decl)), Gen_Id);
7078 exit when Present (E);
7079
7080 else
7081 E := Empty;
7082 end if;
7083
7084 Enclosing_Instance := Scope (Enclosing_Instance);
7085 end loop;
7086
7087 if No (E) then
7088
7089 -- Not a child unit
7090
7091 Analyze (Gen_Id);
7092 return False;
7093
7094 else
7095 Rewrite (Gen_Id,
7096 Make_Expanded_Name (Loc,
7097 Chars => Chars (E),
7098 Prefix => New_Occurrence_Of (Enclosing_Instance, Loc),
7099 Selector_Name => New_Occurrence_Of (E, Loc)));
7100
7101 Set_Entity (Gen_Id, E);
7102 Set_Etype (Gen_Id, Etype (E));
7103 Parent_Installed := False; -- Already in scope.
7104 return True;
7105 end if;
7106 end In_Enclosing_Instance;
7107
7108 -- Start of processing for Check_Generic_Child_Unit
7109
7110 begin
7111 -- If the name of the generic is given by a selected component, it may
7112 -- be the name of a generic child unit, and the prefix is the name of an
7113 -- instance of the parent, in which case the child unit must be visible.
7114 -- If this instance is not in scope, it must be placed there and removed
7115 -- after instantiation, because what is being instantiated is not the
7116 -- original child, but the corresponding child present in the instance
7117 -- of the parent.
7118
7119 -- If the child is instantiated within the parent, it can be given by
7120 -- a simple name. In this case the instance is already in scope, but
7121 -- the child generic must be recovered from the generic parent as well.
7122
7123 if Nkind (Gen_Id) = N_Selected_Component then
7124 S := Selector_Name (Gen_Id);
7125 Analyze (Prefix (Gen_Id));
7126 Inst_Par := Entity (Prefix (Gen_Id));
7127
7128 if Ekind (Inst_Par) = E_Package
7129 and then Present (Renamed_Object (Inst_Par))
7130 then
7131 Inst_Par := Renamed_Object (Inst_Par);
7132 end if;
7133
7134 if Ekind (Inst_Par) = E_Package then
7135 if Nkind (Parent (Inst_Par)) = N_Package_Specification then
7136 Gen_Par := Generic_Parent (Parent (Inst_Par));
7137
7138 elsif Nkind (Parent (Inst_Par)) = N_Defining_Program_Unit_Name
7139 and then
7140 Nkind (Parent (Parent (Inst_Par))) = N_Package_Specification
7141 then
7142 Gen_Par := Generic_Parent (Parent (Parent (Inst_Par)));
7143 end if;
7144
7145 elsif Ekind (Inst_Par) = E_Generic_Package
7146 and then Nkind (Parent (Gen_Id)) = N_Formal_Package_Declaration
7147 then
7148 -- A formal package may be a real child package, and not the
7149 -- implicit instance within a parent. In this case the child is
7150 -- not visible and has to be retrieved explicitly as well.
7151
7152 Gen_Par := Inst_Par;
7153 end if;
7154
7155 if Present (Gen_Par) then
7156
7157 -- The prefix denotes an instantiation. The entity itself may be a
7158 -- nested generic, or a child unit.
7159
7160 E := Find_Generic_Child (Gen_Par, S);
7161
7162 if Present (E) then
7163 Change_Selected_Component_To_Expanded_Name (Gen_Id);
7164 Set_Entity (Gen_Id, E);
7165 Set_Etype (Gen_Id, Etype (E));
7166 Set_Entity (S, E);
7167 Set_Etype (S, Etype (E));
7168
7169 -- Indicate that this is a reference to the parent
7170
7171 if In_Extended_Main_Source_Unit (Gen_Id) then
7172 Set_Is_Instantiated (Inst_Par);
7173 end if;
7174
7175 -- A common mistake is to replicate the naming scheme of a
7176 -- hierarchy by instantiating a generic child directly, rather
7177 -- than the implicit child in a parent instance:
7178
7179 -- generic .. package Gpar is ..
7180 -- generic .. package Gpar.Child is ..
7181 -- package Par is new Gpar ();
7182
7183 -- with Gpar.Child;
7184 -- package Par.Child is new Gpar.Child ();
7185 -- rather than Par.Child
7186
7187 -- In this case the instantiation is within Par, which is an
7188 -- instance, but Gpar does not denote Par because we are not IN
7189 -- the instance of Gpar, so this is illegal. The test below
7190 -- recognizes this particular case.
7191
7192 if Is_Child_Unit (E)
7193 and then not Comes_From_Source (Entity (Prefix (Gen_Id)))
7194 and then (not In_Instance
7195 or else Nkind (Parent (Parent (Gen_Id))) =
7196 N_Compilation_Unit)
7197 then
7198 Error_Msg_N
7199 ("prefix of generic child unit must be instance of parent",
7200 Gen_Id);
7201 end if;
7202
7203 if not In_Open_Scopes (Inst_Par)
7204 and then Nkind (Parent (Gen_Id)) not in
7205 N_Generic_Renaming_Declaration
7206 then
7207 Install_Parent (Inst_Par);
7208 Parent_Installed := True;
7209
7210 elsif In_Open_Scopes (Inst_Par) then
7211
7212 -- If the parent is already installed, install the actuals
7213 -- for its formal packages. This is necessary when the child
7214 -- instance is a child of the parent instance: in this case,
7215 -- the parent is placed on the scope stack but the formal
7216 -- packages are not made visible.
7217
7218 Install_Formal_Packages (Inst_Par);
7219 end if;
7220
7221 else
7222 -- If the generic parent does not contain an entity that
7223 -- corresponds to the selector, the instance doesn't either.
7224 -- Analyzing the node will yield the appropriate error message.
7225 -- If the entity is not a child unit, then it is an inner
7226 -- generic in the parent.
7227
7228 Analyze (Gen_Id);
7229 end if;
7230
7231 else
7232 Analyze (Gen_Id);
7233
7234 if Is_Child_Unit (Entity (Gen_Id))
7235 and then
7236 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
7237 and then not In_Open_Scopes (Inst_Par)
7238 then
7239 Install_Parent (Inst_Par);
7240 Parent_Installed := True;
7241
7242 -- The generic unit may be the renaming of the implicit child
7243 -- present in an instance. In that case the parent instance is
7244 -- obtained from the name of the renamed entity.
7245
7246 elsif Ekind (Entity (Gen_Id)) = E_Generic_Package
7247 and then Present (Renamed_Entity (Entity (Gen_Id)))
7248 and then Is_Child_Unit (Renamed_Entity (Entity (Gen_Id)))
7249 then
7250 declare
7251 Renamed_Package : constant Node_Id :=
7252 Name (Parent (Entity (Gen_Id)));
7253 begin
7254 if Nkind (Renamed_Package) = N_Expanded_Name then
7255 Inst_Par := Entity (Prefix (Renamed_Package));
7256 Install_Parent (Inst_Par);
7257 Parent_Installed := True;
7258 end if;
7259 end;
7260 end if;
7261 end if;
7262
7263 elsif Nkind (Gen_Id) = N_Expanded_Name then
7264
7265 -- Entity already present, analyze prefix, whose meaning may be an
7266 -- instance in the current context. If it is an instance of a
7267 -- relative within another, the proper parent may still have to be
7268 -- installed, if they are not of the same generation.
7269
7270 Analyze (Prefix (Gen_Id));
7271
7272 -- Prevent cascaded errors
7273
7274 if Etype (Prefix (Gen_Id)) = Any_Type then
7275 return;
7276 end if;
7277
7278 -- In the unlikely case that a local declaration hides the name of
7279 -- the parent package, locate it on the homonym chain. If the context
7280 -- is an instance of the parent, the renaming entity is flagged as
7281 -- such.
7282
7283 Inst_Par := Entity (Prefix (Gen_Id));
7284 while Present (Inst_Par)
7285 and then not Is_Package_Or_Generic_Package (Inst_Par)
7286 loop
7287 Inst_Par := Homonym (Inst_Par);
7288 end loop;
7289
7290 pragma Assert (Present (Inst_Par));
7291 Set_Entity (Prefix (Gen_Id), Inst_Par);
7292
7293 if In_Enclosing_Instance then
7294 null;
7295
7296 elsif Present (Entity (Gen_Id))
7297 and then Is_Child_Unit (Entity (Gen_Id))
7298 and then not In_Open_Scopes (Inst_Par)
7299 then
7300 Install_Parent (Inst_Par);
7301 Parent_Installed := True;
7302 end if;
7303
7304 elsif In_Enclosing_Instance then
7305
7306 -- The child unit is found in some enclosing scope
7307
7308 null;
7309
7310 else
7311 Analyze (Gen_Id);
7312
7313 -- If this is the renaming of the implicit child in a parent
7314 -- instance, recover the parent name and install it.
7315
7316 if Is_Entity_Name (Gen_Id) then
7317 E := Entity (Gen_Id);
7318
7319 if Is_Generic_Unit (E)
7320 and then Nkind (Parent (E)) in N_Generic_Renaming_Declaration
7321 and then Is_Child_Unit (Renamed_Object (E))
7322 and then Is_Generic_Unit (Scope (Renamed_Object (E)))
7323 and then Nkind (Name (Parent (E))) = N_Expanded_Name
7324 then
7325 Rewrite (Gen_Id, New_Copy_Tree (Name (Parent (E))));
7326 Inst_Par := Entity (Prefix (Gen_Id));
7327
7328 if not In_Open_Scopes (Inst_Par) then
7329 Install_Parent (Inst_Par);
7330 Parent_Installed := True;
7331 end if;
7332
7333 -- If it is a child unit of a non-generic parent, it may be
7334 -- use-visible and given by a direct name. Install parent as
7335 -- for other cases.
7336
7337 elsif Is_Generic_Unit (E)
7338 and then Is_Child_Unit (E)
7339 and then
7340 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
7341 and then not Is_Generic_Unit (Scope (E))
7342 then
7343 if not In_Open_Scopes (Scope (E)) then
7344 Install_Parent (Scope (E));
7345 Parent_Installed := True;
7346 end if;
7347 end if;
7348 end if;
7349 end if;
7350 end Check_Generic_Child_Unit;
7351
7352 -----------------------------
7353 -- Check_Hidden_Child_Unit --
7354 -----------------------------
7355
7356 procedure Check_Hidden_Child_Unit
7357 (N : Node_Id;
7358 Gen_Unit : Entity_Id;
7359 Act_Decl_Id : Entity_Id)
7360 is
7361 Gen_Id : constant Node_Id := Name (N);
7362
7363 begin
7364 if Is_Child_Unit (Gen_Unit)
7365 and then Is_Child_Unit (Act_Decl_Id)
7366 and then Nkind (Gen_Id) = N_Expanded_Name
7367 and then Entity (Prefix (Gen_Id)) = Scope (Act_Decl_Id)
7368 and then Chars (Gen_Unit) = Chars (Act_Decl_Id)
7369 then
7370 Error_Msg_Node_2 := Scope (Act_Decl_Id);
7371 Error_Msg_NE
7372 ("generic unit & is implicitly declared in &",
7373 Defining_Unit_Name (N), Gen_Unit);
7374 Error_Msg_N ("\instance must have different name",
7375 Defining_Unit_Name (N));
7376 end if;
7377 end Check_Hidden_Child_Unit;
7378
7379 ------------------------
7380 -- Check_Private_View --
7381 ------------------------
7382
7383 procedure Check_Private_View (N : Node_Id) is
7384 T : constant Entity_Id := Etype (N);
7385 BT : Entity_Id;
7386
7387 begin
7388 -- Exchange views if the type was not private in the generic but is
7389 -- private at the point of instantiation. Do not exchange views if
7390 -- the scope of the type is in scope. This can happen if both generic
7391 -- and instance are sibling units, or if type is defined in a parent.
7392 -- In this case the visibility of the type will be correct for all
7393 -- semantic checks.
7394
7395 if Present (T) then
7396 BT := Base_Type (T);
7397
7398 if Is_Private_Type (T)
7399 and then not Has_Private_View (N)
7400 and then Present (Full_View (T))
7401 and then not In_Open_Scopes (Scope (T))
7402 then
7403 -- In the generic, the full declaration was visible
7404
7405 Switch_View (T);
7406
7407 elsif Has_Private_View (N)
7408 and then not Is_Private_Type (T)
7409 and then not Has_Been_Exchanged (T)
7410 and then (not In_Open_Scopes (Scope (T))
7411 or else Nkind (Parent (N)) = N_Subtype_Declaration)
7412 then
7413 -- In the generic, only the private declaration was visible
7414
7415 -- If the type appears in a subtype declaration, the subtype in
7416 -- instance must have a view compatible with that of its parent,
7417 -- which must be exchanged (see corresponding code in Restore_
7418 -- Private_Views) so we make an exception to the open scope rule.
7419
7420 Prepend_Elmt (T, Exchanged_Views);
7421 Exchange_Declarations (Etype (Get_Associated_Node (N)));
7422
7423 -- Finally, a non-private subtype may have a private base type, which
7424 -- must be exchanged for consistency. This can happen when a package
7425 -- body is instantiated, when the scope stack is empty but in fact
7426 -- the subtype and the base type are declared in an enclosing scope.
7427
7428 -- Note that in this case we introduce an inconsistency in the view
7429 -- set, because we switch the base type BT, but there could be some
7430 -- private dependent subtypes of BT which remain unswitched. Such
7431 -- subtypes might need to be switched at a later point (see specific
7432 -- provision for that case in Switch_View).
7433
7434 elsif not Is_Private_Type (T)
7435 and then not Has_Private_View (N)
7436 and then Is_Private_Type (BT)
7437 and then Present (Full_View (BT))
7438 and then not Is_Generic_Type (BT)
7439 and then not In_Open_Scopes (BT)
7440 then
7441 Prepend_Elmt (Full_View (BT), Exchanged_Views);
7442 Exchange_Declarations (BT);
7443 end if;
7444 end if;
7445 end Check_Private_View;
7446
7447 -----------------------------
7448 -- Check_Hidden_Primitives --
7449 -----------------------------
7450
7451 function Check_Hidden_Primitives (Assoc_List : List_Id) return Elist_Id is
7452 Actual : Node_Id;
7453 Gen_T : Entity_Id;
7454 Result : Elist_Id := No_Elist;
7455
7456 begin
7457 if No (Assoc_List) then
7458 return No_Elist;
7459 end if;
7460
7461 -- Traverse the list of associations between formals and actuals
7462 -- searching for renamings of tagged types
7463
7464 Actual := First (Assoc_List);
7465 while Present (Actual) loop
7466 if Nkind (Actual) = N_Subtype_Declaration then
7467 Gen_T := Generic_Parent_Type (Actual);
7468
7469 if Present (Gen_T) and then Is_Tagged_Type (Gen_T) then
7470
7471 -- Traverse the list of primitives of the actual types
7472 -- searching for hidden primitives that are visible in the
7473 -- corresponding generic formal; leave them visible and
7474 -- append them to Result to restore their decoration later.
7475
7476 Install_Hidden_Primitives
7477 (Prims_List => Result,
7478 Gen_T => Gen_T,
7479 Act_T => Entity (Subtype_Indication (Actual)));
7480 end if;
7481 end if;
7482
7483 Next (Actual);
7484 end loop;
7485
7486 return Result;
7487 end Check_Hidden_Primitives;
7488
7489 --------------------------
7490 -- Contains_Instance_Of --
7491 --------------------------
7492
7493 function Contains_Instance_Of
7494 (Inner : Entity_Id;
7495 Outer : Entity_Id;
7496 N : Node_Id) return Boolean
7497 is
7498 Elmt : Elmt_Id;
7499 Scop : Entity_Id;
7500
7501 begin
7502 Scop := Outer;
7503
7504 -- Verify that there are no circular instantiations. We check whether
7505 -- the unit contains an instance of the current scope or some enclosing
7506 -- scope (in case one of the instances appears in a subunit). Longer
7507 -- circularities involving subunits might seem too pathological to
7508 -- consider, but they were not too pathological for the authors of
7509 -- DEC bc30vsq, so we loop over all enclosing scopes, and mark all
7510 -- enclosing generic scopes as containing an instance.
7511
7512 loop
7513 -- Within a generic subprogram body, the scope is not generic, to
7514 -- allow for recursive subprograms. Use the declaration to determine
7515 -- whether this is a generic unit.
7516
7517 if Ekind (Scop) = E_Generic_Package
7518 or else (Is_Subprogram (Scop)
7519 and then Nkind (Unit_Declaration_Node (Scop)) =
7520 N_Generic_Subprogram_Declaration)
7521 then
7522 Elmt := First_Elmt (Inner_Instances (Inner));
7523
7524 while Present (Elmt) loop
7525 if Node (Elmt) = Scop then
7526 Error_Msg_Node_2 := Inner;
7527 Error_Msg_NE
7528 ("circular Instantiation: & instantiated within &!",
7529 N, Scop);
7530 return True;
7531
7532 elsif Node (Elmt) = Inner then
7533 return True;
7534
7535 elsif Contains_Instance_Of (Node (Elmt), Scop, N) then
7536 Error_Msg_Node_2 := Inner;
7537 Error_Msg_NE
7538 ("circular Instantiation: & instantiated within &!",
7539 N, Node (Elmt));
7540 return True;
7541 end if;
7542
7543 Next_Elmt (Elmt);
7544 end loop;
7545
7546 -- Indicate that Inner is being instantiated within Scop
7547
7548 Append_Elmt (Inner, Inner_Instances (Scop));
7549 end if;
7550
7551 if Scop = Standard_Standard then
7552 exit;
7553 else
7554 Scop := Scope (Scop);
7555 end if;
7556 end loop;
7557
7558 return False;
7559 end Contains_Instance_Of;
7560
7561 -----------------------
7562 -- Copy_Generic_Node --
7563 -----------------------
7564
7565 function Copy_Generic_Node
7566 (N : Node_Id;
7567 Parent_Id : Node_Id;
7568 Instantiating : Boolean) return Node_Id
7569 is
7570 Ent : Entity_Id;
7571 New_N : Node_Id;
7572
7573 function Copy_Generic_Descendant (D : Union_Id) return Union_Id;
7574 -- Check the given value of one of the Fields referenced by the current
7575 -- node to determine whether to copy it recursively. The field may hold
7576 -- a Node_Id, a List_Id, or an Elist_Id, or a plain value (Sloc, Uint,
7577 -- Char) in which case it need not be copied.
7578
7579 procedure Copy_Descendants;
7580 -- Common utility for various nodes
7581
7582 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id;
7583 -- Make copy of element list
7584
7585 function Copy_Generic_List
7586 (L : List_Id;
7587 Parent_Id : Node_Id) return List_Id;
7588 -- Apply Copy_Node recursively to the members of a node list
7589
7590 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean;
7591 -- True if an identifier is part of the defining program unit name of
7592 -- a child unit.
7593 -- Consider removing this subprogram now that ASIS no longer uses it.
7594
7595 ----------------------
7596 -- Copy_Descendants --
7597 ----------------------
7598
7599 procedure Copy_Descendants is
7600 use Atree.Unchecked_Access;
7601 -- This code section is part of the implementation of an untyped
7602 -- tree traversal, so it needs direct access to node fields.
7603
7604 begin
7605 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
7606 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
7607 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
7608 Set_Field4 (New_N, Copy_Generic_Descendant (Field4 (N)));
7609 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
7610 end Copy_Descendants;
7611
7612 -----------------------------
7613 -- Copy_Generic_Descendant --
7614 -----------------------------
7615
7616 function Copy_Generic_Descendant (D : Union_Id) return Union_Id is
7617 begin
7618 if D = Union_Id (Empty) then
7619 return D;
7620
7621 elsif D in Node_Range then
7622 return Union_Id
7623 (Copy_Generic_Node (Node_Id (D), New_N, Instantiating));
7624
7625 elsif D in List_Range then
7626 return Union_Id (Copy_Generic_List (List_Id (D), New_N));
7627
7628 elsif D in Elist_Range then
7629 return Union_Id (Copy_Generic_Elist (Elist_Id (D)));
7630
7631 -- Nothing else is copyable (e.g. Uint values), return as is
7632
7633 else
7634 return D;
7635 end if;
7636 end Copy_Generic_Descendant;
7637
7638 ------------------------
7639 -- Copy_Generic_Elist --
7640 ------------------------
7641
7642 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id is
7643 M : Elmt_Id;
7644 L : Elist_Id;
7645
7646 begin
7647 if Present (E) then
7648 L := New_Elmt_List;
7649 M := First_Elmt (E);
7650 while Present (M) loop
7651 Append_Elmt
7652 (Copy_Generic_Node (Node (M), Empty, Instantiating), L);
7653 Next_Elmt (M);
7654 end loop;
7655
7656 return L;
7657
7658 else
7659 return No_Elist;
7660 end if;
7661 end Copy_Generic_Elist;
7662
7663 -----------------------
7664 -- Copy_Generic_List --
7665 -----------------------
7666
7667 function Copy_Generic_List
7668 (L : List_Id;
7669 Parent_Id : Node_Id) return List_Id
7670 is
7671 N : Node_Id;
7672 New_L : List_Id;
7673
7674 begin
7675 if Present (L) then
7676 New_L := New_List;
7677 Set_Parent (New_L, Parent_Id);
7678
7679 N := First (L);
7680 while Present (N) loop
7681 Append (Copy_Generic_Node (N, Empty, Instantiating), New_L);
7682 Next (N);
7683 end loop;
7684
7685 return New_L;
7686
7687 else
7688 return No_List;
7689 end if;
7690 end Copy_Generic_List;
7691
7692 ---------------------------
7693 -- In_Defining_Unit_Name --
7694 ---------------------------
7695
7696 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean is
7697 begin
7698 return
7699 Present (Parent (Nam))
7700 and then (Nkind (Parent (Nam)) = N_Defining_Program_Unit_Name
7701 or else
7702 (Nkind (Parent (Nam)) = N_Expanded_Name
7703 and then In_Defining_Unit_Name (Parent (Nam))));
7704 end In_Defining_Unit_Name;
7705
7706 -- Start of processing for Copy_Generic_Node
7707
7708 begin
7709 if N = Empty then
7710 return N;
7711 end if;
7712
7713 New_N := New_Copy (N);
7714
7715 -- Copy aspects if present
7716
7717 if Has_Aspects (N) then
7718 Set_Has_Aspects (New_N, False);
7719 Set_Aspect_Specifications
7720 (New_N, Copy_Generic_List (Aspect_Specifications (N), Parent_Id));
7721 end if;
7722
7723 -- If we are instantiating, we want to adjust the sloc based on the
7724 -- current S_Adjustment. However, if this is the root node of a subunit,
7725 -- we need to defer that adjustment to below (see "elsif Instantiating
7726 -- and Was_Stub"), so it comes after Create_Instantiation_Source has
7727 -- computed the adjustment.
7728
7729 if Instantiating
7730 and then not (Nkind (N) in N_Proper_Body
7731 and then Was_Originally_Stub (N))
7732 then
7733 Adjust_Instantiation_Sloc (New_N, S_Adjustment);
7734 end if;
7735
7736 if not Is_List_Member (N) then
7737 Set_Parent (New_N, Parent_Id);
7738 end if;
7739
7740 -- Special casing for identifiers and other entity names and operators
7741
7742 if Nkind_In (New_N, N_Character_Literal,
7743 N_Expanded_Name,
7744 N_Identifier,
7745 N_Operator_Symbol)
7746 or else Nkind (New_N) in N_Op
7747 then
7748 if not Instantiating then
7749
7750 -- Link both nodes in order to assign subsequently the entity of
7751 -- the copy to the original node, in case this is a global
7752 -- reference.
7753
7754 Set_Associated_Node (N, New_N);
7755
7756 -- If we are within an instantiation, this is a nested generic
7757 -- that has already been analyzed at the point of definition.
7758 -- We must preserve references that were global to the enclosing
7759 -- parent at that point. Other occurrences, whether global or
7760 -- local to the current generic, must be resolved anew, so we
7761 -- reset the entity in the generic copy. A global reference has a
7762 -- smaller depth than the parent, or else the same depth in case
7763 -- both are distinct compilation units.
7764
7765 -- A child unit is implicitly declared within the enclosing parent
7766 -- but is in fact global to it, and must be preserved.
7767
7768 -- It is also possible for Current_Instantiated_Parent to be
7769 -- defined, and for this not to be a nested generic, namely if
7770 -- the unit is loaded through Rtsfind. In that case, the entity of
7771 -- New_N is only a link to the associated node, and not a defining
7772 -- occurrence.
7773
7774 -- The entities for parent units in the defining_program_unit of a
7775 -- generic child unit are established when the context of the unit
7776 -- is first analyzed, before the generic copy is made. They are
7777 -- preserved in the copy for use in e.g. ASIS queries.
7778
7779 Ent := Entity (New_N);
7780
7781 if No (Current_Instantiated_Parent.Gen_Id) then
7782 if No (Ent)
7783 or else Nkind (Ent) /= N_Defining_Identifier
7784 or else not In_Defining_Unit_Name (N)
7785 then
7786 Set_Associated_Node (New_N, Empty);
7787 end if;
7788
7789 elsif No (Ent)
7790 or else
7791 not Nkind_In (Ent, N_Defining_Identifier,
7792 N_Defining_Character_Literal,
7793 N_Defining_Operator_Symbol)
7794 or else No (Scope (Ent))
7795 or else
7796 (Scope (Ent) = Current_Instantiated_Parent.Gen_Id
7797 and then not Is_Child_Unit (Ent))
7798 or else
7799 (Scope_Depth (Scope (Ent)) >
7800 Scope_Depth (Current_Instantiated_Parent.Gen_Id)
7801 and then
7802 Get_Source_Unit (Ent) =
7803 Get_Source_Unit (Current_Instantiated_Parent.Gen_Id))
7804 then
7805 Set_Associated_Node (New_N, Empty);
7806 end if;
7807
7808 -- Case of instantiating identifier or some other name or operator
7809
7810 else
7811 -- If the associated node is still defined, the entity in it
7812 -- is global, and must be copied to the instance. If this copy
7813 -- is being made for a body to inline, it is applied to an
7814 -- instantiated tree, and the entity is already present and
7815 -- must be also preserved.
7816
7817 declare
7818 Assoc : constant Node_Id := Get_Associated_Node (N);
7819
7820 begin
7821 if Present (Assoc) then
7822 if Nkind (Assoc) = Nkind (N) then
7823 Set_Entity (New_N, Entity (Assoc));
7824 Check_Private_View (N);
7825
7826 -- Here we deal with a very peculiar case for which the
7827 -- Has_Private_View mechanism is not sufficient, because
7828 -- the reference to the type is implicit in the tree,
7829 -- that is to say, it's not referenced from a node but
7830 -- only from another type, namely through Component_Type.
7831
7832 -- package P is
7833
7834 -- type Pt is private;
7835
7836 -- generic
7837 -- type Ft is array (Positive range <>) of Pt;
7838 -- package G is
7839 -- procedure Check (F1, F2 : Ft; Lt : Boolean);
7840 -- end G;
7841
7842 -- private
7843 -- type Pt is new Boolean;
7844 -- end P;
7845
7846 -- package body P is
7847 -- package body G is
7848 -- procedure Check (F1, F2 : Ft; Lt : Boolean) is
7849 -- begin
7850 -- if (F1 < F2) /= Lt then
7851 -- null;
7852 -- end if;
7853 -- end Check;
7854 -- end G;
7855 -- end P;
7856
7857 -- type Arr is array (Positive range <>) of P.Pt;
7858
7859 -- package Inst is new P.G (Arr);
7860
7861 -- Pt is a global type for the generic package G and it
7862 -- is not referenced in its body, but only as component
7863 -- type of Ft, which is a local type. This means that no
7864 -- references to Pt or Ft are seen during the copy of the
7865 -- body, the only reference to Pt being seen is when the
7866 -- actuals are checked by Check_Generic_Actuals, but Pt
7867 -- is still private at this point. In the end, the views
7868 -- of Pt are not switched in the body and, therefore, the
7869 -- array comparison is rejected because the component is
7870 -- still private.
7871
7872 -- Adding e.g. a dummy variable of type Pt in the body is
7873 -- sufficient to make everything work, so we generate an
7874 -- artificial reference to Pt on the fly and thus force
7875 -- the switching of views on the grounds that, if the
7876 -- comparison was accepted during the semantic analysis
7877 -- of the generic, this means that the component cannot
7878 -- have been private (see Sem_Type.Valid_Comparison_Arg).
7879
7880 if Nkind (Assoc) in N_Op_Compare
7881 and then Present (Etype (Left_Opnd (Assoc)))
7882 and then Is_Array_Type (Etype (Left_Opnd (Assoc)))
7883 and then Present (Etype (Right_Opnd (Assoc)))
7884 and then Is_Array_Type (Etype (Right_Opnd (Assoc)))
7885 then
7886 declare
7887 Ltyp : constant Entity_Id :=
7888 Etype (Left_Opnd (Assoc));
7889 Rtyp : constant Entity_Id :=
7890 Etype (Right_Opnd (Assoc));
7891 begin
7892 if Is_Private_Type (Component_Type (Ltyp)) then
7893 Check_Private_View
7894 (New_Occurrence_Of (Component_Type (Ltyp),
7895 Sloc (N)));
7896 end if;
7897 if Is_Private_Type (Component_Type (Rtyp)) then
7898 Check_Private_View
7899 (New_Occurrence_Of (Component_Type (Rtyp),
7900 Sloc (N)));
7901 end if;
7902 end;
7903 end if;
7904
7905 -- The node is a reference to a global type and acts as the
7906 -- subtype mark of a qualified expression created in order
7907 -- to aid resolution of accidental overloading in instances.
7908 -- Since N is a reference to a type, the Associated_Node of
7909 -- N denotes an entity rather than another identifier. See
7910 -- Qualify_Universal_Operands for details.
7911
7912 elsif Nkind (N) = N_Identifier
7913 and then Nkind (Parent (N)) = N_Qualified_Expression
7914 and then Subtype_Mark (Parent (N)) = N
7915 and then Is_Qualified_Universal_Literal (Parent (N))
7916 then
7917 Set_Entity (New_N, Assoc);
7918
7919 -- The name in the call may be a selected component if the
7920 -- call has not been analyzed yet, as may be the case for
7921 -- pre/post conditions in a generic unit.
7922
7923 elsif Nkind (Assoc) = N_Function_Call
7924 and then Is_Entity_Name (Name (Assoc))
7925 then
7926 Set_Entity (New_N, Entity (Name (Assoc)));
7927
7928 elsif Nkind_In (Assoc, N_Defining_Identifier,
7929 N_Defining_Character_Literal,
7930 N_Defining_Operator_Symbol)
7931 and then Expander_Active
7932 then
7933 -- Inlining case: we are copying a tree that contains
7934 -- global entities, which are preserved in the copy to be
7935 -- used for subsequent inlining.
7936
7937 null;
7938
7939 else
7940 Set_Entity (New_N, Empty);
7941 end if;
7942 end if;
7943 end;
7944 end if;
7945
7946 -- For expanded name, we must copy the Prefix and Selector_Name
7947
7948 if Nkind (N) = N_Expanded_Name then
7949 Set_Prefix
7950 (New_N, Copy_Generic_Node (Prefix (N), New_N, Instantiating));
7951
7952 Set_Selector_Name (New_N,
7953 Copy_Generic_Node (Selector_Name (N), New_N, Instantiating));
7954
7955 -- For operators, copy the operands
7956
7957 elsif Nkind (N) in N_Op then
7958 if Nkind (N) in N_Binary_Op then
7959 Set_Left_Opnd (New_N,
7960 Copy_Generic_Node (Left_Opnd (N), New_N, Instantiating));
7961 end if;
7962
7963 Set_Right_Opnd (New_N,
7964 Copy_Generic_Node (Right_Opnd (N), New_N, Instantiating));
7965 end if;
7966
7967 -- Establish a link between an entity from the generic template and the
7968 -- corresponding entity in the generic copy to be analyzed.
7969
7970 elsif Nkind (N) in N_Entity then
7971 if not Instantiating then
7972 Set_Associated_Entity (N, New_N);
7973 end if;
7974
7975 -- Clear any existing link the copy may inherit from the replicated
7976 -- generic template entity.
7977
7978 Set_Associated_Entity (New_N, Empty);
7979
7980 -- Special casing for stubs
7981
7982 elsif Nkind (N) in N_Body_Stub then
7983
7984 -- In any case, we must copy the specification or defining
7985 -- identifier as appropriate.
7986
7987 if Nkind (N) = N_Subprogram_Body_Stub then
7988 Set_Specification (New_N,
7989 Copy_Generic_Node (Specification (N), New_N, Instantiating));
7990
7991 else
7992 Set_Defining_Identifier (New_N,
7993 Copy_Generic_Node
7994 (Defining_Identifier (N), New_N, Instantiating));
7995 end if;
7996
7997 -- If we are not instantiating, then this is where we load and
7998 -- analyze subunits, i.e. at the point where the stub occurs. A
7999 -- more permissive system might defer this analysis to the point
8000 -- of instantiation, but this seems too complicated for now.
8001
8002 if not Instantiating then
8003 declare
8004 Subunit_Name : constant Unit_Name_Type := Get_Unit_Name (N);
8005 Subunit : Node_Id;
8006 Unum : Unit_Number_Type;
8007 New_Body : Node_Id;
8008
8009 begin
8010 -- Make sure that, if it is a subunit of the main unit that is
8011 -- preprocessed and if -gnateG is specified, the preprocessed
8012 -- file will be written.
8013
8014 Lib.Analysing_Subunit_Of_Main :=
8015 Lib.In_Extended_Main_Source_Unit (N);
8016 Unum :=
8017 Load_Unit
8018 (Load_Name => Subunit_Name,
8019 Required => False,
8020 Subunit => True,
8021 Error_Node => N);
8022 Lib.Analysing_Subunit_Of_Main := False;
8023
8024 -- If the proper body is not found, a warning message will be
8025 -- emitted when analyzing the stub, or later at the point of
8026 -- instantiation. Here we just leave the stub as is.
8027
8028 if Unum = No_Unit then
8029 Subunits_Missing := True;
8030 goto Subunit_Not_Found;
8031 end if;
8032
8033 Subunit := Cunit (Unum);
8034
8035 if Nkind (Unit (Subunit)) /= N_Subunit then
8036 Error_Msg_N
8037 ("found child unit instead of expected SEPARATE subunit",
8038 Subunit);
8039 Error_Msg_Sloc := Sloc (N);
8040 Error_Msg_N ("\to complete stub #", Subunit);
8041 goto Subunit_Not_Found;
8042 end if;
8043
8044 -- We must create a generic copy of the subunit, in order to
8045 -- perform semantic analysis on it, and we must replace the
8046 -- stub in the original generic unit with the subunit, in order
8047 -- to preserve non-local references within.
8048
8049 -- Only the proper body needs to be copied. Library_Unit and
8050 -- context clause are simply inherited by the generic copy.
8051 -- Note that the copy (which may be recursive if there are
8052 -- nested subunits) must be done first, before attaching it to
8053 -- the enclosing generic.
8054
8055 New_Body :=
8056 Copy_Generic_Node
8057 (Proper_Body (Unit (Subunit)),
8058 Empty, Instantiating => False);
8059
8060 -- Now place the original proper body in the original generic
8061 -- unit. This is a body, not a compilation unit.
8062
8063 Rewrite (N, Proper_Body (Unit (Subunit)));
8064 Set_Is_Compilation_Unit (Defining_Entity (N), False);
8065 Set_Was_Originally_Stub (N);
8066
8067 -- Finally replace the body of the subunit with its copy, and
8068 -- make this new subunit into the library unit of the generic
8069 -- copy, which does not have stubs any longer.
8070
8071 Set_Proper_Body (Unit (Subunit), New_Body);
8072 Set_Library_Unit (New_N, Subunit);
8073 Inherit_Context (Unit (Subunit), N);
8074 end;
8075
8076 -- If we are instantiating, this must be an error case, since
8077 -- otherwise we would have replaced the stub node by the proper body
8078 -- that corresponds. So just ignore it in the copy (i.e. we have
8079 -- copied it, and that is good enough).
8080
8081 else
8082 null;
8083 end if;
8084
8085 <<Subunit_Not_Found>> null;
8086
8087 -- If the node is a compilation unit, it is the subunit of a stub, which
8088 -- has been loaded already (see code below). In this case, the library
8089 -- unit field of N points to the parent unit (which is a compilation
8090 -- unit) and need not (and cannot) be copied.
8091
8092 -- When the proper body of the stub is analyzed, the library_unit link
8093 -- is used to establish the proper context (see sem_ch10).
8094
8095 -- The other fields of a compilation unit are copied as usual
8096
8097 elsif Nkind (N) = N_Compilation_Unit then
8098
8099 -- This code can only be executed when not instantiating, because in
8100 -- the copy made for an instantiation, the compilation unit node has
8101 -- disappeared at the point that a stub is replaced by its proper
8102 -- body.
8103
8104 pragma Assert (not Instantiating);
8105
8106 Set_Context_Items (New_N,
8107 Copy_Generic_List (Context_Items (N), New_N));
8108
8109 Set_Unit (New_N,
8110 Copy_Generic_Node (Unit (N), New_N, Instantiating => False));
8111
8112 Set_First_Inlined_Subprogram (New_N,
8113 Copy_Generic_Node
8114 (First_Inlined_Subprogram (N), New_N, Instantiating => False));
8115
8116 Set_Aux_Decls_Node
8117 (New_N,
8118 Copy_Generic_Node
8119 (Aux_Decls_Node (N), New_N, Instantiating => False));
8120
8121 -- For an assignment node, the assignment is known to be semantically
8122 -- legal if we are instantiating the template. This avoids incorrect
8123 -- diagnostics in generated code.
8124
8125 elsif Nkind (N) = N_Assignment_Statement then
8126
8127 -- Copy name and expression fields in usual manner
8128
8129 Set_Name (New_N,
8130 Copy_Generic_Node (Name (N), New_N, Instantiating));
8131
8132 Set_Expression (New_N,
8133 Copy_Generic_Node (Expression (N), New_N, Instantiating));
8134
8135 if Instantiating then
8136 Set_Assignment_OK (Name (New_N), True);
8137 end if;
8138
8139 elsif Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
8140 if not Instantiating then
8141 Set_Associated_Node (N, New_N);
8142
8143 else
8144 if Present (Get_Associated_Node (N))
8145 and then Nkind (Get_Associated_Node (N)) = Nkind (N)
8146 then
8147 -- In the generic the aggregate has some composite type. If at
8148 -- the point of instantiation the type has a private view,
8149 -- install the full view (and that of its ancestors, if any).
8150
8151 declare
8152 T : Entity_Id := (Etype (Get_Associated_Node (New_N)));
8153 Rt : Entity_Id;
8154
8155 begin
8156 if Present (T) and then Is_Private_Type (T) then
8157 Switch_View (T);
8158 end if;
8159
8160 if Present (T)
8161 and then Is_Tagged_Type (T)
8162 and then Is_Derived_Type (T)
8163 then
8164 Rt := Root_Type (T);
8165
8166 loop
8167 T := Etype (T);
8168
8169 if Is_Private_Type (T) then
8170 Switch_View (T);
8171 end if;
8172
8173 exit when T = Rt;
8174 end loop;
8175 end if;
8176 end;
8177 end if;
8178 end if;
8179
8180 -- Do not copy the associated node, which points to the generic copy
8181 -- of the aggregate.
8182
8183 declare
8184 use Atree.Unchecked_Access;
8185 -- This code section is part of the implementation of an untyped
8186 -- tree traversal, so it needs direct access to node fields.
8187
8188 begin
8189 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
8190 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
8191 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
8192 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
8193 end;
8194
8195 -- Allocators do not have an identifier denoting the access type, so we
8196 -- must locate it through the expression to check whether the views are
8197 -- consistent.
8198
8199 elsif Nkind (N) = N_Allocator
8200 and then Nkind (Expression (N)) = N_Qualified_Expression
8201 and then Is_Entity_Name (Subtype_Mark (Expression (N)))
8202 and then Instantiating
8203 then
8204 declare
8205 T : constant Node_Id :=
8206 Get_Associated_Node (Subtype_Mark (Expression (N)));
8207 Acc_T : Entity_Id;
8208
8209 begin
8210 if Present (T) then
8211
8212 -- Retrieve the allocator node in the generic copy
8213
8214 Acc_T := Etype (Parent (Parent (T)));
8215
8216 if Present (Acc_T) and then Is_Private_Type (Acc_T) then
8217 Switch_View (Acc_T);
8218 end if;
8219 end if;
8220
8221 Copy_Descendants;
8222 end;
8223
8224 -- For a proper body, we must catch the case of a proper body that
8225 -- replaces a stub. This represents the point at which a separate
8226 -- compilation unit, and hence template file, may be referenced, so we
8227 -- must make a new source instantiation entry for the template of the
8228 -- subunit, and ensure that all nodes in the subunit are adjusted using
8229 -- this new source instantiation entry.
8230
8231 elsif Nkind (N) in N_Proper_Body then
8232 declare
8233 Save_Adjustment : constant Sloc_Adjustment := S_Adjustment;
8234 begin
8235 if Instantiating and then Was_Originally_Stub (N) then
8236 Create_Instantiation_Source
8237 (Instantiation_Node,
8238 Defining_Entity (N),
8239 S_Adjustment);
8240
8241 Adjust_Instantiation_Sloc (New_N, S_Adjustment);
8242 end if;
8243
8244 -- Now copy the fields of the proper body, using the new
8245 -- adjustment factor if one was needed as per test above.
8246
8247 Copy_Descendants;
8248
8249 -- Restore the original adjustment factor
8250
8251 S_Adjustment := Save_Adjustment;
8252 end;
8253
8254 elsif Nkind (N) = N_Pragma and then Instantiating then
8255
8256 -- Do not copy Comment or Ident pragmas their content is relevant to
8257 -- the generic unit, not to the instantiating unit.
8258
8259 if Nam_In (Pragma_Name_Unmapped (N), Name_Comment, Name_Ident) then
8260 New_N := Make_Null_Statement (Sloc (N));
8261
8262 -- Do not copy pragmas generated from aspects because the pragmas do
8263 -- not carry any semantic information, plus they will be regenerated
8264 -- in the instance.
8265
8266 -- However, generating C we need to copy them since postconditions
8267 -- are inlined by the front end, and the front-end inlining machinery
8268 -- relies on this routine to perform inlining.
8269
8270 elsif From_Aspect_Specification (N)
8271 and then not Modify_Tree_For_C
8272 then
8273 New_N := Make_Null_Statement (Sloc (N));
8274
8275 else
8276 Copy_Descendants;
8277 end if;
8278
8279 elsif Nkind_In (N, N_Integer_Literal, N_Real_Literal) then
8280
8281 -- No descendant fields need traversing
8282
8283 null;
8284
8285 elsif Nkind (N) = N_String_Literal
8286 and then Present (Etype (N))
8287 and then Instantiating
8288 then
8289 -- If the string is declared in an outer scope, the string_literal
8290 -- subtype created for it may have the wrong scope. Force reanalysis
8291 -- of the constant to generate a new itype in the proper context.
8292
8293 Set_Etype (New_N, Empty);
8294 Set_Analyzed (New_N, False);
8295
8296 -- For the remaining nodes, copy their descendants recursively
8297
8298 else
8299 Copy_Descendants;
8300
8301 if Instantiating and then Nkind (N) = N_Subprogram_Body then
8302 Set_Generic_Parent (Specification (New_N), N);
8303
8304 -- Should preserve Corresponding_Spec??? (12.3(14))
8305 end if;
8306 end if;
8307
8308 -- Propagate dimensions if present, so that they are reflected in the
8309 -- instance.
8310
8311 if Nkind (N) in N_Has_Etype
8312 and then (Nkind (N) in N_Op or else Is_Entity_Name (N))
8313 and then Present (Etype (N))
8314 and then Is_Floating_Point_Type (Etype (N))
8315 and then Has_Dimension_System (Etype (N))
8316 then
8317 Copy_Dimensions (N, New_N);
8318 end if;
8319
8320 return New_N;
8321 end Copy_Generic_Node;
8322
8323 ----------------------------
8324 -- Denotes_Formal_Package --
8325 ----------------------------
8326
8327 function Denotes_Formal_Package
8328 (Pack : Entity_Id;
8329 On_Exit : Boolean := False;
8330 Instance : Entity_Id := Empty) return Boolean
8331 is
8332 Par : Entity_Id;
8333 Scop : constant Entity_Id := Scope (Pack);
8334 E : Entity_Id;
8335
8336 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean;
8337 -- The package in question may be an actual for a previous formal
8338 -- package P of the current instance, so examine its actuals as well.
8339 -- This must be recursive over other formal packages.
8340
8341 ----------------------------------
8342 -- Is_Actual_Of_Previous_Formal --
8343 ----------------------------------
8344
8345 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean is
8346 E1 : Entity_Id;
8347
8348 begin
8349 E1 := First_Entity (P);
8350 while Present (E1) and then E1 /= Instance loop
8351 if Ekind (E1) = E_Package
8352 and then Nkind (Parent (E1)) = N_Package_Renaming_Declaration
8353 then
8354 if Renamed_Object (E1) = Pack then
8355 return True;
8356
8357 elsif E1 = P or else Renamed_Object (E1) = P then
8358 return False;
8359
8360 elsif Is_Actual_Of_Previous_Formal (E1) then
8361 return True;
8362 end if;
8363 end if;
8364
8365 Next_Entity (E1);
8366 end loop;
8367
8368 return False;
8369 end Is_Actual_Of_Previous_Formal;
8370
8371 -- Start of processing for Denotes_Formal_Package
8372
8373 begin
8374 if On_Exit then
8375 Par :=
8376 Instance_Envs.Table
8377 (Instance_Envs.Last).Instantiated_Parent.Act_Id;
8378 else
8379 Par := Current_Instantiated_Parent.Act_Id;
8380 end if;
8381
8382 if Ekind (Scop) = E_Generic_Package
8383 or else Nkind (Unit_Declaration_Node (Scop)) =
8384 N_Generic_Subprogram_Declaration
8385 then
8386 return True;
8387
8388 elsif Nkind (Original_Node (Unit_Declaration_Node (Pack))) =
8389 N_Formal_Package_Declaration
8390 then
8391 return True;
8392
8393 elsif No (Par) then
8394 return False;
8395
8396 else
8397 -- Check whether this package is associated with a formal package of
8398 -- the enclosing instantiation. Iterate over the list of renamings.
8399
8400 E := First_Entity (Par);
8401 while Present (E) loop
8402 if Ekind (E) /= E_Package
8403 or else Nkind (Parent (E)) /= N_Package_Renaming_Declaration
8404 then
8405 null;
8406
8407 elsif Renamed_Object (E) = Par then
8408 return False;
8409
8410 elsif Renamed_Object (E) = Pack then
8411 return True;
8412
8413 elsif Is_Actual_Of_Previous_Formal (E) then
8414 return True;
8415
8416 end if;
8417
8418 Next_Entity (E);
8419 end loop;
8420
8421 return False;
8422 end if;
8423 end Denotes_Formal_Package;
8424
8425 -----------------
8426 -- End_Generic --
8427 -----------------
8428
8429 procedure End_Generic is
8430 begin
8431 -- ??? More things could be factored out in this routine. Should
8432 -- probably be done at a later stage.
8433
8434 Inside_A_Generic := Generic_Flags.Table (Generic_Flags.Last);
8435 Generic_Flags.Decrement_Last;
8436
8437 Expander_Mode_Restore;
8438 end End_Generic;
8439
8440 -------------
8441 -- Earlier --
8442 -------------
8443
8444 function Earlier (N1, N2 : Node_Id) return Boolean is
8445 procedure Find_Depth (P : in out Node_Id; D : in out Integer);
8446 -- Find distance from given node to enclosing compilation unit
8447
8448 ----------------
8449 -- Find_Depth --
8450 ----------------
8451
8452 procedure Find_Depth (P : in out Node_Id; D : in out Integer) is
8453 begin
8454 while Present (P)
8455 and then Nkind (P) /= N_Compilation_Unit
8456 loop
8457 P := True_Parent (P);
8458 D := D + 1;
8459 end loop;
8460 end Find_Depth;
8461
8462 -- Local declarations
8463
8464 D1 : Integer := 0;
8465 D2 : Integer := 0;
8466 P1 : Node_Id := N1;
8467 P2 : Node_Id := N2;
8468 T1 : Source_Ptr;
8469 T2 : Source_Ptr;
8470
8471 -- Start of processing for Earlier
8472
8473 begin
8474 Find_Depth (P1, D1);
8475 Find_Depth (P2, D2);
8476
8477 if P1 /= P2 then
8478 return False;
8479 else
8480 P1 := N1;
8481 P2 := N2;
8482 end if;
8483
8484 while D1 > D2 loop
8485 P1 := True_Parent (P1);
8486 D1 := D1 - 1;
8487 end loop;
8488
8489 while D2 > D1 loop
8490 P2 := True_Parent (P2);
8491 D2 := D2 - 1;
8492 end loop;
8493
8494 -- At this point P1 and P2 are at the same distance from the root.
8495 -- We examine their parents until we find a common declarative list.
8496 -- If we reach the root, N1 and N2 do not descend from the same
8497 -- declarative list (e.g. one is nested in the declarative part and
8498 -- the other is in a block in the statement part) and the earlier
8499 -- one is already frozen.
8500
8501 while not Is_List_Member (P1)
8502 or else not Is_List_Member (P2)
8503 or else List_Containing (P1) /= List_Containing (P2)
8504 loop
8505 P1 := True_Parent (P1);
8506 P2 := True_Parent (P2);
8507
8508 if Nkind (Parent (P1)) = N_Subunit then
8509 P1 := Corresponding_Stub (Parent (P1));
8510 end if;
8511
8512 if Nkind (Parent (P2)) = N_Subunit then
8513 P2 := Corresponding_Stub (Parent (P2));
8514 end if;
8515
8516 if P1 = P2 then
8517 return False;
8518 end if;
8519 end loop;
8520
8521 -- Expanded code usually shares the source location of the original
8522 -- construct it was generated for. This however may not necessarily
8523 -- reflect the true location of the code within the tree.
8524
8525 -- Before comparing the slocs of the two nodes, make sure that we are
8526 -- working with correct source locations. Assume that P1 is to the left
8527 -- of P2. If either one does not come from source, traverse the common
8528 -- list heading towards the other node and locate the first source
8529 -- statement.
8530
8531 -- P1 P2
8532 -- ----+===+===+--------------+===+===+----
8533 -- expanded code expanded code
8534
8535 if not Comes_From_Source (P1) then
8536 while Present (P1) loop
8537
8538 -- Neither P2 nor a source statement were located during the
8539 -- search. If we reach the end of the list, then P1 does not
8540 -- occur earlier than P2.
8541
8542 -- ---->
8543 -- start --- P2 ----- P1 --- end
8544
8545 if No (Next (P1)) then
8546 return False;
8547
8548 -- We encounter P2 while going to the right of the list. This
8549 -- means that P1 does indeed appear earlier.
8550
8551 -- ---->
8552 -- start --- P1 ===== P2 --- end
8553 -- expanded code in between
8554
8555 elsif P1 = P2 then
8556 return True;
8557
8558 -- No need to look any further since we have located a source
8559 -- statement.
8560
8561 elsif Comes_From_Source (P1) then
8562 exit;
8563 end if;
8564
8565 -- Keep going right
8566
8567 Next (P1);
8568 end loop;
8569 end if;
8570
8571 if not Comes_From_Source (P2) then
8572 while Present (P2) loop
8573
8574 -- Neither P1 nor a source statement were located during the
8575 -- search. If we reach the start of the list, then P1 does not
8576 -- occur earlier than P2.
8577
8578 -- <----
8579 -- start --- P2 --- P1 --- end
8580
8581 if No (Prev (P2)) then
8582 return False;
8583
8584 -- We encounter P1 while going to the left of the list. This
8585 -- means that P1 does indeed appear earlier.
8586
8587 -- <----
8588 -- start --- P1 ===== P2 --- end
8589 -- expanded code in between
8590
8591 elsif P2 = P1 then
8592 return True;
8593
8594 -- No need to look any further since we have located a source
8595 -- statement.
8596
8597 elsif Comes_From_Source (P2) then
8598 exit;
8599 end if;
8600
8601 -- Keep going left
8602
8603 Prev (P2);
8604 end loop;
8605 end if;
8606
8607 -- At this point either both nodes came from source or we approximated
8608 -- their source locations through neighboring source statements.
8609
8610 T1 := Top_Level_Location (Sloc (P1));
8611 T2 := Top_Level_Location (Sloc (P2));
8612
8613 -- When two nodes come from the same instance, they have identical top
8614 -- level locations. To determine proper relation within the tree, check
8615 -- their locations within the template.
8616
8617 if T1 = T2 then
8618 return Sloc (P1) < Sloc (P2);
8619
8620 -- The two nodes either come from unrelated instances or do not come
8621 -- from instantiated code at all.
8622
8623 else
8624 return T1 < T2;
8625 end if;
8626 end Earlier;
8627
8628 ----------------------
8629 -- Find_Actual_Type --
8630 ----------------------
8631
8632 function Find_Actual_Type
8633 (Typ : Entity_Id;
8634 Gen_Type : Entity_Id) return Entity_Id
8635 is
8636 Gen_Scope : constant Entity_Id := Scope (Gen_Type);
8637 T : Entity_Id;
8638
8639 begin
8640 -- Special processing only applies to child units
8641
8642 if not Is_Child_Unit (Gen_Scope) then
8643 return Get_Instance_Of (Typ);
8644
8645 -- If designated or component type is itself a formal of the child unit,
8646 -- its instance is available.
8647
8648 elsif Scope (Typ) = Gen_Scope then
8649 return Get_Instance_Of (Typ);
8650
8651 -- If the array or access type is not declared in the parent unit,
8652 -- no special processing needed.
8653
8654 elsif not Is_Generic_Type (Typ)
8655 and then Scope (Gen_Scope) /= Scope (Typ)
8656 then
8657 return Get_Instance_Of (Typ);
8658
8659 -- Otherwise, retrieve designated or component type by visibility
8660
8661 else
8662 T := Current_Entity (Typ);
8663 while Present (T) loop
8664 if In_Open_Scopes (Scope (T)) then
8665 return T;
8666 elsif Is_Generic_Actual_Type (T) then
8667 return T;
8668 end if;
8669
8670 T := Homonym (T);
8671 end loop;
8672
8673 return Typ;
8674 end if;
8675 end Find_Actual_Type;
8676
8677 ----------------------------
8678 -- Freeze_Subprogram_Body --
8679 ----------------------------
8680
8681 procedure Freeze_Subprogram_Body
8682 (Inst_Node : Node_Id;
8683 Gen_Body : Node_Id;
8684 Pack_Id : Entity_Id)
8685 is
8686 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
8687 Par : constant Entity_Id := Scope (Gen_Unit);
8688 E_G_Id : Entity_Id;
8689 Enc_G : Entity_Id;
8690 Enc_I : Node_Id;
8691 F_Node : Node_Id;
8692
8693 function Enclosing_Package_Body (N : Node_Id) return Node_Id;
8694 -- Find innermost package body that encloses the given node, and which
8695 -- is not a compilation unit. Freeze nodes for the instance, or for its
8696 -- enclosing body, may be inserted after the enclosing_body of the
8697 -- generic unit. Used to determine proper placement of freeze node for
8698 -- both package and subprogram instances.
8699
8700 function Package_Freeze_Node (B : Node_Id) return Node_Id;
8701 -- Find entity for given package body, and locate or create a freeze
8702 -- node for it.
8703
8704 ----------------------------
8705 -- Enclosing_Package_Body --
8706 ----------------------------
8707
8708 function Enclosing_Package_Body (N : Node_Id) return Node_Id is
8709 P : Node_Id;
8710
8711 begin
8712 P := Parent (N);
8713 while Present (P)
8714 and then Nkind (Parent (P)) /= N_Compilation_Unit
8715 loop
8716 if Nkind (P) = N_Package_Body then
8717 if Nkind (Parent (P)) = N_Subunit then
8718 return Corresponding_Stub (Parent (P));
8719 else
8720 return P;
8721 end if;
8722 end if;
8723
8724 P := True_Parent (P);
8725 end loop;
8726
8727 return Empty;
8728 end Enclosing_Package_Body;
8729
8730 -------------------------
8731 -- Package_Freeze_Node --
8732 -------------------------
8733
8734 function Package_Freeze_Node (B : Node_Id) return Node_Id is
8735 Id : Entity_Id;
8736
8737 begin
8738 if Nkind (B) = N_Package_Body then
8739 Id := Corresponding_Spec (B);
8740 else pragma Assert (Nkind (B) = N_Package_Body_Stub);
8741 Id := Corresponding_Spec (Proper_Body (Unit (Library_Unit (B))));
8742 end if;
8743
8744 Ensure_Freeze_Node (Id);
8745 return Freeze_Node (Id);
8746 end Package_Freeze_Node;
8747
8748 -- Start of processing for Freeze_Subprogram_Body
8749
8750 begin
8751 -- If the instance and the generic body appear within the same unit, and
8752 -- the instance precedes the generic, the freeze node for the instance
8753 -- must appear after that of the generic. If the generic is nested
8754 -- within another instance I2, then current instance must be frozen
8755 -- after I2. In both cases, the freeze nodes are those of enclosing
8756 -- packages. Otherwise, the freeze node is placed at the end of the
8757 -- current declarative part.
8758
8759 Enc_G := Enclosing_Package_Body (Gen_Body);
8760 Enc_I := Enclosing_Package_Body (Inst_Node);
8761 Ensure_Freeze_Node (Pack_Id);
8762 F_Node := Freeze_Node (Pack_Id);
8763
8764 if Is_Generic_Instance (Par)
8765 and then Present (Freeze_Node (Par))
8766 and then In_Same_Declarative_Part
8767 (Parent (Freeze_Node (Par)), Inst_Node)
8768 then
8769 -- The parent was a premature instantiation. Insert freeze node at
8770 -- the end the current declarative part.
8771
8772 if Is_Known_Guaranteed_ABE (Get_Unit_Instantiation_Node (Par)) then
8773 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8774
8775 -- Handle the following case:
8776 --
8777 -- package Parent_Inst is new ...
8778 -- Parent_Inst []
8779 --
8780 -- procedure P ... -- this body freezes Parent_Inst
8781 --
8782 -- package Inst is new ...
8783 --
8784 -- In this particular scenario, the freeze node for Inst must be
8785 -- inserted in the same manner as that of Parent_Inst - before the
8786 -- next source body or at the end of the declarative list (body not
8787 -- available). If body P did not exist and Parent_Inst was frozen
8788 -- after Inst, either by a body following Inst or at the end of the
8789 -- declarative region, the freeze node for Inst must be inserted
8790 -- after that of Parent_Inst. This relation is established by
8791 -- comparing the Slocs of Parent_Inst freeze node and Inst.
8792
8793 elsif List_Containing (Get_Unit_Instantiation_Node (Par)) =
8794 List_Containing (Inst_Node)
8795 and then Sloc (Freeze_Node (Par)) < Sloc (Inst_Node)
8796 then
8797 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8798
8799 else
8800 Insert_After (Freeze_Node (Par), F_Node);
8801 end if;
8802
8803 -- The body enclosing the instance should be frozen after the body that
8804 -- includes the generic, because the body of the instance may make
8805 -- references to entities therein. If the two are not in the same
8806 -- declarative part, or if the one enclosing the instance is frozen
8807 -- already, freeze the instance at the end of the current declarative
8808 -- part.
8809
8810 elsif Is_Generic_Instance (Par)
8811 and then Present (Freeze_Node (Par))
8812 and then Present (Enc_I)
8813 then
8814 if In_Same_Declarative_Part (Parent (Freeze_Node (Par)), Enc_I)
8815 or else
8816 (Nkind (Enc_I) = N_Package_Body
8817 and then In_Same_Declarative_Part
8818 (Parent (Freeze_Node (Par)), Parent (Enc_I)))
8819 then
8820 -- The enclosing package may contain several instances. Rather
8821 -- than computing the earliest point at which to insert its freeze
8822 -- node, we place it at the end of the declarative part of the
8823 -- parent of the generic.
8824
8825 Insert_Freeze_Node_For_Instance
8826 (Freeze_Node (Par), Package_Freeze_Node (Enc_I));
8827 end if;
8828
8829 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8830
8831 elsif Present (Enc_G)
8832 and then Present (Enc_I)
8833 and then Enc_G /= Enc_I
8834 and then Earlier (Inst_Node, Gen_Body)
8835 then
8836 if Nkind (Enc_G) = N_Package_Body then
8837 E_G_Id :=
8838 Corresponding_Spec (Enc_G);
8839 else pragma Assert (Nkind (Enc_G) = N_Package_Body_Stub);
8840 E_G_Id :=
8841 Corresponding_Spec (Proper_Body (Unit (Library_Unit (Enc_G))));
8842 end if;
8843
8844 -- Freeze package that encloses instance, and place node after the
8845 -- package that encloses generic. If enclosing package is already
8846 -- frozen we have to assume it is at the proper place. This may be a
8847 -- potential ABE that requires dynamic checking. Do not add a freeze
8848 -- node if the package that encloses the generic is inside the body
8849 -- that encloses the instance, because the freeze node would be in
8850 -- the wrong scope. Additional contortions needed if the bodies are
8851 -- within a subunit.
8852
8853 declare
8854 Enclosing_Body : Node_Id;
8855
8856 begin
8857 if Nkind (Enc_I) = N_Package_Body_Stub then
8858 Enclosing_Body := Proper_Body (Unit (Library_Unit (Enc_I)));
8859 else
8860 Enclosing_Body := Enc_I;
8861 end if;
8862
8863 if Parent (List_Containing (Enc_G)) /= Enclosing_Body then
8864 Insert_Freeze_Node_For_Instance
8865 (Enc_G, Package_Freeze_Node (Enc_I));
8866 end if;
8867 end;
8868
8869 -- Freeze enclosing subunit before instance
8870
8871 Ensure_Freeze_Node (E_G_Id);
8872
8873 if not Is_List_Member (Freeze_Node (E_G_Id)) then
8874 Insert_After (Enc_G, Freeze_Node (E_G_Id));
8875 end if;
8876
8877 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8878
8879 else
8880 -- If none of the above, insert freeze node at the end of the current
8881 -- declarative part.
8882
8883 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8884 end if;
8885 end Freeze_Subprogram_Body;
8886
8887 ----------------
8888 -- Get_Gen_Id --
8889 ----------------
8890
8891 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id is
8892 begin
8893 return Generic_Renamings.Table (E).Gen_Id;
8894 end Get_Gen_Id;
8895
8896 ---------------------
8897 -- Get_Instance_Of --
8898 ---------------------
8899
8900 function Get_Instance_Of (A : Entity_Id) return Entity_Id is
8901 Res : constant Assoc_Ptr := Generic_Renamings_HTable.Get (A);
8902
8903 begin
8904 if Res /= Assoc_Null then
8905 return Generic_Renamings.Table (Res).Act_Id;
8906
8907 else
8908 -- On exit, entity is not instantiated: not a generic parameter, or
8909 -- else parameter of an inner generic unit.
8910
8911 return A;
8912 end if;
8913 end Get_Instance_Of;
8914
8915 ---------------------------------
8916 -- Get_Unit_Instantiation_Node --
8917 ---------------------------------
8918
8919 function Get_Unit_Instantiation_Node (A : Entity_Id) return Node_Id is
8920 Decl : Node_Id := Unit_Declaration_Node (A);
8921 Inst : Node_Id;
8922
8923 begin
8924 -- If the Package_Instantiation attribute has been set on the package
8925 -- entity, then use it directly when it (or its Original_Node) refers
8926 -- to an N_Package_Instantiation node. In principle it should be
8927 -- possible to have this field set in all cases, which should be
8928 -- investigated, and would allow this function to be significantly
8929 -- simplified. ???
8930
8931 Inst := Package_Instantiation (A);
8932
8933 if Present (Inst) then
8934 if Nkind (Inst) = N_Package_Instantiation then
8935 return Inst;
8936
8937 elsif Nkind (Original_Node (Inst)) = N_Package_Instantiation then
8938 return Original_Node (Inst);
8939 end if;
8940 end if;
8941
8942 -- If the instantiation is a compilation unit that does not need body
8943 -- then the instantiation node has been rewritten as a package
8944 -- declaration for the instance, and we return the original node.
8945
8946 -- If it is a compilation unit and the instance node has not been
8947 -- rewritten, then it is still the unit of the compilation. Finally, if
8948 -- a body is present, this is a parent of the main unit whose body has
8949 -- been compiled for inlining purposes, and the instantiation node has
8950 -- been rewritten with the instance body.
8951
8952 -- Otherwise the instantiation node appears after the declaration. If
8953 -- the entity is a formal package, the declaration may have been
8954 -- rewritten as a generic declaration (in the case of a formal with box)
8955 -- or left as a formal package declaration if it has actuals, and is
8956 -- found with a forward search.
8957
8958 if Nkind (Parent (Decl)) = N_Compilation_Unit then
8959 if Nkind (Decl) = N_Package_Declaration
8960 and then Present (Corresponding_Body (Decl))
8961 then
8962 Decl := Unit_Declaration_Node (Corresponding_Body (Decl));
8963 end if;
8964
8965 if Nkind (Original_Node (Decl)) in N_Generic_Instantiation then
8966 return Original_Node (Decl);
8967 else
8968 return Unit (Parent (Decl));
8969 end if;
8970
8971 elsif Nkind (Decl) = N_Package_Declaration
8972 and then Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration
8973 then
8974 return Original_Node (Decl);
8975
8976 else
8977 Inst := Next (Decl);
8978 while not Nkind_In (Inst, N_Formal_Package_Declaration,
8979 N_Function_Instantiation,
8980 N_Package_Instantiation,
8981 N_Procedure_Instantiation)
8982 loop
8983 Next (Inst);
8984 end loop;
8985
8986 return Inst;
8987 end if;
8988 end Get_Unit_Instantiation_Node;
8989
8990 ------------------------
8991 -- Has_Been_Exchanged --
8992 ------------------------
8993
8994 function Has_Been_Exchanged (E : Entity_Id) return Boolean is
8995 Next : Elmt_Id;
8996
8997 begin
8998 Next := First_Elmt (Exchanged_Views);
8999 while Present (Next) loop
9000 if Full_View (Node (Next)) = E then
9001 return True;
9002 end if;
9003
9004 Next_Elmt (Next);
9005 end loop;
9006
9007 return False;
9008 end Has_Been_Exchanged;
9009
9010 ----------
9011 -- Hash --
9012 ----------
9013
9014 function Hash (F : Entity_Id) return HTable_Range is
9015 begin
9016 return HTable_Range (F mod HTable_Size);
9017 end Hash;
9018
9019 ------------------------
9020 -- Hide_Current_Scope --
9021 ------------------------
9022
9023 procedure Hide_Current_Scope is
9024 C : constant Entity_Id := Current_Scope;
9025 E : Entity_Id;
9026
9027 begin
9028 Set_Is_Hidden_Open_Scope (C);
9029
9030 E := First_Entity (C);
9031 while Present (E) loop
9032 if Is_Immediately_Visible (E) then
9033 Set_Is_Immediately_Visible (E, False);
9034 Append_Elmt (E, Hidden_Entities);
9035 end if;
9036
9037 Next_Entity (E);
9038 end loop;
9039
9040 -- Make the scope name invisible as well. This is necessary, but might
9041 -- conflict with calls to Rtsfind later on, in case the scope is a
9042 -- predefined one. There is no clean solution to this problem, so for
9043 -- now we depend on the user not redefining Standard itself in one of
9044 -- the parent units.
9045
9046 if Is_Immediately_Visible (C) and then C /= Standard_Standard then
9047 Set_Is_Immediately_Visible (C, False);
9048 Append_Elmt (C, Hidden_Entities);
9049 end if;
9050
9051 end Hide_Current_Scope;
9052
9053 --------------
9054 -- Init_Env --
9055 --------------
9056
9057 procedure Init_Env is
9058 Saved : Instance_Env;
9059
9060 begin
9061 Saved.Instantiated_Parent := Current_Instantiated_Parent;
9062 Saved.Exchanged_Views := Exchanged_Views;
9063 Saved.Hidden_Entities := Hidden_Entities;
9064 Saved.Current_Sem_Unit := Current_Sem_Unit;
9065 Saved.Parent_Unit_Visible := Parent_Unit_Visible;
9066 Saved.Instance_Parent_Unit := Instance_Parent_Unit;
9067
9068 -- Save configuration switches. These may be reset if the unit is a
9069 -- predefined unit, and the current mode is not Ada 2005.
9070
9071 Saved.Switches := Save_Config_Switches;
9072
9073 Instance_Envs.Append (Saved);
9074
9075 Exchanged_Views := New_Elmt_List;
9076 Hidden_Entities := New_Elmt_List;
9077
9078 -- Make dummy entry for Instantiated parent. If generic unit is legal,
9079 -- this is set properly in Set_Instance_Env.
9080
9081 Current_Instantiated_Parent :=
9082 (Current_Scope, Current_Scope, Assoc_Null);
9083 end Init_Env;
9084
9085 ---------------------
9086 -- In_Main_Context --
9087 ---------------------
9088
9089 function In_Main_Context (E : Entity_Id) return Boolean is
9090 Context : List_Id;
9091 Clause : Node_Id;
9092 Nam : Node_Id;
9093
9094 begin
9095 if not Is_Compilation_Unit (E)
9096 or else Ekind (E) /= E_Package
9097 or else In_Private_Part (E)
9098 then
9099 return False;
9100 end if;
9101
9102 Context := Context_Items (Cunit (Main_Unit));
9103
9104 Clause := First (Context);
9105 while Present (Clause) loop
9106 if Nkind (Clause) = N_With_Clause then
9107 Nam := Name (Clause);
9108
9109 -- If the current scope is part of the context of the main unit,
9110 -- analysis of the corresponding with_clause is not complete, and
9111 -- the entity is not set. We use the Chars field directly, which
9112 -- might produce false positives in rare cases, but guarantees
9113 -- that we produce all the instance bodies we will need.
9114
9115 if (Is_Entity_Name (Nam) and then Chars (Nam) = Chars (E))
9116 or else (Nkind (Nam) = N_Selected_Component
9117 and then Chars (Selector_Name (Nam)) = Chars (E))
9118 then
9119 return True;
9120 end if;
9121 end if;
9122
9123 Next (Clause);
9124 end loop;
9125
9126 return False;
9127 end In_Main_Context;
9128
9129 ---------------------
9130 -- Inherit_Context --
9131 ---------------------
9132
9133 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id) is
9134 Current_Context : List_Id;
9135 Current_Unit : Node_Id;
9136 Item : Node_Id;
9137 New_I : Node_Id;
9138
9139 Clause : Node_Id;
9140 OK : Boolean;
9141 Lib_Unit : Node_Id;
9142
9143 begin
9144 if Nkind (Parent (Gen_Decl)) = N_Compilation_Unit then
9145
9146 -- The inherited context is attached to the enclosing compilation
9147 -- unit. This is either the main unit, or the declaration for the
9148 -- main unit (in case the instantiation appears within the package
9149 -- declaration and the main unit is its body).
9150
9151 Current_Unit := Parent (Inst);
9152 while Present (Current_Unit)
9153 and then Nkind (Current_Unit) /= N_Compilation_Unit
9154 loop
9155 Current_Unit := Parent (Current_Unit);
9156 end loop;
9157
9158 Current_Context := Context_Items (Current_Unit);
9159
9160 Item := First (Context_Items (Parent (Gen_Decl)));
9161 while Present (Item) loop
9162 if Nkind (Item) = N_With_Clause then
9163 Lib_Unit := Library_Unit (Item);
9164
9165 -- Take care to prevent direct cyclic with's
9166
9167 if Lib_Unit /= Current_Unit then
9168
9169 -- Do not add a unit if it is already in the context
9170
9171 Clause := First (Current_Context);
9172 OK := True;
9173 while Present (Clause) loop
9174 if Nkind (Clause) = N_With_Clause
9175 and then Library_Unit (Clause) = Lib_Unit
9176 then
9177 OK := False;
9178 exit;
9179 end if;
9180
9181 Next (Clause);
9182 end loop;
9183
9184 if OK then
9185 New_I := New_Copy (Item);
9186 Set_Implicit_With (New_I);
9187
9188 Append (New_I, Current_Context);
9189 end if;
9190 end if;
9191 end if;
9192
9193 Next (Item);
9194 end loop;
9195 end if;
9196 end Inherit_Context;
9197
9198 ----------------
9199 -- Initialize --
9200 ----------------
9201
9202 procedure Initialize is
9203 begin
9204 Generic_Renamings.Init;
9205 Instance_Envs.Init;
9206 Generic_Flags.Init;
9207 Generic_Renamings_HTable.Reset;
9208 Circularity_Detected := False;
9209 Exchanged_Views := No_Elist;
9210 Hidden_Entities := No_Elist;
9211 end Initialize;
9212
9213 -------------------------------------
9214 -- Insert_Freeze_Node_For_Instance --
9215 -------------------------------------
9216
9217 procedure Insert_Freeze_Node_For_Instance
9218 (N : Node_Id;
9219 F_Node : Node_Id)
9220 is
9221 Decl : Node_Id;
9222 Decls : List_Id;
9223 Inst : Entity_Id;
9224 Par_N : Node_Id;
9225
9226 function Enclosing_Body (N : Node_Id) return Node_Id;
9227 -- Find enclosing package or subprogram body, if any. Freeze node may
9228 -- be placed at end of current declarative list if previous instance
9229 -- and current one have different enclosing bodies.
9230
9231 function Previous_Instance (Gen : Entity_Id) return Entity_Id;
9232 -- Find the local instance, if any, that declares the generic that is
9233 -- being instantiated. If present, the freeze node for this instance
9234 -- must follow the freeze node for the previous instance.
9235
9236 --------------------
9237 -- Enclosing_Body --
9238 --------------------
9239
9240 function Enclosing_Body (N : Node_Id) return Node_Id is
9241 P : Node_Id;
9242
9243 begin
9244 P := Parent (N);
9245 while Present (P)
9246 and then Nkind (Parent (P)) /= N_Compilation_Unit
9247 loop
9248 if Nkind_In (P, N_Package_Body, N_Subprogram_Body) then
9249 if Nkind (Parent (P)) = N_Subunit then
9250 return Corresponding_Stub (Parent (P));
9251 else
9252 return P;
9253 end if;
9254 end if;
9255
9256 P := True_Parent (P);
9257 end loop;
9258
9259 return Empty;
9260 end Enclosing_Body;
9261
9262 -----------------------
9263 -- Previous_Instance --
9264 -----------------------
9265
9266 function Previous_Instance (Gen : Entity_Id) return Entity_Id is
9267 S : Entity_Id;
9268
9269 begin
9270 S := Scope (Gen);
9271 while Present (S) and then S /= Standard_Standard loop
9272 if Is_Generic_Instance (S)
9273 and then In_Same_Source_Unit (S, N)
9274 then
9275 return S;
9276 end if;
9277
9278 S := Scope (S);
9279 end loop;
9280
9281 return Empty;
9282 end Previous_Instance;
9283
9284 -- Start of processing for Insert_Freeze_Node_For_Instance
9285
9286 begin
9287 if not Is_List_Member (F_Node) then
9288 Decl := N;
9289 Decls := List_Containing (N);
9290 Inst := Entity (F_Node);
9291 Par_N := Parent (Decls);
9292
9293 -- When processing a subprogram instantiation, utilize the actual
9294 -- subprogram instantiation rather than its package wrapper as it
9295 -- carries all the context information.
9296
9297 if Is_Wrapper_Package (Inst) then
9298 Inst := Related_Instance (Inst);
9299 end if;
9300
9301 -- If this is a package instance, check whether the generic is
9302 -- declared in a previous instance and the current instance is
9303 -- not within the previous one.
9304
9305 if Present (Generic_Parent (Parent (Inst)))
9306 and then Is_In_Main_Unit (N)
9307 then
9308 declare
9309 Enclosing_N : constant Node_Id := Enclosing_Body (N);
9310 Par_I : constant Entity_Id :=
9311 Previous_Instance
9312 (Generic_Parent (Parent (Inst)));
9313 Scop : Entity_Id;
9314
9315 begin
9316 if Present (Par_I)
9317 and then Earlier (N, Freeze_Node (Par_I))
9318 then
9319 Scop := Scope (Inst);
9320
9321 -- If the current instance is within the one that contains
9322 -- the generic, the freeze node for the current one must
9323 -- appear in the current declarative part. Ditto, if the
9324 -- current instance is within another package instance or
9325 -- within a body that does not enclose the current instance.
9326 -- In these three cases the freeze node of the previous
9327 -- instance is not relevant.
9328
9329 while Present (Scop) and then Scop /= Standard_Standard loop
9330 exit when Scop = Par_I
9331 or else
9332 (Is_Generic_Instance (Scop)
9333 and then Scope_Depth (Scop) > Scope_Depth (Par_I));
9334 Scop := Scope (Scop);
9335 end loop;
9336
9337 -- Previous instance encloses current instance
9338
9339 if Scop = Par_I then
9340 null;
9341
9342 -- If the next node is a source body we must freeze in
9343 -- the current scope as well.
9344
9345 elsif Present (Next (N))
9346 and then Nkind_In (Next (N), N_Subprogram_Body,
9347 N_Package_Body)
9348 and then Comes_From_Source (Next (N))
9349 then
9350 null;
9351
9352 -- Current instance is within an unrelated instance
9353
9354 elsif Is_Generic_Instance (Scop) then
9355 null;
9356
9357 -- Current instance is within an unrelated body
9358
9359 elsif Present (Enclosing_N)
9360 and then Enclosing_N /= Enclosing_Body (Par_I)
9361 then
9362 null;
9363
9364 else
9365 Insert_After (Freeze_Node (Par_I), F_Node);
9366 return;
9367 end if;
9368 end if;
9369 end;
9370 end if;
9371
9372 -- When the instantiation occurs in a package declaration, append the
9373 -- freeze node to the private declarations (if any).
9374
9375 if Nkind (Par_N) = N_Package_Specification
9376 and then Decls = Visible_Declarations (Par_N)
9377 and then Present (Private_Declarations (Par_N))
9378 and then not Is_Empty_List (Private_Declarations (Par_N))
9379 then
9380 Decls := Private_Declarations (Par_N);
9381 Decl := First (Decls);
9382 end if;
9383
9384 -- Determine the proper freeze point of a package instantiation. We
9385 -- adhere to the general rule of a package or subprogram body causing
9386 -- freezing of anything before it in the same declarative region. In
9387 -- this case, the proper freeze point of a package instantiation is
9388 -- before the first source body which follows, or before a stub. This
9389 -- ensures that entities coming from the instance are already frozen
9390 -- and usable in source bodies.
9391
9392 if Nkind (Par_N) /= N_Package_Declaration
9393 and then Ekind (Inst) = E_Package
9394 and then Is_Generic_Instance (Inst)
9395 and then
9396 not In_Same_Source_Unit (Generic_Parent (Parent (Inst)), Inst)
9397 then
9398 while Present (Decl) loop
9399 if (Nkind (Decl) in N_Unit_Body
9400 or else
9401 Nkind (Decl) in N_Body_Stub)
9402 and then Comes_From_Source (Decl)
9403 then
9404 Insert_Before (Decl, F_Node);
9405 return;
9406 end if;
9407
9408 Next (Decl);
9409 end loop;
9410 end if;
9411
9412 -- In a package declaration, or if no previous body, insert at end
9413 -- of list.
9414
9415 Set_Sloc (F_Node, Sloc (Last (Decls)));
9416 Insert_After (Last (Decls), F_Node);
9417 end if;
9418 end Insert_Freeze_Node_For_Instance;
9419
9420 ------------------
9421 -- Install_Body --
9422 ------------------
9423
9424 procedure Install_Body
9425 (Act_Body : Node_Id;
9426 N : Node_Id;
9427 Gen_Body : Node_Id;
9428 Gen_Decl : Node_Id)
9429 is
9430 function In_Same_Scope (Gen_Id, Act_Id : Node_Id) return Boolean;
9431 -- Check if the generic definition and the instantiation come from
9432 -- a common scope, in which case the instance must be frozen after
9433 -- the generic body.
9434
9435 function True_Sloc (N, Act_Unit : Node_Id) return Source_Ptr;
9436 -- If the instance is nested inside a generic unit, the Sloc of the
9437 -- instance indicates the place of the original definition, not the
9438 -- point of the current enclosing instance. Pending a better usage of
9439 -- Slocs to indicate instantiation places, we determine the place of
9440 -- origin of a node by finding the maximum sloc of any ancestor node.
9441 -- Why is this not equivalent to Top_Level_Location ???
9442
9443 -------------------
9444 -- In_Same_Scope --
9445 -------------------
9446
9447 function In_Same_Scope (Gen_Id, Act_Id : Node_Id) return Boolean is
9448 Act_Scop : Entity_Id := Scope (Act_Id);
9449 Gen_Scop : Entity_Id := Scope (Gen_Id);
9450
9451 begin
9452 while Act_Scop /= Standard_Standard
9453 and then Gen_Scop /= Standard_Standard
9454 loop
9455 if Act_Scop = Gen_Scop then
9456 return True;
9457 end if;
9458
9459 Act_Scop := Scope (Act_Scop);
9460 Gen_Scop := Scope (Gen_Scop);
9461 end loop;
9462
9463 return False;
9464 end In_Same_Scope;
9465
9466 ---------------
9467 -- True_Sloc --
9468 ---------------
9469
9470 function True_Sloc (N, Act_Unit : Node_Id) return Source_Ptr is
9471 N1 : Node_Id;
9472 Res : Source_Ptr;
9473
9474 begin
9475 Res := Sloc (N);
9476 N1 := N;
9477 while Present (N1) and then N1 /= Act_Unit loop
9478 if Sloc (N1) > Res then
9479 Res := Sloc (N1);
9480 end if;
9481
9482 N1 := Parent (N1);
9483 end loop;
9484
9485 return Res;
9486 end True_Sloc;
9487
9488 Act_Id : constant Entity_Id := Corresponding_Spec (Act_Body);
9489 Act_Unit : constant Node_Id := Unit (Cunit (Get_Source_Unit (N)));
9490 Gen_Id : constant Entity_Id := Corresponding_Spec (Gen_Body);
9491 Par : constant Entity_Id := Scope (Gen_Id);
9492 Gen_Unit : constant Node_Id :=
9493 Unit (Cunit (Get_Source_Unit (Gen_Decl)));
9494
9495 Body_Unit : Node_Id;
9496 F_Node : Node_Id;
9497 Must_Delay : Boolean;
9498 Orig_Body : Node_Id := Gen_Body;
9499
9500 -- Start of processing for Install_Body
9501
9502 begin
9503 -- Handle first the case of an instance with incomplete actual types.
9504 -- The instance body cannot be placed after the declaration because
9505 -- full views have not been seen yet. Any use of the non-limited views
9506 -- in the instance body requires the presence of a regular with_clause
9507 -- in the enclosing unit, and will fail if this with_clause is missing.
9508 -- We place the instance body at the beginning of the enclosing body,
9509 -- which is the unit being compiled. The freeze node for the instance
9510 -- is then placed after the instance body.
9511
9512 if not Is_Empty_Elmt_List (Incomplete_Actuals (Act_Id))
9513 and then Expander_Active
9514 and then Ekind (Scope (Act_Id)) = E_Package
9515 then
9516 declare
9517 Scop : constant Entity_Id := Scope (Act_Id);
9518 Body_Id : constant Node_Id :=
9519 Corresponding_Body (Unit_Declaration_Node (Scop));
9520
9521 begin
9522 Ensure_Freeze_Node (Act_Id);
9523 F_Node := Freeze_Node (Act_Id);
9524 if Present (Body_Id) then
9525 Set_Is_Frozen (Act_Id, False);
9526 Prepend (Act_Body, Declarations (Parent (Body_Id)));
9527 if Is_List_Member (F_Node) then
9528 Remove (F_Node);
9529 end if;
9530
9531 Insert_After (Act_Body, F_Node);
9532 end if;
9533 end;
9534 return;
9535 end if;
9536
9537 -- If the body is a subunit, the freeze point is the corresponding stub
9538 -- in the current compilation, not the subunit itself.
9539
9540 if Nkind (Parent (Gen_Body)) = N_Subunit then
9541 Orig_Body := Corresponding_Stub (Parent (Gen_Body));
9542 else
9543 Orig_Body := Gen_Body;
9544 end if;
9545
9546 Body_Unit := Unit (Cunit (Get_Source_Unit (Orig_Body)));
9547
9548 -- If the instantiation and the generic definition appear in the same
9549 -- package declaration, this is an early instantiation. If they appear
9550 -- in the same declarative part, it is an early instantiation only if
9551 -- the generic body appears textually later, and the generic body is
9552 -- also in the main unit.
9553
9554 -- If instance is nested within a subprogram, and the generic body
9555 -- is not, the instance is delayed because the enclosing body is. If
9556 -- instance and body are within the same scope, or the same subprogram
9557 -- body, indicate explicitly that the instance is delayed.
9558
9559 Must_Delay :=
9560 (Gen_Unit = Act_Unit
9561 and then (Nkind_In (Gen_Unit, N_Generic_Package_Declaration,
9562 N_Package_Declaration)
9563 or else (Gen_Unit = Body_Unit
9564 and then True_Sloc (N, Act_Unit) <
9565 Sloc (Orig_Body)))
9566 and then Is_In_Main_Unit (Original_Node (Gen_Unit))
9567 and then In_Same_Scope (Gen_Id, Act_Id));
9568
9569 -- If this is an early instantiation, the freeze node is placed after
9570 -- the generic body. Otherwise, if the generic appears in an instance,
9571 -- we cannot freeze the current instance until the outer one is frozen.
9572 -- This is only relevant if the current instance is nested within some
9573 -- inner scope not itself within the outer instance. If this scope is
9574 -- a package body in the same declarative part as the outer instance,
9575 -- then that body needs to be frozen after the outer instance. Finally,
9576 -- if no delay is needed, we place the freeze node at the end of the
9577 -- current declarative part.
9578
9579 if Expander_Active
9580 and then (No (Freeze_Node (Act_Id))
9581 or else not Is_List_Member (Freeze_Node (Act_Id)))
9582 then
9583 Ensure_Freeze_Node (Act_Id);
9584 F_Node := Freeze_Node (Act_Id);
9585
9586 if Must_Delay then
9587 Insert_After (Orig_Body, F_Node);
9588
9589 elsif Is_Generic_Instance (Par)
9590 and then Present (Freeze_Node (Par))
9591 and then Scope (Act_Id) /= Par
9592 then
9593 -- Freeze instance of inner generic after instance of enclosing
9594 -- generic.
9595
9596 if In_Same_Declarative_Part (Parent (Freeze_Node (Par)), N) then
9597
9598 -- Handle the following case:
9599
9600 -- package Parent_Inst is new ...
9601 -- Parent_Inst []
9602
9603 -- procedure P ... -- this body freezes Parent_Inst
9604
9605 -- package Inst is new ...
9606
9607 -- In this particular scenario, the freeze node for Inst must
9608 -- be inserted in the same manner as that of Parent_Inst,
9609 -- before the next source body or at the end of the declarative
9610 -- list (body not available). If body P did not exist and
9611 -- Parent_Inst was frozen after Inst, either by a body
9612 -- following Inst or at the end of the declarative region,
9613 -- the freeze node for Inst must be inserted after that of
9614 -- Parent_Inst. This relation is established by comparing
9615 -- the Slocs of Parent_Inst freeze node and Inst.
9616 -- We examine the parents of the enclosing lists to handle
9617 -- the case where the parent instance is in the visible part
9618 -- of a package declaration, and the inner instance is in
9619 -- the corresponding private part.
9620
9621 if Parent (List_Containing (Get_Unit_Instantiation_Node (Par)))
9622 = Parent (List_Containing (N))
9623 and then Sloc (Freeze_Node (Par)) < Sloc (N)
9624 then
9625 Insert_Freeze_Node_For_Instance (N, F_Node);
9626 else
9627 Insert_After (Freeze_Node (Par), F_Node);
9628 end if;
9629
9630 -- Freeze package enclosing instance of inner generic after
9631 -- instance of enclosing generic.
9632
9633 elsif Nkind_In (Parent (N), N_Package_Body, N_Subprogram_Body)
9634 and then In_Same_Declarative_Part
9635 (Parent (Freeze_Node (Par)), Parent (N))
9636 then
9637 declare
9638 Enclosing : Entity_Id;
9639
9640 begin
9641 Enclosing := Corresponding_Spec (Parent (N));
9642
9643 if No (Enclosing) then
9644 Enclosing := Defining_Entity (Parent (N));
9645 end if;
9646
9647 Insert_Freeze_Node_For_Instance (N, F_Node);
9648 Ensure_Freeze_Node (Enclosing);
9649
9650 if not Is_List_Member (Freeze_Node (Enclosing)) then
9651
9652 -- The enclosing context is a subunit, insert the freeze
9653 -- node after the stub.
9654
9655 if Nkind (Parent (Parent (N))) = N_Subunit then
9656 Insert_Freeze_Node_For_Instance
9657 (Corresponding_Stub (Parent (Parent (N))),
9658 Freeze_Node (Enclosing));
9659
9660 -- The enclosing context is a package with a stub body
9661 -- which has already been replaced by the real body.
9662 -- Insert the freeze node after the actual body.
9663
9664 elsif Ekind (Enclosing) = E_Package
9665 and then Present (Body_Entity (Enclosing))
9666 and then Was_Originally_Stub
9667 (Parent (Body_Entity (Enclosing)))
9668 then
9669 Insert_Freeze_Node_For_Instance
9670 (Parent (Body_Entity (Enclosing)),
9671 Freeze_Node (Enclosing));
9672
9673 -- The parent instance has been frozen before the body of
9674 -- the enclosing package, insert the freeze node after
9675 -- the body.
9676
9677 elsif List_Containing (Freeze_Node (Par)) =
9678 List_Containing (Parent (N))
9679 and then Sloc (Freeze_Node (Par)) < Sloc (Parent (N))
9680 then
9681 Insert_Freeze_Node_For_Instance
9682 (Parent (N), Freeze_Node (Enclosing));
9683
9684 else
9685 Insert_After
9686 (Freeze_Node (Par), Freeze_Node (Enclosing));
9687 end if;
9688 end if;
9689 end;
9690
9691 else
9692 Insert_Freeze_Node_For_Instance (N, F_Node);
9693 end if;
9694
9695 else
9696 Insert_Freeze_Node_For_Instance (N, F_Node);
9697 end if;
9698 end if;
9699
9700 Set_Is_Frozen (Act_Id);
9701 Insert_Before (N, Act_Body);
9702 Mark_Rewrite_Insertion (Act_Body);
9703 end Install_Body;
9704
9705 -----------------------------
9706 -- Install_Formal_Packages --
9707 -----------------------------
9708
9709 procedure Install_Formal_Packages (Par : Entity_Id) is
9710 E : Entity_Id;
9711 Gen : Entity_Id;
9712 Gen_E : Entity_Id := Empty;
9713
9714 begin
9715 E := First_Entity (Par);
9716
9717 -- If we are installing an instance parent, locate the formal packages
9718 -- of its generic parent.
9719
9720 if Is_Generic_Instance (Par) then
9721 Gen := Generic_Parent (Package_Specification (Par));
9722 Gen_E := First_Entity (Gen);
9723 end if;
9724
9725 while Present (E) loop
9726 if Ekind (E) = E_Package
9727 and then Nkind (Parent (E)) = N_Package_Renaming_Declaration
9728 then
9729 -- If this is the renaming for the parent instance, done
9730
9731 if Renamed_Object (E) = Par then
9732 exit;
9733
9734 -- The visibility of a formal of an enclosing generic is already
9735 -- correct.
9736
9737 elsif Denotes_Formal_Package (E) then
9738 null;
9739
9740 elsif Present (Associated_Formal_Package (E)) then
9741 Check_Generic_Actuals (Renamed_Object (E), True);
9742 Set_Is_Hidden (E, False);
9743
9744 -- Find formal package in generic unit that corresponds to
9745 -- (instance of) formal package in instance.
9746
9747 while Present (Gen_E) and then Chars (Gen_E) /= Chars (E) loop
9748 Next_Entity (Gen_E);
9749 end loop;
9750
9751 if Present (Gen_E) then
9752 Map_Formal_Package_Entities (Gen_E, E);
9753 end if;
9754 end if;
9755 end if;
9756
9757 Next_Entity (E);
9758
9759 if Present (Gen_E) then
9760 Next_Entity (Gen_E);
9761 end if;
9762 end loop;
9763 end Install_Formal_Packages;
9764
9765 --------------------
9766 -- Install_Parent --
9767 --------------------
9768
9769 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False) is
9770 Ancestors : constant Elist_Id := New_Elmt_List;
9771 S : constant Entity_Id := Current_Scope;
9772 Inst_Par : Entity_Id;
9773 First_Par : Entity_Id;
9774 Inst_Node : Node_Id;
9775 Gen_Par : Entity_Id;
9776 First_Gen : Entity_Id;
9777 Elmt : Elmt_Id;
9778
9779 procedure Install_Noninstance_Specs (Par : Entity_Id);
9780 -- Install the scopes of noninstance parent units ending with Par
9781
9782 procedure Install_Spec (Par : Entity_Id);
9783 -- The child unit is within the declarative part of the parent, so the
9784 -- declarations within the parent are immediately visible.
9785
9786 -------------------------------
9787 -- Install_Noninstance_Specs --
9788 -------------------------------
9789
9790 procedure Install_Noninstance_Specs (Par : Entity_Id) is
9791 begin
9792 if Present (Par)
9793 and then Par /= Standard_Standard
9794 and then not In_Open_Scopes (Par)
9795 then
9796 Install_Noninstance_Specs (Scope (Par));
9797 Install_Spec (Par);
9798 end if;
9799 end Install_Noninstance_Specs;
9800
9801 ------------------
9802 -- Install_Spec --
9803 ------------------
9804
9805 procedure Install_Spec (Par : Entity_Id) is
9806 Spec : constant Node_Id := Package_Specification (Par);
9807
9808 begin
9809 -- If this parent of the child instance is a top-level unit,
9810 -- then record the unit and its visibility for later resetting in
9811 -- Remove_Parent. We exclude units that are generic instances, as we
9812 -- only want to record this information for the ultimate top-level
9813 -- noninstance parent (is that always correct???).
9814
9815 if Scope (Par) = Standard_Standard
9816 and then not Is_Generic_Instance (Par)
9817 then
9818 Parent_Unit_Visible := Is_Immediately_Visible (Par);
9819 Instance_Parent_Unit := Par;
9820 end if;
9821
9822 -- Open the parent scope and make it and its declarations visible.
9823 -- If this point is not within a body, then only the visible
9824 -- declarations should be made visible, and installation of the
9825 -- private declarations is deferred until the appropriate point
9826 -- within analysis of the spec being instantiated (see the handling
9827 -- of parent visibility in Analyze_Package_Specification). This is
9828 -- relaxed in the case where the parent unit is Ada.Tags, to avoid
9829 -- private view problems that occur when compiling instantiations of
9830 -- a generic child of that package (Generic_Dispatching_Constructor).
9831 -- If the instance freezes a tagged type, inlinings of operations
9832 -- from Ada.Tags may need the full view of type Tag. If inlining took
9833 -- proper account of establishing visibility of inlined subprograms'
9834 -- parents then it should be possible to remove this
9835 -- special check. ???
9836
9837 Push_Scope (Par);
9838 Set_Is_Immediately_Visible (Par);
9839 Install_Visible_Declarations (Par);
9840 Set_Use (Visible_Declarations (Spec));
9841
9842 if In_Body or else Is_RTU (Par, Ada_Tags) then
9843 Install_Private_Declarations (Par);
9844 Set_Use (Private_Declarations (Spec));
9845 end if;
9846 end Install_Spec;
9847
9848 -- Start of processing for Install_Parent
9849
9850 begin
9851 -- We need to install the parent instance to compile the instantiation
9852 -- of the child, but the child instance must appear in the current
9853 -- scope. Given that we cannot place the parent above the current scope
9854 -- in the scope stack, we duplicate the current scope and unstack both
9855 -- after the instantiation is complete.
9856
9857 -- If the parent is itself the instantiation of a child unit, we must
9858 -- also stack the instantiation of its parent, and so on. Each such
9859 -- ancestor is the prefix of the name in a prior instantiation.
9860
9861 -- If this is a nested instance, the parent unit itself resolves to
9862 -- a renaming of the parent instance, whose declaration we need.
9863
9864 -- Finally, the parent may be a generic (not an instance) when the
9865 -- child unit appears as a formal package.
9866
9867 Inst_Par := P;
9868
9869 if Present (Renamed_Entity (Inst_Par)) then
9870 Inst_Par := Renamed_Entity (Inst_Par);
9871 end if;
9872
9873 First_Par := Inst_Par;
9874
9875 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
9876
9877 First_Gen := Gen_Par;
9878
9879 while Present (Gen_Par) and then Is_Child_Unit (Gen_Par) loop
9880
9881 -- Load grandparent instance as well
9882
9883 Inst_Node := Get_Unit_Instantiation_Node (Inst_Par);
9884
9885 if Nkind (Name (Inst_Node)) = N_Expanded_Name then
9886 Inst_Par := Entity (Prefix (Name (Inst_Node)));
9887
9888 if Present (Renamed_Entity (Inst_Par)) then
9889 Inst_Par := Renamed_Entity (Inst_Par);
9890 end if;
9891
9892 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
9893
9894 if Present (Gen_Par) then
9895 Prepend_Elmt (Inst_Par, Ancestors);
9896
9897 else
9898 -- Parent is not the name of an instantiation
9899
9900 Install_Noninstance_Specs (Inst_Par);
9901 exit;
9902 end if;
9903
9904 else
9905 -- Previous error
9906
9907 exit;
9908 end if;
9909 end loop;
9910
9911 if Present (First_Gen) then
9912 Append_Elmt (First_Par, Ancestors);
9913 else
9914 Install_Noninstance_Specs (First_Par);
9915 end if;
9916
9917 if not Is_Empty_Elmt_List (Ancestors) then
9918 Elmt := First_Elmt (Ancestors);
9919 while Present (Elmt) loop
9920 Install_Spec (Node (Elmt));
9921 Install_Formal_Packages (Node (Elmt));
9922 Next_Elmt (Elmt);
9923 end loop;
9924 end if;
9925
9926 if not In_Body then
9927 Push_Scope (S);
9928 end if;
9929 end Install_Parent;
9930
9931 -------------------------------
9932 -- Install_Hidden_Primitives --
9933 -------------------------------
9934
9935 procedure Install_Hidden_Primitives
9936 (Prims_List : in out Elist_Id;
9937 Gen_T : Entity_Id;
9938 Act_T : Entity_Id)
9939 is
9940 Elmt : Elmt_Id;
9941 List : Elist_Id := No_Elist;
9942 Prim_G_Elmt : Elmt_Id;
9943 Prim_A_Elmt : Elmt_Id;
9944 Prim_G : Node_Id;
9945 Prim_A : Node_Id;
9946
9947 begin
9948 -- No action needed in case of serious errors because we cannot trust
9949 -- in the order of primitives
9950
9951 if Serious_Errors_Detected > 0 then
9952 return;
9953
9954 -- No action possible if we don't have available the list of primitive
9955 -- operations
9956
9957 elsif No (Gen_T)
9958 or else not Is_Record_Type (Gen_T)
9959 or else not Is_Tagged_Type (Gen_T)
9960 or else not Is_Record_Type (Act_T)
9961 or else not Is_Tagged_Type (Act_T)
9962 then
9963 return;
9964
9965 -- There is no need to handle interface types since their primitives
9966 -- cannot be hidden
9967
9968 elsif Is_Interface (Gen_T) then
9969 return;
9970 end if;
9971
9972 Prim_G_Elmt := First_Elmt (Primitive_Operations (Gen_T));
9973
9974 if not Is_Class_Wide_Type (Act_T) then
9975 Prim_A_Elmt := First_Elmt (Primitive_Operations (Act_T));
9976 else
9977 Prim_A_Elmt := First_Elmt (Primitive_Operations (Root_Type (Act_T)));
9978 end if;
9979
9980 loop
9981 -- Skip predefined primitives in the generic formal
9982
9983 while Present (Prim_G_Elmt)
9984 and then Is_Predefined_Dispatching_Operation (Node (Prim_G_Elmt))
9985 loop
9986 Next_Elmt (Prim_G_Elmt);
9987 end loop;
9988
9989 -- Skip predefined primitives in the generic actual
9990
9991 while Present (Prim_A_Elmt)
9992 and then Is_Predefined_Dispatching_Operation (Node (Prim_A_Elmt))
9993 loop
9994 Next_Elmt (Prim_A_Elmt);
9995 end loop;
9996
9997 exit when No (Prim_G_Elmt) or else No (Prim_A_Elmt);
9998
9999 Prim_G := Node (Prim_G_Elmt);
10000 Prim_A := Node (Prim_A_Elmt);
10001
10002 -- There is no need to handle interface primitives because their
10003 -- primitives are not hidden
10004
10005 exit when Present (Interface_Alias (Prim_G));
10006
10007 -- Here we install one hidden primitive
10008
10009 if Chars (Prim_G) /= Chars (Prim_A)
10010 and then Has_Suffix (Prim_A, 'P')
10011 and then Remove_Suffix (Prim_A, 'P') = Chars (Prim_G)
10012 then
10013 Set_Chars (Prim_A, Chars (Prim_G));
10014 Append_New_Elmt (Prim_A, To => List);
10015 end if;
10016
10017 Next_Elmt (Prim_A_Elmt);
10018 Next_Elmt (Prim_G_Elmt);
10019 end loop;
10020
10021 -- Append the elements to the list of temporarily visible primitives
10022 -- avoiding duplicates.
10023
10024 if Present (List) then
10025 if No (Prims_List) then
10026 Prims_List := New_Elmt_List;
10027 end if;
10028
10029 Elmt := First_Elmt (List);
10030 while Present (Elmt) loop
10031 Append_Unique_Elmt (Node (Elmt), Prims_List);
10032 Next_Elmt (Elmt);
10033 end loop;
10034 end if;
10035 end Install_Hidden_Primitives;
10036
10037 -------------------------------
10038 -- Restore_Hidden_Primitives --
10039 -------------------------------
10040
10041 procedure Restore_Hidden_Primitives (Prims_List : in out Elist_Id) is
10042 Prim_Elmt : Elmt_Id;
10043 Prim : Node_Id;
10044
10045 begin
10046 if Prims_List /= No_Elist then
10047 Prim_Elmt := First_Elmt (Prims_List);
10048 while Present (Prim_Elmt) loop
10049 Prim := Node (Prim_Elmt);
10050 Set_Chars (Prim, Add_Suffix (Prim, 'P'));
10051 Next_Elmt (Prim_Elmt);
10052 end loop;
10053
10054 Prims_List := No_Elist;
10055 end if;
10056 end Restore_Hidden_Primitives;
10057
10058 --------------------------------
10059 -- Instantiate_Formal_Package --
10060 --------------------------------
10061
10062 function Instantiate_Formal_Package
10063 (Formal : Node_Id;
10064 Actual : Node_Id;
10065 Analyzed_Formal : Node_Id) return List_Id
10066 is
10067 Loc : constant Source_Ptr := Sloc (Actual);
10068 Hidden_Formals : constant Elist_Id := New_Elmt_List;
10069 Actual_Pack : Entity_Id;
10070 Formal_Pack : Entity_Id;
10071 Gen_Parent : Entity_Id;
10072 Decls : List_Id;
10073 Nod : Node_Id;
10074 Parent_Spec : Node_Id;
10075
10076 procedure Find_Matching_Actual
10077 (F : Node_Id;
10078 Act : in out Entity_Id);
10079 -- We need to associate each formal entity in the formal package with
10080 -- the corresponding entity in the actual package. The actual package
10081 -- has been analyzed and possibly expanded, and as a result there is
10082 -- no one-to-one correspondence between the two lists (for example,
10083 -- the actual may include subtypes, itypes, and inherited primitive
10084 -- operations, interspersed among the renaming declarations for the
10085 -- actuals). We retrieve the corresponding actual by name because each
10086 -- actual has the same name as the formal, and they do appear in the
10087 -- same order.
10088
10089 function Get_Formal_Entity (N : Node_Id) return Entity_Id;
10090 -- Retrieve entity of defining entity of generic formal parameter.
10091 -- Only the declarations of formals need to be considered when
10092 -- linking them to actuals, but the declarative list may include
10093 -- internal entities generated during analysis, and those are ignored.
10094
10095 procedure Match_Formal_Entity
10096 (Formal_Node : Node_Id;
10097 Formal_Ent : Entity_Id;
10098 Actual_Ent : Entity_Id);
10099 -- Associates the formal entity with the actual. In the case where
10100 -- Formal_Ent is a formal package, this procedure iterates through all
10101 -- of its formals and enters associations between the actuals occurring
10102 -- in the formal package's corresponding actual package (given by
10103 -- Actual_Ent) and the formal package's formal parameters. This
10104 -- procedure recurses if any of the parameters is itself a package.
10105
10106 function Is_Instance_Of
10107 (Act_Spec : Entity_Id;
10108 Gen_Anc : Entity_Id) return Boolean;
10109 -- The actual can be an instantiation of a generic within another
10110 -- instance, in which case there is no direct link from it to the
10111 -- original generic ancestor. In that case, we recognize that the
10112 -- ultimate ancestor is the same by examining names and scopes.
10113
10114 procedure Process_Nested_Formal (Formal : Entity_Id);
10115 -- If the current formal is declared with a box, its own formals are
10116 -- visible in the instance, as they were in the generic, and their
10117 -- Hidden flag must be reset. If some of these formals are themselves
10118 -- packages declared with a box, the processing must be recursive.
10119
10120 --------------------------
10121 -- Find_Matching_Actual --
10122 --------------------------
10123
10124 procedure Find_Matching_Actual
10125 (F : Node_Id;
10126 Act : in out Entity_Id)
10127 is
10128 Formal_Ent : Entity_Id;
10129
10130 begin
10131 case Nkind (Original_Node (F)) is
10132 when N_Formal_Object_Declaration
10133 | N_Formal_Type_Declaration
10134 =>
10135 Formal_Ent := Defining_Identifier (F);
10136
10137 while Chars (Act) /= Chars (Formal_Ent) loop
10138 Next_Entity (Act);
10139 end loop;
10140
10141 when N_Formal_Package_Declaration
10142 | N_Formal_Subprogram_Declaration
10143 | N_Generic_Package_Declaration
10144 | N_Package_Declaration
10145 =>
10146 Formal_Ent := Defining_Entity (F);
10147
10148 while Chars (Act) /= Chars (Formal_Ent) loop
10149 Next_Entity (Act);
10150 end loop;
10151
10152 when others =>
10153 raise Program_Error;
10154 end case;
10155 end Find_Matching_Actual;
10156
10157 -------------------------
10158 -- Match_Formal_Entity --
10159 -------------------------
10160
10161 procedure Match_Formal_Entity
10162 (Formal_Node : Node_Id;
10163 Formal_Ent : Entity_Id;
10164 Actual_Ent : Entity_Id)
10165 is
10166 Act_Pkg : Entity_Id;
10167
10168 begin
10169 Set_Instance_Of (Formal_Ent, Actual_Ent);
10170
10171 if Ekind (Actual_Ent) = E_Package then
10172
10173 -- Record associations for each parameter
10174
10175 Act_Pkg := Actual_Ent;
10176
10177 declare
10178 A_Ent : Entity_Id := First_Entity (Act_Pkg);
10179 F_Ent : Entity_Id;
10180 F_Node : Node_Id;
10181
10182 Gen_Decl : Node_Id;
10183 Formals : List_Id;
10184 Actual : Entity_Id;
10185
10186 begin
10187 -- Retrieve the actual given in the formal package declaration
10188
10189 Actual := Entity (Name (Original_Node (Formal_Node)));
10190
10191 -- The actual in the formal package declaration may be a
10192 -- renamed generic package, in which case we want to retrieve
10193 -- the original generic in order to traverse its formal part.
10194
10195 if Present (Renamed_Entity (Actual)) then
10196 Gen_Decl := Unit_Declaration_Node (Renamed_Entity (Actual));
10197 else
10198 Gen_Decl := Unit_Declaration_Node (Actual);
10199 end if;
10200
10201 Formals := Generic_Formal_Declarations (Gen_Decl);
10202
10203 if Present (Formals) then
10204 F_Node := First_Non_Pragma (Formals);
10205 else
10206 F_Node := Empty;
10207 end if;
10208
10209 while Present (A_Ent)
10210 and then Present (F_Node)
10211 and then A_Ent /= First_Private_Entity (Act_Pkg)
10212 loop
10213 F_Ent := Get_Formal_Entity (F_Node);
10214
10215 if Present (F_Ent) then
10216
10217 -- This is a formal of the original package. Record
10218 -- association and recurse.
10219
10220 Find_Matching_Actual (F_Node, A_Ent);
10221 Match_Formal_Entity (F_Node, F_Ent, A_Ent);
10222 Next_Entity (A_Ent);
10223 end if;
10224
10225 Next_Non_Pragma (F_Node);
10226 end loop;
10227 end;
10228 end if;
10229 end Match_Formal_Entity;
10230
10231 -----------------------
10232 -- Get_Formal_Entity --
10233 -----------------------
10234
10235 function Get_Formal_Entity (N : Node_Id) return Entity_Id is
10236 Kind : constant Node_Kind := Nkind (Original_Node (N));
10237 begin
10238 case Kind is
10239 when N_Formal_Object_Declaration =>
10240 return Defining_Identifier (N);
10241
10242 when N_Formal_Type_Declaration =>
10243 return Defining_Identifier (N);
10244
10245 when N_Formal_Subprogram_Declaration =>
10246 return Defining_Unit_Name (Specification (N));
10247
10248 when N_Formal_Package_Declaration =>
10249 return Defining_Identifier (Original_Node (N));
10250
10251 when N_Generic_Package_Declaration =>
10252 return Defining_Identifier (Original_Node (N));
10253
10254 -- All other declarations are introduced by semantic analysis and
10255 -- have no match in the actual.
10256
10257 when others =>
10258 return Empty;
10259 end case;
10260 end Get_Formal_Entity;
10261
10262 --------------------
10263 -- Is_Instance_Of --
10264 --------------------
10265
10266 function Is_Instance_Of
10267 (Act_Spec : Entity_Id;
10268 Gen_Anc : Entity_Id) return Boolean
10269 is
10270 Gen_Par : constant Entity_Id := Generic_Parent (Act_Spec);
10271
10272 begin
10273 if No (Gen_Par) then
10274 return False;
10275
10276 -- Simplest case: the generic parent of the actual is the formal
10277
10278 elsif Gen_Par = Gen_Anc then
10279 return True;
10280
10281 elsif Chars (Gen_Par) /= Chars (Gen_Anc) then
10282 return False;
10283
10284 -- The actual may be obtained through several instantiations. Its
10285 -- scope must itself be an instance of a generic declared in the
10286 -- same scope as the formal. Any other case is detected above.
10287
10288 elsif not Is_Generic_Instance (Scope (Gen_Par)) then
10289 return False;
10290
10291 else
10292 return Generic_Parent (Parent (Scope (Gen_Par))) = Scope (Gen_Anc);
10293 end if;
10294 end Is_Instance_Of;
10295
10296 ---------------------------
10297 -- Process_Nested_Formal --
10298 ---------------------------
10299
10300 procedure Process_Nested_Formal (Formal : Entity_Id) is
10301 Ent : Entity_Id;
10302
10303 begin
10304 if Present (Associated_Formal_Package (Formal))
10305 and then Box_Present (Parent (Associated_Formal_Package (Formal)))
10306 then
10307 Ent := First_Entity (Formal);
10308 while Present (Ent) loop
10309 Set_Is_Hidden (Ent, False);
10310 Set_Is_Visible_Formal (Ent);
10311 Set_Is_Potentially_Use_Visible
10312 (Ent, Is_Potentially_Use_Visible (Formal));
10313
10314 if Ekind (Ent) = E_Package then
10315 exit when Renamed_Entity (Ent) = Renamed_Entity (Formal);
10316 Process_Nested_Formal (Ent);
10317 end if;
10318
10319 Next_Entity (Ent);
10320 end loop;
10321 end if;
10322 end Process_Nested_Formal;
10323
10324 -- Start of processing for Instantiate_Formal_Package
10325
10326 begin
10327 Analyze (Actual);
10328
10329 -- The actual must be a package instance, or else a current instance
10330 -- such as a parent generic within the body of a generic child.
10331
10332 if not Is_Entity_Name (Actual)
10333 or else not Is_Package_Or_Generic_Package (Entity (Actual))
10334 then
10335 Error_Msg_N
10336 ("expect package instance to instantiate formal", Actual);
10337 Abandon_Instantiation (Actual);
10338 raise Program_Error;
10339
10340 else
10341 Actual_Pack := Entity (Actual);
10342 Set_Is_Instantiated (Actual_Pack);
10343
10344 -- The actual may be a renamed package, or an outer generic formal
10345 -- package whose instantiation is converted into a renaming.
10346
10347 if Present (Renamed_Object (Actual_Pack)) then
10348 Actual_Pack := Renamed_Object (Actual_Pack);
10349 end if;
10350
10351 if Nkind (Analyzed_Formal) = N_Formal_Package_Declaration then
10352 Gen_Parent := Get_Instance_Of (Entity (Name (Analyzed_Formal)));
10353 Formal_Pack := Defining_Identifier (Analyzed_Formal);
10354 else
10355 Gen_Parent :=
10356 Generic_Parent (Specification (Analyzed_Formal));
10357 Formal_Pack :=
10358 Defining_Unit_Name (Specification (Analyzed_Formal));
10359 end if;
10360
10361 if Nkind (Parent (Actual_Pack)) = N_Defining_Program_Unit_Name then
10362 Parent_Spec := Package_Specification (Actual_Pack);
10363 else
10364 Parent_Spec := Parent (Actual_Pack);
10365 end if;
10366
10367 if Gen_Parent = Any_Id then
10368 Error_Msg_N
10369 ("previous error in declaration of formal package", Actual);
10370 Abandon_Instantiation (Actual);
10371
10372 elsif Is_Instance_Of (Parent_Spec, Get_Instance_Of (Gen_Parent)) then
10373 null;
10374
10375 -- If this is the current instance of an enclosing generic, that unit
10376 -- is the generic package we need.
10377
10378 elsif In_Open_Scopes (Actual_Pack)
10379 and then Ekind (Actual_Pack) = E_Generic_Package
10380 then
10381 null;
10382
10383 else
10384 Error_Msg_NE
10385 ("actual parameter must be instance of&", Actual, Gen_Parent);
10386 Abandon_Instantiation (Actual);
10387 end if;
10388
10389 Set_Instance_Of (Defining_Identifier (Formal), Actual_Pack);
10390 Map_Formal_Package_Entities (Formal_Pack, Actual_Pack);
10391
10392 Nod :=
10393 Make_Package_Renaming_Declaration (Loc,
10394 Defining_Unit_Name => New_Copy (Defining_Identifier (Formal)),
10395 Name => New_Occurrence_Of (Actual_Pack, Loc));
10396
10397 Set_Associated_Formal_Package
10398 (Defining_Unit_Name (Nod), Defining_Identifier (Formal));
10399 Decls := New_List (Nod);
10400
10401 -- If the formal F has a box, then the generic declarations are
10402 -- visible in the generic G. In an instance of G, the corresponding
10403 -- entities in the actual for F (which are the actuals for the
10404 -- instantiation of the generic that F denotes) must also be made
10405 -- visible for analysis of the current instance. On exit from the
10406 -- current instance, those entities are made private again. If the
10407 -- actual is currently in use, these entities are also use-visible.
10408
10409 -- The loop through the actual entities also steps through the formal
10410 -- entities and enters associations from formals to actuals into the
10411 -- renaming map. This is necessary to properly handle checking of
10412 -- actual parameter associations for later formals that depend on
10413 -- actuals declared in the formal package.
10414
10415 -- In Ada 2005, partial parameterization requires that we make
10416 -- visible the actuals corresponding to formals that were defaulted
10417 -- in the formal package. There formals are identified because they
10418 -- remain formal generics within the formal package, rather than
10419 -- being renamings of the actuals supplied.
10420
10421 declare
10422 Gen_Decl : constant Node_Id :=
10423 Unit_Declaration_Node (Gen_Parent);
10424 Formals : constant List_Id :=
10425 Generic_Formal_Declarations (Gen_Decl);
10426
10427 Actual_Ent : Entity_Id;
10428 Actual_Of_Formal : Node_Id;
10429 Formal_Node : Node_Id;
10430 Formal_Ent : Entity_Id;
10431
10432 begin
10433 if Present (Formals) then
10434 Formal_Node := First_Non_Pragma (Formals);
10435 else
10436 Formal_Node := Empty;
10437 end if;
10438
10439 Actual_Ent := First_Entity (Actual_Pack);
10440 Actual_Of_Formal :=
10441 First (Visible_Declarations (Specification (Analyzed_Formal)));
10442 while Present (Actual_Ent)
10443 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
10444 loop
10445 if Present (Formal_Node) then
10446 Formal_Ent := Get_Formal_Entity (Formal_Node);
10447
10448 if Present (Formal_Ent) then
10449 Find_Matching_Actual (Formal_Node, Actual_Ent);
10450 Match_Formal_Entity (Formal_Node, Formal_Ent, Actual_Ent);
10451
10452 -- We iterate at the same time over the actuals of the
10453 -- local package created for the formal, to determine
10454 -- which one of the formals of the original generic were
10455 -- defaulted in the formal. The corresponding actual
10456 -- entities are visible in the enclosing instance.
10457
10458 if Box_Present (Formal)
10459 or else
10460 (Present (Actual_Of_Formal)
10461 and then
10462 Is_Generic_Formal
10463 (Get_Formal_Entity (Actual_Of_Formal)))
10464 then
10465 Set_Is_Hidden (Actual_Ent, False);
10466 Set_Is_Visible_Formal (Actual_Ent);
10467 Set_Is_Potentially_Use_Visible
10468 (Actual_Ent, In_Use (Actual_Pack));
10469
10470 if Ekind (Actual_Ent) = E_Package then
10471 Process_Nested_Formal (Actual_Ent);
10472 end if;
10473
10474 else
10475 if not Is_Hidden (Actual_Ent) then
10476 Append_Elmt (Actual_Ent, Hidden_Formals);
10477 end if;
10478
10479 Set_Is_Hidden (Actual_Ent);
10480 Set_Is_Potentially_Use_Visible (Actual_Ent, False);
10481 end if;
10482 end if;
10483
10484 Next_Non_Pragma (Formal_Node);
10485 Next (Actual_Of_Formal);
10486
10487 else
10488 -- No further formals to match, but the generic part may
10489 -- contain inherited operation that are not hidden in the
10490 -- enclosing instance.
10491
10492 Next_Entity (Actual_Ent);
10493 end if;
10494 end loop;
10495
10496 -- Inherited subprograms generated by formal derived types are
10497 -- also visible if the types are.
10498
10499 Actual_Ent := First_Entity (Actual_Pack);
10500 while Present (Actual_Ent)
10501 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
10502 loop
10503 if Is_Overloadable (Actual_Ent)
10504 and then
10505 Nkind (Parent (Actual_Ent)) = N_Subtype_Declaration
10506 and then
10507 not Is_Hidden (Defining_Identifier (Parent (Actual_Ent)))
10508 then
10509 Set_Is_Hidden (Actual_Ent, False);
10510 Set_Is_Potentially_Use_Visible
10511 (Actual_Ent, In_Use (Actual_Pack));
10512 end if;
10513
10514 Next_Entity (Actual_Ent);
10515 end loop;
10516
10517 -- No conformance to check if the generic has no formal parameters
10518 -- and the formal package has no generic associations.
10519
10520 if Is_Empty_List (Formals)
10521 and then
10522 (Box_Present (Formal)
10523 or else No (Generic_Associations (Formal)))
10524 then
10525 return Decls;
10526 end if;
10527 end;
10528
10529 -- If the formal is not declared with a box, reanalyze it as an
10530 -- abbreviated instantiation, to verify the matching rules of 12.7.
10531 -- The actual checks are performed after the generic associations
10532 -- have been analyzed, to guarantee the same visibility for this
10533 -- instantiation and for the actuals.
10534
10535 -- In Ada 2005, the generic associations for the formal can include
10536 -- defaulted parameters. These are ignored during check. This
10537 -- internal instantiation is removed from the tree after conformance
10538 -- checking, because it contains formal declarations for those
10539 -- defaulted parameters, and those should not reach the back-end.
10540
10541 if not Box_Present (Formal) then
10542 declare
10543 I_Pack : constant Entity_Id :=
10544 Make_Temporary (Sloc (Actual), 'P');
10545
10546 begin
10547 Set_Is_Internal (I_Pack);
10548 Set_Ekind (I_Pack, E_Package);
10549 Set_Hidden_In_Formal_Instance (I_Pack, Hidden_Formals);
10550
10551 Append_To (Decls,
10552 Make_Package_Instantiation (Sloc (Actual),
10553 Defining_Unit_Name => I_Pack,
10554 Name =>
10555 New_Occurrence_Of
10556 (Get_Instance_Of (Gen_Parent), Sloc (Actual)),
10557 Generic_Associations => Generic_Associations (Formal)));
10558 end;
10559 end if;
10560
10561 return Decls;
10562 end if;
10563 end Instantiate_Formal_Package;
10564
10565 -----------------------------------
10566 -- Instantiate_Formal_Subprogram --
10567 -----------------------------------
10568
10569 function Instantiate_Formal_Subprogram
10570 (Formal : Node_Id;
10571 Actual : Node_Id;
10572 Analyzed_Formal : Node_Id) return Node_Id
10573 is
10574 Analyzed_S : constant Entity_Id :=
10575 Defining_Unit_Name (Specification (Analyzed_Formal));
10576 Formal_Sub : constant Entity_Id :=
10577 Defining_Unit_Name (Specification (Formal));
10578
10579 function From_Parent_Scope (Subp : Entity_Id) return Boolean;
10580 -- If the generic is a child unit, the parent has been installed on the
10581 -- scope stack, but a default subprogram cannot resolve to something
10582 -- on the parent because that parent is not really part of the visible
10583 -- context (it is there to resolve explicit local entities). If the
10584 -- default has resolved in this way, we remove the entity from immediate
10585 -- visibility and analyze the node again to emit an error message or
10586 -- find another visible candidate.
10587
10588 procedure Valid_Actual_Subprogram (Act : Node_Id);
10589 -- Perform legality check and raise exception on failure
10590
10591 -----------------------
10592 -- From_Parent_Scope --
10593 -----------------------
10594
10595 function From_Parent_Scope (Subp : Entity_Id) return Boolean is
10596 Gen_Scope : Node_Id;
10597
10598 begin
10599 Gen_Scope := Scope (Analyzed_S);
10600 while Present (Gen_Scope) and then Is_Child_Unit (Gen_Scope) loop
10601 if Scope (Subp) = Scope (Gen_Scope) then
10602 return True;
10603 end if;
10604
10605 Gen_Scope := Scope (Gen_Scope);
10606 end loop;
10607
10608 return False;
10609 end From_Parent_Scope;
10610
10611 -----------------------------
10612 -- Valid_Actual_Subprogram --
10613 -----------------------------
10614
10615 procedure Valid_Actual_Subprogram (Act : Node_Id) is
10616 Act_E : Entity_Id;
10617
10618 begin
10619 if Is_Entity_Name (Act) then
10620 Act_E := Entity (Act);
10621
10622 elsif Nkind (Act) = N_Selected_Component
10623 and then Is_Entity_Name (Selector_Name (Act))
10624 then
10625 Act_E := Entity (Selector_Name (Act));
10626
10627 else
10628 Act_E := Empty;
10629 end if;
10630
10631 if (Present (Act_E) and then Is_Overloadable (Act_E))
10632 or else Nkind_In (Act, N_Attribute_Reference,
10633 N_Indexed_Component,
10634 N_Character_Literal,
10635 N_Explicit_Dereference)
10636 then
10637 return;
10638 end if;
10639
10640 Error_Msg_NE
10641 ("expect subprogram or entry name in instantiation of &",
10642 Instantiation_Node, Formal_Sub);
10643 Abandon_Instantiation (Instantiation_Node);
10644 end Valid_Actual_Subprogram;
10645
10646 -- Local variables
10647
10648 Decl_Node : Node_Id;
10649 Loc : Source_Ptr;
10650 Nam : Node_Id;
10651 New_Spec : Node_Id;
10652 New_Subp : Entity_Id;
10653
10654 -- Start of processing for Instantiate_Formal_Subprogram
10655
10656 begin
10657 New_Spec := New_Copy_Tree (Specification (Formal));
10658
10659 -- The tree copy has created the proper instantiation sloc for the
10660 -- new specification. Use this location for all other constructed
10661 -- declarations.
10662
10663 Loc := Sloc (Defining_Unit_Name (New_Spec));
10664
10665 -- Create new entity for the actual (New_Copy_Tree does not), and
10666 -- indicate that it is an actual.
10667
10668 New_Subp := Make_Defining_Identifier (Loc, Chars (Formal_Sub));
10669 Set_Ekind (New_Subp, Ekind (Analyzed_S));
10670 Set_Is_Generic_Actual_Subprogram (New_Subp);
10671 Set_Defining_Unit_Name (New_Spec, New_Subp);
10672
10673 -- Create new entities for the each of the formals in the specification
10674 -- of the renaming declaration built for the actual.
10675
10676 if Present (Parameter_Specifications (New_Spec)) then
10677 declare
10678 F : Node_Id;
10679 F_Id : Entity_Id;
10680
10681 begin
10682 F := First (Parameter_Specifications (New_Spec));
10683 while Present (F) loop
10684 F_Id := Defining_Identifier (F);
10685
10686 Set_Defining_Identifier (F,
10687 Make_Defining_Identifier (Sloc (F_Id), Chars (F_Id)));
10688 Next (F);
10689 end loop;
10690 end;
10691 end if;
10692
10693 -- Find entity of actual. If the actual is an attribute reference, it
10694 -- cannot be resolved here (its formal is missing) but is handled
10695 -- instead in Attribute_Renaming. If the actual is overloaded, it is
10696 -- fully resolved subsequently, when the renaming declaration for the
10697 -- formal is analyzed. If it is an explicit dereference, resolve the
10698 -- prefix but not the actual itself, to prevent interpretation as call.
10699
10700 if Present (Actual) then
10701 Loc := Sloc (Actual);
10702 Set_Sloc (New_Spec, Loc);
10703
10704 if Nkind (Actual) = N_Operator_Symbol then
10705 Find_Direct_Name (Actual);
10706
10707 elsif Nkind (Actual) = N_Explicit_Dereference then
10708 Analyze (Prefix (Actual));
10709
10710 elsif Nkind (Actual) /= N_Attribute_Reference then
10711 Analyze (Actual);
10712 end if;
10713
10714 Valid_Actual_Subprogram (Actual);
10715 Nam := Actual;
10716
10717 elsif Present (Default_Name (Formal)) then
10718 if not Nkind_In (Default_Name (Formal), N_Attribute_Reference,
10719 N_Selected_Component,
10720 N_Indexed_Component,
10721 N_Character_Literal)
10722 and then Present (Entity (Default_Name (Formal)))
10723 then
10724 Nam := New_Occurrence_Of (Entity (Default_Name (Formal)), Loc);
10725 else
10726 Nam := New_Copy (Default_Name (Formal));
10727 Set_Sloc (Nam, Loc);
10728 end if;
10729
10730 elsif Box_Present (Formal) then
10731
10732 -- Actual is resolved at the point of instantiation. Create an
10733 -- identifier or operator with the same name as the formal.
10734
10735 if Nkind (Formal_Sub) = N_Defining_Operator_Symbol then
10736 Nam :=
10737 Make_Operator_Symbol (Loc,
10738 Chars => Chars (Formal_Sub),
10739 Strval => No_String);
10740 else
10741 Nam := Make_Identifier (Loc, Chars (Formal_Sub));
10742 end if;
10743
10744 elsif Nkind (Specification (Formal)) = N_Procedure_Specification
10745 and then Null_Present (Specification (Formal))
10746 then
10747 -- Generate null body for procedure, for use in the instance
10748
10749 Decl_Node :=
10750 Make_Subprogram_Body (Loc,
10751 Specification => New_Spec,
10752 Declarations => New_List,
10753 Handled_Statement_Sequence =>
10754 Make_Handled_Sequence_Of_Statements (Loc,
10755 Statements => New_List (Make_Null_Statement (Loc))));
10756
10757 Set_Is_Intrinsic_Subprogram (Defining_Unit_Name (New_Spec));
10758 return Decl_Node;
10759
10760 else
10761 Error_Msg_Sloc := Sloc (Scope (Analyzed_S));
10762 Error_Msg_NE
10763 ("missing actual&", Instantiation_Node, Formal_Sub);
10764 Error_Msg_NE
10765 ("\in instantiation of & declared#",
10766 Instantiation_Node, Scope (Analyzed_S));
10767 Abandon_Instantiation (Instantiation_Node);
10768 end if;
10769
10770 Decl_Node :=
10771 Make_Subprogram_Renaming_Declaration (Loc,
10772 Specification => New_Spec,
10773 Name => Nam);
10774
10775 -- If we do not have an actual and the formal specified <> then set to
10776 -- get proper default.
10777
10778 if No (Actual) and then Box_Present (Formal) then
10779 Set_From_Default (Decl_Node);
10780 end if;
10781
10782 -- Gather possible interpretations for the actual before analyzing the
10783 -- instance. If overloaded, it will be resolved when analyzing the
10784 -- renaming declaration.
10785
10786 if Box_Present (Formal) and then No (Actual) then
10787 Analyze (Nam);
10788
10789 if Is_Child_Unit (Scope (Analyzed_S))
10790 and then Present (Entity (Nam))
10791 then
10792 if not Is_Overloaded (Nam) then
10793 if From_Parent_Scope (Entity (Nam)) then
10794 Set_Is_Immediately_Visible (Entity (Nam), False);
10795 Set_Entity (Nam, Empty);
10796 Set_Etype (Nam, Empty);
10797
10798 Analyze (Nam);
10799 Set_Is_Immediately_Visible (Entity (Nam));
10800 end if;
10801
10802 else
10803 declare
10804 I : Interp_Index;
10805 It : Interp;
10806
10807 begin
10808 Get_First_Interp (Nam, I, It);
10809 while Present (It.Nam) loop
10810 if From_Parent_Scope (It.Nam) then
10811 Remove_Interp (I);
10812 end if;
10813
10814 Get_Next_Interp (I, It);
10815 end loop;
10816 end;
10817 end if;
10818 end if;
10819 end if;
10820
10821 -- The generic instantiation freezes the actual. This can only be done
10822 -- once the actual is resolved, in the analysis of the renaming
10823 -- declaration. To make the formal subprogram entity available, we set
10824 -- Corresponding_Formal_Spec to point to the formal subprogram entity.
10825 -- This is also needed in Analyze_Subprogram_Renaming for the processing
10826 -- of formal abstract subprograms.
10827
10828 Set_Corresponding_Formal_Spec (Decl_Node, Analyzed_S);
10829
10830 -- We cannot analyze the renaming declaration, and thus find the actual,
10831 -- until all the actuals are assembled in the instance. For subsequent
10832 -- checks of other actuals, indicate the node that will hold the
10833 -- instance of this formal.
10834
10835 Set_Instance_Of (Analyzed_S, Nam);
10836
10837 if Nkind (Actual) = N_Selected_Component
10838 and then Is_Task_Type (Etype (Prefix (Actual)))
10839 and then not Is_Frozen (Etype (Prefix (Actual)))
10840 then
10841 -- The renaming declaration will create a body, which must appear
10842 -- outside of the instantiation, We move the renaming declaration
10843 -- out of the instance, and create an additional renaming inside,
10844 -- to prevent freezing anomalies.
10845
10846 declare
10847 Anon_Id : constant Entity_Id := Make_Temporary (Loc, 'E');
10848
10849 begin
10850 Set_Defining_Unit_Name (New_Spec, Anon_Id);
10851 Insert_Before (Instantiation_Node, Decl_Node);
10852 Analyze (Decl_Node);
10853
10854 -- Now create renaming within the instance
10855
10856 Decl_Node :=
10857 Make_Subprogram_Renaming_Declaration (Loc,
10858 Specification => New_Copy_Tree (New_Spec),
10859 Name => New_Occurrence_Of (Anon_Id, Loc));
10860
10861 Set_Defining_Unit_Name (Specification (Decl_Node),
10862 Make_Defining_Identifier (Loc, Chars (Formal_Sub)));
10863 end;
10864 end if;
10865
10866 return Decl_Node;
10867 end Instantiate_Formal_Subprogram;
10868
10869 ------------------------
10870 -- Instantiate_Object --
10871 ------------------------
10872
10873 function Instantiate_Object
10874 (Formal : Node_Id;
10875 Actual : Node_Id;
10876 Analyzed_Formal : Node_Id) return List_Id
10877 is
10878 Gen_Obj : constant Entity_Id := Defining_Identifier (Formal);
10879 A_Gen_Obj : constant Entity_Id :=
10880 Defining_Identifier (Analyzed_Formal);
10881 Acc_Def : Node_Id := Empty;
10882 Act_Assoc : constant Node_Id := Parent (Actual);
10883 Actual_Decl : Node_Id := Empty;
10884 Decl_Node : Node_Id;
10885 Def : Node_Id;
10886 Ftyp : Entity_Id;
10887 List : constant List_Id := New_List;
10888 Loc : constant Source_Ptr := Sloc (Actual);
10889 Orig_Ftyp : constant Entity_Id := Etype (A_Gen_Obj);
10890 Subt_Decl : Node_Id := Empty;
10891 Subt_Mark : Node_Id := Empty;
10892
10893 -- Start of processing for Instantiate_Object
10894
10895 begin
10896 -- Formal may be an anonymous access
10897
10898 if Present (Subtype_Mark (Formal)) then
10899 Subt_Mark := Subtype_Mark (Formal);
10900 else
10901 Check_Access_Definition (Formal);
10902 Acc_Def := Access_Definition (Formal);
10903 end if;
10904
10905 -- Sloc for error message on missing actual
10906
10907 Error_Msg_Sloc := Sloc (Scope (A_Gen_Obj));
10908
10909 if Get_Instance_Of (Gen_Obj) /= Gen_Obj then
10910 Error_Msg_N ("duplicate instantiation of generic parameter", Actual);
10911 end if;
10912
10913 Set_Parent (List, Parent (Actual));
10914
10915 -- OUT present
10916
10917 if Out_Present (Formal) then
10918
10919 -- An IN OUT generic actual must be a name. The instantiation is a
10920 -- renaming declaration. The actual is the name being renamed. We
10921 -- use the actual directly, rather than a copy, because it is not
10922 -- used further in the list of actuals, and because a copy or a use
10923 -- of relocate_node is incorrect if the instance is nested within a
10924 -- generic. In order to simplify e.g. ASIS queries, the
10925 -- Generic_Parent field links the declaration to the generic
10926 -- association.
10927
10928 if No (Actual) then
10929 Error_Msg_NE
10930 ("missing actual &",
10931 Instantiation_Node, Gen_Obj);
10932 Error_Msg_NE
10933 ("\in instantiation of & declared#",
10934 Instantiation_Node, Scope (A_Gen_Obj));
10935 Abandon_Instantiation (Instantiation_Node);
10936 end if;
10937
10938 if Present (Subt_Mark) then
10939 Decl_Node :=
10940 Make_Object_Renaming_Declaration (Loc,
10941 Defining_Identifier => New_Copy (Gen_Obj),
10942 Subtype_Mark => New_Copy_Tree (Subt_Mark),
10943 Name => Actual);
10944
10945 else pragma Assert (Present (Acc_Def));
10946 Decl_Node :=
10947 Make_Object_Renaming_Declaration (Loc,
10948 Defining_Identifier => New_Copy (Gen_Obj),
10949 Access_Definition => New_Copy_Tree (Acc_Def),
10950 Name => Actual);
10951 end if;
10952
10953 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
10954
10955 -- The analysis of the actual may produce Insert_Action nodes, so
10956 -- the declaration must have a context in which to attach them.
10957
10958 Append (Decl_Node, List);
10959 Analyze (Actual);
10960
10961 -- Return if the analysis of the actual reported some error
10962
10963 if Etype (Actual) = Any_Type then
10964 return List;
10965 end if;
10966
10967 -- This check is performed here because Analyze_Object_Renaming will
10968 -- not check it when Comes_From_Source is False. Note though that the
10969 -- check for the actual being the name of an object will be performed
10970 -- in Analyze_Object_Renaming.
10971
10972 if Is_Object_Reference (Actual)
10973 and then Is_Dependent_Component_Of_Mutable_Object (Actual)
10974 then
10975 Error_Msg_N
10976 ("illegal discriminant-dependent component for in out parameter",
10977 Actual);
10978 end if;
10979
10980 -- The actual has to be resolved in order to check that it is a
10981 -- variable (due to cases such as F (1), where F returns access to
10982 -- an array, and for overloaded prefixes).
10983
10984 Ftyp := Get_Instance_Of (Etype (A_Gen_Obj));
10985
10986 -- If the type of the formal is not itself a formal, and the current
10987 -- unit is a child unit, the formal type must be declared in a
10988 -- parent, and must be retrieved by visibility.
10989
10990 if Ftyp = Orig_Ftyp
10991 and then Is_Generic_Unit (Scope (Ftyp))
10992 and then Is_Child_Unit (Scope (A_Gen_Obj))
10993 then
10994 declare
10995 Temp : constant Node_Id :=
10996 New_Copy_Tree (Subtype_Mark (Analyzed_Formal));
10997 begin
10998 Set_Entity (Temp, Empty);
10999 Find_Type (Temp);
11000 Ftyp := Entity (Temp);
11001 end;
11002 end if;
11003
11004 if Is_Private_Type (Ftyp)
11005 and then not Is_Private_Type (Etype (Actual))
11006 and then (Base_Type (Full_View (Ftyp)) = Base_Type (Etype (Actual))
11007 or else Base_Type (Etype (Actual)) = Ftyp)
11008 then
11009 -- If the actual has the type of the full view of the formal, or
11010 -- else a non-private subtype of the formal, then the visibility
11011 -- of the formal type has changed. Add to the actuals a subtype
11012 -- declaration that will force the exchange of views in the body
11013 -- of the instance as well.
11014
11015 Subt_Decl :=
11016 Make_Subtype_Declaration (Loc,
11017 Defining_Identifier => Make_Temporary (Loc, 'P'),
11018 Subtype_Indication => New_Occurrence_Of (Ftyp, Loc));
11019
11020 Prepend (Subt_Decl, List);
11021
11022 Prepend_Elmt (Full_View (Ftyp), Exchanged_Views);
11023 Exchange_Declarations (Ftyp);
11024 end if;
11025
11026 Resolve (Actual, Ftyp);
11027
11028 if not Denotes_Variable (Actual) then
11029 Error_Msg_NE ("actual for& must be a variable", Actual, Gen_Obj);
11030
11031 elsif Base_Type (Ftyp) /= Base_Type (Etype (Actual)) then
11032
11033 -- Ada 2005 (AI-423): For a generic formal object of mode in out,
11034 -- the type of the actual shall resolve to a specific anonymous
11035 -- access type.
11036
11037 if Ada_Version < Ada_2005
11038 or else Ekind (Base_Type (Ftyp)) /=
11039 E_Anonymous_Access_Type
11040 or else Ekind (Base_Type (Etype (Actual))) /=
11041 E_Anonymous_Access_Type
11042 then
11043 Error_Msg_NE
11044 ("type of actual does not match type of&", Actual, Gen_Obj);
11045 end if;
11046 end if;
11047
11048 Note_Possible_Modification (Actual, Sure => True);
11049
11050 -- Check for instantiation with atomic/volatile object actual for
11051 -- nonatomic/nonvolatile formal (RM C.6 (12)).
11052
11053 if Is_Atomic_Object (Actual) and then not Is_Atomic (Orig_Ftyp) then
11054 Error_Msg_NE
11055 ("cannot instantiate nonatomic formal & of mode in out",
11056 Actual, Gen_Obj);
11057 Error_Msg_N ("\with atomic object actual (RM C.6(12))", Actual);
11058
11059 elsif Is_Volatile_Object (Actual) and then not Is_Volatile (Orig_Ftyp)
11060 then
11061 Error_Msg_NE
11062 ("cannot instantiate nonvolatile formal & of mode in out",
11063 Actual, Gen_Obj);
11064 Error_Msg_N ("\with volatile object actual (RM C.6(12))", Actual);
11065 end if;
11066
11067 -- Check for instantiation on nonatomic subcomponent of an atomic
11068 -- object in Ada 2020 (RM C.6 (13)).
11069
11070 if Ada_Version >= Ada_2020
11071 and then Is_Subcomponent_Of_Atomic_Object (Actual)
11072 and then not Is_Atomic_Object (Actual)
11073 then
11074 Error_Msg_NE
11075 ("cannot instantiate formal & of mode in out with actual",
11076 Actual, Gen_Obj);
11077 Error_Msg_N
11078 ("\nonatomic subcomponent of atomic object (RM C.6(13))",
11079 Actual);
11080 end if;
11081
11082 -- Formal in-parameter
11083
11084 else
11085 -- The instantiation of a generic formal in-parameter is constant
11086 -- declaration. The actual is the expression for that declaration.
11087 -- Its type is a full copy of the type of the formal. This may be
11088 -- an access to subprogram, for which we need to generate entities
11089 -- for the formals in the new signature.
11090
11091 if Present (Actual) then
11092 if Present (Subt_Mark) then
11093 Def := New_Copy_Tree (Subt_Mark);
11094 else
11095 pragma Assert (Present (Acc_Def));
11096 Def := New_Copy_Tree (Acc_Def);
11097 end if;
11098
11099 Decl_Node :=
11100 Make_Object_Declaration (Loc,
11101 Defining_Identifier => New_Copy (Gen_Obj),
11102 Constant_Present => True,
11103 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
11104 Object_Definition => Def,
11105 Expression => Actual);
11106
11107 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
11108
11109 -- A generic formal object of a tagged type is defined to be
11110 -- aliased so the new constant must also be treated as aliased.
11111
11112 if Is_Tagged_Type (Etype (A_Gen_Obj)) then
11113 Set_Aliased_Present (Decl_Node);
11114 end if;
11115
11116 Append (Decl_Node, List);
11117
11118 -- No need to repeat (pre-)analysis of some expression nodes
11119 -- already handled in Preanalyze_Actuals.
11120
11121 if Nkind (Actual) /= N_Allocator then
11122 Analyze (Actual);
11123
11124 -- Return if the analysis of the actual reported some error
11125
11126 if Etype (Actual) = Any_Type then
11127 return List;
11128 end if;
11129 end if;
11130
11131 declare
11132 Formal_Type : constant Entity_Id := Etype (A_Gen_Obj);
11133 Typ : Entity_Id;
11134
11135 begin
11136 Typ := Get_Instance_Of (Formal_Type);
11137
11138 -- If the actual appears in the current or an enclosing scope,
11139 -- use its type directly. This is relevant if it has an actual
11140 -- subtype that is distinct from its nominal one. This cannot
11141 -- be done in general because the type of the actual may
11142 -- depend on other actuals, and only be fully determined when
11143 -- the enclosing instance is analyzed.
11144
11145 if Present (Etype (Actual))
11146 and then Is_Constr_Subt_For_U_Nominal (Etype (Actual))
11147 then
11148 Freeze_Before (Instantiation_Node, Etype (Actual));
11149 else
11150 Freeze_Before (Instantiation_Node, Typ);
11151 end if;
11152
11153 -- If the actual is an aggregate, perform name resolution on
11154 -- its components (the analysis of an aggregate does not do it)
11155 -- to capture local names that may be hidden if the generic is
11156 -- a child unit.
11157
11158 if Nkind (Actual) = N_Aggregate then
11159 Preanalyze_And_Resolve (Actual, Typ);
11160 end if;
11161
11162 if Is_Limited_Type (Typ)
11163 and then not OK_For_Limited_Init (Typ, Actual)
11164 then
11165 Error_Msg_N
11166 ("initialization not allowed for limited types", Actual);
11167 Explain_Limited_Type (Typ, Actual);
11168 end if;
11169 end;
11170
11171 elsif Present (Default_Expression (Formal)) then
11172
11173 -- Use default to construct declaration
11174
11175 if Present (Subt_Mark) then
11176 Def := New_Copy (Subt_Mark);
11177 else
11178 pragma Assert (Present (Acc_Def));
11179 Def := New_Copy_Tree (Acc_Def);
11180 end if;
11181
11182 Decl_Node :=
11183 Make_Object_Declaration (Sloc (Formal),
11184 Defining_Identifier => New_Copy (Gen_Obj),
11185 Constant_Present => True,
11186 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
11187 Object_Definition => Def,
11188 Expression => New_Copy_Tree
11189 (Default_Expression (Formal)));
11190
11191 Set_Corresponding_Generic_Association
11192 (Decl_Node, Expression (Decl_Node));
11193
11194 Append (Decl_Node, List);
11195 Set_Analyzed (Expression (Decl_Node), False);
11196
11197 else
11198 Error_Msg_NE ("missing actual&", Instantiation_Node, Gen_Obj);
11199 Error_Msg_NE ("\in instantiation of & declared#",
11200 Instantiation_Node, Scope (A_Gen_Obj));
11201
11202 if Is_Scalar_Type (Etype (A_Gen_Obj)) then
11203
11204 -- Create dummy constant declaration so that instance can be
11205 -- analyzed, to minimize cascaded visibility errors.
11206
11207 if Present (Subt_Mark) then
11208 Def := Subt_Mark;
11209 else pragma Assert (Present (Acc_Def));
11210 Def := Acc_Def;
11211 end if;
11212
11213 Decl_Node :=
11214 Make_Object_Declaration (Loc,
11215 Defining_Identifier => New_Copy (Gen_Obj),
11216 Constant_Present => True,
11217 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
11218 Object_Definition => New_Copy (Def),
11219 Expression =>
11220 Make_Attribute_Reference (Sloc (Gen_Obj),
11221 Attribute_Name => Name_First,
11222 Prefix => New_Copy (Def)));
11223
11224 Append (Decl_Node, List);
11225
11226 else
11227 Abandon_Instantiation (Instantiation_Node);
11228 end if;
11229 end if;
11230 end if;
11231
11232 if Nkind (Actual) in N_Has_Entity then
11233 Actual_Decl := Parent (Entity (Actual));
11234 end if;
11235
11236 -- Ada 2005 (AI-423): For a formal object declaration with a null
11237 -- exclusion or an access definition that has a null exclusion: If the
11238 -- actual matching the formal object declaration denotes a generic
11239 -- formal object of another generic unit G, and the instantiation
11240 -- containing the actual occurs within the body of G or within the body
11241 -- of a generic unit declared within the declarative region of G, then
11242 -- the declaration of the formal object of G must have a null exclusion.
11243 -- Otherwise, the subtype of the actual matching the formal object
11244 -- declaration shall exclude null.
11245
11246 if Ada_Version >= Ada_2005
11247 and then Present (Actual_Decl)
11248 and then Nkind_In (Actual_Decl, N_Formal_Object_Declaration,
11249 N_Object_Declaration)
11250 and then Nkind (Analyzed_Formal) = N_Formal_Object_Declaration
11251 and then not Has_Null_Exclusion (Actual_Decl)
11252 and then Has_Null_Exclusion (Analyzed_Formal)
11253 then
11254 Error_Msg_Sloc := Sloc (Analyzed_Formal);
11255 Error_Msg_N
11256 ("actual must exclude null to match generic formal#", Actual);
11257 end if;
11258
11259 -- An effectively volatile object cannot be used as an actual in a
11260 -- generic instantiation (SPARK RM 7.1.3(7)). The following check is
11261 -- relevant only when SPARK_Mode is on as it is not a standard Ada
11262 -- legality rule, and also verifies that the actual is an object.
11263
11264 if SPARK_Mode = On
11265 and then Present (Actual)
11266 and then Is_Object_Reference (Actual)
11267 and then Is_Effectively_Volatile_Object (Actual)
11268 then
11269 Error_Msg_N
11270 ("volatile object cannot act as actual in generic instantiation",
11271 Actual);
11272 end if;
11273
11274 return List;
11275 end Instantiate_Object;
11276
11277 ------------------------------
11278 -- Instantiate_Package_Body --
11279 ------------------------------
11280
11281 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
11282 -- must be replaced by gotos which jump to the end of the routine in order
11283 -- to restore the Ghost and SPARK modes.
11284
11285 procedure Instantiate_Package_Body
11286 (Body_Info : Pending_Body_Info;
11287 Inlined_Body : Boolean := False;
11288 Body_Optional : Boolean := False)
11289 is
11290 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
11291 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Decl);
11292 Act_Spec : constant Node_Id := Specification (Act_Decl);
11293 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
11294 Gen_Id : constant Node_Id := Name (Inst_Node);
11295 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
11296 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
11297 Loc : constant Source_Ptr := Sloc (Inst_Node);
11298
11299 procedure Check_Initialized_Types;
11300 -- In a generic package body, an entity of a generic private type may
11301 -- appear uninitialized. This is suspicious, unless the actual is a
11302 -- fully initialized type.
11303
11304 -----------------------------
11305 -- Check_Initialized_Types --
11306 -----------------------------
11307
11308 procedure Check_Initialized_Types is
11309 Decl : Node_Id;
11310 Formal : Entity_Id;
11311 Actual : Entity_Id;
11312 Uninit_Var : Entity_Id;
11313
11314 begin
11315 Decl := First (Generic_Formal_Declarations (Gen_Decl));
11316 while Present (Decl) loop
11317 Uninit_Var := Empty;
11318
11319 if Nkind (Decl) = N_Private_Extension_Declaration then
11320 Uninit_Var := Uninitialized_Variable (Decl);
11321
11322 elsif Nkind (Decl) = N_Formal_Type_Declaration
11323 and then Nkind (Formal_Type_Definition (Decl)) =
11324 N_Formal_Private_Type_Definition
11325 then
11326 Uninit_Var :=
11327 Uninitialized_Variable (Formal_Type_Definition (Decl));
11328 end if;
11329
11330 if Present (Uninit_Var) then
11331 Formal := Defining_Identifier (Decl);
11332 Actual := First_Entity (Act_Decl_Id);
11333
11334 -- For each formal there is a subtype declaration that renames
11335 -- the actual and has the same name as the formal. Locate the
11336 -- formal for warning message about uninitialized variables
11337 -- in the generic, for which the actual type should be a fully
11338 -- initialized type.
11339
11340 while Present (Actual) loop
11341 exit when Ekind (Actual) = E_Package
11342 and then Present (Renamed_Object (Actual));
11343
11344 if Chars (Actual) = Chars (Formal)
11345 and then not Is_Scalar_Type (Actual)
11346 and then not Is_Fully_Initialized_Type (Actual)
11347 and then Warn_On_No_Value_Assigned
11348 then
11349 Error_Msg_Node_2 := Formal;
11350 Error_Msg_NE
11351 ("generic unit has uninitialized variable& of "
11352 & "formal private type &?v?", Actual, Uninit_Var);
11353 Error_Msg_NE
11354 ("actual type for& should be fully initialized type?v?",
11355 Actual, Formal);
11356 exit;
11357 end if;
11358
11359 Next_Entity (Actual);
11360 end loop;
11361 end if;
11362
11363 Next (Decl);
11364 end loop;
11365 end Check_Initialized_Types;
11366
11367 -- Local variables
11368
11369 -- The following constants capture the context prior to instantiating
11370 -- the package body.
11371
11372 Saved_CS : constant Config_Switches_Type := Save_Config_Switches;
11373 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
11374 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
11375 Saved_ISMP : constant Boolean :=
11376 Ignore_SPARK_Mode_Pragmas_In_Instance;
11377 Saved_LSST : constant Suppress_Stack_Entry_Ptr :=
11378 Local_Suppress_Stack_Top;
11379 Saved_SC : constant Boolean := Style_Check;
11380 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
11381 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
11382 Saved_SS : constant Suppress_Record := Scope_Suppress;
11383 Saved_Warn : constant Warning_Record := Save_Warnings;
11384
11385 Act_Body : Node_Id;
11386 Act_Body_Id : Entity_Id;
11387 Act_Body_Name : Node_Id;
11388 Gen_Body : Node_Id;
11389 Gen_Body_Id : Node_Id;
11390 Par_Ent : Entity_Id := Empty;
11391 Par_Installed : Boolean := False;
11392 Par_Vis : Boolean := False;
11393
11394 Vis_Prims_List : Elist_Id := No_Elist;
11395 -- List of primitives made temporarily visible in the instantiation
11396 -- to match the visibility of the formal type.
11397
11398 -- Start of processing for Instantiate_Package_Body
11399
11400 begin
11401 Gen_Body_Id := Corresponding_Body (Gen_Decl);
11402
11403 -- The instance body may already have been processed, as the parent of
11404 -- another instance that is inlined (Load_Parent_Of_Generic).
11405
11406 if Present (Corresponding_Body (Instance_Spec (Inst_Node))) then
11407 return;
11408 end if;
11409
11410 -- The package being instantiated may be subject to pragma Ghost. Set
11411 -- the mode now to ensure that any nodes generated during instantiation
11412 -- are properly marked as Ghost.
11413
11414 Set_Ghost_Mode (Act_Decl_Id);
11415
11416 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
11417
11418 -- Re-establish the state of information on which checks are suppressed.
11419 -- This information was set in Body_Info at the point of instantiation,
11420 -- and now we restore it so that the instance is compiled using the
11421 -- check status at the instantiation (RM 11.5(7.2/2), AI95-00224-01).
11422
11423 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
11424 Scope_Suppress := Body_Info.Scope_Suppress;
11425
11426 Restore_Config_Switches (Body_Info.Config_Switches);
11427 Restore_Warnings (Body_Info.Warnings);
11428
11429 if No (Gen_Body_Id) then
11430
11431 -- Do not look for parent of generic body if none is required.
11432 -- This may happen when the routine is called as part of the
11433 -- Pending_Instantiations processing, when nested instances
11434 -- may precede the one generated from the main unit.
11435
11436 if not Unit_Requires_Body (Defining_Entity (Gen_Decl))
11437 and then Body_Optional
11438 then
11439 goto Leave;
11440 else
11441 Load_Parent_Of_Generic
11442 (Inst_Node, Specification (Gen_Decl), Body_Optional);
11443
11444 -- Surprisingly enough, loading the body of the parent can cause
11445 -- the body to be instantiated and the double instantiation needs
11446 -- to be prevented in order to avoid giving bogus semantic errors.
11447
11448 -- This case can occur because of the Collect_Previous_Instances
11449 -- machinery of Load_Parent_Of_Generic, which will instantiate
11450 -- bodies that are deemed to be ahead of the body of the parent
11451 -- in the compilation unit. But the relative position of these
11452 -- bodies is computed using the mere comparison of their Sloc.
11453
11454 -- Now suppose that you have two generic packages G and H, with
11455 -- G containing a mere instantiation of H:
11456
11457 -- generic
11458 -- package H is
11459
11460 -- generic
11461 -- package Nested_G is
11462 -- ...
11463 -- end Nested_G;
11464
11465 -- end H;
11466
11467 -- with H;
11468
11469 -- generic
11470 -- package G is
11471
11472 -- package My_H is new H;
11473
11474 -- end G;
11475
11476 -- and a third package Q instantiating G and Nested_G:
11477
11478 -- with G;
11479
11480 -- package Q is
11481
11482 -- package My_G is new G;
11483
11484 -- package My_Nested_G is new My_G.My_H.Nested_G;
11485
11486 -- end Q;
11487
11488 -- The body to be instantiated is that of My_Nested_G and its
11489 -- parent is the instance My_G.My_H. This latter instantiation
11490 -- is done when My_G is analyzed, i.e. after the declarations
11491 -- of My_G and My_Nested_G have been parsed; as a result, the
11492 -- Sloc of My_G.My_H is greater than the Sloc of My_Nested_G.
11493
11494 -- Therefore loading the body of My_G.My_H will cause the body
11495 -- of My_Nested_G to be instantiated because it is deemed to be
11496 -- ahead of My_G.My_H. This means that Load_Parent_Of_Generic
11497 -- will again be invoked on My_G.My_H, but this time with the
11498 -- Collect_Previous_Instances machinery disabled, so there is
11499 -- no endless mutual recursion and things are done in order.
11500
11501 if Present (Corresponding_Body (Instance_Spec (Inst_Node))) then
11502 goto Leave;
11503 end if;
11504
11505 Gen_Body_Id := Corresponding_Body (Gen_Decl);
11506 end if;
11507 end if;
11508
11509 -- Establish global variable for sloc adjustment and for error recovery
11510 -- In the case of an instance body for an instantiation with actuals
11511 -- from a limited view, the instance body is placed at the beginning
11512 -- of the enclosing package body: use the body entity as the source
11513 -- location for nodes of the instance body.
11514
11515 if not Is_Empty_Elmt_List (Incomplete_Actuals (Act_Decl_Id)) then
11516 declare
11517 Scop : constant Entity_Id := Scope (Act_Decl_Id);
11518 Body_Id : constant Node_Id :=
11519 Corresponding_Body (Unit_Declaration_Node (Scop));
11520
11521 begin
11522 Instantiation_Node := Body_Id;
11523 end;
11524 else
11525 Instantiation_Node := Inst_Node;
11526 end if;
11527
11528 if Present (Gen_Body_Id) then
11529 Save_Env (Gen_Unit, Act_Decl_Id);
11530 Style_Check := False;
11531
11532 -- If the context of the instance is subject to SPARK_Mode "off", the
11533 -- annotation is missing, or the body is instantiated at a later pass
11534 -- and its spec ignored SPARK_Mode pragma, set the global flag which
11535 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within the
11536 -- instance.
11537
11538 if SPARK_Mode /= On
11539 or else Ignore_SPARK_Mode_Pragmas (Act_Decl_Id)
11540 then
11541 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
11542 end if;
11543
11544 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
11545 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
11546
11547 Create_Instantiation_Source
11548 (Inst_Node, Gen_Body_Id, S_Adjustment);
11549
11550 Act_Body :=
11551 Copy_Generic_Node
11552 (Original_Node (Gen_Body), Empty, Instantiating => True);
11553
11554 -- Create proper (possibly qualified) defining name for the body, to
11555 -- correspond to the one in the spec.
11556
11557 Act_Body_Id :=
11558 Make_Defining_Identifier (Sloc (Act_Decl_Id), Chars (Act_Decl_Id));
11559 Set_Comes_From_Source (Act_Body_Id, Comes_From_Source (Act_Decl_Id));
11560
11561 -- Some attributes of spec entity are not inherited by body entity
11562
11563 Set_Handler_Records (Act_Body_Id, No_List);
11564
11565 if Nkind (Defining_Unit_Name (Act_Spec)) =
11566 N_Defining_Program_Unit_Name
11567 then
11568 Act_Body_Name :=
11569 Make_Defining_Program_Unit_Name (Loc,
11570 Name =>
11571 New_Copy_Tree (Name (Defining_Unit_Name (Act_Spec))),
11572 Defining_Identifier => Act_Body_Id);
11573 else
11574 Act_Body_Name := Act_Body_Id;
11575 end if;
11576
11577 Set_Defining_Unit_Name (Act_Body, Act_Body_Name);
11578
11579 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
11580 Check_Generic_Actuals (Act_Decl_Id, False);
11581 Check_Initialized_Types;
11582
11583 -- Install primitives hidden at the point of the instantiation but
11584 -- visible when processing the generic formals
11585
11586 declare
11587 E : Entity_Id;
11588
11589 begin
11590 E := First_Entity (Act_Decl_Id);
11591 while Present (E) loop
11592 if Is_Type (E)
11593 and then not Is_Itype (E)
11594 and then Is_Generic_Actual_Type (E)
11595 and then Is_Tagged_Type (E)
11596 then
11597 Install_Hidden_Primitives
11598 (Prims_List => Vis_Prims_List,
11599 Gen_T => Generic_Parent_Type (Parent (E)),
11600 Act_T => E);
11601 end if;
11602
11603 Next_Entity (E);
11604 end loop;
11605 end;
11606
11607 -- If it is a child unit, make the parent instance (which is an
11608 -- instance of the parent of the generic) visible. The parent
11609 -- instance is the prefix of the name of the generic unit.
11610
11611 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
11612 and then Nkind (Gen_Id) = N_Expanded_Name
11613 then
11614 Par_Ent := Entity (Prefix (Gen_Id));
11615 Par_Vis := Is_Immediately_Visible (Par_Ent);
11616 Install_Parent (Par_Ent, In_Body => True);
11617 Par_Installed := True;
11618
11619 elsif Is_Child_Unit (Gen_Unit) then
11620 Par_Ent := Scope (Gen_Unit);
11621 Par_Vis := Is_Immediately_Visible (Par_Ent);
11622 Install_Parent (Par_Ent, In_Body => True);
11623 Par_Installed := True;
11624 end if;
11625
11626 -- If the instantiation is a library unit, and this is the main unit,
11627 -- then build the resulting compilation unit nodes for the instance.
11628 -- If this is a compilation unit but it is not the main unit, then it
11629 -- is the body of a unit in the context, that is being compiled
11630 -- because it is encloses some inlined unit or another generic unit
11631 -- being instantiated. In that case, this body is not part of the
11632 -- current compilation, and is not attached to the tree, but its
11633 -- parent must be set for analysis.
11634
11635 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
11636
11637 -- Replace instance node with body of instance, and create new
11638 -- node for corresponding instance declaration.
11639
11640 Build_Instance_Compilation_Unit_Nodes
11641 (Inst_Node, Act_Body, Act_Decl);
11642 Analyze (Inst_Node);
11643
11644 if Parent (Inst_Node) = Cunit (Main_Unit) then
11645
11646 -- If the instance is a child unit itself, then set the scope
11647 -- of the expanded body to be the parent of the instantiation
11648 -- (ensuring that the fully qualified name will be generated
11649 -- for the elaboration subprogram).
11650
11651 if Nkind (Defining_Unit_Name (Act_Spec)) =
11652 N_Defining_Program_Unit_Name
11653 then
11654 Set_Scope (Defining_Entity (Inst_Node), Scope (Act_Decl_Id));
11655 end if;
11656 end if;
11657
11658 -- Case where instantiation is not a library unit
11659
11660 else
11661 -- If this is an early instantiation, i.e. appears textually
11662 -- before the corresponding body and must be elaborated first,
11663 -- indicate that the body instance is to be delayed.
11664
11665 Install_Body (Act_Body, Inst_Node, Gen_Body, Gen_Decl);
11666 Analyze (Act_Body);
11667 end if;
11668
11669 Inherit_Context (Gen_Body, Inst_Node);
11670
11671 -- Remove the parent instances if they have been placed on the scope
11672 -- stack to compile the body.
11673
11674 if Par_Installed then
11675 Remove_Parent (In_Body => True);
11676
11677 -- Restore the previous visibility of the parent
11678
11679 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
11680 end if;
11681
11682 Restore_Hidden_Primitives (Vis_Prims_List);
11683 Restore_Private_Views (Act_Decl_Id);
11684
11685 -- Remove the current unit from visibility if this is an instance
11686 -- that is not elaborated on the fly for inlining purposes.
11687
11688 if not Inlined_Body then
11689 Set_Is_Immediately_Visible (Act_Decl_Id, False);
11690 end if;
11691
11692 Restore_Env;
11693
11694 -- If we have no body, and the unit requires a body, then complain. This
11695 -- complaint is suppressed if we have detected other errors (since a
11696 -- common reason for missing the body is that it had errors).
11697 -- In CodePeer mode, a warning has been emitted already, no need for
11698 -- further messages.
11699
11700 elsif Unit_Requires_Body (Gen_Unit)
11701 and then not Body_Optional
11702 then
11703 if CodePeer_Mode then
11704 null;
11705
11706 elsif Serious_Errors_Detected = 0 then
11707 Error_Msg_NE
11708 ("cannot find body of generic package &", Inst_Node, Gen_Unit);
11709
11710 -- Don't attempt to perform any cleanup actions if some other error
11711 -- was already detected, since this can cause blowups.
11712
11713 else
11714 goto Leave;
11715 end if;
11716
11717 -- Case of package that does not need a body
11718
11719 else
11720 -- If the instantiation of the declaration is a library unit, rewrite
11721 -- the original package instantiation as a package declaration in the
11722 -- compilation unit node.
11723
11724 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
11725 Set_Parent_Spec (Act_Decl, Parent_Spec (Inst_Node));
11726 Rewrite (Inst_Node, Act_Decl);
11727
11728 -- Generate elaboration entity, in case spec has elaboration code.
11729 -- This cannot be done when the instance is analyzed, because it
11730 -- is not known yet whether the body exists.
11731
11732 Set_Elaboration_Entity_Required (Act_Decl_Id, False);
11733 Build_Elaboration_Entity (Parent (Inst_Node), Act_Decl_Id);
11734
11735 -- If the instantiation is not a library unit, then append the
11736 -- declaration to the list of implicitly generated entities, unless
11737 -- it is already a list member which means that it was already
11738 -- processed
11739
11740 elsif not Is_List_Member (Act_Decl) then
11741 Mark_Rewrite_Insertion (Act_Decl);
11742 Insert_Before (Inst_Node, Act_Decl);
11743 end if;
11744 end if;
11745
11746 <<Leave>>
11747
11748 -- Restore the context that was in effect prior to instantiating the
11749 -- package body.
11750
11751 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
11752 Local_Suppress_Stack_Top := Saved_LSST;
11753 Scope_Suppress := Saved_SS;
11754 Style_Check := Saved_SC;
11755
11756 Expander_Mode_Restore;
11757 Restore_Config_Switches (Saved_CS);
11758 Restore_Ghost_Region (Saved_GM, Saved_IGR);
11759 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
11760 Restore_Warnings (Saved_Warn);
11761 end Instantiate_Package_Body;
11762
11763 ---------------------------------
11764 -- Instantiate_Subprogram_Body --
11765 ---------------------------------
11766
11767 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
11768 -- must be replaced by gotos which jump to the end of the routine in order
11769 -- to restore the Ghost and SPARK modes.
11770
11771 procedure Instantiate_Subprogram_Body
11772 (Body_Info : Pending_Body_Info;
11773 Body_Optional : Boolean := False)
11774 is
11775 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
11776 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Decl);
11777 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
11778 Gen_Id : constant Node_Id := Name (Inst_Node);
11779 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
11780 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
11781 Loc : constant Source_Ptr := Sloc (Inst_Node);
11782 Pack_Id : constant Entity_Id :=
11783 Defining_Unit_Name (Parent (Act_Decl));
11784
11785 -- The following constants capture the context prior to instantiating
11786 -- the subprogram body.
11787
11788 Saved_CS : constant Config_Switches_Type := Save_Config_Switches;
11789 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
11790 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
11791 Saved_ISMP : constant Boolean :=
11792 Ignore_SPARK_Mode_Pragmas_In_Instance;
11793 Saved_LSST : constant Suppress_Stack_Entry_Ptr :=
11794 Local_Suppress_Stack_Top;
11795 Saved_SC : constant Boolean := Style_Check;
11796 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
11797 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
11798 Saved_SS : constant Suppress_Record := Scope_Suppress;
11799 Saved_Warn : constant Warning_Record := Save_Warnings;
11800
11801 Act_Body : Node_Id;
11802 Act_Body_Id : Entity_Id;
11803 Gen_Body : Node_Id;
11804 Gen_Body_Id : Node_Id;
11805 Pack_Body : Node_Id;
11806 Par_Ent : Entity_Id := Empty;
11807 Par_Installed : Boolean := False;
11808 Par_Vis : Boolean := False;
11809 Ret_Expr : Node_Id;
11810
11811 begin
11812 Gen_Body_Id := Corresponding_Body (Gen_Decl);
11813
11814 -- Subprogram body may have been created already because of an inline
11815 -- pragma, or because of multiple elaborations of the enclosing package
11816 -- when several instances of the subprogram appear in the main unit.
11817
11818 if Present (Corresponding_Body (Act_Decl)) then
11819 return;
11820 end if;
11821
11822 -- The subprogram being instantiated may be subject to pragma Ghost. Set
11823 -- the mode now to ensure that any nodes generated during instantiation
11824 -- are properly marked as Ghost.
11825
11826 Set_Ghost_Mode (Act_Decl_Id);
11827
11828 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
11829
11830 -- Re-establish the state of information on which checks are suppressed.
11831 -- This information was set in Body_Info at the point of instantiation,
11832 -- and now we restore it so that the instance is compiled using the
11833 -- check status at the instantiation (RM 11.5(7.2/2), AI95-00224-01).
11834
11835 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
11836 Scope_Suppress := Body_Info.Scope_Suppress;
11837
11838 Restore_Config_Switches (Body_Info.Config_Switches);
11839 Restore_Warnings (Body_Info.Warnings);
11840
11841 if No (Gen_Body_Id) then
11842
11843 -- For imported generic subprogram, no body to compile, complete
11844 -- the spec entity appropriately.
11845
11846 if Is_Imported (Gen_Unit) then
11847 Set_Is_Imported (Act_Decl_Id);
11848 Set_First_Rep_Item (Act_Decl_Id, First_Rep_Item (Gen_Unit));
11849 Set_Interface_Name (Act_Decl_Id, Interface_Name (Gen_Unit));
11850 Set_Convention (Act_Decl_Id, Convention (Gen_Unit));
11851 Set_Has_Completion (Act_Decl_Id);
11852 goto Leave;
11853
11854 -- For other cases, compile the body
11855
11856 else
11857 Load_Parent_Of_Generic
11858 (Inst_Node, Specification (Gen_Decl), Body_Optional);
11859 Gen_Body_Id := Corresponding_Body (Gen_Decl);
11860 end if;
11861 end if;
11862
11863 Instantiation_Node := Inst_Node;
11864
11865 if Present (Gen_Body_Id) then
11866 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
11867
11868 if Nkind (Gen_Body) = N_Subprogram_Body_Stub then
11869
11870 -- Either body is not present, or context is non-expanding, as
11871 -- when compiling a subunit. Mark the instance as completed, and
11872 -- diagnose a missing body when needed.
11873
11874 if Expander_Active
11875 and then Operating_Mode = Generate_Code
11876 then
11877 Error_Msg_N ("missing proper body for instantiation", Gen_Body);
11878 end if;
11879
11880 Set_Has_Completion (Act_Decl_Id);
11881 goto Leave;
11882 end if;
11883
11884 Save_Env (Gen_Unit, Act_Decl_Id);
11885 Style_Check := False;
11886
11887 -- If the context of the instance is subject to SPARK_Mode "off", the
11888 -- annotation is missing, or the body is instantiated at a later pass
11889 -- and its spec ignored SPARK_Mode pragma, set the global flag which
11890 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within the
11891 -- instance.
11892
11893 if SPARK_Mode /= On
11894 or else Ignore_SPARK_Mode_Pragmas (Act_Decl_Id)
11895 then
11896 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
11897 end if;
11898
11899 -- If the context of an instance is not subject to SPARK_Mode "off",
11900 -- and the generic body is subject to an explicit SPARK_Mode pragma,
11901 -- the latter should be the one applicable to the instance.
11902
11903 if not Ignore_SPARK_Mode_Pragmas_In_Instance
11904 and then SPARK_Mode /= Off
11905 and then Present (SPARK_Pragma (Gen_Body_Id))
11906 then
11907 Set_SPARK_Mode (Gen_Body_Id);
11908 end if;
11909
11910 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
11911 Create_Instantiation_Source
11912 (Inst_Node,
11913 Gen_Body_Id,
11914 S_Adjustment);
11915
11916 Act_Body :=
11917 Copy_Generic_Node
11918 (Original_Node (Gen_Body), Empty, Instantiating => True);
11919
11920 -- Create proper defining name for the body, to correspond to the one
11921 -- in the spec.
11922
11923 Act_Body_Id :=
11924 Make_Defining_Identifier (Sloc (Act_Decl_Id), Chars (Act_Decl_Id));
11925
11926 Set_Comes_From_Source (Act_Body_Id, Comes_From_Source (Act_Decl_Id));
11927 Set_Defining_Unit_Name (Specification (Act_Body), Act_Body_Id);
11928
11929 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
11930 Set_Has_Completion (Act_Decl_Id);
11931 Check_Generic_Actuals (Pack_Id, False);
11932
11933 -- Generate a reference to link the visible subprogram instance to
11934 -- the generic body, which for navigation purposes is the only
11935 -- available source for the instance.
11936
11937 Generate_Reference
11938 (Related_Instance (Pack_Id),
11939 Gen_Body_Id, 'b', Set_Ref => False, Force => True);
11940
11941 -- If it is a child unit, make the parent instance (which is an
11942 -- instance of the parent of the generic) visible. The parent
11943 -- instance is the prefix of the name of the generic unit.
11944
11945 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
11946 and then Nkind (Gen_Id) = N_Expanded_Name
11947 then
11948 Par_Ent := Entity (Prefix (Gen_Id));
11949 Par_Vis := Is_Immediately_Visible (Par_Ent);
11950 Install_Parent (Par_Ent, In_Body => True);
11951 Par_Installed := True;
11952
11953 elsif Is_Child_Unit (Gen_Unit) then
11954 Par_Ent := Scope (Gen_Unit);
11955 Par_Vis := Is_Immediately_Visible (Par_Ent);
11956 Install_Parent (Par_Ent, In_Body => True);
11957 Par_Installed := True;
11958 end if;
11959
11960 -- Subprogram body is placed in the body of wrapper package,
11961 -- whose spec contains the subprogram declaration as well as
11962 -- the renaming declarations for the generic parameters.
11963
11964 Pack_Body :=
11965 Make_Package_Body (Loc,
11966 Defining_Unit_Name => New_Copy (Pack_Id),
11967 Declarations => New_List (Act_Body));
11968
11969 Set_Corresponding_Spec (Pack_Body, Pack_Id);
11970
11971 -- If the instantiation is a library unit, then build resulting
11972 -- compilation unit nodes for the instance. The declaration of
11973 -- the enclosing package is the grandparent of the subprogram
11974 -- declaration. First replace the instantiation node as the unit
11975 -- of the corresponding compilation.
11976
11977 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
11978 if Parent (Inst_Node) = Cunit (Main_Unit) then
11979 Set_Unit (Parent (Inst_Node), Inst_Node);
11980 Build_Instance_Compilation_Unit_Nodes
11981 (Inst_Node, Pack_Body, Parent (Parent (Act_Decl)));
11982 Analyze (Inst_Node);
11983 else
11984 Set_Parent (Pack_Body, Parent (Inst_Node));
11985 Analyze (Pack_Body);
11986 end if;
11987
11988 else
11989 Insert_Before (Inst_Node, Pack_Body);
11990 Mark_Rewrite_Insertion (Pack_Body);
11991 Analyze (Pack_Body);
11992
11993 if Expander_Active then
11994 Freeze_Subprogram_Body (Inst_Node, Gen_Body, Pack_Id);
11995 end if;
11996 end if;
11997
11998 Inherit_Context (Gen_Body, Inst_Node);
11999
12000 Restore_Private_Views (Pack_Id, False);
12001
12002 if Par_Installed then
12003 Remove_Parent (In_Body => True);
12004
12005 -- Restore the previous visibility of the parent
12006
12007 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
12008 end if;
12009
12010 Restore_Env;
12011
12012 -- Body not found. Error was emitted already. If there were no previous
12013 -- errors, this may be an instance whose scope is a premature instance.
12014 -- In that case we must insure that the (legal) program does raise
12015 -- program error if executed. We generate a subprogram body for this
12016 -- purpose. See DEC ac30vso.
12017
12018 -- Should not reference proprietary DEC tests in comments ???
12019
12020 elsif Serious_Errors_Detected = 0
12021 and then Nkind (Parent (Inst_Node)) /= N_Compilation_Unit
12022 then
12023 if Body_Optional then
12024 goto Leave;
12025
12026 elsif Ekind (Act_Decl_Id) = E_Procedure then
12027 Act_Body :=
12028 Make_Subprogram_Body (Loc,
12029 Specification =>
12030 Make_Procedure_Specification (Loc,
12031 Defining_Unit_Name =>
12032 Make_Defining_Identifier (Loc, Chars (Act_Decl_Id)),
12033 Parameter_Specifications =>
12034 New_Copy_List
12035 (Parameter_Specifications (Parent (Act_Decl_Id)))),
12036
12037 Declarations => Empty_List,
12038 Handled_Statement_Sequence =>
12039 Make_Handled_Sequence_Of_Statements (Loc,
12040 Statements => New_List (
12041 Make_Raise_Program_Error (Loc,
12042 Reason => PE_Access_Before_Elaboration))));
12043
12044 else
12045 Ret_Expr :=
12046 Make_Raise_Program_Error (Loc,
12047 Reason => PE_Access_Before_Elaboration);
12048
12049 Set_Etype (Ret_Expr, (Etype (Act_Decl_Id)));
12050 Set_Analyzed (Ret_Expr);
12051
12052 Act_Body :=
12053 Make_Subprogram_Body (Loc,
12054 Specification =>
12055 Make_Function_Specification (Loc,
12056 Defining_Unit_Name =>
12057 Make_Defining_Identifier (Loc, Chars (Act_Decl_Id)),
12058 Parameter_Specifications =>
12059 New_Copy_List
12060 (Parameter_Specifications (Parent (Act_Decl_Id))),
12061 Result_Definition =>
12062 New_Occurrence_Of (Etype (Act_Decl_Id), Loc)),
12063
12064 Declarations => Empty_List,
12065 Handled_Statement_Sequence =>
12066 Make_Handled_Sequence_Of_Statements (Loc,
12067 Statements => New_List (
12068 Make_Simple_Return_Statement (Loc, Ret_Expr))));
12069 end if;
12070
12071 Pack_Body :=
12072 Make_Package_Body (Loc,
12073 Defining_Unit_Name => New_Copy (Pack_Id),
12074 Declarations => New_List (Act_Body));
12075
12076 Insert_After (Inst_Node, Pack_Body);
12077 Set_Corresponding_Spec (Pack_Body, Pack_Id);
12078 Analyze (Pack_Body);
12079 end if;
12080
12081 <<Leave>>
12082
12083 -- Restore the context that was in effect prior to instantiating the
12084 -- subprogram body.
12085
12086 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
12087 Local_Suppress_Stack_Top := Saved_LSST;
12088 Scope_Suppress := Saved_SS;
12089 Style_Check := Saved_SC;
12090
12091 Expander_Mode_Restore;
12092 Restore_Config_Switches (Saved_CS);
12093 Restore_Ghost_Region (Saved_GM, Saved_IGR);
12094 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
12095 Restore_Warnings (Saved_Warn);
12096 end Instantiate_Subprogram_Body;
12097
12098 ----------------------
12099 -- Instantiate_Type --
12100 ----------------------
12101
12102 function Instantiate_Type
12103 (Formal : Node_Id;
12104 Actual : Node_Id;
12105 Analyzed_Formal : Node_Id;
12106 Actual_Decls : List_Id) return List_Id
12107 is
12108 A_Gen_T : constant Entity_Id :=
12109 Defining_Identifier (Analyzed_Formal);
12110 Def : constant Node_Id := Formal_Type_Definition (Formal);
12111 Gen_T : constant Entity_Id := Defining_Identifier (Formal);
12112 Act_T : Entity_Id;
12113 Ancestor : Entity_Id := Empty;
12114 Decl_Node : Node_Id;
12115 Decl_Nodes : List_Id;
12116 Loc : Source_Ptr;
12117 Subt : Entity_Id;
12118
12119 procedure Check_Shared_Variable_Control_Aspects;
12120 -- Ada_2020: Verify that shared variable control aspects (RM C.6)
12121 -- that may be specified for a formal type are obeyed by the actual.
12122
12123 procedure Diagnose_Predicated_Actual;
12124 -- There are a number of constructs in which a discrete type with
12125 -- predicates is illegal, e.g. as an index in an array type declaration.
12126 -- If a generic type is used is such a construct in a generic package
12127 -- declaration, it carries the flag No_Predicate_On_Actual. it is part
12128 -- of the generic contract that the actual cannot have predicates.
12129
12130 procedure Validate_Array_Type_Instance;
12131 procedure Validate_Access_Subprogram_Instance;
12132 procedure Validate_Access_Type_Instance;
12133 procedure Validate_Derived_Type_Instance;
12134 procedure Validate_Derived_Interface_Type_Instance;
12135 procedure Validate_Discriminated_Formal_Type;
12136 procedure Validate_Interface_Type_Instance;
12137 procedure Validate_Private_Type_Instance;
12138 procedure Validate_Incomplete_Type_Instance;
12139 -- These procedures perform validation tests for the named case.
12140 -- Validate_Discriminated_Formal_Type is shared by formal private
12141 -- types and Ada 2012 formal incomplete types.
12142
12143 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean;
12144 -- Check that base types are the same and that the subtypes match
12145 -- statically. Used in several of the above.
12146
12147 --------------------------------------------
12148 -- Check_Shared_Variable_Control_Aspects --
12149 --------------------------------------------
12150
12151 -- Ada_2020: Verify that shared variable control aspects (RM C.6)
12152 -- that may be specified for the formal are obeyed by the actual.
12153
12154 procedure Check_Shared_Variable_Control_Aspects is
12155 begin
12156 if Ada_Version >= Ada_2020 then
12157 if Is_Atomic (A_Gen_T) and then not Is_Atomic (Act_T) then
12158 Error_Msg_NE
12159 ("actual for& must be an atomic type", Actual, A_Gen_T);
12160 end if;
12161
12162 if Is_Volatile (A_Gen_T) and then not Is_Volatile (Act_T) then
12163 Error_Msg_NE
12164 ("actual for& must be a Volatile type", Actual, A_Gen_T);
12165 end if;
12166
12167 if
12168 Is_Independent (A_Gen_T) and then not Is_Independent (Act_T)
12169 then
12170 Error_Msg_NE
12171 ("actual for& must be an Independent type", Actual, A_Gen_T);
12172 end if;
12173
12174 -- We assume that an array type whose atomic component type
12175 -- is Atomic is equivalent to an array type with the explicit
12176 -- aspect Has_Atomic_Components. This is a reasonable inference
12177 -- from the intent of AI12-0282, and makes it legal to use an
12178 -- actual that does not have the identical aspect as the formal.
12179
12180 if Has_Atomic_Components (A_Gen_T)
12181 and then not Has_Atomic_Components (Act_T)
12182 then
12183 if Is_Array_Type (Act_T)
12184 and then Is_Atomic (Component_Type (Act_T))
12185 then
12186 null;
12187
12188 else
12189 Error_Msg_NE
12190 ("actual for& must have atomic components",
12191 Actual, A_Gen_T);
12192 end if;
12193 end if;
12194
12195 if Has_Independent_Components (A_Gen_T)
12196 and then not Has_Independent_Components (Act_T)
12197 then
12198 Error_Msg_NE
12199 ("actual for& must have independent components",
12200 Actual, A_Gen_T);
12201 end if;
12202
12203 if Has_Volatile_Components (A_Gen_T)
12204 and then not Has_Volatile_Components (Act_T)
12205 then
12206 if Is_Array_Type (Act_T)
12207 and then Is_Volatile (Component_Type (Act_T))
12208 then
12209 null;
12210
12211 else
12212 Error_Msg_NE
12213 ("actual for& must have volatile components",
12214 Actual, A_Gen_T);
12215 end if;
12216 end if;
12217 end if;
12218 end Check_Shared_Variable_Control_Aspects;
12219
12220 ---------------------------------
12221 -- Diagnose_Predicated_Actual --
12222 ---------------------------------
12223
12224 procedure Diagnose_Predicated_Actual is
12225 begin
12226 if No_Predicate_On_Actual (A_Gen_T)
12227 and then Has_Predicates (Act_T)
12228 then
12229 Error_Msg_NE
12230 ("actual for& cannot be a type with predicate",
12231 Instantiation_Node, A_Gen_T);
12232
12233 elsif No_Dynamic_Predicate_On_Actual (A_Gen_T)
12234 and then Has_Predicates (Act_T)
12235 and then not Has_Static_Predicate_Aspect (Act_T)
12236 then
12237 Error_Msg_NE
12238 ("actual for& cannot be a type with a dynamic predicate",
12239 Instantiation_Node, A_Gen_T);
12240 end if;
12241 end Diagnose_Predicated_Actual;
12242
12243 --------------------
12244 -- Subtypes_Match --
12245 --------------------
12246
12247 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean is
12248 T : constant Entity_Id := Get_Instance_Of (Gen_T);
12249
12250 begin
12251 -- Some detailed comments would be useful here ???
12252
12253 return ((Base_Type (T) = Act_T
12254 or else Base_Type (T) = Base_Type (Act_T))
12255 and then Subtypes_Statically_Match (T, Act_T))
12256
12257 or else (Is_Class_Wide_Type (Gen_T)
12258 and then Is_Class_Wide_Type (Act_T)
12259 and then Subtypes_Match
12260 (Get_Instance_Of (Root_Type (Gen_T)),
12261 Root_Type (Act_T)))
12262
12263 or else
12264 (Ekind_In (Gen_T, E_Anonymous_Access_Subprogram_Type,
12265 E_Anonymous_Access_Type)
12266 and then Ekind (Act_T) = Ekind (Gen_T)
12267 and then Subtypes_Statically_Match
12268 (Designated_Type (Gen_T), Designated_Type (Act_T)));
12269 end Subtypes_Match;
12270
12271 -----------------------------------------
12272 -- Validate_Access_Subprogram_Instance --
12273 -----------------------------------------
12274
12275 procedure Validate_Access_Subprogram_Instance is
12276 begin
12277 if not Is_Access_Type (Act_T)
12278 or else Ekind (Designated_Type (Act_T)) /= E_Subprogram_Type
12279 then
12280 Error_Msg_NE
12281 ("expect access type in instantiation of &", Actual, Gen_T);
12282 Abandon_Instantiation (Actual);
12283 end if;
12284
12285 -- According to AI05-288, actuals for access_to_subprograms must be
12286 -- subtype conformant with the generic formal. Previous to AI05-288
12287 -- only mode conformance was required.
12288
12289 -- This is a binding interpretation that applies to previous versions
12290 -- of the language, no need to maintain previous weaker checks.
12291
12292 Check_Subtype_Conformant
12293 (Designated_Type (Act_T),
12294 Designated_Type (A_Gen_T),
12295 Actual,
12296 Get_Inst => True);
12297
12298 if Ekind (Base_Type (Act_T)) = E_Access_Protected_Subprogram_Type then
12299 if Ekind (A_Gen_T) = E_Access_Subprogram_Type then
12300 Error_Msg_NE
12301 ("protected access type not allowed for formal &",
12302 Actual, Gen_T);
12303 end if;
12304
12305 elsif Ekind (A_Gen_T) = E_Access_Protected_Subprogram_Type then
12306 Error_Msg_NE
12307 ("expect protected access type for formal &",
12308 Actual, Gen_T);
12309 end if;
12310
12311 -- If the formal has a specified convention (which in most cases
12312 -- will be StdCall) verify that the actual has the same convention.
12313
12314 if Has_Convention_Pragma (A_Gen_T)
12315 and then Convention (A_Gen_T) /= Convention (Act_T)
12316 then
12317 Error_Msg_Name_1 := Get_Convention_Name (Convention (A_Gen_T));
12318 Error_Msg_NE
12319 ("actual for formal & must have convention %", Actual, Gen_T);
12320 end if;
12321
12322 if Can_Never_Be_Null (A_Gen_T) /= Can_Never_Be_Null (Act_T) then
12323 Error_Msg_NE
12324 ("non null exclusion of actual and formal & do not match",
12325 Actual, Gen_T);
12326 end if;
12327 end Validate_Access_Subprogram_Instance;
12328
12329 -----------------------------------
12330 -- Validate_Access_Type_Instance --
12331 -----------------------------------
12332
12333 procedure Validate_Access_Type_Instance is
12334 Desig_Type : constant Entity_Id :=
12335 Find_Actual_Type (Designated_Type (A_Gen_T), A_Gen_T);
12336 Desig_Act : Entity_Id;
12337
12338 begin
12339 if not Is_Access_Type (Act_T) then
12340 Error_Msg_NE
12341 ("expect access type in instantiation of &", Actual, Gen_T);
12342 Abandon_Instantiation (Actual);
12343 end if;
12344
12345 if Is_Access_Constant (A_Gen_T) then
12346 if not Is_Access_Constant (Act_T) then
12347 Error_Msg_N
12348 ("actual type must be access-to-constant type", Actual);
12349 Abandon_Instantiation (Actual);
12350 end if;
12351 else
12352 if Is_Access_Constant (Act_T) then
12353 Error_Msg_N
12354 ("actual type must be access-to-variable type", Actual);
12355 Abandon_Instantiation (Actual);
12356
12357 elsif Ekind (A_Gen_T) = E_General_Access_Type
12358 and then Ekind (Base_Type (Act_T)) /= E_General_Access_Type
12359 then
12360 Error_Msg_N -- CODEFIX
12361 ("actual must be general access type!", Actual);
12362 Error_Msg_NE -- CODEFIX
12363 ("add ALL to }!", Actual, Act_T);
12364 Abandon_Instantiation (Actual);
12365 end if;
12366 end if;
12367
12368 -- The designated subtypes, that is to say the subtypes introduced
12369 -- by an access type declaration (and not by a subtype declaration)
12370 -- must match.
12371
12372 Desig_Act := Designated_Type (Base_Type (Act_T));
12373
12374 -- The designated type may have been introduced through a limited_
12375 -- with clause, in which case retrieve the non-limited view. This
12376 -- applies to incomplete types as well as to class-wide types.
12377
12378 if From_Limited_With (Desig_Act) then
12379 Desig_Act := Available_View (Desig_Act);
12380 end if;
12381
12382 if not Subtypes_Match (Desig_Type, Desig_Act) then
12383 Error_Msg_NE
12384 ("designated type of actual does not match that of formal &",
12385 Actual, Gen_T);
12386
12387 if not Predicates_Match (Desig_Type, Desig_Act) then
12388 Error_Msg_N ("\predicates do not match", Actual);
12389 end if;
12390
12391 Abandon_Instantiation (Actual);
12392
12393 elsif Is_Access_Type (Designated_Type (Act_T))
12394 and then Is_Constrained (Designated_Type (Designated_Type (Act_T)))
12395 /=
12396 Is_Constrained (Designated_Type (Desig_Type))
12397 then
12398 Error_Msg_NE
12399 ("designated type of actual does not match that of formal &",
12400 Actual, Gen_T);
12401
12402 if not Predicates_Match (Desig_Type, Desig_Act) then
12403 Error_Msg_N ("\predicates do not match", Actual);
12404 end if;
12405
12406 Abandon_Instantiation (Actual);
12407 end if;
12408
12409 -- Ada 2005: null-exclusion indicators of the two types must agree
12410
12411 if Can_Never_Be_Null (A_Gen_T) /= Can_Never_Be_Null (Act_T) then
12412 Error_Msg_NE
12413 ("non null exclusion of actual and formal & do not match",
12414 Actual, Gen_T);
12415 end if;
12416 end Validate_Access_Type_Instance;
12417
12418 ----------------------------------
12419 -- Validate_Array_Type_Instance --
12420 ----------------------------------
12421
12422 procedure Validate_Array_Type_Instance is
12423 I1 : Node_Id;
12424 I2 : Node_Id;
12425 T2 : Entity_Id;
12426
12427 function Formal_Dimensions return Nat;
12428 -- Count number of dimensions in array type formal
12429
12430 -----------------------
12431 -- Formal_Dimensions --
12432 -----------------------
12433
12434 function Formal_Dimensions return Nat is
12435 Num : Nat := 0;
12436 Index : Node_Id;
12437
12438 begin
12439 if Nkind (Def) = N_Constrained_Array_Definition then
12440 Index := First (Discrete_Subtype_Definitions (Def));
12441 else
12442 Index := First (Subtype_Marks (Def));
12443 end if;
12444
12445 while Present (Index) loop
12446 Num := Num + 1;
12447 Next_Index (Index);
12448 end loop;
12449
12450 return Num;
12451 end Formal_Dimensions;
12452
12453 -- Start of processing for Validate_Array_Type_Instance
12454
12455 begin
12456 if not Is_Array_Type (Act_T) then
12457 Error_Msg_NE
12458 ("expect array type in instantiation of &", Actual, Gen_T);
12459 Abandon_Instantiation (Actual);
12460
12461 elsif Nkind (Def) = N_Constrained_Array_Definition then
12462 if not (Is_Constrained (Act_T)) then
12463 Error_Msg_NE
12464 ("expect constrained array in instantiation of &",
12465 Actual, Gen_T);
12466 Abandon_Instantiation (Actual);
12467 end if;
12468
12469 else
12470 if Is_Constrained (Act_T) then
12471 Error_Msg_NE
12472 ("expect unconstrained array in instantiation of &",
12473 Actual, Gen_T);
12474 Abandon_Instantiation (Actual);
12475 end if;
12476 end if;
12477
12478 if Formal_Dimensions /= Number_Dimensions (Act_T) then
12479 Error_Msg_NE
12480 ("dimensions of actual do not match formal &", Actual, Gen_T);
12481 Abandon_Instantiation (Actual);
12482 end if;
12483
12484 I1 := First_Index (A_Gen_T);
12485 I2 := First_Index (Act_T);
12486 for J in 1 .. Formal_Dimensions loop
12487
12488 -- If the indexes of the actual were given by a subtype_mark,
12489 -- the index was transformed into a range attribute. Retrieve
12490 -- the original type mark for checking.
12491
12492 if Is_Entity_Name (Original_Node (I2)) then
12493 T2 := Entity (Original_Node (I2));
12494 else
12495 T2 := Etype (I2);
12496 end if;
12497
12498 if not Subtypes_Match
12499 (Find_Actual_Type (Etype (I1), A_Gen_T), T2)
12500 then
12501 Error_Msg_NE
12502 ("index types of actual do not match those of formal &",
12503 Actual, Gen_T);
12504 Abandon_Instantiation (Actual);
12505 end if;
12506
12507 Next_Index (I1);
12508 Next_Index (I2);
12509 end loop;
12510
12511 -- Check matching subtypes. Note that there are complex visibility
12512 -- issues when the generic is a child unit and some aspect of the
12513 -- generic type is declared in a parent unit of the generic. We do
12514 -- the test to handle this special case only after a direct check
12515 -- for static matching has failed. The case where both the component
12516 -- type and the array type are separate formals, and the component
12517 -- type is a private view may also require special checking in
12518 -- Subtypes_Match. Finally, we assume that a child instance where
12519 -- the component type comes from a formal of a parent instance is
12520 -- correct because the generic was correct. A more precise check
12521 -- seems too complex to install???
12522
12523 if Subtypes_Match
12524 (Component_Type (A_Gen_T), Component_Type (Act_T))
12525 or else
12526 Subtypes_Match
12527 (Find_Actual_Type (Component_Type (A_Gen_T), A_Gen_T),
12528 Component_Type (Act_T))
12529 or else
12530 (not Inside_A_Generic
12531 and then Is_Child_Unit (Scope (Component_Type (A_Gen_T))))
12532 then
12533 null;
12534 else
12535 Error_Msg_NE
12536 ("component subtype of actual does not match that of formal &",
12537 Actual, Gen_T);
12538 Abandon_Instantiation (Actual);
12539 end if;
12540
12541 if Has_Aliased_Components (A_Gen_T)
12542 and then not Has_Aliased_Components (Act_T)
12543 then
12544 Error_Msg_NE
12545 ("actual must have aliased components to match formal type &",
12546 Actual, Gen_T);
12547 end if;
12548 end Validate_Array_Type_Instance;
12549
12550 -----------------------------------------------
12551 -- Validate_Derived_Interface_Type_Instance --
12552 -----------------------------------------------
12553
12554 procedure Validate_Derived_Interface_Type_Instance is
12555 Par : constant Entity_Id := Entity (Subtype_Indication (Def));
12556 Elmt : Elmt_Id;
12557
12558 begin
12559 -- First apply interface instance checks
12560
12561 Validate_Interface_Type_Instance;
12562
12563 -- Verify that immediate parent interface is an ancestor of
12564 -- the actual.
12565
12566 if Present (Par)
12567 and then not Interface_Present_In_Ancestor (Act_T, Par)
12568 then
12569 Error_Msg_NE
12570 ("interface actual must include progenitor&", Actual, Par);
12571 end if;
12572
12573 -- Now verify that the actual includes all other ancestors of
12574 -- the formal.
12575
12576 Elmt := First_Elmt (Interfaces (A_Gen_T));
12577 while Present (Elmt) loop
12578 if not Interface_Present_In_Ancestor
12579 (Act_T, Get_Instance_Of (Node (Elmt)))
12580 then
12581 Error_Msg_NE
12582 ("interface actual must include progenitor&",
12583 Actual, Node (Elmt));
12584 end if;
12585
12586 Next_Elmt (Elmt);
12587 end loop;
12588 end Validate_Derived_Interface_Type_Instance;
12589
12590 ------------------------------------
12591 -- Validate_Derived_Type_Instance --
12592 ------------------------------------
12593
12594 procedure Validate_Derived_Type_Instance is
12595 Actual_Discr : Entity_Id;
12596 Ancestor_Discr : Entity_Id;
12597
12598 begin
12599 -- Verify that the actual includes the progenitors of the formal,
12600 -- if any. The formal may depend on previous formals and their
12601 -- instance, so we must examine instance of interfaces if present.
12602 -- The actual may be an extension of an interface, in which case
12603 -- it does not appear in the interface list, so this must be
12604 -- checked separately.
12605
12606 if Present (Interface_List (Def)) then
12607 if not Has_Interfaces (Act_T) then
12608 Error_Msg_NE
12609 ("actual must implement all interfaces of formal&",
12610 Actual, A_Gen_T);
12611
12612 else
12613 declare
12614 Act_Iface_List : Elist_Id;
12615 Iface : Node_Id;
12616 Iface_Ent : Entity_Id;
12617
12618 function Instance_Exists (I : Entity_Id) return Boolean;
12619 -- If the interface entity is declared in a generic unit,
12620 -- this can only be legal if we are within an instantiation
12621 -- of a child of that generic. There is currently no
12622 -- mechanism to relate an interface declared within a
12623 -- generic to the corresponding interface in an instance,
12624 -- so we traverse the list of interfaces of the actual,
12625 -- looking for a name match.
12626
12627 ---------------------
12628 -- Instance_Exists --
12629 ---------------------
12630
12631 function Instance_Exists (I : Entity_Id) return Boolean is
12632 Iface_Elmt : Elmt_Id;
12633
12634 begin
12635 Iface_Elmt := First_Elmt (Act_Iface_List);
12636 while Present (Iface_Elmt) loop
12637 if Is_Generic_Instance (Scope (Node (Iface_Elmt)))
12638 and then Chars (Node (Iface_Elmt)) = Chars (I)
12639 then
12640 return True;
12641 end if;
12642
12643 Next_Elmt (Iface_Elmt);
12644 end loop;
12645
12646 return False;
12647 end Instance_Exists;
12648
12649 begin
12650 Iface := First (Abstract_Interface_List (A_Gen_T));
12651 Collect_Interfaces (Act_T, Act_Iface_List);
12652
12653 while Present (Iface) loop
12654 Iface_Ent := Get_Instance_Of (Entity (Iface));
12655
12656 if Is_Ancestor (Iface_Ent, Act_T)
12657 or else Is_Progenitor (Iface_Ent, Act_T)
12658 then
12659 null;
12660
12661 elsif Ekind (Scope (Iface_Ent)) = E_Generic_Package
12662 and then Instance_Exists (Iface_Ent)
12663 then
12664 null;
12665
12666 else
12667 Error_Msg_Name_1 := Chars (Act_T);
12668 Error_Msg_NE
12669 ("Actual% must implement interface&",
12670 Actual, Etype (Iface));
12671 end if;
12672
12673 Next (Iface);
12674 end loop;
12675 end;
12676 end if;
12677 end if;
12678
12679 -- If the parent type in the generic declaration is itself a previous
12680 -- formal type, then it is local to the generic and absent from the
12681 -- analyzed generic definition. In that case the ancestor is the
12682 -- instance of the formal (which must have been instantiated
12683 -- previously), unless the ancestor is itself a formal derived type.
12684 -- In this latter case (which is the subject of Corrigendum 8652/0038
12685 -- (AI-202) the ancestor of the formals is the ancestor of its
12686 -- parent. Otherwise, the analyzed generic carries the parent type.
12687 -- If the parent type is defined in a previous formal package, then
12688 -- the scope of that formal package is that of the generic type
12689 -- itself, and it has already been mapped into the corresponding type
12690 -- in the actual package.
12691
12692 -- Common case: parent type defined outside of the generic
12693
12694 if Is_Entity_Name (Subtype_Mark (Def))
12695 and then Present (Entity (Subtype_Mark (Def)))
12696 then
12697 Ancestor := Get_Instance_Of (Entity (Subtype_Mark (Def)));
12698
12699 -- Check whether parent is defined in a previous formal package
12700
12701 elsif
12702 Scope (Scope (Base_Type (Etype (A_Gen_T)))) = Scope (A_Gen_T)
12703 then
12704 Ancestor :=
12705 Get_Instance_Of (Base_Type (Etype (A_Gen_T)));
12706
12707 -- The type may be a local derivation, or a type extension of a
12708 -- previous formal, or of a formal of a parent package.
12709
12710 elsif Is_Derived_Type (Get_Instance_Of (A_Gen_T))
12711 or else
12712 Ekind (Get_Instance_Of (A_Gen_T)) = E_Record_Type_With_Private
12713 then
12714 -- Check whether the parent is another derived formal type in the
12715 -- same generic unit.
12716
12717 if Etype (A_Gen_T) /= A_Gen_T
12718 and then Is_Generic_Type (Etype (A_Gen_T))
12719 and then Scope (Etype (A_Gen_T)) = Scope (A_Gen_T)
12720 and then Etype (Etype (A_Gen_T)) /= Etype (A_Gen_T)
12721 then
12722 -- Locate ancestor of parent from the subtype declaration
12723 -- created for the actual.
12724
12725 declare
12726 Decl : Node_Id;
12727
12728 begin
12729 Decl := First (Actual_Decls);
12730 while Present (Decl) loop
12731 if Nkind (Decl) = N_Subtype_Declaration
12732 and then Chars (Defining_Identifier (Decl)) =
12733 Chars (Etype (A_Gen_T))
12734 then
12735 Ancestor := Generic_Parent_Type (Decl);
12736 exit;
12737 else
12738 Next (Decl);
12739 end if;
12740 end loop;
12741 end;
12742
12743 pragma Assert (Present (Ancestor));
12744
12745 -- The ancestor itself may be a previous formal that has been
12746 -- instantiated.
12747
12748 Ancestor := Get_Instance_Of (Ancestor);
12749
12750 else
12751 Ancestor :=
12752 Get_Instance_Of (Base_Type (Get_Instance_Of (A_Gen_T)));
12753 end if;
12754
12755 -- Check whether parent is a previous formal of the current generic
12756
12757 elsif Is_Derived_Type (A_Gen_T)
12758 and then Is_Generic_Type (Etype (A_Gen_T))
12759 and then Scope (A_Gen_T) = Scope (Etype (A_Gen_T))
12760 then
12761 Ancestor := Get_Instance_Of (First_Subtype (Etype (A_Gen_T)));
12762
12763 -- An unusual case: the actual is a type declared in a parent unit,
12764 -- but is not a formal type so there is no instance_of for it.
12765 -- Retrieve it by analyzing the record extension.
12766
12767 elsif Is_Child_Unit (Scope (A_Gen_T))
12768 and then In_Open_Scopes (Scope (Act_T))
12769 and then Is_Generic_Instance (Scope (Act_T))
12770 then
12771 Analyze (Subtype_Mark (Def));
12772 Ancestor := Entity (Subtype_Mark (Def));
12773
12774 else
12775 Ancestor := Get_Instance_Of (Etype (Base_Type (A_Gen_T)));
12776 end if;
12777
12778 -- If the formal derived type has pragma Preelaborable_Initialization
12779 -- then the actual type must have preelaborable initialization.
12780
12781 if Known_To_Have_Preelab_Init (A_Gen_T)
12782 and then not Has_Preelaborable_Initialization (Act_T)
12783 then
12784 Error_Msg_NE
12785 ("actual for & must have preelaborable initialization",
12786 Actual, Gen_T);
12787 end if;
12788
12789 -- Ada 2005 (AI-251)
12790
12791 if Ada_Version >= Ada_2005 and then Is_Interface (Ancestor) then
12792 if not Interface_Present_In_Ancestor (Act_T, Ancestor) then
12793 Error_Msg_NE
12794 ("(Ada 2005) expected type implementing & in instantiation",
12795 Actual, Ancestor);
12796 end if;
12797
12798 -- Finally verify that the (instance of) the ancestor is an ancestor
12799 -- of the actual.
12800
12801 elsif not Is_Ancestor (Base_Type (Ancestor), Act_T) then
12802 Error_Msg_NE
12803 ("expect type derived from & in instantiation",
12804 Actual, First_Subtype (Ancestor));
12805 Abandon_Instantiation (Actual);
12806 end if;
12807
12808 -- Ada 2005 (AI-443): Synchronized formal derived type checks. Note
12809 -- that the formal type declaration has been rewritten as a private
12810 -- extension.
12811
12812 if Ada_Version >= Ada_2005
12813 and then Nkind (Parent (A_Gen_T)) = N_Private_Extension_Declaration
12814 and then Synchronized_Present (Parent (A_Gen_T))
12815 then
12816 -- The actual must be a synchronized tagged type
12817
12818 if not Is_Tagged_Type (Act_T) then
12819 Error_Msg_N
12820 ("actual of synchronized type must be tagged", Actual);
12821 Abandon_Instantiation (Actual);
12822
12823 elsif Nkind (Parent (Act_T)) = N_Full_Type_Declaration
12824 and then Nkind (Type_Definition (Parent (Act_T))) =
12825 N_Derived_Type_Definition
12826 and then not Synchronized_Present
12827 (Type_Definition (Parent (Act_T)))
12828 then
12829 Error_Msg_N
12830 ("actual of synchronized type must be synchronized", Actual);
12831 Abandon_Instantiation (Actual);
12832 end if;
12833 end if;
12834
12835 -- Perform atomic/volatile checks (RM C.6(12)). Note that AI05-0218-1
12836 -- removes the second instance of the phrase "or allow pass by copy".
12837
12838 -- In Ada_2020 the aspect may be specified explicitly for the formal
12839 -- regardless of whether an ancestor obeys it.
12840
12841 if Is_Atomic (Act_T)
12842 and then not Is_Atomic (Ancestor)
12843 and then not Is_Atomic (A_Gen_T)
12844 then
12845 Error_Msg_N
12846 ("cannot have atomic actual type for non-atomic formal type",
12847 Actual);
12848
12849 elsif Is_Volatile (Act_T)
12850 and then not Is_Volatile (Ancestor)
12851 and then not Is_Volatile (A_Gen_T)
12852 then
12853 Error_Msg_N
12854 ("cannot have volatile actual type for non-volatile formal type",
12855 Actual);
12856 end if;
12857
12858 -- It should not be necessary to check for unknown discriminants on
12859 -- Formal, but for some reason Has_Unknown_Discriminants is false for
12860 -- A_Gen_T, so Is_Definite_Subtype incorrectly returns True. This
12861 -- needs fixing. ???
12862
12863 if Is_Definite_Subtype (A_Gen_T)
12864 and then not Unknown_Discriminants_Present (Formal)
12865 and then not Is_Definite_Subtype (Act_T)
12866 then
12867 Error_Msg_N ("actual subtype must be constrained", Actual);
12868 Abandon_Instantiation (Actual);
12869 end if;
12870
12871 if not Unknown_Discriminants_Present (Formal) then
12872 if Is_Constrained (Ancestor) then
12873 if not Is_Constrained (Act_T) then
12874 Error_Msg_N ("actual subtype must be constrained", Actual);
12875 Abandon_Instantiation (Actual);
12876 end if;
12877
12878 -- Ancestor is unconstrained, Check if generic formal and actual
12879 -- agree on constrainedness. The check only applies to array types
12880 -- and discriminated types.
12881
12882 elsif Is_Constrained (Act_T) then
12883 if Ekind (Ancestor) = E_Access_Type
12884 or else (not Is_Constrained (A_Gen_T)
12885 and then Is_Composite_Type (A_Gen_T))
12886 then
12887 Error_Msg_N ("actual subtype must be unconstrained", Actual);
12888 Abandon_Instantiation (Actual);
12889 end if;
12890
12891 -- A class-wide type is only allowed if the formal has unknown
12892 -- discriminants.
12893
12894 elsif Is_Class_Wide_Type (Act_T)
12895 and then not Has_Unknown_Discriminants (Ancestor)
12896 then
12897 Error_Msg_NE
12898 ("actual for & cannot be a class-wide type", Actual, Gen_T);
12899 Abandon_Instantiation (Actual);
12900
12901 -- Otherwise, the formal and actual must have the same number
12902 -- of discriminants and each discriminant of the actual must
12903 -- correspond to a discriminant of the formal.
12904
12905 elsif Has_Discriminants (Act_T)
12906 and then not Has_Unknown_Discriminants (Act_T)
12907 and then Has_Discriminants (Ancestor)
12908 then
12909 Actual_Discr := First_Discriminant (Act_T);
12910 Ancestor_Discr := First_Discriminant (Ancestor);
12911 while Present (Actual_Discr)
12912 and then Present (Ancestor_Discr)
12913 loop
12914 if Base_Type (Act_T) /= Base_Type (Ancestor) and then
12915 No (Corresponding_Discriminant (Actual_Discr))
12916 then
12917 Error_Msg_NE
12918 ("discriminant & does not correspond "
12919 & "to ancestor discriminant", Actual, Actual_Discr);
12920 Abandon_Instantiation (Actual);
12921 end if;
12922
12923 Next_Discriminant (Actual_Discr);
12924 Next_Discriminant (Ancestor_Discr);
12925 end loop;
12926
12927 if Present (Actual_Discr) or else Present (Ancestor_Discr) then
12928 Error_Msg_NE
12929 ("actual for & must have same number of discriminants",
12930 Actual, Gen_T);
12931 Abandon_Instantiation (Actual);
12932 end if;
12933
12934 -- This case should be caught by the earlier check for
12935 -- constrainedness, but the check here is added for completeness.
12936
12937 elsif Has_Discriminants (Act_T)
12938 and then not Has_Unknown_Discriminants (Act_T)
12939 then
12940 Error_Msg_NE
12941 ("actual for & must not have discriminants", Actual, Gen_T);
12942 Abandon_Instantiation (Actual);
12943
12944 elsif Has_Discriminants (Ancestor) then
12945 Error_Msg_NE
12946 ("actual for & must have known discriminants", Actual, Gen_T);
12947 Abandon_Instantiation (Actual);
12948 end if;
12949
12950 if not Subtypes_Statically_Compatible
12951 (Act_T, Ancestor, Formal_Derived_Matching => True)
12952 then
12953 Error_Msg_N
12954 ("constraint on actual is incompatible with formal", Actual);
12955 Abandon_Instantiation (Actual);
12956 end if;
12957 end if;
12958
12959 -- If the formal and actual types are abstract, check that there
12960 -- are no abstract primitives of the actual type that correspond to
12961 -- nonabstract primitives of the formal type (second sentence of
12962 -- RM95 3.9.3(9)).
12963
12964 if Is_Abstract_Type (A_Gen_T) and then Is_Abstract_Type (Act_T) then
12965 Check_Abstract_Primitives : declare
12966 Gen_Prims : constant Elist_Id :=
12967 Primitive_Operations (A_Gen_T);
12968 Gen_Elmt : Elmt_Id;
12969 Gen_Subp : Entity_Id;
12970 Anc_Subp : Entity_Id;
12971 Anc_Formal : Entity_Id;
12972 Anc_F_Type : Entity_Id;
12973
12974 Act_Prims : constant Elist_Id := Primitive_Operations (Act_T);
12975 Act_Elmt : Elmt_Id;
12976 Act_Subp : Entity_Id;
12977 Act_Formal : Entity_Id;
12978 Act_F_Type : Entity_Id;
12979
12980 Subprograms_Correspond : Boolean;
12981
12982 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean;
12983 -- Returns true if T2 is derived directly or indirectly from
12984 -- T1, including derivations from interfaces. T1 and T2 are
12985 -- required to be specific tagged base types.
12986
12987 ------------------------
12988 -- Is_Tagged_Ancestor --
12989 ------------------------
12990
12991 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean
12992 is
12993 Intfc_Elmt : Elmt_Id;
12994
12995 begin
12996 -- The predicate is satisfied if the types are the same
12997
12998 if T1 = T2 then
12999 return True;
13000
13001 -- If we've reached the top of the derivation chain then
13002 -- we know that T1 is not an ancestor of T2.
13003
13004 elsif Etype (T2) = T2 then
13005 return False;
13006
13007 -- Proceed to check T2's immediate parent
13008
13009 elsif Is_Ancestor (T1, Base_Type (Etype (T2))) then
13010 return True;
13011
13012 -- Finally, check to see if T1 is an ancestor of any of T2's
13013 -- progenitors.
13014
13015 else
13016 Intfc_Elmt := First_Elmt (Interfaces (T2));
13017 while Present (Intfc_Elmt) loop
13018 if Is_Ancestor (T1, Node (Intfc_Elmt)) then
13019 return True;
13020 end if;
13021
13022 Next_Elmt (Intfc_Elmt);
13023 end loop;
13024 end if;
13025
13026 return False;
13027 end Is_Tagged_Ancestor;
13028
13029 -- Start of processing for Check_Abstract_Primitives
13030
13031 begin
13032 -- Loop over all of the formal derived type's primitives
13033
13034 Gen_Elmt := First_Elmt (Gen_Prims);
13035 while Present (Gen_Elmt) loop
13036 Gen_Subp := Node (Gen_Elmt);
13037
13038 -- If the primitive of the formal is not abstract, then
13039 -- determine whether there is a corresponding primitive of
13040 -- the actual type that's abstract.
13041
13042 if not Is_Abstract_Subprogram (Gen_Subp) then
13043 Act_Elmt := First_Elmt (Act_Prims);
13044 while Present (Act_Elmt) loop
13045 Act_Subp := Node (Act_Elmt);
13046
13047 -- If we find an abstract primitive of the actual,
13048 -- then we need to test whether it corresponds to the
13049 -- subprogram from which the generic formal primitive
13050 -- is inherited.
13051
13052 if Is_Abstract_Subprogram (Act_Subp) then
13053 Anc_Subp := Alias (Gen_Subp);
13054
13055 -- Test whether we have a corresponding primitive
13056 -- by comparing names, kinds, formal types, and
13057 -- result types.
13058
13059 if Chars (Anc_Subp) = Chars (Act_Subp)
13060 and then Ekind (Anc_Subp) = Ekind (Act_Subp)
13061 then
13062 Anc_Formal := First_Formal (Anc_Subp);
13063 Act_Formal := First_Formal (Act_Subp);
13064 while Present (Anc_Formal)
13065 and then Present (Act_Formal)
13066 loop
13067 Anc_F_Type := Etype (Anc_Formal);
13068 Act_F_Type := Etype (Act_Formal);
13069
13070 if Ekind (Anc_F_Type) =
13071 E_Anonymous_Access_Type
13072 then
13073 Anc_F_Type := Designated_Type (Anc_F_Type);
13074
13075 if Ekind (Act_F_Type) =
13076 E_Anonymous_Access_Type
13077 then
13078 Act_F_Type :=
13079 Designated_Type (Act_F_Type);
13080 else
13081 exit;
13082 end if;
13083
13084 elsif
13085 Ekind (Act_F_Type) = E_Anonymous_Access_Type
13086 then
13087 exit;
13088 end if;
13089
13090 Anc_F_Type := Base_Type (Anc_F_Type);
13091 Act_F_Type := Base_Type (Act_F_Type);
13092
13093 -- If the formal is controlling, then the
13094 -- the type of the actual primitive's formal
13095 -- must be derived directly or indirectly
13096 -- from the type of the ancestor primitive's
13097 -- formal.
13098
13099 if Is_Controlling_Formal (Anc_Formal) then
13100 if not Is_Tagged_Ancestor
13101 (Anc_F_Type, Act_F_Type)
13102 then
13103 exit;
13104 end if;
13105
13106 -- Otherwise the types of the formals must
13107 -- be the same.
13108
13109 elsif Anc_F_Type /= Act_F_Type then
13110 exit;
13111 end if;
13112
13113 Next_Entity (Anc_Formal);
13114 Next_Entity (Act_Formal);
13115 end loop;
13116
13117 -- If we traversed through all of the formals
13118 -- then so far the subprograms correspond, so
13119 -- now check that any result types correspond.
13120
13121 if No (Anc_Formal) and then No (Act_Formal) then
13122 Subprograms_Correspond := True;
13123
13124 if Ekind (Act_Subp) = E_Function then
13125 Anc_F_Type := Etype (Anc_Subp);
13126 Act_F_Type := Etype (Act_Subp);
13127
13128 if Ekind (Anc_F_Type) =
13129 E_Anonymous_Access_Type
13130 then
13131 Anc_F_Type :=
13132 Designated_Type (Anc_F_Type);
13133
13134 if Ekind (Act_F_Type) =
13135 E_Anonymous_Access_Type
13136 then
13137 Act_F_Type :=
13138 Designated_Type (Act_F_Type);
13139 else
13140 Subprograms_Correspond := False;
13141 end if;
13142
13143 elsif
13144 Ekind (Act_F_Type)
13145 = E_Anonymous_Access_Type
13146 then
13147 Subprograms_Correspond := False;
13148 end if;
13149
13150 Anc_F_Type := Base_Type (Anc_F_Type);
13151 Act_F_Type := Base_Type (Act_F_Type);
13152
13153 -- Now either the result types must be
13154 -- the same or, if the result type is
13155 -- controlling, the result type of the
13156 -- actual primitive must descend from the
13157 -- result type of the ancestor primitive.
13158
13159 if Subprograms_Correspond
13160 and then Anc_F_Type /= Act_F_Type
13161 and then
13162 Has_Controlling_Result (Anc_Subp)
13163 and then not Is_Tagged_Ancestor
13164 (Anc_F_Type, Act_F_Type)
13165 then
13166 Subprograms_Correspond := False;
13167 end if;
13168 end if;
13169
13170 -- Found a matching subprogram belonging to
13171 -- formal ancestor type, so actual subprogram
13172 -- corresponds and this violates 3.9.3(9).
13173
13174 if Subprograms_Correspond then
13175 Error_Msg_NE
13176 ("abstract subprogram & overrides "
13177 & "nonabstract subprogram of ancestor",
13178 Actual, Act_Subp);
13179 end if;
13180 end if;
13181 end if;
13182 end if;
13183
13184 Next_Elmt (Act_Elmt);
13185 end loop;
13186 end if;
13187
13188 Next_Elmt (Gen_Elmt);
13189 end loop;
13190 end Check_Abstract_Primitives;
13191 end if;
13192
13193 -- Verify that limitedness matches. If parent is a limited
13194 -- interface then the generic formal is not unless declared
13195 -- explicitly so. If not declared limited, the actual cannot be
13196 -- limited (see AI05-0087).
13197
13198 -- Even though this AI is a binding interpretation, we enable the
13199 -- check only in Ada 2012 mode, because this improper construct
13200 -- shows up in user code and in existing B-tests.
13201
13202 if Is_Limited_Type (Act_T)
13203 and then not Is_Limited_Type (A_Gen_T)
13204 and then Ada_Version >= Ada_2012
13205 then
13206 if In_Instance then
13207 null;
13208 else
13209 Error_Msg_NE
13210 ("actual for non-limited & cannot be a limited type",
13211 Actual, Gen_T);
13212 Explain_Limited_Type (Act_T, Actual);
13213 Abandon_Instantiation (Actual);
13214 end if;
13215 end if;
13216
13217 -- Don't check Ada_Version here (for now) because AI12-0036 is
13218 -- a binding interpretation; this decision may be reversed if
13219 -- the situation turns out to be similar to that of the preceding
13220 -- Is_Limited_Type test (see preceding comment).
13221
13222 declare
13223 Formal_Is_Private_Extension : constant Boolean :=
13224 Nkind (Parent (A_Gen_T)) = N_Private_Extension_Declaration;
13225
13226 Actual_Is_Tagged : constant Boolean := Is_Tagged_Type (Act_T);
13227 begin
13228 if Actual_Is_Tagged /= Formal_Is_Private_Extension then
13229 if In_Instance then
13230 null;
13231 else
13232 if Actual_Is_Tagged then
13233 Error_Msg_NE
13234 ("actual for & cannot be a tagged type",
13235 Actual, Gen_T);
13236 else
13237 Error_Msg_NE
13238 ("actual for & must be a tagged type",
13239 Actual, Gen_T);
13240 end if;
13241 Abandon_Instantiation (Actual);
13242 end if;
13243 end if;
13244 end;
13245 end Validate_Derived_Type_Instance;
13246
13247 ----------------------------------------
13248 -- Validate_Discriminated_Formal_Type --
13249 ----------------------------------------
13250
13251 procedure Validate_Discriminated_Formal_Type is
13252 Formal_Discr : Entity_Id;
13253 Actual_Discr : Entity_Id;
13254 Formal_Subt : Entity_Id;
13255
13256 begin
13257 if Has_Discriminants (A_Gen_T) then
13258 if not Has_Discriminants (Act_T) then
13259 Error_Msg_NE
13260 ("actual for & must have discriminants", Actual, Gen_T);
13261 Abandon_Instantiation (Actual);
13262
13263 elsif Is_Constrained (Act_T) then
13264 Error_Msg_NE
13265 ("actual for & must be unconstrained", Actual, Gen_T);
13266 Abandon_Instantiation (Actual);
13267
13268 else
13269 Formal_Discr := First_Discriminant (A_Gen_T);
13270 Actual_Discr := First_Discriminant (Act_T);
13271 while Formal_Discr /= Empty loop
13272 if Actual_Discr = Empty then
13273 Error_Msg_NE
13274 ("discriminants on actual do not match formal",
13275 Actual, Gen_T);
13276 Abandon_Instantiation (Actual);
13277 end if;
13278
13279 Formal_Subt := Get_Instance_Of (Etype (Formal_Discr));
13280
13281 -- Access discriminants match if designated types do
13282
13283 if Ekind (Base_Type (Formal_Subt)) = E_Anonymous_Access_Type
13284 and then (Ekind (Base_Type (Etype (Actual_Discr)))) =
13285 E_Anonymous_Access_Type
13286 and then
13287 Get_Instance_Of
13288 (Designated_Type (Base_Type (Formal_Subt))) =
13289 Designated_Type (Base_Type (Etype (Actual_Discr)))
13290 then
13291 null;
13292
13293 elsif Base_Type (Formal_Subt) /=
13294 Base_Type (Etype (Actual_Discr))
13295 then
13296 Error_Msg_NE
13297 ("types of actual discriminants must match formal",
13298 Actual, Gen_T);
13299 Abandon_Instantiation (Actual);
13300
13301 elsif not Subtypes_Statically_Match
13302 (Formal_Subt, Etype (Actual_Discr))
13303 and then Ada_Version >= Ada_95
13304 then
13305 Error_Msg_NE
13306 ("subtypes of actual discriminants must match formal",
13307 Actual, Gen_T);
13308 Abandon_Instantiation (Actual);
13309 end if;
13310
13311 Next_Discriminant (Formal_Discr);
13312 Next_Discriminant (Actual_Discr);
13313 end loop;
13314
13315 if Actual_Discr /= Empty then
13316 Error_Msg_NE
13317 ("discriminants on actual do not match formal",
13318 Actual, Gen_T);
13319 Abandon_Instantiation (Actual);
13320 end if;
13321 end if;
13322 end if;
13323 end Validate_Discriminated_Formal_Type;
13324
13325 ---------------------------------------
13326 -- Validate_Incomplete_Type_Instance --
13327 ---------------------------------------
13328
13329 procedure Validate_Incomplete_Type_Instance is
13330 begin
13331 if not Is_Tagged_Type (Act_T)
13332 and then Is_Tagged_Type (A_Gen_T)
13333 then
13334 Error_Msg_NE
13335 ("actual for & must be a tagged type", Actual, Gen_T);
13336 end if;
13337
13338 Validate_Discriminated_Formal_Type;
13339 end Validate_Incomplete_Type_Instance;
13340
13341 --------------------------------------
13342 -- Validate_Interface_Type_Instance --
13343 --------------------------------------
13344
13345 procedure Validate_Interface_Type_Instance is
13346 begin
13347 if not Is_Interface (Act_T) then
13348 Error_Msg_NE
13349 ("actual for formal interface type must be an interface",
13350 Actual, Gen_T);
13351
13352 elsif Is_Limited_Type (Act_T) /= Is_Limited_Type (A_Gen_T)
13353 or else Is_Task_Interface (A_Gen_T) /= Is_Task_Interface (Act_T)
13354 or else Is_Protected_Interface (A_Gen_T) /=
13355 Is_Protected_Interface (Act_T)
13356 or else Is_Synchronized_Interface (A_Gen_T) /=
13357 Is_Synchronized_Interface (Act_T)
13358 then
13359 Error_Msg_NE
13360 ("actual for interface& does not match (RM 12.5.5(4))",
13361 Actual, Gen_T);
13362 end if;
13363 end Validate_Interface_Type_Instance;
13364
13365 ------------------------------------
13366 -- Validate_Private_Type_Instance --
13367 ------------------------------------
13368
13369 procedure Validate_Private_Type_Instance is
13370 begin
13371 if Is_Limited_Type (Act_T)
13372 and then not Is_Limited_Type (A_Gen_T)
13373 then
13374 if In_Instance then
13375 null;
13376 else
13377 Error_Msg_NE
13378 ("actual for non-limited & cannot be a limited type", Actual,
13379 Gen_T);
13380 Explain_Limited_Type (Act_T, Actual);
13381 Abandon_Instantiation (Actual);
13382 end if;
13383
13384 elsif Known_To_Have_Preelab_Init (A_Gen_T)
13385 and then not Has_Preelaborable_Initialization (Act_T)
13386 then
13387 Error_Msg_NE
13388 ("actual for & must have preelaborable initialization", Actual,
13389 Gen_T);
13390
13391 elsif not Is_Definite_Subtype (Act_T)
13392 and then Is_Definite_Subtype (A_Gen_T)
13393 and then Ada_Version >= Ada_95
13394 then
13395 Error_Msg_NE
13396 ("actual for & must be a definite subtype", Actual, Gen_T);
13397
13398 elsif not Is_Tagged_Type (Act_T)
13399 and then Is_Tagged_Type (A_Gen_T)
13400 then
13401 Error_Msg_NE
13402 ("actual for & must be a tagged type", Actual, Gen_T);
13403 end if;
13404
13405 Validate_Discriminated_Formal_Type;
13406 Ancestor := Gen_T;
13407 end Validate_Private_Type_Instance;
13408
13409 -- Start of processing for Instantiate_Type
13410
13411 begin
13412 if Get_Instance_Of (A_Gen_T) /= A_Gen_T then
13413 Error_Msg_N ("duplicate instantiation of generic type", Actual);
13414 return New_List (Error);
13415
13416 elsif not Is_Entity_Name (Actual)
13417 or else not Is_Type (Entity (Actual))
13418 then
13419 Error_Msg_NE
13420 ("expect valid subtype mark to instantiate &", Actual, Gen_T);
13421 Abandon_Instantiation (Actual);
13422
13423 else
13424 Act_T := Entity (Actual);
13425
13426 -- Ada 2005 (AI-216): An Unchecked_Union subtype shall only be passed
13427 -- as a generic actual parameter if the corresponding formal type
13428 -- does not have a known_discriminant_part, or is a formal derived
13429 -- type that is an Unchecked_Union type.
13430
13431 if Is_Unchecked_Union (Base_Type (Act_T)) then
13432 if not Has_Discriminants (A_Gen_T)
13433 or else (Is_Derived_Type (A_Gen_T)
13434 and then Is_Unchecked_Union (A_Gen_T))
13435 then
13436 null;
13437 else
13438 Error_Msg_N ("unchecked union cannot be the actual for a "
13439 & "discriminated formal type", Act_T);
13440
13441 end if;
13442 end if;
13443
13444 -- Deal with fixed/floating restrictions
13445
13446 if Is_Floating_Point_Type (Act_T) then
13447 Check_Restriction (No_Floating_Point, Actual);
13448 elsif Is_Fixed_Point_Type (Act_T) then
13449 Check_Restriction (No_Fixed_Point, Actual);
13450 end if;
13451
13452 -- Deal with error of using incomplete type as generic actual.
13453 -- This includes limited views of a type, even if the non-limited
13454 -- view may be available.
13455
13456 if Ekind (Act_T) = E_Incomplete_Type
13457 or else (Is_Class_Wide_Type (Act_T)
13458 and then Ekind (Root_Type (Act_T)) = E_Incomplete_Type)
13459 then
13460 -- If the formal is an incomplete type, the actual can be
13461 -- incomplete as well.
13462
13463 if Ekind (A_Gen_T) = E_Incomplete_Type then
13464 null;
13465
13466 elsif Is_Class_Wide_Type (Act_T)
13467 or else No (Full_View (Act_T))
13468 then
13469 Error_Msg_N ("premature use of incomplete type", Actual);
13470 Abandon_Instantiation (Actual);
13471 else
13472 Act_T := Full_View (Act_T);
13473 Set_Entity (Actual, Act_T);
13474
13475 if Has_Private_Component (Act_T) then
13476 Error_Msg_N
13477 ("premature use of type with private component", Actual);
13478 end if;
13479 end if;
13480
13481 -- Deal with error of premature use of private type as generic actual
13482
13483 elsif Is_Private_Type (Act_T)
13484 and then Is_Private_Type (Base_Type (Act_T))
13485 and then not Is_Generic_Type (Act_T)
13486 and then not Is_Derived_Type (Act_T)
13487 and then No (Full_View (Root_Type (Act_T)))
13488 then
13489 -- If the formal is an incomplete type, the actual can be
13490 -- private or incomplete as well.
13491
13492 if Ekind (A_Gen_T) = E_Incomplete_Type then
13493 null;
13494 else
13495 Error_Msg_N ("premature use of private type", Actual);
13496 end if;
13497
13498 elsif Has_Private_Component (Act_T) then
13499 Error_Msg_N
13500 ("premature use of type with private component", Actual);
13501 end if;
13502
13503 Set_Instance_Of (A_Gen_T, Act_T);
13504
13505 -- If the type is generic, the class-wide type may also be used
13506
13507 if Is_Tagged_Type (A_Gen_T)
13508 and then Is_Tagged_Type (Act_T)
13509 and then not Is_Class_Wide_Type (A_Gen_T)
13510 then
13511 Set_Instance_Of (Class_Wide_Type (A_Gen_T),
13512 Class_Wide_Type (Act_T));
13513 end if;
13514
13515 if not Is_Abstract_Type (A_Gen_T)
13516 and then Is_Abstract_Type (Act_T)
13517 then
13518 Error_Msg_N
13519 ("actual of non-abstract formal cannot be abstract", Actual);
13520 end if;
13521
13522 -- A generic scalar type is a first subtype for which we generate
13523 -- an anonymous base type. Indicate that the instance of this base
13524 -- is the base type of the actual.
13525
13526 if Is_Scalar_Type (A_Gen_T) then
13527 Set_Instance_Of (Etype (A_Gen_T), Etype (Act_T));
13528 end if;
13529 end if;
13530
13531 Check_Shared_Variable_Control_Aspects;
13532
13533 if Error_Posted (Act_T) then
13534 null;
13535 else
13536 case Nkind (Def) is
13537 when N_Formal_Private_Type_Definition =>
13538 Validate_Private_Type_Instance;
13539
13540 when N_Formal_Incomplete_Type_Definition =>
13541 Validate_Incomplete_Type_Instance;
13542
13543 when N_Formal_Derived_Type_Definition =>
13544 Validate_Derived_Type_Instance;
13545
13546 when N_Formal_Discrete_Type_Definition =>
13547 if not Is_Discrete_Type (Act_T) then
13548 Error_Msg_NE
13549 ("expect discrete type in instantiation of&",
13550 Actual, Gen_T);
13551 Abandon_Instantiation (Actual);
13552 end if;
13553
13554 Diagnose_Predicated_Actual;
13555
13556 when N_Formal_Signed_Integer_Type_Definition =>
13557 if not Is_Signed_Integer_Type (Act_T) then
13558 Error_Msg_NE
13559 ("expect signed integer type in instantiation of&",
13560 Actual, Gen_T);
13561 Abandon_Instantiation (Actual);
13562 end if;
13563
13564 Diagnose_Predicated_Actual;
13565
13566 when N_Formal_Modular_Type_Definition =>
13567 if not Is_Modular_Integer_Type (Act_T) then
13568 Error_Msg_NE
13569 ("expect modular type in instantiation of &",
13570 Actual, Gen_T);
13571 Abandon_Instantiation (Actual);
13572 end if;
13573
13574 Diagnose_Predicated_Actual;
13575
13576 when N_Formal_Floating_Point_Definition =>
13577 if not Is_Floating_Point_Type (Act_T) then
13578 Error_Msg_NE
13579 ("expect float type in instantiation of &", Actual, Gen_T);
13580 Abandon_Instantiation (Actual);
13581 end if;
13582
13583 when N_Formal_Ordinary_Fixed_Point_Definition =>
13584 if not Is_Ordinary_Fixed_Point_Type (Act_T) then
13585 Error_Msg_NE
13586 ("expect ordinary fixed point type in instantiation of &",
13587 Actual, Gen_T);
13588 Abandon_Instantiation (Actual);
13589 end if;
13590
13591 when N_Formal_Decimal_Fixed_Point_Definition =>
13592 if not Is_Decimal_Fixed_Point_Type (Act_T) then
13593 Error_Msg_NE
13594 ("expect decimal type in instantiation of &",
13595 Actual, Gen_T);
13596 Abandon_Instantiation (Actual);
13597 end if;
13598
13599 when N_Array_Type_Definition =>
13600 Validate_Array_Type_Instance;
13601
13602 when N_Access_To_Object_Definition =>
13603 Validate_Access_Type_Instance;
13604
13605 when N_Access_Function_Definition
13606 | N_Access_Procedure_Definition
13607 =>
13608 Validate_Access_Subprogram_Instance;
13609
13610 when N_Record_Definition =>
13611 Validate_Interface_Type_Instance;
13612
13613 when N_Derived_Type_Definition =>
13614 Validate_Derived_Interface_Type_Instance;
13615
13616 when others =>
13617 raise Program_Error;
13618 end case;
13619 end if;
13620
13621 Subt := New_Copy (Gen_T);
13622
13623 -- Use adjusted sloc of subtype name as the location for other nodes in
13624 -- the subtype declaration.
13625
13626 Loc := Sloc (Subt);
13627
13628 Decl_Node :=
13629 Make_Subtype_Declaration (Loc,
13630 Defining_Identifier => Subt,
13631 Subtype_Indication => New_Occurrence_Of (Act_T, Loc));
13632
13633 -- Record whether the actual is private at this point, so that
13634 -- Check_Generic_Actuals can restore its proper view before the
13635 -- semantic analysis of the instance.
13636
13637 if Is_Private_Type (Act_T) then
13638 Set_Has_Private_View (Subtype_Indication (Decl_Node));
13639 end if;
13640
13641 -- In Ada 2012 the actual may be a limited view. Indicate that
13642 -- the local subtype must be treated as such.
13643
13644 if From_Limited_With (Act_T) then
13645 Set_Ekind (Subt, E_Incomplete_Subtype);
13646 Set_From_Limited_With (Subt);
13647 end if;
13648
13649 Decl_Nodes := New_List (Decl_Node);
13650
13651 -- Flag actual derived types so their elaboration produces the
13652 -- appropriate renamings for the primitive operations of the ancestor.
13653 -- Flag actual for formal private types as well, to determine whether
13654 -- operations in the private part may override inherited operations.
13655 -- If the formal has an interface list, the ancestor is not the
13656 -- parent, but the analyzed formal that includes the interface
13657 -- operations of all its progenitors.
13658
13659 -- Same treatment for formal private types, so we can check whether the
13660 -- type is tagged limited when validating derivations in the private
13661 -- part. (See AI05-096).
13662
13663 if Nkind (Def) = N_Formal_Derived_Type_Definition then
13664 if Present (Interface_List (Def)) then
13665 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
13666 else
13667 Set_Generic_Parent_Type (Decl_Node, Ancestor);
13668 end if;
13669
13670 elsif Nkind_In (Def, N_Formal_Private_Type_Definition,
13671 N_Formal_Incomplete_Type_Definition)
13672 then
13673 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
13674 end if;
13675
13676 -- If the actual is a synchronized type that implements an interface,
13677 -- the primitive operations are attached to the corresponding record,
13678 -- and we have to treat it as an additional generic actual, so that its
13679 -- primitive operations become visible in the instance. The task or
13680 -- protected type itself does not carry primitive operations.
13681
13682 if Is_Concurrent_Type (Act_T)
13683 and then Is_Tagged_Type (Act_T)
13684 and then Present (Corresponding_Record_Type (Act_T))
13685 and then Present (Ancestor)
13686 and then Is_Interface (Ancestor)
13687 then
13688 declare
13689 Corr_Rec : constant Entity_Id :=
13690 Corresponding_Record_Type (Act_T);
13691 New_Corr : Entity_Id;
13692 Corr_Decl : Node_Id;
13693
13694 begin
13695 New_Corr := Make_Temporary (Loc, 'S');
13696 Corr_Decl :=
13697 Make_Subtype_Declaration (Loc,
13698 Defining_Identifier => New_Corr,
13699 Subtype_Indication =>
13700 New_Occurrence_Of (Corr_Rec, Loc));
13701 Append_To (Decl_Nodes, Corr_Decl);
13702
13703 if Ekind (Act_T) = E_Task_Type then
13704 Set_Ekind (Subt, E_Task_Subtype);
13705 else
13706 Set_Ekind (Subt, E_Protected_Subtype);
13707 end if;
13708
13709 Set_Corresponding_Record_Type (Subt, Corr_Rec);
13710 Set_Generic_Parent_Type (Corr_Decl, Ancestor);
13711 Set_Generic_Parent_Type (Decl_Node, Empty);
13712 end;
13713 end if;
13714
13715 -- For a floating-point type, capture dimension info if any, because
13716 -- the generated subtype declaration does not come from source and
13717 -- will not process dimensions.
13718
13719 if Is_Floating_Point_Type (Act_T) then
13720 Copy_Dimensions (Act_T, Subt);
13721 end if;
13722
13723 return Decl_Nodes;
13724 end Instantiate_Type;
13725
13726 ---------------------
13727 -- Is_In_Main_Unit --
13728 ---------------------
13729
13730 function Is_In_Main_Unit (N : Node_Id) return Boolean is
13731 Unum : constant Unit_Number_Type := Get_Source_Unit (N);
13732 Current_Unit : Node_Id;
13733
13734 begin
13735 if Unum = Main_Unit then
13736 return True;
13737
13738 -- If the current unit is a subunit then it is either the main unit or
13739 -- is being compiled as part of the main unit.
13740
13741 elsif Nkind (N) = N_Compilation_Unit then
13742 return Nkind (Unit (N)) = N_Subunit;
13743 end if;
13744
13745 Current_Unit := Parent (N);
13746 while Present (Current_Unit)
13747 and then Nkind (Current_Unit) /= N_Compilation_Unit
13748 loop
13749 Current_Unit := Parent (Current_Unit);
13750 end loop;
13751
13752 -- The instantiation node is in the main unit, or else the current node
13753 -- (perhaps as the result of nested instantiations) is in the main unit,
13754 -- or in the declaration of the main unit, which in this last case must
13755 -- be a body.
13756
13757 return
13758 Current_Unit = Cunit (Main_Unit)
13759 or else Current_Unit = Library_Unit (Cunit (Main_Unit))
13760 or else (Present (Current_Unit)
13761 and then Present (Library_Unit (Current_Unit))
13762 and then Is_In_Main_Unit (Library_Unit (Current_Unit)));
13763 end Is_In_Main_Unit;
13764
13765 ----------------------------
13766 -- Load_Parent_Of_Generic --
13767 ----------------------------
13768
13769 procedure Load_Parent_Of_Generic
13770 (N : Node_Id;
13771 Spec : Node_Id;
13772 Body_Optional : Boolean := False)
13773 is
13774 Comp_Unit : constant Node_Id := Cunit (Get_Source_Unit (Spec));
13775 Saved_Style_Check : constant Boolean := Style_Check;
13776 Saved_Warnings : constant Warning_Record := Save_Warnings;
13777 True_Parent : Node_Id;
13778 Inst_Node : Node_Id;
13779 OK : Boolean;
13780 Previous_Instances : constant Elist_Id := New_Elmt_List;
13781
13782 procedure Collect_Previous_Instances (Decls : List_Id);
13783 -- Collect all instantiations in the given list of declarations, that
13784 -- precede the generic that we need to load. If the bodies of these
13785 -- instantiations are available, we must analyze them, to ensure that
13786 -- the public symbols generated are the same when the unit is compiled
13787 -- to generate code, and when it is compiled in the context of a unit
13788 -- that needs a particular nested instance. This process is applied to
13789 -- both package and subprogram instances.
13790
13791 --------------------------------
13792 -- Collect_Previous_Instances --
13793 --------------------------------
13794
13795 procedure Collect_Previous_Instances (Decls : List_Id) is
13796 Decl : Node_Id;
13797
13798 begin
13799 Decl := First (Decls);
13800 while Present (Decl) loop
13801 if Sloc (Decl) >= Sloc (Inst_Node) then
13802 return;
13803
13804 -- If Decl is an instantiation, then record it as requiring
13805 -- instantiation of the corresponding body, except if it is an
13806 -- abbreviated instantiation generated internally for conformance
13807 -- checking purposes only for the case of a formal package
13808 -- declared without a box (see Instantiate_Formal_Package). Such
13809 -- an instantiation does not generate any code (the actual code
13810 -- comes from actual) and thus does not need to be analyzed here.
13811 -- If the instantiation appears with a generic package body it is
13812 -- not analyzed here either.
13813
13814 elsif Nkind (Decl) = N_Package_Instantiation
13815 and then not Is_Internal (Defining_Entity (Decl))
13816 then
13817 Append_Elmt (Decl, Previous_Instances);
13818
13819 -- For a subprogram instantiation, omit instantiations intrinsic
13820 -- operations (Unchecked_Conversions, etc.) that have no bodies.
13821
13822 elsif Nkind_In (Decl, N_Function_Instantiation,
13823 N_Procedure_Instantiation)
13824 and then not Is_Intrinsic_Subprogram (Entity (Name (Decl)))
13825 then
13826 Append_Elmt (Decl, Previous_Instances);
13827
13828 elsif Nkind (Decl) = N_Package_Declaration then
13829 Collect_Previous_Instances
13830 (Visible_Declarations (Specification (Decl)));
13831 Collect_Previous_Instances
13832 (Private_Declarations (Specification (Decl)));
13833
13834 -- Previous non-generic bodies may contain instances as well
13835
13836 elsif Nkind (Decl) = N_Package_Body
13837 and then Ekind (Corresponding_Spec (Decl)) /= E_Generic_Package
13838 then
13839 Collect_Previous_Instances (Declarations (Decl));
13840
13841 elsif Nkind (Decl) = N_Subprogram_Body
13842 and then not Acts_As_Spec (Decl)
13843 and then not Is_Generic_Subprogram (Corresponding_Spec (Decl))
13844 then
13845 Collect_Previous_Instances (Declarations (Decl));
13846 end if;
13847
13848 Next (Decl);
13849 end loop;
13850 end Collect_Previous_Instances;
13851
13852 -- Start of processing for Load_Parent_Of_Generic
13853
13854 begin
13855 if not In_Same_Source_Unit (N, Spec)
13856 or else Nkind (Unit (Comp_Unit)) = N_Package_Declaration
13857 or else (Nkind (Unit (Comp_Unit)) = N_Package_Body
13858 and then not Is_In_Main_Unit (Spec))
13859 then
13860 -- Find body of parent of spec, and analyze it. A special case arises
13861 -- when the parent is an instantiation, that is to say when we are
13862 -- currently instantiating a nested generic. In that case, there is
13863 -- no separate file for the body of the enclosing instance. Instead,
13864 -- the enclosing body must be instantiated as if it were a pending
13865 -- instantiation, in order to produce the body for the nested generic
13866 -- we require now. Note that in that case the generic may be defined
13867 -- in a package body, the instance defined in the same package body,
13868 -- and the original enclosing body may not be in the main unit.
13869
13870 Inst_Node := Empty;
13871
13872 True_Parent := Parent (Spec);
13873 while Present (True_Parent)
13874 and then Nkind (True_Parent) /= N_Compilation_Unit
13875 loop
13876 if Nkind (True_Parent) = N_Package_Declaration
13877 and then
13878 Nkind (Original_Node (True_Parent)) = N_Package_Instantiation
13879 then
13880 -- Parent is a compilation unit that is an instantiation, and
13881 -- instantiation node has been replaced with package decl.
13882
13883 Inst_Node := Original_Node (True_Parent);
13884 exit;
13885
13886 elsif Nkind (True_Parent) = N_Package_Declaration
13887 and then Nkind (Parent (True_Parent)) = N_Compilation_Unit
13888 and then
13889 Nkind (Unit (Parent (True_Parent))) = N_Package_Instantiation
13890 then
13891 -- Parent is a compilation unit that is an instantiation, but
13892 -- instantiation node has not been replaced with package decl.
13893
13894 Inst_Node := Unit (Parent (True_Parent));
13895 exit;
13896
13897 elsif Nkind (True_Parent) = N_Package_Declaration
13898 and then Nkind (Parent (True_Parent)) /= N_Compilation_Unit
13899 and then Present (Generic_Parent (Specification (True_Parent)))
13900 then
13901 -- Parent is an instantiation within another specification.
13902 -- Declaration for instance has been inserted before original
13903 -- instantiation node. A direct link would be preferable?
13904
13905 Inst_Node := Next (True_Parent);
13906 while Present (Inst_Node)
13907 and then Nkind (Inst_Node) /= N_Package_Instantiation
13908 loop
13909 Next (Inst_Node);
13910 end loop;
13911
13912 -- If the instance appears within a generic, and the generic
13913 -- unit is defined within a formal package of the enclosing
13914 -- generic, there is no generic body available, and none
13915 -- needed. A more precise test should be used ???
13916
13917 if No (Inst_Node) then
13918 return;
13919 end if;
13920
13921 exit;
13922
13923 else
13924 True_Parent := Parent (True_Parent);
13925 end if;
13926 end loop;
13927
13928 -- Case where we are currently instantiating a nested generic
13929
13930 if Present (Inst_Node) then
13931 if Nkind (Parent (True_Parent)) = N_Compilation_Unit then
13932
13933 -- Instantiation node and declaration of instantiated package
13934 -- were exchanged when only the declaration was needed.
13935 -- Restore instantiation node before proceeding with body.
13936
13937 Set_Unit (Parent (True_Parent), Inst_Node);
13938 end if;
13939
13940 -- Now complete instantiation of enclosing body, if it appears in
13941 -- some other unit. If it appears in the current unit, the body
13942 -- will have been instantiated already.
13943
13944 if No (Corresponding_Body (Instance_Spec (Inst_Node))) then
13945
13946 -- We need to determine the expander mode to instantiate the
13947 -- enclosing body. Because the generic body we need may use
13948 -- global entities declared in the enclosing package (including
13949 -- aggregates) it is in general necessary to compile this body
13950 -- with expansion enabled, except if we are within a generic
13951 -- package, in which case the usual generic rule applies.
13952
13953 declare
13954 Exp_Status : Boolean := True;
13955 Scop : Entity_Id;
13956
13957 begin
13958 -- Loop through scopes looking for generic package
13959
13960 Scop := Scope (Defining_Entity (Instance_Spec (Inst_Node)));
13961 while Present (Scop)
13962 and then Scop /= Standard_Standard
13963 loop
13964 if Ekind (Scop) = E_Generic_Package then
13965 Exp_Status := False;
13966 exit;
13967 end if;
13968
13969 Scop := Scope (Scop);
13970 end loop;
13971
13972 -- Collect previous instantiations in the unit that contains
13973 -- the desired generic.
13974
13975 if Nkind (Parent (True_Parent)) /= N_Compilation_Unit
13976 and then not Body_Optional
13977 then
13978 declare
13979 Decl : Elmt_Id;
13980 Info : Pending_Body_Info;
13981 Par : Node_Id;
13982
13983 begin
13984 Par := Parent (Inst_Node);
13985 while Present (Par) loop
13986 exit when Nkind (Parent (Par)) = N_Compilation_Unit;
13987 Par := Parent (Par);
13988 end loop;
13989
13990 pragma Assert (Present (Par));
13991
13992 if Nkind (Par) = N_Package_Body then
13993 Collect_Previous_Instances (Declarations (Par));
13994
13995 elsif Nkind (Par) = N_Package_Declaration then
13996 Collect_Previous_Instances
13997 (Visible_Declarations (Specification (Par)));
13998 Collect_Previous_Instances
13999 (Private_Declarations (Specification (Par)));
14000
14001 else
14002 -- Enclosing unit is a subprogram body. In this
14003 -- case all instance bodies are processed in order
14004 -- and there is no need to collect them separately.
14005
14006 null;
14007 end if;
14008
14009 Decl := First_Elmt (Previous_Instances);
14010 while Present (Decl) loop
14011 Info :=
14012 (Act_Decl =>
14013 Instance_Spec (Node (Decl)),
14014 Config_Switches => Save_Config_Switches,
14015 Current_Sem_Unit =>
14016 Get_Code_Unit (Sloc (Node (Decl))),
14017 Expander_Status => Exp_Status,
14018 Inst_Node => Node (Decl),
14019 Local_Suppress_Stack_Top =>
14020 Local_Suppress_Stack_Top,
14021 Scope_Suppress => Scope_Suppress,
14022 Warnings => Save_Warnings);
14023
14024 -- Package instance
14025
14026 if Nkind (Node (Decl)) = N_Package_Instantiation
14027 then
14028 Instantiate_Package_Body
14029 (Info, Body_Optional => True);
14030
14031 -- Subprogram instance
14032
14033 else
14034 -- The instance_spec is in the wrapper package,
14035 -- usually followed by its local renaming
14036 -- declaration. See Build_Subprogram_Renaming
14037 -- for details. If the instance carries aspects,
14038 -- these result in the corresponding pragmas,
14039 -- inserted after the subprogram declaration.
14040 -- They must be skipped as well when retrieving
14041 -- the desired spec. Some of them may have been
14042 -- rewritten as null statements.
14043 -- A direct link would be more robust ???
14044
14045 declare
14046 Decl : Node_Id :=
14047 (Last (Visible_Declarations
14048 (Specification (Info.Act_Decl))));
14049 begin
14050 while Nkind_In (Decl,
14051 N_Null_Statement,
14052 N_Pragma,
14053 N_Subprogram_Renaming_Declaration)
14054 loop
14055 Decl := Prev (Decl);
14056 end loop;
14057
14058 Info.Act_Decl := Decl;
14059 end;
14060
14061 Instantiate_Subprogram_Body
14062 (Info, Body_Optional => True);
14063 end if;
14064
14065 Next_Elmt (Decl);
14066 end loop;
14067 end;
14068 end if;
14069
14070 Instantiate_Package_Body
14071 (Body_Info =>
14072 ((Act_Decl => True_Parent,
14073 Config_Switches => Save_Config_Switches,
14074 Current_Sem_Unit =>
14075 Get_Code_Unit (Sloc (Inst_Node)),
14076 Expander_Status => Exp_Status,
14077 Inst_Node => Inst_Node,
14078 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
14079 Scope_Suppress => Scope_Suppress,
14080 Warnings => Save_Warnings)),
14081 Body_Optional => Body_Optional);
14082 end;
14083 end if;
14084
14085 -- Case where we are not instantiating a nested generic
14086
14087 else
14088 Opt.Style_Check := False;
14089 Expander_Mode_Save_And_Set (True);
14090 Load_Needed_Body (Comp_Unit, OK);
14091 Opt.Style_Check := Saved_Style_Check;
14092 Restore_Warnings (Saved_Warnings);
14093 Expander_Mode_Restore;
14094
14095 if not OK
14096 and then Unit_Requires_Body (Defining_Entity (Spec))
14097 and then not Body_Optional
14098 then
14099 declare
14100 Bname : constant Unit_Name_Type :=
14101 Get_Body_Name (Get_Unit_Name (Unit (Comp_Unit)));
14102
14103 begin
14104 -- In CodePeer mode, the missing body may make the analysis
14105 -- incomplete, but we do not treat it as fatal.
14106
14107 if CodePeer_Mode then
14108 return;
14109
14110 else
14111 Error_Msg_Unit_1 := Bname;
14112 Error_Msg_N ("this instantiation requires$!", N);
14113 Error_Msg_File_1 :=
14114 Get_File_Name (Bname, Subunit => False);
14115 Error_Msg_N ("\but file{ was not found!", N);
14116 raise Unrecoverable_Error;
14117 end if;
14118 end;
14119 end if;
14120 end if;
14121 end if;
14122
14123 -- If loading parent of the generic caused an instantiation circularity,
14124 -- we abandon compilation at this point, because otherwise in some cases
14125 -- we get into trouble with infinite recursions after this point.
14126
14127 if Circularity_Detected then
14128 raise Unrecoverable_Error;
14129 end if;
14130 end Load_Parent_Of_Generic;
14131
14132 ---------------------------------
14133 -- Map_Formal_Package_Entities --
14134 ---------------------------------
14135
14136 procedure Map_Formal_Package_Entities (Form : Entity_Id; Act : Entity_Id) is
14137 E1 : Entity_Id;
14138 E2 : Entity_Id;
14139
14140 begin
14141 Set_Instance_Of (Form, Act);
14142
14143 -- Traverse formal and actual package to map the corresponding entities.
14144 -- We skip over internal entities that may be generated during semantic
14145 -- analysis, and find the matching entities by name, given that they
14146 -- must appear in the same order.
14147
14148 E1 := First_Entity (Form);
14149 E2 := First_Entity (Act);
14150 while Present (E1) and then E1 /= First_Private_Entity (Form) loop
14151 -- Could this test be a single condition??? Seems like it could, and
14152 -- isn't FPE (Form) a constant anyway???
14153
14154 if not Is_Internal (E1)
14155 and then Present (Parent (E1))
14156 and then not Is_Class_Wide_Type (E1)
14157 and then not Is_Internal_Name (Chars (E1))
14158 then
14159 while Present (E2) and then Chars (E2) /= Chars (E1) loop
14160 Next_Entity (E2);
14161 end loop;
14162
14163 if No (E2) then
14164 exit;
14165 else
14166 Set_Instance_Of (E1, E2);
14167
14168 if Is_Type (E1) and then Is_Tagged_Type (E2) then
14169 Set_Instance_Of (Class_Wide_Type (E1), Class_Wide_Type (E2));
14170 end if;
14171
14172 if Is_Constrained (E1) then
14173 Set_Instance_Of (Base_Type (E1), Base_Type (E2));
14174 end if;
14175
14176 if Ekind (E1) = E_Package and then No (Renamed_Object (E1)) then
14177 Map_Formal_Package_Entities (E1, E2);
14178 end if;
14179 end if;
14180 end if;
14181
14182 Next_Entity (E1);
14183 end loop;
14184 end Map_Formal_Package_Entities;
14185
14186 -----------------------
14187 -- Move_Freeze_Nodes --
14188 -----------------------
14189
14190 procedure Move_Freeze_Nodes
14191 (Out_Of : Entity_Id;
14192 After : Node_Id;
14193 L : List_Id)
14194 is
14195 Decl : Node_Id;
14196 Next_Decl : Node_Id;
14197 Next_Node : Node_Id := After;
14198 Spec : Node_Id;
14199
14200 function Is_Outer_Type (T : Entity_Id) return Boolean;
14201 -- Check whether entity is declared in a scope external to that of the
14202 -- generic unit.
14203
14204 -------------------
14205 -- Is_Outer_Type --
14206 -------------------
14207
14208 function Is_Outer_Type (T : Entity_Id) return Boolean is
14209 Scop : Entity_Id := Scope (T);
14210
14211 begin
14212 if Scope_Depth (Scop) < Scope_Depth (Out_Of) then
14213 return True;
14214
14215 else
14216 while Scop /= Standard_Standard loop
14217 if Scop = Out_Of then
14218 return False;
14219 else
14220 Scop := Scope (Scop);
14221 end if;
14222 end loop;
14223
14224 return True;
14225 end if;
14226 end Is_Outer_Type;
14227
14228 -- Start of processing for Move_Freeze_Nodes
14229
14230 begin
14231 if No (L) then
14232 return;
14233 end if;
14234
14235 -- First remove the freeze nodes that may appear before all other
14236 -- declarations.
14237
14238 Decl := First (L);
14239 while Present (Decl)
14240 and then Nkind (Decl) = N_Freeze_Entity
14241 and then Is_Outer_Type (Entity (Decl))
14242 loop
14243 Decl := Remove_Head (L);
14244 Insert_After (Next_Node, Decl);
14245 Set_Analyzed (Decl, False);
14246 Next_Node := Decl;
14247 Decl := First (L);
14248 end loop;
14249
14250 -- Next scan the list of declarations and remove each freeze node that
14251 -- appears ahead of the current node.
14252
14253 while Present (Decl) loop
14254 while Present (Next (Decl))
14255 and then Nkind (Next (Decl)) = N_Freeze_Entity
14256 and then Is_Outer_Type (Entity (Next (Decl)))
14257 loop
14258 Next_Decl := Remove_Next (Decl);
14259 Insert_After (Next_Node, Next_Decl);
14260 Set_Analyzed (Next_Decl, False);
14261 Next_Node := Next_Decl;
14262 end loop;
14263
14264 -- If the declaration is a nested package or concurrent type, then
14265 -- recurse. Nested generic packages will have been processed from the
14266 -- inside out.
14267
14268 case Nkind (Decl) is
14269 when N_Package_Declaration =>
14270 Spec := Specification (Decl);
14271
14272 when N_Task_Type_Declaration =>
14273 Spec := Task_Definition (Decl);
14274
14275 when N_Protected_Type_Declaration =>
14276 Spec := Protected_Definition (Decl);
14277
14278 when others =>
14279 Spec := Empty;
14280 end case;
14281
14282 if Present (Spec) then
14283 Move_Freeze_Nodes (Out_Of, Next_Node, Visible_Declarations (Spec));
14284 Move_Freeze_Nodes (Out_Of, Next_Node, Private_Declarations (Spec));
14285 end if;
14286
14287 Next (Decl);
14288 end loop;
14289 end Move_Freeze_Nodes;
14290
14291 ----------------
14292 -- Next_Assoc --
14293 ----------------
14294
14295 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr is
14296 begin
14297 return Generic_Renamings.Table (E).Next_In_HTable;
14298 end Next_Assoc;
14299
14300 ------------------------
14301 -- Preanalyze_Actuals --
14302 ------------------------
14303
14304 procedure Preanalyze_Actuals (N : Node_Id; Inst : Entity_Id := Empty) is
14305 procedure Perform_Appropriate_Analysis (N : Node_Id);
14306 -- Determine if the actuals we are analyzing come from a generic
14307 -- instantiation that is a library unit and dispatch accordingly.
14308
14309 ----------------------------------
14310 -- Perform_Appropriate_Analysis --
14311 ----------------------------------
14312
14313 procedure Perform_Appropriate_Analysis (N : Node_Id) is
14314 begin
14315 -- When we have a library instantiation we cannot allow any expansion
14316 -- to occur, since there may be no place to put it. Instead, in that
14317 -- case we perform a preanalysis of the actual.
14318
14319 if Present (Inst) and then Is_Compilation_Unit (Inst) then
14320 Preanalyze (N);
14321 else
14322 Analyze (N);
14323 end if;
14324 end Perform_Appropriate_Analysis;
14325
14326 -- Local variables
14327
14328 Errs : constant Nat := Serious_Errors_Detected;
14329
14330 Assoc : Node_Id;
14331 Act : Node_Id;
14332
14333 Cur : Entity_Id := Empty;
14334 -- Current homograph of the instance name
14335
14336 Vis : Boolean := False;
14337 -- Saved visibility status of the current homograph
14338
14339 -- Start of processing for Preanalyze_Actuals
14340
14341 begin
14342 Assoc := First (Generic_Associations (N));
14343
14344 -- If the instance is a child unit, its name may hide an outer homonym,
14345 -- so make it invisible to perform name resolution on the actuals.
14346
14347 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name
14348 and then Present
14349 (Current_Entity (Defining_Identifier (Defining_Unit_Name (N))))
14350 then
14351 Cur := Current_Entity (Defining_Identifier (Defining_Unit_Name (N)));
14352
14353 if Is_Compilation_Unit (Cur) then
14354 Vis := Is_Immediately_Visible (Cur);
14355 Set_Is_Immediately_Visible (Cur, False);
14356 else
14357 Cur := Empty;
14358 end if;
14359 end if;
14360
14361 while Present (Assoc) loop
14362 if Nkind (Assoc) /= N_Others_Choice then
14363 Act := Explicit_Generic_Actual_Parameter (Assoc);
14364
14365 -- Within a nested instantiation, a defaulted actual is an empty
14366 -- association, so nothing to analyze. If the subprogram actual
14367 -- is an attribute, analyze prefix only, because actual is not a
14368 -- complete attribute reference.
14369
14370 -- If actual is an allocator, analyze expression only. The full
14371 -- analysis can generate code, and if instance is a compilation
14372 -- unit we have to wait until the package instance is installed
14373 -- to have a proper place to insert this code.
14374
14375 -- String literals may be operators, but at this point we do not
14376 -- know whether the actual is a formal subprogram or a string.
14377
14378 if No (Act) then
14379 null;
14380
14381 elsif Nkind (Act) = N_Attribute_Reference then
14382 Perform_Appropriate_Analysis (Prefix (Act));
14383
14384 elsif Nkind (Act) = N_Explicit_Dereference then
14385 Perform_Appropriate_Analysis (Prefix (Act));
14386
14387 elsif Nkind (Act) = N_Allocator then
14388 declare
14389 Expr : constant Node_Id := Expression (Act);
14390
14391 begin
14392 if Nkind (Expr) = N_Subtype_Indication then
14393 Perform_Appropriate_Analysis (Subtype_Mark (Expr));
14394
14395 -- Analyze separately each discriminant constraint, when
14396 -- given with a named association.
14397
14398 declare
14399 Constr : Node_Id;
14400
14401 begin
14402 Constr := First (Constraints (Constraint (Expr)));
14403 while Present (Constr) loop
14404 if Nkind (Constr) = N_Discriminant_Association then
14405 Perform_Appropriate_Analysis
14406 (Expression (Constr));
14407 else
14408 Perform_Appropriate_Analysis (Constr);
14409 end if;
14410
14411 Next (Constr);
14412 end loop;
14413 end;
14414
14415 else
14416 Perform_Appropriate_Analysis (Expr);
14417 end if;
14418 end;
14419
14420 elsif Nkind (Act) /= N_Operator_Symbol then
14421 Perform_Appropriate_Analysis (Act);
14422
14423 -- Within a package instance, mark actuals that are limited
14424 -- views, so their use can be moved to the body of the
14425 -- enclosing unit.
14426
14427 if Is_Entity_Name (Act)
14428 and then Is_Type (Entity (Act))
14429 and then From_Limited_With (Entity (Act))
14430 and then Present (Inst)
14431 then
14432 Append_Elmt (Entity (Act), Incomplete_Actuals (Inst));
14433 end if;
14434 end if;
14435
14436 if Errs /= Serious_Errors_Detected then
14437
14438 -- Do a minimal analysis of the generic, to prevent spurious
14439 -- warnings complaining about the generic being unreferenced,
14440 -- before abandoning the instantiation.
14441
14442 Perform_Appropriate_Analysis (Name (N));
14443
14444 if Is_Entity_Name (Name (N))
14445 and then Etype (Name (N)) /= Any_Type
14446 then
14447 Generate_Reference (Entity (Name (N)), Name (N));
14448 Set_Is_Instantiated (Entity (Name (N)));
14449 end if;
14450
14451 if Present (Cur) then
14452
14453 -- For the case of a child instance hiding an outer homonym,
14454 -- provide additional warning which might explain the error.
14455
14456 Set_Is_Immediately_Visible (Cur, Vis);
14457 Error_Msg_NE
14458 ("& hides outer unit with the same name??",
14459 N, Defining_Unit_Name (N));
14460 end if;
14461
14462 Abandon_Instantiation (Act);
14463 end if;
14464 end if;
14465
14466 Next (Assoc);
14467 end loop;
14468
14469 if Present (Cur) then
14470 Set_Is_Immediately_Visible (Cur, Vis);
14471 end if;
14472 end Preanalyze_Actuals;
14473
14474 -------------------------------
14475 -- Provide_Completing_Bodies --
14476 -------------------------------
14477
14478 procedure Provide_Completing_Bodies (N : Node_Id) is
14479 procedure Build_Completing_Body (Subp_Decl : Node_Id);
14480 -- Generate the completing body for subprogram declaration Subp_Decl
14481
14482 procedure Provide_Completing_Bodies_In (Decls : List_Id);
14483 -- Generating completing bodies for all subprograms found in declarative
14484 -- list Decls.
14485
14486 ---------------------------
14487 -- Build_Completing_Body --
14488 ---------------------------
14489
14490 procedure Build_Completing_Body (Subp_Decl : Node_Id) is
14491 Loc : constant Source_Ptr := Sloc (Subp_Decl);
14492 Subp_Id : constant Entity_Id := Defining_Entity (Subp_Decl);
14493 Spec : Node_Id;
14494
14495 begin
14496 -- Nothing to do if the subprogram already has a completing body
14497
14498 if Present (Corresponding_Body (Subp_Decl)) then
14499 return;
14500
14501 -- Mark the function as having a valid return statement even though
14502 -- the body contains a single raise statement.
14503
14504 elsif Ekind (Subp_Id) = E_Function then
14505 Set_Return_Present (Subp_Id);
14506 end if;
14507
14508 -- Clone the specification to obtain new entities and reset the only
14509 -- semantic field.
14510
14511 Spec := Copy_Subprogram_Spec (Specification (Subp_Decl));
14512 Set_Generic_Parent (Spec, Empty);
14513
14514 -- Generate:
14515 -- function Func ... return ... is
14516 -- <or>
14517 -- procedure Proc ... is
14518 -- begin
14519 -- raise Program_Error with "access before elaboration";
14520 -- edn Proc;
14521
14522 Insert_After_And_Analyze (Subp_Decl,
14523 Make_Subprogram_Body (Loc,
14524 Specification => Spec,
14525 Declarations => New_List,
14526 Handled_Statement_Sequence =>
14527 Make_Handled_Sequence_Of_Statements (Loc,
14528 Statements => New_List (
14529 Make_Raise_Program_Error (Loc,
14530 Reason => PE_Access_Before_Elaboration)))));
14531 end Build_Completing_Body;
14532
14533 ----------------------------------
14534 -- Provide_Completing_Bodies_In --
14535 ----------------------------------
14536
14537 procedure Provide_Completing_Bodies_In (Decls : List_Id) is
14538 Decl : Node_Id;
14539
14540 begin
14541 if Present (Decls) then
14542 Decl := First (Decls);
14543 while Present (Decl) loop
14544 Provide_Completing_Bodies (Decl);
14545 Next (Decl);
14546 end loop;
14547 end if;
14548 end Provide_Completing_Bodies_In;
14549
14550 -- Local variables
14551
14552 Spec : Node_Id;
14553
14554 -- Start of processing for Provide_Completing_Bodies
14555
14556 begin
14557 if Nkind (N) = N_Package_Declaration then
14558 Spec := Specification (N);
14559
14560 Push_Scope (Defining_Entity (N));
14561 Provide_Completing_Bodies_In (Visible_Declarations (Spec));
14562 Provide_Completing_Bodies_In (Private_Declarations (Spec));
14563 Pop_Scope;
14564
14565 elsif Nkind (N) = N_Subprogram_Declaration then
14566 Build_Completing_Body (N);
14567 end if;
14568 end Provide_Completing_Bodies;
14569
14570 -------------------
14571 -- Remove_Parent --
14572 -------------------
14573
14574 procedure Remove_Parent (In_Body : Boolean := False) is
14575 S : Entity_Id := Current_Scope;
14576 -- S is the scope containing the instantiation just completed. The scope
14577 -- stack contains the parent instances of the instantiation, followed by
14578 -- the original S.
14579
14580 Cur_P : Entity_Id;
14581 E : Entity_Id;
14582 P : Entity_Id;
14583 Hidden : Elmt_Id;
14584
14585 begin
14586 -- After child instantiation is complete, remove from scope stack the
14587 -- extra copy of the current scope, and then remove parent instances.
14588
14589 if not In_Body then
14590 Pop_Scope;
14591
14592 while Current_Scope /= S loop
14593 P := Current_Scope;
14594 End_Package_Scope (Current_Scope);
14595
14596 if In_Open_Scopes (P) then
14597 E := First_Entity (P);
14598 while Present (E) loop
14599 Set_Is_Immediately_Visible (E, True);
14600 Next_Entity (E);
14601 end loop;
14602
14603 -- If instantiation is declared in a block, it is the enclosing
14604 -- scope that might be a parent instance. Note that only one
14605 -- block can be involved, because the parent instances have
14606 -- been installed within it.
14607
14608 if Ekind (P) = E_Block then
14609 Cur_P := Scope (P);
14610 else
14611 Cur_P := P;
14612 end if;
14613
14614 if Is_Generic_Instance (Cur_P) and then P /= Current_Scope then
14615 -- We are within an instance of some sibling. Retain
14616 -- visibility of parent, for proper subsequent cleanup, and
14617 -- reinstall private declarations as well.
14618
14619 Set_In_Private_Part (P);
14620 Install_Private_Declarations (P);
14621 end if;
14622
14623 -- If the ultimate parent is a top-level unit recorded in
14624 -- Instance_Parent_Unit, then reset its visibility to what it was
14625 -- before instantiation. (It's not clear what the purpose is of
14626 -- testing whether Scope (P) is In_Open_Scopes, but that test was
14627 -- present before the ultimate parent test was added.???)
14628
14629 elsif not In_Open_Scopes (Scope (P))
14630 or else (P = Instance_Parent_Unit
14631 and then not Parent_Unit_Visible)
14632 then
14633 Set_Is_Immediately_Visible (P, False);
14634
14635 -- If the current scope is itself an instantiation of a generic
14636 -- nested within P, and we are in the private part of body of this
14637 -- instantiation, restore the full views of P, that were removed
14638 -- in End_Package_Scope above. This obscure case can occur when a
14639 -- subunit of a generic contains an instance of a child unit of
14640 -- its generic parent unit.
14641
14642 elsif S = Current_Scope and then Is_Generic_Instance (S) then
14643 declare
14644 Par : constant Entity_Id :=
14645 Generic_Parent (Package_Specification (S));
14646 begin
14647 if Present (Par)
14648 and then P = Scope (Par)
14649 and then (In_Package_Body (S) or else In_Private_Part (S))
14650 then
14651 Set_In_Private_Part (P);
14652 Install_Private_Declarations (P);
14653 end if;
14654 end;
14655 end if;
14656 end loop;
14657
14658 -- Reset visibility of entities in the enclosing scope
14659
14660 Set_Is_Hidden_Open_Scope (Current_Scope, False);
14661
14662 Hidden := First_Elmt (Hidden_Entities);
14663 while Present (Hidden) loop
14664 Set_Is_Immediately_Visible (Node (Hidden), True);
14665 Next_Elmt (Hidden);
14666 end loop;
14667
14668 else
14669 -- Each body is analyzed separately, and there is no context that
14670 -- needs preserving from one body instance to the next, so remove all
14671 -- parent scopes that have been installed.
14672
14673 while Present (S) loop
14674 End_Package_Scope (S);
14675 Set_Is_Immediately_Visible (S, False);
14676 S := Current_Scope;
14677 exit when S = Standard_Standard;
14678 end loop;
14679 end if;
14680 end Remove_Parent;
14681
14682 -----------------
14683 -- Restore_Env --
14684 -----------------
14685
14686 procedure Restore_Env is
14687 Saved : Instance_Env renames Instance_Envs.Table (Instance_Envs.Last);
14688
14689 begin
14690 if No (Current_Instantiated_Parent.Act_Id) then
14691 -- Restore environment after subprogram inlining
14692
14693 Restore_Private_Views (Empty);
14694 end if;
14695
14696 Current_Instantiated_Parent := Saved.Instantiated_Parent;
14697 Exchanged_Views := Saved.Exchanged_Views;
14698 Hidden_Entities := Saved.Hidden_Entities;
14699 Current_Sem_Unit := Saved.Current_Sem_Unit;
14700 Parent_Unit_Visible := Saved.Parent_Unit_Visible;
14701 Instance_Parent_Unit := Saved.Instance_Parent_Unit;
14702
14703 Restore_Config_Switches (Saved.Switches);
14704
14705 Instance_Envs.Decrement_Last;
14706 end Restore_Env;
14707
14708 ---------------------------
14709 -- Restore_Private_Views --
14710 ---------------------------
14711
14712 procedure Restore_Private_Views
14713 (Pack_Id : Entity_Id;
14714 Is_Package : Boolean := True)
14715 is
14716 M : Elmt_Id;
14717 E : Entity_Id;
14718 Typ : Entity_Id;
14719 Dep_Elmt : Elmt_Id;
14720 Dep_Typ : Node_Id;
14721
14722 procedure Restore_Nested_Formal (Formal : Entity_Id);
14723 -- Hide the generic formals of formal packages declared with box which
14724 -- were reachable in the current instantiation.
14725
14726 ---------------------------
14727 -- Restore_Nested_Formal --
14728 ---------------------------
14729
14730 procedure Restore_Nested_Formal (Formal : Entity_Id) is
14731 Ent : Entity_Id;
14732
14733 begin
14734 if Present (Renamed_Object (Formal))
14735 and then Denotes_Formal_Package (Renamed_Object (Formal), True)
14736 then
14737 return;
14738
14739 elsif Present (Associated_Formal_Package (Formal)) then
14740 Ent := First_Entity (Formal);
14741 while Present (Ent) loop
14742 exit when Ekind (Ent) = E_Package
14743 and then Renamed_Entity (Ent) = Renamed_Entity (Formal);
14744
14745 Set_Is_Hidden (Ent);
14746 Set_Is_Potentially_Use_Visible (Ent, False);
14747
14748 -- If package, then recurse
14749
14750 if Ekind (Ent) = E_Package then
14751 Restore_Nested_Formal (Ent);
14752 end if;
14753
14754 Next_Entity (Ent);
14755 end loop;
14756 end if;
14757 end Restore_Nested_Formal;
14758
14759 -- Start of processing for Restore_Private_Views
14760
14761 begin
14762 M := First_Elmt (Exchanged_Views);
14763 while Present (M) loop
14764 Typ := Node (M);
14765
14766 -- Subtypes of types whose views have been exchanged, and that are
14767 -- defined within the instance, were not on the Private_Dependents
14768 -- list on entry to the instance, so they have to be exchanged
14769 -- explicitly now, in order to remain consistent with the view of the
14770 -- parent type.
14771
14772 if Ekind_In (Typ, E_Private_Type,
14773 E_Limited_Private_Type,
14774 E_Record_Type_With_Private)
14775 then
14776 Dep_Elmt := First_Elmt (Private_Dependents (Typ));
14777 while Present (Dep_Elmt) loop
14778 Dep_Typ := Node (Dep_Elmt);
14779
14780 if Scope (Dep_Typ) = Pack_Id
14781 and then Present (Full_View (Dep_Typ))
14782 then
14783 Replace_Elmt (Dep_Elmt, Full_View (Dep_Typ));
14784 Exchange_Declarations (Dep_Typ);
14785 end if;
14786
14787 Next_Elmt (Dep_Elmt);
14788 end loop;
14789 end if;
14790
14791 Exchange_Declarations (Node (M));
14792 Next_Elmt (M);
14793 end loop;
14794
14795 if No (Pack_Id) then
14796 return;
14797 end if;
14798
14799 -- Make the generic formal parameters private, and make the formal types
14800 -- into subtypes of the actuals again.
14801
14802 E := First_Entity (Pack_Id);
14803 while Present (E) loop
14804 Set_Is_Hidden (E, True);
14805
14806 if Is_Type (E)
14807 and then Nkind (Parent (E)) = N_Subtype_Declaration
14808 then
14809 -- If the actual for E is itself a generic actual type from
14810 -- an enclosing instance, E is still a generic actual type
14811 -- outside of the current instance. This matter when resolving
14812 -- an overloaded call that may be ambiguous in the enclosing
14813 -- instance, when two of its actuals coincide.
14814
14815 if Is_Entity_Name (Subtype_Indication (Parent (E)))
14816 and then Is_Generic_Actual_Type
14817 (Entity (Subtype_Indication (Parent (E))))
14818 then
14819 null;
14820 else
14821 Set_Is_Generic_Actual_Type (E, False);
14822
14823 -- It might seem reasonable to clear the Is_Generic_Actual_Type
14824 -- flag also on the Full_View if the type is private, since it
14825 -- was set also on this Full_View. However, this flag is relied
14826 -- upon by Covers to spot "types exported from instantiations"
14827 -- which are implicit Full_Views built for instantiations made
14828 -- on private types and we get type mismatches if we do it when
14829 -- the block exchanging the declarations below triggers ???
14830
14831 -- if Is_Private_Type (E) and then Present (Full_View (E)) then
14832 -- Set_Is_Generic_Actual_Type (Full_View (E), False);
14833 -- end if;
14834 end if;
14835
14836 -- An unusual case of aliasing: the actual may also be directly
14837 -- visible in the generic, and be private there, while it is fully
14838 -- visible in the context of the instance. The internal subtype
14839 -- is private in the instance but has full visibility like its
14840 -- parent in the enclosing scope. This enforces the invariant that
14841 -- the privacy status of all private dependents of a type coincide
14842 -- with that of the parent type. This can only happen when a
14843 -- generic child unit is instantiated within a sibling.
14844
14845 if Is_Private_Type (E)
14846 and then not Is_Private_Type (Etype (E))
14847 then
14848 Exchange_Declarations (E);
14849 end if;
14850
14851 elsif Ekind (E) = E_Package then
14852
14853 -- The end of the renaming list is the renaming of the generic
14854 -- package itself. If the instance is a subprogram, all entities
14855 -- in the corresponding package are renamings. If this entity is
14856 -- a formal package, make its own formals private as well. The
14857 -- actual in this case is itself the renaming of an instantiation.
14858 -- If the entity is not a package renaming, it is the entity
14859 -- created to validate formal package actuals: ignore it.
14860
14861 -- If the actual is itself a formal package for the enclosing
14862 -- generic, or the actual for such a formal package, it remains
14863 -- visible on exit from the instance, and therefore nothing needs
14864 -- to be done either, except to keep it accessible.
14865
14866 if Is_Package and then Renamed_Object (E) = Pack_Id then
14867 exit;
14868
14869 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
14870 null;
14871
14872 elsif
14873 Denotes_Formal_Package (Renamed_Object (E), True, Pack_Id)
14874 then
14875 Set_Is_Hidden (E, False);
14876
14877 else
14878 declare
14879 Act_P : constant Entity_Id := Renamed_Object (E);
14880 Id : Entity_Id;
14881
14882 begin
14883 Id := First_Entity (Act_P);
14884 while Present (Id)
14885 and then Id /= First_Private_Entity (Act_P)
14886 loop
14887 exit when Ekind (Id) = E_Package
14888 and then Renamed_Object (Id) = Act_P;
14889
14890 Set_Is_Hidden (Id, True);
14891 Set_Is_Potentially_Use_Visible (Id, In_Use (Act_P));
14892
14893 if Ekind (Id) = E_Package then
14894 Restore_Nested_Formal (Id);
14895 end if;
14896
14897 Next_Entity (Id);
14898 end loop;
14899 end;
14900 end if;
14901 end if;
14902
14903 Next_Entity (E);
14904 end loop;
14905 end Restore_Private_Views;
14906
14907 --------------
14908 -- Save_Env --
14909 --------------
14910
14911 procedure Save_Env
14912 (Gen_Unit : Entity_Id;
14913 Act_Unit : Entity_Id)
14914 is
14915 begin
14916 Init_Env;
14917 Set_Instance_Env (Gen_Unit, Act_Unit);
14918 end Save_Env;
14919
14920 ----------------------------
14921 -- Save_Global_References --
14922 ----------------------------
14923
14924 procedure Save_Global_References (Templ : Node_Id) is
14925
14926 -- ??? it is horrible to use global variables in highly recursive code
14927
14928 E : Entity_Id;
14929 -- The entity of the current associated node
14930
14931 Gen_Scope : Entity_Id;
14932 -- The scope of the generic for which references are being saved
14933
14934 N2 : Node_Id;
14935 -- The current associated node
14936
14937 function Is_Global (E : Entity_Id) return Boolean;
14938 -- Check whether entity is defined outside of generic unit. Examine the
14939 -- scope of an entity, and the scope of the scope, etc, until we find
14940 -- either Standard, in which case the entity is global, or the generic
14941 -- unit itself, which indicates that the entity is local. If the entity
14942 -- is the generic unit itself, as in the case of a recursive call, or
14943 -- the enclosing generic unit, if different from the current scope, then
14944 -- it is local as well, because it will be replaced at the point of
14945 -- instantiation. On the other hand, if it is a reference to a child
14946 -- unit of a common ancestor, which appears in an instantiation, it is
14947 -- global because it is used to denote a specific compilation unit at
14948 -- the time the instantiations will be analyzed.
14949
14950 procedure Qualify_Universal_Operands
14951 (Op : Node_Id;
14952 Func_Call : Node_Id);
14953 -- Op denotes a binary or unary operator in generic template Templ. Node
14954 -- Func_Call is the function call alternative of the operator within the
14955 -- the analyzed copy of the template. Change each operand which yields a
14956 -- universal type by wrapping it into a qualified expression
14957 --
14958 -- Actual_Typ'(Operand)
14959 --
14960 -- where Actual_Typ is the type of corresponding actual parameter of
14961 -- Operand in Func_Call.
14962
14963 procedure Reset_Entity (N : Node_Id);
14964 -- Save semantic information on global entity so that it is not resolved
14965 -- again at instantiation time.
14966
14967 procedure Save_Entity_Descendants (N : Node_Id);
14968 -- Apply Save_Global_References to the two syntactic descendants of
14969 -- non-terminal nodes that carry an Associated_Node and are processed
14970 -- through Reset_Entity. Once the global entity (if any) has been
14971 -- captured together with its type, only two syntactic descendants need
14972 -- to be traversed to complete the processing of the tree rooted at N.
14973 -- This applies to Selected_Components, Expanded_Names, and to Operator
14974 -- nodes. N can also be a character literal, identifier, or operator
14975 -- symbol node, but the call has no effect in these cases.
14976
14977 procedure Save_Global_Defaults (N1 : Node_Id; N2 : Node_Id);
14978 -- Default actuals in nested instances must be handled specially
14979 -- because there is no link to them from the original tree. When an
14980 -- actual subprogram is given by a default, we add an explicit generic
14981 -- association for it in the instantiation node. When we save the
14982 -- global references on the name of the instance, we recover the list
14983 -- of generic associations, and add an explicit one to the original
14984 -- generic tree, through which a global actual can be preserved.
14985 -- Similarly, if a child unit is instantiated within a sibling, in the
14986 -- context of the parent, we must preserve the identifier of the parent
14987 -- so that it can be properly resolved in a subsequent instantiation.
14988
14989 procedure Save_Global_Descendant (D : Union_Id);
14990 -- Apply Save_References recursively to the descendants of node D
14991
14992 procedure Save_References (N : Node_Id);
14993 -- This is the recursive procedure that does the work, once the
14994 -- enclosing generic scope has been established.
14995
14996 ---------------
14997 -- Is_Global --
14998 ---------------
14999
15000 function Is_Global (E : Entity_Id) return Boolean is
15001 Se : Entity_Id;
15002
15003 function Is_Instance_Node (Decl : Node_Id) return Boolean;
15004 -- Determine whether the parent node of a reference to a child unit
15005 -- denotes an instantiation or a formal package, in which case the
15006 -- reference to the child unit is global, even if it appears within
15007 -- the current scope (e.g. when the instance appears within the body
15008 -- of an ancestor).
15009
15010 ----------------------
15011 -- Is_Instance_Node --
15012 ----------------------
15013
15014 function Is_Instance_Node (Decl : Node_Id) return Boolean is
15015 begin
15016 return Nkind (Decl) in N_Generic_Instantiation
15017 or else
15018 Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration;
15019 end Is_Instance_Node;
15020
15021 -- Start of processing for Is_Global
15022
15023 begin
15024 if E = Gen_Scope then
15025 return False;
15026
15027 elsif E = Standard_Standard then
15028 return True;
15029
15030 elsif Is_Child_Unit (E)
15031 and then (Is_Instance_Node (Parent (N2))
15032 or else (Nkind (Parent (N2)) = N_Expanded_Name
15033 and then N2 = Selector_Name (Parent (N2))
15034 and then
15035 Is_Instance_Node (Parent (Parent (N2)))))
15036 then
15037 return True;
15038
15039 else
15040 Se := Scope (E);
15041 while Se /= Gen_Scope loop
15042 if Se = Standard_Standard then
15043 return True;
15044 else
15045 Se := Scope (Se);
15046 end if;
15047 end loop;
15048
15049 return False;
15050 end if;
15051 end Is_Global;
15052
15053 --------------------------------
15054 -- Qualify_Universal_Operands --
15055 --------------------------------
15056
15057 procedure Qualify_Universal_Operands
15058 (Op : Node_Id;
15059 Func_Call : Node_Id)
15060 is
15061 procedure Qualify_Operand (Opnd : Node_Id; Actual : Node_Id);
15062 -- Rewrite operand Opnd as a qualified expression of the form
15063 --
15064 -- Actual_Typ'(Opnd)
15065 --
15066 -- where Actual is the corresponding actual parameter of Opnd in
15067 -- function call Func_Call.
15068
15069 function Qualify_Type
15070 (Loc : Source_Ptr;
15071 Typ : Entity_Id) return Node_Id;
15072 -- Qualify type Typ by creating a selected component of the form
15073 --
15074 -- Scope_Of_Typ.Typ
15075
15076 ---------------------
15077 -- Qualify_Operand --
15078 ---------------------
15079
15080 procedure Qualify_Operand (Opnd : Node_Id; Actual : Node_Id) is
15081 Loc : constant Source_Ptr := Sloc (Opnd);
15082 Typ : constant Entity_Id := Etype (Actual);
15083 Mark : Node_Id;
15084 Qual : Node_Id;
15085
15086 begin
15087 -- Qualify the operand when it is of a universal type. Note that
15088 -- the template is unanalyzed and it is not possible to directly
15089 -- query the type. This transformation is not done when the type
15090 -- of the actual is internally generated because the type will be
15091 -- regenerated in the instance.
15092
15093 if Yields_Universal_Type (Opnd)
15094 and then Comes_From_Source (Typ)
15095 and then not Is_Hidden (Typ)
15096 then
15097 -- The type of the actual may be a global reference. Save this
15098 -- information by creating a reference to it.
15099
15100 if Is_Global (Typ) then
15101 Mark := New_Occurrence_Of (Typ, Loc);
15102
15103 -- Otherwise rely on resolution to find the proper type within
15104 -- the instance.
15105
15106 else
15107 Mark := Qualify_Type (Loc, Typ);
15108 end if;
15109
15110 Qual :=
15111 Make_Qualified_Expression (Loc,
15112 Subtype_Mark => Mark,
15113 Expression => Relocate_Node (Opnd));
15114
15115 -- Mark the qualification to distinguish it from other source
15116 -- constructs and signal the instantiation mechanism that this
15117 -- node requires special processing. See Copy_Generic_Node for
15118 -- details.
15119
15120 Set_Is_Qualified_Universal_Literal (Qual);
15121
15122 Rewrite (Opnd, Qual);
15123 end if;
15124 end Qualify_Operand;
15125
15126 ------------------
15127 -- Qualify_Type --
15128 ------------------
15129
15130 function Qualify_Type
15131 (Loc : Source_Ptr;
15132 Typ : Entity_Id) return Node_Id
15133 is
15134 Scop : constant Entity_Id := Scope (Typ);
15135 Result : Node_Id;
15136
15137 begin
15138 Result := Make_Identifier (Loc, Chars (Typ));
15139
15140 if Present (Scop) and then not Is_Generic_Unit (Scop) then
15141 Result :=
15142 Make_Selected_Component (Loc,
15143 Prefix => Make_Identifier (Loc, Chars (Scop)),
15144 Selector_Name => Result);
15145 end if;
15146
15147 return Result;
15148 end Qualify_Type;
15149
15150 -- Local variables
15151
15152 Actuals : constant List_Id := Parameter_Associations (Func_Call);
15153
15154 -- Start of processing for Qualify_Universal_Operands
15155
15156 begin
15157 if Nkind (Op) in N_Binary_Op then
15158 Qualify_Operand (Left_Opnd (Op), First (Actuals));
15159 Qualify_Operand (Right_Opnd (Op), Next (First (Actuals)));
15160
15161 elsif Nkind (Op) in N_Unary_Op then
15162 Qualify_Operand (Right_Opnd (Op), First (Actuals));
15163 end if;
15164 end Qualify_Universal_Operands;
15165
15166 ------------------
15167 -- Reset_Entity --
15168 ------------------
15169
15170 procedure Reset_Entity (N : Node_Id) is
15171 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id);
15172 -- If the type of N2 is global to the generic unit, save the type in
15173 -- the generic node. Just as we perform name capture for explicit
15174 -- references within the generic, we must capture the global types
15175 -- of local entities because they may participate in resolution in
15176 -- the instance.
15177
15178 function Top_Ancestor (E : Entity_Id) return Entity_Id;
15179 -- Find the ultimate ancestor of the current unit. If it is not a
15180 -- generic unit, then the name of the current unit in the prefix of
15181 -- an expanded name must be replaced with its generic homonym to
15182 -- ensure that it will be properly resolved in an instance.
15183
15184 ---------------------
15185 -- Set_Global_Type --
15186 ---------------------
15187
15188 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id) is
15189 Typ : constant Entity_Id := Etype (N2);
15190
15191 begin
15192 Set_Etype (N, Typ);
15193
15194 -- If the entity of N is not the associated node, this is a
15195 -- nested generic and it has an associated node as well, whose
15196 -- type is already the full view (see below). Indicate that the
15197 -- original node has a private view.
15198
15199 if Entity (N) /= N2 and then Has_Private_View (Entity (N)) then
15200 Set_Has_Private_View (N);
15201 end if;
15202
15203 -- If not a private type, nothing else to do
15204
15205 if not Is_Private_Type (Typ) then
15206 null;
15207
15208 -- If it is a derivation of a private type in a context where no
15209 -- full view is needed, nothing to do either.
15210
15211 elsif No (Full_View (Typ)) and then Typ /= Etype (Typ) then
15212 null;
15213
15214 -- Otherwise mark the type for flipping and use the full view when
15215 -- available.
15216
15217 else
15218 Set_Has_Private_View (N);
15219
15220 if Present (Full_View (Typ)) then
15221 Set_Etype (N2, Full_View (Typ));
15222 end if;
15223 end if;
15224
15225 if Is_Floating_Point_Type (Typ)
15226 and then Has_Dimension_System (Typ)
15227 then
15228 Copy_Dimensions (N2, N);
15229 end if;
15230 end Set_Global_Type;
15231
15232 ------------------
15233 -- Top_Ancestor --
15234 ------------------
15235
15236 function Top_Ancestor (E : Entity_Id) return Entity_Id is
15237 Par : Entity_Id;
15238
15239 begin
15240 Par := E;
15241 while Is_Child_Unit (Par) loop
15242 Par := Scope (Par);
15243 end loop;
15244
15245 return Par;
15246 end Top_Ancestor;
15247
15248 -- Start of processing for Reset_Entity
15249
15250 begin
15251 N2 := Get_Associated_Node (N);
15252 E := Entity (N2);
15253
15254 if Present (E) then
15255
15256 -- If the node is an entry call to an entry in an enclosing task,
15257 -- it is rewritten as a selected component. No global entity to
15258 -- preserve in this case, since the expansion will be redone in
15259 -- the instance.
15260
15261 if not Nkind_In (E, N_Defining_Character_Literal,
15262 N_Defining_Identifier,
15263 N_Defining_Operator_Symbol)
15264 then
15265 Set_Associated_Node (N, Empty);
15266 Set_Etype (N, Empty);
15267 return;
15268 end if;
15269
15270 -- If the entity is an itype created as a subtype of an access
15271 -- type with a null exclusion restore source entity for proper
15272 -- visibility. The itype will be created anew in the instance.
15273
15274 if Is_Itype (E)
15275 and then Ekind (E) = E_Access_Subtype
15276 and then Is_Entity_Name (N)
15277 and then Chars (Etype (E)) = Chars (N)
15278 then
15279 E := Etype (E);
15280 Set_Entity (N2, E);
15281 Set_Etype (N2, E);
15282 end if;
15283
15284 if Is_Global (E) then
15285 Set_Global_Type (N, N2);
15286
15287 elsif Nkind (N) = N_Op_Concat
15288 and then Is_Generic_Type (Etype (N2))
15289 and then (Base_Type (Etype (Right_Opnd (N2))) = Etype (N2)
15290 or else
15291 Base_Type (Etype (Left_Opnd (N2))) = Etype (N2))
15292 and then Is_Intrinsic_Subprogram (E)
15293 then
15294 null;
15295
15296 -- Entity is local. Mark generic node as unresolved. Note that now
15297 -- it does not have an entity.
15298
15299 else
15300 Set_Associated_Node (N, Empty);
15301 Set_Etype (N, Empty);
15302 end if;
15303
15304 if Nkind (Parent (N)) in N_Generic_Instantiation
15305 and then N = Name (Parent (N))
15306 then
15307 Save_Global_Defaults (Parent (N), Parent (N2));
15308 end if;
15309
15310 elsif Nkind (Parent (N)) = N_Selected_Component
15311 and then Nkind (Parent (N2)) = N_Expanded_Name
15312 then
15313 if Is_Global (Entity (Parent (N2))) then
15314 Change_Selected_Component_To_Expanded_Name (Parent (N));
15315 Set_Associated_Node (Parent (N), Parent (N2));
15316 Set_Global_Type (Parent (N), Parent (N2));
15317 Save_Entity_Descendants (N);
15318
15319 -- If this is a reference to the current generic entity, replace
15320 -- by the name of the generic homonym of the current package. This
15321 -- is because in an instantiation Par.P.Q will not resolve to the
15322 -- name of the instance, whose enclosing scope is not necessarily
15323 -- Par. We use the generic homonym rather that the name of the
15324 -- generic itself because it may be hidden by a local declaration.
15325
15326 elsif In_Open_Scopes (Entity (Parent (N2)))
15327 and then not
15328 Is_Generic_Unit (Top_Ancestor (Entity (Prefix (Parent (N2)))))
15329 then
15330 if Ekind (Entity (Parent (N2))) = E_Generic_Package then
15331 Rewrite (Parent (N),
15332 Make_Identifier (Sloc (N),
15333 Chars =>
15334 Chars (Generic_Homonym (Entity (Parent (N2))))));
15335 else
15336 Rewrite (Parent (N),
15337 Make_Identifier (Sloc (N),
15338 Chars => Chars (Selector_Name (Parent (N2)))));
15339 end if;
15340 end if;
15341
15342 if Nkind (Parent (Parent (N))) in N_Generic_Instantiation
15343 and then Parent (N) = Name (Parent (Parent (N)))
15344 then
15345 Save_Global_Defaults
15346 (Parent (Parent (N)), Parent (Parent (N2)));
15347 end if;
15348
15349 -- A selected component may denote a static constant that has been
15350 -- folded. If the static constant is global to the generic, capture
15351 -- its value. Otherwise the folding will happen in any instantiation.
15352
15353 elsif Nkind (Parent (N)) = N_Selected_Component
15354 and then Nkind_In (Parent (N2), N_Integer_Literal, N_Real_Literal)
15355 then
15356 if Present (Entity (Original_Node (Parent (N2))))
15357 and then Is_Global (Entity (Original_Node (Parent (N2))))
15358 then
15359 Rewrite (Parent (N), New_Copy (Parent (N2)));
15360 Set_Analyzed (Parent (N), False);
15361 end if;
15362
15363 -- A selected component may be transformed into a parameterless
15364 -- function call. If the called entity is global, rewrite the node
15365 -- appropriately, i.e. as an extended name for the global entity.
15366
15367 elsif Nkind (Parent (N)) = N_Selected_Component
15368 and then Nkind (Parent (N2)) = N_Function_Call
15369 and then N = Selector_Name (Parent (N))
15370 then
15371 if No (Parameter_Associations (Parent (N2))) then
15372 if Is_Global (Entity (Name (Parent (N2)))) then
15373 Change_Selected_Component_To_Expanded_Name (Parent (N));
15374 Set_Associated_Node (Parent (N), Name (Parent (N2)));
15375 Set_Global_Type (Parent (N), Name (Parent (N2)));
15376 Save_Entity_Descendants (N);
15377
15378 else
15379 Set_Is_Prefixed_Call (Parent (N));
15380 Set_Associated_Node (N, Empty);
15381 Set_Etype (N, Empty);
15382 end if;
15383
15384 -- In Ada 2005, X.F may be a call to a primitive operation,
15385 -- rewritten as F (X). This rewriting will be done again in an
15386 -- instance, so keep the original node. Global entities will be
15387 -- captured as for other constructs. Indicate that this must
15388 -- resolve as a call, to prevent accidental overloading in the
15389 -- instance, if both a component and a primitive operation appear
15390 -- as candidates.
15391
15392 else
15393 Set_Is_Prefixed_Call (Parent (N));
15394 end if;
15395
15396 -- Entity is local. Reset in generic unit, so that node is resolved
15397 -- anew at the point of instantiation.
15398
15399 else
15400 Set_Associated_Node (N, Empty);
15401 Set_Etype (N, Empty);
15402 end if;
15403 end Reset_Entity;
15404
15405 -----------------------------
15406 -- Save_Entity_Descendants --
15407 -----------------------------
15408
15409 procedure Save_Entity_Descendants (N : Node_Id) is
15410 begin
15411 case Nkind (N) is
15412 when N_Binary_Op =>
15413 Save_Global_Descendant (Union_Id (Left_Opnd (N)));
15414 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
15415
15416 when N_Unary_Op =>
15417 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
15418
15419 when N_Expanded_Name
15420 | N_Selected_Component
15421 =>
15422 Save_Global_Descendant (Union_Id (Prefix (N)));
15423 Save_Global_Descendant (Union_Id (Selector_Name (N)));
15424
15425 when N_Character_Literal
15426 | N_Identifier
15427 | N_Operator_Symbol
15428 =>
15429 null;
15430
15431 when others =>
15432 raise Program_Error;
15433 end case;
15434 end Save_Entity_Descendants;
15435
15436 --------------------------
15437 -- Save_Global_Defaults --
15438 --------------------------
15439
15440 procedure Save_Global_Defaults (N1 : Node_Id; N2 : Node_Id) is
15441 Loc : constant Source_Ptr := Sloc (N1);
15442 Assoc2 : constant List_Id := Generic_Associations (N2);
15443 Gen_Id : constant Entity_Id := Get_Generic_Entity (N2);
15444 Assoc1 : List_Id;
15445 Act1 : Node_Id;
15446 Act2 : Node_Id;
15447 Def : Node_Id;
15448 Ndec : Node_Id;
15449 Subp : Entity_Id;
15450 Actual : Entity_Id;
15451
15452 begin
15453 Assoc1 := Generic_Associations (N1);
15454
15455 if Present (Assoc1) then
15456 Act1 := First (Assoc1);
15457 else
15458 Act1 := Empty;
15459 Set_Generic_Associations (N1, New_List);
15460 Assoc1 := Generic_Associations (N1);
15461 end if;
15462
15463 if Present (Assoc2) then
15464 Act2 := First (Assoc2);
15465 else
15466 return;
15467 end if;
15468
15469 while Present (Act1) and then Present (Act2) loop
15470 Next (Act1);
15471 Next (Act2);
15472 end loop;
15473
15474 -- Find the associations added for default subprograms
15475
15476 if Present (Act2) then
15477 while Nkind (Act2) /= N_Generic_Association
15478 or else No (Entity (Selector_Name (Act2)))
15479 or else not Is_Overloadable (Entity (Selector_Name (Act2)))
15480 loop
15481 Next (Act2);
15482 end loop;
15483
15484 -- Add a similar association if the default is global. The
15485 -- renaming declaration for the actual has been analyzed, and
15486 -- its alias is the program it renames. Link the actual in the
15487 -- original generic tree with the node in the analyzed tree.
15488
15489 while Present (Act2) loop
15490 Subp := Entity (Selector_Name (Act2));
15491 Def := Explicit_Generic_Actual_Parameter (Act2);
15492
15493 -- Following test is defence against rubbish errors
15494
15495 if No (Alias (Subp)) then
15496 return;
15497 end if;
15498
15499 -- Retrieve the resolved actual from the renaming declaration
15500 -- created for the instantiated formal.
15501
15502 Actual := Entity (Name (Parent (Parent (Subp))));
15503 Set_Entity (Def, Actual);
15504 Set_Etype (Def, Etype (Actual));
15505
15506 if Is_Global (Actual) then
15507 Ndec :=
15508 Make_Generic_Association (Loc,
15509 Selector_Name =>
15510 New_Occurrence_Of (Subp, Loc),
15511 Explicit_Generic_Actual_Parameter =>
15512 New_Occurrence_Of (Actual, Loc));
15513
15514 Set_Associated_Node
15515 (Explicit_Generic_Actual_Parameter (Ndec), Def);
15516
15517 Append (Ndec, Assoc1);
15518
15519 -- If there are other defaults, add a dummy association in case
15520 -- there are other defaulted formals with the same name.
15521
15522 elsif Present (Next (Act2)) then
15523 Ndec :=
15524 Make_Generic_Association (Loc,
15525 Selector_Name =>
15526 New_Occurrence_Of (Subp, Loc),
15527 Explicit_Generic_Actual_Parameter => Empty);
15528
15529 Append (Ndec, Assoc1);
15530 end if;
15531
15532 Next (Act2);
15533 end loop;
15534 end if;
15535
15536 if Nkind (Name (N1)) = N_Identifier
15537 and then Is_Child_Unit (Gen_Id)
15538 and then Is_Global (Gen_Id)
15539 and then Is_Generic_Unit (Scope (Gen_Id))
15540 and then In_Open_Scopes (Scope (Gen_Id))
15541 then
15542 -- This is an instantiation of a child unit within a sibling, so
15543 -- that the generic parent is in scope. An eventual instance must
15544 -- occur within the scope of an instance of the parent. Make name
15545 -- in instance into an expanded name, to preserve the identifier
15546 -- of the parent, so it can be resolved subsequently.
15547
15548 Rewrite (Name (N2),
15549 Make_Expanded_Name (Loc,
15550 Chars => Chars (Gen_Id),
15551 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
15552 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
15553 Set_Entity (Name (N2), Gen_Id);
15554
15555 Rewrite (Name (N1),
15556 Make_Expanded_Name (Loc,
15557 Chars => Chars (Gen_Id),
15558 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
15559 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
15560
15561 Set_Associated_Node (Name (N1), Name (N2));
15562 Set_Associated_Node (Prefix (Name (N1)), Empty);
15563 Set_Associated_Node
15564 (Selector_Name (Name (N1)), Selector_Name (Name (N2)));
15565 Set_Etype (Name (N1), Etype (Gen_Id));
15566 end if;
15567 end Save_Global_Defaults;
15568
15569 ----------------------------
15570 -- Save_Global_Descendant --
15571 ----------------------------
15572
15573 procedure Save_Global_Descendant (D : Union_Id) is
15574 N1 : Node_Id;
15575
15576 begin
15577 if D in Node_Range then
15578 if D = Union_Id (Empty) then
15579 null;
15580
15581 elsif Nkind (Node_Id (D)) /= N_Compilation_Unit then
15582 Save_References (Node_Id (D));
15583 end if;
15584
15585 elsif D in List_Range then
15586 pragma Assert (D /= Union_Id (No_List));
15587 -- Because No_List = Empty, which is in Node_Range above
15588
15589 if Is_Empty_List (List_Id (D)) then
15590 null;
15591
15592 else
15593 N1 := First (List_Id (D));
15594 while Present (N1) loop
15595 Save_References (N1);
15596 Next (N1);
15597 end loop;
15598 end if;
15599
15600 -- Element list or other non-node field, nothing to do
15601
15602 else
15603 null;
15604 end if;
15605 end Save_Global_Descendant;
15606
15607 ---------------------
15608 -- Save_References --
15609 ---------------------
15610
15611 -- This is the recursive procedure that does the work once the enclosing
15612 -- generic scope has been established. We have to treat specially a
15613 -- number of node rewritings that are required by semantic processing
15614 -- and which change the kind of nodes in the generic copy: typically
15615 -- constant-folding, replacing an operator node by a string literal, or
15616 -- a selected component by an expanded name. In each of those cases, the
15617 -- transformation is propagated to the generic unit.
15618
15619 procedure Save_References (N : Node_Id) is
15620 Loc : constant Source_Ptr := Sloc (N);
15621
15622 function Requires_Delayed_Save (Nod : Node_Id) return Boolean;
15623 -- Determine whether arbitrary node Nod requires delayed capture of
15624 -- global references within its aspect specifications.
15625
15626 procedure Save_References_In_Aggregate (N : Node_Id);
15627 -- Save all global references in [extension] aggregate node N
15628
15629 procedure Save_References_In_Char_Lit_Or_Op_Symbol (N : Node_Id);
15630 -- Save all global references in a character literal or operator
15631 -- symbol denoted by N.
15632
15633 procedure Save_References_In_Descendants (N : Node_Id);
15634 -- Save all global references in all descendants of node N
15635
15636 procedure Save_References_In_Identifier (N : Node_Id);
15637 -- Save all global references in identifier node N
15638
15639 procedure Save_References_In_Operator (N : Node_Id);
15640 -- Save all global references in operator node N
15641
15642 procedure Save_References_In_Pragma (Prag : Node_Id);
15643 -- Save all global references found within the expression of pragma
15644 -- Prag.
15645
15646 ---------------------------
15647 -- Requires_Delayed_Save --
15648 ---------------------------
15649
15650 function Requires_Delayed_Save (Nod : Node_Id) return Boolean is
15651 begin
15652 -- Generic packages and subprograms require delayed capture of
15653 -- global references within their aspects due to the timing of
15654 -- annotation analysis.
15655
15656 if Nkind_In (Nod, N_Generic_Package_Declaration,
15657 N_Generic_Subprogram_Declaration,
15658 N_Package_Body,
15659 N_Package_Body_Stub,
15660 N_Subprogram_Body,
15661 N_Subprogram_Body_Stub)
15662 then
15663 -- Since the capture of global references is done on the
15664 -- unanalyzed generic template, there is no information around
15665 -- to infer the context. Use the Associated_Entity linkages to
15666 -- peek into the analyzed generic copy and determine what the
15667 -- template corresponds to.
15668
15669 if Nod = Templ then
15670 return
15671 Is_Generic_Declaration_Or_Body
15672 (Unit_Declaration_Node
15673 (Associated_Entity (Defining_Entity (Nod))));
15674
15675 -- Otherwise the generic unit being processed is not the top
15676 -- level template. It is safe to capture of global references
15677 -- within the generic unit because at this point the top level
15678 -- copy is fully analyzed.
15679
15680 else
15681 return False;
15682 end if;
15683
15684 -- Otherwise capture the global references without interference
15685
15686 else
15687 return False;
15688 end if;
15689 end Requires_Delayed_Save;
15690
15691 ----------------------------------
15692 -- Save_References_In_Aggregate --
15693 ----------------------------------
15694
15695 procedure Save_References_In_Aggregate (N : Node_Id) is
15696 Nam : Node_Id;
15697 Qual : Node_Id := Empty;
15698 Typ : Entity_Id := Empty;
15699
15700 use Atree.Unchecked_Access;
15701 -- This code section is part of implementing an untyped tree
15702 -- traversal, so it needs direct access to node fields.
15703
15704 begin
15705 N2 := Get_Associated_Node (N);
15706
15707 if Present (N2) then
15708 Typ := Etype (N2);
15709
15710 -- In an instance within a generic, use the name of the actual
15711 -- and not the original generic parameter. If the actual is
15712 -- global in the current generic it must be preserved for its
15713 -- instantiation.
15714
15715 if Nkind (Parent (Typ)) = N_Subtype_Declaration
15716 and then Present (Generic_Parent_Type (Parent (Typ)))
15717 then
15718 Typ := Base_Type (Typ);
15719 Set_Etype (N2, Typ);
15720 end if;
15721 end if;
15722
15723 if No (N2) or else No (Typ) or else not Is_Global (Typ) then
15724 Set_Associated_Node (N, Empty);
15725
15726 -- If the aggregate is an actual in a call, it has been
15727 -- resolved in the current context, to some local type. The
15728 -- enclosing call may have been disambiguated by the aggregate,
15729 -- and this disambiguation might fail at instantiation time
15730 -- because the type to which the aggregate did resolve is not
15731 -- preserved. In order to preserve some of this information,
15732 -- wrap the aggregate in a qualified expression, using the id
15733 -- of its type. For further disambiguation we qualify the type
15734 -- name with its scope (if visible and not hidden by a local
15735 -- homograph) because both id's will have corresponding
15736 -- entities in an instance. This resolves most of the problems
15737 -- with missing type information on aggregates in instances.
15738
15739 if Present (N2)
15740 and then Nkind (N2) = Nkind (N)
15741 and then Nkind (Parent (N2)) in N_Subprogram_Call
15742 and then Present (Typ)
15743 and then Comes_From_Source (Typ)
15744 then
15745 Nam := Make_Identifier (Loc, Chars (Typ));
15746
15747 if Is_Immediately_Visible (Scope (Typ))
15748 and then
15749 (not In_Open_Scopes (Scope (Typ))
15750 or else Current_Entity (Scope (Typ)) = Scope (Typ))
15751 then
15752 Nam :=
15753 Make_Selected_Component (Loc,
15754 Prefix =>
15755 Make_Identifier (Loc, Chars (Scope (Typ))),
15756 Selector_Name => Nam);
15757 end if;
15758
15759 Qual :=
15760 Make_Qualified_Expression (Loc,
15761 Subtype_Mark => Nam,
15762 Expression => Relocate_Node (N));
15763 end if;
15764 end if;
15765
15766 Save_Global_Descendant (Field1 (N));
15767 Save_Global_Descendant (Field2 (N));
15768 Save_Global_Descendant (Field3 (N));
15769 Save_Global_Descendant (Field5 (N));
15770
15771 if Present (Qual) then
15772 Rewrite (N, Qual);
15773 end if;
15774 end Save_References_In_Aggregate;
15775
15776 ----------------------------------------------
15777 -- Save_References_In_Char_Lit_Or_Op_Symbol --
15778 ----------------------------------------------
15779
15780 procedure Save_References_In_Char_Lit_Or_Op_Symbol (N : Node_Id) is
15781 begin
15782 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
15783 Reset_Entity (N);
15784
15785 elsif Nkind (N) = N_Operator_Symbol
15786 and then Nkind (Get_Associated_Node (N)) = N_String_Literal
15787 then
15788 Change_Operator_Symbol_To_String_Literal (N);
15789 end if;
15790 end Save_References_In_Char_Lit_Or_Op_Symbol;
15791
15792 ------------------------------------
15793 -- Save_References_In_Descendants --
15794 ------------------------------------
15795
15796 procedure Save_References_In_Descendants (N : Node_Id) is
15797 use Atree.Unchecked_Access;
15798 -- This code section is part of implementing an untyped tree
15799 -- traversal, so it needs direct access to node fields.
15800
15801 begin
15802 Save_Global_Descendant (Field1 (N));
15803 Save_Global_Descendant (Field2 (N));
15804 Save_Global_Descendant (Field3 (N));
15805 Save_Global_Descendant (Field4 (N));
15806 Save_Global_Descendant (Field5 (N));
15807 end Save_References_In_Descendants;
15808
15809 -----------------------------------
15810 -- Save_References_In_Identifier --
15811 -----------------------------------
15812
15813 procedure Save_References_In_Identifier (N : Node_Id) is
15814 begin
15815 -- The node did not undergo a transformation
15816
15817 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
15818 -- If this is a discriminant reference, always save it.
15819 -- It is used in the instance to find the corresponding
15820 -- discriminant positionally rather than by name.
15821
15822 Set_Original_Discriminant
15823 (N, Original_Discriminant (Get_Associated_Node (N)));
15824
15825 Reset_Entity (N);
15826
15827 -- The analysis of the generic copy transformed the identifier
15828 -- into another construct. Propagate the changes to the template.
15829
15830 else
15831 N2 := Get_Associated_Node (N);
15832
15833 -- The identifier denotes a call to a parameterless function.
15834 -- Mark the node as resolved when the function is external.
15835
15836 if Nkind (N2) = N_Function_Call then
15837 E := Entity (Name (N2));
15838
15839 if Present (E) and then Is_Global (E) then
15840 Set_Etype (N, Etype (N2));
15841 else
15842 Set_Associated_Node (N, Empty);
15843 Set_Etype (N, Empty);
15844 end if;
15845
15846 -- The identifier denotes a named number that was constant
15847 -- folded. Preserve the original name for ASIS and undo the
15848 -- constant folding which will be repeated in the instance.
15849 -- Is this still needed???
15850
15851 elsif Nkind_In (N2, N_Integer_Literal, N_Real_Literal)
15852 and then Is_Entity_Name (Original_Node (N2))
15853 then
15854 Set_Associated_Node (N, Original_Node (N2));
15855 Reset_Entity (N);
15856
15857 -- The identifier resolved to a string literal. Propagate this
15858 -- information to the generic template.
15859
15860 elsif Nkind (N2) = N_String_Literal then
15861 Rewrite (N, New_Copy (N2));
15862
15863 -- The identifier is rewritten as a dereference if it is the
15864 -- prefix of an implicit dereference. Preserve the original
15865 -- tree as the analysis of the instance will expand the node
15866 -- again, but preserve the resolved entity if it is global.
15867
15868 elsif Nkind (N2) = N_Explicit_Dereference then
15869 if Is_Entity_Name (Prefix (N2))
15870 and then Present (Entity (Prefix (N2)))
15871 and then Is_Global (Entity (Prefix (N2)))
15872 then
15873 Set_Associated_Node (N, Prefix (N2));
15874
15875 elsif Nkind (Prefix (N2)) = N_Function_Call
15876 and then Present (Entity (Name (Prefix (N2))))
15877 and then Is_Global (Entity (Name (Prefix (N2))))
15878 then
15879 Rewrite (N,
15880 Make_Explicit_Dereference (Loc,
15881 Prefix =>
15882 Make_Function_Call (Loc,
15883 Name =>
15884 New_Occurrence_Of
15885 (Entity (Name (Prefix (N2))), Loc))));
15886
15887 else
15888 Set_Associated_Node (N, Empty);
15889 Set_Etype (N, Empty);
15890 end if;
15891
15892 -- The subtype mark of a nominally unconstrained object is
15893 -- rewritten as a subtype indication using the bounds of the
15894 -- expression. Recover the original subtype mark.
15895
15896 elsif Nkind (N2) = N_Subtype_Indication
15897 and then Is_Entity_Name (Original_Node (N2))
15898 then
15899 Set_Associated_Node (N, Original_Node (N2));
15900 Reset_Entity (N);
15901 end if;
15902 end if;
15903 end Save_References_In_Identifier;
15904
15905 ---------------------------------
15906 -- Save_References_In_Operator --
15907 ---------------------------------
15908
15909 procedure Save_References_In_Operator (N : Node_Id) is
15910 begin
15911 -- The node did not undergo a transformation
15912
15913 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
15914 if Nkind (N) = N_Op_Concat then
15915 Set_Is_Component_Left_Opnd (N,
15916 Is_Component_Left_Opnd (Get_Associated_Node (N)));
15917
15918 Set_Is_Component_Right_Opnd (N,
15919 Is_Component_Right_Opnd (Get_Associated_Node (N)));
15920 end if;
15921
15922 Reset_Entity (N);
15923
15924 -- The analysis of the generic copy transformed the operator into
15925 -- some other construct. Propagate the changes to the template if
15926 -- applicable.
15927
15928 else
15929 N2 := Get_Associated_Node (N);
15930
15931 -- The operator resoved to a function call
15932
15933 if Nkind (N2) = N_Function_Call then
15934
15935 -- Add explicit qualifications in the generic template for
15936 -- all operands of universal type. This aids resolution by
15937 -- preserving the actual type of a literal or an attribute
15938 -- that yields a universal result.
15939
15940 Qualify_Universal_Operands (N, N2);
15941
15942 E := Entity (Name (N2));
15943
15944 if Present (E) and then Is_Global (E) then
15945 Set_Etype (N, Etype (N2));
15946 else
15947 Set_Associated_Node (N, Empty);
15948 Set_Etype (N, Empty);
15949 end if;
15950
15951 -- The operator was folded into a literal
15952
15953 elsif Nkind_In (N2, N_Integer_Literal,
15954 N_Real_Literal,
15955 N_String_Literal)
15956 then
15957 if Present (Original_Node (N2))
15958 and then Nkind (Original_Node (N2)) = Nkind (N)
15959 then
15960 -- Operation was constant-folded. Whenever possible,
15961 -- recover semantic information from unfolded node.
15962 -- This was initially done for ASIS but is apparently
15963 -- needed also for e.g. compiling a-nbnbin.adb.
15964
15965 Set_Associated_Node (N, Original_Node (N2));
15966
15967 if Nkind (N) = N_Op_Concat then
15968 Set_Is_Component_Left_Opnd (N,
15969 Is_Component_Left_Opnd (Get_Associated_Node (N)));
15970 Set_Is_Component_Right_Opnd (N,
15971 Is_Component_Right_Opnd (Get_Associated_Node (N)));
15972 end if;
15973
15974 Reset_Entity (N);
15975
15976 -- Propagate the constant folding back to the template
15977
15978 else
15979 Rewrite (N, New_Copy (N2));
15980 Set_Analyzed (N, False);
15981 end if;
15982
15983 -- The operator was folded into an enumeration literal. Retain
15984 -- the entity to avoid spurious ambiguities if it is overloaded
15985 -- at the point of instantiation or inlining.
15986
15987 elsif Nkind (N2) = N_Identifier
15988 and then Ekind (Entity (N2)) = E_Enumeration_Literal
15989 then
15990 Rewrite (N, New_Copy (N2));
15991 Set_Analyzed (N, False);
15992 end if;
15993 end if;
15994
15995 -- Complete the operands check if node has not been constant
15996 -- folded.
15997
15998 if Nkind (N) in N_Op then
15999 Save_Entity_Descendants (N);
16000 end if;
16001 end Save_References_In_Operator;
16002
16003 -------------------------------
16004 -- Save_References_In_Pragma --
16005 -------------------------------
16006
16007 procedure Save_References_In_Pragma (Prag : Node_Id) is
16008 Context : Node_Id;
16009 Do_Save : Boolean := True;
16010
16011 use Atree.Unchecked_Access;
16012 -- This code section is part of implementing an untyped tree
16013 -- traversal, so it needs direct access to node fields.
16014
16015 begin
16016 -- Do not save global references in pragmas generated from aspects
16017 -- because the pragmas will be regenerated at instantiation time.
16018
16019 if From_Aspect_Specification (Prag) then
16020 Do_Save := False;
16021
16022 -- The capture of global references within contract-related source
16023 -- pragmas associated with generic packages, subprograms or their
16024 -- respective bodies must be delayed due to timing of annotation
16025 -- analysis. Global references are still captured in routine
16026 -- Save_Global_References_In_Contract.
16027
16028 elsif Is_Generic_Contract_Pragma (Prag) and then Prag /= Templ then
16029 if Is_Package_Contract_Annotation (Prag) then
16030 Context := Find_Related_Package_Or_Body (Prag);
16031 else
16032 pragma Assert (Is_Subprogram_Contract_Annotation (Prag));
16033 Context := Find_Related_Declaration_Or_Body (Prag);
16034 end if;
16035
16036 -- The use of Original_Node accounts for the case when the
16037 -- related context is generic template.
16038
16039 if Requires_Delayed_Save (Original_Node (Context)) then
16040 Do_Save := False;
16041 end if;
16042 end if;
16043
16044 -- For all other cases, save all global references within the
16045 -- descendants, but skip the following semantic fields:
16046
16047 -- Field1 - Next_Pragma
16048 -- Field3 - Corresponding_Aspect
16049 -- Field5 - Next_Rep_Item
16050
16051 if Do_Save then
16052 Save_Global_Descendant (Field2 (Prag));
16053 Save_Global_Descendant (Field4 (Prag));
16054 end if;
16055 end Save_References_In_Pragma;
16056
16057 -- Start of processing for Save_References
16058
16059 begin
16060 if N = Empty then
16061 null;
16062
16063 -- Aggregates
16064
16065 elsif Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
16066 Save_References_In_Aggregate (N);
16067
16068 -- Character literals, operator symbols
16069
16070 elsif Nkind_In (N, N_Character_Literal, N_Operator_Symbol) then
16071 Save_References_In_Char_Lit_Or_Op_Symbol (N);
16072
16073 -- Defining identifiers
16074
16075 elsif Nkind (N) in N_Entity then
16076 null;
16077
16078 -- Identifiers
16079
16080 elsif Nkind (N) = N_Identifier then
16081 Save_References_In_Identifier (N);
16082
16083 -- Operators
16084
16085 elsif Nkind (N) in N_Op then
16086 Save_References_In_Operator (N);
16087
16088 -- Pragmas
16089
16090 elsif Nkind (N) = N_Pragma then
16091 Save_References_In_Pragma (N);
16092
16093 else
16094 Save_References_In_Descendants (N);
16095 end if;
16096
16097 -- Save all global references found within the aspect specifications
16098 -- of the related node.
16099
16100 if Permits_Aspect_Specifications (N) and then Has_Aspects (N) then
16101
16102 -- The capture of global references within aspects associated with
16103 -- generic packages, subprograms or their bodies must be delayed
16104 -- due to timing of annotation analysis. Global references are
16105 -- still captured in routine Save_Global_References_In_Contract.
16106
16107 if Requires_Delayed_Save (N) then
16108 null;
16109
16110 -- Otherwise save all global references within the aspects
16111
16112 else
16113 Save_Global_References_In_Aspects (N);
16114 end if;
16115 end if;
16116 end Save_References;
16117
16118 -- Start of processing for Save_Global_References
16119
16120 begin
16121 Gen_Scope := Current_Scope;
16122
16123 -- If the generic unit is a child unit, references to entities in the
16124 -- parent are treated as local, because they will be resolved anew in
16125 -- the context of the instance of the parent.
16126
16127 while Is_Child_Unit (Gen_Scope)
16128 and then Ekind (Scope (Gen_Scope)) = E_Generic_Package
16129 loop
16130 Gen_Scope := Scope (Gen_Scope);
16131 end loop;
16132
16133 Save_References (Templ);
16134 end Save_Global_References;
16135
16136 ---------------------------------------
16137 -- Save_Global_References_In_Aspects --
16138 ---------------------------------------
16139
16140 procedure Save_Global_References_In_Aspects (N : Node_Id) is
16141 Asp : Node_Id;
16142 Expr : Node_Id;
16143
16144 begin
16145 Asp := First (Aspect_Specifications (N));
16146 while Present (Asp) loop
16147 Expr := Expression (Asp);
16148
16149 if Present (Expr) then
16150 Save_Global_References (Expr);
16151 end if;
16152
16153 Next (Asp);
16154 end loop;
16155 end Save_Global_References_In_Aspects;
16156
16157 ------------------------------------------
16158 -- Set_Copied_Sloc_For_Inherited_Pragma --
16159 ------------------------------------------
16160
16161 procedure Set_Copied_Sloc_For_Inherited_Pragma
16162 (N : Node_Id;
16163 E : Entity_Id)
16164 is
16165 begin
16166 Create_Instantiation_Source (N, E,
16167 Inlined_Body => False,
16168 Inherited_Pragma => True,
16169 Factor => S_Adjustment);
16170 end Set_Copied_Sloc_For_Inherited_Pragma;
16171
16172 --------------------------------------
16173 -- Set_Copied_Sloc_For_Inlined_Body --
16174 --------------------------------------
16175
16176 procedure Set_Copied_Sloc_For_Inlined_Body (N : Node_Id; E : Entity_Id) is
16177 begin
16178 Create_Instantiation_Source (N, E,
16179 Inlined_Body => True,
16180 Inherited_Pragma => False,
16181 Factor => S_Adjustment);
16182 end Set_Copied_Sloc_For_Inlined_Body;
16183
16184 ---------------------
16185 -- Set_Instance_Of --
16186 ---------------------
16187
16188 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id) is
16189 begin
16190 Generic_Renamings.Table (Generic_Renamings.Last) := (A, B, Assoc_Null);
16191 Generic_Renamings_HTable.Set (Generic_Renamings.Last);
16192 Generic_Renamings.Increment_Last;
16193 end Set_Instance_Of;
16194
16195 --------------------
16196 -- Set_Next_Assoc --
16197 --------------------
16198
16199 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr) is
16200 begin
16201 Generic_Renamings.Table (E).Next_In_HTable := Next;
16202 end Set_Next_Assoc;
16203
16204 -------------------
16205 -- Start_Generic --
16206 -------------------
16207
16208 procedure Start_Generic is
16209 begin
16210 -- ??? More things could be factored out in this routine.
16211 -- Should probably be done at a later stage.
16212
16213 Generic_Flags.Append (Inside_A_Generic);
16214 Inside_A_Generic := True;
16215
16216 Expander_Mode_Save_And_Set (False);
16217 end Start_Generic;
16218
16219 ----------------------
16220 -- Set_Instance_Env --
16221 ----------------------
16222
16223 -- WARNING: This routine manages SPARK regions
16224
16225 procedure Set_Instance_Env
16226 (Gen_Unit : Entity_Id;
16227 Act_Unit : Entity_Id)
16228 is
16229 Saved_AE : constant Boolean := Assertions_Enabled;
16230 Saved_CPL : constant Node_Id := Check_Policy_List;
16231 Saved_DEC : constant Boolean := Dynamic_Elaboration_Checks;
16232 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
16233 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
16234
16235 begin
16236 -- Regardless of the current mode, predefined units are analyzed in the
16237 -- most current Ada mode, and earlier version Ada checks do not apply
16238 -- to predefined units. Nothing needs to be done for non-internal units.
16239 -- These are always analyzed in the current mode.
16240
16241 if In_Internal_Unit (Gen_Unit) then
16242
16243 -- The following call resets all configuration attributes to default
16244 -- or the xxx_Config versions of the attributes when the current sem
16245 -- unit is the main unit. At the same time, internal units must also
16246 -- inherit certain configuration attributes from their context. It
16247 -- is unclear what these two sets are.
16248
16249 Set_Config_Switches (True, Current_Sem_Unit = Main_Unit);
16250
16251 -- Reinstall relevant configuration attributes of the context
16252
16253 Assertions_Enabled := Saved_AE;
16254 Check_Policy_List := Saved_CPL;
16255 Dynamic_Elaboration_Checks := Saved_DEC;
16256
16257 Install_SPARK_Mode (Saved_SM, Saved_SMP);
16258 end if;
16259
16260 Current_Instantiated_Parent :=
16261 (Gen_Id => Gen_Unit,
16262 Act_Id => Act_Unit,
16263 Next_In_HTable => Assoc_Null);
16264 end Set_Instance_Env;
16265
16266 -----------------
16267 -- Switch_View --
16268 -----------------
16269
16270 procedure Switch_View (T : Entity_Id) is
16271 BT : constant Entity_Id := Base_Type (T);
16272 Priv_Elmt : Elmt_Id := No_Elmt;
16273 Priv_Sub : Entity_Id;
16274
16275 begin
16276 -- T may be private but its base type may have been exchanged through
16277 -- some other occurrence, in which case there is nothing to switch
16278 -- besides T itself. Note that a private dependent subtype of a private
16279 -- type might not have been switched even if the base type has been,
16280 -- because of the last branch of Check_Private_View (see comment there).
16281
16282 if not Is_Private_Type (BT) then
16283 Prepend_Elmt (Full_View (T), Exchanged_Views);
16284 Exchange_Declarations (T);
16285 return;
16286 end if;
16287
16288 Priv_Elmt := First_Elmt (Private_Dependents (BT));
16289
16290 if Present (Full_View (BT)) then
16291 Prepend_Elmt (Full_View (BT), Exchanged_Views);
16292 Exchange_Declarations (BT);
16293 end if;
16294
16295 while Present (Priv_Elmt) loop
16296 Priv_Sub := (Node (Priv_Elmt));
16297
16298 -- We avoid flipping the subtype if the Etype of its full view is
16299 -- private because this would result in a malformed subtype. This
16300 -- occurs when the Etype of the subtype full view is the full view of
16301 -- the base type (and since the base types were just switched, the
16302 -- subtype is pointing to the wrong view). This is currently the case
16303 -- for tagged record types, access types (maybe more?) and needs to
16304 -- be resolved. ???
16305
16306 if Present (Full_View (Priv_Sub))
16307 and then not Is_Private_Type (Etype (Full_View (Priv_Sub)))
16308 then
16309 Prepend_Elmt (Full_View (Priv_Sub), Exchanged_Views);
16310 Exchange_Declarations (Priv_Sub);
16311 end if;
16312
16313 Next_Elmt (Priv_Elmt);
16314 end loop;
16315 end Switch_View;
16316
16317 -----------------
16318 -- True_Parent --
16319 -----------------
16320
16321 function True_Parent (N : Node_Id) return Node_Id is
16322 begin
16323 if Nkind (Parent (N)) = N_Subunit then
16324 return Parent (Corresponding_Stub (Parent (N)));
16325 else
16326 return Parent (N);
16327 end if;
16328 end True_Parent;
16329
16330 -----------------------------
16331 -- Valid_Default_Attribute --
16332 -----------------------------
16333
16334 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id) is
16335 Attr_Id : constant Attribute_Id :=
16336 Get_Attribute_Id (Attribute_Name (Def));
16337 T : constant Entity_Id := Entity (Prefix (Def));
16338 Is_Fun : constant Boolean := (Ekind (Nam) = E_Function);
16339 F : Entity_Id;
16340 Num_F : Nat;
16341 OK : Boolean;
16342
16343 begin
16344 if No (T) or else T = Any_Id then
16345 return;
16346 end if;
16347
16348 Num_F := 0;
16349 F := First_Formal (Nam);
16350 while Present (F) loop
16351 Num_F := Num_F + 1;
16352 Next_Formal (F);
16353 end loop;
16354
16355 case Attr_Id is
16356 when Attribute_Adjacent
16357 | Attribute_Ceiling
16358 | Attribute_Copy_Sign
16359 | Attribute_Floor
16360 | Attribute_Fraction
16361 | Attribute_Machine
16362 | Attribute_Model
16363 | Attribute_Remainder
16364 | Attribute_Rounding
16365 | Attribute_Unbiased_Rounding
16366 =>
16367 OK := Is_Fun
16368 and then Num_F = 1
16369 and then Is_Floating_Point_Type (T);
16370
16371 when Attribute_Image
16372 | Attribute_Pred
16373 | Attribute_Succ
16374 | Attribute_Value
16375 | Attribute_Wide_Image
16376 | Attribute_Wide_Value
16377 =>
16378 OK := Is_Fun and then Num_F = 1 and then Is_Scalar_Type (T);
16379
16380 when Attribute_Max
16381 | Attribute_Min
16382 =>
16383 OK := Is_Fun and then Num_F = 2 and then Is_Scalar_Type (T);
16384
16385 when Attribute_Input =>
16386 OK := (Is_Fun and then Num_F = 1);
16387
16388 when Attribute_Output
16389 | Attribute_Read
16390 | Attribute_Write
16391 =>
16392 OK := not Is_Fun and then Num_F = 2;
16393
16394 when others =>
16395 OK := False;
16396 end case;
16397
16398 if not OK then
16399 Error_Msg_N
16400 ("attribute reference has wrong profile for subprogram", Def);
16401 end if;
16402 end Valid_Default_Attribute;
16403
16404 end Sem_Ch12;