[Ada] Analyze aspects once generic subprograms are recognized
[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 Push_Scope (Id);
3864 Enter_Generic_Scope (Id);
3865 Set_Inner_Instances (Id, New_Elmt_List);
3866 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3867
3868 Analyze_Generic_Formal_Part (N);
3869
3870 if Nkind (Spec) = N_Function_Specification then
3871 Set_Ekind (Id, E_Generic_Function);
3872 else
3873 Set_Ekind (Id, E_Generic_Procedure);
3874 end if;
3875
3876 -- Analyze the aspects of the generic copy to ensure that all generated
3877 -- pragmas (if any) perform their semantic effects.
3878
3879 if Has_Aspects (N) then
3880 Analyze_Aspect_Specifications (N, Id);
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 Is_Generic_Subprogram (Renamed_Object (Gen_Unit))
5668 then
5669 Gen_Unit := Renamed_Object (Gen_Unit);
5670 Set_Is_Instantiated (Gen_Unit);
5671 Generate_Reference (Gen_Unit, N);
5672 end if;
5673
5674 if Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
5675 Error_Msg_Node_2 := Current_Scope;
5676 Error_Msg_NE
5677 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
5678 Circularity_Detected := True;
5679 Restore_Hidden_Primitives (Vis_Prims_List);
5680 goto Leave;
5681 end if;
5682
5683 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
5684
5685 -- Initialize renamings map, for error checking
5686
5687 Generic_Renamings.Set_Last (0);
5688 Generic_Renamings_HTable.Reset;
5689
5690 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
5691
5692 -- Copy original generic tree, to produce text for instantiation
5693
5694 Act_Tree :=
5695 Copy_Generic_Node
5696 (Original_Node (Gen_Decl), Empty, Instantiating => True);
5697
5698 -- Inherit overriding indicator from instance node
5699
5700 Act_Spec := Specification (Act_Tree);
5701 Set_Must_Override (Act_Spec, Must_Override (N));
5702 Set_Must_Not_Override (Act_Spec, Must_Not_Override (N));
5703
5704 Renaming_List :=
5705 Analyze_Associations
5706 (I_Node => N,
5707 Formals => Generic_Formal_Declarations (Act_Tree),
5708 F_Copy => Generic_Formal_Declarations (Gen_Decl));
5709
5710 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
5711
5712 -- The subprogram itself cannot contain a nested instance, so the
5713 -- current parent is left empty.
5714
5715 Set_Instance_Env (Gen_Unit, Empty);
5716
5717 -- Build the subprogram declaration, which does not appear in the
5718 -- generic template, and give it a sloc consistent with that of the
5719 -- template.
5720
5721 Set_Defining_Unit_Name (Act_Spec, Anon_Id);
5722 Set_Generic_Parent (Act_Spec, Gen_Unit);
5723 Act_Decl :=
5724 Make_Subprogram_Declaration (Sloc (Act_Spec),
5725 Specification => Act_Spec);
5726
5727 -- The aspects have been copied previously, but they have to be
5728 -- linked explicitly to the new subprogram declaration. Explicit
5729 -- pre/postconditions on the instance are analyzed below, in a
5730 -- separate step.
5731
5732 Move_Aspects (Act_Tree, To => Act_Decl);
5733 Set_Categorization_From_Pragmas (Act_Decl);
5734
5735 if Parent_Installed then
5736 Hide_Current_Scope;
5737 end if;
5738
5739 Append (Act_Decl, Renaming_List);
5740
5741 -- Contract-related source pragmas that follow a generic subprogram
5742 -- must be instantiated explicitly because they are not part of the
5743 -- subprogram template.
5744
5745 Instantiate_Subprogram_Contract
5746 (Original_Node (Gen_Decl), Renaming_List);
5747
5748 Build_Subprogram_Renaming;
5749
5750 -- If the context of the instance is subject to SPARK_Mode "off" or
5751 -- the annotation is altogether missing, set the global flag which
5752 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
5753 -- the instance. This should be done prior to analyzing the instance.
5754
5755 if SPARK_Mode /= On then
5756 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
5757 end if;
5758
5759 -- If the context of an instance is not subject to SPARK_Mode "off",
5760 -- and the generic spec is subject to an explicit SPARK_Mode pragma,
5761 -- the latter should be the one applicable to the instance.
5762
5763 if not Ignore_SPARK_Mode_Pragmas_In_Instance
5764 and then Saved_SM /= Off
5765 and then Present (SPARK_Pragma (Gen_Unit))
5766 then
5767 Set_SPARK_Mode (Gen_Unit);
5768 end if;
5769
5770 Analyze_Instance_And_Renamings;
5771
5772 -- Restore SPARK_Mode from the context after analysis of the package
5773 -- declaration, so that the SPARK_Mode on the generic spec does not
5774 -- apply to the pending instance for the instance body.
5775
5776 if not Ignore_SPARK_Mode_Pragmas_In_Instance
5777 and then Saved_SM /= Off
5778 and then Present (SPARK_Pragma (Gen_Unit))
5779 then
5780 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
5781 end if;
5782
5783 -- If the generic is marked Import (Intrinsic), then so is the
5784 -- instance. This indicates that there is no body to instantiate. If
5785 -- generic is marked inline, so it the instance, and the anonymous
5786 -- subprogram it renames. If inlined, or else if inlining is enabled
5787 -- for the compilation, we generate the instance body even if it is
5788 -- not within the main unit.
5789
5790 if Is_Intrinsic_Subprogram (Gen_Unit) then
5791 Set_Is_Intrinsic_Subprogram (Anon_Id);
5792 Set_Is_Intrinsic_Subprogram (Act_Decl_Id);
5793
5794 if Chars (Gen_Unit) = Name_Unchecked_Conversion then
5795 Validate_Unchecked_Conversion (N, Act_Decl_Id);
5796 end if;
5797 end if;
5798
5799 -- Inherit convention from generic unit. Intrinsic convention, as for
5800 -- an instance of unchecked conversion, is not inherited because an
5801 -- explicit Ada instance has been created.
5802
5803 if Has_Convention_Pragma (Gen_Unit)
5804 and then Convention (Gen_Unit) /= Convention_Intrinsic
5805 then
5806 Set_Convention (Act_Decl_Id, Convention (Gen_Unit));
5807 Set_Is_Exported (Act_Decl_Id, Is_Exported (Gen_Unit));
5808 end if;
5809
5810 Generate_Definition (Act_Decl_Id);
5811
5812 -- Inherit all inlining-related flags which apply to the generic in
5813 -- the subprogram and its declaration.
5814
5815 Set_Is_Inlined (Act_Decl_Id, Is_Inlined (Gen_Unit));
5816 Set_Is_Inlined (Anon_Id, Is_Inlined (Gen_Unit));
5817
5818 Set_Has_Pragma_Inline (Act_Decl_Id, Has_Pragma_Inline (Gen_Unit));
5819 Set_Has_Pragma_Inline (Anon_Id, Has_Pragma_Inline (Gen_Unit));
5820
5821 Set_Has_Pragma_Inline_Always
5822 (Act_Decl_Id, Has_Pragma_Inline_Always (Gen_Unit));
5823 Set_Has_Pragma_Inline_Always
5824 (Anon_Id, Has_Pragma_Inline_Always (Gen_Unit));
5825
5826 Set_Has_Pragma_No_Inline
5827 (Act_Decl_Id, Has_Pragma_No_Inline (Gen_Unit));
5828 Set_Has_Pragma_No_Inline
5829 (Anon_Id, Has_Pragma_No_Inline (Gen_Unit));
5830
5831 -- Propagate No_Return if pragma applied to generic unit. This must
5832 -- be done explicitly because pragma does not appear in generic
5833 -- declaration (unlike the aspect case).
5834
5835 if No_Return (Gen_Unit) then
5836 Set_No_Return (Act_Decl_Id);
5837 Set_No_Return (Anon_Id);
5838 end if;
5839
5840 -- Mark both the instance spec and the anonymous package in case the
5841 -- body is instantiated at a later pass. This preserves the original
5842 -- context in effect for the body.
5843
5844 if SPARK_Mode /= On then
5845 Set_Ignore_SPARK_Mode_Pragmas (Act_Decl_Id);
5846 Set_Ignore_SPARK_Mode_Pragmas (Anon_Id);
5847 end if;
5848
5849 if Legacy_Elaboration_Checks
5850 and then not Is_Intrinsic_Subprogram (Gen_Unit)
5851 then
5852 Check_Elab_Instantiation (N);
5853 end if;
5854
5855 -- Save the scenario for later examination by the ABE Processing
5856 -- phase.
5857
5858 Record_Elaboration_Scenario (N);
5859
5860 -- The instantiation results in a guaranteed ABE. Create a completing
5861 -- body for the subprogram declaration because the real body will not
5862 -- be instantiated.
5863
5864 if Is_Known_Guaranteed_ABE (N) then
5865 Provide_Completing_Bodies (Instance_Spec (N));
5866 end if;
5867
5868 if Is_Dispatching_Operation (Act_Decl_Id)
5869 and then Ada_Version >= Ada_2005
5870 then
5871 declare
5872 Formal : Entity_Id;
5873
5874 begin
5875 Formal := First_Formal (Act_Decl_Id);
5876 while Present (Formal) loop
5877 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type
5878 and then Is_Controlling_Formal (Formal)
5879 and then not Can_Never_Be_Null (Formal)
5880 then
5881 Error_Msg_NE
5882 ("access parameter& is controlling,", N, Formal);
5883 Error_Msg_NE
5884 ("\corresponding parameter of & must be explicitly "
5885 & "null-excluding", N, Gen_Id);
5886 end if;
5887
5888 Next_Formal (Formal);
5889 end loop;
5890 end;
5891 end if;
5892
5893 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
5894
5895 Validate_Categorization_Dependency (N, Act_Decl_Id);
5896
5897 if not Is_Intrinsic_Subprogram (Act_Decl_Id) then
5898 Inherit_Context (Gen_Decl, N);
5899
5900 Restore_Private_Views (Pack_Id, False);
5901
5902 -- If the context requires a full instantiation, mark node for
5903 -- subsequent construction of the body.
5904
5905 if Need_Subprogram_Instance_Body (N, Act_Decl_Id) then
5906 Check_Forward_Instantiation (Gen_Decl);
5907
5908 -- The wrapper package is always delayed, because it does not
5909 -- constitute a freeze point, but to insure that the freeze node
5910 -- is placed properly, it is created directly when instantiating
5911 -- the body (otherwise the freeze node might appear to early for
5912 -- nested instantiations).
5913
5914 elsif Nkind (Parent (N)) = N_Compilation_Unit then
5915 Rewrite (N, Unit (Parent (N)));
5916 Set_Unit (Parent (N), N);
5917 end if;
5918
5919 -- Replace instance node for library-level instantiations of
5920 -- intrinsic subprograms.
5921
5922 elsif Nkind (Parent (N)) = N_Compilation_Unit then
5923 Rewrite (N, Unit (Parent (N)));
5924 Set_Unit (Parent (N), N);
5925 end if;
5926
5927 if Parent_Installed then
5928 Remove_Parent;
5929 end if;
5930
5931 Restore_Hidden_Primitives (Vis_Prims_List);
5932 Restore_Env;
5933 Env_Installed := False;
5934 Generic_Renamings.Set_Last (0);
5935 Generic_Renamings_HTable.Reset;
5936 end if;
5937
5938 <<Leave>>
5939 -- Analyze aspects in declaration if no errors appear in the instance.
5940
5941 if Has_Aspects (N) and then Serious_Errors_Detected = Errs then
5942 Analyze_Aspect_Specifications (N, Act_Decl_Id);
5943 end if;
5944
5945 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
5946 Restore_Ghost_Region (Saved_GM, Saved_IGR);
5947 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
5948
5949 exception
5950 when Instantiation_Error =>
5951 if Parent_Installed then
5952 Remove_Parent;
5953 end if;
5954
5955 if Env_Installed then
5956 Restore_Env;
5957 end if;
5958
5959 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
5960 Restore_Ghost_Region (Saved_GM, Saved_IGR);
5961 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
5962 end Analyze_Subprogram_Instantiation;
5963
5964 -------------------------
5965 -- Get_Associated_Node --
5966 -------------------------
5967
5968 function Get_Associated_Node (N : Node_Id) return Node_Id is
5969 Assoc : Node_Id;
5970
5971 begin
5972 Assoc := Associated_Node (N);
5973
5974 if Nkind (Assoc) /= Nkind (N) then
5975 return Assoc;
5976
5977 elsif Nkind_In (Assoc, N_Aggregate, N_Extension_Aggregate) then
5978 return Assoc;
5979
5980 else
5981 -- If the node is part of an inner generic, it may itself have been
5982 -- remapped into a further generic copy. Associated_Node is otherwise
5983 -- used for the entity of the node, and will be of a different node
5984 -- kind, or else N has been rewritten as a literal or function call.
5985
5986 while Present (Associated_Node (Assoc))
5987 and then Nkind (Associated_Node (Assoc)) = Nkind (Assoc)
5988 loop
5989 Assoc := Associated_Node (Assoc);
5990 end loop;
5991
5992 -- Follow an additional link in case the final node was rewritten.
5993 -- This can only happen with nested generic units.
5994
5995 if (Nkind (Assoc) = N_Identifier or else Nkind (Assoc) in N_Op)
5996 and then Present (Associated_Node (Assoc))
5997 and then (Nkind_In (Associated_Node (Assoc), N_Function_Call,
5998 N_Explicit_Dereference,
5999 N_Integer_Literal,
6000 N_Real_Literal,
6001 N_String_Literal))
6002 then
6003 Assoc := Associated_Node (Assoc);
6004 end if;
6005
6006 -- An additional special case: an unconstrained type in an object
6007 -- declaration may have been rewritten as a local subtype constrained
6008 -- by the expression in the declaration. We need to recover the
6009 -- original entity, which may be global.
6010
6011 if Present (Original_Node (Assoc))
6012 and then Nkind (Parent (N)) = N_Object_Declaration
6013 then
6014 Assoc := Original_Node (Assoc);
6015 end if;
6016
6017 return Assoc;
6018 end if;
6019 end Get_Associated_Node;
6020
6021 ----------------------------
6022 -- Build_Function_Wrapper --
6023 ----------------------------
6024
6025 function Build_Function_Wrapper
6026 (Formal_Subp : Entity_Id;
6027 Actual_Subp : Entity_Id) return Node_Id
6028 is
6029 Loc : constant Source_Ptr := Sloc (Current_Scope);
6030 Ret_Type : constant Entity_Id := Get_Instance_Of (Etype (Formal_Subp));
6031 Actuals : List_Id;
6032 Decl : Node_Id;
6033 Func_Name : Node_Id;
6034 Func : Entity_Id;
6035 Parm_Type : Node_Id;
6036 Profile : List_Id := New_List;
6037 Spec : Node_Id;
6038 Act_F : Entity_Id;
6039 Form_F : Entity_Id;
6040 New_F : Entity_Id;
6041
6042 begin
6043 Func_Name := New_Occurrence_Of (Actual_Subp, Loc);
6044
6045 Func := Make_Defining_Identifier (Loc, Chars (Formal_Subp));
6046 Set_Ekind (Func, E_Function);
6047 Set_Is_Generic_Actual_Subprogram (Func);
6048
6049 Actuals := New_List;
6050 Profile := New_List;
6051
6052 Act_F := First_Formal (Actual_Subp);
6053 Form_F := First_Formal (Formal_Subp);
6054 while Present (Form_F) loop
6055
6056 -- Create new formal for profile of wrapper, and add a reference
6057 -- to it in the list of actuals for the enclosing call. The name
6058 -- must be that of the formal in the formal subprogram, because
6059 -- calls to it in the generic body may use named associations.
6060
6061 New_F := Make_Defining_Identifier (Loc, Chars (Form_F));
6062
6063 Parm_Type :=
6064 New_Occurrence_Of (Get_Instance_Of (Etype (Form_F)), Loc);
6065
6066 Append_To (Profile,
6067 Make_Parameter_Specification (Loc,
6068 Defining_Identifier => New_F,
6069 Parameter_Type => Parm_Type));
6070
6071 Append_To (Actuals, New_Occurrence_Of (New_F, Loc));
6072 Next_Formal (Form_F);
6073
6074 if Present (Act_F) then
6075 Next_Formal (Act_F);
6076 end if;
6077 end loop;
6078
6079 Spec :=
6080 Make_Function_Specification (Loc,
6081 Defining_Unit_Name => Func,
6082 Parameter_Specifications => Profile,
6083 Result_Definition => New_Occurrence_Of (Ret_Type, Loc));
6084
6085 Decl :=
6086 Make_Expression_Function (Loc,
6087 Specification => Spec,
6088 Expression =>
6089 Make_Function_Call (Loc,
6090 Name => Func_Name,
6091 Parameter_Associations => Actuals));
6092
6093 return Decl;
6094 end Build_Function_Wrapper;
6095
6096 ----------------------------
6097 -- Build_Operator_Wrapper --
6098 ----------------------------
6099
6100 function Build_Operator_Wrapper
6101 (Formal_Subp : Entity_Id;
6102 Actual_Subp : Entity_Id) return Node_Id
6103 is
6104 Loc : constant Source_Ptr := Sloc (Current_Scope);
6105 Ret_Type : constant Entity_Id :=
6106 Get_Instance_Of (Etype (Formal_Subp));
6107 Op_Type : constant Entity_Id :=
6108 Get_Instance_Of (Etype (First_Formal (Formal_Subp)));
6109 Is_Binary : constant Boolean :=
6110 Present (Next_Formal (First_Formal (Formal_Subp)));
6111
6112 Decl : Node_Id;
6113 Expr : Node_Id := Empty;
6114 F1, F2 : Entity_Id;
6115 Func : Entity_Id;
6116 Op_Name : Name_Id;
6117 Spec : Node_Id;
6118 L, R : Node_Id;
6119
6120 begin
6121 Op_Name := Chars (Actual_Subp);
6122
6123 -- Create entities for wrapper function and its formals
6124
6125 F1 := Make_Temporary (Loc, 'A');
6126 F2 := Make_Temporary (Loc, 'B');
6127 L := New_Occurrence_Of (F1, Loc);
6128 R := New_Occurrence_Of (F2, Loc);
6129
6130 Func := Make_Defining_Identifier (Loc, Chars (Formal_Subp));
6131 Set_Ekind (Func, E_Function);
6132 Set_Is_Generic_Actual_Subprogram (Func);
6133
6134 Spec :=
6135 Make_Function_Specification (Loc,
6136 Defining_Unit_Name => Func,
6137 Parameter_Specifications => New_List (
6138 Make_Parameter_Specification (Loc,
6139 Defining_Identifier => F1,
6140 Parameter_Type => New_Occurrence_Of (Op_Type, Loc))),
6141 Result_Definition => New_Occurrence_Of (Ret_Type, Loc));
6142
6143 if Is_Binary then
6144 Append_To (Parameter_Specifications (Spec),
6145 Make_Parameter_Specification (Loc,
6146 Defining_Identifier => F2,
6147 Parameter_Type => New_Occurrence_Of (Op_Type, Loc)));
6148 end if;
6149
6150 -- Build expression as a function call, or as an operator node
6151 -- that corresponds to the name of the actual, starting with
6152 -- binary operators.
6153
6154 if Op_Name not in Any_Operator_Name then
6155 Expr :=
6156 Make_Function_Call (Loc,
6157 Name =>
6158 New_Occurrence_Of (Actual_Subp, Loc),
6159 Parameter_Associations => New_List (L));
6160
6161 if Is_Binary then
6162 Append_To (Parameter_Associations (Expr), R);
6163 end if;
6164
6165 -- Binary operators
6166
6167 elsif Is_Binary then
6168 if Op_Name = Name_Op_And then
6169 Expr := Make_Op_And (Loc, Left_Opnd => L, Right_Opnd => R);
6170 elsif Op_Name = Name_Op_Or then
6171 Expr := Make_Op_Or (Loc, Left_Opnd => L, Right_Opnd => R);
6172 elsif Op_Name = Name_Op_Xor then
6173 Expr := Make_Op_Xor (Loc, Left_Opnd => L, Right_Opnd => R);
6174 elsif Op_Name = Name_Op_Eq then
6175 Expr := Make_Op_Eq (Loc, Left_Opnd => L, Right_Opnd => R);
6176 elsif Op_Name = Name_Op_Ne then
6177 Expr := Make_Op_Ne (Loc, Left_Opnd => L, Right_Opnd => R);
6178 elsif Op_Name = Name_Op_Le then
6179 Expr := Make_Op_Le (Loc, Left_Opnd => L, Right_Opnd => R);
6180 elsif Op_Name = Name_Op_Gt then
6181 Expr := Make_Op_Gt (Loc, Left_Opnd => L, Right_Opnd => R);
6182 elsif Op_Name = Name_Op_Ge then
6183 Expr := Make_Op_Ge (Loc, Left_Opnd => L, Right_Opnd => R);
6184 elsif Op_Name = Name_Op_Lt then
6185 Expr := Make_Op_Lt (Loc, Left_Opnd => L, Right_Opnd => R);
6186 elsif Op_Name = Name_Op_Add then
6187 Expr := Make_Op_Add (Loc, Left_Opnd => L, Right_Opnd => R);
6188 elsif Op_Name = Name_Op_Subtract then
6189 Expr := Make_Op_Subtract (Loc, Left_Opnd => L, Right_Opnd => R);
6190 elsif Op_Name = Name_Op_Concat then
6191 Expr := Make_Op_Concat (Loc, Left_Opnd => L, Right_Opnd => R);
6192 elsif Op_Name = Name_Op_Multiply then
6193 Expr := Make_Op_Multiply (Loc, Left_Opnd => L, Right_Opnd => R);
6194 elsif Op_Name = Name_Op_Divide then
6195 Expr := Make_Op_Divide (Loc, Left_Opnd => L, Right_Opnd => R);
6196 elsif Op_Name = Name_Op_Mod then
6197 Expr := Make_Op_Mod (Loc, Left_Opnd => L, Right_Opnd => R);
6198 elsif Op_Name = Name_Op_Rem then
6199 Expr := Make_Op_Rem (Loc, Left_Opnd => L, Right_Opnd => R);
6200 elsif Op_Name = Name_Op_Expon then
6201 Expr := Make_Op_Expon (Loc, Left_Opnd => L, Right_Opnd => R);
6202 end if;
6203
6204 -- Unary operators
6205
6206 else
6207 if Op_Name = Name_Op_Add then
6208 Expr := Make_Op_Plus (Loc, Right_Opnd => L);
6209 elsif Op_Name = Name_Op_Subtract then
6210 Expr := Make_Op_Minus (Loc, Right_Opnd => L);
6211 elsif Op_Name = Name_Op_Abs then
6212 Expr := Make_Op_Abs (Loc, Right_Opnd => L);
6213 elsif Op_Name = Name_Op_Not then
6214 Expr := Make_Op_Not (Loc, Right_Opnd => L);
6215 end if;
6216 end if;
6217
6218 Decl :=
6219 Make_Expression_Function (Loc,
6220 Specification => Spec,
6221 Expression => Expr);
6222
6223 return Decl;
6224 end Build_Operator_Wrapper;
6225
6226 -----------------------------------
6227 -- Build_Subprogram_Decl_Wrapper --
6228 -----------------------------------
6229
6230 function Build_Subprogram_Decl_Wrapper
6231 (Formal_Subp : Entity_Id) return Node_Id
6232 is
6233 Loc : constant Source_Ptr := Sloc (Current_Scope);
6234 Ret_Type : constant Entity_Id := Get_Instance_Of (Etype (Formal_Subp));
6235 Decl : Node_Id;
6236 Subp : Entity_Id;
6237 Parm_Spec : Node_Id;
6238 Profile : List_Id := New_List;
6239 Spec : Node_Id;
6240 Form_F : Entity_Id;
6241 New_F : Entity_Id;
6242
6243 begin
6244
6245 Subp := Make_Defining_Identifier (Loc, Chars (Formal_Subp));
6246 Set_Ekind (Subp, Ekind (Formal_Subp));
6247 Set_Is_Generic_Actual_Subprogram (Subp);
6248
6249 Profile := Parameter_Specifications (
6250 New_Copy_Tree
6251 (Specification (Unit_Declaration_Node (Formal_Subp))));
6252
6253 Form_F := First_Formal (Formal_Subp);
6254 Parm_Spec := First (Profile);
6255
6256 -- Create new entities for the formals. Reset entities so that
6257 -- parameter types are properly resolved when wrapper declaration
6258 -- is analyzed.
6259
6260 while Present (Parm_Spec) loop
6261 New_F := Make_Defining_Identifier (Loc, Chars (Form_F));
6262 Set_Defining_Identifier (Parm_Spec, New_F);
6263 Set_Entity (Parameter_Type (Parm_Spec), Empty);
6264 Next (Parm_Spec);
6265 Next_Formal (Form_F);
6266 end loop;
6267
6268 if Ret_Type = Standard_Void_Type then
6269 Spec :=
6270 Make_Procedure_Specification (Loc,
6271 Defining_Unit_Name => Subp,
6272 Parameter_Specifications => Profile);
6273 else
6274 Spec :=
6275 Make_Function_Specification (Loc,
6276 Defining_Unit_Name => Subp,
6277 Parameter_Specifications => Profile,
6278 Result_Definition => New_Occurrence_Of (Ret_Type, Loc));
6279 end if;
6280
6281 Decl :=
6282 Make_Subprogram_Declaration (Loc, Specification => Spec);
6283
6284 return Decl;
6285 end Build_Subprogram_Decl_Wrapper;
6286
6287 -----------------------------------
6288 -- Build_Subprogram_Body_Wrapper --
6289 -----------------------------------
6290
6291 function Build_Subprogram_Body_Wrapper
6292 (Formal_Subp : Entity_Id;
6293 Actual_Name : Node_Id) return Node_Id
6294 is
6295 Loc : constant Source_Ptr := Sloc (Current_Scope);
6296 Ret_Type : constant Entity_Id := Get_Instance_Of (Etype (Formal_Subp));
6297 Spec_Node : constant Node_Id :=
6298 Specification
6299 (Build_Subprogram_Decl_Wrapper (Formal_Subp));
6300 Act : Node_Id;
6301 Actuals : List_Id;
6302 Body_Node : Node_Id;
6303 Stmt : Node_Id;
6304 begin
6305 Actuals := New_List;
6306 Act := First (Parameter_Specifications (Spec_Node));
6307
6308 while Present (Act) loop
6309 Append_To (Actuals,
6310 Make_Identifier (Loc, Chars (Defining_Identifier (Act))));
6311 Next (Act);
6312 end loop;
6313
6314 if Ret_Type = Standard_Void_Type then
6315 Stmt := Make_Procedure_Call_Statement (Loc,
6316 Name => Actual_Name,
6317 Parameter_Associations => Actuals);
6318
6319 else
6320 Stmt := Make_Simple_Return_Statement (Loc,
6321 Expression =>
6322 Make_Function_Call (Loc,
6323 Name => Actual_Name,
6324 Parameter_Associations => Actuals));
6325 end if;
6326
6327 Body_Node := Make_Subprogram_Body (Loc,
6328 Specification => Spec_Node,
6329 Declarations => New_List,
6330 Handled_Statement_Sequence =>
6331 Make_Handled_Sequence_Of_Statements (Loc,
6332 Statements => New_List (Stmt)));
6333
6334 return Body_Node;
6335 end Build_Subprogram_Body_Wrapper;
6336
6337 -------------------------------------------
6338 -- Build_Instance_Compilation_Unit_Nodes --
6339 -------------------------------------------
6340
6341 procedure Build_Instance_Compilation_Unit_Nodes
6342 (N : Node_Id;
6343 Act_Body : Node_Id;
6344 Act_Decl : Node_Id)
6345 is
6346 Decl_Cunit : Node_Id;
6347 Body_Cunit : Node_Id;
6348 Citem : Node_Id;
6349 New_Main : constant Entity_Id := Defining_Entity (Act_Decl);
6350 Old_Main : constant Entity_Id := Cunit_Entity (Main_Unit);
6351
6352 begin
6353 -- A new compilation unit node is built for the instance declaration
6354
6355 Decl_Cunit :=
6356 Make_Compilation_Unit (Sloc (N),
6357 Context_Items => Empty_List,
6358 Unit => Act_Decl,
6359 Aux_Decls_Node => Make_Compilation_Unit_Aux (Sloc (N)));
6360
6361 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
6362
6363 -- The new compilation unit is linked to its body, but both share the
6364 -- same file, so we do not set Body_Required on the new unit so as not
6365 -- to create a spurious dependency on a non-existent body in the ali.
6366 -- This simplifies CodePeer unit traversal.
6367
6368 -- We use the original instantiation compilation unit as the resulting
6369 -- compilation unit of the instance, since this is the main unit.
6370
6371 Rewrite (N, Act_Body);
6372
6373 -- Propagate the aspect specifications from the package body template to
6374 -- the instantiated version of the package body.
6375
6376 if Has_Aspects (Act_Body) then
6377 Set_Aspect_Specifications
6378 (N, New_Copy_List_Tree (Aspect_Specifications (Act_Body)));
6379 end if;
6380
6381 Body_Cunit := Parent (N);
6382
6383 -- The two compilation unit nodes are linked by the Library_Unit field
6384
6385 Set_Library_Unit (Decl_Cunit, Body_Cunit);
6386 Set_Library_Unit (Body_Cunit, Decl_Cunit);
6387
6388 -- Preserve the private nature of the package if needed
6389
6390 Set_Private_Present (Decl_Cunit, Private_Present (Body_Cunit));
6391
6392 -- If the instance is not the main unit, its context, categorization
6393 -- and elaboration entity are not relevant to the compilation.
6394
6395 if Body_Cunit /= Cunit (Main_Unit) then
6396 Make_Instance_Unit (Body_Cunit, In_Main => False);
6397 return;
6398 end if;
6399
6400 -- The context clause items on the instantiation, which are now attached
6401 -- to the body compilation unit (since the body overwrote the original
6402 -- instantiation node), semantically belong on the spec, so copy them
6403 -- there. It's harmless to leave them on the body as well. In fact one
6404 -- could argue that they belong in both places.
6405
6406 Citem := First (Context_Items (Body_Cunit));
6407 while Present (Citem) loop
6408 Append (New_Copy (Citem), Context_Items (Decl_Cunit));
6409 Next (Citem);
6410 end loop;
6411
6412 -- Propagate categorization flags on packages, so that they appear in
6413 -- the ali file for the spec of the unit.
6414
6415 if Ekind (New_Main) = E_Package then
6416 Set_Is_Pure (Old_Main, Is_Pure (New_Main));
6417 Set_Is_Preelaborated (Old_Main, Is_Preelaborated (New_Main));
6418 Set_Is_Remote_Types (Old_Main, Is_Remote_Types (New_Main));
6419 Set_Is_Shared_Passive (Old_Main, Is_Shared_Passive (New_Main));
6420 Set_Is_Remote_Call_Interface
6421 (Old_Main, Is_Remote_Call_Interface (New_Main));
6422 end if;
6423
6424 -- Make entry in Units table, so that binder can generate call to
6425 -- elaboration procedure for body, if any.
6426
6427 Make_Instance_Unit (Body_Cunit, In_Main => True);
6428 Main_Unit_Entity := New_Main;
6429 Set_Cunit_Entity (Main_Unit, Main_Unit_Entity);
6430
6431 -- Build elaboration entity, since the instance may certainly generate
6432 -- elaboration code requiring a flag for protection.
6433
6434 Build_Elaboration_Entity (Decl_Cunit, New_Main);
6435 end Build_Instance_Compilation_Unit_Nodes;
6436
6437 -----------------------------
6438 -- Check_Access_Definition --
6439 -----------------------------
6440
6441 procedure Check_Access_Definition (N : Node_Id) is
6442 begin
6443 pragma Assert
6444 (Ada_Version >= Ada_2005 and then Present (Access_Definition (N)));
6445 null;
6446 end Check_Access_Definition;
6447
6448 -----------------------------------
6449 -- Check_Formal_Package_Instance --
6450 -----------------------------------
6451
6452 -- If the formal has specific parameters, they must match those of the
6453 -- actual. Both of them are instances, and the renaming declarations for
6454 -- their formal parameters appear in the same order in both. The analyzed
6455 -- formal has been analyzed in the context of the current instance.
6456
6457 procedure Check_Formal_Package_Instance
6458 (Formal_Pack : Entity_Id;
6459 Actual_Pack : Entity_Id)
6460 is
6461 E1 : Entity_Id := First_Entity (Actual_Pack);
6462 E2 : Entity_Id := First_Entity (Formal_Pack);
6463 Prev_E1 : Entity_Id;
6464
6465 Expr1 : Node_Id;
6466 Expr2 : Node_Id;
6467
6468 procedure Check_Mismatch (B : Boolean);
6469 -- Common error routine for mismatch between the parameters of the
6470 -- actual instance and those of the formal package.
6471
6472 function Is_Defaulted (Param : Entity_Id) return Boolean;
6473 -- If the formal package has partly box-initialized formals, skip
6474 -- conformance check for these formals. Previously the code assumed
6475 -- that box initialization for a formal package applied to all its
6476 -- formal parameters.
6477
6478 function Same_Instantiated_Constant (E1, E2 : Entity_Id) return Boolean;
6479 -- The formal may come from a nested formal package, and the actual may
6480 -- have been constant-folded. To determine whether the two denote the
6481 -- same entity we may have to traverse several definitions to recover
6482 -- the ultimate entity that they refer to.
6483
6484 function Same_Instantiated_Function (E1, E2 : Entity_Id) return Boolean;
6485 -- The formal and the actual must be identical, but if both are
6486 -- given by attributes they end up renaming different generated bodies,
6487 -- and we must verify that the attributes themselves match.
6488
6489 function Same_Instantiated_Variable (E1, E2 : Entity_Id) return Boolean;
6490 -- Similarly, if the formal comes from a nested formal package, the
6491 -- actual may designate the formal through multiple renamings, which
6492 -- have to be followed to determine the original variable in question.
6493
6494 --------------------
6495 -- Check_Mismatch --
6496 --------------------
6497
6498 procedure Check_Mismatch (B : Boolean) is
6499 -- A Formal_Type_Declaration for a derived private type is rewritten
6500 -- as a private extension decl. (see Analyze_Formal_Derived_Type),
6501 -- which is why we examine the original node.
6502
6503 Kind : constant Node_Kind := Nkind (Original_Node (Parent (E2)));
6504
6505 begin
6506 if Kind = N_Formal_Type_Declaration then
6507 return;
6508
6509 elsif Nkind_In (Kind, N_Formal_Object_Declaration,
6510 N_Formal_Package_Declaration)
6511 or else Kind in N_Formal_Subprogram_Declaration
6512 then
6513 null;
6514
6515 -- Ada 2012: If both formal and actual are incomplete types they
6516 -- are conformant.
6517
6518 elsif Is_Incomplete_Type (E1) and then Is_Incomplete_Type (E2) then
6519 null;
6520
6521 elsif B then
6522 Error_Msg_NE
6523 ("actual for & in actual instance does not match formal",
6524 Parent (Actual_Pack), E1);
6525 end if;
6526 end Check_Mismatch;
6527
6528 ------------------
6529 -- Is_Defaulted --
6530 ------------------
6531
6532 function Is_Defaulted (Param : Entity_Id) return Boolean is
6533 Assoc : Node_Id;
6534
6535 begin
6536 Assoc :=
6537 First (Generic_Associations (Parent
6538 (Associated_Formal_Package (Actual_Pack))));
6539
6540 while Present (Assoc) loop
6541 if Nkind (Assoc) = N_Others_Choice then
6542 return True;
6543
6544 elsif Nkind (Assoc) = N_Generic_Association
6545 and then Chars (Selector_Name (Assoc)) = Chars (Param)
6546 then
6547 return Box_Present (Assoc);
6548 end if;
6549
6550 Next (Assoc);
6551 end loop;
6552
6553 return False;
6554 end Is_Defaulted;
6555
6556 --------------------------------
6557 -- Same_Instantiated_Constant --
6558 --------------------------------
6559
6560 function Same_Instantiated_Constant
6561 (E1, E2 : Entity_Id) return Boolean
6562 is
6563 Ent : Entity_Id;
6564
6565 begin
6566 Ent := E2;
6567 while Present (Ent) loop
6568 if E1 = Ent then
6569 return True;
6570
6571 elsif Ekind (Ent) /= E_Constant then
6572 return False;
6573
6574 elsif Is_Entity_Name (Constant_Value (Ent)) then
6575 if Entity (Constant_Value (Ent)) = E1 then
6576 return True;
6577 else
6578 Ent := Entity (Constant_Value (Ent));
6579 end if;
6580
6581 -- The actual may be a constant that has been folded. Recover
6582 -- original name.
6583
6584 elsif Is_Entity_Name (Original_Node (Constant_Value (Ent))) then
6585 Ent := Entity (Original_Node (Constant_Value (Ent)));
6586
6587 else
6588 return False;
6589 end if;
6590 end loop;
6591
6592 return False;
6593 end Same_Instantiated_Constant;
6594
6595 --------------------------------
6596 -- Same_Instantiated_Function --
6597 --------------------------------
6598
6599 function Same_Instantiated_Function
6600 (E1, E2 : Entity_Id) return Boolean
6601 is
6602 U1, U2 : Node_Id;
6603 begin
6604 if Alias (E1) = Alias (E2) then
6605 return True;
6606
6607 elsif Present (Alias (E2)) then
6608 U1 := Original_Node (Unit_Declaration_Node (E1));
6609 U2 := Original_Node (Unit_Declaration_Node (Alias (E2)));
6610
6611 return Nkind (U1) = N_Subprogram_Renaming_Declaration
6612 and then Nkind (Name (U1)) = N_Attribute_Reference
6613
6614 and then Nkind (U2) = N_Subprogram_Renaming_Declaration
6615 and then Nkind (Name (U2)) = N_Attribute_Reference
6616
6617 and then
6618 Attribute_Name (Name (U1)) = Attribute_Name (Name (U2));
6619 else
6620 return False;
6621 end if;
6622 end Same_Instantiated_Function;
6623
6624 --------------------------------
6625 -- Same_Instantiated_Variable --
6626 --------------------------------
6627
6628 function Same_Instantiated_Variable
6629 (E1, E2 : Entity_Id) return Boolean
6630 is
6631 function Original_Entity (E : Entity_Id) return Entity_Id;
6632 -- Follow chain of renamings to the ultimate ancestor
6633
6634 ---------------------
6635 -- Original_Entity --
6636 ---------------------
6637
6638 function Original_Entity (E : Entity_Id) return Entity_Id is
6639 Orig : Entity_Id;
6640
6641 begin
6642 Orig := E;
6643 while Nkind (Parent (Orig)) = N_Object_Renaming_Declaration
6644 and then Present (Renamed_Object (Orig))
6645 and then Is_Entity_Name (Renamed_Object (Orig))
6646 loop
6647 Orig := Entity (Renamed_Object (Orig));
6648 end loop;
6649
6650 return Orig;
6651 end Original_Entity;
6652
6653 -- Start of processing for Same_Instantiated_Variable
6654
6655 begin
6656 return Ekind (E1) = Ekind (E2)
6657 and then Original_Entity (E1) = Original_Entity (E2);
6658 end Same_Instantiated_Variable;
6659
6660 -- Start of processing for Check_Formal_Package_Instance
6661
6662 begin
6663 Prev_E1 := E1;
6664 while Present (E1) and then Present (E2) loop
6665 exit when Ekind (E1) = E_Package
6666 and then Renamed_Entity (E1) = Renamed_Entity (Actual_Pack);
6667
6668 -- If the formal is the renaming of the formal package, this
6669 -- is the end of its formal part, which may occur before the
6670 -- end of the formal part in the actual in the presence of
6671 -- defaulted parameters in the formal package.
6672
6673 exit when Nkind (Parent (E2)) = N_Package_Renaming_Declaration
6674 and then Renamed_Entity (E2) = Scope (E2);
6675
6676 -- The analysis of the actual may generate additional internal
6677 -- entities. If the formal is defaulted, there is no corresponding
6678 -- analysis and the internal entities must be skipped, until we
6679 -- find corresponding entities again.
6680
6681 if Comes_From_Source (E2)
6682 and then not Comes_From_Source (E1)
6683 and then Chars (E1) /= Chars (E2)
6684 then
6685 while Present (E1) and then Chars (E1) /= Chars (E2) loop
6686 Next_Entity (E1);
6687 end loop;
6688 end if;
6689
6690 if No (E1) then
6691 return;
6692
6693 -- Entities may be declared without full declaration, such as
6694 -- itypes and predefined operators (concatenation for arrays, eg).
6695 -- Skip it and keep the formal entity to find a later match for it.
6696
6697 elsif No (Parent (E2)) and then Ekind (E1) /= Ekind (E2) then
6698 E1 := Prev_E1;
6699 goto Next_E;
6700
6701 -- If the formal entity comes from a formal declaration, it was
6702 -- defaulted in the formal package, and no check is needed on it.
6703
6704 elsif Nkind_In (Original_Node (Parent (E2)),
6705 N_Formal_Object_Declaration,
6706 N_Formal_Type_Declaration)
6707 then
6708 -- If the formal is a tagged type the corresponding class-wide
6709 -- type has been generated as well, and it must be skipped.
6710
6711 if Is_Type (E2) and then Is_Tagged_Type (E2) then
6712 Next_Entity (E2);
6713 end if;
6714
6715 goto Next_E;
6716
6717 -- Ditto for defaulted formal subprograms.
6718
6719 elsif Is_Overloadable (E1)
6720 and then Nkind (Unit_Declaration_Node (E2)) in
6721 N_Formal_Subprogram_Declaration
6722 then
6723 goto Next_E;
6724
6725 elsif Is_Defaulted (E1) then
6726 goto Next_E;
6727
6728 elsif Is_Type (E1) then
6729
6730 -- Subtypes must statically match. E1, E2 are the local entities
6731 -- that are subtypes of the actuals. Itypes generated for other
6732 -- parameters need not be checked, the check will be performed
6733 -- on the parameters themselves.
6734
6735 -- If E2 is a formal type declaration, it is a defaulted parameter
6736 -- and needs no checking.
6737
6738 if not Is_Itype (E1) and then not Is_Itype (E2) then
6739 Check_Mismatch
6740 (not Is_Type (E2)
6741 or else Etype (E1) /= Etype (E2)
6742 or else not Subtypes_Statically_Match (E1, E2));
6743 end if;
6744
6745 elsif Ekind (E1) = E_Constant then
6746
6747 -- IN parameters must denote the same static value, or the same
6748 -- constant, or the literal null.
6749
6750 Expr1 := Expression (Parent (E1));
6751
6752 if Ekind (E2) /= E_Constant then
6753 Check_Mismatch (True);
6754 goto Next_E;
6755 else
6756 Expr2 := Expression (Parent (E2));
6757 end if;
6758
6759 if Is_OK_Static_Expression (Expr1) then
6760 if not Is_OK_Static_Expression (Expr2) then
6761 Check_Mismatch (True);
6762
6763 elsif Is_Discrete_Type (Etype (E1)) then
6764 declare
6765 V1 : constant Uint := Expr_Value (Expr1);
6766 V2 : constant Uint := Expr_Value (Expr2);
6767 begin
6768 Check_Mismatch (V1 /= V2);
6769 end;
6770
6771 elsif Is_Real_Type (Etype (E1)) then
6772 declare
6773 V1 : constant Ureal := Expr_Value_R (Expr1);
6774 V2 : constant Ureal := Expr_Value_R (Expr2);
6775 begin
6776 Check_Mismatch (V1 /= V2);
6777 end;
6778
6779 elsif Is_String_Type (Etype (E1))
6780 and then Nkind (Expr1) = N_String_Literal
6781 then
6782 if Nkind (Expr2) /= N_String_Literal then
6783 Check_Mismatch (True);
6784 else
6785 Check_Mismatch
6786 (not String_Equal (Strval (Expr1), Strval (Expr2)));
6787 end if;
6788 end if;
6789
6790 elsif Is_Entity_Name (Expr1) then
6791 if Is_Entity_Name (Expr2) then
6792 if Entity (Expr1) = Entity (Expr2) then
6793 null;
6794 else
6795 Check_Mismatch
6796 (not Same_Instantiated_Constant
6797 (Entity (Expr1), Entity (Expr2)));
6798 end if;
6799
6800 else
6801 Check_Mismatch (True);
6802 end if;
6803
6804 elsif Is_Entity_Name (Original_Node (Expr1))
6805 and then Is_Entity_Name (Expr2)
6806 and then Same_Instantiated_Constant
6807 (Entity (Original_Node (Expr1)), Entity (Expr2))
6808 then
6809 null;
6810
6811 elsif Nkind (Expr1) = N_Null then
6812 Check_Mismatch (Nkind (Expr1) /= N_Null);
6813
6814 else
6815 Check_Mismatch (True);
6816 end if;
6817
6818 elsif Ekind (E1) = E_Variable then
6819 Check_Mismatch (not Same_Instantiated_Variable (E1, E2));
6820
6821 elsif Ekind (E1) = E_Package then
6822 Check_Mismatch
6823 (Ekind (E1) /= Ekind (E2)
6824 or else (Present (Renamed_Object (E2))
6825 and then Renamed_Object (E1) /=
6826 Renamed_Object (E2)));
6827
6828 elsif Is_Overloadable (E1) then
6829 -- Verify that the actual subprograms match. Note that actuals
6830 -- that are attributes are rewritten as subprograms. If the
6831 -- subprogram in the formal package is defaulted, no check is
6832 -- needed. Note that this can only happen in Ada 2005 when the
6833 -- formal package can be partially parameterized.
6834
6835 if Nkind (Unit_Declaration_Node (E1)) =
6836 N_Subprogram_Renaming_Declaration
6837 and then From_Default (Unit_Declaration_Node (E1))
6838 then
6839 null;
6840
6841 -- If the formal package has an "others" box association that
6842 -- covers this formal, there is no need for a check either.
6843
6844 elsif Nkind (Unit_Declaration_Node (E2)) in
6845 N_Formal_Subprogram_Declaration
6846 and then Box_Present (Unit_Declaration_Node (E2))
6847 then
6848 null;
6849
6850 -- No check needed if subprogram is a defaulted null procedure
6851
6852 elsif No (Alias (E2))
6853 and then Ekind (E2) = E_Procedure
6854 and then
6855 Null_Present (Specification (Unit_Declaration_Node (E2)))
6856 then
6857 null;
6858
6859 -- Otherwise the actual in the formal and the actual in the
6860 -- instantiation of the formal must match, up to renamings.
6861
6862 else
6863 Check_Mismatch
6864 (Ekind (E2) /= Ekind (E1)
6865 or else not Same_Instantiated_Function (E1, E2));
6866 end if;
6867
6868 else
6869 raise Program_Error;
6870 end if;
6871
6872 <<Next_E>>
6873 Prev_E1 := E1;
6874 Next_Entity (E1);
6875 Next_Entity (E2);
6876 end loop;
6877 end Check_Formal_Package_Instance;
6878
6879 ---------------------------
6880 -- Check_Formal_Packages --
6881 ---------------------------
6882
6883 procedure Check_Formal_Packages (P_Id : Entity_Id) is
6884 E : Entity_Id;
6885 Formal_P : Entity_Id;
6886 Formal_Decl : Node_Id;
6887 begin
6888 -- Iterate through the declarations in the instance, looking for package
6889 -- renaming declarations that denote instances of formal packages. Stop
6890 -- when we find the renaming of the current package itself. The
6891 -- declaration for a formal package without a box is followed by an
6892 -- internal entity that repeats the instantiation.
6893
6894 E := First_Entity (P_Id);
6895 while Present (E) loop
6896 if Ekind (E) = E_Package then
6897 if Renamed_Object (E) = P_Id then
6898 exit;
6899
6900 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
6901 null;
6902
6903 else
6904 Formal_Decl := Parent (Associated_Formal_Package (E));
6905
6906 -- Nothing to check if the formal has a box or an others_clause
6907 -- (necessarily with a box), or no associations altogether
6908
6909 if Box_Present (Formal_Decl)
6910 or else No (Generic_Associations (Formal_Decl))
6911 then
6912 null;
6913
6914 elsif Nkind (First (Generic_Associations (Formal_Decl))) =
6915 N_Others_Choice
6916 then
6917 -- The internal validating package was generated but formal
6918 -- and instance are known to be compatible.
6919
6920 Formal_P := Next_Entity (E);
6921 Remove (Unit_Declaration_Node (Formal_P));
6922
6923 else
6924 Formal_P := Next_Entity (E);
6925
6926 -- If the instance is within an enclosing instance body
6927 -- there is no need to verify the legality of current formal
6928 -- packages because they were legal in the generic body.
6929 -- This optimization may be applicable elsewhere, and it
6930 -- also removes spurious errors that may arise with
6931 -- on-the-fly inlining and confusion between private and
6932 -- full views.
6933
6934 if not In_Instance_Body then
6935 Check_Formal_Package_Instance (Formal_P, E);
6936 end if;
6937
6938 -- Restore the visibility of formals of the formal instance
6939 -- that are not defaulted, and are hidden within the current
6940 -- generic. These formals may be visible within an enclosing
6941 -- generic.
6942
6943 declare
6944 Elmt : Elmt_Id;
6945 begin
6946 Elmt := First_Elmt (Hidden_In_Formal_Instance (Formal_P));
6947 while Present (Elmt) loop
6948 Set_Is_Hidden (Node (Elmt), False);
6949 Next_Elmt (Elmt);
6950 end loop;
6951 end;
6952
6953 -- After checking, remove the internal validating package.
6954 -- It is only needed for semantic checks, and as it may
6955 -- contain generic formal declarations it should not reach
6956 -- gigi.
6957
6958 Remove (Unit_Declaration_Node (Formal_P));
6959 end if;
6960 end if;
6961 end if;
6962
6963 Next_Entity (E);
6964 end loop;
6965 end Check_Formal_Packages;
6966
6967 ---------------------------------
6968 -- Check_Forward_Instantiation --
6969 ---------------------------------
6970
6971 procedure Check_Forward_Instantiation (Decl : Node_Id) is
6972 S : Entity_Id;
6973 Gen_Comp : Entity_Id := Cunit_Entity (Get_Source_Unit (Decl));
6974
6975 begin
6976 -- The instantiation appears before the generic body if we are in the
6977 -- scope of the unit containing the generic, either in its spec or in
6978 -- the package body, and before the generic body.
6979
6980 if Ekind (Gen_Comp) = E_Package_Body then
6981 Gen_Comp := Spec_Entity (Gen_Comp);
6982 end if;
6983
6984 if In_Open_Scopes (Gen_Comp)
6985 and then No (Corresponding_Body (Decl))
6986 then
6987 S := Current_Scope;
6988
6989 while Present (S)
6990 and then not Is_Compilation_Unit (S)
6991 and then not Is_Child_Unit (S)
6992 loop
6993 if Ekind (S) = E_Package then
6994 Set_Has_Forward_Instantiation (S);
6995 end if;
6996
6997 S := Scope (S);
6998 end loop;
6999 end if;
7000 end Check_Forward_Instantiation;
7001
7002 ---------------------------
7003 -- Check_Generic_Actuals --
7004 ---------------------------
7005
7006 -- The visibility of the actuals may be different between the point of
7007 -- generic instantiation and the instantiation of the body.
7008
7009 procedure Check_Generic_Actuals
7010 (Instance : Entity_Id;
7011 Is_Formal_Box : Boolean)
7012 is
7013 E : Entity_Id;
7014 Astype : Entity_Id;
7015
7016 begin
7017 E := First_Entity (Instance);
7018 while Present (E) loop
7019 if Is_Type (E)
7020 and then Nkind (Parent (E)) = N_Subtype_Declaration
7021 and then Scope (Etype (E)) /= Instance
7022 and then Is_Entity_Name (Subtype_Indication (Parent (E)))
7023 then
7024 -- Restore the proper view of the actual from the information
7025 -- saved earlier by Instantiate_Type.
7026
7027 Check_Private_View (Subtype_Indication (Parent (E)));
7028
7029 -- If the actual is itself the formal of a parent instance,
7030 -- then also restore the proper view of its actual and so on.
7031 -- That's necessary for nested instantiations of the form
7032
7033 -- generic
7034 -- type Component is private;
7035 -- type Array_Type is array (Positive range <>) of Component;
7036 -- procedure Proc;
7037
7038 -- when the outermost actuals have inconsistent views, because
7039 -- the Component_Type of Array_Type of the inner instantiations
7040 -- is the actual of Component of the outermost one and not that
7041 -- of the corresponding inner instantiations.
7042
7043 Astype := Ancestor_Subtype (E);
7044 while Present (Astype)
7045 and then Nkind (Parent (Astype)) = N_Subtype_Declaration
7046 and then Present (Generic_Parent_Type (Parent (Astype)))
7047 and then Is_Entity_Name (Subtype_Indication (Parent (Astype)))
7048 loop
7049 Check_Private_View (Subtype_Indication (Parent (Astype)));
7050 Astype := Ancestor_Subtype (Astype);
7051 end loop;
7052
7053 Set_Is_Generic_Actual_Type (E);
7054
7055 if Is_Private_Type (E) and then Present (Full_View (E)) then
7056 Set_Is_Generic_Actual_Type (Full_View (E));
7057 end if;
7058
7059 Set_Is_Hidden (E, False);
7060 Set_Is_Potentially_Use_Visible (E, In_Use (Instance));
7061
7062 -- We constructed the generic actual type as a subtype of the
7063 -- supplied type. This means that it normally would not inherit
7064 -- subtype specific attributes of the actual, which is wrong for
7065 -- the generic case.
7066
7067 Astype := Ancestor_Subtype (E);
7068
7069 if No (Astype) then
7070
7071 -- This can happen when E is an itype that is the full view of
7072 -- a private type completed, e.g. with a constrained array. In
7073 -- that case, use the first subtype, which will carry size
7074 -- information. The base type itself is unconstrained and will
7075 -- not carry it.
7076
7077 Astype := First_Subtype (E);
7078 end if;
7079
7080 Set_Size_Info (E, (Astype));
7081 Set_RM_Size (E, RM_Size (Astype));
7082 Set_First_Rep_Item (E, First_Rep_Item (Astype));
7083
7084 if Is_Discrete_Or_Fixed_Point_Type (E) then
7085 Set_RM_Size (E, RM_Size (Astype));
7086 end if;
7087
7088 elsif Ekind (E) = E_Package then
7089
7090 -- If this is the renaming for the current instance, we're done.
7091 -- Otherwise it is a formal package. If the corresponding formal
7092 -- was declared with a box, the (instantiations of the) generic
7093 -- formal part are also visible. Otherwise, ignore the entity
7094 -- created to validate the actuals.
7095
7096 if Renamed_Object (E) = Instance then
7097 exit;
7098
7099 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
7100 null;
7101
7102 -- The visibility of a formal of an enclosing generic is already
7103 -- correct.
7104
7105 elsif Denotes_Formal_Package (E) then
7106 null;
7107
7108 elsif Present (Associated_Formal_Package (E))
7109 and then not Is_Generic_Formal (E)
7110 then
7111 if Box_Present (Parent (Associated_Formal_Package (E))) then
7112 Check_Generic_Actuals (Renamed_Object (E), True);
7113
7114 else
7115 Check_Generic_Actuals (Renamed_Object (E), False);
7116 end if;
7117
7118 Set_Is_Hidden (E, False);
7119 end if;
7120
7121 -- If this is a subprogram instance (in a wrapper package) the
7122 -- actual is fully visible.
7123
7124 elsif Is_Wrapper_Package (Instance) then
7125 Set_Is_Hidden (E, False);
7126
7127 -- If the formal package is declared with a box, or if the formal
7128 -- parameter is defaulted, it is visible in the body.
7129
7130 elsif Is_Formal_Box or else Is_Visible_Formal (E) then
7131 Set_Is_Hidden (E, False);
7132 end if;
7133
7134 if Ekind (E) = E_Constant then
7135
7136 -- If the type of the actual is a private type declared in the
7137 -- enclosing scope of the generic unit, the body of the generic
7138 -- sees the full view of the type (because it has to appear in
7139 -- the corresponding package body). If the type is private now,
7140 -- exchange views to restore the proper visiblity in the instance.
7141
7142 declare
7143 Typ : constant Entity_Id := Base_Type (Etype (E));
7144 -- The type of the actual
7145
7146 Gen_Id : Entity_Id;
7147 -- The generic unit
7148
7149 Parent_Scope : Entity_Id;
7150 -- The enclosing scope of the generic unit
7151
7152 begin
7153 if Is_Wrapper_Package (Instance) then
7154 Gen_Id :=
7155 Generic_Parent
7156 (Specification
7157 (Unit_Declaration_Node
7158 (Related_Instance (Instance))));
7159 else
7160 Gen_Id :=
7161 Generic_Parent (Package_Specification (Instance));
7162 end if;
7163
7164 Parent_Scope := Scope (Gen_Id);
7165
7166 -- The exchange is only needed if the generic is defined
7167 -- within a package which is not a common ancestor of the
7168 -- scope of the instance, and is not already in scope.
7169
7170 if Is_Private_Type (Typ)
7171 and then Scope (Typ) = Parent_Scope
7172 and then Scope (Instance) /= Parent_Scope
7173 and then Ekind (Parent_Scope) = E_Package
7174 and then not Is_Child_Unit (Gen_Id)
7175 then
7176 Switch_View (Typ);
7177
7178 -- If the type of the entity is a subtype, it may also have
7179 -- to be made visible, together with the base type of its
7180 -- full view, after exchange.
7181
7182 if Is_Private_Type (Etype (E)) then
7183 Switch_View (Etype (E));
7184 Switch_View (Base_Type (Etype (E)));
7185 end if;
7186 end if;
7187 end;
7188 end if;
7189
7190 Next_Entity (E);
7191 end loop;
7192 end Check_Generic_Actuals;
7193
7194 ------------------------------
7195 -- Check_Generic_Child_Unit --
7196 ------------------------------
7197
7198 procedure Check_Generic_Child_Unit
7199 (Gen_Id : Node_Id;
7200 Parent_Installed : in out Boolean)
7201 is
7202 Loc : constant Source_Ptr := Sloc (Gen_Id);
7203 Gen_Par : Entity_Id := Empty;
7204 E : Entity_Id;
7205 Inst_Par : Entity_Id;
7206 S : Node_Id;
7207
7208 function Find_Generic_Child
7209 (Scop : Entity_Id;
7210 Id : Node_Id) return Entity_Id;
7211 -- Search generic parent for possible child unit with the given name
7212
7213 function In_Enclosing_Instance return Boolean;
7214 -- Within an instance of the parent, the child unit may be denoted by
7215 -- a simple name, or an abbreviated expanded name. Examine enclosing
7216 -- scopes to locate a possible parent instantiation.
7217
7218 ------------------------
7219 -- Find_Generic_Child --
7220 ------------------------
7221
7222 function Find_Generic_Child
7223 (Scop : Entity_Id;
7224 Id : Node_Id) return Entity_Id
7225 is
7226 E : Entity_Id;
7227
7228 begin
7229 -- If entity of name is already set, instance has already been
7230 -- resolved, e.g. in an enclosing instantiation.
7231
7232 if Present (Entity (Id)) then
7233 if Scope (Entity (Id)) = Scop then
7234 return Entity (Id);
7235 else
7236 return Empty;
7237 end if;
7238
7239 else
7240 E := First_Entity (Scop);
7241 while Present (E) loop
7242 if Chars (E) = Chars (Id)
7243 and then Is_Child_Unit (E)
7244 then
7245 if Is_Child_Unit (E)
7246 and then not Is_Visible_Lib_Unit (E)
7247 then
7248 Error_Msg_NE
7249 ("generic child unit& is not visible", Gen_Id, E);
7250 end if;
7251
7252 Set_Entity (Id, E);
7253 return E;
7254 end if;
7255
7256 Next_Entity (E);
7257 end loop;
7258
7259 return Empty;
7260 end if;
7261 end Find_Generic_Child;
7262
7263 ---------------------------
7264 -- In_Enclosing_Instance --
7265 ---------------------------
7266
7267 function In_Enclosing_Instance return Boolean is
7268 Enclosing_Instance : Node_Id;
7269 Instance_Decl : Node_Id;
7270
7271 begin
7272 -- We do not inline any call that contains instantiations, except
7273 -- for instantiations of Unchecked_Conversion, so if we are within
7274 -- an inlined body the current instance does not require parents.
7275
7276 if In_Inlined_Body then
7277 pragma Assert (Chars (Gen_Id) = Name_Unchecked_Conversion);
7278 return False;
7279 end if;
7280
7281 -- Loop to check enclosing scopes
7282
7283 Enclosing_Instance := Current_Scope;
7284 while Present (Enclosing_Instance) loop
7285 Instance_Decl := Unit_Declaration_Node (Enclosing_Instance);
7286
7287 if Ekind (Enclosing_Instance) = E_Package
7288 and then Is_Generic_Instance (Enclosing_Instance)
7289 and then Present
7290 (Generic_Parent (Specification (Instance_Decl)))
7291 then
7292 -- Check whether the generic we are looking for is a child of
7293 -- this instance.
7294
7295 E := Find_Generic_Child
7296 (Generic_Parent (Specification (Instance_Decl)), Gen_Id);
7297 exit when Present (E);
7298
7299 else
7300 E := Empty;
7301 end if;
7302
7303 Enclosing_Instance := Scope (Enclosing_Instance);
7304 end loop;
7305
7306 if No (E) then
7307
7308 -- Not a child unit
7309
7310 Analyze (Gen_Id);
7311 return False;
7312
7313 else
7314 Rewrite (Gen_Id,
7315 Make_Expanded_Name (Loc,
7316 Chars => Chars (E),
7317 Prefix => New_Occurrence_Of (Enclosing_Instance, Loc),
7318 Selector_Name => New_Occurrence_Of (E, Loc)));
7319
7320 Set_Entity (Gen_Id, E);
7321 Set_Etype (Gen_Id, Etype (E));
7322 Parent_Installed := False; -- Already in scope.
7323 return True;
7324 end if;
7325 end In_Enclosing_Instance;
7326
7327 -- Start of processing for Check_Generic_Child_Unit
7328
7329 begin
7330 -- If the name of the generic is given by a selected component, it may
7331 -- be the name of a generic child unit, and the prefix is the name of an
7332 -- instance of the parent, in which case the child unit must be visible.
7333 -- If this instance is not in scope, it must be placed there and removed
7334 -- after instantiation, because what is being instantiated is not the
7335 -- original child, but the corresponding child present in the instance
7336 -- of the parent.
7337
7338 -- If the child is instantiated within the parent, it can be given by
7339 -- a simple name. In this case the instance is already in scope, but
7340 -- the child generic must be recovered from the generic parent as well.
7341
7342 if Nkind (Gen_Id) = N_Selected_Component then
7343 S := Selector_Name (Gen_Id);
7344 Analyze (Prefix (Gen_Id));
7345 Inst_Par := Entity (Prefix (Gen_Id));
7346
7347 if Ekind (Inst_Par) = E_Package
7348 and then Present (Renamed_Object (Inst_Par))
7349 then
7350 Inst_Par := Renamed_Object (Inst_Par);
7351 end if;
7352
7353 if Ekind (Inst_Par) = E_Package then
7354 if Nkind (Parent (Inst_Par)) = N_Package_Specification then
7355 Gen_Par := Generic_Parent (Parent (Inst_Par));
7356
7357 elsif Nkind (Parent (Inst_Par)) = N_Defining_Program_Unit_Name
7358 and then
7359 Nkind (Parent (Parent (Inst_Par))) = N_Package_Specification
7360 then
7361 Gen_Par := Generic_Parent (Parent (Parent (Inst_Par)));
7362 end if;
7363
7364 elsif Ekind (Inst_Par) = E_Generic_Package
7365 and then Nkind (Parent (Gen_Id)) = N_Formal_Package_Declaration
7366 then
7367 -- A formal package may be a real child package, and not the
7368 -- implicit instance within a parent. In this case the child is
7369 -- not visible and has to be retrieved explicitly as well.
7370
7371 Gen_Par := Inst_Par;
7372 end if;
7373
7374 if Present (Gen_Par) then
7375
7376 -- The prefix denotes an instantiation. The entity itself may be a
7377 -- nested generic, or a child unit.
7378
7379 E := Find_Generic_Child (Gen_Par, S);
7380
7381 if Present (E) then
7382 Change_Selected_Component_To_Expanded_Name (Gen_Id);
7383 Set_Entity (Gen_Id, E);
7384 Set_Etype (Gen_Id, Etype (E));
7385 Set_Entity (S, E);
7386 Set_Etype (S, Etype (E));
7387
7388 -- Indicate that this is a reference to the parent
7389
7390 if In_Extended_Main_Source_Unit (Gen_Id) then
7391 Set_Is_Instantiated (Inst_Par);
7392 end if;
7393
7394 -- A common mistake is to replicate the naming scheme of a
7395 -- hierarchy by instantiating a generic child directly, rather
7396 -- than the implicit child in a parent instance:
7397
7398 -- generic .. package Gpar is ..
7399 -- generic .. package Gpar.Child is ..
7400 -- package Par is new Gpar ();
7401
7402 -- with Gpar.Child;
7403 -- package Par.Child is new Gpar.Child ();
7404 -- rather than Par.Child
7405
7406 -- In this case the instantiation is within Par, which is an
7407 -- instance, but Gpar does not denote Par because we are not IN
7408 -- the instance of Gpar, so this is illegal. The test below
7409 -- recognizes this particular case.
7410
7411 if Is_Child_Unit (E)
7412 and then not Comes_From_Source (Entity (Prefix (Gen_Id)))
7413 and then (not In_Instance
7414 or else Nkind (Parent (Parent (Gen_Id))) =
7415 N_Compilation_Unit)
7416 then
7417 Error_Msg_N
7418 ("prefix of generic child unit must be instance of parent",
7419 Gen_Id);
7420 end if;
7421
7422 if not In_Open_Scopes (Inst_Par)
7423 and then Nkind (Parent (Gen_Id)) not in
7424 N_Generic_Renaming_Declaration
7425 then
7426 Install_Parent (Inst_Par);
7427 Parent_Installed := True;
7428
7429 elsif In_Open_Scopes (Inst_Par) then
7430
7431 -- If the parent is already installed, install the actuals
7432 -- for its formal packages. This is necessary when the child
7433 -- instance is a child of the parent instance: in this case,
7434 -- the parent is placed on the scope stack but the formal
7435 -- packages are not made visible.
7436
7437 Install_Formal_Packages (Inst_Par);
7438 end if;
7439
7440 else
7441 -- If the generic parent does not contain an entity that
7442 -- corresponds to the selector, the instance doesn't either.
7443 -- Analyzing the node will yield the appropriate error message.
7444 -- If the entity is not a child unit, then it is an inner
7445 -- generic in the parent.
7446
7447 Analyze (Gen_Id);
7448 end if;
7449
7450 else
7451 Analyze (Gen_Id);
7452
7453 if Is_Child_Unit (Entity (Gen_Id))
7454 and then
7455 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
7456 and then not In_Open_Scopes (Inst_Par)
7457 then
7458 Install_Parent (Inst_Par);
7459 Parent_Installed := True;
7460
7461 -- The generic unit may be the renaming of the implicit child
7462 -- present in an instance. In that case the parent instance is
7463 -- obtained from the name of the renamed entity.
7464
7465 elsif Ekind (Entity (Gen_Id)) = E_Generic_Package
7466 and then Present (Renamed_Entity (Entity (Gen_Id)))
7467 and then Is_Child_Unit (Renamed_Entity (Entity (Gen_Id)))
7468 then
7469 declare
7470 Renamed_Package : constant Node_Id :=
7471 Name (Parent (Entity (Gen_Id)));
7472 begin
7473 if Nkind (Renamed_Package) = N_Expanded_Name then
7474 Inst_Par := Entity (Prefix (Renamed_Package));
7475 Install_Parent (Inst_Par);
7476 Parent_Installed := True;
7477 end if;
7478 end;
7479 end if;
7480 end if;
7481
7482 elsif Nkind (Gen_Id) = N_Expanded_Name then
7483
7484 -- Entity already present, analyze prefix, whose meaning may be an
7485 -- instance in the current context. If it is an instance of a
7486 -- relative within another, the proper parent may still have to be
7487 -- installed, if they are not of the same generation.
7488
7489 Analyze (Prefix (Gen_Id));
7490
7491 -- Prevent cascaded errors
7492
7493 if Etype (Prefix (Gen_Id)) = Any_Type then
7494 return;
7495 end if;
7496
7497 -- In the unlikely case that a local declaration hides the name of
7498 -- the parent package, locate it on the homonym chain. If the context
7499 -- is an instance of the parent, the renaming entity is flagged as
7500 -- such.
7501
7502 Inst_Par := Entity (Prefix (Gen_Id));
7503 while Present (Inst_Par)
7504 and then not Is_Package_Or_Generic_Package (Inst_Par)
7505 loop
7506 Inst_Par := Homonym (Inst_Par);
7507 end loop;
7508
7509 pragma Assert (Present (Inst_Par));
7510 Set_Entity (Prefix (Gen_Id), Inst_Par);
7511
7512 if In_Enclosing_Instance then
7513 null;
7514
7515 elsif Present (Entity (Gen_Id))
7516 and then Is_Child_Unit (Entity (Gen_Id))
7517 and then not In_Open_Scopes (Inst_Par)
7518 then
7519 Install_Parent (Inst_Par);
7520 Parent_Installed := True;
7521 end if;
7522
7523 elsif In_Enclosing_Instance then
7524
7525 -- The child unit is found in some enclosing scope
7526
7527 null;
7528
7529 else
7530 Analyze (Gen_Id);
7531
7532 -- If this is the renaming of the implicit child in a parent
7533 -- instance, recover the parent name and install it.
7534
7535 if Is_Entity_Name (Gen_Id) then
7536 E := Entity (Gen_Id);
7537
7538 if Is_Generic_Unit (E)
7539 and then Nkind (Parent (E)) in N_Generic_Renaming_Declaration
7540 and then Is_Child_Unit (Renamed_Object (E))
7541 and then Is_Generic_Unit (Scope (Renamed_Object (E)))
7542 and then Nkind (Name (Parent (E))) = N_Expanded_Name
7543 then
7544 Rewrite (Gen_Id, New_Copy_Tree (Name (Parent (E))));
7545 Inst_Par := Entity (Prefix (Gen_Id));
7546
7547 if not In_Open_Scopes (Inst_Par) then
7548 Install_Parent (Inst_Par);
7549 Parent_Installed := True;
7550 end if;
7551
7552 -- If it is a child unit of a non-generic parent, it may be
7553 -- use-visible and given by a direct name. Install parent as
7554 -- for other cases.
7555
7556 elsif Is_Generic_Unit (E)
7557 and then Is_Child_Unit (E)
7558 and then
7559 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
7560 and then not Is_Generic_Unit (Scope (E))
7561 then
7562 if not In_Open_Scopes (Scope (E)) then
7563 Install_Parent (Scope (E));
7564 Parent_Installed := True;
7565 end if;
7566 end if;
7567 end if;
7568 end if;
7569 end Check_Generic_Child_Unit;
7570
7571 -----------------------------
7572 -- Check_Hidden_Child_Unit --
7573 -----------------------------
7574
7575 procedure Check_Hidden_Child_Unit
7576 (N : Node_Id;
7577 Gen_Unit : Entity_Id;
7578 Act_Decl_Id : Entity_Id)
7579 is
7580 Gen_Id : constant Node_Id := Name (N);
7581
7582 begin
7583 if Is_Child_Unit (Gen_Unit)
7584 and then Is_Child_Unit (Act_Decl_Id)
7585 and then Nkind (Gen_Id) = N_Expanded_Name
7586 and then Entity (Prefix (Gen_Id)) = Scope (Act_Decl_Id)
7587 and then Chars (Gen_Unit) = Chars (Act_Decl_Id)
7588 then
7589 Error_Msg_Node_2 := Scope (Act_Decl_Id);
7590 Error_Msg_NE
7591 ("generic unit & is implicitly declared in &",
7592 Defining_Unit_Name (N), Gen_Unit);
7593 Error_Msg_N ("\instance must have different name",
7594 Defining_Unit_Name (N));
7595 end if;
7596 end Check_Hidden_Child_Unit;
7597
7598 ------------------------
7599 -- Check_Private_View --
7600 ------------------------
7601
7602 procedure Check_Private_View (N : Node_Id) is
7603 T : constant Entity_Id := Etype (N);
7604 BT : Entity_Id;
7605
7606 begin
7607 -- Exchange views if the type was not private in the generic but is
7608 -- private at the point of instantiation. Do not exchange views if
7609 -- the scope of the type is in scope. This can happen if both generic
7610 -- and instance are sibling units, or if type is defined in a parent.
7611 -- In this case the visibility of the type will be correct for all
7612 -- semantic checks.
7613
7614 if Present (T) then
7615 BT := Base_Type (T);
7616
7617 if Is_Private_Type (T)
7618 and then not Has_Private_View (N)
7619 and then Present (Full_View (T))
7620 and then not In_Open_Scopes (Scope (T))
7621 then
7622 -- In the generic, the full declaration was visible
7623
7624 Switch_View (T);
7625
7626 elsif Has_Private_View (N)
7627 and then not Is_Private_Type (T)
7628 and then not Has_Been_Exchanged (T)
7629 and then (not In_Open_Scopes (Scope (T))
7630 or else Nkind (Parent (N)) = N_Subtype_Declaration)
7631 then
7632 -- In the generic, only the private declaration was visible
7633
7634 -- If the type appears in a subtype declaration, the subtype in
7635 -- instance must have a view compatible with that of its parent,
7636 -- which must be exchanged (see corresponding code in Restore_
7637 -- Private_Views) so we make an exception to the open scope rule.
7638
7639 Prepend_Elmt (T, Exchanged_Views);
7640 Exchange_Declarations (Etype (Get_Associated_Node (N)));
7641
7642 -- Finally, a non-private subtype may have a private base type, which
7643 -- must be exchanged for consistency. This can happen when a package
7644 -- body is instantiated, when the scope stack is empty but in fact
7645 -- the subtype and the base type are declared in an enclosing scope.
7646
7647 -- Note that in this case we introduce an inconsistency in the view
7648 -- set, because we switch the base type BT, but there could be some
7649 -- private dependent subtypes of BT which remain unswitched. Such
7650 -- subtypes might need to be switched at a later point (see specific
7651 -- provision for that case in Switch_View).
7652
7653 elsif not Is_Private_Type (T)
7654 and then not Has_Private_View (N)
7655 and then Is_Private_Type (BT)
7656 and then Present (Full_View (BT))
7657 and then not Is_Generic_Type (BT)
7658 and then not In_Open_Scopes (BT)
7659 then
7660 Prepend_Elmt (Full_View (BT), Exchanged_Views);
7661 Exchange_Declarations (BT);
7662 end if;
7663 end if;
7664 end Check_Private_View;
7665
7666 -----------------------------
7667 -- Check_Hidden_Primitives --
7668 -----------------------------
7669
7670 function Check_Hidden_Primitives (Assoc_List : List_Id) return Elist_Id is
7671 Actual : Node_Id;
7672 Gen_T : Entity_Id;
7673 Result : Elist_Id := No_Elist;
7674
7675 begin
7676 if No (Assoc_List) then
7677 return No_Elist;
7678 end if;
7679
7680 -- Traverse the list of associations between formals and actuals
7681 -- searching for renamings of tagged types
7682
7683 Actual := First (Assoc_List);
7684 while Present (Actual) loop
7685 if Nkind (Actual) = N_Subtype_Declaration then
7686 Gen_T := Generic_Parent_Type (Actual);
7687
7688 if Present (Gen_T) and then Is_Tagged_Type (Gen_T) then
7689
7690 -- Traverse the list of primitives of the actual types
7691 -- searching for hidden primitives that are visible in the
7692 -- corresponding generic formal; leave them visible and
7693 -- append them to Result to restore their decoration later.
7694
7695 Install_Hidden_Primitives
7696 (Prims_List => Result,
7697 Gen_T => Gen_T,
7698 Act_T => Entity (Subtype_Indication (Actual)));
7699 end if;
7700 end if;
7701
7702 Next (Actual);
7703 end loop;
7704
7705 return Result;
7706 end Check_Hidden_Primitives;
7707
7708 --------------------------
7709 -- Contains_Instance_Of --
7710 --------------------------
7711
7712 function Contains_Instance_Of
7713 (Inner : Entity_Id;
7714 Outer : Entity_Id;
7715 N : Node_Id) return Boolean
7716 is
7717 Elmt : Elmt_Id;
7718 Scop : Entity_Id;
7719
7720 begin
7721 Scop := Outer;
7722
7723 -- Verify that there are no circular instantiations. We check whether
7724 -- the unit contains an instance of the current scope or some enclosing
7725 -- scope (in case one of the instances appears in a subunit). Longer
7726 -- circularities involving subunits might seem too pathological to
7727 -- consider, but they were not too pathological for the authors of
7728 -- DEC bc30vsq, so we loop over all enclosing scopes, and mark all
7729 -- enclosing generic scopes as containing an instance.
7730
7731 loop
7732 -- Within a generic subprogram body, the scope is not generic, to
7733 -- allow for recursive subprograms. Use the declaration to determine
7734 -- whether this is a generic unit.
7735
7736 if Ekind (Scop) = E_Generic_Package
7737 or else (Is_Subprogram (Scop)
7738 and then Nkind (Unit_Declaration_Node (Scop)) =
7739 N_Generic_Subprogram_Declaration)
7740 then
7741 Elmt := First_Elmt (Inner_Instances (Inner));
7742
7743 while Present (Elmt) loop
7744 if Node (Elmt) = Scop then
7745 Error_Msg_Node_2 := Inner;
7746 Error_Msg_NE
7747 ("circular Instantiation: & instantiated within &!",
7748 N, Scop);
7749 return True;
7750
7751 elsif Node (Elmt) = Inner then
7752 return True;
7753
7754 elsif Contains_Instance_Of (Node (Elmt), Scop, N) then
7755 Error_Msg_Node_2 := Inner;
7756 Error_Msg_NE
7757 ("circular Instantiation: & instantiated within &!",
7758 N, Node (Elmt));
7759 return True;
7760 end if;
7761
7762 Next_Elmt (Elmt);
7763 end loop;
7764
7765 -- Indicate that Inner is being instantiated within Scop
7766
7767 Append_Elmt (Inner, Inner_Instances (Scop));
7768 end if;
7769
7770 if Scop = Standard_Standard then
7771 exit;
7772 else
7773 Scop := Scope (Scop);
7774 end if;
7775 end loop;
7776
7777 return False;
7778 end Contains_Instance_Of;
7779
7780 -----------------------
7781 -- Copy_Generic_Node --
7782 -----------------------
7783
7784 function Copy_Generic_Node
7785 (N : Node_Id;
7786 Parent_Id : Node_Id;
7787 Instantiating : Boolean) return Node_Id
7788 is
7789 Ent : Entity_Id;
7790 New_N : Node_Id;
7791
7792 function Copy_Generic_Descendant (D : Union_Id) return Union_Id;
7793 -- Check the given value of one of the Fields referenced by the current
7794 -- node to determine whether to copy it recursively. The field may hold
7795 -- a Node_Id, a List_Id, or an Elist_Id, or a plain value (Sloc, Uint,
7796 -- Char) in which case it need not be copied.
7797
7798 procedure Copy_Descendants;
7799 -- Common utility for various nodes
7800
7801 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id;
7802 -- Make copy of element list
7803
7804 function Copy_Generic_List
7805 (L : List_Id;
7806 Parent_Id : Node_Id) return List_Id;
7807 -- Apply Copy_Node recursively to the members of a node list
7808
7809 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean;
7810 -- True if an identifier is part of the defining program unit name of
7811 -- a child unit.
7812 -- Consider removing this subprogram now that ASIS no longer uses it.
7813
7814 ----------------------
7815 -- Copy_Descendants --
7816 ----------------------
7817
7818 procedure Copy_Descendants is
7819 use Atree.Unchecked_Access;
7820 -- This code section is part of the implementation of an untyped
7821 -- tree traversal, so it needs direct access to node fields.
7822
7823 begin
7824 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
7825 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
7826 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
7827 Set_Field4 (New_N, Copy_Generic_Descendant (Field4 (N)));
7828 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
7829 end Copy_Descendants;
7830
7831 -----------------------------
7832 -- Copy_Generic_Descendant --
7833 -----------------------------
7834
7835 function Copy_Generic_Descendant (D : Union_Id) return Union_Id is
7836 begin
7837 if D = Union_Id (Empty) then
7838 return D;
7839
7840 elsif D in Node_Range then
7841 return Union_Id
7842 (Copy_Generic_Node (Node_Id (D), New_N, Instantiating));
7843
7844 elsif D in List_Range then
7845 return Union_Id (Copy_Generic_List (List_Id (D), New_N));
7846
7847 elsif D in Elist_Range then
7848 return Union_Id (Copy_Generic_Elist (Elist_Id (D)));
7849
7850 -- Nothing else is copyable (e.g. Uint values), return as is
7851
7852 else
7853 return D;
7854 end if;
7855 end Copy_Generic_Descendant;
7856
7857 ------------------------
7858 -- Copy_Generic_Elist --
7859 ------------------------
7860
7861 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id is
7862 M : Elmt_Id;
7863 L : Elist_Id;
7864
7865 begin
7866 if Present (E) then
7867 L := New_Elmt_List;
7868 M := First_Elmt (E);
7869 while Present (M) loop
7870 Append_Elmt
7871 (Copy_Generic_Node (Node (M), Empty, Instantiating), L);
7872 Next_Elmt (M);
7873 end loop;
7874
7875 return L;
7876
7877 else
7878 return No_Elist;
7879 end if;
7880 end Copy_Generic_Elist;
7881
7882 -----------------------
7883 -- Copy_Generic_List --
7884 -----------------------
7885
7886 function Copy_Generic_List
7887 (L : List_Id;
7888 Parent_Id : Node_Id) return List_Id
7889 is
7890 N : Node_Id;
7891 New_L : List_Id;
7892
7893 begin
7894 if Present (L) then
7895 New_L := New_List;
7896 Set_Parent (New_L, Parent_Id);
7897
7898 N := First (L);
7899 while Present (N) loop
7900 Append (Copy_Generic_Node (N, Empty, Instantiating), New_L);
7901 Next (N);
7902 end loop;
7903
7904 return New_L;
7905
7906 else
7907 return No_List;
7908 end if;
7909 end Copy_Generic_List;
7910
7911 ---------------------------
7912 -- In_Defining_Unit_Name --
7913 ---------------------------
7914
7915 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean is
7916 begin
7917 return
7918 Present (Parent (Nam))
7919 and then (Nkind (Parent (Nam)) = N_Defining_Program_Unit_Name
7920 or else
7921 (Nkind (Parent (Nam)) = N_Expanded_Name
7922 and then In_Defining_Unit_Name (Parent (Nam))));
7923 end In_Defining_Unit_Name;
7924
7925 -- Start of processing for Copy_Generic_Node
7926
7927 begin
7928 if N = Empty then
7929 return N;
7930 end if;
7931
7932 New_N := New_Copy (N);
7933
7934 -- Copy aspects if present
7935
7936 if Has_Aspects (N) then
7937 Set_Has_Aspects (New_N, False);
7938 Set_Aspect_Specifications
7939 (New_N, Copy_Generic_List (Aspect_Specifications (N), Parent_Id));
7940 end if;
7941
7942 -- If we are instantiating, we want to adjust the sloc based on the
7943 -- current S_Adjustment. However, if this is the root node of a subunit,
7944 -- we need to defer that adjustment to below (see "elsif Instantiating
7945 -- and Was_Stub"), so it comes after Create_Instantiation_Source has
7946 -- computed the adjustment.
7947
7948 if Instantiating
7949 and then not (Nkind (N) in N_Proper_Body
7950 and then Was_Originally_Stub (N))
7951 then
7952 Adjust_Instantiation_Sloc (New_N, S_Adjustment);
7953 end if;
7954
7955 if not Is_List_Member (N) then
7956 Set_Parent (New_N, Parent_Id);
7957 end if;
7958
7959 -- Special casing for identifiers and other entity names and operators
7960
7961 if Nkind_In (New_N, N_Character_Literal,
7962 N_Expanded_Name,
7963 N_Identifier,
7964 N_Operator_Symbol)
7965 or else Nkind (New_N) in N_Op
7966 then
7967 if not Instantiating then
7968
7969 -- Link both nodes in order to assign subsequently the entity of
7970 -- the copy to the original node, in case this is a global
7971 -- reference.
7972
7973 Set_Associated_Node (N, New_N);
7974
7975 -- If we are within an instantiation, this is a nested generic
7976 -- that has already been analyzed at the point of definition.
7977 -- We must preserve references that were global to the enclosing
7978 -- parent at that point. Other occurrences, whether global or
7979 -- local to the current generic, must be resolved anew, so we
7980 -- reset the entity in the generic copy. A global reference has a
7981 -- smaller depth than the parent, or else the same depth in case
7982 -- both are distinct compilation units.
7983
7984 -- A child unit is implicitly declared within the enclosing parent
7985 -- but is in fact global to it, and must be preserved.
7986
7987 -- It is also possible for Current_Instantiated_Parent to be
7988 -- defined, and for this not to be a nested generic, namely if
7989 -- the unit is loaded through Rtsfind. In that case, the entity of
7990 -- New_N is only a link to the associated node, and not a defining
7991 -- occurrence.
7992
7993 -- The entities for parent units in the defining_program_unit of a
7994 -- generic child unit are established when the context of the unit
7995 -- is first analyzed, before the generic copy is made. They are
7996 -- preserved in the copy for use in e.g. ASIS queries.
7997
7998 Ent := Entity (New_N);
7999
8000 if No (Current_Instantiated_Parent.Gen_Id) then
8001 if No (Ent)
8002 or else Nkind (Ent) /= N_Defining_Identifier
8003 or else not In_Defining_Unit_Name (N)
8004 then
8005 Set_Associated_Node (New_N, Empty);
8006 end if;
8007
8008 elsif No (Ent)
8009 or else
8010 not Nkind_In (Ent, N_Defining_Identifier,
8011 N_Defining_Character_Literal,
8012 N_Defining_Operator_Symbol)
8013 or else No (Scope (Ent))
8014 or else
8015 (Scope (Ent) = Current_Instantiated_Parent.Gen_Id
8016 and then not Is_Child_Unit (Ent))
8017 or else
8018 (Scope_Depth (Scope (Ent)) >
8019 Scope_Depth (Current_Instantiated_Parent.Gen_Id)
8020 and then
8021 Get_Source_Unit (Ent) =
8022 Get_Source_Unit (Current_Instantiated_Parent.Gen_Id))
8023 then
8024 Set_Associated_Node (New_N, Empty);
8025 end if;
8026
8027 -- Case of instantiating identifier or some other name or operator
8028
8029 else
8030 -- If the associated node is still defined, the entity in it
8031 -- is global, and must be copied to the instance. If this copy
8032 -- is being made for a body to inline, it is applied to an
8033 -- instantiated tree, and the entity is already present and
8034 -- must be also preserved.
8035
8036 declare
8037 Assoc : constant Node_Id := Get_Associated_Node (N);
8038
8039 begin
8040 if Present (Assoc) then
8041 if Nkind (Assoc) = Nkind (N) then
8042 Set_Entity (New_N, Entity (Assoc));
8043 Check_Private_View (N);
8044
8045 -- Here we deal with a very peculiar case for which the
8046 -- Has_Private_View mechanism is not sufficient, because
8047 -- the reference to the type is implicit in the tree,
8048 -- that is to say, it's not referenced from a node but
8049 -- only from another type, namely through Component_Type.
8050
8051 -- package P is
8052
8053 -- type Pt is private;
8054
8055 -- generic
8056 -- type Ft is array (Positive range <>) of Pt;
8057 -- package G is
8058 -- procedure Check (F1, F2 : Ft; Lt : Boolean);
8059 -- end G;
8060
8061 -- private
8062 -- type Pt is new Boolean;
8063 -- end P;
8064
8065 -- package body P is
8066 -- package body G is
8067 -- procedure Check (F1, F2 : Ft; Lt : Boolean) is
8068 -- begin
8069 -- if (F1 < F2) /= Lt then
8070 -- null;
8071 -- end if;
8072 -- end Check;
8073 -- end G;
8074 -- end P;
8075
8076 -- type Arr is array (Positive range <>) of P.Pt;
8077
8078 -- package Inst is new P.G (Arr);
8079
8080 -- Pt is a global type for the generic package G and it
8081 -- is not referenced in its body, but only as component
8082 -- type of Ft, which is a local type. This means that no
8083 -- references to Pt or Ft are seen during the copy of the
8084 -- body, the only reference to Pt being seen is when the
8085 -- actuals are checked by Check_Generic_Actuals, but Pt
8086 -- is still private at this point. In the end, the views
8087 -- of Pt are not switched in the body and, therefore, the
8088 -- array comparison is rejected because the component is
8089 -- still private.
8090
8091 -- Adding e.g. a dummy variable of type Pt in the body is
8092 -- sufficient to make everything work, so we generate an
8093 -- artificial reference to Pt on the fly and thus force
8094 -- the switching of views on the grounds that, if the
8095 -- comparison was accepted during the semantic analysis
8096 -- of the generic, this means that the component cannot
8097 -- have been private (see Sem_Type.Valid_Comparison_Arg).
8098
8099 if Nkind (Assoc) in N_Op_Compare
8100 and then Present (Etype (Left_Opnd (Assoc)))
8101 and then Is_Array_Type (Etype (Left_Opnd (Assoc)))
8102 and then Present (Etype (Right_Opnd (Assoc)))
8103 and then Is_Array_Type (Etype (Right_Opnd (Assoc)))
8104 then
8105 declare
8106 Ltyp : constant Entity_Id :=
8107 Etype (Left_Opnd (Assoc));
8108 Rtyp : constant Entity_Id :=
8109 Etype (Right_Opnd (Assoc));
8110 begin
8111 if Is_Private_Type (Component_Type (Ltyp)) then
8112 Check_Private_View
8113 (New_Occurrence_Of (Component_Type (Ltyp),
8114 Sloc (N)));
8115 end if;
8116 if Is_Private_Type (Component_Type (Rtyp)) then
8117 Check_Private_View
8118 (New_Occurrence_Of (Component_Type (Rtyp),
8119 Sloc (N)));
8120 end if;
8121 end;
8122
8123 -- Here is a similar case, for the Designated_Type of an
8124 -- access type that is present as target type in a type
8125 -- conversion from another access type. In this case, if
8126 -- the base types of the designated types are different
8127 -- and the conversion was accepted during the semantic
8128 -- analysis of the generic, this means that the target
8129 -- type cannot have been private (see Valid_Conversion).
8130
8131 elsif Nkind (Assoc) = N_Identifier
8132 and then Nkind (Parent (Assoc)) = N_Type_Conversion
8133 and then Subtype_Mark (Parent (Assoc)) = Assoc
8134 and then Present (Etype (Assoc))
8135 and then Is_Access_Type (Etype (Assoc))
8136 and then Present (Etype (Expression (Parent (Assoc))))
8137 and then
8138 Is_Access_Type (Etype (Expression (Parent (Assoc))))
8139 then
8140 declare
8141 Targ_Desig : constant Entity_Id :=
8142 Designated_Type (Etype (Assoc));
8143 Expr_Desig : constant Entity_Id :=
8144 Designated_Type
8145 (Etype (Expression (Parent (Assoc))));
8146 begin
8147 if Base_Type (Targ_Desig) /= Base_Type (Expr_Desig)
8148 and then Is_Private_Type (Targ_Desig)
8149 then
8150 Check_Private_View
8151 (New_Occurrence_Of (Targ_Desig, Sloc (N)));
8152 end if;
8153 end;
8154 end if;
8155
8156 -- The node is a reference to a global type and acts as the
8157 -- subtype mark of a qualified expression created in order
8158 -- to aid resolution of accidental overloading in instances.
8159 -- Since N is a reference to a type, the Associated_Node of
8160 -- N denotes an entity rather than another identifier. See
8161 -- Qualify_Universal_Operands for details.
8162
8163 elsif Nkind (N) = N_Identifier
8164 and then Nkind (Parent (N)) = N_Qualified_Expression
8165 and then Subtype_Mark (Parent (N)) = N
8166 and then Is_Qualified_Universal_Literal (Parent (N))
8167 then
8168 Set_Entity (New_N, Assoc);
8169
8170 -- The name in the call may be a selected component if the
8171 -- call has not been analyzed yet, as may be the case for
8172 -- pre/post conditions in a generic unit.
8173
8174 elsif Nkind (Assoc) = N_Function_Call
8175 and then Is_Entity_Name (Name (Assoc))
8176 then
8177 Set_Entity (New_N, Entity (Name (Assoc)));
8178
8179 elsif Nkind_In (Assoc, N_Defining_Identifier,
8180 N_Defining_Character_Literal,
8181 N_Defining_Operator_Symbol)
8182 and then Expander_Active
8183 then
8184 -- Inlining case: we are copying a tree that contains
8185 -- global entities, which are preserved in the copy to be
8186 -- used for subsequent inlining.
8187
8188 null;
8189
8190 else
8191 Set_Entity (New_N, Empty);
8192 end if;
8193 end if;
8194 end;
8195 end if;
8196
8197 -- For expanded name, we must copy the Prefix and Selector_Name
8198
8199 if Nkind (N) = N_Expanded_Name then
8200 Set_Prefix
8201 (New_N, Copy_Generic_Node (Prefix (N), New_N, Instantiating));
8202
8203 Set_Selector_Name (New_N,
8204 Copy_Generic_Node (Selector_Name (N), New_N, Instantiating));
8205
8206 -- For operators, copy the operands
8207
8208 elsif Nkind (N) in N_Op then
8209 if Nkind (N) in N_Binary_Op then
8210 Set_Left_Opnd (New_N,
8211 Copy_Generic_Node (Left_Opnd (N), New_N, Instantiating));
8212 end if;
8213
8214 Set_Right_Opnd (New_N,
8215 Copy_Generic_Node (Right_Opnd (N), New_N, Instantiating));
8216 end if;
8217
8218 -- Establish a link between an entity from the generic template and the
8219 -- corresponding entity in the generic copy to be analyzed.
8220
8221 elsif Nkind (N) in N_Entity then
8222 if not Instantiating then
8223 Set_Associated_Entity (N, New_N);
8224 end if;
8225
8226 -- Clear any existing link the copy may inherit from the replicated
8227 -- generic template entity.
8228
8229 Set_Associated_Entity (New_N, Empty);
8230
8231 -- Special casing for stubs
8232
8233 elsif Nkind (N) in N_Body_Stub then
8234
8235 -- In any case, we must copy the specification or defining
8236 -- identifier as appropriate.
8237
8238 if Nkind (N) = N_Subprogram_Body_Stub then
8239 Set_Specification (New_N,
8240 Copy_Generic_Node (Specification (N), New_N, Instantiating));
8241
8242 else
8243 Set_Defining_Identifier (New_N,
8244 Copy_Generic_Node
8245 (Defining_Identifier (N), New_N, Instantiating));
8246 end if;
8247
8248 -- If we are not instantiating, then this is where we load and
8249 -- analyze subunits, i.e. at the point where the stub occurs. A
8250 -- more permissive system might defer this analysis to the point
8251 -- of instantiation, but this seems too complicated for now.
8252
8253 if not Instantiating then
8254 declare
8255 Subunit_Name : constant Unit_Name_Type := Get_Unit_Name (N);
8256 Subunit : Node_Id;
8257 Unum : Unit_Number_Type;
8258 New_Body : Node_Id;
8259
8260 begin
8261 -- Make sure that, if it is a subunit of the main unit that is
8262 -- preprocessed and if -gnateG is specified, the preprocessed
8263 -- file will be written.
8264
8265 Lib.Analysing_Subunit_Of_Main :=
8266 Lib.In_Extended_Main_Source_Unit (N);
8267 Unum :=
8268 Load_Unit
8269 (Load_Name => Subunit_Name,
8270 Required => False,
8271 Subunit => True,
8272 Error_Node => N);
8273 Lib.Analysing_Subunit_Of_Main := False;
8274
8275 -- If the proper body is not found, a warning message will be
8276 -- emitted when analyzing the stub, or later at the point of
8277 -- instantiation. Here we just leave the stub as is.
8278
8279 if Unum = No_Unit then
8280 Subunits_Missing := True;
8281 goto Subunit_Not_Found;
8282 end if;
8283
8284 Subunit := Cunit (Unum);
8285
8286 if Nkind (Unit (Subunit)) /= N_Subunit then
8287 Error_Msg_N
8288 ("found child unit instead of expected SEPARATE subunit",
8289 Subunit);
8290 Error_Msg_Sloc := Sloc (N);
8291 Error_Msg_N ("\to complete stub #", Subunit);
8292 goto Subunit_Not_Found;
8293 end if;
8294
8295 -- We must create a generic copy of the subunit, in order to
8296 -- perform semantic analysis on it, and we must replace the
8297 -- stub in the original generic unit with the subunit, in order
8298 -- to preserve non-local references within.
8299
8300 -- Only the proper body needs to be copied. Library_Unit and
8301 -- context clause are simply inherited by the generic copy.
8302 -- Note that the copy (which may be recursive if there are
8303 -- nested subunits) must be done first, before attaching it to
8304 -- the enclosing generic.
8305
8306 New_Body :=
8307 Copy_Generic_Node
8308 (Proper_Body (Unit (Subunit)),
8309 Empty, Instantiating => False);
8310
8311 -- Now place the original proper body in the original generic
8312 -- unit. This is a body, not a compilation unit.
8313
8314 Rewrite (N, Proper_Body (Unit (Subunit)));
8315 Set_Is_Compilation_Unit (Defining_Entity (N), False);
8316 Set_Was_Originally_Stub (N);
8317
8318 -- Finally replace the body of the subunit with its copy, and
8319 -- make this new subunit into the library unit of the generic
8320 -- copy, which does not have stubs any longer.
8321
8322 Set_Proper_Body (Unit (Subunit), New_Body);
8323 Set_Library_Unit (New_N, Subunit);
8324 Inherit_Context (Unit (Subunit), N);
8325 end;
8326
8327 -- If we are instantiating, this must be an error case, since
8328 -- otherwise we would have replaced the stub node by the proper body
8329 -- that corresponds. So just ignore it in the copy (i.e. we have
8330 -- copied it, and that is good enough).
8331
8332 else
8333 null;
8334 end if;
8335
8336 <<Subunit_Not_Found>> null;
8337
8338 -- If the node is a compilation unit, it is the subunit of a stub, which
8339 -- has been loaded already (see code below). In this case, the library
8340 -- unit field of N points to the parent unit (which is a compilation
8341 -- unit) and need not (and cannot) be copied.
8342
8343 -- When the proper body of the stub is analyzed, the library_unit link
8344 -- is used to establish the proper context (see sem_ch10).
8345
8346 -- The other fields of a compilation unit are copied as usual
8347
8348 elsif Nkind (N) = N_Compilation_Unit then
8349
8350 -- This code can only be executed when not instantiating, because in
8351 -- the copy made for an instantiation, the compilation unit node has
8352 -- disappeared at the point that a stub is replaced by its proper
8353 -- body.
8354
8355 pragma Assert (not Instantiating);
8356
8357 Set_Context_Items (New_N,
8358 Copy_Generic_List (Context_Items (N), New_N));
8359
8360 Set_Unit (New_N,
8361 Copy_Generic_Node (Unit (N), New_N, Instantiating => False));
8362
8363 Set_First_Inlined_Subprogram (New_N,
8364 Copy_Generic_Node
8365 (First_Inlined_Subprogram (N), New_N, Instantiating => False));
8366
8367 Set_Aux_Decls_Node
8368 (New_N,
8369 Copy_Generic_Node
8370 (Aux_Decls_Node (N), New_N, Instantiating => False));
8371
8372 -- For an assignment node, the assignment is known to be semantically
8373 -- legal if we are instantiating the template. This avoids incorrect
8374 -- diagnostics in generated code.
8375
8376 elsif Nkind (N) = N_Assignment_Statement then
8377
8378 -- Copy name and expression fields in usual manner
8379
8380 Set_Name (New_N,
8381 Copy_Generic_Node (Name (N), New_N, Instantiating));
8382
8383 Set_Expression (New_N,
8384 Copy_Generic_Node (Expression (N), New_N, Instantiating));
8385
8386 if Instantiating then
8387 Set_Assignment_OK (Name (New_N), True);
8388 end if;
8389
8390 elsif Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
8391 if not Instantiating then
8392 Set_Associated_Node (N, New_N);
8393
8394 else
8395 if Present (Get_Associated_Node (N))
8396 and then Nkind (Get_Associated_Node (N)) = Nkind (N)
8397 then
8398 -- In the generic the aggregate has some composite type. If at
8399 -- the point of instantiation the type has a private view,
8400 -- install the full view (and that of its ancestors, if any).
8401
8402 declare
8403 T : Entity_Id := (Etype (Get_Associated_Node (New_N)));
8404 Rt : Entity_Id;
8405
8406 begin
8407 if Present (T) and then Is_Private_Type (T) then
8408 Switch_View (T);
8409 end if;
8410
8411 if Present (T)
8412 and then Is_Tagged_Type (T)
8413 and then Is_Derived_Type (T)
8414 then
8415 Rt := Root_Type (T);
8416
8417 loop
8418 T := Etype (T);
8419
8420 if Is_Private_Type (T) then
8421 Switch_View (T);
8422 end if;
8423
8424 exit when T = Rt;
8425 end loop;
8426 end if;
8427 end;
8428 end if;
8429 end if;
8430
8431 -- Do not copy the associated node, which points to the generic copy
8432 -- of the aggregate.
8433
8434 declare
8435 use Atree.Unchecked_Access;
8436 -- This code section is part of the implementation of an untyped
8437 -- tree traversal, so it needs direct access to node fields.
8438
8439 begin
8440 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
8441 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
8442 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
8443 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
8444 end;
8445
8446 -- Allocators do not have an identifier denoting the access type, so we
8447 -- must locate it through the expression to check whether the views are
8448 -- consistent.
8449
8450 elsif Nkind (N) = N_Allocator
8451 and then Nkind (Expression (N)) = N_Qualified_Expression
8452 and then Is_Entity_Name (Subtype_Mark (Expression (N)))
8453 and then Instantiating
8454 then
8455 declare
8456 T : constant Node_Id :=
8457 Get_Associated_Node (Subtype_Mark (Expression (N)));
8458 Acc_T : Entity_Id;
8459
8460 begin
8461 if Present (T) then
8462
8463 -- Retrieve the allocator node in the generic copy
8464
8465 Acc_T := Etype (Parent (Parent (T)));
8466
8467 if Present (Acc_T) and then Is_Private_Type (Acc_T) then
8468 Switch_View (Acc_T);
8469 end if;
8470 end if;
8471
8472 Copy_Descendants;
8473 end;
8474
8475 -- For a proper body, we must catch the case of a proper body that
8476 -- replaces a stub. This represents the point at which a separate
8477 -- compilation unit, and hence template file, may be referenced, so we
8478 -- must make a new source instantiation entry for the template of the
8479 -- subunit, and ensure that all nodes in the subunit are adjusted using
8480 -- this new source instantiation entry.
8481
8482 elsif Nkind (N) in N_Proper_Body then
8483 declare
8484 Save_Adjustment : constant Sloc_Adjustment := S_Adjustment;
8485 begin
8486 if Instantiating and then Was_Originally_Stub (N) then
8487 Create_Instantiation_Source
8488 (Instantiation_Node,
8489 Defining_Entity (N),
8490 S_Adjustment);
8491
8492 Adjust_Instantiation_Sloc (New_N, S_Adjustment);
8493 end if;
8494
8495 -- Now copy the fields of the proper body, using the new
8496 -- adjustment factor if one was needed as per test above.
8497
8498 Copy_Descendants;
8499
8500 -- Restore the original adjustment factor
8501
8502 S_Adjustment := Save_Adjustment;
8503 end;
8504
8505 elsif Nkind (N) = N_Pragma and then Instantiating then
8506
8507 -- Do not copy Comment or Ident pragmas their content is relevant to
8508 -- the generic unit, not to the instantiating unit.
8509
8510 if Nam_In (Pragma_Name_Unmapped (N), Name_Comment, Name_Ident) then
8511 New_N := Make_Null_Statement (Sloc (N));
8512
8513 -- Do not copy pragmas generated from aspects because the pragmas do
8514 -- not carry any semantic information, plus they will be regenerated
8515 -- in the instance.
8516
8517 -- However, generating C we need to copy them since postconditions
8518 -- are inlined by the front end, and the front-end inlining machinery
8519 -- relies on this routine to perform inlining.
8520
8521 elsif From_Aspect_Specification (N)
8522 and then not Modify_Tree_For_C
8523 then
8524 New_N := Make_Null_Statement (Sloc (N));
8525
8526 else
8527 Copy_Descendants;
8528 end if;
8529
8530 elsif Nkind_In (N, N_Integer_Literal, N_Real_Literal) then
8531
8532 -- No descendant fields need traversing
8533
8534 null;
8535
8536 elsif Nkind (N) = N_String_Literal
8537 and then Present (Etype (N))
8538 and then Instantiating
8539 then
8540 -- If the string is declared in an outer scope, the string_literal
8541 -- subtype created for it may have the wrong scope. Force reanalysis
8542 -- of the constant to generate a new itype in the proper context.
8543
8544 Set_Etype (New_N, Empty);
8545 Set_Analyzed (New_N, False);
8546
8547 -- For the remaining nodes, copy their descendants recursively
8548
8549 else
8550 Copy_Descendants;
8551
8552 if Instantiating and then Nkind (N) = N_Subprogram_Body then
8553 Set_Generic_Parent (Specification (New_N), N);
8554
8555 -- Should preserve Corresponding_Spec??? (12.3(14))
8556 end if;
8557 end if;
8558
8559 -- Propagate dimensions if present, so that they are reflected in the
8560 -- instance.
8561
8562 if Nkind (N) in N_Has_Etype
8563 and then (Nkind (N) in N_Op or else Is_Entity_Name (N))
8564 and then Present (Etype (N))
8565 and then Is_Floating_Point_Type (Etype (N))
8566 and then Has_Dimension_System (Etype (N))
8567 then
8568 Copy_Dimensions (N, New_N);
8569 end if;
8570
8571 return New_N;
8572 end Copy_Generic_Node;
8573
8574 ----------------------------
8575 -- Denotes_Formal_Package --
8576 ----------------------------
8577
8578 function Denotes_Formal_Package
8579 (Pack : Entity_Id;
8580 On_Exit : Boolean := False;
8581 Instance : Entity_Id := Empty) return Boolean
8582 is
8583 Par : Entity_Id;
8584 Scop : constant Entity_Id := Scope (Pack);
8585 E : Entity_Id;
8586
8587 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean;
8588 -- The package in question may be an actual for a previous formal
8589 -- package P of the current instance, so examine its actuals as well.
8590 -- This must be recursive over other formal packages.
8591
8592 ----------------------------------
8593 -- Is_Actual_Of_Previous_Formal --
8594 ----------------------------------
8595
8596 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean is
8597 E1 : Entity_Id;
8598
8599 begin
8600 E1 := First_Entity (P);
8601 while Present (E1) and then E1 /= Instance loop
8602 if Ekind (E1) = E_Package
8603 and then Nkind (Parent (E1)) = N_Package_Renaming_Declaration
8604 then
8605 if Renamed_Object (E1) = Pack then
8606 return True;
8607
8608 elsif E1 = P or else Renamed_Object (E1) = P then
8609 return False;
8610
8611 elsif Is_Actual_Of_Previous_Formal (E1) then
8612 return True;
8613 end if;
8614 end if;
8615
8616 Next_Entity (E1);
8617 end loop;
8618
8619 return False;
8620 end Is_Actual_Of_Previous_Formal;
8621
8622 -- Start of processing for Denotes_Formal_Package
8623
8624 begin
8625 if On_Exit then
8626 Par :=
8627 Instance_Envs.Table
8628 (Instance_Envs.Last).Instantiated_Parent.Act_Id;
8629 else
8630 Par := Current_Instantiated_Parent.Act_Id;
8631 end if;
8632
8633 if Ekind (Scop) = E_Generic_Package
8634 or else Nkind (Unit_Declaration_Node (Scop)) =
8635 N_Generic_Subprogram_Declaration
8636 then
8637 return True;
8638
8639 elsif Nkind (Original_Node (Unit_Declaration_Node (Pack))) =
8640 N_Formal_Package_Declaration
8641 then
8642 return True;
8643
8644 elsif No (Par) then
8645 return False;
8646
8647 else
8648 -- Check whether this package is associated with a formal package of
8649 -- the enclosing instantiation. Iterate over the list of renamings.
8650
8651 E := First_Entity (Par);
8652 while Present (E) loop
8653 if Ekind (E) /= E_Package
8654 or else Nkind (Parent (E)) /= N_Package_Renaming_Declaration
8655 then
8656 null;
8657
8658 elsif Renamed_Object (E) = Par then
8659 return False;
8660
8661 elsif Renamed_Object (E) = Pack then
8662 return True;
8663
8664 elsif Is_Actual_Of_Previous_Formal (E) then
8665 return True;
8666
8667 end if;
8668
8669 Next_Entity (E);
8670 end loop;
8671
8672 return False;
8673 end if;
8674 end Denotes_Formal_Package;
8675
8676 -----------------
8677 -- End_Generic --
8678 -----------------
8679
8680 procedure End_Generic is
8681 begin
8682 -- ??? More things could be factored out in this routine. Should
8683 -- probably be done at a later stage.
8684
8685 Inside_A_Generic := Generic_Flags.Table (Generic_Flags.Last);
8686 Generic_Flags.Decrement_Last;
8687
8688 Expander_Mode_Restore;
8689 end End_Generic;
8690
8691 -------------
8692 -- Earlier --
8693 -------------
8694
8695 function Earlier (N1, N2 : Node_Id) return Boolean is
8696 procedure Find_Depth (P : in out Node_Id; D : in out Integer);
8697 -- Find distance from given node to enclosing compilation unit
8698
8699 ----------------
8700 -- Find_Depth --
8701 ----------------
8702
8703 procedure Find_Depth (P : in out Node_Id; D : in out Integer) is
8704 begin
8705 while Present (P)
8706 and then Nkind (P) /= N_Compilation_Unit
8707 loop
8708 P := True_Parent (P);
8709 D := D + 1;
8710 end loop;
8711 end Find_Depth;
8712
8713 -- Local declarations
8714
8715 D1 : Integer := 0;
8716 D2 : Integer := 0;
8717 P1 : Node_Id := N1;
8718 P2 : Node_Id := N2;
8719 T1 : Source_Ptr;
8720 T2 : Source_Ptr;
8721
8722 -- Start of processing for Earlier
8723
8724 begin
8725 Find_Depth (P1, D1);
8726 Find_Depth (P2, D2);
8727
8728 if P1 /= P2 then
8729 return False;
8730 else
8731 P1 := N1;
8732 P2 := N2;
8733 end if;
8734
8735 while D1 > D2 loop
8736 P1 := True_Parent (P1);
8737 D1 := D1 - 1;
8738 end loop;
8739
8740 while D2 > D1 loop
8741 P2 := True_Parent (P2);
8742 D2 := D2 - 1;
8743 end loop;
8744
8745 -- At this point P1 and P2 are at the same distance from the root.
8746 -- We examine their parents until we find a common declarative list.
8747 -- If we reach the root, N1 and N2 do not descend from the same
8748 -- declarative list (e.g. one is nested in the declarative part and
8749 -- the other is in a block in the statement part) and the earlier
8750 -- one is already frozen.
8751
8752 while not Is_List_Member (P1)
8753 or else not Is_List_Member (P2)
8754 or else List_Containing (P1) /= List_Containing (P2)
8755 loop
8756 P1 := True_Parent (P1);
8757 P2 := True_Parent (P2);
8758
8759 if Nkind (Parent (P1)) = N_Subunit then
8760 P1 := Corresponding_Stub (Parent (P1));
8761 end if;
8762
8763 if Nkind (Parent (P2)) = N_Subunit then
8764 P2 := Corresponding_Stub (Parent (P2));
8765 end if;
8766
8767 if P1 = P2 then
8768 return False;
8769 end if;
8770 end loop;
8771
8772 -- Expanded code usually shares the source location of the original
8773 -- construct it was generated for. This however may not necessarily
8774 -- reflect the true location of the code within the tree.
8775
8776 -- Before comparing the slocs of the two nodes, make sure that we are
8777 -- working with correct source locations. Assume that P1 is to the left
8778 -- of P2. If either one does not come from source, traverse the common
8779 -- list heading towards the other node and locate the first source
8780 -- statement.
8781
8782 -- P1 P2
8783 -- ----+===+===+--------------+===+===+----
8784 -- expanded code expanded code
8785
8786 if not Comes_From_Source (P1) then
8787 while Present (P1) loop
8788
8789 -- Neither P2 nor a source statement were located during the
8790 -- search. If we reach the end of the list, then P1 does not
8791 -- occur earlier than P2.
8792
8793 -- ---->
8794 -- start --- P2 ----- P1 --- end
8795
8796 if No (Next (P1)) then
8797 return False;
8798
8799 -- We encounter P2 while going to the right of the list. This
8800 -- means that P1 does indeed appear earlier.
8801
8802 -- ---->
8803 -- start --- P1 ===== P2 --- end
8804 -- expanded code in between
8805
8806 elsif P1 = P2 then
8807 return True;
8808
8809 -- No need to look any further since we have located a source
8810 -- statement.
8811
8812 elsif Comes_From_Source (P1) then
8813 exit;
8814 end if;
8815
8816 -- Keep going right
8817
8818 Next (P1);
8819 end loop;
8820 end if;
8821
8822 if not Comes_From_Source (P2) then
8823 while Present (P2) loop
8824
8825 -- Neither P1 nor a source statement were located during the
8826 -- search. If we reach the start of the list, then P1 does not
8827 -- occur earlier than P2.
8828
8829 -- <----
8830 -- start --- P2 --- P1 --- end
8831
8832 if No (Prev (P2)) then
8833 return False;
8834
8835 -- We encounter P1 while going to the left of the list. This
8836 -- means that P1 does indeed appear earlier.
8837
8838 -- <----
8839 -- start --- P1 ===== P2 --- end
8840 -- expanded code in between
8841
8842 elsif P2 = P1 then
8843 return True;
8844
8845 -- No need to look any further since we have located a source
8846 -- statement.
8847
8848 elsif Comes_From_Source (P2) then
8849 exit;
8850 end if;
8851
8852 -- Keep going left
8853
8854 Prev (P2);
8855 end loop;
8856 end if;
8857
8858 -- At this point either both nodes came from source or we approximated
8859 -- their source locations through neighboring source statements.
8860
8861 T1 := Top_Level_Location (Sloc (P1));
8862 T2 := Top_Level_Location (Sloc (P2));
8863
8864 -- When two nodes come from the same instance, they have identical top
8865 -- level locations. To determine proper relation within the tree, check
8866 -- their locations within the template.
8867
8868 if T1 = T2 then
8869 return Sloc (P1) < Sloc (P2);
8870
8871 -- The two nodes either come from unrelated instances or do not come
8872 -- from instantiated code at all.
8873
8874 else
8875 return T1 < T2;
8876 end if;
8877 end Earlier;
8878
8879 ----------------------
8880 -- Find_Actual_Type --
8881 ----------------------
8882
8883 function Find_Actual_Type
8884 (Typ : Entity_Id;
8885 Gen_Type : Entity_Id) return Entity_Id
8886 is
8887 Gen_Scope : constant Entity_Id := Scope (Gen_Type);
8888 T : Entity_Id;
8889
8890 begin
8891 -- Special processing only applies to child units
8892
8893 if not Is_Child_Unit (Gen_Scope) then
8894 return Get_Instance_Of (Typ);
8895
8896 -- If designated or component type is itself a formal of the child unit,
8897 -- its instance is available.
8898
8899 elsif Scope (Typ) = Gen_Scope then
8900 return Get_Instance_Of (Typ);
8901
8902 -- If the array or access type is not declared in the parent unit,
8903 -- no special processing needed.
8904
8905 elsif not Is_Generic_Type (Typ)
8906 and then Scope (Gen_Scope) /= Scope (Typ)
8907 then
8908 return Get_Instance_Of (Typ);
8909
8910 -- Otherwise, retrieve designated or component type by visibility
8911
8912 else
8913 T := Current_Entity (Typ);
8914 while Present (T) loop
8915 if In_Open_Scopes (Scope (T)) then
8916 return T;
8917 elsif Is_Generic_Actual_Type (T) then
8918 return T;
8919 end if;
8920
8921 T := Homonym (T);
8922 end loop;
8923
8924 return Typ;
8925 end if;
8926 end Find_Actual_Type;
8927
8928 ----------------------------
8929 -- Freeze_Subprogram_Body --
8930 ----------------------------
8931
8932 procedure Freeze_Subprogram_Body
8933 (Inst_Node : Node_Id;
8934 Gen_Body : Node_Id;
8935 Pack_Id : Entity_Id)
8936 is
8937 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
8938 Par : constant Entity_Id := Scope (Gen_Unit);
8939 E_G_Id : Entity_Id;
8940 Enc_G : Entity_Id;
8941 Enc_I : Node_Id;
8942 F_Node : Node_Id;
8943
8944 function Enclosing_Package_Body (N : Node_Id) return Node_Id;
8945 -- Find innermost package body that encloses the given node, and which
8946 -- is not a compilation unit. Freeze nodes for the instance, or for its
8947 -- enclosing body, may be inserted after the enclosing_body of the
8948 -- generic unit. Used to determine proper placement of freeze node for
8949 -- both package and subprogram instances.
8950
8951 function Package_Freeze_Node (B : Node_Id) return Node_Id;
8952 -- Find entity for given package body, and locate or create a freeze
8953 -- node for it.
8954
8955 ----------------------------
8956 -- Enclosing_Package_Body --
8957 ----------------------------
8958
8959 function Enclosing_Package_Body (N : Node_Id) return Node_Id is
8960 P : Node_Id;
8961
8962 begin
8963 P := Parent (N);
8964 while Present (P)
8965 and then Nkind (Parent (P)) /= N_Compilation_Unit
8966 loop
8967 if Nkind (P) = N_Package_Body then
8968 if Nkind (Parent (P)) = N_Subunit then
8969 return Corresponding_Stub (Parent (P));
8970 else
8971 return P;
8972 end if;
8973 end if;
8974
8975 P := True_Parent (P);
8976 end loop;
8977
8978 return Empty;
8979 end Enclosing_Package_Body;
8980
8981 -------------------------
8982 -- Package_Freeze_Node --
8983 -------------------------
8984
8985 function Package_Freeze_Node (B : Node_Id) return Node_Id is
8986 Id : Entity_Id;
8987
8988 begin
8989 if Nkind (B) = N_Package_Body then
8990 Id := Corresponding_Spec (B);
8991 else pragma Assert (Nkind (B) = N_Package_Body_Stub);
8992 Id := Corresponding_Spec (Proper_Body (Unit (Library_Unit (B))));
8993 end if;
8994
8995 Ensure_Freeze_Node (Id);
8996 return Freeze_Node (Id);
8997 end Package_Freeze_Node;
8998
8999 -- Start of processing for Freeze_Subprogram_Body
9000
9001 begin
9002 -- If the instance and the generic body appear within the same unit, and
9003 -- the instance precedes the generic, the freeze node for the instance
9004 -- must appear after that of the generic. If the generic is nested
9005 -- within another instance I2, then current instance must be frozen
9006 -- after I2. In both cases, the freeze nodes are those of enclosing
9007 -- packages. Otherwise, the freeze node is placed at the end of the
9008 -- current declarative part.
9009
9010 Enc_G := Enclosing_Package_Body (Gen_Body);
9011 Enc_I := Enclosing_Package_Body (Inst_Node);
9012 Ensure_Freeze_Node (Pack_Id);
9013 F_Node := Freeze_Node (Pack_Id);
9014
9015 if Is_Generic_Instance (Par)
9016 and then Present (Freeze_Node (Par))
9017 and then In_Same_Declarative_Part
9018 (Parent (Freeze_Node (Par)), Inst_Node)
9019 then
9020 -- The parent was a premature instantiation. Insert freeze node at
9021 -- the end the current declarative part.
9022
9023 if Is_Known_Guaranteed_ABE (Get_Unit_Instantiation_Node (Par)) then
9024 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
9025
9026 -- Handle the following case:
9027 --
9028 -- package Parent_Inst is new ...
9029 -- Parent_Inst []
9030 --
9031 -- procedure P ... -- this body freezes Parent_Inst
9032 --
9033 -- package Inst is new ...
9034 --
9035 -- In this particular scenario, the freeze node for Inst must be
9036 -- inserted in the same manner as that of Parent_Inst - before the
9037 -- next source body or at the end of the declarative list (body not
9038 -- available). If body P did not exist and Parent_Inst was frozen
9039 -- after Inst, either by a body following Inst or at the end of the
9040 -- declarative region, the freeze node for Inst must be inserted
9041 -- after that of Parent_Inst. This relation is established by
9042 -- comparing the Slocs of Parent_Inst freeze node and Inst.
9043
9044 elsif List_Containing (Get_Unit_Instantiation_Node (Par)) =
9045 List_Containing (Inst_Node)
9046 and then Sloc (Freeze_Node (Par)) < Sloc (Inst_Node)
9047 then
9048 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
9049
9050 else
9051 Insert_After (Freeze_Node (Par), F_Node);
9052 end if;
9053
9054 -- The body enclosing the instance should be frozen after the body that
9055 -- includes the generic, because the body of the instance may make
9056 -- references to entities therein. If the two are not in the same
9057 -- declarative part, or if the one enclosing the instance is frozen
9058 -- already, freeze the instance at the end of the current declarative
9059 -- part.
9060
9061 elsif Is_Generic_Instance (Par)
9062 and then Present (Freeze_Node (Par))
9063 and then Present (Enc_I)
9064 then
9065 if In_Same_Declarative_Part (Parent (Freeze_Node (Par)), Enc_I)
9066 or else
9067 (Nkind (Enc_I) = N_Package_Body
9068 and then In_Same_Declarative_Part
9069 (Parent (Freeze_Node (Par)), Parent (Enc_I)))
9070 then
9071 -- The enclosing package may contain several instances. Rather
9072 -- than computing the earliest point at which to insert its freeze
9073 -- node, we place it at the end of the declarative part of the
9074 -- parent of the generic.
9075
9076 Insert_Freeze_Node_For_Instance
9077 (Freeze_Node (Par), Package_Freeze_Node (Enc_I));
9078 end if;
9079
9080 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
9081
9082 elsif Present (Enc_G)
9083 and then Present (Enc_I)
9084 and then Enc_G /= Enc_I
9085 and then Earlier (Inst_Node, Gen_Body)
9086 then
9087 if Nkind (Enc_G) = N_Package_Body then
9088 E_G_Id :=
9089 Corresponding_Spec (Enc_G);
9090 else pragma Assert (Nkind (Enc_G) = N_Package_Body_Stub);
9091 E_G_Id :=
9092 Corresponding_Spec (Proper_Body (Unit (Library_Unit (Enc_G))));
9093 end if;
9094
9095 -- Freeze package that encloses instance, and place node after the
9096 -- package that encloses generic. If enclosing package is already
9097 -- frozen we have to assume it is at the proper place. This may be a
9098 -- potential ABE that requires dynamic checking. Do not add a freeze
9099 -- node if the package that encloses the generic is inside the body
9100 -- that encloses the instance, because the freeze node would be in
9101 -- the wrong scope. Additional contortions needed if the bodies are
9102 -- within a subunit.
9103
9104 declare
9105 Enclosing_Body : Node_Id;
9106
9107 begin
9108 if Nkind (Enc_I) = N_Package_Body_Stub then
9109 Enclosing_Body := Proper_Body (Unit (Library_Unit (Enc_I)));
9110 else
9111 Enclosing_Body := Enc_I;
9112 end if;
9113
9114 if Parent (List_Containing (Enc_G)) /= Enclosing_Body then
9115 Insert_Freeze_Node_For_Instance
9116 (Enc_G, Package_Freeze_Node (Enc_I));
9117 end if;
9118 end;
9119
9120 -- Freeze enclosing subunit before instance
9121
9122 Ensure_Freeze_Node (E_G_Id);
9123
9124 if not Is_List_Member (Freeze_Node (E_G_Id)) then
9125 Insert_After (Enc_G, Freeze_Node (E_G_Id));
9126 end if;
9127
9128 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
9129
9130 else
9131 -- If none of the above, insert freeze node at the end of the current
9132 -- declarative part.
9133
9134 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
9135 end if;
9136 end Freeze_Subprogram_Body;
9137
9138 ----------------
9139 -- Get_Gen_Id --
9140 ----------------
9141
9142 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id is
9143 begin
9144 return Generic_Renamings.Table (E).Gen_Id;
9145 end Get_Gen_Id;
9146
9147 ---------------------
9148 -- Get_Instance_Of --
9149 ---------------------
9150
9151 function Get_Instance_Of (A : Entity_Id) return Entity_Id is
9152 Res : constant Assoc_Ptr := Generic_Renamings_HTable.Get (A);
9153
9154 begin
9155 if Res /= Assoc_Null then
9156 return Generic_Renamings.Table (Res).Act_Id;
9157
9158 else
9159 -- On exit, entity is not instantiated: not a generic parameter, or
9160 -- else parameter of an inner generic unit.
9161
9162 return A;
9163 end if;
9164 end Get_Instance_Of;
9165
9166 ---------------------------------
9167 -- Get_Unit_Instantiation_Node --
9168 ---------------------------------
9169
9170 function Get_Unit_Instantiation_Node (A : Entity_Id) return Node_Id is
9171 Decl : Node_Id := Unit_Declaration_Node (A);
9172 Inst : Node_Id;
9173
9174 begin
9175 -- If the Package_Instantiation attribute has been set on the package
9176 -- entity, then use it directly when it (or its Original_Node) refers
9177 -- to an N_Package_Instantiation node. In principle it should be
9178 -- possible to have this field set in all cases, which should be
9179 -- investigated, and would allow this function to be significantly
9180 -- simplified. ???
9181
9182 Inst := Package_Instantiation (A);
9183
9184 if Present (Inst) then
9185 if Nkind (Inst) = N_Package_Instantiation then
9186 return Inst;
9187
9188 elsif Nkind (Original_Node (Inst)) = N_Package_Instantiation then
9189 return Original_Node (Inst);
9190 end if;
9191 end if;
9192
9193 -- If the instantiation is a compilation unit that does not need body
9194 -- then the instantiation node has been rewritten as a package
9195 -- declaration for the instance, and we return the original node.
9196
9197 -- If it is a compilation unit and the instance node has not been
9198 -- rewritten, then it is still the unit of the compilation. Finally, if
9199 -- a body is present, this is a parent of the main unit whose body has
9200 -- been compiled for inlining purposes, and the instantiation node has
9201 -- been rewritten with the instance body.
9202
9203 -- Otherwise the instantiation node appears after the declaration. If
9204 -- the entity is a formal package, the declaration may have been
9205 -- rewritten as a generic declaration (in the case of a formal with box)
9206 -- or left as a formal package declaration if it has actuals, and is
9207 -- found with a forward search.
9208
9209 if Nkind (Parent (Decl)) = N_Compilation_Unit then
9210 if Nkind (Decl) = N_Package_Declaration
9211 and then Present (Corresponding_Body (Decl))
9212 then
9213 Decl := Unit_Declaration_Node (Corresponding_Body (Decl));
9214 end if;
9215
9216 if Nkind (Original_Node (Decl)) in N_Generic_Instantiation then
9217 return Original_Node (Decl);
9218 else
9219 return Unit (Parent (Decl));
9220 end if;
9221
9222 elsif Nkind (Decl) = N_Package_Declaration
9223 and then Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration
9224 then
9225 return Original_Node (Decl);
9226
9227 else
9228 Inst := Next (Decl);
9229 while not Nkind_In (Inst, N_Formal_Package_Declaration,
9230 N_Function_Instantiation,
9231 N_Package_Instantiation,
9232 N_Procedure_Instantiation)
9233 loop
9234 Next (Inst);
9235 end loop;
9236
9237 return Inst;
9238 end if;
9239 end Get_Unit_Instantiation_Node;
9240
9241 ------------------------
9242 -- Has_Been_Exchanged --
9243 ------------------------
9244
9245 function Has_Been_Exchanged (E : Entity_Id) return Boolean is
9246 Next : Elmt_Id;
9247
9248 begin
9249 Next := First_Elmt (Exchanged_Views);
9250 while Present (Next) loop
9251 if Full_View (Node (Next)) = E then
9252 return True;
9253 end if;
9254
9255 Next_Elmt (Next);
9256 end loop;
9257
9258 return False;
9259 end Has_Been_Exchanged;
9260
9261 -------------------
9262 -- Has_Contracts --
9263 -------------------
9264
9265 function Has_Contracts (Decl : Node_Id) return Boolean is
9266 A_List : constant List_Id := Aspect_Specifications (Decl);
9267 A_Spec : Node_Id;
9268 A_Id : Aspect_Id;
9269 begin
9270 if No (A_List) then
9271 return False;
9272 else
9273 A_Spec := First (A_List);
9274 while Present (A_Spec) loop
9275 A_Id := Get_Aspect_Id (A_Spec);
9276 if A_Id = Aspect_Pre or else A_Id = Aspect_Post then
9277 return True;
9278 end if;
9279
9280 Next (A_Spec);
9281 end loop;
9282
9283 return False;
9284 end if;
9285 end Has_Contracts;
9286
9287 ----------
9288 -- Hash --
9289 ----------
9290
9291 function Hash (F : Entity_Id) return HTable_Range is
9292 begin
9293 return HTable_Range (F mod HTable_Size);
9294 end Hash;
9295
9296 ------------------------
9297 -- Hide_Current_Scope --
9298 ------------------------
9299
9300 procedure Hide_Current_Scope is
9301 C : constant Entity_Id := Current_Scope;
9302 E : Entity_Id;
9303
9304 begin
9305 Set_Is_Hidden_Open_Scope (C);
9306
9307 E := First_Entity (C);
9308 while Present (E) loop
9309 if Is_Immediately_Visible (E) then
9310 Set_Is_Immediately_Visible (E, False);
9311 Append_Elmt (E, Hidden_Entities);
9312 end if;
9313
9314 Next_Entity (E);
9315 end loop;
9316
9317 -- Make the scope name invisible as well. This is necessary, but might
9318 -- conflict with calls to Rtsfind later on, in case the scope is a
9319 -- predefined one. There is no clean solution to this problem, so for
9320 -- now we depend on the user not redefining Standard itself in one of
9321 -- the parent units.
9322
9323 if Is_Immediately_Visible (C) and then C /= Standard_Standard then
9324 Set_Is_Immediately_Visible (C, False);
9325 Append_Elmt (C, Hidden_Entities);
9326 end if;
9327
9328 end Hide_Current_Scope;
9329
9330 --------------
9331 -- Init_Env --
9332 --------------
9333
9334 procedure Init_Env is
9335 Saved : Instance_Env;
9336
9337 begin
9338 Saved.Instantiated_Parent := Current_Instantiated_Parent;
9339 Saved.Exchanged_Views := Exchanged_Views;
9340 Saved.Hidden_Entities := Hidden_Entities;
9341 Saved.Current_Sem_Unit := Current_Sem_Unit;
9342 Saved.Parent_Unit_Visible := Parent_Unit_Visible;
9343 Saved.Instance_Parent_Unit := Instance_Parent_Unit;
9344
9345 -- Save configuration switches. These may be reset if the unit is a
9346 -- predefined unit, and the current mode is not Ada 2005.
9347
9348 Saved.Switches := Save_Config_Switches;
9349
9350 Instance_Envs.Append (Saved);
9351
9352 Exchanged_Views := New_Elmt_List;
9353 Hidden_Entities := New_Elmt_List;
9354
9355 -- Make dummy entry for Instantiated parent. If generic unit is legal,
9356 -- this is set properly in Set_Instance_Env.
9357
9358 Current_Instantiated_Parent :=
9359 (Current_Scope, Current_Scope, Assoc_Null);
9360 end Init_Env;
9361
9362 ---------------------
9363 -- In_Main_Context --
9364 ---------------------
9365
9366 function In_Main_Context (E : Entity_Id) return Boolean is
9367 Context : List_Id;
9368 Clause : Node_Id;
9369 Nam : Node_Id;
9370
9371 begin
9372 if not Is_Compilation_Unit (E)
9373 or else Ekind (E) /= E_Package
9374 or else In_Private_Part (E)
9375 then
9376 return False;
9377 end if;
9378
9379 Context := Context_Items (Cunit (Main_Unit));
9380
9381 Clause := First (Context);
9382 while Present (Clause) loop
9383 if Nkind (Clause) = N_With_Clause then
9384 Nam := Name (Clause);
9385
9386 -- If the current scope is part of the context of the main unit,
9387 -- analysis of the corresponding with_clause is not complete, and
9388 -- the entity is not set. We use the Chars field directly, which
9389 -- might produce false positives in rare cases, but guarantees
9390 -- that we produce all the instance bodies we will need.
9391
9392 if (Is_Entity_Name (Nam) and then Chars (Nam) = Chars (E))
9393 or else (Nkind (Nam) = N_Selected_Component
9394 and then Chars (Selector_Name (Nam)) = Chars (E))
9395 then
9396 return True;
9397 end if;
9398 end if;
9399
9400 Next (Clause);
9401 end loop;
9402
9403 return False;
9404 end In_Main_Context;
9405
9406 ---------------------
9407 -- Inherit_Context --
9408 ---------------------
9409
9410 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id) is
9411 Current_Context : List_Id;
9412 Current_Unit : Node_Id;
9413 Item : Node_Id;
9414 New_I : Node_Id;
9415
9416 Clause : Node_Id;
9417 OK : Boolean;
9418 Lib_Unit : Node_Id;
9419
9420 begin
9421 if Nkind (Parent (Gen_Decl)) = N_Compilation_Unit then
9422
9423 -- The inherited context is attached to the enclosing compilation
9424 -- unit. This is either the main unit, or the declaration for the
9425 -- main unit (in case the instantiation appears within the package
9426 -- declaration and the main unit is its body).
9427
9428 Current_Unit := Parent (Inst);
9429 while Present (Current_Unit)
9430 and then Nkind (Current_Unit) /= N_Compilation_Unit
9431 loop
9432 Current_Unit := Parent (Current_Unit);
9433 end loop;
9434
9435 Current_Context := Context_Items (Current_Unit);
9436
9437 Item := First (Context_Items (Parent (Gen_Decl)));
9438 while Present (Item) loop
9439 if Nkind (Item) = N_With_Clause then
9440 Lib_Unit := Library_Unit (Item);
9441
9442 -- Take care to prevent direct cyclic with's
9443
9444 if Lib_Unit /= Current_Unit then
9445
9446 -- Do not add a unit if it is already in the context
9447
9448 Clause := First (Current_Context);
9449 OK := True;
9450 while Present (Clause) loop
9451 if Nkind (Clause) = N_With_Clause
9452 and then Library_Unit (Clause) = Lib_Unit
9453 then
9454 OK := False;
9455 exit;
9456 end if;
9457
9458 Next (Clause);
9459 end loop;
9460
9461 if OK then
9462 New_I := New_Copy (Item);
9463 Set_Implicit_With (New_I);
9464
9465 Append (New_I, Current_Context);
9466 end if;
9467 end if;
9468 end if;
9469
9470 Next (Item);
9471 end loop;
9472 end if;
9473 end Inherit_Context;
9474
9475 ----------------
9476 -- Initialize --
9477 ----------------
9478
9479 procedure Initialize is
9480 begin
9481 Generic_Renamings.Init;
9482 Instance_Envs.Init;
9483 Generic_Flags.Init;
9484 Generic_Renamings_HTable.Reset;
9485 Circularity_Detected := False;
9486 Exchanged_Views := No_Elist;
9487 Hidden_Entities := No_Elist;
9488 end Initialize;
9489
9490 -------------------------------------
9491 -- Insert_Freeze_Node_For_Instance --
9492 -------------------------------------
9493
9494 procedure Insert_Freeze_Node_For_Instance
9495 (N : Node_Id;
9496 F_Node : Node_Id)
9497 is
9498 Decl : Node_Id;
9499 Decls : List_Id;
9500 Inst : Entity_Id;
9501 Par_N : Node_Id;
9502
9503 function Enclosing_Body (N : Node_Id) return Node_Id;
9504 -- Find enclosing package or subprogram body, if any. Freeze node may
9505 -- be placed at end of current declarative list if previous instance
9506 -- and current one have different enclosing bodies.
9507
9508 function Previous_Instance (Gen : Entity_Id) return Entity_Id;
9509 -- Find the local instance, if any, that declares the generic that is
9510 -- being instantiated. If present, the freeze node for this instance
9511 -- must follow the freeze node for the previous instance.
9512
9513 --------------------
9514 -- Enclosing_Body --
9515 --------------------
9516
9517 function Enclosing_Body (N : Node_Id) return Node_Id is
9518 P : Node_Id;
9519
9520 begin
9521 P := Parent (N);
9522 while Present (P)
9523 and then Nkind (Parent (P)) /= N_Compilation_Unit
9524 loop
9525 if Nkind_In (P, N_Package_Body, N_Subprogram_Body) then
9526 if Nkind (Parent (P)) = N_Subunit then
9527 return Corresponding_Stub (Parent (P));
9528 else
9529 return P;
9530 end if;
9531 end if;
9532
9533 P := True_Parent (P);
9534 end loop;
9535
9536 return Empty;
9537 end Enclosing_Body;
9538
9539 -----------------------
9540 -- Previous_Instance --
9541 -----------------------
9542
9543 function Previous_Instance (Gen : Entity_Id) return Entity_Id is
9544 S : Entity_Id;
9545
9546 begin
9547 S := Scope (Gen);
9548 while Present (S) and then S /= Standard_Standard loop
9549 if Is_Generic_Instance (S)
9550 and then In_Same_Source_Unit (S, N)
9551 then
9552 return S;
9553 end if;
9554
9555 S := Scope (S);
9556 end loop;
9557
9558 return Empty;
9559 end Previous_Instance;
9560
9561 -- Start of processing for Insert_Freeze_Node_For_Instance
9562
9563 begin
9564 if not Is_List_Member (F_Node) then
9565 Decl := N;
9566 Decls := List_Containing (N);
9567 Inst := Entity (F_Node);
9568 Par_N := Parent (Decls);
9569
9570 -- When processing a subprogram instantiation, utilize the actual
9571 -- subprogram instantiation rather than its package wrapper as it
9572 -- carries all the context information.
9573
9574 if Is_Wrapper_Package (Inst) then
9575 Inst := Related_Instance (Inst);
9576 end if;
9577
9578 -- If this is a package instance, check whether the generic is
9579 -- declared in a previous instance and the current instance is
9580 -- not within the previous one.
9581
9582 if Present (Generic_Parent (Parent (Inst)))
9583 and then Is_In_Main_Unit (N)
9584 then
9585 declare
9586 Enclosing_N : constant Node_Id := Enclosing_Body (N);
9587 Par_I : constant Entity_Id :=
9588 Previous_Instance
9589 (Generic_Parent (Parent (Inst)));
9590 Scop : Entity_Id;
9591
9592 begin
9593 if Present (Par_I)
9594 and then Earlier (N, Freeze_Node (Par_I))
9595 then
9596 Scop := Scope (Inst);
9597
9598 -- If the current instance is within the one that contains
9599 -- the generic, the freeze node for the current one must
9600 -- appear in the current declarative part. Ditto, if the
9601 -- current instance is within another package instance or
9602 -- within a body that does not enclose the current instance.
9603 -- In these three cases the freeze node of the previous
9604 -- instance is not relevant.
9605
9606 while Present (Scop) and then Scop /= Standard_Standard loop
9607 exit when Scop = Par_I
9608 or else
9609 (Is_Generic_Instance (Scop)
9610 and then Scope_Depth (Scop) > Scope_Depth (Par_I));
9611 Scop := Scope (Scop);
9612 end loop;
9613
9614 -- Previous instance encloses current instance
9615
9616 if Scop = Par_I then
9617 null;
9618
9619 -- If the next node is a source body we must freeze in
9620 -- the current scope as well.
9621
9622 elsif Present (Next (N))
9623 and then Nkind_In (Next (N), N_Subprogram_Body,
9624 N_Package_Body)
9625 and then Comes_From_Source (Next (N))
9626 then
9627 null;
9628
9629 -- Current instance is within an unrelated instance
9630
9631 elsif Is_Generic_Instance (Scop) then
9632 null;
9633
9634 -- Current instance is within an unrelated body
9635
9636 elsif Present (Enclosing_N)
9637 and then Enclosing_N /= Enclosing_Body (Par_I)
9638 then
9639 null;
9640
9641 else
9642 Insert_After (Freeze_Node (Par_I), F_Node);
9643 return;
9644 end if;
9645 end if;
9646 end;
9647 end if;
9648
9649 -- When the instantiation occurs in a package declaration, append the
9650 -- freeze node to the private declarations (if any).
9651
9652 if Nkind (Par_N) = N_Package_Specification
9653 and then Decls = Visible_Declarations (Par_N)
9654 and then Present (Private_Declarations (Par_N))
9655 and then not Is_Empty_List (Private_Declarations (Par_N))
9656 then
9657 Decls := Private_Declarations (Par_N);
9658 Decl := First (Decls);
9659 end if;
9660
9661 -- Determine the proper freeze point of a package instantiation. We
9662 -- adhere to the general rule of a package or subprogram body causing
9663 -- freezing of anything before it in the same declarative region. In
9664 -- this case, the proper freeze point of a package instantiation is
9665 -- before the first source body which follows, or before a stub. This
9666 -- ensures that entities coming from the instance are already frozen
9667 -- and usable in source bodies.
9668
9669 if Nkind (Par_N) /= N_Package_Declaration
9670 and then Ekind (Inst) = E_Package
9671 and then Is_Generic_Instance (Inst)
9672 and then
9673 not In_Same_Source_Unit (Generic_Parent (Parent (Inst)), Inst)
9674 then
9675 while Present (Decl) loop
9676 if (Nkind (Decl) in N_Unit_Body
9677 or else
9678 Nkind (Decl) in N_Body_Stub)
9679 and then Comes_From_Source (Decl)
9680 then
9681 Insert_Before (Decl, F_Node);
9682 return;
9683 end if;
9684
9685 Next (Decl);
9686 end loop;
9687 end if;
9688
9689 -- In a package declaration, or if no previous body, insert at end
9690 -- of list.
9691
9692 Set_Sloc (F_Node, Sloc (Last (Decls)));
9693 Insert_After (Last (Decls), F_Node);
9694 end if;
9695 end Insert_Freeze_Node_For_Instance;
9696
9697 ------------------
9698 -- Install_Body --
9699 ------------------
9700
9701 procedure Install_Body
9702 (Act_Body : Node_Id;
9703 N : Node_Id;
9704 Gen_Body : Node_Id;
9705 Gen_Decl : Node_Id)
9706 is
9707 function In_Same_Scope (Gen_Id, Act_Id : Node_Id) return Boolean;
9708 -- Check if the generic definition and the instantiation come from
9709 -- a common scope, in which case the instance must be frozen after
9710 -- the generic body.
9711
9712 function True_Sloc (N, Act_Unit : Node_Id) return Source_Ptr;
9713 -- If the instance is nested inside a generic unit, the Sloc of the
9714 -- instance indicates the place of the original definition, not the
9715 -- point of the current enclosing instance. Pending a better usage of
9716 -- Slocs to indicate instantiation places, we determine the place of
9717 -- origin of a node by finding the maximum sloc of any ancestor node.
9718 -- Why is this not equivalent to Top_Level_Location ???
9719
9720 -------------------
9721 -- In_Same_Scope --
9722 -------------------
9723
9724 function In_Same_Scope (Gen_Id, Act_Id : Node_Id) return Boolean is
9725 Act_Scop : Entity_Id := Scope (Act_Id);
9726 Gen_Scop : Entity_Id := Scope (Gen_Id);
9727
9728 begin
9729 while Act_Scop /= Standard_Standard
9730 and then Gen_Scop /= Standard_Standard
9731 loop
9732 if Act_Scop = Gen_Scop then
9733 return True;
9734 end if;
9735
9736 Act_Scop := Scope (Act_Scop);
9737 Gen_Scop := Scope (Gen_Scop);
9738 end loop;
9739
9740 return False;
9741 end In_Same_Scope;
9742
9743 ---------------
9744 -- True_Sloc --
9745 ---------------
9746
9747 function True_Sloc (N, Act_Unit : Node_Id) return Source_Ptr is
9748 N1 : Node_Id;
9749 Res : Source_Ptr;
9750
9751 begin
9752 Res := Sloc (N);
9753 N1 := N;
9754 while Present (N1) and then N1 /= Act_Unit loop
9755 if Sloc (N1) > Res then
9756 Res := Sloc (N1);
9757 end if;
9758
9759 N1 := Parent (N1);
9760 end loop;
9761
9762 return Res;
9763 end True_Sloc;
9764
9765 Act_Id : constant Entity_Id := Corresponding_Spec (Act_Body);
9766 Act_Unit : constant Node_Id := Unit (Cunit (Get_Source_Unit (N)));
9767 Gen_Id : constant Entity_Id := Corresponding_Spec (Gen_Body);
9768 Par : constant Entity_Id := Scope (Gen_Id);
9769 Gen_Unit : constant Node_Id :=
9770 Unit (Cunit (Get_Source_Unit (Gen_Decl)));
9771
9772 Body_Unit : Node_Id;
9773 F_Node : Node_Id;
9774 Must_Delay : Boolean;
9775 Orig_Body : Node_Id := Gen_Body;
9776
9777 -- Start of processing for Install_Body
9778
9779 begin
9780 -- Handle first the case of an instance with incomplete actual types.
9781 -- The instance body cannot be placed after the declaration because
9782 -- full views have not been seen yet. Any use of the non-limited views
9783 -- in the instance body requires the presence of a regular with_clause
9784 -- in the enclosing unit, and will fail if this with_clause is missing.
9785 -- We place the instance body at the beginning of the enclosing body,
9786 -- which is the unit being compiled. The freeze node for the instance
9787 -- is then placed after the instance body.
9788
9789 if not Is_Empty_Elmt_List (Incomplete_Actuals (Act_Id))
9790 and then Expander_Active
9791 and then Ekind (Scope (Act_Id)) = E_Package
9792 then
9793 declare
9794 Scop : constant Entity_Id := Scope (Act_Id);
9795 Body_Id : constant Node_Id :=
9796 Corresponding_Body (Unit_Declaration_Node (Scop));
9797
9798 begin
9799 Ensure_Freeze_Node (Act_Id);
9800 F_Node := Freeze_Node (Act_Id);
9801 if Present (Body_Id) then
9802 Set_Is_Frozen (Act_Id, False);
9803 Prepend (Act_Body, Declarations (Parent (Body_Id)));
9804 if Is_List_Member (F_Node) then
9805 Remove (F_Node);
9806 end if;
9807
9808 Insert_After (Act_Body, F_Node);
9809 end if;
9810 end;
9811 return;
9812 end if;
9813
9814 -- If the body is a subunit, the freeze point is the corresponding stub
9815 -- in the current compilation, not the subunit itself.
9816
9817 if Nkind (Parent (Gen_Body)) = N_Subunit then
9818 Orig_Body := Corresponding_Stub (Parent (Gen_Body));
9819 else
9820 Orig_Body := Gen_Body;
9821 end if;
9822
9823 Body_Unit := Unit (Cunit (Get_Source_Unit (Orig_Body)));
9824
9825 -- If the instantiation and the generic definition appear in the same
9826 -- package declaration, this is an early instantiation. If they appear
9827 -- in the same declarative part, it is an early instantiation only if
9828 -- the generic body appears textually later, and the generic body is
9829 -- also in the main unit.
9830
9831 -- If instance is nested within a subprogram, and the generic body
9832 -- is not, the instance is delayed because the enclosing body is. If
9833 -- instance and body are within the same scope, or the same subprogram
9834 -- body, indicate explicitly that the instance is delayed.
9835
9836 Must_Delay :=
9837 (Gen_Unit = Act_Unit
9838 and then (Nkind_In (Gen_Unit, N_Generic_Package_Declaration,
9839 N_Package_Declaration)
9840 or else (Gen_Unit = Body_Unit
9841 and then True_Sloc (N, Act_Unit) <
9842 Sloc (Orig_Body)))
9843 and then Is_In_Main_Unit (Original_Node (Gen_Unit))
9844 and then In_Same_Scope (Gen_Id, Act_Id));
9845
9846 -- If this is an early instantiation, the freeze node is placed after
9847 -- the generic body. Otherwise, if the generic appears in an instance,
9848 -- we cannot freeze the current instance until the outer one is frozen.
9849 -- This is only relevant if the current instance is nested within some
9850 -- inner scope not itself within the outer instance. If this scope is
9851 -- a package body in the same declarative part as the outer instance,
9852 -- then that body needs to be frozen after the outer instance. Finally,
9853 -- if no delay is needed, we place the freeze node at the end of the
9854 -- current declarative part.
9855
9856 if Expander_Active
9857 and then (No (Freeze_Node (Act_Id))
9858 or else not Is_List_Member (Freeze_Node (Act_Id)))
9859 then
9860 Ensure_Freeze_Node (Act_Id);
9861 F_Node := Freeze_Node (Act_Id);
9862
9863 if Must_Delay then
9864 Insert_After (Orig_Body, F_Node);
9865
9866 elsif Is_Generic_Instance (Par)
9867 and then Present (Freeze_Node (Par))
9868 and then Scope (Act_Id) /= Par
9869 then
9870 -- Freeze instance of inner generic after instance of enclosing
9871 -- generic.
9872
9873 if In_Same_Declarative_Part (Parent (Freeze_Node (Par)), N) then
9874
9875 -- Handle the following case:
9876
9877 -- package Parent_Inst is new ...
9878 -- Parent_Inst []
9879
9880 -- procedure P ... -- this body freezes Parent_Inst
9881
9882 -- package Inst is new ...
9883
9884 -- In this particular scenario, the freeze node for Inst must
9885 -- be inserted in the same manner as that of Parent_Inst,
9886 -- before the next source body or at the end of the declarative
9887 -- list (body not available). If body P did not exist and
9888 -- Parent_Inst was frozen after Inst, either by a body
9889 -- following Inst or at the end of the declarative region,
9890 -- the freeze node for Inst must be inserted after that of
9891 -- Parent_Inst. This relation is established by comparing
9892 -- the Slocs of Parent_Inst freeze node and Inst.
9893 -- We examine the parents of the enclosing lists to handle
9894 -- the case where the parent instance is in the visible part
9895 -- of a package declaration, and the inner instance is in
9896 -- the corresponding private part.
9897
9898 if Parent (List_Containing (Get_Unit_Instantiation_Node (Par)))
9899 = Parent (List_Containing (N))
9900 and then Sloc (Freeze_Node (Par)) < Sloc (N)
9901 then
9902 Insert_Freeze_Node_For_Instance (N, F_Node);
9903 else
9904 Insert_After (Freeze_Node (Par), F_Node);
9905 end if;
9906
9907 -- Freeze package enclosing instance of inner generic after
9908 -- instance of enclosing generic.
9909
9910 elsif Nkind_In (Parent (N), N_Package_Body, N_Subprogram_Body)
9911 and then In_Same_Declarative_Part
9912 (Parent (Freeze_Node (Par)), Parent (N))
9913 then
9914 declare
9915 Enclosing : Entity_Id;
9916
9917 begin
9918 Enclosing := Corresponding_Spec (Parent (N));
9919
9920 if No (Enclosing) then
9921 Enclosing := Defining_Entity (Parent (N));
9922 end if;
9923
9924 Insert_Freeze_Node_For_Instance (N, F_Node);
9925 Ensure_Freeze_Node (Enclosing);
9926
9927 if not Is_List_Member (Freeze_Node (Enclosing)) then
9928
9929 -- The enclosing context is a subunit, insert the freeze
9930 -- node after the stub.
9931
9932 if Nkind (Parent (Parent (N))) = N_Subunit then
9933 Insert_Freeze_Node_For_Instance
9934 (Corresponding_Stub (Parent (Parent (N))),
9935 Freeze_Node (Enclosing));
9936
9937 -- The enclosing context is a package with a stub body
9938 -- which has already been replaced by the real body.
9939 -- Insert the freeze node after the actual body.
9940
9941 elsif Ekind (Enclosing) = E_Package
9942 and then Present (Body_Entity (Enclosing))
9943 and then Was_Originally_Stub
9944 (Parent (Body_Entity (Enclosing)))
9945 then
9946 Insert_Freeze_Node_For_Instance
9947 (Parent (Body_Entity (Enclosing)),
9948 Freeze_Node (Enclosing));
9949
9950 -- The parent instance has been frozen before the body of
9951 -- the enclosing package, insert the freeze node after
9952 -- the body.
9953
9954 elsif List_Containing (Freeze_Node (Par)) =
9955 List_Containing (Parent (N))
9956 and then Sloc (Freeze_Node (Par)) < Sloc (Parent (N))
9957 then
9958 Insert_Freeze_Node_For_Instance
9959 (Parent (N), Freeze_Node (Enclosing));
9960
9961 else
9962 Insert_After
9963 (Freeze_Node (Par), Freeze_Node (Enclosing));
9964 end if;
9965 end if;
9966 end;
9967
9968 else
9969 Insert_Freeze_Node_For_Instance (N, F_Node);
9970 end if;
9971
9972 else
9973 Insert_Freeze_Node_For_Instance (N, F_Node);
9974 end if;
9975 end if;
9976
9977 Set_Is_Frozen (Act_Id);
9978 Insert_Before (N, Act_Body);
9979 Mark_Rewrite_Insertion (Act_Body);
9980 end Install_Body;
9981
9982 -----------------------------
9983 -- Install_Formal_Packages --
9984 -----------------------------
9985
9986 procedure Install_Formal_Packages (Par : Entity_Id) is
9987 E : Entity_Id;
9988 Gen : Entity_Id;
9989 Gen_E : Entity_Id := Empty;
9990
9991 begin
9992 E := First_Entity (Par);
9993
9994 -- If we are installing an instance parent, locate the formal packages
9995 -- of its generic parent.
9996
9997 if Is_Generic_Instance (Par) then
9998 Gen := Generic_Parent (Package_Specification (Par));
9999 Gen_E := First_Entity (Gen);
10000 end if;
10001
10002 while Present (E) loop
10003 if Ekind (E) = E_Package
10004 and then Nkind (Parent (E)) = N_Package_Renaming_Declaration
10005 then
10006 -- If this is the renaming for the parent instance, done
10007
10008 if Renamed_Object (E) = Par then
10009 exit;
10010
10011 -- The visibility of a formal of an enclosing generic is already
10012 -- correct.
10013
10014 elsif Denotes_Formal_Package (E) then
10015 null;
10016
10017 elsif Present (Associated_Formal_Package (E)) then
10018 Check_Generic_Actuals (Renamed_Object (E), True);
10019 Set_Is_Hidden (E, False);
10020
10021 -- Find formal package in generic unit that corresponds to
10022 -- (instance of) formal package in instance.
10023
10024 while Present (Gen_E) and then Chars (Gen_E) /= Chars (E) loop
10025 Next_Entity (Gen_E);
10026 end loop;
10027
10028 if Present (Gen_E) then
10029 Map_Formal_Package_Entities (Gen_E, E);
10030 end if;
10031 end if;
10032 end if;
10033
10034 Next_Entity (E);
10035
10036 if Present (Gen_E) then
10037 Next_Entity (Gen_E);
10038 end if;
10039 end loop;
10040 end Install_Formal_Packages;
10041
10042 --------------------
10043 -- Install_Parent --
10044 --------------------
10045
10046 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False) is
10047 Ancestors : constant Elist_Id := New_Elmt_List;
10048 S : constant Entity_Id := Current_Scope;
10049 Inst_Par : Entity_Id;
10050 First_Par : Entity_Id;
10051 Inst_Node : Node_Id;
10052 Gen_Par : Entity_Id;
10053 First_Gen : Entity_Id;
10054 Elmt : Elmt_Id;
10055
10056 procedure Install_Noninstance_Specs (Par : Entity_Id);
10057 -- Install the scopes of noninstance parent units ending with Par
10058
10059 procedure Install_Spec (Par : Entity_Id);
10060 -- The child unit is within the declarative part of the parent, so the
10061 -- declarations within the parent are immediately visible.
10062
10063 -------------------------------
10064 -- Install_Noninstance_Specs --
10065 -------------------------------
10066
10067 procedure Install_Noninstance_Specs (Par : Entity_Id) is
10068 begin
10069 if Present (Par)
10070 and then Par /= Standard_Standard
10071 and then not In_Open_Scopes (Par)
10072 then
10073 Install_Noninstance_Specs (Scope (Par));
10074 Install_Spec (Par);
10075 end if;
10076 end Install_Noninstance_Specs;
10077
10078 ------------------
10079 -- Install_Spec --
10080 ------------------
10081
10082 procedure Install_Spec (Par : Entity_Id) is
10083 Spec : constant Node_Id := Package_Specification (Par);
10084
10085 begin
10086 -- If this parent of the child instance is a top-level unit,
10087 -- then record the unit and its visibility for later resetting in
10088 -- Remove_Parent. We exclude units that are generic instances, as we
10089 -- only want to record this information for the ultimate top-level
10090 -- noninstance parent (is that always correct???).
10091
10092 if Scope (Par) = Standard_Standard
10093 and then not Is_Generic_Instance (Par)
10094 then
10095 Parent_Unit_Visible := Is_Immediately_Visible (Par);
10096 Instance_Parent_Unit := Par;
10097 end if;
10098
10099 -- Open the parent scope and make it and its declarations visible.
10100 -- If this point is not within a body, then only the visible
10101 -- declarations should be made visible, and installation of the
10102 -- private declarations is deferred until the appropriate point
10103 -- within analysis of the spec being instantiated (see the handling
10104 -- of parent visibility in Analyze_Package_Specification). This is
10105 -- relaxed in the case where the parent unit is Ada.Tags, to avoid
10106 -- private view problems that occur when compiling instantiations of
10107 -- a generic child of that package (Generic_Dispatching_Constructor).
10108 -- If the instance freezes a tagged type, inlinings of operations
10109 -- from Ada.Tags may need the full view of type Tag. If inlining took
10110 -- proper account of establishing visibility of inlined subprograms'
10111 -- parents then it should be possible to remove this
10112 -- special check. ???
10113
10114 Push_Scope (Par);
10115 Set_Is_Immediately_Visible (Par);
10116 Install_Visible_Declarations (Par);
10117 Set_Use (Visible_Declarations (Spec));
10118
10119 if In_Body or else Is_RTU (Par, Ada_Tags) then
10120 Install_Private_Declarations (Par);
10121 Set_Use (Private_Declarations (Spec));
10122 end if;
10123 end Install_Spec;
10124
10125 -- Start of processing for Install_Parent
10126
10127 begin
10128 -- We need to install the parent instance to compile the instantiation
10129 -- of the child, but the child instance must appear in the current
10130 -- scope. Given that we cannot place the parent above the current scope
10131 -- in the scope stack, we duplicate the current scope and unstack both
10132 -- after the instantiation is complete.
10133
10134 -- If the parent is itself the instantiation of a child unit, we must
10135 -- also stack the instantiation of its parent, and so on. Each such
10136 -- ancestor is the prefix of the name in a prior instantiation.
10137
10138 -- If this is a nested instance, the parent unit itself resolves to
10139 -- a renaming of the parent instance, whose declaration we need.
10140
10141 -- Finally, the parent may be a generic (not an instance) when the
10142 -- child unit appears as a formal package.
10143
10144 Inst_Par := P;
10145
10146 if Present (Renamed_Entity (Inst_Par)) then
10147 Inst_Par := Renamed_Entity (Inst_Par);
10148 end if;
10149
10150 First_Par := Inst_Par;
10151
10152 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
10153
10154 First_Gen := Gen_Par;
10155
10156 while Present (Gen_Par) and then Is_Child_Unit (Gen_Par) loop
10157
10158 -- Load grandparent instance as well
10159
10160 Inst_Node := Get_Unit_Instantiation_Node (Inst_Par);
10161
10162 if Nkind (Name (Inst_Node)) = N_Expanded_Name then
10163 Inst_Par := Entity (Prefix (Name (Inst_Node)));
10164
10165 if Present (Renamed_Entity (Inst_Par)) then
10166 Inst_Par := Renamed_Entity (Inst_Par);
10167 end if;
10168
10169 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
10170
10171 if Present (Gen_Par) then
10172 Prepend_Elmt (Inst_Par, Ancestors);
10173
10174 else
10175 -- Parent is not the name of an instantiation
10176
10177 Install_Noninstance_Specs (Inst_Par);
10178 exit;
10179 end if;
10180
10181 else
10182 -- Previous error
10183
10184 exit;
10185 end if;
10186 end loop;
10187
10188 if Present (First_Gen) then
10189 Append_Elmt (First_Par, Ancestors);
10190 else
10191 Install_Noninstance_Specs (First_Par);
10192 end if;
10193
10194 if not Is_Empty_Elmt_List (Ancestors) then
10195 Elmt := First_Elmt (Ancestors);
10196 while Present (Elmt) loop
10197 Install_Spec (Node (Elmt));
10198 Install_Formal_Packages (Node (Elmt));
10199 Next_Elmt (Elmt);
10200 end loop;
10201 end if;
10202
10203 if not In_Body then
10204 Push_Scope (S);
10205 end if;
10206 end Install_Parent;
10207
10208 -------------------------------
10209 -- Install_Hidden_Primitives --
10210 -------------------------------
10211
10212 procedure Install_Hidden_Primitives
10213 (Prims_List : in out Elist_Id;
10214 Gen_T : Entity_Id;
10215 Act_T : Entity_Id)
10216 is
10217 Elmt : Elmt_Id;
10218 List : Elist_Id := No_Elist;
10219 Prim_G_Elmt : Elmt_Id;
10220 Prim_A_Elmt : Elmt_Id;
10221 Prim_G : Node_Id;
10222 Prim_A : Node_Id;
10223
10224 begin
10225 -- No action needed in case of serious errors because we cannot trust
10226 -- in the order of primitives
10227
10228 if Serious_Errors_Detected > 0 then
10229 return;
10230
10231 -- No action possible if we don't have available the list of primitive
10232 -- operations
10233
10234 elsif No (Gen_T)
10235 or else not Is_Record_Type (Gen_T)
10236 or else not Is_Tagged_Type (Gen_T)
10237 or else not Is_Record_Type (Act_T)
10238 or else not Is_Tagged_Type (Act_T)
10239 then
10240 return;
10241
10242 -- There is no need to handle interface types since their primitives
10243 -- cannot be hidden
10244
10245 elsif Is_Interface (Gen_T) then
10246 return;
10247 end if;
10248
10249 Prim_G_Elmt := First_Elmt (Primitive_Operations (Gen_T));
10250
10251 if not Is_Class_Wide_Type (Act_T) then
10252 Prim_A_Elmt := First_Elmt (Primitive_Operations (Act_T));
10253 else
10254 Prim_A_Elmt := First_Elmt (Primitive_Operations (Root_Type (Act_T)));
10255 end if;
10256
10257 loop
10258 -- Skip predefined primitives in the generic formal
10259
10260 while Present (Prim_G_Elmt)
10261 and then Is_Predefined_Dispatching_Operation (Node (Prim_G_Elmt))
10262 loop
10263 Next_Elmt (Prim_G_Elmt);
10264 end loop;
10265
10266 -- Skip predefined primitives in the generic actual
10267
10268 while Present (Prim_A_Elmt)
10269 and then Is_Predefined_Dispatching_Operation (Node (Prim_A_Elmt))
10270 loop
10271 Next_Elmt (Prim_A_Elmt);
10272 end loop;
10273
10274 exit when No (Prim_G_Elmt) or else No (Prim_A_Elmt);
10275
10276 Prim_G := Node (Prim_G_Elmt);
10277 Prim_A := Node (Prim_A_Elmt);
10278
10279 -- There is no need to handle interface primitives because their
10280 -- primitives are not hidden
10281
10282 exit when Present (Interface_Alias (Prim_G));
10283
10284 -- Here we install one hidden primitive
10285
10286 if Chars (Prim_G) /= Chars (Prim_A)
10287 and then Has_Suffix (Prim_A, 'P')
10288 and then Remove_Suffix (Prim_A, 'P') = Chars (Prim_G)
10289 then
10290 Set_Chars (Prim_A, Chars (Prim_G));
10291 Append_New_Elmt (Prim_A, To => List);
10292 end if;
10293
10294 Next_Elmt (Prim_A_Elmt);
10295 Next_Elmt (Prim_G_Elmt);
10296 end loop;
10297
10298 -- Append the elements to the list of temporarily visible primitives
10299 -- avoiding duplicates.
10300
10301 if Present (List) then
10302 if No (Prims_List) then
10303 Prims_List := New_Elmt_List;
10304 end if;
10305
10306 Elmt := First_Elmt (List);
10307 while Present (Elmt) loop
10308 Append_Unique_Elmt (Node (Elmt), Prims_List);
10309 Next_Elmt (Elmt);
10310 end loop;
10311 end if;
10312 end Install_Hidden_Primitives;
10313
10314 -------------------------------
10315 -- Restore_Hidden_Primitives --
10316 -------------------------------
10317
10318 procedure Restore_Hidden_Primitives (Prims_List : in out Elist_Id) is
10319 Prim_Elmt : Elmt_Id;
10320 Prim : Node_Id;
10321
10322 begin
10323 if Prims_List /= No_Elist then
10324 Prim_Elmt := First_Elmt (Prims_List);
10325 while Present (Prim_Elmt) loop
10326 Prim := Node (Prim_Elmt);
10327 Set_Chars (Prim, Add_Suffix (Prim, 'P'));
10328 Next_Elmt (Prim_Elmt);
10329 end loop;
10330
10331 Prims_List := No_Elist;
10332 end if;
10333 end Restore_Hidden_Primitives;
10334
10335 --------------------------------
10336 -- Instantiate_Formal_Package --
10337 --------------------------------
10338
10339 function Instantiate_Formal_Package
10340 (Formal : Node_Id;
10341 Actual : Node_Id;
10342 Analyzed_Formal : Node_Id) return List_Id
10343 is
10344 Loc : constant Source_Ptr := Sloc (Actual);
10345 Hidden_Formals : constant Elist_Id := New_Elmt_List;
10346 Actual_Pack : Entity_Id;
10347 Formal_Pack : Entity_Id;
10348 Gen_Parent : Entity_Id;
10349 Decls : List_Id;
10350 Nod : Node_Id;
10351 Parent_Spec : Node_Id;
10352
10353 procedure Find_Matching_Actual
10354 (F : Node_Id;
10355 Act : in out Entity_Id);
10356 -- We need to associate each formal entity in the formal package with
10357 -- the corresponding entity in the actual package. The actual package
10358 -- has been analyzed and possibly expanded, and as a result there is
10359 -- no one-to-one correspondence between the two lists (for example,
10360 -- the actual may include subtypes, itypes, and inherited primitive
10361 -- operations, interspersed among the renaming declarations for the
10362 -- actuals). We retrieve the corresponding actual by name because each
10363 -- actual has the same name as the formal, and they do appear in the
10364 -- same order.
10365
10366 function Get_Formal_Entity (N : Node_Id) return Entity_Id;
10367 -- Retrieve entity of defining entity of generic formal parameter.
10368 -- Only the declarations of formals need to be considered when
10369 -- linking them to actuals, but the declarative list may include
10370 -- internal entities generated during analysis, and those are ignored.
10371
10372 procedure Match_Formal_Entity
10373 (Formal_Node : Node_Id;
10374 Formal_Ent : Entity_Id;
10375 Actual_Ent : Entity_Id);
10376 -- Associates the formal entity with the actual. In the case where
10377 -- Formal_Ent is a formal package, this procedure iterates through all
10378 -- of its formals and enters associations between the actuals occurring
10379 -- in the formal package's corresponding actual package (given by
10380 -- Actual_Ent) and the formal package's formal parameters. This
10381 -- procedure recurses if any of the parameters is itself a package.
10382
10383 function Is_Instance_Of
10384 (Act_Spec : Entity_Id;
10385 Gen_Anc : Entity_Id) return Boolean;
10386 -- The actual can be an instantiation of a generic within another
10387 -- instance, in which case there is no direct link from it to the
10388 -- original generic ancestor. In that case, we recognize that the
10389 -- ultimate ancestor is the same by examining names and scopes.
10390
10391 procedure Process_Nested_Formal (Formal : Entity_Id);
10392 -- If the current formal is declared with a box, its own formals are
10393 -- visible in the instance, as they were in the generic, and their
10394 -- Hidden flag must be reset. If some of these formals are themselves
10395 -- packages declared with a box, the processing must be recursive.
10396
10397 --------------------------
10398 -- Find_Matching_Actual --
10399 --------------------------
10400
10401 procedure Find_Matching_Actual
10402 (F : Node_Id;
10403 Act : in out Entity_Id)
10404 is
10405 Formal_Ent : Entity_Id;
10406
10407 begin
10408 case Nkind (Original_Node (F)) is
10409 when N_Formal_Object_Declaration
10410 | N_Formal_Type_Declaration
10411 =>
10412 Formal_Ent := Defining_Identifier (F);
10413
10414 while Present (Act)
10415 and then Chars (Act) /= Chars (Formal_Ent)
10416 loop
10417 Next_Entity (Act);
10418 end loop;
10419
10420 when N_Formal_Package_Declaration
10421 | N_Formal_Subprogram_Declaration
10422 | N_Generic_Package_Declaration
10423 | N_Package_Declaration
10424 =>
10425 Formal_Ent := Defining_Entity (F);
10426
10427 while Present (Act)
10428 and then Chars (Act) /= Chars (Formal_Ent)
10429 loop
10430 Next_Entity (Act);
10431 end loop;
10432
10433 when others =>
10434 raise Program_Error;
10435 end case;
10436 end Find_Matching_Actual;
10437
10438 -------------------------
10439 -- Match_Formal_Entity --
10440 -------------------------
10441
10442 procedure Match_Formal_Entity
10443 (Formal_Node : Node_Id;
10444 Formal_Ent : Entity_Id;
10445 Actual_Ent : Entity_Id)
10446 is
10447 Act_Pkg : Entity_Id;
10448
10449 begin
10450 Set_Instance_Of (Formal_Ent, Actual_Ent);
10451
10452 if Ekind (Actual_Ent) = E_Package then
10453
10454 -- Record associations for each parameter
10455
10456 Act_Pkg := Actual_Ent;
10457
10458 declare
10459 A_Ent : Entity_Id := First_Entity (Act_Pkg);
10460 F_Ent : Entity_Id;
10461 F_Node : Node_Id;
10462
10463 Gen_Decl : Node_Id;
10464 Formals : List_Id;
10465 Actual : Entity_Id;
10466
10467 begin
10468 -- Retrieve the actual given in the formal package declaration
10469
10470 Actual := Entity (Name (Original_Node (Formal_Node)));
10471
10472 -- The actual in the formal package declaration may be a
10473 -- renamed generic package, in which case we want to retrieve
10474 -- the original generic in order to traverse its formal part.
10475
10476 if Present (Renamed_Entity (Actual)) then
10477 Gen_Decl := Unit_Declaration_Node (Renamed_Entity (Actual));
10478 else
10479 Gen_Decl := Unit_Declaration_Node (Actual);
10480 end if;
10481
10482 Formals := Generic_Formal_Declarations (Gen_Decl);
10483
10484 if Present (Formals) then
10485 F_Node := First_Non_Pragma (Formals);
10486 else
10487 F_Node := Empty;
10488 end if;
10489
10490 while Present (A_Ent)
10491 and then Present (F_Node)
10492 and then A_Ent /= First_Private_Entity (Act_Pkg)
10493 loop
10494 F_Ent := Get_Formal_Entity (F_Node);
10495
10496 if Present (F_Ent) then
10497
10498 -- This is a formal of the original package. Record
10499 -- association and recurse.
10500
10501 Find_Matching_Actual (F_Node, A_Ent);
10502 Match_Formal_Entity (F_Node, F_Ent, A_Ent);
10503 Next_Entity (A_Ent);
10504 end if;
10505
10506 Next_Non_Pragma (F_Node);
10507 end loop;
10508 end;
10509 end if;
10510 end Match_Formal_Entity;
10511
10512 -----------------------
10513 -- Get_Formal_Entity --
10514 -----------------------
10515
10516 function Get_Formal_Entity (N : Node_Id) return Entity_Id is
10517 Kind : constant Node_Kind := Nkind (Original_Node (N));
10518 begin
10519 case Kind is
10520 when N_Formal_Object_Declaration =>
10521 return Defining_Identifier (N);
10522
10523 when N_Formal_Type_Declaration =>
10524 return Defining_Identifier (N);
10525
10526 when N_Formal_Subprogram_Declaration =>
10527 return Defining_Unit_Name (Specification (N));
10528
10529 when N_Formal_Package_Declaration =>
10530 return Defining_Identifier (Original_Node (N));
10531
10532 when N_Generic_Package_Declaration =>
10533 return Defining_Identifier (Original_Node (N));
10534
10535 -- All other declarations are introduced by semantic analysis and
10536 -- have no match in the actual.
10537
10538 when others =>
10539 return Empty;
10540 end case;
10541 end Get_Formal_Entity;
10542
10543 --------------------
10544 -- Is_Instance_Of --
10545 --------------------
10546
10547 function Is_Instance_Of
10548 (Act_Spec : Entity_Id;
10549 Gen_Anc : Entity_Id) return Boolean
10550 is
10551 Gen_Par : constant Entity_Id := Generic_Parent (Act_Spec);
10552
10553 begin
10554 if No (Gen_Par) then
10555 return False;
10556
10557 -- Simplest case: the generic parent of the actual is the formal
10558
10559 elsif Gen_Par = Gen_Anc then
10560 return True;
10561
10562 elsif Chars (Gen_Par) /= Chars (Gen_Anc) then
10563 return False;
10564
10565 -- The actual may be obtained through several instantiations. Its
10566 -- scope must itself be an instance of a generic declared in the
10567 -- same scope as the formal. Any other case is detected above.
10568
10569 elsif not Is_Generic_Instance (Scope (Gen_Par)) then
10570 return False;
10571
10572 else
10573 return Generic_Parent (Parent (Scope (Gen_Par))) = Scope (Gen_Anc);
10574 end if;
10575 end Is_Instance_Of;
10576
10577 ---------------------------
10578 -- Process_Nested_Formal --
10579 ---------------------------
10580
10581 procedure Process_Nested_Formal (Formal : Entity_Id) is
10582 Ent : Entity_Id;
10583
10584 begin
10585 if Present (Associated_Formal_Package (Formal))
10586 and then Box_Present (Parent (Associated_Formal_Package (Formal)))
10587 then
10588 Ent := First_Entity (Formal);
10589 while Present (Ent) loop
10590 Set_Is_Hidden (Ent, False);
10591 Set_Is_Visible_Formal (Ent);
10592 Set_Is_Potentially_Use_Visible
10593 (Ent, Is_Potentially_Use_Visible (Formal));
10594
10595 if Ekind (Ent) = E_Package then
10596 exit when Renamed_Entity (Ent) = Renamed_Entity (Formal);
10597 Process_Nested_Formal (Ent);
10598 end if;
10599
10600 Next_Entity (Ent);
10601 end loop;
10602 end if;
10603 end Process_Nested_Formal;
10604
10605 -- Start of processing for Instantiate_Formal_Package
10606
10607 begin
10608 Analyze (Actual);
10609
10610 -- The actual must be a package instance, or else a current instance
10611 -- such as a parent generic within the body of a generic child.
10612
10613 if not Is_Entity_Name (Actual)
10614 or else not Is_Package_Or_Generic_Package (Entity (Actual))
10615 then
10616 Error_Msg_N
10617 ("expect package instance to instantiate formal", Actual);
10618 Abandon_Instantiation (Actual);
10619 raise Program_Error;
10620
10621 else
10622 Actual_Pack := Entity (Actual);
10623 Set_Is_Instantiated (Actual_Pack);
10624
10625 -- The actual may be a renamed package, or an outer generic formal
10626 -- package whose instantiation is converted into a renaming.
10627
10628 if Present (Renamed_Object (Actual_Pack)) then
10629 Actual_Pack := Renamed_Object (Actual_Pack);
10630 end if;
10631
10632 if Nkind (Analyzed_Formal) = N_Formal_Package_Declaration then
10633 Gen_Parent := Get_Instance_Of (Entity (Name (Analyzed_Formal)));
10634 Formal_Pack := Defining_Identifier (Analyzed_Formal);
10635 else
10636 Gen_Parent :=
10637 Generic_Parent (Specification (Analyzed_Formal));
10638 Formal_Pack :=
10639 Defining_Unit_Name (Specification (Analyzed_Formal));
10640 end if;
10641
10642 if Nkind (Parent (Actual_Pack)) = N_Defining_Program_Unit_Name then
10643 Parent_Spec := Package_Specification (Actual_Pack);
10644 else
10645 Parent_Spec := Parent (Actual_Pack);
10646 end if;
10647
10648 if Gen_Parent = Any_Id then
10649 Error_Msg_N
10650 ("previous error in declaration of formal package", Actual);
10651 Abandon_Instantiation (Actual);
10652
10653 elsif Is_Instance_Of (Parent_Spec, Get_Instance_Of (Gen_Parent)) then
10654 null;
10655
10656 -- If this is the current instance of an enclosing generic, that unit
10657 -- is the generic package we need.
10658
10659 elsif In_Open_Scopes (Actual_Pack)
10660 and then Ekind (Actual_Pack) = E_Generic_Package
10661 then
10662 null;
10663
10664 else
10665 Error_Msg_NE
10666 ("actual parameter must be instance of&", Actual, Gen_Parent);
10667 Abandon_Instantiation (Actual);
10668 end if;
10669
10670 Set_Instance_Of (Defining_Identifier (Formal), Actual_Pack);
10671 Map_Formal_Package_Entities (Formal_Pack, Actual_Pack);
10672
10673 Nod :=
10674 Make_Package_Renaming_Declaration (Loc,
10675 Defining_Unit_Name => New_Copy (Defining_Identifier (Formal)),
10676 Name => New_Occurrence_Of (Actual_Pack, Loc));
10677
10678 Set_Associated_Formal_Package
10679 (Defining_Unit_Name (Nod), Defining_Identifier (Formal));
10680 Decls := New_List (Nod);
10681
10682 -- If the formal F has a box, then the generic declarations are
10683 -- visible in the generic G. In an instance of G, the corresponding
10684 -- entities in the actual for F (which are the actuals for the
10685 -- instantiation of the generic that F denotes) must also be made
10686 -- visible for analysis of the current instance. On exit from the
10687 -- current instance, those entities are made private again. If the
10688 -- actual is currently in use, these entities are also use-visible.
10689
10690 -- The loop through the actual entities also steps through the formal
10691 -- entities and enters associations from formals to actuals into the
10692 -- renaming map. This is necessary to properly handle checking of
10693 -- actual parameter associations for later formals that depend on
10694 -- actuals declared in the formal package.
10695
10696 -- In Ada 2005, partial parameterization requires that we make
10697 -- visible the actuals corresponding to formals that were defaulted
10698 -- in the formal package. There formals are identified because they
10699 -- remain formal generics within the formal package, rather than
10700 -- being renamings of the actuals supplied.
10701
10702 declare
10703 Gen_Decl : constant Node_Id :=
10704 Unit_Declaration_Node (Gen_Parent);
10705 Formals : constant List_Id :=
10706 Generic_Formal_Declarations (Gen_Decl);
10707
10708 Actual_Ent : Entity_Id;
10709 Actual_Of_Formal : Node_Id;
10710 Formal_Node : Node_Id;
10711 Formal_Ent : Entity_Id;
10712
10713 begin
10714 if Present (Formals) then
10715 Formal_Node := First_Non_Pragma (Formals);
10716 else
10717 Formal_Node := Empty;
10718 end if;
10719
10720 Actual_Ent := First_Entity (Actual_Pack);
10721 Actual_Of_Formal :=
10722 First (Visible_Declarations (Specification (Analyzed_Formal)));
10723 while Present (Actual_Ent)
10724 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
10725 loop
10726 if Present (Formal_Node) then
10727 Formal_Ent := Get_Formal_Entity (Formal_Node);
10728
10729 if Present (Formal_Ent) then
10730 Find_Matching_Actual (Formal_Node, Actual_Ent);
10731 Match_Formal_Entity (Formal_Node, Formal_Ent, Actual_Ent);
10732
10733 -- We iterate at the same time over the actuals of the
10734 -- local package created for the formal, to determine
10735 -- which one of the formals of the original generic were
10736 -- defaulted in the formal. The corresponding actual
10737 -- entities are visible in the enclosing instance.
10738
10739 if Box_Present (Formal)
10740 or else
10741 (Present (Actual_Of_Formal)
10742 and then
10743 Is_Generic_Formal
10744 (Get_Formal_Entity (Actual_Of_Formal)))
10745 then
10746 Set_Is_Hidden (Actual_Ent, False);
10747 Set_Is_Visible_Formal (Actual_Ent);
10748 Set_Is_Potentially_Use_Visible
10749 (Actual_Ent, In_Use (Actual_Pack));
10750
10751 if Ekind (Actual_Ent) = E_Package then
10752 Process_Nested_Formal (Actual_Ent);
10753 end if;
10754
10755 else
10756 if not Is_Hidden (Actual_Ent) then
10757 Append_Elmt (Actual_Ent, Hidden_Formals);
10758 end if;
10759
10760 Set_Is_Hidden (Actual_Ent);
10761 Set_Is_Potentially_Use_Visible (Actual_Ent, False);
10762 end if;
10763 end if;
10764
10765 Next_Non_Pragma (Formal_Node);
10766 Next (Actual_Of_Formal);
10767
10768 else
10769 -- No further formals to match, but the generic part may
10770 -- contain inherited operation that are not hidden in the
10771 -- enclosing instance.
10772
10773 Next_Entity (Actual_Ent);
10774 end if;
10775 end loop;
10776
10777 -- Inherited subprograms generated by formal derived types are
10778 -- also visible if the types are.
10779
10780 Actual_Ent := First_Entity (Actual_Pack);
10781 while Present (Actual_Ent)
10782 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
10783 loop
10784 if Is_Overloadable (Actual_Ent)
10785 and then
10786 Nkind (Parent (Actual_Ent)) = N_Subtype_Declaration
10787 and then
10788 not Is_Hidden (Defining_Identifier (Parent (Actual_Ent)))
10789 then
10790 Set_Is_Hidden (Actual_Ent, False);
10791 Set_Is_Potentially_Use_Visible
10792 (Actual_Ent, In_Use (Actual_Pack));
10793 end if;
10794
10795 Next_Entity (Actual_Ent);
10796 end loop;
10797
10798 -- No conformance to check if the generic has no formal parameters
10799 -- and the formal package has no generic associations.
10800
10801 if Is_Empty_List (Formals)
10802 and then
10803 (Box_Present (Formal)
10804 or else No (Generic_Associations (Formal)))
10805 then
10806 return Decls;
10807 end if;
10808 end;
10809
10810 -- If the formal is not declared with a box, reanalyze it as an
10811 -- abbreviated instantiation, to verify the matching rules of 12.7.
10812 -- The actual checks are performed after the generic associations
10813 -- have been analyzed, to guarantee the same visibility for this
10814 -- instantiation and for the actuals.
10815
10816 -- In Ada 2005, the generic associations for the formal can include
10817 -- defaulted parameters. These are ignored during check. This
10818 -- internal instantiation is removed from the tree after conformance
10819 -- checking, because it contains formal declarations for those
10820 -- defaulted parameters, and those should not reach the back-end.
10821
10822 if not Box_Present (Formal) then
10823 declare
10824 I_Pack : constant Entity_Id :=
10825 Make_Temporary (Sloc (Actual), 'P');
10826
10827 begin
10828 Set_Is_Internal (I_Pack);
10829 Set_Ekind (I_Pack, E_Package);
10830 Set_Hidden_In_Formal_Instance (I_Pack, Hidden_Formals);
10831
10832 Append_To (Decls,
10833 Make_Package_Instantiation (Sloc (Actual),
10834 Defining_Unit_Name => I_Pack,
10835 Name =>
10836 New_Occurrence_Of
10837 (Get_Instance_Of (Gen_Parent), Sloc (Actual)),
10838 Generic_Associations => Generic_Associations (Formal)));
10839 end;
10840 end if;
10841
10842 return Decls;
10843 end if;
10844 end Instantiate_Formal_Package;
10845
10846 -----------------------------------
10847 -- Instantiate_Formal_Subprogram --
10848 -----------------------------------
10849
10850 function Instantiate_Formal_Subprogram
10851 (Formal : Node_Id;
10852 Actual : Node_Id;
10853 Analyzed_Formal : Node_Id) return Node_Id
10854 is
10855 Analyzed_S : constant Entity_Id :=
10856 Defining_Unit_Name (Specification (Analyzed_Formal));
10857 Formal_Sub : constant Entity_Id :=
10858 Defining_Unit_Name (Specification (Formal));
10859
10860 function From_Parent_Scope (Subp : Entity_Id) return Boolean;
10861 -- If the generic is a child unit, the parent has been installed on the
10862 -- scope stack, but a default subprogram cannot resolve to something
10863 -- on the parent because that parent is not really part of the visible
10864 -- context (it is there to resolve explicit local entities). If the
10865 -- default has resolved in this way, we remove the entity from immediate
10866 -- visibility and analyze the node again to emit an error message or
10867 -- find another visible candidate.
10868
10869 procedure Valid_Actual_Subprogram (Act : Node_Id);
10870 -- Perform legality check and raise exception on failure
10871
10872 -----------------------
10873 -- From_Parent_Scope --
10874 -----------------------
10875
10876 function From_Parent_Scope (Subp : Entity_Id) return Boolean is
10877 Gen_Scope : Node_Id;
10878
10879 begin
10880 Gen_Scope := Scope (Analyzed_S);
10881 while Present (Gen_Scope) and then Is_Child_Unit (Gen_Scope) loop
10882 if Scope (Subp) = Scope (Gen_Scope) then
10883 return True;
10884 end if;
10885
10886 Gen_Scope := Scope (Gen_Scope);
10887 end loop;
10888
10889 return False;
10890 end From_Parent_Scope;
10891
10892 -----------------------------
10893 -- Valid_Actual_Subprogram --
10894 -----------------------------
10895
10896 procedure Valid_Actual_Subprogram (Act : Node_Id) is
10897 Act_E : Entity_Id;
10898
10899 begin
10900 if Is_Entity_Name (Act) then
10901 Act_E := Entity (Act);
10902
10903 elsif Nkind (Act) = N_Selected_Component
10904 and then Is_Entity_Name (Selector_Name (Act))
10905 then
10906 Act_E := Entity (Selector_Name (Act));
10907
10908 else
10909 Act_E := Empty;
10910 end if;
10911
10912 if (Present (Act_E) and then Is_Overloadable (Act_E))
10913 or else Nkind_In (Act, N_Attribute_Reference,
10914 N_Indexed_Component,
10915 N_Character_Literal,
10916 N_Explicit_Dereference)
10917 then
10918 return;
10919 end if;
10920
10921 Error_Msg_NE
10922 ("expect subprogram or entry name in instantiation of &",
10923 Instantiation_Node, Formal_Sub);
10924 Abandon_Instantiation (Instantiation_Node);
10925 end Valid_Actual_Subprogram;
10926
10927 -- Local variables
10928
10929 Decl_Node : Node_Id;
10930 Loc : Source_Ptr;
10931 Nam : Node_Id;
10932 New_Spec : Node_Id;
10933 New_Subp : Entity_Id;
10934
10935 -- Start of processing for Instantiate_Formal_Subprogram
10936
10937 begin
10938 New_Spec := New_Copy_Tree (Specification (Formal));
10939
10940 -- The tree copy has created the proper instantiation sloc for the
10941 -- new specification. Use this location for all other constructed
10942 -- declarations.
10943
10944 Loc := Sloc (Defining_Unit_Name (New_Spec));
10945
10946 -- Create new entity for the actual (New_Copy_Tree does not), and
10947 -- indicate that it is an actual.
10948
10949 -- If the actual is not an entity (i.e. an attribute reference)
10950 -- and the formal includes aspect specifications for contracts,
10951 -- we create an internal name for the renaming declaration. The
10952 -- constructed wrapper contains a call to the entity in the renaming.
10953 -- This is an expansion activity, as is the wrapper creation.
10954
10955 if Ada_Version >= Ada_2020
10956 and then Has_Contracts (Analyzed_Formal)
10957 and then not Is_Entity_Name (Actual)
10958 and then Expander_Active
10959 then
10960 New_Subp := Make_Temporary (Sloc (Actual), 'S');
10961 Set_Defining_Unit_Name (New_Spec, New_Subp);
10962 else
10963 New_Subp := Make_Defining_Identifier (Loc, Chars (Formal_Sub));
10964 end if;
10965
10966 Set_Ekind (New_Subp, Ekind (Analyzed_S));
10967 Set_Is_Generic_Actual_Subprogram (New_Subp);
10968 Set_Defining_Unit_Name (New_Spec, New_Subp);
10969
10970 -- Create new entities for the each of the formals in the specification
10971 -- of the renaming declaration built for the actual.
10972
10973 if Present (Parameter_Specifications (New_Spec)) then
10974 declare
10975 F : Node_Id;
10976 F_Id : Entity_Id;
10977
10978 begin
10979 F := First (Parameter_Specifications (New_Spec));
10980 while Present (F) loop
10981 F_Id := Defining_Identifier (F);
10982
10983 Set_Defining_Identifier (F,
10984 Make_Defining_Identifier (Sloc (F_Id), Chars (F_Id)));
10985 Next (F);
10986 end loop;
10987 end;
10988 end if;
10989
10990 -- Find entity of actual. If the actual is an attribute reference, it
10991 -- cannot be resolved here (its formal is missing) but is handled
10992 -- instead in Attribute_Renaming. If the actual is overloaded, it is
10993 -- fully resolved subsequently, when the renaming declaration for the
10994 -- formal is analyzed. If it is an explicit dereference, resolve the
10995 -- prefix but not the actual itself, to prevent interpretation as call.
10996
10997 if Present (Actual) then
10998 Loc := Sloc (Actual);
10999 Set_Sloc (New_Spec, Loc);
11000
11001 if Nkind (Actual) = N_Operator_Symbol then
11002 Find_Direct_Name (Actual);
11003
11004 elsif Nkind (Actual) = N_Explicit_Dereference then
11005 Analyze (Prefix (Actual));
11006
11007 elsif Nkind (Actual) /= N_Attribute_Reference then
11008 Analyze (Actual);
11009 end if;
11010
11011 Valid_Actual_Subprogram (Actual);
11012 Nam := Actual;
11013
11014 elsif Present (Default_Name (Formal)) then
11015 if not Nkind_In (Default_Name (Formal), N_Attribute_Reference,
11016 N_Selected_Component,
11017 N_Indexed_Component,
11018 N_Character_Literal)
11019 and then Present (Entity (Default_Name (Formal)))
11020 then
11021 Nam := New_Occurrence_Of (Entity (Default_Name (Formal)), Loc);
11022 else
11023 Nam := New_Copy (Default_Name (Formal));
11024 Set_Sloc (Nam, Loc);
11025 end if;
11026
11027 elsif Box_Present (Formal) then
11028
11029 -- Actual is resolved at the point of instantiation. Create an
11030 -- identifier or operator with the same name as the formal.
11031
11032 if Nkind (Formal_Sub) = N_Defining_Operator_Symbol then
11033 Nam :=
11034 Make_Operator_Symbol (Loc,
11035 Chars => Chars (Formal_Sub),
11036 Strval => No_String);
11037 else
11038 Nam := Make_Identifier (Loc, Chars (Formal_Sub));
11039 end if;
11040
11041 elsif Nkind (Specification (Formal)) = N_Procedure_Specification
11042 and then Null_Present (Specification (Formal))
11043 then
11044 -- Generate null body for procedure, for use in the instance
11045
11046 Decl_Node :=
11047 Make_Subprogram_Body (Loc,
11048 Specification => New_Spec,
11049 Declarations => New_List,
11050 Handled_Statement_Sequence =>
11051 Make_Handled_Sequence_Of_Statements (Loc,
11052 Statements => New_List (Make_Null_Statement (Loc))));
11053
11054 -- RM 12.6 (16 2/2): The procedure has convention Intrinsic
11055
11056 Set_Convention (Defining_Unit_Name (New_Spec), Convention_Intrinsic);
11057
11058 -- Eliminate the calls to it when optimization is enabled
11059
11060 Set_Is_Inlined (Defining_Unit_Name (New_Spec));
11061 return Decl_Node;
11062
11063 else
11064 Error_Msg_Sloc := Sloc (Scope (Analyzed_S));
11065 Error_Msg_NE
11066 ("missing actual&", Instantiation_Node, Formal_Sub);
11067 Error_Msg_NE
11068 ("\in instantiation of & declared#",
11069 Instantiation_Node, Scope (Analyzed_S));
11070 Abandon_Instantiation (Instantiation_Node);
11071 end if;
11072
11073 Decl_Node :=
11074 Make_Subprogram_Renaming_Declaration (Loc,
11075 Specification => New_Spec,
11076 Name => Nam);
11077
11078 -- If we do not have an actual and the formal specified <> then set to
11079 -- get proper default.
11080
11081 if No (Actual) and then Box_Present (Formal) then
11082 Set_From_Default (Decl_Node);
11083 end if;
11084
11085 -- Gather possible interpretations for the actual before analyzing the
11086 -- instance. If overloaded, it will be resolved when analyzing the
11087 -- renaming declaration.
11088
11089 if Box_Present (Formal) and then No (Actual) then
11090 Analyze (Nam);
11091
11092 if Is_Child_Unit (Scope (Analyzed_S))
11093 and then Present (Entity (Nam))
11094 then
11095 if not Is_Overloaded (Nam) then
11096 if From_Parent_Scope (Entity (Nam)) then
11097 Set_Is_Immediately_Visible (Entity (Nam), False);
11098 Set_Entity (Nam, Empty);
11099 Set_Etype (Nam, Empty);
11100
11101 Analyze (Nam);
11102 Set_Is_Immediately_Visible (Entity (Nam));
11103 end if;
11104
11105 else
11106 declare
11107 I : Interp_Index;
11108 It : Interp;
11109
11110 begin
11111 Get_First_Interp (Nam, I, It);
11112 while Present (It.Nam) loop
11113 if From_Parent_Scope (It.Nam) then
11114 Remove_Interp (I);
11115 end if;
11116
11117 Get_Next_Interp (I, It);
11118 end loop;
11119 end;
11120 end if;
11121 end if;
11122 end if;
11123
11124 -- The generic instantiation freezes the actual. This can only be done
11125 -- once the actual is resolved, in the analysis of the renaming
11126 -- declaration. To make the formal subprogram entity available, we set
11127 -- Corresponding_Formal_Spec to point to the formal subprogram entity.
11128 -- This is also needed in Analyze_Subprogram_Renaming for the processing
11129 -- of formal abstract subprograms.
11130
11131 Set_Corresponding_Formal_Spec (Decl_Node, Analyzed_S);
11132
11133 -- We cannot analyze the renaming declaration, and thus find the actual,
11134 -- until all the actuals are assembled in the instance. For subsequent
11135 -- checks of other actuals, indicate the node that will hold the
11136 -- instance of this formal.
11137
11138 Set_Instance_Of (Analyzed_S, Nam);
11139
11140 if Nkind (Actual) = N_Selected_Component
11141 and then Is_Task_Type (Etype (Prefix (Actual)))
11142 and then not Is_Frozen (Etype (Prefix (Actual)))
11143 then
11144 -- The renaming declaration will create a body, which must appear
11145 -- outside of the instantiation, We move the renaming declaration
11146 -- out of the instance, and create an additional renaming inside,
11147 -- to prevent freezing anomalies.
11148
11149 declare
11150 Anon_Id : constant Entity_Id := Make_Temporary (Loc, 'E');
11151
11152 begin
11153 Set_Defining_Unit_Name (New_Spec, Anon_Id);
11154 Insert_Before (Instantiation_Node, Decl_Node);
11155 Analyze (Decl_Node);
11156
11157 -- Now create renaming within the instance
11158
11159 Decl_Node :=
11160 Make_Subprogram_Renaming_Declaration (Loc,
11161 Specification => New_Copy_Tree (New_Spec),
11162 Name => New_Occurrence_Of (Anon_Id, Loc));
11163
11164 Set_Defining_Unit_Name (Specification (Decl_Node),
11165 Make_Defining_Identifier (Loc, Chars (Formal_Sub)));
11166 end;
11167 end if;
11168
11169 return Decl_Node;
11170 end Instantiate_Formal_Subprogram;
11171
11172 ------------------------
11173 -- Instantiate_Object --
11174 ------------------------
11175
11176 function Instantiate_Object
11177 (Formal : Node_Id;
11178 Actual : Node_Id;
11179 Analyzed_Formal : Node_Id) return List_Id
11180 is
11181 Gen_Obj : constant Entity_Id := Defining_Identifier (Formal);
11182 A_Gen_Obj : constant Entity_Id :=
11183 Defining_Identifier (Analyzed_Formal);
11184 Acc_Def : Node_Id := Empty;
11185 Act_Assoc : constant Node_Id := Parent (Actual);
11186 Actual_Decl : Node_Id := Empty;
11187 Decl_Node : Node_Id;
11188 Def : Node_Id;
11189 Ftyp : Entity_Id;
11190 List : constant List_Id := New_List;
11191 Loc : constant Source_Ptr := Sloc (Actual);
11192 Orig_Ftyp : constant Entity_Id := Etype (A_Gen_Obj);
11193 Subt_Decl : Node_Id := Empty;
11194 Subt_Mark : Node_Id := Empty;
11195
11196 -- Start of processing for Instantiate_Object
11197
11198 begin
11199 -- Formal may be an anonymous access
11200
11201 if Present (Subtype_Mark (Formal)) then
11202 Subt_Mark := Subtype_Mark (Formal);
11203 else
11204 Check_Access_Definition (Formal);
11205 Acc_Def := Access_Definition (Formal);
11206 end if;
11207
11208 -- Sloc for error message on missing actual
11209
11210 Error_Msg_Sloc := Sloc (Scope (A_Gen_Obj));
11211
11212 if Get_Instance_Of (Gen_Obj) /= Gen_Obj then
11213 Error_Msg_N ("duplicate instantiation of generic parameter", Actual);
11214 end if;
11215
11216 Set_Parent (List, Parent (Actual));
11217
11218 -- OUT present
11219
11220 if Out_Present (Formal) then
11221
11222 -- An IN OUT generic actual must be a name. The instantiation is a
11223 -- renaming declaration. The actual is the name being renamed. We
11224 -- use the actual directly, rather than a copy, because it is not
11225 -- used further in the list of actuals, and because a copy or a use
11226 -- of relocate_node is incorrect if the instance is nested within a
11227 -- generic. In order to simplify e.g. ASIS queries, the
11228 -- Generic_Parent field links the declaration to the generic
11229 -- association.
11230
11231 if No (Actual) then
11232 Error_Msg_NE
11233 ("missing actual &",
11234 Instantiation_Node, Gen_Obj);
11235 Error_Msg_NE
11236 ("\in instantiation of & declared#",
11237 Instantiation_Node, Scope (A_Gen_Obj));
11238 Abandon_Instantiation (Instantiation_Node);
11239 end if;
11240
11241 if Present (Subt_Mark) then
11242 Decl_Node :=
11243 Make_Object_Renaming_Declaration (Loc,
11244 Defining_Identifier => New_Copy (Gen_Obj),
11245 Subtype_Mark => New_Copy_Tree (Subt_Mark),
11246 Name => Actual);
11247
11248 else pragma Assert (Present (Acc_Def));
11249 Decl_Node :=
11250 Make_Object_Renaming_Declaration (Loc,
11251 Defining_Identifier => New_Copy (Gen_Obj),
11252 Access_Definition => New_Copy_Tree (Acc_Def),
11253 Name => Actual);
11254 end if;
11255
11256 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
11257
11258 -- The analysis of the actual may produce Insert_Action nodes, so
11259 -- the declaration must have a context in which to attach them.
11260
11261 Append (Decl_Node, List);
11262 Analyze (Actual);
11263
11264 -- Return if the analysis of the actual reported some error
11265
11266 if Etype (Actual) = Any_Type then
11267 return List;
11268 end if;
11269
11270 -- This check is performed here because Analyze_Object_Renaming will
11271 -- not check it when Comes_From_Source is False. Note though that the
11272 -- check for the actual being the name of an object will be performed
11273 -- in Analyze_Object_Renaming.
11274
11275 if Is_Object_Reference (Actual)
11276 and then Is_Dependent_Component_Of_Mutable_Object (Actual)
11277 then
11278 Error_Msg_N
11279 ("illegal discriminant-dependent component for in out parameter",
11280 Actual);
11281 end if;
11282
11283 -- The actual has to be resolved in order to check that it is a
11284 -- variable (due to cases such as F (1), where F returns access to
11285 -- an array, and for overloaded prefixes).
11286
11287 Ftyp := Get_Instance_Of (Etype (A_Gen_Obj));
11288
11289 -- If the type of the formal is not itself a formal, and the current
11290 -- unit is a child unit, the formal type must be declared in a
11291 -- parent, and must be retrieved by visibility.
11292
11293 if Ftyp = Orig_Ftyp
11294 and then Is_Generic_Unit (Scope (Ftyp))
11295 and then Is_Child_Unit (Scope (A_Gen_Obj))
11296 then
11297 declare
11298 Temp : constant Node_Id :=
11299 New_Copy_Tree (Subtype_Mark (Analyzed_Formal));
11300 begin
11301 Set_Entity (Temp, Empty);
11302 Find_Type (Temp);
11303 Ftyp := Entity (Temp);
11304 end;
11305 end if;
11306
11307 if Is_Private_Type (Ftyp)
11308 and then not Is_Private_Type (Etype (Actual))
11309 and then (Base_Type (Full_View (Ftyp)) = Base_Type (Etype (Actual))
11310 or else Base_Type (Etype (Actual)) = Ftyp)
11311 then
11312 -- If the actual has the type of the full view of the formal, or
11313 -- else a non-private subtype of the formal, then the visibility
11314 -- of the formal type has changed. Add to the actuals a subtype
11315 -- declaration that will force the exchange of views in the body
11316 -- of the instance as well.
11317
11318 Subt_Decl :=
11319 Make_Subtype_Declaration (Loc,
11320 Defining_Identifier => Make_Temporary (Loc, 'P'),
11321 Subtype_Indication => New_Occurrence_Of (Ftyp, Loc));
11322
11323 Prepend (Subt_Decl, List);
11324
11325 Prepend_Elmt (Full_View (Ftyp), Exchanged_Views);
11326 Exchange_Declarations (Ftyp);
11327 end if;
11328
11329 Resolve (Actual, Ftyp);
11330
11331 if not Denotes_Variable (Actual) then
11332 Error_Msg_NE ("actual for& must be a variable", Actual, Gen_Obj);
11333
11334 elsif Base_Type (Ftyp) /= Base_Type (Etype (Actual)) then
11335
11336 -- Ada 2005 (AI-423): For a generic formal object of mode in out,
11337 -- the type of the actual shall resolve to a specific anonymous
11338 -- access type.
11339
11340 if Ada_Version < Ada_2005
11341 or else not Is_Anonymous_Access_Type (Base_Type (Ftyp))
11342 or else not Is_Anonymous_Access_Type (Base_Type (Etype (Actual)))
11343 then
11344 Error_Msg_NE
11345 ("type of actual does not match type of&", Actual, Gen_Obj);
11346 end if;
11347 end if;
11348
11349 Note_Possible_Modification (Actual, Sure => True);
11350
11351 -- Check for instantiation with atomic/volatile object actual for
11352 -- nonatomic/nonvolatile formal (RM C.6 (12)).
11353
11354 if Is_Atomic_Object (Actual) and then not Is_Atomic (Orig_Ftyp) then
11355 Error_Msg_NE
11356 ("cannot instantiate nonatomic formal & of mode in out",
11357 Actual, Gen_Obj);
11358 Error_Msg_N ("\with atomic object actual (RM C.6(12))", Actual);
11359
11360 elsif Is_Volatile_Object (Actual) and then not Is_Volatile (Orig_Ftyp)
11361 then
11362 Error_Msg_NE
11363 ("cannot instantiate nonvolatile formal & of mode in out",
11364 Actual, Gen_Obj);
11365 Error_Msg_N ("\with volatile object actual (RM C.6(12))", Actual);
11366 end if;
11367
11368 -- Check for instantiation on nonatomic subcomponent of an atomic
11369 -- object in Ada 2020 (RM C.6 (13)).
11370
11371 if Ada_Version >= Ada_2020
11372 and then Is_Subcomponent_Of_Atomic_Object (Actual)
11373 and then not Is_Atomic_Object (Actual)
11374 then
11375 Error_Msg_NE
11376 ("cannot instantiate formal & of mode in out with actual",
11377 Actual, Gen_Obj);
11378 Error_Msg_N
11379 ("\nonatomic subcomponent of atomic object (RM C.6(13))",
11380 Actual);
11381 end if;
11382
11383 -- Check actual/formal compatibility with respect to the four
11384 -- volatility refinement aspects.
11385
11386 declare
11387 Actual_Obj : Entity_Id;
11388 N : Node_Id := Actual;
11389 begin
11390 -- Similar to Sem_Util.Get_Enclosing_Object, but treat
11391 -- pointer dereference like component selection.
11392 loop
11393 if Is_Entity_Name (N) then
11394 Actual_Obj := Entity (N);
11395 exit;
11396 end if;
11397
11398 case Nkind (N) is
11399 when N_Indexed_Component
11400 | N_Selected_Component
11401 | N_Slice
11402 | N_Explicit_Dereference
11403 =>
11404 N := Prefix (N);
11405
11406 when N_Type_Conversion =>
11407 N := Expression (N);
11408
11409 when others =>
11410 Actual_Obj := Etype (N);
11411 exit;
11412 end case;
11413 end loop;
11414
11415 Check_Volatility_Compatibility
11416 (Actual_Obj, A_Gen_Obj, "actual object",
11417 "its corresponding formal object of mode in out",
11418 Srcpos_Bearer => Actual);
11419 end;
11420
11421 -- Formal in-parameter
11422
11423 else
11424 -- The instantiation of a generic formal in-parameter is constant
11425 -- declaration. The actual is the expression for that declaration.
11426 -- Its type is a full copy of the type of the formal. This may be
11427 -- an access to subprogram, for which we need to generate entities
11428 -- for the formals in the new signature.
11429
11430 if Present (Actual) then
11431 if Present (Subt_Mark) then
11432 Def := New_Copy_Tree (Subt_Mark);
11433 else
11434 pragma Assert (Present (Acc_Def));
11435 Def := New_Copy_Tree (Acc_Def);
11436 end if;
11437
11438 Decl_Node :=
11439 Make_Object_Declaration (Loc,
11440 Defining_Identifier => New_Copy (Gen_Obj),
11441 Constant_Present => True,
11442 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
11443 Object_Definition => Def,
11444 Expression => Actual);
11445
11446 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
11447
11448 -- A generic formal object of a tagged type is defined to be
11449 -- aliased so the new constant must also be treated as aliased.
11450
11451 if Is_Tagged_Type (Etype (A_Gen_Obj)) then
11452 Set_Aliased_Present (Decl_Node);
11453 end if;
11454
11455 Append (Decl_Node, List);
11456
11457 -- No need to repeat (pre-)analysis of some expression nodes
11458 -- already handled in Preanalyze_Actuals.
11459
11460 if Nkind (Actual) /= N_Allocator then
11461 Analyze (Actual);
11462
11463 -- Return if the analysis of the actual reported some error
11464
11465 if Etype (Actual) = Any_Type then
11466 return List;
11467 end if;
11468 end if;
11469
11470 declare
11471 Formal_Type : constant Entity_Id := Etype (A_Gen_Obj);
11472 Typ : Entity_Id;
11473
11474 begin
11475 Typ := Get_Instance_Of (Formal_Type);
11476
11477 -- If the actual appears in the current or an enclosing scope,
11478 -- use its type directly. This is relevant if it has an actual
11479 -- subtype that is distinct from its nominal one. This cannot
11480 -- be done in general because the type of the actual may
11481 -- depend on other actuals, and only be fully determined when
11482 -- the enclosing instance is analyzed.
11483
11484 if Present (Etype (Actual))
11485 and then Is_Constr_Subt_For_U_Nominal (Etype (Actual))
11486 then
11487 Freeze_Before (Instantiation_Node, Etype (Actual));
11488 else
11489 Freeze_Before (Instantiation_Node, Typ);
11490 end if;
11491
11492 -- If the actual is an aggregate, perform name resolution on
11493 -- its components (the analysis of an aggregate does not do it)
11494 -- to capture local names that may be hidden if the generic is
11495 -- a child unit.
11496
11497 if Nkind (Actual) = N_Aggregate then
11498 Preanalyze_And_Resolve (Actual, Typ);
11499 end if;
11500
11501 if Is_Limited_Type (Typ)
11502 and then not OK_For_Limited_Init (Typ, Actual)
11503 then
11504 Error_Msg_N
11505 ("initialization not allowed for limited types", Actual);
11506 Explain_Limited_Type (Typ, Actual);
11507 end if;
11508 end;
11509
11510 elsif Present (Default_Expression (Formal)) then
11511
11512 -- Use default to construct declaration
11513
11514 if Present (Subt_Mark) then
11515 Def := New_Copy (Subt_Mark);
11516 else
11517 pragma Assert (Present (Acc_Def));
11518 Def := New_Copy_Tree (Acc_Def);
11519 end if;
11520
11521 Decl_Node :=
11522 Make_Object_Declaration (Sloc (Formal),
11523 Defining_Identifier => New_Copy (Gen_Obj),
11524 Constant_Present => True,
11525 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
11526 Object_Definition => Def,
11527 Expression => New_Copy_Tree
11528 (Default_Expression (Formal)));
11529
11530 Set_Corresponding_Generic_Association
11531 (Decl_Node, Expression (Decl_Node));
11532
11533 Append (Decl_Node, List);
11534 Set_Analyzed (Expression (Decl_Node), False);
11535
11536 else
11537 Error_Msg_NE ("missing actual&", Instantiation_Node, Gen_Obj);
11538 Error_Msg_NE ("\in instantiation of & declared#",
11539 Instantiation_Node, Scope (A_Gen_Obj));
11540
11541 if Is_Scalar_Type (Etype (A_Gen_Obj)) then
11542
11543 -- Create dummy constant declaration so that instance can be
11544 -- analyzed, to minimize cascaded visibility errors.
11545
11546 if Present (Subt_Mark) then
11547 Def := Subt_Mark;
11548 else pragma Assert (Present (Acc_Def));
11549 Def := Acc_Def;
11550 end if;
11551
11552 Decl_Node :=
11553 Make_Object_Declaration (Loc,
11554 Defining_Identifier => New_Copy (Gen_Obj),
11555 Constant_Present => True,
11556 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
11557 Object_Definition => New_Copy (Def),
11558 Expression =>
11559 Make_Attribute_Reference (Sloc (Gen_Obj),
11560 Attribute_Name => Name_First,
11561 Prefix => New_Copy (Def)));
11562
11563 Append (Decl_Node, List);
11564
11565 else
11566 Abandon_Instantiation (Instantiation_Node);
11567 end if;
11568 end if;
11569 end if;
11570
11571 if Nkind (Actual) in N_Has_Entity then
11572 Actual_Decl := Parent (Entity (Actual));
11573 end if;
11574
11575 -- Ada 2005 (AI-423) refined by AI12-0287:
11576 -- For an object_renaming_declaration with a null_exclusion or an
11577 -- access_definition that has a null_exclusion, the subtype of the
11578 -- object_name shall exclude null. In addition, if the
11579 -- object_renaming_declaration occurs within the body of a generic unit
11580 -- G or within the body of a generic unit declared within the
11581 -- declarative region of generic unit G, then:
11582 -- * if the object_name statically denotes a generic formal object of
11583 -- mode in out of G, then the declaration of that object shall have a
11584 -- null_exclusion;
11585 -- * if the object_name statically denotes a call of a generic formal
11586 -- function of G, then the declaration of the result of that function
11587 -- shall have a null_exclusion.
11588
11589 if Ada_Version >= Ada_2005
11590 and then Present (Actual_Decl)
11591 and then Nkind_In (Actual_Decl, N_Formal_Object_Declaration,
11592 N_Object_Declaration)
11593 and then Nkind (Analyzed_Formal) = N_Formal_Object_Declaration
11594 and then not Has_Null_Exclusion (Actual_Decl)
11595 and then Has_Null_Exclusion (Analyzed_Formal)
11596 and then Ekind (Defining_Identifier (Analyzed_Formal))
11597 = E_Generic_In_Out_Parameter
11598 and then ((In_Generic_Scope (Entity (Actual))
11599 and then In_Package_Body (Scope (Entity (Actual))))
11600 or else not Can_Never_Be_Null (Etype (Actual)))
11601 then
11602 Error_Msg_Sloc := Sloc (Analyzed_Formal);
11603 Error_Msg_N
11604 ("actual must exclude null to match generic formal#", Actual);
11605 end if;
11606
11607 -- An effectively volatile object cannot be used as an actual in a
11608 -- generic instantiation (SPARK RM 7.1.3(7)). The following check is
11609 -- relevant only when SPARK_Mode is on as it is not a standard Ada
11610 -- legality rule, and also verifies that the actual is an object.
11611
11612 if SPARK_Mode = On
11613 and then Present (Actual)
11614 and then Is_Object_Reference (Actual)
11615 and then Is_Effectively_Volatile_Object (Actual)
11616 and then not Is_Effectively_Volatile (A_Gen_Obj)
11617 then
11618 Error_Msg_N
11619 ("volatile object cannot act as actual in generic instantiation",
11620 Actual);
11621 end if;
11622
11623 return List;
11624 end Instantiate_Object;
11625
11626 ------------------------------
11627 -- Instantiate_Package_Body --
11628 ------------------------------
11629
11630 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
11631 -- must be replaced by gotos which jump to the end of the routine in order
11632 -- to restore the Ghost and SPARK modes.
11633
11634 procedure Instantiate_Package_Body
11635 (Body_Info : Pending_Body_Info;
11636 Inlined_Body : Boolean := False;
11637 Body_Optional : Boolean := False)
11638 is
11639 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
11640 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Decl);
11641 Act_Spec : constant Node_Id := Specification (Act_Decl);
11642 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
11643 Gen_Id : constant Node_Id := Name (Inst_Node);
11644 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
11645 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
11646 Loc : constant Source_Ptr := Sloc (Inst_Node);
11647
11648 procedure Check_Initialized_Types;
11649 -- In a generic package body, an entity of a generic private type may
11650 -- appear uninitialized. This is suspicious, unless the actual is a
11651 -- fully initialized type.
11652
11653 -----------------------------
11654 -- Check_Initialized_Types --
11655 -----------------------------
11656
11657 procedure Check_Initialized_Types is
11658 Decl : Node_Id;
11659 Formal : Entity_Id;
11660 Actual : Entity_Id;
11661 Uninit_Var : Entity_Id;
11662
11663 begin
11664 Decl := First (Generic_Formal_Declarations (Gen_Decl));
11665 while Present (Decl) loop
11666 Uninit_Var := Empty;
11667
11668 if Nkind (Decl) = N_Private_Extension_Declaration then
11669 Uninit_Var := Uninitialized_Variable (Decl);
11670
11671 elsif Nkind (Decl) = N_Formal_Type_Declaration
11672 and then Nkind (Formal_Type_Definition (Decl)) =
11673 N_Formal_Private_Type_Definition
11674 then
11675 Uninit_Var :=
11676 Uninitialized_Variable (Formal_Type_Definition (Decl));
11677 end if;
11678
11679 if Present (Uninit_Var) then
11680 Formal := Defining_Identifier (Decl);
11681 Actual := First_Entity (Act_Decl_Id);
11682
11683 -- For each formal there is a subtype declaration that renames
11684 -- the actual and has the same name as the formal. Locate the
11685 -- formal for warning message about uninitialized variables
11686 -- in the generic, for which the actual type should be a fully
11687 -- initialized type.
11688
11689 while Present (Actual) loop
11690 exit when Ekind (Actual) = E_Package
11691 and then Present (Renamed_Object (Actual));
11692
11693 if Chars (Actual) = Chars (Formal)
11694 and then not Is_Scalar_Type (Actual)
11695 and then not Is_Fully_Initialized_Type (Actual)
11696 and then Warn_On_No_Value_Assigned
11697 then
11698 Error_Msg_Node_2 := Formal;
11699 Error_Msg_NE
11700 ("generic unit has uninitialized variable& of "
11701 & "formal private type &?v?", Actual, Uninit_Var);
11702 Error_Msg_NE
11703 ("actual type for& should be fully initialized type?v?",
11704 Actual, Formal);
11705 exit;
11706 end if;
11707
11708 Next_Entity (Actual);
11709 end loop;
11710 end if;
11711
11712 Next (Decl);
11713 end loop;
11714 end Check_Initialized_Types;
11715
11716 -- Local variables
11717
11718 -- The following constants capture the context prior to instantiating
11719 -- the package body.
11720
11721 Saved_CS : constant Config_Switches_Type := Save_Config_Switches;
11722 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
11723 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
11724 Saved_ISMP : constant Boolean :=
11725 Ignore_SPARK_Mode_Pragmas_In_Instance;
11726 Saved_LSST : constant Suppress_Stack_Entry_Ptr :=
11727 Local_Suppress_Stack_Top;
11728 Saved_SC : constant Boolean := Style_Check;
11729 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
11730 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
11731 Saved_SS : constant Suppress_Record := Scope_Suppress;
11732 Saved_Warn : constant Warning_Record := Save_Warnings;
11733
11734 Act_Body : Node_Id;
11735 Act_Body_Id : Entity_Id;
11736 Act_Body_Name : Node_Id;
11737 Gen_Body : Node_Id;
11738 Gen_Body_Id : Node_Id;
11739 Par_Ent : Entity_Id := Empty;
11740 Par_Installed : Boolean := False;
11741 Par_Vis : Boolean := False;
11742
11743 Vis_Prims_List : Elist_Id := No_Elist;
11744 -- List of primitives made temporarily visible in the instantiation
11745 -- to match the visibility of the formal type.
11746
11747 -- Start of processing for Instantiate_Package_Body
11748
11749 begin
11750 Gen_Body_Id := Corresponding_Body (Gen_Decl);
11751
11752 -- The instance body may already have been processed, as the parent of
11753 -- another instance that is inlined (Load_Parent_Of_Generic).
11754
11755 if Present (Corresponding_Body (Instance_Spec (Inst_Node))) then
11756 return;
11757 end if;
11758
11759 -- The package being instantiated may be subject to pragma Ghost. Set
11760 -- the mode now to ensure that any nodes generated during instantiation
11761 -- are properly marked as Ghost.
11762
11763 Set_Ghost_Mode (Act_Decl_Id);
11764
11765 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
11766
11767 -- Re-establish the state of information on which checks are suppressed.
11768 -- This information was set in Body_Info at the point of instantiation,
11769 -- and now we restore it so that the instance is compiled using the
11770 -- check status at the instantiation (RM 11.5(7.2/2), AI95-00224-01).
11771
11772 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
11773 Scope_Suppress := Body_Info.Scope_Suppress;
11774
11775 Restore_Config_Switches (Body_Info.Config_Switches);
11776 Restore_Warnings (Body_Info.Warnings);
11777
11778 if No (Gen_Body_Id) then
11779
11780 -- Do not look for parent of generic body if none is required.
11781 -- This may happen when the routine is called as part of the
11782 -- Pending_Instantiations processing, when nested instances
11783 -- may precede the one generated from the main unit.
11784
11785 if not Unit_Requires_Body (Defining_Entity (Gen_Decl))
11786 and then Body_Optional
11787 then
11788 goto Leave;
11789 else
11790 Load_Parent_Of_Generic
11791 (Inst_Node, Specification (Gen_Decl), Body_Optional);
11792
11793 -- Surprisingly enough, loading the body of the parent can cause
11794 -- the body to be instantiated and the double instantiation needs
11795 -- to be prevented in order to avoid giving bogus semantic errors.
11796
11797 -- This case can occur because of the Collect_Previous_Instances
11798 -- machinery of Load_Parent_Of_Generic, which will instantiate
11799 -- bodies that are deemed to be ahead of the body of the parent
11800 -- in the compilation unit. But the relative position of these
11801 -- bodies is computed using the mere comparison of their Sloc.
11802
11803 -- Now suppose that you have two generic packages G and H, with
11804 -- G containing a mere instantiation of H:
11805
11806 -- generic
11807 -- package H is
11808
11809 -- generic
11810 -- package Nested_G is
11811 -- ...
11812 -- end Nested_G;
11813
11814 -- end H;
11815
11816 -- with H;
11817
11818 -- generic
11819 -- package G is
11820
11821 -- package My_H is new H;
11822
11823 -- end G;
11824
11825 -- and a third package Q instantiating G and Nested_G:
11826
11827 -- with G;
11828
11829 -- package Q is
11830
11831 -- package My_G is new G;
11832
11833 -- package My_Nested_G is new My_G.My_H.Nested_G;
11834
11835 -- end Q;
11836
11837 -- The body to be instantiated is that of My_Nested_G and its
11838 -- parent is the instance My_G.My_H. This latter instantiation
11839 -- is done when My_G is analyzed, i.e. after the declarations
11840 -- of My_G and My_Nested_G have been parsed; as a result, the
11841 -- Sloc of My_G.My_H is greater than the Sloc of My_Nested_G.
11842
11843 -- Therefore loading the body of My_G.My_H will cause the body
11844 -- of My_Nested_G to be instantiated because it is deemed to be
11845 -- ahead of My_G.My_H. This means that Load_Parent_Of_Generic
11846 -- will again be invoked on My_G.My_H, but this time with the
11847 -- Collect_Previous_Instances machinery disabled, so there is
11848 -- no endless mutual recursion and things are done in order.
11849
11850 if Present (Corresponding_Body (Instance_Spec (Inst_Node))) then
11851 goto Leave;
11852 end if;
11853
11854 Gen_Body_Id := Corresponding_Body (Gen_Decl);
11855 end if;
11856 end if;
11857
11858 -- Establish global variable for sloc adjustment and for error recovery
11859 -- In the case of an instance body for an instantiation with actuals
11860 -- from a limited view, the instance body is placed at the beginning
11861 -- of the enclosing package body: use the body entity as the source
11862 -- location for nodes of the instance body.
11863
11864 if not Is_Empty_Elmt_List (Incomplete_Actuals (Act_Decl_Id)) then
11865 declare
11866 Scop : constant Entity_Id := Scope (Act_Decl_Id);
11867 Body_Id : constant Node_Id :=
11868 Corresponding_Body (Unit_Declaration_Node (Scop));
11869
11870 begin
11871 Instantiation_Node := Body_Id;
11872 end;
11873 else
11874 Instantiation_Node := Inst_Node;
11875 end if;
11876
11877 if Present (Gen_Body_Id) then
11878 Save_Env (Gen_Unit, Act_Decl_Id);
11879 Style_Check := False;
11880
11881 -- If the context of the instance is subject to SPARK_Mode "off", the
11882 -- annotation is missing, or the body is instantiated at a later pass
11883 -- and its spec ignored SPARK_Mode pragma, set the global flag which
11884 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within the
11885 -- instance.
11886
11887 if SPARK_Mode /= On
11888 or else Ignore_SPARK_Mode_Pragmas (Act_Decl_Id)
11889 then
11890 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
11891 end if;
11892
11893 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
11894 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
11895
11896 Create_Instantiation_Source
11897 (Inst_Node, Gen_Body_Id, S_Adjustment);
11898
11899 Act_Body :=
11900 Copy_Generic_Node
11901 (Original_Node (Gen_Body), Empty, Instantiating => True);
11902
11903 -- Create proper (possibly qualified) defining name for the body, to
11904 -- correspond to the one in the spec.
11905
11906 Act_Body_Id :=
11907 Make_Defining_Identifier (Sloc (Act_Decl_Id), Chars (Act_Decl_Id));
11908 Preserve_Comes_From_Source (Act_Body_Id, Act_Decl_Id);
11909
11910 -- Some attributes of spec entity are not inherited by body entity
11911
11912 Set_Handler_Records (Act_Body_Id, No_List);
11913
11914 if Nkind (Defining_Unit_Name (Act_Spec)) =
11915 N_Defining_Program_Unit_Name
11916 then
11917 Act_Body_Name :=
11918 Make_Defining_Program_Unit_Name (Loc,
11919 Name =>
11920 New_Copy_Tree (Name (Defining_Unit_Name (Act_Spec))),
11921 Defining_Identifier => Act_Body_Id);
11922 else
11923 Act_Body_Name := Act_Body_Id;
11924 end if;
11925
11926 Set_Defining_Unit_Name (Act_Body, Act_Body_Name);
11927
11928 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
11929 Check_Generic_Actuals (Act_Decl_Id, False);
11930 Check_Initialized_Types;
11931
11932 -- Install primitives hidden at the point of the instantiation but
11933 -- visible when processing the generic formals
11934
11935 declare
11936 E : Entity_Id;
11937
11938 begin
11939 E := First_Entity (Act_Decl_Id);
11940 while Present (E) loop
11941 if Is_Type (E)
11942 and then not Is_Itype (E)
11943 and then Is_Generic_Actual_Type (E)
11944 and then Is_Tagged_Type (E)
11945 then
11946 Install_Hidden_Primitives
11947 (Prims_List => Vis_Prims_List,
11948 Gen_T => Generic_Parent_Type (Parent (E)),
11949 Act_T => E);
11950 end if;
11951
11952 Next_Entity (E);
11953 end loop;
11954 end;
11955
11956 -- If it is a child unit, make the parent instance (which is an
11957 -- instance of the parent of the generic) visible. The parent
11958 -- instance is the prefix of the name of the generic unit.
11959
11960 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
11961 and then Nkind (Gen_Id) = N_Expanded_Name
11962 then
11963 Par_Ent := Entity (Prefix (Gen_Id));
11964 Par_Vis := Is_Immediately_Visible (Par_Ent);
11965 Install_Parent (Par_Ent, In_Body => True);
11966 Par_Installed := True;
11967
11968 elsif Is_Child_Unit (Gen_Unit) then
11969 Par_Ent := Scope (Gen_Unit);
11970 Par_Vis := Is_Immediately_Visible (Par_Ent);
11971 Install_Parent (Par_Ent, In_Body => True);
11972 Par_Installed := True;
11973 end if;
11974
11975 -- If the instantiation is a library unit, and this is the main unit,
11976 -- then build the resulting compilation unit nodes for the instance.
11977 -- If this is a compilation unit but it is not the main unit, then it
11978 -- is the body of a unit in the context, that is being compiled
11979 -- because it is encloses some inlined unit or another generic unit
11980 -- being instantiated. In that case, this body is not part of the
11981 -- current compilation, and is not attached to the tree, but its
11982 -- parent must be set for analysis.
11983
11984 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
11985
11986 -- Replace instance node with body of instance, and create new
11987 -- node for corresponding instance declaration.
11988
11989 Build_Instance_Compilation_Unit_Nodes
11990 (Inst_Node, Act_Body, Act_Decl);
11991 Analyze (Inst_Node);
11992
11993 if Parent (Inst_Node) = Cunit (Main_Unit) then
11994
11995 -- If the instance is a child unit itself, then set the scope
11996 -- of the expanded body to be the parent of the instantiation
11997 -- (ensuring that the fully qualified name will be generated
11998 -- for the elaboration subprogram).
11999
12000 if Nkind (Defining_Unit_Name (Act_Spec)) =
12001 N_Defining_Program_Unit_Name
12002 then
12003 Set_Scope (Defining_Entity (Inst_Node), Scope (Act_Decl_Id));
12004 end if;
12005 end if;
12006
12007 -- Case where instantiation is not a library unit
12008
12009 else
12010 -- If this is an early instantiation, i.e. appears textually
12011 -- before the corresponding body and must be elaborated first,
12012 -- indicate that the body instance is to be delayed.
12013
12014 Install_Body (Act_Body, Inst_Node, Gen_Body, Gen_Decl);
12015 Analyze (Act_Body);
12016 end if;
12017
12018 Inherit_Context (Gen_Body, Inst_Node);
12019
12020 -- Remove the parent instances if they have been placed on the scope
12021 -- stack to compile the body.
12022
12023 if Par_Installed then
12024 Remove_Parent (In_Body => True);
12025
12026 -- Restore the previous visibility of the parent
12027
12028 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
12029 end if;
12030
12031 Restore_Hidden_Primitives (Vis_Prims_List);
12032 Restore_Private_Views (Act_Decl_Id);
12033
12034 -- Remove the current unit from visibility if this is an instance
12035 -- that is not elaborated on the fly for inlining purposes.
12036
12037 if not Inlined_Body then
12038 Set_Is_Immediately_Visible (Act_Decl_Id, False);
12039 end if;
12040
12041 Restore_Env;
12042
12043 -- If we have no body, and the unit requires a body, then complain. This
12044 -- complaint is suppressed if we have detected other errors (since a
12045 -- common reason for missing the body is that it had errors).
12046 -- In CodePeer mode, a warning has been emitted already, no need for
12047 -- further messages.
12048
12049 elsif Unit_Requires_Body (Gen_Unit)
12050 and then not Body_Optional
12051 then
12052 if CodePeer_Mode then
12053 null;
12054
12055 elsif Serious_Errors_Detected = 0 then
12056 Error_Msg_NE
12057 ("cannot find body of generic package &", Inst_Node, Gen_Unit);
12058
12059 -- Don't attempt to perform any cleanup actions if some other error
12060 -- was already detected, since this can cause blowups.
12061
12062 else
12063 goto Leave;
12064 end if;
12065
12066 -- Case of package that does not need a body
12067
12068 else
12069 -- If the instantiation of the declaration is a library unit, rewrite
12070 -- the original package instantiation as a package declaration in the
12071 -- compilation unit node.
12072
12073 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
12074 Set_Parent_Spec (Act_Decl, Parent_Spec (Inst_Node));
12075 Rewrite (Inst_Node, Act_Decl);
12076
12077 -- Generate elaboration entity, in case spec has elaboration code.
12078 -- This cannot be done when the instance is analyzed, because it
12079 -- is not known yet whether the body exists.
12080
12081 Set_Elaboration_Entity_Required (Act_Decl_Id, False);
12082 Build_Elaboration_Entity (Parent (Inst_Node), Act_Decl_Id);
12083
12084 -- If the instantiation is not a library unit, then append the
12085 -- declaration to the list of implicitly generated entities, unless
12086 -- it is already a list member which means that it was already
12087 -- processed
12088
12089 elsif not Is_List_Member (Act_Decl) then
12090 Mark_Rewrite_Insertion (Act_Decl);
12091 Insert_Before (Inst_Node, Act_Decl);
12092 end if;
12093 end if;
12094
12095 <<Leave>>
12096
12097 -- Restore the context that was in effect prior to instantiating the
12098 -- package body.
12099
12100 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
12101 Local_Suppress_Stack_Top := Saved_LSST;
12102 Scope_Suppress := Saved_SS;
12103 Style_Check := Saved_SC;
12104
12105 Expander_Mode_Restore;
12106 Restore_Config_Switches (Saved_CS);
12107 Restore_Ghost_Region (Saved_GM, Saved_IGR);
12108 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
12109 Restore_Warnings (Saved_Warn);
12110 end Instantiate_Package_Body;
12111
12112 ---------------------------------
12113 -- Instantiate_Subprogram_Body --
12114 ---------------------------------
12115
12116 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
12117 -- must be replaced by gotos which jump to the end of the routine in order
12118 -- to restore the Ghost and SPARK modes.
12119
12120 procedure Instantiate_Subprogram_Body
12121 (Body_Info : Pending_Body_Info;
12122 Body_Optional : Boolean := False)
12123 is
12124 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
12125 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Decl);
12126 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
12127 Gen_Id : constant Node_Id := Name (Inst_Node);
12128 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
12129 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
12130 Loc : constant Source_Ptr := Sloc (Inst_Node);
12131 Pack_Id : constant Entity_Id :=
12132 Defining_Unit_Name (Parent (Act_Decl));
12133
12134 -- The following constants capture the context prior to instantiating
12135 -- the subprogram body.
12136
12137 Saved_CS : constant Config_Switches_Type := Save_Config_Switches;
12138 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
12139 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
12140 Saved_ISMP : constant Boolean :=
12141 Ignore_SPARK_Mode_Pragmas_In_Instance;
12142 Saved_LSST : constant Suppress_Stack_Entry_Ptr :=
12143 Local_Suppress_Stack_Top;
12144 Saved_SC : constant Boolean := Style_Check;
12145 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
12146 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
12147 Saved_SS : constant Suppress_Record := Scope_Suppress;
12148 Saved_Warn : constant Warning_Record := Save_Warnings;
12149
12150 Act_Body : Node_Id;
12151 Act_Body_Id : Entity_Id;
12152 Gen_Body : Node_Id;
12153 Gen_Body_Id : Node_Id;
12154 Pack_Body : Node_Id;
12155 Par_Ent : Entity_Id := Empty;
12156 Par_Installed : Boolean := False;
12157 Par_Vis : Boolean := False;
12158 Ret_Expr : Node_Id;
12159
12160 begin
12161 Gen_Body_Id := Corresponding_Body (Gen_Decl);
12162
12163 -- Subprogram body may have been created already because of an inline
12164 -- pragma, or because of multiple elaborations of the enclosing package
12165 -- when several instances of the subprogram appear in the main unit.
12166
12167 if Present (Corresponding_Body (Act_Decl)) then
12168 return;
12169 end if;
12170
12171 -- The subprogram being instantiated may be subject to pragma Ghost. Set
12172 -- the mode now to ensure that any nodes generated during instantiation
12173 -- are properly marked as Ghost.
12174
12175 Set_Ghost_Mode (Act_Decl_Id);
12176
12177 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
12178
12179 -- Re-establish the state of information on which checks are suppressed.
12180 -- This information was set in Body_Info at the point of instantiation,
12181 -- and now we restore it so that the instance is compiled using the
12182 -- check status at the instantiation (RM 11.5(7.2/2), AI95-00224-01).
12183
12184 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
12185 Scope_Suppress := Body_Info.Scope_Suppress;
12186
12187 Restore_Config_Switches (Body_Info.Config_Switches);
12188 Restore_Warnings (Body_Info.Warnings);
12189
12190 if No (Gen_Body_Id) then
12191
12192 -- For imported generic subprogram, no body to compile, complete
12193 -- the spec entity appropriately.
12194
12195 if Is_Imported (Gen_Unit) then
12196 Set_Is_Imported (Act_Decl_Id);
12197 Set_First_Rep_Item (Act_Decl_Id, First_Rep_Item (Gen_Unit));
12198 Set_Interface_Name (Act_Decl_Id, Interface_Name (Gen_Unit));
12199 Set_Convention (Act_Decl_Id, Convention (Gen_Unit));
12200 Set_Has_Completion (Act_Decl_Id);
12201 goto Leave;
12202
12203 -- For other cases, compile the body
12204
12205 else
12206 Load_Parent_Of_Generic
12207 (Inst_Node, Specification (Gen_Decl), Body_Optional);
12208 Gen_Body_Id := Corresponding_Body (Gen_Decl);
12209 end if;
12210 end if;
12211
12212 Instantiation_Node := Inst_Node;
12213
12214 if Present (Gen_Body_Id) then
12215 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
12216
12217 if Nkind (Gen_Body) = N_Subprogram_Body_Stub then
12218
12219 -- Either body is not present, or context is non-expanding, as
12220 -- when compiling a subunit. Mark the instance as completed, and
12221 -- diagnose a missing body when needed.
12222
12223 if Expander_Active
12224 and then Operating_Mode = Generate_Code
12225 then
12226 Error_Msg_N ("missing proper body for instantiation", Gen_Body);
12227 end if;
12228
12229 Set_Has_Completion (Act_Decl_Id);
12230 goto Leave;
12231 end if;
12232
12233 Save_Env (Gen_Unit, Act_Decl_Id);
12234 Style_Check := False;
12235
12236 -- If the context of the instance is subject to SPARK_Mode "off", the
12237 -- annotation is missing, or the body is instantiated at a later pass
12238 -- and its spec ignored SPARK_Mode pragma, set the global flag which
12239 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within the
12240 -- instance.
12241
12242 if SPARK_Mode /= On
12243 or else Ignore_SPARK_Mode_Pragmas (Act_Decl_Id)
12244 then
12245 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
12246 end if;
12247
12248 -- If the context of an instance is not subject to SPARK_Mode "off",
12249 -- and the generic body is subject to an explicit SPARK_Mode pragma,
12250 -- the latter should be the one applicable to the instance.
12251
12252 if not Ignore_SPARK_Mode_Pragmas_In_Instance
12253 and then SPARK_Mode /= Off
12254 and then Present (SPARK_Pragma (Gen_Body_Id))
12255 then
12256 Set_SPARK_Mode (Gen_Body_Id);
12257 end if;
12258
12259 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
12260 Create_Instantiation_Source
12261 (Inst_Node,
12262 Gen_Body_Id,
12263 S_Adjustment);
12264
12265 Act_Body :=
12266 Copy_Generic_Node
12267 (Original_Node (Gen_Body), Empty, Instantiating => True);
12268
12269 -- Create proper defining name for the body, to correspond to the one
12270 -- in the spec.
12271
12272 Act_Body_Id :=
12273 Make_Defining_Identifier (Sloc (Act_Decl_Id), Chars (Act_Decl_Id));
12274
12275 Preserve_Comes_From_Source (Act_Body_Id, Act_Decl_Id);
12276 Set_Defining_Unit_Name (Specification (Act_Body), Act_Body_Id);
12277
12278 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
12279 Set_Has_Completion (Act_Decl_Id);
12280 Check_Generic_Actuals (Pack_Id, False);
12281
12282 -- Generate a reference to link the visible subprogram instance to
12283 -- the generic body, which for navigation purposes is the only
12284 -- available source for the instance.
12285
12286 Generate_Reference
12287 (Related_Instance (Pack_Id),
12288 Gen_Body_Id, 'b', Set_Ref => False, Force => True);
12289
12290 -- If it is a child unit, make the parent instance (which is an
12291 -- instance of the parent of the generic) visible. The parent
12292 -- instance is the prefix of the name of the generic unit.
12293
12294 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
12295 and then Nkind (Gen_Id) = N_Expanded_Name
12296 then
12297 Par_Ent := Entity (Prefix (Gen_Id));
12298 Par_Vis := Is_Immediately_Visible (Par_Ent);
12299 Install_Parent (Par_Ent, In_Body => True);
12300 Par_Installed := True;
12301
12302 elsif Is_Child_Unit (Gen_Unit) then
12303 Par_Ent := Scope (Gen_Unit);
12304 Par_Vis := Is_Immediately_Visible (Par_Ent);
12305 Install_Parent (Par_Ent, In_Body => True);
12306 Par_Installed := True;
12307 end if;
12308
12309 -- Subprogram body is placed in the body of wrapper package,
12310 -- whose spec contains the subprogram declaration as well as
12311 -- the renaming declarations for the generic parameters.
12312
12313 Pack_Body :=
12314 Make_Package_Body (Loc,
12315 Defining_Unit_Name => New_Copy (Pack_Id),
12316 Declarations => New_List (Act_Body));
12317
12318 Set_Corresponding_Spec (Pack_Body, Pack_Id);
12319
12320 -- If the instantiation is a library unit, then build resulting
12321 -- compilation unit nodes for the instance. The declaration of
12322 -- the enclosing package is the grandparent of the subprogram
12323 -- declaration. First replace the instantiation node as the unit
12324 -- of the corresponding compilation.
12325
12326 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
12327 if Parent (Inst_Node) = Cunit (Main_Unit) then
12328 Set_Unit (Parent (Inst_Node), Inst_Node);
12329 Build_Instance_Compilation_Unit_Nodes
12330 (Inst_Node, Pack_Body, Parent (Parent (Act_Decl)));
12331 Analyze (Inst_Node);
12332 else
12333 Set_Parent (Pack_Body, Parent (Inst_Node));
12334 Analyze (Pack_Body);
12335 end if;
12336
12337 else
12338 Insert_Before (Inst_Node, Pack_Body);
12339 Mark_Rewrite_Insertion (Pack_Body);
12340 Analyze (Pack_Body);
12341
12342 if Expander_Active then
12343 Freeze_Subprogram_Body (Inst_Node, Gen_Body, Pack_Id);
12344 end if;
12345 end if;
12346
12347 Inherit_Context (Gen_Body, Inst_Node);
12348
12349 Restore_Private_Views (Pack_Id, False);
12350
12351 if Par_Installed then
12352 Remove_Parent (In_Body => True);
12353
12354 -- Restore the previous visibility of the parent
12355
12356 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
12357 end if;
12358
12359 Restore_Env;
12360
12361 -- Body not found. Error was emitted already. If there were no previous
12362 -- errors, this may be an instance whose scope is a premature instance.
12363 -- In that case we must insure that the (legal) program does raise
12364 -- program error if executed. We generate a subprogram body for this
12365 -- purpose. See DEC ac30vso.
12366
12367 -- Should not reference proprietary DEC tests in comments ???
12368
12369 elsif Serious_Errors_Detected = 0
12370 and then Nkind (Parent (Inst_Node)) /= N_Compilation_Unit
12371 then
12372 if Body_Optional then
12373 goto Leave;
12374
12375 elsif Ekind (Act_Decl_Id) = E_Procedure then
12376 Act_Body :=
12377 Make_Subprogram_Body (Loc,
12378 Specification =>
12379 Make_Procedure_Specification (Loc,
12380 Defining_Unit_Name =>
12381 Make_Defining_Identifier (Loc, Chars (Act_Decl_Id)),
12382 Parameter_Specifications =>
12383 New_Copy_List
12384 (Parameter_Specifications (Parent (Act_Decl_Id)))),
12385
12386 Declarations => Empty_List,
12387 Handled_Statement_Sequence =>
12388 Make_Handled_Sequence_Of_Statements (Loc,
12389 Statements => New_List (
12390 Make_Raise_Program_Error (Loc,
12391 Reason => PE_Access_Before_Elaboration))));
12392
12393 else
12394 Ret_Expr :=
12395 Make_Raise_Program_Error (Loc,
12396 Reason => PE_Access_Before_Elaboration);
12397
12398 Set_Etype (Ret_Expr, (Etype (Act_Decl_Id)));
12399 Set_Analyzed (Ret_Expr);
12400
12401 Act_Body :=
12402 Make_Subprogram_Body (Loc,
12403 Specification =>
12404 Make_Function_Specification (Loc,
12405 Defining_Unit_Name =>
12406 Make_Defining_Identifier (Loc, Chars (Act_Decl_Id)),
12407 Parameter_Specifications =>
12408 New_Copy_List
12409 (Parameter_Specifications (Parent (Act_Decl_Id))),
12410 Result_Definition =>
12411 New_Occurrence_Of (Etype (Act_Decl_Id), Loc)),
12412
12413 Declarations => Empty_List,
12414 Handled_Statement_Sequence =>
12415 Make_Handled_Sequence_Of_Statements (Loc,
12416 Statements => New_List (
12417 Make_Simple_Return_Statement (Loc, Ret_Expr))));
12418 end if;
12419
12420 Pack_Body :=
12421 Make_Package_Body (Loc,
12422 Defining_Unit_Name => New_Copy (Pack_Id),
12423 Declarations => New_List (Act_Body));
12424
12425 Insert_After (Inst_Node, Pack_Body);
12426 Set_Corresponding_Spec (Pack_Body, Pack_Id);
12427 Analyze (Pack_Body);
12428 end if;
12429
12430 <<Leave>>
12431
12432 -- Restore the context that was in effect prior to instantiating the
12433 -- subprogram body.
12434
12435 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
12436 Local_Suppress_Stack_Top := Saved_LSST;
12437 Scope_Suppress := Saved_SS;
12438 Style_Check := Saved_SC;
12439
12440 Expander_Mode_Restore;
12441 Restore_Config_Switches (Saved_CS);
12442 Restore_Ghost_Region (Saved_GM, Saved_IGR);
12443 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
12444 Restore_Warnings (Saved_Warn);
12445 end Instantiate_Subprogram_Body;
12446
12447 ----------------------
12448 -- Instantiate_Type --
12449 ----------------------
12450
12451 function Instantiate_Type
12452 (Formal : Node_Id;
12453 Actual : Node_Id;
12454 Analyzed_Formal : Node_Id;
12455 Actual_Decls : List_Id) return List_Id
12456 is
12457 A_Gen_T : constant Entity_Id :=
12458 Defining_Identifier (Analyzed_Formal);
12459 Def : constant Node_Id := Formal_Type_Definition (Formal);
12460 Gen_T : constant Entity_Id := Defining_Identifier (Formal);
12461 Act_T : Entity_Id;
12462 Ancestor : Entity_Id := Empty;
12463 Decl_Node : Node_Id;
12464 Decl_Nodes : List_Id;
12465 Loc : Source_Ptr;
12466 Subt : Entity_Id;
12467
12468 procedure Check_Shared_Variable_Control_Aspects;
12469 -- Ada 2020: Verify that shared variable control aspects (RM C.6)
12470 -- that may be specified for a formal type are obeyed by the actual.
12471
12472 procedure Diagnose_Predicated_Actual;
12473 -- There are a number of constructs in which a discrete type with
12474 -- predicates is illegal, e.g. as an index in an array type declaration.
12475 -- If a generic type is used is such a construct in a generic package
12476 -- declaration, it carries the flag No_Predicate_On_Actual. it is part
12477 -- of the generic contract that the actual cannot have predicates.
12478
12479 procedure Validate_Array_Type_Instance;
12480 procedure Validate_Access_Subprogram_Instance;
12481 procedure Validate_Access_Type_Instance;
12482 procedure Validate_Derived_Type_Instance;
12483 procedure Validate_Derived_Interface_Type_Instance;
12484 procedure Validate_Discriminated_Formal_Type;
12485 procedure Validate_Interface_Type_Instance;
12486 procedure Validate_Private_Type_Instance;
12487 procedure Validate_Incomplete_Type_Instance;
12488 -- These procedures perform validation tests for the named case.
12489 -- Validate_Discriminated_Formal_Type is shared by formal private
12490 -- types and Ada 2012 formal incomplete types.
12491
12492 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean;
12493 -- Check that base types are the same and that the subtypes match
12494 -- statically. Used in several of the above.
12495
12496 --------------------------------------------
12497 -- Check_Shared_Variable_Control_Aspects --
12498 --------------------------------------------
12499
12500 -- Ada 2020: Verify that shared variable control aspects (RM C.6)
12501 -- that may be specified for the formal are obeyed by the actual.
12502 -- If the formal is a derived type the aspect specifications must match.
12503 -- NOTE: AI12-0282 implies that matching of aspects is required between
12504 -- formal and actual in all cases, but this is too restrictive.
12505 -- In particular it violates a language design rule: a limited private
12506 -- indefinite formal can be matched by any actual. The current code
12507 -- reflects an older and more permissive version of RM C.6 (12/5).
12508
12509 procedure Check_Shared_Variable_Control_Aspects is
12510 begin
12511 if Ada_Version >= Ada_2020 then
12512 if Is_Atomic (A_Gen_T) and then not Is_Atomic (Act_T) then
12513 Error_Msg_NE
12514 ("actual for& must have Atomic aspect", Actual, A_Gen_T);
12515
12516 elsif Is_Derived_Type (A_Gen_T)
12517 and then Is_Atomic (A_Gen_T) /= Is_Atomic (Act_T)
12518 then
12519 Error_Msg_NE
12520 ("actual for& has different Atomic aspect", Actual, A_Gen_T);
12521 end if;
12522
12523 if Is_Volatile (A_Gen_T) and then not Is_Volatile (Act_T) then
12524 Error_Msg_NE
12525 ("actual for& has different Volatile aspect",
12526 Actual, A_Gen_T);
12527
12528 elsif Is_Derived_Type (A_Gen_T)
12529 and then Is_Volatile (A_Gen_T) /= Is_Volatile (Act_T)
12530 then
12531 Error_Msg_NE
12532 ("actual for& has different Volatile aspect",
12533 Actual, A_Gen_T);
12534 end if;
12535
12536 -- We assume that an array type whose atomic component type
12537 -- is Atomic is equivalent to an array type with the explicit
12538 -- aspect Has_Atomic_Components. This is a reasonable inference
12539 -- from the intent of AI12-0282, and makes it legal to use an
12540 -- actual that does not have the identical aspect as the formal.
12541 -- Ditto for volatile components.
12542
12543 declare
12544 Actual_Atomic_Comp : constant Boolean :=
12545 Has_Atomic_Components (Act_T)
12546 or else (Is_Array_Type (Act_T)
12547 and then Is_Atomic (Component_Type (Act_T)));
12548 begin
12549 if Has_Atomic_Components (A_Gen_T) /= Actual_Atomic_Comp then
12550 Error_Msg_NE
12551 ("formal and actual for& must agree on atomic components",
12552 Actual, A_Gen_T);
12553 end if;
12554 end;
12555
12556 declare
12557 Actual_Volatile_Comp : constant Boolean :=
12558 Has_Volatile_Components (Act_T)
12559 or else (Is_Array_Type (Act_T)
12560 and then Is_Volatile (Component_Type (Act_T)));
12561 begin
12562 if Has_Volatile_Components (A_Gen_T) /= Actual_Volatile_Comp
12563 then
12564 Error_Msg_NE
12565 ("actual for& must have volatile components",
12566 Actual, A_Gen_T);
12567 end if;
12568 end;
12569
12570 -- The following two aspects do not require exact matching,
12571 -- but only one-way agreement. See RM C.6.
12572
12573 if Is_Independent (A_Gen_T) and then not Is_Independent (Act_T)
12574 then
12575 Error_Msg_NE
12576 ("actual for& must have Independent aspect specified",
12577 Actual, A_Gen_T);
12578 end if;
12579
12580 if Has_Independent_Components (A_Gen_T)
12581 and then not Has_Independent_Components (Act_T)
12582 then
12583 Error_Msg_NE
12584 ("actual for& must have Independent_Components specified",
12585 Actual, A_Gen_T);
12586 end if;
12587
12588 -- Check actual/formal compatibility with respect to the four
12589 -- volatility refinement aspects.
12590
12591 Check_Volatility_Compatibility
12592 (Act_T, A_Gen_T,
12593 "actual type", "its corresponding formal type",
12594 Srcpos_Bearer => Act_T);
12595 end if;
12596 end Check_Shared_Variable_Control_Aspects;
12597
12598 ---------------------------------
12599 -- Diagnose_Predicated_Actual --
12600 ---------------------------------
12601
12602 procedure Diagnose_Predicated_Actual is
12603 begin
12604 if No_Predicate_On_Actual (A_Gen_T)
12605 and then Has_Predicates (Act_T)
12606 then
12607 Error_Msg_NE
12608 ("actual for& cannot be a type with predicate",
12609 Instantiation_Node, A_Gen_T);
12610
12611 elsif No_Dynamic_Predicate_On_Actual (A_Gen_T)
12612 and then Has_Predicates (Act_T)
12613 and then not Has_Static_Predicate_Aspect (Act_T)
12614 then
12615 Error_Msg_NE
12616 ("actual for& cannot be a type with a dynamic predicate",
12617 Instantiation_Node, A_Gen_T);
12618 end if;
12619 end Diagnose_Predicated_Actual;
12620
12621 --------------------
12622 -- Subtypes_Match --
12623 --------------------
12624
12625 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean is
12626 T : constant Entity_Id := Get_Instance_Of (Gen_T);
12627
12628 begin
12629 -- Some detailed comments would be useful here ???
12630
12631 return ((Base_Type (T) = Act_T
12632 or else Base_Type (T) = Base_Type (Act_T))
12633 and then Subtypes_Statically_Match (T, Act_T))
12634
12635 or else (Is_Class_Wide_Type (Gen_T)
12636 and then Is_Class_Wide_Type (Act_T)
12637 and then Subtypes_Match
12638 (Get_Instance_Of (Root_Type (Gen_T)),
12639 Root_Type (Act_T)))
12640
12641 or else
12642 (Ekind_In (Gen_T, E_Anonymous_Access_Subprogram_Type,
12643 E_Anonymous_Access_Type)
12644 and then Ekind (Act_T) = Ekind (Gen_T)
12645 and then Subtypes_Statically_Match
12646 (Designated_Type (Gen_T), Designated_Type (Act_T)));
12647 end Subtypes_Match;
12648
12649 -----------------------------------------
12650 -- Validate_Access_Subprogram_Instance --
12651 -----------------------------------------
12652
12653 procedure Validate_Access_Subprogram_Instance is
12654 begin
12655 if not Is_Access_Type (Act_T)
12656 or else Ekind (Designated_Type (Act_T)) /= E_Subprogram_Type
12657 then
12658 Error_Msg_NE
12659 ("expect access type in instantiation of &", Actual, Gen_T);
12660 Abandon_Instantiation (Actual);
12661 end if;
12662
12663 -- According to AI05-288, actuals for access_to_subprograms must be
12664 -- subtype conformant with the generic formal. Previous to AI05-288
12665 -- only mode conformance was required.
12666
12667 -- This is a binding interpretation that applies to previous versions
12668 -- of the language, no need to maintain previous weaker checks.
12669
12670 Check_Subtype_Conformant
12671 (Designated_Type (Act_T),
12672 Designated_Type (A_Gen_T),
12673 Actual,
12674 Get_Inst => True);
12675
12676 if Ekind (Base_Type (Act_T)) = E_Access_Protected_Subprogram_Type then
12677 if Ekind (A_Gen_T) = E_Access_Subprogram_Type then
12678 Error_Msg_NE
12679 ("protected access type not allowed for formal &",
12680 Actual, Gen_T);
12681 end if;
12682
12683 elsif Ekind (A_Gen_T) = E_Access_Protected_Subprogram_Type then
12684 Error_Msg_NE
12685 ("expect protected access type for formal &",
12686 Actual, Gen_T);
12687 end if;
12688
12689 -- If the formal has a specified convention (which in most cases
12690 -- will be StdCall) verify that the actual has the same convention.
12691
12692 if Has_Convention_Pragma (A_Gen_T)
12693 and then Convention (A_Gen_T) /= Convention (Act_T)
12694 then
12695 Error_Msg_Name_1 := Get_Convention_Name (Convention (A_Gen_T));
12696 Error_Msg_NE
12697 ("actual for formal & must have convention %", Actual, Gen_T);
12698 end if;
12699
12700 if Can_Never_Be_Null (A_Gen_T) /= Can_Never_Be_Null (Act_T) then
12701 Error_Msg_NE
12702 ("non null exclusion of actual and formal & do not match",
12703 Actual, Gen_T);
12704 end if;
12705 end Validate_Access_Subprogram_Instance;
12706
12707 -----------------------------------
12708 -- Validate_Access_Type_Instance --
12709 -----------------------------------
12710
12711 procedure Validate_Access_Type_Instance is
12712 Desig_Type : constant Entity_Id :=
12713 Find_Actual_Type (Designated_Type (A_Gen_T), A_Gen_T);
12714 Desig_Act : Entity_Id;
12715
12716 begin
12717 if not Is_Access_Type (Act_T) then
12718 Error_Msg_NE
12719 ("expect access type in instantiation of &", Actual, Gen_T);
12720 Abandon_Instantiation (Actual);
12721 end if;
12722
12723 if Is_Access_Constant (A_Gen_T) then
12724 if not Is_Access_Constant (Act_T) then
12725 Error_Msg_N
12726 ("actual type must be access-to-constant type", Actual);
12727 Abandon_Instantiation (Actual);
12728 end if;
12729 else
12730 if Is_Access_Constant (Act_T) then
12731 Error_Msg_N
12732 ("actual type must be access-to-variable type", Actual);
12733 Abandon_Instantiation (Actual);
12734
12735 elsif Ekind (A_Gen_T) = E_General_Access_Type
12736 and then Ekind (Base_Type (Act_T)) /= E_General_Access_Type
12737 then
12738 Error_Msg_N -- CODEFIX
12739 ("actual must be general access type!", Actual);
12740 Error_Msg_NE -- CODEFIX
12741 ("add ALL to }!", Actual, Act_T);
12742 Abandon_Instantiation (Actual);
12743 end if;
12744 end if;
12745
12746 -- The designated subtypes, that is to say the subtypes introduced
12747 -- by an access type declaration (and not by a subtype declaration)
12748 -- must match.
12749
12750 Desig_Act := Designated_Type (Base_Type (Act_T));
12751
12752 -- The designated type may have been introduced through a limited_
12753 -- with clause, in which case retrieve the non-limited view. This
12754 -- applies to incomplete types as well as to class-wide types.
12755
12756 if From_Limited_With (Desig_Act) then
12757 Desig_Act := Available_View (Desig_Act);
12758 end if;
12759
12760 if not Subtypes_Match (Desig_Type, Desig_Act) then
12761 Error_Msg_NE
12762 ("designated type of actual does not match that of formal &",
12763 Actual, Gen_T);
12764
12765 if not Predicates_Match (Desig_Type, Desig_Act) then
12766 Error_Msg_N ("\predicates do not match", Actual);
12767 end if;
12768
12769 Abandon_Instantiation (Actual);
12770
12771 elsif Is_Access_Type (Designated_Type (Act_T))
12772 and then Is_Constrained (Designated_Type (Designated_Type (Act_T)))
12773 /=
12774 Is_Constrained (Designated_Type (Desig_Type))
12775 then
12776 Error_Msg_NE
12777 ("designated type of actual does not match that of formal &",
12778 Actual, Gen_T);
12779
12780 if not Predicates_Match (Desig_Type, Desig_Act) then
12781 Error_Msg_N ("\predicates do not match", Actual);
12782 end if;
12783
12784 Abandon_Instantiation (Actual);
12785 end if;
12786
12787 -- Ada 2005: null-exclusion indicators of the two types must agree
12788
12789 if Can_Never_Be_Null (A_Gen_T) /= Can_Never_Be_Null (Act_T) then
12790 Error_Msg_NE
12791 ("non null exclusion of actual and formal & do not match",
12792 Actual, Gen_T);
12793 end if;
12794 end Validate_Access_Type_Instance;
12795
12796 ----------------------------------
12797 -- Validate_Array_Type_Instance --
12798 ----------------------------------
12799
12800 procedure Validate_Array_Type_Instance is
12801 I1 : Node_Id;
12802 I2 : Node_Id;
12803 T2 : Entity_Id;
12804
12805 function Formal_Dimensions return Nat;
12806 -- Count number of dimensions in array type formal
12807
12808 -----------------------
12809 -- Formal_Dimensions --
12810 -----------------------
12811
12812 function Formal_Dimensions return Nat is
12813 Num : Nat := 0;
12814 Index : Node_Id;
12815
12816 begin
12817 if Nkind (Def) = N_Constrained_Array_Definition then
12818 Index := First (Discrete_Subtype_Definitions (Def));
12819 else
12820 Index := First (Subtype_Marks (Def));
12821 end if;
12822
12823 while Present (Index) loop
12824 Num := Num + 1;
12825 Next_Index (Index);
12826 end loop;
12827
12828 return Num;
12829 end Formal_Dimensions;
12830
12831 -- Start of processing for Validate_Array_Type_Instance
12832
12833 begin
12834 if not Is_Array_Type (Act_T) then
12835 Error_Msg_NE
12836 ("expect array type in instantiation of &", Actual, Gen_T);
12837 Abandon_Instantiation (Actual);
12838
12839 elsif Nkind (Def) = N_Constrained_Array_Definition then
12840 if not (Is_Constrained (Act_T)) then
12841 Error_Msg_NE
12842 ("expect constrained array in instantiation of &",
12843 Actual, Gen_T);
12844 Abandon_Instantiation (Actual);
12845 end if;
12846
12847 else
12848 if Is_Constrained (Act_T) then
12849 Error_Msg_NE
12850 ("expect unconstrained array in instantiation of &",
12851 Actual, Gen_T);
12852 Abandon_Instantiation (Actual);
12853 end if;
12854 end if;
12855
12856 if Formal_Dimensions /= Number_Dimensions (Act_T) then
12857 Error_Msg_NE
12858 ("dimensions of actual do not match formal &", Actual, Gen_T);
12859 Abandon_Instantiation (Actual);
12860 end if;
12861
12862 I1 := First_Index (A_Gen_T);
12863 I2 := First_Index (Act_T);
12864 for J in 1 .. Formal_Dimensions loop
12865
12866 -- If the indexes of the actual were given by a subtype_mark,
12867 -- the index was transformed into a range attribute. Retrieve
12868 -- the original type mark for checking.
12869
12870 if Is_Entity_Name (Original_Node (I2)) then
12871 T2 := Entity (Original_Node (I2));
12872 else
12873 T2 := Etype (I2);
12874 end if;
12875
12876 if not Subtypes_Match
12877 (Find_Actual_Type (Etype (I1), A_Gen_T), T2)
12878 then
12879 Error_Msg_NE
12880 ("index types of actual do not match those of formal &",
12881 Actual, Gen_T);
12882 Abandon_Instantiation (Actual);
12883 end if;
12884
12885 Next_Index (I1);
12886 Next_Index (I2);
12887 end loop;
12888
12889 -- Check matching subtypes. Note that there are complex visibility
12890 -- issues when the generic is a child unit and some aspect of the
12891 -- generic type is declared in a parent unit of the generic. We do
12892 -- the test to handle this special case only after a direct check
12893 -- for static matching has failed. The case where both the component
12894 -- type and the array type are separate formals, and the component
12895 -- type is a private view may also require special checking in
12896 -- Subtypes_Match. Finally, we assume that a child instance where
12897 -- the component type comes from a formal of a parent instance is
12898 -- correct because the generic was correct. A more precise check
12899 -- seems too complex to install???
12900
12901 if Subtypes_Match
12902 (Component_Type (A_Gen_T), Component_Type (Act_T))
12903 or else
12904 Subtypes_Match
12905 (Find_Actual_Type (Component_Type (A_Gen_T), A_Gen_T),
12906 Component_Type (Act_T))
12907 or else
12908 (not Inside_A_Generic
12909 and then Is_Child_Unit (Scope (Component_Type (A_Gen_T))))
12910 then
12911 null;
12912 else
12913 Error_Msg_NE
12914 ("component subtype of actual does not match that of formal &",
12915 Actual, Gen_T);
12916 Abandon_Instantiation (Actual);
12917 end if;
12918
12919 if Has_Aliased_Components (A_Gen_T)
12920 and then not Has_Aliased_Components (Act_T)
12921 then
12922 Error_Msg_NE
12923 ("actual must have aliased components to match formal type &",
12924 Actual, Gen_T);
12925 end if;
12926 end Validate_Array_Type_Instance;
12927
12928 -----------------------------------------------
12929 -- Validate_Derived_Interface_Type_Instance --
12930 -----------------------------------------------
12931
12932 procedure Validate_Derived_Interface_Type_Instance is
12933 Par : constant Entity_Id := Entity (Subtype_Indication (Def));
12934 Elmt : Elmt_Id;
12935
12936 begin
12937 -- First apply interface instance checks
12938
12939 Validate_Interface_Type_Instance;
12940
12941 -- Verify that immediate parent interface is an ancestor of
12942 -- the actual.
12943
12944 if Present (Par)
12945 and then not Interface_Present_In_Ancestor (Act_T, Par)
12946 then
12947 Error_Msg_NE
12948 ("interface actual must include progenitor&", Actual, Par);
12949 end if;
12950
12951 -- Now verify that the actual includes all other ancestors of
12952 -- the formal.
12953
12954 Elmt := First_Elmt (Interfaces (A_Gen_T));
12955 while Present (Elmt) loop
12956 if not Interface_Present_In_Ancestor
12957 (Act_T, Get_Instance_Of (Node (Elmt)))
12958 then
12959 Error_Msg_NE
12960 ("interface actual must include progenitor&",
12961 Actual, Node (Elmt));
12962 end if;
12963
12964 Next_Elmt (Elmt);
12965 end loop;
12966 end Validate_Derived_Interface_Type_Instance;
12967
12968 ------------------------------------
12969 -- Validate_Derived_Type_Instance --
12970 ------------------------------------
12971
12972 procedure Validate_Derived_Type_Instance is
12973 Actual_Discr : Entity_Id;
12974 Ancestor_Discr : Entity_Id;
12975
12976 begin
12977 -- Verify that the actual includes the progenitors of the formal,
12978 -- if any. The formal may depend on previous formals and their
12979 -- instance, so we must examine instance of interfaces if present.
12980 -- The actual may be an extension of an interface, in which case
12981 -- it does not appear in the interface list, so this must be
12982 -- checked separately.
12983
12984 if Present (Interface_List (Def)) then
12985 if not Has_Interfaces (Act_T) then
12986 Error_Msg_NE
12987 ("actual must implement all interfaces of formal&",
12988 Actual, A_Gen_T);
12989
12990 else
12991 declare
12992 Act_Iface_List : Elist_Id;
12993 Iface : Node_Id;
12994 Iface_Ent : Entity_Id;
12995
12996 function Instance_Exists (I : Entity_Id) return Boolean;
12997 -- If the interface entity is declared in a generic unit,
12998 -- this can only be legal if we are within an instantiation
12999 -- of a child of that generic. There is currently no
13000 -- mechanism to relate an interface declared within a
13001 -- generic to the corresponding interface in an instance,
13002 -- so we traverse the list of interfaces of the actual,
13003 -- looking for a name match.
13004
13005 ---------------------
13006 -- Instance_Exists --
13007 ---------------------
13008
13009 function Instance_Exists (I : Entity_Id) return Boolean is
13010 Iface_Elmt : Elmt_Id;
13011
13012 begin
13013 Iface_Elmt := First_Elmt (Act_Iface_List);
13014 while Present (Iface_Elmt) loop
13015 if Is_Generic_Instance (Scope (Node (Iface_Elmt)))
13016 and then Chars (Node (Iface_Elmt)) = Chars (I)
13017 then
13018 return True;
13019 end if;
13020
13021 Next_Elmt (Iface_Elmt);
13022 end loop;
13023
13024 return False;
13025 end Instance_Exists;
13026
13027 begin
13028 Iface := First (Abstract_Interface_List (A_Gen_T));
13029 Collect_Interfaces (Act_T, Act_Iface_List);
13030
13031 while Present (Iface) loop
13032 Iface_Ent := Get_Instance_Of (Entity (Iface));
13033
13034 if Is_Ancestor (Iface_Ent, Act_T)
13035 or else Is_Progenitor (Iface_Ent, Act_T)
13036 then
13037 null;
13038
13039 elsif Ekind (Scope (Iface_Ent)) = E_Generic_Package
13040 and then Instance_Exists (Iface_Ent)
13041 then
13042 null;
13043
13044 else
13045 Error_Msg_Name_1 := Chars (Act_T);
13046 Error_Msg_NE
13047 ("Actual% must implement interface&",
13048 Actual, Etype (Iface));
13049 end if;
13050
13051 Next (Iface);
13052 end loop;
13053 end;
13054 end if;
13055 end if;
13056
13057 -- If the parent type in the generic declaration is itself a previous
13058 -- formal type, then it is local to the generic and absent from the
13059 -- analyzed generic definition. In that case the ancestor is the
13060 -- instance of the formal (which must have been instantiated
13061 -- previously), unless the ancestor is itself a formal derived type.
13062 -- In this latter case (which is the subject of Corrigendum 8652/0038
13063 -- (AI-202) the ancestor of the formals is the ancestor of its
13064 -- parent. Otherwise, the analyzed generic carries the parent type.
13065 -- If the parent type is defined in a previous formal package, then
13066 -- the scope of that formal package is that of the generic type
13067 -- itself, and it has already been mapped into the corresponding type
13068 -- in the actual package.
13069
13070 -- Common case: parent type defined outside of the generic
13071
13072 if Is_Entity_Name (Subtype_Mark (Def))
13073 and then Present (Entity (Subtype_Mark (Def)))
13074 then
13075 Ancestor := Get_Instance_Of (Entity (Subtype_Mark (Def)));
13076
13077 -- Check whether parent is defined in a previous formal package
13078
13079 elsif
13080 Scope (Scope (Base_Type (Etype (A_Gen_T)))) = Scope (A_Gen_T)
13081 then
13082 Ancestor :=
13083 Get_Instance_Of (Base_Type (Etype (A_Gen_T)));
13084
13085 -- The type may be a local derivation, or a type extension of a
13086 -- previous formal, or of a formal of a parent package.
13087
13088 elsif Is_Derived_Type (Get_Instance_Of (A_Gen_T))
13089 or else
13090 Ekind (Get_Instance_Of (A_Gen_T)) = E_Record_Type_With_Private
13091 then
13092 -- Check whether the parent is another derived formal type in the
13093 -- same generic unit.
13094
13095 if Etype (A_Gen_T) /= A_Gen_T
13096 and then Is_Generic_Type (Etype (A_Gen_T))
13097 and then Scope (Etype (A_Gen_T)) = Scope (A_Gen_T)
13098 and then Etype (Etype (A_Gen_T)) /= Etype (A_Gen_T)
13099 then
13100 -- Locate ancestor of parent from the subtype declaration
13101 -- created for the actual.
13102
13103 declare
13104 Decl : Node_Id;
13105
13106 begin
13107 Decl := First (Actual_Decls);
13108 while Present (Decl) loop
13109 if Nkind (Decl) = N_Subtype_Declaration
13110 and then Chars (Defining_Identifier (Decl)) =
13111 Chars (Etype (A_Gen_T))
13112 then
13113 Ancestor := Generic_Parent_Type (Decl);
13114 exit;
13115 else
13116 Next (Decl);
13117 end if;
13118 end loop;
13119 end;
13120
13121 pragma Assert (Present (Ancestor));
13122
13123 -- The ancestor itself may be a previous formal that has been
13124 -- instantiated.
13125
13126 Ancestor := Get_Instance_Of (Ancestor);
13127
13128 else
13129 Ancestor :=
13130 Get_Instance_Of (Base_Type (Get_Instance_Of (A_Gen_T)));
13131 end if;
13132
13133 -- Check whether parent is a previous formal of the current generic
13134
13135 elsif Is_Derived_Type (A_Gen_T)
13136 and then Is_Generic_Type (Etype (A_Gen_T))
13137 and then Scope (A_Gen_T) = Scope (Etype (A_Gen_T))
13138 then
13139 Ancestor := Get_Instance_Of (First_Subtype (Etype (A_Gen_T)));
13140
13141 -- An unusual case: the actual is a type declared in a parent unit,
13142 -- but is not a formal type so there is no instance_of for it.
13143 -- Retrieve it by analyzing the record extension.
13144
13145 elsif Is_Child_Unit (Scope (A_Gen_T))
13146 and then In_Open_Scopes (Scope (Act_T))
13147 and then Is_Generic_Instance (Scope (Act_T))
13148 then
13149 Analyze (Subtype_Mark (Def));
13150 Ancestor := Entity (Subtype_Mark (Def));
13151
13152 else
13153 Ancestor := Get_Instance_Of (Etype (Base_Type (A_Gen_T)));
13154 end if;
13155
13156 -- If the formal derived type has pragma Preelaborable_Initialization
13157 -- then the actual type must have preelaborable initialization.
13158
13159 if Known_To_Have_Preelab_Init (A_Gen_T)
13160 and then not Has_Preelaborable_Initialization (Act_T)
13161 then
13162 Error_Msg_NE
13163 ("actual for & must have preelaborable initialization",
13164 Actual, Gen_T);
13165 end if;
13166
13167 -- Ada 2005 (AI-251)
13168
13169 if Ada_Version >= Ada_2005 and then Is_Interface (Ancestor) then
13170 if not Interface_Present_In_Ancestor (Act_T, Ancestor) then
13171 Error_Msg_NE
13172 ("(Ada 2005) expected type implementing & in instantiation",
13173 Actual, Ancestor);
13174 end if;
13175
13176 -- Finally verify that the (instance of) the ancestor is an ancestor
13177 -- of the actual.
13178
13179 elsif not Is_Ancestor (Base_Type (Ancestor), Act_T) then
13180 Error_Msg_NE
13181 ("expect type derived from & in instantiation",
13182 Actual, First_Subtype (Ancestor));
13183 Abandon_Instantiation (Actual);
13184 end if;
13185
13186 -- Ada 2005 (AI-443): Synchronized formal derived type checks. Note
13187 -- that the formal type declaration has been rewritten as a private
13188 -- extension.
13189
13190 if Ada_Version >= Ada_2005
13191 and then Nkind (Parent (A_Gen_T)) = N_Private_Extension_Declaration
13192 and then Synchronized_Present (Parent (A_Gen_T))
13193 then
13194 -- The actual must be a synchronized tagged type
13195
13196 if not Is_Tagged_Type (Act_T) then
13197 Error_Msg_N
13198 ("actual of synchronized type must be tagged", Actual);
13199 Abandon_Instantiation (Actual);
13200
13201 elsif Nkind (Parent (Act_T)) = N_Full_Type_Declaration
13202 and then Nkind (Type_Definition (Parent (Act_T))) =
13203 N_Derived_Type_Definition
13204 and then not Synchronized_Present
13205 (Type_Definition (Parent (Act_T)))
13206 then
13207 Error_Msg_N
13208 ("actual of synchronized type must be synchronized", Actual);
13209 Abandon_Instantiation (Actual);
13210 end if;
13211 end if;
13212
13213 -- Perform atomic/volatile checks (RM C.6(12)). Note that AI05-0218-1
13214 -- removes the second instance of the phrase "or allow pass by copy".
13215
13216 -- For Ada 2020, the aspect may be specified explicitly for the
13217 -- formal regardless of whether an ancestor obeys it.
13218
13219 if Is_Atomic (Act_T)
13220 and then not Is_Atomic (Ancestor)
13221 and then not Is_Atomic (A_Gen_T)
13222 then
13223 Error_Msg_N
13224 ("cannot have atomic actual type for non-atomic formal type",
13225 Actual);
13226
13227 elsif Is_Volatile (Act_T)
13228 and then not Is_Volatile (Ancestor)
13229 and then not Is_Volatile (A_Gen_T)
13230 then
13231 Error_Msg_N
13232 ("cannot have volatile actual type for non-volatile formal type",
13233 Actual);
13234 end if;
13235
13236 -- It should not be necessary to check for unknown discriminants on
13237 -- Formal, but for some reason Has_Unknown_Discriminants is false for
13238 -- A_Gen_T, so Is_Definite_Subtype incorrectly returns True. This
13239 -- needs fixing. ???
13240
13241 if Is_Definite_Subtype (A_Gen_T)
13242 and then not Unknown_Discriminants_Present (Formal)
13243 and then not Is_Definite_Subtype (Act_T)
13244 then
13245 Error_Msg_N ("actual subtype must be constrained", Actual);
13246 Abandon_Instantiation (Actual);
13247 end if;
13248
13249 if not Unknown_Discriminants_Present (Formal) then
13250 if Is_Constrained (Ancestor) then
13251 if not Is_Constrained (Act_T) then
13252 Error_Msg_N ("actual subtype must be constrained", Actual);
13253 Abandon_Instantiation (Actual);
13254 end if;
13255
13256 -- Ancestor is unconstrained, Check if generic formal and actual
13257 -- agree on constrainedness. The check only applies to array types
13258 -- and discriminated types.
13259
13260 elsif Is_Constrained (Act_T) then
13261 if Ekind (Ancestor) = E_Access_Type
13262 or else (not Is_Constrained (A_Gen_T)
13263 and then Is_Composite_Type (A_Gen_T))
13264 then
13265 Error_Msg_N ("actual subtype must be unconstrained", Actual);
13266 Abandon_Instantiation (Actual);
13267 end if;
13268
13269 -- A class-wide type is only allowed if the formal has unknown
13270 -- discriminants.
13271
13272 elsif Is_Class_Wide_Type (Act_T)
13273 and then not Has_Unknown_Discriminants (Ancestor)
13274 then
13275 Error_Msg_NE
13276 ("actual for & cannot be a class-wide type", Actual, Gen_T);
13277 Abandon_Instantiation (Actual);
13278
13279 -- Otherwise, the formal and actual must have the same number
13280 -- of discriminants and each discriminant of the actual must
13281 -- correspond to a discriminant of the formal.
13282
13283 elsif Has_Discriminants (Act_T)
13284 and then not Has_Unknown_Discriminants (Act_T)
13285 and then Has_Discriminants (Ancestor)
13286 then
13287 Actual_Discr := First_Discriminant (Act_T);
13288 Ancestor_Discr := First_Discriminant (Ancestor);
13289 while Present (Actual_Discr)
13290 and then Present (Ancestor_Discr)
13291 loop
13292 if Base_Type (Act_T) /= Base_Type (Ancestor) and then
13293 No (Corresponding_Discriminant (Actual_Discr))
13294 then
13295 Error_Msg_NE
13296 ("discriminant & does not correspond "
13297 & "to ancestor discriminant", Actual, Actual_Discr);
13298 Abandon_Instantiation (Actual);
13299 end if;
13300
13301 Next_Discriminant (Actual_Discr);
13302 Next_Discriminant (Ancestor_Discr);
13303 end loop;
13304
13305 if Present (Actual_Discr) or else Present (Ancestor_Discr) then
13306 Error_Msg_NE
13307 ("actual for & must have same number of discriminants",
13308 Actual, Gen_T);
13309 Abandon_Instantiation (Actual);
13310 end if;
13311
13312 -- This case should be caught by the earlier check for
13313 -- constrainedness, but the check here is added for completeness.
13314
13315 elsif Has_Discriminants (Act_T)
13316 and then not Has_Unknown_Discriminants (Act_T)
13317 then
13318 Error_Msg_NE
13319 ("actual for & must not have discriminants", Actual, Gen_T);
13320 Abandon_Instantiation (Actual);
13321
13322 elsif Has_Discriminants (Ancestor) then
13323 Error_Msg_NE
13324 ("actual for & must have known discriminants", Actual, Gen_T);
13325 Abandon_Instantiation (Actual);
13326 end if;
13327
13328 if not Subtypes_Statically_Compatible
13329 (Act_T, Ancestor, Formal_Derived_Matching => True)
13330 then
13331 Error_Msg_NE
13332 ("actual for & must be statically compatible with ancestor",
13333 Actual, Gen_T);
13334
13335 if not Predicates_Compatible (Act_T, Ancestor) then
13336 Error_Msg_N
13337 ("\predicate on actual is not compatible with ancestor",
13338 Actual);
13339 end if;
13340
13341 Abandon_Instantiation (Actual);
13342 end if;
13343 end if;
13344
13345 -- If the formal and actual types are abstract, check that there
13346 -- are no abstract primitives of the actual type that correspond to
13347 -- nonabstract primitives of the formal type (second sentence of
13348 -- RM95 3.9.3(9)).
13349
13350 if Is_Abstract_Type (A_Gen_T) and then Is_Abstract_Type (Act_T) then
13351 Check_Abstract_Primitives : declare
13352 Gen_Prims : constant Elist_Id :=
13353 Primitive_Operations (A_Gen_T);
13354 Gen_Elmt : Elmt_Id;
13355 Gen_Subp : Entity_Id;
13356 Anc_Subp : Entity_Id;
13357 Anc_Formal : Entity_Id;
13358 Anc_F_Type : Entity_Id;
13359
13360 Act_Prims : constant Elist_Id := Primitive_Operations (Act_T);
13361 Act_Elmt : Elmt_Id;
13362 Act_Subp : Entity_Id;
13363 Act_Formal : Entity_Id;
13364 Act_F_Type : Entity_Id;
13365
13366 Subprograms_Correspond : Boolean;
13367
13368 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean;
13369 -- Returns true if T2 is derived directly or indirectly from
13370 -- T1, including derivations from interfaces. T1 and T2 are
13371 -- required to be specific tagged base types.
13372
13373 ------------------------
13374 -- Is_Tagged_Ancestor --
13375 ------------------------
13376
13377 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean
13378 is
13379 Intfc_Elmt : Elmt_Id;
13380
13381 begin
13382 -- The predicate is satisfied if the types are the same
13383
13384 if T1 = T2 then
13385 return True;
13386
13387 -- If we've reached the top of the derivation chain then
13388 -- we know that T1 is not an ancestor of T2.
13389
13390 elsif Etype (T2) = T2 then
13391 return False;
13392
13393 -- Proceed to check T2's immediate parent
13394
13395 elsif Is_Ancestor (T1, Base_Type (Etype (T2))) then
13396 return True;
13397
13398 -- Finally, check to see if T1 is an ancestor of any of T2's
13399 -- progenitors.
13400
13401 else
13402 Intfc_Elmt := First_Elmt (Interfaces (T2));
13403 while Present (Intfc_Elmt) loop
13404 if Is_Ancestor (T1, Node (Intfc_Elmt)) then
13405 return True;
13406 end if;
13407
13408 Next_Elmt (Intfc_Elmt);
13409 end loop;
13410 end if;
13411
13412 return False;
13413 end Is_Tagged_Ancestor;
13414
13415 -- Start of processing for Check_Abstract_Primitives
13416
13417 begin
13418 -- Loop over all of the formal derived type's primitives
13419
13420 Gen_Elmt := First_Elmt (Gen_Prims);
13421 while Present (Gen_Elmt) loop
13422 Gen_Subp := Node (Gen_Elmt);
13423
13424 -- If the primitive of the formal is not abstract, then
13425 -- determine whether there is a corresponding primitive of
13426 -- the actual type that's abstract.
13427
13428 if not Is_Abstract_Subprogram (Gen_Subp) then
13429 Act_Elmt := First_Elmt (Act_Prims);
13430 while Present (Act_Elmt) loop
13431 Act_Subp := Node (Act_Elmt);
13432
13433 -- If we find an abstract primitive of the actual,
13434 -- then we need to test whether it corresponds to the
13435 -- subprogram from which the generic formal primitive
13436 -- is inherited.
13437
13438 if Is_Abstract_Subprogram (Act_Subp) then
13439 Anc_Subp := Alias (Gen_Subp);
13440
13441 -- Test whether we have a corresponding primitive
13442 -- by comparing names, kinds, formal types, and
13443 -- result types.
13444
13445 if Chars (Anc_Subp) = Chars (Act_Subp)
13446 and then Ekind (Anc_Subp) = Ekind (Act_Subp)
13447 then
13448 Anc_Formal := First_Formal (Anc_Subp);
13449 Act_Formal := First_Formal (Act_Subp);
13450 while Present (Anc_Formal)
13451 and then Present (Act_Formal)
13452 loop
13453 Anc_F_Type := Etype (Anc_Formal);
13454 Act_F_Type := Etype (Act_Formal);
13455
13456 if Ekind (Anc_F_Type) =
13457 E_Anonymous_Access_Type
13458 then
13459 Anc_F_Type := Designated_Type (Anc_F_Type);
13460
13461 if Ekind (Act_F_Type) =
13462 E_Anonymous_Access_Type
13463 then
13464 Act_F_Type :=
13465 Designated_Type (Act_F_Type);
13466 else
13467 exit;
13468 end if;
13469
13470 elsif
13471 Ekind (Act_F_Type) = E_Anonymous_Access_Type
13472 then
13473 exit;
13474 end if;
13475
13476 Anc_F_Type := Base_Type (Anc_F_Type);
13477 Act_F_Type := Base_Type (Act_F_Type);
13478
13479 -- If the formal is controlling, then the
13480 -- the type of the actual primitive's formal
13481 -- must be derived directly or indirectly
13482 -- from the type of the ancestor primitive's
13483 -- formal.
13484
13485 if Is_Controlling_Formal (Anc_Formal) then
13486 if not Is_Tagged_Ancestor
13487 (Anc_F_Type, Act_F_Type)
13488 then
13489 exit;
13490 end if;
13491
13492 -- Otherwise the types of the formals must
13493 -- be the same.
13494
13495 elsif Anc_F_Type /= Act_F_Type then
13496 exit;
13497 end if;
13498
13499 Next_Entity (Anc_Formal);
13500 Next_Entity (Act_Formal);
13501 end loop;
13502
13503 -- If we traversed through all of the formals
13504 -- then so far the subprograms correspond, so
13505 -- now check that any result types correspond.
13506
13507 if No (Anc_Formal) and then No (Act_Formal) then
13508 Subprograms_Correspond := True;
13509
13510 if Ekind (Act_Subp) = E_Function then
13511 Anc_F_Type := Etype (Anc_Subp);
13512 Act_F_Type := Etype (Act_Subp);
13513
13514 if Ekind (Anc_F_Type) =
13515 E_Anonymous_Access_Type
13516 then
13517 Anc_F_Type :=
13518 Designated_Type (Anc_F_Type);
13519
13520 if Ekind (Act_F_Type) =
13521 E_Anonymous_Access_Type
13522 then
13523 Act_F_Type :=
13524 Designated_Type (Act_F_Type);
13525 else
13526 Subprograms_Correspond := False;
13527 end if;
13528
13529 elsif
13530 Ekind (Act_F_Type)
13531 = E_Anonymous_Access_Type
13532 then
13533 Subprograms_Correspond := False;
13534 end if;
13535
13536 Anc_F_Type := Base_Type (Anc_F_Type);
13537 Act_F_Type := Base_Type (Act_F_Type);
13538
13539 -- Now either the result types must be
13540 -- the same or, if the result type is
13541 -- controlling, the result type of the
13542 -- actual primitive must descend from the
13543 -- result type of the ancestor primitive.
13544
13545 if Subprograms_Correspond
13546 and then Anc_F_Type /= Act_F_Type
13547 and then
13548 Has_Controlling_Result (Anc_Subp)
13549 and then not Is_Tagged_Ancestor
13550 (Anc_F_Type, Act_F_Type)
13551 then
13552 Subprograms_Correspond := False;
13553 end if;
13554 end if;
13555
13556 -- Found a matching subprogram belonging to
13557 -- formal ancestor type, so actual subprogram
13558 -- corresponds and this violates 3.9.3(9).
13559
13560 if Subprograms_Correspond then
13561 Error_Msg_NE
13562 ("abstract subprogram & overrides "
13563 & "nonabstract subprogram of ancestor",
13564 Actual, Act_Subp);
13565 end if;
13566 end if;
13567 end if;
13568 end if;
13569
13570 Next_Elmt (Act_Elmt);
13571 end loop;
13572 end if;
13573
13574 Next_Elmt (Gen_Elmt);
13575 end loop;
13576 end Check_Abstract_Primitives;
13577 end if;
13578
13579 -- Verify that limitedness matches. If parent is a limited
13580 -- interface then the generic formal is not unless declared
13581 -- explicitly so. If not declared limited, the actual cannot be
13582 -- limited (see AI05-0087).
13583
13584 if Is_Limited_Type (Act_T) and then not Is_Limited_Type (A_Gen_T) then
13585 if not In_Instance then
13586 Error_Msg_NE
13587 ("actual for non-limited & cannot be a limited type",
13588 Actual, Gen_T);
13589 Explain_Limited_Type (Act_T, Actual);
13590 Abandon_Instantiation (Actual);
13591 end if;
13592 end if;
13593
13594 -- Check for AI12-0036
13595
13596 declare
13597 Formal_Is_Private_Extension : constant Boolean :=
13598 Nkind (Parent (A_Gen_T)) = N_Private_Extension_Declaration;
13599
13600 Actual_Is_Tagged : constant Boolean := Is_Tagged_Type (Act_T);
13601
13602 begin
13603 if Actual_Is_Tagged /= Formal_Is_Private_Extension then
13604 if not In_Instance then
13605 if Actual_Is_Tagged then
13606 Error_Msg_NE
13607 ("actual for & cannot be a tagged type", Actual, Gen_T);
13608 else
13609 Error_Msg_NE
13610 ("actual for & must be a tagged type", Actual, Gen_T);
13611 end if;
13612
13613 Abandon_Instantiation (Actual);
13614 end if;
13615 end if;
13616 end;
13617 end Validate_Derived_Type_Instance;
13618
13619 ----------------------------------------
13620 -- Validate_Discriminated_Formal_Type --
13621 ----------------------------------------
13622
13623 procedure Validate_Discriminated_Formal_Type is
13624 Formal_Discr : Entity_Id;
13625 Actual_Discr : Entity_Id;
13626 Formal_Subt : Entity_Id;
13627
13628 begin
13629 if Has_Discriminants (A_Gen_T) then
13630 if not Has_Discriminants (Act_T) then
13631 Error_Msg_NE
13632 ("actual for & must have discriminants", Actual, Gen_T);
13633 Abandon_Instantiation (Actual);
13634
13635 elsif Is_Constrained (Act_T) then
13636 Error_Msg_NE
13637 ("actual for & must be unconstrained", Actual, Gen_T);
13638 Abandon_Instantiation (Actual);
13639
13640 else
13641 Formal_Discr := First_Discriminant (A_Gen_T);
13642 Actual_Discr := First_Discriminant (Act_T);
13643 while Formal_Discr /= Empty loop
13644 if Actual_Discr = Empty then
13645 Error_Msg_NE
13646 ("discriminants on actual do not match formal",
13647 Actual, Gen_T);
13648 Abandon_Instantiation (Actual);
13649 end if;
13650
13651 Formal_Subt := Get_Instance_Of (Etype (Formal_Discr));
13652
13653 -- Access discriminants match if designated types do
13654
13655 if Ekind (Base_Type (Formal_Subt)) = E_Anonymous_Access_Type
13656 and then (Ekind (Base_Type (Etype (Actual_Discr)))) =
13657 E_Anonymous_Access_Type
13658 and then
13659 Get_Instance_Of
13660 (Designated_Type (Base_Type (Formal_Subt))) =
13661 Designated_Type (Base_Type (Etype (Actual_Discr)))
13662 then
13663 null;
13664
13665 elsif Base_Type (Formal_Subt) /=
13666 Base_Type (Etype (Actual_Discr))
13667 then
13668 Error_Msg_NE
13669 ("types of actual discriminants must match formal",
13670 Actual, Gen_T);
13671 Abandon_Instantiation (Actual);
13672
13673 elsif not Subtypes_Statically_Match
13674 (Formal_Subt, Etype (Actual_Discr))
13675 and then Ada_Version >= Ada_95
13676 then
13677 Error_Msg_NE
13678 ("subtypes of actual discriminants must match formal",
13679 Actual, Gen_T);
13680 Abandon_Instantiation (Actual);
13681 end if;
13682
13683 Next_Discriminant (Formal_Discr);
13684 Next_Discriminant (Actual_Discr);
13685 end loop;
13686
13687 if Actual_Discr /= Empty then
13688 Error_Msg_NE
13689 ("discriminants on actual do not match formal",
13690 Actual, Gen_T);
13691 Abandon_Instantiation (Actual);
13692 end if;
13693 end if;
13694 end if;
13695 end Validate_Discriminated_Formal_Type;
13696
13697 ---------------------------------------
13698 -- Validate_Incomplete_Type_Instance --
13699 ---------------------------------------
13700
13701 procedure Validate_Incomplete_Type_Instance is
13702 begin
13703 if not Is_Tagged_Type (Act_T)
13704 and then Is_Tagged_Type (A_Gen_T)
13705 then
13706 Error_Msg_NE
13707 ("actual for & must be a tagged type", Actual, Gen_T);
13708 end if;
13709
13710 Validate_Discriminated_Formal_Type;
13711 end Validate_Incomplete_Type_Instance;
13712
13713 --------------------------------------
13714 -- Validate_Interface_Type_Instance --
13715 --------------------------------------
13716
13717 procedure Validate_Interface_Type_Instance is
13718 begin
13719 if not Is_Interface (Act_T) then
13720 Error_Msg_NE
13721 ("actual for formal interface type must be an interface",
13722 Actual, Gen_T);
13723
13724 elsif Is_Limited_Type (Act_T) /= Is_Limited_Type (A_Gen_T)
13725 or else Is_Task_Interface (A_Gen_T) /= Is_Task_Interface (Act_T)
13726 or else Is_Protected_Interface (A_Gen_T) /=
13727 Is_Protected_Interface (Act_T)
13728 or else Is_Synchronized_Interface (A_Gen_T) /=
13729 Is_Synchronized_Interface (Act_T)
13730 then
13731 Error_Msg_NE
13732 ("actual for interface& does not match (RM 12.5.5(4))",
13733 Actual, Gen_T);
13734 end if;
13735 end Validate_Interface_Type_Instance;
13736
13737 ------------------------------------
13738 -- Validate_Private_Type_Instance --
13739 ------------------------------------
13740
13741 procedure Validate_Private_Type_Instance is
13742 begin
13743 if Is_Limited_Type (Act_T)
13744 and then not Is_Limited_Type (A_Gen_T)
13745 then
13746 if In_Instance then
13747 null;
13748 else
13749 Error_Msg_NE
13750 ("actual for non-limited & cannot be a limited type", Actual,
13751 Gen_T);
13752 Explain_Limited_Type (Act_T, Actual);
13753 Abandon_Instantiation (Actual);
13754 end if;
13755
13756 elsif Known_To_Have_Preelab_Init (A_Gen_T)
13757 and then not Has_Preelaborable_Initialization (Act_T)
13758 then
13759 Error_Msg_NE
13760 ("actual for & must have preelaborable initialization", Actual,
13761 Gen_T);
13762
13763 elsif not Is_Definite_Subtype (Act_T)
13764 and then Is_Definite_Subtype (A_Gen_T)
13765 and then Ada_Version >= Ada_95
13766 then
13767 Error_Msg_NE
13768 ("actual for & must be a definite subtype", Actual, Gen_T);
13769
13770 elsif not Is_Tagged_Type (Act_T)
13771 and then Is_Tagged_Type (A_Gen_T)
13772 then
13773 Error_Msg_NE
13774 ("actual for & must be a tagged type", Actual, Gen_T);
13775 end if;
13776
13777 Validate_Discriminated_Formal_Type;
13778 Ancestor := Gen_T;
13779 end Validate_Private_Type_Instance;
13780
13781 -- Start of processing for Instantiate_Type
13782
13783 begin
13784 if Get_Instance_Of (A_Gen_T) /= A_Gen_T then
13785 Error_Msg_N ("duplicate instantiation of generic type", Actual);
13786 return New_List (Error);
13787
13788 elsif not Is_Entity_Name (Actual)
13789 or else not Is_Type (Entity (Actual))
13790 then
13791 Error_Msg_NE
13792 ("expect valid subtype mark to instantiate &", Actual, Gen_T);
13793 Abandon_Instantiation (Actual);
13794
13795 else
13796 Act_T := Entity (Actual);
13797
13798 -- Ada 2005 (AI-216): An Unchecked_Union subtype shall only be passed
13799 -- as a generic actual parameter if the corresponding formal type
13800 -- does not have a known_discriminant_part, or is a formal derived
13801 -- type that is an Unchecked_Union type.
13802
13803 if Is_Unchecked_Union (Base_Type (Act_T)) then
13804 if not Has_Discriminants (A_Gen_T)
13805 or else (Is_Derived_Type (A_Gen_T)
13806 and then Is_Unchecked_Union (A_Gen_T))
13807 then
13808 null;
13809 else
13810 Error_Msg_N ("unchecked union cannot be the actual for a "
13811 & "discriminated formal type", Act_T);
13812
13813 end if;
13814 end if;
13815
13816 -- Deal with fixed/floating restrictions
13817
13818 if Is_Floating_Point_Type (Act_T) then
13819 Check_Restriction (No_Floating_Point, Actual);
13820 elsif Is_Fixed_Point_Type (Act_T) then
13821 Check_Restriction (No_Fixed_Point, Actual);
13822 end if;
13823
13824 -- Deal with error of using incomplete type as generic actual.
13825 -- This includes limited views of a type, even if the non-limited
13826 -- view may be available.
13827
13828 if Ekind (Act_T) = E_Incomplete_Type
13829 or else (Is_Class_Wide_Type (Act_T)
13830 and then Ekind (Root_Type (Act_T)) = E_Incomplete_Type)
13831 then
13832 -- If the formal is an incomplete type, the actual can be
13833 -- incomplete as well.
13834
13835 if Ekind (A_Gen_T) = E_Incomplete_Type then
13836 null;
13837
13838 elsif Is_Class_Wide_Type (Act_T)
13839 or else No (Full_View (Act_T))
13840 then
13841 Error_Msg_N ("premature use of incomplete type", Actual);
13842 Abandon_Instantiation (Actual);
13843 else
13844 Act_T := Full_View (Act_T);
13845 Set_Entity (Actual, Act_T);
13846
13847 if Has_Private_Component (Act_T) then
13848 Error_Msg_N
13849 ("premature use of type with private component", Actual);
13850 end if;
13851 end if;
13852
13853 -- Deal with error of premature use of private type as generic actual
13854
13855 elsif Is_Private_Type (Act_T)
13856 and then Is_Private_Type (Base_Type (Act_T))
13857 and then not Is_Generic_Type (Act_T)
13858 and then not Is_Derived_Type (Act_T)
13859 and then No (Full_View (Root_Type (Act_T)))
13860 then
13861 -- If the formal is an incomplete type, the actual can be
13862 -- private or incomplete as well.
13863
13864 if Ekind (A_Gen_T) = E_Incomplete_Type then
13865 null;
13866 else
13867 Error_Msg_N ("premature use of private type", Actual);
13868 end if;
13869
13870 elsif Has_Private_Component (Act_T) then
13871 Error_Msg_N
13872 ("premature use of type with private component", Actual);
13873 end if;
13874
13875 Set_Instance_Of (A_Gen_T, Act_T);
13876
13877 -- If the type is generic, the class-wide type may also be used
13878
13879 if Is_Tagged_Type (A_Gen_T)
13880 and then Is_Tagged_Type (Act_T)
13881 and then not Is_Class_Wide_Type (A_Gen_T)
13882 then
13883 Set_Instance_Of (Class_Wide_Type (A_Gen_T),
13884 Class_Wide_Type (Act_T));
13885 end if;
13886
13887 if not Is_Abstract_Type (A_Gen_T)
13888 and then Is_Abstract_Type (Act_T)
13889 then
13890 Error_Msg_N
13891 ("actual of non-abstract formal cannot be abstract", Actual);
13892 end if;
13893
13894 -- A generic scalar type is a first subtype for which we generate
13895 -- an anonymous base type. Indicate that the instance of this base
13896 -- is the base type of the actual.
13897
13898 if Is_Scalar_Type (A_Gen_T) then
13899 Set_Instance_Of (Etype (A_Gen_T), Etype (Act_T));
13900 end if;
13901 end if;
13902
13903 Check_Shared_Variable_Control_Aspects;
13904
13905 if Error_Posted (Act_T) then
13906 null;
13907 else
13908 case Nkind (Def) is
13909 when N_Formal_Private_Type_Definition =>
13910 Validate_Private_Type_Instance;
13911
13912 when N_Formal_Incomplete_Type_Definition =>
13913 Validate_Incomplete_Type_Instance;
13914
13915 when N_Formal_Derived_Type_Definition =>
13916 Validate_Derived_Type_Instance;
13917
13918 when N_Formal_Discrete_Type_Definition =>
13919 if not Is_Discrete_Type (Act_T) then
13920 Error_Msg_NE
13921 ("expect discrete type in instantiation of&",
13922 Actual, Gen_T);
13923 Abandon_Instantiation (Actual);
13924 end if;
13925
13926 Diagnose_Predicated_Actual;
13927
13928 when N_Formal_Signed_Integer_Type_Definition =>
13929 if not Is_Signed_Integer_Type (Act_T) then
13930 Error_Msg_NE
13931 ("expect signed integer type in instantiation of&",
13932 Actual, Gen_T);
13933 Abandon_Instantiation (Actual);
13934 end if;
13935
13936 Diagnose_Predicated_Actual;
13937
13938 when N_Formal_Modular_Type_Definition =>
13939 if not Is_Modular_Integer_Type (Act_T) then
13940 Error_Msg_NE
13941 ("expect modular type in instantiation of &",
13942 Actual, Gen_T);
13943 Abandon_Instantiation (Actual);
13944 end if;
13945
13946 Diagnose_Predicated_Actual;
13947
13948 when N_Formal_Floating_Point_Definition =>
13949 if not Is_Floating_Point_Type (Act_T) then
13950 Error_Msg_NE
13951 ("expect float type in instantiation of &", Actual, Gen_T);
13952 Abandon_Instantiation (Actual);
13953 end if;
13954
13955 when N_Formal_Ordinary_Fixed_Point_Definition =>
13956 if not Is_Ordinary_Fixed_Point_Type (Act_T) then
13957 Error_Msg_NE
13958 ("expect ordinary fixed point type in instantiation of &",
13959 Actual, Gen_T);
13960 Abandon_Instantiation (Actual);
13961 end if;
13962
13963 when N_Formal_Decimal_Fixed_Point_Definition =>
13964 if not Is_Decimal_Fixed_Point_Type (Act_T) then
13965 Error_Msg_NE
13966 ("expect decimal type in instantiation of &",
13967 Actual, Gen_T);
13968 Abandon_Instantiation (Actual);
13969 end if;
13970
13971 when N_Array_Type_Definition =>
13972 Validate_Array_Type_Instance;
13973
13974 when N_Access_To_Object_Definition =>
13975 Validate_Access_Type_Instance;
13976
13977 when N_Access_Function_Definition
13978 | N_Access_Procedure_Definition
13979 =>
13980 Validate_Access_Subprogram_Instance;
13981
13982 when N_Record_Definition =>
13983 Validate_Interface_Type_Instance;
13984
13985 when N_Derived_Type_Definition =>
13986 Validate_Derived_Interface_Type_Instance;
13987
13988 when others =>
13989 raise Program_Error;
13990 end case;
13991 end if;
13992
13993 Subt := New_Copy (Gen_T);
13994
13995 -- Use adjusted sloc of subtype name as the location for other nodes in
13996 -- the subtype declaration.
13997
13998 Loc := Sloc (Subt);
13999
14000 Decl_Node :=
14001 Make_Subtype_Declaration (Loc,
14002 Defining_Identifier => Subt,
14003 Subtype_Indication => New_Occurrence_Of (Act_T, Loc));
14004
14005 -- Record whether the actual is private at this point, so that
14006 -- Check_Generic_Actuals can restore its proper view before the
14007 -- semantic analysis of the instance.
14008
14009 if Is_Private_Type (Act_T) then
14010 Set_Has_Private_View (Subtype_Indication (Decl_Node));
14011 end if;
14012
14013 -- In Ada 2012 the actual may be a limited view. Indicate that
14014 -- the local subtype must be treated as such.
14015
14016 if From_Limited_With (Act_T) then
14017 Set_Ekind (Subt, E_Incomplete_Subtype);
14018 Set_From_Limited_With (Subt);
14019 end if;
14020
14021 Decl_Nodes := New_List (Decl_Node);
14022
14023 -- Flag actual derived types so their elaboration produces the
14024 -- appropriate renamings for the primitive operations of the ancestor.
14025 -- Flag actual for formal private types as well, to determine whether
14026 -- operations in the private part may override inherited operations.
14027 -- If the formal has an interface list, the ancestor is not the
14028 -- parent, but the analyzed formal that includes the interface
14029 -- operations of all its progenitors.
14030
14031 -- Same treatment for formal private types, so we can check whether the
14032 -- type is tagged limited when validating derivations in the private
14033 -- part. (See AI05-096).
14034
14035 if Nkind (Def) = N_Formal_Derived_Type_Definition then
14036 if Present (Interface_List (Def)) then
14037 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
14038 else
14039 Set_Generic_Parent_Type (Decl_Node, Ancestor);
14040 end if;
14041
14042 elsif Nkind_In (Def, N_Formal_Private_Type_Definition,
14043 N_Formal_Incomplete_Type_Definition)
14044 then
14045 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
14046 end if;
14047
14048 -- If the actual is a synchronized type that implements an interface,
14049 -- the primitive operations are attached to the corresponding record,
14050 -- and we have to treat it as an additional generic actual, so that its
14051 -- primitive operations become visible in the instance. The task or
14052 -- protected type itself does not carry primitive operations.
14053
14054 if Is_Concurrent_Type (Act_T)
14055 and then Is_Tagged_Type (Act_T)
14056 and then Present (Corresponding_Record_Type (Act_T))
14057 and then Present (Ancestor)
14058 and then Is_Interface (Ancestor)
14059 then
14060 declare
14061 Corr_Rec : constant Entity_Id :=
14062 Corresponding_Record_Type (Act_T);
14063 New_Corr : Entity_Id;
14064 Corr_Decl : Node_Id;
14065
14066 begin
14067 New_Corr := Make_Temporary (Loc, 'S');
14068 Corr_Decl :=
14069 Make_Subtype_Declaration (Loc,
14070 Defining_Identifier => New_Corr,
14071 Subtype_Indication =>
14072 New_Occurrence_Of (Corr_Rec, Loc));
14073 Append_To (Decl_Nodes, Corr_Decl);
14074
14075 if Ekind (Act_T) = E_Task_Type then
14076 Set_Ekind (Subt, E_Task_Subtype);
14077 else
14078 Set_Ekind (Subt, E_Protected_Subtype);
14079 end if;
14080
14081 Set_Corresponding_Record_Type (Subt, Corr_Rec);
14082 Set_Generic_Parent_Type (Corr_Decl, Ancestor);
14083 Set_Generic_Parent_Type (Decl_Node, Empty);
14084 end;
14085 end if;
14086
14087 -- For a floating-point type, capture dimension info if any, because
14088 -- the generated subtype declaration does not come from source and
14089 -- will not process dimensions.
14090
14091 if Is_Floating_Point_Type (Act_T) then
14092 Copy_Dimensions (Act_T, Subt);
14093 end if;
14094
14095 return Decl_Nodes;
14096 end Instantiate_Type;
14097
14098 ---------------------
14099 -- Is_In_Main_Unit --
14100 ---------------------
14101
14102 function Is_In_Main_Unit (N : Node_Id) return Boolean is
14103 Unum : constant Unit_Number_Type := Get_Source_Unit (N);
14104 Current_Unit : Node_Id;
14105
14106 begin
14107 if Unum = Main_Unit then
14108 return True;
14109
14110 -- If the current unit is a subunit then it is either the main unit or
14111 -- is being compiled as part of the main unit.
14112
14113 elsif Nkind (N) = N_Compilation_Unit then
14114 return Nkind (Unit (N)) = N_Subunit;
14115 end if;
14116
14117 Current_Unit := Parent (N);
14118 while Present (Current_Unit)
14119 and then Nkind (Current_Unit) /= N_Compilation_Unit
14120 loop
14121 Current_Unit := Parent (Current_Unit);
14122 end loop;
14123
14124 -- The instantiation node is in the main unit, or else the current node
14125 -- (perhaps as the result of nested instantiations) is in the main unit,
14126 -- or in the declaration of the main unit, which in this last case must
14127 -- be a body.
14128
14129 return
14130 Current_Unit = Cunit (Main_Unit)
14131 or else Current_Unit = Library_Unit (Cunit (Main_Unit))
14132 or else (Present (Current_Unit)
14133 and then Present (Library_Unit (Current_Unit))
14134 and then Is_In_Main_Unit (Library_Unit (Current_Unit)));
14135 end Is_In_Main_Unit;
14136
14137 ----------------------------
14138 -- Load_Parent_Of_Generic --
14139 ----------------------------
14140
14141 procedure Load_Parent_Of_Generic
14142 (N : Node_Id;
14143 Spec : Node_Id;
14144 Body_Optional : Boolean := False)
14145 is
14146 Comp_Unit : constant Node_Id := Cunit (Get_Source_Unit (Spec));
14147 Saved_Style_Check : constant Boolean := Style_Check;
14148 Saved_Warnings : constant Warning_Record := Save_Warnings;
14149 True_Parent : Node_Id;
14150 Inst_Node : Node_Id;
14151 OK : Boolean;
14152 Previous_Instances : constant Elist_Id := New_Elmt_List;
14153
14154 procedure Collect_Previous_Instances (Decls : List_Id);
14155 -- Collect all instantiations in the given list of declarations, that
14156 -- precede the generic that we need to load. If the bodies of these
14157 -- instantiations are available, we must analyze them, to ensure that
14158 -- the public symbols generated are the same when the unit is compiled
14159 -- to generate code, and when it is compiled in the context of a unit
14160 -- that needs a particular nested instance. This process is applied to
14161 -- both package and subprogram instances.
14162
14163 --------------------------------
14164 -- Collect_Previous_Instances --
14165 --------------------------------
14166
14167 procedure Collect_Previous_Instances (Decls : List_Id) is
14168 Decl : Node_Id;
14169
14170 begin
14171 Decl := First (Decls);
14172 while Present (Decl) loop
14173 if Sloc (Decl) >= Sloc (Inst_Node) then
14174 return;
14175
14176 -- If Decl is an instantiation, then record it as requiring
14177 -- instantiation of the corresponding body, except if it is an
14178 -- abbreviated instantiation generated internally for conformance
14179 -- checking purposes only for the case of a formal package
14180 -- declared without a box (see Instantiate_Formal_Package). Such
14181 -- an instantiation does not generate any code (the actual code
14182 -- comes from actual) and thus does not need to be analyzed here.
14183 -- If the instantiation appears with a generic package body it is
14184 -- not analyzed here either.
14185
14186 elsif Nkind (Decl) = N_Package_Instantiation
14187 and then not Is_Internal (Defining_Entity (Decl))
14188 then
14189 Append_Elmt (Decl, Previous_Instances);
14190
14191 -- For a subprogram instantiation, omit instantiations intrinsic
14192 -- operations (Unchecked_Conversions, etc.) that have no bodies.
14193
14194 elsif Nkind_In (Decl, N_Function_Instantiation,
14195 N_Procedure_Instantiation)
14196 and then not Is_Intrinsic_Subprogram (Entity (Name (Decl)))
14197 then
14198 Append_Elmt (Decl, Previous_Instances);
14199
14200 elsif Nkind (Decl) = N_Package_Declaration then
14201 Collect_Previous_Instances
14202 (Visible_Declarations (Specification (Decl)));
14203 Collect_Previous_Instances
14204 (Private_Declarations (Specification (Decl)));
14205
14206 -- Previous non-generic bodies may contain instances as well
14207
14208 elsif Nkind (Decl) = N_Package_Body
14209 and then Ekind (Corresponding_Spec (Decl)) /= E_Generic_Package
14210 then
14211 Collect_Previous_Instances (Declarations (Decl));
14212
14213 elsif Nkind (Decl) = N_Subprogram_Body
14214 and then not Acts_As_Spec (Decl)
14215 and then not Is_Generic_Subprogram (Corresponding_Spec (Decl))
14216 then
14217 Collect_Previous_Instances (Declarations (Decl));
14218 end if;
14219
14220 Next (Decl);
14221 end loop;
14222 end Collect_Previous_Instances;
14223
14224 -- Start of processing for Load_Parent_Of_Generic
14225
14226 begin
14227 if not In_Same_Source_Unit (N, Spec)
14228 or else Nkind (Unit (Comp_Unit)) = N_Package_Declaration
14229 or else (Nkind (Unit (Comp_Unit)) = N_Package_Body
14230 and then not Is_In_Main_Unit (Spec))
14231 then
14232 -- Find body of parent of spec, and analyze it. A special case arises
14233 -- when the parent is an instantiation, that is to say when we are
14234 -- currently instantiating a nested generic. In that case, there is
14235 -- no separate file for the body of the enclosing instance. Instead,
14236 -- the enclosing body must be instantiated as if it were a pending
14237 -- instantiation, in order to produce the body for the nested generic
14238 -- we require now. Note that in that case the generic may be defined
14239 -- in a package body, the instance defined in the same package body,
14240 -- and the original enclosing body may not be in the main unit.
14241
14242 Inst_Node := Empty;
14243
14244 True_Parent := Parent (Spec);
14245 while Present (True_Parent)
14246 and then Nkind (True_Parent) /= N_Compilation_Unit
14247 loop
14248 if Nkind (True_Parent) = N_Package_Declaration
14249 and then
14250 Nkind (Original_Node (True_Parent)) = N_Package_Instantiation
14251 then
14252 -- Parent is a compilation unit that is an instantiation, and
14253 -- instantiation node has been replaced with package decl.
14254
14255 Inst_Node := Original_Node (True_Parent);
14256 exit;
14257
14258 elsif Nkind (True_Parent) = N_Package_Declaration
14259 and then Nkind (Parent (True_Parent)) = N_Compilation_Unit
14260 and then
14261 Nkind (Unit (Parent (True_Parent))) = N_Package_Instantiation
14262 then
14263 -- Parent is a compilation unit that is an instantiation, but
14264 -- instantiation node has not been replaced with package decl.
14265
14266 Inst_Node := Unit (Parent (True_Parent));
14267 exit;
14268
14269 elsif Nkind (True_Parent) = N_Package_Declaration
14270 and then Nkind (Parent (True_Parent)) /= N_Compilation_Unit
14271 and then Present (Generic_Parent (Specification (True_Parent)))
14272 then
14273 -- Parent is an instantiation within another specification.
14274 -- Declaration for instance has been inserted before original
14275 -- instantiation node. A direct link would be preferable?
14276
14277 Inst_Node := Next (True_Parent);
14278 while Present (Inst_Node)
14279 and then Nkind (Inst_Node) /= N_Package_Instantiation
14280 loop
14281 Next (Inst_Node);
14282 end loop;
14283
14284 -- If the instance appears within a generic, and the generic
14285 -- unit is defined within a formal package of the enclosing
14286 -- generic, there is no generic body available, and none
14287 -- needed. A more precise test should be used ???
14288
14289 if No (Inst_Node) then
14290 return;
14291 end if;
14292
14293 exit;
14294
14295 else
14296 True_Parent := Parent (True_Parent);
14297 end if;
14298 end loop;
14299
14300 -- Case where we are currently instantiating a nested generic
14301
14302 if Present (Inst_Node) then
14303 if Nkind (Parent (True_Parent)) = N_Compilation_Unit then
14304
14305 -- Instantiation node and declaration of instantiated package
14306 -- were exchanged when only the declaration was needed.
14307 -- Restore instantiation node before proceeding with body.
14308
14309 Set_Unit (Parent (True_Parent), Inst_Node);
14310 end if;
14311
14312 -- Now complete instantiation of enclosing body, if it appears in
14313 -- some other unit. If it appears in the current unit, the body
14314 -- will have been instantiated already.
14315
14316 if No (Corresponding_Body (Instance_Spec (Inst_Node))) then
14317
14318 -- We need to determine the expander mode to instantiate the
14319 -- enclosing body. Because the generic body we need may use
14320 -- global entities declared in the enclosing package (including
14321 -- aggregates) it is in general necessary to compile this body
14322 -- with expansion enabled, except if we are within a generic
14323 -- package, in which case the usual generic rule applies.
14324
14325 declare
14326 Exp_Status : Boolean := True;
14327 Scop : Entity_Id;
14328
14329 begin
14330 -- Loop through scopes looking for generic package
14331
14332 Scop := Scope (Defining_Entity (Instance_Spec (Inst_Node)));
14333 while Present (Scop)
14334 and then Scop /= Standard_Standard
14335 loop
14336 if Ekind (Scop) = E_Generic_Package then
14337 Exp_Status := False;
14338 exit;
14339 end if;
14340
14341 Scop := Scope (Scop);
14342 end loop;
14343
14344 -- Collect previous instantiations in the unit that contains
14345 -- the desired generic.
14346
14347 if Nkind (Parent (True_Parent)) /= N_Compilation_Unit
14348 and then not Body_Optional
14349 then
14350 declare
14351 Decl : Elmt_Id;
14352 Info : Pending_Body_Info;
14353 Par : Node_Id;
14354
14355 begin
14356 Par := Parent (Inst_Node);
14357 while Present (Par) loop
14358 exit when Nkind (Parent (Par)) = N_Compilation_Unit;
14359 Par := Parent (Par);
14360 end loop;
14361
14362 pragma Assert (Present (Par));
14363
14364 if Nkind (Par) = N_Package_Body then
14365 Collect_Previous_Instances (Declarations (Par));
14366
14367 elsif Nkind (Par) = N_Package_Declaration then
14368 Collect_Previous_Instances
14369 (Visible_Declarations (Specification (Par)));
14370 Collect_Previous_Instances
14371 (Private_Declarations (Specification (Par)));
14372
14373 else
14374 -- Enclosing unit is a subprogram body. In this
14375 -- case all instance bodies are processed in order
14376 -- and there is no need to collect them separately.
14377
14378 null;
14379 end if;
14380
14381 Decl := First_Elmt (Previous_Instances);
14382 while Present (Decl) loop
14383 Info :=
14384 (Act_Decl =>
14385 Instance_Spec (Node (Decl)),
14386 Config_Switches => Save_Config_Switches,
14387 Current_Sem_Unit =>
14388 Get_Code_Unit (Sloc (Node (Decl))),
14389 Expander_Status => Exp_Status,
14390 Inst_Node => Node (Decl),
14391 Local_Suppress_Stack_Top =>
14392 Local_Suppress_Stack_Top,
14393 Scope_Suppress => Scope_Suppress,
14394 Warnings => Save_Warnings);
14395
14396 -- Package instance
14397
14398 if Nkind (Node (Decl)) = N_Package_Instantiation
14399 then
14400 Instantiate_Package_Body
14401 (Info, Body_Optional => True);
14402
14403 -- Subprogram instance
14404
14405 else
14406 -- The instance_spec is in the wrapper package,
14407 -- usually followed by its local renaming
14408 -- declaration. See Build_Subprogram_Renaming
14409 -- for details. If the instance carries aspects,
14410 -- these result in the corresponding pragmas,
14411 -- inserted after the subprogram declaration.
14412 -- They must be skipped as well when retrieving
14413 -- the desired spec. Some of them may have been
14414 -- rewritten as null statements.
14415 -- A direct link would be more robust ???
14416
14417 declare
14418 Decl : Node_Id :=
14419 (Last (Visible_Declarations
14420 (Specification (Info.Act_Decl))));
14421 begin
14422 while Nkind_In (Decl,
14423 N_Null_Statement,
14424 N_Pragma,
14425 N_Subprogram_Renaming_Declaration)
14426 loop
14427 Decl := Prev (Decl);
14428 end loop;
14429
14430 Info.Act_Decl := Decl;
14431 end;
14432
14433 Instantiate_Subprogram_Body
14434 (Info, Body_Optional => True);
14435 end if;
14436
14437 Next_Elmt (Decl);
14438 end loop;
14439 end;
14440 end if;
14441
14442 Instantiate_Package_Body
14443 (Body_Info =>
14444 ((Act_Decl => True_Parent,
14445 Config_Switches => Save_Config_Switches,
14446 Current_Sem_Unit =>
14447 Get_Code_Unit (Sloc (Inst_Node)),
14448 Expander_Status => Exp_Status,
14449 Inst_Node => Inst_Node,
14450 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
14451 Scope_Suppress => Scope_Suppress,
14452 Warnings => Save_Warnings)),
14453 Body_Optional => Body_Optional);
14454 end;
14455 end if;
14456
14457 -- Case where we are not instantiating a nested generic
14458
14459 else
14460 Opt.Style_Check := False;
14461 Expander_Mode_Save_And_Set (True);
14462 Load_Needed_Body (Comp_Unit, OK);
14463 Opt.Style_Check := Saved_Style_Check;
14464 Restore_Warnings (Saved_Warnings);
14465 Expander_Mode_Restore;
14466
14467 if not OK
14468 and then Unit_Requires_Body (Defining_Entity (Spec))
14469 and then not Body_Optional
14470 then
14471 declare
14472 Bname : constant Unit_Name_Type :=
14473 Get_Body_Name (Get_Unit_Name (Unit (Comp_Unit)));
14474
14475 begin
14476 -- In CodePeer mode, the missing body may make the analysis
14477 -- incomplete, but we do not treat it as fatal.
14478
14479 if CodePeer_Mode then
14480 return;
14481
14482 else
14483 Error_Msg_Unit_1 := Bname;
14484 Error_Msg_N ("this instantiation requires$!", N);
14485 Error_Msg_File_1 :=
14486 Get_File_Name (Bname, Subunit => False);
14487 Error_Msg_N ("\but file{ was not found!", N);
14488 raise Unrecoverable_Error;
14489 end if;
14490 end;
14491 end if;
14492 end if;
14493 end if;
14494
14495 -- If loading parent of the generic caused an instantiation circularity,
14496 -- we abandon compilation at this point, because otherwise in some cases
14497 -- we get into trouble with infinite recursions after this point.
14498
14499 if Circularity_Detected then
14500 raise Unrecoverable_Error;
14501 end if;
14502 end Load_Parent_Of_Generic;
14503
14504 ---------------------------------
14505 -- Map_Formal_Package_Entities --
14506 ---------------------------------
14507
14508 procedure Map_Formal_Package_Entities (Form : Entity_Id; Act : Entity_Id) is
14509 E1 : Entity_Id;
14510 E2 : Entity_Id;
14511
14512 begin
14513 Set_Instance_Of (Form, Act);
14514
14515 -- Traverse formal and actual package to map the corresponding entities.
14516 -- We skip over internal entities that may be generated during semantic
14517 -- analysis, and find the matching entities by name, given that they
14518 -- must appear in the same order.
14519
14520 E1 := First_Entity (Form);
14521 E2 := First_Entity (Act);
14522 while Present (E1) and then E1 /= First_Private_Entity (Form) loop
14523 -- Could this test be a single condition??? Seems like it could, and
14524 -- isn't FPE (Form) a constant anyway???
14525
14526 if not Is_Internal (E1)
14527 and then Present (Parent (E1))
14528 and then not Is_Class_Wide_Type (E1)
14529 and then not Is_Internal_Name (Chars (E1))
14530 then
14531 while Present (E2) and then Chars (E2) /= Chars (E1) loop
14532 Next_Entity (E2);
14533 end loop;
14534
14535 if No (E2) then
14536 exit;
14537 else
14538 Set_Instance_Of (E1, E2);
14539
14540 if Is_Type (E1) and then Is_Tagged_Type (E2) then
14541 Set_Instance_Of (Class_Wide_Type (E1), Class_Wide_Type (E2));
14542 end if;
14543
14544 if Is_Constrained (E1) then
14545 Set_Instance_Of (Base_Type (E1), Base_Type (E2));
14546 end if;
14547
14548 if Ekind (E1) = E_Package and then No (Renamed_Object (E1)) then
14549 Map_Formal_Package_Entities (E1, E2);
14550 end if;
14551 end if;
14552 end if;
14553
14554 Next_Entity (E1);
14555 end loop;
14556 end Map_Formal_Package_Entities;
14557
14558 -----------------------
14559 -- Move_Freeze_Nodes --
14560 -----------------------
14561
14562 procedure Move_Freeze_Nodes
14563 (Out_Of : Entity_Id;
14564 After : Node_Id;
14565 L : List_Id)
14566 is
14567 Decl : Node_Id;
14568 Next_Decl : Node_Id;
14569 Next_Node : Node_Id := After;
14570 Spec : Node_Id;
14571
14572 function Is_Outer_Type (T : Entity_Id) return Boolean;
14573 -- Check whether entity is declared in a scope external to that of the
14574 -- generic unit.
14575
14576 -------------------
14577 -- Is_Outer_Type --
14578 -------------------
14579
14580 function Is_Outer_Type (T : Entity_Id) return Boolean is
14581 Scop : Entity_Id := Scope (T);
14582
14583 begin
14584 if Scope_Depth (Scop) < Scope_Depth (Out_Of) then
14585 return True;
14586
14587 else
14588 while Scop /= Standard_Standard loop
14589 if Scop = Out_Of then
14590 return False;
14591 else
14592 Scop := Scope (Scop);
14593 end if;
14594 end loop;
14595
14596 return True;
14597 end if;
14598 end Is_Outer_Type;
14599
14600 -- Start of processing for Move_Freeze_Nodes
14601
14602 begin
14603 if No (L) then
14604 return;
14605 end if;
14606
14607 -- First remove the freeze nodes that may appear before all other
14608 -- declarations.
14609
14610 Decl := First (L);
14611 while Present (Decl)
14612 and then Nkind (Decl) = N_Freeze_Entity
14613 and then Is_Outer_Type (Entity (Decl))
14614 loop
14615 Decl := Remove_Head (L);
14616 Insert_After (Next_Node, Decl);
14617 Set_Analyzed (Decl, False);
14618 Next_Node := Decl;
14619 Decl := First (L);
14620 end loop;
14621
14622 -- Next scan the list of declarations and remove each freeze node that
14623 -- appears ahead of the current node.
14624
14625 while Present (Decl) loop
14626 while Present (Next (Decl))
14627 and then Nkind (Next (Decl)) = N_Freeze_Entity
14628 and then Is_Outer_Type (Entity (Next (Decl)))
14629 loop
14630 Next_Decl := Remove_Next (Decl);
14631 Insert_After (Next_Node, Next_Decl);
14632 Set_Analyzed (Next_Decl, False);
14633 Next_Node := Next_Decl;
14634 end loop;
14635
14636 -- If the declaration is a nested package or concurrent type, then
14637 -- recurse. Nested generic packages will have been processed from the
14638 -- inside out.
14639
14640 case Nkind (Decl) is
14641 when N_Package_Declaration =>
14642 Spec := Specification (Decl);
14643
14644 when N_Task_Type_Declaration =>
14645 Spec := Task_Definition (Decl);
14646
14647 when N_Protected_Type_Declaration =>
14648 Spec := Protected_Definition (Decl);
14649
14650 when others =>
14651 Spec := Empty;
14652 end case;
14653
14654 if Present (Spec) then
14655 Move_Freeze_Nodes (Out_Of, Next_Node, Visible_Declarations (Spec));
14656 Move_Freeze_Nodes (Out_Of, Next_Node, Private_Declarations (Spec));
14657 end if;
14658
14659 Next (Decl);
14660 end loop;
14661 end Move_Freeze_Nodes;
14662
14663 ----------------
14664 -- Next_Assoc --
14665 ----------------
14666
14667 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr is
14668 begin
14669 return Generic_Renamings.Table (E).Next_In_HTable;
14670 end Next_Assoc;
14671
14672 ------------------------
14673 -- Preanalyze_Actuals --
14674 ------------------------
14675
14676 procedure Preanalyze_Actuals (N : Node_Id; Inst : Entity_Id := Empty) is
14677 procedure Perform_Appropriate_Analysis (N : Node_Id);
14678 -- Determine if the actuals we are analyzing come from a generic
14679 -- instantiation that is a library unit and dispatch accordingly.
14680
14681 ----------------------------------
14682 -- Perform_Appropriate_Analysis --
14683 ----------------------------------
14684
14685 procedure Perform_Appropriate_Analysis (N : Node_Id) is
14686 begin
14687 -- When we have a library instantiation we cannot allow any expansion
14688 -- to occur, since there may be no place to put it. Instead, in that
14689 -- case we perform a preanalysis of the actual.
14690
14691 if Present (Inst) and then Is_Compilation_Unit (Inst) then
14692 Preanalyze (N);
14693 else
14694 Analyze (N);
14695 end if;
14696 end Perform_Appropriate_Analysis;
14697
14698 -- Local variables
14699
14700 Errs : constant Nat := Serious_Errors_Detected;
14701
14702 Assoc : Node_Id;
14703 Act : Node_Id;
14704
14705 Cur : Entity_Id := Empty;
14706 -- Current homograph of the instance name
14707
14708 Vis : Boolean := False;
14709 -- Saved visibility status of the current homograph
14710
14711 -- Start of processing for Preanalyze_Actuals
14712
14713 begin
14714 Assoc := First (Generic_Associations (N));
14715
14716 -- If the instance is a child unit, its name may hide an outer homonym,
14717 -- so make it invisible to perform name resolution on the actuals.
14718
14719 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name
14720 and then Present
14721 (Current_Entity (Defining_Identifier (Defining_Unit_Name (N))))
14722 then
14723 Cur := Current_Entity (Defining_Identifier (Defining_Unit_Name (N)));
14724
14725 if Is_Compilation_Unit (Cur) then
14726 Vis := Is_Immediately_Visible (Cur);
14727 Set_Is_Immediately_Visible (Cur, False);
14728 else
14729 Cur := Empty;
14730 end if;
14731 end if;
14732
14733 while Present (Assoc) loop
14734 if Nkind (Assoc) /= N_Others_Choice then
14735 Act := Explicit_Generic_Actual_Parameter (Assoc);
14736
14737 -- Within a nested instantiation, a defaulted actual is an empty
14738 -- association, so nothing to analyze. If the subprogram actual
14739 -- is an attribute, analyze prefix only, because actual is not a
14740 -- complete attribute reference.
14741
14742 -- If actual is an allocator, analyze expression only. The full
14743 -- analysis can generate code, and if instance is a compilation
14744 -- unit we have to wait until the package instance is installed
14745 -- to have a proper place to insert this code.
14746
14747 -- String literals may be operators, but at this point we do not
14748 -- know whether the actual is a formal subprogram or a string.
14749
14750 if No (Act) then
14751 null;
14752
14753 elsif Nkind (Act) = N_Attribute_Reference then
14754 Perform_Appropriate_Analysis (Prefix (Act));
14755
14756 elsif Nkind (Act) = N_Explicit_Dereference then
14757 Perform_Appropriate_Analysis (Prefix (Act));
14758
14759 elsif Nkind (Act) = N_Allocator then
14760 declare
14761 Expr : constant Node_Id := Expression (Act);
14762
14763 begin
14764 if Nkind (Expr) = N_Subtype_Indication then
14765 Perform_Appropriate_Analysis (Subtype_Mark (Expr));
14766
14767 -- Analyze separately each discriminant constraint, when
14768 -- given with a named association.
14769
14770 declare
14771 Constr : Node_Id;
14772
14773 begin
14774 Constr := First (Constraints (Constraint (Expr)));
14775 while Present (Constr) loop
14776 if Nkind (Constr) = N_Discriminant_Association then
14777 Perform_Appropriate_Analysis
14778 (Expression (Constr));
14779 else
14780 Perform_Appropriate_Analysis (Constr);
14781 end if;
14782
14783 Next (Constr);
14784 end loop;
14785 end;
14786
14787 else
14788 Perform_Appropriate_Analysis (Expr);
14789 end if;
14790 end;
14791
14792 elsif Nkind (Act) /= N_Operator_Symbol then
14793 Perform_Appropriate_Analysis (Act);
14794
14795 -- Within a package instance, mark actuals that are limited
14796 -- views, so their use can be moved to the body of the
14797 -- enclosing unit.
14798
14799 if Is_Entity_Name (Act)
14800 and then Is_Type (Entity (Act))
14801 and then From_Limited_With (Entity (Act))
14802 and then Present (Inst)
14803 then
14804 Append_Elmt (Entity (Act), Incomplete_Actuals (Inst));
14805 end if;
14806 end if;
14807
14808 if Errs /= Serious_Errors_Detected then
14809
14810 -- Do a minimal analysis of the generic, to prevent spurious
14811 -- warnings complaining about the generic being unreferenced,
14812 -- before abandoning the instantiation.
14813
14814 Perform_Appropriate_Analysis (Name (N));
14815
14816 if Is_Entity_Name (Name (N))
14817 and then Etype (Name (N)) /= Any_Type
14818 then
14819 Generate_Reference (Entity (Name (N)), Name (N));
14820 Set_Is_Instantiated (Entity (Name (N)));
14821 end if;
14822
14823 if Present (Cur) then
14824
14825 -- For the case of a child instance hiding an outer homonym,
14826 -- provide additional warning which might explain the error.
14827
14828 Set_Is_Immediately_Visible (Cur, Vis);
14829 Error_Msg_NE
14830 ("& hides outer unit with the same name??",
14831 N, Defining_Unit_Name (N));
14832 end if;
14833
14834 Abandon_Instantiation (Act);
14835 end if;
14836 end if;
14837
14838 Next (Assoc);
14839 end loop;
14840
14841 if Present (Cur) then
14842 Set_Is_Immediately_Visible (Cur, Vis);
14843 end if;
14844 end Preanalyze_Actuals;
14845
14846 -------------------------------
14847 -- Provide_Completing_Bodies --
14848 -------------------------------
14849
14850 procedure Provide_Completing_Bodies (N : Node_Id) is
14851 procedure Build_Completing_Body (Subp_Decl : Node_Id);
14852 -- Generate the completing body for subprogram declaration Subp_Decl
14853
14854 procedure Provide_Completing_Bodies_In (Decls : List_Id);
14855 -- Generating completing bodies for all subprograms found in declarative
14856 -- list Decls.
14857
14858 ---------------------------
14859 -- Build_Completing_Body --
14860 ---------------------------
14861
14862 procedure Build_Completing_Body (Subp_Decl : Node_Id) is
14863 Loc : constant Source_Ptr := Sloc (Subp_Decl);
14864 Subp_Id : constant Entity_Id := Defining_Entity (Subp_Decl);
14865 Spec : Node_Id;
14866
14867 begin
14868 -- Nothing to do if the subprogram already has a completing body
14869
14870 if Present (Corresponding_Body (Subp_Decl)) then
14871 return;
14872
14873 -- Mark the function as having a valid return statement even though
14874 -- the body contains a single raise statement.
14875
14876 elsif Ekind (Subp_Id) = E_Function then
14877 Set_Return_Present (Subp_Id);
14878 end if;
14879
14880 -- Clone the specification to obtain new entities and reset the only
14881 -- semantic field.
14882
14883 Spec := Copy_Subprogram_Spec (Specification (Subp_Decl));
14884 Set_Generic_Parent (Spec, Empty);
14885
14886 -- Generate:
14887 -- function Func ... return ... is
14888 -- <or>
14889 -- procedure Proc ... is
14890 -- begin
14891 -- raise Program_Error with "access before elaboration";
14892 -- edn Proc;
14893
14894 Insert_After_And_Analyze (Subp_Decl,
14895 Make_Subprogram_Body (Loc,
14896 Specification => Spec,
14897 Declarations => New_List,
14898 Handled_Statement_Sequence =>
14899 Make_Handled_Sequence_Of_Statements (Loc,
14900 Statements => New_List (
14901 Make_Raise_Program_Error (Loc,
14902 Reason => PE_Access_Before_Elaboration)))));
14903 end Build_Completing_Body;
14904
14905 ----------------------------------
14906 -- Provide_Completing_Bodies_In --
14907 ----------------------------------
14908
14909 procedure Provide_Completing_Bodies_In (Decls : List_Id) is
14910 Decl : Node_Id;
14911
14912 begin
14913 if Present (Decls) then
14914 Decl := First (Decls);
14915 while Present (Decl) loop
14916 Provide_Completing_Bodies (Decl);
14917 Next (Decl);
14918 end loop;
14919 end if;
14920 end Provide_Completing_Bodies_In;
14921
14922 -- Local variables
14923
14924 Spec : Node_Id;
14925
14926 -- Start of processing for Provide_Completing_Bodies
14927
14928 begin
14929 if Nkind (N) = N_Package_Declaration then
14930 Spec := Specification (N);
14931
14932 Push_Scope (Defining_Entity (N));
14933 Provide_Completing_Bodies_In (Visible_Declarations (Spec));
14934 Provide_Completing_Bodies_In (Private_Declarations (Spec));
14935 Pop_Scope;
14936
14937 elsif Nkind (N) = N_Subprogram_Declaration then
14938 Build_Completing_Body (N);
14939 end if;
14940 end Provide_Completing_Bodies;
14941
14942 -------------------
14943 -- Remove_Parent --
14944 -------------------
14945
14946 procedure Remove_Parent (In_Body : Boolean := False) is
14947 S : Entity_Id := Current_Scope;
14948 -- S is the scope containing the instantiation just completed. The scope
14949 -- stack contains the parent instances of the instantiation, followed by
14950 -- the original S.
14951
14952 Cur_P : Entity_Id;
14953 E : Entity_Id;
14954 P : Entity_Id;
14955 Hidden : Elmt_Id;
14956
14957 begin
14958 -- After child instantiation is complete, remove from scope stack the
14959 -- extra copy of the current scope, and then remove parent instances.
14960
14961 if not In_Body then
14962 Pop_Scope;
14963
14964 while Current_Scope /= S loop
14965 P := Current_Scope;
14966 End_Package_Scope (Current_Scope);
14967
14968 if In_Open_Scopes (P) then
14969 E := First_Entity (P);
14970 while Present (E) loop
14971 Set_Is_Immediately_Visible (E, True);
14972 Next_Entity (E);
14973 end loop;
14974
14975 -- If instantiation is declared in a block, it is the enclosing
14976 -- scope that might be a parent instance. Note that only one
14977 -- block can be involved, because the parent instances have
14978 -- been installed within it.
14979
14980 if Ekind (P) = E_Block then
14981 Cur_P := Scope (P);
14982 else
14983 Cur_P := P;
14984 end if;
14985
14986 if Is_Generic_Instance (Cur_P) and then P /= Current_Scope then
14987 -- We are within an instance of some sibling. Retain
14988 -- visibility of parent, for proper subsequent cleanup, and
14989 -- reinstall private declarations as well.
14990
14991 Set_In_Private_Part (P);
14992 Install_Private_Declarations (P);
14993 end if;
14994
14995 -- If the ultimate parent is a top-level unit recorded in
14996 -- Instance_Parent_Unit, then reset its visibility to what it was
14997 -- before instantiation. (It's not clear what the purpose is of
14998 -- testing whether Scope (P) is In_Open_Scopes, but that test was
14999 -- present before the ultimate parent test was added.???)
15000
15001 elsif not In_Open_Scopes (Scope (P))
15002 or else (P = Instance_Parent_Unit
15003 and then not Parent_Unit_Visible)
15004 then
15005 Set_Is_Immediately_Visible (P, False);
15006
15007 -- If the current scope is itself an instantiation of a generic
15008 -- nested within P, and we are in the private part of body of this
15009 -- instantiation, restore the full views of P, that were removed
15010 -- in End_Package_Scope above. This obscure case can occur when a
15011 -- subunit of a generic contains an instance of a child unit of
15012 -- its generic parent unit.
15013
15014 elsif S = Current_Scope and then Is_Generic_Instance (S) then
15015 declare
15016 Par : constant Entity_Id :=
15017 Generic_Parent (Package_Specification (S));
15018 begin
15019 if Present (Par)
15020 and then P = Scope (Par)
15021 and then (In_Package_Body (S) or else In_Private_Part (S))
15022 then
15023 Set_In_Private_Part (P);
15024 Install_Private_Declarations (P);
15025 end if;
15026 end;
15027 end if;
15028 end loop;
15029
15030 -- Reset visibility of entities in the enclosing scope
15031
15032 Set_Is_Hidden_Open_Scope (Current_Scope, False);
15033
15034 Hidden := First_Elmt (Hidden_Entities);
15035 while Present (Hidden) loop
15036 Set_Is_Immediately_Visible (Node (Hidden), True);
15037 Next_Elmt (Hidden);
15038 end loop;
15039
15040 else
15041 -- Each body is analyzed separately, and there is no context that
15042 -- needs preserving from one body instance to the next, so remove all
15043 -- parent scopes that have been installed.
15044
15045 while Present (S) loop
15046 End_Package_Scope (S);
15047 Set_Is_Immediately_Visible (S, False);
15048 S := Current_Scope;
15049 exit when S = Standard_Standard;
15050 end loop;
15051 end if;
15052 end Remove_Parent;
15053
15054 -----------------
15055 -- Restore_Env --
15056 -----------------
15057
15058 procedure Restore_Env is
15059 Saved : Instance_Env renames Instance_Envs.Table (Instance_Envs.Last);
15060
15061 begin
15062 if No (Current_Instantiated_Parent.Act_Id) then
15063 -- Restore environment after subprogram inlining
15064
15065 Restore_Private_Views (Empty);
15066 end if;
15067
15068 Current_Instantiated_Parent := Saved.Instantiated_Parent;
15069 Exchanged_Views := Saved.Exchanged_Views;
15070 Hidden_Entities := Saved.Hidden_Entities;
15071 Current_Sem_Unit := Saved.Current_Sem_Unit;
15072 Parent_Unit_Visible := Saved.Parent_Unit_Visible;
15073 Instance_Parent_Unit := Saved.Instance_Parent_Unit;
15074
15075 Restore_Config_Switches (Saved.Switches);
15076
15077 Instance_Envs.Decrement_Last;
15078 end Restore_Env;
15079
15080 ---------------------------
15081 -- Restore_Private_Views --
15082 ---------------------------
15083
15084 procedure Restore_Private_Views
15085 (Pack_Id : Entity_Id;
15086 Is_Package : Boolean := True)
15087 is
15088 M : Elmt_Id;
15089 E : Entity_Id;
15090 Typ : Entity_Id;
15091 Dep_Elmt : Elmt_Id;
15092 Dep_Typ : Node_Id;
15093
15094 procedure Restore_Nested_Formal (Formal : Entity_Id);
15095 -- Hide the generic formals of formal packages declared with box which
15096 -- were reachable in the current instantiation.
15097
15098 ---------------------------
15099 -- Restore_Nested_Formal --
15100 ---------------------------
15101
15102 procedure Restore_Nested_Formal (Formal : Entity_Id) is
15103 Ent : Entity_Id;
15104
15105 begin
15106 if Present (Renamed_Object (Formal))
15107 and then Denotes_Formal_Package (Renamed_Object (Formal), True)
15108 then
15109 return;
15110
15111 elsif Present (Associated_Formal_Package (Formal)) then
15112 Ent := First_Entity (Formal);
15113 while Present (Ent) loop
15114 exit when Ekind (Ent) = E_Package
15115 and then Renamed_Entity (Ent) = Renamed_Entity (Formal);
15116
15117 Set_Is_Hidden (Ent);
15118 Set_Is_Potentially_Use_Visible (Ent, False);
15119
15120 -- If package, then recurse
15121
15122 if Ekind (Ent) = E_Package then
15123 Restore_Nested_Formal (Ent);
15124 end if;
15125
15126 Next_Entity (Ent);
15127 end loop;
15128 end if;
15129 end Restore_Nested_Formal;
15130
15131 -- Start of processing for Restore_Private_Views
15132
15133 begin
15134 M := First_Elmt (Exchanged_Views);
15135 while Present (M) loop
15136 Typ := Node (M);
15137
15138 -- Subtypes of types whose views have been exchanged, and that are
15139 -- defined within the instance, were not on the Private_Dependents
15140 -- list on entry to the instance, so they have to be exchanged
15141 -- explicitly now, in order to remain consistent with the view of the
15142 -- parent type.
15143
15144 if Ekind_In (Typ, E_Private_Type,
15145 E_Limited_Private_Type,
15146 E_Record_Type_With_Private)
15147 then
15148 Dep_Elmt := First_Elmt (Private_Dependents (Typ));
15149 while Present (Dep_Elmt) loop
15150 Dep_Typ := Node (Dep_Elmt);
15151
15152 if Scope (Dep_Typ) = Pack_Id
15153 and then Present (Full_View (Dep_Typ))
15154 then
15155 Replace_Elmt (Dep_Elmt, Full_View (Dep_Typ));
15156 Exchange_Declarations (Dep_Typ);
15157 end if;
15158
15159 Next_Elmt (Dep_Elmt);
15160 end loop;
15161 end if;
15162
15163 Exchange_Declarations (Node (M));
15164 Next_Elmt (M);
15165 end loop;
15166
15167 if No (Pack_Id) then
15168 return;
15169 end if;
15170
15171 -- Make the generic formal parameters private, and make the formal types
15172 -- into subtypes of the actuals again.
15173
15174 E := First_Entity (Pack_Id);
15175 while Present (E) loop
15176 Set_Is_Hidden (E, True);
15177
15178 if Is_Type (E)
15179 and then Nkind (Parent (E)) = N_Subtype_Declaration
15180 then
15181 -- If the actual for E is itself a generic actual type from
15182 -- an enclosing instance, E is still a generic actual type
15183 -- outside of the current instance. This matter when resolving
15184 -- an overloaded call that may be ambiguous in the enclosing
15185 -- instance, when two of its actuals coincide.
15186
15187 if Is_Entity_Name (Subtype_Indication (Parent (E)))
15188 and then Is_Generic_Actual_Type
15189 (Entity (Subtype_Indication (Parent (E))))
15190 then
15191 null;
15192 else
15193 Set_Is_Generic_Actual_Type (E, False);
15194
15195 -- It might seem reasonable to clear the Is_Generic_Actual_Type
15196 -- flag also on the Full_View if the type is private, since it
15197 -- was set also on this Full_View. However, this flag is relied
15198 -- upon by Covers to spot "types exported from instantiations"
15199 -- which are implicit Full_Views built for instantiations made
15200 -- on private types and we get type mismatches if we do it when
15201 -- the block exchanging the declarations below triggers ???
15202
15203 -- if Is_Private_Type (E) and then Present (Full_View (E)) then
15204 -- Set_Is_Generic_Actual_Type (Full_View (E), False);
15205 -- end if;
15206 end if;
15207
15208 -- An unusual case of aliasing: the actual may also be directly
15209 -- visible in the generic, and be private there, while it is fully
15210 -- visible in the context of the instance. The internal subtype
15211 -- is private in the instance but has full visibility like its
15212 -- parent in the enclosing scope. This enforces the invariant that
15213 -- the privacy status of all private dependents of a type coincide
15214 -- with that of the parent type. This can only happen when a
15215 -- generic child unit is instantiated within a sibling.
15216
15217 if Is_Private_Type (E)
15218 and then not Is_Private_Type (Etype (E))
15219 then
15220 Exchange_Declarations (E);
15221 end if;
15222
15223 elsif Ekind (E) = E_Package then
15224
15225 -- The end of the renaming list is the renaming of the generic
15226 -- package itself. If the instance is a subprogram, all entities
15227 -- in the corresponding package are renamings. If this entity is
15228 -- a formal package, make its own formals private as well. The
15229 -- actual in this case is itself the renaming of an instantiation.
15230 -- If the entity is not a package renaming, it is the entity
15231 -- created to validate formal package actuals: ignore it.
15232
15233 -- If the actual is itself a formal package for the enclosing
15234 -- generic, or the actual for such a formal package, it remains
15235 -- visible on exit from the instance, and therefore nothing needs
15236 -- to be done either, except to keep it accessible.
15237
15238 if Is_Package and then Renamed_Object (E) = Pack_Id then
15239 exit;
15240
15241 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
15242 null;
15243
15244 elsif
15245 Denotes_Formal_Package (Renamed_Object (E), True, Pack_Id)
15246 then
15247 Set_Is_Hidden (E, False);
15248
15249 else
15250 declare
15251 Act_P : constant Entity_Id := Renamed_Object (E);
15252 Id : Entity_Id;
15253
15254 begin
15255 Id := First_Entity (Act_P);
15256 while Present (Id)
15257 and then Id /= First_Private_Entity (Act_P)
15258 loop
15259 exit when Ekind (Id) = E_Package
15260 and then Renamed_Object (Id) = Act_P;
15261
15262 Set_Is_Hidden (Id, True);
15263 Set_Is_Potentially_Use_Visible (Id, In_Use (Act_P));
15264
15265 if Ekind (Id) = E_Package then
15266 Restore_Nested_Formal (Id);
15267 end if;
15268
15269 Next_Entity (Id);
15270 end loop;
15271 end;
15272 end if;
15273 end if;
15274
15275 Next_Entity (E);
15276 end loop;
15277 end Restore_Private_Views;
15278
15279 --------------
15280 -- Save_Env --
15281 --------------
15282
15283 procedure Save_Env
15284 (Gen_Unit : Entity_Id;
15285 Act_Unit : Entity_Id)
15286 is
15287 begin
15288 Init_Env;
15289 Set_Instance_Env (Gen_Unit, Act_Unit);
15290 end Save_Env;
15291
15292 ----------------------------
15293 -- Save_Global_References --
15294 ----------------------------
15295
15296 procedure Save_Global_References (Templ : Node_Id) is
15297
15298 -- ??? it is horrible to use global variables in highly recursive code
15299
15300 E : Entity_Id;
15301 -- The entity of the current associated node
15302
15303 Gen_Scope : Entity_Id;
15304 -- The scope of the generic for which references are being saved
15305
15306 N2 : Node_Id;
15307 -- The current associated node
15308
15309 function Is_Global (E : Entity_Id) return Boolean;
15310 -- Check whether entity is defined outside of generic unit. Examine the
15311 -- scope of an entity, and the scope of the scope, etc, until we find
15312 -- either Standard, in which case the entity is global, or the generic
15313 -- unit itself, which indicates that the entity is local. If the entity
15314 -- is the generic unit itself, as in the case of a recursive call, or
15315 -- the enclosing generic unit, if different from the current scope, then
15316 -- it is local as well, because it will be replaced at the point of
15317 -- instantiation. On the other hand, if it is a reference to a child
15318 -- unit of a common ancestor, which appears in an instantiation, it is
15319 -- global because it is used to denote a specific compilation unit at
15320 -- the time the instantiations will be analyzed.
15321
15322 procedure Qualify_Universal_Operands
15323 (Op : Node_Id;
15324 Func_Call : Node_Id);
15325 -- Op denotes a binary or unary operator in generic template Templ. Node
15326 -- Func_Call is the function call alternative of the operator within the
15327 -- the analyzed copy of the template. Change each operand which yields a
15328 -- universal type by wrapping it into a qualified expression
15329 --
15330 -- Actual_Typ'(Operand)
15331 --
15332 -- where Actual_Typ is the type of corresponding actual parameter of
15333 -- Operand in Func_Call.
15334
15335 procedure Reset_Entity (N : Node_Id);
15336 -- Save semantic information on global entity so that it is not resolved
15337 -- again at instantiation time.
15338
15339 procedure Save_Entity_Descendants (N : Node_Id);
15340 -- Apply Save_Global_References to the two syntactic descendants of
15341 -- non-terminal nodes that carry an Associated_Node and are processed
15342 -- through Reset_Entity. Once the global entity (if any) has been
15343 -- captured together with its type, only two syntactic descendants need
15344 -- to be traversed to complete the processing of the tree rooted at N.
15345 -- This applies to Selected_Components, Expanded_Names, and to Operator
15346 -- nodes. N can also be a character literal, identifier, or operator
15347 -- symbol node, but the call has no effect in these cases.
15348
15349 procedure Save_Global_Defaults (N1 : Node_Id; N2 : Node_Id);
15350 -- Default actuals in nested instances must be handled specially
15351 -- because there is no link to them from the original tree. When an
15352 -- actual subprogram is given by a default, we add an explicit generic
15353 -- association for it in the instantiation node. When we save the
15354 -- global references on the name of the instance, we recover the list
15355 -- of generic associations, and add an explicit one to the original
15356 -- generic tree, through which a global actual can be preserved.
15357 -- Similarly, if a child unit is instantiated within a sibling, in the
15358 -- context of the parent, we must preserve the identifier of the parent
15359 -- so that it can be properly resolved in a subsequent instantiation.
15360
15361 procedure Save_Global_Descendant (D : Union_Id);
15362 -- Apply Save_References recursively to the descendants of node D
15363
15364 procedure Save_References (N : Node_Id);
15365 -- This is the recursive procedure that does the work, once the
15366 -- enclosing generic scope has been established.
15367
15368 ---------------
15369 -- Is_Global --
15370 ---------------
15371
15372 function Is_Global (E : Entity_Id) return Boolean is
15373 Se : Entity_Id;
15374
15375 function Is_Instance_Node (Decl : Node_Id) return Boolean;
15376 -- Determine whether the parent node of a reference to a child unit
15377 -- denotes an instantiation or a formal package, in which case the
15378 -- reference to the child unit is global, even if it appears within
15379 -- the current scope (e.g. when the instance appears within the body
15380 -- of an ancestor).
15381
15382 ----------------------
15383 -- Is_Instance_Node --
15384 ----------------------
15385
15386 function Is_Instance_Node (Decl : Node_Id) return Boolean is
15387 begin
15388 return Nkind (Decl) in N_Generic_Instantiation
15389 or else
15390 Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration;
15391 end Is_Instance_Node;
15392
15393 -- Start of processing for Is_Global
15394
15395 begin
15396 if E = Gen_Scope then
15397 return False;
15398
15399 elsif E = Standard_Standard then
15400 return True;
15401
15402 elsif Is_Child_Unit (E)
15403 and then (Is_Instance_Node (Parent (N2))
15404 or else (Nkind (Parent (N2)) = N_Expanded_Name
15405 and then N2 = Selector_Name (Parent (N2))
15406 and then
15407 Is_Instance_Node (Parent (Parent (N2)))))
15408 then
15409 return True;
15410
15411 else
15412 Se := Scope (E);
15413 while Se /= Gen_Scope loop
15414 if Se = Standard_Standard then
15415 return True;
15416 else
15417 Se := Scope (Se);
15418 end if;
15419 end loop;
15420
15421 return False;
15422 end if;
15423 end Is_Global;
15424
15425 --------------------------------
15426 -- Qualify_Universal_Operands --
15427 --------------------------------
15428
15429 procedure Qualify_Universal_Operands
15430 (Op : Node_Id;
15431 Func_Call : Node_Id)
15432 is
15433 procedure Qualify_Operand (Opnd : Node_Id; Actual : Node_Id);
15434 -- Rewrite operand Opnd as a qualified expression of the form
15435 --
15436 -- Actual_Typ'(Opnd)
15437 --
15438 -- where Actual is the corresponding actual parameter of Opnd in
15439 -- function call Func_Call.
15440
15441 function Qualify_Type
15442 (Loc : Source_Ptr;
15443 Typ : Entity_Id) return Node_Id;
15444 -- Qualify type Typ by creating a selected component of the form
15445 --
15446 -- Scope_Of_Typ.Typ
15447
15448 ---------------------
15449 -- Qualify_Operand --
15450 ---------------------
15451
15452 procedure Qualify_Operand (Opnd : Node_Id; Actual : Node_Id) is
15453 Loc : constant Source_Ptr := Sloc (Opnd);
15454 Typ : constant Entity_Id := Etype (Actual);
15455 Mark : Node_Id;
15456 Qual : Node_Id;
15457
15458 begin
15459 -- Qualify the operand when it is of a universal type. Note that
15460 -- the template is unanalyzed and it is not possible to directly
15461 -- query the type. This transformation is not done when the type
15462 -- of the actual is internally generated because the type will be
15463 -- regenerated in the instance.
15464
15465 if Yields_Universal_Type (Opnd)
15466 and then Comes_From_Source (Typ)
15467 and then not Is_Hidden (Typ)
15468 then
15469 -- The type of the actual may be a global reference. Save this
15470 -- information by creating a reference to it.
15471
15472 if Is_Global (Typ) then
15473 Mark := New_Occurrence_Of (Typ, Loc);
15474
15475 -- Otherwise rely on resolution to find the proper type within
15476 -- the instance.
15477
15478 else
15479 Mark := Qualify_Type (Loc, Typ);
15480 end if;
15481
15482 Qual :=
15483 Make_Qualified_Expression (Loc,
15484 Subtype_Mark => Mark,
15485 Expression => Relocate_Node (Opnd));
15486
15487 -- Mark the qualification to distinguish it from other source
15488 -- constructs and signal the instantiation mechanism that this
15489 -- node requires special processing. See Copy_Generic_Node for
15490 -- details.
15491
15492 Set_Is_Qualified_Universal_Literal (Qual);
15493
15494 Rewrite (Opnd, Qual);
15495 end if;
15496 end Qualify_Operand;
15497
15498 ------------------
15499 -- Qualify_Type --
15500 ------------------
15501
15502 function Qualify_Type
15503 (Loc : Source_Ptr;
15504 Typ : Entity_Id) return Node_Id
15505 is
15506 Scop : constant Entity_Id := Scope (Typ);
15507 Result : Node_Id;
15508
15509 begin
15510 Result := Make_Identifier (Loc, Chars (Typ));
15511
15512 if Present (Scop) and then not Is_Generic_Unit (Scop) then
15513 Result :=
15514 Make_Selected_Component (Loc,
15515 Prefix => Make_Identifier (Loc, Chars (Scop)),
15516 Selector_Name => Result);
15517 end if;
15518
15519 return Result;
15520 end Qualify_Type;
15521
15522 -- Local variables
15523
15524 Actuals : constant List_Id := Parameter_Associations (Func_Call);
15525
15526 -- Start of processing for Qualify_Universal_Operands
15527
15528 begin
15529 if Nkind (Op) in N_Binary_Op then
15530 Qualify_Operand (Left_Opnd (Op), First (Actuals));
15531 Qualify_Operand (Right_Opnd (Op), Next (First (Actuals)));
15532
15533 elsif Nkind (Op) in N_Unary_Op then
15534 Qualify_Operand (Right_Opnd (Op), First (Actuals));
15535 end if;
15536 end Qualify_Universal_Operands;
15537
15538 ------------------
15539 -- Reset_Entity --
15540 ------------------
15541
15542 procedure Reset_Entity (N : Node_Id) is
15543 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id);
15544 -- If the type of N2 is global to the generic unit, save the type in
15545 -- the generic node. Just as we perform name capture for explicit
15546 -- references within the generic, we must capture the global types
15547 -- of local entities because they may participate in resolution in
15548 -- the instance.
15549
15550 function Top_Ancestor (E : Entity_Id) return Entity_Id;
15551 -- Find the ultimate ancestor of the current unit. If it is not a
15552 -- generic unit, then the name of the current unit in the prefix of
15553 -- an expanded name must be replaced with its generic homonym to
15554 -- ensure that it will be properly resolved in an instance.
15555
15556 ---------------------
15557 -- Set_Global_Type --
15558 ---------------------
15559
15560 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id) is
15561 Typ : constant Entity_Id := Etype (N2);
15562
15563 begin
15564 Set_Etype (N, Typ);
15565
15566 -- If the entity of N is not the associated node, this is a
15567 -- nested generic and it has an associated node as well, whose
15568 -- type is already the full view (see below). Indicate that the
15569 -- original node has a private view.
15570
15571 if Entity (N) /= N2 and then Has_Private_View (Entity (N)) then
15572 Set_Has_Private_View (N);
15573 end if;
15574
15575 -- If not a private type, nothing else to do
15576
15577 if not Is_Private_Type (Typ) then
15578 null;
15579
15580 -- If it is a derivation of a private type in a context where no
15581 -- full view is needed, nothing to do either.
15582
15583 elsif No (Full_View (Typ)) and then Typ /= Etype (Typ) then
15584 null;
15585
15586 -- Otherwise mark the type for flipping and use the full view when
15587 -- available.
15588
15589 else
15590 Set_Has_Private_View (N);
15591
15592 if Present (Full_View (Typ)) then
15593 Set_Etype (N2, Full_View (Typ));
15594 end if;
15595 end if;
15596
15597 if Is_Floating_Point_Type (Typ)
15598 and then Has_Dimension_System (Typ)
15599 then
15600 Copy_Dimensions (N2, N);
15601 end if;
15602 end Set_Global_Type;
15603
15604 ------------------
15605 -- Top_Ancestor --
15606 ------------------
15607
15608 function Top_Ancestor (E : Entity_Id) return Entity_Id is
15609 Par : Entity_Id;
15610
15611 begin
15612 Par := E;
15613 while Is_Child_Unit (Par) loop
15614 Par := Scope (Par);
15615 end loop;
15616
15617 return Par;
15618 end Top_Ancestor;
15619
15620 -- Start of processing for Reset_Entity
15621
15622 begin
15623 N2 := Get_Associated_Node (N);
15624 E := Entity (N2);
15625
15626 if Present (E) then
15627
15628 -- If the node is an entry call to an entry in an enclosing task,
15629 -- it is rewritten as a selected component. No global entity to
15630 -- preserve in this case, since the expansion will be redone in
15631 -- the instance.
15632
15633 if not Nkind_In (E, N_Defining_Character_Literal,
15634 N_Defining_Identifier,
15635 N_Defining_Operator_Symbol)
15636 then
15637 Set_Associated_Node (N, Empty);
15638 Set_Etype (N, Empty);
15639 return;
15640 end if;
15641
15642 -- If the entity is an itype created as a subtype of an access
15643 -- type with a null exclusion restore source entity for proper
15644 -- visibility. The itype will be created anew in the instance.
15645
15646 if Is_Itype (E)
15647 and then Ekind (E) = E_Access_Subtype
15648 and then Is_Entity_Name (N)
15649 and then Chars (Etype (E)) = Chars (N)
15650 then
15651 E := Etype (E);
15652 Set_Entity (N2, E);
15653 Set_Etype (N2, E);
15654 end if;
15655
15656 if Is_Global (E) then
15657 Set_Global_Type (N, N2);
15658
15659 elsif Nkind (N) = N_Op_Concat
15660 and then Is_Generic_Type (Etype (N2))
15661 and then (Base_Type (Etype (Right_Opnd (N2))) = Etype (N2)
15662 or else
15663 Base_Type (Etype (Left_Opnd (N2))) = Etype (N2))
15664 and then Is_Intrinsic_Subprogram (E)
15665 then
15666 null;
15667
15668 -- Entity is local. Mark generic node as unresolved. Note that now
15669 -- it does not have an entity.
15670
15671 else
15672 Set_Associated_Node (N, Empty);
15673 Set_Etype (N, Empty);
15674 end if;
15675
15676 if Nkind (Parent (N)) in N_Generic_Instantiation
15677 and then N = Name (Parent (N))
15678 then
15679 Save_Global_Defaults (Parent (N), Parent (N2));
15680 end if;
15681
15682 elsif Nkind (Parent (N)) = N_Selected_Component
15683 and then Nkind (Parent (N2)) = N_Expanded_Name
15684 then
15685 if Is_Global (Entity (Parent (N2))) then
15686 Change_Selected_Component_To_Expanded_Name (Parent (N));
15687 Set_Associated_Node (Parent (N), Parent (N2));
15688 Set_Global_Type (Parent (N), Parent (N2));
15689 Save_Entity_Descendants (N);
15690
15691 -- If this is a reference to the current generic entity, replace
15692 -- by the name of the generic homonym of the current package. This
15693 -- is because in an instantiation Par.P.Q will not resolve to the
15694 -- name of the instance, whose enclosing scope is not necessarily
15695 -- Par. We use the generic homonym rather that the name of the
15696 -- generic itself because it may be hidden by a local declaration.
15697
15698 elsif In_Open_Scopes (Entity (Parent (N2)))
15699 and then not
15700 Is_Generic_Unit (Top_Ancestor (Entity (Prefix (Parent (N2)))))
15701 then
15702 if Ekind (Entity (Parent (N2))) = E_Generic_Package then
15703 Rewrite (Parent (N),
15704 Make_Identifier (Sloc (N),
15705 Chars =>
15706 Chars (Generic_Homonym (Entity (Parent (N2))))));
15707 else
15708 Rewrite (Parent (N),
15709 Make_Identifier (Sloc (N),
15710 Chars => Chars (Selector_Name (Parent (N2)))));
15711 end if;
15712 end if;
15713
15714 if Nkind (Parent (Parent (N))) in N_Generic_Instantiation
15715 and then Parent (N) = Name (Parent (Parent (N)))
15716 then
15717 Save_Global_Defaults
15718 (Parent (Parent (N)), Parent (Parent (N2)));
15719 end if;
15720
15721 -- A selected component may denote a static constant that has been
15722 -- folded. If the static constant is global to the generic, capture
15723 -- its value. Otherwise the folding will happen in any instantiation.
15724
15725 elsif Nkind (Parent (N)) = N_Selected_Component
15726 and then Nkind_In (Parent (N2), N_Integer_Literal, N_Real_Literal)
15727 then
15728 if Present (Entity (Original_Node (Parent (N2))))
15729 and then Is_Global (Entity (Original_Node (Parent (N2))))
15730 then
15731 Rewrite (Parent (N), New_Copy (Parent (N2)));
15732 Set_Analyzed (Parent (N), False);
15733 end if;
15734
15735 -- A selected component may be transformed into a parameterless
15736 -- function call. If the called entity is global, rewrite the node
15737 -- appropriately, i.e. as an extended name for the global entity.
15738
15739 elsif Nkind (Parent (N)) = N_Selected_Component
15740 and then Nkind (Parent (N2)) = N_Function_Call
15741 and then N = Selector_Name (Parent (N))
15742 then
15743 if No (Parameter_Associations (Parent (N2))) then
15744 if Is_Global (Entity (Name (Parent (N2)))) then
15745 Change_Selected_Component_To_Expanded_Name (Parent (N));
15746 Set_Associated_Node (Parent (N), Name (Parent (N2)));
15747 Set_Global_Type (Parent (N), Name (Parent (N2)));
15748 Save_Entity_Descendants (N);
15749
15750 else
15751 Set_Is_Prefixed_Call (Parent (N));
15752 Set_Associated_Node (N, Empty);
15753 Set_Etype (N, Empty);
15754 end if;
15755
15756 -- In Ada 2005, X.F may be a call to a primitive operation,
15757 -- rewritten as F (X). This rewriting will be done again in an
15758 -- instance, so keep the original node. Global entities will be
15759 -- captured as for other constructs. Indicate that this must
15760 -- resolve as a call, to prevent accidental overloading in the
15761 -- instance, if both a component and a primitive operation appear
15762 -- as candidates.
15763
15764 else
15765 Set_Is_Prefixed_Call (Parent (N));
15766 end if;
15767
15768 -- Entity is local. Reset in generic unit, so that node is resolved
15769 -- anew at the point of instantiation.
15770
15771 else
15772 Set_Associated_Node (N, Empty);
15773 Set_Etype (N, Empty);
15774 end if;
15775 end Reset_Entity;
15776
15777 -----------------------------
15778 -- Save_Entity_Descendants --
15779 -----------------------------
15780
15781 procedure Save_Entity_Descendants (N : Node_Id) is
15782 begin
15783 case Nkind (N) is
15784 when N_Binary_Op =>
15785 Save_Global_Descendant (Union_Id (Left_Opnd (N)));
15786 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
15787
15788 when N_Unary_Op =>
15789 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
15790
15791 when N_Expanded_Name
15792 | N_Selected_Component
15793 =>
15794 Save_Global_Descendant (Union_Id (Prefix (N)));
15795 Save_Global_Descendant (Union_Id (Selector_Name (N)));
15796
15797 when N_Character_Literal
15798 | N_Identifier
15799 | N_Operator_Symbol
15800 =>
15801 null;
15802
15803 when others =>
15804 raise Program_Error;
15805 end case;
15806 end Save_Entity_Descendants;
15807
15808 --------------------------
15809 -- Save_Global_Defaults --
15810 --------------------------
15811
15812 procedure Save_Global_Defaults (N1 : Node_Id; N2 : Node_Id) is
15813 Loc : constant Source_Ptr := Sloc (N1);
15814 Assoc2 : constant List_Id := Generic_Associations (N2);
15815 Gen_Id : constant Entity_Id := Get_Generic_Entity (N2);
15816 Assoc1 : List_Id;
15817 Act1 : Node_Id;
15818 Act2 : Node_Id;
15819 Def : Node_Id;
15820 Ndec : Node_Id;
15821 Subp : Entity_Id;
15822 Actual : Entity_Id;
15823
15824 begin
15825 Assoc1 := Generic_Associations (N1);
15826
15827 if Present (Assoc1) then
15828 Act1 := First (Assoc1);
15829 else
15830 Act1 := Empty;
15831 Set_Generic_Associations (N1, New_List);
15832 Assoc1 := Generic_Associations (N1);
15833 end if;
15834
15835 if Present (Assoc2) then
15836 Act2 := First (Assoc2);
15837 else
15838 return;
15839 end if;
15840
15841 while Present (Act1) and then Present (Act2) loop
15842 Next (Act1);
15843 Next (Act2);
15844 end loop;
15845
15846 -- Find the associations added for default subprograms
15847
15848 if Present (Act2) then
15849 while Nkind (Act2) /= N_Generic_Association
15850 or else No (Entity (Selector_Name (Act2)))
15851 or else not Is_Overloadable (Entity (Selector_Name (Act2)))
15852 loop
15853 Next (Act2);
15854 end loop;
15855
15856 -- Add a similar association if the default is global. The
15857 -- renaming declaration for the actual has been analyzed, and
15858 -- its alias is the program it renames. Link the actual in the
15859 -- original generic tree with the node in the analyzed tree.
15860
15861 while Present (Act2) loop
15862 Subp := Entity (Selector_Name (Act2));
15863 Def := Explicit_Generic_Actual_Parameter (Act2);
15864
15865 -- Following test is defence against rubbish errors
15866
15867 if No (Alias (Subp)) then
15868 return;
15869 end if;
15870
15871 -- Retrieve the resolved actual from the renaming declaration
15872 -- created for the instantiated formal.
15873
15874 Actual := Entity (Name (Parent (Parent (Subp))));
15875 Set_Entity (Def, Actual);
15876 Set_Etype (Def, Etype (Actual));
15877
15878 if Is_Global (Actual) then
15879 Ndec :=
15880 Make_Generic_Association (Loc,
15881 Selector_Name =>
15882 New_Occurrence_Of (Subp, Loc),
15883 Explicit_Generic_Actual_Parameter =>
15884 New_Occurrence_Of (Actual, Loc));
15885
15886 Set_Associated_Node
15887 (Explicit_Generic_Actual_Parameter (Ndec), Def);
15888
15889 Append (Ndec, Assoc1);
15890
15891 -- If there are other defaults, add a dummy association in case
15892 -- there are other defaulted formals with the same name.
15893
15894 elsif Present (Next (Act2)) then
15895 Ndec :=
15896 Make_Generic_Association (Loc,
15897 Selector_Name =>
15898 New_Occurrence_Of (Subp, Loc),
15899 Explicit_Generic_Actual_Parameter => Empty);
15900
15901 Append (Ndec, Assoc1);
15902 end if;
15903
15904 Next (Act2);
15905 end loop;
15906 end if;
15907
15908 if Nkind (Name (N1)) = N_Identifier
15909 and then Is_Child_Unit (Gen_Id)
15910 and then Is_Global (Gen_Id)
15911 and then Is_Generic_Unit (Scope (Gen_Id))
15912 and then In_Open_Scopes (Scope (Gen_Id))
15913 then
15914 -- This is an instantiation of a child unit within a sibling, so
15915 -- that the generic parent is in scope. An eventual instance must
15916 -- occur within the scope of an instance of the parent. Make name
15917 -- in instance into an expanded name, to preserve the identifier
15918 -- of the parent, so it can be resolved subsequently.
15919
15920 Rewrite (Name (N2),
15921 Make_Expanded_Name (Loc,
15922 Chars => Chars (Gen_Id),
15923 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
15924 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
15925 Set_Entity (Name (N2), Gen_Id);
15926
15927 Rewrite (Name (N1),
15928 Make_Expanded_Name (Loc,
15929 Chars => Chars (Gen_Id),
15930 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
15931 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
15932
15933 Set_Associated_Node (Name (N1), Name (N2));
15934 Set_Associated_Node (Prefix (Name (N1)), Empty);
15935 Set_Associated_Node
15936 (Selector_Name (Name (N1)), Selector_Name (Name (N2)));
15937 Set_Etype (Name (N1), Etype (Gen_Id));
15938 end if;
15939 end Save_Global_Defaults;
15940
15941 ----------------------------
15942 -- Save_Global_Descendant --
15943 ----------------------------
15944
15945 procedure Save_Global_Descendant (D : Union_Id) is
15946 N1 : Node_Id;
15947
15948 begin
15949 if D in Node_Range then
15950 if D = Union_Id (Empty) then
15951 null;
15952
15953 elsif Nkind (Node_Id (D)) /= N_Compilation_Unit then
15954 Save_References (Node_Id (D));
15955 end if;
15956
15957 elsif D in List_Range then
15958 pragma Assert (D /= Union_Id (No_List));
15959 -- Because No_List = Empty, which is in Node_Range above
15960
15961 if Is_Empty_List (List_Id (D)) then
15962 null;
15963
15964 else
15965 N1 := First (List_Id (D));
15966 while Present (N1) loop
15967 Save_References (N1);
15968 Next (N1);
15969 end loop;
15970 end if;
15971
15972 -- Element list or other non-node field, nothing to do
15973
15974 else
15975 null;
15976 end if;
15977 end Save_Global_Descendant;
15978
15979 ---------------------
15980 -- Save_References --
15981 ---------------------
15982
15983 -- This is the recursive procedure that does the work once the enclosing
15984 -- generic scope has been established. We have to treat specially a
15985 -- number of node rewritings that are required by semantic processing
15986 -- and which change the kind of nodes in the generic copy: typically
15987 -- constant-folding, replacing an operator node by a string literal, or
15988 -- a selected component by an expanded name. In each of those cases, the
15989 -- transformation is propagated to the generic unit.
15990
15991 procedure Save_References (N : Node_Id) is
15992 Loc : constant Source_Ptr := Sloc (N);
15993
15994 function Requires_Delayed_Save (Nod : Node_Id) return Boolean;
15995 -- Determine whether arbitrary node Nod requires delayed capture of
15996 -- global references within its aspect specifications.
15997
15998 procedure Save_References_In_Aggregate (N : Node_Id);
15999 -- Save all global references in [extension] aggregate node N
16000
16001 procedure Save_References_In_Char_Lit_Or_Op_Symbol (N : Node_Id);
16002 -- Save all global references in a character literal or operator
16003 -- symbol denoted by N.
16004
16005 procedure Save_References_In_Descendants (N : Node_Id);
16006 -- Save all global references in all descendants of node N
16007
16008 procedure Save_References_In_Identifier (N : Node_Id);
16009 -- Save all global references in identifier node N
16010
16011 procedure Save_References_In_Operator (N : Node_Id);
16012 -- Save all global references in operator node N
16013
16014 procedure Save_References_In_Pragma (Prag : Node_Id);
16015 -- Save all global references found within the expression of pragma
16016 -- Prag.
16017
16018 ---------------------------
16019 -- Requires_Delayed_Save --
16020 ---------------------------
16021
16022 function Requires_Delayed_Save (Nod : Node_Id) return Boolean is
16023 begin
16024 -- Generic packages and subprograms require delayed capture of
16025 -- global references within their aspects due to the timing of
16026 -- annotation analysis.
16027
16028 if Nkind_In (Nod, N_Generic_Package_Declaration,
16029 N_Generic_Subprogram_Declaration,
16030 N_Package_Body,
16031 N_Package_Body_Stub,
16032 N_Subprogram_Body,
16033 N_Subprogram_Body_Stub)
16034 then
16035 -- Since the capture of global references is done on the
16036 -- unanalyzed generic template, there is no information around
16037 -- to infer the context. Use the Associated_Entity linkages to
16038 -- peek into the analyzed generic copy and determine what the
16039 -- template corresponds to.
16040
16041 if Nod = Templ then
16042 return
16043 Is_Generic_Declaration_Or_Body
16044 (Unit_Declaration_Node
16045 (Associated_Entity (Defining_Entity (Nod))));
16046
16047 -- Otherwise the generic unit being processed is not the top
16048 -- level template. It is safe to capture of global references
16049 -- within the generic unit because at this point the top level
16050 -- copy is fully analyzed.
16051
16052 else
16053 return False;
16054 end if;
16055
16056 -- Otherwise capture the global references without interference
16057
16058 else
16059 return False;
16060 end if;
16061 end Requires_Delayed_Save;
16062
16063 ----------------------------------
16064 -- Save_References_In_Aggregate --
16065 ----------------------------------
16066
16067 procedure Save_References_In_Aggregate (N : Node_Id) is
16068 Nam : Node_Id;
16069 Qual : Node_Id := Empty;
16070 Typ : Entity_Id := Empty;
16071
16072 use Atree.Unchecked_Access;
16073 -- This code section is part of implementing an untyped tree
16074 -- traversal, so it needs direct access to node fields.
16075
16076 begin
16077 N2 := Get_Associated_Node (N);
16078
16079 if Present (N2) then
16080 Typ := Etype (N2);
16081
16082 -- In an instance within a generic, use the name of the actual
16083 -- and not the original generic parameter. If the actual is
16084 -- global in the current generic it must be preserved for its
16085 -- instantiation.
16086
16087 if Nkind (Parent (Typ)) = N_Subtype_Declaration
16088 and then Present (Generic_Parent_Type (Parent (Typ)))
16089 then
16090 Typ := Base_Type (Typ);
16091 Set_Etype (N2, Typ);
16092 end if;
16093 end if;
16094
16095 if No (N2) or else No (Typ) or else not Is_Global (Typ) then
16096 Set_Associated_Node (N, Empty);
16097
16098 -- If the aggregate is an actual in a call, it has been
16099 -- resolved in the current context, to some local type. The
16100 -- enclosing call may have been disambiguated by the aggregate,
16101 -- and this disambiguation might fail at instantiation time
16102 -- because the type to which the aggregate did resolve is not
16103 -- preserved. In order to preserve some of this information,
16104 -- wrap the aggregate in a qualified expression, using the id
16105 -- of its type. For further disambiguation we qualify the type
16106 -- name with its scope (if visible and not hidden by a local
16107 -- homograph) because both id's will have corresponding
16108 -- entities in an instance. This resolves most of the problems
16109 -- with missing type information on aggregates in instances.
16110
16111 if Present (N2)
16112 and then Nkind (N2) = Nkind (N)
16113 and then Nkind (Parent (N2)) in N_Subprogram_Call
16114 and then Present (Typ)
16115 and then Comes_From_Source (Typ)
16116 then
16117 Nam := Make_Identifier (Loc, Chars (Typ));
16118
16119 if Is_Immediately_Visible (Scope (Typ))
16120 and then
16121 (not In_Open_Scopes (Scope (Typ))
16122 or else Current_Entity (Scope (Typ)) = Scope (Typ))
16123 then
16124 Nam :=
16125 Make_Selected_Component (Loc,
16126 Prefix =>
16127 Make_Identifier (Loc, Chars (Scope (Typ))),
16128 Selector_Name => Nam);
16129 end if;
16130
16131 Qual :=
16132 Make_Qualified_Expression (Loc,
16133 Subtype_Mark => Nam,
16134 Expression => Relocate_Node (N));
16135 end if;
16136 end if;
16137
16138 Save_Global_Descendant (Field1 (N));
16139 Save_Global_Descendant (Field2 (N));
16140 Save_Global_Descendant (Field3 (N));
16141 Save_Global_Descendant (Field5 (N));
16142
16143 if Present (Qual) then
16144 Rewrite (N, Qual);
16145 end if;
16146 end Save_References_In_Aggregate;
16147
16148 ----------------------------------------------
16149 -- Save_References_In_Char_Lit_Or_Op_Symbol --
16150 ----------------------------------------------
16151
16152 procedure Save_References_In_Char_Lit_Or_Op_Symbol (N : Node_Id) is
16153 begin
16154 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
16155 Reset_Entity (N);
16156
16157 elsif Nkind (N) = N_Operator_Symbol
16158 and then Nkind (Get_Associated_Node (N)) = N_String_Literal
16159 then
16160 Change_Operator_Symbol_To_String_Literal (N);
16161 end if;
16162 end Save_References_In_Char_Lit_Or_Op_Symbol;
16163
16164 ------------------------------------
16165 -- Save_References_In_Descendants --
16166 ------------------------------------
16167
16168 procedure Save_References_In_Descendants (N : Node_Id) is
16169 use Atree.Unchecked_Access;
16170 -- This code section is part of implementing an untyped tree
16171 -- traversal, so it needs direct access to node fields.
16172
16173 begin
16174 Save_Global_Descendant (Field1 (N));
16175 Save_Global_Descendant (Field2 (N));
16176 Save_Global_Descendant (Field3 (N));
16177 Save_Global_Descendant (Field4 (N));
16178 Save_Global_Descendant (Field5 (N));
16179 end Save_References_In_Descendants;
16180
16181 -----------------------------------
16182 -- Save_References_In_Identifier --
16183 -----------------------------------
16184
16185 procedure Save_References_In_Identifier (N : Node_Id) is
16186 begin
16187 -- The node did not undergo a transformation
16188
16189 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
16190 -- If this is a discriminant reference, always save it.
16191 -- It is used in the instance to find the corresponding
16192 -- discriminant positionally rather than by name.
16193
16194 Set_Original_Discriminant
16195 (N, Original_Discriminant (Get_Associated_Node (N)));
16196
16197 Reset_Entity (N);
16198
16199 -- The analysis of the generic copy transformed the identifier
16200 -- into another construct. Propagate the changes to the template.
16201
16202 else
16203 N2 := Get_Associated_Node (N);
16204
16205 -- The identifier denotes a call to a parameterless function.
16206 -- Mark the node as resolved when the function is external.
16207
16208 if Nkind (N2) = N_Function_Call then
16209 E := Entity (Name (N2));
16210
16211 if Present (E) and then Is_Global (E) then
16212 Set_Etype (N, Etype (N2));
16213 else
16214 Set_Associated_Node (N, Empty);
16215 Set_Etype (N, Empty);
16216 end if;
16217
16218 -- The identifier denotes a named number that was constant
16219 -- folded. Preserve the original name for ASIS and undo the
16220 -- constant folding which will be repeated in the instance.
16221 -- Is this still needed???
16222
16223 elsif Nkind_In (N2, N_Integer_Literal, N_Real_Literal)
16224 and then Is_Entity_Name (Original_Node (N2))
16225 then
16226 Set_Associated_Node (N, Original_Node (N2));
16227 Reset_Entity (N);
16228
16229 -- The identifier resolved to a string literal. Propagate this
16230 -- information to the generic template.
16231
16232 elsif Nkind (N2) = N_String_Literal then
16233 Rewrite (N, New_Copy (N2));
16234
16235 -- The identifier is rewritten as a dereference if it is the
16236 -- prefix of an implicit dereference. Preserve the original
16237 -- tree as the analysis of the instance will expand the node
16238 -- again, but preserve the resolved entity if it is global.
16239
16240 elsif Nkind (N2) = N_Explicit_Dereference then
16241 if Is_Entity_Name (Prefix (N2))
16242 and then Present (Entity (Prefix (N2)))
16243 and then Is_Global (Entity (Prefix (N2)))
16244 then
16245 Set_Associated_Node (N, Prefix (N2));
16246
16247 elsif Nkind (Prefix (N2)) = N_Function_Call
16248 and then Present (Entity (Name (Prefix (N2))))
16249 and then Is_Global (Entity (Name (Prefix (N2))))
16250 then
16251 Rewrite (N,
16252 Make_Explicit_Dereference (Loc,
16253 Prefix =>
16254 Make_Function_Call (Loc,
16255 Name =>
16256 New_Occurrence_Of
16257 (Entity (Name (Prefix (N2))), Loc))));
16258
16259 else
16260 Set_Associated_Node (N, Empty);
16261 Set_Etype (N, Empty);
16262 end if;
16263
16264 -- The subtype mark of a nominally unconstrained object is
16265 -- rewritten as a subtype indication using the bounds of the
16266 -- expression. Recover the original subtype mark.
16267
16268 elsif Nkind (N2) = N_Subtype_Indication
16269 and then Is_Entity_Name (Original_Node (N2))
16270 then
16271 Set_Associated_Node (N, Original_Node (N2));
16272 Reset_Entity (N);
16273 end if;
16274 end if;
16275 end Save_References_In_Identifier;
16276
16277 ---------------------------------
16278 -- Save_References_In_Operator --
16279 ---------------------------------
16280
16281 procedure Save_References_In_Operator (N : Node_Id) is
16282 begin
16283 -- The node did not undergo a transformation
16284
16285 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
16286 if Nkind (N) = N_Op_Concat then
16287 Set_Is_Component_Left_Opnd (N,
16288 Is_Component_Left_Opnd (Get_Associated_Node (N)));
16289
16290 Set_Is_Component_Right_Opnd (N,
16291 Is_Component_Right_Opnd (Get_Associated_Node (N)));
16292 end if;
16293
16294 Reset_Entity (N);
16295
16296 -- The analysis of the generic copy transformed the operator into
16297 -- some other construct. Propagate the changes to the template if
16298 -- applicable.
16299
16300 else
16301 N2 := Get_Associated_Node (N);
16302
16303 -- The operator resoved to a function call
16304
16305 if Nkind (N2) = N_Function_Call then
16306
16307 -- Add explicit qualifications in the generic template for
16308 -- all operands of universal type. This aids resolution by
16309 -- preserving the actual type of a literal or an attribute
16310 -- that yields a universal result.
16311
16312 Qualify_Universal_Operands (N, N2);
16313
16314 E := Entity (Name (N2));
16315
16316 if Present (E) and then Is_Global (E) then
16317 Set_Etype (N, Etype (N2));
16318 else
16319 Set_Associated_Node (N, Empty);
16320 Set_Etype (N, Empty);
16321 end if;
16322
16323 -- The operator was folded into a literal
16324
16325 elsif Nkind_In (N2, N_Integer_Literal,
16326 N_Real_Literal,
16327 N_String_Literal)
16328 then
16329 if Present (Original_Node (N2))
16330 and then Nkind (Original_Node (N2)) = Nkind (N)
16331 then
16332 -- Operation was constant-folded. Whenever possible,
16333 -- recover semantic information from unfolded node.
16334 -- This was initially done for ASIS but is apparently
16335 -- needed also for e.g. compiling a-nbnbin.adb.
16336
16337 Set_Associated_Node (N, Original_Node (N2));
16338
16339 if Nkind (N) = N_Op_Concat then
16340 Set_Is_Component_Left_Opnd (N,
16341 Is_Component_Left_Opnd (Get_Associated_Node (N)));
16342 Set_Is_Component_Right_Opnd (N,
16343 Is_Component_Right_Opnd (Get_Associated_Node (N)));
16344 end if;
16345
16346 Reset_Entity (N);
16347
16348 -- Propagate the constant folding back to the template
16349
16350 else
16351 Rewrite (N, New_Copy (N2));
16352 Set_Analyzed (N, False);
16353 end if;
16354
16355 -- The operator was folded into an enumeration literal. Retain
16356 -- the entity to avoid spurious ambiguities if it is overloaded
16357 -- at the point of instantiation or inlining.
16358
16359 elsif Nkind (N2) = N_Identifier
16360 and then Ekind (Entity (N2)) = E_Enumeration_Literal
16361 then
16362 Rewrite (N, New_Copy (N2));
16363 Set_Analyzed (N, False);
16364 end if;
16365 end if;
16366
16367 -- Complete the operands check if node has not been constant
16368 -- folded.
16369
16370 if Nkind (N) in N_Op then
16371 Save_Entity_Descendants (N);
16372 end if;
16373 end Save_References_In_Operator;
16374
16375 -------------------------------
16376 -- Save_References_In_Pragma --
16377 -------------------------------
16378
16379 procedure Save_References_In_Pragma (Prag : Node_Id) is
16380 Context : Node_Id;
16381 Do_Save : Boolean := True;
16382
16383 use Atree.Unchecked_Access;
16384 -- This code section is part of implementing an untyped tree
16385 -- traversal, so it needs direct access to node fields.
16386
16387 begin
16388 -- Do not save global references in pragmas generated from aspects
16389 -- because the pragmas will be regenerated at instantiation time.
16390
16391 if From_Aspect_Specification (Prag) then
16392 Do_Save := False;
16393
16394 -- The capture of global references within contract-related source
16395 -- pragmas associated with generic packages, subprograms or their
16396 -- respective bodies must be delayed due to timing of annotation
16397 -- analysis. Global references are still captured in routine
16398 -- Save_Global_References_In_Contract.
16399
16400 elsif Is_Generic_Contract_Pragma (Prag) and then Prag /= Templ then
16401 if Is_Package_Contract_Annotation (Prag) then
16402 Context := Find_Related_Package_Or_Body (Prag);
16403 else
16404 pragma Assert (Is_Subprogram_Contract_Annotation (Prag));
16405 Context := Find_Related_Declaration_Or_Body (Prag);
16406 end if;
16407
16408 -- The use of Original_Node accounts for the case when the
16409 -- related context is generic template.
16410
16411 if Requires_Delayed_Save (Original_Node (Context)) then
16412 Do_Save := False;
16413 end if;
16414 end if;
16415
16416 -- For all other cases, save all global references within the
16417 -- descendants, but skip the following semantic fields:
16418
16419 -- Field1 - Next_Pragma
16420 -- Field3 - Corresponding_Aspect
16421 -- Field5 - Next_Rep_Item
16422
16423 if Do_Save then
16424 Save_Global_Descendant (Field2 (Prag));
16425 Save_Global_Descendant (Field4 (Prag));
16426 end if;
16427 end Save_References_In_Pragma;
16428
16429 -- Start of processing for Save_References
16430
16431 begin
16432 if N = Empty then
16433 null;
16434
16435 -- Aggregates
16436
16437 elsif Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
16438 Save_References_In_Aggregate (N);
16439
16440 -- Character literals, operator symbols
16441
16442 elsif Nkind_In (N, N_Character_Literal, N_Operator_Symbol) then
16443 Save_References_In_Char_Lit_Or_Op_Symbol (N);
16444
16445 -- Defining identifiers
16446
16447 elsif Nkind (N) in N_Entity then
16448 null;
16449
16450 -- Identifiers
16451
16452 elsif Nkind (N) = N_Identifier then
16453 Save_References_In_Identifier (N);
16454
16455 -- Operators
16456
16457 elsif Nkind (N) in N_Op then
16458 Save_References_In_Operator (N);
16459
16460 -- Pragmas
16461
16462 elsif Nkind (N) = N_Pragma then
16463 Save_References_In_Pragma (N);
16464
16465 else
16466 Save_References_In_Descendants (N);
16467 end if;
16468
16469 -- Save all global references found within the aspect specifications
16470 -- of the related node.
16471
16472 if Permits_Aspect_Specifications (N) and then Has_Aspects (N) then
16473
16474 -- The capture of global references within aspects associated with
16475 -- generic packages, subprograms or their bodies must be delayed
16476 -- due to timing of annotation analysis. Global references are
16477 -- still captured in routine Save_Global_References_In_Contract.
16478
16479 if Requires_Delayed_Save (N) then
16480 null;
16481
16482 -- Otherwise save all global references within the aspects
16483
16484 else
16485 Save_Global_References_In_Aspects (N);
16486 end if;
16487 end if;
16488 end Save_References;
16489
16490 -- Start of processing for Save_Global_References
16491
16492 begin
16493 Gen_Scope := Current_Scope;
16494
16495 -- If the generic unit is a child unit, references to entities in the
16496 -- parent are treated as local, because they will be resolved anew in
16497 -- the context of the instance of the parent.
16498
16499 while Is_Child_Unit (Gen_Scope)
16500 and then Ekind (Scope (Gen_Scope)) = E_Generic_Package
16501 loop
16502 Gen_Scope := Scope (Gen_Scope);
16503 end loop;
16504
16505 Save_References (Templ);
16506 end Save_Global_References;
16507
16508 ---------------------------------------
16509 -- Save_Global_References_In_Aspects --
16510 ---------------------------------------
16511
16512 procedure Save_Global_References_In_Aspects (N : Node_Id) is
16513 Asp : Node_Id;
16514 Expr : Node_Id;
16515
16516 begin
16517 Asp := First (Aspect_Specifications (N));
16518 while Present (Asp) loop
16519 Expr := Expression (Asp);
16520
16521 if Present (Expr) then
16522 Save_Global_References (Expr);
16523 end if;
16524
16525 Next (Asp);
16526 end loop;
16527 end Save_Global_References_In_Aspects;
16528
16529 ------------------------------------------
16530 -- Set_Copied_Sloc_For_Inherited_Pragma --
16531 ------------------------------------------
16532
16533 procedure Set_Copied_Sloc_For_Inherited_Pragma
16534 (N : Node_Id;
16535 E : Entity_Id)
16536 is
16537 begin
16538 Create_Instantiation_Source (N, E,
16539 Inlined_Body => False,
16540 Inherited_Pragma => True,
16541 Factor => S_Adjustment);
16542 end Set_Copied_Sloc_For_Inherited_Pragma;
16543
16544 --------------------------------------
16545 -- Set_Copied_Sloc_For_Inlined_Body --
16546 --------------------------------------
16547
16548 procedure Set_Copied_Sloc_For_Inlined_Body (N : Node_Id; E : Entity_Id) is
16549 begin
16550 Create_Instantiation_Source (N, E,
16551 Inlined_Body => True,
16552 Inherited_Pragma => False,
16553 Factor => S_Adjustment);
16554 end Set_Copied_Sloc_For_Inlined_Body;
16555
16556 ---------------------
16557 -- Set_Instance_Of --
16558 ---------------------
16559
16560 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id) is
16561 begin
16562 Generic_Renamings.Table (Generic_Renamings.Last) := (A, B, Assoc_Null);
16563 Generic_Renamings_HTable.Set (Generic_Renamings.Last);
16564 Generic_Renamings.Increment_Last;
16565 end Set_Instance_Of;
16566
16567 --------------------
16568 -- Set_Next_Assoc --
16569 --------------------
16570
16571 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr) is
16572 begin
16573 Generic_Renamings.Table (E).Next_In_HTable := Next;
16574 end Set_Next_Assoc;
16575
16576 -------------------
16577 -- Start_Generic --
16578 -------------------
16579
16580 procedure Start_Generic is
16581 begin
16582 -- ??? More things could be factored out in this routine.
16583 -- Should probably be done at a later stage.
16584
16585 Generic_Flags.Append (Inside_A_Generic);
16586 Inside_A_Generic := True;
16587
16588 Expander_Mode_Save_And_Set (False);
16589 end Start_Generic;
16590
16591 ----------------------
16592 -- Set_Instance_Env --
16593 ----------------------
16594
16595 -- WARNING: This routine manages SPARK regions
16596
16597 procedure Set_Instance_Env
16598 (Gen_Unit : Entity_Id;
16599 Act_Unit : Entity_Id)
16600 is
16601 Saved_AE : constant Boolean := Assertions_Enabled;
16602 Saved_CPL : constant Node_Id := Check_Policy_List;
16603 Saved_DEC : constant Boolean := Dynamic_Elaboration_Checks;
16604 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
16605 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
16606
16607 begin
16608 -- Regardless of the current mode, predefined units are analyzed in the
16609 -- most current Ada mode, and earlier version Ada checks do not apply
16610 -- to predefined units. Nothing needs to be done for non-internal units.
16611 -- These are always analyzed in the current mode.
16612
16613 if In_Internal_Unit (Gen_Unit) then
16614
16615 -- The following call resets all configuration attributes to default
16616 -- or the xxx_Config versions of the attributes when the current sem
16617 -- unit is the main unit. At the same time, internal units must also
16618 -- inherit certain configuration attributes from their context. It
16619 -- is unclear what these two sets are.
16620
16621 Set_Config_Switches (True, Current_Sem_Unit = Main_Unit);
16622
16623 -- Reinstall relevant configuration attributes of the context
16624
16625 Assertions_Enabled := Saved_AE;
16626 Check_Policy_List := Saved_CPL;
16627 Dynamic_Elaboration_Checks := Saved_DEC;
16628
16629 Install_SPARK_Mode (Saved_SM, Saved_SMP);
16630 end if;
16631
16632 Current_Instantiated_Parent :=
16633 (Gen_Id => Gen_Unit,
16634 Act_Id => Act_Unit,
16635 Next_In_HTable => Assoc_Null);
16636 end Set_Instance_Env;
16637
16638 -----------------
16639 -- Switch_View --
16640 -----------------
16641
16642 procedure Switch_View (T : Entity_Id) is
16643 BT : constant Entity_Id := Base_Type (T);
16644 Priv_Elmt : Elmt_Id := No_Elmt;
16645 Priv_Sub : Entity_Id;
16646
16647 begin
16648 -- T may be private but its base type may have been exchanged through
16649 -- some other occurrence, in which case there is nothing to switch
16650 -- besides T itself. Note that a private dependent subtype of a private
16651 -- type might not have been switched even if the base type has been,
16652 -- because of the last branch of Check_Private_View (see comment there).
16653
16654 if not Is_Private_Type (BT) then
16655 Prepend_Elmt (Full_View (T), Exchanged_Views);
16656 Exchange_Declarations (T);
16657 return;
16658 end if;
16659
16660 Priv_Elmt := First_Elmt (Private_Dependents (BT));
16661
16662 if Present (Full_View (BT)) then
16663 Prepend_Elmt (Full_View (BT), Exchanged_Views);
16664 Exchange_Declarations (BT);
16665 end if;
16666
16667 while Present (Priv_Elmt) loop
16668 Priv_Sub := (Node (Priv_Elmt));
16669
16670 -- We avoid flipping the subtype if the Etype of its full view is
16671 -- private because this would result in a malformed subtype. This
16672 -- occurs when the Etype of the subtype full view is the full view of
16673 -- the base type (and since the base types were just switched, the
16674 -- subtype is pointing to the wrong view). This is currently the case
16675 -- for tagged record types, access types (maybe more?) and needs to
16676 -- be resolved. ???
16677
16678 if Present (Full_View (Priv_Sub))
16679 and then not Is_Private_Type (Etype (Full_View (Priv_Sub)))
16680 then
16681 Prepend_Elmt (Full_View (Priv_Sub), Exchanged_Views);
16682 Exchange_Declarations (Priv_Sub);
16683 end if;
16684
16685 Next_Elmt (Priv_Elmt);
16686 end loop;
16687 end Switch_View;
16688
16689 -----------------
16690 -- True_Parent --
16691 -----------------
16692
16693 function True_Parent (N : Node_Id) return Node_Id is
16694 begin
16695 if Nkind (Parent (N)) = N_Subunit then
16696 return Parent (Corresponding_Stub (Parent (N)));
16697 else
16698 return Parent (N);
16699 end if;
16700 end True_Parent;
16701
16702 -----------------------------
16703 -- Valid_Default_Attribute --
16704 -----------------------------
16705
16706 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id) is
16707 Attr_Id : constant Attribute_Id :=
16708 Get_Attribute_Id (Attribute_Name (Def));
16709 T : constant Entity_Id := Entity (Prefix (Def));
16710 Is_Fun : constant Boolean := (Ekind (Nam) = E_Function);
16711 F : Entity_Id;
16712 Num_F : Nat;
16713 OK : Boolean;
16714
16715 begin
16716 if No (T) or else T = Any_Id then
16717 return;
16718 end if;
16719
16720 Num_F := 0;
16721 F := First_Formal (Nam);
16722 while Present (F) loop
16723 Num_F := Num_F + 1;
16724 Next_Formal (F);
16725 end loop;
16726
16727 case Attr_Id is
16728 when Attribute_Adjacent
16729 | Attribute_Ceiling
16730 | Attribute_Copy_Sign
16731 | Attribute_Floor
16732 | Attribute_Fraction
16733 | Attribute_Machine
16734 | Attribute_Model
16735 | Attribute_Remainder
16736 | Attribute_Rounding
16737 | Attribute_Unbiased_Rounding
16738 =>
16739 OK := Is_Fun
16740 and then Num_F = 1
16741 and then Is_Floating_Point_Type (T);
16742
16743 when Attribute_Image
16744 | Attribute_Pred
16745 | Attribute_Succ
16746 | Attribute_Value
16747 | Attribute_Wide_Image
16748 | Attribute_Wide_Value
16749 =>
16750 OK := Is_Fun and then Num_F = 1 and then Is_Scalar_Type (T);
16751
16752 when Attribute_Max
16753 | Attribute_Min
16754 =>
16755 OK := Is_Fun and then Num_F = 2 and then Is_Scalar_Type (T);
16756
16757 when Attribute_Input =>
16758 OK := (Is_Fun and then Num_F = 1);
16759
16760 when Attribute_Output
16761 | Attribute_Put_Image
16762 | Attribute_Read
16763 | Attribute_Write
16764 =>
16765 OK := not Is_Fun and then Num_F = 2;
16766
16767 when others =>
16768 OK := False;
16769 end case;
16770
16771 if not OK then
16772 Error_Msg_N
16773 ("attribute reference has wrong profile for subprogram", Def);
16774 end if;
16775 end Valid_Default_Attribute;
16776
16777 end Sem_Ch12;