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