]> gcc.gnu.org Git - gcc.git/blob - gcc/ada/sem_ch12.adb
[Ada] Hang on generic declaration with ambiguous formal package
[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 -- The name of the formal package may be hidden by the
1952 -- formal parameter itself.
1953
1954 if Error_Posted (Analyzed_Formal) then
1955 Abandon_Instantiation (Instantiation_Node);
1956
1957 else
1958 Match :=
1959 Matching_Actual
1960 (Defining_Identifier (Formal),
1961 Defining_Identifier
1962 (Original_Node (Analyzed_Formal)));
1963 end if;
1964
1965 if No (Match) then
1966 if Partial_Parameterization then
1967 Process_Default (Formal);
1968
1969 else
1970 Error_Msg_Sloc := Sloc (Gen_Unit);
1971 Error_Msg_NE
1972 ("missing actual&",
1973 Instantiation_Node, Defining_Identifier (Formal));
1974 Error_Msg_NE
1975 ("\in instantiation of & declared#",
1976 Instantiation_Node, Gen_Unit);
1977
1978 Abandon_Instantiation (Instantiation_Node);
1979 end if;
1980
1981 else
1982 Analyze (Match);
1983 Append_List
1984 (Instantiate_Formal_Package
1985 (Formal, Match, Analyzed_Formal),
1986 Assoc_List);
1987
1988 -- Determine whether the actual package needs an explicit
1989 -- freeze node. This is only the case if the actual is
1990 -- declared in the same unit and has a body. Normally
1991 -- packages do not have explicit freeze nodes, and gigi
1992 -- only uses them to elaborate entities in a package
1993 -- body.
1994
1995 Explicit_Freeze_Check : declare
1996 Actual : constant Entity_Id := Entity (Match);
1997 Gen_Par : Entity_Id;
1998
1999 Needs_Freezing : Boolean;
2000 S : Entity_Id;
2001
2002 procedure Check_Generic_Parent;
2003 -- The actual may be an instantiation of a unit
2004 -- declared in a previous instantiation. If that
2005 -- one is also in the current compilation, it must
2006 -- itself be frozen before the actual. The actual
2007 -- may be an instantiation of a generic child unit,
2008 -- in which case the same applies to the instance
2009 -- of the parent which must be frozen before the
2010 -- actual.
2011 -- Should this itself be recursive ???
2012
2013 --------------------------
2014 -- Check_Generic_Parent --
2015 --------------------------
2016
2017 procedure Check_Generic_Parent is
2018 Inst : constant Node_Id :=
2019 Next (Unit_Declaration_Node (Actual));
2020 Par : Entity_Id;
2021
2022 begin
2023 Par := Empty;
2024
2025 if Nkind (Parent (Actual)) = N_Package_Specification
2026 then
2027 Par := Scope (Generic_Parent (Parent (Actual)));
2028
2029 if Is_Generic_Instance (Par) then
2030 null;
2031
2032 -- If the actual is a child generic unit, check
2033 -- whether the instantiation of the parent is
2034 -- also local and must also be frozen now. We
2035 -- must retrieve the instance node to locate the
2036 -- parent instance if any.
2037
2038 elsif Ekind (Par) = E_Generic_Package
2039 and then Is_Child_Unit (Gen_Par)
2040 and then Ekind (Scope (Gen_Par)) =
2041 E_Generic_Package
2042 then
2043 if Nkind (Inst) = N_Package_Instantiation
2044 and then Nkind (Name (Inst)) =
2045 N_Expanded_Name
2046 then
2047 -- Retrieve entity of parent instance
2048
2049 Par := Entity (Prefix (Name (Inst)));
2050 end if;
2051
2052 else
2053 Par := Empty;
2054 end if;
2055 end if;
2056
2057 if Present (Par)
2058 and then Is_Generic_Instance (Par)
2059 and then Scope (Par) = Current_Scope
2060 and then
2061 (No (Freeze_Node (Par))
2062 or else
2063 not Is_List_Member (Freeze_Node (Par)))
2064 then
2065 Set_Has_Delayed_Freeze (Par);
2066 Append_Elmt (Par, Actuals_To_Freeze);
2067 end if;
2068 end Check_Generic_Parent;
2069
2070 -- Start of processing for Explicit_Freeze_Check
2071
2072 begin
2073 if Present (Renamed_Entity (Actual)) then
2074 Gen_Par :=
2075 Generic_Parent (Specification
2076 (Unit_Declaration_Node
2077 (Renamed_Entity (Actual))));
2078 else
2079 Gen_Par :=
2080 Generic_Parent (Specification
2081 (Unit_Declaration_Node (Actual)));
2082 end if;
2083
2084 if not Expander_Active
2085 or else not Has_Completion (Actual)
2086 or else not In_Same_Source_Unit (I_Node, Actual)
2087 or else Is_Frozen (Actual)
2088 or else
2089 (Present (Renamed_Entity (Actual))
2090 and then
2091 not In_Same_Source_Unit
2092 (I_Node, (Renamed_Entity (Actual))))
2093 then
2094 null;
2095
2096 else
2097 -- Finally we want to exclude such freeze nodes
2098 -- from statement sequences, which freeze
2099 -- everything before them.
2100 -- Is this strictly necessary ???
2101
2102 Needs_Freezing := True;
2103
2104 S := Current_Scope;
2105 while Present (S) loop
2106 if Ekind_In (S, E_Block,
2107 E_Function,
2108 E_Loop,
2109 E_Procedure)
2110 then
2111 Needs_Freezing := False;
2112 exit;
2113 end if;
2114
2115 S := Scope (S);
2116 end loop;
2117
2118 if Needs_Freezing then
2119 Check_Generic_Parent;
2120
2121 -- If the actual is a renaming of a proper
2122 -- instance of the formal package, indicate
2123 -- that it is the instance that must be frozen.
2124
2125 if Nkind (Parent (Actual)) =
2126 N_Package_Renaming_Declaration
2127 then
2128 Set_Has_Delayed_Freeze
2129 (Renamed_Entity (Actual));
2130 Append_Elmt
2131 (Renamed_Entity (Actual),
2132 Actuals_To_Freeze);
2133 else
2134 Set_Has_Delayed_Freeze (Actual);
2135 Append_Elmt (Actual, Actuals_To_Freeze);
2136 end if;
2137 end if;
2138 end if;
2139 end Explicit_Freeze_Check;
2140 end if;
2141
2142 -- For use type and use package appearing in the generic part,
2143 -- we have already copied them, so we can just move them where
2144 -- they belong (we mustn't recopy them since this would mess up
2145 -- the Sloc values).
2146
2147 when N_Use_Package_Clause
2148 | N_Use_Type_Clause
2149 =>
2150 if Nkind (Original_Node (I_Node)) =
2151 N_Formal_Package_Declaration
2152 then
2153 Append (New_Copy_Tree (Formal), Assoc_List);
2154 else
2155 Remove (Formal);
2156 Append (Formal, Assoc_List);
2157 end if;
2158
2159 when others =>
2160 raise Program_Error;
2161 end case;
2162
2163 Formal := Saved_Formal;
2164 Next_Non_Pragma (Analyzed_Formal);
2165 end loop;
2166
2167 if Num_Actuals > Num_Matched then
2168 Error_Msg_Sloc := Sloc (Gen_Unit);
2169
2170 if Present (Selector_Name (Actual)) then
2171 Error_Msg_NE
2172 ("unmatched actual &", Actual, Selector_Name (Actual));
2173 Error_Msg_NE
2174 ("\in instantiation of & declared#", Actual, Gen_Unit);
2175 else
2176 Error_Msg_NE
2177 ("unmatched actual in instantiation of & declared#",
2178 Actual, Gen_Unit);
2179 end if;
2180 end if;
2181
2182 elsif Present (Actuals) then
2183 Error_Msg_N
2184 ("too many actuals in generic instantiation", Instantiation_Node);
2185 end if;
2186
2187 -- An instantiation freezes all generic actuals. The only exceptions
2188 -- to this are incomplete types and subprograms which are not fully
2189 -- defined at the point of instantiation.
2190
2191 declare
2192 Elmt : Elmt_Id := First_Elmt (Actuals_To_Freeze);
2193 begin
2194 while Present (Elmt) loop
2195 Freeze_Before (I_Node, Node (Elmt));
2196 Next_Elmt (Elmt);
2197 end loop;
2198 end;
2199
2200 -- If there are default subprograms, normalize the tree by adding
2201 -- explicit associations for them. This is required if the instance
2202 -- appears within a generic.
2203
2204 if not Is_Empty_List (Default_Actuals) then
2205 declare
2206 Default : Node_Id;
2207
2208 begin
2209 Default := First (Default_Actuals);
2210 while Present (Default) loop
2211 Mark_Rewrite_Insertion (Default);
2212 Next (Default);
2213 end loop;
2214
2215 if No (Actuals) then
2216 Set_Generic_Associations (I_Node, Default_Actuals);
2217 else
2218 Append_List_To (Actuals, Default_Actuals);
2219 end if;
2220 end;
2221 end if;
2222
2223 -- If this is a formal package, normalize the parameter list by adding
2224 -- explicit box associations for the formals that are covered by an
2225 -- Others_Choice.
2226
2227 if not Is_Empty_List (Default_Formals) then
2228 Append_List (Default_Formals, Formals);
2229 end if;
2230
2231 return Assoc_List;
2232 end Analyze_Associations;
2233
2234 -------------------------------
2235 -- Analyze_Formal_Array_Type --
2236 -------------------------------
2237
2238 procedure Analyze_Formal_Array_Type
2239 (T : in out Entity_Id;
2240 Def : Node_Id)
2241 is
2242 DSS : Node_Id;
2243
2244 begin
2245 -- Treated like a non-generic array declaration, with additional
2246 -- semantic checks.
2247
2248 Enter_Name (T);
2249
2250 if Nkind (Def) = N_Constrained_Array_Definition then
2251 DSS := First (Discrete_Subtype_Definitions (Def));
2252 while Present (DSS) loop
2253 if Nkind_In (DSS, N_Subtype_Indication,
2254 N_Range,
2255 N_Attribute_Reference)
2256 then
2257 Error_Msg_N ("only a subtype mark is allowed in a formal", DSS);
2258 end if;
2259
2260 Next (DSS);
2261 end loop;
2262 end if;
2263
2264 Array_Type_Declaration (T, Def);
2265 Set_Is_Generic_Type (Base_Type (T));
2266
2267 if Ekind (Component_Type (T)) = E_Incomplete_Type
2268 and then No (Full_View (Component_Type (T)))
2269 then
2270 Error_Msg_N ("premature usage of incomplete type", Def);
2271
2272 -- Check that range constraint is not allowed on the component type
2273 -- of a generic formal array type (AARM 12.5.3(3))
2274
2275 elsif Is_Internal (Component_Type (T))
2276 and then Present (Subtype_Indication (Component_Definition (Def)))
2277 and then Nkind (Original_Node
2278 (Subtype_Indication (Component_Definition (Def)))) =
2279 N_Subtype_Indication
2280 then
2281 Error_Msg_N
2282 ("in a formal, a subtype indication can only be "
2283 & "a subtype mark (RM 12.5.3(3))",
2284 Subtype_Indication (Component_Definition (Def)));
2285 end if;
2286
2287 end Analyze_Formal_Array_Type;
2288
2289 ---------------------------------------------
2290 -- Analyze_Formal_Decimal_Fixed_Point_Type --
2291 ---------------------------------------------
2292
2293 -- As for other generic types, we create a valid type representation with
2294 -- legal but arbitrary attributes, whose values are never considered
2295 -- static. For all scalar types we introduce an anonymous base type, with
2296 -- the same attributes. We choose the corresponding integer type to be
2297 -- Standard_Integer.
2298 -- Here and in other similar routines, the Sloc of the generated internal
2299 -- type must be the same as the sloc of the defining identifier of the
2300 -- formal type declaration, to provide proper source navigation.
2301
2302 procedure Analyze_Formal_Decimal_Fixed_Point_Type
2303 (T : Entity_Id;
2304 Def : Node_Id)
2305 is
2306 Loc : constant Source_Ptr := Sloc (Def);
2307
2308 Base : constant Entity_Id :=
2309 New_Internal_Entity
2310 (E_Decimal_Fixed_Point_Type,
2311 Current_Scope,
2312 Sloc (Defining_Identifier (Parent (Def))), 'G');
2313
2314 Int_Base : constant Entity_Id := Standard_Integer;
2315 Delta_Val : constant Ureal := Ureal_1;
2316 Digs_Val : constant Uint := Uint_6;
2317
2318 function Make_Dummy_Bound return Node_Id;
2319 -- Return a properly typed universal real literal to use as a bound
2320
2321 ----------------------
2322 -- Make_Dummy_Bound --
2323 ----------------------
2324
2325 function Make_Dummy_Bound return Node_Id is
2326 Bound : constant Node_Id := Make_Real_Literal (Loc, Ureal_1);
2327 begin
2328 Set_Etype (Bound, Universal_Real);
2329 return Bound;
2330 end Make_Dummy_Bound;
2331
2332 -- Start of processing for Analyze_Formal_Decimal_Fixed_Point_Type
2333
2334 begin
2335 Enter_Name (T);
2336
2337 Set_Etype (Base, Base);
2338 Set_Size_Info (Base, Int_Base);
2339 Set_RM_Size (Base, RM_Size (Int_Base));
2340 Set_First_Rep_Item (Base, First_Rep_Item (Int_Base));
2341 Set_Digits_Value (Base, Digs_Val);
2342 Set_Delta_Value (Base, Delta_Val);
2343 Set_Small_Value (Base, Delta_Val);
2344 Set_Scalar_Range (Base,
2345 Make_Range (Loc,
2346 Low_Bound => Make_Dummy_Bound,
2347 High_Bound => Make_Dummy_Bound));
2348
2349 Set_Is_Generic_Type (Base);
2350 Set_Parent (Base, Parent (Def));
2351
2352 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
2353 Set_Etype (T, Base);
2354 Set_Size_Info (T, Int_Base);
2355 Set_RM_Size (T, RM_Size (Int_Base));
2356 Set_First_Rep_Item (T, First_Rep_Item (Int_Base));
2357 Set_Digits_Value (T, Digs_Val);
2358 Set_Delta_Value (T, Delta_Val);
2359 Set_Small_Value (T, Delta_Val);
2360 Set_Scalar_Range (T, Scalar_Range (Base));
2361 Set_Is_Constrained (T);
2362
2363 Check_Restriction (No_Fixed_Point, Def);
2364 end Analyze_Formal_Decimal_Fixed_Point_Type;
2365
2366 -------------------------------------------
2367 -- Analyze_Formal_Derived_Interface_Type --
2368 -------------------------------------------
2369
2370 procedure Analyze_Formal_Derived_Interface_Type
2371 (N : Node_Id;
2372 T : Entity_Id;
2373 Def : Node_Id)
2374 is
2375 Loc : constant Source_Ptr := Sloc (Def);
2376
2377 begin
2378 -- Rewrite as a type declaration of a derived type. This ensures that
2379 -- the interface list and primitive operations are properly captured.
2380
2381 Rewrite (N,
2382 Make_Full_Type_Declaration (Loc,
2383 Defining_Identifier => T,
2384 Type_Definition => Def));
2385 Analyze (N);
2386 Set_Is_Generic_Type (T);
2387 end Analyze_Formal_Derived_Interface_Type;
2388
2389 ---------------------------------
2390 -- Analyze_Formal_Derived_Type --
2391 ---------------------------------
2392
2393 procedure Analyze_Formal_Derived_Type
2394 (N : Node_Id;
2395 T : Entity_Id;
2396 Def : Node_Id)
2397 is
2398 Loc : constant Source_Ptr := Sloc (Def);
2399 Unk_Disc : constant Boolean := Unknown_Discriminants_Present (N);
2400 New_N : Node_Id;
2401
2402 begin
2403 Set_Is_Generic_Type (T);
2404
2405 if Private_Present (Def) then
2406 New_N :=
2407 Make_Private_Extension_Declaration (Loc,
2408 Defining_Identifier => T,
2409 Discriminant_Specifications => Discriminant_Specifications (N),
2410 Unknown_Discriminants_Present => Unk_Disc,
2411 Subtype_Indication => Subtype_Mark (Def),
2412 Interface_List => Interface_List (Def));
2413
2414 Set_Abstract_Present (New_N, Abstract_Present (Def));
2415 Set_Limited_Present (New_N, Limited_Present (Def));
2416 Set_Synchronized_Present (New_N, Synchronized_Present (Def));
2417
2418 else
2419 New_N :=
2420 Make_Full_Type_Declaration (Loc,
2421 Defining_Identifier => T,
2422 Discriminant_Specifications =>
2423 Discriminant_Specifications (Parent (T)),
2424 Type_Definition =>
2425 Make_Derived_Type_Definition (Loc,
2426 Subtype_Indication => Subtype_Mark (Def)));
2427
2428 Set_Abstract_Present
2429 (Type_Definition (New_N), Abstract_Present (Def));
2430 Set_Limited_Present
2431 (Type_Definition (New_N), Limited_Present (Def));
2432 end if;
2433
2434 Rewrite (N, New_N);
2435 Analyze (N);
2436
2437 if Unk_Disc then
2438 if not Is_Composite_Type (T) then
2439 Error_Msg_N
2440 ("unknown discriminants not allowed for elementary types", N);
2441 else
2442 Set_Has_Unknown_Discriminants (T);
2443 Set_Is_Constrained (T, False);
2444 end if;
2445 end if;
2446
2447 -- If the parent type has a known size, so does the formal, which makes
2448 -- legal representation clauses that involve the formal.
2449
2450 Set_Size_Known_At_Compile_Time
2451 (T, Size_Known_At_Compile_Time (Entity (Subtype_Mark (Def))));
2452 end Analyze_Formal_Derived_Type;
2453
2454 ----------------------------------
2455 -- Analyze_Formal_Discrete_Type --
2456 ----------------------------------
2457
2458 -- The operations defined for a discrete types are those of an enumeration
2459 -- type. The size is set to an arbitrary value, for use in analyzing the
2460 -- generic unit.
2461
2462 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id) is
2463 Loc : constant Source_Ptr := Sloc (Def);
2464 Lo : Node_Id;
2465 Hi : Node_Id;
2466
2467 Base : constant Entity_Id :=
2468 New_Internal_Entity
2469 (E_Floating_Point_Type, Current_Scope,
2470 Sloc (Defining_Identifier (Parent (Def))), 'G');
2471
2472 begin
2473 Enter_Name (T);
2474 Set_Ekind (T, E_Enumeration_Subtype);
2475 Set_Etype (T, Base);
2476 Init_Size (T, 8);
2477 Init_Alignment (T);
2478 Set_Is_Generic_Type (T);
2479 Set_Is_Constrained (T);
2480
2481 -- For semantic analysis, the bounds of the type must be set to some
2482 -- non-static value. The simplest is to create attribute nodes for those
2483 -- bounds, that refer to the type itself. These bounds are never
2484 -- analyzed but serve as place-holders.
2485
2486 Lo :=
2487 Make_Attribute_Reference (Loc,
2488 Attribute_Name => Name_First,
2489 Prefix => New_Occurrence_Of (T, Loc));
2490 Set_Etype (Lo, T);
2491
2492 Hi :=
2493 Make_Attribute_Reference (Loc,
2494 Attribute_Name => Name_Last,
2495 Prefix => New_Occurrence_Of (T, Loc));
2496 Set_Etype (Hi, T);
2497
2498 Set_Scalar_Range (T,
2499 Make_Range (Loc,
2500 Low_Bound => Lo,
2501 High_Bound => Hi));
2502
2503 Set_Ekind (Base, E_Enumeration_Type);
2504 Set_Etype (Base, Base);
2505 Init_Size (Base, 8);
2506 Init_Alignment (Base);
2507 Set_Is_Generic_Type (Base);
2508 Set_Scalar_Range (Base, Scalar_Range (T));
2509 Set_Parent (Base, Parent (Def));
2510 end Analyze_Formal_Discrete_Type;
2511
2512 ----------------------------------
2513 -- Analyze_Formal_Floating_Type --
2514 ---------------------------------
2515
2516 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id) is
2517 Base : constant Entity_Id :=
2518 New_Internal_Entity
2519 (E_Floating_Point_Type, Current_Scope,
2520 Sloc (Defining_Identifier (Parent (Def))), 'G');
2521
2522 begin
2523 -- The various semantic attributes are taken from the predefined type
2524 -- Float, just so that all of them are initialized. Their values are
2525 -- never used because no constant folding or expansion takes place in
2526 -- the generic itself.
2527
2528 Enter_Name (T);
2529 Set_Ekind (T, E_Floating_Point_Subtype);
2530 Set_Etype (T, Base);
2531 Set_Size_Info (T, (Standard_Float));
2532 Set_RM_Size (T, RM_Size (Standard_Float));
2533 Set_Digits_Value (T, Digits_Value (Standard_Float));
2534 Set_Scalar_Range (T, Scalar_Range (Standard_Float));
2535 Set_Is_Constrained (T);
2536
2537 Set_Is_Generic_Type (Base);
2538 Set_Etype (Base, Base);
2539 Set_Size_Info (Base, (Standard_Float));
2540 Set_RM_Size (Base, RM_Size (Standard_Float));
2541 Set_Digits_Value (Base, Digits_Value (Standard_Float));
2542 Set_Scalar_Range (Base, Scalar_Range (Standard_Float));
2543 Set_Parent (Base, Parent (Def));
2544
2545 Check_Restriction (No_Floating_Point, Def);
2546 end Analyze_Formal_Floating_Type;
2547
2548 -----------------------------------
2549 -- Analyze_Formal_Interface_Type;--
2550 -----------------------------------
2551
2552 procedure Analyze_Formal_Interface_Type
2553 (N : Node_Id;
2554 T : Entity_Id;
2555 Def : Node_Id)
2556 is
2557 Loc : constant Source_Ptr := Sloc (N);
2558 New_N : Node_Id;
2559
2560 begin
2561 New_N :=
2562 Make_Full_Type_Declaration (Loc,
2563 Defining_Identifier => T,
2564 Type_Definition => Def);
2565
2566 Rewrite (N, New_N);
2567 Analyze (N);
2568 Set_Is_Generic_Type (T);
2569 end Analyze_Formal_Interface_Type;
2570
2571 ---------------------------------
2572 -- Analyze_Formal_Modular_Type --
2573 ---------------------------------
2574
2575 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id) is
2576 begin
2577 -- Apart from their entity kind, generic modular types are treated like
2578 -- signed integer types, and have the same attributes.
2579
2580 Analyze_Formal_Signed_Integer_Type (T, Def);
2581 Set_Ekind (T, E_Modular_Integer_Subtype);
2582 Set_Ekind (Etype (T), E_Modular_Integer_Type);
2583
2584 end Analyze_Formal_Modular_Type;
2585
2586 ---------------------------------------
2587 -- Analyze_Formal_Object_Declaration --
2588 ---------------------------------------
2589
2590 procedure Analyze_Formal_Object_Declaration (N : Node_Id) is
2591 E : constant Node_Id := Default_Expression (N);
2592 Id : constant Node_Id := Defining_Identifier (N);
2593 K : Entity_Kind;
2594 T : Node_Id;
2595
2596 begin
2597 Enter_Name (Id);
2598
2599 -- Determine the mode of the formal object
2600
2601 if Out_Present (N) then
2602 K := E_Generic_In_Out_Parameter;
2603
2604 if not In_Present (N) then
2605 Error_Msg_N ("formal generic objects cannot have mode OUT", N);
2606 end if;
2607
2608 else
2609 K := E_Generic_In_Parameter;
2610 end if;
2611
2612 if Present (Subtype_Mark (N)) then
2613 Find_Type (Subtype_Mark (N));
2614 T := Entity (Subtype_Mark (N));
2615
2616 -- Verify that there is no redundant null exclusion
2617
2618 if Null_Exclusion_Present (N) then
2619 if not Is_Access_Type (T) then
2620 Error_Msg_N
2621 ("null exclusion can only apply to an access type", N);
2622
2623 elsif Can_Never_Be_Null (T) then
2624 Error_Msg_NE
2625 ("`NOT NULL` not allowed (& already excludes null)", N, T);
2626 end if;
2627 end if;
2628
2629 -- Ada 2005 (AI-423): Formal object with an access definition
2630
2631 else
2632 Check_Access_Definition (N);
2633 T := Access_Definition
2634 (Related_Nod => N,
2635 N => Access_Definition (N));
2636 end if;
2637
2638 if Ekind (T) = E_Incomplete_Type then
2639 declare
2640 Error_Node : Node_Id;
2641
2642 begin
2643 if Present (Subtype_Mark (N)) then
2644 Error_Node := Subtype_Mark (N);
2645 else
2646 Check_Access_Definition (N);
2647 Error_Node := Access_Definition (N);
2648 end if;
2649
2650 Error_Msg_N ("premature usage of incomplete type", Error_Node);
2651 end;
2652 end if;
2653
2654 if K = E_Generic_In_Parameter then
2655
2656 -- Ada 2005 (AI-287): Limited aggregates allowed in generic formals
2657
2658 if Ada_Version < Ada_2005 and then Is_Limited_Type (T) then
2659 Error_Msg_N
2660 ("generic formal of mode IN must not be of limited type", N);
2661 Explain_Limited_Type (T, N);
2662 end if;
2663
2664 if Is_Abstract_Type (T) then
2665 Error_Msg_N
2666 ("generic formal of mode IN must not be of abstract type", N);
2667 end if;
2668
2669 if Present (E) then
2670 Preanalyze_Spec_Expression (E, T);
2671
2672 if Is_Limited_Type (T) and then not OK_For_Limited_Init (T, E) then
2673 Error_Msg_N
2674 ("initialization not allowed for limited types", E);
2675 Explain_Limited_Type (T, E);
2676 end if;
2677 end if;
2678
2679 Set_Ekind (Id, K);
2680 Set_Etype (Id, T);
2681
2682 -- Case of generic IN OUT parameter
2683
2684 else
2685 -- If the formal has an unconstrained type, construct its actual
2686 -- subtype, as is done for subprogram formals. In this fashion, all
2687 -- its uses can refer to specific bounds.
2688
2689 Set_Ekind (Id, K);
2690 Set_Etype (Id, T);
2691
2692 if (Is_Array_Type (T) and then not Is_Constrained (T))
2693 or else (Ekind (T) = E_Record_Type and then Has_Discriminants (T))
2694 then
2695 declare
2696 Non_Freezing_Ref : constant Node_Id :=
2697 New_Occurrence_Of (Id, Sloc (Id));
2698 Decl : Node_Id;
2699
2700 begin
2701 -- Make sure the actual subtype doesn't generate bogus freezing
2702
2703 Set_Must_Not_Freeze (Non_Freezing_Ref);
2704 Decl := Build_Actual_Subtype (T, Non_Freezing_Ref);
2705 Insert_Before_And_Analyze (N, Decl);
2706 Set_Actual_Subtype (Id, Defining_Identifier (Decl));
2707 end;
2708 else
2709 Set_Actual_Subtype (Id, T);
2710 end if;
2711
2712 if Present (E) then
2713 Error_Msg_N
2714 ("initialization not allowed for `IN OUT` formals", N);
2715 end if;
2716 end if;
2717
2718 if Has_Aspects (N) then
2719 Analyze_Aspect_Specifications (N, Id);
2720 end if;
2721 end Analyze_Formal_Object_Declaration;
2722
2723 ----------------------------------------------
2724 -- Analyze_Formal_Ordinary_Fixed_Point_Type --
2725 ----------------------------------------------
2726
2727 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
2728 (T : Entity_Id;
2729 Def : Node_Id)
2730 is
2731 Loc : constant Source_Ptr := Sloc (Def);
2732 Base : constant Entity_Id :=
2733 New_Internal_Entity
2734 (E_Ordinary_Fixed_Point_Type, Current_Scope,
2735 Sloc (Defining_Identifier (Parent (Def))), 'G');
2736
2737 begin
2738 -- The semantic attributes are set for completeness only, their values
2739 -- will never be used, since all properties of the type are non-static.
2740
2741 Enter_Name (T);
2742 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
2743 Set_Etype (T, Base);
2744 Set_Size_Info (T, Standard_Integer);
2745 Set_RM_Size (T, RM_Size (Standard_Integer));
2746 Set_Small_Value (T, Ureal_1);
2747 Set_Delta_Value (T, Ureal_1);
2748 Set_Scalar_Range (T,
2749 Make_Range (Loc,
2750 Low_Bound => Make_Real_Literal (Loc, Ureal_1),
2751 High_Bound => Make_Real_Literal (Loc, Ureal_1)));
2752 Set_Is_Constrained (T);
2753
2754 Set_Is_Generic_Type (Base);
2755 Set_Etype (Base, Base);
2756 Set_Size_Info (Base, Standard_Integer);
2757 Set_RM_Size (Base, RM_Size (Standard_Integer));
2758 Set_Small_Value (Base, Ureal_1);
2759 Set_Delta_Value (Base, Ureal_1);
2760 Set_Scalar_Range (Base, Scalar_Range (T));
2761 Set_Parent (Base, Parent (Def));
2762
2763 Check_Restriction (No_Fixed_Point, Def);
2764 end Analyze_Formal_Ordinary_Fixed_Point_Type;
2765
2766 ----------------------------------------
2767 -- Analyze_Formal_Package_Declaration --
2768 ----------------------------------------
2769
2770 procedure Analyze_Formal_Package_Declaration (N : Node_Id) is
2771 Gen_Id : constant Node_Id := Name (N);
2772 Loc : constant Source_Ptr := Sloc (N);
2773 Pack_Id : constant Entity_Id := Defining_Identifier (N);
2774 Formal : Entity_Id;
2775 Gen_Decl : Node_Id;
2776 Gen_Unit : Entity_Id;
2777 Renaming : Node_Id;
2778
2779 Vis_Prims_List : Elist_Id := No_Elist;
2780 -- List of primitives made temporarily visible in the instantiation
2781 -- to match the visibility of the formal type.
2782
2783 function Build_Local_Package return Node_Id;
2784 -- The formal package is rewritten so that its parameters are replaced
2785 -- with corresponding declarations. For parameters with bona fide
2786 -- associations these declarations are created by Analyze_Associations
2787 -- as for a regular instantiation. For boxed parameters, we preserve
2788 -- the formal declarations and analyze them, in order to introduce
2789 -- entities of the right kind in the environment of the formal.
2790
2791 -------------------------
2792 -- Build_Local_Package --
2793 -------------------------
2794
2795 function Build_Local_Package return Node_Id is
2796 Decls : List_Id;
2797 Pack_Decl : Node_Id;
2798
2799 begin
2800 -- Within the formal, the name of the generic package is a renaming
2801 -- of the formal (as for a regular instantiation).
2802
2803 Pack_Decl :=
2804 Make_Package_Declaration (Loc,
2805 Specification =>
2806 Copy_Generic_Node
2807 (Specification (Original_Node (Gen_Decl)),
2808 Empty, Instantiating => True));
2809
2810 Renaming :=
2811 Make_Package_Renaming_Declaration (Loc,
2812 Defining_Unit_Name =>
2813 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
2814 Name => New_Occurrence_Of (Formal, Loc));
2815
2816 if Nkind (Gen_Id) = N_Identifier
2817 and then Chars (Gen_Id) = Chars (Pack_Id)
2818 then
2819 Error_Msg_NE
2820 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
2821 end if;
2822
2823 -- If the formal is declared with a box, or with an others choice,
2824 -- create corresponding declarations for all entities in the formal
2825 -- part, so that names with the proper types are available in the
2826 -- specification of the formal package.
2827
2828 -- On the other hand, if there are no associations, then all the
2829 -- formals must have defaults, and this will be checked by the
2830 -- call to Analyze_Associations.
2831
2832 if Box_Present (N)
2833 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2834 then
2835 declare
2836 Formal_Decl : Node_Id;
2837
2838 begin
2839 -- TBA : for a formal package, need to recurse ???
2840
2841 Decls := New_List;
2842 Formal_Decl :=
2843 First
2844 (Generic_Formal_Declarations (Original_Node (Gen_Decl)));
2845 while Present (Formal_Decl) loop
2846 Append_To
2847 (Decls,
2848 Copy_Generic_Node
2849 (Formal_Decl, Empty, Instantiating => True));
2850 Next (Formal_Decl);
2851 end loop;
2852 end;
2853
2854 -- If generic associations are present, use Analyze_Associations to
2855 -- create the proper renaming declarations.
2856
2857 else
2858 declare
2859 Act_Tree : constant Node_Id :=
2860 Copy_Generic_Node
2861 (Original_Node (Gen_Decl), Empty,
2862 Instantiating => True);
2863
2864 begin
2865 Generic_Renamings.Set_Last (0);
2866 Generic_Renamings_HTable.Reset;
2867 Instantiation_Node := N;
2868
2869 Decls :=
2870 Analyze_Associations
2871 (I_Node => Original_Node (N),
2872 Formals => Generic_Formal_Declarations (Act_Tree),
2873 F_Copy => Generic_Formal_Declarations (Gen_Decl));
2874
2875 Vis_Prims_List := Check_Hidden_Primitives (Decls);
2876 end;
2877 end if;
2878
2879 Append (Renaming, To => Decls);
2880
2881 -- Add generated declarations ahead of local declarations in
2882 -- the package.
2883
2884 if No (Visible_Declarations (Specification (Pack_Decl))) then
2885 Set_Visible_Declarations (Specification (Pack_Decl), Decls);
2886 else
2887 Insert_List_Before
2888 (First (Visible_Declarations (Specification (Pack_Decl))),
2889 Decls);
2890 end if;
2891
2892 return Pack_Decl;
2893 end Build_Local_Package;
2894
2895 -- Local variables
2896
2897 Save_ISMP : constant Boolean := Ignore_SPARK_Mode_Pragmas_In_Instance;
2898 -- Save flag Ignore_SPARK_Mode_Pragmas_In_Instance for restore on exit
2899
2900 Associations : Boolean := True;
2901 New_N : Node_Id;
2902 Parent_Installed : Boolean := False;
2903 Parent_Instance : Entity_Id;
2904 Renaming_In_Par : Entity_Id;
2905
2906 -- Start of processing for Analyze_Formal_Package_Declaration
2907
2908 begin
2909 Check_Text_IO_Special_Unit (Gen_Id);
2910
2911 Init_Env;
2912 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
2913 Gen_Unit := Entity (Gen_Id);
2914
2915 -- Check for a formal package that is a package renaming
2916
2917 if Present (Renamed_Object (Gen_Unit)) then
2918
2919 -- Indicate that unit is used, before replacing it with renamed
2920 -- entity for use below.
2921
2922 if In_Extended_Main_Source_Unit (N) then
2923 Set_Is_Instantiated (Gen_Unit);
2924 Generate_Reference (Gen_Unit, N);
2925 end if;
2926
2927 Gen_Unit := Renamed_Object (Gen_Unit);
2928 end if;
2929
2930 if Ekind (Gen_Unit) /= E_Generic_Package then
2931 Error_Msg_N ("expect generic package name", Gen_Id);
2932 Restore_Env;
2933 goto Leave;
2934
2935 elsif Gen_Unit = Current_Scope then
2936 Error_Msg_N
2937 ("generic package cannot be used as a formal package of itself",
2938 Gen_Id);
2939 Restore_Env;
2940 goto Leave;
2941
2942 elsif In_Open_Scopes (Gen_Unit) then
2943 if Is_Compilation_Unit (Gen_Unit)
2944 and then Is_Child_Unit (Current_Scope)
2945 then
2946 -- Special-case the error when the formal is a parent, and
2947 -- continue analysis to minimize cascaded errors.
2948
2949 Error_Msg_N
2950 ("generic parent cannot be used as formal package of a child "
2951 & "unit", Gen_Id);
2952
2953 else
2954 Error_Msg_N
2955 ("generic package cannot be used as a formal package within "
2956 & "itself", Gen_Id);
2957 Restore_Env;
2958 goto Leave;
2959 end if;
2960 end if;
2961
2962 -- Check that name of formal package does not hide name of generic,
2963 -- or its leading prefix. This check must be done separately because
2964 -- the name of the generic has already been analyzed.
2965
2966 declare
2967 Gen_Name : Entity_Id;
2968
2969 begin
2970 Gen_Name := Gen_Id;
2971 while Nkind (Gen_Name) = N_Expanded_Name loop
2972 Gen_Name := Prefix (Gen_Name);
2973 end loop;
2974
2975 if Chars (Gen_Name) = Chars (Pack_Id) then
2976 Error_Msg_NE
2977 ("& is hidden within declaration of formal package",
2978 Gen_Id, Gen_Name);
2979 end if;
2980 end;
2981
2982 if Box_Present (N)
2983 or else No (Generic_Associations (N))
2984 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2985 then
2986 Associations := False;
2987 end if;
2988
2989 -- If there are no generic associations, the generic parameters appear
2990 -- as local entities and are instantiated like them. We copy the generic
2991 -- package declaration as if it were an instantiation, and analyze it
2992 -- like a regular package, except that we treat the formals as
2993 -- additional visible components.
2994
2995 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
2996
2997 if In_Extended_Main_Source_Unit (N) then
2998 Set_Is_Instantiated (Gen_Unit);
2999 Generate_Reference (Gen_Unit, N);
3000 end if;
3001
3002 Formal := New_Copy (Pack_Id);
3003 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
3004
3005 -- Make local generic without formals. The formals will be replaced with
3006 -- internal declarations.
3007
3008 begin
3009 New_N := Build_Local_Package;
3010
3011 -- If there are errors in the parameter list, Analyze_Associations
3012 -- raises Instantiation_Error. Patch the declaration to prevent further
3013 -- exception propagation.
3014
3015 exception
3016 when Instantiation_Error =>
3017 Enter_Name (Formal);
3018 Set_Ekind (Formal, E_Variable);
3019 Set_Etype (Formal, Any_Type);
3020 Restore_Hidden_Primitives (Vis_Prims_List);
3021
3022 if Parent_Installed then
3023 Remove_Parent;
3024 end if;
3025
3026 goto Leave;
3027 end;
3028
3029 Rewrite (N, New_N);
3030 Set_Defining_Unit_Name (Specification (New_N), Formal);
3031 Set_Generic_Parent (Specification (N), Gen_Unit);
3032 Set_Instance_Env (Gen_Unit, Formal);
3033 Set_Is_Generic_Instance (Formal);
3034
3035 Enter_Name (Formal);
3036 Set_Ekind (Formal, E_Package);
3037 Set_Etype (Formal, Standard_Void_Type);
3038 Set_Inner_Instances (Formal, New_Elmt_List);
3039
3040 -- It is unclear that any aspects can apply to a formal package
3041 -- declaration, given that they look like a hidden conformance
3042 -- requirement on the corresponding actual. However, Abstract_State
3043 -- must be treated specially because it generates declarations that
3044 -- must appear before other declarations in the specification and
3045 -- must be analyzed at once.
3046
3047 if Present (Aspect_Specifications (Gen_Decl)) then
3048 if No (Aspect_Specifications (N)) then
3049 Set_Aspect_Specifications (N, New_List);
3050 Set_Has_Aspects (N);
3051 end if;
3052
3053 declare
3054 ASN : Node_Id := First (Aspect_Specifications (Gen_Decl));
3055 New_A : Node_Id;
3056
3057 begin
3058 while Present (ASN) loop
3059 if Get_Aspect_Id (ASN) = Aspect_Abstract_State then
3060 New_A :=
3061 Copy_Generic_Node (ASN, Empty, Instantiating => True);
3062 Set_Entity (New_A, Formal);
3063 Set_Analyzed (New_A, False);
3064 Append (New_A, Aspect_Specifications (N));
3065 Analyze_Aspect_Specifications (N, Formal);
3066 exit;
3067 end if;
3068
3069 Next (ASN);
3070 end loop;
3071 end;
3072 end if;
3073
3074 Push_Scope (Formal);
3075
3076 -- Manually set the SPARK_Mode from the context because the package
3077 -- declaration is never analyzed.
3078
3079 Set_SPARK_Pragma (Formal, SPARK_Mode_Pragma);
3080 Set_SPARK_Aux_Pragma (Formal, SPARK_Mode_Pragma);
3081 Set_SPARK_Pragma_Inherited (Formal);
3082 Set_SPARK_Aux_Pragma_Inherited (Formal);
3083
3084 if Is_Child_Unit (Gen_Unit) and then Parent_Installed then
3085
3086 -- Similarly, we have to make the name of the formal visible in the
3087 -- parent instance, to resolve properly fully qualified names that
3088 -- may appear in the generic unit. The parent instance has been
3089 -- placed on the scope stack ahead of the current scope.
3090
3091 Parent_Instance := Scope_Stack.Table (Scope_Stack.Last - 1).Entity;
3092
3093 Renaming_In_Par :=
3094 Make_Defining_Identifier (Loc, Chars (Gen_Unit));
3095 Set_Ekind (Renaming_In_Par, E_Package);
3096 Set_Etype (Renaming_In_Par, Standard_Void_Type);
3097 Set_Scope (Renaming_In_Par, Parent_Instance);
3098 Set_Parent (Renaming_In_Par, Parent (Formal));
3099 Set_Renamed_Object (Renaming_In_Par, Formal);
3100 Append_Entity (Renaming_In_Par, Parent_Instance);
3101 end if;
3102
3103 -- A formal package declaration behaves as a package instantiation with
3104 -- respect to SPARK_Mode "off". If the annotation is "off" or altogether
3105 -- missing, set the global flag which signals Analyze_Pragma to ingnore
3106 -- all SPARK_Mode pragmas within the generic_package_name.
3107
3108 if SPARK_Mode /= On then
3109 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
3110
3111 -- Mark the formal spec in case the body is instantiated at a later
3112 -- pass. This preserves the original context in effect for the body.
3113
3114 Set_Ignore_SPARK_Mode_Pragmas (Formal);
3115 end if;
3116
3117 Analyze (Specification (N));
3118
3119 -- The formals for which associations are provided are not visible
3120 -- outside of the formal package. The others are still declared by a
3121 -- formal parameter declaration.
3122
3123 -- If there are no associations, the only local entity to hide is the
3124 -- generated package renaming itself.
3125
3126 declare
3127 E : Entity_Id;
3128
3129 begin
3130 E := First_Entity (Formal);
3131 while Present (E) loop
3132 if Associations and then not Is_Generic_Formal (E) then
3133 Set_Is_Hidden (E);
3134 end if;
3135
3136 if Ekind (E) = E_Package and then Renamed_Entity (E) = Formal then
3137 Set_Is_Hidden (E);
3138 exit;
3139 end if;
3140
3141 Next_Entity (E);
3142 end loop;
3143 end;
3144
3145 End_Package_Scope (Formal);
3146 Restore_Hidden_Primitives (Vis_Prims_List);
3147
3148 if Parent_Installed then
3149 Remove_Parent;
3150 end if;
3151
3152 Restore_Env;
3153
3154 -- Inside the generic unit, the formal package is a regular package, but
3155 -- no body is needed for it. Note that after instantiation, the defining
3156 -- unit name we need is in the new tree and not in the original (see
3157 -- Package_Instantiation). A generic formal package is an instance, and
3158 -- can be used as an actual for an inner instance.
3159
3160 Set_Has_Completion (Formal, True);
3161
3162 -- Add semantic information to the original defining identifier.
3163
3164 Set_Ekind (Pack_Id, E_Package);
3165 Set_Etype (Pack_Id, Standard_Void_Type);
3166 Set_Scope (Pack_Id, Scope (Formal));
3167 Set_Has_Completion (Pack_Id, True);
3168
3169 <<Leave>>
3170 if Has_Aspects (N) then
3171 -- Unclear that any other aspects may appear here, snalyze them
3172 -- for completion, given that the grammar allows their appearance.
3173
3174 Analyze_Aspect_Specifications (N, Pack_Id);
3175 end if;
3176
3177 Ignore_SPARK_Mode_Pragmas_In_Instance := Save_ISMP;
3178 end Analyze_Formal_Package_Declaration;
3179
3180 ---------------------------------
3181 -- Analyze_Formal_Private_Type --
3182 ---------------------------------
3183
3184 procedure Analyze_Formal_Private_Type
3185 (N : Node_Id;
3186 T : Entity_Id;
3187 Def : Node_Id)
3188 is
3189 begin
3190 New_Private_Type (N, T, Def);
3191
3192 -- Set the size to an arbitrary but legal value
3193
3194 Set_Size_Info (T, Standard_Integer);
3195 Set_RM_Size (T, RM_Size (Standard_Integer));
3196 end Analyze_Formal_Private_Type;
3197
3198 ------------------------------------
3199 -- Analyze_Formal_Incomplete_Type --
3200 ------------------------------------
3201
3202 procedure Analyze_Formal_Incomplete_Type
3203 (T : Entity_Id;
3204 Def : Node_Id)
3205 is
3206 begin
3207 Enter_Name (T);
3208 Set_Ekind (T, E_Incomplete_Type);
3209 Set_Etype (T, T);
3210 Set_Private_Dependents (T, New_Elmt_List);
3211
3212 if Tagged_Present (Def) then
3213 Set_Is_Tagged_Type (T);
3214 Make_Class_Wide_Type (T);
3215 Set_Direct_Primitive_Operations (T, New_Elmt_List);
3216 end if;
3217 end Analyze_Formal_Incomplete_Type;
3218
3219 ----------------------------------------
3220 -- Analyze_Formal_Signed_Integer_Type --
3221 ----------------------------------------
3222
3223 procedure Analyze_Formal_Signed_Integer_Type
3224 (T : Entity_Id;
3225 Def : Node_Id)
3226 is
3227 Base : constant Entity_Id :=
3228 New_Internal_Entity
3229 (E_Signed_Integer_Type,
3230 Current_Scope,
3231 Sloc (Defining_Identifier (Parent (Def))), 'G');
3232
3233 begin
3234 Enter_Name (T);
3235
3236 Set_Ekind (T, E_Signed_Integer_Subtype);
3237 Set_Etype (T, Base);
3238 Set_Size_Info (T, Standard_Integer);
3239 Set_RM_Size (T, RM_Size (Standard_Integer));
3240 Set_Scalar_Range (T, Scalar_Range (Standard_Integer));
3241 Set_Is_Constrained (T);
3242
3243 Set_Is_Generic_Type (Base);
3244 Set_Size_Info (Base, Standard_Integer);
3245 Set_RM_Size (Base, RM_Size (Standard_Integer));
3246 Set_Etype (Base, Base);
3247 Set_Scalar_Range (Base, Scalar_Range (Standard_Integer));
3248 Set_Parent (Base, Parent (Def));
3249 end Analyze_Formal_Signed_Integer_Type;
3250
3251 -------------------------------------------
3252 -- Analyze_Formal_Subprogram_Declaration --
3253 -------------------------------------------
3254
3255 procedure Analyze_Formal_Subprogram_Declaration (N : Node_Id) is
3256 Spec : constant Node_Id := Specification (N);
3257 Def : constant Node_Id := Default_Name (N);
3258 Nam : constant Entity_Id := Defining_Unit_Name (Spec);
3259 Subp : Entity_Id;
3260
3261 begin
3262 if Nam = Error then
3263 return;
3264 end if;
3265
3266 if Nkind (Nam) = N_Defining_Program_Unit_Name then
3267 Error_Msg_N ("name of formal subprogram must be a direct name", Nam);
3268 goto Leave;
3269 end if;
3270
3271 Analyze_Subprogram_Declaration (N);
3272 Set_Is_Formal_Subprogram (Nam);
3273 Set_Has_Completion (Nam);
3274
3275 if Nkind (N) = N_Formal_Abstract_Subprogram_Declaration then
3276 Set_Is_Abstract_Subprogram (Nam);
3277
3278 Set_Is_Dispatching_Operation (Nam);
3279
3280 -- A formal abstract procedure cannot have a null default
3281 -- (RM 12.6(4.1/2)).
3282
3283 if Nkind (Spec) = N_Procedure_Specification
3284 and then Null_Present (Spec)
3285 then
3286 Error_Msg_N
3287 ("a formal abstract subprogram cannot default to null", Spec);
3288 end if;
3289
3290 declare
3291 Ctrl_Type : constant Entity_Id := Find_Dispatching_Type (Nam);
3292 begin
3293 if No (Ctrl_Type) then
3294 Error_Msg_N
3295 ("abstract formal subprogram must have a controlling type",
3296 N);
3297
3298 elsif Ada_Version >= Ada_2012
3299 and then Is_Incomplete_Type (Ctrl_Type)
3300 then
3301 Error_Msg_NE
3302 ("controlling type of abstract formal subprogram cannot "
3303 & "be incomplete type", N, Ctrl_Type);
3304
3305 else
3306 Check_Controlling_Formals (Ctrl_Type, Nam);
3307 end if;
3308 end;
3309 end if;
3310
3311 -- Default name is resolved at the point of instantiation
3312
3313 if Box_Present (N) then
3314 null;
3315
3316 -- Else default is bound at the point of generic declaration
3317
3318 elsif Present (Def) then
3319 if Nkind (Def) = N_Operator_Symbol then
3320 Find_Direct_Name (Def);
3321
3322 elsif Nkind (Def) /= N_Attribute_Reference then
3323 Analyze (Def);
3324
3325 else
3326 -- For an attribute reference, analyze the prefix and verify
3327 -- that it has the proper profile for the subprogram.
3328
3329 Analyze (Prefix (Def));
3330 Valid_Default_Attribute (Nam, Def);
3331 goto Leave;
3332 end if;
3333
3334 -- Default name may be overloaded, in which case the interpretation
3335 -- with the correct profile must be selected, as for a renaming.
3336 -- If the definition is an indexed component, it must denote a
3337 -- member of an entry family. If it is a selected component, it
3338 -- can be a protected operation.
3339
3340 if Etype (Def) = Any_Type then
3341 goto Leave;
3342
3343 elsif Nkind (Def) = N_Selected_Component then
3344 if not Is_Overloadable (Entity (Selector_Name (Def))) then
3345 Error_Msg_N ("expect valid subprogram name as default", Def);
3346 end if;
3347
3348 elsif Nkind (Def) = N_Indexed_Component then
3349 if Is_Entity_Name (Prefix (Def)) then
3350 if Ekind (Entity (Prefix (Def))) /= E_Entry_Family then
3351 Error_Msg_N ("expect valid subprogram name as default", Def);
3352 end if;
3353
3354 elsif Nkind (Prefix (Def)) = N_Selected_Component then
3355 if Ekind (Entity (Selector_Name (Prefix (Def)))) /=
3356 E_Entry_Family
3357 then
3358 Error_Msg_N ("expect valid subprogram name as default", Def);
3359 end if;
3360
3361 else
3362 Error_Msg_N ("expect valid subprogram name as default", Def);
3363 goto Leave;
3364 end if;
3365
3366 elsif Nkind (Def) = N_Character_Literal then
3367
3368 -- Needs some type checks: subprogram should be parameterless???
3369
3370 Resolve (Def, (Etype (Nam)));
3371
3372 elsif not Is_Entity_Name (Def)
3373 or else not Is_Overloadable (Entity (Def))
3374 then
3375 Error_Msg_N ("expect valid subprogram name as default", Def);
3376 goto Leave;
3377
3378 elsif not Is_Overloaded (Def) then
3379 Subp := Entity (Def);
3380
3381 if Subp = Nam then
3382 Error_Msg_N ("premature usage of formal subprogram", Def);
3383
3384 elsif not Entity_Matches_Spec (Subp, Nam) then
3385 Error_Msg_N ("no visible entity matches specification", Def);
3386 end if;
3387
3388 -- More than one interpretation, so disambiguate as for a renaming
3389
3390 else
3391 declare
3392 I : Interp_Index;
3393 I1 : Interp_Index := 0;
3394 It : Interp;
3395 It1 : Interp;
3396
3397 begin
3398 Subp := Any_Id;
3399 Get_First_Interp (Def, I, It);
3400 while Present (It.Nam) loop
3401 if Entity_Matches_Spec (It.Nam, Nam) then
3402 if Subp /= Any_Id then
3403 It1 := Disambiguate (Def, I1, I, Etype (Subp));
3404
3405 if It1 = No_Interp then
3406 Error_Msg_N ("ambiguous default subprogram", Def);
3407 else
3408 Subp := It1.Nam;
3409 end if;
3410
3411 exit;
3412
3413 else
3414 I1 := I;
3415 Subp := It.Nam;
3416 end if;
3417 end if;
3418
3419 Get_Next_Interp (I, It);
3420 end loop;
3421 end;
3422
3423 if Subp /= Any_Id then
3424
3425 -- Subprogram found, generate reference to it
3426
3427 Set_Entity (Def, Subp);
3428 Generate_Reference (Subp, Def);
3429
3430 if Subp = Nam then
3431 Error_Msg_N ("premature usage of formal subprogram", Def);
3432
3433 elsif Ekind (Subp) /= E_Operator then
3434 Check_Mode_Conformant (Subp, Nam);
3435 end if;
3436
3437 else
3438 Error_Msg_N ("no visible subprogram matches specification", N);
3439 end if;
3440 end if;
3441 end if;
3442
3443 <<Leave>>
3444 if Has_Aspects (N) then
3445 Analyze_Aspect_Specifications (N, Nam);
3446 end if;
3447
3448 end Analyze_Formal_Subprogram_Declaration;
3449
3450 -------------------------------------
3451 -- Analyze_Formal_Type_Declaration --
3452 -------------------------------------
3453
3454 procedure Analyze_Formal_Type_Declaration (N : Node_Id) is
3455 Def : constant Node_Id := Formal_Type_Definition (N);
3456 T : Entity_Id;
3457
3458 begin
3459 T := Defining_Identifier (N);
3460
3461 if Present (Discriminant_Specifications (N))
3462 and then Nkind (Def) /= N_Formal_Private_Type_Definition
3463 then
3464 Error_Msg_N
3465 ("discriminants not allowed for this formal type", T);
3466 end if;
3467
3468 -- Enter the new name, and branch to specific routine
3469
3470 case Nkind (Def) is
3471 when N_Formal_Private_Type_Definition =>
3472 Analyze_Formal_Private_Type (N, T, Def);
3473
3474 when N_Formal_Derived_Type_Definition =>
3475 Analyze_Formal_Derived_Type (N, T, Def);
3476
3477 when N_Formal_Incomplete_Type_Definition =>
3478 Analyze_Formal_Incomplete_Type (T, Def);
3479
3480 when N_Formal_Discrete_Type_Definition =>
3481 Analyze_Formal_Discrete_Type (T, Def);
3482
3483 when N_Formal_Signed_Integer_Type_Definition =>
3484 Analyze_Formal_Signed_Integer_Type (T, Def);
3485
3486 when N_Formal_Modular_Type_Definition =>
3487 Analyze_Formal_Modular_Type (T, Def);
3488
3489 when N_Formal_Floating_Point_Definition =>
3490 Analyze_Formal_Floating_Type (T, Def);
3491
3492 when N_Formal_Ordinary_Fixed_Point_Definition =>
3493 Analyze_Formal_Ordinary_Fixed_Point_Type (T, Def);
3494
3495 when N_Formal_Decimal_Fixed_Point_Definition =>
3496 Analyze_Formal_Decimal_Fixed_Point_Type (T, Def);
3497
3498 when N_Array_Type_Definition =>
3499 Analyze_Formal_Array_Type (T, Def);
3500
3501 when N_Access_Function_Definition
3502 | N_Access_Procedure_Definition
3503 | N_Access_To_Object_Definition
3504 =>
3505 Analyze_Generic_Access_Type (T, Def);
3506
3507 -- Ada 2005: a interface declaration is encoded as an abstract
3508 -- record declaration or a abstract type derivation.
3509
3510 when N_Record_Definition =>
3511 Analyze_Formal_Interface_Type (N, T, Def);
3512
3513 when N_Derived_Type_Definition =>
3514 Analyze_Formal_Derived_Interface_Type (N, T, Def);
3515
3516 when N_Error =>
3517 null;
3518
3519 when others =>
3520 raise Program_Error;
3521 end case;
3522
3523 -- A formal type declaration declares a type and its first
3524 -- subtype.
3525
3526 Set_Is_Generic_Type (T);
3527 Set_Is_First_Subtype (T);
3528
3529 if Has_Aspects (N) then
3530 Analyze_Aspect_Specifications (N, T);
3531 end if;
3532 end Analyze_Formal_Type_Declaration;
3533
3534 ------------------------------------
3535 -- Analyze_Function_Instantiation --
3536 ------------------------------------
3537
3538 procedure Analyze_Function_Instantiation (N : Node_Id) is
3539 begin
3540 Analyze_Subprogram_Instantiation (N, E_Function);
3541 end Analyze_Function_Instantiation;
3542
3543 ---------------------------------
3544 -- Analyze_Generic_Access_Type --
3545 ---------------------------------
3546
3547 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id) is
3548 begin
3549 Enter_Name (T);
3550
3551 if Nkind (Def) = N_Access_To_Object_Definition then
3552 Access_Type_Declaration (T, Def);
3553
3554 if Is_Incomplete_Or_Private_Type (Designated_Type (T))
3555 and then No (Full_View (Designated_Type (T)))
3556 and then not Is_Generic_Type (Designated_Type (T))
3557 then
3558 Error_Msg_N ("premature usage of incomplete type", Def);
3559
3560 elsif not Is_Entity_Name (Subtype_Indication (Def)) then
3561 Error_Msg_N
3562 ("only a subtype mark is allowed in a formal", Def);
3563 end if;
3564
3565 else
3566 Access_Subprogram_Declaration (T, Def);
3567 end if;
3568 end Analyze_Generic_Access_Type;
3569
3570 ---------------------------------
3571 -- Analyze_Generic_Formal_Part --
3572 ---------------------------------
3573
3574 procedure Analyze_Generic_Formal_Part (N : Node_Id) is
3575 Gen_Parm_Decl : Node_Id;
3576
3577 begin
3578 -- The generic formals are processed in the scope of the generic unit,
3579 -- where they are immediately visible. The scope is installed by the
3580 -- caller.
3581
3582 Gen_Parm_Decl := First (Generic_Formal_Declarations (N));
3583 while Present (Gen_Parm_Decl) loop
3584 Analyze (Gen_Parm_Decl);
3585 Next (Gen_Parm_Decl);
3586 end loop;
3587
3588 Generate_Reference_To_Generic_Formals (Current_Scope);
3589
3590 -- For Ada 2020, some formal parameters can carry aspects, which must
3591 -- be name-resolved at the end of the list of formal parameters (which
3592 -- has the semantics of a declaration list).
3593
3594 Analyze_Contracts (Generic_Formal_Declarations (N));
3595 end Analyze_Generic_Formal_Part;
3596
3597 ------------------------------------------
3598 -- Analyze_Generic_Package_Declaration --
3599 ------------------------------------------
3600
3601 procedure Analyze_Generic_Package_Declaration (N : Node_Id) is
3602 Decls : constant List_Id := Visible_Declarations (Specification (N));
3603 Loc : constant Source_Ptr := Sloc (N);
3604
3605 Decl : Node_Id;
3606 Id : Entity_Id;
3607 New_N : Node_Id;
3608 Renaming : Node_Id;
3609 Save_Parent : Node_Id;
3610
3611 begin
3612 -- A generic may grant access to its private enclosing context depending
3613 -- on the placement of its corresponding body. From elaboration point of
3614 -- view, the flow of execution may enter this private context, and then
3615 -- reach an external unit, thus producing a dependency on that external
3616 -- unit. For such a path to be properly discovered and encoded in the
3617 -- ALI file of the main unit, let the ABE mechanism process the body of
3618 -- the main unit, and encode all relevant invocation constructs and the
3619 -- relations between them.
3620
3621 Mark_Save_Invocation_Graph_Of_Body;
3622
3623 -- We introduce a renaming of the enclosing package, to have a usable
3624 -- entity as the prefix of an expanded name for a local entity of the
3625 -- form Par.P.Q, where P is the generic package. This is because a local
3626 -- entity named P may hide it, so that the usual visibility rules in
3627 -- the instance will not resolve properly.
3628
3629 Renaming :=
3630 Make_Package_Renaming_Declaration (Loc,
3631 Defining_Unit_Name =>
3632 Make_Defining_Identifier (Loc,
3633 Chars => New_External_Name (Chars (Defining_Entity (N)), "GH")),
3634 Name =>
3635 Make_Identifier (Loc, Chars (Defining_Entity (N))));
3636
3637 -- The declaration is inserted before other declarations, but before
3638 -- pragmas that may be library-unit pragmas and must appear before other
3639 -- declarations. The pragma Compile_Time_Error is not in this class, and
3640 -- may contain an expression that includes such a qualified name, so the
3641 -- renaming declaration must appear before it.
3642
3643 -- Are there other pragmas that require this special handling ???
3644
3645 if Present (Decls) then
3646 Decl := First (Decls);
3647 while Present (Decl)
3648 and then Nkind (Decl) = N_Pragma
3649 and then Get_Pragma_Id (Decl) /= Pragma_Compile_Time_Error
3650 loop
3651 Next (Decl);
3652 end loop;
3653
3654 if Present (Decl) then
3655 Insert_Before (Decl, Renaming);
3656 else
3657 Append (Renaming, Visible_Declarations (Specification (N)));
3658 end if;
3659
3660 else
3661 Set_Visible_Declarations (Specification (N), New_List (Renaming));
3662 end if;
3663
3664 -- Create copy of generic unit, and save for instantiation. If the unit
3665 -- is a child unit, do not copy the specifications for the parent, which
3666 -- are not part of the generic tree.
3667
3668 Save_Parent := Parent_Spec (N);
3669 Set_Parent_Spec (N, Empty);
3670
3671 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
3672 Set_Parent_Spec (New_N, Save_Parent);
3673 Rewrite (N, New_N);
3674
3675 -- Once the contents of the generic copy and the template are swapped,
3676 -- do the same for their respective aspect specifications.
3677
3678 Exchange_Aspects (N, New_N);
3679
3680 -- Collect all contract-related source pragmas found within the template
3681 -- and attach them to the contract of the package spec. This contract is
3682 -- used in the capture of global references within annotations.
3683
3684 Create_Generic_Contract (N);
3685
3686 Id := Defining_Entity (N);
3687 Generate_Definition (Id);
3688
3689 -- Expansion is not applied to generic units
3690
3691 Start_Generic;
3692
3693 Enter_Name (Id);
3694 Set_Ekind (Id, E_Generic_Package);
3695 Set_Etype (Id, Standard_Void_Type);
3696
3697 -- Set SPARK_Mode from context
3698
3699 Set_SPARK_Pragma (Id, SPARK_Mode_Pragma);
3700 Set_SPARK_Aux_Pragma (Id, SPARK_Mode_Pragma);
3701 Set_SPARK_Pragma_Inherited (Id);
3702 Set_SPARK_Aux_Pragma_Inherited (Id);
3703
3704 -- Preserve relevant elaboration-related attributes of the context which
3705 -- are no longer available or very expensive to recompute once analysis,
3706 -- resolution, and expansion are over.
3707
3708 Mark_Elaboration_Attributes
3709 (N_Id => Id,
3710 Checks => True,
3711 Warnings => True);
3712
3713 -- Analyze aspects now, so that generated pragmas appear in the
3714 -- declarations before building and analyzing the generic copy.
3715
3716 if Has_Aspects (N) then
3717 Analyze_Aspect_Specifications (N, Id);
3718 end if;
3719
3720 Push_Scope (Id);
3721 Enter_Generic_Scope (Id);
3722 Set_Inner_Instances (Id, New_Elmt_List);
3723
3724 Set_Categorization_From_Pragmas (N);
3725 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3726
3727 -- Link the declaration of the generic homonym in the generic copy to
3728 -- the package it renames, so that it is always resolved properly.
3729
3730 Set_Generic_Homonym (Id, Defining_Unit_Name (Renaming));
3731 Set_Entity (Associated_Node (Name (Renaming)), Id);
3732
3733 -- For a library unit, we have reconstructed the entity for the unit,
3734 -- and must reset it in the library tables.
3735
3736 if Nkind (Parent (N)) = N_Compilation_Unit then
3737 Set_Cunit_Entity (Current_Sem_Unit, Id);
3738 end if;
3739
3740 Analyze_Generic_Formal_Part (N);
3741
3742 -- After processing the generic formals, analysis proceeds as for a
3743 -- non-generic package.
3744
3745 Analyze (Specification (N));
3746
3747 Validate_Categorization_Dependency (N, Id);
3748
3749 End_Generic;
3750
3751 End_Package_Scope (Id);
3752 Exit_Generic_Scope (Id);
3753
3754 -- If the generic appears within a package unit, the body of that unit
3755 -- has to be present for instantiation and inlining.
3756
3757 if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Declaration then
3758 Set_Body_Needed_For_Inlining
3759 (Defining_Entity (Unit (Cunit (Current_Sem_Unit))));
3760 end if;
3761
3762 if Nkind (Parent (N)) /= N_Compilation_Unit then
3763 Move_Freeze_Nodes (Id, N, Visible_Declarations (Specification (N)));
3764 Move_Freeze_Nodes (Id, N, Private_Declarations (Specification (N)));
3765 Move_Freeze_Nodes (Id, N, Generic_Formal_Declarations (N));
3766
3767 else
3768 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
3769 Validate_RT_RAT_Component (N);
3770
3771 -- If this is a spec without a body, check that generic parameters
3772 -- are referenced.
3773
3774 if not Body_Required (Parent (N)) then
3775 Check_References (Id);
3776 end if;
3777 end if;
3778
3779 -- If there is a specified storage pool in the context, create an
3780 -- aspect on the package declaration, so that it is used in any
3781 -- instance that does not override it.
3782
3783 if Present (Default_Pool) then
3784 declare
3785 ASN : Node_Id;
3786
3787 begin
3788 ASN :=
3789 Make_Aspect_Specification (Loc,
3790 Identifier => Make_Identifier (Loc, Name_Default_Storage_Pool),
3791 Expression => New_Copy (Default_Pool));
3792
3793 if No (Aspect_Specifications (Specification (N))) then
3794 Set_Aspect_Specifications (Specification (N), New_List (ASN));
3795 else
3796 Append (ASN, Aspect_Specifications (Specification (N)));
3797 end if;
3798 end;
3799 end if;
3800 end Analyze_Generic_Package_Declaration;
3801
3802 --------------------------------------------
3803 -- Analyze_Generic_Subprogram_Declaration --
3804 --------------------------------------------
3805
3806 procedure Analyze_Generic_Subprogram_Declaration (N : Node_Id) is
3807 Formals : List_Id;
3808 Id : Entity_Id;
3809 New_N : Node_Id;
3810 Result_Type : Entity_Id;
3811 Save_Parent : Node_Id;
3812 Spec : Node_Id;
3813 Typ : Entity_Id;
3814
3815 begin
3816 -- A generic may grant access to its private enclosing context depending
3817 -- on the placement of its corresponding body. From elaboration point of
3818 -- view, the flow of execution may enter this private context, and then
3819 -- reach an external unit, thus producing a dependency on that external
3820 -- unit. For such a path to be properly discovered and encoded in the
3821 -- ALI file of the main unit, let the ABE mechanism process the body of
3822 -- the main unit, and encode all relevant invocation constructs and the
3823 -- relations between them.
3824
3825 Mark_Save_Invocation_Graph_Of_Body;
3826
3827 -- Create copy of generic unit, and save for instantiation. If the unit
3828 -- is a child unit, do not copy the specifications for the parent, which
3829 -- are not part of the generic tree.
3830
3831 Save_Parent := Parent_Spec (N);
3832 Set_Parent_Spec (N, Empty);
3833
3834 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
3835 Set_Parent_Spec (New_N, Save_Parent);
3836 Rewrite (N, New_N);
3837
3838 -- Once the contents of the generic copy and the template are swapped,
3839 -- do the same for their respective aspect specifications.
3840
3841 Exchange_Aspects (N, New_N);
3842
3843 -- Collect all contract-related source pragmas found within the template
3844 -- and attach them to the contract of the subprogram spec. This contract
3845 -- is used in the capture of global references within annotations.
3846
3847 Create_Generic_Contract (N);
3848
3849 Spec := Specification (N);
3850 Id := Defining_Entity (Spec);
3851 Generate_Definition (Id);
3852
3853 if Nkind (Id) = N_Defining_Operator_Symbol then
3854 Error_Msg_N
3855 ("operator symbol not allowed for generic subprogram", Id);
3856 end if;
3857
3858 Start_Generic;
3859
3860 Enter_Name (Id);
3861 Set_Scope_Depth_Value (Id, Scope_Depth (Current_Scope) + 1);
3862
3863 -- Analyze the aspects of the generic copy to ensure that all generated
3864 -- pragmas (if any) perform their semantic effects.
3865
3866 if Has_Aspects (N) then
3867 Analyze_Aspect_Specifications (N, Id);
3868 end if;
3869
3870 Push_Scope (Id);
3871 Enter_Generic_Scope (Id);
3872 Set_Inner_Instances (Id, New_Elmt_List);
3873 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3874
3875 Analyze_Generic_Formal_Part (N);
3876
3877 if Nkind (Spec) = N_Function_Specification then
3878 Set_Ekind (Id, E_Generic_Function);
3879 else
3880 Set_Ekind (Id, E_Generic_Procedure);
3881 end if;
3882
3883 -- Set SPARK_Mode from context
3884
3885 Set_SPARK_Pragma (Id, SPARK_Mode_Pragma);
3886 Set_SPARK_Pragma_Inherited (Id);
3887
3888 -- Preserve relevant elaboration-related attributes of the context which
3889 -- are no longer available or very expensive to recompute once analysis,
3890 -- resolution, and expansion are over.
3891
3892 Mark_Elaboration_Attributes
3893 (N_Id => Id,
3894 Checks => True,
3895 Warnings => True);
3896
3897 Formals := Parameter_Specifications (Spec);
3898
3899 if Present (Formals) then
3900 Process_Formals (Formals, Spec);
3901 end if;
3902
3903 if Nkind (Spec) = N_Function_Specification then
3904 if Nkind (Result_Definition (Spec)) = N_Access_Definition then
3905 Result_Type := Access_Definition (Spec, Result_Definition (Spec));
3906 Set_Etype (Id, Result_Type);
3907
3908 -- Check restriction imposed by AI05-073: a generic function
3909 -- cannot return an abstract type or an access to such.
3910
3911 -- This is a binding interpretation should it apply to earlier
3912 -- versions of Ada as well as Ada 2012???
3913
3914 if Is_Abstract_Type (Designated_Type (Result_Type))
3915 and then Ada_Version >= Ada_2012
3916 then
3917 Error_Msg_N
3918 ("generic function cannot have an access result "
3919 & "that designates an abstract type", Spec);
3920 end if;
3921
3922 else
3923 Find_Type (Result_Definition (Spec));
3924 Typ := Entity (Result_Definition (Spec));
3925
3926 if Is_Abstract_Type (Typ)
3927 and then Ada_Version >= Ada_2012
3928 then
3929 Error_Msg_N
3930 ("generic function cannot have abstract result type", Spec);
3931 end if;
3932
3933 -- If a null exclusion is imposed on the result type, then create
3934 -- a null-excluding itype (an access subtype) and use it as the
3935 -- function's Etype.
3936
3937 if Is_Access_Type (Typ)
3938 and then Null_Exclusion_Present (Spec)
3939 then
3940 Set_Etype (Id,
3941 Create_Null_Excluding_Itype
3942 (T => Typ,
3943 Related_Nod => Spec,
3944 Scope_Id => Defining_Unit_Name (Spec)));
3945 else
3946 Set_Etype (Id, Typ);
3947 end if;
3948 end if;
3949
3950 else
3951 Set_Etype (Id, Standard_Void_Type);
3952 end if;
3953
3954 -- For a library unit, we have reconstructed the entity for the unit,
3955 -- and must reset it in the library tables. We also make sure that
3956 -- Body_Required is set properly in the original compilation unit node.
3957
3958 if Nkind (Parent (N)) = N_Compilation_Unit then
3959 Set_Cunit_Entity (Current_Sem_Unit, Id);
3960 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
3961 end if;
3962
3963 -- If the generic appears within a package unit, the body of that unit
3964 -- has to be present for instantiation and inlining.
3965
3966 if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Declaration
3967 and then Unit_Requires_Body (Id)
3968 then
3969 Set_Body_Needed_For_Inlining
3970 (Defining_Entity (Unit (Cunit (Current_Sem_Unit))));
3971 end if;
3972
3973 Set_Categorization_From_Pragmas (N);
3974 Validate_Categorization_Dependency (N, Id);
3975
3976 -- Capture all global references that occur within the profile of the
3977 -- generic subprogram. Aspects are not part of this processing because
3978 -- they must be delayed. If processed now, Save_Global_References will
3979 -- destroy the Associated_Node links and prevent the capture of global
3980 -- references when the contract of the generic subprogram is analyzed.
3981
3982 Save_Global_References (Original_Node (N));
3983
3984 End_Generic;
3985 End_Scope;
3986 Exit_Generic_Scope (Id);
3987 Generate_Reference_To_Formals (Id);
3988
3989 List_Inherited_Pre_Post_Aspects (Id);
3990 end Analyze_Generic_Subprogram_Declaration;
3991
3992 -----------------------------------
3993 -- Analyze_Package_Instantiation --
3994 -----------------------------------
3995
3996 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
3997 -- must be replaced by gotos which jump to the end of the routine in order
3998 -- to restore the Ghost and SPARK modes.
3999
4000 procedure Analyze_Package_Instantiation (N : Node_Id) is
4001 Has_Inline_Always : Boolean := False;
4002 -- Set if the generic unit contains any subprograms with Inline_Always.
4003 -- Only relevant when back-end inlining is not enabled.
4004
4005 function Might_Inline_Subp (Gen_Unit : Entity_Id) return Boolean;
4006 -- Return True if inlining is active and Gen_Unit contains inlined
4007 -- subprograms. In this case, we may either instantiate the body when
4008 -- front-end inlining is enabled, or add a pending instantiation when
4009 -- back-end inlining is enabled. In the former case, this may cause
4010 -- superfluous instantiations, but in either case we need to perform
4011 -- the instantiation of the body in the context of the instance and
4012 -- not in that of the point of inlining.
4013
4014 function Needs_Body_Instantiated (Gen_Unit : Entity_Id) return Boolean;
4015 -- Return True if Gen_Unit needs to have its body instantiated in the
4016 -- context of N. This in particular excludes generic contexts.
4017
4018 -----------------------
4019 -- Might_Inline_Subp --
4020 -----------------------
4021
4022 function Might_Inline_Subp (Gen_Unit : Entity_Id) return Boolean is
4023 E : Entity_Id;
4024
4025 begin
4026 if Inline_Processing_Required then
4027 -- No need to recompute the answer if we know it is positive
4028 -- and back-end inlining is enabled.
4029
4030 if Is_Inlined (Gen_Unit) and then Back_End_Inlining then
4031 return True;
4032 end if;
4033
4034 E := First_Entity (Gen_Unit);
4035 while Present (E) loop
4036 if Is_Subprogram (E) and then Is_Inlined (E) then
4037 -- Remember if there are any subprograms with Inline_Always
4038
4039 if Has_Pragma_Inline_Always (E) then
4040 Has_Inline_Always := True;
4041 end if;
4042
4043 Set_Is_Inlined (Gen_Unit);
4044 return True;
4045 end if;
4046
4047 Next_Entity (E);
4048 end loop;
4049 end if;
4050
4051 return False;
4052 end Might_Inline_Subp;
4053
4054 -------------------------------
4055 -- Needs_Body_Instantiated --
4056 -------------------------------
4057
4058 function Needs_Body_Instantiated (Gen_Unit : Entity_Id) return Boolean is
4059 begin
4060 -- No need to instantiate bodies in generic units
4061
4062 if Is_Generic_Unit (Cunit_Entity (Main_Unit)) then
4063 return False;
4064 end if;
4065
4066 -- If the instantiation is in the main unit, then the body is needed
4067
4068 if Is_In_Main_Unit (N) then
4069 return True;
4070 end if;
4071
4072 -- If not, then again no need to instantiate bodies in generic units
4073
4074 if Is_Generic_Unit (Cunit_Entity (Get_Code_Unit (N))) then
4075 return False;
4076 end if;
4077
4078 -- Here we have a special handling for back-end inlining: if inline
4079 -- processing is required, then we unconditionally want to have the
4080 -- body instantiated. The reason is that Might_Inline_Subp does not
4081 -- catch all the cases (as it does not recurse into nested packages)
4082 -- so this avoids the need to patch things up afterwards. Moreover,
4083 -- these instantiations are only performed on demand when back-end
4084 -- inlining is enabled, so this causes very little extra work.
4085
4086 if Inline_Processing_Required and then Back_End_Inlining then
4087 return True;
4088 end if;
4089
4090 -- We want to have the bodies instantiated in non-main units if
4091 -- they might contribute inlined subprograms.
4092
4093 return Might_Inline_Subp (Gen_Unit);
4094 end Needs_Body_Instantiated;
4095
4096 -- Local declarations
4097
4098 Gen_Id : constant Node_Id := Name (N);
4099 Inst_Id : constant Entity_Id := Defining_Entity (N);
4100 Is_Actual_Pack : constant Boolean := Is_Internal (Inst_Id);
4101 Loc : constant Source_Ptr := Sloc (N);
4102
4103 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
4104 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
4105 Saved_ISMP : constant Boolean :=
4106 Ignore_SPARK_Mode_Pragmas_In_Instance;
4107 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
4108 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
4109 -- Save the Ghost and SPARK mode-related data to restore on exit
4110
4111 Saved_Style_Check : constant Boolean := Style_Check;
4112 -- Save style check mode for restore on exit
4113
4114 Act_Decl : Node_Id;
4115 Act_Decl_Name : Node_Id;
4116 Act_Decl_Id : Entity_Id;
4117 Act_Spec : Node_Id;
4118 Act_Tree : Node_Id;
4119 Env_Installed : Boolean := False;
4120 Gen_Decl : Node_Id;
4121 Gen_Spec : Node_Id;
4122 Gen_Unit : Entity_Id;
4123 Inline_Now : Boolean := False;
4124 Needs_Body : Boolean;
4125 Parent_Installed : Boolean := False;
4126 Renaming_List : List_Id;
4127 Unit_Renaming : Node_Id;
4128
4129 Vis_Prims_List : Elist_Id := No_Elist;
4130 -- List of primitives made temporarily visible in the instantiation
4131 -- to match the visibility of the formal type
4132
4133 -- Start of processing for Analyze_Package_Instantiation
4134
4135 begin
4136 -- Preserve relevant elaboration-related attributes of the context which
4137 -- are no longer available or very expensive to recompute once analysis,
4138 -- resolution, and expansion are over.
4139
4140 Mark_Elaboration_Attributes
4141 (N_Id => N,
4142 Checks => True,
4143 Level => True,
4144 Modes => True,
4145 Warnings => True);
4146
4147 -- Very first thing: check for Text_IO special unit in case we are
4148 -- instantiating one of the children of [[Wide_]Wide_]Text_IO.
4149
4150 Check_Text_IO_Special_Unit (Name (N));
4151
4152 -- Make node global for error reporting
4153
4154 Instantiation_Node := N;
4155
4156 -- Case of instantiation of a generic package
4157
4158 if Nkind (N) = N_Package_Instantiation then
4159 Act_Decl_Id := New_Copy (Defining_Entity (N));
4160 Set_Comes_From_Source (Act_Decl_Id, True);
4161
4162 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name then
4163 Act_Decl_Name :=
4164 Make_Defining_Program_Unit_Name (Loc,
4165 Name =>
4166 New_Copy_Tree (Name (Defining_Unit_Name (N))),
4167 Defining_Identifier => Act_Decl_Id);
4168 else
4169 Act_Decl_Name := Act_Decl_Id;
4170 end if;
4171
4172 -- Case of instantiation of a formal package
4173
4174 else
4175 Act_Decl_Id := Defining_Identifier (N);
4176 Act_Decl_Name := Act_Decl_Id;
4177 end if;
4178
4179 Generate_Definition (Act_Decl_Id);
4180 Set_Ekind (Act_Decl_Id, E_Package);
4181
4182 -- Initialize list of incomplete actuals before analysis
4183
4184 Set_Incomplete_Actuals (Act_Decl_Id, New_Elmt_List);
4185
4186 Preanalyze_Actuals (N, Act_Decl_Id);
4187
4188 -- Turn off style checking in instances. If the check is enabled on the
4189 -- generic unit, a warning in an instance would just be noise. If not
4190 -- enabled on the generic, then a warning in an instance is just wrong.
4191 -- This must be done after analyzing the actuals, which do come from
4192 -- source and are subject to style checking.
4193
4194 Style_Check := False;
4195
4196 Init_Env;
4197 Env_Installed := True;
4198
4199 -- Reset renaming map for formal types. The mapping is established
4200 -- when analyzing the generic associations, but some mappings are
4201 -- inherited from formal packages of parent units, and these are
4202 -- constructed when the parents are installed.
4203
4204 Generic_Renamings.Set_Last (0);
4205 Generic_Renamings_HTable.Reset;
4206
4207 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
4208 Gen_Unit := Entity (Gen_Id);
4209
4210 -- A package instantiation is Ghost when it is subject to pragma Ghost
4211 -- or the generic template is Ghost. Set the mode now to ensure that
4212 -- any nodes generated during analysis and expansion are marked as
4213 -- Ghost.
4214
4215 Mark_And_Set_Ghost_Instantiation (N, Gen_Unit);
4216
4217 -- Verify that it is the name of a generic package
4218
4219 -- A visibility glitch: if the instance is a child unit and the generic
4220 -- is the generic unit of a parent instance (i.e. both the parent and
4221 -- the child units are instances of the same package) the name now
4222 -- denotes the renaming within the parent, not the intended generic
4223 -- unit. See if there is a homonym that is the desired generic. The
4224 -- renaming declaration must be visible inside the instance of the
4225 -- child, but not when analyzing the name in the instantiation itself.
4226
4227 if Ekind (Gen_Unit) = E_Package
4228 and then Present (Renamed_Entity (Gen_Unit))
4229 and then In_Open_Scopes (Renamed_Entity (Gen_Unit))
4230 and then Is_Generic_Instance (Renamed_Entity (Gen_Unit))
4231 and then Present (Homonym (Gen_Unit))
4232 then
4233 Gen_Unit := Homonym (Gen_Unit);
4234 end if;
4235
4236 if Etype (Gen_Unit) = Any_Type then
4237 Restore_Env;
4238 goto Leave;
4239
4240 elsif Ekind (Gen_Unit) /= E_Generic_Package then
4241
4242 -- Ada 2005 (AI-50217): Cannot use instance in limited with_clause
4243
4244 if From_Limited_With (Gen_Unit) then
4245 Error_Msg_N
4246 ("cannot instantiate a limited withed package", Gen_Id);
4247 else
4248 Error_Msg_NE
4249 ("& is not the name of a generic package", Gen_Id, Gen_Unit);
4250 end if;
4251
4252 Restore_Env;
4253 goto Leave;
4254 end if;
4255
4256 if In_Extended_Main_Source_Unit (N) then
4257 Set_Is_Instantiated (Gen_Unit);
4258 Generate_Reference (Gen_Unit, N);
4259
4260 if Present (Renamed_Object (Gen_Unit)) then
4261 Set_Is_Instantiated (Renamed_Object (Gen_Unit));
4262 Generate_Reference (Renamed_Object (Gen_Unit), N);
4263 end if;
4264 end if;
4265
4266 if Nkind (Gen_Id) = N_Identifier
4267 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
4268 then
4269 Error_Msg_NE
4270 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
4271
4272 elsif Nkind (Gen_Id) = N_Expanded_Name
4273 and then Is_Child_Unit (Gen_Unit)
4274 and then Nkind (Prefix (Gen_Id)) = N_Identifier
4275 and then Chars (Act_Decl_Id) = Chars (Prefix (Gen_Id))
4276 then
4277 Error_Msg_N
4278 ("& is hidden within declaration of instance ", Prefix (Gen_Id));
4279 end if;
4280
4281 Set_Entity (Gen_Id, Gen_Unit);
4282
4283 -- If generic is a renaming, get original generic unit
4284
4285 if Present (Renamed_Object (Gen_Unit))
4286 and then Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Package
4287 then
4288 Gen_Unit := Renamed_Object (Gen_Unit);
4289 end if;
4290
4291 -- Verify that there are no circular instantiations
4292
4293 if In_Open_Scopes (Gen_Unit) then
4294 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
4295 Restore_Env;
4296 goto Leave;
4297
4298 elsif Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
4299 Error_Msg_Node_2 := Current_Scope;
4300 Error_Msg_NE
4301 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
4302 Circularity_Detected := True;
4303 Restore_Env;
4304 goto Leave;
4305
4306 else
4307 Set_Ekind (Inst_Id, E_Package);
4308 Set_Scope (Inst_Id, Current_Scope);
4309
4310 -- If the context of the instance is subject to SPARK_Mode "off" or
4311 -- the annotation is altogether missing, set the global flag which
4312 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
4313 -- the instance.
4314
4315 if SPARK_Mode /= On then
4316 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
4317
4318 -- Mark the instance spec in case the body is instantiated at a
4319 -- later pass. This preserves the original context in effect for
4320 -- the body.
4321
4322 Set_Ignore_SPARK_Mode_Pragmas (Act_Decl_Id);
4323 end if;
4324
4325 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
4326 Gen_Spec := Specification (Gen_Decl);
4327
4328 -- Initialize renamings map, for error checking, and the list that
4329 -- holds private entities whose views have changed between generic
4330 -- definition and instantiation. If this is the instance created to
4331 -- validate an actual package, the instantiation environment is that
4332 -- of the enclosing instance.
4333
4334 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
4335
4336 -- Copy original generic tree, to produce text for instantiation
4337
4338 Act_Tree :=
4339 Copy_Generic_Node
4340 (Original_Node (Gen_Decl), Empty, Instantiating => True);
4341
4342 Act_Spec := Specification (Act_Tree);
4343
4344 -- If this is the instance created to validate an actual package,
4345 -- only the formals matter, do not examine the package spec itself.
4346
4347 if Is_Actual_Pack then
4348 Set_Visible_Declarations (Act_Spec, New_List);
4349 Set_Private_Declarations (Act_Spec, New_List);
4350 end if;
4351
4352 Renaming_List :=
4353 Analyze_Associations
4354 (I_Node => N,
4355 Formals => Generic_Formal_Declarations (Act_Tree),
4356 F_Copy => Generic_Formal_Declarations (Gen_Decl));
4357
4358 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
4359
4360 Set_Instance_Env (Gen_Unit, Act_Decl_Id);
4361 Set_Defining_Unit_Name (Act_Spec, Act_Decl_Name);
4362 Set_Is_Generic_Instance (Act_Decl_Id);
4363 Set_Generic_Parent (Act_Spec, Gen_Unit);
4364
4365 -- References to the generic in its own declaration or its body are
4366 -- references to the instance. Add a renaming declaration for the
4367 -- generic unit itself. This declaration, as well as the renaming
4368 -- declarations for the generic formals, must remain private to the
4369 -- unit: the formals, because this is the language semantics, and
4370 -- the unit because its use is an artifact of the implementation.
4371
4372 Unit_Renaming :=
4373 Make_Package_Renaming_Declaration (Loc,
4374 Defining_Unit_Name =>
4375 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
4376 Name => New_Occurrence_Of (Act_Decl_Id, Loc));
4377
4378 Append (Unit_Renaming, Renaming_List);
4379
4380 -- The renaming declarations are the first local declarations of the
4381 -- new unit.
4382
4383 if Is_Non_Empty_List (Visible_Declarations (Act_Spec)) then
4384 Insert_List_Before
4385 (First (Visible_Declarations (Act_Spec)), Renaming_List);
4386 else
4387 Set_Visible_Declarations (Act_Spec, Renaming_List);
4388 end if;
4389
4390 Act_Decl := Make_Package_Declaration (Loc, Specification => Act_Spec);
4391
4392 -- Propagate the aspect specifications from the package declaration
4393 -- template to the instantiated version of the package declaration.
4394
4395 if Has_Aspects (Act_Tree) then
4396 Set_Aspect_Specifications (Act_Decl,
4397 New_Copy_List_Tree (Aspect_Specifications (Act_Tree)));
4398 end if;
4399
4400 -- The generic may have a generated Default_Storage_Pool aspect,
4401 -- set at the point of generic declaration. If the instance has
4402 -- that aspect, it overrides the one inherited from the generic.
4403
4404 if Has_Aspects (Gen_Spec) then
4405 if No (Aspect_Specifications (N)) then
4406 Set_Aspect_Specifications (N,
4407 (New_Copy_List_Tree
4408 (Aspect_Specifications (Gen_Spec))));
4409
4410 else
4411 declare
4412 Inherited_Aspects : constant List_Id :=
4413 New_Copy_List_Tree
4414 (Aspect_Specifications (Gen_Spec));
4415
4416 ASN1 : Node_Id;
4417 ASN2 : Node_Id;
4418 Pool_Present : Boolean := False;
4419
4420 begin
4421 ASN1 := First (Aspect_Specifications (N));
4422 while Present (ASN1) loop
4423 if Chars (Identifier (ASN1)) =
4424 Name_Default_Storage_Pool
4425 then
4426 Pool_Present := True;
4427 exit;
4428 end if;
4429
4430 Next (ASN1);
4431 end loop;
4432
4433 if Pool_Present then
4434
4435 -- If generic carries a default storage pool, remove it
4436 -- in favor of the instance one.
4437
4438 ASN2 := First (Inherited_Aspects);
4439 while Present (ASN2) loop
4440 if Chars (Identifier (ASN2)) =
4441 Name_Default_Storage_Pool
4442 then
4443 Remove (ASN2);
4444 exit;
4445 end if;
4446
4447 Next (ASN2);
4448 end loop;
4449 end if;
4450
4451 Prepend_List_To
4452 (Aspect_Specifications (N), Inherited_Aspects);
4453 end;
4454 end if;
4455 end if;
4456
4457 -- Save the instantiation node for a subsequent instantiation of the
4458 -- body if there is one and it needs to be instantiated here.
4459
4460 -- We instantiate the body only if we are generating code, or if we
4461 -- are generating cross-reference information, or for GNATprove use.
4462
4463 declare
4464 Enclosing_Body_Present : Boolean := False;
4465 -- If the generic unit is not a compilation unit, then a body may
4466 -- be present in its parent even if none is required. We create a
4467 -- tentative pending instantiation for the body, which will be
4468 -- discarded if none is actually present.
4469
4470 Scop : Entity_Id;
4471
4472 begin
4473 if Scope (Gen_Unit) /= Standard_Standard
4474 and then not Is_Child_Unit (Gen_Unit)
4475 then
4476 Scop := Scope (Gen_Unit);
4477 while Present (Scop) and then Scop /= Standard_Standard loop
4478 if Unit_Requires_Body (Scop) then
4479 Enclosing_Body_Present := True;
4480 exit;
4481
4482 elsif In_Open_Scopes (Scop)
4483 and then In_Package_Body (Scop)
4484 then
4485 Enclosing_Body_Present := True;
4486 exit;
4487 end if;
4488
4489 exit when Is_Compilation_Unit (Scop);
4490 Scop := Scope (Scop);
4491 end loop;
4492 end if;
4493
4494 -- If front-end inlining is enabled or there are any subprograms
4495 -- marked with Inline_Always, and this is a unit for which code
4496 -- will be generated, we instantiate the body at once.
4497
4498 -- This is done if the instance is not the main unit, and if the
4499 -- generic is not a child unit of another generic, to avoid scope
4500 -- problems and the reinstallation of parent instances.
4501
4502 if Expander_Active
4503 and then (not Is_Child_Unit (Gen_Unit)
4504 or else not Is_Generic_Unit (Scope (Gen_Unit)))
4505 and then Might_Inline_Subp (Gen_Unit)
4506 and then not Is_Actual_Pack
4507 then
4508 if not Back_End_Inlining
4509 and then (Front_End_Inlining or else Has_Inline_Always)
4510 and then (Is_In_Main_Unit (N)
4511 or else In_Main_Context (Current_Scope))
4512 and then Nkind (Parent (N)) /= N_Compilation_Unit
4513 then
4514 Inline_Now := True;
4515
4516 -- In configurable_run_time mode we force the inlining of
4517 -- predefined subprograms marked Inline_Always, to minimize
4518 -- the use of the run-time library.
4519
4520 elsif In_Predefined_Unit (Gen_Decl)
4521 and then Configurable_Run_Time_Mode
4522 and then Nkind (Parent (N)) /= N_Compilation_Unit
4523 then
4524 Inline_Now := True;
4525 end if;
4526
4527 -- If the current scope is itself an instance within a child
4528 -- unit, there will be duplications in the scope stack, and the
4529 -- unstacking mechanism in Inline_Instance_Body will fail.
4530 -- This loses some rare cases of optimization, and might be
4531 -- improved some day, if we can find a proper abstraction for
4532 -- "the complete compilation context" that can be saved and
4533 -- restored. ???
4534
4535 if Is_Generic_Instance (Current_Scope) then
4536 declare
4537 Curr_Unit : constant Entity_Id :=
4538 Cunit_Entity (Current_Sem_Unit);
4539 begin
4540 if Curr_Unit /= Current_Scope
4541 and then Is_Child_Unit (Curr_Unit)
4542 then
4543 Inline_Now := False;
4544 end if;
4545 end;
4546 end if;
4547 end if;
4548
4549 Needs_Body :=
4550 (Unit_Requires_Body (Gen_Unit)
4551 or else Enclosing_Body_Present
4552 or else Present (Corresponding_Body (Gen_Decl)))
4553 and then Needs_Body_Instantiated (Gen_Unit)
4554 and then not Is_Actual_Pack
4555 and then not Inline_Now
4556 and then (Operating_Mode = Generate_Code
4557 or else (Operating_Mode = Check_Semantics
4558 and then GNATprove_Mode));
4559
4560 -- If front-end inlining is enabled or there are any subprograms
4561 -- marked with Inline_Always, do not instantiate body when within
4562 -- a generic context.
4563
4564 if not Back_End_Inlining
4565 and then (Front_End_Inlining or else Has_Inline_Always)
4566 and then not Expander_Active
4567 then
4568 Needs_Body := False;
4569 end if;
4570
4571 -- If the current context is generic, and the package being
4572 -- instantiated is declared within a formal package, there is no
4573 -- body to instantiate until the enclosing generic is instantiated
4574 -- and there is an actual for the formal package. If the formal
4575 -- package has parameters, we build a regular package instance for
4576 -- it, that precedes the original formal package declaration.
4577
4578 if In_Open_Scopes (Scope (Scope (Gen_Unit))) then
4579 declare
4580 Decl : constant Node_Id :=
4581 Original_Node
4582 (Unit_Declaration_Node (Scope (Gen_Unit)));
4583 begin
4584 if Nkind (Decl) = N_Formal_Package_Declaration
4585 or else (Nkind (Decl) = N_Package_Declaration
4586 and then Is_List_Member (Decl)
4587 and then Present (Next (Decl))
4588 and then
4589 Nkind (Next (Decl)) =
4590 N_Formal_Package_Declaration)
4591 then
4592 Needs_Body := False;
4593 end if;
4594 end;
4595 end if;
4596 end;
4597
4598 -- For RCI unit calling stubs, we omit the instance body if the
4599 -- instance is the RCI library unit itself.
4600
4601 -- However there is a special case for nested instances: in this case
4602 -- we do generate the instance body, as it might be required, e.g.
4603 -- because it provides stream attributes for some type used in the
4604 -- profile of a remote subprogram. This is consistent with 12.3(12),
4605 -- which indicates that the instance body occurs at the place of the
4606 -- instantiation, and thus is part of the RCI declaration, which is
4607 -- present on all client partitions (this is E.2.3(18)).
4608
4609 -- Note that AI12-0002 may make it illegal at some point to have
4610 -- stream attributes defined in an RCI unit, in which case this
4611 -- special case will become unnecessary. In the meantime, there
4612 -- is known application code in production that depends on this
4613 -- being possible, so we definitely cannot eliminate the body in
4614 -- the case of nested instances for the time being.
4615
4616 -- When we generate a nested instance body, calling stubs for any
4617 -- relevant subprogram will be inserted immediately after the
4618 -- subprogram declarations, and will take precedence over the
4619 -- subsequent (original) body. (The stub and original body will be
4620 -- complete homographs, but this is permitted in an instance).
4621 -- (Could we do better and remove the original body???)
4622
4623 if Distribution_Stub_Mode = Generate_Caller_Stub_Body
4624 and then Comes_From_Source (N)
4625 and then Nkind (Parent (N)) = N_Compilation_Unit
4626 then
4627 Needs_Body := False;
4628 end if;
4629
4630 if Needs_Body then
4631 -- Indicate that the enclosing scopes contain an instantiation,
4632 -- and that cleanup actions should be delayed until after the
4633 -- instance body is expanded.
4634
4635 Check_Forward_Instantiation (Gen_Decl);
4636 if Nkind (N) = N_Package_Instantiation then
4637 declare
4638 Enclosing_Master : Entity_Id;
4639
4640 begin
4641 -- Loop to search enclosing masters
4642
4643 Enclosing_Master := Current_Scope;
4644 Scope_Loop : while Enclosing_Master /= Standard_Standard loop
4645 if Ekind (Enclosing_Master) = E_Package then
4646 if Is_Compilation_Unit (Enclosing_Master) then
4647 if In_Package_Body (Enclosing_Master) then
4648 Set_Delay_Subprogram_Descriptors
4649 (Body_Entity (Enclosing_Master));
4650 else
4651 Set_Delay_Subprogram_Descriptors
4652 (Enclosing_Master);
4653 end if;
4654
4655 exit Scope_Loop;
4656
4657 else
4658 Enclosing_Master := Scope (Enclosing_Master);
4659 end if;
4660
4661 elsif Is_Generic_Unit (Enclosing_Master)
4662 or else Ekind (Enclosing_Master) = E_Void
4663 then
4664 -- Cleanup actions will eventually be performed on the
4665 -- enclosing subprogram or package instance, if any.
4666 -- Enclosing scope is void in the formal part of a
4667 -- generic subprogram.
4668
4669 exit Scope_Loop;
4670
4671 else
4672 if Ekind (Enclosing_Master) = E_Entry
4673 and then
4674 Ekind (Scope (Enclosing_Master)) = E_Protected_Type
4675 then
4676 if not Expander_Active then
4677 exit Scope_Loop;
4678 else
4679 Enclosing_Master :=
4680 Protected_Body_Subprogram (Enclosing_Master);
4681 end if;
4682 end if;
4683
4684 Set_Delay_Cleanups (Enclosing_Master);
4685
4686 while Ekind (Enclosing_Master) = E_Block loop
4687 Enclosing_Master := Scope (Enclosing_Master);
4688 end loop;
4689
4690 if Is_Subprogram (Enclosing_Master) then
4691 Set_Delay_Subprogram_Descriptors (Enclosing_Master);
4692
4693 elsif Is_Task_Type (Enclosing_Master) then
4694 declare
4695 TBP : constant Node_Id :=
4696 Get_Task_Body_Procedure
4697 (Enclosing_Master);
4698 begin
4699 if Present (TBP) then
4700 Set_Delay_Subprogram_Descriptors (TBP);
4701 Set_Delay_Cleanups (TBP);
4702 end if;
4703 end;
4704 end if;
4705
4706 exit Scope_Loop;
4707 end if;
4708 end loop Scope_Loop;
4709 end;
4710
4711 -- Make entry in table
4712
4713 Add_Pending_Instantiation (N, Act_Decl);
4714 end if;
4715 end if;
4716
4717 Set_Categorization_From_Pragmas (Act_Decl);
4718
4719 if Parent_Installed then
4720 Hide_Current_Scope;
4721 end if;
4722
4723 Set_Instance_Spec (N, Act_Decl);
4724
4725 -- If not a compilation unit, insert the package declaration before
4726 -- the original instantiation node.
4727
4728 if Nkind (Parent (N)) /= N_Compilation_Unit then
4729 Mark_Rewrite_Insertion (Act_Decl);
4730 Insert_Before (N, Act_Decl);
4731
4732 if Has_Aspects (N) then
4733 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4734
4735 -- The pragma created for a Default_Storage_Pool aspect must
4736 -- appear ahead of the declarations in the instance spec.
4737 -- Analysis has placed it after the instance node, so remove
4738 -- it and reinsert it properly now.
4739
4740 declare
4741 ASN : constant Node_Id := First (Aspect_Specifications (N));
4742 A_Name : constant Name_Id := Chars (Identifier (ASN));
4743 Decl : Node_Id;
4744
4745 begin
4746 if A_Name = Name_Default_Storage_Pool then
4747 if No (Visible_Declarations (Act_Spec)) then
4748 Set_Visible_Declarations (Act_Spec, New_List);
4749 end if;
4750
4751 Decl := Next (N);
4752 while Present (Decl) loop
4753 if Nkind (Decl) = N_Pragma then
4754 Remove (Decl);
4755 Prepend (Decl, Visible_Declarations (Act_Spec));
4756 exit;
4757 end if;
4758
4759 Next (Decl);
4760 end loop;
4761 end if;
4762 end;
4763 end if;
4764
4765 Analyze (Act_Decl);
4766
4767 -- For an instantiation that is a compilation unit, place
4768 -- declaration on current node so context is complete for analysis
4769 -- (including nested instantiations). If this is the main unit,
4770 -- the declaration eventually replaces the instantiation node.
4771 -- If the instance body is created later, it replaces the
4772 -- instance node, and the declaration is attached to it
4773 -- (see Build_Instance_Compilation_Unit_Nodes).
4774
4775 else
4776 if Cunit_Entity (Current_Sem_Unit) = Defining_Entity (N) then
4777
4778 -- The entity for the current unit is the newly created one,
4779 -- and all semantic information is attached to it.
4780
4781 Set_Cunit_Entity (Current_Sem_Unit, Act_Decl_Id);
4782
4783 -- If this is the main unit, replace the main entity as well
4784
4785 if Current_Sem_Unit = Main_Unit then
4786 Main_Unit_Entity := Act_Decl_Id;
4787 end if;
4788 end if;
4789
4790 Set_Unit (Parent (N), Act_Decl);
4791 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
4792 Set_Package_Instantiation (Act_Decl_Id, N);
4793
4794 -- Process aspect specifications of the instance node, if any, to
4795 -- take into account categorization pragmas before analyzing the
4796 -- instance.
4797
4798 if Has_Aspects (N) then
4799 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4800 end if;
4801
4802 Analyze (Act_Decl);
4803 Set_Unit (Parent (N), N);
4804 Set_Body_Required (Parent (N), False);
4805
4806 -- We never need elaboration checks on instantiations, since by
4807 -- definition, the body instantiation is elaborated at the same
4808 -- time as the spec instantiation.
4809
4810 if Legacy_Elaboration_Checks then
4811 Set_Kill_Elaboration_Checks (Act_Decl_Id);
4812 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
4813 end if;
4814 end if;
4815
4816 if Legacy_Elaboration_Checks then
4817 Check_Elab_Instantiation (N);
4818 end if;
4819
4820 -- Save the scenario for later examination by the ABE Processing
4821 -- phase.
4822
4823 Record_Elaboration_Scenario (N);
4824
4825 -- The instantiation results in a guaranteed ABE
4826
4827 if Is_Known_Guaranteed_ABE (N) and then Needs_Body then
4828 -- Do not instantiate the corresponding body because gigi cannot
4829 -- handle certain types of premature instantiations.
4830
4831 Remove_Dead_Instance (N);
4832
4833 -- Create completing bodies for all subprogram declarations since
4834 -- their real bodies will not be instantiated.
4835
4836 Provide_Completing_Bodies (Instance_Spec (N));
4837 end if;
4838
4839 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
4840
4841 Set_First_Private_Entity (Defining_Unit_Name (Unit_Renaming),
4842 First_Private_Entity (Act_Decl_Id));
4843
4844 -- If the instantiation will receive a body, the unit will be
4845 -- transformed into a package body, and receive its own elaboration
4846 -- entity. Otherwise, the nature of the unit is now a package
4847 -- declaration.
4848
4849 if Nkind (Parent (N)) = N_Compilation_Unit
4850 and then not Needs_Body
4851 then
4852 Rewrite (N, Act_Decl);
4853 end if;
4854
4855 if Present (Corresponding_Body (Gen_Decl))
4856 or else Unit_Requires_Body (Gen_Unit)
4857 then
4858 Set_Has_Completion (Act_Decl_Id);
4859 end if;
4860
4861 Check_Formal_Packages (Act_Decl_Id);
4862
4863 Restore_Hidden_Primitives (Vis_Prims_List);
4864 Restore_Private_Views (Act_Decl_Id);
4865
4866 Inherit_Context (Gen_Decl, N);
4867
4868 if Parent_Installed then
4869 Remove_Parent;
4870 end if;
4871
4872 Restore_Env;
4873 Env_Installed := False;
4874 end if;
4875
4876 Validate_Categorization_Dependency (N, Act_Decl_Id);
4877
4878 -- There used to be a check here to prevent instantiations in local
4879 -- contexts if the No_Local_Allocators restriction was active. This
4880 -- check was removed by a binding interpretation in AI-95-00130/07,
4881 -- but we retain the code for documentation purposes.
4882
4883 -- if Ekind (Act_Decl_Id) /= E_Void
4884 -- and then not Is_Library_Level_Entity (Act_Decl_Id)
4885 -- then
4886 -- Check_Restriction (No_Local_Allocators, N);
4887 -- end if;
4888
4889 if Inline_Now then
4890 Inline_Instance_Body (N, Gen_Unit, Act_Decl);
4891 end if;
4892
4893 -- Check that if N is an instantiation of System.Dim_Float_IO or
4894 -- System.Dim_Integer_IO, the formal type has a dimension system.
4895
4896 if Nkind (N) = N_Package_Instantiation
4897 and then Is_Dim_IO_Package_Instantiation (N)
4898 then
4899 declare
4900 Assoc : constant Node_Id := First (Generic_Associations (N));
4901 begin
4902 if not Has_Dimension_System
4903 (Etype (Explicit_Generic_Actual_Parameter (Assoc)))
4904 then
4905 Error_Msg_N ("type with a dimension system expected", Assoc);
4906 end if;
4907 end;
4908 end if;
4909
4910 <<Leave>>
4911 if Has_Aspects (N) and then Nkind (Parent (N)) /= N_Compilation_Unit then
4912 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4913 end if;
4914
4915 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
4916 Restore_Ghost_Region (Saved_GM, Saved_IGR);
4917 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
4918 Style_Check := Saved_Style_Check;
4919
4920 exception
4921 when Instantiation_Error =>
4922 if Parent_Installed then
4923 Remove_Parent;
4924 end if;
4925
4926 if Env_Installed then
4927 Restore_Env;
4928 end if;
4929
4930 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
4931 Restore_Ghost_Region (Saved_GM, Saved_IGR);
4932 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
4933 Style_Check := Saved_Style_Check;
4934 end Analyze_Package_Instantiation;
4935
4936 --------------------------
4937 -- Inline_Instance_Body --
4938 --------------------------
4939
4940 -- WARNING: This routine manages SPARK regions. Return statements must be
4941 -- replaced by gotos which jump to the end of the routine and restore the
4942 -- SPARK mode.
4943
4944 procedure Inline_Instance_Body
4945 (N : Node_Id;
4946 Gen_Unit : Entity_Id;
4947 Act_Decl : Node_Id)
4948 is
4949 Config_Attrs : constant Config_Switches_Type := Save_Config_Switches;
4950
4951 Curr_Comp : constant Node_Id := Cunit (Current_Sem_Unit);
4952 Curr_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
4953 Gen_Comp : constant Entity_Id :=
4954 Cunit_Entity (Get_Source_Unit (Gen_Unit));
4955
4956 Scope_Stack_Depth : constant Pos :=
4957 Scope_Stack.Last - Scope_Stack.First + 1;
4958
4959 Inner_Scopes : array (1 .. Scope_Stack_Depth) of Entity_Id;
4960 Instances : array (1 .. Scope_Stack_Depth) of Entity_Id;
4961 Use_Clauses : array (1 .. Scope_Stack_Depth) of Node_Id;
4962
4963 Curr_Scope : Entity_Id := Empty;
4964 List : Elist_Id := No_Elist; -- init to avoid warning
4965 N_Instances : Nat := 0;
4966 Num_Inner : Nat := 0;
4967 Num_Scopes : Nat := 0;
4968 Removed : Boolean := False;
4969 S : Entity_Id;
4970 Vis : Boolean;
4971
4972 begin
4973 -- Case of generic unit defined in another unit. We must remove the
4974 -- complete context of the current unit to install that of the generic.
4975
4976 if Gen_Comp /= Cunit_Entity (Current_Sem_Unit) then
4977
4978 -- Add some comments for the following two loops ???
4979
4980 S := Current_Scope;
4981 while Present (S) and then S /= Standard_Standard loop
4982 loop
4983 Num_Scopes := Num_Scopes + 1;
4984
4985 Use_Clauses (Num_Scopes) :=
4986 (Scope_Stack.Table
4987 (Scope_Stack.Last - Num_Scopes + 1).
4988 First_Use_Clause);
4989 End_Use_Clauses (Use_Clauses (Num_Scopes));
4990
4991 exit when Scope_Stack.Last - Num_Scopes + 1 = Scope_Stack.First
4992 or else Scope_Stack.Table
4993 (Scope_Stack.Last - Num_Scopes).Entity = Scope (S);
4994 end loop;
4995
4996 exit when Is_Generic_Instance (S)
4997 and then (In_Package_Body (S)
4998 or else Ekind (S) = E_Procedure
4999 or else Ekind (S) = E_Function);
5000 S := Scope (S);
5001 end loop;
5002
5003 Vis := Is_Immediately_Visible (Gen_Comp);
5004
5005 -- Find and save all enclosing instances
5006
5007 S := Current_Scope;
5008
5009 while Present (S)
5010 and then S /= Standard_Standard
5011 loop
5012 if Is_Generic_Instance (S) then
5013 N_Instances := N_Instances + 1;
5014 Instances (N_Instances) := S;
5015
5016 exit when In_Package_Body (S);
5017 end if;
5018
5019 S := Scope (S);
5020 end loop;
5021
5022 -- Remove context of current compilation unit, unless we are within a
5023 -- nested package instantiation, in which case the context has been
5024 -- removed previously.
5025
5026 -- If current scope is the body of a child unit, remove context of
5027 -- spec as well. If an enclosing scope is an instance body, the
5028 -- context has already been removed, but the entities in the body
5029 -- must be made invisible as well.
5030
5031 S := Current_Scope;
5032 while Present (S) and then S /= Standard_Standard loop
5033 if Is_Generic_Instance (S)
5034 and then (In_Package_Body (S)
5035 or else Ekind_In (S, E_Procedure, E_Function))
5036 then
5037 -- We still have to remove the entities of the enclosing
5038 -- instance from direct visibility.
5039
5040 declare
5041 E : Entity_Id;
5042 begin
5043 E := First_Entity (S);
5044 while Present (E) loop
5045 Set_Is_Immediately_Visible (E, False);
5046 Next_Entity (E);
5047 end loop;
5048 end;
5049
5050 exit;
5051 end if;
5052
5053 if S = Curr_Unit
5054 or else (Ekind (Curr_Unit) = E_Package_Body
5055 and then S = Spec_Entity (Curr_Unit))
5056 or else (Ekind (Curr_Unit) = E_Subprogram_Body
5057 and then S = Corresponding_Spec
5058 (Unit_Declaration_Node (Curr_Unit)))
5059 then
5060 Removed := True;
5061
5062 -- Remove entities in current scopes from visibility, so that
5063 -- instance body is compiled in a clean environment.
5064
5065 List := Save_Scope_Stack (Handle_Use => False);
5066
5067 if Is_Child_Unit (S) then
5068
5069 -- Remove child unit from stack, as well as inner scopes.
5070 -- Removing the context of a child unit removes parent units
5071 -- as well.
5072
5073 while Current_Scope /= S loop
5074 Num_Inner := Num_Inner + 1;
5075 Inner_Scopes (Num_Inner) := Current_Scope;
5076 Pop_Scope;
5077 end loop;
5078
5079 Pop_Scope;
5080 Remove_Context (Curr_Comp);
5081 Curr_Scope := S;
5082
5083 else
5084 Remove_Context (Curr_Comp);
5085 end if;
5086
5087 if Ekind (Curr_Unit) = E_Package_Body then
5088 Remove_Context (Library_Unit (Curr_Comp));
5089 end if;
5090 end if;
5091
5092 S := Scope (S);
5093 end loop;
5094
5095 pragma Assert (Num_Inner < Num_Scopes);
5096
5097 Push_Scope (Standard_Standard);
5098 Scope_Stack.Table (Scope_Stack.Last).Is_Active_Stack_Base := True;
5099
5100 -- The inlined package body is analyzed with the configuration state
5101 -- of the context prior to the scope manipulations performed above.
5102
5103 -- ??? shouldn't this also use the warning state of the context prior
5104 -- to the scope manipulations?
5105
5106 Instantiate_Package_Body
5107 (Body_Info =>
5108 ((Act_Decl => Act_Decl,
5109 Config_Switches => Config_Attrs,
5110 Current_Sem_Unit => Current_Sem_Unit,
5111 Expander_Status => Expander_Active,
5112 Inst_Node => N,
5113 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
5114 Scope_Suppress => Scope_Suppress,
5115 Warnings => Save_Warnings)),
5116 Inlined_Body => True);
5117
5118 Pop_Scope;
5119
5120 -- Restore context
5121
5122 Set_Is_Immediately_Visible (Gen_Comp, Vis);
5123
5124 -- Reset Generic_Instance flag so that use clauses can be installed
5125 -- in the proper order. (See Use_One_Package for effect of enclosing
5126 -- instances on processing of use clauses).
5127
5128 for J in 1 .. N_Instances loop
5129 Set_Is_Generic_Instance (Instances (J), False);
5130 end loop;
5131
5132 if Removed then
5133 Install_Context (Curr_Comp, Chain => False);
5134
5135 if Present (Curr_Scope)
5136 and then Is_Child_Unit (Curr_Scope)
5137 then
5138 Push_Scope (Curr_Scope);
5139 Set_Is_Immediately_Visible (Curr_Scope);
5140
5141 -- Finally, restore inner scopes as well
5142
5143 for J in reverse 1 .. Num_Inner loop
5144 Push_Scope (Inner_Scopes (J));
5145 end loop;
5146 end if;
5147
5148 Restore_Scope_Stack (List, Handle_Use => False);
5149
5150 if Present (Curr_Scope)
5151 and then
5152 (In_Private_Part (Curr_Scope)
5153 or else In_Package_Body (Curr_Scope))
5154 then
5155 -- Install private declaration of ancestor units, which are
5156 -- currently available. Restore_Scope_Stack and Install_Context
5157 -- only install the visible part of parents.
5158
5159 declare
5160 Par : Entity_Id;
5161 begin
5162 Par := Scope (Curr_Scope);
5163 while (Present (Par)) and then Par /= Standard_Standard loop
5164 Install_Private_Declarations (Par);
5165 Par := Scope (Par);
5166 end loop;
5167 end;
5168 end if;
5169 end if;
5170
5171 -- Restore use clauses. For a child unit, use clauses in the parents
5172 -- are restored when installing the context, so only those in inner
5173 -- scopes (and those local to the child unit itself) need to be
5174 -- installed explicitly.
5175
5176 if Is_Child_Unit (Curr_Unit) and then Removed then
5177 for J in reverse 1 .. Num_Inner + 1 loop
5178 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
5179 Use_Clauses (J);
5180 Install_Use_Clauses (Use_Clauses (J));
5181 end loop;
5182
5183 else
5184 for J in reverse 1 .. Num_Scopes loop
5185 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
5186 Use_Clauses (J);
5187 Install_Use_Clauses (Use_Clauses (J));
5188 end loop;
5189 end if;
5190
5191 -- Restore status of instances. If one of them is a body, make its
5192 -- local entities visible again.
5193
5194 declare
5195 E : Entity_Id;
5196 Inst : Entity_Id;
5197
5198 begin
5199 for J in 1 .. N_Instances loop
5200 Inst := Instances (J);
5201 Set_Is_Generic_Instance (Inst, True);
5202
5203 if In_Package_Body (Inst)
5204 or else Ekind_In (S, E_Procedure, E_Function)
5205 then
5206 E := First_Entity (Instances (J));
5207 while Present (E) loop
5208 Set_Is_Immediately_Visible (E);
5209 Next_Entity (E);
5210 end loop;
5211 end if;
5212 end loop;
5213 end;
5214
5215 -- If generic unit is in current unit, current context is correct. Note
5216 -- that the context is guaranteed to carry the correct SPARK_Mode as no
5217 -- enclosing scopes were removed.
5218
5219 else
5220 Instantiate_Package_Body
5221 (Body_Info =>
5222 ((Act_Decl => Act_Decl,
5223 Config_Switches => Save_Config_Switches,
5224 Current_Sem_Unit => Current_Sem_Unit,
5225 Expander_Status => Expander_Active,
5226 Inst_Node => N,
5227 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
5228 Scope_Suppress => Scope_Suppress,
5229 Warnings => Save_Warnings)),
5230 Inlined_Body => True);
5231 end if;
5232 end Inline_Instance_Body;
5233
5234 -------------------------------------
5235 -- Analyze_Procedure_Instantiation --
5236 -------------------------------------
5237
5238 procedure Analyze_Procedure_Instantiation (N : Node_Id) is
5239 begin
5240 Analyze_Subprogram_Instantiation (N, E_Procedure);
5241 end Analyze_Procedure_Instantiation;
5242
5243 -----------------------------------
5244 -- Need_Subprogram_Instance_Body --
5245 -----------------------------------
5246
5247 function Need_Subprogram_Instance_Body
5248 (N : Node_Id;
5249 Subp : Entity_Id) return Boolean
5250 is
5251 function Is_Inlined_Or_Child_Of_Inlined (E : Entity_Id) return Boolean;
5252 -- Return True if E is an inlined subprogram, an inlined renaming or a
5253 -- subprogram nested in an inlined subprogram. The inlining machinery
5254 -- totally disregards nested subprograms since it considers that they
5255 -- will always be compiled if the parent is (see Inline.Is_Nested).
5256
5257 ------------------------------------
5258 -- Is_Inlined_Or_Child_Of_Inlined --
5259 ------------------------------------
5260
5261 function Is_Inlined_Or_Child_Of_Inlined (E : Entity_Id) return Boolean is
5262 Scop : Entity_Id;
5263
5264 begin
5265 if Is_Inlined (E) or else Is_Inlined (Alias (E)) then
5266 return True;
5267 end if;
5268
5269 Scop := Scope (E);
5270 while Scop /= Standard_Standard loop
5271 if Ekind (Scop) in Subprogram_Kind and then Is_Inlined (Scop) then
5272 return True;
5273 end if;
5274
5275 Scop := Scope (Scop);
5276 end loop;
5277
5278 return False;
5279 end Is_Inlined_Or_Child_Of_Inlined;
5280
5281 begin
5282 -- Must be in the main unit or inlined (or child of inlined)
5283
5284 if (Is_In_Main_Unit (N) or else Is_Inlined_Or_Child_Of_Inlined (Subp))
5285
5286 -- Must be generating code or analyzing code in GNATprove mode
5287
5288 and then (Operating_Mode = Generate_Code
5289 or else (Operating_Mode = Check_Semantics
5290 and then GNATprove_Mode))
5291
5292 -- The body is needed when generating code (full expansion) and in
5293 -- in GNATprove mode (special expansion) for formal verification of
5294 -- the body itself.
5295
5296 and then (Expander_Active or GNATprove_Mode)
5297
5298 -- No point in inlining if ABE is inevitable
5299
5300 and then not Is_Known_Guaranteed_ABE (N)
5301
5302 -- Or if subprogram is eliminated
5303
5304 and then not Is_Eliminated (Subp)
5305 then
5306 Add_Pending_Instantiation (N, Unit_Declaration_Node (Subp));
5307 return True;
5308
5309 -- Here if not inlined, or we ignore the inlining
5310
5311 else
5312 return False;
5313 end if;
5314 end Need_Subprogram_Instance_Body;
5315
5316 --------------------------------------
5317 -- Analyze_Subprogram_Instantiation --
5318 --------------------------------------
5319
5320 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
5321 -- must be replaced by gotos which jump to the end of the routine in order
5322 -- to restore the Ghost and SPARK modes.
5323
5324 procedure Analyze_Subprogram_Instantiation
5325 (N : Node_Id;
5326 K : Entity_Kind)
5327 is
5328 Errs : constant Nat := Serious_Errors_Detected;
5329 Gen_Id : constant Node_Id := Name (N);
5330 Inst_Id : constant Entity_Id := Defining_Entity (N);
5331 Anon_Id : constant Entity_Id :=
5332 Make_Defining_Identifier (Sloc (Inst_Id),
5333 Chars => New_External_Name (Chars (Inst_Id), 'R'));
5334 Loc : constant Source_Ptr := Sloc (N);
5335
5336 Act_Decl_Id : Entity_Id := Empty; -- init to avoid warning
5337 Act_Decl : Node_Id;
5338 Act_Spec : Node_Id;
5339 Act_Tree : Node_Id;
5340
5341 Env_Installed : Boolean := False;
5342 Gen_Unit : Entity_Id;
5343 Gen_Decl : Node_Id;
5344 Pack_Id : Entity_Id;
5345 Parent_Installed : Boolean := False;
5346
5347 Renaming_List : List_Id;
5348 -- The list of declarations that link formals and actuals of the
5349 -- instance. These are subtype declarations for formal types, and
5350 -- renaming declarations for other formals. The subprogram declaration
5351 -- for the instance is then appended to the list, and the last item on
5352 -- the list is the renaming declaration for the instance.
5353
5354 procedure Analyze_Instance_And_Renamings;
5355 -- The instance must be analyzed in a context that includes the mappings
5356 -- of generic parameters into actuals. We create a package declaration
5357 -- for this purpose, and a subprogram with an internal name within the
5358 -- package. The subprogram instance is simply an alias for the internal
5359 -- subprogram, declared in the current scope.
5360
5361 procedure Build_Subprogram_Renaming;
5362 -- If the subprogram is recursive, there are occurrences of the name of
5363 -- the generic within the body, which must resolve to the current
5364 -- instance. We add a renaming declaration after the declaration, which
5365 -- is available in the instance body, as well as in the analysis of
5366 -- aspects that appear in the generic. This renaming declaration is
5367 -- inserted after the instance declaration which it renames.
5368
5369 ------------------------------------
5370 -- Analyze_Instance_And_Renamings --
5371 ------------------------------------
5372
5373 procedure Analyze_Instance_And_Renamings is
5374 Def_Ent : constant Entity_Id := Defining_Entity (N);
5375 Pack_Decl : Node_Id;
5376
5377 begin
5378 if Nkind (Parent (N)) = N_Compilation_Unit then
5379
5380 -- For the case of a compilation unit, the container package has
5381 -- the same name as the instantiation, to insure that the binder
5382 -- calls the elaboration procedure with the right name. Copy the
5383 -- entity of the instance, which may have compilation level flags
5384 -- (e.g. Is_Child_Unit) set.
5385
5386 Pack_Id := New_Copy (Def_Ent);
5387
5388 else
5389 -- Otherwise we use the name of the instantiation concatenated
5390 -- with its source position to ensure uniqueness if there are
5391 -- several instantiations with the same name.
5392
5393 Pack_Id :=
5394 Make_Defining_Identifier (Loc,
5395 Chars => New_External_Name
5396 (Related_Id => Chars (Def_Ent),
5397 Suffix => "GP",
5398 Suffix_Index => Source_Offset (Sloc (Def_Ent))));
5399 end if;
5400
5401 Pack_Decl :=
5402 Make_Package_Declaration (Loc,
5403 Specification => Make_Package_Specification (Loc,
5404 Defining_Unit_Name => Pack_Id,
5405 Visible_Declarations => Renaming_List,
5406 End_Label => Empty));
5407
5408 Set_Instance_Spec (N, Pack_Decl);
5409 Set_Is_Generic_Instance (Pack_Id);
5410 Set_Debug_Info_Needed (Pack_Id);
5411
5412 -- Case of not a compilation unit
5413
5414 if Nkind (Parent (N)) /= N_Compilation_Unit then
5415 Mark_Rewrite_Insertion (Pack_Decl);
5416 Insert_Before (N, Pack_Decl);
5417 Set_Has_Completion (Pack_Id);
5418
5419 -- Case of an instantiation that is a compilation unit
5420
5421 -- Place declaration on current node so context is complete for
5422 -- analysis (including nested instantiations), and for use in a
5423 -- context_clause (see Analyze_With_Clause).
5424
5425 else
5426 Set_Unit (Parent (N), Pack_Decl);
5427 Set_Parent_Spec (Pack_Decl, Parent_Spec (N));
5428 end if;
5429
5430 Analyze (Pack_Decl);
5431 Check_Formal_Packages (Pack_Id);
5432
5433 -- Body of the enclosing package is supplied when instantiating the
5434 -- subprogram body, after semantic analysis is completed.
5435
5436 if Nkind (Parent (N)) = N_Compilation_Unit then
5437
5438 -- Remove package itself from visibility, so it does not
5439 -- conflict with subprogram.
5440
5441 Set_Name_Entity_Id (Chars (Pack_Id), Homonym (Pack_Id));
5442
5443 -- Set name and scope of internal subprogram so that the proper
5444 -- external name will be generated. The proper scope is the scope
5445 -- of the wrapper package. We need to generate debugging info for
5446 -- the internal subprogram, so set flag accordingly.
5447
5448 Set_Chars (Anon_Id, Chars (Defining_Entity (N)));
5449 Set_Scope (Anon_Id, Scope (Pack_Id));
5450
5451 -- Mark wrapper package as referenced, to avoid spurious warnings
5452 -- if the instantiation appears in various with_ clauses of
5453 -- subunits of the main unit.
5454
5455 Set_Referenced (Pack_Id);
5456 end if;
5457
5458 Set_Is_Generic_Instance (Anon_Id);
5459 Set_Debug_Info_Needed (Anon_Id);
5460 Act_Decl_Id := New_Copy (Anon_Id);
5461
5462 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
5463 Set_Chars (Act_Decl_Id, Chars (Defining_Entity (N)));
5464 Set_Sloc (Act_Decl_Id, Sloc (Defining_Entity (N)));
5465
5466 -- Subprogram instance comes from source only if generic does
5467
5468 Preserve_Comes_From_Source (Act_Decl_Id, Gen_Unit);
5469
5470 -- If the instance is a child unit, mark the Id accordingly. Mark
5471 -- the anonymous entity as well, which is the real subprogram and
5472 -- which is used when the instance appears in a context clause.
5473 -- Similarly, propagate the Is_Eliminated flag to handle properly
5474 -- nested eliminated subprograms.
5475
5476 Set_Is_Child_Unit (Act_Decl_Id, Is_Child_Unit (Defining_Entity (N)));
5477 Set_Is_Child_Unit (Anon_Id, Is_Child_Unit (Defining_Entity (N)));
5478 New_Overloaded_Entity (Act_Decl_Id);
5479 Check_Eliminated (Act_Decl_Id);
5480 Set_Is_Eliminated (Anon_Id, Is_Eliminated (Act_Decl_Id));
5481
5482 if Nkind (Parent (N)) = N_Compilation_Unit then
5483
5484 -- In compilation unit case, kill elaboration checks on the
5485 -- instantiation, since they are never needed - the body is
5486 -- instantiated at the same point as the spec.
5487
5488 if Legacy_Elaboration_Checks then
5489 Set_Kill_Elaboration_Checks (Act_Decl_Id);
5490 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
5491 end if;
5492
5493 Set_Is_Compilation_Unit (Anon_Id);
5494 Set_Cunit_Entity (Current_Sem_Unit, Pack_Id);
5495 end if;
5496
5497 -- The instance is not a freezing point for the new subprogram.
5498 -- The anonymous subprogram may have a freeze node, created for
5499 -- some delayed aspects. This freeze node must not be inherited
5500 -- by the visible subprogram entity.
5501
5502 Set_Is_Frozen (Act_Decl_Id, False);
5503 Set_Freeze_Node (Act_Decl_Id, Empty);
5504
5505 if Nkind (Defining_Entity (N)) = N_Defining_Operator_Symbol then
5506 Valid_Operator_Definition (Act_Decl_Id);
5507 end if;
5508
5509 Set_Alias (Act_Decl_Id, Anon_Id);
5510 Set_Has_Completion (Act_Decl_Id);
5511 Set_Related_Instance (Pack_Id, Act_Decl_Id);
5512
5513 if Nkind (Parent (N)) = N_Compilation_Unit then
5514 Set_Body_Required (Parent (N), False);
5515 end if;
5516 end Analyze_Instance_And_Renamings;
5517
5518 -------------------------------
5519 -- Build_Subprogram_Renaming --
5520 -------------------------------
5521
5522 procedure Build_Subprogram_Renaming is
5523 Renaming_Decl : Node_Id;
5524 Unit_Renaming : Node_Id;
5525
5526 begin
5527 Unit_Renaming :=
5528 Make_Subprogram_Renaming_Declaration (Loc,
5529 Specification =>
5530 Copy_Generic_Node
5531 (Specification (Original_Node (Gen_Decl)),
5532 Empty,
5533 Instantiating => True),
5534 Name => New_Occurrence_Of (Anon_Id, Loc));
5535
5536 -- The generic may be a child unit. The renaming needs an identifier
5537 -- with the proper name.
5538
5539 Set_Defining_Unit_Name (Specification (Unit_Renaming),
5540 Make_Defining_Identifier (Loc, Chars (Gen_Unit)));
5541
5542 -- If there is a formal subprogram with the same name as the unit
5543 -- itself, do not add this renaming declaration, to prevent
5544 -- ambiguities when there is a call with that name in the body.
5545 -- This is a partial and ugly fix for one ACATS test. ???
5546
5547 Renaming_Decl := First (Renaming_List);
5548 while Present (Renaming_Decl) loop
5549 if Nkind (Renaming_Decl) = N_Subprogram_Renaming_Declaration
5550 and then
5551 Chars (Defining_Entity (Renaming_Decl)) = Chars (Gen_Unit)
5552 then
5553 exit;
5554 end if;
5555
5556 Next (Renaming_Decl);
5557 end loop;
5558
5559 if No (Renaming_Decl) then
5560 Append (Unit_Renaming, Renaming_List);
5561 end if;
5562 end Build_Subprogram_Renaming;
5563
5564 -- Local variables
5565
5566 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
5567 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
5568 Saved_ISMP : constant Boolean :=
5569 Ignore_SPARK_Mode_Pragmas_In_Instance;
5570 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
5571 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
5572 -- Save the Ghost and SPARK mode-related data to restore on exit
5573
5574 Vis_Prims_List : Elist_Id := No_Elist;
5575 -- List of primitives made temporarily visible in the instantiation
5576 -- to match the visibility of the formal type
5577
5578 -- Start of processing for Analyze_Subprogram_Instantiation
5579
5580 begin
5581 -- Preserve relevant elaboration-related attributes of the context which
5582 -- are no longer available or very expensive to recompute once analysis,
5583 -- resolution, and expansion are over.
5584
5585 Mark_Elaboration_Attributes
5586 (N_Id => N,
5587 Checks => True,
5588 Level => True,
5589 Modes => True,
5590 Warnings => True);
5591
5592 -- Very first thing: check for special Text_IO unit in case we are
5593 -- instantiating one of the children of [[Wide_]Wide_]Text_IO. Of course
5594 -- such an instantiation is bogus (these are packages, not subprograms),
5595 -- but we get a better error message if we do this.
5596
5597 Check_Text_IO_Special_Unit (Gen_Id);
5598
5599 -- Make node global for error reporting
5600
5601 Instantiation_Node := N;
5602
5603 -- For package instantiations we turn off style checks, because they
5604 -- will have been emitted in the generic. For subprogram instantiations
5605 -- we want to apply at least the check on overriding indicators so we
5606 -- do not modify the style check status.
5607
5608 -- The renaming declarations for the actuals do not come from source and
5609 -- will not generate spurious warnings.
5610
5611 Preanalyze_Actuals (N);
5612
5613 Init_Env;
5614 Env_Installed := True;
5615 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
5616 Gen_Unit := Entity (Gen_Id);
5617
5618 -- A subprogram instantiation is Ghost when it is subject to pragma
5619 -- Ghost or the generic template is Ghost. Set the mode now to ensure
5620 -- that any nodes generated during analysis and expansion are marked as
5621 -- Ghost.
5622
5623 Mark_And_Set_Ghost_Instantiation (N, Gen_Unit);
5624
5625 Generate_Reference (Gen_Unit, Gen_Id);
5626
5627 if Nkind (Gen_Id) = N_Identifier
5628 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
5629 then
5630 Error_Msg_NE
5631 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
5632 end if;
5633
5634 if Etype (Gen_Unit) = Any_Type then
5635 Restore_Env;
5636 goto Leave;
5637 end if;
5638
5639 -- Verify that it is a generic subprogram of the right kind, and that
5640 -- it does not lead to a circular instantiation.
5641
5642 if K = E_Procedure and then Ekind (Gen_Unit) /= E_Generic_Procedure then
5643 Error_Msg_NE
5644 ("& is not the name of a generic procedure", Gen_Id, Gen_Unit);
5645
5646 elsif K = E_Function and then Ekind (Gen_Unit) /= E_Generic_Function then
5647 Error_Msg_NE
5648 ("& is not the name of a generic function", Gen_Id, Gen_Unit);
5649
5650 elsif In_Open_Scopes (Gen_Unit) then
5651 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
5652
5653 else
5654 Set_Ekind (Inst_Id, K);
5655 Set_Scope (Inst_Id, Current_Scope);
5656
5657 Set_Entity (Gen_Id, Gen_Unit);
5658 Set_Is_Instantiated (Gen_Unit);
5659
5660 if In_Extended_Main_Source_Unit (N) then
5661 Generate_Reference (Gen_Unit, N);
5662 end if;
5663
5664 -- If renaming, get original unit
5665
5666 if Present (Renamed_Object (Gen_Unit))
5667 and then Ekind_In (Renamed_Object (Gen_Unit), E_Generic_Procedure,
5668 E_Generic_Function)
5669 then
5670 Gen_Unit := Renamed_Object (Gen_Unit);
5671 Set_Is_Instantiated (Gen_Unit);
5672 Generate_Reference (Gen_Unit, N);
5673 end if;
5674
5675 if Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
5676 Error_Msg_Node_2 := Current_Scope;
5677 Error_Msg_NE
5678 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
5679 Circularity_Detected := True;
5680 Restore_Hidden_Primitives (Vis_Prims_List);
5681 goto Leave;
5682 end if;
5683
5684 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
5685
5686 -- Initialize renamings map, for error checking
5687
5688 Generic_Renamings.Set_Last (0);
5689 Generic_Renamings_HTable.Reset;
5690
5691 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
5692
5693 -- Copy original generic tree, to produce text for instantiation
5694
5695 Act_Tree :=
5696 Copy_Generic_Node
5697 (Original_Node (Gen_Decl), Empty, Instantiating => True);
5698
5699 -- Inherit overriding indicator from instance node
5700
5701 Act_Spec := Specification (Act_Tree);
5702 Set_Must_Override (Act_Spec, Must_Override (N));
5703 Set_Must_Not_Override (Act_Spec, Must_Not_Override (N));
5704
5705 Renaming_List :=
5706 Analyze_Associations
5707 (I_Node => N,
5708 Formals => Generic_Formal_Declarations (Act_Tree),
5709 F_Copy => Generic_Formal_Declarations (Gen_Decl));
5710
5711 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
5712
5713 -- The subprogram itself cannot contain a nested instance, so the
5714 -- current parent is left empty.
5715
5716 Set_Instance_Env (Gen_Unit, Empty);
5717
5718 -- Build the subprogram declaration, which does not appear in the
5719 -- generic template, and give it a sloc consistent with that of the
5720 -- template.
5721
5722 Set_Defining_Unit_Name (Act_Spec, Anon_Id);
5723 Set_Generic_Parent (Act_Spec, Gen_Unit);
5724 Act_Decl :=
5725 Make_Subprogram_Declaration (Sloc (Act_Spec),
5726 Specification => Act_Spec);
5727
5728 -- The aspects have been copied previously, but they have to be
5729 -- linked explicitly to the new subprogram declaration. Explicit
5730 -- pre/postconditions on the instance are analyzed below, in a
5731 -- separate step.
5732
5733 Move_Aspects (Act_Tree, To => Act_Decl);
5734 Set_Categorization_From_Pragmas (Act_Decl);
5735
5736 if Parent_Installed then
5737 Hide_Current_Scope;
5738 end if;
5739
5740 Append (Act_Decl, Renaming_List);
5741
5742 -- Contract-related source pragmas that follow a generic subprogram
5743 -- must be instantiated explicitly because they are not part of the
5744 -- subprogram template.
5745
5746 Instantiate_Subprogram_Contract
5747 (Original_Node (Gen_Decl), Renaming_List);
5748
5749 Build_Subprogram_Renaming;
5750
5751 -- If the context of the instance is subject to SPARK_Mode "off" or
5752 -- the annotation is altogether missing, set the global flag which
5753 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
5754 -- the instance. This should be done prior to analyzing the instance.
5755
5756 if SPARK_Mode /= On then
5757 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
5758 end if;
5759
5760 -- If the context of an instance is not subject to SPARK_Mode "off",
5761 -- and the generic spec is subject to an explicit SPARK_Mode pragma,
5762 -- the latter should be the one applicable to the instance.
5763
5764 if not Ignore_SPARK_Mode_Pragmas_In_Instance
5765 and then Saved_SM /= Off
5766 and then Present (SPARK_Pragma (Gen_Unit))
5767 then
5768 Set_SPARK_Mode (Gen_Unit);
5769 end if;
5770
5771 Analyze_Instance_And_Renamings;
5772
5773 -- Restore SPARK_Mode from the context after analysis of the package
5774 -- declaration, so that the SPARK_Mode on the generic spec does not
5775 -- apply to the pending instance for the instance body.
5776
5777 if not Ignore_SPARK_Mode_Pragmas_In_Instance
5778 and then Saved_SM /= Off
5779 and then Present (SPARK_Pragma (Gen_Unit))
5780 then
5781 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
5782 end if;
5783
5784 -- If the generic is marked Import (Intrinsic), then so is the
5785 -- instance. This indicates that there is no body to instantiate. If
5786 -- generic is marked inline, so it the instance, and the anonymous
5787 -- subprogram it renames. If inlined, or else if inlining is enabled
5788 -- for the compilation, we generate the instance body even if it is
5789 -- not within the main unit.
5790
5791 if Is_Intrinsic_Subprogram (Gen_Unit) then
5792 Set_Is_Intrinsic_Subprogram (Anon_Id);
5793 Set_Is_Intrinsic_Subprogram (Act_Decl_Id);
5794
5795 if Chars (Gen_Unit) = Name_Unchecked_Conversion then
5796 Validate_Unchecked_Conversion (N, Act_Decl_Id);
5797 end if;
5798 end if;
5799
5800 -- Inherit convention from generic unit. Intrinsic convention, as for
5801 -- an instance of unchecked conversion, is not inherited because an
5802 -- explicit Ada instance has been created.
5803
5804 if Has_Convention_Pragma (Gen_Unit)
5805 and then Convention (Gen_Unit) /= Convention_Intrinsic
5806 then
5807 Set_Convention (Act_Decl_Id, Convention (Gen_Unit));
5808 Set_Is_Exported (Act_Decl_Id, Is_Exported (Gen_Unit));
5809 end if;
5810
5811 Generate_Definition (Act_Decl_Id);
5812
5813 -- Inherit all inlining-related flags which apply to the generic in
5814 -- the subprogram and its declaration.
5815
5816 Set_Is_Inlined (Act_Decl_Id, Is_Inlined (Gen_Unit));
5817 Set_Is_Inlined (Anon_Id, Is_Inlined (Gen_Unit));
5818
5819 Set_Has_Pragma_Inline (Act_Decl_Id, Has_Pragma_Inline (Gen_Unit));
5820 Set_Has_Pragma_Inline (Anon_Id, Has_Pragma_Inline (Gen_Unit));
5821
5822 Set_Has_Pragma_Inline_Always
5823 (Act_Decl_Id, Has_Pragma_Inline_Always (Gen_Unit));
5824 Set_Has_Pragma_Inline_Always
5825 (Anon_Id, Has_Pragma_Inline_Always (Gen_Unit));
5826
5827 Set_Has_Pragma_No_Inline
5828 (Act_Decl_Id, Has_Pragma_No_Inline (Gen_Unit));
5829 Set_Has_Pragma_No_Inline
5830 (Anon_Id, Has_Pragma_No_Inline (Gen_Unit));
5831
5832 -- Propagate No_Return if pragma applied to generic unit. This must
5833 -- be done explicitly because pragma does not appear in generic
5834 -- declaration (unlike the aspect case).
5835
5836 if No_Return (Gen_Unit) then
5837 Set_No_Return (Act_Decl_Id);
5838 Set_No_Return (Anon_Id);
5839 end if;
5840
5841 -- Mark both the instance spec and the anonymous package in case the
5842 -- body is instantiated at a later pass. This preserves the original
5843 -- context in effect for the body.
5844
5845 if SPARK_Mode /= On then
5846 Set_Ignore_SPARK_Mode_Pragmas (Act_Decl_Id);
5847 Set_Ignore_SPARK_Mode_Pragmas (Anon_Id);
5848 end if;
5849
5850 if Legacy_Elaboration_Checks
5851 and then not Is_Intrinsic_Subprogram (Gen_Unit)
5852 then
5853 Check_Elab_Instantiation (N);
5854 end if;
5855
5856 -- Save the scenario for later examination by the ABE Processing
5857 -- phase.
5858
5859 Record_Elaboration_Scenario (N);
5860
5861 -- The instantiation results in a guaranteed ABE. Create a completing
5862 -- body for the subprogram declaration because the real body will not
5863 -- be instantiated.
5864
5865 if Is_Known_Guaranteed_ABE (N) then
5866 Provide_Completing_Bodies (Instance_Spec (N));
5867 end if;
5868
5869 if Is_Dispatching_Operation (Act_Decl_Id)
5870 and then Ada_Version >= Ada_2005
5871 then
5872 declare
5873 Formal : Entity_Id;
5874
5875 begin
5876 Formal := First_Formal (Act_Decl_Id);
5877 while Present (Formal) loop
5878 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type
5879 and then Is_Controlling_Formal (Formal)
5880 and then not Can_Never_Be_Null (Formal)
5881 then
5882 Error_Msg_NE
5883 ("access parameter& is controlling,", N, Formal);
5884 Error_Msg_NE
5885 ("\corresponding parameter of & must be explicitly "
5886 & "null-excluding", N, Gen_Id);
5887 end if;
5888
5889 Next_Formal (Formal);
5890 end loop;
5891 end;
5892 end if;
5893
5894 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
5895
5896 Validate_Categorization_Dependency (N, Act_Decl_Id);
5897
5898 if not Is_Intrinsic_Subprogram (Act_Decl_Id) then
5899 Inherit_Context (Gen_Decl, N);
5900
5901 Restore_Private_Views (Pack_Id, False);
5902
5903 -- If the context requires a full instantiation, mark node for
5904 -- subsequent construction of the body.
5905
5906 if Need_Subprogram_Instance_Body (N, Act_Decl_Id) then
5907 Check_Forward_Instantiation (Gen_Decl);
5908
5909 -- The wrapper package is always delayed, because it does not
5910 -- constitute a freeze point, but to insure that the freeze node
5911 -- is placed properly, it is created directly when instantiating
5912 -- the body (otherwise the freeze node might appear to early for
5913 -- nested instantiations).
5914
5915 elsif Nkind (Parent (N)) = N_Compilation_Unit then
5916 Rewrite (N, Unit (Parent (N)));
5917 Set_Unit (Parent (N), N);
5918 end if;
5919
5920 -- Replace instance node for library-level instantiations of
5921 -- intrinsic subprograms.
5922
5923 elsif Nkind (Parent (N)) = N_Compilation_Unit then
5924 Rewrite (N, Unit (Parent (N)));
5925 Set_Unit (Parent (N), N);
5926 end if;
5927
5928 if Parent_Installed then
5929 Remove_Parent;
5930 end if;
5931
5932 Restore_Hidden_Primitives (Vis_Prims_List);
5933 Restore_Env;
5934 Env_Installed := False;
5935 Generic_Renamings.Set_Last (0);
5936 Generic_Renamings_HTable.Reset;
5937 end if;
5938
5939 <<Leave>>
5940 -- Analyze aspects in declaration if no errors appear in the instance.
5941
5942 if Has_Aspects (N) and then Serious_Errors_Detected = Errs then
5943 Analyze_Aspect_Specifications (N, Act_Decl_Id);
5944 end if;
5945
5946 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
5947 Restore_Ghost_Region (Saved_GM, Saved_IGR);
5948 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
5949
5950 exception
5951 when Instantiation_Error =>
5952 if Parent_Installed then
5953 Remove_Parent;
5954 end if;
5955
5956 if Env_Installed then
5957 Restore_Env;
5958 end if;
5959
5960 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
5961 Restore_Ghost_Region (Saved_GM, Saved_IGR);
5962 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
5963 end Analyze_Subprogram_Instantiation;
5964
5965 -------------------------
5966 -- Get_Associated_Node --
5967 -------------------------
5968
5969 function Get_Associated_Node (N : Node_Id) return Node_Id is
5970 Assoc : Node_Id;
5971
5972 begin
5973 Assoc := Associated_Node (N);
5974
5975 if Nkind (Assoc) /= Nkind (N) then
5976 return Assoc;
5977
5978 elsif Nkind_In (Assoc, N_Aggregate, N_Extension_Aggregate) then
5979 return Assoc;
5980
5981 else
5982 -- If the node is part of an inner generic, it may itself have been
5983 -- remapped into a further generic copy. Associated_Node is otherwise
5984 -- used for the entity of the node, and will be of a different node
5985 -- kind, or else N has been rewritten as a literal or function call.
5986
5987 while Present (Associated_Node (Assoc))
5988 and then Nkind (Associated_Node (Assoc)) = Nkind (Assoc)
5989 loop
5990 Assoc := Associated_Node (Assoc);
5991 end loop;
5992
5993 -- Follow an additional link in case the final node was rewritten.
5994 -- This can only happen with nested generic units.
5995
5996 if (Nkind (Assoc) = N_Identifier or else Nkind (Assoc) in N_Op)
5997 and then Present (Associated_Node (Assoc))
5998 and then (Nkind_In (Associated_Node (Assoc), N_Function_Call,
5999 N_Explicit_Dereference,
6000 N_Integer_Literal,
6001 N_Real_Literal,
6002 N_String_Literal))
6003 then
6004 Assoc := Associated_Node (Assoc);
6005 end if;
6006
6007 -- An additional special case: an unconstrained type in an object
6008 -- declaration may have been rewritten as a local subtype constrained
6009 -- by the expression in the declaration. We need to recover the
6010 -- original entity, which may be global.
6011
6012 if Present (Original_Node (Assoc))
6013 and then Nkind (Parent (N)) = N_Object_Declaration
6014 then
6015 Assoc := Original_Node (Assoc);
6016 end if;
6017
6018 return Assoc;
6019 end if;
6020 end Get_Associated_Node;
6021
6022 ----------------------------
6023 -- Build_Function_Wrapper --
6024 ----------------------------
6025
6026 function Build_Function_Wrapper
6027 (Formal_Subp : Entity_Id;
6028 Actual_Subp : Entity_Id) return Node_Id
6029 is
6030 Loc : constant Source_Ptr := Sloc (Current_Scope);
6031 Ret_Type : constant Entity_Id := Get_Instance_Of (Etype (Formal_Subp));
6032 Actuals : List_Id;
6033 Decl : Node_Id;
6034 Func_Name : Node_Id;
6035 Func : Entity_Id;
6036 Parm_Type : Node_Id;
6037 Profile : List_Id := New_List;
6038 Spec : Node_Id;
6039 Act_F : Entity_Id;
6040 Form_F : Entity_Id;
6041 New_F : Entity_Id;
6042
6043 begin
6044 Func_Name := New_Occurrence_Of (Actual_Subp, Loc);
6045
6046 Func := Make_Defining_Identifier (Loc, Chars (Formal_Subp));
6047 Set_Ekind (Func, E_Function);
6048 Set_Is_Generic_Actual_Subprogram (Func);
6049
6050 Actuals := New_List;
6051 Profile := New_List;
6052
6053 Act_F := First_Formal (Actual_Subp);
6054 Form_F := First_Formal (Formal_Subp);
6055 while Present (Form_F) loop
6056
6057 -- Create new formal for profile of wrapper, and add a reference
6058 -- to it in the list of actuals for the enclosing call. The name
6059 -- must be that of the formal in the formal subprogram, because
6060 -- calls to it in the generic body may use named associations.
6061
6062 New_F := Make_Defining_Identifier (Loc, Chars (Form_F));
6063
6064 Parm_Type :=
6065 New_Occurrence_Of (Get_Instance_Of (Etype (Form_F)), Loc);
6066
6067 Append_To (Profile,
6068 Make_Parameter_Specification (Loc,
6069 Defining_Identifier => New_F,
6070 Parameter_Type => Parm_Type));
6071
6072 Append_To (Actuals, New_Occurrence_Of (New_F, Loc));
6073 Next_Formal (Form_F);
6074
6075 if Present (Act_F) then
6076 Next_Formal (Act_F);
6077 end if;
6078 end loop;
6079
6080 Spec :=
6081 Make_Function_Specification (Loc,
6082 Defining_Unit_Name => Func,
6083 Parameter_Specifications => Profile,
6084 Result_Definition => New_Occurrence_Of (Ret_Type, Loc));
6085
6086 Decl :=
6087 Make_Expression_Function (Loc,
6088 Specification => Spec,
6089 Expression =>
6090 Make_Function_Call (Loc,
6091 Name => Func_Name,
6092 Parameter_Associations => Actuals));
6093
6094 return Decl;
6095 end Build_Function_Wrapper;
6096
6097 ----------------------------
6098 -- Build_Operator_Wrapper --
6099 ----------------------------
6100
6101 function Build_Operator_Wrapper
6102 (Formal_Subp : Entity_Id;
6103 Actual_Subp : Entity_Id) return Node_Id
6104 is
6105 Loc : constant Source_Ptr := Sloc (Current_Scope);
6106 Ret_Type : constant Entity_Id :=
6107 Get_Instance_Of (Etype (Formal_Subp));
6108 Op_Type : constant Entity_Id :=
6109 Get_Instance_Of (Etype (First_Formal (Formal_Subp)));
6110 Is_Binary : constant Boolean :=
6111 Present (Next_Formal (First_Formal (Formal_Subp)));
6112
6113 Decl : Node_Id;
6114 Expr : Node_Id := Empty;
6115 F1, F2 : Entity_Id;
6116 Func : Entity_Id;
6117 Op_Name : Name_Id;
6118 Spec : Node_Id;
6119 L, R : Node_Id;
6120
6121 begin
6122 Op_Name := Chars (Actual_Subp);
6123
6124 -- Create entities for wrapper function and its formals
6125
6126 F1 := Make_Temporary (Loc, 'A');
6127 F2 := Make_Temporary (Loc, 'B');
6128 L := New_Occurrence_Of (F1, Loc);
6129 R := New_Occurrence_Of (F2, Loc);
6130
6131 Func := Make_Defining_Identifier (Loc, Chars (Formal_Subp));
6132 Set_Ekind (Func, E_Function);
6133 Set_Is_Generic_Actual_Subprogram (Func);
6134
6135 Spec :=
6136 Make_Function_Specification (Loc,
6137 Defining_Unit_Name => Func,
6138 Parameter_Specifications => New_List (
6139 Make_Parameter_Specification (Loc,
6140 Defining_Identifier => F1,
6141 Parameter_Type => New_Occurrence_Of (Op_Type, Loc))),
6142 Result_Definition => New_Occurrence_Of (Ret_Type, Loc));
6143
6144 if Is_Binary then
6145 Append_To (Parameter_Specifications (Spec),
6146 Make_Parameter_Specification (Loc,
6147 Defining_Identifier => F2,
6148 Parameter_Type => New_Occurrence_Of (Op_Type, Loc)));
6149 end if;
6150
6151 -- Build expression as a function call, or as an operator node
6152 -- that corresponds to the name of the actual, starting with
6153 -- binary operators.
6154
6155 if Op_Name not in Any_Operator_Name then
6156 Expr :=
6157 Make_Function_Call (Loc,
6158 Name =>
6159 New_Occurrence_Of (Actual_Subp, Loc),
6160 Parameter_Associations => New_List (L));
6161
6162 if Is_Binary then
6163 Append_To (Parameter_Associations (Expr), R);
6164 end if;
6165
6166 -- Binary operators
6167
6168 elsif Is_Binary then
6169 if Op_Name = Name_Op_And then
6170 Expr := Make_Op_And (Loc, Left_Opnd => L, Right_Opnd => R);
6171 elsif Op_Name = Name_Op_Or then
6172 Expr := Make_Op_Or (Loc, Left_Opnd => L, Right_Opnd => R);
6173 elsif Op_Name = Name_Op_Xor then
6174 Expr := Make_Op_Xor (Loc, Left_Opnd => L, Right_Opnd => R);
6175 elsif Op_Name = Name_Op_Eq then
6176 Expr := Make_Op_Eq (Loc, Left_Opnd => L, Right_Opnd => R);
6177 elsif Op_Name = Name_Op_Ne then
6178 Expr := Make_Op_Ne (Loc, Left_Opnd => L, Right_Opnd => R);
6179 elsif Op_Name = Name_Op_Le then
6180 Expr := Make_Op_Le (Loc, Left_Opnd => L, Right_Opnd => R);
6181 elsif Op_Name = Name_Op_Gt then
6182 Expr := Make_Op_Gt (Loc, Left_Opnd => L, Right_Opnd => R);
6183 elsif Op_Name = Name_Op_Ge then
6184 Expr := Make_Op_Ge (Loc, Left_Opnd => L, Right_Opnd => R);
6185 elsif Op_Name = Name_Op_Lt then
6186 Expr := Make_Op_Lt (Loc, Left_Opnd => L, Right_Opnd => R);
6187 elsif Op_Name = Name_Op_Add then
6188 Expr := Make_Op_Add (Loc, Left_Opnd => L, Right_Opnd => R);
6189 elsif Op_Name = Name_Op_Subtract then
6190 Expr := Make_Op_Subtract (Loc, Left_Opnd => L, Right_Opnd => R);
6191 elsif Op_Name = Name_Op_Concat then
6192 Expr := Make_Op_Concat (Loc, Left_Opnd => L, Right_Opnd => R);
6193 elsif Op_Name = Name_Op_Multiply then
6194 Expr := Make_Op_Multiply (Loc, Left_Opnd => L, Right_Opnd => R);
6195 elsif Op_Name = Name_Op_Divide then
6196 Expr := Make_Op_Divide (Loc, Left_Opnd => L, Right_Opnd => R);
6197 elsif Op_Name = Name_Op_Mod then
6198 Expr := Make_Op_Mod (Loc, Left_Opnd => L, Right_Opnd => R);
6199 elsif Op_Name = Name_Op_Rem then
6200 Expr := Make_Op_Rem (Loc, Left_Opnd => L, Right_Opnd => R);
6201 elsif Op_Name = Name_Op_Expon then
6202 Expr := Make_Op_Expon (Loc, Left_Opnd => L, Right_Opnd => R);
6203 end if;
6204
6205 -- Unary operators
6206
6207 else
6208 if Op_Name = Name_Op_Add then
6209 Expr := Make_Op_Plus (Loc, Right_Opnd => L);
6210 elsif Op_Name = Name_Op_Subtract then
6211 Expr := Make_Op_Minus (Loc, Right_Opnd => L);
6212 elsif Op_Name = Name_Op_Abs then
6213 Expr := Make_Op_Abs (Loc, Right_Opnd => L);
6214 elsif Op_Name = Name_Op_Not then
6215 Expr := Make_Op_Not (Loc, Right_Opnd => L);
6216 end if;
6217 end if;
6218
6219 Decl :=
6220 Make_Expression_Function (Loc,
6221 Specification => Spec,
6222 Expression => Expr);
6223
6224 return Decl;
6225 end Build_Operator_Wrapper;
6226
6227 -----------------------------------
6228 -- Build_Subprogram_Decl_Wrapper --
6229 -----------------------------------
6230
6231 function Build_Subprogram_Decl_Wrapper
6232 (Formal_Subp : Entity_Id) return Node_Id
6233 is
6234 Loc : constant Source_Ptr := Sloc (Current_Scope);
6235 Ret_Type : constant Entity_Id := Get_Instance_Of (Etype (Formal_Subp));
6236 Decl : Node_Id;
6237 Subp : Entity_Id;
6238 Parm_Spec : Node_Id;
6239 Profile : List_Id := New_List;
6240 Spec : Node_Id;
6241 Form_F : Entity_Id;
6242 New_F : Entity_Id;
6243
6244 begin
6245
6246 Subp := Make_Defining_Identifier (Loc, Chars (Formal_Subp));
6247 Set_Ekind (Subp, Ekind (Formal_Subp));
6248 Set_Is_Generic_Actual_Subprogram (Subp);
6249
6250 Profile := Parameter_Specifications (
6251 New_Copy_Tree
6252 (Specification (Unit_Declaration_Node (Formal_Subp))));
6253
6254 Form_F := First_Formal (Formal_Subp);
6255 Parm_Spec := First (Profile);
6256
6257 -- Create new entities for the formals. Reset entities so that
6258 -- parameter types are properly resolved when wrapper declaration
6259 -- is analyzed.
6260
6261 while Present (Parm_Spec) loop
6262 New_F := Make_Defining_Identifier (Loc, Chars (Form_F));
6263 Set_Defining_Identifier (Parm_Spec, New_F);
6264 Set_Entity (Parameter_Type (Parm_Spec), Empty);
6265 Next (Parm_Spec);
6266 Next_Formal (Form_F);
6267 end loop;
6268
6269 if Ret_Type = Standard_Void_Type then
6270 Spec :=
6271 Make_Procedure_Specification (Loc,
6272 Defining_Unit_Name => Subp,
6273 Parameter_Specifications => Profile);
6274 else
6275 Spec :=
6276 Make_Function_Specification (Loc,
6277 Defining_Unit_Name => Subp,
6278 Parameter_Specifications => Profile,
6279 Result_Definition => New_Occurrence_Of (Ret_Type, Loc));
6280 end if;
6281
6282 Decl :=
6283 Make_Subprogram_Declaration (Loc, Specification => Spec);
6284
6285 return Decl;
6286 end Build_Subprogram_Decl_Wrapper;
6287
6288 -----------------------------------
6289 -- Build_Subprogram_Body_Wrapper --
6290 -----------------------------------
6291
6292 function Build_Subprogram_Body_Wrapper
6293 (Formal_Subp : Entity_Id;
6294 Actual_Name : Node_Id) return Node_Id
6295 is
6296 Loc : constant Source_Ptr := Sloc (Current_Scope);
6297 Ret_Type : constant Entity_Id := Get_Instance_Of (Etype (Formal_Subp));
6298 Spec_Node : constant Node_Id :=
6299 Specification
6300 (Build_Subprogram_Decl_Wrapper (Formal_Subp));
6301 Act : Node_Id;
6302 Actuals : List_Id;
6303 Body_Node : Node_Id;
6304 Stmt : Node_Id;
6305 begin
6306 Actuals := New_List;
6307 Act := First (Parameter_Specifications (Spec_Node));
6308
6309 while Present (Act) loop
6310 Append_To (Actuals,
6311 Make_Identifier (Loc, Chars (Defining_Identifier (Act))));
6312 Next (Act);
6313 end loop;
6314
6315 if Ret_Type = Standard_Void_Type then
6316 Stmt := Make_Procedure_Call_Statement (Loc,
6317 Name => Actual_Name,
6318 Parameter_Associations => Actuals);
6319
6320 else
6321 Stmt := Make_Simple_Return_Statement (Loc,
6322 Expression =>
6323 Make_Function_Call (Loc,
6324 Name => Actual_Name,
6325 Parameter_Associations => Actuals));
6326 end if;
6327
6328 Body_Node := Make_Subprogram_Body (Loc,
6329 Specification => Spec_Node,
6330 Declarations => New_List,
6331 Handled_Statement_Sequence =>
6332 Make_Handled_Sequence_Of_Statements (Loc,
6333 Statements => New_List (Stmt)));
6334
6335 return Body_Node;
6336 end Build_Subprogram_Body_Wrapper;
6337
6338 -------------------------------------------
6339 -- Build_Instance_Compilation_Unit_Nodes --
6340 -------------------------------------------
6341
6342 procedure Build_Instance_Compilation_Unit_Nodes
6343 (N : Node_Id;
6344 Act_Body : Node_Id;
6345 Act_Decl : Node_Id)
6346 is
6347 Decl_Cunit : Node_Id;
6348 Body_Cunit : Node_Id;
6349 Citem : Node_Id;
6350 New_Main : constant Entity_Id := Defining_Entity (Act_Decl);
6351 Old_Main : constant Entity_Id := Cunit_Entity (Main_Unit);
6352
6353 begin
6354 -- A new compilation unit node is built for the instance declaration
6355
6356 Decl_Cunit :=
6357 Make_Compilation_Unit (Sloc (N),
6358 Context_Items => Empty_List,
6359 Unit => Act_Decl,
6360 Aux_Decls_Node => Make_Compilation_Unit_Aux (Sloc (N)));
6361
6362 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
6363
6364 -- The new compilation unit is linked to its body, but both share the
6365 -- same file, so we do not set Body_Required on the new unit so as not
6366 -- to create a spurious dependency on a non-existent body in the ali.
6367 -- This simplifies CodePeer unit traversal.
6368
6369 -- We use the original instantiation compilation unit as the resulting
6370 -- compilation unit of the instance, since this is the main unit.
6371
6372 Rewrite (N, Act_Body);
6373
6374 -- Propagate the aspect specifications from the package body template to
6375 -- the instantiated version of the package body.
6376
6377 if Has_Aspects (Act_Body) then
6378 Set_Aspect_Specifications
6379 (N, New_Copy_List_Tree (Aspect_Specifications (Act_Body)));
6380 end if;
6381
6382 Body_Cunit := Parent (N);
6383
6384 -- The two compilation unit nodes are linked by the Library_Unit field
6385
6386 Set_Library_Unit (Decl_Cunit, Body_Cunit);
6387 Set_Library_Unit (Body_Cunit, Decl_Cunit);
6388
6389 -- Preserve the private nature of the package if needed
6390
6391 Set_Private_Present (Decl_Cunit, Private_Present (Body_Cunit));
6392
6393 -- If the instance is not the main unit, its context, categorization
6394 -- and elaboration entity are not relevant to the compilation.
6395
6396 if Body_Cunit /= Cunit (Main_Unit) then
6397 Make_Instance_Unit (Body_Cunit, In_Main => False);
6398 return;
6399 end if;
6400
6401 -- The context clause items on the instantiation, which are now attached
6402 -- to the body compilation unit (since the body overwrote the original
6403 -- instantiation node), semantically belong on the spec, so copy them
6404 -- there. It's harmless to leave them on the body as well. In fact one
6405 -- could argue that they belong in both places.
6406
6407 Citem := First (Context_Items (Body_Cunit));
6408 while Present (Citem) loop
6409 Append (New_Copy (Citem), Context_Items (Decl_Cunit));
6410 Next (Citem);
6411 end loop;
6412
6413 -- Propagate categorization flags on packages, so that they appear in
6414 -- the ali file for the spec of the unit.
6415
6416 if Ekind (New_Main) = E_Package then
6417 Set_Is_Pure (Old_Main, Is_Pure (New_Main));
6418 Set_Is_Preelaborated (Old_Main, Is_Preelaborated (New_Main));
6419 Set_Is_Remote_Types (Old_Main, Is_Remote_Types (New_Main));
6420 Set_Is_Shared_Passive (Old_Main, Is_Shared_Passive (New_Main));
6421 Set_Is_Remote_Call_Interface
6422 (Old_Main, Is_Remote_Call_Interface (New_Main));
6423 end if;
6424
6425 -- Make entry in Units table, so that binder can generate call to
6426 -- elaboration procedure for body, if any.
6427
6428 Make_Instance_Unit (Body_Cunit, In_Main => True);
6429 Main_Unit_Entity := New_Main;
6430 Set_Cunit_Entity (Main_Unit, Main_Unit_Entity);
6431
6432 -- Build elaboration entity, since the instance may certainly generate
6433 -- elaboration code requiring a flag for protection.
6434
6435 Build_Elaboration_Entity (Decl_Cunit, New_Main);
6436 end Build_Instance_Compilation_Unit_Nodes;
6437
6438 -----------------------------
6439 -- Check_Access_Definition --
6440 -----------------------------
6441
6442 procedure Check_Access_Definition (N : Node_Id) is
6443 begin
6444 pragma Assert
6445 (Ada_Version >= Ada_2005 and then Present (Access_Definition (N)));
6446 null;
6447 end Check_Access_Definition;
6448
6449 -----------------------------------
6450 -- Check_Formal_Package_Instance --
6451 -----------------------------------
6452
6453 -- If the formal has specific parameters, they must match those of the
6454 -- actual. Both of them are instances, and the renaming declarations for
6455 -- their formal parameters appear in the same order in both. The analyzed
6456 -- formal has been analyzed in the context of the current instance.
6457
6458 procedure Check_Formal_Package_Instance
6459 (Formal_Pack : Entity_Id;
6460 Actual_Pack : Entity_Id)
6461 is
6462 E1 : Entity_Id := First_Entity (Actual_Pack);
6463 E2 : Entity_Id := First_Entity (Formal_Pack);
6464 Prev_E1 : Entity_Id;
6465
6466 Expr1 : Node_Id;
6467 Expr2 : Node_Id;
6468
6469 procedure Check_Mismatch (B : Boolean);
6470 -- Common error routine for mismatch between the parameters of the
6471 -- actual instance and those of the formal package.
6472
6473 function Is_Defaulted (Param : Entity_Id) return Boolean;
6474 -- If the formal package has partly box-initialized formals, skip
6475 -- conformance check for these formals. Previously the code assumed
6476 -- that box initialization for a formal package applied to all its
6477 -- formal parameters.
6478
6479 function Same_Instantiated_Constant (E1, E2 : Entity_Id) return Boolean;
6480 -- The formal may come from a nested formal package, and the actual may
6481 -- have been constant-folded. To determine whether the two denote the
6482 -- same entity we may have to traverse several definitions to recover
6483 -- the ultimate entity that they refer to.
6484
6485 function Same_Instantiated_Function (E1, E2 : Entity_Id) return Boolean;
6486 -- The formal and the actual must be identical, but if both are
6487 -- given by attributes they end up renaming different generated bodies,
6488 -- and we must verify that the attributes themselves match.
6489
6490 function Same_Instantiated_Variable (E1, E2 : Entity_Id) return Boolean;
6491 -- Similarly, if the formal comes from a nested formal package, the
6492 -- actual may designate the formal through multiple renamings, which
6493 -- have to be followed to determine the original variable in question.
6494
6495 --------------------
6496 -- Check_Mismatch --
6497 --------------------
6498
6499 procedure Check_Mismatch (B : Boolean) is
6500 -- A Formal_Type_Declaration for a derived private type is rewritten
6501 -- as a private extension decl. (see Analyze_Formal_Derived_Type),
6502 -- which is why we examine the original node.
6503
6504 Kind : constant Node_Kind := Nkind (Original_Node (Parent (E2)));
6505
6506 begin
6507 if Kind = N_Formal_Type_Declaration then
6508 return;
6509
6510 elsif Nkind_In (Kind, N_Formal_Object_Declaration,
6511 N_Formal_Package_Declaration)
6512 or else Kind in N_Formal_Subprogram_Declaration
6513 then
6514 null;
6515
6516 -- Ada 2012: If both formal and actual are incomplete types they
6517 -- are conformant.
6518
6519 elsif Is_Incomplete_Type (E1) and then Is_Incomplete_Type (E2) then
6520 null;
6521
6522 elsif B then
6523 Error_Msg_NE
6524 ("actual for & in actual instance does not match formal",
6525 Parent (Actual_Pack), E1);
6526 end if;
6527 end Check_Mismatch;
6528
6529 ------------------
6530 -- Is_Defaulted --
6531 ------------------
6532
6533 function Is_Defaulted (Param : Entity_Id) return Boolean is
6534 Assoc : Node_Id;
6535
6536 begin
6537 Assoc :=
6538 First (Generic_Associations (Parent
6539 (Associated_Formal_Package (Actual_Pack))));
6540
6541 while Present (Assoc) loop
6542 if Nkind (Assoc) = N_Others_Choice then
6543 return True;
6544
6545 elsif Nkind (Assoc) = N_Generic_Association
6546 and then Chars (Selector_Name (Assoc)) = Chars (Param)
6547 then
6548 return Box_Present (Assoc);
6549 end if;
6550
6551 Next (Assoc);
6552 end loop;
6553
6554 return False;
6555 end Is_Defaulted;
6556
6557 --------------------------------
6558 -- Same_Instantiated_Constant --
6559 --------------------------------
6560
6561 function Same_Instantiated_Constant
6562 (E1, E2 : Entity_Id) return Boolean
6563 is
6564 Ent : Entity_Id;
6565
6566 begin
6567 Ent := E2;
6568 while Present (Ent) loop
6569 if E1 = Ent then
6570 return True;
6571
6572 elsif Ekind (Ent) /= E_Constant then
6573 return False;
6574
6575 elsif Is_Entity_Name (Constant_Value (Ent)) then
6576 if Entity (Constant_Value (Ent)) = E1 then
6577 return True;
6578 else
6579 Ent := Entity (Constant_Value (Ent));
6580 end if;
6581
6582 -- The actual may be a constant that has been folded. Recover
6583 -- original name.
6584
6585 elsif Is_Entity_Name (Original_Node (Constant_Value (Ent))) then
6586 Ent := Entity (Original_Node (Constant_Value (Ent)));
6587
6588 else
6589 return False;
6590 end if;
6591 end loop;
6592
6593 return False;
6594 end Same_Instantiated_Constant;
6595
6596 --------------------------------
6597 -- Same_Instantiated_Function --
6598 --------------------------------
6599
6600 function Same_Instantiated_Function
6601 (E1, E2 : Entity_Id) return Boolean
6602 is
6603 U1, U2 : Node_Id;
6604 begin
6605 if Alias (E1) = Alias (E2) then
6606 return True;
6607
6608 elsif Present (Alias (E2)) then
6609 U1 := Original_Node (Unit_Declaration_Node (E1));
6610 U2 := Original_Node (Unit_Declaration_Node (Alias (E2)));
6611
6612 return Nkind (U1) = N_Subprogram_Renaming_Declaration
6613 and then Nkind (Name (U1)) = N_Attribute_Reference
6614
6615 and then Nkind (U2) = N_Subprogram_Renaming_Declaration
6616 and then Nkind (Name (U2)) = N_Attribute_Reference
6617
6618 and then
6619 Attribute_Name (Name (U1)) = Attribute_Name (Name (U2));
6620 else
6621 return False;
6622 end if;
6623 end Same_Instantiated_Function;
6624
6625 --------------------------------
6626 -- Same_Instantiated_Variable --
6627 --------------------------------
6628
6629 function Same_Instantiated_Variable
6630 (E1, E2 : Entity_Id) return Boolean
6631 is
6632 function Original_Entity (E : Entity_Id) return Entity_Id;
6633 -- Follow chain of renamings to the ultimate ancestor
6634
6635 ---------------------
6636 -- Original_Entity --
6637 ---------------------
6638
6639 function Original_Entity (E : Entity_Id) return Entity_Id is
6640 Orig : Entity_Id;
6641
6642 begin
6643 Orig := E;
6644 while Nkind (Parent (Orig)) = N_Object_Renaming_Declaration
6645 and then Present (Renamed_Object (Orig))
6646 and then Is_Entity_Name (Renamed_Object (Orig))
6647 loop
6648 Orig := Entity (Renamed_Object (Orig));
6649 end loop;
6650
6651 return Orig;
6652 end Original_Entity;
6653
6654 -- Start of processing for Same_Instantiated_Variable
6655
6656 begin
6657 return Ekind (E1) = Ekind (E2)
6658 and then Original_Entity (E1) = Original_Entity (E2);
6659 end Same_Instantiated_Variable;
6660
6661 -- Start of processing for Check_Formal_Package_Instance
6662
6663 begin
6664 Prev_E1 := E1;
6665 while Present (E1) and then Present (E2) loop
6666 exit when Ekind (E1) = E_Package
6667 and then Renamed_Entity (E1) = Renamed_Entity (Actual_Pack);
6668
6669 -- If the formal is the renaming of the formal package, this
6670 -- is the end of its formal part, which may occur before the
6671 -- end of the formal part in the actual in the presence of
6672 -- defaulted parameters in the formal package.
6673
6674 exit when Nkind (Parent (E2)) = N_Package_Renaming_Declaration
6675 and then Renamed_Entity (E2) = Scope (E2);
6676
6677 -- The analysis of the actual may generate additional internal
6678 -- entities. If the formal is defaulted, there is no corresponding
6679 -- analysis and the internal entities must be skipped, until we
6680 -- find corresponding entities again.
6681
6682 if Comes_From_Source (E2)
6683 and then not Comes_From_Source (E1)
6684 and then Chars (E1) /= Chars (E2)
6685 then
6686 while Present (E1) and then Chars (E1) /= Chars (E2) loop
6687 Next_Entity (E1);
6688 end loop;
6689 end if;
6690
6691 if No (E1) then
6692 return;
6693
6694 -- Entities may be declared without full declaration, such as
6695 -- itypes and predefined operators (concatenation for arrays, eg).
6696 -- Skip it and keep the formal entity to find a later match for it.
6697
6698 elsif No (Parent (E2)) and then Ekind (E1) /= Ekind (E2) then
6699 E1 := Prev_E1;
6700 goto Next_E;
6701
6702 -- If the formal entity comes from a formal declaration, it was
6703 -- defaulted in the formal package, and no check is needed on it.
6704
6705 elsif Nkind_In (Original_Node (Parent (E2)),
6706 N_Formal_Object_Declaration,
6707 N_Formal_Type_Declaration)
6708 then
6709 -- If the formal is a tagged type the corresponding class-wide
6710 -- type has been generated as well, and it must be skipped.
6711
6712 if Is_Type (E2) and then Is_Tagged_Type (E2) then
6713 Next_Entity (E2);
6714 end if;
6715
6716 goto Next_E;
6717
6718 -- Ditto for defaulted formal subprograms.
6719
6720 elsif Is_Overloadable (E1)
6721 and then Nkind (Unit_Declaration_Node (E2)) in
6722 N_Formal_Subprogram_Declaration
6723 then
6724 goto Next_E;
6725
6726 elsif Is_Defaulted (E1) then
6727 goto Next_E;
6728
6729 elsif Is_Type (E1) then
6730
6731 -- Subtypes must statically match. E1, E2 are the local entities
6732 -- that are subtypes of the actuals. Itypes generated for other
6733 -- parameters need not be checked, the check will be performed
6734 -- on the parameters themselves.
6735
6736 -- If E2 is a formal type declaration, it is a defaulted parameter
6737 -- and needs no checking.
6738
6739 if not Is_Itype (E1) and then not Is_Itype (E2) then
6740 Check_Mismatch
6741 (not Is_Type (E2)
6742 or else Etype (E1) /= Etype (E2)
6743 or else not Subtypes_Statically_Match (E1, E2));
6744 end if;
6745
6746 elsif Ekind (E1) = E_Constant then
6747
6748 -- IN parameters must denote the same static value, or the same
6749 -- constant, or the literal null.
6750
6751 Expr1 := Expression (Parent (E1));
6752
6753 if Ekind (E2) /= E_Constant then
6754 Check_Mismatch (True);
6755 goto Next_E;
6756 else
6757 Expr2 := Expression (Parent (E2));
6758 end if;
6759
6760 if Is_OK_Static_Expression (Expr1) then
6761 if not Is_OK_Static_Expression (Expr2) then
6762 Check_Mismatch (True);
6763
6764 elsif Is_Discrete_Type (Etype (E1)) then
6765 declare
6766 V1 : constant Uint := Expr_Value (Expr1);
6767 V2 : constant Uint := Expr_Value (Expr2);
6768 begin
6769 Check_Mismatch (V1 /= V2);
6770 end;
6771
6772 elsif Is_Real_Type (Etype (E1)) then
6773 declare
6774 V1 : constant Ureal := Expr_Value_R (Expr1);
6775 V2 : constant Ureal := Expr_Value_R (Expr2);
6776 begin
6777 Check_Mismatch (V1 /= V2);
6778 end;
6779
6780 elsif Is_String_Type (Etype (E1))
6781 and then Nkind (Expr1) = N_String_Literal
6782 then
6783 if Nkind (Expr2) /= N_String_Literal then
6784 Check_Mismatch (True);
6785 else
6786 Check_Mismatch
6787 (not String_Equal (Strval (Expr1), Strval (Expr2)));
6788 end if;
6789 end if;
6790
6791 elsif Is_Entity_Name (Expr1) then
6792 if Is_Entity_Name (Expr2) then
6793 if Entity (Expr1) = Entity (Expr2) then
6794 null;
6795 else
6796 Check_Mismatch
6797 (not Same_Instantiated_Constant
6798 (Entity (Expr1), Entity (Expr2)));
6799 end if;
6800
6801 else
6802 Check_Mismatch (True);
6803 end if;
6804
6805 elsif Is_Entity_Name (Original_Node (Expr1))
6806 and then Is_Entity_Name (Expr2)
6807 and then Same_Instantiated_Constant
6808 (Entity (Original_Node (Expr1)), Entity (Expr2))
6809 then
6810 null;
6811
6812 elsif Nkind (Expr1) = N_Null then
6813 Check_Mismatch (Nkind (Expr1) /= N_Null);
6814
6815 else
6816 Check_Mismatch (True);
6817 end if;
6818
6819 elsif Ekind (E1) = E_Variable then
6820 Check_Mismatch (not Same_Instantiated_Variable (E1, E2));
6821
6822 elsif Ekind (E1) = E_Package then
6823 Check_Mismatch
6824 (Ekind (E1) /= Ekind (E2)
6825 or else (Present (Renamed_Object (E2))
6826 and then Renamed_Object (E1) /=
6827 Renamed_Object (E2)));
6828
6829 elsif Is_Overloadable (E1) then
6830 -- Verify that the actual subprograms match. Note that actuals
6831 -- that are attributes are rewritten as subprograms. If the
6832 -- subprogram in the formal package is defaulted, no check is
6833 -- needed. Note that this can only happen in Ada 2005 when the
6834 -- formal package can be partially parameterized.
6835
6836 if Nkind (Unit_Declaration_Node (E1)) =
6837 N_Subprogram_Renaming_Declaration
6838 and then From_Default (Unit_Declaration_Node (E1))
6839 then
6840 null;
6841
6842 -- If the formal package has an "others" box association that
6843 -- covers this formal, there is no need for a check either.
6844
6845 elsif Nkind (Unit_Declaration_Node (E2)) in
6846 N_Formal_Subprogram_Declaration
6847 and then Box_Present (Unit_Declaration_Node (E2))
6848 then
6849 null;
6850
6851 -- No check needed if subprogram is a defaulted null procedure
6852
6853 elsif No (Alias (E2))
6854 and then Ekind (E2) = E_Procedure
6855 and then
6856 Null_Present (Specification (Unit_Declaration_Node (E2)))
6857 then
6858 null;
6859
6860 -- Otherwise the actual in the formal and the actual in the
6861 -- instantiation of the formal must match, up to renamings.
6862
6863 else
6864 Check_Mismatch
6865 (Ekind (E2) /= Ekind (E1)
6866 or else not Same_Instantiated_Function (E1, E2));
6867 end if;
6868
6869 else
6870 raise Program_Error;
6871 end if;
6872
6873 <<Next_E>>
6874 Prev_E1 := E1;
6875 Next_Entity (E1);
6876 Next_Entity (E2);
6877 end loop;
6878 end Check_Formal_Package_Instance;
6879
6880 ---------------------------
6881 -- Check_Formal_Packages --
6882 ---------------------------
6883
6884 procedure Check_Formal_Packages (P_Id : Entity_Id) is
6885 E : Entity_Id;
6886 Formal_P : Entity_Id;
6887 Formal_Decl : Node_Id;
6888 begin
6889 -- Iterate through the declarations in the instance, looking for package
6890 -- renaming declarations that denote instances of formal packages. Stop
6891 -- when we find the renaming of the current package itself. The
6892 -- declaration for a formal package without a box is followed by an
6893 -- internal entity that repeats the instantiation.
6894
6895 E := First_Entity (P_Id);
6896 while Present (E) loop
6897 if Ekind (E) = E_Package then
6898 if Renamed_Object (E) = P_Id then
6899 exit;
6900
6901 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
6902 null;
6903
6904 else
6905 Formal_Decl := Parent (Associated_Formal_Package (E));
6906
6907 -- Nothing to check if the formal has a box or an others_clause
6908 -- (necessarily with a box), or no associations altogether
6909
6910 if Box_Present (Formal_Decl)
6911 or else No (Generic_Associations (Formal_Decl))
6912 then
6913 null;
6914
6915 elsif Nkind (First (Generic_Associations (Formal_Decl))) =
6916 N_Others_Choice
6917 then
6918 -- The internal validating package was generated but formal
6919 -- and instance are known to be compatible.
6920
6921 Formal_P := Next_Entity (E);
6922 Remove (Unit_Declaration_Node (Formal_P));
6923
6924 else
6925 Formal_P := Next_Entity (E);
6926
6927 -- If the instance is within an enclosing instance body
6928 -- there is no need to verify the legality of current formal
6929 -- packages because they were legal in the generic body.
6930 -- This optimization may be applicable elsewhere, and it
6931 -- also removes spurious errors that may arise with
6932 -- on-the-fly inlining and confusion between private and
6933 -- full views.
6934
6935 if not In_Instance_Body then
6936 Check_Formal_Package_Instance (Formal_P, E);
6937 end if;
6938
6939 -- Restore the visibility of formals of the formal instance
6940 -- that are not defaulted, and are hidden within the current
6941 -- generic. These formals may be visible within an enclosing
6942 -- generic.
6943
6944 declare
6945 Elmt : Elmt_Id;
6946 begin
6947 Elmt := First_Elmt (Hidden_In_Formal_Instance (Formal_P));
6948 while Present (Elmt) loop
6949 Set_Is_Hidden (Node (Elmt), False);
6950 Next_Elmt (Elmt);
6951 end loop;
6952 end;
6953
6954 -- After checking, remove the internal validating package.
6955 -- It is only needed for semantic checks, and as it may
6956 -- contain generic formal declarations it should not reach
6957 -- gigi.
6958
6959 Remove (Unit_Declaration_Node (Formal_P));
6960 end if;
6961 end if;
6962 end if;
6963
6964 Next_Entity (E);
6965 end loop;
6966 end Check_Formal_Packages;
6967
6968 ---------------------------------
6969 -- Check_Forward_Instantiation --
6970 ---------------------------------
6971
6972 procedure Check_Forward_Instantiation (Decl : Node_Id) is
6973 S : Entity_Id;
6974 Gen_Comp : Entity_Id := Cunit_Entity (Get_Source_Unit (Decl));
6975
6976 begin
6977 -- The instantiation appears before the generic body if we are in the
6978 -- scope of the unit containing the generic, either in its spec or in
6979 -- the package body, and before the generic body.
6980
6981 if Ekind (Gen_Comp) = E_Package_Body then
6982 Gen_Comp := Spec_Entity (Gen_Comp);
6983 end if;
6984
6985 if In_Open_Scopes (Gen_Comp)
6986 and then No (Corresponding_Body (Decl))
6987 then
6988 S := Current_Scope;
6989
6990 while Present (S)
6991 and then not Is_Compilation_Unit (S)
6992 and then not Is_Child_Unit (S)
6993 loop
6994 if Ekind (S) = E_Package then
6995 Set_Has_Forward_Instantiation (S);
6996 end if;
6997
6998 S := Scope (S);
6999 end loop;
7000 end if;
7001 end Check_Forward_Instantiation;
7002
7003 ---------------------------
7004 -- Check_Generic_Actuals --
7005 ---------------------------
7006
7007 -- The visibility of the actuals may be different between the point of
7008 -- generic instantiation and the instantiation of the body.
7009
7010 procedure Check_Generic_Actuals
7011 (Instance : Entity_Id;
7012 Is_Formal_Box : Boolean)
7013 is
7014 E : Entity_Id;
7015 Astype : Entity_Id;
7016
7017 begin
7018 E := First_Entity (Instance);
7019 while Present (E) loop
7020 if Is_Type (E)
7021 and then Nkind (Parent (E)) = N_Subtype_Declaration
7022 and then Scope (Etype (E)) /= Instance
7023 and then Is_Entity_Name (Subtype_Indication (Parent (E)))
7024 then
7025 -- Restore the proper view of the actual from the information
7026 -- saved earlier by Instantiate_Type.
7027
7028 Check_Private_View (Subtype_Indication (Parent (E)));
7029
7030 -- If the actual is itself the formal of a parent instance,
7031 -- then also restore the proper view of its actual and so on.
7032 -- That's necessary for nested instantiations of the form
7033
7034 -- generic
7035 -- type Component is private;
7036 -- type Array_Type is array (Positive range <>) of Component;
7037 -- procedure Proc;
7038
7039 -- when the outermost actuals have inconsistent views, because
7040 -- the Component_Type of Array_Type of the inner instantiations
7041 -- is the actual of Component of the outermost one and not that
7042 -- of the corresponding inner instantiations.
7043
7044 Astype := Ancestor_Subtype (E);
7045 while Present (Astype)
7046 and then Nkind (Parent (Astype)) = N_Subtype_Declaration
7047 and then Present (Generic_Parent_Type (Parent (Astype)))
7048 and then Is_Entity_Name (Subtype_Indication (Parent (Astype)))
7049 loop
7050 Check_Private_View (Subtype_Indication (Parent (Astype)));
7051 Astype := Ancestor_Subtype (Astype);
7052 end loop;
7053
7054 Set_Is_Generic_Actual_Type (E);
7055
7056 if Is_Private_Type (E) and then Present (Full_View (E)) then
7057 Set_Is_Generic_Actual_Type (Full_View (E));
7058 end if;
7059
7060 Set_Is_Hidden (E, False);
7061 Set_Is_Potentially_Use_Visible (E, In_Use (Instance));
7062
7063 -- We constructed the generic actual type as a subtype of the
7064 -- supplied type. This means that it normally would not inherit
7065 -- subtype specific attributes of the actual, which is wrong for
7066 -- the generic case.
7067
7068 Astype := Ancestor_Subtype (E);
7069
7070 if No (Astype) then
7071
7072 -- This can happen when E is an itype that is the full view of
7073 -- a private type completed, e.g. with a constrained array. In
7074 -- that case, use the first subtype, which will carry size
7075 -- information. The base type itself is unconstrained and will
7076 -- not carry it.
7077
7078 Astype := First_Subtype (E);
7079 end if;
7080
7081 Set_Size_Info (E, (Astype));
7082 Set_RM_Size (E, RM_Size (Astype));
7083 Set_First_Rep_Item (E, First_Rep_Item (Astype));
7084
7085 if Is_Discrete_Or_Fixed_Point_Type (E) then
7086 Set_RM_Size (E, RM_Size (Astype));
7087 end if;
7088
7089 elsif Ekind (E) = E_Package then
7090
7091 -- If this is the renaming for the current instance, we're done.
7092 -- Otherwise it is a formal package. If the corresponding formal
7093 -- was declared with a box, the (instantiations of the) generic
7094 -- formal part are also visible. Otherwise, ignore the entity
7095 -- created to validate the actuals.
7096
7097 if Renamed_Object (E) = Instance then
7098 exit;
7099
7100 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
7101 null;
7102
7103 -- The visibility of a formal of an enclosing generic is already
7104 -- correct.
7105
7106 elsif Denotes_Formal_Package (E) then
7107 null;
7108
7109 elsif Present (Associated_Formal_Package (E))
7110 and then not Is_Generic_Formal (E)
7111 then
7112 if Box_Present (Parent (Associated_Formal_Package (E))) then
7113 Check_Generic_Actuals (Renamed_Object (E), True);
7114
7115 else
7116 Check_Generic_Actuals (Renamed_Object (E), False);
7117 end if;
7118
7119 Set_Is_Hidden (E, False);
7120 end if;
7121
7122 -- If this is a subprogram instance (in a wrapper package) the
7123 -- actual is fully visible.
7124
7125 elsif Is_Wrapper_Package (Instance) then
7126 Set_Is_Hidden (E, False);
7127
7128 -- If the formal package is declared with a box, or if the formal
7129 -- parameter is defaulted, it is visible in the body.
7130
7131 elsif Is_Formal_Box or else Is_Visible_Formal (E) then
7132 Set_Is_Hidden (E, False);
7133 end if;
7134
7135 if Ekind (E) = E_Constant then
7136
7137 -- If the type of the actual is a private type declared in the
7138 -- enclosing scope of the generic unit, the body of the generic
7139 -- sees the full view of the type (because it has to appear in
7140 -- the corresponding package body). If the type is private now,
7141 -- exchange views to restore the proper visiblity in the instance.
7142
7143 declare
7144 Typ : constant Entity_Id := Base_Type (Etype (E));
7145 -- The type of the actual
7146
7147 Gen_Id : Entity_Id;
7148 -- The generic unit
7149
7150 Parent_Scope : Entity_Id;
7151 -- The enclosing scope of the generic unit
7152
7153 begin
7154 if Is_Wrapper_Package (Instance) then
7155 Gen_Id :=
7156 Generic_Parent
7157 (Specification
7158 (Unit_Declaration_Node
7159 (Related_Instance (Instance))));
7160 else
7161 Gen_Id :=
7162 Generic_Parent (Package_Specification (Instance));
7163 end if;
7164
7165 Parent_Scope := Scope (Gen_Id);
7166
7167 -- The exchange is only needed if the generic is defined
7168 -- within a package which is not a common ancestor of the
7169 -- scope of the instance, and is not already in scope.
7170
7171 if Is_Private_Type (Typ)
7172 and then Scope (Typ) = Parent_Scope
7173 and then Scope (Instance) /= Parent_Scope
7174 and then Ekind (Parent_Scope) = E_Package
7175 and then not Is_Child_Unit (Gen_Id)
7176 then
7177 Switch_View (Typ);
7178
7179 -- If the type of the entity is a subtype, it may also have
7180 -- to be made visible, together with the base type of its
7181 -- full view, after exchange.
7182
7183 if Is_Private_Type (Etype (E)) then
7184 Switch_View (Etype (E));
7185 Switch_View (Base_Type (Etype (E)));
7186 end if;
7187 end if;
7188 end;
7189 end if;
7190
7191 Next_Entity (E);
7192 end loop;
7193 end Check_Generic_Actuals;
7194
7195 ------------------------------
7196 -- Check_Generic_Child_Unit --
7197 ------------------------------
7198
7199 procedure Check_Generic_Child_Unit
7200 (Gen_Id : Node_Id;
7201 Parent_Installed : in out Boolean)
7202 is
7203 Loc : constant Source_Ptr := Sloc (Gen_Id);
7204 Gen_Par : Entity_Id := Empty;
7205 E : Entity_Id;
7206 Inst_Par : Entity_Id;
7207 S : Node_Id;
7208
7209 function Find_Generic_Child
7210 (Scop : Entity_Id;
7211 Id : Node_Id) return Entity_Id;
7212 -- Search generic parent for possible child unit with the given name
7213
7214 function In_Enclosing_Instance return Boolean;
7215 -- Within an instance of the parent, the child unit may be denoted by
7216 -- a simple name, or an abbreviated expanded name. Examine enclosing
7217 -- scopes to locate a possible parent instantiation.
7218
7219 ------------------------
7220 -- Find_Generic_Child --
7221 ------------------------
7222
7223 function Find_Generic_Child
7224 (Scop : Entity_Id;
7225 Id : Node_Id) return Entity_Id
7226 is
7227 E : Entity_Id;
7228
7229 begin
7230 -- If entity of name is already set, instance has already been
7231 -- resolved, e.g. in an enclosing instantiation.
7232
7233 if Present (Entity (Id)) then
7234 if Scope (Entity (Id)) = Scop then
7235 return Entity (Id);
7236 else
7237 return Empty;
7238 end if;
7239
7240 else
7241 E := First_Entity (Scop);
7242 while Present (E) loop
7243 if Chars (E) = Chars (Id)
7244 and then Is_Child_Unit (E)
7245 then
7246 if Is_Child_Unit (E)
7247 and then not Is_Visible_Lib_Unit (E)
7248 then
7249 Error_Msg_NE
7250 ("generic child unit& is not visible", Gen_Id, E);
7251 end if;
7252
7253 Set_Entity (Id, E);
7254 return E;
7255 end if;
7256
7257 Next_Entity (E);
7258 end loop;
7259
7260 return Empty;
7261 end if;
7262 end Find_Generic_Child;
7263
7264 ---------------------------
7265 -- In_Enclosing_Instance --
7266 ---------------------------
7267
7268 function In_Enclosing_Instance return Boolean is
7269 Enclosing_Instance : Node_Id;
7270 Instance_Decl : Node_Id;
7271
7272 begin
7273 -- We do not inline any call that contains instantiations, except
7274 -- for instantiations of Unchecked_Conversion, so if we are within
7275 -- an inlined body the current instance does not require parents.
7276
7277 if In_Inlined_Body then
7278 pragma Assert (Chars (Gen_Id) = Name_Unchecked_Conversion);
7279 return False;
7280 end if;
7281
7282 -- Loop to check enclosing scopes
7283
7284 Enclosing_Instance := Current_Scope;
7285 while Present (Enclosing_Instance) loop
7286 Instance_Decl := Unit_Declaration_Node (Enclosing_Instance);
7287
7288 if Ekind (Enclosing_Instance) = E_Package
7289 and then Is_Generic_Instance (Enclosing_Instance)
7290 and then Present
7291 (Generic_Parent (Specification (Instance_Decl)))
7292 then
7293 -- Check whether the generic we are looking for is a child of
7294 -- this instance.
7295
7296 E := Find_Generic_Child
7297 (Generic_Parent (Specification (Instance_Decl)), Gen_Id);
7298 exit when Present (E);
7299
7300 else
7301 E := Empty;
7302 end if;
7303
7304 Enclosing_Instance := Scope (Enclosing_Instance);
7305 end loop;
7306
7307 if No (E) then
7308
7309 -- Not a child unit
7310
7311 Analyze (Gen_Id);
7312 return False;
7313
7314 else
7315 Rewrite (Gen_Id,
7316 Make_Expanded_Name (Loc,
7317 Chars => Chars (E),
7318 Prefix => New_Occurrence_Of (Enclosing_Instance, Loc),
7319 Selector_Name => New_Occurrence_Of (E, Loc)));
7320
7321 Set_Entity (Gen_Id, E);
7322 Set_Etype (Gen_Id, Etype (E));
7323 Parent_Installed := False; -- Already in scope.
7324 return True;
7325 end if;
7326 end In_Enclosing_Instance;
7327
7328 -- Start of processing for Check_Generic_Child_Unit
7329
7330 begin
7331 -- If the name of the generic is given by a selected component, it may
7332 -- be the name of a generic child unit, and the prefix is the name of an
7333 -- instance of the parent, in which case the child unit must be visible.
7334 -- If this instance is not in scope, it must be placed there and removed
7335 -- after instantiation, because what is being instantiated is not the
7336 -- original child, but the corresponding child present in the instance
7337 -- of the parent.
7338
7339 -- If the child is instantiated within the parent, it can be given by
7340 -- a simple name. In this case the instance is already in scope, but
7341 -- the child generic must be recovered from the generic parent as well.
7342
7343 if Nkind (Gen_Id) = N_Selected_Component then
7344 S := Selector_Name (Gen_Id);
7345 Analyze (Prefix (Gen_Id));
7346 Inst_Par := Entity (Prefix (Gen_Id));
7347
7348 if Ekind (Inst_Par) = E_Package
7349 and then Present (Renamed_Object (Inst_Par))
7350 then
7351 Inst_Par := Renamed_Object (Inst_Par);
7352 end if;
7353
7354 if Ekind (Inst_Par) = E_Package then
7355 if Nkind (Parent (Inst_Par)) = N_Package_Specification then
7356 Gen_Par := Generic_Parent (Parent (Inst_Par));
7357
7358 elsif Nkind (Parent (Inst_Par)) = N_Defining_Program_Unit_Name
7359 and then
7360 Nkind (Parent (Parent (Inst_Par))) = N_Package_Specification
7361 then
7362 Gen_Par := Generic_Parent (Parent (Parent (Inst_Par)));
7363 end if;
7364
7365 elsif Ekind (Inst_Par) = E_Generic_Package
7366 and then Nkind (Parent (Gen_Id)) = N_Formal_Package_Declaration
7367 then
7368 -- A formal package may be a real child package, and not the
7369 -- implicit instance within a parent. In this case the child is
7370 -- not visible and has to be retrieved explicitly as well.
7371
7372 Gen_Par := Inst_Par;
7373 end if;
7374
7375 if Present (Gen_Par) then
7376
7377 -- The prefix denotes an instantiation. The entity itself may be a
7378 -- nested generic, or a child unit.
7379
7380 E := Find_Generic_Child (Gen_Par, S);
7381
7382 if Present (E) then
7383 Change_Selected_Component_To_Expanded_Name (Gen_Id);
7384 Set_Entity (Gen_Id, E);
7385 Set_Etype (Gen_Id, Etype (E));
7386 Set_Entity (S, E);
7387 Set_Etype (S, Etype (E));
7388
7389 -- Indicate that this is a reference to the parent
7390
7391 if In_Extended_Main_Source_Unit (Gen_Id) then
7392 Set_Is_Instantiated (Inst_Par);
7393 end if;
7394
7395 -- A common mistake is to replicate the naming scheme of a
7396 -- hierarchy by instantiating a generic child directly, rather
7397 -- than the implicit child in a parent instance:
7398
7399 -- generic .. package Gpar is ..
7400 -- generic .. package Gpar.Child is ..
7401 -- package Par is new Gpar ();
7402
7403 -- with Gpar.Child;
7404 -- package Par.Child is new Gpar.Child ();
7405 -- rather than Par.Child
7406
7407 -- In this case the instantiation is within Par, which is an
7408 -- instance, but Gpar does not denote Par because we are not IN
7409 -- the instance of Gpar, so this is illegal. The test below
7410 -- recognizes this particular case.
7411
7412 if Is_Child_Unit (E)
7413 and then not Comes_From_Source (Entity (Prefix (Gen_Id)))
7414 and then (not In_Instance
7415 or else Nkind (Parent (Parent (Gen_Id))) =
7416 N_Compilation_Unit)
7417 then
7418 Error_Msg_N
7419 ("prefix of generic child unit must be instance of parent",
7420 Gen_Id);
7421 end if;
7422
7423 if not In_Open_Scopes (Inst_Par)
7424 and then Nkind (Parent (Gen_Id)) not in
7425 N_Generic_Renaming_Declaration
7426 then
7427 Install_Parent (Inst_Par);
7428 Parent_Installed := True;
7429
7430 elsif In_Open_Scopes (Inst_Par) then
7431
7432 -- If the parent is already installed, install the actuals
7433 -- for its formal packages. This is necessary when the child
7434 -- instance is a child of the parent instance: in this case,
7435 -- the parent is placed on the scope stack but the formal
7436 -- packages are not made visible.
7437
7438 Install_Formal_Packages (Inst_Par);
7439 end if;
7440
7441 else
7442 -- If the generic parent does not contain an entity that
7443 -- corresponds to the selector, the instance doesn't either.
7444 -- Analyzing the node will yield the appropriate error message.
7445 -- If the entity is not a child unit, then it is an inner
7446 -- generic in the parent.
7447
7448 Analyze (Gen_Id);
7449 end if;
7450
7451 else
7452 Analyze (Gen_Id);
7453
7454 if Is_Child_Unit (Entity (Gen_Id))
7455 and then
7456 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
7457 and then not In_Open_Scopes (Inst_Par)
7458 then
7459 Install_Parent (Inst_Par);
7460 Parent_Installed := True;
7461
7462 -- The generic unit may be the renaming of the implicit child
7463 -- present in an instance. In that case the parent instance is
7464 -- obtained from the name of the renamed entity.
7465
7466 elsif Ekind (Entity (Gen_Id)) = E_Generic_Package
7467 and then Present (Renamed_Entity (Entity (Gen_Id)))
7468 and then Is_Child_Unit (Renamed_Entity (Entity (Gen_Id)))
7469 then
7470 declare
7471 Renamed_Package : constant Node_Id :=
7472 Name (Parent (Entity (Gen_Id)));
7473 begin
7474 if Nkind (Renamed_Package) = N_Expanded_Name then
7475 Inst_Par := Entity (Prefix (Renamed_Package));
7476 Install_Parent (Inst_Par);
7477 Parent_Installed := True;
7478 end if;
7479 end;
7480 end if;
7481 end if;
7482
7483 elsif Nkind (Gen_Id) = N_Expanded_Name then
7484
7485 -- Entity already present, analyze prefix, whose meaning may be an
7486 -- instance in the current context. If it is an instance of a
7487 -- relative within another, the proper parent may still have to be
7488 -- installed, if they are not of the same generation.
7489
7490 Analyze (Prefix (Gen_Id));
7491
7492 -- Prevent cascaded errors
7493
7494 if Etype (Prefix (Gen_Id)) = Any_Type then
7495 return;
7496 end if;
7497
7498 -- In the unlikely case that a local declaration hides the name of
7499 -- the parent package, locate it on the homonym chain. If the context
7500 -- is an instance of the parent, the renaming entity is flagged as
7501 -- such.
7502
7503 Inst_Par := Entity (Prefix (Gen_Id));
7504 while Present (Inst_Par)
7505 and then not Is_Package_Or_Generic_Package (Inst_Par)
7506 loop
7507 Inst_Par := Homonym (Inst_Par);
7508 end loop;
7509
7510 pragma Assert (Present (Inst_Par));
7511 Set_Entity (Prefix (Gen_Id), Inst_Par);
7512
7513 if In_Enclosing_Instance then
7514 null;
7515
7516 elsif Present (Entity (Gen_Id))
7517 and then Is_Child_Unit (Entity (Gen_Id))
7518 and then not In_Open_Scopes (Inst_Par)
7519 then
7520 Install_Parent (Inst_Par);
7521 Parent_Installed := True;
7522 end if;
7523
7524 elsif In_Enclosing_Instance then
7525
7526 -- The child unit is found in some enclosing scope
7527
7528 null;
7529
7530 else
7531 Analyze (Gen_Id);
7532
7533 -- If this is the renaming of the implicit child in a parent
7534 -- instance, recover the parent name and install it.
7535
7536 if Is_Entity_Name (Gen_Id) then
7537 E := Entity (Gen_Id);
7538
7539 if Is_Generic_Unit (E)
7540 and then Nkind (Parent (E)) in N_Generic_Renaming_Declaration
7541 and then Is_Child_Unit (Renamed_Object (E))
7542 and then Is_Generic_Unit (Scope (Renamed_Object (E)))
7543 and then Nkind (Name (Parent (E))) = N_Expanded_Name
7544 then
7545 Rewrite (Gen_Id, New_Copy_Tree (Name (Parent (E))));
7546 Inst_Par := Entity (Prefix (Gen_Id));
7547
7548 if not In_Open_Scopes (Inst_Par) then
7549 Install_Parent (Inst_Par);
7550 Parent_Installed := True;
7551 end if;
7552
7553 -- If it is a child unit of a non-generic parent, it may be
7554 -- use-visible and given by a direct name. Install parent as
7555 -- for other cases.
7556
7557 elsif Is_Generic_Unit (E)
7558 and then Is_Child_Unit (E)
7559 and then
7560 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
7561 and then not Is_Generic_Unit (Scope (E))
7562 then
7563 if not In_Open_Scopes (Scope (E)) then
7564 Install_Parent (Scope (E));
7565 Parent_Installed := True;
7566 end if;
7567 end if;
7568 end if;
7569 end if;
7570 end Check_Generic_Child_Unit;
7571
7572 -----------------------------
7573 -- Check_Hidden_Child_Unit --
7574 -----------------------------
7575
7576 procedure Check_Hidden_Child_Unit
7577 (N : Node_Id;
7578 Gen_Unit : Entity_Id;
7579 Act_Decl_Id : Entity_Id)
7580 is
7581 Gen_Id : constant Node_Id := Name (N);
7582
7583 begin
7584 if Is_Child_Unit (Gen_Unit)
7585 and then Is_Child_Unit (Act_Decl_Id)
7586 and then Nkind (Gen_Id) = N_Expanded_Name
7587 and then Entity (Prefix (Gen_Id)) = Scope (Act_Decl_Id)
7588 and then Chars (Gen_Unit) = Chars (Act_Decl_Id)
7589 then
7590 Error_Msg_Node_2 := Scope (Act_Decl_Id);
7591 Error_Msg_NE
7592 ("generic unit & is implicitly declared in &",
7593 Defining_Unit_Name (N), Gen_Unit);
7594 Error_Msg_N ("\instance must have different name",
7595 Defining_Unit_Name (N));
7596 end if;
7597 end Check_Hidden_Child_Unit;
7598
7599 ------------------------
7600 -- Check_Private_View --
7601 ------------------------
7602
7603 procedure Check_Private_View (N : Node_Id) is
7604 T : constant Entity_Id := Etype (N);
7605 BT : Entity_Id;
7606
7607 begin
7608 -- Exchange views if the type was not private in the generic but is
7609 -- private at the point of instantiation. Do not exchange views if
7610 -- the scope of the type is in scope. This can happen if both generic
7611 -- and instance are sibling units, or if type is defined in a parent.
7612 -- In this case the visibility of the type will be correct for all
7613 -- semantic checks.
7614
7615 if Present (T) then
7616 BT := Base_Type (T);
7617
7618 if Is_Private_Type (T)
7619 and then not Has_Private_View (N)
7620 and then Present (Full_View (T))
7621 and then not In_Open_Scopes (Scope (T))
7622 then
7623 -- In the generic, the full declaration was visible
7624
7625 Switch_View (T);
7626
7627 elsif Has_Private_View (N)
7628 and then not Is_Private_Type (T)
7629 and then not Has_Been_Exchanged (T)
7630 and then (not In_Open_Scopes (Scope (T))
7631 or else Nkind (Parent (N)) = N_Subtype_Declaration)
7632 then
7633 -- In the generic, only the private declaration was visible
7634
7635 -- If the type appears in a subtype declaration, the subtype in
7636 -- instance must have a view compatible with that of its parent,
7637 -- which must be exchanged (see corresponding code in Restore_
7638 -- Private_Views) so we make an exception to the open scope rule.
7639
7640 Prepend_Elmt (T, Exchanged_Views);
7641 Exchange_Declarations (Etype (Get_Associated_Node (N)));
7642
7643 -- Finally, a non-private subtype may have a private base type, which
7644 -- must be exchanged for consistency. This can happen when a package
7645 -- body is instantiated, when the scope stack is empty but in fact
7646 -- the subtype and the base type are declared in an enclosing scope.
7647
7648 -- Note that in this case we introduce an inconsistency in the view
7649 -- set, because we switch the base type BT, but there could be some
7650 -- private dependent subtypes of BT which remain unswitched. Such
7651 -- subtypes might need to be switched at a later point (see specific
7652 -- provision for that case in Switch_View).
7653
7654 elsif not Is_Private_Type (T)
7655 and then not Has_Private_View (N)
7656 and then Is_Private_Type (BT)
7657 and then Present (Full_View (BT))
7658 and then not Is_Generic_Type (BT)
7659 and then not In_Open_Scopes (BT)
7660 then
7661 Prepend_Elmt (Full_View (BT), Exchanged_Views);
7662 Exchange_Declarations (BT);
7663 end if;
7664 end if;
7665 end Check_Private_View;
7666
7667 -----------------------------
7668 -- Check_Hidden_Primitives --
7669 -----------------------------
7670
7671 function Check_Hidden_Primitives (Assoc_List : List_Id) return Elist_Id is
7672 Actual : Node_Id;
7673 Gen_T : Entity_Id;
7674 Result : Elist_Id := No_Elist;
7675
7676 begin
7677 if No (Assoc_List) then
7678 return No_Elist;
7679 end if;
7680
7681 -- Traverse the list of associations between formals and actuals
7682 -- searching for renamings of tagged types
7683
7684 Actual := First (Assoc_List);
7685 while Present (Actual) loop
7686 if Nkind (Actual) = N_Subtype_Declaration then
7687 Gen_T := Generic_Parent_Type (Actual);
7688
7689 if Present (Gen_T) and then Is_Tagged_Type (Gen_T) then
7690
7691 -- Traverse the list of primitives of the actual types
7692 -- searching for hidden primitives that are visible in the
7693 -- corresponding generic formal; leave them visible and
7694 -- append them to Result to restore their decoration later.
7695
7696 Install_Hidden_Primitives
7697 (Prims_List => Result,
7698 Gen_T => Gen_T,
7699 Act_T => Entity (Subtype_Indication (Actual)));
7700 end if;
7701 end if;
7702
7703 Next (Actual);
7704 end loop;
7705
7706 return Result;
7707 end Check_Hidden_Primitives;
7708
7709 --------------------------
7710 -- Contains_Instance_Of --
7711 --------------------------
7712
7713 function Contains_Instance_Of
7714 (Inner : Entity_Id;
7715 Outer : Entity_Id;
7716 N : Node_Id) return Boolean
7717 is
7718 Elmt : Elmt_Id;
7719 Scop : Entity_Id;
7720
7721 begin
7722 Scop := Outer;
7723
7724 -- Verify that there are no circular instantiations. We check whether
7725 -- the unit contains an instance of the current scope or some enclosing
7726 -- scope (in case one of the instances appears in a subunit). Longer
7727 -- circularities involving subunits might seem too pathological to
7728 -- consider, but they were not too pathological for the authors of
7729 -- DEC bc30vsq, so we loop over all enclosing scopes, and mark all
7730 -- enclosing generic scopes as containing an instance.
7731
7732 loop
7733 -- Within a generic subprogram body, the scope is not generic, to
7734 -- allow for recursive subprograms. Use the declaration to determine
7735 -- whether this is a generic unit.
7736
7737 if Ekind (Scop) = E_Generic_Package
7738 or else (Is_Subprogram (Scop)
7739 and then Nkind (Unit_Declaration_Node (Scop)) =
7740 N_Generic_Subprogram_Declaration)
7741 then
7742 Elmt := First_Elmt (Inner_Instances (Inner));
7743
7744 while Present (Elmt) loop
7745 if Node (Elmt) = Scop then
7746 Error_Msg_Node_2 := Inner;
7747 Error_Msg_NE
7748 ("circular Instantiation: & instantiated within &!",
7749 N, Scop);
7750 return True;
7751
7752 elsif Node (Elmt) = Inner then
7753 return True;
7754
7755 elsif Contains_Instance_Of (Node (Elmt), Scop, N) then
7756 Error_Msg_Node_2 := Inner;
7757 Error_Msg_NE
7758 ("circular Instantiation: & instantiated within &!",
7759 N, Node (Elmt));
7760 return True;
7761 end if;
7762
7763 Next_Elmt (Elmt);
7764 end loop;
7765
7766 -- Indicate that Inner is being instantiated within Scop
7767
7768 Append_Elmt (Inner, Inner_Instances (Scop));
7769 end if;
7770
7771 if Scop = Standard_Standard then
7772 exit;
7773 else
7774 Scop := Scope (Scop);
7775 end if;
7776 end loop;
7777
7778 return False;
7779 end Contains_Instance_Of;
7780
7781 -----------------------
7782 -- Copy_Generic_Node --
7783 -----------------------
7784
7785 function Copy_Generic_Node
7786 (N : Node_Id;
7787 Parent_Id : Node_Id;
7788 Instantiating : Boolean) return Node_Id
7789 is
7790 Ent : Entity_Id;
7791 New_N : Node_Id;
7792
7793 function Copy_Generic_Descendant (D : Union_Id) return Union_Id;
7794 -- Check the given value of one of the Fields referenced by the current
7795 -- node to determine whether to copy it recursively. The field may hold
7796 -- a Node_Id, a List_Id, or an Elist_Id, or a plain value (Sloc, Uint,
7797 -- Char) in which case it need not be copied.
7798
7799 procedure Copy_Descendants;
7800 -- Common utility for various nodes
7801
7802 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id;
7803 -- Make copy of element list
7804
7805 function Copy_Generic_List
7806 (L : List_Id;
7807 Parent_Id : Node_Id) return List_Id;
7808 -- Apply Copy_Node recursively to the members of a node list
7809
7810 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean;
7811 -- True if an identifier is part of the defining program unit name of
7812 -- a child unit.
7813 -- Consider removing this subprogram now that ASIS no longer uses it.
7814
7815 ----------------------
7816 -- Copy_Descendants --
7817 ----------------------
7818
7819 procedure Copy_Descendants is
7820 use Atree.Unchecked_Access;
7821 -- This code section is part of the implementation of an untyped
7822 -- tree traversal, so it needs direct access to node fields.
7823
7824 begin
7825 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
7826 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
7827 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
7828 Set_Field4 (New_N, Copy_Generic_Descendant (Field4 (N)));
7829 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
7830 end Copy_Descendants;
7831
7832 -----------------------------
7833 -- Copy_Generic_Descendant --
7834 -----------------------------
7835
7836 function Copy_Generic_Descendant (D : Union_Id) return Union_Id is
7837 begin
7838 if D = Union_Id (Empty) then
7839 return D;
7840
7841 elsif D in Node_Range then
7842 return Union_Id
7843 (Copy_Generic_Node (Node_Id (D), New_N, Instantiating));
7844
7845 elsif D in List_Range then
7846 return Union_Id (Copy_Generic_List (List_Id (D), New_N));
7847
7848 elsif D in Elist_Range then
7849 return Union_Id (Copy_Generic_Elist (Elist_Id (D)));
7850
7851 -- Nothing else is copyable (e.g. Uint values), return as is
7852
7853 else
7854 return D;
7855 end if;
7856 end Copy_Generic_Descendant;
7857
7858 ------------------------
7859 -- Copy_Generic_Elist --
7860 ------------------------
7861
7862 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id is
7863 M : Elmt_Id;
7864 L : Elist_Id;
7865
7866 begin
7867 if Present (E) then
7868 L := New_Elmt_List;
7869 M := First_Elmt (E);
7870 while Present (M) loop
7871 Append_Elmt
7872 (Copy_Generic_Node (Node (M), Empty, Instantiating), L);
7873 Next_Elmt (M);
7874 end loop;
7875
7876 return L;
7877
7878 else
7879 return No_Elist;
7880 end if;
7881 end Copy_Generic_Elist;
7882
7883 -----------------------
7884 -- Copy_Generic_List --
7885 -----------------------
7886
7887 function Copy_Generic_List
7888 (L : List_Id;
7889 Parent_Id : Node_Id) return List_Id
7890 is
7891 N : Node_Id;
7892 New_L : List_Id;
7893
7894 begin
7895 if Present (L) then
7896 New_L := New_List;
7897 Set_Parent (New_L, Parent_Id);
7898
7899 N := First (L);
7900 while Present (N) loop
7901 Append (Copy_Generic_Node (N, Empty, Instantiating), New_L);
7902 Next (N);
7903 end loop;
7904
7905 return New_L;
7906
7907 else
7908 return No_List;
7909 end if;
7910 end Copy_Generic_List;
7911
7912 ---------------------------
7913 -- In_Defining_Unit_Name --
7914 ---------------------------
7915
7916 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean is
7917 begin
7918 return
7919 Present (Parent (Nam))
7920 and then (Nkind (Parent (Nam)) = N_Defining_Program_Unit_Name
7921 or else
7922 (Nkind (Parent (Nam)) = N_Expanded_Name
7923 and then In_Defining_Unit_Name (Parent (Nam))));
7924 end In_Defining_Unit_Name;
7925
7926 -- Start of processing for Copy_Generic_Node
7927
7928 begin
7929 if N = Empty then
7930 return N;
7931 end if;
7932
7933 New_N := New_Copy (N);
7934
7935 -- Copy aspects if present
7936
7937 if Has_Aspects (N) then
7938 Set_Has_Aspects (New_N, False);
7939 Set_Aspect_Specifications
7940 (New_N, Copy_Generic_List (Aspect_Specifications (N), Parent_Id));
7941 end if;
7942
7943 -- If we are instantiating, we want to adjust the sloc based on the
7944 -- current S_Adjustment. However, if this is the root node of a subunit,
7945 -- we need to defer that adjustment to below (see "elsif Instantiating
7946 -- and Was_Stub"), so it comes after Create_Instantiation_Source has
7947 -- computed the adjustment.
7948
7949 if Instantiating
7950 and then not (Nkind (N) in N_Proper_Body
7951 and then Was_Originally_Stub (N))
7952 then
7953 Adjust_Instantiation_Sloc (New_N, S_Adjustment);
7954 end if;
7955
7956 if not Is_List_Member (N) then
7957 Set_Parent (New_N, Parent_Id);
7958 end if;
7959
7960 -- Special casing for identifiers and other entity names and operators
7961
7962 if Nkind_In (New_N, N_Character_Literal,
7963 N_Expanded_Name,
7964 N_Identifier,
7965 N_Operator_Symbol)
7966 or else Nkind (New_N) in N_Op
7967 then
7968 if not Instantiating then
7969
7970 -- Link both nodes in order to assign subsequently the entity of
7971 -- the copy to the original node, in case this is a global
7972 -- reference.
7973
7974 Set_Associated_Node (N, New_N);
7975
7976 -- If we are within an instantiation, this is a nested generic
7977 -- that has already been analyzed at the point of definition.
7978 -- We must preserve references that were global to the enclosing
7979 -- parent at that point. Other occurrences, whether global or
7980 -- local to the current generic, must be resolved anew, so we
7981 -- reset the entity in the generic copy. A global reference has a
7982 -- smaller depth than the parent, or else the same depth in case
7983 -- both are distinct compilation units.
7984
7985 -- A child unit is implicitly declared within the enclosing parent
7986 -- but is in fact global to it, and must be preserved.
7987
7988 -- It is also possible for Current_Instantiated_Parent to be
7989 -- defined, and for this not to be a nested generic, namely if
7990 -- the unit is loaded through Rtsfind. In that case, the entity of
7991 -- New_N is only a link to the associated node, and not a defining
7992 -- occurrence.
7993
7994 -- The entities for parent units in the defining_program_unit of a
7995 -- generic child unit are established when the context of the unit
7996 -- is first analyzed, before the generic copy is made. They are
7997 -- preserved in the copy for use in e.g. ASIS queries.
7998
7999 Ent := Entity (New_N);
8000
8001 if No (Current_Instantiated_Parent.Gen_Id) then
8002 if No (Ent)
8003 or else Nkind (Ent) /= N_Defining_Identifier
8004 or else not In_Defining_Unit_Name (N)
8005 then
8006 Set_Associated_Node (New_N, Empty);
8007 end if;
8008
8009 elsif No (Ent)
8010 or else
8011 not Nkind_In (Ent, N_Defining_Identifier,
8012 N_Defining_Character_Literal,
8013 N_Defining_Operator_Symbol)
8014 or else No (Scope (Ent))
8015 or else
8016 (Scope (Ent) = Current_Instantiated_Parent.Gen_Id
8017 and then not Is_Child_Unit (Ent))
8018 or else
8019 (Scope_Depth (Scope (Ent)) >
8020 Scope_Depth (Current_Instantiated_Parent.Gen_Id)
8021 and then
8022 Get_Source_Unit (Ent) =
8023 Get_Source_Unit (Current_Instantiated_Parent.Gen_Id))
8024 then
8025 Set_Associated_Node (New_N, Empty);
8026 end if;
8027
8028 -- Case of instantiating identifier or some other name or operator
8029
8030 else
8031 -- If the associated node is still defined, the entity in it
8032 -- is global, and must be copied to the instance. If this copy
8033 -- is being made for a body to inline, it is applied to an
8034 -- instantiated tree, and the entity is already present and
8035 -- must be also preserved.
8036
8037 declare
8038 Assoc : constant Node_Id := Get_Associated_Node (N);
8039
8040 begin
8041 if Present (Assoc) then
8042 if Nkind (Assoc) = Nkind (N) then
8043 Set_Entity (New_N, Entity (Assoc));
8044 Check_Private_View (N);
8045
8046 -- Here we deal with a very peculiar case for which the
8047 -- Has_Private_View mechanism is not sufficient, because
8048 -- the reference to the type is implicit in the tree,
8049 -- that is to say, it's not referenced from a node but
8050 -- only from another type, namely through Component_Type.
8051
8052 -- package P is
8053
8054 -- type Pt is private;
8055
8056 -- generic
8057 -- type Ft is array (Positive range <>) of Pt;
8058 -- package G is
8059 -- procedure Check (F1, F2 : Ft; Lt : Boolean);
8060 -- end G;
8061
8062 -- private
8063 -- type Pt is new Boolean;
8064 -- end P;
8065
8066 -- package body P is
8067 -- package body G is
8068 -- procedure Check (F1, F2 : Ft; Lt : Boolean) is
8069 -- begin
8070 -- if (F1 < F2) /= Lt then
8071 -- null;
8072 -- end if;
8073 -- end Check;
8074 -- end G;
8075 -- end P;
8076
8077 -- type Arr is array (Positive range <>) of P.Pt;
8078
8079 -- package Inst is new P.G (Arr);
8080
8081 -- Pt is a global type for the generic package G and it
8082 -- is not referenced in its body, but only as component
8083 -- type of Ft, which is a local type. This means that no
8084 -- references to Pt or Ft are seen during the copy of the
8085 -- body, the only reference to Pt being seen is when the
8086 -- actuals are checked by Check_Generic_Actuals, but Pt
8087 -- is still private at this point. In the end, the views
8088 -- of Pt are not switched in the body and, therefore, the
8089 -- array comparison is rejected because the component is
8090 -- still private.
8091
8092 -- Adding e.g. a dummy variable of type Pt in the body is
8093 -- sufficient to make everything work, so we generate an
8094 -- artificial reference to Pt on the fly and thus force
8095 -- the switching of views on the grounds that, if the
8096 -- comparison was accepted during the semantic analysis
8097 -- of the generic, this means that the component cannot
8098 -- have been private (see Sem_Type.Valid_Comparison_Arg).
8099
8100 if Nkind (Assoc) in N_Op_Compare
8101 and then Present (Etype (Left_Opnd (Assoc)))
8102 and then Is_Array_Type (Etype (Left_Opnd (Assoc)))
8103 and then Present (Etype (Right_Opnd (Assoc)))
8104 and then Is_Array_Type (Etype (Right_Opnd (Assoc)))
8105 then
8106 declare
8107 Ltyp : constant Entity_Id :=
8108 Etype (Left_Opnd (Assoc));
8109 Rtyp : constant Entity_Id :=
8110 Etype (Right_Opnd (Assoc));
8111 begin
8112 if Is_Private_Type (Component_Type (Ltyp)) then
8113 Check_Private_View
8114 (New_Occurrence_Of (Component_Type (Ltyp),
8115 Sloc (N)));
8116 end if;
8117 if Is_Private_Type (Component_Type (Rtyp)) then
8118 Check_Private_View
8119 (New_Occurrence_Of (Component_Type (Rtyp),
8120 Sloc (N)));
8121 end if;
8122 end;
8123
8124 -- Here is a similar case, for the Designated_Type of an
8125 -- access type that is present as target type in a type
8126 -- conversion from another access type. In this case, if
8127 -- the base types of the designated types are different
8128 -- and the conversion was accepted during the semantic
8129 -- analysis of the generic, this means that the target
8130 -- type cannot have been private (see Valid_Conversion).
8131
8132 elsif Nkind (Assoc) = N_Identifier
8133 and then Nkind (Parent (Assoc)) = N_Type_Conversion
8134 and then Subtype_Mark (Parent (Assoc)) = Assoc
8135 and then Present (Etype (Assoc))
8136 and then Is_Access_Type (Etype (Assoc))
8137 and then Present (Etype (Expression (Parent (Assoc))))
8138 and then
8139 Is_Access_Type (Etype (Expression (Parent (Assoc))))
8140 then
8141 declare
8142 Targ_Desig : constant Entity_Id :=
8143 Designated_Type (Etype (Assoc));
8144 Expr_Desig : constant Entity_Id :=
8145 Designated_Type
8146 (Etype (Expression (Parent (Assoc))));
8147 begin
8148 if Base_Type (Targ_Desig) /= Base_Type (Expr_Desig)
8149 and then Is_Private_Type (Targ_Desig)
8150 then
8151 Check_Private_View
8152 (New_Occurrence_Of (Targ_Desig, Sloc (N)));
8153 end if;
8154 end;
8155 end if;
8156
8157 -- The node is a reference to a global type and acts as the
8158 -- subtype mark of a qualified expression created in order
8159 -- to aid resolution of accidental overloading in instances.
8160 -- Since N is a reference to a type, the Associated_Node of
8161 -- N denotes an entity rather than another identifier. See
8162 -- Qualify_Universal_Operands for details.
8163
8164 elsif Nkind (N) = N_Identifier
8165 and then Nkind (Parent (N)) = N_Qualified_Expression
8166 and then Subtype_Mark (Parent (N)) = N
8167 and then Is_Qualified_Universal_Literal (Parent (N))
8168 then
8169 Set_Entity (New_N, Assoc);
8170
8171 -- The name in the call may be a selected component if the
8172 -- call has not been analyzed yet, as may be the case for
8173 -- pre/post conditions in a generic unit.
8174
8175 elsif Nkind (Assoc) = N_Function_Call
8176 and then Is_Entity_Name (Name (Assoc))
8177 then
8178 Set_Entity (New_N, Entity (Name (Assoc)));
8179
8180 elsif Nkind_In (Assoc, N_Defining_Identifier,
8181 N_Defining_Character_Literal,
8182 N_Defining_Operator_Symbol)
8183 and then Expander_Active
8184 then
8185 -- Inlining case: we are copying a tree that contains
8186 -- global entities, which are preserved in the copy to be
8187 -- used for subsequent inlining.
8188
8189 null;
8190
8191 else
8192 Set_Entity (New_N, Empty);
8193 end if;
8194 end if;
8195 end;
8196 end if;
8197
8198 -- For expanded name, we must copy the Prefix and Selector_Name
8199
8200 if Nkind (N) = N_Expanded_Name then
8201 Set_Prefix
8202 (New_N, Copy_Generic_Node (Prefix (N), New_N, Instantiating));
8203
8204 Set_Selector_Name (New_N,
8205 Copy_Generic_Node (Selector_Name (N), New_N, Instantiating));
8206
8207 -- For operators, copy the operands
8208
8209 elsif Nkind (N) in N_Op then
8210 if Nkind (N) in N_Binary_Op then
8211 Set_Left_Opnd (New_N,
8212 Copy_Generic_Node (Left_Opnd (N), New_N, Instantiating));
8213 end if;
8214
8215 Set_Right_Opnd (New_N,
8216 Copy_Generic_Node (Right_Opnd (N), New_N, Instantiating));
8217 end if;
8218
8219 -- Establish a link between an entity from the generic template and the
8220 -- corresponding entity in the generic copy to be analyzed.
8221
8222 elsif Nkind (N) in N_Entity then
8223 if not Instantiating then
8224 Set_Associated_Entity (N, New_N);
8225 end if;
8226
8227 -- Clear any existing link the copy may inherit from the replicated
8228 -- generic template entity.
8229
8230 Set_Associated_Entity (New_N, Empty);
8231
8232 -- Special casing for stubs
8233
8234 elsif Nkind (N) in N_Body_Stub then
8235
8236 -- In any case, we must copy the specification or defining
8237 -- identifier as appropriate.
8238
8239 if Nkind (N) = N_Subprogram_Body_Stub then
8240 Set_Specification (New_N,
8241 Copy_Generic_Node (Specification (N), New_N, Instantiating));
8242
8243 else
8244 Set_Defining_Identifier (New_N,
8245 Copy_Generic_Node
8246 (Defining_Identifier (N), New_N, Instantiating));
8247 end if;
8248
8249 -- If we are not instantiating, then this is where we load and
8250 -- analyze subunits, i.e. at the point where the stub occurs. A
8251 -- more permissive system might defer this analysis to the point
8252 -- of instantiation, but this seems too complicated for now.
8253
8254 if not Instantiating then
8255 declare
8256 Subunit_Name : constant Unit_Name_Type := Get_Unit_Name (N);
8257 Subunit : Node_Id;
8258 Unum : Unit_Number_Type;
8259 New_Body : Node_Id;
8260
8261 begin
8262 -- Make sure that, if it is a subunit of the main unit that is
8263 -- preprocessed and if -gnateG is specified, the preprocessed
8264 -- file will be written.
8265
8266 Lib.Analysing_Subunit_Of_Main :=
8267 Lib.In_Extended_Main_Source_Unit (N);
8268 Unum :=
8269 Load_Unit
8270 (Load_Name => Subunit_Name,
8271 Required => False,
8272 Subunit => True,
8273 Error_Node => N);
8274 Lib.Analysing_Subunit_Of_Main := False;
8275
8276 -- If the proper body is not found, a warning message will be
8277 -- emitted when analyzing the stub, or later at the point of
8278 -- instantiation. Here we just leave the stub as is.
8279
8280 if Unum = No_Unit then
8281 Subunits_Missing := True;
8282 goto Subunit_Not_Found;
8283 end if;
8284
8285 Subunit := Cunit (Unum);
8286
8287 if Nkind (Unit (Subunit)) /= N_Subunit then
8288 Error_Msg_N
8289 ("found child unit instead of expected SEPARATE subunit",
8290 Subunit);
8291 Error_Msg_Sloc := Sloc (N);
8292 Error_Msg_N ("\to complete stub #", Subunit);
8293 goto Subunit_Not_Found;
8294 end if;
8295
8296 -- We must create a generic copy of the subunit, in order to
8297 -- perform semantic analysis on it, and we must replace the
8298 -- stub in the original generic unit with the subunit, in order
8299 -- to preserve non-local references within.
8300
8301 -- Only the proper body needs to be copied. Library_Unit and
8302 -- context clause are simply inherited by the generic copy.
8303 -- Note that the copy (which may be recursive if there are
8304 -- nested subunits) must be done first, before attaching it to
8305 -- the enclosing generic.
8306
8307 New_Body :=
8308 Copy_Generic_Node
8309 (Proper_Body (Unit (Subunit)),
8310 Empty, Instantiating => False);
8311
8312 -- Now place the original proper body in the original generic
8313 -- unit. This is a body, not a compilation unit.
8314
8315 Rewrite (N, Proper_Body (Unit (Subunit)));
8316 Set_Is_Compilation_Unit (Defining_Entity (N), False);
8317 Set_Was_Originally_Stub (N);
8318
8319 -- Finally replace the body of the subunit with its copy, and
8320 -- make this new subunit into the library unit of the generic
8321 -- copy, which does not have stubs any longer.
8322
8323 Set_Proper_Body (Unit (Subunit), New_Body);
8324 Set_Library_Unit (New_N, Subunit);
8325 Inherit_Context (Unit (Subunit), N);
8326 end;
8327
8328 -- If we are instantiating, this must be an error case, since
8329 -- otherwise we would have replaced the stub node by the proper body
8330 -- that corresponds. So just ignore it in the copy (i.e. we have
8331 -- copied it, and that is good enough).
8332
8333 else
8334 null;
8335 end if;
8336
8337 <<Subunit_Not_Found>> null;
8338
8339 -- If the node is a compilation unit, it is the subunit of a stub, which
8340 -- has been loaded already (see code below). In this case, the library
8341 -- unit field of N points to the parent unit (which is a compilation
8342 -- unit) and need not (and cannot) be copied.
8343
8344 -- When the proper body of the stub is analyzed, the library_unit link
8345 -- is used to establish the proper context (see sem_ch10).
8346
8347 -- The other fields of a compilation unit are copied as usual
8348
8349 elsif Nkind (N) = N_Compilation_Unit then
8350
8351 -- This code can only be executed when not instantiating, because in
8352 -- the copy made for an instantiation, the compilation unit node has
8353 -- disappeared at the point that a stub is replaced by its proper
8354 -- body.
8355
8356 pragma Assert (not Instantiating);
8357
8358 Set_Context_Items (New_N,
8359 Copy_Generic_List (Context_Items (N), New_N));
8360
8361 Set_Unit (New_N,
8362 Copy_Generic_Node (Unit (N), New_N, Instantiating => False));
8363
8364 Set_First_Inlined_Subprogram (New_N,
8365 Copy_Generic_Node
8366 (First_Inlined_Subprogram (N), New_N, Instantiating => False));
8367
8368 Set_Aux_Decls_Node
8369 (New_N,
8370 Copy_Generic_Node
8371 (Aux_Decls_Node (N), New_N, Instantiating => False));
8372
8373 -- For an assignment node, the assignment is known to be semantically
8374 -- legal if we are instantiating the template. This avoids incorrect
8375 -- diagnostics in generated code.
8376
8377 elsif Nkind (N) = N_Assignment_Statement then
8378
8379 -- Copy name and expression fields in usual manner
8380
8381 Set_Name (New_N,
8382 Copy_Generic_Node (Name (N), New_N, Instantiating));
8383
8384 Set_Expression (New_N,
8385 Copy_Generic_Node (Expression (N), New_N, Instantiating));
8386
8387 if Instantiating then
8388 Set_Assignment_OK (Name (New_N), True);
8389 end if;
8390
8391 elsif Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
8392 if not Instantiating then
8393 Set_Associated_Node (N, New_N);
8394
8395 else
8396 if Present (Get_Associated_Node (N))
8397 and then Nkind (Get_Associated_Node (N)) = Nkind (N)
8398 then
8399 -- In the generic the aggregate has some composite type. If at
8400 -- the point of instantiation the type has a private view,
8401 -- install the full view (and that of its ancestors, if any).
8402
8403 declare
8404 T : Entity_Id := (Etype (Get_Associated_Node (New_N)));
8405 Rt : Entity_Id;
8406
8407 begin
8408 if Present (T) and then Is_Private_Type (T) then
8409 Switch_View (T);
8410 end if;
8411
8412 if Present (T)
8413 and then Is_Tagged_Type (T)
8414 and then Is_Derived_Type (T)
8415 then
8416 Rt := Root_Type (T);
8417
8418 loop
8419 T := Etype (T);
8420
8421 if Is_Private_Type (T) then
8422 Switch_View (T);
8423 end if;
8424
8425 exit when T = Rt;
8426 end loop;
8427 end if;
8428 end;
8429 end if;
8430 end if;
8431
8432 -- Do not copy the associated node, which points to the generic copy
8433 -- of the aggregate.
8434
8435 declare
8436 use Atree.Unchecked_Access;
8437 -- This code section is part of the implementation of an untyped
8438 -- tree traversal, so it needs direct access to node fields.
8439
8440 begin
8441 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
8442 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
8443 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
8444 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
8445 end;
8446
8447 -- Allocators do not have an identifier denoting the access type, so we
8448 -- must locate it through the expression to check whether the views are
8449 -- consistent.
8450
8451 elsif Nkind (N) = N_Allocator
8452 and then Nkind (Expression (N)) = N_Qualified_Expression
8453 and then Is_Entity_Name (Subtype_Mark (Expression (N)))
8454 and then Instantiating
8455 then
8456 declare
8457 T : constant Node_Id :=
8458 Get_Associated_Node (Subtype_Mark (Expression (N)));
8459 Acc_T : Entity_Id;
8460
8461 begin
8462 if Present (T) then
8463
8464 -- Retrieve the allocator node in the generic copy
8465
8466 Acc_T := Etype (Parent (Parent (T)));
8467
8468 if Present (Acc_T) and then Is_Private_Type (Acc_T) then
8469 Switch_View (Acc_T);
8470 end if;
8471 end if;
8472
8473 Copy_Descendants;
8474 end;
8475
8476 -- For a proper body, we must catch the case of a proper body that
8477 -- replaces a stub. This represents the point at which a separate
8478 -- compilation unit, and hence template file, may be referenced, so we
8479 -- must make a new source instantiation entry for the template of the
8480 -- subunit, and ensure that all nodes in the subunit are adjusted using
8481 -- this new source instantiation entry.
8482
8483 elsif Nkind (N) in N_Proper_Body then
8484 declare
8485 Save_Adjustment : constant Sloc_Adjustment := S_Adjustment;
8486 begin
8487 if Instantiating and then Was_Originally_Stub (N) then
8488 Create_Instantiation_Source
8489 (Instantiation_Node,
8490 Defining_Entity (N),
8491 S_Adjustment);
8492
8493 Adjust_Instantiation_Sloc (New_N, S_Adjustment);
8494 end if;
8495
8496 -- Now copy the fields of the proper body, using the new
8497 -- adjustment factor if one was needed as per test above.
8498
8499 Copy_Descendants;
8500
8501 -- Restore the original adjustment factor
8502
8503 S_Adjustment := Save_Adjustment;
8504 end;
8505
8506 elsif Nkind (N) = N_Pragma and then Instantiating then
8507
8508 -- Do not copy Comment or Ident pragmas their content is relevant to
8509 -- the generic unit, not to the instantiating unit.
8510
8511 if Nam_In (Pragma_Name_Unmapped (N), Name_Comment, Name_Ident) then
8512 New_N := Make_Null_Statement (Sloc (N));
8513
8514 -- Do not copy pragmas generated from aspects because the pragmas do
8515 -- not carry any semantic information, plus they will be regenerated
8516 -- in the instance.
8517
8518 -- However, generating C we need to copy them since postconditions
8519 -- are inlined by the front end, and the front-end inlining machinery
8520 -- relies on this routine to perform inlining.
8521
8522 elsif From_Aspect_Specification (N)
8523 and then not Modify_Tree_For_C
8524 then
8525 New_N := Make_Null_Statement (Sloc (N));
8526
8527 else
8528 Copy_Descendants;
8529 end if;
8530
8531 elsif Nkind_In (N, N_Integer_Literal, N_Real_Literal) then
8532
8533 -- No descendant fields need traversing
8534
8535 null;
8536
8537 elsif Nkind (N) = N_String_Literal
8538 and then Present (Etype (N))
8539 and then Instantiating
8540 then
8541 -- If the string is declared in an outer scope, the string_literal
8542 -- subtype created for it may have the wrong scope. Force reanalysis
8543 -- of the constant to generate a new itype in the proper context.
8544
8545 Set_Etype (New_N, Empty);
8546 Set_Analyzed (New_N, False);
8547
8548 -- For the remaining nodes, copy their descendants recursively
8549
8550 else
8551 Copy_Descendants;
8552
8553 if Instantiating and then Nkind (N) = N_Subprogram_Body then
8554 Set_Generic_Parent (Specification (New_N), N);
8555
8556 -- Should preserve Corresponding_Spec??? (12.3(14))
8557 end if;
8558 end if;
8559
8560 -- Propagate dimensions if present, so that they are reflected in the
8561 -- instance.
8562
8563 if Nkind (N) in N_Has_Etype
8564 and then (Nkind (N) in N_Op or else Is_Entity_Name (N))
8565 and then Present (Etype (N))
8566 and then Is_Floating_Point_Type (Etype (N))
8567 and then Has_Dimension_System (Etype (N))
8568 then
8569 Copy_Dimensions (N, New_N);
8570 end if;
8571
8572 return New_N;
8573 end Copy_Generic_Node;
8574
8575 ----------------------------
8576 -- Denotes_Formal_Package --
8577 ----------------------------
8578
8579 function Denotes_Formal_Package
8580 (Pack : Entity_Id;
8581 On_Exit : Boolean := False;
8582 Instance : Entity_Id := Empty) return Boolean
8583 is
8584 Par : Entity_Id;
8585 Scop : constant Entity_Id := Scope (Pack);
8586 E : Entity_Id;
8587
8588 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean;
8589 -- The package in question may be an actual for a previous formal
8590 -- package P of the current instance, so examine its actuals as well.
8591 -- This must be recursive over other formal packages.
8592
8593 ----------------------------------
8594 -- Is_Actual_Of_Previous_Formal --
8595 ----------------------------------
8596
8597 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean is
8598 E1 : Entity_Id;
8599
8600 begin
8601 E1 := First_Entity (P);
8602 while Present (E1) and then E1 /= Instance loop
8603 if Ekind (E1) = E_Package
8604 and then Nkind (Parent (E1)) = N_Package_Renaming_Declaration
8605 then
8606 if Renamed_Object (E1) = Pack then
8607 return True;
8608
8609 elsif E1 = P or else Renamed_Object (E1) = P then
8610 return False;
8611
8612 elsif Is_Actual_Of_Previous_Formal (E1) then
8613 return True;
8614 end if;
8615 end if;
8616
8617 Next_Entity (E1);
8618 end loop;
8619
8620 return False;
8621 end Is_Actual_Of_Previous_Formal;
8622
8623 -- Start of processing for Denotes_Formal_Package
8624
8625 begin
8626 if On_Exit then
8627 Par :=
8628 Instance_Envs.Table
8629 (Instance_Envs.Last).Instantiated_Parent.Act_Id;
8630 else
8631 Par := Current_Instantiated_Parent.Act_Id;
8632 end if;
8633
8634 if Ekind (Scop) = E_Generic_Package
8635 or else Nkind (Unit_Declaration_Node (Scop)) =
8636 N_Generic_Subprogram_Declaration
8637 then
8638 return True;
8639
8640 elsif Nkind (Original_Node (Unit_Declaration_Node (Pack))) =
8641 N_Formal_Package_Declaration
8642 then
8643 return True;
8644
8645 elsif No (Par) then
8646 return False;
8647
8648 else
8649 -- Check whether this package is associated with a formal package of
8650 -- the enclosing instantiation. Iterate over the list of renamings.
8651
8652 E := First_Entity (Par);
8653 while Present (E) loop
8654 if Ekind (E) /= E_Package
8655 or else Nkind (Parent (E)) /= N_Package_Renaming_Declaration
8656 then
8657 null;
8658
8659 elsif Renamed_Object (E) = Par then
8660 return False;
8661
8662 elsif Renamed_Object (E) = Pack then
8663 return True;
8664
8665 elsif Is_Actual_Of_Previous_Formal (E) then
8666 return True;
8667
8668 end if;
8669
8670 Next_Entity (E);
8671 end loop;
8672
8673 return False;
8674 end if;
8675 end Denotes_Formal_Package;
8676
8677 -----------------
8678 -- End_Generic --
8679 -----------------
8680
8681 procedure End_Generic is
8682 begin
8683 -- ??? More things could be factored out in this routine. Should
8684 -- probably be done at a later stage.
8685
8686 Inside_A_Generic := Generic_Flags.Table (Generic_Flags.Last);
8687 Generic_Flags.Decrement_Last;
8688
8689 Expander_Mode_Restore;
8690 end End_Generic;
8691
8692 -------------
8693 -- Earlier --
8694 -------------
8695
8696 function Earlier (N1, N2 : Node_Id) return Boolean is
8697 procedure Find_Depth (P : in out Node_Id; D : in out Integer);
8698 -- Find distance from given node to enclosing compilation unit
8699
8700 ----------------
8701 -- Find_Depth --
8702 ----------------
8703
8704 procedure Find_Depth (P : in out Node_Id; D : in out Integer) is
8705 begin
8706 while Present (P)
8707 and then Nkind (P) /= N_Compilation_Unit
8708 loop
8709 P := True_Parent (P);
8710 D := D + 1;
8711 end loop;
8712 end Find_Depth;
8713
8714 -- Local declarations
8715
8716 D1 : Integer := 0;
8717 D2 : Integer := 0;
8718 P1 : Node_Id := N1;
8719 P2 : Node_Id := N2;
8720 T1 : Source_Ptr;
8721 T2 : Source_Ptr;
8722
8723 -- Start of processing for Earlier
8724
8725 begin
8726 Find_Depth (P1, D1);
8727 Find_Depth (P2, D2);
8728
8729 if P1 /= P2 then
8730 return False;
8731 else
8732 P1 := N1;
8733 P2 := N2;
8734 end if;
8735
8736 while D1 > D2 loop
8737 P1 := True_Parent (P1);
8738 D1 := D1 - 1;
8739 end loop;
8740
8741 while D2 > D1 loop
8742 P2 := True_Parent (P2);
8743 D2 := D2 - 1;
8744 end loop;
8745
8746 -- At this point P1 and P2 are at the same distance from the root.
8747 -- We examine their parents until we find a common declarative list.
8748 -- If we reach the root, N1 and N2 do not descend from the same
8749 -- declarative list (e.g. one is nested in the declarative part and
8750 -- the other is in a block in the statement part) and the earlier
8751 -- one is already frozen.
8752
8753 while not Is_List_Member (P1)
8754 or else not Is_List_Member (P2)
8755 or else List_Containing (P1) /= List_Containing (P2)
8756 loop
8757 P1 := True_Parent (P1);
8758 P2 := True_Parent (P2);
8759
8760 if Nkind (Parent (P1)) = N_Subunit then
8761 P1 := Corresponding_Stub (Parent (P1));
8762 end if;
8763
8764 if Nkind (Parent (P2)) = N_Subunit then
8765 P2 := Corresponding_Stub (Parent (P2));
8766 end if;
8767
8768 if P1 = P2 then
8769 return False;
8770 end if;
8771 end loop;
8772
8773 -- Expanded code usually shares the source location of the original
8774 -- construct it was generated for. This however may not necessarily
8775 -- reflect the true location of the code within the tree.
8776
8777 -- Before comparing the slocs of the two nodes, make sure that we are
8778 -- working with correct source locations. Assume that P1 is to the left
8779 -- of P2. If either one does not come from source, traverse the common
8780 -- list heading towards the other node and locate the first source
8781 -- statement.
8782
8783 -- P1 P2
8784 -- ----+===+===+--------------+===+===+----
8785 -- expanded code expanded code
8786
8787 if not Comes_From_Source (P1) then
8788 while Present (P1) loop
8789
8790 -- Neither P2 nor a source statement were located during the
8791 -- search. If we reach the end of the list, then P1 does not
8792 -- occur earlier than P2.
8793
8794 -- ---->
8795 -- start --- P2 ----- P1 --- end
8796
8797 if No (Next (P1)) then
8798 return False;
8799
8800 -- We encounter P2 while going to the right of the list. This
8801 -- means that P1 does indeed appear earlier.
8802
8803 -- ---->
8804 -- start --- P1 ===== P2 --- end
8805 -- expanded code in between
8806
8807 elsif P1 = P2 then
8808 return True;
8809
8810 -- No need to look any further since we have located a source
8811 -- statement.
8812
8813 elsif Comes_From_Source (P1) then
8814 exit;
8815 end if;
8816
8817 -- Keep going right
8818
8819 Next (P1);
8820 end loop;
8821 end if;
8822
8823 if not Comes_From_Source (P2) then
8824 while Present (P2) loop
8825
8826 -- Neither P1 nor a source statement were located during the
8827 -- search. If we reach the start of the list, then P1 does not
8828 -- occur earlier than P2.
8829
8830 -- <----
8831 -- start --- P2 --- P1 --- end
8832
8833 if No (Prev (P2)) then
8834 return False;
8835
8836 -- We encounter P1 while going to the left of the list. This
8837 -- means that P1 does indeed appear earlier.
8838
8839 -- <----
8840 -- start --- P1 ===== P2 --- end
8841 -- expanded code in between
8842
8843 elsif P2 = P1 then
8844 return True;
8845
8846 -- No need to look any further since we have located a source
8847 -- statement.
8848
8849 elsif Comes_From_Source (P2) then
8850 exit;
8851 end if;
8852
8853 -- Keep going left
8854
8855 Prev (P2);
8856 end loop;
8857 end if;
8858
8859 -- At this point either both nodes came from source or we approximated
8860 -- their source locations through neighboring source statements.
8861
8862 T1 := Top_Level_Location (Sloc (P1));
8863 T2 := Top_Level_Location (Sloc (P2));
8864
8865 -- When two nodes come from the same instance, they have identical top
8866 -- level locations. To determine proper relation within the tree, check
8867 -- their locations within the template.
8868
8869 if T1 = T2 then
8870 return Sloc (P1) < Sloc (P2);
8871
8872 -- The two nodes either come from unrelated instances or do not come
8873 -- from instantiated code at all.
8874
8875 else
8876 return T1 < T2;
8877 end if;
8878 end Earlier;
8879
8880 ----------------------
8881 -- Find_Actual_Type --
8882 ----------------------
8883
8884 function Find_Actual_Type
8885 (Typ : Entity_Id;
8886 Gen_Type : Entity_Id) return Entity_Id
8887 is
8888 Gen_Scope : constant Entity_Id := Scope (Gen_Type);
8889 T : Entity_Id;
8890
8891 begin
8892 -- Special processing only applies to child units
8893
8894 if not Is_Child_Unit (Gen_Scope) then
8895 return Get_Instance_Of (Typ);
8896
8897 -- If designated or component type is itself a formal of the child unit,
8898 -- its instance is available.
8899
8900 elsif Scope (Typ) = Gen_Scope then
8901 return Get_Instance_Of (Typ);
8902
8903 -- If the array or access type is not declared in the parent unit,
8904 -- no special processing needed.
8905
8906 elsif not Is_Generic_Type (Typ)
8907 and then Scope (Gen_Scope) /= Scope (Typ)
8908 then
8909 return Get_Instance_Of (Typ);
8910
8911 -- Otherwise, retrieve designated or component type by visibility
8912
8913 else
8914 T := Current_Entity (Typ);
8915 while Present (T) loop
8916 if In_Open_Scopes (Scope (T)) then
8917 return T;
8918 elsif Is_Generic_Actual_Type (T) then
8919 return T;
8920 end if;
8921
8922 T := Homonym (T);
8923 end loop;
8924
8925 return Typ;
8926 end if;
8927 end Find_Actual_Type;
8928
8929 ----------------------------
8930 -- Freeze_Subprogram_Body --
8931 ----------------------------
8932
8933 procedure Freeze_Subprogram_Body
8934 (Inst_Node : Node_Id;
8935 Gen_Body : Node_Id;
8936 Pack_Id : Entity_Id)
8937 is
8938 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
8939 Par : constant Entity_Id := Scope (Gen_Unit);
8940 E_G_Id : Entity_Id;
8941 Enc_G : Entity_Id;
8942 Enc_I : Node_Id;
8943 F_Node : Node_Id;
8944
8945 function Enclosing_Package_Body (N : Node_Id) return Node_Id;
8946 -- Find innermost package body that encloses the given node, and which
8947 -- is not a compilation unit. Freeze nodes for the instance, or for its
8948 -- enclosing body, may be inserted after the enclosing_body of the
8949 -- generic unit. Used to determine proper placement of freeze node for
8950 -- both package and subprogram instances.
8951
8952 function Package_Freeze_Node (B : Node_Id) return Node_Id;
8953 -- Find entity for given package body, and locate or create a freeze
8954 -- node for it.
8955
8956 ----------------------------
8957 -- Enclosing_Package_Body --
8958 ----------------------------
8959
8960 function Enclosing_Package_Body (N : Node_Id) return Node_Id is
8961 P : Node_Id;
8962
8963 begin
8964 P := Parent (N);
8965 while Present (P)
8966 and then Nkind (Parent (P)) /= N_Compilation_Unit
8967 loop
8968 if Nkind (P) = N_Package_Body then
8969 if Nkind (Parent (P)) = N_Subunit then
8970 return Corresponding_Stub (Parent (P));
8971 else
8972 return P;
8973 end if;
8974 end if;
8975
8976 P := True_Parent (P);
8977 end loop;
8978
8979 return Empty;
8980 end Enclosing_Package_Body;
8981
8982 -------------------------
8983 -- Package_Freeze_Node --
8984 -------------------------
8985
8986 function Package_Freeze_Node (B : Node_Id) return Node_Id is
8987 Id : Entity_Id;
8988
8989 begin
8990 if Nkind (B) = N_Package_Body then
8991 Id := Corresponding_Spec (B);
8992 else pragma Assert (Nkind (B) = N_Package_Body_Stub);
8993 Id := Corresponding_Spec (Proper_Body (Unit (Library_Unit (B))));
8994 end if;
8995
8996 Ensure_Freeze_Node (Id);
8997 return Freeze_Node (Id);
8998 end Package_Freeze_Node;
8999
9000 -- Start of processing for Freeze_Subprogram_Body
9001
9002 begin
9003 -- If the instance and the generic body appear within the same unit, and
9004 -- the instance precedes the generic, the freeze node for the instance
9005 -- must appear after that of the generic. If the generic is nested
9006 -- within another instance I2, then current instance must be frozen
9007 -- after I2. In both cases, the freeze nodes are those of enclosing
9008 -- packages. Otherwise, the freeze node is placed at the end of the
9009 -- current declarative part.
9010
9011 Enc_G := Enclosing_Package_Body (Gen_Body);
9012 Enc_I := Enclosing_Package_Body (Inst_Node);
9013 Ensure_Freeze_Node (Pack_Id);
9014 F_Node := Freeze_Node (Pack_Id);
9015
9016 if Is_Generic_Instance (Par)
9017 and then Present (Freeze_Node (Par))
9018 and then In_Same_Declarative_Part
9019 (Parent (Freeze_Node (Par)), Inst_Node)
9020 then
9021 -- The parent was a premature instantiation. Insert freeze node at
9022 -- the end the current declarative part.
9023
9024 if Is_Known_Guaranteed_ABE (Get_Unit_Instantiation_Node (Par)) then
9025 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
9026
9027 -- Handle the following case:
9028 --
9029 -- package Parent_Inst is new ...
9030 -- Parent_Inst []
9031 --
9032 -- procedure P ... -- this body freezes Parent_Inst
9033 --
9034 -- package Inst is new ...
9035 --
9036 -- In this particular scenario, the freeze node for Inst must be
9037 -- inserted in the same manner as that of Parent_Inst - before the
9038 -- next source body or at the end of the declarative list (body not
9039 -- available). If body P did not exist and Parent_Inst was frozen
9040 -- after Inst, either by a body following Inst or at the end of the
9041 -- declarative region, the freeze node for Inst must be inserted
9042 -- after that of Parent_Inst. This relation is established by
9043 -- comparing the Slocs of Parent_Inst freeze node and Inst.
9044
9045 elsif List_Containing (Get_Unit_Instantiation_Node (Par)) =
9046 List_Containing (Inst_Node)
9047 and then Sloc (Freeze_Node (Par)) < Sloc (Inst_Node)
9048 then
9049 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
9050
9051 else
9052 Insert_After (Freeze_Node (Par), F_Node);
9053 end if;
9054
9055 -- The body enclosing the instance should be frozen after the body that
9056 -- includes the generic, because the body of the instance may make
9057 -- references to entities therein. If the two are not in the same
9058 -- declarative part, or if the one enclosing the instance is frozen
9059 -- already, freeze the instance at the end of the current declarative
9060 -- part.
9061
9062 elsif Is_Generic_Instance (Par)
9063 and then Present (Freeze_Node (Par))
9064 and then Present (Enc_I)
9065 then
9066 if In_Same_Declarative_Part (Parent (Freeze_Node (Par)), Enc_I)
9067 or else
9068 (Nkind (Enc_I) = N_Package_Body
9069 and then In_Same_Declarative_Part
9070 (Parent (Freeze_Node (Par)), Parent (Enc_I)))
9071 then
9072 -- The enclosing package may contain several instances. Rather
9073 -- than computing the earliest point at which to insert its freeze
9074 -- node, we place it at the end of the declarative part of the
9075 -- parent of the generic.
9076
9077 Insert_Freeze_Node_For_Instance
9078 (Freeze_Node (Par), Package_Freeze_Node (Enc_I));
9079 end if;
9080
9081 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
9082
9083 elsif Present (Enc_G)
9084 and then Present (Enc_I)
9085 and then Enc_G /= Enc_I
9086 and then Earlier (Inst_Node, Gen_Body)
9087 then
9088 if Nkind (Enc_G) = N_Package_Body then
9089 E_G_Id :=
9090 Corresponding_Spec (Enc_G);
9091 else pragma Assert (Nkind (Enc_G) = N_Package_Body_Stub);
9092 E_G_Id :=
9093 Corresponding_Spec (Proper_Body (Unit (Library_Unit (Enc_G))));
9094 end if;
9095
9096 -- Freeze package that encloses instance, and place node after the
9097 -- package that encloses generic. If enclosing package is already
9098 -- frozen we have to assume it is at the proper place. This may be a
9099 -- potential ABE that requires dynamic checking. Do not add a freeze
9100 -- node if the package that encloses the generic is inside the body
9101 -- that encloses the instance, because the freeze node would be in
9102 -- the wrong scope. Additional contortions needed if the bodies are
9103 -- within a subunit.
9104
9105 declare
9106 Enclosing_Body : Node_Id;
9107
9108 begin
9109 if Nkind (Enc_I) = N_Package_Body_Stub then
9110 Enclosing_Body := Proper_Body (Unit (Library_Unit (Enc_I)));
9111 else
9112 Enclosing_Body := Enc_I;
9113 end if;
9114
9115 if Parent (List_Containing (Enc_G)) /= Enclosing_Body then
9116 Insert_Freeze_Node_For_Instance
9117 (Enc_G, Package_Freeze_Node (Enc_I));
9118 end if;
9119 end;
9120
9121 -- Freeze enclosing subunit before instance
9122
9123 Ensure_Freeze_Node (E_G_Id);
9124
9125 if not Is_List_Member (Freeze_Node (E_G_Id)) then
9126 Insert_After (Enc_G, Freeze_Node (E_G_Id));
9127 end if;
9128
9129 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
9130
9131 else
9132 -- If none of the above, insert freeze node at the end of the current
9133 -- declarative part.
9134
9135 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
9136 end if;
9137 end Freeze_Subprogram_Body;
9138
9139 ----------------
9140 -- Get_Gen_Id --
9141 ----------------
9142
9143 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id is
9144 begin
9145 return Generic_Renamings.Table (E).Gen_Id;
9146 end Get_Gen_Id;
9147
9148 ---------------------
9149 -- Get_Instance_Of --
9150 ---------------------
9151
9152 function Get_Instance_Of (A : Entity_Id) return Entity_Id is
9153 Res : constant Assoc_Ptr := Generic_Renamings_HTable.Get (A);
9154
9155 begin
9156 if Res /= Assoc_Null then
9157 return Generic_Renamings.Table (Res).Act_Id;
9158
9159 else
9160 -- On exit, entity is not instantiated: not a generic parameter, or
9161 -- else parameter of an inner generic unit.
9162
9163 return A;
9164 end if;
9165 end Get_Instance_Of;
9166
9167 ---------------------------------
9168 -- Get_Unit_Instantiation_Node --
9169 ---------------------------------
9170
9171 function Get_Unit_Instantiation_Node (A : Entity_Id) return Node_Id is
9172 Decl : Node_Id := Unit_Declaration_Node (A);
9173 Inst : Node_Id;
9174
9175 begin
9176 -- If the Package_Instantiation attribute has been set on the package
9177 -- entity, then use it directly when it (or its Original_Node) refers
9178 -- to an N_Package_Instantiation node. In principle it should be
9179 -- possible to have this field set in all cases, which should be
9180 -- investigated, and would allow this function to be significantly
9181 -- simplified. ???
9182
9183 Inst := Package_Instantiation (A);
9184
9185 if Present (Inst) then
9186 if Nkind (Inst) = N_Package_Instantiation then
9187 return Inst;
9188
9189 elsif Nkind (Original_Node (Inst)) = N_Package_Instantiation then
9190 return Original_Node (Inst);
9191 end if;
9192 end if;
9193
9194 -- If the instantiation is a compilation unit that does not need body
9195 -- then the instantiation node has been rewritten as a package
9196 -- declaration for the instance, and we return the original node.
9197
9198 -- If it is a compilation unit and the instance node has not been
9199 -- rewritten, then it is still the unit of the compilation. Finally, if
9200 -- a body is present, this is a parent of the main unit whose body has
9201 -- been compiled for inlining purposes, and the instantiation node has
9202 -- been rewritten with the instance body.
9203
9204 -- Otherwise the instantiation node appears after the declaration. If
9205 -- the entity is a formal package, the declaration may have been
9206 -- rewritten as a generic declaration (in the case of a formal with box)
9207 -- or left as a formal package declaration if it has actuals, and is
9208 -- found with a forward search.
9209
9210 if Nkind (Parent (Decl)) = N_Compilation_Unit then
9211 if Nkind (Decl) = N_Package_Declaration
9212 and then Present (Corresponding_Body (Decl))
9213 then
9214 Decl := Unit_Declaration_Node (Corresponding_Body (Decl));
9215 end if;
9216
9217 if Nkind (Original_Node (Decl)) in N_Generic_Instantiation then
9218 return Original_Node (Decl);
9219 else
9220 return Unit (Parent (Decl));
9221 end if;
9222
9223 elsif Nkind (Decl) = N_Package_Declaration
9224 and then Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration
9225 then
9226 return Original_Node (Decl);
9227
9228 else
9229 Inst := Next (Decl);
9230 while not Nkind_In (Inst, N_Formal_Package_Declaration,
9231 N_Function_Instantiation,
9232 N_Package_Instantiation,
9233 N_Procedure_Instantiation)
9234 loop
9235 Next (Inst);
9236 end loop;
9237
9238 return Inst;
9239 end if;
9240 end Get_Unit_Instantiation_Node;
9241
9242 ------------------------
9243 -- Has_Been_Exchanged --
9244 ------------------------
9245
9246 function Has_Been_Exchanged (E : Entity_Id) return Boolean is
9247 Next : Elmt_Id;
9248
9249 begin
9250 Next := First_Elmt (Exchanged_Views);
9251 while Present (Next) loop
9252 if Full_View (Node (Next)) = E then
9253 return True;
9254 end if;
9255
9256 Next_Elmt (Next);
9257 end loop;
9258
9259 return False;
9260 end Has_Been_Exchanged;
9261
9262 -------------------
9263 -- Has_Contracts --
9264 -------------------
9265
9266 function Has_Contracts (Decl : Node_Id) return Boolean is
9267 A_List : constant List_Id := Aspect_Specifications (Decl);
9268 A_Spec : Node_Id;
9269 A_Id : Aspect_Id;
9270 begin
9271 if No (A_List) then
9272 return False;
9273 else
9274 A_Spec := First (A_List);
9275 while Present (A_Spec) loop
9276 A_Id := Get_Aspect_Id (A_Spec);
9277 if A_Id = Aspect_Pre or else A_Id = Aspect_Post then
9278 return True;
9279 end if;
9280
9281 Next (A_Spec);
9282 end loop;
9283
9284 return False;
9285 end if;
9286 end Has_Contracts;
9287
9288 ----------
9289 -- Hash --
9290 ----------
9291
9292 function Hash (F : Entity_Id) return HTable_Range is
9293 begin
9294 return HTable_Range (F mod HTable_Size);
9295 end Hash;
9296
9297 ------------------------
9298 -- Hide_Current_Scope --
9299 ------------------------
9300
9301 procedure Hide_Current_Scope is
9302 C : constant Entity_Id := Current_Scope;
9303 E : Entity_Id;
9304
9305 begin
9306 Set_Is_Hidden_Open_Scope (C);
9307
9308 E := First_Entity (C);
9309 while Present (E) loop
9310 if Is_Immediately_Visible (E) then
9311 Set_Is_Immediately_Visible (E, False);
9312 Append_Elmt (E, Hidden_Entities);
9313 end if;
9314
9315 Next_Entity (E);
9316 end loop;
9317
9318 -- Make the scope name invisible as well. This is necessary, but might
9319 -- conflict with calls to Rtsfind later on, in case the scope is a
9320 -- predefined one. There is no clean solution to this problem, so for
9321 -- now we depend on the user not redefining Standard itself in one of
9322 -- the parent units.
9323
9324 if Is_Immediately_Visible (C) and then C /= Standard_Standard then
9325 Set_Is_Immediately_Visible (C, False);
9326 Append_Elmt (C, Hidden_Entities);
9327 end if;
9328
9329 end Hide_Current_Scope;
9330
9331 --------------
9332 -- Init_Env --
9333 --------------
9334
9335 procedure Init_Env is
9336 Saved : Instance_Env;
9337
9338 begin
9339 Saved.Instantiated_Parent := Current_Instantiated_Parent;
9340 Saved.Exchanged_Views := Exchanged_Views;
9341 Saved.Hidden_Entities := Hidden_Entities;
9342 Saved.Current_Sem_Unit := Current_Sem_Unit;
9343 Saved.Parent_Unit_Visible := Parent_Unit_Visible;
9344 Saved.Instance_Parent_Unit := Instance_Parent_Unit;
9345
9346 -- Save configuration switches. These may be reset if the unit is a
9347 -- predefined unit, and the current mode is not Ada 2005.
9348
9349 Saved.Switches := Save_Config_Switches;
9350
9351 Instance_Envs.Append (Saved);
9352
9353 Exchanged_Views := New_Elmt_List;
9354 Hidden_Entities := New_Elmt_List;
9355
9356 -- Make dummy entry for Instantiated parent. If generic unit is legal,
9357 -- this is set properly in Set_Instance_Env.
9358
9359 Current_Instantiated_Parent :=
9360 (Current_Scope, Current_Scope, Assoc_Null);
9361 end Init_Env;
9362
9363 ---------------------
9364 -- In_Main_Context --
9365 ---------------------
9366
9367 function In_Main_Context (E : Entity_Id) return Boolean is
9368 Context : List_Id;
9369 Clause : Node_Id;
9370 Nam : Node_Id;
9371
9372 begin
9373 if not Is_Compilation_Unit (E)
9374 or else Ekind (E) /= E_Package
9375 or else In_Private_Part (E)
9376 then
9377 return False;
9378 end if;
9379
9380 Context := Context_Items (Cunit (Main_Unit));
9381
9382 Clause := First (Context);
9383 while Present (Clause) loop
9384 if Nkind (Clause) = N_With_Clause then
9385 Nam := Name (Clause);
9386
9387 -- If the current scope is part of the context of the main unit,
9388 -- analysis of the corresponding with_clause is not complete, and
9389 -- the entity is not set. We use the Chars field directly, which
9390 -- might produce false positives in rare cases, but guarantees
9391 -- that we produce all the instance bodies we will need.
9392
9393 if (Is_Entity_Name (Nam) and then Chars (Nam) = Chars (E))
9394 or else (Nkind (Nam) = N_Selected_Component
9395 and then Chars (Selector_Name (Nam)) = Chars (E))
9396 then
9397 return True;
9398 end if;
9399 end if;
9400
9401 Next (Clause);
9402 end loop;
9403
9404 return False;
9405 end In_Main_Context;
9406
9407 ---------------------
9408 -- Inherit_Context --
9409 ---------------------
9410
9411 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id) is
9412 Current_Context : List_Id;
9413 Current_Unit : Node_Id;
9414 Item : Node_Id;
9415 New_I : Node_Id;
9416
9417 Clause : Node_Id;
9418 OK : Boolean;
9419 Lib_Unit : Node_Id;
9420
9421 begin
9422 if Nkind (Parent (Gen_Decl)) = N_Compilation_Unit then
9423
9424 -- The inherited context is attached to the enclosing compilation
9425 -- unit. This is either the main unit, or the declaration for the
9426 -- main unit (in case the instantiation appears within the package
9427 -- declaration and the main unit is its body).
9428
9429 Current_Unit := Parent (Inst);
9430 while Present (Current_Unit)
9431 and then Nkind (Current_Unit) /= N_Compilation_Unit
9432 loop
9433 Current_Unit := Parent (Current_Unit);
9434 end loop;
9435
9436 Current_Context := Context_Items (Current_Unit);
9437
9438 Item := First (Context_Items (Parent (Gen_Decl)));
9439 while Present (Item) loop
9440 if Nkind (Item) = N_With_Clause then
9441 Lib_Unit := Library_Unit (Item);
9442
9443 -- Take care to prevent direct cyclic with's
9444
9445 if Lib_Unit /= Current_Unit then
9446
9447 -- Do not add a unit if it is already in the context
9448
9449 Clause := First (Current_Context);
9450 OK := True;
9451 while Present (Clause) loop
9452 if Nkind (Clause) = N_With_Clause
9453 and then Library_Unit (Clause) = Lib_Unit
9454 then
9455 OK := False;
9456 exit;
9457 end if;
9458
9459 Next (Clause);
9460 end loop;
9461
9462 if OK then
9463 New_I := New_Copy (Item);
9464 Set_Implicit_With (New_I);
9465
9466 Append (New_I, Current_Context);
9467 end if;
9468 end if;
9469 end if;
9470
9471 Next (Item);
9472 end loop;
9473 end if;
9474 end Inherit_Context;
9475
9476 ----------------
9477 -- Initialize --
9478 ----------------
9479
9480 procedure Initialize is
9481 begin
9482 Generic_Renamings.Init;
9483 Instance_Envs.Init;
9484 Generic_Flags.Init;
9485 Generic_Renamings_HTable.Reset;
9486 Circularity_Detected := False;
9487 Exchanged_Views := No_Elist;
9488 Hidden_Entities := No_Elist;
9489 end Initialize;
9490
9491 -------------------------------------
9492 -- Insert_Freeze_Node_For_Instance --
9493 -------------------------------------
9494
9495 procedure Insert_Freeze_Node_For_Instance
9496 (N : Node_Id;
9497 F_Node : Node_Id)
9498 is
9499 Decl : Node_Id;
9500 Decls : List_Id;
9501 Inst : Entity_Id;
9502 Par_N : Node_Id;
9503
9504 function Enclosing_Body (N : Node_Id) return Node_Id;
9505 -- Find enclosing package or subprogram body, if any. Freeze node may
9506 -- be placed at end of current declarative list if previous instance
9507 -- and current one have different enclosing bodies.
9508
9509 function Previous_Instance (Gen : Entity_Id) return Entity_Id;
9510 -- Find the local instance, if any, that declares the generic that is
9511 -- being instantiated. If present, the freeze node for this instance
9512 -- must follow the freeze node for the previous instance.
9513
9514 --------------------
9515 -- Enclosing_Body --
9516 --------------------
9517
9518 function Enclosing_Body (N : Node_Id) return Node_Id is
9519 P : Node_Id;
9520
9521 begin
9522 P := Parent (N);
9523 while Present (P)
9524 and then Nkind (Parent (P)) /= N_Compilation_Unit
9525 loop
9526 if Nkind_In (P, N_Package_Body, N_Subprogram_Body) then
9527 if Nkind (Parent (P)) = N_Subunit then
9528 return Corresponding_Stub (Parent (P));
9529 else
9530 return P;
9531 end if;
9532 end if;
9533
9534 P := True_Parent (P);
9535 end loop;
9536
9537 return Empty;
9538 end Enclosing_Body;
9539
9540 -----------------------
9541 -- Previous_Instance --
9542 -----------------------
9543
9544 function Previous_Instance (Gen : Entity_Id) return Entity_Id is
9545 S : Entity_Id;
9546
9547 begin
9548 S := Scope (Gen);
9549 while Present (S) and then S /= Standard_Standard loop
9550 if Is_Generic_Instance (S)
9551 and then In_Same_Source_Unit (S, N)
9552 then
9553 return S;
9554 end if;
9555
9556 S := Scope (S);
9557 end loop;
9558
9559 return Empty;
9560 end Previous_Instance;
9561
9562 -- Start of processing for Insert_Freeze_Node_For_Instance
9563
9564 begin
9565 if not Is_List_Member (F_Node) then
9566 Decl := N;
9567 Decls := List_Containing (N);
9568 Inst := Entity (F_Node);
9569 Par_N := Parent (Decls);
9570
9571 -- When processing a subprogram instantiation, utilize the actual
9572 -- subprogram instantiation rather than its package wrapper as it
9573 -- carries all the context information.
9574
9575 if Is_Wrapper_Package (Inst) then
9576 Inst := Related_Instance (Inst);
9577 end if;
9578
9579 -- If this is a package instance, check whether the generic is
9580 -- declared in a previous instance and the current instance is
9581 -- not within the previous one.
9582
9583 if Present (Generic_Parent (Parent (Inst)))
9584 and then Is_In_Main_Unit (N)
9585 then
9586 declare
9587 Enclosing_N : constant Node_Id := Enclosing_Body (N);
9588 Par_I : constant Entity_Id :=
9589 Previous_Instance
9590 (Generic_Parent (Parent (Inst)));
9591 Scop : Entity_Id;
9592
9593 begin
9594 if Present (Par_I)
9595 and then Earlier (N, Freeze_Node (Par_I))
9596 then
9597 Scop := Scope (Inst);
9598
9599 -- If the current instance is within the one that contains
9600 -- the generic, the freeze node for the current one must
9601 -- appear in the current declarative part. Ditto, if the
9602 -- current instance is within another package instance or
9603 -- within a body that does not enclose the current instance.
9604 -- In these three cases the freeze node of the previous
9605 -- instance is not relevant.
9606
9607 while Present (Scop) and then Scop /= Standard_Standard loop
9608 exit when Scop = Par_I
9609 or else
9610 (Is_Generic_Instance (Scop)
9611 and then Scope_Depth (Scop) > Scope_Depth (Par_I));
9612 Scop := Scope (Scop);
9613 end loop;
9614
9615 -- Previous instance encloses current instance
9616
9617 if Scop = Par_I then
9618 null;
9619
9620 -- If the next node is a source body we must freeze in
9621 -- the current scope as well.
9622
9623 elsif Present (Next (N))
9624 and then Nkind_In (Next (N), N_Subprogram_Body,
9625 N_Package_Body)
9626 and then Comes_From_Source (Next (N))
9627 then
9628 null;
9629
9630 -- Current instance is within an unrelated instance
9631
9632 elsif Is_Generic_Instance (Scop) then
9633 null;
9634
9635 -- Current instance is within an unrelated body
9636
9637 elsif Present (Enclosing_N)
9638 and then Enclosing_N /= Enclosing_Body (Par_I)
9639 then
9640 null;
9641
9642 else
9643 Insert_After (Freeze_Node (Par_I), F_Node);
9644 return;
9645 end if;
9646 end if;
9647 end;
9648 end if;
9649
9650 -- When the instantiation occurs in a package declaration, append the
9651 -- freeze node to the private declarations (if any).
9652
9653 if Nkind (Par_N) = N_Package_Specification
9654 and then Decls = Visible_Declarations (Par_N)
9655 and then Present (Private_Declarations (Par_N))
9656 and then not Is_Empty_List (Private_Declarations (Par_N))
9657 then
9658 Decls := Private_Declarations (Par_N);
9659 Decl := First (Decls);
9660 end if;
9661
9662 -- Determine the proper freeze point of a package instantiation. We
9663 -- adhere to the general rule of a package or subprogram body causing
9664 -- freezing of anything before it in the same declarative region. In
9665 -- this case, the proper freeze point of a package instantiation is
9666 -- before the first source body which follows, or before a stub. This
9667 -- ensures that entities coming from the instance are already frozen
9668 -- and usable in source bodies.
9669
9670 if Nkind (Par_N) /= N_Package_Declaration
9671 and then Ekind (Inst) = E_Package
9672 and then Is_Generic_Instance (Inst)
9673 and then
9674 not In_Same_Source_Unit (Generic_Parent (Parent (Inst)), Inst)
9675 then
9676 while Present (Decl) loop
9677 if (Nkind (Decl) in N_Unit_Body
9678 or else
9679 Nkind (Decl) in N_Body_Stub)
9680 and then Comes_From_Source (Decl)
9681 then
9682 Insert_Before (Decl, F_Node);
9683 return;
9684 end if;
9685
9686 Next (Decl);
9687 end loop;
9688 end if;
9689
9690 -- In a package declaration, or if no previous body, insert at end
9691 -- of list.
9692
9693 Set_Sloc (F_Node, Sloc (Last (Decls)));
9694 Insert_After (Last (Decls), F_Node);
9695 end if;
9696 end Insert_Freeze_Node_For_Instance;
9697
9698 ------------------
9699 -- Install_Body --
9700 ------------------
9701
9702 procedure Install_Body
9703 (Act_Body : Node_Id;
9704 N : Node_Id;
9705 Gen_Body : Node_Id;
9706 Gen_Decl : Node_Id)
9707 is
9708 function In_Same_Scope (Gen_Id, Act_Id : Node_Id) return Boolean;
9709 -- Check if the generic definition and the instantiation come from
9710 -- a common scope, in which case the instance must be frozen after
9711 -- the generic body.
9712
9713 function True_Sloc (N, Act_Unit : Node_Id) return Source_Ptr;
9714 -- If the instance is nested inside a generic unit, the Sloc of the
9715 -- instance indicates the place of the original definition, not the
9716 -- point of the current enclosing instance. Pending a better usage of
9717 -- Slocs to indicate instantiation places, we determine the place of
9718 -- origin of a node by finding the maximum sloc of any ancestor node.
9719 -- Why is this not equivalent to Top_Level_Location ???
9720
9721 -------------------
9722 -- In_Same_Scope --
9723 -------------------
9724
9725 function In_Same_Scope (Gen_Id, Act_Id : Node_Id) return Boolean is
9726 Act_Scop : Entity_Id := Scope (Act_Id);
9727 Gen_Scop : Entity_Id := Scope (Gen_Id);
9728
9729 begin
9730 while Act_Scop /= Standard_Standard
9731 and then Gen_Scop /= Standard_Standard
9732 loop
9733 if Act_Scop = Gen_Scop then
9734 return True;
9735 end if;
9736
9737 Act_Scop := Scope (Act_Scop);
9738 Gen_Scop := Scope (Gen_Scop);
9739 end loop;
9740
9741 return False;
9742 end In_Same_Scope;
9743
9744 ---------------
9745 -- True_Sloc --
9746 ---------------
9747
9748 function True_Sloc (N, Act_Unit : Node_Id) return Source_Ptr is
9749 N1 : Node_Id;
9750 Res : Source_Ptr;
9751
9752 begin
9753 Res := Sloc (N);
9754 N1 := N;
9755 while Present (N1) and then N1 /= Act_Unit loop
9756 if Sloc (N1) > Res then
9757 Res := Sloc (N1);
9758 end if;
9759
9760 N1 := Parent (N1);
9761 end loop;
9762
9763 return Res;
9764 end True_Sloc;
9765
9766 Act_Id : constant Entity_Id := Corresponding_Spec (Act_Body);
9767 Act_Unit : constant Node_Id := Unit (Cunit (Get_Source_Unit (N)));
9768 Gen_Id : constant Entity_Id := Corresponding_Spec (Gen_Body);
9769 Par : constant Entity_Id := Scope (Gen_Id);
9770 Gen_Unit : constant Node_Id :=
9771 Unit (Cunit (Get_Source_Unit (Gen_Decl)));
9772
9773 Body_Unit : Node_Id;
9774 F_Node : Node_Id;
9775 Must_Delay : Boolean;
9776 Orig_Body : Node_Id := Gen_Body;
9777
9778 -- Start of processing for Install_Body
9779
9780 begin
9781 -- Handle first the case of an instance with incomplete actual types.
9782 -- The instance body cannot be placed after the declaration because
9783 -- full views have not been seen yet. Any use of the non-limited views
9784 -- in the instance body requires the presence of a regular with_clause
9785 -- in the enclosing unit, and will fail if this with_clause is missing.
9786 -- We place the instance body at the beginning of the enclosing body,
9787 -- which is the unit being compiled. The freeze node for the instance
9788 -- is then placed after the instance body.
9789
9790 if not Is_Empty_Elmt_List (Incomplete_Actuals (Act_Id))
9791 and then Expander_Active
9792 and then Ekind (Scope (Act_Id)) = E_Package
9793 then
9794 declare
9795 Scop : constant Entity_Id := Scope (Act_Id);
9796 Body_Id : constant Node_Id :=
9797 Corresponding_Body (Unit_Declaration_Node (Scop));
9798
9799 begin
9800 Ensure_Freeze_Node (Act_Id);
9801 F_Node := Freeze_Node (Act_Id);
9802 if Present (Body_Id) then
9803 Set_Is_Frozen (Act_Id, False);
9804 Prepend (Act_Body, Declarations (Parent (Body_Id)));
9805 if Is_List_Member (F_Node) then
9806 Remove (F_Node);
9807 end if;
9808
9809 Insert_After (Act_Body, F_Node);
9810 end if;
9811 end;
9812 return;
9813 end if;
9814
9815 -- If the body is a subunit, the freeze point is the corresponding stub
9816 -- in the current compilation, not the subunit itself.
9817
9818 if Nkind (Parent (Gen_Body)) = N_Subunit then
9819 Orig_Body := Corresponding_Stub (Parent (Gen_Body));
9820 else
9821 Orig_Body := Gen_Body;
9822 end if;
9823
9824 Body_Unit := Unit (Cunit (Get_Source_Unit (Orig_Body)));
9825
9826 -- If the instantiation and the generic definition appear in the same
9827 -- package declaration, this is an early instantiation. If they appear
9828 -- in the same declarative part, it is an early instantiation only if
9829 -- the generic body appears textually later, and the generic body is
9830 -- also in the main unit.
9831
9832 -- If instance is nested within a subprogram, and the generic body
9833 -- is not, the instance is delayed because the enclosing body is. If
9834 -- instance and body are within the same scope, or the same subprogram
9835 -- body, indicate explicitly that the instance is delayed.
9836
9837 Must_Delay :=
9838 (Gen_Unit = Act_Unit
9839 and then (Nkind_In (Gen_Unit, N_Generic_Package_Declaration,
9840 N_Package_Declaration)
9841 or else (Gen_Unit = Body_Unit
9842 and then True_Sloc (N, Act_Unit) <
9843 Sloc (Orig_Body)))
9844 and then Is_In_Main_Unit (Original_Node (Gen_Unit))
9845 and then In_Same_Scope (Gen_Id, Act_Id));
9846
9847 -- If this is an early instantiation, the freeze node is placed after
9848 -- the generic body. Otherwise, if the generic appears in an instance,
9849 -- we cannot freeze the current instance until the outer one is frozen.
9850 -- This is only relevant if the current instance is nested within some
9851 -- inner scope not itself within the outer instance. If this scope is
9852 -- a package body in the same declarative part as the outer instance,
9853 -- then that body needs to be frozen after the outer instance. Finally,
9854 -- if no delay is needed, we place the freeze node at the end of the
9855 -- current declarative part.
9856
9857 if Expander_Active
9858 and then (No (Freeze_Node (Act_Id))
9859 or else not Is_List_Member (Freeze_Node (Act_Id)))
9860 then
9861 Ensure_Freeze_Node (Act_Id);
9862 F_Node := Freeze_Node (Act_Id);
9863
9864 if Must_Delay then
9865 Insert_After (Orig_Body, F_Node);
9866
9867 elsif Is_Generic_Instance (Par)
9868 and then Present (Freeze_Node (Par))
9869 and then Scope (Act_Id) /= Par
9870 then
9871 -- Freeze instance of inner generic after instance of enclosing
9872 -- generic.
9873
9874 if In_Same_Declarative_Part (Parent (Freeze_Node (Par)), N) then
9875
9876 -- Handle the following case:
9877
9878 -- package Parent_Inst is new ...
9879 -- Parent_Inst []
9880
9881 -- procedure P ... -- this body freezes Parent_Inst
9882
9883 -- package Inst is new ...
9884
9885 -- In this particular scenario, the freeze node for Inst must
9886 -- be inserted in the same manner as that of Parent_Inst,
9887 -- before the next source body or at the end of the declarative
9888 -- list (body not available). If body P did not exist and
9889 -- Parent_Inst was frozen after Inst, either by a body
9890 -- following Inst or at the end of the declarative region,
9891 -- the freeze node for Inst must be inserted after that of
9892 -- Parent_Inst. This relation is established by comparing
9893 -- the Slocs of Parent_Inst freeze node and Inst.
9894 -- We examine the parents of the enclosing lists to handle
9895 -- the case where the parent instance is in the visible part
9896 -- of a package declaration, and the inner instance is in
9897 -- the corresponding private part.
9898
9899 if Parent (List_Containing (Get_Unit_Instantiation_Node (Par)))
9900 = Parent (List_Containing (N))
9901 and then Sloc (Freeze_Node (Par)) < Sloc (N)
9902 then
9903 Insert_Freeze_Node_For_Instance (N, F_Node);
9904 else
9905 Insert_After (Freeze_Node (Par), F_Node);
9906 end if;
9907
9908 -- Freeze package enclosing instance of inner generic after
9909 -- instance of enclosing generic.
9910
9911 elsif Nkind_In (Parent (N), N_Package_Body, N_Subprogram_Body)
9912 and then In_Same_Declarative_Part
9913 (Parent (Freeze_Node (Par)), Parent (N))
9914 then
9915 declare
9916 Enclosing : Entity_Id;
9917
9918 begin
9919 Enclosing := Corresponding_Spec (Parent (N));
9920
9921 if No (Enclosing) then
9922 Enclosing := Defining_Entity (Parent (N));
9923 end if;
9924
9925 Insert_Freeze_Node_For_Instance (N, F_Node);
9926 Ensure_Freeze_Node (Enclosing);
9927
9928 if not Is_List_Member (Freeze_Node (Enclosing)) then
9929
9930 -- The enclosing context is a subunit, insert the freeze
9931 -- node after the stub.
9932
9933 if Nkind (Parent (Parent (N))) = N_Subunit then
9934 Insert_Freeze_Node_For_Instance
9935 (Corresponding_Stub (Parent (Parent (N))),
9936 Freeze_Node (Enclosing));
9937
9938 -- The enclosing context is a package with a stub body
9939 -- which has already been replaced by the real body.
9940 -- Insert the freeze node after the actual body.
9941
9942 elsif Ekind (Enclosing) = E_Package
9943 and then Present (Body_Entity (Enclosing))
9944 and then Was_Originally_Stub
9945 (Parent (Body_Entity (Enclosing)))
9946 then
9947 Insert_Freeze_Node_For_Instance
9948 (Parent (Body_Entity (Enclosing)),
9949 Freeze_Node (Enclosing));
9950
9951 -- The parent instance has been frozen before the body of
9952 -- the enclosing package, insert the freeze node after
9953 -- the body.
9954
9955 elsif List_Containing (Freeze_Node (Par)) =
9956 List_Containing (Parent (N))
9957 and then Sloc (Freeze_Node (Par)) < Sloc (Parent (N))
9958 then
9959 Insert_Freeze_Node_For_Instance
9960 (Parent (N), Freeze_Node (Enclosing));
9961
9962 else
9963 Insert_After
9964 (Freeze_Node (Par), Freeze_Node (Enclosing));
9965 end if;
9966 end if;
9967 end;
9968
9969 else
9970 Insert_Freeze_Node_For_Instance (N, F_Node);
9971 end if;
9972
9973 else
9974 Insert_Freeze_Node_For_Instance (N, F_Node);
9975 end if;
9976 end if;
9977
9978 Set_Is_Frozen (Act_Id);
9979 Insert_Before (N, Act_Body);
9980 Mark_Rewrite_Insertion (Act_Body);
9981 end Install_Body;
9982
9983 -----------------------------
9984 -- Install_Formal_Packages --
9985 -----------------------------
9986
9987 procedure Install_Formal_Packages (Par : Entity_Id) is
9988 E : Entity_Id;
9989 Gen : Entity_Id;
9990 Gen_E : Entity_Id := Empty;
9991
9992 begin
9993 E := First_Entity (Par);
9994
9995 -- If we are installing an instance parent, locate the formal packages
9996 -- of its generic parent.
9997
9998 if Is_Generic_Instance (Par) then
9999 Gen := Generic_Parent (Package_Specification (Par));
10000 Gen_E := First_Entity (Gen);
10001 end if;
10002
10003 while Present (E) loop
10004 if Ekind (E) = E_Package
10005 and then Nkind (Parent (E)) = N_Package_Renaming_Declaration
10006 then
10007 -- If this is the renaming for the parent instance, done
10008
10009 if Renamed_Object (E) = Par then
10010 exit;
10011
10012 -- The visibility of a formal of an enclosing generic is already
10013 -- correct.
10014
10015 elsif Denotes_Formal_Package (E) then
10016 null;
10017
10018 elsif Present (Associated_Formal_Package (E)) then
10019 Check_Generic_Actuals (Renamed_Object (E), True);
10020 Set_Is_Hidden (E, False);
10021
10022 -- Find formal package in generic unit that corresponds to
10023 -- (instance of) formal package in instance.
10024
10025 while Present (Gen_E) and then Chars (Gen_E) /= Chars (E) loop
10026 Next_Entity (Gen_E);
10027 end loop;
10028
10029 if Present (Gen_E) then
10030 Map_Formal_Package_Entities (Gen_E, E);
10031 end if;
10032 end if;
10033 end if;
10034
10035 Next_Entity (E);
10036
10037 if Present (Gen_E) then
10038 Next_Entity (Gen_E);
10039 end if;
10040 end loop;
10041 end Install_Formal_Packages;
10042
10043 --------------------
10044 -- Install_Parent --
10045 --------------------
10046
10047 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False) is
10048 Ancestors : constant Elist_Id := New_Elmt_List;
10049 S : constant Entity_Id := Current_Scope;
10050 Inst_Par : Entity_Id;
10051 First_Par : Entity_Id;
10052 Inst_Node : Node_Id;
10053 Gen_Par : Entity_Id;
10054 First_Gen : Entity_Id;
10055 Elmt : Elmt_Id;
10056
10057 procedure Install_Noninstance_Specs (Par : Entity_Id);
10058 -- Install the scopes of noninstance parent units ending with Par
10059
10060 procedure Install_Spec (Par : Entity_Id);
10061 -- The child unit is within the declarative part of the parent, so the
10062 -- declarations within the parent are immediately visible.
10063
10064 -------------------------------
10065 -- Install_Noninstance_Specs --
10066 -------------------------------
10067
10068 procedure Install_Noninstance_Specs (Par : Entity_Id) is
10069 begin
10070 if Present (Par)
10071 and then Par /= Standard_Standard
10072 and then not In_Open_Scopes (Par)
10073 then
10074 Install_Noninstance_Specs (Scope (Par));
10075 Install_Spec (Par);
10076 end if;
10077 end Install_Noninstance_Specs;
10078
10079 ------------------
10080 -- Install_Spec --
10081 ------------------
10082
10083 procedure Install_Spec (Par : Entity_Id) is
10084 Spec : constant Node_Id := Package_Specification (Par);
10085
10086 begin
10087 -- If this parent of the child instance is a top-level unit,
10088 -- then record the unit and its visibility for later resetting in
10089 -- Remove_Parent. We exclude units that are generic instances, as we
10090 -- only want to record this information for the ultimate top-level
10091 -- noninstance parent (is that always correct???).
10092
10093 if Scope (Par) = Standard_Standard
10094 and then not Is_Generic_Instance (Par)
10095 then
10096 Parent_Unit_Visible := Is_Immediately_Visible (Par);
10097 Instance_Parent_Unit := Par;
10098 end if;
10099
10100 -- Open the parent scope and make it and its declarations visible.
10101 -- If this point is not within a body, then only the visible
10102 -- declarations should be made visible, and installation of the
10103 -- private declarations is deferred until the appropriate point
10104 -- within analysis of the spec being instantiated (see the handling
10105 -- of parent visibility in Analyze_Package_Specification). This is
10106 -- relaxed in the case where the parent unit is Ada.Tags, to avoid
10107 -- private view problems that occur when compiling instantiations of
10108 -- a generic child of that package (Generic_Dispatching_Constructor).
10109 -- If the instance freezes a tagged type, inlinings of operations
10110 -- from Ada.Tags may need the full view of type Tag. If inlining took
10111 -- proper account of establishing visibility of inlined subprograms'
10112 -- parents then it should be possible to remove this
10113 -- special check. ???
10114
10115 Push_Scope (Par);
10116 Set_Is_Immediately_Visible (Par);
10117 Install_Visible_Declarations (Par);
10118 Set_Use (Visible_Declarations (Spec));
10119
10120 if In_Body or else Is_RTU (Par, Ada_Tags) then
10121 Install_Private_Declarations (Par);
10122 Set_Use (Private_Declarations (Spec));
10123 end if;
10124 end Install_Spec;
10125
10126 -- Start of processing for Install_Parent
10127
10128 begin
10129 -- We need to install the parent instance to compile the instantiation
10130 -- of the child, but the child instance must appear in the current
10131 -- scope. Given that we cannot place the parent above the current scope
10132 -- in the scope stack, we duplicate the current scope and unstack both
10133 -- after the instantiation is complete.
10134
10135 -- If the parent is itself the instantiation of a child unit, we must
10136 -- also stack the instantiation of its parent, and so on. Each such
10137 -- ancestor is the prefix of the name in a prior instantiation.
10138
10139 -- If this is a nested instance, the parent unit itself resolves to
10140 -- a renaming of the parent instance, whose declaration we need.
10141
10142 -- Finally, the parent may be a generic (not an instance) when the
10143 -- child unit appears as a formal package.
10144
10145 Inst_Par := P;
10146
10147 if Present (Renamed_Entity (Inst_Par)) then
10148 Inst_Par := Renamed_Entity (Inst_Par);
10149 end if;
10150
10151 First_Par := Inst_Par;
10152
10153 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
10154
10155 First_Gen := Gen_Par;
10156
10157 while Present (Gen_Par) and then Is_Child_Unit (Gen_Par) loop
10158
10159 -- Load grandparent instance as well
10160
10161 Inst_Node := Get_Unit_Instantiation_Node (Inst_Par);
10162
10163 if Nkind (Name (Inst_Node)) = N_Expanded_Name then
10164 Inst_Par := Entity (Prefix (Name (Inst_Node)));
10165
10166 if Present (Renamed_Entity (Inst_Par)) then
10167 Inst_Par := Renamed_Entity (Inst_Par);
10168 end if;
10169
10170 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
10171
10172 if Present (Gen_Par) then
10173 Prepend_Elmt (Inst_Par, Ancestors);
10174
10175 else
10176 -- Parent is not the name of an instantiation
10177
10178 Install_Noninstance_Specs (Inst_Par);
10179 exit;
10180 end if;
10181
10182 else
10183 -- Previous error
10184
10185 exit;
10186 end if;
10187 end loop;
10188
10189 if Present (First_Gen) then
10190 Append_Elmt (First_Par, Ancestors);
10191 else
10192 Install_Noninstance_Specs (First_Par);
10193 end if;
10194
10195 if not Is_Empty_Elmt_List (Ancestors) then
10196 Elmt := First_Elmt (Ancestors);
10197 while Present (Elmt) loop
10198 Install_Spec (Node (Elmt));
10199 Install_Formal_Packages (Node (Elmt));
10200 Next_Elmt (Elmt);
10201 end loop;
10202 end if;
10203
10204 if not In_Body then
10205 Push_Scope (S);
10206 end if;
10207 end Install_Parent;
10208
10209 -------------------------------
10210 -- Install_Hidden_Primitives --
10211 -------------------------------
10212
10213 procedure Install_Hidden_Primitives
10214 (Prims_List : in out Elist_Id;
10215 Gen_T : Entity_Id;
10216 Act_T : Entity_Id)
10217 is
10218 Elmt : Elmt_Id;
10219 List : Elist_Id := No_Elist;
10220 Prim_G_Elmt : Elmt_Id;
10221 Prim_A_Elmt : Elmt_Id;
10222 Prim_G : Node_Id;
10223 Prim_A : Node_Id;
10224
10225 begin
10226 -- No action needed in case of serious errors because we cannot trust
10227 -- in the order of primitives
10228
10229 if Serious_Errors_Detected > 0 then
10230 return;
10231
10232 -- No action possible if we don't have available the list of primitive
10233 -- operations
10234
10235 elsif No (Gen_T)
10236 or else not Is_Record_Type (Gen_T)
10237 or else not Is_Tagged_Type (Gen_T)
10238 or else not Is_Record_Type (Act_T)
10239 or else not Is_Tagged_Type (Act_T)
10240 then
10241 return;
10242
10243 -- There is no need to handle interface types since their primitives
10244 -- cannot be hidden
10245
10246 elsif Is_Interface (Gen_T) then
10247 return;
10248 end if;
10249
10250 Prim_G_Elmt := First_Elmt (Primitive_Operations (Gen_T));
10251
10252 if not Is_Class_Wide_Type (Act_T) then
10253 Prim_A_Elmt := First_Elmt (Primitive_Operations (Act_T));
10254 else
10255 Prim_A_Elmt := First_Elmt (Primitive_Operations (Root_Type (Act_T)));
10256 end if;
10257
10258 loop
10259 -- Skip predefined primitives in the generic formal
10260
10261 while Present (Prim_G_Elmt)
10262 and then Is_Predefined_Dispatching_Operation (Node (Prim_G_Elmt))
10263 loop
10264 Next_Elmt (Prim_G_Elmt);
10265 end loop;
10266
10267 -- Skip predefined primitives in the generic actual
10268
10269 while Present (Prim_A_Elmt)
10270 and then Is_Predefined_Dispatching_Operation (Node (Prim_A_Elmt))
10271 loop
10272 Next_Elmt (Prim_A_Elmt);
10273 end loop;
10274
10275 exit when No (Prim_G_Elmt) or else No (Prim_A_Elmt);
10276
10277 Prim_G := Node (Prim_G_Elmt);
10278 Prim_A := Node (Prim_A_Elmt);
10279
10280 -- There is no need to handle interface primitives because their
10281 -- primitives are not hidden
10282
10283 exit when Present (Interface_Alias (Prim_G));
10284
10285 -- Here we install one hidden primitive
10286
10287 if Chars (Prim_G) /= Chars (Prim_A)
10288 and then Has_Suffix (Prim_A, 'P')
10289 and then Remove_Suffix (Prim_A, 'P') = Chars (Prim_G)
10290 then
10291 Set_Chars (Prim_A, Chars (Prim_G));
10292 Append_New_Elmt (Prim_A, To => List);
10293 end if;
10294
10295 Next_Elmt (Prim_A_Elmt);
10296 Next_Elmt (Prim_G_Elmt);
10297 end loop;
10298
10299 -- Append the elements to the list of temporarily visible primitives
10300 -- avoiding duplicates.
10301
10302 if Present (List) then
10303 if No (Prims_List) then
10304 Prims_List := New_Elmt_List;
10305 end if;
10306
10307 Elmt := First_Elmt (List);
10308 while Present (Elmt) loop
10309 Append_Unique_Elmt (Node (Elmt), Prims_List);
10310 Next_Elmt (Elmt);
10311 end loop;
10312 end if;
10313 end Install_Hidden_Primitives;
10314
10315 -------------------------------
10316 -- Restore_Hidden_Primitives --
10317 -------------------------------
10318
10319 procedure Restore_Hidden_Primitives (Prims_List : in out Elist_Id) is
10320 Prim_Elmt : Elmt_Id;
10321 Prim : Node_Id;
10322
10323 begin
10324 if Prims_List /= No_Elist then
10325 Prim_Elmt := First_Elmt (Prims_List);
10326 while Present (Prim_Elmt) loop
10327 Prim := Node (Prim_Elmt);
10328 Set_Chars (Prim, Add_Suffix (Prim, 'P'));
10329 Next_Elmt (Prim_Elmt);
10330 end loop;
10331
10332 Prims_List := No_Elist;
10333 end if;
10334 end Restore_Hidden_Primitives;
10335
10336 --------------------------------
10337 -- Instantiate_Formal_Package --
10338 --------------------------------
10339
10340 function Instantiate_Formal_Package
10341 (Formal : Node_Id;
10342 Actual : Node_Id;
10343 Analyzed_Formal : Node_Id) return List_Id
10344 is
10345 Loc : constant Source_Ptr := Sloc (Actual);
10346 Hidden_Formals : constant Elist_Id := New_Elmt_List;
10347 Actual_Pack : Entity_Id;
10348 Formal_Pack : Entity_Id;
10349 Gen_Parent : Entity_Id;
10350 Decls : List_Id;
10351 Nod : Node_Id;
10352 Parent_Spec : Node_Id;
10353
10354 procedure Find_Matching_Actual
10355 (F : Node_Id;
10356 Act : in out Entity_Id);
10357 -- We need to associate each formal entity in the formal package with
10358 -- the corresponding entity in the actual package. The actual package
10359 -- has been analyzed and possibly expanded, and as a result there is
10360 -- no one-to-one correspondence between the two lists (for example,
10361 -- the actual may include subtypes, itypes, and inherited primitive
10362 -- operations, interspersed among the renaming declarations for the
10363 -- actuals). We retrieve the corresponding actual by name because each
10364 -- actual has the same name as the formal, and they do appear in the
10365 -- same order.
10366
10367 function Get_Formal_Entity (N : Node_Id) return Entity_Id;
10368 -- Retrieve entity of defining entity of generic formal parameter.
10369 -- Only the declarations of formals need to be considered when
10370 -- linking them to actuals, but the declarative list may include
10371 -- internal entities generated during analysis, and those are ignored.
10372
10373 procedure Match_Formal_Entity
10374 (Formal_Node : Node_Id;
10375 Formal_Ent : Entity_Id;
10376 Actual_Ent : Entity_Id);
10377 -- Associates the formal entity with the actual. In the case where
10378 -- Formal_Ent is a formal package, this procedure iterates through all
10379 -- of its formals and enters associations between the actuals occurring
10380 -- in the formal package's corresponding actual package (given by
10381 -- Actual_Ent) and the formal package's formal parameters. This
10382 -- procedure recurses if any of the parameters is itself a package.
10383
10384 function Is_Instance_Of
10385 (Act_Spec : Entity_Id;
10386 Gen_Anc : Entity_Id) return Boolean;
10387 -- The actual can be an instantiation of a generic within another
10388 -- instance, in which case there is no direct link from it to the
10389 -- original generic ancestor. In that case, we recognize that the
10390 -- ultimate ancestor is the same by examining names and scopes.
10391
10392 procedure Process_Nested_Formal (Formal : Entity_Id);
10393 -- If the current formal is declared with a box, its own formals are
10394 -- visible in the instance, as they were in the generic, and their
10395 -- Hidden flag must be reset. If some of these formals are themselves
10396 -- packages declared with a box, the processing must be recursive.
10397
10398 --------------------------
10399 -- Find_Matching_Actual --
10400 --------------------------
10401
10402 procedure Find_Matching_Actual
10403 (F : Node_Id;
10404 Act : in out Entity_Id)
10405 is
10406 Formal_Ent : Entity_Id;
10407
10408 begin
10409 case Nkind (Original_Node (F)) is
10410 when N_Formal_Object_Declaration
10411 | N_Formal_Type_Declaration
10412 =>
10413 Formal_Ent := Defining_Identifier (F);
10414
10415 while Present (Act)
10416 and then Chars (Act) /= Chars (Formal_Ent)
10417 loop
10418 Next_Entity (Act);
10419 end loop;
10420
10421 when N_Formal_Package_Declaration
10422 | N_Formal_Subprogram_Declaration
10423 | N_Generic_Package_Declaration
10424 | N_Package_Declaration
10425 =>
10426 Formal_Ent := Defining_Entity (F);
10427
10428 while Present (Act)
10429 and then Chars (Act) /= Chars (Formal_Ent)
10430 loop
10431 Next_Entity (Act);
10432 end loop;
10433
10434 when others =>
10435 raise Program_Error;
10436 end case;
10437 end Find_Matching_Actual;
10438
10439 -------------------------
10440 -- Match_Formal_Entity --
10441 -------------------------
10442
10443 procedure Match_Formal_Entity
10444 (Formal_Node : Node_Id;
10445 Formal_Ent : Entity_Id;
10446 Actual_Ent : Entity_Id)
10447 is
10448 Act_Pkg : Entity_Id;
10449
10450 begin
10451 Set_Instance_Of (Formal_Ent, Actual_Ent);
10452
10453 if Ekind (Actual_Ent) = E_Package then
10454
10455 -- Record associations for each parameter
10456
10457 Act_Pkg := Actual_Ent;
10458
10459 declare
10460 A_Ent : Entity_Id := First_Entity (Act_Pkg);
10461 F_Ent : Entity_Id;
10462 F_Node : Node_Id;
10463
10464 Gen_Decl : Node_Id;
10465 Formals : List_Id;
10466 Actual : Entity_Id;
10467
10468 begin
10469 -- Retrieve the actual given in the formal package declaration
10470
10471 Actual := Entity (Name (Original_Node (Formal_Node)));
10472
10473 -- The actual in the formal package declaration may be a
10474 -- renamed generic package, in which case we want to retrieve
10475 -- the original generic in order to traverse its formal part.
10476
10477 if Present (Renamed_Entity (Actual)) then
10478 Gen_Decl := Unit_Declaration_Node (Renamed_Entity (Actual));
10479 else
10480 Gen_Decl := Unit_Declaration_Node (Actual);
10481 end if;
10482
10483 Formals := Generic_Formal_Declarations (Gen_Decl);
10484
10485 if Present (Formals) then
10486 F_Node := First_Non_Pragma (Formals);
10487 else
10488 F_Node := Empty;
10489 end if;
10490
10491 while Present (A_Ent)
10492 and then Present (F_Node)
10493 and then A_Ent /= First_Private_Entity (Act_Pkg)
10494 loop
10495 F_Ent := Get_Formal_Entity (F_Node);
10496
10497 if Present (F_Ent) then
10498
10499 -- This is a formal of the original package. Record
10500 -- association and recurse.
10501
10502 Find_Matching_Actual (F_Node, A_Ent);
10503 Match_Formal_Entity (F_Node, F_Ent, A_Ent);
10504 Next_Entity (A_Ent);
10505 end if;
10506
10507 Next_Non_Pragma (F_Node);
10508 end loop;
10509 end;
10510 end if;
10511 end Match_Formal_Entity;
10512
10513 -----------------------
10514 -- Get_Formal_Entity --
10515 -----------------------
10516
10517 function Get_Formal_Entity (N : Node_Id) return Entity_Id is
10518 Kind : constant Node_Kind := Nkind (Original_Node (N));
10519 begin
10520 case Kind is
10521 when N_Formal_Object_Declaration =>
10522 return Defining_Identifier (N);
10523
10524 when N_Formal_Type_Declaration =>
10525 return Defining_Identifier (N);
10526
10527 when N_Formal_Subprogram_Declaration =>
10528 return Defining_Unit_Name (Specification (N));
10529
10530 when N_Formal_Package_Declaration =>
10531 return Defining_Identifier (Original_Node (N));
10532
10533 when N_Generic_Package_Declaration =>
10534 return Defining_Identifier (Original_Node (N));
10535
10536 -- All other declarations are introduced by semantic analysis and
10537 -- have no match in the actual.
10538
10539 when others =>
10540 return Empty;
10541 end case;
10542 end Get_Formal_Entity;
10543
10544 --------------------
10545 -- Is_Instance_Of --
10546 --------------------
10547
10548 function Is_Instance_Of
10549 (Act_Spec : Entity_Id;
10550 Gen_Anc : Entity_Id) return Boolean
10551 is
10552 Gen_Par : constant Entity_Id := Generic_Parent (Act_Spec);
10553
10554 begin
10555 if No (Gen_Par) then
10556 return False;
10557
10558 -- Simplest case: the generic parent of the actual is the formal
10559
10560 elsif Gen_Par = Gen_Anc then
10561 return True;
10562
10563 elsif Chars (Gen_Par) /= Chars (Gen_Anc) then
10564 return False;
10565
10566 -- The actual may be obtained through several instantiations. Its
10567 -- scope must itself be an instance of a generic declared in the
10568 -- same scope as the formal. Any other case is detected above.
10569
10570 elsif not Is_Generic_Instance (Scope (Gen_Par)) then
10571 return False;
10572
10573 else
10574 return Generic_Parent (Parent (Scope (Gen_Par))) = Scope (Gen_Anc);
10575 end if;
10576 end Is_Instance_Of;
10577
10578 ---------------------------
10579 -- Process_Nested_Formal --
10580 ---------------------------
10581
10582 procedure Process_Nested_Formal (Formal : Entity_Id) is
10583 Ent : Entity_Id;
10584
10585 begin
10586 if Present (Associated_Formal_Package (Formal))
10587 and then Box_Present (Parent (Associated_Formal_Package (Formal)))
10588 then
10589 Ent := First_Entity (Formal);
10590 while Present (Ent) loop
10591 Set_Is_Hidden (Ent, False);
10592 Set_Is_Visible_Formal (Ent);
10593 Set_Is_Potentially_Use_Visible
10594 (Ent, Is_Potentially_Use_Visible (Formal));
10595
10596 if Ekind (Ent) = E_Package then
10597 exit when Renamed_Entity (Ent) = Renamed_Entity (Formal);
10598 Process_Nested_Formal (Ent);
10599 end if;
10600
10601 Next_Entity (Ent);
10602 end loop;
10603 end if;
10604 end Process_Nested_Formal;
10605
10606 -- Start of processing for Instantiate_Formal_Package
10607
10608 begin
10609 Analyze (Actual);
10610
10611 -- The actual must be a package instance, or else a current instance
10612 -- such as a parent generic within the body of a generic child.
10613
10614 if not Is_Entity_Name (Actual)
10615 or else not Is_Package_Or_Generic_Package (Entity (Actual))
10616 then
10617 Error_Msg_N
10618 ("expect package instance to instantiate formal", Actual);
10619 Abandon_Instantiation (Actual);
10620 raise Program_Error;
10621
10622 else
10623 Actual_Pack := Entity (Actual);
10624 Set_Is_Instantiated (Actual_Pack);
10625
10626 -- The actual may be a renamed package, or an outer generic formal
10627 -- package whose instantiation is converted into a renaming.
10628
10629 if Present (Renamed_Object (Actual_Pack)) then
10630 Actual_Pack := Renamed_Object (Actual_Pack);
10631 end if;
10632
10633 if Nkind (Analyzed_Formal) = N_Formal_Package_Declaration then
10634 Gen_Parent := Get_Instance_Of (Entity (Name (Analyzed_Formal)));
10635 Formal_Pack := Defining_Identifier (Analyzed_Formal);
10636 else
10637 Gen_Parent :=
10638 Generic_Parent (Specification (Analyzed_Formal));
10639 Formal_Pack :=
10640 Defining_Unit_Name (Specification (Analyzed_Formal));
10641 end if;
10642
10643 if Nkind (Parent (Actual_Pack)) = N_Defining_Program_Unit_Name then
10644 Parent_Spec := Package_Specification (Actual_Pack);
10645 else
10646 Parent_Spec := Parent (Actual_Pack);
10647 end if;
10648
10649 if Gen_Parent = Any_Id then
10650 Error_Msg_N
10651 ("previous error in declaration of formal package", Actual);
10652 Abandon_Instantiation (Actual);
10653
10654 elsif Is_Instance_Of (Parent_Spec, Get_Instance_Of (Gen_Parent)) then
10655 null;
10656
10657 -- If this is the current instance of an enclosing generic, that unit
10658 -- is the generic package we need.
10659
10660 elsif In_Open_Scopes (Actual_Pack)
10661 and then Ekind (Actual_Pack) = E_Generic_Package
10662 then
10663 null;
10664
10665 else
10666 Error_Msg_NE
10667 ("actual parameter must be instance of&", Actual, Gen_Parent);
10668 Abandon_Instantiation (Actual);
10669 end if;
10670
10671 Set_Instance_Of (Defining_Identifier (Formal), Actual_Pack);
10672 Map_Formal_Package_Entities (Formal_Pack, Actual_Pack);
10673
10674 Nod :=
10675 Make_Package_Renaming_Declaration (Loc,
10676 Defining_Unit_Name => New_Copy (Defining_Identifier (Formal)),
10677 Name => New_Occurrence_Of (Actual_Pack, Loc));
10678
10679 Set_Associated_Formal_Package
10680 (Defining_Unit_Name (Nod), Defining_Identifier (Formal));
10681 Decls := New_List (Nod);
10682
10683 -- If the formal F has a box, then the generic declarations are
10684 -- visible in the generic G. In an instance of G, the corresponding
10685 -- entities in the actual for F (which are the actuals for the
10686 -- instantiation of the generic that F denotes) must also be made
10687 -- visible for analysis of the current instance. On exit from the
10688 -- current instance, those entities are made private again. If the
10689 -- actual is currently in use, these entities are also use-visible.
10690
10691 -- The loop through the actual entities also steps through the formal
10692 -- entities and enters associations from formals to actuals into the
10693 -- renaming map. This is necessary to properly handle checking of
10694 -- actual parameter associations for later formals that depend on
10695 -- actuals declared in the formal package.
10696
10697 -- In Ada 2005, partial parameterization requires that we make
10698 -- visible the actuals corresponding to formals that were defaulted
10699 -- in the formal package. There formals are identified because they
10700 -- remain formal generics within the formal package, rather than
10701 -- being renamings of the actuals supplied.
10702
10703 declare
10704 Gen_Decl : constant Node_Id :=
10705 Unit_Declaration_Node (Gen_Parent);
10706 Formals : constant List_Id :=
10707 Generic_Formal_Declarations (Gen_Decl);
10708
10709 Actual_Ent : Entity_Id;
10710 Actual_Of_Formal : Node_Id;
10711 Formal_Node : Node_Id;
10712 Formal_Ent : Entity_Id;
10713
10714 begin
10715 if Present (Formals) then
10716 Formal_Node := First_Non_Pragma (Formals);
10717 else
10718 Formal_Node := Empty;
10719 end if;
10720
10721 Actual_Ent := First_Entity (Actual_Pack);
10722 Actual_Of_Formal :=
10723 First (Visible_Declarations (Specification (Analyzed_Formal)));
10724 while Present (Actual_Ent)
10725 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
10726 loop
10727 if Present (Formal_Node) then
10728 Formal_Ent := Get_Formal_Entity (Formal_Node);
10729
10730 if Present (Formal_Ent) then
10731 Find_Matching_Actual (Formal_Node, Actual_Ent);
10732 Match_Formal_Entity (Formal_Node, Formal_Ent, Actual_Ent);
10733
10734 -- We iterate at the same time over the actuals of the
10735 -- local package created for the formal, to determine
10736 -- which one of the formals of the original generic were
10737 -- defaulted in the formal. The corresponding actual
10738 -- entities are visible in the enclosing instance.
10739
10740 if Box_Present (Formal)
10741 or else
10742 (Present (Actual_Of_Formal)
10743 and then
10744 Is_Generic_Formal
10745 (Get_Formal_Entity (Actual_Of_Formal)))
10746 then
10747 Set_Is_Hidden (Actual_Ent, False);
10748 Set_Is_Visible_Formal (Actual_Ent);
10749 Set_Is_Potentially_Use_Visible
10750 (Actual_Ent, In_Use (Actual_Pack));
10751
10752 if Ekind (Actual_Ent) = E_Package then
10753 Process_Nested_Formal (Actual_Ent);
10754 end if;
10755
10756 else
10757 if not Is_Hidden (Actual_Ent) then
10758 Append_Elmt (Actual_Ent, Hidden_Formals);
10759 end if;
10760
10761 Set_Is_Hidden (Actual_Ent);
10762 Set_Is_Potentially_Use_Visible (Actual_Ent, False);
10763 end if;
10764 end if;
10765
10766 Next_Non_Pragma (Formal_Node);
10767 Next (Actual_Of_Formal);
10768
10769 else
10770 -- No further formals to match, but the generic part may
10771 -- contain inherited operation that are not hidden in the
10772 -- enclosing instance.
10773
10774 Next_Entity (Actual_Ent);
10775 end if;
10776 end loop;
10777
10778 -- Inherited subprograms generated by formal derived types are
10779 -- also visible if the types are.
10780
10781 Actual_Ent := First_Entity (Actual_Pack);
10782 while Present (Actual_Ent)
10783 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
10784 loop
10785 if Is_Overloadable (Actual_Ent)
10786 and then
10787 Nkind (Parent (Actual_Ent)) = N_Subtype_Declaration
10788 and then
10789 not Is_Hidden (Defining_Identifier (Parent (Actual_Ent)))
10790 then
10791 Set_Is_Hidden (Actual_Ent, False);
10792 Set_Is_Potentially_Use_Visible
10793 (Actual_Ent, In_Use (Actual_Pack));
10794 end if;
10795
10796 Next_Entity (Actual_Ent);
10797 end loop;
10798
10799 -- No conformance to check if the generic has no formal parameters
10800 -- and the formal package has no generic associations.
10801
10802 if Is_Empty_List (Formals)
10803 and then
10804 (Box_Present (Formal)
10805 or else No (Generic_Associations (Formal)))
10806 then
10807 return Decls;
10808 end if;
10809 end;
10810
10811 -- If the formal is not declared with a box, reanalyze it as an
10812 -- abbreviated instantiation, to verify the matching rules of 12.7.
10813 -- The actual checks are performed after the generic associations
10814 -- have been analyzed, to guarantee the same visibility for this
10815 -- instantiation and for the actuals.
10816
10817 -- In Ada 2005, the generic associations for the formal can include
10818 -- defaulted parameters. These are ignored during check. This
10819 -- internal instantiation is removed from the tree after conformance
10820 -- checking, because it contains formal declarations for those
10821 -- defaulted parameters, and those should not reach the back-end.
10822
10823 if not Box_Present (Formal) then
10824 declare
10825 I_Pack : constant Entity_Id :=
10826 Make_Temporary (Sloc (Actual), 'P');
10827
10828 begin
10829 Set_Is_Internal (I_Pack);
10830 Set_Ekind (I_Pack, E_Package);
10831 Set_Hidden_In_Formal_Instance (I_Pack, Hidden_Formals);
10832
10833 Append_To (Decls,
10834 Make_Package_Instantiation (Sloc (Actual),
10835 Defining_Unit_Name => I_Pack,
10836 Name =>
10837 New_Occurrence_Of
10838 (Get_Instance_Of (Gen_Parent), Sloc (Actual)),
10839 Generic_Associations => Generic_Associations (Formal)));
10840 end;
10841 end if;
10842
10843 return Decls;
10844 end if;
10845 end Instantiate_Formal_Package;
10846
10847 -----------------------------------
10848 -- Instantiate_Formal_Subprogram --
10849 -----------------------------------
10850
10851 function Instantiate_Formal_Subprogram
10852 (Formal : Node_Id;
10853 Actual : Node_Id;
10854 Analyzed_Formal : Node_Id) return Node_Id
10855 is
10856 Analyzed_S : constant Entity_Id :=
10857 Defining_Unit_Name (Specification (Analyzed_Formal));
10858 Formal_Sub : constant Entity_Id :=
10859 Defining_Unit_Name (Specification (Formal));
10860
10861 function From_Parent_Scope (Subp : Entity_Id) return Boolean;
10862 -- If the generic is a child unit, the parent has been installed on the
10863 -- scope stack, but a default subprogram cannot resolve to something
10864 -- on the parent because that parent is not really part of the visible
10865 -- context (it is there to resolve explicit local entities). If the
10866 -- default has resolved in this way, we remove the entity from immediate
10867 -- visibility and analyze the node again to emit an error message or
10868 -- find another visible candidate.
10869
10870 procedure Valid_Actual_Subprogram (Act : Node_Id);
10871 -- Perform legality check and raise exception on failure
10872
10873 -----------------------
10874 -- From_Parent_Scope --
10875 -----------------------
10876
10877 function From_Parent_Scope (Subp : Entity_Id) return Boolean is
10878 Gen_Scope : Node_Id;
10879
10880 begin
10881 Gen_Scope := Scope (Analyzed_S);
10882 while Present (Gen_Scope) and then Is_Child_Unit (Gen_Scope) loop
10883 if Scope (Subp) = Scope (Gen_Scope) then
10884 return True;
10885 end if;
10886
10887 Gen_Scope := Scope (Gen_Scope);
10888 end loop;
10889
10890 return False;
10891 end From_Parent_Scope;
10892
10893 -----------------------------
10894 -- Valid_Actual_Subprogram --
10895 -----------------------------
10896
10897 procedure Valid_Actual_Subprogram (Act : Node_Id) is
10898 Act_E : Entity_Id;
10899
10900 begin
10901 if Is_Entity_Name (Act) then
10902 Act_E := Entity (Act);
10903
10904 elsif Nkind (Act) = N_Selected_Component
10905 and then Is_Entity_Name (Selector_Name (Act))
10906 then
10907 Act_E := Entity (Selector_Name (Act));
10908
10909 else
10910 Act_E := Empty;
10911 end if;
10912
10913 if (Present (Act_E) and then Is_Overloadable (Act_E))
10914 or else Nkind_In (Act, N_Attribute_Reference,
10915 N_Indexed_Component,
10916 N_Character_Literal,
10917 N_Explicit_Dereference)
10918 then
10919 return;
10920 end if;
10921
10922 Error_Msg_NE
10923 ("expect subprogram or entry name in instantiation of &",
10924 Instantiation_Node, Formal_Sub);
10925 Abandon_Instantiation (Instantiation_Node);
10926 end Valid_Actual_Subprogram;
10927
10928 -- Local variables
10929
10930 Decl_Node : Node_Id;
10931 Loc : Source_Ptr;
10932 Nam : Node_Id;
10933 New_Spec : Node_Id;
10934 New_Subp : Entity_Id;
10935
10936 -- Start of processing for Instantiate_Formal_Subprogram
10937
10938 begin
10939 New_Spec := New_Copy_Tree (Specification (Formal));
10940
10941 -- The tree copy has created the proper instantiation sloc for the
10942 -- new specification. Use this location for all other constructed
10943 -- declarations.
10944
10945 Loc := Sloc (Defining_Unit_Name (New_Spec));
10946
10947 -- Create new entity for the actual (New_Copy_Tree does not), and
10948 -- indicate that it is an actual.
10949
10950 -- If the actual is not an entity (i.e. an attribute reference)
10951 -- and the formal includes aspect specifications for contracts,
10952 -- we create an internal name for the renaming declaration. The
10953 -- constructed wrapper contains a call to the entity in the renaming.
10954 -- This is an expansion activity, as is the wrapper creation.
10955
10956 if Ada_Version >= Ada_2020
10957 and then Has_Contracts (Analyzed_Formal)
10958 and then not Is_Entity_Name (Actual)
10959 and then Expander_Active
10960 then
10961 New_Subp := Make_Temporary (Sloc (Actual), 'S');
10962 Set_Defining_Unit_Name (New_Spec, New_Subp);
10963 else
10964 New_Subp := Make_Defining_Identifier (Loc, Chars (Formal_Sub));
10965 end if;
10966
10967 Set_Ekind (New_Subp, Ekind (Analyzed_S));
10968 Set_Is_Generic_Actual_Subprogram (New_Subp);
10969 Set_Defining_Unit_Name (New_Spec, New_Subp);
10970
10971 -- Create new entities for the each of the formals in the specification
10972 -- of the renaming declaration built for the actual.
10973
10974 if Present (Parameter_Specifications (New_Spec)) then
10975 declare
10976 F : Node_Id;
10977 F_Id : Entity_Id;
10978
10979 begin
10980 F := First (Parameter_Specifications (New_Spec));
10981 while Present (F) loop
10982 F_Id := Defining_Identifier (F);
10983
10984 Set_Defining_Identifier (F,
10985 Make_Defining_Identifier (Sloc (F_Id), Chars (F_Id)));
10986 Next (F);
10987 end loop;
10988 end;
10989 end if;
10990
10991 -- Find entity of actual. If the actual is an attribute reference, it
10992 -- cannot be resolved here (its formal is missing) but is handled
10993 -- instead in Attribute_Renaming. If the actual is overloaded, it is
10994 -- fully resolved subsequently, when the renaming declaration for the
10995 -- formal is analyzed. If it is an explicit dereference, resolve the
10996 -- prefix but not the actual itself, to prevent interpretation as call.
10997
10998 if Present (Actual) then
10999 Loc := Sloc (Actual);
11000 Set_Sloc (New_Spec, Loc);
11001
11002 if Nkind (Actual) = N_Operator_Symbol then
11003 Find_Direct_Name (Actual);
11004
11005 elsif Nkind (Actual) = N_Explicit_Dereference then
11006 Analyze (Prefix (Actual));
11007
11008 elsif Nkind (Actual) /= N_Attribute_Reference then
11009 Analyze (Actual);
11010 end if;
11011
11012 Valid_Actual_Subprogram (Actual);
11013 Nam := Actual;
11014
11015 elsif Present (Default_Name (Formal)) then
11016 if not Nkind_In (Default_Name (Formal), N_Attribute_Reference,
11017 N_Selected_Component,
11018 N_Indexed_Component,
11019 N_Character_Literal)
11020 and then Present (Entity (Default_Name (Formal)))
11021 then
11022 Nam := New_Occurrence_Of (Entity (Default_Name (Formal)), Loc);
11023 else
11024 Nam := New_Copy (Default_Name (Formal));
11025 Set_Sloc (Nam, Loc);
11026 end if;
11027
11028 elsif Box_Present (Formal) then
11029
11030 -- Actual is resolved at the point of instantiation. Create an
11031 -- identifier or operator with the same name as the formal.
11032
11033 if Nkind (Formal_Sub) = N_Defining_Operator_Symbol then
11034 Nam :=
11035 Make_Operator_Symbol (Loc,
11036 Chars => Chars (Formal_Sub),
11037 Strval => No_String);
11038 else
11039 Nam := Make_Identifier (Loc, Chars (Formal_Sub));
11040 end if;
11041
11042 elsif Nkind (Specification (Formal)) = N_Procedure_Specification
11043 and then Null_Present (Specification (Formal))
11044 then
11045 -- Generate null body for procedure, for use in the instance
11046
11047 Decl_Node :=
11048 Make_Subprogram_Body (Loc,
11049 Specification => New_Spec,
11050 Declarations => New_List,
11051 Handled_Statement_Sequence =>
11052 Make_Handled_Sequence_Of_Statements (Loc,
11053 Statements => New_List (Make_Null_Statement (Loc))));
11054
11055 -- RM 12.6 (16 2/2): The procedure has convention Intrinsic
11056
11057 Set_Convention (Defining_Unit_Name (New_Spec), Convention_Intrinsic);
11058
11059 -- Eliminate the calls to it when optimization is enabled
11060
11061 Set_Is_Inlined (Defining_Unit_Name (New_Spec));
11062 return Decl_Node;
11063
11064 else
11065 Error_Msg_Sloc := Sloc (Scope (Analyzed_S));
11066 Error_Msg_NE
11067 ("missing actual&", Instantiation_Node, Formal_Sub);
11068 Error_Msg_NE
11069 ("\in instantiation of & declared#",
11070 Instantiation_Node, Scope (Analyzed_S));
11071 Abandon_Instantiation (Instantiation_Node);
11072 end if;
11073
11074 Decl_Node :=
11075 Make_Subprogram_Renaming_Declaration (Loc,
11076 Specification => New_Spec,
11077 Name => Nam);
11078
11079 -- If we do not have an actual and the formal specified <> then set to
11080 -- get proper default.
11081
11082 if No (Actual) and then Box_Present (Formal) then
11083 Set_From_Default (Decl_Node);
11084 end if;
11085
11086 -- Gather possible interpretations for the actual before analyzing the
11087 -- instance. If overloaded, it will be resolved when analyzing the
11088 -- renaming declaration.
11089
11090 if Box_Present (Formal) and then No (Actual) then
11091 Analyze (Nam);
11092
11093 if Is_Child_Unit (Scope (Analyzed_S))
11094 and then Present (Entity (Nam))
11095 then
11096 if not Is_Overloaded (Nam) then
11097 if From_Parent_Scope (Entity (Nam)) then
11098 Set_Is_Immediately_Visible (Entity (Nam), False);
11099 Set_Entity (Nam, Empty);
11100 Set_Etype (Nam, Empty);
11101
11102 Analyze (Nam);
11103 Set_Is_Immediately_Visible (Entity (Nam));
11104 end if;
11105
11106 else
11107 declare
11108 I : Interp_Index;
11109 It : Interp;
11110
11111 begin
11112 Get_First_Interp (Nam, I, It);
11113 while Present (It.Nam) loop
11114 if From_Parent_Scope (It.Nam) then
11115 Remove_Interp (I);
11116 end if;
11117
11118 Get_Next_Interp (I, It);
11119 end loop;
11120 end;
11121 end if;
11122 end if;
11123 end if;
11124
11125 -- The generic instantiation freezes the actual. This can only be done
11126 -- once the actual is resolved, in the analysis of the renaming
11127 -- declaration. To make the formal subprogram entity available, we set
11128 -- Corresponding_Formal_Spec to point to the formal subprogram entity.
11129 -- This is also needed in Analyze_Subprogram_Renaming for the processing
11130 -- of formal abstract subprograms.
11131
11132 Set_Corresponding_Formal_Spec (Decl_Node, Analyzed_S);
11133
11134 -- We cannot analyze the renaming declaration, and thus find the actual,
11135 -- until all the actuals are assembled in the instance. For subsequent
11136 -- checks of other actuals, indicate the node that will hold the
11137 -- instance of this formal.
11138
11139 Set_Instance_Of (Analyzed_S, Nam);
11140
11141 if Nkind (Actual) = N_Selected_Component
11142 and then Is_Task_Type (Etype (Prefix (Actual)))
11143 and then not Is_Frozen (Etype (Prefix (Actual)))
11144 then
11145 -- The renaming declaration will create a body, which must appear
11146 -- outside of the instantiation, We move the renaming declaration
11147 -- out of the instance, and create an additional renaming inside,
11148 -- to prevent freezing anomalies.
11149
11150 declare
11151 Anon_Id : constant Entity_Id := Make_Temporary (Loc, 'E');
11152
11153 begin
11154 Set_Defining_Unit_Name (New_Spec, Anon_Id);
11155 Insert_Before (Instantiation_Node, Decl_Node);
11156 Analyze (Decl_Node);
11157
11158 -- Now create renaming within the instance
11159
11160 Decl_Node :=
11161 Make_Subprogram_Renaming_Declaration (Loc,
11162 Specification => New_Copy_Tree (New_Spec),
11163 Name => New_Occurrence_Of (Anon_Id, Loc));
11164
11165 Set_Defining_Unit_Name (Specification (Decl_Node),
11166 Make_Defining_Identifier (Loc, Chars (Formal_Sub)));
11167 end;
11168 end if;
11169
11170 return Decl_Node;
11171 end Instantiate_Formal_Subprogram;
11172
11173 ------------------------
11174 -- Instantiate_Object --
11175 ------------------------
11176
11177 function Instantiate_Object
11178 (Formal : Node_Id;
11179 Actual : Node_Id;
11180 Analyzed_Formal : Node_Id) return List_Id
11181 is
11182 Gen_Obj : constant Entity_Id := Defining_Identifier (Formal);
11183 A_Gen_Obj : constant Entity_Id :=
11184 Defining_Identifier (Analyzed_Formal);
11185 Acc_Def : Node_Id := Empty;
11186 Act_Assoc : constant Node_Id := Parent (Actual);
11187 Actual_Decl : Node_Id := Empty;
11188 Decl_Node : Node_Id;
11189 Def : Node_Id;
11190 Ftyp : Entity_Id;
11191 List : constant List_Id := New_List;
11192 Loc : constant Source_Ptr := Sloc (Actual);
11193 Orig_Ftyp : constant Entity_Id := Etype (A_Gen_Obj);
11194 Subt_Decl : Node_Id := Empty;
11195 Subt_Mark : Node_Id := Empty;
11196
11197 -- Start of processing for Instantiate_Object
11198
11199 begin
11200 -- Formal may be an anonymous access
11201
11202 if Present (Subtype_Mark (Formal)) then
11203 Subt_Mark := Subtype_Mark (Formal);
11204 else
11205 Check_Access_Definition (Formal);
11206 Acc_Def := Access_Definition (Formal);
11207 end if;
11208
11209 -- Sloc for error message on missing actual
11210
11211 Error_Msg_Sloc := Sloc (Scope (A_Gen_Obj));
11212
11213 if Get_Instance_Of (Gen_Obj) /= Gen_Obj then
11214 Error_Msg_N ("duplicate instantiation of generic parameter", Actual);
11215 end if;
11216
11217 Set_Parent (List, Parent (Actual));
11218
11219 -- OUT present
11220
11221 if Out_Present (Formal) then
11222
11223 -- An IN OUT generic actual must be a name. The instantiation is a
11224 -- renaming declaration. The actual is the name being renamed. We
11225 -- use the actual directly, rather than a copy, because it is not
11226 -- used further in the list of actuals, and because a copy or a use
11227 -- of relocate_node is incorrect if the instance is nested within a
11228 -- generic. In order to simplify e.g. ASIS queries, the
11229 -- Generic_Parent field links the declaration to the generic
11230 -- association.
11231
11232 if No (Actual) then
11233 Error_Msg_NE
11234 ("missing actual &",
11235 Instantiation_Node, Gen_Obj);
11236 Error_Msg_NE
11237 ("\in instantiation of & declared#",
11238 Instantiation_Node, Scope (A_Gen_Obj));
11239 Abandon_Instantiation (Instantiation_Node);
11240 end if;
11241
11242 if Present (Subt_Mark) then
11243 Decl_Node :=
11244 Make_Object_Renaming_Declaration (Loc,
11245 Defining_Identifier => New_Copy (Gen_Obj),
11246 Subtype_Mark => New_Copy_Tree (Subt_Mark),
11247 Name => Actual);
11248
11249 else pragma Assert (Present (Acc_Def));
11250 Decl_Node :=
11251 Make_Object_Renaming_Declaration (Loc,
11252 Defining_Identifier => New_Copy (Gen_Obj),
11253 Access_Definition => New_Copy_Tree (Acc_Def),
11254 Name => Actual);
11255 end if;
11256
11257 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
11258
11259 -- The analysis of the actual may produce Insert_Action nodes, so
11260 -- the declaration must have a context in which to attach them.
11261
11262 Append (Decl_Node, List);
11263 Analyze (Actual);
11264
11265 -- Return if the analysis of the actual reported some error
11266
11267 if Etype (Actual) = Any_Type then
11268 return List;
11269 end if;
11270
11271 -- This check is performed here because Analyze_Object_Renaming will
11272 -- not check it when Comes_From_Source is False. Note though that the
11273 -- check for the actual being the name of an object will be performed
11274 -- in Analyze_Object_Renaming.
11275
11276 if Is_Object_Reference (Actual)
11277 and then Is_Dependent_Component_Of_Mutable_Object (Actual)
11278 then
11279 Error_Msg_N
11280 ("illegal discriminant-dependent component for in out parameter",
11281 Actual);
11282 end if;
11283
11284 -- The actual has to be resolved in order to check that it is a
11285 -- variable (due to cases such as F (1), where F returns access to
11286 -- an array, and for overloaded prefixes).
11287
11288 Ftyp := Get_Instance_Of (Etype (A_Gen_Obj));
11289
11290 -- If the type of the formal is not itself a formal, and the current
11291 -- unit is a child unit, the formal type must be declared in a
11292 -- parent, and must be retrieved by visibility.
11293
11294 if Ftyp = Orig_Ftyp
11295 and then Is_Generic_Unit (Scope (Ftyp))
11296 and then Is_Child_Unit (Scope (A_Gen_Obj))
11297 then
11298 declare
11299 Temp : constant Node_Id :=
11300 New_Copy_Tree (Subtype_Mark (Analyzed_Formal));
11301 begin
11302 Set_Entity (Temp, Empty);
11303 Find_Type (Temp);
11304 Ftyp := Entity (Temp);
11305 end;
11306 end if;
11307
11308 if Is_Private_Type (Ftyp)
11309 and then not Is_Private_Type (Etype (Actual))
11310 and then (Base_Type (Full_View (Ftyp)) = Base_Type (Etype (Actual))
11311 or else Base_Type (Etype (Actual)) = Ftyp)
11312 then
11313 -- If the actual has the type of the full view of the formal, or
11314 -- else a non-private subtype of the formal, then the visibility
11315 -- of the formal type has changed. Add to the actuals a subtype
11316 -- declaration that will force the exchange of views in the body
11317 -- of the instance as well.
11318
11319 Subt_Decl :=
11320 Make_Subtype_Declaration (Loc,
11321 Defining_Identifier => Make_Temporary (Loc, 'P'),
11322 Subtype_Indication => New_Occurrence_Of (Ftyp, Loc));
11323
11324 Prepend (Subt_Decl, List);
11325
11326 Prepend_Elmt (Full_View (Ftyp), Exchanged_Views);
11327 Exchange_Declarations (Ftyp);
11328 end if;
11329
11330 Resolve (Actual, Ftyp);
11331
11332 if not Denotes_Variable (Actual) then
11333 Error_Msg_NE ("actual for& must be a variable", Actual, Gen_Obj);
11334
11335 elsif Base_Type (Ftyp) /= Base_Type (Etype (Actual)) then
11336
11337 -- Ada 2005 (AI-423): For a generic formal object of mode in out,
11338 -- the type of the actual shall resolve to a specific anonymous
11339 -- access type.
11340
11341 if Ada_Version < Ada_2005
11342 or else Ekind (Base_Type (Ftyp)) not in Anonymous_Access_Kind
11343 or else Ekind (Base_Type (Etype (Actual)))
11344 not in Anonymous_Access_Kind
11345 then
11346 Error_Msg_NE
11347 ("type of actual does not match type of&", Actual, Gen_Obj);
11348 end if;
11349 end if;
11350
11351 Note_Possible_Modification (Actual, Sure => True);
11352
11353 -- Check for instantiation with atomic/volatile object actual for
11354 -- nonatomic/nonvolatile formal (RM C.6 (12)).
11355
11356 if Is_Atomic_Object (Actual) and then not Is_Atomic (Orig_Ftyp) then
11357 Error_Msg_NE
11358 ("cannot instantiate nonatomic formal & of mode in out",
11359 Actual, Gen_Obj);
11360 Error_Msg_N ("\with atomic object actual (RM C.6(12))", Actual);
11361
11362 elsif Is_Volatile_Object (Actual) and then not Is_Volatile (Orig_Ftyp)
11363 then
11364 Error_Msg_NE
11365 ("cannot instantiate nonvolatile formal & of mode in out",
11366 Actual, Gen_Obj);
11367 Error_Msg_N ("\with volatile object actual (RM C.6(12))", Actual);
11368 end if;
11369
11370 -- Check for instantiation on nonatomic subcomponent of an atomic
11371 -- object in Ada 2020 (RM C.6 (13)).
11372
11373 if Ada_Version >= Ada_2020
11374 and then Is_Subcomponent_Of_Atomic_Object (Actual)
11375 and then not Is_Atomic_Object (Actual)
11376 then
11377 Error_Msg_NE
11378 ("cannot instantiate formal & of mode in out with actual",
11379 Actual, Gen_Obj);
11380 Error_Msg_N
11381 ("\nonatomic subcomponent of atomic object (RM C.6(13))",
11382 Actual);
11383 end if;
11384
11385 -- Check actual/formal compatibility with respect to the four
11386 -- volatility refinement aspects.
11387
11388 declare
11389 Actual_Obj : Entity_Id;
11390 N : Node_Id := Actual;
11391 begin
11392 -- Similar to Sem_Util.Get_Enclosing_Object, but treat
11393 -- pointer dereference like component selection.
11394 loop
11395 if Is_Entity_Name (N) then
11396 Actual_Obj := Entity (N);
11397 exit;
11398 end if;
11399
11400 case Nkind (N) is
11401 when N_Indexed_Component
11402 | N_Selected_Component
11403 | N_Slice
11404 | N_Explicit_Dereference
11405 =>
11406 N := Prefix (N);
11407
11408 when N_Type_Conversion =>
11409 N := Expression (N);
11410
11411 when others =>
11412 Actual_Obj := Etype (N);
11413 exit;
11414 end case;
11415 end loop;
11416
11417 Check_Volatility_Compatibility
11418 (Actual_Obj, A_Gen_Obj, "actual object",
11419 "its corresponding formal object of mode in out",
11420 Srcpos_Bearer => Actual);
11421 end;
11422
11423 -- Formal in-parameter
11424
11425 else
11426 -- The instantiation of a generic formal in-parameter is constant
11427 -- declaration. The actual is the expression for that declaration.
11428 -- Its type is a full copy of the type of the formal. This may be
11429 -- an access to subprogram, for which we need to generate entities
11430 -- for the formals in the new signature.
11431
11432 if Present (Actual) then
11433 if Present (Subt_Mark) then
11434 Def := New_Copy_Tree (Subt_Mark);
11435 else
11436 pragma Assert (Present (Acc_Def));
11437 Def := New_Copy_Tree (Acc_Def);
11438 end if;
11439
11440 Decl_Node :=
11441 Make_Object_Declaration (Loc,
11442 Defining_Identifier => New_Copy (Gen_Obj),
11443 Constant_Present => True,
11444 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
11445 Object_Definition => Def,
11446 Expression => Actual);
11447
11448 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
11449
11450 -- A generic formal object of a tagged type is defined to be
11451 -- aliased so the new constant must also be treated as aliased.
11452
11453 if Is_Tagged_Type (Etype (A_Gen_Obj)) then
11454 Set_Aliased_Present (Decl_Node);
11455 end if;
11456
11457 Append (Decl_Node, List);
11458
11459 -- No need to repeat (pre-)analysis of some expression nodes
11460 -- already handled in Preanalyze_Actuals.
11461
11462 if Nkind (Actual) /= N_Allocator then
11463 Analyze (Actual);
11464
11465 -- Return if the analysis of the actual reported some error
11466
11467 if Etype (Actual) = Any_Type then
11468 return List;
11469 end if;
11470 end if;
11471
11472 declare
11473 Formal_Type : constant Entity_Id := Etype (A_Gen_Obj);
11474 Typ : Entity_Id;
11475
11476 begin
11477 Typ := Get_Instance_Of (Formal_Type);
11478
11479 -- If the actual appears in the current or an enclosing scope,
11480 -- use its type directly. This is relevant if it has an actual
11481 -- subtype that is distinct from its nominal one. This cannot
11482 -- be done in general because the type of the actual may
11483 -- depend on other actuals, and only be fully determined when
11484 -- the enclosing instance is analyzed.
11485
11486 if Present (Etype (Actual))
11487 and then Is_Constr_Subt_For_U_Nominal (Etype (Actual))
11488 then
11489 Freeze_Before (Instantiation_Node, Etype (Actual));
11490 else
11491 Freeze_Before (Instantiation_Node, Typ);
11492 end if;
11493
11494 -- If the actual is an aggregate, perform name resolution on
11495 -- its components (the analysis of an aggregate does not do it)
11496 -- to capture local names that may be hidden if the generic is
11497 -- a child unit.
11498
11499 if Nkind (Actual) = N_Aggregate then
11500 Preanalyze_And_Resolve (Actual, Typ);
11501 end if;
11502
11503 if Is_Limited_Type (Typ)
11504 and then not OK_For_Limited_Init (Typ, Actual)
11505 then
11506 Error_Msg_N
11507 ("initialization not allowed for limited types", Actual);
11508 Explain_Limited_Type (Typ, Actual);
11509 end if;
11510 end;
11511
11512 elsif Present (Default_Expression (Formal)) then
11513
11514 -- Use default to construct declaration
11515
11516 if Present (Subt_Mark) then
11517 Def := New_Copy (Subt_Mark);
11518 else
11519 pragma Assert (Present (Acc_Def));
11520 Def := New_Copy_Tree (Acc_Def);
11521 end if;
11522
11523 Decl_Node :=
11524 Make_Object_Declaration (Sloc (Formal),
11525 Defining_Identifier => New_Copy (Gen_Obj),
11526 Constant_Present => True,
11527 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
11528 Object_Definition => Def,
11529 Expression => New_Copy_Tree
11530 (Default_Expression (Formal)));
11531
11532 Set_Corresponding_Generic_Association
11533 (Decl_Node, Expression (Decl_Node));
11534
11535 Append (Decl_Node, List);
11536 Set_Analyzed (Expression (Decl_Node), False);
11537
11538 else
11539 Error_Msg_NE ("missing actual&", Instantiation_Node, Gen_Obj);
11540 Error_Msg_NE ("\in instantiation of & declared#",
11541 Instantiation_Node, Scope (A_Gen_Obj));
11542
11543 if Is_Scalar_Type (Etype (A_Gen_Obj)) then
11544
11545 -- Create dummy constant declaration so that instance can be
11546 -- analyzed, to minimize cascaded visibility errors.
11547
11548 if Present (Subt_Mark) then
11549 Def := Subt_Mark;
11550 else pragma Assert (Present (Acc_Def));
11551 Def := Acc_Def;
11552 end if;
11553
11554 Decl_Node :=
11555 Make_Object_Declaration (Loc,
11556 Defining_Identifier => New_Copy (Gen_Obj),
11557 Constant_Present => True,
11558 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
11559 Object_Definition => New_Copy (Def),
11560 Expression =>
11561 Make_Attribute_Reference (Sloc (Gen_Obj),
11562 Attribute_Name => Name_First,
11563 Prefix => New_Copy (Def)));
11564
11565 Append (Decl_Node, List);
11566
11567 else
11568 Abandon_Instantiation (Instantiation_Node);
11569 end if;
11570 end if;
11571 end if;
11572
11573 if Nkind (Actual) in N_Has_Entity then
11574 Actual_Decl := Parent (Entity (Actual));
11575 end if;
11576
11577 -- Ada 2005 (AI-423) refined by AI12-0287:
11578 -- For an object_renaming_declaration with a null_exclusion or an
11579 -- access_definition that has a null_exclusion, the subtype of the
11580 -- object_name shall exclude null. In addition, if the
11581 -- object_renaming_declaration occurs within the body of a generic unit
11582 -- G or within the body of a generic unit declared within the
11583 -- declarative region of generic unit G, then:
11584 -- * if the object_name statically denotes a generic formal object of
11585 -- mode in out of G, then the declaration of that object shall have a
11586 -- null_exclusion;
11587 -- * if the object_name statically denotes a call of a generic formal
11588 -- function of G, then the declaration of the result of that function
11589 -- shall have a null_exclusion.
11590
11591 if Ada_Version >= Ada_2005
11592 and then Present (Actual_Decl)
11593 and then Nkind_In (Actual_Decl, N_Formal_Object_Declaration,
11594 N_Object_Declaration)
11595 and then Nkind (Analyzed_Formal) = N_Formal_Object_Declaration
11596 and then not Has_Null_Exclusion (Actual_Decl)
11597 and then Has_Null_Exclusion (Analyzed_Formal)
11598 and then Ekind (Defining_Identifier (Analyzed_Formal))
11599 = E_Generic_In_Out_Parameter
11600 and then ((In_Generic_Scope (Entity (Actual))
11601 and then In_Package_Body (Scope (Entity (Actual))))
11602 or else not Can_Never_Be_Null (Etype (Actual)))
11603 then
11604 Error_Msg_Sloc := Sloc (Analyzed_Formal);
11605 Error_Msg_N
11606 ("actual must exclude null to match generic formal#", Actual);
11607 end if;
11608
11609 -- An effectively volatile object cannot be used as an actual in a
11610 -- generic instantiation (SPARK RM 7.1.3(7)). The following check is
11611 -- relevant only when SPARK_Mode is on as it is not a standard Ada
11612 -- legality rule, and also verifies that the actual is an object.
11613
11614 if SPARK_Mode = On
11615 and then Present (Actual)
11616 and then Is_Object_Reference (Actual)
11617 and then Is_Effectively_Volatile_Object (Actual)
11618 and then not Is_Effectively_Volatile (A_Gen_Obj)
11619 then
11620 Error_Msg_N
11621 ("volatile object cannot act as actual in generic instantiation",
11622 Actual);
11623 end if;
11624
11625 return List;
11626 end Instantiate_Object;
11627
11628 ------------------------------
11629 -- Instantiate_Package_Body --
11630 ------------------------------
11631
11632 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
11633 -- must be replaced by gotos which jump to the end of the routine in order
11634 -- to restore the Ghost and SPARK modes.
11635
11636 procedure Instantiate_Package_Body
11637 (Body_Info : Pending_Body_Info;
11638 Inlined_Body : Boolean := False;
11639 Body_Optional : Boolean := False)
11640 is
11641 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
11642 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Decl);
11643 Act_Spec : constant Node_Id := Specification (Act_Decl);
11644 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
11645 Gen_Id : constant Node_Id := Name (Inst_Node);
11646 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
11647 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
11648 Loc : constant Source_Ptr := Sloc (Inst_Node);
11649
11650 procedure Check_Initialized_Types;
11651 -- In a generic package body, an entity of a generic private type may
11652 -- appear uninitialized. This is suspicious, unless the actual is a
11653 -- fully initialized type.
11654
11655 -----------------------------
11656 -- Check_Initialized_Types --
11657 -----------------------------
11658
11659 procedure Check_Initialized_Types is
11660 Decl : Node_Id;
11661 Formal : Entity_Id;
11662 Actual : Entity_Id;
11663 Uninit_Var : Entity_Id;
11664
11665 begin
11666 Decl := First (Generic_Formal_Declarations (Gen_Decl));
11667 while Present (Decl) loop
11668 Uninit_Var := Empty;
11669
11670 if Nkind (Decl) = N_Private_Extension_Declaration then
11671 Uninit_Var := Uninitialized_Variable (Decl);
11672
11673 elsif Nkind (Decl) = N_Formal_Type_Declaration
11674 and then Nkind (Formal_Type_Definition (Decl)) =
11675 N_Formal_Private_Type_Definition
11676 then
11677 Uninit_Var :=
11678 Uninitialized_Variable (Formal_Type_Definition (Decl));
11679 end if;
11680
11681 if Present (Uninit_Var) then
11682 Formal := Defining_Identifier (Decl);
11683 Actual := First_Entity (Act_Decl_Id);
11684
11685 -- For each formal there is a subtype declaration that renames
11686 -- the actual and has the same name as the formal. Locate the
11687 -- formal for warning message about uninitialized variables
11688 -- in the generic, for which the actual type should be a fully
11689 -- initialized type.
11690
11691 while Present (Actual) loop
11692 exit when Ekind (Actual) = E_Package
11693 and then Present (Renamed_Object (Actual));
11694
11695 if Chars (Actual) = Chars (Formal)
11696 and then not Is_Scalar_Type (Actual)
11697 and then not Is_Fully_Initialized_Type (Actual)
11698 and then Warn_On_No_Value_Assigned
11699 then
11700 Error_Msg_Node_2 := Formal;
11701 Error_Msg_NE
11702 ("generic unit has uninitialized variable& of "
11703 & "formal private type &?v?", Actual, Uninit_Var);
11704 Error_Msg_NE
11705 ("actual type for& should be fully initialized type?v?",
11706 Actual, Formal);
11707 exit;
11708 end if;
11709
11710 Next_Entity (Actual);
11711 end loop;
11712 end if;
11713
11714 Next (Decl);
11715 end loop;
11716 end Check_Initialized_Types;
11717
11718 -- Local variables
11719
11720 -- The following constants capture the context prior to instantiating
11721 -- the package body.
11722
11723 Saved_CS : constant Config_Switches_Type := Save_Config_Switches;
11724 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
11725 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
11726 Saved_ISMP : constant Boolean :=
11727 Ignore_SPARK_Mode_Pragmas_In_Instance;
11728 Saved_LSST : constant Suppress_Stack_Entry_Ptr :=
11729 Local_Suppress_Stack_Top;
11730 Saved_SC : constant Boolean := Style_Check;
11731 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
11732 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
11733 Saved_SS : constant Suppress_Record := Scope_Suppress;
11734 Saved_Warn : constant Warning_Record := Save_Warnings;
11735
11736 Act_Body : Node_Id;
11737 Act_Body_Id : Entity_Id;
11738 Act_Body_Name : Node_Id;
11739 Gen_Body : Node_Id;
11740 Gen_Body_Id : Node_Id;
11741 Par_Ent : Entity_Id := Empty;
11742 Par_Installed : Boolean := False;
11743 Par_Vis : Boolean := False;
11744
11745 Vis_Prims_List : Elist_Id := No_Elist;
11746 -- List of primitives made temporarily visible in the instantiation
11747 -- to match the visibility of the formal type.
11748
11749 -- Start of processing for Instantiate_Package_Body
11750
11751 begin
11752 Gen_Body_Id := Corresponding_Body (Gen_Decl);
11753
11754 -- The instance body may already have been processed, as the parent of
11755 -- another instance that is inlined (Load_Parent_Of_Generic).
11756
11757 if Present (Corresponding_Body (Instance_Spec (Inst_Node))) then
11758 return;
11759 end if;
11760
11761 -- The package being instantiated may be subject to pragma Ghost. Set
11762 -- the mode now to ensure that any nodes generated during instantiation
11763 -- are properly marked as Ghost.
11764
11765 Set_Ghost_Mode (Act_Decl_Id);
11766
11767 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
11768
11769 -- Re-establish the state of information on which checks are suppressed.
11770 -- This information was set in Body_Info at the point of instantiation,
11771 -- and now we restore it so that the instance is compiled using the
11772 -- check status at the instantiation (RM 11.5(7.2/2), AI95-00224-01).
11773
11774 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
11775 Scope_Suppress := Body_Info.Scope_Suppress;
11776
11777 Restore_Config_Switches (Body_Info.Config_Switches);
11778 Restore_Warnings (Body_Info.Warnings);
11779
11780 if No (Gen_Body_Id) then
11781
11782 -- Do not look for parent of generic body if none is required.
11783 -- This may happen when the routine is called as part of the
11784 -- Pending_Instantiations processing, when nested instances
11785 -- may precede the one generated from the main unit.
11786
11787 if not Unit_Requires_Body (Defining_Entity (Gen_Decl))
11788 and then Body_Optional
11789 then
11790 goto Leave;
11791 else
11792 Load_Parent_Of_Generic
11793 (Inst_Node, Specification (Gen_Decl), Body_Optional);
11794
11795 -- Surprisingly enough, loading the body of the parent can cause
11796 -- the body to be instantiated and the double instantiation needs
11797 -- to be prevented in order to avoid giving bogus semantic errors.
11798
11799 -- This case can occur because of the Collect_Previous_Instances
11800 -- machinery of Load_Parent_Of_Generic, which will instantiate
11801 -- bodies that are deemed to be ahead of the body of the parent
11802 -- in the compilation unit. But the relative position of these
11803 -- bodies is computed using the mere comparison of their Sloc.
11804
11805 -- Now suppose that you have two generic packages G and H, with
11806 -- G containing a mere instantiation of H:
11807
11808 -- generic
11809 -- package H is
11810
11811 -- generic
11812 -- package Nested_G is
11813 -- ...
11814 -- end Nested_G;
11815
11816 -- end H;
11817
11818 -- with H;
11819
11820 -- generic
11821 -- package G is
11822
11823 -- package My_H is new H;
11824
11825 -- end G;
11826
11827 -- and a third package Q instantiating G and Nested_G:
11828
11829 -- with G;
11830
11831 -- package Q is
11832
11833 -- package My_G is new G;
11834
11835 -- package My_Nested_G is new My_G.My_H.Nested_G;
11836
11837 -- end Q;
11838
11839 -- The body to be instantiated is that of My_Nested_G and its
11840 -- parent is the instance My_G.My_H. This latter instantiation
11841 -- is done when My_G is analyzed, i.e. after the declarations
11842 -- of My_G and My_Nested_G have been parsed; as a result, the
11843 -- Sloc of My_G.My_H is greater than the Sloc of My_Nested_G.
11844
11845 -- Therefore loading the body of My_G.My_H will cause the body
11846 -- of My_Nested_G to be instantiated because it is deemed to be
11847 -- ahead of My_G.My_H. This means that Load_Parent_Of_Generic
11848 -- will again be invoked on My_G.My_H, but this time with the
11849 -- Collect_Previous_Instances machinery disabled, so there is
11850 -- no endless mutual recursion and things are done in order.
11851
11852 if Present (Corresponding_Body (Instance_Spec (Inst_Node))) then
11853 goto Leave;
11854 end if;
11855
11856 Gen_Body_Id := Corresponding_Body (Gen_Decl);
11857 end if;
11858 end if;
11859
11860 -- Establish global variable for sloc adjustment and for error recovery
11861 -- In the case of an instance body for an instantiation with actuals
11862 -- from a limited view, the instance body is placed at the beginning
11863 -- of the enclosing package body: use the body entity as the source
11864 -- location for nodes of the instance body.
11865
11866 if not Is_Empty_Elmt_List (Incomplete_Actuals (Act_Decl_Id)) then
11867 declare
11868 Scop : constant Entity_Id := Scope (Act_Decl_Id);
11869 Body_Id : constant Node_Id :=
11870 Corresponding_Body (Unit_Declaration_Node (Scop));
11871
11872 begin
11873 Instantiation_Node := Body_Id;
11874 end;
11875 else
11876 Instantiation_Node := Inst_Node;
11877 end if;
11878
11879 if Present (Gen_Body_Id) then
11880 Save_Env (Gen_Unit, Act_Decl_Id);
11881 Style_Check := False;
11882
11883 -- If the context of the instance is subject to SPARK_Mode "off", the
11884 -- annotation is missing, or the body is instantiated at a later pass
11885 -- and its spec ignored SPARK_Mode pragma, set the global flag which
11886 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within the
11887 -- instance.
11888
11889 if SPARK_Mode /= On
11890 or else Ignore_SPARK_Mode_Pragmas (Act_Decl_Id)
11891 then
11892 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
11893 end if;
11894
11895 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
11896 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
11897
11898 Create_Instantiation_Source
11899 (Inst_Node, Gen_Body_Id, S_Adjustment);
11900
11901 Act_Body :=
11902 Copy_Generic_Node
11903 (Original_Node (Gen_Body), Empty, Instantiating => True);
11904
11905 -- Create proper (possibly qualified) defining name for the body, to
11906 -- correspond to the one in the spec.
11907
11908 Act_Body_Id :=
11909 Make_Defining_Identifier (Sloc (Act_Decl_Id), Chars (Act_Decl_Id));
11910 Preserve_Comes_From_Source (Act_Body_Id, Act_Decl_Id);
11911
11912 -- Some attributes of spec entity are not inherited by body entity
11913
11914 Set_Handler_Records (Act_Body_Id, No_List);
11915
11916 if Nkind (Defining_Unit_Name (Act_Spec)) =
11917 N_Defining_Program_Unit_Name
11918 then
11919 Act_Body_Name :=
11920 Make_Defining_Program_Unit_Name (Loc,
11921 Name =>
11922 New_Copy_Tree (Name (Defining_Unit_Name (Act_Spec))),
11923 Defining_Identifier => Act_Body_Id);
11924 else
11925 Act_Body_Name := Act_Body_Id;
11926 end if;
11927
11928 Set_Defining_Unit_Name (Act_Body, Act_Body_Name);
11929
11930 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
11931 Check_Generic_Actuals (Act_Decl_Id, False);
11932 Check_Initialized_Types;
11933
11934 -- Install primitives hidden at the point of the instantiation but
11935 -- visible when processing the generic formals
11936
11937 declare
11938 E : Entity_Id;
11939
11940 begin
11941 E := First_Entity (Act_Decl_Id);
11942 while Present (E) loop
11943 if Is_Type (E)
11944 and then not Is_Itype (E)
11945 and then Is_Generic_Actual_Type (E)
11946 and then Is_Tagged_Type (E)
11947 then
11948 Install_Hidden_Primitives
11949 (Prims_List => Vis_Prims_List,
11950 Gen_T => Generic_Parent_Type (Parent (E)),
11951 Act_T => E);
11952 end if;
11953
11954 Next_Entity (E);
11955 end loop;
11956 end;
11957
11958 -- If it is a child unit, make the parent instance (which is an
11959 -- instance of the parent of the generic) visible. The parent
11960 -- instance is the prefix of the name of the generic unit.
11961
11962 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
11963 and then Nkind (Gen_Id) = N_Expanded_Name
11964 then
11965 Par_Ent := Entity (Prefix (Gen_Id));
11966 Par_Vis := Is_Immediately_Visible (Par_Ent);
11967 Install_Parent (Par_Ent, In_Body => True);
11968 Par_Installed := True;
11969
11970 elsif Is_Child_Unit (Gen_Unit) then
11971 Par_Ent := Scope (Gen_Unit);
11972 Par_Vis := Is_Immediately_Visible (Par_Ent);
11973 Install_Parent (Par_Ent, In_Body => True);
11974 Par_Installed := True;
11975 end if;
11976
11977 -- If the instantiation is a library unit, and this is the main unit,
11978 -- then build the resulting compilation unit nodes for the instance.
11979 -- If this is a compilation unit but it is not the main unit, then it
11980 -- is the body of a unit in the context, that is being compiled
11981 -- because it is encloses some inlined unit or another generic unit
11982 -- being instantiated. In that case, this body is not part of the
11983 -- current compilation, and is not attached to the tree, but its
11984 -- parent must be set for analysis.
11985
11986 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
11987
11988 -- Replace instance node with body of instance, and create new
11989 -- node for corresponding instance declaration.
11990
11991 Build_Instance_Compilation_Unit_Nodes
11992 (Inst_Node, Act_Body, Act_Decl);
11993 Analyze (Inst_Node);
11994
11995 if Parent (Inst_Node) = Cunit (Main_Unit) then
11996
11997 -- If the instance is a child unit itself, then set the scope
11998 -- of the expanded body to be the parent of the instantiation
11999 -- (ensuring that the fully qualified name will be generated
12000 -- for the elaboration subprogram).
12001
12002 if Nkind (Defining_Unit_Name (Act_Spec)) =
12003 N_Defining_Program_Unit_Name
12004 then
12005 Set_Scope (Defining_Entity (Inst_Node), Scope (Act_Decl_Id));
12006 end if;
12007 end if;
12008
12009 -- Case where instantiation is not a library unit
12010
12011 else
12012 -- If this is an early instantiation, i.e. appears textually
12013 -- before the corresponding body and must be elaborated first,
12014 -- indicate that the body instance is to be delayed.
12015
12016 Install_Body (Act_Body, Inst_Node, Gen_Body, Gen_Decl);
12017 Analyze (Act_Body);
12018 end if;
12019
12020 Inherit_Context (Gen_Body, Inst_Node);
12021
12022 -- Remove the parent instances if they have been placed on the scope
12023 -- stack to compile the body.
12024
12025 if Par_Installed then
12026 Remove_Parent (In_Body => True);
12027
12028 -- Restore the previous visibility of the parent
12029
12030 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
12031 end if;
12032
12033 Restore_Hidden_Primitives (Vis_Prims_List);
12034 Restore_Private_Views (Act_Decl_Id);
12035
12036 -- Remove the current unit from visibility if this is an instance
12037 -- that is not elaborated on the fly for inlining purposes.
12038
12039 if not Inlined_Body then
12040 Set_Is_Immediately_Visible (Act_Decl_Id, False);
12041 end if;
12042
12043 Restore_Env;
12044
12045 -- If we have no body, and the unit requires a body, then complain. This
12046 -- complaint is suppressed if we have detected other errors (since a
12047 -- common reason for missing the body is that it had errors).
12048 -- In CodePeer mode, a warning has been emitted already, no need for
12049 -- further messages.
12050
12051 elsif Unit_Requires_Body (Gen_Unit)
12052 and then not Body_Optional
12053 then
12054 if CodePeer_Mode then
12055 null;
12056
12057 elsif Serious_Errors_Detected = 0 then
12058 Error_Msg_NE
12059 ("cannot find body of generic package &", Inst_Node, Gen_Unit);
12060
12061 -- Don't attempt to perform any cleanup actions if some other error
12062 -- was already detected, since this can cause blowups.
12063
12064 else
12065 goto Leave;
12066 end if;
12067
12068 -- Case of package that does not need a body
12069
12070 else
12071 -- If the instantiation of the declaration is a library unit, rewrite
12072 -- the original package instantiation as a package declaration in the
12073 -- compilation unit node.
12074
12075 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
12076 Set_Parent_Spec (Act_Decl, Parent_Spec (Inst_Node));
12077 Rewrite (Inst_Node, Act_Decl);
12078
12079 -- Generate elaboration entity, in case spec has elaboration code.
12080 -- This cannot be done when the instance is analyzed, because it
12081 -- is not known yet whether the body exists.
12082
12083 Set_Elaboration_Entity_Required (Act_Decl_Id, False);
12084 Build_Elaboration_Entity (Parent (Inst_Node), Act_Decl_Id);
12085
12086 -- If the instantiation is not a library unit, then append the
12087 -- declaration to the list of implicitly generated entities, unless
12088 -- it is already a list member which means that it was already
12089 -- processed
12090
12091 elsif not Is_List_Member (Act_Decl) then
12092 Mark_Rewrite_Insertion (Act_Decl);
12093 Insert_Before (Inst_Node, Act_Decl);
12094 end if;
12095 end if;
12096
12097 <<Leave>>
12098
12099 -- Restore the context that was in effect prior to instantiating the
12100 -- package body.
12101
12102 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
12103 Local_Suppress_Stack_Top := Saved_LSST;
12104 Scope_Suppress := Saved_SS;
12105 Style_Check := Saved_SC;
12106
12107 Expander_Mode_Restore;
12108 Restore_Config_Switches (Saved_CS);
12109 Restore_Ghost_Region (Saved_GM, Saved_IGR);
12110 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
12111 Restore_Warnings (Saved_Warn);
12112 end Instantiate_Package_Body;
12113
12114 ---------------------------------
12115 -- Instantiate_Subprogram_Body --
12116 ---------------------------------
12117
12118 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
12119 -- must be replaced by gotos which jump to the end of the routine in order
12120 -- to restore the Ghost and SPARK modes.
12121
12122 procedure Instantiate_Subprogram_Body
12123 (Body_Info : Pending_Body_Info;
12124 Body_Optional : Boolean := False)
12125 is
12126 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
12127 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Decl);
12128 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
12129 Gen_Id : constant Node_Id := Name (Inst_Node);
12130 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
12131 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
12132 Loc : constant Source_Ptr := Sloc (Inst_Node);
12133 Pack_Id : constant Entity_Id :=
12134 Defining_Unit_Name (Parent (Act_Decl));
12135
12136 -- The following constants capture the context prior to instantiating
12137 -- the subprogram body.
12138
12139 Saved_CS : constant Config_Switches_Type := Save_Config_Switches;
12140 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
12141 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
12142 Saved_ISMP : constant Boolean :=
12143 Ignore_SPARK_Mode_Pragmas_In_Instance;
12144 Saved_LSST : constant Suppress_Stack_Entry_Ptr :=
12145 Local_Suppress_Stack_Top;
12146 Saved_SC : constant Boolean := Style_Check;
12147 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
12148 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
12149 Saved_SS : constant Suppress_Record := Scope_Suppress;
12150 Saved_Warn : constant Warning_Record := Save_Warnings;
12151
12152 Act_Body : Node_Id;
12153 Act_Body_Id : Entity_Id;
12154 Gen_Body : Node_Id;
12155 Gen_Body_Id : Node_Id;
12156 Pack_Body : Node_Id;
12157 Par_Ent : Entity_Id := Empty;
12158 Par_Installed : Boolean := False;
12159 Par_Vis : Boolean := False;
12160 Ret_Expr : Node_Id;
12161
12162 begin
12163 Gen_Body_Id := Corresponding_Body (Gen_Decl);
12164
12165 -- Subprogram body may have been created already because of an inline
12166 -- pragma, or because of multiple elaborations of the enclosing package
12167 -- when several instances of the subprogram appear in the main unit.
12168
12169 if Present (Corresponding_Body (Act_Decl)) then
12170 return;
12171 end if;
12172
12173 -- The subprogram being instantiated may be subject to pragma Ghost. Set
12174 -- the mode now to ensure that any nodes generated during instantiation
12175 -- are properly marked as Ghost.
12176
12177 Set_Ghost_Mode (Act_Decl_Id);
12178
12179 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
12180
12181 -- Re-establish the state of information on which checks are suppressed.
12182 -- This information was set in Body_Info at the point of instantiation,
12183 -- and now we restore it so that the instance is compiled using the
12184 -- check status at the instantiation (RM 11.5(7.2/2), AI95-00224-01).
12185
12186 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
12187 Scope_Suppress := Body_Info.Scope_Suppress;
12188
12189 Restore_Config_Switches (Body_Info.Config_Switches);
12190 Restore_Warnings (Body_Info.Warnings);
12191
12192 if No (Gen_Body_Id) then
12193
12194 -- For imported generic subprogram, no body to compile, complete
12195 -- the spec entity appropriately.
12196
12197 if Is_Imported (Gen_Unit) then
12198 Set_Is_Imported (Act_Decl_Id);
12199 Set_First_Rep_Item (Act_Decl_Id, First_Rep_Item (Gen_Unit));
12200 Set_Interface_Name (Act_Decl_Id, Interface_Name (Gen_Unit));
12201 Set_Convention (Act_Decl_Id, Convention (Gen_Unit));
12202 Set_Has_Completion (Act_Decl_Id);
12203 goto Leave;
12204
12205 -- For other cases, compile the body
12206
12207 else
12208 Load_Parent_Of_Generic
12209 (Inst_Node, Specification (Gen_Decl), Body_Optional);
12210 Gen_Body_Id := Corresponding_Body (Gen_Decl);
12211 end if;
12212 end if;
12213
12214 Instantiation_Node := Inst_Node;
12215
12216 if Present (Gen_Body_Id) then
12217 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
12218
12219 if Nkind (Gen_Body) = N_Subprogram_Body_Stub then
12220
12221 -- Either body is not present, or context is non-expanding, as
12222 -- when compiling a subunit. Mark the instance as completed, and
12223 -- diagnose a missing body when needed.
12224
12225 if Expander_Active
12226 and then Operating_Mode = Generate_Code
12227 then
12228 Error_Msg_N ("missing proper body for instantiation", Gen_Body);
12229 end if;
12230
12231 Set_Has_Completion (Act_Decl_Id);
12232 goto Leave;
12233 end if;
12234
12235 Save_Env (Gen_Unit, Act_Decl_Id);
12236 Style_Check := False;
12237
12238 -- If the context of the instance is subject to SPARK_Mode "off", the
12239 -- annotation is missing, or the body is instantiated at a later pass
12240 -- and its spec ignored SPARK_Mode pragma, set the global flag which
12241 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within the
12242 -- instance.
12243
12244 if SPARK_Mode /= On
12245 or else Ignore_SPARK_Mode_Pragmas (Act_Decl_Id)
12246 then
12247 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
12248 end if;
12249
12250 -- If the context of an instance is not subject to SPARK_Mode "off",
12251 -- and the generic body is subject to an explicit SPARK_Mode pragma,
12252 -- the latter should be the one applicable to the instance.
12253
12254 if not Ignore_SPARK_Mode_Pragmas_In_Instance
12255 and then SPARK_Mode /= Off
12256 and then Present (SPARK_Pragma (Gen_Body_Id))
12257 then
12258 Set_SPARK_Mode (Gen_Body_Id);
12259 end if;
12260
12261 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
12262 Create_Instantiation_Source
12263 (Inst_Node,
12264 Gen_Body_Id,
12265 S_Adjustment);
12266
12267 Act_Body :=
12268 Copy_Generic_Node
12269 (Original_Node (Gen_Body), Empty, Instantiating => True);
12270
12271 -- Create proper defining name for the body, to correspond to the one
12272 -- in the spec.
12273
12274 Act_Body_Id :=
12275 Make_Defining_Identifier (Sloc (Act_Decl_Id), Chars (Act_Decl_Id));
12276
12277 Preserve_Comes_From_Source (Act_Body_Id, Act_Decl_Id);
12278 Set_Defining_Unit_Name (Specification (Act_Body), Act_Body_Id);
12279
12280 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
12281 Set_Has_Completion (Act_Decl_Id);
12282 Check_Generic_Actuals (Pack_Id, False);
12283
12284 -- Generate a reference to link the visible subprogram instance to
12285 -- the generic body, which for navigation purposes is the only
12286 -- available source for the instance.
12287
12288 Generate_Reference
12289 (Related_Instance (Pack_Id),
12290 Gen_Body_Id, 'b', Set_Ref => False, Force => True);
12291
12292 -- If it is a child unit, make the parent instance (which is an
12293 -- instance of the parent of the generic) visible. The parent
12294 -- instance is the prefix of the name of the generic unit.
12295
12296 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
12297 and then Nkind (Gen_Id) = N_Expanded_Name
12298 then
12299 Par_Ent := Entity (Prefix (Gen_Id));
12300 Par_Vis := Is_Immediately_Visible (Par_Ent);
12301 Install_Parent (Par_Ent, In_Body => True);
12302 Par_Installed := True;
12303
12304 elsif Is_Child_Unit (Gen_Unit) then
12305 Par_Ent := Scope (Gen_Unit);
12306 Par_Vis := Is_Immediately_Visible (Par_Ent);
12307 Install_Parent (Par_Ent, In_Body => True);
12308 Par_Installed := True;
12309 end if;
12310
12311 -- Subprogram body is placed in the body of wrapper package,
12312 -- whose spec contains the subprogram declaration as well as
12313 -- the renaming declarations for the generic parameters.
12314
12315 Pack_Body :=
12316 Make_Package_Body (Loc,
12317 Defining_Unit_Name => New_Copy (Pack_Id),
12318 Declarations => New_List (Act_Body));
12319
12320 Set_Corresponding_Spec (Pack_Body, Pack_Id);
12321
12322 -- If the instantiation is a library unit, then build resulting
12323 -- compilation unit nodes for the instance. The declaration of
12324 -- the enclosing package is the grandparent of the subprogram
12325 -- declaration. First replace the instantiation node as the unit
12326 -- of the corresponding compilation.
12327
12328 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
12329 if Parent (Inst_Node) = Cunit (Main_Unit) then
12330 Set_Unit (Parent (Inst_Node), Inst_Node);
12331 Build_Instance_Compilation_Unit_Nodes
12332 (Inst_Node, Pack_Body, Parent (Parent (Act_Decl)));
12333 Analyze (Inst_Node);
12334 else
12335 Set_Parent (Pack_Body, Parent (Inst_Node));
12336 Analyze (Pack_Body);
12337 end if;
12338
12339 else
12340 Insert_Before (Inst_Node, Pack_Body);
12341 Mark_Rewrite_Insertion (Pack_Body);
12342 Analyze (Pack_Body);
12343
12344 if Expander_Active then
12345 Freeze_Subprogram_Body (Inst_Node, Gen_Body, Pack_Id);
12346 end if;
12347 end if;
12348
12349 Inherit_Context (Gen_Body, Inst_Node);
12350
12351 Restore_Private_Views (Pack_Id, False);
12352
12353 if Par_Installed then
12354 Remove_Parent (In_Body => True);
12355
12356 -- Restore the previous visibility of the parent
12357
12358 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
12359 end if;
12360
12361 Restore_Env;
12362
12363 -- Body not found. Error was emitted already. If there were no previous
12364 -- errors, this may be an instance whose scope is a premature instance.
12365 -- In that case we must insure that the (legal) program does raise
12366 -- program error if executed. We generate a subprogram body for this
12367 -- purpose. See DEC ac30vso.
12368
12369 -- Should not reference proprietary DEC tests in comments ???
12370
12371 elsif Serious_Errors_Detected = 0
12372 and then Nkind (Parent (Inst_Node)) /= N_Compilation_Unit
12373 then
12374 if Body_Optional then
12375 goto Leave;
12376
12377 elsif Ekind (Act_Decl_Id) = E_Procedure then
12378 Act_Body :=
12379 Make_Subprogram_Body (Loc,
12380 Specification =>
12381 Make_Procedure_Specification (Loc,
12382 Defining_Unit_Name =>
12383 Make_Defining_Identifier (Loc, Chars (Act_Decl_Id)),
12384 Parameter_Specifications =>
12385 New_Copy_List
12386 (Parameter_Specifications (Parent (Act_Decl_Id)))),
12387
12388 Declarations => Empty_List,
12389 Handled_Statement_Sequence =>
12390 Make_Handled_Sequence_Of_Statements (Loc,
12391 Statements => New_List (
12392 Make_Raise_Program_Error (Loc,
12393 Reason => PE_Access_Before_Elaboration))));
12394
12395 else
12396 Ret_Expr :=
12397 Make_Raise_Program_Error (Loc,
12398 Reason => PE_Access_Before_Elaboration);
12399
12400 Set_Etype (Ret_Expr, (Etype (Act_Decl_Id)));
12401 Set_Analyzed (Ret_Expr);
12402
12403 Act_Body :=
12404 Make_Subprogram_Body (Loc,
12405 Specification =>
12406 Make_Function_Specification (Loc,
12407 Defining_Unit_Name =>
12408 Make_Defining_Identifier (Loc, Chars (Act_Decl_Id)),
12409 Parameter_Specifications =>
12410 New_Copy_List
12411 (Parameter_Specifications (Parent (Act_Decl_Id))),
12412 Result_Definition =>
12413 New_Occurrence_Of (Etype (Act_Decl_Id), Loc)),
12414
12415 Declarations => Empty_List,
12416 Handled_Statement_Sequence =>
12417 Make_Handled_Sequence_Of_Statements (Loc,
12418 Statements => New_List (
12419 Make_Simple_Return_Statement (Loc, Ret_Expr))));
12420 end if;
12421
12422 Pack_Body :=
12423 Make_Package_Body (Loc,
12424 Defining_Unit_Name => New_Copy (Pack_Id),
12425 Declarations => New_List (Act_Body));
12426
12427 Insert_After (Inst_Node, Pack_Body);
12428 Set_Corresponding_Spec (Pack_Body, Pack_Id);
12429 Analyze (Pack_Body);
12430 end if;
12431
12432 <<Leave>>
12433
12434 -- Restore the context that was in effect prior to instantiating the
12435 -- subprogram body.
12436
12437 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
12438 Local_Suppress_Stack_Top := Saved_LSST;
12439 Scope_Suppress := Saved_SS;
12440 Style_Check := Saved_SC;
12441
12442 Expander_Mode_Restore;
12443 Restore_Config_Switches (Saved_CS);
12444 Restore_Ghost_Region (Saved_GM, Saved_IGR);
12445 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
12446 Restore_Warnings (Saved_Warn);
12447 end Instantiate_Subprogram_Body;
12448
12449 ----------------------
12450 -- Instantiate_Type --
12451 ----------------------
12452
12453 function Instantiate_Type
12454 (Formal : Node_Id;
12455 Actual : Node_Id;
12456 Analyzed_Formal : Node_Id;
12457 Actual_Decls : List_Id) return List_Id
12458 is
12459 A_Gen_T : constant Entity_Id :=
12460 Defining_Identifier (Analyzed_Formal);
12461 Def : constant Node_Id := Formal_Type_Definition (Formal);
12462 Gen_T : constant Entity_Id := Defining_Identifier (Formal);
12463 Act_T : Entity_Id;
12464 Ancestor : Entity_Id := Empty;
12465 Decl_Node : Node_Id;
12466 Decl_Nodes : List_Id;
12467 Loc : Source_Ptr;
12468 Subt : Entity_Id;
12469
12470 procedure Check_Shared_Variable_Control_Aspects;
12471 -- Ada 2020: Verify that shared variable control aspects (RM C.6)
12472 -- that may be specified for a formal type are obeyed by the actual.
12473
12474 procedure Diagnose_Predicated_Actual;
12475 -- There are a number of constructs in which a discrete type with
12476 -- predicates is illegal, e.g. as an index in an array type declaration.
12477 -- If a generic type is used is such a construct in a generic package
12478 -- declaration, it carries the flag No_Predicate_On_Actual. it is part
12479 -- of the generic contract that the actual cannot have predicates.
12480
12481 procedure Validate_Array_Type_Instance;
12482 procedure Validate_Access_Subprogram_Instance;
12483 procedure Validate_Access_Type_Instance;
12484 procedure Validate_Derived_Type_Instance;
12485 procedure Validate_Derived_Interface_Type_Instance;
12486 procedure Validate_Discriminated_Formal_Type;
12487 procedure Validate_Interface_Type_Instance;
12488 procedure Validate_Private_Type_Instance;
12489 procedure Validate_Incomplete_Type_Instance;
12490 -- These procedures perform validation tests for the named case.
12491 -- Validate_Discriminated_Formal_Type is shared by formal private
12492 -- types and Ada 2012 formal incomplete types.
12493
12494 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean;
12495 -- Check that base types are the same and that the subtypes match
12496 -- statically. Used in several of the above.
12497
12498 --------------------------------------------
12499 -- Check_Shared_Variable_Control_Aspects --
12500 --------------------------------------------
12501
12502 -- Ada 2020: Verify that shared variable control aspects (RM C.6)
12503 -- that may be specified for the formal are obeyed by the actual.
12504 -- If the formal is a derived type the aspect specifications must match.
12505 -- NOTE: AI12-0282 implies that matching of aspects is required between
12506 -- formal and actual in all cases, but this is too restrictive.
12507 -- In particular it violates a language design rule: a limited private
12508 -- indefinite formal can be matched by any actual. The current code
12509 -- reflects an older and more permissive version of RM C.6 (12/5).
12510
12511 procedure Check_Shared_Variable_Control_Aspects is
12512 begin
12513 if Ada_Version >= Ada_2020 then
12514 if Is_Atomic (A_Gen_T) and then not Is_Atomic (Act_T) then
12515 Error_Msg_NE
12516 ("actual for& must have Atomic aspect", Actual, A_Gen_T);
12517
12518 elsif Is_Derived_Type (A_Gen_T)
12519 and then Is_Atomic (A_Gen_T) /= Is_Atomic (Act_T)
12520 then
12521 Error_Msg_NE
12522 ("actual for& has different Atomic aspect", Actual, A_Gen_T);
12523 end if;
12524
12525 if Is_Volatile (A_Gen_T) and then not Is_Volatile (Act_T) then
12526 Error_Msg_NE
12527 ("actual for& has different Volatile aspect",
12528 Actual, A_Gen_T);
12529
12530 elsif Is_Derived_Type (A_Gen_T)
12531 and then Is_Volatile (A_Gen_T) /= Is_Volatile (Act_T)
12532 then
12533 Error_Msg_NE
12534 ("actual for& has different Volatile aspect",
12535 Actual, A_Gen_T);
12536 end if;
12537
12538 -- We assume that an array type whose atomic component type
12539 -- is Atomic is equivalent to an array type with the explicit
12540 -- aspect Has_Atomic_Components. This is a reasonable inference
12541 -- from the intent of AI12-0282, and makes it legal to use an
12542 -- actual that does not have the identical aspect as the formal.
12543 -- Ditto for volatile components.
12544
12545 declare
12546 Actual_Atomic_Comp : constant Boolean :=
12547 Has_Atomic_Components (Act_T)
12548 or else (Is_Array_Type (Act_T)
12549 and then Is_Atomic (Component_Type (Act_T)));
12550 begin
12551 if Has_Atomic_Components (A_Gen_T) /= Actual_Atomic_Comp then
12552 Error_Msg_NE
12553 ("formal and actual for& must agree on atomic components",
12554 Actual, A_Gen_T);
12555 end if;
12556 end;
12557
12558 declare
12559 Actual_Volatile_Comp : constant Boolean :=
12560 Has_Volatile_Components (Act_T)
12561 or else (Is_Array_Type (Act_T)
12562 and then Is_Volatile (Component_Type (Act_T)));
12563 begin
12564 if Has_Volatile_Components (A_Gen_T) /= Actual_Volatile_Comp
12565 then
12566 Error_Msg_NE
12567 ("actual for& must have volatile components",
12568 Actual, A_Gen_T);
12569 end if;
12570 end;
12571
12572 -- The following two aspects do not require exact matching,
12573 -- but only one-way agreement. See RM C.6.
12574
12575 if Is_Independent (A_Gen_T) and then not Is_Independent (Act_T)
12576 then
12577 Error_Msg_NE
12578 ("actual for& must have Independent aspect specified",
12579 Actual, A_Gen_T);
12580 end if;
12581
12582 if Has_Independent_Components (A_Gen_T)
12583 and then not Has_Independent_Components (Act_T)
12584 then
12585 Error_Msg_NE
12586 ("actual for& must have Independent_Components specified",
12587 Actual, A_Gen_T);
12588 end if;
12589
12590 -- Check actual/formal compatibility with respect to the four
12591 -- volatility refinement aspects.
12592
12593 Check_Volatility_Compatibility
12594 (Act_T, A_Gen_T,
12595 "actual type", "its corresponding formal type",
12596 Srcpos_Bearer => Act_T);
12597 end if;
12598 end Check_Shared_Variable_Control_Aspects;
12599
12600 ---------------------------------
12601 -- Diagnose_Predicated_Actual --
12602 ---------------------------------
12603
12604 procedure Diagnose_Predicated_Actual is
12605 begin
12606 if No_Predicate_On_Actual (A_Gen_T)
12607 and then Has_Predicates (Act_T)
12608 then
12609 Error_Msg_NE
12610 ("actual for& cannot be a type with predicate",
12611 Instantiation_Node, A_Gen_T);
12612
12613 elsif No_Dynamic_Predicate_On_Actual (A_Gen_T)
12614 and then Has_Predicates (Act_T)
12615 and then not Has_Static_Predicate_Aspect (Act_T)
12616 then
12617 Error_Msg_NE
12618 ("actual for& cannot be a type with a dynamic predicate",
12619 Instantiation_Node, A_Gen_T);
12620 end if;
12621 end Diagnose_Predicated_Actual;
12622
12623 --------------------
12624 -- Subtypes_Match --
12625 --------------------
12626
12627 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean is
12628 T : constant Entity_Id := Get_Instance_Of (Gen_T);
12629
12630 begin
12631 -- Some detailed comments would be useful here ???
12632
12633 return ((Base_Type (T) = Act_T
12634 or else Base_Type (T) = Base_Type (Act_T))
12635 and then Subtypes_Statically_Match (T, Act_T))
12636
12637 or else (Is_Class_Wide_Type (Gen_T)
12638 and then Is_Class_Wide_Type (Act_T)
12639 and then Subtypes_Match
12640 (Get_Instance_Of (Root_Type (Gen_T)),
12641 Root_Type (Act_T)))
12642
12643 or else
12644 (Ekind_In (Gen_T, E_Anonymous_Access_Subprogram_Type,
12645 E_Anonymous_Access_Type)
12646 and then Ekind (Act_T) = Ekind (Gen_T)
12647 and then Subtypes_Statically_Match
12648 (Designated_Type (Gen_T), Designated_Type (Act_T)));
12649 end Subtypes_Match;
12650
12651 -----------------------------------------
12652 -- Validate_Access_Subprogram_Instance --
12653 -----------------------------------------
12654
12655 procedure Validate_Access_Subprogram_Instance is
12656 begin
12657 if not Is_Access_Type (Act_T)
12658 or else Ekind (Designated_Type (Act_T)) /= E_Subprogram_Type
12659 then
12660 Error_Msg_NE
12661 ("expect access type in instantiation of &", Actual, Gen_T);
12662 Abandon_Instantiation (Actual);
12663 end if;
12664
12665 -- According to AI05-288, actuals for access_to_subprograms must be
12666 -- subtype conformant with the generic formal. Previous to AI05-288
12667 -- only mode conformance was required.
12668
12669 -- This is a binding interpretation that applies to previous versions
12670 -- of the language, no need to maintain previous weaker checks.
12671
12672 Check_Subtype_Conformant
12673 (Designated_Type (Act_T),
12674 Designated_Type (A_Gen_T),
12675 Actual,
12676 Get_Inst => True);
12677
12678 if Ekind (Base_Type (Act_T)) = E_Access_Protected_Subprogram_Type then
12679 if Ekind (A_Gen_T) = E_Access_Subprogram_Type then
12680 Error_Msg_NE
12681 ("protected access type not allowed for formal &",
12682 Actual, Gen_T);
12683 end if;
12684
12685 elsif Ekind (A_Gen_T) = E_Access_Protected_Subprogram_Type then
12686 Error_Msg_NE
12687 ("expect protected access type for formal &",
12688 Actual, Gen_T);
12689 end if;
12690
12691 -- If the formal has a specified convention (which in most cases
12692 -- will be StdCall) verify that the actual has the same convention.
12693
12694 if Has_Convention_Pragma (A_Gen_T)
12695 and then Convention (A_Gen_T) /= Convention (Act_T)
12696 then
12697 Error_Msg_Name_1 := Get_Convention_Name (Convention (A_Gen_T));
12698 Error_Msg_NE
12699 ("actual for formal & must have convention %", Actual, Gen_T);
12700 end if;
12701
12702 if Can_Never_Be_Null (A_Gen_T) /= Can_Never_Be_Null (Act_T) then
12703 Error_Msg_NE
12704 ("non null exclusion of actual and formal & do not match",
12705 Actual, Gen_T);
12706 end if;
12707 end Validate_Access_Subprogram_Instance;
12708
12709 -----------------------------------
12710 -- Validate_Access_Type_Instance --
12711 -----------------------------------
12712
12713 procedure Validate_Access_Type_Instance is
12714 Desig_Type : constant Entity_Id :=
12715 Find_Actual_Type (Designated_Type (A_Gen_T), A_Gen_T);
12716 Desig_Act : Entity_Id;
12717
12718 begin
12719 if not Is_Access_Type (Act_T) then
12720 Error_Msg_NE
12721 ("expect access type in instantiation of &", Actual, Gen_T);
12722 Abandon_Instantiation (Actual);
12723 end if;
12724
12725 if Is_Access_Constant (A_Gen_T) then
12726 if not Is_Access_Constant (Act_T) then
12727 Error_Msg_N
12728 ("actual type must be access-to-constant type", Actual);
12729 Abandon_Instantiation (Actual);
12730 end if;
12731 else
12732 if Is_Access_Constant (Act_T) then
12733 Error_Msg_N
12734 ("actual type must be access-to-variable type", Actual);
12735 Abandon_Instantiation (Actual);
12736
12737 elsif Ekind (A_Gen_T) = E_General_Access_Type
12738 and then Ekind (Base_Type (Act_T)) /= E_General_Access_Type
12739 then
12740 Error_Msg_N -- CODEFIX
12741 ("actual must be general access type!", Actual);
12742 Error_Msg_NE -- CODEFIX
12743 ("add ALL to }!", Actual, Act_T);
12744 Abandon_Instantiation (Actual);
12745 end if;
12746 end if;
12747
12748 -- The designated subtypes, that is to say the subtypes introduced
12749 -- by an access type declaration (and not by a subtype declaration)
12750 -- must match.
12751
12752 Desig_Act := Designated_Type (Base_Type (Act_T));
12753
12754 -- The designated type may have been introduced through a limited_
12755 -- with clause, in which case retrieve the non-limited view. This
12756 -- applies to incomplete types as well as to class-wide types.
12757
12758 if From_Limited_With (Desig_Act) then
12759 Desig_Act := Available_View (Desig_Act);
12760 end if;
12761
12762 if not Subtypes_Match (Desig_Type, Desig_Act) then
12763 Error_Msg_NE
12764 ("designated type of actual does not match that of formal &",
12765 Actual, Gen_T);
12766
12767 if not Predicates_Match (Desig_Type, Desig_Act) then
12768 Error_Msg_N ("\predicates do not match", Actual);
12769 end if;
12770
12771 Abandon_Instantiation (Actual);
12772
12773 elsif Is_Access_Type (Designated_Type (Act_T))
12774 and then Is_Constrained (Designated_Type (Designated_Type (Act_T)))
12775 /=
12776 Is_Constrained (Designated_Type (Desig_Type))
12777 then
12778 Error_Msg_NE
12779 ("designated type of actual does not match that of formal &",
12780 Actual, Gen_T);
12781
12782 if not Predicates_Match (Desig_Type, Desig_Act) then
12783 Error_Msg_N ("\predicates do not match", Actual);
12784 end if;
12785
12786 Abandon_Instantiation (Actual);
12787 end if;
12788
12789 -- Ada 2005: null-exclusion indicators of the two types must agree
12790
12791 if Can_Never_Be_Null (A_Gen_T) /= Can_Never_Be_Null (Act_T) then
12792 Error_Msg_NE
12793 ("non null exclusion of actual and formal & do not match",
12794 Actual, Gen_T);
12795 end if;
12796 end Validate_Access_Type_Instance;
12797
12798 ----------------------------------
12799 -- Validate_Array_Type_Instance --
12800 ----------------------------------
12801
12802 procedure Validate_Array_Type_Instance is
12803 I1 : Node_Id;
12804 I2 : Node_Id;
12805 T2 : Entity_Id;
12806
12807 function Formal_Dimensions return Nat;
12808 -- Count number of dimensions in array type formal
12809
12810 -----------------------
12811 -- Formal_Dimensions --
12812 -----------------------
12813
12814 function Formal_Dimensions return Nat is
12815 Num : Nat := 0;
12816 Index : Node_Id;
12817
12818 begin
12819 if Nkind (Def) = N_Constrained_Array_Definition then
12820 Index := First (Discrete_Subtype_Definitions (Def));
12821 else
12822 Index := First (Subtype_Marks (Def));
12823 end if;
12824
12825 while Present (Index) loop
12826 Num := Num + 1;
12827 Next_Index (Index);
12828 end loop;
12829
12830 return Num;
12831 end Formal_Dimensions;
12832
12833 -- Start of processing for Validate_Array_Type_Instance
12834
12835 begin
12836 if not Is_Array_Type (Act_T) then
12837 Error_Msg_NE
12838 ("expect array type in instantiation of &", Actual, Gen_T);
12839 Abandon_Instantiation (Actual);
12840
12841 elsif Nkind (Def) = N_Constrained_Array_Definition then
12842 if not (Is_Constrained (Act_T)) then
12843 Error_Msg_NE
12844 ("expect constrained array in instantiation of &",
12845 Actual, Gen_T);
12846 Abandon_Instantiation (Actual);
12847 end if;
12848
12849 else
12850 if Is_Constrained (Act_T) then
12851 Error_Msg_NE
12852 ("expect unconstrained array in instantiation of &",
12853 Actual, Gen_T);
12854 Abandon_Instantiation (Actual);
12855 end if;
12856 end if;
12857
12858 if Formal_Dimensions /= Number_Dimensions (Act_T) then
12859 Error_Msg_NE
12860 ("dimensions of actual do not match formal &", Actual, Gen_T);
12861 Abandon_Instantiation (Actual);
12862 end if;
12863
12864 I1 := First_Index (A_Gen_T);
12865 I2 := First_Index (Act_T);
12866 for J in 1 .. Formal_Dimensions loop
12867
12868 -- If the indexes of the actual were given by a subtype_mark,
12869 -- the index was transformed into a range attribute. Retrieve
12870 -- the original type mark for checking.
12871
12872 if Is_Entity_Name (Original_Node (I2)) then
12873 T2 := Entity (Original_Node (I2));
12874 else
12875 T2 := Etype (I2);
12876 end if;
12877
12878 if not Subtypes_Match
12879 (Find_Actual_Type (Etype (I1), A_Gen_T), T2)
12880 then
12881 Error_Msg_NE
12882 ("index types of actual do not match those of formal &",
12883 Actual, Gen_T);
12884 Abandon_Instantiation (Actual);
12885 end if;
12886
12887 Next_Index (I1);
12888 Next_Index (I2);
12889 end loop;
12890
12891 -- Check matching subtypes. Note that there are complex visibility
12892 -- issues when the generic is a child unit and some aspect of the
12893 -- generic type is declared in a parent unit of the generic. We do
12894 -- the test to handle this special case only after a direct check
12895 -- for static matching has failed. The case where both the component
12896 -- type and the array type are separate formals, and the component
12897 -- type is a private view may also require special checking in
12898 -- Subtypes_Match. Finally, we assume that a child instance where
12899 -- the component type comes from a formal of a parent instance is
12900 -- correct because the generic was correct. A more precise check
12901 -- seems too complex to install???
12902
12903 if Subtypes_Match
12904 (Component_Type (A_Gen_T), Component_Type (Act_T))
12905 or else
12906 Subtypes_Match
12907 (Find_Actual_Type (Component_Type (A_Gen_T), A_Gen_T),
12908 Component_Type (Act_T))
12909 or else
12910 (not Inside_A_Generic
12911 and then Is_Child_Unit (Scope (Component_Type (A_Gen_T))))
12912 then
12913 null;
12914 else
12915 Error_Msg_NE
12916 ("component subtype of actual does not match that of formal &",
12917 Actual, Gen_T);
12918 Abandon_Instantiation (Actual);
12919 end if;
12920
12921 if Has_Aliased_Components (A_Gen_T)
12922 and then not Has_Aliased_Components (Act_T)
12923 then
12924 Error_Msg_NE
12925 ("actual must have aliased components to match formal type &",
12926 Actual, Gen_T);
12927 end if;
12928 end Validate_Array_Type_Instance;
12929
12930 -----------------------------------------------
12931 -- Validate_Derived_Interface_Type_Instance --
12932 -----------------------------------------------
12933
12934 procedure Validate_Derived_Interface_Type_Instance is
12935 Par : constant Entity_Id := Entity (Subtype_Indication (Def));
12936 Elmt : Elmt_Id;
12937
12938 begin
12939 -- First apply interface instance checks
12940
12941 Validate_Interface_Type_Instance;
12942
12943 -- Verify that immediate parent interface is an ancestor of
12944 -- the actual.
12945
12946 if Present (Par)
12947 and then not Interface_Present_In_Ancestor (Act_T, Par)
12948 then
12949 Error_Msg_NE
12950 ("interface actual must include progenitor&", Actual, Par);
12951 end if;
12952
12953 -- Now verify that the actual includes all other ancestors of
12954 -- the formal.
12955
12956 Elmt := First_Elmt (Interfaces (A_Gen_T));
12957 while Present (Elmt) loop
12958 if not Interface_Present_In_Ancestor
12959 (Act_T, Get_Instance_Of (Node (Elmt)))
12960 then
12961 Error_Msg_NE
12962 ("interface actual must include progenitor&",
12963 Actual, Node (Elmt));
12964 end if;
12965
12966 Next_Elmt (Elmt);
12967 end loop;
12968 end Validate_Derived_Interface_Type_Instance;
12969
12970 ------------------------------------
12971 -- Validate_Derived_Type_Instance --
12972 ------------------------------------
12973
12974 procedure Validate_Derived_Type_Instance is
12975 Actual_Discr : Entity_Id;
12976 Ancestor_Discr : Entity_Id;
12977
12978 begin
12979 -- Verify that the actual includes the progenitors of the formal,
12980 -- if any. The formal may depend on previous formals and their
12981 -- instance, so we must examine instance of interfaces if present.
12982 -- The actual may be an extension of an interface, in which case
12983 -- it does not appear in the interface list, so this must be
12984 -- checked separately.
12985
12986 if Present (Interface_List (Def)) then
12987 if not Has_Interfaces (Act_T) then
12988 Error_Msg_NE
12989 ("actual must implement all interfaces of formal&",
12990 Actual, A_Gen_T);
12991
12992 else
12993 declare
12994 Act_Iface_List : Elist_Id;
12995 Iface : Node_Id;
12996 Iface_Ent : Entity_Id;
12997
12998 function Instance_Exists (I : Entity_Id) return Boolean;
12999 -- If the interface entity is declared in a generic unit,
13000 -- this can only be legal if we are within an instantiation
13001 -- of a child of that generic. There is currently no
13002 -- mechanism to relate an interface declared within a
13003 -- generic to the corresponding interface in an instance,
13004 -- so we traverse the list of interfaces of the actual,
13005 -- looking for a name match.
13006
13007 ---------------------
13008 -- Instance_Exists --
13009 ---------------------
13010
13011 function Instance_Exists (I : Entity_Id) return Boolean is
13012 Iface_Elmt : Elmt_Id;
13013
13014 begin
13015 Iface_Elmt := First_Elmt (Act_Iface_List);
13016 while Present (Iface_Elmt) loop
13017 if Is_Generic_Instance (Scope (Node (Iface_Elmt)))
13018 and then Chars (Node (Iface_Elmt)) = Chars (I)
13019 then
13020 return True;
13021 end if;
13022
13023 Next_Elmt (Iface_Elmt);
13024 end loop;
13025
13026 return False;
13027 end Instance_Exists;
13028
13029 begin
13030 Iface := First (Abstract_Interface_List (A_Gen_T));
13031 Collect_Interfaces (Act_T, Act_Iface_List);
13032
13033 while Present (Iface) loop
13034 Iface_Ent := Get_Instance_Of (Entity (Iface));
13035
13036 if Is_Ancestor (Iface_Ent, Act_T)
13037 or else Is_Progenitor (Iface_Ent, Act_T)
13038 then
13039 null;
13040
13041 elsif Ekind (Scope (Iface_Ent)) = E_Generic_Package
13042 and then Instance_Exists (Iface_Ent)
13043 then
13044 null;
13045
13046 else
13047 Error_Msg_Name_1 := Chars (Act_T);
13048 Error_Msg_NE
13049 ("Actual% must implement interface&",
13050 Actual, Etype (Iface));
13051 end if;
13052
13053 Next (Iface);
13054 end loop;
13055 end;
13056 end if;
13057 end if;
13058
13059 -- If the parent type in the generic declaration is itself a previous
13060 -- formal type, then it is local to the generic and absent from the
13061 -- analyzed generic definition. In that case the ancestor is the
13062 -- instance of the formal (which must have been instantiated
13063 -- previously), unless the ancestor is itself a formal derived type.
13064 -- In this latter case (which is the subject of Corrigendum 8652/0038
13065 -- (AI-202) the ancestor of the formals is the ancestor of its
13066 -- parent. Otherwise, the analyzed generic carries the parent type.
13067 -- If the parent type is defined in a previous formal package, then
13068 -- the scope of that formal package is that of the generic type
13069 -- itself, and it has already been mapped into the corresponding type
13070 -- in the actual package.
13071
13072 -- Common case: parent type defined outside of the generic
13073
13074 if Is_Entity_Name (Subtype_Mark (Def))
13075 and then Present (Entity (Subtype_Mark (Def)))
13076 then
13077 Ancestor := Get_Instance_Of (Entity (Subtype_Mark (Def)));
13078
13079 -- Check whether parent is defined in a previous formal package
13080
13081 elsif
13082 Scope (Scope (Base_Type (Etype (A_Gen_T)))) = Scope (A_Gen_T)
13083 then
13084 Ancestor :=
13085 Get_Instance_Of (Base_Type (Etype (A_Gen_T)));
13086
13087 -- The type may be a local derivation, or a type extension of a
13088 -- previous formal, or of a formal of a parent package.
13089
13090 elsif Is_Derived_Type (Get_Instance_Of (A_Gen_T))
13091 or else
13092 Ekind (Get_Instance_Of (A_Gen_T)) = E_Record_Type_With_Private
13093 then
13094 -- Check whether the parent is another derived formal type in the
13095 -- same generic unit.
13096
13097 if Etype (A_Gen_T) /= A_Gen_T
13098 and then Is_Generic_Type (Etype (A_Gen_T))
13099 and then Scope (Etype (A_Gen_T)) = Scope (A_Gen_T)
13100 and then Etype (Etype (A_Gen_T)) /= Etype (A_Gen_T)
13101 then
13102 -- Locate ancestor of parent from the subtype declaration
13103 -- created for the actual.
13104
13105 declare
13106 Decl : Node_Id;
13107
13108 begin
13109 Decl := First (Actual_Decls);
13110 while Present (Decl) loop
13111 if Nkind (Decl) = N_Subtype_Declaration
13112 and then Chars (Defining_Identifier (Decl)) =
13113 Chars (Etype (A_Gen_T))
13114 then
13115 Ancestor := Generic_Parent_Type (Decl);
13116 exit;
13117 else
13118 Next (Decl);
13119 end if;
13120 end loop;
13121 end;
13122
13123 pragma Assert (Present (Ancestor));
13124
13125 -- The ancestor itself may be a previous formal that has been
13126 -- instantiated.
13127
13128 Ancestor := Get_Instance_Of (Ancestor);
13129
13130 else
13131 Ancestor :=
13132 Get_Instance_Of (Base_Type (Get_Instance_Of (A_Gen_T)));
13133 end if;
13134
13135 -- Check whether parent is a previous formal of the current generic
13136
13137 elsif Is_Derived_Type (A_Gen_T)
13138 and then Is_Generic_Type (Etype (A_Gen_T))
13139 and then Scope (A_Gen_T) = Scope (Etype (A_Gen_T))
13140 then
13141 Ancestor := Get_Instance_Of (First_Subtype (Etype (A_Gen_T)));
13142
13143 -- An unusual case: the actual is a type declared in a parent unit,
13144 -- but is not a formal type so there is no instance_of for it.
13145 -- Retrieve it by analyzing the record extension.
13146
13147 elsif Is_Child_Unit (Scope (A_Gen_T))
13148 and then In_Open_Scopes (Scope (Act_T))
13149 and then Is_Generic_Instance (Scope (Act_T))
13150 then
13151 Analyze (Subtype_Mark (Def));
13152 Ancestor := Entity (Subtype_Mark (Def));
13153
13154 else
13155 Ancestor := Get_Instance_Of (Etype (Base_Type (A_Gen_T)));
13156 end if;
13157
13158 -- If the formal derived type has pragma Preelaborable_Initialization
13159 -- then the actual type must have preelaborable initialization.
13160
13161 if Known_To_Have_Preelab_Init (A_Gen_T)
13162 and then not Has_Preelaborable_Initialization (Act_T)
13163 then
13164 Error_Msg_NE
13165 ("actual for & must have preelaborable initialization",
13166 Actual, Gen_T);
13167 end if;
13168
13169 -- Ada 2005 (AI-251)
13170
13171 if Ada_Version >= Ada_2005 and then Is_Interface (Ancestor) then
13172 if not Interface_Present_In_Ancestor (Act_T, Ancestor) then
13173 Error_Msg_NE
13174 ("(Ada 2005) expected type implementing & in instantiation",
13175 Actual, Ancestor);
13176 end if;
13177
13178 -- Finally verify that the (instance of) the ancestor is an ancestor
13179 -- of the actual.
13180
13181 elsif not Is_Ancestor (Base_Type (Ancestor), Act_T) then
13182 Error_Msg_NE
13183 ("expect type derived from & in instantiation",
13184 Actual, First_Subtype (Ancestor));
13185 Abandon_Instantiation (Actual);
13186 end if;
13187
13188 -- Ada 2005 (AI-443): Synchronized formal derived type checks. Note
13189 -- that the formal type declaration has been rewritten as a private
13190 -- extension.
13191
13192 if Ada_Version >= Ada_2005
13193 and then Nkind (Parent (A_Gen_T)) = N_Private_Extension_Declaration
13194 and then Synchronized_Present (Parent (A_Gen_T))
13195 then
13196 -- The actual must be a synchronized tagged type
13197
13198 if not Is_Tagged_Type (Act_T) then
13199 Error_Msg_N
13200 ("actual of synchronized type must be tagged", Actual);
13201 Abandon_Instantiation (Actual);
13202
13203 elsif Nkind (Parent (Act_T)) = N_Full_Type_Declaration
13204 and then Nkind (Type_Definition (Parent (Act_T))) =
13205 N_Derived_Type_Definition
13206 and then not Synchronized_Present
13207 (Type_Definition (Parent (Act_T)))
13208 then
13209 Error_Msg_N
13210 ("actual of synchronized type must be synchronized", Actual);
13211 Abandon_Instantiation (Actual);
13212 end if;
13213 end if;
13214
13215 -- Perform atomic/volatile checks (RM C.6(12)). Note that AI05-0218-1
13216 -- removes the second instance of the phrase "or allow pass by copy".
13217
13218 -- For Ada 2020, the aspect may be specified explicitly for the
13219 -- formal regardless of whether an ancestor obeys it.
13220
13221 if Is_Atomic (Act_T)
13222 and then not Is_Atomic (Ancestor)
13223 and then not Is_Atomic (A_Gen_T)
13224 then
13225 Error_Msg_N
13226 ("cannot have atomic actual type for non-atomic formal type",
13227 Actual);
13228
13229 elsif Is_Volatile (Act_T)
13230 and then not Is_Volatile (Ancestor)
13231 and then not Is_Volatile (A_Gen_T)
13232 then
13233 Error_Msg_N
13234 ("cannot have volatile actual type for non-volatile formal type",
13235 Actual);
13236 end if;
13237
13238 -- It should not be necessary to check for unknown discriminants on
13239 -- Formal, but for some reason Has_Unknown_Discriminants is false for
13240 -- A_Gen_T, so Is_Definite_Subtype incorrectly returns True. This
13241 -- needs fixing. ???
13242
13243 if Is_Definite_Subtype (A_Gen_T)
13244 and then not Unknown_Discriminants_Present (Formal)
13245 and then not Is_Definite_Subtype (Act_T)
13246 then
13247 Error_Msg_N ("actual subtype must be constrained", Actual);
13248 Abandon_Instantiation (Actual);
13249 end if;
13250
13251 if not Unknown_Discriminants_Present (Formal) then
13252 if Is_Constrained (Ancestor) then
13253 if not Is_Constrained (Act_T) then
13254 Error_Msg_N ("actual subtype must be constrained", Actual);
13255 Abandon_Instantiation (Actual);
13256 end if;
13257
13258 -- Ancestor is unconstrained, Check if generic formal and actual
13259 -- agree on constrainedness. The check only applies to array types
13260 -- and discriminated types.
13261
13262 elsif Is_Constrained (Act_T) then
13263 if Ekind (Ancestor) = E_Access_Type
13264 or else (not Is_Constrained (A_Gen_T)
13265 and then Is_Composite_Type (A_Gen_T))
13266 then
13267 Error_Msg_N ("actual subtype must be unconstrained", Actual);
13268 Abandon_Instantiation (Actual);
13269 end if;
13270
13271 -- A class-wide type is only allowed if the formal has unknown
13272 -- discriminants.
13273
13274 elsif Is_Class_Wide_Type (Act_T)
13275 and then not Has_Unknown_Discriminants (Ancestor)
13276 then
13277 Error_Msg_NE
13278 ("actual for & cannot be a class-wide type", Actual, Gen_T);
13279 Abandon_Instantiation (Actual);
13280
13281 -- Otherwise, the formal and actual must have the same number
13282 -- of discriminants and each discriminant of the actual must
13283 -- correspond to a discriminant of the formal.
13284
13285 elsif Has_Discriminants (Act_T)
13286 and then not Has_Unknown_Discriminants (Act_T)
13287 and then Has_Discriminants (Ancestor)
13288 then
13289 Actual_Discr := First_Discriminant (Act_T);
13290 Ancestor_Discr := First_Discriminant (Ancestor);
13291 while Present (Actual_Discr)
13292 and then Present (Ancestor_Discr)
13293 loop
13294 if Base_Type (Act_T) /= Base_Type (Ancestor) and then
13295 No (Corresponding_Discriminant (Actual_Discr))
13296 then
13297 Error_Msg_NE
13298 ("discriminant & does not correspond "
13299 & "to ancestor discriminant", Actual, Actual_Discr);
13300 Abandon_Instantiation (Actual);
13301 end if;
13302
13303 Next_Discriminant (Actual_Discr);
13304 Next_Discriminant (Ancestor_Discr);
13305 end loop;
13306
13307 if Present (Actual_Discr) or else Present (Ancestor_Discr) then
13308 Error_Msg_NE
13309 ("actual for & must have same number of discriminants",
13310 Actual, Gen_T);
13311 Abandon_Instantiation (Actual);
13312 end if;
13313
13314 -- This case should be caught by the earlier check for
13315 -- constrainedness, but the check here is added for completeness.
13316
13317 elsif Has_Discriminants (Act_T)
13318 and then not Has_Unknown_Discriminants (Act_T)
13319 then
13320 Error_Msg_NE
13321 ("actual for & must not have discriminants", Actual, Gen_T);
13322 Abandon_Instantiation (Actual);
13323
13324 elsif Has_Discriminants (Ancestor) then
13325 Error_Msg_NE
13326 ("actual for & must have known discriminants", Actual, Gen_T);
13327 Abandon_Instantiation (Actual);
13328 end if;
13329
13330 if not Subtypes_Statically_Compatible
13331 (Act_T, Ancestor, Formal_Derived_Matching => True)
13332 then
13333 Error_Msg_NE
13334 ("actual for & must be statically compatible with ancestor",
13335 Actual, Gen_T);
13336
13337 if not Predicates_Compatible (Act_T, Ancestor) then
13338 Error_Msg_N
13339 ("\predicate on actual is not compatible with ancestor",
13340 Actual);
13341 end if;
13342
13343 Abandon_Instantiation (Actual);
13344 end if;
13345 end if;
13346
13347 -- If the formal and actual types are abstract, check that there
13348 -- are no abstract primitives of the actual type that correspond to
13349 -- nonabstract primitives of the formal type (second sentence of
13350 -- RM95 3.9.3(9)).
13351
13352 if Is_Abstract_Type (A_Gen_T) and then Is_Abstract_Type (Act_T) then
13353 Check_Abstract_Primitives : declare
13354 Gen_Prims : constant Elist_Id :=
13355 Primitive_Operations (A_Gen_T);
13356 Gen_Elmt : Elmt_Id;
13357 Gen_Subp : Entity_Id;
13358 Anc_Subp : Entity_Id;
13359 Anc_Formal : Entity_Id;
13360 Anc_F_Type : Entity_Id;
13361
13362 Act_Prims : constant Elist_Id := Primitive_Operations (Act_T);
13363 Act_Elmt : Elmt_Id;
13364 Act_Subp : Entity_Id;
13365 Act_Formal : Entity_Id;
13366 Act_F_Type : Entity_Id;
13367
13368 Subprograms_Correspond : Boolean;
13369
13370 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean;
13371 -- Returns true if T2 is derived directly or indirectly from
13372 -- T1, including derivations from interfaces. T1 and T2 are
13373 -- required to be specific tagged base types.
13374
13375 ------------------------
13376 -- Is_Tagged_Ancestor --
13377 ------------------------
13378
13379 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean
13380 is
13381 Intfc_Elmt : Elmt_Id;
13382
13383 begin
13384 -- The predicate is satisfied if the types are the same
13385
13386 if T1 = T2 then
13387 return True;
13388
13389 -- If we've reached the top of the derivation chain then
13390 -- we know that T1 is not an ancestor of T2.
13391
13392 elsif Etype (T2) = T2 then
13393 return False;
13394
13395 -- Proceed to check T2's immediate parent
13396
13397 elsif Is_Ancestor (T1, Base_Type (Etype (T2))) then
13398 return True;
13399
13400 -- Finally, check to see if T1 is an ancestor of any of T2's
13401 -- progenitors.
13402
13403 else
13404 Intfc_Elmt := First_Elmt (Interfaces (T2));
13405 while Present (Intfc_Elmt) loop
13406 if Is_Ancestor (T1, Node (Intfc_Elmt)) then
13407 return True;
13408 end if;
13409
13410 Next_Elmt (Intfc_Elmt);
13411 end loop;
13412 end if;
13413
13414 return False;
13415 end Is_Tagged_Ancestor;
13416
13417 -- Start of processing for Check_Abstract_Primitives
13418
13419 begin
13420 -- Loop over all of the formal derived type's primitives
13421
13422 Gen_Elmt := First_Elmt (Gen_Prims);
13423 while Present (Gen_Elmt) loop
13424 Gen_Subp := Node (Gen_Elmt);
13425
13426 -- If the primitive of the formal is not abstract, then
13427 -- determine whether there is a corresponding primitive of
13428 -- the actual type that's abstract.
13429
13430 if not Is_Abstract_Subprogram (Gen_Subp) then
13431 Act_Elmt := First_Elmt (Act_Prims);
13432 while Present (Act_Elmt) loop
13433 Act_Subp := Node (Act_Elmt);
13434
13435 -- If we find an abstract primitive of the actual,
13436 -- then we need to test whether it corresponds to the
13437 -- subprogram from which the generic formal primitive
13438 -- is inherited.
13439
13440 if Is_Abstract_Subprogram (Act_Subp) then
13441 Anc_Subp := Alias (Gen_Subp);
13442
13443 -- Test whether we have a corresponding primitive
13444 -- by comparing names, kinds, formal types, and
13445 -- result types.
13446
13447 if Chars (Anc_Subp) = Chars (Act_Subp)
13448 and then Ekind (Anc_Subp) = Ekind (Act_Subp)
13449 then
13450 Anc_Formal := First_Formal (Anc_Subp);
13451 Act_Formal := First_Formal (Act_Subp);
13452 while Present (Anc_Formal)
13453 and then Present (Act_Formal)
13454 loop
13455 Anc_F_Type := Etype (Anc_Formal);
13456 Act_F_Type := Etype (Act_Formal);
13457
13458 if Ekind (Anc_F_Type) =
13459 E_Anonymous_Access_Type
13460 then
13461 Anc_F_Type := Designated_Type (Anc_F_Type);
13462
13463 if Ekind (Act_F_Type) =
13464 E_Anonymous_Access_Type
13465 then
13466 Act_F_Type :=
13467 Designated_Type (Act_F_Type);
13468 else
13469 exit;
13470 end if;
13471
13472 elsif
13473 Ekind (Act_F_Type) = E_Anonymous_Access_Type
13474 then
13475 exit;
13476 end if;
13477
13478 Anc_F_Type := Base_Type (Anc_F_Type);
13479 Act_F_Type := Base_Type (Act_F_Type);
13480
13481 -- If the formal is controlling, then the
13482 -- the type of the actual primitive's formal
13483 -- must be derived directly or indirectly
13484 -- from the type of the ancestor primitive's
13485 -- formal.
13486
13487 if Is_Controlling_Formal (Anc_Formal) then
13488 if not Is_Tagged_Ancestor
13489 (Anc_F_Type, Act_F_Type)
13490 then
13491 exit;
13492 end if;
13493
13494 -- Otherwise the types of the formals must
13495 -- be the same.
13496
13497 elsif Anc_F_Type /= Act_F_Type then
13498 exit;
13499 end if;
13500
13501 Next_Entity (Anc_Formal);
13502 Next_Entity (Act_Formal);
13503 end loop;
13504
13505 -- If we traversed through all of the formals
13506 -- then so far the subprograms correspond, so
13507 -- now check that any result types correspond.
13508
13509 if No (Anc_Formal) and then No (Act_Formal) then
13510 Subprograms_Correspond := True;
13511
13512 if Ekind (Act_Subp) = E_Function then
13513 Anc_F_Type := Etype (Anc_Subp);
13514 Act_F_Type := Etype (Act_Subp);
13515
13516 if Ekind (Anc_F_Type) =
13517 E_Anonymous_Access_Type
13518 then
13519 Anc_F_Type :=
13520 Designated_Type (Anc_F_Type);
13521
13522 if Ekind (Act_F_Type) =
13523 E_Anonymous_Access_Type
13524 then
13525 Act_F_Type :=
13526 Designated_Type (Act_F_Type);
13527 else
13528 Subprograms_Correspond := False;
13529 end if;
13530
13531 elsif
13532 Ekind (Act_F_Type)
13533 = E_Anonymous_Access_Type
13534 then
13535 Subprograms_Correspond := False;
13536 end if;
13537
13538 Anc_F_Type := Base_Type (Anc_F_Type);
13539 Act_F_Type := Base_Type (Act_F_Type);
13540
13541 -- Now either the result types must be
13542 -- the same or, if the result type is
13543 -- controlling, the result type of the
13544 -- actual primitive must descend from the
13545 -- result type of the ancestor primitive.
13546
13547 if Subprograms_Correspond
13548 and then Anc_F_Type /= Act_F_Type
13549 and then
13550 Has_Controlling_Result (Anc_Subp)
13551 and then not Is_Tagged_Ancestor
13552 (Anc_F_Type, Act_F_Type)
13553 then
13554 Subprograms_Correspond := False;
13555 end if;
13556 end if;
13557
13558 -- Found a matching subprogram belonging to
13559 -- formal ancestor type, so actual subprogram
13560 -- corresponds and this violates 3.9.3(9).
13561
13562 if Subprograms_Correspond then
13563 Error_Msg_NE
13564 ("abstract subprogram & overrides "
13565 & "nonabstract subprogram of ancestor",
13566 Actual, Act_Subp);
13567 end if;
13568 end if;
13569 end if;
13570 end if;
13571
13572 Next_Elmt (Act_Elmt);
13573 end loop;
13574 end if;
13575
13576 Next_Elmt (Gen_Elmt);
13577 end loop;
13578 end Check_Abstract_Primitives;
13579 end if;
13580
13581 -- Verify that limitedness matches. If parent is a limited
13582 -- interface then the generic formal is not unless declared
13583 -- explicitly so. If not declared limited, the actual cannot be
13584 -- limited (see AI05-0087).
13585
13586 if Is_Limited_Type (Act_T) and then not Is_Limited_Type (A_Gen_T) then
13587 if not In_Instance then
13588 Error_Msg_NE
13589 ("actual for non-limited & cannot be a limited type",
13590 Actual, Gen_T);
13591 Explain_Limited_Type (Act_T, Actual);
13592 Abandon_Instantiation (Actual);
13593 end if;
13594 end if;
13595
13596 -- Check for AI12-0036
13597
13598 declare
13599 Formal_Is_Private_Extension : constant Boolean :=
13600 Nkind (Parent (A_Gen_T)) = N_Private_Extension_Declaration;
13601
13602 Actual_Is_Tagged : constant Boolean := Is_Tagged_Type (Act_T);
13603
13604 begin
13605 if Actual_Is_Tagged /= Formal_Is_Private_Extension then
13606 if not In_Instance then
13607 if Actual_Is_Tagged then
13608 Error_Msg_NE
13609 ("actual for & cannot be a tagged type", Actual, Gen_T);
13610 else
13611 Error_Msg_NE
13612 ("actual for & must be a tagged type", Actual, Gen_T);
13613 end if;
13614
13615 Abandon_Instantiation (Actual);
13616 end if;
13617 end if;
13618 end;
13619 end Validate_Derived_Type_Instance;
13620
13621 ----------------------------------------
13622 -- Validate_Discriminated_Formal_Type --
13623 ----------------------------------------
13624
13625 procedure Validate_Discriminated_Formal_Type is
13626 Formal_Discr : Entity_Id;
13627 Actual_Discr : Entity_Id;
13628 Formal_Subt : Entity_Id;
13629
13630 begin
13631 if Has_Discriminants (A_Gen_T) then
13632 if not Has_Discriminants (Act_T) then
13633 Error_Msg_NE
13634 ("actual for & must have discriminants", Actual, Gen_T);
13635 Abandon_Instantiation (Actual);
13636
13637 elsif Is_Constrained (Act_T) then
13638 Error_Msg_NE
13639 ("actual for & must be unconstrained", Actual, Gen_T);
13640 Abandon_Instantiation (Actual);
13641
13642 else
13643 Formal_Discr := First_Discriminant (A_Gen_T);
13644 Actual_Discr := First_Discriminant (Act_T);
13645 while Formal_Discr /= Empty loop
13646 if Actual_Discr = Empty then
13647 Error_Msg_NE
13648 ("discriminants on actual do not match formal",
13649 Actual, Gen_T);
13650 Abandon_Instantiation (Actual);
13651 end if;
13652
13653 Formal_Subt := Get_Instance_Of (Etype (Formal_Discr));
13654
13655 -- Access discriminants match if designated types do
13656
13657 if Ekind (Base_Type (Formal_Subt)) = E_Anonymous_Access_Type
13658 and then (Ekind (Base_Type (Etype (Actual_Discr)))) =
13659 E_Anonymous_Access_Type
13660 and then
13661 Get_Instance_Of
13662 (Designated_Type (Base_Type (Formal_Subt))) =
13663 Designated_Type (Base_Type (Etype (Actual_Discr)))
13664 then
13665 null;
13666
13667 elsif Base_Type (Formal_Subt) /=
13668 Base_Type (Etype (Actual_Discr))
13669 then
13670 Error_Msg_NE
13671 ("types of actual discriminants must match formal",
13672 Actual, Gen_T);
13673 Abandon_Instantiation (Actual);
13674
13675 elsif not Subtypes_Statically_Match
13676 (Formal_Subt, Etype (Actual_Discr))
13677 and then Ada_Version >= Ada_95
13678 then
13679 Error_Msg_NE
13680 ("subtypes of actual discriminants must match formal",
13681 Actual, Gen_T);
13682 Abandon_Instantiation (Actual);
13683 end if;
13684
13685 Next_Discriminant (Formal_Discr);
13686 Next_Discriminant (Actual_Discr);
13687 end loop;
13688
13689 if Actual_Discr /= Empty then
13690 Error_Msg_NE
13691 ("discriminants on actual do not match formal",
13692 Actual, Gen_T);
13693 Abandon_Instantiation (Actual);
13694 end if;
13695 end if;
13696 end if;
13697 end Validate_Discriminated_Formal_Type;
13698
13699 ---------------------------------------
13700 -- Validate_Incomplete_Type_Instance --
13701 ---------------------------------------
13702
13703 procedure Validate_Incomplete_Type_Instance is
13704 begin
13705 if not Is_Tagged_Type (Act_T)
13706 and then Is_Tagged_Type (A_Gen_T)
13707 then
13708 Error_Msg_NE
13709 ("actual for & must be a tagged type", Actual, Gen_T);
13710 end if;
13711
13712 Validate_Discriminated_Formal_Type;
13713 end Validate_Incomplete_Type_Instance;
13714
13715 --------------------------------------
13716 -- Validate_Interface_Type_Instance --
13717 --------------------------------------
13718
13719 procedure Validate_Interface_Type_Instance is
13720 begin
13721 if not Is_Interface (Act_T) then
13722 Error_Msg_NE
13723 ("actual for formal interface type must be an interface",
13724 Actual, Gen_T);
13725
13726 elsif Is_Limited_Type (Act_T) /= Is_Limited_Type (A_Gen_T)
13727 or else Is_Task_Interface (A_Gen_T) /= Is_Task_Interface (Act_T)
13728 or else Is_Protected_Interface (A_Gen_T) /=
13729 Is_Protected_Interface (Act_T)
13730 or else Is_Synchronized_Interface (A_Gen_T) /=
13731 Is_Synchronized_Interface (Act_T)
13732 then
13733 Error_Msg_NE
13734 ("actual for interface& does not match (RM 12.5.5(4))",
13735 Actual, Gen_T);
13736 end if;
13737 end Validate_Interface_Type_Instance;
13738
13739 ------------------------------------
13740 -- Validate_Private_Type_Instance --
13741 ------------------------------------
13742
13743 procedure Validate_Private_Type_Instance is
13744 begin
13745 if Is_Limited_Type (Act_T)
13746 and then not Is_Limited_Type (A_Gen_T)
13747 then
13748 if In_Instance then
13749 null;
13750 else
13751 Error_Msg_NE
13752 ("actual for non-limited & cannot be a limited type", Actual,
13753 Gen_T);
13754 Explain_Limited_Type (Act_T, Actual);
13755 Abandon_Instantiation (Actual);
13756 end if;
13757
13758 elsif Known_To_Have_Preelab_Init (A_Gen_T)
13759 and then not Has_Preelaborable_Initialization (Act_T)
13760 then
13761 Error_Msg_NE
13762 ("actual for & must have preelaborable initialization", Actual,
13763 Gen_T);
13764
13765 elsif not Is_Definite_Subtype (Act_T)
13766 and then Is_Definite_Subtype (A_Gen_T)
13767 and then Ada_Version >= Ada_95
13768 then
13769 Error_Msg_NE
13770 ("actual for & must be a definite subtype", Actual, Gen_T);
13771
13772 elsif not Is_Tagged_Type (Act_T)
13773 and then Is_Tagged_Type (A_Gen_T)
13774 then
13775 Error_Msg_NE
13776 ("actual for & must be a tagged type", Actual, Gen_T);
13777 end if;
13778
13779 Validate_Discriminated_Formal_Type;
13780 Ancestor := Gen_T;
13781 end Validate_Private_Type_Instance;
13782
13783 -- Start of processing for Instantiate_Type
13784
13785 begin
13786 if Get_Instance_Of (A_Gen_T) /= A_Gen_T then
13787 Error_Msg_N ("duplicate instantiation of generic type", Actual);
13788 return New_List (Error);
13789
13790 elsif not Is_Entity_Name (Actual)
13791 or else not Is_Type (Entity (Actual))
13792 then
13793 Error_Msg_NE
13794 ("expect valid subtype mark to instantiate &", Actual, Gen_T);
13795 Abandon_Instantiation (Actual);
13796
13797 else
13798 Act_T := Entity (Actual);
13799
13800 -- Ada 2005 (AI-216): An Unchecked_Union subtype shall only be passed
13801 -- as a generic actual parameter if the corresponding formal type
13802 -- does not have a known_discriminant_part, or is a formal derived
13803 -- type that is an Unchecked_Union type.
13804
13805 if Is_Unchecked_Union (Base_Type (Act_T)) then
13806 if not Has_Discriminants (A_Gen_T)
13807 or else (Is_Derived_Type (A_Gen_T)
13808 and then Is_Unchecked_Union (A_Gen_T))
13809 then
13810 null;
13811 else
13812 Error_Msg_N ("unchecked union cannot be the actual for a "
13813 & "discriminated formal type", Act_T);
13814
13815 end if;
13816 end if;
13817
13818 -- Deal with fixed/floating restrictions
13819
13820 if Is_Floating_Point_Type (Act_T) then
13821 Check_Restriction (No_Floating_Point, Actual);
13822 elsif Is_Fixed_Point_Type (Act_T) then
13823 Check_Restriction (No_Fixed_Point, Actual);
13824 end if;
13825
13826 -- Deal with error of using incomplete type as generic actual.
13827 -- This includes limited views of a type, even if the non-limited
13828 -- view may be available.
13829
13830 if Ekind (Act_T) = E_Incomplete_Type
13831 or else (Is_Class_Wide_Type (Act_T)
13832 and then Ekind (Root_Type (Act_T)) = E_Incomplete_Type)
13833 then
13834 -- If the formal is an incomplete type, the actual can be
13835 -- incomplete as well.
13836
13837 if Ekind (A_Gen_T) = E_Incomplete_Type then
13838 null;
13839
13840 elsif Is_Class_Wide_Type (Act_T)
13841 or else No (Full_View (Act_T))
13842 then
13843 Error_Msg_N ("premature use of incomplete type", Actual);
13844 Abandon_Instantiation (Actual);
13845 else
13846 Act_T := Full_View (Act_T);
13847 Set_Entity (Actual, Act_T);
13848
13849 if Has_Private_Component (Act_T) then
13850 Error_Msg_N
13851 ("premature use of type with private component", Actual);
13852 end if;
13853 end if;
13854
13855 -- Deal with error of premature use of private type as generic actual
13856
13857 elsif Is_Private_Type (Act_T)
13858 and then Is_Private_Type (Base_Type (Act_T))
13859 and then not Is_Generic_Type (Act_T)
13860 and then not Is_Derived_Type (Act_T)
13861 and then No (Full_View (Root_Type (Act_T)))
13862 then
13863 -- If the formal is an incomplete type, the actual can be
13864 -- private or incomplete as well.
13865
13866 if Ekind (A_Gen_T) = E_Incomplete_Type then
13867 null;
13868 else
13869 Error_Msg_N ("premature use of private type", Actual);
13870 end if;
13871
13872 elsif Has_Private_Component (Act_T) then
13873 Error_Msg_N
13874 ("premature use of type with private component", Actual);
13875 end if;
13876
13877 Set_Instance_Of (A_Gen_T, Act_T);
13878
13879 -- If the type is generic, the class-wide type may also be used
13880
13881 if Is_Tagged_Type (A_Gen_T)
13882 and then Is_Tagged_Type (Act_T)
13883 and then not Is_Class_Wide_Type (A_Gen_T)
13884 then
13885 Set_Instance_Of (Class_Wide_Type (A_Gen_T),
13886 Class_Wide_Type (Act_T));
13887 end if;
13888
13889 if not Is_Abstract_Type (A_Gen_T)
13890 and then Is_Abstract_Type (Act_T)
13891 then
13892 Error_Msg_N
13893 ("actual of non-abstract formal cannot be abstract", Actual);
13894 end if;
13895
13896 -- A generic scalar type is a first subtype for which we generate
13897 -- an anonymous base type. Indicate that the instance of this base
13898 -- is the base type of the actual.
13899
13900 if Is_Scalar_Type (A_Gen_T) then
13901 Set_Instance_Of (Etype (A_Gen_T), Etype (Act_T));
13902 end if;
13903 end if;
13904
13905 Check_Shared_Variable_Control_Aspects;
13906
13907 if Error_Posted (Act_T) then
13908 null;
13909 else
13910 case Nkind (Def) is
13911 when N_Formal_Private_Type_Definition =>
13912 Validate_Private_Type_Instance;
13913
13914 when N_Formal_Incomplete_Type_Definition =>
13915 Validate_Incomplete_Type_Instance;
13916
13917 when N_Formal_Derived_Type_Definition =>
13918 Validate_Derived_Type_Instance;
13919
13920 when N_Formal_Discrete_Type_Definition =>
13921 if not Is_Discrete_Type (Act_T) then
13922 Error_Msg_NE
13923 ("expect discrete type in instantiation of&",
13924 Actual, Gen_T);
13925 Abandon_Instantiation (Actual);
13926 end if;
13927
13928 Diagnose_Predicated_Actual;
13929
13930 when N_Formal_Signed_Integer_Type_Definition =>
13931 if not Is_Signed_Integer_Type (Act_T) then
13932 Error_Msg_NE
13933 ("expect signed integer type in instantiation of&",
13934 Actual, Gen_T);
13935 Abandon_Instantiation (Actual);
13936 end if;
13937
13938 Diagnose_Predicated_Actual;
13939
13940 when N_Formal_Modular_Type_Definition =>
13941 if not Is_Modular_Integer_Type (Act_T) then
13942 Error_Msg_NE
13943 ("expect modular type in instantiation of &",
13944 Actual, Gen_T);
13945 Abandon_Instantiation (Actual);
13946 end if;
13947
13948 Diagnose_Predicated_Actual;
13949
13950 when N_Formal_Floating_Point_Definition =>
13951 if not Is_Floating_Point_Type (Act_T) then
13952 Error_Msg_NE
13953 ("expect float type in instantiation of &", Actual, Gen_T);
13954 Abandon_Instantiation (Actual);
13955 end if;
13956
13957 when N_Formal_Ordinary_Fixed_Point_Definition =>
13958 if not Is_Ordinary_Fixed_Point_Type (Act_T) then
13959 Error_Msg_NE
13960 ("expect ordinary fixed point type in instantiation of &",
13961 Actual, Gen_T);
13962 Abandon_Instantiation (Actual);
13963 end if;
13964
13965 when N_Formal_Decimal_Fixed_Point_Definition =>
13966 if not Is_Decimal_Fixed_Point_Type (Act_T) then
13967 Error_Msg_NE
13968 ("expect decimal type in instantiation of &",
13969 Actual, Gen_T);
13970 Abandon_Instantiation (Actual);
13971 end if;
13972
13973 when N_Array_Type_Definition =>
13974 Validate_Array_Type_Instance;
13975
13976 when N_Access_To_Object_Definition =>
13977 Validate_Access_Type_Instance;
13978
13979 when N_Access_Function_Definition
13980 | N_Access_Procedure_Definition
13981 =>
13982 Validate_Access_Subprogram_Instance;
13983
13984 when N_Record_Definition =>
13985 Validate_Interface_Type_Instance;
13986
13987 when N_Derived_Type_Definition =>
13988 Validate_Derived_Interface_Type_Instance;
13989
13990 when others =>
13991 raise Program_Error;
13992 end case;
13993 end if;
13994
13995 Subt := New_Copy (Gen_T);
13996
13997 -- Use adjusted sloc of subtype name as the location for other nodes in
13998 -- the subtype declaration.
13999
14000 Loc := Sloc (Subt);
14001
14002 Decl_Node :=
14003 Make_Subtype_Declaration (Loc,
14004 Defining_Identifier => Subt,
14005 Subtype_Indication => New_Occurrence_Of (Act_T, Loc));
14006
14007 -- Record whether the actual is private at this point, so that
14008 -- Check_Generic_Actuals can restore its proper view before the
14009 -- semantic analysis of the instance.
14010
14011 if Is_Private_Type (Act_T) then
14012 Set_Has_Private_View (Subtype_Indication (Decl_Node));
14013 end if;
14014
14015 -- In Ada 2012 the actual may be a limited view. Indicate that
14016 -- the local subtype must be treated as such.
14017
14018 if From_Limited_With (Act_T) then
14019 Set_Ekind (Subt, E_Incomplete_Subtype);
14020 Set_From_Limited_With (Subt);
14021 end if;
14022
14023 Decl_Nodes := New_List (Decl_Node);
14024
14025 -- Flag actual derived types so their elaboration produces the
14026 -- appropriate renamings for the primitive operations of the ancestor.
14027 -- Flag actual for formal private types as well, to determine whether
14028 -- operations in the private part may override inherited operations.
14029 -- If the formal has an interface list, the ancestor is not the
14030 -- parent, but the analyzed formal that includes the interface
14031 -- operations of all its progenitors.
14032
14033 -- Same treatment for formal private types, so we can check whether the
14034 -- type is tagged limited when validating derivations in the private
14035 -- part. (See AI05-096).
14036
14037 if Nkind (Def) = N_Formal_Derived_Type_Definition then
14038 if Present (Interface_List (Def)) then
14039 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
14040 else
14041 Set_Generic_Parent_Type (Decl_Node, Ancestor);
14042 end if;
14043
14044 elsif Nkind_In (Def, N_Formal_Private_Type_Definition,
14045 N_Formal_Incomplete_Type_Definition)
14046 then
14047 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
14048 end if;
14049
14050 -- If the actual is a synchronized type that implements an interface,
14051 -- the primitive operations are attached to the corresponding record,
14052 -- and we have to treat it as an additional generic actual, so that its
14053 -- primitive operations become visible in the instance. The task or
14054 -- protected type itself does not carry primitive operations.
14055
14056 if Is_Concurrent_Type (Act_T)
14057 and then Is_Tagged_Type (Act_T)
14058 and then Present (Corresponding_Record_Type (Act_T))
14059 and then Present (Ancestor)
14060 and then Is_Interface (Ancestor)
14061 then
14062 declare
14063 Corr_Rec : constant Entity_Id :=
14064 Corresponding_Record_Type (Act_T);
14065 New_Corr : Entity_Id;
14066 Corr_Decl : Node_Id;
14067
14068 begin
14069 New_Corr := Make_Temporary (Loc, 'S');
14070 Corr_Decl :=
14071 Make_Subtype_Declaration (Loc,
14072 Defining_Identifier => New_Corr,
14073 Subtype_Indication =>
14074 New_Occurrence_Of (Corr_Rec, Loc));
14075 Append_To (Decl_Nodes, Corr_Decl);
14076
14077 if Ekind (Act_T) = E_Task_Type then
14078 Set_Ekind (Subt, E_Task_Subtype);
14079 else
14080 Set_Ekind (Subt, E_Protected_Subtype);
14081 end if;
14082
14083 Set_Corresponding_Record_Type (Subt, Corr_Rec);
14084 Set_Generic_Parent_Type (Corr_Decl, Ancestor);
14085 Set_Generic_Parent_Type (Decl_Node, Empty);
14086 end;
14087 end if;
14088
14089 -- For a floating-point type, capture dimension info if any, because
14090 -- the generated subtype declaration does not come from source and
14091 -- will not process dimensions.
14092
14093 if Is_Floating_Point_Type (Act_T) then
14094 Copy_Dimensions (Act_T, Subt);
14095 end if;
14096
14097 return Decl_Nodes;
14098 end Instantiate_Type;
14099
14100 ---------------------
14101 -- Is_In_Main_Unit --
14102 ---------------------
14103
14104 function Is_In_Main_Unit (N : Node_Id) return Boolean is
14105 Unum : constant Unit_Number_Type := Get_Source_Unit (N);
14106 Current_Unit : Node_Id;
14107
14108 begin
14109 if Unum = Main_Unit then
14110 return True;
14111
14112 -- If the current unit is a subunit then it is either the main unit or
14113 -- is being compiled as part of the main unit.
14114
14115 elsif Nkind (N) = N_Compilation_Unit then
14116 return Nkind (Unit (N)) = N_Subunit;
14117 end if;
14118
14119 Current_Unit := Parent (N);
14120 while Present (Current_Unit)
14121 and then Nkind (Current_Unit) /= N_Compilation_Unit
14122 loop
14123 Current_Unit := Parent (Current_Unit);
14124 end loop;
14125
14126 -- The instantiation node is in the main unit, or else the current node
14127 -- (perhaps as the result of nested instantiations) is in the main unit,
14128 -- or in the declaration of the main unit, which in this last case must
14129 -- be a body.
14130
14131 return
14132 Current_Unit = Cunit (Main_Unit)
14133 or else Current_Unit = Library_Unit (Cunit (Main_Unit))
14134 or else (Present (Current_Unit)
14135 and then Present (Library_Unit (Current_Unit))
14136 and then Is_In_Main_Unit (Library_Unit (Current_Unit)));
14137 end Is_In_Main_Unit;
14138
14139 ----------------------------
14140 -- Load_Parent_Of_Generic --
14141 ----------------------------
14142
14143 procedure Load_Parent_Of_Generic
14144 (N : Node_Id;
14145 Spec : Node_Id;
14146 Body_Optional : Boolean := False)
14147 is
14148 Comp_Unit : constant Node_Id := Cunit (Get_Source_Unit (Spec));
14149 Saved_Style_Check : constant Boolean := Style_Check;
14150 Saved_Warnings : constant Warning_Record := Save_Warnings;
14151 True_Parent : Node_Id;
14152 Inst_Node : Node_Id;
14153 OK : Boolean;
14154 Previous_Instances : constant Elist_Id := New_Elmt_List;
14155
14156 procedure Collect_Previous_Instances (Decls : List_Id);
14157 -- Collect all instantiations in the given list of declarations, that
14158 -- precede the generic that we need to load. If the bodies of these
14159 -- instantiations are available, we must analyze them, to ensure that
14160 -- the public symbols generated are the same when the unit is compiled
14161 -- to generate code, and when it is compiled in the context of a unit
14162 -- that needs a particular nested instance. This process is applied to
14163 -- both package and subprogram instances.
14164
14165 --------------------------------
14166 -- Collect_Previous_Instances --
14167 --------------------------------
14168
14169 procedure Collect_Previous_Instances (Decls : List_Id) is
14170 Decl : Node_Id;
14171
14172 begin
14173 Decl := First (Decls);
14174 while Present (Decl) loop
14175 if Sloc (Decl) >= Sloc (Inst_Node) then
14176 return;
14177
14178 -- If Decl is an instantiation, then record it as requiring
14179 -- instantiation of the corresponding body, except if it is an
14180 -- abbreviated instantiation generated internally for conformance
14181 -- checking purposes only for the case of a formal package
14182 -- declared without a box (see Instantiate_Formal_Package). Such
14183 -- an instantiation does not generate any code (the actual code
14184 -- comes from actual) and thus does not need to be analyzed here.
14185 -- If the instantiation appears with a generic package body it is
14186 -- not analyzed here either.
14187
14188 elsif Nkind (Decl) = N_Package_Instantiation
14189 and then not Is_Internal (Defining_Entity (Decl))
14190 then
14191 Append_Elmt (Decl, Previous_Instances);
14192
14193 -- For a subprogram instantiation, omit instantiations intrinsic
14194 -- operations (Unchecked_Conversions, etc.) that have no bodies.
14195
14196 elsif Nkind_In (Decl, N_Function_Instantiation,
14197 N_Procedure_Instantiation)
14198 and then not Is_Intrinsic_Subprogram (Entity (Name (Decl)))
14199 then
14200 Append_Elmt (Decl, Previous_Instances);
14201
14202 elsif Nkind (Decl) = N_Package_Declaration then
14203 Collect_Previous_Instances
14204 (Visible_Declarations (Specification (Decl)));
14205 Collect_Previous_Instances
14206 (Private_Declarations (Specification (Decl)));
14207
14208 -- Previous non-generic bodies may contain instances as well
14209
14210 elsif Nkind (Decl) = N_Package_Body
14211 and then Ekind (Corresponding_Spec (Decl)) /= E_Generic_Package
14212 then
14213 Collect_Previous_Instances (Declarations (Decl));
14214
14215 elsif Nkind (Decl) = N_Subprogram_Body
14216 and then not Acts_As_Spec (Decl)
14217 and then not Is_Generic_Subprogram (Corresponding_Spec (Decl))
14218 then
14219 Collect_Previous_Instances (Declarations (Decl));
14220 end if;
14221
14222 Next (Decl);
14223 end loop;
14224 end Collect_Previous_Instances;
14225
14226 -- Start of processing for Load_Parent_Of_Generic
14227
14228 begin
14229 if not In_Same_Source_Unit (N, Spec)
14230 or else Nkind (Unit (Comp_Unit)) = N_Package_Declaration
14231 or else (Nkind (Unit (Comp_Unit)) = N_Package_Body
14232 and then not Is_In_Main_Unit (Spec))
14233 then
14234 -- Find body of parent of spec, and analyze it. A special case arises
14235 -- when the parent is an instantiation, that is to say when we are
14236 -- currently instantiating a nested generic. In that case, there is
14237 -- no separate file for the body of the enclosing instance. Instead,
14238 -- the enclosing body must be instantiated as if it were a pending
14239 -- instantiation, in order to produce the body for the nested generic
14240 -- we require now. Note that in that case the generic may be defined
14241 -- in a package body, the instance defined in the same package body,
14242 -- and the original enclosing body may not be in the main unit.
14243
14244 Inst_Node := Empty;
14245
14246 True_Parent := Parent (Spec);
14247 while Present (True_Parent)
14248 and then Nkind (True_Parent) /= N_Compilation_Unit
14249 loop
14250 if Nkind (True_Parent) = N_Package_Declaration
14251 and then
14252 Nkind (Original_Node (True_Parent)) = N_Package_Instantiation
14253 then
14254 -- Parent is a compilation unit that is an instantiation, and
14255 -- instantiation node has been replaced with package decl.
14256
14257 Inst_Node := Original_Node (True_Parent);
14258 exit;
14259
14260 elsif Nkind (True_Parent) = N_Package_Declaration
14261 and then Nkind (Parent (True_Parent)) = N_Compilation_Unit
14262 and then
14263 Nkind (Unit (Parent (True_Parent))) = N_Package_Instantiation
14264 then
14265 -- Parent is a compilation unit that is an instantiation, but
14266 -- instantiation node has not been replaced with package decl.
14267
14268 Inst_Node := Unit (Parent (True_Parent));
14269 exit;
14270
14271 elsif Nkind (True_Parent) = N_Package_Declaration
14272 and then Nkind (Parent (True_Parent)) /= N_Compilation_Unit
14273 and then Present (Generic_Parent (Specification (True_Parent)))
14274 then
14275 -- Parent is an instantiation within another specification.
14276 -- Declaration for instance has been inserted before original
14277 -- instantiation node. A direct link would be preferable?
14278
14279 Inst_Node := Next (True_Parent);
14280 while Present (Inst_Node)
14281 and then Nkind (Inst_Node) /= N_Package_Instantiation
14282 loop
14283 Next (Inst_Node);
14284 end loop;
14285
14286 -- If the instance appears within a generic, and the generic
14287 -- unit is defined within a formal package of the enclosing
14288 -- generic, there is no generic body available, and none
14289 -- needed. A more precise test should be used ???
14290
14291 if No (Inst_Node) then
14292 return;
14293 end if;
14294
14295 exit;
14296
14297 else
14298 True_Parent := Parent (True_Parent);
14299 end if;
14300 end loop;
14301
14302 -- Case where we are currently instantiating a nested generic
14303
14304 if Present (Inst_Node) then
14305 if Nkind (Parent (True_Parent)) = N_Compilation_Unit then
14306
14307 -- Instantiation node and declaration of instantiated package
14308 -- were exchanged when only the declaration was needed.
14309 -- Restore instantiation node before proceeding with body.
14310
14311 Set_Unit (Parent (True_Parent), Inst_Node);
14312 end if;
14313
14314 -- Now complete instantiation of enclosing body, if it appears in
14315 -- some other unit. If it appears in the current unit, the body
14316 -- will have been instantiated already.
14317
14318 if No (Corresponding_Body (Instance_Spec (Inst_Node))) then
14319
14320 -- We need to determine the expander mode to instantiate the
14321 -- enclosing body. Because the generic body we need may use
14322 -- global entities declared in the enclosing package (including
14323 -- aggregates) it is in general necessary to compile this body
14324 -- with expansion enabled, except if we are within a generic
14325 -- package, in which case the usual generic rule applies.
14326
14327 declare
14328 Exp_Status : Boolean := True;
14329 Scop : Entity_Id;
14330
14331 begin
14332 -- Loop through scopes looking for generic package
14333
14334 Scop := Scope (Defining_Entity (Instance_Spec (Inst_Node)));
14335 while Present (Scop)
14336 and then Scop /= Standard_Standard
14337 loop
14338 if Ekind (Scop) = E_Generic_Package then
14339 Exp_Status := False;
14340 exit;
14341 end if;
14342
14343 Scop := Scope (Scop);
14344 end loop;
14345
14346 -- Collect previous instantiations in the unit that contains
14347 -- the desired generic.
14348
14349 if Nkind (Parent (True_Parent)) /= N_Compilation_Unit
14350 and then not Body_Optional
14351 then
14352 declare
14353 Decl : Elmt_Id;
14354 Info : Pending_Body_Info;
14355 Par : Node_Id;
14356
14357 begin
14358 Par := Parent (Inst_Node);
14359 while Present (Par) loop
14360 exit when Nkind (Parent (Par)) = N_Compilation_Unit;
14361 Par := Parent (Par);
14362 end loop;
14363
14364 pragma Assert (Present (Par));
14365
14366 if Nkind (Par) = N_Package_Body then
14367 Collect_Previous_Instances (Declarations (Par));
14368
14369 elsif Nkind (Par) = N_Package_Declaration then
14370 Collect_Previous_Instances
14371 (Visible_Declarations (Specification (Par)));
14372 Collect_Previous_Instances
14373 (Private_Declarations (Specification (Par)));
14374
14375 else
14376 -- Enclosing unit is a subprogram body. In this
14377 -- case all instance bodies are processed in order
14378 -- and there is no need to collect them separately.
14379
14380 null;
14381 end if;
14382
14383 Decl := First_Elmt (Previous_Instances);
14384 while Present (Decl) loop
14385 Info :=
14386 (Act_Decl =>
14387 Instance_Spec (Node (Decl)),
14388 Config_Switches => Save_Config_Switches,
14389 Current_Sem_Unit =>
14390 Get_Code_Unit (Sloc (Node (Decl))),
14391 Expander_Status => Exp_Status,
14392 Inst_Node => Node (Decl),
14393 Local_Suppress_Stack_Top =>
14394 Local_Suppress_Stack_Top,
14395 Scope_Suppress => Scope_Suppress,
14396 Warnings => Save_Warnings);
14397
14398 -- Package instance
14399
14400 if Nkind (Node (Decl)) = N_Package_Instantiation
14401 then
14402 Instantiate_Package_Body
14403 (Info, Body_Optional => True);
14404
14405 -- Subprogram instance
14406
14407 else
14408 -- The instance_spec is in the wrapper package,
14409 -- usually followed by its local renaming
14410 -- declaration. See Build_Subprogram_Renaming
14411 -- for details. If the instance carries aspects,
14412 -- these result in the corresponding pragmas,
14413 -- inserted after the subprogram declaration.
14414 -- They must be skipped as well when retrieving
14415 -- the desired spec. Some of them may have been
14416 -- rewritten as null statements.
14417 -- A direct link would be more robust ???
14418
14419 declare
14420 Decl : Node_Id :=
14421 (Last (Visible_Declarations
14422 (Specification (Info.Act_Decl))));
14423 begin
14424 while Nkind_In (Decl,
14425 N_Null_Statement,
14426 N_Pragma,
14427 N_Subprogram_Renaming_Declaration)
14428 loop
14429 Decl := Prev (Decl);
14430 end loop;
14431
14432 Info.Act_Decl := Decl;
14433 end;
14434
14435 Instantiate_Subprogram_Body
14436 (Info, Body_Optional => True);
14437 end if;
14438
14439 Next_Elmt (Decl);
14440 end loop;
14441 end;
14442 end if;
14443
14444 Instantiate_Package_Body
14445 (Body_Info =>
14446 ((Act_Decl => True_Parent,
14447 Config_Switches => Save_Config_Switches,
14448 Current_Sem_Unit =>
14449 Get_Code_Unit (Sloc (Inst_Node)),
14450 Expander_Status => Exp_Status,
14451 Inst_Node => Inst_Node,
14452 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
14453 Scope_Suppress => Scope_Suppress,
14454 Warnings => Save_Warnings)),
14455 Body_Optional => Body_Optional);
14456 end;
14457 end if;
14458
14459 -- Case where we are not instantiating a nested generic
14460
14461 else
14462 Opt.Style_Check := False;
14463 Expander_Mode_Save_And_Set (True);
14464 Load_Needed_Body (Comp_Unit, OK);
14465 Opt.Style_Check := Saved_Style_Check;
14466 Restore_Warnings (Saved_Warnings);
14467 Expander_Mode_Restore;
14468
14469 if not OK
14470 and then Unit_Requires_Body (Defining_Entity (Spec))
14471 and then not Body_Optional
14472 then
14473 declare
14474 Bname : constant Unit_Name_Type :=
14475 Get_Body_Name (Get_Unit_Name (Unit (Comp_Unit)));
14476
14477 begin
14478 -- In CodePeer mode, the missing body may make the analysis
14479 -- incomplete, but we do not treat it as fatal.
14480
14481 if CodePeer_Mode then
14482 return;
14483
14484 else
14485 Error_Msg_Unit_1 := Bname;
14486 Error_Msg_N ("this instantiation requires$!", N);
14487 Error_Msg_File_1 :=
14488 Get_File_Name (Bname, Subunit => False);
14489 Error_Msg_N ("\but file{ was not found!", N);
14490 raise Unrecoverable_Error;
14491 end if;
14492 end;
14493 end if;
14494 end if;
14495 end if;
14496
14497 -- If loading parent of the generic caused an instantiation circularity,
14498 -- we abandon compilation at this point, because otherwise in some cases
14499 -- we get into trouble with infinite recursions after this point.
14500
14501 if Circularity_Detected then
14502 raise Unrecoverable_Error;
14503 end if;
14504 end Load_Parent_Of_Generic;
14505
14506 ---------------------------------
14507 -- Map_Formal_Package_Entities --
14508 ---------------------------------
14509
14510 procedure Map_Formal_Package_Entities (Form : Entity_Id; Act : Entity_Id) is
14511 E1 : Entity_Id;
14512 E2 : Entity_Id;
14513
14514 begin
14515 Set_Instance_Of (Form, Act);
14516
14517 -- Traverse formal and actual package to map the corresponding entities.
14518 -- We skip over internal entities that may be generated during semantic
14519 -- analysis, and find the matching entities by name, given that they
14520 -- must appear in the same order.
14521
14522 E1 := First_Entity (Form);
14523 E2 := First_Entity (Act);
14524 while Present (E1) and then E1 /= First_Private_Entity (Form) loop
14525 -- Could this test be a single condition??? Seems like it could, and
14526 -- isn't FPE (Form) a constant anyway???
14527
14528 if not Is_Internal (E1)
14529 and then Present (Parent (E1))
14530 and then not Is_Class_Wide_Type (E1)
14531 and then not Is_Internal_Name (Chars (E1))
14532 then
14533 while Present (E2) and then Chars (E2) /= Chars (E1) loop
14534 Next_Entity (E2);
14535 end loop;
14536
14537 if No (E2) then
14538 exit;
14539 else
14540 Set_Instance_Of (E1, E2);
14541
14542 if Is_Type (E1) and then Is_Tagged_Type (E2) then
14543 Set_Instance_Of (Class_Wide_Type (E1), Class_Wide_Type (E2));
14544 end if;
14545
14546 if Is_Constrained (E1) then
14547 Set_Instance_Of (Base_Type (E1), Base_Type (E2));
14548 end if;
14549
14550 if Ekind (E1) = E_Package and then No (Renamed_Object (E1)) then
14551 Map_Formal_Package_Entities (E1, E2);
14552 end if;
14553 end if;
14554 end if;
14555
14556 Next_Entity (E1);
14557 end loop;
14558 end Map_Formal_Package_Entities;
14559
14560 -----------------------
14561 -- Move_Freeze_Nodes --
14562 -----------------------
14563
14564 procedure Move_Freeze_Nodes
14565 (Out_Of : Entity_Id;
14566 After : Node_Id;
14567 L : List_Id)
14568 is
14569 Decl : Node_Id;
14570 Next_Decl : Node_Id;
14571 Next_Node : Node_Id := After;
14572 Spec : Node_Id;
14573
14574 function Is_Outer_Type (T : Entity_Id) return Boolean;
14575 -- Check whether entity is declared in a scope external to that of the
14576 -- generic unit.
14577
14578 -------------------
14579 -- Is_Outer_Type --
14580 -------------------
14581
14582 function Is_Outer_Type (T : Entity_Id) return Boolean is
14583 Scop : Entity_Id := Scope (T);
14584
14585 begin
14586 if Scope_Depth (Scop) < Scope_Depth (Out_Of) then
14587 return True;
14588
14589 else
14590 while Scop /= Standard_Standard loop
14591 if Scop = Out_Of then
14592 return False;
14593 else
14594 Scop := Scope (Scop);
14595 end if;
14596 end loop;
14597
14598 return True;
14599 end if;
14600 end Is_Outer_Type;
14601
14602 -- Start of processing for Move_Freeze_Nodes
14603
14604 begin
14605 if No (L) then
14606 return;
14607 end if;
14608
14609 -- First remove the freeze nodes that may appear before all other
14610 -- declarations.
14611
14612 Decl := First (L);
14613 while Present (Decl)
14614 and then Nkind (Decl) = N_Freeze_Entity
14615 and then Is_Outer_Type (Entity (Decl))
14616 loop
14617 Decl := Remove_Head (L);
14618 Insert_After (Next_Node, Decl);
14619 Set_Analyzed (Decl, False);
14620 Next_Node := Decl;
14621 Decl := First (L);
14622 end loop;
14623
14624 -- Next scan the list of declarations and remove each freeze node that
14625 -- appears ahead of the current node.
14626
14627 while Present (Decl) loop
14628 while Present (Next (Decl))
14629 and then Nkind (Next (Decl)) = N_Freeze_Entity
14630 and then Is_Outer_Type (Entity (Next (Decl)))
14631 loop
14632 Next_Decl := Remove_Next (Decl);
14633 Insert_After (Next_Node, Next_Decl);
14634 Set_Analyzed (Next_Decl, False);
14635 Next_Node := Next_Decl;
14636 end loop;
14637
14638 -- If the declaration is a nested package or concurrent type, then
14639 -- recurse. Nested generic packages will have been processed from the
14640 -- inside out.
14641
14642 case Nkind (Decl) is
14643 when N_Package_Declaration =>
14644 Spec := Specification (Decl);
14645
14646 when N_Task_Type_Declaration =>
14647 Spec := Task_Definition (Decl);
14648
14649 when N_Protected_Type_Declaration =>
14650 Spec := Protected_Definition (Decl);
14651
14652 when others =>
14653 Spec := Empty;
14654 end case;
14655
14656 if Present (Spec) then
14657 Move_Freeze_Nodes (Out_Of, Next_Node, Visible_Declarations (Spec));
14658 Move_Freeze_Nodes (Out_Of, Next_Node, Private_Declarations (Spec));
14659 end if;
14660
14661 Next (Decl);
14662 end loop;
14663 end Move_Freeze_Nodes;
14664
14665 ----------------
14666 -- Next_Assoc --
14667 ----------------
14668
14669 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr is
14670 begin
14671 return Generic_Renamings.Table (E).Next_In_HTable;
14672 end Next_Assoc;
14673
14674 ------------------------
14675 -- Preanalyze_Actuals --
14676 ------------------------
14677
14678 procedure Preanalyze_Actuals (N : Node_Id; Inst : Entity_Id := Empty) is
14679 procedure Perform_Appropriate_Analysis (N : Node_Id);
14680 -- Determine if the actuals we are analyzing come from a generic
14681 -- instantiation that is a library unit and dispatch accordingly.
14682
14683 ----------------------------------
14684 -- Perform_Appropriate_Analysis --
14685 ----------------------------------
14686
14687 procedure Perform_Appropriate_Analysis (N : Node_Id) is
14688 begin
14689 -- When we have a library instantiation we cannot allow any expansion
14690 -- to occur, since there may be no place to put it. Instead, in that
14691 -- case we perform a preanalysis of the actual.
14692
14693 if Present (Inst) and then Is_Compilation_Unit (Inst) then
14694 Preanalyze (N);
14695 else
14696 Analyze (N);
14697 end if;
14698 end Perform_Appropriate_Analysis;
14699
14700 -- Local variables
14701
14702 Errs : constant Nat := Serious_Errors_Detected;
14703
14704 Assoc : Node_Id;
14705 Act : Node_Id;
14706
14707 Cur : Entity_Id := Empty;
14708 -- Current homograph of the instance name
14709
14710 Vis : Boolean := False;
14711 -- Saved visibility status of the current homograph
14712
14713 -- Start of processing for Preanalyze_Actuals
14714
14715 begin
14716 Assoc := First (Generic_Associations (N));
14717
14718 -- If the instance is a child unit, its name may hide an outer homonym,
14719 -- so make it invisible to perform name resolution on the actuals.
14720
14721 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name
14722 and then Present
14723 (Current_Entity (Defining_Identifier (Defining_Unit_Name (N))))
14724 then
14725 Cur := Current_Entity (Defining_Identifier (Defining_Unit_Name (N)));
14726
14727 if Is_Compilation_Unit (Cur) then
14728 Vis := Is_Immediately_Visible (Cur);
14729 Set_Is_Immediately_Visible (Cur, False);
14730 else
14731 Cur := Empty;
14732 end if;
14733 end if;
14734
14735 while Present (Assoc) loop
14736 if Nkind (Assoc) /= N_Others_Choice then
14737 Act := Explicit_Generic_Actual_Parameter (Assoc);
14738
14739 -- Within a nested instantiation, a defaulted actual is an empty
14740 -- association, so nothing to analyze. If the subprogram actual
14741 -- is an attribute, analyze prefix only, because actual is not a
14742 -- complete attribute reference.
14743
14744 -- If actual is an allocator, analyze expression only. The full
14745 -- analysis can generate code, and if instance is a compilation
14746 -- unit we have to wait until the package instance is installed
14747 -- to have a proper place to insert this code.
14748
14749 -- String literals may be operators, but at this point we do not
14750 -- know whether the actual is a formal subprogram or a string.
14751
14752 if No (Act) then
14753 null;
14754
14755 elsif Nkind (Act) = N_Attribute_Reference then
14756 Perform_Appropriate_Analysis (Prefix (Act));
14757
14758 elsif Nkind (Act) = N_Explicit_Dereference then
14759 Perform_Appropriate_Analysis (Prefix (Act));
14760
14761 elsif Nkind (Act) = N_Allocator then
14762 declare
14763 Expr : constant Node_Id := Expression (Act);
14764
14765 begin
14766 if Nkind (Expr) = N_Subtype_Indication then
14767 Perform_Appropriate_Analysis (Subtype_Mark (Expr));
14768
14769 -- Analyze separately each discriminant constraint, when
14770 -- given with a named association.
14771
14772 declare
14773 Constr : Node_Id;
14774
14775 begin
14776 Constr := First (Constraints (Constraint (Expr)));
14777 while Present (Constr) loop
14778 if Nkind (Constr) = N_Discriminant_Association then
14779 Perform_Appropriate_Analysis
14780 (Expression (Constr));
14781 else
14782 Perform_Appropriate_Analysis (Constr);
14783 end if;
14784
14785 Next (Constr);
14786 end loop;
14787 end;
14788
14789 else
14790 Perform_Appropriate_Analysis (Expr);
14791 end if;
14792 end;
14793
14794 elsif Nkind (Act) /= N_Operator_Symbol then
14795 Perform_Appropriate_Analysis (Act);
14796
14797 -- Within a package instance, mark actuals that are limited
14798 -- views, so their use can be moved to the body of the
14799 -- enclosing unit.
14800
14801 if Is_Entity_Name (Act)
14802 and then Is_Type (Entity (Act))
14803 and then From_Limited_With (Entity (Act))
14804 and then Present (Inst)
14805 then
14806 Append_Elmt (Entity (Act), Incomplete_Actuals (Inst));
14807 end if;
14808 end if;
14809
14810 if Errs /= Serious_Errors_Detected then
14811
14812 -- Do a minimal analysis of the generic, to prevent spurious
14813 -- warnings complaining about the generic being unreferenced,
14814 -- before abandoning the instantiation.
14815
14816 Perform_Appropriate_Analysis (Name (N));
14817
14818 if Is_Entity_Name (Name (N))
14819 and then Etype (Name (N)) /= Any_Type
14820 then
14821 Generate_Reference (Entity (Name (N)), Name (N));
14822 Set_Is_Instantiated (Entity (Name (N)));
14823 end if;
14824
14825 if Present (Cur) then
14826
14827 -- For the case of a child instance hiding an outer homonym,
14828 -- provide additional warning which might explain the error.
14829
14830 Set_Is_Immediately_Visible (Cur, Vis);
14831 Error_Msg_NE
14832 ("& hides outer unit with the same name??",
14833 N, Defining_Unit_Name (N));
14834 end if;
14835
14836 Abandon_Instantiation (Act);
14837 end if;
14838 end if;
14839
14840 Next (Assoc);
14841 end loop;
14842
14843 if Present (Cur) then
14844 Set_Is_Immediately_Visible (Cur, Vis);
14845 end if;
14846 end Preanalyze_Actuals;
14847
14848 -------------------------------
14849 -- Provide_Completing_Bodies --
14850 -------------------------------
14851
14852 procedure Provide_Completing_Bodies (N : Node_Id) is
14853 procedure Build_Completing_Body (Subp_Decl : Node_Id);
14854 -- Generate the completing body for subprogram declaration Subp_Decl
14855
14856 procedure Provide_Completing_Bodies_In (Decls : List_Id);
14857 -- Generating completing bodies for all subprograms found in declarative
14858 -- list Decls.
14859
14860 ---------------------------
14861 -- Build_Completing_Body --
14862 ---------------------------
14863
14864 procedure Build_Completing_Body (Subp_Decl : Node_Id) is
14865 Loc : constant Source_Ptr := Sloc (Subp_Decl);
14866 Subp_Id : constant Entity_Id := Defining_Entity (Subp_Decl);
14867 Spec : Node_Id;
14868
14869 begin
14870 -- Nothing to do if the subprogram already has a completing body
14871
14872 if Present (Corresponding_Body (Subp_Decl)) then
14873 return;
14874
14875 -- Mark the function as having a valid return statement even though
14876 -- the body contains a single raise statement.
14877
14878 elsif Ekind (Subp_Id) = E_Function then
14879 Set_Return_Present (Subp_Id);
14880 end if;
14881
14882 -- Clone the specification to obtain new entities and reset the only
14883 -- semantic field.
14884
14885 Spec := Copy_Subprogram_Spec (Specification (Subp_Decl));
14886 Set_Generic_Parent (Spec, Empty);
14887
14888 -- Generate:
14889 -- function Func ... return ... is
14890 -- <or>
14891 -- procedure Proc ... is
14892 -- begin
14893 -- raise Program_Error with "access before elaboration";
14894 -- edn Proc;
14895
14896 Insert_After_And_Analyze (Subp_Decl,
14897 Make_Subprogram_Body (Loc,
14898 Specification => Spec,
14899 Declarations => New_List,
14900 Handled_Statement_Sequence =>
14901 Make_Handled_Sequence_Of_Statements (Loc,
14902 Statements => New_List (
14903 Make_Raise_Program_Error (Loc,
14904 Reason => PE_Access_Before_Elaboration)))));
14905 end Build_Completing_Body;
14906
14907 ----------------------------------
14908 -- Provide_Completing_Bodies_In --
14909 ----------------------------------
14910
14911 procedure Provide_Completing_Bodies_In (Decls : List_Id) is
14912 Decl : Node_Id;
14913
14914 begin
14915 if Present (Decls) then
14916 Decl := First (Decls);
14917 while Present (Decl) loop
14918 Provide_Completing_Bodies (Decl);
14919 Next (Decl);
14920 end loop;
14921 end if;
14922 end Provide_Completing_Bodies_In;
14923
14924 -- Local variables
14925
14926 Spec : Node_Id;
14927
14928 -- Start of processing for Provide_Completing_Bodies
14929
14930 begin
14931 if Nkind (N) = N_Package_Declaration then
14932 Spec := Specification (N);
14933
14934 Push_Scope (Defining_Entity (N));
14935 Provide_Completing_Bodies_In (Visible_Declarations (Spec));
14936 Provide_Completing_Bodies_In (Private_Declarations (Spec));
14937 Pop_Scope;
14938
14939 elsif Nkind (N) = N_Subprogram_Declaration then
14940 Build_Completing_Body (N);
14941 end if;
14942 end Provide_Completing_Bodies;
14943
14944 -------------------
14945 -- Remove_Parent --
14946 -------------------
14947
14948 procedure Remove_Parent (In_Body : Boolean := False) is
14949 S : Entity_Id := Current_Scope;
14950 -- S is the scope containing the instantiation just completed. The scope
14951 -- stack contains the parent instances of the instantiation, followed by
14952 -- the original S.
14953
14954 Cur_P : Entity_Id;
14955 E : Entity_Id;
14956 P : Entity_Id;
14957 Hidden : Elmt_Id;
14958
14959 begin
14960 -- After child instantiation is complete, remove from scope stack the
14961 -- extra copy of the current scope, and then remove parent instances.
14962
14963 if not In_Body then
14964 Pop_Scope;
14965
14966 while Current_Scope /= S loop
14967 P := Current_Scope;
14968 End_Package_Scope (Current_Scope);
14969
14970 if In_Open_Scopes (P) then
14971 E := First_Entity (P);
14972 while Present (E) loop
14973 Set_Is_Immediately_Visible (E, True);
14974 Next_Entity (E);
14975 end loop;
14976
14977 -- If instantiation is declared in a block, it is the enclosing
14978 -- scope that might be a parent instance. Note that only one
14979 -- block can be involved, because the parent instances have
14980 -- been installed within it.
14981
14982 if Ekind (P) = E_Block then
14983 Cur_P := Scope (P);
14984 else
14985 Cur_P := P;
14986 end if;
14987
14988 if Is_Generic_Instance (Cur_P) and then P /= Current_Scope then
14989 -- We are within an instance of some sibling. Retain
14990 -- visibility of parent, for proper subsequent cleanup, and
14991 -- reinstall private declarations as well.
14992
14993 Set_In_Private_Part (P);
14994 Install_Private_Declarations (P);
14995 end if;
14996
14997 -- If the ultimate parent is a top-level unit recorded in
14998 -- Instance_Parent_Unit, then reset its visibility to what it was
14999 -- before instantiation. (It's not clear what the purpose is of
15000 -- testing whether Scope (P) is In_Open_Scopes, but that test was
15001 -- present before the ultimate parent test was added.???)
15002
15003 elsif not In_Open_Scopes (Scope (P))
15004 or else (P = Instance_Parent_Unit
15005 and then not Parent_Unit_Visible)
15006 then
15007 Set_Is_Immediately_Visible (P, False);
15008
15009 -- If the current scope is itself an instantiation of a generic
15010 -- nested within P, and we are in the private part of body of this
15011 -- instantiation, restore the full views of P, that were removed
15012 -- in End_Package_Scope above. This obscure case can occur when a
15013 -- subunit of a generic contains an instance of a child unit of
15014 -- its generic parent unit.
15015
15016 elsif S = Current_Scope and then Is_Generic_Instance (S) then
15017 declare
15018 Par : constant Entity_Id :=
15019 Generic_Parent (Package_Specification (S));
15020 begin
15021 if Present (Par)
15022 and then P = Scope (Par)
15023 and then (In_Package_Body (S) or else In_Private_Part (S))
15024 then
15025 Set_In_Private_Part (P);
15026 Install_Private_Declarations (P);
15027 end if;
15028 end;
15029 end if;
15030 end loop;
15031
15032 -- Reset visibility of entities in the enclosing scope
15033
15034 Set_Is_Hidden_Open_Scope (Current_Scope, False);
15035
15036 Hidden := First_Elmt (Hidden_Entities);
15037 while Present (Hidden) loop
15038 Set_Is_Immediately_Visible (Node (Hidden), True);
15039 Next_Elmt (Hidden);
15040 end loop;
15041
15042 else
15043 -- Each body is analyzed separately, and there is no context that
15044 -- needs preserving from one body instance to the next, so remove all
15045 -- parent scopes that have been installed.
15046
15047 while Present (S) loop
15048 End_Package_Scope (S);
15049 Set_Is_Immediately_Visible (S, False);
15050 S := Current_Scope;
15051 exit when S = Standard_Standard;
15052 end loop;
15053 end if;
15054 end Remove_Parent;
15055
15056 -----------------
15057 -- Restore_Env --
15058 -----------------
15059
15060 procedure Restore_Env is
15061 Saved : Instance_Env renames Instance_Envs.Table (Instance_Envs.Last);
15062
15063 begin
15064 if No (Current_Instantiated_Parent.Act_Id) then
15065 -- Restore environment after subprogram inlining
15066
15067 Restore_Private_Views (Empty);
15068 end if;
15069
15070 Current_Instantiated_Parent := Saved.Instantiated_Parent;
15071 Exchanged_Views := Saved.Exchanged_Views;
15072 Hidden_Entities := Saved.Hidden_Entities;
15073 Current_Sem_Unit := Saved.Current_Sem_Unit;
15074 Parent_Unit_Visible := Saved.Parent_Unit_Visible;
15075 Instance_Parent_Unit := Saved.Instance_Parent_Unit;
15076
15077 Restore_Config_Switches (Saved.Switches);
15078
15079 Instance_Envs.Decrement_Last;
15080 end Restore_Env;
15081
15082 ---------------------------
15083 -- Restore_Private_Views --
15084 ---------------------------
15085
15086 procedure Restore_Private_Views
15087 (Pack_Id : Entity_Id;
15088 Is_Package : Boolean := True)
15089 is
15090 M : Elmt_Id;
15091 E : Entity_Id;
15092 Typ : Entity_Id;
15093 Dep_Elmt : Elmt_Id;
15094 Dep_Typ : Node_Id;
15095
15096 procedure Restore_Nested_Formal (Formal : Entity_Id);
15097 -- Hide the generic formals of formal packages declared with box which
15098 -- were reachable in the current instantiation.
15099
15100 ---------------------------
15101 -- Restore_Nested_Formal --
15102 ---------------------------
15103
15104 procedure Restore_Nested_Formal (Formal : Entity_Id) is
15105 Ent : Entity_Id;
15106
15107 begin
15108 if Present (Renamed_Object (Formal))
15109 and then Denotes_Formal_Package (Renamed_Object (Formal), True)
15110 then
15111 return;
15112
15113 elsif Present (Associated_Formal_Package (Formal)) then
15114 Ent := First_Entity (Formal);
15115 while Present (Ent) loop
15116 exit when Ekind (Ent) = E_Package
15117 and then Renamed_Entity (Ent) = Renamed_Entity (Formal);
15118
15119 Set_Is_Hidden (Ent);
15120 Set_Is_Potentially_Use_Visible (Ent, False);
15121
15122 -- If package, then recurse
15123
15124 if Ekind (Ent) = E_Package then
15125 Restore_Nested_Formal (Ent);
15126 end if;
15127
15128 Next_Entity (Ent);
15129 end loop;
15130 end if;
15131 end Restore_Nested_Formal;
15132
15133 -- Start of processing for Restore_Private_Views
15134
15135 begin
15136 M := First_Elmt (Exchanged_Views);
15137 while Present (M) loop
15138 Typ := Node (M);
15139
15140 -- Subtypes of types whose views have been exchanged, and that are
15141 -- defined within the instance, were not on the Private_Dependents
15142 -- list on entry to the instance, so they have to be exchanged
15143 -- explicitly now, in order to remain consistent with the view of the
15144 -- parent type.
15145
15146 if Ekind_In (Typ, E_Private_Type,
15147 E_Limited_Private_Type,
15148 E_Record_Type_With_Private)
15149 then
15150 Dep_Elmt := First_Elmt (Private_Dependents (Typ));
15151 while Present (Dep_Elmt) loop
15152 Dep_Typ := Node (Dep_Elmt);
15153
15154 if Scope (Dep_Typ) = Pack_Id
15155 and then Present (Full_View (Dep_Typ))
15156 then
15157 Replace_Elmt (Dep_Elmt, Full_View (Dep_Typ));
15158 Exchange_Declarations (Dep_Typ);
15159 end if;
15160
15161 Next_Elmt (Dep_Elmt);
15162 end loop;
15163 end if;
15164
15165 Exchange_Declarations (Node (M));
15166 Next_Elmt (M);
15167 end loop;
15168
15169 if No (Pack_Id) then
15170 return;
15171 end if;
15172
15173 -- Make the generic formal parameters private, and make the formal types
15174 -- into subtypes of the actuals again.
15175
15176 E := First_Entity (Pack_Id);
15177 while Present (E) loop
15178 Set_Is_Hidden (E, True);
15179
15180 if Is_Type (E)
15181 and then Nkind (Parent (E)) = N_Subtype_Declaration
15182 then
15183 -- If the actual for E is itself a generic actual type from
15184 -- an enclosing instance, E is still a generic actual type
15185 -- outside of the current instance. This matter when resolving
15186 -- an overloaded call that may be ambiguous in the enclosing
15187 -- instance, when two of its actuals coincide.
15188
15189 if Is_Entity_Name (Subtype_Indication (Parent (E)))
15190 and then Is_Generic_Actual_Type
15191 (Entity (Subtype_Indication (Parent (E))))
15192 then
15193 null;
15194 else
15195 Set_Is_Generic_Actual_Type (E, False);
15196
15197 -- It might seem reasonable to clear the Is_Generic_Actual_Type
15198 -- flag also on the Full_View if the type is private, since it
15199 -- was set also on this Full_View. However, this flag is relied
15200 -- upon by Covers to spot "types exported from instantiations"
15201 -- which are implicit Full_Views built for instantiations made
15202 -- on private types and we get type mismatches if we do it when
15203 -- the block exchanging the declarations below triggers ???
15204
15205 -- if Is_Private_Type (E) and then Present (Full_View (E)) then
15206 -- Set_Is_Generic_Actual_Type (Full_View (E), False);
15207 -- end if;
15208 end if;
15209
15210 -- An unusual case of aliasing: the actual may also be directly
15211 -- visible in the generic, and be private there, while it is fully
15212 -- visible in the context of the instance. The internal subtype
15213 -- is private in the instance but has full visibility like its
15214 -- parent in the enclosing scope. This enforces the invariant that
15215 -- the privacy status of all private dependents of a type coincide
15216 -- with that of the parent type. This can only happen when a
15217 -- generic child unit is instantiated within a sibling.
15218
15219 if Is_Private_Type (E)
15220 and then not Is_Private_Type (Etype (E))
15221 then
15222 Exchange_Declarations (E);
15223 end if;
15224
15225 elsif Ekind (E) = E_Package then
15226
15227 -- The end of the renaming list is the renaming of the generic
15228 -- package itself. If the instance is a subprogram, all entities
15229 -- in the corresponding package are renamings. If this entity is
15230 -- a formal package, make its own formals private as well. The
15231 -- actual in this case is itself the renaming of an instantiation.
15232 -- If the entity is not a package renaming, it is the entity
15233 -- created to validate formal package actuals: ignore it.
15234
15235 -- If the actual is itself a formal package for the enclosing
15236 -- generic, or the actual for such a formal package, it remains
15237 -- visible on exit from the instance, and therefore nothing needs
15238 -- to be done either, except to keep it accessible.
15239
15240 if Is_Package and then Renamed_Object (E) = Pack_Id then
15241 exit;
15242
15243 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
15244 null;
15245
15246 elsif
15247 Denotes_Formal_Package (Renamed_Object (E), True, Pack_Id)
15248 then
15249 Set_Is_Hidden (E, False);
15250
15251 else
15252 declare
15253 Act_P : constant Entity_Id := Renamed_Object (E);
15254 Id : Entity_Id;
15255
15256 begin
15257 Id := First_Entity (Act_P);
15258 while Present (Id)
15259 and then Id /= First_Private_Entity (Act_P)
15260 loop
15261 exit when Ekind (Id) = E_Package
15262 and then Renamed_Object (Id) = Act_P;
15263
15264 Set_Is_Hidden (Id, True);
15265 Set_Is_Potentially_Use_Visible (Id, In_Use (Act_P));
15266
15267 if Ekind (Id) = E_Package then
15268 Restore_Nested_Formal (Id);
15269 end if;
15270
15271 Next_Entity (Id);
15272 end loop;
15273 end;
15274 end if;
15275 end if;
15276
15277 Next_Entity (E);
15278 end loop;
15279 end Restore_Private_Views;
15280
15281 --------------
15282 -- Save_Env --
15283 --------------
15284
15285 procedure Save_Env
15286 (Gen_Unit : Entity_Id;
15287 Act_Unit : Entity_Id)
15288 is
15289 begin
15290 Init_Env;
15291 Set_Instance_Env (Gen_Unit, Act_Unit);
15292 end Save_Env;
15293
15294 ----------------------------
15295 -- Save_Global_References --
15296 ----------------------------
15297
15298 procedure Save_Global_References (Templ : Node_Id) is
15299
15300 -- ??? it is horrible to use global variables in highly recursive code
15301
15302 E : Entity_Id;
15303 -- The entity of the current associated node
15304
15305 Gen_Scope : Entity_Id;
15306 -- The scope of the generic for which references are being saved
15307
15308 N2 : Node_Id;
15309 -- The current associated node
15310
15311 function Is_Global (E : Entity_Id) return Boolean;
15312 -- Check whether entity is defined outside of generic unit. Examine the
15313 -- scope of an entity, and the scope of the scope, etc, until we find
15314 -- either Standard, in which case the entity is global, or the generic
15315 -- unit itself, which indicates that the entity is local. If the entity
15316 -- is the generic unit itself, as in the case of a recursive call, or
15317 -- the enclosing generic unit, if different from the current scope, then
15318 -- it is local as well, because it will be replaced at the point of
15319 -- instantiation. On the other hand, if it is a reference to a child
15320 -- unit of a common ancestor, which appears in an instantiation, it is
15321 -- global because it is used to denote a specific compilation unit at
15322 -- the time the instantiations will be analyzed.
15323
15324 procedure Qualify_Universal_Operands
15325 (Op : Node_Id;
15326 Func_Call : Node_Id);
15327 -- Op denotes a binary or unary operator in generic template Templ. Node
15328 -- Func_Call is the function call alternative of the operator within the
15329 -- the analyzed copy of the template. Change each operand which yields a
15330 -- universal type by wrapping it into a qualified expression
15331 --
15332 -- Actual_Typ'(Operand)
15333 --
15334 -- where Actual_Typ is the type of corresponding actual parameter of
15335 -- Operand in Func_Call.
15336
15337 procedure Reset_Entity (N : Node_Id);
15338 -- Save semantic information on global entity so that it is not resolved
15339 -- again at instantiation time.
15340
15341 procedure Save_Entity_Descendants (N : Node_Id);
15342 -- Apply Save_Global_References to the two syntactic descendants of
15343 -- non-terminal nodes that carry an Associated_Node and are processed
15344 -- through Reset_Entity. Once the global entity (if any) has been
15345 -- captured together with its type, only two syntactic descendants need
15346 -- to be traversed to complete the processing of the tree rooted at N.
15347 -- This applies to Selected_Components, Expanded_Names, and to Operator
15348 -- nodes. N can also be a character literal, identifier, or operator
15349 -- symbol node, but the call has no effect in these cases.
15350
15351 procedure Save_Global_Defaults (N1 : Node_Id; N2 : Node_Id);
15352 -- Default actuals in nested instances must be handled specially
15353 -- because there is no link to them from the original tree. When an
15354 -- actual subprogram is given by a default, we add an explicit generic
15355 -- association for it in the instantiation node. When we save the
15356 -- global references on the name of the instance, we recover the list
15357 -- of generic associations, and add an explicit one to the original
15358 -- generic tree, through which a global actual can be preserved.
15359 -- Similarly, if a child unit is instantiated within a sibling, in the
15360 -- context of the parent, we must preserve the identifier of the parent
15361 -- so that it can be properly resolved in a subsequent instantiation.
15362
15363 procedure Save_Global_Descendant (D : Union_Id);
15364 -- Apply Save_References recursively to the descendants of node D
15365
15366 procedure Save_References (N : Node_Id);
15367 -- This is the recursive procedure that does the work, once the
15368 -- enclosing generic scope has been established.
15369
15370 ---------------
15371 -- Is_Global --
15372 ---------------
15373
15374 function Is_Global (E : Entity_Id) return Boolean is
15375 Se : Entity_Id;
15376
15377 function Is_Instance_Node (Decl : Node_Id) return Boolean;
15378 -- Determine whether the parent node of a reference to a child unit
15379 -- denotes an instantiation or a formal package, in which case the
15380 -- reference to the child unit is global, even if it appears within
15381 -- the current scope (e.g. when the instance appears within the body
15382 -- of an ancestor).
15383
15384 ----------------------
15385 -- Is_Instance_Node --
15386 ----------------------
15387
15388 function Is_Instance_Node (Decl : Node_Id) return Boolean is
15389 begin
15390 return Nkind (Decl) in N_Generic_Instantiation
15391 or else
15392 Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration;
15393 end Is_Instance_Node;
15394
15395 -- Start of processing for Is_Global
15396
15397 begin
15398 if E = Gen_Scope then
15399 return False;
15400
15401 elsif E = Standard_Standard then
15402 return True;
15403
15404 elsif Is_Child_Unit (E)
15405 and then (Is_Instance_Node (Parent (N2))
15406 or else (Nkind (Parent (N2)) = N_Expanded_Name
15407 and then N2 = Selector_Name (Parent (N2))
15408 and then
15409 Is_Instance_Node (Parent (Parent (N2)))))
15410 then
15411 return True;
15412
15413 else
15414 Se := Scope (E);
15415 while Se /= Gen_Scope loop
15416 if Se = Standard_Standard then
15417 return True;
15418 else
15419 Se := Scope (Se);
15420 end if;
15421 end loop;
15422
15423 return False;
15424 end if;
15425 end Is_Global;
15426
15427 --------------------------------
15428 -- Qualify_Universal_Operands --
15429 --------------------------------
15430
15431 procedure Qualify_Universal_Operands
15432 (Op : Node_Id;
15433 Func_Call : Node_Id)
15434 is
15435 procedure Qualify_Operand (Opnd : Node_Id; Actual : Node_Id);
15436 -- Rewrite operand Opnd as a qualified expression of the form
15437 --
15438 -- Actual_Typ'(Opnd)
15439 --
15440 -- where Actual is the corresponding actual parameter of Opnd in
15441 -- function call Func_Call.
15442
15443 function Qualify_Type
15444 (Loc : Source_Ptr;
15445 Typ : Entity_Id) return Node_Id;
15446 -- Qualify type Typ by creating a selected component of the form
15447 --
15448 -- Scope_Of_Typ.Typ
15449
15450 ---------------------
15451 -- Qualify_Operand --
15452 ---------------------
15453
15454 procedure Qualify_Operand (Opnd : Node_Id; Actual : Node_Id) is
15455 Loc : constant Source_Ptr := Sloc (Opnd);
15456 Typ : constant Entity_Id := Etype (Actual);
15457 Mark : Node_Id;
15458 Qual : Node_Id;
15459
15460 begin
15461 -- Qualify the operand when it is of a universal type. Note that
15462 -- the template is unanalyzed and it is not possible to directly
15463 -- query the type. This transformation is not done when the type
15464 -- of the actual is internally generated because the type will be
15465 -- regenerated in the instance.
15466
15467 if Yields_Universal_Type (Opnd)
15468 and then Comes_From_Source (Typ)
15469 and then not Is_Hidden (Typ)
15470 then
15471 -- The type of the actual may be a global reference. Save this
15472 -- information by creating a reference to it.
15473
15474 if Is_Global (Typ) then
15475 Mark := New_Occurrence_Of (Typ, Loc);
15476
15477 -- Otherwise rely on resolution to find the proper type within
15478 -- the instance.
15479
15480 else
15481 Mark := Qualify_Type (Loc, Typ);
15482 end if;
15483
15484 Qual :=
15485 Make_Qualified_Expression (Loc,
15486 Subtype_Mark => Mark,
15487 Expression => Relocate_Node (Opnd));
15488
15489 -- Mark the qualification to distinguish it from other source
15490 -- constructs and signal the instantiation mechanism that this
15491 -- node requires special processing. See Copy_Generic_Node for
15492 -- details.
15493
15494 Set_Is_Qualified_Universal_Literal (Qual);
15495
15496 Rewrite (Opnd, Qual);
15497 end if;
15498 end Qualify_Operand;
15499
15500 ------------------
15501 -- Qualify_Type --
15502 ------------------
15503
15504 function Qualify_Type
15505 (Loc : Source_Ptr;
15506 Typ : Entity_Id) return Node_Id
15507 is
15508 Scop : constant Entity_Id := Scope (Typ);
15509 Result : Node_Id;
15510
15511 begin
15512 Result := Make_Identifier (Loc, Chars (Typ));
15513
15514 if Present (Scop) and then not Is_Generic_Unit (Scop) then
15515 Result :=
15516 Make_Selected_Component (Loc,
15517 Prefix => Make_Identifier (Loc, Chars (Scop)),
15518 Selector_Name => Result);
15519 end if;
15520
15521 return Result;
15522 end Qualify_Type;
15523
15524 -- Local variables
15525
15526 Actuals : constant List_Id := Parameter_Associations (Func_Call);
15527
15528 -- Start of processing for Qualify_Universal_Operands
15529
15530 begin
15531 if Nkind (Op) in N_Binary_Op then
15532 Qualify_Operand (Left_Opnd (Op), First (Actuals));
15533 Qualify_Operand (Right_Opnd (Op), Next (First (Actuals)));
15534
15535 elsif Nkind (Op) in N_Unary_Op then
15536 Qualify_Operand (Right_Opnd (Op), First (Actuals));
15537 end if;
15538 end Qualify_Universal_Operands;
15539
15540 ------------------
15541 -- Reset_Entity --
15542 ------------------
15543
15544 procedure Reset_Entity (N : Node_Id) is
15545 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id);
15546 -- If the type of N2 is global to the generic unit, save the type in
15547 -- the generic node. Just as we perform name capture for explicit
15548 -- references within the generic, we must capture the global types
15549 -- of local entities because they may participate in resolution in
15550 -- the instance.
15551
15552 function Top_Ancestor (E : Entity_Id) return Entity_Id;
15553 -- Find the ultimate ancestor of the current unit. If it is not a
15554 -- generic unit, then the name of the current unit in the prefix of
15555 -- an expanded name must be replaced with its generic homonym to
15556 -- ensure that it will be properly resolved in an instance.
15557
15558 ---------------------
15559 -- Set_Global_Type --
15560 ---------------------
15561
15562 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id) is
15563 Typ : constant Entity_Id := Etype (N2);
15564
15565 begin
15566 Set_Etype (N, Typ);
15567
15568 -- If the entity of N is not the associated node, this is a
15569 -- nested generic and it has an associated node as well, whose
15570 -- type is already the full view (see below). Indicate that the
15571 -- original node has a private view.
15572
15573 if Entity (N) /= N2 and then Has_Private_View (Entity (N)) then
15574 Set_Has_Private_View (N);
15575 end if;
15576
15577 -- If not a private type, nothing else to do
15578
15579 if not Is_Private_Type (Typ) then
15580 null;
15581
15582 -- If it is a derivation of a private type in a context where no
15583 -- full view is needed, nothing to do either.
15584
15585 elsif No (Full_View (Typ)) and then Typ /= Etype (Typ) then
15586 null;
15587
15588 -- Otherwise mark the type for flipping and use the full view when
15589 -- available.
15590
15591 else
15592 Set_Has_Private_View (N);
15593
15594 if Present (Full_View (Typ)) then
15595 Set_Etype (N2, Full_View (Typ));
15596 end if;
15597 end if;
15598
15599 if Is_Floating_Point_Type (Typ)
15600 and then Has_Dimension_System (Typ)
15601 then
15602 Copy_Dimensions (N2, N);
15603 end if;
15604 end Set_Global_Type;
15605
15606 ------------------
15607 -- Top_Ancestor --
15608 ------------------
15609
15610 function Top_Ancestor (E : Entity_Id) return Entity_Id is
15611 Par : Entity_Id;
15612
15613 begin
15614 Par := E;
15615 while Is_Child_Unit (Par) loop
15616 Par := Scope (Par);
15617 end loop;
15618
15619 return Par;
15620 end Top_Ancestor;
15621
15622 -- Start of processing for Reset_Entity
15623
15624 begin
15625 N2 := Get_Associated_Node (N);
15626 E := Entity (N2);
15627
15628 if Present (E) then
15629
15630 -- If the node is an entry call to an entry in an enclosing task,
15631 -- it is rewritten as a selected component. No global entity to
15632 -- preserve in this case, since the expansion will be redone in
15633 -- the instance.
15634
15635 if not Nkind_In (E, N_Defining_Character_Literal,
15636 N_Defining_Identifier,
15637 N_Defining_Operator_Symbol)
15638 then
15639 Set_Associated_Node (N, Empty);
15640 Set_Etype (N, Empty);
15641 return;
15642 end if;
15643
15644 -- If the entity is an itype created as a subtype of an access
15645 -- type with a null exclusion restore source entity for proper
15646 -- visibility. The itype will be created anew in the instance.
15647
15648 if Is_Itype (E)
15649 and then Ekind (E) = E_Access_Subtype
15650 and then Is_Entity_Name (N)
15651 and then Chars (Etype (E)) = Chars (N)
15652 then
15653 E := Etype (E);
15654 Set_Entity (N2, E);
15655 Set_Etype (N2, E);
15656 end if;
15657
15658 if Is_Global (E) then
15659 Set_Global_Type (N, N2);
15660
15661 elsif Nkind (N) = N_Op_Concat
15662 and then Is_Generic_Type (Etype (N2))
15663 and then (Base_Type (Etype (Right_Opnd (N2))) = Etype (N2)
15664 or else
15665 Base_Type (Etype (Left_Opnd (N2))) = Etype (N2))
15666 and then Is_Intrinsic_Subprogram (E)
15667 then
15668 null;
15669
15670 -- Entity is local. Mark generic node as unresolved. Note that now
15671 -- it does not have an entity.
15672
15673 else
15674 Set_Associated_Node (N, Empty);
15675 Set_Etype (N, Empty);
15676 end if;
15677
15678 if Nkind (Parent (N)) in N_Generic_Instantiation
15679 and then N = Name (Parent (N))
15680 then
15681 Save_Global_Defaults (Parent (N), Parent (N2));
15682 end if;
15683
15684 elsif Nkind (Parent (N)) = N_Selected_Component
15685 and then Nkind (Parent (N2)) = N_Expanded_Name
15686 then
15687 if Is_Global (Entity (Parent (N2))) then
15688 Change_Selected_Component_To_Expanded_Name (Parent (N));
15689 Set_Associated_Node (Parent (N), Parent (N2));
15690 Set_Global_Type (Parent (N), Parent (N2));
15691 Save_Entity_Descendants (N);
15692
15693 -- If this is a reference to the current generic entity, replace
15694 -- by the name of the generic homonym of the current package. This
15695 -- is because in an instantiation Par.P.Q will not resolve to the
15696 -- name of the instance, whose enclosing scope is not necessarily
15697 -- Par. We use the generic homonym rather that the name of the
15698 -- generic itself because it may be hidden by a local declaration.
15699
15700 elsif In_Open_Scopes (Entity (Parent (N2)))
15701 and then not
15702 Is_Generic_Unit (Top_Ancestor (Entity (Prefix (Parent (N2)))))
15703 then
15704 if Ekind (Entity (Parent (N2))) = E_Generic_Package then
15705 Rewrite (Parent (N),
15706 Make_Identifier (Sloc (N),
15707 Chars =>
15708 Chars (Generic_Homonym (Entity (Parent (N2))))));
15709 else
15710 Rewrite (Parent (N),
15711 Make_Identifier (Sloc (N),
15712 Chars => Chars (Selector_Name (Parent (N2)))));
15713 end if;
15714 end if;
15715
15716 if Nkind (Parent (Parent (N))) in N_Generic_Instantiation
15717 and then Parent (N) = Name (Parent (Parent (N)))
15718 then
15719 Save_Global_Defaults
15720 (Parent (Parent (N)), Parent (Parent (N2)));
15721 end if;
15722
15723 -- A selected component may denote a static constant that has been
15724 -- folded. If the static constant is global to the generic, capture
15725 -- its value. Otherwise the folding will happen in any instantiation.
15726
15727 elsif Nkind (Parent (N)) = N_Selected_Component
15728 and then Nkind_In (Parent (N2), N_Integer_Literal, N_Real_Literal)
15729 then
15730 if Present (Entity (Original_Node (Parent (N2))))
15731 and then Is_Global (Entity (Original_Node (Parent (N2))))
15732 then
15733 Rewrite (Parent (N), New_Copy (Parent (N2)));
15734 Set_Analyzed (Parent (N), False);
15735 end if;
15736
15737 -- A selected component may be transformed into a parameterless
15738 -- function call. If the called entity is global, rewrite the node
15739 -- appropriately, i.e. as an extended name for the global entity.
15740
15741 elsif Nkind (Parent (N)) = N_Selected_Component
15742 and then Nkind (Parent (N2)) = N_Function_Call
15743 and then N = Selector_Name (Parent (N))
15744 then
15745 if No (Parameter_Associations (Parent (N2))) then
15746 if Is_Global (Entity (Name (Parent (N2)))) then
15747 Change_Selected_Component_To_Expanded_Name (Parent (N));
15748 Set_Associated_Node (Parent (N), Name (Parent (N2)));
15749 Set_Global_Type (Parent (N), Name (Parent (N2)));
15750 Save_Entity_Descendants (N);
15751
15752 else
15753 Set_Is_Prefixed_Call (Parent (N));
15754 Set_Associated_Node (N, Empty);
15755 Set_Etype (N, Empty);
15756 end if;
15757
15758 -- In Ada 2005, X.F may be a call to a primitive operation,
15759 -- rewritten as F (X). This rewriting will be done again in an
15760 -- instance, so keep the original node. Global entities will be
15761 -- captured as for other constructs. Indicate that this must
15762 -- resolve as a call, to prevent accidental overloading in the
15763 -- instance, if both a component and a primitive operation appear
15764 -- as candidates.
15765
15766 else
15767 Set_Is_Prefixed_Call (Parent (N));
15768 end if;
15769
15770 -- Entity is local. Reset in generic unit, so that node is resolved
15771 -- anew at the point of instantiation.
15772
15773 else
15774 Set_Associated_Node (N, Empty);
15775 Set_Etype (N, Empty);
15776 end if;
15777 end Reset_Entity;
15778
15779 -----------------------------
15780 -- Save_Entity_Descendants --
15781 -----------------------------
15782
15783 procedure Save_Entity_Descendants (N : Node_Id) is
15784 begin
15785 case Nkind (N) is
15786 when N_Binary_Op =>
15787 Save_Global_Descendant (Union_Id (Left_Opnd (N)));
15788 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
15789
15790 when N_Unary_Op =>
15791 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
15792
15793 when N_Expanded_Name
15794 | N_Selected_Component
15795 =>
15796 Save_Global_Descendant (Union_Id (Prefix (N)));
15797 Save_Global_Descendant (Union_Id (Selector_Name (N)));
15798
15799 when N_Character_Literal
15800 | N_Identifier
15801 | N_Operator_Symbol
15802 =>
15803 null;
15804
15805 when others =>
15806 raise Program_Error;
15807 end case;
15808 end Save_Entity_Descendants;
15809
15810 --------------------------
15811 -- Save_Global_Defaults --
15812 --------------------------
15813
15814 procedure Save_Global_Defaults (N1 : Node_Id; N2 : Node_Id) is
15815 Loc : constant Source_Ptr := Sloc (N1);
15816 Assoc2 : constant List_Id := Generic_Associations (N2);
15817 Gen_Id : constant Entity_Id := Get_Generic_Entity (N2);
15818 Assoc1 : List_Id;
15819 Act1 : Node_Id;
15820 Act2 : Node_Id;
15821 Def : Node_Id;
15822 Ndec : Node_Id;
15823 Subp : Entity_Id;
15824 Actual : Entity_Id;
15825
15826 begin
15827 Assoc1 := Generic_Associations (N1);
15828
15829 if Present (Assoc1) then
15830 Act1 := First (Assoc1);
15831 else
15832 Act1 := Empty;
15833 Set_Generic_Associations (N1, New_List);
15834 Assoc1 := Generic_Associations (N1);
15835 end if;
15836
15837 if Present (Assoc2) then
15838 Act2 := First (Assoc2);
15839 else
15840 return;
15841 end if;
15842
15843 while Present (Act1) and then Present (Act2) loop
15844 Next (Act1);
15845 Next (Act2);
15846 end loop;
15847
15848 -- Find the associations added for default subprograms
15849
15850 if Present (Act2) then
15851 while Nkind (Act2) /= N_Generic_Association
15852 or else No (Entity (Selector_Name (Act2)))
15853 or else not Is_Overloadable (Entity (Selector_Name (Act2)))
15854 loop
15855 Next (Act2);
15856 end loop;
15857
15858 -- Add a similar association if the default is global. The
15859 -- renaming declaration for the actual has been analyzed, and
15860 -- its alias is the program it renames. Link the actual in the
15861 -- original generic tree with the node in the analyzed tree.
15862
15863 while Present (Act2) loop
15864 Subp := Entity (Selector_Name (Act2));
15865 Def := Explicit_Generic_Actual_Parameter (Act2);
15866
15867 -- Following test is defence against rubbish errors
15868
15869 if No (Alias (Subp)) then
15870 return;
15871 end if;
15872
15873 -- Retrieve the resolved actual from the renaming declaration
15874 -- created for the instantiated formal.
15875
15876 Actual := Entity (Name (Parent (Parent (Subp))));
15877 Set_Entity (Def, Actual);
15878 Set_Etype (Def, Etype (Actual));
15879
15880 if Is_Global (Actual) then
15881 Ndec :=
15882 Make_Generic_Association (Loc,
15883 Selector_Name =>
15884 New_Occurrence_Of (Subp, Loc),
15885 Explicit_Generic_Actual_Parameter =>
15886 New_Occurrence_Of (Actual, Loc));
15887
15888 Set_Associated_Node
15889 (Explicit_Generic_Actual_Parameter (Ndec), Def);
15890
15891 Append (Ndec, Assoc1);
15892
15893 -- If there are other defaults, add a dummy association in case
15894 -- there are other defaulted formals with the same name.
15895
15896 elsif Present (Next (Act2)) then
15897 Ndec :=
15898 Make_Generic_Association (Loc,
15899 Selector_Name =>
15900 New_Occurrence_Of (Subp, Loc),
15901 Explicit_Generic_Actual_Parameter => Empty);
15902
15903 Append (Ndec, Assoc1);
15904 end if;
15905
15906 Next (Act2);
15907 end loop;
15908 end if;
15909
15910 if Nkind (Name (N1)) = N_Identifier
15911 and then Is_Child_Unit (Gen_Id)
15912 and then Is_Global (Gen_Id)
15913 and then Is_Generic_Unit (Scope (Gen_Id))
15914 and then In_Open_Scopes (Scope (Gen_Id))
15915 then
15916 -- This is an instantiation of a child unit within a sibling, so
15917 -- that the generic parent is in scope. An eventual instance must
15918 -- occur within the scope of an instance of the parent. Make name
15919 -- in instance into an expanded name, to preserve the identifier
15920 -- of the parent, so it can be resolved subsequently.
15921
15922 Rewrite (Name (N2),
15923 Make_Expanded_Name (Loc,
15924 Chars => Chars (Gen_Id),
15925 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
15926 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
15927 Set_Entity (Name (N2), Gen_Id);
15928
15929 Rewrite (Name (N1),
15930 Make_Expanded_Name (Loc,
15931 Chars => Chars (Gen_Id),
15932 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
15933 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
15934
15935 Set_Associated_Node (Name (N1), Name (N2));
15936 Set_Associated_Node (Prefix (Name (N1)), Empty);
15937 Set_Associated_Node
15938 (Selector_Name (Name (N1)), Selector_Name (Name (N2)));
15939 Set_Etype (Name (N1), Etype (Gen_Id));
15940 end if;
15941 end Save_Global_Defaults;
15942
15943 ----------------------------
15944 -- Save_Global_Descendant --
15945 ----------------------------
15946
15947 procedure Save_Global_Descendant (D : Union_Id) is
15948 N1 : Node_Id;
15949
15950 begin
15951 if D in Node_Range then
15952 if D = Union_Id (Empty) then
15953 null;
15954
15955 elsif Nkind (Node_Id (D)) /= N_Compilation_Unit then
15956 Save_References (Node_Id (D));
15957 end if;
15958
15959 elsif D in List_Range then
15960 pragma Assert (D /= Union_Id (No_List));
15961 -- Because No_List = Empty, which is in Node_Range above
15962
15963 if Is_Empty_List (List_Id (D)) then
15964 null;
15965
15966 else
15967 N1 := First (List_Id (D));
15968 while Present (N1) loop
15969 Save_References (N1);
15970 Next (N1);
15971 end loop;
15972 end if;
15973
15974 -- Element list or other non-node field, nothing to do
15975
15976 else
15977 null;
15978 end if;
15979 end Save_Global_Descendant;
15980
15981 ---------------------
15982 -- Save_References --
15983 ---------------------
15984
15985 -- This is the recursive procedure that does the work once the enclosing
15986 -- generic scope has been established. We have to treat specially a
15987 -- number of node rewritings that are required by semantic processing
15988 -- and which change the kind of nodes in the generic copy: typically
15989 -- constant-folding, replacing an operator node by a string literal, or
15990 -- a selected component by an expanded name. In each of those cases, the
15991 -- transformation is propagated to the generic unit.
15992
15993 procedure Save_References (N : Node_Id) is
15994 Loc : constant Source_Ptr := Sloc (N);
15995
15996 function Requires_Delayed_Save (Nod : Node_Id) return Boolean;
15997 -- Determine whether arbitrary node Nod requires delayed capture of
15998 -- global references within its aspect specifications.
15999
16000 procedure Save_References_In_Aggregate (N : Node_Id);
16001 -- Save all global references in [extension] aggregate node N
16002
16003 procedure Save_References_In_Char_Lit_Or_Op_Symbol (N : Node_Id);
16004 -- Save all global references in a character literal or operator
16005 -- symbol denoted by N.
16006
16007 procedure Save_References_In_Descendants (N : Node_Id);
16008 -- Save all global references in all descendants of node N
16009
16010 procedure Save_References_In_Identifier (N : Node_Id);
16011 -- Save all global references in identifier node N
16012
16013 procedure Save_References_In_Operator (N : Node_Id);
16014 -- Save all global references in operator node N
16015
16016 procedure Save_References_In_Pragma (Prag : Node_Id);
16017 -- Save all global references found within the expression of pragma
16018 -- Prag.
16019
16020 ---------------------------
16021 -- Requires_Delayed_Save --
16022 ---------------------------
16023
16024 function Requires_Delayed_Save (Nod : Node_Id) return Boolean is
16025 begin
16026 -- Generic packages and subprograms require delayed capture of
16027 -- global references within their aspects due to the timing of
16028 -- annotation analysis.
16029
16030 if Nkind_In (Nod, N_Generic_Package_Declaration,
16031 N_Generic_Subprogram_Declaration,
16032 N_Package_Body,
16033 N_Package_Body_Stub,
16034 N_Subprogram_Body,
16035 N_Subprogram_Body_Stub)
16036 then
16037 -- Since the capture of global references is done on the
16038 -- unanalyzed generic template, there is no information around
16039 -- to infer the context. Use the Associated_Entity linkages to
16040 -- peek into the analyzed generic copy and determine what the
16041 -- template corresponds to.
16042
16043 if Nod = Templ then
16044 return
16045 Is_Generic_Declaration_Or_Body
16046 (Unit_Declaration_Node
16047 (Associated_Entity (Defining_Entity (Nod))));
16048
16049 -- Otherwise the generic unit being processed is not the top
16050 -- level template. It is safe to capture of global references
16051 -- within the generic unit because at this point the top level
16052 -- copy is fully analyzed.
16053
16054 else
16055 return False;
16056 end if;
16057
16058 -- Otherwise capture the global references without interference
16059
16060 else
16061 return False;
16062 end if;
16063 end Requires_Delayed_Save;
16064
16065 ----------------------------------
16066 -- Save_References_In_Aggregate --
16067 ----------------------------------
16068
16069 procedure Save_References_In_Aggregate (N : Node_Id) is
16070 Nam : Node_Id;
16071 Qual : Node_Id := Empty;
16072 Typ : Entity_Id := Empty;
16073
16074 use Atree.Unchecked_Access;
16075 -- This code section is part of implementing an untyped tree
16076 -- traversal, so it needs direct access to node fields.
16077
16078 begin
16079 N2 := Get_Associated_Node (N);
16080
16081 if Present (N2) then
16082 Typ := Etype (N2);
16083
16084 -- In an instance within a generic, use the name of the actual
16085 -- and not the original generic parameter. If the actual is
16086 -- global in the current generic it must be preserved for its
16087 -- instantiation.
16088
16089 if Nkind (Parent (Typ)) = N_Subtype_Declaration
16090 and then Present (Generic_Parent_Type (Parent (Typ)))
16091 then
16092 Typ := Base_Type (Typ);
16093 Set_Etype (N2, Typ);
16094 end if;
16095 end if;
16096
16097 if No (N2) or else No (Typ) or else not Is_Global (Typ) then
16098 Set_Associated_Node (N, Empty);
16099
16100 -- If the aggregate is an actual in a call, it has been
16101 -- resolved in the current context, to some local type. The
16102 -- enclosing call may have been disambiguated by the aggregate,
16103 -- and this disambiguation might fail at instantiation time
16104 -- because the type to which the aggregate did resolve is not
16105 -- preserved. In order to preserve some of this information,
16106 -- wrap the aggregate in a qualified expression, using the id
16107 -- of its type. For further disambiguation we qualify the type
16108 -- name with its scope (if visible and not hidden by a local
16109 -- homograph) because both id's will have corresponding
16110 -- entities in an instance. This resolves most of the problems
16111 -- with missing type information on aggregates in instances.
16112
16113 if Present (N2)
16114 and then Nkind (N2) = Nkind (N)
16115 and then Nkind (Parent (N2)) in N_Subprogram_Call
16116 and then Present (Typ)
16117 and then Comes_From_Source (Typ)
16118 then
16119 Nam := Make_Identifier (Loc, Chars (Typ));
16120
16121 if Is_Immediately_Visible (Scope (Typ))
16122 and then
16123 (not In_Open_Scopes (Scope (Typ))
16124 or else Current_Entity (Scope (Typ)) = Scope (Typ))
16125 then
16126 Nam :=
16127 Make_Selected_Component (Loc,
16128 Prefix =>
16129 Make_Identifier (Loc, Chars (Scope (Typ))),
16130 Selector_Name => Nam);
16131 end if;
16132
16133 Qual :=
16134 Make_Qualified_Expression (Loc,
16135 Subtype_Mark => Nam,
16136 Expression => Relocate_Node (N));
16137 end if;
16138 end if;
16139
16140 Save_Global_Descendant (Field1 (N));
16141 Save_Global_Descendant (Field2 (N));
16142 Save_Global_Descendant (Field3 (N));
16143 Save_Global_Descendant (Field5 (N));
16144
16145 if Present (Qual) then
16146 Rewrite (N, Qual);
16147 end if;
16148 end Save_References_In_Aggregate;
16149
16150 ----------------------------------------------
16151 -- Save_References_In_Char_Lit_Or_Op_Symbol --
16152 ----------------------------------------------
16153
16154 procedure Save_References_In_Char_Lit_Or_Op_Symbol (N : Node_Id) is
16155 begin
16156 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
16157 Reset_Entity (N);
16158
16159 elsif Nkind (N) = N_Operator_Symbol
16160 and then Nkind (Get_Associated_Node (N)) = N_String_Literal
16161 then
16162 Change_Operator_Symbol_To_String_Literal (N);
16163 end if;
16164 end Save_References_In_Char_Lit_Or_Op_Symbol;
16165
16166 ------------------------------------
16167 -- Save_References_In_Descendants --
16168 ------------------------------------
16169
16170 procedure Save_References_In_Descendants (N : Node_Id) is
16171 use Atree.Unchecked_Access;
16172 -- This code section is part of implementing an untyped tree
16173 -- traversal, so it needs direct access to node fields.
16174
16175 begin
16176 Save_Global_Descendant (Field1 (N));
16177 Save_Global_Descendant (Field2 (N));
16178 Save_Global_Descendant (Field3 (N));
16179 Save_Global_Descendant (Field4 (N));
16180 Save_Global_Descendant (Field5 (N));
16181 end Save_References_In_Descendants;
16182
16183 -----------------------------------
16184 -- Save_References_In_Identifier --
16185 -----------------------------------
16186
16187 procedure Save_References_In_Identifier (N : Node_Id) is
16188 begin
16189 -- The node did not undergo a transformation
16190
16191 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
16192 -- If this is a discriminant reference, always save it.
16193 -- It is used in the instance to find the corresponding
16194 -- discriminant positionally rather than by name.
16195
16196 Set_Original_Discriminant
16197 (N, Original_Discriminant (Get_Associated_Node (N)));
16198
16199 Reset_Entity (N);
16200
16201 -- The analysis of the generic copy transformed the identifier
16202 -- into another construct. Propagate the changes to the template.
16203
16204 else
16205 N2 := Get_Associated_Node (N);
16206
16207 -- The identifier denotes a call to a parameterless function.
16208 -- Mark the node as resolved when the function is external.
16209
16210 if Nkind (N2) = N_Function_Call then
16211 E := Entity (Name (N2));
16212
16213 if Present (E) and then Is_Global (E) then
16214 Set_Etype (N, Etype (N2));
16215 else
16216 Set_Associated_Node (N, Empty);
16217 Set_Etype (N, Empty);
16218 end if;
16219
16220 -- The identifier denotes a named number that was constant
16221 -- folded. Preserve the original name for ASIS and undo the
16222 -- constant folding which will be repeated in the instance.
16223 -- Is this still needed???
16224
16225 elsif Nkind_In (N2, N_Integer_Literal, N_Real_Literal)
16226 and then Is_Entity_Name (Original_Node (N2))
16227 then
16228 Set_Associated_Node (N, Original_Node (N2));
16229 Reset_Entity (N);
16230
16231 -- The identifier resolved to a string literal. Propagate this
16232 -- information to the generic template.
16233
16234 elsif Nkind (N2) = N_String_Literal then
16235 Rewrite (N, New_Copy (N2));
16236
16237 -- The identifier is rewritten as a dereference if it is the
16238 -- prefix of an implicit dereference. Preserve the original
16239 -- tree as the analysis of the instance will expand the node
16240 -- again, but preserve the resolved entity if it is global.
16241
16242 elsif Nkind (N2) = N_Explicit_Dereference then
16243 if Is_Entity_Name (Prefix (N2))
16244 and then Present (Entity (Prefix (N2)))
16245 and then Is_Global (Entity (Prefix (N2)))
16246 then
16247 Set_Associated_Node (N, Prefix (N2));
16248
16249 elsif Nkind (Prefix (N2)) = N_Function_Call
16250 and then Present (Entity (Name (Prefix (N2))))
16251 and then Is_Global (Entity (Name (Prefix (N2))))
16252 then
16253 Rewrite (N,
16254 Make_Explicit_Dereference (Loc,
16255 Prefix =>
16256 Make_Function_Call (Loc,
16257 Name =>
16258 New_Occurrence_Of
16259 (Entity (Name (Prefix (N2))), Loc))));
16260
16261 else
16262 Set_Associated_Node (N, Empty);
16263 Set_Etype (N, Empty);
16264 end if;
16265
16266 -- The subtype mark of a nominally unconstrained object is
16267 -- rewritten as a subtype indication using the bounds of the
16268 -- expression. Recover the original subtype mark.
16269
16270 elsif Nkind (N2) = N_Subtype_Indication
16271 and then Is_Entity_Name (Original_Node (N2))
16272 then
16273 Set_Associated_Node (N, Original_Node (N2));
16274 Reset_Entity (N);
16275 end if;
16276 end if;
16277 end Save_References_In_Identifier;
16278
16279 ---------------------------------
16280 -- Save_References_In_Operator --
16281 ---------------------------------
16282
16283 procedure Save_References_In_Operator (N : Node_Id) is
16284 begin
16285 -- The node did not undergo a transformation
16286
16287 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
16288 if Nkind (N) = N_Op_Concat then
16289 Set_Is_Component_Left_Opnd (N,
16290 Is_Component_Left_Opnd (Get_Associated_Node (N)));
16291
16292 Set_Is_Component_Right_Opnd (N,
16293 Is_Component_Right_Opnd (Get_Associated_Node (N)));
16294 end if;
16295
16296 Reset_Entity (N);
16297
16298 -- The analysis of the generic copy transformed the operator into
16299 -- some other construct. Propagate the changes to the template if
16300 -- applicable.
16301
16302 else
16303 N2 := Get_Associated_Node (N);
16304
16305 -- The operator resoved to a function call
16306
16307 if Nkind (N2) = N_Function_Call then
16308
16309 -- Add explicit qualifications in the generic template for
16310 -- all operands of universal type. This aids resolution by
16311 -- preserving the actual type of a literal or an attribute
16312 -- that yields a universal result.
16313
16314 Qualify_Universal_Operands (N, N2);
16315
16316 E := Entity (Name (N2));
16317
16318 if Present (E) and then Is_Global (E) then
16319 Set_Etype (N, Etype (N2));
16320 else
16321 Set_Associated_Node (N, Empty);
16322 Set_Etype (N, Empty);
16323 end if;
16324
16325 -- The operator was folded into a literal
16326
16327 elsif Nkind_In (N2, N_Integer_Literal,
16328 N_Real_Literal,
16329 N_String_Literal)
16330 then
16331 if Present (Original_Node (N2))
16332 and then Nkind (Original_Node (N2)) = Nkind (N)
16333 then
16334 -- Operation was constant-folded. Whenever possible,
16335 -- recover semantic information from unfolded node.
16336 -- This was initially done for ASIS but is apparently
16337 -- needed also for e.g. compiling a-nbnbin.adb.
16338
16339 Set_Associated_Node (N, Original_Node (N2));
16340
16341 if Nkind (N) = N_Op_Concat then
16342 Set_Is_Component_Left_Opnd (N,
16343 Is_Component_Left_Opnd (Get_Associated_Node (N)));
16344 Set_Is_Component_Right_Opnd (N,
16345 Is_Component_Right_Opnd (Get_Associated_Node (N)));
16346 end if;
16347
16348 Reset_Entity (N);
16349
16350 -- Propagate the constant folding back to the template
16351
16352 else
16353 Rewrite (N, New_Copy (N2));
16354 Set_Analyzed (N, False);
16355 end if;
16356
16357 -- The operator was folded into an enumeration literal. Retain
16358 -- the entity to avoid spurious ambiguities if it is overloaded
16359 -- at the point of instantiation or inlining.
16360
16361 elsif Nkind (N2) = N_Identifier
16362 and then Ekind (Entity (N2)) = E_Enumeration_Literal
16363 then
16364 Rewrite (N, New_Copy (N2));
16365 Set_Analyzed (N, False);
16366 end if;
16367 end if;
16368
16369 -- Complete the operands check if node has not been constant
16370 -- folded.
16371
16372 if Nkind (N) in N_Op then
16373 Save_Entity_Descendants (N);
16374 end if;
16375 end Save_References_In_Operator;
16376
16377 -------------------------------
16378 -- Save_References_In_Pragma --
16379 -------------------------------
16380
16381 procedure Save_References_In_Pragma (Prag : Node_Id) is
16382 Context : Node_Id;
16383 Do_Save : Boolean := True;
16384
16385 use Atree.Unchecked_Access;
16386 -- This code section is part of implementing an untyped tree
16387 -- traversal, so it needs direct access to node fields.
16388
16389 begin
16390 -- Do not save global references in pragmas generated from aspects
16391 -- because the pragmas will be regenerated at instantiation time.
16392
16393 if From_Aspect_Specification (Prag) then
16394 Do_Save := False;
16395
16396 -- The capture of global references within contract-related source
16397 -- pragmas associated with generic packages, subprograms or their
16398 -- respective bodies must be delayed due to timing of annotation
16399 -- analysis. Global references are still captured in routine
16400 -- Save_Global_References_In_Contract.
16401
16402 elsif Is_Generic_Contract_Pragma (Prag) and then Prag /= Templ then
16403 if Is_Package_Contract_Annotation (Prag) then
16404 Context := Find_Related_Package_Or_Body (Prag);
16405 else
16406 pragma Assert (Is_Subprogram_Contract_Annotation (Prag));
16407 Context := Find_Related_Declaration_Or_Body (Prag);
16408 end if;
16409
16410 -- The use of Original_Node accounts for the case when the
16411 -- related context is generic template.
16412
16413 if Requires_Delayed_Save (Original_Node (Context)) then
16414 Do_Save := False;
16415 end if;
16416 end if;
16417
16418 -- For all other cases, save all global references within the
16419 -- descendants, but skip the following semantic fields:
16420
16421 -- Field1 - Next_Pragma
16422 -- Field3 - Corresponding_Aspect
16423 -- Field5 - Next_Rep_Item
16424
16425 if Do_Save then
16426 Save_Global_Descendant (Field2 (Prag));
16427 Save_Global_Descendant (Field4 (Prag));
16428 end if;
16429 end Save_References_In_Pragma;
16430
16431 -- Start of processing for Save_References
16432
16433 begin
16434 if N = Empty then
16435 null;
16436
16437 -- Aggregates
16438
16439 elsif Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
16440 Save_References_In_Aggregate (N);
16441
16442 -- Character literals, operator symbols
16443
16444 elsif Nkind_In (N, N_Character_Literal, N_Operator_Symbol) then
16445 Save_References_In_Char_Lit_Or_Op_Symbol (N);
16446
16447 -- Defining identifiers
16448
16449 elsif Nkind (N) in N_Entity then
16450 null;
16451
16452 -- Identifiers
16453
16454 elsif Nkind (N) = N_Identifier then
16455 Save_References_In_Identifier (N);
16456
16457 -- Operators
16458
16459 elsif Nkind (N) in N_Op then
16460 Save_References_In_Operator (N);
16461
16462 -- Pragmas
16463
16464 elsif Nkind (N) = N_Pragma then
16465 Save_References_In_Pragma (N);
16466
16467 else
16468 Save_References_In_Descendants (N);
16469 end if;
16470
16471 -- Save all global references found within the aspect specifications
16472 -- of the related node.
16473
16474 if Permits_Aspect_Specifications (N) and then Has_Aspects (N) then
16475
16476 -- The capture of global references within aspects associated with
16477 -- generic packages, subprograms or their bodies must be delayed
16478 -- due to timing of annotation analysis. Global references are
16479 -- still captured in routine Save_Global_References_In_Contract.
16480
16481 if Requires_Delayed_Save (N) then
16482 null;
16483
16484 -- Otherwise save all global references within the aspects
16485
16486 else
16487 Save_Global_References_In_Aspects (N);
16488 end if;
16489 end if;
16490 end Save_References;
16491
16492 -- Start of processing for Save_Global_References
16493
16494 begin
16495 Gen_Scope := Current_Scope;
16496
16497 -- If the generic unit is a child unit, references to entities in the
16498 -- parent are treated as local, because they will be resolved anew in
16499 -- the context of the instance of the parent.
16500
16501 while Is_Child_Unit (Gen_Scope)
16502 and then Ekind (Scope (Gen_Scope)) = E_Generic_Package
16503 loop
16504 Gen_Scope := Scope (Gen_Scope);
16505 end loop;
16506
16507 Save_References (Templ);
16508 end Save_Global_References;
16509
16510 ---------------------------------------
16511 -- Save_Global_References_In_Aspects --
16512 ---------------------------------------
16513
16514 procedure Save_Global_References_In_Aspects (N : Node_Id) is
16515 Asp : Node_Id;
16516 Expr : Node_Id;
16517
16518 begin
16519 Asp := First (Aspect_Specifications (N));
16520 while Present (Asp) loop
16521 Expr := Expression (Asp);
16522
16523 if Present (Expr) then
16524 Save_Global_References (Expr);
16525 end if;
16526
16527 Next (Asp);
16528 end loop;
16529 end Save_Global_References_In_Aspects;
16530
16531 ------------------------------------------
16532 -- Set_Copied_Sloc_For_Inherited_Pragma --
16533 ------------------------------------------
16534
16535 procedure Set_Copied_Sloc_For_Inherited_Pragma
16536 (N : Node_Id;
16537 E : Entity_Id)
16538 is
16539 begin
16540 Create_Instantiation_Source (N, E,
16541 Inlined_Body => False,
16542 Inherited_Pragma => True,
16543 Factor => S_Adjustment);
16544 end Set_Copied_Sloc_For_Inherited_Pragma;
16545
16546 --------------------------------------
16547 -- Set_Copied_Sloc_For_Inlined_Body --
16548 --------------------------------------
16549
16550 procedure Set_Copied_Sloc_For_Inlined_Body (N : Node_Id; E : Entity_Id) is
16551 begin
16552 Create_Instantiation_Source (N, E,
16553 Inlined_Body => True,
16554 Inherited_Pragma => False,
16555 Factor => S_Adjustment);
16556 end Set_Copied_Sloc_For_Inlined_Body;
16557
16558 ---------------------
16559 -- Set_Instance_Of --
16560 ---------------------
16561
16562 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id) is
16563 begin
16564 Generic_Renamings.Table (Generic_Renamings.Last) := (A, B, Assoc_Null);
16565 Generic_Renamings_HTable.Set (Generic_Renamings.Last);
16566 Generic_Renamings.Increment_Last;
16567 end Set_Instance_Of;
16568
16569 --------------------
16570 -- Set_Next_Assoc --
16571 --------------------
16572
16573 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr) is
16574 begin
16575 Generic_Renamings.Table (E).Next_In_HTable := Next;
16576 end Set_Next_Assoc;
16577
16578 -------------------
16579 -- Start_Generic --
16580 -------------------
16581
16582 procedure Start_Generic is
16583 begin
16584 -- ??? More things could be factored out in this routine.
16585 -- Should probably be done at a later stage.
16586
16587 Generic_Flags.Append (Inside_A_Generic);
16588 Inside_A_Generic := True;
16589
16590 Expander_Mode_Save_And_Set (False);
16591 end Start_Generic;
16592
16593 ----------------------
16594 -- Set_Instance_Env --
16595 ----------------------
16596
16597 -- WARNING: This routine manages SPARK regions
16598
16599 procedure Set_Instance_Env
16600 (Gen_Unit : Entity_Id;
16601 Act_Unit : Entity_Id)
16602 is
16603 Saved_AE : constant Boolean := Assertions_Enabled;
16604 Saved_CPL : constant Node_Id := Check_Policy_List;
16605 Saved_DEC : constant Boolean := Dynamic_Elaboration_Checks;
16606 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
16607 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
16608
16609 begin
16610 -- Regardless of the current mode, predefined units are analyzed in the
16611 -- most current Ada mode, and earlier version Ada checks do not apply
16612 -- to predefined units. Nothing needs to be done for non-internal units.
16613 -- These are always analyzed in the current mode.
16614
16615 if In_Internal_Unit (Gen_Unit) then
16616
16617 -- The following call resets all configuration attributes to default
16618 -- or the xxx_Config versions of the attributes when the current sem
16619 -- unit is the main unit. At the same time, internal units must also
16620 -- inherit certain configuration attributes from their context. It
16621 -- is unclear what these two sets are.
16622
16623 Set_Config_Switches (True, Current_Sem_Unit = Main_Unit);
16624
16625 -- Reinstall relevant configuration attributes of the context
16626
16627 Assertions_Enabled := Saved_AE;
16628 Check_Policy_List := Saved_CPL;
16629 Dynamic_Elaboration_Checks := Saved_DEC;
16630
16631 Install_SPARK_Mode (Saved_SM, Saved_SMP);
16632 end if;
16633
16634 Current_Instantiated_Parent :=
16635 (Gen_Id => Gen_Unit,
16636 Act_Id => Act_Unit,
16637 Next_In_HTable => Assoc_Null);
16638 end Set_Instance_Env;
16639
16640 -----------------
16641 -- Switch_View --
16642 -----------------
16643
16644 procedure Switch_View (T : Entity_Id) is
16645 BT : constant Entity_Id := Base_Type (T);
16646 Priv_Elmt : Elmt_Id := No_Elmt;
16647 Priv_Sub : Entity_Id;
16648
16649 begin
16650 -- T may be private but its base type may have been exchanged through
16651 -- some other occurrence, in which case there is nothing to switch
16652 -- besides T itself. Note that a private dependent subtype of a private
16653 -- type might not have been switched even if the base type has been,
16654 -- because of the last branch of Check_Private_View (see comment there).
16655
16656 if not Is_Private_Type (BT) then
16657 Prepend_Elmt (Full_View (T), Exchanged_Views);
16658 Exchange_Declarations (T);
16659 return;
16660 end if;
16661
16662 Priv_Elmt := First_Elmt (Private_Dependents (BT));
16663
16664 if Present (Full_View (BT)) then
16665 Prepend_Elmt (Full_View (BT), Exchanged_Views);
16666 Exchange_Declarations (BT);
16667 end if;
16668
16669 while Present (Priv_Elmt) loop
16670 Priv_Sub := (Node (Priv_Elmt));
16671
16672 -- We avoid flipping the subtype if the Etype of its full view is
16673 -- private because this would result in a malformed subtype. This
16674 -- occurs when the Etype of the subtype full view is the full view of
16675 -- the base type (and since the base types were just switched, the
16676 -- subtype is pointing to the wrong view). This is currently the case
16677 -- for tagged record types, access types (maybe more?) and needs to
16678 -- be resolved. ???
16679
16680 if Present (Full_View (Priv_Sub))
16681 and then not Is_Private_Type (Etype (Full_View (Priv_Sub)))
16682 then
16683 Prepend_Elmt (Full_View (Priv_Sub), Exchanged_Views);
16684 Exchange_Declarations (Priv_Sub);
16685 end if;
16686
16687 Next_Elmt (Priv_Elmt);
16688 end loop;
16689 end Switch_View;
16690
16691 -----------------
16692 -- True_Parent --
16693 -----------------
16694
16695 function True_Parent (N : Node_Id) return Node_Id is
16696 begin
16697 if Nkind (Parent (N)) = N_Subunit then
16698 return Parent (Corresponding_Stub (Parent (N)));
16699 else
16700 return Parent (N);
16701 end if;
16702 end True_Parent;
16703
16704 -----------------------------
16705 -- Valid_Default_Attribute --
16706 -----------------------------
16707
16708 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id) is
16709 Attr_Id : constant Attribute_Id :=
16710 Get_Attribute_Id (Attribute_Name (Def));
16711 T : constant Entity_Id := Entity (Prefix (Def));
16712 Is_Fun : constant Boolean := (Ekind (Nam) = E_Function);
16713 F : Entity_Id;
16714 Num_F : Nat;
16715 OK : Boolean;
16716
16717 begin
16718 if No (T) or else T = Any_Id then
16719 return;
16720 end if;
16721
16722 Num_F := 0;
16723 F := First_Formal (Nam);
16724 while Present (F) loop
16725 Num_F := Num_F + 1;
16726 Next_Formal (F);
16727 end loop;
16728
16729 case Attr_Id is
16730 when Attribute_Adjacent
16731 | Attribute_Ceiling
16732 | Attribute_Copy_Sign
16733 | Attribute_Floor
16734 | Attribute_Fraction
16735 | Attribute_Machine
16736 | Attribute_Model
16737 | Attribute_Remainder
16738 | Attribute_Rounding
16739 | Attribute_Unbiased_Rounding
16740 =>
16741 OK := Is_Fun
16742 and then Num_F = 1
16743 and then Is_Floating_Point_Type (T);
16744
16745 when Attribute_Image
16746 | Attribute_Pred
16747 | Attribute_Succ
16748 | Attribute_Value
16749 | Attribute_Wide_Image
16750 | Attribute_Wide_Value
16751 =>
16752 OK := Is_Fun and then Num_F = 1 and then Is_Scalar_Type (T);
16753
16754 when Attribute_Max
16755 | Attribute_Min
16756 =>
16757 OK := Is_Fun and then Num_F = 2 and then Is_Scalar_Type (T);
16758
16759 when Attribute_Input =>
16760 OK := (Is_Fun and then Num_F = 1);
16761
16762 when Attribute_Output
16763 | Attribute_Put_Image
16764 | Attribute_Read
16765 | Attribute_Write
16766 =>
16767 OK := not Is_Fun and then Num_F = 2;
16768
16769 when others =>
16770 OK := False;
16771 end case;
16772
16773 if not OK then
16774 Error_Msg_N
16775 ("attribute reference has wrong profile for subprogram", Def);
16776 end if;
16777 end Valid_Default_Attribute;
16778
16779 end Sem_Ch12;
This page took 0.855043 seconds and 6 git commands to generate.