[Ada] Compiler crash on instance with overloaded actual and aspects
[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 function Build_Subprogram_Decl_Wrapper
499 (Formal_Subp : Entity_Id) return Node_Id;
500 -- Ada 2020 allows formal subprograms to carry pre/postconditions.
501 -- At the point of instantiation these contracts apply to uses of
502 -- the actual subprogram. This is implemented by creating wrapper
503 -- subprograms instead of the renamings previously used to link
504 -- formal subprograms and the corresponding actuals. If the actual
505 -- is not an entity (e.g. an attribute reference) a renaming is
506 -- created to handle the expansion of the attribute.
507
508 function Build_Subprogram_Body_Wrapper
509 (Formal_Subp : Entity_Id;
510 Actual_Name : Node_Id) return Node_Id;
511 -- The body of the wrapper is a call to the actual, with the generated
512 -- pre/postconditon checks added.
513
514 procedure Check_Access_Definition (N : Node_Id);
515 -- Subsidiary routine to null exclusion processing. Perform an assertion
516 -- check on Ada version and the presence of an access definition in N.
517
518 procedure Check_Formal_Packages (P_Id : Entity_Id);
519 -- Apply the following to all formal packages in generic associations.
520 -- Restore the visibility of the formals of the instance that are not
521 -- defaulted (see RM 12.7 (10)). Remove the anonymous package declaration
522 -- created for formal instances that are not defaulted.
523
524 procedure Check_Formal_Package_Instance
525 (Formal_Pack : Entity_Id;
526 Actual_Pack : Entity_Id);
527 -- Verify that the actuals of the actual instance match the actuals of
528 -- the template for a formal package that is not declared with a box.
529
530 procedure Check_Forward_Instantiation (Decl : Node_Id);
531 -- If the generic is a local entity and the corresponding body has not
532 -- been seen yet, flag enclosing packages to indicate that it will be
533 -- elaborated after the generic body. Subprograms declared in the same
534 -- package cannot be inlined by the front end because front-end inlining
535 -- requires a strict linear order of elaboration.
536
537 function Check_Hidden_Primitives (Assoc_List : List_Id) return Elist_Id;
538 -- Check if some association between formals and actuals requires to make
539 -- visible primitives of a tagged type, and make those primitives visible.
540 -- Return the list of primitives whose visibility is modified (to restore
541 -- their visibility later through Restore_Hidden_Primitives). If no
542 -- candidate is found then return No_Elist.
543
544 procedure Check_Hidden_Child_Unit
545 (N : Node_Id;
546 Gen_Unit : Entity_Id;
547 Act_Decl_Id : Entity_Id);
548 -- If the generic unit is an implicit child instance within a parent
549 -- instance, we need to make an explicit test that it is not hidden by
550 -- a child instance of the same name and parent.
551
552 procedure Check_Generic_Actuals
553 (Instance : Entity_Id;
554 Is_Formal_Box : Boolean);
555 -- Similar to previous one. Check the actuals in the instantiation,
556 -- whose views can change between the point of instantiation and the point
557 -- of instantiation of the body. In addition, mark the generic renamings
558 -- as generic actuals, so that they are not compatible with other actuals.
559 -- Recurse on an actual that is a formal package whose declaration has
560 -- a box.
561
562 function Contains_Instance_Of
563 (Inner : Entity_Id;
564 Outer : Entity_Id;
565 N : Node_Id) return Boolean;
566 -- Inner is instantiated within the generic Outer. Check whether Inner
567 -- directly or indirectly contains an instance of Outer or of one of its
568 -- parents, in the case of a subunit. Each generic unit holds a list of
569 -- the entities instantiated within (at any depth). This procedure
570 -- determines whether the set of such lists contains a cycle, i.e. an
571 -- illegal circular instantiation.
572
573 function Denotes_Formal_Package
574 (Pack : Entity_Id;
575 On_Exit : Boolean := False;
576 Instance : Entity_Id := Empty) return Boolean;
577 -- Returns True if E is a formal package of an enclosing generic, or
578 -- the actual for such a formal in an enclosing instantiation. If such
579 -- a package is used as a formal in an nested generic, or as an actual
580 -- in a nested instantiation, the visibility of ITS formals should not
581 -- be modified. When called from within Restore_Private_Views, the flag
582 -- On_Exit is true, to indicate that the search for a possible enclosing
583 -- instance should ignore the current one. In that case Instance denotes
584 -- the declaration for which this is an actual. This declaration may be
585 -- an instantiation in the source, or the internal instantiation that
586 -- corresponds to the actual for a formal package.
587
588 function Earlier (N1, N2 : Node_Id) return Boolean;
589 -- Yields True if N1 and N2 appear in the same compilation unit,
590 -- ignoring subunits, and if N1 is to the left of N2 in a left-to-right
591 -- traversal of the tree for the unit. Used to determine the placement
592 -- of freeze nodes for instance bodies that may depend on other instances.
593
594 function Find_Actual_Type
595 (Typ : Entity_Id;
596 Gen_Type : Entity_Id) return Entity_Id;
597 -- When validating the actual types of a child instance, check whether
598 -- the formal is a formal type of the parent unit, and retrieve the current
599 -- actual for it. Typ is the entity in the analyzed formal type declaration
600 -- (component or index type of an array type, or designated type of an
601 -- access formal) and Gen_Type is the enclosing analyzed formal array
602 -- or access type. The desired actual may be a formal of a parent, or may
603 -- be declared in a formal package of a parent. In both cases it is a
604 -- generic actual type because it appears within a visible instance.
605 -- Finally, it may be declared in a parent unit without being a formal
606 -- of that unit, in which case it must be retrieved by visibility.
607 -- Ambiguities may still arise if two homonyms are declared in two formal
608 -- packages, and the prefix of the formal type may be needed to resolve
609 -- the ambiguity in the instance ???
610
611 procedure Freeze_Subprogram_Body
612 (Inst_Node : Node_Id;
613 Gen_Body : Node_Id;
614 Pack_Id : Entity_Id);
615 -- The generic body may appear textually after the instance, including
616 -- in the proper body of a stub, or within a different package instance.
617 -- Given that the instance can only be elaborated after the generic, we
618 -- place freeze_nodes for the instance and/or for packages that may enclose
619 -- the instance and the generic, so that the back-end can establish the
620 -- proper order of elaboration.
621
622 function Get_Associated_Node (N : Node_Id) return Node_Id;
623 -- In order to propagate semantic information back from the analyzed copy
624 -- to the original generic, we maintain links between selected nodes in the
625 -- generic and their corresponding copies. At the end of generic analysis,
626 -- the routine Save_Global_References traverses the generic tree, examines
627 -- the semantic information, and preserves the links to those nodes that
628 -- contain global information. At instantiation, the information from the
629 -- associated node is placed on the new copy, so that name resolution is
630 -- not repeated.
631 --
632 -- Three kinds of source nodes have associated nodes:
633 --
634 -- a) those that can reference (denote) entities, that is identifiers,
635 -- character literals, expanded_names, operator symbols, operators,
636 -- and attribute reference nodes. These nodes have an Entity field
637 -- and are the set of nodes that are in N_Has_Entity.
638 --
639 -- b) aggregates (N_Aggregate and N_Extension_Aggregate)
640 --
641 -- c) selected components (N_Selected_Component)
642 --
643 -- For the first class, the associated node preserves the entity if it is
644 -- global. If the generic contains nested instantiations, the associated
645 -- node itself has been recopied, and a chain of them must be followed.
646 --
647 -- For aggregates, the associated node allows retrieval of the type, which
648 -- may otherwise not appear in the generic. The view of this type may be
649 -- different between generic and instantiation, and the full view can be
650 -- installed before the instantiation is analyzed. For aggregates of type
651 -- extensions, the same view exchange may have to be performed for some of
652 -- the ancestor types, if their view is private at the point of
653 -- instantiation.
654 --
655 -- Nodes that are selected components in the parse tree may be rewritten
656 -- as expanded names after resolution, and must be treated as potential
657 -- entity holders, which is why they also have an Associated_Node.
658 --
659 -- Nodes that do not come from source, such as freeze nodes, do not appear
660 -- in the generic tree, and need not have an associated node.
661 --
662 -- The associated node is stored in the Associated_Node field. Note that
663 -- this field overlaps Entity, which is fine, because the whole point is
664 -- that we don't need or want the normal Entity field in this situation.
665
666 function Has_Been_Exchanged (E : Entity_Id) return Boolean;
667 -- Traverse the Exchanged_Views list to see if a type was private
668 -- and has already been flipped during this phase of instantiation.
669
670 function Has_Contracts (Decl : Node_Id) return Boolean;
671 -- Determine whether a formal subprogram has a Pre- or Postcondition,
672 -- in which case a subprogram wrapper has to be built for the actual.
673
674 procedure Hide_Current_Scope;
675 -- When instantiating a generic child unit, the parent context must be
676 -- present, but the instance and all entities that may be generated
677 -- must be inserted in the current scope. We leave the current scope
678 -- on the stack, but make its entities invisible to avoid visibility
679 -- problems. This is reversed at the end of the instantiation. This is
680 -- not done for the instantiation of the bodies, which only require the
681 -- instances of the generic parents to be in scope.
682
683 function In_Main_Context (E : Entity_Id) return Boolean;
684 -- Check whether an instantiation is in the context of the main unit.
685 -- Used to determine whether its body should be elaborated to allow
686 -- front-end inlining.
687
688 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id);
689 -- Add the context clause of the unit containing a generic unit to a
690 -- compilation unit that is, or contains, an instantiation.
691
692 procedure Init_Env;
693 -- Establish environment for subsequent instantiation. Separated from
694 -- Save_Env because data-structures for visibility handling must be
695 -- initialized before call to Check_Generic_Child_Unit.
696
697 procedure Inline_Instance_Body
698 (N : Node_Id;
699 Gen_Unit : Entity_Id;
700 Act_Decl : Node_Id);
701 -- If front-end inlining is requested, instantiate the package body,
702 -- and preserve the visibility of its compilation unit, to insure
703 -- that successive instantiations succeed.
704
705 procedure Insert_Freeze_Node_For_Instance
706 (N : Node_Id;
707 F_Node : Node_Id);
708 -- N denotes a package or a subprogram instantiation and F_Node is the
709 -- associated freeze node. Insert the freeze node before the first source
710 -- body which follows immediately after N. If no such body is found, the
711 -- freeze node is inserted at the end of the declarative region which
712 -- contains N.
713
714 procedure Install_Body
715 (Act_Body : Node_Id;
716 N : Node_Id;
717 Gen_Body : Node_Id;
718 Gen_Decl : Node_Id);
719 -- If the instantiation happens textually before the body of the generic,
720 -- the instantiation of the body must be analyzed after the generic body,
721 -- and not at the point of instantiation. Such early instantiations can
722 -- happen if the generic and the instance appear in a package declaration
723 -- because the generic body can only appear in the corresponding package
724 -- body. Early instantiations can also appear if generic, instance and
725 -- body are all in the declarative part of a subprogram or entry. Entities
726 -- of packages that are early instantiations are delayed, and their freeze
727 -- node appears after the generic body. This rather complex machinery is
728 -- needed when nested instantiations are present, because the source does
729 -- not carry any indication of where the corresponding instance bodies must
730 -- be installed and frozen.
731
732 procedure Install_Formal_Packages (Par : Entity_Id);
733 -- Install the visible part of any formal of the parent that is a formal
734 -- package. Note that for the case of a formal package with a box, this
735 -- includes the formal part of the formal package (12.7(10/2)).
736
737 procedure Install_Hidden_Primitives
738 (Prims_List : in out Elist_Id;
739 Gen_T : Entity_Id;
740 Act_T : Entity_Id);
741 -- Remove suffix 'P' from hidden primitives of Act_T to match the
742 -- visibility of primitives of Gen_T. The list of primitives to which
743 -- the suffix is removed is added to Prims_List to restore them later.
744
745 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False);
746 -- When compiling an instance of a child unit the parent (which is
747 -- itself an instance) is an enclosing scope that must be made
748 -- immediately visible. This procedure is also used to install the non-
749 -- generic parent of a generic child unit when compiling its body, so
750 -- that full views of types in the parent are made visible.
751
752 -- The functions Instantiate_XXX perform various legality checks and build
753 -- the declarations for instantiated generic parameters. In all of these
754 -- Formal is the entity in the generic unit, Actual is the entity of
755 -- expression in the generic associations, and Analyzed_Formal is the
756 -- formal in the generic copy, which contains the semantic information to
757 -- be used to validate the actual.
758
759 function Instantiate_Object
760 (Formal : Node_Id;
761 Actual : Node_Id;
762 Analyzed_Formal : Node_Id) return List_Id;
763
764 function Instantiate_Type
765 (Formal : Node_Id;
766 Actual : Node_Id;
767 Analyzed_Formal : Node_Id;
768 Actual_Decls : List_Id) return List_Id;
769
770 function Instantiate_Formal_Subprogram
771 (Formal : Node_Id;
772 Actual : Node_Id;
773 Analyzed_Formal : Node_Id) return Node_Id;
774
775 function Instantiate_Formal_Package
776 (Formal : Node_Id;
777 Actual : Node_Id;
778 Analyzed_Formal : Node_Id) return List_Id;
779 -- If the formal package is declared with a box, special visibility rules
780 -- apply to its formals: they are in the visible part of the package. This
781 -- is true in the declarative region of the formal package, that is to say
782 -- in the enclosing generic or instantiation. For an instantiation, the
783 -- parameters of the formal package are made visible in an explicit step.
784 -- Furthermore, if the actual has a visible USE clause, these formals must
785 -- be made potentially use-visible as well. On exit from the enclosing
786 -- instantiation, the reverse must be done.
787
788 -- For a formal package declared without a box, there are conformance rules
789 -- that apply to the actuals in the generic declaration and the actuals of
790 -- the actual package in the enclosing instantiation. The simplest way to
791 -- apply these rules is to repeat the instantiation of the formal package
792 -- in the context of the enclosing instance, and compare the generic
793 -- associations of this instantiation with those of the actual package.
794 -- This internal instantiation only needs to contain the renamings of the
795 -- formals: the visible and private declarations themselves need not be
796 -- created.
797
798 -- In Ada 2005, the formal package may be only partially parameterized.
799 -- In that case the visibility step must make visible those actuals whose
800 -- corresponding formals were given with a box. A final complication
801 -- involves inherited operations from formal derived types, which must
802 -- be visible if the type is.
803
804 function Is_In_Main_Unit (N : Node_Id) return Boolean;
805 -- Test if given node is in the main unit
806
807 procedure Load_Parent_Of_Generic
808 (N : Node_Id;
809 Spec : Node_Id;
810 Body_Optional : Boolean := False);
811 -- If the generic appears in a separate non-generic library unit, load the
812 -- corresponding body to retrieve the body of the generic. N is the node
813 -- for the generic instantiation, Spec is the generic package declaration.
814 --
815 -- Body_Optional is a flag that indicates that the body is being loaded to
816 -- ensure that temporaries are generated consistently when there are other
817 -- instances in the current declarative part that precede the one being
818 -- loaded. In that case a missing body is acceptable.
819
820 procedure Map_Formal_Package_Entities (Form : Entity_Id; Act : Entity_Id);
821 -- Within the generic part, entities in the formal package are
822 -- visible. To validate subsequent type declarations, indicate
823 -- the correspondence between the entities in the analyzed formal,
824 -- and the entities in the actual package. There are three packages
825 -- involved in the instantiation of a formal package: the parent
826 -- generic P1 which appears in the generic declaration, the fake
827 -- instantiation P2 which appears in the analyzed generic, and whose
828 -- visible entities may be used in subsequent formals, and the actual
829 -- P3 in the instance. To validate subsequent formals, me indicate
830 -- that the entities in P2 are mapped into those of P3. The mapping of
831 -- entities has to be done recursively for nested packages.
832
833 procedure Move_Freeze_Nodes
834 (Out_Of : Entity_Id;
835 After : Node_Id;
836 L : List_Id);
837 -- Freeze nodes can be generated in the analysis of a generic unit, but
838 -- will not be seen by the back-end. It is necessary to move those nodes
839 -- to the enclosing scope if they freeze an outer entity. We place them
840 -- at the end of the enclosing generic package, which is semantically
841 -- neutral.
842
843 procedure Preanalyze_Actuals (N : Node_Id; Inst : Entity_Id := Empty);
844 -- Analyze actuals to perform name resolution. Full resolution is done
845 -- later, when the expected types are known, but names have to be captured
846 -- before installing parents of generics, that are not visible for the
847 -- actuals themselves.
848 --
849 -- If Inst is present, it is the entity of the package instance. This
850 -- entity is marked as having a limited_view actual when some actual is
851 -- a limited view. This is used to place the instance body properly.
852
853 procedure Provide_Completing_Bodies (N : Node_Id);
854 -- Generate completing bodies for all subprograms found within package or
855 -- subprogram declaration N.
856
857 procedure Remove_Parent (In_Body : Boolean := False);
858 -- Reverse effect after instantiation of child is complete
859
860 procedure Restore_Hidden_Primitives (Prims_List : in out Elist_Id);
861 -- Restore suffix 'P' to primitives of Prims_List and leave Prims_List
862 -- set to No_Elist.
863
864 procedure Set_Instance_Env
865 (Gen_Unit : Entity_Id;
866 Act_Unit : Entity_Id);
867 -- Save current instance on saved environment, to be used to determine
868 -- the global status of entities in nested instances. Part of Save_Env.
869 -- called after verifying that the generic unit is legal for the instance,
870 -- The procedure also examines whether the generic unit is a predefined
871 -- unit, in order to set configuration switches accordingly. As a result
872 -- the procedure must be called after analyzing and freezing the actuals.
873
874 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id);
875 -- Associate analyzed generic parameter with corresponding instance. Used
876 -- for semantic checks at instantiation time.
877
878 function True_Parent (N : Node_Id) return Node_Id;
879 -- For a subunit, return parent of corresponding stub, else return
880 -- parent of node.
881
882 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id);
883 -- Verify that an attribute that appears as the default for a formal
884 -- subprogram is a function or procedure with the correct profile.
885
886 -------------------------------------------
887 -- Data Structures for Generic Renamings --
888 -------------------------------------------
889
890 -- The map Generic_Renamings associates generic entities with their
891 -- corresponding actuals. Currently used to validate type instances. It
892 -- will eventually be used for all generic parameters to eliminate the
893 -- need for overload resolution in the instance.
894
895 type Assoc_Ptr is new Int;
896
897 Assoc_Null : constant Assoc_Ptr := -1;
898
899 type Assoc is record
900 Gen_Id : Entity_Id;
901 Act_Id : Entity_Id;
902 Next_In_HTable : Assoc_Ptr;
903 end record;
904
905 package Generic_Renamings is new Table.Table
906 (Table_Component_Type => Assoc,
907 Table_Index_Type => Assoc_Ptr,
908 Table_Low_Bound => 0,
909 Table_Initial => 10,
910 Table_Increment => 100,
911 Table_Name => "Generic_Renamings");
912
913 -- Variable to hold enclosing instantiation. When the environment is
914 -- saved for a subprogram inlining, the corresponding Act_Id is empty.
915
916 Current_Instantiated_Parent : Assoc := (Empty, Empty, Assoc_Null);
917
918 -- Hash table for associations
919
920 HTable_Size : constant := 37;
921 type HTable_Range is range 0 .. HTable_Size - 1;
922
923 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr);
924 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr;
925 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id;
926 function Hash (F : Entity_Id) return HTable_Range;
927
928 package Generic_Renamings_HTable is new GNAT.HTable.Static_HTable (
929 Header_Num => HTable_Range,
930 Element => Assoc,
931 Elmt_Ptr => Assoc_Ptr,
932 Null_Ptr => Assoc_Null,
933 Set_Next => Set_Next_Assoc,
934 Next => Next_Assoc,
935 Key => Entity_Id,
936 Get_Key => Get_Gen_Id,
937 Hash => Hash,
938 Equal => "=");
939
940 Exchanged_Views : Elist_Id;
941 -- This list holds the private views that have been exchanged during
942 -- instantiation to restore the visibility of the generic declaration.
943 -- (see comments above). After instantiation, the current visibility is
944 -- reestablished by means of a traversal of this list.
945
946 Hidden_Entities : Elist_Id;
947 -- This list holds the entities of the current scope that are removed
948 -- from immediate visibility when instantiating a child unit. Their
949 -- visibility is restored in Remove_Parent.
950
951 -- Because instantiations can be recursive, the following must be saved
952 -- on entry and restored on exit from an instantiation (spec or body).
953 -- This is done by the two procedures Save_Env and Restore_Env. For
954 -- package and subprogram instantiations (but not for the body instances)
955 -- the action of Save_Env is done in two steps: Init_Env is called before
956 -- Check_Generic_Child_Unit, because setting the parent instances requires
957 -- that the visibility data structures be properly initialized. Once the
958 -- generic is unit is validated, Set_Instance_Env completes Save_Env.
959
960 Parent_Unit_Visible : Boolean := False;
961 -- Parent_Unit_Visible is used when the generic is a child unit, and
962 -- indicates whether the ultimate parent of the generic is visible in the
963 -- instantiation environment. It is used to reset the visibility of the
964 -- parent at the end of the instantiation (see Remove_Parent).
965
966 Instance_Parent_Unit : Entity_Id := Empty;
967 -- This records the ultimate parent unit of an instance of a generic
968 -- child unit and is used in conjunction with Parent_Unit_Visible to
969 -- indicate the unit to which the Parent_Unit_Visible flag corresponds.
970
971 type Instance_Env is record
972 Instantiated_Parent : Assoc;
973 Exchanged_Views : Elist_Id;
974 Hidden_Entities : Elist_Id;
975 Current_Sem_Unit : Unit_Number_Type;
976 Parent_Unit_Visible : Boolean := False;
977 Instance_Parent_Unit : Entity_Id := Empty;
978 Switches : Config_Switches_Type;
979 end record;
980
981 package Instance_Envs is new Table.Table (
982 Table_Component_Type => Instance_Env,
983 Table_Index_Type => Int,
984 Table_Low_Bound => 0,
985 Table_Initial => 32,
986 Table_Increment => 100,
987 Table_Name => "Instance_Envs");
988
989 procedure Restore_Private_Views
990 (Pack_Id : Entity_Id;
991 Is_Package : Boolean := True);
992 -- Restore the private views of external types, and unmark the generic
993 -- renamings of actuals, so that they become compatible subtypes again.
994 -- For subprograms, Pack_Id is the package constructed to hold the
995 -- renamings.
996
997 procedure Switch_View (T : Entity_Id);
998 -- Switch the partial and full views of a type and its private
999 -- dependents (i.e. its subtypes and derived types).
1000
1001 ------------------------------------
1002 -- Structures for Error Reporting --
1003 ------------------------------------
1004
1005 Instantiation_Node : Node_Id;
1006 -- Used by subprograms that validate instantiation of formal parameters
1007 -- where there might be no actual on which to place the error message.
1008 -- Also used to locate the instantiation node for generic subunits.
1009
1010 Instantiation_Error : exception;
1011 -- When there is a semantic error in the generic parameter matching,
1012 -- there is no point in continuing the instantiation, because the
1013 -- number of cascaded errors is unpredictable. This exception aborts
1014 -- the instantiation process altogether.
1015
1016 S_Adjustment : Sloc_Adjustment;
1017 -- Offset created for each node in an instantiation, in order to keep
1018 -- track of the source position of the instantiation in each of its nodes.
1019 -- A subsequent semantic error or warning on a construct of the instance
1020 -- points to both places: the original generic node, and the point of
1021 -- instantiation. See Sinput and Sinput.L for additional details.
1022
1023 ------------------------------------------------------------
1024 -- Data structure for keeping track when inside a Generic --
1025 ------------------------------------------------------------
1026
1027 -- The following table is used to save values of the Inside_A_Generic
1028 -- flag (see spec of Sem) when they are saved by Start_Generic.
1029
1030 package Generic_Flags is new Table.Table (
1031 Table_Component_Type => Boolean,
1032 Table_Index_Type => Int,
1033 Table_Low_Bound => 0,
1034 Table_Initial => 32,
1035 Table_Increment => 200,
1036 Table_Name => "Generic_Flags");
1037
1038 ---------------------------
1039 -- Abandon_Instantiation --
1040 ---------------------------
1041
1042 procedure Abandon_Instantiation (N : Node_Id) is
1043 begin
1044 Error_Msg_N ("\instantiation abandoned!", N);
1045 raise Instantiation_Error;
1046 end Abandon_Instantiation;
1047
1048 ----------------------------------
1049 -- Adjust_Inherited_Pragma_Sloc --
1050 ----------------------------------
1051
1052 procedure Adjust_Inherited_Pragma_Sloc (N : Node_Id) is
1053 begin
1054 Adjust_Instantiation_Sloc (N, S_Adjustment);
1055 end Adjust_Inherited_Pragma_Sloc;
1056
1057 --------------------------
1058 -- Analyze_Associations --
1059 --------------------------
1060
1061 function Analyze_Associations
1062 (I_Node : Node_Id;
1063 Formals : List_Id;
1064 F_Copy : List_Id) return List_Id
1065 is
1066 Actuals_To_Freeze : constant Elist_Id := New_Elmt_List;
1067 Assoc_List : constant List_Id := New_List;
1068 Default_Actuals : constant List_Id := New_List;
1069 Gen_Unit : constant Entity_Id :=
1070 Defining_Entity (Parent (F_Copy));
1071
1072 Actuals : List_Id;
1073 Actual : Node_Id;
1074 Analyzed_Formal : Node_Id;
1075 First_Named : Node_Id := Empty;
1076 Formal : Node_Id;
1077 Match : Node_Id;
1078 Named : Node_Id;
1079 Saved_Formal : Node_Id;
1080
1081 Default_Formals : constant List_Id := New_List;
1082 -- If an Others_Choice is present, some of the formals may be defaulted.
1083 -- To simplify the treatment of visibility in an instance, we introduce
1084 -- individual defaults for each such formal. These defaults are
1085 -- appended to the list of associations and replace the Others_Choice.
1086
1087 Found_Assoc : Node_Id;
1088 -- Association for the current formal being match. Empty if there are
1089 -- no remaining actuals, or if there is no named association with the
1090 -- name of the formal.
1091
1092 Is_Named_Assoc : Boolean;
1093 Num_Matched : Nat := 0;
1094 Num_Actuals : Nat := 0;
1095
1096 Others_Present : Boolean := False;
1097 Others_Choice : Node_Id := Empty;
1098 -- In Ada 2005, indicates partial parameterization of a formal
1099 -- package. As usual an other association must be last in the list.
1100
1101 procedure Build_Subprogram_Wrappers;
1102 -- Ada 2020: AI12-0272 introduces pre/postconditions for formal
1103 -- subprograms. The implementation of making the formal into a renaming
1104 -- of the actual does not work, given that subprogram renaming cannot
1105 -- carry aspect specifications. Instead we must create subprogram
1106 -- wrappers whose body is a call to the actual, and whose declaration
1107 -- carries the aspects of the formal.
1108
1109 procedure Check_Fixed_Point_Actual (Actual : Node_Id);
1110 -- Warn if an actual fixed-point type has user-defined arithmetic
1111 -- operations, but there is no corresponding formal in the generic,
1112 -- in which case the predefined operations will be used. This merits
1113 -- a warning because of the special semantics of fixed point ops.
1114
1115 procedure Check_Overloaded_Formal_Subprogram (Formal : Entity_Id);
1116 -- Apply RM 12.3(9): if a formal subprogram is overloaded, the instance
1117 -- cannot have a named association for it. AI05-0025 extends this rule
1118 -- to formals of formal packages by AI05-0025, and it also applies to
1119 -- box-initialized formals.
1120
1121 function Has_Fully_Defined_Profile (Subp : Entity_Id) return Boolean;
1122 -- Determine whether the parameter types and the return type of Subp
1123 -- are fully defined at the point of instantiation.
1124
1125 function Matching_Actual
1126 (F : Entity_Id;
1127 A_F : Entity_Id) return Node_Id;
1128 -- Find actual that corresponds to a given a formal parameter. If the
1129 -- actuals are positional, return the next one, if any. If the actuals
1130 -- are named, scan the parameter associations to find the right one.
1131 -- A_F is the corresponding entity in the analyzed generic, which is
1132 -- placed on the selector name.
1133 --
1134 -- In Ada 2005, a named association may be given with a box, in which
1135 -- case Matching_Actual sets Found_Assoc to the generic association,
1136 -- but return Empty for the actual itself. In this case the code below
1137 -- creates a corresponding declaration for the formal.
1138
1139 function Partial_Parameterization return Boolean;
1140 -- Ada 2005: if no match is found for a given formal, check if the
1141 -- association for it includes a box, or whether the associations
1142 -- include an Others clause.
1143
1144 procedure Process_Default (F : Entity_Id);
1145 -- Add a copy of the declaration of generic formal F to the list of
1146 -- associations, and add an explicit box association for F if there
1147 -- is none yet, and the default comes from an Others_Choice.
1148
1149 function Renames_Standard_Subprogram (Subp : Entity_Id) return Boolean;
1150 -- Determine whether Subp renames one of the subprograms defined in the
1151 -- generated package Standard.
1152
1153 procedure Set_Analyzed_Formal;
1154 -- Find the node in the generic copy that corresponds to a given formal.
1155 -- The semantic information on this node is used to perform legality
1156 -- checks on the actuals. Because semantic analysis can introduce some
1157 -- anonymous entities or modify the declaration node itself, the
1158 -- correspondence between the two lists is not one-one. In addition to
1159 -- anonymous types, the presence a formal equality will introduce an
1160 -- implicit declaration for the corresponding inequality.
1161
1162 -----------------------------------------
1163 -- procedure Build_Subprogram_Wrappers --
1164 -----------------------------------------
1165
1166 procedure Build_Subprogram_Wrappers is
1167 Formal : constant Entity_Id :=
1168 Defining_Unit_Name (Specification (Analyzed_Formal));
1169 Aspect_Spec : Node_Id;
1170 Decl_Node : Node_Id;
1171 Actual_Name : Node_Id;
1172
1173 begin
1174 -- Create declaration for wrapper subprogram
1175 -- The actual can be overloaded, in which case it will be
1176 -- resolved when the call in the wrapper body is analyzed.
1177 -- We attach the possible interpretations of the actual to
1178 -- the name to be used in the call in the wrapper body.
1179
1180 if Is_Entity_Name (Match) then
1181 Actual_Name := New_Occurrence_Of (Entity (Match), Sloc (Match));
1182
1183 if Is_Overloaded (Match) then
1184 Save_Interps (Match, Actual_Name);
1185 end if;
1186
1187 else
1188 -- Use renaming declaration created when analyzing actual.
1189 -- This may be incomplete if there are several formal
1190 -- subprograms whose actual is an attribute ???
1191
1192 declare
1193 Renaming_Decl : constant Node_Id := Last (Assoc_List);
1194
1195 begin
1196 Actual_Name := New_Occurrence_Of
1197 (Defining_Entity (Renaming_Decl), Sloc (Match));
1198 Set_Etype (Actual_Name, Get_Instance_Of (Etype (Formal)));
1199 end;
1200 end if;
1201
1202 Decl_Node := Build_Subprogram_Decl_Wrapper (Formal);
1203
1204 -- Transfer aspect specifications from formal subprogram to wrapper
1205
1206 Set_Aspect_Specifications (Decl_Node,
1207 New_Copy_List_Tree (Aspect_Specifications (Analyzed_Formal)));
1208
1209 Aspect_Spec := First (Aspect_Specifications (Decl_Node));
1210 while Present (Aspect_Spec) loop
1211 Set_Analyzed (Aspect_Spec, False);
1212 Next (Aspect_Spec);
1213 end loop;
1214
1215 Append_To (Assoc_List, Decl_Node);
1216
1217 -- Create corresponding body, and append it to association list
1218 -- that appears at the head of the declarations in the instance.
1219 -- The subprogram may be called in the analysis of subsequent
1220 -- actuals.
1221
1222 Append_To (Assoc_List,
1223 Build_Subprogram_Body_Wrapper (Formal, Actual_Name));
1224 end Build_Subprogram_Wrappers;
1225
1226 ----------------------------------------
1227 -- Check_Overloaded_Formal_Subprogram --
1228 ----------------------------------------
1229
1230 procedure Check_Overloaded_Formal_Subprogram (Formal : Entity_Id) is
1231 Temp_Formal : Entity_Id;
1232
1233 begin
1234 Temp_Formal := First (Formals);
1235 while Present (Temp_Formal) loop
1236 if Nkind (Temp_Formal) in N_Formal_Subprogram_Declaration
1237 and then Temp_Formal /= Formal
1238 and then
1239 Chars (Defining_Unit_Name (Specification (Formal))) =
1240 Chars (Defining_Unit_Name (Specification (Temp_Formal)))
1241 then
1242 if Present (Found_Assoc) then
1243 Error_Msg_N
1244 ("named association not allowed for overloaded formal",
1245 Found_Assoc);
1246
1247 else
1248 Error_Msg_N
1249 ("named association not allowed for overloaded formal",
1250 Others_Choice);
1251 end if;
1252
1253 Abandon_Instantiation (Instantiation_Node);
1254 end if;
1255
1256 Next (Temp_Formal);
1257 end loop;
1258 end Check_Overloaded_Formal_Subprogram;
1259
1260 -------------------------------
1261 -- Check_Fixed_Point_Actual --
1262 -------------------------------
1263
1264 procedure Check_Fixed_Point_Actual (Actual : Node_Id) is
1265 Typ : constant Entity_Id := Entity (Actual);
1266 Prims : constant Elist_Id := Collect_Primitive_Operations (Typ);
1267 Elem : Elmt_Id;
1268 Formal : Node_Id;
1269 Op : Entity_Id;
1270
1271 begin
1272 -- Locate primitive operations of the type that are arithmetic
1273 -- operations.
1274
1275 Elem := First_Elmt (Prims);
1276 while Present (Elem) loop
1277 if Nkind (Node (Elem)) = N_Defining_Operator_Symbol then
1278
1279 -- Check whether the generic unit has a formal subprogram of
1280 -- the same name. This does not check types but is good enough
1281 -- to justify a warning.
1282
1283 Formal := First_Non_Pragma (Formals);
1284 Op := Alias (Node (Elem));
1285
1286 while Present (Formal) loop
1287 if Nkind (Formal) = N_Formal_Concrete_Subprogram_Declaration
1288 and then Chars (Defining_Entity (Formal)) =
1289 Chars (Node (Elem))
1290 then
1291 exit;
1292
1293 elsif Nkind (Formal) = N_Formal_Package_Declaration then
1294 declare
1295 Assoc : Node_Id;
1296 Ent : Entity_Id;
1297
1298 begin
1299 -- Locate corresponding actual, and check whether it
1300 -- includes a fixed-point type.
1301
1302 Assoc := First (Assoc_List);
1303 while Present (Assoc) loop
1304 exit when
1305 Nkind (Assoc) = N_Package_Renaming_Declaration
1306 and then Chars (Defining_Unit_Name (Assoc)) =
1307 Chars (Defining_Identifier (Formal));
1308
1309 Next (Assoc);
1310 end loop;
1311
1312 if Present (Assoc) then
1313
1314 -- If formal package declares a fixed-point type,
1315 -- and the user-defined operator is derived from
1316 -- a generic instance package, the fixed-point type
1317 -- does not use the corresponding predefined op.
1318
1319 Ent := First_Entity (Entity (Name (Assoc)));
1320 while Present (Ent) loop
1321 if Is_Fixed_Point_Type (Ent)
1322 and then Present (Op)
1323 and then Is_Generic_Instance (Scope (Op))
1324 then
1325 return;
1326 end if;
1327
1328 Next_Entity (Ent);
1329 end loop;
1330 end if;
1331 end;
1332 end if;
1333
1334 Next (Formal);
1335 end loop;
1336
1337 if No (Formal) then
1338 Error_Msg_Sloc := Sloc (Node (Elem));
1339 Error_Msg_NE
1340 ("?instance uses predefined operation, not primitive "
1341 & "operation&#", Actual, Node (Elem));
1342 end if;
1343 end if;
1344
1345 Next_Elmt (Elem);
1346 end loop;
1347 end Check_Fixed_Point_Actual;
1348
1349 -------------------------------
1350 -- Has_Fully_Defined_Profile --
1351 -------------------------------
1352
1353 function Has_Fully_Defined_Profile (Subp : Entity_Id) return Boolean is
1354 function Is_Fully_Defined_Type (Typ : Entity_Id) return Boolean;
1355 -- Determine whethet type Typ is fully defined
1356
1357 ---------------------------
1358 -- Is_Fully_Defined_Type --
1359 ---------------------------
1360
1361 function Is_Fully_Defined_Type (Typ : Entity_Id) return Boolean is
1362 begin
1363 -- A private type without a full view is not fully defined
1364
1365 if Is_Private_Type (Typ)
1366 and then No (Full_View (Typ))
1367 then
1368 return False;
1369
1370 -- An incomplete type is never fully defined
1371
1372 elsif Is_Incomplete_Type (Typ) then
1373 return False;
1374
1375 -- All other types are fully defined
1376
1377 else
1378 return True;
1379 end if;
1380 end Is_Fully_Defined_Type;
1381
1382 -- Local declarations
1383
1384 Param : Entity_Id;
1385
1386 -- Start of processing for Has_Fully_Defined_Profile
1387
1388 begin
1389 -- Check the parameters
1390
1391 Param := First_Formal (Subp);
1392 while Present (Param) loop
1393 if not Is_Fully_Defined_Type (Etype (Param)) then
1394 return False;
1395 end if;
1396
1397 Next_Formal (Param);
1398 end loop;
1399
1400 -- Check the return type
1401
1402 return Is_Fully_Defined_Type (Etype (Subp));
1403 end Has_Fully_Defined_Profile;
1404
1405 ---------------------
1406 -- Matching_Actual --
1407 ---------------------
1408
1409 function Matching_Actual
1410 (F : Entity_Id;
1411 A_F : Entity_Id) return Node_Id
1412 is
1413 Prev : Node_Id;
1414 Act : Node_Id;
1415
1416 begin
1417 Is_Named_Assoc := False;
1418
1419 -- End of list of purely positional parameters
1420
1421 if No (Actual) or else Nkind (Actual) = N_Others_Choice then
1422 Found_Assoc := Empty;
1423 Act := Empty;
1424
1425 -- Case of positional parameter corresponding to current formal
1426
1427 elsif No (Selector_Name (Actual)) then
1428 Found_Assoc := Actual;
1429 Act := Explicit_Generic_Actual_Parameter (Actual);
1430 Num_Matched := Num_Matched + 1;
1431 Next (Actual);
1432
1433 -- Otherwise scan list of named actuals to find the one with the
1434 -- desired name. All remaining actuals have explicit names.
1435
1436 else
1437 Is_Named_Assoc := True;
1438 Found_Assoc := Empty;
1439 Act := Empty;
1440 Prev := Empty;
1441
1442 while Present (Actual) loop
1443 if Nkind (Actual) = N_Others_Choice then
1444 Found_Assoc := Empty;
1445 Act := Empty;
1446
1447 elsif Chars (Selector_Name (Actual)) = Chars (F) then
1448 Set_Entity (Selector_Name (Actual), A_F);
1449 Set_Etype (Selector_Name (Actual), Etype (A_F));
1450 Generate_Reference (A_F, Selector_Name (Actual));
1451
1452 Found_Assoc := Actual;
1453 Act := Explicit_Generic_Actual_Parameter (Actual);
1454 Num_Matched := Num_Matched + 1;
1455 exit;
1456 end if;
1457
1458 Prev := Actual;
1459 Next (Actual);
1460 end loop;
1461
1462 -- Reset for subsequent searches. In most cases the named
1463 -- associations are in order. If they are not, we reorder them
1464 -- to avoid scanning twice the same actual. This is not just a
1465 -- question of efficiency: there may be multiple defaults with
1466 -- boxes that have the same name. In a nested instantiation we
1467 -- insert actuals for those defaults, and cannot rely on their
1468 -- names to disambiguate them.
1469
1470 if Actual = First_Named then
1471 Next (First_Named);
1472
1473 elsif Present (Actual) then
1474 Insert_Before (First_Named, Remove_Next (Prev));
1475 end if;
1476
1477 Actual := First_Named;
1478 end if;
1479
1480 if Is_Entity_Name (Act) and then Present (Entity (Act)) then
1481 Set_Used_As_Generic_Actual (Entity (Act));
1482 end if;
1483
1484 return Act;
1485 end Matching_Actual;
1486
1487 ------------------------------
1488 -- Partial_Parameterization --
1489 ------------------------------
1490
1491 function Partial_Parameterization return Boolean is
1492 begin
1493 return Others_Present
1494 or else (Present (Found_Assoc) and then Box_Present (Found_Assoc));
1495 end Partial_Parameterization;
1496
1497 ---------------------
1498 -- Process_Default --
1499 ---------------------
1500
1501 procedure Process_Default (F : Entity_Id) is
1502 Loc : constant Source_Ptr := Sloc (I_Node);
1503 F_Id : constant Entity_Id := Defining_Entity (F);
1504 Decl : Node_Id;
1505 Default : Node_Id;
1506 Id : Entity_Id;
1507
1508 begin
1509 -- Append copy of formal declaration to associations, and create new
1510 -- defining identifier for it.
1511
1512 Decl := New_Copy_Tree (F);
1513 Id := Make_Defining_Identifier (Sloc (F_Id), Chars (F_Id));
1514
1515 if Nkind (F) in N_Formal_Subprogram_Declaration then
1516 Set_Defining_Unit_Name (Specification (Decl), Id);
1517
1518 else
1519 Set_Defining_Identifier (Decl, Id);
1520 end if;
1521
1522 Append (Decl, Assoc_List);
1523
1524 if No (Found_Assoc) then
1525 Default :=
1526 Make_Generic_Association (Loc,
1527 Selector_Name =>
1528 New_Occurrence_Of (Id, Loc),
1529 Explicit_Generic_Actual_Parameter => Empty);
1530 Set_Box_Present (Default);
1531 Append (Default, Default_Formals);
1532 end if;
1533 end Process_Default;
1534
1535 ---------------------------------
1536 -- Renames_Standard_Subprogram --
1537 ---------------------------------
1538
1539 function Renames_Standard_Subprogram (Subp : Entity_Id) return Boolean is
1540 Id : Entity_Id;
1541
1542 begin
1543 Id := Alias (Subp);
1544 while Present (Id) loop
1545 if Scope (Id) = Standard_Standard then
1546 return True;
1547 end if;
1548
1549 Id := Alias (Id);
1550 end loop;
1551
1552 return False;
1553 end Renames_Standard_Subprogram;
1554
1555 -------------------------
1556 -- Set_Analyzed_Formal --
1557 -------------------------
1558
1559 procedure Set_Analyzed_Formal is
1560 Kind : Node_Kind;
1561
1562 begin
1563 while Present (Analyzed_Formal) loop
1564 Kind := Nkind (Analyzed_Formal);
1565
1566 case Nkind (Formal) is
1567 when N_Formal_Subprogram_Declaration =>
1568 exit when Kind in N_Formal_Subprogram_Declaration
1569 and then
1570 Chars
1571 (Defining_Unit_Name (Specification (Formal))) =
1572 Chars
1573 (Defining_Unit_Name (Specification (Analyzed_Formal)));
1574
1575 when N_Formal_Package_Declaration =>
1576 exit when Nkind_In (Kind, N_Formal_Package_Declaration,
1577 N_Generic_Package_Declaration,
1578 N_Package_Declaration);
1579
1580 when N_Use_Package_Clause
1581 | N_Use_Type_Clause
1582 =>
1583 exit;
1584
1585 when others =>
1586
1587 -- Skip freeze nodes, and nodes inserted to replace
1588 -- unrecognized pragmas.
1589
1590 exit when
1591 Kind not in N_Formal_Subprogram_Declaration
1592 and then not Nkind_In (Kind, N_Subprogram_Declaration,
1593 N_Freeze_Entity,
1594 N_Null_Statement,
1595 N_Itype_Reference)
1596 and then Chars (Defining_Identifier (Formal)) =
1597 Chars (Defining_Identifier (Analyzed_Formal));
1598 end case;
1599
1600 Next (Analyzed_Formal);
1601 end loop;
1602 end Set_Analyzed_Formal;
1603
1604 -- Start of processing for Analyze_Associations
1605
1606 begin
1607 Actuals := Generic_Associations (I_Node);
1608
1609 if Present (Actuals) then
1610
1611 -- Check for an Others choice, indicating a partial parameterization
1612 -- for a formal package.
1613
1614 Actual := First (Actuals);
1615 while Present (Actual) loop
1616 if Nkind (Actual) = N_Others_Choice then
1617 Others_Present := True;
1618 Others_Choice := Actual;
1619
1620 if Present (Next (Actual)) then
1621 Error_Msg_N ("others must be last association", Actual);
1622 end if;
1623
1624 -- This subprogram is used both for formal packages and for
1625 -- instantiations. For the latter, associations must all be
1626 -- explicit.
1627
1628 if Nkind (I_Node) /= N_Formal_Package_Declaration
1629 and then Comes_From_Source (I_Node)
1630 then
1631 Error_Msg_N
1632 ("others association not allowed in an instance",
1633 Actual);
1634 end if;
1635
1636 -- In any case, nothing to do after the others association
1637
1638 exit;
1639
1640 elsif Box_Present (Actual)
1641 and then Comes_From_Source (I_Node)
1642 and then Nkind (I_Node) /= N_Formal_Package_Declaration
1643 then
1644 Error_Msg_N
1645 ("box association not allowed in an instance", Actual);
1646 end if;
1647
1648 Next (Actual);
1649 end loop;
1650
1651 -- If named associations are present, save first named association
1652 -- (it may of course be Empty) to facilitate subsequent name search.
1653
1654 First_Named := First (Actuals);
1655 while Present (First_Named)
1656 and then Nkind (First_Named) /= N_Others_Choice
1657 and then No (Selector_Name (First_Named))
1658 loop
1659 Num_Actuals := Num_Actuals + 1;
1660 Next (First_Named);
1661 end loop;
1662 end if;
1663
1664 Named := First_Named;
1665 while Present (Named) loop
1666 if Nkind (Named) /= N_Others_Choice
1667 and then No (Selector_Name (Named))
1668 then
1669 Error_Msg_N ("invalid positional actual after named one", Named);
1670 Abandon_Instantiation (Named);
1671 end if;
1672
1673 -- A named association may lack an actual parameter, if it was
1674 -- introduced for a default subprogram that turns out to be local
1675 -- to the outer instantiation. If it has a box association it must
1676 -- correspond to some formal in the generic.
1677
1678 if Nkind (Named) /= N_Others_Choice
1679 and then (Present (Explicit_Generic_Actual_Parameter (Named))
1680 or else Box_Present (Named))
1681 then
1682 Num_Actuals := Num_Actuals + 1;
1683 end if;
1684
1685 Next (Named);
1686 end loop;
1687
1688 if Present (Formals) then
1689 Formal := First_Non_Pragma (Formals);
1690 Analyzed_Formal := First_Non_Pragma (F_Copy);
1691
1692 if Present (Actuals) then
1693 Actual := First (Actuals);
1694
1695 -- All formals should have default values
1696
1697 else
1698 Actual := Empty;
1699 end if;
1700
1701 while Present (Formal) loop
1702 Set_Analyzed_Formal;
1703 Saved_Formal := Next_Non_Pragma (Formal);
1704
1705 case Nkind (Formal) is
1706 when N_Formal_Object_Declaration =>
1707 Match :=
1708 Matching_Actual
1709 (Defining_Identifier (Formal),
1710 Defining_Identifier (Analyzed_Formal));
1711
1712 if No (Match) and then Partial_Parameterization then
1713 Process_Default (Formal);
1714
1715 else
1716 Append_List
1717 (Instantiate_Object (Formal, Match, Analyzed_Formal),
1718 Assoc_List);
1719
1720 -- For a defaulted in_parameter, create an entry in the
1721 -- the list of defaulted actuals, for GNATProve use. Do
1722 -- not included these defaults for an instance nested
1723 -- within a generic, because the defaults are also used
1724 -- in the analysis of the enclosing generic, and only
1725 -- defaulted subprograms are relevant there.
1726
1727 if No (Match) and then not Inside_A_Generic then
1728 Append_To (Default_Actuals,
1729 Make_Generic_Association (Sloc (I_Node),
1730 Selector_Name =>
1731 New_Occurrence_Of
1732 (Defining_Identifier (Formal), Sloc (I_Node)),
1733 Explicit_Generic_Actual_Parameter =>
1734 New_Copy_Tree (Default_Expression (Formal))));
1735 end if;
1736 end if;
1737
1738 -- If the object is a call to an expression function, this
1739 -- is a freezing point for it.
1740
1741 if Is_Entity_Name (Match)
1742 and then Present (Entity (Match))
1743 and then Nkind
1744 (Original_Node (Unit_Declaration_Node (Entity (Match))))
1745 = N_Expression_Function
1746 then
1747 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1748 end if;
1749
1750 when N_Formal_Type_Declaration =>
1751 Match :=
1752 Matching_Actual
1753 (Defining_Identifier (Formal),
1754 Defining_Identifier (Analyzed_Formal));
1755
1756 if No (Match) then
1757 if Partial_Parameterization then
1758 Process_Default (Formal);
1759
1760 else
1761 Error_Msg_Sloc := Sloc (Gen_Unit);
1762 Error_Msg_NE
1763 ("missing actual&",
1764 Instantiation_Node, Defining_Identifier (Formal));
1765 Error_Msg_NE
1766 ("\in instantiation of & declared#",
1767 Instantiation_Node, Gen_Unit);
1768 Abandon_Instantiation (Instantiation_Node);
1769 end if;
1770
1771 else
1772 Analyze (Match);
1773 Append_List
1774 (Instantiate_Type
1775 (Formal, Match, Analyzed_Formal, Assoc_List),
1776 Assoc_List);
1777
1778 -- Warn when an actual is a fixed-point with user-
1779 -- defined promitives. The warning is superfluous
1780 -- if the formal is private, because there can be
1781 -- no arithmetic operations in the generic so there
1782 -- no danger of confusion.
1783
1784 if Is_Fixed_Point_Type (Entity (Match))
1785 and then not Is_Private_Type
1786 (Defining_Identifier (Analyzed_Formal))
1787 then
1788 Check_Fixed_Point_Actual (Match);
1789 end if;
1790
1791 -- An instantiation is a freeze point for the actuals,
1792 -- unless this is a rewritten formal package, or the
1793 -- formal is an Ada 2012 formal incomplete type.
1794
1795 if Nkind (I_Node) = N_Formal_Package_Declaration
1796 or else
1797 (Ada_Version >= Ada_2012
1798 and then
1799 Ekind (Defining_Identifier (Analyzed_Formal)) =
1800 E_Incomplete_Type)
1801 then
1802 null;
1803
1804 else
1805 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1806 end if;
1807 end if;
1808
1809 -- A remote access-to-class-wide type is not a legal actual
1810 -- for a generic formal of an access type (E.2.2(17/2)).
1811 -- In GNAT an exception to this rule is introduced when
1812 -- the formal is marked as remote using implementation
1813 -- defined aspect/pragma Remote_Access_Type. In that case
1814 -- the actual must be remote as well.
1815
1816 -- If the current instantiation is the construction of a
1817 -- local copy for a formal package the actuals may be
1818 -- defaulted, and there is no matching actual to check.
1819
1820 if Nkind (Analyzed_Formal) = N_Formal_Type_Declaration
1821 and then
1822 Nkind (Formal_Type_Definition (Analyzed_Formal)) =
1823 N_Access_To_Object_Definition
1824 and then Present (Match)
1825 then
1826 declare
1827 Formal_Ent : constant Entity_Id :=
1828 Defining_Identifier (Analyzed_Formal);
1829 begin
1830 if Is_Remote_Access_To_Class_Wide_Type (Entity (Match))
1831 = Is_Remote_Types (Formal_Ent)
1832 then
1833 -- Remoteness of formal and actual match
1834
1835 null;
1836
1837 elsif Is_Remote_Types (Formal_Ent) then
1838
1839 -- Remote formal, non-remote actual
1840
1841 Error_Msg_NE
1842 ("actual for& must be remote", Match, Formal_Ent);
1843
1844 else
1845 -- Non-remote formal, remote actual
1846
1847 Error_Msg_NE
1848 ("actual for& may not be remote",
1849 Match, Formal_Ent);
1850 end if;
1851 end;
1852 end if;
1853
1854 when N_Formal_Subprogram_Declaration =>
1855 Match :=
1856 Matching_Actual
1857 (Defining_Unit_Name (Specification (Formal)),
1858 Defining_Unit_Name (Specification (Analyzed_Formal)));
1859
1860 -- If the formal subprogram has the same name as another
1861 -- formal subprogram of the generic, then a named
1862 -- association is illegal (12.3(9)). Exclude named
1863 -- associations that are generated for a nested instance.
1864
1865 if Present (Match)
1866 and then Is_Named_Assoc
1867 and then Comes_From_Source (Found_Assoc)
1868 then
1869 Check_Overloaded_Formal_Subprogram (Formal);
1870 end if;
1871
1872 -- If there is no corresponding actual, this may be case
1873 -- of partial parameterization, or else the formal has a
1874 -- default or a box.
1875
1876 if No (Match) and then Partial_Parameterization then
1877 Process_Default (Formal);
1878
1879 if Nkind (I_Node) = N_Formal_Package_Declaration then
1880 Check_Overloaded_Formal_Subprogram (Formal);
1881 end if;
1882
1883 else
1884 Append_To (Assoc_List,
1885 Instantiate_Formal_Subprogram
1886 (Formal, Match, Analyzed_Formal));
1887
1888 -- If formal subprogram has contracts, create wrappers
1889 -- for it. This is an expansion activity that cannot
1890 -- take place e.g. within an enclosing generic unit.
1891
1892 if Has_Contracts (Analyzed_Formal)
1893 and then Expander_Active
1894 then
1895 Build_Subprogram_Wrappers;
1896 end if;
1897
1898 -- An instantiation is a freeze point for the actuals,
1899 -- unless this is a rewritten formal package.
1900
1901 if Nkind (I_Node) /= N_Formal_Package_Declaration
1902 and then Nkind (Match) = N_Identifier
1903 and then Is_Subprogram (Entity (Match))
1904
1905 -- The actual subprogram may rename a routine defined
1906 -- in Standard. Avoid freezing such renamings because
1907 -- subprograms coming from Standard cannot be frozen.
1908
1909 and then
1910 not Renames_Standard_Subprogram (Entity (Match))
1911
1912 -- If the actual subprogram comes from a different
1913 -- unit, it is already frozen, either by a body in
1914 -- that unit or by the end of the declarative part
1915 -- of the unit. This check avoids the freezing of
1916 -- subprograms defined in Standard which are used
1917 -- as generic actuals.
1918
1919 and then In_Same_Code_Unit (Entity (Match), I_Node)
1920 and then Has_Fully_Defined_Profile (Entity (Match))
1921 then
1922 -- Mark the subprogram as having a delayed freeze
1923 -- since this may be an out-of-order action.
1924
1925 Set_Has_Delayed_Freeze (Entity (Match));
1926 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1927 end if;
1928 end if;
1929
1930 -- If this is a nested generic, preserve default for later
1931 -- instantiations. We do this as well for GNATProve use,
1932 -- so that the list of generic associations is complete.
1933
1934 if No (Match) and then Box_Present (Formal) then
1935 declare
1936 Subp : constant Entity_Id :=
1937 Defining_Unit_Name
1938 (Specification (Last (Assoc_List)));
1939
1940 begin
1941 Append_To (Default_Actuals,
1942 Make_Generic_Association (Sloc (I_Node),
1943 Selector_Name =>
1944 New_Occurrence_Of (Subp, Sloc (I_Node)),
1945 Explicit_Generic_Actual_Parameter =>
1946 New_Occurrence_Of (Subp, Sloc (I_Node))));
1947 end;
1948 end if;
1949
1950 when N_Formal_Package_Declaration =>
1951 Match :=
1952 Matching_Actual
1953 (Defining_Identifier (Formal),
1954 Defining_Identifier (Original_Node (Analyzed_Formal)));
1955
1956 if No (Match) then
1957 if Partial_Parameterization then
1958 Process_Default (Formal);
1959
1960 else
1961 Error_Msg_Sloc := Sloc (Gen_Unit);
1962 Error_Msg_NE
1963 ("missing actual&",
1964 Instantiation_Node, Defining_Identifier (Formal));
1965 Error_Msg_NE
1966 ("\in instantiation of & declared#",
1967 Instantiation_Node, Gen_Unit);
1968
1969 Abandon_Instantiation (Instantiation_Node);
1970 end if;
1971
1972 else
1973 Analyze (Match);
1974 Append_List
1975 (Instantiate_Formal_Package
1976 (Formal, Match, Analyzed_Formal),
1977 Assoc_List);
1978
1979 -- Determine whether the actual package needs an explicit
1980 -- freeze node. This is only the case if the actual is
1981 -- declared in the same unit and has a body. Normally
1982 -- packages do not have explicit freeze nodes, and gigi
1983 -- only uses them to elaborate entities in a package
1984 -- body.
1985
1986 Explicit_Freeze_Check : declare
1987 Actual : constant Entity_Id := Entity (Match);
1988 Gen_Par : Entity_Id;
1989
1990 Needs_Freezing : Boolean;
1991 S : Entity_Id;
1992
1993 procedure Check_Generic_Parent;
1994 -- The actual may be an instantiation of a unit
1995 -- declared in a previous instantiation. If that
1996 -- one is also in the current compilation, it must
1997 -- itself be frozen before the actual. The actual
1998 -- may be an instantiation of a generic child unit,
1999 -- in which case the same applies to the instance
2000 -- of the parent which must be frozen before the
2001 -- actual.
2002 -- Should this itself be recursive ???
2003
2004 --------------------------
2005 -- Check_Generic_Parent --
2006 --------------------------
2007
2008 procedure Check_Generic_Parent is
2009 Inst : constant Node_Id :=
2010 Next (Unit_Declaration_Node (Actual));
2011 Par : Entity_Id;
2012
2013 begin
2014 Par := Empty;
2015
2016 if Nkind (Parent (Actual)) = N_Package_Specification
2017 then
2018 Par := Scope (Generic_Parent (Parent (Actual)));
2019
2020 if Is_Generic_Instance (Par) then
2021 null;
2022
2023 -- If the actual is a child generic unit, check
2024 -- whether the instantiation of the parent is
2025 -- also local and must also be frozen now. We
2026 -- must retrieve the instance node to locate the
2027 -- parent instance if any.
2028
2029 elsif Ekind (Par) = E_Generic_Package
2030 and then Is_Child_Unit (Gen_Par)
2031 and then Ekind (Scope (Gen_Par)) =
2032 E_Generic_Package
2033 then
2034 if Nkind (Inst) = N_Package_Instantiation
2035 and then Nkind (Name (Inst)) =
2036 N_Expanded_Name
2037 then
2038 -- Retrieve entity of parent instance
2039
2040 Par := Entity (Prefix (Name (Inst)));
2041 end if;
2042
2043 else
2044 Par := Empty;
2045 end if;
2046 end if;
2047
2048 if Present (Par)
2049 and then Is_Generic_Instance (Par)
2050 and then Scope (Par) = Current_Scope
2051 and then
2052 (No (Freeze_Node (Par))
2053 or else
2054 not Is_List_Member (Freeze_Node (Par)))
2055 then
2056 Set_Has_Delayed_Freeze (Par);
2057 Append_Elmt (Par, Actuals_To_Freeze);
2058 end if;
2059 end Check_Generic_Parent;
2060
2061 -- Start of processing for Explicit_Freeze_Check
2062
2063 begin
2064 if Present (Renamed_Entity (Actual)) then
2065 Gen_Par :=
2066 Generic_Parent (Specification
2067 (Unit_Declaration_Node
2068 (Renamed_Entity (Actual))));
2069 else
2070 Gen_Par :=
2071 Generic_Parent (Specification
2072 (Unit_Declaration_Node (Actual)));
2073 end if;
2074
2075 if not Expander_Active
2076 or else not Has_Completion (Actual)
2077 or else not In_Same_Source_Unit (I_Node, Actual)
2078 or else Is_Frozen (Actual)
2079 or else
2080 (Present (Renamed_Entity (Actual))
2081 and then
2082 not In_Same_Source_Unit
2083 (I_Node, (Renamed_Entity (Actual))))
2084 then
2085 null;
2086
2087 else
2088 -- Finally we want to exclude such freeze nodes
2089 -- from statement sequences, which freeze
2090 -- everything before them.
2091 -- Is this strictly necessary ???
2092
2093 Needs_Freezing := True;
2094
2095 S := Current_Scope;
2096 while Present (S) loop
2097 if Ekind_In (S, E_Block,
2098 E_Function,
2099 E_Loop,
2100 E_Procedure)
2101 then
2102 Needs_Freezing := False;
2103 exit;
2104 end if;
2105
2106 S := Scope (S);
2107 end loop;
2108
2109 if Needs_Freezing then
2110 Check_Generic_Parent;
2111
2112 -- If the actual is a renaming of a proper
2113 -- instance of the formal package, indicate
2114 -- that it is the instance that must be frozen.
2115
2116 if Nkind (Parent (Actual)) =
2117 N_Package_Renaming_Declaration
2118 then
2119 Set_Has_Delayed_Freeze
2120 (Renamed_Entity (Actual));
2121 Append_Elmt
2122 (Renamed_Entity (Actual),
2123 Actuals_To_Freeze);
2124 else
2125 Set_Has_Delayed_Freeze (Actual);
2126 Append_Elmt (Actual, Actuals_To_Freeze);
2127 end if;
2128 end if;
2129 end if;
2130 end Explicit_Freeze_Check;
2131 end if;
2132
2133 -- For use type and use package appearing in the generic part,
2134 -- we have already copied them, so we can just move them where
2135 -- they belong (we mustn't recopy them since this would mess up
2136 -- the Sloc values).
2137
2138 when N_Use_Package_Clause
2139 | N_Use_Type_Clause
2140 =>
2141 if Nkind (Original_Node (I_Node)) =
2142 N_Formal_Package_Declaration
2143 then
2144 Append (New_Copy_Tree (Formal), Assoc_List);
2145 else
2146 Remove (Formal);
2147 Append (Formal, Assoc_List);
2148 end if;
2149
2150 when others =>
2151 raise Program_Error;
2152 end case;
2153
2154 Formal := Saved_Formal;
2155 Next_Non_Pragma (Analyzed_Formal);
2156 end loop;
2157
2158 if Num_Actuals > Num_Matched then
2159 Error_Msg_Sloc := Sloc (Gen_Unit);
2160
2161 if Present (Selector_Name (Actual)) then
2162 Error_Msg_NE
2163 ("unmatched actual &", Actual, Selector_Name (Actual));
2164 Error_Msg_NE
2165 ("\in instantiation of & declared#", Actual, Gen_Unit);
2166 else
2167 Error_Msg_NE
2168 ("unmatched actual in instantiation of & declared#",
2169 Actual, Gen_Unit);
2170 end if;
2171 end if;
2172
2173 elsif Present (Actuals) then
2174 Error_Msg_N
2175 ("too many actuals in generic instantiation", Instantiation_Node);
2176 end if;
2177
2178 -- An instantiation freezes all generic actuals. The only exceptions
2179 -- to this are incomplete types and subprograms which are not fully
2180 -- defined at the point of instantiation.
2181
2182 declare
2183 Elmt : Elmt_Id := First_Elmt (Actuals_To_Freeze);
2184 begin
2185 while Present (Elmt) loop
2186 Freeze_Before (I_Node, Node (Elmt));
2187 Next_Elmt (Elmt);
2188 end loop;
2189 end;
2190
2191 -- If there are default subprograms, normalize the tree by adding
2192 -- explicit associations for them. This is required if the instance
2193 -- appears within a generic.
2194
2195 if not Is_Empty_List (Default_Actuals) then
2196 declare
2197 Default : Node_Id;
2198
2199 begin
2200 Default := First (Default_Actuals);
2201 while Present (Default) loop
2202 Mark_Rewrite_Insertion (Default);
2203 Next (Default);
2204 end loop;
2205
2206 if No (Actuals) then
2207 Set_Generic_Associations (I_Node, Default_Actuals);
2208 else
2209 Append_List_To (Actuals, Default_Actuals);
2210 end if;
2211 end;
2212 end if;
2213
2214 -- If this is a formal package, normalize the parameter list by adding
2215 -- explicit box associations for the formals that are covered by an
2216 -- Others_Choice.
2217
2218 if not Is_Empty_List (Default_Formals) then
2219 Append_List (Default_Formals, Formals);
2220 end if;
2221
2222 return Assoc_List;
2223 end Analyze_Associations;
2224
2225 -------------------------------
2226 -- Analyze_Formal_Array_Type --
2227 -------------------------------
2228
2229 procedure Analyze_Formal_Array_Type
2230 (T : in out Entity_Id;
2231 Def : Node_Id)
2232 is
2233 DSS : Node_Id;
2234
2235 begin
2236 -- Treated like a non-generic array declaration, with additional
2237 -- semantic checks.
2238
2239 Enter_Name (T);
2240
2241 if Nkind (Def) = N_Constrained_Array_Definition then
2242 DSS := First (Discrete_Subtype_Definitions (Def));
2243 while Present (DSS) loop
2244 if Nkind_In (DSS, N_Subtype_Indication,
2245 N_Range,
2246 N_Attribute_Reference)
2247 then
2248 Error_Msg_N ("only a subtype mark is allowed in a formal", DSS);
2249 end if;
2250
2251 Next (DSS);
2252 end loop;
2253 end if;
2254
2255 Array_Type_Declaration (T, Def);
2256 Set_Is_Generic_Type (Base_Type (T));
2257
2258 if Ekind (Component_Type (T)) = E_Incomplete_Type
2259 and then No (Full_View (Component_Type (T)))
2260 then
2261 Error_Msg_N ("premature usage of incomplete type", Def);
2262
2263 -- Check that range constraint is not allowed on the component type
2264 -- of a generic formal array type (AARM 12.5.3(3))
2265
2266 elsif Is_Internal (Component_Type (T))
2267 and then Present (Subtype_Indication (Component_Definition (Def)))
2268 and then Nkind (Original_Node
2269 (Subtype_Indication (Component_Definition (Def)))) =
2270 N_Subtype_Indication
2271 then
2272 Error_Msg_N
2273 ("in a formal, a subtype indication can only be "
2274 & "a subtype mark (RM 12.5.3(3))",
2275 Subtype_Indication (Component_Definition (Def)));
2276 end if;
2277
2278 end Analyze_Formal_Array_Type;
2279
2280 ---------------------------------------------
2281 -- Analyze_Formal_Decimal_Fixed_Point_Type --
2282 ---------------------------------------------
2283
2284 -- As for other generic types, we create a valid type representation with
2285 -- legal but arbitrary attributes, whose values are never considered
2286 -- static. For all scalar types we introduce an anonymous base type, with
2287 -- the same attributes. We choose the corresponding integer type to be
2288 -- Standard_Integer.
2289 -- Here and in other similar routines, the Sloc of the generated internal
2290 -- type must be the same as the sloc of the defining identifier of the
2291 -- formal type declaration, to provide proper source navigation.
2292
2293 procedure Analyze_Formal_Decimal_Fixed_Point_Type
2294 (T : Entity_Id;
2295 Def : Node_Id)
2296 is
2297 Loc : constant Source_Ptr := Sloc (Def);
2298
2299 Base : constant Entity_Id :=
2300 New_Internal_Entity
2301 (E_Decimal_Fixed_Point_Type,
2302 Current_Scope,
2303 Sloc (Defining_Identifier (Parent (Def))), 'G');
2304
2305 Int_Base : constant Entity_Id := Standard_Integer;
2306 Delta_Val : constant Ureal := Ureal_1;
2307 Digs_Val : constant Uint := Uint_6;
2308
2309 function Make_Dummy_Bound return Node_Id;
2310 -- Return a properly typed universal real literal to use as a bound
2311
2312 ----------------------
2313 -- Make_Dummy_Bound --
2314 ----------------------
2315
2316 function Make_Dummy_Bound return Node_Id is
2317 Bound : constant Node_Id := Make_Real_Literal (Loc, Ureal_1);
2318 begin
2319 Set_Etype (Bound, Universal_Real);
2320 return Bound;
2321 end Make_Dummy_Bound;
2322
2323 -- Start of processing for Analyze_Formal_Decimal_Fixed_Point_Type
2324
2325 begin
2326 Enter_Name (T);
2327
2328 Set_Etype (Base, Base);
2329 Set_Size_Info (Base, Int_Base);
2330 Set_RM_Size (Base, RM_Size (Int_Base));
2331 Set_First_Rep_Item (Base, First_Rep_Item (Int_Base));
2332 Set_Digits_Value (Base, Digs_Val);
2333 Set_Delta_Value (Base, Delta_Val);
2334 Set_Small_Value (Base, Delta_Val);
2335 Set_Scalar_Range (Base,
2336 Make_Range (Loc,
2337 Low_Bound => Make_Dummy_Bound,
2338 High_Bound => Make_Dummy_Bound));
2339
2340 Set_Is_Generic_Type (Base);
2341 Set_Parent (Base, Parent (Def));
2342
2343 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
2344 Set_Etype (T, Base);
2345 Set_Size_Info (T, Int_Base);
2346 Set_RM_Size (T, RM_Size (Int_Base));
2347 Set_First_Rep_Item (T, First_Rep_Item (Int_Base));
2348 Set_Digits_Value (T, Digs_Val);
2349 Set_Delta_Value (T, Delta_Val);
2350 Set_Small_Value (T, Delta_Val);
2351 Set_Scalar_Range (T, Scalar_Range (Base));
2352 Set_Is_Constrained (T);
2353
2354 Check_Restriction (No_Fixed_Point, Def);
2355 end Analyze_Formal_Decimal_Fixed_Point_Type;
2356
2357 -------------------------------------------
2358 -- Analyze_Formal_Derived_Interface_Type --
2359 -------------------------------------------
2360
2361 procedure Analyze_Formal_Derived_Interface_Type
2362 (N : Node_Id;
2363 T : Entity_Id;
2364 Def : Node_Id)
2365 is
2366 Loc : constant Source_Ptr := Sloc (Def);
2367
2368 begin
2369 -- Rewrite as a type declaration of a derived type. This ensures that
2370 -- the interface list and primitive operations are properly captured.
2371
2372 Rewrite (N,
2373 Make_Full_Type_Declaration (Loc,
2374 Defining_Identifier => T,
2375 Type_Definition => Def));
2376 Analyze (N);
2377 Set_Is_Generic_Type (T);
2378 end Analyze_Formal_Derived_Interface_Type;
2379
2380 ---------------------------------
2381 -- Analyze_Formal_Derived_Type --
2382 ---------------------------------
2383
2384 procedure Analyze_Formal_Derived_Type
2385 (N : Node_Id;
2386 T : Entity_Id;
2387 Def : Node_Id)
2388 is
2389 Loc : constant Source_Ptr := Sloc (Def);
2390 Unk_Disc : constant Boolean := Unknown_Discriminants_Present (N);
2391 New_N : Node_Id;
2392
2393 begin
2394 Set_Is_Generic_Type (T);
2395
2396 if Private_Present (Def) then
2397 New_N :=
2398 Make_Private_Extension_Declaration (Loc,
2399 Defining_Identifier => T,
2400 Discriminant_Specifications => Discriminant_Specifications (N),
2401 Unknown_Discriminants_Present => Unk_Disc,
2402 Subtype_Indication => Subtype_Mark (Def),
2403 Interface_List => Interface_List (Def));
2404
2405 Set_Abstract_Present (New_N, Abstract_Present (Def));
2406 Set_Limited_Present (New_N, Limited_Present (Def));
2407 Set_Synchronized_Present (New_N, Synchronized_Present (Def));
2408
2409 else
2410 New_N :=
2411 Make_Full_Type_Declaration (Loc,
2412 Defining_Identifier => T,
2413 Discriminant_Specifications =>
2414 Discriminant_Specifications (Parent (T)),
2415 Type_Definition =>
2416 Make_Derived_Type_Definition (Loc,
2417 Subtype_Indication => Subtype_Mark (Def)));
2418
2419 Set_Abstract_Present
2420 (Type_Definition (New_N), Abstract_Present (Def));
2421 Set_Limited_Present
2422 (Type_Definition (New_N), Limited_Present (Def));
2423 end if;
2424
2425 Rewrite (N, New_N);
2426 Analyze (N);
2427
2428 if Unk_Disc then
2429 if not Is_Composite_Type (T) then
2430 Error_Msg_N
2431 ("unknown discriminants not allowed for elementary types", N);
2432 else
2433 Set_Has_Unknown_Discriminants (T);
2434 Set_Is_Constrained (T, False);
2435 end if;
2436 end if;
2437
2438 -- If the parent type has a known size, so does the formal, which makes
2439 -- legal representation clauses that involve the formal.
2440
2441 Set_Size_Known_At_Compile_Time
2442 (T, Size_Known_At_Compile_Time (Entity (Subtype_Mark (Def))));
2443 end Analyze_Formal_Derived_Type;
2444
2445 ----------------------------------
2446 -- Analyze_Formal_Discrete_Type --
2447 ----------------------------------
2448
2449 -- The operations defined for a discrete types are those of an enumeration
2450 -- type. The size is set to an arbitrary value, for use in analyzing the
2451 -- generic unit.
2452
2453 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id) is
2454 Loc : constant Source_Ptr := Sloc (Def);
2455 Lo : Node_Id;
2456 Hi : Node_Id;
2457
2458 Base : constant Entity_Id :=
2459 New_Internal_Entity
2460 (E_Floating_Point_Type, Current_Scope,
2461 Sloc (Defining_Identifier (Parent (Def))), 'G');
2462
2463 begin
2464 Enter_Name (T);
2465 Set_Ekind (T, E_Enumeration_Subtype);
2466 Set_Etype (T, Base);
2467 Init_Size (T, 8);
2468 Init_Alignment (T);
2469 Set_Is_Generic_Type (T);
2470 Set_Is_Constrained (T);
2471
2472 -- For semantic analysis, the bounds of the type must be set to some
2473 -- non-static value. The simplest is to create attribute nodes for those
2474 -- bounds, that refer to the type itself. These bounds are never
2475 -- analyzed but serve as place-holders.
2476
2477 Lo :=
2478 Make_Attribute_Reference (Loc,
2479 Attribute_Name => Name_First,
2480 Prefix => New_Occurrence_Of (T, Loc));
2481 Set_Etype (Lo, T);
2482
2483 Hi :=
2484 Make_Attribute_Reference (Loc,
2485 Attribute_Name => Name_Last,
2486 Prefix => New_Occurrence_Of (T, Loc));
2487 Set_Etype (Hi, T);
2488
2489 Set_Scalar_Range (T,
2490 Make_Range (Loc,
2491 Low_Bound => Lo,
2492 High_Bound => Hi));
2493
2494 Set_Ekind (Base, E_Enumeration_Type);
2495 Set_Etype (Base, Base);
2496 Init_Size (Base, 8);
2497 Init_Alignment (Base);
2498 Set_Is_Generic_Type (Base);
2499 Set_Scalar_Range (Base, Scalar_Range (T));
2500 Set_Parent (Base, Parent (Def));
2501 end Analyze_Formal_Discrete_Type;
2502
2503 ----------------------------------
2504 -- Analyze_Formal_Floating_Type --
2505 ---------------------------------
2506
2507 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id) is
2508 Base : constant Entity_Id :=
2509 New_Internal_Entity
2510 (E_Floating_Point_Type, Current_Scope,
2511 Sloc (Defining_Identifier (Parent (Def))), 'G');
2512
2513 begin
2514 -- The various semantic attributes are taken from the predefined type
2515 -- Float, just so that all of them are initialized. Their values are
2516 -- never used because no constant folding or expansion takes place in
2517 -- the generic itself.
2518
2519 Enter_Name (T);
2520 Set_Ekind (T, E_Floating_Point_Subtype);
2521 Set_Etype (T, Base);
2522 Set_Size_Info (T, (Standard_Float));
2523 Set_RM_Size (T, RM_Size (Standard_Float));
2524 Set_Digits_Value (T, Digits_Value (Standard_Float));
2525 Set_Scalar_Range (T, Scalar_Range (Standard_Float));
2526 Set_Is_Constrained (T);
2527
2528 Set_Is_Generic_Type (Base);
2529 Set_Etype (Base, Base);
2530 Set_Size_Info (Base, (Standard_Float));
2531 Set_RM_Size (Base, RM_Size (Standard_Float));
2532 Set_Digits_Value (Base, Digits_Value (Standard_Float));
2533 Set_Scalar_Range (Base, Scalar_Range (Standard_Float));
2534 Set_Parent (Base, Parent (Def));
2535
2536 Check_Restriction (No_Floating_Point, Def);
2537 end Analyze_Formal_Floating_Type;
2538
2539 -----------------------------------
2540 -- Analyze_Formal_Interface_Type;--
2541 -----------------------------------
2542
2543 procedure Analyze_Formal_Interface_Type
2544 (N : Node_Id;
2545 T : Entity_Id;
2546 Def : Node_Id)
2547 is
2548 Loc : constant Source_Ptr := Sloc (N);
2549 New_N : Node_Id;
2550
2551 begin
2552 New_N :=
2553 Make_Full_Type_Declaration (Loc,
2554 Defining_Identifier => T,
2555 Type_Definition => Def);
2556
2557 Rewrite (N, New_N);
2558 Analyze (N);
2559 Set_Is_Generic_Type (T);
2560 end Analyze_Formal_Interface_Type;
2561
2562 ---------------------------------
2563 -- Analyze_Formal_Modular_Type --
2564 ---------------------------------
2565
2566 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id) is
2567 begin
2568 -- Apart from their entity kind, generic modular types are treated like
2569 -- signed integer types, and have the same attributes.
2570
2571 Analyze_Formal_Signed_Integer_Type (T, Def);
2572 Set_Ekind (T, E_Modular_Integer_Subtype);
2573 Set_Ekind (Etype (T), E_Modular_Integer_Type);
2574
2575 end Analyze_Formal_Modular_Type;
2576
2577 ---------------------------------------
2578 -- Analyze_Formal_Object_Declaration --
2579 ---------------------------------------
2580
2581 procedure Analyze_Formal_Object_Declaration (N : Node_Id) is
2582 E : constant Node_Id := Default_Expression (N);
2583 Id : constant Node_Id := Defining_Identifier (N);
2584 K : Entity_Kind;
2585 T : Node_Id;
2586
2587 begin
2588 Enter_Name (Id);
2589
2590 -- Determine the mode of the formal object
2591
2592 if Out_Present (N) then
2593 K := E_Generic_In_Out_Parameter;
2594
2595 if not In_Present (N) then
2596 Error_Msg_N ("formal generic objects cannot have mode OUT", N);
2597 end if;
2598
2599 else
2600 K := E_Generic_In_Parameter;
2601 end if;
2602
2603 if Present (Subtype_Mark (N)) then
2604 Find_Type (Subtype_Mark (N));
2605 T := Entity (Subtype_Mark (N));
2606
2607 -- Verify that there is no redundant null exclusion
2608
2609 if Null_Exclusion_Present (N) then
2610 if not Is_Access_Type (T) then
2611 Error_Msg_N
2612 ("null exclusion can only apply to an access type", N);
2613
2614 elsif Can_Never_Be_Null (T) then
2615 Error_Msg_NE
2616 ("`NOT NULL` not allowed (& already excludes null)", N, T);
2617 end if;
2618 end if;
2619
2620 -- Ada 2005 (AI-423): Formal object with an access definition
2621
2622 else
2623 Check_Access_Definition (N);
2624 T := Access_Definition
2625 (Related_Nod => N,
2626 N => Access_Definition (N));
2627 end if;
2628
2629 if Ekind (T) = E_Incomplete_Type then
2630 declare
2631 Error_Node : Node_Id;
2632
2633 begin
2634 if Present (Subtype_Mark (N)) then
2635 Error_Node := Subtype_Mark (N);
2636 else
2637 Check_Access_Definition (N);
2638 Error_Node := Access_Definition (N);
2639 end if;
2640
2641 Error_Msg_N ("premature usage of incomplete type", Error_Node);
2642 end;
2643 end if;
2644
2645 if K = E_Generic_In_Parameter then
2646
2647 -- Ada 2005 (AI-287): Limited aggregates allowed in generic formals
2648
2649 if Ada_Version < Ada_2005 and then Is_Limited_Type (T) then
2650 Error_Msg_N
2651 ("generic formal of mode IN must not be of limited type", N);
2652 Explain_Limited_Type (T, N);
2653 end if;
2654
2655 if Is_Abstract_Type (T) then
2656 Error_Msg_N
2657 ("generic formal of mode IN must not be of abstract type", N);
2658 end if;
2659
2660 if Present (E) then
2661 Preanalyze_Spec_Expression (E, T);
2662
2663 if Is_Limited_Type (T) and then not OK_For_Limited_Init (T, E) then
2664 Error_Msg_N
2665 ("initialization not allowed for limited types", E);
2666 Explain_Limited_Type (T, E);
2667 end if;
2668 end if;
2669
2670 Set_Ekind (Id, K);
2671 Set_Etype (Id, T);
2672
2673 -- Case of generic IN OUT parameter
2674
2675 else
2676 -- If the formal has an unconstrained type, construct its actual
2677 -- subtype, as is done for subprogram formals. In this fashion, all
2678 -- its uses can refer to specific bounds.
2679
2680 Set_Ekind (Id, K);
2681 Set_Etype (Id, T);
2682
2683 if (Is_Array_Type (T) and then not Is_Constrained (T))
2684 or else (Ekind (T) = E_Record_Type and then Has_Discriminants (T))
2685 then
2686 declare
2687 Non_Freezing_Ref : constant Node_Id :=
2688 New_Occurrence_Of (Id, Sloc (Id));
2689 Decl : Node_Id;
2690
2691 begin
2692 -- Make sure the actual subtype doesn't generate bogus freezing
2693
2694 Set_Must_Not_Freeze (Non_Freezing_Ref);
2695 Decl := Build_Actual_Subtype (T, Non_Freezing_Ref);
2696 Insert_Before_And_Analyze (N, Decl);
2697 Set_Actual_Subtype (Id, Defining_Identifier (Decl));
2698 end;
2699 else
2700 Set_Actual_Subtype (Id, T);
2701 end if;
2702
2703 if Present (E) then
2704 Error_Msg_N
2705 ("initialization not allowed for `IN OUT` formals", N);
2706 end if;
2707 end if;
2708
2709 if Has_Aspects (N) then
2710 Analyze_Aspect_Specifications (N, Id);
2711 end if;
2712 end Analyze_Formal_Object_Declaration;
2713
2714 ----------------------------------------------
2715 -- Analyze_Formal_Ordinary_Fixed_Point_Type --
2716 ----------------------------------------------
2717
2718 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
2719 (T : Entity_Id;
2720 Def : Node_Id)
2721 is
2722 Loc : constant Source_Ptr := Sloc (Def);
2723 Base : constant Entity_Id :=
2724 New_Internal_Entity
2725 (E_Ordinary_Fixed_Point_Type, Current_Scope,
2726 Sloc (Defining_Identifier (Parent (Def))), 'G');
2727
2728 begin
2729 -- The semantic attributes are set for completeness only, their values
2730 -- will never be used, since all properties of the type are non-static.
2731
2732 Enter_Name (T);
2733 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
2734 Set_Etype (T, Base);
2735 Set_Size_Info (T, Standard_Integer);
2736 Set_RM_Size (T, RM_Size (Standard_Integer));
2737 Set_Small_Value (T, Ureal_1);
2738 Set_Delta_Value (T, Ureal_1);
2739 Set_Scalar_Range (T,
2740 Make_Range (Loc,
2741 Low_Bound => Make_Real_Literal (Loc, Ureal_1),
2742 High_Bound => Make_Real_Literal (Loc, Ureal_1)));
2743 Set_Is_Constrained (T);
2744
2745 Set_Is_Generic_Type (Base);
2746 Set_Etype (Base, Base);
2747 Set_Size_Info (Base, Standard_Integer);
2748 Set_RM_Size (Base, RM_Size (Standard_Integer));
2749 Set_Small_Value (Base, Ureal_1);
2750 Set_Delta_Value (Base, Ureal_1);
2751 Set_Scalar_Range (Base, Scalar_Range (T));
2752 Set_Parent (Base, Parent (Def));
2753
2754 Check_Restriction (No_Fixed_Point, Def);
2755 end Analyze_Formal_Ordinary_Fixed_Point_Type;
2756
2757 ----------------------------------------
2758 -- Analyze_Formal_Package_Declaration --
2759 ----------------------------------------
2760
2761 procedure Analyze_Formal_Package_Declaration (N : Node_Id) is
2762 Gen_Id : constant Node_Id := Name (N);
2763 Loc : constant Source_Ptr := Sloc (N);
2764 Pack_Id : constant Entity_Id := Defining_Identifier (N);
2765 Formal : Entity_Id;
2766 Gen_Decl : Node_Id;
2767 Gen_Unit : Entity_Id;
2768 Renaming : Node_Id;
2769
2770 Vis_Prims_List : Elist_Id := No_Elist;
2771 -- List of primitives made temporarily visible in the instantiation
2772 -- to match the visibility of the formal type.
2773
2774 function Build_Local_Package return Node_Id;
2775 -- The formal package is rewritten so that its parameters are replaced
2776 -- with corresponding declarations. For parameters with bona fide
2777 -- associations these declarations are created by Analyze_Associations
2778 -- as for a regular instantiation. For boxed parameters, we preserve
2779 -- the formal declarations and analyze them, in order to introduce
2780 -- entities of the right kind in the environment of the formal.
2781
2782 -------------------------
2783 -- Build_Local_Package --
2784 -------------------------
2785
2786 function Build_Local_Package return Node_Id is
2787 Decls : List_Id;
2788 Pack_Decl : Node_Id;
2789
2790 begin
2791 -- Within the formal, the name of the generic package is a renaming
2792 -- of the formal (as for a regular instantiation).
2793
2794 Pack_Decl :=
2795 Make_Package_Declaration (Loc,
2796 Specification =>
2797 Copy_Generic_Node
2798 (Specification (Original_Node (Gen_Decl)),
2799 Empty, Instantiating => True));
2800
2801 Renaming :=
2802 Make_Package_Renaming_Declaration (Loc,
2803 Defining_Unit_Name =>
2804 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
2805 Name => New_Occurrence_Of (Formal, Loc));
2806
2807 if Nkind (Gen_Id) = N_Identifier
2808 and then Chars (Gen_Id) = Chars (Pack_Id)
2809 then
2810 Error_Msg_NE
2811 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
2812 end if;
2813
2814 -- If the formal is declared with a box, or with an others choice,
2815 -- create corresponding declarations for all entities in the formal
2816 -- part, so that names with the proper types are available in the
2817 -- specification of the formal package.
2818
2819 -- On the other hand, if there are no associations, then all the
2820 -- formals must have defaults, and this will be checked by the
2821 -- call to Analyze_Associations.
2822
2823 if Box_Present (N)
2824 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2825 then
2826 declare
2827 Formal_Decl : Node_Id;
2828
2829 begin
2830 -- TBA : for a formal package, need to recurse ???
2831
2832 Decls := New_List;
2833 Formal_Decl :=
2834 First
2835 (Generic_Formal_Declarations (Original_Node (Gen_Decl)));
2836 while Present (Formal_Decl) loop
2837 Append_To
2838 (Decls,
2839 Copy_Generic_Node
2840 (Formal_Decl, Empty, Instantiating => True));
2841 Next (Formal_Decl);
2842 end loop;
2843 end;
2844
2845 -- If generic associations are present, use Analyze_Associations to
2846 -- create the proper renaming declarations.
2847
2848 else
2849 declare
2850 Act_Tree : constant Node_Id :=
2851 Copy_Generic_Node
2852 (Original_Node (Gen_Decl), Empty,
2853 Instantiating => True);
2854
2855 begin
2856 Generic_Renamings.Set_Last (0);
2857 Generic_Renamings_HTable.Reset;
2858 Instantiation_Node := N;
2859
2860 Decls :=
2861 Analyze_Associations
2862 (I_Node => Original_Node (N),
2863 Formals => Generic_Formal_Declarations (Act_Tree),
2864 F_Copy => Generic_Formal_Declarations (Gen_Decl));
2865
2866 Vis_Prims_List := Check_Hidden_Primitives (Decls);
2867 end;
2868 end if;
2869
2870 Append (Renaming, To => Decls);
2871
2872 -- Add generated declarations ahead of local declarations in
2873 -- the package.
2874
2875 if No (Visible_Declarations (Specification (Pack_Decl))) then
2876 Set_Visible_Declarations (Specification (Pack_Decl), Decls);
2877 else
2878 Insert_List_Before
2879 (First (Visible_Declarations (Specification (Pack_Decl))),
2880 Decls);
2881 end if;
2882
2883 return Pack_Decl;
2884 end Build_Local_Package;
2885
2886 -- Local variables
2887
2888 Save_ISMP : constant Boolean := Ignore_SPARK_Mode_Pragmas_In_Instance;
2889 -- Save flag Ignore_SPARK_Mode_Pragmas_In_Instance for restore on exit
2890
2891 Associations : Boolean := True;
2892 New_N : Node_Id;
2893 Parent_Installed : Boolean := False;
2894 Parent_Instance : Entity_Id;
2895 Renaming_In_Par : Entity_Id;
2896
2897 -- Start of processing for Analyze_Formal_Package_Declaration
2898
2899 begin
2900 Check_Text_IO_Special_Unit (Gen_Id);
2901
2902 Init_Env;
2903 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
2904 Gen_Unit := Entity (Gen_Id);
2905
2906 -- Check for a formal package that is a package renaming
2907
2908 if Present (Renamed_Object (Gen_Unit)) then
2909
2910 -- Indicate that unit is used, before replacing it with renamed
2911 -- entity for use below.
2912
2913 if In_Extended_Main_Source_Unit (N) then
2914 Set_Is_Instantiated (Gen_Unit);
2915 Generate_Reference (Gen_Unit, N);
2916 end if;
2917
2918 Gen_Unit := Renamed_Object (Gen_Unit);
2919 end if;
2920
2921 if Ekind (Gen_Unit) /= E_Generic_Package then
2922 Error_Msg_N ("expect generic package name", Gen_Id);
2923 Restore_Env;
2924 goto Leave;
2925
2926 elsif Gen_Unit = Current_Scope then
2927 Error_Msg_N
2928 ("generic package cannot be used as a formal package of itself",
2929 Gen_Id);
2930 Restore_Env;
2931 goto Leave;
2932
2933 elsif In_Open_Scopes (Gen_Unit) then
2934 if Is_Compilation_Unit (Gen_Unit)
2935 and then Is_Child_Unit (Current_Scope)
2936 then
2937 -- Special-case the error when the formal is a parent, and
2938 -- continue analysis to minimize cascaded errors.
2939
2940 Error_Msg_N
2941 ("generic parent cannot be used as formal package of a child "
2942 & "unit", Gen_Id);
2943
2944 else
2945 Error_Msg_N
2946 ("generic package cannot be used as a formal package within "
2947 & "itself", Gen_Id);
2948 Restore_Env;
2949 goto Leave;
2950 end if;
2951 end if;
2952
2953 -- Check that name of formal package does not hide name of generic,
2954 -- or its leading prefix. This check must be done separately because
2955 -- the name of the generic has already been analyzed.
2956
2957 declare
2958 Gen_Name : Entity_Id;
2959
2960 begin
2961 Gen_Name := Gen_Id;
2962 while Nkind (Gen_Name) = N_Expanded_Name loop
2963 Gen_Name := Prefix (Gen_Name);
2964 end loop;
2965
2966 if Chars (Gen_Name) = Chars (Pack_Id) then
2967 Error_Msg_NE
2968 ("& is hidden within declaration of formal package",
2969 Gen_Id, Gen_Name);
2970 end if;
2971 end;
2972
2973 if Box_Present (N)
2974 or else No (Generic_Associations (N))
2975 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2976 then
2977 Associations := False;
2978 end if;
2979
2980 -- If there are no generic associations, the generic parameters appear
2981 -- as local entities and are instantiated like them. We copy the generic
2982 -- package declaration as if it were an instantiation, and analyze it
2983 -- like a regular package, except that we treat the formals as
2984 -- additional visible components.
2985
2986 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
2987
2988 if In_Extended_Main_Source_Unit (N) then
2989 Set_Is_Instantiated (Gen_Unit);
2990 Generate_Reference (Gen_Unit, N);
2991 end if;
2992
2993 Formal := New_Copy (Pack_Id);
2994 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
2995
2996 -- Make local generic without formals. The formals will be replaced with
2997 -- internal declarations.
2998
2999 begin
3000 New_N := Build_Local_Package;
3001
3002 -- If there are errors in the parameter list, Analyze_Associations
3003 -- raises Instantiation_Error. Patch the declaration to prevent further
3004 -- exception propagation.
3005
3006 exception
3007 when Instantiation_Error =>
3008 Enter_Name (Formal);
3009 Set_Ekind (Formal, E_Variable);
3010 Set_Etype (Formal, Any_Type);
3011 Restore_Hidden_Primitives (Vis_Prims_List);
3012
3013 if Parent_Installed then
3014 Remove_Parent;
3015 end if;
3016
3017 goto Leave;
3018 end;
3019
3020 Rewrite (N, New_N);
3021 Set_Defining_Unit_Name (Specification (New_N), Formal);
3022 Set_Generic_Parent (Specification (N), Gen_Unit);
3023 Set_Instance_Env (Gen_Unit, Formal);
3024 Set_Is_Generic_Instance (Formal);
3025
3026 Enter_Name (Formal);
3027 Set_Ekind (Formal, E_Package);
3028 Set_Etype (Formal, Standard_Void_Type);
3029 Set_Inner_Instances (Formal, New_Elmt_List);
3030
3031 -- It is unclear that any aspects can apply to a formal package
3032 -- declaration, given that they look like a hidden conformance
3033 -- requirement on the corresponding actual. However, Abstract_State
3034 -- must be treated specially because it generates declarations that
3035 -- must appear before other declarations in the specification and
3036 -- must be analyzed at once.
3037
3038 if Present (Aspect_Specifications (Gen_Decl)) then
3039 if No (Aspect_Specifications (N)) then
3040 Set_Aspect_Specifications (N, New_List);
3041 Set_Has_Aspects (N);
3042 end if;
3043
3044 declare
3045 ASN : Node_Id := First (Aspect_Specifications (Gen_Decl));
3046 New_A : Node_Id;
3047
3048 begin
3049 while Present (ASN) loop
3050 if Get_Aspect_Id (ASN) = Aspect_Abstract_State then
3051 New_A :=
3052 Copy_Generic_Node (ASN, Empty, Instantiating => True);
3053 Set_Entity (New_A, Formal);
3054 Set_Analyzed (New_A, False);
3055 Append (New_A, Aspect_Specifications (N));
3056 Analyze_Aspect_Specifications (N, Formal);
3057 exit;
3058 end if;
3059
3060 Next (ASN);
3061 end loop;
3062 end;
3063 end if;
3064
3065 Push_Scope (Formal);
3066
3067 -- Manually set the SPARK_Mode from the context because the package
3068 -- declaration is never analyzed.
3069
3070 Set_SPARK_Pragma (Formal, SPARK_Mode_Pragma);
3071 Set_SPARK_Aux_Pragma (Formal, SPARK_Mode_Pragma);
3072 Set_SPARK_Pragma_Inherited (Formal);
3073 Set_SPARK_Aux_Pragma_Inherited (Formal);
3074
3075 if Is_Child_Unit (Gen_Unit) and then Parent_Installed then
3076
3077 -- Similarly, we have to make the name of the formal visible in the
3078 -- parent instance, to resolve properly fully qualified names that
3079 -- may appear in the generic unit. The parent instance has been
3080 -- placed on the scope stack ahead of the current scope.
3081
3082 Parent_Instance := Scope_Stack.Table (Scope_Stack.Last - 1).Entity;
3083
3084 Renaming_In_Par :=
3085 Make_Defining_Identifier (Loc, Chars (Gen_Unit));
3086 Set_Ekind (Renaming_In_Par, E_Package);
3087 Set_Etype (Renaming_In_Par, Standard_Void_Type);
3088 Set_Scope (Renaming_In_Par, Parent_Instance);
3089 Set_Parent (Renaming_In_Par, Parent (Formal));
3090 Set_Renamed_Object (Renaming_In_Par, Formal);
3091 Append_Entity (Renaming_In_Par, Parent_Instance);
3092 end if;
3093
3094 -- A formal package declaration behaves as a package instantiation with
3095 -- respect to SPARK_Mode "off". If the annotation is "off" or altogether
3096 -- missing, set the global flag which signals Analyze_Pragma to ingnore
3097 -- all SPARK_Mode pragmas within the generic_package_name.
3098
3099 if SPARK_Mode /= On then
3100 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
3101
3102 -- Mark the formal spec in case the body is instantiated at a later
3103 -- pass. This preserves the original context in effect for the body.
3104
3105 Set_Ignore_SPARK_Mode_Pragmas (Formal);
3106 end if;
3107
3108 Analyze (Specification (N));
3109
3110 -- The formals for which associations are provided are not visible
3111 -- outside of the formal package. The others are still declared by a
3112 -- formal parameter declaration.
3113
3114 -- If there are no associations, the only local entity to hide is the
3115 -- generated package renaming itself.
3116
3117 declare
3118 E : Entity_Id;
3119
3120 begin
3121 E := First_Entity (Formal);
3122 while Present (E) loop
3123 if Associations and then not Is_Generic_Formal (E) then
3124 Set_Is_Hidden (E);
3125 end if;
3126
3127 if Ekind (E) = E_Package and then Renamed_Entity (E) = Formal then
3128 Set_Is_Hidden (E);
3129 exit;
3130 end if;
3131
3132 Next_Entity (E);
3133 end loop;
3134 end;
3135
3136 End_Package_Scope (Formal);
3137 Restore_Hidden_Primitives (Vis_Prims_List);
3138
3139 if Parent_Installed then
3140 Remove_Parent;
3141 end if;
3142
3143 Restore_Env;
3144
3145 -- Inside the generic unit, the formal package is a regular package, but
3146 -- no body is needed for it. Note that after instantiation, the defining
3147 -- unit name we need is in the new tree and not in the original (see
3148 -- Package_Instantiation). A generic formal package is an instance, and
3149 -- can be used as an actual for an inner instance.
3150
3151 Set_Has_Completion (Formal, True);
3152
3153 -- Add semantic information to the original defining identifier.
3154
3155 Set_Ekind (Pack_Id, E_Package);
3156 Set_Etype (Pack_Id, Standard_Void_Type);
3157 Set_Scope (Pack_Id, Scope (Formal));
3158 Set_Has_Completion (Pack_Id, True);
3159
3160 <<Leave>>
3161 if Has_Aspects (N) then
3162 -- Unclear that any other aspects may appear here, snalyze them
3163 -- for completion, given that the grammar allows their appearance.
3164
3165 Analyze_Aspect_Specifications (N, Pack_Id);
3166 end if;
3167
3168 Ignore_SPARK_Mode_Pragmas_In_Instance := Save_ISMP;
3169 end Analyze_Formal_Package_Declaration;
3170
3171 ---------------------------------
3172 -- Analyze_Formal_Private_Type --
3173 ---------------------------------
3174
3175 procedure Analyze_Formal_Private_Type
3176 (N : Node_Id;
3177 T : Entity_Id;
3178 Def : Node_Id)
3179 is
3180 begin
3181 New_Private_Type (N, T, Def);
3182
3183 -- Set the size to an arbitrary but legal value
3184
3185 Set_Size_Info (T, Standard_Integer);
3186 Set_RM_Size (T, RM_Size (Standard_Integer));
3187 end Analyze_Formal_Private_Type;
3188
3189 ------------------------------------
3190 -- Analyze_Formal_Incomplete_Type --
3191 ------------------------------------
3192
3193 procedure Analyze_Formal_Incomplete_Type
3194 (T : Entity_Id;
3195 Def : Node_Id)
3196 is
3197 begin
3198 Enter_Name (T);
3199 Set_Ekind (T, E_Incomplete_Type);
3200 Set_Etype (T, T);
3201 Set_Private_Dependents (T, New_Elmt_List);
3202
3203 if Tagged_Present (Def) then
3204 Set_Is_Tagged_Type (T);
3205 Make_Class_Wide_Type (T);
3206 Set_Direct_Primitive_Operations (T, New_Elmt_List);
3207 end if;
3208 end Analyze_Formal_Incomplete_Type;
3209
3210 ----------------------------------------
3211 -- Analyze_Formal_Signed_Integer_Type --
3212 ----------------------------------------
3213
3214 procedure Analyze_Formal_Signed_Integer_Type
3215 (T : Entity_Id;
3216 Def : Node_Id)
3217 is
3218 Base : constant Entity_Id :=
3219 New_Internal_Entity
3220 (E_Signed_Integer_Type,
3221 Current_Scope,
3222 Sloc (Defining_Identifier (Parent (Def))), 'G');
3223
3224 begin
3225 Enter_Name (T);
3226
3227 Set_Ekind (T, E_Signed_Integer_Subtype);
3228 Set_Etype (T, Base);
3229 Set_Size_Info (T, Standard_Integer);
3230 Set_RM_Size (T, RM_Size (Standard_Integer));
3231 Set_Scalar_Range (T, Scalar_Range (Standard_Integer));
3232 Set_Is_Constrained (T);
3233
3234 Set_Is_Generic_Type (Base);
3235 Set_Size_Info (Base, Standard_Integer);
3236 Set_RM_Size (Base, RM_Size (Standard_Integer));
3237 Set_Etype (Base, Base);
3238 Set_Scalar_Range (Base, Scalar_Range (Standard_Integer));
3239 Set_Parent (Base, Parent (Def));
3240 end Analyze_Formal_Signed_Integer_Type;
3241
3242 -------------------------------------------
3243 -- Analyze_Formal_Subprogram_Declaration --
3244 -------------------------------------------
3245
3246 procedure Analyze_Formal_Subprogram_Declaration (N : Node_Id) is
3247 Spec : constant Node_Id := Specification (N);
3248 Def : constant Node_Id := Default_Name (N);
3249 Nam : constant Entity_Id := Defining_Unit_Name (Spec);
3250 Subp : Entity_Id;
3251
3252 begin
3253 if Nam = Error then
3254 return;
3255 end if;
3256
3257 if Nkind (Nam) = N_Defining_Program_Unit_Name then
3258 Error_Msg_N ("name of formal subprogram must be a direct name", Nam);
3259 goto Leave;
3260 end if;
3261
3262 Analyze_Subprogram_Declaration (N);
3263 Set_Is_Formal_Subprogram (Nam);
3264 Set_Has_Completion (Nam);
3265
3266 if Nkind (N) = N_Formal_Abstract_Subprogram_Declaration then
3267 Set_Is_Abstract_Subprogram (Nam);
3268
3269 Set_Is_Dispatching_Operation (Nam);
3270
3271 -- A formal abstract procedure cannot have a null default
3272 -- (RM 12.6(4.1/2)).
3273
3274 if Nkind (Spec) = N_Procedure_Specification
3275 and then Null_Present (Spec)
3276 then
3277 Error_Msg_N
3278 ("a formal abstract subprogram cannot default to null", Spec);
3279 end if;
3280
3281 declare
3282 Ctrl_Type : constant Entity_Id := Find_Dispatching_Type (Nam);
3283 begin
3284 if No (Ctrl_Type) then
3285 Error_Msg_N
3286 ("abstract formal subprogram must have a controlling type",
3287 N);
3288
3289 elsif Ada_Version >= Ada_2012
3290 and then Is_Incomplete_Type (Ctrl_Type)
3291 then
3292 Error_Msg_NE
3293 ("controlling type of abstract formal subprogram cannot "
3294 & "be incomplete type", N, Ctrl_Type);
3295
3296 else
3297 Check_Controlling_Formals (Ctrl_Type, Nam);
3298 end if;
3299 end;
3300 end if;
3301
3302 -- Default name is resolved at the point of instantiation
3303
3304 if Box_Present (N) then
3305 null;
3306
3307 -- Else default is bound at the point of generic declaration
3308
3309 elsif Present (Def) then
3310 if Nkind (Def) = N_Operator_Symbol then
3311 Find_Direct_Name (Def);
3312
3313 elsif Nkind (Def) /= N_Attribute_Reference then
3314 Analyze (Def);
3315
3316 else
3317 -- For an attribute reference, analyze the prefix and verify
3318 -- that it has the proper profile for the subprogram.
3319
3320 Analyze (Prefix (Def));
3321 Valid_Default_Attribute (Nam, Def);
3322 goto Leave;
3323 end if;
3324
3325 -- Default name may be overloaded, in which case the interpretation
3326 -- with the correct profile must be selected, as for a renaming.
3327 -- If the definition is an indexed component, it must denote a
3328 -- member of an entry family. If it is a selected component, it
3329 -- can be a protected operation.
3330
3331 if Etype (Def) = Any_Type then
3332 goto Leave;
3333
3334 elsif Nkind (Def) = N_Selected_Component then
3335 if not Is_Overloadable (Entity (Selector_Name (Def))) then
3336 Error_Msg_N ("expect valid subprogram name as default", Def);
3337 end if;
3338
3339 elsif Nkind (Def) = N_Indexed_Component then
3340 if Is_Entity_Name (Prefix (Def)) then
3341 if Ekind (Entity (Prefix (Def))) /= E_Entry_Family then
3342 Error_Msg_N ("expect valid subprogram name as default", Def);
3343 end if;
3344
3345 elsif Nkind (Prefix (Def)) = N_Selected_Component then
3346 if Ekind (Entity (Selector_Name (Prefix (Def)))) /=
3347 E_Entry_Family
3348 then
3349 Error_Msg_N ("expect valid subprogram name as default", Def);
3350 end if;
3351
3352 else
3353 Error_Msg_N ("expect valid subprogram name as default", Def);
3354 goto Leave;
3355 end if;
3356
3357 elsif Nkind (Def) = N_Character_Literal then
3358
3359 -- Needs some type checks: subprogram should be parameterless???
3360
3361 Resolve (Def, (Etype (Nam)));
3362
3363 elsif not Is_Entity_Name (Def)
3364 or else not Is_Overloadable (Entity (Def))
3365 then
3366 Error_Msg_N ("expect valid subprogram name as default", Def);
3367 goto Leave;
3368
3369 elsif not Is_Overloaded (Def) then
3370 Subp := Entity (Def);
3371
3372 if Subp = Nam then
3373 Error_Msg_N ("premature usage of formal subprogram", Def);
3374
3375 elsif not Entity_Matches_Spec (Subp, Nam) then
3376 Error_Msg_N ("no visible entity matches specification", Def);
3377 end if;
3378
3379 -- More than one interpretation, so disambiguate as for a renaming
3380
3381 else
3382 declare
3383 I : Interp_Index;
3384 I1 : Interp_Index := 0;
3385 It : Interp;
3386 It1 : Interp;
3387
3388 begin
3389 Subp := Any_Id;
3390 Get_First_Interp (Def, I, It);
3391 while Present (It.Nam) loop
3392 if Entity_Matches_Spec (It.Nam, Nam) then
3393 if Subp /= Any_Id then
3394 It1 := Disambiguate (Def, I1, I, Etype (Subp));
3395
3396 if It1 = No_Interp then
3397 Error_Msg_N ("ambiguous default subprogram", Def);
3398 else
3399 Subp := It1.Nam;
3400 end if;
3401
3402 exit;
3403
3404 else
3405 I1 := I;
3406 Subp := It.Nam;
3407 end if;
3408 end if;
3409
3410 Get_Next_Interp (I, It);
3411 end loop;
3412 end;
3413
3414 if Subp /= Any_Id then
3415
3416 -- Subprogram found, generate reference to it
3417
3418 Set_Entity (Def, Subp);
3419 Generate_Reference (Subp, Def);
3420
3421 if Subp = Nam then
3422 Error_Msg_N ("premature usage of formal subprogram", Def);
3423
3424 elsif Ekind (Subp) /= E_Operator then
3425 Check_Mode_Conformant (Subp, Nam);
3426 end if;
3427
3428 else
3429 Error_Msg_N ("no visible subprogram matches specification", N);
3430 end if;
3431 end if;
3432 end if;
3433
3434 <<Leave>>
3435 if Has_Aspects (N) then
3436 Analyze_Aspect_Specifications (N, Nam);
3437 end if;
3438
3439 end Analyze_Formal_Subprogram_Declaration;
3440
3441 -------------------------------------
3442 -- Analyze_Formal_Type_Declaration --
3443 -------------------------------------
3444
3445 procedure Analyze_Formal_Type_Declaration (N : Node_Id) is
3446 Def : constant Node_Id := Formal_Type_Definition (N);
3447 T : Entity_Id;
3448
3449 begin
3450 T := Defining_Identifier (N);
3451
3452 if Present (Discriminant_Specifications (N))
3453 and then Nkind (Def) /= N_Formal_Private_Type_Definition
3454 then
3455 Error_Msg_N
3456 ("discriminants not allowed for this formal type", T);
3457 end if;
3458
3459 -- Enter the new name, and branch to specific routine
3460
3461 case Nkind (Def) is
3462 when N_Formal_Private_Type_Definition =>
3463 Analyze_Formal_Private_Type (N, T, Def);
3464
3465 when N_Formal_Derived_Type_Definition =>
3466 Analyze_Formal_Derived_Type (N, T, Def);
3467
3468 when N_Formal_Incomplete_Type_Definition =>
3469 Analyze_Formal_Incomplete_Type (T, Def);
3470
3471 when N_Formal_Discrete_Type_Definition =>
3472 Analyze_Formal_Discrete_Type (T, Def);
3473
3474 when N_Formal_Signed_Integer_Type_Definition =>
3475 Analyze_Formal_Signed_Integer_Type (T, Def);
3476
3477 when N_Formal_Modular_Type_Definition =>
3478 Analyze_Formal_Modular_Type (T, Def);
3479
3480 when N_Formal_Floating_Point_Definition =>
3481 Analyze_Formal_Floating_Type (T, Def);
3482
3483 when N_Formal_Ordinary_Fixed_Point_Definition =>
3484 Analyze_Formal_Ordinary_Fixed_Point_Type (T, Def);
3485
3486 when N_Formal_Decimal_Fixed_Point_Definition =>
3487 Analyze_Formal_Decimal_Fixed_Point_Type (T, Def);
3488
3489 when N_Array_Type_Definition =>
3490 Analyze_Formal_Array_Type (T, Def);
3491
3492 when N_Access_Function_Definition
3493 | N_Access_Procedure_Definition
3494 | N_Access_To_Object_Definition
3495 =>
3496 Analyze_Generic_Access_Type (T, Def);
3497
3498 -- Ada 2005: a interface declaration is encoded as an abstract
3499 -- record declaration or a abstract type derivation.
3500
3501 when N_Record_Definition =>
3502 Analyze_Formal_Interface_Type (N, T, Def);
3503
3504 when N_Derived_Type_Definition =>
3505 Analyze_Formal_Derived_Interface_Type (N, T, Def);
3506
3507 when N_Error =>
3508 null;
3509
3510 when others =>
3511 raise Program_Error;
3512 end case;
3513
3514 -- A formal type declaration declares a type and its first
3515 -- subtype.
3516
3517 Set_Is_Generic_Type (T);
3518 Set_Is_First_Subtype (T);
3519
3520 if Has_Aspects (N) then
3521 Analyze_Aspect_Specifications (N, T);
3522 end if;
3523 end Analyze_Formal_Type_Declaration;
3524
3525 ------------------------------------
3526 -- Analyze_Function_Instantiation --
3527 ------------------------------------
3528
3529 procedure Analyze_Function_Instantiation (N : Node_Id) is
3530 begin
3531 Analyze_Subprogram_Instantiation (N, E_Function);
3532 end Analyze_Function_Instantiation;
3533
3534 ---------------------------------
3535 -- Analyze_Generic_Access_Type --
3536 ---------------------------------
3537
3538 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id) is
3539 begin
3540 Enter_Name (T);
3541
3542 if Nkind (Def) = N_Access_To_Object_Definition then
3543 Access_Type_Declaration (T, Def);
3544
3545 if Is_Incomplete_Or_Private_Type (Designated_Type (T))
3546 and then No (Full_View (Designated_Type (T)))
3547 and then not Is_Generic_Type (Designated_Type (T))
3548 then
3549 Error_Msg_N ("premature usage of incomplete type", Def);
3550
3551 elsif not Is_Entity_Name (Subtype_Indication (Def)) then
3552 Error_Msg_N
3553 ("only a subtype mark is allowed in a formal", Def);
3554 end if;
3555
3556 else
3557 Access_Subprogram_Declaration (T, Def);
3558 end if;
3559 end Analyze_Generic_Access_Type;
3560
3561 ---------------------------------
3562 -- Analyze_Generic_Formal_Part --
3563 ---------------------------------
3564
3565 procedure Analyze_Generic_Formal_Part (N : Node_Id) is
3566 Gen_Parm_Decl : Node_Id;
3567
3568 begin
3569 -- The generic formals are processed in the scope of the generic unit,
3570 -- where they are immediately visible. The scope is installed by the
3571 -- caller.
3572
3573 Gen_Parm_Decl := First (Generic_Formal_Declarations (N));
3574 while Present (Gen_Parm_Decl) loop
3575 Analyze (Gen_Parm_Decl);
3576 Next (Gen_Parm_Decl);
3577 end loop;
3578
3579 Generate_Reference_To_Generic_Formals (Current_Scope);
3580
3581 -- For Ada 2020, some formal parameters can carry aspects, which must
3582 -- be name-resolved at the end of the list of formal parameters (which
3583 -- has the semantics of a declaration list).
3584
3585 Analyze_Contracts (Generic_Formal_Declarations (N));
3586 end Analyze_Generic_Formal_Part;
3587
3588 ------------------------------------------
3589 -- Analyze_Generic_Package_Declaration --
3590 ------------------------------------------
3591
3592 procedure Analyze_Generic_Package_Declaration (N : Node_Id) is
3593 Decls : constant List_Id := Visible_Declarations (Specification (N));
3594 Loc : constant Source_Ptr := Sloc (N);
3595
3596 Decl : Node_Id;
3597 Id : Entity_Id;
3598 New_N : Node_Id;
3599 Renaming : Node_Id;
3600 Save_Parent : Node_Id;
3601
3602 begin
3603 -- A generic may grant access to its private enclosing context depending
3604 -- on the placement of its corresponding body. From elaboration point of
3605 -- view, the flow of execution may enter this private context, and then
3606 -- reach an external unit, thus producing a dependency on that external
3607 -- unit. For such a path to be properly discovered and encoded in the
3608 -- ALI file of the main unit, let the ABE mechanism process the body of
3609 -- the main unit, and encode all relevant invocation constructs and the
3610 -- relations between them.
3611
3612 Mark_Save_Invocation_Graph_Of_Body;
3613
3614 -- We introduce a renaming of the enclosing package, to have a usable
3615 -- entity as the prefix of an expanded name for a local entity of the
3616 -- form Par.P.Q, where P is the generic package. This is because a local
3617 -- entity named P may hide it, so that the usual visibility rules in
3618 -- the instance will not resolve properly.
3619
3620 Renaming :=
3621 Make_Package_Renaming_Declaration (Loc,
3622 Defining_Unit_Name =>
3623 Make_Defining_Identifier (Loc,
3624 Chars => New_External_Name (Chars (Defining_Entity (N)), "GH")),
3625 Name =>
3626 Make_Identifier (Loc, Chars (Defining_Entity (N))));
3627
3628 -- The declaration is inserted before other declarations, but before
3629 -- pragmas that may be library-unit pragmas and must appear before other
3630 -- declarations. The pragma Compile_Time_Error is not in this class, and
3631 -- may contain an expression that includes such a qualified name, so the
3632 -- renaming declaration must appear before it.
3633
3634 -- Are there other pragmas that require this special handling ???
3635
3636 if Present (Decls) then
3637 Decl := First (Decls);
3638 while Present (Decl)
3639 and then Nkind (Decl) = N_Pragma
3640 and then Get_Pragma_Id (Decl) /= Pragma_Compile_Time_Error
3641 loop
3642 Next (Decl);
3643 end loop;
3644
3645 if Present (Decl) then
3646 Insert_Before (Decl, Renaming);
3647 else
3648 Append (Renaming, Visible_Declarations (Specification (N)));
3649 end if;
3650
3651 else
3652 Set_Visible_Declarations (Specification (N), New_List (Renaming));
3653 end if;
3654
3655 -- Create copy of generic unit, and save for instantiation. If the unit
3656 -- is a child unit, do not copy the specifications for the parent, which
3657 -- are not part of the generic tree.
3658
3659 Save_Parent := Parent_Spec (N);
3660 Set_Parent_Spec (N, Empty);
3661
3662 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
3663 Set_Parent_Spec (New_N, Save_Parent);
3664 Rewrite (N, New_N);
3665
3666 -- Once the contents of the generic copy and the template are swapped,
3667 -- do the same for their respective aspect specifications.
3668
3669 Exchange_Aspects (N, New_N);
3670
3671 -- Collect all contract-related source pragmas found within the template
3672 -- and attach them to the contract of the package spec. This contract is
3673 -- used in the capture of global references within annotations.
3674
3675 Create_Generic_Contract (N);
3676
3677 Id := Defining_Entity (N);
3678 Generate_Definition (Id);
3679
3680 -- Expansion is not applied to generic units
3681
3682 Start_Generic;
3683
3684 Enter_Name (Id);
3685 Set_Ekind (Id, E_Generic_Package);
3686 Set_Etype (Id, Standard_Void_Type);
3687
3688 -- Set SPARK_Mode from context
3689
3690 Set_SPARK_Pragma (Id, SPARK_Mode_Pragma);
3691 Set_SPARK_Aux_Pragma (Id, SPARK_Mode_Pragma);
3692 Set_SPARK_Pragma_Inherited (Id);
3693 Set_SPARK_Aux_Pragma_Inherited (Id);
3694
3695 -- Preserve relevant elaboration-related attributes of the context which
3696 -- are no longer available or very expensive to recompute once analysis,
3697 -- resolution, and expansion are over.
3698
3699 Mark_Elaboration_Attributes
3700 (N_Id => Id,
3701 Checks => True,
3702 Warnings => True);
3703
3704 -- Analyze aspects now, so that generated pragmas appear in the
3705 -- declarations before building and analyzing the generic copy.
3706
3707 if Has_Aspects (N) then
3708 Analyze_Aspect_Specifications (N, Id);
3709 end if;
3710
3711 Push_Scope (Id);
3712 Enter_Generic_Scope (Id);
3713 Set_Inner_Instances (Id, New_Elmt_List);
3714
3715 Set_Categorization_From_Pragmas (N);
3716 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3717
3718 -- Link the declaration of the generic homonym in the generic copy to
3719 -- the package it renames, so that it is always resolved properly.
3720
3721 Set_Generic_Homonym (Id, Defining_Unit_Name (Renaming));
3722 Set_Entity (Associated_Node (Name (Renaming)), Id);
3723
3724 -- For a library unit, we have reconstructed the entity for the unit,
3725 -- and must reset it in the library tables.
3726
3727 if Nkind (Parent (N)) = N_Compilation_Unit then
3728 Set_Cunit_Entity (Current_Sem_Unit, Id);
3729 end if;
3730
3731 Analyze_Generic_Formal_Part (N);
3732
3733 -- After processing the generic formals, analysis proceeds as for a
3734 -- non-generic package.
3735
3736 Analyze (Specification (N));
3737
3738 Validate_Categorization_Dependency (N, Id);
3739
3740 End_Generic;
3741
3742 End_Package_Scope (Id);
3743 Exit_Generic_Scope (Id);
3744
3745 -- If the generic appears within a package unit, the body of that unit
3746 -- has to be present for instantiation and inlining.
3747
3748 if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Declaration then
3749 Set_Body_Needed_For_Inlining
3750 (Defining_Entity (Unit (Cunit (Current_Sem_Unit))));
3751 end if;
3752
3753 if Nkind (Parent (N)) /= N_Compilation_Unit then
3754 Move_Freeze_Nodes (Id, N, Visible_Declarations (Specification (N)));
3755 Move_Freeze_Nodes (Id, N, Private_Declarations (Specification (N)));
3756 Move_Freeze_Nodes (Id, N, Generic_Formal_Declarations (N));
3757
3758 else
3759 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
3760 Validate_RT_RAT_Component (N);
3761
3762 -- If this is a spec without a body, check that generic parameters
3763 -- are referenced.
3764
3765 if not Body_Required (Parent (N)) then
3766 Check_References (Id);
3767 end if;
3768 end if;
3769
3770 -- If there is a specified storage pool in the context, create an
3771 -- aspect on the package declaration, so that it is used in any
3772 -- instance that does not override it.
3773
3774 if Present (Default_Pool) then
3775 declare
3776 ASN : Node_Id;
3777
3778 begin
3779 ASN :=
3780 Make_Aspect_Specification (Loc,
3781 Identifier => Make_Identifier (Loc, Name_Default_Storage_Pool),
3782 Expression => New_Copy (Default_Pool));
3783
3784 if No (Aspect_Specifications (Specification (N))) then
3785 Set_Aspect_Specifications (Specification (N), New_List (ASN));
3786 else
3787 Append (ASN, Aspect_Specifications (Specification (N)));
3788 end if;
3789 end;
3790 end if;
3791 end Analyze_Generic_Package_Declaration;
3792
3793 --------------------------------------------
3794 -- Analyze_Generic_Subprogram_Declaration --
3795 --------------------------------------------
3796
3797 procedure Analyze_Generic_Subprogram_Declaration (N : Node_Id) is
3798 Formals : List_Id;
3799 Id : Entity_Id;
3800 New_N : Node_Id;
3801 Result_Type : Entity_Id;
3802 Save_Parent : Node_Id;
3803 Spec : Node_Id;
3804 Typ : Entity_Id;
3805
3806 begin
3807 -- A generic may grant access to its private enclosing context depending
3808 -- on the placement of its corresponding body. From elaboration point of
3809 -- view, the flow of execution may enter this private context, and then
3810 -- reach an external unit, thus producing a dependency on that external
3811 -- unit. For such a path to be properly discovered and encoded in the
3812 -- ALI file of the main unit, let the ABE mechanism process the body of
3813 -- the main unit, and encode all relevant invocation constructs and the
3814 -- relations between them.
3815
3816 Mark_Save_Invocation_Graph_Of_Body;
3817
3818 -- Create copy of generic unit, and save for instantiation. If the unit
3819 -- is a child unit, do not copy the specifications for the parent, which
3820 -- are not part of the generic tree.
3821
3822 Save_Parent := Parent_Spec (N);
3823 Set_Parent_Spec (N, Empty);
3824
3825 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
3826 Set_Parent_Spec (New_N, Save_Parent);
3827 Rewrite (N, New_N);
3828
3829 -- Once the contents of the generic copy and the template are swapped,
3830 -- do the same for their respective aspect specifications.
3831
3832 Exchange_Aspects (N, New_N);
3833
3834 -- Collect all contract-related source pragmas found within the template
3835 -- and attach them to the contract of the subprogram spec. This contract
3836 -- is used in the capture of global references within annotations.
3837
3838 Create_Generic_Contract (N);
3839
3840 Spec := Specification (N);
3841 Id := Defining_Entity (Spec);
3842 Generate_Definition (Id);
3843
3844 if Nkind (Id) = N_Defining_Operator_Symbol then
3845 Error_Msg_N
3846 ("operator symbol not allowed for generic subprogram", Id);
3847 end if;
3848
3849 Start_Generic;
3850
3851 Enter_Name (Id);
3852 Set_Scope_Depth_Value (Id, Scope_Depth (Current_Scope) + 1);
3853
3854 -- Analyze the aspects of the generic copy to ensure that all generated
3855 -- pragmas (if any) perform their semantic effects.
3856
3857 if Has_Aspects (N) then
3858 Analyze_Aspect_Specifications (N, Id);
3859 end if;
3860
3861 Push_Scope (Id);
3862 Enter_Generic_Scope (Id);
3863 Set_Inner_Instances (Id, New_Elmt_List);
3864 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3865
3866 Analyze_Generic_Formal_Part (N);
3867
3868 if Nkind (Spec) = N_Function_Specification then
3869 Set_Ekind (Id, E_Generic_Function);
3870 else
3871 Set_Ekind (Id, E_Generic_Procedure);
3872 end if;
3873
3874 -- Set SPARK_Mode from context
3875
3876 Set_SPARK_Pragma (Id, SPARK_Mode_Pragma);
3877 Set_SPARK_Pragma_Inherited (Id);
3878
3879 -- Preserve relevant elaboration-related attributes of the context which
3880 -- are no longer available or very expensive to recompute once analysis,
3881 -- resolution, and expansion are over.
3882
3883 Mark_Elaboration_Attributes
3884 (N_Id => Id,
3885 Checks => True,
3886 Warnings => True);
3887
3888 Formals := Parameter_Specifications (Spec);
3889
3890 if Present (Formals) then
3891 Process_Formals (Formals, Spec);
3892 end if;
3893
3894 if Nkind (Spec) = N_Function_Specification then
3895 if Nkind (Result_Definition (Spec)) = N_Access_Definition then
3896 Result_Type := Access_Definition (Spec, Result_Definition (Spec));
3897 Set_Etype (Id, Result_Type);
3898
3899 -- Check restriction imposed by AI05-073: a generic function
3900 -- cannot return an abstract type or an access to such.
3901
3902 -- This is a binding interpretation should it apply to earlier
3903 -- versions of Ada as well as Ada 2012???
3904
3905 if Is_Abstract_Type (Designated_Type (Result_Type))
3906 and then Ada_Version >= Ada_2012
3907 then
3908 Error_Msg_N
3909 ("generic function cannot have an access result "
3910 & "that designates an abstract type", Spec);
3911 end if;
3912
3913 else
3914 Find_Type (Result_Definition (Spec));
3915 Typ := Entity (Result_Definition (Spec));
3916
3917 if Is_Abstract_Type (Typ)
3918 and then Ada_Version >= Ada_2012
3919 then
3920 Error_Msg_N
3921 ("generic function cannot have abstract result type", Spec);
3922 end if;
3923
3924 -- If a null exclusion is imposed on the result type, then create
3925 -- a null-excluding itype (an access subtype) and use it as the
3926 -- function's Etype.
3927
3928 if Is_Access_Type (Typ)
3929 and then Null_Exclusion_Present (Spec)
3930 then
3931 Set_Etype (Id,
3932 Create_Null_Excluding_Itype
3933 (T => Typ,
3934 Related_Nod => Spec,
3935 Scope_Id => Defining_Unit_Name (Spec)));
3936 else
3937 Set_Etype (Id, Typ);
3938 end if;
3939 end if;
3940
3941 else
3942 Set_Etype (Id, Standard_Void_Type);
3943 end if;
3944
3945 -- For a library unit, we have reconstructed the entity for the unit,
3946 -- and must reset it in the library tables. We also make sure that
3947 -- Body_Required is set properly in the original compilation unit node.
3948
3949 if Nkind (Parent (N)) = N_Compilation_Unit then
3950 Set_Cunit_Entity (Current_Sem_Unit, Id);
3951 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
3952 end if;
3953
3954 -- If the generic appears within a package unit, the body of that unit
3955 -- has to be present for instantiation and inlining.
3956
3957 if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Declaration
3958 and then Unit_Requires_Body (Id)
3959 then
3960 Set_Body_Needed_For_Inlining
3961 (Defining_Entity (Unit (Cunit (Current_Sem_Unit))));
3962 end if;
3963
3964 Set_Categorization_From_Pragmas (N);
3965 Validate_Categorization_Dependency (N, Id);
3966
3967 -- Capture all global references that occur within the profile of the
3968 -- generic subprogram. Aspects are not part of this processing because
3969 -- they must be delayed. If processed now, Save_Global_References will
3970 -- destroy the Associated_Node links and prevent the capture of global
3971 -- references when the contract of the generic subprogram is analyzed.
3972
3973 Save_Global_References (Original_Node (N));
3974
3975 End_Generic;
3976 End_Scope;
3977 Exit_Generic_Scope (Id);
3978 Generate_Reference_To_Formals (Id);
3979
3980 List_Inherited_Pre_Post_Aspects (Id);
3981 end Analyze_Generic_Subprogram_Declaration;
3982
3983 -----------------------------------
3984 -- Analyze_Package_Instantiation --
3985 -----------------------------------
3986
3987 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
3988 -- must be replaced by gotos which jump to the end of the routine in order
3989 -- to restore the Ghost and SPARK modes.
3990
3991 procedure Analyze_Package_Instantiation (N : Node_Id) is
3992 Has_Inline_Always : Boolean := False;
3993 -- Set if the generic unit contains any subprograms with Inline_Always.
3994 -- Only relevant when back-end inlining is not enabled.
3995
3996 function Might_Inline_Subp (Gen_Unit : Entity_Id) return Boolean;
3997 -- Return True if inlining is active and Gen_Unit contains inlined
3998 -- subprograms. In this case, we may either instantiate the body when
3999 -- front-end inlining is enabled, or add a pending instantiation when
4000 -- back-end inlining is enabled. In the former case, this may cause
4001 -- superfluous instantiations, but in either case we need to perform
4002 -- the instantiation of the body in the context of the instance and
4003 -- not in that of the point of inlining.
4004
4005 function Needs_Body_Instantiated (Gen_Unit : Entity_Id) return Boolean;
4006 -- Return True if Gen_Unit needs to have its body instantiated in the
4007 -- context of N. This in particular excludes generic contexts.
4008
4009 -----------------------
4010 -- Might_Inline_Subp --
4011 -----------------------
4012
4013 function Might_Inline_Subp (Gen_Unit : Entity_Id) return Boolean is
4014 E : Entity_Id;
4015
4016 begin
4017 if Inline_Processing_Required then
4018 -- No need to recompute the answer if we know it is positive
4019 -- and back-end inlining is enabled.
4020
4021 if Is_Inlined (Gen_Unit) and then Back_End_Inlining then
4022 return True;
4023 end if;
4024
4025 E := First_Entity (Gen_Unit);
4026 while Present (E) loop
4027 if Is_Subprogram (E) and then Is_Inlined (E) then
4028 -- Remember if there are any subprograms with Inline_Always
4029
4030 if Has_Pragma_Inline_Always (E) then
4031 Has_Inline_Always := True;
4032 end if;
4033
4034 Set_Is_Inlined (Gen_Unit);
4035 return True;
4036 end if;
4037
4038 Next_Entity (E);
4039 end loop;
4040 end if;
4041
4042 return False;
4043 end Might_Inline_Subp;
4044
4045 -------------------------------
4046 -- Needs_Body_Instantiated --
4047 -------------------------------
4048
4049 function Needs_Body_Instantiated (Gen_Unit : Entity_Id) return Boolean is
4050 begin
4051 -- No need to instantiate bodies in generic units
4052
4053 if Is_Generic_Unit (Cunit_Entity (Main_Unit)) then
4054 return False;
4055 end if;
4056
4057 -- If the instantiation is in the main unit, then the body is needed
4058
4059 if Is_In_Main_Unit (N) then
4060 return True;
4061 end if;
4062
4063 -- If not, then again no need to instantiate bodies in generic units
4064
4065 if Is_Generic_Unit (Cunit_Entity (Get_Code_Unit (N))) then
4066 return False;
4067 end if;
4068
4069 -- Here we have a special handling for back-end inlining: if inline
4070 -- processing is required, then we unconditionally want to have the
4071 -- body instantiated. The reason is that Might_Inline_Subp does not
4072 -- catch all the cases (as it does not recurse into nested packages)
4073 -- so this avoids the need to patch things up afterwards. Moreover,
4074 -- these instantiations are only performed on demand when back-end
4075 -- inlining is enabled, so this causes very little extra work.
4076
4077 if Inline_Processing_Required and then Back_End_Inlining then
4078 return True;
4079 end if;
4080
4081 -- We want to have the bodies instantiated in non-main units if
4082 -- they might contribute inlined subprograms.
4083
4084 return Might_Inline_Subp (Gen_Unit);
4085 end Needs_Body_Instantiated;
4086
4087 -- Local declarations
4088
4089 Gen_Id : constant Node_Id := Name (N);
4090 Inst_Id : constant Entity_Id := Defining_Entity (N);
4091 Is_Actual_Pack : constant Boolean := Is_Internal (Inst_Id);
4092 Loc : constant Source_Ptr := Sloc (N);
4093
4094 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
4095 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
4096 Saved_ISMP : constant Boolean :=
4097 Ignore_SPARK_Mode_Pragmas_In_Instance;
4098 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
4099 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
4100 -- Save the Ghost and SPARK mode-related data to restore on exit
4101
4102 Saved_Style_Check : constant Boolean := Style_Check;
4103 -- Save style check mode for restore on exit
4104
4105 Act_Decl : Node_Id;
4106 Act_Decl_Name : Node_Id;
4107 Act_Decl_Id : Entity_Id;
4108 Act_Spec : Node_Id;
4109 Act_Tree : Node_Id;
4110 Env_Installed : Boolean := False;
4111 Gen_Decl : Node_Id;
4112 Gen_Spec : Node_Id;
4113 Gen_Unit : Entity_Id;
4114 Inline_Now : Boolean := False;
4115 Needs_Body : Boolean;
4116 Parent_Installed : Boolean := False;
4117 Renaming_List : List_Id;
4118 Unit_Renaming : Node_Id;
4119
4120 Vis_Prims_List : Elist_Id := No_Elist;
4121 -- List of primitives made temporarily visible in the instantiation
4122 -- to match the visibility of the formal type
4123
4124 -- Start of processing for Analyze_Package_Instantiation
4125
4126 begin
4127 -- Preserve relevant elaboration-related attributes of the context which
4128 -- are no longer available or very expensive to recompute once analysis,
4129 -- resolution, and expansion are over.
4130
4131 Mark_Elaboration_Attributes
4132 (N_Id => N,
4133 Checks => True,
4134 Level => True,
4135 Modes => True,
4136 Warnings => True);
4137
4138 -- Very first thing: check for Text_IO special unit in case we are
4139 -- instantiating one of the children of [[Wide_]Wide_]Text_IO.
4140
4141 Check_Text_IO_Special_Unit (Name (N));
4142
4143 -- Make node global for error reporting
4144
4145 Instantiation_Node := N;
4146
4147 -- Case of instantiation of a generic package
4148
4149 if Nkind (N) = N_Package_Instantiation then
4150 Act_Decl_Id := New_Copy (Defining_Entity (N));
4151 Set_Comes_From_Source (Act_Decl_Id, True);
4152
4153 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name then
4154 Act_Decl_Name :=
4155 Make_Defining_Program_Unit_Name (Loc,
4156 Name =>
4157 New_Copy_Tree (Name (Defining_Unit_Name (N))),
4158 Defining_Identifier => Act_Decl_Id);
4159 else
4160 Act_Decl_Name := Act_Decl_Id;
4161 end if;
4162
4163 -- Case of instantiation of a formal package
4164
4165 else
4166 Act_Decl_Id := Defining_Identifier (N);
4167 Act_Decl_Name := Act_Decl_Id;
4168 end if;
4169
4170 Generate_Definition (Act_Decl_Id);
4171 Set_Ekind (Act_Decl_Id, E_Package);
4172
4173 -- Initialize list of incomplete actuals before analysis
4174
4175 Set_Incomplete_Actuals (Act_Decl_Id, New_Elmt_List);
4176
4177 Preanalyze_Actuals (N, Act_Decl_Id);
4178
4179 -- Turn off style checking in instances. If the check is enabled on the
4180 -- generic unit, a warning in an instance would just be noise. If not
4181 -- enabled on the generic, then a warning in an instance is just wrong.
4182 -- This must be done after analyzing the actuals, which do come from
4183 -- source and are subject to style checking.
4184
4185 Style_Check := False;
4186
4187 Init_Env;
4188 Env_Installed := True;
4189
4190 -- Reset renaming map for formal types. The mapping is established
4191 -- when analyzing the generic associations, but some mappings are
4192 -- inherited from formal packages of parent units, and these are
4193 -- constructed when the parents are installed.
4194
4195 Generic_Renamings.Set_Last (0);
4196 Generic_Renamings_HTable.Reset;
4197
4198 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
4199 Gen_Unit := Entity (Gen_Id);
4200
4201 -- A package instantiation is Ghost when it is subject to pragma Ghost
4202 -- or the generic template is Ghost. Set the mode now to ensure that
4203 -- any nodes generated during analysis and expansion are marked as
4204 -- Ghost.
4205
4206 Mark_And_Set_Ghost_Instantiation (N, Gen_Unit);
4207
4208 -- Verify that it is the name of a generic package
4209
4210 -- A visibility glitch: if the instance is a child unit and the generic
4211 -- is the generic unit of a parent instance (i.e. both the parent and
4212 -- the child units are instances of the same package) the name now
4213 -- denotes the renaming within the parent, not the intended generic
4214 -- unit. See if there is a homonym that is the desired generic. The
4215 -- renaming declaration must be visible inside the instance of the
4216 -- child, but not when analyzing the name in the instantiation itself.
4217
4218 if Ekind (Gen_Unit) = E_Package
4219 and then Present (Renamed_Entity (Gen_Unit))
4220 and then In_Open_Scopes (Renamed_Entity (Gen_Unit))
4221 and then Is_Generic_Instance (Renamed_Entity (Gen_Unit))
4222 and then Present (Homonym (Gen_Unit))
4223 then
4224 Gen_Unit := Homonym (Gen_Unit);
4225 end if;
4226
4227 if Etype (Gen_Unit) = Any_Type then
4228 Restore_Env;
4229 goto Leave;
4230
4231 elsif Ekind (Gen_Unit) /= E_Generic_Package then
4232
4233 -- Ada 2005 (AI-50217): Cannot use instance in limited with_clause
4234
4235 if From_Limited_With (Gen_Unit) then
4236 Error_Msg_N
4237 ("cannot instantiate a limited withed package", Gen_Id);
4238 else
4239 Error_Msg_NE
4240 ("& is not the name of a generic package", Gen_Id, Gen_Unit);
4241 end if;
4242
4243 Restore_Env;
4244 goto Leave;
4245 end if;
4246
4247 if In_Extended_Main_Source_Unit (N) then
4248 Set_Is_Instantiated (Gen_Unit);
4249 Generate_Reference (Gen_Unit, N);
4250
4251 if Present (Renamed_Object (Gen_Unit)) then
4252 Set_Is_Instantiated (Renamed_Object (Gen_Unit));
4253 Generate_Reference (Renamed_Object (Gen_Unit), N);
4254 end if;
4255 end if;
4256
4257 if Nkind (Gen_Id) = N_Identifier
4258 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
4259 then
4260 Error_Msg_NE
4261 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
4262
4263 elsif Nkind (Gen_Id) = N_Expanded_Name
4264 and then Is_Child_Unit (Gen_Unit)
4265 and then Nkind (Prefix (Gen_Id)) = N_Identifier
4266 and then Chars (Act_Decl_Id) = Chars (Prefix (Gen_Id))
4267 then
4268 Error_Msg_N
4269 ("& is hidden within declaration of instance ", Prefix (Gen_Id));
4270 end if;
4271
4272 Set_Entity (Gen_Id, Gen_Unit);
4273
4274 -- If generic is a renaming, get original generic unit
4275
4276 if Present (Renamed_Object (Gen_Unit))
4277 and then Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Package
4278 then
4279 Gen_Unit := Renamed_Object (Gen_Unit);
4280 end if;
4281
4282 -- Verify that there are no circular instantiations
4283
4284 if In_Open_Scopes (Gen_Unit) then
4285 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
4286 Restore_Env;
4287 goto Leave;
4288
4289 elsif Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
4290 Error_Msg_Node_2 := Current_Scope;
4291 Error_Msg_NE
4292 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
4293 Circularity_Detected := True;
4294 Restore_Env;
4295 goto Leave;
4296
4297 else
4298 Set_Ekind (Inst_Id, E_Package);
4299 Set_Scope (Inst_Id, Current_Scope);
4300
4301 -- If the context of the instance is subject to SPARK_Mode "off" or
4302 -- the annotation is altogether missing, set the global flag which
4303 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
4304 -- the instance.
4305
4306 if SPARK_Mode /= On then
4307 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
4308
4309 -- Mark the instance spec in case the body is instantiated at a
4310 -- later pass. This preserves the original context in effect for
4311 -- the body.
4312
4313 Set_Ignore_SPARK_Mode_Pragmas (Act_Decl_Id);
4314 end if;
4315
4316 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
4317 Gen_Spec := Specification (Gen_Decl);
4318
4319 -- Initialize renamings map, for error checking, and the list that
4320 -- holds private entities whose views have changed between generic
4321 -- definition and instantiation. If this is the instance created to
4322 -- validate an actual package, the instantiation environment is that
4323 -- of the enclosing instance.
4324
4325 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
4326
4327 -- Copy original generic tree, to produce text for instantiation
4328
4329 Act_Tree :=
4330 Copy_Generic_Node
4331 (Original_Node (Gen_Decl), Empty, Instantiating => True);
4332
4333 Act_Spec := Specification (Act_Tree);
4334
4335 -- If this is the instance created to validate an actual package,
4336 -- only the formals matter, do not examine the package spec itself.
4337
4338 if Is_Actual_Pack then
4339 Set_Visible_Declarations (Act_Spec, New_List);
4340 Set_Private_Declarations (Act_Spec, New_List);
4341 end if;
4342
4343 Renaming_List :=
4344 Analyze_Associations
4345 (I_Node => N,
4346 Formals => Generic_Formal_Declarations (Act_Tree),
4347 F_Copy => Generic_Formal_Declarations (Gen_Decl));
4348
4349 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
4350
4351 Set_Instance_Env (Gen_Unit, Act_Decl_Id);
4352 Set_Defining_Unit_Name (Act_Spec, Act_Decl_Name);
4353 Set_Is_Generic_Instance (Act_Decl_Id);
4354 Set_Generic_Parent (Act_Spec, Gen_Unit);
4355
4356 -- References to the generic in its own declaration or its body are
4357 -- references to the instance. Add a renaming declaration for the
4358 -- generic unit itself. This declaration, as well as the renaming
4359 -- declarations for the generic formals, must remain private to the
4360 -- unit: the formals, because this is the language semantics, and
4361 -- the unit because its use is an artifact of the implementation.
4362
4363 Unit_Renaming :=
4364 Make_Package_Renaming_Declaration (Loc,
4365 Defining_Unit_Name =>
4366 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
4367 Name => New_Occurrence_Of (Act_Decl_Id, Loc));
4368
4369 Append (Unit_Renaming, Renaming_List);
4370
4371 -- The renaming declarations are the first local declarations of the
4372 -- new unit.
4373
4374 if Is_Non_Empty_List (Visible_Declarations (Act_Spec)) then
4375 Insert_List_Before
4376 (First (Visible_Declarations (Act_Spec)), Renaming_List);
4377 else
4378 Set_Visible_Declarations (Act_Spec, Renaming_List);
4379 end if;
4380
4381 Act_Decl := Make_Package_Declaration (Loc, Specification => Act_Spec);
4382
4383 -- Propagate the aspect specifications from the package declaration
4384 -- template to the instantiated version of the package declaration.
4385
4386 if Has_Aspects (Act_Tree) then
4387 Set_Aspect_Specifications (Act_Decl,
4388 New_Copy_List_Tree (Aspect_Specifications (Act_Tree)));
4389 end if;
4390
4391 -- The generic may have a generated Default_Storage_Pool aspect,
4392 -- set at the point of generic declaration. If the instance has
4393 -- that aspect, it overrides the one inherited from the generic.
4394
4395 if Has_Aspects (Gen_Spec) then
4396 if No (Aspect_Specifications (N)) then
4397 Set_Aspect_Specifications (N,
4398 (New_Copy_List_Tree
4399 (Aspect_Specifications (Gen_Spec))));
4400
4401 else
4402 declare
4403 Inherited_Aspects : constant List_Id :=
4404 New_Copy_List_Tree
4405 (Aspect_Specifications (Gen_Spec));
4406
4407 ASN1 : Node_Id;
4408 ASN2 : Node_Id;
4409 Pool_Present : Boolean := False;
4410
4411 begin
4412 ASN1 := First (Aspect_Specifications (N));
4413 while Present (ASN1) loop
4414 if Chars (Identifier (ASN1)) =
4415 Name_Default_Storage_Pool
4416 then
4417 Pool_Present := True;
4418 exit;
4419 end if;
4420
4421 Next (ASN1);
4422 end loop;
4423
4424 if Pool_Present then
4425
4426 -- If generic carries a default storage pool, remove it
4427 -- in favor of the instance one.
4428
4429 ASN2 := First (Inherited_Aspects);
4430 while Present (ASN2) loop
4431 if Chars (Identifier (ASN2)) =
4432 Name_Default_Storage_Pool
4433 then
4434 Remove (ASN2);
4435 exit;
4436 end if;
4437
4438 Next (ASN2);
4439 end loop;
4440 end if;
4441
4442 Prepend_List_To
4443 (Aspect_Specifications (N), Inherited_Aspects);
4444 end;
4445 end if;
4446 end if;
4447
4448 -- Save the instantiation node for a subsequent instantiation of the
4449 -- body if there is one and it needs to be instantiated here.
4450
4451 -- We instantiate the body only if we are generating code, or if we
4452 -- are generating cross-reference information, or for GNATprove use.
4453
4454 declare
4455 Enclosing_Body_Present : Boolean := False;
4456 -- If the generic unit is not a compilation unit, then a body may
4457 -- be present in its parent even if none is required. We create a
4458 -- tentative pending instantiation for the body, which will be
4459 -- discarded if none is actually present.
4460
4461 Scop : Entity_Id;
4462
4463 begin
4464 if Scope (Gen_Unit) /= Standard_Standard
4465 and then not Is_Child_Unit (Gen_Unit)
4466 then
4467 Scop := Scope (Gen_Unit);
4468 while Present (Scop) and then Scop /= Standard_Standard loop
4469 if Unit_Requires_Body (Scop) then
4470 Enclosing_Body_Present := True;
4471 exit;
4472
4473 elsif In_Open_Scopes (Scop)
4474 and then In_Package_Body (Scop)
4475 then
4476 Enclosing_Body_Present := True;
4477 exit;
4478 end if;
4479
4480 exit when Is_Compilation_Unit (Scop);
4481 Scop := Scope (Scop);
4482 end loop;
4483 end if;
4484
4485 -- If front-end inlining is enabled or there are any subprograms
4486 -- marked with Inline_Always, and this is a unit for which code
4487 -- will be generated, we instantiate the body at once.
4488
4489 -- This is done if the instance is not the main unit, and if the
4490 -- generic is not a child unit of another generic, to avoid scope
4491 -- problems and the reinstallation of parent instances.
4492
4493 if Expander_Active
4494 and then (not Is_Child_Unit (Gen_Unit)
4495 or else not Is_Generic_Unit (Scope (Gen_Unit)))
4496 and then Might_Inline_Subp (Gen_Unit)
4497 and then not Is_Actual_Pack
4498 then
4499 if not Back_End_Inlining
4500 and then (Front_End_Inlining or else Has_Inline_Always)
4501 and then (Is_In_Main_Unit (N)
4502 or else In_Main_Context (Current_Scope))
4503 and then Nkind (Parent (N)) /= N_Compilation_Unit
4504 then
4505 Inline_Now := True;
4506
4507 -- In configurable_run_time mode we force the inlining of
4508 -- predefined subprograms marked Inline_Always, to minimize
4509 -- the use of the run-time library.
4510
4511 elsif In_Predefined_Unit (Gen_Decl)
4512 and then Configurable_Run_Time_Mode
4513 and then Nkind (Parent (N)) /= N_Compilation_Unit
4514 then
4515 Inline_Now := True;
4516 end if;
4517
4518 -- If the current scope is itself an instance within a child
4519 -- unit, there will be duplications in the scope stack, and the
4520 -- unstacking mechanism in Inline_Instance_Body will fail.
4521 -- This loses some rare cases of optimization, and might be
4522 -- improved some day, if we can find a proper abstraction for
4523 -- "the complete compilation context" that can be saved and
4524 -- restored. ???
4525
4526 if Is_Generic_Instance (Current_Scope) then
4527 declare
4528 Curr_Unit : constant Entity_Id :=
4529 Cunit_Entity (Current_Sem_Unit);
4530 begin
4531 if Curr_Unit /= Current_Scope
4532 and then Is_Child_Unit (Curr_Unit)
4533 then
4534 Inline_Now := False;
4535 end if;
4536 end;
4537 end if;
4538 end if;
4539
4540 Needs_Body :=
4541 (Unit_Requires_Body (Gen_Unit)
4542 or else Enclosing_Body_Present
4543 or else Present (Corresponding_Body (Gen_Decl)))
4544 and then Needs_Body_Instantiated (Gen_Unit)
4545 and then not Is_Actual_Pack
4546 and then not Inline_Now
4547 and then (Operating_Mode = Generate_Code
4548 or else (Operating_Mode = Check_Semantics
4549 and then GNATprove_Mode));
4550
4551 -- If front-end inlining is enabled or there are any subprograms
4552 -- marked with Inline_Always, do not instantiate body when within
4553 -- a generic context.
4554
4555 if not Back_End_Inlining
4556 and then (Front_End_Inlining or else Has_Inline_Always)
4557 and then not Expander_Active
4558 then
4559 Needs_Body := False;
4560 end if;
4561
4562 -- If the current context is generic, and the package being
4563 -- instantiated is declared within a formal package, there is no
4564 -- body to instantiate until the enclosing generic is instantiated
4565 -- and there is an actual for the formal package. If the formal
4566 -- package has parameters, we build a regular package instance for
4567 -- it, that precedes the original formal package declaration.
4568
4569 if In_Open_Scopes (Scope (Scope (Gen_Unit))) then
4570 declare
4571 Decl : constant Node_Id :=
4572 Original_Node
4573 (Unit_Declaration_Node (Scope (Gen_Unit)));
4574 begin
4575 if Nkind (Decl) = N_Formal_Package_Declaration
4576 or else (Nkind (Decl) = N_Package_Declaration
4577 and then Is_List_Member (Decl)
4578 and then Present (Next (Decl))
4579 and then
4580 Nkind (Next (Decl)) =
4581 N_Formal_Package_Declaration)
4582 then
4583 Needs_Body := False;
4584 end if;
4585 end;
4586 end if;
4587 end;
4588
4589 -- For RCI unit calling stubs, we omit the instance body if the
4590 -- instance is the RCI library unit itself.
4591
4592 -- However there is a special case for nested instances: in this case
4593 -- we do generate the instance body, as it might be required, e.g.
4594 -- because it provides stream attributes for some type used in the
4595 -- profile of a remote subprogram. This is consistent with 12.3(12),
4596 -- which indicates that the instance body occurs at the place of the
4597 -- instantiation, and thus is part of the RCI declaration, which is
4598 -- present on all client partitions (this is E.2.3(18)).
4599
4600 -- Note that AI12-0002 may make it illegal at some point to have
4601 -- stream attributes defined in an RCI unit, in which case this
4602 -- special case will become unnecessary. In the meantime, there
4603 -- is known application code in production that depends on this
4604 -- being possible, so we definitely cannot eliminate the body in
4605 -- the case of nested instances for the time being.
4606
4607 -- When we generate a nested instance body, calling stubs for any
4608 -- relevant subprogram will be inserted immediately after the
4609 -- subprogram declarations, and will take precedence over the
4610 -- subsequent (original) body. (The stub and original body will be
4611 -- complete homographs, but this is permitted in an instance).
4612 -- (Could we do better and remove the original body???)
4613
4614 if Distribution_Stub_Mode = Generate_Caller_Stub_Body
4615 and then Comes_From_Source (N)
4616 and then Nkind (Parent (N)) = N_Compilation_Unit
4617 then
4618 Needs_Body := False;
4619 end if;
4620
4621 if Needs_Body then
4622 -- Indicate that the enclosing scopes contain an instantiation,
4623 -- and that cleanup actions should be delayed until after the
4624 -- instance body is expanded.
4625
4626 Check_Forward_Instantiation (Gen_Decl);
4627 if Nkind (N) = N_Package_Instantiation then
4628 declare
4629 Enclosing_Master : Entity_Id;
4630
4631 begin
4632 -- Loop to search enclosing masters
4633
4634 Enclosing_Master := Current_Scope;
4635 Scope_Loop : while Enclosing_Master /= Standard_Standard loop
4636 if Ekind (Enclosing_Master) = E_Package then
4637 if Is_Compilation_Unit (Enclosing_Master) then
4638 if In_Package_Body (Enclosing_Master) then
4639 Set_Delay_Subprogram_Descriptors
4640 (Body_Entity (Enclosing_Master));
4641 else
4642 Set_Delay_Subprogram_Descriptors
4643 (Enclosing_Master);
4644 end if;
4645
4646 exit Scope_Loop;
4647
4648 else
4649 Enclosing_Master := Scope (Enclosing_Master);
4650 end if;
4651
4652 elsif Is_Generic_Unit (Enclosing_Master)
4653 or else Ekind (Enclosing_Master) = E_Void
4654 then
4655 -- Cleanup actions will eventually be performed on the
4656 -- enclosing subprogram or package instance, if any.
4657 -- Enclosing scope is void in the formal part of a
4658 -- generic subprogram.
4659
4660 exit Scope_Loop;
4661
4662 else
4663 if Ekind (Enclosing_Master) = E_Entry
4664 and then
4665 Ekind (Scope (Enclosing_Master)) = E_Protected_Type
4666 then
4667 if not Expander_Active then
4668 exit Scope_Loop;
4669 else
4670 Enclosing_Master :=
4671 Protected_Body_Subprogram (Enclosing_Master);
4672 end if;
4673 end if;
4674
4675 Set_Delay_Cleanups (Enclosing_Master);
4676
4677 while Ekind (Enclosing_Master) = E_Block loop
4678 Enclosing_Master := Scope (Enclosing_Master);
4679 end loop;
4680
4681 if Is_Subprogram (Enclosing_Master) then
4682 Set_Delay_Subprogram_Descriptors (Enclosing_Master);
4683
4684 elsif Is_Task_Type (Enclosing_Master) then
4685 declare
4686 TBP : constant Node_Id :=
4687 Get_Task_Body_Procedure
4688 (Enclosing_Master);
4689 begin
4690 if Present (TBP) then
4691 Set_Delay_Subprogram_Descriptors (TBP);
4692 Set_Delay_Cleanups (TBP);
4693 end if;
4694 end;
4695 end if;
4696
4697 exit Scope_Loop;
4698 end if;
4699 end loop Scope_Loop;
4700 end;
4701
4702 -- Make entry in table
4703
4704 Add_Pending_Instantiation (N, Act_Decl);
4705 end if;
4706 end if;
4707
4708 Set_Categorization_From_Pragmas (Act_Decl);
4709
4710 if Parent_Installed then
4711 Hide_Current_Scope;
4712 end if;
4713
4714 Set_Instance_Spec (N, Act_Decl);
4715
4716 -- If not a compilation unit, insert the package declaration before
4717 -- the original instantiation node.
4718
4719 if Nkind (Parent (N)) /= N_Compilation_Unit then
4720 Mark_Rewrite_Insertion (Act_Decl);
4721 Insert_Before (N, Act_Decl);
4722
4723 if Has_Aspects (N) then
4724 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4725
4726 -- The pragma created for a Default_Storage_Pool aspect must
4727 -- appear ahead of the declarations in the instance spec.
4728 -- Analysis has placed it after the instance node, so remove
4729 -- it and reinsert it properly now.
4730
4731 declare
4732 ASN : constant Node_Id := First (Aspect_Specifications (N));
4733 A_Name : constant Name_Id := Chars (Identifier (ASN));
4734 Decl : Node_Id;
4735
4736 begin
4737 if A_Name = Name_Default_Storage_Pool then
4738 if No (Visible_Declarations (Act_Spec)) then
4739 Set_Visible_Declarations (Act_Spec, New_List);
4740 end if;
4741
4742 Decl := Next (N);
4743 while Present (Decl) loop
4744 if Nkind (Decl) = N_Pragma then
4745 Remove (Decl);
4746 Prepend (Decl, Visible_Declarations (Act_Spec));
4747 exit;
4748 end if;
4749
4750 Next (Decl);
4751 end loop;
4752 end if;
4753 end;
4754 end if;
4755
4756 Analyze (Act_Decl);
4757
4758 -- For an instantiation that is a compilation unit, place
4759 -- declaration on current node so context is complete for analysis
4760 -- (including nested instantiations). If this is the main unit,
4761 -- the declaration eventually replaces the instantiation node.
4762 -- If the instance body is created later, it replaces the
4763 -- instance node, and the declaration is attached to it
4764 -- (see Build_Instance_Compilation_Unit_Nodes).
4765
4766 else
4767 if Cunit_Entity (Current_Sem_Unit) = Defining_Entity (N) then
4768
4769 -- The entity for the current unit is the newly created one,
4770 -- and all semantic information is attached to it.
4771
4772 Set_Cunit_Entity (Current_Sem_Unit, Act_Decl_Id);
4773
4774 -- If this is the main unit, replace the main entity as well
4775
4776 if Current_Sem_Unit = Main_Unit then
4777 Main_Unit_Entity := Act_Decl_Id;
4778 end if;
4779 end if;
4780
4781 Set_Unit (Parent (N), Act_Decl);
4782 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
4783 Set_Package_Instantiation (Act_Decl_Id, N);
4784
4785 -- Process aspect specifications of the instance node, if any, to
4786 -- take into account categorization pragmas before analyzing the
4787 -- instance.
4788
4789 if Has_Aspects (N) then
4790 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4791 end if;
4792
4793 Analyze (Act_Decl);
4794 Set_Unit (Parent (N), N);
4795 Set_Body_Required (Parent (N), False);
4796
4797 -- We never need elaboration checks on instantiations, since by
4798 -- definition, the body instantiation is elaborated at the same
4799 -- time as the spec instantiation.
4800
4801 if Legacy_Elaboration_Checks then
4802 Set_Kill_Elaboration_Checks (Act_Decl_Id);
4803 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
4804 end if;
4805 end if;
4806
4807 if Legacy_Elaboration_Checks then
4808 Check_Elab_Instantiation (N);
4809 end if;
4810
4811 -- Save the scenario for later examination by the ABE Processing
4812 -- phase.
4813
4814 Record_Elaboration_Scenario (N);
4815
4816 -- The instantiation results in a guaranteed ABE
4817
4818 if Is_Known_Guaranteed_ABE (N) and then Needs_Body then
4819 -- Do not instantiate the corresponding body because gigi cannot
4820 -- handle certain types of premature instantiations.
4821
4822 Remove_Dead_Instance (N);
4823
4824 -- Create completing bodies for all subprogram declarations since
4825 -- their real bodies will not be instantiated.
4826
4827 Provide_Completing_Bodies (Instance_Spec (N));
4828 end if;
4829
4830 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
4831
4832 Set_First_Private_Entity (Defining_Unit_Name (Unit_Renaming),
4833 First_Private_Entity (Act_Decl_Id));
4834
4835 -- If the instantiation will receive a body, the unit will be
4836 -- transformed into a package body, and receive its own elaboration
4837 -- entity. Otherwise, the nature of the unit is now a package
4838 -- declaration.
4839
4840 if Nkind (Parent (N)) = N_Compilation_Unit
4841 and then not Needs_Body
4842 then
4843 Rewrite (N, Act_Decl);
4844 end if;
4845
4846 if Present (Corresponding_Body (Gen_Decl))
4847 or else Unit_Requires_Body (Gen_Unit)
4848 then
4849 Set_Has_Completion (Act_Decl_Id);
4850 end if;
4851
4852 Check_Formal_Packages (Act_Decl_Id);
4853
4854 Restore_Hidden_Primitives (Vis_Prims_List);
4855 Restore_Private_Views (Act_Decl_Id);
4856
4857 Inherit_Context (Gen_Decl, N);
4858
4859 if Parent_Installed then
4860 Remove_Parent;
4861 end if;
4862
4863 Restore_Env;
4864 Env_Installed := False;
4865 end if;
4866
4867 Validate_Categorization_Dependency (N, Act_Decl_Id);
4868
4869 -- There used to be a check here to prevent instantiations in local
4870 -- contexts if the No_Local_Allocators restriction was active. This
4871 -- check was removed by a binding interpretation in AI-95-00130/07,
4872 -- but we retain the code for documentation purposes.
4873
4874 -- if Ekind (Act_Decl_Id) /= E_Void
4875 -- and then not Is_Library_Level_Entity (Act_Decl_Id)
4876 -- then
4877 -- Check_Restriction (No_Local_Allocators, N);
4878 -- end if;
4879
4880 if Inline_Now then
4881 Inline_Instance_Body (N, Gen_Unit, Act_Decl);
4882 end if;
4883
4884 -- Check that if N is an instantiation of System.Dim_Float_IO or
4885 -- System.Dim_Integer_IO, the formal type has a dimension system.
4886
4887 if Nkind (N) = N_Package_Instantiation
4888 and then Is_Dim_IO_Package_Instantiation (N)
4889 then
4890 declare
4891 Assoc : constant Node_Id := First (Generic_Associations (N));
4892 begin
4893 if not Has_Dimension_System
4894 (Etype (Explicit_Generic_Actual_Parameter (Assoc)))
4895 then
4896 Error_Msg_N ("type with a dimension system expected", Assoc);
4897 end if;
4898 end;
4899 end if;
4900
4901 <<Leave>>
4902 if Has_Aspects (N) and then Nkind (Parent (N)) /= N_Compilation_Unit then
4903 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4904 end if;
4905
4906 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
4907 Restore_Ghost_Region (Saved_GM, Saved_IGR);
4908 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
4909 Style_Check := Saved_Style_Check;
4910
4911 exception
4912 when Instantiation_Error =>
4913 if Parent_Installed then
4914 Remove_Parent;
4915 end if;
4916
4917 if Env_Installed then
4918 Restore_Env;
4919 end if;
4920
4921 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
4922 Restore_Ghost_Region (Saved_GM, Saved_IGR);
4923 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
4924 Style_Check := Saved_Style_Check;
4925 end Analyze_Package_Instantiation;
4926
4927 --------------------------
4928 -- Inline_Instance_Body --
4929 --------------------------
4930
4931 -- WARNING: This routine manages SPARK regions. Return statements must be
4932 -- replaced by gotos which jump to the end of the routine and restore the
4933 -- SPARK mode.
4934
4935 procedure Inline_Instance_Body
4936 (N : Node_Id;
4937 Gen_Unit : Entity_Id;
4938 Act_Decl : Node_Id)
4939 is
4940 Config_Attrs : constant Config_Switches_Type := Save_Config_Switches;
4941
4942 Curr_Comp : constant Node_Id := Cunit (Current_Sem_Unit);
4943 Curr_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
4944 Gen_Comp : constant Entity_Id :=
4945 Cunit_Entity (Get_Source_Unit (Gen_Unit));
4946
4947 Scope_Stack_Depth : constant Pos :=
4948 Scope_Stack.Last - Scope_Stack.First + 1;
4949
4950 Inner_Scopes : array (1 .. Scope_Stack_Depth) of Entity_Id;
4951 Instances : array (1 .. Scope_Stack_Depth) of Entity_Id;
4952 Use_Clauses : array (1 .. Scope_Stack_Depth) of Node_Id;
4953
4954 Curr_Scope : Entity_Id := Empty;
4955 List : Elist_Id := No_Elist; -- init to avoid warning
4956 N_Instances : Nat := 0;
4957 Num_Inner : Nat := 0;
4958 Num_Scopes : Nat := 0;
4959 Removed : Boolean := False;
4960 S : Entity_Id;
4961 Vis : Boolean;
4962
4963 begin
4964 -- Case of generic unit defined in another unit. We must remove the
4965 -- complete context of the current unit to install that of the generic.
4966
4967 if Gen_Comp /= Cunit_Entity (Current_Sem_Unit) then
4968
4969 -- Add some comments for the following two loops ???
4970
4971 S := Current_Scope;
4972 while Present (S) and then S /= Standard_Standard loop
4973 loop
4974 Num_Scopes := Num_Scopes + 1;
4975
4976 Use_Clauses (Num_Scopes) :=
4977 (Scope_Stack.Table
4978 (Scope_Stack.Last - Num_Scopes + 1).
4979 First_Use_Clause);
4980 End_Use_Clauses (Use_Clauses (Num_Scopes));
4981
4982 exit when Scope_Stack.Last - Num_Scopes + 1 = Scope_Stack.First
4983 or else Scope_Stack.Table
4984 (Scope_Stack.Last - Num_Scopes).Entity = Scope (S);
4985 end loop;
4986
4987 exit when Is_Generic_Instance (S)
4988 and then (In_Package_Body (S)
4989 or else Ekind (S) = E_Procedure
4990 or else Ekind (S) = E_Function);
4991 S := Scope (S);
4992 end loop;
4993
4994 Vis := Is_Immediately_Visible (Gen_Comp);
4995
4996 -- Find and save all enclosing instances
4997
4998 S := Current_Scope;
4999
5000 while Present (S)
5001 and then S /= Standard_Standard
5002 loop
5003 if Is_Generic_Instance (S) then
5004 N_Instances := N_Instances + 1;
5005 Instances (N_Instances) := S;
5006
5007 exit when In_Package_Body (S);
5008 end if;
5009
5010 S := Scope (S);
5011 end loop;
5012
5013 -- Remove context of current compilation unit, unless we are within a
5014 -- nested package instantiation, in which case the context has been
5015 -- removed previously.
5016
5017 -- If current scope is the body of a child unit, remove context of
5018 -- spec as well. If an enclosing scope is an instance body, the
5019 -- context has already been removed, but the entities in the body
5020 -- must be made invisible as well.
5021
5022 S := Current_Scope;
5023 while Present (S) and then S /= Standard_Standard loop
5024 if Is_Generic_Instance (S)
5025 and then (In_Package_Body (S)
5026 or else Ekind_In (S, E_Procedure, E_Function))
5027 then
5028 -- We still have to remove the entities of the enclosing
5029 -- instance from direct visibility.
5030
5031 declare
5032 E : Entity_Id;
5033 begin
5034 E := First_Entity (S);
5035 while Present (E) loop
5036 Set_Is_Immediately_Visible (E, False);
5037 Next_Entity (E);
5038 end loop;
5039 end;
5040
5041 exit;
5042 end if;
5043
5044 if S = Curr_Unit
5045 or else (Ekind (Curr_Unit) = E_Package_Body
5046 and then S = Spec_Entity (Curr_Unit))
5047 or else (Ekind (Curr_Unit) = E_Subprogram_Body
5048 and then S = Corresponding_Spec
5049 (Unit_Declaration_Node (Curr_Unit)))
5050 then
5051 Removed := True;
5052
5053 -- Remove entities in current scopes from visibility, so that
5054 -- instance body is compiled in a clean environment.
5055
5056 List := Save_Scope_Stack (Handle_Use => False);
5057
5058 if Is_Child_Unit (S) then
5059
5060 -- Remove child unit from stack, as well as inner scopes.
5061 -- Removing the context of a child unit removes parent units
5062 -- as well.
5063
5064 while Current_Scope /= S loop
5065 Num_Inner := Num_Inner + 1;
5066 Inner_Scopes (Num_Inner) := Current_Scope;
5067 Pop_Scope;
5068 end loop;
5069
5070 Pop_Scope;
5071 Remove_Context (Curr_Comp);
5072 Curr_Scope := S;
5073
5074 else
5075 Remove_Context (Curr_Comp);
5076 end if;
5077
5078 if Ekind (Curr_Unit) = E_Package_Body then
5079 Remove_Context (Library_Unit (Curr_Comp));
5080 end if;
5081 end if;
5082
5083 S := Scope (S);
5084 end loop;
5085
5086 pragma Assert (Num_Inner < Num_Scopes);
5087
5088 Push_Scope (Standard_Standard);
5089 Scope_Stack.Table (Scope_Stack.Last).Is_Active_Stack_Base := True;
5090
5091 -- The inlined package body is analyzed with the configuration state
5092 -- of the context prior to the scope manipulations performed above.
5093
5094 -- ??? shouldn't this also use the warning state of the context prior
5095 -- to the scope manipulations?
5096
5097 Instantiate_Package_Body
5098 (Body_Info =>
5099 ((Act_Decl => Act_Decl,
5100 Config_Switches => Config_Attrs,
5101 Current_Sem_Unit => Current_Sem_Unit,
5102 Expander_Status => Expander_Active,
5103 Inst_Node => N,
5104 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
5105 Scope_Suppress => Scope_Suppress,
5106 Warnings => Save_Warnings)),
5107 Inlined_Body => True);
5108
5109 Pop_Scope;
5110
5111 -- Restore context
5112
5113 Set_Is_Immediately_Visible (Gen_Comp, Vis);
5114
5115 -- Reset Generic_Instance flag so that use clauses can be installed
5116 -- in the proper order. (See Use_One_Package for effect of enclosing
5117 -- instances on processing of use clauses).
5118
5119 for J in 1 .. N_Instances loop
5120 Set_Is_Generic_Instance (Instances (J), False);
5121 end loop;
5122
5123 if Removed then
5124 Install_Context (Curr_Comp, Chain => False);
5125
5126 if Present (Curr_Scope)
5127 and then Is_Child_Unit (Curr_Scope)
5128 then
5129 Push_Scope (Curr_Scope);
5130 Set_Is_Immediately_Visible (Curr_Scope);
5131
5132 -- Finally, restore inner scopes as well
5133
5134 for J in reverse 1 .. Num_Inner loop
5135 Push_Scope (Inner_Scopes (J));
5136 end loop;
5137 end if;
5138
5139 Restore_Scope_Stack (List, Handle_Use => False);
5140
5141 if Present (Curr_Scope)
5142 and then
5143 (In_Private_Part (Curr_Scope)
5144 or else In_Package_Body (Curr_Scope))
5145 then
5146 -- Install private declaration of ancestor units, which are
5147 -- currently available. Restore_Scope_Stack and Install_Context
5148 -- only install the visible part of parents.
5149
5150 declare
5151 Par : Entity_Id;
5152 begin
5153 Par := Scope (Curr_Scope);
5154 while (Present (Par)) and then Par /= Standard_Standard loop
5155 Install_Private_Declarations (Par);
5156 Par := Scope (Par);
5157 end loop;
5158 end;
5159 end if;
5160 end if;
5161
5162 -- Restore use clauses. For a child unit, use clauses in the parents
5163 -- are restored when installing the context, so only those in inner
5164 -- scopes (and those local to the child unit itself) need to be
5165 -- installed explicitly.
5166
5167 if Is_Child_Unit (Curr_Unit) and then Removed then
5168 for J in reverse 1 .. Num_Inner + 1 loop
5169 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
5170 Use_Clauses (J);
5171 Install_Use_Clauses (Use_Clauses (J));
5172 end loop;
5173
5174 else
5175 for J in reverse 1 .. Num_Scopes loop
5176 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
5177 Use_Clauses (J);
5178 Install_Use_Clauses (Use_Clauses (J));
5179 end loop;
5180 end if;
5181
5182 -- Restore status of instances. If one of them is a body, make its
5183 -- local entities visible again.
5184
5185 declare
5186 E : Entity_Id;
5187 Inst : Entity_Id;
5188
5189 begin
5190 for J in 1 .. N_Instances loop
5191 Inst := Instances (J);
5192 Set_Is_Generic_Instance (Inst, True);
5193
5194 if In_Package_Body (Inst)
5195 or else Ekind_In (S, E_Procedure, E_Function)
5196 then
5197 E := First_Entity (Instances (J));
5198 while Present (E) loop
5199 Set_Is_Immediately_Visible (E);
5200 Next_Entity (E);
5201 end loop;
5202 end if;
5203 end loop;
5204 end;
5205
5206 -- If generic unit is in current unit, current context is correct. Note
5207 -- that the context is guaranteed to carry the correct SPARK_Mode as no
5208 -- enclosing scopes were removed.
5209
5210 else
5211 Instantiate_Package_Body
5212 (Body_Info =>
5213 ((Act_Decl => Act_Decl,
5214 Config_Switches => Save_Config_Switches,
5215 Current_Sem_Unit => Current_Sem_Unit,
5216 Expander_Status => Expander_Active,
5217 Inst_Node => N,
5218 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
5219 Scope_Suppress => Scope_Suppress,
5220 Warnings => Save_Warnings)),
5221 Inlined_Body => True);
5222 end if;
5223 end Inline_Instance_Body;
5224
5225 -------------------------------------
5226 -- Analyze_Procedure_Instantiation --
5227 -------------------------------------
5228
5229 procedure Analyze_Procedure_Instantiation (N : Node_Id) is
5230 begin
5231 Analyze_Subprogram_Instantiation (N, E_Procedure);
5232 end Analyze_Procedure_Instantiation;
5233
5234 -----------------------------------
5235 -- Need_Subprogram_Instance_Body --
5236 -----------------------------------
5237
5238 function Need_Subprogram_Instance_Body
5239 (N : Node_Id;
5240 Subp : Entity_Id) return Boolean
5241 is
5242 function Is_Inlined_Or_Child_Of_Inlined (E : Entity_Id) return Boolean;
5243 -- Return True if E is an inlined subprogram, an inlined renaming or a
5244 -- subprogram nested in an inlined subprogram. The inlining machinery
5245 -- totally disregards nested subprograms since it considers that they
5246 -- will always be compiled if the parent is (see Inline.Is_Nested).
5247
5248 ------------------------------------
5249 -- Is_Inlined_Or_Child_Of_Inlined --
5250 ------------------------------------
5251
5252 function Is_Inlined_Or_Child_Of_Inlined (E : Entity_Id) return Boolean is
5253 Scop : Entity_Id;
5254
5255 begin
5256 if Is_Inlined (E) or else Is_Inlined (Alias (E)) then
5257 return True;
5258 end if;
5259
5260 Scop := Scope (E);
5261 while Scop /= Standard_Standard loop
5262 if Ekind (Scop) in Subprogram_Kind and then Is_Inlined (Scop) then
5263 return True;
5264 end if;
5265
5266 Scop := Scope (Scop);
5267 end loop;
5268
5269 return False;
5270 end Is_Inlined_Or_Child_Of_Inlined;
5271
5272 begin
5273 -- Must be in the main unit or inlined (or child of inlined)
5274
5275 if (Is_In_Main_Unit (N) or else Is_Inlined_Or_Child_Of_Inlined (Subp))
5276
5277 -- Must be generating code or analyzing code in GNATprove mode
5278
5279 and then (Operating_Mode = Generate_Code
5280 or else (Operating_Mode = Check_Semantics
5281 and then GNATprove_Mode))
5282
5283 -- The body is needed when generating code (full expansion) and in
5284 -- in GNATprove mode (special expansion) for formal verification of
5285 -- the body itself.
5286
5287 and then (Expander_Active or GNATprove_Mode)
5288
5289 -- No point in inlining if ABE is inevitable
5290
5291 and then not Is_Known_Guaranteed_ABE (N)
5292
5293 -- Or if subprogram is eliminated
5294
5295 and then not Is_Eliminated (Subp)
5296 then
5297 Add_Pending_Instantiation (N, Unit_Declaration_Node (Subp));
5298 return True;
5299
5300 -- Here if not inlined, or we ignore the inlining
5301
5302 else
5303 return False;
5304 end if;
5305 end Need_Subprogram_Instance_Body;
5306
5307 --------------------------------------
5308 -- Analyze_Subprogram_Instantiation --
5309 --------------------------------------
5310
5311 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
5312 -- must be replaced by gotos which jump to the end of the routine in order
5313 -- to restore the Ghost and SPARK modes.
5314
5315 procedure Analyze_Subprogram_Instantiation
5316 (N : Node_Id;
5317 K : Entity_Kind)
5318 is
5319 Errs : constant Nat := Serious_Errors_Detected;
5320 Gen_Id : constant Node_Id := Name (N);
5321 Inst_Id : constant Entity_Id := Defining_Entity (N);
5322 Anon_Id : constant Entity_Id :=
5323 Make_Defining_Identifier (Sloc (Inst_Id),
5324 Chars => New_External_Name (Chars (Inst_Id), 'R'));
5325 Loc : constant Source_Ptr := Sloc (N);
5326
5327 Act_Decl_Id : Entity_Id := Empty; -- init to avoid warning
5328 Act_Decl : Node_Id;
5329 Act_Spec : Node_Id;
5330 Act_Tree : Node_Id;
5331
5332 Env_Installed : Boolean := False;
5333 Gen_Unit : Entity_Id;
5334 Gen_Decl : Node_Id;
5335 Pack_Id : Entity_Id;
5336 Parent_Installed : Boolean := False;
5337
5338 Renaming_List : List_Id;
5339 -- The list of declarations that link formals and actuals of the
5340 -- instance. These are subtype declarations for formal types, and
5341 -- renaming declarations for other formals. The subprogram declaration
5342 -- for the instance is then appended to the list, and the last item on
5343 -- the list is the renaming declaration for the instance.
5344
5345 procedure Analyze_Instance_And_Renamings;
5346 -- The instance must be analyzed in a context that includes the mappings
5347 -- of generic parameters into actuals. We create a package declaration
5348 -- for this purpose, and a subprogram with an internal name within the
5349 -- package. The subprogram instance is simply an alias for the internal
5350 -- subprogram, declared in the current scope.
5351
5352 procedure Build_Subprogram_Renaming;
5353 -- If the subprogram is recursive, there are occurrences of the name of
5354 -- the generic within the body, which must resolve to the current
5355 -- instance. We add a renaming declaration after the declaration, which
5356 -- is available in the instance body, as well as in the analysis of
5357 -- aspects that appear in the generic. This renaming declaration is
5358 -- inserted after the instance declaration which it renames.
5359
5360 ------------------------------------
5361 -- Analyze_Instance_And_Renamings --
5362 ------------------------------------
5363
5364 procedure Analyze_Instance_And_Renamings is
5365 Def_Ent : constant Entity_Id := Defining_Entity (N);
5366 Pack_Decl : Node_Id;
5367
5368 begin
5369 if Nkind (Parent (N)) = N_Compilation_Unit then
5370
5371 -- For the case of a compilation unit, the container package has
5372 -- the same name as the instantiation, to insure that the binder
5373 -- calls the elaboration procedure with the right name. Copy the
5374 -- entity of the instance, which may have compilation level flags
5375 -- (e.g. Is_Child_Unit) set.
5376
5377 Pack_Id := New_Copy (Def_Ent);
5378
5379 else
5380 -- Otherwise we use the name of the instantiation concatenated
5381 -- with its source position to ensure uniqueness if there are
5382 -- several instantiations with the same name.
5383
5384 Pack_Id :=
5385 Make_Defining_Identifier (Loc,
5386 Chars => New_External_Name
5387 (Related_Id => Chars (Def_Ent),
5388 Suffix => "GP",
5389 Suffix_Index => Source_Offset (Sloc (Def_Ent))));
5390 end if;
5391
5392 Pack_Decl :=
5393 Make_Package_Declaration (Loc,
5394 Specification => Make_Package_Specification (Loc,
5395 Defining_Unit_Name => Pack_Id,
5396 Visible_Declarations => Renaming_List,
5397 End_Label => Empty));
5398
5399 Set_Instance_Spec (N, Pack_Decl);
5400 Set_Is_Generic_Instance (Pack_Id);
5401 Set_Debug_Info_Needed (Pack_Id);
5402
5403 -- Case of not a compilation unit
5404
5405 if Nkind (Parent (N)) /= N_Compilation_Unit then
5406 Mark_Rewrite_Insertion (Pack_Decl);
5407 Insert_Before (N, Pack_Decl);
5408 Set_Has_Completion (Pack_Id);
5409
5410 -- Case of an instantiation that is a compilation unit
5411
5412 -- Place declaration on current node so context is complete for
5413 -- analysis (including nested instantiations), and for use in a
5414 -- context_clause (see Analyze_With_Clause).
5415
5416 else
5417 Set_Unit (Parent (N), Pack_Decl);
5418 Set_Parent_Spec (Pack_Decl, Parent_Spec (N));
5419 end if;
5420
5421 Analyze (Pack_Decl);
5422 Check_Formal_Packages (Pack_Id);
5423
5424 -- Body of the enclosing package is supplied when instantiating the
5425 -- subprogram body, after semantic analysis is completed.
5426
5427 if Nkind (Parent (N)) = N_Compilation_Unit then
5428
5429 -- Remove package itself from visibility, so it does not
5430 -- conflict with subprogram.
5431
5432 Set_Name_Entity_Id (Chars (Pack_Id), Homonym (Pack_Id));
5433
5434 -- Set name and scope of internal subprogram so that the proper
5435 -- external name will be generated. The proper scope is the scope
5436 -- of the wrapper package. We need to generate debugging info for
5437 -- the internal subprogram, so set flag accordingly.
5438
5439 Set_Chars (Anon_Id, Chars (Defining_Entity (N)));
5440 Set_Scope (Anon_Id, Scope (Pack_Id));
5441
5442 -- Mark wrapper package as referenced, to avoid spurious warnings
5443 -- if the instantiation appears in various with_ clauses of
5444 -- subunits of the main unit.
5445
5446 Set_Referenced (Pack_Id);
5447 end if;
5448
5449 Set_Is_Generic_Instance (Anon_Id);
5450 Set_Debug_Info_Needed (Anon_Id);
5451 Act_Decl_Id := New_Copy (Anon_Id);
5452
5453 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
5454 Set_Chars (Act_Decl_Id, Chars (Defining_Entity (N)));
5455 Set_Sloc (Act_Decl_Id, Sloc (Defining_Entity (N)));
5456
5457 -- Subprogram instance comes from source only if generic does
5458
5459 Set_Comes_From_Source (Act_Decl_Id, Comes_From_Source (Gen_Unit));
5460
5461 -- If the instance is a child unit, mark the Id accordingly. Mark
5462 -- the anonymous entity as well, which is the real subprogram and
5463 -- which is used when the instance appears in a context clause.
5464 -- Similarly, propagate the Is_Eliminated flag to handle properly
5465 -- nested eliminated subprograms.
5466
5467 Set_Is_Child_Unit (Act_Decl_Id, Is_Child_Unit (Defining_Entity (N)));
5468 Set_Is_Child_Unit (Anon_Id, Is_Child_Unit (Defining_Entity (N)));
5469 New_Overloaded_Entity (Act_Decl_Id);
5470 Check_Eliminated (Act_Decl_Id);
5471 Set_Is_Eliminated (Anon_Id, Is_Eliminated (Act_Decl_Id));
5472
5473 if Nkind (Parent (N)) = N_Compilation_Unit then
5474
5475 -- In compilation unit case, kill elaboration checks on the
5476 -- instantiation, since they are never needed - the body is
5477 -- instantiated at the same point as the spec.
5478
5479 if Legacy_Elaboration_Checks then
5480 Set_Kill_Elaboration_Checks (Act_Decl_Id);
5481 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
5482 end if;
5483
5484 Set_Is_Compilation_Unit (Anon_Id);
5485 Set_Cunit_Entity (Current_Sem_Unit, Pack_Id);
5486 end if;
5487
5488 -- The instance is not a freezing point for the new subprogram.
5489 -- The anonymous subprogram may have a freeze node, created for
5490 -- some delayed aspects. This freeze node must not be inherited
5491 -- by the visible subprogram entity.
5492
5493 Set_Is_Frozen (Act_Decl_Id, False);
5494 Set_Freeze_Node (Act_Decl_Id, Empty);
5495
5496 if Nkind (Defining_Entity (N)) = N_Defining_Operator_Symbol then
5497 Valid_Operator_Definition (Act_Decl_Id);
5498 end if;
5499
5500 Set_Alias (Act_Decl_Id, Anon_Id);
5501 Set_Has_Completion (Act_Decl_Id);
5502 Set_Related_Instance (Pack_Id, Act_Decl_Id);
5503
5504 if Nkind (Parent (N)) = N_Compilation_Unit then
5505 Set_Body_Required (Parent (N), False);
5506 end if;
5507 end Analyze_Instance_And_Renamings;
5508
5509 -------------------------------
5510 -- Build_Subprogram_Renaming --
5511 -------------------------------
5512
5513 procedure Build_Subprogram_Renaming is
5514 Renaming_Decl : Node_Id;
5515 Unit_Renaming : Node_Id;
5516
5517 begin
5518 Unit_Renaming :=
5519 Make_Subprogram_Renaming_Declaration (Loc,
5520 Specification =>
5521 Copy_Generic_Node
5522 (Specification (Original_Node (Gen_Decl)),
5523 Empty,
5524 Instantiating => True),
5525 Name => New_Occurrence_Of (Anon_Id, Loc));
5526
5527 -- The generic may be a child unit. The renaming needs an identifier
5528 -- with the proper name.
5529
5530 Set_Defining_Unit_Name (Specification (Unit_Renaming),
5531 Make_Defining_Identifier (Loc, Chars (Gen_Unit)));
5532
5533 -- If there is a formal subprogram with the same name as the unit
5534 -- itself, do not add this renaming declaration, to prevent
5535 -- ambiguities when there is a call with that name in the body.
5536 -- This is a partial and ugly fix for one ACATS test. ???
5537
5538 Renaming_Decl := First (Renaming_List);
5539 while Present (Renaming_Decl) loop
5540 if Nkind (Renaming_Decl) = N_Subprogram_Renaming_Declaration
5541 and then
5542 Chars (Defining_Entity (Renaming_Decl)) = Chars (Gen_Unit)
5543 then
5544 exit;
5545 end if;
5546
5547 Next (Renaming_Decl);
5548 end loop;
5549
5550 if No (Renaming_Decl) then
5551 Append (Unit_Renaming, Renaming_List);
5552 end if;
5553 end Build_Subprogram_Renaming;
5554
5555 -- Local variables
5556
5557 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
5558 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
5559 Saved_ISMP : constant Boolean :=
5560 Ignore_SPARK_Mode_Pragmas_In_Instance;
5561 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
5562 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
5563 -- Save the Ghost and SPARK mode-related data to restore on exit
5564
5565 Vis_Prims_List : Elist_Id := No_Elist;
5566 -- List of primitives made temporarily visible in the instantiation
5567 -- to match the visibility of the formal type
5568
5569 -- Start of processing for Analyze_Subprogram_Instantiation
5570
5571 begin
5572 -- Preserve relevant elaboration-related attributes of the context which
5573 -- are no longer available or very expensive to recompute once analysis,
5574 -- resolution, and expansion are over.
5575
5576 Mark_Elaboration_Attributes
5577 (N_Id => N,
5578 Checks => True,
5579 Level => True,
5580 Modes => True,
5581 Warnings => True);
5582
5583 -- Very first thing: check for special Text_IO unit in case we are
5584 -- instantiating one of the children of [[Wide_]Wide_]Text_IO. Of course
5585 -- such an instantiation is bogus (these are packages, not subprograms),
5586 -- but we get a better error message if we do this.
5587
5588 Check_Text_IO_Special_Unit (Gen_Id);
5589
5590 -- Make node global for error reporting
5591
5592 Instantiation_Node := N;
5593
5594 -- For package instantiations we turn off style checks, because they
5595 -- will have been emitted in the generic. For subprogram instantiations
5596 -- we want to apply at least the check on overriding indicators so we
5597 -- do not modify the style check status.
5598
5599 -- The renaming declarations for the actuals do not come from source and
5600 -- will not generate spurious warnings.
5601
5602 Preanalyze_Actuals (N);
5603
5604 Init_Env;
5605 Env_Installed := True;
5606 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
5607 Gen_Unit := Entity (Gen_Id);
5608
5609 -- A subprogram instantiation is Ghost when it is subject to pragma
5610 -- Ghost or the generic template is Ghost. Set the mode now to ensure
5611 -- that any nodes generated during analysis and expansion are marked as
5612 -- Ghost.
5613
5614 Mark_And_Set_Ghost_Instantiation (N, Gen_Unit);
5615
5616 Generate_Reference (Gen_Unit, Gen_Id);
5617
5618 if Nkind (Gen_Id) = N_Identifier
5619 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
5620 then
5621 Error_Msg_NE
5622 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
5623 end if;
5624
5625 if Etype (Gen_Unit) = Any_Type then
5626 Restore_Env;
5627 goto Leave;
5628 end if;
5629
5630 -- Verify that it is a generic subprogram of the right kind, and that
5631 -- it does not lead to a circular instantiation.
5632
5633 if K = E_Procedure and then Ekind (Gen_Unit) /= E_Generic_Procedure then
5634 Error_Msg_NE
5635 ("& is not the name of a generic procedure", Gen_Id, Gen_Unit);
5636
5637 elsif K = E_Function and then Ekind (Gen_Unit) /= E_Generic_Function then
5638 Error_Msg_NE
5639 ("& is not the name of a generic function", Gen_Id, Gen_Unit);
5640
5641 elsif In_Open_Scopes (Gen_Unit) then
5642 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
5643
5644 else
5645 Set_Ekind (Inst_Id, K);
5646 Set_Scope (Inst_Id, Current_Scope);
5647
5648 Set_Entity (Gen_Id, Gen_Unit);
5649 Set_Is_Instantiated (Gen_Unit);
5650
5651 if In_Extended_Main_Source_Unit (N) then
5652 Generate_Reference (Gen_Unit, N);
5653 end if;
5654
5655 -- If renaming, get original unit
5656
5657 if Present (Renamed_Object (Gen_Unit))
5658 and then Ekind_In (Renamed_Object (Gen_Unit), E_Generic_Procedure,
5659 E_Generic_Function)
5660 then
5661 Gen_Unit := Renamed_Object (Gen_Unit);
5662 Set_Is_Instantiated (Gen_Unit);
5663 Generate_Reference (Gen_Unit, N);
5664 end if;
5665
5666 if Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
5667 Error_Msg_Node_2 := Current_Scope;
5668 Error_Msg_NE
5669 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
5670 Circularity_Detected := True;
5671 Restore_Hidden_Primitives (Vis_Prims_List);
5672 goto Leave;
5673 end if;
5674
5675 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
5676
5677 -- Initialize renamings map, for error checking
5678
5679 Generic_Renamings.Set_Last (0);
5680 Generic_Renamings_HTable.Reset;
5681
5682 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
5683
5684 -- Copy original generic tree, to produce text for instantiation
5685
5686 Act_Tree :=
5687 Copy_Generic_Node
5688 (Original_Node (Gen_Decl), Empty, Instantiating => True);
5689
5690 -- Inherit overriding indicator from instance node
5691
5692 Act_Spec := Specification (Act_Tree);
5693 Set_Must_Override (Act_Spec, Must_Override (N));
5694 Set_Must_Not_Override (Act_Spec, Must_Not_Override (N));
5695
5696 Renaming_List :=
5697 Analyze_Associations
5698 (I_Node => N,
5699 Formals => Generic_Formal_Declarations (Act_Tree),
5700 F_Copy => Generic_Formal_Declarations (Gen_Decl));
5701
5702 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
5703
5704 -- The subprogram itself cannot contain a nested instance, so the
5705 -- current parent is left empty.
5706
5707 Set_Instance_Env (Gen_Unit, Empty);
5708
5709 -- Build the subprogram declaration, which does not appear in the
5710 -- generic template, and give it a sloc consistent with that of the
5711 -- template.
5712
5713 Set_Defining_Unit_Name (Act_Spec, Anon_Id);
5714 Set_Generic_Parent (Act_Spec, Gen_Unit);
5715 Act_Decl :=
5716 Make_Subprogram_Declaration (Sloc (Act_Spec),
5717 Specification => Act_Spec);
5718
5719 -- The aspects have been copied previously, but they have to be
5720 -- linked explicitly to the new subprogram declaration. Explicit
5721 -- pre/postconditions on the instance are analyzed below, in a
5722 -- separate step.
5723
5724 Move_Aspects (Act_Tree, To => Act_Decl);
5725 Set_Categorization_From_Pragmas (Act_Decl);
5726
5727 if Parent_Installed then
5728 Hide_Current_Scope;
5729 end if;
5730
5731 Append (Act_Decl, Renaming_List);
5732
5733 -- Contract-related source pragmas that follow a generic subprogram
5734 -- must be instantiated explicitly because they are not part of the
5735 -- subprogram template.
5736
5737 Instantiate_Subprogram_Contract
5738 (Original_Node (Gen_Decl), Renaming_List);
5739
5740 Build_Subprogram_Renaming;
5741
5742 -- If the context of the instance is subject to SPARK_Mode "off" or
5743 -- the annotation is altogether missing, set the global flag which
5744 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
5745 -- the instance. This should be done prior to analyzing the instance.
5746
5747 if SPARK_Mode /= On then
5748 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
5749 end if;
5750
5751 -- If the context of an instance is not subject to SPARK_Mode "off",
5752 -- and the generic spec is subject to an explicit SPARK_Mode pragma,
5753 -- the latter should be the one applicable to the instance.
5754
5755 if not Ignore_SPARK_Mode_Pragmas_In_Instance
5756 and then Saved_SM /= Off
5757 and then Present (SPARK_Pragma (Gen_Unit))
5758 then
5759 Set_SPARK_Mode (Gen_Unit);
5760 end if;
5761
5762 Analyze_Instance_And_Renamings;
5763
5764 -- Restore SPARK_Mode from the context after analysis of the package
5765 -- declaration, so that the SPARK_Mode on the generic spec does not
5766 -- apply to the pending instance for the instance body.
5767
5768 if not Ignore_SPARK_Mode_Pragmas_In_Instance
5769 and then Saved_SM /= Off
5770 and then Present (SPARK_Pragma (Gen_Unit))
5771 then
5772 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
5773 end if;
5774
5775 -- If the generic is marked Import (Intrinsic), then so is the
5776 -- instance. This indicates that there is no body to instantiate. If
5777 -- generic is marked inline, so it the instance, and the anonymous
5778 -- subprogram it renames. If inlined, or else if inlining is enabled
5779 -- for the compilation, we generate the instance body even if it is
5780 -- not within the main unit.
5781
5782 if Is_Intrinsic_Subprogram (Gen_Unit) then
5783 Set_Is_Intrinsic_Subprogram (Anon_Id);
5784 Set_Is_Intrinsic_Subprogram (Act_Decl_Id);
5785
5786 if Chars (Gen_Unit) = Name_Unchecked_Conversion then
5787 Validate_Unchecked_Conversion (N, Act_Decl_Id);
5788 end if;
5789 end if;
5790
5791 -- Inherit convention from generic unit. Intrinsic convention, as for
5792 -- an instance of unchecked conversion, is not inherited because an
5793 -- explicit Ada instance has been created.
5794
5795 if Has_Convention_Pragma (Gen_Unit)
5796 and then Convention (Gen_Unit) /= Convention_Intrinsic
5797 then
5798 Set_Convention (Act_Decl_Id, Convention (Gen_Unit));
5799 Set_Is_Exported (Act_Decl_Id, Is_Exported (Gen_Unit));
5800 end if;
5801
5802 Generate_Definition (Act_Decl_Id);
5803
5804 -- Inherit all inlining-related flags which apply to the generic in
5805 -- the subprogram and its declaration.
5806
5807 Set_Is_Inlined (Act_Decl_Id, Is_Inlined (Gen_Unit));
5808 Set_Is_Inlined (Anon_Id, Is_Inlined (Gen_Unit));
5809
5810 Set_Has_Pragma_Inline (Act_Decl_Id, Has_Pragma_Inline (Gen_Unit));
5811 Set_Has_Pragma_Inline (Anon_Id, Has_Pragma_Inline (Gen_Unit));
5812
5813 Set_Has_Pragma_Inline_Always
5814 (Act_Decl_Id, Has_Pragma_Inline_Always (Gen_Unit));
5815 Set_Has_Pragma_Inline_Always
5816 (Anon_Id, Has_Pragma_Inline_Always (Gen_Unit));
5817
5818 Set_Has_Pragma_No_Inline
5819 (Act_Decl_Id, Has_Pragma_No_Inline (Gen_Unit));
5820 Set_Has_Pragma_No_Inline
5821 (Anon_Id, Has_Pragma_No_Inline (Gen_Unit));
5822
5823 -- Propagate No_Return if pragma applied to generic unit. This must
5824 -- be done explicitly because pragma does not appear in generic
5825 -- declaration (unlike the aspect case).
5826
5827 if No_Return (Gen_Unit) then
5828 Set_No_Return (Act_Decl_Id);
5829 Set_No_Return (Anon_Id);
5830 end if;
5831
5832 -- Mark both the instance spec and the anonymous package in case the
5833 -- body is instantiated at a later pass. This preserves the original
5834 -- context in effect for the body.
5835
5836 if SPARK_Mode /= On then
5837 Set_Ignore_SPARK_Mode_Pragmas (Act_Decl_Id);
5838 Set_Ignore_SPARK_Mode_Pragmas (Anon_Id);
5839 end if;
5840
5841 if Legacy_Elaboration_Checks
5842 and then not Is_Intrinsic_Subprogram (Gen_Unit)
5843 then
5844 Check_Elab_Instantiation (N);
5845 end if;
5846
5847 -- Save the scenario for later examination by the ABE Processing
5848 -- phase.
5849
5850 Record_Elaboration_Scenario (N);
5851
5852 -- The instantiation results in a guaranteed ABE. Create a completing
5853 -- body for the subprogram declaration because the real body will not
5854 -- be instantiated.
5855
5856 if Is_Known_Guaranteed_ABE (N) then
5857 Provide_Completing_Bodies (Instance_Spec (N));
5858 end if;
5859
5860 if Is_Dispatching_Operation (Act_Decl_Id)
5861 and then Ada_Version >= Ada_2005
5862 then
5863 declare
5864 Formal : Entity_Id;
5865
5866 begin
5867 Formal := First_Formal (Act_Decl_Id);
5868 while Present (Formal) loop
5869 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type
5870 and then Is_Controlling_Formal (Formal)
5871 and then not Can_Never_Be_Null (Formal)
5872 then
5873 Error_Msg_NE
5874 ("access parameter& is controlling,", N, Formal);
5875 Error_Msg_NE
5876 ("\corresponding parameter of & must be explicitly "
5877 & "null-excluding", N, Gen_Id);
5878 end if;
5879
5880 Next_Formal (Formal);
5881 end loop;
5882 end;
5883 end if;
5884
5885 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
5886
5887 Validate_Categorization_Dependency (N, Act_Decl_Id);
5888
5889 if not Is_Intrinsic_Subprogram (Act_Decl_Id) then
5890 Inherit_Context (Gen_Decl, N);
5891
5892 Restore_Private_Views (Pack_Id, False);
5893
5894 -- If the context requires a full instantiation, mark node for
5895 -- subsequent construction of the body.
5896
5897 if Need_Subprogram_Instance_Body (N, Act_Decl_Id) then
5898 Check_Forward_Instantiation (Gen_Decl);
5899
5900 -- The wrapper package is always delayed, because it does not
5901 -- constitute a freeze point, but to insure that the freeze node
5902 -- is placed properly, it is created directly when instantiating
5903 -- the body (otherwise the freeze node might appear to early for
5904 -- nested instantiations).
5905
5906 elsif Nkind (Parent (N)) = N_Compilation_Unit then
5907 Rewrite (N, Unit (Parent (N)));
5908 Set_Unit (Parent (N), N);
5909 end if;
5910
5911 -- Replace instance node for library-level instantiations of
5912 -- intrinsic subprograms.
5913
5914 elsif Nkind (Parent (N)) = N_Compilation_Unit then
5915 Rewrite (N, Unit (Parent (N)));
5916 Set_Unit (Parent (N), N);
5917 end if;
5918
5919 if Parent_Installed then
5920 Remove_Parent;
5921 end if;
5922
5923 Restore_Hidden_Primitives (Vis_Prims_List);
5924 Restore_Env;
5925 Env_Installed := False;
5926 Generic_Renamings.Set_Last (0);
5927 Generic_Renamings_HTable.Reset;
5928 end if;
5929
5930 <<Leave>>
5931 -- Analyze aspects in declaration if no errors appear in the instance.
5932
5933 if Has_Aspects (N) and then Serious_Errors_Detected = Errs then
5934 Analyze_Aspect_Specifications (N, Act_Decl_Id);
5935 end if;
5936
5937 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
5938 Restore_Ghost_Region (Saved_GM, Saved_IGR);
5939 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
5940
5941 exception
5942 when Instantiation_Error =>
5943 if Parent_Installed then
5944 Remove_Parent;
5945 end if;
5946
5947 if Env_Installed then
5948 Restore_Env;
5949 end if;
5950
5951 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
5952 Restore_Ghost_Region (Saved_GM, Saved_IGR);
5953 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
5954 end Analyze_Subprogram_Instantiation;
5955
5956 -------------------------
5957 -- Get_Associated_Node --
5958 -------------------------
5959
5960 function Get_Associated_Node (N : Node_Id) return Node_Id is
5961 Assoc : Node_Id;
5962
5963 begin
5964 Assoc := Associated_Node (N);
5965
5966 if Nkind (Assoc) /= Nkind (N) then
5967 return Assoc;
5968
5969 elsif Nkind_In (Assoc, N_Aggregate, N_Extension_Aggregate) then
5970 return Assoc;
5971
5972 else
5973 -- If the node is part of an inner generic, it may itself have been
5974 -- remapped into a further generic copy. Associated_Node is otherwise
5975 -- used for the entity of the node, and will be of a different node
5976 -- kind, or else N has been rewritten as a literal or function call.
5977
5978 while Present (Associated_Node (Assoc))
5979 and then Nkind (Associated_Node (Assoc)) = Nkind (Assoc)
5980 loop
5981 Assoc := Associated_Node (Assoc);
5982 end loop;
5983
5984 -- Follow an additional link in case the final node was rewritten.
5985 -- This can only happen with nested generic units.
5986
5987 if (Nkind (Assoc) = N_Identifier or else Nkind (Assoc) in N_Op)
5988 and then Present (Associated_Node (Assoc))
5989 and then (Nkind_In (Associated_Node (Assoc), N_Function_Call,
5990 N_Explicit_Dereference,
5991 N_Integer_Literal,
5992 N_Real_Literal,
5993 N_String_Literal))
5994 then
5995 Assoc := Associated_Node (Assoc);
5996 end if;
5997
5998 -- An additional special case: an unconstrained type in an object
5999 -- declaration may have been rewritten as a local subtype constrained
6000 -- by the expression in the declaration. We need to recover the
6001 -- original entity, which may be global.
6002
6003 if Present (Original_Node (Assoc))
6004 and then Nkind (Parent (N)) = N_Object_Declaration
6005 then
6006 Assoc := Original_Node (Assoc);
6007 end if;
6008
6009 return Assoc;
6010 end if;
6011 end Get_Associated_Node;
6012
6013 ----------------------------
6014 -- Build_Function_Wrapper --
6015 ----------------------------
6016
6017 function Build_Function_Wrapper
6018 (Formal_Subp : Entity_Id;
6019 Actual_Subp : Entity_Id) return Node_Id
6020 is
6021 Loc : constant Source_Ptr := Sloc (Current_Scope);
6022 Ret_Type : constant Entity_Id := Get_Instance_Of (Etype (Formal_Subp));
6023 Actuals : List_Id;
6024 Decl : Node_Id;
6025 Func_Name : Node_Id;
6026 Func : Entity_Id;
6027 Parm_Type : Node_Id;
6028 Profile : List_Id := New_List;
6029 Spec : Node_Id;
6030 Act_F : Entity_Id;
6031 Form_F : Entity_Id;
6032 New_F : Entity_Id;
6033
6034 begin
6035 Func_Name := New_Occurrence_Of (Actual_Subp, Loc);
6036
6037 Func := Make_Defining_Identifier (Loc, Chars (Formal_Subp));
6038 Set_Ekind (Func, E_Function);
6039 Set_Is_Generic_Actual_Subprogram (Func);
6040
6041 Actuals := New_List;
6042 Profile := New_List;
6043
6044 Act_F := First_Formal (Actual_Subp);
6045 Form_F := First_Formal (Formal_Subp);
6046 while Present (Form_F) loop
6047
6048 -- Create new formal for profile of wrapper, and add a reference
6049 -- to it in the list of actuals for the enclosing call. The name
6050 -- must be that of the formal in the formal subprogram, because
6051 -- calls to it in the generic body may use named associations.
6052
6053 New_F := Make_Defining_Identifier (Loc, Chars (Form_F));
6054
6055 Parm_Type :=
6056 New_Occurrence_Of (Get_Instance_Of (Etype (Form_F)), Loc);
6057
6058 Append_To (Profile,
6059 Make_Parameter_Specification (Loc,
6060 Defining_Identifier => New_F,
6061 Parameter_Type => Parm_Type));
6062
6063 Append_To (Actuals, New_Occurrence_Of (New_F, Loc));
6064 Next_Formal (Form_F);
6065
6066 if Present (Act_F) then
6067 Next_Formal (Act_F);
6068 end if;
6069 end loop;
6070
6071 Spec :=
6072 Make_Function_Specification (Loc,
6073 Defining_Unit_Name => Func,
6074 Parameter_Specifications => Profile,
6075 Result_Definition => New_Occurrence_Of (Ret_Type, Loc));
6076
6077 Decl :=
6078 Make_Expression_Function (Loc,
6079 Specification => Spec,
6080 Expression =>
6081 Make_Function_Call (Loc,
6082 Name => Func_Name,
6083 Parameter_Associations => Actuals));
6084
6085 return Decl;
6086 end Build_Function_Wrapper;
6087
6088 ----------------------------
6089 -- Build_Operator_Wrapper --
6090 ----------------------------
6091
6092 function Build_Operator_Wrapper
6093 (Formal_Subp : Entity_Id;
6094 Actual_Subp : Entity_Id) return Node_Id
6095 is
6096 Loc : constant Source_Ptr := Sloc (Current_Scope);
6097 Ret_Type : constant Entity_Id :=
6098 Get_Instance_Of (Etype (Formal_Subp));
6099 Op_Type : constant Entity_Id :=
6100 Get_Instance_Of (Etype (First_Formal (Formal_Subp)));
6101 Is_Binary : constant Boolean :=
6102 Present (Next_Formal (First_Formal (Formal_Subp)));
6103
6104 Decl : Node_Id;
6105 Expr : Node_Id := Empty;
6106 F1, F2 : Entity_Id;
6107 Func : Entity_Id;
6108 Op_Name : Name_Id;
6109 Spec : Node_Id;
6110 L, R : Node_Id;
6111
6112 begin
6113 Op_Name := Chars (Actual_Subp);
6114
6115 -- Create entities for wrapper function and its formals
6116
6117 F1 := Make_Temporary (Loc, 'A');
6118 F2 := Make_Temporary (Loc, 'B');
6119 L := New_Occurrence_Of (F1, Loc);
6120 R := New_Occurrence_Of (F2, Loc);
6121
6122 Func := Make_Defining_Identifier (Loc, Chars (Formal_Subp));
6123 Set_Ekind (Func, E_Function);
6124 Set_Is_Generic_Actual_Subprogram (Func);
6125
6126 Spec :=
6127 Make_Function_Specification (Loc,
6128 Defining_Unit_Name => Func,
6129 Parameter_Specifications => New_List (
6130 Make_Parameter_Specification (Loc,
6131 Defining_Identifier => F1,
6132 Parameter_Type => New_Occurrence_Of (Op_Type, Loc))),
6133 Result_Definition => New_Occurrence_Of (Ret_Type, Loc));
6134
6135 if Is_Binary then
6136 Append_To (Parameter_Specifications (Spec),
6137 Make_Parameter_Specification (Loc,
6138 Defining_Identifier => F2,
6139 Parameter_Type => New_Occurrence_Of (Op_Type, Loc)));
6140 end if;
6141
6142 -- Build expression as a function call, or as an operator node
6143 -- that corresponds to the name of the actual, starting with
6144 -- binary operators.
6145
6146 if Op_Name not in Any_Operator_Name then
6147 Expr :=
6148 Make_Function_Call (Loc,
6149 Name =>
6150 New_Occurrence_Of (Actual_Subp, Loc),
6151 Parameter_Associations => New_List (L));
6152
6153 if Is_Binary then
6154 Append_To (Parameter_Associations (Expr), R);
6155 end if;
6156
6157 -- Binary operators
6158
6159 elsif Is_Binary then
6160 if Op_Name = Name_Op_And then
6161 Expr := Make_Op_And (Loc, Left_Opnd => L, Right_Opnd => R);
6162 elsif Op_Name = Name_Op_Or then
6163 Expr := Make_Op_Or (Loc, Left_Opnd => L, Right_Opnd => R);
6164 elsif Op_Name = Name_Op_Xor then
6165 Expr := Make_Op_Xor (Loc, Left_Opnd => L, Right_Opnd => R);
6166 elsif Op_Name = Name_Op_Eq then
6167 Expr := Make_Op_Eq (Loc, Left_Opnd => L, Right_Opnd => R);
6168 elsif Op_Name = Name_Op_Ne then
6169 Expr := Make_Op_Ne (Loc, Left_Opnd => L, Right_Opnd => R);
6170 elsif Op_Name = Name_Op_Le then
6171 Expr := Make_Op_Le (Loc, Left_Opnd => L, Right_Opnd => R);
6172 elsif Op_Name = Name_Op_Gt then
6173 Expr := Make_Op_Gt (Loc, Left_Opnd => L, Right_Opnd => R);
6174 elsif Op_Name = Name_Op_Ge then
6175 Expr := Make_Op_Ge (Loc, Left_Opnd => L, Right_Opnd => R);
6176 elsif Op_Name = Name_Op_Lt then
6177 Expr := Make_Op_Lt (Loc, Left_Opnd => L, Right_Opnd => R);
6178 elsif Op_Name = Name_Op_Add then
6179 Expr := Make_Op_Add (Loc, Left_Opnd => L, Right_Opnd => R);
6180 elsif Op_Name = Name_Op_Subtract then
6181 Expr := Make_Op_Subtract (Loc, Left_Opnd => L, Right_Opnd => R);
6182 elsif Op_Name = Name_Op_Concat then
6183 Expr := Make_Op_Concat (Loc, Left_Opnd => L, Right_Opnd => R);
6184 elsif Op_Name = Name_Op_Multiply then
6185 Expr := Make_Op_Multiply (Loc, Left_Opnd => L, Right_Opnd => R);
6186 elsif Op_Name = Name_Op_Divide then
6187 Expr := Make_Op_Divide (Loc, Left_Opnd => L, Right_Opnd => R);
6188 elsif Op_Name = Name_Op_Mod then
6189 Expr := Make_Op_Mod (Loc, Left_Opnd => L, Right_Opnd => R);
6190 elsif Op_Name = Name_Op_Rem then
6191 Expr := Make_Op_Rem (Loc, Left_Opnd => L, Right_Opnd => R);
6192 elsif Op_Name = Name_Op_Expon then
6193 Expr := Make_Op_Expon (Loc, Left_Opnd => L, Right_Opnd => R);
6194 end if;
6195
6196 -- Unary operators
6197
6198 else
6199 if Op_Name = Name_Op_Add then
6200 Expr := Make_Op_Plus (Loc, Right_Opnd => L);
6201 elsif Op_Name = Name_Op_Subtract then
6202 Expr := Make_Op_Minus (Loc, Right_Opnd => L);
6203 elsif Op_Name = Name_Op_Abs then
6204 Expr := Make_Op_Abs (Loc, Right_Opnd => L);
6205 elsif Op_Name = Name_Op_Not then
6206 Expr := Make_Op_Not (Loc, Right_Opnd => L);
6207 end if;
6208 end if;
6209
6210 Decl :=
6211 Make_Expression_Function (Loc,
6212 Specification => Spec,
6213 Expression => Expr);
6214
6215 return Decl;
6216 end Build_Operator_Wrapper;
6217
6218 -----------------------------------
6219 -- Build_Subprogram_Decl_Wrapper --
6220 -----------------------------------
6221
6222 function Build_Subprogram_Decl_Wrapper
6223 (Formal_Subp : Entity_Id) return Node_Id
6224 is
6225 Loc : constant Source_Ptr := Sloc (Current_Scope);
6226 Ret_Type : constant Entity_Id := Get_Instance_Of (Etype (Formal_Subp));
6227 Decl : Node_Id;
6228 Subp : Entity_Id;
6229 Parm_Spec : Node_Id;
6230 Profile : List_Id := New_List;
6231 Spec : Node_Id;
6232 Form_F : Entity_Id;
6233 New_F : Entity_Id;
6234
6235 begin
6236
6237 Subp := Make_Defining_Identifier (Loc, Chars (Formal_Subp));
6238 Set_Ekind (Subp, Ekind (Formal_Subp));
6239 Set_Is_Generic_Actual_Subprogram (Subp);
6240
6241 Profile := Parameter_Specifications (
6242 New_Copy_Tree
6243 (Specification (Unit_Declaration_Node (Formal_Subp))));
6244
6245 Form_F := First_Formal (Formal_Subp);
6246 Parm_Spec := First (Profile);
6247
6248 -- Create new entities for the formals. Reset entities so that
6249 -- parameter types are properly resolved when wrapper declaration
6250 -- is analyzed.
6251
6252 while Present (Parm_Spec) loop
6253 New_F := Make_Defining_Identifier (Loc, Chars (Form_F));
6254 Set_Defining_Identifier (Parm_Spec, New_F);
6255 Set_Entity (Parameter_Type (Parm_Spec), Empty);
6256 Next (Parm_Spec);
6257 Next_Formal (Form_F);
6258 end loop;
6259
6260 if Ret_Type = Standard_Void_Type then
6261 Spec :=
6262 Make_Procedure_Specification (Loc,
6263 Defining_Unit_Name => Subp,
6264 Parameter_Specifications => Profile);
6265 else
6266 Spec :=
6267 Make_Function_Specification (Loc,
6268 Defining_Unit_Name => Subp,
6269 Parameter_Specifications => Profile,
6270 Result_Definition => New_Occurrence_Of (Ret_Type, Loc));
6271 end if;
6272
6273 Decl :=
6274 Make_Subprogram_Declaration (Loc, Specification => Spec);
6275
6276 return Decl;
6277 end Build_Subprogram_Decl_Wrapper;
6278
6279 -----------------------------------
6280 -- Build_Subprogram_Body_Wrapper --
6281 -----------------------------------
6282
6283 function Build_Subprogram_Body_Wrapper
6284 (Formal_Subp : Entity_Id;
6285 Actual_Name : Node_Id) return Node_Id
6286 is
6287 Loc : constant Source_Ptr := Sloc (Current_Scope);
6288 Ret_Type : constant Entity_Id := Get_Instance_Of (Etype (Formal_Subp));
6289 Spec_Node : constant Node_Id :=
6290 Specification
6291 (Build_Subprogram_Decl_Wrapper (Formal_Subp));
6292 Act : Node_Id;
6293 Actuals : List_Id;
6294 Body_Node : Node_Id;
6295 Stmt : Node_Id;
6296 begin
6297 Actuals := New_List;
6298 Act := First (Parameter_Specifications (Spec_Node));
6299
6300 while Present (Act) loop
6301 Append_To (Actuals,
6302 Make_Identifier (Loc, Chars (Defining_Identifier (Act))));
6303 Next (Act);
6304 end loop;
6305
6306 if Ret_Type = Standard_Void_Type then
6307 Stmt := Make_Procedure_Call_Statement (Loc,
6308 Name => Actual_Name,
6309 Parameter_Associations => Actuals);
6310
6311 else
6312 Stmt := Make_Simple_Return_Statement (Loc,
6313 Expression =>
6314 Make_Function_Call (Loc,
6315 Name => Actual_Name,
6316 Parameter_Associations => Actuals));
6317 end if;
6318
6319 Body_Node := Make_Subprogram_Body (Loc,
6320 Specification => Spec_Node,
6321 Declarations => New_List,
6322 Handled_Statement_Sequence =>
6323 Make_Handled_Sequence_Of_Statements (Loc,
6324 Statements => New_List (Stmt)));
6325
6326 return Body_Node;
6327 end Build_Subprogram_Body_Wrapper;
6328
6329 -------------------------------------------
6330 -- Build_Instance_Compilation_Unit_Nodes --
6331 -------------------------------------------
6332
6333 procedure Build_Instance_Compilation_Unit_Nodes
6334 (N : Node_Id;
6335 Act_Body : Node_Id;
6336 Act_Decl : Node_Id)
6337 is
6338 Decl_Cunit : Node_Id;
6339 Body_Cunit : Node_Id;
6340 Citem : Node_Id;
6341 New_Main : constant Entity_Id := Defining_Entity (Act_Decl);
6342 Old_Main : constant Entity_Id := Cunit_Entity (Main_Unit);
6343
6344 begin
6345 -- A new compilation unit node is built for the instance declaration
6346
6347 Decl_Cunit :=
6348 Make_Compilation_Unit (Sloc (N),
6349 Context_Items => Empty_List,
6350 Unit => Act_Decl,
6351 Aux_Decls_Node => Make_Compilation_Unit_Aux (Sloc (N)));
6352
6353 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
6354
6355 -- The new compilation unit is linked to its body, but both share the
6356 -- same file, so we do not set Body_Required on the new unit so as not
6357 -- to create a spurious dependency on a non-existent body in the ali.
6358 -- This simplifies CodePeer unit traversal.
6359
6360 -- We use the original instantiation compilation unit as the resulting
6361 -- compilation unit of the instance, since this is the main unit.
6362
6363 Rewrite (N, Act_Body);
6364
6365 -- Propagate the aspect specifications from the package body template to
6366 -- the instantiated version of the package body.
6367
6368 if Has_Aspects (Act_Body) then
6369 Set_Aspect_Specifications
6370 (N, New_Copy_List_Tree (Aspect_Specifications (Act_Body)));
6371 end if;
6372
6373 Body_Cunit := Parent (N);
6374
6375 -- The two compilation unit nodes are linked by the Library_Unit field
6376
6377 Set_Library_Unit (Decl_Cunit, Body_Cunit);
6378 Set_Library_Unit (Body_Cunit, Decl_Cunit);
6379
6380 -- Preserve the private nature of the package if needed
6381
6382 Set_Private_Present (Decl_Cunit, Private_Present (Body_Cunit));
6383
6384 -- If the instance is not the main unit, its context, categorization
6385 -- and elaboration entity are not relevant to the compilation.
6386
6387 if Body_Cunit /= Cunit (Main_Unit) then
6388 Make_Instance_Unit (Body_Cunit, In_Main => False);
6389 return;
6390 end if;
6391
6392 -- The context clause items on the instantiation, which are now attached
6393 -- to the body compilation unit (since the body overwrote the original
6394 -- instantiation node), semantically belong on the spec, so copy them
6395 -- there. It's harmless to leave them on the body as well. In fact one
6396 -- could argue that they belong in both places.
6397
6398 Citem := First (Context_Items (Body_Cunit));
6399 while Present (Citem) loop
6400 Append (New_Copy (Citem), Context_Items (Decl_Cunit));
6401 Next (Citem);
6402 end loop;
6403
6404 -- Propagate categorization flags on packages, so that they appear in
6405 -- the ali file for the spec of the unit.
6406
6407 if Ekind (New_Main) = E_Package then
6408 Set_Is_Pure (Old_Main, Is_Pure (New_Main));
6409 Set_Is_Preelaborated (Old_Main, Is_Preelaborated (New_Main));
6410 Set_Is_Remote_Types (Old_Main, Is_Remote_Types (New_Main));
6411 Set_Is_Shared_Passive (Old_Main, Is_Shared_Passive (New_Main));
6412 Set_Is_Remote_Call_Interface
6413 (Old_Main, Is_Remote_Call_Interface (New_Main));
6414 end if;
6415
6416 -- Make entry in Units table, so that binder can generate call to
6417 -- elaboration procedure for body, if any.
6418
6419 Make_Instance_Unit (Body_Cunit, In_Main => True);
6420 Main_Unit_Entity := New_Main;
6421 Set_Cunit_Entity (Main_Unit, Main_Unit_Entity);
6422
6423 -- Build elaboration entity, since the instance may certainly generate
6424 -- elaboration code requiring a flag for protection.
6425
6426 Build_Elaboration_Entity (Decl_Cunit, New_Main);
6427 end Build_Instance_Compilation_Unit_Nodes;
6428
6429 -----------------------------
6430 -- Check_Access_Definition --
6431 -----------------------------
6432
6433 procedure Check_Access_Definition (N : Node_Id) is
6434 begin
6435 pragma Assert
6436 (Ada_Version >= Ada_2005 and then Present (Access_Definition (N)));
6437 null;
6438 end Check_Access_Definition;
6439
6440 -----------------------------------
6441 -- Check_Formal_Package_Instance --
6442 -----------------------------------
6443
6444 -- If the formal has specific parameters, they must match those of the
6445 -- actual. Both of them are instances, and the renaming declarations for
6446 -- their formal parameters appear in the same order in both. The analyzed
6447 -- formal has been analyzed in the context of the current instance.
6448
6449 procedure Check_Formal_Package_Instance
6450 (Formal_Pack : Entity_Id;
6451 Actual_Pack : Entity_Id)
6452 is
6453 E1 : Entity_Id := First_Entity (Actual_Pack);
6454 E2 : Entity_Id := First_Entity (Formal_Pack);
6455 Prev_E1 : Entity_Id;
6456
6457 Expr1 : Node_Id;
6458 Expr2 : Node_Id;
6459
6460 procedure Check_Mismatch (B : Boolean);
6461 -- Common error routine for mismatch between the parameters of the
6462 -- actual instance and those of the formal package.
6463
6464 function Is_Defaulted (Param : Entity_Id) return Boolean;
6465 -- If the formal package has partly box-initialized formals, skip
6466 -- conformance check for these formals. Previously the code assumed
6467 -- that box initialization for a formal package applied to all its
6468 -- formal parameters.
6469
6470 function Same_Instantiated_Constant (E1, E2 : Entity_Id) return Boolean;
6471 -- The formal may come from a nested formal package, and the actual may
6472 -- have been constant-folded. To determine whether the two denote the
6473 -- same entity we may have to traverse several definitions to recover
6474 -- the ultimate entity that they refer to.
6475
6476 function Same_Instantiated_Function (E1, E2 : Entity_Id) return Boolean;
6477 -- The formal and the actual must be identical, but if both are
6478 -- given by attributes they end up renaming different generated bodies,
6479 -- and we must verify that the attributes themselves match.
6480
6481 function Same_Instantiated_Variable (E1, E2 : Entity_Id) return Boolean;
6482 -- Similarly, if the formal comes from a nested formal package, the
6483 -- actual may designate the formal through multiple renamings, which
6484 -- have to be followed to determine the original variable in question.
6485
6486 --------------------
6487 -- Check_Mismatch --
6488 --------------------
6489
6490 procedure Check_Mismatch (B : Boolean) is
6491 -- A Formal_Type_Declaration for a derived private type is rewritten
6492 -- as a private extension decl. (see Analyze_Formal_Derived_Type),
6493 -- which is why we examine the original node.
6494
6495 Kind : constant Node_Kind := Nkind (Original_Node (Parent (E2)));
6496
6497 begin
6498 if Kind = N_Formal_Type_Declaration then
6499 return;
6500
6501 elsif Nkind_In (Kind, N_Formal_Object_Declaration,
6502 N_Formal_Package_Declaration)
6503 or else Kind in N_Formal_Subprogram_Declaration
6504 then
6505 null;
6506
6507 -- Ada 2012: If both formal and actual are incomplete types they
6508 -- are conformant.
6509
6510 elsif Is_Incomplete_Type (E1) and then Is_Incomplete_Type (E2) then
6511 null;
6512
6513 elsif B then
6514 Error_Msg_NE
6515 ("actual for & in actual instance does not match formal",
6516 Parent (Actual_Pack), E1);
6517 end if;
6518 end Check_Mismatch;
6519
6520 ------------------
6521 -- Is_Defaulted --
6522 ------------------
6523
6524 function Is_Defaulted (Param : Entity_Id) return Boolean is
6525 Assoc : Node_Id;
6526
6527 begin
6528 Assoc :=
6529 First (Generic_Associations (Parent
6530 (Associated_Formal_Package (Actual_Pack))));
6531
6532 while Present (Assoc) loop
6533 if Nkind (Assoc) = N_Others_Choice then
6534 return True;
6535
6536 elsif Nkind (Assoc) = N_Generic_Association
6537 and then Chars (Selector_Name (Assoc)) = Chars (Param)
6538 then
6539 return Box_Present (Assoc);
6540 end if;
6541
6542 Next (Assoc);
6543 end loop;
6544
6545 return False;
6546 end Is_Defaulted;
6547
6548 --------------------------------
6549 -- Same_Instantiated_Constant --
6550 --------------------------------
6551
6552 function Same_Instantiated_Constant
6553 (E1, E2 : Entity_Id) return Boolean
6554 is
6555 Ent : Entity_Id;
6556
6557 begin
6558 Ent := E2;
6559 while Present (Ent) loop
6560 if E1 = Ent then
6561 return True;
6562
6563 elsif Ekind (Ent) /= E_Constant then
6564 return False;
6565
6566 elsif Is_Entity_Name (Constant_Value (Ent)) then
6567 if Entity (Constant_Value (Ent)) = E1 then
6568 return True;
6569 else
6570 Ent := Entity (Constant_Value (Ent));
6571 end if;
6572
6573 -- The actual may be a constant that has been folded. Recover
6574 -- original name.
6575
6576 elsif Is_Entity_Name (Original_Node (Constant_Value (Ent))) then
6577 Ent := Entity (Original_Node (Constant_Value (Ent)));
6578
6579 else
6580 return False;
6581 end if;
6582 end loop;
6583
6584 return False;
6585 end Same_Instantiated_Constant;
6586
6587 --------------------------------
6588 -- Same_Instantiated_Function --
6589 --------------------------------
6590
6591 function Same_Instantiated_Function
6592 (E1, E2 : Entity_Id) return Boolean
6593 is
6594 U1, U2 : Node_Id;
6595 begin
6596 if Alias (E1) = Alias (E2) then
6597 return True;
6598
6599 elsif Present (Alias (E2)) then
6600 U1 := Original_Node (Unit_Declaration_Node (E1));
6601 U2 := Original_Node (Unit_Declaration_Node (Alias (E2)));
6602
6603 return Nkind (U1) = N_Subprogram_Renaming_Declaration
6604 and then Nkind (Name (U1)) = N_Attribute_Reference
6605
6606 and then Nkind (U2) = N_Subprogram_Renaming_Declaration
6607 and then Nkind (Name (U2)) = N_Attribute_Reference
6608
6609 and then
6610 Attribute_Name (Name (U1)) = Attribute_Name (Name (U2));
6611 else
6612 return False;
6613 end if;
6614 end Same_Instantiated_Function;
6615
6616 --------------------------------
6617 -- Same_Instantiated_Variable --
6618 --------------------------------
6619
6620 function Same_Instantiated_Variable
6621 (E1, E2 : Entity_Id) return Boolean
6622 is
6623 function Original_Entity (E : Entity_Id) return Entity_Id;
6624 -- Follow chain of renamings to the ultimate ancestor
6625
6626 ---------------------
6627 -- Original_Entity --
6628 ---------------------
6629
6630 function Original_Entity (E : Entity_Id) return Entity_Id is
6631 Orig : Entity_Id;
6632
6633 begin
6634 Orig := E;
6635 while Nkind (Parent (Orig)) = N_Object_Renaming_Declaration
6636 and then Present (Renamed_Object (Orig))
6637 and then Is_Entity_Name (Renamed_Object (Orig))
6638 loop
6639 Orig := Entity (Renamed_Object (Orig));
6640 end loop;
6641
6642 return Orig;
6643 end Original_Entity;
6644
6645 -- Start of processing for Same_Instantiated_Variable
6646
6647 begin
6648 return Ekind (E1) = Ekind (E2)
6649 and then Original_Entity (E1) = Original_Entity (E2);
6650 end Same_Instantiated_Variable;
6651
6652 -- Start of processing for Check_Formal_Package_Instance
6653
6654 begin
6655 Prev_E1 := E1;
6656 while Present (E1) and then Present (E2) loop
6657 exit when Ekind (E1) = E_Package
6658 and then Renamed_Entity (E1) = Renamed_Entity (Actual_Pack);
6659
6660 -- If the formal is the renaming of the formal package, this
6661 -- is the end of its formal part, which may occur before the
6662 -- end of the formal part in the actual in the presence of
6663 -- defaulted parameters in the formal package.
6664
6665 exit when Nkind (Parent (E2)) = N_Package_Renaming_Declaration
6666 and then Renamed_Entity (E2) = Scope (E2);
6667
6668 -- The analysis of the actual may generate additional internal
6669 -- entities. If the formal is defaulted, there is no corresponding
6670 -- analysis and the internal entities must be skipped, until we
6671 -- find corresponding entities again.
6672
6673 if Comes_From_Source (E2)
6674 and then not Comes_From_Source (E1)
6675 and then Chars (E1) /= Chars (E2)
6676 then
6677 while Present (E1) and then Chars (E1) /= Chars (E2) loop
6678 Next_Entity (E1);
6679 end loop;
6680 end if;
6681
6682 if No (E1) then
6683 return;
6684
6685 -- Entities may be declared without full declaration, such as
6686 -- itypes and predefined operators (concatenation for arrays, eg).
6687 -- Skip it and keep the formal entity to find a later match for it.
6688
6689 elsif No (Parent (E2)) and then Ekind (E1) /= Ekind (E2) then
6690 E1 := Prev_E1;
6691 goto Next_E;
6692
6693 -- If the formal entity comes from a formal declaration, it was
6694 -- defaulted in the formal package, and no check is needed on it.
6695
6696 elsif Nkind_In (Original_Node (Parent (E2)),
6697 N_Formal_Object_Declaration,
6698 N_Formal_Type_Declaration)
6699 then
6700 -- If the formal is a tagged type the corresponding class-wide
6701 -- type has been generated as well, and it must be skipped.
6702
6703 if Is_Type (E2) and then Is_Tagged_Type (E2) then
6704 Next_Entity (E2);
6705 end if;
6706
6707 goto Next_E;
6708
6709 -- Ditto for defaulted formal subprograms.
6710
6711 elsif Is_Overloadable (E1)
6712 and then Nkind (Unit_Declaration_Node (E2)) in
6713 N_Formal_Subprogram_Declaration
6714 then
6715 goto Next_E;
6716
6717 elsif Is_Defaulted (E1) then
6718 goto Next_E;
6719
6720 elsif Is_Type (E1) then
6721
6722 -- Subtypes must statically match. E1, E2 are the local entities
6723 -- that are subtypes of the actuals. Itypes generated for other
6724 -- parameters need not be checked, the check will be performed
6725 -- on the parameters themselves.
6726
6727 -- If E2 is a formal type declaration, it is a defaulted parameter
6728 -- and needs no checking.
6729
6730 if not Is_Itype (E1) and then not Is_Itype (E2) then
6731 Check_Mismatch
6732 (not Is_Type (E2)
6733 or else Etype (E1) /= Etype (E2)
6734 or else not Subtypes_Statically_Match (E1, E2));
6735 end if;
6736
6737 elsif Ekind (E1) = E_Constant then
6738
6739 -- IN parameters must denote the same static value, or the same
6740 -- constant, or the literal null.
6741
6742 Expr1 := Expression (Parent (E1));
6743
6744 if Ekind (E2) /= E_Constant then
6745 Check_Mismatch (True);
6746 goto Next_E;
6747 else
6748 Expr2 := Expression (Parent (E2));
6749 end if;
6750
6751 if Is_OK_Static_Expression (Expr1) then
6752 if not Is_OK_Static_Expression (Expr2) then
6753 Check_Mismatch (True);
6754
6755 elsif Is_Discrete_Type (Etype (E1)) then
6756 declare
6757 V1 : constant Uint := Expr_Value (Expr1);
6758 V2 : constant Uint := Expr_Value (Expr2);
6759 begin
6760 Check_Mismatch (V1 /= V2);
6761 end;
6762
6763 elsif Is_Real_Type (Etype (E1)) then
6764 declare
6765 V1 : constant Ureal := Expr_Value_R (Expr1);
6766 V2 : constant Ureal := Expr_Value_R (Expr2);
6767 begin
6768 Check_Mismatch (V1 /= V2);
6769 end;
6770
6771 elsif Is_String_Type (Etype (E1))
6772 and then Nkind (Expr1) = N_String_Literal
6773 then
6774 if Nkind (Expr2) /= N_String_Literal then
6775 Check_Mismatch (True);
6776 else
6777 Check_Mismatch
6778 (not String_Equal (Strval (Expr1), Strval (Expr2)));
6779 end if;
6780 end if;
6781
6782 elsif Is_Entity_Name (Expr1) then
6783 if Is_Entity_Name (Expr2) then
6784 if Entity (Expr1) = Entity (Expr2) then
6785 null;
6786 else
6787 Check_Mismatch
6788 (not Same_Instantiated_Constant
6789 (Entity (Expr1), Entity (Expr2)));
6790 end if;
6791
6792 else
6793 Check_Mismatch (True);
6794 end if;
6795
6796 elsif Is_Entity_Name (Original_Node (Expr1))
6797 and then Is_Entity_Name (Expr2)
6798 and then Same_Instantiated_Constant
6799 (Entity (Original_Node (Expr1)), Entity (Expr2))
6800 then
6801 null;
6802
6803 elsif Nkind (Expr1) = N_Null then
6804 Check_Mismatch (Nkind (Expr1) /= N_Null);
6805
6806 else
6807 Check_Mismatch (True);
6808 end if;
6809
6810 elsif Ekind (E1) = E_Variable then
6811 Check_Mismatch (not Same_Instantiated_Variable (E1, E2));
6812
6813 elsif Ekind (E1) = E_Package then
6814 Check_Mismatch
6815 (Ekind (E1) /= Ekind (E2)
6816 or else (Present (Renamed_Object (E2))
6817 and then Renamed_Object (E1) /=
6818 Renamed_Object (E2)));
6819
6820 elsif Is_Overloadable (E1) then
6821 -- Verify that the actual subprograms match. Note that actuals
6822 -- that are attributes are rewritten as subprograms. If the
6823 -- subprogram in the formal package is defaulted, no check is
6824 -- needed. Note that this can only happen in Ada 2005 when the
6825 -- formal package can be partially parameterized.
6826
6827 if Nkind (Unit_Declaration_Node (E1)) =
6828 N_Subprogram_Renaming_Declaration
6829 and then From_Default (Unit_Declaration_Node (E1))
6830 then
6831 null;
6832
6833 -- If the formal package has an "others" box association that
6834 -- covers this formal, there is no need for a check either.
6835
6836 elsif Nkind (Unit_Declaration_Node (E2)) in
6837 N_Formal_Subprogram_Declaration
6838 and then Box_Present (Unit_Declaration_Node (E2))
6839 then
6840 null;
6841
6842 -- No check needed if subprogram is a defaulted null procedure
6843
6844 elsif No (Alias (E2))
6845 and then Ekind (E2) = E_Procedure
6846 and then
6847 Null_Present (Specification (Unit_Declaration_Node (E2)))
6848 then
6849 null;
6850
6851 -- Otherwise the actual in the formal and the actual in the
6852 -- instantiation of the formal must match, up to renamings.
6853
6854 else
6855 Check_Mismatch
6856 (Ekind (E2) /= Ekind (E1)
6857 or else not Same_Instantiated_Function (E1, E2));
6858 end if;
6859
6860 else
6861 raise Program_Error;
6862 end if;
6863
6864 <<Next_E>>
6865 Prev_E1 := E1;
6866 Next_Entity (E1);
6867 Next_Entity (E2);
6868 end loop;
6869 end Check_Formal_Package_Instance;
6870
6871 ---------------------------
6872 -- Check_Formal_Packages --
6873 ---------------------------
6874
6875 procedure Check_Formal_Packages (P_Id : Entity_Id) is
6876 E : Entity_Id;
6877 Formal_P : Entity_Id;
6878 Formal_Decl : Node_Id;
6879 begin
6880 -- Iterate through the declarations in the instance, looking for package
6881 -- renaming declarations that denote instances of formal packages. Stop
6882 -- when we find the renaming of the current package itself. The
6883 -- declaration for a formal package without a box is followed by an
6884 -- internal entity that repeats the instantiation.
6885
6886 E := First_Entity (P_Id);
6887 while Present (E) loop
6888 if Ekind (E) = E_Package then
6889 if Renamed_Object (E) = P_Id then
6890 exit;
6891
6892 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
6893 null;
6894
6895 else
6896 Formal_Decl := Parent (Associated_Formal_Package (E));
6897
6898 -- Nothing to check if the formal has a box or an others_clause
6899 -- (necessarily with a box), or no associations altogether
6900
6901 if Box_Present (Formal_Decl)
6902 or else No (Generic_Associations (Formal_Decl))
6903 then
6904 null;
6905
6906 elsif Nkind (First (Generic_Associations (Formal_Decl))) =
6907 N_Others_Choice
6908 then
6909 -- The internal validating package was generated but formal
6910 -- and instance are known to be compatible.
6911
6912 Formal_P := Next_Entity (E);
6913 Remove (Unit_Declaration_Node (Formal_P));
6914
6915 else
6916 Formal_P := Next_Entity (E);
6917
6918 -- If the instance is within an enclosing instance body
6919 -- there is no need to verify the legality of current formal
6920 -- packages because they were legal in the generic body.
6921 -- This optimization may be applicable elsewhere, and it
6922 -- also removes spurious errors that may arise with
6923 -- on-the-fly inlining and confusion between private and
6924 -- full views.
6925
6926 if not In_Instance_Body then
6927 Check_Formal_Package_Instance (Formal_P, E);
6928 end if;
6929
6930 -- Restore the visibility of formals of the formal instance
6931 -- that are not defaulted, and are hidden within the current
6932 -- generic. These formals may be visible within an enclosing
6933 -- generic.
6934
6935 declare
6936 Elmt : Elmt_Id;
6937 begin
6938 Elmt := First_Elmt (Hidden_In_Formal_Instance (Formal_P));
6939 while Present (Elmt) loop
6940 Set_Is_Hidden (Node (Elmt), False);
6941 Next_Elmt (Elmt);
6942 end loop;
6943 end;
6944
6945 -- After checking, remove the internal validating package.
6946 -- It is only needed for semantic checks, and as it may
6947 -- contain generic formal declarations it should not reach
6948 -- gigi.
6949
6950 Remove (Unit_Declaration_Node (Formal_P));
6951 end if;
6952 end if;
6953 end if;
6954
6955 Next_Entity (E);
6956 end loop;
6957 end Check_Formal_Packages;
6958
6959 ---------------------------------
6960 -- Check_Forward_Instantiation --
6961 ---------------------------------
6962
6963 procedure Check_Forward_Instantiation (Decl : Node_Id) is
6964 S : Entity_Id;
6965 Gen_Comp : Entity_Id := Cunit_Entity (Get_Source_Unit (Decl));
6966
6967 begin
6968 -- The instantiation appears before the generic body if we are in the
6969 -- scope of the unit containing the generic, either in its spec or in
6970 -- the package body, and before the generic body.
6971
6972 if Ekind (Gen_Comp) = E_Package_Body then
6973 Gen_Comp := Spec_Entity (Gen_Comp);
6974 end if;
6975
6976 if In_Open_Scopes (Gen_Comp)
6977 and then No (Corresponding_Body (Decl))
6978 then
6979 S := Current_Scope;
6980
6981 while Present (S)
6982 and then not Is_Compilation_Unit (S)
6983 and then not Is_Child_Unit (S)
6984 loop
6985 if Ekind (S) = E_Package then
6986 Set_Has_Forward_Instantiation (S);
6987 end if;
6988
6989 S := Scope (S);
6990 end loop;
6991 end if;
6992 end Check_Forward_Instantiation;
6993
6994 ---------------------------
6995 -- Check_Generic_Actuals --
6996 ---------------------------
6997
6998 -- The visibility of the actuals may be different between the point of
6999 -- generic instantiation and the instantiation of the body.
7000
7001 procedure Check_Generic_Actuals
7002 (Instance : Entity_Id;
7003 Is_Formal_Box : Boolean)
7004 is
7005 E : Entity_Id;
7006 Astype : Entity_Id;
7007
7008 begin
7009 E := First_Entity (Instance);
7010 while Present (E) loop
7011 if Is_Type (E)
7012 and then Nkind (Parent (E)) = N_Subtype_Declaration
7013 and then Scope (Etype (E)) /= Instance
7014 and then Is_Entity_Name (Subtype_Indication (Parent (E)))
7015 then
7016 -- Restore the proper view of the actual from the information
7017 -- saved earlier by Instantiate_Type.
7018
7019 Check_Private_View (Subtype_Indication (Parent (E)));
7020
7021 -- If the actual is itself the formal of a parent instance,
7022 -- then also restore the proper view of its actual and so on.
7023 -- That's necessary for nested instantiations of the form
7024
7025 -- generic
7026 -- type Component is private;
7027 -- type Array_Type is array (Positive range <>) of Component;
7028 -- procedure Proc;
7029
7030 -- when the outermost actuals have inconsistent views, because
7031 -- the Component_Type of Array_Type of the inner instantiations
7032 -- is the actual of Component of the outermost one and not that
7033 -- of the corresponding inner instantiations.
7034
7035 Astype := Ancestor_Subtype (E);
7036 while Present (Astype)
7037 and then Nkind (Parent (Astype)) = N_Subtype_Declaration
7038 and then Present (Generic_Parent_Type (Parent (Astype)))
7039 and then Is_Entity_Name (Subtype_Indication (Parent (Astype)))
7040 loop
7041 Check_Private_View (Subtype_Indication (Parent (Astype)));
7042 Astype := Ancestor_Subtype (Astype);
7043 end loop;
7044
7045 Set_Is_Generic_Actual_Type (E);
7046
7047 if Is_Private_Type (E) and then Present (Full_View (E)) then
7048 Set_Is_Generic_Actual_Type (Full_View (E));
7049 end if;
7050
7051 Set_Is_Hidden (E, False);
7052 Set_Is_Potentially_Use_Visible (E, In_Use (Instance));
7053
7054 -- We constructed the generic actual type as a subtype of the
7055 -- supplied type. This means that it normally would not inherit
7056 -- subtype specific attributes of the actual, which is wrong for
7057 -- the generic case.
7058
7059 Astype := Ancestor_Subtype (E);
7060
7061 if No (Astype) then
7062
7063 -- This can happen when E is an itype that is the full view of
7064 -- a private type completed, e.g. with a constrained array. In
7065 -- that case, use the first subtype, which will carry size
7066 -- information. The base type itself is unconstrained and will
7067 -- not carry it.
7068
7069 Astype := First_Subtype (E);
7070 end if;
7071
7072 Set_Size_Info (E, (Astype));
7073 Set_RM_Size (E, RM_Size (Astype));
7074 Set_First_Rep_Item (E, First_Rep_Item (Astype));
7075
7076 if Is_Discrete_Or_Fixed_Point_Type (E) then
7077 Set_RM_Size (E, RM_Size (Astype));
7078 end if;
7079
7080 elsif Ekind (E) = E_Package then
7081
7082 -- If this is the renaming for the current instance, we're done.
7083 -- Otherwise it is a formal package. If the corresponding formal
7084 -- was declared with a box, the (instantiations of the) generic
7085 -- formal part are also visible. Otherwise, ignore the entity
7086 -- created to validate the actuals.
7087
7088 if Renamed_Object (E) = Instance then
7089 exit;
7090
7091 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
7092 null;
7093
7094 -- The visibility of a formal of an enclosing generic is already
7095 -- correct.
7096
7097 elsif Denotes_Formal_Package (E) then
7098 null;
7099
7100 elsif Present (Associated_Formal_Package (E))
7101 and then not Is_Generic_Formal (E)
7102 then
7103 if Box_Present (Parent (Associated_Formal_Package (E))) then
7104 Check_Generic_Actuals (Renamed_Object (E), True);
7105
7106 else
7107 Check_Generic_Actuals (Renamed_Object (E), False);
7108 end if;
7109
7110 Set_Is_Hidden (E, False);
7111 end if;
7112
7113 -- If this is a subprogram instance (in a wrapper package) the
7114 -- actual is fully visible.
7115
7116 elsif Is_Wrapper_Package (Instance) then
7117 Set_Is_Hidden (E, False);
7118
7119 -- If the formal package is declared with a box, or if the formal
7120 -- parameter is defaulted, it is visible in the body.
7121
7122 elsif Is_Formal_Box or else Is_Visible_Formal (E) then
7123 Set_Is_Hidden (E, False);
7124 end if;
7125
7126 if Ekind (E) = E_Constant then
7127
7128 -- If the type of the actual is a private type declared in the
7129 -- enclosing scope of the generic unit, the body of the generic
7130 -- sees the full view of the type (because it has to appear in
7131 -- the corresponding package body). If the type is private now,
7132 -- exchange views to restore the proper visiblity in the instance.
7133
7134 declare
7135 Typ : constant Entity_Id := Base_Type (Etype (E));
7136 -- The type of the actual
7137
7138 Gen_Id : Entity_Id;
7139 -- The generic unit
7140
7141 Parent_Scope : Entity_Id;
7142 -- The enclosing scope of the generic unit
7143
7144 begin
7145 if Is_Wrapper_Package (Instance) then
7146 Gen_Id :=
7147 Generic_Parent
7148 (Specification
7149 (Unit_Declaration_Node
7150 (Related_Instance (Instance))));
7151 else
7152 Gen_Id :=
7153 Generic_Parent (Package_Specification (Instance));
7154 end if;
7155
7156 Parent_Scope := Scope (Gen_Id);
7157
7158 -- The exchange is only needed if the generic is defined
7159 -- within a package which is not a common ancestor of the
7160 -- scope of the instance, and is not already in scope.
7161
7162 if Is_Private_Type (Typ)
7163 and then Scope (Typ) = Parent_Scope
7164 and then Scope (Instance) /= Parent_Scope
7165 and then Ekind (Parent_Scope) = E_Package
7166 and then not Is_Child_Unit (Gen_Id)
7167 then
7168 Switch_View (Typ);
7169
7170 -- If the type of the entity is a subtype, it may also have
7171 -- to be made visible, together with the base type of its
7172 -- full view, after exchange.
7173
7174 if Is_Private_Type (Etype (E)) then
7175 Switch_View (Etype (E));
7176 Switch_View (Base_Type (Etype (E)));
7177 end if;
7178 end if;
7179 end;
7180 end if;
7181
7182 Next_Entity (E);
7183 end loop;
7184 end Check_Generic_Actuals;
7185
7186 ------------------------------
7187 -- Check_Generic_Child_Unit --
7188 ------------------------------
7189
7190 procedure Check_Generic_Child_Unit
7191 (Gen_Id : Node_Id;
7192 Parent_Installed : in out Boolean)
7193 is
7194 Loc : constant Source_Ptr := Sloc (Gen_Id);
7195 Gen_Par : Entity_Id := Empty;
7196 E : Entity_Id;
7197 Inst_Par : Entity_Id;
7198 S : Node_Id;
7199
7200 function Find_Generic_Child
7201 (Scop : Entity_Id;
7202 Id : Node_Id) return Entity_Id;
7203 -- Search generic parent for possible child unit with the given name
7204
7205 function In_Enclosing_Instance return Boolean;
7206 -- Within an instance of the parent, the child unit may be denoted by
7207 -- a simple name, or an abbreviated expanded name. Examine enclosing
7208 -- scopes to locate a possible parent instantiation.
7209
7210 ------------------------
7211 -- Find_Generic_Child --
7212 ------------------------
7213
7214 function Find_Generic_Child
7215 (Scop : Entity_Id;
7216 Id : Node_Id) return Entity_Id
7217 is
7218 E : Entity_Id;
7219
7220 begin
7221 -- If entity of name is already set, instance has already been
7222 -- resolved, e.g. in an enclosing instantiation.
7223
7224 if Present (Entity (Id)) then
7225 if Scope (Entity (Id)) = Scop then
7226 return Entity (Id);
7227 else
7228 return Empty;
7229 end if;
7230
7231 else
7232 E := First_Entity (Scop);
7233 while Present (E) loop
7234 if Chars (E) = Chars (Id)
7235 and then Is_Child_Unit (E)
7236 then
7237 if Is_Child_Unit (E)
7238 and then not Is_Visible_Lib_Unit (E)
7239 then
7240 Error_Msg_NE
7241 ("generic child unit& is not visible", Gen_Id, E);
7242 end if;
7243
7244 Set_Entity (Id, E);
7245 return E;
7246 end if;
7247
7248 Next_Entity (E);
7249 end loop;
7250
7251 return Empty;
7252 end if;
7253 end Find_Generic_Child;
7254
7255 ---------------------------
7256 -- In_Enclosing_Instance --
7257 ---------------------------
7258
7259 function In_Enclosing_Instance return Boolean is
7260 Enclosing_Instance : Node_Id;
7261 Instance_Decl : Node_Id;
7262
7263 begin
7264 -- We do not inline any call that contains instantiations, except
7265 -- for instantiations of Unchecked_Conversion, so if we are within
7266 -- an inlined body the current instance does not require parents.
7267
7268 if In_Inlined_Body then
7269 pragma Assert (Chars (Gen_Id) = Name_Unchecked_Conversion);
7270 return False;
7271 end if;
7272
7273 -- Loop to check enclosing scopes
7274
7275 Enclosing_Instance := Current_Scope;
7276 while Present (Enclosing_Instance) loop
7277 Instance_Decl := Unit_Declaration_Node (Enclosing_Instance);
7278
7279 if Ekind (Enclosing_Instance) = E_Package
7280 and then Is_Generic_Instance (Enclosing_Instance)
7281 and then Present
7282 (Generic_Parent (Specification (Instance_Decl)))
7283 then
7284 -- Check whether the generic we are looking for is a child of
7285 -- this instance.
7286
7287 E := Find_Generic_Child
7288 (Generic_Parent (Specification (Instance_Decl)), Gen_Id);
7289 exit when Present (E);
7290
7291 else
7292 E := Empty;
7293 end if;
7294
7295 Enclosing_Instance := Scope (Enclosing_Instance);
7296 end loop;
7297
7298 if No (E) then
7299
7300 -- Not a child unit
7301
7302 Analyze (Gen_Id);
7303 return False;
7304
7305 else
7306 Rewrite (Gen_Id,
7307 Make_Expanded_Name (Loc,
7308 Chars => Chars (E),
7309 Prefix => New_Occurrence_Of (Enclosing_Instance, Loc),
7310 Selector_Name => New_Occurrence_Of (E, Loc)));
7311
7312 Set_Entity (Gen_Id, E);
7313 Set_Etype (Gen_Id, Etype (E));
7314 Parent_Installed := False; -- Already in scope.
7315 return True;
7316 end if;
7317 end In_Enclosing_Instance;
7318
7319 -- Start of processing for Check_Generic_Child_Unit
7320
7321 begin
7322 -- If the name of the generic is given by a selected component, it may
7323 -- be the name of a generic child unit, and the prefix is the name of an
7324 -- instance of the parent, in which case the child unit must be visible.
7325 -- If this instance is not in scope, it must be placed there and removed
7326 -- after instantiation, because what is being instantiated is not the
7327 -- original child, but the corresponding child present in the instance
7328 -- of the parent.
7329
7330 -- If the child is instantiated within the parent, it can be given by
7331 -- a simple name. In this case the instance is already in scope, but
7332 -- the child generic must be recovered from the generic parent as well.
7333
7334 if Nkind (Gen_Id) = N_Selected_Component then
7335 S := Selector_Name (Gen_Id);
7336 Analyze (Prefix (Gen_Id));
7337 Inst_Par := Entity (Prefix (Gen_Id));
7338
7339 if Ekind (Inst_Par) = E_Package
7340 and then Present (Renamed_Object (Inst_Par))
7341 then
7342 Inst_Par := Renamed_Object (Inst_Par);
7343 end if;
7344
7345 if Ekind (Inst_Par) = E_Package then
7346 if Nkind (Parent (Inst_Par)) = N_Package_Specification then
7347 Gen_Par := Generic_Parent (Parent (Inst_Par));
7348
7349 elsif Nkind (Parent (Inst_Par)) = N_Defining_Program_Unit_Name
7350 and then
7351 Nkind (Parent (Parent (Inst_Par))) = N_Package_Specification
7352 then
7353 Gen_Par := Generic_Parent (Parent (Parent (Inst_Par)));
7354 end if;
7355
7356 elsif Ekind (Inst_Par) = E_Generic_Package
7357 and then Nkind (Parent (Gen_Id)) = N_Formal_Package_Declaration
7358 then
7359 -- A formal package may be a real child package, and not the
7360 -- implicit instance within a parent. In this case the child is
7361 -- not visible and has to be retrieved explicitly as well.
7362
7363 Gen_Par := Inst_Par;
7364 end if;
7365
7366 if Present (Gen_Par) then
7367
7368 -- The prefix denotes an instantiation. The entity itself may be a
7369 -- nested generic, or a child unit.
7370
7371 E := Find_Generic_Child (Gen_Par, S);
7372
7373 if Present (E) then
7374 Change_Selected_Component_To_Expanded_Name (Gen_Id);
7375 Set_Entity (Gen_Id, E);
7376 Set_Etype (Gen_Id, Etype (E));
7377 Set_Entity (S, E);
7378 Set_Etype (S, Etype (E));
7379
7380 -- Indicate that this is a reference to the parent
7381
7382 if In_Extended_Main_Source_Unit (Gen_Id) then
7383 Set_Is_Instantiated (Inst_Par);
7384 end if;
7385
7386 -- A common mistake is to replicate the naming scheme of a
7387 -- hierarchy by instantiating a generic child directly, rather
7388 -- than the implicit child in a parent instance:
7389
7390 -- generic .. package Gpar is ..
7391 -- generic .. package Gpar.Child is ..
7392 -- package Par is new Gpar ();
7393
7394 -- with Gpar.Child;
7395 -- package Par.Child is new Gpar.Child ();
7396 -- rather than Par.Child
7397
7398 -- In this case the instantiation is within Par, which is an
7399 -- instance, but Gpar does not denote Par because we are not IN
7400 -- the instance of Gpar, so this is illegal. The test below
7401 -- recognizes this particular case.
7402
7403 if Is_Child_Unit (E)
7404 and then not Comes_From_Source (Entity (Prefix (Gen_Id)))
7405 and then (not In_Instance
7406 or else Nkind (Parent (Parent (Gen_Id))) =
7407 N_Compilation_Unit)
7408 then
7409 Error_Msg_N
7410 ("prefix of generic child unit must be instance of parent",
7411 Gen_Id);
7412 end if;
7413
7414 if not In_Open_Scopes (Inst_Par)
7415 and then Nkind (Parent (Gen_Id)) not in
7416 N_Generic_Renaming_Declaration
7417 then
7418 Install_Parent (Inst_Par);
7419 Parent_Installed := True;
7420
7421 elsif In_Open_Scopes (Inst_Par) then
7422
7423 -- If the parent is already installed, install the actuals
7424 -- for its formal packages. This is necessary when the child
7425 -- instance is a child of the parent instance: in this case,
7426 -- the parent is placed on the scope stack but the formal
7427 -- packages are not made visible.
7428
7429 Install_Formal_Packages (Inst_Par);
7430 end if;
7431
7432 else
7433 -- If the generic parent does not contain an entity that
7434 -- corresponds to the selector, the instance doesn't either.
7435 -- Analyzing the node will yield the appropriate error message.
7436 -- If the entity is not a child unit, then it is an inner
7437 -- generic in the parent.
7438
7439 Analyze (Gen_Id);
7440 end if;
7441
7442 else
7443 Analyze (Gen_Id);
7444
7445 if Is_Child_Unit (Entity (Gen_Id))
7446 and then
7447 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
7448 and then not In_Open_Scopes (Inst_Par)
7449 then
7450 Install_Parent (Inst_Par);
7451 Parent_Installed := True;
7452
7453 -- The generic unit may be the renaming of the implicit child
7454 -- present in an instance. In that case the parent instance is
7455 -- obtained from the name of the renamed entity.
7456
7457 elsif Ekind (Entity (Gen_Id)) = E_Generic_Package
7458 and then Present (Renamed_Entity (Entity (Gen_Id)))
7459 and then Is_Child_Unit (Renamed_Entity (Entity (Gen_Id)))
7460 then
7461 declare
7462 Renamed_Package : constant Node_Id :=
7463 Name (Parent (Entity (Gen_Id)));
7464 begin
7465 if Nkind (Renamed_Package) = N_Expanded_Name then
7466 Inst_Par := Entity (Prefix (Renamed_Package));
7467 Install_Parent (Inst_Par);
7468 Parent_Installed := True;
7469 end if;
7470 end;
7471 end if;
7472 end if;
7473
7474 elsif Nkind (Gen_Id) = N_Expanded_Name then
7475
7476 -- Entity already present, analyze prefix, whose meaning may be an
7477 -- instance in the current context. If it is an instance of a
7478 -- relative within another, the proper parent may still have to be
7479 -- installed, if they are not of the same generation.
7480
7481 Analyze (Prefix (Gen_Id));
7482
7483 -- Prevent cascaded errors
7484
7485 if Etype (Prefix (Gen_Id)) = Any_Type then
7486 return;
7487 end if;
7488
7489 -- In the unlikely case that a local declaration hides the name of
7490 -- the parent package, locate it on the homonym chain. If the context
7491 -- is an instance of the parent, the renaming entity is flagged as
7492 -- such.
7493
7494 Inst_Par := Entity (Prefix (Gen_Id));
7495 while Present (Inst_Par)
7496 and then not Is_Package_Or_Generic_Package (Inst_Par)
7497 loop
7498 Inst_Par := Homonym (Inst_Par);
7499 end loop;
7500
7501 pragma Assert (Present (Inst_Par));
7502 Set_Entity (Prefix (Gen_Id), Inst_Par);
7503
7504 if In_Enclosing_Instance then
7505 null;
7506
7507 elsif Present (Entity (Gen_Id))
7508 and then Is_Child_Unit (Entity (Gen_Id))
7509 and then not In_Open_Scopes (Inst_Par)
7510 then
7511 Install_Parent (Inst_Par);
7512 Parent_Installed := True;
7513 end if;
7514
7515 elsif In_Enclosing_Instance then
7516
7517 -- The child unit is found in some enclosing scope
7518
7519 null;
7520
7521 else
7522 Analyze (Gen_Id);
7523
7524 -- If this is the renaming of the implicit child in a parent
7525 -- instance, recover the parent name and install it.
7526
7527 if Is_Entity_Name (Gen_Id) then
7528 E := Entity (Gen_Id);
7529
7530 if Is_Generic_Unit (E)
7531 and then Nkind (Parent (E)) in N_Generic_Renaming_Declaration
7532 and then Is_Child_Unit (Renamed_Object (E))
7533 and then Is_Generic_Unit (Scope (Renamed_Object (E)))
7534 and then Nkind (Name (Parent (E))) = N_Expanded_Name
7535 then
7536 Rewrite (Gen_Id, New_Copy_Tree (Name (Parent (E))));
7537 Inst_Par := Entity (Prefix (Gen_Id));
7538
7539 if not In_Open_Scopes (Inst_Par) then
7540 Install_Parent (Inst_Par);
7541 Parent_Installed := True;
7542 end if;
7543
7544 -- If it is a child unit of a non-generic parent, it may be
7545 -- use-visible and given by a direct name. Install parent as
7546 -- for other cases.
7547
7548 elsif Is_Generic_Unit (E)
7549 and then Is_Child_Unit (E)
7550 and then
7551 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
7552 and then not Is_Generic_Unit (Scope (E))
7553 then
7554 if not In_Open_Scopes (Scope (E)) then
7555 Install_Parent (Scope (E));
7556 Parent_Installed := True;
7557 end if;
7558 end if;
7559 end if;
7560 end if;
7561 end Check_Generic_Child_Unit;
7562
7563 -----------------------------
7564 -- Check_Hidden_Child_Unit --
7565 -----------------------------
7566
7567 procedure Check_Hidden_Child_Unit
7568 (N : Node_Id;
7569 Gen_Unit : Entity_Id;
7570 Act_Decl_Id : Entity_Id)
7571 is
7572 Gen_Id : constant Node_Id := Name (N);
7573
7574 begin
7575 if Is_Child_Unit (Gen_Unit)
7576 and then Is_Child_Unit (Act_Decl_Id)
7577 and then Nkind (Gen_Id) = N_Expanded_Name
7578 and then Entity (Prefix (Gen_Id)) = Scope (Act_Decl_Id)
7579 and then Chars (Gen_Unit) = Chars (Act_Decl_Id)
7580 then
7581 Error_Msg_Node_2 := Scope (Act_Decl_Id);
7582 Error_Msg_NE
7583 ("generic unit & is implicitly declared in &",
7584 Defining_Unit_Name (N), Gen_Unit);
7585 Error_Msg_N ("\instance must have different name",
7586 Defining_Unit_Name (N));
7587 end if;
7588 end Check_Hidden_Child_Unit;
7589
7590 ------------------------
7591 -- Check_Private_View --
7592 ------------------------
7593
7594 procedure Check_Private_View (N : Node_Id) is
7595 T : constant Entity_Id := Etype (N);
7596 BT : Entity_Id;
7597
7598 begin
7599 -- Exchange views if the type was not private in the generic but is
7600 -- private at the point of instantiation. Do not exchange views if
7601 -- the scope of the type is in scope. This can happen if both generic
7602 -- and instance are sibling units, or if type is defined in a parent.
7603 -- In this case the visibility of the type will be correct for all
7604 -- semantic checks.
7605
7606 if Present (T) then
7607 BT := Base_Type (T);
7608
7609 if Is_Private_Type (T)
7610 and then not Has_Private_View (N)
7611 and then Present (Full_View (T))
7612 and then not In_Open_Scopes (Scope (T))
7613 then
7614 -- In the generic, the full declaration was visible
7615
7616 Switch_View (T);
7617
7618 elsif Has_Private_View (N)
7619 and then not Is_Private_Type (T)
7620 and then not Has_Been_Exchanged (T)
7621 and then (not In_Open_Scopes (Scope (T))
7622 or else Nkind (Parent (N)) = N_Subtype_Declaration)
7623 then
7624 -- In the generic, only the private declaration was visible
7625
7626 -- If the type appears in a subtype declaration, the subtype in
7627 -- instance must have a view compatible with that of its parent,
7628 -- which must be exchanged (see corresponding code in Restore_
7629 -- Private_Views) so we make an exception to the open scope rule.
7630
7631 Prepend_Elmt (T, Exchanged_Views);
7632 Exchange_Declarations (Etype (Get_Associated_Node (N)));
7633
7634 -- Finally, a non-private subtype may have a private base type, which
7635 -- must be exchanged for consistency. This can happen when a package
7636 -- body is instantiated, when the scope stack is empty but in fact
7637 -- the subtype and the base type are declared in an enclosing scope.
7638
7639 -- Note that in this case we introduce an inconsistency in the view
7640 -- set, because we switch the base type BT, but there could be some
7641 -- private dependent subtypes of BT which remain unswitched. Such
7642 -- subtypes might need to be switched at a later point (see specific
7643 -- provision for that case in Switch_View).
7644
7645 elsif not Is_Private_Type (T)
7646 and then not Has_Private_View (N)
7647 and then Is_Private_Type (BT)
7648 and then Present (Full_View (BT))
7649 and then not Is_Generic_Type (BT)
7650 and then not In_Open_Scopes (BT)
7651 then
7652 Prepend_Elmt (Full_View (BT), Exchanged_Views);
7653 Exchange_Declarations (BT);
7654 end if;
7655 end if;
7656 end Check_Private_View;
7657
7658 -----------------------------
7659 -- Check_Hidden_Primitives --
7660 -----------------------------
7661
7662 function Check_Hidden_Primitives (Assoc_List : List_Id) return Elist_Id is
7663 Actual : Node_Id;
7664 Gen_T : Entity_Id;
7665 Result : Elist_Id := No_Elist;
7666
7667 begin
7668 if No (Assoc_List) then
7669 return No_Elist;
7670 end if;
7671
7672 -- Traverse the list of associations between formals and actuals
7673 -- searching for renamings of tagged types
7674
7675 Actual := First (Assoc_List);
7676 while Present (Actual) loop
7677 if Nkind (Actual) = N_Subtype_Declaration then
7678 Gen_T := Generic_Parent_Type (Actual);
7679
7680 if Present (Gen_T) and then Is_Tagged_Type (Gen_T) then
7681
7682 -- Traverse the list of primitives of the actual types
7683 -- searching for hidden primitives that are visible in the
7684 -- corresponding generic formal; leave them visible and
7685 -- append them to Result to restore their decoration later.
7686
7687 Install_Hidden_Primitives
7688 (Prims_List => Result,
7689 Gen_T => Gen_T,
7690 Act_T => Entity (Subtype_Indication (Actual)));
7691 end if;
7692 end if;
7693
7694 Next (Actual);
7695 end loop;
7696
7697 return Result;
7698 end Check_Hidden_Primitives;
7699
7700 --------------------------
7701 -- Contains_Instance_Of --
7702 --------------------------
7703
7704 function Contains_Instance_Of
7705 (Inner : Entity_Id;
7706 Outer : Entity_Id;
7707 N : Node_Id) return Boolean
7708 is
7709 Elmt : Elmt_Id;
7710 Scop : Entity_Id;
7711
7712 begin
7713 Scop := Outer;
7714
7715 -- Verify that there are no circular instantiations. We check whether
7716 -- the unit contains an instance of the current scope or some enclosing
7717 -- scope (in case one of the instances appears in a subunit). Longer
7718 -- circularities involving subunits might seem too pathological to
7719 -- consider, but they were not too pathological for the authors of
7720 -- DEC bc30vsq, so we loop over all enclosing scopes, and mark all
7721 -- enclosing generic scopes as containing an instance.
7722
7723 loop
7724 -- Within a generic subprogram body, the scope is not generic, to
7725 -- allow for recursive subprograms. Use the declaration to determine
7726 -- whether this is a generic unit.
7727
7728 if Ekind (Scop) = E_Generic_Package
7729 or else (Is_Subprogram (Scop)
7730 and then Nkind (Unit_Declaration_Node (Scop)) =
7731 N_Generic_Subprogram_Declaration)
7732 then
7733 Elmt := First_Elmt (Inner_Instances (Inner));
7734
7735 while Present (Elmt) loop
7736 if Node (Elmt) = Scop then
7737 Error_Msg_Node_2 := Inner;
7738 Error_Msg_NE
7739 ("circular Instantiation: & instantiated within &!",
7740 N, Scop);
7741 return True;
7742
7743 elsif Node (Elmt) = Inner then
7744 return True;
7745
7746 elsif Contains_Instance_Of (Node (Elmt), Scop, N) then
7747 Error_Msg_Node_2 := Inner;
7748 Error_Msg_NE
7749 ("circular Instantiation: & instantiated within &!",
7750 N, Node (Elmt));
7751 return True;
7752 end if;
7753
7754 Next_Elmt (Elmt);
7755 end loop;
7756
7757 -- Indicate that Inner is being instantiated within Scop
7758
7759 Append_Elmt (Inner, Inner_Instances (Scop));
7760 end if;
7761
7762 if Scop = Standard_Standard then
7763 exit;
7764 else
7765 Scop := Scope (Scop);
7766 end if;
7767 end loop;
7768
7769 return False;
7770 end Contains_Instance_Of;
7771
7772 -----------------------
7773 -- Copy_Generic_Node --
7774 -----------------------
7775
7776 function Copy_Generic_Node
7777 (N : Node_Id;
7778 Parent_Id : Node_Id;
7779 Instantiating : Boolean) return Node_Id
7780 is
7781 Ent : Entity_Id;
7782 New_N : Node_Id;
7783
7784 function Copy_Generic_Descendant (D : Union_Id) return Union_Id;
7785 -- Check the given value of one of the Fields referenced by the current
7786 -- node to determine whether to copy it recursively. The field may hold
7787 -- a Node_Id, a List_Id, or an Elist_Id, or a plain value (Sloc, Uint,
7788 -- Char) in which case it need not be copied.
7789
7790 procedure Copy_Descendants;
7791 -- Common utility for various nodes
7792
7793 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id;
7794 -- Make copy of element list
7795
7796 function Copy_Generic_List
7797 (L : List_Id;
7798 Parent_Id : Node_Id) return List_Id;
7799 -- Apply Copy_Node recursively to the members of a node list
7800
7801 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean;
7802 -- True if an identifier is part of the defining program unit name of
7803 -- a child unit.
7804 -- Consider removing this subprogram now that ASIS no longer uses it.
7805
7806 ----------------------
7807 -- Copy_Descendants --
7808 ----------------------
7809
7810 procedure Copy_Descendants is
7811 use Atree.Unchecked_Access;
7812 -- This code section is part of the implementation of an untyped
7813 -- tree traversal, so it needs direct access to node fields.
7814
7815 begin
7816 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
7817 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
7818 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
7819 Set_Field4 (New_N, Copy_Generic_Descendant (Field4 (N)));
7820 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
7821 end Copy_Descendants;
7822
7823 -----------------------------
7824 -- Copy_Generic_Descendant --
7825 -----------------------------
7826
7827 function Copy_Generic_Descendant (D : Union_Id) return Union_Id is
7828 begin
7829 if D = Union_Id (Empty) then
7830 return D;
7831
7832 elsif D in Node_Range then
7833 return Union_Id
7834 (Copy_Generic_Node (Node_Id (D), New_N, Instantiating));
7835
7836 elsif D in List_Range then
7837 return Union_Id (Copy_Generic_List (List_Id (D), New_N));
7838
7839 elsif D in Elist_Range then
7840 return Union_Id (Copy_Generic_Elist (Elist_Id (D)));
7841
7842 -- Nothing else is copyable (e.g. Uint values), return as is
7843
7844 else
7845 return D;
7846 end if;
7847 end Copy_Generic_Descendant;
7848
7849 ------------------------
7850 -- Copy_Generic_Elist --
7851 ------------------------
7852
7853 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id is
7854 M : Elmt_Id;
7855 L : Elist_Id;
7856
7857 begin
7858 if Present (E) then
7859 L := New_Elmt_List;
7860 M := First_Elmt (E);
7861 while Present (M) loop
7862 Append_Elmt
7863 (Copy_Generic_Node (Node (M), Empty, Instantiating), L);
7864 Next_Elmt (M);
7865 end loop;
7866
7867 return L;
7868
7869 else
7870 return No_Elist;
7871 end if;
7872 end Copy_Generic_Elist;
7873
7874 -----------------------
7875 -- Copy_Generic_List --
7876 -----------------------
7877
7878 function Copy_Generic_List
7879 (L : List_Id;
7880 Parent_Id : Node_Id) return List_Id
7881 is
7882 N : Node_Id;
7883 New_L : List_Id;
7884
7885 begin
7886 if Present (L) then
7887 New_L := New_List;
7888 Set_Parent (New_L, Parent_Id);
7889
7890 N := First (L);
7891 while Present (N) loop
7892 Append (Copy_Generic_Node (N, Empty, Instantiating), New_L);
7893 Next (N);
7894 end loop;
7895
7896 return New_L;
7897
7898 else
7899 return No_List;
7900 end if;
7901 end Copy_Generic_List;
7902
7903 ---------------------------
7904 -- In_Defining_Unit_Name --
7905 ---------------------------
7906
7907 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean is
7908 begin
7909 return
7910 Present (Parent (Nam))
7911 and then (Nkind (Parent (Nam)) = N_Defining_Program_Unit_Name
7912 or else
7913 (Nkind (Parent (Nam)) = N_Expanded_Name
7914 and then In_Defining_Unit_Name (Parent (Nam))));
7915 end In_Defining_Unit_Name;
7916
7917 -- Start of processing for Copy_Generic_Node
7918
7919 begin
7920 if N = Empty then
7921 return N;
7922 end if;
7923
7924 New_N := New_Copy (N);
7925
7926 -- Copy aspects if present
7927
7928 if Has_Aspects (N) then
7929 Set_Has_Aspects (New_N, False);
7930 Set_Aspect_Specifications
7931 (New_N, Copy_Generic_List (Aspect_Specifications (N), Parent_Id));
7932 end if;
7933
7934 -- If we are instantiating, we want to adjust the sloc based on the
7935 -- current S_Adjustment. However, if this is the root node of a subunit,
7936 -- we need to defer that adjustment to below (see "elsif Instantiating
7937 -- and Was_Stub"), so it comes after Create_Instantiation_Source has
7938 -- computed the adjustment.
7939
7940 if Instantiating
7941 and then not (Nkind (N) in N_Proper_Body
7942 and then Was_Originally_Stub (N))
7943 then
7944 Adjust_Instantiation_Sloc (New_N, S_Adjustment);
7945 end if;
7946
7947 if not Is_List_Member (N) then
7948 Set_Parent (New_N, Parent_Id);
7949 end if;
7950
7951 -- Special casing for identifiers and other entity names and operators
7952
7953 if Nkind_In (New_N, N_Character_Literal,
7954 N_Expanded_Name,
7955 N_Identifier,
7956 N_Operator_Symbol)
7957 or else Nkind (New_N) in N_Op
7958 then
7959 if not Instantiating then
7960
7961 -- Link both nodes in order to assign subsequently the entity of
7962 -- the copy to the original node, in case this is a global
7963 -- reference.
7964
7965 Set_Associated_Node (N, New_N);
7966
7967 -- If we are within an instantiation, this is a nested generic
7968 -- that has already been analyzed at the point of definition.
7969 -- We must preserve references that were global to the enclosing
7970 -- parent at that point. Other occurrences, whether global or
7971 -- local to the current generic, must be resolved anew, so we
7972 -- reset the entity in the generic copy. A global reference has a
7973 -- smaller depth than the parent, or else the same depth in case
7974 -- both are distinct compilation units.
7975
7976 -- A child unit is implicitly declared within the enclosing parent
7977 -- but is in fact global to it, and must be preserved.
7978
7979 -- It is also possible for Current_Instantiated_Parent to be
7980 -- defined, and for this not to be a nested generic, namely if
7981 -- the unit is loaded through Rtsfind. In that case, the entity of
7982 -- New_N is only a link to the associated node, and not a defining
7983 -- occurrence.
7984
7985 -- The entities for parent units in the defining_program_unit of a
7986 -- generic child unit are established when the context of the unit
7987 -- is first analyzed, before the generic copy is made. They are
7988 -- preserved in the copy for use in e.g. ASIS queries.
7989
7990 Ent := Entity (New_N);
7991
7992 if No (Current_Instantiated_Parent.Gen_Id) then
7993 if No (Ent)
7994 or else Nkind (Ent) /= N_Defining_Identifier
7995 or else not In_Defining_Unit_Name (N)
7996 then
7997 Set_Associated_Node (New_N, Empty);
7998 end if;
7999
8000 elsif No (Ent)
8001 or else
8002 not Nkind_In (Ent, N_Defining_Identifier,
8003 N_Defining_Character_Literal,
8004 N_Defining_Operator_Symbol)
8005 or else No (Scope (Ent))
8006 or else
8007 (Scope (Ent) = Current_Instantiated_Parent.Gen_Id
8008 and then not Is_Child_Unit (Ent))
8009 or else
8010 (Scope_Depth (Scope (Ent)) >
8011 Scope_Depth (Current_Instantiated_Parent.Gen_Id)
8012 and then
8013 Get_Source_Unit (Ent) =
8014 Get_Source_Unit (Current_Instantiated_Parent.Gen_Id))
8015 then
8016 Set_Associated_Node (New_N, Empty);
8017 end if;
8018
8019 -- Case of instantiating identifier or some other name or operator
8020
8021 else
8022 -- If the associated node is still defined, the entity in it
8023 -- is global, and must be copied to the instance. If this copy
8024 -- is being made for a body to inline, it is applied to an
8025 -- instantiated tree, and the entity is already present and
8026 -- must be also preserved.
8027
8028 declare
8029 Assoc : constant Node_Id := Get_Associated_Node (N);
8030
8031 begin
8032 if Present (Assoc) then
8033 if Nkind (Assoc) = Nkind (N) then
8034 Set_Entity (New_N, Entity (Assoc));
8035 Check_Private_View (N);
8036
8037 -- Here we deal with a very peculiar case for which the
8038 -- Has_Private_View mechanism is not sufficient, because
8039 -- the reference to the type is implicit in the tree,
8040 -- that is to say, it's not referenced from a node but
8041 -- only from another type, namely through Component_Type.
8042
8043 -- package P is
8044
8045 -- type Pt is private;
8046
8047 -- generic
8048 -- type Ft is array (Positive range <>) of Pt;
8049 -- package G is
8050 -- procedure Check (F1, F2 : Ft; Lt : Boolean);
8051 -- end G;
8052
8053 -- private
8054 -- type Pt is new Boolean;
8055 -- end P;
8056
8057 -- package body P is
8058 -- package body G is
8059 -- procedure Check (F1, F2 : Ft; Lt : Boolean) is
8060 -- begin
8061 -- if (F1 < F2) /= Lt then
8062 -- null;
8063 -- end if;
8064 -- end Check;
8065 -- end G;
8066 -- end P;
8067
8068 -- type Arr is array (Positive range <>) of P.Pt;
8069
8070 -- package Inst is new P.G (Arr);
8071
8072 -- Pt is a global type for the generic package G and it
8073 -- is not referenced in its body, but only as component
8074 -- type of Ft, which is a local type. This means that no
8075 -- references to Pt or Ft are seen during the copy of the
8076 -- body, the only reference to Pt being seen is when the
8077 -- actuals are checked by Check_Generic_Actuals, but Pt
8078 -- is still private at this point. In the end, the views
8079 -- of Pt are not switched in the body and, therefore, the
8080 -- array comparison is rejected because the component is
8081 -- still private.
8082
8083 -- Adding e.g. a dummy variable of type Pt in the body is
8084 -- sufficient to make everything work, so we generate an
8085 -- artificial reference to Pt on the fly and thus force
8086 -- the switching of views on the grounds that, if the
8087 -- comparison was accepted during the semantic analysis
8088 -- of the generic, this means that the component cannot
8089 -- have been private (see Sem_Type.Valid_Comparison_Arg).
8090
8091 if Nkind (Assoc) in N_Op_Compare
8092 and then Present (Etype (Left_Opnd (Assoc)))
8093 and then Is_Array_Type (Etype (Left_Opnd (Assoc)))
8094 and then Present (Etype (Right_Opnd (Assoc)))
8095 and then Is_Array_Type (Etype (Right_Opnd (Assoc)))
8096 then
8097 declare
8098 Ltyp : constant Entity_Id :=
8099 Etype (Left_Opnd (Assoc));
8100 Rtyp : constant Entity_Id :=
8101 Etype (Right_Opnd (Assoc));
8102 begin
8103 if Is_Private_Type (Component_Type (Ltyp)) then
8104 Check_Private_View
8105 (New_Occurrence_Of (Component_Type (Ltyp),
8106 Sloc (N)));
8107 end if;
8108 if Is_Private_Type (Component_Type (Rtyp)) then
8109 Check_Private_View
8110 (New_Occurrence_Of (Component_Type (Rtyp),
8111 Sloc (N)));
8112 end if;
8113 end;
8114
8115 -- Here is a similar case, for the Designated_Type of an
8116 -- access type that is present as target type in a type
8117 -- conversion from another access type. In this case, if
8118 -- the base types of the designated types are different
8119 -- and the conversion was accepted during the semantic
8120 -- analysis of the generic, this means that the target
8121 -- type cannot have been private (see Valid_Conversion).
8122
8123 elsif Nkind (Assoc) = N_Identifier
8124 and then Nkind (Parent (Assoc)) = N_Type_Conversion
8125 and then Subtype_Mark (Parent (Assoc)) = Assoc
8126 and then Present (Etype (Assoc))
8127 and then Is_Access_Type (Etype (Assoc))
8128 and then Present (Etype (Expression (Parent (Assoc))))
8129 and then
8130 Is_Access_Type (Etype (Expression (Parent (Assoc))))
8131 then
8132 declare
8133 Targ_Desig : constant Entity_Id :=
8134 Designated_Type (Etype (Assoc));
8135 Expr_Desig : constant Entity_Id :=
8136 Designated_Type
8137 (Etype (Expression (Parent (Assoc))));
8138 begin
8139 if Base_Type (Targ_Desig) /= Base_Type (Expr_Desig)
8140 and then Is_Private_Type (Targ_Desig)
8141 then
8142 Check_Private_View
8143 (New_Occurrence_Of (Targ_Desig, Sloc (N)));
8144 end if;
8145 end;
8146 end if;
8147
8148 -- The node is a reference to a global type and acts as the
8149 -- subtype mark of a qualified expression created in order
8150 -- to aid resolution of accidental overloading in instances.
8151 -- Since N is a reference to a type, the Associated_Node of
8152 -- N denotes an entity rather than another identifier. See
8153 -- Qualify_Universal_Operands for details.
8154
8155 elsif Nkind (N) = N_Identifier
8156 and then Nkind (Parent (N)) = N_Qualified_Expression
8157 and then Subtype_Mark (Parent (N)) = N
8158 and then Is_Qualified_Universal_Literal (Parent (N))
8159 then
8160 Set_Entity (New_N, Assoc);
8161
8162 -- The name in the call may be a selected component if the
8163 -- call has not been analyzed yet, as may be the case for
8164 -- pre/post conditions in a generic unit.
8165
8166 elsif Nkind (Assoc) = N_Function_Call
8167 and then Is_Entity_Name (Name (Assoc))
8168 then
8169 Set_Entity (New_N, Entity (Name (Assoc)));
8170
8171 elsif Nkind_In (Assoc, N_Defining_Identifier,
8172 N_Defining_Character_Literal,
8173 N_Defining_Operator_Symbol)
8174 and then Expander_Active
8175 then
8176 -- Inlining case: we are copying a tree that contains
8177 -- global entities, which are preserved in the copy to be
8178 -- used for subsequent inlining.
8179
8180 null;
8181
8182 else
8183 Set_Entity (New_N, Empty);
8184 end if;
8185 end if;
8186 end;
8187 end if;
8188
8189 -- For expanded name, we must copy the Prefix and Selector_Name
8190
8191 if Nkind (N) = N_Expanded_Name then
8192 Set_Prefix
8193 (New_N, Copy_Generic_Node (Prefix (N), New_N, Instantiating));
8194
8195 Set_Selector_Name (New_N,
8196 Copy_Generic_Node (Selector_Name (N), New_N, Instantiating));
8197
8198 -- For operators, copy the operands
8199
8200 elsif Nkind (N) in N_Op then
8201 if Nkind (N) in N_Binary_Op then
8202 Set_Left_Opnd (New_N,
8203 Copy_Generic_Node (Left_Opnd (N), New_N, Instantiating));
8204 end if;
8205
8206 Set_Right_Opnd (New_N,
8207 Copy_Generic_Node (Right_Opnd (N), New_N, Instantiating));
8208 end if;
8209
8210 -- Establish a link between an entity from the generic template and the
8211 -- corresponding entity in the generic copy to be analyzed.
8212
8213 elsif Nkind (N) in N_Entity then
8214 if not Instantiating then
8215 Set_Associated_Entity (N, New_N);
8216 end if;
8217
8218 -- Clear any existing link the copy may inherit from the replicated
8219 -- generic template entity.
8220
8221 Set_Associated_Entity (New_N, Empty);
8222
8223 -- Special casing for stubs
8224
8225 elsif Nkind (N) in N_Body_Stub then
8226
8227 -- In any case, we must copy the specification or defining
8228 -- identifier as appropriate.
8229
8230 if Nkind (N) = N_Subprogram_Body_Stub then
8231 Set_Specification (New_N,
8232 Copy_Generic_Node (Specification (N), New_N, Instantiating));
8233
8234 else
8235 Set_Defining_Identifier (New_N,
8236 Copy_Generic_Node
8237 (Defining_Identifier (N), New_N, Instantiating));
8238 end if;
8239
8240 -- If we are not instantiating, then this is where we load and
8241 -- analyze subunits, i.e. at the point where the stub occurs. A
8242 -- more permissive system might defer this analysis to the point
8243 -- of instantiation, but this seems too complicated for now.
8244
8245 if not Instantiating then
8246 declare
8247 Subunit_Name : constant Unit_Name_Type := Get_Unit_Name (N);
8248 Subunit : Node_Id;
8249 Unum : Unit_Number_Type;
8250 New_Body : Node_Id;
8251
8252 begin
8253 -- Make sure that, if it is a subunit of the main unit that is
8254 -- preprocessed and if -gnateG is specified, the preprocessed
8255 -- file will be written.
8256
8257 Lib.Analysing_Subunit_Of_Main :=
8258 Lib.In_Extended_Main_Source_Unit (N);
8259 Unum :=
8260 Load_Unit
8261 (Load_Name => Subunit_Name,
8262 Required => False,
8263 Subunit => True,
8264 Error_Node => N);
8265 Lib.Analysing_Subunit_Of_Main := False;
8266
8267 -- If the proper body is not found, a warning message will be
8268 -- emitted when analyzing the stub, or later at the point of
8269 -- instantiation. Here we just leave the stub as is.
8270
8271 if Unum = No_Unit then
8272 Subunits_Missing := True;
8273 goto Subunit_Not_Found;
8274 end if;
8275
8276 Subunit := Cunit (Unum);
8277
8278 if Nkind (Unit (Subunit)) /= N_Subunit then
8279 Error_Msg_N
8280 ("found child unit instead of expected SEPARATE subunit",
8281 Subunit);
8282 Error_Msg_Sloc := Sloc (N);
8283 Error_Msg_N ("\to complete stub #", Subunit);
8284 goto Subunit_Not_Found;
8285 end if;
8286
8287 -- We must create a generic copy of the subunit, in order to
8288 -- perform semantic analysis on it, and we must replace the
8289 -- stub in the original generic unit with the subunit, in order
8290 -- to preserve non-local references within.
8291
8292 -- Only the proper body needs to be copied. Library_Unit and
8293 -- context clause are simply inherited by the generic copy.
8294 -- Note that the copy (which may be recursive if there are
8295 -- nested subunits) must be done first, before attaching it to
8296 -- the enclosing generic.
8297
8298 New_Body :=
8299 Copy_Generic_Node
8300 (Proper_Body (Unit (Subunit)),
8301 Empty, Instantiating => False);
8302
8303 -- Now place the original proper body in the original generic
8304 -- unit. This is a body, not a compilation unit.
8305
8306 Rewrite (N, Proper_Body (Unit (Subunit)));
8307 Set_Is_Compilation_Unit (Defining_Entity (N), False);
8308 Set_Was_Originally_Stub (N);
8309
8310 -- Finally replace the body of the subunit with its copy, and
8311 -- make this new subunit into the library unit of the generic
8312 -- copy, which does not have stubs any longer.
8313
8314 Set_Proper_Body (Unit (Subunit), New_Body);
8315 Set_Library_Unit (New_N, Subunit);
8316 Inherit_Context (Unit (Subunit), N);
8317 end;
8318
8319 -- If we are instantiating, this must be an error case, since
8320 -- otherwise we would have replaced the stub node by the proper body
8321 -- that corresponds. So just ignore it in the copy (i.e. we have
8322 -- copied it, and that is good enough).
8323
8324 else
8325 null;
8326 end if;
8327
8328 <<Subunit_Not_Found>> null;
8329
8330 -- If the node is a compilation unit, it is the subunit of a stub, which
8331 -- has been loaded already (see code below). In this case, the library
8332 -- unit field of N points to the parent unit (which is a compilation
8333 -- unit) and need not (and cannot) be copied.
8334
8335 -- When the proper body of the stub is analyzed, the library_unit link
8336 -- is used to establish the proper context (see sem_ch10).
8337
8338 -- The other fields of a compilation unit are copied as usual
8339
8340 elsif Nkind (N) = N_Compilation_Unit then
8341
8342 -- This code can only be executed when not instantiating, because in
8343 -- the copy made for an instantiation, the compilation unit node has
8344 -- disappeared at the point that a stub is replaced by its proper
8345 -- body.
8346
8347 pragma Assert (not Instantiating);
8348
8349 Set_Context_Items (New_N,
8350 Copy_Generic_List (Context_Items (N), New_N));
8351
8352 Set_Unit (New_N,
8353 Copy_Generic_Node (Unit (N), New_N, Instantiating => False));
8354
8355 Set_First_Inlined_Subprogram (New_N,
8356 Copy_Generic_Node
8357 (First_Inlined_Subprogram (N), New_N, Instantiating => False));
8358
8359 Set_Aux_Decls_Node
8360 (New_N,
8361 Copy_Generic_Node
8362 (Aux_Decls_Node (N), New_N, Instantiating => False));
8363
8364 -- For an assignment node, the assignment is known to be semantically
8365 -- legal if we are instantiating the template. This avoids incorrect
8366 -- diagnostics in generated code.
8367
8368 elsif Nkind (N) = N_Assignment_Statement then
8369
8370 -- Copy name and expression fields in usual manner
8371
8372 Set_Name (New_N,
8373 Copy_Generic_Node (Name (N), New_N, Instantiating));
8374
8375 Set_Expression (New_N,
8376 Copy_Generic_Node (Expression (N), New_N, Instantiating));
8377
8378 if Instantiating then
8379 Set_Assignment_OK (Name (New_N), True);
8380 end if;
8381
8382 elsif Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
8383 if not Instantiating then
8384 Set_Associated_Node (N, New_N);
8385
8386 else
8387 if Present (Get_Associated_Node (N))
8388 and then Nkind (Get_Associated_Node (N)) = Nkind (N)
8389 then
8390 -- In the generic the aggregate has some composite type. If at
8391 -- the point of instantiation the type has a private view,
8392 -- install the full view (and that of its ancestors, if any).
8393
8394 declare
8395 T : Entity_Id := (Etype (Get_Associated_Node (New_N)));
8396 Rt : Entity_Id;
8397
8398 begin
8399 if Present (T) and then Is_Private_Type (T) then
8400 Switch_View (T);
8401 end if;
8402
8403 if Present (T)
8404 and then Is_Tagged_Type (T)
8405 and then Is_Derived_Type (T)
8406 then
8407 Rt := Root_Type (T);
8408
8409 loop
8410 T := Etype (T);
8411
8412 if Is_Private_Type (T) then
8413 Switch_View (T);
8414 end if;
8415
8416 exit when T = Rt;
8417 end loop;
8418 end if;
8419 end;
8420 end if;
8421 end if;
8422
8423 -- Do not copy the associated node, which points to the generic copy
8424 -- of the aggregate.
8425
8426 declare
8427 use Atree.Unchecked_Access;
8428 -- This code section is part of the implementation of an untyped
8429 -- tree traversal, so it needs direct access to node fields.
8430
8431 begin
8432 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
8433 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
8434 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
8435 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
8436 end;
8437
8438 -- Allocators do not have an identifier denoting the access type, so we
8439 -- must locate it through the expression to check whether the views are
8440 -- consistent.
8441
8442 elsif Nkind (N) = N_Allocator
8443 and then Nkind (Expression (N)) = N_Qualified_Expression
8444 and then Is_Entity_Name (Subtype_Mark (Expression (N)))
8445 and then Instantiating
8446 then
8447 declare
8448 T : constant Node_Id :=
8449 Get_Associated_Node (Subtype_Mark (Expression (N)));
8450 Acc_T : Entity_Id;
8451
8452 begin
8453 if Present (T) then
8454
8455 -- Retrieve the allocator node in the generic copy
8456
8457 Acc_T := Etype (Parent (Parent (T)));
8458
8459 if Present (Acc_T) and then Is_Private_Type (Acc_T) then
8460 Switch_View (Acc_T);
8461 end if;
8462 end if;
8463
8464 Copy_Descendants;
8465 end;
8466
8467 -- For a proper body, we must catch the case of a proper body that
8468 -- replaces a stub. This represents the point at which a separate
8469 -- compilation unit, and hence template file, may be referenced, so we
8470 -- must make a new source instantiation entry for the template of the
8471 -- subunit, and ensure that all nodes in the subunit are adjusted using
8472 -- this new source instantiation entry.
8473
8474 elsif Nkind (N) in N_Proper_Body then
8475 declare
8476 Save_Adjustment : constant Sloc_Adjustment := S_Adjustment;
8477 begin
8478 if Instantiating and then Was_Originally_Stub (N) then
8479 Create_Instantiation_Source
8480 (Instantiation_Node,
8481 Defining_Entity (N),
8482 S_Adjustment);
8483
8484 Adjust_Instantiation_Sloc (New_N, S_Adjustment);
8485 end if;
8486
8487 -- Now copy the fields of the proper body, using the new
8488 -- adjustment factor if one was needed as per test above.
8489
8490 Copy_Descendants;
8491
8492 -- Restore the original adjustment factor
8493
8494 S_Adjustment := Save_Adjustment;
8495 end;
8496
8497 elsif Nkind (N) = N_Pragma and then Instantiating then
8498
8499 -- Do not copy Comment or Ident pragmas their content is relevant to
8500 -- the generic unit, not to the instantiating unit.
8501
8502 if Nam_In (Pragma_Name_Unmapped (N), Name_Comment, Name_Ident) then
8503 New_N := Make_Null_Statement (Sloc (N));
8504
8505 -- Do not copy pragmas generated from aspects because the pragmas do
8506 -- not carry any semantic information, plus they will be regenerated
8507 -- in the instance.
8508
8509 -- However, generating C we need to copy them since postconditions
8510 -- are inlined by the front end, and the front-end inlining machinery
8511 -- relies on this routine to perform inlining.
8512
8513 elsif From_Aspect_Specification (N)
8514 and then not Modify_Tree_For_C
8515 then
8516 New_N := Make_Null_Statement (Sloc (N));
8517
8518 else
8519 Copy_Descendants;
8520 end if;
8521
8522 elsif Nkind_In (N, N_Integer_Literal, N_Real_Literal) then
8523
8524 -- No descendant fields need traversing
8525
8526 null;
8527
8528 elsif Nkind (N) = N_String_Literal
8529 and then Present (Etype (N))
8530 and then Instantiating
8531 then
8532 -- If the string is declared in an outer scope, the string_literal
8533 -- subtype created for it may have the wrong scope. Force reanalysis
8534 -- of the constant to generate a new itype in the proper context.
8535
8536 Set_Etype (New_N, Empty);
8537 Set_Analyzed (New_N, False);
8538
8539 -- For the remaining nodes, copy their descendants recursively
8540
8541 else
8542 Copy_Descendants;
8543
8544 if Instantiating and then Nkind (N) = N_Subprogram_Body then
8545 Set_Generic_Parent (Specification (New_N), N);
8546
8547 -- Should preserve Corresponding_Spec??? (12.3(14))
8548 end if;
8549 end if;
8550
8551 -- Propagate dimensions if present, so that they are reflected in the
8552 -- instance.
8553
8554 if Nkind (N) in N_Has_Etype
8555 and then (Nkind (N) in N_Op or else Is_Entity_Name (N))
8556 and then Present (Etype (N))
8557 and then Is_Floating_Point_Type (Etype (N))
8558 and then Has_Dimension_System (Etype (N))
8559 then
8560 Copy_Dimensions (N, New_N);
8561 end if;
8562
8563 return New_N;
8564 end Copy_Generic_Node;
8565
8566 ----------------------------
8567 -- Denotes_Formal_Package --
8568 ----------------------------
8569
8570 function Denotes_Formal_Package
8571 (Pack : Entity_Id;
8572 On_Exit : Boolean := False;
8573 Instance : Entity_Id := Empty) return Boolean
8574 is
8575 Par : Entity_Id;
8576 Scop : constant Entity_Id := Scope (Pack);
8577 E : Entity_Id;
8578
8579 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean;
8580 -- The package in question may be an actual for a previous formal
8581 -- package P of the current instance, so examine its actuals as well.
8582 -- This must be recursive over other formal packages.
8583
8584 ----------------------------------
8585 -- Is_Actual_Of_Previous_Formal --
8586 ----------------------------------
8587
8588 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean is
8589 E1 : Entity_Id;
8590
8591 begin
8592 E1 := First_Entity (P);
8593 while Present (E1) and then E1 /= Instance loop
8594 if Ekind (E1) = E_Package
8595 and then Nkind (Parent (E1)) = N_Package_Renaming_Declaration
8596 then
8597 if Renamed_Object (E1) = Pack then
8598 return True;
8599
8600 elsif E1 = P or else Renamed_Object (E1) = P then
8601 return False;
8602
8603 elsif Is_Actual_Of_Previous_Formal (E1) then
8604 return True;
8605 end if;
8606 end if;
8607
8608 Next_Entity (E1);
8609 end loop;
8610
8611 return False;
8612 end Is_Actual_Of_Previous_Formal;
8613
8614 -- Start of processing for Denotes_Formal_Package
8615
8616 begin
8617 if On_Exit then
8618 Par :=
8619 Instance_Envs.Table
8620 (Instance_Envs.Last).Instantiated_Parent.Act_Id;
8621 else
8622 Par := Current_Instantiated_Parent.Act_Id;
8623 end if;
8624
8625 if Ekind (Scop) = E_Generic_Package
8626 or else Nkind (Unit_Declaration_Node (Scop)) =
8627 N_Generic_Subprogram_Declaration
8628 then
8629 return True;
8630
8631 elsif Nkind (Original_Node (Unit_Declaration_Node (Pack))) =
8632 N_Formal_Package_Declaration
8633 then
8634 return True;
8635
8636 elsif No (Par) then
8637 return False;
8638
8639 else
8640 -- Check whether this package is associated with a formal package of
8641 -- the enclosing instantiation. Iterate over the list of renamings.
8642
8643 E := First_Entity (Par);
8644 while Present (E) loop
8645 if Ekind (E) /= E_Package
8646 or else Nkind (Parent (E)) /= N_Package_Renaming_Declaration
8647 then
8648 null;
8649
8650 elsif Renamed_Object (E) = Par then
8651 return False;
8652
8653 elsif Renamed_Object (E) = Pack then
8654 return True;
8655
8656 elsif Is_Actual_Of_Previous_Formal (E) then
8657 return True;
8658
8659 end if;
8660
8661 Next_Entity (E);
8662 end loop;
8663
8664 return False;
8665 end if;
8666 end Denotes_Formal_Package;
8667
8668 -----------------
8669 -- End_Generic --
8670 -----------------
8671
8672 procedure End_Generic is
8673 begin
8674 -- ??? More things could be factored out in this routine. Should
8675 -- probably be done at a later stage.
8676
8677 Inside_A_Generic := Generic_Flags.Table (Generic_Flags.Last);
8678 Generic_Flags.Decrement_Last;
8679
8680 Expander_Mode_Restore;
8681 end End_Generic;
8682
8683 -------------
8684 -- Earlier --
8685 -------------
8686
8687 function Earlier (N1, N2 : Node_Id) return Boolean is
8688 procedure Find_Depth (P : in out Node_Id; D : in out Integer);
8689 -- Find distance from given node to enclosing compilation unit
8690
8691 ----------------
8692 -- Find_Depth --
8693 ----------------
8694
8695 procedure Find_Depth (P : in out Node_Id; D : in out Integer) is
8696 begin
8697 while Present (P)
8698 and then Nkind (P) /= N_Compilation_Unit
8699 loop
8700 P := True_Parent (P);
8701 D := D + 1;
8702 end loop;
8703 end Find_Depth;
8704
8705 -- Local declarations
8706
8707 D1 : Integer := 0;
8708 D2 : Integer := 0;
8709 P1 : Node_Id := N1;
8710 P2 : Node_Id := N2;
8711 T1 : Source_Ptr;
8712 T2 : Source_Ptr;
8713
8714 -- Start of processing for Earlier
8715
8716 begin
8717 Find_Depth (P1, D1);
8718 Find_Depth (P2, D2);
8719
8720 if P1 /= P2 then
8721 return False;
8722 else
8723 P1 := N1;
8724 P2 := N2;
8725 end if;
8726
8727 while D1 > D2 loop
8728 P1 := True_Parent (P1);
8729 D1 := D1 - 1;
8730 end loop;
8731
8732 while D2 > D1 loop
8733 P2 := True_Parent (P2);
8734 D2 := D2 - 1;
8735 end loop;
8736
8737 -- At this point P1 and P2 are at the same distance from the root.
8738 -- We examine their parents until we find a common declarative list.
8739 -- If we reach the root, N1 and N2 do not descend from the same
8740 -- declarative list (e.g. one is nested in the declarative part and
8741 -- the other is in a block in the statement part) and the earlier
8742 -- one is already frozen.
8743
8744 while not Is_List_Member (P1)
8745 or else not Is_List_Member (P2)
8746 or else List_Containing (P1) /= List_Containing (P2)
8747 loop
8748 P1 := True_Parent (P1);
8749 P2 := True_Parent (P2);
8750
8751 if Nkind (Parent (P1)) = N_Subunit then
8752 P1 := Corresponding_Stub (Parent (P1));
8753 end if;
8754
8755 if Nkind (Parent (P2)) = N_Subunit then
8756 P2 := Corresponding_Stub (Parent (P2));
8757 end if;
8758
8759 if P1 = P2 then
8760 return False;
8761 end if;
8762 end loop;
8763
8764 -- Expanded code usually shares the source location of the original
8765 -- construct it was generated for. This however may not necessarily
8766 -- reflect the true location of the code within the tree.
8767
8768 -- Before comparing the slocs of the two nodes, make sure that we are
8769 -- working with correct source locations. Assume that P1 is to the left
8770 -- of P2. If either one does not come from source, traverse the common
8771 -- list heading towards the other node and locate the first source
8772 -- statement.
8773
8774 -- P1 P2
8775 -- ----+===+===+--------------+===+===+----
8776 -- expanded code expanded code
8777
8778 if not Comes_From_Source (P1) then
8779 while Present (P1) loop
8780
8781 -- Neither P2 nor a source statement were located during the
8782 -- search. If we reach the end of the list, then P1 does not
8783 -- occur earlier than P2.
8784
8785 -- ---->
8786 -- start --- P2 ----- P1 --- end
8787
8788 if No (Next (P1)) then
8789 return False;
8790
8791 -- We encounter P2 while going to the right of the list. This
8792 -- means that P1 does indeed appear earlier.
8793
8794 -- ---->
8795 -- start --- P1 ===== P2 --- end
8796 -- expanded code in between
8797
8798 elsif P1 = P2 then
8799 return True;
8800
8801 -- No need to look any further since we have located a source
8802 -- statement.
8803
8804 elsif Comes_From_Source (P1) then
8805 exit;
8806 end if;
8807
8808 -- Keep going right
8809
8810 Next (P1);
8811 end loop;
8812 end if;
8813
8814 if not Comes_From_Source (P2) then
8815 while Present (P2) loop
8816
8817 -- Neither P1 nor a source statement were located during the
8818 -- search. If we reach the start of the list, then P1 does not
8819 -- occur earlier than P2.
8820
8821 -- <----
8822 -- start --- P2 --- P1 --- end
8823
8824 if No (Prev (P2)) then
8825 return False;
8826
8827 -- We encounter P1 while going to the left of the list. This
8828 -- means that P1 does indeed appear earlier.
8829
8830 -- <----
8831 -- start --- P1 ===== P2 --- end
8832 -- expanded code in between
8833
8834 elsif P2 = P1 then
8835 return True;
8836
8837 -- No need to look any further since we have located a source
8838 -- statement.
8839
8840 elsif Comes_From_Source (P2) then
8841 exit;
8842 end if;
8843
8844 -- Keep going left
8845
8846 Prev (P2);
8847 end loop;
8848 end if;
8849
8850 -- At this point either both nodes came from source or we approximated
8851 -- their source locations through neighboring source statements.
8852
8853 T1 := Top_Level_Location (Sloc (P1));
8854 T2 := Top_Level_Location (Sloc (P2));
8855
8856 -- When two nodes come from the same instance, they have identical top
8857 -- level locations. To determine proper relation within the tree, check
8858 -- their locations within the template.
8859
8860 if T1 = T2 then
8861 return Sloc (P1) < Sloc (P2);
8862
8863 -- The two nodes either come from unrelated instances or do not come
8864 -- from instantiated code at all.
8865
8866 else
8867 return T1 < T2;
8868 end if;
8869 end Earlier;
8870
8871 ----------------------
8872 -- Find_Actual_Type --
8873 ----------------------
8874
8875 function Find_Actual_Type
8876 (Typ : Entity_Id;
8877 Gen_Type : Entity_Id) return Entity_Id
8878 is
8879 Gen_Scope : constant Entity_Id := Scope (Gen_Type);
8880 T : Entity_Id;
8881
8882 begin
8883 -- Special processing only applies to child units
8884
8885 if not Is_Child_Unit (Gen_Scope) then
8886 return Get_Instance_Of (Typ);
8887
8888 -- If designated or component type is itself a formal of the child unit,
8889 -- its instance is available.
8890
8891 elsif Scope (Typ) = Gen_Scope then
8892 return Get_Instance_Of (Typ);
8893
8894 -- If the array or access type is not declared in the parent unit,
8895 -- no special processing needed.
8896
8897 elsif not Is_Generic_Type (Typ)
8898 and then Scope (Gen_Scope) /= Scope (Typ)
8899 then
8900 return Get_Instance_Of (Typ);
8901
8902 -- Otherwise, retrieve designated or component type by visibility
8903
8904 else
8905 T := Current_Entity (Typ);
8906 while Present (T) loop
8907 if In_Open_Scopes (Scope (T)) then
8908 return T;
8909 elsif Is_Generic_Actual_Type (T) then
8910 return T;
8911 end if;
8912
8913 T := Homonym (T);
8914 end loop;
8915
8916 return Typ;
8917 end if;
8918 end Find_Actual_Type;
8919
8920 ----------------------------
8921 -- Freeze_Subprogram_Body --
8922 ----------------------------
8923
8924 procedure Freeze_Subprogram_Body
8925 (Inst_Node : Node_Id;
8926 Gen_Body : Node_Id;
8927 Pack_Id : Entity_Id)
8928 is
8929 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
8930 Par : constant Entity_Id := Scope (Gen_Unit);
8931 E_G_Id : Entity_Id;
8932 Enc_G : Entity_Id;
8933 Enc_I : Node_Id;
8934 F_Node : Node_Id;
8935
8936 function Enclosing_Package_Body (N : Node_Id) return Node_Id;
8937 -- Find innermost package body that encloses the given node, and which
8938 -- is not a compilation unit. Freeze nodes for the instance, or for its
8939 -- enclosing body, may be inserted after the enclosing_body of the
8940 -- generic unit. Used to determine proper placement of freeze node for
8941 -- both package and subprogram instances.
8942
8943 function Package_Freeze_Node (B : Node_Id) return Node_Id;
8944 -- Find entity for given package body, and locate or create a freeze
8945 -- node for it.
8946
8947 ----------------------------
8948 -- Enclosing_Package_Body --
8949 ----------------------------
8950
8951 function Enclosing_Package_Body (N : Node_Id) return Node_Id is
8952 P : Node_Id;
8953
8954 begin
8955 P := Parent (N);
8956 while Present (P)
8957 and then Nkind (Parent (P)) /= N_Compilation_Unit
8958 loop
8959 if Nkind (P) = N_Package_Body then
8960 if Nkind (Parent (P)) = N_Subunit then
8961 return Corresponding_Stub (Parent (P));
8962 else
8963 return P;
8964 end if;
8965 end if;
8966
8967 P := True_Parent (P);
8968 end loop;
8969
8970 return Empty;
8971 end Enclosing_Package_Body;
8972
8973 -------------------------
8974 -- Package_Freeze_Node --
8975 -------------------------
8976
8977 function Package_Freeze_Node (B : Node_Id) return Node_Id is
8978 Id : Entity_Id;
8979
8980 begin
8981 if Nkind (B) = N_Package_Body then
8982 Id := Corresponding_Spec (B);
8983 else pragma Assert (Nkind (B) = N_Package_Body_Stub);
8984 Id := Corresponding_Spec (Proper_Body (Unit (Library_Unit (B))));
8985 end if;
8986
8987 Ensure_Freeze_Node (Id);
8988 return Freeze_Node (Id);
8989 end Package_Freeze_Node;
8990
8991 -- Start of processing for Freeze_Subprogram_Body
8992
8993 begin
8994 -- If the instance and the generic body appear within the same unit, and
8995 -- the instance precedes the generic, the freeze node for the instance
8996 -- must appear after that of the generic. If the generic is nested
8997 -- within another instance I2, then current instance must be frozen
8998 -- after I2. In both cases, the freeze nodes are those of enclosing
8999 -- packages. Otherwise, the freeze node is placed at the end of the
9000 -- current declarative part.
9001
9002 Enc_G := Enclosing_Package_Body (Gen_Body);
9003 Enc_I := Enclosing_Package_Body (Inst_Node);
9004 Ensure_Freeze_Node (Pack_Id);
9005 F_Node := Freeze_Node (Pack_Id);
9006
9007 if Is_Generic_Instance (Par)
9008 and then Present (Freeze_Node (Par))
9009 and then In_Same_Declarative_Part
9010 (Parent (Freeze_Node (Par)), Inst_Node)
9011 then
9012 -- The parent was a premature instantiation. Insert freeze node at
9013 -- the end the current declarative part.
9014
9015 if Is_Known_Guaranteed_ABE (Get_Unit_Instantiation_Node (Par)) then
9016 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
9017
9018 -- Handle the following case:
9019 --
9020 -- package Parent_Inst is new ...
9021 -- Parent_Inst []
9022 --
9023 -- procedure P ... -- this body freezes Parent_Inst
9024 --
9025 -- package Inst is new ...
9026 --
9027 -- In this particular scenario, the freeze node for Inst must be
9028 -- inserted in the same manner as that of Parent_Inst - before the
9029 -- next source body or at the end of the declarative list (body not
9030 -- available). If body P did not exist and Parent_Inst was frozen
9031 -- after Inst, either by a body following Inst or at the end of the
9032 -- declarative region, the freeze node for Inst must be inserted
9033 -- after that of Parent_Inst. This relation is established by
9034 -- comparing the Slocs of Parent_Inst freeze node and Inst.
9035
9036 elsif List_Containing (Get_Unit_Instantiation_Node (Par)) =
9037 List_Containing (Inst_Node)
9038 and then Sloc (Freeze_Node (Par)) < Sloc (Inst_Node)
9039 then
9040 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
9041
9042 else
9043 Insert_After (Freeze_Node (Par), F_Node);
9044 end if;
9045
9046 -- The body enclosing the instance should be frozen after the body that
9047 -- includes the generic, because the body of the instance may make
9048 -- references to entities therein. If the two are not in the same
9049 -- declarative part, or if the one enclosing the instance is frozen
9050 -- already, freeze the instance at the end of the current declarative
9051 -- part.
9052
9053 elsif Is_Generic_Instance (Par)
9054 and then Present (Freeze_Node (Par))
9055 and then Present (Enc_I)
9056 then
9057 if In_Same_Declarative_Part (Parent (Freeze_Node (Par)), Enc_I)
9058 or else
9059 (Nkind (Enc_I) = N_Package_Body
9060 and then In_Same_Declarative_Part
9061 (Parent (Freeze_Node (Par)), Parent (Enc_I)))
9062 then
9063 -- The enclosing package may contain several instances. Rather
9064 -- than computing the earliest point at which to insert its freeze
9065 -- node, we place it at the end of the declarative part of the
9066 -- parent of the generic.
9067
9068 Insert_Freeze_Node_For_Instance
9069 (Freeze_Node (Par), Package_Freeze_Node (Enc_I));
9070 end if;
9071
9072 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
9073
9074 elsif Present (Enc_G)
9075 and then Present (Enc_I)
9076 and then Enc_G /= Enc_I
9077 and then Earlier (Inst_Node, Gen_Body)
9078 then
9079 if Nkind (Enc_G) = N_Package_Body then
9080 E_G_Id :=
9081 Corresponding_Spec (Enc_G);
9082 else pragma Assert (Nkind (Enc_G) = N_Package_Body_Stub);
9083 E_G_Id :=
9084 Corresponding_Spec (Proper_Body (Unit (Library_Unit (Enc_G))));
9085 end if;
9086
9087 -- Freeze package that encloses instance, and place node after the
9088 -- package that encloses generic. If enclosing package is already
9089 -- frozen we have to assume it is at the proper place. This may be a
9090 -- potential ABE that requires dynamic checking. Do not add a freeze
9091 -- node if the package that encloses the generic is inside the body
9092 -- that encloses the instance, because the freeze node would be in
9093 -- the wrong scope. Additional contortions needed if the bodies are
9094 -- within a subunit.
9095
9096 declare
9097 Enclosing_Body : Node_Id;
9098
9099 begin
9100 if Nkind (Enc_I) = N_Package_Body_Stub then
9101 Enclosing_Body := Proper_Body (Unit (Library_Unit (Enc_I)));
9102 else
9103 Enclosing_Body := Enc_I;
9104 end if;
9105
9106 if Parent (List_Containing (Enc_G)) /= Enclosing_Body then
9107 Insert_Freeze_Node_For_Instance
9108 (Enc_G, Package_Freeze_Node (Enc_I));
9109 end if;
9110 end;
9111
9112 -- Freeze enclosing subunit before instance
9113
9114 Ensure_Freeze_Node (E_G_Id);
9115
9116 if not Is_List_Member (Freeze_Node (E_G_Id)) then
9117 Insert_After (Enc_G, Freeze_Node (E_G_Id));
9118 end if;
9119
9120 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
9121
9122 else
9123 -- If none of the above, insert freeze node at the end of the current
9124 -- declarative part.
9125
9126 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
9127 end if;
9128 end Freeze_Subprogram_Body;
9129
9130 ----------------
9131 -- Get_Gen_Id --
9132 ----------------
9133
9134 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id is
9135 begin
9136 return Generic_Renamings.Table (E).Gen_Id;
9137 end Get_Gen_Id;
9138
9139 ---------------------
9140 -- Get_Instance_Of --
9141 ---------------------
9142
9143 function Get_Instance_Of (A : Entity_Id) return Entity_Id is
9144 Res : constant Assoc_Ptr := Generic_Renamings_HTable.Get (A);
9145
9146 begin
9147 if Res /= Assoc_Null then
9148 return Generic_Renamings.Table (Res).Act_Id;
9149
9150 else
9151 -- On exit, entity is not instantiated: not a generic parameter, or
9152 -- else parameter of an inner generic unit.
9153
9154 return A;
9155 end if;
9156 end Get_Instance_Of;
9157
9158 ---------------------------------
9159 -- Get_Unit_Instantiation_Node --
9160 ---------------------------------
9161
9162 function Get_Unit_Instantiation_Node (A : Entity_Id) return Node_Id is
9163 Decl : Node_Id := Unit_Declaration_Node (A);
9164 Inst : Node_Id;
9165
9166 begin
9167 -- If the Package_Instantiation attribute has been set on the package
9168 -- entity, then use it directly when it (or its Original_Node) refers
9169 -- to an N_Package_Instantiation node. In principle it should be
9170 -- possible to have this field set in all cases, which should be
9171 -- investigated, and would allow this function to be significantly
9172 -- simplified. ???
9173
9174 Inst := Package_Instantiation (A);
9175
9176 if Present (Inst) then
9177 if Nkind (Inst) = N_Package_Instantiation then
9178 return Inst;
9179
9180 elsif Nkind (Original_Node (Inst)) = N_Package_Instantiation then
9181 return Original_Node (Inst);
9182 end if;
9183 end if;
9184
9185 -- If the instantiation is a compilation unit that does not need body
9186 -- then the instantiation node has been rewritten as a package
9187 -- declaration for the instance, and we return the original node.
9188
9189 -- If it is a compilation unit and the instance node has not been
9190 -- rewritten, then it is still the unit of the compilation. Finally, if
9191 -- a body is present, this is a parent of the main unit whose body has
9192 -- been compiled for inlining purposes, and the instantiation node has
9193 -- been rewritten with the instance body.
9194
9195 -- Otherwise the instantiation node appears after the declaration. If
9196 -- the entity is a formal package, the declaration may have been
9197 -- rewritten as a generic declaration (in the case of a formal with box)
9198 -- or left as a formal package declaration if it has actuals, and is
9199 -- found with a forward search.
9200
9201 if Nkind (Parent (Decl)) = N_Compilation_Unit then
9202 if Nkind (Decl) = N_Package_Declaration
9203 and then Present (Corresponding_Body (Decl))
9204 then
9205 Decl := Unit_Declaration_Node (Corresponding_Body (Decl));
9206 end if;
9207
9208 if Nkind (Original_Node (Decl)) in N_Generic_Instantiation then
9209 return Original_Node (Decl);
9210 else
9211 return Unit (Parent (Decl));
9212 end if;
9213
9214 elsif Nkind (Decl) = N_Package_Declaration
9215 and then Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration
9216 then
9217 return Original_Node (Decl);
9218
9219 else
9220 Inst := Next (Decl);
9221 while not Nkind_In (Inst, N_Formal_Package_Declaration,
9222 N_Function_Instantiation,
9223 N_Package_Instantiation,
9224 N_Procedure_Instantiation)
9225 loop
9226 Next (Inst);
9227 end loop;
9228
9229 return Inst;
9230 end if;
9231 end Get_Unit_Instantiation_Node;
9232
9233 ------------------------
9234 -- Has_Been_Exchanged --
9235 ------------------------
9236
9237 function Has_Been_Exchanged (E : Entity_Id) return Boolean is
9238 Next : Elmt_Id;
9239
9240 begin
9241 Next := First_Elmt (Exchanged_Views);
9242 while Present (Next) loop
9243 if Full_View (Node (Next)) = E then
9244 return True;
9245 end if;
9246
9247 Next_Elmt (Next);
9248 end loop;
9249
9250 return False;
9251 end Has_Been_Exchanged;
9252
9253 -------------------
9254 -- Has_Contracts --
9255 -------------------
9256
9257 function Has_Contracts (Decl : Node_Id) return Boolean is
9258 A_List : constant List_Id := Aspect_Specifications (Decl);
9259 A_Spec : Node_Id;
9260 A_Id : Aspect_Id;
9261 begin
9262 if No (A_List) then
9263 return False;
9264 else
9265 A_Spec := First (A_List);
9266 while Present (A_Spec) loop
9267 A_Id := Get_Aspect_Id (A_Spec);
9268 if A_Id = Aspect_Pre or else A_Id = Aspect_Post then
9269 return True;
9270 end if;
9271
9272 Next (A_Spec);
9273 end loop;
9274
9275 return False;
9276 end if;
9277 end Has_Contracts;
9278
9279 ----------
9280 -- Hash --
9281 ----------
9282
9283 function Hash (F : Entity_Id) return HTable_Range is
9284 begin
9285 return HTable_Range (F mod HTable_Size);
9286 end Hash;
9287
9288 ------------------------
9289 -- Hide_Current_Scope --
9290 ------------------------
9291
9292 procedure Hide_Current_Scope is
9293 C : constant Entity_Id := Current_Scope;
9294 E : Entity_Id;
9295
9296 begin
9297 Set_Is_Hidden_Open_Scope (C);
9298
9299 E := First_Entity (C);
9300 while Present (E) loop
9301 if Is_Immediately_Visible (E) then
9302 Set_Is_Immediately_Visible (E, False);
9303 Append_Elmt (E, Hidden_Entities);
9304 end if;
9305
9306 Next_Entity (E);
9307 end loop;
9308
9309 -- Make the scope name invisible as well. This is necessary, but might
9310 -- conflict with calls to Rtsfind later on, in case the scope is a
9311 -- predefined one. There is no clean solution to this problem, so for
9312 -- now we depend on the user not redefining Standard itself in one of
9313 -- the parent units.
9314
9315 if Is_Immediately_Visible (C) and then C /= Standard_Standard then
9316 Set_Is_Immediately_Visible (C, False);
9317 Append_Elmt (C, Hidden_Entities);
9318 end if;
9319
9320 end Hide_Current_Scope;
9321
9322 --------------
9323 -- Init_Env --
9324 --------------
9325
9326 procedure Init_Env is
9327 Saved : Instance_Env;
9328
9329 begin
9330 Saved.Instantiated_Parent := Current_Instantiated_Parent;
9331 Saved.Exchanged_Views := Exchanged_Views;
9332 Saved.Hidden_Entities := Hidden_Entities;
9333 Saved.Current_Sem_Unit := Current_Sem_Unit;
9334 Saved.Parent_Unit_Visible := Parent_Unit_Visible;
9335 Saved.Instance_Parent_Unit := Instance_Parent_Unit;
9336
9337 -- Save configuration switches. These may be reset if the unit is a
9338 -- predefined unit, and the current mode is not Ada 2005.
9339
9340 Saved.Switches := Save_Config_Switches;
9341
9342 Instance_Envs.Append (Saved);
9343
9344 Exchanged_Views := New_Elmt_List;
9345 Hidden_Entities := New_Elmt_List;
9346
9347 -- Make dummy entry for Instantiated parent. If generic unit is legal,
9348 -- this is set properly in Set_Instance_Env.
9349
9350 Current_Instantiated_Parent :=
9351 (Current_Scope, Current_Scope, Assoc_Null);
9352 end Init_Env;
9353
9354 ---------------------
9355 -- In_Main_Context --
9356 ---------------------
9357
9358 function In_Main_Context (E : Entity_Id) return Boolean is
9359 Context : List_Id;
9360 Clause : Node_Id;
9361 Nam : Node_Id;
9362
9363 begin
9364 if not Is_Compilation_Unit (E)
9365 or else Ekind (E) /= E_Package
9366 or else In_Private_Part (E)
9367 then
9368 return False;
9369 end if;
9370
9371 Context := Context_Items (Cunit (Main_Unit));
9372
9373 Clause := First (Context);
9374 while Present (Clause) loop
9375 if Nkind (Clause) = N_With_Clause then
9376 Nam := Name (Clause);
9377
9378 -- If the current scope is part of the context of the main unit,
9379 -- analysis of the corresponding with_clause is not complete, and
9380 -- the entity is not set. We use the Chars field directly, which
9381 -- might produce false positives in rare cases, but guarantees
9382 -- that we produce all the instance bodies we will need.
9383
9384 if (Is_Entity_Name (Nam) and then Chars (Nam) = Chars (E))
9385 or else (Nkind (Nam) = N_Selected_Component
9386 and then Chars (Selector_Name (Nam)) = Chars (E))
9387 then
9388 return True;
9389 end if;
9390 end if;
9391
9392 Next (Clause);
9393 end loop;
9394
9395 return False;
9396 end In_Main_Context;
9397
9398 ---------------------
9399 -- Inherit_Context --
9400 ---------------------
9401
9402 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id) is
9403 Current_Context : List_Id;
9404 Current_Unit : Node_Id;
9405 Item : Node_Id;
9406 New_I : Node_Id;
9407
9408 Clause : Node_Id;
9409 OK : Boolean;
9410 Lib_Unit : Node_Id;
9411
9412 begin
9413 if Nkind (Parent (Gen_Decl)) = N_Compilation_Unit then
9414
9415 -- The inherited context is attached to the enclosing compilation
9416 -- unit. This is either the main unit, or the declaration for the
9417 -- main unit (in case the instantiation appears within the package
9418 -- declaration and the main unit is its body).
9419
9420 Current_Unit := Parent (Inst);
9421 while Present (Current_Unit)
9422 and then Nkind (Current_Unit) /= N_Compilation_Unit
9423 loop
9424 Current_Unit := Parent (Current_Unit);
9425 end loop;
9426
9427 Current_Context := Context_Items (Current_Unit);
9428
9429 Item := First (Context_Items (Parent (Gen_Decl)));
9430 while Present (Item) loop
9431 if Nkind (Item) = N_With_Clause then
9432 Lib_Unit := Library_Unit (Item);
9433
9434 -- Take care to prevent direct cyclic with's
9435
9436 if Lib_Unit /= Current_Unit then
9437
9438 -- Do not add a unit if it is already in the context
9439
9440 Clause := First (Current_Context);
9441 OK := True;
9442 while Present (Clause) loop
9443 if Nkind (Clause) = N_With_Clause
9444 and then Library_Unit (Clause) = Lib_Unit
9445 then
9446 OK := False;
9447 exit;
9448 end if;
9449
9450 Next (Clause);
9451 end loop;
9452
9453 if OK then
9454 New_I := New_Copy (Item);
9455 Set_Implicit_With (New_I);
9456
9457 Append (New_I, Current_Context);
9458 end if;
9459 end if;
9460 end if;
9461
9462 Next (Item);
9463 end loop;
9464 end if;
9465 end Inherit_Context;
9466
9467 ----------------
9468 -- Initialize --
9469 ----------------
9470
9471 procedure Initialize is
9472 begin
9473 Generic_Renamings.Init;
9474 Instance_Envs.Init;
9475 Generic_Flags.Init;
9476 Generic_Renamings_HTable.Reset;
9477 Circularity_Detected := False;
9478 Exchanged_Views := No_Elist;
9479 Hidden_Entities := No_Elist;
9480 end Initialize;
9481
9482 -------------------------------------
9483 -- Insert_Freeze_Node_For_Instance --
9484 -------------------------------------
9485
9486 procedure Insert_Freeze_Node_For_Instance
9487 (N : Node_Id;
9488 F_Node : Node_Id)
9489 is
9490 Decl : Node_Id;
9491 Decls : List_Id;
9492 Inst : Entity_Id;
9493 Par_N : Node_Id;
9494
9495 function Enclosing_Body (N : Node_Id) return Node_Id;
9496 -- Find enclosing package or subprogram body, if any. Freeze node may
9497 -- be placed at end of current declarative list if previous instance
9498 -- and current one have different enclosing bodies.
9499
9500 function Previous_Instance (Gen : Entity_Id) return Entity_Id;
9501 -- Find the local instance, if any, that declares the generic that is
9502 -- being instantiated. If present, the freeze node for this instance
9503 -- must follow the freeze node for the previous instance.
9504
9505 --------------------
9506 -- Enclosing_Body --
9507 --------------------
9508
9509 function Enclosing_Body (N : Node_Id) return Node_Id is
9510 P : Node_Id;
9511
9512 begin
9513 P := Parent (N);
9514 while Present (P)
9515 and then Nkind (Parent (P)) /= N_Compilation_Unit
9516 loop
9517 if Nkind_In (P, N_Package_Body, N_Subprogram_Body) then
9518 if Nkind (Parent (P)) = N_Subunit then
9519 return Corresponding_Stub (Parent (P));
9520 else
9521 return P;
9522 end if;
9523 end if;
9524
9525 P := True_Parent (P);
9526 end loop;
9527
9528 return Empty;
9529 end Enclosing_Body;
9530
9531 -----------------------
9532 -- Previous_Instance --
9533 -----------------------
9534
9535 function Previous_Instance (Gen : Entity_Id) return Entity_Id is
9536 S : Entity_Id;
9537
9538 begin
9539 S := Scope (Gen);
9540 while Present (S) and then S /= Standard_Standard loop
9541 if Is_Generic_Instance (S)
9542 and then In_Same_Source_Unit (S, N)
9543 then
9544 return S;
9545 end if;
9546
9547 S := Scope (S);
9548 end loop;
9549
9550 return Empty;
9551 end Previous_Instance;
9552
9553 -- Start of processing for Insert_Freeze_Node_For_Instance
9554
9555 begin
9556 if not Is_List_Member (F_Node) then
9557 Decl := N;
9558 Decls := List_Containing (N);
9559 Inst := Entity (F_Node);
9560 Par_N := Parent (Decls);
9561
9562 -- When processing a subprogram instantiation, utilize the actual
9563 -- subprogram instantiation rather than its package wrapper as it
9564 -- carries all the context information.
9565
9566 if Is_Wrapper_Package (Inst) then
9567 Inst := Related_Instance (Inst);
9568 end if;
9569
9570 -- If this is a package instance, check whether the generic is
9571 -- declared in a previous instance and the current instance is
9572 -- not within the previous one.
9573
9574 if Present (Generic_Parent (Parent (Inst)))
9575 and then Is_In_Main_Unit (N)
9576 then
9577 declare
9578 Enclosing_N : constant Node_Id := Enclosing_Body (N);
9579 Par_I : constant Entity_Id :=
9580 Previous_Instance
9581 (Generic_Parent (Parent (Inst)));
9582 Scop : Entity_Id;
9583
9584 begin
9585 if Present (Par_I)
9586 and then Earlier (N, Freeze_Node (Par_I))
9587 then
9588 Scop := Scope (Inst);
9589
9590 -- If the current instance is within the one that contains
9591 -- the generic, the freeze node for the current one must
9592 -- appear in the current declarative part. Ditto, if the
9593 -- current instance is within another package instance or
9594 -- within a body that does not enclose the current instance.
9595 -- In these three cases the freeze node of the previous
9596 -- instance is not relevant.
9597
9598 while Present (Scop) and then Scop /= Standard_Standard loop
9599 exit when Scop = Par_I
9600 or else
9601 (Is_Generic_Instance (Scop)
9602 and then Scope_Depth (Scop) > Scope_Depth (Par_I));
9603 Scop := Scope (Scop);
9604 end loop;
9605
9606 -- Previous instance encloses current instance
9607
9608 if Scop = Par_I then
9609 null;
9610
9611 -- If the next node is a source body we must freeze in
9612 -- the current scope as well.
9613
9614 elsif Present (Next (N))
9615 and then Nkind_In (Next (N), N_Subprogram_Body,
9616 N_Package_Body)
9617 and then Comes_From_Source (Next (N))
9618 then
9619 null;
9620
9621 -- Current instance is within an unrelated instance
9622
9623 elsif Is_Generic_Instance (Scop) then
9624 null;
9625
9626 -- Current instance is within an unrelated body
9627
9628 elsif Present (Enclosing_N)
9629 and then Enclosing_N /= Enclosing_Body (Par_I)
9630 then
9631 null;
9632
9633 else
9634 Insert_After (Freeze_Node (Par_I), F_Node);
9635 return;
9636 end if;
9637 end if;
9638 end;
9639 end if;
9640
9641 -- When the instantiation occurs in a package declaration, append the
9642 -- freeze node to the private declarations (if any).
9643
9644 if Nkind (Par_N) = N_Package_Specification
9645 and then Decls = Visible_Declarations (Par_N)
9646 and then Present (Private_Declarations (Par_N))
9647 and then not Is_Empty_List (Private_Declarations (Par_N))
9648 then
9649 Decls := Private_Declarations (Par_N);
9650 Decl := First (Decls);
9651 end if;
9652
9653 -- Determine the proper freeze point of a package instantiation. We
9654 -- adhere to the general rule of a package or subprogram body causing
9655 -- freezing of anything before it in the same declarative region. In
9656 -- this case, the proper freeze point of a package instantiation is
9657 -- before the first source body which follows, or before a stub. This
9658 -- ensures that entities coming from the instance are already frozen
9659 -- and usable in source bodies.
9660
9661 if Nkind (Par_N) /= N_Package_Declaration
9662 and then Ekind (Inst) = E_Package
9663 and then Is_Generic_Instance (Inst)
9664 and then
9665 not In_Same_Source_Unit (Generic_Parent (Parent (Inst)), Inst)
9666 then
9667 while Present (Decl) loop
9668 if (Nkind (Decl) in N_Unit_Body
9669 or else
9670 Nkind (Decl) in N_Body_Stub)
9671 and then Comes_From_Source (Decl)
9672 then
9673 Insert_Before (Decl, F_Node);
9674 return;
9675 end if;
9676
9677 Next (Decl);
9678 end loop;
9679 end if;
9680
9681 -- In a package declaration, or if no previous body, insert at end
9682 -- of list.
9683
9684 Set_Sloc (F_Node, Sloc (Last (Decls)));
9685 Insert_After (Last (Decls), F_Node);
9686 end if;
9687 end Insert_Freeze_Node_For_Instance;
9688
9689 ------------------
9690 -- Install_Body --
9691 ------------------
9692
9693 procedure Install_Body
9694 (Act_Body : Node_Id;
9695 N : Node_Id;
9696 Gen_Body : Node_Id;
9697 Gen_Decl : Node_Id)
9698 is
9699 function In_Same_Scope (Gen_Id, Act_Id : Node_Id) return Boolean;
9700 -- Check if the generic definition and the instantiation come from
9701 -- a common scope, in which case the instance must be frozen after
9702 -- the generic body.
9703
9704 function True_Sloc (N, Act_Unit : Node_Id) return Source_Ptr;
9705 -- If the instance is nested inside a generic unit, the Sloc of the
9706 -- instance indicates the place of the original definition, not the
9707 -- point of the current enclosing instance. Pending a better usage of
9708 -- Slocs to indicate instantiation places, we determine the place of
9709 -- origin of a node by finding the maximum sloc of any ancestor node.
9710 -- Why is this not equivalent to Top_Level_Location ???
9711
9712 -------------------
9713 -- In_Same_Scope --
9714 -------------------
9715
9716 function In_Same_Scope (Gen_Id, Act_Id : Node_Id) return Boolean is
9717 Act_Scop : Entity_Id := Scope (Act_Id);
9718 Gen_Scop : Entity_Id := Scope (Gen_Id);
9719
9720 begin
9721 while Act_Scop /= Standard_Standard
9722 and then Gen_Scop /= Standard_Standard
9723 loop
9724 if Act_Scop = Gen_Scop then
9725 return True;
9726 end if;
9727
9728 Act_Scop := Scope (Act_Scop);
9729 Gen_Scop := Scope (Gen_Scop);
9730 end loop;
9731
9732 return False;
9733 end In_Same_Scope;
9734
9735 ---------------
9736 -- True_Sloc --
9737 ---------------
9738
9739 function True_Sloc (N, Act_Unit : Node_Id) return Source_Ptr is
9740 N1 : Node_Id;
9741 Res : Source_Ptr;
9742
9743 begin
9744 Res := Sloc (N);
9745 N1 := N;
9746 while Present (N1) and then N1 /= Act_Unit loop
9747 if Sloc (N1) > Res then
9748 Res := Sloc (N1);
9749 end if;
9750
9751 N1 := Parent (N1);
9752 end loop;
9753
9754 return Res;
9755 end True_Sloc;
9756
9757 Act_Id : constant Entity_Id := Corresponding_Spec (Act_Body);
9758 Act_Unit : constant Node_Id := Unit (Cunit (Get_Source_Unit (N)));
9759 Gen_Id : constant Entity_Id := Corresponding_Spec (Gen_Body);
9760 Par : constant Entity_Id := Scope (Gen_Id);
9761 Gen_Unit : constant Node_Id :=
9762 Unit (Cunit (Get_Source_Unit (Gen_Decl)));
9763
9764 Body_Unit : Node_Id;
9765 F_Node : Node_Id;
9766 Must_Delay : Boolean;
9767 Orig_Body : Node_Id := Gen_Body;
9768
9769 -- Start of processing for Install_Body
9770
9771 begin
9772 -- Handle first the case of an instance with incomplete actual types.
9773 -- The instance body cannot be placed after the declaration because
9774 -- full views have not been seen yet. Any use of the non-limited views
9775 -- in the instance body requires the presence of a regular with_clause
9776 -- in the enclosing unit, and will fail if this with_clause is missing.
9777 -- We place the instance body at the beginning of the enclosing body,
9778 -- which is the unit being compiled. The freeze node for the instance
9779 -- is then placed after the instance body.
9780
9781 if not Is_Empty_Elmt_List (Incomplete_Actuals (Act_Id))
9782 and then Expander_Active
9783 and then Ekind (Scope (Act_Id)) = E_Package
9784 then
9785 declare
9786 Scop : constant Entity_Id := Scope (Act_Id);
9787 Body_Id : constant Node_Id :=
9788 Corresponding_Body (Unit_Declaration_Node (Scop));
9789
9790 begin
9791 Ensure_Freeze_Node (Act_Id);
9792 F_Node := Freeze_Node (Act_Id);
9793 if Present (Body_Id) then
9794 Set_Is_Frozen (Act_Id, False);
9795 Prepend (Act_Body, Declarations (Parent (Body_Id)));
9796 if Is_List_Member (F_Node) then
9797 Remove (F_Node);
9798 end if;
9799
9800 Insert_After (Act_Body, F_Node);
9801 end if;
9802 end;
9803 return;
9804 end if;
9805
9806 -- If the body is a subunit, the freeze point is the corresponding stub
9807 -- in the current compilation, not the subunit itself.
9808
9809 if Nkind (Parent (Gen_Body)) = N_Subunit then
9810 Orig_Body := Corresponding_Stub (Parent (Gen_Body));
9811 else
9812 Orig_Body := Gen_Body;
9813 end if;
9814
9815 Body_Unit := Unit (Cunit (Get_Source_Unit (Orig_Body)));
9816
9817 -- If the instantiation and the generic definition appear in the same
9818 -- package declaration, this is an early instantiation. If they appear
9819 -- in the same declarative part, it is an early instantiation only if
9820 -- the generic body appears textually later, and the generic body is
9821 -- also in the main unit.
9822
9823 -- If instance is nested within a subprogram, and the generic body
9824 -- is not, the instance is delayed because the enclosing body is. If
9825 -- instance and body are within the same scope, or the same subprogram
9826 -- body, indicate explicitly that the instance is delayed.
9827
9828 Must_Delay :=
9829 (Gen_Unit = Act_Unit
9830 and then (Nkind_In (Gen_Unit, N_Generic_Package_Declaration,
9831 N_Package_Declaration)
9832 or else (Gen_Unit = Body_Unit
9833 and then True_Sloc (N, Act_Unit) <
9834 Sloc (Orig_Body)))
9835 and then Is_In_Main_Unit (Original_Node (Gen_Unit))
9836 and then In_Same_Scope (Gen_Id, Act_Id));
9837
9838 -- If this is an early instantiation, the freeze node is placed after
9839 -- the generic body. Otherwise, if the generic appears in an instance,
9840 -- we cannot freeze the current instance until the outer one is frozen.
9841 -- This is only relevant if the current instance is nested within some
9842 -- inner scope not itself within the outer instance. If this scope is
9843 -- a package body in the same declarative part as the outer instance,
9844 -- then that body needs to be frozen after the outer instance. Finally,
9845 -- if no delay is needed, we place the freeze node at the end of the
9846 -- current declarative part.
9847
9848 if Expander_Active
9849 and then (No (Freeze_Node (Act_Id))
9850 or else not Is_List_Member (Freeze_Node (Act_Id)))
9851 then
9852 Ensure_Freeze_Node (Act_Id);
9853 F_Node := Freeze_Node (Act_Id);
9854
9855 if Must_Delay then
9856 Insert_After (Orig_Body, F_Node);
9857
9858 elsif Is_Generic_Instance (Par)
9859 and then Present (Freeze_Node (Par))
9860 and then Scope (Act_Id) /= Par
9861 then
9862 -- Freeze instance of inner generic after instance of enclosing
9863 -- generic.
9864
9865 if In_Same_Declarative_Part (Parent (Freeze_Node (Par)), N) then
9866
9867 -- Handle the following case:
9868
9869 -- package Parent_Inst is new ...
9870 -- Parent_Inst []
9871
9872 -- procedure P ... -- this body freezes Parent_Inst
9873
9874 -- package Inst is new ...
9875
9876 -- In this particular scenario, the freeze node for Inst must
9877 -- be inserted in the same manner as that of Parent_Inst,
9878 -- before the next source body or at the end of the declarative
9879 -- list (body not available). If body P did not exist and
9880 -- Parent_Inst was frozen after Inst, either by a body
9881 -- following Inst or at the end of the declarative region,
9882 -- the freeze node for Inst must be inserted after that of
9883 -- Parent_Inst. This relation is established by comparing
9884 -- the Slocs of Parent_Inst freeze node and Inst.
9885 -- We examine the parents of the enclosing lists to handle
9886 -- the case where the parent instance is in the visible part
9887 -- of a package declaration, and the inner instance is in
9888 -- the corresponding private part.
9889
9890 if Parent (List_Containing (Get_Unit_Instantiation_Node (Par)))
9891 = Parent (List_Containing (N))
9892 and then Sloc (Freeze_Node (Par)) < Sloc (N)
9893 then
9894 Insert_Freeze_Node_For_Instance (N, F_Node);
9895 else
9896 Insert_After (Freeze_Node (Par), F_Node);
9897 end if;
9898
9899 -- Freeze package enclosing instance of inner generic after
9900 -- instance of enclosing generic.
9901
9902 elsif Nkind_In (Parent (N), N_Package_Body, N_Subprogram_Body)
9903 and then In_Same_Declarative_Part
9904 (Parent (Freeze_Node (Par)), Parent (N))
9905 then
9906 declare
9907 Enclosing : Entity_Id;
9908
9909 begin
9910 Enclosing := Corresponding_Spec (Parent (N));
9911
9912 if No (Enclosing) then
9913 Enclosing := Defining_Entity (Parent (N));
9914 end if;
9915
9916 Insert_Freeze_Node_For_Instance (N, F_Node);
9917 Ensure_Freeze_Node (Enclosing);
9918
9919 if not Is_List_Member (Freeze_Node (Enclosing)) then
9920
9921 -- The enclosing context is a subunit, insert the freeze
9922 -- node after the stub.
9923
9924 if Nkind (Parent (Parent (N))) = N_Subunit then
9925 Insert_Freeze_Node_For_Instance
9926 (Corresponding_Stub (Parent (Parent (N))),
9927 Freeze_Node (Enclosing));
9928
9929 -- The enclosing context is a package with a stub body
9930 -- which has already been replaced by the real body.
9931 -- Insert the freeze node after the actual body.
9932
9933 elsif Ekind (Enclosing) = E_Package
9934 and then Present (Body_Entity (Enclosing))
9935 and then Was_Originally_Stub
9936 (Parent (Body_Entity (Enclosing)))
9937 then
9938 Insert_Freeze_Node_For_Instance
9939 (Parent (Body_Entity (Enclosing)),
9940 Freeze_Node (Enclosing));
9941
9942 -- The parent instance has been frozen before the body of
9943 -- the enclosing package, insert the freeze node after
9944 -- the body.
9945
9946 elsif List_Containing (Freeze_Node (Par)) =
9947 List_Containing (Parent (N))
9948 and then Sloc (Freeze_Node (Par)) < Sloc (Parent (N))
9949 then
9950 Insert_Freeze_Node_For_Instance
9951 (Parent (N), Freeze_Node (Enclosing));
9952
9953 else
9954 Insert_After
9955 (Freeze_Node (Par), Freeze_Node (Enclosing));
9956 end if;
9957 end if;
9958 end;
9959
9960 else
9961 Insert_Freeze_Node_For_Instance (N, F_Node);
9962 end if;
9963
9964 else
9965 Insert_Freeze_Node_For_Instance (N, F_Node);
9966 end if;
9967 end if;
9968
9969 Set_Is_Frozen (Act_Id);
9970 Insert_Before (N, Act_Body);
9971 Mark_Rewrite_Insertion (Act_Body);
9972 end Install_Body;
9973
9974 -----------------------------
9975 -- Install_Formal_Packages --
9976 -----------------------------
9977
9978 procedure Install_Formal_Packages (Par : Entity_Id) is
9979 E : Entity_Id;
9980 Gen : Entity_Id;
9981 Gen_E : Entity_Id := Empty;
9982
9983 begin
9984 E := First_Entity (Par);
9985
9986 -- If we are installing an instance parent, locate the formal packages
9987 -- of its generic parent.
9988
9989 if Is_Generic_Instance (Par) then
9990 Gen := Generic_Parent (Package_Specification (Par));
9991 Gen_E := First_Entity (Gen);
9992 end if;
9993
9994 while Present (E) loop
9995 if Ekind (E) = E_Package
9996 and then Nkind (Parent (E)) = N_Package_Renaming_Declaration
9997 then
9998 -- If this is the renaming for the parent instance, done
9999
10000 if Renamed_Object (E) = Par then
10001 exit;
10002
10003 -- The visibility of a formal of an enclosing generic is already
10004 -- correct.
10005
10006 elsif Denotes_Formal_Package (E) then
10007 null;
10008
10009 elsif Present (Associated_Formal_Package (E)) then
10010 Check_Generic_Actuals (Renamed_Object (E), True);
10011 Set_Is_Hidden (E, False);
10012
10013 -- Find formal package in generic unit that corresponds to
10014 -- (instance of) formal package in instance.
10015
10016 while Present (Gen_E) and then Chars (Gen_E) /= Chars (E) loop
10017 Next_Entity (Gen_E);
10018 end loop;
10019
10020 if Present (Gen_E) then
10021 Map_Formal_Package_Entities (Gen_E, E);
10022 end if;
10023 end if;
10024 end if;
10025
10026 Next_Entity (E);
10027
10028 if Present (Gen_E) then
10029 Next_Entity (Gen_E);
10030 end if;
10031 end loop;
10032 end Install_Formal_Packages;
10033
10034 --------------------
10035 -- Install_Parent --
10036 --------------------
10037
10038 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False) is
10039 Ancestors : constant Elist_Id := New_Elmt_List;
10040 S : constant Entity_Id := Current_Scope;
10041 Inst_Par : Entity_Id;
10042 First_Par : Entity_Id;
10043 Inst_Node : Node_Id;
10044 Gen_Par : Entity_Id;
10045 First_Gen : Entity_Id;
10046 Elmt : Elmt_Id;
10047
10048 procedure Install_Noninstance_Specs (Par : Entity_Id);
10049 -- Install the scopes of noninstance parent units ending with Par
10050
10051 procedure Install_Spec (Par : Entity_Id);
10052 -- The child unit is within the declarative part of the parent, so the
10053 -- declarations within the parent are immediately visible.
10054
10055 -------------------------------
10056 -- Install_Noninstance_Specs --
10057 -------------------------------
10058
10059 procedure Install_Noninstance_Specs (Par : Entity_Id) is
10060 begin
10061 if Present (Par)
10062 and then Par /= Standard_Standard
10063 and then not In_Open_Scopes (Par)
10064 then
10065 Install_Noninstance_Specs (Scope (Par));
10066 Install_Spec (Par);
10067 end if;
10068 end Install_Noninstance_Specs;
10069
10070 ------------------
10071 -- Install_Spec --
10072 ------------------
10073
10074 procedure Install_Spec (Par : Entity_Id) is
10075 Spec : constant Node_Id := Package_Specification (Par);
10076
10077 begin
10078 -- If this parent of the child instance is a top-level unit,
10079 -- then record the unit and its visibility for later resetting in
10080 -- Remove_Parent. We exclude units that are generic instances, as we
10081 -- only want to record this information for the ultimate top-level
10082 -- noninstance parent (is that always correct???).
10083
10084 if Scope (Par) = Standard_Standard
10085 and then not Is_Generic_Instance (Par)
10086 then
10087 Parent_Unit_Visible := Is_Immediately_Visible (Par);
10088 Instance_Parent_Unit := Par;
10089 end if;
10090
10091 -- Open the parent scope and make it and its declarations visible.
10092 -- If this point is not within a body, then only the visible
10093 -- declarations should be made visible, and installation of the
10094 -- private declarations is deferred until the appropriate point
10095 -- within analysis of the spec being instantiated (see the handling
10096 -- of parent visibility in Analyze_Package_Specification). This is
10097 -- relaxed in the case where the parent unit is Ada.Tags, to avoid
10098 -- private view problems that occur when compiling instantiations of
10099 -- a generic child of that package (Generic_Dispatching_Constructor).
10100 -- If the instance freezes a tagged type, inlinings of operations
10101 -- from Ada.Tags may need the full view of type Tag. If inlining took
10102 -- proper account of establishing visibility of inlined subprograms'
10103 -- parents then it should be possible to remove this
10104 -- special check. ???
10105
10106 Push_Scope (Par);
10107 Set_Is_Immediately_Visible (Par);
10108 Install_Visible_Declarations (Par);
10109 Set_Use (Visible_Declarations (Spec));
10110
10111 if In_Body or else Is_RTU (Par, Ada_Tags) then
10112 Install_Private_Declarations (Par);
10113 Set_Use (Private_Declarations (Spec));
10114 end if;
10115 end Install_Spec;
10116
10117 -- Start of processing for Install_Parent
10118
10119 begin
10120 -- We need to install the parent instance to compile the instantiation
10121 -- of the child, but the child instance must appear in the current
10122 -- scope. Given that we cannot place the parent above the current scope
10123 -- in the scope stack, we duplicate the current scope and unstack both
10124 -- after the instantiation is complete.
10125
10126 -- If the parent is itself the instantiation of a child unit, we must
10127 -- also stack the instantiation of its parent, and so on. Each such
10128 -- ancestor is the prefix of the name in a prior instantiation.
10129
10130 -- If this is a nested instance, the parent unit itself resolves to
10131 -- a renaming of the parent instance, whose declaration we need.
10132
10133 -- Finally, the parent may be a generic (not an instance) when the
10134 -- child unit appears as a formal package.
10135
10136 Inst_Par := P;
10137
10138 if Present (Renamed_Entity (Inst_Par)) then
10139 Inst_Par := Renamed_Entity (Inst_Par);
10140 end if;
10141
10142 First_Par := Inst_Par;
10143
10144 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
10145
10146 First_Gen := Gen_Par;
10147
10148 while Present (Gen_Par) and then Is_Child_Unit (Gen_Par) loop
10149
10150 -- Load grandparent instance as well
10151
10152 Inst_Node := Get_Unit_Instantiation_Node (Inst_Par);
10153
10154 if Nkind (Name (Inst_Node)) = N_Expanded_Name then
10155 Inst_Par := Entity (Prefix (Name (Inst_Node)));
10156
10157 if Present (Renamed_Entity (Inst_Par)) then
10158 Inst_Par := Renamed_Entity (Inst_Par);
10159 end if;
10160
10161 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
10162
10163 if Present (Gen_Par) then
10164 Prepend_Elmt (Inst_Par, Ancestors);
10165
10166 else
10167 -- Parent is not the name of an instantiation
10168
10169 Install_Noninstance_Specs (Inst_Par);
10170 exit;
10171 end if;
10172
10173 else
10174 -- Previous error
10175
10176 exit;
10177 end if;
10178 end loop;
10179
10180 if Present (First_Gen) then
10181 Append_Elmt (First_Par, Ancestors);
10182 else
10183 Install_Noninstance_Specs (First_Par);
10184 end if;
10185
10186 if not Is_Empty_Elmt_List (Ancestors) then
10187 Elmt := First_Elmt (Ancestors);
10188 while Present (Elmt) loop
10189 Install_Spec (Node (Elmt));
10190 Install_Formal_Packages (Node (Elmt));
10191 Next_Elmt (Elmt);
10192 end loop;
10193 end if;
10194
10195 if not In_Body then
10196 Push_Scope (S);
10197 end if;
10198 end Install_Parent;
10199
10200 -------------------------------
10201 -- Install_Hidden_Primitives --
10202 -------------------------------
10203
10204 procedure Install_Hidden_Primitives
10205 (Prims_List : in out Elist_Id;
10206 Gen_T : Entity_Id;
10207 Act_T : Entity_Id)
10208 is
10209 Elmt : Elmt_Id;
10210 List : Elist_Id := No_Elist;
10211 Prim_G_Elmt : Elmt_Id;
10212 Prim_A_Elmt : Elmt_Id;
10213 Prim_G : Node_Id;
10214 Prim_A : Node_Id;
10215
10216 begin
10217 -- No action needed in case of serious errors because we cannot trust
10218 -- in the order of primitives
10219
10220 if Serious_Errors_Detected > 0 then
10221 return;
10222
10223 -- No action possible if we don't have available the list of primitive
10224 -- operations
10225
10226 elsif No (Gen_T)
10227 or else not Is_Record_Type (Gen_T)
10228 or else not Is_Tagged_Type (Gen_T)
10229 or else not Is_Record_Type (Act_T)
10230 or else not Is_Tagged_Type (Act_T)
10231 then
10232 return;
10233
10234 -- There is no need to handle interface types since their primitives
10235 -- cannot be hidden
10236
10237 elsif Is_Interface (Gen_T) then
10238 return;
10239 end if;
10240
10241 Prim_G_Elmt := First_Elmt (Primitive_Operations (Gen_T));
10242
10243 if not Is_Class_Wide_Type (Act_T) then
10244 Prim_A_Elmt := First_Elmt (Primitive_Operations (Act_T));
10245 else
10246 Prim_A_Elmt := First_Elmt (Primitive_Operations (Root_Type (Act_T)));
10247 end if;
10248
10249 loop
10250 -- Skip predefined primitives in the generic formal
10251
10252 while Present (Prim_G_Elmt)
10253 and then Is_Predefined_Dispatching_Operation (Node (Prim_G_Elmt))
10254 loop
10255 Next_Elmt (Prim_G_Elmt);
10256 end loop;
10257
10258 -- Skip predefined primitives in the generic actual
10259
10260 while Present (Prim_A_Elmt)
10261 and then Is_Predefined_Dispatching_Operation (Node (Prim_A_Elmt))
10262 loop
10263 Next_Elmt (Prim_A_Elmt);
10264 end loop;
10265
10266 exit when No (Prim_G_Elmt) or else No (Prim_A_Elmt);
10267
10268 Prim_G := Node (Prim_G_Elmt);
10269 Prim_A := Node (Prim_A_Elmt);
10270
10271 -- There is no need to handle interface primitives because their
10272 -- primitives are not hidden
10273
10274 exit when Present (Interface_Alias (Prim_G));
10275
10276 -- Here we install one hidden primitive
10277
10278 if Chars (Prim_G) /= Chars (Prim_A)
10279 and then Has_Suffix (Prim_A, 'P')
10280 and then Remove_Suffix (Prim_A, 'P') = Chars (Prim_G)
10281 then
10282 Set_Chars (Prim_A, Chars (Prim_G));
10283 Append_New_Elmt (Prim_A, To => List);
10284 end if;
10285
10286 Next_Elmt (Prim_A_Elmt);
10287 Next_Elmt (Prim_G_Elmt);
10288 end loop;
10289
10290 -- Append the elements to the list of temporarily visible primitives
10291 -- avoiding duplicates.
10292
10293 if Present (List) then
10294 if No (Prims_List) then
10295 Prims_List := New_Elmt_List;
10296 end if;
10297
10298 Elmt := First_Elmt (List);
10299 while Present (Elmt) loop
10300 Append_Unique_Elmt (Node (Elmt), Prims_List);
10301 Next_Elmt (Elmt);
10302 end loop;
10303 end if;
10304 end Install_Hidden_Primitives;
10305
10306 -------------------------------
10307 -- Restore_Hidden_Primitives --
10308 -------------------------------
10309
10310 procedure Restore_Hidden_Primitives (Prims_List : in out Elist_Id) is
10311 Prim_Elmt : Elmt_Id;
10312 Prim : Node_Id;
10313
10314 begin
10315 if Prims_List /= No_Elist then
10316 Prim_Elmt := First_Elmt (Prims_List);
10317 while Present (Prim_Elmt) loop
10318 Prim := Node (Prim_Elmt);
10319 Set_Chars (Prim, Add_Suffix (Prim, 'P'));
10320 Next_Elmt (Prim_Elmt);
10321 end loop;
10322
10323 Prims_List := No_Elist;
10324 end if;
10325 end Restore_Hidden_Primitives;
10326
10327 --------------------------------
10328 -- Instantiate_Formal_Package --
10329 --------------------------------
10330
10331 function Instantiate_Formal_Package
10332 (Formal : Node_Id;
10333 Actual : Node_Id;
10334 Analyzed_Formal : Node_Id) return List_Id
10335 is
10336 Loc : constant Source_Ptr := Sloc (Actual);
10337 Hidden_Formals : constant Elist_Id := New_Elmt_List;
10338 Actual_Pack : Entity_Id;
10339 Formal_Pack : Entity_Id;
10340 Gen_Parent : Entity_Id;
10341 Decls : List_Id;
10342 Nod : Node_Id;
10343 Parent_Spec : Node_Id;
10344
10345 procedure Find_Matching_Actual
10346 (F : Node_Id;
10347 Act : in out Entity_Id);
10348 -- We need to associate each formal entity in the formal package with
10349 -- the corresponding entity in the actual package. The actual package
10350 -- has been analyzed and possibly expanded, and as a result there is
10351 -- no one-to-one correspondence between the two lists (for example,
10352 -- the actual may include subtypes, itypes, and inherited primitive
10353 -- operations, interspersed among the renaming declarations for the
10354 -- actuals). We retrieve the corresponding actual by name because each
10355 -- actual has the same name as the formal, and they do appear in the
10356 -- same order.
10357
10358 function Get_Formal_Entity (N : Node_Id) return Entity_Id;
10359 -- Retrieve entity of defining entity of generic formal parameter.
10360 -- Only the declarations of formals need to be considered when
10361 -- linking them to actuals, but the declarative list may include
10362 -- internal entities generated during analysis, and those are ignored.
10363
10364 procedure Match_Formal_Entity
10365 (Formal_Node : Node_Id;
10366 Formal_Ent : Entity_Id;
10367 Actual_Ent : Entity_Id);
10368 -- Associates the formal entity with the actual. In the case where
10369 -- Formal_Ent is a formal package, this procedure iterates through all
10370 -- of its formals and enters associations between the actuals occurring
10371 -- in the formal package's corresponding actual package (given by
10372 -- Actual_Ent) and the formal package's formal parameters. This
10373 -- procedure recurses if any of the parameters is itself a package.
10374
10375 function Is_Instance_Of
10376 (Act_Spec : Entity_Id;
10377 Gen_Anc : Entity_Id) return Boolean;
10378 -- The actual can be an instantiation of a generic within another
10379 -- instance, in which case there is no direct link from it to the
10380 -- original generic ancestor. In that case, we recognize that the
10381 -- ultimate ancestor is the same by examining names and scopes.
10382
10383 procedure Process_Nested_Formal (Formal : Entity_Id);
10384 -- If the current formal is declared with a box, its own formals are
10385 -- visible in the instance, as they were in the generic, and their
10386 -- Hidden flag must be reset. If some of these formals are themselves
10387 -- packages declared with a box, the processing must be recursive.
10388
10389 --------------------------
10390 -- Find_Matching_Actual --
10391 --------------------------
10392
10393 procedure Find_Matching_Actual
10394 (F : Node_Id;
10395 Act : in out Entity_Id)
10396 is
10397 Formal_Ent : Entity_Id;
10398
10399 begin
10400 case Nkind (Original_Node (F)) is
10401 when N_Formal_Object_Declaration
10402 | N_Formal_Type_Declaration
10403 =>
10404 Formal_Ent := Defining_Identifier (F);
10405
10406 while Chars (Act) /= Chars (Formal_Ent) loop
10407 Next_Entity (Act);
10408 end loop;
10409
10410 when N_Formal_Package_Declaration
10411 | N_Formal_Subprogram_Declaration
10412 | N_Generic_Package_Declaration
10413 | N_Package_Declaration
10414 =>
10415 Formal_Ent := Defining_Entity (F);
10416
10417 while Chars (Act) /= Chars (Formal_Ent) loop
10418 Next_Entity (Act);
10419 end loop;
10420
10421 when others =>
10422 raise Program_Error;
10423 end case;
10424 end Find_Matching_Actual;
10425
10426 -------------------------
10427 -- Match_Formal_Entity --
10428 -------------------------
10429
10430 procedure Match_Formal_Entity
10431 (Formal_Node : Node_Id;
10432 Formal_Ent : Entity_Id;
10433 Actual_Ent : Entity_Id)
10434 is
10435 Act_Pkg : Entity_Id;
10436
10437 begin
10438 Set_Instance_Of (Formal_Ent, Actual_Ent);
10439
10440 if Ekind (Actual_Ent) = E_Package then
10441
10442 -- Record associations for each parameter
10443
10444 Act_Pkg := Actual_Ent;
10445
10446 declare
10447 A_Ent : Entity_Id := First_Entity (Act_Pkg);
10448 F_Ent : Entity_Id;
10449 F_Node : Node_Id;
10450
10451 Gen_Decl : Node_Id;
10452 Formals : List_Id;
10453 Actual : Entity_Id;
10454
10455 begin
10456 -- Retrieve the actual given in the formal package declaration
10457
10458 Actual := Entity (Name (Original_Node (Formal_Node)));
10459
10460 -- The actual in the formal package declaration may be a
10461 -- renamed generic package, in which case we want to retrieve
10462 -- the original generic in order to traverse its formal part.
10463
10464 if Present (Renamed_Entity (Actual)) then
10465 Gen_Decl := Unit_Declaration_Node (Renamed_Entity (Actual));
10466 else
10467 Gen_Decl := Unit_Declaration_Node (Actual);
10468 end if;
10469
10470 Formals := Generic_Formal_Declarations (Gen_Decl);
10471
10472 if Present (Formals) then
10473 F_Node := First_Non_Pragma (Formals);
10474 else
10475 F_Node := Empty;
10476 end if;
10477
10478 while Present (A_Ent)
10479 and then Present (F_Node)
10480 and then A_Ent /= First_Private_Entity (Act_Pkg)
10481 loop
10482 F_Ent := Get_Formal_Entity (F_Node);
10483
10484 if Present (F_Ent) then
10485
10486 -- This is a formal of the original package. Record
10487 -- association and recurse.
10488
10489 Find_Matching_Actual (F_Node, A_Ent);
10490 Match_Formal_Entity (F_Node, F_Ent, A_Ent);
10491 Next_Entity (A_Ent);
10492 end if;
10493
10494 Next_Non_Pragma (F_Node);
10495 end loop;
10496 end;
10497 end if;
10498 end Match_Formal_Entity;
10499
10500 -----------------------
10501 -- Get_Formal_Entity --
10502 -----------------------
10503
10504 function Get_Formal_Entity (N : Node_Id) return Entity_Id is
10505 Kind : constant Node_Kind := Nkind (Original_Node (N));
10506 begin
10507 case Kind is
10508 when N_Formal_Object_Declaration =>
10509 return Defining_Identifier (N);
10510
10511 when N_Formal_Type_Declaration =>
10512 return Defining_Identifier (N);
10513
10514 when N_Formal_Subprogram_Declaration =>
10515 return Defining_Unit_Name (Specification (N));
10516
10517 when N_Formal_Package_Declaration =>
10518 return Defining_Identifier (Original_Node (N));
10519
10520 when N_Generic_Package_Declaration =>
10521 return Defining_Identifier (Original_Node (N));
10522
10523 -- All other declarations are introduced by semantic analysis and
10524 -- have no match in the actual.
10525
10526 when others =>
10527 return Empty;
10528 end case;
10529 end Get_Formal_Entity;
10530
10531 --------------------
10532 -- Is_Instance_Of --
10533 --------------------
10534
10535 function Is_Instance_Of
10536 (Act_Spec : Entity_Id;
10537 Gen_Anc : Entity_Id) return Boolean
10538 is
10539 Gen_Par : constant Entity_Id := Generic_Parent (Act_Spec);
10540
10541 begin
10542 if No (Gen_Par) then
10543 return False;
10544
10545 -- Simplest case: the generic parent of the actual is the formal
10546
10547 elsif Gen_Par = Gen_Anc then
10548 return True;
10549
10550 elsif Chars (Gen_Par) /= Chars (Gen_Anc) then
10551 return False;
10552
10553 -- The actual may be obtained through several instantiations. Its
10554 -- scope must itself be an instance of a generic declared in the
10555 -- same scope as the formal. Any other case is detected above.
10556
10557 elsif not Is_Generic_Instance (Scope (Gen_Par)) then
10558 return False;
10559
10560 else
10561 return Generic_Parent (Parent (Scope (Gen_Par))) = Scope (Gen_Anc);
10562 end if;
10563 end Is_Instance_Of;
10564
10565 ---------------------------
10566 -- Process_Nested_Formal --
10567 ---------------------------
10568
10569 procedure Process_Nested_Formal (Formal : Entity_Id) is
10570 Ent : Entity_Id;
10571
10572 begin
10573 if Present (Associated_Formal_Package (Formal))
10574 and then Box_Present (Parent (Associated_Formal_Package (Formal)))
10575 then
10576 Ent := First_Entity (Formal);
10577 while Present (Ent) loop
10578 Set_Is_Hidden (Ent, False);
10579 Set_Is_Visible_Formal (Ent);
10580 Set_Is_Potentially_Use_Visible
10581 (Ent, Is_Potentially_Use_Visible (Formal));
10582
10583 if Ekind (Ent) = E_Package then
10584 exit when Renamed_Entity (Ent) = Renamed_Entity (Formal);
10585 Process_Nested_Formal (Ent);
10586 end if;
10587
10588 Next_Entity (Ent);
10589 end loop;
10590 end if;
10591 end Process_Nested_Formal;
10592
10593 -- Start of processing for Instantiate_Formal_Package
10594
10595 begin
10596 Analyze (Actual);
10597
10598 -- The actual must be a package instance, or else a current instance
10599 -- such as a parent generic within the body of a generic child.
10600
10601 if not Is_Entity_Name (Actual)
10602 or else not Is_Package_Or_Generic_Package (Entity (Actual))
10603 then
10604 Error_Msg_N
10605 ("expect package instance to instantiate formal", Actual);
10606 Abandon_Instantiation (Actual);
10607 raise Program_Error;
10608
10609 else
10610 Actual_Pack := Entity (Actual);
10611 Set_Is_Instantiated (Actual_Pack);
10612
10613 -- The actual may be a renamed package, or an outer generic formal
10614 -- package whose instantiation is converted into a renaming.
10615
10616 if Present (Renamed_Object (Actual_Pack)) then
10617 Actual_Pack := Renamed_Object (Actual_Pack);
10618 end if;
10619
10620 if Nkind (Analyzed_Formal) = N_Formal_Package_Declaration then
10621 Gen_Parent := Get_Instance_Of (Entity (Name (Analyzed_Formal)));
10622 Formal_Pack := Defining_Identifier (Analyzed_Formal);
10623 else
10624 Gen_Parent :=
10625 Generic_Parent (Specification (Analyzed_Formal));
10626 Formal_Pack :=
10627 Defining_Unit_Name (Specification (Analyzed_Formal));
10628 end if;
10629
10630 if Nkind (Parent (Actual_Pack)) = N_Defining_Program_Unit_Name then
10631 Parent_Spec := Package_Specification (Actual_Pack);
10632 else
10633 Parent_Spec := Parent (Actual_Pack);
10634 end if;
10635
10636 if Gen_Parent = Any_Id then
10637 Error_Msg_N
10638 ("previous error in declaration of formal package", Actual);
10639 Abandon_Instantiation (Actual);
10640
10641 elsif Is_Instance_Of (Parent_Spec, Get_Instance_Of (Gen_Parent)) then
10642 null;
10643
10644 -- If this is the current instance of an enclosing generic, that unit
10645 -- is the generic package we need.
10646
10647 elsif In_Open_Scopes (Actual_Pack)
10648 and then Ekind (Actual_Pack) = E_Generic_Package
10649 then
10650 null;
10651
10652 else
10653 Error_Msg_NE
10654 ("actual parameter must be instance of&", Actual, Gen_Parent);
10655 Abandon_Instantiation (Actual);
10656 end if;
10657
10658 Set_Instance_Of (Defining_Identifier (Formal), Actual_Pack);
10659 Map_Formal_Package_Entities (Formal_Pack, Actual_Pack);
10660
10661 Nod :=
10662 Make_Package_Renaming_Declaration (Loc,
10663 Defining_Unit_Name => New_Copy (Defining_Identifier (Formal)),
10664 Name => New_Occurrence_Of (Actual_Pack, Loc));
10665
10666 Set_Associated_Formal_Package
10667 (Defining_Unit_Name (Nod), Defining_Identifier (Formal));
10668 Decls := New_List (Nod);
10669
10670 -- If the formal F has a box, then the generic declarations are
10671 -- visible in the generic G. In an instance of G, the corresponding
10672 -- entities in the actual for F (which are the actuals for the
10673 -- instantiation of the generic that F denotes) must also be made
10674 -- visible for analysis of the current instance. On exit from the
10675 -- current instance, those entities are made private again. If the
10676 -- actual is currently in use, these entities are also use-visible.
10677
10678 -- The loop through the actual entities also steps through the formal
10679 -- entities and enters associations from formals to actuals into the
10680 -- renaming map. This is necessary to properly handle checking of
10681 -- actual parameter associations for later formals that depend on
10682 -- actuals declared in the formal package.
10683
10684 -- In Ada 2005, partial parameterization requires that we make
10685 -- visible the actuals corresponding to formals that were defaulted
10686 -- in the formal package. There formals are identified because they
10687 -- remain formal generics within the formal package, rather than
10688 -- being renamings of the actuals supplied.
10689
10690 declare
10691 Gen_Decl : constant Node_Id :=
10692 Unit_Declaration_Node (Gen_Parent);
10693 Formals : constant List_Id :=
10694 Generic_Formal_Declarations (Gen_Decl);
10695
10696 Actual_Ent : Entity_Id;
10697 Actual_Of_Formal : Node_Id;
10698 Formal_Node : Node_Id;
10699 Formal_Ent : Entity_Id;
10700
10701 begin
10702 if Present (Formals) then
10703 Formal_Node := First_Non_Pragma (Formals);
10704 else
10705 Formal_Node := Empty;
10706 end if;
10707
10708 Actual_Ent := First_Entity (Actual_Pack);
10709 Actual_Of_Formal :=
10710 First (Visible_Declarations (Specification (Analyzed_Formal)));
10711 while Present (Actual_Ent)
10712 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
10713 loop
10714 if Present (Formal_Node) then
10715 Formal_Ent := Get_Formal_Entity (Formal_Node);
10716
10717 if Present (Formal_Ent) then
10718 Find_Matching_Actual (Formal_Node, Actual_Ent);
10719 Match_Formal_Entity (Formal_Node, Formal_Ent, Actual_Ent);
10720
10721 -- We iterate at the same time over the actuals of the
10722 -- local package created for the formal, to determine
10723 -- which one of the formals of the original generic were
10724 -- defaulted in the formal. The corresponding actual
10725 -- entities are visible in the enclosing instance.
10726
10727 if Box_Present (Formal)
10728 or else
10729 (Present (Actual_Of_Formal)
10730 and then
10731 Is_Generic_Formal
10732 (Get_Formal_Entity (Actual_Of_Formal)))
10733 then
10734 Set_Is_Hidden (Actual_Ent, False);
10735 Set_Is_Visible_Formal (Actual_Ent);
10736 Set_Is_Potentially_Use_Visible
10737 (Actual_Ent, In_Use (Actual_Pack));
10738
10739 if Ekind (Actual_Ent) = E_Package then
10740 Process_Nested_Formal (Actual_Ent);
10741 end if;
10742
10743 else
10744 if not Is_Hidden (Actual_Ent) then
10745 Append_Elmt (Actual_Ent, Hidden_Formals);
10746 end if;
10747
10748 Set_Is_Hidden (Actual_Ent);
10749 Set_Is_Potentially_Use_Visible (Actual_Ent, False);
10750 end if;
10751 end if;
10752
10753 Next_Non_Pragma (Formal_Node);
10754 Next (Actual_Of_Formal);
10755
10756 else
10757 -- No further formals to match, but the generic part may
10758 -- contain inherited operation that are not hidden in the
10759 -- enclosing instance.
10760
10761 Next_Entity (Actual_Ent);
10762 end if;
10763 end loop;
10764
10765 -- Inherited subprograms generated by formal derived types are
10766 -- also visible if the types are.
10767
10768 Actual_Ent := First_Entity (Actual_Pack);
10769 while Present (Actual_Ent)
10770 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
10771 loop
10772 if Is_Overloadable (Actual_Ent)
10773 and then
10774 Nkind (Parent (Actual_Ent)) = N_Subtype_Declaration
10775 and then
10776 not Is_Hidden (Defining_Identifier (Parent (Actual_Ent)))
10777 then
10778 Set_Is_Hidden (Actual_Ent, False);
10779 Set_Is_Potentially_Use_Visible
10780 (Actual_Ent, In_Use (Actual_Pack));
10781 end if;
10782
10783 Next_Entity (Actual_Ent);
10784 end loop;
10785
10786 -- No conformance to check if the generic has no formal parameters
10787 -- and the formal package has no generic associations.
10788
10789 if Is_Empty_List (Formals)
10790 and then
10791 (Box_Present (Formal)
10792 or else No (Generic_Associations (Formal)))
10793 then
10794 return Decls;
10795 end if;
10796 end;
10797
10798 -- If the formal is not declared with a box, reanalyze it as an
10799 -- abbreviated instantiation, to verify the matching rules of 12.7.
10800 -- The actual checks are performed after the generic associations
10801 -- have been analyzed, to guarantee the same visibility for this
10802 -- instantiation and for the actuals.
10803
10804 -- In Ada 2005, the generic associations for the formal can include
10805 -- defaulted parameters. These are ignored during check. This
10806 -- internal instantiation is removed from the tree after conformance
10807 -- checking, because it contains formal declarations for those
10808 -- defaulted parameters, and those should not reach the back-end.
10809
10810 if not Box_Present (Formal) then
10811 declare
10812 I_Pack : constant Entity_Id :=
10813 Make_Temporary (Sloc (Actual), 'P');
10814
10815 begin
10816 Set_Is_Internal (I_Pack);
10817 Set_Ekind (I_Pack, E_Package);
10818 Set_Hidden_In_Formal_Instance (I_Pack, Hidden_Formals);
10819
10820 Append_To (Decls,
10821 Make_Package_Instantiation (Sloc (Actual),
10822 Defining_Unit_Name => I_Pack,
10823 Name =>
10824 New_Occurrence_Of
10825 (Get_Instance_Of (Gen_Parent), Sloc (Actual)),
10826 Generic_Associations => Generic_Associations (Formal)));
10827 end;
10828 end if;
10829
10830 return Decls;
10831 end if;
10832 end Instantiate_Formal_Package;
10833
10834 -----------------------------------
10835 -- Instantiate_Formal_Subprogram --
10836 -----------------------------------
10837
10838 function Instantiate_Formal_Subprogram
10839 (Formal : Node_Id;
10840 Actual : Node_Id;
10841 Analyzed_Formal : Node_Id) return Node_Id
10842 is
10843 Analyzed_S : constant Entity_Id :=
10844 Defining_Unit_Name (Specification (Analyzed_Formal));
10845 Formal_Sub : constant Entity_Id :=
10846 Defining_Unit_Name (Specification (Formal));
10847
10848 function From_Parent_Scope (Subp : Entity_Id) return Boolean;
10849 -- If the generic is a child unit, the parent has been installed on the
10850 -- scope stack, but a default subprogram cannot resolve to something
10851 -- on the parent because that parent is not really part of the visible
10852 -- context (it is there to resolve explicit local entities). If the
10853 -- default has resolved in this way, we remove the entity from immediate
10854 -- visibility and analyze the node again to emit an error message or
10855 -- find another visible candidate.
10856
10857 procedure Valid_Actual_Subprogram (Act : Node_Id);
10858 -- Perform legality check and raise exception on failure
10859
10860 -----------------------
10861 -- From_Parent_Scope --
10862 -----------------------
10863
10864 function From_Parent_Scope (Subp : Entity_Id) return Boolean is
10865 Gen_Scope : Node_Id;
10866
10867 begin
10868 Gen_Scope := Scope (Analyzed_S);
10869 while Present (Gen_Scope) and then Is_Child_Unit (Gen_Scope) loop
10870 if Scope (Subp) = Scope (Gen_Scope) then
10871 return True;
10872 end if;
10873
10874 Gen_Scope := Scope (Gen_Scope);
10875 end loop;
10876
10877 return False;
10878 end From_Parent_Scope;
10879
10880 -----------------------------
10881 -- Valid_Actual_Subprogram --
10882 -----------------------------
10883
10884 procedure Valid_Actual_Subprogram (Act : Node_Id) is
10885 Act_E : Entity_Id;
10886
10887 begin
10888 if Is_Entity_Name (Act) then
10889 Act_E := Entity (Act);
10890
10891 elsif Nkind (Act) = N_Selected_Component
10892 and then Is_Entity_Name (Selector_Name (Act))
10893 then
10894 Act_E := Entity (Selector_Name (Act));
10895
10896 else
10897 Act_E := Empty;
10898 end if;
10899
10900 if (Present (Act_E) and then Is_Overloadable (Act_E))
10901 or else Nkind_In (Act, N_Attribute_Reference,
10902 N_Indexed_Component,
10903 N_Character_Literal,
10904 N_Explicit_Dereference)
10905 then
10906 return;
10907 end if;
10908
10909 Error_Msg_NE
10910 ("expect subprogram or entry name in instantiation of &",
10911 Instantiation_Node, Formal_Sub);
10912 Abandon_Instantiation (Instantiation_Node);
10913 end Valid_Actual_Subprogram;
10914
10915 -- Local variables
10916
10917 Decl_Node : Node_Id;
10918 Loc : Source_Ptr;
10919 Nam : Node_Id;
10920 New_Spec : Node_Id;
10921 New_Subp : Entity_Id;
10922
10923 -- Start of processing for Instantiate_Formal_Subprogram
10924
10925 begin
10926 New_Spec := New_Copy_Tree (Specification (Formal));
10927
10928 -- The tree copy has created the proper instantiation sloc for the
10929 -- new specification. Use this location for all other constructed
10930 -- declarations.
10931
10932 Loc := Sloc (Defining_Unit_Name (New_Spec));
10933
10934 -- Create new entity for the actual (New_Copy_Tree does not), and
10935 -- indicate that it is an actual.
10936
10937 -- If the actual is not an entity and the formal includes aspect
10938 -- specifications for contracts, we create an internal name for
10939 -- the renaming declaration. The constructed wrapper contains a
10940 -- call to the entity in the renaming.
10941
10942 if Ada_Version >= Ada_2020
10943 and then Present (Aspect_Specifications (Analyzed_Formal))
10944 then
10945 New_Subp := Make_Temporary (Sloc (Actual), 'S');
10946 Set_Defining_Unit_Name (New_Spec, New_Subp);
10947 else
10948 New_Subp := Make_Defining_Identifier (Loc, Chars (Formal_Sub));
10949 end if;
10950
10951 Set_Ekind (New_Subp, Ekind (Analyzed_S));
10952 Set_Is_Generic_Actual_Subprogram (New_Subp);
10953 Set_Defining_Unit_Name (New_Spec, New_Subp);
10954
10955 -- Create new entities for the each of the formals in the specification
10956 -- of the renaming declaration built for the actual.
10957
10958 if Present (Parameter_Specifications (New_Spec)) then
10959 declare
10960 F : Node_Id;
10961 F_Id : Entity_Id;
10962
10963 begin
10964 F := First (Parameter_Specifications (New_Spec));
10965 while Present (F) loop
10966 F_Id := Defining_Identifier (F);
10967
10968 Set_Defining_Identifier (F,
10969 Make_Defining_Identifier (Sloc (F_Id), Chars (F_Id)));
10970 Next (F);
10971 end loop;
10972 end;
10973 end if;
10974
10975 -- Find entity of actual. If the actual is an attribute reference, it
10976 -- cannot be resolved here (its formal is missing) but is handled
10977 -- instead in Attribute_Renaming. If the actual is overloaded, it is
10978 -- fully resolved subsequently, when the renaming declaration for the
10979 -- formal is analyzed. If it is an explicit dereference, resolve the
10980 -- prefix but not the actual itself, to prevent interpretation as call.
10981
10982 if Present (Actual) then
10983 Loc := Sloc (Actual);
10984 Set_Sloc (New_Spec, Loc);
10985
10986 if Nkind (Actual) = N_Operator_Symbol then
10987 Find_Direct_Name (Actual);
10988
10989 elsif Nkind (Actual) = N_Explicit_Dereference then
10990 Analyze (Prefix (Actual));
10991
10992 elsif Nkind (Actual) /= N_Attribute_Reference then
10993 Analyze (Actual);
10994 end if;
10995
10996 Valid_Actual_Subprogram (Actual);
10997 Nam := Actual;
10998
10999 elsif Present (Default_Name (Formal)) then
11000 if not Nkind_In (Default_Name (Formal), N_Attribute_Reference,
11001 N_Selected_Component,
11002 N_Indexed_Component,
11003 N_Character_Literal)
11004 and then Present (Entity (Default_Name (Formal)))
11005 then
11006 Nam := New_Occurrence_Of (Entity (Default_Name (Formal)), Loc);
11007 else
11008 Nam := New_Copy (Default_Name (Formal));
11009 Set_Sloc (Nam, Loc);
11010 end if;
11011
11012 elsif Box_Present (Formal) then
11013
11014 -- Actual is resolved at the point of instantiation. Create an
11015 -- identifier or operator with the same name as the formal.
11016
11017 if Nkind (Formal_Sub) = N_Defining_Operator_Symbol then
11018 Nam :=
11019 Make_Operator_Symbol (Loc,
11020 Chars => Chars (Formal_Sub),
11021 Strval => No_String);
11022 else
11023 Nam := Make_Identifier (Loc, Chars (Formal_Sub));
11024 end if;
11025
11026 elsif Nkind (Specification (Formal)) = N_Procedure_Specification
11027 and then Null_Present (Specification (Formal))
11028 then
11029 -- Generate null body for procedure, for use in the instance
11030
11031 Decl_Node :=
11032 Make_Subprogram_Body (Loc,
11033 Specification => New_Spec,
11034 Declarations => New_List,
11035 Handled_Statement_Sequence =>
11036 Make_Handled_Sequence_Of_Statements (Loc,
11037 Statements => New_List (Make_Null_Statement (Loc))));
11038
11039 -- RM 12.6 (16 2/2): The procedure has convention Intrinsic
11040
11041 Set_Convention (Defining_Unit_Name (New_Spec), Convention_Intrinsic);
11042
11043 -- Eliminate the calls to it when optimization is enabled
11044
11045 Set_Is_Inlined (Defining_Unit_Name (New_Spec));
11046 return Decl_Node;
11047
11048 else
11049 Error_Msg_Sloc := Sloc (Scope (Analyzed_S));
11050 Error_Msg_NE
11051 ("missing actual&", Instantiation_Node, Formal_Sub);
11052 Error_Msg_NE
11053 ("\in instantiation of & declared#",
11054 Instantiation_Node, Scope (Analyzed_S));
11055 Abandon_Instantiation (Instantiation_Node);
11056 end if;
11057
11058 Decl_Node :=
11059 Make_Subprogram_Renaming_Declaration (Loc,
11060 Specification => New_Spec,
11061 Name => Nam);
11062
11063 -- If we do not have an actual and the formal specified <> then set to
11064 -- get proper default.
11065
11066 if No (Actual) and then Box_Present (Formal) then
11067 Set_From_Default (Decl_Node);
11068 end if;
11069
11070 -- Gather possible interpretations for the actual before analyzing the
11071 -- instance. If overloaded, it will be resolved when analyzing the
11072 -- renaming declaration.
11073
11074 if Box_Present (Formal) and then No (Actual) then
11075 Analyze (Nam);
11076
11077 if Is_Child_Unit (Scope (Analyzed_S))
11078 and then Present (Entity (Nam))
11079 then
11080 if not Is_Overloaded (Nam) then
11081 if From_Parent_Scope (Entity (Nam)) then
11082 Set_Is_Immediately_Visible (Entity (Nam), False);
11083 Set_Entity (Nam, Empty);
11084 Set_Etype (Nam, Empty);
11085
11086 Analyze (Nam);
11087 Set_Is_Immediately_Visible (Entity (Nam));
11088 end if;
11089
11090 else
11091 declare
11092 I : Interp_Index;
11093 It : Interp;
11094
11095 begin
11096 Get_First_Interp (Nam, I, It);
11097 while Present (It.Nam) loop
11098 if From_Parent_Scope (It.Nam) then
11099 Remove_Interp (I);
11100 end if;
11101
11102 Get_Next_Interp (I, It);
11103 end loop;
11104 end;
11105 end if;
11106 end if;
11107 end if;
11108
11109 -- The generic instantiation freezes the actual. This can only be done
11110 -- once the actual is resolved, in the analysis of the renaming
11111 -- declaration. To make the formal subprogram entity available, we set
11112 -- Corresponding_Formal_Spec to point to the formal subprogram entity.
11113 -- This is also needed in Analyze_Subprogram_Renaming for the processing
11114 -- of formal abstract subprograms.
11115
11116 Set_Corresponding_Formal_Spec (Decl_Node, Analyzed_S);
11117
11118 -- We cannot analyze the renaming declaration, and thus find the actual,
11119 -- until all the actuals are assembled in the instance. For subsequent
11120 -- checks of other actuals, indicate the node that will hold the
11121 -- instance of this formal.
11122
11123 Set_Instance_Of (Analyzed_S, Nam);
11124
11125 if Nkind (Actual) = N_Selected_Component
11126 and then Is_Task_Type (Etype (Prefix (Actual)))
11127 and then not Is_Frozen (Etype (Prefix (Actual)))
11128 then
11129 -- The renaming declaration will create a body, which must appear
11130 -- outside of the instantiation, We move the renaming declaration
11131 -- out of the instance, and create an additional renaming inside,
11132 -- to prevent freezing anomalies.
11133
11134 declare
11135 Anon_Id : constant Entity_Id := Make_Temporary (Loc, 'E');
11136
11137 begin
11138 Set_Defining_Unit_Name (New_Spec, Anon_Id);
11139 Insert_Before (Instantiation_Node, Decl_Node);
11140 Analyze (Decl_Node);
11141
11142 -- Now create renaming within the instance
11143
11144 Decl_Node :=
11145 Make_Subprogram_Renaming_Declaration (Loc,
11146 Specification => New_Copy_Tree (New_Spec),
11147 Name => New_Occurrence_Of (Anon_Id, Loc));
11148
11149 Set_Defining_Unit_Name (Specification (Decl_Node),
11150 Make_Defining_Identifier (Loc, Chars (Formal_Sub)));
11151 end;
11152 end if;
11153
11154 return Decl_Node;
11155 end Instantiate_Formal_Subprogram;
11156
11157 ------------------------
11158 -- Instantiate_Object --
11159 ------------------------
11160
11161 function Instantiate_Object
11162 (Formal : Node_Id;
11163 Actual : Node_Id;
11164 Analyzed_Formal : Node_Id) return List_Id
11165 is
11166 Gen_Obj : constant Entity_Id := Defining_Identifier (Formal);
11167 A_Gen_Obj : constant Entity_Id :=
11168 Defining_Identifier (Analyzed_Formal);
11169 Acc_Def : Node_Id := Empty;
11170 Act_Assoc : constant Node_Id := Parent (Actual);
11171 Actual_Decl : Node_Id := Empty;
11172 Decl_Node : Node_Id;
11173 Def : Node_Id;
11174 Ftyp : Entity_Id;
11175 List : constant List_Id := New_List;
11176 Loc : constant Source_Ptr := Sloc (Actual);
11177 Orig_Ftyp : constant Entity_Id := Etype (A_Gen_Obj);
11178 Subt_Decl : Node_Id := Empty;
11179 Subt_Mark : Node_Id := Empty;
11180
11181 -- Start of processing for Instantiate_Object
11182
11183 begin
11184 -- Formal may be an anonymous access
11185
11186 if Present (Subtype_Mark (Formal)) then
11187 Subt_Mark := Subtype_Mark (Formal);
11188 else
11189 Check_Access_Definition (Formal);
11190 Acc_Def := Access_Definition (Formal);
11191 end if;
11192
11193 -- Sloc for error message on missing actual
11194
11195 Error_Msg_Sloc := Sloc (Scope (A_Gen_Obj));
11196
11197 if Get_Instance_Of (Gen_Obj) /= Gen_Obj then
11198 Error_Msg_N ("duplicate instantiation of generic parameter", Actual);
11199 end if;
11200
11201 Set_Parent (List, Parent (Actual));
11202
11203 -- OUT present
11204
11205 if Out_Present (Formal) then
11206
11207 -- An IN OUT generic actual must be a name. The instantiation is a
11208 -- renaming declaration. The actual is the name being renamed. We
11209 -- use the actual directly, rather than a copy, because it is not
11210 -- used further in the list of actuals, and because a copy or a use
11211 -- of relocate_node is incorrect if the instance is nested within a
11212 -- generic. In order to simplify e.g. ASIS queries, the
11213 -- Generic_Parent field links the declaration to the generic
11214 -- association.
11215
11216 if No (Actual) then
11217 Error_Msg_NE
11218 ("missing actual &",
11219 Instantiation_Node, Gen_Obj);
11220 Error_Msg_NE
11221 ("\in instantiation of & declared#",
11222 Instantiation_Node, Scope (A_Gen_Obj));
11223 Abandon_Instantiation (Instantiation_Node);
11224 end if;
11225
11226 if Present (Subt_Mark) then
11227 Decl_Node :=
11228 Make_Object_Renaming_Declaration (Loc,
11229 Defining_Identifier => New_Copy (Gen_Obj),
11230 Subtype_Mark => New_Copy_Tree (Subt_Mark),
11231 Name => Actual);
11232
11233 else pragma Assert (Present (Acc_Def));
11234 Decl_Node :=
11235 Make_Object_Renaming_Declaration (Loc,
11236 Defining_Identifier => New_Copy (Gen_Obj),
11237 Access_Definition => New_Copy_Tree (Acc_Def),
11238 Name => Actual);
11239 end if;
11240
11241 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
11242
11243 -- The analysis of the actual may produce Insert_Action nodes, so
11244 -- the declaration must have a context in which to attach them.
11245
11246 Append (Decl_Node, List);
11247 Analyze (Actual);
11248
11249 -- Return if the analysis of the actual reported some error
11250
11251 if Etype (Actual) = Any_Type then
11252 return List;
11253 end if;
11254
11255 -- This check is performed here because Analyze_Object_Renaming will
11256 -- not check it when Comes_From_Source is False. Note though that the
11257 -- check for the actual being the name of an object will be performed
11258 -- in Analyze_Object_Renaming.
11259
11260 if Is_Object_Reference (Actual)
11261 and then Is_Dependent_Component_Of_Mutable_Object (Actual)
11262 then
11263 Error_Msg_N
11264 ("illegal discriminant-dependent component for in out parameter",
11265 Actual);
11266 end if;
11267
11268 -- The actual has to be resolved in order to check that it is a
11269 -- variable (due to cases such as F (1), where F returns access to
11270 -- an array, and for overloaded prefixes).
11271
11272 Ftyp := Get_Instance_Of (Etype (A_Gen_Obj));
11273
11274 -- If the type of the formal is not itself a formal, and the current
11275 -- unit is a child unit, the formal type must be declared in a
11276 -- parent, and must be retrieved by visibility.
11277
11278 if Ftyp = Orig_Ftyp
11279 and then Is_Generic_Unit (Scope (Ftyp))
11280 and then Is_Child_Unit (Scope (A_Gen_Obj))
11281 then
11282 declare
11283 Temp : constant Node_Id :=
11284 New_Copy_Tree (Subtype_Mark (Analyzed_Formal));
11285 begin
11286 Set_Entity (Temp, Empty);
11287 Find_Type (Temp);
11288 Ftyp := Entity (Temp);
11289 end;
11290 end if;
11291
11292 if Is_Private_Type (Ftyp)
11293 and then not Is_Private_Type (Etype (Actual))
11294 and then (Base_Type (Full_View (Ftyp)) = Base_Type (Etype (Actual))
11295 or else Base_Type (Etype (Actual)) = Ftyp)
11296 then
11297 -- If the actual has the type of the full view of the formal, or
11298 -- else a non-private subtype of the formal, then the visibility
11299 -- of the formal type has changed. Add to the actuals a subtype
11300 -- declaration that will force the exchange of views in the body
11301 -- of the instance as well.
11302
11303 Subt_Decl :=
11304 Make_Subtype_Declaration (Loc,
11305 Defining_Identifier => Make_Temporary (Loc, 'P'),
11306 Subtype_Indication => New_Occurrence_Of (Ftyp, Loc));
11307
11308 Prepend (Subt_Decl, List);
11309
11310 Prepend_Elmt (Full_View (Ftyp), Exchanged_Views);
11311 Exchange_Declarations (Ftyp);
11312 end if;
11313
11314 Resolve (Actual, Ftyp);
11315
11316 if not Denotes_Variable (Actual) then
11317 Error_Msg_NE ("actual for& must be a variable", Actual, Gen_Obj);
11318
11319 elsif Base_Type (Ftyp) /= Base_Type (Etype (Actual)) then
11320
11321 -- Ada 2005 (AI-423): For a generic formal object of mode in out,
11322 -- the type of the actual shall resolve to a specific anonymous
11323 -- access type.
11324
11325 if Ada_Version < Ada_2005
11326 or else Ekind (Base_Type (Ftyp)) not in Anonymous_Access_Kind
11327 or else Ekind (Base_Type (Etype (Actual)))
11328 not in Anonymous_Access_Kind
11329 then
11330 Error_Msg_NE
11331 ("type of actual does not match type of&", Actual, Gen_Obj);
11332 end if;
11333 end if;
11334
11335 Note_Possible_Modification (Actual, Sure => True);
11336
11337 -- Check for instantiation with atomic/volatile object actual for
11338 -- nonatomic/nonvolatile formal (RM C.6 (12)).
11339
11340 if Is_Atomic_Object (Actual) and then not Is_Atomic (Orig_Ftyp) then
11341 Error_Msg_NE
11342 ("cannot instantiate nonatomic formal & of mode in out",
11343 Actual, Gen_Obj);
11344 Error_Msg_N ("\with atomic object actual (RM C.6(12))", Actual);
11345
11346 elsif Is_Volatile_Object (Actual) and then not Is_Volatile (Orig_Ftyp)
11347 then
11348 Error_Msg_NE
11349 ("cannot instantiate nonvolatile formal & of mode in out",
11350 Actual, Gen_Obj);
11351 Error_Msg_N ("\with volatile object actual (RM C.6(12))", Actual);
11352 end if;
11353
11354 -- Check for instantiation on nonatomic subcomponent of an atomic
11355 -- object in Ada 2020 (RM C.6 (13)).
11356
11357 if Ada_Version >= Ada_2020
11358 and then Is_Subcomponent_Of_Atomic_Object (Actual)
11359 and then not Is_Atomic_Object (Actual)
11360 then
11361 Error_Msg_NE
11362 ("cannot instantiate formal & of mode in out with actual",
11363 Actual, Gen_Obj);
11364 Error_Msg_N
11365 ("\nonatomic subcomponent of atomic object (RM C.6(13))",
11366 Actual);
11367 end if;
11368
11369 -- Check actual/formal compatibility with respect to the four
11370 -- volatility refinement aspects.
11371
11372 declare
11373 Actual_Obj : Entity_Id;
11374 N : Node_Id := Actual;
11375 begin
11376 -- Similar to Sem_Util.Get_Enclosing_Object, but treat
11377 -- pointer dereference like component selection.
11378 loop
11379 if Is_Entity_Name (N) then
11380 Actual_Obj := Entity (N);
11381 exit;
11382 end if;
11383
11384 case Nkind (N) is
11385 when N_Indexed_Component
11386 | N_Selected_Component
11387 | N_Slice
11388 | N_Explicit_Dereference
11389 =>
11390 N := Prefix (N);
11391
11392 when N_Type_Conversion =>
11393 N := Expression (N);
11394
11395 when others =>
11396 Actual_Obj := Etype (N);
11397 exit;
11398 end case;
11399 end loop;
11400
11401 Check_Volatility_Compatibility
11402 (Actual_Obj, A_Gen_Obj, "actual object",
11403 "its corresponding formal object of mode in out",
11404 Srcpos_Bearer => Actual);
11405 end;
11406
11407 -- Formal in-parameter
11408
11409 else
11410 -- The instantiation of a generic formal in-parameter is constant
11411 -- declaration. The actual is the expression for that declaration.
11412 -- Its type is a full copy of the type of the formal. This may be
11413 -- an access to subprogram, for which we need to generate entities
11414 -- for the formals in the new signature.
11415
11416 if Present (Actual) then
11417 if Present (Subt_Mark) then
11418 Def := New_Copy_Tree (Subt_Mark);
11419 else
11420 pragma Assert (Present (Acc_Def));
11421 Def := New_Copy_Tree (Acc_Def);
11422 end if;
11423
11424 Decl_Node :=
11425 Make_Object_Declaration (Loc,
11426 Defining_Identifier => New_Copy (Gen_Obj),
11427 Constant_Present => True,
11428 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
11429 Object_Definition => Def,
11430 Expression => Actual);
11431
11432 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
11433
11434 -- A generic formal object of a tagged type is defined to be
11435 -- aliased so the new constant must also be treated as aliased.
11436
11437 if Is_Tagged_Type (Etype (A_Gen_Obj)) then
11438 Set_Aliased_Present (Decl_Node);
11439 end if;
11440
11441 Append (Decl_Node, List);
11442
11443 -- No need to repeat (pre-)analysis of some expression nodes
11444 -- already handled in Preanalyze_Actuals.
11445
11446 if Nkind (Actual) /= N_Allocator then
11447 Analyze (Actual);
11448
11449 -- Return if the analysis of the actual reported some error
11450
11451 if Etype (Actual) = Any_Type then
11452 return List;
11453 end if;
11454 end if;
11455
11456 declare
11457 Formal_Type : constant Entity_Id := Etype (A_Gen_Obj);
11458 Typ : Entity_Id;
11459
11460 begin
11461 Typ := Get_Instance_Of (Formal_Type);
11462
11463 -- If the actual appears in the current or an enclosing scope,
11464 -- use its type directly. This is relevant if it has an actual
11465 -- subtype that is distinct from its nominal one. This cannot
11466 -- be done in general because the type of the actual may
11467 -- depend on other actuals, and only be fully determined when
11468 -- the enclosing instance is analyzed.
11469
11470 if Present (Etype (Actual))
11471 and then Is_Constr_Subt_For_U_Nominal (Etype (Actual))
11472 then
11473 Freeze_Before (Instantiation_Node, Etype (Actual));
11474 else
11475 Freeze_Before (Instantiation_Node, Typ);
11476 end if;
11477
11478 -- If the actual is an aggregate, perform name resolution on
11479 -- its components (the analysis of an aggregate does not do it)
11480 -- to capture local names that may be hidden if the generic is
11481 -- a child unit.
11482
11483 if Nkind (Actual) = N_Aggregate then
11484 Preanalyze_And_Resolve (Actual, Typ);
11485 end if;
11486
11487 if Is_Limited_Type (Typ)
11488 and then not OK_For_Limited_Init (Typ, Actual)
11489 then
11490 Error_Msg_N
11491 ("initialization not allowed for limited types", Actual);
11492 Explain_Limited_Type (Typ, Actual);
11493 end if;
11494 end;
11495
11496 elsif Present (Default_Expression (Formal)) then
11497
11498 -- Use default to construct declaration
11499
11500 if Present (Subt_Mark) then
11501 Def := New_Copy (Subt_Mark);
11502 else
11503 pragma Assert (Present (Acc_Def));
11504 Def := New_Copy_Tree (Acc_Def);
11505 end if;
11506
11507 Decl_Node :=
11508 Make_Object_Declaration (Sloc (Formal),
11509 Defining_Identifier => New_Copy (Gen_Obj),
11510 Constant_Present => True,
11511 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
11512 Object_Definition => Def,
11513 Expression => New_Copy_Tree
11514 (Default_Expression (Formal)));
11515
11516 Set_Corresponding_Generic_Association
11517 (Decl_Node, Expression (Decl_Node));
11518
11519 Append (Decl_Node, List);
11520 Set_Analyzed (Expression (Decl_Node), False);
11521
11522 else
11523 Error_Msg_NE ("missing actual&", Instantiation_Node, Gen_Obj);
11524 Error_Msg_NE ("\in instantiation of & declared#",
11525 Instantiation_Node, Scope (A_Gen_Obj));
11526
11527 if Is_Scalar_Type (Etype (A_Gen_Obj)) then
11528
11529 -- Create dummy constant declaration so that instance can be
11530 -- analyzed, to minimize cascaded visibility errors.
11531
11532 if Present (Subt_Mark) then
11533 Def := Subt_Mark;
11534 else pragma Assert (Present (Acc_Def));
11535 Def := Acc_Def;
11536 end if;
11537
11538 Decl_Node :=
11539 Make_Object_Declaration (Loc,
11540 Defining_Identifier => New_Copy (Gen_Obj),
11541 Constant_Present => True,
11542 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
11543 Object_Definition => New_Copy (Def),
11544 Expression =>
11545 Make_Attribute_Reference (Sloc (Gen_Obj),
11546 Attribute_Name => Name_First,
11547 Prefix => New_Copy (Def)));
11548
11549 Append (Decl_Node, List);
11550
11551 else
11552 Abandon_Instantiation (Instantiation_Node);
11553 end if;
11554 end if;
11555 end if;
11556
11557 if Nkind (Actual) in N_Has_Entity then
11558 Actual_Decl := Parent (Entity (Actual));
11559 end if;
11560
11561 -- Ada 2005 (AI-423) refined by AI12-0287:
11562 -- For an object_renaming_declaration with a null_exclusion or an
11563 -- access_definition that has a null_exclusion, the subtype of the
11564 -- object_name shall exclude null. In addition, if the
11565 -- object_renaming_declaration occurs within the body of a generic unit
11566 -- G or within the body of a generic unit declared within the
11567 -- declarative region of generic unit G, then:
11568 -- * if the object_name statically denotes a generic formal object of
11569 -- mode in out of G, then the declaration of that object shall have a
11570 -- null_exclusion;
11571 -- * if the object_name statically denotes a call of a generic formal
11572 -- function of G, then the declaration of the result of that function
11573 -- shall have a null_exclusion.
11574
11575 if Ada_Version >= Ada_2005
11576 and then Present (Actual_Decl)
11577 and then Nkind_In (Actual_Decl, N_Formal_Object_Declaration,
11578 N_Object_Declaration)
11579 and then Nkind (Analyzed_Formal) = N_Formal_Object_Declaration
11580 and then not Has_Null_Exclusion (Actual_Decl)
11581 and then Has_Null_Exclusion (Analyzed_Formal)
11582 and then Ekind (Defining_Identifier (Analyzed_Formal))
11583 = E_Generic_In_Out_Parameter
11584 and then ((In_Generic_Scope (Entity (Actual))
11585 and then In_Package_Body (Scope (Entity (Actual))))
11586 or else not Can_Never_Be_Null (Etype (Actual)))
11587 then
11588 Error_Msg_Sloc := Sloc (Analyzed_Formal);
11589 Error_Msg_N
11590 ("actual must exclude null to match generic formal#", Actual);
11591 end if;
11592
11593 -- An effectively volatile object cannot be used as an actual in a
11594 -- generic instantiation (SPARK RM 7.1.3(7)). The following check is
11595 -- relevant only when SPARK_Mode is on as it is not a standard Ada
11596 -- legality rule, and also verifies that the actual is an object.
11597
11598 if SPARK_Mode = On
11599 and then Present (Actual)
11600 and then Is_Object_Reference (Actual)
11601 and then Is_Effectively_Volatile_Object (Actual)
11602 and then not Is_Effectively_Volatile (A_Gen_Obj)
11603 then
11604 Error_Msg_N
11605 ("volatile object cannot act as actual in generic instantiation",
11606 Actual);
11607 end if;
11608
11609 return List;
11610 end Instantiate_Object;
11611
11612 ------------------------------
11613 -- Instantiate_Package_Body --
11614 ------------------------------
11615
11616 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
11617 -- must be replaced by gotos which jump to the end of the routine in order
11618 -- to restore the Ghost and SPARK modes.
11619
11620 procedure Instantiate_Package_Body
11621 (Body_Info : Pending_Body_Info;
11622 Inlined_Body : Boolean := False;
11623 Body_Optional : Boolean := False)
11624 is
11625 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
11626 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Decl);
11627 Act_Spec : constant Node_Id := Specification (Act_Decl);
11628 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
11629 Gen_Id : constant Node_Id := Name (Inst_Node);
11630 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
11631 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
11632 Loc : constant Source_Ptr := Sloc (Inst_Node);
11633
11634 procedure Check_Initialized_Types;
11635 -- In a generic package body, an entity of a generic private type may
11636 -- appear uninitialized. This is suspicious, unless the actual is a
11637 -- fully initialized type.
11638
11639 -----------------------------
11640 -- Check_Initialized_Types --
11641 -----------------------------
11642
11643 procedure Check_Initialized_Types is
11644 Decl : Node_Id;
11645 Formal : Entity_Id;
11646 Actual : Entity_Id;
11647 Uninit_Var : Entity_Id;
11648
11649 begin
11650 Decl := First (Generic_Formal_Declarations (Gen_Decl));
11651 while Present (Decl) loop
11652 Uninit_Var := Empty;
11653
11654 if Nkind (Decl) = N_Private_Extension_Declaration then
11655 Uninit_Var := Uninitialized_Variable (Decl);
11656
11657 elsif Nkind (Decl) = N_Formal_Type_Declaration
11658 and then Nkind (Formal_Type_Definition (Decl)) =
11659 N_Formal_Private_Type_Definition
11660 then
11661 Uninit_Var :=
11662 Uninitialized_Variable (Formal_Type_Definition (Decl));
11663 end if;
11664
11665 if Present (Uninit_Var) then
11666 Formal := Defining_Identifier (Decl);
11667 Actual := First_Entity (Act_Decl_Id);
11668
11669 -- For each formal there is a subtype declaration that renames
11670 -- the actual and has the same name as the formal. Locate the
11671 -- formal for warning message about uninitialized variables
11672 -- in the generic, for which the actual type should be a fully
11673 -- initialized type.
11674
11675 while Present (Actual) loop
11676 exit when Ekind (Actual) = E_Package
11677 and then Present (Renamed_Object (Actual));
11678
11679 if Chars (Actual) = Chars (Formal)
11680 and then not Is_Scalar_Type (Actual)
11681 and then not Is_Fully_Initialized_Type (Actual)
11682 and then Warn_On_No_Value_Assigned
11683 then
11684 Error_Msg_Node_2 := Formal;
11685 Error_Msg_NE
11686 ("generic unit has uninitialized variable& of "
11687 & "formal private type &?v?", Actual, Uninit_Var);
11688 Error_Msg_NE
11689 ("actual type for& should be fully initialized type?v?",
11690 Actual, Formal);
11691 exit;
11692 end if;
11693
11694 Next_Entity (Actual);
11695 end loop;
11696 end if;
11697
11698 Next (Decl);
11699 end loop;
11700 end Check_Initialized_Types;
11701
11702 -- Local variables
11703
11704 -- The following constants capture the context prior to instantiating
11705 -- the package body.
11706
11707 Saved_CS : constant Config_Switches_Type := Save_Config_Switches;
11708 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
11709 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
11710 Saved_ISMP : constant Boolean :=
11711 Ignore_SPARK_Mode_Pragmas_In_Instance;
11712 Saved_LSST : constant Suppress_Stack_Entry_Ptr :=
11713 Local_Suppress_Stack_Top;
11714 Saved_SC : constant Boolean := Style_Check;
11715 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
11716 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
11717 Saved_SS : constant Suppress_Record := Scope_Suppress;
11718 Saved_Warn : constant Warning_Record := Save_Warnings;
11719
11720 Act_Body : Node_Id;
11721 Act_Body_Id : Entity_Id;
11722 Act_Body_Name : Node_Id;
11723 Gen_Body : Node_Id;
11724 Gen_Body_Id : Node_Id;
11725 Par_Ent : Entity_Id := Empty;
11726 Par_Installed : Boolean := False;
11727 Par_Vis : Boolean := False;
11728
11729 Vis_Prims_List : Elist_Id := No_Elist;
11730 -- List of primitives made temporarily visible in the instantiation
11731 -- to match the visibility of the formal type.
11732
11733 -- Start of processing for Instantiate_Package_Body
11734
11735 begin
11736 Gen_Body_Id := Corresponding_Body (Gen_Decl);
11737
11738 -- The instance body may already have been processed, as the parent of
11739 -- another instance that is inlined (Load_Parent_Of_Generic).
11740
11741 if Present (Corresponding_Body (Instance_Spec (Inst_Node))) then
11742 return;
11743 end if;
11744
11745 -- The package being instantiated may be subject to pragma Ghost. Set
11746 -- the mode now to ensure that any nodes generated during instantiation
11747 -- are properly marked as Ghost.
11748
11749 Set_Ghost_Mode (Act_Decl_Id);
11750
11751 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
11752
11753 -- Re-establish the state of information on which checks are suppressed.
11754 -- This information was set in Body_Info at the point of instantiation,
11755 -- and now we restore it so that the instance is compiled using the
11756 -- check status at the instantiation (RM 11.5(7.2/2), AI95-00224-01).
11757
11758 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
11759 Scope_Suppress := Body_Info.Scope_Suppress;
11760
11761 Restore_Config_Switches (Body_Info.Config_Switches);
11762 Restore_Warnings (Body_Info.Warnings);
11763
11764 if No (Gen_Body_Id) then
11765
11766 -- Do not look for parent of generic body if none is required.
11767 -- This may happen when the routine is called as part of the
11768 -- Pending_Instantiations processing, when nested instances
11769 -- may precede the one generated from the main unit.
11770
11771 if not Unit_Requires_Body (Defining_Entity (Gen_Decl))
11772 and then Body_Optional
11773 then
11774 goto Leave;
11775 else
11776 Load_Parent_Of_Generic
11777 (Inst_Node, Specification (Gen_Decl), Body_Optional);
11778
11779 -- Surprisingly enough, loading the body of the parent can cause
11780 -- the body to be instantiated and the double instantiation needs
11781 -- to be prevented in order to avoid giving bogus semantic errors.
11782
11783 -- This case can occur because of the Collect_Previous_Instances
11784 -- machinery of Load_Parent_Of_Generic, which will instantiate
11785 -- bodies that are deemed to be ahead of the body of the parent
11786 -- in the compilation unit. But the relative position of these
11787 -- bodies is computed using the mere comparison of their Sloc.
11788
11789 -- Now suppose that you have two generic packages G and H, with
11790 -- G containing a mere instantiation of H:
11791
11792 -- generic
11793 -- package H is
11794
11795 -- generic
11796 -- package Nested_G is
11797 -- ...
11798 -- end Nested_G;
11799
11800 -- end H;
11801
11802 -- with H;
11803
11804 -- generic
11805 -- package G is
11806
11807 -- package My_H is new H;
11808
11809 -- end G;
11810
11811 -- and a third package Q instantiating G and Nested_G:
11812
11813 -- with G;
11814
11815 -- package Q is
11816
11817 -- package My_G is new G;
11818
11819 -- package My_Nested_G is new My_G.My_H.Nested_G;
11820
11821 -- end Q;
11822
11823 -- The body to be instantiated is that of My_Nested_G and its
11824 -- parent is the instance My_G.My_H. This latter instantiation
11825 -- is done when My_G is analyzed, i.e. after the declarations
11826 -- of My_G and My_Nested_G have been parsed; as a result, the
11827 -- Sloc of My_G.My_H is greater than the Sloc of My_Nested_G.
11828
11829 -- Therefore loading the body of My_G.My_H will cause the body
11830 -- of My_Nested_G to be instantiated because it is deemed to be
11831 -- ahead of My_G.My_H. This means that Load_Parent_Of_Generic
11832 -- will again be invoked on My_G.My_H, but this time with the
11833 -- Collect_Previous_Instances machinery disabled, so there is
11834 -- no endless mutual recursion and things are done in order.
11835
11836 if Present (Corresponding_Body (Instance_Spec (Inst_Node))) then
11837 goto Leave;
11838 end if;
11839
11840 Gen_Body_Id := Corresponding_Body (Gen_Decl);
11841 end if;
11842 end if;
11843
11844 -- Establish global variable for sloc adjustment and for error recovery
11845 -- In the case of an instance body for an instantiation with actuals
11846 -- from a limited view, the instance body is placed at the beginning
11847 -- of the enclosing package body: use the body entity as the source
11848 -- location for nodes of the instance body.
11849
11850 if not Is_Empty_Elmt_List (Incomplete_Actuals (Act_Decl_Id)) then
11851 declare
11852 Scop : constant Entity_Id := Scope (Act_Decl_Id);
11853 Body_Id : constant Node_Id :=
11854 Corresponding_Body (Unit_Declaration_Node (Scop));
11855
11856 begin
11857 Instantiation_Node := Body_Id;
11858 end;
11859 else
11860 Instantiation_Node := Inst_Node;
11861 end if;
11862
11863 if Present (Gen_Body_Id) then
11864 Save_Env (Gen_Unit, Act_Decl_Id);
11865 Style_Check := False;
11866
11867 -- If the context of the instance is subject to SPARK_Mode "off", the
11868 -- annotation is missing, or the body is instantiated at a later pass
11869 -- and its spec ignored SPARK_Mode pragma, set the global flag which
11870 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within the
11871 -- instance.
11872
11873 if SPARK_Mode /= On
11874 or else Ignore_SPARK_Mode_Pragmas (Act_Decl_Id)
11875 then
11876 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
11877 end if;
11878
11879 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
11880 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
11881
11882 Create_Instantiation_Source
11883 (Inst_Node, Gen_Body_Id, S_Adjustment);
11884
11885 Act_Body :=
11886 Copy_Generic_Node
11887 (Original_Node (Gen_Body), Empty, Instantiating => True);
11888
11889 -- Create proper (possibly qualified) defining name for the body, to
11890 -- correspond to the one in the spec.
11891
11892 Act_Body_Id :=
11893 Make_Defining_Identifier (Sloc (Act_Decl_Id), Chars (Act_Decl_Id));
11894 Set_Comes_From_Source (Act_Body_Id, Comes_From_Source (Act_Decl_Id));
11895
11896 -- Some attributes of spec entity are not inherited by body entity
11897
11898 Set_Handler_Records (Act_Body_Id, No_List);
11899
11900 if Nkind (Defining_Unit_Name (Act_Spec)) =
11901 N_Defining_Program_Unit_Name
11902 then
11903 Act_Body_Name :=
11904 Make_Defining_Program_Unit_Name (Loc,
11905 Name =>
11906 New_Copy_Tree (Name (Defining_Unit_Name (Act_Spec))),
11907 Defining_Identifier => Act_Body_Id);
11908 else
11909 Act_Body_Name := Act_Body_Id;
11910 end if;
11911
11912 Set_Defining_Unit_Name (Act_Body, Act_Body_Name);
11913
11914 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
11915 Check_Generic_Actuals (Act_Decl_Id, False);
11916 Check_Initialized_Types;
11917
11918 -- Install primitives hidden at the point of the instantiation but
11919 -- visible when processing the generic formals
11920
11921 declare
11922 E : Entity_Id;
11923
11924 begin
11925 E := First_Entity (Act_Decl_Id);
11926 while Present (E) loop
11927 if Is_Type (E)
11928 and then not Is_Itype (E)
11929 and then Is_Generic_Actual_Type (E)
11930 and then Is_Tagged_Type (E)
11931 then
11932 Install_Hidden_Primitives
11933 (Prims_List => Vis_Prims_List,
11934 Gen_T => Generic_Parent_Type (Parent (E)),
11935 Act_T => E);
11936 end if;
11937
11938 Next_Entity (E);
11939 end loop;
11940 end;
11941
11942 -- If it is a child unit, make the parent instance (which is an
11943 -- instance of the parent of the generic) visible. The parent
11944 -- instance is the prefix of the name of the generic unit.
11945
11946 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
11947 and then Nkind (Gen_Id) = N_Expanded_Name
11948 then
11949 Par_Ent := Entity (Prefix (Gen_Id));
11950 Par_Vis := Is_Immediately_Visible (Par_Ent);
11951 Install_Parent (Par_Ent, In_Body => True);
11952 Par_Installed := True;
11953
11954 elsif Is_Child_Unit (Gen_Unit) then
11955 Par_Ent := Scope (Gen_Unit);
11956 Par_Vis := Is_Immediately_Visible (Par_Ent);
11957 Install_Parent (Par_Ent, In_Body => True);
11958 Par_Installed := True;
11959 end if;
11960
11961 -- If the instantiation is a library unit, and this is the main unit,
11962 -- then build the resulting compilation unit nodes for the instance.
11963 -- If this is a compilation unit but it is not the main unit, then it
11964 -- is the body of a unit in the context, that is being compiled
11965 -- because it is encloses some inlined unit or another generic unit
11966 -- being instantiated. In that case, this body is not part of the
11967 -- current compilation, and is not attached to the tree, but its
11968 -- parent must be set for analysis.
11969
11970 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
11971
11972 -- Replace instance node with body of instance, and create new
11973 -- node for corresponding instance declaration.
11974
11975 Build_Instance_Compilation_Unit_Nodes
11976 (Inst_Node, Act_Body, Act_Decl);
11977 Analyze (Inst_Node);
11978
11979 if Parent (Inst_Node) = Cunit (Main_Unit) then
11980
11981 -- If the instance is a child unit itself, then set the scope
11982 -- of the expanded body to be the parent of the instantiation
11983 -- (ensuring that the fully qualified name will be generated
11984 -- for the elaboration subprogram).
11985
11986 if Nkind (Defining_Unit_Name (Act_Spec)) =
11987 N_Defining_Program_Unit_Name
11988 then
11989 Set_Scope (Defining_Entity (Inst_Node), Scope (Act_Decl_Id));
11990 end if;
11991 end if;
11992
11993 -- Case where instantiation is not a library unit
11994
11995 else
11996 -- If this is an early instantiation, i.e. appears textually
11997 -- before the corresponding body and must be elaborated first,
11998 -- indicate that the body instance is to be delayed.
11999
12000 Install_Body (Act_Body, Inst_Node, Gen_Body, Gen_Decl);
12001 Analyze (Act_Body);
12002 end if;
12003
12004 Inherit_Context (Gen_Body, Inst_Node);
12005
12006 -- Remove the parent instances if they have been placed on the scope
12007 -- stack to compile the body.
12008
12009 if Par_Installed then
12010 Remove_Parent (In_Body => True);
12011
12012 -- Restore the previous visibility of the parent
12013
12014 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
12015 end if;
12016
12017 Restore_Hidden_Primitives (Vis_Prims_List);
12018 Restore_Private_Views (Act_Decl_Id);
12019
12020 -- Remove the current unit from visibility if this is an instance
12021 -- that is not elaborated on the fly for inlining purposes.
12022
12023 if not Inlined_Body then
12024 Set_Is_Immediately_Visible (Act_Decl_Id, False);
12025 end if;
12026
12027 Restore_Env;
12028
12029 -- If we have no body, and the unit requires a body, then complain. This
12030 -- complaint is suppressed if we have detected other errors (since a
12031 -- common reason for missing the body is that it had errors).
12032 -- In CodePeer mode, a warning has been emitted already, no need for
12033 -- further messages.
12034
12035 elsif Unit_Requires_Body (Gen_Unit)
12036 and then not Body_Optional
12037 then
12038 if CodePeer_Mode then
12039 null;
12040
12041 elsif Serious_Errors_Detected = 0 then
12042 Error_Msg_NE
12043 ("cannot find body of generic package &", Inst_Node, Gen_Unit);
12044
12045 -- Don't attempt to perform any cleanup actions if some other error
12046 -- was already detected, since this can cause blowups.
12047
12048 else
12049 goto Leave;
12050 end if;
12051
12052 -- Case of package that does not need a body
12053
12054 else
12055 -- If the instantiation of the declaration is a library unit, rewrite
12056 -- the original package instantiation as a package declaration in the
12057 -- compilation unit node.
12058
12059 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
12060 Set_Parent_Spec (Act_Decl, Parent_Spec (Inst_Node));
12061 Rewrite (Inst_Node, Act_Decl);
12062
12063 -- Generate elaboration entity, in case spec has elaboration code.
12064 -- This cannot be done when the instance is analyzed, because it
12065 -- is not known yet whether the body exists.
12066
12067 Set_Elaboration_Entity_Required (Act_Decl_Id, False);
12068 Build_Elaboration_Entity (Parent (Inst_Node), Act_Decl_Id);
12069
12070 -- If the instantiation is not a library unit, then append the
12071 -- declaration to the list of implicitly generated entities, unless
12072 -- it is already a list member which means that it was already
12073 -- processed
12074
12075 elsif not Is_List_Member (Act_Decl) then
12076 Mark_Rewrite_Insertion (Act_Decl);
12077 Insert_Before (Inst_Node, Act_Decl);
12078 end if;
12079 end if;
12080
12081 <<Leave>>
12082
12083 -- Restore the context that was in effect prior to instantiating the
12084 -- package 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_Package_Body;
12097
12098 ---------------------------------
12099 -- Instantiate_Subprogram_Body --
12100 ---------------------------------
12101
12102 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
12103 -- must be replaced by gotos which jump to the end of the routine in order
12104 -- to restore the Ghost and SPARK modes.
12105
12106 procedure Instantiate_Subprogram_Body
12107 (Body_Info : Pending_Body_Info;
12108 Body_Optional : Boolean := False)
12109 is
12110 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
12111 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Decl);
12112 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
12113 Gen_Id : constant Node_Id := Name (Inst_Node);
12114 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
12115 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
12116 Loc : constant Source_Ptr := Sloc (Inst_Node);
12117 Pack_Id : constant Entity_Id :=
12118 Defining_Unit_Name (Parent (Act_Decl));
12119
12120 -- The following constants capture the context prior to instantiating
12121 -- the subprogram body.
12122
12123 Saved_CS : constant Config_Switches_Type := Save_Config_Switches;
12124 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
12125 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
12126 Saved_ISMP : constant Boolean :=
12127 Ignore_SPARK_Mode_Pragmas_In_Instance;
12128 Saved_LSST : constant Suppress_Stack_Entry_Ptr :=
12129 Local_Suppress_Stack_Top;
12130 Saved_SC : constant Boolean := Style_Check;
12131 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
12132 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
12133 Saved_SS : constant Suppress_Record := Scope_Suppress;
12134 Saved_Warn : constant Warning_Record := Save_Warnings;
12135
12136 Act_Body : Node_Id;
12137 Act_Body_Id : Entity_Id;
12138 Gen_Body : Node_Id;
12139 Gen_Body_Id : Node_Id;
12140 Pack_Body : Node_Id;
12141 Par_Ent : Entity_Id := Empty;
12142 Par_Installed : Boolean := False;
12143 Par_Vis : Boolean := False;
12144 Ret_Expr : Node_Id;
12145
12146 begin
12147 Gen_Body_Id := Corresponding_Body (Gen_Decl);
12148
12149 -- Subprogram body may have been created already because of an inline
12150 -- pragma, or because of multiple elaborations of the enclosing package
12151 -- when several instances of the subprogram appear in the main unit.
12152
12153 if Present (Corresponding_Body (Act_Decl)) then
12154 return;
12155 end if;
12156
12157 -- The subprogram being instantiated may be subject to pragma Ghost. Set
12158 -- the mode now to ensure that any nodes generated during instantiation
12159 -- are properly marked as Ghost.
12160
12161 Set_Ghost_Mode (Act_Decl_Id);
12162
12163 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
12164
12165 -- Re-establish the state of information on which checks are suppressed.
12166 -- This information was set in Body_Info at the point of instantiation,
12167 -- and now we restore it so that the instance is compiled using the
12168 -- check status at the instantiation (RM 11.5(7.2/2), AI95-00224-01).
12169
12170 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
12171 Scope_Suppress := Body_Info.Scope_Suppress;
12172
12173 Restore_Config_Switches (Body_Info.Config_Switches);
12174 Restore_Warnings (Body_Info.Warnings);
12175
12176 if No (Gen_Body_Id) then
12177
12178 -- For imported generic subprogram, no body to compile, complete
12179 -- the spec entity appropriately.
12180
12181 if Is_Imported (Gen_Unit) then
12182 Set_Is_Imported (Act_Decl_Id);
12183 Set_First_Rep_Item (Act_Decl_Id, First_Rep_Item (Gen_Unit));
12184 Set_Interface_Name (Act_Decl_Id, Interface_Name (Gen_Unit));
12185 Set_Convention (Act_Decl_Id, Convention (Gen_Unit));
12186 Set_Has_Completion (Act_Decl_Id);
12187 goto Leave;
12188
12189 -- For other cases, compile the body
12190
12191 else
12192 Load_Parent_Of_Generic
12193 (Inst_Node, Specification (Gen_Decl), Body_Optional);
12194 Gen_Body_Id := Corresponding_Body (Gen_Decl);
12195 end if;
12196 end if;
12197
12198 Instantiation_Node := Inst_Node;
12199
12200 if Present (Gen_Body_Id) then
12201 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
12202
12203 if Nkind (Gen_Body) = N_Subprogram_Body_Stub then
12204
12205 -- Either body is not present, or context is non-expanding, as
12206 -- when compiling a subunit. Mark the instance as completed, and
12207 -- diagnose a missing body when needed.
12208
12209 if Expander_Active
12210 and then Operating_Mode = Generate_Code
12211 then
12212 Error_Msg_N ("missing proper body for instantiation", Gen_Body);
12213 end if;
12214
12215 Set_Has_Completion (Act_Decl_Id);
12216 goto Leave;
12217 end if;
12218
12219 Save_Env (Gen_Unit, Act_Decl_Id);
12220 Style_Check := False;
12221
12222 -- If the context of the instance is subject to SPARK_Mode "off", the
12223 -- annotation is missing, or the body is instantiated at a later pass
12224 -- and its spec ignored SPARK_Mode pragma, set the global flag which
12225 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within the
12226 -- instance.
12227
12228 if SPARK_Mode /= On
12229 or else Ignore_SPARK_Mode_Pragmas (Act_Decl_Id)
12230 then
12231 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
12232 end if;
12233
12234 -- If the context of an instance is not subject to SPARK_Mode "off",
12235 -- and the generic body is subject to an explicit SPARK_Mode pragma,
12236 -- the latter should be the one applicable to the instance.
12237
12238 if not Ignore_SPARK_Mode_Pragmas_In_Instance
12239 and then SPARK_Mode /= Off
12240 and then Present (SPARK_Pragma (Gen_Body_Id))
12241 then
12242 Set_SPARK_Mode (Gen_Body_Id);
12243 end if;
12244
12245 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
12246 Create_Instantiation_Source
12247 (Inst_Node,
12248 Gen_Body_Id,
12249 S_Adjustment);
12250
12251 Act_Body :=
12252 Copy_Generic_Node
12253 (Original_Node (Gen_Body), Empty, Instantiating => True);
12254
12255 -- Create proper defining name for the body, to correspond to the one
12256 -- in the spec.
12257
12258 Act_Body_Id :=
12259 Make_Defining_Identifier (Sloc (Act_Decl_Id), Chars (Act_Decl_Id));
12260
12261 Set_Comes_From_Source (Act_Body_Id, Comes_From_Source (Act_Decl_Id));
12262 Set_Defining_Unit_Name (Specification (Act_Body), Act_Body_Id);
12263
12264 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
12265 Set_Has_Completion (Act_Decl_Id);
12266 Check_Generic_Actuals (Pack_Id, False);
12267
12268 -- Generate a reference to link the visible subprogram instance to
12269 -- the generic body, which for navigation purposes is the only
12270 -- available source for the instance.
12271
12272 Generate_Reference
12273 (Related_Instance (Pack_Id),
12274 Gen_Body_Id, 'b', Set_Ref => False, Force => True);
12275
12276 -- If it is a child unit, make the parent instance (which is an
12277 -- instance of the parent of the generic) visible. The parent
12278 -- instance is the prefix of the name of the generic unit.
12279
12280 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
12281 and then Nkind (Gen_Id) = N_Expanded_Name
12282 then
12283 Par_Ent := Entity (Prefix (Gen_Id));
12284 Par_Vis := Is_Immediately_Visible (Par_Ent);
12285 Install_Parent (Par_Ent, In_Body => True);
12286 Par_Installed := True;
12287
12288 elsif Is_Child_Unit (Gen_Unit) then
12289 Par_Ent := Scope (Gen_Unit);
12290 Par_Vis := Is_Immediately_Visible (Par_Ent);
12291 Install_Parent (Par_Ent, In_Body => True);
12292 Par_Installed := True;
12293 end if;
12294
12295 -- Subprogram body is placed in the body of wrapper package,
12296 -- whose spec contains the subprogram declaration as well as
12297 -- the renaming declarations for the generic parameters.
12298
12299 Pack_Body :=
12300 Make_Package_Body (Loc,
12301 Defining_Unit_Name => New_Copy (Pack_Id),
12302 Declarations => New_List (Act_Body));
12303
12304 Set_Corresponding_Spec (Pack_Body, Pack_Id);
12305
12306 -- If the instantiation is a library unit, then build resulting
12307 -- compilation unit nodes for the instance. The declaration of
12308 -- the enclosing package is the grandparent of the subprogram
12309 -- declaration. First replace the instantiation node as the unit
12310 -- of the corresponding compilation.
12311
12312 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
12313 if Parent (Inst_Node) = Cunit (Main_Unit) then
12314 Set_Unit (Parent (Inst_Node), Inst_Node);
12315 Build_Instance_Compilation_Unit_Nodes
12316 (Inst_Node, Pack_Body, Parent (Parent (Act_Decl)));
12317 Analyze (Inst_Node);
12318 else
12319 Set_Parent (Pack_Body, Parent (Inst_Node));
12320 Analyze (Pack_Body);
12321 end if;
12322
12323 else
12324 Insert_Before (Inst_Node, Pack_Body);
12325 Mark_Rewrite_Insertion (Pack_Body);
12326 Analyze (Pack_Body);
12327
12328 if Expander_Active then
12329 Freeze_Subprogram_Body (Inst_Node, Gen_Body, Pack_Id);
12330 end if;
12331 end if;
12332
12333 Inherit_Context (Gen_Body, Inst_Node);
12334
12335 Restore_Private_Views (Pack_Id, False);
12336
12337 if Par_Installed then
12338 Remove_Parent (In_Body => True);
12339
12340 -- Restore the previous visibility of the parent
12341
12342 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
12343 end if;
12344
12345 Restore_Env;
12346
12347 -- Body not found. Error was emitted already. If there were no previous
12348 -- errors, this may be an instance whose scope is a premature instance.
12349 -- In that case we must insure that the (legal) program does raise
12350 -- program error if executed. We generate a subprogram body for this
12351 -- purpose. See DEC ac30vso.
12352
12353 -- Should not reference proprietary DEC tests in comments ???
12354
12355 elsif Serious_Errors_Detected = 0
12356 and then Nkind (Parent (Inst_Node)) /= N_Compilation_Unit
12357 then
12358 if Body_Optional then
12359 goto Leave;
12360
12361 elsif Ekind (Act_Decl_Id) = E_Procedure then
12362 Act_Body :=
12363 Make_Subprogram_Body (Loc,
12364 Specification =>
12365 Make_Procedure_Specification (Loc,
12366 Defining_Unit_Name =>
12367 Make_Defining_Identifier (Loc, Chars (Act_Decl_Id)),
12368 Parameter_Specifications =>
12369 New_Copy_List
12370 (Parameter_Specifications (Parent (Act_Decl_Id)))),
12371
12372 Declarations => Empty_List,
12373 Handled_Statement_Sequence =>
12374 Make_Handled_Sequence_Of_Statements (Loc,
12375 Statements => New_List (
12376 Make_Raise_Program_Error (Loc,
12377 Reason => PE_Access_Before_Elaboration))));
12378
12379 else
12380 Ret_Expr :=
12381 Make_Raise_Program_Error (Loc,
12382 Reason => PE_Access_Before_Elaboration);
12383
12384 Set_Etype (Ret_Expr, (Etype (Act_Decl_Id)));
12385 Set_Analyzed (Ret_Expr);
12386
12387 Act_Body :=
12388 Make_Subprogram_Body (Loc,
12389 Specification =>
12390 Make_Function_Specification (Loc,
12391 Defining_Unit_Name =>
12392 Make_Defining_Identifier (Loc, Chars (Act_Decl_Id)),
12393 Parameter_Specifications =>
12394 New_Copy_List
12395 (Parameter_Specifications (Parent (Act_Decl_Id))),
12396 Result_Definition =>
12397 New_Occurrence_Of (Etype (Act_Decl_Id), Loc)),
12398
12399 Declarations => Empty_List,
12400 Handled_Statement_Sequence =>
12401 Make_Handled_Sequence_Of_Statements (Loc,
12402 Statements => New_List (
12403 Make_Simple_Return_Statement (Loc, Ret_Expr))));
12404 end if;
12405
12406 Pack_Body :=
12407 Make_Package_Body (Loc,
12408 Defining_Unit_Name => New_Copy (Pack_Id),
12409 Declarations => New_List (Act_Body));
12410
12411 Insert_After (Inst_Node, Pack_Body);
12412 Set_Corresponding_Spec (Pack_Body, Pack_Id);
12413 Analyze (Pack_Body);
12414 end if;
12415
12416 <<Leave>>
12417
12418 -- Restore the context that was in effect prior to instantiating the
12419 -- subprogram body.
12420
12421 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
12422 Local_Suppress_Stack_Top := Saved_LSST;
12423 Scope_Suppress := Saved_SS;
12424 Style_Check := Saved_SC;
12425
12426 Expander_Mode_Restore;
12427 Restore_Config_Switches (Saved_CS);
12428 Restore_Ghost_Region (Saved_GM, Saved_IGR);
12429 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
12430 Restore_Warnings (Saved_Warn);
12431 end Instantiate_Subprogram_Body;
12432
12433 ----------------------
12434 -- Instantiate_Type --
12435 ----------------------
12436
12437 function Instantiate_Type
12438 (Formal : Node_Id;
12439 Actual : Node_Id;
12440 Analyzed_Formal : Node_Id;
12441 Actual_Decls : List_Id) return List_Id
12442 is
12443 A_Gen_T : constant Entity_Id :=
12444 Defining_Identifier (Analyzed_Formal);
12445 Def : constant Node_Id := Formal_Type_Definition (Formal);
12446 Gen_T : constant Entity_Id := Defining_Identifier (Formal);
12447 Act_T : Entity_Id;
12448 Ancestor : Entity_Id := Empty;
12449 Decl_Node : Node_Id;
12450 Decl_Nodes : List_Id;
12451 Loc : Source_Ptr;
12452 Subt : Entity_Id;
12453
12454 procedure Check_Shared_Variable_Control_Aspects;
12455 -- Ada 2020: Verify that shared variable control aspects (RM C.6)
12456 -- that may be specified for a formal type are obeyed by the actual.
12457
12458 procedure Diagnose_Predicated_Actual;
12459 -- There are a number of constructs in which a discrete type with
12460 -- predicates is illegal, e.g. as an index in an array type declaration.
12461 -- If a generic type is used is such a construct in a generic package
12462 -- declaration, it carries the flag No_Predicate_On_Actual. it is part
12463 -- of the generic contract that the actual cannot have predicates.
12464
12465 procedure Validate_Array_Type_Instance;
12466 procedure Validate_Access_Subprogram_Instance;
12467 procedure Validate_Access_Type_Instance;
12468 procedure Validate_Derived_Type_Instance;
12469 procedure Validate_Derived_Interface_Type_Instance;
12470 procedure Validate_Discriminated_Formal_Type;
12471 procedure Validate_Interface_Type_Instance;
12472 procedure Validate_Private_Type_Instance;
12473 procedure Validate_Incomplete_Type_Instance;
12474 -- These procedures perform validation tests for the named case.
12475 -- Validate_Discriminated_Formal_Type is shared by formal private
12476 -- types and Ada 2012 formal incomplete types.
12477
12478 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean;
12479 -- Check that base types are the same and that the subtypes match
12480 -- statically. Used in several of the above.
12481
12482 --------------------------------------------
12483 -- Check_Shared_Variable_Control_Aspects --
12484 --------------------------------------------
12485
12486 -- Ada 2020: Verify that shared variable control aspects (RM C.6)
12487 -- that may be specified for the formal are obeyed by the actual.
12488 -- If the formal is a derived type the aspect specifications must match.
12489 -- NOTE: AI12-0282 implies that matching of aspects is required between
12490 -- formal and actual in all cases, but this is too restrictive.
12491 -- In particular it violates a language design rule: a limited private
12492 -- indefinite formal can be matched by any actual. The current code
12493 -- reflects an older and more permissive version of RM C.6 (12/5).
12494
12495 procedure Check_Shared_Variable_Control_Aspects is
12496 begin
12497 if Ada_Version >= Ada_2020 then
12498 if Is_Atomic (A_Gen_T) and then not Is_Atomic (Act_T) then
12499 Error_Msg_NE
12500 ("actual for& must have Atomic aspect", Actual, A_Gen_T);
12501
12502 elsif Is_Derived_Type (A_Gen_T)
12503 and then Is_Atomic (A_Gen_T) /= Is_Atomic (Act_T)
12504 then
12505 Error_Msg_NE
12506 ("actual for& has different Atomic aspect", Actual, A_Gen_T);
12507 end if;
12508
12509 if Is_Volatile (A_Gen_T) and then not Is_Volatile (Act_T) then
12510 Error_Msg_NE
12511 ("actual for& has different Volatile aspect",
12512 Actual, A_Gen_T);
12513
12514 elsif Is_Derived_Type (A_Gen_T)
12515 and then Is_Volatile (A_Gen_T) /= Is_Volatile (Act_T)
12516 then
12517 Error_Msg_NE
12518 ("actual for& has different Volatile aspect",
12519 Actual, A_Gen_T);
12520 end if;
12521
12522 -- We assume that an array type whose atomic component type
12523 -- is Atomic is equivalent to an array type with the explicit
12524 -- aspect Has_Atomic_Components. This is a reasonable inference
12525 -- from the intent of AI12-0282, and makes it legal to use an
12526 -- actual that does not have the identical aspect as the formal.
12527 -- Ditto for volatile components.
12528
12529 declare
12530 Actual_Atomic_Comp : constant Boolean :=
12531 Has_Atomic_Components (Act_T)
12532 or else (Is_Array_Type (Act_T)
12533 and then Is_Atomic (Component_Type (Act_T)));
12534 begin
12535 if Has_Atomic_Components (A_Gen_T) /= Actual_Atomic_Comp then
12536 Error_Msg_NE
12537 ("formal and actual for& must agree on atomic components",
12538 Actual, A_Gen_T);
12539 end if;
12540 end;
12541
12542 declare
12543 Actual_Volatile_Comp : constant Boolean :=
12544 Has_Volatile_Components (Act_T)
12545 or else (Is_Array_Type (Act_T)
12546 and then Is_Volatile (Component_Type (Act_T)));
12547 begin
12548 if Has_Volatile_Components (A_Gen_T) /= Actual_Volatile_Comp
12549 then
12550 Error_Msg_NE
12551 ("actual for& must have volatile components",
12552 Actual, A_Gen_T);
12553 end if;
12554 end;
12555
12556 -- The following two aspects do not require exact matching,
12557 -- but only one-way agreement. See RM C.6.
12558
12559 if Is_Independent (A_Gen_T) and then not Is_Independent (Act_T)
12560 then
12561 Error_Msg_NE
12562 ("actual for& must have Independent aspect specified",
12563 Actual, A_Gen_T);
12564 end if;
12565
12566 if Has_Independent_Components (A_Gen_T)
12567 and then not Has_Independent_Components (Act_T)
12568 then
12569 Error_Msg_NE
12570 ("actual for& must have Independent_Components specified",
12571 Actual, A_Gen_T);
12572 end if;
12573
12574 -- Check actual/formal compatibility with respect to the four
12575 -- volatility refinement aspects.
12576
12577 Check_Volatility_Compatibility
12578 (Act_T, A_Gen_T,
12579 "actual type", "its corresponding formal type",
12580 Srcpos_Bearer => Act_T);
12581 end if;
12582 end Check_Shared_Variable_Control_Aspects;
12583
12584 ---------------------------------
12585 -- Diagnose_Predicated_Actual --
12586 ---------------------------------
12587
12588 procedure Diagnose_Predicated_Actual is
12589 begin
12590 if No_Predicate_On_Actual (A_Gen_T)
12591 and then Has_Predicates (Act_T)
12592 then
12593 Error_Msg_NE
12594 ("actual for& cannot be a type with predicate",
12595 Instantiation_Node, A_Gen_T);
12596
12597 elsif No_Dynamic_Predicate_On_Actual (A_Gen_T)
12598 and then Has_Predicates (Act_T)
12599 and then not Has_Static_Predicate_Aspect (Act_T)
12600 then
12601 Error_Msg_NE
12602 ("actual for& cannot be a type with a dynamic predicate",
12603 Instantiation_Node, A_Gen_T);
12604 end if;
12605 end Diagnose_Predicated_Actual;
12606
12607 --------------------
12608 -- Subtypes_Match --
12609 --------------------
12610
12611 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean is
12612 T : constant Entity_Id := Get_Instance_Of (Gen_T);
12613
12614 begin
12615 -- Some detailed comments would be useful here ???
12616
12617 return ((Base_Type (T) = Act_T
12618 or else Base_Type (T) = Base_Type (Act_T))
12619 and then Subtypes_Statically_Match (T, Act_T))
12620
12621 or else (Is_Class_Wide_Type (Gen_T)
12622 and then Is_Class_Wide_Type (Act_T)
12623 and then Subtypes_Match
12624 (Get_Instance_Of (Root_Type (Gen_T)),
12625 Root_Type (Act_T)))
12626
12627 or else
12628 (Ekind_In (Gen_T, E_Anonymous_Access_Subprogram_Type,
12629 E_Anonymous_Access_Type)
12630 and then Ekind (Act_T) = Ekind (Gen_T)
12631 and then Subtypes_Statically_Match
12632 (Designated_Type (Gen_T), Designated_Type (Act_T)));
12633 end Subtypes_Match;
12634
12635 -----------------------------------------
12636 -- Validate_Access_Subprogram_Instance --
12637 -----------------------------------------
12638
12639 procedure Validate_Access_Subprogram_Instance is
12640 begin
12641 if not Is_Access_Type (Act_T)
12642 or else Ekind (Designated_Type (Act_T)) /= E_Subprogram_Type
12643 then
12644 Error_Msg_NE
12645 ("expect access type in instantiation of &", Actual, Gen_T);
12646 Abandon_Instantiation (Actual);
12647 end if;
12648
12649 -- According to AI05-288, actuals for access_to_subprograms must be
12650 -- subtype conformant with the generic formal. Previous to AI05-288
12651 -- only mode conformance was required.
12652
12653 -- This is a binding interpretation that applies to previous versions
12654 -- of the language, no need to maintain previous weaker checks.
12655
12656 Check_Subtype_Conformant
12657 (Designated_Type (Act_T),
12658 Designated_Type (A_Gen_T),
12659 Actual,
12660 Get_Inst => True);
12661
12662 if Ekind (Base_Type (Act_T)) = E_Access_Protected_Subprogram_Type then
12663 if Ekind (A_Gen_T) = E_Access_Subprogram_Type then
12664 Error_Msg_NE
12665 ("protected access type not allowed for formal &",
12666 Actual, Gen_T);
12667 end if;
12668
12669 elsif Ekind (A_Gen_T) = E_Access_Protected_Subprogram_Type then
12670 Error_Msg_NE
12671 ("expect protected access type for formal &",
12672 Actual, Gen_T);
12673 end if;
12674
12675 -- If the formal has a specified convention (which in most cases
12676 -- will be StdCall) verify that the actual has the same convention.
12677
12678 if Has_Convention_Pragma (A_Gen_T)
12679 and then Convention (A_Gen_T) /= Convention (Act_T)
12680 then
12681 Error_Msg_Name_1 := Get_Convention_Name (Convention (A_Gen_T));
12682 Error_Msg_NE
12683 ("actual for formal & must have convention %", Actual, Gen_T);
12684 end if;
12685
12686 if Can_Never_Be_Null (A_Gen_T) /= Can_Never_Be_Null (Act_T) then
12687 Error_Msg_NE
12688 ("non null exclusion of actual and formal & do not match",
12689 Actual, Gen_T);
12690 end if;
12691 end Validate_Access_Subprogram_Instance;
12692
12693 -----------------------------------
12694 -- Validate_Access_Type_Instance --
12695 -----------------------------------
12696
12697 procedure Validate_Access_Type_Instance is
12698 Desig_Type : constant Entity_Id :=
12699 Find_Actual_Type (Designated_Type (A_Gen_T), A_Gen_T);
12700 Desig_Act : Entity_Id;
12701
12702 begin
12703 if not Is_Access_Type (Act_T) then
12704 Error_Msg_NE
12705 ("expect access type in instantiation of &", Actual, Gen_T);
12706 Abandon_Instantiation (Actual);
12707 end if;
12708
12709 if Is_Access_Constant (A_Gen_T) then
12710 if not Is_Access_Constant (Act_T) then
12711 Error_Msg_N
12712 ("actual type must be access-to-constant type", Actual);
12713 Abandon_Instantiation (Actual);
12714 end if;
12715 else
12716 if Is_Access_Constant (Act_T) then
12717 Error_Msg_N
12718 ("actual type must be access-to-variable type", Actual);
12719 Abandon_Instantiation (Actual);
12720
12721 elsif Ekind (A_Gen_T) = E_General_Access_Type
12722 and then Ekind (Base_Type (Act_T)) /= E_General_Access_Type
12723 then
12724 Error_Msg_N -- CODEFIX
12725 ("actual must be general access type!", Actual);
12726 Error_Msg_NE -- CODEFIX
12727 ("add ALL to }!", Actual, Act_T);
12728 Abandon_Instantiation (Actual);
12729 end if;
12730 end if;
12731
12732 -- The designated subtypes, that is to say the subtypes introduced
12733 -- by an access type declaration (and not by a subtype declaration)
12734 -- must match.
12735
12736 Desig_Act := Designated_Type (Base_Type (Act_T));
12737
12738 -- The designated type may have been introduced through a limited_
12739 -- with clause, in which case retrieve the non-limited view. This
12740 -- applies to incomplete types as well as to class-wide types.
12741
12742 if From_Limited_With (Desig_Act) then
12743 Desig_Act := Available_View (Desig_Act);
12744 end if;
12745
12746 if not Subtypes_Match (Desig_Type, Desig_Act) then
12747 Error_Msg_NE
12748 ("designated type of actual does not match that of formal &",
12749 Actual, Gen_T);
12750
12751 if not Predicates_Match (Desig_Type, Desig_Act) then
12752 Error_Msg_N ("\predicates do not match", Actual);
12753 end if;
12754
12755 Abandon_Instantiation (Actual);
12756
12757 elsif Is_Access_Type (Designated_Type (Act_T))
12758 and then Is_Constrained (Designated_Type (Designated_Type (Act_T)))
12759 /=
12760 Is_Constrained (Designated_Type (Desig_Type))
12761 then
12762 Error_Msg_NE
12763 ("designated type of actual does not match that of formal &",
12764 Actual, Gen_T);
12765
12766 if not Predicates_Match (Desig_Type, Desig_Act) then
12767 Error_Msg_N ("\predicates do not match", Actual);
12768 end if;
12769
12770 Abandon_Instantiation (Actual);
12771 end if;
12772
12773 -- Ada 2005: null-exclusion indicators of the two types must agree
12774
12775 if Can_Never_Be_Null (A_Gen_T) /= Can_Never_Be_Null (Act_T) then
12776 Error_Msg_NE
12777 ("non null exclusion of actual and formal & do not match",
12778 Actual, Gen_T);
12779 end if;
12780 end Validate_Access_Type_Instance;
12781
12782 ----------------------------------
12783 -- Validate_Array_Type_Instance --
12784 ----------------------------------
12785
12786 procedure Validate_Array_Type_Instance is
12787 I1 : Node_Id;
12788 I2 : Node_Id;
12789 T2 : Entity_Id;
12790
12791 function Formal_Dimensions return Nat;
12792 -- Count number of dimensions in array type formal
12793
12794 -----------------------
12795 -- Formal_Dimensions --
12796 -----------------------
12797
12798 function Formal_Dimensions return Nat is
12799 Num : Nat := 0;
12800 Index : Node_Id;
12801
12802 begin
12803 if Nkind (Def) = N_Constrained_Array_Definition then
12804 Index := First (Discrete_Subtype_Definitions (Def));
12805 else
12806 Index := First (Subtype_Marks (Def));
12807 end if;
12808
12809 while Present (Index) loop
12810 Num := Num + 1;
12811 Next_Index (Index);
12812 end loop;
12813
12814 return Num;
12815 end Formal_Dimensions;
12816
12817 -- Start of processing for Validate_Array_Type_Instance
12818
12819 begin
12820 if not Is_Array_Type (Act_T) then
12821 Error_Msg_NE
12822 ("expect array type in instantiation of &", Actual, Gen_T);
12823 Abandon_Instantiation (Actual);
12824
12825 elsif Nkind (Def) = N_Constrained_Array_Definition then
12826 if not (Is_Constrained (Act_T)) then
12827 Error_Msg_NE
12828 ("expect constrained array in instantiation of &",
12829 Actual, Gen_T);
12830 Abandon_Instantiation (Actual);
12831 end if;
12832
12833 else
12834 if Is_Constrained (Act_T) then
12835 Error_Msg_NE
12836 ("expect unconstrained array in instantiation of &",
12837 Actual, Gen_T);
12838 Abandon_Instantiation (Actual);
12839 end if;
12840 end if;
12841
12842 if Formal_Dimensions /= Number_Dimensions (Act_T) then
12843 Error_Msg_NE
12844 ("dimensions of actual do not match formal &", Actual, Gen_T);
12845 Abandon_Instantiation (Actual);
12846 end if;
12847
12848 I1 := First_Index (A_Gen_T);
12849 I2 := First_Index (Act_T);
12850 for J in 1 .. Formal_Dimensions loop
12851
12852 -- If the indexes of the actual were given by a subtype_mark,
12853 -- the index was transformed into a range attribute. Retrieve
12854 -- the original type mark for checking.
12855
12856 if Is_Entity_Name (Original_Node (I2)) then
12857 T2 := Entity (Original_Node (I2));
12858 else
12859 T2 := Etype (I2);
12860 end if;
12861
12862 if not Subtypes_Match
12863 (Find_Actual_Type (Etype (I1), A_Gen_T), T2)
12864 then
12865 Error_Msg_NE
12866 ("index types of actual do not match those of formal &",
12867 Actual, Gen_T);
12868 Abandon_Instantiation (Actual);
12869 end if;
12870
12871 Next_Index (I1);
12872 Next_Index (I2);
12873 end loop;
12874
12875 -- Check matching subtypes. Note that there are complex visibility
12876 -- issues when the generic is a child unit and some aspect of the
12877 -- generic type is declared in a parent unit of the generic. We do
12878 -- the test to handle this special case only after a direct check
12879 -- for static matching has failed. The case where both the component
12880 -- type and the array type are separate formals, and the component
12881 -- type is a private view may also require special checking in
12882 -- Subtypes_Match. Finally, we assume that a child instance where
12883 -- the component type comes from a formal of a parent instance is
12884 -- correct because the generic was correct. A more precise check
12885 -- seems too complex to install???
12886
12887 if Subtypes_Match
12888 (Component_Type (A_Gen_T), Component_Type (Act_T))
12889 or else
12890 Subtypes_Match
12891 (Find_Actual_Type (Component_Type (A_Gen_T), A_Gen_T),
12892 Component_Type (Act_T))
12893 or else
12894 (not Inside_A_Generic
12895 and then Is_Child_Unit (Scope (Component_Type (A_Gen_T))))
12896 then
12897 null;
12898 else
12899 Error_Msg_NE
12900 ("component subtype of actual does not match that of formal &",
12901 Actual, Gen_T);
12902 Abandon_Instantiation (Actual);
12903 end if;
12904
12905 if Has_Aliased_Components (A_Gen_T)
12906 and then not Has_Aliased_Components (Act_T)
12907 then
12908 Error_Msg_NE
12909 ("actual must have aliased components to match formal type &",
12910 Actual, Gen_T);
12911 end if;
12912 end Validate_Array_Type_Instance;
12913
12914 -----------------------------------------------
12915 -- Validate_Derived_Interface_Type_Instance --
12916 -----------------------------------------------
12917
12918 procedure Validate_Derived_Interface_Type_Instance is
12919 Par : constant Entity_Id := Entity (Subtype_Indication (Def));
12920 Elmt : Elmt_Id;
12921
12922 begin
12923 -- First apply interface instance checks
12924
12925 Validate_Interface_Type_Instance;
12926
12927 -- Verify that immediate parent interface is an ancestor of
12928 -- the actual.
12929
12930 if Present (Par)
12931 and then not Interface_Present_In_Ancestor (Act_T, Par)
12932 then
12933 Error_Msg_NE
12934 ("interface actual must include progenitor&", Actual, Par);
12935 end if;
12936
12937 -- Now verify that the actual includes all other ancestors of
12938 -- the formal.
12939
12940 Elmt := First_Elmt (Interfaces (A_Gen_T));
12941 while Present (Elmt) loop
12942 if not Interface_Present_In_Ancestor
12943 (Act_T, Get_Instance_Of (Node (Elmt)))
12944 then
12945 Error_Msg_NE
12946 ("interface actual must include progenitor&",
12947 Actual, Node (Elmt));
12948 end if;
12949
12950 Next_Elmt (Elmt);
12951 end loop;
12952 end Validate_Derived_Interface_Type_Instance;
12953
12954 ------------------------------------
12955 -- Validate_Derived_Type_Instance --
12956 ------------------------------------
12957
12958 procedure Validate_Derived_Type_Instance is
12959 Actual_Discr : Entity_Id;
12960 Ancestor_Discr : Entity_Id;
12961
12962 begin
12963 -- Verify that the actual includes the progenitors of the formal,
12964 -- if any. The formal may depend on previous formals and their
12965 -- instance, so we must examine instance of interfaces if present.
12966 -- The actual may be an extension of an interface, in which case
12967 -- it does not appear in the interface list, so this must be
12968 -- checked separately.
12969
12970 if Present (Interface_List (Def)) then
12971 if not Has_Interfaces (Act_T) then
12972 Error_Msg_NE
12973 ("actual must implement all interfaces of formal&",
12974 Actual, A_Gen_T);
12975
12976 else
12977 declare
12978 Act_Iface_List : Elist_Id;
12979 Iface : Node_Id;
12980 Iface_Ent : Entity_Id;
12981
12982 function Instance_Exists (I : Entity_Id) return Boolean;
12983 -- If the interface entity is declared in a generic unit,
12984 -- this can only be legal if we are within an instantiation
12985 -- of a child of that generic. There is currently no
12986 -- mechanism to relate an interface declared within a
12987 -- generic to the corresponding interface in an instance,
12988 -- so we traverse the list of interfaces of the actual,
12989 -- looking for a name match.
12990
12991 ---------------------
12992 -- Instance_Exists --
12993 ---------------------
12994
12995 function Instance_Exists (I : Entity_Id) return Boolean is
12996 Iface_Elmt : Elmt_Id;
12997
12998 begin
12999 Iface_Elmt := First_Elmt (Act_Iface_List);
13000 while Present (Iface_Elmt) loop
13001 if Is_Generic_Instance (Scope (Node (Iface_Elmt)))
13002 and then Chars (Node (Iface_Elmt)) = Chars (I)
13003 then
13004 return True;
13005 end if;
13006
13007 Next_Elmt (Iface_Elmt);
13008 end loop;
13009
13010 return False;
13011 end Instance_Exists;
13012
13013 begin
13014 Iface := First (Abstract_Interface_List (A_Gen_T));
13015 Collect_Interfaces (Act_T, Act_Iface_List);
13016
13017 while Present (Iface) loop
13018 Iface_Ent := Get_Instance_Of (Entity (Iface));
13019
13020 if Is_Ancestor (Iface_Ent, Act_T)
13021 or else Is_Progenitor (Iface_Ent, Act_T)
13022 then
13023 null;
13024
13025 elsif Ekind (Scope (Iface_Ent)) = E_Generic_Package
13026 and then Instance_Exists (Iface_Ent)
13027 then
13028 null;
13029
13030 else
13031 Error_Msg_Name_1 := Chars (Act_T);
13032 Error_Msg_NE
13033 ("Actual% must implement interface&",
13034 Actual, Etype (Iface));
13035 end if;
13036
13037 Next (Iface);
13038 end loop;
13039 end;
13040 end if;
13041 end if;
13042
13043 -- If the parent type in the generic declaration is itself a previous
13044 -- formal type, then it is local to the generic and absent from the
13045 -- analyzed generic definition. In that case the ancestor is the
13046 -- instance of the formal (which must have been instantiated
13047 -- previously), unless the ancestor is itself a formal derived type.
13048 -- In this latter case (which is the subject of Corrigendum 8652/0038
13049 -- (AI-202) the ancestor of the formals is the ancestor of its
13050 -- parent. Otherwise, the analyzed generic carries the parent type.
13051 -- If the parent type is defined in a previous formal package, then
13052 -- the scope of that formal package is that of the generic type
13053 -- itself, and it has already been mapped into the corresponding type
13054 -- in the actual package.
13055
13056 -- Common case: parent type defined outside of the generic
13057
13058 if Is_Entity_Name (Subtype_Mark (Def))
13059 and then Present (Entity (Subtype_Mark (Def)))
13060 then
13061 Ancestor := Get_Instance_Of (Entity (Subtype_Mark (Def)));
13062
13063 -- Check whether parent is defined in a previous formal package
13064
13065 elsif
13066 Scope (Scope (Base_Type (Etype (A_Gen_T)))) = Scope (A_Gen_T)
13067 then
13068 Ancestor :=
13069 Get_Instance_Of (Base_Type (Etype (A_Gen_T)));
13070
13071 -- The type may be a local derivation, or a type extension of a
13072 -- previous formal, or of a formal of a parent package.
13073
13074 elsif Is_Derived_Type (Get_Instance_Of (A_Gen_T))
13075 or else
13076 Ekind (Get_Instance_Of (A_Gen_T)) = E_Record_Type_With_Private
13077 then
13078 -- Check whether the parent is another derived formal type in the
13079 -- same generic unit.
13080
13081 if Etype (A_Gen_T) /= A_Gen_T
13082 and then Is_Generic_Type (Etype (A_Gen_T))
13083 and then Scope (Etype (A_Gen_T)) = Scope (A_Gen_T)
13084 and then Etype (Etype (A_Gen_T)) /= Etype (A_Gen_T)
13085 then
13086 -- Locate ancestor of parent from the subtype declaration
13087 -- created for the actual.
13088
13089 declare
13090 Decl : Node_Id;
13091
13092 begin
13093 Decl := First (Actual_Decls);
13094 while Present (Decl) loop
13095 if Nkind (Decl) = N_Subtype_Declaration
13096 and then Chars (Defining_Identifier (Decl)) =
13097 Chars (Etype (A_Gen_T))
13098 then
13099 Ancestor := Generic_Parent_Type (Decl);
13100 exit;
13101 else
13102 Next (Decl);
13103 end if;
13104 end loop;
13105 end;
13106
13107 pragma Assert (Present (Ancestor));
13108
13109 -- The ancestor itself may be a previous formal that has been
13110 -- instantiated.
13111
13112 Ancestor := Get_Instance_Of (Ancestor);
13113
13114 else
13115 Ancestor :=
13116 Get_Instance_Of (Base_Type (Get_Instance_Of (A_Gen_T)));
13117 end if;
13118
13119 -- Check whether parent is a previous formal of the current generic
13120
13121 elsif Is_Derived_Type (A_Gen_T)
13122 and then Is_Generic_Type (Etype (A_Gen_T))
13123 and then Scope (A_Gen_T) = Scope (Etype (A_Gen_T))
13124 then
13125 Ancestor := Get_Instance_Of (First_Subtype (Etype (A_Gen_T)));
13126
13127 -- An unusual case: the actual is a type declared in a parent unit,
13128 -- but is not a formal type so there is no instance_of for it.
13129 -- Retrieve it by analyzing the record extension.
13130
13131 elsif Is_Child_Unit (Scope (A_Gen_T))
13132 and then In_Open_Scopes (Scope (Act_T))
13133 and then Is_Generic_Instance (Scope (Act_T))
13134 then
13135 Analyze (Subtype_Mark (Def));
13136 Ancestor := Entity (Subtype_Mark (Def));
13137
13138 else
13139 Ancestor := Get_Instance_Of (Etype (Base_Type (A_Gen_T)));
13140 end if;
13141
13142 -- If the formal derived type has pragma Preelaborable_Initialization
13143 -- then the actual type must have preelaborable initialization.
13144
13145 if Known_To_Have_Preelab_Init (A_Gen_T)
13146 and then not Has_Preelaborable_Initialization (Act_T)
13147 then
13148 Error_Msg_NE
13149 ("actual for & must have preelaborable initialization",
13150 Actual, Gen_T);
13151 end if;
13152
13153 -- Ada 2005 (AI-251)
13154
13155 if Ada_Version >= Ada_2005 and then Is_Interface (Ancestor) then
13156 if not Interface_Present_In_Ancestor (Act_T, Ancestor) then
13157 Error_Msg_NE
13158 ("(Ada 2005) expected type implementing & in instantiation",
13159 Actual, Ancestor);
13160 end if;
13161
13162 -- Finally verify that the (instance of) the ancestor is an ancestor
13163 -- of the actual.
13164
13165 elsif not Is_Ancestor (Base_Type (Ancestor), Act_T) then
13166 Error_Msg_NE
13167 ("expect type derived from & in instantiation",
13168 Actual, First_Subtype (Ancestor));
13169 Abandon_Instantiation (Actual);
13170 end if;
13171
13172 -- Ada 2005 (AI-443): Synchronized formal derived type checks. Note
13173 -- that the formal type declaration has been rewritten as a private
13174 -- extension.
13175
13176 if Ada_Version >= Ada_2005
13177 and then Nkind (Parent (A_Gen_T)) = N_Private_Extension_Declaration
13178 and then Synchronized_Present (Parent (A_Gen_T))
13179 then
13180 -- The actual must be a synchronized tagged type
13181
13182 if not Is_Tagged_Type (Act_T) then
13183 Error_Msg_N
13184 ("actual of synchronized type must be tagged", Actual);
13185 Abandon_Instantiation (Actual);
13186
13187 elsif Nkind (Parent (Act_T)) = N_Full_Type_Declaration
13188 and then Nkind (Type_Definition (Parent (Act_T))) =
13189 N_Derived_Type_Definition
13190 and then not Synchronized_Present
13191 (Type_Definition (Parent (Act_T)))
13192 then
13193 Error_Msg_N
13194 ("actual of synchronized type must be synchronized", Actual);
13195 Abandon_Instantiation (Actual);
13196 end if;
13197 end if;
13198
13199 -- Perform atomic/volatile checks (RM C.6(12)). Note that AI05-0218-1
13200 -- removes the second instance of the phrase "or allow pass by copy".
13201
13202 -- For Ada 2020, the aspect may be specified explicitly for the
13203 -- formal regardless of whether an ancestor obeys it.
13204
13205 if Is_Atomic (Act_T)
13206 and then not Is_Atomic (Ancestor)
13207 and then not Is_Atomic (A_Gen_T)
13208 then
13209 Error_Msg_N
13210 ("cannot have atomic actual type for non-atomic formal type",
13211 Actual);
13212
13213 elsif Is_Volatile (Act_T)
13214 and then not Is_Volatile (Ancestor)
13215 and then not Is_Volatile (A_Gen_T)
13216 then
13217 Error_Msg_N
13218 ("cannot have volatile actual type for non-volatile formal type",
13219 Actual);
13220 end if;
13221
13222 -- It should not be necessary to check for unknown discriminants on
13223 -- Formal, but for some reason Has_Unknown_Discriminants is false for
13224 -- A_Gen_T, so Is_Definite_Subtype incorrectly returns True. This
13225 -- needs fixing. ???
13226
13227 if Is_Definite_Subtype (A_Gen_T)
13228 and then not Unknown_Discriminants_Present (Formal)
13229 and then not Is_Definite_Subtype (Act_T)
13230 then
13231 Error_Msg_N ("actual subtype must be constrained", Actual);
13232 Abandon_Instantiation (Actual);
13233 end if;
13234
13235 if not Unknown_Discriminants_Present (Formal) then
13236 if Is_Constrained (Ancestor) then
13237 if not Is_Constrained (Act_T) then
13238 Error_Msg_N ("actual subtype must be constrained", Actual);
13239 Abandon_Instantiation (Actual);
13240 end if;
13241
13242 -- Ancestor is unconstrained, Check if generic formal and actual
13243 -- agree on constrainedness. The check only applies to array types
13244 -- and discriminated types.
13245
13246 elsif Is_Constrained (Act_T) then
13247 if Ekind (Ancestor) = E_Access_Type
13248 or else (not Is_Constrained (A_Gen_T)
13249 and then Is_Composite_Type (A_Gen_T))
13250 then
13251 Error_Msg_N ("actual subtype must be unconstrained", Actual);
13252 Abandon_Instantiation (Actual);
13253 end if;
13254
13255 -- A class-wide type is only allowed if the formal has unknown
13256 -- discriminants.
13257
13258 elsif Is_Class_Wide_Type (Act_T)
13259 and then not Has_Unknown_Discriminants (Ancestor)
13260 then
13261 Error_Msg_NE
13262 ("actual for & cannot be a class-wide type", Actual, Gen_T);
13263 Abandon_Instantiation (Actual);
13264
13265 -- Otherwise, the formal and actual must have the same number
13266 -- of discriminants and each discriminant of the actual must
13267 -- correspond to a discriminant of the formal.
13268
13269 elsif Has_Discriminants (Act_T)
13270 and then not Has_Unknown_Discriminants (Act_T)
13271 and then Has_Discriminants (Ancestor)
13272 then
13273 Actual_Discr := First_Discriminant (Act_T);
13274 Ancestor_Discr := First_Discriminant (Ancestor);
13275 while Present (Actual_Discr)
13276 and then Present (Ancestor_Discr)
13277 loop
13278 if Base_Type (Act_T) /= Base_Type (Ancestor) and then
13279 No (Corresponding_Discriminant (Actual_Discr))
13280 then
13281 Error_Msg_NE
13282 ("discriminant & does not correspond "
13283 & "to ancestor discriminant", Actual, Actual_Discr);
13284 Abandon_Instantiation (Actual);
13285 end if;
13286
13287 Next_Discriminant (Actual_Discr);
13288 Next_Discriminant (Ancestor_Discr);
13289 end loop;
13290
13291 if Present (Actual_Discr) or else Present (Ancestor_Discr) then
13292 Error_Msg_NE
13293 ("actual for & must have same number of discriminants",
13294 Actual, Gen_T);
13295 Abandon_Instantiation (Actual);
13296 end if;
13297
13298 -- This case should be caught by the earlier check for
13299 -- constrainedness, but the check here is added for completeness.
13300
13301 elsif Has_Discriminants (Act_T)
13302 and then not Has_Unknown_Discriminants (Act_T)
13303 then
13304 Error_Msg_NE
13305 ("actual for & must not have discriminants", Actual, Gen_T);
13306 Abandon_Instantiation (Actual);
13307
13308 elsif Has_Discriminants (Ancestor) then
13309 Error_Msg_NE
13310 ("actual for & must have known discriminants", Actual, Gen_T);
13311 Abandon_Instantiation (Actual);
13312 end if;
13313
13314 if not Subtypes_Statically_Compatible
13315 (Act_T, Ancestor, Formal_Derived_Matching => True)
13316 then
13317 Error_Msg_N
13318 ("constraint on actual is incompatible with formal", Actual);
13319 Abandon_Instantiation (Actual);
13320 end if;
13321 end if;
13322
13323 -- If the formal and actual types are abstract, check that there
13324 -- are no abstract primitives of the actual type that correspond to
13325 -- nonabstract primitives of the formal type (second sentence of
13326 -- RM95 3.9.3(9)).
13327
13328 if Is_Abstract_Type (A_Gen_T) and then Is_Abstract_Type (Act_T) then
13329 Check_Abstract_Primitives : declare
13330 Gen_Prims : constant Elist_Id :=
13331 Primitive_Operations (A_Gen_T);
13332 Gen_Elmt : Elmt_Id;
13333 Gen_Subp : Entity_Id;
13334 Anc_Subp : Entity_Id;
13335 Anc_Formal : Entity_Id;
13336 Anc_F_Type : Entity_Id;
13337
13338 Act_Prims : constant Elist_Id := Primitive_Operations (Act_T);
13339 Act_Elmt : Elmt_Id;
13340 Act_Subp : Entity_Id;
13341 Act_Formal : Entity_Id;
13342 Act_F_Type : Entity_Id;
13343
13344 Subprograms_Correspond : Boolean;
13345
13346 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean;
13347 -- Returns true if T2 is derived directly or indirectly from
13348 -- T1, including derivations from interfaces. T1 and T2 are
13349 -- required to be specific tagged base types.
13350
13351 ------------------------
13352 -- Is_Tagged_Ancestor --
13353 ------------------------
13354
13355 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean
13356 is
13357 Intfc_Elmt : Elmt_Id;
13358
13359 begin
13360 -- The predicate is satisfied if the types are the same
13361
13362 if T1 = T2 then
13363 return True;
13364
13365 -- If we've reached the top of the derivation chain then
13366 -- we know that T1 is not an ancestor of T2.
13367
13368 elsif Etype (T2) = T2 then
13369 return False;
13370
13371 -- Proceed to check T2's immediate parent
13372
13373 elsif Is_Ancestor (T1, Base_Type (Etype (T2))) then
13374 return True;
13375
13376 -- Finally, check to see if T1 is an ancestor of any of T2's
13377 -- progenitors.
13378
13379 else
13380 Intfc_Elmt := First_Elmt (Interfaces (T2));
13381 while Present (Intfc_Elmt) loop
13382 if Is_Ancestor (T1, Node (Intfc_Elmt)) then
13383 return True;
13384 end if;
13385
13386 Next_Elmt (Intfc_Elmt);
13387 end loop;
13388 end if;
13389
13390 return False;
13391 end Is_Tagged_Ancestor;
13392
13393 -- Start of processing for Check_Abstract_Primitives
13394
13395 begin
13396 -- Loop over all of the formal derived type's primitives
13397
13398 Gen_Elmt := First_Elmt (Gen_Prims);
13399 while Present (Gen_Elmt) loop
13400 Gen_Subp := Node (Gen_Elmt);
13401
13402 -- If the primitive of the formal is not abstract, then
13403 -- determine whether there is a corresponding primitive of
13404 -- the actual type that's abstract.
13405
13406 if not Is_Abstract_Subprogram (Gen_Subp) then
13407 Act_Elmt := First_Elmt (Act_Prims);
13408 while Present (Act_Elmt) loop
13409 Act_Subp := Node (Act_Elmt);
13410
13411 -- If we find an abstract primitive of the actual,
13412 -- then we need to test whether it corresponds to the
13413 -- subprogram from which the generic formal primitive
13414 -- is inherited.
13415
13416 if Is_Abstract_Subprogram (Act_Subp) then
13417 Anc_Subp := Alias (Gen_Subp);
13418
13419 -- Test whether we have a corresponding primitive
13420 -- by comparing names, kinds, formal types, and
13421 -- result types.
13422
13423 if Chars (Anc_Subp) = Chars (Act_Subp)
13424 and then Ekind (Anc_Subp) = Ekind (Act_Subp)
13425 then
13426 Anc_Formal := First_Formal (Anc_Subp);
13427 Act_Formal := First_Formal (Act_Subp);
13428 while Present (Anc_Formal)
13429 and then Present (Act_Formal)
13430 loop
13431 Anc_F_Type := Etype (Anc_Formal);
13432 Act_F_Type := Etype (Act_Formal);
13433
13434 if Ekind (Anc_F_Type) =
13435 E_Anonymous_Access_Type
13436 then
13437 Anc_F_Type := Designated_Type (Anc_F_Type);
13438
13439 if Ekind (Act_F_Type) =
13440 E_Anonymous_Access_Type
13441 then
13442 Act_F_Type :=
13443 Designated_Type (Act_F_Type);
13444 else
13445 exit;
13446 end if;
13447
13448 elsif
13449 Ekind (Act_F_Type) = E_Anonymous_Access_Type
13450 then
13451 exit;
13452 end if;
13453
13454 Anc_F_Type := Base_Type (Anc_F_Type);
13455 Act_F_Type := Base_Type (Act_F_Type);
13456
13457 -- If the formal is controlling, then the
13458 -- the type of the actual primitive's formal
13459 -- must be derived directly or indirectly
13460 -- from the type of the ancestor primitive's
13461 -- formal.
13462
13463 if Is_Controlling_Formal (Anc_Formal) then
13464 if not Is_Tagged_Ancestor
13465 (Anc_F_Type, Act_F_Type)
13466 then
13467 exit;
13468 end if;
13469
13470 -- Otherwise the types of the formals must
13471 -- be the same.
13472
13473 elsif Anc_F_Type /= Act_F_Type then
13474 exit;
13475 end if;
13476
13477 Next_Entity (Anc_Formal);
13478 Next_Entity (Act_Formal);
13479 end loop;
13480
13481 -- If we traversed through all of the formals
13482 -- then so far the subprograms correspond, so
13483 -- now check that any result types correspond.
13484
13485 if No (Anc_Formal) and then No (Act_Formal) then
13486 Subprograms_Correspond := True;
13487
13488 if Ekind (Act_Subp) = E_Function then
13489 Anc_F_Type := Etype (Anc_Subp);
13490 Act_F_Type := Etype (Act_Subp);
13491
13492 if Ekind (Anc_F_Type) =
13493 E_Anonymous_Access_Type
13494 then
13495 Anc_F_Type :=
13496 Designated_Type (Anc_F_Type);
13497
13498 if Ekind (Act_F_Type) =
13499 E_Anonymous_Access_Type
13500 then
13501 Act_F_Type :=
13502 Designated_Type (Act_F_Type);
13503 else
13504 Subprograms_Correspond := False;
13505 end if;
13506
13507 elsif
13508 Ekind (Act_F_Type)
13509 = E_Anonymous_Access_Type
13510 then
13511 Subprograms_Correspond := False;
13512 end if;
13513
13514 Anc_F_Type := Base_Type (Anc_F_Type);
13515 Act_F_Type := Base_Type (Act_F_Type);
13516
13517 -- Now either the result types must be
13518 -- the same or, if the result type is
13519 -- controlling, the result type of the
13520 -- actual primitive must descend from the
13521 -- result type of the ancestor primitive.
13522
13523 if Subprograms_Correspond
13524 and then Anc_F_Type /= Act_F_Type
13525 and then
13526 Has_Controlling_Result (Anc_Subp)
13527 and then not Is_Tagged_Ancestor
13528 (Anc_F_Type, Act_F_Type)
13529 then
13530 Subprograms_Correspond := False;
13531 end if;
13532 end if;
13533
13534 -- Found a matching subprogram belonging to
13535 -- formal ancestor type, so actual subprogram
13536 -- corresponds and this violates 3.9.3(9).
13537
13538 if Subprograms_Correspond then
13539 Error_Msg_NE
13540 ("abstract subprogram & overrides "
13541 & "nonabstract subprogram of ancestor",
13542 Actual, Act_Subp);
13543 end if;
13544 end if;
13545 end if;
13546 end if;
13547
13548 Next_Elmt (Act_Elmt);
13549 end loop;
13550 end if;
13551
13552 Next_Elmt (Gen_Elmt);
13553 end loop;
13554 end Check_Abstract_Primitives;
13555 end if;
13556
13557 -- Verify that limitedness matches. If parent is a limited
13558 -- interface then the generic formal is not unless declared
13559 -- explicitly so. If not declared limited, the actual cannot be
13560 -- limited (see AI05-0087).
13561
13562 if Is_Limited_Type (Act_T) and then not Is_Limited_Type (A_Gen_T) then
13563 if not In_Instance then
13564 Error_Msg_NE
13565 ("actual for non-limited & cannot be a limited type",
13566 Actual, Gen_T);
13567 Explain_Limited_Type (Act_T, Actual);
13568 Abandon_Instantiation (Actual);
13569 end if;
13570 end if;
13571
13572 -- Check for AI12-0036
13573
13574 declare
13575 Formal_Is_Private_Extension : constant Boolean :=
13576 Nkind (Parent (A_Gen_T)) = N_Private_Extension_Declaration;
13577
13578 Actual_Is_Tagged : constant Boolean := Is_Tagged_Type (Act_T);
13579
13580 begin
13581 if Actual_Is_Tagged /= Formal_Is_Private_Extension then
13582 if not In_Instance then
13583 if Actual_Is_Tagged then
13584 Error_Msg_NE
13585 ("actual for & cannot be a tagged type", Actual, Gen_T);
13586 else
13587 Error_Msg_NE
13588 ("actual for & must be a tagged type", Actual, Gen_T);
13589 end if;
13590
13591 Abandon_Instantiation (Actual);
13592 end if;
13593 end if;
13594 end;
13595 end Validate_Derived_Type_Instance;
13596
13597 ----------------------------------------
13598 -- Validate_Discriminated_Formal_Type --
13599 ----------------------------------------
13600
13601 procedure Validate_Discriminated_Formal_Type is
13602 Formal_Discr : Entity_Id;
13603 Actual_Discr : Entity_Id;
13604 Formal_Subt : Entity_Id;
13605
13606 begin
13607 if Has_Discriminants (A_Gen_T) then
13608 if not Has_Discriminants (Act_T) then
13609 Error_Msg_NE
13610 ("actual for & must have discriminants", Actual, Gen_T);
13611 Abandon_Instantiation (Actual);
13612
13613 elsif Is_Constrained (Act_T) then
13614 Error_Msg_NE
13615 ("actual for & must be unconstrained", Actual, Gen_T);
13616 Abandon_Instantiation (Actual);
13617
13618 else
13619 Formal_Discr := First_Discriminant (A_Gen_T);
13620 Actual_Discr := First_Discriminant (Act_T);
13621 while Formal_Discr /= Empty loop
13622 if Actual_Discr = Empty then
13623 Error_Msg_NE
13624 ("discriminants on actual do not match formal",
13625 Actual, Gen_T);
13626 Abandon_Instantiation (Actual);
13627 end if;
13628
13629 Formal_Subt := Get_Instance_Of (Etype (Formal_Discr));
13630
13631 -- Access discriminants match if designated types do
13632
13633 if Ekind (Base_Type (Formal_Subt)) = E_Anonymous_Access_Type
13634 and then (Ekind (Base_Type (Etype (Actual_Discr)))) =
13635 E_Anonymous_Access_Type
13636 and then
13637 Get_Instance_Of
13638 (Designated_Type (Base_Type (Formal_Subt))) =
13639 Designated_Type (Base_Type (Etype (Actual_Discr)))
13640 then
13641 null;
13642
13643 elsif Base_Type (Formal_Subt) /=
13644 Base_Type (Etype (Actual_Discr))
13645 then
13646 Error_Msg_NE
13647 ("types of actual discriminants must match formal",
13648 Actual, Gen_T);
13649 Abandon_Instantiation (Actual);
13650
13651 elsif not Subtypes_Statically_Match
13652 (Formal_Subt, Etype (Actual_Discr))
13653 and then Ada_Version >= Ada_95
13654 then
13655 Error_Msg_NE
13656 ("subtypes of actual discriminants must match formal",
13657 Actual, Gen_T);
13658 Abandon_Instantiation (Actual);
13659 end if;
13660
13661 Next_Discriminant (Formal_Discr);
13662 Next_Discriminant (Actual_Discr);
13663 end loop;
13664
13665 if Actual_Discr /= Empty then
13666 Error_Msg_NE
13667 ("discriminants on actual do not match formal",
13668 Actual, Gen_T);
13669 Abandon_Instantiation (Actual);
13670 end if;
13671 end if;
13672 end if;
13673 end Validate_Discriminated_Formal_Type;
13674
13675 ---------------------------------------
13676 -- Validate_Incomplete_Type_Instance --
13677 ---------------------------------------
13678
13679 procedure Validate_Incomplete_Type_Instance is
13680 begin
13681 if not Is_Tagged_Type (Act_T)
13682 and then Is_Tagged_Type (A_Gen_T)
13683 then
13684 Error_Msg_NE
13685 ("actual for & must be a tagged type", Actual, Gen_T);
13686 end if;
13687
13688 Validate_Discriminated_Formal_Type;
13689 end Validate_Incomplete_Type_Instance;
13690
13691 --------------------------------------
13692 -- Validate_Interface_Type_Instance --
13693 --------------------------------------
13694
13695 procedure Validate_Interface_Type_Instance is
13696 begin
13697 if not Is_Interface (Act_T) then
13698 Error_Msg_NE
13699 ("actual for formal interface type must be an interface",
13700 Actual, Gen_T);
13701
13702 elsif Is_Limited_Type (Act_T) /= Is_Limited_Type (A_Gen_T)
13703 or else Is_Task_Interface (A_Gen_T) /= Is_Task_Interface (Act_T)
13704 or else Is_Protected_Interface (A_Gen_T) /=
13705 Is_Protected_Interface (Act_T)
13706 or else Is_Synchronized_Interface (A_Gen_T) /=
13707 Is_Synchronized_Interface (Act_T)
13708 then
13709 Error_Msg_NE
13710 ("actual for interface& does not match (RM 12.5.5(4))",
13711 Actual, Gen_T);
13712 end if;
13713 end Validate_Interface_Type_Instance;
13714
13715 ------------------------------------
13716 -- Validate_Private_Type_Instance --
13717 ------------------------------------
13718
13719 procedure Validate_Private_Type_Instance is
13720 begin
13721 if Is_Limited_Type (Act_T)
13722 and then not Is_Limited_Type (A_Gen_T)
13723 then
13724 if In_Instance then
13725 null;
13726 else
13727 Error_Msg_NE
13728 ("actual for non-limited & cannot be a limited type", Actual,
13729 Gen_T);
13730 Explain_Limited_Type (Act_T, Actual);
13731 Abandon_Instantiation (Actual);
13732 end if;
13733
13734 elsif Known_To_Have_Preelab_Init (A_Gen_T)
13735 and then not Has_Preelaborable_Initialization (Act_T)
13736 then
13737 Error_Msg_NE
13738 ("actual for & must have preelaborable initialization", Actual,
13739 Gen_T);
13740
13741 elsif not Is_Definite_Subtype (Act_T)
13742 and then Is_Definite_Subtype (A_Gen_T)
13743 and then Ada_Version >= Ada_95
13744 then
13745 Error_Msg_NE
13746 ("actual for & must be a definite subtype", Actual, Gen_T);
13747
13748 elsif not Is_Tagged_Type (Act_T)
13749 and then Is_Tagged_Type (A_Gen_T)
13750 then
13751 Error_Msg_NE
13752 ("actual for & must be a tagged type", Actual, Gen_T);
13753 end if;
13754
13755 Validate_Discriminated_Formal_Type;
13756 Ancestor := Gen_T;
13757 end Validate_Private_Type_Instance;
13758
13759 -- Start of processing for Instantiate_Type
13760
13761 begin
13762 if Get_Instance_Of (A_Gen_T) /= A_Gen_T then
13763 Error_Msg_N ("duplicate instantiation of generic type", Actual);
13764 return New_List (Error);
13765
13766 elsif not Is_Entity_Name (Actual)
13767 or else not Is_Type (Entity (Actual))
13768 then
13769 Error_Msg_NE
13770 ("expect valid subtype mark to instantiate &", Actual, Gen_T);
13771 Abandon_Instantiation (Actual);
13772
13773 else
13774 Act_T := Entity (Actual);
13775
13776 -- Ada 2005 (AI-216): An Unchecked_Union subtype shall only be passed
13777 -- as a generic actual parameter if the corresponding formal type
13778 -- does not have a known_discriminant_part, or is a formal derived
13779 -- type that is an Unchecked_Union type.
13780
13781 if Is_Unchecked_Union (Base_Type (Act_T)) then
13782 if not Has_Discriminants (A_Gen_T)
13783 or else (Is_Derived_Type (A_Gen_T)
13784 and then Is_Unchecked_Union (A_Gen_T))
13785 then
13786 null;
13787 else
13788 Error_Msg_N ("unchecked union cannot be the actual for a "
13789 & "discriminated formal type", Act_T);
13790
13791 end if;
13792 end if;
13793
13794 -- Deal with fixed/floating restrictions
13795
13796 if Is_Floating_Point_Type (Act_T) then
13797 Check_Restriction (No_Floating_Point, Actual);
13798 elsif Is_Fixed_Point_Type (Act_T) then
13799 Check_Restriction (No_Fixed_Point, Actual);
13800 end if;
13801
13802 -- Deal with error of using incomplete type as generic actual.
13803 -- This includes limited views of a type, even if the non-limited
13804 -- view may be available.
13805
13806 if Ekind (Act_T) = E_Incomplete_Type
13807 or else (Is_Class_Wide_Type (Act_T)
13808 and then Ekind (Root_Type (Act_T)) = E_Incomplete_Type)
13809 then
13810 -- If the formal is an incomplete type, the actual can be
13811 -- incomplete as well.
13812
13813 if Ekind (A_Gen_T) = E_Incomplete_Type then
13814 null;
13815
13816 elsif Is_Class_Wide_Type (Act_T)
13817 or else No (Full_View (Act_T))
13818 then
13819 Error_Msg_N ("premature use of incomplete type", Actual);
13820 Abandon_Instantiation (Actual);
13821 else
13822 Act_T := Full_View (Act_T);
13823 Set_Entity (Actual, Act_T);
13824
13825 if Has_Private_Component (Act_T) then
13826 Error_Msg_N
13827 ("premature use of type with private component", Actual);
13828 end if;
13829 end if;
13830
13831 -- Deal with error of premature use of private type as generic actual
13832
13833 elsif Is_Private_Type (Act_T)
13834 and then Is_Private_Type (Base_Type (Act_T))
13835 and then not Is_Generic_Type (Act_T)
13836 and then not Is_Derived_Type (Act_T)
13837 and then No (Full_View (Root_Type (Act_T)))
13838 then
13839 -- If the formal is an incomplete type, the actual can be
13840 -- private or incomplete as well.
13841
13842 if Ekind (A_Gen_T) = E_Incomplete_Type then
13843 null;
13844 else
13845 Error_Msg_N ("premature use of private type", Actual);
13846 end if;
13847
13848 elsif Has_Private_Component (Act_T) then
13849 Error_Msg_N
13850 ("premature use of type with private component", Actual);
13851 end if;
13852
13853 Set_Instance_Of (A_Gen_T, Act_T);
13854
13855 -- If the type is generic, the class-wide type may also be used
13856
13857 if Is_Tagged_Type (A_Gen_T)
13858 and then Is_Tagged_Type (Act_T)
13859 and then not Is_Class_Wide_Type (A_Gen_T)
13860 then
13861 Set_Instance_Of (Class_Wide_Type (A_Gen_T),
13862 Class_Wide_Type (Act_T));
13863 end if;
13864
13865 if not Is_Abstract_Type (A_Gen_T)
13866 and then Is_Abstract_Type (Act_T)
13867 then
13868 Error_Msg_N
13869 ("actual of non-abstract formal cannot be abstract", Actual);
13870 end if;
13871
13872 -- A generic scalar type is a first subtype for which we generate
13873 -- an anonymous base type. Indicate that the instance of this base
13874 -- is the base type of the actual.
13875
13876 if Is_Scalar_Type (A_Gen_T) then
13877 Set_Instance_Of (Etype (A_Gen_T), Etype (Act_T));
13878 end if;
13879 end if;
13880
13881 Check_Shared_Variable_Control_Aspects;
13882
13883 if Error_Posted (Act_T) then
13884 null;
13885 else
13886 case Nkind (Def) is
13887 when N_Formal_Private_Type_Definition =>
13888 Validate_Private_Type_Instance;
13889
13890 when N_Formal_Incomplete_Type_Definition =>
13891 Validate_Incomplete_Type_Instance;
13892
13893 when N_Formal_Derived_Type_Definition =>
13894 Validate_Derived_Type_Instance;
13895
13896 when N_Formal_Discrete_Type_Definition =>
13897 if not Is_Discrete_Type (Act_T) then
13898 Error_Msg_NE
13899 ("expect discrete type in instantiation of&",
13900 Actual, Gen_T);
13901 Abandon_Instantiation (Actual);
13902 end if;
13903
13904 Diagnose_Predicated_Actual;
13905
13906 when N_Formal_Signed_Integer_Type_Definition =>
13907 if not Is_Signed_Integer_Type (Act_T) then
13908 Error_Msg_NE
13909 ("expect signed integer type in instantiation of&",
13910 Actual, Gen_T);
13911 Abandon_Instantiation (Actual);
13912 end if;
13913
13914 Diagnose_Predicated_Actual;
13915
13916 when N_Formal_Modular_Type_Definition =>
13917 if not Is_Modular_Integer_Type (Act_T) then
13918 Error_Msg_NE
13919 ("expect modular type in instantiation of &",
13920 Actual, Gen_T);
13921 Abandon_Instantiation (Actual);
13922 end if;
13923
13924 Diagnose_Predicated_Actual;
13925
13926 when N_Formal_Floating_Point_Definition =>
13927 if not Is_Floating_Point_Type (Act_T) then
13928 Error_Msg_NE
13929 ("expect float type in instantiation of &", Actual, Gen_T);
13930 Abandon_Instantiation (Actual);
13931 end if;
13932
13933 when N_Formal_Ordinary_Fixed_Point_Definition =>
13934 if not Is_Ordinary_Fixed_Point_Type (Act_T) then
13935 Error_Msg_NE
13936 ("expect ordinary fixed point type in instantiation of &",
13937 Actual, Gen_T);
13938 Abandon_Instantiation (Actual);
13939 end if;
13940
13941 when N_Formal_Decimal_Fixed_Point_Definition =>
13942 if not Is_Decimal_Fixed_Point_Type (Act_T) then
13943 Error_Msg_NE
13944 ("expect decimal type in instantiation of &",
13945 Actual, Gen_T);
13946 Abandon_Instantiation (Actual);
13947 end if;
13948
13949 when N_Array_Type_Definition =>
13950 Validate_Array_Type_Instance;
13951
13952 when N_Access_To_Object_Definition =>
13953 Validate_Access_Type_Instance;
13954
13955 when N_Access_Function_Definition
13956 | N_Access_Procedure_Definition
13957 =>
13958 Validate_Access_Subprogram_Instance;
13959
13960 when N_Record_Definition =>
13961 Validate_Interface_Type_Instance;
13962
13963 when N_Derived_Type_Definition =>
13964 Validate_Derived_Interface_Type_Instance;
13965
13966 when others =>
13967 raise Program_Error;
13968 end case;
13969 end if;
13970
13971 Subt := New_Copy (Gen_T);
13972
13973 -- Use adjusted sloc of subtype name as the location for other nodes in
13974 -- the subtype declaration.
13975
13976 Loc := Sloc (Subt);
13977
13978 Decl_Node :=
13979 Make_Subtype_Declaration (Loc,
13980 Defining_Identifier => Subt,
13981 Subtype_Indication => New_Occurrence_Of (Act_T, Loc));
13982
13983 -- Record whether the actual is private at this point, so that
13984 -- Check_Generic_Actuals can restore its proper view before the
13985 -- semantic analysis of the instance.
13986
13987 if Is_Private_Type (Act_T) then
13988 Set_Has_Private_View (Subtype_Indication (Decl_Node));
13989 end if;
13990
13991 -- In Ada 2012 the actual may be a limited view. Indicate that
13992 -- the local subtype must be treated as such.
13993
13994 if From_Limited_With (Act_T) then
13995 Set_Ekind (Subt, E_Incomplete_Subtype);
13996 Set_From_Limited_With (Subt);
13997 end if;
13998
13999 Decl_Nodes := New_List (Decl_Node);
14000
14001 -- Flag actual derived types so their elaboration produces the
14002 -- appropriate renamings for the primitive operations of the ancestor.
14003 -- Flag actual for formal private types as well, to determine whether
14004 -- operations in the private part may override inherited operations.
14005 -- If the formal has an interface list, the ancestor is not the
14006 -- parent, but the analyzed formal that includes the interface
14007 -- operations of all its progenitors.
14008
14009 -- Same treatment for formal private types, so we can check whether the
14010 -- type is tagged limited when validating derivations in the private
14011 -- part. (See AI05-096).
14012
14013 if Nkind (Def) = N_Formal_Derived_Type_Definition then
14014 if Present (Interface_List (Def)) then
14015 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
14016 else
14017 Set_Generic_Parent_Type (Decl_Node, Ancestor);
14018 end if;
14019
14020 elsif Nkind_In (Def, N_Formal_Private_Type_Definition,
14021 N_Formal_Incomplete_Type_Definition)
14022 then
14023 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
14024 end if;
14025
14026 -- If the actual is a synchronized type that implements an interface,
14027 -- the primitive operations are attached to the corresponding record,
14028 -- and we have to treat it as an additional generic actual, so that its
14029 -- primitive operations become visible in the instance. The task or
14030 -- protected type itself does not carry primitive operations.
14031
14032 if Is_Concurrent_Type (Act_T)
14033 and then Is_Tagged_Type (Act_T)
14034 and then Present (Corresponding_Record_Type (Act_T))
14035 and then Present (Ancestor)
14036 and then Is_Interface (Ancestor)
14037 then
14038 declare
14039 Corr_Rec : constant Entity_Id :=
14040 Corresponding_Record_Type (Act_T);
14041 New_Corr : Entity_Id;
14042 Corr_Decl : Node_Id;
14043
14044 begin
14045 New_Corr := Make_Temporary (Loc, 'S');
14046 Corr_Decl :=
14047 Make_Subtype_Declaration (Loc,
14048 Defining_Identifier => New_Corr,
14049 Subtype_Indication =>
14050 New_Occurrence_Of (Corr_Rec, Loc));
14051 Append_To (Decl_Nodes, Corr_Decl);
14052
14053 if Ekind (Act_T) = E_Task_Type then
14054 Set_Ekind (Subt, E_Task_Subtype);
14055 else
14056 Set_Ekind (Subt, E_Protected_Subtype);
14057 end if;
14058
14059 Set_Corresponding_Record_Type (Subt, Corr_Rec);
14060 Set_Generic_Parent_Type (Corr_Decl, Ancestor);
14061 Set_Generic_Parent_Type (Decl_Node, Empty);
14062 end;
14063 end if;
14064
14065 -- For a floating-point type, capture dimension info if any, because
14066 -- the generated subtype declaration does not come from source and
14067 -- will not process dimensions.
14068
14069 if Is_Floating_Point_Type (Act_T) then
14070 Copy_Dimensions (Act_T, Subt);
14071 end if;
14072
14073 return Decl_Nodes;
14074 end Instantiate_Type;
14075
14076 ---------------------
14077 -- Is_In_Main_Unit --
14078 ---------------------
14079
14080 function Is_In_Main_Unit (N : Node_Id) return Boolean is
14081 Unum : constant Unit_Number_Type := Get_Source_Unit (N);
14082 Current_Unit : Node_Id;
14083
14084 begin
14085 if Unum = Main_Unit then
14086 return True;
14087
14088 -- If the current unit is a subunit then it is either the main unit or
14089 -- is being compiled as part of the main unit.
14090
14091 elsif Nkind (N) = N_Compilation_Unit then
14092 return Nkind (Unit (N)) = N_Subunit;
14093 end if;
14094
14095 Current_Unit := Parent (N);
14096 while Present (Current_Unit)
14097 and then Nkind (Current_Unit) /= N_Compilation_Unit
14098 loop
14099 Current_Unit := Parent (Current_Unit);
14100 end loop;
14101
14102 -- The instantiation node is in the main unit, or else the current node
14103 -- (perhaps as the result of nested instantiations) is in the main unit,
14104 -- or in the declaration of the main unit, which in this last case must
14105 -- be a body.
14106
14107 return
14108 Current_Unit = Cunit (Main_Unit)
14109 or else Current_Unit = Library_Unit (Cunit (Main_Unit))
14110 or else (Present (Current_Unit)
14111 and then Present (Library_Unit (Current_Unit))
14112 and then Is_In_Main_Unit (Library_Unit (Current_Unit)));
14113 end Is_In_Main_Unit;
14114
14115 ----------------------------
14116 -- Load_Parent_Of_Generic --
14117 ----------------------------
14118
14119 procedure Load_Parent_Of_Generic
14120 (N : Node_Id;
14121 Spec : Node_Id;
14122 Body_Optional : Boolean := False)
14123 is
14124 Comp_Unit : constant Node_Id := Cunit (Get_Source_Unit (Spec));
14125 Saved_Style_Check : constant Boolean := Style_Check;
14126 Saved_Warnings : constant Warning_Record := Save_Warnings;
14127 True_Parent : Node_Id;
14128 Inst_Node : Node_Id;
14129 OK : Boolean;
14130 Previous_Instances : constant Elist_Id := New_Elmt_List;
14131
14132 procedure Collect_Previous_Instances (Decls : List_Id);
14133 -- Collect all instantiations in the given list of declarations, that
14134 -- precede the generic that we need to load. If the bodies of these
14135 -- instantiations are available, we must analyze them, to ensure that
14136 -- the public symbols generated are the same when the unit is compiled
14137 -- to generate code, and when it is compiled in the context of a unit
14138 -- that needs a particular nested instance. This process is applied to
14139 -- both package and subprogram instances.
14140
14141 --------------------------------
14142 -- Collect_Previous_Instances --
14143 --------------------------------
14144
14145 procedure Collect_Previous_Instances (Decls : List_Id) is
14146 Decl : Node_Id;
14147
14148 begin
14149 Decl := First (Decls);
14150 while Present (Decl) loop
14151 if Sloc (Decl) >= Sloc (Inst_Node) then
14152 return;
14153
14154 -- If Decl is an instantiation, then record it as requiring
14155 -- instantiation of the corresponding body, except if it is an
14156 -- abbreviated instantiation generated internally for conformance
14157 -- checking purposes only for the case of a formal package
14158 -- declared without a box (see Instantiate_Formal_Package). Such
14159 -- an instantiation does not generate any code (the actual code
14160 -- comes from actual) and thus does not need to be analyzed here.
14161 -- If the instantiation appears with a generic package body it is
14162 -- not analyzed here either.
14163
14164 elsif Nkind (Decl) = N_Package_Instantiation
14165 and then not Is_Internal (Defining_Entity (Decl))
14166 then
14167 Append_Elmt (Decl, Previous_Instances);
14168
14169 -- For a subprogram instantiation, omit instantiations intrinsic
14170 -- operations (Unchecked_Conversions, etc.) that have no bodies.
14171
14172 elsif Nkind_In (Decl, N_Function_Instantiation,
14173 N_Procedure_Instantiation)
14174 and then not Is_Intrinsic_Subprogram (Entity (Name (Decl)))
14175 then
14176 Append_Elmt (Decl, Previous_Instances);
14177
14178 elsif Nkind (Decl) = N_Package_Declaration then
14179 Collect_Previous_Instances
14180 (Visible_Declarations (Specification (Decl)));
14181 Collect_Previous_Instances
14182 (Private_Declarations (Specification (Decl)));
14183
14184 -- Previous non-generic bodies may contain instances as well
14185
14186 elsif Nkind (Decl) = N_Package_Body
14187 and then Ekind (Corresponding_Spec (Decl)) /= E_Generic_Package
14188 then
14189 Collect_Previous_Instances (Declarations (Decl));
14190
14191 elsif Nkind (Decl) = N_Subprogram_Body
14192 and then not Acts_As_Spec (Decl)
14193 and then not Is_Generic_Subprogram (Corresponding_Spec (Decl))
14194 then
14195 Collect_Previous_Instances (Declarations (Decl));
14196 end if;
14197
14198 Next (Decl);
14199 end loop;
14200 end Collect_Previous_Instances;
14201
14202 -- Start of processing for Load_Parent_Of_Generic
14203
14204 begin
14205 if not In_Same_Source_Unit (N, Spec)
14206 or else Nkind (Unit (Comp_Unit)) = N_Package_Declaration
14207 or else (Nkind (Unit (Comp_Unit)) = N_Package_Body
14208 and then not Is_In_Main_Unit (Spec))
14209 then
14210 -- Find body of parent of spec, and analyze it. A special case arises
14211 -- when the parent is an instantiation, that is to say when we are
14212 -- currently instantiating a nested generic. In that case, there is
14213 -- no separate file for the body of the enclosing instance. Instead,
14214 -- the enclosing body must be instantiated as if it were a pending
14215 -- instantiation, in order to produce the body for the nested generic
14216 -- we require now. Note that in that case the generic may be defined
14217 -- in a package body, the instance defined in the same package body,
14218 -- and the original enclosing body may not be in the main unit.
14219
14220 Inst_Node := Empty;
14221
14222 True_Parent := Parent (Spec);
14223 while Present (True_Parent)
14224 and then Nkind (True_Parent) /= N_Compilation_Unit
14225 loop
14226 if Nkind (True_Parent) = N_Package_Declaration
14227 and then
14228 Nkind (Original_Node (True_Parent)) = N_Package_Instantiation
14229 then
14230 -- Parent is a compilation unit that is an instantiation, and
14231 -- instantiation node has been replaced with package decl.
14232
14233 Inst_Node := Original_Node (True_Parent);
14234 exit;
14235
14236 elsif Nkind (True_Parent) = N_Package_Declaration
14237 and then Nkind (Parent (True_Parent)) = N_Compilation_Unit
14238 and then
14239 Nkind (Unit (Parent (True_Parent))) = N_Package_Instantiation
14240 then
14241 -- Parent is a compilation unit that is an instantiation, but
14242 -- instantiation node has not been replaced with package decl.
14243
14244 Inst_Node := Unit (Parent (True_Parent));
14245 exit;
14246
14247 elsif Nkind (True_Parent) = N_Package_Declaration
14248 and then Nkind (Parent (True_Parent)) /= N_Compilation_Unit
14249 and then Present (Generic_Parent (Specification (True_Parent)))
14250 then
14251 -- Parent is an instantiation within another specification.
14252 -- Declaration for instance has been inserted before original
14253 -- instantiation node. A direct link would be preferable?
14254
14255 Inst_Node := Next (True_Parent);
14256 while Present (Inst_Node)
14257 and then Nkind (Inst_Node) /= N_Package_Instantiation
14258 loop
14259 Next (Inst_Node);
14260 end loop;
14261
14262 -- If the instance appears within a generic, and the generic
14263 -- unit is defined within a formal package of the enclosing
14264 -- generic, there is no generic body available, and none
14265 -- needed. A more precise test should be used ???
14266
14267 if No (Inst_Node) then
14268 return;
14269 end if;
14270
14271 exit;
14272
14273 else
14274 True_Parent := Parent (True_Parent);
14275 end if;
14276 end loop;
14277
14278 -- Case where we are currently instantiating a nested generic
14279
14280 if Present (Inst_Node) then
14281 if Nkind (Parent (True_Parent)) = N_Compilation_Unit then
14282
14283 -- Instantiation node and declaration of instantiated package
14284 -- were exchanged when only the declaration was needed.
14285 -- Restore instantiation node before proceeding with body.
14286
14287 Set_Unit (Parent (True_Parent), Inst_Node);
14288 end if;
14289
14290 -- Now complete instantiation of enclosing body, if it appears in
14291 -- some other unit. If it appears in the current unit, the body
14292 -- will have been instantiated already.
14293
14294 if No (Corresponding_Body (Instance_Spec (Inst_Node))) then
14295
14296 -- We need to determine the expander mode to instantiate the
14297 -- enclosing body. Because the generic body we need may use
14298 -- global entities declared in the enclosing package (including
14299 -- aggregates) it is in general necessary to compile this body
14300 -- with expansion enabled, except if we are within a generic
14301 -- package, in which case the usual generic rule applies.
14302
14303 declare
14304 Exp_Status : Boolean := True;
14305 Scop : Entity_Id;
14306
14307 begin
14308 -- Loop through scopes looking for generic package
14309
14310 Scop := Scope (Defining_Entity (Instance_Spec (Inst_Node)));
14311 while Present (Scop)
14312 and then Scop /= Standard_Standard
14313 loop
14314 if Ekind (Scop) = E_Generic_Package then
14315 Exp_Status := False;
14316 exit;
14317 end if;
14318
14319 Scop := Scope (Scop);
14320 end loop;
14321
14322 -- Collect previous instantiations in the unit that contains
14323 -- the desired generic.
14324
14325 if Nkind (Parent (True_Parent)) /= N_Compilation_Unit
14326 and then not Body_Optional
14327 then
14328 declare
14329 Decl : Elmt_Id;
14330 Info : Pending_Body_Info;
14331 Par : Node_Id;
14332
14333 begin
14334 Par := Parent (Inst_Node);
14335 while Present (Par) loop
14336 exit when Nkind (Parent (Par)) = N_Compilation_Unit;
14337 Par := Parent (Par);
14338 end loop;
14339
14340 pragma Assert (Present (Par));
14341
14342 if Nkind (Par) = N_Package_Body then
14343 Collect_Previous_Instances (Declarations (Par));
14344
14345 elsif Nkind (Par) = N_Package_Declaration then
14346 Collect_Previous_Instances
14347 (Visible_Declarations (Specification (Par)));
14348 Collect_Previous_Instances
14349 (Private_Declarations (Specification (Par)));
14350
14351 else
14352 -- Enclosing unit is a subprogram body. In this
14353 -- case all instance bodies are processed in order
14354 -- and there is no need to collect them separately.
14355
14356 null;
14357 end if;
14358
14359 Decl := First_Elmt (Previous_Instances);
14360 while Present (Decl) loop
14361 Info :=
14362 (Act_Decl =>
14363 Instance_Spec (Node (Decl)),
14364 Config_Switches => Save_Config_Switches,
14365 Current_Sem_Unit =>
14366 Get_Code_Unit (Sloc (Node (Decl))),
14367 Expander_Status => Exp_Status,
14368 Inst_Node => Node (Decl),
14369 Local_Suppress_Stack_Top =>
14370 Local_Suppress_Stack_Top,
14371 Scope_Suppress => Scope_Suppress,
14372 Warnings => Save_Warnings);
14373
14374 -- Package instance
14375
14376 if Nkind (Node (Decl)) = N_Package_Instantiation
14377 then
14378 Instantiate_Package_Body
14379 (Info, Body_Optional => True);
14380
14381 -- Subprogram instance
14382
14383 else
14384 -- The instance_spec is in the wrapper package,
14385 -- usually followed by its local renaming
14386 -- declaration. See Build_Subprogram_Renaming
14387 -- for details. If the instance carries aspects,
14388 -- these result in the corresponding pragmas,
14389 -- inserted after the subprogram declaration.
14390 -- They must be skipped as well when retrieving
14391 -- the desired spec. Some of them may have been
14392 -- rewritten as null statements.
14393 -- A direct link would be more robust ???
14394
14395 declare
14396 Decl : Node_Id :=
14397 (Last (Visible_Declarations
14398 (Specification (Info.Act_Decl))));
14399 begin
14400 while Nkind_In (Decl,
14401 N_Null_Statement,
14402 N_Pragma,
14403 N_Subprogram_Renaming_Declaration)
14404 loop
14405 Decl := Prev (Decl);
14406 end loop;
14407
14408 Info.Act_Decl := Decl;
14409 end;
14410
14411 Instantiate_Subprogram_Body
14412 (Info, Body_Optional => True);
14413 end if;
14414
14415 Next_Elmt (Decl);
14416 end loop;
14417 end;
14418 end if;
14419
14420 Instantiate_Package_Body
14421 (Body_Info =>
14422 ((Act_Decl => True_Parent,
14423 Config_Switches => Save_Config_Switches,
14424 Current_Sem_Unit =>
14425 Get_Code_Unit (Sloc (Inst_Node)),
14426 Expander_Status => Exp_Status,
14427 Inst_Node => Inst_Node,
14428 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
14429 Scope_Suppress => Scope_Suppress,
14430 Warnings => Save_Warnings)),
14431 Body_Optional => Body_Optional);
14432 end;
14433 end if;
14434
14435 -- Case where we are not instantiating a nested generic
14436
14437 else
14438 Opt.Style_Check := False;
14439 Expander_Mode_Save_And_Set (True);
14440 Load_Needed_Body (Comp_Unit, OK);
14441 Opt.Style_Check := Saved_Style_Check;
14442 Restore_Warnings (Saved_Warnings);
14443 Expander_Mode_Restore;
14444
14445 if not OK
14446 and then Unit_Requires_Body (Defining_Entity (Spec))
14447 and then not Body_Optional
14448 then
14449 declare
14450 Bname : constant Unit_Name_Type :=
14451 Get_Body_Name (Get_Unit_Name (Unit (Comp_Unit)));
14452
14453 begin
14454 -- In CodePeer mode, the missing body may make the analysis
14455 -- incomplete, but we do not treat it as fatal.
14456
14457 if CodePeer_Mode then
14458 return;
14459
14460 else
14461 Error_Msg_Unit_1 := Bname;
14462 Error_Msg_N ("this instantiation requires$!", N);
14463 Error_Msg_File_1 :=
14464 Get_File_Name (Bname, Subunit => False);
14465 Error_Msg_N ("\but file{ was not found!", N);
14466 raise Unrecoverable_Error;
14467 end if;
14468 end;
14469 end if;
14470 end if;
14471 end if;
14472
14473 -- If loading parent of the generic caused an instantiation circularity,
14474 -- we abandon compilation at this point, because otherwise in some cases
14475 -- we get into trouble with infinite recursions after this point.
14476
14477 if Circularity_Detected then
14478 raise Unrecoverable_Error;
14479 end if;
14480 end Load_Parent_Of_Generic;
14481
14482 ---------------------------------
14483 -- Map_Formal_Package_Entities --
14484 ---------------------------------
14485
14486 procedure Map_Formal_Package_Entities (Form : Entity_Id; Act : Entity_Id) is
14487 E1 : Entity_Id;
14488 E2 : Entity_Id;
14489
14490 begin
14491 Set_Instance_Of (Form, Act);
14492
14493 -- Traverse formal and actual package to map the corresponding entities.
14494 -- We skip over internal entities that may be generated during semantic
14495 -- analysis, and find the matching entities by name, given that they
14496 -- must appear in the same order.
14497
14498 E1 := First_Entity (Form);
14499 E2 := First_Entity (Act);
14500 while Present (E1) and then E1 /= First_Private_Entity (Form) loop
14501 -- Could this test be a single condition??? Seems like it could, and
14502 -- isn't FPE (Form) a constant anyway???
14503
14504 if not Is_Internal (E1)
14505 and then Present (Parent (E1))
14506 and then not Is_Class_Wide_Type (E1)
14507 and then not Is_Internal_Name (Chars (E1))
14508 then
14509 while Present (E2) and then Chars (E2) /= Chars (E1) loop
14510 Next_Entity (E2);
14511 end loop;
14512
14513 if No (E2) then
14514 exit;
14515 else
14516 Set_Instance_Of (E1, E2);
14517
14518 if Is_Type (E1) and then Is_Tagged_Type (E2) then
14519 Set_Instance_Of (Class_Wide_Type (E1), Class_Wide_Type (E2));
14520 end if;
14521
14522 if Is_Constrained (E1) then
14523 Set_Instance_Of (Base_Type (E1), Base_Type (E2));
14524 end if;
14525
14526 if Ekind (E1) = E_Package and then No (Renamed_Object (E1)) then
14527 Map_Formal_Package_Entities (E1, E2);
14528 end if;
14529 end if;
14530 end if;
14531
14532 Next_Entity (E1);
14533 end loop;
14534 end Map_Formal_Package_Entities;
14535
14536 -----------------------
14537 -- Move_Freeze_Nodes --
14538 -----------------------
14539
14540 procedure Move_Freeze_Nodes
14541 (Out_Of : Entity_Id;
14542 After : Node_Id;
14543 L : List_Id)
14544 is
14545 Decl : Node_Id;
14546 Next_Decl : Node_Id;
14547 Next_Node : Node_Id := After;
14548 Spec : Node_Id;
14549
14550 function Is_Outer_Type (T : Entity_Id) return Boolean;
14551 -- Check whether entity is declared in a scope external to that of the
14552 -- generic unit.
14553
14554 -------------------
14555 -- Is_Outer_Type --
14556 -------------------
14557
14558 function Is_Outer_Type (T : Entity_Id) return Boolean is
14559 Scop : Entity_Id := Scope (T);
14560
14561 begin
14562 if Scope_Depth (Scop) < Scope_Depth (Out_Of) then
14563 return True;
14564
14565 else
14566 while Scop /= Standard_Standard loop
14567 if Scop = Out_Of then
14568 return False;
14569 else
14570 Scop := Scope (Scop);
14571 end if;
14572 end loop;
14573
14574 return True;
14575 end if;
14576 end Is_Outer_Type;
14577
14578 -- Start of processing for Move_Freeze_Nodes
14579
14580 begin
14581 if No (L) then
14582 return;
14583 end if;
14584
14585 -- First remove the freeze nodes that may appear before all other
14586 -- declarations.
14587
14588 Decl := First (L);
14589 while Present (Decl)
14590 and then Nkind (Decl) = N_Freeze_Entity
14591 and then Is_Outer_Type (Entity (Decl))
14592 loop
14593 Decl := Remove_Head (L);
14594 Insert_After (Next_Node, Decl);
14595 Set_Analyzed (Decl, False);
14596 Next_Node := Decl;
14597 Decl := First (L);
14598 end loop;
14599
14600 -- Next scan the list of declarations and remove each freeze node that
14601 -- appears ahead of the current node.
14602
14603 while Present (Decl) loop
14604 while Present (Next (Decl))
14605 and then Nkind (Next (Decl)) = N_Freeze_Entity
14606 and then Is_Outer_Type (Entity (Next (Decl)))
14607 loop
14608 Next_Decl := Remove_Next (Decl);
14609 Insert_After (Next_Node, Next_Decl);
14610 Set_Analyzed (Next_Decl, False);
14611 Next_Node := Next_Decl;
14612 end loop;
14613
14614 -- If the declaration is a nested package or concurrent type, then
14615 -- recurse. Nested generic packages will have been processed from the
14616 -- inside out.
14617
14618 case Nkind (Decl) is
14619 when N_Package_Declaration =>
14620 Spec := Specification (Decl);
14621
14622 when N_Task_Type_Declaration =>
14623 Spec := Task_Definition (Decl);
14624
14625 when N_Protected_Type_Declaration =>
14626 Spec := Protected_Definition (Decl);
14627
14628 when others =>
14629 Spec := Empty;
14630 end case;
14631
14632 if Present (Spec) then
14633 Move_Freeze_Nodes (Out_Of, Next_Node, Visible_Declarations (Spec));
14634 Move_Freeze_Nodes (Out_Of, Next_Node, Private_Declarations (Spec));
14635 end if;
14636
14637 Next (Decl);
14638 end loop;
14639 end Move_Freeze_Nodes;
14640
14641 ----------------
14642 -- Next_Assoc --
14643 ----------------
14644
14645 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr is
14646 begin
14647 return Generic_Renamings.Table (E).Next_In_HTable;
14648 end Next_Assoc;
14649
14650 ------------------------
14651 -- Preanalyze_Actuals --
14652 ------------------------
14653
14654 procedure Preanalyze_Actuals (N : Node_Id; Inst : Entity_Id := Empty) is
14655 procedure Perform_Appropriate_Analysis (N : Node_Id);
14656 -- Determine if the actuals we are analyzing come from a generic
14657 -- instantiation that is a library unit and dispatch accordingly.
14658
14659 ----------------------------------
14660 -- Perform_Appropriate_Analysis --
14661 ----------------------------------
14662
14663 procedure Perform_Appropriate_Analysis (N : Node_Id) is
14664 begin
14665 -- When we have a library instantiation we cannot allow any expansion
14666 -- to occur, since there may be no place to put it. Instead, in that
14667 -- case we perform a preanalysis of the actual.
14668
14669 if Present (Inst) and then Is_Compilation_Unit (Inst) then
14670 Preanalyze (N);
14671 else
14672 Analyze (N);
14673 end if;
14674 end Perform_Appropriate_Analysis;
14675
14676 -- Local variables
14677
14678 Errs : constant Nat := Serious_Errors_Detected;
14679
14680 Assoc : Node_Id;
14681 Act : Node_Id;
14682
14683 Cur : Entity_Id := Empty;
14684 -- Current homograph of the instance name
14685
14686 Vis : Boolean := False;
14687 -- Saved visibility status of the current homograph
14688
14689 -- Start of processing for Preanalyze_Actuals
14690
14691 begin
14692 Assoc := First (Generic_Associations (N));
14693
14694 -- If the instance is a child unit, its name may hide an outer homonym,
14695 -- so make it invisible to perform name resolution on the actuals.
14696
14697 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name
14698 and then Present
14699 (Current_Entity (Defining_Identifier (Defining_Unit_Name (N))))
14700 then
14701 Cur := Current_Entity (Defining_Identifier (Defining_Unit_Name (N)));
14702
14703 if Is_Compilation_Unit (Cur) then
14704 Vis := Is_Immediately_Visible (Cur);
14705 Set_Is_Immediately_Visible (Cur, False);
14706 else
14707 Cur := Empty;
14708 end if;
14709 end if;
14710
14711 while Present (Assoc) loop
14712 if Nkind (Assoc) /= N_Others_Choice then
14713 Act := Explicit_Generic_Actual_Parameter (Assoc);
14714
14715 -- Within a nested instantiation, a defaulted actual is an empty
14716 -- association, so nothing to analyze. If the subprogram actual
14717 -- is an attribute, analyze prefix only, because actual is not a
14718 -- complete attribute reference.
14719
14720 -- If actual is an allocator, analyze expression only. The full
14721 -- analysis can generate code, and if instance is a compilation
14722 -- unit we have to wait until the package instance is installed
14723 -- to have a proper place to insert this code.
14724
14725 -- String literals may be operators, but at this point we do not
14726 -- know whether the actual is a formal subprogram or a string.
14727
14728 if No (Act) then
14729 null;
14730
14731 elsif Nkind (Act) = N_Attribute_Reference then
14732 Perform_Appropriate_Analysis (Prefix (Act));
14733
14734 elsif Nkind (Act) = N_Explicit_Dereference then
14735 Perform_Appropriate_Analysis (Prefix (Act));
14736
14737 elsif Nkind (Act) = N_Allocator then
14738 declare
14739 Expr : constant Node_Id := Expression (Act);
14740
14741 begin
14742 if Nkind (Expr) = N_Subtype_Indication then
14743 Perform_Appropriate_Analysis (Subtype_Mark (Expr));
14744
14745 -- Analyze separately each discriminant constraint, when
14746 -- given with a named association.
14747
14748 declare
14749 Constr : Node_Id;
14750
14751 begin
14752 Constr := First (Constraints (Constraint (Expr)));
14753 while Present (Constr) loop
14754 if Nkind (Constr) = N_Discriminant_Association then
14755 Perform_Appropriate_Analysis
14756 (Expression (Constr));
14757 else
14758 Perform_Appropriate_Analysis (Constr);
14759 end if;
14760
14761 Next (Constr);
14762 end loop;
14763 end;
14764
14765 else
14766 Perform_Appropriate_Analysis (Expr);
14767 end if;
14768 end;
14769
14770 elsif Nkind (Act) /= N_Operator_Symbol then
14771 Perform_Appropriate_Analysis (Act);
14772
14773 -- Within a package instance, mark actuals that are limited
14774 -- views, so their use can be moved to the body of the
14775 -- enclosing unit.
14776
14777 if Is_Entity_Name (Act)
14778 and then Is_Type (Entity (Act))
14779 and then From_Limited_With (Entity (Act))
14780 and then Present (Inst)
14781 then
14782 Append_Elmt (Entity (Act), Incomplete_Actuals (Inst));
14783 end if;
14784 end if;
14785
14786 if Errs /= Serious_Errors_Detected then
14787
14788 -- Do a minimal analysis of the generic, to prevent spurious
14789 -- warnings complaining about the generic being unreferenced,
14790 -- before abandoning the instantiation.
14791
14792 Perform_Appropriate_Analysis (Name (N));
14793
14794 if Is_Entity_Name (Name (N))
14795 and then Etype (Name (N)) /= Any_Type
14796 then
14797 Generate_Reference (Entity (Name (N)), Name (N));
14798 Set_Is_Instantiated (Entity (Name (N)));
14799 end if;
14800
14801 if Present (Cur) then
14802
14803 -- For the case of a child instance hiding an outer homonym,
14804 -- provide additional warning which might explain the error.
14805
14806 Set_Is_Immediately_Visible (Cur, Vis);
14807 Error_Msg_NE
14808 ("& hides outer unit with the same name??",
14809 N, Defining_Unit_Name (N));
14810 end if;
14811
14812 Abandon_Instantiation (Act);
14813 end if;
14814 end if;
14815
14816 Next (Assoc);
14817 end loop;
14818
14819 if Present (Cur) then
14820 Set_Is_Immediately_Visible (Cur, Vis);
14821 end if;
14822 end Preanalyze_Actuals;
14823
14824 -------------------------------
14825 -- Provide_Completing_Bodies --
14826 -------------------------------
14827
14828 procedure Provide_Completing_Bodies (N : Node_Id) is
14829 procedure Build_Completing_Body (Subp_Decl : Node_Id);
14830 -- Generate the completing body for subprogram declaration Subp_Decl
14831
14832 procedure Provide_Completing_Bodies_In (Decls : List_Id);
14833 -- Generating completing bodies for all subprograms found in declarative
14834 -- list Decls.
14835
14836 ---------------------------
14837 -- Build_Completing_Body --
14838 ---------------------------
14839
14840 procedure Build_Completing_Body (Subp_Decl : Node_Id) is
14841 Loc : constant Source_Ptr := Sloc (Subp_Decl);
14842 Subp_Id : constant Entity_Id := Defining_Entity (Subp_Decl);
14843 Spec : Node_Id;
14844
14845 begin
14846 -- Nothing to do if the subprogram already has a completing body
14847
14848 if Present (Corresponding_Body (Subp_Decl)) then
14849 return;
14850
14851 -- Mark the function as having a valid return statement even though
14852 -- the body contains a single raise statement.
14853
14854 elsif Ekind (Subp_Id) = E_Function then
14855 Set_Return_Present (Subp_Id);
14856 end if;
14857
14858 -- Clone the specification to obtain new entities and reset the only
14859 -- semantic field.
14860
14861 Spec := Copy_Subprogram_Spec (Specification (Subp_Decl));
14862 Set_Generic_Parent (Spec, Empty);
14863
14864 -- Generate:
14865 -- function Func ... return ... is
14866 -- <or>
14867 -- procedure Proc ... is
14868 -- begin
14869 -- raise Program_Error with "access before elaboration";
14870 -- edn Proc;
14871
14872 Insert_After_And_Analyze (Subp_Decl,
14873 Make_Subprogram_Body (Loc,
14874 Specification => Spec,
14875 Declarations => New_List,
14876 Handled_Statement_Sequence =>
14877 Make_Handled_Sequence_Of_Statements (Loc,
14878 Statements => New_List (
14879 Make_Raise_Program_Error (Loc,
14880 Reason => PE_Access_Before_Elaboration)))));
14881 end Build_Completing_Body;
14882
14883 ----------------------------------
14884 -- Provide_Completing_Bodies_In --
14885 ----------------------------------
14886
14887 procedure Provide_Completing_Bodies_In (Decls : List_Id) is
14888 Decl : Node_Id;
14889
14890 begin
14891 if Present (Decls) then
14892 Decl := First (Decls);
14893 while Present (Decl) loop
14894 Provide_Completing_Bodies (Decl);
14895 Next (Decl);
14896 end loop;
14897 end if;
14898 end Provide_Completing_Bodies_In;
14899
14900 -- Local variables
14901
14902 Spec : Node_Id;
14903
14904 -- Start of processing for Provide_Completing_Bodies
14905
14906 begin
14907 if Nkind (N) = N_Package_Declaration then
14908 Spec := Specification (N);
14909
14910 Push_Scope (Defining_Entity (N));
14911 Provide_Completing_Bodies_In (Visible_Declarations (Spec));
14912 Provide_Completing_Bodies_In (Private_Declarations (Spec));
14913 Pop_Scope;
14914
14915 elsif Nkind (N) = N_Subprogram_Declaration then
14916 Build_Completing_Body (N);
14917 end if;
14918 end Provide_Completing_Bodies;
14919
14920 -------------------
14921 -- Remove_Parent --
14922 -------------------
14923
14924 procedure Remove_Parent (In_Body : Boolean := False) is
14925 S : Entity_Id := Current_Scope;
14926 -- S is the scope containing the instantiation just completed. The scope
14927 -- stack contains the parent instances of the instantiation, followed by
14928 -- the original S.
14929
14930 Cur_P : Entity_Id;
14931 E : Entity_Id;
14932 P : Entity_Id;
14933 Hidden : Elmt_Id;
14934
14935 begin
14936 -- After child instantiation is complete, remove from scope stack the
14937 -- extra copy of the current scope, and then remove parent instances.
14938
14939 if not In_Body then
14940 Pop_Scope;
14941
14942 while Current_Scope /= S loop
14943 P := Current_Scope;
14944 End_Package_Scope (Current_Scope);
14945
14946 if In_Open_Scopes (P) then
14947 E := First_Entity (P);
14948 while Present (E) loop
14949 Set_Is_Immediately_Visible (E, True);
14950 Next_Entity (E);
14951 end loop;
14952
14953 -- If instantiation is declared in a block, it is the enclosing
14954 -- scope that might be a parent instance. Note that only one
14955 -- block can be involved, because the parent instances have
14956 -- been installed within it.
14957
14958 if Ekind (P) = E_Block then
14959 Cur_P := Scope (P);
14960 else
14961 Cur_P := P;
14962 end if;
14963
14964 if Is_Generic_Instance (Cur_P) and then P /= Current_Scope then
14965 -- We are within an instance of some sibling. Retain
14966 -- visibility of parent, for proper subsequent cleanup, and
14967 -- reinstall private declarations as well.
14968
14969 Set_In_Private_Part (P);
14970 Install_Private_Declarations (P);
14971 end if;
14972
14973 -- If the ultimate parent is a top-level unit recorded in
14974 -- Instance_Parent_Unit, then reset its visibility to what it was
14975 -- before instantiation. (It's not clear what the purpose is of
14976 -- testing whether Scope (P) is In_Open_Scopes, but that test was
14977 -- present before the ultimate parent test was added.???)
14978
14979 elsif not In_Open_Scopes (Scope (P))
14980 or else (P = Instance_Parent_Unit
14981 and then not Parent_Unit_Visible)
14982 then
14983 Set_Is_Immediately_Visible (P, False);
14984
14985 -- If the current scope is itself an instantiation of a generic
14986 -- nested within P, and we are in the private part of body of this
14987 -- instantiation, restore the full views of P, that were removed
14988 -- in End_Package_Scope above. This obscure case can occur when a
14989 -- subunit of a generic contains an instance of a child unit of
14990 -- its generic parent unit.
14991
14992 elsif S = Current_Scope and then Is_Generic_Instance (S) then
14993 declare
14994 Par : constant Entity_Id :=
14995 Generic_Parent (Package_Specification (S));
14996 begin
14997 if Present (Par)
14998 and then P = Scope (Par)
14999 and then (In_Package_Body (S) or else In_Private_Part (S))
15000 then
15001 Set_In_Private_Part (P);
15002 Install_Private_Declarations (P);
15003 end if;
15004 end;
15005 end if;
15006 end loop;
15007
15008 -- Reset visibility of entities in the enclosing scope
15009
15010 Set_Is_Hidden_Open_Scope (Current_Scope, False);
15011
15012 Hidden := First_Elmt (Hidden_Entities);
15013 while Present (Hidden) loop
15014 Set_Is_Immediately_Visible (Node (Hidden), True);
15015 Next_Elmt (Hidden);
15016 end loop;
15017
15018 else
15019 -- Each body is analyzed separately, and there is no context that
15020 -- needs preserving from one body instance to the next, so remove all
15021 -- parent scopes that have been installed.
15022
15023 while Present (S) loop
15024 End_Package_Scope (S);
15025 Set_Is_Immediately_Visible (S, False);
15026 S := Current_Scope;
15027 exit when S = Standard_Standard;
15028 end loop;
15029 end if;
15030 end Remove_Parent;
15031
15032 -----------------
15033 -- Restore_Env --
15034 -----------------
15035
15036 procedure Restore_Env is
15037 Saved : Instance_Env renames Instance_Envs.Table (Instance_Envs.Last);
15038
15039 begin
15040 if No (Current_Instantiated_Parent.Act_Id) then
15041 -- Restore environment after subprogram inlining
15042
15043 Restore_Private_Views (Empty);
15044 end if;
15045
15046 Current_Instantiated_Parent := Saved.Instantiated_Parent;
15047 Exchanged_Views := Saved.Exchanged_Views;
15048 Hidden_Entities := Saved.Hidden_Entities;
15049 Current_Sem_Unit := Saved.Current_Sem_Unit;
15050 Parent_Unit_Visible := Saved.Parent_Unit_Visible;
15051 Instance_Parent_Unit := Saved.Instance_Parent_Unit;
15052
15053 Restore_Config_Switches (Saved.Switches);
15054
15055 Instance_Envs.Decrement_Last;
15056 end Restore_Env;
15057
15058 ---------------------------
15059 -- Restore_Private_Views --
15060 ---------------------------
15061
15062 procedure Restore_Private_Views
15063 (Pack_Id : Entity_Id;
15064 Is_Package : Boolean := True)
15065 is
15066 M : Elmt_Id;
15067 E : Entity_Id;
15068 Typ : Entity_Id;
15069 Dep_Elmt : Elmt_Id;
15070 Dep_Typ : Node_Id;
15071
15072 procedure Restore_Nested_Formal (Formal : Entity_Id);
15073 -- Hide the generic formals of formal packages declared with box which
15074 -- were reachable in the current instantiation.
15075
15076 ---------------------------
15077 -- Restore_Nested_Formal --
15078 ---------------------------
15079
15080 procedure Restore_Nested_Formal (Formal : Entity_Id) is
15081 Ent : Entity_Id;
15082
15083 begin
15084 if Present (Renamed_Object (Formal))
15085 and then Denotes_Formal_Package (Renamed_Object (Formal), True)
15086 then
15087 return;
15088
15089 elsif Present (Associated_Formal_Package (Formal)) then
15090 Ent := First_Entity (Formal);
15091 while Present (Ent) loop
15092 exit when Ekind (Ent) = E_Package
15093 and then Renamed_Entity (Ent) = Renamed_Entity (Formal);
15094
15095 Set_Is_Hidden (Ent);
15096 Set_Is_Potentially_Use_Visible (Ent, False);
15097
15098 -- If package, then recurse
15099
15100 if Ekind (Ent) = E_Package then
15101 Restore_Nested_Formal (Ent);
15102 end if;
15103
15104 Next_Entity (Ent);
15105 end loop;
15106 end if;
15107 end Restore_Nested_Formal;
15108
15109 -- Start of processing for Restore_Private_Views
15110
15111 begin
15112 M := First_Elmt (Exchanged_Views);
15113 while Present (M) loop
15114 Typ := Node (M);
15115
15116 -- Subtypes of types whose views have been exchanged, and that are
15117 -- defined within the instance, were not on the Private_Dependents
15118 -- list on entry to the instance, so they have to be exchanged
15119 -- explicitly now, in order to remain consistent with the view of the
15120 -- parent type.
15121
15122 if Ekind_In (Typ, E_Private_Type,
15123 E_Limited_Private_Type,
15124 E_Record_Type_With_Private)
15125 then
15126 Dep_Elmt := First_Elmt (Private_Dependents (Typ));
15127 while Present (Dep_Elmt) loop
15128 Dep_Typ := Node (Dep_Elmt);
15129
15130 if Scope (Dep_Typ) = Pack_Id
15131 and then Present (Full_View (Dep_Typ))
15132 then
15133 Replace_Elmt (Dep_Elmt, Full_View (Dep_Typ));
15134 Exchange_Declarations (Dep_Typ);
15135 end if;
15136
15137 Next_Elmt (Dep_Elmt);
15138 end loop;
15139 end if;
15140
15141 Exchange_Declarations (Node (M));
15142 Next_Elmt (M);
15143 end loop;
15144
15145 if No (Pack_Id) then
15146 return;
15147 end if;
15148
15149 -- Make the generic formal parameters private, and make the formal types
15150 -- into subtypes of the actuals again.
15151
15152 E := First_Entity (Pack_Id);
15153 while Present (E) loop
15154 Set_Is_Hidden (E, True);
15155
15156 if Is_Type (E)
15157 and then Nkind (Parent (E)) = N_Subtype_Declaration
15158 then
15159 -- If the actual for E is itself a generic actual type from
15160 -- an enclosing instance, E is still a generic actual type
15161 -- outside of the current instance. This matter when resolving
15162 -- an overloaded call that may be ambiguous in the enclosing
15163 -- instance, when two of its actuals coincide.
15164
15165 if Is_Entity_Name (Subtype_Indication (Parent (E)))
15166 and then Is_Generic_Actual_Type
15167 (Entity (Subtype_Indication (Parent (E))))
15168 then
15169 null;
15170 else
15171 Set_Is_Generic_Actual_Type (E, False);
15172
15173 -- It might seem reasonable to clear the Is_Generic_Actual_Type
15174 -- flag also on the Full_View if the type is private, since it
15175 -- was set also on this Full_View. However, this flag is relied
15176 -- upon by Covers to spot "types exported from instantiations"
15177 -- which are implicit Full_Views built for instantiations made
15178 -- on private types and we get type mismatches if we do it when
15179 -- the block exchanging the declarations below triggers ???
15180
15181 -- if Is_Private_Type (E) and then Present (Full_View (E)) then
15182 -- Set_Is_Generic_Actual_Type (Full_View (E), False);
15183 -- end if;
15184 end if;
15185
15186 -- An unusual case of aliasing: the actual may also be directly
15187 -- visible in the generic, and be private there, while it is fully
15188 -- visible in the context of the instance. The internal subtype
15189 -- is private in the instance but has full visibility like its
15190 -- parent in the enclosing scope. This enforces the invariant that
15191 -- the privacy status of all private dependents of a type coincide
15192 -- with that of the parent type. This can only happen when a
15193 -- generic child unit is instantiated within a sibling.
15194
15195 if Is_Private_Type (E)
15196 and then not Is_Private_Type (Etype (E))
15197 then
15198 Exchange_Declarations (E);
15199 end if;
15200
15201 elsif Ekind (E) = E_Package then
15202
15203 -- The end of the renaming list is the renaming of the generic
15204 -- package itself. If the instance is a subprogram, all entities
15205 -- in the corresponding package are renamings. If this entity is
15206 -- a formal package, make its own formals private as well. The
15207 -- actual in this case is itself the renaming of an instantiation.
15208 -- If the entity is not a package renaming, it is the entity
15209 -- created to validate formal package actuals: ignore it.
15210
15211 -- If the actual is itself a formal package for the enclosing
15212 -- generic, or the actual for such a formal package, it remains
15213 -- visible on exit from the instance, and therefore nothing needs
15214 -- to be done either, except to keep it accessible.
15215
15216 if Is_Package and then Renamed_Object (E) = Pack_Id then
15217 exit;
15218
15219 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
15220 null;
15221
15222 elsif
15223 Denotes_Formal_Package (Renamed_Object (E), True, Pack_Id)
15224 then
15225 Set_Is_Hidden (E, False);
15226
15227 else
15228 declare
15229 Act_P : constant Entity_Id := Renamed_Object (E);
15230 Id : Entity_Id;
15231
15232 begin
15233 Id := First_Entity (Act_P);
15234 while Present (Id)
15235 and then Id /= First_Private_Entity (Act_P)
15236 loop
15237 exit when Ekind (Id) = E_Package
15238 and then Renamed_Object (Id) = Act_P;
15239
15240 Set_Is_Hidden (Id, True);
15241 Set_Is_Potentially_Use_Visible (Id, In_Use (Act_P));
15242
15243 if Ekind (Id) = E_Package then
15244 Restore_Nested_Formal (Id);
15245 end if;
15246
15247 Next_Entity (Id);
15248 end loop;
15249 end;
15250 end if;
15251 end if;
15252
15253 Next_Entity (E);
15254 end loop;
15255 end Restore_Private_Views;
15256
15257 --------------
15258 -- Save_Env --
15259 --------------
15260
15261 procedure Save_Env
15262 (Gen_Unit : Entity_Id;
15263 Act_Unit : Entity_Id)
15264 is
15265 begin
15266 Init_Env;
15267 Set_Instance_Env (Gen_Unit, Act_Unit);
15268 end Save_Env;
15269
15270 ----------------------------
15271 -- Save_Global_References --
15272 ----------------------------
15273
15274 procedure Save_Global_References (Templ : Node_Id) is
15275
15276 -- ??? it is horrible to use global variables in highly recursive code
15277
15278 E : Entity_Id;
15279 -- The entity of the current associated node
15280
15281 Gen_Scope : Entity_Id;
15282 -- The scope of the generic for which references are being saved
15283
15284 N2 : Node_Id;
15285 -- The current associated node
15286
15287 function Is_Global (E : Entity_Id) return Boolean;
15288 -- Check whether entity is defined outside of generic unit. Examine the
15289 -- scope of an entity, and the scope of the scope, etc, until we find
15290 -- either Standard, in which case the entity is global, or the generic
15291 -- unit itself, which indicates that the entity is local. If the entity
15292 -- is the generic unit itself, as in the case of a recursive call, or
15293 -- the enclosing generic unit, if different from the current scope, then
15294 -- it is local as well, because it will be replaced at the point of
15295 -- instantiation. On the other hand, if it is a reference to a child
15296 -- unit of a common ancestor, which appears in an instantiation, it is
15297 -- global because it is used to denote a specific compilation unit at
15298 -- the time the instantiations will be analyzed.
15299
15300 procedure Qualify_Universal_Operands
15301 (Op : Node_Id;
15302 Func_Call : Node_Id);
15303 -- Op denotes a binary or unary operator in generic template Templ. Node
15304 -- Func_Call is the function call alternative of the operator within the
15305 -- the analyzed copy of the template. Change each operand which yields a
15306 -- universal type by wrapping it into a qualified expression
15307 --
15308 -- Actual_Typ'(Operand)
15309 --
15310 -- where Actual_Typ is the type of corresponding actual parameter of
15311 -- Operand in Func_Call.
15312
15313 procedure Reset_Entity (N : Node_Id);
15314 -- Save semantic information on global entity so that it is not resolved
15315 -- again at instantiation time.
15316
15317 procedure Save_Entity_Descendants (N : Node_Id);
15318 -- Apply Save_Global_References to the two syntactic descendants of
15319 -- non-terminal nodes that carry an Associated_Node and are processed
15320 -- through Reset_Entity. Once the global entity (if any) has been
15321 -- captured together with its type, only two syntactic descendants need
15322 -- to be traversed to complete the processing of the tree rooted at N.
15323 -- This applies to Selected_Components, Expanded_Names, and to Operator
15324 -- nodes. N can also be a character literal, identifier, or operator
15325 -- symbol node, but the call has no effect in these cases.
15326
15327 procedure Save_Global_Defaults (N1 : Node_Id; N2 : Node_Id);
15328 -- Default actuals in nested instances must be handled specially
15329 -- because there is no link to them from the original tree. When an
15330 -- actual subprogram is given by a default, we add an explicit generic
15331 -- association for it in the instantiation node. When we save the
15332 -- global references on the name of the instance, we recover the list
15333 -- of generic associations, and add an explicit one to the original
15334 -- generic tree, through which a global actual can be preserved.
15335 -- Similarly, if a child unit is instantiated within a sibling, in the
15336 -- context of the parent, we must preserve the identifier of the parent
15337 -- so that it can be properly resolved in a subsequent instantiation.
15338
15339 procedure Save_Global_Descendant (D : Union_Id);
15340 -- Apply Save_References recursively to the descendants of node D
15341
15342 procedure Save_References (N : Node_Id);
15343 -- This is the recursive procedure that does the work, once the
15344 -- enclosing generic scope has been established.
15345
15346 ---------------
15347 -- Is_Global --
15348 ---------------
15349
15350 function Is_Global (E : Entity_Id) return Boolean is
15351 Se : Entity_Id;
15352
15353 function Is_Instance_Node (Decl : Node_Id) return Boolean;
15354 -- Determine whether the parent node of a reference to a child unit
15355 -- denotes an instantiation or a formal package, in which case the
15356 -- reference to the child unit is global, even if it appears within
15357 -- the current scope (e.g. when the instance appears within the body
15358 -- of an ancestor).
15359
15360 ----------------------
15361 -- Is_Instance_Node --
15362 ----------------------
15363
15364 function Is_Instance_Node (Decl : Node_Id) return Boolean is
15365 begin
15366 return Nkind (Decl) in N_Generic_Instantiation
15367 or else
15368 Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration;
15369 end Is_Instance_Node;
15370
15371 -- Start of processing for Is_Global
15372
15373 begin
15374 if E = Gen_Scope then
15375 return False;
15376
15377 elsif E = Standard_Standard then
15378 return True;
15379
15380 elsif Is_Child_Unit (E)
15381 and then (Is_Instance_Node (Parent (N2))
15382 or else (Nkind (Parent (N2)) = N_Expanded_Name
15383 and then N2 = Selector_Name (Parent (N2))
15384 and then
15385 Is_Instance_Node (Parent (Parent (N2)))))
15386 then
15387 return True;
15388
15389 else
15390 Se := Scope (E);
15391 while Se /= Gen_Scope loop
15392 if Se = Standard_Standard then
15393 return True;
15394 else
15395 Se := Scope (Se);
15396 end if;
15397 end loop;
15398
15399 return False;
15400 end if;
15401 end Is_Global;
15402
15403 --------------------------------
15404 -- Qualify_Universal_Operands --
15405 --------------------------------
15406
15407 procedure Qualify_Universal_Operands
15408 (Op : Node_Id;
15409 Func_Call : Node_Id)
15410 is
15411 procedure Qualify_Operand (Opnd : Node_Id; Actual : Node_Id);
15412 -- Rewrite operand Opnd as a qualified expression of the form
15413 --
15414 -- Actual_Typ'(Opnd)
15415 --
15416 -- where Actual is the corresponding actual parameter of Opnd in
15417 -- function call Func_Call.
15418
15419 function Qualify_Type
15420 (Loc : Source_Ptr;
15421 Typ : Entity_Id) return Node_Id;
15422 -- Qualify type Typ by creating a selected component of the form
15423 --
15424 -- Scope_Of_Typ.Typ
15425
15426 ---------------------
15427 -- Qualify_Operand --
15428 ---------------------
15429
15430 procedure Qualify_Operand (Opnd : Node_Id; Actual : Node_Id) is
15431 Loc : constant Source_Ptr := Sloc (Opnd);
15432 Typ : constant Entity_Id := Etype (Actual);
15433 Mark : Node_Id;
15434 Qual : Node_Id;
15435
15436 begin
15437 -- Qualify the operand when it is of a universal type. Note that
15438 -- the template is unanalyzed and it is not possible to directly
15439 -- query the type. This transformation is not done when the type
15440 -- of the actual is internally generated because the type will be
15441 -- regenerated in the instance.
15442
15443 if Yields_Universal_Type (Opnd)
15444 and then Comes_From_Source (Typ)
15445 and then not Is_Hidden (Typ)
15446 then
15447 -- The type of the actual may be a global reference. Save this
15448 -- information by creating a reference to it.
15449
15450 if Is_Global (Typ) then
15451 Mark := New_Occurrence_Of (Typ, Loc);
15452
15453 -- Otherwise rely on resolution to find the proper type within
15454 -- the instance.
15455
15456 else
15457 Mark := Qualify_Type (Loc, Typ);
15458 end if;
15459
15460 Qual :=
15461 Make_Qualified_Expression (Loc,
15462 Subtype_Mark => Mark,
15463 Expression => Relocate_Node (Opnd));
15464
15465 -- Mark the qualification to distinguish it from other source
15466 -- constructs and signal the instantiation mechanism that this
15467 -- node requires special processing. See Copy_Generic_Node for
15468 -- details.
15469
15470 Set_Is_Qualified_Universal_Literal (Qual);
15471
15472 Rewrite (Opnd, Qual);
15473 end if;
15474 end Qualify_Operand;
15475
15476 ------------------
15477 -- Qualify_Type --
15478 ------------------
15479
15480 function Qualify_Type
15481 (Loc : Source_Ptr;
15482 Typ : Entity_Id) return Node_Id
15483 is
15484 Scop : constant Entity_Id := Scope (Typ);
15485 Result : Node_Id;
15486
15487 begin
15488 Result := Make_Identifier (Loc, Chars (Typ));
15489
15490 if Present (Scop) and then not Is_Generic_Unit (Scop) then
15491 Result :=
15492 Make_Selected_Component (Loc,
15493 Prefix => Make_Identifier (Loc, Chars (Scop)),
15494 Selector_Name => Result);
15495 end if;
15496
15497 return Result;
15498 end Qualify_Type;
15499
15500 -- Local variables
15501
15502 Actuals : constant List_Id := Parameter_Associations (Func_Call);
15503
15504 -- Start of processing for Qualify_Universal_Operands
15505
15506 begin
15507 if Nkind (Op) in N_Binary_Op then
15508 Qualify_Operand (Left_Opnd (Op), First (Actuals));
15509 Qualify_Operand (Right_Opnd (Op), Next (First (Actuals)));
15510
15511 elsif Nkind (Op) in N_Unary_Op then
15512 Qualify_Operand (Right_Opnd (Op), First (Actuals));
15513 end if;
15514 end Qualify_Universal_Operands;
15515
15516 ------------------
15517 -- Reset_Entity --
15518 ------------------
15519
15520 procedure Reset_Entity (N : Node_Id) is
15521 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id);
15522 -- If the type of N2 is global to the generic unit, save the type in
15523 -- the generic node. Just as we perform name capture for explicit
15524 -- references within the generic, we must capture the global types
15525 -- of local entities because they may participate in resolution in
15526 -- the instance.
15527
15528 function Top_Ancestor (E : Entity_Id) return Entity_Id;
15529 -- Find the ultimate ancestor of the current unit. If it is not a
15530 -- generic unit, then the name of the current unit in the prefix of
15531 -- an expanded name must be replaced with its generic homonym to
15532 -- ensure that it will be properly resolved in an instance.
15533
15534 ---------------------
15535 -- Set_Global_Type --
15536 ---------------------
15537
15538 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id) is
15539 Typ : constant Entity_Id := Etype (N2);
15540
15541 begin
15542 Set_Etype (N, Typ);
15543
15544 -- If the entity of N is not the associated node, this is a
15545 -- nested generic and it has an associated node as well, whose
15546 -- type is already the full view (see below). Indicate that the
15547 -- original node has a private view.
15548
15549 if Entity (N) /= N2 and then Has_Private_View (Entity (N)) then
15550 Set_Has_Private_View (N);
15551 end if;
15552
15553 -- If not a private type, nothing else to do
15554
15555 if not Is_Private_Type (Typ) then
15556 null;
15557
15558 -- If it is a derivation of a private type in a context where no
15559 -- full view is needed, nothing to do either.
15560
15561 elsif No (Full_View (Typ)) and then Typ /= Etype (Typ) then
15562 null;
15563
15564 -- Otherwise mark the type for flipping and use the full view when
15565 -- available.
15566
15567 else
15568 Set_Has_Private_View (N);
15569
15570 if Present (Full_View (Typ)) then
15571 Set_Etype (N2, Full_View (Typ));
15572 end if;
15573 end if;
15574
15575 if Is_Floating_Point_Type (Typ)
15576 and then Has_Dimension_System (Typ)
15577 then
15578 Copy_Dimensions (N2, N);
15579 end if;
15580 end Set_Global_Type;
15581
15582 ------------------
15583 -- Top_Ancestor --
15584 ------------------
15585
15586 function Top_Ancestor (E : Entity_Id) return Entity_Id is
15587 Par : Entity_Id;
15588
15589 begin
15590 Par := E;
15591 while Is_Child_Unit (Par) loop
15592 Par := Scope (Par);
15593 end loop;
15594
15595 return Par;
15596 end Top_Ancestor;
15597
15598 -- Start of processing for Reset_Entity
15599
15600 begin
15601 N2 := Get_Associated_Node (N);
15602 E := Entity (N2);
15603
15604 if Present (E) then
15605
15606 -- If the node is an entry call to an entry in an enclosing task,
15607 -- it is rewritten as a selected component. No global entity to
15608 -- preserve in this case, since the expansion will be redone in
15609 -- the instance.
15610
15611 if not Nkind_In (E, N_Defining_Character_Literal,
15612 N_Defining_Identifier,
15613 N_Defining_Operator_Symbol)
15614 then
15615 Set_Associated_Node (N, Empty);
15616 Set_Etype (N, Empty);
15617 return;
15618 end if;
15619
15620 -- If the entity is an itype created as a subtype of an access
15621 -- type with a null exclusion restore source entity for proper
15622 -- visibility. The itype will be created anew in the instance.
15623
15624 if Is_Itype (E)
15625 and then Ekind (E) = E_Access_Subtype
15626 and then Is_Entity_Name (N)
15627 and then Chars (Etype (E)) = Chars (N)
15628 then
15629 E := Etype (E);
15630 Set_Entity (N2, E);
15631 Set_Etype (N2, E);
15632 end if;
15633
15634 if Is_Global (E) then
15635 Set_Global_Type (N, N2);
15636
15637 elsif Nkind (N) = N_Op_Concat
15638 and then Is_Generic_Type (Etype (N2))
15639 and then (Base_Type (Etype (Right_Opnd (N2))) = Etype (N2)
15640 or else
15641 Base_Type (Etype (Left_Opnd (N2))) = Etype (N2))
15642 and then Is_Intrinsic_Subprogram (E)
15643 then
15644 null;
15645
15646 -- Entity is local. Mark generic node as unresolved. Note that now
15647 -- it does not have an entity.
15648
15649 else
15650 Set_Associated_Node (N, Empty);
15651 Set_Etype (N, Empty);
15652 end if;
15653
15654 if Nkind (Parent (N)) in N_Generic_Instantiation
15655 and then N = Name (Parent (N))
15656 then
15657 Save_Global_Defaults (Parent (N), Parent (N2));
15658 end if;
15659
15660 elsif Nkind (Parent (N)) = N_Selected_Component
15661 and then Nkind (Parent (N2)) = N_Expanded_Name
15662 then
15663 if Is_Global (Entity (Parent (N2))) then
15664 Change_Selected_Component_To_Expanded_Name (Parent (N));
15665 Set_Associated_Node (Parent (N), Parent (N2));
15666 Set_Global_Type (Parent (N), Parent (N2));
15667 Save_Entity_Descendants (N);
15668
15669 -- If this is a reference to the current generic entity, replace
15670 -- by the name of the generic homonym of the current package. This
15671 -- is because in an instantiation Par.P.Q will not resolve to the
15672 -- name of the instance, whose enclosing scope is not necessarily
15673 -- Par. We use the generic homonym rather that the name of the
15674 -- generic itself because it may be hidden by a local declaration.
15675
15676 elsif In_Open_Scopes (Entity (Parent (N2)))
15677 and then not
15678 Is_Generic_Unit (Top_Ancestor (Entity (Prefix (Parent (N2)))))
15679 then
15680 if Ekind (Entity (Parent (N2))) = E_Generic_Package then
15681 Rewrite (Parent (N),
15682 Make_Identifier (Sloc (N),
15683 Chars =>
15684 Chars (Generic_Homonym (Entity (Parent (N2))))));
15685 else
15686 Rewrite (Parent (N),
15687 Make_Identifier (Sloc (N),
15688 Chars => Chars (Selector_Name (Parent (N2)))));
15689 end if;
15690 end if;
15691
15692 if Nkind (Parent (Parent (N))) in N_Generic_Instantiation
15693 and then Parent (N) = Name (Parent (Parent (N)))
15694 then
15695 Save_Global_Defaults
15696 (Parent (Parent (N)), Parent (Parent (N2)));
15697 end if;
15698
15699 -- A selected component may denote a static constant that has been
15700 -- folded. If the static constant is global to the generic, capture
15701 -- its value. Otherwise the folding will happen in any instantiation.
15702
15703 elsif Nkind (Parent (N)) = N_Selected_Component
15704 and then Nkind_In (Parent (N2), N_Integer_Literal, N_Real_Literal)
15705 then
15706 if Present (Entity (Original_Node (Parent (N2))))
15707 and then Is_Global (Entity (Original_Node (Parent (N2))))
15708 then
15709 Rewrite (Parent (N), New_Copy (Parent (N2)));
15710 Set_Analyzed (Parent (N), False);
15711 end if;
15712
15713 -- A selected component may be transformed into a parameterless
15714 -- function call. If the called entity is global, rewrite the node
15715 -- appropriately, i.e. as an extended name for the global entity.
15716
15717 elsif Nkind (Parent (N)) = N_Selected_Component
15718 and then Nkind (Parent (N2)) = N_Function_Call
15719 and then N = Selector_Name (Parent (N))
15720 then
15721 if No (Parameter_Associations (Parent (N2))) then
15722 if Is_Global (Entity (Name (Parent (N2)))) then
15723 Change_Selected_Component_To_Expanded_Name (Parent (N));
15724 Set_Associated_Node (Parent (N), Name (Parent (N2)));
15725 Set_Global_Type (Parent (N), Name (Parent (N2)));
15726 Save_Entity_Descendants (N);
15727
15728 else
15729 Set_Is_Prefixed_Call (Parent (N));
15730 Set_Associated_Node (N, Empty);
15731 Set_Etype (N, Empty);
15732 end if;
15733
15734 -- In Ada 2005, X.F may be a call to a primitive operation,
15735 -- rewritten as F (X). This rewriting will be done again in an
15736 -- instance, so keep the original node. Global entities will be
15737 -- captured as for other constructs. Indicate that this must
15738 -- resolve as a call, to prevent accidental overloading in the
15739 -- instance, if both a component and a primitive operation appear
15740 -- as candidates.
15741
15742 else
15743 Set_Is_Prefixed_Call (Parent (N));
15744 end if;
15745
15746 -- Entity is local. Reset in generic unit, so that node is resolved
15747 -- anew at the point of instantiation.
15748
15749 else
15750 Set_Associated_Node (N, Empty);
15751 Set_Etype (N, Empty);
15752 end if;
15753 end Reset_Entity;
15754
15755 -----------------------------
15756 -- Save_Entity_Descendants --
15757 -----------------------------
15758
15759 procedure Save_Entity_Descendants (N : Node_Id) is
15760 begin
15761 case Nkind (N) is
15762 when N_Binary_Op =>
15763 Save_Global_Descendant (Union_Id (Left_Opnd (N)));
15764 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
15765
15766 when N_Unary_Op =>
15767 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
15768
15769 when N_Expanded_Name
15770 | N_Selected_Component
15771 =>
15772 Save_Global_Descendant (Union_Id (Prefix (N)));
15773 Save_Global_Descendant (Union_Id (Selector_Name (N)));
15774
15775 when N_Character_Literal
15776 | N_Identifier
15777 | N_Operator_Symbol
15778 =>
15779 null;
15780
15781 when others =>
15782 raise Program_Error;
15783 end case;
15784 end Save_Entity_Descendants;
15785
15786 --------------------------
15787 -- Save_Global_Defaults --
15788 --------------------------
15789
15790 procedure Save_Global_Defaults (N1 : Node_Id; N2 : Node_Id) is
15791 Loc : constant Source_Ptr := Sloc (N1);
15792 Assoc2 : constant List_Id := Generic_Associations (N2);
15793 Gen_Id : constant Entity_Id := Get_Generic_Entity (N2);
15794 Assoc1 : List_Id;
15795 Act1 : Node_Id;
15796 Act2 : Node_Id;
15797 Def : Node_Id;
15798 Ndec : Node_Id;
15799 Subp : Entity_Id;
15800 Actual : Entity_Id;
15801
15802 begin
15803 Assoc1 := Generic_Associations (N1);
15804
15805 if Present (Assoc1) then
15806 Act1 := First (Assoc1);
15807 else
15808 Act1 := Empty;
15809 Set_Generic_Associations (N1, New_List);
15810 Assoc1 := Generic_Associations (N1);
15811 end if;
15812
15813 if Present (Assoc2) then
15814 Act2 := First (Assoc2);
15815 else
15816 return;
15817 end if;
15818
15819 while Present (Act1) and then Present (Act2) loop
15820 Next (Act1);
15821 Next (Act2);
15822 end loop;
15823
15824 -- Find the associations added for default subprograms
15825
15826 if Present (Act2) then
15827 while Nkind (Act2) /= N_Generic_Association
15828 or else No (Entity (Selector_Name (Act2)))
15829 or else not Is_Overloadable (Entity (Selector_Name (Act2)))
15830 loop
15831 Next (Act2);
15832 end loop;
15833
15834 -- Add a similar association if the default is global. The
15835 -- renaming declaration for the actual has been analyzed, and
15836 -- its alias is the program it renames. Link the actual in the
15837 -- original generic tree with the node in the analyzed tree.
15838
15839 while Present (Act2) loop
15840 Subp := Entity (Selector_Name (Act2));
15841 Def := Explicit_Generic_Actual_Parameter (Act2);
15842
15843 -- Following test is defence against rubbish errors
15844
15845 if No (Alias (Subp)) then
15846 return;
15847 end if;
15848
15849 -- Retrieve the resolved actual from the renaming declaration
15850 -- created for the instantiated formal.
15851
15852 Actual := Entity (Name (Parent (Parent (Subp))));
15853 Set_Entity (Def, Actual);
15854 Set_Etype (Def, Etype (Actual));
15855
15856 if Is_Global (Actual) then
15857 Ndec :=
15858 Make_Generic_Association (Loc,
15859 Selector_Name =>
15860 New_Occurrence_Of (Subp, Loc),
15861 Explicit_Generic_Actual_Parameter =>
15862 New_Occurrence_Of (Actual, Loc));
15863
15864 Set_Associated_Node
15865 (Explicit_Generic_Actual_Parameter (Ndec), Def);
15866
15867 Append (Ndec, Assoc1);
15868
15869 -- If there are other defaults, add a dummy association in case
15870 -- there are other defaulted formals with the same name.
15871
15872 elsif Present (Next (Act2)) then
15873 Ndec :=
15874 Make_Generic_Association (Loc,
15875 Selector_Name =>
15876 New_Occurrence_Of (Subp, Loc),
15877 Explicit_Generic_Actual_Parameter => Empty);
15878
15879 Append (Ndec, Assoc1);
15880 end if;
15881
15882 Next (Act2);
15883 end loop;
15884 end if;
15885
15886 if Nkind (Name (N1)) = N_Identifier
15887 and then Is_Child_Unit (Gen_Id)
15888 and then Is_Global (Gen_Id)
15889 and then Is_Generic_Unit (Scope (Gen_Id))
15890 and then In_Open_Scopes (Scope (Gen_Id))
15891 then
15892 -- This is an instantiation of a child unit within a sibling, so
15893 -- that the generic parent is in scope. An eventual instance must
15894 -- occur within the scope of an instance of the parent. Make name
15895 -- in instance into an expanded name, to preserve the identifier
15896 -- of the parent, so it can be resolved subsequently.
15897
15898 Rewrite (Name (N2),
15899 Make_Expanded_Name (Loc,
15900 Chars => Chars (Gen_Id),
15901 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
15902 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
15903 Set_Entity (Name (N2), Gen_Id);
15904
15905 Rewrite (Name (N1),
15906 Make_Expanded_Name (Loc,
15907 Chars => Chars (Gen_Id),
15908 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
15909 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
15910
15911 Set_Associated_Node (Name (N1), Name (N2));
15912 Set_Associated_Node (Prefix (Name (N1)), Empty);
15913 Set_Associated_Node
15914 (Selector_Name (Name (N1)), Selector_Name (Name (N2)));
15915 Set_Etype (Name (N1), Etype (Gen_Id));
15916 end if;
15917 end Save_Global_Defaults;
15918
15919 ----------------------------
15920 -- Save_Global_Descendant --
15921 ----------------------------
15922
15923 procedure Save_Global_Descendant (D : Union_Id) is
15924 N1 : Node_Id;
15925
15926 begin
15927 if D in Node_Range then
15928 if D = Union_Id (Empty) then
15929 null;
15930
15931 elsif Nkind (Node_Id (D)) /= N_Compilation_Unit then
15932 Save_References (Node_Id (D));
15933 end if;
15934
15935 elsif D in List_Range then
15936 pragma Assert (D /= Union_Id (No_List));
15937 -- Because No_List = Empty, which is in Node_Range above
15938
15939 if Is_Empty_List (List_Id (D)) then
15940 null;
15941
15942 else
15943 N1 := First (List_Id (D));
15944 while Present (N1) loop
15945 Save_References (N1);
15946 Next (N1);
15947 end loop;
15948 end if;
15949
15950 -- Element list or other non-node field, nothing to do
15951
15952 else
15953 null;
15954 end if;
15955 end Save_Global_Descendant;
15956
15957 ---------------------
15958 -- Save_References --
15959 ---------------------
15960
15961 -- This is the recursive procedure that does the work once the enclosing
15962 -- generic scope has been established. We have to treat specially a
15963 -- number of node rewritings that are required by semantic processing
15964 -- and which change the kind of nodes in the generic copy: typically
15965 -- constant-folding, replacing an operator node by a string literal, or
15966 -- a selected component by an expanded name. In each of those cases, the
15967 -- transformation is propagated to the generic unit.
15968
15969 procedure Save_References (N : Node_Id) is
15970 Loc : constant Source_Ptr := Sloc (N);
15971
15972 function Requires_Delayed_Save (Nod : Node_Id) return Boolean;
15973 -- Determine whether arbitrary node Nod requires delayed capture of
15974 -- global references within its aspect specifications.
15975
15976 procedure Save_References_In_Aggregate (N : Node_Id);
15977 -- Save all global references in [extension] aggregate node N
15978
15979 procedure Save_References_In_Char_Lit_Or_Op_Symbol (N : Node_Id);
15980 -- Save all global references in a character literal or operator
15981 -- symbol denoted by N.
15982
15983 procedure Save_References_In_Descendants (N : Node_Id);
15984 -- Save all global references in all descendants of node N
15985
15986 procedure Save_References_In_Identifier (N : Node_Id);
15987 -- Save all global references in identifier node N
15988
15989 procedure Save_References_In_Operator (N : Node_Id);
15990 -- Save all global references in operator node N
15991
15992 procedure Save_References_In_Pragma (Prag : Node_Id);
15993 -- Save all global references found within the expression of pragma
15994 -- Prag.
15995
15996 ---------------------------
15997 -- Requires_Delayed_Save --
15998 ---------------------------
15999
16000 function Requires_Delayed_Save (Nod : Node_Id) return Boolean is
16001 begin
16002 -- Generic packages and subprograms require delayed capture of
16003 -- global references within their aspects due to the timing of
16004 -- annotation analysis.
16005
16006 if Nkind_In (Nod, N_Generic_Package_Declaration,
16007 N_Generic_Subprogram_Declaration,
16008 N_Package_Body,
16009 N_Package_Body_Stub,
16010 N_Subprogram_Body,
16011 N_Subprogram_Body_Stub)
16012 then
16013 -- Since the capture of global references is done on the
16014 -- unanalyzed generic template, there is no information around
16015 -- to infer the context. Use the Associated_Entity linkages to
16016 -- peek into the analyzed generic copy and determine what the
16017 -- template corresponds to.
16018
16019 if Nod = Templ then
16020 return
16021 Is_Generic_Declaration_Or_Body
16022 (Unit_Declaration_Node
16023 (Associated_Entity (Defining_Entity (Nod))));
16024
16025 -- Otherwise the generic unit being processed is not the top
16026 -- level template. It is safe to capture of global references
16027 -- within the generic unit because at this point the top level
16028 -- copy is fully analyzed.
16029
16030 else
16031 return False;
16032 end if;
16033
16034 -- Otherwise capture the global references without interference
16035
16036 else
16037 return False;
16038 end if;
16039 end Requires_Delayed_Save;
16040
16041 ----------------------------------
16042 -- Save_References_In_Aggregate --
16043 ----------------------------------
16044
16045 procedure Save_References_In_Aggregate (N : Node_Id) is
16046 Nam : Node_Id;
16047 Qual : Node_Id := Empty;
16048 Typ : Entity_Id := Empty;
16049
16050 use Atree.Unchecked_Access;
16051 -- This code section is part of implementing an untyped tree
16052 -- traversal, so it needs direct access to node fields.
16053
16054 begin
16055 N2 := Get_Associated_Node (N);
16056
16057 if Present (N2) then
16058 Typ := Etype (N2);
16059
16060 -- In an instance within a generic, use the name of the actual
16061 -- and not the original generic parameter. If the actual is
16062 -- global in the current generic it must be preserved for its
16063 -- instantiation.
16064
16065 if Nkind (Parent (Typ)) = N_Subtype_Declaration
16066 and then Present (Generic_Parent_Type (Parent (Typ)))
16067 then
16068 Typ := Base_Type (Typ);
16069 Set_Etype (N2, Typ);
16070 end if;
16071 end if;
16072
16073 if No (N2) or else No (Typ) or else not Is_Global (Typ) then
16074 Set_Associated_Node (N, Empty);
16075
16076 -- If the aggregate is an actual in a call, it has been
16077 -- resolved in the current context, to some local type. The
16078 -- enclosing call may have been disambiguated by the aggregate,
16079 -- and this disambiguation might fail at instantiation time
16080 -- because the type to which the aggregate did resolve is not
16081 -- preserved. In order to preserve some of this information,
16082 -- wrap the aggregate in a qualified expression, using the id
16083 -- of its type. For further disambiguation we qualify the type
16084 -- name with its scope (if visible and not hidden by a local
16085 -- homograph) because both id's will have corresponding
16086 -- entities in an instance. This resolves most of the problems
16087 -- with missing type information on aggregates in instances.
16088
16089 if Present (N2)
16090 and then Nkind (N2) = Nkind (N)
16091 and then Nkind (Parent (N2)) in N_Subprogram_Call
16092 and then Present (Typ)
16093 and then Comes_From_Source (Typ)
16094 then
16095 Nam := Make_Identifier (Loc, Chars (Typ));
16096
16097 if Is_Immediately_Visible (Scope (Typ))
16098 and then
16099 (not In_Open_Scopes (Scope (Typ))
16100 or else Current_Entity (Scope (Typ)) = Scope (Typ))
16101 then
16102 Nam :=
16103 Make_Selected_Component (Loc,
16104 Prefix =>
16105 Make_Identifier (Loc, Chars (Scope (Typ))),
16106 Selector_Name => Nam);
16107 end if;
16108
16109 Qual :=
16110 Make_Qualified_Expression (Loc,
16111 Subtype_Mark => Nam,
16112 Expression => Relocate_Node (N));
16113 end if;
16114 end if;
16115
16116 Save_Global_Descendant (Field1 (N));
16117 Save_Global_Descendant (Field2 (N));
16118 Save_Global_Descendant (Field3 (N));
16119 Save_Global_Descendant (Field5 (N));
16120
16121 if Present (Qual) then
16122 Rewrite (N, Qual);
16123 end if;
16124 end Save_References_In_Aggregate;
16125
16126 ----------------------------------------------
16127 -- Save_References_In_Char_Lit_Or_Op_Symbol --
16128 ----------------------------------------------
16129
16130 procedure Save_References_In_Char_Lit_Or_Op_Symbol (N : Node_Id) is
16131 begin
16132 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
16133 Reset_Entity (N);
16134
16135 elsif Nkind (N) = N_Operator_Symbol
16136 and then Nkind (Get_Associated_Node (N)) = N_String_Literal
16137 then
16138 Change_Operator_Symbol_To_String_Literal (N);
16139 end if;
16140 end Save_References_In_Char_Lit_Or_Op_Symbol;
16141
16142 ------------------------------------
16143 -- Save_References_In_Descendants --
16144 ------------------------------------
16145
16146 procedure Save_References_In_Descendants (N : Node_Id) is
16147 use Atree.Unchecked_Access;
16148 -- This code section is part of implementing an untyped tree
16149 -- traversal, so it needs direct access to node fields.
16150
16151 begin
16152 Save_Global_Descendant (Field1 (N));
16153 Save_Global_Descendant (Field2 (N));
16154 Save_Global_Descendant (Field3 (N));
16155 Save_Global_Descendant (Field4 (N));
16156 Save_Global_Descendant (Field5 (N));
16157 end Save_References_In_Descendants;
16158
16159 -----------------------------------
16160 -- Save_References_In_Identifier --
16161 -----------------------------------
16162
16163 procedure Save_References_In_Identifier (N : Node_Id) is
16164 begin
16165 -- The node did not undergo a transformation
16166
16167 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
16168 -- If this is a discriminant reference, always save it.
16169 -- It is used in the instance to find the corresponding
16170 -- discriminant positionally rather than by name.
16171
16172 Set_Original_Discriminant
16173 (N, Original_Discriminant (Get_Associated_Node (N)));
16174
16175 Reset_Entity (N);
16176
16177 -- The analysis of the generic copy transformed the identifier
16178 -- into another construct. Propagate the changes to the template.
16179
16180 else
16181 N2 := Get_Associated_Node (N);
16182
16183 -- The identifier denotes a call to a parameterless function.
16184 -- Mark the node as resolved when the function is external.
16185
16186 if Nkind (N2) = N_Function_Call then
16187 E := Entity (Name (N2));
16188
16189 if Present (E) and then Is_Global (E) then
16190 Set_Etype (N, Etype (N2));
16191 else
16192 Set_Associated_Node (N, Empty);
16193 Set_Etype (N, Empty);
16194 end if;
16195
16196 -- The identifier denotes a named number that was constant
16197 -- folded. Preserve the original name for ASIS and undo the
16198 -- constant folding which will be repeated in the instance.
16199 -- Is this still needed???
16200
16201 elsif Nkind_In (N2, N_Integer_Literal, N_Real_Literal)
16202 and then Is_Entity_Name (Original_Node (N2))
16203 then
16204 Set_Associated_Node (N, Original_Node (N2));
16205 Reset_Entity (N);
16206
16207 -- The identifier resolved to a string literal. Propagate this
16208 -- information to the generic template.
16209
16210 elsif Nkind (N2) = N_String_Literal then
16211 Rewrite (N, New_Copy (N2));
16212
16213 -- The identifier is rewritten as a dereference if it is the
16214 -- prefix of an implicit dereference. Preserve the original
16215 -- tree as the analysis of the instance will expand the node
16216 -- again, but preserve the resolved entity if it is global.
16217
16218 elsif Nkind (N2) = N_Explicit_Dereference then
16219 if Is_Entity_Name (Prefix (N2))
16220 and then Present (Entity (Prefix (N2)))
16221 and then Is_Global (Entity (Prefix (N2)))
16222 then
16223 Set_Associated_Node (N, Prefix (N2));
16224
16225 elsif Nkind (Prefix (N2)) = N_Function_Call
16226 and then Present (Entity (Name (Prefix (N2))))
16227 and then Is_Global (Entity (Name (Prefix (N2))))
16228 then
16229 Rewrite (N,
16230 Make_Explicit_Dereference (Loc,
16231 Prefix =>
16232 Make_Function_Call (Loc,
16233 Name =>
16234 New_Occurrence_Of
16235 (Entity (Name (Prefix (N2))), Loc))));
16236
16237 else
16238 Set_Associated_Node (N, Empty);
16239 Set_Etype (N, Empty);
16240 end if;
16241
16242 -- The subtype mark of a nominally unconstrained object is
16243 -- rewritten as a subtype indication using the bounds of the
16244 -- expression. Recover the original subtype mark.
16245
16246 elsif Nkind (N2) = N_Subtype_Indication
16247 and then Is_Entity_Name (Original_Node (N2))
16248 then
16249 Set_Associated_Node (N, Original_Node (N2));
16250 Reset_Entity (N);
16251 end if;
16252 end if;
16253 end Save_References_In_Identifier;
16254
16255 ---------------------------------
16256 -- Save_References_In_Operator --
16257 ---------------------------------
16258
16259 procedure Save_References_In_Operator (N : Node_Id) is
16260 begin
16261 -- The node did not undergo a transformation
16262
16263 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
16264 if Nkind (N) = N_Op_Concat then
16265 Set_Is_Component_Left_Opnd (N,
16266 Is_Component_Left_Opnd (Get_Associated_Node (N)));
16267
16268 Set_Is_Component_Right_Opnd (N,
16269 Is_Component_Right_Opnd (Get_Associated_Node (N)));
16270 end if;
16271
16272 Reset_Entity (N);
16273
16274 -- The analysis of the generic copy transformed the operator into
16275 -- some other construct. Propagate the changes to the template if
16276 -- applicable.
16277
16278 else
16279 N2 := Get_Associated_Node (N);
16280
16281 -- The operator resoved to a function call
16282
16283 if Nkind (N2) = N_Function_Call then
16284
16285 -- Add explicit qualifications in the generic template for
16286 -- all operands of universal type. This aids resolution by
16287 -- preserving the actual type of a literal or an attribute
16288 -- that yields a universal result.
16289
16290 Qualify_Universal_Operands (N, N2);
16291
16292 E := Entity (Name (N2));
16293
16294 if Present (E) and then Is_Global (E) then
16295 Set_Etype (N, Etype (N2));
16296 else
16297 Set_Associated_Node (N, Empty);
16298 Set_Etype (N, Empty);
16299 end if;
16300
16301 -- The operator was folded into a literal
16302
16303 elsif Nkind_In (N2, N_Integer_Literal,
16304 N_Real_Literal,
16305 N_String_Literal)
16306 then
16307 if Present (Original_Node (N2))
16308 and then Nkind (Original_Node (N2)) = Nkind (N)
16309 then
16310 -- Operation was constant-folded. Whenever possible,
16311 -- recover semantic information from unfolded node.
16312 -- This was initially done for ASIS but is apparently
16313 -- needed also for e.g. compiling a-nbnbin.adb.
16314
16315 Set_Associated_Node (N, Original_Node (N2));
16316
16317 if Nkind (N) = N_Op_Concat then
16318 Set_Is_Component_Left_Opnd (N,
16319 Is_Component_Left_Opnd (Get_Associated_Node (N)));
16320 Set_Is_Component_Right_Opnd (N,
16321 Is_Component_Right_Opnd (Get_Associated_Node (N)));
16322 end if;
16323
16324 Reset_Entity (N);
16325
16326 -- Propagate the constant folding back to the template
16327
16328 else
16329 Rewrite (N, New_Copy (N2));
16330 Set_Analyzed (N, False);
16331 end if;
16332
16333 -- The operator was folded into an enumeration literal. Retain
16334 -- the entity to avoid spurious ambiguities if it is overloaded
16335 -- at the point of instantiation or inlining.
16336
16337 elsif Nkind (N2) = N_Identifier
16338 and then Ekind (Entity (N2)) = E_Enumeration_Literal
16339 then
16340 Rewrite (N, New_Copy (N2));
16341 Set_Analyzed (N, False);
16342 end if;
16343 end if;
16344
16345 -- Complete the operands check if node has not been constant
16346 -- folded.
16347
16348 if Nkind (N) in N_Op then
16349 Save_Entity_Descendants (N);
16350 end if;
16351 end Save_References_In_Operator;
16352
16353 -------------------------------
16354 -- Save_References_In_Pragma --
16355 -------------------------------
16356
16357 procedure Save_References_In_Pragma (Prag : Node_Id) is
16358 Context : Node_Id;
16359 Do_Save : Boolean := True;
16360
16361 use Atree.Unchecked_Access;
16362 -- This code section is part of implementing an untyped tree
16363 -- traversal, so it needs direct access to node fields.
16364
16365 begin
16366 -- Do not save global references in pragmas generated from aspects
16367 -- because the pragmas will be regenerated at instantiation time.
16368
16369 if From_Aspect_Specification (Prag) then
16370 Do_Save := False;
16371
16372 -- The capture of global references within contract-related source
16373 -- pragmas associated with generic packages, subprograms or their
16374 -- respective bodies must be delayed due to timing of annotation
16375 -- analysis. Global references are still captured in routine
16376 -- Save_Global_References_In_Contract.
16377
16378 elsif Is_Generic_Contract_Pragma (Prag) and then Prag /= Templ then
16379 if Is_Package_Contract_Annotation (Prag) then
16380 Context := Find_Related_Package_Or_Body (Prag);
16381 else
16382 pragma Assert (Is_Subprogram_Contract_Annotation (Prag));
16383 Context := Find_Related_Declaration_Or_Body (Prag);
16384 end if;
16385
16386 -- The use of Original_Node accounts for the case when the
16387 -- related context is generic template.
16388
16389 if Requires_Delayed_Save (Original_Node (Context)) then
16390 Do_Save := False;
16391 end if;
16392 end if;
16393
16394 -- For all other cases, save all global references within the
16395 -- descendants, but skip the following semantic fields:
16396
16397 -- Field1 - Next_Pragma
16398 -- Field3 - Corresponding_Aspect
16399 -- Field5 - Next_Rep_Item
16400
16401 if Do_Save then
16402 Save_Global_Descendant (Field2 (Prag));
16403 Save_Global_Descendant (Field4 (Prag));
16404 end if;
16405 end Save_References_In_Pragma;
16406
16407 -- Start of processing for Save_References
16408
16409 begin
16410 if N = Empty then
16411 null;
16412
16413 -- Aggregates
16414
16415 elsif Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
16416 Save_References_In_Aggregate (N);
16417
16418 -- Character literals, operator symbols
16419
16420 elsif Nkind_In (N, N_Character_Literal, N_Operator_Symbol) then
16421 Save_References_In_Char_Lit_Or_Op_Symbol (N);
16422
16423 -- Defining identifiers
16424
16425 elsif Nkind (N) in N_Entity then
16426 null;
16427
16428 -- Identifiers
16429
16430 elsif Nkind (N) = N_Identifier then
16431 Save_References_In_Identifier (N);
16432
16433 -- Operators
16434
16435 elsif Nkind (N) in N_Op then
16436 Save_References_In_Operator (N);
16437
16438 -- Pragmas
16439
16440 elsif Nkind (N) = N_Pragma then
16441 Save_References_In_Pragma (N);
16442
16443 else
16444 Save_References_In_Descendants (N);
16445 end if;
16446
16447 -- Save all global references found within the aspect specifications
16448 -- of the related node.
16449
16450 if Permits_Aspect_Specifications (N) and then Has_Aspects (N) then
16451
16452 -- The capture of global references within aspects associated with
16453 -- generic packages, subprograms or their bodies must be delayed
16454 -- due to timing of annotation analysis. Global references are
16455 -- still captured in routine Save_Global_References_In_Contract.
16456
16457 if Requires_Delayed_Save (N) then
16458 null;
16459
16460 -- Otherwise save all global references within the aspects
16461
16462 else
16463 Save_Global_References_In_Aspects (N);
16464 end if;
16465 end if;
16466 end Save_References;
16467
16468 -- Start of processing for Save_Global_References
16469
16470 begin
16471 Gen_Scope := Current_Scope;
16472
16473 -- If the generic unit is a child unit, references to entities in the
16474 -- parent are treated as local, because they will be resolved anew in
16475 -- the context of the instance of the parent.
16476
16477 while Is_Child_Unit (Gen_Scope)
16478 and then Ekind (Scope (Gen_Scope)) = E_Generic_Package
16479 loop
16480 Gen_Scope := Scope (Gen_Scope);
16481 end loop;
16482
16483 Save_References (Templ);
16484 end Save_Global_References;
16485
16486 ---------------------------------------
16487 -- Save_Global_References_In_Aspects --
16488 ---------------------------------------
16489
16490 procedure Save_Global_References_In_Aspects (N : Node_Id) is
16491 Asp : Node_Id;
16492 Expr : Node_Id;
16493
16494 begin
16495 Asp := First (Aspect_Specifications (N));
16496 while Present (Asp) loop
16497 Expr := Expression (Asp);
16498
16499 if Present (Expr) then
16500 Save_Global_References (Expr);
16501 end if;
16502
16503 Next (Asp);
16504 end loop;
16505 end Save_Global_References_In_Aspects;
16506
16507 ------------------------------------------
16508 -- Set_Copied_Sloc_For_Inherited_Pragma --
16509 ------------------------------------------
16510
16511 procedure Set_Copied_Sloc_For_Inherited_Pragma
16512 (N : Node_Id;
16513 E : Entity_Id)
16514 is
16515 begin
16516 Create_Instantiation_Source (N, E,
16517 Inlined_Body => False,
16518 Inherited_Pragma => True,
16519 Factor => S_Adjustment);
16520 end Set_Copied_Sloc_For_Inherited_Pragma;
16521
16522 --------------------------------------
16523 -- Set_Copied_Sloc_For_Inlined_Body --
16524 --------------------------------------
16525
16526 procedure Set_Copied_Sloc_For_Inlined_Body (N : Node_Id; E : Entity_Id) is
16527 begin
16528 Create_Instantiation_Source (N, E,
16529 Inlined_Body => True,
16530 Inherited_Pragma => False,
16531 Factor => S_Adjustment);
16532 end Set_Copied_Sloc_For_Inlined_Body;
16533
16534 ---------------------
16535 -- Set_Instance_Of --
16536 ---------------------
16537
16538 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id) is
16539 begin
16540 Generic_Renamings.Table (Generic_Renamings.Last) := (A, B, Assoc_Null);
16541 Generic_Renamings_HTable.Set (Generic_Renamings.Last);
16542 Generic_Renamings.Increment_Last;
16543 end Set_Instance_Of;
16544
16545 --------------------
16546 -- Set_Next_Assoc --
16547 --------------------
16548
16549 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr) is
16550 begin
16551 Generic_Renamings.Table (E).Next_In_HTable := Next;
16552 end Set_Next_Assoc;
16553
16554 -------------------
16555 -- Start_Generic --
16556 -------------------
16557
16558 procedure Start_Generic is
16559 begin
16560 -- ??? More things could be factored out in this routine.
16561 -- Should probably be done at a later stage.
16562
16563 Generic_Flags.Append (Inside_A_Generic);
16564 Inside_A_Generic := True;
16565
16566 Expander_Mode_Save_And_Set (False);
16567 end Start_Generic;
16568
16569 ----------------------
16570 -- Set_Instance_Env --
16571 ----------------------
16572
16573 -- WARNING: This routine manages SPARK regions
16574
16575 procedure Set_Instance_Env
16576 (Gen_Unit : Entity_Id;
16577 Act_Unit : Entity_Id)
16578 is
16579 Saved_AE : constant Boolean := Assertions_Enabled;
16580 Saved_CPL : constant Node_Id := Check_Policy_List;
16581 Saved_DEC : constant Boolean := Dynamic_Elaboration_Checks;
16582 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
16583 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
16584
16585 begin
16586 -- Regardless of the current mode, predefined units are analyzed in the
16587 -- most current Ada mode, and earlier version Ada checks do not apply
16588 -- to predefined units. Nothing needs to be done for non-internal units.
16589 -- These are always analyzed in the current mode.
16590
16591 if In_Internal_Unit (Gen_Unit) then
16592
16593 -- The following call resets all configuration attributes to default
16594 -- or the xxx_Config versions of the attributes when the current sem
16595 -- unit is the main unit. At the same time, internal units must also
16596 -- inherit certain configuration attributes from their context. It
16597 -- is unclear what these two sets are.
16598
16599 Set_Config_Switches (True, Current_Sem_Unit = Main_Unit);
16600
16601 -- Reinstall relevant configuration attributes of the context
16602
16603 Assertions_Enabled := Saved_AE;
16604 Check_Policy_List := Saved_CPL;
16605 Dynamic_Elaboration_Checks := Saved_DEC;
16606
16607 Install_SPARK_Mode (Saved_SM, Saved_SMP);
16608 end if;
16609
16610 Current_Instantiated_Parent :=
16611 (Gen_Id => Gen_Unit,
16612 Act_Id => Act_Unit,
16613 Next_In_HTable => Assoc_Null);
16614 end Set_Instance_Env;
16615
16616 -----------------
16617 -- Switch_View --
16618 -----------------
16619
16620 procedure Switch_View (T : Entity_Id) is
16621 BT : constant Entity_Id := Base_Type (T);
16622 Priv_Elmt : Elmt_Id := No_Elmt;
16623 Priv_Sub : Entity_Id;
16624
16625 begin
16626 -- T may be private but its base type may have been exchanged through
16627 -- some other occurrence, in which case there is nothing to switch
16628 -- besides T itself. Note that a private dependent subtype of a private
16629 -- type might not have been switched even if the base type has been,
16630 -- because of the last branch of Check_Private_View (see comment there).
16631
16632 if not Is_Private_Type (BT) then
16633 Prepend_Elmt (Full_View (T), Exchanged_Views);
16634 Exchange_Declarations (T);
16635 return;
16636 end if;
16637
16638 Priv_Elmt := First_Elmt (Private_Dependents (BT));
16639
16640 if Present (Full_View (BT)) then
16641 Prepend_Elmt (Full_View (BT), Exchanged_Views);
16642 Exchange_Declarations (BT);
16643 end if;
16644
16645 while Present (Priv_Elmt) loop
16646 Priv_Sub := (Node (Priv_Elmt));
16647
16648 -- We avoid flipping the subtype if the Etype of its full view is
16649 -- private because this would result in a malformed subtype. This
16650 -- occurs when the Etype of the subtype full view is the full view of
16651 -- the base type (and since the base types were just switched, the
16652 -- subtype is pointing to the wrong view). This is currently the case
16653 -- for tagged record types, access types (maybe more?) and needs to
16654 -- be resolved. ???
16655
16656 if Present (Full_View (Priv_Sub))
16657 and then not Is_Private_Type (Etype (Full_View (Priv_Sub)))
16658 then
16659 Prepend_Elmt (Full_View (Priv_Sub), Exchanged_Views);
16660 Exchange_Declarations (Priv_Sub);
16661 end if;
16662
16663 Next_Elmt (Priv_Elmt);
16664 end loop;
16665 end Switch_View;
16666
16667 -----------------
16668 -- True_Parent --
16669 -----------------
16670
16671 function True_Parent (N : Node_Id) return Node_Id is
16672 begin
16673 if Nkind (Parent (N)) = N_Subunit then
16674 return Parent (Corresponding_Stub (Parent (N)));
16675 else
16676 return Parent (N);
16677 end if;
16678 end True_Parent;
16679
16680 -----------------------------
16681 -- Valid_Default_Attribute --
16682 -----------------------------
16683
16684 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id) is
16685 Attr_Id : constant Attribute_Id :=
16686 Get_Attribute_Id (Attribute_Name (Def));
16687 T : constant Entity_Id := Entity (Prefix (Def));
16688 Is_Fun : constant Boolean := (Ekind (Nam) = E_Function);
16689 F : Entity_Id;
16690 Num_F : Nat;
16691 OK : Boolean;
16692
16693 begin
16694 if No (T) or else T = Any_Id then
16695 return;
16696 end if;
16697
16698 Num_F := 0;
16699 F := First_Formal (Nam);
16700 while Present (F) loop
16701 Num_F := Num_F + 1;
16702 Next_Formal (F);
16703 end loop;
16704
16705 case Attr_Id is
16706 when Attribute_Adjacent
16707 | Attribute_Ceiling
16708 | Attribute_Copy_Sign
16709 | Attribute_Floor
16710 | Attribute_Fraction
16711 | Attribute_Machine
16712 | Attribute_Model
16713 | Attribute_Remainder
16714 | Attribute_Rounding
16715 | Attribute_Unbiased_Rounding
16716 =>
16717 OK := Is_Fun
16718 and then Num_F = 1
16719 and then Is_Floating_Point_Type (T);
16720
16721 when Attribute_Image
16722 | Attribute_Pred
16723 | Attribute_Succ
16724 | Attribute_Value
16725 | Attribute_Wide_Image
16726 | Attribute_Wide_Value
16727 =>
16728 OK := Is_Fun and then Num_F = 1 and then Is_Scalar_Type (T);
16729
16730 when Attribute_Max
16731 | Attribute_Min
16732 =>
16733 OK := Is_Fun and then Num_F = 2 and then Is_Scalar_Type (T);
16734
16735 when Attribute_Input =>
16736 OK := (Is_Fun and then Num_F = 1);
16737
16738 when Attribute_Output
16739 | Attribute_Put_Image
16740 | Attribute_Read
16741 | Attribute_Write
16742 =>
16743 OK := not Is_Fun and then Num_F = 2;
16744
16745 when others =>
16746 OK := False;
16747 end case;
16748
16749 if not OK then
16750 Error_Msg_N
16751 ("attribute reference has wrong profile for subprogram", Def);
16752 end if;
16753 end Valid_Default_Attribute;
16754
16755 end Sem_Ch12;