3635319884b8def8eb69eb94f90ae7bcb9bb4a18
[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-2017, 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 Exp_Disp; use Exp_Disp;
34 with Fname; use Fname;
35 with Fname.UF; use Fname.UF;
36 with Freeze; use Freeze;
37 with Ghost; use Ghost;
38 with Itypes; use Itypes;
39 with Lib; use Lib;
40 with Lib.Load; use Lib.Load;
41 with Lib.Xref; use Lib.Xref;
42 with Nlists; use Nlists;
43 with Namet; use Namet;
44 with Nmake; use Nmake;
45 with Opt; use Opt;
46 with Rident; use Rident;
47 with Restrict; use Restrict;
48 with Rtsfind; use Rtsfind;
49 with Sem; use Sem;
50 with Sem_Aux; use Sem_Aux;
51 with Sem_Cat; use Sem_Cat;
52 with Sem_Ch3; use Sem_Ch3;
53 with Sem_Ch6; use Sem_Ch6;
54 with Sem_Ch7; use Sem_Ch7;
55 with Sem_Ch8; use Sem_Ch8;
56 with Sem_Ch10; use Sem_Ch10;
57 with Sem_Ch13; use Sem_Ch13;
58 with Sem_Dim; use Sem_Dim;
59 with Sem_Disp; use Sem_Disp;
60 with Sem_Elab; use Sem_Elab;
61 with Sem_Elim; use Sem_Elim;
62 with Sem_Eval; use Sem_Eval;
63 with Sem_Prag; use Sem_Prag;
64 with Sem_Res; use Sem_Res;
65 with Sem_Type; use Sem_Type;
66 with Sem_Util; use Sem_Util;
67 with Sem_Warn; use Sem_Warn;
68 with Stand; use Stand;
69 with Sinfo; use Sinfo;
70 with Sinfo.CN; use Sinfo.CN;
71 with Sinput; use Sinput;
72 with Sinput.L; use Sinput.L;
73 with Snames; use Snames;
74 with Stringt; use Stringt;
75 with Uname; use Uname;
76 with Table;
77 with Tbuild; use Tbuild;
78 with Uintp; use Uintp;
79 with Urealp; use Urealp;
80 with Warnsw; use Warnsw;
81
82 with GNAT.HTable;
83
84 package body Sem_Ch12 is
85
86 ----------------------------------------------------------
87 -- Implementation of Generic Analysis and Instantiation --
88 ----------------------------------------------------------
89
90 -- GNAT implements generics by macro expansion. No attempt is made to share
91 -- generic instantiations (for now). Analysis of a generic definition does
92 -- not perform any expansion action, but the expander must be called on the
93 -- tree for each instantiation, because the expansion may of course depend
94 -- on the generic actuals. All of this is best achieved as follows:
95 --
96 -- a) Semantic analysis of a generic unit is performed on a copy of the
97 -- tree for the generic unit. All tree modifications that follow analysis
98 -- do not affect the original tree. Links are kept between the original
99 -- tree and the copy, in order to recognize non-local references within
100 -- the generic, and propagate them to each instance (recall that name
101 -- resolution is done on the generic declaration: generics are not really
102 -- macros). This is summarized in the following diagram:
103
104 -- .-----------. .----------.
105 -- | semantic |<--------------| generic |
106 -- | copy | | unit |
107 -- | |==============>| |
108 -- |___________| global |__________|
109 -- references | | |
110 -- | | |
111 -- .-----|--|.
112 -- | .-----|---.
113 -- | | .----------.
114 -- | | | generic |
115 -- |__| | |
116 -- |__| instance |
117 -- |__________|
118
119 -- b) Each instantiation copies the original tree, and inserts into it a
120 -- series of declarations that describe the mapping between generic formals
121 -- and actuals. For example, a generic In OUT parameter is an object
122 -- renaming of the corresponding actual, etc. Generic IN parameters are
123 -- constant declarations.
124
125 -- c) In order to give the right visibility for these renamings, we use
126 -- a different scheme for package and subprogram instantiations. For
127 -- packages, the list of renamings is inserted into the package
128 -- specification, before the visible declarations of the package. The
129 -- renamings are analyzed before any of the text of the instance, and are
130 -- thus visible at the right place. Furthermore, outside of the instance,
131 -- the generic parameters are visible and denote their corresponding
132 -- actuals.
133
134 -- For subprograms, we create a container package to hold the renamings
135 -- and the subprogram instance itself. Analysis of the package makes the
136 -- renaming declarations visible to the subprogram. After analyzing the
137 -- package, the defining entity for the subprogram is touched-up so that
138 -- it appears declared in the current scope, and not inside the container
139 -- package.
140
141 -- If the instantiation is a compilation unit, the container package is
142 -- given the same name as the subprogram instance. This ensures that
143 -- the elaboration procedure called by the binder, using the compilation
144 -- unit name, calls in fact the elaboration procedure for the package.
145
146 -- Not surprisingly, private types complicate this approach. By saving in
147 -- the original generic object the non-local references, we guarantee that
148 -- the proper entities are referenced at the point of instantiation.
149 -- However, for private types, this by itself does not insure that the
150 -- proper VIEW of the entity is used (the full type may be visible at the
151 -- point of generic definition, but not at instantiation, or vice-versa).
152 -- In order to reference the proper view, we special-case any reference
153 -- to private types in the generic object, by saving both views, one in
154 -- the generic and one in the semantic copy. At time of instantiation, we
155 -- check whether the two views are consistent, and exchange declarations if
156 -- necessary, in order to restore the correct visibility. Similarly, if
157 -- the instance view is private when the generic view was not, we perform
158 -- the exchange. After completing the instantiation, we restore the
159 -- current visibility. The flag Has_Private_View marks identifiers in the
160 -- the generic unit that require checking.
161
162 -- Visibility within nested generic units requires special handling.
163 -- Consider the following scheme:
164
165 -- type Global is ... -- outside of generic unit.
166 -- generic ...
167 -- package Outer is
168 -- ...
169 -- type Semi_Global is ... -- global to inner.
170
171 -- generic ... -- 1
172 -- procedure inner (X1 : Global; X2 : Semi_Global);
173
174 -- procedure in2 is new inner (...); -- 4
175 -- end Outer;
176
177 -- package New_Outer is new Outer (...); -- 2
178 -- procedure New_Inner is new New_Outer.Inner (...); -- 3
179
180 -- The semantic analysis of Outer captures all occurrences of Global.
181 -- The semantic analysis of Inner (at 1) captures both occurrences of
182 -- Global and Semi_Global.
183
184 -- At point 2 (instantiation of Outer), we also produce a generic copy
185 -- of Inner, even though Inner is, at that point, not being instantiated.
186 -- (This is just part of the semantic analysis of New_Outer).
187
188 -- Critically, references to Global within Inner must be preserved, while
189 -- references to Semi_Global should not preserved, because they must now
190 -- resolve to an entity within New_Outer. To distinguish between these, we
191 -- use a global variable, Current_Instantiated_Parent, which is set when
192 -- performing a generic copy during instantiation (at 2). This variable is
193 -- used when performing a generic copy that is not an instantiation, but
194 -- that is nested within one, as the occurrence of 1 within 2. The analysis
195 -- of a nested generic only preserves references that are global to the
196 -- enclosing Current_Instantiated_Parent. We use the Scope_Depth value to
197 -- determine whether a reference is external to the given parent.
198
199 -- The instantiation at point 3 requires no special treatment. The method
200 -- works as well for further nestings of generic units, but of course the
201 -- variable Current_Instantiated_Parent must be stacked because nested
202 -- instantiations can occur, e.g. the occurrence of 4 within 2.
203
204 -- The instantiation of package and subprogram bodies is handled in a
205 -- similar manner, except that it is delayed until after semantic
206 -- analysis is complete. In this fashion complex cross-dependencies
207 -- between several package declarations and bodies containing generics
208 -- can be compiled which otherwise would diagnose spurious circularities.
209
210 -- For example, it is possible to compile two packages A and B that
211 -- have the following structure:
212
213 -- package A is package B is
214 -- generic ... generic ...
215 -- package G_A is package G_B is
216
217 -- with B; with A;
218 -- package body A is package body B is
219 -- package N_B is new G_B (..) package N_A is new G_A (..)
220
221 -- The table Pending_Instantiations in package Inline is used to keep
222 -- track of body instantiations that are delayed in this manner. Inline
223 -- handles the actual calls to do the body instantiations. This activity
224 -- is part of Inline, since the processing occurs at the same point, and
225 -- for essentially the same reason, as the handling of inlined routines.
226
227 ----------------------------------------------
228 -- Detection of Instantiation Circularities --
229 ----------------------------------------------
230
231 -- If we have a chain of instantiations that is circular, this is static
232 -- error which must be detected at compile time. The detection of these
233 -- circularities is carried out at the point that we insert a generic
234 -- instance spec or body. If there is a circularity, then the analysis of
235 -- the offending spec or body will eventually result in trying to load the
236 -- same unit again, and we detect this problem as we analyze the package
237 -- instantiation for the second time.
238
239 -- At least in some cases after we have detected the circularity, we get
240 -- into trouble if we try to keep going. The following flag is set if a
241 -- circularity is detected, and used to abandon compilation after the
242 -- messages have been posted.
243
244 -----------------------------------------
245 -- Implementation of Generic Contracts --
246 -----------------------------------------
247
248 -- A "contract" is a collection of aspects and pragmas that either verify a
249 -- property of a construct at runtime or classify the data flow to and from
250 -- the construct in some fashion.
251
252 -- Generic packages, subprograms and their respective bodies may be subject
253 -- to the following contract-related aspects or pragmas collectively known
254 -- as annotations:
255
256 -- package subprogram [body]
257 -- Abstract_State Contract_Cases
258 -- Initial_Condition Depends
259 -- Initializes Extensions_Visible
260 -- Global
261 -- package body Post
262 -- Refined_State Post_Class
263 -- Postcondition
264 -- Pre
265 -- Pre_Class
266 -- Precondition
267 -- Refined_Depends
268 -- Refined_Global
269 -- Refined_Post
270 -- Test_Case
271
272 -- Most package contract annotations utilize forward references to classify
273 -- data declared within the package [body]. Subprogram annotations then use
274 -- the classifications to further refine them. These inter dependencies are
275 -- problematic with respect to the implementation of generics because their
276 -- analysis, capture of global references and instantiation does not mesh
277 -- well with the existing mechanism.
278
279 -- 1) Analysis of generic contracts is carried out the same way non-generic
280 -- contracts are analyzed:
281
282 -- 1.1) General rule - a contract is analyzed after all related aspects
283 -- and pragmas are analyzed. This is done by routines
284
285 -- Analyze_Package_Body_Contract
286 -- Analyze_Package_Contract
287 -- Analyze_Subprogram_Body_Contract
288 -- Analyze_Subprogram_Contract
289
290 -- 1.2) Compilation unit - the contract is analyzed after Pragmas_After
291 -- are processed.
292
293 -- 1.3) Compilation unit body - the contract is analyzed at the end of
294 -- the body declaration list.
295
296 -- 1.4) Package - the contract is analyzed at the end of the private or
297 -- visible declarations, prior to analyzing the contracts of any nested
298 -- packages or subprograms.
299
300 -- 1.5) Package body - the contract is analyzed at the end of the body
301 -- declaration list, prior to analyzing the contracts of any nested
302 -- packages or subprograms.
303
304 -- 1.6) Subprogram - if the subprogram is declared inside a block, a
305 -- package or a subprogram, then its contract is analyzed at the end of
306 -- the enclosing declarations, otherwise the subprogram is a compilation
307 -- unit 1.2).
308
309 -- 1.7) Subprogram body - if the subprogram body is declared inside a
310 -- block, a package body or a subprogram body, then its contract is
311 -- analyzed at the end of the enclosing declarations, otherwise the
312 -- subprogram is a compilation unit 1.3).
313
314 -- 2) Capture of global references within contracts is done after capturing
315 -- global references within the generic template. There are two reasons for
316 -- this delay - pragma annotations are not part of the generic template in
317 -- the case of a generic subprogram declaration, and analysis of contracts
318 -- is delayed.
319
320 -- Contract-related source pragmas within generic templates are prepared
321 -- for delayed capture of global references by routine
322
323 -- Create_Generic_Contract
324
325 -- The routine associates these pragmas with the contract of the template.
326 -- In the case of a generic subprogram declaration, the routine creates
327 -- generic templates for the pragmas declared after the subprogram because
328 -- they are not part of the template.
329
330 -- generic -- template starts
331 -- procedure Gen_Proc (Input : Integer); -- template ends
332 -- pragma Precondition (Input > 0); -- requires own template
333
334 -- 2.1) The capture of global references with aspect specifications and
335 -- source pragmas that apply to a generic unit must be suppressed when
336 -- the generic template is being processed because the contracts have not
337 -- been analyzed yet. Any attempts to capture global references at that
338 -- point will destroy the Associated_Node linkages and leave the template
339 -- undecorated. This delay is controlled by routine
340
341 -- Requires_Delayed_Save
342
343 -- 2.2) The real capture of global references within a contract is done
344 -- after the contract has been analyzed, by routine
345
346 -- Save_Global_References_In_Contract
347
348 -- 3) The instantiation of a generic contract occurs as part of the
349 -- instantiation of the contract owner. Generic subprogram declarations
350 -- require additional processing when the contract is specified by pragmas
351 -- because the pragmas are not part of the generic template. This is done
352 -- by routine
353
354 -- Instantiate_Subprogram_Contract
355
356 Circularity_Detected : Boolean := False;
357 -- This should really be reset on encountering a new main unit, but in
358 -- practice we are not using multiple main units so it is not critical.
359
360 --------------------------------------------------
361 -- Formal packages and partial parameterization --
362 --------------------------------------------------
363
364 -- When compiling a generic, a formal package is a local instantiation. If
365 -- declared with a box, its generic formals are visible in the enclosing
366 -- generic. If declared with a partial list of actuals, those actuals that
367 -- are defaulted (covered by an Others clause, or given an explicit box
368 -- initialization) are also visible in the enclosing generic, while those
369 -- that have a corresponding actual are not.
370
371 -- In our source model of instantiation, the same visibility must be
372 -- present in the spec and body of an instance: the names of the formals
373 -- that are defaulted must be made visible within the instance, and made
374 -- invisible (hidden) after the instantiation is complete, so that they
375 -- are not accessible outside of the instance.
376
377 -- In a generic, a formal package is treated like a special instantiation.
378 -- Our Ada 95 compiler handled formals with and without box in different
379 -- ways. With partial parameterization, we use a single model for both.
380 -- We create a package declaration that consists of the specification of
381 -- the generic package, and a set of declarations that map the actuals
382 -- into local renamings, just as we do for bona fide instantiations. For
383 -- defaulted parameters and formals with a box, we copy directly the
384 -- declarations of the formal into this local package. The result is a
385 -- a package whose visible declarations may include generic formals. This
386 -- package is only used for type checking and visibility analysis, and
387 -- never reaches the back-end, so it can freely violate the placement
388 -- rules for generic formal declarations.
389
390 -- The list of declarations (renamings and copies of formals) is built
391 -- by Analyze_Associations, just as for regular instantiations.
392
393 -- At the point of instantiation, conformance checking must be applied only
394 -- to those parameters that were specified in the formal. We perform this
395 -- checking by creating another internal instantiation, this one including
396 -- only the renamings and the formals (the rest of the package spec is not
397 -- relevant to conformance checking). We can then traverse two lists: the
398 -- list of actuals in the instance that corresponds to the formal package,
399 -- and the list of actuals produced for this bogus instantiation. We apply
400 -- the conformance rules to those actuals that are not defaulted (i.e.
401 -- which still appear as generic formals.
402
403 -- When we compile an instance body we must make the right parameters
404 -- visible again. The predicate Is_Generic_Formal indicates which of the
405 -- formals should have its Is_Hidden flag reset.
406
407 -----------------------
408 -- Local subprograms --
409 -----------------------
410
411 procedure Abandon_Instantiation (N : Node_Id);
412 pragma No_Return (Abandon_Instantiation);
413 -- Posts an error message "instantiation abandoned" at the indicated node
414 -- and then raises the exception Instantiation_Error to do it.
415
416 procedure Analyze_Formal_Array_Type
417 (T : in out Entity_Id;
418 Def : Node_Id);
419 -- A formal array type is treated like an array type declaration, and
420 -- invokes Array_Type_Declaration (sem_ch3) whose first parameter is
421 -- in-out, because in the case of an anonymous type the entity is
422 -- actually created in the procedure.
423
424 -- The following procedures treat other kinds of formal parameters
425
426 procedure Analyze_Formal_Derived_Interface_Type
427 (N : Node_Id;
428 T : Entity_Id;
429 Def : Node_Id);
430
431 procedure Analyze_Formal_Derived_Type
432 (N : Node_Id;
433 T : Entity_Id;
434 Def : Node_Id);
435
436 procedure Analyze_Formal_Interface_Type
437 (N : Node_Id;
438 T : Entity_Id;
439 Def : Node_Id);
440
441 -- The following subprograms create abbreviated declarations for formal
442 -- scalar types. We introduce an anonymous base of the proper class for
443 -- each of them, and define the formals as constrained first subtypes of
444 -- their bases. The bounds are expressions that are non-static in the
445 -- generic.
446
447 procedure Analyze_Formal_Decimal_Fixed_Point_Type
448 (T : Entity_Id; Def : Node_Id);
449 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id);
450 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id);
451 procedure Analyze_Formal_Signed_Integer_Type (T : Entity_Id; Def : Node_Id);
452 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id);
453 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
454 (T : Entity_Id; Def : Node_Id);
455
456 procedure Analyze_Formal_Private_Type
457 (N : Node_Id;
458 T : Entity_Id;
459 Def : Node_Id);
460 -- Creates a new private type, which does not require completion
461
462 procedure Analyze_Formal_Incomplete_Type (T : Entity_Id; Def : Node_Id);
463 -- Ada 2012: Creates a new incomplete type whose actual does not freeze
464
465 procedure Analyze_Generic_Formal_Part (N : Node_Id);
466 -- Analyze generic formal part
467
468 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id);
469 -- Create a new access type with the given designated type
470
471 function Analyze_Associations
472 (I_Node : Node_Id;
473 Formals : List_Id;
474 F_Copy : List_Id) return List_Id;
475 -- At instantiation time, build the list of associations between formals
476 -- and actuals. Each association becomes a renaming declaration for the
477 -- formal entity. F_Copy is the analyzed list of formals in the generic
478 -- copy. It is used to apply legality checks to the actuals. I_Node is the
479 -- instantiation node itself.
480
481 procedure Analyze_Subprogram_Instantiation
482 (N : Node_Id;
483 K : Entity_Kind);
484
485 procedure Build_Instance_Compilation_Unit_Nodes
486 (N : Node_Id;
487 Act_Body : Node_Id;
488 Act_Decl : Node_Id);
489 -- This procedure is used in the case where the generic instance of a
490 -- subprogram body or package body is a library unit. In this case, the
491 -- original library unit node for the generic instantiation must be
492 -- replaced by the resulting generic body, and a link made to a new
493 -- compilation unit node for the generic declaration. The argument N is
494 -- the original generic instantiation. Act_Body and Act_Decl are the body
495 -- and declaration of the instance (either package body and declaration
496 -- nodes or subprogram body and declaration nodes depending on the case).
497 -- On return, the node N has been rewritten with the actual body.
498
499 procedure Check_Access_Definition (N : Node_Id);
500 -- Subsidiary routine to null exclusion processing. Perform an assertion
501 -- check on Ada version and the presence of an access definition in N.
502
503 procedure Check_Formal_Packages (P_Id : Entity_Id);
504 -- Apply the following to all formal packages in generic associations
505
506 procedure Check_Formal_Package_Instance
507 (Formal_Pack : Entity_Id;
508 Actual_Pack : Entity_Id);
509 -- Verify that the actuals of the actual instance match the actuals of
510 -- the template for a formal package that is not declared with a box.
511
512 procedure Check_Forward_Instantiation (Decl : Node_Id);
513 -- If the generic is a local entity and the corresponding body has not
514 -- been seen yet, flag enclosing packages to indicate that it will be
515 -- elaborated after the generic body. Subprograms declared in the same
516 -- package cannot be inlined by the front end because front-end inlining
517 -- requires a strict linear order of elaboration.
518
519 function Check_Hidden_Primitives (Assoc_List : List_Id) return Elist_Id;
520 -- Check if some association between formals and actuals requires to make
521 -- visible primitives of a tagged type, and make those primitives visible.
522 -- Return the list of primitives whose visibility is modified (to restore
523 -- their visibility later through Restore_Hidden_Primitives). If no
524 -- candidate is found then return No_Elist.
525
526 procedure Check_Hidden_Child_Unit
527 (N : Node_Id;
528 Gen_Unit : Entity_Id;
529 Act_Decl_Id : Entity_Id);
530 -- If the generic unit is an implicit child instance within a parent
531 -- instance, we need to make an explicit test that it is not hidden by
532 -- a child instance of the same name and parent.
533
534 procedure Check_Generic_Actuals
535 (Instance : Entity_Id;
536 Is_Formal_Box : Boolean);
537 -- Similar to previous one. Check the actuals in the instantiation,
538 -- whose views can change between the point of instantiation and the point
539 -- of instantiation of the body. In addition, mark the generic renamings
540 -- as generic actuals, so that they are not compatible with other actuals.
541 -- Recurse on an actual that is a formal package whose declaration has
542 -- a box.
543
544 function Contains_Instance_Of
545 (Inner : Entity_Id;
546 Outer : Entity_Id;
547 N : Node_Id) return Boolean;
548 -- Inner is instantiated within the generic Outer. Check whether Inner
549 -- directly or indirectly contains an instance of Outer or of one of its
550 -- parents, in the case of a subunit. Each generic unit holds a list of
551 -- the entities instantiated within (at any depth). This procedure
552 -- determines whether the set of such lists contains a cycle, i.e. an
553 -- illegal circular instantiation.
554
555 function Denotes_Formal_Package
556 (Pack : Entity_Id;
557 On_Exit : Boolean := False;
558 Instance : Entity_Id := Empty) return Boolean;
559 -- Returns True if E is a formal package of an enclosing generic, or
560 -- the actual for such a formal in an enclosing instantiation. If such
561 -- a package is used as a formal in an nested generic, or as an actual
562 -- in a nested instantiation, the visibility of ITS formals should not
563 -- be modified. When called from within Restore_Private_Views, the flag
564 -- On_Exit is true, to indicate that the search for a possible enclosing
565 -- instance should ignore the current one. In that case Instance denotes
566 -- the declaration for which this is an actual. This declaration may be
567 -- an instantiation in the source, or the internal instantiation that
568 -- corresponds to the actual for a formal package.
569
570 function Earlier (N1, N2 : Node_Id) return Boolean;
571 -- Yields True if N1 and N2 appear in the same compilation unit,
572 -- ignoring subunits, and if N1 is to the left of N2 in a left-to-right
573 -- traversal of the tree for the unit. Used to determine the placement
574 -- of freeze nodes for instance bodies that may depend on other instances.
575
576 function Find_Actual_Type
577 (Typ : Entity_Id;
578 Gen_Type : Entity_Id) return Entity_Id;
579 -- When validating the actual types of a child instance, check whether
580 -- the formal is a formal type of the parent unit, and retrieve the current
581 -- actual for it. Typ is the entity in the analyzed formal type declaration
582 -- (component or index type of an array type, or designated type of an
583 -- access formal) and Gen_Type is the enclosing analyzed formal array
584 -- or access type. The desired actual may be a formal of a parent, or may
585 -- be declared in a formal package of a parent. In both cases it is a
586 -- generic actual type because it appears within a visible instance.
587 -- Finally, it may be declared in a parent unit without being a formal
588 -- of that unit, in which case it must be retrieved by visibility.
589 -- Ambiguities may still arise if two homonyms are declared in two formal
590 -- packages, and the prefix of the formal type may be needed to resolve
591 -- the ambiguity in the instance ???
592
593 procedure Freeze_Subprogram_Body
594 (Inst_Node : Node_Id;
595 Gen_Body : Node_Id;
596 Pack_Id : Entity_Id);
597 -- The generic body may appear textually after the instance, including
598 -- in the proper body of a stub, or within a different package instance.
599 -- Given that the instance can only be elaborated after the generic, we
600 -- place freeze_nodes for the instance and/or for packages that may enclose
601 -- the instance and the generic, so that the back-end can establish the
602 -- proper order of elaboration.
603
604 function Get_Associated_Node (N : Node_Id) return Node_Id;
605 -- In order to propagate semantic information back from the analyzed copy
606 -- to the original generic, we maintain links between selected nodes in the
607 -- generic and their corresponding copies. At the end of generic analysis,
608 -- the routine Save_Global_References traverses the generic tree, examines
609 -- the semantic information, and preserves the links to those nodes that
610 -- contain global information. At instantiation, the information from the
611 -- associated node is placed on the new copy, so that name resolution is
612 -- not repeated.
613 --
614 -- Three kinds of source nodes have associated nodes:
615 --
616 -- a) those that can reference (denote) entities, that is identifiers,
617 -- character literals, expanded_names, operator symbols, operators,
618 -- and attribute reference nodes. These nodes have an Entity field
619 -- and are the set of nodes that are in N_Has_Entity.
620 --
621 -- b) aggregates (N_Aggregate and N_Extension_Aggregate)
622 --
623 -- c) selected components (N_Selected_Component)
624 --
625 -- For the first class, the associated node preserves the entity if it is
626 -- global. If the generic contains nested instantiations, the associated
627 -- node itself has been recopied, and a chain of them must be followed.
628 --
629 -- For aggregates, the associated node allows retrieval of the type, which
630 -- may otherwise not appear in the generic. The view of this type may be
631 -- different between generic and instantiation, and the full view can be
632 -- installed before the instantiation is analyzed. For aggregates of type
633 -- extensions, the same view exchange may have to be performed for some of
634 -- the ancestor types, if their view is private at the point of
635 -- instantiation.
636 --
637 -- Nodes that are selected components in the parse tree may be rewritten
638 -- as expanded names after resolution, and must be treated as potential
639 -- entity holders, which is why they also have an Associated_Node.
640 --
641 -- Nodes that do not come from source, such as freeze nodes, do not appear
642 -- in the generic tree, and need not have an associated node.
643 --
644 -- The associated node is stored in the Associated_Node field. Note that
645 -- this field overlaps Entity, which is fine, because the whole point is
646 -- that we don't need or want the normal Entity field in this situation.
647
648 function Has_Been_Exchanged (E : Entity_Id) return Boolean;
649 -- Traverse the Exchanged_Views list to see if a type was private
650 -- and has already been flipped during this phase of instantiation.
651
652 procedure Hide_Current_Scope;
653 -- When instantiating a generic child unit, the parent context must be
654 -- present, but the instance and all entities that may be generated
655 -- must be inserted in the current scope. We leave the current scope
656 -- on the stack, but make its entities invisible to avoid visibility
657 -- problems. This is reversed at the end of the instantiation. This is
658 -- not done for the instantiation of the bodies, which only require the
659 -- instances of the generic parents to be in scope.
660
661 function In_Same_Declarative_Part
662 (F_Node : Node_Id;
663 Inst : Node_Id) return Boolean;
664 -- True if the instantiation Inst and the given freeze_node F_Node appear
665 -- within the same declarative part, ignoring subunits, but with no inter-
666 -- vening subprograms or concurrent units. Used to find the proper plave
667 -- for the freeze node of an instance, when the generic is declared in a
668 -- previous instance. If predicate is true, the freeze node of the instance
669 -- can be placed after the freeze node of the previous instance, Otherwise
670 -- it has to be placed at the end of the current declarative part.
671
672 function In_Main_Context (E : Entity_Id) return Boolean;
673 -- Check whether an instantiation is in the context of the main unit.
674 -- Used to determine whether its body should be elaborated to allow
675 -- front-end inlining.
676
677 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id);
678 -- Add the context clause of the unit containing a generic unit to a
679 -- compilation unit that is, or contains, an instantiation.
680
681 procedure Init_Env;
682 -- Establish environment for subsequent instantiation. Separated from
683 -- Save_Env because data-structures for visibility handling must be
684 -- initialized before call to Check_Generic_Child_Unit.
685
686 procedure Inline_Instance_Body
687 (N : Node_Id;
688 Gen_Unit : Entity_Id;
689 Act_Decl : Node_Id);
690 -- If front-end inlining is requested, instantiate the package body,
691 -- and preserve the visibility of its compilation unit, to insure
692 -- that successive instantiations succeed.
693
694 procedure Insert_Freeze_Node_For_Instance
695 (N : Node_Id;
696 F_Node : Node_Id);
697 -- N denotes a package or a subprogram instantiation and F_Node is the
698 -- associated freeze node. Insert the freeze node before the first source
699 -- body which follows immediately after N. If no such body is found, the
700 -- freeze node is inserted at the end of the declarative region which
701 -- contains N.
702
703 procedure Install_Body
704 (Act_Body : Node_Id;
705 N : Node_Id;
706 Gen_Body : Node_Id;
707 Gen_Decl : Node_Id);
708 -- If the instantiation happens textually before the body of the generic,
709 -- the instantiation of the body must be analyzed after the generic body,
710 -- and not at the point of instantiation. Such early instantiations can
711 -- happen if the generic and the instance appear in a package declaration
712 -- because the generic body can only appear in the corresponding package
713 -- body. Early instantiations can also appear if generic, instance and
714 -- body are all in the declarative part of a subprogram or entry. Entities
715 -- of packages that are early instantiations are delayed, and their freeze
716 -- node appears after the generic body. This rather complex machinery is
717 -- needed when nested instantiations are present, because the source does
718 -- not carry any indication of where the corresponding instance bodies must
719 -- be installed and frozen.
720
721 procedure Install_Formal_Packages (Par : Entity_Id);
722 -- Install the visible part of any formal of the parent that is a formal
723 -- package. Note that for the case of a formal package with a box, this
724 -- includes the formal part of the formal package (12.7(10/2)).
725
726 procedure Install_Hidden_Primitives
727 (Prims_List : in out Elist_Id;
728 Gen_T : Entity_Id;
729 Act_T : Entity_Id);
730 -- Remove suffix 'P' from hidden primitives of Act_T to match the
731 -- visibility of primitives of Gen_T. The list of primitives to which
732 -- the suffix is removed is added to Prims_List to restore them later.
733
734 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False);
735 -- When compiling an instance of a child unit the parent (which is
736 -- itself an instance) is an enclosing scope that must be made
737 -- immediately visible. This procedure is also used to install the non-
738 -- generic parent of a generic child unit when compiling its body, so
739 -- that full views of types in the parent are made visible.
740
741 -- The functions Instantiate_XXX perform various legality checks and build
742 -- the declarations for instantiated generic parameters. In all of these
743 -- Formal is the entity in the generic unit, Actual is the entity of
744 -- expression in the generic associations, and Analyzed_Formal is the
745 -- formal in the generic copy, which contains the semantic information to
746 -- be used to validate the actual.
747
748 function Instantiate_Object
749 (Formal : Node_Id;
750 Actual : Node_Id;
751 Analyzed_Formal : Node_Id) return List_Id;
752
753 function Instantiate_Type
754 (Formal : Node_Id;
755 Actual : Node_Id;
756 Analyzed_Formal : Node_Id;
757 Actual_Decls : List_Id) return List_Id;
758
759 function Instantiate_Formal_Subprogram
760 (Formal : Node_Id;
761 Actual : Node_Id;
762 Analyzed_Formal : Node_Id) return Node_Id;
763
764 function Instantiate_Formal_Package
765 (Formal : Node_Id;
766 Actual : Node_Id;
767 Analyzed_Formal : Node_Id) return List_Id;
768 -- If the formal package is declared with a box, special visibility rules
769 -- apply to its formals: they are in the visible part of the package. This
770 -- is true in the declarative region of the formal package, that is to say
771 -- in the enclosing generic or instantiation. For an instantiation, the
772 -- parameters of the formal package are made visible in an explicit step.
773 -- Furthermore, if the actual has a visible USE clause, these formals must
774 -- be made potentially use-visible as well. On exit from the enclosing
775 -- instantiation, the reverse must be done.
776
777 -- For a formal package declared without a box, there are conformance rules
778 -- that apply to the actuals in the generic declaration and the actuals of
779 -- the actual package in the enclosing instantiation. The simplest way to
780 -- apply these rules is to repeat the instantiation of the formal package
781 -- in the context of the enclosing instance, and compare the generic
782 -- associations of this instantiation with those of the actual package.
783 -- This internal instantiation only needs to contain the renamings of the
784 -- formals: the visible and private declarations themselves need not be
785 -- created.
786
787 -- In Ada 2005, the formal package may be only partially parameterized.
788 -- In that case the visibility step must make visible those actuals whose
789 -- corresponding formals were given with a box. A final complication
790 -- involves inherited operations from formal derived types, which must
791 -- be visible if the type is.
792
793 function Is_In_Main_Unit (N : Node_Id) return Boolean;
794 -- Test if given node is in the main unit
795
796 procedure Load_Parent_Of_Generic
797 (N : Node_Id;
798 Spec : Node_Id;
799 Body_Optional : Boolean := False);
800 -- If the generic appears in a separate non-generic library unit, load the
801 -- corresponding body to retrieve the body of the generic. N is the node
802 -- for the generic instantiation, Spec is the generic package declaration.
803 --
804 -- Body_Optional is a flag that indicates that the body is being loaded to
805 -- ensure that temporaries are generated consistently when there are other
806 -- instances in the current declarative part that precede the one being
807 -- loaded. In that case a missing body is acceptable.
808
809 procedure Map_Formal_Package_Entities (Form : Entity_Id; Act : Entity_Id);
810 -- Within the generic part, entities in the formal package are
811 -- visible. To validate subsequent type declarations, indicate
812 -- the correspondence between the entities in the analyzed formal,
813 -- and the entities in the actual package. There are three packages
814 -- involved in the instantiation of a formal package: the parent
815 -- generic P1 which appears in the generic declaration, the fake
816 -- instantiation P2 which appears in the analyzed generic, and whose
817 -- visible entities may be used in subsequent formals, and the actual
818 -- P3 in the instance. To validate subsequent formals, me indicate
819 -- that the entities in P2 are mapped into those of P3. The mapping of
820 -- entities has to be done recursively for nested packages.
821
822 procedure Move_Freeze_Nodes
823 (Out_Of : Entity_Id;
824 After : Node_Id;
825 L : List_Id);
826 -- Freeze nodes can be generated in the analysis of a generic unit, but
827 -- will not be seen by the back-end. It is necessary to move those nodes
828 -- to the enclosing scope if they freeze an outer entity. We place them
829 -- at the end of the enclosing generic package, which is semantically
830 -- neutral.
831
832 procedure Preanalyze_Actuals (N : Node_Id; Inst : Entity_Id := Empty);
833 -- Analyze actuals to perform name resolution. Full resolution is done
834 -- later, when the expected types are known, but names have to be captured
835 -- before installing parents of generics, that are not visible for the
836 -- actuals themselves.
837 --
838 -- If Inst is present, it is the entity of the package instance. This
839 -- entity is marked as having a limited_view actual when some actual is
840 -- a limited view. This is used to place the instance body properly.
841
842 procedure Remove_Parent (In_Body : Boolean := False);
843 -- Reverse effect after instantiation of child is complete
844
845 procedure Restore_Hidden_Primitives (Prims_List : in out Elist_Id);
846 -- Restore suffix 'P' to primitives of Prims_List and leave Prims_List
847 -- set to No_Elist.
848
849 procedure Set_Instance_Env
850 (Gen_Unit : Entity_Id;
851 Act_Unit : Entity_Id);
852 -- Save current instance on saved environment, to be used to determine
853 -- the global status of entities in nested instances. Part of Save_Env.
854 -- called after verifying that the generic unit is legal for the instance,
855 -- The procedure also examines whether the generic unit is a predefined
856 -- unit, in order to set configuration switches accordingly. As a result
857 -- the procedure must be called after analyzing and freezing the actuals.
858
859 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id);
860 -- Associate analyzed generic parameter with corresponding instance. Used
861 -- for semantic checks at instantiation time.
862
863 function True_Parent (N : Node_Id) return Node_Id;
864 -- For a subunit, return parent of corresponding stub, else return
865 -- parent of node.
866
867 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id);
868 -- Verify that an attribute that appears as the default for a formal
869 -- subprogram is a function or procedure with the correct profile.
870
871 -------------------------------------------
872 -- Data Structures for Generic Renamings --
873 -------------------------------------------
874
875 -- The map Generic_Renamings associates generic entities with their
876 -- corresponding actuals. Currently used to validate type instances. It
877 -- will eventually be used for all generic parameters to eliminate the
878 -- need for overload resolution in the instance.
879
880 type Assoc_Ptr is new Int;
881
882 Assoc_Null : constant Assoc_Ptr := -1;
883
884 type Assoc is record
885 Gen_Id : Entity_Id;
886 Act_Id : Entity_Id;
887 Next_In_HTable : Assoc_Ptr;
888 end record;
889
890 package Generic_Renamings is new Table.Table
891 (Table_Component_Type => Assoc,
892 Table_Index_Type => Assoc_Ptr,
893 Table_Low_Bound => 0,
894 Table_Initial => 10,
895 Table_Increment => 100,
896 Table_Name => "Generic_Renamings");
897
898 -- Variable to hold enclosing instantiation. When the environment is
899 -- saved for a subprogram inlining, the corresponding Act_Id is empty.
900
901 Current_Instantiated_Parent : Assoc := (Empty, Empty, Assoc_Null);
902
903 -- Hash table for associations
904
905 HTable_Size : constant := 37;
906 type HTable_Range is range 0 .. HTable_Size - 1;
907
908 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr);
909 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr;
910 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id;
911 function Hash (F : Entity_Id) return HTable_Range;
912
913 package Generic_Renamings_HTable is new GNAT.HTable.Static_HTable (
914 Header_Num => HTable_Range,
915 Element => Assoc,
916 Elmt_Ptr => Assoc_Ptr,
917 Null_Ptr => Assoc_Null,
918 Set_Next => Set_Next_Assoc,
919 Next => Next_Assoc,
920 Key => Entity_Id,
921 Get_Key => Get_Gen_Id,
922 Hash => Hash,
923 Equal => "=");
924
925 Exchanged_Views : Elist_Id;
926 -- This list holds the private views that have been exchanged during
927 -- instantiation to restore the visibility of the generic declaration.
928 -- (see comments above). After instantiation, the current visibility is
929 -- reestablished by means of a traversal of this list.
930
931 Hidden_Entities : Elist_Id;
932 -- This list holds the entities of the current scope that are removed
933 -- from immediate visibility when instantiating a child unit. Their
934 -- visibility is restored in Remove_Parent.
935
936 -- Because instantiations can be recursive, the following must be saved
937 -- on entry and restored on exit from an instantiation (spec or body).
938 -- This is done by the two procedures Save_Env and Restore_Env. For
939 -- package and subprogram instantiations (but not for the body instances)
940 -- the action of Save_Env is done in two steps: Init_Env is called before
941 -- Check_Generic_Child_Unit, because setting the parent instances requires
942 -- that the visibility data structures be properly initialized. Once the
943 -- generic is unit is validated, Set_Instance_Env completes Save_Env.
944
945 Parent_Unit_Visible : Boolean := False;
946 -- Parent_Unit_Visible is used when the generic is a child unit, and
947 -- indicates whether the ultimate parent of the generic is visible in the
948 -- instantiation environment. It is used to reset the visibility of the
949 -- parent at the end of the instantiation (see Remove_Parent).
950
951 Instance_Parent_Unit : Entity_Id := Empty;
952 -- This records the ultimate parent unit of an instance of a generic
953 -- child unit and is used in conjunction with Parent_Unit_Visible to
954 -- indicate the unit to which the Parent_Unit_Visible flag corresponds.
955
956 type Instance_Env is record
957 Instantiated_Parent : Assoc;
958 Exchanged_Views : Elist_Id;
959 Hidden_Entities : Elist_Id;
960 Current_Sem_Unit : Unit_Number_Type;
961 Parent_Unit_Visible : Boolean := False;
962 Instance_Parent_Unit : Entity_Id := Empty;
963 Switches : Config_Switches_Type;
964 end record;
965
966 package Instance_Envs is new Table.Table (
967 Table_Component_Type => Instance_Env,
968 Table_Index_Type => Int,
969 Table_Low_Bound => 0,
970 Table_Initial => 32,
971 Table_Increment => 100,
972 Table_Name => "Instance_Envs");
973
974 procedure Restore_Private_Views
975 (Pack_Id : Entity_Id;
976 Is_Package : Boolean := True);
977 -- Restore the private views of external types, and unmark the generic
978 -- renamings of actuals, so that they become compatible subtypes again.
979 -- For subprograms, Pack_Id is the package constructed to hold the
980 -- renamings.
981
982 procedure Switch_View (T : Entity_Id);
983 -- Switch the partial and full views of a type and its private
984 -- dependents (i.e. its subtypes and derived types).
985
986 ------------------------------------
987 -- Structures for Error Reporting --
988 ------------------------------------
989
990 Instantiation_Node : Node_Id;
991 -- Used by subprograms that validate instantiation of formal parameters
992 -- where there might be no actual on which to place the error message.
993 -- Also used to locate the instantiation node for generic subunits.
994
995 Instantiation_Error : exception;
996 -- When there is a semantic error in the generic parameter matching,
997 -- there is no point in continuing the instantiation, because the
998 -- number of cascaded errors is unpredictable. This exception aborts
999 -- the instantiation process altogether.
1000
1001 S_Adjustment : Sloc_Adjustment;
1002 -- Offset created for each node in an instantiation, in order to keep
1003 -- track of the source position of the instantiation in each of its nodes.
1004 -- A subsequent semantic error or warning on a construct of the instance
1005 -- points to both places: the original generic node, and the point of
1006 -- instantiation. See Sinput and Sinput.L for additional details.
1007
1008 ------------------------------------------------------------
1009 -- Data structure for keeping track when inside a Generic --
1010 ------------------------------------------------------------
1011
1012 -- The following table is used to save values of the Inside_A_Generic
1013 -- flag (see spec of Sem) when they are saved by Start_Generic.
1014
1015 package Generic_Flags is new Table.Table (
1016 Table_Component_Type => Boolean,
1017 Table_Index_Type => Int,
1018 Table_Low_Bound => 0,
1019 Table_Initial => 32,
1020 Table_Increment => 200,
1021 Table_Name => "Generic_Flags");
1022
1023 ---------------------------
1024 -- Abandon_Instantiation --
1025 ---------------------------
1026
1027 procedure Abandon_Instantiation (N : Node_Id) is
1028 begin
1029 Error_Msg_N ("\instantiation abandoned!", N);
1030 raise Instantiation_Error;
1031 end Abandon_Instantiation;
1032
1033 --------------------------------
1034 -- Add_Pending_Instantiation --
1035 --------------------------------
1036
1037 procedure Add_Pending_Instantiation (Inst : Node_Id; Act_Decl : Node_Id) is
1038 begin
1039
1040 -- Add to the instantiation node and the corresponding unit declaration
1041 -- the current values of global flags to be used when analyzing the
1042 -- instance body.
1043
1044 Pending_Instantiations.Append
1045 ((Inst_Node => Inst,
1046 Act_Decl => Act_Decl,
1047 Expander_Status => Expander_Active,
1048 Current_Sem_Unit => Current_Sem_Unit,
1049 Scope_Suppress => Scope_Suppress,
1050 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
1051 Version => Ada_Version,
1052 Version_Pragma => Ada_Version_Pragma,
1053 Warnings => Save_Warnings,
1054 SPARK_Mode => SPARK_Mode,
1055 SPARK_Mode_Pragma => SPARK_Mode_Pragma));
1056 end Add_Pending_Instantiation;
1057
1058 ----------------------------------
1059 -- Adjust_Inherited_Pragma_Sloc --
1060 ----------------------------------
1061
1062 procedure Adjust_Inherited_Pragma_Sloc (N : Node_Id) is
1063 begin
1064 Adjust_Instantiation_Sloc (N, S_Adjustment);
1065 end Adjust_Inherited_Pragma_Sloc;
1066
1067 --------------------------
1068 -- Analyze_Associations --
1069 --------------------------
1070
1071 function Analyze_Associations
1072 (I_Node : Node_Id;
1073 Formals : List_Id;
1074 F_Copy : List_Id) return List_Id
1075 is
1076 Actuals_To_Freeze : constant Elist_Id := New_Elmt_List;
1077 Assoc_List : constant List_Id := New_List;
1078 Default_Actuals : constant List_Id := New_List;
1079 Gen_Unit : constant Entity_Id :=
1080 Defining_Entity (Parent (F_Copy));
1081
1082 Actuals : List_Id;
1083 Actual : Node_Id;
1084 Analyzed_Formal : Node_Id;
1085 First_Named : Node_Id := Empty;
1086 Formal : Node_Id;
1087 Match : Node_Id;
1088 Named : Node_Id;
1089 Saved_Formal : Node_Id;
1090
1091 Default_Formals : constant List_Id := New_List;
1092 -- If an Others_Choice is present, some of the formals may be defaulted.
1093 -- To simplify the treatment of visibility in an instance, we introduce
1094 -- individual defaults for each such formal. These defaults are
1095 -- appended to the list of associations and replace the Others_Choice.
1096
1097 Found_Assoc : Node_Id;
1098 -- Association for the current formal being match. Empty if there are
1099 -- no remaining actuals, or if there is no named association with the
1100 -- name of the formal.
1101
1102 Is_Named_Assoc : Boolean;
1103 Num_Matched : Nat := 0;
1104 Num_Actuals : Nat := 0;
1105
1106 Others_Present : Boolean := False;
1107 Others_Choice : Node_Id := Empty;
1108 -- In Ada 2005, indicates partial parameterization of a formal
1109 -- package. As usual an other association must be last in the list.
1110
1111 procedure Check_Fixed_Point_Actual (Actual : Node_Id);
1112 -- Warn if an actual fixed-point type has user-defined arithmetic
1113 -- operations, but there is no corresponding formal in the generic,
1114 -- in which case the predefined operations will be used. This merits
1115 -- a warning because of the special semantics of fixed point ops.
1116
1117 procedure Check_Overloaded_Formal_Subprogram (Formal : Entity_Id);
1118 -- Apply RM 12.3(9): if a formal subprogram is overloaded, the instance
1119 -- cannot have a named association for it. AI05-0025 extends this rule
1120 -- to formals of formal packages by AI05-0025, and it also applies to
1121 -- box-initialized formals.
1122
1123 function Has_Fully_Defined_Profile (Subp : Entity_Id) return Boolean;
1124 -- Determine whether the parameter types and the return type of Subp
1125 -- are fully defined at the point of instantiation.
1126
1127 function Matching_Actual
1128 (F : Entity_Id;
1129 A_F : Entity_Id) return Node_Id;
1130 -- Find actual that corresponds to a given a formal parameter. If the
1131 -- actuals are positional, return the next one, if any. If the actuals
1132 -- are named, scan the parameter associations to find the right one.
1133 -- A_F is the corresponding entity in the analyzed generic, which is
1134 -- placed on the selector name for ASIS use.
1135 --
1136 -- In Ada 2005, a named association may be given with a box, in which
1137 -- case Matching_Actual sets Found_Assoc to the generic association,
1138 -- but return Empty for the actual itself. In this case the code below
1139 -- creates a corresponding declaration for the formal.
1140
1141 function Partial_Parameterization return Boolean;
1142 -- Ada 2005: if no match is found for a given formal, check if the
1143 -- association for it includes a box, or whether the associations
1144 -- include an Others clause.
1145
1146 procedure Process_Default (F : Entity_Id);
1147 -- Add a copy of the declaration of generic formal F to the list of
1148 -- associations, and add an explicit box association for F if there
1149 -- is none yet, and the default comes from an Others_Choice.
1150
1151 function Renames_Standard_Subprogram (Subp : Entity_Id) return Boolean;
1152 -- Determine whether Subp renames one of the subprograms defined in the
1153 -- generated package Standard.
1154
1155 procedure Set_Analyzed_Formal;
1156 -- Find the node in the generic copy that corresponds to a given formal.
1157 -- The semantic information on this node is used to perform legality
1158 -- checks on the actuals. Because semantic analysis can introduce some
1159 -- anonymous entities or modify the declaration node itself, the
1160 -- correspondence between the two lists is not one-one. In addition to
1161 -- anonymous types, the presence a formal equality will introduce an
1162 -- implicit declaration for the corresponding inequality.
1163
1164 ----------------------------------------
1165 -- Check_Overloaded_Formal_Subprogram --
1166 ----------------------------------------
1167
1168 procedure Check_Overloaded_Formal_Subprogram (Formal : Entity_Id) is
1169 Temp_Formal : Entity_Id;
1170
1171 begin
1172 Temp_Formal := First (Formals);
1173 while Present (Temp_Formal) loop
1174 if Nkind (Temp_Formal) in N_Formal_Subprogram_Declaration
1175 and then Temp_Formal /= Formal
1176 and then
1177 Chars (Defining_Unit_Name (Specification (Formal))) =
1178 Chars (Defining_Unit_Name (Specification (Temp_Formal)))
1179 then
1180 if Present (Found_Assoc) then
1181 Error_Msg_N
1182 ("named association not allowed for overloaded formal",
1183 Found_Assoc);
1184
1185 else
1186 Error_Msg_N
1187 ("named association not allowed for overloaded formal",
1188 Others_Choice);
1189 end if;
1190
1191 Abandon_Instantiation (Instantiation_Node);
1192 end if;
1193
1194 Next (Temp_Formal);
1195 end loop;
1196 end Check_Overloaded_Formal_Subprogram;
1197
1198 -------------------------------
1199 -- Check_Fixed_Point_Actual --
1200 -------------------------------
1201
1202 procedure Check_Fixed_Point_Actual (Actual : Node_Id) is
1203 Typ : constant Entity_Id := Entity (Actual);
1204 Prims : constant Elist_Id := Collect_Primitive_Operations (Typ);
1205 Elem : Elmt_Id;
1206 Formal : Node_Id;
1207 Op : Entity_Id;
1208
1209 begin
1210 -- Locate primitive operations of the type that are arithmetic
1211 -- operations.
1212
1213 Elem := First_Elmt (Prims);
1214 while Present (Elem) loop
1215 if Nkind (Node (Elem)) = N_Defining_Operator_Symbol then
1216
1217 -- Check whether the generic unit has a formal subprogram of
1218 -- the same name. This does not check types but is good enough
1219 -- to justify a warning.
1220
1221 Formal := First_Non_Pragma (Formals);
1222 Op := Alias (Node (Elem));
1223
1224 while Present (Formal) loop
1225 if Nkind (Formal) = N_Formal_Concrete_Subprogram_Declaration
1226 and then Chars (Defining_Entity (Formal)) =
1227 Chars (Node (Elem))
1228 then
1229 exit;
1230
1231 elsif Nkind (Formal) = N_Formal_Package_Declaration then
1232 declare
1233 Assoc : Node_Id;
1234 Ent : Entity_Id;
1235
1236 begin
1237 -- Locate corresponding actual, and check whether it
1238 -- includes a fixed-point type.
1239
1240 Assoc := First (Assoc_List);
1241 while Present (Assoc) loop
1242 exit when
1243 Nkind (Assoc) = N_Package_Renaming_Declaration
1244 and then Chars (Defining_Unit_Name (Assoc)) =
1245 Chars (Defining_Identifier (Formal));
1246
1247 Next (Assoc);
1248 end loop;
1249
1250 if Present (Assoc) then
1251
1252 -- If formal package declares a fixed-point type,
1253 -- and the user-defined operator is derived from
1254 -- a generic instance package, the fixed-point type
1255 -- does not use the corresponding predefined op.
1256
1257 Ent := First_Entity (Entity (Name (Assoc)));
1258 while Present (Ent) loop
1259 if Is_Fixed_Point_Type (Ent)
1260 and then Present (Op)
1261 and then Is_Generic_Instance (Scope (Op))
1262 then
1263 return;
1264 end if;
1265
1266 Next_Entity (Ent);
1267 end loop;
1268 end if;
1269 end;
1270 end if;
1271
1272 Next (Formal);
1273 end loop;
1274
1275 if No (Formal) then
1276 Error_Msg_Sloc := Sloc (Node (Elem));
1277 Error_Msg_NE
1278 ("?instance does not use primitive operation&#",
1279 Actual, Node (Elem));
1280 end if;
1281 end if;
1282
1283 Next_Elmt (Elem);
1284 end loop;
1285 end Check_Fixed_Point_Actual;
1286
1287 -------------------------------
1288 -- Has_Fully_Defined_Profile --
1289 -------------------------------
1290
1291 function Has_Fully_Defined_Profile (Subp : Entity_Id) return Boolean is
1292 function Is_Fully_Defined_Type (Typ : Entity_Id) return Boolean;
1293 -- Determine whethet type Typ is fully defined
1294
1295 ---------------------------
1296 -- Is_Fully_Defined_Type --
1297 ---------------------------
1298
1299 function Is_Fully_Defined_Type (Typ : Entity_Id) return Boolean is
1300 begin
1301 -- A private type without a full view is not fully defined
1302
1303 if Is_Private_Type (Typ)
1304 and then No (Full_View (Typ))
1305 then
1306 return False;
1307
1308 -- An incomplete type is never fully defined
1309
1310 elsif Is_Incomplete_Type (Typ) then
1311 return False;
1312
1313 -- All other types are fully defined
1314
1315 else
1316 return True;
1317 end if;
1318 end Is_Fully_Defined_Type;
1319
1320 -- Local declarations
1321
1322 Param : Entity_Id;
1323
1324 -- Start of processing for Has_Fully_Defined_Profile
1325
1326 begin
1327 -- Check the parameters
1328
1329 Param := First_Formal (Subp);
1330 while Present (Param) loop
1331 if not Is_Fully_Defined_Type (Etype (Param)) then
1332 return False;
1333 end if;
1334
1335 Next_Formal (Param);
1336 end loop;
1337
1338 -- Check the return type
1339
1340 return Is_Fully_Defined_Type (Etype (Subp));
1341 end Has_Fully_Defined_Profile;
1342
1343 ---------------------
1344 -- Matching_Actual --
1345 ---------------------
1346
1347 function Matching_Actual
1348 (F : Entity_Id;
1349 A_F : Entity_Id) return Node_Id
1350 is
1351 Prev : Node_Id;
1352 Act : Node_Id;
1353
1354 begin
1355 Is_Named_Assoc := False;
1356
1357 -- End of list of purely positional parameters
1358
1359 if No (Actual) or else Nkind (Actual) = N_Others_Choice then
1360 Found_Assoc := Empty;
1361 Act := Empty;
1362
1363 -- Case of positional parameter corresponding to current formal
1364
1365 elsif No (Selector_Name (Actual)) then
1366 Found_Assoc := Actual;
1367 Act := Explicit_Generic_Actual_Parameter (Actual);
1368 Num_Matched := Num_Matched + 1;
1369 Next (Actual);
1370
1371 -- Otherwise scan list of named actuals to find the one with the
1372 -- desired name. All remaining actuals have explicit names.
1373
1374 else
1375 Is_Named_Assoc := True;
1376 Found_Assoc := Empty;
1377 Act := Empty;
1378 Prev := Empty;
1379
1380 while Present (Actual) loop
1381 if Nkind (Actual) = N_Others_Choice then
1382 Found_Assoc := Empty;
1383 Act := Empty;
1384
1385 elsif Chars (Selector_Name (Actual)) = Chars (F) then
1386 Set_Entity (Selector_Name (Actual), A_F);
1387 Set_Etype (Selector_Name (Actual), Etype (A_F));
1388 Generate_Reference (A_F, Selector_Name (Actual));
1389
1390 Found_Assoc := Actual;
1391 Act := Explicit_Generic_Actual_Parameter (Actual);
1392 Num_Matched := Num_Matched + 1;
1393 exit;
1394 end if;
1395
1396 Prev := Actual;
1397 Next (Actual);
1398 end loop;
1399
1400 -- Reset for subsequent searches. In most cases the named
1401 -- associations are in order. If they are not, we reorder them
1402 -- to avoid scanning twice the same actual. This is not just a
1403 -- question of efficiency: there may be multiple defaults with
1404 -- boxes that have the same name. In a nested instantiation we
1405 -- insert actuals for those defaults, and cannot rely on their
1406 -- names to disambiguate them.
1407
1408 if Actual = First_Named then
1409 Next (First_Named);
1410
1411 elsif Present (Actual) then
1412 Insert_Before (First_Named, Remove_Next (Prev));
1413 end if;
1414
1415 Actual := First_Named;
1416 end if;
1417
1418 if Is_Entity_Name (Act) and then Present (Entity (Act)) then
1419 Set_Used_As_Generic_Actual (Entity (Act));
1420 end if;
1421
1422 return Act;
1423 end Matching_Actual;
1424
1425 ------------------------------
1426 -- Partial_Parameterization --
1427 ------------------------------
1428
1429 function Partial_Parameterization return Boolean is
1430 begin
1431 return Others_Present
1432 or else (Present (Found_Assoc) and then Box_Present (Found_Assoc));
1433 end Partial_Parameterization;
1434
1435 ---------------------
1436 -- Process_Default --
1437 ---------------------
1438
1439 procedure Process_Default (F : Entity_Id) is
1440 Loc : constant Source_Ptr := Sloc (I_Node);
1441 F_Id : constant Entity_Id := Defining_Entity (F);
1442 Decl : Node_Id;
1443 Default : Node_Id;
1444 Id : Entity_Id;
1445
1446 begin
1447 -- Append copy of formal declaration to associations, and create new
1448 -- defining identifier for it.
1449
1450 Decl := New_Copy_Tree (F);
1451 Id := Make_Defining_Identifier (Sloc (F_Id), Chars (F_Id));
1452
1453 if Nkind (F) in N_Formal_Subprogram_Declaration then
1454 Set_Defining_Unit_Name (Specification (Decl), Id);
1455
1456 else
1457 Set_Defining_Identifier (Decl, Id);
1458 end if;
1459
1460 Append (Decl, Assoc_List);
1461
1462 if No (Found_Assoc) then
1463 Default :=
1464 Make_Generic_Association (Loc,
1465 Selector_Name =>
1466 New_Occurrence_Of (Id, Loc),
1467 Explicit_Generic_Actual_Parameter => Empty);
1468 Set_Box_Present (Default);
1469 Append (Default, Default_Formals);
1470 end if;
1471 end Process_Default;
1472
1473 ---------------------------------
1474 -- Renames_Standard_Subprogram --
1475 ---------------------------------
1476
1477 function Renames_Standard_Subprogram (Subp : Entity_Id) return Boolean is
1478 Id : Entity_Id;
1479
1480 begin
1481 Id := Alias (Subp);
1482 while Present (Id) loop
1483 if Scope (Id) = Standard_Standard then
1484 return True;
1485 end if;
1486
1487 Id := Alias (Id);
1488 end loop;
1489
1490 return False;
1491 end Renames_Standard_Subprogram;
1492
1493 -------------------------
1494 -- Set_Analyzed_Formal --
1495 -------------------------
1496
1497 procedure Set_Analyzed_Formal is
1498 Kind : Node_Kind;
1499
1500 begin
1501 while Present (Analyzed_Formal) loop
1502 Kind := Nkind (Analyzed_Formal);
1503
1504 case Nkind (Formal) is
1505 when N_Formal_Subprogram_Declaration =>
1506 exit when Kind in N_Formal_Subprogram_Declaration
1507 and then
1508 Chars
1509 (Defining_Unit_Name (Specification (Formal))) =
1510 Chars
1511 (Defining_Unit_Name (Specification (Analyzed_Formal)));
1512
1513 when N_Formal_Package_Declaration =>
1514 exit when Nkind_In (Kind, N_Formal_Package_Declaration,
1515 N_Generic_Package_Declaration,
1516 N_Package_Declaration);
1517
1518 when N_Use_Package_Clause
1519 | N_Use_Type_Clause
1520 =>
1521 exit;
1522
1523 when others =>
1524
1525 -- Skip freeze nodes, and nodes inserted to replace
1526 -- unrecognized pragmas.
1527
1528 exit when
1529 Kind not in N_Formal_Subprogram_Declaration
1530 and then not Nkind_In (Kind, N_Subprogram_Declaration,
1531 N_Freeze_Entity,
1532 N_Null_Statement,
1533 N_Itype_Reference)
1534 and then Chars (Defining_Identifier (Formal)) =
1535 Chars (Defining_Identifier (Analyzed_Formal));
1536 end case;
1537
1538 Next (Analyzed_Formal);
1539 end loop;
1540 end Set_Analyzed_Formal;
1541
1542 -- Start of processing for Analyze_Associations
1543
1544 begin
1545 Actuals := Generic_Associations (I_Node);
1546
1547 if Present (Actuals) then
1548
1549 -- Check for an Others choice, indicating a partial parameterization
1550 -- for a formal package.
1551
1552 Actual := First (Actuals);
1553 while Present (Actual) loop
1554 if Nkind (Actual) = N_Others_Choice then
1555 Others_Present := True;
1556 Others_Choice := Actual;
1557
1558 if Present (Next (Actual)) then
1559 Error_Msg_N ("others must be last association", Actual);
1560 end if;
1561
1562 -- This subprogram is used both for formal packages and for
1563 -- instantiations. For the latter, associations must all be
1564 -- explicit.
1565
1566 if Nkind (I_Node) /= N_Formal_Package_Declaration
1567 and then Comes_From_Source (I_Node)
1568 then
1569 Error_Msg_N
1570 ("others association not allowed in an instance",
1571 Actual);
1572 end if;
1573
1574 -- In any case, nothing to do after the others association
1575
1576 exit;
1577
1578 elsif Box_Present (Actual)
1579 and then Comes_From_Source (I_Node)
1580 and then Nkind (I_Node) /= N_Formal_Package_Declaration
1581 then
1582 Error_Msg_N
1583 ("box association not allowed in an instance", Actual);
1584 end if;
1585
1586 Next (Actual);
1587 end loop;
1588
1589 -- If named associations are present, save first named association
1590 -- (it may of course be Empty) to facilitate subsequent name search.
1591
1592 First_Named := First (Actuals);
1593 while Present (First_Named)
1594 and then Nkind (First_Named) /= N_Others_Choice
1595 and then No (Selector_Name (First_Named))
1596 loop
1597 Num_Actuals := Num_Actuals + 1;
1598 Next (First_Named);
1599 end loop;
1600 end if;
1601
1602 Named := First_Named;
1603 while Present (Named) loop
1604 if Nkind (Named) /= N_Others_Choice
1605 and then No (Selector_Name (Named))
1606 then
1607 Error_Msg_N ("invalid positional actual after named one", Named);
1608 Abandon_Instantiation (Named);
1609 end if;
1610
1611 -- A named association may lack an actual parameter, if it was
1612 -- introduced for a default subprogram that turns out to be local
1613 -- to the outer instantiation. If it has a box association it must
1614 -- correspond to some formal in the generic.
1615
1616 if Nkind (Named) /= N_Others_Choice
1617 and then (Present (Explicit_Generic_Actual_Parameter (Named))
1618 or else Box_Present (Named))
1619 then
1620 Num_Actuals := Num_Actuals + 1;
1621 end if;
1622
1623 Next (Named);
1624 end loop;
1625
1626 if Present (Formals) then
1627 Formal := First_Non_Pragma (Formals);
1628 Analyzed_Formal := First_Non_Pragma (F_Copy);
1629
1630 if Present (Actuals) then
1631 Actual := First (Actuals);
1632
1633 -- All formals should have default values
1634
1635 else
1636 Actual := Empty;
1637 end if;
1638
1639 while Present (Formal) loop
1640 Set_Analyzed_Formal;
1641 Saved_Formal := Next_Non_Pragma (Formal);
1642
1643 case Nkind (Formal) is
1644 when N_Formal_Object_Declaration =>
1645 Match :=
1646 Matching_Actual
1647 (Defining_Identifier (Formal),
1648 Defining_Identifier (Analyzed_Formal));
1649
1650 if No (Match) and then Partial_Parameterization then
1651 Process_Default (Formal);
1652
1653 else
1654 Append_List
1655 (Instantiate_Object (Formal, Match, Analyzed_Formal),
1656 Assoc_List);
1657
1658 -- For a defaulted in_parameter, create an entry in the
1659 -- the list of defaulted actuals, for GNATProve use. Do
1660 -- not included these defaults for an instance nested
1661 -- within a generic, because the defaults are also used
1662 -- in the analysis of the enclosing generic, and only
1663 -- defaulted subprograms are relevant there.
1664
1665 if No (Match) and then not Inside_A_Generic then
1666 Append_To (Default_Actuals,
1667 Make_Generic_Association (Sloc (I_Node),
1668 Selector_Name =>
1669 New_Occurrence_Of
1670 (Defining_Identifier (Formal), Sloc (I_Node)),
1671 Explicit_Generic_Actual_Parameter =>
1672 New_Copy_Tree (Default_Expression (Formal))));
1673 end if;
1674 end if;
1675
1676 -- If the object is a call to an expression function, this
1677 -- is a freezing point for it.
1678
1679 if Is_Entity_Name (Match)
1680 and then Present (Entity (Match))
1681 and then Nkind
1682 (Original_Node (Unit_Declaration_Node (Entity (Match))))
1683 = N_Expression_Function
1684 then
1685 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1686 end if;
1687
1688 when N_Formal_Type_Declaration =>
1689 Match :=
1690 Matching_Actual
1691 (Defining_Identifier (Formal),
1692 Defining_Identifier (Analyzed_Formal));
1693
1694 if No (Match) then
1695 if Partial_Parameterization then
1696 Process_Default (Formal);
1697
1698 else
1699 Error_Msg_Sloc := Sloc (Gen_Unit);
1700 Error_Msg_NE
1701 ("missing actual&",
1702 Instantiation_Node, Defining_Identifier (Formal));
1703 Error_Msg_NE
1704 ("\in instantiation of & declared#",
1705 Instantiation_Node, Gen_Unit);
1706 Abandon_Instantiation (Instantiation_Node);
1707 end if;
1708
1709 else
1710 Analyze (Match);
1711 Append_List
1712 (Instantiate_Type
1713 (Formal, Match, Analyzed_Formal, Assoc_List),
1714 Assoc_List);
1715
1716 if Is_Fixed_Point_Type (Entity (Match)) then
1717 Check_Fixed_Point_Actual (Match);
1718 end if;
1719
1720 -- An instantiation is a freeze point for the actuals,
1721 -- unless this is a rewritten formal package, or the
1722 -- formal is an Ada 2012 formal incomplete type.
1723
1724 if Nkind (I_Node) = N_Formal_Package_Declaration
1725 or else
1726 (Ada_Version >= Ada_2012
1727 and then
1728 Ekind (Defining_Identifier (Analyzed_Formal)) =
1729 E_Incomplete_Type)
1730 then
1731 null;
1732
1733 else
1734 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1735 end if;
1736 end if;
1737
1738 -- A remote access-to-class-wide type is not a legal actual
1739 -- for a generic formal of an access type (E.2.2(17/2)).
1740 -- In GNAT an exception to this rule is introduced when
1741 -- the formal is marked as remote using implementation
1742 -- defined aspect/pragma Remote_Access_Type. In that case
1743 -- the actual must be remote as well.
1744
1745 -- If the current instantiation is the construction of a
1746 -- local copy for a formal package the actuals may be
1747 -- defaulted, and there is no matching actual to check.
1748
1749 if Nkind (Analyzed_Formal) = N_Formal_Type_Declaration
1750 and then
1751 Nkind (Formal_Type_Definition (Analyzed_Formal)) =
1752 N_Access_To_Object_Definition
1753 and then Present (Match)
1754 then
1755 declare
1756 Formal_Ent : constant Entity_Id :=
1757 Defining_Identifier (Analyzed_Formal);
1758 begin
1759 if Is_Remote_Access_To_Class_Wide_Type (Entity (Match))
1760 = Is_Remote_Types (Formal_Ent)
1761 then
1762 -- Remoteness of formal and actual match
1763
1764 null;
1765
1766 elsif Is_Remote_Types (Formal_Ent) then
1767
1768 -- Remote formal, non-remote actual
1769
1770 Error_Msg_NE
1771 ("actual for& must be remote", Match, Formal_Ent);
1772
1773 else
1774 -- Non-remote formal, remote actual
1775
1776 Error_Msg_NE
1777 ("actual for& may not be remote",
1778 Match, Formal_Ent);
1779 end if;
1780 end;
1781 end if;
1782
1783 when N_Formal_Subprogram_Declaration =>
1784 Match :=
1785 Matching_Actual
1786 (Defining_Unit_Name (Specification (Formal)),
1787 Defining_Unit_Name (Specification (Analyzed_Formal)));
1788
1789 -- If the formal subprogram has the same name as another
1790 -- formal subprogram of the generic, then a named
1791 -- association is illegal (12.3(9)). Exclude named
1792 -- associations that are generated for a nested instance.
1793
1794 if Present (Match)
1795 and then Is_Named_Assoc
1796 and then Comes_From_Source (Found_Assoc)
1797 then
1798 Check_Overloaded_Formal_Subprogram (Formal);
1799 end if;
1800
1801 -- If there is no corresponding actual, this may be case
1802 -- of partial parameterization, or else the formal has a
1803 -- default or a box.
1804
1805 if No (Match) and then Partial_Parameterization then
1806 Process_Default (Formal);
1807
1808 if Nkind (I_Node) = N_Formal_Package_Declaration then
1809 Check_Overloaded_Formal_Subprogram (Formal);
1810 end if;
1811
1812 else
1813 Append_To (Assoc_List,
1814 Instantiate_Formal_Subprogram
1815 (Formal, Match, Analyzed_Formal));
1816
1817 -- An instantiation is a freeze point for the actuals,
1818 -- unless this is a rewritten formal package.
1819
1820 if Nkind (I_Node) /= N_Formal_Package_Declaration
1821 and then Nkind (Match) = N_Identifier
1822 and then Is_Subprogram (Entity (Match))
1823
1824 -- The actual subprogram may rename a routine defined
1825 -- in Standard. Avoid freezing such renamings because
1826 -- subprograms coming from Standard cannot be frozen.
1827
1828 and then
1829 not Renames_Standard_Subprogram (Entity (Match))
1830
1831 -- If the actual subprogram comes from a different
1832 -- unit, it is already frozen, either by a body in
1833 -- that unit or by the end of the declarative part
1834 -- of the unit. This check avoids the freezing of
1835 -- subprograms defined in Standard which are used
1836 -- as generic actuals.
1837
1838 and then In_Same_Code_Unit (Entity (Match), I_Node)
1839 and then Has_Fully_Defined_Profile (Entity (Match))
1840 then
1841 -- Mark the subprogram as having a delayed freeze
1842 -- since this may be an out-of-order action.
1843
1844 Set_Has_Delayed_Freeze (Entity (Match));
1845 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1846 end if;
1847 end if;
1848
1849 -- If this is a nested generic, preserve default for later
1850 -- instantiations. We do this as well for GNATProve use,
1851 -- so that the list of generic associations is complete.
1852
1853 if No (Match) and then Box_Present (Formal) then
1854 declare
1855 Subp : constant Entity_Id :=
1856 Defining_Unit_Name
1857 (Specification (Last (Assoc_List)));
1858
1859 begin
1860 Append_To (Default_Actuals,
1861 Make_Generic_Association (Sloc (I_Node),
1862 Selector_Name =>
1863 New_Occurrence_Of (Subp, Sloc (I_Node)),
1864 Explicit_Generic_Actual_Parameter =>
1865 New_Occurrence_Of (Subp, Sloc (I_Node))));
1866 end;
1867 end if;
1868
1869 when N_Formal_Package_Declaration =>
1870 Match :=
1871 Matching_Actual
1872 (Defining_Identifier (Formal),
1873 Defining_Identifier (Original_Node (Analyzed_Formal)));
1874
1875 if No (Match) then
1876 if Partial_Parameterization then
1877 Process_Default (Formal);
1878
1879 else
1880 Error_Msg_Sloc := Sloc (Gen_Unit);
1881 Error_Msg_NE
1882 ("missing actual&",
1883 Instantiation_Node, Defining_Identifier (Formal));
1884 Error_Msg_NE
1885 ("\in instantiation of & declared#",
1886 Instantiation_Node, Gen_Unit);
1887
1888 Abandon_Instantiation (Instantiation_Node);
1889 end if;
1890
1891 else
1892 Analyze (Match);
1893 Append_List
1894 (Instantiate_Formal_Package
1895 (Formal, Match, Analyzed_Formal),
1896 Assoc_List);
1897
1898 -- Determine whether the actual package needs an explicit
1899 -- freeze node. This is only the case if the actual is
1900 -- declared in the same unit and has a body. Normally
1901 -- packages do not have explicit freeze nodes, and gigi
1902 -- only uses them to elaborate entities in a package
1903 -- body.
1904
1905 declare
1906 Actual : constant Entity_Id := Entity (Match);
1907
1908 Needs_Freezing : Boolean;
1909 S : Entity_Id;
1910
1911 begin
1912 if not Expander_Active
1913 or else not Has_Completion (Actual)
1914 or else not In_Same_Source_Unit (I_Node, Actual)
1915 or else
1916 (Present (Renamed_Entity (Actual))
1917 and then not
1918 In_Same_Source_Unit
1919 (I_Node, (Renamed_Entity (Actual))))
1920 then
1921 null;
1922
1923 else
1924 -- Finally we want to exclude such freeze nodes
1925 -- from statement sequences, which freeze
1926 -- everything before them.
1927 -- Is this strictly necessary ???
1928
1929 Needs_Freezing := True;
1930
1931 S := Current_Scope;
1932 while Present (S) loop
1933 if Ekind_In (S, E_Block,
1934 E_Function,
1935 E_Loop,
1936 E_Procedure)
1937 then
1938 Needs_Freezing := False;
1939 exit;
1940 end if;
1941
1942 S := Scope (S);
1943 end loop;
1944
1945 if Needs_Freezing then
1946 Set_Has_Delayed_Freeze (Actual);
1947 Append_Elmt (Actual, Actuals_To_Freeze);
1948 end if;
1949 end if;
1950 end;
1951 end if;
1952
1953 -- For use type and use package appearing in the generic part,
1954 -- we have already copied them, so we can just move them where
1955 -- they belong (we mustn't recopy them since this would mess up
1956 -- the Sloc values).
1957
1958 when N_Use_Package_Clause
1959 | N_Use_Type_Clause
1960 =>
1961 if Nkind (Original_Node (I_Node)) =
1962 N_Formal_Package_Declaration
1963 then
1964 Append (New_Copy_Tree (Formal), Assoc_List);
1965 else
1966 Remove (Formal);
1967 Append (Formal, Assoc_List);
1968 end if;
1969
1970 when others =>
1971 raise Program_Error;
1972 end case;
1973
1974 Formal := Saved_Formal;
1975 Next_Non_Pragma (Analyzed_Formal);
1976 end loop;
1977
1978 if Num_Actuals > Num_Matched then
1979 Error_Msg_Sloc := Sloc (Gen_Unit);
1980
1981 if Present (Selector_Name (Actual)) then
1982 Error_Msg_NE
1983 ("unmatched actual &", Actual, Selector_Name (Actual));
1984 Error_Msg_NE
1985 ("\in instantiation of & declared#", Actual, Gen_Unit);
1986 else
1987 Error_Msg_NE
1988 ("unmatched actual in instantiation of & declared#",
1989 Actual, Gen_Unit);
1990 end if;
1991 end if;
1992
1993 elsif Present (Actuals) then
1994 Error_Msg_N
1995 ("too many actuals in generic instantiation", Instantiation_Node);
1996 end if;
1997
1998 -- An instantiation freezes all generic actuals. The only exceptions
1999 -- to this are incomplete types and subprograms which are not fully
2000 -- defined at the point of instantiation.
2001
2002 declare
2003 Elmt : Elmt_Id := First_Elmt (Actuals_To_Freeze);
2004 begin
2005 while Present (Elmt) loop
2006 Freeze_Before (I_Node, Node (Elmt));
2007 Next_Elmt (Elmt);
2008 end loop;
2009 end;
2010
2011 -- If there are default subprograms, normalize the tree by adding
2012 -- explicit associations for them. This is required if the instance
2013 -- appears within a generic.
2014
2015 if not Is_Empty_List (Default_Actuals) then
2016 declare
2017 Default : Node_Id;
2018
2019 begin
2020 Default := First (Default_Actuals);
2021 while Present (Default) loop
2022 Mark_Rewrite_Insertion (Default);
2023 Next (Default);
2024 end loop;
2025
2026 if No (Actuals) then
2027 Set_Generic_Associations (I_Node, Default_Actuals);
2028 else
2029 Append_List_To (Actuals, Default_Actuals);
2030 end if;
2031 end;
2032 end if;
2033
2034 -- If this is a formal package, normalize the parameter list by adding
2035 -- explicit box associations for the formals that are covered by an
2036 -- Others_Choice.
2037
2038 if not Is_Empty_List (Default_Formals) then
2039 Append_List (Default_Formals, Formals);
2040 end if;
2041
2042 return Assoc_List;
2043 end Analyze_Associations;
2044
2045 -------------------------------
2046 -- Analyze_Formal_Array_Type --
2047 -------------------------------
2048
2049 procedure Analyze_Formal_Array_Type
2050 (T : in out Entity_Id;
2051 Def : Node_Id)
2052 is
2053 DSS : Node_Id;
2054
2055 begin
2056 -- Treated like a non-generic array declaration, with additional
2057 -- semantic checks.
2058
2059 Enter_Name (T);
2060
2061 if Nkind (Def) = N_Constrained_Array_Definition then
2062 DSS := First (Discrete_Subtype_Definitions (Def));
2063 while Present (DSS) loop
2064 if Nkind_In (DSS, N_Subtype_Indication,
2065 N_Range,
2066 N_Attribute_Reference)
2067 then
2068 Error_Msg_N ("only a subtype mark is allowed in a formal", DSS);
2069 end if;
2070
2071 Next (DSS);
2072 end loop;
2073 end if;
2074
2075 Array_Type_Declaration (T, Def);
2076 Set_Is_Generic_Type (Base_Type (T));
2077
2078 if Ekind (Component_Type (T)) = E_Incomplete_Type
2079 and then No (Full_View (Component_Type (T)))
2080 then
2081 Error_Msg_N ("premature usage of incomplete type", Def);
2082
2083 -- Check that range constraint is not allowed on the component type
2084 -- of a generic formal array type (AARM 12.5.3(3))
2085
2086 elsif Is_Internal (Component_Type (T))
2087 and then Present (Subtype_Indication (Component_Definition (Def)))
2088 and then Nkind (Original_Node
2089 (Subtype_Indication (Component_Definition (Def)))) =
2090 N_Subtype_Indication
2091 then
2092 Error_Msg_N
2093 ("in a formal, a subtype indication can only be "
2094 & "a subtype mark (RM 12.5.3(3))",
2095 Subtype_Indication (Component_Definition (Def)));
2096 end if;
2097
2098 end Analyze_Formal_Array_Type;
2099
2100 ---------------------------------------------
2101 -- Analyze_Formal_Decimal_Fixed_Point_Type --
2102 ---------------------------------------------
2103
2104 -- As for other generic types, we create a valid type representation with
2105 -- legal but arbitrary attributes, whose values are never considered
2106 -- static. For all scalar types we introduce an anonymous base type, with
2107 -- the same attributes. We choose the corresponding integer type to be
2108 -- Standard_Integer.
2109 -- Here and in other similar routines, the Sloc of the generated internal
2110 -- type must be the same as the sloc of the defining identifier of the
2111 -- formal type declaration, to provide proper source navigation.
2112
2113 procedure Analyze_Formal_Decimal_Fixed_Point_Type
2114 (T : Entity_Id;
2115 Def : Node_Id)
2116 is
2117 Loc : constant Source_Ptr := Sloc (Def);
2118
2119 Base : constant Entity_Id :=
2120 New_Internal_Entity
2121 (E_Decimal_Fixed_Point_Type,
2122 Current_Scope,
2123 Sloc (Defining_Identifier (Parent (Def))), 'G');
2124
2125 Int_Base : constant Entity_Id := Standard_Integer;
2126 Delta_Val : constant Ureal := Ureal_1;
2127 Digs_Val : constant Uint := Uint_6;
2128
2129 function Make_Dummy_Bound return Node_Id;
2130 -- Return a properly typed universal real literal to use as a bound
2131
2132 ----------------------
2133 -- Make_Dummy_Bound --
2134 ----------------------
2135
2136 function Make_Dummy_Bound return Node_Id is
2137 Bound : constant Node_Id := Make_Real_Literal (Loc, Ureal_1);
2138 begin
2139 Set_Etype (Bound, Universal_Real);
2140 return Bound;
2141 end Make_Dummy_Bound;
2142
2143 -- Start of processing for Analyze_Formal_Decimal_Fixed_Point_Type
2144
2145 begin
2146 Enter_Name (T);
2147
2148 Set_Etype (Base, Base);
2149 Set_Size_Info (Base, Int_Base);
2150 Set_RM_Size (Base, RM_Size (Int_Base));
2151 Set_First_Rep_Item (Base, First_Rep_Item (Int_Base));
2152 Set_Digits_Value (Base, Digs_Val);
2153 Set_Delta_Value (Base, Delta_Val);
2154 Set_Small_Value (Base, Delta_Val);
2155 Set_Scalar_Range (Base,
2156 Make_Range (Loc,
2157 Low_Bound => Make_Dummy_Bound,
2158 High_Bound => Make_Dummy_Bound));
2159
2160 Set_Is_Generic_Type (Base);
2161 Set_Parent (Base, Parent (Def));
2162
2163 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
2164 Set_Etype (T, Base);
2165 Set_Size_Info (T, Int_Base);
2166 Set_RM_Size (T, RM_Size (Int_Base));
2167 Set_First_Rep_Item (T, First_Rep_Item (Int_Base));
2168 Set_Digits_Value (T, Digs_Val);
2169 Set_Delta_Value (T, Delta_Val);
2170 Set_Small_Value (T, Delta_Val);
2171 Set_Scalar_Range (T, Scalar_Range (Base));
2172 Set_Is_Constrained (T);
2173
2174 Check_Restriction (No_Fixed_Point, Def);
2175 end Analyze_Formal_Decimal_Fixed_Point_Type;
2176
2177 -------------------------------------------
2178 -- Analyze_Formal_Derived_Interface_Type --
2179 -------------------------------------------
2180
2181 procedure Analyze_Formal_Derived_Interface_Type
2182 (N : Node_Id;
2183 T : Entity_Id;
2184 Def : Node_Id)
2185 is
2186 Loc : constant Source_Ptr := Sloc (Def);
2187
2188 begin
2189 -- Rewrite as a type declaration of a derived type. This ensures that
2190 -- the interface list and primitive operations are properly captured.
2191
2192 Rewrite (N,
2193 Make_Full_Type_Declaration (Loc,
2194 Defining_Identifier => T,
2195 Type_Definition => Def));
2196 Analyze (N);
2197 Set_Is_Generic_Type (T);
2198 end Analyze_Formal_Derived_Interface_Type;
2199
2200 ---------------------------------
2201 -- Analyze_Formal_Derived_Type --
2202 ---------------------------------
2203
2204 procedure Analyze_Formal_Derived_Type
2205 (N : Node_Id;
2206 T : Entity_Id;
2207 Def : Node_Id)
2208 is
2209 Loc : constant Source_Ptr := Sloc (Def);
2210 Unk_Disc : constant Boolean := Unknown_Discriminants_Present (N);
2211 New_N : Node_Id;
2212
2213 begin
2214 Set_Is_Generic_Type (T);
2215
2216 if Private_Present (Def) then
2217 New_N :=
2218 Make_Private_Extension_Declaration (Loc,
2219 Defining_Identifier => T,
2220 Discriminant_Specifications => Discriminant_Specifications (N),
2221 Unknown_Discriminants_Present => Unk_Disc,
2222 Subtype_Indication => Subtype_Mark (Def),
2223 Interface_List => Interface_List (Def));
2224
2225 Set_Abstract_Present (New_N, Abstract_Present (Def));
2226 Set_Limited_Present (New_N, Limited_Present (Def));
2227 Set_Synchronized_Present (New_N, Synchronized_Present (Def));
2228
2229 else
2230 New_N :=
2231 Make_Full_Type_Declaration (Loc,
2232 Defining_Identifier => T,
2233 Discriminant_Specifications =>
2234 Discriminant_Specifications (Parent (T)),
2235 Type_Definition =>
2236 Make_Derived_Type_Definition (Loc,
2237 Subtype_Indication => Subtype_Mark (Def)));
2238
2239 Set_Abstract_Present
2240 (Type_Definition (New_N), Abstract_Present (Def));
2241 Set_Limited_Present
2242 (Type_Definition (New_N), Limited_Present (Def));
2243 end if;
2244
2245 Rewrite (N, New_N);
2246 Analyze (N);
2247
2248 if Unk_Disc then
2249 if not Is_Composite_Type (T) then
2250 Error_Msg_N
2251 ("unknown discriminants not allowed for elementary types", N);
2252 else
2253 Set_Has_Unknown_Discriminants (T);
2254 Set_Is_Constrained (T, False);
2255 end if;
2256 end if;
2257
2258 -- If the parent type has a known size, so does the formal, which makes
2259 -- legal representation clauses that involve the formal.
2260
2261 Set_Size_Known_At_Compile_Time
2262 (T, Size_Known_At_Compile_Time (Entity (Subtype_Mark (Def))));
2263 end Analyze_Formal_Derived_Type;
2264
2265 ----------------------------------
2266 -- Analyze_Formal_Discrete_Type --
2267 ----------------------------------
2268
2269 -- The operations defined for a discrete types are those of an enumeration
2270 -- type. The size is set to an arbitrary value, for use in analyzing the
2271 -- generic unit.
2272
2273 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id) is
2274 Loc : constant Source_Ptr := Sloc (Def);
2275 Lo : Node_Id;
2276 Hi : Node_Id;
2277
2278 Base : constant Entity_Id :=
2279 New_Internal_Entity
2280 (E_Floating_Point_Type, Current_Scope,
2281 Sloc (Defining_Identifier (Parent (Def))), 'G');
2282
2283 begin
2284 Enter_Name (T);
2285 Set_Ekind (T, E_Enumeration_Subtype);
2286 Set_Etype (T, Base);
2287 Init_Size (T, 8);
2288 Init_Alignment (T);
2289 Set_Is_Generic_Type (T);
2290 Set_Is_Constrained (T);
2291
2292 -- For semantic analysis, the bounds of the type must be set to some
2293 -- non-static value. The simplest is to create attribute nodes for those
2294 -- bounds, that refer to the type itself. These bounds are never
2295 -- analyzed but serve as place-holders.
2296
2297 Lo :=
2298 Make_Attribute_Reference (Loc,
2299 Attribute_Name => Name_First,
2300 Prefix => New_Occurrence_Of (T, Loc));
2301 Set_Etype (Lo, T);
2302
2303 Hi :=
2304 Make_Attribute_Reference (Loc,
2305 Attribute_Name => Name_Last,
2306 Prefix => New_Occurrence_Of (T, Loc));
2307 Set_Etype (Hi, T);
2308
2309 Set_Scalar_Range (T,
2310 Make_Range (Loc,
2311 Low_Bound => Lo,
2312 High_Bound => Hi));
2313
2314 Set_Ekind (Base, E_Enumeration_Type);
2315 Set_Etype (Base, Base);
2316 Init_Size (Base, 8);
2317 Init_Alignment (Base);
2318 Set_Is_Generic_Type (Base);
2319 Set_Scalar_Range (Base, Scalar_Range (T));
2320 Set_Parent (Base, Parent (Def));
2321 end Analyze_Formal_Discrete_Type;
2322
2323 ----------------------------------
2324 -- Analyze_Formal_Floating_Type --
2325 ---------------------------------
2326
2327 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id) is
2328 Base : constant Entity_Id :=
2329 New_Internal_Entity
2330 (E_Floating_Point_Type, Current_Scope,
2331 Sloc (Defining_Identifier (Parent (Def))), 'G');
2332
2333 begin
2334 -- The various semantic attributes are taken from the predefined type
2335 -- Float, just so that all of them are initialized. Their values are
2336 -- never used because no constant folding or expansion takes place in
2337 -- the generic itself.
2338
2339 Enter_Name (T);
2340 Set_Ekind (T, E_Floating_Point_Subtype);
2341 Set_Etype (T, Base);
2342 Set_Size_Info (T, (Standard_Float));
2343 Set_RM_Size (T, RM_Size (Standard_Float));
2344 Set_Digits_Value (T, Digits_Value (Standard_Float));
2345 Set_Scalar_Range (T, Scalar_Range (Standard_Float));
2346 Set_Is_Constrained (T);
2347
2348 Set_Is_Generic_Type (Base);
2349 Set_Etype (Base, Base);
2350 Set_Size_Info (Base, (Standard_Float));
2351 Set_RM_Size (Base, RM_Size (Standard_Float));
2352 Set_Digits_Value (Base, Digits_Value (Standard_Float));
2353 Set_Scalar_Range (Base, Scalar_Range (Standard_Float));
2354 Set_Parent (Base, Parent (Def));
2355
2356 Check_Restriction (No_Floating_Point, Def);
2357 end Analyze_Formal_Floating_Type;
2358
2359 -----------------------------------
2360 -- Analyze_Formal_Interface_Type;--
2361 -----------------------------------
2362
2363 procedure Analyze_Formal_Interface_Type
2364 (N : Node_Id;
2365 T : Entity_Id;
2366 Def : Node_Id)
2367 is
2368 Loc : constant Source_Ptr := Sloc (N);
2369 New_N : Node_Id;
2370
2371 begin
2372 New_N :=
2373 Make_Full_Type_Declaration (Loc,
2374 Defining_Identifier => T,
2375 Type_Definition => Def);
2376
2377 Rewrite (N, New_N);
2378 Analyze (N);
2379 Set_Is_Generic_Type (T);
2380 end Analyze_Formal_Interface_Type;
2381
2382 ---------------------------------
2383 -- Analyze_Formal_Modular_Type --
2384 ---------------------------------
2385
2386 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id) is
2387 begin
2388 -- Apart from their entity kind, generic modular types are treated like
2389 -- signed integer types, and have the same attributes.
2390
2391 Analyze_Formal_Signed_Integer_Type (T, Def);
2392 Set_Ekind (T, E_Modular_Integer_Subtype);
2393 Set_Ekind (Etype (T), E_Modular_Integer_Type);
2394
2395 end Analyze_Formal_Modular_Type;
2396
2397 ---------------------------------------
2398 -- Analyze_Formal_Object_Declaration --
2399 ---------------------------------------
2400
2401 procedure Analyze_Formal_Object_Declaration (N : Node_Id) is
2402 E : constant Node_Id := Default_Expression (N);
2403 Id : constant Node_Id := Defining_Identifier (N);
2404 K : Entity_Kind;
2405 T : Node_Id;
2406
2407 begin
2408 Enter_Name (Id);
2409
2410 -- Determine the mode of the formal object
2411
2412 if Out_Present (N) then
2413 K := E_Generic_In_Out_Parameter;
2414
2415 if not In_Present (N) then
2416 Error_Msg_N ("formal generic objects cannot have mode OUT", N);
2417 end if;
2418
2419 else
2420 K := E_Generic_In_Parameter;
2421 end if;
2422
2423 if Present (Subtype_Mark (N)) then
2424 Find_Type (Subtype_Mark (N));
2425 T := Entity (Subtype_Mark (N));
2426
2427 -- Verify that there is no redundant null exclusion
2428
2429 if Null_Exclusion_Present (N) then
2430 if not Is_Access_Type (T) then
2431 Error_Msg_N
2432 ("null exclusion can only apply to an access type", N);
2433
2434 elsif Can_Never_Be_Null (T) then
2435 Error_Msg_NE
2436 ("`NOT NULL` not allowed (& already excludes null)", N, T);
2437 end if;
2438 end if;
2439
2440 -- Ada 2005 (AI-423): Formal object with an access definition
2441
2442 else
2443 Check_Access_Definition (N);
2444 T := Access_Definition
2445 (Related_Nod => N,
2446 N => Access_Definition (N));
2447 end if;
2448
2449 if Ekind (T) = E_Incomplete_Type then
2450 declare
2451 Error_Node : Node_Id;
2452
2453 begin
2454 if Present (Subtype_Mark (N)) then
2455 Error_Node := Subtype_Mark (N);
2456 else
2457 Check_Access_Definition (N);
2458 Error_Node := Access_Definition (N);
2459 end if;
2460
2461 Error_Msg_N ("premature usage of incomplete type", Error_Node);
2462 end;
2463 end if;
2464
2465 if K = E_Generic_In_Parameter then
2466
2467 -- Ada 2005 (AI-287): Limited aggregates allowed in generic formals
2468
2469 if Ada_Version < Ada_2005 and then Is_Limited_Type (T) then
2470 Error_Msg_N
2471 ("generic formal of mode IN must not be of limited type", N);
2472 Explain_Limited_Type (T, N);
2473 end if;
2474
2475 if Is_Abstract_Type (T) then
2476 Error_Msg_N
2477 ("generic formal of mode IN must not be of abstract type", N);
2478 end if;
2479
2480 if Present (E) then
2481 Preanalyze_Spec_Expression (E, T);
2482
2483 if Is_Limited_Type (T) and then not OK_For_Limited_Init (T, E) then
2484 Error_Msg_N
2485 ("initialization not allowed for limited types", E);
2486 Explain_Limited_Type (T, E);
2487 end if;
2488 end if;
2489
2490 Set_Ekind (Id, K);
2491 Set_Etype (Id, T);
2492
2493 -- Case of generic IN OUT parameter
2494
2495 else
2496 -- If the formal has an unconstrained type, construct its actual
2497 -- subtype, as is done for subprogram formals. In this fashion, all
2498 -- its uses can refer to specific bounds.
2499
2500 Set_Ekind (Id, K);
2501 Set_Etype (Id, T);
2502
2503 if (Is_Array_Type (T) and then not Is_Constrained (T))
2504 or else (Ekind (T) = E_Record_Type and then Has_Discriminants (T))
2505 then
2506 declare
2507 Non_Freezing_Ref : constant Node_Id :=
2508 New_Occurrence_Of (Id, Sloc (Id));
2509 Decl : Node_Id;
2510
2511 begin
2512 -- Make sure the actual subtype doesn't generate bogus freezing
2513
2514 Set_Must_Not_Freeze (Non_Freezing_Ref);
2515 Decl := Build_Actual_Subtype (T, Non_Freezing_Ref);
2516 Insert_Before_And_Analyze (N, Decl);
2517 Set_Actual_Subtype (Id, Defining_Identifier (Decl));
2518 end;
2519 else
2520 Set_Actual_Subtype (Id, T);
2521 end if;
2522
2523 if Present (E) then
2524 Error_Msg_N
2525 ("initialization not allowed for `IN OUT` formals", N);
2526 end if;
2527 end if;
2528
2529 if Has_Aspects (N) then
2530 Analyze_Aspect_Specifications (N, Id);
2531 end if;
2532 end Analyze_Formal_Object_Declaration;
2533
2534 ----------------------------------------------
2535 -- Analyze_Formal_Ordinary_Fixed_Point_Type --
2536 ----------------------------------------------
2537
2538 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
2539 (T : Entity_Id;
2540 Def : Node_Id)
2541 is
2542 Loc : constant Source_Ptr := Sloc (Def);
2543 Base : constant Entity_Id :=
2544 New_Internal_Entity
2545 (E_Ordinary_Fixed_Point_Type, Current_Scope,
2546 Sloc (Defining_Identifier (Parent (Def))), 'G');
2547
2548 begin
2549 -- The semantic attributes are set for completeness only, their values
2550 -- will never be used, since all properties of the type are non-static.
2551
2552 Enter_Name (T);
2553 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
2554 Set_Etype (T, Base);
2555 Set_Size_Info (T, Standard_Integer);
2556 Set_RM_Size (T, RM_Size (Standard_Integer));
2557 Set_Small_Value (T, Ureal_1);
2558 Set_Delta_Value (T, Ureal_1);
2559 Set_Scalar_Range (T,
2560 Make_Range (Loc,
2561 Low_Bound => Make_Real_Literal (Loc, Ureal_1),
2562 High_Bound => Make_Real_Literal (Loc, Ureal_1)));
2563 Set_Is_Constrained (T);
2564
2565 Set_Is_Generic_Type (Base);
2566 Set_Etype (Base, Base);
2567 Set_Size_Info (Base, Standard_Integer);
2568 Set_RM_Size (Base, RM_Size (Standard_Integer));
2569 Set_Small_Value (Base, Ureal_1);
2570 Set_Delta_Value (Base, Ureal_1);
2571 Set_Scalar_Range (Base, Scalar_Range (T));
2572 Set_Parent (Base, Parent (Def));
2573
2574 Check_Restriction (No_Fixed_Point, Def);
2575 end Analyze_Formal_Ordinary_Fixed_Point_Type;
2576
2577 ----------------------------------------
2578 -- Analyze_Formal_Package_Declaration --
2579 ----------------------------------------
2580
2581 procedure Analyze_Formal_Package_Declaration (N : Node_Id) is
2582 Gen_Id : constant Node_Id := Name (N);
2583 Loc : constant Source_Ptr := Sloc (N);
2584 Pack_Id : constant Entity_Id := Defining_Identifier (N);
2585 Formal : Entity_Id;
2586 Gen_Decl : Node_Id;
2587 Gen_Unit : Entity_Id;
2588 Renaming : Node_Id;
2589
2590 Vis_Prims_List : Elist_Id := No_Elist;
2591 -- List of primitives made temporarily visible in the instantiation
2592 -- to match the visibility of the formal type.
2593
2594 function Build_Local_Package return Node_Id;
2595 -- The formal package is rewritten so that its parameters are replaced
2596 -- with corresponding declarations. For parameters with bona fide
2597 -- associations these declarations are created by Analyze_Associations
2598 -- as for a regular instantiation. For boxed parameters, we preserve
2599 -- the formal declarations and analyze them, in order to introduce
2600 -- entities of the right kind in the environment of the formal.
2601
2602 -------------------------
2603 -- Build_Local_Package --
2604 -------------------------
2605
2606 function Build_Local_Package return Node_Id is
2607 Decls : List_Id;
2608 Pack_Decl : Node_Id;
2609
2610 begin
2611 -- Within the formal, the name of the generic package is a renaming
2612 -- of the formal (as for a regular instantiation).
2613
2614 Pack_Decl :=
2615 Make_Package_Declaration (Loc,
2616 Specification =>
2617 Copy_Generic_Node
2618 (Specification (Original_Node (Gen_Decl)),
2619 Empty, Instantiating => True));
2620
2621 Renaming :=
2622 Make_Package_Renaming_Declaration (Loc,
2623 Defining_Unit_Name =>
2624 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
2625 Name => New_Occurrence_Of (Formal, Loc));
2626
2627 if Nkind (Gen_Id) = N_Identifier
2628 and then Chars (Gen_Id) = Chars (Pack_Id)
2629 then
2630 Error_Msg_NE
2631 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
2632 end if;
2633
2634 -- If the formal is declared with a box, or with an others choice,
2635 -- create corresponding declarations for all entities in the formal
2636 -- part, so that names with the proper types are available in the
2637 -- specification of the formal package.
2638
2639 -- On the other hand, if there are no associations, then all the
2640 -- formals must have defaults, and this will be checked by the
2641 -- call to Analyze_Associations.
2642
2643 if Box_Present (N)
2644 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2645 then
2646 declare
2647 Formal_Decl : Node_Id;
2648
2649 begin
2650 -- TBA : for a formal package, need to recurse ???
2651
2652 Decls := New_List;
2653 Formal_Decl :=
2654 First
2655 (Generic_Formal_Declarations (Original_Node (Gen_Decl)));
2656 while Present (Formal_Decl) loop
2657 Append_To
2658 (Decls,
2659 Copy_Generic_Node
2660 (Formal_Decl, Empty, Instantiating => True));
2661 Next (Formal_Decl);
2662 end loop;
2663 end;
2664
2665 -- If generic associations are present, use Analyze_Associations to
2666 -- create the proper renaming declarations.
2667
2668 else
2669 declare
2670 Act_Tree : constant Node_Id :=
2671 Copy_Generic_Node
2672 (Original_Node (Gen_Decl), Empty,
2673 Instantiating => True);
2674
2675 begin
2676 Generic_Renamings.Set_Last (0);
2677 Generic_Renamings_HTable.Reset;
2678 Instantiation_Node := N;
2679
2680 Decls :=
2681 Analyze_Associations
2682 (I_Node => Original_Node (N),
2683 Formals => Generic_Formal_Declarations (Act_Tree),
2684 F_Copy => Generic_Formal_Declarations (Gen_Decl));
2685
2686 Vis_Prims_List := Check_Hidden_Primitives (Decls);
2687 end;
2688 end if;
2689
2690 Append (Renaming, To => Decls);
2691
2692 -- Add generated declarations ahead of local declarations in
2693 -- the package.
2694
2695 if No (Visible_Declarations (Specification (Pack_Decl))) then
2696 Set_Visible_Declarations (Specification (Pack_Decl), Decls);
2697 else
2698 Insert_List_Before
2699 (First (Visible_Declarations (Specification (Pack_Decl))),
2700 Decls);
2701 end if;
2702
2703 return Pack_Decl;
2704 end Build_Local_Package;
2705
2706 -- Local variables
2707
2708 Save_ISMP : constant Boolean := Ignore_SPARK_Mode_Pragmas_In_Instance;
2709 -- Save flag Ignore_SPARK_Mode_Pragmas_In_Instance for restore on exit
2710
2711 Associations : Boolean := True;
2712 New_N : Node_Id;
2713 Parent_Installed : Boolean := False;
2714 Parent_Instance : Entity_Id;
2715 Renaming_In_Par : Entity_Id;
2716
2717 -- Start of processing for Analyze_Formal_Package_Declaration
2718
2719 begin
2720 Check_Text_IO_Special_Unit (Gen_Id);
2721
2722 Init_Env;
2723 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
2724 Gen_Unit := Entity (Gen_Id);
2725
2726 -- Check for a formal package that is a package renaming
2727
2728 if Present (Renamed_Object (Gen_Unit)) then
2729
2730 -- Indicate that unit is used, before replacing it with renamed
2731 -- entity for use below.
2732
2733 if In_Extended_Main_Source_Unit (N) then
2734 Set_Is_Instantiated (Gen_Unit);
2735 Generate_Reference (Gen_Unit, N);
2736 end if;
2737
2738 Gen_Unit := Renamed_Object (Gen_Unit);
2739 end if;
2740
2741 if Ekind (Gen_Unit) /= E_Generic_Package then
2742 Error_Msg_N ("expect generic package name", Gen_Id);
2743 Restore_Env;
2744 goto Leave;
2745
2746 elsif Gen_Unit = Current_Scope then
2747 Error_Msg_N
2748 ("generic package cannot be used as a formal package of itself",
2749 Gen_Id);
2750 Restore_Env;
2751 goto Leave;
2752
2753 elsif In_Open_Scopes (Gen_Unit) then
2754 if Is_Compilation_Unit (Gen_Unit)
2755 and then Is_Child_Unit (Current_Scope)
2756 then
2757 -- Special-case the error when the formal is a parent, and
2758 -- continue analysis to minimize cascaded errors.
2759
2760 Error_Msg_N
2761 ("generic parent cannot be used as formal package of a child "
2762 & "unit", Gen_Id);
2763
2764 else
2765 Error_Msg_N
2766 ("generic package cannot be used as a formal package within "
2767 & "itself", Gen_Id);
2768 Restore_Env;
2769 goto Leave;
2770 end if;
2771 end if;
2772
2773 -- Check that name of formal package does not hide name of generic,
2774 -- or its leading prefix. This check must be done separately because
2775 -- the name of the generic has already been analyzed.
2776
2777 declare
2778 Gen_Name : Entity_Id;
2779
2780 begin
2781 Gen_Name := Gen_Id;
2782 while Nkind (Gen_Name) = N_Expanded_Name loop
2783 Gen_Name := Prefix (Gen_Name);
2784 end loop;
2785
2786 if Chars (Gen_Name) = Chars (Pack_Id) then
2787 Error_Msg_NE
2788 ("& is hidden within declaration of formal package",
2789 Gen_Id, Gen_Name);
2790 end if;
2791 end;
2792
2793 if Box_Present (N)
2794 or else No (Generic_Associations (N))
2795 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2796 then
2797 Associations := False;
2798 end if;
2799
2800 -- If there are no generic associations, the generic parameters appear
2801 -- as local entities and are instantiated like them. We copy the generic
2802 -- package declaration as if it were an instantiation, and analyze it
2803 -- like a regular package, except that we treat the formals as
2804 -- additional visible components.
2805
2806 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
2807
2808 if In_Extended_Main_Source_Unit (N) then
2809 Set_Is_Instantiated (Gen_Unit);
2810 Generate_Reference (Gen_Unit, N);
2811 end if;
2812
2813 Formal := New_Copy (Pack_Id);
2814 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
2815
2816 -- Make local generic without formals. The formals will be replaced with
2817 -- internal declarations.
2818
2819 begin
2820 New_N := Build_Local_Package;
2821
2822 -- If there are errors in the parameter list, Analyze_Associations
2823 -- raises Instantiation_Error. Patch the declaration to prevent further
2824 -- exception propagation.
2825
2826 exception
2827 when Instantiation_Error =>
2828 Enter_Name (Formal);
2829 Set_Ekind (Formal, E_Variable);
2830 Set_Etype (Formal, Any_Type);
2831 Restore_Hidden_Primitives (Vis_Prims_List);
2832
2833 if Parent_Installed then
2834 Remove_Parent;
2835 end if;
2836
2837 goto Leave;
2838 end;
2839
2840 Rewrite (N, New_N);
2841 Set_Defining_Unit_Name (Specification (New_N), Formal);
2842 Set_Generic_Parent (Specification (N), Gen_Unit);
2843 Set_Instance_Env (Gen_Unit, Formal);
2844 Set_Is_Generic_Instance (Formal);
2845
2846 Enter_Name (Formal);
2847 Set_Ekind (Formal, E_Package);
2848 Set_Etype (Formal, Standard_Void_Type);
2849 Set_Inner_Instances (Formal, New_Elmt_List);
2850 Push_Scope (Formal);
2851
2852 -- Manually set the SPARK_Mode from the context because the package
2853 -- declaration is never analyzed.
2854
2855 Set_SPARK_Pragma (Formal, SPARK_Mode_Pragma);
2856 Set_SPARK_Aux_Pragma (Formal, SPARK_Mode_Pragma);
2857 Set_SPARK_Pragma_Inherited (Formal);
2858 Set_SPARK_Aux_Pragma_Inherited (Formal);
2859
2860 if Is_Child_Unit (Gen_Unit) and then Parent_Installed then
2861
2862 -- Similarly, we have to make the name of the formal visible in the
2863 -- parent instance, to resolve properly fully qualified names that
2864 -- may appear in the generic unit. The parent instance has been
2865 -- placed on the scope stack ahead of the current scope.
2866
2867 Parent_Instance := Scope_Stack.Table (Scope_Stack.Last - 1).Entity;
2868
2869 Renaming_In_Par :=
2870 Make_Defining_Identifier (Loc, Chars (Gen_Unit));
2871 Set_Ekind (Renaming_In_Par, E_Package);
2872 Set_Etype (Renaming_In_Par, Standard_Void_Type);
2873 Set_Scope (Renaming_In_Par, Parent_Instance);
2874 Set_Parent (Renaming_In_Par, Parent (Formal));
2875 Set_Renamed_Object (Renaming_In_Par, Formal);
2876 Append_Entity (Renaming_In_Par, Parent_Instance);
2877 end if;
2878
2879 -- A formal package declaration behaves as a package instantiation with
2880 -- respect to SPARK_Mode "off". If the annotation is "off" or altogether
2881 -- missing, set the global flag which signals Analyze_Pragma to ingnore
2882 -- all SPARK_Mode pragmas within the generic_package_name.
2883
2884 if SPARK_Mode /= On then
2885 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
2886
2887 -- Mark the formal spec in case the body is instantiated at a later
2888 -- pass. This preserves the original context in effect for the body.
2889
2890 Set_Ignore_SPARK_Mode_Pragmas (Formal);
2891 end if;
2892
2893 Analyze (Specification (N));
2894
2895 -- The formals for which associations are provided are not visible
2896 -- outside of the formal package. The others are still declared by a
2897 -- formal parameter declaration.
2898
2899 -- If there are no associations, the only local entity to hide is the
2900 -- generated package renaming itself.
2901
2902 declare
2903 E : Entity_Id;
2904
2905 begin
2906 E := First_Entity (Formal);
2907 while Present (E) loop
2908 if Associations and then not Is_Generic_Formal (E) then
2909 Set_Is_Hidden (E);
2910 end if;
2911
2912 if Ekind (E) = E_Package and then Renamed_Entity (E) = Formal then
2913 Set_Is_Hidden (E);
2914 exit;
2915 end if;
2916
2917 Next_Entity (E);
2918 end loop;
2919 end;
2920
2921 End_Package_Scope (Formal);
2922 Restore_Hidden_Primitives (Vis_Prims_List);
2923
2924 if Parent_Installed then
2925 Remove_Parent;
2926 end if;
2927
2928 Restore_Env;
2929
2930 -- Inside the generic unit, the formal package is a regular package, but
2931 -- no body is needed for it. Note that after instantiation, the defining
2932 -- unit name we need is in the new tree and not in the original (see
2933 -- Package_Instantiation). A generic formal package is an instance, and
2934 -- can be used as an actual for an inner instance.
2935
2936 Set_Has_Completion (Formal, True);
2937
2938 -- Add semantic information to the original defining identifier for ASIS
2939 -- use.
2940
2941 Set_Ekind (Pack_Id, E_Package);
2942 Set_Etype (Pack_Id, Standard_Void_Type);
2943 Set_Scope (Pack_Id, Scope (Formal));
2944 Set_Has_Completion (Pack_Id, True);
2945
2946 <<Leave>>
2947 if Has_Aspects (N) then
2948 Analyze_Aspect_Specifications (N, Pack_Id);
2949 end if;
2950
2951 Ignore_SPARK_Mode_Pragmas_In_Instance := Save_ISMP;
2952 end Analyze_Formal_Package_Declaration;
2953
2954 ---------------------------------
2955 -- Analyze_Formal_Private_Type --
2956 ---------------------------------
2957
2958 procedure Analyze_Formal_Private_Type
2959 (N : Node_Id;
2960 T : Entity_Id;
2961 Def : Node_Id)
2962 is
2963 begin
2964 New_Private_Type (N, T, Def);
2965
2966 -- Set the size to an arbitrary but legal value
2967
2968 Set_Size_Info (T, Standard_Integer);
2969 Set_RM_Size (T, RM_Size (Standard_Integer));
2970 end Analyze_Formal_Private_Type;
2971
2972 ------------------------------------
2973 -- Analyze_Formal_Incomplete_Type --
2974 ------------------------------------
2975
2976 procedure Analyze_Formal_Incomplete_Type
2977 (T : Entity_Id;
2978 Def : Node_Id)
2979 is
2980 begin
2981 Enter_Name (T);
2982 Set_Ekind (T, E_Incomplete_Type);
2983 Set_Etype (T, T);
2984 Set_Private_Dependents (T, New_Elmt_List);
2985
2986 if Tagged_Present (Def) then
2987 Set_Is_Tagged_Type (T);
2988 Make_Class_Wide_Type (T);
2989 Set_Direct_Primitive_Operations (T, New_Elmt_List);
2990 end if;
2991 end Analyze_Formal_Incomplete_Type;
2992
2993 ----------------------------------------
2994 -- Analyze_Formal_Signed_Integer_Type --
2995 ----------------------------------------
2996
2997 procedure Analyze_Formal_Signed_Integer_Type
2998 (T : Entity_Id;
2999 Def : Node_Id)
3000 is
3001 Base : constant Entity_Id :=
3002 New_Internal_Entity
3003 (E_Signed_Integer_Type,
3004 Current_Scope,
3005 Sloc (Defining_Identifier (Parent (Def))), 'G');
3006
3007 begin
3008 Enter_Name (T);
3009
3010 Set_Ekind (T, E_Signed_Integer_Subtype);
3011 Set_Etype (T, Base);
3012 Set_Size_Info (T, Standard_Integer);
3013 Set_RM_Size (T, RM_Size (Standard_Integer));
3014 Set_Scalar_Range (T, Scalar_Range (Standard_Integer));
3015 Set_Is_Constrained (T);
3016
3017 Set_Is_Generic_Type (Base);
3018 Set_Size_Info (Base, Standard_Integer);
3019 Set_RM_Size (Base, RM_Size (Standard_Integer));
3020 Set_Etype (Base, Base);
3021 Set_Scalar_Range (Base, Scalar_Range (Standard_Integer));
3022 Set_Parent (Base, Parent (Def));
3023 end Analyze_Formal_Signed_Integer_Type;
3024
3025 -------------------------------------------
3026 -- Analyze_Formal_Subprogram_Declaration --
3027 -------------------------------------------
3028
3029 procedure Analyze_Formal_Subprogram_Declaration (N : Node_Id) is
3030 Spec : constant Node_Id := Specification (N);
3031 Def : constant Node_Id := Default_Name (N);
3032 Nam : constant Entity_Id := Defining_Unit_Name (Spec);
3033 Subp : Entity_Id;
3034
3035 begin
3036 if Nam = Error then
3037 return;
3038 end if;
3039
3040 if Nkind (Nam) = N_Defining_Program_Unit_Name then
3041 Error_Msg_N ("name of formal subprogram must be a direct name", Nam);
3042 goto Leave;
3043 end if;
3044
3045 Analyze_Subprogram_Declaration (N);
3046 Set_Is_Formal_Subprogram (Nam);
3047 Set_Has_Completion (Nam);
3048
3049 if Nkind (N) = N_Formal_Abstract_Subprogram_Declaration then
3050 Set_Is_Abstract_Subprogram (Nam);
3051
3052 Set_Is_Dispatching_Operation (Nam);
3053
3054 -- A formal abstract procedure cannot have a null default
3055 -- (RM 12.6(4.1/2)).
3056
3057 if Nkind (Spec) = N_Procedure_Specification
3058 and then Null_Present (Spec)
3059 then
3060 Error_Msg_N
3061 ("a formal abstract subprogram cannot default to null", Spec);
3062 end if;
3063
3064 declare
3065 Ctrl_Type : constant Entity_Id := Find_Dispatching_Type (Nam);
3066 begin
3067 if No (Ctrl_Type) then
3068 Error_Msg_N
3069 ("abstract formal subprogram must have a controlling type",
3070 N);
3071
3072 elsif Ada_Version >= Ada_2012
3073 and then Is_Incomplete_Type (Ctrl_Type)
3074 then
3075 Error_Msg_NE
3076 ("controlling type of abstract formal subprogram cannot "
3077 & "be incomplete type", N, Ctrl_Type);
3078
3079 else
3080 Check_Controlling_Formals (Ctrl_Type, Nam);
3081 end if;
3082 end;
3083 end if;
3084
3085 -- Default name is resolved at the point of instantiation
3086
3087 if Box_Present (N) then
3088 null;
3089
3090 -- Else default is bound at the point of generic declaration
3091
3092 elsif Present (Def) then
3093 if Nkind (Def) = N_Operator_Symbol then
3094 Find_Direct_Name (Def);
3095
3096 elsif Nkind (Def) /= N_Attribute_Reference then
3097 Analyze (Def);
3098
3099 else
3100 -- For an attribute reference, analyze the prefix and verify
3101 -- that it has the proper profile for the subprogram.
3102
3103 Analyze (Prefix (Def));
3104 Valid_Default_Attribute (Nam, Def);
3105 goto Leave;
3106 end if;
3107
3108 -- Default name may be overloaded, in which case the interpretation
3109 -- with the correct profile must be selected, as for a renaming.
3110 -- If the definition is an indexed component, it must denote a
3111 -- member of an entry family. If it is a selected component, it
3112 -- can be a protected operation.
3113
3114 if Etype (Def) = Any_Type then
3115 goto Leave;
3116
3117 elsif Nkind (Def) = N_Selected_Component then
3118 if not Is_Overloadable (Entity (Selector_Name (Def))) then
3119 Error_Msg_N ("expect valid subprogram name as default", Def);
3120 end if;
3121
3122 elsif Nkind (Def) = N_Indexed_Component then
3123 if Is_Entity_Name (Prefix (Def)) then
3124 if Ekind (Entity (Prefix (Def))) /= E_Entry_Family then
3125 Error_Msg_N ("expect valid subprogram name as default", Def);
3126 end if;
3127
3128 elsif Nkind (Prefix (Def)) = N_Selected_Component then
3129 if Ekind (Entity (Selector_Name (Prefix (Def)))) /=
3130 E_Entry_Family
3131 then
3132 Error_Msg_N ("expect valid subprogram name as default", Def);
3133 end if;
3134
3135 else
3136 Error_Msg_N ("expect valid subprogram name as default", Def);
3137 goto Leave;
3138 end if;
3139
3140 elsif Nkind (Def) = N_Character_Literal then
3141
3142 -- Needs some type checks: subprogram should be parameterless???
3143
3144 Resolve (Def, (Etype (Nam)));
3145
3146 elsif not Is_Entity_Name (Def)
3147 or else not Is_Overloadable (Entity (Def))
3148 then
3149 Error_Msg_N ("expect valid subprogram name as default", Def);
3150 goto Leave;
3151
3152 elsif not Is_Overloaded (Def) then
3153 Subp := Entity (Def);
3154
3155 if Subp = Nam then
3156 Error_Msg_N ("premature usage of formal subprogram", Def);
3157
3158 elsif not Entity_Matches_Spec (Subp, Nam) then
3159 Error_Msg_N ("no visible entity matches specification", Def);
3160 end if;
3161
3162 -- More than one interpretation, so disambiguate as for a renaming
3163
3164 else
3165 declare
3166 I : Interp_Index;
3167 I1 : Interp_Index := 0;
3168 It : Interp;
3169 It1 : Interp;
3170
3171 begin
3172 Subp := Any_Id;
3173 Get_First_Interp (Def, I, It);
3174 while Present (It.Nam) loop
3175 if Entity_Matches_Spec (It.Nam, Nam) then
3176 if Subp /= Any_Id then
3177 It1 := Disambiguate (Def, I1, I, Etype (Subp));
3178
3179 if It1 = No_Interp then
3180 Error_Msg_N ("ambiguous default subprogram", Def);
3181 else
3182 Subp := It1.Nam;
3183 end if;
3184
3185 exit;
3186
3187 else
3188 I1 := I;
3189 Subp := It.Nam;
3190 end if;
3191 end if;
3192
3193 Get_Next_Interp (I, It);
3194 end loop;
3195 end;
3196
3197 if Subp /= Any_Id then
3198
3199 -- Subprogram found, generate reference to it
3200
3201 Set_Entity (Def, Subp);
3202 Generate_Reference (Subp, Def);
3203
3204 if Subp = Nam then
3205 Error_Msg_N ("premature usage of formal subprogram", Def);
3206
3207 elsif Ekind (Subp) /= E_Operator then
3208 Check_Mode_Conformant (Subp, Nam);
3209 end if;
3210
3211 else
3212 Error_Msg_N ("no visible subprogram matches specification", N);
3213 end if;
3214 end if;
3215 end if;
3216
3217 <<Leave>>
3218 if Has_Aspects (N) then
3219 Analyze_Aspect_Specifications (N, Nam);
3220 end if;
3221
3222 end Analyze_Formal_Subprogram_Declaration;
3223
3224 -------------------------------------
3225 -- Analyze_Formal_Type_Declaration --
3226 -------------------------------------
3227
3228 procedure Analyze_Formal_Type_Declaration (N : Node_Id) is
3229 Def : constant Node_Id := Formal_Type_Definition (N);
3230 T : Entity_Id;
3231
3232 begin
3233 T := Defining_Identifier (N);
3234
3235 if Present (Discriminant_Specifications (N))
3236 and then Nkind (Def) /= N_Formal_Private_Type_Definition
3237 then
3238 Error_Msg_N
3239 ("discriminants not allowed for this formal type", T);
3240 end if;
3241
3242 -- Enter the new name, and branch to specific routine
3243
3244 case Nkind (Def) is
3245 when N_Formal_Private_Type_Definition =>
3246 Analyze_Formal_Private_Type (N, T, Def);
3247
3248 when N_Formal_Derived_Type_Definition =>
3249 Analyze_Formal_Derived_Type (N, T, Def);
3250
3251 when N_Formal_Incomplete_Type_Definition =>
3252 Analyze_Formal_Incomplete_Type (T, Def);
3253
3254 when N_Formal_Discrete_Type_Definition =>
3255 Analyze_Formal_Discrete_Type (T, Def);
3256
3257 when N_Formal_Signed_Integer_Type_Definition =>
3258 Analyze_Formal_Signed_Integer_Type (T, Def);
3259
3260 when N_Formal_Modular_Type_Definition =>
3261 Analyze_Formal_Modular_Type (T, Def);
3262
3263 when N_Formal_Floating_Point_Definition =>
3264 Analyze_Formal_Floating_Type (T, Def);
3265
3266 when N_Formal_Ordinary_Fixed_Point_Definition =>
3267 Analyze_Formal_Ordinary_Fixed_Point_Type (T, Def);
3268
3269 when N_Formal_Decimal_Fixed_Point_Definition =>
3270 Analyze_Formal_Decimal_Fixed_Point_Type (T, Def);
3271
3272 when N_Array_Type_Definition =>
3273 Analyze_Formal_Array_Type (T, Def);
3274
3275 when N_Access_Function_Definition
3276 | N_Access_Procedure_Definition
3277 | N_Access_To_Object_Definition
3278 =>
3279 Analyze_Generic_Access_Type (T, Def);
3280
3281 -- Ada 2005: a interface declaration is encoded as an abstract
3282 -- record declaration or a abstract type derivation.
3283
3284 when N_Record_Definition =>
3285 Analyze_Formal_Interface_Type (N, T, Def);
3286
3287 when N_Derived_Type_Definition =>
3288 Analyze_Formal_Derived_Interface_Type (N, T, Def);
3289
3290 when N_Error =>
3291 null;
3292
3293 when others =>
3294 raise Program_Error;
3295 end case;
3296
3297 Set_Is_Generic_Type (T);
3298
3299 if Has_Aspects (N) then
3300 Analyze_Aspect_Specifications (N, T);
3301 end if;
3302 end Analyze_Formal_Type_Declaration;
3303
3304 ------------------------------------
3305 -- Analyze_Function_Instantiation --
3306 ------------------------------------
3307
3308 procedure Analyze_Function_Instantiation (N : Node_Id) is
3309 begin
3310 Analyze_Subprogram_Instantiation (N, E_Function);
3311 end Analyze_Function_Instantiation;
3312
3313 ---------------------------------
3314 -- Analyze_Generic_Access_Type --
3315 ---------------------------------
3316
3317 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id) is
3318 begin
3319 Enter_Name (T);
3320
3321 if Nkind (Def) = N_Access_To_Object_Definition then
3322 Access_Type_Declaration (T, Def);
3323
3324 if Is_Incomplete_Or_Private_Type (Designated_Type (T))
3325 and then No (Full_View (Designated_Type (T)))
3326 and then not Is_Generic_Type (Designated_Type (T))
3327 then
3328 Error_Msg_N ("premature usage of incomplete type", Def);
3329
3330 elsif not Is_Entity_Name (Subtype_Indication (Def)) then
3331 Error_Msg_N
3332 ("only a subtype mark is allowed in a formal", Def);
3333 end if;
3334
3335 else
3336 Access_Subprogram_Declaration (T, Def);
3337 end if;
3338 end Analyze_Generic_Access_Type;
3339
3340 ---------------------------------
3341 -- Analyze_Generic_Formal_Part --
3342 ---------------------------------
3343
3344 procedure Analyze_Generic_Formal_Part (N : Node_Id) is
3345 Gen_Parm_Decl : Node_Id;
3346
3347 begin
3348 -- The generic formals are processed in the scope of the generic unit,
3349 -- where they are immediately visible. The scope is installed by the
3350 -- caller.
3351
3352 Gen_Parm_Decl := First (Generic_Formal_Declarations (N));
3353 while Present (Gen_Parm_Decl) loop
3354 Analyze (Gen_Parm_Decl);
3355 Next (Gen_Parm_Decl);
3356 end loop;
3357
3358 Generate_Reference_To_Generic_Formals (Current_Scope);
3359 end Analyze_Generic_Formal_Part;
3360
3361 ------------------------------------------
3362 -- Analyze_Generic_Package_Declaration --
3363 ------------------------------------------
3364
3365 procedure Analyze_Generic_Package_Declaration (N : Node_Id) is
3366 Loc : constant Source_Ptr := Sloc (N);
3367 Decls : constant List_Id :=
3368 Visible_Declarations (Specification (N));
3369 Decl : Node_Id;
3370 Id : Entity_Id;
3371 New_N : Node_Id;
3372 Renaming : Node_Id;
3373 Save_Parent : Node_Id;
3374
3375 begin
3376 Check_SPARK_05_Restriction ("generic is not allowed", N);
3377
3378 -- We introduce a renaming of the enclosing package, to have a usable
3379 -- entity as the prefix of an expanded name for a local entity of the
3380 -- form Par.P.Q, where P is the generic package. This is because a local
3381 -- entity named P may hide it, so that the usual visibility rules in
3382 -- the instance will not resolve properly.
3383
3384 Renaming :=
3385 Make_Package_Renaming_Declaration (Loc,
3386 Defining_Unit_Name =>
3387 Make_Defining_Identifier (Loc,
3388 Chars => New_External_Name (Chars (Defining_Entity (N)), "GH")),
3389 Name =>
3390 Make_Identifier (Loc, Chars (Defining_Entity (N))));
3391
3392 if Present (Decls) then
3393 Decl := First (Decls);
3394 while Present (Decl) and then Nkind (Decl) = N_Pragma loop
3395 Next (Decl);
3396 end loop;
3397
3398 if Present (Decl) then
3399 Insert_Before (Decl, Renaming);
3400 else
3401 Append (Renaming, Visible_Declarations (Specification (N)));
3402 end if;
3403
3404 else
3405 Set_Visible_Declarations (Specification (N), New_List (Renaming));
3406 end if;
3407
3408 -- Create copy of generic unit, and save for instantiation. If the unit
3409 -- is a child unit, do not copy the specifications for the parent, which
3410 -- are not part of the generic tree.
3411
3412 Save_Parent := Parent_Spec (N);
3413 Set_Parent_Spec (N, Empty);
3414
3415 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
3416 Set_Parent_Spec (New_N, Save_Parent);
3417 Rewrite (N, New_N);
3418
3419 -- Once the contents of the generic copy and the template are swapped,
3420 -- do the same for their respective aspect specifications.
3421
3422 Exchange_Aspects (N, New_N);
3423
3424 -- Collect all contract-related source pragmas found within the template
3425 -- and attach them to the contract of the package spec. This contract is
3426 -- used in the capture of global references within annotations.
3427
3428 Create_Generic_Contract (N);
3429
3430 Id := Defining_Entity (N);
3431 Generate_Definition (Id);
3432
3433 -- Expansion is not applied to generic units
3434
3435 Start_Generic;
3436
3437 Enter_Name (Id);
3438 Set_Ekind (Id, E_Generic_Package);
3439 Set_Etype (Id, Standard_Void_Type);
3440
3441 -- Set SPARK_Mode from context
3442
3443 Set_SPARK_Pragma (Id, SPARK_Mode_Pragma);
3444 Set_SPARK_Aux_Pragma (Id, SPARK_Mode_Pragma);
3445 Set_SPARK_Pragma_Inherited (Id);
3446 Set_SPARK_Aux_Pragma_Inherited (Id);
3447
3448 -- Analyze aspects now, so that generated pragmas appear in the
3449 -- declarations before building and analyzing the generic copy.
3450
3451 if Has_Aspects (N) then
3452 Analyze_Aspect_Specifications (N, Id);
3453 end if;
3454
3455 Push_Scope (Id);
3456 Enter_Generic_Scope (Id);
3457 Set_Inner_Instances (Id, New_Elmt_List);
3458
3459 Set_Categorization_From_Pragmas (N);
3460 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3461
3462 -- Link the declaration of the generic homonym in the generic copy to
3463 -- the package it renames, so that it is always resolved properly.
3464
3465 Set_Generic_Homonym (Id, Defining_Unit_Name (Renaming));
3466 Set_Entity (Associated_Node (Name (Renaming)), Id);
3467
3468 -- For a library unit, we have reconstructed the entity for the unit,
3469 -- and must reset it in the library tables.
3470
3471 if Nkind (Parent (N)) = N_Compilation_Unit then
3472 Set_Cunit_Entity (Current_Sem_Unit, Id);
3473 end if;
3474
3475 Analyze_Generic_Formal_Part (N);
3476
3477 -- After processing the generic formals, analysis proceeds as for a
3478 -- non-generic package.
3479
3480 Analyze (Specification (N));
3481
3482 Validate_Categorization_Dependency (N, Id);
3483
3484 End_Generic;
3485
3486 End_Package_Scope (Id);
3487 Exit_Generic_Scope (Id);
3488
3489 -- If the generic appears within a package unit, the body of that unit
3490 -- has to be present for instantiation and inlining.
3491
3492 if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Declaration then
3493 Set_Body_Needed_For_Inlining
3494 (Defining_Entity (Unit (Cunit (Current_Sem_Unit))));
3495 end if;
3496
3497 if Nkind (Parent (N)) /= N_Compilation_Unit then
3498 Move_Freeze_Nodes (Id, N, Visible_Declarations (Specification (N)));
3499 Move_Freeze_Nodes (Id, N, Private_Declarations (Specification (N)));
3500 Move_Freeze_Nodes (Id, N, Generic_Formal_Declarations (N));
3501
3502 else
3503 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
3504 Validate_RT_RAT_Component (N);
3505
3506 -- If this is a spec without a body, check that generic parameters
3507 -- are referenced.
3508
3509 if not Body_Required (Parent (N)) then
3510 Check_References (Id);
3511 end if;
3512 end if;
3513
3514 -- If there is a specified storage pool in the context, create an
3515 -- aspect on the package declaration, so that it is used in any
3516 -- instance that does not override it.
3517
3518 if Present (Default_Pool) then
3519 declare
3520 ASN : Node_Id;
3521
3522 begin
3523 ASN :=
3524 Make_Aspect_Specification (Loc,
3525 Identifier => Make_Identifier (Loc, Name_Default_Storage_Pool),
3526 Expression => New_Copy (Default_Pool));
3527
3528 if No (Aspect_Specifications (Specification (N))) then
3529 Set_Aspect_Specifications (Specification (N), New_List (ASN));
3530 else
3531 Append (ASN, Aspect_Specifications (Specification (N)));
3532 end if;
3533 end;
3534 end if;
3535 end Analyze_Generic_Package_Declaration;
3536
3537 --------------------------------------------
3538 -- Analyze_Generic_Subprogram_Declaration --
3539 --------------------------------------------
3540
3541 procedure Analyze_Generic_Subprogram_Declaration (N : Node_Id) is
3542 Formals : List_Id;
3543 Id : Entity_Id;
3544 New_N : Node_Id;
3545 Result_Type : Entity_Id;
3546 Save_Parent : Node_Id;
3547 Spec : Node_Id;
3548 Typ : Entity_Id;
3549
3550 begin
3551 Check_SPARK_05_Restriction ("generic is not allowed", N);
3552
3553 -- Create copy of generic unit, and save for instantiation. If the unit
3554 -- is a child unit, do not copy the specifications for the parent, which
3555 -- are not part of the generic tree.
3556
3557 Save_Parent := Parent_Spec (N);
3558 Set_Parent_Spec (N, Empty);
3559
3560 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
3561 Set_Parent_Spec (New_N, Save_Parent);
3562 Rewrite (N, New_N);
3563
3564 -- Once the contents of the generic copy and the template are swapped,
3565 -- do the same for their respective aspect specifications.
3566
3567 Exchange_Aspects (N, New_N);
3568
3569 -- Collect all contract-related source pragmas found within the template
3570 -- and attach them to the contract of the subprogram spec. This contract
3571 -- is used in the capture of global references within annotations.
3572
3573 Create_Generic_Contract (N);
3574
3575 Spec := Specification (N);
3576 Id := Defining_Entity (Spec);
3577 Generate_Definition (Id);
3578
3579 if Nkind (Id) = N_Defining_Operator_Symbol then
3580 Error_Msg_N
3581 ("operator symbol not allowed for generic subprogram", Id);
3582 end if;
3583
3584 Start_Generic;
3585
3586 Enter_Name (Id);
3587 Set_Scope_Depth_Value (Id, Scope_Depth (Current_Scope) + 1);
3588
3589 -- Analyze the aspects of the generic copy to ensure that all generated
3590 -- pragmas (if any) perform their semantic effects.
3591
3592 if Has_Aspects (N) then
3593 Analyze_Aspect_Specifications (N, Id);
3594 end if;
3595
3596 Push_Scope (Id);
3597 Enter_Generic_Scope (Id);
3598 Set_Inner_Instances (Id, New_Elmt_List);
3599 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3600
3601 Analyze_Generic_Formal_Part (N);
3602
3603 Formals := Parameter_Specifications (Spec);
3604
3605 if Nkind (Spec) = N_Function_Specification then
3606 Set_Ekind (Id, E_Generic_Function);
3607 else
3608 Set_Ekind (Id, E_Generic_Procedure);
3609 end if;
3610
3611 if Present (Formals) then
3612 Process_Formals (Formals, Spec);
3613 end if;
3614
3615 if Nkind (Spec) = N_Function_Specification then
3616 if Nkind (Result_Definition (Spec)) = N_Access_Definition then
3617 Result_Type := Access_Definition (Spec, Result_Definition (Spec));
3618 Set_Etype (Id, Result_Type);
3619
3620 -- Check restriction imposed by AI05-073: a generic function
3621 -- cannot return an abstract type or an access to such.
3622
3623 -- This is a binding interpretation should it apply to earlier
3624 -- versions of Ada as well as Ada 2012???
3625
3626 if Is_Abstract_Type (Designated_Type (Result_Type))
3627 and then Ada_Version >= Ada_2012
3628 then
3629 Error_Msg_N
3630 ("generic function cannot have an access result "
3631 & "that designates an abstract type", Spec);
3632 end if;
3633
3634 else
3635 Find_Type (Result_Definition (Spec));
3636 Typ := Entity (Result_Definition (Spec));
3637
3638 if Is_Abstract_Type (Typ)
3639 and then Ada_Version >= Ada_2012
3640 then
3641 Error_Msg_N
3642 ("generic function cannot have abstract result type", Spec);
3643 end if;
3644
3645 -- If a null exclusion is imposed on the result type, then create
3646 -- a null-excluding itype (an access subtype) and use it as the
3647 -- function's Etype.
3648
3649 if Is_Access_Type (Typ)
3650 and then Null_Exclusion_Present (Spec)
3651 then
3652 Set_Etype (Id,
3653 Create_Null_Excluding_Itype
3654 (T => Typ,
3655 Related_Nod => Spec,
3656 Scope_Id => Defining_Unit_Name (Spec)));
3657 else
3658 Set_Etype (Id, Typ);
3659 end if;
3660 end if;
3661
3662 else
3663 Set_Etype (Id, Standard_Void_Type);
3664 end if;
3665
3666 -- For a library unit, we have reconstructed the entity for the unit,
3667 -- and must reset it in the library tables. We also make sure that
3668 -- Body_Required is set properly in the original compilation unit node.
3669
3670 if Nkind (Parent (N)) = N_Compilation_Unit then
3671 Set_Cunit_Entity (Current_Sem_Unit, Id);
3672 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
3673 end if;
3674
3675 -- If the generic appears within a package unit, the body of that unit
3676 -- has to be present for instantiation and inlining.
3677
3678 if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Declaration
3679 and then Unit_Requires_Body (Id)
3680 then
3681 Set_Body_Needed_For_Inlining
3682 (Defining_Entity (Unit (Cunit (Current_Sem_Unit))));
3683 end if;
3684
3685 Set_Categorization_From_Pragmas (N);
3686 Validate_Categorization_Dependency (N, Id);
3687
3688 -- Capture all global references that occur within the profile of the
3689 -- generic subprogram. Aspects are not part of this processing because
3690 -- they must be delayed. If processed now, Save_Global_References will
3691 -- destroy the Associated_Node links and prevent the capture of global
3692 -- references when the contract of the generic subprogram is analyzed.
3693
3694 Save_Global_References (Original_Node (N));
3695
3696 End_Generic;
3697 End_Scope;
3698 Exit_Generic_Scope (Id);
3699 Generate_Reference_To_Formals (Id);
3700
3701 List_Inherited_Pre_Post_Aspects (Id);
3702 end Analyze_Generic_Subprogram_Declaration;
3703
3704 -----------------------------------
3705 -- Analyze_Package_Instantiation --
3706 -----------------------------------
3707
3708 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
3709 -- must be replaced by gotos which jump to the end of the routine in order
3710 -- to restore the Ghost and SPARK modes.
3711
3712 procedure Analyze_Package_Instantiation (N : Node_Id) is
3713 Has_Inline_Always : Boolean := False;
3714
3715 procedure Delay_Descriptors (E : Entity_Id);
3716 -- Delay generation of subprogram descriptors for given entity
3717
3718 function Might_Inline_Subp (Gen_Unit : Entity_Id) return Boolean;
3719 -- If inlining is active and the generic contains inlined subprograms,
3720 -- we instantiate the body. This may cause superfluous instantiations,
3721 -- but it is simpler than detecting the need for the body at the point
3722 -- of inlining, when the context of the instance is not available.
3723
3724 -----------------------
3725 -- Delay_Descriptors --
3726 -----------------------
3727
3728 procedure Delay_Descriptors (E : Entity_Id) is
3729 begin
3730 if not Delay_Subprogram_Descriptors (E) then
3731 Set_Delay_Subprogram_Descriptors (E);
3732 Pending_Descriptor.Append (E);
3733 end if;
3734 end Delay_Descriptors;
3735
3736 -----------------------
3737 -- Might_Inline_Subp --
3738 -----------------------
3739
3740 function Might_Inline_Subp (Gen_Unit : Entity_Id) return Boolean is
3741 E : Entity_Id;
3742
3743 begin
3744 if not Inline_Processing_Required then
3745 return False;
3746
3747 else
3748 E := First_Entity (Gen_Unit);
3749 while Present (E) loop
3750 if Is_Subprogram (E) and then Is_Inlined (E) then
3751 -- Remember if there are any subprograms with Inline_Always
3752
3753 if Has_Pragma_Inline_Always (E) then
3754 Has_Inline_Always := True;
3755 end if;
3756
3757 return True;
3758 end if;
3759
3760 Next_Entity (E);
3761 end loop;
3762 end if;
3763
3764 return False;
3765 end Might_Inline_Subp;
3766
3767 -- Local declarations
3768
3769 Gen_Id : constant Node_Id := Name (N);
3770 Is_Actual_Pack : constant Boolean :=
3771 Is_Internal (Defining_Entity (N));
3772 Loc : constant Source_Ptr := Sloc (N);
3773
3774 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
3775 Saved_ISMP : constant Boolean :=
3776 Ignore_SPARK_Mode_Pragmas_In_Instance;
3777 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
3778 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
3779 -- Save the Ghost and SPARK mode-related data to restore on exit
3780
3781 Saved_Style_Check : constant Boolean := Style_Check;
3782 -- Save style check mode for restore on exit
3783
3784 Act_Decl : Node_Id;
3785 Act_Decl_Name : Node_Id;
3786 Act_Decl_Id : Entity_Id;
3787 Act_Spec : Node_Id;
3788 Act_Tree : Node_Id;
3789 Env_Installed : Boolean := False;
3790 Gen_Decl : Node_Id;
3791 Gen_Spec : Node_Id;
3792 Gen_Unit : Entity_Id;
3793 Inline_Now : Boolean := False;
3794 Needs_Body : Boolean;
3795 Parent_Installed : Boolean := False;
3796 Renaming_List : List_Id;
3797 Unit_Renaming : Node_Id;
3798
3799 Vis_Prims_List : Elist_Id := No_Elist;
3800 -- List of primitives made temporarily visible in the instantiation
3801 -- to match the visibility of the formal type
3802
3803 -- Start of processing for Analyze_Package_Instantiation
3804
3805 begin
3806 Check_SPARK_05_Restriction ("generic is not allowed", N);
3807
3808 -- Very first thing: check for Text_IO special unit in case we are
3809 -- instantiating one of the children of [[Wide_]Wide_]Text_IO.
3810
3811 Check_Text_IO_Special_Unit (Name (N));
3812
3813 -- Make node global for error reporting
3814
3815 Instantiation_Node := N;
3816
3817 -- Case of instantiation of a generic package
3818
3819 if Nkind (N) = N_Package_Instantiation then
3820 Act_Decl_Id := New_Copy (Defining_Entity (N));
3821 Set_Comes_From_Source (Act_Decl_Id, True);
3822
3823 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name then
3824 Act_Decl_Name :=
3825 Make_Defining_Program_Unit_Name (Loc,
3826 Name =>
3827 New_Copy_Tree (Name (Defining_Unit_Name (N))),
3828 Defining_Identifier => Act_Decl_Id);
3829 else
3830 Act_Decl_Name := Act_Decl_Id;
3831 end if;
3832
3833 -- Case of instantiation of a formal package
3834
3835 else
3836 Act_Decl_Id := Defining_Identifier (N);
3837 Act_Decl_Name := Act_Decl_Id;
3838 end if;
3839
3840 Generate_Definition (Act_Decl_Id);
3841 Set_Ekind (Act_Decl_Id, E_Package);
3842
3843 -- Initialize list of incomplete actuals before analysis
3844
3845 Set_Incomplete_Actuals (Act_Decl_Id, New_Elmt_List);
3846
3847 Preanalyze_Actuals (N, Act_Decl_Id);
3848
3849 -- Turn off style checking in instances. If the check is enabled on the
3850 -- generic unit, a warning in an instance would just be noise. If not
3851 -- enabled on the generic, then a warning in an instance is just wrong.
3852 -- This must be done after analyzing the actuals, which do come from
3853 -- source and are subject to style checking.
3854
3855 Style_Check := False;
3856
3857 Init_Env;
3858 Env_Installed := True;
3859
3860 -- Reset renaming map for formal types. The mapping is established
3861 -- when analyzing the generic associations, but some mappings are
3862 -- inherited from formal packages of parent units, and these are
3863 -- constructed when the parents are installed.
3864
3865 Generic_Renamings.Set_Last (0);
3866 Generic_Renamings_HTable.Reset;
3867
3868 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
3869 Gen_Unit := Entity (Gen_Id);
3870
3871 -- A package instantiation is Ghost when it is subject to pragma Ghost
3872 -- or the generic template is Ghost. Set the mode now to ensure that
3873 -- any nodes generated during analysis and expansion are marked as
3874 -- Ghost.
3875
3876 Mark_And_Set_Ghost_Instantiation (N, Gen_Unit);
3877
3878 -- Verify that it is the name of a generic package
3879
3880 -- A visibility glitch: if the instance is a child unit and the generic
3881 -- is the generic unit of a parent instance (i.e. both the parent and
3882 -- the child units are instances of the same package) the name now
3883 -- denotes the renaming within the parent, not the intended generic
3884 -- unit. See if there is a homonym that is the desired generic. The
3885 -- renaming declaration must be visible inside the instance of the
3886 -- child, but not when analyzing the name in the instantiation itself.
3887
3888 if Ekind (Gen_Unit) = E_Package
3889 and then Present (Renamed_Entity (Gen_Unit))
3890 and then In_Open_Scopes (Renamed_Entity (Gen_Unit))
3891 and then Is_Generic_Instance (Renamed_Entity (Gen_Unit))
3892 and then Present (Homonym (Gen_Unit))
3893 then
3894 Gen_Unit := Homonym (Gen_Unit);
3895 end if;
3896
3897 if Etype (Gen_Unit) = Any_Type then
3898 Restore_Env;
3899 goto Leave;
3900
3901 elsif Ekind (Gen_Unit) /= E_Generic_Package then
3902
3903 -- Ada 2005 (AI-50217): Cannot use instance in limited with_clause
3904
3905 if From_Limited_With (Gen_Unit) then
3906 Error_Msg_N
3907 ("cannot instantiate a limited withed package", Gen_Id);
3908 else
3909 Error_Msg_NE
3910 ("& is not the name of a generic package", Gen_Id, Gen_Unit);
3911 end if;
3912
3913 Restore_Env;
3914 goto Leave;
3915 end if;
3916
3917 if In_Extended_Main_Source_Unit (N) then
3918 Set_Is_Instantiated (Gen_Unit);
3919 Generate_Reference (Gen_Unit, N);
3920
3921 if Present (Renamed_Object (Gen_Unit)) then
3922 Set_Is_Instantiated (Renamed_Object (Gen_Unit));
3923 Generate_Reference (Renamed_Object (Gen_Unit), N);
3924 end if;
3925 end if;
3926
3927 if Nkind (Gen_Id) = N_Identifier
3928 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
3929 then
3930 Error_Msg_NE
3931 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
3932
3933 elsif Nkind (Gen_Id) = N_Expanded_Name
3934 and then Is_Child_Unit (Gen_Unit)
3935 and then Nkind (Prefix (Gen_Id)) = N_Identifier
3936 and then Chars (Act_Decl_Id) = Chars (Prefix (Gen_Id))
3937 then
3938 Error_Msg_N
3939 ("& is hidden within declaration of instance ", Prefix (Gen_Id));
3940 end if;
3941
3942 Set_Entity (Gen_Id, Gen_Unit);
3943
3944 -- If generic is a renaming, get original generic unit
3945
3946 if Present (Renamed_Object (Gen_Unit))
3947 and then Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Package
3948 then
3949 Gen_Unit := Renamed_Object (Gen_Unit);
3950 end if;
3951
3952 -- Verify that there are no circular instantiations
3953
3954 if In_Open_Scopes (Gen_Unit) then
3955 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
3956 Restore_Env;
3957 goto Leave;
3958
3959 elsif Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
3960 Error_Msg_Node_2 := Current_Scope;
3961 Error_Msg_NE
3962 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
3963 Circularity_Detected := True;
3964 Restore_Env;
3965 goto Leave;
3966
3967 else
3968 -- If the context of the instance is subject to SPARK_Mode "off" or
3969 -- the annotation is altogether missing, set the global flag which
3970 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
3971 -- the instance.
3972
3973 if SPARK_Mode /= On then
3974 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
3975
3976 -- Mark the instance spec in case the body is instantiated at a
3977 -- later pass. This preserves the original context in effect for
3978 -- the body.
3979
3980 Set_Ignore_SPARK_Mode_Pragmas (Act_Decl_Id);
3981 end if;
3982
3983 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
3984 Gen_Spec := Specification (Gen_Decl);
3985
3986 -- Initialize renamings map, for error checking, and the list that
3987 -- holds private entities whose views have changed between generic
3988 -- definition and instantiation. If this is the instance created to
3989 -- validate an actual package, the instantiation environment is that
3990 -- of the enclosing instance.
3991
3992 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
3993
3994 -- Copy original generic tree, to produce text for instantiation
3995
3996 Act_Tree :=
3997 Copy_Generic_Node
3998 (Original_Node (Gen_Decl), Empty, Instantiating => True);
3999
4000 Act_Spec := Specification (Act_Tree);
4001
4002 -- If this is the instance created to validate an actual package,
4003 -- only the formals matter, do not examine the package spec itself.
4004
4005 if Is_Actual_Pack then
4006 Set_Visible_Declarations (Act_Spec, New_List);
4007 Set_Private_Declarations (Act_Spec, New_List);
4008 end if;
4009
4010 Renaming_List :=
4011 Analyze_Associations
4012 (I_Node => N,
4013 Formals => Generic_Formal_Declarations (Act_Tree),
4014 F_Copy => Generic_Formal_Declarations (Gen_Decl));
4015
4016 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
4017
4018 Set_Instance_Env (Gen_Unit, Act_Decl_Id);
4019 Set_Defining_Unit_Name (Act_Spec, Act_Decl_Name);
4020 Set_Is_Generic_Instance (Act_Decl_Id);
4021 Set_Generic_Parent (Act_Spec, Gen_Unit);
4022
4023 -- References to the generic in its own declaration or its body are
4024 -- references to the instance. Add a renaming declaration for the
4025 -- generic unit itself. This declaration, as well as the renaming
4026 -- declarations for the generic formals, must remain private to the
4027 -- unit: the formals, because this is the language semantics, and
4028 -- the unit because its use is an artifact of the implementation.
4029
4030 Unit_Renaming :=
4031 Make_Package_Renaming_Declaration (Loc,
4032 Defining_Unit_Name =>
4033 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
4034 Name => New_Occurrence_Of (Act_Decl_Id, Loc));
4035
4036 Append (Unit_Renaming, Renaming_List);
4037
4038 -- The renaming declarations are the first local declarations of the
4039 -- new unit.
4040
4041 if Is_Non_Empty_List (Visible_Declarations (Act_Spec)) then
4042 Insert_List_Before
4043 (First (Visible_Declarations (Act_Spec)), Renaming_List);
4044 else
4045 Set_Visible_Declarations (Act_Spec, Renaming_List);
4046 end if;
4047
4048 Act_Decl := Make_Package_Declaration (Loc, Specification => Act_Spec);
4049
4050 -- Propagate the aspect specifications from the package declaration
4051 -- template to the instantiated version of the package declaration.
4052
4053 if Has_Aspects (Act_Tree) then
4054 Set_Aspect_Specifications (Act_Decl,
4055 New_Copy_List_Tree (Aspect_Specifications (Act_Tree)));
4056 end if;
4057
4058 -- The generic may have a generated Default_Storage_Pool aspect,
4059 -- set at the point of generic declaration. If the instance has
4060 -- that aspect, it overrides the one inherited from the generic.
4061
4062 if Has_Aspects (Gen_Spec) then
4063 if No (Aspect_Specifications (N)) then
4064 Set_Aspect_Specifications (N,
4065 (New_Copy_List_Tree
4066 (Aspect_Specifications (Gen_Spec))));
4067
4068 else
4069 declare
4070 ASN1, ASN2 : Node_Id;
4071
4072 begin
4073 ASN1 := First (Aspect_Specifications (N));
4074 while Present (ASN1) loop
4075 if Chars (Identifier (ASN1)) = Name_Default_Storage_Pool
4076 then
4077 -- If generic carries a default storage pool, remove
4078 -- it in favor of the instance one.
4079
4080 ASN2 := First (Aspect_Specifications (Gen_Spec));
4081 while Present (ASN2) loop
4082 if Chars (Identifier (ASN2)) =
4083 Name_Default_Storage_Pool
4084 then
4085 Remove (ASN2);
4086 exit;
4087 end if;
4088
4089 Next (ASN2);
4090 end loop;
4091 end if;
4092
4093 Next (ASN1);
4094 end loop;
4095
4096 Prepend_List_To (Aspect_Specifications (N),
4097 (New_Copy_List_Tree
4098 (Aspect_Specifications (Gen_Spec))));
4099 end;
4100 end if;
4101 end if;
4102
4103 -- Save the instantiation node, for subsequent instantiation of the
4104 -- body, if there is one and we are generating code for the current
4105 -- unit. Mark unit as having a body (avoids premature error message).
4106
4107 -- We instantiate the body if we are generating code, if we are
4108 -- generating cross-reference information, or if we are building
4109 -- trees for ASIS use or GNATprove use.
4110
4111 declare
4112 Enclosing_Body_Present : Boolean := False;
4113 -- If the generic unit is not a compilation unit, then a body may
4114 -- be present in its parent even if none is required. We create a
4115 -- tentative pending instantiation for the body, which will be
4116 -- discarded if none is actually present.
4117
4118 Scop : Entity_Id;
4119
4120 begin
4121 if Scope (Gen_Unit) /= Standard_Standard
4122 and then not Is_Child_Unit (Gen_Unit)
4123 then
4124 Scop := Scope (Gen_Unit);
4125 while Present (Scop) and then Scop /= Standard_Standard loop
4126 if Unit_Requires_Body (Scop) then
4127 Enclosing_Body_Present := True;
4128 exit;
4129
4130 elsif In_Open_Scopes (Scop)
4131 and then In_Package_Body (Scop)
4132 then
4133 Enclosing_Body_Present := True;
4134 exit;
4135 end if;
4136
4137 exit when Is_Compilation_Unit (Scop);
4138 Scop := Scope (Scop);
4139 end loop;
4140 end if;
4141
4142 -- If front-end inlining is enabled or there are any subprograms
4143 -- marked with Inline_Always, and this is a unit for which code
4144 -- will be generated, we instantiate the body at once.
4145
4146 -- This is done if the instance is not the main unit, and if the
4147 -- generic is not a child unit of another generic, to avoid scope
4148 -- problems and the reinstallation of parent instances.
4149
4150 if Expander_Active
4151 and then (not Is_Child_Unit (Gen_Unit)
4152 or else not Is_Generic_Unit (Scope (Gen_Unit)))
4153 and then Might_Inline_Subp (Gen_Unit)
4154 and then not Is_Actual_Pack
4155 then
4156 if not Back_End_Inlining
4157 and then (Front_End_Inlining or else Has_Inline_Always)
4158 and then (Is_In_Main_Unit (N)
4159 or else In_Main_Context (Current_Scope))
4160 and then Nkind (Parent (N)) /= N_Compilation_Unit
4161 then
4162 Inline_Now := True;
4163
4164 -- In configurable_run_time mode we force the inlining of
4165 -- predefined subprograms marked Inline_Always, to minimize
4166 -- the use of the run-time library.
4167
4168 elsif In_Predefined_Unit (Gen_Decl)
4169 and then Configurable_Run_Time_Mode
4170 and then Nkind (Parent (N)) /= N_Compilation_Unit
4171 then
4172 Inline_Now := True;
4173 end if;
4174
4175 -- If the current scope is itself an instance within a child
4176 -- unit, there will be duplications in the scope stack, and the
4177 -- unstacking mechanism in Inline_Instance_Body will fail.
4178 -- This loses some rare cases of optimization, and might be
4179 -- improved some day, if we can find a proper abstraction for
4180 -- "the complete compilation context" that can be saved and
4181 -- restored. ???
4182
4183 if Is_Generic_Instance (Current_Scope) then
4184 declare
4185 Curr_Unit : constant Entity_Id :=
4186 Cunit_Entity (Current_Sem_Unit);
4187 begin
4188 if Curr_Unit /= Current_Scope
4189 and then Is_Child_Unit (Curr_Unit)
4190 then
4191 Inline_Now := False;
4192 end if;
4193 end;
4194 end if;
4195 end if;
4196
4197 Needs_Body :=
4198 (Unit_Requires_Body (Gen_Unit)
4199 or else Enclosing_Body_Present
4200 or else Present (Corresponding_Body (Gen_Decl)))
4201 and then (Is_In_Main_Unit (N)
4202 or else Might_Inline_Subp (Gen_Unit))
4203 and then not Is_Actual_Pack
4204 and then not Inline_Now
4205 and then (Operating_Mode = Generate_Code
4206
4207 -- Need comment for this check ???
4208
4209 or else (Operating_Mode = Check_Semantics
4210 and then (ASIS_Mode or GNATprove_Mode)));
4211
4212 -- If front-end inlining is enabled or there are any subprograms
4213 -- marked with Inline_Always, do not instantiate body when within
4214 -- a generic context.
4215
4216 if ((Front_End_Inlining or else Has_Inline_Always)
4217 and then not Expander_Active)
4218 or else Is_Generic_Unit (Cunit_Entity (Main_Unit))
4219 then
4220 Needs_Body := False;
4221 end if;
4222
4223 -- If the current context is generic, and the package being
4224 -- instantiated is declared within a formal package, there is no
4225 -- body to instantiate until the enclosing generic is instantiated
4226 -- and there is an actual for the formal package. If the formal
4227 -- package has parameters, we build a regular package instance for
4228 -- it, that precedes the original formal package declaration.
4229
4230 if In_Open_Scopes (Scope (Scope (Gen_Unit))) then
4231 declare
4232 Decl : constant Node_Id :=
4233 Original_Node
4234 (Unit_Declaration_Node (Scope (Gen_Unit)));
4235 begin
4236 if Nkind (Decl) = N_Formal_Package_Declaration
4237 or else (Nkind (Decl) = N_Package_Declaration
4238 and then Is_List_Member (Decl)
4239 and then Present (Next (Decl))
4240 and then
4241 Nkind (Next (Decl)) =
4242 N_Formal_Package_Declaration)
4243 then
4244 Needs_Body := False;
4245 end if;
4246 end;
4247 end if;
4248 end;
4249
4250 -- For RCI unit calling stubs, we omit the instance body if the
4251 -- instance is the RCI library unit itself.
4252
4253 -- However there is a special case for nested instances: in this case
4254 -- we do generate the instance body, as it might be required, e.g.
4255 -- because it provides stream attributes for some type used in the
4256 -- profile of a remote subprogram. This is consistent with 12.3(12),
4257 -- which indicates that the instance body occurs at the place of the
4258 -- instantiation, and thus is part of the RCI declaration, which is
4259 -- present on all client partitions (this is E.2.3(18)).
4260
4261 -- Note that AI12-0002 may make it illegal at some point to have
4262 -- stream attributes defined in an RCI unit, in which case this
4263 -- special case will become unnecessary. In the meantime, there
4264 -- is known application code in production that depends on this
4265 -- being possible, so we definitely cannot eliminate the body in
4266 -- the case of nested instances for the time being.
4267
4268 -- When we generate a nested instance body, calling stubs for any
4269 -- relevant subprogram will be be inserted immediately after the
4270 -- subprogram declarations, and will take precedence over the
4271 -- subsequent (original) body. (The stub and original body will be
4272 -- complete homographs, but this is permitted in an instance).
4273 -- (Could we do better and remove the original body???)
4274
4275 if Distribution_Stub_Mode = Generate_Caller_Stub_Body
4276 and then Comes_From_Source (N)
4277 and then Nkind (Parent (N)) = N_Compilation_Unit
4278 then
4279 Needs_Body := False;
4280 end if;
4281
4282 if Needs_Body then
4283
4284 -- Here is a defence against a ludicrous number of instantiations
4285 -- caused by a circular set of instantiation attempts.
4286
4287 if Pending_Instantiations.Last > Maximum_Instantiations then
4288 Error_Msg_Uint_1 := UI_From_Int (Maximum_Instantiations);
4289 Error_Msg_N ("too many instantiations, exceeds max of^", N);
4290 Error_Msg_N ("\limit can be changed using -gnateinn switch", N);
4291 raise Unrecoverable_Error;
4292 end if;
4293
4294 -- Indicate that the enclosing scopes contain an instantiation,
4295 -- and that cleanup actions should be delayed until after the
4296 -- instance body is expanded.
4297
4298 Check_Forward_Instantiation (Gen_Decl);
4299 if Nkind (N) = N_Package_Instantiation then
4300 declare
4301 Enclosing_Master : Entity_Id;
4302
4303 begin
4304 -- Loop to search enclosing masters
4305
4306 Enclosing_Master := Current_Scope;
4307 Scope_Loop : while Enclosing_Master /= Standard_Standard loop
4308 if Ekind (Enclosing_Master) = E_Package then
4309 if Is_Compilation_Unit (Enclosing_Master) then
4310 if In_Package_Body (Enclosing_Master) then
4311 Delay_Descriptors
4312 (Body_Entity (Enclosing_Master));
4313 else
4314 Delay_Descriptors
4315 (Enclosing_Master);
4316 end if;
4317
4318 exit Scope_Loop;
4319
4320 else
4321 Enclosing_Master := Scope (Enclosing_Master);
4322 end if;
4323
4324 elsif Is_Generic_Unit (Enclosing_Master)
4325 or else Ekind (Enclosing_Master) = E_Void
4326 then
4327 -- Cleanup actions will eventually be performed on the
4328 -- enclosing subprogram or package instance, if any.
4329 -- Enclosing scope is void in the formal part of a
4330 -- generic subprogram.
4331
4332 exit Scope_Loop;
4333
4334 else
4335 if Ekind (Enclosing_Master) = E_Entry
4336 and then
4337 Ekind (Scope (Enclosing_Master)) = E_Protected_Type
4338 then
4339 if not Expander_Active then
4340 exit Scope_Loop;
4341 else
4342 Enclosing_Master :=
4343 Protected_Body_Subprogram (Enclosing_Master);
4344 end if;
4345 end if;
4346
4347 Set_Delay_Cleanups (Enclosing_Master);
4348
4349 while Ekind (Enclosing_Master) = E_Block loop
4350 Enclosing_Master := Scope (Enclosing_Master);
4351 end loop;
4352
4353 if Is_Subprogram (Enclosing_Master) then
4354 Delay_Descriptors (Enclosing_Master);
4355
4356 elsif Is_Task_Type (Enclosing_Master) then
4357 declare
4358 TBP : constant Node_Id :=
4359 Get_Task_Body_Procedure
4360 (Enclosing_Master);
4361 begin
4362 if Present (TBP) then
4363 Delay_Descriptors (TBP);
4364 Set_Delay_Cleanups (TBP);
4365 end if;
4366 end;
4367 end if;
4368
4369 exit Scope_Loop;
4370 end if;
4371 end loop Scope_Loop;
4372 end;
4373
4374 -- Make entry in table
4375
4376 Add_Pending_Instantiation (N, Act_Decl);
4377 end if;
4378 end if;
4379
4380 Set_Categorization_From_Pragmas (Act_Decl);
4381
4382 if Parent_Installed then
4383 Hide_Current_Scope;
4384 end if;
4385
4386 Set_Instance_Spec (N, Act_Decl);
4387
4388 -- If not a compilation unit, insert the package declaration before
4389 -- the original instantiation node.
4390
4391 if Nkind (Parent (N)) /= N_Compilation_Unit then
4392 Mark_Rewrite_Insertion (Act_Decl);
4393 Insert_Before (N, Act_Decl);
4394
4395 if Has_Aspects (N) then
4396 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4397
4398 -- The pragma created for a Default_Storage_Pool aspect must
4399 -- appear ahead of the declarations in the instance spec.
4400 -- Analysis has placed it after the instance node, so remove
4401 -- it and reinsert it properly now.
4402
4403 declare
4404 ASN : constant Node_Id := First (Aspect_Specifications (N));
4405 A_Name : constant Name_Id := Chars (Identifier (ASN));
4406 Decl : Node_Id;
4407
4408 begin
4409 if A_Name = Name_Default_Storage_Pool then
4410 if No (Visible_Declarations (Act_Spec)) then
4411 Set_Visible_Declarations (Act_Spec, New_List);
4412 end if;
4413
4414 Decl := Next (N);
4415 while Present (Decl) loop
4416 if Nkind (Decl) = N_Pragma then
4417 Remove (Decl);
4418 Prepend (Decl, Visible_Declarations (Act_Spec));
4419 exit;
4420 end if;
4421
4422 Next (Decl);
4423 end loop;
4424 end if;
4425 end;
4426 end if;
4427
4428 Analyze (Act_Decl);
4429
4430 -- For an instantiation that is a compilation unit, place
4431 -- declaration on current node so context is complete for analysis
4432 -- (including nested instantiations). If this is the main unit,
4433 -- the declaration eventually replaces the instantiation node.
4434 -- If the instance body is created later, it replaces the
4435 -- instance node, and the declaration is attached to it
4436 -- (see Build_Instance_Compilation_Unit_Nodes).
4437
4438 else
4439 if Cunit_Entity (Current_Sem_Unit) = Defining_Entity (N) then
4440
4441 -- The entity for the current unit is the newly created one,
4442 -- and all semantic information is attached to it.
4443
4444 Set_Cunit_Entity (Current_Sem_Unit, Act_Decl_Id);
4445
4446 -- If this is the main unit, replace the main entity as well
4447
4448 if Current_Sem_Unit = Main_Unit then
4449 Main_Unit_Entity := Act_Decl_Id;
4450 end if;
4451 end if;
4452
4453 Set_Unit (Parent (N), Act_Decl);
4454 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
4455 Set_Package_Instantiation (Act_Decl_Id, N);
4456
4457 -- Process aspect specifications of the instance node, if any, to
4458 -- take into account categorization pragmas before analyzing the
4459 -- instance.
4460
4461 if Has_Aspects (N) then
4462 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4463 end if;
4464
4465 Analyze (Act_Decl);
4466 Set_Unit (Parent (N), N);
4467 Set_Body_Required (Parent (N), False);
4468
4469 -- We never need elaboration checks on instantiations, since by
4470 -- definition, the body instantiation is elaborated at the same
4471 -- time as the spec instantiation.
4472
4473 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
4474 Set_Kill_Elaboration_Checks (Act_Decl_Id);
4475 end if;
4476
4477 Check_Elab_Instantiation (N);
4478
4479 if ABE_Is_Certain (N) and then Needs_Body then
4480 Pending_Instantiations.Decrement_Last;
4481 end if;
4482
4483 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
4484
4485 Set_First_Private_Entity (Defining_Unit_Name (Unit_Renaming),
4486 First_Private_Entity (Act_Decl_Id));
4487
4488 -- If the instantiation will receive a body, the unit will be
4489 -- transformed into a package body, and receive its own elaboration
4490 -- entity. Otherwise, the nature of the unit is now a package
4491 -- declaration.
4492
4493 if Nkind (Parent (N)) = N_Compilation_Unit
4494 and then not Needs_Body
4495 then
4496 Rewrite (N, Act_Decl);
4497 end if;
4498
4499 if Present (Corresponding_Body (Gen_Decl))
4500 or else Unit_Requires_Body (Gen_Unit)
4501 then
4502 Set_Has_Completion (Act_Decl_Id);
4503 end if;
4504
4505 Check_Formal_Packages (Act_Decl_Id);
4506
4507 Restore_Hidden_Primitives (Vis_Prims_List);
4508 Restore_Private_Views (Act_Decl_Id);
4509
4510 Inherit_Context (Gen_Decl, N);
4511
4512 if Parent_Installed then
4513 Remove_Parent;
4514 end if;
4515
4516 Restore_Env;
4517 Env_Installed := False;
4518 end if;
4519
4520 Validate_Categorization_Dependency (N, Act_Decl_Id);
4521
4522 -- There used to be a check here to prevent instantiations in local
4523 -- contexts if the No_Local_Allocators restriction was active. This
4524 -- check was removed by a binding interpretation in AI-95-00130/07,
4525 -- but we retain the code for documentation purposes.
4526
4527 -- if Ekind (Act_Decl_Id) /= E_Void
4528 -- and then not Is_Library_Level_Entity (Act_Decl_Id)
4529 -- then
4530 -- Check_Restriction (No_Local_Allocators, N);
4531 -- end if;
4532
4533 if Inline_Now then
4534 Inline_Instance_Body (N, Gen_Unit, Act_Decl);
4535 end if;
4536
4537 -- The following is a tree patch for ASIS: ASIS needs separate nodes to
4538 -- be used as defining identifiers for a formal package and for the
4539 -- corresponding expanded package.
4540
4541 if Nkind (N) = N_Formal_Package_Declaration then
4542 Act_Decl_Id := New_Copy (Defining_Entity (N));
4543 Set_Comes_From_Source (Act_Decl_Id, True);
4544 Set_Is_Generic_Instance (Act_Decl_Id, False);
4545 Set_Defining_Identifier (N, Act_Decl_Id);
4546 end if;
4547
4548 -- Check that if N is an instantiation of System.Dim_Float_IO or
4549 -- System.Dim_Integer_IO, the formal type has a dimension system.
4550
4551 if Nkind (N) = N_Package_Instantiation
4552 and then Is_Dim_IO_Package_Instantiation (N)
4553 then
4554 declare
4555 Assoc : constant Node_Id := First (Generic_Associations (N));
4556 begin
4557 if not Has_Dimension_System
4558 (Etype (Explicit_Generic_Actual_Parameter (Assoc)))
4559 then
4560 Error_Msg_N ("type with a dimension system expected", Assoc);
4561 end if;
4562 end;
4563 end if;
4564
4565 <<Leave>>
4566 if Has_Aspects (N) and then Nkind (Parent (N)) /= N_Compilation_Unit then
4567 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4568 end if;
4569
4570 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
4571 Restore_Ghost_Mode (Saved_GM);
4572 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
4573 Style_Check := Saved_Style_Check;
4574
4575 exception
4576 when Instantiation_Error =>
4577 if Parent_Installed then
4578 Remove_Parent;
4579 end if;
4580
4581 if Env_Installed then
4582 Restore_Env;
4583 end if;
4584
4585 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
4586 Restore_Ghost_Mode (Saved_GM);
4587 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
4588 Style_Check := Saved_Style_Check;
4589 end Analyze_Package_Instantiation;
4590
4591 --------------------------
4592 -- Inline_Instance_Body --
4593 --------------------------
4594
4595 -- WARNING: This routine manages SPARK regions. Return statements must be
4596 -- replaced by gotos which jump to the end of the routine and restore the
4597 -- SPARK mode.
4598
4599 procedure Inline_Instance_Body
4600 (N : Node_Id;
4601 Gen_Unit : Entity_Id;
4602 Act_Decl : Node_Id)
4603 is
4604 Curr_Comp : constant Node_Id := Cunit (Current_Sem_Unit);
4605 Curr_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
4606 Gen_Comp : constant Entity_Id :=
4607 Cunit_Entity (Get_Source_Unit (Gen_Unit));
4608
4609 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
4610 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
4611 -- Save the SPARK mode-related data to restore on exit. Removing
4612 -- enclosing scopes to provide a clean environment for analysis of
4613 -- the inlined body will eliminate any previously set SPARK_Mode.
4614
4615 Scope_Stack_Depth : constant Pos :=
4616 Scope_Stack.Last - Scope_Stack.First + 1;
4617
4618 Inner_Scopes : array (1 .. Scope_Stack_Depth) of Entity_Id;
4619 Instances : array (1 .. Scope_Stack_Depth) of Entity_Id;
4620 Use_Clauses : array (1 .. Scope_Stack_Depth) of Node_Id;
4621
4622 Curr_Scope : Entity_Id := Empty;
4623 List : Elist_Id;
4624 N_Instances : Nat := 0;
4625 Num_Inner : Nat := 0;
4626 Num_Scopes : Nat := 0;
4627 Removed : Boolean := False;
4628 S : Entity_Id;
4629 Vis : Boolean;
4630
4631 begin
4632 -- Case of generic unit defined in another unit. We must remove the
4633 -- complete context of the current unit to install that of the generic.
4634
4635 if Gen_Comp /= Cunit_Entity (Current_Sem_Unit) then
4636
4637 -- Add some comments for the following two loops ???
4638
4639 S := Current_Scope;
4640 while Present (S) and then S /= Standard_Standard loop
4641 loop
4642 Num_Scopes := Num_Scopes + 1;
4643
4644 Use_Clauses (Num_Scopes) :=
4645 (Scope_Stack.Table
4646 (Scope_Stack.Last - Num_Scopes + 1).
4647 First_Use_Clause);
4648 End_Use_Clauses (Use_Clauses (Num_Scopes));
4649
4650 exit when Scope_Stack.Last - Num_Scopes + 1 = Scope_Stack.First
4651 or else Scope_Stack.Table
4652 (Scope_Stack.Last - Num_Scopes).Entity = Scope (S);
4653 end loop;
4654
4655 exit when Is_Generic_Instance (S)
4656 and then (In_Package_Body (S)
4657 or else Ekind (S) = E_Procedure
4658 or else Ekind (S) = E_Function);
4659 S := Scope (S);
4660 end loop;
4661
4662 Vis := Is_Immediately_Visible (Gen_Comp);
4663
4664 -- Find and save all enclosing instances
4665
4666 S := Current_Scope;
4667
4668 while Present (S)
4669 and then S /= Standard_Standard
4670 loop
4671 if Is_Generic_Instance (S) then
4672 N_Instances := N_Instances + 1;
4673 Instances (N_Instances) := S;
4674
4675 exit when In_Package_Body (S);
4676 end if;
4677
4678 S := Scope (S);
4679 end loop;
4680
4681 -- Remove context of current compilation unit, unless we are within a
4682 -- nested package instantiation, in which case the context has been
4683 -- removed previously.
4684
4685 -- If current scope is the body of a child unit, remove context of
4686 -- spec as well. If an enclosing scope is an instance body, the
4687 -- context has already been removed, but the entities in the body
4688 -- must be made invisible as well.
4689
4690 S := Current_Scope;
4691 while Present (S) and then S /= Standard_Standard loop
4692 if Is_Generic_Instance (S)
4693 and then (In_Package_Body (S)
4694 or else Ekind_In (S, E_Procedure, E_Function))
4695 then
4696 -- We still have to remove the entities of the enclosing
4697 -- instance from direct visibility.
4698
4699 declare
4700 E : Entity_Id;
4701 begin
4702 E := First_Entity (S);
4703 while Present (E) loop
4704 Set_Is_Immediately_Visible (E, False);
4705 Next_Entity (E);
4706 end loop;
4707 end;
4708
4709 exit;
4710 end if;
4711
4712 if S = Curr_Unit
4713 or else (Ekind (Curr_Unit) = E_Package_Body
4714 and then S = Spec_Entity (Curr_Unit))
4715 or else (Ekind (Curr_Unit) = E_Subprogram_Body
4716 and then S = Corresponding_Spec
4717 (Unit_Declaration_Node (Curr_Unit)))
4718 then
4719 Removed := True;
4720
4721 -- Remove entities in current scopes from visibility, so that
4722 -- instance body is compiled in a clean environment.
4723
4724 List := Save_Scope_Stack (Handle_Use => False);
4725
4726 if Is_Child_Unit (S) then
4727
4728 -- Remove child unit from stack, as well as inner scopes.
4729 -- Removing the context of a child unit removes parent units
4730 -- as well.
4731
4732 while Current_Scope /= S loop
4733 Num_Inner := Num_Inner + 1;
4734 Inner_Scopes (Num_Inner) := Current_Scope;
4735 Pop_Scope;
4736 end loop;
4737
4738 Pop_Scope;
4739 Remove_Context (Curr_Comp);
4740 Curr_Scope := S;
4741
4742 else
4743 Remove_Context (Curr_Comp);
4744 end if;
4745
4746 if Ekind (Curr_Unit) = E_Package_Body then
4747 Remove_Context (Library_Unit (Curr_Comp));
4748 end if;
4749 end if;
4750
4751 S := Scope (S);
4752 end loop;
4753
4754 pragma Assert (Num_Inner < Num_Scopes);
4755
4756 -- The inlined package body must be analyzed with the SPARK_Mode of
4757 -- the enclosing context, otherwise the body may cause bogus errors
4758 -- if a configuration SPARK_Mode pragma in in effect.
4759
4760 Push_Scope (Standard_Standard);
4761 Scope_Stack.Table (Scope_Stack.Last).Is_Active_Stack_Base := True;
4762 Instantiate_Package_Body
4763 (Body_Info =>
4764 ((Inst_Node => N,
4765 Act_Decl => Act_Decl,
4766 Expander_Status => Expander_Active,
4767 Current_Sem_Unit => Current_Sem_Unit,
4768 Scope_Suppress => Scope_Suppress,
4769 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
4770 Version => Ada_Version,
4771 Version_Pragma => Ada_Version_Pragma,
4772 Warnings => Save_Warnings,
4773 SPARK_Mode => Saved_SM,
4774 SPARK_Mode_Pragma => Saved_SMP)),
4775 Inlined_Body => True);
4776
4777 Pop_Scope;
4778
4779 -- Restore context
4780
4781 Set_Is_Immediately_Visible (Gen_Comp, Vis);
4782
4783 -- Reset Generic_Instance flag so that use clauses can be installed
4784 -- in the proper order. (See Use_One_Package for effect of enclosing
4785 -- instances on processing of use clauses).
4786
4787 for J in 1 .. N_Instances loop
4788 Set_Is_Generic_Instance (Instances (J), False);
4789 end loop;
4790
4791 if Removed then
4792 Install_Context (Curr_Comp);
4793
4794 if Present (Curr_Scope)
4795 and then Is_Child_Unit (Curr_Scope)
4796 then
4797 Push_Scope (Curr_Scope);
4798 Set_Is_Immediately_Visible (Curr_Scope);
4799
4800 -- Finally, restore inner scopes as well
4801
4802 for J in reverse 1 .. Num_Inner loop
4803 Push_Scope (Inner_Scopes (J));
4804 end loop;
4805 end if;
4806
4807 Restore_Scope_Stack (List, Handle_Use => False);
4808
4809 if Present (Curr_Scope)
4810 and then
4811 (In_Private_Part (Curr_Scope)
4812 or else In_Package_Body (Curr_Scope))
4813 then
4814 -- Install private declaration of ancestor units, which are
4815 -- currently available. Restore_Scope_Stack and Install_Context
4816 -- only install the visible part of parents.
4817
4818 declare
4819 Par : Entity_Id;
4820 begin
4821 Par := Scope (Curr_Scope);
4822 while (Present (Par)) and then Par /= Standard_Standard loop
4823 Install_Private_Declarations (Par);
4824 Par := Scope (Par);
4825 end loop;
4826 end;
4827 end if;
4828 end if;
4829
4830 -- Restore use clauses. For a child unit, use clauses in the parents
4831 -- are restored when installing the context, so only those in inner
4832 -- scopes (and those local to the child unit itself) need to be
4833 -- installed explicitly.
4834
4835 if Is_Child_Unit (Curr_Unit) and then Removed then
4836 for J in reverse 1 .. Num_Inner + 1 loop
4837 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
4838 Use_Clauses (J);
4839 Install_Use_Clauses (Use_Clauses (J));
4840 end loop;
4841
4842 else
4843 for J in reverse 1 .. Num_Scopes loop
4844 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
4845 Use_Clauses (J);
4846 Install_Use_Clauses (Use_Clauses (J));
4847 end loop;
4848 end if;
4849
4850 -- Restore status of instances. If one of them is a body, make its
4851 -- local entities visible again.
4852
4853 declare
4854 E : Entity_Id;
4855 Inst : Entity_Id;
4856
4857 begin
4858 for J in 1 .. N_Instances loop
4859 Inst := Instances (J);
4860 Set_Is_Generic_Instance (Inst, True);
4861
4862 if In_Package_Body (Inst)
4863 or else Ekind_In (S, E_Procedure, E_Function)
4864 then
4865 E := First_Entity (Instances (J));
4866 while Present (E) loop
4867 Set_Is_Immediately_Visible (E);
4868 Next_Entity (E);
4869 end loop;
4870 end if;
4871 end loop;
4872 end;
4873
4874 -- If generic unit is in current unit, current context is correct. Note
4875 -- that the context is guaranteed to carry the correct SPARK_Mode as no
4876 -- enclosing scopes were removed.
4877
4878 else
4879 Instantiate_Package_Body
4880 (Body_Info =>
4881 ((Inst_Node => N,
4882 Act_Decl => Act_Decl,
4883 Expander_Status => Expander_Active,
4884 Current_Sem_Unit => Current_Sem_Unit,
4885 Scope_Suppress => Scope_Suppress,
4886 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
4887 Version => Ada_Version,
4888 Version_Pragma => Ada_Version_Pragma,
4889 Warnings => Save_Warnings,
4890 SPARK_Mode => SPARK_Mode,
4891 SPARK_Mode_Pragma => SPARK_Mode_Pragma)),
4892 Inlined_Body => True);
4893 end if;
4894 end Inline_Instance_Body;
4895
4896 -------------------------------------
4897 -- Analyze_Procedure_Instantiation --
4898 -------------------------------------
4899
4900 procedure Analyze_Procedure_Instantiation (N : Node_Id) is
4901 begin
4902 Analyze_Subprogram_Instantiation (N, E_Procedure);
4903 end Analyze_Procedure_Instantiation;
4904
4905 -----------------------------------
4906 -- Need_Subprogram_Instance_Body --
4907 -----------------------------------
4908
4909 function Need_Subprogram_Instance_Body
4910 (N : Node_Id;
4911 Subp : Entity_Id) return Boolean
4912 is
4913 function Is_Inlined_Or_Child_Of_Inlined (E : Entity_Id) return Boolean;
4914 -- Return True if E is an inlined subprogram, an inlined renaming or a
4915 -- subprogram nested in an inlined subprogram. The inlining machinery
4916 -- totally disregards nested subprograms since it considers that they
4917 -- will always be compiled if the parent is (see Inline.Is_Nested).
4918
4919 ------------------------------------
4920 -- Is_Inlined_Or_Child_Of_Inlined --
4921 ------------------------------------
4922
4923 function Is_Inlined_Or_Child_Of_Inlined (E : Entity_Id) return Boolean is
4924 Scop : Entity_Id;
4925
4926 begin
4927 if Is_Inlined (E) or else Is_Inlined (Alias (E)) then
4928 return True;
4929 end if;
4930
4931 Scop := Scope (E);
4932 while Scop /= Standard_Standard loop
4933 if Ekind (Scop) in Subprogram_Kind and then Is_Inlined (Scop) then
4934 return True;
4935 end if;
4936
4937 Scop := Scope (Scop);
4938 end loop;
4939
4940 return False;
4941 end Is_Inlined_Or_Child_Of_Inlined;
4942
4943 begin
4944 -- Must be in the main unit or inlined (or child of inlined)
4945
4946 if (Is_In_Main_Unit (N) or else Is_Inlined_Or_Child_Of_Inlined (Subp))
4947
4948 -- Must be generating code or analyzing code in ASIS/GNATprove mode
4949
4950 and then (Operating_Mode = Generate_Code
4951 or else (Operating_Mode = Check_Semantics
4952 and then (ASIS_Mode or GNATprove_Mode)))
4953
4954 -- The body is needed when generating code (full expansion), in ASIS
4955 -- mode for other tools, and in GNATprove mode (special expansion) for
4956 -- formal verification of the body itself.
4957
4958 and then (Expander_Active or ASIS_Mode or GNATprove_Mode)
4959
4960 -- No point in inlining if ABE is inevitable
4961
4962 and then not ABE_Is_Certain (N)
4963
4964 -- Or if subprogram is eliminated
4965
4966 and then not Is_Eliminated (Subp)
4967 then
4968 Add_Pending_Instantiation (N, Unit_Declaration_Node (Subp));
4969 return True;
4970
4971 -- Here if not inlined, or we ignore the inlining
4972
4973 else
4974 return False;
4975 end if;
4976 end Need_Subprogram_Instance_Body;
4977
4978 --------------------------------------
4979 -- Analyze_Subprogram_Instantiation --
4980 --------------------------------------
4981
4982 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
4983 -- must be replaced by gotos which jump to the end of the routine in order
4984 -- to restore the Ghost and SPARK modes.
4985
4986 procedure Analyze_Subprogram_Instantiation
4987 (N : Node_Id;
4988 K : Entity_Kind)
4989 is
4990 Loc : constant Source_Ptr := Sloc (N);
4991 Gen_Id : constant Node_Id := Name (N);
4992
4993 Anon_Id : constant Entity_Id :=
4994 Make_Defining_Identifier (Sloc (Defining_Entity (N)),
4995 Chars => New_External_Name
4996 (Chars (Defining_Entity (N)), 'R'));
4997
4998 Act_Decl_Id : Entity_Id;
4999 Act_Decl : Node_Id;
5000 Act_Spec : Node_Id;
5001 Act_Tree : Node_Id;
5002
5003 Env_Installed : Boolean := False;
5004 Gen_Unit : Entity_Id;
5005 Gen_Decl : Node_Id;
5006 Pack_Id : Entity_Id;
5007 Parent_Installed : Boolean := False;
5008
5009 Renaming_List : List_Id;
5010 -- The list of declarations that link formals and actuals of the
5011 -- instance. These are subtype declarations for formal types, and
5012 -- renaming declarations for other formals. The subprogram declaration
5013 -- for the instance is then appended to the list, and the last item on
5014 -- the list is the renaming declaration for the instance.
5015
5016 procedure Analyze_Instance_And_Renamings;
5017 -- The instance must be analyzed in a context that includes the mappings
5018 -- of generic parameters into actuals. We create a package declaration
5019 -- for this purpose, and a subprogram with an internal name within the
5020 -- package. The subprogram instance is simply an alias for the internal
5021 -- subprogram, declared in the current scope.
5022
5023 procedure Build_Subprogram_Renaming;
5024 -- If the subprogram is recursive, there are occurrences of the name of
5025 -- the generic within the body, which must resolve to the current
5026 -- instance. We add a renaming declaration after the declaration, which
5027 -- is available in the instance body, as well as in the analysis of
5028 -- aspects that appear in the generic. This renaming declaration is
5029 -- inserted after the instance declaration which it renames.
5030
5031 ------------------------------------
5032 -- Analyze_Instance_And_Renamings --
5033 ------------------------------------
5034
5035 procedure Analyze_Instance_And_Renamings is
5036 Def_Ent : constant Entity_Id := Defining_Entity (N);
5037 Pack_Decl : Node_Id;
5038
5039 begin
5040 if Nkind (Parent (N)) = N_Compilation_Unit then
5041
5042 -- For the case of a compilation unit, the container package has
5043 -- the same name as the instantiation, to insure that the binder
5044 -- calls the elaboration procedure with the right name. Copy the
5045 -- entity of the instance, which may have compilation level flags
5046 -- (e.g. Is_Child_Unit) set.
5047
5048 Pack_Id := New_Copy (Def_Ent);
5049
5050 else
5051 -- Otherwise we use the name of the instantiation concatenated
5052 -- with its source position to ensure uniqueness if there are
5053 -- several instantiations with the same name.
5054
5055 Pack_Id :=
5056 Make_Defining_Identifier (Loc,
5057 Chars => New_External_Name
5058 (Related_Id => Chars (Def_Ent),
5059 Suffix => "GP",
5060 Suffix_Index => Source_Offset (Sloc (Def_Ent))));
5061 end if;
5062
5063 Pack_Decl :=
5064 Make_Package_Declaration (Loc,
5065 Specification => Make_Package_Specification (Loc,
5066 Defining_Unit_Name => Pack_Id,
5067 Visible_Declarations => Renaming_List,
5068 End_Label => Empty));
5069
5070 Set_Instance_Spec (N, Pack_Decl);
5071 Set_Is_Generic_Instance (Pack_Id);
5072 Set_Debug_Info_Needed (Pack_Id);
5073
5074 -- Case of not a compilation unit
5075
5076 if Nkind (Parent (N)) /= N_Compilation_Unit then
5077 Mark_Rewrite_Insertion (Pack_Decl);
5078 Insert_Before (N, Pack_Decl);
5079 Set_Has_Completion (Pack_Id);
5080
5081 -- Case of an instantiation that is a compilation unit
5082
5083 -- Place declaration on current node so context is complete for
5084 -- analysis (including nested instantiations), and for use in a
5085 -- context_clause (see Analyze_With_Clause).
5086
5087 else
5088 Set_Unit (Parent (N), Pack_Decl);
5089 Set_Parent_Spec (Pack_Decl, Parent_Spec (N));
5090 end if;
5091
5092 Analyze (Pack_Decl);
5093 Check_Formal_Packages (Pack_Id);
5094 Set_Is_Generic_Instance (Pack_Id, False);
5095
5096 -- Why do we clear Is_Generic_Instance??? We set it 20 lines
5097 -- above???
5098
5099 -- Body of the enclosing package is supplied when instantiating the
5100 -- subprogram body, after semantic analysis is completed.
5101
5102 if Nkind (Parent (N)) = N_Compilation_Unit then
5103
5104 -- Remove package itself from visibility, so it does not
5105 -- conflict with subprogram.
5106
5107 Set_Name_Entity_Id (Chars (Pack_Id), Homonym (Pack_Id));
5108
5109 -- Set name and scope of internal subprogram so that the proper
5110 -- external name will be generated. The proper scope is the scope
5111 -- of the wrapper package. We need to generate debugging info for
5112 -- the internal subprogram, so set flag accordingly.
5113
5114 Set_Chars (Anon_Id, Chars (Defining_Entity (N)));
5115 Set_Scope (Anon_Id, Scope (Pack_Id));
5116
5117 -- Mark wrapper package as referenced, to avoid spurious warnings
5118 -- if the instantiation appears in various with_ clauses of
5119 -- subunits of the main unit.
5120
5121 Set_Referenced (Pack_Id);
5122 end if;
5123
5124 Set_Is_Generic_Instance (Anon_Id);
5125 Set_Debug_Info_Needed (Anon_Id);
5126 Act_Decl_Id := New_Copy (Anon_Id);
5127
5128 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
5129 Set_Chars (Act_Decl_Id, Chars (Defining_Entity (N)));
5130 Set_Sloc (Act_Decl_Id, Sloc (Defining_Entity (N)));
5131
5132 -- Subprogram instance comes from source only if generic does
5133
5134 Set_Comes_From_Source (Act_Decl_Id, Comes_From_Source (Gen_Unit));
5135
5136 -- If the instance is a child unit, mark the Id accordingly. Mark
5137 -- the anonymous entity as well, which is the real subprogram and
5138 -- which is used when the instance appears in a context clause.
5139 -- Similarly, propagate the Is_Eliminated flag to handle properly
5140 -- nested eliminated subprograms.
5141
5142 Set_Is_Child_Unit (Act_Decl_Id, Is_Child_Unit (Defining_Entity (N)));
5143 Set_Is_Child_Unit (Anon_Id, Is_Child_Unit (Defining_Entity (N)));
5144 New_Overloaded_Entity (Act_Decl_Id);
5145 Check_Eliminated (Act_Decl_Id);
5146 Set_Is_Eliminated (Anon_Id, Is_Eliminated (Act_Decl_Id));
5147
5148 -- In compilation unit case, kill elaboration checks on the
5149 -- instantiation, since they are never needed -- the body is
5150 -- instantiated at the same point as the spec.
5151
5152 if Nkind (Parent (N)) = N_Compilation_Unit then
5153 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
5154 Set_Kill_Elaboration_Checks (Act_Decl_Id);
5155 Set_Is_Compilation_Unit (Anon_Id);
5156
5157 Set_Cunit_Entity (Current_Sem_Unit, Pack_Id);
5158 end if;
5159
5160 -- The instance is not a freezing point for the new subprogram.
5161 -- The anonymous subprogram may have a freeze node, created for
5162 -- some delayed aspects. This freeze node must not be inherited
5163 -- by the visible subprogram entity.
5164
5165 Set_Is_Frozen (Act_Decl_Id, False);
5166 Set_Freeze_Node (Act_Decl_Id, Empty);
5167
5168 if Nkind (Defining_Entity (N)) = N_Defining_Operator_Symbol then
5169 Valid_Operator_Definition (Act_Decl_Id);
5170 end if;
5171
5172 Set_Alias (Act_Decl_Id, Anon_Id);
5173 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
5174 Set_Has_Completion (Act_Decl_Id);
5175 Set_Related_Instance (Pack_Id, Act_Decl_Id);
5176
5177 if Nkind (Parent (N)) = N_Compilation_Unit then
5178 Set_Body_Required (Parent (N), False);
5179 end if;
5180 end Analyze_Instance_And_Renamings;
5181
5182 -------------------------------
5183 -- Build_Subprogram_Renaming --
5184 -------------------------------
5185
5186 procedure Build_Subprogram_Renaming is
5187 Renaming_Decl : Node_Id;
5188 Unit_Renaming : Node_Id;
5189
5190 begin
5191 Unit_Renaming :=
5192 Make_Subprogram_Renaming_Declaration (Loc,
5193 Specification =>
5194 Copy_Generic_Node
5195 (Specification (Original_Node (Gen_Decl)),
5196 Empty,
5197 Instantiating => True),
5198 Name => New_Occurrence_Of (Anon_Id, Loc));
5199
5200 -- The generic may be a a child unit. The renaming needs an
5201 -- identifier with the proper name.
5202
5203 Set_Defining_Unit_Name (Specification (Unit_Renaming),
5204 Make_Defining_Identifier (Loc, Chars (Gen_Unit)));
5205
5206 -- If there is a formal subprogram with the same name as the unit
5207 -- itself, do not add this renaming declaration, to prevent
5208 -- ambiguities when there is a call with that name in the body.
5209 -- This is a partial and ugly fix for one ACATS test. ???
5210
5211 Renaming_Decl := First (Renaming_List);
5212 while Present (Renaming_Decl) loop
5213 if Nkind (Renaming_Decl) = N_Subprogram_Renaming_Declaration
5214 and then
5215 Chars (Defining_Entity (Renaming_Decl)) = Chars (Gen_Unit)
5216 then
5217 exit;
5218 end if;
5219
5220 Next (Renaming_Decl);
5221 end loop;
5222
5223 if No (Renaming_Decl) then
5224 Append (Unit_Renaming, Renaming_List);
5225 end if;
5226 end Build_Subprogram_Renaming;
5227
5228 -- Local variables
5229
5230 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
5231 Saved_ISMP : constant Boolean :=
5232 Ignore_SPARK_Mode_Pragmas_In_Instance;
5233 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
5234 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
5235 -- Save the Ghost and SPARK mode-related data to restore on exit
5236
5237 Vis_Prims_List : Elist_Id := No_Elist;
5238 -- List of primitives made temporarily visible in the instantiation
5239 -- to match the visibility of the formal type
5240
5241 -- Start of processing for Analyze_Subprogram_Instantiation
5242
5243 begin
5244 Check_SPARK_05_Restriction ("generic is not allowed", N);
5245
5246 -- Very first thing: check for special Text_IO unit in case we are
5247 -- instantiating one of the children of [[Wide_]Wide_]Text_IO. Of course
5248 -- such an instantiation is bogus (these are packages, not subprograms),
5249 -- but we get a better error message if we do this.
5250
5251 Check_Text_IO_Special_Unit (Gen_Id);
5252
5253 -- Make node global for error reporting
5254
5255 Instantiation_Node := N;
5256
5257 -- For package instantiations we turn off style checks, because they
5258 -- will have been emitted in the generic. For subprogram instantiations
5259 -- we want to apply at least the check on overriding indicators so we
5260 -- do not modify the style check status.
5261
5262 -- The renaming declarations for the actuals do not come from source and
5263 -- will not generate spurious warnings.
5264
5265 Preanalyze_Actuals (N);
5266
5267 Init_Env;
5268 Env_Installed := True;
5269 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
5270 Gen_Unit := Entity (Gen_Id);
5271
5272 -- A subprogram instantiation is Ghost when it is subject to pragma
5273 -- Ghost or the generic template is Ghost. Set the mode now to ensure
5274 -- that any nodes generated during analysis and expansion are marked as
5275 -- Ghost.
5276
5277 Mark_And_Set_Ghost_Instantiation (N, Gen_Unit);
5278
5279 Generate_Reference (Gen_Unit, Gen_Id);
5280
5281 if Nkind (Gen_Id) = N_Identifier
5282 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
5283 then
5284 Error_Msg_NE
5285 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
5286 end if;
5287
5288 if Etype (Gen_Unit) = Any_Type then
5289 Restore_Env;
5290 goto Leave;
5291 end if;
5292
5293 -- Verify that it is a generic subprogram of the right kind, and that
5294 -- it does not lead to a circular instantiation.
5295
5296 if K = E_Procedure and then Ekind (Gen_Unit) /= E_Generic_Procedure then
5297 Error_Msg_NE
5298 ("& is not the name of a generic procedure", Gen_Id, Gen_Unit);
5299
5300 elsif K = E_Function and then Ekind (Gen_Unit) /= E_Generic_Function then
5301 Error_Msg_NE
5302 ("& is not the name of a generic function", Gen_Id, Gen_Unit);
5303
5304 elsif In_Open_Scopes (Gen_Unit) then
5305 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
5306
5307 else
5308 Set_Entity (Gen_Id, Gen_Unit);
5309 Set_Is_Instantiated (Gen_Unit);
5310
5311 if In_Extended_Main_Source_Unit (N) then
5312 Generate_Reference (Gen_Unit, N);
5313 end if;
5314
5315 -- If renaming, get original unit
5316
5317 if Present (Renamed_Object (Gen_Unit))
5318 and then Ekind_In (Renamed_Object (Gen_Unit), E_Generic_Procedure,
5319 E_Generic_Function)
5320 then
5321 Gen_Unit := Renamed_Object (Gen_Unit);
5322 Set_Is_Instantiated (Gen_Unit);
5323 Generate_Reference (Gen_Unit, N);
5324 end if;
5325
5326 if Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
5327 Error_Msg_Node_2 := Current_Scope;
5328 Error_Msg_NE
5329 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
5330 Circularity_Detected := True;
5331 Restore_Hidden_Primitives (Vis_Prims_List);
5332 goto Leave;
5333 end if;
5334
5335 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
5336
5337 -- Initialize renamings map, for error checking
5338
5339 Generic_Renamings.Set_Last (0);
5340 Generic_Renamings_HTable.Reset;
5341
5342 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
5343
5344 -- Copy original generic tree, to produce text for instantiation
5345
5346 Act_Tree :=
5347 Copy_Generic_Node
5348 (Original_Node (Gen_Decl), Empty, Instantiating => True);
5349
5350 -- Inherit overriding indicator from instance node
5351
5352 Act_Spec := Specification (Act_Tree);
5353 Set_Must_Override (Act_Spec, Must_Override (N));
5354 Set_Must_Not_Override (Act_Spec, Must_Not_Override (N));
5355
5356 Renaming_List :=
5357 Analyze_Associations
5358 (I_Node => N,
5359 Formals => Generic_Formal_Declarations (Act_Tree),
5360 F_Copy => Generic_Formal_Declarations (Gen_Decl));
5361
5362 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
5363
5364 -- The subprogram itself cannot contain a nested instance, so the
5365 -- current parent is left empty.
5366
5367 Set_Instance_Env (Gen_Unit, Empty);
5368
5369 -- Build the subprogram declaration, which does not appear in the
5370 -- generic template, and give it a sloc consistent with that of the
5371 -- template.
5372
5373 Set_Defining_Unit_Name (Act_Spec, Anon_Id);
5374 Set_Generic_Parent (Act_Spec, Gen_Unit);
5375 Act_Decl :=
5376 Make_Subprogram_Declaration (Sloc (Act_Spec),
5377 Specification => Act_Spec);
5378
5379 -- The aspects have been copied previously, but they have to be
5380 -- linked explicitly to the new subprogram declaration. Explicit
5381 -- pre/postconditions on the instance are analyzed below, in a
5382 -- separate step.
5383
5384 Move_Aspects (Act_Tree, To => Act_Decl);
5385 Set_Categorization_From_Pragmas (Act_Decl);
5386
5387 if Parent_Installed then
5388 Hide_Current_Scope;
5389 end if;
5390
5391 Append (Act_Decl, Renaming_List);
5392
5393 -- Contract-related source pragmas that follow a generic subprogram
5394 -- must be instantiated explicitly because they are not part of the
5395 -- subprogram template.
5396
5397 Instantiate_Subprogram_Contract
5398 (Original_Node (Gen_Decl), Renaming_List);
5399
5400 Build_Subprogram_Renaming;
5401 Analyze_Instance_And_Renamings;
5402
5403 -- If the generic is marked Import (Intrinsic), then so is the
5404 -- instance. This indicates that there is no body to instantiate. If
5405 -- generic is marked inline, so it the instance, and the anonymous
5406 -- subprogram it renames. If inlined, or else if inlining is enabled
5407 -- for the compilation, we generate the instance body even if it is
5408 -- not within the main unit.
5409
5410 if Is_Intrinsic_Subprogram (Gen_Unit) then
5411 Set_Is_Intrinsic_Subprogram (Anon_Id);
5412 Set_Is_Intrinsic_Subprogram (Act_Decl_Id);
5413
5414 if Chars (Gen_Unit) = Name_Unchecked_Conversion then
5415 Validate_Unchecked_Conversion (N, Act_Decl_Id);
5416 end if;
5417 end if;
5418
5419 -- Inherit convention from generic unit. Intrinsic convention, as for
5420 -- an instance of unchecked conversion, is not inherited because an
5421 -- explicit Ada instance has been created.
5422
5423 if Has_Convention_Pragma (Gen_Unit)
5424 and then Convention (Gen_Unit) /= Convention_Intrinsic
5425 then
5426 Set_Convention (Act_Decl_Id, Convention (Gen_Unit));
5427 Set_Is_Exported (Act_Decl_Id, Is_Exported (Gen_Unit));
5428 end if;
5429
5430 Generate_Definition (Act_Decl_Id);
5431
5432 -- Inherit all inlining-related flags which apply to the generic in
5433 -- the subprogram and its declaration.
5434
5435 Set_Is_Inlined (Act_Decl_Id, Is_Inlined (Gen_Unit));
5436 Set_Is_Inlined (Anon_Id, Is_Inlined (Gen_Unit));
5437
5438 Set_Has_Pragma_Inline (Act_Decl_Id, Has_Pragma_Inline (Gen_Unit));
5439 Set_Has_Pragma_Inline (Anon_Id, Has_Pragma_Inline (Gen_Unit));
5440
5441 -- Propagate No_Return if pragma applied to generic unit. This must
5442 -- be done explicitly because pragma does not appear in generic
5443 -- declaration (unlike the aspect case).
5444
5445 if No_Return (Gen_Unit) then
5446 Set_No_Return (Act_Decl_Id);
5447 Set_No_Return (Anon_Id);
5448 end if;
5449
5450 Set_Has_Pragma_Inline_Always
5451 (Act_Decl_Id, Has_Pragma_Inline_Always (Gen_Unit));
5452 Set_Has_Pragma_Inline_Always
5453 (Anon_Id, Has_Pragma_Inline_Always (Gen_Unit));
5454
5455 -- If the context of the instance is subject to SPARK_Mode "off" or
5456 -- the annotation is altogether missing, set the global flag which
5457 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
5458 -- the instance.
5459
5460 if SPARK_Mode /= On then
5461 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
5462
5463 -- Mark both the instance spec and the anonymous package in case
5464 -- the body is instantiated at a later pass. This preserves the
5465 -- original context in effect for the body.
5466
5467 Set_Ignore_SPARK_Mode_Pragmas (Act_Decl_Id);
5468 Set_Ignore_SPARK_Mode_Pragmas (Anon_Id);
5469 end if;
5470
5471 if not Is_Intrinsic_Subprogram (Gen_Unit) then
5472 Check_Elab_Instantiation (N);
5473 end if;
5474
5475 if Is_Dispatching_Operation (Act_Decl_Id)
5476 and then Ada_Version >= Ada_2005
5477 then
5478 declare
5479 Formal : Entity_Id;
5480
5481 begin
5482 Formal := First_Formal (Act_Decl_Id);
5483 while Present (Formal) loop
5484 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type
5485 and then Is_Controlling_Formal (Formal)
5486 and then not Can_Never_Be_Null (Formal)
5487 then
5488 Error_Msg_NE
5489 ("access parameter& is controlling,", N, Formal);
5490 Error_Msg_NE
5491 ("\corresponding parameter of & must be explicitly "
5492 & "null-excluding", N, Gen_Id);
5493 end if;
5494
5495 Next_Formal (Formal);
5496 end loop;
5497 end;
5498 end if;
5499
5500 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
5501
5502 Validate_Categorization_Dependency (N, Act_Decl_Id);
5503
5504 if not Is_Intrinsic_Subprogram (Act_Decl_Id) then
5505 Inherit_Context (Gen_Decl, N);
5506
5507 Restore_Private_Views (Pack_Id, False);
5508
5509 -- If the context requires a full instantiation, mark node for
5510 -- subsequent construction of the body.
5511
5512 if Need_Subprogram_Instance_Body (N, Act_Decl_Id) then
5513 Check_Forward_Instantiation (Gen_Decl);
5514
5515 -- The wrapper package is always delayed, because it does not
5516 -- constitute a freeze point, but to insure that the freeze node
5517 -- is placed properly, it is created directly when instantiating
5518 -- the body (otherwise the freeze node might appear to early for
5519 -- nested instantiations). For ASIS purposes, indicate that the
5520 -- wrapper package has replaced the instantiation node.
5521
5522 elsif Nkind (Parent (N)) = N_Compilation_Unit then
5523 Rewrite (N, Unit (Parent (N)));
5524 Set_Unit (Parent (N), N);
5525 end if;
5526
5527 -- Replace instance node for library-level instantiations of
5528 -- intrinsic subprograms, for ASIS use.
5529
5530 elsif Nkind (Parent (N)) = N_Compilation_Unit then
5531 Rewrite (N, Unit (Parent (N)));
5532 Set_Unit (Parent (N), N);
5533 end if;
5534
5535 if Parent_Installed then
5536 Remove_Parent;
5537 end if;
5538
5539 Restore_Hidden_Primitives (Vis_Prims_List);
5540 Restore_Env;
5541 Env_Installed := False;
5542 Generic_Renamings.Set_Last (0);
5543 Generic_Renamings_HTable.Reset;
5544 end if;
5545
5546 <<Leave>>
5547 if Has_Aspects (N) then
5548 Analyze_Aspect_Specifications (N, Act_Decl_Id);
5549 end if;
5550
5551 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
5552 Restore_Ghost_Mode (Saved_GM);
5553 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
5554
5555 exception
5556 when Instantiation_Error =>
5557 if Parent_Installed then
5558 Remove_Parent;
5559 end if;
5560
5561 if Env_Installed then
5562 Restore_Env;
5563 end if;
5564
5565 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
5566 Restore_Ghost_Mode (Saved_GM);
5567 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
5568 end Analyze_Subprogram_Instantiation;
5569
5570 -------------------------
5571 -- Get_Associated_Node --
5572 -------------------------
5573
5574 function Get_Associated_Node (N : Node_Id) return Node_Id is
5575 Assoc : Node_Id;
5576
5577 begin
5578 Assoc := Associated_Node (N);
5579
5580 if Nkind (Assoc) /= Nkind (N) then
5581 return Assoc;
5582
5583 elsif Nkind_In (Assoc, N_Aggregate, N_Extension_Aggregate) then
5584 return Assoc;
5585
5586 else
5587 -- If the node is part of an inner generic, it may itself have been
5588 -- remapped into a further generic copy. Associated_Node is otherwise
5589 -- used for the entity of the node, and will be of a different node
5590 -- kind, or else N has been rewritten as a literal or function call.
5591
5592 while Present (Associated_Node (Assoc))
5593 and then Nkind (Associated_Node (Assoc)) = Nkind (Assoc)
5594 loop
5595 Assoc := Associated_Node (Assoc);
5596 end loop;
5597
5598 -- Follow an additional link in case the final node was rewritten.
5599 -- This can only happen with nested generic units.
5600
5601 if (Nkind (Assoc) = N_Identifier or else Nkind (Assoc) in N_Op)
5602 and then Present (Associated_Node (Assoc))
5603 and then (Nkind_In (Associated_Node (Assoc), N_Function_Call,
5604 N_Explicit_Dereference,
5605 N_Integer_Literal,
5606 N_Real_Literal,
5607 N_String_Literal))
5608 then
5609 Assoc := Associated_Node (Assoc);
5610 end if;
5611
5612 -- An additional special case: an unconstrained type in an object
5613 -- declaration may have been rewritten as a local subtype constrained
5614 -- by the expression in the declaration. We need to recover the
5615 -- original entity, which may be global.
5616
5617 if Present (Original_Node (Assoc))
5618 and then Nkind (Parent (N)) = N_Object_Declaration
5619 then
5620 Assoc := Original_Node (Assoc);
5621 end if;
5622
5623 return Assoc;
5624 end if;
5625 end Get_Associated_Node;
5626
5627 ----------------------------
5628 -- Build_Function_Wrapper --
5629 ----------------------------
5630
5631 function Build_Function_Wrapper
5632 (Formal_Subp : Entity_Id;
5633 Actual_Subp : Entity_Id) return Node_Id
5634 is
5635 Loc : constant Source_Ptr := Sloc (Current_Scope);
5636 Ret_Type : constant Entity_Id := Get_Instance_Of (Etype (Formal_Subp));
5637 Actuals : List_Id;
5638 Decl : Node_Id;
5639 Func_Name : Node_Id;
5640 Func : Entity_Id;
5641 Parm_Type : Node_Id;
5642 Profile : List_Id := New_List;
5643 Spec : Node_Id;
5644 Act_F : Entity_Id;
5645 Form_F : Entity_Id;
5646 New_F : Entity_Id;
5647
5648 begin
5649 Func_Name := New_Occurrence_Of (Actual_Subp, Loc);
5650
5651 Func := Make_Defining_Identifier (Loc, Chars (Formal_Subp));
5652 Set_Ekind (Func, E_Function);
5653 Set_Is_Generic_Actual_Subprogram (Func);
5654
5655 Actuals := New_List;
5656 Profile := New_List;
5657
5658 Act_F := First_Formal (Actual_Subp);
5659 Form_F := First_Formal (Formal_Subp);
5660 while Present (Form_F) loop
5661
5662 -- Create new formal for profile of wrapper, and add a reference
5663 -- to it in the list of actuals for the enclosing call. The name
5664 -- must be that of the formal in the formal subprogram, because
5665 -- calls to it in the generic body may use named associations.
5666
5667 New_F := Make_Defining_Identifier (Loc, Chars (Form_F));
5668
5669 Parm_Type :=
5670 New_Occurrence_Of (Get_Instance_Of (Etype (Form_F)), Loc);
5671
5672 Append_To (Profile,
5673 Make_Parameter_Specification (Loc,
5674 Defining_Identifier => New_F,
5675 Parameter_Type => Parm_Type));
5676
5677 Append_To (Actuals, New_Occurrence_Of (New_F, Loc));
5678 Next_Formal (Form_F);
5679
5680 if Present (Act_F) then
5681 Next_Formal (Act_F);
5682 end if;
5683 end loop;
5684
5685 Spec :=
5686 Make_Function_Specification (Loc,
5687 Defining_Unit_Name => Func,
5688 Parameter_Specifications => Profile,
5689 Result_Definition => New_Occurrence_Of (Ret_Type, Loc));
5690
5691 Decl :=
5692 Make_Expression_Function (Loc,
5693 Specification => Spec,
5694 Expression =>
5695 Make_Function_Call (Loc,
5696 Name => Func_Name,
5697 Parameter_Associations => Actuals));
5698
5699 return Decl;
5700 end Build_Function_Wrapper;
5701
5702 ----------------------------
5703 -- Build_Operator_Wrapper --
5704 ----------------------------
5705
5706 function Build_Operator_Wrapper
5707 (Formal_Subp : Entity_Id;
5708 Actual_Subp : Entity_Id) return Node_Id
5709 is
5710 Loc : constant Source_Ptr := Sloc (Current_Scope);
5711 Ret_Type : constant Entity_Id :=
5712 Get_Instance_Of (Etype (Formal_Subp));
5713 Op_Type : constant Entity_Id :=
5714 Get_Instance_Of (Etype (First_Formal (Formal_Subp)));
5715 Is_Binary : constant Boolean :=
5716 Present (Next_Formal (First_Formal (Formal_Subp)));
5717
5718 Decl : Node_Id;
5719 Expr : Node_Id;
5720 pragma Warnings (Off, Expr);
5721 F1, F2 : Entity_Id;
5722 Func : Entity_Id;
5723 Op_Name : Name_Id;
5724 Spec : Node_Id;
5725 L, R : Node_Id;
5726
5727 begin
5728 Op_Name := Chars (Actual_Subp);
5729
5730 -- Create entities for wrapper function and its formals
5731
5732 F1 := Make_Temporary (Loc, 'A');
5733 F2 := Make_Temporary (Loc, 'B');
5734 L := New_Occurrence_Of (F1, Loc);
5735 R := New_Occurrence_Of (F2, Loc);
5736
5737 Func := Make_Defining_Identifier (Loc, Chars (Formal_Subp));
5738 Set_Ekind (Func, E_Function);
5739 Set_Is_Generic_Actual_Subprogram (Func);
5740
5741 Spec :=
5742 Make_Function_Specification (Loc,
5743 Defining_Unit_Name => Func,
5744 Parameter_Specifications => New_List (
5745 Make_Parameter_Specification (Loc,
5746 Defining_Identifier => F1,
5747 Parameter_Type => New_Occurrence_Of (Op_Type, Loc))),
5748 Result_Definition => New_Occurrence_Of (Ret_Type, Loc));
5749
5750 if Is_Binary then
5751 Append_To (Parameter_Specifications (Spec),
5752 Make_Parameter_Specification (Loc,
5753 Defining_Identifier => F2,
5754 Parameter_Type => New_Occurrence_Of (Op_Type, Loc)));
5755 end if;
5756
5757 -- Build expression as a function call, or as an operator node
5758 -- that corresponds to the name of the actual, starting with
5759 -- binary operators.
5760
5761 if Op_Name not in Any_Operator_Name then
5762 Expr :=
5763 Make_Function_Call (Loc,
5764 Name =>
5765 New_Occurrence_Of (Actual_Subp, Loc),
5766 Parameter_Associations => New_List (L));
5767
5768 if Is_Binary then
5769 Append_To (Parameter_Associations (Expr), R);
5770 end if;
5771
5772 -- Binary operators
5773
5774 elsif Is_Binary then
5775 if Op_Name = Name_Op_And then
5776 Expr := Make_Op_And (Loc, Left_Opnd => L, Right_Opnd => R);
5777 elsif Op_Name = Name_Op_Or then
5778 Expr := Make_Op_Or (Loc, Left_Opnd => L, Right_Opnd => R);
5779 elsif Op_Name = Name_Op_Xor then
5780 Expr := Make_Op_Xor (Loc, Left_Opnd => L, Right_Opnd => R);
5781 elsif Op_Name = Name_Op_Eq then
5782 Expr := Make_Op_Eq (Loc, Left_Opnd => L, Right_Opnd => R);
5783 elsif Op_Name = Name_Op_Ne then
5784 Expr := Make_Op_Ne (Loc, Left_Opnd => L, Right_Opnd => R);
5785 elsif Op_Name = Name_Op_Le then
5786 Expr := Make_Op_Le (Loc, Left_Opnd => L, Right_Opnd => R);
5787 elsif Op_Name = Name_Op_Gt then
5788 Expr := Make_Op_Gt (Loc, Left_Opnd => L, Right_Opnd => R);
5789 elsif Op_Name = Name_Op_Ge then
5790 Expr := Make_Op_Ge (Loc, Left_Opnd => L, Right_Opnd => R);
5791 elsif Op_Name = Name_Op_Lt then
5792 Expr := Make_Op_Lt (Loc, Left_Opnd => L, Right_Opnd => R);
5793 elsif Op_Name = Name_Op_Add then
5794 Expr := Make_Op_Add (Loc, Left_Opnd => L, Right_Opnd => R);
5795 elsif Op_Name = Name_Op_Subtract then
5796 Expr := Make_Op_Subtract (Loc, Left_Opnd => L, Right_Opnd => R);
5797 elsif Op_Name = Name_Op_Concat then
5798 Expr := Make_Op_Concat (Loc, Left_Opnd => L, Right_Opnd => R);
5799 elsif Op_Name = Name_Op_Multiply then
5800 Expr := Make_Op_Multiply (Loc, Left_Opnd => L, Right_Opnd => R);
5801 elsif Op_Name = Name_Op_Divide then
5802 Expr := Make_Op_Divide (Loc, Left_Opnd => L, Right_Opnd => R);
5803 elsif Op_Name = Name_Op_Mod then
5804 Expr := Make_Op_Mod (Loc, Left_Opnd => L, Right_Opnd => R);
5805 elsif Op_Name = Name_Op_Rem then
5806 Expr := Make_Op_Rem (Loc, Left_Opnd => L, Right_Opnd => R);
5807 elsif Op_Name = Name_Op_Expon then
5808 Expr := Make_Op_Expon (Loc, Left_Opnd => L, Right_Opnd => R);
5809 end if;
5810
5811 -- Unary operators
5812
5813 else
5814 if Op_Name = Name_Op_Add then
5815 Expr := Make_Op_Plus (Loc, Right_Opnd => L);
5816 elsif Op_Name = Name_Op_Subtract then
5817 Expr := Make_Op_Minus (Loc, Right_Opnd => L);
5818 elsif Op_Name = Name_Op_Abs then
5819 Expr := Make_Op_Abs (Loc, Right_Opnd => L);
5820 elsif Op_Name = Name_Op_Not then
5821 Expr := Make_Op_Not (Loc, Right_Opnd => L);
5822 end if;
5823 end if;
5824
5825 Decl :=
5826 Make_Expression_Function (Loc,
5827 Specification => Spec,
5828 Expression => Expr);
5829
5830 return Decl;
5831 end Build_Operator_Wrapper;
5832
5833 -------------------------------------------
5834 -- Build_Instance_Compilation_Unit_Nodes --
5835 -------------------------------------------
5836
5837 procedure Build_Instance_Compilation_Unit_Nodes
5838 (N : Node_Id;
5839 Act_Body : Node_Id;
5840 Act_Decl : Node_Id)
5841 is
5842 Decl_Cunit : Node_Id;
5843 Body_Cunit : Node_Id;
5844 Citem : Node_Id;
5845 New_Main : constant Entity_Id := Defining_Entity (Act_Decl);
5846 Old_Main : constant Entity_Id := Cunit_Entity (Main_Unit);
5847
5848 begin
5849 -- A new compilation unit node is built for the instance declaration
5850
5851 Decl_Cunit :=
5852 Make_Compilation_Unit (Sloc (N),
5853 Context_Items => Empty_List,
5854 Unit => Act_Decl,
5855 Aux_Decls_Node => Make_Compilation_Unit_Aux (Sloc (N)));
5856
5857 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
5858
5859 -- The new compilation unit is linked to its body, but both share the
5860 -- same file, so we do not set Body_Required on the new unit so as not
5861 -- to create a spurious dependency on a non-existent body in the ali.
5862 -- This simplifies CodePeer unit traversal.
5863
5864 -- We use the original instantiation compilation unit as the resulting
5865 -- compilation unit of the instance, since this is the main unit.
5866
5867 Rewrite (N, Act_Body);
5868
5869 -- Propagate the aspect specifications from the package body template to
5870 -- the instantiated version of the package body.
5871
5872 if Has_Aspects (Act_Body) then
5873 Set_Aspect_Specifications
5874 (N, New_Copy_List_Tree (Aspect_Specifications (Act_Body)));
5875 end if;
5876
5877 Body_Cunit := Parent (N);
5878
5879 -- The two compilation unit nodes are linked by the Library_Unit field
5880
5881 Set_Library_Unit (Decl_Cunit, Body_Cunit);
5882 Set_Library_Unit (Body_Cunit, Decl_Cunit);
5883
5884 -- Preserve the private nature of the package if needed
5885
5886 Set_Private_Present (Decl_Cunit, Private_Present (Body_Cunit));
5887
5888 -- If the instance is not the main unit, its context, categorization
5889 -- and elaboration entity are not relevant to the compilation.
5890
5891 if Body_Cunit /= Cunit (Main_Unit) then
5892 Make_Instance_Unit (Body_Cunit, In_Main => False);
5893 return;
5894 end if;
5895
5896 -- The context clause items on the instantiation, which are now attached
5897 -- to the body compilation unit (since the body overwrote the original
5898 -- instantiation node), semantically belong on the spec, so copy them
5899 -- there. It's harmless to leave them on the body as well. In fact one
5900 -- could argue that they belong in both places.
5901
5902 Citem := First (Context_Items (Body_Cunit));
5903 while Present (Citem) loop
5904 Append (New_Copy (Citem), Context_Items (Decl_Cunit));
5905 Next (Citem);
5906 end loop;
5907
5908 -- Propagate categorization flags on packages, so that they appear in
5909 -- the ali file for the spec of the unit.
5910
5911 if Ekind (New_Main) = E_Package then
5912 Set_Is_Pure (Old_Main, Is_Pure (New_Main));
5913 Set_Is_Preelaborated (Old_Main, Is_Preelaborated (New_Main));
5914 Set_Is_Remote_Types (Old_Main, Is_Remote_Types (New_Main));
5915 Set_Is_Shared_Passive (Old_Main, Is_Shared_Passive (New_Main));
5916 Set_Is_Remote_Call_Interface
5917 (Old_Main, Is_Remote_Call_Interface (New_Main));
5918 end if;
5919
5920 -- Make entry in Units table, so that binder can generate call to
5921 -- elaboration procedure for body, if any.
5922
5923 Make_Instance_Unit (Body_Cunit, In_Main => True);
5924 Main_Unit_Entity := New_Main;
5925 Set_Cunit_Entity (Main_Unit, Main_Unit_Entity);
5926
5927 -- Build elaboration entity, since the instance may certainly generate
5928 -- elaboration code requiring a flag for protection.
5929
5930 Build_Elaboration_Entity (Decl_Cunit, New_Main);
5931 end Build_Instance_Compilation_Unit_Nodes;
5932
5933 -----------------------------
5934 -- Check_Access_Definition --
5935 -----------------------------
5936
5937 procedure Check_Access_Definition (N : Node_Id) is
5938 begin
5939 pragma Assert
5940 (Ada_Version >= Ada_2005 and then Present (Access_Definition (N)));
5941 null;
5942 end Check_Access_Definition;
5943
5944 -----------------------------------
5945 -- Check_Formal_Package_Instance --
5946 -----------------------------------
5947
5948 -- If the formal has specific parameters, they must match those of the
5949 -- actual. Both of them are instances, and the renaming declarations for
5950 -- their formal parameters appear in the same order in both. The analyzed
5951 -- formal has been analyzed in the context of the current instance.
5952
5953 procedure Check_Formal_Package_Instance
5954 (Formal_Pack : Entity_Id;
5955 Actual_Pack : Entity_Id)
5956 is
5957 E1 : Entity_Id := First_Entity (Actual_Pack);
5958 E2 : Entity_Id := First_Entity (Formal_Pack);
5959 Prev_E1 : Entity_Id;
5960
5961 Expr1 : Node_Id;
5962 Expr2 : Node_Id;
5963
5964 procedure Check_Mismatch (B : Boolean);
5965 -- Common error routine for mismatch between the parameters of the
5966 -- actual instance and those of the formal package.
5967
5968 function Same_Instantiated_Constant (E1, E2 : Entity_Id) return Boolean;
5969 -- The formal may come from a nested formal package, and the actual may
5970 -- have been constant-folded. To determine whether the two denote the
5971 -- same entity we may have to traverse several definitions to recover
5972 -- the ultimate entity that they refer to.
5973
5974 function Same_Instantiated_Function (E1, E2 : Entity_Id) return Boolean;
5975 -- The formal and the actual must be identical, but if both are
5976 -- given by attributes they end up renaming different generated bodies,
5977 -- and we must verify that the attributes themselves match.
5978
5979 function Same_Instantiated_Variable (E1, E2 : Entity_Id) return Boolean;
5980 -- Similarly, if the formal comes from a nested formal package, the
5981 -- actual may designate the formal through multiple renamings, which
5982 -- have to be followed to determine the original variable in question.
5983
5984 --------------------
5985 -- Check_Mismatch --
5986 --------------------
5987
5988 procedure Check_Mismatch (B : Boolean) is
5989 -- A Formal_Type_Declaration for a derived private type is rewritten
5990 -- as a private extension decl. (see Analyze_Formal_Derived_Type),
5991 -- which is why we examine the original node.
5992
5993 Kind : constant Node_Kind := Nkind (Original_Node (Parent (E2)));
5994
5995 begin
5996 if Kind = N_Formal_Type_Declaration then
5997 return;
5998
5999 elsif Nkind_In (Kind, N_Formal_Object_Declaration,
6000 N_Formal_Package_Declaration)
6001 or else Kind in N_Formal_Subprogram_Declaration
6002 then
6003 null;
6004
6005 -- Ada 2012: If both formal and actual are incomplete types they
6006 -- are conformant.
6007
6008 elsif Is_Incomplete_Type (E1) and then Is_Incomplete_Type (E2) then
6009 null;
6010
6011 elsif B then
6012 Error_Msg_NE
6013 ("actual for & in actual instance does not match formal",
6014 Parent (Actual_Pack), E1);
6015 end if;
6016 end Check_Mismatch;
6017
6018 --------------------------------
6019 -- Same_Instantiated_Constant --
6020 --------------------------------
6021
6022 function Same_Instantiated_Constant
6023 (E1, E2 : Entity_Id) return Boolean
6024 is
6025 Ent : Entity_Id;
6026
6027 begin
6028 Ent := E2;
6029 while Present (Ent) loop
6030 if E1 = Ent then
6031 return True;
6032
6033 elsif Ekind (Ent) /= E_Constant then
6034 return False;
6035
6036 elsif Is_Entity_Name (Constant_Value (Ent)) then
6037 if Entity (Constant_Value (Ent)) = E1 then
6038 return True;
6039 else
6040 Ent := Entity (Constant_Value (Ent));
6041 end if;
6042
6043 -- The actual may be a constant that has been folded. Recover
6044 -- original name.
6045
6046 elsif Is_Entity_Name (Original_Node (Constant_Value (Ent))) then
6047 Ent := Entity (Original_Node (Constant_Value (Ent)));
6048
6049 else
6050 return False;
6051 end if;
6052 end loop;
6053
6054 return False;
6055 end Same_Instantiated_Constant;
6056
6057 --------------------------------
6058 -- Same_Instantiated_Function --
6059 --------------------------------
6060
6061 function Same_Instantiated_Function
6062 (E1, E2 : Entity_Id) return Boolean
6063 is
6064 U1, U2 : Node_Id;
6065 begin
6066 if Alias (E1) = Alias (E2) then
6067 return True;
6068
6069 elsif Present (Alias (E2)) then
6070 U1 := Original_Node (Unit_Declaration_Node (E1));
6071 U2 := Original_Node (Unit_Declaration_Node (Alias (E2)));
6072
6073 return Nkind (U1) = N_Subprogram_Renaming_Declaration
6074 and then Nkind (Name (U1)) = N_Attribute_Reference
6075
6076 and then Nkind (U2) = N_Subprogram_Renaming_Declaration
6077 and then Nkind (Name (U2)) = N_Attribute_Reference
6078
6079 and then
6080 Attribute_Name (Name (U1)) = Attribute_Name (Name (U2));
6081 else
6082 return False;
6083 end if;
6084 end Same_Instantiated_Function;
6085
6086 --------------------------------
6087 -- Same_Instantiated_Variable --
6088 --------------------------------
6089
6090 function Same_Instantiated_Variable
6091 (E1, E2 : Entity_Id) return Boolean
6092 is
6093 function Original_Entity (E : Entity_Id) return Entity_Id;
6094 -- Follow chain of renamings to the ultimate ancestor
6095
6096 ---------------------
6097 -- Original_Entity --
6098 ---------------------
6099
6100 function Original_Entity (E : Entity_Id) return Entity_Id is
6101 Orig : Entity_Id;
6102
6103 begin
6104 Orig := E;
6105 while Nkind (Parent (Orig)) = N_Object_Renaming_Declaration
6106 and then Present (Renamed_Object (Orig))
6107 and then Is_Entity_Name (Renamed_Object (Orig))
6108 loop
6109 Orig := Entity (Renamed_Object (Orig));
6110 end loop;
6111
6112 return Orig;
6113 end Original_Entity;
6114
6115 -- Start of processing for Same_Instantiated_Variable
6116
6117 begin
6118 return Ekind (E1) = Ekind (E2)
6119 and then Original_Entity (E1) = Original_Entity (E2);
6120 end Same_Instantiated_Variable;
6121
6122 -- Start of processing for Check_Formal_Package_Instance
6123
6124 begin
6125 Prev_E1 := E1;
6126 while Present (E1) and then Present (E2) loop
6127 exit when Ekind (E1) = E_Package
6128 and then Renamed_Entity (E1) = Renamed_Entity (Actual_Pack);
6129
6130 -- If the formal is the renaming of the formal package, this
6131 -- is the end of its formal part, which may occur before the
6132 -- end of the formal part in the actual in the presence of
6133 -- defaulted parameters in the formal package.
6134
6135 exit when Nkind (Parent (E2)) = N_Package_Renaming_Declaration
6136 and then Renamed_Entity (E2) = Scope (E2);
6137
6138 -- The analysis of the actual may generate additional internal
6139 -- entities. If the formal is defaulted, there is no corresponding
6140 -- analysis and the internal entities must be skipped, until we
6141 -- find corresponding entities again.
6142
6143 if Comes_From_Source (E2)
6144 and then not Comes_From_Source (E1)
6145 and then Chars (E1) /= Chars (E2)
6146 then
6147 while Present (E1) and then Chars (E1) /= Chars (E2) loop
6148 Next_Entity (E1);
6149 end loop;
6150 end if;
6151
6152 if No (E1) then
6153 return;
6154
6155 -- Entities may be declared without full declaration, such as
6156 -- itypes and predefined operators (concatenation for arrays, eg).
6157 -- Skip it and keep the formal entity to find a later match for it.
6158
6159 elsif No (Parent (E2)) and then Ekind (E1) /= Ekind (E2) then
6160 E1 := Prev_E1;
6161 goto Next_E;
6162
6163 -- If the formal entity comes from a formal declaration, it was
6164 -- defaulted in the formal package, and no check is needed on it.
6165
6166 elsif Nkind_In (Original_Node (Parent (E2)),
6167 N_Formal_Object_Declaration,
6168 N_Formal_Type_Declaration)
6169 then
6170 -- If the formal is a tagged type the corresponding class-wide
6171 -- type has been generated as well, and it must be skipped.
6172
6173 if Is_Type (E2) and then Is_Tagged_Type (E2) then
6174 Next_Entity (E2);
6175 end if;
6176
6177 goto Next_E;
6178
6179 -- Ditto for defaulted formal subprograms.
6180
6181 elsif Is_Overloadable (E1)
6182 and then Nkind (Unit_Declaration_Node (E2)) in
6183 N_Formal_Subprogram_Declaration
6184 then
6185 goto Next_E;
6186
6187 elsif Is_Type (E1) then
6188
6189 -- Subtypes must statically match. E1, E2 are the local entities
6190 -- that are subtypes of the actuals. Itypes generated for other
6191 -- parameters need not be checked, the check will be performed
6192 -- on the parameters themselves.
6193
6194 -- If E2 is a formal type declaration, it is a defaulted parameter
6195 -- and needs no checking.
6196
6197 if not Is_Itype (E1) and then not Is_Itype (E2) then
6198 Check_Mismatch
6199 (not Is_Type (E2)
6200 or else Etype (E1) /= Etype (E2)
6201 or else not Subtypes_Statically_Match (E1, E2));
6202 end if;
6203
6204 elsif Ekind (E1) = E_Constant then
6205
6206 -- IN parameters must denote the same static value, or the same
6207 -- constant, or the literal null.
6208
6209 Expr1 := Expression (Parent (E1));
6210
6211 if Ekind (E2) /= E_Constant then
6212 Check_Mismatch (True);
6213 goto Next_E;
6214 else
6215 Expr2 := Expression (Parent (E2));
6216 end if;
6217
6218 if Is_OK_Static_Expression (Expr1) then
6219 if not Is_OK_Static_Expression (Expr2) then
6220 Check_Mismatch (True);
6221
6222 elsif Is_Discrete_Type (Etype (E1)) then
6223 declare
6224 V1 : constant Uint := Expr_Value (Expr1);
6225 V2 : constant Uint := Expr_Value (Expr2);
6226 begin
6227 Check_Mismatch (V1 /= V2);
6228 end;
6229
6230 elsif Is_Real_Type (Etype (E1)) then
6231 declare
6232 V1 : constant Ureal := Expr_Value_R (Expr1);
6233 V2 : constant Ureal := Expr_Value_R (Expr2);
6234 begin
6235 Check_Mismatch (V1 /= V2);
6236 end;
6237
6238 elsif Is_String_Type (Etype (E1))
6239 and then Nkind (Expr1) = N_String_Literal
6240 then
6241 if Nkind (Expr2) /= N_String_Literal then
6242 Check_Mismatch (True);
6243 else
6244 Check_Mismatch
6245 (not String_Equal (Strval (Expr1), Strval (Expr2)));
6246 end if;
6247 end if;
6248
6249 elsif Is_Entity_Name (Expr1) then
6250 if Is_Entity_Name (Expr2) then
6251 if Entity (Expr1) = Entity (Expr2) then
6252 null;
6253 else
6254 Check_Mismatch
6255 (not Same_Instantiated_Constant
6256 (Entity (Expr1), Entity (Expr2)));
6257 end if;
6258
6259 else
6260 Check_Mismatch (True);
6261 end if;
6262
6263 elsif Is_Entity_Name (Original_Node (Expr1))
6264 and then Is_Entity_Name (Expr2)
6265 and then Same_Instantiated_Constant
6266 (Entity (Original_Node (Expr1)), Entity (Expr2))
6267 then
6268 null;
6269
6270 elsif Nkind (Expr1) = N_Null then
6271 Check_Mismatch (Nkind (Expr1) /= N_Null);
6272
6273 else
6274 Check_Mismatch (True);
6275 end if;
6276
6277 elsif Ekind (E1) = E_Variable then
6278 Check_Mismatch (not Same_Instantiated_Variable (E1, E2));
6279
6280 elsif Ekind (E1) = E_Package then
6281 Check_Mismatch
6282 (Ekind (E1) /= Ekind (E2)
6283 or else Renamed_Object (E1) /= Renamed_Object (E2));
6284
6285 elsif Is_Overloadable (E1) then
6286
6287 -- Verify that the actual subprograms match. Note that actuals
6288 -- that are attributes are rewritten as subprograms. If the
6289 -- subprogram in the formal package is defaulted, no check is
6290 -- needed. Note that this can only happen in Ada 2005 when the
6291 -- formal package can be partially parameterized.
6292
6293 if Nkind (Unit_Declaration_Node (E1)) =
6294 N_Subprogram_Renaming_Declaration
6295 and then From_Default (Unit_Declaration_Node (E1))
6296 then
6297 null;
6298
6299 -- If the formal package has an "others" box association that
6300 -- covers this formal, there is no need for a check either.
6301
6302 elsif Nkind (Unit_Declaration_Node (E2)) in
6303 N_Formal_Subprogram_Declaration
6304 and then Box_Present (Unit_Declaration_Node (E2))
6305 then
6306 null;
6307
6308 -- No check needed if subprogram is a defaulted null procedure
6309
6310 elsif No (Alias (E2))
6311 and then Ekind (E2) = E_Procedure
6312 and then
6313 Null_Present (Specification (Unit_Declaration_Node (E2)))
6314 then
6315 null;
6316
6317 -- Otherwise the actual in the formal and the actual in the
6318 -- instantiation of the formal must match, up to renamings.
6319
6320 else
6321 Check_Mismatch
6322 (Ekind (E2) /= Ekind (E1)
6323 or else not Same_Instantiated_Function (E1, E2));
6324 end if;
6325
6326 else
6327 raise Program_Error;
6328 end if;
6329
6330 <<Next_E>>
6331 Prev_E1 := E1;
6332 Next_Entity (E1);
6333 Next_Entity (E2);
6334 end loop;
6335 end Check_Formal_Package_Instance;
6336
6337 ---------------------------
6338 -- Check_Formal_Packages --
6339 ---------------------------
6340
6341 procedure Check_Formal_Packages (P_Id : Entity_Id) is
6342 E : Entity_Id;
6343 Formal_P : Entity_Id;
6344 Formal_Decl : Node_Id;
6345
6346 begin
6347 -- Iterate through the declarations in the instance, looking for package
6348 -- renaming declarations that denote instances of formal packages. Stop
6349 -- when we find the renaming of the current package itself. The
6350 -- declaration for a formal package without a box is followed by an
6351 -- internal entity that repeats the instantiation.
6352
6353 E := First_Entity (P_Id);
6354 while Present (E) loop
6355 if Ekind (E) = E_Package then
6356 if Renamed_Object (E) = P_Id then
6357 exit;
6358
6359 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
6360 null;
6361
6362 else
6363 Formal_Decl := Parent (Associated_Formal_Package (E));
6364
6365 -- Nothing to check if the formal has a box or an others_clause
6366 -- (necessarily with a box).
6367
6368 if Box_Present (Formal_Decl) then
6369 null;
6370
6371 elsif Nkind (First (Generic_Associations (Formal_Decl))) =
6372 N_Others_Choice
6373 then
6374 -- The internal validating package was generated but formal
6375 -- and instance are known to be compatible.
6376
6377 Formal_P := Next_Entity (E);
6378 Remove (Unit_Declaration_Node (Formal_P));
6379
6380 else
6381 Formal_P := Next_Entity (E);
6382 Check_Formal_Package_Instance (Formal_P, E);
6383
6384 -- After checking, remove the internal validating package.
6385 -- It is only needed for semantic checks, and as it may
6386 -- contain generic formal declarations it should not reach
6387 -- gigi.
6388
6389 Remove (Unit_Declaration_Node (Formal_P));
6390 end if;
6391 end if;
6392 end if;
6393
6394 Next_Entity (E);
6395 end loop;
6396 end Check_Formal_Packages;
6397
6398 ---------------------------------
6399 -- Check_Forward_Instantiation --
6400 ---------------------------------
6401
6402 procedure Check_Forward_Instantiation (Decl : Node_Id) is
6403 S : Entity_Id;
6404 Gen_Comp : Entity_Id := Cunit_Entity (Get_Source_Unit (Decl));
6405
6406 begin
6407 -- The instantiation appears before the generic body if we are in the
6408 -- scope of the unit containing the generic, either in its spec or in
6409 -- the package body, and before the generic body.
6410
6411 if Ekind (Gen_Comp) = E_Package_Body then
6412 Gen_Comp := Spec_Entity (Gen_Comp);
6413 end if;
6414
6415 if In_Open_Scopes (Gen_Comp)
6416 and then No (Corresponding_Body (Decl))
6417 then
6418 S := Current_Scope;
6419
6420 while Present (S)
6421 and then not Is_Compilation_Unit (S)
6422 and then not Is_Child_Unit (S)
6423 loop
6424 if Ekind (S) = E_Package then
6425 Set_Has_Forward_Instantiation (S);
6426 end if;
6427
6428 S := Scope (S);
6429 end loop;
6430 end if;
6431 end Check_Forward_Instantiation;
6432
6433 ---------------------------
6434 -- Check_Generic_Actuals --
6435 ---------------------------
6436
6437 -- The visibility of the actuals may be different between the point of
6438 -- generic instantiation and the instantiation of the body.
6439
6440 procedure Check_Generic_Actuals
6441 (Instance : Entity_Id;
6442 Is_Formal_Box : Boolean)
6443 is
6444 E : Entity_Id;
6445 Astype : Entity_Id;
6446
6447 function Denotes_Previous_Actual (Typ : Entity_Id) return Boolean;
6448 -- For a formal that is an array type, the component type is often a
6449 -- previous formal in the same unit. The privacy status of the component
6450 -- type will have been examined earlier in the traversal of the
6451 -- corresponding actuals, and this status should not be modified for
6452 -- the array (sub)type itself. However, if the base type of the array
6453 -- (sub)type is private, its full view must be restored in the body to
6454 -- be consistent with subsequent index subtypes, etc.
6455 --
6456 -- To detect this case we have to rescan the list of formals, which is
6457 -- usually short enough to ignore the resulting inefficiency.
6458
6459 -----------------------------
6460 -- Denotes_Previous_Actual --
6461 -----------------------------
6462
6463 function Denotes_Previous_Actual (Typ : Entity_Id) return Boolean is
6464 Prev : Entity_Id;
6465
6466 begin
6467 Prev := First_Entity (Instance);
6468 while Present (Prev) loop
6469 if Is_Type (Prev)
6470 and then Nkind (Parent (Prev)) = N_Subtype_Declaration
6471 and then Is_Entity_Name (Subtype_Indication (Parent (Prev)))
6472 and then Entity (Subtype_Indication (Parent (Prev))) = Typ
6473 then
6474 return True;
6475
6476 elsif Prev = E then
6477 return False;
6478
6479 else
6480 Next_Entity (Prev);
6481 end if;
6482 end loop;
6483
6484 return False;
6485 end Denotes_Previous_Actual;
6486
6487 -- Start of processing for Check_Generic_Actuals
6488
6489 begin
6490 E := First_Entity (Instance);
6491 while Present (E) loop
6492 if Is_Type (E)
6493 and then Nkind (Parent (E)) = N_Subtype_Declaration
6494 and then Scope (Etype (E)) /= Instance
6495 and then Is_Entity_Name (Subtype_Indication (Parent (E)))
6496 then
6497 if Is_Array_Type (E)
6498 and then not Is_Private_Type (Etype (E))
6499 and then Denotes_Previous_Actual (Component_Type (E))
6500 then
6501 null;
6502 else
6503 Check_Private_View (Subtype_Indication (Parent (E)));
6504 end if;
6505
6506 Set_Is_Generic_Actual_Type (E, True);
6507 Set_Is_Hidden (E, False);
6508 Set_Is_Potentially_Use_Visible (E, In_Use (Instance));
6509
6510 -- We constructed the generic actual type as a subtype of the
6511 -- supplied type. This means that it normally would not inherit
6512 -- subtype specific attributes of the actual, which is wrong for
6513 -- the generic case.
6514
6515 Astype := Ancestor_Subtype (E);
6516
6517 if No (Astype) then
6518
6519 -- This can happen when E is an itype that is the full view of
6520 -- a private type completed, e.g. with a constrained array. In
6521 -- that case, use the first subtype, which will carry size
6522 -- information. The base type itself is unconstrained and will
6523 -- not carry it.
6524
6525 Astype := First_Subtype (E);
6526 end if;
6527
6528 Set_Size_Info (E, (Astype));
6529 Set_RM_Size (E, RM_Size (Astype));
6530 Set_First_Rep_Item (E, First_Rep_Item (Astype));
6531
6532 if Is_Discrete_Or_Fixed_Point_Type (E) then
6533 Set_RM_Size (E, RM_Size (Astype));
6534
6535 -- In nested instances, the base type of an access actual may
6536 -- itself be private, and need to be exchanged.
6537
6538 elsif Is_Access_Type (E)
6539 and then Is_Private_Type (Etype (E))
6540 then
6541 Check_Private_View
6542 (New_Occurrence_Of (Etype (E), Sloc (Instance)));
6543 end if;
6544
6545 elsif Ekind (E) = E_Package then
6546
6547 -- If this is the renaming for the current instance, we're done.
6548 -- Otherwise it is a formal package. If the corresponding formal
6549 -- was declared with a box, the (instantiations of the) generic
6550 -- formal part are also visible. Otherwise, ignore the entity
6551 -- created to validate the actuals.
6552
6553 if Renamed_Object (E) = Instance then
6554 exit;
6555
6556 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
6557 null;
6558
6559 -- The visibility of a formal of an enclosing generic is already
6560 -- correct.
6561
6562 elsif Denotes_Formal_Package (E) then
6563 null;
6564
6565 elsif Present (Associated_Formal_Package (E))
6566 and then not Is_Generic_Formal (E)
6567 then
6568 if Box_Present (Parent (Associated_Formal_Package (E))) then
6569 Check_Generic_Actuals (Renamed_Object (E), True);
6570
6571 else
6572 Check_Generic_Actuals (Renamed_Object (E), False);
6573 end if;
6574
6575 Set_Is_Hidden (E, False);
6576 end if;
6577
6578 -- If this is a subprogram instance (in a wrapper package) the
6579 -- actual is fully visible.
6580
6581 elsif Is_Wrapper_Package (Instance) then
6582 Set_Is_Hidden (E, False);
6583
6584 -- If the formal package is declared with a box, or if the formal
6585 -- parameter is defaulted, it is visible in the body.
6586
6587 elsif Is_Formal_Box or else Is_Visible_Formal (E) then
6588 Set_Is_Hidden (E, False);
6589 end if;
6590
6591 if Ekind (E) = E_Constant then
6592
6593 -- If the type of the actual is a private type declared in the
6594 -- enclosing scope of the generic unit, the body of the generic
6595 -- sees the full view of the type (because it has to appear in
6596 -- the corresponding package body). If the type is private now,
6597 -- exchange views to restore the proper visiblity in the instance.
6598
6599 declare
6600 Typ : constant Entity_Id := Base_Type (Etype (E));
6601 -- The type of the actual
6602
6603 Gen_Id : Entity_Id;
6604 -- The generic unit
6605
6606 Parent_Scope : Entity_Id;
6607 -- The enclosing scope of the generic unit
6608
6609 begin
6610 if Is_Wrapper_Package (Instance) then
6611 Gen_Id :=
6612 Generic_Parent
6613 (Specification
6614 (Unit_Declaration_Node
6615 (Related_Instance (Instance))));
6616 else
6617 Gen_Id :=
6618 Generic_Parent (Package_Specification (Instance));
6619 end if;
6620
6621 Parent_Scope := Scope (Gen_Id);
6622
6623 -- The exchange is only needed if the generic is defined
6624 -- within a package which is not a common ancestor of the
6625 -- scope of the instance, and is not already in scope.
6626
6627 if Is_Private_Type (Typ)
6628 and then Scope (Typ) = Parent_Scope
6629 and then Scope (Instance) /= Parent_Scope
6630 and then Ekind (Parent_Scope) = E_Package
6631 and then not Is_Child_Unit (Gen_Id)
6632 then
6633 Switch_View (Typ);
6634
6635 -- If the type of the entity is a subtype, it may also have
6636 -- to be made visible, together with the base type of its
6637 -- full view, after exchange.
6638
6639 if Is_Private_Type (Etype (E)) then
6640 Switch_View (Etype (E));
6641 Switch_View (Base_Type (Etype (E)));
6642 end if;
6643 end if;
6644 end;
6645 end if;
6646
6647 Next_Entity (E);
6648 end loop;
6649 end Check_Generic_Actuals;
6650
6651 ------------------------------
6652 -- Check_Generic_Child_Unit --
6653 ------------------------------
6654
6655 procedure Check_Generic_Child_Unit
6656 (Gen_Id : Node_Id;
6657 Parent_Installed : in out Boolean)
6658 is
6659 Loc : constant Source_Ptr := Sloc (Gen_Id);
6660 Gen_Par : Entity_Id := Empty;
6661 E : Entity_Id;
6662 Inst_Par : Entity_Id;
6663 S : Node_Id;
6664
6665 function Find_Generic_Child
6666 (Scop : Entity_Id;
6667 Id : Node_Id) return Entity_Id;
6668 -- Search generic parent for possible child unit with the given name
6669
6670 function In_Enclosing_Instance return Boolean;
6671 -- Within an instance of the parent, the child unit may be denoted by
6672 -- a simple name, or an abbreviated expanded name. Examine enclosing
6673 -- scopes to locate a possible parent instantiation.
6674
6675 ------------------------
6676 -- Find_Generic_Child --
6677 ------------------------
6678
6679 function Find_Generic_Child
6680 (Scop : Entity_Id;
6681 Id : Node_Id) return Entity_Id
6682 is
6683 E : Entity_Id;
6684
6685 begin
6686 -- If entity of name is already set, instance has already been
6687 -- resolved, e.g. in an enclosing instantiation.
6688
6689 if Present (Entity (Id)) then
6690 if Scope (Entity (Id)) = Scop then
6691 return Entity (Id);
6692 else
6693 return Empty;
6694 end if;
6695
6696 else
6697 E := First_Entity (Scop);
6698 while Present (E) loop
6699 if Chars (E) = Chars (Id)
6700 and then Is_Child_Unit (E)
6701 then
6702 if Is_Child_Unit (E)
6703 and then not Is_Visible_Lib_Unit (E)
6704 then
6705 Error_Msg_NE
6706 ("generic child unit& is not visible", Gen_Id, E);
6707 end if;
6708
6709 Set_Entity (Id, E);
6710 return E;
6711 end if;
6712
6713 Next_Entity (E);
6714 end loop;
6715
6716 return Empty;
6717 end if;
6718 end Find_Generic_Child;
6719
6720 ---------------------------
6721 -- In_Enclosing_Instance --
6722 ---------------------------
6723
6724 function In_Enclosing_Instance return Boolean is
6725 Enclosing_Instance : Node_Id;
6726 Instance_Decl : Node_Id;
6727
6728 begin
6729 -- We do not inline any call that contains instantiations, except
6730 -- for instantiations of Unchecked_Conversion, so if we are within
6731 -- an inlined body the current instance does not require parents.
6732
6733 if In_Inlined_Body then
6734 pragma Assert (Chars (Gen_Id) = Name_Unchecked_Conversion);
6735 return False;
6736 end if;
6737
6738 -- Loop to check enclosing scopes
6739
6740 Enclosing_Instance := Current_Scope;
6741 while Present (Enclosing_Instance) loop
6742 Instance_Decl := Unit_Declaration_Node (Enclosing_Instance);
6743
6744 if Ekind (Enclosing_Instance) = E_Package
6745 and then Is_Generic_Instance (Enclosing_Instance)
6746 and then Present
6747 (Generic_Parent (Specification (Instance_Decl)))
6748 then
6749 -- Check whether the generic we are looking for is a child of
6750 -- this instance.
6751
6752 E := Find_Generic_Child
6753 (Generic_Parent (Specification (Instance_Decl)), Gen_Id);
6754 exit when Present (E);
6755
6756 else
6757 E := Empty;
6758 end if;
6759
6760 Enclosing_Instance := Scope (Enclosing_Instance);
6761 end loop;
6762
6763 if No (E) then
6764
6765 -- Not a child unit
6766
6767 Analyze (Gen_Id);
6768 return False;
6769
6770 else
6771 Rewrite (Gen_Id,
6772 Make_Expanded_Name (Loc,
6773 Chars => Chars (E),
6774 Prefix => New_Occurrence_Of (Enclosing_Instance, Loc),
6775 Selector_Name => New_Occurrence_Of (E, Loc)));
6776
6777 Set_Entity (Gen_Id, E);
6778 Set_Etype (Gen_Id, Etype (E));
6779 Parent_Installed := False; -- Already in scope.
6780 return True;
6781 end if;
6782 end In_Enclosing_Instance;
6783
6784 -- Start of processing for Check_Generic_Child_Unit
6785
6786 begin
6787 -- If the name of the generic is given by a selected component, it may
6788 -- be the name of a generic child unit, and the prefix is the name of an
6789 -- instance of the parent, in which case the child unit must be visible.
6790 -- If this instance is not in scope, it must be placed there and removed
6791 -- after instantiation, because what is being instantiated is not the
6792 -- original child, but the corresponding child present in the instance
6793 -- of the parent.
6794
6795 -- If the child is instantiated within the parent, it can be given by
6796 -- a simple name. In this case the instance is already in scope, but
6797 -- the child generic must be recovered from the generic parent as well.
6798
6799 if Nkind (Gen_Id) = N_Selected_Component then
6800 S := Selector_Name (Gen_Id);
6801 Analyze (Prefix (Gen_Id));
6802 Inst_Par := Entity (Prefix (Gen_Id));
6803
6804 if Ekind (Inst_Par) = E_Package
6805 and then Present (Renamed_Object (Inst_Par))
6806 then
6807 Inst_Par := Renamed_Object (Inst_Par);
6808 end if;
6809
6810 if Ekind (Inst_Par) = E_Package then
6811 if Nkind (Parent (Inst_Par)) = N_Package_Specification then
6812 Gen_Par := Generic_Parent (Parent (Inst_Par));
6813
6814 elsif Nkind (Parent (Inst_Par)) = N_Defining_Program_Unit_Name
6815 and then
6816 Nkind (Parent (Parent (Inst_Par))) = N_Package_Specification
6817 then
6818 Gen_Par := Generic_Parent (Parent (Parent (Inst_Par)));
6819 end if;
6820
6821 elsif Ekind (Inst_Par) = E_Generic_Package
6822 and then Nkind (Parent (Gen_Id)) = N_Formal_Package_Declaration
6823 then
6824 -- A formal package may be a real child package, and not the
6825 -- implicit instance within a parent. In this case the child is
6826 -- not visible and has to be retrieved explicitly as well.
6827
6828 Gen_Par := Inst_Par;
6829 end if;
6830
6831 if Present (Gen_Par) then
6832
6833 -- The prefix denotes an instantiation. The entity itself may be a
6834 -- nested generic, or a child unit.
6835
6836 E := Find_Generic_Child (Gen_Par, S);
6837
6838 if Present (E) then
6839 Change_Selected_Component_To_Expanded_Name (Gen_Id);
6840 Set_Entity (Gen_Id, E);
6841 Set_Etype (Gen_Id, Etype (E));
6842 Set_Entity (S, E);
6843 Set_Etype (S, Etype (E));
6844
6845 -- Indicate that this is a reference to the parent
6846
6847 if In_Extended_Main_Source_Unit (Gen_Id) then
6848 Set_Is_Instantiated (Inst_Par);
6849 end if;
6850
6851 -- A common mistake is to replicate the naming scheme of a
6852 -- hierarchy by instantiating a generic child directly, rather
6853 -- than the implicit child in a parent instance:
6854
6855 -- generic .. package Gpar is ..
6856 -- generic .. package Gpar.Child is ..
6857 -- package Par is new Gpar ();
6858
6859 -- with Gpar.Child;
6860 -- package Par.Child is new Gpar.Child ();
6861 -- rather than Par.Child
6862
6863 -- In this case the instantiation is within Par, which is an
6864 -- instance, but Gpar does not denote Par because we are not IN
6865 -- the instance of Gpar, so this is illegal. The test below
6866 -- recognizes this particular case.
6867
6868 if Is_Child_Unit (E)
6869 and then not Comes_From_Source (Entity (Prefix (Gen_Id)))
6870 and then (not In_Instance
6871 or else Nkind (Parent (Parent (Gen_Id))) =
6872 N_Compilation_Unit)
6873 then
6874 Error_Msg_N
6875 ("prefix of generic child unit must be instance of parent",
6876 Gen_Id);
6877 end if;
6878
6879 if not In_Open_Scopes (Inst_Par)
6880 and then Nkind (Parent (Gen_Id)) not in
6881 N_Generic_Renaming_Declaration
6882 then
6883 Install_Parent (Inst_Par);
6884 Parent_Installed := True;
6885
6886 elsif In_Open_Scopes (Inst_Par) then
6887
6888 -- If the parent is already installed, install the actuals
6889 -- for its formal packages. This is necessary when the child
6890 -- instance is a child of the parent instance: in this case,
6891 -- the parent is placed on the scope stack but the formal
6892 -- packages are not made visible.
6893
6894 Install_Formal_Packages (Inst_Par);
6895 end if;
6896
6897 else
6898 -- If the generic parent does not contain an entity that
6899 -- corresponds to the selector, the instance doesn't either.
6900 -- Analyzing the node will yield the appropriate error message.
6901 -- If the entity is not a child unit, then it is an inner
6902 -- generic in the parent.
6903
6904 Analyze (Gen_Id);
6905 end if;
6906
6907 else
6908 Analyze (Gen_Id);
6909
6910 if Is_Child_Unit (Entity (Gen_Id))
6911 and then
6912 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
6913 and then not In_Open_Scopes (Inst_Par)
6914 then
6915 Install_Parent (Inst_Par);
6916 Parent_Installed := True;
6917
6918 -- The generic unit may be the renaming of the implicit child
6919 -- present in an instance. In that case the parent instance is
6920 -- obtained from the name of the renamed entity.
6921
6922 elsif Ekind (Entity (Gen_Id)) = E_Generic_Package
6923 and then Present (Renamed_Entity (Entity (Gen_Id)))
6924 and then Is_Child_Unit (Renamed_Entity (Entity (Gen_Id)))
6925 then
6926 declare
6927 Renamed_Package : constant Node_Id :=
6928 Name (Parent (Entity (Gen_Id)));
6929 begin
6930 if Nkind (Renamed_Package) = N_Expanded_Name then
6931 Inst_Par := Entity (Prefix (Renamed_Package));
6932 Install_Parent (Inst_Par);
6933 Parent_Installed := True;
6934 end if;
6935 end;
6936 end if;
6937 end if;
6938
6939 elsif Nkind (Gen_Id) = N_Expanded_Name then
6940
6941 -- Entity already present, analyze prefix, whose meaning may be an
6942 -- instance in the current context. If it is an instance of a
6943 -- relative within another, the proper parent may still have to be
6944 -- installed, if they are not of the same generation.
6945
6946 Analyze (Prefix (Gen_Id));
6947
6948 -- Prevent cascaded errors
6949
6950 if Etype (Prefix (Gen_Id)) = Any_Type then
6951 return;
6952 end if;
6953
6954 -- In the unlikely case that a local declaration hides the name of
6955 -- the parent package, locate it on the homonym chain. If the context
6956 -- is an instance of the parent, the renaming entity is flagged as
6957 -- such.
6958
6959 Inst_Par := Entity (Prefix (Gen_Id));
6960 while Present (Inst_Par)
6961 and then not Is_Package_Or_Generic_Package (Inst_Par)
6962 loop
6963 Inst_Par := Homonym (Inst_Par);
6964 end loop;
6965
6966 pragma Assert (Present (Inst_Par));
6967 Set_Entity (Prefix (Gen_Id), Inst_Par);
6968
6969 if In_Enclosing_Instance then
6970 null;
6971
6972 elsif Present (Entity (Gen_Id))
6973 and then Is_Child_Unit (Entity (Gen_Id))
6974 and then not In_Open_Scopes (Inst_Par)
6975 then
6976 Install_Parent (Inst_Par);
6977 Parent_Installed := True;
6978 end if;
6979
6980 elsif In_Enclosing_Instance then
6981
6982 -- The child unit is found in some enclosing scope
6983
6984 null;
6985
6986 else
6987 Analyze (Gen_Id);
6988
6989 -- If this is the renaming of the implicit child in a parent
6990 -- instance, recover the parent name and install it.
6991
6992 if Is_Entity_Name (Gen_Id) then
6993 E := Entity (Gen_Id);
6994
6995 if Is_Generic_Unit (E)
6996 and then Nkind (Parent (E)) in N_Generic_Renaming_Declaration
6997 and then Is_Child_Unit (Renamed_Object (E))
6998 and then Is_Generic_Unit (Scope (Renamed_Object (E)))
6999 and then Nkind (Name (Parent (E))) = N_Expanded_Name
7000 then
7001 Rewrite (Gen_Id, New_Copy_Tree (Name (Parent (E))));
7002 Inst_Par := Entity (Prefix (Gen_Id));
7003
7004 if not In_Open_Scopes (Inst_Par) then
7005 Install_Parent (Inst_Par);
7006 Parent_Installed := True;
7007 end if;
7008
7009 -- If it is a child unit of a non-generic parent, it may be
7010 -- use-visible and given by a direct name. Install parent as
7011 -- for other cases.
7012
7013 elsif Is_Generic_Unit (E)
7014 and then Is_Child_Unit (E)
7015 and then
7016 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
7017 and then not Is_Generic_Unit (Scope (E))
7018 then
7019 if not In_Open_Scopes (Scope (E)) then
7020 Install_Parent (Scope (E));
7021 Parent_Installed := True;
7022 end if;
7023 end if;
7024 end if;
7025 end if;
7026 end Check_Generic_Child_Unit;
7027
7028 -----------------------------
7029 -- Check_Hidden_Child_Unit --
7030 -----------------------------
7031
7032 procedure Check_Hidden_Child_Unit
7033 (N : Node_Id;
7034 Gen_Unit : Entity_Id;
7035 Act_Decl_Id : Entity_Id)
7036 is
7037 Gen_Id : constant Node_Id := Name (N);
7038
7039 begin
7040 if Is_Child_Unit (Gen_Unit)
7041 and then Is_Child_Unit (Act_Decl_Id)
7042 and then Nkind (Gen_Id) = N_Expanded_Name
7043 and then Entity (Prefix (Gen_Id)) = Scope (Act_Decl_Id)
7044 and then Chars (Gen_Unit) = Chars (Act_Decl_Id)
7045 then
7046 Error_Msg_Node_2 := Scope (Act_Decl_Id);
7047 Error_Msg_NE
7048 ("generic unit & is implicitly declared in &",
7049 Defining_Unit_Name (N), Gen_Unit);
7050 Error_Msg_N ("\instance must have different name",
7051 Defining_Unit_Name (N));
7052 end if;
7053 end Check_Hidden_Child_Unit;
7054
7055 ------------------------
7056 -- Check_Private_View --
7057 ------------------------
7058
7059 procedure Check_Private_View (N : Node_Id) is
7060 T : constant Entity_Id := Etype (N);
7061 BT : Entity_Id;
7062
7063 begin
7064 -- Exchange views if the type was not private in the generic but is
7065 -- private at the point of instantiation. Do not exchange views if
7066 -- the scope of the type is in scope. This can happen if both generic
7067 -- and instance are sibling units, or if type is defined in a parent.
7068 -- In this case the visibility of the type will be correct for all
7069 -- semantic checks.
7070
7071 if Present (T) then
7072 BT := Base_Type (T);
7073
7074 if Is_Private_Type (T)
7075 and then not Has_Private_View (N)
7076 and then Present (Full_View (T))
7077 and then not In_Open_Scopes (Scope (T))
7078 then
7079 -- In the generic, the full type was visible. Save the private
7080 -- entity, for subsequent exchange.
7081
7082 Switch_View (T);
7083
7084 elsif Has_Private_View (N)
7085 and then not Is_Private_Type (T)
7086 and then not Has_Been_Exchanged (T)
7087 and then Etype (Get_Associated_Node (N)) /= T
7088 then
7089 -- Only the private declaration was visible in the generic. If
7090 -- the type appears in a subtype declaration, the subtype in the
7091 -- instance must have a view compatible with that of its parent,
7092 -- which must be exchanged (see corresponding code in Restore_
7093 -- Private_Views). Otherwise, if the type is defined in a parent
7094 -- unit, leave full visibility within instance, which is safe.
7095
7096 if In_Open_Scopes (Scope (Base_Type (T)))
7097 and then not Is_Private_Type (Base_Type (T))
7098 and then Comes_From_Source (Base_Type (T))
7099 then
7100 null;
7101
7102 elsif Nkind (Parent (N)) = N_Subtype_Declaration
7103 or else not In_Private_Part (Scope (Base_Type (T)))
7104 then
7105 Prepend_Elmt (T, Exchanged_Views);
7106 Exchange_Declarations (Etype (Get_Associated_Node (N)));
7107 end if;
7108
7109 -- For composite types with inconsistent representation exchange
7110 -- component types accordingly.
7111
7112 elsif Is_Access_Type (T)
7113 and then Is_Private_Type (Designated_Type (T))
7114 and then not Has_Private_View (N)
7115 and then Present (Full_View (Designated_Type (T)))
7116 then
7117 Switch_View (Designated_Type (T));
7118
7119 elsif Is_Array_Type (T) then
7120 if Is_Private_Type (Component_Type (T))
7121 and then not Has_Private_View (N)
7122 and then Present (Full_View (Component_Type (T)))
7123 then
7124 Switch_View (Component_Type (T));
7125 end if;
7126
7127 -- The normal exchange mechanism relies on the setting of a
7128 -- flag on the reference in the generic. However, an additional
7129 -- mechanism is needed for types that are not explicitly
7130 -- mentioned in the generic, but may be needed in expanded code
7131 -- in the instance. This includes component types of arrays and
7132 -- designated types of access types. This processing must also
7133 -- include the index types of arrays which we take care of here.
7134
7135 declare
7136 Indx : Node_Id;
7137 Typ : Entity_Id;
7138
7139 begin
7140 Indx := First_Index (T);
7141 while Present (Indx) loop
7142 Typ := Base_Type (Etype (Indx));
7143
7144 if Is_Private_Type (Typ)
7145 and then Present (Full_View (Typ))
7146 then
7147 Switch_View (Typ);
7148 end if;
7149
7150 Next_Index (Indx);
7151 end loop;
7152 end;
7153
7154 elsif Is_Private_Type (T)
7155 and then Present (Full_View (T))
7156 and then Is_Array_Type (Full_View (T))
7157 and then Is_Private_Type (Component_Type (Full_View (T)))
7158 then
7159 Switch_View (T);
7160
7161 -- Finally, a non-private subtype may have a private base type, which
7162 -- must be exchanged for consistency. This can happen when a package
7163 -- body is instantiated, when the scope stack is empty but in fact
7164 -- the subtype and the base type are declared in an enclosing scope.
7165
7166 -- Note that in this case we introduce an inconsistency in the view
7167 -- set, because we switch the base type BT, but there could be some
7168 -- private dependent subtypes of BT which remain unswitched. Such
7169 -- subtypes might need to be switched at a later point (see specific
7170 -- provision for that case in Switch_View).
7171
7172 elsif not Is_Private_Type (T)
7173 and then not Has_Private_View (N)
7174 and then Is_Private_Type (BT)
7175 and then Present (Full_View (BT))
7176 and then not Is_Generic_Type (BT)
7177 and then not In_Open_Scopes (BT)
7178 then
7179 Prepend_Elmt (Full_View (BT), Exchanged_Views);
7180 Exchange_Declarations (BT);
7181 end if;
7182 end if;
7183 end Check_Private_View;
7184
7185 -----------------------------
7186 -- Check_Hidden_Primitives --
7187 -----------------------------
7188
7189 function Check_Hidden_Primitives (Assoc_List : List_Id) return Elist_Id is
7190 Actual : Node_Id;
7191 Gen_T : Entity_Id;
7192 Result : Elist_Id := No_Elist;
7193
7194 begin
7195 if No (Assoc_List) then
7196 return No_Elist;
7197 end if;
7198
7199 -- Traverse the list of associations between formals and actuals
7200 -- searching for renamings of tagged types
7201
7202 Actual := First (Assoc_List);
7203 while Present (Actual) loop
7204 if Nkind (Actual) = N_Subtype_Declaration then
7205 Gen_T := Generic_Parent_Type (Actual);
7206
7207 if Present (Gen_T) and then Is_Tagged_Type (Gen_T) then
7208
7209 -- Traverse the list of primitives of the actual types
7210 -- searching for hidden primitives that are visible in the
7211 -- corresponding generic formal; leave them visible and
7212 -- append them to Result to restore their decoration later.
7213
7214 Install_Hidden_Primitives
7215 (Prims_List => Result,
7216 Gen_T => Gen_T,
7217 Act_T => Entity (Subtype_Indication (Actual)));
7218 end if;
7219 end if;
7220
7221 Next (Actual);
7222 end loop;
7223
7224 return Result;
7225 end Check_Hidden_Primitives;
7226
7227 --------------------------
7228 -- Contains_Instance_Of --
7229 --------------------------
7230
7231 function Contains_Instance_Of
7232 (Inner : Entity_Id;
7233 Outer : Entity_Id;
7234 N : Node_Id) return Boolean
7235 is
7236 Elmt : Elmt_Id;
7237 Scop : Entity_Id;
7238
7239 begin
7240 Scop := Outer;
7241
7242 -- Verify that there are no circular instantiations. We check whether
7243 -- the unit contains an instance of the current scope or some enclosing
7244 -- scope (in case one of the instances appears in a subunit). Longer
7245 -- circularities involving subunits might seem too pathological to
7246 -- consider, but they were not too pathological for the authors of
7247 -- DEC bc30vsq, so we loop over all enclosing scopes, and mark all
7248 -- enclosing generic scopes as containing an instance.
7249
7250 loop
7251 -- Within a generic subprogram body, the scope is not generic, to
7252 -- allow for recursive subprograms. Use the declaration to determine
7253 -- whether this is a generic unit.
7254
7255 if Ekind (Scop) = E_Generic_Package
7256 or else (Is_Subprogram (Scop)
7257 and then Nkind (Unit_Declaration_Node (Scop)) =
7258 N_Generic_Subprogram_Declaration)
7259 then
7260 Elmt := First_Elmt (Inner_Instances (Inner));
7261
7262 while Present (Elmt) loop
7263 if Node (Elmt) = Scop then
7264 Error_Msg_Node_2 := Inner;
7265 Error_Msg_NE
7266 ("circular Instantiation: & instantiated within &!",
7267 N, Scop);
7268 return True;
7269
7270 elsif Node (Elmt) = Inner then
7271 return True;
7272
7273 elsif Contains_Instance_Of (Node (Elmt), Scop, N) then
7274 Error_Msg_Node_2 := Inner;
7275 Error_Msg_NE
7276 ("circular Instantiation: & instantiated within &!",
7277 N, Node (Elmt));
7278 return True;
7279 end if;
7280
7281 Next_Elmt (Elmt);
7282 end loop;
7283
7284 -- Indicate that Inner is being instantiated within Scop
7285
7286 Append_Elmt (Inner, Inner_Instances (Scop));
7287 end if;
7288
7289 if Scop = Standard_Standard then
7290 exit;
7291 else
7292 Scop := Scope (Scop);
7293 end if;
7294 end loop;
7295
7296 return False;
7297 end Contains_Instance_Of;
7298
7299 -----------------------
7300 -- Copy_Generic_Node --
7301 -----------------------
7302
7303 function Copy_Generic_Node
7304 (N : Node_Id;
7305 Parent_Id : Node_Id;
7306 Instantiating : Boolean) return Node_Id
7307 is
7308 Ent : Entity_Id;
7309 New_N : Node_Id;
7310
7311 function Copy_Generic_Descendant (D : Union_Id) return Union_Id;
7312 -- Check the given value of one of the Fields referenced by the current
7313 -- node to determine whether to copy it recursively. The field may hold
7314 -- a Node_Id, a List_Id, or an Elist_Id, or a plain value (Sloc, Uint,
7315 -- Char) in which case it need not be copied.
7316
7317 procedure Copy_Descendants;
7318 -- Common utility for various nodes
7319
7320 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id;
7321 -- Make copy of element list
7322
7323 function Copy_Generic_List
7324 (L : List_Id;
7325 Parent_Id : Node_Id) return List_Id;
7326 -- Apply Copy_Node recursively to the members of a node list
7327
7328 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean;
7329 -- True if an identifier is part of the defining program unit name of
7330 -- a child unit. The entity of such an identifier must be kept (for
7331 -- ASIS use) even though as the name of an enclosing generic it would
7332 -- otherwise not be preserved in the generic tree.
7333
7334 ----------------------
7335 -- Copy_Descendants --
7336 ----------------------
7337
7338 procedure Copy_Descendants is
7339 use Atree.Unchecked_Access;
7340 -- This code section is part of the implementation of an untyped
7341 -- tree traversal, so it needs direct access to node fields.
7342
7343 begin
7344 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
7345 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
7346 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
7347 Set_Field4 (New_N, Copy_Generic_Descendant (Field4 (N)));
7348 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
7349 end Copy_Descendants;
7350
7351 -----------------------------
7352 -- Copy_Generic_Descendant --
7353 -----------------------------
7354
7355 function Copy_Generic_Descendant (D : Union_Id) return Union_Id is
7356 begin
7357 if D = Union_Id (Empty) then
7358 return D;
7359
7360 elsif D in Node_Range then
7361 return Union_Id
7362 (Copy_Generic_Node (Node_Id (D), New_N, Instantiating));
7363
7364 elsif D in List_Range then
7365 return Union_Id (Copy_Generic_List (List_Id (D), New_N));
7366
7367 elsif D in Elist_Range then
7368 return Union_Id (Copy_Generic_Elist (Elist_Id (D)));
7369
7370 -- Nothing else is copyable (e.g. Uint values), return as is
7371
7372 else
7373 return D;
7374 end if;
7375 end Copy_Generic_Descendant;
7376
7377 ------------------------
7378 -- Copy_Generic_Elist --
7379 ------------------------
7380
7381 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id is
7382 M : Elmt_Id;
7383 L : Elist_Id;
7384
7385 begin
7386 if Present (E) then
7387 L := New_Elmt_List;
7388 M := First_Elmt (E);
7389 while Present (M) loop
7390 Append_Elmt
7391 (Copy_Generic_Node (Node (M), Empty, Instantiating), L);
7392 Next_Elmt (M);
7393 end loop;
7394
7395 return L;
7396
7397 else
7398 return No_Elist;
7399 end if;
7400 end Copy_Generic_Elist;
7401
7402 -----------------------
7403 -- Copy_Generic_List --
7404 -----------------------
7405
7406 function Copy_Generic_List
7407 (L : List_Id;
7408 Parent_Id : Node_Id) return List_Id
7409 is
7410 N : Node_Id;
7411 New_L : List_Id;
7412
7413 begin
7414 if Present (L) then
7415 New_L := New_List;
7416 Set_Parent (New_L, Parent_Id);
7417
7418 N := First (L);
7419 while Present (N) loop
7420 Append (Copy_Generic_Node (N, Empty, Instantiating), New_L);
7421 Next (N);
7422 end loop;
7423
7424 return New_L;
7425
7426 else
7427 return No_List;
7428 end if;
7429 end Copy_Generic_List;
7430
7431 ---------------------------
7432 -- In_Defining_Unit_Name --
7433 ---------------------------
7434
7435 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean is
7436 begin
7437 return
7438 Present (Parent (Nam))
7439 and then (Nkind (Parent (Nam)) = N_Defining_Program_Unit_Name
7440 or else
7441 (Nkind (Parent (Nam)) = N_Expanded_Name
7442 and then In_Defining_Unit_Name (Parent (Nam))));
7443 end In_Defining_Unit_Name;
7444
7445 -- Start of processing for Copy_Generic_Node
7446
7447 begin
7448 if N = Empty then
7449 return N;
7450 end if;
7451
7452 New_N := New_Copy (N);
7453
7454 -- Copy aspects if present
7455
7456 if Has_Aspects (N) then
7457 Set_Has_Aspects (New_N, False);
7458 Set_Aspect_Specifications
7459 (New_N, Copy_Generic_List (Aspect_Specifications (N), Parent_Id));
7460 end if;
7461
7462 -- If we are instantiating, we want to adjust the sloc based on the
7463 -- current S_Adjustment. However, if this is the root node of a subunit,
7464 -- we need to defer that adjustment to below (see "elsif Instantiating
7465 -- and Was_Stub"), so it comes after Create_Instantiation_Source has
7466 -- computed the adjustment.
7467
7468 if Instantiating
7469 and then not (Nkind (N) in N_Proper_Body
7470 and then Was_Originally_Stub (N))
7471 then
7472 Adjust_Instantiation_Sloc (New_N, S_Adjustment);
7473 end if;
7474
7475 if not Is_List_Member (N) then
7476 Set_Parent (New_N, Parent_Id);
7477 end if;
7478
7479 -- Special casing for identifiers and other entity names and operators
7480
7481 if Nkind_In (New_N, N_Character_Literal,
7482 N_Expanded_Name,
7483 N_Identifier,
7484 N_Operator_Symbol)
7485 or else Nkind (New_N) in N_Op
7486 then
7487 if not Instantiating then
7488
7489 -- Link both nodes in order to assign subsequently the entity of
7490 -- the copy to the original node, in case this is a global
7491 -- reference.
7492
7493 Set_Associated_Node (N, New_N);
7494
7495 -- If we are within an instantiation, this is a nested generic
7496 -- that has already been analyzed at the point of definition.
7497 -- We must preserve references that were global to the enclosing
7498 -- parent at that point. Other occurrences, whether global or
7499 -- local to the current generic, must be resolved anew, so we
7500 -- reset the entity in the generic copy. A global reference has a
7501 -- smaller depth than the parent, or else the same depth in case
7502 -- both are distinct compilation units.
7503
7504 -- A child unit is implicitly declared within the enclosing parent
7505 -- but is in fact global to it, and must be preserved.
7506
7507 -- It is also possible for Current_Instantiated_Parent to be
7508 -- defined, and for this not to be a nested generic, namely if
7509 -- the unit is loaded through Rtsfind. In that case, the entity of
7510 -- New_N is only a link to the associated node, and not a defining
7511 -- occurrence.
7512
7513 -- The entities for parent units in the defining_program_unit of a
7514 -- generic child unit are established when the context of the unit
7515 -- is first analyzed, before the generic copy is made. They are
7516 -- preserved in the copy for use in ASIS queries.
7517
7518 Ent := Entity (New_N);
7519
7520 if No (Current_Instantiated_Parent.Gen_Id) then
7521 if No (Ent)
7522 or else Nkind (Ent) /= N_Defining_Identifier
7523 or else not In_Defining_Unit_Name (N)
7524 then
7525 Set_Associated_Node (New_N, Empty);
7526 end if;
7527
7528 elsif No (Ent)
7529 or else
7530 not Nkind_In (Ent, N_Defining_Identifier,
7531 N_Defining_Character_Literal,
7532 N_Defining_Operator_Symbol)
7533 or else No (Scope (Ent))
7534 or else
7535 (Scope (Ent) = Current_Instantiated_Parent.Gen_Id
7536 and then not Is_Child_Unit (Ent))
7537 or else
7538 (Scope_Depth (Scope (Ent)) >
7539 Scope_Depth (Current_Instantiated_Parent.Gen_Id)
7540 and then
7541 Get_Source_Unit (Ent) =
7542 Get_Source_Unit (Current_Instantiated_Parent.Gen_Id))
7543 then
7544 Set_Associated_Node (New_N, Empty);
7545 end if;
7546
7547 -- Case of instantiating identifier or some other name or operator
7548
7549 else
7550 -- If the associated node is still defined, the entity in it
7551 -- is global, and must be copied to the instance. If this copy
7552 -- is being made for a body to inline, it is applied to an
7553 -- instantiated tree, and the entity is already present and
7554 -- must be also preserved.
7555
7556 declare
7557 Assoc : constant Node_Id := Get_Associated_Node (N);
7558
7559 begin
7560 if Present (Assoc) then
7561 if Nkind (Assoc) = Nkind (N) then
7562 Set_Entity (New_N, Entity (Assoc));
7563 Check_Private_View (N);
7564
7565 -- The node is a reference to a global type and acts as the
7566 -- subtype mark of a qualified expression created in order
7567 -- to aid resolution of accidental overloading in instances.
7568 -- Since N is a reference to a type, the Associated_Node of
7569 -- N denotes an entity rather than another identifier. See
7570 -- Qualify_Universal_Operands for details.
7571
7572 elsif Nkind (N) = N_Identifier
7573 and then Nkind (Parent (N)) = N_Qualified_Expression
7574 and then Subtype_Mark (Parent (N)) = N
7575 and then Is_Qualified_Universal_Literal (Parent (N))
7576 then
7577 Set_Entity (New_N, Assoc);
7578
7579 -- The name in the call may be a selected component if the
7580 -- call has not been analyzed yet, as may be the case for
7581 -- pre/post conditions in a generic unit.
7582
7583 elsif Nkind (Assoc) = N_Function_Call
7584 and then Is_Entity_Name (Name (Assoc))
7585 then
7586 Set_Entity (New_N, Entity (Name (Assoc)));
7587
7588 elsif Nkind_In (Assoc, N_Defining_Identifier,
7589 N_Defining_Character_Literal,
7590 N_Defining_Operator_Symbol)
7591 and then Expander_Active
7592 then
7593 -- Inlining case: we are copying a tree that contains
7594 -- global entities, which are preserved in the copy to be
7595 -- used for subsequent inlining.
7596
7597 null;
7598
7599 else
7600 Set_Entity (New_N, Empty);
7601 end if;
7602 end if;
7603 end;
7604 end if;
7605
7606 -- For expanded name, we must copy the Prefix and Selector_Name
7607
7608 if Nkind (N) = N_Expanded_Name then
7609 Set_Prefix
7610 (New_N, Copy_Generic_Node (Prefix (N), New_N, Instantiating));
7611
7612 Set_Selector_Name (New_N,
7613 Copy_Generic_Node (Selector_Name (N), New_N, Instantiating));
7614
7615 -- For operators, copy the operands
7616
7617 elsif Nkind (N) in N_Op then
7618 if Nkind (N) in N_Binary_Op then
7619 Set_Left_Opnd (New_N,
7620 Copy_Generic_Node (Left_Opnd (N), New_N, Instantiating));
7621 end if;
7622
7623 Set_Right_Opnd (New_N,
7624 Copy_Generic_Node (Right_Opnd (N), New_N, Instantiating));
7625 end if;
7626
7627 -- Establish a link between an entity from the generic template and the
7628 -- corresponding entity in the generic copy to be analyzed.
7629
7630 elsif Nkind (N) in N_Entity then
7631 if not Instantiating then
7632 Set_Associated_Entity (N, New_N);
7633 end if;
7634
7635 -- Clear any existing link the copy may inherit from the replicated
7636 -- generic template entity.
7637
7638 Set_Associated_Entity (New_N, Empty);
7639
7640 -- Special casing for stubs
7641
7642 elsif Nkind (N) in N_Body_Stub then
7643
7644 -- In any case, we must copy the specification or defining
7645 -- identifier as appropriate.
7646
7647 if Nkind (N) = N_Subprogram_Body_Stub then
7648 Set_Specification (New_N,
7649 Copy_Generic_Node (Specification (N), New_N, Instantiating));
7650
7651 else
7652 Set_Defining_Identifier (New_N,
7653 Copy_Generic_Node
7654 (Defining_Identifier (N), New_N, Instantiating));
7655 end if;
7656
7657 -- If we are not instantiating, then this is where we load and
7658 -- analyze subunits, i.e. at the point where the stub occurs. A
7659 -- more permissive system might defer this analysis to the point
7660 -- of instantiation, but this seems too complicated for now.
7661
7662 if not Instantiating then
7663 declare
7664 Subunit_Name : constant Unit_Name_Type := Get_Unit_Name (N);
7665 Subunit : Node_Id;
7666 Unum : Unit_Number_Type;
7667 New_Body : Node_Id;
7668
7669 begin
7670 -- Make sure that, if it is a subunit of the main unit that is
7671 -- preprocessed and if -gnateG is specified, the preprocessed
7672 -- file will be written.
7673
7674 Lib.Analysing_Subunit_Of_Main :=
7675 Lib.In_Extended_Main_Source_Unit (N);
7676 Unum :=
7677 Load_Unit
7678 (Load_Name => Subunit_Name,
7679 Required => False,
7680 Subunit => True,
7681 Error_Node => N);
7682 Lib.Analysing_Subunit_Of_Main := False;
7683
7684 -- If the proper body is not found, a warning message will be
7685 -- emitted when analyzing the stub, or later at the point of
7686 -- instantiation. Here we just leave the stub as is.
7687
7688 if Unum = No_Unit then
7689 Subunits_Missing := True;
7690 goto Subunit_Not_Found;
7691 end if;
7692
7693 Subunit := Cunit (Unum);
7694
7695 if Nkind (Unit (Subunit)) /= N_Subunit then
7696 Error_Msg_N
7697 ("found child unit instead of expected SEPARATE subunit",
7698 Subunit);
7699 Error_Msg_Sloc := Sloc (N);
7700 Error_Msg_N ("\to complete stub #", Subunit);
7701 goto Subunit_Not_Found;
7702 end if;
7703
7704 -- We must create a generic copy of the subunit, in order to
7705 -- perform semantic analysis on it, and we must replace the
7706 -- stub in the original generic unit with the subunit, in order
7707 -- to preserve non-local references within.
7708
7709 -- Only the proper body needs to be copied. Library_Unit and
7710 -- context clause are simply inherited by the generic copy.
7711 -- Note that the copy (which may be recursive if there are
7712 -- nested subunits) must be done first, before attaching it to
7713 -- the enclosing generic.
7714
7715 New_Body :=
7716 Copy_Generic_Node
7717 (Proper_Body (Unit (Subunit)),
7718 Empty, Instantiating => False);
7719
7720 -- Now place the original proper body in the original generic
7721 -- unit. This is a body, not a compilation unit.
7722
7723 Rewrite (N, Proper_Body (Unit (Subunit)));
7724 Set_Is_Compilation_Unit (Defining_Entity (N), False);
7725 Set_Was_Originally_Stub (N);
7726
7727 -- Finally replace the body of the subunit with its copy, and
7728 -- make this new subunit into the library unit of the generic
7729 -- copy, which does not have stubs any longer.
7730
7731 Set_Proper_Body (Unit (Subunit), New_Body);
7732 Set_Library_Unit (New_N, Subunit);
7733 Inherit_Context (Unit (Subunit), N);
7734 end;
7735
7736 -- If we are instantiating, this must be an error case, since
7737 -- otherwise we would have replaced the stub node by the proper body
7738 -- that corresponds. So just ignore it in the copy (i.e. we have
7739 -- copied it, and that is good enough).
7740
7741 else
7742 null;
7743 end if;
7744
7745 <<Subunit_Not_Found>> null;
7746
7747 -- If the node is a compilation unit, it is the subunit of a stub, which
7748 -- has been loaded already (see code below). In this case, the library
7749 -- unit field of N points to the parent unit (which is a compilation
7750 -- unit) and need not (and cannot) be copied.
7751
7752 -- When the proper body of the stub is analyzed, the library_unit link
7753 -- is used to establish the proper context (see sem_ch10).
7754
7755 -- The other fields of a compilation unit are copied as usual
7756
7757 elsif Nkind (N) = N_Compilation_Unit then
7758
7759 -- This code can only be executed when not instantiating, because in
7760 -- the copy made for an instantiation, the compilation unit node has
7761 -- disappeared at the point that a stub is replaced by its proper
7762 -- body.
7763
7764 pragma Assert (not Instantiating);
7765
7766 Set_Context_Items (New_N,
7767 Copy_Generic_List (Context_Items (N), New_N));
7768
7769 Set_Unit (New_N,
7770 Copy_Generic_Node (Unit (N), New_N, Instantiating => False));
7771
7772 Set_First_Inlined_Subprogram (New_N,
7773 Copy_Generic_Node
7774 (First_Inlined_Subprogram (N), New_N, Instantiating => False));
7775
7776 Set_Aux_Decls_Node
7777 (New_N,
7778 Copy_Generic_Node
7779 (Aux_Decls_Node (N), New_N, Instantiating => False));
7780
7781 -- For an assignment node, the assignment is known to be semantically
7782 -- legal if we are instantiating the template. This avoids incorrect
7783 -- diagnostics in generated code.
7784
7785 elsif Nkind (N) = N_Assignment_Statement then
7786
7787 -- Copy name and expression fields in usual manner
7788
7789 Set_Name (New_N,
7790 Copy_Generic_Node (Name (N), New_N, Instantiating));
7791
7792 Set_Expression (New_N,
7793 Copy_Generic_Node (Expression (N), New_N, Instantiating));
7794
7795 if Instantiating then
7796 Set_Assignment_OK (Name (New_N), True);
7797 end if;
7798
7799 elsif Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
7800 if not Instantiating then
7801 Set_Associated_Node (N, New_N);
7802
7803 else
7804 if Present (Get_Associated_Node (N))
7805 and then Nkind (Get_Associated_Node (N)) = Nkind (N)
7806 then
7807 -- In the generic the aggregate has some composite type. If at
7808 -- the point of instantiation the type has a private view,
7809 -- install the full view (and that of its ancestors, if any).
7810
7811 declare
7812 T : Entity_Id := (Etype (Get_Associated_Node (New_N)));
7813 Rt : Entity_Id;
7814
7815 begin
7816 if Present (T) and then Is_Private_Type (T) then
7817 Switch_View (T);
7818 end if;
7819
7820 if Present (T)
7821 and then Is_Tagged_Type (T)
7822 and then Is_Derived_Type (T)
7823 then
7824 Rt := Root_Type (T);
7825
7826 loop
7827 T := Etype (T);
7828
7829 if Is_Private_Type (T) then
7830 Switch_View (T);
7831 end if;
7832
7833 exit when T = Rt;
7834 end loop;
7835 end if;
7836 end;
7837 end if;
7838 end if;
7839
7840 -- Do not copy the associated node, which points to the generic copy
7841 -- of the aggregate.
7842
7843 declare
7844 use Atree.Unchecked_Access;
7845 -- This code section is part of the implementation of an untyped
7846 -- tree traversal, so it needs direct access to node fields.
7847
7848 begin
7849 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
7850 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
7851 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
7852 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
7853 end;
7854
7855 -- Allocators do not have an identifier denoting the access type, so we
7856 -- must locate it through the expression to check whether the views are
7857 -- consistent.
7858
7859 elsif Nkind (N) = N_Allocator
7860 and then Nkind (Expression (N)) = N_Qualified_Expression
7861 and then Is_Entity_Name (Subtype_Mark (Expression (N)))
7862 and then Instantiating
7863 then
7864 declare
7865 T : constant Node_Id :=
7866 Get_Associated_Node (Subtype_Mark (Expression (N)));
7867 Acc_T : Entity_Id;
7868
7869 begin
7870 if Present (T) then
7871
7872 -- Retrieve the allocator node in the generic copy
7873
7874 Acc_T := Etype (Parent (Parent (T)));
7875
7876 if Present (Acc_T) and then Is_Private_Type (Acc_T) then
7877 Switch_View (Acc_T);
7878 end if;
7879 end if;
7880
7881 Copy_Descendants;
7882 end;
7883
7884 -- For a proper body, we must catch the case of a proper body that
7885 -- replaces a stub. This represents the point at which a separate
7886 -- compilation unit, and hence template file, may be referenced, so we
7887 -- must make a new source instantiation entry for the template of the
7888 -- subunit, and ensure that all nodes in the subunit are adjusted using
7889 -- this new source instantiation entry.
7890
7891 elsif Nkind (N) in N_Proper_Body then
7892 declare
7893 Save_Adjustment : constant Sloc_Adjustment := S_Adjustment;
7894 begin
7895 if Instantiating and then Was_Originally_Stub (N) then
7896 Create_Instantiation_Source
7897 (Instantiation_Node,
7898 Defining_Entity (N),
7899 S_Adjustment);
7900
7901 Adjust_Instantiation_Sloc (New_N, S_Adjustment);
7902 end if;
7903
7904 -- Now copy the fields of the proper body, using the new
7905 -- adjustment factor if one was needed as per test above.
7906
7907 Copy_Descendants;
7908
7909 -- Restore the original adjustment factor
7910
7911 S_Adjustment := Save_Adjustment;
7912 end;
7913
7914 elsif Nkind (N) = N_Pragma and then Instantiating then
7915
7916 -- Do not copy Comment or Ident pragmas their content is relevant to
7917 -- the generic unit, not to the instantiating unit.
7918
7919 if Nam_In (Pragma_Name_Unmapped (N), Name_Comment, Name_Ident) then
7920 New_N := Make_Null_Statement (Sloc (N));
7921
7922 -- Do not copy pragmas generated from aspects because the pragmas do
7923 -- not carry any semantic information, plus they will be regenerated
7924 -- in the instance.
7925
7926 -- However, generating C we need to copy them since postconditions
7927 -- are inlined by the front end, and the front-end inlining machinery
7928 -- relies on this routine to perform inlining.
7929
7930 elsif From_Aspect_Specification (N)
7931 and then not Modify_Tree_For_C
7932 then
7933 New_N := Make_Null_Statement (Sloc (N));
7934
7935 else
7936 Copy_Descendants;
7937 end if;
7938
7939 elsif Nkind_In (N, N_Integer_Literal, N_Real_Literal) then
7940
7941 -- No descendant fields need traversing
7942
7943 null;
7944
7945 elsif Nkind (N) = N_String_Literal
7946 and then Present (Etype (N))
7947 and then Instantiating
7948 then
7949 -- If the string is declared in an outer scope, the string_literal
7950 -- subtype created for it may have the wrong scope. Force reanalysis
7951 -- of the constant to generate a new itype in the proper context.
7952
7953 Set_Etype (New_N, Empty);
7954 Set_Analyzed (New_N, False);
7955
7956 -- For the remaining nodes, copy their descendants recursively
7957
7958 else
7959 Copy_Descendants;
7960
7961 if Instantiating and then Nkind (N) = N_Subprogram_Body then
7962 Set_Generic_Parent (Specification (New_N), N);
7963
7964 -- Should preserve Corresponding_Spec??? (12.3(14))
7965 end if;
7966 end if;
7967
7968 -- Propagate dimensions if present, so that they are reflected in the
7969 -- instance.
7970
7971 if Nkind (N) in N_Has_Etype
7972 and then (Nkind (N) in N_Op or else Is_Entity_Name (N))
7973 and then Present (Etype (N))
7974 and then Is_Floating_Point_Type (Etype (N))
7975 and then Has_Dimension_System (Etype (N))
7976 then
7977 Copy_Dimensions (N, New_N);
7978 end if;
7979
7980 return New_N;
7981 end Copy_Generic_Node;
7982
7983 ----------------------------
7984 -- Denotes_Formal_Package --
7985 ----------------------------
7986
7987 function Denotes_Formal_Package
7988 (Pack : Entity_Id;
7989 On_Exit : Boolean := False;
7990 Instance : Entity_Id := Empty) return Boolean
7991 is
7992 Par : Entity_Id;
7993 Scop : constant Entity_Id := Scope (Pack);
7994 E : Entity_Id;
7995
7996 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean;
7997 -- The package in question may be an actual for a previous formal
7998 -- package P of the current instance, so examine its actuals as well.
7999 -- This must be recursive over other formal packages.
8000
8001 ----------------------------------
8002 -- Is_Actual_Of_Previous_Formal --
8003 ----------------------------------
8004
8005 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean is
8006 E1 : Entity_Id;
8007
8008 begin
8009 E1 := First_Entity (P);
8010 while Present (E1) and then E1 /= Instance loop
8011 if Ekind (E1) = E_Package
8012 and then Nkind (Parent (E1)) = N_Package_Renaming_Declaration
8013 then
8014 if Renamed_Object (E1) = Pack then
8015 return True;
8016
8017 elsif E1 = P or else Renamed_Object (E1) = P then
8018 return False;
8019
8020 elsif Is_Actual_Of_Previous_Formal (E1) then
8021 return True;
8022 end if;
8023 end if;
8024
8025 Next_Entity (E1);
8026 end loop;
8027
8028 return False;
8029 end Is_Actual_Of_Previous_Formal;
8030
8031 -- Start of processing for Denotes_Formal_Package
8032
8033 begin
8034 if On_Exit then
8035 Par :=
8036 Instance_Envs.Table
8037 (Instance_Envs.Last).Instantiated_Parent.Act_Id;
8038 else
8039 Par := Current_Instantiated_Parent.Act_Id;
8040 end if;
8041
8042 if Ekind (Scop) = E_Generic_Package
8043 or else Nkind (Unit_Declaration_Node (Scop)) =
8044 N_Generic_Subprogram_Declaration
8045 then
8046 return True;
8047
8048 elsif Nkind (Original_Node (Unit_Declaration_Node (Pack))) =
8049 N_Formal_Package_Declaration
8050 then
8051 return True;
8052
8053 elsif No (Par) then
8054 return False;
8055
8056 else
8057 -- Check whether this package is associated with a formal package of
8058 -- the enclosing instantiation. Iterate over the list of renamings.
8059
8060 E := First_Entity (Par);
8061 while Present (E) loop
8062 if Ekind (E) /= E_Package
8063 or else Nkind (Parent (E)) /= N_Package_Renaming_Declaration
8064 then
8065 null;
8066
8067 elsif Renamed_Object (E) = Par then
8068 return False;
8069
8070 elsif Renamed_Object (E) = Pack then
8071 return True;
8072
8073 elsif Is_Actual_Of_Previous_Formal (E) then
8074 return True;
8075
8076 end if;
8077
8078 Next_Entity (E);
8079 end loop;
8080
8081 return False;
8082 end if;
8083 end Denotes_Formal_Package;
8084
8085 -----------------
8086 -- End_Generic --
8087 -----------------
8088
8089 procedure End_Generic is
8090 begin
8091 -- ??? More things could be factored out in this routine. Should
8092 -- probably be done at a later stage.
8093
8094 Inside_A_Generic := Generic_Flags.Table (Generic_Flags.Last);
8095 Generic_Flags.Decrement_Last;
8096
8097 Expander_Mode_Restore;
8098 end End_Generic;
8099
8100 -------------
8101 -- Earlier --
8102 -------------
8103
8104 function Earlier (N1, N2 : Node_Id) return Boolean is
8105 procedure Find_Depth (P : in out Node_Id; D : in out Integer);
8106 -- Find distance from given node to enclosing compilation unit
8107
8108 ----------------
8109 -- Find_Depth --
8110 ----------------
8111
8112 procedure Find_Depth (P : in out Node_Id; D : in out Integer) is
8113 begin
8114 while Present (P)
8115 and then Nkind (P) /= N_Compilation_Unit
8116 loop
8117 P := True_Parent (P);
8118 D := D + 1;
8119 end loop;
8120 end Find_Depth;
8121
8122 -- Local declarations
8123
8124 D1 : Integer := 0;
8125 D2 : Integer := 0;
8126 P1 : Node_Id := N1;
8127 P2 : Node_Id := N2;
8128 T1 : Source_Ptr;
8129 T2 : Source_Ptr;
8130
8131 -- Start of processing for Earlier
8132
8133 begin
8134 Find_Depth (P1, D1);
8135 Find_Depth (P2, D2);
8136
8137 if P1 /= P2 then
8138 return False;
8139 else
8140 P1 := N1;
8141 P2 := N2;
8142 end if;
8143
8144 while D1 > D2 loop
8145 P1 := True_Parent (P1);
8146 D1 := D1 - 1;
8147 end loop;
8148
8149 while D2 > D1 loop
8150 P2 := True_Parent (P2);
8151 D2 := D2 - 1;
8152 end loop;
8153
8154 -- At this point P1 and P2 are at the same distance from the root.
8155 -- We examine their parents until we find a common declarative list.
8156 -- If we reach the root, N1 and N2 do not descend from the same
8157 -- declarative list (e.g. one is nested in the declarative part and
8158 -- the other is in a block in the statement part) and the earlier
8159 -- one is already frozen.
8160
8161 while not Is_List_Member (P1)
8162 or else not Is_List_Member (P2)
8163 or else List_Containing (P1) /= List_Containing (P2)
8164 loop
8165 P1 := True_Parent (P1);
8166 P2 := True_Parent (P2);
8167
8168 if Nkind (Parent (P1)) = N_Subunit then
8169 P1 := Corresponding_Stub (Parent (P1));
8170 end if;
8171
8172 if Nkind (Parent (P2)) = N_Subunit then
8173 P2 := Corresponding_Stub (Parent (P2));
8174 end if;
8175
8176 if P1 = P2 then
8177 return False;
8178 end if;
8179 end loop;
8180
8181 -- Expanded code usually shares the source location of the original
8182 -- construct it was generated for. This however may not necessarily
8183 -- reflect the true location of the code within the tree.
8184
8185 -- Before comparing the slocs of the two nodes, make sure that we are
8186 -- working with correct source locations. Assume that P1 is to the left
8187 -- of P2. If either one does not come from source, traverse the common
8188 -- list heading towards the other node and locate the first source
8189 -- statement.
8190
8191 -- P1 P2
8192 -- ----+===+===+--------------+===+===+----
8193 -- expanded code expanded code
8194
8195 if not Comes_From_Source (P1) then
8196 while Present (P1) loop
8197
8198 -- Neither P2 nor a source statement were located during the
8199 -- search. If we reach the end of the list, then P1 does not
8200 -- occur earlier than P2.
8201
8202 -- ---->
8203 -- start --- P2 ----- P1 --- end
8204
8205 if No (Next (P1)) then
8206 return False;
8207
8208 -- We encounter P2 while going to the right of the list. This
8209 -- means that P1 does indeed appear earlier.
8210
8211 -- ---->
8212 -- start --- P1 ===== P2 --- end
8213 -- expanded code in between
8214
8215 elsif P1 = P2 then
8216 return True;
8217
8218 -- No need to look any further since we have located a source
8219 -- statement.
8220
8221 elsif Comes_From_Source (P1) then
8222 exit;
8223 end if;
8224
8225 -- Keep going right
8226
8227 Next (P1);
8228 end loop;
8229 end if;
8230
8231 if not Comes_From_Source (P2) then
8232 while Present (P2) loop
8233
8234 -- Neither P1 nor a source statement were located during the
8235 -- search. If we reach the start of the list, then P1 does not
8236 -- occur earlier than P2.
8237
8238 -- <----
8239 -- start --- P2 --- P1 --- end
8240
8241 if No (Prev (P2)) then
8242 return False;
8243
8244 -- We encounter P1 while going to the left of the list. This
8245 -- means that P1 does indeed appear earlier.
8246
8247 -- <----
8248 -- start --- P1 ===== P2 --- end
8249 -- expanded code in between
8250
8251 elsif P2 = P1 then
8252 return True;
8253
8254 -- No need to look any further since we have located a source
8255 -- statement.
8256
8257 elsif Comes_From_Source (P2) then
8258 exit;
8259 end if;
8260
8261 -- Keep going left
8262
8263 Prev (P2);
8264 end loop;
8265 end if;
8266
8267 -- At this point either both nodes came from source or we approximated
8268 -- their source locations through neighboring source statements.
8269
8270 T1 := Top_Level_Location (Sloc (P1));
8271 T2 := Top_Level_Location (Sloc (P2));
8272
8273 -- When two nodes come from the same instance, they have identical top
8274 -- level locations. To determine proper relation within the tree, check
8275 -- their locations within the template.
8276
8277 if T1 = T2 then
8278 return Sloc (P1) < Sloc (P2);
8279
8280 -- The two nodes either come from unrelated instances or do not come
8281 -- from instantiated code at all.
8282
8283 else
8284 return T1 < T2;
8285 end if;
8286 end Earlier;
8287
8288 ----------------------
8289 -- Find_Actual_Type --
8290 ----------------------
8291
8292 function Find_Actual_Type
8293 (Typ : Entity_Id;
8294 Gen_Type : Entity_Id) return Entity_Id
8295 is
8296 Gen_Scope : constant Entity_Id := Scope (Gen_Type);
8297 T : Entity_Id;
8298
8299 begin
8300 -- Special processing only applies to child units
8301
8302 if not Is_Child_Unit (Gen_Scope) then
8303 return Get_Instance_Of (Typ);
8304
8305 -- If designated or component type is itself a formal of the child unit,
8306 -- its instance is available.
8307
8308 elsif Scope (Typ) = Gen_Scope then
8309 return Get_Instance_Of (Typ);
8310
8311 -- If the array or access type is not declared in the parent unit,
8312 -- no special processing needed.
8313
8314 elsif not Is_Generic_Type (Typ)
8315 and then Scope (Gen_Scope) /= Scope (Typ)
8316 then
8317 return Get_Instance_Of (Typ);
8318
8319 -- Otherwise, retrieve designated or component type by visibility
8320
8321 else
8322 T := Current_Entity (Typ);
8323 while Present (T) loop
8324 if In_Open_Scopes (Scope (T)) then
8325 return T;
8326 elsif Is_Generic_Actual_Type (T) then
8327 return T;
8328 end if;
8329
8330 T := Homonym (T);
8331 end loop;
8332
8333 return Typ;
8334 end if;
8335 end Find_Actual_Type;
8336
8337 ----------------------------
8338 -- Freeze_Subprogram_Body --
8339 ----------------------------
8340
8341 procedure Freeze_Subprogram_Body
8342 (Inst_Node : Node_Id;
8343 Gen_Body : Node_Id;
8344 Pack_Id : Entity_Id)
8345 is
8346 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
8347 Par : constant Entity_Id := Scope (Gen_Unit);
8348 E_G_Id : Entity_Id;
8349 Enc_G : Entity_Id;
8350 Enc_I : Node_Id;
8351 F_Node : Node_Id;
8352
8353 function Enclosing_Package_Body (N : Node_Id) return Node_Id;
8354 -- Find innermost package body that encloses the given node, and which
8355 -- is not a compilation unit. Freeze nodes for the instance, or for its
8356 -- enclosing body, may be inserted after the enclosing_body of the
8357 -- generic unit. Used to determine proper placement of freeze node for
8358 -- both package and subprogram instances.
8359
8360 function Package_Freeze_Node (B : Node_Id) return Node_Id;
8361 -- Find entity for given package body, and locate or create a freeze
8362 -- node for it.
8363
8364 ----------------------------
8365 -- Enclosing_Package_Body --
8366 ----------------------------
8367
8368 function Enclosing_Package_Body (N : Node_Id) return Node_Id is
8369 P : Node_Id;
8370
8371 begin
8372 P := Parent (N);
8373 while Present (P)
8374 and then Nkind (Parent (P)) /= N_Compilation_Unit
8375 loop
8376 if Nkind (P) = N_Package_Body then
8377 if Nkind (Parent (P)) = N_Subunit then
8378 return Corresponding_Stub (Parent (P));
8379 else
8380 return P;
8381 end if;
8382 end if;
8383
8384 P := True_Parent (P);
8385 end loop;
8386
8387 return Empty;
8388 end Enclosing_Package_Body;
8389
8390 -------------------------
8391 -- Package_Freeze_Node --
8392 -------------------------
8393
8394 function Package_Freeze_Node (B : Node_Id) return Node_Id is
8395 Id : Entity_Id;
8396
8397 begin
8398 if Nkind (B) = N_Package_Body then
8399 Id := Corresponding_Spec (B);
8400 else pragma Assert (Nkind (B) = N_Package_Body_Stub);
8401 Id := Corresponding_Spec (Proper_Body (Unit (Library_Unit (B))));
8402 end if;
8403
8404 Ensure_Freeze_Node (Id);
8405 return Freeze_Node (Id);
8406 end Package_Freeze_Node;
8407
8408 -- Start of processing for Freeze_Subprogram_Body
8409
8410 begin
8411 -- If the instance and the generic body appear within the same unit, and
8412 -- the instance precedes the generic, the freeze node for the instance
8413 -- must appear after that of the generic. If the generic is nested
8414 -- within another instance I2, then current instance must be frozen
8415 -- after I2. In both cases, the freeze nodes are those of enclosing
8416 -- packages. Otherwise, the freeze node is placed at the end of the
8417 -- current declarative part.
8418
8419 Enc_G := Enclosing_Package_Body (Gen_Body);
8420 Enc_I := Enclosing_Package_Body (Inst_Node);
8421 Ensure_Freeze_Node (Pack_Id);
8422 F_Node := Freeze_Node (Pack_Id);
8423
8424 if Is_Generic_Instance (Par)
8425 and then Present (Freeze_Node (Par))
8426 and then In_Same_Declarative_Part (Freeze_Node (Par), Inst_Node)
8427 then
8428 -- The parent was a premature instantiation. Insert freeze node at
8429 -- the end the current declarative part.
8430
8431 if ABE_Is_Certain (Get_Package_Instantiation_Node (Par)) then
8432 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8433
8434 -- Handle the following case:
8435 --
8436 -- package Parent_Inst is new ...
8437 -- Parent_Inst []
8438 --
8439 -- procedure P ... -- this body freezes Parent_Inst
8440 --
8441 -- package Inst is new ...
8442 --
8443 -- In this particular scenario, the freeze node for Inst must be
8444 -- inserted in the same manner as that of Parent_Inst - before the
8445 -- next source body or at the end of the declarative list (body not
8446 -- available). If body P did not exist and Parent_Inst was frozen
8447 -- after Inst, either by a body following Inst or at the end of the
8448 -- declarative region, the freeze node for Inst must be inserted
8449 -- after that of Parent_Inst. This relation is established by
8450 -- comparing the Slocs of Parent_Inst freeze node and Inst.
8451
8452 elsif List_Containing (Get_Package_Instantiation_Node (Par)) =
8453 List_Containing (Inst_Node)
8454 and then Sloc (Freeze_Node (Par)) < Sloc (Inst_Node)
8455 then
8456 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8457
8458 else
8459 Insert_After (Freeze_Node (Par), F_Node);
8460 end if;
8461
8462 -- The body enclosing the instance should be frozen after the body that
8463 -- includes the generic, because the body of the instance may make
8464 -- references to entities therein. If the two are not in the same
8465 -- declarative part, or if the one enclosing the instance is frozen
8466 -- already, freeze the instance at the end of the current declarative
8467 -- part.
8468
8469 elsif Is_Generic_Instance (Par)
8470 and then Present (Freeze_Node (Par))
8471 and then Present (Enc_I)
8472 then
8473 if In_Same_Declarative_Part (Freeze_Node (Par), Enc_I)
8474 or else
8475 (Nkind (Enc_I) = N_Package_Body
8476 and then
8477 In_Same_Declarative_Part (Freeze_Node (Par), Parent (Enc_I)))
8478 then
8479 -- The enclosing package may contain several instances. Rather
8480 -- than computing the earliest point at which to insert its freeze
8481 -- node, we place it at the end of the declarative part of the
8482 -- parent of the generic.
8483
8484 Insert_Freeze_Node_For_Instance
8485 (Freeze_Node (Par), Package_Freeze_Node (Enc_I));
8486 end if;
8487
8488 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8489
8490 elsif Present (Enc_G)
8491 and then Present (Enc_I)
8492 and then Enc_G /= Enc_I
8493 and then Earlier (Inst_Node, Gen_Body)
8494 then
8495 if Nkind (Enc_G) = N_Package_Body then
8496 E_G_Id :=
8497 Corresponding_Spec (Enc_G);
8498 else pragma Assert (Nkind (Enc_G) = N_Package_Body_Stub);
8499 E_G_Id :=
8500 Corresponding_Spec (Proper_Body (Unit (Library_Unit (Enc_G))));
8501 end if;
8502
8503 -- Freeze package that encloses instance, and place node after the
8504 -- package that encloses generic. If enclosing package is already
8505 -- frozen we have to assume it is at the proper place. This may be a
8506 -- potential ABE that requires dynamic checking. Do not add a freeze
8507 -- node if the package that encloses the generic is inside the body
8508 -- that encloses the instance, because the freeze node would be in
8509 -- the wrong scope. Additional contortions needed if the bodies are
8510 -- within a subunit.
8511
8512 declare
8513 Enclosing_Body : Node_Id;
8514
8515 begin
8516 if Nkind (Enc_I) = N_Package_Body_Stub then
8517 Enclosing_Body := Proper_Body (Unit (Library_Unit (Enc_I)));
8518 else
8519 Enclosing_Body := Enc_I;
8520 end if;
8521
8522 if Parent (List_Containing (Enc_G)) /= Enclosing_Body then
8523 Insert_Freeze_Node_For_Instance
8524 (Enc_G, Package_Freeze_Node (Enc_I));
8525 end if;
8526 end;
8527
8528 -- Freeze enclosing subunit before instance
8529
8530 Ensure_Freeze_Node (E_G_Id);
8531
8532 if not Is_List_Member (Freeze_Node (E_G_Id)) then
8533 Insert_After (Enc_G, Freeze_Node (E_G_Id));
8534 end if;
8535
8536 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8537
8538 else
8539 -- If none of the above, insert freeze node at the end of the current
8540 -- declarative part.
8541
8542 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8543 end if;
8544 end Freeze_Subprogram_Body;
8545
8546 ----------------
8547 -- Get_Gen_Id --
8548 ----------------
8549
8550 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id is
8551 begin
8552 return Generic_Renamings.Table (E).Gen_Id;
8553 end Get_Gen_Id;
8554
8555 ---------------------
8556 -- Get_Instance_Of --
8557 ---------------------
8558
8559 function Get_Instance_Of (A : Entity_Id) return Entity_Id is
8560 Res : constant Assoc_Ptr := Generic_Renamings_HTable.Get (A);
8561
8562 begin
8563 if Res /= Assoc_Null then
8564 return Generic_Renamings.Table (Res).Act_Id;
8565
8566 else
8567 -- On exit, entity is not instantiated: not a generic parameter, or
8568 -- else parameter of an inner generic unit.
8569
8570 return A;
8571 end if;
8572 end Get_Instance_Of;
8573
8574 ------------------------------------
8575 -- Get_Package_Instantiation_Node --
8576 ------------------------------------
8577
8578 function Get_Package_Instantiation_Node (A : Entity_Id) return Node_Id is
8579 Decl : Node_Id := Unit_Declaration_Node (A);
8580 Inst : Node_Id;
8581
8582 begin
8583 -- If the Package_Instantiation attribute has been set on the package
8584 -- entity, then use it directly when it (or its Original_Node) refers
8585 -- to an N_Package_Instantiation node. In principle it should be
8586 -- possible to have this field set in all cases, which should be
8587 -- investigated, and would allow this function to be significantly
8588 -- simplified. ???
8589
8590 Inst := Package_Instantiation (A);
8591
8592 if Present (Inst) then
8593 if Nkind (Inst) = N_Package_Instantiation then
8594 return Inst;
8595
8596 elsif Nkind (Original_Node (Inst)) = N_Package_Instantiation then
8597 return Original_Node (Inst);
8598 end if;
8599 end if;
8600
8601 -- If the instantiation is a compilation unit that does not need body
8602 -- then the instantiation node has been rewritten as a package
8603 -- declaration for the instance, and we return the original node.
8604
8605 -- If it is a compilation unit and the instance node has not been
8606 -- rewritten, then it is still the unit of the compilation. Finally, if
8607 -- a body is present, this is a parent of the main unit whose body has
8608 -- been compiled for inlining purposes, and the instantiation node has
8609 -- been rewritten with the instance body.
8610
8611 -- Otherwise the instantiation node appears after the declaration. If
8612 -- the entity is a formal package, the declaration may have been
8613 -- rewritten as a generic declaration (in the case of a formal with box)
8614 -- or left as a formal package declaration if it has actuals, and is
8615 -- found with a forward search.
8616
8617 if Nkind (Parent (Decl)) = N_Compilation_Unit then
8618 if Nkind (Decl) = N_Package_Declaration
8619 and then Present (Corresponding_Body (Decl))
8620 then
8621 Decl := Unit_Declaration_Node (Corresponding_Body (Decl));
8622 end if;
8623
8624 if Nkind (Original_Node (Decl)) = N_Package_Instantiation then
8625 return Original_Node (Decl);
8626 else
8627 return Unit (Parent (Decl));
8628 end if;
8629
8630 elsif Nkind (Decl) = N_Package_Declaration
8631 and then Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration
8632 then
8633 return Original_Node (Decl);
8634
8635 else
8636 Inst := Next (Decl);
8637 while not Nkind_In (Inst, N_Package_Instantiation,
8638 N_Formal_Package_Declaration)
8639 loop
8640 Next (Inst);
8641 end loop;
8642
8643 return Inst;
8644 end if;
8645 end Get_Package_Instantiation_Node;
8646
8647 ------------------------
8648 -- Has_Been_Exchanged --
8649 ------------------------
8650
8651 function Has_Been_Exchanged (E : Entity_Id) return Boolean is
8652 Next : Elmt_Id;
8653
8654 begin
8655 Next := First_Elmt (Exchanged_Views);
8656 while Present (Next) loop
8657 if Full_View (Node (Next)) = E then
8658 return True;
8659 end if;
8660
8661 Next_Elmt (Next);
8662 end loop;
8663
8664 return False;
8665 end Has_Been_Exchanged;
8666
8667 ----------
8668 -- Hash --
8669 ----------
8670
8671 function Hash (F : Entity_Id) return HTable_Range is
8672 begin
8673 return HTable_Range (F mod HTable_Size);
8674 end Hash;
8675
8676 ------------------------
8677 -- Hide_Current_Scope --
8678 ------------------------
8679
8680 procedure Hide_Current_Scope is
8681 C : constant Entity_Id := Current_Scope;
8682 E : Entity_Id;
8683
8684 begin
8685 Set_Is_Hidden_Open_Scope (C);
8686
8687 E := First_Entity (C);
8688 while Present (E) loop
8689 if Is_Immediately_Visible (E) then
8690 Set_Is_Immediately_Visible (E, False);
8691 Append_Elmt (E, Hidden_Entities);
8692 end if;
8693
8694 Next_Entity (E);
8695 end loop;
8696
8697 -- Make the scope name invisible as well. This is necessary, but might
8698 -- conflict with calls to Rtsfind later on, in case the scope is a
8699 -- predefined one. There is no clean solution to this problem, so for
8700 -- now we depend on the user not redefining Standard itself in one of
8701 -- the parent units.
8702
8703 if Is_Immediately_Visible (C) and then C /= Standard_Standard then
8704 Set_Is_Immediately_Visible (C, False);
8705 Append_Elmt (C, Hidden_Entities);
8706 end if;
8707
8708 end Hide_Current_Scope;
8709
8710 --------------
8711 -- Init_Env --
8712 --------------
8713
8714 procedure Init_Env is
8715 Saved : Instance_Env;
8716
8717 begin
8718 Saved.Instantiated_Parent := Current_Instantiated_Parent;
8719 Saved.Exchanged_Views := Exchanged_Views;
8720 Saved.Hidden_Entities := Hidden_Entities;
8721 Saved.Current_Sem_Unit := Current_Sem_Unit;
8722 Saved.Parent_Unit_Visible := Parent_Unit_Visible;
8723 Saved.Instance_Parent_Unit := Instance_Parent_Unit;
8724
8725 -- Save configuration switches. These may be reset if the unit is a
8726 -- predefined unit, and the current mode is not Ada 2005.
8727
8728 Save_Opt_Config_Switches (Saved.Switches);
8729
8730 Instance_Envs.Append (Saved);
8731
8732 Exchanged_Views := New_Elmt_List;
8733 Hidden_Entities := New_Elmt_List;
8734
8735 -- Make dummy entry for Instantiated parent. If generic unit is legal,
8736 -- this is set properly in Set_Instance_Env.
8737
8738 Current_Instantiated_Parent :=
8739 (Current_Scope, Current_Scope, Assoc_Null);
8740 end Init_Env;
8741
8742 ------------------------------
8743 -- In_Same_Declarative_Part --
8744 ------------------------------
8745
8746 function In_Same_Declarative_Part
8747 (F_Node : Node_Id;
8748 Inst : Node_Id) return Boolean
8749 is
8750 Decls : constant Node_Id := Parent (F_Node);
8751 Nod : Node_Id;
8752
8753 begin
8754 Nod := Parent (Inst);
8755 while Present (Nod) loop
8756 if Nod = Decls then
8757 return True;
8758
8759 elsif Nkind_In (Nod, N_Subprogram_Body,
8760 N_Package_Body,
8761 N_Package_Declaration,
8762 N_Task_Body,
8763 N_Protected_Body,
8764 N_Block_Statement)
8765 then
8766 return False;
8767
8768 elsif Nkind (Nod) = N_Subunit then
8769 Nod := Corresponding_Stub (Nod);
8770
8771 elsif Nkind (Nod) = N_Compilation_Unit then
8772 return False;
8773
8774 else
8775 Nod := Parent (Nod);
8776 end if;
8777 end loop;
8778
8779 return False;
8780 end In_Same_Declarative_Part;
8781
8782 ---------------------
8783 -- In_Main_Context --
8784 ---------------------
8785
8786 function In_Main_Context (E : Entity_Id) return Boolean is
8787 Context : List_Id;
8788 Clause : Node_Id;
8789 Nam : Node_Id;
8790
8791 begin
8792 if not Is_Compilation_Unit (E)
8793 or else Ekind (E) /= E_Package
8794 or else In_Private_Part (E)
8795 then
8796 return False;
8797 end if;
8798
8799 Context := Context_Items (Cunit (Main_Unit));
8800
8801 Clause := First (Context);
8802 while Present (Clause) loop
8803 if Nkind (Clause) = N_With_Clause then
8804 Nam := Name (Clause);
8805
8806 -- If the current scope is part of the context of the main unit,
8807 -- analysis of the corresponding with_clause is not complete, and
8808 -- the entity is not set. We use the Chars field directly, which
8809 -- might produce false positives in rare cases, but guarantees
8810 -- that we produce all the instance bodies we will need.
8811
8812 if (Is_Entity_Name (Nam) and then Chars (Nam) = Chars (E))
8813 or else (Nkind (Nam) = N_Selected_Component
8814 and then Chars (Selector_Name (Nam)) = Chars (E))
8815 then
8816 return True;
8817 end if;
8818 end if;
8819
8820 Next (Clause);
8821 end loop;
8822
8823 return False;
8824 end In_Main_Context;
8825
8826 ---------------------
8827 -- Inherit_Context --
8828 ---------------------
8829
8830 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id) is
8831 Current_Context : List_Id;
8832 Current_Unit : Node_Id;
8833 Item : Node_Id;
8834 New_I : Node_Id;
8835
8836 Clause : Node_Id;
8837 OK : Boolean;
8838 Lib_Unit : Node_Id;
8839
8840 begin
8841 if Nkind (Parent (Gen_Decl)) = N_Compilation_Unit then
8842
8843 -- The inherited context is attached to the enclosing compilation
8844 -- unit. This is either the main unit, or the declaration for the
8845 -- main unit (in case the instantiation appears within the package
8846 -- declaration and the main unit is its body).
8847
8848 Current_Unit := Parent (Inst);
8849 while Present (Current_Unit)
8850 and then Nkind (Current_Unit) /= N_Compilation_Unit
8851 loop
8852 Current_Unit := Parent (Current_Unit);
8853 end loop;
8854
8855 Current_Context := Context_Items (Current_Unit);
8856
8857 Item := First (Context_Items (Parent (Gen_Decl)));
8858 while Present (Item) loop
8859 if Nkind (Item) = N_With_Clause then
8860 Lib_Unit := Library_Unit (Item);
8861
8862 -- Take care to prevent direct cyclic with's
8863
8864 if Lib_Unit /= Current_Unit then
8865
8866 -- Do not add a unit if it is already in the context
8867
8868 Clause := First (Current_Context);
8869 OK := True;
8870 while Present (Clause) loop
8871 if Nkind (Clause) = N_With_Clause and then
8872 Library_Unit (Clause) = Lib_Unit
8873 then
8874 OK := False;
8875 exit;
8876 end if;
8877
8878 Next (Clause);
8879 end loop;
8880
8881 if OK then
8882 New_I := New_Copy (Item);
8883 Set_Implicit_With (New_I, True);
8884 Set_Implicit_With_From_Instantiation (New_I, True);
8885 Append (New_I, Current_Context);
8886 end if;
8887 end if;
8888 end if;
8889
8890 Next (Item);
8891 end loop;
8892 end if;
8893 end Inherit_Context;
8894
8895 ----------------
8896 -- Initialize --
8897 ----------------
8898
8899 procedure Initialize is
8900 begin
8901 Generic_Renamings.Init;
8902 Instance_Envs.Init;
8903 Generic_Flags.Init;
8904 Generic_Renamings_HTable.Reset;
8905 Circularity_Detected := False;
8906 Exchanged_Views := No_Elist;
8907 Hidden_Entities := No_Elist;
8908 end Initialize;
8909
8910 -------------------------------------
8911 -- Insert_Freeze_Node_For_Instance --
8912 -------------------------------------
8913
8914 procedure Insert_Freeze_Node_For_Instance
8915 (N : Node_Id;
8916 F_Node : Node_Id)
8917 is
8918 Decl : Node_Id;
8919 Decls : List_Id;
8920 Inst : Entity_Id;
8921 Par_N : Node_Id;
8922
8923 function Enclosing_Body (N : Node_Id) return Node_Id;
8924 -- Find enclosing package or subprogram body, if any. Freeze node may
8925 -- be placed at end of current declarative list if previous instance
8926 -- and current one have different enclosing bodies.
8927
8928 function Previous_Instance (Gen : Entity_Id) return Entity_Id;
8929 -- Find the local instance, if any, that declares the generic that is
8930 -- being instantiated. If present, the freeze node for this instance
8931 -- must follow the freeze node for the previous instance.
8932
8933 --------------------
8934 -- Enclosing_Body --
8935 --------------------
8936
8937 function Enclosing_Body (N : Node_Id) return Node_Id is
8938 P : Node_Id;
8939
8940 begin
8941 P := Parent (N);
8942 while Present (P)
8943 and then Nkind (Parent (P)) /= N_Compilation_Unit
8944 loop
8945 if Nkind_In (P, N_Package_Body, N_Subprogram_Body) then
8946 if Nkind (Parent (P)) = N_Subunit then
8947 return Corresponding_Stub (Parent (P));
8948 else
8949 return P;
8950 end if;
8951 end if;
8952
8953 P := True_Parent (P);
8954 end loop;
8955
8956 return Empty;
8957 end Enclosing_Body;
8958
8959 -----------------------
8960 -- Previous_Instance --
8961 -----------------------
8962
8963 function Previous_Instance (Gen : Entity_Id) return Entity_Id is
8964 S : Entity_Id;
8965
8966 begin
8967 S := Scope (Gen);
8968 while Present (S) and then S /= Standard_Standard loop
8969 if Is_Generic_Instance (S)
8970 and then In_Same_Source_Unit (S, N)
8971 then
8972 return S;
8973 end if;
8974
8975 S := Scope (S);
8976 end loop;
8977
8978 return Empty;
8979 end Previous_Instance;
8980
8981 -- Start of processing for Insert_Freeze_Node_For_Instance
8982
8983 begin
8984 if not Is_List_Member (F_Node) then
8985 Decl := N;
8986 Decls := List_Containing (N);
8987 Inst := Entity (F_Node);
8988 Par_N := Parent (Decls);
8989
8990 -- When processing a subprogram instantiation, utilize the actual
8991 -- subprogram instantiation rather than its package wrapper as it
8992 -- carries all the context information.
8993
8994 if Is_Wrapper_Package (Inst) then
8995 Inst := Related_Instance (Inst);
8996 end if;
8997
8998 -- If this is a package instance, check whether the generic is
8999 -- declared in a previous instance and the current instance is
9000 -- not within the previous one.
9001
9002 if Present (Generic_Parent (Parent (Inst)))
9003 and then Is_In_Main_Unit (N)
9004 then
9005 declare
9006 Enclosing_N : constant Node_Id := Enclosing_Body (N);
9007 Par_I : constant Entity_Id :=
9008 Previous_Instance
9009 (Generic_Parent (Parent (Inst)));
9010 Scop : Entity_Id;
9011
9012 begin
9013 if Present (Par_I)
9014 and then Earlier (N, Freeze_Node (Par_I))
9015 then
9016 Scop := Scope (Inst);
9017
9018 -- If the current instance is within the one that contains
9019 -- the generic, the freeze node for the current one must
9020 -- appear in the current declarative part. Ditto, if the
9021 -- current instance is within another package instance or
9022 -- within a body that does not enclose the current instance.
9023 -- In these three cases the freeze node of the previous
9024 -- instance is not relevant.
9025
9026 while Present (Scop) and then Scop /= Standard_Standard loop
9027 exit when Scop = Par_I
9028 or else
9029 (Is_Generic_Instance (Scop)
9030 and then Scope_Depth (Scop) > Scope_Depth (Par_I));
9031 Scop := Scope (Scop);
9032 end loop;
9033
9034 -- Previous instance encloses current instance
9035
9036 if Scop = Par_I then
9037 null;
9038
9039 -- If the next node is a source body we must freeze in
9040 -- the current scope as well.
9041
9042 elsif Present (Next (N))
9043 and then Nkind_In (Next (N), N_Subprogram_Body,
9044 N_Package_Body)
9045 and then Comes_From_Source (Next (N))
9046 then
9047 null;
9048
9049 -- Current instance is within an unrelated instance
9050
9051 elsif Is_Generic_Instance (Scop) then
9052 null;
9053
9054 -- Current instance is within an unrelated body
9055
9056 elsif Present (Enclosing_N)
9057 and then Enclosing_N /= Enclosing_Body (Par_I)
9058 then
9059 null;
9060
9061 else
9062 Insert_After (Freeze_Node (Par_I), F_Node);
9063 return;
9064 end if;
9065 end if;
9066 end;
9067 end if;
9068
9069 -- When the instantiation occurs in a package declaration, append the
9070 -- freeze node to the private declarations (if any).
9071
9072 if Nkind (Par_N) = N_Package_Specification
9073 and then Decls = Visible_Declarations (Par_N)
9074 and then Present (Private_Declarations (Par_N))
9075 and then not Is_Empty_List (Private_Declarations (Par_N))
9076 then
9077 Decls := Private_Declarations (Par_N);
9078 Decl := First (Decls);
9079 end if;
9080
9081 -- Determine the proper freeze point of a package instantiation. We
9082 -- adhere to the general rule of a package or subprogram body causing
9083 -- freezing of anything before it in the same declarative region. In
9084 -- this case, the proper freeze point of a package instantiation is
9085 -- before the first source body which follows, or before a stub. This
9086 -- ensures that entities coming from the instance are already frozen
9087 -- and usable in source bodies.
9088
9089 if Nkind (Par_N) /= N_Package_Declaration
9090 and then Ekind (Inst) = E_Package
9091 and then Is_Generic_Instance (Inst)
9092 and then
9093 not In_Same_Source_Unit (Generic_Parent (Parent (Inst)), Inst)
9094 then
9095 while Present (Decl) loop
9096 if (Nkind (Decl) in N_Unit_Body
9097 or else
9098 Nkind (Decl) in N_Body_Stub)
9099 and then Comes_From_Source (Decl)
9100 then
9101 Insert_Before (Decl, F_Node);
9102 return;
9103 end if;
9104
9105 Next (Decl);
9106 end loop;
9107 end if;
9108
9109 -- In a package declaration, or if no previous body, insert at end
9110 -- of list.
9111
9112 Set_Sloc (F_Node, Sloc (Last (Decls)));
9113 Insert_After (Last (Decls), F_Node);
9114 end if;
9115 end Insert_Freeze_Node_For_Instance;
9116
9117 ------------------
9118 -- Install_Body --
9119 ------------------
9120
9121 procedure Install_Body
9122 (Act_Body : Node_Id;
9123 N : Node_Id;
9124 Gen_Body : Node_Id;
9125 Gen_Decl : Node_Id)
9126 is
9127 function In_Same_Scope (Gen_Id, Act_Id : Node_Id) return Boolean;
9128 -- Check if the generic definition and the instantiation come from
9129 -- a common scope, in which case the instance must be frozen after
9130 -- the generic body.
9131
9132 function True_Sloc (N, Act_Unit : Node_Id) return Source_Ptr;
9133 -- If the instance is nested inside a generic unit, the Sloc of the
9134 -- instance indicates the place of the original definition, not the
9135 -- point of the current enclosing instance. Pending a better usage of
9136 -- Slocs to indicate instantiation places, we determine the place of
9137 -- origin of a node by finding the maximum sloc of any ancestor node.
9138 -- Why is this not equivalent to Top_Level_Location ???
9139
9140 -------------------
9141 -- In_Same_Scope --
9142 -------------------
9143
9144 function In_Same_Scope (Gen_Id, Act_Id : Node_Id) return Boolean is
9145 Act_Scop : Entity_Id := Scope (Act_Id);
9146 Gen_Scop : Entity_Id := Scope (Gen_Id);
9147
9148 begin
9149 while Act_Scop /= Standard_Standard
9150 and then Gen_Scop /= Standard_Standard
9151 loop
9152 if Act_Scop = Gen_Scop then
9153 return True;
9154 end if;
9155
9156 Act_Scop := Scope (Act_Scop);
9157 Gen_Scop := Scope (Gen_Scop);
9158 end loop;
9159
9160 return False;
9161 end In_Same_Scope;
9162
9163 ---------------
9164 -- True_Sloc --
9165 ---------------
9166
9167 function True_Sloc (N, Act_Unit : Node_Id) return Source_Ptr is
9168 N1 : Node_Id;
9169 Res : Source_Ptr;
9170
9171 begin
9172 Res := Sloc (N);
9173 N1 := N;
9174 while Present (N1) and then N1 /= Act_Unit loop
9175 if Sloc (N1) > Res then
9176 Res := Sloc (N1);
9177 end if;
9178
9179 N1 := Parent (N1);
9180 end loop;
9181
9182 return Res;
9183 end True_Sloc;
9184
9185 Act_Id : constant Entity_Id := Corresponding_Spec (Act_Body);
9186 Act_Unit : constant Node_Id := Unit (Cunit (Get_Source_Unit (N)));
9187 Gen_Id : constant Entity_Id := Corresponding_Spec (Gen_Body);
9188 Par : constant Entity_Id := Scope (Gen_Id);
9189 Gen_Unit : constant Node_Id :=
9190 Unit (Cunit (Get_Source_Unit (Gen_Decl)));
9191
9192 Body_Unit : Node_Id;
9193 F_Node : Node_Id;
9194 Must_Delay : Boolean;
9195 Orig_Body : Node_Id := Gen_Body;
9196
9197 -- Start of processing for Install_Body
9198
9199 begin
9200 -- Handle first the case of an instance with incomplete actual types.
9201 -- The instance body cannot be placed after the declaration because
9202 -- full views have not been seen yet. Any use of the non-limited views
9203 -- in the instance body requires the presence of a regular with_clause
9204 -- in the enclosing unit, and will fail if this with_clause is missing.
9205 -- We place the instance body at the beginning of the enclosing body,
9206 -- which is the unit being compiled. The freeze node for the instance
9207 -- is then placed after the instance body.
9208
9209 if not Is_Empty_Elmt_List (Incomplete_Actuals (Act_Id))
9210 and then Expander_Active
9211 and then Ekind (Scope (Act_Id)) = E_Package
9212 then
9213 declare
9214 Scop : constant Entity_Id := Scope (Act_Id);
9215 Body_Id : constant Node_Id :=
9216 Corresponding_Body (Unit_Declaration_Node (Scop));
9217
9218 begin
9219 Ensure_Freeze_Node (Act_Id);
9220 F_Node := Freeze_Node (Act_Id);
9221 if Present (Body_Id) then
9222 Set_Is_Frozen (Act_Id, False);
9223 Prepend (Act_Body, Declarations (Parent (Body_Id)));
9224 if Is_List_Member (F_Node) then
9225 Remove (F_Node);
9226 end if;
9227
9228 Insert_After (Act_Body, F_Node);
9229 end if;
9230 end;
9231 return;
9232 end if;
9233
9234 -- If the body is a subunit, the freeze point is the corresponding stub
9235 -- in the current compilation, not the subunit itself.
9236
9237 if Nkind (Parent (Gen_Body)) = N_Subunit then
9238 Orig_Body := Corresponding_Stub (Parent (Gen_Body));
9239 else
9240 Orig_Body := Gen_Body;
9241 end if;
9242
9243 Body_Unit := Unit (Cunit (Get_Source_Unit (Orig_Body)));
9244
9245 -- If the instantiation and the generic definition appear in the same
9246 -- package declaration, this is an early instantiation. If they appear
9247 -- in the same declarative part, it is an early instantiation only if
9248 -- the generic body appears textually later, and the generic body is
9249 -- also in the main unit.
9250
9251 -- If instance is nested within a subprogram, and the generic body
9252 -- is not, the instance is delayed because the enclosing body is. If
9253 -- instance and body are within the same scope, or the same subprogram
9254 -- body, indicate explicitly that the instance is delayed.
9255
9256 Must_Delay :=
9257 (Gen_Unit = Act_Unit
9258 and then (Nkind_In (Gen_Unit, N_Generic_Package_Declaration,
9259 N_Package_Declaration)
9260 or else (Gen_Unit = Body_Unit
9261 and then True_Sloc (N, Act_Unit)
9262 < Sloc (Orig_Body)))
9263 and then Is_In_Main_Unit (Original_Node (Gen_Unit))
9264 and then In_Same_Scope (Gen_Id, Act_Id));
9265
9266 -- If this is an early instantiation, the freeze node is placed after
9267 -- the generic body. Otherwise, if the generic appears in an instance,
9268 -- we cannot freeze the current instance until the outer one is frozen.
9269 -- This is only relevant if the current instance is nested within some
9270 -- inner scope not itself within the outer instance. If this scope is
9271 -- a package body in the same declarative part as the outer instance,
9272 -- then that body needs to be frozen after the outer instance. Finally,
9273 -- if no delay is needed, we place the freeze node at the end of the
9274 -- current declarative part.
9275
9276 if Expander_Active then
9277 Ensure_Freeze_Node (Act_Id);
9278 F_Node := Freeze_Node (Act_Id);
9279
9280 if Must_Delay then
9281 Insert_After (Orig_Body, F_Node);
9282
9283 elsif Is_Generic_Instance (Par)
9284 and then Present (Freeze_Node (Par))
9285 and then Scope (Act_Id) /= Par
9286 then
9287 -- Freeze instance of inner generic after instance of enclosing
9288 -- generic.
9289
9290 if In_Same_Declarative_Part (Freeze_Node (Par), N) then
9291
9292 -- Handle the following case:
9293
9294 -- package Parent_Inst is new ...
9295 -- Parent_Inst []
9296
9297 -- procedure P ... -- this body freezes Parent_Inst
9298
9299 -- package Inst is new ...
9300
9301 -- In this particular scenario, the freeze node for Inst must
9302 -- be inserted in the same manner as that of Parent_Inst,
9303 -- before the next source body or at the end of the declarative
9304 -- list (body not available). If body P did not exist and
9305 -- Parent_Inst was frozen after Inst, either by a body
9306 -- following Inst or at the end of the declarative region,
9307 -- the freeze node for Inst must be inserted after that of
9308 -- Parent_Inst. This relation is established by comparing
9309 -- the Slocs of Parent_Inst freeze node and Inst.
9310
9311 if List_Containing (Get_Package_Instantiation_Node (Par)) =
9312 List_Containing (N)
9313 and then Sloc (Freeze_Node (Par)) < Sloc (N)
9314 then
9315 Insert_Freeze_Node_For_Instance (N, F_Node);
9316 else
9317 Insert_After (Freeze_Node (Par), F_Node);
9318 end if;
9319
9320 -- Freeze package enclosing instance of inner generic after
9321 -- instance of enclosing generic.
9322
9323 elsif Nkind_In (Parent (N), N_Package_Body, N_Subprogram_Body)
9324 and then In_Same_Declarative_Part (Freeze_Node (Par), Parent (N))
9325 then
9326 declare
9327 Enclosing : Entity_Id;
9328
9329 begin
9330 Enclosing := Corresponding_Spec (Parent (N));
9331
9332 if No (Enclosing) then
9333 Enclosing := Defining_Entity (Parent (N));
9334 end if;
9335
9336 Insert_Freeze_Node_For_Instance (N, F_Node);
9337 Ensure_Freeze_Node (Enclosing);
9338
9339 if not Is_List_Member (Freeze_Node (Enclosing)) then
9340
9341 -- The enclosing context is a subunit, insert the freeze
9342 -- node after the stub.
9343
9344 if Nkind (Parent (Parent (N))) = N_Subunit then
9345 Insert_Freeze_Node_For_Instance
9346 (Corresponding_Stub (Parent (Parent (N))),
9347 Freeze_Node (Enclosing));
9348
9349 -- The enclosing context is a package with a stub body
9350 -- which has already been replaced by the real body.
9351 -- Insert the freeze node after the actual body.
9352
9353 elsif Ekind (Enclosing) = E_Package
9354 and then Present (Body_Entity (Enclosing))
9355 and then Was_Originally_Stub
9356 (Parent (Body_Entity (Enclosing)))
9357 then
9358 Insert_Freeze_Node_For_Instance
9359 (Parent (Body_Entity (Enclosing)),
9360 Freeze_Node (Enclosing));
9361
9362 -- The parent instance has been frozen before the body of
9363 -- the enclosing package, insert the freeze node after
9364 -- the body.
9365
9366 elsif List_Containing (Freeze_Node (Par)) =
9367 List_Containing (Parent (N))
9368 and then Sloc (Freeze_Node (Par)) < Sloc (Parent (N))
9369 then
9370 Insert_Freeze_Node_For_Instance
9371 (Parent (N), Freeze_Node (Enclosing));
9372
9373 else
9374 Insert_After
9375 (Freeze_Node (Par), Freeze_Node (Enclosing));
9376 end if;
9377 end if;
9378 end;
9379
9380 else
9381 Insert_Freeze_Node_For_Instance (N, F_Node);
9382 end if;
9383
9384 else
9385 Insert_Freeze_Node_For_Instance (N, F_Node);
9386 end if;
9387 end if;
9388
9389 Set_Is_Frozen (Act_Id);
9390 Insert_Before (N, Act_Body);
9391 Mark_Rewrite_Insertion (Act_Body);
9392 end Install_Body;
9393
9394 -----------------------------
9395 -- Install_Formal_Packages --
9396 -----------------------------
9397
9398 procedure Install_Formal_Packages (Par : Entity_Id) is
9399 E : Entity_Id;
9400 Gen : Entity_Id;
9401 Gen_E : Entity_Id := Empty;
9402
9403 begin
9404 E := First_Entity (Par);
9405
9406 -- If we are installing an instance parent, locate the formal packages
9407 -- of its generic parent.
9408
9409 if Is_Generic_Instance (Par) then
9410 Gen := Generic_Parent (Package_Specification (Par));
9411 Gen_E := First_Entity (Gen);
9412 end if;
9413
9414 while Present (E) loop
9415 if Ekind (E) = E_Package
9416 and then Nkind (Parent (E)) = N_Package_Renaming_Declaration
9417 then
9418 -- If this is the renaming for the parent instance, done
9419
9420 if Renamed_Object (E) = Par then
9421 exit;
9422
9423 -- The visibility of a formal of an enclosing generic is already
9424 -- correct.
9425
9426 elsif Denotes_Formal_Package (E) then
9427 null;
9428
9429 elsif Present (Associated_Formal_Package (E)) then
9430 Check_Generic_Actuals (Renamed_Object (E), True);
9431 Set_Is_Hidden (E, False);
9432
9433 -- Find formal package in generic unit that corresponds to
9434 -- (instance of) formal package in instance.
9435
9436 while Present (Gen_E) and then Chars (Gen_E) /= Chars (E) loop
9437 Next_Entity (Gen_E);
9438 end loop;
9439
9440 if Present (Gen_E) then
9441 Map_Formal_Package_Entities (Gen_E, E);
9442 end if;
9443 end if;
9444 end if;
9445
9446 Next_Entity (E);
9447
9448 if Present (Gen_E) then
9449 Next_Entity (Gen_E);
9450 end if;
9451 end loop;
9452 end Install_Formal_Packages;
9453
9454 --------------------
9455 -- Install_Parent --
9456 --------------------
9457
9458 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False) is
9459 Ancestors : constant Elist_Id := New_Elmt_List;
9460 S : constant Entity_Id := Current_Scope;
9461 Inst_Par : Entity_Id;
9462 First_Par : Entity_Id;
9463 Inst_Node : Node_Id;
9464 Gen_Par : Entity_Id;
9465 First_Gen : Entity_Id;
9466 Elmt : Elmt_Id;
9467
9468 procedure Install_Noninstance_Specs (Par : Entity_Id);
9469 -- Install the scopes of noninstance parent units ending with Par
9470
9471 procedure Install_Spec (Par : Entity_Id);
9472 -- The child unit is within the declarative part of the parent, so the
9473 -- declarations within the parent are immediately visible.
9474
9475 -------------------------------
9476 -- Install_Noninstance_Specs --
9477 -------------------------------
9478
9479 procedure Install_Noninstance_Specs (Par : Entity_Id) is
9480 begin
9481 if Present (Par)
9482 and then Par /= Standard_Standard
9483 and then not In_Open_Scopes (Par)
9484 then
9485 Install_Noninstance_Specs (Scope (Par));
9486 Install_Spec (Par);
9487 end if;
9488 end Install_Noninstance_Specs;
9489
9490 ------------------
9491 -- Install_Spec --
9492 ------------------
9493
9494 procedure Install_Spec (Par : Entity_Id) is
9495 Spec : constant Node_Id := Package_Specification (Par);
9496
9497 begin
9498 -- If this parent of the child instance is a top-level unit,
9499 -- then record the unit and its visibility for later resetting in
9500 -- Remove_Parent. We exclude units that are generic instances, as we
9501 -- only want to record this information for the ultimate top-level
9502 -- noninstance parent (is that always correct???).
9503
9504 if Scope (Par) = Standard_Standard
9505 and then not Is_Generic_Instance (Par)
9506 then
9507 Parent_Unit_Visible := Is_Immediately_Visible (Par);
9508 Instance_Parent_Unit := Par;
9509 end if;
9510
9511 -- Open the parent scope and make it and its declarations visible.
9512 -- If this point is not within a body, then only the visible
9513 -- declarations should be made visible, and installation of the
9514 -- private declarations is deferred until the appropriate point
9515 -- within analysis of the spec being instantiated (see the handling
9516 -- of parent visibility in Analyze_Package_Specification). This is
9517 -- relaxed in the case where the parent unit is Ada.Tags, to avoid
9518 -- private view problems that occur when compiling instantiations of
9519 -- a generic child of that package (Generic_Dispatching_Constructor).
9520 -- If the instance freezes a tagged type, inlinings of operations
9521 -- from Ada.Tags may need the full view of type Tag. If inlining took
9522 -- proper account of establishing visibility of inlined subprograms'
9523 -- parents then it should be possible to remove this
9524 -- special check. ???
9525
9526 Push_Scope (Par);
9527 Set_Is_Immediately_Visible (Par);
9528 Install_Visible_Declarations (Par);
9529 Set_Use (Visible_Declarations (Spec));
9530
9531 if In_Body or else Is_RTU (Par, Ada_Tags) then
9532 Install_Private_Declarations (Par);
9533 Set_Use (Private_Declarations (Spec));
9534 end if;
9535 end Install_Spec;
9536
9537 -- Start of processing for Install_Parent
9538
9539 begin
9540 -- We need to install the parent instance to compile the instantiation
9541 -- of the child, but the child instance must appear in the current
9542 -- scope. Given that we cannot place the parent above the current scope
9543 -- in the scope stack, we duplicate the current scope and unstack both
9544 -- after the instantiation is complete.
9545
9546 -- If the parent is itself the instantiation of a child unit, we must
9547 -- also stack the instantiation of its parent, and so on. Each such
9548 -- ancestor is the prefix of the name in a prior instantiation.
9549
9550 -- If this is a nested instance, the parent unit itself resolves to
9551 -- a renaming of the parent instance, whose declaration we need.
9552
9553 -- Finally, the parent may be a generic (not an instance) when the
9554 -- child unit appears as a formal package.
9555
9556 Inst_Par := P;
9557
9558 if Present (Renamed_Entity (Inst_Par)) then
9559 Inst_Par := Renamed_Entity (Inst_Par);
9560 end if;
9561
9562 First_Par := Inst_Par;
9563
9564 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
9565
9566 First_Gen := Gen_Par;
9567
9568 while Present (Gen_Par) and then Is_Child_Unit (Gen_Par) loop
9569
9570 -- Load grandparent instance as well
9571
9572 Inst_Node := Get_Package_Instantiation_Node (Inst_Par);
9573
9574 if Nkind (Name (Inst_Node)) = N_Expanded_Name then
9575 Inst_Par := Entity (Prefix (Name (Inst_Node)));
9576
9577 if Present (Renamed_Entity (Inst_Par)) then
9578 Inst_Par := Renamed_Entity (Inst_Par);
9579 end if;
9580
9581 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
9582
9583 if Present (Gen_Par) then
9584 Prepend_Elmt (Inst_Par, Ancestors);
9585
9586 else
9587 -- Parent is not the name of an instantiation
9588
9589 Install_Noninstance_Specs (Inst_Par);
9590 exit;
9591 end if;
9592
9593 else
9594 -- Previous error
9595
9596 exit;
9597 end if;
9598 end loop;
9599
9600 if Present (First_Gen) then
9601 Append_Elmt (First_Par, Ancestors);
9602 else
9603 Install_Noninstance_Specs (First_Par);
9604 end if;
9605
9606 if not Is_Empty_Elmt_List (Ancestors) then
9607 Elmt := First_Elmt (Ancestors);
9608 while Present (Elmt) loop
9609 Install_Spec (Node (Elmt));
9610 Install_Formal_Packages (Node (Elmt));
9611 Next_Elmt (Elmt);
9612 end loop;
9613 end if;
9614
9615 if not In_Body then
9616 Push_Scope (S);
9617 end if;
9618 end Install_Parent;
9619
9620 -------------------------------
9621 -- Install_Hidden_Primitives --
9622 -------------------------------
9623
9624 procedure Install_Hidden_Primitives
9625 (Prims_List : in out Elist_Id;
9626 Gen_T : Entity_Id;
9627 Act_T : Entity_Id)
9628 is
9629 Elmt : Elmt_Id;
9630 List : Elist_Id := No_Elist;
9631 Prim_G_Elmt : Elmt_Id;
9632 Prim_A_Elmt : Elmt_Id;
9633 Prim_G : Node_Id;
9634 Prim_A : Node_Id;
9635
9636 begin
9637 -- No action needed in case of serious errors because we cannot trust
9638 -- in the order of primitives
9639
9640 if Serious_Errors_Detected > 0 then
9641 return;
9642
9643 -- No action possible if we don't have available the list of primitive
9644 -- operations
9645
9646 elsif No (Gen_T)
9647 or else not Is_Record_Type (Gen_T)
9648 or else not Is_Tagged_Type (Gen_T)
9649 or else not Is_Record_Type (Act_T)
9650 or else not Is_Tagged_Type (Act_T)
9651 then
9652 return;
9653
9654 -- There is no need to handle interface types since their primitives
9655 -- cannot be hidden
9656
9657 elsif Is_Interface (Gen_T) then
9658 return;
9659 end if;
9660
9661 Prim_G_Elmt := First_Elmt (Primitive_Operations (Gen_T));
9662
9663 if not Is_Class_Wide_Type (Act_T) then
9664 Prim_A_Elmt := First_Elmt (Primitive_Operations (Act_T));
9665 else
9666 Prim_A_Elmt := First_Elmt (Primitive_Operations (Root_Type (Act_T)));
9667 end if;
9668
9669 loop
9670 -- Skip predefined primitives in the generic formal
9671
9672 while Present (Prim_G_Elmt)
9673 and then Is_Predefined_Dispatching_Operation (Node (Prim_G_Elmt))
9674 loop
9675 Next_Elmt (Prim_G_Elmt);
9676 end loop;
9677
9678 -- Skip predefined primitives in the generic actual
9679
9680 while Present (Prim_A_Elmt)
9681 and then Is_Predefined_Dispatching_Operation (Node (Prim_A_Elmt))
9682 loop
9683 Next_Elmt (Prim_A_Elmt);
9684 end loop;
9685
9686 exit when No (Prim_G_Elmt) or else No (Prim_A_Elmt);
9687
9688 Prim_G := Node (Prim_G_Elmt);
9689 Prim_A := Node (Prim_A_Elmt);
9690
9691 -- There is no need to handle interface primitives because their
9692 -- primitives are not hidden
9693
9694 exit when Present (Interface_Alias (Prim_G));
9695
9696 -- Here we install one hidden primitive
9697
9698 if Chars (Prim_G) /= Chars (Prim_A)
9699 and then Has_Suffix (Prim_A, 'P')
9700 and then Remove_Suffix (Prim_A, 'P') = Chars (Prim_G)
9701 then
9702 Set_Chars (Prim_A, Chars (Prim_G));
9703 Append_New_Elmt (Prim_A, To => List);
9704 end if;
9705
9706 Next_Elmt (Prim_A_Elmt);
9707 Next_Elmt (Prim_G_Elmt);
9708 end loop;
9709
9710 -- Append the elements to the list of temporarily visible primitives
9711 -- avoiding duplicates.
9712
9713 if Present (List) then
9714 if No (Prims_List) then
9715 Prims_List := New_Elmt_List;
9716 end if;
9717
9718 Elmt := First_Elmt (List);
9719 while Present (Elmt) loop
9720 Append_Unique_Elmt (Node (Elmt), Prims_List);
9721 Next_Elmt (Elmt);
9722 end loop;
9723 end if;
9724 end Install_Hidden_Primitives;
9725
9726 -------------------------------
9727 -- Restore_Hidden_Primitives --
9728 -------------------------------
9729
9730 procedure Restore_Hidden_Primitives (Prims_List : in out Elist_Id) is
9731 Prim_Elmt : Elmt_Id;
9732 Prim : Node_Id;
9733
9734 begin
9735 if Prims_List /= No_Elist then
9736 Prim_Elmt := First_Elmt (Prims_List);
9737 while Present (Prim_Elmt) loop
9738 Prim := Node (Prim_Elmt);
9739 Set_Chars (Prim, Add_Suffix (Prim, 'P'));
9740 Next_Elmt (Prim_Elmt);
9741 end loop;
9742
9743 Prims_List := No_Elist;
9744 end if;
9745 end Restore_Hidden_Primitives;
9746
9747 --------------------------------
9748 -- Instantiate_Formal_Package --
9749 --------------------------------
9750
9751 function Instantiate_Formal_Package
9752 (Formal : Node_Id;
9753 Actual : Node_Id;
9754 Analyzed_Formal : Node_Id) return List_Id
9755 is
9756 Loc : constant Source_Ptr := Sloc (Actual);
9757 Actual_Pack : Entity_Id;
9758 Formal_Pack : Entity_Id;
9759 Gen_Parent : Entity_Id;
9760 Decls : List_Id;
9761 Nod : Node_Id;
9762 Parent_Spec : Node_Id;
9763
9764 procedure Find_Matching_Actual
9765 (F : Node_Id;
9766 Act : in out Entity_Id);
9767 -- We need to associate each formal entity in the formal package with
9768 -- the corresponding entity in the actual package. The actual package
9769 -- has been analyzed and possibly expanded, and as a result there is
9770 -- no one-to-one correspondence between the two lists (for example,
9771 -- the actual may include subtypes, itypes, and inherited primitive
9772 -- operations, interspersed among the renaming declarations for the
9773 -- actuals). We retrieve the corresponding actual by name because each
9774 -- actual has the same name as the formal, and they do appear in the
9775 -- same order.
9776
9777 function Get_Formal_Entity (N : Node_Id) return Entity_Id;
9778 -- Retrieve entity of defining entity of generic formal parameter.
9779 -- Only the declarations of formals need to be considered when
9780 -- linking them to actuals, but the declarative list may include
9781 -- internal entities generated during analysis, and those are ignored.
9782
9783 procedure Match_Formal_Entity
9784 (Formal_Node : Node_Id;
9785 Formal_Ent : Entity_Id;
9786 Actual_Ent : Entity_Id);
9787 -- Associates the formal entity with the actual. In the case where
9788 -- Formal_Ent is a formal package, this procedure iterates through all
9789 -- of its formals and enters associations between the actuals occurring
9790 -- in the formal package's corresponding actual package (given by
9791 -- Actual_Ent) and the formal package's formal parameters. This
9792 -- procedure recurses if any of the parameters is itself a package.
9793
9794 function Is_Instance_Of
9795 (Act_Spec : Entity_Id;
9796 Gen_Anc : Entity_Id) return Boolean;
9797 -- The actual can be an instantiation of a generic within another
9798 -- instance, in which case there is no direct link from it to the
9799 -- original generic ancestor. In that case, we recognize that the
9800 -- ultimate ancestor is the same by examining names and scopes.
9801
9802 procedure Process_Nested_Formal (Formal : Entity_Id);
9803 -- If the current formal is declared with a box, its own formals are
9804 -- visible in the instance, as they were in the generic, and their
9805 -- Hidden flag must be reset. If some of these formals are themselves
9806 -- packages declared with a box, the processing must be recursive.
9807
9808 --------------------------
9809 -- Find_Matching_Actual --
9810 --------------------------
9811
9812 procedure Find_Matching_Actual
9813 (F : Node_Id;
9814 Act : in out Entity_Id)
9815 is
9816 Formal_Ent : Entity_Id;
9817
9818 begin
9819 case Nkind (Original_Node (F)) is
9820 when N_Formal_Object_Declaration
9821 | N_Formal_Type_Declaration
9822 =>
9823 Formal_Ent := Defining_Identifier (F);
9824
9825 while Chars (Act) /= Chars (Formal_Ent) loop
9826 Next_Entity (Act);
9827 end loop;
9828
9829 when N_Formal_Package_Declaration
9830 | N_Formal_Subprogram_Declaration
9831 | N_Generic_Package_Declaration
9832 | N_Package_Declaration
9833 =>
9834 Formal_Ent := Defining_Entity (F);
9835
9836 while Chars (Act) /= Chars (Formal_Ent) loop
9837 Next_Entity (Act);
9838 end loop;
9839
9840 when others =>
9841 raise Program_Error;
9842 end case;
9843 end Find_Matching_Actual;
9844
9845 -------------------------
9846 -- Match_Formal_Entity --
9847 -------------------------
9848
9849 procedure Match_Formal_Entity
9850 (Formal_Node : Node_Id;
9851 Formal_Ent : Entity_Id;
9852 Actual_Ent : Entity_Id)
9853 is
9854 Act_Pkg : Entity_Id;
9855
9856 begin
9857 Set_Instance_Of (Formal_Ent, Actual_Ent);
9858
9859 if Ekind (Actual_Ent) = E_Package then
9860
9861 -- Record associations for each parameter
9862
9863 Act_Pkg := Actual_Ent;
9864
9865 declare
9866 A_Ent : Entity_Id := First_Entity (Act_Pkg);
9867 F_Ent : Entity_Id;
9868 F_Node : Node_Id;
9869
9870 Gen_Decl : Node_Id;
9871 Formals : List_Id;
9872 Actual : Entity_Id;
9873
9874 begin
9875 -- Retrieve the actual given in the formal package declaration
9876
9877 Actual := Entity (Name (Original_Node (Formal_Node)));
9878
9879 -- The actual in the formal package declaration may be a
9880 -- renamed generic package, in which case we want to retrieve
9881 -- the original generic in order to traverse its formal part.
9882
9883 if Present (Renamed_Entity (Actual)) then
9884 Gen_Decl := Unit_Declaration_Node (Renamed_Entity (Actual));
9885 else
9886 Gen_Decl := Unit_Declaration_Node (Actual);
9887 end if;
9888
9889 Formals := Generic_Formal_Declarations (Gen_Decl);
9890
9891 if Present (Formals) then
9892 F_Node := First_Non_Pragma (Formals);
9893 else
9894 F_Node := Empty;
9895 end if;
9896
9897 while Present (A_Ent)
9898 and then Present (F_Node)
9899 and then A_Ent /= First_Private_Entity (Act_Pkg)
9900 loop
9901 F_Ent := Get_Formal_Entity (F_Node);
9902
9903 if Present (F_Ent) then
9904
9905 -- This is a formal of the original package. Record
9906 -- association and recurse.
9907
9908 Find_Matching_Actual (F_Node, A_Ent);
9909 Match_Formal_Entity (F_Node, F_Ent, A_Ent);
9910 Next_Entity (A_Ent);
9911 end if;
9912
9913 Next_Non_Pragma (F_Node);
9914 end loop;
9915 end;
9916 end if;
9917 end Match_Formal_Entity;
9918
9919 -----------------------
9920 -- Get_Formal_Entity --
9921 -----------------------
9922
9923 function Get_Formal_Entity (N : Node_Id) return Entity_Id is
9924 Kind : constant Node_Kind := Nkind (Original_Node (N));
9925 begin
9926 case Kind is
9927 when N_Formal_Object_Declaration =>
9928 return Defining_Identifier (N);
9929
9930 when N_Formal_Type_Declaration =>
9931 return Defining_Identifier (N);
9932
9933 when N_Formal_Subprogram_Declaration =>
9934 return Defining_Unit_Name (Specification (N));
9935
9936 when N_Formal_Package_Declaration =>
9937 return Defining_Identifier (Original_Node (N));
9938
9939 when N_Generic_Package_Declaration =>
9940 return Defining_Identifier (Original_Node (N));
9941
9942 -- All other declarations are introduced by semantic analysis and
9943 -- have no match in the actual.
9944
9945 when others =>
9946 return Empty;
9947 end case;
9948 end Get_Formal_Entity;
9949
9950 --------------------
9951 -- Is_Instance_Of --
9952 --------------------
9953
9954 function Is_Instance_Of
9955 (Act_Spec : Entity_Id;
9956 Gen_Anc : Entity_Id) return Boolean
9957 is
9958 Gen_Par : constant Entity_Id := Generic_Parent (Act_Spec);
9959
9960 begin
9961 if No (Gen_Par) then
9962 return False;
9963
9964 -- Simplest case: the generic parent of the actual is the formal
9965
9966 elsif Gen_Par = Gen_Anc then
9967 return True;
9968
9969 elsif Chars (Gen_Par) /= Chars (Gen_Anc) then
9970 return False;
9971
9972 -- The actual may be obtained through several instantiations. Its
9973 -- scope must itself be an instance of a generic declared in the
9974 -- same scope as the formal. Any other case is detected above.
9975
9976 elsif not Is_Generic_Instance (Scope (Gen_Par)) then
9977 return False;
9978
9979 else
9980 return Generic_Parent (Parent (Scope (Gen_Par))) = Scope (Gen_Anc);
9981 end if;
9982 end Is_Instance_Of;
9983
9984 ---------------------------
9985 -- Process_Nested_Formal --
9986 ---------------------------
9987
9988 procedure Process_Nested_Formal (Formal : Entity_Id) is
9989 Ent : Entity_Id;
9990
9991 begin
9992 if Present (Associated_Formal_Package (Formal))
9993 and then Box_Present (Parent (Associated_Formal_Package (Formal)))
9994 then
9995 Ent := First_Entity (Formal);
9996 while Present (Ent) loop
9997 Set_Is_Hidden (Ent, False);
9998 Set_Is_Visible_Formal (Ent);
9999 Set_Is_Potentially_Use_Visible
10000 (Ent, Is_Potentially_Use_Visible (Formal));
10001
10002 if Ekind (Ent) = E_Package then
10003 exit when Renamed_Entity (Ent) = Renamed_Entity (Formal);
10004 Process_Nested_Formal (Ent);
10005 end if;
10006
10007 Next_Entity (Ent);
10008 end loop;
10009 end if;
10010 end Process_Nested_Formal;
10011
10012 -- Start of processing for Instantiate_Formal_Package
10013
10014 begin
10015 Analyze (Actual);
10016
10017 if not Is_Entity_Name (Actual)
10018 or else Ekind (Entity (Actual)) /= E_Package
10019 then
10020 Error_Msg_N
10021 ("expect package instance to instantiate formal", Actual);
10022 Abandon_Instantiation (Actual);
10023 raise Program_Error;
10024
10025 else
10026 Actual_Pack := Entity (Actual);
10027 Set_Is_Instantiated (Actual_Pack);
10028
10029 -- The actual may be a renamed package, or an outer generic formal
10030 -- package whose instantiation is converted into a renaming.
10031
10032 if Present (Renamed_Object (Actual_Pack)) then
10033 Actual_Pack := Renamed_Object (Actual_Pack);
10034 end if;
10035
10036 if Nkind (Analyzed_Formal) = N_Formal_Package_Declaration then
10037 Gen_Parent := Get_Instance_Of (Entity (Name (Analyzed_Formal)));
10038 Formal_Pack := Defining_Identifier (Analyzed_Formal);
10039 else
10040 Gen_Parent :=
10041 Generic_Parent (Specification (Analyzed_Formal));
10042 Formal_Pack :=
10043 Defining_Unit_Name (Specification (Analyzed_Formal));
10044 end if;
10045
10046 if Nkind (Parent (Actual_Pack)) = N_Defining_Program_Unit_Name then
10047 Parent_Spec := Package_Specification (Actual_Pack);
10048 else
10049 Parent_Spec := Parent (Actual_Pack);
10050 end if;
10051
10052 if Gen_Parent = Any_Id then
10053 Error_Msg_N
10054 ("previous error in declaration of formal package", Actual);
10055 Abandon_Instantiation (Actual);
10056
10057 elsif
10058 Is_Instance_Of (Parent_Spec, Get_Instance_Of (Gen_Parent))
10059 then
10060 null;
10061
10062 else
10063 Error_Msg_NE
10064 ("actual parameter must be instance of&", Actual, Gen_Parent);
10065 Abandon_Instantiation (Actual);
10066 end if;
10067
10068 Set_Instance_Of (Defining_Identifier (Formal), Actual_Pack);
10069 Map_Formal_Package_Entities (Formal_Pack, Actual_Pack);
10070
10071 Nod :=
10072 Make_Package_Renaming_Declaration (Loc,
10073 Defining_Unit_Name => New_Copy (Defining_Identifier (Formal)),
10074 Name => New_Occurrence_Of (Actual_Pack, Loc));
10075
10076 Set_Associated_Formal_Package
10077 (Defining_Unit_Name (Nod), Defining_Identifier (Formal));
10078 Decls := New_List (Nod);
10079
10080 -- If the formal F has a box, then the generic declarations are
10081 -- visible in the generic G. In an instance of G, the corresponding
10082 -- entities in the actual for F (which are the actuals for the
10083 -- instantiation of the generic that F denotes) must also be made
10084 -- visible for analysis of the current instance. On exit from the
10085 -- current instance, those entities are made private again. If the
10086 -- actual is currently in use, these entities are also use-visible.
10087
10088 -- The loop through the actual entities also steps through the formal
10089 -- entities and enters associations from formals to actuals into the
10090 -- renaming map. This is necessary to properly handle checking of
10091 -- actual parameter associations for later formals that depend on
10092 -- actuals declared in the formal package.
10093
10094 -- In Ada 2005, partial parameterization requires that we make
10095 -- visible the actuals corresponding to formals that were defaulted
10096 -- in the formal package. There formals are identified because they
10097 -- remain formal generics within the formal package, rather than
10098 -- being renamings of the actuals supplied.
10099
10100 declare
10101 Gen_Decl : constant Node_Id :=
10102 Unit_Declaration_Node (Gen_Parent);
10103 Formals : constant List_Id :=
10104 Generic_Formal_Declarations (Gen_Decl);
10105
10106 Actual_Ent : Entity_Id;
10107 Actual_Of_Formal : Node_Id;
10108 Formal_Node : Node_Id;
10109 Formal_Ent : Entity_Id;
10110
10111 begin
10112 if Present (Formals) then
10113 Formal_Node := First_Non_Pragma (Formals);
10114 else
10115 Formal_Node := Empty;
10116 end if;
10117
10118 Actual_Ent := First_Entity (Actual_Pack);
10119 Actual_Of_Formal :=
10120 First (Visible_Declarations (Specification (Analyzed_Formal)));
10121 while Present (Actual_Ent)
10122 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
10123 loop
10124 if Present (Formal_Node) then
10125 Formal_Ent := Get_Formal_Entity (Formal_Node);
10126
10127 if Present (Formal_Ent) then
10128 Find_Matching_Actual (Formal_Node, Actual_Ent);
10129 Match_Formal_Entity (Formal_Node, Formal_Ent, Actual_Ent);
10130
10131 -- We iterate at the same time over the actuals of the
10132 -- local package created for the formal, to determine
10133 -- which one of the formals of the original generic were
10134 -- defaulted in the formal. The corresponding actual
10135 -- entities are visible in the enclosing instance.
10136
10137 if Box_Present (Formal)
10138 or else
10139 (Present (Actual_Of_Formal)
10140 and then
10141 Is_Generic_Formal
10142 (Get_Formal_Entity (Actual_Of_Formal)))
10143 then
10144 Set_Is_Hidden (Actual_Ent, False);
10145 Set_Is_Visible_Formal (Actual_Ent);
10146 Set_Is_Potentially_Use_Visible
10147 (Actual_Ent, In_Use (Actual_Pack));
10148
10149 if Ekind (Actual_Ent) = E_Package then
10150 Process_Nested_Formal (Actual_Ent);
10151 end if;
10152
10153 else
10154 Set_Is_Hidden (Actual_Ent);
10155 Set_Is_Potentially_Use_Visible (Actual_Ent, False);
10156 end if;
10157 end if;
10158
10159 Next_Non_Pragma (Formal_Node);
10160 Next (Actual_Of_Formal);
10161
10162 else
10163 -- No further formals to match, but the generic part may
10164 -- contain inherited operation that are not hidden in the
10165 -- enclosing instance.
10166
10167 Next_Entity (Actual_Ent);
10168 end if;
10169 end loop;
10170
10171 -- Inherited subprograms generated by formal derived types are
10172 -- also visible if the types are.
10173
10174 Actual_Ent := First_Entity (Actual_Pack);
10175 while Present (Actual_Ent)
10176 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
10177 loop
10178 if Is_Overloadable (Actual_Ent)
10179 and then
10180 Nkind (Parent (Actual_Ent)) = N_Subtype_Declaration
10181 and then
10182 not Is_Hidden (Defining_Identifier (Parent (Actual_Ent)))
10183 then
10184 Set_Is_Hidden (Actual_Ent, False);
10185 Set_Is_Potentially_Use_Visible
10186 (Actual_Ent, In_Use (Actual_Pack));
10187 end if;
10188
10189 Next_Entity (Actual_Ent);
10190 end loop;
10191 end;
10192
10193 -- If the formal is not declared with a box, reanalyze it as an
10194 -- abbreviated instantiation, to verify the matching rules of 12.7.
10195 -- The actual checks are performed after the generic associations
10196 -- have been analyzed, to guarantee the same visibility for this
10197 -- instantiation and for the actuals.
10198
10199 -- In Ada 2005, the generic associations for the formal can include
10200 -- defaulted parameters. These are ignored during check. This
10201 -- internal instantiation is removed from the tree after conformance
10202 -- checking, because it contains formal declarations for those
10203 -- defaulted parameters, and those should not reach the back-end.
10204
10205 if not Box_Present (Formal) then
10206 declare
10207 I_Pack : constant Entity_Id :=
10208 Make_Temporary (Sloc (Actual), 'P');
10209
10210 begin
10211 Set_Is_Internal (I_Pack);
10212
10213 Append_To (Decls,
10214 Make_Package_Instantiation (Sloc (Actual),
10215 Defining_Unit_Name => I_Pack,
10216 Name =>
10217 New_Occurrence_Of
10218 (Get_Instance_Of (Gen_Parent), Sloc (Actual)),
10219 Generic_Associations => Generic_Associations (Formal)));
10220 end;
10221 end if;
10222
10223 return Decls;
10224 end if;
10225 end Instantiate_Formal_Package;
10226
10227 -----------------------------------
10228 -- Instantiate_Formal_Subprogram --
10229 -----------------------------------
10230
10231 function Instantiate_Formal_Subprogram
10232 (Formal : Node_Id;
10233 Actual : Node_Id;
10234 Analyzed_Formal : Node_Id) return Node_Id
10235 is
10236 Analyzed_S : constant Entity_Id :=
10237 Defining_Unit_Name (Specification (Analyzed_Formal));
10238 Formal_Sub : constant Entity_Id :=
10239 Defining_Unit_Name (Specification (Formal));
10240
10241 function From_Parent_Scope (Subp : Entity_Id) return Boolean;
10242 -- If the generic is a child unit, the parent has been installed on the
10243 -- scope stack, but a default subprogram cannot resolve to something
10244 -- on the parent because that parent is not really part of the visible
10245 -- context (it is there to resolve explicit local entities). If the
10246 -- default has resolved in this way, we remove the entity from immediate
10247 -- visibility and analyze the node again to emit an error message or
10248 -- find another visible candidate.
10249
10250 procedure Valid_Actual_Subprogram (Act : Node_Id);
10251 -- Perform legality check and raise exception on failure
10252
10253 -----------------------
10254 -- From_Parent_Scope --
10255 -----------------------
10256
10257 function From_Parent_Scope (Subp : Entity_Id) return Boolean is
10258 Gen_Scope : Node_Id;
10259
10260 begin
10261 Gen_Scope := Scope (Analyzed_S);
10262 while Present (Gen_Scope) and then Is_Child_Unit (Gen_Scope) loop
10263 if Scope (Subp) = Scope (Gen_Scope) then
10264 return True;
10265 end if;
10266
10267 Gen_Scope := Scope (Gen_Scope);
10268 end loop;
10269
10270 return False;
10271 end From_Parent_Scope;
10272
10273 -----------------------------
10274 -- Valid_Actual_Subprogram --
10275 -----------------------------
10276
10277 procedure Valid_Actual_Subprogram (Act : Node_Id) is
10278 Act_E : Entity_Id;
10279
10280 begin
10281 if Is_Entity_Name (Act) then
10282 Act_E := Entity (Act);
10283
10284 elsif Nkind (Act) = N_Selected_Component
10285 and then Is_Entity_Name (Selector_Name (Act))
10286 then
10287 Act_E := Entity (Selector_Name (Act));
10288
10289 else
10290 Act_E := Empty;
10291 end if;
10292
10293 if (Present (Act_E) and then Is_Overloadable (Act_E))
10294 or else Nkind_In (Act, N_Attribute_Reference,
10295 N_Indexed_Component,
10296 N_Character_Literal,
10297 N_Explicit_Dereference)
10298 then
10299 return;
10300 end if;
10301
10302 Error_Msg_NE
10303 ("expect subprogram or entry name in instantiation of &",
10304 Instantiation_Node, Formal_Sub);
10305 Abandon_Instantiation (Instantiation_Node);
10306 end Valid_Actual_Subprogram;
10307
10308 -- Local variables
10309
10310 Decl_Node : Node_Id;
10311 Loc : Source_Ptr;
10312 Nam : Node_Id;
10313 New_Spec : Node_Id;
10314 New_Subp : Entity_Id;
10315
10316 -- Start of processing for Instantiate_Formal_Subprogram
10317
10318 begin
10319 New_Spec := New_Copy_Tree (Specification (Formal));
10320
10321 -- The tree copy has created the proper instantiation sloc for the
10322 -- new specification. Use this location for all other constructed
10323 -- declarations.
10324
10325 Loc := Sloc (Defining_Unit_Name (New_Spec));
10326
10327 -- Create new entity for the actual (New_Copy_Tree does not), and
10328 -- indicate that it is an actual.
10329
10330 New_Subp := Make_Defining_Identifier (Loc, Chars (Formal_Sub));
10331 Set_Ekind (New_Subp, Ekind (Analyzed_S));
10332 Set_Is_Generic_Actual_Subprogram (New_Subp);
10333 Set_Defining_Unit_Name (New_Spec, New_Subp);
10334
10335 -- Create new entities for the each of the formals in the specification
10336 -- of the renaming declaration built for the actual.
10337
10338 if Present (Parameter_Specifications (New_Spec)) then
10339 declare
10340 F : Node_Id;
10341 F_Id : Entity_Id;
10342
10343 begin
10344 F := First (Parameter_Specifications (New_Spec));
10345 while Present (F) loop
10346 F_Id := Defining_Identifier (F);
10347
10348 Set_Defining_Identifier (F,
10349 Make_Defining_Identifier (Sloc (F_Id), Chars (F_Id)));
10350 Next (F);
10351 end loop;
10352 end;
10353 end if;
10354
10355 -- Find entity of actual. If the actual is an attribute reference, it
10356 -- cannot be resolved here (its formal is missing) but is handled
10357 -- instead in Attribute_Renaming. If the actual is overloaded, it is
10358 -- fully resolved subsequently, when the renaming declaration for the
10359 -- formal is analyzed. If it is an explicit dereference, resolve the
10360 -- prefix but not the actual itself, to prevent interpretation as call.
10361
10362 if Present (Actual) then
10363 Loc := Sloc (Actual);
10364 Set_Sloc (New_Spec, Loc);
10365
10366 if Nkind (Actual) = N_Operator_Symbol then
10367 Find_Direct_Name (Actual);
10368
10369 elsif Nkind (Actual) = N_Explicit_Dereference then
10370 Analyze (Prefix (Actual));
10371
10372 elsif Nkind (Actual) /= N_Attribute_Reference then
10373 Analyze (Actual);
10374 end if;
10375
10376 Valid_Actual_Subprogram (Actual);
10377 Nam := Actual;
10378
10379 elsif Present (Default_Name (Formal)) then
10380 if not Nkind_In (Default_Name (Formal), N_Attribute_Reference,
10381 N_Selected_Component,
10382 N_Indexed_Component,
10383 N_Character_Literal)
10384 and then Present (Entity (Default_Name (Formal)))
10385 then
10386 Nam := New_Occurrence_Of (Entity (Default_Name (Formal)), Loc);
10387 else
10388 Nam := New_Copy (Default_Name (Formal));
10389 Set_Sloc (Nam, Loc);
10390 end if;
10391
10392 elsif Box_Present (Formal) then
10393
10394 -- Actual is resolved at the point of instantiation. Create an
10395 -- identifier or operator with the same name as the formal.
10396
10397 if Nkind (Formal_Sub) = N_Defining_Operator_Symbol then
10398 Nam :=
10399 Make_Operator_Symbol (Loc,
10400 Chars => Chars (Formal_Sub),
10401 Strval => No_String);
10402 else
10403 Nam := Make_Identifier (Loc, Chars (Formal_Sub));
10404 end if;
10405
10406 elsif Nkind (Specification (Formal)) = N_Procedure_Specification
10407 and then Null_Present (Specification (Formal))
10408 then
10409 -- Generate null body for procedure, for use in the instance
10410
10411 Decl_Node :=
10412 Make_Subprogram_Body (Loc,
10413 Specification => New_Spec,
10414 Declarations => New_List,
10415 Handled_Statement_Sequence =>
10416 Make_Handled_Sequence_Of_Statements (Loc,
10417 Statements => New_List (Make_Null_Statement (Loc))));
10418
10419 Set_Is_Intrinsic_Subprogram (Defining_Unit_Name (New_Spec));
10420 return Decl_Node;
10421
10422 else
10423 Error_Msg_Sloc := Sloc (Scope (Analyzed_S));
10424 Error_Msg_NE
10425 ("missing actual&", Instantiation_Node, Formal_Sub);
10426 Error_Msg_NE
10427 ("\in instantiation of & declared#",
10428 Instantiation_Node, Scope (Analyzed_S));
10429 Abandon_Instantiation (Instantiation_Node);
10430 end if;
10431
10432 Decl_Node :=
10433 Make_Subprogram_Renaming_Declaration (Loc,
10434 Specification => New_Spec,
10435 Name => Nam);
10436
10437 -- If we do not have an actual and the formal specified <> then set to
10438 -- get proper default.
10439
10440 if No (Actual) and then Box_Present (Formal) then
10441 Set_From_Default (Decl_Node);
10442 end if;
10443
10444 -- Gather possible interpretations for the actual before analyzing the
10445 -- instance. If overloaded, it will be resolved when analyzing the
10446 -- renaming declaration.
10447
10448 if Box_Present (Formal) and then No (Actual) then
10449 Analyze (Nam);
10450
10451 if Is_Child_Unit (Scope (Analyzed_S))
10452 and then Present (Entity (Nam))
10453 then
10454 if not Is_Overloaded (Nam) then
10455 if From_Parent_Scope (Entity (Nam)) then
10456 Set_Is_Immediately_Visible (Entity (Nam), False);
10457 Set_Entity (Nam, Empty);
10458 Set_Etype (Nam, Empty);
10459
10460 Analyze (Nam);
10461 Set_Is_Immediately_Visible (Entity (Nam));
10462 end if;
10463
10464 else
10465 declare
10466 I : Interp_Index;
10467 It : Interp;
10468
10469 begin
10470 Get_First_Interp (Nam, I, It);
10471 while Present (It.Nam) loop
10472 if From_Parent_Scope (It.Nam) then
10473 Remove_Interp (I);
10474 end if;
10475
10476 Get_Next_Interp (I, It);
10477 end loop;
10478 end;
10479 end if;
10480 end if;
10481 end if;
10482
10483 -- The generic instantiation freezes the actual. This can only be done
10484 -- once the actual is resolved, in the analysis of the renaming
10485 -- declaration. To make the formal subprogram entity available, we set
10486 -- Corresponding_Formal_Spec to point to the formal subprogram entity.
10487 -- This is also needed in Analyze_Subprogram_Renaming for the processing
10488 -- of formal abstract subprograms.
10489
10490 Set_Corresponding_Formal_Spec (Decl_Node, Analyzed_S);
10491
10492 -- We cannot analyze the renaming declaration, and thus find the actual,
10493 -- until all the actuals are assembled in the instance. For subsequent
10494 -- checks of other actuals, indicate the node that will hold the
10495 -- instance of this formal.
10496
10497 Set_Instance_Of (Analyzed_S, Nam);
10498
10499 if Nkind (Actual) = N_Selected_Component
10500 and then Is_Task_Type (Etype (Prefix (Actual)))
10501 and then not Is_Frozen (Etype (Prefix (Actual)))
10502 then
10503 -- The renaming declaration will create a body, which must appear
10504 -- outside of the instantiation, We move the renaming declaration
10505 -- out of the instance, and create an additional renaming inside,
10506 -- to prevent freezing anomalies.
10507
10508 declare
10509 Anon_Id : constant Entity_Id := Make_Temporary (Loc, 'E');
10510
10511 begin
10512 Set_Defining_Unit_Name (New_Spec, Anon_Id);
10513 Insert_Before (Instantiation_Node, Decl_Node);
10514 Analyze (Decl_Node);
10515
10516 -- Now create renaming within the instance
10517
10518 Decl_Node :=
10519 Make_Subprogram_Renaming_Declaration (Loc,
10520 Specification => New_Copy_Tree (New_Spec),
10521 Name => New_Occurrence_Of (Anon_Id, Loc));
10522
10523 Set_Defining_Unit_Name (Specification (Decl_Node),
10524 Make_Defining_Identifier (Loc, Chars (Formal_Sub)));
10525 end;
10526 end if;
10527
10528 return Decl_Node;
10529 end Instantiate_Formal_Subprogram;
10530
10531 ------------------------
10532 -- Instantiate_Object --
10533 ------------------------
10534
10535 function Instantiate_Object
10536 (Formal : Node_Id;
10537 Actual : Node_Id;
10538 Analyzed_Formal : Node_Id) return List_Id
10539 is
10540 Gen_Obj : constant Entity_Id := Defining_Identifier (Formal);
10541 A_Gen_Obj : constant Entity_Id :=
10542 Defining_Identifier (Analyzed_Formal);
10543 Acc_Def : Node_Id := Empty;
10544 Act_Assoc : constant Node_Id := Parent (Actual);
10545 Actual_Decl : Node_Id := Empty;
10546 Decl_Node : Node_Id;
10547 Def : Node_Id;
10548 Ftyp : Entity_Id;
10549 List : constant List_Id := New_List;
10550 Loc : constant Source_Ptr := Sloc (Actual);
10551 Orig_Ftyp : constant Entity_Id := Etype (A_Gen_Obj);
10552 Subt_Decl : Node_Id := Empty;
10553 Subt_Mark : Node_Id := Empty;
10554
10555 function Copy_Access_Def return Node_Id;
10556 -- If formal is an anonymous access, copy access definition of formal
10557 -- for generated object declaration.
10558
10559 ---------------------
10560 -- Copy_Access_Def --
10561 ---------------------
10562
10563 function Copy_Access_Def return Node_Id is
10564 begin
10565 Def := New_Copy_Tree (Acc_Def);
10566
10567 -- In addition, if formal is an access to subprogram we need to
10568 -- generate new formals for the signature of the default, so that
10569 -- the tree is properly formatted for ASIS use.
10570
10571 if Present (Access_To_Subprogram_Definition (Acc_Def)) then
10572 declare
10573 Par_Spec : Node_Id;
10574 begin
10575 Par_Spec :=
10576 First (Parameter_Specifications
10577 (Access_To_Subprogram_Definition (Def)));
10578 while Present (Par_Spec) loop
10579 Set_Defining_Identifier (Par_Spec,
10580 Make_Defining_Identifier (Sloc (Acc_Def),
10581 Chars => Chars (Defining_Identifier (Par_Spec))));
10582 Next (Par_Spec);
10583 end loop;
10584 end;
10585 end if;
10586
10587 return Def;
10588 end Copy_Access_Def;
10589
10590 -- Start of processing for Instantiate_Object
10591
10592 begin
10593 -- Formal may be an anonymous access
10594
10595 if Present (Subtype_Mark (Formal)) then
10596 Subt_Mark := Subtype_Mark (Formal);
10597 else
10598 Check_Access_Definition (Formal);
10599 Acc_Def := Access_Definition (Formal);
10600 end if;
10601
10602 -- Sloc for error message on missing actual
10603
10604 Error_Msg_Sloc := Sloc (Scope (A_Gen_Obj));
10605
10606 if Get_Instance_Of (Gen_Obj) /= Gen_Obj then
10607 Error_Msg_N ("duplicate instantiation of generic parameter", Actual);
10608 end if;
10609
10610 Set_Parent (List, Parent (Actual));
10611
10612 -- OUT present
10613
10614 if Out_Present (Formal) then
10615
10616 -- An IN OUT generic actual must be a name. The instantiation is a
10617 -- renaming declaration. The actual is the name being renamed. We
10618 -- use the actual directly, rather than a copy, because it is not
10619 -- used further in the list of actuals, and because a copy or a use
10620 -- of relocate_node is incorrect if the instance is nested within a
10621 -- generic. In order to simplify ASIS searches, the Generic_Parent
10622 -- field links the declaration to the generic association.
10623
10624 if No (Actual) then
10625 Error_Msg_NE
10626 ("missing actual &",
10627 Instantiation_Node, Gen_Obj);
10628 Error_Msg_NE
10629 ("\in instantiation of & declared#",
10630 Instantiation_Node, Scope (A_Gen_Obj));
10631 Abandon_Instantiation (Instantiation_Node);
10632 end if;
10633
10634 if Present (Subt_Mark) then
10635 Decl_Node :=
10636 Make_Object_Renaming_Declaration (Loc,
10637 Defining_Identifier => New_Copy (Gen_Obj),
10638 Subtype_Mark => New_Copy_Tree (Subt_Mark),
10639 Name => Actual);
10640
10641 else pragma Assert (Present (Acc_Def));
10642 Decl_Node :=
10643 Make_Object_Renaming_Declaration (Loc,
10644 Defining_Identifier => New_Copy (Gen_Obj),
10645 Access_Definition => New_Copy_Tree (Acc_Def),
10646 Name => Actual);
10647 end if;
10648
10649 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
10650
10651 -- The analysis of the actual may produce Insert_Action nodes, so
10652 -- the declaration must have a context in which to attach them.
10653
10654 Append (Decl_Node, List);
10655 Analyze (Actual);
10656
10657 -- Return if the analysis of the actual reported some error
10658
10659 if Etype (Actual) = Any_Type then
10660 return List;
10661 end if;
10662
10663 -- This check is performed here because Analyze_Object_Renaming will
10664 -- not check it when Comes_From_Source is False. Note though that the
10665 -- check for the actual being the name of an object will be performed
10666 -- in Analyze_Object_Renaming.
10667
10668 if Is_Object_Reference (Actual)
10669 and then Is_Dependent_Component_Of_Mutable_Object (Actual)
10670 then
10671 Error_Msg_N
10672 ("illegal discriminant-dependent component for in out parameter",
10673 Actual);
10674 end if;
10675
10676 -- The actual has to be resolved in order to check that it is a
10677 -- variable (due to cases such as F (1), where F returns access to
10678 -- an array, and for overloaded prefixes).
10679
10680 Ftyp := Get_Instance_Of (Etype (A_Gen_Obj));
10681
10682 -- If the type of the formal is not itself a formal, and the current
10683 -- unit is a child unit, the formal type must be declared in a
10684 -- parent, and must be retrieved by visibility.
10685
10686 if Ftyp = Orig_Ftyp
10687 and then Is_Generic_Unit (Scope (Ftyp))
10688 and then Is_Child_Unit (Scope (A_Gen_Obj))
10689 then
10690 declare
10691 Temp : constant Node_Id :=
10692 New_Copy_Tree (Subtype_Mark (Analyzed_Formal));
10693 begin
10694 Set_Entity (Temp, Empty);
10695 Find_Type (Temp);
10696 Ftyp := Entity (Temp);
10697 end;
10698 end if;
10699
10700 if Is_Private_Type (Ftyp)
10701 and then not Is_Private_Type (Etype (Actual))
10702 and then (Base_Type (Full_View (Ftyp)) = Base_Type (Etype (Actual))
10703 or else Base_Type (Etype (Actual)) = Ftyp)
10704 then
10705 -- If the actual has the type of the full view of the formal, or
10706 -- else a non-private subtype of the formal, then the visibility
10707 -- of the formal type has changed. Add to the actuals a subtype
10708 -- declaration that will force the exchange of views in the body
10709 -- of the instance as well.
10710
10711 Subt_Decl :=
10712 Make_Subtype_Declaration (Loc,
10713 Defining_Identifier => Make_Temporary (Loc, 'P'),
10714 Subtype_Indication => New_Occurrence_Of (Ftyp, Loc));
10715
10716 Prepend (Subt_Decl, List);
10717
10718 Prepend_Elmt (Full_View (Ftyp), Exchanged_Views);
10719 Exchange_Declarations (Ftyp);
10720 end if;
10721
10722 Resolve (Actual, Ftyp);
10723
10724 if not Denotes_Variable (Actual) then
10725 Error_Msg_NE ("actual for& must be a variable", Actual, Gen_Obj);
10726
10727 elsif Base_Type (Ftyp) /= Base_Type (Etype (Actual)) then
10728
10729 -- Ada 2005 (AI-423): For a generic formal object of mode in out,
10730 -- the type of the actual shall resolve to a specific anonymous
10731 -- access type.
10732
10733 if Ada_Version < Ada_2005
10734 or else Ekind (Base_Type (Ftyp)) /=
10735 E_Anonymous_Access_Type
10736 or else Ekind (Base_Type (Etype (Actual))) /=
10737 E_Anonymous_Access_Type
10738 then
10739 Error_Msg_NE
10740 ("type of actual does not match type of&", Actual, Gen_Obj);
10741 end if;
10742 end if;
10743
10744 Note_Possible_Modification (Actual, Sure => True);
10745
10746 -- Check for instantiation of atomic/volatile actual for
10747 -- non-atomic/volatile formal (RM C.6 (12)).
10748
10749 if Is_Atomic_Object (Actual) and then not Is_Atomic (Orig_Ftyp) then
10750 Error_Msg_N
10751 ("cannot instantiate non-atomic formal object "
10752 & "with atomic actual", Actual);
10753
10754 elsif Is_Volatile_Object (Actual) and then not Is_Volatile (Orig_Ftyp)
10755 then
10756 Error_Msg_N
10757 ("cannot instantiate non-volatile formal object "
10758 & "with volatile actual", Actual);
10759 end if;
10760
10761 -- Formal in-parameter
10762
10763 else
10764 -- The instantiation of a generic formal in-parameter is constant
10765 -- declaration. The actual is the expression for that declaration.
10766 -- Its type is a full copy of the type of the formal. This may be
10767 -- an access to subprogram, for which we need to generate entities
10768 -- for the formals in the new signature.
10769
10770 if Present (Actual) then
10771 if Present (Subt_Mark) then
10772 Def := New_Copy_Tree (Subt_Mark);
10773 else pragma Assert (Present (Acc_Def));
10774 Def := Copy_Access_Def;
10775 end if;
10776
10777 Decl_Node :=
10778 Make_Object_Declaration (Loc,
10779 Defining_Identifier => New_Copy (Gen_Obj),
10780 Constant_Present => True,
10781 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
10782 Object_Definition => Def,
10783 Expression => Actual);
10784
10785 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
10786
10787 -- A generic formal object of a tagged type is defined to be
10788 -- aliased so the new constant must also be treated as aliased.
10789
10790 if Is_Tagged_Type (Etype (A_Gen_Obj)) then
10791 Set_Aliased_Present (Decl_Node);
10792 end if;
10793
10794 Append (Decl_Node, List);
10795
10796 -- No need to repeat (pre-)analysis of some expression nodes
10797 -- already handled in Preanalyze_Actuals.
10798
10799 if Nkind (Actual) /= N_Allocator then
10800 Analyze (Actual);
10801
10802 -- Return if the analysis of the actual reported some error
10803
10804 if Etype (Actual) = Any_Type then
10805 return List;
10806 end if;
10807 end if;
10808
10809 declare
10810 Formal_Type : constant Entity_Id := Etype (A_Gen_Obj);
10811 Typ : Entity_Id;
10812
10813 begin
10814 Typ := Get_Instance_Of (Formal_Type);
10815
10816 -- If the actual appears in the current or an enclosing scope,
10817 -- use its type directly. This is relevant if it has an actual
10818 -- subtype that is distinct from its nominal one. This cannot
10819 -- be done in general because the type of the actual may
10820 -- depend on other actuals, and only be fully determined when
10821 -- the enclosing instance is analyzed.
10822
10823 if Present (Etype (Actual))
10824 and then Is_Constr_Subt_For_U_Nominal (Etype (Actual))
10825 then
10826 Freeze_Before (Instantiation_Node, Etype (Actual));
10827 else
10828 Freeze_Before (Instantiation_Node, Typ);
10829 end if;
10830
10831 -- If the actual is an aggregate, perform name resolution on
10832 -- its components (the analysis of an aggregate does not do it)
10833 -- to capture local names that may be hidden if the generic is
10834 -- a child unit.
10835
10836 if Nkind (Actual) = N_Aggregate then
10837 Preanalyze_And_Resolve (Actual, Typ);
10838 end if;
10839
10840 if Is_Limited_Type (Typ)
10841 and then not OK_For_Limited_Init (Typ, Actual)
10842 then
10843 Error_Msg_N
10844 ("initialization not allowed for limited types", Actual);
10845 Explain_Limited_Type (Typ, Actual);
10846 end if;
10847 end;
10848
10849 elsif Present (Default_Expression (Formal)) then
10850
10851 -- Use default to construct declaration
10852
10853 if Present (Subt_Mark) then
10854 Def := New_Copy (Subt_Mark);
10855 else pragma Assert (Present (Acc_Def));
10856 Def := Copy_Access_Def;
10857 end if;
10858
10859 Decl_Node :=
10860 Make_Object_Declaration (Sloc (Formal),
10861 Defining_Identifier => New_Copy (Gen_Obj),
10862 Constant_Present => True,
10863 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
10864 Object_Definition => Def,
10865 Expression => New_Copy_Tree
10866 (Default_Expression (Formal)));
10867
10868 Append (Decl_Node, List);
10869 Set_Analyzed (Expression (Decl_Node), False);
10870
10871 else
10872 Error_Msg_NE ("missing actual&", Instantiation_Node, Gen_Obj);
10873 Error_Msg_NE ("\in instantiation of & declared#",
10874 Instantiation_Node, Scope (A_Gen_Obj));
10875
10876 if Is_Scalar_Type (Etype (A_Gen_Obj)) then
10877
10878 -- Create dummy constant declaration so that instance can be
10879 -- analyzed, to minimize cascaded visibility errors.
10880
10881 if Present (Subt_Mark) then
10882 Def := Subt_Mark;
10883 else pragma Assert (Present (Acc_Def));
10884 Def := Acc_Def;
10885 end if;
10886
10887 Decl_Node :=
10888 Make_Object_Declaration (Loc,
10889 Defining_Identifier => New_Copy (Gen_Obj),
10890 Constant_Present => True,
10891 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
10892 Object_Definition => New_Copy (Def),
10893 Expression =>
10894 Make_Attribute_Reference (Sloc (Gen_Obj),
10895 Attribute_Name => Name_First,
10896 Prefix => New_Copy (Def)));
10897
10898 Append (Decl_Node, List);
10899
10900 else
10901 Abandon_Instantiation (Instantiation_Node);
10902 end if;
10903 end if;
10904 end if;
10905
10906 if Nkind (Actual) in N_Has_Entity then
10907 Actual_Decl := Parent (Entity (Actual));
10908 end if;
10909
10910 -- Ada 2005 (AI-423): For a formal object declaration with a null
10911 -- exclusion or an access definition that has a null exclusion: If the
10912 -- actual matching the formal object declaration denotes a generic
10913 -- formal object of another generic unit G, and the instantiation
10914 -- containing the actual occurs within the body of G or within the body
10915 -- of a generic unit declared within the declarative region of G, then
10916 -- the declaration of the formal object of G must have a null exclusion.
10917 -- Otherwise, the subtype of the actual matching the formal object
10918 -- declaration shall exclude null.
10919
10920 if Ada_Version >= Ada_2005
10921 and then Present (Actual_Decl)
10922 and then Nkind_In (Actual_Decl, N_Formal_Object_Declaration,
10923 N_Object_Declaration)
10924 and then Nkind (Analyzed_Formal) = N_Formal_Object_Declaration
10925 and then not Has_Null_Exclusion (Actual_Decl)
10926 and then Has_Null_Exclusion (Analyzed_Formal)
10927 then
10928 Error_Msg_Sloc := Sloc (Analyzed_Formal);
10929 Error_Msg_N
10930 ("actual must exclude null to match generic formal#", Actual);
10931 end if;
10932
10933 -- An effectively volatile object cannot be used as an actual in a
10934 -- generic instantiation (SPARK RM 7.1.3(7)). The following check is
10935 -- relevant only when SPARK_Mode is on as it is not a standard Ada
10936 -- legality rule, and also verifies that the actual is an object.
10937
10938 if SPARK_Mode = On
10939 and then Present (Actual)
10940 and then Is_Object_Reference (Actual)
10941 and then Is_Effectively_Volatile_Object (Actual)
10942 then
10943 Error_Msg_N
10944 ("volatile object cannot act as actual in generic instantiation",
10945 Actual);
10946 end if;
10947
10948 return List;
10949 end Instantiate_Object;
10950
10951 ------------------------------
10952 -- Instantiate_Package_Body --
10953 ------------------------------
10954
10955 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
10956 -- must be replaced by gotos which jump to the end of the routine in order
10957 -- to restore the Ghost and SPARK modes.
10958
10959 procedure Instantiate_Package_Body
10960 (Body_Info : Pending_Body_Info;
10961 Inlined_Body : Boolean := False;
10962 Body_Optional : Boolean := False)
10963 is
10964 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
10965 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Decl);
10966 Act_Spec : constant Node_Id := Specification (Act_Decl);
10967 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
10968 Gen_Id : constant Node_Id := Name (Inst_Node);
10969 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
10970 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
10971 Loc : constant Source_Ptr := Sloc (Inst_Node);
10972
10973 Saved_ISMP : constant Boolean :=
10974 Ignore_SPARK_Mode_Pragmas_In_Instance;
10975 Saved_Style_Check : constant Boolean := Style_Check;
10976
10977 procedure Check_Initialized_Types;
10978 -- In a generic package body, an entity of a generic private type may
10979 -- appear uninitialized. This is suspicious, unless the actual is a
10980 -- fully initialized type.
10981
10982 -----------------------------
10983 -- Check_Initialized_Types --
10984 -----------------------------
10985
10986 procedure Check_Initialized_Types is
10987 Decl : Node_Id;
10988 Formal : Entity_Id;
10989 Actual : Entity_Id;
10990 Uninit_Var : Entity_Id;
10991
10992 begin
10993 Decl := First (Generic_Formal_Declarations (Gen_Decl));
10994 while Present (Decl) loop
10995 Uninit_Var := Empty;
10996
10997 if Nkind (Decl) = N_Private_Extension_Declaration then
10998 Uninit_Var := Uninitialized_Variable (Decl);
10999
11000 elsif Nkind (Decl) = N_Formal_Type_Declaration
11001 and then Nkind (Formal_Type_Definition (Decl)) =
11002 N_Formal_Private_Type_Definition
11003 then
11004 Uninit_Var :=
11005 Uninitialized_Variable (Formal_Type_Definition (Decl));
11006 end if;
11007
11008 if Present (Uninit_Var) then
11009 Formal := Defining_Identifier (Decl);
11010 Actual := First_Entity (Act_Decl_Id);
11011
11012 -- For each formal there is a subtype declaration that renames
11013 -- the actual and has the same name as the formal. Locate the
11014 -- formal for warning message about uninitialized variables
11015 -- in the generic, for which the actual type should be a fully
11016 -- initialized type.
11017
11018 while Present (Actual) loop
11019 exit when Ekind (Actual) = E_Package
11020 and then Present (Renamed_Object (Actual));
11021
11022 if Chars (Actual) = Chars (Formal)
11023 and then not Is_Scalar_Type (Actual)
11024 and then not Is_Fully_Initialized_Type (Actual)
11025 and then Warn_On_No_Value_Assigned
11026 then
11027 Error_Msg_Node_2 := Formal;
11028 Error_Msg_NE
11029 ("generic unit has uninitialized variable& of "
11030 & "formal private type &?v?", Actual, Uninit_Var);
11031 Error_Msg_NE
11032 ("actual type for& should be fully initialized type?v?",
11033 Actual, Formal);
11034 exit;
11035 end if;
11036
11037 Next_Entity (Actual);
11038 end loop;
11039 end if;
11040
11041 Next (Decl);
11042 end loop;
11043 end Check_Initialized_Types;
11044
11045 -- Local variables
11046
11047 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
11048 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
11049 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
11050 -- Save the Ghost and SPARK mode-related data to restore on exit
11051
11052 Act_Body : Node_Id;
11053 Act_Body_Id : Entity_Id;
11054 Act_Body_Name : Node_Id;
11055 Gen_Body : Node_Id;
11056 Gen_Body_Id : Node_Id;
11057 Par_Ent : Entity_Id := Empty;
11058 Par_Vis : Boolean := False;
11059 Parent_Installed : Boolean := False;
11060
11061 Vis_Prims_List : Elist_Id := No_Elist;
11062 -- List of primitives made temporarily visible in the instantiation
11063 -- to match the visibility of the formal type.
11064
11065 -- Start of processing for Instantiate_Package_Body
11066
11067 begin
11068 Gen_Body_Id := Corresponding_Body (Gen_Decl);
11069
11070 -- The instance body may already have been processed, as the parent of
11071 -- another instance that is inlined (Load_Parent_Of_Generic).
11072
11073 if Present (Corresponding_Body (Instance_Spec (Inst_Node))) then
11074 return;
11075 end if;
11076
11077 -- The package being instantiated may be subject to pragma Ghost. Set
11078 -- the mode now to ensure that any nodes generated during instantiation
11079 -- are properly marked as Ghost.
11080
11081 Set_Ghost_Mode (Act_Decl_Id);
11082
11083 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
11084
11085 -- Re-establish the state of information on which checks are suppressed.
11086 -- This information was set in Body_Info at the point of instantiation,
11087 -- and now we restore it so that the instance is compiled using the
11088 -- check status at the instantiation (RM 11.5(7.2/2), AI95-00224-01).
11089
11090 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
11091 Scope_Suppress := Body_Info.Scope_Suppress;
11092 Opt.Ada_Version := Body_Info.Version;
11093 Opt.Ada_Version_Pragma := Body_Info.Version_Pragma;
11094 Restore_Warnings (Body_Info.Warnings);
11095
11096 -- Install the SPARK mode which applies to the package body
11097
11098 Install_SPARK_Mode (Body_Info.SPARK_Mode, Body_Info.SPARK_Mode_Pragma);
11099
11100 if No (Gen_Body_Id) then
11101
11102 -- Do not look for parent of generic body if none is required.
11103 -- This may happen when the routine is called as part of the
11104 -- Pending_Instantiations processing, when nested instances
11105 -- may precede the one generated from the main unit.
11106
11107 if not Unit_Requires_Body (Defining_Entity (Gen_Decl))
11108 and then Body_Optional
11109 then
11110 goto Leave;
11111 else
11112 Load_Parent_Of_Generic
11113 (Inst_Node, Specification (Gen_Decl), Body_Optional);
11114 Gen_Body_Id := Corresponding_Body (Gen_Decl);
11115 end if;
11116 end if;
11117
11118 -- Establish global variable for sloc adjustment and for error recovery
11119 -- In the case of an instance body for an instantiation with actuals
11120 -- from a limited view, the instance body is placed at the beginning
11121 -- of the enclosing package body: use the body entity as the source
11122 -- location for nodes of the instance body.
11123
11124 if not Is_Empty_Elmt_List (Incomplete_Actuals (Act_Decl_Id)) then
11125 declare
11126 Scop : constant Entity_Id := Scope (Act_Decl_Id);
11127 Body_Id : constant Node_Id :=
11128 Corresponding_Body (Unit_Declaration_Node (Scop));
11129
11130 begin
11131 Instantiation_Node := Body_Id;
11132 end;
11133 else
11134 Instantiation_Node := Inst_Node;
11135 end if;
11136
11137 if Present (Gen_Body_Id) then
11138 Save_Env (Gen_Unit, Act_Decl_Id);
11139 Style_Check := False;
11140
11141 -- If the context of the instance is subject to SPARK_Mode "off", the
11142 -- annotation is missing, or the body is instantiated at a later pass
11143 -- and its spec ignored SPARK_Mode pragma, set the global flag which
11144 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within the
11145 -- instance.
11146
11147 if SPARK_Mode /= On
11148 or else Ignore_SPARK_Mode_Pragmas (Act_Decl_Id)
11149 then
11150 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
11151 end if;
11152
11153 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
11154 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
11155
11156 Create_Instantiation_Source
11157 (Inst_Node, Gen_Body_Id, S_Adjustment);
11158
11159 Act_Body :=
11160 Copy_Generic_Node
11161 (Original_Node (Gen_Body), Empty, Instantiating => True);
11162
11163 -- Create proper (possibly qualified) defining name for the body, to
11164 -- correspond to the one in the spec.
11165
11166 Act_Body_Id :=
11167 Make_Defining_Identifier (Sloc (Act_Decl_Id), Chars (Act_Decl_Id));
11168 Set_Comes_From_Source (Act_Body_Id, Comes_From_Source (Act_Decl_Id));
11169
11170 -- Some attributes of spec entity are not inherited by body entity
11171
11172 Set_Handler_Records (Act_Body_Id, No_List);
11173
11174 if Nkind (Defining_Unit_Name (Act_Spec)) =
11175 N_Defining_Program_Unit_Name
11176 then
11177 Act_Body_Name :=
11178 Make_Defining_Program_Unit_Name (Loc,
11179 Name =>
11180 New_Copy_Tree (Name (Defining_Unit_Name (Act_Spec))),
11181 Defining_Identifier => Act_Body_Id);
11182 else
11183 Act_Body_Name := Act_Body_Id;
11184 end if;
11185
11186 Set_Defining_Unit_Name (Act_Body, Act_Body_Name);
11187
11188 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
11189 Check_Generic_Actuals (Act_Decl_Id, False);
11190 Check_Initialized_Types;
11191
11192 -- Install primitives hidden at the point of the instantiation but
11193 -- visible when processing the generic formals
11194
11195 declare
11196 E : Entity_Id;
11197
11198 begin
11199 E := First_Entity (Act_Decl_Id);
11200 while Present (E) loop
11201 if Is_Type (E)
11202 and then not Is_Itype (E)
11203 and then Is_Generic_Actual_Type (E)
11204 and then Is_Tagged_Type (E)
11205 then
11206 Install_Hidden_Primitives
11207 (Prims_List => Vis_Prims_List,
11208 Gen_T => Generic_Parent_Type (Parent (E)),
11209 Act_T => E);
11210 end if;
11211
11212 Next_Entity (E);
11213 end loop;
11214 end;
11215
11216 -- If it is a child unit, make the parent instance (which is an
11217 -- instance of the parent of the generic) visible. The parent
11218 -- instance is the prefix of the name of the generic unit.
11219
11220 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
11221 and then Nkind (Gen_Id) = N_Expanded_Name
11222 then
11223 Par_Ent := Entity (Prefix (Gen_Id));
11224 Par_Vis := Is_Immediately_Visible (Par_Ent);
11225 Install_Parent (Par_Ent, In_Body => True);
11226 Parent_Installed := True;
11227
11228 elsif Is_Child_Unit (Gen_Unit) then
11229 Par_Ent := Scope (Gen_Unit);
11230 Par_Vis := Is_Immediately_Visible (Par_Ent);
11231 Install_Parent (Par_Ent, In_Body => True);
11232 Parent_Installed := True;
11233 end if;
11234
11235 -- If the instantiation is a library unit, and this is the main unit,
11236 -- then build the resulting compilation unit nodes for the instance.
11237 -- If this is a compilation unit but it is not the main unit, then it
11238 -- is the body of a unit in the context, that is being compiled
11239 -- because it is encloses some inlined unit or another generic unit
11240 -- being instantiated. In that case, this body is not part of the
11241 -- current compilation, and is not attached to the tree, but its
11242 -- parent must be set for analysis.
11243
11244 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
11245
11246 -- Replace instance node with body of instance, and create new
11247 -- node for corresponding instance declaration.
11248
11249 Build_Instance_Compilation_Unit_Nodes
11250 (Inst_Node, Act_Body, Act_Decl);
11251 Analyze (Inst_Node);
11252
11253 if Parent (Inst_Node) = Cunit (Main_Unit) then
11254
11255 -- If the instance is a child unit itself, then set the scope
11256 -- of the expanded body to be the parent of the instantiation
11257 -- (ensuring that the fully qualified name will be generated
11258 -- for the elaboration subprogram).
11259
11260 if Nkind (Defining_Unit_Name (Act_Spec)) =
11261 N_Defining_Program_Unit_Name
11262 then
11263 Set_Scope (Defining_Entity (Inst_Node), Scope (Act_Decl_Id));
11264 end if;
11265 end if;
11266
11267 -- Case where instantiation is not a library unit
11268
11269 else
11270 -- If this is an early instantiation, i.e. appears textually
11271 -- before the corresponding body and must be elaborated first,
11272 -- indicate that the body instance is to be delayed.
11273
11274 Install_Body (Act_Body, Inst_Node, Gen_Body, Gen_Decl);
11275
11276 -- Now analyze the body. We turn off all checks if this is an
11277 -- internal unit, since there is no reason to have checks on for
11278 -- any predefined run-time library code. All such code is designed
11279 -- to be compiled with checks off.
11280
11281 -- Note that we do NOT apply this criterion to children of GNAT
11282 -- The latter units must suppress checks explicitly if needed.
11283
11284 -- We also do not suppress checks in CodePeer mode where we are
11285 -- interested in finding possible runtime errors.
11286
11287 if not CodePeer_Mode
11288 and then In_Predefined_Unit (Gen_Decl)
11289 then
11290 Analyze (Act_Body, Suppress => All_Checks);
11291 else
11292 Analyze (Act_Body);
11293 end if;
11294 end if;
11295
11296 Inherit_Context (Gen_Body, Inst_Node);
11297
11298 -- Remove the parent instances if they have been placed on the scope
11299 -- stack to compile the body.
11300
11301 if Parent_Installed then
11302 Remove_Parent (In_Body => True);
11303
11304 -- Restore the previous visibility of the parent
11305
11306 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
11307 end if;
11308
11309 Restore_Hidden_Primitives (Vis_Prims_List);
11310 Restore_Private_Views (Act_Decl_Id);
11311
11312 -- Remove the current unit from visibility if this is an instance
11313 -- that is not elaborated on the fly for inlining purposes.
11314
11315 if not Inlined_Body then
11316 Set_Is_Immediately_Visible (Act_Decl_Id, False);
11317 end if;
11318
11319 Restore_Env;
11320
11321 -- If we have no body, and the unit requires a body, then complain. This
11322 -- complaint is suppressed if we have detected other errors (since a
11323 -- common reason for missing the body is that it had errors).
11324 -- In CodePeer mode, a warning has been emitted already, no need for
11325 -- further messages.
11326
11327 elsif Unit_Requires_Body (Gen_Unit)
11328 and then not Body_Optional
11329 then
11330 if CodePeer_Mode then
11331 null;
11332
11333 elsif Serious_Errors_Detected = 0 then
11334 Error_Msg_NE
11335 ("cannot find body of generic package &", Inst_Node, Gen_Unit);
11336
11337 -- Don't attempt to perform any cleanup actions if some other error
11338 -- was already detected, since this can cause blowups.
11339
11340 else
11341 goto Leave;
11342 end if;
11343
11344 -- Case of package that does not need a body
11345
11346 else
11347 -- If the instantiation of the declaration is a library unit, rewrite
11348 -- the original package instantiation as a package declaration in the
11349 -- compilation unit node.
11350
11351 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
11352 Set_Parent_Spec (Act_Decl, Parent_Spec (Inst_Node));
11353 Rewrite (Inst_Node, Act_Decl);
11354
11355 -- Generate elaboration entity, in case spec has elaboration code.
11356 -- This cannot be done when the instance is analyzed, because it
11357 -- is not known yet whether the body exists.
11358
11359 Set_Elaboration_Entity_Required (Act_Decl_Id, False);
11360 Build_Elaboration_Entity (Parent (Inst_Node), Act_Decl_Id);
11361
11362 -- If the instantiation is not a library unit, then append the
11363 -- declaration to the list of implicitly generated entities, unless
11364 -- it is already a list member which means that it was already
11365 -- processed
11366
11367 elsif not Is_List_Member (Act_Decl) then
11368 Mark_Rewrite_Insertion (Act_Decl);
11369 Insert_Before (Inst_Node, Act_Decl);
11370 end if;
11371 end if;
11372
11373 Expander_Mode_Restore;
11374
11375 <<Leave>>
11376 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
11377 Restore_Ghost_Mode (Saved_GM);
11378 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
11379 Style_Check := Saved_Style_Check;
11380 end Instantiate_Package_Body;
11381
11382 ---------------------------------
11383 -- Instantiate_Subprogram_Body --
11384 ---------------------------------
11385
11386 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
11387 -- must be replaced by gotos which jump to the end of the routine in order
11388 -- to restore the Ghost and SPARK modes.
11389
11390 procedure Instantiate_Subprogram_Body
11391 (Body_Info : Pending_Body_Info;
11392 Body_Optional : Boolean := False)
11393 is
11394 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
11395 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Decl);
11396 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
11397 Gen_Id : constant Node_Id := Name (Inst_Node);
11398 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
11399 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
11400 Loc : constant Source_Ptr := Sloc (Inst_Node);
11401 Pack_Id : constant Entity_Id :=
11402 Defining_Unit_Name (Parent (Act_Decl));
11403
11404 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
11405 Saved_ISMP : constant Boolean :=
11406 Ignore_SPARK_Mode_Pragmas_In_Instance;
11407 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
11408 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
11409 -- Save the Ghost and SPARK mode-related data to restore on exit
11410
11411 Saved_Style_Check : constant Boolean := Style_Check;
11412 Saved_Warnings : constant Warning_Record := Save_Warnings;
11413
11414 Act_Body : Node_Id;
11415 Act_Body_Id : Entity_Id;
11416 Gen_Body : Node_Id;
11417 Gen_Body_Id : Node_Id;
11418 Pack_Body : Node_Id;
11419 Par_Ent : Entity_Id := Empty;
11420 Par_Vis : Boolean := False;
11421 Ret_Expr : Node_Id;
11422
11423 Parent_Installed : Boolean := False;
11424
11425 begin
11426 Gen_Body_Id := Corresponding_Body (Gen_Decl);
11427
11428 -- Subprogram body may have been created already because of an inline
11429 -- pragma, or because of multiple elaborations of the enclosing package
11430 -- when several instances of the subprogram appear in the main unit.
11431
11432 if Present (Corresponding_Body (Act_Decl)) then
11433 return;
11434 end if;
11435
11436 -- The subprogram being instantiated may be subject to pragma Ghost. Set
11437 -- the mode now to ensure that any nodes generated during instantiation
11438 -- are properly marked as Ghost.
11439
11440 Set_Ghost_Mode (Act_Decl_Id);
11441
11442 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
11443
11444 -- Re-establish the state of information on which checks are suppressed.
11445 -- This information was set in Body_Info at the point of instantiation,
11446 -- and now we restore it so that the instance is compiled using the
11447 -- check status at the instantiation (RM 11.5(7.2/2), AI95-00224-01).
11448
11449 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
11450 Scope_Suppress := Body_Info.Scope_Suppress;
11451 Opt.Ada_Version := Body_Info.Version;
11452 Opt.Ada_Version_Pragma := Body_Info.Version_Pragma;
11453 Restore_Warnings (Body_Info.Warnings);
11454
11455 -- Install the SPARK mode which applies to the subprogram body
11456
11457 Install_SPARK_Mode (Body_Info.SPARK_Mode, Body_Info.SPARK_Mode_Pragma);
11458
11459 if No (Gen_Body_Id) then
11460
11461 -- For imported generic subprogram, no body to compile, complete
11462 -- the spec entity appropriately.
11463
11464 if Is_Imported (Gen_Unit) then
11465 Set_Is_Imported (Act_Decl_Id);
11466 Set_First_Rep_Item (Act_Decl_Id, First_Rep_Item (Gen_Unit));
11467 Set_Interface_Name (Act_Decl_Id, Interface_Name (Gen_Unit));
11468 Set_Convention (Act_Decl_Id, Convention (Gen_Unit));
11469 Set_Has_Completion (Act_Decl_Id);
11470 goto Leave;
11471
11472 -- For other cases, compile the body
11473
11474 else
11475 Load_Parent_Of_Generic
11476 (Inst_Node, Specification (Gen_Decl), Body_Optional);
11477 Gen_Body_Id := Corresponding_Body (Gen_Decl);
11478 end if;
11479 end if;
11480
11481 Instantiation_Node := Inst_Node;
11482
11483 if Present (Gen_Body_Id) then
11484 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
11485
11486 if Nkind (Gen_Body) = N_Subprogram_Body_Stub then
11487
11488 -- Either body is not present, or context is non-expanding, as
11489 -- when compiling a subunit. Mark the instance as completed, and
11490 -- diagnose a missing body when needed.
11491
11492 if Expander_Active
11493 and then Operating_Mode = Generate_Code
11494 then
11495 Error_Msg_N ("missing proper body for instantiation", Gen_Body);
11496 end if;
11497
11498 Set_Has_Completion (Act_Decl_Id);
11499 goto Leave;
11500 end if;
11501
11502 Save_Env (Gen_Unit, Act_Decl_Id);
11503 Style_Check := False;
11504
11505 -- If the context of the instance is subject to SPARK_Mode "off", the
11506 -- annotation is missing, or the body is instantiated at a later pass
11507 -- and its spec ignored SPARK_Mode pragma, set the global flag which
11508 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within the
11509 -- instance.
11510
11511 if SPARK_Mode /= On
11512 or else Ignore_SPARK_Mode_Pragmas (Act_Decl_Id)
11513 then
11514 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
11515 end if;
11516
11517 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
11518 Create_Instantiation_Source
11519 (Inst_Node,
11520 Gen_Body_Id,
11521 S_Adjustment);
11522
11523 Act_Body :=
11524 Copy_Generic_Node
11525 (Original_Node (Gen_Body), Empty, Instantiating => True);
11526
11527 -- Create proper defining name for the body, to correspond to the one
11528 -- in the spec.
11529
11530 Act_Body_Id :=
11531 Make_Defining_Identifier (Sloc (Act_Decl_Id), Chars (Act_Decl_Id));
11532
11533 Set_Comes_From_Source (Act_Body_Id, Comes_From_Source (Act_Decl_Id));
11534 Set_Defining_Unit_Name (Specification (Act_Body), Act_Body_Id);
11535
11536 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
11537 Set_Has_Completion (Act_Decl_Id);
11538 Check_Generic_Actuals (Pack_Id, False);
11539
11540 -- Generate a reference to link the visible subprogram instance to
11541 -- the generic body, which for navigation purposes is the only
11542 -- available source for the instance.
11543
11544 Generate_Reference
11545 (Related_Instance (Pack_Id),
11546 Gen_Body_Id, 'b', Set_Ref => False, Force => True);
11547
11548 -- If it is a child unit, make the parent instance (which is an
11549 -- instance of the parent of the generic) visible. The parent
11550 -- instance is the prefix of the name of the generic unit.
11551
11552 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
11553 and then Nkind (Gen_Id) = N_Expanded_Name
11554 then
11555 Par_Ent := Entity (Prefix (Gen_Id));
11556 Par_Vis := Is_Immediately_Visible (Par_Ent);
11557 Install_Parent (Par_Ent, In_Body => True);
11558 Parent_Installed := True;
11559
11560 elsif Is_Child_Unit (Gen_Unit) then
11561 Par_Ent := Scope (Gen_Unit);
11562 Par_Vis := Is_Immediately_Visible (Par_Ent);
11563 Install_Parent (Par_Ent, In_Body => True);
11564 Parent_Installed := True;
11565 end if;
11566
11567 -- Subprogram body is placed in the body of wrapper package,
11568 -- whose spec contains the subprogram declaration as well as
11569 -- the renaming declarations for the generic parameters.
11570
11571 Pack_Body :=
11572 Make_Package_Body (Loc,
11573 Defining_Unit_Name => New_Copy (Pack_Id),
11574 Declarations => New_List (Act_Body));
11575
11576 Set_Corresponding_Spec (Pack_Body, Pack_Id);
11577
11578 -- If the instantiation is a library unit, then build resulting
11579 -- compilation unit nodes for the instance. The declaration of
11580 -- the enclosing package is the grandparent of the subprogram
11581 -- declaration. First replace the instantiation node as the unit
11582 -- of the corresponding compilation.
11583
11584 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
11585 if Parent (Inst_Node) = Cunit (Main_Unit) then
11586 Set_Unit (Parent (Inst_Node), Inst_Node);
11587 Build_Instance_Compilation_Unit_Nodes
11588 (Inst_Node, Pack_Body, Parent (Parent (Act_Decl)));
11589 Analyze (Inst_Node);
11590 else
11591 Set_Parent (Pack_Body, Parent (Inst_Node));
11592 Analyze (Pack_Body);
11593 end if;
11594
11595 else
11596 Insert_Before (Inst_Node, Pack_Body);
11597 Mark_Rewrite_Insertion (Pack_Body);
11598 Analyze (Pack_Body);
11599
11600 if Expander_Active then
11601 Freeze_Subprogram_Body (Inst_Node, Gen_Body, Pack_Id);
11602 end if;
11603 end if;
11604
11605 Inherit_Context (Gen_Body, Inst_Node);
11606
11607 Restore_Private_Views (Pack_Id, False);
11608
11609 if Parent_Installed then
11610 Remove_Parent (In_Body => True);
11611
11612 -- Restore the previous visibility of the parent
11613
11614 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
11615 end if;
11616
11617 Restore_Env;
11618 Restore_Warnings (Saved_Warnings);
11619
11620 -- Body not found. Error was emitted already. If there were no previous
11621 -- errors, this may be an instance whose scope is a premature instance.
11622 -- In that case we must insure that the (legal) program does raise
11623 -- program error if executed. We generate a subprogram body for this
11624 -- purpose. See DEC ac30vso.
11625
11626 -- Should not reference proprietary DEC tests in comments ???
11627
11628 elsif Serious_Errors_Detected = 0
11629 and then Nkind (Parent (Inst_Node)) /= N_Compilation_Unit
11630 then
11631 if Body_Optional then
11632 goto Leave;
11633
11634 elsif Ekind (Act_Decl_Id) = E_Procedure then
11635 Act_Body :=
11636 Make_Subprogram_Body (Loc,
11637 Specification =>
11638 Make_Procedure_Specification (Loc,
11639 Defining_Unit_Name =>
11640 Make_Defining_Identifier (Loc, Chars (Act_Decl_Id)),
11641 Parameter_Specifications =>
11642 New_Copy_List
11643 (Parameter_Specifications (Parent (Act_Decl_Id)))),
11644
11645 Declarations => Empty_List,
11646 Handled_Statement_Sequence =>
11647 Make_Handled_Sequence_Of_Statements (Loc,
11648 Statements => New_List (
11649 Make_Raise_Program_Error (Loc,
11650 Reason => PE_Access_Before_Elaboration))));
11651
11652 else
11653 Ret_Expr :=
11654 Make_Raise_Program_Error (Loc,
11655 Reason => PE_Access_Before_Elaboration);
11656
11657 Set_Etype (Ret_Expr, (Etype (Act_Decl_Id)));
11658 Set_Analyzed (Ret_Expr);
11659
11660 Act_Body :=
11661 Make_Subprogram_Body (Loc,
11662 Specification =>
11663 Make_Function_Specification (Loc,
11664 Defining_Unit_Name =>
11665 Make_Defining_Identifier (Loc, Chars (Act_Decl_Id)),
11666 Parameter_Specifications =>
11667 New_Copy_List
11668 (Parameter_Specifications (Parent (Act_Decl_Id))),
11669 Result_Definition =>
11670 New_Occurrence_Of (Etype (Act_Decl_Id), Loc)),
11671
11672 Declarations => Empty_List,
11673 Handled_Statement_Sequence =>
11674 Make_Handled_Sequence_Of_Statements (Loc,
11675 Statements => New_List (
11676 Make_Simple_Return_Statement (Loc, Ret_Expr))));
11677 end if;
11678
11679 Pack_Body :=
11680 Make_Package_Body (Loc,
11681 Defining_Unit_Name => New_Copy (Pack_Id),
11682 Declarations => New_List (Act_Body));
11683
11684 Insert_After (Inst_Node, Pack_Body);
11685 Set_Corresponding_Spec (Pack_Body, Pack_Id);
11686 Analyze (Pack_Body);
11687 end if;
11688
11689 Expander_Mode_Restore;
11690
11691 <<Leave>>
11692 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
11693 Restore_Ghost_Mode (Saved_GM);
11694 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
11695 Style_Check := Saved_Style_Check;
11696 end Instantiate_Subprogram_Body;
11697
11698 ----------------------
11699 -- Instantiate_Type --
11700 ----------------------
11701
11702 function Instantiate_Type
11703 (Formal : Node_Id;
11704 Actual : Node_Id;
11705 Analyzed_Formal : Node_Id;
11706 Actual_Decls : List_Id) return List_Id
11707 is
11708 A_Gen_T : constant Entity_Id :=
11709 Defining_Identifier (Analyzed_Formal);
11710 Def : constant Node_Id := Formal_Type_Definition (Formal);
11711 Gen_T : constant Entity_Id := Defining_Identifier (Formal);
11712 Act_T : Entity_Id;
11713 Ancestor : Entity_Id := Empty;
11714 Decl_Node : Node_Id;
11715 Decl_Nodes : List_Id;
11716 Loc : Source_Ptr;
11717 Subt : Entity_Id;
11718
11719 procedure Diagnose_Predicated_Actual;
11720 -- There are a number of constructs in which a discrete type with
11721 -- predicates is illegal, e.g. as an index in an array type declaration.
11722 -- If a generic type is used is such a construct in a generic package
11723 -- declaration, it carries the flag No_Predicate_On_Actual. it is part
11724 -- of the generic contract that the actual cannot have predicates.
11725
11726 procedure Validate_Array_Type_Instance;
11727 procedure Validate_Access_Subprogram_Instance;
11728 procedure Validate_Access_Type_Instance;
11729 procedure Validate_Derived_Type_Instance;
11730 procedure Validate_Derived_Interface_Type_Instance;
11731 procedure Validate_Discriminated_Formal_Type;
11732 procedure Validate_Interface_Type_Instance;
11733 procedure Validate_Private_Type_Instance;
11734 procedure Validate_Incomplete_Type_Instance;
11735 -- These procedures perform validation tests for the named case.
11736 -- Validate_Discriminated_Formal_Type is shared by formal private
11737 -- types and Ada 2012 formal incomplete types.
11738
11739 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean;
11740 -- Check that base types are the same and that the subtypes match
11741 -- statically. Used in several of the above.
11742
11743 ---------------------------------
11744 -- Diagnose_Predicated_Actual --
11745 ---------------------------------
11746
11747 procedure Diagnose_Predicated_Actual is
11748 begin
11749 if No_Predicate_On_Actual (A_Gen_T)
11750 and then Has_Predicates (Act_T)
11751 then
11752 Error_Msg_NE
11753 ("actual for& cannot be a type with predicate",
11754 Instantiation_Node, A_Gen_T);
11755
11756 elsif No_Dynamic_Predicate_On_Actual (A_Gen_T)
11757 and then Has_Predicates (Act_T)
11758 and then not Has_Static_Predicate_Aspect (Act_T)
11759 then
11760 Error_Msg_NE
11761 ("actual for& cannot be a type with a dynamic predicate",
11762 Instantiation_Node, A_Gen_T);
11763 end if;
11764 end Diagnose_Predicated_Actual;
11765
11766 --------------------
11767 -- Subtypes_Match --
11768 --------------------
11769
11770 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean is
11771 T : constant Entity_Id := Get_Instance_Of (Gen_T);
11772
11773 begin
11774 -- Some detailed comments would be useful here ???
11775
11776 return ((Base_Type (T) = Act_T
11777 or else Base_Type (T) = Base_Type (Act_T))
11778 and then Subtypes_Statically_Match (T, Act_T))
11779
11780 or else (Is_Class_Wide_Type (Gen_T)
11781 and then Is_Class_Wide_Type (Act_T)
11782 and then Subtypes_Match
11783 (Get_Instance_Of (Root_Type (Gen_T)),
11784 Root_Type (Act_T)))
11785
11786 or else
11787 (Ekind_In (Gen_T, E_Anonymous_Access_Subprogram_Type,
11788 E_Anonymous_Access_Type)
11789 and then Ekind (Act_T) = Ekind (Gen_T)
11790 and then Subtypes_Statically_Match
11791 (Designated_Type (Gen_T), Designated_Type (Act_T)));
11792 end Subtypes_Match;
11793
11794 -----------------------------------------
11795 -- Validate_Access_Subprogram_Instance --
11796 -----------------------------------------
11797
11798 procedure Validate_Access_Subprogram_Instance is
11799 begin
11800 if not Is_Access_Type (Act_T)
11801 or else Ekind (Designated_Type (Act_T)) /= E_Subprogram_Type
11802 then
11803 Error_Msg_NE
11804 ("expect access type in instantiation of &", Actual, Gen_T);
11805 Abandon_Instantiation (Actual);
11806 end if;
11807
11808 -- According to AI05-288, actuals for access_to_subprograms must be
11809 -- subtype conformant with the generic formal. Previous to AI05-288
11810 -- only mode conformance was required.
11811
11812 -- This is a binding interpretation that applies to previous versions
11813 -- of the language, no need to maintain previous weaker checks.
11814
11815 Check_Subtype_Conformant
11816 (Designated_Type (Act_T),
11817 Designated_Type (A_Gen_T),
11818 Actual,
11819 Get_Inst => True);
11820
11821 if Ekind (Base_Type (Act_T)) = E_Access_Protected_Subprogram_Type then
11822 if Ekind (A_Gen_T) = E_Access_Subprogram_Type then
11823 Error_Msg_NE
11824 ("protected access type not allowed for formal &",
11825 Actual, Gen_T);
11826 end if;
11827
11828 elsif Ekind (A_Gen_T) = E_Access_Protected_Subprogram_Type then
11829 Error_Msg_NE
11830 ("expect protected access type for formal &",
11831 Actual, Gen_T);
11832 end if;
11833
11834 -- If the formal has a specified convention (which in most cases
11835 -- will be StdCall) verify that the actual has the same convention.
11836
11837 if Has_Convention_Pragma (A_Gen_T)
11838 and then Convention (A_Gen_T) /= Convention (Act_T)
11839 then
11840 Error_Msg_Name_1 := Get_Convention_Name (Convention (A_Gen_T));
11841 Error_Msg_NE
11842 ("actual for formal & must have convention %", Actual, Gen_T);
11843 end if;
11844 end Validate_Access_Subprogram_Instance;
11845
11846 -----------------------------------
11847 -- Validate_Access_Type_Instance --
11848 -----------------------------------
11849
11850 procedure Validate_Access_Type_Instance is
11851 Desig_Type : constant Entity_Id :=
11852 Find_Actual_Type (Designated_Type (A_Gen_T), A_Gen_T);
11853 Desig_Act : Entity_Id;
11854
11855 begin
11856 if not Is_Access_Type (Act_T) then
11857 Error_Msg_NE
11858 ("expect access type in instantiation of &", Actual, Gen_T);
11859 Abandon_Instantiation (Actual);
11860 end if;
11861
11862 if Is_Access_Constant (A_Gen_T) then
11863 if not Is_Access_Constant (Act_T) then
11864 Error_Msg_N
11865 ("actual type must be access-to-constant type", Actual);
11866 Abandon_Instantiation (Actual);
11867 end if;
11868 else
11869 if Is_Access_Constant (Act_T) then
11870 Error_Msg_N
11871 ("actual type must be access-to-variable type", Actual);
11872 Abandon_Instantiation (Actual);
11873
11874 elsif Ekind (A_Gen_T) = E_General_Access_Type
11875 and then Ekind (Base_Type (Act_T)) /= E_General_Access_Type
11876 then
11877 Error_Msg_N -- CODEFIX
11878 ("actual must be general access type!", Actual);
11879 Error_Msg_NE -- CODEFIX
11880 ("add ALL to }!", Actual, Act_T);
11881 Abandon_Instantiation (Actual);
11882 end if;
11883 end if;
11884
11885 -- The designated subtypes, that is to say the subtypes introduced
11886 -- by an access type declaration (and not by a subtype declaration)
11887 -- must match.
11888
11889 Desig_Act := Designated_Type (Base_Type (Act_T));
11890
11891 -- The designated type may have been introduced through a limited_
11892 -- with clause, in which case retrieve the non-limited view. This
11893 -- applies to incomplete types as well as to class-wide types.
11894
11895 if From_Limited_With (Desig_Act) then
11896 Desig_Act := Available_View (Desig_Act);
11897 end if;
11898
11899 if not Subtypes_Match (Desig_Type, Desig_Act) then
11900 Error_Msg_NE
11901 ("designated type of actual does not match that of formal &",
11902 Actual, Gen_T);
11903
11904 if not Predicates_Match (Desig_Type, Desig_Act) then
11905 Error_Msg_N ("\predicates do not match", Actual);
11906 end if;
11907
11908 Abandon_Instantiation (Actual);
11909
11910 elsif Is_Access_Type (Designated_Type (Act_T))
11911 and then Is_Constrained (Designated_Type (Designated_Type (Act_T)))
11912 /=
11913 Is_Constrained (Designated_Type (Desig_Type))
11914 then
11915 Error_Msg_NE
11916 ("designated type of actual does not match that of formal &",
11917 Actual, Gen_T);
11918
11919 if not Predicates_Match (Desig_Type, Desig_Act) then
11920 Error_Msg_N ("\predicates do not match", Actual);
11921 end if;
11922
11923 Abandon_Instantiation (Actual);
11924 end if;
11925
11926 -- Ada 2005: null-exclusion indicators of the two types must agree
11927
11928 if Can_Never_Be_Null (A_Gen_T) /= Can_Never_Be_Null (Act_T) then
11929 Error_Msg_NE
11930 ("non null exclusion of actual and formal & do not match",
11931 Actual, Gen_T);
11932 end if;
11933 end Validate_Access_Type_Instance;
11934
11935 ----------------------------------
11936 -- Validate_Array_Type_Instance --
11937 ----------------------------------
11938
11939 procedure Validate_Array_Type_Instance is
11940 I1 : Node_Id;
11941 I2 : Node_Id;
11942 T2 : Entity_Id;
11943
11944 function Formal_Dimensions return Nat;
11945 -- Count number of dimensions in array type formal
11946
11947 -----------------------
11948 -- Formal_Dimensions --
11949 -----------------------
11950
11951 function Formal_Dimensions return Nat is
11952 Num : Nat := 0;
11953 Index : Node_Id;
11954
11955 begin
11956 if Nkind (Def) = N_Constrained_Array_Definition then
11957 Index := First (Discrete_Subtype_Definitions (Def));
11958 else
11959 Index := First (Subtype_Marks (Def));
11960 end if;
11961
11962 while Present (Index) loop
11963 Num := Num + 1;
11964 Next_Index (Index);
11965 end loop;
11966
11967 return Num;
11968 end Formal_Dimensions;
11969
11970 -- Start of processing for Validate_Array_Type_Instance
11971
11972 begin
11973 if not Is_Array_Type (Act_T) then
11974 Error_Msg_NE
11975 ("expect array type in instantiation of &", Actual, Gen_T);
11976 Abandon_Instantiation (Actual);
11977
11978 elsif Nkind (Def) = N_Constrained_Array_Definition then
11979 if not (Is_Constrained (Act_T)) then
11980 Error_Msg_NE
11981 ("expect constrained array in instantiation of &",
11982 Actual, Gen_T);
11983 Abandon_Instantiation (Actual);
11984 end if;
11985
11986 else
11987 if Is_Constrained (Act_T) then
11988 Error_Msg_NE
11989 ("expect unconstrained array in instantiation of &",
11990 Actual, Gen_T);
11991 Abandon_Instantiation (Actual);
11992 end if;
11993 end if;
11994
11995 if Formal_Dimensions /= Number_Dimensions (Act_T) then
11996 Error_Msg_NE
11997 ("dimensions of actual do not match formal &", Actual, Gen_T);
11998 Abandon_Instantiation (Actual);
11999 end if;
12000
12001 I1 := First_Index (A_Gen_T);
12002 I2 := First_Index (Act_T);
12003 for J in 1 .. Formal_Dimensions loop
12004
12005 -- If the indexes of the actual were given by a subtype_mark,
12006 -- the index was transformed into a range attribute. Retrieve
12007 -- the original type mark for checking.
12008
12009 if Is_Entity_Name (Original_Node (I2)) then
12010 T2 := Entity (Original_Node (I2));
12011 else
12012 T2 := Etype (I2);
12013 end if;
12014
12015 if not Subtypes_Match
12016 (Find_Actual_Type (Etype (I1), A_Gen_T), T2)
12017 then
12018 Error_Msg_NE
12019 ("index types of actual do not match those of formal &",
12020 Actual, Gen_T);
12021 Abandon_Instantiation (Actual);
12022 end if;
12023
12024 Next_Index (I1);
12025 Next_Index (I2);
12026 end loop;
12027
12028 -- Check matching subtypes. Note that there are complex visibility
12029 -- issues when the generic is a child unit and some aspect of the
12030 -- generic type is declared in a parent unit of the generic. We do
12031 -- the test to handle this special case only after a direct check
12032 -- for static matching has failed. The case where both the component
12033 -- type and the array type are separate formals, and the component
12034 -- type is a private view may also require special checking in
12035 -- Subtypes_Match.
12036
12037 if Subtypes_Match
12038 (Component_Type (A_Gen_T), Component_Type (Act_T))
12039 or else
12040 Subtypes_Match
12041 (Find_Actual_Type (Component_Type (A_Gen_T), A_Gen_T),
12042 Component_Type (Act_T))
12043 then
12044 null;
12045 else
12046 Error_Msg_NE
12047 ("component subtype of actual does not match that of formal &",
12048 Actual, Gen_T);
12049 Abandon_Instantiation (Actual);
12050 end if;
12051
12052 if Has_Aliased_Components (A_Gen_T)
12053 and then not Has_Aliased_Components (Act_T)
12054 then
12055 Error_Msg_NE
12056 ("actual must have aliased components to match formal type &",
12057 Actual, Gen_T);
12058 end if;
12059 end Validate_Array_Type_Instance;
12060
12061 -----------------------------------------------
12062 -- Validate_Derived_Interface_Type_Instance --
12063 -----------------------------------------------
12064
12065 procedure Validate_Derived_Interface_Type_Instance is
12066 Par : constant Entity_Id := Entity (Subtype_Indication (Def));
12067 Elmt : Elmt_Id;
12068
12069 begin
12070 -- First apply interface instance checks
12071
12072 Validate_Interface_Type_Instance;
12073
12074 -- Verify that immediate parent interface is an ancestor of
12075 -- the actual.
12076
12077 if Present (Par)
12078 and then not Interface_Present_In_Ancestor (Act_T, Par)
12079 then
12080 Error_Msg_NE
12081 ("interface actual must include progenitor&", Actual, Par);
12082 end if;
12083
12084 -- Now verify that the actual includes all other ancestors of
12085 -- the formal.
12086
12087 Elmt := First_Elmt (Interfaces (A_Gen_T));
12088 while Present (Elmt) loop
12089 if not Interface_Present_In_Ancestor
12090 (Act_T, Get_Instance_Of (Node (Elmt)))
12091 then
12092 Error_Msg_NE
12093 ("interface actual must include progenitor&",
12094 Actual, Node (Elmt));
12095 end if;
12096
12097 Next_Elmt (Elmt);
12098 end loop;
12099 end Validate_Derived_Interface_Type_Instance;
12100
12101 ------------------------------------
12102 -- Validate_Derived_Type_Instance --
12103 ------------------------------------
12104
12105 procedure Validate_Derived_Type_Instance is
12106 Actual_Discr : Entity_Id;
12107 Ancestor_Discr : Entity_Id;
12108
12109 begin
12110 -- If the parent type in the generic declaration is itself a previous
12111 -- formal type, then it is local to the generic and absent from the
12112 -- analyzed generic definition. In that case the ancestor is the
12113 -- instance of the formal (which must have been instantiated
12114 -- previously), unless the ancestor is itself a formal derived type.
12115 -- In this latter case (which is the subject of Corrigendum 8652/0038
12116 -- (AI-202) the ancestor of the formals is the ancestor of its
12117 -- parent. Otherwise, the analyzed generic carries the parent type.
12118 -- If the parent type is defined in a previous formal package, then
12119 -- the scope of that formal package is that of the generic type
12120 -- itself, and it has already been mapped into the corresponding type
12121 -- in the actual package.
12122
12123 -- Common case: parent type defined outside of the generic
12124
12125 if Is_Entity_Name (Subtype_Mark (Def))
12126 and then Present (Entity (Subtype_Mark (Def)))
12127 then
12128 Ancestor := Get_Instance_Of (Entity (Subtype_Mark (Def)));
12129
12130 -- Check whether parent is defined in a previous formal package
12131
12132 elsif
12133 Scope (Scope (Base_Type (Etype (A_Gen_T)))) = Scope (A_Gen_T)
12134 then
12135 Ancestor :=
12136 Get_Instance_Of (Base_Type (Etype (A_Gen_T)));
12137
12138 -- The type may be a local derivation, or a type extension of a
12139 -- previous formal, or of a formal of a parent package.
12140
12141 elsif Is_Derived_Type (Get_Instance_Of (A_Gen_T))
12142 or else
12143 Ekind (Get_Instance_Of (A_Gen_T)) = E_Record_Type_With_Private
12144 then
12145 -- Check whether the parent is another derived formal type in the
12146 -- same generic unit.
12147
12148 if Etype (A_Gen_T) /= A_Gen_T
12149 and then Is_Generic_Type (Etype (A_Gen_T))
12150 and then Scope (Etype (A_Gen_T)) = Scope (A_Gen_T)
12151 and then Etype (Etype (A_Gen_T)) /= Etype (A_Gen_T)
12152 then
12153 -- Locate ancestor of parent from the subtype declaration
12154 -- created for the actual.
12155
12156 declare
12157 Decl : Node_Id;
12158
12159 begin
12160 Decl := First (Actual_Decls);
12161 while Present (Decl) loop
12162 if Nkind (Decl) = N_Subtype_Declaration
12163 and then Chars (Defining_Identifier (Decl)) =
12164 Chars (Etype (A_Gen_T))
12165 then
12166 Ancestor := Generic_Parent_Type (Decl);
12167 exit;
12168 else
12169 Next (Decl);
12170 end if;
12171 end loop;
12172 end;
12173
12174 pragma Assert (Present (Ancestor));
12175
12176 -- The ancestor itself may be a previous formal that has been
12177 -- instantiated.
12178
12179 Ancestor := Get_Instance_Of (Ancestor);
12180
12181 else
12182 Ancestor :=
12183 Get_Instance_Of (Base_Type (Get_Instance_Of (A_Gen_T)));
12184 end if;
12185
12186 -- Check whether parent is a previous formal of the current generic
12187
12188 elsif Is_Derived_Type (A_Gen_T)
12189 and then Is_Generic_Type (Etype (A_Gen_T))
12190 and then Scope (A_Gen_T) = Scope (Etype (A_Gen_T))
12191 then
12192 Ancestor := Get_Instance_Of (First_Subtype (Etype (A_Gen_T)));
12193
12194 -- An unusual case: the actual is a type declared in a parent unit,
12195 -- but is not a formal type so there is no instance_of for it.
12196 -- Retrieve it by analyzing the record extension.
12197
12198 elsif Is_Child_Unit (Scope (A_Gen_T))
12199 and then In_Open_Scopes (Scope (Act_T))
12200 and then Is_Generic_Instance (Scope (Act_T))
12201 then
12202 Analyze (Subtype_Mark (Def));
12203 Ancestor := Entity (Subtype_Mark (Def));
12204
12205 else
12206 Ancestor := Get_Instance_Of (Etype (Base_Type (A_Gen_T)));
12207 end if;
12208
12209 -- If the formal derived type has pragma Preelaborable_Initialization
12210 -- then the actual type must have preelaborable initialization.
12211
12212 if Known_To_Have_Preelab_Init (A_Gen_T)
12213 and then not Has_Preelaborable_Initialization (Act_T)
12214 then
12215 Error_Msg_NE
12216 ("actual for & must have preelaborable initialization",
12217 Actual, Gen_T);
12218 end if;
12219
12220 -- Ada 2005 (AI-251)
12221
12222 if Ada_Version >= Ada_2005 and then Is_Interface (Ancestor) then
12223 if not Interface_Present_In_Ancestor (Act_T, Ancestor) then
12224 Error_Msg_NE
12225 ("(Ada 2005) expected type implementing & in instantiation",
12226 Actual, Ancestor);
12227 end if;
12228
12229 -- Finally verify that the (instance of) the ancestor is an ancestor
12230 -- of the actual.
12231
12232 elsif not Is_Ancestor (Base_Type (Ancestor), Act_T) then
12233 Error_Msg_NE
12234 ("expect type derived from & in instantiation",
12235 Actual, First_Subtype (Ancestor));
12236 Abandon_Instantiation (Actual);
12237 end if;
12238
12239 -- Ada 2005 (AI-443): Synchronized formal derived type checks. Note
12240 -- that the formal type declaration has been rewritten as a private
12241 -- extension.
12242
12243 if Ada_Version >= Ada_2005
12244 and then Nkind (Parent (A_Gen_T)) = N_Private_Extension_Declaration
12245 and then Synchronized_Present (Parent (A_Gen_T))
12246 then
12247 -- The actual must be a synchronized tagged type
12248
12249 if not Is_Tagged_Type (Act_T) then
12250 Error_Msg_N
12251 ("actual of synchronized type must be tagged", Actual);
12252 Abandon_Instantiation (Actual);
12253
12254 elsif Nkind (Parent (Act_T)) = N_Full_Type_Declaration
12255 and then Nkind (Type_Definition (Parent (Act_T))) =
12256 N_Derived_Type_Definition
12257 and then not Synchronized_Present
12258 (Type_Definition (Parent (Act_T)))
12259 then
12260 Error_Msg_N
12261 ("actual of synchronized type must be synchronized", Actual);
12262 Abandon_Instantiation (Actual);
12263 end if;
12264 end if;
12265
12266 -- Perform atomic/volatile checks (RM C.6(12)). Note that AI05-0218-1
12267 -- removes the second instance of the phrase "or allow pass by copy".
12268
12269 if Is_Atomic (Act_T) and then not Is_Atomic (Ancestor) then
12270 Error_Msg_N
12271 ("cannot have atomic actual type for non-atomic formal type",
12272 Actual);
12273
12274 elsif Is_Volatile (Act_T) and then not Is_Volatile (Ancestor) then
12275 Error_Msg_N
12276 ("cannot have volatile actual type for non-volatile formal type",
12277 Actual);
12278 end if;
12279
12280 -- It should not be necessary to check for unknown discriminants on
12281 -- Formal, but for some reason Has_Unknown_Discriminants is false for
12282 -- A_Gen_T, so Is_Definite_Subtype incorrectly returns True. This
12283 -- needs fixing. ???
12284
12285 if Is_Definite_Subtype (A_Gen_T)
12286 and then not Unknown_Discriminants_Present (Formal)
12287 and then not Is_Definite_Subtype (Act_T)
12288 then
12289 Error_Msg_N ("actual subtype must be constrained", Actual);
12290 Abandon_Instantiation (Actual);
12291 end if;
12292
12293 if not Unknown_Discriminants_Present (Formal) then
12294 if Is_Constrained (Ancestor) then
12295 if not Is_Constrained (Act_T) then
12296 Error_Msg_N ("actual subtype must be constrained", Actual);
12297 Abandon_Instantiation (Actual);
12298 end if;
12299
12300 -- Ancestor is unconstrained, Check if generic formal and actual
12301 -- agree on constrainedness. The check only applies to array types
12302 -- and discriminated types.
12303
12304 elsif Is_Constrained (Act_T) then
12305 if Ekind (Ancestor) = E_Access_Type
12306 or else (not Is_Constrained (A_Gen_T)
12307 and then Is_Composite_Type (A_Gen_T))
12308 then
12309 Error_Msg_N ("actual subtype must be unconstrained", Actual);
12310 Abandon_Instantiation (Actual);
12311 end if;
12312
12313 -- A class-wide type is only allowed if the formal has unknown
12314 -- discriminants.
12315
12316 elsif Is_Class_Wide_Type (Act_T)
12317 and then not Has_Unknown_Discriminants (Ancestor)
12318 then
12319 Error_Msg_NE
12320 ("actual for & cannot be a class-wide type", Actual, Gen_T);
12321 Abandon_Instantiation (Actual);
12322
12323 -- Otherwise, the formal and actual must have the same number
12324 -- of discriminants and each discriminant of the actual must
12325 -- correspond to a discriminant of the formal.
12326
12327 elsif Has_Discriminants (Act_T)
12328 and then not Has_Unknown_Discriminants (Act_T)
12329 and then Has_Discriminants (Ancestor)
12330 then
12331 Actual_Discr := First_Discriminant (Act_T);
12332 Ancestor_Discr := First_Discriminant (Ancestor);
12333 while Present (Actual_Discr)
12334 and then Present (Ancestor_Discr)
12335 loop
12336 if Base_Type (Act_T) /= Base_Type (Ancestor) and then
12337 No (Corresponding_Discriminant (Actual_Discr))
12338 then
12339 Error_Msg_NE
12340 ("discriminant & does not correspond "
12341 & "to ancestor discriminant", Actual, Actual_Discr);
12342 Abandon_Instantiation (Actual);
12343 end if;
12344
12345 Next_Discriminant (Actual_Discr);
12346 Next_Discriminant (Ancestor_Discr);
12347 end loop;
12348
12349 if Present (Actual_Discr) or else Present (Ancestor_Discr) then
12350 Error_Msg_NE
12351 ("actual for & must have same number of discriminants",
12352 Actual, Gen_T);
12353 Abandon_Instantiation (Actual);
12354 end if;
12355
12356 -- This case should be caught by the earlier check for
12357 -- constrainedness, but the check here is added for completeness.
12358
12359 elsif Has_Discriminants (Act_T)
12360 and then not Has_Unknown_Discriminants (Act_T)
12361 then
12362 Error_Msg_NE
12363 ("actual for & must not have discriminants", Actual, Gen_T);
12364 Abandon_Instantiation (Actual);
12365
12366 elsif Has_Discriminants (Ancestor) then
12367 Error_Msg_NE
12368 ("actual for & must have known discriminants", Actual, Gen_T);
12369 Abandon_Instantiation (Actual);
12370 end if;
12371
12372 if not Subtypes_Statically_Compatible
12373 (Act_T, Ancestor, Formal_Derived_Matching => True)
12374 then
12375 Error_Msg_N
12376 ("constraint on actual is incompatible with formal", Actual);
12377 Abandon_Instantiation (Actual);
12378 end if;
12379 end if;
12380
12381 -- If the formal and actual types are abstract, check that there
12382 -- are no abstract primitives of the actual type that correspond to
12383 -- nonabstract primitives of the formal type (second sentence of
12384 -- RM95 3.9.3(9)).
12385
12386 if Is_Abstract_Type (A_Gen_T) and then Is_Abstract_Type (Act_T) then
12387 Check_Abstract_Primitives : declare
12388 Gen_Prims : constant Elist_Id :=
12389 Primitive_Operations (A_Gen_T);
12390 Gen_Elmt : Elmt_Id;
12391 Gen_Subp : Entity_Id;
12392 Anc_Subp : Entity_Id;
12393 Anc_Formal : Entity_Id;
12394 Anc_F_Type : Entity_Id;
12395
12396 Act_Prims : constant Elist_Id := Primitive_Operations (Act_T);
12397 Act_Elmt : Elmt_Id;
12398 Act_Subp : Entity_Id;
12399 Act_Formal : Entity_Id;
12400 Act_F_Type : Entity_Id;
12401
12402 Subprograms_Correspond : Boolean;
12403
12404 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean;
12405 -- Returns true if T2 is derived directly or indirectly from
12406 -- T1, including derivations from interfaces. T1 and T2 are
12407 -- required to be specific tagged base types.
12408
12409 ------------------------
12410 -- Is_Tagged_Ancestor --
12411 ------------------------
12412
12413 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean
12414 is
12415 Intfc_Elmt : Elmt_Id;
12416
12417 begin
12418 -- The predicate is satisfied if the types are the same
12419
12420 if T1 = T2 then
12421 return True;
12422
12423 -- If we've reached the top of the derivation chain then
12424 -- we know that T1 is not an ancestor of T2.
12425
12426 elsif Etype (T2) = T2 then
12427 return False;
12428
12429 -- Proceed to check T2's immediate parent
12430
12431 elsif Is_Ancestor (T1, Base_Type (Etype (T2))) then
12432 return True;
12433
12434 -- Finally, check to see if T1 is an ancestor of any of T2's
12435 -- progenitors.
12436
12437 else
12438 Intfc_Elmt := First_Elmt (Interfaces (T2));
12439 while Present (Intfc_Elmt) loop
12440 if Is_Ancestor (T1, Node (Intfc_Elmt)) then
12441 return True;
12442 end if;
12443
12444 Next_Elmt (Intfc_Elmt);
12445 end loop;
12446 end if;
12447
12448 return False;
12449 end Is_Tagged_Ancestor;
12450
12451 -- Start of processing for Check_Abstract_Primitives
12452
12453 begin
12454 -- Loop over all of the formal derived type's primitives
12455
12456 Gen_Elmt := First_Elmt (Gen_Prims);
12457 while Present (Gen_Elmt) loop
12458 Gen_Subp := Node (Gen_Elmt);
12459
12460 -- If the primitive of the formal is not abstract, then
12461 -- determine whether there is a corresponding primitive of
12462 -- the actual type that's abstract.
12463
12464 if not Is_Abstract_Subprogram (Gen_Subp) then
12465 Act_Elmt := First_Elmt (Act_Prims);
12466 while Present (Act_Elmt) loop
12467 Act_Subp := Node (Act_Elmt);
12468
12469 -- If we find an abstract primitive of the actual,
12470 -- then we need to test whether it corresponds to the
12471 -- subprogram from which the generic formal primitive
12472 -- is inherited.
12473
12474 if Is_Abstract_Subprogram (Act_Subp) then
12475 Anc_Subp := Alias (Gen_Subp);
12476
12477 -- Test whether we have a corresponding primitive
12478 -- by comparing names, kinds, formal types, and
12479 -- result types.
12480
12481 if Chars (Anc_Subp) = Chars (Act_Subp)
12482 and then Ekind (Anc_Subp) = Ekind (Act_Subp)
12483 then
12484 Anc_Formal := First_Formal (Anc_Subp);
12485 Act_Formal := First_Formal (Act_Subp);
12486 while Present (Anc_Formal)
12487 and then Present (Act_Formal)
12488 loop
12489 Anc_F_Type := Etype (Anc_Formal);
12490 Act_F_Type := Etype (Act_Formal);
12491
12492 if Ekind (Anc_F_Type) =
12493 E_Anonymous_Access_Type
12494 then
12495 Anc_F_Type := Designated_Type (Anc_F_Type);
12496
12497 if Ekind (Act_F_Type) =
12498 E_Anonymous_Access_Type
12499 then
12500 Act_F_Type :=
12501 Designated_Type (Act_F_Type);
12502 else
12503 exit;
12504 end if;
12505
12506 elsif
12507 Ekind (Act_F_Type) = E_Anonymous_Access_Type
12508 then
12509 exit;
12510 end if;
12511
12512 Anc_F_Type := Base_Type (Anc_F_Type);
12513 Act_F_Type := Base_Type (Act_F_Type);
12514
12515 -- If the formal is controlling, then the
12516 -- the type of the actual primitive's formal
12517 -- must be derived directly or indirectly
12518 -- from the type of the ancestor primitive's
12519 -- formal.
12520
12521 if Is_Controlling_Formal (Anc_Formal) then
12522 if not Is_Tagged_Ancestor
12523 (Anc_F_Type, Act_F_Type)
12524 then
12525 exit;
12526 end if;
12527
12528 -- Otherwise the types of the formals must
12529 -- be the same.
12530
12531 elsif Anc_F_Type /= Act_F_Type then
12532 exit;
12533 end if;
12534
12535 Next_Entity (Anc_Formal);
12536 Next_Entity (Act_Formal);
12537 end loop;
12538
12539 -- If we traversed through all of the formals
12540 -- then so far the subprograms correspond, so
12541 -- now check that any result types correspond.
12542
12543 if No (Anc_Formal) and then No (Act_Formal) then
12544 Subprograms_Correspond := True;
12545
12546 if Ekind (Act_Subp) = E_Function then
12547 Anc_F_Type := Etype (Anc_Subp);
12548 Act_F_Type := Etype (Act_Subp);
12549
12550 if Ekind (Anc_F_Type) =
12551 E_Anonymous_Access_Type
12552 then
12553 Anc_F_Type :=
12554 Designated_Type (Anc_F_Type);
12555
12556 if Ekind (Act_F_Type) =
12557 E_Anonymous_Access_Type
12558 then
12559 Act_F_Type :=
12560 Designated_Type (Act_F_Type);
12561 else
12562 Subprograms_Correspond := False;
12563 end if;
12564
12565 elsif
12566 Ekind (Act_F_Type)
12567 = E_Anonymous_Access_Type
12568 then
12569 Subprograms_Correspond := False;
12570 end if;
12571
12572 Anc_F_Type := Base_Type (Anc_F_Type);
12573 Act_F_Type := Base_Type (Act_F_Type);
12574
12575 -- Now either the result types must be
12576 -- the same or, if the result type is
12577 -- controlling, the result type of the
12578 -- actual primitive must descend from the
12579 -- result type of the ancestor primitive.
12580
12581 if Subprograms_Correspond
12582 and then Anc_F_Type /= Act_F_Type
12583 and then
12584 Has_Controlling_Result (Anc_Subp)
12585 and then not Is_Tagged_Ancestor
12586 (Anc_F_Type, Act_F_Type)
12587 then
12588 Subprograms_Correspond := False;
12589 end if;
12590 end if;
12591
12592 -- Found a matching subprogram belonging to
12593 -- formal ancestor type, so actual subprogram
12594 -- corresponds and this violates 3.9.3(9).
12595
12596 if Subprograms_Correspond then
12597 Error_Msg_NE
12598 ("abstract subprogram & overrides "
12599 & "nonabstract subprogram of ancestor",
12600 Actual, Act_Subp);
12601 end if;
12602 end if;
12603 end if;
12604 end if;
12605
12606 Next_Elmt (Act_Elmt);
12607 end loop;
12608 end if;
12609
12610 Next_Elmt (Gen_Elmt);
12611 end loop;
12612 end Check_Abstract_Primitives;
12613 end if;
12614
12615 -- Verify that limitedness matches. If parent is a limited
12616 -- interface then the generic formal is not unless declared
12617 -- explicitly so. If not declared limited, the actual cannot be
12618 -- limited (see AI05-0087).
12619
12620 -- Even though this AI is a binding interpretation, we enable the
12621 -- check only in Ada 2012 mode, because this improper construct
12622 -- shows up in user code and in existing B-tests.
12623
12624 if Is_Limited_Type (Act_T)
12625 and then not Is_Limited_Type (A_Gen_T)
12626 and then Ada_Version >= Ada_2012
12627 then
12628 if In_Instance then
12629 null;
12630 else
12631 Error_Msg_NE
12632 ("actual for non-limited & cannot be a limited type",
12633 Actual, Gen_T);
12634 Explain_Limited_Type (Act_T, Actual);
12635 Abandon_Instantiation (Actual);
12636 end if;
12637 end if;
12638 end Validate_Derived_Type_Instance;
12639
12640 ----------------------------------------
12641 -- Validate_Discriminated_Formal_Type --
12642 ----------------------------------------
12643
12644 procedure Validate_Discriminated_Formal_Type is
12645 Formal_Discr : Entity_Id;
12646 Actual_Discr : Entity_Id;
12647 Formal_Subt : Entity_Id;
12648
12649 begin
12650 if Has_Discriminants (A_Gen_T) then
12651 if not Has_Discriminants (Act_T) then
12652 Error_Msg_NE
12653 ("actual for & must have discriminants", Actual, Gen_T);
12654 Abandon_Instantiation (Actual);
12655
12656 elsif Is_Constrained (Act_T) then
12657 Error_Msg_NE
12658 ("actual for & must be unconstrained", Actual, Gen_T);
12659 Abandon_Instantiation (Actual);
12660
12661 else
12662 Formal_Discr := First_Discriminant (A_Gen_T);
12663 Actual_Discr := First_Discriminant (Act_T);
12664 while Formal_Discr /= Empty loop
12665 if Actual_Discr = Empty then
12666 Error_Msg_NE
12667 ("discriminants on actual do not match formal",
12668 Actual, Gen_T);
12669 Abandon_Instantiation (Actual);
12670 end if;
12671
12672 Formal_Subt := Get_Instance_Of (Etype (Formal_Discr));
12673
12674 -- Access discriminants match if designated types do
12675
12676 if Ekind (Base_Type (Formal_Subt)) = E_Anonymous_Access_Type
12677 and then (Ekind (Base_Type (Etype (Actual_Discr)))) =
12678 E_Anonymous_Access_Type
12679 and then
12680 Get_Instance_Of
12681 (Designated_Type (Base_Type (Formal_Subt))) =
12682 Designated_Type (Base_Type (Etype (Actual_Discr)))
12683 then
12684 null;
12685
12686 elsif Base_Type (Formal_Subt) /=
12687 Base_Type (Etype (Actual_Discr))
12688 then
12689 Error_Msg_NE
12690 ("types of actual discriminants must match formal",
12691 Actual, Gen_T);
12692 Abandon_Instantiation (Actual);
12693
12694 elsif not Subtypes_Statically_Match
12695 (Formal_Subt, Etype (Actual_Discr))
12696 and then Ada_Version >= Ada_95
12697 then
12698 Error_Msg_NE
12699 ("subtypes of actual discriminants must match formal",
12700 Actual, Gen_T);
12701 Abandon_Instantiation (Actual);
12702 end if;
12703
12704 Next_Discriminant (Formal_Discr);
12705 Next_Discriminant (Actual_Discr);
12706 end loop;
12707
12708 if Actual_Discr /= Empty then
12709 Error_Msg_NE
12710 ("discriminants on actual do not match formal",
12711 Actual, Gen_T);
12712 Abandon_Instantiation (Actual);
12713 end if;
12714 end if;
12715 end if;
12716 end Validate_Discriminated_Formal_Type;
12717
12718 ---------------------------------------
12719 -- Validate_Incomplete_Type_Instance --
12720 ---------------------------------------
12721
12722 procedure Validate_Incomplete_Type_Instance is
12723 begin
12724 if not Is_Tagged_Type (Act_T)
12725 and then Is_Tagged_Type (A_Gen_T)
12726 then
12727 Error_Msg_NE
12728 ("actual for & must be a tagged type", Actual, Gen_T);
12729 end if;
12730
12731 Validate_Discriminated_Formal_Type;
12732 end Validate_Incomplete_Type_Instance;
12733
12734 --------------------------------------
12735 -- Validate_Interface_Type_Instance --
12736 --------------------------------------
12737
12738 procedure Validate_Interface_Type_Instance is
12739 begin
12740 if not Is_Interface (Act_T) then
12741 Error_Msg_NE
12742 ("actual for formal interface type must be an interface",
12743 Actual, Gen_T);
12744
12745 elsif Is_Limited_Type (Act_T) /= Is_Limited_Type (A_Gen_T)
12746 or else Is_Task_Interface (A_Gen_T) /= Is_Task_Interface (Act_T)
12747 or else Is_Protected_Interface (A_Gen_T) /=
12748 Is_Protected_Interface (Act_T)
12749 or else Is_Synchronized_Interface (A_Gen_T) /=
12750 Is_Synchronized_Interface (Act_T)
12751 then
12752 Error_Msg_NE
12753 ("actual for interface& does not match (RM 12.5.5(4))",
12754 Actual, Gen_T);
12755 end if;
12756 end Validate_Interface_Type_Instance;
12757
12758 ------------------------------------
12759 -- Validate_Private_Type_Instance --
12760 ------------------------------------
12761
12762 procedure Validate_Private_Type_Instance is
12763 begin
12764 if Is_Limited_Type (Act_T)
12765 and then not Is_Limited_Type (A_Gen_T)
12766 then
12767 if In_Instance then
12768 null;
12769 else
12770 Error_Msg_NE
12771 ("actual for non-limited & cannot be a limited type", Actual,
12772 Gen_T);
12773 Explain_Limited_Type (Act_T, Actual);
12774 Abandon_Instantiation (Actual);
12775 end if;
12776
12777 elsif Known_To_Have_Preelab_Init (A_Gen_T)
12778 and then not Has_Preelaborable_Initialization (Act_T)
12779 then
12780 Error_Msg_NE
12781 ("actual for & must have preelaborable initialization", Actual,
12782 Gen_T);
12783
12784 elsif not Is_Definite_Subtype (Act_T)
12785 and then Is_Definite_Subtype (A_Gen_T)
12786 and then Ada_Version >= Ada_95
12787 then
12788 Error_Msg_NE
12789 ("actual for & must be a definite subtype", Actual, Gen_T);
12790
12791 elsif not Is_Tagged_Type (Act_T)
12792 and then Is_Tagged_Type (A_Gen_T)
12793 then
12794 Error_Msg_NE
12795 ("actual for & must be a tagged type", Actual, Gen_T);
12796 end if;
12797
12798 Validate_Discriminated_Formal_Type;
12799 Ancestor := Gen_T;
12800 end Validate_Private_Type_Instance;
12801
12802 -- Start of processing for Instantiate_Type
12803
12804 begin
12805 if Get_Instance_Of (A_Gen_T) /= A_Gen_T then
12806 Error_Msg_N ("duplicate instantiation of generic type", Actual);
12807 return New_List (Error);
12808
12809 elsif not Is_Entity_Name (Actual)
12810 or else not Is_Type (Entity (Actual))
12811 then
12812 Error_Msg_NE
12813 ("expect valid subtype mark to instantiate &", Actual, Gen_T);
12814 Abandon_Instantiation (Actual);
12815
12816 else
12817 Act_T := Entity (Actual);
12818
12819 -- Ada 2005 (AI-216): An Unchecked_Union subtype shall only be passed
12820 -- as a generic actual parameter if the corresponding formal type
12821 -- does not have a known_discriminant_part, or is a formal derived
12822 -- type that is an Unchecked_Union type.
12823
12824 if Is_Unchecked_Union (Base_Type (Act_T)) then
12825 if not Has_Discriminants (A_Gen_T)
12826 or else (Is_Derived_Type (A_Gen_T)
12827 and then Is_Unchecked_Union (A_Gen_T))
12828 then
12829 null;
12830 else
12831 Error_Msg_N ("unchecked union cannot be the actual for a "
12832 & "discriminated formal type", Act_T);
12833
12834 end if;
12835 end if;
12836
12837 -- Deal with fixed/floating restrictions
12838
12839 if Is_Floating_Point_Type (Act_T) then
12840 Check_Restriction (No_Floating_Point, Actual);
12841 elsif Is_Fixed_Point_Type (Act_T) then
12842 Check_Restriction (No_Fixed_Point, Actual);
12843 end if;
12844
12845 -- Deal with error of using incomplete type as generic actual.
12846 -- This includes limited views of a type, even if the non-limited
12847 -- view may be available.
12848
12849 if Ekind (Act_T) = E_Incomplete_Type
12850 or else (Is_Class_Wide_Type (Act_T)
12851 and then Ekind (Root_Type (Act_T)) = E_Incomplete_Type)
12852 then
12853 -- If the formal is an incomplete type, the actual can be
12854 -- incomplete as well.
12855
12856 if Ekind (A_Gen_T) = E_Incomplete_Type then
12857 null;
12858
12859 elsif Is_Class_Wide_Type (Act_T)
12860 or else No (Full_View (Act_T))
12861 then
12862 Error_Msg_N ("premature use of incomplete type", Actual);
12863 Abandon_Instantiation (Actual);
12864 else
12865 Act_T := Full_View (Act_T);
12866 Set_Entity (Actual, Act_T);
12867
12868 if Has_Private_Component (Act_T) then
12869 Error_Msg_N
12870 ("premature use of type with private component", Actual);
12871 end if;
12872 end if;
12873
12874 -- Deal with error of premature use of private type as generic actual
12875
12876 elsif Is_Private_Type (Act_T)
12877 and then Is_Private_Type (Base_Type (Act_T))
12878 and then not Is_Generic_Type (Act_T)
12879 and then not Is_Derived_Type (Act_T)
12880 and then No (Full_View (Root_Type (Act_T)))
12881 then
12882 -- If the formal is an incomplete type, the actual can be
12883 -- private or incomplete as well.
12884
12885 if Ekind (A_Gen_T) = E_Incomplete_Type then
12886 null;
12887 else
12888 Error_Msg_N ("premature use of private type", Actual);
12889 end if;
12890
12891 elsif Has_Private_Component (Act_T) then
12892 Error_Msg_N
12893 ("premature use of type with private component", Actual);
12894 end if;
12895
12896 Set_Instance_Of (A_Gen_T, Act_T);
12897
12898 -- If the type is generic, the class-wide type may also be used
12899
12900 if Is_Tagged_Type (A_Gen_T)
12901 and then Is_Tagged_Type (Act_T)
12902 and then not Is_Class_Wide_Type (A_Gen_T)
12903 then
12904 Set_Instance_Of (Class_Wide_Type (A_Gen_T),
12905 Class_Wide_Type (Act_T));
12906 end if;
12907
12908 if not Is_Abstract_Type (A_Gen_T)
12909 and then Is_Abstract_Type (Act_T)
12910 then
12911 Error_Msg_N
12912 ("actual of non-abstract formal cannot be abstract", Actual);
12913 end if;
12914
12915 -- A generic scalar type is a first subtype for which we generate
12916 -- an anonymous base type. Indicate that the instance of this base
12917 -- is the base type of the actual.
12918
12919 if Is_Scalar_Type (A_Gen_T) then
12920 Set_Instance_Of (Etype (A_Gen_T), Etype (Act_T));
12921 end if;
12922 end if;
12923
12924 if Error_Posted (Act_T) then
12925 null;
12926 else
12927 case Nkind (Def) is
12928 when N_Formal_Private_Type_Definition =>
12929 Validate_Private_Type_Instance;
12930
12931 when N_Formal_Incomplete_Type_Definition =>
12932 Validate_Incomplete_Type_Instance;
12933
12934 when N_Formal_Derived_Type_Definition =>
12935 Validate_Derived_Type_Instance;
12936
12937 when N_Formal_Discrete_Type_Definition =>
12938 if not Is_Discrete_Type (Act_T) then
12939 Error_Msg_NE
12940 ("expect discrete type in instantiation of&",
12941 Actual, Gen_T);
12942 Abandon_Instantiation (Actual);
12943 end if;
12944
12945 Diagnose_Predicated_Actual;
12946
12947 when N_Formal_Signed_Integer_Type_Definition =>
12948 if not Is_Signed_Integer_Type (Act_T) then
12949 Error_Msg_NE
12950 ("expect signed integer type in instantiation of&",
12951 Actual, Gen_T);
12952 Abandon_Instantiation (Actual);
12953 end if;
12954
12955 Diagnose_Predicated_Actual;
12956
12957 when N_Formal_Modular_Type_Definition =>
12958 if not Is_Modular_Integer_Type (Act_T) then
12959 Error_Msg_NE
12960 ("expect modular type in instantiation of &",
12961 Actual, Gen_T);
12962 Abandon_Instantiation (Actual);
12963 end if;
12964
12965 Diagnose_Predicated_Actual;
12966
12967 when N_Formal_Floating_Point_Definition =>
12968 if not Is_Floating_Point_Type (Act_T) then
12969 Error_Msg_NE
12970 ("expect float type in instantiation of &", Actual, Gen_T);
12971 Abandon_Instantiation (Actual);
12972 end if;
12973
12974 when N_Formal_Ordinary_Fixed_Point_Definition =>
12975 if not Is_Ordinary_Fixed_Point_Type (Act_T) then
12976 Error_Msg_NE
12977 ("expect ordinary fixed point type in instantiation of &",
12978 Actual, Gen_T);
12979 Abandon_Instantiation (Actual);
12980 end if;
12981
12982 when N_Formal_Decimal_Fixed_Point_Definition =>
12983 if not Is_Decimal_Fixed_Point_Type (Act_T) then
12984 Error_Msg_NE
12985 ("expect decimal type in instantiation of &",
12986 Actual, Gen_T);
12987 Abandon_Instantiation (Actual);
12988 end if;
12989
12990 when N_Array_Type_Definition =>
12991 Validate_Array_Type_Instance;
12992
12993 when N_Access_To_Object_Definition =>
12994 Validate_Access_Type_Instance;
12995
12996 when N_Access_Function_Definition
12997 | N_Access_Procedure_Definition
12998 =>
12999 Validate_Access_Subprogram_Instance;
13000
13001 when N_Record_Definition =>
13002 Validate_Interface_Type_Instance;
13003
13004 when N_Derived_Type_Definition =>
13005 Validate_Derived_Interface_Type_Instance;
13006
13007 when others =>
13008 raise Program_Error;
13009 end case;
13010 end if;
13011
13012 Subt := New_Copy (Gen_T);
13013
13014 -- Use adjusted sloc of subtype name as the location for other nodes in
13015 -- the subtype declaration.
13016
13017 Loc := Sloc (Subt);
13018
13019 Decl_Node :=
13020 Make_Subtype_Declaration (Loc,
13021 Defining_Identifier => Subt,
13022 Subtype_Indication => New_Occurrence_Of (Act_T, Loc));
13023
13024 if Is_Private_Type (Act_T) then
13025 Set_Has_Private_View (Subtype_Indication (Decl_Node));
13026
13027 elsif Is_Access_Type (Act_T)
13028 and then Is_Private_Type (Designated_Type (Act_T))
13029 then
13030 Set_Has_Private_View (Subtype_Indication (Decl_Node));
13031 end if;
13032
13033 -- In Ada 2012 the actual may be a limited view. Indicate that
13034 -- the local subtype must be treated as such.
13035
13036 if From_Limited_With (Act_T) then
13037 Set_Ekind (Subt, E_Incomplete_Subtype);
13038 Set_From_Limited_With (Subt);
13039 end if;
13040
13041 Decl_Nodes := New_List (Decl_Node);
13042
13043 -- Flag actual derived types so their elaboration produces the
13044 -- appropriate renamings for the primitive operations of the ancestor.
13045 -- Flag actual for formal private types as well, to determine whether
13046 -- operations in the private part may override inherited operations.
13047 -- If the formal has an interface list, the ancestor is not the
13048 -- parent, but the analyzed formal that includes the interface
13049 -- operations of all its progenitors.
13050
13051 -- Same treatment for formal private types, so we can check whether the
13052 -- type is tagged limited when validating derivations in the private
13053 -- part. (See AI05-096).
13054
13055 if Nkind (Def) = N_Formal_Derived_Type_Definition then
13056 if Present (Interface_List (Def)) then
13057 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
13058 else
13059 Set_Generic_Parent_Type (Decl_Node, Ancestor);
13060 end if;
13061
13062 elsif Nkind_In (Def, N_Formal_Private_Type_Definition,
13063 N_Formal_Incomplete_Type_Definition)
13064 then
13065 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
13066 end if;
13067
13068 -- If the actual is a synchronized type that implements an interface,
13069 -- the primitive operations are attached to the corresponding record,
13070 -- and we have to treat it as an additional generic actual, so that its
13071 -- primitive operations become visible in the instance. The task or
13072 -- protected type itself does not carry primitive operations.
13073
13074 if Is_Concurrent_Type (Act_T)
13075 and then Is_Tagged_Type (Act_T)
13076 and then Present (Corresponding_Record_Type (Act_T))
13077 and then Present (Ancestor)
13078 and then Is_Interface (Ancestor)
13079 then
13080 declare
13081 Corr_Rec : constant Entity_Id :=
13082 Corresponding_Record_Type (Act_T);
13083 New_Corr : Entity_Id;
13084 Corr_Decl : Node_Id;
13085
13086 begin
13087 New_Corr := Make_Temporary (Loc, 'S');
13088 Corr_Decl :=
13089 Make_Subtype_Declaration (Loc,
13090 Defining_Identifier => New_Corr,
13091 Subtype_Indication =>
13092 New_Occurrence_Of (Corr_Rec, Loc));
13093 Append_To (Decl_Nodes, Corr_Decl);
13094
13095 if Ekind (Act_T) = E_Task_Type then
13096 Set_Ekind (Subt, E_Task_Subtype);
13097 else
13098 Set_Ekind (Subt, E_Protected_Subtype);
13099 end if;
13100
13101 Set_Corresponding_Record_Type (Subt, Corr_Rec);
13102 Set_Generic_Parent_Type (Corr_Decl, Ancestor);
13103 Set_Generic_Parent_Type (Decl_Node, Empty);
13104 end;
13105 end if;
13106
13107 -- For a floating-point type, capture dimension info if any, because
13108 -- the generated subtype declaration does not come from source and
13109 -- will not process dimensions.
13110
13111 if Is_Floating_Point_Type (Act_T) then
13112 Copy_Dimensions (Act_T, Subt);
13113 end if;
13114
13115 return Decl_Nodes;
13116 end Instantiate_Type;
13117
13118 ---------------------
13119 -- Is_In_Main_Unit --
13120 ---------------------
13121
13122 function Is_In_Main_Unit (N : Node_Id) return Boolean is
13123 Unum : constant Unit_Number_Type := Get_Source_Unit (N);
13124 Current_Unit : Node_Id;
13125
13126 begin
13127 if Unum = Main_Unit then
13128 return True;
13129
13130 -- If the current unit is a subunit then it is either the main unit or
13131 -- is being compiled as part of the main unit.
13132
13133 elsif Nkind (N) = N_Compilation_Unit then
13134 return Nkind (Unit (N)) = N_Subunit;
13135 end if;
13136
13137 Current_Unit := Parent (N);
13138 while Present (Current_Unit)
13139 and then Nkind (Current_Unit) /= N_Compilation_Unit
13140 loop
13141 Current_Unit := Parent (Current_Unit);
13142 end loop;
13143
13144 -- The instantiation node is in the main unit, or else the current node
13145 -- (perhaps as the result of nested instantiations) is in the main unit,
13146 -- or in the declaration of the main unit, which in this last case must
13147 -- be a body.
13148
13149 return
13150 Current_Unit = Cunit (Main_Unit)
13151 or else Current_Unit = Library_Unit (Cunit (Main_Unit))
13152 or else (Present (Current_Unit)
13153 and then Present (Library_Unit (Current_Unit))
13154 and then Is_In_Main_Unit (Library_Unit (Current_Unit)));
13155 end Is_In_Main_Unit;
13156
13157 ----------------------------
13158 -- Load_Parent_Of_Generic --
13159 ----------------------------
13160
13161 procedure Load_Parent_Of_Generic
13162 (N : Node_Id;
13163 Spec : Node_Id;
13164 Body_Optional : Boolean := False)
13165 is
13166 Comp_Unit : constant Node_Id := Cunit (Get_Source_Unit (Spec));
13167 Saved_Style_Check : constant Boolean := Style_Check;
13168 Saved_Warnings : constant Warning_Record := Save_Warnings;
13169 True_Parent : Node_Id;
13170 Inst_Node : Node_Id;
13171 OK : Boolean;
13172 Previous_Instances : constant Elist_Id := New_Elmt_List;
13173
13174 procedure Collect_Previous_Instances (Decls : List_Id);
13175 -- Collect all instantiations in the given list of declarations, that
13176 -- precede the generic that we need to load. If the bodies of these
13177 -- instantiations are available, we must analyze them, to ensure that
13178 -- the public symbols generated are the same when the unit is compiled
13179 -- to generate code, and when it is compiled in the context of a unit
13180 -- that needs a particular nested instance. This process is applied to
13181 -- both package and subprogram instances.
13182
13183 --------------------------------
13184 -- Collect_Previous_Instances --
13185 --------------------------------
13186
13187 procedure Collect_Previous_Instances (Decls : List_Id) is
13188 Decl : Node_Id;
13189
13190 begin
13191 Decl := First (Decls);
13192 while Present (Decl) loop
13193 if Sloc (Decl) >= Sloc (Inst_Node) then
13194 return;
13195
13196 -- If Decl is an instantiation, then record it as requiring
13197 -- instantiation of the corresponding body, except if it is an
13198 -- abbreviated instantiation generated internally for conformance
13199 -- checking purposes only for the case of a formal package
13200 -- declared without a box (see Instantiate_Formal_Package). Such
13201 -- an instantiation does not generate any code (the actual code
13202 -- comes from actual) and thus does not need to be analyzed here.
13203 -- If the instantiation appears with a generic package body it is
13204 -- not analyzed here either.
13205
13206 elsif Nkind (Decl) = N_Package_Instantiation
13207 and then not Is_Internal (Defining_Entity (Decl))
13208 then
13209 Append_Elmt (Decl, Previous_Instances);
13210
13211 -- For a subprogram instantiation, omit instantiations intrinsic
13212 -- operations (Unchecked_Conversions, etc.) that have no bodies.
13213
13214 elsif Nkind_In (Decl, N_Function_Instantiation,
13215 N_Procedure_Instantiation)
13216 and then not Is_Intrinsic_Subprogram (Entity (Name (Decl)))
13217 then
13218 Append_Elmt (Decl, Previous_Instances);
13219
13220 elsif Nkind (Decl) = N_Package_Declaration then
13221 Collect_Previous_Instances
13222 (Visible_Declarations (Specification (Decl)));
13223 Collect_Previous_Instances
13224 (Private_Declarations (Specification (Decl)));
13225
13226 -- Previous non-generic bodies may contain instances as well
13227
13228 elsif Nkind (Decl) = N_Package_Body
13229 and then Ekind (Corresponding_Spec (Decl)) /= E_Generic_Package
13230 then
13231 Collect_Previous_Instances (Declarations (Decl));
13232
13233 elsif Nkind (Decl) = N_Subprogram_Body
13234 and then not Acts_As_Spec (Decl)
13235 and then not Is_Generic_Subprogram (Corresponding_Spec (Decl))
13236 then
13237 Collect_Previous_Instances (Declarations (Decl));
13238 end if;
13239
13240 Next (Decl);
13241 end loop;
13242 end Collect_Previous_Instances;
13243
13244 -- Start of processing for Load_Parent_Of_Generic
13245
13246 begin
13247 if not In_Same_Source_Unit (N, Spec)
13248 or else Nkind (Unit (Comp_Unit)) = N_Package_Declaration
13249 or else (Nkind (Unit (Comp_Unit)) = N_Package_Body
13250 and then not Is_In_Main_Unit (Spec))
13251 then
13252 -- Find body of parent of spec, and analyze it. A special case arises
13253 -- when the parent is an instantiation, that is to say when we are
13254 -- currently instantiating a nested generic. In that case, there is
13255 -- no separate file for the body of the enclosing instance. Instead,
13256 -- the enclosing body must be instantiated as if it were a pending
13257 -- instantiation, in order to produce the body for the nested generic
13258 -- we require now. Note that in that case the generic may be defined
13259 -- in a package body, the instance defined in the same package body,
13260 -- and the original enclosing body may not be in the main unit.
13261
13262 Inst_Node := Empty;
13263
13264 True_Parent := Parent (Spec);
13265 while Present (True_Parent)
13266 and then Nkind (True_Parent) /= N_Compilation_Unit
13267 loop
13268 if Nkind (True_Parent) = N_Package_Declaration
13269 and then
13270 Nkind (Original_Node (True_Parent)) = N_Package_Instantiation
13271 then
13272 -- Parent is a compilation unit that is an instantiation.
13273 -- Instantiation node has been replaced with package decl.
13274
13275 Inst_Node := Original_Node (True_Parent);
13276 exit;
13277
13278 elsif Nkind (True_Parent) = N_Package_Declaration
13279 and then Present (Generic_Parent (Specification (True_Parent)))
13280 and then Nkind (Parent (True_Parent)) /= N_Compilation_Unit
13281 then
13282 -- Parent is an instantiation within another specification.
13283 -- Declaration for instance has been inserted before original
13284 -- instantiation node. A direct link would be preferable?
13285
13286 Inst_Node := Next (True_Parent);
13287 while Present (Inst_Node)
13288 and then Nkind (Inst_Node) /= N_Package_Instantiation
13289 loop
13290 Next (Inst_Node);
13291 end loop;
13292
13293 -- If the instance appears within a generic, and the generic
13294 -- unit is defined within a formal package of the enclosing
13295 -- generic, there is no generic body available, and none
13296 -- needed. A more precise test should be used ???
13297
13298 if No (Inst_Node) then
13299 return;
13300 end if;
13301
13302 exit;
13303
13304 else
13305 True_Parent := Parent (True_Parent);
13306 end if;
13307 end loop;
13308
13309 -- Case where we are currently instantiating a nested generic
13310
13311 if Present (Inst_Node) then
13312 if Nkind (Parent (True_Parent)) = N_Compilation_Unit then
13313
13314 -- Instantiation node and declaration of instantiated package
13315 -- were exchanged when only the declaration was needed.
13316 -- Restore instantiation node before proceeding with body.
13317
13318 Set_Unit (Parent (True_Parent), Inst_Node);
13319 end if;
13320
13321 -- Now complete instantiation of enclosing body, if it appears in
13322 -- some other unit. If it appears in the current unit, the body
13323 -- will have been instantiated already.
13324
13325 if No (Corresponding_Body (Instance_Spec (Inst_Node))) then
13326
13327 -- We need to determine the expander mode to instantiate the
13328 -- enclosing body. Because the generic body we need may use
13329 -- global entities declared in the enclosing package (including
13330 -- aggregates) it is in general necessary to compile this body
13331 -- with expansion enabled, except if we are within a generic
13332 -- package, in which case the usual generic rule applies.
13333
13334 declare
13335 Exp_Status : Boolean := True;
13336 Scop : Entity_Id;
13337
13338 begin
13339 -- Loop through scopes looking for generic package
13340
13341 Scop := Scope (Defining_Entity (Instance_Spec (Inst_Node)));
13342 while Present (Scop)
13343 and then Scop /= Standard_Standard
13344 loop
13345 if Ekind (Scop) = E_Generic_Package then
13346 Exp_Status := False;
13347 exit;
13348 end if;
13349
13350 Scop := Scope (Scop);
13351 end loop;
13352
13353 -- Collect previous instantiations in the unit that contains
13354 -- the desired generic.
13355
13356 if Nkind (Parent (True_Parent)) /= N_Compilation_Unit
13357 and then not Body_Optional
13358 then
13359 declare
13360 Decl : Elmt_Id;
13361 Info : Pending_Body_Info;
13362 Par : Node_Id;
13363
13364 begin
13365 Par := Parent (Inst_Node);
13366 while Present (Par) loop
13367 exit when Nkind (Parent (Par)) = N_Compilation_Unit;
13368 Par := Parent (Par);
13369 end loop;
13370
13371 pragma Assert (Present (Par));
13372
13373 if Nkind (Par) = N_Package_Body then
13374 Collect_Previous_Instances (Declarations (Par));
13375
13376 elsif Nkind (Par) = N_Package_Declaration then
13377 Collect_Previous_Instances
13378 (Visible_Declarations (Specification (Par)));
13379 Collect_Previous_Instances
13380 (Private_Declarations (Specification (Par)));
13381
13382 else
13383 -- Enclosing unit is a subprogram body. In this
13384 -- case all instance bodies are processed in order
13385 -- and there is no need to collect them separately.
13386
13387 null;
13388 end if;
13389
13390 Decl := First_Elmt (Previous_Instances);
13391 while Present (Decl) loop
13392 Info :=
13393 (Inst_Node => Node (Decl),
13394 Act_Decl =>
13395 Instance_Spec (Node (Decl)),
13396 Expander_Status => Exp_Status,
13397 Current_Sem_Unit =>
13398 Get_Code_Unit (Sloc (Node (Decl))),
13399 Scope_Suppress => Scope_Suppress,
13400 Local_Suppress_Stack_Top =>
13401 Local_Suppress_Stack_Top,
13402 Version => Ada_Version,
13403 Version_Pragma => Ada_Version_Pragma,
13404 Warnings => Save_Warnings,
13405 SPARK_Mode => SPARK_Mode,
13406 SPARK_Mode_Pragma => SPARK_Mode_Pragma);
13407
13408 -- Package instance
13409
13410 if Nkind (Node (Decl)) = N_Package_Instantiation
13411 then
13412 Instantiate_Package_Body
13413 (Info, Body_Optional => True);
13414
13415 -- Subprogram instance
13416
13417 else
13418 -- The instance_spec is in the wrapper package,
13419 -- usually followed by its local renaming
13420 -- declaration. See Build_Subprogram_Renaming
13421 -- for details. If the instance carries aspects,
13422 -- these result in the corresponding pragmas,
13423 -- inserted after the subprogram declaration.
13424 -- They must be skipped as well when retrieving
13425 -- the desired spec. Some of them may have been
13426 -- rewritten as null statements.
13427 -- A direct link would be more robust ???
13428
13429 declare
13430 Decl : Node_Id :=
13431 (Last (Visible_Declarations
13432 (Specification (Info.Act_Decl))));
13433 begin
13434 while Nkind_In (Decl,
13435 N_Null_Statement,
13436 N_Pragma,
13437 N_Subprogram_Renaming_Declaration)
13438 loop
13439 Decl := Prev (Decl);
13440 end loop;
13441
13442 Info.Act_Decl := Decl;
13443 end;
13444
13445 Instantiate_Subprogram_Body
13446 (Info, Body_Optional => True);
13447 end if;
13448
13449 Next_Elmt (Decl);
13450 end loop;
13451 end;
13452 end if;
13453
13454 Instantiate_Package_Body
13455 (Body_Info =>
13456 ((Inst_Node => Inst_Node,
13457 Act_Decl => True_Parent,
13458 Expander_Status => Exp_Status,
13459 Current_Sem_Unit => Get_Code_Unit
13460 (Sloc (Inst_Node)),
13461 Scope_Suppress => Scope_Suppress,
13462 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
13463 Version => Ada_Version,
13464 Version_Pragma => Ada_Version_Pragma,
13465 Warnings => Save_Warnings,
13466 SPARK_Mode => SPARK_Mode,
13467 SPARK_Mode_Pragma => SPARK_Mode_Pragma)),
13468 Body_Optional => Body_Optional);
13469 end;
13470 end if;
13471
13472 -- Case where we are not instantiating a nested generic
13473
13474 else
13475 Opt.Style_Check := False;
13476 Expander_Mode_Save_And_Set (True);
13477 Load_Needed_Body (Comp_Unit, OK);
13478 Opt.Style_Check := Saved_Style_Check;
13479 Restore_Warnings (Saved_Warnings);
13480 Expander_Mode_Restore;
13481
13482 if not OK
13483 and then Unit_Requires_Body (Defining_Entity (Spec))
13484 and then not Body_Optional
13485 then
13486 declare
13487 Bname : constant Unit_Name_Type :=
13488 Get_Body_Name (Get_Unit_Name (Unit (Comp_Unit)));
13489
13490 begin
13491 -- In CodePeer mode, the missing body may make the analysis
13492 -- incomplete, but we do not treat it as fatal.
13493
13494 if CodePeer_Mode then
13495 return;
13496
13497 else
13498 Error_Msg_Unit_1 := Bname;
13499 Error_Msg_N ("this instantiation requires$!", N);
13500 Error_Msg_File_1 :=
13501 Get_File_Name (Bname, Subunit => False);
13502 Error_Msg_N ("\but file{ was not found!", N);
13503 raise Unrecoverable_Error;
13504 end if;
13505 end;
13506 end if;
13507 end if;
13508 end if;
13509
13510 -- If loading parent of the generic caused an instantiation circularity,
13511 -- we abandon compilation at this point, because otherwise in some cases
13512 -- we get into trouble with infinite recursions after this point.
13513
13514 if Circularity_Detected then
13515 raise Unrecoverable_Error;
13516 end if;
13517 end Load_Parent_Of_Generic;
13518
13519 ---------------------------------
13520 -- Map_Formal_Package_Entities --
13521 ---------------------------------
13522
13523 procedure Map_Formal_Package_Entities (Form : Entity_Id; Act : Entity_Id) is
13524 E1 : Entity_Id;
13525 E2 : Entity_Id;
13526
13527 begin
13528 Set_Instance_Of (Form, Act);
13529
13530 -- Traverse formal and actual package to map the corresponding entities.
13531 -- We skip over internal entities that may be generated during semantic
13532 -- analysis, and find the matching entities by name, given that they
13533 -- must appear in the same order.
13534
13535 E1 := First_Entity (Form);
13536 E2 := First_Entity (Act);
13537 while Present (E1) and then E1 /= First_Private_Entity (Form) loop
13538 -- Could this test be a single condition??? Seems like it could, and
13539 -- isn't FPE (Form) a constant anyway???
13540
13541 if not Is_Internal (E1)
13542 and then Present (Parent (E1))
13543 and then not Is_Class_Wide_Type (E1)
13544 and then not Is_Internal_Name (Chars (E1))
13545 then
13546 while Present (E2) and then Chars (E2) /= Chars (E1) loop
13547 Next_Entity (E2);
13548 end loop;
13549
13550 if No (E2) then
13551 exit;
13552 else
13553 Set_Instance_Of (E1, E2);
13554
13555 if Is_Type (E1) and then Is_Tagged_Type (E2) then
13556 Set_Instance_Of (Class_Wide_Type (E1), Class_Wide_Type (E2));
13557 end if;
13558
13559 if Is_Constrained (E1) then
13560 Set_Instance_Of (Base_Type (E1), Base_Type (E2));
13561 end if;
13562
13563 if Ekind (E1) = E_Package and then No (Renamed_Object (E1)) then
13564 Map_Formal_Package_Entities (E1, E2);
13565 end if;
13566 end if;
13567 end if;
13568
13569 Next_Entity (E1);
13570 end loop;
13571 end Map_Formal_Package_Entities;
13572
13573 -----------------------
13574 -- Move_Freeze_Nodes --
13575 -----------------------
13576
13577 procedure Move_Freeze_Nodes
13578 (Out_Of : Entity_Id;
13579 After : Node_Id;
13580 L : List_Id)
13581 is
13582 Decl : Node_Id;
13583 Next_Decl : Node_Id;
13584 Next_Node : Node_Id := After;
13585 Spec : Node_Id;
13586
13587 function Is_Outer_Type (T : Entity_Id) return Boolean;
13588 -- Check whether entity is declared in a scope external to that of the
13589 -- generic unit.
13590
13591 -------------------
13592 -- Is_Outer_Type --
13593 -------------------
13594
13595 function Is_Outer_Type (T : Entity_Id) return Boolean is
13596 Scop : Entity_Id := Scope (T);
13597
13598 begin
13599 if Scope_Depth (Scop) < Scope_Depth (Out_Of) then
13600 return True;
13601
13602 else
13603 while Scop /= Standard_Standard loop
13604 if Scop = Out_Of then
13605 return False;
13606 else
13607 Scop := Scope (Scop);
13608 end if;
13609 end loop;
13610
13611 return True;
13612 end if;
13613 end Is_Outer_Type;
13614
13615 -- Start of processing for Move_Freeze_Nodes
13616
13617 begin
13618 if No (L) then
13619 return;
13620 end if;
13621
13622 -- First remove the freeze nodes that may appear before all other
13623 -- declarations.
13624
13625 Decl := First (L);
13626 while Present (Decl)
13627 and then Nkind (Decl) = N_Freeze_Entity
13628 and then Is_Outer_Type (Entity (Decl))
13629 loop
13630 Decl := Remove_Head (L);
13631 Insert_After (Next_Node, Decl);
13632 Set_Analyzed (Decl, False);
13633 Next_Node := Decl;
13634 Decl := First (L);
13635 end loop;
13636
13637 -- Next scan the list of declarations and remove each freeze node that
13638 -- appears ahead of the current node.
13639
13640 while Present (Decl) loop
13641 while Present (Next (Decl))
13642 and then Nkind (Next (Decl)) = N_Freeze_Entity
13643 and then Is_Outer_Type (Entity (Next (Decl)))
13644 loop
13645 Next_Decl := Remove_Next (Decl);
13646 Insert_After (Next_Node, Next_Decl);
13647 Set_Analyzed (Next_Decl, False);
13648 Next_Node := Next_Decl;
13649 end loop;
13650
13651 -- If the declaration is a nested package or concurrent type, then
13652 -- recurse. Nested generic packages will have been processed from the
13653 -- inside out.
13654
13655 case Nkind (Decl) is
13656 when N_Package_Declaration =>
13657 Spec := Specification (Decl);
13658
13659 when N_Task_Type_Declaration =>
13660 Spec := Task_Definition (Decl);
13661
13662 when N_Protected_Type_Declaration =>
13663 Spec := Protected_Definition (Decl);
13664
13665 when others =>
13666 Spec := Empty;
13667 end case;
13668
13669 if Present (Spec) then
13670 Move_Freeze_Nodes (Out_Of, Next_Node, Visible_Declarations (Spec));
13671 Move_Freeze_Nodes (Out_Of, Next_Node, Private_Declarations (Spec));
13672 end if;
13673
13674 Next (Decl);
13675 end loop;
13676 end Move_Freeze_Nodes;
13677
13678 ----------------
13679 -- Next_Assoc --
13680 ----------------
13681
13682 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr is
13683 begin
13684 return Generic_Renamings.Table (E).Next_In_HTable;
13685 end Next_Assoc;
13686
13687 ------------------------
13688 -- Preanalyze_Actuals --
13689 ------------------------
13690
13691 procedure Preanalyze_Actuals (N : Node_Id; Inst : Entity_Id := Empty) is
13692 Assoc : Node_Id;
13693 Act : Node_Id;
13694 Errs : constant Nat := Serious_Errors_Detected;
13695
13696 Cur : Entity_Id := Empty;
13697 -- Current homograph of the instance name
13698
13699 Vis : Boolean := False;
13700 -- Saved visibility status of the current homograph
13701
13702 begin
13703 Assoc := First (Generic_Associations (N));
13704
13705 -- If the instance is a child unit, its name may hide an outer homonym,
13706 -- so make it invisible to perform name resolution on the actuals.
13707
13708 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name
13709 and then Present
13710 (Current_Entity (Defining_Identifier (Defining_Unit_Name (N))))
13711 then
13712 Cur := Current_Entity (Defining_Identifier (Defining_Unit_Name (N)));
13713
13714 if Is_Compilation_Unit (Cur) then
13715 Vis := Is_Immediately_Visible (Cur);
13716 Set_Is_Immediately_Visible (Cur, False);
13717 else
13718 Cur := Empty;
13719 end if;
13720 end if;
13721
13722 while Present (Assoc) loop
13723 if Nkind (Assoc) /= N_Others_Choice then
13724 Act := Explicit_Generic_Actual_Parameter (Assoc);
13725
13726 -- Within a nested instantiation, a defaulted actual is an empty
13727 -- association, so nothing to analyze. If the subprogram actual
13728 -- is an attribute, analyze prefix only, because actual is not a
13729 -- complete attribute reference.
13730
13731 -- If actual is an allocator, analyze expression only. The full
13732 -- analysis can generate code, and if instance is a compilation
13733 -- unit we have to wait until the package instance is installed
13734 -- to have a proper place to insert this code.
13735
13736 -- String literals may be operators, but at this point we do not
13737 -- know whether the actual is a formal subprogram or a string.
13738
13739 if No (Act) then
13740 null;
13741
13742 elsif Nkind (Act) = N_Attribute_Reference then
13743 Analyze (Prefix (Act));
13744
13745 elsif Nkind (Act) = N_Explicit_Dereference then
13746 Analyze (Prefix (Act));
13747
13748 elsif Nkind (Act) = N_Allocator then
13749 declare
13750 Expr : constant Node_Id := Expression (Act);
13751
13752 begin
13753 if Nkind (Expr) = N_Subtype_Indication then
13754 Analyze (Subtype_Mark (Expr));
13755
13756 -- Analyze separately each discriminant constraint, when
13757 -- given with a named association.
13758
13759 declare
13760 Constr : Node_Id;
13761
13762 begin
13763 Constr := First (Constraints (Constraint (Expr)));
13764 while Present (Constr) loop
13765 if Nkind (Constr) = N_Discriminant_Association then
13766 Analyze (Expression (Constr));
13767 else
13768 Analyze (Constr);
13769 end if;
13770
13771 Next (Constr);
13772 end loop;
13773 end;
13774
13775 else
13776 Analyze (Expr);
13777 end if;
13778 end;
13779
13780 elsif Nkind (Act) /= N_Operator_Symbol then
13781 Analyze (Act);
13782
13783 -- Within a package instance, mark actuals that are limited
13784 -- views, so their use can be moved to the body of the
13785 -- enclosing unit.
13786
13787 if Is_Entity_Name (Act)
13788 and then Is_Type (Entity (Act))
13789 and then From_Limited_With (Entity (Act))
13790 and then Present (Inst)
13791 then
13792 Append_Elmt (Entity (Act), Incomplete_Actuals (Inst));
13793 end if;
13794 end if;
13795
13796 if Errs /= Serious_Errors_Detected then
13797
13798 -- Do a minimal analysis of the generic, to prevent spurious
13799 -- warnings complaining about the generic being unreferenced,
13800 -- before abandoning the instantiation.
13801
13802 Analyze (Name (N));
13803
13804 if Is_Entity_Name (Name (N))
13805 and then Etype (Name (N)) /= Any_Type
13806 then
13807 Generate_Reference (Entity (Name (N)), Name (N));
13808 Set_Is_Instantiated (Entity (Name (N)));
13809 end if;
13810
13811 if Present (Cur) then
13812
13813 -- For the case of a child instance hiding an outer homonym,
13814 -- provide additional warning which might explain the error.
13815
13816 Set_Is_Immediately_Visible (Cur, Vis);
13817 Error_Msg_NE
13818 ("& hides outer unit with the same name??",
13819 N, Defining_Unit_Name (N));
13820 end if;
13821
13822 Abandon_Instantiation (Act);
13823 end if;
13824 end if;
13825
13826 Next (Assoc);
13827 end loop;
13828
13829 if Present (Cur) then
13830 Set_Is_Immediately_Visible (Cur, Vis);
13831 end if;
13832 end Preanalyze_Actuals;
13833
13834 -------------------
13835 -- Remove_Parent --
13836 -------------------
13837
13838 procedure Remove_Parent (In_Body : Boolean := False) is
13839 S : Entity_Id := Current_Scope;
13840 -- S is the scope containing the instantiation just completed. The scope
13841 -- stack contains the parent instances of the instantiation, followed by
13842 -- the original S.
13843
13844 Cur_P : Entity_Id;
13845 E : Entity_Id;
13846 P : Entity_Id;
13847 Hidden : Elmt_Id;
13848
13849 begin
13850 -- After child instantiation is complete, remove from scope stack the
13851 -- extra copy of the current scope, and then remove parent instances.
13852
13853 if not In_Body then
13854 Pop_Scope;
13855
13856 while Current_Scope /= S loop
13857 P := Current_Scope;
13858 End_Package_Scope (Current_Scope);
13859
13860 if In_Open_Scopes (P) then
13861 E := First_Entity (P);
13862 while Present (E) loop
13863 Set_Is_Immediately_Visible (E, True);
13864 Next_Entity (E);
13865 end loop;
13866
13867 -- If instantiation is declared in a block, it is the enclosing
13868 -- scope that might be a parent instance. Note that only one
13869 -- block can be involved, because the parent instances have
13870 -- been installed within it.
13871
13872 if Ekind (P) = E_Block then
13873 Cur_P := Scope (P);
13874 else
13875 Cur_P := P;
13876 end if;
13877
13878 if Is_Generic_Instance (Cur_P) and then P /= Current_Scope then
13879 -- We are within an instance of some sibling. Retain
13880 -- visibility of parent, for proper subsequent cleanup, and
13881 -- reinstall private declarations as well.
13882
13883 Set_In_Private_Part (P);
13884 Install_Private_Declarations (P);
13885 end if;
13886
13887 -- If the ultimate parent is a top-level unit recorded in
13888 -- Instance_Parent_Unit, then reset its visibility to what it was
13889 -- before instantiation. (It's not clear what the purpose is of
13890 -- testing whether Scope (P) is In_Open_Scopes, but that test was
13891 -- present before the ultimate parent test was added.???)
13892
13893 elsif not In_Open_Scopes (Scope (P))
13894 or else (P = Instance_Parent_Unit
13895 and then not Parent_Unit_Visible)
13896 then
13897 Set_Is_Immediately_Visible (P, False);
13898
13899 -- If the current scope is itself an instantiation of a generic
13900 -- nested within P, and we are in the private part of body of this
13901 -- instantiation, restore the full views of P, that were removed
13902 -- in End_Package_Scope above. This obscure case can occur when a
13903 -- subunit of a generic contains an instance of a child unit of
13904 -- its generic parent unit.
13905
13906 elsif S = Current_Scope and then Is_Generic_Instance (S) then
13907 declare
13908 Par : constant Entity_Id :=
13909 Generic_Parent (Package_Specification (S));
13910 begin
13911 if Present (Par)
13912 and then P = Scope (Par)
13913 and then (In_Package_Body (S) or else In_Private_Part (S))
13914 then
13915 Set_In_Private_Part (P);
13916 Install_Private_Declarations (P);
13917 end if;
13918 end;
13919 end if;
13920 end loop;
13921
13922 -- Reset visibility of entities in the enclosing scope
13923
13924 Set_Is_Hidden_Open_Scope (Current_Scope, False);
13925
13926 Hidden := First_Elmt (Hidden_Entities);
13927 while Present (Hidden) loop
13928 Set_Is_Immediately_Visible (Node (Hidden), True);
13929 Next_Elmt (Hidden);
13930 end loop;
13931
13932 else
13933 -- Each body is analyzed separately, and there is no context that
13934 -- needs preserving from one body instance to the next, so remove all
13935 -- parent scopes that have been installed.
13936
13937 while Present (S) loop
13938 End_Package_Scope (S);
13939 Set_Is_Immediately_Visible (S, False);
13940 S := Current_Scope;
13941 exit when S = Standard_Standard;
13942 end loop;
13943 end if;
13944 end Remove_Parent;
13945
13946 -----------------
13947 -- Restore_Env --
13948 -----------------
13949
13950 procedure Restore_Env is
13951 Saved : Instance_Env renames Instance_Envs.Table (Instance_Envs.Last);
13952
13953 begin
13954 if No (Current_Instantiated_Parent.Act_Id) then
13955 -- Restore environment after subprogram inlining
13956
13957 Restore_Private_Views (Empty);
13958 end if;
13959
13960 Current_Instantiated_Parent := Saved.Instantiated_Parent;
13961 Exchanged_Views := Saved.Exchanged_Views;
13962 Hidden_Entities := Saved.Hidden_Entities;
13963 Current_Sem_Unit := Saved.Current_Sem_Unit;
13964 Parent_Unit_Visible := Saved.Parent_Unit_Visible;
13965 Instance_Parent_Unit := Saved.Instance_Parent_Unit;
13966
13967 Restore_Opt_Config_Switches (Saved.Switches);
13968
13969 Instance_Envs.Decrement_Last;
13970 end Restore_Env;
13971
13972 ---------------------------
13973 -- Restore_Private_Views --
13974 ---------------------------
13975
13976 procedure Restore_Private_Views
13977 (Pack_Id : Entity_Id;
13978 Is_Package : Boolean := True)
13979 is
13980 M : Elmt_Id;
13981 E : Entity_Id;
13982 Typ : Entity_Id;
13983 Dep_Elmt : Elmt_Id;
13984 Dep_Typ : Node_Id;
13985
13986 procedure Restore_Nested_Formal (Formal : Entity_Id);
13987 -- Hide the generic formals of formal packages declared with box which
13988 -- were reachable in the current instantiation.
13989
13990 ---------------------------
13991 -- Restore_Nested_Formal --
13992 ---------------------------
13993
13994 procedure Restore_Nested_Formal (Formal : Entity_Id) is
13995 Ent : Entity_Id;
13996
13997 begin
13998 if Present (Renamed_Object (Formal))
13999 and then Denotes_Formal_Package (Renamed_Object (Formal), True)
14000 then
14001 return;
14002
14003 elsif Present (Associated_Formal_Package (Formal)) then
14004 Ent := First_Entity (Formal);
14005 while Present (Ent) loop
14006 exit when Ekind (Ent) = E_Package
14007 and then Renamed_Entity (Ent) = Renamed_Entity (Formal);
14008
14009 Set_Is_Hidden (Ent);
14010 Set_Is_Potentially_Use_Visible (Ent, False);
14011
14012 -- If package, then recurse
14013
14014 if Ekind (Ent) = E_Package then
14015 Restore_Nested_Formal (Ent);
14016 end if;
14017
14018 Next_Entity (Ent);
14019 end loop;
14020 end if;
14021 end Restore_Nested_Formal;
14022
14023 -- Start of processing for Restore_Private_Views
14024
14025 begin
14026 M := First_Elmt (Exchanged_Views);
14027 while Present (M) loop
14028 Typ := Node (M);
14029
14030 -- Subtypes of types whose views have been exchanged, and that are
14031 -- defined within the instance, were not on the Private_Dependents
14032 -- list on entry to the instance, so they have to be exchanged
14033 -- explicitly now, in order to remain consistent with the view of the
14034 -- parent type.
14035
14036 if Ekind_In (Typ, E_Private_Type,
14037 E_Limited_Private_Type,
14038 E_Record_Type_With_Private)
14039 then
14040 Dep_Elmt := First_Elmt (Private_Dependents (Typ));
14041 while Present (Dep_Elmt) loop
14042 Dep_Typ := Node (Dep_Elmt);
14043
14044 if Scope (Dep_Typ) = Pack_Id
14045 and then Present (Full_View (Dep_Typ))
14046 then
14047 Replace_Elmt (Dep_Elmt, Full_View (Dep_Typ));
14048 Exchange_Declarations (Dep_Typ);
14049 end if;
14050
14051 Next_Elmt (Dep_Elmt);
14052 end loop;
14053 end if;
14054
14055 Exchange_Declarations (Node (M));
14056 Next_Elmt (M);
14057 end loop;
14058
14059 if No (Pack_Id) then
14060 return;
14061 end if;
14062
14063 -- Make the generic formal parameters private, and make the formal types
14064 -- into subtypes of the actuals again.
14065
14066 E := First_Entity (Pack_Id);
14067 while Present (E) loop
14068 Set_Is_Hidden (E, True);
14069
14070 if Is_Type (E)
14071 and then Nkind (Parent (E)) = N_Subtype_Declaration
14072 then
14073 -- If the actual for E is itself a generic actual type from
14074 -- an enclosing instance, E is still a generic actual type
14075 -- outside of the current instance. This matter when resolving
14076 -- an overloaded call that may be ambiguous in the enclosing
14077 -- instance, when two of its actuals coincide.
14078
14079 if Is_Entity_Name (Subtype_Indication (Parent (E)))
14080 and then Is_Generic_Actual_Type
14081 (Entity (Subtype_Indication (Parent (E))))
14082 then
14083 null;
14084 else
14085 Set_Is_Generic_Actual_Type (E, False);
14086 end if;
14087
14088 -- An unusual case of aliasing: the actual may also be directly
14089 -- visible in the generic, and be private there, while it is fully
14090 -- visible in the context of the instance. The internal subtype
14091 -- is private in the instance but has full visibility like its
14092 -- parent in the enclosing scope. This enforces the invariant that
14093 -- the privacy status of all private dependents of a type coincide
14094 -- with that of the parent type. This can only happen when a
14095 -- generic child unit is instantiated within a sibling.
14096
14097 if Is_Private_Type (E)
14098 and then not Is_Private_Type (Etype (E))
14099 then
14100 Exchange_Declarations (E);
14101 end if;
14102
14103 elsif Ekind (E) = E_Package then
14104
14105 -- The end of the renaming list is the renaming of the generic
14106 -- package itself. If the instance is a subprogram, all entities
14107 -- in the corresponding package are renamings. If this entity is
14108 -- a formal package, make its own formals private as well. The
14109 -- actual in this case is itself the renaming of an instantiation.
14110 -- If the entity is not a package renaming, it is the entity
14111 -- created to validate formal package actuals: ignore it.
14112
14113 -- If the actual is itself a formal package for the enclosing
14114 -- generic, or the actual for such a formal package, it remains
14115 -- visible on exit from the instance, and therefore nothing needs
14116 -- to be done either, except to keep it accessible.
14117
14118 if Is_Package and then Renamed_Object (E) = Pack_Id then
14119 exit;
14120
14121 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
14122 null;
14123
14124 elsif
14125 Denotes_Formal_Package (Renamed_Object (E), True, Pack_Id)
14126 then
14127 Set_Is_Hidden (E, False);
14128
14129 else
14130 declare
14131 Act_P : constant Entity_Id := Renamed_Object (E);
14132 Id : Entity_Id;
14133
14134 begin
14135 Id := First_Entity (Act_P);
14136 while Present (Id)
14137 and then Id /= First_Private_Entity (Act_P)
14138 loop
14139 exit when Ekind (Id) = E_Package
14140 and then Renamed_Object (Id) = Act_P;
14141
14142 Set_Is_Hidden (Id, True);
14143 Set_Is_Potentially_Use_Visible (Id, In_Use (Act_P));
14144
14145 if Ekind (Id) = E_Package then
14146 Restore_Nested_Formal (Id);
14147 end if;
14148
14149 Next_Entity (Id);
14150 end loop;
14151 end;
14152 end if;
14153 end if;
14154
14155 Next_Entity (E);
14156 end loop;
14157 end Restore_Private_Views;
14158
14159 --------------
14160 -- Save_Env --
14161 --------------
14162
14163 procedure Save_Env
14164 (Gen_Unit : Entity_Id;
14165 Act_Unit : Entity_Id)
14166 is
14167 begin
14168 Init_Env;
14169 Set_Instance_Env (Gen_Unit, Act_Unit);
14170 end Save_Env;
14171
14172 ----------------------------
14173 -- Save_Global_References --
14174 ----------------------------
14175
14176 procedure Save_Global_References (Templ : Node_Id) is
14177
14178 -- ??? it is horrible to use global variables in highly recursive code
14179
14180 E : Entity_Id;
14181 -- The entity of the current associated node
14182
14183 Gen_Scope : Entity_Id;
14184 -- The scope of the generic for which references are being saved
14185
14186 N2 : Node_Id;
14187 -- The current associated node
14188
14189 function Is_Global (E : Entity_Id) return Boolean;
14190 -- Check whether entity is defined outside of generic unit. Examine the
14191 -- scope of an entity, and the scope of the scope, etc, until we find
14192 -- either Standard, in which case the entity is global, or the generic
14193 -- unit itself, which indicates that the entity is local. If the entity
14194 -- is the generic unit itself, as in the case of a recursive call, or
14195 -- the enclosing generic unit, if different from the current scope, then
14196 -- it is local as well, because it will be replaced at the point of
14197 -- instantiation. On the other hand, if it is a reference to a child
14198 -- unit of a common ancestor, which appears in an instantiation, it is
14199 -- global because it is used to denote a specific compilation unit at
14200 -- the time the instantiations will be analyzed.
14201
14202 procedure Qualify_Universal_Operands
14203 (Op : Node_Id;
14204 Func_Call : Node_Id);
14205 -- Op denotes a binary or unary operator in generic template Templ. Node
14206 -- Func_Call is the function call alternative of the operator within the
14207 -- the analyzed copy of the template. Change each operand which yields a
14208 -- universal type by wrapping it into a qualified expression
14209 --
14210 -- Actual_Typ'(Operand)
14211 --
14212 -- where Actual_Typ is the type of corresponding actual parameter of
14213 -- Operand in Func_Call.
14214
14215 procedure Reset_Entity (N : Node_Id);
14216 -- Save semantic information on global entity so that it is not resolved
14217 -- again at instantiation time.
14218
14219 procedure Save_Entity_Descendants (N : Node_Id);
14220 -- Apply Save_Global_References to the two syntactic descendants of
14221 -- non-terminal nodes that carry an Associated_Node and are processed
14222 -- through Reset_Entity. Once the global entity (if any) has been
14223 -- captured together with its type, only two syntactic descendants need
14224 -- to be traversed to complete the processing of the tree rooted at N.
14225 -- This applies to Selected_Components, Expanded_Names, and to Operator
14226 -- nodes. N can also be a character literal, identifier, or operator
14227 -- symbol node, but the call has no effect in these cases.
14228
14229 procedure Save_Global_Defaults (N1 : Node_Id; N2 : Node_Id);
14230 -- Default actuals in nested instances must be handled specially
14231 -- because there is no link to them from the original tree. When an
14232 -- actual subprogram is given by a default, we add an explicit generic
14233 -- association for it in the instantiation node. When we save the
14234 -- global references on the name of the instance, we recover the list
14235 -- of generic associations, and add an explicit one to the original
14236 -- generic tree, through which a global actual can be preserved.
14237 -- Similarly, if a child unit is instantiated within a sibling, in the
14238 -- context of the parent, we must preserve the identifier of the parent
14239 -- so that it can be properly resolved in a subsequent instantiation.
14240
14241 procedure Save_Global_Descendant (D : Union_Id);
14242 -- Apply Save_References recursively to the descendants of node D
14243
14244 procedure Save_References (N : Node_Id);
14245 -- This is the recursive procedure that does the work, once the
14246 -- enclosing generic scope has been established.
14247
14248 ---------------
14249 -- Is_Global --
14250 ---------------
14251
14252 function Is_Global (E : Entity_Id) return Boolean is
14253 Se : Entity_Id;
14254
14255 function Is_Instance_Node (Decl : Node_Id) return Boolean;
14256 -- Determine whether the parent node of a reference to a child unit
14257 -- denotes an instantiation or a formal package, in which case the
14258 -- reference to the child unit is global, even if it appears within
14259 -- the current scope (e.g. when the instance appears within the body
14260 -- of an ancestor).
14261
14262 ----------------------
14263 -- Is_Instance_Node --
14264 ----------------------
14265
14266 function Is_Instance_Node (Decl : Node_Id) return Boolean is
14267 begin
14268 return Nkind (Decl) in N_Generic_Instantiation
14269 or else
14270 Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration;
14271 end Is_Instance_Node;
14272
14273 -- Start of processing for Is_Global
14274
14275 begin
14276 if E = Gen_Scope then
14277 return False;
14278
14279 elsif E = Standard_Standard then
14280 return True;
14281
14282 elsif Is_Child_Unit (E)
14283 and then (Is_Instance_Node (Parent (N2))
14284 or else (Nkind (Parent (N2)) = N_Expanded_Name
14285 and then N2 = Selector_Name (Parent (N2))
14286 and then
14287 Is_Instance_Node (Parent (Parent (N2)))))
14288 then
14289 return True;
14290
14291 else
14292 Se := Scope (E);
14293 while Se /= Gen_Scope loop
14294 if Se = Standard_Standard then
14295 return True;
14296 else
14297 Se := Scope (Se);
14298 end if;
14299 end loop;
14300
14301 return False;
14302 end if;
14303 end Is_Global;
14304
14305 --------------------------------
14306 -- Qualify_Universal_Operands --
14307 --------------------------------
14308
14309 procedure Qualify_Universal_Operands
14310 (Op : Node_Id;
14311 Func_Call : Node_Id)
14312 is
14313 procedure Qualify_Operand (Opnd : Node_Id; Actual : Node_Id);
14314 -- Rewrite operand Opnd as a qualified expression of the form
14315 --
14316 -- Actual_Typ'(Opnd)
14317 --
14318 -- where Actual is the corresponding actual parameter of Opnd in
14319 -- function call Func_Call.
14320
14321 function Qualify_Type
14322 (Loc : Source_Ptr;
14323 Typ : Entity_Id) return Node_Id;
14324 -- Qualify type Typ by creating a selected component of the form
14325 --
14326 -- Scope_Of_Typ.Typ
14327
14328 ---------------------
14329 -- Qualify_Operand --
14330 ---------------------
14331
14332 procedure Qualify_Operand (Opnd : Node_Id; Actual : Node_Id) is
14333 Loc : constant Source_Ptr := Sloc (Opnd);
14334 Typ : constant Entity_Id := Etype (Actual);
14335 Mark : Node_Id;
14336 Qual : Node_Id;
14337
14338 begin
14339 -- Qualify the operand when it is of a universal type. Note that
14340 -- the template is unanalyzed and it is not possible to directly
14341 -- query the type. This transformation is not done when the type
14342 -- of the actual is internally generated because the type will be
14343 -- regenerated in the instance.
14344
14345 if Yields_Universal_Type (Opnd)
14346 and then Comes_From_Source (Typ)
14347 and then not Is_Hidden (Typ)
14348 then
14349 -- The type of the actual may be a global reference. Save this
14350 -- information by creating a reference to it.
14351
14352 if Is_Global (Typ) then
14353 Mark := New_Occurrence_Of (Typ, Loc);
14354
14355 -- Otherwise rely on resolution to find the proper type within
14356 -- the instance.
14357
14358 else
14359 Mark := Qualify_Type (Loc, Typ);
14360 end if;
14361
14362 Qual :=
14363 Make_Qualified_Expression (Loc,
14364 Subtype_Mark => Mark,
14365 Expression => Relocate_Node (Opnd));
14366
14367 -- Mark the qualification to distinguish it from other source
14368 -- constructs and signal the instantiation mechanism that this
14369 -- node requires special processing. See Copy_Generic_Node for
14370 -- details.
14371
14372 Set_Is_Qualified_Universal_Literal (Qual);
14373
14374 Rewrite (Opnd, Qual);
14375 end if;
14376 end Qualify_Operand;
14377
14378 ------------------
14379 -- Qualify_Type --
14380 ------------------
14381
14382 function Qualify_Type
14383 (Loc : Source_Ptr;
14384 Typ : Entity_Id) return Node_Id
14385 is
14386 Scop : constant Entity_Id := Scope (Typ);
14387 Result : Node_Id;
14388
14389 begin
14390 Result := Make_Identifier (Loc, Chars (Typ));
14391
14392 if Present (Scop) and then not Is_Generic_Unit (Scop) then
14393 Result :=
14394 Make_Selected_Component (Loc,
14395 Prefix => Make_Identifier (Loc, Chars (Scop)),
14396 Selector_Name => Result);
14397 end if;
14398
14399 return Result;
14400 end Qualify_Type;
14401
14402 -- Local variables
14403
14404 Actuals : constant List_Id := Parameter_Associations (Func_Call);
14405
14406 -- Start of processing for Qualify_Universal_Operands
14407
14408 begin
14409 if Nkind (Op) in N_Binary_Op then
14410 Qualify_Operand (Left_Opnd (Op), First (Actuals));
14411 Qualify_Operand (Right_Opnd (Op), Next (First (Actuals)));
14412
14413 elsif Nkind (Op) in N_Unary_Op then
14414 Qualify_Operand (Right_Opnd (Op), First (Actuals));
14415 end if;
14416 end Qualify_Universal_Operands;
14417
14418 ------------------
14419 -- Reset_Entity --
14420 ------------------
14421
14422 procedure Reset_Entity (N : Node_Id) is
14423 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id);
14424 -- If the type of N2 is global to the generic unit, save the type in
14425 -- the generic node. Just as we perform name capture for explicit
14426 -- references within the generic, we must capture the global types
14427 -- of local entities because they may participate in resolution in
14428 -- the instance.
14429
14430 function Top_Ancestor (E : Entity_Id) return Entity_Id;
14431 -- Find the ultimate ancestor of the current unit. If it is not a
14432 -- generic unit, then the name of the current unit in the prefix of
14433 -- an expanded name must be replaced with its generic homonym to
14434 -- ensure that it will be properly resolved in an instance.
14435
14436 ---------------------
14437 -- Set_Global_Type --
14438 ---------------------
14439
14440 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id) is
14441 Typ : constant Entity_Id := Etype (N2);
14442
14443 begin
14444 Set_Etype (N, Typ);
14445
14446 -- If the entity of N is not the associated node, this is a
14447 -- nested generic and it has an associated node as well, whose
14448 -- type is already the full view (see below). Indicate that the
14449 -- original node has a private view.
14450
14451 if Entity (N) /= N2 and then Has_Private_View (Entity (N)) then
14452 Set_Has_Private_View (N);
14453 end if;
14454
14455 -- If not a private type, nothing else to do
14456
14457 if not Is_Private_Type (Typ) then
14458 if Is_Array_Type (Typ)
14459 and then Is_Private_Type (Component_Type (Typ))
14460 then
14461 Set_Has_Private_View (N);
14462 end if;
14463
14464 -- If it is a derivation of a private type in a context where no
14465 -- full view is needed, nothing to do either.
14466
14467 elsif No (Full_View (Typ)) and then Typ /= Etype (Typ) then
14468 null;
14469
14470 -- Otherwise mark the type for flipping and use the full view when
14471 -- available.
14472
14473 else
14474 Set_Has_Private_View (N);
14475
14476 if Present (Full_View (Typ)) then
14477 Set_Etype (N2, Full_View (Typ));
14478 end if;
14479 end if;
14480
14481 if Is_Floating_Point_Type (Typ)
14482 and then Has_Dimension_System (Typ)
14483 then
14484 Copy_Dimensions (N2, N);
14485 end if;
14486 end Set_Global_Type;
14487
14488 ------------------
14489 -- Top_Ancestor --
14490 ------------------
14491
14492 function Top_Ancestor (E : Entity_Id) return Entity_Id is
14493 Par : Entity_Id;
14494
14495 begin
14496 Par := E;
14497 while Is_Child_Unit (Par) loop
14498 Par := Scope (Par);
14499 end loop;
14500
14501 return Par;
14502 end Top_Ancestor;
14503
14504 -- Start of processing for Reset_Entity
14505
14506 begin
14507 N2 := Get_Associated_Node (N);
14508 E := Entity (N2);
14509
14510 if Present (E) then
14511
14512 -- If the node is an entry call to an entry in an enclosing task,
14513 -- it is rewritten as a selected component. No global entity to
14514 -- preserve in this case, since the expansion will be redone in
14515 -- the instance.
14516
14517 if not Nkind_In (E, N_Defining_Character_Literal,
14518 N_Defining_Identifier,
14519 N_Defining_Operator_Symbol)
14520 then
14521 Set_Associated_Node (N, Empty);
14522 Set_Etype (N, Empty);
14523 return;
14524 end if;
14525
14526 -- If the entity is an itype created as a subtype of an access
14527 -- type with a null exclusion restore source entity for proper
14528 -- visibility. The itype will be created anew in the instance.
14529
14530 if Is_Itype (E)
14531 and then Ekind (E) = E_Access_Subtype
14532 and then Is_Entity_Name (N)
14533 and then Chars (Etype (E)) = Chars (N)
14534 then
14535 E := Etype (E);
14536 Set_Entity (N2, E);
14537 Set_Etype (N2, E);
14538 end if;
14539
14540 if Is_Global (E) then
14541
14542 -- If the entity is a package renaming that is the prefix of
14543 -- an expanded name, it has been rewritten as the renamed
14544 -- package, which is necessary semantically but complicates
14545 -- ASIS tree traversal, so we recover the original entity to
14546 -- expose the renaming. Take into account that the context may
14547 -- be a nested generic, that the original node may itself have
14548 -- an associated node that had better be an entity, and that
14549 -- the current node is still a selected component.
14550
14551 if Ekind (E) = E_Package
14552 and then Nkind (N) = N_Selected_Component
14553 and then Nkind (Parent (N)) = N_Expanded_Name
14554 and then Present (Original_Node (N2))
14555 and then Is_Entity_Name (Original_Node (N2))
14556 and then Present (Entity (Original_Node (N2)))
14557 then
14558 if Is_Global (Entity (Original_Node (N2))) then
14559 N2 := Original_Node (N2);
14560 Set_Associated_Node (N, N2);
14561 Set_Global_Type (N, N2);
14562
14563 -- Renaming is local, and will be resolved in instance
14564
14565 else
14566 Set_Associated_Node (N, Empty);
14567 Set_Etype (N, Empty);
14568 end if;
14569
14570 else
14571 Set_Global_Type (N, N2);
14572 end if;
14573
14574 elsif Nkind (N) = N_Op_Concat
14575 and then Is_Generic_Type (Etype (N2))
14576 and then (Base_Type (Etype (Right_Opnd (N2))) = Etype (N2)
14577 or else
14578 Base_Type (Etype (Left_Opnd (N2))) = Etype (N2))
14579 and then Is_Intrinsic_Subprogram (E)
14580 then
14581 null;
14582
14583 -- Entity is local. Mark generic node as unresolved. Note that now
14584 -- it does not have an entity.
14585
14586 else
14587 Set_Associated_Node (N, Empty);
14588 Set_Etype (N, Empty);
14589 end if;
14590
14591 if Nkind (Parent (N)) in N_Generic_Instantiation
14592 and then N = Name (Parent (N))
14593 then
14594 Save_Global_Defaults (Parent (N), Parent (N2));
14595 end if;
14596
14597 elsif Nkind (Parent (N)) = N_Selected_Component
14598 and then Nkind (Parent (N2)) = N_Expanded_Name
14599 then
14600 if Is_Global (Entity (Parent (N2))) then
14601 Change_Selected_Component_To_Expanded_Name (Parent (N));
14602 Set_Associated_Node (Parent (N), Parent (N2));
14603 Set_Global_Type (Parent (N), Parent (N2));
14604 Save_Entity_Descendants (N);
14605
14606 -- If this is a reference to the current generic entity, replace
14607 -- by the name of the generic homonym of the current package. This
14608 -- is because in an instantiation Par.P.Q will not resolve to the
14609 -- name of the instance, whose enclosing scope is not necessarily
14610 -- Par. We use the generic homonym rather that the name of the
14611 -- generic itself because it may be hidden by a local declaration.
14612
14613 elsif In_Open_Scopes (Entity (Parent (N2)))
14614 and then not
14615 Is_Generic_Unit (Top_Ancestor (Entity (Prefix (Parent (N2)))))
14616 then
14617 if Ekind (Entity (Parent (N2))) = E_Generic_Package then
14618 Rewrite (Parent (N),
14619 Make_Identifier (Sloc (N),
14620 Chars =>
14621 Chars (Generic_Homonym (Entity (Parent (N2))))));
14622 else
14623 Rewrite (Parent (N),
14624 Make_Identifier (Sloc (N),
14625 Chars => Chars (Selector_Name (Parent (N2)))));
14626 end if;
14627 end if;
14628
14629 if Nkind (Parent (Parent (N))) in N_Generic_Instantiation
14630 and then Parent (N) = Name (Parent (Parent (N)))
14631 then
14632 Save_Global_Defaults
14633 (Parent (Parent (N)), Parent (Parent (N2)));
14634 end if;
14635
14636 -- A selected component may denote a static constant that has been
14637 -- folded. If the static constant is global to the generic, capture
14638 -- its value. Otherwise the folding will happen in any instantiation.
14639
14640 elsif Nkind (Parent (N)) = N_Selected_Component
14641 and then Nkind_In (Parent (N2), N_Integer_Literal, N_Real_Literal)
14642 then
14643 if Present (Entity (Original_Node (Parent (N2))))
14644 and then Is_Global (Entity (Original_Node (Parent (N2))))
14645 then
14646 Rewrite (Parent (N), New_Copy (Parent (N2)));
14647 Set_Analyzed (Parent (N), False);
14648 end if;
14649
14650 -- A selected component may be transformed into a parameterless
14651 -- function call. If the called entity is global, rewrite the node
14652 -- appropriately, i.e. as an extended name for the global entity.
14653
14654 elsif Nkind (Parent (N)) = N_Selected_Component
14655 and then Nkind (Parent (N2)) = N_Function_Call
14656 and then N = Selector_Name (Parent (N))
14657 then
14658 if No (Parameter_Associations (Parent (N2))) then
14659 if Is_Global (Entity (Name (Parent (N2)))) then
14660 Change_Selected_Component_To_Expanded_Name (Parent (N));
14661 Set_Associated_Node (Parent (N), Name (Parent (N2)));
14662 Set_Global_Type (Parent (N), Name (Parent (N2)));
14663 Save_Entity_Descendants (N);
14664
14665 else
14666 Set_Is_Prefixed_Call (Parent (N));
14667 Set_Associated_Node (N, Empty);
14668 Set_Etype (N, Empty);
14669 end if;
14670
14671 -- In Ada 2005, X.F may be a call to a primitive operation,
14672 -- rewritten as F (X). This rewriting will be done again in an
14673 -- instance, so keep the original node. Global entities will be
14674 -- captured as for other constructs. Indicate that this must
14675 -- resolve as a call, to prevent accidental overloading in the
14676 -- instance, if both a component and a primitive operation appear
14677 -- as candidates.
14678
14679 else
14680 Set_Is_Prefixed_Call (Parent (N));
14681 end if;
14682
14683 -- Entity is local. Reset in generic unit, so that node is resolved
14684 -- anew at the point of instantiation.
14685
14686 else
14687 Set_Associated_Node (N, Empty);
14688 Set_Etype (N, Empty);
14689 end if;
14690 end Reset_Entity;
14691
14692 -----------------------------
14693 -- Save_Entity_Descendants --
14694 -----------------------------
14695
14696 procedure Save_Entity_Descendants (N : Node_Id) is
14697 begin
14698 case Nkind (N) is
14699 when N_Binary_Op =>
14700 Save_Global_Descendant (Union_Id (Left_Opnd (N)));
14701 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
14702
14703 when N_Unary_Op =>
14704 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
14705
14706 when N_Expanded_Name
14707 | N_Selected_Component
14708 =>
14709 Save_Global_Descendant (Union_Id (Prefix (N)));
14710 Save_Global_Descendant (Union_Id (Selector_Name (N)));
14711
14712 when N_Character_Literal
14713 | N_Identifier
14714 | N_Operator_Symbol
14715 =>
14716 null;
14717
14718 when others =>
14719 raise Program_Error;
14720 end case;
14721 end Save_Entity_Descendants;
14722
14723 --------------------------
14724 -- Save_Global_Defaults --
14725 --------------------------
14726
14727 procedure Save_Global_Defaults (N1 : Node_Id; N2 : Node_Id) is
14728 Loc : constant Source_Ptr := Sloc (N1);
14729 Assoc2 : constant List_Id := Generic_Associations (N2);
14730 Gen_Id : constant Entity_Id := Get_Generic_Entity (N2);
14731 Assoc1 : List_Id;
14732 Act1 : Node_Id;
14733 Act2 : Node_Id;
14734 Def : Node_Id;
14735 Ndec : Node_Id;
14736 Subp : Entity_Id;
14737 Actual : Entity_Id;
14738
14739 begin
14740 Assoc1 := Generic_Associations (N1);
14741
14742 if Present (Assoc1) then
14743 Act1 := First (Assoc1);
14744 else
14745 Act1 := Empty;
14746 Set_Generic_Associations (N1, New_List);
14747 Assoc1 := Generic_Associations (N1);
14748 end if;
14749
14750 if Present (Assoc2) then
14751 Act2 := First (Assoc2);
14752 else
14753 return;
14754 end if;
14755
14756 while Present (Act1) and then Present (Act2) loop
14757 Next (Act1);
14758 Next (Act2);
14759 end loop;
14760
14761 -- Find the associations added for default subprograms
14762
14763 if Present (Act2) then
14764 while Nkind (Act2) /= N_Generic_Association
14765 or else No (Entity (Selector_Name (Act2)))
14766 or else not Is_Overloadable (Entity (Selector_Name (Act2)))
14767 loop
14768 Next (Act2);
14769 end loop;
14770
14771 -- Add a similar association if the default is global. The
14772 -- renaming declaration for the actual has been analyzed, and
14773 -- its alias is the program it renames. Link the actual in the
14774 -- original generic tree with the node in the analyzed tree.
14775
14776 while Present (Act2) loop
14777 Subp := Entity (Selector_Name (Act2));
14778 Def := Explicit_Generic_Actual_Parameter (Act2);
14779
14780 -- Following test is defence against rubbish errors
14781
14782 if No (Alias (Subp)) then
14783 return;
14784 end if;
14785
14786 -- Retrieve the resolved actual from the renaming declaration
14787 -- created for the instantiated formal.
14788
14789 Actual := Entity (Name (Parent (Parent (Subp))));
14790 Set_Entity (Def, Actual);
14791 Set_Etype (Def, Etype (Actual));
14792
14793 if Is_Global (Actual) then
14794 Ndec :=
14795 Make_Generic_Association (Loc,
14796 Selector_Name =>
14797 New_Occurrence_Of (Subp, Loc),
14798 Explicit_Generic_Actual_Parameter =>
14799 New_Occurrence_Of (Actual, Loc));
14800
14801 Set_Associated_Node
14802 (Explicit_Generic_Actual_Parameter (Ndec), Def);
14803
14804 Append (Ndec, Assoc1);
14805
14806 -- If there are other defaults, add a dummy association in case
14807 -- there are other defaulted formals with the same name.
14808
14809 elsif Present (Next (Act2)) then
14810 Ndec :=
14811 Make_Generic_Association (Loc,
14812 Selector_Name =>
14813 New_Occurrence_Of (Subp, Loc),
14814 Explicit_Generic_Actual_Parameter => Empty);
14815
14816 Append (Ndec, Assoc1);
14817 end if;
14818
14819 Next (Act2);
14820 end loop;
14821 end if;
14822
14823 if Nkind (Name (N1)) = N_Identifier
14824 and then Is_Child_Unit (Gen_Id)
14825 and then Is_Global (Gen_Id)
14826 and then Is_Generic_Unit (Scope (Gen_Id))
14827 and then In_Open_Scopes (Scope (Gen_Id))
14828 then
14829 -- This is an instantiation of a child unit within a sibling, so
14830 -- that the generic parent is in scope. An eventual instance must
14831 -- occur within the scope of an instance of the parent. Make name
14832 -- in instance into an expanded name, to preserve the identifier
14833 -- of the parent, so it can be resolved subsequently.
14834
14835 Rewrite (Name (N2),
14836 Make_Expanded_Name (Loc,
14837 Chars => Chars (Gen_Id),
14838 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
14839 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
14840 Set_Entity (Name (N2), Gen_Id);
14841
14842 Rewrite (Name (N1),
14843 Make_Expanded_Name (Loc,
14844 Chars => Chars (Gen_Id),
14845 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
14846 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
14847
14848 Set_Associated_Node (Name (N1), Name (N2));
14849 Set_Associated_Node (Prefix (Name (N1)), Empty);
14850 Set_Associated_Node
14851 (Selector_Name (Name (N1)), Selector_Name (Name (N2)));
14852 Set_Etype (Name (N1), Etype (Gen_Id));
14853 end if;
14854 end Save_Global_Defaults;
14855
14856 ----------------------------
14857 -- Save_Global_Descendant --
14858 ----------------------------
14859
14860 procedure Save_Global_Descendant (D : Union_Id) is
14861 N1 : Node_Id;
14862
14863 begin
14864 if D in Node_Range then
14865 if D = Union_Id (Empty) then
14866 null;
14867
14868 elsif Nkind (Node_Id (D)) /= N_Compilation_Unit then
14869 Save_References (Node_Id (D));
14870 end if;
14871
14872 elsif D in List_Range then
14873 pragma Assert (D /= Union_Id (No_List));
14874 -- Because No_List = Empty, which is in Node_Range above
14875
14876 if Is_Empty_List (List_Id (D)) then
14877 null;
14878
14879 else
14880 N1 := First (List_Id (D));
14881 while Present (N1) loop
14882 Save_References (N1);
14883 Next (N1);
14884 end loop;
14885 end if;
14886
14887 -- Element list or other non-node field, nothing to do
14888
14889 else
14890 null;
14891 end if;
14892 end Save_Global_Descendant;
14893
14894 ---------------------
14895 -- Save_References --
14896 ---------------------
14897
14898 -- This is the recursive procedure that does the work once the enclosing
14899 -- generic scope has been established. We have to treat specially a
14900 -- number of node rewritings that are required by semantic processing
14901 -- and which change the kind of nodes in the generic copy: typically
14902 -- constant-folding, replacing an operator node by a string literal, or
14903 -- a selected component by an expanded name. In each of those cases, the
14904 -- transformation is propagated to the generic unit.
14905
14906 procedure Save_References (N : Node_Id) is
14907 Loc : constant Source_Ptr := Sloc (N);
14908
14909 function Requires_Delayed_Save (Nod : Node_Id) return Boolean;
14910 -- Determine whether arbitrary node Nod requires delayed capture of
14911 -- global references within its aspect specifications.
14912
14913 procedure Save_References_In_Aggregate (N : Node_Id);
14914 -- Save all global references in [extension] aggregate node N
14915
14916 procedure Save_References_In_Char_Lit_Or_Op_Symbol (N : Node_Id);
14917 -- Save all global references in a character literal or operator
14918 -- symbol denoted by N.
14919
14920 procedure Save_References_In_Descendants (N : Node_Id);
14921 -- Save all global references in all descendants of node N
14922
14923 procedure Save_References_In_Identifier (N : Node_Id);
14924 -- Save all global references in identifier node N
14925
14926 procedure Save_References_In_Operator (N : Node_Id);
14927 -- Save all global references in operator node N
14928
14929 procedure Save_References_In_Pragma (Prag : Node_Id);
14930 -- Save all global references found within the expression of pragma
14931 -- Prag.
14932
14933 ---------------------------
14934 -- Requires_Delayed_Save --
14935 ---------------------------
14936
14937 function Requires_Delayed_Save (Nod : Node_Id) return Boolean is
14938 begin
14939 -- Generic packages and subprograms require delayed capture of
14940 -- global references within their aspects due to the timing of
14941 -- annotation analysis.
14942
14943 if Nkind_In (Nod, N_Generic_Package_Declaration,
14944 N_Generic_Subprogram_Declaration,
14945 N_Package_Body,
14946 N_Package_Body_Stub,
14947 N_Subprogram_Body,
14948 N_Subprogram_Body_Stub)
14949 then
14950 -- Since the capture of global references is done on the
14951 -- unanalyzed generic template, there is no information around
14952 -- to infer the context. Use the Associated_Entity linkages to
14953 -- peek into the analyzed generic copy and determine what the
14954 -- template corresponds to.
14955
14956 if Nod = Templ then
14957 return
14958 Is_Generic_Declaration_Or_Body
14959 (Unit_Declaration_Node
14960 (Associated_Entity (Defining_Entity (Nod))));
14961
14962 -- Otherwise the generic unit being processed is not the top
14963 -- level template. It is safe to capture of global references
14964 -- within the generic unit because at this point the top level
14965 -- copy is fully analyzed.
14966
14967 else
14968 return False;
14969 end if;
14970
14971 -- Otherwise capture the global references without interference
14972
14973 else
14974 return False;
14975 end if;
14976 end Requires_Delayed_Save;
14977
14978 ----------------------------------
14979 -- Save_References_In_Aggregate --
14980 ----------------------------------
14981
14982 procedure Save_References_In_Aggregate (N : Node_Id) is
14983 Nam : Node_Id;
14984 Qual : Node_Id := Empty;
14985 Typ : Entity_Id := Empty;
14986
14987 use Atree.Unchecked_Access;
14988 -- This code section is part of implementing an untyped tree
14989 -- traversal, so it needs direct access to node fields.
14990
14991 begin
14992 N2 := Get_Associated_Node (N);
14993
14994 if Present (N2) then
14995 Typ := Etype (N2);
14996
14997 -- In an instance within a generic, use the name of the actual
14998 -- and not the original generic parameter. If the actual is
14999 -- global in the current generic it must be preserved for its
15000 -- instantiation.
15001
15002 if Nkind (Parent (Typ)) = N_Subtype_Declaration
15003 and then Present (Generic_Parent_Type (Parent (Typ)))
15004 then
15005 Typ := Base_Type (Typ);
15006 Set_Etype (N2, Typ);
15007 end if;
15008 end if;
15009
15010 if No (N2) or else No (Typ) or else not Is_Global (Typ) then
15011 Set_Associated_Node (N, Empty);
15012
15013 -- If the aggregate is an actual in a call, it has been
15014 -- resolved in the current context, to some local type. The
15015 -- enclosing call may have been disambiguated by the aggregate,
15016 -- and this disambiguation might fail at instantiation time
15017 -- because the type to which the aggregate did resolve is not
15018 -- preserved. In order to preserve some of this information,
15019 -- wrap the aggregate in a qualified expression, using the id
15020 -- of its type. For further disambiguation we qualify the type
15021 -- name with its scope (if visible) because both id's will have
15022 -- corresponding entities in an instance. This resolves most of
15023 -- the problems with missing type information on aggregates in
15024 -- instances.
15025
15026 if Present (N2)
15027 and then Nkind (N2) = Nkind (N)
15028 and then Nkind (Parent (N2)) in N_Subprogram_Call
15029 and then Present (Typ)
15030 and then Comes_From_Source (Typ)
15031 then
15032 Nam := Make_Identifier (Loc, Chars (Typ));
15033
15034 if Is_Immediately_Visible (Scope (Typ)) then
15035 Nam :=
15036 Make_Selected_Component (Loc,
15037 Prefix =>
15038 Make_Identifier (Loc, Chars (Scope (Typ))),
15039 Selector_Name => Nam);
15040 end if;
15041
15042 Qual :=
15043 Make_Qualified_Expression (Loc,
15044 Subtype_Mark => Nam,
15045 Expression => Relocate_Node (N));
15046 end if;
15047 end if;
15048
15049 Save_Global_Descendant (Field1 (N));
15050 Save_Global_Descendant (Field2 (N));
15051 Save_Global_Descendant (Field3 (N));
15052 Save_Global_Descendant (Field5 (N));
15053
15054 if Present (Qual) then
15055 Rewrite (N, Qual);
15056 end if;
15057 end Save_References_In_Aggregate;
15058
15059 ----------------------------------------------
15060 -- Save_References_In_Char_Lit_Or_Op_Symbol --
15061 ----------------------------------------------
15062
15063 procedure Save_References_In_Char_Lit_Or_Op_Symbol (N : Node_Id) is
15064 begin
15065 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
15066 Reset_Entity (N);
15067
15068 elsif Nkind (N) = N_Operator_Symbol
15069 and then Nkind (Get_Associated_Node (N)) = N_String_Literal
15070 then
15071 Change_Operator_Symbol_To_String_Literal (N);
15072 end if;
15073 end Save_References_In_Char_Lit_Or_Op_Symbol;
15074
15075 ------------------------------------
15076 -- Save_References_In_Descendants --
15077 ------------------------------------
15078
15079 procedure Save_References_In_Descendants (N : Node_Id) is
15080 use Atree.Unchecked_Access;
15081 -- This code section is part of implementing an untyped tree
15082 -- traversal, so it needs direct access to node fields.
15083
15084 begin
15085 Save_Global_Descendant (Field1 (N));
15086 Save_Global_Descendant (Field2 (N));
15087 Save_Global_Descendant (Field3 (N));
15088 Save_Global_Descendant (Field4 (N));
15089 Save_Global_Descendant (Field5 (N));
15090 end Save_References_In_Descendants;
15091
15092 -----------------------------------
15093 -- Save_References_In_Identifier --
15094 -----------------------------------
15095
15096 procedure Save_References_In_Identifier (N : Node_Id) is
15097 begin
15098 -- The node did not undergo a transformation
15099
15100 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
15101 declare
15102 Aux_N2 : constant Node_Id := Get_Associated_Node (N);
15103 Orig_N2_Parent : constant Node_Id :=
15104 Original_Node (Parent (Aux_N2));
15105 begin
15106 -- The parent of this identifier is a selected component
15107 -- which denotes a named number that was constant folded.
15108 -- Preserve the original name for ASIS and link the parent
15109 -- with its expanded name. The constant folding will be
15110 -- repeated in the instance.
15111
15112 if Nkind (Parent (N)) = N_Selected_Component
15113 and then Nkind_In (Parent (Aux_N2), N_Integer_Literal,
15114 N_Real_Literal)
15115 and then Is_Entity_Name (Orig_N2_Parent)
15116 and then Ekind (Entity (Orig_N2_Parent)) in Named_Kind
15117 and then Is_Global (Entity (Orig_N2_Parent))
15118 then
15119 N2 := Aux_N2;
15120 Set_Associated_Node
15121 (Parent (N), Original_Node (Parent (N2)));
15122
15123 -- Common case
15124
15125 else
15126 -- If this is a discriminant reference, always save it.
15127 -- It is used in the instance to find the corresponding
15128 -- discriminant positionally rather than by name.
15129
15130 Set_Original_Discriminant
15131 (N, Original_Discriminant (Get_Associated_Node (N)));
15132 end if;
15133
15134 Reset_Entity (N);
15135 end;
15136
15137 -- The analysis of the generic copy transformed the identifier
15138 -- into another construct. Propagate the changes to the template.
15139
15140 else
15141 N2 := Get_Associated_Node (N);
15142
15143 -- The identifier denotes a call to a parameterless function.
15144 -- Mark the node as resolved when the function is external.
15145
15146 if Nkind (N2) = N_Function_Call then
15147 E := Entity (Name (N2));
15148
15149 if Present (E) and then Is_Global (E) then
15150 Set_Etype (N, Etype (N2));
15151 else
15152 Set_Associated_Node (N, Empty);
15153 Set_Etype (N, Empty);
15154 end if;
15155
15156 -- The identifier denotes a named number that was constant
15157 -- folded. Preserve the original name for ASIS and undo the
15158 -- constant folding which will be repeated in the instance.
15159
15160 elsif Nkind_In (N2, N_Integer_Literal, N_Real_Literal)
15161 and then Is_Entity_Name (Original_Node (N2))
15162 then
15163 Set_Associated_Node (N, Original_Node (N2));
15164 Reset_Entity (N);
15165
15166 -- The identifier resolved to a string literal. Propagate this
15167 -- information to the generic template.
15168
15169 elsif Nkind (N2) = N_String_Literal then
15170 Rewrite (N, New_Copy (N2));
15171
15172 -- The identifier is rewritten as a dereference if it is the
15173 -- prefix of an implicit dereference. Preserve the original
15174 -- tree as the analysis of the instance will expand the node
15175 -- again, but preserve the resolved entity if it is global.
15176
15177 elsif Nkind (N2) = N_Explicit_Dereference then
15178 if Is_Entity_Name (Prefix (N2))
15179 and then Present (Entity (Prefix (N2)))
15180 and then Is_Global (Entity (Prefix (N2)))
15181 then
15182 Set_Associated_Node (N, Prefix (N2));
15183
15184 elsif Nkind (Prefix (N2)) = N_Function_Call
15185 and then Present (Entity (Name (Prefix (N2))))
15186 and then Is_Global (Entity (Name (Prefix (N2))))
15187 then
15188 Rewrite (N,
15189 Make_Explicit_Dereference (Loc,
15190 Prefix =>
15191 Make_Function_Call (Loc,
15192 Name =>
15193 New_Occurrence_Of
15194 (Entity (Name (Prefix (N2))), Loc))));
15195
15196 else
15197 Set_Associated_Node (N, Empty);
15198 Set_Etype (N, Empty);
15199 end if;
15200
15201 -- The subtype mark of a nominally unconstrained object is
15202 -- rewritten as a subtype indication using the bounds of the
15203 -- expression. Recover the original subtype mark.
15204
15205 elsif Nkind (N2) = N_Subtype_Indication
15206 and then Is_Entity_Name (Original_Node (N2))
15207 then
15208 Set_Associated_Node (N, Original_Node (N2));
15209 Reset_Entity (N);
15210 end if;
15211 end if;
15212 end Save_References_In_Identifier;
15213
15214 ---------------------------------
15215 -- Save_References_In_Operator --
15216 ---------------------------------
15217
15218 procedure Save_References_In_Operator (N : Node_Id) is
15219 begin
15220 -- The node did not undergo a transformation
15221
15222 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
15223 if Nkind (N) = N_Op_Concat then
15224 Set_Is_Component_Left_Opnd (N,
15225 Is_Component_Left_Opnd (Get_Associated_Node (N)));
15226
15227 Set_Is_Component_Right_Opnd (N,
15228 Is_Component_Right_Opnd (Get_Associated_Node (N)));
15229 end if;
15230
15231 Reset_Entity (N);
15232
15233 -- The analysis of the generic copy transformed the operator into
15234 -- some other construct. Propagate the changes to the template if
15235 -- applicable.
15236
15237 else
15238 N2 := Get_Associated_Node (N);
15239
15240 -- The operator resoved to a function call
15241
15242 if Nkind (N2) = N_Function_Call then
15243
15244 -- Add explicit qualifications in the generic template for
15245 -- all operands of universal type. This aids resolution by
15246 -- preserving the actual type of a literal or an attribute
15247 -- that yields a universal result.
15248
15249 Qualify_Universal_Operands (N, N2);
15250
15251 E := Entity (Name (N2));
15252
15253 if Present (E) and then Is_Global (E) then
15254 Set_Etype (N, Etype (N2));
15255 else
15256 Set_Associated_Node (N, Empty);
15257 Set_Etype (N, Empty);
15258 end if;
15259
15260 -- The operator was folded into a literal
15261
15262 elsif Nkind_In (N2, N_Integer_Literal,
15263 N_Real_Literal,
15264 N_String_Literal)
15265 then
15266 if Present (Original_Node (N2))
15267 and then Nkind (Original_Node (N2)) = Nkind (N)
15268 then
15269 -- Operation was constant-folded. Whenever possible,
15270 -- recover semantic information from unfolded node,
15271 -- for ASIS use.
15272
15273 Set_Associated_Node (N, Original_Node (N2));
15274
15275 if Nkind (N) = N_Op_Concat then
15276 Set_Is_Component_Left_Opnd (N,
15277 Is_Component_Left_Opnd (Get_Associated_Node (N)));
15278 Set_Is_Component_Right_Opnd (N,
15279 Is_Component_Right_Opnd (Get_Associated_Node (N)));
15280 end if;
15281
15282 Reset_Entity (N);
15283
15284 -- Propagate the constant folding back to the template
15285
15286 else
15287 Rewrite (N, New_Copy (N2));
15288 Set_Analyzed (N, False);
15289 end if;
15290
15291 -- The operator was folded into an enumeration literal. Retain
15292 -- the entity to avoid spurious ambiguities if it is overloaded
15293 -- at the point of instantiation or inlining.
15294
15295 elsif Nkind (N2) = N_Identifier
15296 and then Ekind (Entity (N2)) = E_Enumeration_Literal
15297 then
15298 Rewrite (N, New_Copy (N2));
15299 Set_Analyzed (N, False);
15300 end if;
15301 end if;
15302
15303 -- Complete the operands check if node has not been constant
15304 -- folded.
15305
15306 if Nkind (N) in N_Op then
15307 Save_Entity_Descendants (N);
15308 end if;
15309 end Save_References_In_Operator;
15310
15311 -------------------------------
15312 -- Save_References_In_Pragma --
15313 -------------------------------
15314
15315 procedure Save_References_In_Pragma (Prag : Node_Id) is
15316 Context : Node_Id;
15317 Do_Save : Boolean := True;
15318
15319 use Atree.Unchecked_Access;
15320 -- This code section is part of implementing an untyped tree
15321 -- traversal, so it needs direct access to node fields.
15322
15323 begin
15324 -- Do not save global references in pragmas generated from aspects
15325 -- because the pragmas will be regenerated at instantiation time.
15326
15327 if From_Aspect_Specification (Prag) then
15328 Do_Save := False;
15329
15330 -- The capture of global references within contract-related source
15331 -- pragmas associated with generic packages, subprograms or their
15332 -- respective bodies must be delayed due to timing of annotation
15333 -- analysis. Global references are still captured in routine
15334 -- Save_Global_References_In_Contract.
15335
15336 elsif Is_Generic_Contract_Pragma (Prag) and then Prag /= Templ then
15337 if Is_Package_Contract_Annotation (Prag) then
15338 Context := Find_Related_Package_Or_Body (Prag);
15339 else
15340 pragma Assert (Is_Subprogram_Contract_Annotation (Prag));
15341 Context := Find_Related_Declaration_Or_Body (Prag);
15342 end if;
15343
15344 -- The use of Original_Node accounts for the case when the
15345 -- related context is generic template.
15346
15347 if Requires_Delayed_Save (Original_Node (Context)) then
15348 Do_Save := False;
15349 end if;
15350 end if;
15351
15352 -- For all other cases, save all global references within the
15353 -- descendants, but skip the following semantic fields:
15354
15355 -- Field1 - Next_Pragma
15356 -- Field3 - Corresponding_Aspect
15357 -- Field5 - Next_Rep_Item
15358
15359 if Do_Save then
15360 Save_Global_Descendant (Field2 (Prag));
15361 Save_Global_Descendant (Field4 (Prag));
15362 end if;
15363 end Save_References_In_Pragma;
15364
15365 -- Start of processing for Save_References
15366
15367 begin
15368 if N = Empty then
15369 null;
15370
15371 -- Aggregates
15372
15373 elsif Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
15374 Save_References_In_Aggregate (N);
15375
15376 -- Character literals, operator symbols
15377
15378 elsif Nkind_In (N, N_Character_Literal, N_Operator_Symbol) then
15379 Save_References_In_Char_Lit_Or_Op_Symbol (N);
15380
15381 -- Defining identifiers
15382
15383 elsif Nkind (N) in N_Entity then
15384 null;
15385
15386 -- Identifiers
15387
15388 elsif Nkind (N) = N_Identifier then
15389 Save_References_In_Identifier (N);
15390
15391 -- Operators
15392
15393 elsif Nkind (N) in N_Op then
15394 Save_References_In_Operator (N);
15395
15396 -- Pragmas
15397
15398 elsif Nkind (N) = N_Pragma then
15399 Save_References_In_Pragma (N);
15400
15401 else
15402 Save_References_In_Descendants (N);
15403 end if;
15404
15405 -- Save all global references found within the aspect specifications
15406 -- of the related node.
15407
15408 if Permits_Aspect_Specifications (N) and then Has_Aspects (N) then
15409
15410 -- The capture of global references within aspects associated with
15411 -- generic packages, subprograms or their bodies must be delayed
15412 -- due to timing of annotation analysis. Global references are
15413 -- still captured in routine Save_Global_References_In_Contract.
15414
15415 if Requires_Delayed_Save (N) then
15416 null;
15417
15418 -- Otherwise save all global references within the aspects
15419
15420 else
15421 Save_Global_References_In_Aspects (N);
15422 end if;
15423 end if;
15424 end Save_References;
15425
15426 -- Start of processing for Save_Global_References
15427
15428 begin
15429 Gen_Scope := Current_Scope;
15430
15431 -- If the generic unit is a child unit, references to entities in the
15432 -- parent are treated as local, because they will be resolved anew in
15433 -- the context of the instance of the parent.
15434
15435 while Is_Child_Unit (Gen_Scope)
15436 and then Ekind (Scope (Gen_Scope)) = E_Generic_Package
15437 loop
15438 Gen_Scope := Scope (Gen_Scope);
15439 end loop;
15440
15441 Save_References (Templ);
15442 end Save_Global_References;
15443
15444 ---------------------------------------
15445 -- Save_Global_References_In_Aspects --
15446 ---------------------------------------
15447
15448 procedure Save_Global_References_In_Aspects (N : Node_Id) is
15449 Asp : Node_Id;
15450 Expr : Node_Id;
15451
15452 begin
15453 Asp := First (Aspect_Specifications (N));
15454 while Present (Asp) loop
15455 Expr := Expression (Asp);
15456
15457 if Present (Expr) then
15458 Save_Global_References (Expr);
15459 end if;
15460
15461 Next (Asp);
15462 end loop;
15463 end Save_Global_References_In_Aspects;
15464
15465 ------------------------------------------
15466 -- Set_Copied_Sloc_For_Inherited_Pragma --
15467 ------------------------------------------
15468
15469 procedure Set_Copied_Sloc_For_Inherited_Pragma
15470 (N : Node_Id;
15471 E : Entity_Id)
15472 is
15473 begin
15474 Create_Instantiation_Source (N, E,
15475 Inlined_Body => False,
15476 Inherited_Pragma => True,
15477 Factor => S_Adjustment);
15478 end Set_Copied_Sloc_For_Inherited_Pragma;
15479
15480 --------------------------------------
15481 -- Set_Copied_Sloc_For_Inlined_Body --
15482 --------------------------------------
15483
15484 procedure Set_Copied_Sloc_For_Inlined_Body (N : Node_Id; E : Entity_Id) is
15485 begin
15486 Create_Instantiation_Source (N, E,
15487 Inlined_Body => True,
15488 Inherited_Pragma => False,
15489 Factor => S_Adjustment);
15490 end Set_Copied_Sloc_For_Inlined_Body;
15491
15492 ---------------------
15493 -- Set_Instance_Of --
15494 ---------------------
15495
15496 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id) is
15497 begin
15498 Generic_Renamings.Table (Generic_Renamings.Last) := (A, B, Assoc_Null);
15499 Generic_Renamings_HTable.Set (Generic_Renamings.Last);
15500 Generic_Renamings.Increment_Last;
15501 end Set_Instance_Of;
15502
15503 --------------------
15504 -- Set_Next_Assoc --
15505 --------------------
15506
15507 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr) is
15508 begin
15509 Generic_Renamings.Table (E).Next_In_HTable := Next;
15510 end Set_Next_Assoc;
15511
15512 -------------------
15513 -- Start_Generic --
15514 -------------------
15515
15516 procedure Start_Generic is
15517 begin
15518 -- ??? More things could be factored out in this routine.
15519 -- Should probably be done at a later stage.
15520
15521 Generic_Flags.Append (Inside_A_Generic);
15522 Inside_A_Generic := True;
15523
15524 Expander_Mode_Save_And_Set (False);
15525 end Start_Generic;
15526
15527 ----------------------
15528 -- Set_Instance_Env --
15529 ----------------------
15530
15531 -- WARNING: This routine manages SPARK regions
15532
15533 procedure Set_Instance_Env
15534 (Gen_Unit : Entity_Id;
15535 Act_Unit : Entity_Id)
15536 is
15537 Saved_AE : constant Boolean := Assertions_Enabled;
15538 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
15539 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
15540 -- Save the SPARK mode-related data because utilizing the configuration
15541 -- values of pragmas and switches will eliminate any previously set
15542 -- SPARK_Mode.
15543
15544 begin
15545 -- Regardless of the current mode, predefined units are analyzed in the
15546 -- most current Ada mode, and earlier version Ada checks do not apply
15547 -- to predefined units. Nothing needs to be done for non-internal units.
15548 -- These are always analyzed in the current mode.
15549
15550 if In_Internal_Unit (Gen_Unit) then
15551 Set_Opt_Config_Switches (True, Current_Sem_Unit = Main_Unit);
15552
15553 -- In Ada2012 we may want to enable assertions in an instance of a
15554 -- predefined unit, in which case we need to preserve the current
15555 -- setting for the Assertions_Enabled flag. This will become more
15556 -- critical when pre/postconditions are added to predefined units,
15557 -- as is already the case for some numeric libraries.
15558
15559 if Ada_Version >= Ada_2012 then
15560 Assertions_Enabled := Saved_AE;
15561 end if;
15562
15563 -- Reinstall the SPARK_Mode which was in effect at the point of
15564 -- instantiation.
15565
15566 Install_SPARK_Mode (Saved_SM, Saved_SMP);
15567 end if;
15568
15569 Current_Instantiated_Parent :=
15570 (Gen_Id => Gen_Unit,
15571 Act_Id => Act_Unit,
15572 Next_In_HTable => Assoc_Null);
15573 end Set_Instance_Env;
15574
15575 -----------------
15576 -- Switch_View --
15577 -----------------
15578
15579 procedure Switch_View (T : Entity_Id) is
15580 BT : constant Entity_Id := Base_Type (T);
15581 Priv_Elmt : Elmt_Id := No_Elmt;
15582 Priv_Sub : Entity_Id;
15583
15584 begin
15585 -- T may be private but its base type may have been exchanged through
15586 -- some other occurrence, in which case there is nothing to switch
15587 -- besides T itself. Note that a private dependent subtype of a private
15588 -- type might not have been switched even if the base type has been,
15589 -- because of the last branch of Check_Private_View (see comment there).
15590
15591 if not Is_Private_Type (BT) then
15592 Prepend_Elmt (Full_View (T), Exchanged_Views);
15593 Exchange_Declarations (T);
15594 return;
15595 end if;
15596
15597 Priv_Elmt := First_Elmt (Private_Dependents (BT));
15598
15599 if Present (Full_View (BT)) then
15600 Prepend_Elmt (Full_View (BT), Exchanged_Views);
15601 Exchange_Declarations (BT);
15602 end if;
15603
15604 while Present (Priv_Elmt) loop
15605 Priv_Sub := (Node (Priv_Elmt));
15606
15607 -- We avoid flipping the subtype if the Etype of its full view is
15608 -- private because this would result in a malformed subtype. This
15609 -- occurs when the Etype of the subtype full view is the full view of
15610 -- the base type (and since the base types were just switched, the
15611 -- subtype is pointing to the wrong view). This is currently the case
15612 -- for tagged record types, access types (maybe more?) and needs to
15613 -- be resolved. ???
15614
15615 if Present (Full_View (Priv_Sub))
15616 and then not Is_Private_Type (Etype (Full_View (Priv_Sub)))
15617 then
15618 Prepend_Elmt (Full_View (Priv_Sub), Exchanged_Views);
15619 Exchange_Declarations (Priv_Sub);
15620 end if;
15621
15622 Next_Elmt (Priv_Elmt);
15623 end loop;
15624 end Switch_View;
15625
15626 -----------------
15627 -- True_Parent --
15628 -----------------
15629
15630 function True_Parent (N : Node_Id) return Node_Id is
15631 begin
15632 if Nkind (Parent (N)) = N_Subunit then
15633 return Parent (Corresponding_Stub (Parent (N)));
15634 else
15635 return Parent (N);
15636 end if;
15637 end True_Parent;
15638
15639 -----------------------------
15640 -- Valid_Default_Attribute --
15641 -----------------------------
15642
15643 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id) is
15644 Attr_Id : constant Attribute_Id :=
15645 Get_Attribute_Id (Attribute_Name (Def));
15646 T : constant Entity_Id := Entity (Prefix (Def));
15647 Is_Fun : constant Boolean := (Ekind (Nam) = E_Function);
15648 F : Entity_Id;
15649 Num_F : Nat;
15650 OK : Boolean;
15651
15652 begin
15653 if No (T) or else T = Any_Id then
15654 return;
15655 end if;
15656
15657 Num_F := 0;
15658 F := First_Formal (Nam);
15659 while Present (F) loop
15660 Num_F := Num_F + 1;
15661 Next_Formal (F);
15662 end loop;
15663
15664 case Attr_Id is
15665 when Attribute_Adjacent
15666 | Attribute_Ceiling
15667 | Attribute_Copy_Sign
15668 | Attribute_Floor
15669 | Attribute_Fraction
15670 | Attribute_Machine
15671 | Attribute_Model
15672 | Attribute_Remainder
15673 | Attribute_Rounding
15674 | Attribute_Unbiased_Rounding
15675 =>
15676 OK := Is_Fun
15677 and then Num_F = 1
15678 and then Is_Floating_Point_Type (T);
15679
15680 when Attribute_Image
15681 | Attribute_Pred
15682 | Attribute_Succ
15683 | Attribute_Value
15684 | Attribute_Wide_Image
15685 | Attribute_Wide_Value
15686 =>
15687 OK := Is_Fun and then Num_F = 1 and then Is_Scalar_Type (T);
15688
15689 when Attribute_Max
15690 | Attribute_Min
15691 =>
15692 OK := Is_Fun and then Num_F = 2 and then Is_Scalar_Type (T);
15693
15694 when Attribute_Input =>
15695 OK := (Is_Fun and then Num_F = 1);
15696
15697 when Attribute_Output
15698 | Attribute_Read
15699 | Attribute_Write
15700 =>
15701 OK := not Is_Fun and then Num_F = 2;
15702
15703 when others =>
15704 OK := False;
15705 end case;
15706
15707 if not OK then
15708 Error_Msg_N
15709 ("attribute reference has wrong profile for subprogram", Def);
15710 end if;
15711 end Valid_Default_Attribute;
15712
15713 end Sem_Ch12;