[Ada] Warning on use of predefined operations on an actual fixed-point type
[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 Provide_Completing_Bodies (N : Node_Id);
843 -- Generate completing bodies for all subprograms found within package or
844 -- subprogram declaration N.
845
846 procedure Remove_Parent (In_Body : Boolean := False);
847 -- Reverse effect after instantiation of child is complete
848
849 procedure Restore_Hidden_Primitives (Prims_List : in out Elist_Id);
850 -- Restore suffix 'P' to primitives of Prims_List and leave Prims_List
851 -- set to No_Elist.
852
853 procedure Set_Instance_Env
854 (Gen_Unit : Entity_Id;
855 Act_Unit : Entity_Id);
856 -- Save current instance on saved environment, to be used to determine
857 -- the global status of entities in nested instances. Part of Save_Env.
858 -- called after verifying that the generic unit is legal for the instance,
859 -- The procedure also examines whether the generic unit is a predefined
860 -- unit, in order to set configuration switches accordingly. As a result
861 -- the procedure must be called after analyzing and freezing the actuals.
862
863 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id);
864 -- Associate analyzed generic parameter with corresponding instance. Used
865 -- for semantic checks at instantiation time.
866
867 function True_Parent (N : Node_Id) return Node_Id;
868 -- For a subunit, return parent of corresponding stub, else return
869 -- parent of node.
870
871 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id);
872 -- Verify that an attribute that appears as the default for a formal
873 -- subprogram is a function or procedure with the correct profile.
874
875 -------------------------------------------
876 -- Data Structures for Generic Renamings --
877 -------------------------------------------
878
879 -- The map Generic_Renamings associates generic entities with their
880 -- corresponding actuals. Currently used to validate type instances. It
881 -- will eventually be used for all generic parameters to eliminate the
882 -- need for overload resolution in the instance.
883
884 type Assoc_Ptr is new Int;
885
886 Assoc_Null : constant Assoc_Ptr := -1;
887
888 type Assoc is record
889 Gen_Id : Entity_Id;
890 Act_Id : Entity_Id;
891 Next_In_HTable : Assoc_Ptr;
892 end record;
893
894 package Generic_Renamings is new Table.Table
895 (Table_Component_Type => Assoc,
896 Table_Index_Type => Assoc_Ptr,
897 Table_Low_Bound => 0,
898 Table_Initial => 10,
899 Table_Increment => 100,
900 Table_Name => "Generic_Renamings");
901
902 -- Variable to hold enclosing instantiation. When the environment is
903 -- saved for a subprogram inlining, the corresponding Act_Id is empty.
904
905 Current_Instantiated_Parent : Assoc := (Empty, Empty, Assoc_Null);
906
907 -- Hash table for associations
908
909 HTable_Size : constant := 37;
910 type HTable_Range is range 0 .. HTable_Size - 1;
911
912 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr);
913 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr;
914 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id;
915 function Hash (F : Entity_Id) return HTable_Range;
916
917 package Generic_Renamings_HTable is new GNAT.HTable.Static_HTable (
918 Header_Num => HTable_Range,
919 Element => Assoc,
920 Elmt_Ptr => Assoc_Ptr,
921 Null_Ptr => Assoc_Null,
922 Set_Next => Set_Next_Assoc,
923 Next => Next_Assoc,
924 Key => Entity_Id,
925 Get_Key => Get_Gen_Id,
926 Hash => Hash,
927 Equal => "=");
928
929 Exchanged_Views : Elist_Id;
930 -- This list holds the private views that have been exchanged during
931 -- instantiation to restore the visibility of the generic declaration.
932 -- (see comments above). After instantiation, the current visibility is
933 -- reestablished by means of a traversal of this list.
934
935 Hidden_Entities : Elist_Id;
936 -- This list holds the entities of the current scope that are removed
937 -- from immediate visibility when instantiating a child unit. Their
938 -- visibility is restored in Remove_Parent.
939
940 -- Because instantiations can be recursive, the following must be saved
941 -- on entry and restored on exit from an instantiation (spec or body).
942 -- This is done by the two procedures Save_Env and Restore_Env. For
943 -- package and subprogram instantiations (but not for the body instances)
944 -- the action of Save_Env is done in two steps: Init_Env is called before
945 -- Check_Generic_Child_Unit, because setting the parent instances requires
946 -- that the visibility data structures be properly initialized. Once the
947 -- generic is unit is validated, Set_Instance_Env completes Save_Env.
948
949 Parent_Unit_Visible : Boolean := False;
950 -- Parent_Unit_Visible is used when the generic is a child unit, and
951 -- indicates whether the ultimate parent of the generic is visible in the
952 -- instantiation environment. It is used to reset the visibility of the
953 -- parent at the end of the instantiation (see Remove_Parent).
954
955 Instance_Parent_Unit : Entity_Id := Empty;
956 -- This records the ultimate parent unit of an instance of a generic
957 -- child unit and is used in conjunction with Parent_Unit_Visible to
958 -- indicate the unit to which the Parent_Unit_Visible flag corresponds.
959
960 type Instance_Env is record
961 Instantiated_Parent : Assoc;
962 Exchanged_Views : Elist_Id;
963 Hidden_Entities : Elist_Id;
964 Current_Sem_Unit : Unit_Number_Type;
965 Parent_Unit_Visible : Boolean := False;
966 Instance_Parent_Unit : Entity_Id := Empty;
967 Switches : Config_Switches_Type;
968 end record;
969
970 package Instance_Envs is new Table.Table (
971 Table_Component_Type => Instance_Env,
972 Table_Index_Type => Int,
973 Table_Low_Bound => 0,
974 Table_Initial => 32,
975 Table_Increment => 100,
976 Table_Name => "Instance_Envs");
977
978 procedure Restore_Private_Views
979 (Pack_Id : Entity_Id;
980 Is_Package : Boolean := True);
981 -- Restore the private views of external types, and unmark the generic
982 -- renamings of actuals, so that they become compatible subtypes again.
983 -- For subprograms, Pack_Id is the package constructed to hold the
984 -- renamings.
985
986 procedure Switch_View (T : Entity_Id);
987 -- Switch the partial and full views of a type and its private
988 -- dependents (i.e. its subtypes and derived types).
989
990 ------------------------------------
991 -- Structures for Error Reporting --
992 ------------------------------------
993
994 Instantiation_Node : Node_Id;
995 -- Used by subprograms that validate instantiation of formal parameters
996 -- where there might be no actual on which to place the error message.
997 -- Also used to locate the instantiation node for generic subunits.
998
999 Instantiation_Error : exception;
1000 -- When there is a semantic error in the generic parameter matching,
1001 -- there is no point in continuing the instantiation, because the
1002 -- number of cascaded errors is unpredictable. This exception aborts
1003 -- the instantiation process altogether.
1004
1005 S_Adjustment : Sloc_Adjustment;
1006 -- Offset created for each node in an instantiation, in order to keep
1007 -- track of the source position of the instantiation in each of its nodes.
1008 -- A subsequent semantic error or warning on a construct of the instance
1009 -- points to both places: the original generic node, and the point of
1010 -- instantiation. See Sinput and Sinput.L for additional details.
1011
1012 ------------------------------------------------------------
1013 -- Data structure for keeping track when inside a Generic --
1014 ------------------------------------------------------------
1015
1016 -- The following table is used to save values of the Inside_A_Generic
1017 -- flag (see spec of Sem) when they are saved by Start_Generic.
1018
1019 package Generic_Flags is new Table.Table (
1020 Table_Component_Type => Boolean,
1021 Table_Index_Type => Int,
1022 Table_Low_Bound => 0,
1023 Table_Initial => 32,
1024 Table_Increment => 200,
1025 Table_Name => "Generic_Flags");
1026
1027 ---------------------------
1028 -- Abandon_Instantiation --
1029 ---------------------------
1030
1031 procedure Abandon_Instantiation (N : Node_Id) is
1032 begin
1033 Error_Msg_N ("\instantiation abandoned!", N);
1034 raise Instantiation_Error;
1035 end Abandon_Instantiation;
1036
1037 --------------------------------
1038 -- Add_Pending_Instantiation --
1039 --------------------------------
1040
1041 procedure Add_Pending_Instantiation (Inst : Node_Id; Act_Decl : Node_Id) is
1042 begin
1043
1044 -- Add to the instantiation node and the corresponding unit declaration
1045 -- the current values of global flags to be used when analyzing the
1046 -- instance body.
1047
1048 Pending_Instantiations.Append
1049 ((Inst_Node => Inst,
1050 Act_Decl => Act_Decl,
1051 Expander_Status => Expander_Active,
1052 Current_Sem_Unit => Current_Sem_Unit,
1053 Scope_Suppress => Scope_Suppress,
1054 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
1055 Version => Ada_Version,
1056 Version_Pragma => Ada_Version_Pragma,
1057 Warnings => Save_Warnings,
1058 SPARK_Mode => SPARK_Mode,
1059 SPARK_Mode_Pragma => SPARK_Mode_Pragma));
1060 end Add_Pending_Instantiation;
1061
1062 ----------------------------------
1063 -- Adjust_Inherited_Pragma_Sloc --
1064 ----------------------------------
1065
1066 procedure Adjust_Inherited_Pragma_Sloc (N : Node_Id) is
1067 begin
1068 Adjust_Instantiation_Sloc (N, S_Adjustment);
1069 end Adjust_Inherited_Pragma_Sloc;
1070
1071 --------------------------
1072 -- Analyze_Associations --
1073 --------------------------
1074
1075 function Analyze_Associations
1076 (I_Node : Node_Id;
1077 Formals : List_Id;
1078 F_Copy : List_Id) return List_Id
1079 is
1080 Actuals_To_Freeze : constant Elist_Id := New_Elmt_List;
1081 Assoc_List : constant List_Id := New_List;
1082 Default_Actuals : constant List_Id := New_List;
1083 Gen_Unit : constant Entity_Id :=
1084 Defining_Entity (Parent (F_Copy));
1085
1086 Actuals : List_Id;
1087 Actual : Node_Id;
1088 Analyzed_Formal : Node_Id;
1089 First_Named : Node_Id := Empty;
1090 Formal : Node_Id;
1091 Match : Node_Id;
1092 Named : Node_Id;
1093 Saved_Formal : Node_Id;
1094
1095 Default_Formals : constant List_Id := New_List;
1096 -- If an Others_Choice is present, some of the formals may be defaulted.
1097 -- To simplify the treatment of visibility in an instance, we introduce
1098 -- individual defaults for each such formal. These defaults are
1099 -- appended to the list of associations and replace the Others_Choice.
1100
1101 Found_Assoc : Node_Id;
1102 -- Association for the current formal being match. Empty if there are
1103 -- no remaining actuals, or if there is no named association with the
1104 -- name of the formal.
1105
1106 Is_Named_Assoc : Boolean;
1107 Num_Matched : Nat := 0;
1108 Num_Actuals : Nat := 0;
1109
1110 Others_Present : Boolean := False;
1111 Others_Choice : Node_Id := Empty;
1112 -- In Ada 2005, indicates partial parameterization of a formal
1113 -- package. As usual an other association must be last in the list.
1114
1115 procedure Check_Fixed_Point_Actual (Actual : Node_Id);
1116 -- Warn if an actual fixed-point type has user-defined arithmetic
1117 -- operations, but there is no corresponding formal in the generic,
1118 -- in which case the predefined operations will be used. This merits
1119 -- a warning because of the special semantics of fixed point ops.
1120
1121 procedure Check_Overloaded_Formal_Subprogram (Formal : Entity_Id);
1122 -- Apply RM 12.3(9): if a formal subprogram is overloaded, the instance
1123 -- cannot have a named association for it. AI05-0025 extends this rule
1124 -- to formals of formal packages by AI05-0025, and it also applies to
1125 -- box-initialized formals.
1126
1127 function Has_Fully_Defined_Profile (Subp : Entity_Id) return Boolean;
1128 -- Determine whether the parameter types and the return type of Subp
1129 -- are fully defined at the point of instantiation.
1130
1131 function Matching_Actual
1132 (F : Entity_Id;
1133 A_F : Entity_Id) return Node_Id;
1134 -- Find actual that corresponds to a given a formal parameter. If the
1135 -- actuals are positional, return the next one, if any. If the actuals
1136 -- are named, scan the parameter associations to find the right one.
1137 -- A_F is the corresponding entity in the analyzed generic, which is
1138 -- placed on the selector name for ASIS use.
1139 --
1140 -- In Ada 2005, a named association may be given with a box, in which
1141 -- case Matching_Actual sets Found_Assoc to the generic association,
1142 -- but return Empty for the actual itself. In this case the code below
1143 -- creates a corresponding declaration for the formal.
1144
1145 function Partial_Parameterization return Boolean;
1146 -- Ada 2005: if no match is found for a given formal, check if the
1147 -- association for it includes a box, or whether the associations
1148 -- include an Others clause.
1149
1150 procedure Process_Default (F : Entity_Id);
1151 -- Add a copy of the declaration of generic formal F to the list of
1152 -- associations, and add an explicit box association for F if there
1153 -- is none yet, and the default comes from an Others_Choice.
1154
1155 function Renames_Standard_Subprogram (Subp : Entity_Id) return Boolean;
1156 -- Determine whether Subp renames one of the subprograms defined in the
1157 -- generated package Standard.
1158
1159 procedure Set_Analyzed_Formal;
1160 -- Find the node in the generic copy that corresponds to a given formal.
1161 -- The semantic information on this node is used to perform legality
1162 -- checks on the actuals. Because semantic analysis can introduce some
1163 -- anonymous entities or modify the declaration node itself, the
1164 -- correspondence between the two lists is not one-one. In addition to
1165 -- anonymous types, the presence a formal equality will introduce an
1166 -- implicit declaration for the corresponding inequality.
1167
1168 ----------------------------------------
1169 -- Check_Overloaded_Formal_Subprogram --
1170 ----------------------------------------
1171
1172 procedure Check_Overloaded_Formal_Subprogram (Formal : Entity_Id) is
1173 Temp_Formal : Entity_Id;
1174
1175 begin
1176 Temp_Formal := First (Formals);
1177 while Present (Temp_Formal) loop
1178 if Nkind (Temp_Formal) in N_Formal_Subprogram_Declaration
1179 and then Temp_Formal /= Formal
1180 and then
1181 Chars (Defining_Unit_Name (Specification (Formal))) =
1182 Chars (Defining_Unit_Name (Specification (Temp_Formal)))
1183 then
1184 if Present (Found_Assoc) then
1185 Error_Msg_N
1186 ("named association not allowed for overloaded formal",
1187 Found_Assoc);
1188
1189 else
1190 Error_Msg_N
1191 ("named association not allowed for overloaded formal",
1192 Others_Choice);
1193 end if;
1194
1195 Abandon_Instantiation (Instantiation_Node);
1196 end if;
1197
1198 Next (Temp_Formal);
1199 end loop;
1200 end Check_Overloaded_Formal_Subprogram;
1201
1202 -------------------------------
1203 -- Check_Fixed_Point_Actual --
1204 -------------------------------
1205
1206 procedure Check_Fixed_Point_Actual (Actual : Node_Id) is
1207 Typ : constant Entity_Id := Entity (Actual);
1208 Prims : constant Elist_Id := Collect_Primitive_Operations (Typ);
1209 Elem : Elmt_Id;
1210 Formal : Node_Id;
1211 Op : Entity_Id;
1212
1213 begin
1214 -- Locate primitive operations of the type that are arithmetic
1215 -- operations.
1216
1217 Elem := First_Elmt (Prims);
1218 while Present (Elem) loop
1219 if Nkind (Node (Elem)) = N_Defining_Operator_Symbol then
1220
1221 -- Check whether the generic unit has a formal subprogram of
1222 -- the same name. This does not check types but is good enough
1223 -- to justify a warning.
1224
1225 Formal := First_Non_Pragma (Formals);
1226 Op := Alias (Node (Elem));
1227
1228 while Present (Formal) loop
1229 if Nkind (Formal) = N_Formal_Concrete_Subprogram_Declaration
1230 and then Chars (Defining_Entity (Formal)) =
1231 Chars (Node (Elem))
1232 then
1233 exit;
1234
1235 elsif Nkind (Formal) = N_Formal_Package_Declaration then
1236 declare
1237 Assoc : Node_Id;
1238 Ent : Entity_Id;
1239
1240 begin
1241 -- Locate corresponding actual, and check whether it
1242 -- includes a fixed-point type.
1243
1244 Assoc := First (Assoc_List);
1245 while Present (Assoc) loop
1246 exit when
1247 Nkind (Assoc) = N_Package_Renaming_Declaration
1248 and then Chars (Defining_Unit_Name (Assoc)) =
1249 Chars (Defining_Identifier (Formal));
1250
1251 Next (Assoc);
1252 end loop;
1253
1254 if Present (Assoc) then
1255
1256 -- If formal package declares a fixed-point type,
1257 -- and the user-defined operator is derived from
1258 -- a generic instance package, the fixed-point type
1259 -- does not use the corresponding predefined op.
1260
1261 Ent := First_Entity (Entity (Name (Assoc)));
1262 while Present (Ent) loop
1263 if Is_Fixed_Point_Type (Ent)
1264 and then Present (Op)
1265 and then Is_Generic_Instance (Scope (Op))
1266 then
1267 return;
1268 end if;
1269
1270 Next_Entity (Ent);
1271 end loop;
1272 end if;
1273 end;
1274 end if;
1275
1276 Next (Formal);
1277 end loop;
1278
1279 if No (Formal) then
1280 Error_Msg_Sloc := Sloc (Node (Elem));
1281 Error_Msg_NE
1282 ("?instance uses predefined operation, "
1283 & "not primitive operation&#",
1284 Actual, Node (Elem));
1285 end if;
1286 end if;
1287
1288 Next_Elmt (Elem);
1289 end loop;
1290 end Check_Fixed_Point_Actual;
1291
1292 -------------------------------
1293 -- Has_Fully_Defined_Profile --
1294 -------------------------------
1295
1296 function Has_Fully_Defined_Profile (Subp : Entity_Id) return Boolean is
1297 function Is_Fully_Defined_Type (Typ : Entity_Id) return Boolean;
1298 -- Determine whethet type Typ is fully defined
1299
1300 ---------------------------
1301 -- Is_Fully_Defined_Type --
1302 ---------------------------
1303
1304 function Is_Fully_Defined_Type (Typ : Entity_Id) return Boolean is
1305 begin
1306 -- A private type without a full view is not fully defined
1307
1308 if Is_Private_Type (Typ)
1309 and then No (Full_View (Typ))
1310 then
1311 return False;
1312
1313 -- An incomplete type is never fully defined
1314
1315 elsif Is_Incomplete_Type (Typ) then
1316 return False;
1317
1318 -- All other types are fully defined
1319
1320 else
1321 return True;
1322 end if;
1323 end Is_Fully_Defined_Type;
1324
1325 -- Local declarations
1326
1327 Param : Entity_Id;
1328
1329 -- Start of processing for Has_Fully_Defined_Profile
1330
1331 begin
1332 -- Check the parameters
1333
1334 Param := First_Formal (Subp);
1335 while Present (Param) loop
1336 if not Is_Fully_Defined_Type (Etype (Param)) then
1337 return False;
1338 end if;
1339
1340 Next_Formal (Param);
1341 end loop;
1342
1343 -- Check the return type
1344
1345 return Is_Fully_Defined_Type (Etype (Subp));
1346 end Has_Fully_Defined_Profile;
1347
1348 ---------------------
1349 -- Matching_Actual --
1350 ---------------------
1351
1352 function Matching_Actual
1353 (F : Entity_Id;
1354 A_F : Entity_Id) return Node_Id
1355 is
1356 Prev : Node_Id;
1357 Act : Node_Id;
1358
1359 begin
1360 Is_Named_Assoc := False;
1361
1362 -- End of list of purely positional parameters
1363
1364 if No (Actual) or else Nkind (Actual) = N_Others_Choice then
1365 Found_Assoc := Empty;
1366 Act := Empty;
1367
1368 -- Case of positional parameter corresponding to current formal
1369
1370 elsif No (Selector_Name (Actual)) then
1371 Found_Assoc := Actual;
1372 Act := Explicit_Generic_Actual_Parameter (Actual);
1373 Num_Matched := Num_Matched + 1;
1374 Next (Actual);
1375
1376 -- Otherwise scan list of named actuals to find the one with the
1377 -- desired name. All remaining actuals have explicit names.
1378
1379 else
1380 Is_Named_Assoc := True;
1381 Found_Assoc := Empty;
1382 Act := Empty;
1383 Prev := Empty;
1384
1385 while Present (Actual) loop
1386 if Nkind (Actual) = N_Others_Choice then
1387 Found_Assoc := Empty;
1388 Act := Empty;
1389
1390 elsif Chars (Selector_Name (Actual)) = Chars (F) then
1391 Set_Entity (Selector_Name (Actual), A_F);
1392 Set_Etype (Selector_Name (Actual), Etype (A_F));
1393 Generate_Reference (A_F, Selector_Name (Actual));
1394
1395 Found_Assoc := Actual;
1396 Act := Explicit_Generic_Actual_Parameter (Actual);
1397 Num_Matched := Num_Matched + 1;
1398 exit;
1399 end if;
1400
1401 Prev := Actual;
1402 Next (Actual);
1403 end loop;
1404
1405 -- Reset for subsequent searches. In most cases the named
1406 -- associations are in order. If they are not, we reorder them
1407 -- to avoid scanning twice the same actual. This is not just a
1408 -- question of efficiency: there may be multiple defaults with
1409 -- boxes that have the same name. In a nested instantiation we
1410 -- insert actuals for those defaults, and cannot rely on their
1411 -- names to disambiguate them.
1412
1413 if Actual = First_Named then
1414 Next (First_Named);
1415
1416 elsif Present (Actual) then
1417 Insert_Before (First_Named, Remove_Next (Prev));
1418 end if;
1419
1420 Actual := First_Named;
1421 end if;
1422
1423 if Is_Entity_Name (Act) and then Present (Entity (Act)) then
1424 Set_Used_As_Generic_Actual (Entity (Act));
1425 end if;
1426
1427 return Act;
1428 end Matching_Actual;
1429
1430 ------------------------------
1431 -- Partial_Parameterization --
1432 ------------------------------
1433
1434 function Partial_Parameterization return Boolean is
1435 begin
1436 return Others_Present
1437 or else (Present (Found_Assoc) and then Box_Present (Found_Assoc));
1438 end Partial_Parameterization;
1439
1440 ---------------------
1441 -- Process_Default --
1442 ---------------------
1443
1444 procedure Process_Default (F : Entity_Id) is
1445 Loc : constant Source_Ptr := Sloc (I_Node);
1446 F_Id : constant Entity_Id := Defining_Entity (F);
1447 Decl : Node_Id;
1448 Default : Node_Id;
1449 Id : Entity_Id;
1450
1451 begin
1452 -- Append copy of formal declaration to associations, and create new
1453 -- defining identifier for it.
1454
1455 Decl := New_Copy_Tree (F);
1456 Id := Make_Defining_Identifier (Sloc (F_Id), Chars (F_Id));
1457
1458 if Nkind (F) in N_Formal_Subprogram_Declaration then
1459 Set_Defining_Unit_Name (Specification (Decl), Id);
1460
1461 else
1462 Set_Defining_Identifier (Decl, Id);
1463 end if;
1464
1465 Append (Decl, Assoc_List);
1466
1467 if No (Found_Assoc) then
1468 Default :=
1469 Make_Generic_Association (Loc,
1470 Selector_Name =>
1471 New_Occurrence_Of (Id, Loc),
1472 Explicit_Generic_Actual_Parameter => Empty);
1473 Set_Box_Present (Default);
1474 Append (Default, Default_Formals);
1475 end if;
1476 end Process_Default;
1477
1478 ---------------------------------
1479 -- Renames_Standard_Subprogram --
1480 ---------------------------------
1481
1482 function Renames_Standard_Subprogram (Subp : Entity_Id) return Boolean is
1483 Id : Entity_Id;
1484
1485 begin
1486 Id := Alias (Subp);
1487 while Present (Id) loop
1488 if Scope (Id) = Standard_Standard then
1489 return True;
1490 end if;
1491
1492 Id := Alias (Id);
1493 end loop;
1494
1495 return False;
1496 end Renames_Standard_Subprogram;
1497
1498 -------------------------
1499 -- Set_Analyzed_Formal --
1500 -------------------------
1501
1502 procedure Set_Analyzed_Formal is
1503 Kind : Node_Kind;
1504
1505 begin
1506 while Present (Analyzed_Formal) loop
1507 Kind := Nkind (Analyzed_Formal);
1508
1509 case Nkind (Formal) is
1510 when N_Formal_Subprogram_Declaration =>
1511 exit when Kind in N_Formal_Subprogram_Declaration
1512 and then
1513 Chars
1514 (Defining_Unit_Name (Specification (Formal))) =
1515 Chars
1516 (Defining_Unit_Name (Specification (Analyzed_Formal)));
1517
1518 when N_Formal_Package_Declaration =>
1519 exit when Nkind_In (Kind, N_Formal_Package_Declaration,
1520 N_Generic_Package_Declaration,
1521 N_Package_Declaration);
1522
1523 when N_Use_Package_Clause
1524 | N_Use_Type_Clause
1525 =>
1526 exit;
1527
1528 when others =>
1529
1530 -- Skip freeze nodes, and nodes inserted to replace
1531 -- unrecognized pragmas.
1532
1533 exit when
1534 Kind not in N_Formal_Subprogram_Declaration
1535 and then not Nkind_In (Kind, N_Subprogram_Declaration,
1536 N_Freeze_Entity,
1537 N_Null_Statement,
1538 N_Itype_Reference)
1539 and then Chars (Defining_Identifier (Formal)) =
1540 Chars (Defining_Identifier (Analyzed_Formal));
1541 end case;
1542
1543 Next (Analyzed_Formal);
1544 end loop;
1545 end Set_Analyzed_Formal;
1546
1547 -- Start of processing for Analyze_Associations
1548
1549 begin
1550 Actuals := Generic_Associations (I_Node);
1551
1552 if Present (Actuals) then
1553
1554 -- Check for an Others choice, indicating a partial parameterization
1555 -- for a formal package.
1556
1557 Actual := First (Actuals);
1558 while Present (Actual) loop
1559 if Nkind (Actual) = N_Others_Choice then
1560 Others_Present := True;
1561 Others_Choice := Actual;
1562
1563 if Present (Next (Actual)) then
1564 Error_Msg_N ("others must be last association", Actual);
1565 end if;
1566
1567 -- This subprogram is used both for formal packages and for
1568 -- instantiations. For the latter, associations must all be
1569 -- explicit.
1570
1571 if Nkind (I_Node) /= N_Formal_Package_Declaration
1572 and then Comes_From_Source (I_Node)
1573 then
1574 Error_Msg_N
1575 ("others association not allowed in an instance",
1576 Actual);
1577 end if;
1578
1579 -- In any case, nothing to do after the others association
1580
1581 exit;
1582
1583 elsif Box_Present (Actual)
1584 and then Comes_From_Source (I_Node)
1585 and then Nkind (I_Node) /= N_Formal_Package_Declaration
1586 then
1587 Error_Msg_N
1588 ("box association not allowed in an instance", Actual);
1589 end if;
1590
1591 Next (Actual);
1592 end loop;
1593
1594 -- If named associations are present, save first named association
1595 -- (it may of course be Empty) to facilitate subsequent name search.
1596
1597 First_Named := First (Actuals);
1598 while Present (First_Named)
1599 and then Nkind (First_Named) /= N_Others_Choice
1600 and then No (Selector_Name (First_Named))
1601 loop
1602 Num_Actuals := Num_Actuals + 1;
1603 Next (First_Named);
1604 end loop;
1605 end if;
1606
1607 Named := First_Named;
1608 while Present (Named) loop
1609 if Nkind (Named) /= N_Others_Choice
1610 and then No (Selector_Name (Named))
1611 then
1612 Error_Msg_N ("invalid positional actual after named one", Named);
1613 Abandon_Instantiation (Named);
1614 end if;
1615
1616 -- A named association may lack an actual parameter, if it was
1617 -- introduced for a default subprogram that turns out to be local
1618 -- to the outer instantiation. If it has a box association it must
1619 -- correspond to some formal in the generic.
1620
1621 if Nkind (Named) /= N_Others_Choice
1622 and then (Present (Explicit_Generic_Actual_Parameter (Named))
1623 or else Box_Present (Named))
1624 then
1625 Num_Actuals := Num_Actuals + 1;
1626 end if;
1627
1628 Next (Named);
1629 end loop;
1630
1631 if Present (Formals) then
1632 Formal := First_Non_Pragma (Formals);
1633 Analyzed_Formal := First_Non_Pragma (F_Copy);
1634
1635 if Present (Actuals) then
1636 Actual := First (Actuals);
1637
1638 -- All formals should have default values
1639
1640 else
1641 Actual := Empty;
1642 end if;
1643
1644 while Present (Formal) loop
1645 Set_Analyzed_Formal;
1646 Saved_Formal := Next_Non_Pragma (Formal);
1647
1648 case Nkind (Formal) is
1649 when N_Formal_Object_Declaration =>
1650 Match :=
1651 Matching_Actual
1652 (Defining_Identifier (Formal),
1653 Defining_Identifier (Analyzed_Formal));
1654
1655 if No (Match) and then Partial_Parameterization then
1656 Process_Default (Formal);
1657
1658 else
1659 Append_List
1660 (Instantiate_Object (Formal, Match, Analyzed_Formal),
1661 Assoc_List);
1662
1663 -- For a defaulted in_parameter, create an entry in the
1664 -- the list of defaulted actuals, for GNATProve use. Do
1665 -- not included these defaults for an instance nested
1666 -- within a generic, because the defaults are also used
1667 -- in the analysis of the enclosing generic, and only
1668 -- defaulted subprograms are relevant there.
1669
1670 if No (Match) and then not Inside_A_Generic then
1671 Append_To (Default_Actuals,
1672 Make_Generic_Association (Sloc (I_Node),
1673 Selector_Name =>
1674 New_Occurrence_Of
1675 (Defining_Identifier (Formal), Sloc (I_Node)),
1676 Explicit_Generic_Actual_Parameter =>
1677 New_Copy_Tree (Default_Expression (Formal))));
1678 end if;
1679 end if;
1680
1681 -- If the object is a call to an expression function, this
1682 -- is a freezing point for it.
1683
1684 if Is_Entity_Name (Match)
1685 and then Present (Entity (Match))
1686 and then Nkind
1687 (Original_Node (Unit_Declaration_Node (Entity (Match))))
1688 = N_Expression_Function
1689 then
1690 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1691 end if;
1692
1693 when N_Formal_Type_Declaration =>
1694 Match :=
1695 Matching_Actual
1696 (Defining_Identifier (Formal),
1697 Defining_Identifier (Analyzed_Formal));
1698
1699 if No (Match) then
1700 if Partial_Parameterization then
1701 Process_Default (Formal);
1702
1703 else
1704 Error_Msg_Sloc := Sloc (Gen_Unit);
1705 Error_Msg_NE
1706 ("missing actual&",
1707 Instantiation_Node, Defining_Identifier (Formal));
1708 Error_Msg_NE
1709 ("\in instantiation of & declared#",
1710 Instantiation_Node, Gen_Unit);
1711 Abandon_Instantiation (Instantiation_Node);
1712 end if;
1713
1714 else
1715 Analyze (Match);
1716 Append_List
1717 (Instantiate_Type
1718 (Formal, Match, Analyzed_Formal, Assoc_List),
1719 Assoc_List);
1720
1721 -- Warn when an actual is a fixed-point with user-
1722 -- defined promitives. The warning is superfluous
1723 -- if the fornal is private, because there can be
1724 -- no arithmetic operations in the generic so there
1725 -- no danger of confusion.
1726
1727 if Is_Fixed_Point_Type (Entity (Match))
1728 and then not Is_Private_Type
1729 (Defining_Identifier (Analyzed_Formal))
1730 then
1731 Check_Fixed_Point_Actual (Match);
1732 end if;
1733
1734 -- An instantiation is a freeze point for the actuals,
1735 -- unless this is a rewritten formal package, or the
1736 -- formal is an Ada 2012 formal incomplete type.
1737
1738 if Nkind (I_Node) = N_Formal_Package_Declaration
1739 or else
1740 (Ada_Version >= Ada_2012
1741 and then
1742 Ekind (Defining_Identifier (Analyzed_Formal)) =
1743 E_Incomplete_Type)
1744 then
1745 null;
1746
1747 else
1748 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1749 end if;
1750 end if;
1751
1752 -- A remote access-to-class-wide type is not a legal actual
1753 -- for a generic formal of an access type (E.2.2(17/2)).
1754 -- In GNAT an exception to this rule is introduced when
1755 -- the formal is marked as remote using implementation
1756 -- defined aspect/pragma Remote_Access_Type. In that case
1757 -- the actual must be remote as well.
1758
1759 -- If the current instantiation is the construction of a
1760 -- local copy for a formal package the actuals may be
1761 -- defaulted, and there is no matching actual to check.
1762
1763 if Nkind (Analyzed_Formal) = N_Formal_Type_Declaration
1764 and then
1765 Nkind (Formal_Type_Definition (Analyzed_Formal)) =
1766 N_Access_To_Object_Definition
1767 and then Present (Match)
1768 then
1769 declare
1770 Formal_Ent : constant Entity_Id :=
1771 Defining_Identifier (Analyzed_Formal);
1772 begin
1773 if Is_Remote_Access_To_Class_Wide_Type (Entity (Match))
1774 = Is_Remote_Types (Formal_Ent)
1775 then
1776 -- Remoteness of formal and actual match
1777
1778 null;
1779
1780 elsif Is_Remote_Types (Formal_Ent) then
1781
1782 -- Remote formal, non-remote actual
1783
1784 Error_Msg_NE
1785 ("actual for& must be remote", Match, Formal_Ent);
1786
1787 else
1788 -- Non-remote formal, remote actual
1789
1790 Error_Msg_NE
1791 ("actual for& may not be remote",
1792 Match, Formal_Ent);
1793 end if;
1794 end;
1795 end if;
1796
1797 when N_Formal_Subprogram_Declaration =>
1798 Match :=
1799 Matching_Actual
1800 (Defining_Unit_Name (Specification (Formal)),
1801 Defining_Unit_Name (Specification (Analyzed_Formal)));
1802
1803 -- If the formal subprogram has the same name as another
1804 -- formal subprogram of the generic, then a named
1805 -- association is illegal (12.3(9)). Exclude named
1806 -- associations that are generated for a nested instance.
1807
1808 if Present (Match)
1809 and then Is_Named_Assoc
1810 and then Comes_From_Source (Found_Assoc)
1811 then
1812 Check_Overloaded_Formal_Subprogram (Formal);
1813 end if;
1814
1815 -- If there is no corresponding actual, this may be case
1816 -- of partial parameterization, or else the formal has a
1817 -- default or a box.
1818
1819 if No (Match) and then Partial_Parameterization then
1820 Process_Default (Formal);
1821
1822 if Nkind (I_Node) = N_Formal_Package_Declaration then
1823 Check_Overloaded_Formal_Subprogram (Formal);
1824 end if;
1825
1826 else
1827 Append_To (Assoc_List,
1828 Instantiate_Formal_Subprogram
1829 (Formal, Match, Analyzed_Formal));
1830
1831 -- An instantiation is a freeze point for the actuals,
1832 -- unless this is a rewritten formal package.
1833
1834 if Nkind (I_Node) /= N_Formal_Package_Declaration
1835 and then Nkind (Match) = N_Identifier
1836 and then Is_Subprogram (Entity (Match))
1837
1838 -- The actual subprogram may rename a routine defined
1839 -- in Standard. Avoid freezing such renamings because
1840 -- subprograms coming from Standard cannot be frozen.
1841
1842 and then
1843 not Renames_Standard_Subprogram (Entity (Match))
1844
1845 -- If the actual subprogram comes from a different
1846 -- unit, it is already frozen, either by a body in
1847 -- that unit or by the end of the declarative part
1848 -- of the unit. This check avoids the freezing of
1849 -- subprograms defined in Standard which are used
1850 -- as generic actuals.
1851
1852 and then In_Same_Code_Unit (Entity (Match), I_Node)
1853 and then Has_Fully_Defined_Profile (Entity (Match))
1854 then
1855 -- Mark the subprogram as having a delayed freeze
1856 -- since this may be an out-of-order action.
1857
1858 Set_Has_Delayed_Freeze (Entity (Match));
1859 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1860 end if;
1861 end if;
1862
1863 -- If this is a nested generic, preserve default for later
1864 -- instantiations. We do this as well for GNATProve use,
1865 -- so that the list of generic associations is complete.
1866
1867 if No (Match) and then Box_Present (Formal) then
1868 declare
1869 Subp : constant Entity_Id :=
1870 Defining_Unit_Name
1871 (Specification (Last (Assoc_List)));
1872
1873 begin
1874 Append_To (Default_Actuals,
1875 Make_Generic_Association (Sloc (I_Node),
1876 Selector_Name =>
1877 New_Occurrence_Of (Subp, Sloc (I_Node)),
1878 Explicit_Generic_Actual_Parameter =>
1879 New_Occurrence_Of (Subp, Sloc (I_Node))));
1880 end;
1881 end if;
1882
1883 when N_Formal_Package_Declaration =>
1884 Match :=
1885 Matching_Actual
1886 (Defining_Identifier (Formal),
1887 Defining_Identifier (Original_Node (Analyzed_Formal)));
1888
1889 if No (Match) then
1890 if Partial_Parameterization then
1891 Process_Default (Formal);
1892
1893 else
1894 Error_Msg_Sloc := Sloc (Gen_Unit);
1895 Error_Msg_NE
1896 ("missing actual&",
1897 Instantiation_Node, Defining_Identifier (Formal));
1898 Error_Msg_NE
1899 ("\in instantiation of & declared#",
1900 Instantiation_Node, Gen_Unit);
1901
1902 Abandon_Instantiation (Instantiation_Node);
1903 end if;
1904
1905 else
1906 Analyze (Match);
1907 Append_List
1908 (Instantiate_Formal_Package
1909 (Formal, Match, Analyzed_Formal),
1910 Assoc_List);
1911
1912 -- Determine whether the actual package needs an explicit
1913 -- freeze node. This is only the case if the actual is
1914 -- declared in the same unit and has a body. Normally
1915 -- packages do not have explicit freeze nodes, and gigi
1916 -- only uses them to elaborate entities in a package
1917 -- body.
1918
1919 Explicit_Freeze_Check : declare
1920 Actual : constant Entity_Id := Entity (Match);
1921 Gen_Par : Entity_Id;
1922
1923 Needs_Freezing : Boolean;
1924 S : Entity_Id;
1925
1926 procedure Check_Generic_Parent;
1927 -- The actual may be an instantiation of a unit
1928 -- declared in a previous instantiation. If that
1929 -- one is also in the current compilation, it must
1930 -- itself be frozen before the actual. The actual
1931 -- may be an instantiation of a generic child unit,
1932 -- in which case the same applies to the instance
1933 -- of the parent which must be frozen before the
1934 -- actual.
1935 -- Should this itself be recursive ???
1936
1937 --------------------------
1938 -- Check_Generic_Parent --
1939 --------------------------
1940
1941 procedure Check_Generic_Parent is
1942 Inst : constant Node_Id :=
1943 Next (Unit_Declaration_Node (Actual));
1944 Par : Entity_Id;
1945
1946 begin
1947 Par := Empty;
1948
1949 if Nkind (Parent (Actual)) = N_Package_Specification
1950 then
1951 Par := Scope (Generic_Parent (Parent (Actual)));
1952
1953 if Is_Generic_Instance (Par) then
1954 null;
1955
1956 -- If the actual is a child generic unit, check
1957 -- whether the instantiation of the parent is
1958 -- also local and must also be frozen now. We
1959 -- must retrieve the instance node to locate the
1960 -- parent instance if any.
1961
1962 elsif Ekind (Par) = E_Generic_Package
1963 and then Is_Child_Unit (Gen_Par)
1964 and then Ekind (Scope (Gen_Par)) =
1965 E_Generic_Package
1966 then
1967 if Nkind (Inst) = N_Package_Instantiation
1968 and then Nkind (Name (Inst)) =
1969 N_Expanded_Name
1970 then
1971 -- Retrieve entity of parent instance
1972
1973 Par := Entity (Prefix (Name (Inst)));
1974 end if;
1975
1976 else
1977 Par := Empty;
1978 end if;
1979 end if;
1980
1981 if Present (Par)
1982 and then Is_Generic_Instance (Par)
1983 and then Scope (Par) = Current_Scope
1984 and then
1985 (No (Freeze_Node (Par))
1986 or else
1987 not Is_List_Member (Freeze_Node (Par)))
1988 then
1989 Set_Has_Delayed_Freeze (Par);
1990 Append_Elmt (Par, Actuals_To_Freeze);
1991 end if;
1992 end Check_Generic_Parent;
1993
1994 -- Start of processing for Explicit_Freeze_Check
1995
1996 begin
1997 if Present (Renamed_Entity (Actual)) then
1998 Gen_Par :=
1999 Generic_Parent (Specification
2000 (Unit_Declaration_Node
2001 (Renamed_Entity (Actual))));
2002 else
2003 Gen_Par :=
2004 Generic_Parent (Specification
2005 (Unit_Declaration_Node (Actual)));
2006 end if;
2007
2008 if not Expander_Active
2009 or else not Has_Completion (Actual)
2010 or else not In_Same_Source_Unit (I_Node, Actual)
2011 or else Is_Frozen (Actual)
2012 or else
2013 (Present (Renamed_Entity (Actual))
2014 and then
2015 not In_Same_Source_Unit
2016 (I_Node, (Renamed_Entity (Actual))))
2017 then
2018 null;
2019
2020 else
2021 -- Finally we want to exclude such freeze nodes
2022 -- from statement sequences, which freeze
2023 -- everything before them.
2024 -- Is this strictly necessary ???
2025
2026 Needs_Freezing := True;
2027
2028 S := Current_Scope;
2029 while Present (S) loop
2030 if Ekind_In (S, E_Block,
2031 E_Function,
2032 E_Loop,
2033 E_Procedure)
2034 then
2035 Needs_Freezing := False;
2036 exit;
2037 end if;
2038
2039 S := Scope (S);
2040 end loop;
2041
2042 if Needs_Freezing then
2043 Check_Generic_Parent;
2044
2045 -- If the actual is a renaming of a proper
2046 -- instance of the formal package, indicate
2047 -- that it is the instance that must be frozen.
2048
2049 if Nkind (Parent (Actual)) =
2050 N_Package_Renaming_Declaration
2051 then
2052 Set_Has_Delayed_Freeze
2053 (Renamed_Entity (Actual));
2054 Append_Elmt
2055 (Renamed_Entity (Actual),
2056 Actuals_To_Freeze);
2057 else
2058 Set_Has_Delayed_Freeze (Actual);
2059 Append_Elmt (Actual, Actuals_To_Freeze);
2060 end if;
2061 end if;
2062 end if;
2063 end Explicit_Freeze_Check;
2064 end if;
2065
2066 -- For use type and use package appearing in the generic part,
2067 -- we have already copied them, so we can just move them where
2068 -- they belong (we mustn't recopy them since this would mess up
2069 -- the Sloc values).
2070
2071 when N_Use_Package_Clause
2072 | N_Use_Type_Clause
2073 =>
2074 if Nkind (Original_Node (I_Node)) =
2075 N_Formal_Package_Declaration
2076 then
2077 Append (New_Copy_Tree (Formal), Assoc_List);
2078 else
2079 Remove (Formal);
2080 Append (Formal, Assoc_List);
2081 end if;
2082
2083 when others =>
2084 raise Program_Error;
2085 end case;
2086
2087 Formal := Saved_Formal;
2088 Next_Non_Pragma (Analyzed_Formal);
2089 end loop;
2090
2091 if Num_Actuals > Num_Matched then
2092 Error_Msg_Sloc := Sloc (Gen_Unit);
2093
2094 if Present (Selector_Name (Actual)) then
2095 Error_Msg_NE
2096 ("unmatched actual &", Actual, Selector_Name (Actual));
2097 Error_Msg_NE
2098 ("\in instantiation of & declared#", Actual, Gen_Unit);
2099 else
2100 Error_Msg_NE
2101 ("unmatched actual in instantiation of & declared#",
2102 Actual, Gen_Unit);
2103 end if;
2104 end if;
2105
2106 elsif Present (Actuals) then
2107 Error_Msg_N
2108 ("too many actuals in generic instantiation", Instantiation_Node);
2109 end if;
2110
2111 -- An instantiation freezes all generic actuals. The only exceptions
2112 -- to this are incomplete types and subprograms which are not fully
2113 -- defined at the point of instantiation.
2114
2115 declare
2116 Elmt : Elmt_Id := First_Elmt (Actuals_To_Freeze);
2117 begin
2118 while Present (Elmt) loop
2119 Freeze_Before (I_Node, Node (Elmt));
2120 Next_Elmt (Elmt);
2121 end loop;
2122 end;
2123
2124 -- If there are default subprograms, normalize the tree by adding
2125 -- explicit associations for them. This is required if the instance
2126 -- appears within a generic.
2127
2128 if not Is_Empty_List (Default_Actuals) then
2129 declare
2130 Default : Node_Id;
2131
2132 begin
2133 Default := First (Default_Actuals);
2134 while Present (Default) loop
2135 Mark_Rewrite_Insertion (Default);
2136 Next (Default);
2137 end loop;
2138
2139 if No (Actuals) then
2140 Set_Generic_Associations (I_Node, Default_Actuals);
2141 else
2142 Append_List_To (Actuals, Default_Actuals);
2143 end if;
2144 end;
2145 end if;
2146
2147 -- If this is a formal package, normalize the parameter list by adding
2148 -- explicit box associations for the formals that are covered by an
2149 -- Others_Choice.
2150
2151 if not Is_Empty_List (Default_Formals) then
2152 Append_List (Default_Formals, Formals);
2153 end if;
2154
2155 return Assoc_List;
2156 end Analyze_Associations;
2157
2158 -------------------------------
2159 -- Analyze_Formal_Array_Type --
2160 -------------------------------
2161
2162 procedure Analyze_Formal_Array_Type
2163 (T : in out Entity_Id;
2164 Def : Node_Id)
2165 is
2166 DSS : Node_Id;
2167
2168 begin
2169 -- Treated like a non-generic array declaration, with additional
2170 -- semantic checks.
2171
2172 Enter_Name (T);
2173
2174 if Nkind (Def) = N_Constrained_Array_Definition then
2175 DSS := First (Discrete_Subtype_Definitions (Def));
2176 while Present (DSS) loop
2177 if Nkind_In (DSS, N_Subtype_Indication,
2178 N_Range,
2179 N_Attribute_Reference)
2180 then
2181 Error_Msg_N ("only a subtype mark is allowed in a formal", DSS);
2182 end if;
2183
2184 Next (DSS);
2185 end loop;
2186 end if;
2187
2188 Array_Type_Declaration (T, Def);
2189 Set_Is_Generic_Type (Base_Type (T));
2190
2191 if Ekind (Component_Type (T)) = E_Incomplete_Type
2192 and then No (Full_View (Component_Type (T)))
2193 then
2194 Error_Msg_N ("premature usage of incomplete type", Def);
2195
2196 -- Check that range constraint is not allowed on the component type
2197 -- of a generic formal array type (AARM 12.5.3(3))
2198
2199 elsif Is_Internal (Component_Type (T))
2200 and then Present (Subtype_Indication (Component_Definition (Def)))
2201 and then Nkind (Original_Node
2202 (Subtype_Indication (Component_Definition (Def)))) =
2203 N_Subtype_Indication
2204 then
2205 Error_Msg_N
2206 ("in a formal, a subtype indication can only be "
2207 & "a subtype mark (RM 12.5.3(3))",
2208 Subtype_Indication (Component_Definition (Def)));
2209 end if;
2210
2211 end Analyze_Formal_Array_Type;
2212
2213 ---------------------------------------------
2214 -- Analyze_Formal_Decimal_Fixed_Point_Type --
2215 ---------------------------------------------
2216
2217 -- As for other generic types, we create a valid type representation with
2218 -- legal but arbitrary attributes, whose values are never considered
2219 -- static. For all scalar types we introduce an anonymous base type, with
2220 -- the same attributes. We choose the corresponding integer type to be
2221 -- Standard_Integer.
2222 -- Here and in other similar routines, the Sloc of the generated internal
2223 -- type must be the same as the sloc of the defining identifier of the
2224 -- formal type declaration, to provide proper source navigation.
2225
2226 procedure Analyze_Formal_Decimal_Fixed_Point_Type
2227 (T : Entity_Id;
2228 Def : Node_Id)
2229 is
2230 Loc : constant Source_Ptr := Sloc (Def);
2231
2232 Base : constant Entity_Id :=
2233 New_Internal_Entity
2234 (E_Decimal_Fixed_Point_Type,
2235 Current_Scope,
2236 Sloc (Defining_Identifier (Parent (Def))), 'G');
2237
2238 Int_Base : constant Entity_Id := Standard_Integer;
2239 Delta_Val : constant Ureal := Ureal_1;
2240 Digs_Val : constant Uint := Uint_6;
2241
2242 function Make_Dummy_Bound return Node_Id;
2243 -- Return a properly typed universal real literal to use as a bound
2244
2245 ----------------------
2246 -- Make_Dummy_Bound --
2247 ----------------------
2248
2249 function Make_Dummy_Bound return Node_Id is
2250 Bound : constant Node_Id := Make_Real_Literal (Loc, Ureal_1);
2251 begin
2252 Set_Etype (Bound, Universal_Real);
2253 return Bound;
2254 end Make_Dummy_Bound;
2255
2256 -- Start of processing for Analyze_Formal_Decimal_Fixed_Point_Type
2257
2258 begin
2259 Enter_Name (T);
2260
2261 Set_Etype (Base, Base);
2262 Set_Size_Info (Base, Int_Base);
2263 Set_RM_Size (Base, RM_Size (Int_Base));
2264 Set_First_Rep_Item (Base, First_Rep_Item (Int_Base));
2265 Set_Digits_Value (Base, Digs_Val);
2266 Set_Delta_Value (Base, Delta_Val);
2267 Set_Small_Value (Base, Delta_Val);
2268 Set_Scalar_Range (Base,
2269 Make_Range (Loc,
2270 Low_Bound => Make_Dummy_Bound,
2271 High_Bound => Make_Dummy_Bound));
2272
2273 Set_Is_Generic_Type (Base);
2274 Set_Parent (Base, Parent (Def));
2275
2276 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
2277 Set_Etype (T, Base);
2278 Set_Size_Info (T, Int_Base);
2279 Set_RM_Size (T, RM_Size (Int_Base));
2280 Set_First_Rep_Item (T, First_Rep_Item (Int_Base));
2281 Set_Digits_Value (T, Digs_Val);
2282 Set_Delta_Value (T, Delta_Val);
2283 Set_Small_Value (T, Delta_Val);
2284 Set_Scalar_Range (T, Scalar_Range (Base));
2285 Set_Is_Constrained (T);
2286
2287 Check_Restriction (No_Fixed_Point, Def);
2288 end Analyze_Formal_Decimal_Fixed_Point_Type;
2289
2290 -------------------------------------------
2291 -- Analyze_Formal_Derived_Interface_Type --
2292 -------------------------------------------
2293
2294 procedure Analyze_Formal_Derived_Interface_Type
2295 (N : Node_Id;
2296 T : Entity_Id;
2297 Def : Node_Id)
2298 is
2299 Loc : constant Source_Ptr := Sloc (Def);
2300
2301 begin
2302 -- Rewrite as a type declaration of a derived type. This ensures that
2303 -- the interface list and primitive operations are properly captured.
2304
2305 Rewrite (N,
2306 Make_Full_Type_Declaration (Loc,
2307 Defining_Identifier => T,
2308 Type_Definition => Def));
2309 Analyze (N);
2310 Set_Is_Generic_Type (T);
2311 end Analyze_Formal_Derived_Interface_Type;
2312
2313 ---------------------------------
2314 -- Analyze_Formal_Derived_Type --
2315 ---------------------------------
2316
2317 procedure Analyze_Formal_Derived_Type
2318 (N : Node_Id;
2319 T : Entity_Id;
2320 Def : Node_Id)
2321 is
2322 Loc : constant Source_Ptr := Sloc (Def);
2323 Unk_Disc : constant Boolean := Unknown_Discriminants_Present (N);
2324 New_N : Node_Id;
2325
2326 begin
2327 Set_Is_Generic_Type (T);
2328
2329 if Private_Present (Def) then
2330 New_N :=
2331 Make_Private_Extension_Declaration (Loc,
2332 Defining_Identifier => T,
2333 Discriminant_Specifications => Discriminant_Specifications (N),
2334 Unknown_Discriminants_Present => Unk_Disc,
2335 Subtype_Indication => Subtype_Mark (Def),
2336 Interface_List => Interface_List (Def));
2337
2338 Set_Abstract_Present (New_N, Abstract_Present (Def));
2339 Set_Limited_Present (New_N, Limited_Present (Def));
2340 Set_Synchronized_Present (New_N, Synchronized_Present (Def));
2341
2342 else
2343 New_N :=
2344 Make_Full_Type_Declaration (Loc,
2345 Defining_Identifier => T,
2346 Discriminant_Specifications =>
2347 Discriminant_Specifications (Parent (T)),
2348 Type_Definition =>
2349 Make_Derived_Type_Definition (Loc,
2350 Subtype_Indication => Subtype_Mark (Def)));
2351
2352 Set_Abstract_Present
2353 (Type_Definition (New_N), Abstract_Present (Def));
2354 Set_Limited_Present
2355 (Type_Definition (New_N), Limited_Present (Def));
2356 end if;
2357
2358 Rewrite (N, New_N);
2359 Analyze (N);
2360
2361 if Unk_Disc then
2362 if not Is_Composite_Type (T) then
2363 Error_Msg_N
2364 ("unknown discriminants not allowed for elementary types", N);
2365 else
2366 Set_Has_Unknown_Discriminants (T);
2367 Set_Is_Constrained (T, False);
2368 end if;
2369 end if;
2370
2371 -- If the parent type has a known size, so does the formal, which makes
2372 -- legal representation clauses that involve the formal.
2373
2374 Set_Size_Known_At_Compile_Time
2375 (T, Size_Known_At_Compile_Time (Entity (Subtype_Mark (Def))));
2376 end Analyze_Formal_Derived_Type;
2377
2378 ----------------------------------
2379 -- Analyze_Formal_Discrete_Type --
2380 ----------------------------------
2381
2382 -- The operations defined for a discrete types are those of an enumeration
2383 -- type. The size is set to an arbitrary value, for use in analyzing the
2384 -- generic unit.
2385
2386 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id) is
2387 Loc : constant Source_Ptr := Sloc (Def);
2388 Lo : Node_Id;
2389 Hi : Node_Id;
2390
2391 Base : constant Entity_Id :=
2392 New_Internal_Entity
2393 (E_Floating_Point_Type, Current_Scope,
2394 Sloc (Defining_Identifier (Parent (Def))), 'G');
2395
2396 begin
2397 Enter_Name (T);
2398 Set_Ekind (T, E_Enumeration_Subtype);
2399 Set_Etype (T, Base);
2400 Init_Size (T, 8);
2401 Init_Alignment (T);
2402 Set_Is_Generic_Type (T);
2403 Set_Is_Constrained (T);
2404
2405 -- For semantic analysis, the bounds of the type must be set to some
2406 -- non-static value. The simplest is to create attribute nodes for those
2407 -- bounds, that refer to the type itself. These bounds are never
2408 -- analyzed but serve as place-holders.
2409
2410 Lo :=
2411 Make_Attribute_Reference (Loc,
2412 Attribute_Name => Name_First,
2413 Prefix => New_Occurrence_Of (T, Loc));
2414 Set_Etype (Lo, T);
2415
2416 Hi :=
2417 Make_Attribute_Reference (Loc,
2418 Attribute_Name => Name_Last,
2419 Prefix => New_Occurrence_Of (T, Loc));
2420 Set_Etype (Hi, T);
2421
2422 Set_Scalar_Range (T,
2423 Make_Range (Loc,
2424 Low_Bound => Lo,
2425 High_Bound => Hi));
2426
2427 Set_Ekind (Base, E_Enumeration_Type);
2428 Set_Etype (Base, Base);
2429 Init_Size (Base, 8);
2430 Init_Alignment (Base);
2431 Set_Is_Generic_Type (Base);
2432 Set_Scalar_Range (Base, Scalar_Range (T));
2433 Set_Parent (Base, Parent (Def));
2434 end Analyze_Formal_Discrete_Type;
2435
2436 ----------------------------------
2437 -- Analyze_Formal_Floating_Type --
2438 ---------------------------------
2439
2440 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id) is
2441 Base : constant Entity_Id :=
2442 New_Internal_Entity
2443 (E_Floating_Point_Type, Current_Scope,
2444 Sloc (Defining_Identifier (Parent (Def))), 'G');
2445
2446 begin
2447 -- The various semantic attributes are taken from the predefined type
2448 -- Float, just so that all of them are initialized. Their values are
2449 -- never used because no constant folding or expansion takes place in
2450 -- the generic itself.
2451
2452 Enter_Name (T);
2453 Set_Ekind (T, E_Floating_Point_Subtype);
2454 Set_Etype (T, Base);
2455 Set_Size_Info (T, (Standard_Float));
2456 Set_RM_Size (T, RM_Size (Standard_Float));
2457 Set_Digits_Value (T, Digits_Value (Standard_Float));
2458 Set_Scalar_Range (T, Scalar_Range (Standard_Float));
2459 Set_Is_Constrained (T);
2460
2461 Set_Is_Generic_Type (Base);
2462 Set_Etype (Base, Base);
2463 Set_Size_Info (Base, (Standard_Float));
2464 Set_RM_Size (Base, RM_Size (Standard_Float));
2465 Set_Digits_Value (Base, Digits_Value (Standard_Float));
2466 Set_Scalar_Range (Base, Scalar_Range (Standard_Float));
2467 Set_Parent (Base, Parent (Def));
2468
2469 Check_Restriction (No_Floating_Point, Def);
2470 end Analyze_Formal_Floating_Type;
2471
2472 -----------------------------------
2473 -- Analyze_Formal_Interface_Type;--
2474 -----------------------------------
2475
2476 procedure Analyze_Formal_Interface_Type
2477 (N : Node_Id;
2478 T : Entity_Id;
2479 Def : Node_Id)
2480 is
2481 Loc : constant Source_Ptr := Sloc (N);
2482 New_N : Node_Id;
2483
2484 begin
2485 New_N :=
2486 Make_Full_Type_Declaration (Loc,
2487 Defining_Identifier => T,
2488 Type_Definition => Def);
2489
2490 Rewrite (N, New_N);
2491 Analyze (N);
2492 Set_Is_Generic_Type (T);
2493 end Analyze_Formal_Interface_Type;
2494
2495 ---------------------------------
2496 -- Analyze_Formal_Modular_Type --
2497 ---------------------------------
2498
2499 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id) is
2500 begin
2501 -- Apart from their entity kind, generic modular types are treated like
2502 -- signed integer types, and have the same attributes.
2503
2504 Analyze_Formal_Signed_Integer_Type (T, Def);
2505 Set_Ekind (T, E_Modular_Integer_Subtype);
2506 Set_Ekind (Etype (T), E_Modular_Integer_Type);
2507
2508 end Analyze_Formal_Modular_Type;
2509
2510 ---------------------------------------
2511 -- Analyze_Formal_Object_Declaration --
2512 ---------------------------------------
2513
2514 procedure Analyze_Formal_Object_Declaration (N : Node_Id) is
2515 E : constant Node_Id := Default_Expression (N);
2516 Id : constant Node_Id := Defining_Identifier (N);
2517 K : Entity_Kind;
2518 T : Node_Id;
2519
2520 begin
2521 Enter_Name (Id);
2522
2523 -- Determine the mode of the formal object
2524
2525 if Out_Present (N) then
2526 K := E_Generic_In_Out_Parameter;
2527
2528 if not In_Present (N) then
2529 Error_Msg_N ("formal generic objects cannot have mode OUT", N);
2530 end if;
2531
2532 else
2533 K := E_Generic_In_Parameter;
2534 end if;
2535
2536 if Present (Subtype_Mark (N)) then
2537 Find_Type (Subtype_Mark (N));
2538 T := Entity (Subtype_Mark (N));
2539
2540 -- Verify that there is no redundant null exclusion
2541
2542 if Null_Exclusion_Present (N) then
2543 if not Is_Access_Type (T) then
2544 Error_Msg_N
2545 ("null exclusion can only apply to an access type", N);
2546
2547 elsif Can_Never_Be_Null (T) then
2548 Error_Msg_NE
2549 ("`NOT NULL` not allowed (& already excludes null)", N, T);
2550 end if;
2551 end if;
2552
2553 -- Ada 2005 (AI-423): Formal object with an access definition
2554
2555 else
2556 Check_Access_Definition (N);
2557 T := Access_Definition
2558 (Related_Nod => N,
2559 N => Access_Definition (N));
2560 end if;
2561
2562 if Ekind (T) = E_Incomplete_Type then
2563 declare
2564 Error_Node : Node_Id;
2565
2566 begin
2567 if Present (Subtype_Mark (N)) then
2568 Error_Node := Subtype_Mark (N);
2569 else
2570 Check_Access_Definition (N);
2571 Error_Node := Access_Definition (N);
2572 end if;
2573
2574 Error_Msg_N ("premature usage of incomplete type", Error_Node);
2575 end;
2576 end if;
2577
2578 if K = E_Generic_In_Parameter then
2579
2580 -- Ada 2005 (AI-287): Limited aggregates allowed in generic formals
2581
2582 if Ada_Version < Ada_2005 and then Is_Limited_Type (T) then
2583 Error_Msg_N
2584 ("generic formal of mode IN must not be of limited type", N);
2585 Explain_Limited_Type (T, N);
2586 end if;
2587
2588 if Is_Abstract_Type (T) then
2589 Error_Msg_N
2590 ("generic formal of mode IN must not be of abstract type", N);
2591 end if;
2592
2593 if Present (E) then
2594 Preanalyze_Spec_Expression (E, T);
2595
2596 if Is_Limited_Type (T) and then not OK_For_Limited_Init (T, E) then
2597 Error_Msg_N
2598 ("initialization not allowed for limited types", E);
2599 Explain_Limited_Type (T, E);
2600 end if;
2601 end if;
2602
2603 Set_Ekind (Id, K);
2604 Set_Etype (Id, T);
2605
2606 -- Case of generic IN OUT parameter
2607
2608 else
2609 -- If the formal has an unconstrained type, construct its actual
2610 -- subtype, as is done for subprogram formals. In this fashion, all
2611 -- its uses can refer to specific bounds.
2612
2613 Set_Ekind (Id, K);
2614 Set_Etype (Id, T);
2615
2616 if (Is_Array_Type (T) and then not Is_Constrained (T))
2617 or else (Ekind (T) = E_Record_Type and then Has_Discriminants (T))
2618 then
2619 declare
2620 Non_Freezing_Ref : constant Node_Id :=
2621 New_Occurrence_Of (Id, Sloc (Id));
2622 Decl : Node_Id;
2623
2624 begin
2625 -- Make sure the actual subtype doesn't generate bogus freezing
2626
2627 Set_Must_Not_Freeze (Non_Freezing_Ref);
2628 Decl := Build_Actual_Subtype (T, Non_Freezing_Ref);
2629 Insert_Before_And_Analyze (N, Decl);
2630 Set_Actual_Subtype (Id, Defining_Identifier (Decl));
2631 end;
2632 else
2633 Set_Actual_Subtype (Id, T);
2634 end if;
2635
2636 if Present (E) then
2637 Error_Msg_N
2638 ("initialization not allowed for `IN OUT` formals", N);
2639 end if;
2640 end if;
2641
2642 if Has_Aspects (N) then
2643 Analyze_Aspect_Specifications (N, Id);
2644 end if;
2645 end Analyze_Formal_Object_Declaration;
2646
2647 ----------------------------------------------
2648 -- Analyze_Formal_Ordinary_Fixed_Point_Type --
2649 ----------------------------------------------
2650
2651 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
2652 (T : Entity_Id;
2653 Def : Node_Id)
2654 is
2655 Loc : constant Source_Ptr := Sloc (Def);
2656 Base : constant Entity_Id :=
2657 New_Internal_Entity
2658 (E_Ordinary_Fixed_Point_Type, Current_Scope,
2659 Sloc (Defining_Identifier (Parent (Def))), 'G');
2660
2661 begin
2662 -- The semantic attributes are set for completeness only, their values
2663 -- will never be used, since all properties of the type are non-static.
2664
2665 Enter_Name (T);
2666 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
2667 Set_Etype (T, Base);
2668 Set_Size_Info (T, Standard_Integer);
2669 Set_RM_Size (T, RM_Size (Standard_Integer));
2670 Set_Small_Value (T, Ureal_1);
2671 Set_Delta_Value (T, Ureal_1);
2672 Set_Scalar_Range (T,
2673 Make_Range (Loc,
2674 Low_Bound => Make_Real_Literal (Loc, Ureal_1),
2675 High_Bound => Make_Real_Literal (Loc, Ureal_1)));
2676 Set_Is_Constrained (T);
2677
2678 Set_Is_Generic_Type (Base);
2679 Set_Etype (Base, Base);
2680 Set_Size_Info (Base, Standard_Integer);
2681 Set_RM_Size (Base, RM_Size (Standard_Integer));
2682 Set_Small_Value (Base, Ureal_1);
2683 Set_Delta_Value (Base, Ureal_1);
2684 Set_Scalar_Range (Base, Scalar_Range (T));
2685 Set_Parent (Base, Parent (Def));
2686
2687 Check_Restriction (No_Fixed_Point, Def);
2688 end Analyze_Formal_Ordinary_Fixed_Point_Type;
2689
2690 ----------------------------------------
2691 -- Analyze_Formal_Package_Declaration --
2692 ----------------------------------------
2693
2694 procedure Analyze_Formal_Package_Declaration (N : Node_Id) is
2695 Gen_Id : constant Node_Id := Name (N);
2696 Loc : constant Source_Ptr := Sloc (N);
2697 Pack_Id : constant Entity_Id := Defining_Identifier (N);
2698 Formal : Entity_Id;
2699 Gen_Decl : Node_Id;
2700 Gen_Unit : Entity_Id;
2701 Renaming : Node_Id;
2702
2703 Vis_Prims_List : Elist_Id := No_Elist;
2704 -- List of primitives made temporarily visible in the instantiation
2705 -- to match the visibility of the formal type.
2706
2707 function Build_Local_Package return Node_Id;
2708 -- The formal package is rewritten so that its parameters are replaced
2709 -- with corresponding declarations. For parameters with bona fide
2710 -- associations these declarations are created by Analyze_Associations
2711 -- as for a regular instantiation. For boxed parameters, we preserve
2712 -- the formal declarations and analyze them, in order to introduce
2713 -- entities of the right kind in the environment of the formal.
2714
2715 -------------------------
2716 -- Build_Local_Package --
2717 -------------------------
2718
2719 function Build_Local_Package return Node_Id is
2720 Decls : List_Id;
2721 Pack_Decl : Node_Id;
2722
2723 begin
2724 -- Within the formal, the name of the generic package is a renaming
2725 -- of the formal (as for a regular instantiation).
2726
2727 Pack_Decl :=
2728 Make_Package_Declaration (Loc,
2729 Specification =>
2730 Copy_Generic_Node
2731 (Specification (Original_Node (Gen_Decl)),
2732 Empty, Instantiating => True));
2733
2734 Renaming :=
2735 Make_Package_Renaming_Declaration (Loc,
2736 Defining_Unit_Name =>
2737 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
2738 Name => New_Occurrence_Of (Formal, Loc));
2739
2740 if Nkind (Gen_Id) = N_Identifier
2741 and then Chars (Gen_Id) = Chars (Pack_Id)
2742 then
2743 Error_Msg_NE
2744 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
2745 end if;
2746
2747 -- If the formal is declared with a box, or with an others choice,
2748 -- create corresponding declarations for all entities in the formal
2749 -- part, so that names with the proper types are available in the
2750 -- specification of the formal package.
2751
2752 -- On the other hand, if there are no associations, then all the
2753 -- formals must have defaults, and this will be checked by the
2754 -- call to Analyze_Associations.
2755
2756 if Box_Present (N)
2757 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2758 then
2759 declare
2760 Formal_Decl : Node_Id;
2761
2762 begin
2763 -- TBA : for a formal package, need to recurse ???
2764
2765 Decls := New_List;
2766 Formal_Decl :=
2767 First
2768 (Generic_Formal_Declarations (Original_Node (Gen_Decl)));
2769 while Present (Formal_Decl) loop
2770 Append_To
2771 (Decls,
2772 Copy_Generic_Node
2773 (Formal_Decl, Empty, Instantiating => True));
2774 Next (Formal_Decl);
2775 end loop;
2776 end;
2777
2778 -- If generic associations are present, use Analyze_Associations to
2779 -- create the proper renaming declarations.
2780
2781 else
2782 declare
2783 Act_Tree : constant Node_Id :=
2784 Copy_Generic_Node
2785 (Original_Node (Gen_Decl), Empty,
2786 Instantiating => True);
2787
2788 begin
2789 Generic_Renamings.Set_Last (0);
2790 Generic_Renamings_HTable.Reset;
2791 Instantiation_Node := N;
2792
2793 Decls :=
2794 Analyze_Associations
2795 (I_Node => Original_Node (N),
2796 Formals => Generic_Formal_Declarations (Act_Tree),
2797 F_Copy => Generic_Formal_Declarations (Gen_Decl));
2798
2799 Vis_Prims_List := Check_Hidden_Primitives (Decls);
2800 end;
2801 end if;
2802
2803 Append (Renaming, To => Decls);
2804
2805 -- Add generated declarations ahead of local declarations in
2806 -- the package.
2807
2808 if No (Visible_Declarations (Specification (Pack_Decl))) then
2809 Set_Visible_Declarations (Specification (Pack_Decl), Decls);
2810 else
2811 Insert_List_Before
2812 (First (Visible_Declarations (Specification (Pack_Decl))),
2813 Decls);
2814 end if;
2815
2816 return Pack_Decl;
2817 end Build_Local_Package;
2818
2819 -- Local variables
2820
2821 Save_ISMP : constant Boolean := Ignore_SPARK_Mode_Pragmas_In_Instance;
2822 -- Save flag Ignore_SPARK_Mode_Pragmas_In_Instance for restore on exit
2823
2824 Associations : Boolean := True;
2825 New_N : Node_Id;
2826 Parent_Installed : Boolean := False;
2827 Parent_Instance : Entity_Id;
2828 Renaming_In_Par : Entity_Id;
2829
2830 -- Start of processing for Analyze_Formal_Package_Declaration
2831
2832 begin
2833 Check_Text_IO_Special_Unit (Gen_Id);
2834
2835 Init_Env;
2836 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
2837 Gen_Unit := Entity (Gen_Id);
2838
2839 -- Check for a formal package that is a package renaming
2840
2841 if Present (Renamed_Object (Gen_Unit)) then
2842
2843 -- Indicate that unit is used, before replacing it with renamed
2844 -- entity for use below.
2845
2846 if In_Extended_Main_Source_Unit (N) then
2847 Set_Is_Instantiated (Gen_Unit);
2848 Generate_Reference (Gen_Unit, N);
2849 end if;
2850
2851 Gen_Unit := Renamed_Object (Gen_Unit);
2852 end if;
2853
2854 if Ekind (Gen_Unit) /= E_Generic_Package then
2855 Error_Msg_N ("expect generic package name", Gen_Id);
2856 Restore_Env;
2857 goto Leave;
2858
2859 elsif Gen_Unit = Current_Scope then
2860 Error_Msg_N
2861 ("generic package cannot be used as a formal package of itself",
2862 Gen_Id);
2863 Restore_Env;
2864 goto Leave;
2865
2866 elsif In_Open_Scopes (Gen_Unit) then
2867 if Is_Compilation_Unit (Gen_Unit)
2868 and then Is_Child_Unit (Current_Scope)
2869 then
2870 -- Special-case the error when the formal is a parent, and
2871 -- continue analysis to minimize cascaded errors.
2872
2873 Error_Msg_N
2874 ("generic parent cannot be used as formal package of a child "
2875 & "unit", Gen_Id);
2876
2877 else
2878 Error_Msg_N
2879 ("generic package cannot be used as a formal package within "
2880 & "itself", Gen_Id);
2881 Restore_Env;
2882 goto Leave;
2883 end if;
2884 end if;
2885
2886 -- Check that name of formal package does not hide name of generic,
2887 -- or its leading prefix. This check must be done separately because
2888 -- the name of the generic has already been analyzed.
2889
2890 declare
2891 Gen_Name : Entity_Id;
2892
2893 begin
2894 Gen_Name := Gen_Id;
2895 while Nkind (Gen_Name) = N_Expanded_Name loop
2896 Gen_Name := Prefix (Gen_Name);
2897 end loop;
2898
2899 if Chars (Gen_Name) = Chars (Pack_Id) then
2900 Error_Msg_NE
2901 ("& is hidden within declaration of formal package",
2902 Gen_Id, Gen_Name);
2903 end if;
2904 end;
2905
2906 if Box_Present (N)
2907 or else No (Generic_Associations (N))
2908 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2909 then
2910 Associations := False;
2911 end if;
2912
2913 -- If there are no generic associations, the generic parameters appear
2914 -- as local entities and are instantiated like them. We copy the generic
2915 -- package declaration as if it were an instantiation, and analyze it
2916 -- like a regular package, except that we treat the formals as
2917 -- additional visible components.
2918
2919 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
2920
2921 if In_Extended_Main_Source_Unit (N) then
2922 Set_Is_Instantiated (Gen_Unit);
2923 Generate_Reference (Gen_Unit, N);
2924 end if;
2925
2926 Formal := New_Copy (Pack_Id);
2927 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
2928
2929 -- Make local generic without formals. The formals will be replaced with
2930 -- internal declarations.
2931
2932 begin
2933 New_N := Build_Local_Package;
2934
2935 -- If there are errors in the parameter list, Analyze_Associations
2936 -- raises Instantiation_Error. Patch the declaration to prevent further
2937 -- exception propagation.
2938
2939 exception
2940 when Instantiation_Error =>
2941 Enter_Name (Formal);
2942 Set_Ekind (Formal, E_Variable);
2943 Set_Etype (Formal, Any_Type);
2944 Restore_Hidden_Primitives (Vis_Prims_List);
2945
2946 if Parent_Installed then
2947 Remove_Parent;
2948 end if;
2949
2950 goto Leave;
2951 end;
2952
2953 Rewrite (N, New_N);
2954 Set_Defining_Unit_Name (Specification (New_N), Formal);
2955 Set_Generic_Parent (Specification (N), Gen_Unit);
2956 Set_Instance_Env (Gen_Unit, Formal);
2957 Set_Is_Generic_Instance (Formal);
2958
2959 Enter_Name (Formal);
2960 Set_Ekind (Formal, E_Package);
2961 Set_Etype (Formal, Standard_Void_Type);
2962 Set_Inner_Instances (Formal, New_Elmt_List);
2963 Push_Scope (Formal);
2964
2965 -- Manually set the SPARK_Mode from the context because the package
2966 -- declaration is never analyzed.
2967
2968 Set_SPARK_Pragma (Formal, SPARK_Mode_Pragma);
2969 Set_SPARK_Aux_Pragma (Formal, SPARK_Mode_Pragma);
2970 Set_SPARK_Pragma_Inherited (Formal);
2971 Set_SPARK_Aux_Pragma_Inherited (Formal);
2972
2973 if Is_Child_Unit (Gen_Unit) and then Parent_Installed then
2974
2975 -- Similarly, we have to make the name of the formal visible in the
2976 -- parent instance, to resolve properly fully qualified names that
2977 -- may appear in the generic unit. The parent instance has been
2978 -- placed on the scope stack ahead of the current scope.
2979
2980 Parent_Instance := Scope_Stack.Table (Scope_Stack.Last - 1).Entity;
2981
2982 Renaming_In_Par :=
2983 Make_Defining_Identifier (Loc, Chars (Gen_Unit));
2984 Set_Ekind (Renaming_In_Par, E_Package);
2985 Set_Etype (Renaming_In_Par, Standard_Void_Type);
2986 Set_Scope (Renaming_In_Par, Parent_Instance);
2987 Set_Parent (Renaming_In_Par, Parent (Formal));
2988 Set_Renamed_Object (Renaming_In_Par, Formal);
2989 Append_Entity (Renaming_In_Par, Parent_Instance);
2990 end if;
2991
2992 -- A formal package declaration behaves as a package instantiation with
2993 -- respect to SPARK_Mode "off". If the annotation is "off" or altogether
2994 -- missing, set the global flag which signals Analyze_Pragma to ingnore
2995 -- all SPARK_Mode pragmas within the generic_package_name.
2996
2997 if SPARK_Mode /= On then
2998 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
2999
3000 -- Mark the formal spec in case the body is instantiated at a later
3001 -- pass. This preserves the original context in effect for the body.
3002
3003 Set_Ignore_SPARK_Mode_Pragmas (Formal);
3004 end if;
3005
3006 Analyze (Specification (N));
3007
3008 -- The formals for which associations are provided are not visible
3009 -- outside of the formal package. The others are still declared by a
3010 -- formal parameter declaration.
3011
3012 -- If there are no associations, the only local entity to hide is the
3013 -- generated package renaming itself.
3014
3015 declare
3016 E : Entity_Id;
3017
3018 begin
3019 E := First_Entity (Formal);
3020 while Present (E) loop
3021 if Associations and then not Is_Generic_Formal (E) then
3022 Set_Is_Hidden (E);
3023 end if;
3024
3025 if Ekind (E) = E_Package and then Renamed_Entity (E) = Formal then
3026 Set_Is_Hidden (E);
3027 exit;
3028 end if;
3029
3030 Next_Entity (E);
3031 end loop;
3032 end;
3033
3034 End_Package_Scope (Formal);
3035 Restore_Hidden_Primitives (Vis_Prims_List);
3036
3037 if Parent_Installed then
3038 Remove_Parent;
3039 end if;
3040
3041 Restore_Env;
3042
3043 -- Inside the generic unit, the formal package is a regular package, but
3044 -- no body is needed for it. Note that after instantiation, the defining
3045 -- unit name we need is in the new tree and not in the original (see
3046 -- Package_Instantiation). A generic formal package is an instance, and
3047 -- can be used as an actual for an inner instance.
3048
3049 Set_Has_Completion (Formal, True);
3050
3051 -- Add semantic information to the original defining identifier for ASIS
3052 -- use.
3053
3054 Set_Ekind (Pack_Id, E_Package);
3055 Set_Etype (Pack_Id, Standard_Void_Type);
3056 Set_Scope (Pack_Id, Scope (Formal));
3057 Set_Has_Completion (Pack_Id, True);
3058
3059 <<Leave>>
3060 if Has_Aspects (N) then
3061 Analyze_Aspect_Specifications (N, Pack_Id);
3062 end if;
3063
3064 Ignore_SPARK_Mode_Pragmas_In_Instance := Save_ISMP;
3065 end Analyze_Formal_Package_Declaration;
3066
3067 ---------------------------------
3068 -- Analyze_Formal_Private_Type --
3069 ---------------------------------
3070
3071 procedure Analyze_Formal_Private_Type
3072 (N : Node_Id;
3073 T : Entity_Id;
3074 Def : Node_Id)
3075 is
3076 begin
3077 New_Private_Type (N, T, Def);
3078
3079 -- Set the size to an arbitrary but legal value
3080
3081 Set_Size_Info (T, Standard_Integer);
3082 Set_RM_Size (T, RM_Size (Standard_Integer));
3083 end Analyze_Formal_Private_Type;
3084
3085 ------------------------------------
3086 -- Analyze_Formal_Incomplete_Type --
3087 ------------------------------------
3088
3089 procedure Analyze_Formal_Incomplete_Type
3090 (T : Entity_Id;
3091 Def : Node_Id)
3092 is
3093 begin
3094 Enter_Name (T);
3095 Set_Ekind (T, E_Incomplete_Type);
3096 Set_Etype (T, T);
3097 Set_Private_Dependents (T, New_Elmt_List);
3098
3099 if Tagged_Present (Def) then
3100 Set_Is_Tagged_Type (T);
3101 Make_Class_Wide_Type (T);
3102 Set_Direct_Primitive_Operations (T, New_Elmt_List);
3103 end if;
3104 end Analyze_Formal_Incomplete_Type;
3105
3106 ----------------------------------------
3107 -- Analyze_Formal_Signed_Integer_Type --
3108 ----------------------------------------
3109
3110 procedure Analyze_Formal_Signed_Integer_Type
3111 (T : Entity_Id;
3112 Def : Node_Id)
3113 is
3114 Base : constant Entity_Id :=
3115 New_Internal_Entity
3116 (E_Signed_Integer_Type,
3117 Current_Scope,
3118 Sloc (Defining_Identifier (Parent (Def))), 'G');
3119
3120 begin
3121 Enter_Name (T);
3122
3123 Set_Ekind (T, E_Signed_Integer_Subtype);
3124 Set_Etype (T, Base);
3125 Set_Size_Info (T, Standard_Integer);
3126 Set_RM_Size (T, RM_Size (Standard_Integer));
3127 Set_Scalar_Range (T, Scalar_Range (Standard_Integer));
3128 Set_Is_Constrained (T);
3129
3130 Set_Is_Generic_Type (Base);
3131 Set_Size_Info (Base, Standard_Integer);
3132 Set_RM_Size (Base, RM_Size (Standard_Integer));
3133 Set_Etype (Base, Base);
3134 Set_Scalar_Range (Base, Scalar_Range (Standard_Integer));
3135 Set_Parent (Base, Parent (Def));
3136 end Analyze_Formal_Signed_Integer_Type;
3137
3138 -------------------------------------------
3139 -- Analyze_Formal_Subprogram_Declaration --
3140 -------------------------------------------
3141
3142 procedure Analyze_Formal_Subprogram_Declaration (N : Node_Id) is
3143 Spec : constant Node_Id := Specification (N);
3144 Def : constant Node_Id := Default_Name (N);
3145 Nam : constant Entity_Id := Defining_Unit_Name (Spec);
3146 Subp : Entity_Id;
3147
3148 begin
3149 if Nam = Error then
3150 return;
3151 end if;
3152
3153 if Nkind (Nam) = N_Defining_Program_Unit_Name then
3154 Error_Msg_N ("name of formal subprogram must be a direct name", Nam);
3155 goto Leave;
3156 end if;
3157
3158 Analyze_Subprogram_Declaration (N);
3159 Set_Is_Formal_Subprogram (Nam);
3160 Set_Has_Completion (Nam);
3161
3162 if Nkind (N) = N_Formal_Abstract_Subprogram_Declaration then
3163 Set_Is_Abstract_Subprogram (Nam);
3164
3165 Set_Is_Dispatching_Operation (Nam);
3166
3167 -- A formal abstract procedure cannot have a null default
3168 -- (RM 12.6(4.1/2)).
3169
3170 if Nkind (Spec) = N_Procedure_Specification
3171 and then Null_Present (Spec)
3172 then
3173 Error_Msg_N
3174 ("a formal abstract subprogram cannot default to null", Spec);
3175 end if;
3176
3177 declare
3178 Ctrl_Type : constant Entity_Id := Find_Dispatching_Type (Nam);
3179 begin
3180 if No (Ctrl_Type) then
3181 Error_Msg_N
3182 ("abstract formal subprogram must have a controlling type",
3183 N);
3184
3185 elsif Ada_Version >= Ada_2012
3186 and then Is_Incomplete_Type (Ctrl_Type)
3187 then
3188 Error_Msg_NE
3189 ("controlling type of abstract formal subprogram cannot "
3190 & "be incomplete type", N, Ctrl_Type);
3191
3192 else
3193 Check_Controlling_Formals (Ctrl_Type, Nam);
3194 end if;
3195 end;
3196 end if;
3197
3198 -- Default name is resolved at the point of instantiation
3199
3200 if Box_Present (N) then
3201 null;
3202
3203 -- Else default is bound at the point of generic declaration
3204
3205 elsif Present (Def) then
3206 if Nkind (Def) = N_Operator_Symbol then
3207 Find_Direct_Name (Def);
3208
3209 elsif Nkind (Def) /= N_Attribute_Reference then
3210 Analyze (Def);
3211
3212 else
3213 -- For an attribute reference, analyze the prefix and verify
3214 -- that it has the proper profile for the subprogram.
3215
3216 Analyze (Prefix (Def));
3217 Valid_Default_Attribute (Nam, Def);
3218 goto Leave;
3219 end if;
3220
3221 -- Default name may be overloaded, in which case the interpretation
3222 -- with the correct profile must be selected, as for a renaming.
3223 -- If the definition is an indexed component, it must denote a
3224 -- member of an entry family. If it is a selected component, it
3225 -- can be a protected operation.
3226
3227 if Etype (Def) = Any_Type then
3228 goto Leave;
3229
3230 elsif Nkind (Def) = N_Selected_Component then
3231 if not Is_Overloadable (Entity (Selector_Name (Def))) then
3232 Error_Msg_N ("expect valid subprogram name as default", Def);
3233 end if;
3234
3235 elsif Nkind (Def) = N_Indexed_Component then
3236 if Is_Entity_Name (Prefix (Def)) then
3237 if Ekind (Entity (Prefix (Def))) /= E_Entry_Family then
3238 Error_Msg_N ("expect valid subprogram name as default", Def);
3239 end if;
3240
3241 elsif Nkind (Prefix (Def)) = N_Selected_Component then
3242 if Ekind (Entity (Selector_Name (Prefix (Def)))) /=
3243 E_Entry_Family
3244 then
3245 Error_Msg_N ("expect valid subprogram name as default", Def);
3246 end if;
3247
3248 else
3249 Error_Msg_N ("expect valid subprogram name as default", Def);
3250 goto Leave;
3251 end if;
3252
3253 elsif Nkind (Def) = N_Character_Literal then
3254
3255 -- Needs some type checks: subprogram should be parameterless???
3256
3257 Resolve (Def, (Etype (Nam)));
3258
3259 elsif not Is_Entity_Name (Def)
3260 or else not Is_Overloadable (Entity (Def))
3261 then
3262 Error_Msg_N ("expect valid subprogram name as default", Def);
3263 goto Leave;
3264
3265 elsif not Is_Overloaded (Def) then
3266 Subp := Entity (Def);
3267
3268 if Subp = Nam then
3269 Error_Msg_N ("premature usage of formal subprogram", Def);
3270
3271 elsif not Entity_Matches_Spec (Subp, Nam) then
3272 Error_Msg_N ("no visible entity matches specification", Def);
3273 end if;
3274
3275 -- More than one interpretation, so disambiguate as for a renaming
3276
3277 else
3278 declare
3279 I : Interp_Index;
3280 I1 : Interp_Index := 0;
3281 It : Interp;
3282 It1 : Interp;
3283
3284 begin
3285 Subp := Any_Id;
3286 Get_First_Interp (Def, I, It);
3287 while Present (It.Nam) loop
3288 if Entity_Matches_Spec (It.Nam, Nam) then
3289 if Subp /= Any_Id then
3290 It1 := Disambiguate (Def, I1, I, Etype (Subp));
3291
3292 if It1 = No_Interp then
3293 Error_Msg_N ("ambiguous default subprogram", Def);
3294 else
3295 Subp := It1.Nam;
3296 end if;
3297
3298 exit;
3299
3300 else
3301 I1 := I;
3302 Subp := It.Nam;
3303 end if;
3304 end if;
3305
3306 Get_Next_Interp (I, It);
3307 end loop;
3308 end;
3309
3310 if Subp /= Any_Id then
3311
3312 -- Subprogram found, generate reference to it
3313
3314 Set_Entity (Def, Subp);
3315 Generate_Reference (Subp, Def);
3316
3317 if Subp = Nam then
3318 Error_Msg_N ("premature usage of formal subprogram", Def);
3319
3320 elsif Ekind (Subp) /= E_Operator then
3321 Check_Mode_Conformant (Subp, Nam);
3322 end if;
3323
3324 else
3325 Error_Msg_N ("no visible subprogram matches specification", N);
3326 end if;
3327 end if;
3328 end if;
3329
3330 <<Leave>>
3331 if Has_Aspects (N) then
3332 Analyze_Aspect_Specifications (N, Nam);
3333 end if;
3334
3335 end Analyze_Formal_Subprogram_Declaration;
3336
3337 -------------------------------------
3338 -- Analyze_Formal_Type_Declaration --
3339 -------------------------------------
3340
3341 procedure Analyze_Formal_Type_Declaration (N : Node_Id) is
3342 Def : constant Node_Id := Formal_Type_Definition (N);
3343 T : Entity_Id;
3344
3345 begin
3346 T := Defining_Identifier (N);
3347
3348 if Present (Discriminant_Specifications (N))
3349 and then Nkind (Def) /= N_Formal_Private_Type_Definition
3350 then
3351 Error_Msg_N
3352 ("discriminants not allowed for this formal type", T);
3353 end if;
3354
3355 -- Enter the new name, and branch to specific routine
3356
3357 case Nkind (Def) is
3358 when N_Formal_Private_Type_Definition =>
3359 Analyze_Formal_Private_Type (N, T, Def);
3360
3361 when N_Formal_Derived_Type_Definition =>
3362 Analyze_Formal_Derived_Type (N, T, Def);
3363
3364 when N_Formal_Incomplete_Type_Definition =>
3365 Analyze_Formal_Incomplete_Type (T, Def);
3366
3367 when N_Formal_Discrete_Type_Definition =>
3368 Analyze_Formal_Discrete_Type (T, Def);
3369
3370 when N_Formal_Signed_Integer_Type_Definition =>
3371 Analyze_Formal_Signed_Integer_Type (T, Def);
3372
3373 when N_Formal_Modular_Type_Definition =>
3374 Analyze_Formal_Modular_Type (T, Def);
3375
3376 when N_Formal_Floating_Point_Definition =>
3377 Analyze_Formal_Floating_Type (T, Def);
3378
3379 when N_Formal_Ordinary_Fixed_Point_Definition =>
3380 Analyze_Formal_Ordinary_Fixed_Point_Type (T, Def);
3381
3382 when N_Formal_Decimal_Fixed_Point_Definition =>
3383 Analyze_Formal_Decimal_Fixed_Point_Type (T, Def);
3384
3385 when N_Array_Type_Definition =>
3386 Analyze_Formal_Array_Type (T, Def);
3387
3388 when N_Access_Function_Definition
3389 | N_Access_Procedure_Definition
3390 | N_Access_To_Object_Definition
3391 =>
3392 Analyze_Generic_Access_Type (T, Def);
3393
3394 -- Ada 2005: a interface declaration is encoded as an abstract
3395 -- record declaration or a abstract type derivation.
3396
3397 when N_Record_Definition =>
3398 Analyze_Formal_Interface_Type (N, T, Def);
3399
3400 when N_Derived_Type_Definition =>
3401 Analyze_Formal_Derived_Interface_Type (N, T, Def);
3402
3403 when N_Error =>
3404 null;
3405
3406 when others =>
3407 raise Program_Error;
3408 end case;
3409
3410 Set_Is_Generic_Type (T);
3411
3412 if Has_Aspects (N) then
3413 Analyze_Aspect_Specifications (N, T);
3414 end if;
3415 end Analyze_Formal_Type_Declaration;
3416
3417 ------------------------------------
3418 -- Analyze_Function_Instantiation --
3419 ------------------------------------
3420
3421 procedure Analyze_Function_Instantiation (N : Node_Id) is
3422 begin
3423 Analyze_Subprogram_Instantiation (N, E_Function);
3424 end Analyze_Function_Instantiation;
3425
3426 ---------------------------------
3427 -- Analyze_Generic_Access_Type --
3428 ---------------------------------
3429
3430 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id) is
3431 begin
3432 Enter_Name (T);
3433
3434 if Nkind (Def) = N_Access_To_Object_Definition then
3435 Access_Type_Declaration (T, Def);
3436
3437 if Is_Incomplete_Or_Private_Type (Designated_Type (T))
3438 and then No (Full_View (Designated_Type (T)))
3439 and then not Is_Generic_Type (Designated_Type (T))
3440 then
3441 Error_Msg_N ("premature usage of incomplete type", Def);
3442
3443 elsif not Is_Entity_Name (Subtype_Indication (Def)) then
3444 Error_Msg_N
3445 ("only a subtype mark is allowed in a formal", Def);
3446 end if;
3447
3448 else
3449 Access_Subprogram_Declaration (T, Def);
3450 end if;
3451 end Analyze_Generic_Access_Type;
3452
3453 ---------------------------------
3454 -- Analyze_Generic_Formal_Part --
3455 ---------------------------------
3456
3457 procedure Analyze_Generic_Formal_Part (N : Node_Id) is
3458 Gen_Parm_Decl : Node_Id;
3459
3460 begin
3461 -- The generic formals are processed in the scope of the generic unit,
3462 -- where they are immediately visible. The scope is installed by the
3463 -- caller.
3464
3465 Gen_Parm_Decl := First (Generic_Formal_Declarations (N));
3466 while Present (Gen_Parm_Decl) loop
3467 Analyze (Gen_Parm_Decl);
3468 Next (Gen_Parm_Decl);
3469 end loop;
3470
3471 Generate_Reference_To_Generic_Formals (Current_Scope);
3472 end Analyze_Generic_Formal_Part;
3473
3474 ------------------------------------------
3475 -- Analyze_Generic_Package_Declaration --
3476 ------------------------------------------
3477
3478 procedure Analyze_Generic_Package_Declaration (N : Node_Id) is
3479 Decls : constant List_Id := Visible_Declarations (Specification (N));
3480 Loc : constant Source_Ptr := Sloc (N);
3481
3482 Decl : Node_Id;
3483 Id : Entity_Id;
3484 New_N : Node_Id;
3485 Renaming : Node_Id;
3486 Save_Parent : Node_Id;
3487
3488 begin
3489 Check_SPARK_05_Restriction ("generic is not allowed", N);
3490
3491 -- We introduce a renaming of the enclosing package, to have a usable
3492 -- entity as the prefix of an expanded name for a local entity of the
3493 -- form Par.P.Q, where P is the generic package. This is because a local
3494 -- entity named P may hide it, so that the usual visibility rules in
3495 -- the instance will not resolve properly.
3496
3497 Renaming :=
3498 Make_Package_Renaming_Declaration (Loc,
3499 Defining_Unit_Name =>
3500 Make_Defining_Identifier (Loc,
3501 Chars => New_External_Name (Chars (Defining_Entity (N)), "GH")),
3502 Name =>
3503 Make_Identifier (Loc, Chars (Defining_Entity (N))));
3504
3505 -- The declaration is inserted before other declarations, but before
3506 -- pragmas that may be library-unit pragmas and must appear before other
3507 -- declarations. The pragma Compile_Time_Error is not in this class, and
3508 -- may contain an expression that includes such a qualified name, so the
3509 -- renaming declaration must appear before it.
3510
3511 -- Are there other pragmas that require this special handling ???
3512
3513 if Present (Decls) then
3514 Decl := First (Decls);
3515 while Present (Decl)
3516 and then Nkind (Decl) = N_Pragma
3517 and then Get_Pragma_Id (Decl) /= Pragma_Compile_Time_Error
3518 loop
3519 Next (Decl);
3520 end loop;
3521
3522 if Present (Decl) then
3523 Insert_Before (Decl, Renaming);
3524 else
3525 Append (Renaming, Visible_Declarations (Specification (N)));
3526 end if;
3527
3528 else
3529 Set_Visible_Declarations (Specification (N), New_List (Renaming));
3530 end if;
3531
3532 -- Create copy of generic unit, and save for instantiation. If the unit
3533 -- is a child unit, do not copy the specifications for the parent, which
3534 -- are not part of the generic tree.
3535
3536 Save_Parent := Parent_Spec (N);
3537 Set_Parent_Spec (N, Empty);
3538
3539 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
3540 Set_Parent_Spec (New_N, Save_Parent);
3541 Rewrite (N, New_N);
3542
3543 -- Once the contents of the generic copy and the template are swapped,
3544 -- do the same for their respective aspect specifications.
3545
3546 Exchange_Aspects (N, New_N);
3547
3548 -- Collect all contract-related source pragmas found within the template
3549 -- and attach them to the contract of the package spec. This contract is
3550 -- used in the capture of global references within annotations.
3551
3552 Create_Generic_Contract (N);
3553
3554 Id := Defining_Entity (N);
3555 Generate_Definition (Id);
3556
3557 -- Expansion is not applied to generic units
3558
3559 Start_Generic;
3560
3561 Enter_Name (Id);
3562 Set_Ekind (Id, E_Generic_Package);
3563 Set_Etype (Id, Standard_Void_Type);
3564
3565 -- Set SPARK_Mode from context
3566
3567 Set_SPARK_Pragma (Id, SPARK_Mode_Pragma);
3568 Set_SPARK_Aux_Pragma (Id, SPARK_Mode_Pragma);
3569 Set_SPARK_Pragma_Inherited (Id);
3570 Set_SPARK_Aux_Pragma_Inherited (Id);
3571
3572 -- Preserve relevant elaboration-related attributes of the context which
3573 -- are no longer available or very expensive to recompute once analysis,
3574 -- resolution, and expansion are over.
3575
3576 Mark_Elaboration_Attributes
3577 (N_Id => Id,
3578 Checks => True);
3579
3580 -- Analyze aspects now, so that generated pragmas appear in the
3581 -- declarations before building and analyzing the generic copy.
3582
3583 if Has_Aspects (N) then
3584 Analyze_Aspect_Specifications (N, Id);
3585 end if;
3586
3587 Push_Scope (Id);
3588 Enter_Generic_Scope (Id);
3589 Set_Inner_Instances (Id, New_Elmt_List);
3590
3591 Set_Categorization_From_Pragmas (N);
3592 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3593
3594 -- Link the declaration of the generic homonym in the generic copy to
3595 -- the package it renames, so that it is always resolved properly.
3596
3597 Set_Generic_Homonym (Id, Defining_Unit_Name (Renaming));
3598 Set_Entity (Associated_Node (Name (Renaming)), Id);
3599
3600 -- For a library unit, we have reconstructed the entity for the unit,
3601 -- and must reset it in the library tables.
3602
3603 if Nkind (Parent (N)) = N_Compilation_Unit then
3604 Set_Cunit_Entity (Current_Sem_Unit, Id);
3605 end if;
3606
3607 Analyze_Generic_Formal_Part (N);
3608
3609 -- After processing the generic formals, analysis proceeds as for a
3610 -- non-generic package.
3611
3612 Analyze (Specification (N));
3613
3614 Validate_Categorization_Dependency (N, Id);
3615
3616 End_Generic;
3617
3618 End_Package_Scope (Id);
3619 Exit_Generic_Scope (Id);
3620
3621 -- If the generic appears within a package unit, the body of that unit
3622 -- has to be present for instantiation and inlining.
3623
3624 if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Declaration then
3625 Set_Body_Needed_For_Inlining
3626 (Defining_Entity (Unit (Cunit (Current_Sem_Unit))));
3627 end if;
3628
3629 if Nkind (Parent (N)) /= N_Compilation_Unit then
3630 Move_Freeze_Nodes (Id, N, Visible_Declarations (Specification (N)));
3631 Move_Freeze_Nodes (Id, N, Private_Declarations (Specification (N)));
3632 Move_Freeze_Nodes (Id, N, Generic_Formal_Declarations (N));
3633
3634 else
3635 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
3636 Validate_RT_RAT_Component (N);
3637
3638 -- If this is a spec without a body, check that generic parameters
3639 -- are referenced.
3640
3641 if not Body_Required (Parent (N)) then
3642 Check_References (Id);
3643 end if;
3644 end if;
3645
3646 -- If there is a specified storage pool in the context, create an
3647 -- aspect on the package declaration, so that it is used in any
3648 -- instance that does not override it.
3649
3650 if Present (Default_Pool) then
3651 declare
3652 ASN : Node_Id;
3653
3654 begin
3655 ASN :=
3656 Make_Aspect_Specification (Loc,
3657 Identifier => Make_Identifier (Loc, Name_Default_Storage_Pool),
3658 Expression => New_Copy (Default_Pool));
3659
3660 if No (Aspect_Specifications (Specification (N))) then
3661 Set_Aspect_Specifications (Specification (N), New_List (ASN));
3662 else
3663 Append (ASN, Aspect_Specifications (Specification (N)));
3664 end if;
3665 end;
3666 end if;
3667 end Analyze_Generic_Package_Declaration;
3668
3669 --------------------------------------------
3670 -- Analyze_Generic_Subprogram_Declaration --
3671 --------------------------------------------
3672
3673 procedure Analyze_Generic_Subprogram_Declaration (N : Node_Id) is
3674 Formals : List_Id;
3675 Id : Entity_Id;
3676 New_N : Node_Id;
3677 Result_Type : Entity_Id;
3678 Save_Parent : Node_Id;
3679 Spec : Node_Id;
3680 Typ : Entity_Id;
3681
3682 begin
3683 Check_SPARK_05_Restriction ("generic is not allowed", N);
3684
3685 -- Create copy of generic unit, and save for instantiation. If the unit
3686 -- is a child unit, do not copy the specifications for the parent, which
3687 -- are not part of the generic tree.
3688
3689 Save_Parent := Parent_Spec (N);
3690 Set_Parent_Spec (N, Empty);
3691
3692 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
3693 Set_Parent_Spec (New_N, Save_Parent);
3694 Rewrite (N, New_N);
3695
3696 -- Once the contents of the generic copy and the template are swapped,
3697 -- do the same for their respective aspect specifications.
3698
3699 Exchange_Aspects (N, New_N);
3700
3701 -- Collect all contract-related source pragmas found within the template
3702 -- and attach them to the contract of the subprogram spec. This contract
3703 -- is used in the capture of global references within annotations.
3704
3705 Create_Generic_Contract (N);
3706
3707 Spec := Specification (N);
3708 Id := Defining_Entity (Spec);
3709 Generate_Definition (Id);
3710
3711 if Nkind (Id) = N_Defining_Operator_Symbol then
3712 Error_Msg_N
3713 ("operator symbol not allowed for generic subprogram", Id);
3714 end if;
3715
3716 Start_Generic;
3717
3718 Enter_Name (Id);
3719 Set_Scope_Depth_Value (Id, Scope_Depth (Current_Scope) + 1);
3720
3721 -- Analyze the aspects of the generic copy to ensure that all generated
3722 -- pragmas (if any) perform their semantic effects.
3723
3724 if Has_Aspects (N) then
3725 Analyze_Aspect_Specifications (N, Id);
3726 end if;
3727
3728 Push_Scope (Id);
3729 Enter_Generic_Scope (Id);
3730 Set_Inner_Instances (Id, New_Elmt_List);
3731 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3732
3733 Analyze_Generic_Formal_Part (N);
3734
3735 if Nkind (Spec) = N_Function_Specification then
3736 Set_Ekind (Id, E_Generic_Function);
3737 else
3738 Set_Ekind (Id, E_Generic_Procedure);
3739 end if;
3740
3741 -- Set SPARK_Mode from context
3742
3743 Set_SPARK_Pragma (Id, SPARK_Mode_Pragma);
3744 Set_SPARK_Pragma_Inherited (Id);
3745
3746 -- Preserve relevant elaboration-related attributes of the context which
3747 -- are no longer available or very expensive to recompute once analysis,
3748 -- resolution, and expansion are over.
3749
3750 Mark_Elaboration_Attributes
3751 (N_Id => Id,
3752 Checks => True);
3753
3754 Formals := Parameter_Specifications (Spec);
3755
3756 if Present (Formals) then
3757 Process_Formals (Formals, Spec);
3758 end if;
3759
3760 if Nkind (Spec) = N_Function_Specification then
3761 if Nkind (Result_Definition (Spec)) = N_Access_Definition then
3762 Result_Type := Access_Definition (Spec, Result_Definition (Spec));
3763 Set_Etype (Id, Result_Type);
3764
3765 -- Check restriction imposed by AI05-073: a generic function
3766 -- cannot return an abstract type or an access to such.
3767
3768 -- This is a binding interpretation should it apply to earlier
3769 -- versions of Ada as well as Ada 2012???
3770
3771 if Is_Abstract_Type (Designated_Type (Result_Type))
3772 and then Ada_Version >= Ada_2012
3773 then
3774 Error_Msg_N
3775 ("generic function cannot have an access result "
3776 & "that designates an abstract type", Spec);
3777 end if;
3778
3779 else
3780 Find_Type (Result_Definition (Spec));
3781 Typ := Entity (Result_Definition (Spec));
3782
3783 if Is_Abstract_Type (Typ)
3784 and then Ada_Version >= Ada_2012
3785 then
3786 Error_Msg_N
3787 ("generic function cannot have abstract result type", Spec);
3788 end if;
3789
3790 -- If a null exclusion is imposed on the result type, then create
3791 -- a null-excluding itype (an access subtype) and use it as the
3792 -- function's Etype.
3793
3794 if Is_Access_Type (Typ)
3795 and then Null_Exclusion_Present (Spec)
3796 then
3797 Set_Etype (Id,
3798 Create_Null_Excluding_Itype
3799 (T => Typ,
3800 Related_Nod => Spec,
3801 Scope_Id => Defining_Unit_Name (Spec)));
3802 else
3803 Set_Etype (Id, Typ);
3804 end if;
3805 end if;
3806
3807 else
3808 Set_Etype (Id, Standard_Void_Type);
3809 end if;
3810
3811 -- For a library unit, we have reconstructed the entity for the unit,
3812 -- and must reset it in the library tables. We also make sure that
3813 -- Body_Required is set properly in the original compilation unit node.
3814
3815 if Nkind (Parent (N)) = N_Compilation_Unit then
3816 Set_Cunit_Entity (Current_Sem_Unit, Id);
3817 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
3818 end if;
3819
3820 -- If the generic appears within a package unit, the body of that unit
3821 -- has to be present for instantiation and inlining.
3822
3823 if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Declaration
3824 and then Unit_Requires_Body (Id)
3825 then
3826 Set_Body_Needed_For_Inlining
3827 (Defining_Entity (Unit (Cunit (Current_Sem_Unit))));
3828 end if;
3829
3830 Set_Categorization_From_Pragmas (N);
3831 Validate_Categorization_Dependency (N, Id);
3832
3833 -- Capture all global references that occur within the profile of the
3834 -- generic subprogram. Aspects are not part of this processing because
3835 -- they must be delayed. If processed now, Save_Global_References will
3836 -- destroy the Associated_Node links and prevent the capture of global
3837 -- references when the contract of the generic subprogram is analyzed.
3838
3839 Save_Global_References (Original_Node (N));
3840
3841 End_Generic;
3842 End_Scope;
3843 Exit_Generic_Scope (Id);
3844 Generate_Reference_To_Formals (Id);
3845
3846 List_Inherited_Pre_Post_Aspects (Id);
3847 end Analyze_Generic_Subprogram_Declaration;
3848
3849 -----------------------------------
3850 -- Analyze_Package_Instantiation --
3851 -----------------------------------
3852
3853 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
3854 -- must be replaced by gotos which jump to the end of the routine in order
3855 -- to restore the Ghost and SPARK modes.
3856
3857 procedure Analyze_Package_Instantiation (N : Node_Id) is
3858 Has_Inline_Always : Boolean := False;
3859
3860 procedure Delay_Descriptors (E : Entity_Id);
3861 -- Delay generation of subprogram descriptors for given entity
3862
3863 function Might_Inline_Subp (Gen_Unit : Entity_Id) return Boolean;
3864 -- If inlining is active and the generic contains inlined subprograms,
3865 -- we instantiate the body. This may cause superfluous instantiations,
3866 -- but it is simpler than detecting the need for the body at the point
3867 -- of inlining, when the context of the instance is not available.
3868
3869 -----------------------
3870 -- Delay_Descriptors --
3871 -----------------------
3872
3873 procedure Delay_Descriptors (E : Entity_Id) is
3874 begin
3875 if not Delay_Subprogram_Descriptors (E) then
3876 Set_Delay_Subprogram_Descriptors (E);
3877 Pending_Descriptor.Append (E);
3878 end if;
3879 end Delay_Descriptors;
3880
3881 -----------------------
3882 -- Might_Inline_Subp --
3883 -----------------------
3884
3885 function Might_Inline_Subp (Gen_Unit : Entity_Id) return Boolean is
3886 E : Entity_Id;
3887
3888 begin
3889 if not Inline_Processing_Required then
3890 return False;
3891
3892 else
3893 E := First_Entity (Gen_Unit);
3894 while Present (E) loop
3895 if Is_Subprogram (E) and then Is_Inlined (E) then
3896 -- Remember if there are any subprograms with Inline_Always
3897
3898 if Has_Pragma_Inline_Always (E) then
3899 Has_Inline_Always := True;
3900 end if;
3901
3902 return True;
3903 end if;
3904
3905 Next_Entity (E);
3906 end loop;
3907 end if;
3908
3909 return False;
3910 end Might_Inline_Subp;
3911
3912 -- Local declarations
3913
3914 Gen_Id : constant Node_Id := Name (N);
3915 Is_Actual_Pack : constant Boolean :=
3916 Is_Internal (Defining_Entity (N));
3917 Loc : constant Source_Ptr := Sloc (N);
3918
3919 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
3920 Saved_ISMP : constant Boolean :=
3921 Ignore_SPARK_Mode_Pragmas_In_Instance;
3922 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
3923 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
3924 -- Save the Ghost and SPARK mode-related data to restore on exit
3925
3926 Saved_Style_Check : constant Boolean := Style_Check;
3927 -- Save style check mode for restore on exit
3928
3929 Act_Decl : Node_Id;
3930 Act_Decl_Name : Node_Id;
3931 Act_Decl_Id : Entity_Id;
3932 Act_Spec : Node_Id;
3933 Act_Tree : Node_Id;
3934 Env_Installed : Boolean := False;
3935 Gen_Decl : Node_Id;
3936 Gen_Spec : Node_Id;
3937 Gen_Unit : Entity_Id;
3938 Inline_Now : Boolean := False;
3939 Needs_Body : Boolean;
3940 Parent_Installed : Boolean := False;
3941 Renaming_List : List_Id;
3942 Unit_Renaming : Node_Id;
3943
3944 Vis_Prims_List : Elist_Id := No_Elist;
3945 -- List of primitives made temporarily visible in the instantiation
3946 -- to match the visibility of the formal type
3947
3948 -- Start of processing for Analyze_Package_Instantiation
3949
3950 begin
3951 -- Preserve relevant elaboration-related attributes of the context which
3952 -- are no longer available or very expensive to recompute once analysis,
3953 -- resolution, and expansion are over.
3954
3955 Mark_Elaboration_Attributes
3956 (N_Id => N,
3957 Checks => True,
3958 Level => True,
3959 Modes => True,
3960 Warnings => True);
3961
3962 Check_SPARK_05_Restriction ("generic is not allowed", N);
3963
3964 -- Very first thing: check for Text_IO special unit in case we are
3965 -- instantiating one of the children of [[Wide_]Wide_]Text_IO.
3966
3967 Check_Text_IO_Special_Unit (Name (N));
3968
3969 -- Make node global for error reporting
3970
3971 Instantiation_Node := N;
3972
3973 -- Case of instantiation of a generic package
3974
3975 if Nkind (N) = N_Package_Instantiation then
3976 Act_Decl_Id := New_Copy (Defining_Entity (N));
3977 Set_Comes_From_Source (Act_Decl_Id, True);
3978
3979 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name then
3980 Act_Decl_Name :=
3981 Make_Defining_Program_Unit_Name (Loc,
3982 Name =>
3983 New_Copy_Tree (Name (Defining_Unit_Name (N))),
3984 Defining_Identifier => Act_Decl_Id);
3985 else
3986 Act_Decl_Name := Act_Decl_Id;
3987 end if;
3988
3989 -- Case of instantiation of a formal package
3990
3991 else
3992 Act_Decl_Id := Defining_Identifier (N);
3993 Act_Decl_Name := Act_Decl_Id;
3994 end if;
3995
3996 Generate_Definition (Act_Decl_Id);
3997 Set_Ekind (Act_Decl_Id, E_Package);
3998
3999 -- Initialize list of incomplete actuals before analysis
4000
4001 Set_Incomplete_Actuals (Act_Decl_Id, New_Elmt_List);
4002
4003 Preanalyze_Actuals (N, Act_Decl_Id);
4004
4005 -- Turn off style checking in instances. If the check is enabled on the
4006 -- generic unit, a warning in an instance would just be noise. If not
4007 -- enabled on the generic, then a warning in an instance is just wrong.
4008 -- This must be done after analyzing the actuals, which do come from
4009 -- source and are subject to style checking.
4010
4011 Style_Check := False;
4012
4013 Init_Env;
4014 Env_Installed := True;
4015
4016 -- Reset renaming map for formal types. The mapping is established
4017 -- when analyzing the generic associations, but some mappings are
4018 -- inherited from formal packages of parent units, and these are
4019 -- constructed when the parents are installed.
4020
4021 Generic_Renamings.Set_Last (0);
4022 Generic_Renamings_HTable.Reset;
4023
4024 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
4025 Gen_Unit := Entity (Gen_Id);
4026
4027 -- A package instantiation is Ghost when it is subject to pragma Ghost
4028 -- or the generic template is Ghost. Set the mode now to ensure that
4029 -- any nodes generated during analysis and expansion are marked as
4030 -- Ghost.
4031
4032 Mark_And_Set_Ghost_Instantiation (N, Gen_Unit);
4033
4034 -- Verify that it is the name of a generic package
4035
4036 -- A visibility glitch: if the instance is a child unit and the generic
4037 -- is the generic unit of a parent instance (i.e. both the parent and
4038 -- the child units are instances of the same package) the name now
4039 -- denotes the renaming within the parent, not the intended generic
4040 -- unit. See if there is a homonym that is the desired generic. The
4041 -- renaming declaration must be visible inside the instance of the
4042 -- child, but not when analyzing the name in the instantiation itself.
4043
4044 if Ekind (Gen_Unit) = E_Package
4045 and then Present (Renamed_Entity (Gen_Unit))
4046 and then In_Open_Scopes (Renamed_Entity (Gen_Unit))
4047 and then Is_Generic_Instance (Renamed_Entity (Gen_Unit))
4048 and then Present (Homonym (Gen_Unit))
4049 then
4050 Gen_Unit := Homonym (Gen_Unit);
4051 end if;
4052
4053 if Etype (Gen_Unit) = Any_Type then
4054 Restore_Env;
4055 goto Leave;
4056
4057 elsif Ekind (Gen_Unit) /= E_Generic_Package then
4058
4059 -- Ada 2005 (AI-50217): Cannot use instance in limited with_clause
4060
4061 if From_Limited_With (Gen_Unit) then
4062 Error_Msg_N
4063 ("cannot instantiate a limited withed package", Gen_Id);
4064 else
4065 Error_Msg_NE
4066 ("& is not the name of a generic package", Gen_Id, Gen_Unit);
4067 end if;
4068
4069 Restore_Env;
4070 goto Leave;
4071 end if;
4072
4073 if In_Extended_Main_Source_Unit (N) then
4074 Set_Is_Instantiated (Gen_Unit);
4075 Generate_Reference (Gen_Unit, N);
4076
4077 if Present (Renamed_Object (Gen_Unit)) then
4078 Set_Is_Instantiated (Renamed_Object (Gen_Unit));
4079 Generate_Reference (Renamed_Object (Gen_Unit), N);
4080 end if;
4081 end if;
4082
4083 if Nkind (Gen_Id) = N_Identifier
4084 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
4085 then
4086 Error_Msg_NE
4087 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
4088
4089 elsif Nkind (Gen_Id) = N_Expanded_Name
4090 and then Is_Child_Unit (Gen_Unit)
4091 and then Nkind (Prefix (Gen_Id)) = N_Identifier
4092 and then Chars (Act_Decl_Id) = Chars (Prefix (Gen_Id))
4093 then
4094 Error_Msg_N
4095 ("& is hidden within declaration of instance ", Prefix (Gen_Id));
4096 end if;
4097
4098 Set_Entity (Gen_Id, Gen_Unit);
4099
4100 -- If generic is a renaming, get original generic unit
4101
4102 if Present (Renamed_Object (Gen_Unit))
4103 and then Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Package
4104 then
4105 Gen_Unit := Renamed_Object (Gen_Unit);
4106 end if;
4107
4108 -- Verify that there are no circular instantiations
4109
4110 if In_Open_Scopes (Gen_Unit) then
4111 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
4112 Restore_Env;
4113 goto Leave;
4114
4115 elsif Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
4116 Error_Msg_Node_2 := Current_Scope;
4117 Error_Msg_NE
4118 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
4119 Circularity_Detected := True;
4120 Restore_Env;
4121 goto Leave;
4122
4123 else
4124 -- If the context of the instance is subject to SPARK_Mode "off" or
4125 -- the annotation is altogether missing, set the global flag which
4126 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
4127 -- the instance.
4128
4129 if SPARK_Mode /= On then
4130 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
4131
4132 -- Mark the instance spec in case the body is instantiated at a
4133 -- later pass. This preserves the original context in effect for
4134 -- the body.
4135
4136 Set_Ignore_SPARK_Mode_Pragmas (Act_Decl_Id);
4137 end if;
4138
4139 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
4140 Gen_Spec := Specification (Gen_Decl);
4141
4142 -- Initialize renamings map, for error checking, and the list that
4143 -- holds private entities whose views have changed between generic
4144 -- definition and instantiation. If this is the instance created to
4145 -- validate an actual package, the instantiation environment is that
4146 -- of the enclosing instance.
4147
4148 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
4149
4150 -- Copy original generic tree, to produce text for instantiation
4151
4152 Act_Tree :=
4153 Copy_Generic_Node
4154 (Original_Node (Gen_Decl), Empty, Instantiating => True);
4155
4156 Act_Spec := Specification (Act_Tree);
4157
4158 -- If this is the instance created to validate an actual package,
4159 -- only the formals matter, do not examine the package spec itself.
4160
4161 if Is_Actual_Pack then
4162 Set_Visible_Declarations (Act_Spec, New_List);
4163 Set_Private_Declarations (Act_Spec, New_List);
4164 end if;
4165
4166 Renaming_List :=
4167 Analyze_Associations
4168 (I_Node => N,
4169 Formals => Generic_Formal_Declarations (Act_Tree),
4170 F_Copy => Generic_Formal_Declarations (Gen_Decl));
4171
4172 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
4173
4174 Set_Instance_Env (Gen_Unit, Act_Decl_Id);
4175 Set_Defining_Unit_Name (Act_Spec, Act_Decl_Name);
4176 Set_Is_Generic_Instance (Act_Decl_Id);
4177 Set_Generic_Parent (Act_Spec, Gen_Unit);
4178
4179 -- References to the generic in its own declaration or its body are
4180 -- references to the instance. Add a renaming declaration for the
4181 -- generic unit itself. This declaration, as well as the renaming
4182 -- declarations for the generic formals, must remain private to the
4183 -- unit: the formals, because this is the language semantics, and
4184 -- the unit because its use is an artifact of the implementation.
4185
4186 Unit_Renaming :=
4187 Make_Package_Renaming_Declaration (Loc,
4188 Defining_Unit_Name =>
4189 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
4190 Name => New_Occurrence_Of (Act_Decl_Id, Loc));
4191
4192 Append (Unit_Renaming, Renaming_List);
4193
4194 -- The renaming declarations are the first local declarations of the
4195 -- new unit.
4196
4197 if Is_Non_Empty_List (Visible_Declarations (Act_Spec)) then
4198 Insert_List_Before
4199 (First (Visible_Declarations (Act_Spec)), Renaming_List);
4200 else
4201 Set_Visible_Declarations (Act_Spec, Renaming_List);
4202 end if;
4203
4204 Act_Decl := Make_Package_Declaration (Loc, Specification => Act_Spec);
4205
4206 -- Propagate the aspect specifications from the package declaration
4207 -- template to the instantiated version of the package declaration.
4208
4209 if Has_Aspects (Act_Tree) then
4210 Set_Aspect_Specifications (Act_Decl,
4211 New_Copy_List_Tree (Aspect_Specifications (Act_Tree)));
4212 end if;
4213
4214 -- The generic may have a generated Default_Storage_Pool aspect,
4215 -- set at the point of generic declaration. If the instance has
4216 -- that aspect, it overrides the one inherited from the generic.
4217
4218 if Has_Aspects (Gen_Spec) then
4219 if No (Aspect_Specifications (N)) then
4220 Set_Aspect_Specifications (N,
4221 (New_Copy_List_Tree
4222 (Aspect_Specifications (Gen_Spec))));
4223
4224 else
4225 declare
4226 ASN1, ASN2 : Node_Id;
4227
4228 begin
4229 ASN1 := First (Aspect_Specifications (N));
4230 while Present (ASN1) loop
4231 if Chars (Identifier (ASN1)) = Name_Default_Storage_Pool
4232 then
4233 -- If generic carries a default storage pool, remove
4234 -- it in favor of the instance one.
4235
4236 ASN2 := First (Aspect_Specifications (Gen_Spec));
4237 while Present (ASN2) loop
4238 if Chars (Identifier (ASN2)) =
4239 Name_Default_Storage_Pool
4240 then
4241 Remove (ASN2);
4242 exit;
4243 end if;
4244
4245 Next (ASN2);
4246 end loop;
4247 end if;
4248
4249 Next (ASN1);
4250 end loop;
4251
4252 Prepend_List_To (Aspect_Specifications (N),
4253 (New_Copy_List_Tree
4254 (Aspect_Specifications (Gen_Spec))));
4255 end;
4256 end if;
4257 end if;
4258
4259 -- Save the instantiation node, for subsequent instantiation of the
4260 -- body, if there is one and we are generating code for the current
4261 -- unit. Mark unit as having a body (avoids premature error message).
4262
4263 -- We instantiate the body if we are generating code, if we are
4264 -- generating cross-reference information, or if we are building
4265 -- trees for ASIS use or GNATprove use.
4266
4267 declare
4268 Enclosing_Body_Present : Boolean := False;
4269 -- If the generic unit is not a compilation unit, then a body may
4270 -- be present in its parent even if none is required. We create a
4271 -- tentative pending instantiation for the body, which will be
4272 -- discarded if none is actually present.
4273
4274 Scop : Entity_Id;
4275
4276 begin
4277 if Scope (Gen_Unit) /= Standard_Standard
4278 and then not Is_Child_Unit (Gen_Unit)
4279 then
4280 Scop := Scope (Gen_Unit);
4281 while Present (Scop) and then Scop /= Standard_Standard loop
4282 if Unit_Requires_Body (Scop) then
4283 Enclosing_Body_Present := True;
4284 exit;
4285
4286 elsif In_Open_Scopes (Scop)
4287 and then In_Package_Body (Scop)
4288 then
4289 Enclosing_Body_Present := True;
4290 exit;
4291 end if;
4292
4293 exit when Is_Compilation_Unit (Scop);
4294 Scop := Scope (Scop);
4295 end loop;
4296 end if;
4297
4298 -- If front-end inlining is enabled or there are any subprograms
4299 -- marked with Inline_Always, and this is a unit for which code
4300 -- will be generated, we instantiate the body at once.
4301
4302 -- This is done if the instance is not the main unit, and if the
4303 -- generic is not a child unit of another generic, to avoid scope
4304 -- problems and the reinstallation of parent instances.
4305
4306 if Expander_Active
4307 and then (not Is_Child_Unit (Gen_Unit)
4308 or else not Is_Generic_Unit (Scope (Gen_Unit)))
4309 and then Might_Inline_Subp (Gen_Unit)
4310 and then not Is_Actual_Pack
4311 then
4312 if not Back_End_Inlining
4313 and then (Front_End_Inlining or else Has_Inline_Always)
4314 and then (Is_In_Main_Unit (N)
4315 or else In_Main_Context (Current_Scope))
4316 and then Nkind (Parent (N)) /= N_Compilation_Unit
4317 then
4318 Inline_Now := True;
4319
4320 -- In configurable_run_time mode we force the inlining of
4321 -- predefined subprograms marked Inline_Always, to minimize
4322 -- the use of the run-time library.
4323
4324 elsif In_Predefined_Unit (Gen_Decl)
4325 and then Configurable_Run_Time_Mode
4326 and then Nkind (Parent (N)) /= N_Compilation_Unit
4327 then
4328 Inline_Now := True;
4329 end if;
4330
4331 -- If the current scope is itself an instance within a child
4332 -- unit, there will be duplications in the scope stack, and the
4333 -- unstacking mechanism in Inline_Instance_Body will fail.
4334 -- This loses some rare cases of optimization, and might be
4335 -- improved some day, if we can find a proper abstraction for
4336 -- "the complete compilation context" that can be saved and
4337 -- restored. ???
4338
4339 if Is_Generic_Instance (Current_Scope) then
4340 declare
4341 Curr_Unit : constant Entity_Id :=
4342 Cunit_Entity (Current_Sem_Unit);
4343 begin
4344 if Curr_Unit /= Current_Scope
4345 and then Is_Child_Unit (Curr_Unit)
4346 then
4347 Inline_Now := False;
4348 end if;
4349 end;
4350 end if;
4351 end if;
4352
4353 Needs_Body :=
4354 (Unit_Requires_Body (Gen_Unit)
4355 or else Enclosing_Body_Present
4356 or else Present (Corresponding_Body (Gen_Decl)))
4357 and then (Is_In_Main_Unit (N)
4358 or else Might_Inline_Subp (Gen_Unit))
4359 and then not Is_Actual_Pack
4360 and then not Inline_Now
4361 and then (Operating_Mode = Generate_Code
4362
4363 -- Need comment for this check ???
4364
4365 or else (Operating_Mode = Check_Semantics
4366 and then (ASIS_Mode or GNATprove_Mode)));
4367
4368 -- If front-end inlining is enabled or there are any subprograms
4369 -- marked with Inline_Always, do not instantiate body when within
4370 -- a generic context.
4371
4372 if ((Front_End_Inlining or else Has_Inline_Always)
4373 and then not Expander_Active)
4374 or else Is_Generic_Unit (Cunit_Entity (Main_Unit))
4375 then
4376 Needs_Body := False;
4377 end if;
4378
4379 -- If the current context is generic, and the package being
4380 -- instantiated is declared within a formal package, there is no
4381 -- body to instantiate until the enclosing generic is instantiated
4382 -- and there is an actual for the formal package. If the formal
4383 -- package has parameters, we build a regular package instance for
4384 -- it, that precedes the original formal package declaration.
4385
4386 if In_Open_Scopes (Scope (Scope (Gen_Unit))) then
4387 declare
4388 Decl : constant Node_Id :=
4389 Original_Node
4390 (Unit_Declaration_Node (Scope (Gen_Unit)));
4391 begin
4392 if Nkind (Decl) = N_Formal_Package_Declaration
4393 or else (Nkind (Decl) = N_Package_Declaration
4394 and then Is_List_Member (Decl)
4395 and then Present (Next (Decl))
4396 and then
4397 Nkind (Next (Decl)) =
4398 N_Formal_Package_Declaration)
4399 then
4400 Needs_Body := False;
4401 end if;
4402 end;
4403 end if;
4404 end;
4405
4406 -- For RCI unit calling stubs, we omit the instance body if the
4407 -- instance is the RCI library unit itself.
4408
4409 -- However there is a special case for nested instances: in this case
4410 -- we do generate the instance body, as it might be required, e.g.
4411 -- because it provides stream attributes for some type used in the
4412 -- profile of a remote subprogram. This is consistent with 12.3(12),
4413 -- which indicates that the instance body occurs at the place of the
4414 -- instantiation, and thus is part of the RCI declaration, which is
4415 -- present on all client partitions (this is E.2.3(18)).
4416
4417 -- Note that AI12-0002 may make it illegal at some point to have
4418 -- stream attributes defined in an RCI unit, in which case this
4419 -- special case will become unnecessary. In the meantime, there
4420 -- is known application code in production that depends on this
4421 -- being possible, so we definitely cannot eliminate the body in
4422 -- the case of nested instances for the time being.
4423
4424 -- When we generate a nested instance body, calling stubs for any
4425 -- relevant subprogram will be be inserted immediately after the
4426 -- subprogram declarations, and will take precedence over the
4427 -- subsequent (original) body. (The stub and original body will be
4428 -- complete homographs, but this is permitted in an instance).
4429 -- (Could we do better and remove the original body???)
4430
4431 if Distribution_Stub_Mode = Generate_Caller_Stub_Body
4432 and then Comes_From_Source (N)
4433 and then Nkind (Parent (N)) = N_Compilation_Unit
4434 then
4435 Needs_Body := False;
4436 end if;
4437
4438 if Needs_Body then
4439
4440 -- Here is a defence against a ludicrous number of instantiations
4441 -- caused by a circular set of instantiation attempts.
4442
4443 if Pending_Instantiations.Last > Maximum_Instantiations then
4444 Error_Msg_Uint_1 := UI_From_Int (Maximum_Instantiations);
4445 Error_Msg_N ("too many instantiations, exceeds max of^", N);
4446 Error_Msg_N ("\limit can be changed using -gnateinn switch", N);
4447 raise Unrecoverable_Error;
4448 end if;
4449
4450 -- Indicate that the enclosing scopes contain an instantiation,
4451 -- and that cleanup actions should be delayed until after the
4452 -- instance body is expanded.
4453
4454 Check_Forward_Instantiation (Gen_Decl);
4455 if Nkind (N) = N_Package_Instantiation then
4456 declare
4457 Enclosing_Master : Entity_Id;
4458
4459 begin
4460 -- Loop to search enclosing masters
4461
4462 Enclosing_Master := Current_Scope;
4463 Scope_Loop : while Enclosing_Master /= Standard_Standard loop
4464 if Ekind (Enclosing_Master) = E_Package then
4465 if Is_Compilation_Unit (Enclosing_Master) then
4466 if In_Package_Body (Enclosing_Master) then
4467 Delay_Descriptors
4468 (Body_Entity (Enclosing_Master));
4469 else
4470 Delay_Descriptors
4471 (Enclosing_Master);
4472 end if;
4473
4474 exit Scope_Loop;
4475
4476 else
4477 Enclosing_Master := Scope (Enclosing_Master);
4478 end if;
4479
4480 elsif Is_Generic_Unit (Enclosing_Master)
4481 or else Ekind (Enclosing_Master) = E_Void
4482 then
4483 -- Cleanup actions will eventually be performed on the
4484 -- enclosing subprogram or package instance, if any.
4485 -- Enclosing scope is void in the formal part of a
4486 -- generic subprogram.
4487
4488 exit Scope_Loop;
4489
4490 else
4491 if Ekind (Enclosing_Master) = E_Entry
4492 and then
4493 Ekind (Scope (Enclosing_Master)) = E_Protected_Type
4494 then
4495 if not Expander_Active then
4496 exit Scope_Loop;
4497 else
4498 Enclosing_Master :=
4499 Protected_Body_Subprogram (Enclosing_Master);
4500 end if;
4501 end if;
4502
4503 Set_Delay_Cleanups (Enclosing_Master);
4504
4505 while Ekind (Enclosing_Master) = E_Block loop
4506 Enclosing_Master := Scope (Enclosing_Master);
4507 end loop;
4508
4509 if Is_Subprogram (Enclosing_Master) then
4510 Delay_Descriptors (Enclosing_Master);
4511
4512 elsif Is_Task_Type (Enclosing_Master) then
4513 declare
4514 TBP : constant Node_Id :=
4515 Get_Task_Body_Procedure
4516 (Enclosing_Master);
4517 begin
4518 if Present (TBP) then
4519 Delay_Descriptors (TBP);
4520 Set_Delay_Cleanups (TBP);
4521 end if;
4522 end;
4523 end if;
4524
4525 exit Scope_Loop;
4526 end if;
4527 end loop Scope_Loop;
4528 end;
4529
4530 -- Make entry in table
4531
4532 Add_Pending_Instantiation (N, Act_Decl);
4533 end if;
4534 end if;
4535
4536 Set_Categorization_From_Pragmas (Act_Decl);
4537
4538 if Parent_Installed then
4539 Hide_Current_Scope;
4540 end if;
4541
4542 Set_Instance_Spec (N, Act_Decl);
4543
4544 -- If not a compilation unit, insert the package declaration before
4545 -- the original instantiation node.
4546
4547 if Nkind (Parent (N)) /= N_Compilation_Unit then
4548 Mark_Rewrite_Insertion (Act_Decl);
4549 Insert_Before (N, Act_Decl);
4550
4551 if Has_Aspects (N) then
4552 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4553
4554 -- The pragma created for a Default_Storage_Pool aspect must
4555 -- appear ahead of the declarations in the instance spec.
4556 -- Analysis has placed it after the instance node, so remove
4557 -- it and reinsert it properly now.
4558
4559 declare
4560 ASN : constant Node_Id := First (Aspect_Specifications (N));
4561 A_Name : constant Name_Id := Chars (Identifier (ASN));
4562 Decl : Node_Id;
4563
4564 begin
4565 if A_Name = Name_Default_Storage_Pool then
4566 if No (Visible_Declarations (Act_Spec)) then
4567 Set_Visible_Declarations (Act_Spec, New_List);
4568 end if;
4569
4570 Decl := Next (N);
4571 while Present (Decl) loop
4572 if Nkind (Decl) = N_Pragma then
4573 Remove (Decl);
4574 Prepend (Decl, Visible_Declarations (Act_Spec));
4575 exit;
4576 end if;
4577
4578 Next (Decl);
4579 end loop;
4580 end if;
4581 end;
4582 end if;
4583
4584 Analyze (Act_Decl);
4585
4586 -- For an instantiation that is a compilation unit, place
4587 -- declaration on current node so context is complete for analysis
4588 -- (including nested instantiations). If this is the main unit,
4589 -- the declaration eventually replaces the instantiation node.
4590 -- If the instance body is created later, it replaces the
4591 -- instance node, and the declaration is attached to it
4592 -- (see Build_Instance_Compilation_Unit_Nodes).
4593
4594 else
4595 if Cunit_Entity (Current_Sem_Unit) = Defining_Entity (N) then
4596
4597 -- The entity for the current unit is the newly created one,
4598 -- and all semantic information is attached to it.
4599
4600 Set_Cunit_Entity (Current_Sem_Unit, Act_Decl_Id);
4601
4602 -- If this is the main unit, replace the main entity as well
4603
4604 if Current_Sem_Unit = Main_Unit then
4605 Main_Unit_Entity := Act_Decl_Id;
4606 end if;
4607 end if;
4608
4609 Set_Unit (Parent (N), Act_Decl);
4610 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
4611 Set_Package_Instantiation (Act_Decl_Id, N);
4612
4613 -- Process aspect specifications of the instance node, if any, to
4614 -- take into account categorization pragmas before analyzing the
4615 -- instance.
4616
4617 if Has_Aspects (N) then
4618 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4619 end if;
4620
4621 Analyze (Act_Decl);
4622 Set_Unit (Parent (N), N);
4623 Set_Body_Required (Parent (N), False);
4624
4625 -- We never need elaboration checks on instantiations, since by
4626 -- definition, the body instantiation is elaborated at the same
4627 -- time as the spec instantiation.
4628
4629 if Legacy_Elaboration_Checks then
4630 Set_Kill_Elaboration_Checks (Act_Decl_Id);
4631 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
4632 end if;
4633 end if;
4634
4635 if Legacy_Elaboration_Checks then
4636 Check_Elab_Instantiation (N);
4637 end if;
4638
4639 -- Save the scenario for later examination by the ABE Processing
4640 -- phase.
4641
4642 Record_Elaboration_Scenario (N);
4643
4644 -- The instantiation results in a guaranteed ABE
4645
4646 if Is_Known_Guaranteed_ABE (N) and then Needs_Body then
4647
4648 -- Do not instantiate the corresponding body because gigi cannot
4649 -- handle certain types of premature instantiations.
4650
4651 Pending_Instantiations.Decrement_Last;
4652
4653 -- Create completing bodies for all subprogram declarations since
4654 -- their real bodies will not be instantiated.
4655
4656 Provide_Completing_Bodies (Instance_Spec (N));
4657 end if;
4658
4659 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
4660
4661 Set_First_Private_Entity (Defining_Unit_Name (Unit_Renaming),
4662 First_Private_Entity (Act_Decl_Id));
4663
4664 -- If the instantiation will receive a body, the unit will be
4665 -- transformed into a package body, and receive its own elaboration
4666 -- entity. Otherwise, the nature of the unit is now a package
4667 -- declaration.
4668
4669 if Nkind (Parent (N)) = N_Compilation_Unit
4670 and then not Needs_Body
4671 then
4672 Rewrite (N, Act_Decl);
4673 end if;
4674
4675 if Present (Corresponding_Body (Gen_Decl))
4676 or else Unit_Requires_Body (Gen_Unit)
4677 then
4678 Set_Has_Completion (Act_Decl_Id);
4679 end if;
4680
4681 Check_Formal_Packages (Act_Decl_Id);
4682
4683 Restore_Hidden_Primitives (Vis_Prims_List);
4684 Restore_Private_Views (Act_Decl_Id);
4685
4686 Inherit_Context (Gen_Decl, N);
4687
4688 if Parent_Installed then
4689 Remove_Parent;
4690 end if;
4691
4692 Restore_Env;
4693 Env_Installed := False;
4694 end if;
4695
4696 Validate_Categorization_Dependency (N, Act_Decl_Id);
4697
4698 -- There used to be a check here to prevent instantiations in local
4699 -- contexts if the No_Local_Allocators restriction was active. This
4700 -- check was removed by a binding interpretation in AI-95-00130/07,
4701 -- but we retain the code for documentation purposes.
4702
4703 -- if Ekind (Act_Decl_Id) /= E_Void
4704 -- and then not Is_Library_Level_Entity (Act_Decl_Id)
4705 -- then
4706 -- Check_Restriction (No_Local_Allocators, N);
4707 -- end if;
4708
4709 if Inline_Now then
4710 Inline_Instance_Body (N, Gen_Unit, Act_Decl);
4711 end if;
4712
4713 -- The following is a tree patch for ASIS: ASIS needs separate nodes to
4714 -- be used as defining identifiers for a formal package and for the
4715 -- corresponding expanded package.
4716
4717 if Nkind (N) = N_Formal_Package_Declaration then
4718 Act_Decl_Id := New_Copy (Defining_Entity (N));
4719 Set_Comes_From_Source (Act_Decl_Id, True);
4720 Set_Is_Generic_Instance (Act_Decl_Id, False);
4721 Set_Defining_Identifier (N, Act_Decl_Id);
4722 end if;
4723
4724 -- Check that if N is an instantiation of System.Dim_Float_IO or
4725 -- System.Dim_Integer_IO, the formal type has a dimension system.
4726
4727 if Nkind (N) = N_Package_Instantiation
4728 and then Is_Dim_IO_Package_Instantiation (N)
4729 then
4730 declare
4731 Assoc : constant Node_Id := First (Generic_Associations (N));
4732 begin
4733 if not Has_Dimension_System
4734 (Etype (Explicit_Generic_Actual_Parameter (Assoc)))
4735 then
4736 Error_Msg_N ("type with a dimension system expected", Assoc);
4737 end if;
4738 end;
4739 end if;
4740
4741 <<Leave>>
4742 if Has_Aspects (N) and then Nkind (Parent (N)) /= N_Compilation_Unit then
4743 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4744 end if;
4745
4746 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
4747 Restore_Ghost_Mode (Saved_GM);
4748 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
4749 Style_Check := Saved_Style_Check;
4750
4751 exception
4752 when Instantiation_Error =>
4753 if Parent_Installed then
4754 Remove_Parent;
4755 end if;
4756
4757 if Env_Installed then
4758 Restore_Env;
4759 end if;
4760
4761 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
4762 Restore_Ghost_Mode (Saved_GM);
4763 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
4764 Style_Check := Saved_Style_Check;
4765 end Analyze_Package_Instantiation;
4766
4767 --------------------------
4768 -- Inline_Instance_Body --
4769 --------------------------
4770
4771 -- WARNING: This routine manages SPARK regions. Return statements must be
4772 -- replaced by gotos which jump to the end of the routine and restore the
4773 -- SPARK mode.
4774
4775 procedure Inline_Instance_Body
4776 (N : Node_Id;
4777 Gen_Unit : Entity_Id;
4778 Act_Decl : Node_Id)
4779 is
4780 Curr_Comp : constant Node_Id := Cunit (Current_Sem_Unit);
4781 Curr_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
4782 Gen_Comp : constant Entity_Id :=
4783 Cunit_Entity (Get_Source_Unit (Gen_Unit));
4784
4785 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
4786 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
4787 -- Save the SPARK mode-related data to restore on exit. Removing
4788 -- enclosing scopes to provide a clean environment for analysis of
4789 -- the inlined body will eliminate any previously set SPARK_Mode.
4790
4791 Scope_Stack_Depth : constant Pos :=
4792 Scope_Stack.Last - Scope_Stack.First + 1;
4793
4794 Inner_Scopes : array (1 .. Scope_Stack_Depth) of Entity_Id;
4795 Instances : array (1 .. Scope_Stack_Depth) of Entity_Id;
4796 Use_Clauses : array (1 .. Scope_Stack_Depth) of Node_Id;
4797
4798 Curr_Scope : Entity_Id := Empty;
4799 List : Elist_Id := No_Elist; -- init to avoid warning
4800 N_Instances : Nat := 0;
4801 Num_Inner : Nat := 0;
4802 Num_Scopes : Nat := 0;
4803 Removed : Boolean := False;
4804 S : Entity_Id;
4805 Vis : Boolean;
4806
4807 begin
4808 -- Case of generic unit defined in another unit. We must remove the
4809 -- complete context of the current unit to install that of the generic.
4810
4811 if Gen_Comp /= Cunit_Entity (Current_Sem_Unit) then
4812
4813 -- Add some comments for the following two loops ???
4814
4815 S := Current_Scope;
4816 while Present (S) and then S /= Standard_Standard loop
4817 loop
4818 Num_Scopes := Num_Scopes + 1;
4819
4820 Use_Clauses (Num_Scopes) :=
4821 (Scope_Stack.Table
4822 (Scope_Stack.Last - Num_Scopes + 1).
4823 First_Use_Clause);
4824 End_Use_Clauses (Use_Clauses (Num_Scopes));
4825
4826 exit when Scope_Stack.Last - Num_Scopes + 1 = Scope_Stack.First
4827 or else Scope_Stack.Table
4828 (Scope_Stack.Last - Num_Scopes).Entity = Scope (S);
4829 end loop;
4830
4831 exit when Is_Generic_Instance (S)
4832 and then (In_Package_Body (S)
4833 or else Ekind (S) = E_Procedure
4834 or else Ekind (S) = E_Function);
4835 S := Scope (S);
4836 end loop;
4837
4838 Vis := Is_Immediately_Visible (Gen_Comp);
4839
4840 -- Find and save all enclosing instances
4841
4842 S := Current_Scope;
4843
4844 while Present (S)
4845 and then S /= Standard_Standard
4846 loop
4847 if Is_Generic_Instance (S) then
4848 N_Instances := N_Instances + 1;
4849 Instances (N_Instances) := S;
4850
4851 exit when In_Package_Body (S);
4852 end if;
4853
4854 S := Scope (S);
4855 end loop;
4856
4857 -- Remove context of current compilation unit, unless we are within a
4858 -- nested package instantiation, in which case the context has been
4859 -- removed previously.
4860
4861 -- If current scope is the body of a child unit, remove context of
4862 -- spec as well. If an enclosing scope is an instance body, the
4863 -- context has already been removed, but the entities in the body
4864 -- must be made invisible as well.
4865
4866 S := Current_Scope;
4867 while Present (S) and then S /= Standard_Standard loop
4868 if Is_Generic_Instance (S)
4869 and then (In_Package_Body (S)
4870 or else Ekind_In (S, E_Procedure, E_Function))
4871 then
4872 -- We still have to remove the entities of the enclosing
4873 -- instance from direct visibility.
4874
4875 declare
4876 E : Entity_Id;
4877 begin
4878 E := First_Entity (S);
4879 while Present (E) loop
4880 Set_Is_Immediately_Visible (E, False);
4881 Next_Entity (E);
4882 end loop;
4883 end;
4884
4885 exit;
4886 end if;
4887
4888 if S = Curr_Unit
4889 or else (Ekind (Curr_Unit) = E_Package_Body
4890 and then S = Spec_Entity (Curr_Unit))
4891 or else (Ekind (Curr_Unit) = E_Subprogram_Body
4892 and then S = Corresponding_Spec
4893 (Unit_Declaration_Node (Curr_Unit)))
4894 then
4895 Removed := True;
4896
4897 -- Remove entities in current scopes from visibility, so that
4898 -- instance body is compiled in a clean environment.
4899
4900 List := Save_Scope_Stack (Handle_Use => False);
4901
4902 if Is_Child_Unit (S) then
4903
4904 -- Remove child unit from stack, as well as inner scopes.
4905 -- Removing the context of a child unit removes parent units
4906 -- as well.
4907
4908 while Current_Scope /= S loop
4909 Num_Inner := Num_Inner + 1;
4910 Inner_Scopes (Num_Inner) := Current_Scope;
4911 Pop_Scope;
4912 end loop;
4913
4914 Pop_Scope;
4915 Remove_Context (Curr_Comp);
4916 Curr_Scope := S;
4917
4918 else
4919 Remove_Context (Curr_Comp);
4920 end if;
4921
4922 if Ekind (Curr_Unit) = E_Package_Body then
4923 Remove_Context (Library_Unit (Curr_Comp));
4924 end if;
4925 end if;
4926
4927 S := Scope (S);
4928 end loop;
4929
4930 pragma Assert (Num_Inner < Num_Scopes);
4931
4932 -- The inlined package body must be analyzed with the SPARK_Mode of
4933 -- the enclosing context, otherwise the body may cause bogus errors
4934 -- if a configuration SPARK_Mode pragma in in effect.
4935
4936 Push_Scope (Standard_Standard);
4937 Scope_Stack.Table (Scope_Stack.Last).Is_Active_Stack_Base := True;
4938 Instantiate_Package_Body
4939 (Body_Info =>
4940 ((Inst_Node => N,
4941 Act_Decl => Act_Decl,
4942 Expander_Status => Expander_Active,
4943 Current_Sem_Unit => Current_Sem_Unit,
4944 Scope_Suppress => Scope_Suppress,
4945 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
4946 Version => Ada_Version,
4947 Version_Pragma => Ada_Version_Pragma,
4948 Warnings => Save_Warnings,
4949 SPARK_Mode => Saved_SM,
4950 SPARK_Mode_Pragma => Saved_SMP)),
4951 Inlined_Body => True);
4952
4953 Pop_Scope;
4954
4955 -- Restore context
4956
4957 Set_Is_Immediately_Visible (Gen_Comp, Vis);
4958
4959 -- Reset Generic_Instance flag so that use clauses can be installed
4960 -- in the proper order. (See Use_One_Package for effect of enclosing
4961 -- instances on processing of use clauses).
4962
4963 for J in 1 .. N_Instances loop
4964 Set_Is_Generic_Instance (Instances (J), False);
4965 end loop;
4966
4967 if Removed then
4968 Install_Context (Curr_Comp, Chain => False);
4969
4970 if Present (Curr_Scope)
4971 and then Is_Child_Unit (Curr_Scope)
4972 then
4973 Push_Scope (Curr_Scope);
4974 Set_Is_Immediately_Visible (Curr_Scope);
4975
4976 -- Finally, restore inner scopes as well
4977
4978 for J in reverse 1 .. Num_Inner loop
4979 Push_Scope (Inner_Scopes (J));
4980 end loop;
4981 end if;
4982
4983 Restore_Scope_Stack (List, Handle_Use => False);
4984
4985 if Present (Curr_Scope)
4986 and then
4987 (In_Private_Part (Curr_Scope)
4988 or else In_Package_Body (Curr_Scope))
4989 then
4990 -- Install private declaration of ancestor units, which are
4991 -- currently available. Restore_Scope_Stack and Install_Context
4992 -- only install the visible part of parents.
4993
4994 declare
4995 Par : Entity_Id;
4996 begin
4997 Par := Scope (Curr_Scope);
4998 while (Present (Par)) and then Par /= Standard_Standard loop
4999 Install_Private_Declarations (Par);
5000 Par := Scope (Par);
5001 end loop;
5002 end;
5003 end if;
5004 end if;
5005
5006 -- Restore use clauses. For a child unit, use clauses in the parents
5007 -- are restored when installing the context, so only those in inner
5008 -- scopes (and those local to the child unit itself) need to be
5009 -- installed explicitly.
5010
5011 if Is_Child_Unit (Curr_Unit) and then Removed then
5012 for J in reverse 1 .. Num_Inner + 1 loop
5013 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
5014 Use_Clauses (J);
5015 Install_Use_Clauses (Use_Clauses (J));
5016 end loop;
5017
5018 else
5019 for J in reverse 1 .. Num_Scopes loop
5020 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
5021 Use_Clauses (J);
5022 Install_Use_Clauses (Use_Clauses (J));
5023 end loop;
5024 end if;
5025
5026 -- Restore status of instances. If one of them is a body, make its
5027 -- local entities visible again.
5028
5029 declare
5030 E : Entity_Id;
5031 Inst : Entity_Id;
5032
5033 begin
5034 for J in 1 .. N_Instances loop
5035 Inst := Instances (J);
5036 Set_Is_Generic_Instance (Inst, True);
5037
5038 if In_Package_Body (Inst)
5039 or else Ekind_In (S, E_Procedure, E_Function)
5040 then
5041 E := First_Entity (Instances (J));
5042 while Present (E) loop
5043 Set_Is_Immediately_Visible (E);
5044 Next_Entity (E);
5045 end loop;
5046 end if;
5047 end loop;
5048 end;
5049
5050 -- If generic unit is in current unit, current context is correct. Note
5051 -- that the context is guaranteed to carry the correct SPARK_Mode as no
5052 -- enclosing scopes were removed.
5053
5054 else
5055 Instantiate_Package_Body
5056 (Body_Info =>
5057 ((Inst_Node => N,
5058 Act_Decl => Act_Decl,
5059 Expander_Status => Expander_Active,
5060 Current_Sem_Unit => Current_Sem_Unit,
5061 Scope_Suppress => Scope_Suppress,
5062 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
5063 Version => Ada_Version,
5064 Version_Pragma => Ada_Version_Pragma,
5065 Warnings => Save_Warnings,
5066 SPARK_Mode => SPARK_Mode,
5067 SPARK_Mode_Pragma => SPARK_Mode_Pragma)),
5068 Inlined_Body => True);
5069 end if;
5070 end Inline_Instance_Body;
5071
5072 -------------------------------------
5073 -- Analyze_Procedure_Instantiation --
5074 -------------------------------------
5075
5076 procedure Analyze_Procedure_Instantiation (N : Node_Id) is
5077 begin
5078 Analyze_Subprogram_Instantiation (N, E_Procedure);
5079 end Analyze_Procedure_Instantiation;
5080
5081 -----------------------------------
5082 -- Need_Subprogram_Instance_Body --
5083 -----------------------------------
5084
5085 function Need_Subprogram_Instance_Body
5086 (N : Node_Id;
5087 Subp : Entity_Id) return Boolean
5088 is
5089 function Is_Inlined_Or_Child_Of_Inlined (E : Entity_Id) return Boolean;
5090 -- Return True if E is an inlined subprogram, an inlined renaming or a
5091 -- subprogram nested in an inlined subprogram. The inlining machinery
5092 -- totally disregards nested subprograms since it considers that they
5093 -- will always be compiled if the parent is (see Inline.Is_Nested).
5094
5095 ------------------------------------
5096 -- Is_Inlined_Or_Child_Of_Inlined --
5097 ------------------------------------
5098
5099 function Is_Inlined_Or_Child_Of_Inlined (E : Entity_Id) return Boolean is
5100 Scop : Entity_Id;
5101
5102 begin
5103 if Is_Inlined (E) or else Is_Inlined (Alias (E)) then
5104 return True;
5105 end if;
5106
5107 Scop := Scope (E);
5108 while Scop /= Standard_Standard loop
5109 if Ekind (Scop) in Subprogram_Kind and then Is_Inlined (Scop) then
5110 return True;
5111 end if;
5112
5113 Scop := Scope (Scop);
5114 end loop;
5115
5116 return False;
5117 end Is_Inlined_Or_Child_Of_Inlined;
5118
5119 begin
5120 -- Must be in the main unit or inlined (or child of inlined)
5121
5122 if (Is_In_Main_Unit (N) or else Is_Inlined_Or_Child_Of_Inlined (Subp))
5123
5124 -- Must be generating code or analyzing code in ASIS/GNATprove mode
5125
5126 and then (Operating_Mode = Generate_Code
5127 or else (Operating_Mode = Check_Semantics
5128 and then (ASIS_Mode or GNATprove_Mode)))
5129
5130 -- The body is needed when generating code (full expansion), in ASIS
5131 -- mode for other tools, and in GNATprove mode (special expansion) for
5132 -- formal verification of the body itself.
5133
5134 and then (Expander_Active or ASIS_Mode or GNATprove_Mode)
5135
5136 -- No point in inlining if ABE is inevitable
5137
5138 and then not Is_Known_Guaranteed_ABE (N)
5139
5140 -- Or if subprogram is eliminated
5141
5142 and then not Is_Eliminated (Subp)
5143 then
5144 Add_Pending_Instantiation (N, Unit_Declaration_Node (Subp));
5145 return True;
5146
5147 -- Here if not inlined, or we ignore the inlining
5148
5149 else
5150 return False;
5151 end if;
5152 end Need_Subprogram_Instance_Body;
5153
5154 --------------------------------------
5155 -- Analyze_Subprogram_Instantiation --
5156 --------------------------------------
5157
5158 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
5159 -- must be replaced by gotos which jump to the end of the routine in order
5160 -- to restore the Ghost and SPARK modes.
5161
5162 procedure Analyze_Subprogram_Instantiation
5163 (N : Node_Id;
5164 K : Entity_Kind)
5165 is
5166 Loc : constant Source_Ptr := Sloc (N);
5167 Gen_Id : constant Node_Id := Name (N);
5168 Errs : constant Nat := Serious_Errors_Detected;
5169
5170 Anon_Id : constant Entity_Id :=
5171 Make_Defining_Identifier (Sloc (Defining_Entity (N)),
5172 Chars => New_External_Name
5173 (Chars (Defining_Entity (N)), 'R'));
5174
5175 Act_Decl_Id : Entity_Id := Empty; -- init to avoid warning
5176 Act_Decl : Node_Id;
5177 Act_Spec : Node_Id;
5178 Act_Tree : Node_Id;
5179
5180 Env_Installed : Boolean := False;
5181 Gen_Unit : Entity_Id;
5182 Gen_Decl : Node_Id;
5183 Pack_Id : Entity_Id;
5184 Parent_Installed : Boolean := False;
5185
5186 Renaming_List : List_Id;
5187 -- The list of declarations that link formals and actuals of the
5188 -- instance. These are subtype declarations for formal types, and
5189 -- renaming declarations for other formals. The subprogram declaration
5190 -- for the instance is then appended to the list, and the last item on
5191 -- the list is the renaming declaration for the instance.
5192
5193 procedure Analyze_Instance_And_Renamings;
5194 -- The instance must be analyzed in a context that includes the mappings
5195 -- of generic parameters into actuals. We create a package declaration
5196 -- for this purpose, and a subprogram with an internal name within the
5197 -- package. The subprogram instance is simply an alias for the internal
5198 -- subprogram, declared in the current scope.
5199
5200 procedure Build_Subprogram_Renaming;
5201 -- If the subprogram is recursive, there are occurrences of the name of
5202 -- the generic within the body, which must resolve to the current
5203 -- instance. We add a renaming declaration after the declaration, which
5204 -- is available in the instance body, as well as in the analysis of
5205 -- aspects that appear in the generic. This renaming declaration is
5206 -- inserted after the instance declaration which it renames.
5207
5208 ------------------------------------
5209 -- Analyze_Instance_And_Renamings --
5210 ------------------------------------
5211
5212 procedure Analyze_Instance_And_Renamings is
5213 Def_Ent : constant Entity_Id := Defining_Entity (N);
5214 Pack_Decl : Node_Id;
5215
5216 begin
5217 if Nkind (Parent (N)) = N_Compilation_Unit then
5218
5219 -- For the case of a compilation unit, the container package has
5220 -- the same name as the instantiation, to insure that the binder
5221 -- calls the elaboration procedure with the right name. Copy the
5222 -- entity of the instance, which may have compilation level flags
5223 -- (e.g. Is_Child_Unit) set.
5224
5225 Pack_Id := New_Copy (Def_Ent);
5226
5227 else
5228 -- Otherwise we use the name of the instantiation concatenated
5229 -- with its source position to ensure uniqueness if there are
5230 -- several instantiations with the same name.
5231
5232 Pack_Id :=
5233 Make_Defining_Identifier (Loc,
5234 Chars => New_External_Name
5235 (Related_Id => Chars (Def_Ent),
5236 Suffix => "GP",
5237 Suffix_Index => Source_Offset (Sloc (Def_Ent))));
5238 end if;
5239
5240 Pack_Decl :=
5241 Make_Package_Declaration (Loc,
5242 Specification => Make_Package_Specification (Loc,
5243 Defining_Unit_Name => Pack_Id,
5244 Visible_Declarations => Renaming_List,
5245 End_Label => Empty));
5246
5247 Set_Instance_Spec (N, Pack_Decl);
5248 Set_Is_Generic_Instance (Pack_Id);
5249 Set_Debug_Info_Needed (Pack_Id);
5250
5251 -- Case of not a compilation unit
5252
5253 if Nkind (Parent (N)) /= N_Compilation_Unit then
5254 Mark_Rewrite_Insertion (Pack_Decl);
5255 Insert_Before (N, Pack_Decl);
5256 Set_Has_Completion (Pack_Id);
5257
5258 -- Case of an instantiation that is a compilation unit
5259
5260 -- Place declaration on current node so context is complete for
5261 -- analysis (including nested instantiations), and for use in a
5262 -- context_clause (see Analyze_With_Clause).
5263
5264 else
5265 Set_Unit (Parent (N), Pack_Decl);
5266 Set_Parent_Spec (Pack_Decl, Parent_Spec (N));
5267 end if;
5268
5269 Analyze (Pack_Decl);
5270 Check_Formal_Packages (Pack_Id);
5271 Set_Is_Generic_Instance (Pack_Id, False);
5272
5273 -- Why do we clear Is_Generic_Instance??? We set it 20 lines
5274 -- above???
5275
5276 -- Body of the enclosing package is supplied when instantiating the
5277 -- subprogram body, after semantic analysis is completed.
5278
5279 if Nkind (Parent (N)) = N_Compilation_Unit then
5280
5281 -- Remove package itself from visibility, so it does not
5282 -- conflict with subprogram.
5283
5284 Set_Name_Entity_Id (Chars (Pack_Id), Homonym (Pack_Id));
5285
5286 -- Set name and scope of internal subprogram so that the proper
5287 -- external name will be generated. The proper scope is the scope
5288 -- of the wrapper package. We need to generate debugging info for
5289 -- the internal subprogram, so set flag accordingly.
5290
5291 Set_Chars (Anon_Id, Chars (Defining_Entity (N)));
5292 Set_Scope (Anon_Id, Scope (Pack_Id));
5293
5294 -- Mark wrapper package as referenced, to avoid spurious warnings
5295 -- if the instantiation appears in various with_ clauses of
5296 -- subunits of the main unit.
5297
5298 Set_Referenced (Pack_Id);
5299 end if;
5300
5301 Set_Is_Generic_Instance (Anon_Id);
5302 Set_Debug_Info_Needed (Anon_Id);
5303 Act_Decl_Id := New_Copy (Anon_Id);
5304
5305 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
5306 Set_Chars (Act_Decl_Id, Chars (Defining_Entity (N)));
5307 Set_Sloc (Act_Decl_Id, Sloc (Defining_Entity (N)));
5308
5309 -- Subprogram instance comes from source only if generic does
5310
5311 Set_Comes_From_Source (Act_Decl_Id, Comes_From_Source (Gen_Unit));
5312
5313 -- If the instance is a child unit, mark the Id accordingly. Mark
5314 -- the anonymous entity as well, which is the real subprogram and
5315 -- which is used when the instance appears in a context clause.
5316 -- Similarly, propagate the Is_Eliminated flag to handle properly
5317 -- nested eliminated subprograms.
5318
5319 Set_Is_Child_Unit (Act_Decl_Id, Is_Child_Unit (Defining_Entity (N)));
5320 Set_Is_Child_Unit (Anon_Id, Is_Child_Unit (Defining_Entity (N)));
5321 New_Overloaded_Entity (Act_Decl_Id);
5322 Check_Eliminated (Act_Decl_Id);
5323 Set_Is_Eliminated (Anon_Id, Is_Eliminated (Act_Decl_Id));
5324
5325 if Nkind (Parent (N)) = N_Compilation_Unit then
5326
5327 -- In compilation unit case, kill elaboration checks on the
5328 -- instantiation, since they are never needed - the body is
5329 -- instantiated at the same point as the spec.
5330
5331 if Legacy_Elaboration_Checks then
5332 Set_Kill_Elaboration_Checks (Act_Decl_Id);
5333 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
5334 end if;
5335
5336 Set_Is_Compilation_Unit (Anon_Id);
5337 Set_Cunit_Entity (Current_Sem_Unit, Pack_Id);
5338 end if;
5339
5340 -- The instance is not a freezing point for the new subprogram.
5341 -- The anonymous subprogram may have a freeze node, created for
5342 -- some delayed aspects. This freeze node must not be inherited
5343 -- by the visible subprogram entity.
5344
5345 Set_Is_Frozen (Act_Decl_Id, False);
5346 Set_Freeze_Node (Act_Decl_Id, Empty);
5347
5348 if Nkind (Defining_Entity (N)) = N_Defining_Operator_Symbol then
5349 Valid_Operator_Definition (Act_Decl_Id);
5350 end if;
5351
5352 Set_Alias (Act_Decl_Id, Anon_Id);
5353 Set_Has_Completion (Act_Decl_Id);
5354 Set_Related_Instance (Pack_Id, Act_Decl_Id);
5355
5356 if Nkind (Parent (N)) = N_Compilation_Unit then
5357 Set_Body_Required (Parent (N), False);
5358 end if;
5359 end Analyze_Instance_And_Renamings;
5360
5361 -------------------------------
5362 -- Build_Subprogram_Renaming --
5363 -------------------------------
5364
5365 procedure Build_Subprogram_Renaming is
5366 Renaming_Decl : Node_Id;
5367 Unit_Renaming : Node_Id;
5368
5369 begin
5370 Unit_Renaming :=
5371 Make_Subprogram_Renaming_Declaration (Loc,
5372 Specification =>
5373 Copy_Generic_Node
5374 (Specification (Original_Node (Gen_Decl)),
5375 Empty,
5376 Instantiating => True),
5377 Name => New_Occurrence_Of (Anon_Id, Loc));
5378
5379 -- The generic may be a a child unit. The renaming needs an
5380 -- identifier with the proper name.
5381
5382 Set_Defining_Unit_Name (Specification (Unit_Renaming),
5383 Make_Defining_Identifier (Loc, Chars (Gen_Unit)));
5384
5385 -- If there is a formal subprogram with the same name as the unit
5386 -- itself, do not add this renaming declaration, to prevent
5387 -- ambiguities when there is a call with that name in the body.
5388 -- This is a partial and ugly fix for one ACATS test. ???
5389
5390 Renaming_Decl := First (Renaming_List);
5391 while Present (Renaming_Decl) loop
5392 if Nkind (Renaming_Decl) = N_Subprogram_Renaming_Declaration
5393 and then
5394 Chars (Defining_Entity (Renaming_Decl)) = Chars (Gen_Unit)
5395 then
5396 exit;
5397 end if;
5398
5399 Next (Renaming_Decl);
5400 end loop;
5401
5402 if No (Renaming_Decl) then
5403 Append (Unit_Renaming, Renaming_List);
5404 end if;
5405 end Build_Subprogram_Renaming;
5406
5407 -- Local variables
5408
5409 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
5410 Saved_ISMP : constant Boolean :=
5411 Ignore_SPARK_Mode_Pragmas_In_Instance;
5412 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
5413 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
5414 -- Save the Ghost and SPARK mode-related data to restore on exit
5415
5416 Vis_Prims_List : Elist_Id := No_Elist;
5417 -- List of primitives made temporarily visible in the instantiation
5418 -- to match the visibility of the formal type
5419
5420 -- Start of processing for Analyze_Subprogram_Instantiation
5421
5422 begin
5423 -- Preserve relevant elaboration-related attributes of the context which
5424 -- are no longer available or very expensive to recompute once analysis,
5425 -- resolution, and expansion are over.
5426
5427 Mark_Elaboration_Attributes
5428 (N_Id => N,
5429 Checks => True,
5430 Level => True,
5431 Modes => True,
5432 Warnings => True);
5433
5434 Check_SPARK_05_Restriction ("generic is not allowed", N);
5435
5436 -- Very first thing: check for special Text_IO unit in case we are
5437 -- instantiating one of the children of [[Wide_]Wide_]Text_IO. Of course
5438 -- such an instantiation is bogus (these are packages, not subprograms),
5439 -- but we get a better error message if we do this.
5440
5441 Check_Text_IO_Special_Unit (Gen_Id);
5442
5443 -- Make node global for error reporting
5444
5445 Instantiation_Node := N;
5446
5447 -- For package instantiations we turn off style checks, because they
5448 -- will have been emitted in the generic. For subprogram instantiations
5449 -- we want to apply at least the check on overriding indicators so we
5450 -- do not modify the style check status.
5451
5452 -- The renaming declarations for the actuals do not come from source and
5453 -- will not generate spurious warnings.
5454
5455 Preanalyze_Actuals (N);
5456
5457 Init_Env;
5458 Env_Installed := True;
5459 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
5460 Gen_Unit := Entity (Gen_Id);
5461
5462 -- A subprogram instantiation is Ghost when it is subject to pragma
5463 -- Ghost or the generic template is Ghost. Set the mode now to ensure
5464 -- that any nodes generated during analysis and expansion are marked as
5465 -- Ghost.
5466
5467 Mark_And_Set_Ghost_Instantiation (N, Gen_Unit);
5468
5469 Generate_Reference (Gen_Unit, Gen_Id);
5470
5471 if Nkind (Gen_Id) = N_Identifier
5472 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
5473 then
5474 Error_Msg_NE
5475 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
5476 end if;
5477
5478 if Etype (Gen_Unit) = Any_Type then
5479 Restore_Env;
5480 goto Leave;
5481 end if;
5482
5483 -- Verify that it is a generic subprogram of the right kind, and that
5484 -- it does not lead to a circular instantiation.
5485
5486 if K = E_Procedure and then Ekind (Gen_Unit) /= E_Generic_Procedure then
5487 Error_Msg_NE
5488 ("& is not the name of a generic procedure", Gen_Id, Gen_Unit);
5489
5490 elsif K = E_Function and then Ekind (Gen_Unit) /= E_Generic_Function then
5491 Error_Msg_NE
5492 ("& is not the name of a generic function", Gen_Id, Gen_Unit);
5493
5494 elsif In_Open_Scopes (Gen_Unit) then
5495 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
5496
5497 else
5498 Set_Entity (Gen_Id, Gen_Unit);
5499 Set_Is_Instantiated (Gen_Unit);
5500
5501 if In_Extended_Main_Source_Unit (N) then
5502 Generate_Reference (Gen_Unit, N);
5503 end if;
5504
5505 -- If renaming, get original unit
5506
5507 if Present (Renamed_Object (Gen_Unit))
5508 and then Ekind_In (Renamed_Object (Gen_Unit), E_Generic_Procedure,
5509 E_Generic_Function)
5510 then
5511 Gen_Unit := Renamed_Object (Gen_Unit);
5512 Set_Is_Instantiated (Gen_Unit);
5513 Generate_Reference (Gen_Unit, N);
5514 end if;
5515
5516 if Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
5517 Error_Msg_Node_2 := Current_Scope;
5518 Error_Msg_NE
5519 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
5520 Circularity_Detected := True;
5521 Restore_Hidden_Primitives (Vis_Prims_List);
5522 goto Leave;
5523 end if;
5524
5525 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
5526
5527 -- Initialize renamings map, for error checking
5528
5529 Generic_Renamings.Set_Last (0);
5530 Generic_Renamings_HTable.Reset;
5531
5532 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
5533
5534 -- Copy original generic tree, to produce text for instantiation
5535
5536 Act_Tree :=
5537 Copy_Generic_Node
5538 (Original_Node (Gen_Decl), Empty, Instantiating => True);
5539
5540 -- Inherit overriding indicator from instance node
5541
5542 Act_Spec := Specification (Act_Tree);
5543 Set_Must_Override (Act_Spec, Must_Override (N));
5544 Set_Must_Not_Override (Act_Spec, Must_Not_Override (N));
5545
5546 Renaming_List :=
5547 Analyze_Associations
5548 (I_Node => N,
5549 Formals => Generic_Formal_Declarations (Act_Tree),
5550 F_Copy => Generic_Formal_Declarations (Gen_Decl));
5551
5552 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
5553
5554 -- The subprogram itself cannot contain a nested instance, so the
5555 -- current parent is left empty.
5556
5557 Set_Instance_Env (Gen_Unit, Empty);
5558
5559 -- Build the subprogram declaration, which does not appear in the
5560 -- generic template, and give it a sloc consistent with that of the
5561 -- template.
5562
5563 Set_Defining_Unit_Name (Act_Spec, Anon_Id);
5564 Set_Generic_Parent (Act_Spec, Gen_Unit);
5565 Act_Decl :=
5566 Make_Subprogram_Declaration (Sloc (Act_Spec),
5567 Specification => Act_Spec);
5568
5569 -- The aspects have been copied previously, but they have to be
5570 -- linked explicitly to the new subprogram declaration. Explicit
5571 -- pre/postconditions on the instance are analyzed below, in a
5572 -- separate step.
5573
5574 Move_Aspects (Act_Tree, To => Act_Decl);
5575 Set_Categorization_From_Pragmas (Act_Decl);
5576
5577 if Parent_Installed then
5578 Hide_Current_Scope;
5579 end if;
5580
5581 Append (Act_Decl, Renaming_List);
5582
5583 -- Contract-related source pragmas that follow a generic subprogram
5584 -- must be instantiated explicitly because they are not part of the
5585 -- subprogram template.
5586
5587 Instantiate_Subprogram_Contract
5588 (Original_Node (Gen_Decl), Renaming_List);
5589
5590 Build_Subprogram_Renaming;
5591
5592 -- If the context of the instance is subject to SPARK_Mode "off" or
5593 -- the annotation is altogether missing, set the global flag which
5594 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
5595 -- the instance. This should be done prior to analyzing the instance.
5596
5597 if SPARK_Mode /= On then
5598 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
5599 end if;
5600
5601 -- If the context of an instance is not subject to SPARK_Mode "off",
5602 -- and the generic spec is subject to an explicit SPARK_Mode pragma,
5603 -- the latter should be the one applicable to the instance.
5604
5605 if not Ignore_SPARK_Mode_Pragmas_In_Instance
5606 and then Saved_SM /= Off
5607 and then Present (SPARK_Pragma (Gen_Unit))
5608 then
5609 Set_SPARK_Mode (Gen_Unit);
5610 end if;
5611
5612 Analyze_Instance_And_Renamings;
5613
5614 -- Restore SPARK_Mode from the context after analysis of the package
5615 -- declaration, so that the SPARK_Mode on the generic spec does not
5616 -- apply to the pending instance for the instance body.
5617
5618 if not Ignore_SPARK_Mode_Pragmas_In_Instance
5619 and then Saved_SM /= Off
5620 and then Present (SPARK_Pragma (Gen_Unit))
5621 then
5622 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
5623 end if;
5624
5625 -- If the generic is marked Import (Intrinsic), then so is the
5626 -- instance. This indicates that there is no body to instantiate. If
5627 -- generic is marked inline, so it the instance, and the anonymous
5628 -- subprogram it renames. If inlined, or else if inlining is enabled
5629 -- for the compilation, we generate the instance body even if it is
5630 -- not within the main unit.
5631
5632 if Is_Intrinsic_Subprogram (Gen_Unit) then
5633 Set_Is_Intrinsic_Subprogram (Anon_Id);
5634 Set_Is_Intrinsic_Subprogram (Act_Decl_Id);
5635
5636 if Chars (Gen_Unit) = Name_Unchecked_Conversion then
5637 Validate_Unchecked_Conversion (N, Act_Decl_Id);
5638 end if;
5639 end if;
5640
5641 -- Inherit convention from generic unit. Intrinsic convention, as for
5642 -- an instance of unchecked conversion, is not inherited because an
5643 -- explicit Ada instance has been created.
5644
5645 if Has_Convention_Pragma (Gen_Unit)
5646 and then Convention (Gen_Unit) /= Convention_Intrinsic
5647 then
5648 Set_Convention (Act_Decl_Id, Convention (Gen_Unit));
5649 Set_Is_Exported (Act_Decl_Id, Is_Exported (Gen_Unit));
5650 end if;
5651
5652 Generate_Definition (Act_Decl_Id);
5653
5654 -- Inherit all inlining-related flags which apply to the generic in
5655 -- the subprogram and its declaration.
5656
5657 Set_Is_Inlined (Act_Decl_Id, Is_Inlined (Gen_Unit));
5658 Set_Is_Inlined (Anon_Id, Is_Inlined (Gen_Unit));
5659
5660 Set_Has_Pragma_Inline (Act_Decl_Id, Has_Pragma_Inline (Gen_Unit));
5661 Set_Has_Pragma_Inline (Anon_Id, Has_Pragma_Inline (Gen_Unit));
5662
5663 -- Propagate No_Return if pragma applied to generic unit. This must
5664 -- be done explicitly because pragma does not appear in generic
5665 -- declaration (unlike the aspect case).
5666
5667 if No_Return (Gen_Unit) then
5668 Set_No_Return (Act_Decl_Id);
5669 Set_No_Return (Anon_Id);
5670 end if;
5671
5672 Set_Has_Pragma_Inline_Always
5673 (Act_Decl_Id, Has_Pragma_Inline_Always (Gen_Unit));
5674 Set_Has_Pragma_Inline_Always
5675 (Anon_Id, Has_Pragma_Inline_Always (Gen_Unit));
5676
5677 -- Mark both the instance spec and the anonymous package in case the
5678 -- body is instantiated at a later pass. This preserves the original
5679 -- context in effect for the body.
5680
5681 if SPARK_Mode /= On then
5682 Set_Ignore_SPARK_Mode_Pragmas (Act_Decl_Id);
5683 Set_Ignore_SPARK_Mode_Pragmas (Anon_Id);
5684 end if;
5685
5686 if Legacy_Elaboration_Checks
5687 and then not Is_Intrinsic_Subprogram (Gen_Unit)
5688 then
5689 Check_Elab_Instantiation (N);
5690 end if;
5691
5692 -- Save the scenario for later examination by the ABE Processing
5693 -- phase.
5694
5695 Record_Elaboration_Scenario (N);
5696
5697 -- The instantiation results in a guaranteed ABE. Create a completing
5698 -- body for the subprogram declaration because the real body will not
5699 -- be instantiated.
5700
5701 if Is_Known_Guaranteed_ABE (N) then
5702 Provide_Completing_Bodies (Instance_Spec (N));
5703 end if;
5704
5705 if Is_Dispatching_Operation (Act_Decl_Id)
5706 and then Ada_Version >= Ada_2005
5707 then
5708 declare
5709 Formal : Entity_Id;
5710
5711 begin
5712 Formal := First_Formal (Act_Decl_Id);
5713 while Present (Formal) loop
5714 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type
5715 and then Is_Controlling_Formal (Formal)
5716 and then not Can_Never_Be_Null (Formal)
5717 then
5718 Error_Msg_NE
5719 ("access parameter& is controlling,", N, Formal);
5720 Error_Msg_NE
5721 ("\corresponding parameter of & must be explicitly "
5722 & "null-excluding", N, Gen_Id);
5723 end if;
5724
5725 Next_Formal (Formal);
5726 end loop;
5727 end;
5728 end if;
5729
5730 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
5731
5732 Validate_Categorization_Dependency (N, Act_Decl_Id);
5733
5734 if not Is_Intrinsic_Subprogram (Act_Decl_Id) then
5735 Inherit_Context (Gen_Decl, N);
5736
5737 Restore_Private_Views (Pack_Id, False);
5738
5739 -- If the context requires a full instantiation, mark node for
5740 -- subsequent construction of the body.
5741
5742 if Need_Subprogram_Instance_Body (N, Act_Decl_Id) then
5743 Check_Forward_Instantiation (Gen_Decl);
5744
5745 -- The wrapper package is always delayed, because it does not
5746 -- constitute a freeze point, but to insure that the freeze node
5747 -- is placed properly, it is created directly when instantiating
5748 -- the body (otherwise the freeze node might appear to early for
5749 -- nested instantiations). For ASIS purposes, indicate that the
5750 -- wrapper package has replaced the instantiation node.
5751
5752 elsif Nkind (Parent (N)) = N_Compilation_Unit then
5753 Rewrite (N, Unit (Parent (N)));
5754 Set_Unit (Parent (N), N);
5755 end if;
5756
5757 -- Replace instance node for library-level instantiations of
5758 -- intrinsic subprograms, for ASIS use.
5759
5760 elsif Nkind (Parent (N)) = N_Compilation_Unit then
5761 Rewrite (N, Unit (Parent (N)));
5762 Set_Unit (Parent (N), N);
5763 end if;
5764
5765 if Parent_Installed then
5766 Remove_Parent;
5767 end if;
5768
5769 Restore_Hidden_Primitives (Vis_Prims_List);
5770 Restore_Env;
5771 Env_Installed := False;
5772 Generic_Renamings.Set_Last (0);
5773 Generic_Renamings_HTable.Reset;
5774 end if;
5775
5776 <<Leave>>
5777 -- Analyze aspects in declaration if no errors appear in the instance.
5778
5779 if Has_Aspects (N) and then Serious_Errors_Detected = Errs then
5780 Analyze_Aspect_Specifications (N, Act_Decl_Id);
5781 end if;
5782
5783 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
5784 Restore_Ghost_Mode (Saved_GM);
5785 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
5786
5787 exception
5788 when Instantiation_Error =>
5789 if Parent_Installed then
5790 Remove_Parent;
5791 end if;
5792
5793 if Env_Installed then
5794 Restore_Env;
5795 end if;
5796
5797 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
5798 Restore_Ghost_Mode (Saved_GM);
5799 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
5800 end Analyze_Subprogram_Instantiation;
5801
5802 -------------------------
5803 -- Get_Associated_Node --
5804 -------------------------
5805
5806 function Get_Associated_Node (N : Node_Id) return Node_Id is
5807 Assoc : Node_Id;
5808
5809 begin
5810 Assoc := Associated_Node (N);
5811
5812 if Nkind (Assoc) /= Nkind (N) then
5813 return Assoc;
5814
5815 elsif Nkind_In (Assoc, N_Aggregate, N_Extension_Aggregate) then
5816 return Assoc;
5817
5818 else
5819 -- If the node is part of an inner generic, it may itself have been
5820 -- remapped into a further generic copy. Associated_Node is otherwise
5821 -- used for the entity of the node, and will be of a different node
5822 -- kind, or else N has been rewritten as a literal or function call.
5823
5824 while Present (Associated_Node (Assoc))
5825 and then Nkind (Associated_Node (Assoc)) = Nkind (Assoc)
5826 loop
5827 Assoc := Associated_Node (Assoc);
5828 end loop;
5829
5830 -- Follow an additional link in case the final node was rewritten.
5831 -- This can only happen with nested generic units.
5832
5833 if (Nkind (Assoc) = N_Identifier or else Nkind (Assoc) in N_Op)
5834 and then Present (Associated_Node (Assoc))
5835 and then (Nkind_In (Associated_Node (Assoc), N_Function_Call,
5836 N_Explicit_Dereference,
5837 N_Integer_Literal,
5838 N_Real_Literal,
5839 N_String_Literal))
5840 then
5841 Assoc := Associated_Node (Assoc);
5842 end if;
5843
5844 -- An additional special case: an unconstrained type in an object
5845 -- declaration may have been rewritten as a local subtype constrained
5846 -- by the expression in the declaration. We need to recover the
5847 -- original entity, which may be global.
5848
5849 if Present (Original_Node (Assoc))
5850 and then Nkind (Parent (N)) = N_Object_Declaration
5851 then
5852 Assoc := Original_Node (Assoc);
5853 end if;
5854
5855 return Assoc;
5856 end if;
5857 end Get_Associated_Node;
5858
5859 ----------------------------
5860 -- Build_Function_Wrapper --
5861 ----------------------------
5862
5863 function Build_Function_Wrapper
5864 (Formal_Subp : Entity_Id;
5865 Actual_Subp : Entity_Id) return Node_Id
5866 is
5867 Loc : constant Source_Ptr := Sloc (Current_Scope);
5868 Ret_Type : constant Entity_Id := Get_Instance_Of (Etype (Formal_Subp));
5869 Actuals : List_Id;
5870 Decl : Node_Id;
5871 Func_Name : Node_Id;
5872 Func : Entity_Id;
5873 Parm_Type : Node_Id;
5874 Profile : List_Id := New_List;
5875 Spec : Node_Id;
5876 Act_F : Entity_Id;
5877 Form_F : Entity_Id;
5878 New_F : Entity_Id;
5879
5880 begin
5881 Func_Name := New_Occurrence_Of (Actual_Subp, Loc);
5882
5883 Func := Make_Defining_Identifier (Loc, Chars (Formal_Subp));
5884 Set_Ekind (Func, E_Function);
5885 Set_Is_Generic_Actual_Subprogram (Func);
5886
5887 Actuals := New_List;
5888 Profile := New_List;
5889
5890 Act_F := First_Formal (Actual_Subp);
5891 Form_F := First_Formal (Formal_Subp);
5892 while Present (Form_F) loop
5893
5894 -- Create new formal for profile of wrapper, and add a reference
5895 -- to it in the list of actuals for the enclosing call. The name
5896 -- must be that of the formal in the formal subprogram, because
5897 -- calls to it in the generic body may use named associations.
5898
5899 New_F := Make_Defining_Identifier (Loc, Chars (Form_F));
5900
5901 Parm_Type :=
5902 New_Occurrence_Of (Get_Instance_Of (Etype (Form_F)), Loc);
5903
5904 Append_To (Profile,
5905 Make_Parameter_Specification (Loc,
5906 Defining_Identifier => New_F,
5907 Parameter_Type => Parm_Type));
5908
5909 Append_To (Actuals, New_Occurrence_Of (New_F, Loc));
5910 Next_Formal (Form_F);
5911
5912 if Present (Act_F) then
5913 Next_Formal (Act_F);
5914 end if;
5915 end loop;
5916
5917 Spec :=
5918 Make_Function_Specification (Loc,
5919 Defining_Unit_Name => Func,
5920 Parameter_Specifications => Profile,
5921 Result_Definition => New_Occurrence_Of (Ret_Type, Loc));
5922
5923 Decl :=
5924 Make_Expression_Function (Loc,
5925 Specification => Spec,
5926 Expression =>
5927 Make_Function_Call (Loc,
5928 Name => Func_Name,
5929 Parameter_Associations => Actuals));
5930
5931 return Decl;
5932 end Build_Function_Wrapper;
5933
5934 ----------------------------
5935 -- Build_Operator_Wrapper --
5936 ----------------------------
5937
5938 function Build_Operator_Wrapper
5939 (Formal_Subp : Entity_Id;
5940 Actual_Subp : Entity_Id) return Node_Id
5941 is
5942 Loc : constant Source_Ptr := Sloc (Current_Scope);
5943 Ret_Type : constant Entity_Id :=
5944 Get_Instance_Of (Etype (Formal_Subp));
5945 Op_Type : constant Entity_Id :=
5946 Get_Instance_Of (Etype (First_Formal (Formal_Subp)));
5947 Is_Binary : constant Boolean :=
5948 Present (Next_Formal (First_Formal (Formal_Subp)));
5949
5950 Decl : Node_Id;
5951 Expr : Node_Id := Empty;
5952 F1, F2 : Entity_Id;
5953 Func : Entity_Id;
5954 Op_Name : Name_Id;
5955 Spec : Node_Id;
5956 L, R : Node_Id;
5957
5958 begin
5959 Op_Name := Chars (Actual_Subp);
5960
5961 -- Create entities for wrapper function and its formals
5962
5963 F1 := Make_Temporary (Loc, 'A');
5964 F2 := Make_Temporary (Loc, 'B');
5965 L := New_Occurrence_Of (F1, Loc);
5966 R := New_Occurrence_Of (F2, Loc);
5967
5968 Func := Make_Defining_Identifier (Loc, Chars (Formal_Subp));
5969 Set_Ekind (Func, E_Function);
5970 Set_Is_Generic_Actual_Subprogram (Func);
5971
5972 Spec :=
5973 Make_Function_Specification (Loc,
5974 Defining_Unit_Name => Func,
5975 Parameter_Specifications => New_List (
5976 Make_Parameter_Specification (Loc,
5977 Defining_Identifier => F1,
5978 Parameter_Type => New_Occurrence_Of (Op_Type, Loc))),
5979 Result_Definition => New_Occurrence_Of (Ret_Type, Loc));
5980
5981 if Is_Binary then
5982 Append_To (Parameter_Specifications (Spec),
5983 Make_Parameter_Specification (Loc,
5984 Defining_Identifier => F2,
5985 Parameter_Type => New_Occurrence_Of (Op_Type, Loc)));
5986 end if;
5987
5988 -- Build expression as a function call, or as an operator node
5989 -- that corresponds to the name of the actual, starting with
5990 -- binary operators.
5991
5992 if Op_Name not in Any_Operator_Name then
5993 Expr :=
5994 Make_Function_Call (Loc,
5995 Name =>
5996 New_Occurrence_Of (Actual_Subp, Loc),
5997 Parameter_Associations => New_List (L));
5998
5999 if Is_Binary then
6000 Append_To (Parameter_Associations (Expr), R);
6001 end if;
6002
6003 -- Binary operators
6004
6005 elsif Is_Binary then
6006 if Op_Name = Name_Op_And then
6007 Expr := Make_Op_And (Loc, Left_Opnd => L, Right_Opnd => R);
6008 elsif Op_Name = Name_Op_Or then
6009 Expr := Make_Op_Or (Loc, Left_Opnd => L, Right_Opnd => R);
6010 elsif Op_Name = Name_Op_Xor then
6011 Expr := Make_Op_Xor (Loc, Left_Opnd => L, Right_Opnd => R);
6012 elsif Op_Name = Name_Op_Eq then
6013 Expr := Make_Op_Eq (Loc, Left_Opnd => L, Right_Opnd => R);
6014 elsif Op_Name = Name_Op_Ne then
6015 Expr := Make_Op_Ne (Loc, Left_Opnd => L, Right_Opnd => R);
6016 elsif Op_Name = Name_Op_Le then
6017 Expr := Make_Op_Le (Loc, Left_Opnd => L, Right_Opnd => R);
6018 elsif Op_Name = Name_Op_Gt then
6019 Expr := Make_Op_Gt (Loc, Left_Opnd => L, Right_Opnd => R);
6020 elsif Op_Name = Name_Op_Ge then
6021 Expr := Make_Op_Ge (Loc, Left_Opnd => L, Right_Opnd => R);
6022 elsif Op_Name = Name_Op_Lt then
6023 Expr := Make_Op_Lt (Loc, Left_Opnd => L, Right_Opnd => R);
6024 elsif Op_Name = Name_Op_Add then
6025 Expr := Make_Op_Add (Loc, Left_Opnd => L, Right_Opnd => R);
6026 elsif Op_Name = Name_Op_Subtract then
6027 Expr := Make_Op_Subtract (Loc, Left_Opnd => L, Right_Opnd => R);
6028 elsif Op_Name = Name_Op_Concat then
6029 Expr := Make_Op_Concat (Loc, Left_Opnd => L, Right_Opnd => R);
6030 elsif Op_Name = Name_Op_Multiply then
6031 Expr := Make_Op_Multiply (Loc, Left_Opnd => L, Right_Opnd => R);
6032 elsif Op_Name = Name_Op_Divide then
6033 Expr := Make_Op_Divide (Loc, Left_Opnd => L, Right_Opnd => R);
6034 elsif Op_Name = Name_Op_Mod then
6035 Expr := Make_Op_Mod (Loc, Left_Opnd => L, Right_Opnd => R);
6036 elsif Op_Name = Name_Op_Rem then
6037 Expr := Make_Op_Rem (Loc, Left_Opnd => L, Right_Opnd => R);
6038 elsif Op_Name = Name_Op_Expon then
6039 Expr := Make_Op_Expon (Loc, Left_Opnd => L, Right_Opnd => R);
6040 end if;
6041
6042 -- Unary operators
6043
6044 else
6045 if Op_Name = Name_Op_Add then
6046 Expr := Make_Op_Plus (Loc, Right_Opnd => L);
6047 elsif Op_Name = Name_Op_Subtract then
6048 Expr := Make_Op_Minus (Loc, Right_Opnd => L);
6049 elsif Op_Name = Name_Op_Abs then
6050 Expr := Make_Op_Abs (Loc, Right_Opnd => L);
6051 elsif Op_Name = Name_Op_Not then
6052 Expr := Make_Op_Not (Loc, Right_Opnd => L);
6053 end if;
6054 end if;
6055
6056 Decl :=
6057 Make_Expression_Function (Loc,
6058 Specification => Spec,
6059 Expression => Expr);
6060
6061 return Decl;
6062 end Build_Operator_Wrapper;
6063
6064 -------------------------------------------
6065 -- Build_Instance_Compilation_Unit_Nodes --
6066 -------------------------------------------
6067
6068 procedure Build_Instance_Compilation_Unit_Nodes
6069 (N : Node_Id;
6070 Act_Body : Node_Id;
6071 Act_Decl : Node_Id)
6072 is
6073 Decl_Cunit : Node_Id;
6074 Body_Cunit : Node_Id;
6075 Citem : Node_Id;
6076 New_Main : constant Entity_Id := Defining_Entity (Act_Decl);
6077 Old_Main : constant Entity_Id := Cunit_Entity (Main_Unit);
6078
6079 begin
6080 -- A new compilation unit node is built for the instance declaration
6081
6082 Decl_Cunit :=
6083 Make_Compilation_Unit (Sloc (N),
6084 Context_Items => Empty_List,
6085 Unit => Act_Decl,
6086 Aux_Decls_Node => Make_Compilation_Unit_Aux (Sloc (N)));
6087
6088 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
6089
6090 -- The new compilation unit is linked to its body, but both share the
6091 -- same file, so we do not set Body_Required on the new unit so as not
6092 -- to create a spurious dependency on a non-existent body in the ali.
6093 -- This simplifies CodePeer unit traversal.
6094
6095 -- We use the original instantiation compilation unit as the resulting
6096 -- compilation unit of the instance, since this is the main unit.
6097
6098 Rewrite (N, Act_Body);
6099
6100 -- Propagate the aspect specifications from the package body template to
6101 -- the instantiated version of the package body.
6102
6103 if Has_Aspects (Act_Body) then
6104 Set_Aspect_Specifications
6105 (N, New_Copy_List_Tree (Aspect_Specifications (Act_Body)));
6106 end if;
6107
6108 Body_Cunit := Parent (N);
6109
6110 -- The two compilation unit nodes are linked by the Library_Unit field
6111
6112 Set_Library_Unit (Decl_Cunit, Body_Cunit);
6113 Set_Library_Unit (Body_Cunit, Decl_Cunit);
6114
6115 -- Preserve the private nature of the package if needed
6116
6117 Set_Private_Present (Decl_Cunit, Private_Present (Body_Cunit));
6118
6119 -- If the instance is not the main unit, its context, categorization
6120 -- and elaboration entity are not relevant to the compilation.
6121
6122 if Body_Cunit /= Cunit (Main_Unit) then
6123 Make_Instance_Unit (Body_Cunit, In_Main => False);
6124 return;
6125 end if;
6126
6127 -- The context clause items on the instantiation, which are now attached
6128 -- to the body compilation unit (since the body overwrote the original
6129 -- instantiation node), semantically belong on the spec, so copy them
6130 -- there. It's harmless to leave them on the body as well. In fact one
6131 -- could argue that they belong in both places.
6132
6133 Citem := First (Context_Items (Body_Cunit));
6134 while Present (Citem) loop
6135 Append (New_Copy (Citem), Context_Items (Decl_Cunit));
6136 Next (Citem);
6137 end loop;
6138
6139 -- Propagate categorization flags on packages, so that they appear in
6140 -- the ali file for the spec of the unit.
6141
6142 if Ekind (New_Main) = E_Package then
6143 Set_Is_Pure (Old_Main, Is_Pure (New_Main));
6144 Set_Is_Preelaborated (Old_Main, Is_Preelaborated (New_Main));
6145 Set_Is_Remote_Types (Old_Main, Is_Remote_Types (New_Main));
6146 Set_Is_Shared_Passive (Old_Main, Is_Shared_Passive (New_Main));
6147 Set_Is_Remote_Call_Interface
6148 (Old_Main, Is_Remote_Call_Interface (New_Main));
6149 end if;
6150
6151 -- Make entry in Units table, so that binder can generate call to
6152 -- elaboration procedure for body, if any.
6153
6154 Make_Instance_Unit (Body_Cunit, In_Main => True);
6155 Main_Unit_Entity := New_Main;
6156 Set_Cunit_Entity (Main_Unit, Main_Unit_Entity);
6157
6158 -- Build elaboration entity, since the instance may certainly generate
6159 -- elaboration code requiring a flag for protection.
6160
6161 Build_Elaboration_Entity (Decl_Cunit, New_Main);
6162 end Build_Instance_Compilation_Unit_Nodes;
6163
6164 -----------------------------
6165 -- Check_Access_Definition --
6166 -----------------------------
6167
6168 procedure Check_Access_Definition (N : Node_Id) is
6169 begin
6170 pragma Assert
6171 (Ada_Version >= Ada_2005 and then Present (Access_Definition (N)));
6172 null;
6173 end Check_Access_Definition;
6174
6175 -----------------------------------
6176 -- Check_Formal_Package_Instance --
6177 -----------------------------------
6178
6179 -- If the formal has specific parameters, they must match those of the
6180 -- actual. Both of them are instances, and the renaming declarations for
6181 -- their formal parameters appear in the same order in both. The analyzed
6182 -- formal has been analyzed in the context of the current instance.
6183
6184 procedure Check_Formal_Package_Instance
6185 (Formal_Pack : Entity_Id;
6186 Actual_Pack : Entity_Id)
6187 is
6188 E1 : Entity_Id := First_Entity (Actual_Pack);
6189 E2 : Entity_Id := First_Entity (Formal_Pack);
6190 Prev_E1 : Entity_Id;
6191
6192 Expr1 : Node_Id;
6193 Expr2 : Node_Id;
6194
6195 procedure Check_Mismatch (B : Boolean);
6196 -- Common error routine for mismatch between the parameters of the
6197 -- actual instance and those of the formal package.
6198
6199 function Same_Instantiated_Constant (E1, E2 : Entity_Id) return Boolean;
6200 -- The formal may come from a nested formal package, and the actual may
6201 -- have been constant-folded. To determine whether the two denote the
6202 -- same entity we may have to traverse several definitions to recover
6203 -- the ultimate entity that they refer to.
6204
6205 function Same_Instantiated_Function (E1, E2 : Entity_Id) return Boolean;
6206 -- The formal and the actual must be identical, but if both are
6207 -- given by attributes they end up renaming different generated bodies,
6208 -- and we must verify that the attributes themselves match.
6209
6210 function Same_Instantiated_Variable (E1, E2 : Entity_Id) return Boolean;
6211 -- Similarly, if the formal comes from a nested formal package, the
6212 -- actual may designate the formal through multiple renamings, which
6213 -- have to be followed to determine the original variable in question.
6214
6215 --------------------
6216 -- Check_Mismatch --
6217 --------------------
6218
6219 procedure Check_Mismatch (B : Boolean) is
6220 -- A Formal_Type_Declaration for a derived private type is rewritten
6221 -- as a private extension decl. (see Analyze_Formal_Derived_Type),
6222 -- which is why we examine the original node.
6223
6224 Kind : constant Node_Kind := Nkind (Original_Node (Parent (E2)));
6225
6226 begin
6227 if Kind = N_Formal_Type_Declaration then
6228 return;
6229
6230 elsif Nkind_In (Kind, N_Formal_Object_Declaration,
6231 N_Formal_Package_Declaration)
6232 or else Kind in N_Formal_Subprogram_Declaration
6233 then
6234 null;
6235
6236 -- Ada 2012: If both formal and actual are incomplete types they
6237 -- are conformant.
6238
6239 elsif Is_Incomplete_Type (E1) and then Is_Incomplete_Type (E2) then
6240 null;
6241
6242 elsif B then
6243 Error_Msg_NE
6244 ("actual for & in actual instance does not match formal",
6245 Parent (Actual_Pack), E1);
6246 end if;
6247 end Check_Mismatch;
6248
6249 --------------------------------
6250 -- Same_Instantiated_Constant --
6251 --------------------------------
6252
6253 function Same_Instantiated_Constant
6254 (E1, E2 : Entity_Id) return Boolean
6255 is
6256 Ent : Entity_Id;
6257
6258 begin
6259 Ent := E2;
6260 while Present (Ent) loop
6261 if E1 = Ent then
6262 return True;
6263
6264 elsif Ekind (Ent) /= E_Constant then
6265 return False;
6266
6267 elsif Is_Entity_Name (Constant_Value (Ent)) then
6268 if Entity (Constant_Value (Ent)) = E1 then
6269 return True;
6270 else
6271 Ent := Entity (Constant_Value (Ent));
6272 end if;
6273
6274 -- The actual may be a constant that has been folded. Recover
6275 -- original name.
6276
6277 elsif Is_Entity_Name (Original_Node (Constant_Value (Ent))) then
6278 Ent := Entity (Original_Node (Constant_Value (Ent)));
6279
6280 else
6281 return False;
6282 end if;
6283 end loop;
6284
6285 return False;
6286 end Same_Instantiated_Constant;
6287
6288 --------------------------------
6289 -- Same_Instantiated_Function --
6290 --------------------------------
6291
6292 function Same_Instantiated_Function
6293 (E1, E2 : Entity_Id) return Boolean
6294 is
6295 U1, U2 : Node_Id;
6296 begin
6297 if Alias (E1) = Alias (E2) then
6298 return True;
6299
6300 elsif Present (Alias (E2)) then
6301 U1 := Original_Node (Unit_Declaration_Node (E1));
6302 U2 := Original_Node (Unit_Declaration_Node (Alias (E2)));
6303
6304 return Nkind (U1) = N_Subprogram_Renaming_Declaration
6305 and then Nkind (Name (U1)) = N_Attribute_Reference
6306
6307 and then Nkind (U2) = N_Subprogram_Renaming_Declaration
6308 and then Nkind (Name (U2)) = N_Attribute_Reference
6309
6310 and then
6311 Attribute_Name (Name (U1)) = Attribute_Name (Name (U2));
6312 else
6313 return False;
6314 end if;
6315 end Same_Instantiated_Function;
6316
6317 --------------------------------
6318 -- Same_Instantiated_Variable --
6319 --------------------------------
6320
6321 function Same_Instantiated_Variable
6322 (E1, E2 : Entity_Id) return Boolean
6323 is
6324 function Original_Entity (E : Entity_Id) return Entity_Id;
6325 -- Follow chain of renamings to the ultimate ancestor
6326
6327 ---------------------
6328 -- Original_Entity --
6329 ---------------------
6330
6331 function Original_Entity (E : Entity_Id) return Entity_Id is
6332 Orig : Entity_Id;
6333
6334 begin
6335 Orig := E;
6336 while Nkind (Parent (Orig)) = N_Object_Renaming_Declaration
6337 and then Present (Renamed_Object (Orig))
6338 and then Is_Entity_Name (Renamed_Object (Orig))
6339 loop
6340 Orig := Entity (Renamed_Object (Orig));
6341 end loop;
6342
6343 return Orig;
6344 end Original_Entity;
6345
6346 -- Start of processing for Same_Instantiated_Variable
6347
6348 begin
6349 return Ekind (E1) = Ekind (E2)
6350 and then Original_Entity (E1) = Original_Entity (E2);
6351 end Same_Instantiated_Variable;
6352
6353 -- Start of processing for Check_Formal_Package_Instance
6354
6355 begin
6356 Prev_E1 := E1;
6357 while Present (E1) and then Present (E2) loop
6358 exit when Ekind (E1) = E_Package
6359 and then Renamed_Entity (E1) = Renamed_Entity (Actual_Pack);
6360
6361 -- If the formal is the renaming of the formal package, this
6362 -- is the end of its formal part, which may occur before the
6363 -- end of the formal part in the actual in the presence of
6364 -- defaulted parameters in the formal package.
6365
6366 exit when Nkind (Parent (E2)) = N_Package_Renaming_Declaration
6367 and then Renamed_Entity (E2) = Scope (E2);
6368
6369 -- The analysis of the actual may generate additional internal
6370 -- entities. If the formal is defaulted, there is no corresponding
6371 -- analysis and the internal entities must be skipped, until we
6372 -- find corresponding entities again.
6373
6374 if Comes_From_Source (E2)
6375 and then not Comes_From_Source (E1)
6376 and then Chars (E1) /= Chars (E2)
6377 then
6378 while Present (E1) and then Chars (E1) /= Chars (E2) loop
6379 Next_Entity (E1);
6380 end loop;
6381 end if;
6382
6383 if No (E1) then
6384 return;
6385
6386 -- Entities may be declared without full declaration, such as
6387 -- itypes and predefined operators (concatenation for arrays, eg).
6388 -- Skip it and keep the formal entity to find a later match for it.
6389
6390 elsif No (Parent (E2)) and then Ekind (E1) /= Ekind (E2) then
6391 E1 := Prev_E1;
6392 goto Next_E;
6393
6394 -- If the formal entity comes from a formal declaration, it was
6395 -- defaulted in the formal package, and no check is needed on it.
6396
6397 elsif Nkind_In (Original_Node (Parent (E2)),
6398 N_Formal_Object_Declaration,
6399 N_Formal_Type_Declaration)
6400 then
6401 -- If the formal is a tagged type the corresponding class-wide
6402 -- type has been generated as well, and it must be skipped.
6403
6404 if Is_Type (E2) and then Is_Tagged_Type (E2) then
6405 Next_Entity (E2);
6406 end if;
6407
6408 goto Next_E;
6409
6410 -- Ditto for defaulted formal subprograms.
6411
6412 elsif Is_Overloadable (E1)
6413 and then Nkind (Unit_Declaration_Node (E2)) in
6414 N_Formal_Subprogram_Declaration
6415 then
6416 goto Next_E;
6417
6418 elsif Is_Type (E1) then
6419
6420 -- Subtypes must statically match. E1, E2 are the local entities
6421 -- that are subtypes of the actuals. Itypes generated for other
6422 -- parameters need not be checked, the check will be performed
6423 -- on the parameters themselves.
6424
6425 -- If E2 is a formal type declaration, it is a defaulted parameter
6426 -- and needs no checking.
6427
6428 if not Is_Itype (E1) and then not Is_Itype (E2) then
6429 Check_Mismatch
6430 (not Is_Type (E2)
6431 or else Etype (E1) /= Etype (E2)
6432 or else not Subtypes_Statically_Match (E1, E2));
6433 end if;
6434
6435 elsif Ekind (E1) = E_Constant then
6436
6437 -- IN parameters must denote the same static value, or the same
6438 -- constant, or the literal null.
6439
6440 Expr1 := Expression (Parent (E1));
6441
6442 if Ekind (E2) /= E_Constant then
6443 Check_Mismatch (True);
6444 goto Next_E;
6445 else
6446 Expr2 := Expression (Parent (E2));
6447 end if;
6448
6449 if Is_OK_Static_Expression (Expr1) then
6450 if not Is_OK_Static_Expression (Expr2) then
6451 Check_Mismatch (True);
6452
6453 elsif Is_Discrete_Type (Etype (E1)) then
6454 declare
6455 V1 : constant Uint := Expr_Value (Expr1);
6456 V2 : constant Uint := Expr_Value (Expr2);
6457 begin
6458 Check_Mismatch (V1 /= V2);
6459 end;
6460
6461 elsif Is_Real_Type (Etype (E1)) then
6462 declare
6463 V1 : constant Ureal := Expr_Value_R (Expr1);
6464 V2 : constant Ureal := Expr_Value_R (Expr2);
6465 begin
6466 Check_Mismatch (V1 /= V2);
6467 end;
6468
6469 elsif Is_String_Type (Etype (E1))
6470 and then Nkind (Expr1) = N_String_Literal
6471 then
6472 if Nkind (Expr2) /= N_String_Literal then
6473 Check_Mismatch (True);
6474 else
6475 Check_Mismatch
6476 (not String_Equal (Strval (Expr1), Strval (Expr2)));
6477 end if;
6478 end if;
6479
6480 elsif Is_Entity_Name (Expr1) then
6481 if Is_Entity_Name (Expr2) then
6482 if Entity (Expr1) = Entity (Expr2) then
6483 null;
6484 else
6485 Check_Mismatch
6486 (not Same_Instantiated_Constant
6487 (Entity (Expr1), Entity (Expr2)));
6488 end if;
6489
6490 else
6491 Check_Mismatch (True);
6492 end if;
6493
6494 elsif Is_Entity_Name (Original_Node (Expr1))
6495 and then Is_Entity_Name (Expr2)
6496 and then Same_Instantiated_Constant
6497 (Entity (Original_Node (Expr1)), Entity (Expr2))
6498 then
6499 null;
6500
6501 elsif Nkind (Expr1) = N_Null then
6502 Check_Mismatch (Nkind (Expr1) /= N_Null);
6503
6504 else
6505 Check_Mismatch (True);
6506 end if;
6507
6508 elsif Ekind (E1) = E_Variable then
6509 Check_Mismatch (not Same_Instantiated_Variable (E1, E2));
6510
6511 elsif Ekind (E1) = E_Package then
6512 Check_Mismatch
6513 (Ekind (E1) /= Ekind (E2)
6514 or else (Present (Renamed_Object (E2))
6515 and then Renamed_Object (E1) /=
6516 Renamed_Object (E2)));
6517
6518 elsif Is_Overloadable (E1) then
6519 -- Verify that the actual subprograms match. Note that actuals
6520 -- that are attributes are rewritten as subprograms. If the
6521 -- subprogram in the formal package is defaulted, no check is
6522 -- needed. Note that this can only happen in Ada 2005 when the
6523 -- formal package can be partially parameterized.
6524
6525 if Nkind (Unit_Declaration_Node (E1)) =
6526 N_Subprogram_Renaming_Declaration
6527 and then From_Default (Unit_Declaration_Node (E1))
6528 then
6529 null;
6530
6531 -- If the formal package has an "others" box association that
6532 -- covers this formal, there is no need for a check either.
6533
6534 elsif Nkind (Unit_Declaration_Node (E2)) in
6535 N_Formal_Subprogram_Declaration
6536 and then Box_Present (Unit_Declaration_Node (E2))
6537 then
6538 null;
6539
6540 -- No check needed if subprogram is a defaulted null procedure
6541
6542 elsif No (Alias (E2))
6543 and then Ekind (E2) = E_Procedure
6544 and then
6545 Null_Present (Specification (Unit_Declaration_Node (E2)))
6546 then
6547 null;
6548
6549 -- Otherwise the actual in the formal and the actual in the
6550 -- instantiation of the formal must match, up to renamings.
6551
6552 else
6553 Check_Mismatch
6554 (Ekind (E2) /= Ekind (E1)
6555 or else not Same_Instantiated_Function (E1, E2));
6556 end if;
6557
6558 else
6559 raise Program_Error;
6560 end if;
6561
6562 <<Next_E>>
6563 Prev_E1 := E1;
6564 Next_Entity (E1);
6565 Next_Entity (E2);
6566 end loop;
6567 end Check_Formal_Package_Instance;
6568
6569 ---------------------------
6570 -- Check_Formal_Packages --
6571 ---------------------------
6572
6573 procedure Check_Formal_Packages (P_Id : Entity_Id) is
6574 E : Entity_Id;
6575 Formal_P : Entity_Id;
6576 Formal_Decl : Node_Id;
6577
6578 begin
6579 -- Iterate through the declarations in the instance, looking for package
6580 -- renaming declarations that denote instances of formal packages. Stop
6581 -- when we find the renaming of the current package itself. The
6582 -- declaration for a formal package without a box is followed by an
6583 -- internal entity that repeats the instantiation.
6584
6585 E := First_Entity (P_Id);
6586 while Present (E) loop
6587 if Ekind (E) = E_Package then
6588 if Renamed_Object (E) = P_Id then
6589 exit;
6590
6591 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
6592 null;
6593
6594 else
6595 Formal_Decl := Parent (Associated_Formal_Package (E));
6596
6597 -- Nothing to check if the formal has a box or an others_clause
6598 -- (necessarily with a box).
6599
6600 if Box_Present (Formal_Decl) then
6601 null;
6602
6603 elsif Nkind (First (Generic_Associations (Formal_Decl))) =
6604 N_Others_Choice
6605 then
6606 -- The internal validating package was generated but formal
6607 -- and instance are known to be compatible.
6608
6609 Formal_P := Next_Entity (E);
6610 Remove (Unit_Declaration_Node (Formal_P));
6611
6612 else
6613 Formal_P := Next_Entity (E);
6614
6615 -- If the instance is within an enclosing instance body
6616 -- there is no need to verify the legality of current formal
6617 -- packages because they were legal in the generic body.
6618 -- This optimization may be applicable elsewhere, and it
6619 -- also removes spurious errors that may arise with
6620 -- on-the-fly inlining and confusion between private and
6621 -- full views.
6622
6623 if not In_Instance_Body then
6624 Check_Formal_Package_Instance (Formal_P, E);
6625 end if;
6626
6627 -- After checking, remove the internal validating package.
6628 -- It is only needed for semantic checks, and as it may
6629 -- contain generic formal declarations it should not reach
6630 -- gigi.
6631
6632 Remove (Unit_Declaration_Node (Formal_P));
6633 end if;
6634 end if;
6635 end if;
6636
6637 Next_Entity (E);
6638 end loop;
6639 end Check_Formal_Packages;
6640
6641 ---------------------------------
6642 -- Check_Forward_Instantiation --
6643 ---------------------------------
6644
6645 procedure Check_Forward_Instantiation (Decl : Node_Id) is
6646 S : Entity_Id;
6647 Gen_Comp : Entity_Id := Cunit_Entity (Get_Source_Unit (Decl));
6648
6649 begin
6650 -- The instantiation appears before the generic body if we are in the
6651 -- scope of the unit containing the generic, either in its spec or in
6652 -- the package body, and before the generic body.
6653
6654 if Ekind (Gen_Comp) = E_Package_Body then
6655 Gen_Comp := Spec_Entity (Gen_Comp);
6656 end if;
6657
6658 if In_Open_Scopes (Gen_Comp)
6659 and then No (Corresponding_Body (Decl))
6660 then
6661 S := Current_Scope;
6662
6663 while Present (S)
6664 and then not Is_Compilation_Unit (S)
6665 and then not Is_Child_Unit (S)
6666 loop
6667 if Ekind (S) = E_Package then
6668 Set_Has_Forward_Instantiation (S);
6669 end if;
6670
6671 S := Scope (S);
6672 end loop;
6673 end if;
6674 end Check_Forward_Instantiation;
6675
6676 ---------------------------
6677 -- Check_Generic_Actuals --
6678 ---------------------------
6679
6680 -- The visibility of the actuals may be different between the point of
6681 -- generic instantiation and the instantiation of the body.
6682
6683 procedure Check_Generic_Actuals
6684 (Instance : Entity_Id;
6685 Is_Formal_Box : Boolean)
6686 is
6687 E : Entity_Id;
6688 Astype : Entity_Id;
6689
6690 function Denotes_Previous_Actual (Typ : Entity_Id) return Boolean;
6691 -- For a formal that is an array type, the component type is often a
6692 -- previous formal in the same unit. The privacy status of the component
6693 -- type will have been examined earlier in the traversal of the
6694 -- corresponding actuals, and this status should not be modified for
6695 -- the array (sub)type itself. However, if the base type of the array
6696 -- (sub)type is private, its full view must be restored in the body to
6697 -- be consistent with subsequent index subtypes, etc.
6698 --
6699 -- To detect this case we have to rescan the list of formals, which is
6700 -- usually short enough to ignore the resulting inefficiency.
6701
6702 -----------------------------
6703 -- Denotes_Previous_Actual --
6704 -----------------------------
6705
6706 function Denotes_Previous_Actual (Typ : Entity_Id) return Boolean is
6707 Prev : Entity_Id;
6708
6709 begin
6710 Prev := First_Entity (Instance);
6711 while Present (Prev) loop
6712 if Is_Type (Prev)
6713 and then Nkind (Parent (Prev)) = N_Subtype_Declaration
6714 and then Is_Entity_Name (Subtype_Indication (Parent (Prev)))
6715 and then Entity (Subtype_Indication (Parent (Prev))) = Typ
6716 then
6717 return True;
6718
6719 elsif Prev = E then
6720 return False;
6721
6722 else
6723 Next_Entity (Prev);
6724 end if;
6725 end loop;
6726
6727 return False;
6728 end Denotes_Previous_Actual;
6729
6730 -- Start of processing for Check_Generic_Actuals
6731
6732 begin
6733 E := First_Entity (Instance);
6734 while Present (E) loop
6735 if Is_Type (E)
6736 and then Nkind (Parent (E)) = N_Subtype_Declaration
6737 and then Scope (Etype (E)) /= Instance
6738 and then Is_Entity_Name (Subtype_Indication (Parent (E)))
6739 then
6740 if Is_Array_Type (E)
6741 and then not Is_Private_Type (Etype (E))
6742 and then Denotes_Previous_Actual (Component_Type (E))
6743 then
6744 null;
6745 else
6746 Check_Private_View (Subtype_Indication (Parent (E)));
6747 end if;
6748
6749 Set_Is_Generic_Actual_Type (E, True);
6750 Set_Is_Hidden (E, False);
6751 Set_Is_Potentially_Use_Visible (E, In_Use (Instance));
6752
6753 -- We constructed the generic actual type as a subtype of the
6754 -- supplied type. This means that it normally would not inherit
6755 -- subtype specific attributes of the actual, which is wrong for
6756 -- the generic case.
6757
6758 Astype := Ancestor_Subtype (E);
6759
6760 if No (Astype) then
6761
6762 -- This can happen when E is an itype that is the full view of
6763 -- a private type completed, e.g. with a constrained array. In
6764 -- that case, use the first subtype, which will carry size
6765 -- information. The base type itself is unconstrained and will
6766 -- not carry it.
6767
6768 Astype := First_Subtype (E);
6769 end if;
6770
6771 Set_Size_Info (E, (Astype));
6772 Set_RM_Size (E, RM_Size (Astype));
6773 Set_First_Rep_Item (E, First_Rep_Item (Astype));
6774
6775 if Is_Discrete_Or_Fixed_Point_Type (E) then
6776 Set_RM_Size (E, RM_Size (Astype));
6777
6778 -- In nested instances, the base type of an access actual may
6779 -- itself be private, and need to be exchanged.
6780
6781 elsif Is_Access_Type (E)
6782 and then Is_Private_Type (Etype (E))
6783 then
6784 Check_Private_View
6785 (New_Occurrence_Of (Etype (E), Sloc (Instance)));
6786 end if;
6787
6788 elsif Ekind (E) = E_Package then
6789
6790 -- If this is the renaming for the current instance, we're done.
6791 -- Otherwise it is a formal package. If the corresponding formal
6792 -- was declared with a box, the (instantiations of the) generic
6793 -- formal part are also visible. Otherwise, ignore the entity
6794 -- created to validate the actuals.
6795
6796 if Renamed_Object (E) = Instance then
6797 exit;
6798
6799 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
6800 null;
6801
6802 -- The visibility of a formal of an enclosing generic is already
6803 -- correct.
6804
6805 elsif Denotes_Formal_Package (E) then
6806 null;
6807
6808 elsif Present (Associated_Formal_Package (E))
6809 and then not Is_Generic_Formal (E)
6810 then
6811 if Box_Present (Parent (Associated_Formal_Package (E))) then
6812 Check_Generic_Actuals (Renamed_Object (E), True);
6813
6814 else
6815 Check_Generic_Actuals (Renamed_Object (E), False);
6816 end if;
6817
6818 Set_Is_Hidden (E, False);
6819 end if;
6820
6821 -- If this is a subprogram instance (in a wrapper package) the
6822 -- actual is fully visible.
6823
6824 elsif Is_Wrapper_Package (Instance) then
6825 Set_Is_Hidden (E, False);
6826
6827 -- If the formal package is declared with a box, or if the formal
6828 -- parameter is defaulted, it is visible in the body.
6829
6830 elsif Is_Formal_Box or else Is_Visible_Formal (E) then
6831 Set_Is_Hidden (E, False);
6832 end if;
6833
6834 if Ekind (E) = E_Constant then
6835
6836 -- If the type of the actual is a private type declared in the
6837 -- enclosing scope of the generic unit, the body of the generic
6838 -- sees the full view of the type (because it has to appear in
6839 -- the corresponding package body). If the type is private now,
6840 -- exchange views to restore the proper visiblity in the instance.
6841
6842 declare
6843 Typ : constant Entity_Id := Base_Type (Etype (E));
6844 -- The type of the actual
6845
6846 Gen_Id : Entity_Id;
6847 -- The generic unit
6848
6849 Parent_Scope : Entity_Id;
6850 -- The enclosing scope of the generic unit
6851
6852 begin
6853 if Is_Wrapper_Package (Instance) then
6854 Gen_Id :=
6855 Generic_Parent
6856 (Specification
6857 (Unit_Declaration_Node
6858 (Related_Instance (Instance))));
6859 else
6860 Gen_Id :=
6861 Generic_Parent (Package_Specification (Instance));
6862 end if;
6863
6864 Parent_Scope := Scope (Gen_Id);
6865
6866 -- The exchange is only needed if the generic is defined
6867 -- within a package which is not a common ancestor of the
6868 -- scope of the instance, and is not already in scope.
6869
6870 if Is_Private_Type (Typ)
6871 and then Scope (Typ) = Parent_Scope
6872 and then Scope (Instance) /= Parent_Scope
6873 and then Ekind (Parent_Scope) = E_Package
6874 and then not Is_Child_Unit (Gen_Id)
6875 then
6876 Switch_View (Typ);
6877
6878 -- If the type of the entity is a subtype, it may also have
6879 -- to be made visible, together with the base type of its
6880 -- full view, after exchange.
6881
6882 if Is_Private_Type (Etype (E)) then
6883 Switch_View (Etype (E));
6884 Switch_View (Base_Type (Etype (E)));
6885 end if;
6886 end if;
6887 end;
6888 end if;
6889
6890 Next_Entity (E);
6891 end loop;
6892 end Check_Generic_Actuals;
6893
6894 ------------------------------
6895 -- Check_Generic_Child_Unit --
6896 ------------------------------
6897
6898 procedure Check_Generic_Child_Unit
6899 (Gen_Id : Node_Id;
6900 Parent_Installed : in out Boolean)
6901 is
6902 Loc : constant Source_Ptr := Sloc (Gen_Id);
6903 Gen_Par : Entity_Id := Empty;
6904 E : Entity_Id;
6905 Inst_Par : Entity_Id;
6906 S : Node_Id;
6907
6908 function Find_Generic_Child
6909 (Scop : Entity_Id;
6910 Id : Node_Id) return Entity_Id;
6911 -- Search generic parent for possible child unit with the given name
6912
6913 function In_Enclosing_Instance return Boolean;
6914 -- Within an instance of the parent, the child unit may be denoted by
6915 -- a simple name, or an abbreviated expanded name. Examine enclosing
6916 -- scopes to locate a possible parent instantiation.
6917
6918 ------------------------
6919 -- Find_Generic_Child --
6920 ------------------------
6921
6922 function Find_Generic_Child
6923 (Scop : Entity_Id;
6924 Id : Node_Id) return Entity_Id
6925 is
6926 E : Entity_Id;
6927
6928 begin
6929 -- If entity of name is already set, instance has already been
6930 -- resolved, e.g. in an enclosing instantiation.
6931
6932 if Present (Entity (Id)) then
6933 if Scope (Entity (Id)) = Scop then
6934 return Entity (Id);
6935 else
6936 return Empty;
6937 end if;
6938
6939 else
6940 E := First_Entity (Scop);
6941 while Present (E) loop
6942 if Chars (E) = Chars (Id)
6943 and then Is_Child_Unit (E)
6944 then
6945 if Is_Child_Unit (E)
6946 and then not Is_Visible_Lib_Unit (E)
6947 then
6948 Error_Msg_NE
6949 ("generic child unit& is not visible", Gen_Id, E);
6950 end if;
6951
6952 Set_Entity (Id, E);
6953 return E;
6954 end if;
6955
6956 Next_Entity (E);
6957 end loop;
6958
6959 return Empty;
6960 end if;
6961 end Find_Generic_Child;
6962
6963 ---------------------------
6964 -- In_Enclosing_Instance --
6965 ---------------------------
6966
6967 function In_Enclosing_Instance return Boolean is
6968 Enclosing_Instance : Node_Id;
6969 Instance_Decl : Node_Id;
6970
6971 begin
6972 -- We do not inline any call that contains instantiations, except
6973 -- for instantiations of Unchecked_Conversion, so if we are within
6974 -- an inlined body the current instance does not require parents.
6975
6976 if In_Inlined_Body then
6977 pragma Assert (Chars (Gen_Id) = Name_Unchecked_Conversion);
6978 return False;
6979 end if;
6980
6981 -- Loop to check enclosing scopes
6982
6983 Enclosing_Instance := Current_Scope;
6984 while Present (Enclosing_Instance) loop
6985 Instance_Decl := Unit_Declaration_Node (Enclosing_Instance);
6986
6987 if Ekind (Enclosing_Instance) = E_Package
6988 and then Is_Generic_Instance (Enclosing_Instance)
6989 and then Present
6990 (Generic_Parent (Specification (Instance_Decl)))
6991 then
6992 -- Check whether the generic we are looking for is a child of
6993 -- this instance.
6994
6995 E := Find_Generic_Child
6996 (Generic_Parent (Specification (Instance_Decl)), Gen_Id);
6997 exit when Present (E);
6998
6999 else
7000 E := Empty;
7001 end if;
7002
7003 Enclosing_Instance := Scope (Enclosing_Instance);
7004 end loop;
7005
7006 if No (E) then
7007
7008 -- Not a child unit
7009
7010 Analyze (Gen_Id);
7011 return False;
7012
7013 else
7014 Rewrite (Gen_Id,
7015 Make_Expanded_Name (Loc,
7016 Chars => Chars (E),
7017 Prefix => New_Occurrence_Of (Enclosing_Instance, Loc),
7018 Selector_Name => New_Occurrence_Of (E, Loc)));
7019
7020 Set_Entity (Gen_Id, E);
7021 Set_Etype (Gen_Id, Etype (E));
7022 Parent_Installed := False; -- Already in scope.
7023 return True;
7024 end if;
7025 end In_Enclosing_Instance;
7026
7027 -- Start of processing for Check_Generic_Child_Unit
7028
7029 begin
7030 -- If the name of the generic is given by a selected component, it may
7031 -- be the name of a generic child unit, and the prefix is the name of an
7032 -- instance of the parent, in which case the child unit must be visible.
7033 -- If this instance is not in scope, it must be placed there and removed
7034 -- after instantiation, because what is being instantiated is not the
7035 -- original child, but the corresponding child present in the instance
7036 -- of the parent.
7037
7038 -- If the child is instantiated within the parent, it can be given by
7039 -- a simple name. In this case the instance is already in scope, but
7040 -- the child generic must be recovered from the generic parent as well.
7041
7042 if Nkind (Gen_Id) = N_Selected_Component then
7043 S := Selector_Name (Gen_Id);
7044 Analyze (Prefix (Gen_Id));
7045 Inst_Par := Entity (Prefix (Gen_Id));
7046
7047 if Ekind (Inst_Par) = E_Package
7048 and then Present (Renamed_Object (Inst_Par))
7049 then
7050 Inst_Par := Renamed_Object (Inst_Par);
7051 end if;
7052
7053 if Ekind (Inst_Par) = E_Package then
7054 if Nkind (Parent (Inst_Par)) = N_Package_Specification then
7055 Gen_Par := Generic_Parent (Parent (Inst_Par));
7056
7057 elsif Nkind (Parent (Inst_Par)) = N_Defining_Program_Unit_Name
7058 and then
7059 Nkind (Parent (Parent (Inst_Par))) = N_Package_Specification
7060 then
7061 Gen_Par := Generic_Parent (Parent (Parent (Inst_Par)));
7062 end if;
7063
7064 elsif Ekind (Inst_Par) = E_Generic_Package
7065 and then Nkind (Parent (Gen_Id)) = N_Formal_Package_Declaration
7066 then
7067 -- A formal package may be a real child package, and not the
7068 -- implicit instance within a parent. In this case the child is
7069 -- not visible and has to be retrieved explicitly as well.
7070
7071 Gen_Par := Inst_Par;
7072 end if;
7073
7074 if Present (Gen_Par) then
7075
7076 -- The prefix denotes an instantiation. The entity itself may be a
7077 -- nested generic, or a child unit.
7078
7079 E := Find_Generic_Child (Gen_Par, S);
7080
7081 if Present (E) then
7082 Change_Selected_Component_To_Expanded_Name (Gen_Id);
7083 Set_Entity (Gen_Id, E);
7084 Set_Etype (Gen_Id, Etype (E));
7085 Set_Entity (S, E);
7086 Set_Etype (S, Etype (E));
7087
7088 -- Indicate that this is a reference to the parent
7089
7090 if In_Extended_Main_Source_Unit (Gen_Id) then
7091 Set_Is_Instantiated (Inst_Par);
7092 end if;
7093
7094 -- A common mistake is to replicate the naming scheme of a
7095 -- hierarchy by instantiating a generic child directly, rather
7096 -- than the implicit child in a parent instance:
7097
7098 -- generic .. package Gpar is ..
7099 -- generic .. package Gpar.Child is ..
7100 -- package Par is new Gpar ();
7101
7102 -- with Gpar.Child;
7103 -- package Par.Child is new Gpar.Child ();
7104 -- rather than Par.Child
7105
7106 -- In this case the instantiation is within Par, which is an
7107 -- instance, but Gpar does not denote Par because we are not IN
7108 -- the instance of Gpar, so this is illegal. The test below
7109 -- recognizes this particular case.
7110
7111 if Is_Child_Unit (E)
7112 and then not Comes_From_Source (Entity (Prefix (Gen_Id)))
7113 and then (not In_Instance
7114 or else Nkind (Parent (Parent (Gen_Id))) =
7115 N_Compilation_Unit)
7116 then
7117 Error_Msg_N
7118 ("prefix of generic child unit must be instance of parent",
7119 Gen_Id);
7120 end if;
7121
7122 if not In_Open_Scopes (Inst_Par)
7123 and then Nkind (Parent (Gen_Id)) not in
7124 N_Generic_Renaming_Declaration
7125 then
7126 Install_Parent (Inst_Par);
7127 Parent_Installed := True;
7128
7129 elsif In_Open_Scopes (Inst_Par) then
7130
7131 -- If the parent is already installed, install the actuals
7132 -- for its formal packages. This is necessary when the child
7133 -- instance is a child of the parent instance: in this case,
7134 -- the parent is placed on the scope stack but the formal
7135 -- packages are not made visible.
7136
7137 Install_Formal_Packages (Inst_Par);
7138 end if;
7139
7140 else
7141 -- If the generic parent does not contain an entity that
7142 -- corresponds to the selector, the instance doesn't either.
7143 -- Analyzing the node will yield the appropriate error message.
7144 -- If the entity is not a child unit, then it is an inner
7145 -- generic in the parent.
7146
7147 Analyze (Gen_Id);
7148 end if;
7149
7150 else
7151 Analyze (Gen_Id);
7152
7153 if Is_Child_Unit (Entity (Gen_Id))
7154 and then
7155 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
7156 and then not In_Open_Scopes (Inst_Par)
7157 then
7158 Install_Parent (Inst_Par);
7159 Parent_Installed := True;
7160
7161 -- The generic unit may be the renaming of the implicit child
7162 -- present in an instance. In that case the parent instance is
7163 -- obtained from the name of the renamed entity.
7164
7165 elsif Ekind (Entity (Gen_Id)) = E_Generic_Package
7166 and then Present (Renamed_Entity (Entity (Gen_Id)))
7167 and then Is_Child_Unit (Renamed_Entity (Entity (Gen_Id)))
7168 then
7169 declare
7170 Renamed_Package : constant Node_Id :=
7171 Name (Parent (Entity (Gen_Id)));
7172 begin
7173 if Nkind (Renamed_Package) = N_Expanded_Name then
7174 Inst_Par := Entity (Prefix (Renamed_Package));
7175 Install_Parent (Inst_Par);
7176 Parent_Installed := True;
7177 end if;
7178 end;
7179 end if;
7180 end if;
7181
7182 elsif Nkind (Gen_Id) = N_Expanded_Name then
7183
7184 -- Entity already present, analyze prefix, whose meaning may be an
7185 -- instance in the current context. If it is an instance of a
7186 -- relative within another, the proper parent may still have to be
7187 -- installed, if they are not of the same generation.
7188
7189 Analyze (Prefix (Gen_Id));
7190
7191 -- Prevent cascaded errors
7192
7193 if Etype (Prefix (Gen_Id)) = Any_Type then
7194 return;
7195 end if;
7196
7197 -- In the unlikely case that a local declaration hides the name of
7198 -- the parent package, locate it on the homonym chain. If the context
7199 -- is an instance of the parent, the renaming entity is flagged as
7200 -- such.
7201
7202 Inst_Par := Entity (Prefix (Gen_Id));
7203 while Present (Inst_Par)
7204 and then not Is_Package_Or_Generic_Package (Inst_Par)
7205 loop
7206 Inst_Par := Homonym (Inst_Par);
7207 end loop;
7208
7209 pragma Assert (Present (Inst_Par));
7210 Set_Entity (Prefix (Gen_Id), Inst_Par);
7211
7212 if In_Enclosing_Instance then
7213 null;
7214
7215 elsif Present (Entity (Gen_Id))
7216 and then Is_Child_Unit (Entity (Gen_Id))
7217 and then not In_Open_Scopes (Inst_Par)
7218 then
7219 Install_Parent (Inst_Par);
7220 Parent_Installed := True;
7221 end if;
7222
7223 elsif In_Enclosing_Instance then
7224
7225 -- The child unit is found in some enclosing scope
7226
7227 null;
7228
7229 else
7230 Analyze (Gen_Id);
7231
7232 -- If this is the renaming of the implicit child in a parent
7233 -- instance, recover the parent name and install it.
7234
7235 if Is_Entity_Name (Gen_Id) then
7236 E := Entity (Gen_Id);
7237
7238 if Is_Generic_Unit (E)
7239 and then Nkind (Parent (E)) in N_Generic_Renaming_Declaration
7240 and then Is_Child_Unit (Renamed_Object (E))
7241 and then Is_Generic_Unit (Scope (Renamed_Object (E)))
7242 and then Nkind (Name (Parent (E))) = N_Expanded_Name
7243 then
7244 Rewrite (Gen_Id, New_Copy_Tree (Name (Parent (E))));
7245 Inst_Par := Entity (Prefix (Gen_Id));
7246
7247 if not In_Open_Scopes (Inst_Par) then
7248 Install_Parent (Inst_Par);
7249 Parent_Installed := True;
7250 end if;
7251
7252 -- If it is a child unit of a non-generic parent, it may be
7253 -- use-visible and given by a direct name. Install parent as
7254 -- for other cases.
7255
7256 elsif Is_Generic_Unit (E)
7257 and then Is_Child_Unit (E)
7258 and then
7259 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
7260 and then not Is_Generic_Unit (Scope (E))
7261 then
7262 if not In_Open_Scopes (Scope (E)) then
7263 Install_Parent (Scope (E));
7264 Parent_Installed := True;
7265 end if;
7266 end if;
7267 end if;
7268 end if;
7269 end Check_Generic_Child_Unit;
7270
7271 -----------------------------
7272 -- Check_Hidden_Child_Unit --
7273 -----------------------------
7274
7275 procedure Check_Hidden_Child_Unit
7276 (N : Node_Id;
7277 Gen_Unit : Entity_Id;
7278 Act_Decl_Id : Entity_Id)
7279 is
7280 Gen_Id : constant Node_Id := Name (N);
7281
7282 begin
7283 if Is_Child_Unit (Gen_Unit)
7284 and then Is_Child_Unit (Act_Decl_Id)
7285 and then Nkind (Gen_Id) = N_Expanded_Name
7286 and then Entity (Prefix (Gen_Id)) = Scope (Act_Decl_Id)
7287 and then Chars (Gen_Unit) = Chars (Act_Decl_Id)
7288 then
7289 Error_Msg_Node_2 := Scope (Act_Decl_Id);
7290 Error_Msg_NE
7291 ("generic unit & is implicitly declared in &",
7292 Defining_Unit_Name (N), Gen_Unit);
7293 Error_Msg_N ("\instance must have different name",
7294 Defining_Unit_Name (N));
7295 end if;
7296 end Check_Hidden_Child_Unit;
7297
7298 ------------------------
7299 -- Check_Private_View --
7300 ------------------------
7301
7302 procedure Check_Private_View (N : Node_Id) is
7303 T : constant Entity_Id := Etype (N);
7304 BT : Entity_Id;
7305
7306 begin
7307 -- Exchange views if the type was not private in the generic but is
7308 -- private at the point of instantiation. Do not exchange views if
7309 -- the scope of the type is in scope. This can happen if both generic
7310 -- and instance are sibling units, or if type is defined in a parent.
7311 -- In this case the visibility of the type will be correct for all
7312 -- semantic checks.
7313
7314 if Present (T) then
7315 BT := Base_Type (T);
7316
7317 if Is_Private_Type (T)
7318 and then not Has_Private_View (N)
7319 and then Present (Full_View (T))
7320 and then not In_Open_Scopes (Scope (T))
7321 then
7322 -- In the generic, the full type was visible. Save the private
7323 -- entity, for subsequent exchange.
7324
7325 Switch_View (T);
7326
7327 elsif Has_Private_View (N)
7328 and then not Is_Private_Type (T)
7329 and then not Has_Been_Exchanged (T)
7330 and then Etype (Get_Associated_Node (N)) /= T
7331 then
7332 -- Only the private declaration was visible in the generic. If
7333 -- the type appears in a subtype declaration, the subtype in the
7334 -- instance must have a view compatible with that of its parent,
7335 -- which must be exchanged (see corresponding code in Restore_
7336 -- Private_Views). Otherwise, if the type is defined in a parent
7337 -- unit, leave full visibility within instance, which is safe.
7338
7339 if In_Open_Scopes (Scope (Base_Type (T)))
7340 and then not Is_Private_Type (Base_Type (T))
7341 and then Comes_From_Source (Base_Type (T))
7342 then
7343 null;
7344
7345 elsif Nkind (Parent (N)) = N_Subtype_Declaration
7346 or else not In_Private_Part (Scope (Base_Type (T)))
7347 then
7348 Prepend_Elmt (T, Exchanged_Views);
7349 Exchange_Declarations (Etype (Get_Associated_Node (N)));
7350 end if;
7351
7352 -- For composite types with inconsistent representation exchange
7353 -- component types accordingly.
7354
7355 elsif Is_Access_Type (T)
7356 and then Is_Private_Type (Designated_Type (T))
7357 and then not Has_Private_View (N)
7358 and then Present (Full_View (Designated_Type (T)))
7359 then
7360 Switch_View (Designated_Type (T));
7361
7362 elsif Is_Array_Type (T) then
7363 if Is_Private_Type (Component_Type (T))
7364 and then not Has_Private_View (N)
7365 and then Present (Full_View (Component_Type (T)))
7366 then
7367 Switch_View (Component_Type (T));
7368 end if;
7369
7370 -- The normal exchange mechanism relies on the setting of a
7371 -- flag on the reference in the generic. However, an additional
7372 -- mechanism is needed for types that are not explicitly
7373 -- mentioned in the generic, but may be needed in expanded code
7374 -- in the instance. This includes component types of arrays and
7375 -- designated types of access types. This processing must also
7376 -- include the index types of arrays which we take care of here.
7377
7378 declare
7379 Indx : Node_Id;
7380 Typ : Entity_Id;
7381
7382 begin
7383 Indx := First_Index (T);
7384 while Present (Indx) loop
7385 Typ := Base_Type (Etype (Indx));
7386
7387 if Is_Private_Type (Typ)
7388 and then Present (Full_View (Typ))
7389 then
7390 Switch_View (Typ);
7391 end if;
7392
7393 Next_Index (Indx);
7394 end loop;
7395 end;
7396
7397 elsif Is_Private_Type (T)
7398 and then Present (Full_View (T))
7399 and then Is_Array_Type (Full_View (T))
7400 and then Is_Private_Type (Component_Type (Full_View (T)))
7401 then
7402 Switch_View (T);
7403
7404 -- Finally, a non-private subtype may have a private base type, which
7405 -- must be exchanged for consistency. This can happen when a package
7406 -- body is instantiated, when the scope stack is empty but in fact
7407 -- the subtype and the base type are declared in an enclosing scope.
7408
7409 -- Note that in this case we introduce an inconsistency in the view
7410 -- set, because we switch the base type BT, but there could be some
7411 -- private dependent subtypes of BT which remain unswitched. Such
7412 -- subtypes might need to be switched at a later point (see specific
7413 -- provision for that case in Switch_View).
7414
7415 elsif not Is_Private_Type (T)
7416 and then not Has_Private_View (N)
7417 and then Is_Private_Type (BT)
7418 and then Present (Full_View (BT))
7419 and then not Is_Generic_Type (BT)
7420 and then not In_Open_Scopes (BT)
7421 then
7422 Prepend_Elmt (Full_View (BT), Exchanged_Views);
7423 Exchange_Declarations (BT);
7424 end if;
7425 end if;
7426 end Check_Private_View;
7427
7428 -----------------------------
7429 -- Check_Hidden_Primitives --
7430 -----------------------------
7431
7432 function Check_Hidden_Primitives (Assoc_List : List_Id) return Elist_Id is
7433 Actual : Node_Id;
7434 Gen_T : Entity_Id;
7435 Result : Elist_Id := No_Elist;
7436
7437 begin
7438 if No (Assoc_List) then
7439 return No_Elist;
7440 end if;
7441
7442 -- Traverse the list of associations between formals and actuals
7443 -- searching for renamings of tagged types
7444
7445 Actual := First (Assoc_List);
7446 while Present (Actual) loop
7447 if Nkind (Actual) = N_Subtype_Declaration then
7448 Gen_T := Generic_Parent_Type (Actual);
7449
7450 if Present (Gen_T) and then Is_Tagged_Type (Gen_T) then
7451
7452 -- Traverse the list of primitives of the actual types
7453 -- searching for hidden primitives that are visible in the
7454 -- corresponding generic formal; leave them visible and
7455 -- append them to Result to restore their decoration later.
7456
7457 Install_Hidden_Primitives
7458 (Prims_List => Result,
7459 Gen_T => Gen_T,
7460 Act_T => Entity (Subtype_Indication (Actual)));
7461 end if;
7462 end if;
7463
7464 Next (Actual);
7465 end loop;
7466
7467 return Result;
7468 end Check_Hidden_Primitives;
7469
7470 --------------------------
7471 -- Contains_Instance_Of --
7472 --------------------------
7473
7474 function Contains_Instance_Of
7475 (Inner : Entity_Id;
7476 Outer : Entity_Id;
7477 N : Node_Id) return Boolean
7478 is
7479 Elmt : Elmt_Id;
7480 Scop : Entity_Id;
7481
7482 begin
7483 Scop := Outer;
7484
7485 -- Verify that there are no circular instantiations. We check whether
7486 -- the unit contains an instance of the current scope or some enclosing
7487 -- scope (in case one of the instances appears in a subunit). Longer
7488 -- circularities involving subunits might seem too pathological to
7489 -- consider, but they were not too pathological for the authors of
7490 -- DEC bc30vsq, so we loop over all enclosing scopes, and mark all
7491 -- enclosing generic scopes as containing an instance.
7492
7493 loop
7494 -- Within a generic subprogram body, the scope is not generic, to
7495 -- allow for recursive subprograms. Use the declaration to determine
7496 -- whether this is a generic unit.
7497
7498 if Ekind (Scop) = E_Generic_Package
7499 or else (Is_Subprogram (Scop)
7500 and then Nkind (Unit_Declaration_Node (Scop)) =
7501 N_Generic_Subprogram_Declaration)
7502 then
7503 Elmt := First_Elmt (Inner_Instances (Inner));
7504
7505 while Present (Elmt) loop
7506 if Node (Elmt) = Scop then
7507 Error_Msg_Node_2 := Inner;
7508 Error_Msg_NE
7509 ("circular Instantiation: & instantiated within &!",
7510 N, Scop);
7511 return True;
7512
7513 elsif Node (Elmt) = Inner then
7514 return True;
7515
7516 elsif Contains_Instance_Of (Node (Elmt), Scop, N) then
7517 Error_Msg_Node_2 := Inner;
7518 Error_Msg_NE
7519 ("circular Instantiation: & instantiated within &!",
7520 N, Node (Elmt));
7521 return True;
7522 end if;
7523
7524 Next_Elmt (Elmt);
7525 end loop;
7526
7527 -- Indicate that Inner is being instantiated within Scop
7528
7529 Append_Elmt (Inner, Inner_Instances (Scop));
7530 end if;
7531
7532 if Scop = Standard_Standard then
7533 exit;
7534 else
7535 Scop := Scope (Scop);
7536 end if;
7537 end loop;
7538
7539 return False;
7540 end Contains_Instance_Of;
7541
7542 -----------------------
7543 -- Copy_Generic_Node --
7544 -----------------------
7545
7546 function Copy_Generic_Node
7547 (N : Node_Id;
7548 Parent_Id : Node_Id;
7549 Instantiating : Boolean) return Node_Id
7550 is
7551 Ent : Entity_Id;
7552 New_N : Node_Id;
7553
7554 function Copy_Generic_Descendant (D : Union_Id) return Union_Id;
7555 -- Check the given value of one of the Fields referenced by the current
7556 -- node to determine whether to copy it recursively. The field may hold
7557 -- a Node_Id, a List_Id, or an Elist_Id, or a plain value (Sloc, Uint,
7558 -- Char) in which case it need not be copied.
7559
7560 procedure Copy_Descendants;
7561 -- Common utility for various nodes
7562
7563 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id;
7564 -- Make copy of element list
7565
7566 function Copy_Generic_List
7567 (L : List_Id;
7568 Parent_Id : Node_Id) return List_Id;
7569 -- Apply Copy_Node recursively to the members of a node list
7570
7571 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean;
7572 -- True if an identifier is part of the defining program unit name of
7573 -- a child unit. The entity of such an identifier must be kept (for
7574 -- ASIS use) even though as the name of an enclosing generic it would
7575 -- otherwise not be preserved in the generic tree.
7576
7577 ----------------------
7578 -- Copy_Descendants --
7579 ----------------------
7580
7581 procedure Copy_Descendants is
7582 use Atree.Unchecked_Access;
7583 -- This code section is part of the implementation of an untyped
7584 -- tree traversal, so it needs direct access to node fields.
7585
7586 begin
7587 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
7588 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
7589 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
7590 Set_Field4 (New_N, Copy_Generic_Descendant (Field4 (N)));
7591 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
7592 end Copy_Descendants;
7593
7594 -----------------------------
7595 -- Copy_Generic_Descendant --
7596 -----------------------------
7597
7598 function Copy_Generic_Descendant (D : Union_Id) return Union_Id is
7599 begin
7600 if D = Union_Id (Empty) then
7601 return D;
7602
7603 elsif D in Node_Range then
7604 return Union_Id
7605 (Copy_Generic_Node (Node_Id (D), New_N, Instantiating));
7606
7607 elsif D in List_Range then
7608 return Union_Id (Copy_Generic_List (List_Id (D), New_N));
7609
7610 elsif D in Elist_Range then
7611 return Union_Id (Copy_Generic_Elist (Elist_Id (D)));
7612
7613 -- Nothing else is copyable (e.g. Uint values), return as is
7614
7615 else
7616 return D;
7617 end if;
7618 end Copy_Generic_Descendant;
7619
7620 ------------------------
7621 -- Copy_Generic_Elist --
7622 ------------------------
7623
7624 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id is
7625 M : Elmt_Id;
7626 L : Elist_Id;
7627
7628 begin
7629 if Present (E) then
7630 L := New_Elmt_List;
7631 M := First_Elmt (E);
7632 while Present (M) loop
7633 Append_Elmt
7634 (Copy_Generic_Node (Node (M), Empty, Instantiating), L);
7635 Next_Elmt (M);
7636 end loop;
7637
7638 return L;
7639
7640 else
7641 return No_Elist;
7642 end if;
7643 end Copy_Generic_Elist;
7644
7645 -----------------------
7646 -- Copy_Generic_List --
7647 -----------------------
7648
7649 function Copy_Generic_List
7650 (L : List_Id;
7651 Parent_Id : Node_Id) return List_Id
7652 is
7653 N : Node_Id;
7654 New_L : List_Id;
7655
7656 begin
7657 if Present (L) then
7658 New_L := New_List;
7659 Set_Parent (New_L, Parent_Id);
7660
7661 N := First (L);
7662 while Present (N) loop
7663 Append (Copy_Generic_Node (N, Empty, Instantiating), New_L);
7664 Next (N);
7665 end loop;
7666
7667 return New_L;
7668
7669 else
7670 return No_List;
7671 end if;
7672 end Copy_Generic_List;
7673
7674 ---------------------------
7675 -- In_Defining_Unit_Name --
7676 ---------------------------
7677
7678 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean is
7679 begin
7680 return
7681 Present (Parent (Nam))
7682 and then (Nkind (Parent (Nam)) = N_Defining_Program_Unit_Name
7683 or else
7684 (Nkind (Parent (Nam)) = N_Expanded_Name
7685 and then In_Defining_Unit_Name (Parent (Nam))));
7686 end In_Defining_Unit_Name;
7687
7688 -- Start of processing for Copy_Generic_Node
7689
7690 begin
7691 if N = Empty then
7692 return N;
7693 end if;
7694
7695 New_N := New_Copy (N);
7696
7697 -- Copy aspects if present
7698
7699 if Has_Aspects (N) then
7700 Set_Has_Aspects (New_N, False);
7701 Set_Aspect_Specifications
7702 (New_N, Copy_Generic_List (Aspect_Specifications (N), Parent_Id));
7703 end if;
7704
7705 -- If we are instantiating, we want to adjust the sloc based on the
7706 -- current S_Adjustment. However, if this is the root node of a subunit,
7707 -- we need to defer that adjustment to below (see "elsif Instantiating
7708 -- and Was_Stub"), so it comes after Create_Instantiation_Source has
7709 -- computed the adjustment.
7710
7711 if Instantiating
7712 and then not (Nkind (N) in N_Proper_Body
7713 and then Was_Originally_Stub (N))
7714 then
7715 Adjust_Instantiation_Sloc (New_N, S_Adjustment);
7716 end if;
7717
7718 if not Is_List_Member (N) then
7719 Set_Parent (New_N, Parent_Id);
7720 end if;
7721
7722 -- Special casing for identifiers and other entity names and operators
7723
7724 if Nkind_In (New_N, N_Character_Literal,
7725 N_Expanded_Name,
7726 N_Identifier,
7727 N_Operator_Symbol)
7728 or else Nkind (New_N) in N_Op
7729 then
7730 if not Instantiating then
7731
7732 -- Link both nodes in order to assign subsequently the entity of
7733 -- the copy to the original node, in case this is a global
7734 -- reference.
7735
7736 Set_Associated_Node (N, New_N);
7737
7738 -- If we are within an instantiation, this is a nested generic
7739 -- that has already been analyzed at the point of definition.
7740 -- We must preserve references that were global to the enclosing
7741 -- parent at that point. Other occurrences, whether global or
7742 -- local to the current generic, must be resolved anew, so we
7743 -- reset the entity in the generic copy. A global reference has a
7744 -- smaller depth than the parent, or else the same depth in case
7745 -- both are distinct compilation units.
7746
7747 -- A child unit is implicitly declared within the enclosing parent
7748 -- but is in fact global to it, and must be preserved.
7749
7750 -- It is also possible for Current_Instantiated_Parent to be
7751 -- defined, and for this not to be a nested generic, namely if
7752 -- the unit is loaded through Rtsfind. In that case, the entity of
7753 -- New_N is only a link to the associated node, and not a defining
7754 -- occurrence.
7755
7756 -- The entities for parent units in the defining_program_unit of a
7757 -- generic child unit are established when the context of the unit
7758 -- is first analyzed, before the generic copy is made. They are
7759 -- preserved in the copy for use in ASIS queries.
7760
7761 Ent := Entity (New_N);
7762
7763 if No (Current_Instantiated_Parent.Gen_Id) then
7764 if No (Ent)
7765 or else Nkind (Ent) /= N_Defining_Identifier
7766 or else not In_Defining_Unit_Name (N)
7767 then
7768 Set_Associated_Node (New_N, Empty);
7769 end if;
7770
7771 elsif No (Ent)
7772 or else
7773 not Nkind_In (Ent, N_Defining_Identifier,
7774 N_Defining_Character_Literal,
7775 N_Defining_Operator_Symbol)
7776 or else No (Scope (Ent))
7777 or else
7778 (Scope (Ent) = Current_Instantiated_Parent.Gen_Id
7779 and then not Is_Child_Unit (Ent))
7780 or else
7781 (Scope_Depth (Scope (Ent)) >
7782 Scope_Depth (Current_Instantiated_Parent.Gen_Id)
7783 and then
7784 Get_Source_Unit (Ent) =
7785 Get_Source_Unit (Current_Instantiated_Parent.Gen_Id))
7786 then
7787 Set_Associated_Node (New_N, Empty);
7788 end if;
7789
7790 -- Case of instantiating identifier or some other name or operator
7791
7792 else
7793 -- If the associated node is still defined, the entity in it
7794 -- is global, and must be copied to the instance. If this copy
7795 -- is being made for a body to inline, it is applied to an
7796 -- instantiated tree, and the entity is already present and
7797 -- must be also preserved.
7798
7799 declare
7800 Assoc : constant Node_Id := Get_Associated_Node (N);
7801
7802 begin
7803 if Present (Assoc) then
7804 if Nkind (Assoc) = Nkind (N) then
7805 Set_Entity (New_N, Entity (Assoc));
7806 Check_Private_View (N);
7807
7808 -- The node is a reference to a global type and acts as the
7809 -- subtype mark of a qualified expression created in order
7810 -- to aid resolution of accidental overloading in instances.
7811 -- Since N is a reference to a type, the Associated_Node of
7812 -- N denotes an entity rather than another identifier. See
7813 -- Qualify_Universal_Operands for details.
7814
7815 elsif Nkind (N) = N_Identifier
7816 and then Nkind (Parent (N)) = N_Qualified_Expression
7817 and then Subtype_Mark (Parent (N)) = N
7818 and then Is_Qualified_Universal_Literal (Parent (N))
7819 then
7820 Set_Entity (New_N, Assoc);
7821
7822 -- The name in the call may be a selected component if the
7823 -- call has not been analyzed yet, as may be the case for
7824 -- pre/post conditions in a generic unit.
7825
7826 elsif Nkind (Assoc) = N_Function_Call
7827 and then Is_Entity_Name (Name (Assoc))
7828 then
7829 Set_Entity (New_N, Entity (Name (Assoc)));
7830
7831 elsif Nkind_In (Assoc, N_Defining_Identifier,
7832 N_Defining_Character_Literal,
7833 N_Defining_Operator_Symbol)
7834 and then Expander_Active
7835 then
7836 -- Inlining case: we are copying a tree that contains
7837 -- global entities, which are preserved in the copy to be
7838 -- used for subsequent inlining.
7839
7840 null;
7841
7842 else
7843 Set_Entity (New_N, Empty);
7844 end if;
7845 end if;
7846 end;
7847 end if;
7848
7849 -- For expanded name, we must copy the Prefix and Selector_Name
7850
7851 if Nkind (N) = N_Expanded_Name then
7852 Set_Prefix
7853 (New_N, Copy_Generic_Node (Prefix (N), New_N, Instantiating));
7854
7855 Set_Selector_Name (New_N,
7856 Copy_Generic_Node (Selector_Name (N), New_N, Instantiating));
7857
7858 -- For operators, copy the operands
7859
7860 elsif Nkind (N) in N_Op then
7861 if Nkind (N) in N_Binary_Op then
7862 Set_Left_Opnd (New_N,
7863 Copy_Generic_Node (Left_Opnd (N), New_N, Instantiating));
7864 end if;
7865
7866 Set_Right_Opnd (New_N,
7867 Copy_Generic_Node (Right_Opnd (N), New_N, Instantiating));
7868 end if;
7869
7870 -- Establish a link between an entity from the generic template and the
7871 -- corresponding entity in the generic copy to be analyzed.
7872
7873 elsif Nkind (N) in N_Entity then
7874 if not Instantiating then
7875 Set_Associated_Entity (N, New_N);
7876 end if;
7877
7878 -- Clear any existing link the copy may inherit from the replicated
7879 -- generic template entity.
7880
7881 Set_Associated_Entity (New_N, Empty);
7882
7883 -- Special casing for stubs
7884
7885 elsif Nkind (N) in N_Body_Stub then
7886
7887 -- In any case, we must copy the specification or defining
7888 -- identifier as appropriate.
7889
7890 if Nkind (N) = N_Subprogram_Body_Stub then
7891 Set_Specification (New_N,
7892 Copy_Generic_Node (Specification (N), New_N, Instantiating));
7893
7894 else
7895 Set_Defining_Identifier (New_N,
7896 Copy_Generic_Node
7897 (Defining_Identifier (N), New_N, Instantiating));
7898 end if;
7899
7900 -- If we are not instantiating, then this is where we load and
7901 -- analyze subunits, i.e. at the point where the stub occurs. A
7902 -- more permissive system might defer this analysis to the point
7903 -- of instantiation, but this seems too complicated for now.
7904
7905 if not Instantiating then
7906 declare
7907 Subunit_Name : constant Unit_Name_Type := Get_Unit_Name (N);
7908 Subunit : Node_Id;
7909 Unum : Unit_Number_Type;
7910 New_Body : Node_Id;
7911
7912 begin
7913 -- Make sure that, if it is a subunit of the main unit that is
7914 -- preprocessed and if -gnateG is specified, the preprocessed
7915 -- file will be written.
7916
7917 Lib.Analysing_Subunit_Of_Main :=
7918 Lib.In_Extended_Main_Source_Unit (N);
7919 Unum :=
7920 Load_Unit
7921 (Load_Name => Subunit_Name,
7922 Required => False,
7923 Subunit => True,
7924 Error_Node => N);
7925 Lib.Analysing_Subunit_Of_Main := False;
7926
7927 -- If the proper body is not found, a warning message will be
7928 -- emitted when analyzing the stub, or later at the point of
7929 -- instantiation. Here we just leave the stub as is.
7930
7931 if Unum = No_Unit then
7932 Subunits_Missing := True;
7933 goto Subunit_Not_Found;
7934 end if;
7935
7936 Subunit := Cunit (Unum);
7937
7938 if Nkind (Unit (Subunit)) /= N_Subunit then
7939 Error_Msg_N
7940 ("found child unit instead of expected SEPARATE subunit",
7941 Subunit);
7942 Error_Msg_Sloc := Sloc (N);
7943 Error_Msg_N ("\to complete stub #", Subunit);
7944 goto Subunit_Not_Found;
7945 end if;
7946
7947 -- We must create a generic copy of the subunit, in order to
7948 -- perform semantic analysis on it, and we must replace the
7949 -- stub in the original generic unit with the subunit, in order
7950 -- to preserve non-local references within.
7951
7952 -- Only the proper body needs to be copied. Library_Unit and
7953 -- context clause are simply inherited by the generic copy.
7954 -- Note that the copy (which may be recursive if there are
7955 -- nested subunits) must be done first, before attaching it to
7956 -- the enclosing generic.
7957
7958 New_Body :=
7959 Copy_Generic_Node
7960 (Proper_Body (Unit (Subunit)),
7961 Empty, Instantiating => False);
7962
7963 -- Now place the original proper body in the original generic
7964 -- unit. This is a body, not a compilation unit.
7965
7966 Rewrite (N, Proper_Body (Unit (Subunit)));
7967 Set_Is_Compilation_Unit (Defining_Entity (N), False);
7968 Set_Was_Originally_Stub (N);
7969
7970 -- Finally replace the body of the subunit with its copy, and
7971 -- make this new subunit into the library unit of the generic
7972 -- copy, which does not have stubs any longer.
7973
7974 Set_Proper_Body (Unit (Subunit), New_Body);
7975 Set_Library_Unit (New_N, Subunit);
7976 Inherit_Context (Unit (Subunit), N);
7977 end;
7978
7979 -- If we are instantiating, this must be an error case, since
7980 -- otherwise we would have replaced the stub node by the proper body
7981 -- that corresponds. So just ignore it in the copy (i.e. we have
7982 -- copied it, and that is good enough).
7983
7984 else
7985 null;
7986 end if;
7987
7988 <<Subunit_Not_Found>> null;
7989
7990 -- If the node is a compilation unit, it is the subunit of a stub, which
7991 -- has been loaded already (see code below). In this case, the library
7992 -- unit field of N points to the parent unit (which is a compilation
7993 -- unit) and need not (and cannot) be copied.
7994
7995 -- When the proper body of the stub is analyzed, the library_unit link
7996 -- is used to establish the proper context (see sem_ch10).
7997
7998 -- The other fields of a compilation unit are copied as usual
7999
8000 elsif Nkind (N) = N_Compilation_Unit then
8001
8002 -- This code can only be executed when not instantiating, because in
8003 -- the copy made for an instantiation, the compilation unit node has
8004 -- disappeared at the point that a stub is replaced by its proper
8005 -- body.
8006
8007 pragma Assert (not Instantiating);
8008
8009 Set_Context_Items (New_N,
8010 Copy_Generic_List (Context_Items (N), New_N));
8011
8012 Set_Unit (New_N,
8013 Copy_Generic_Node (Unit (N), New_N, Instantiating => False));
8014
8015 Set_First_Inlined_Subprogram (New_N,
8016 Copy_Generic_Node
8017 (First_Inlined_Subprogram (N), New_N, Instantiating => False));
8018
8019 Set_Aux_Decls_Node
8020 (New_N,
8021 Copy_Generic_Node
8022 (Aux_Decls_Node (N), New_N, Instantiating => False));
8023
8024 -- For an assignment node, the assignment is known to be semantically
8025 -- legal if we are instantiating the template. This avoids incorrect
8026 -- diagnostics in generated code.
8027
8028 elsif Nkind (N) = N_Assignment_Statement then
8029
8030 -- Copy name and expression fields in usual manner
8031
8032 Set_Name (New_N,
8033 Copy_Generic_Node (Name (N), New_N, Instantiating));
8034
8035 Set_Expression (New_N,
8036 Copy_Generic_Node (Expression (N), New_N, Instantiating));
8037
8038 if Instantiating then
8039 Set_Assignment_OK (Name (New_N), True);
8040 end if;
8041
8042 elsif Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
8043 if not Instantiating then
8044 Set_Associated_Node (N, New_N);
8045
8046 else
8047 if Present (Get_Associated_Node (N))
8048 and then Nkind (Get_Associated_Node (N)) = Nkind (N)
8049 then
8050 -- In the generic the aggregate has some composite type. If at
8051 -- the point of instantiation the type has a private view,
8052 -- install the full view (and that of its ancestors, if any).
8053
8054 declare
8055 T : Entity_Id := (Etype (Get_Associated_Node (New_N)));
8056 Rt : Entity_Id;
8057
8058 begin
8059 if Present (T) and then Is_Private_Type (T) then
8060 Switch_View (T);
8061 end if;
8062
8063 if Present (T)
8064 and then Is_Tagged_Type (T)
8065 and then Is_Derived_Type (T)
8066 then
8067 Rt := Root_Type (T);
8068
8069 loop
8070 T := Etype (T);
8071
8072 if Is_Private_Type (T) then
8073 Switch_View (T);
8074 end if;
8075
8076 exit when T = Rt;
8077 end loop;
8078 end if;
8079 end;
8080 end if;
8081 end if;
8082
8083 -- Do not copy the associated node, which points to the generic copy
8084 -- of the aggregate.
8085
8086 declare
8087 use Atree.Unchecked_Access;
8088 -- This code section is part of the implementation of an untyped
8089 -- tree traversal, so it needs direct access to node fields.
8090
8091 begin
8092 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
8093 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
8094 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
8095 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
8096 end;
8097
8098 -- Allocators do not have an identifier denoting the access type, so we
8099 -- must locate it through the expression to check whether the views are
8100 -- consistent.
8101
8102 elsif Nkind (N) = N_Allocator
8103 and then Nkind (Expression (N)) = N_Qualified_Expression
8104 and then Is_Entity_Name (Subtype_Mark (Expression (N)))
8105 and then Instantiating
8106 then
8107 declare
8108 T : constant Node_Id :=
8109 Get_Associated_Node (Subtype_Mark (Expression (N)));
8110 Acc_T : Entity_Id;
8111
8112 begin
8113 if Present (T) then
8114
8115 -- Retrieve the allocator node in the generic copy
8116
8117 Acc_T := Etype (Parent (Parent (T)));
8118
8119 if Present (Acc_T) and then Is_Private_Type (Acc_T) then
8120 Switch_View (Acc_T);
8121 end if;
8122 end if;
8123
8124 Copy_Descendants;
8125 end;
8126
8127 -- For a proper body, we must catch the case of a proper body that
8128 -- replaces a stub. This represents the point at which a separate
8129 -- compilation unit, and hence template file, may be referenced, so we
8130 -- must make a new source instantiation entry for the template of the
8131 -- subunit, and ensure that all nodes in the subunit are adjusted using
8132 -- this new source instantiation entry.
8133
8134 elsif Nkind (N) in N_Proper_Body then
8135 declare
8136 Save_Adjustment : constant Sloc_Adjustment := S_Adjustment;
8137 begin
8138 if Instantiating and then Was_Originally_Stub (N) then
8139 Create_Instantiation_Source
8140 (Instantiation_Node,
8141 Defining_Entity (N),
8142 S_Adjustment);
8143
8144 Adjust_Instantiation_Sloc (New_N, S_Adjustment);
8145 end if;
8146
8147 -- Now copy the fields of the proper body, using the new
8148 -- adjustment factor if one was needed as per test above.
8149
8150 Copy_Descendants;
8151
8152 -- Restore the original adjustment factor
8153
8154 S_Adjustment := Save_Adjustment;
8155 end;
8156
8157 elsif Nkind (N) = N_Pragma and then Instantiating then
8158
8159 -- Do not copy Comment or Ident pragmas their content is relevant to
8160 -- the generic unit, not to the instantiating unit.
8161
8162 if Nam_In (Pragma_Name_Unmapped (N), Name_Comment, Name_Ident) then
8163 New_N := Make_Null_Statement (Sloc (N));
8164
8165 -- Do not copy pragmas generated from aspects because the pragmas do
8166 -- not carry any semantic information, plus they will be regenerated
8167 -- in the instance.
8168
8169 -- However, generating C we need to copy them since postconditions
8170 -- are inlined by the front end, and the front-end inlining machinery
8171 -- relies on this routine to perform inlining.
8172
8173 elsif From_Aspect_Specification (N)
8174 and then not Modify_Tree_For_C
8175 then
8176 New_N := Make_Null_Statement (Sloc (N));
8177
8178 else
8179 Copy_Descendants;
8180 end if;
8181
8182 elsif Nkind_In (N, N_Integer_Literal, N_Real_Literal) then
8183
8184 -- No descendant fields need traversing
8185
8186 null;
8187
8188 elsif Nkind (N) = N_String_Literal
8189 and then Present (Etype (N))
8190 and then Instantiating
8191 then
8192 -- If the string is declared in an outer scope, the string_literal
8193 -- subtype created for it may have the wrong scope. Force reanalysis
8194 -- of the constant to generate a new itype in the proper context.
8195
8196 Set_Etype (New_N, Empty);
8197 Set_Analyzed (New_N, False);
8198
8199 -- For the remaining nodes, copy their descendants recursively
8200
8201 else
8202 Copy_Descendants;
8203
8204 if Instantiating and then Nkind (N) = N_Subprogram_Body then
8205 Set_Generic_Parent (Specification (New_N), N);
8206
8207 -- Should preserve Corresponding_Spec??? (12.3(14))
8208 end if;
8209 end if;
8210
8211 -- Propagate dimensions if present, so that they are reflected in the
8212 -- instance.
8213
8214 if Nkind (N) in N_Has_Etype
8215 and then (Nkind (N) in N_Op or else Is_Entity_Name (N))
8216 and then Present (Etype (N))
8217 and then Is_Floating_Point_Type (Etype (N))
8218 and then Has_Dimension_System (Etype (N))
8219 then
8220 Copy_Dimensions (N, New_N);
8221 end if;
8222
8223 return New_N;
8224 end Copy_Generic_Node;
8225
8226 ----------------------------
8227 -- Denotes_Formal_Package --
8228 ----------------------------
8229
8230 function Denotes_Formal_Package
8231 (Pack : Entity_Id;
8232 On_Exit : Boolean := False;
8233 Instance : Entity_Id := Empty) return Boolean
8234 is
8235 Par : Entity_Id;
8236 Scop : constant Entity_Id := Scope (Pack);
8237 E : Entity_Id;
8238
8239 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean;
8240 -- The package in question may be an actual for a previous formal
8241 -- package P of the current instance, so examine its actuals as well.
8242 -- This must be recursive over other formal packages.
8243
8244 ----------------------------------
8245 -- Is_Actual_Of_Previous_Formal --
8246 ----------------------------------
8247
8248 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean is
8249 E1 : Entity_Id;
8250
8251 begin
8252 E1 := First_Entity (P);
8253 while Present (E1) and then E1 /= Instance loop
8254 if Ekind (E1) = E_Package
8255 and then Nkind (Parent (E1)) = N_Package_Renaming_Declaration
8256 then
8257 if Renamed_Object (E1) = Pack then
8258 return True;
8259
8260 elsif E1 = P or else Renamed_Object (E1) = P then
8261 return False;
8262
8263 elsif Is_Actual_Of_Previous_Formal (E1) then
8264 return True;
8265 end if;
8266 end if;
8267
8268 Next_Entity (E1);
8269 end loop;
8270
8271 return False;
8272 end Is_Actual_Of_Previous_Formal;
8273
8274 -- Start of processing for Denotes_Formal_Package
8275
8276 begin
8277 if On_Exit then
8278 Par :=
8279 Instance_Envs.Table
8280 (Instance_Envs.Last).Instantiated_Parent.Act_Id;
8281 else
8282 Par := Current_Instantiated_Parent.Act_Id;
8283 end if;
8284
8285 if Ekind (Scop) = E_Generic_Package
8286 or else Nkind (Unit_Declaration_Node (Scop)) =
8287 N_Generic_Subprogram_Declaration
8288 then
8289 return True;
8290
8291 elsif Nkind (Original_Node (Unit_Declaration_Node (Pack))) =
8292 N_Formal_Package_Declaration
8293 then
8294 return True;
8295
8296 elsif No (Par) then
8297 return False;
8298
8299 else
8300 -- Check whether this package is associated with a formal package of
8301 -- the enclosing instantiation. Iterate over the list of renamings.
8302
8303 E := First_Entity (Par);
8304 while Present (E) loop
8305 if Ekind (E) /= E_Package
8306 or else Nkind (Parent (E)) /= N_Package_Renaming_Declaration
8307 then
8308 null;
8309
8310 elsif Renamed_Object (E) = Par then
8311 return False;
8312
8313 elsif Renamed_Object (E) = Pack then
8314 return True;
8315
8316 elsif Is_Actual_Of_Previous_Formal (E) then
8317 return True;
8318
8319 end if;
8320
8321 Next_Entity (E);
8322 end loop;
8323
8324 return False;
8325 end if;
8326 end Denotes_Formal_Package;
8327
8328 -----------------
8329 -- End_Generic --
8330 -----------------
8331
8332 procedure End_Generic is
8333 begin
8334 -- ??? More things could be factored out in this routine. Should
8335 -- probably be done at a later stage.
8336
8337 Inside_A_Generic := Generic_Flags.Table (Generic_Flags.Last);
8338 Generic_Flags.Decrement_Last;
8339
8340 Expander_Mode_Restore;
8341 end End_Generic;
8342
8343 -------------
8344 -- Earlier --
8345 -------------
8346
8347 function Earlier (N1, N2 : Node_Id) return Boolean is
8348 procedure Find_Depth (P : in out Node_Id; D : in out Integer);
8349 -- Find distance from given node to enclosing compilation unit
8350
8351 ----------------
8352 -- Find_Depth --
8353 ----------------
8354
8355 procedure Find_Depth (P : in out Node_Id; D : in out Integer) is
8356 begin
8357 while Present (P)
8358 and then Nkind (P) /= N_Compilation_Unit
8359 loop
8360 P := True_Parent (P);
8361 D := D + 1;
8362 end loop;
8363 end Find_Depth;
8364
8365 -- Local declarations
8366
8367 D1 : Integer := 0;
8368 D2 : Integer := 0;
8369 P1 : Node_Id := N1;
8370 P2 : Node_Id := N2;
8371 T1 : Source_Ptr;
8372 T2 : Source_Ptr;
8373
8374 -- Start of processing for Earlier
8375
8376 begin
8377 Find_Depth (P1, D1);
8378 Find_Depth (P2, D2);
8379
8380 if P1 /= P2 then
8381 return False;
8382 else
8383 P1 := N1;
8384 P2 := N2;
8385 end if;
8386
8387 while D1 > D2 loop
8388 P1 := True_Parent (P1);
8389 D1 := D1 - 1;
8390 end loop;
8391
8392 while D2 > D1 loop
8393 P2 := True_Parent (P2);
8394 D2 := D2 - 1;
8395 end loop;
8396
8397 -- At this point P1 and P2 are at the same distance from the root.
8398 -- We examine their parents until we find a common declarative list.
8399 -- If we reach the root, N1 and N2 do not descend from the same
8400 -- declarative list (e.g. one is nested in the declarative part and
8401 -- the other is in a block in the statement part) and the earlier
8402 -- one is already frozen.
8403
8404 while not Is_List_Member (P1)
8405 or else not Is_List_Member (P2)
8406 or else List_Containing (P1) /= List_Containing (P2)
8407 loop
8408 P1 := True_Parent (P1);
8409 P2 := True_Parent (P2);
8410
8411 if Nkind (Parent (P1)) = N_Subunit then
8412 P1 := Corresponding_Stub (Parent (P1));
8413 end if;
8414
8415 if Nkind (Parent (P2)) = N_Subunit then
8416 P2 := Corresponding_Stub (Parent (P2));
8417 end if;
8418
8419 if P1 = P2 then
8420 return False;
8421 end if;
8422 end loop;
8423
8424 -- Expanded code usually shares the source location of the original
8425 -- construct it was generated for. This however may not necessarily
8426 -- reflect the true location of the code within the tree.
8427
8428 -- Before comparing the slocs of the two nodes, make sure that we are
8429 -- working with correct source locations. Assume that P1 is to the left
8430 -- of P2. If either one does not come from source, traverse the common
8431 -- list heading towards the other node and locate the first source
8432 -- statement.
8433
8434 -- P1 P2
8435 -- ----+===+===+--------------+===+===+----
8436 -- expanded code expanded code
8437
8438 if not Comes_From_Source (P1) then
8439 while Present (P1) loop
8440
8441 -- Neither P2 nor a source statement were located during the
8442 -- search. If we reach the end of the list, then P1 does not
8443 -- occur earlier than P2.
8444
8445 -- ---->
8446 -- start --- P2 ----- P1 --- end
8447
8448 if No (Next (P1)) then
8449 return False;
8450
8451 -- We encounter P2 while going to the right of the list. This
8452 -- means that P1 does indeed appear earlier.
8453
8454 -- ---->
8455 -- start --- P1 ===== P2 --- end
8456 -- expanded code in between
8457
8458 elsif P1 = P2 then
8459 return True;
8460
8461 -- No need to look any further since we have located a source
8462 -- statement.
8463
8464 elsif Comes_From_Source (P1) then
8465 exit;
8466 end if;
8467
8468 -- Keep going right
8469
8470 Next (P1);
8471 end loop;
8472 end if;
8473
8474 if not Comes_From_Source (P2) then
8475 while Present (P2) loop
8476
8477 -- Neither P1 nor a source statement were located during the
8478 -- search. If we reach the start of the list, then P1 does not
8479 -- occur earlier than P2.
8480
8481 -- <----
8482 -- start --- P2 --- P1 --- end
8483
8484 if No (Prev (P2)) then
8485 return False;
8486
8487 -- We encounter P1 while going to the left of the list. This
8488 -- means that P1 does indeed appear earlier.
8489
8490 -- <----
8491 -- start --- P1 ===== P2 --- end
8492 -- expanded code in between
8493
8494 elsif P2 = P1 then
8495 return True;
8496
8497 -- No need to look any further since we have located a source
8498 -- statement.
8499
8500 elsif Comes_From_Source (P2) then
8501 exit;
8502 end if;
8503
8504 -- Keep going left
8505
8506 Prev (P2);
8507 end loop;
8508 end if;
8509
8510 -- At this point either both nodes came from source or we approximated
8511 -- their source locations through neighboring source statements.
8512
8513 T1 := Top_Level_Location (Sloc (P1));
8514 T2 := Top_Level_Location (Sloc (P2));
8515
8516 -- When two nodes come from the same instance, they have identical top
8517 -- level locations. To determine proper relation within the tree, check
8518 -- their locations within the template.
8519
8520 if T1 = T2 then
8521 return Sloc (P1) < Sloc (P2);
8522
8523 -- The two nodes either come from unrelated instances or do not come
8524 -- from instantiated code at all.
8525
8526 else
8527 return T1 < T2;
8528 end if;
8529 end Earlier;
8530
8531 ----------------------
8532 -- Find_Actual_Type --
8533 ----------------------
8534
8535 function Find_Actual_Type
8536 (Typ : Entity_Id;
8537 Gen_Type : Entity_Id) return Entity_Id
8538 is
8539 Gen_Scope : constant Entity_Id := Scope (Gen_Type);
8540 T : Entity_Id;
8541
8542 begin
8543 -- Special processing only applies to child units
8544
8545 if not Is_Child_Unit (Gen_Scope) then
8546 return Get_Instance_Of (Typ);
8547
8548 -- If designated or component type is itself a formal of the child unit,
8549 -- its instance is available.
8550
8551 elsif Scope (Typ) = Gen_Scope then
8552 return Get_Instance_Of (Typ);
8553
8554 -- If the array or access type is not declared in the parent unit,
8555 -- no special processing needed.
8556
8557 elsif not Is_Generic_Type (Typ)
8558 and then Scope (Gen_Scope) /= Scope (Typ)
8559 then
8560 return Get_Instance_Of (Typ);
8561
8562 -- Otherwise, retrieve designated or component type by visibility
8563
8564 else
8565 T := Current_Entity (Typ);
8566 while Present (T) loop
8567 if In_Open_Scopes (Scope (T)) then
8568 return T;
8569 elsif Is_Generic_Actual_Type (T) then
8570 return T;
8571 end if;
8572
8573 T := Homonym (T);
8574 end loop;
8575
8576 return Typ;
8577 end if;
8578 end Find_Actual_Type;
8579
8580 ----------------------------
8581 -- Freeze_Subprogram_Body --
8582 ----------------------------
8583
8584 procedure Freeze_Subprogram_Body
8585 (Inst_Node : Node_Id;
8586 Gen_Body : Node_Id;
8587 Pack_Id : Entity_Id)
8588 is
8589 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
8590 Par : constant Entity_Id := Scope (Gen_Unit);
8591 E_G_Id : Entity_Id;
8592 Enc_G : Entity_Id;
8593 Enc_I : Node_Id;
8594 F_Node : Node_Id;
8595
8596 function Enclosing_Package_Body (N : Node_Id) return Node_Id;
8597 -- Find innermost package body that encloses the given node, and which
8598 -- is not a compilation unit. Freeze nodes for the instance, or for its
8599 -- enclosing body, may be inserted after the enclosing_body of the
8600 -- generic unit. Used to determine proper placement of freeze node for
8601 -- both package and subprogram instances.
8602
8603 function Package_Freeze_Node (B : Node_Id) return Node_Id;
8604 -- Find entity for given package body, and locate or create a freeze
8605 -- node for it.
8606
8607 ----------------------------
8608 -- Enclosing_Package_Body --
8609 ----------------------------
8610
8611 function Enclosing_Package_Body (N : Node_Id) return Node_Id is
8612 P : Node_Id;
8613
8614 begin
8615 P := Parent (N);
8616 while Present (P)
8617 and then Nkind (Parent (P)) /= N_Compilation_Unit
8618 loop
8619 if Nkind (P) = N_Package_Body then
8620 if Nkind (Parent (P)) = N_Subunit then
8621 return Corresponding_Stub (Parent (P));
8622 else
8623 return P;
8624 end if;
8625 end if;
8626
8627 P := True_Parent (P);
8628 end loop;
8629
8630 return Empty;
8631 end Enclosing_Package_Body;
8632
8633 -------------------------
8634 -- Package_Freeze_Node --
8635 -------------------------
8636
8637 function Package_Freeze_Node (B : Node_Id) return Node_Id is
8638 Id : Entity_Id;
8639
8640 begin
8641 if Nkind (B) = N_Package_Body then
8642 Id := Corresponding_Spec (B);
8643 else pragma Assert (Nkind (B) = N_Package_Body_Stub);
8644 Id := Corresponding_Spec (Proper_Body (Unit (Library_Unit (B))));
8645 end if;
8646
8647 Ensure_Freeze_Node (Id);
8648 return Freeze_Node (Id);
8649 end Package_Freeze_Node;
8650
8651 -- Start of processing for Freeze_Subprogram_Body
8652
8653 begin
8654 -- If the instance and the generic body appear within the same unit, and
8655 -- the instance precedes the generic, the freeze node for the instance
8656 -- must appear after that of the generic. If the generic is nested
8657 -- within another instance I2, then current instance must be frozen
8658 -- after I2. In both cases, the freeze nodes are those of enclosing
8659 -- packages. Otherwise, the freeze node is placed at the end of the
8660 -- current declarative part.
8661
8662 Enc_G := Enclosing_Package_Body (Gen_Body);
8663 Enc_I := Enclosing_Package_Body (Inst_Node);
8664 Ensure_Freeze_Node (Pack_Id);
8665 F_Node := Freeze_Node (Pack_Id);
8666
8667 if Is_Generic_Instance (Par)
8668 and then Present (Freeze_Node (Par))
8669 and then In_Same_Declarative_Part (Freeze_Node (Par), Inst_Node)
8670 then
8671 -- The parent was a premature instantiation. Insert freeze node at
8672 -- the end the current declarative part.
8673
8674 if Is_Known_Guaranteed_ABE (Get_Unit_Instantiation_Node (Par)) then
8675 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8676
8677 -- Handle the following case:
8678 --
8679 -- package Parent_Inst is new ...
8680 -- Parent_Inst []
8681 --
8682 -- procedure P ... -- this body freezes Parent_Inst
8683 --
8684 -- package Inst is new ...
8685 --
8686 -- In this particular scenario, the freeze node for Inst must be
8687 -- inserted in the same manner as that of Parent_Inst - before the
8688 -- next source body or at the end of the declarative list (body not
8689 -- available). If body P did not exist and Parent_Inst was frozen
8690 -- after Inst, either by a body following Inst or at the end of the
8691 -- declarative region, the freeze node for Inst must be inserted
8692 -- after that of Parent_Inst. This relation is established by
8693 -- comparing the Slocs of Parent_Inst freeze node and Inst.
8694
8695 elsif List_Containing (Get_Unit_Instantiation_Node (Par)) =
8696 List_Containing (Inst_Node)
8697 and then Sloc (Freeze_Node (Par)) < Sloc (Inst_Node)
8698 then
8699 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8700
8701 else
8702 Insert_After (Freeze_Node (Par), F_Node);
8703 end if;
8704
8705 -- The body enclosing the instance should be frozen after the body that
8706 -- includes the generic, because the body of the instance may make
8707 -- references to entities therein. If the two are not in the same
8708 -- declarative part, or if the one enclosing the instance is frozen
8709 -- already, freeze the instance at the end of the current declarative
8710 -- part.
8711
8712 elsif Is_Generic_Instance (Par)
8713 and then Present (Freeze_Node (Par))
8714 and then Present (Enc_I)
8715 then
8716 if In_Same_Declarative_Part (Freeze_Node (Par), Enc_I)
8717 or else
8718 (Nkind (Enc_I) = N_Package_Body
8719 and then
8720 In_Same_Declarative_Part (Freeze_Node (Par), Parent (Enc_I)))
8721 then
8722 -- The enclosing package may contain several instances. Rather
8723 -- than computing the earliest point at which to insert its freeze
8724 -- node, we place it at the end of the declarative part of the
8725 -- parent of the generic.
8726
8727 Insert_Freeze_Node_For_Instance
8728 (Freeze_Node (Par), Package_Freeze_Node (Enc_I));
8729 end if;
8730
8731 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8732
8733 elsif Present (Enc_G)
8734 and then Present (Enc_I)
8735 and then Enc_G /= Enc_I
8736 and then Earlier (Inst_Node, Gen_Body)
8737 then
8738 if Nkind (Enc_G) = N_Package_Body then
8739 E_G_Id :=
8740 Corresponding_Spec (Enc_G);
8741 else pragma Assert (Nkind (Enc_G) = N_Package_Body_Stub);
8742 E_G_Id :=
8743 Corresponding_Spec (Proper_Body (Unit (Library_Unit (Enc_G))));
8744 end if;
8745
8746 -- Freeze package that encloses instance, and place node after the
8747 -- package that encloses generic. If enclosing package is already
8748 -- frozen we have to assume it is at the proper place. This may be a
8749 -- potential ABE that requires dynamic checking. Do not add a freeze
8750 -- node if the package that encloses the generic is inside the body
8751 -- that encloses the instance, because the freeze node would be in
8752 -- the wrong scope. Additional contortions needed if the bodies are
8753 -- within a subunit.
8754
8755 declare
8756 Enclosing_Body : Node_Id;
8757
8758 begin
8759 if Nkind (Enc_I) = N_Package_Body_Stub then
8760 Enclosing_Body := Proper_Body (Unit (Library_Unit (Enc_I)));
8761 else
8762 Enclosing_Body := Enc_I;
8763 end if;
8764
8765 if Parent (List_Containing (Enc_G)) /= Enclosing_Body then
8766 Insert_Freeze_Node_For_Instance
8767 (Enc_G, Package_Freeze_Node (Enc_I));
8768 end if;
8769 end;
8770
8771 -- Freeze enclosing subunit before instance
8772
8773 Ensure_Freeze_Node (E_G_Id);
8774
8775 if not Is_List_Member (Freeze_Node (E_G_Id)) then
8776 Insert_After (Enc_G, Freeze_Node (E_G_Id));
8777 end if;
8778
8779 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8780
8781 else
8782 -- If none of the above, insert freeze node at the end of the current
8783 -- declarative part.
8784
8785 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8786 end if;
8787 end Freeze_Subprogram_Body;
8788
8789 ----------------
8790 -- Get_Gen_Id --
8791 ----------------
8792
8793 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id is
8794 begin
8795 return Generic_Renamings.Table (E).Gen_Id;
8796 end Get_Gen_Id;
8797
8798 ---------------------
8799 -- Get_Instance_Of --
8800 ---------------------
8801
8802 function Get_Instance_Of (A : Entity_Id) return Entity_Id is
8803 Res : constant Assoc_Ptr := Generic_Renamings_HTable.Get (A);
8804
8805 begin
8806 if Res /= Assoc_Null then
8807 return Generic_Renamings.Table (Res).Act_Id;
8808
8809 else
8810 -- On exit, entity is not instantiated: not a generic parameter, or
8811 -- else parameter of an inner generic unit.
8812
8813 return A;
8814 end if;
8815 end Get_Instance_Of;
8816
8817 ---------------------------------
8818 -- Get_Unit_Instantiation_Node --
8819 ---------------------------------
8820
8821 function Get_Unit_Instantiation_Node (A : Entity_Id) return Node_Id is
8822 Decl : Node_Id := Unit_Declaration_Node (A);
8823 Inst : Node_Id;
8824
8825 begin
8826 -- If the Package_Instantiation attribute has been set on the package
8827 -- entity, then use it directly when it (or its Original_Node) refers
8828 -- to an N_Package_Instantiation node. In principle it should be
8829 -- possible to have this field set in all cases, which should be
8830 -- investigated, and would allow this function to be significantly
8831 -- simplified. ???
8832
8833 Inst := Package_Instantiation (A);
8834
8835 if Present (Inst) then
8836 if Nkind (Inst) = N_Package_Instantiation then
8837 return Inst;
8838
8839 elsif Nkind (Original_Node (Inst)) = N_Package_Instantiation then
8840 return Original_Node (Inst);
8841 end if;
8842 end if;
8843
8844 -- If the instantiation is a compilation unit that does not need body
8845 -- then the instantiation node has been rewritten as a package
8846 -- declaration for the instance, and we return the original node.
8847
8848 -- If it is a compilation unit and the instance node has not been
8849 -- rewritten, then it is still the unit of the compilation. Finally, if
8850 -- a body is present, this is a parent of the main unit whose body has
8851 -- been compiled for inlining purposes, and the instantiation node has
8852 -- been rewritten with the instance body.
8853
8854 -- Otherwise the instantiation node appears after the declaration. If
8855 -- the entity is a formal package, the declaration may have been
8856 -- rewritten as a generic declaration (in the case of a formal with box)
8857 -- or left as a formal package declaration if it has actuals, and is
8858 -- found with a forward search.
8859
8860 if Nkind (Parent (Decl)) = N_Compilation_Unit then
8861 if Nkind (Decl) = N_Package_Declaration
8862 and then Present (Corresponding_Body (Decl))
8863 then
8864 Decl := Unit_Declaration_Node (Corresponding_Body (Decl));
8865 end if;
8866
8867 if Nkind_In (Original_Node (Decl), N_Function_Instantiation,
8868 N_Package_Instantiation,
8869 N_Procedure_Instantiation)
8870 then
8871 return Original_Node (Decl);
8872 else
8873 return Unit (Parent (Decl));
8874 end if;
8875
8876 elsif Nkind (Decl) = N_Package_Declaration
8877 and then Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration
8878 then
8879 return Original_Node (Decl);
8880
8881 else
8882 Inst := Next (Decl);
8883 while not Nkind_In (Inst, N_Formal_Package_Declaration,
8884 N_Function_Instantiation,
8885 N_Package_Instantiation,
8886 N_Procedure_Instantiation)
8887 loop
8888 Next (Inst);
8889 end loop;
8890
8891 return Inst;
8892 end if;
8893 end Get_Unit_Instantiation_Node;
8894
8895 ------------------------
8896 -- Has_Been_Exchanged --
8897 ------------------------
8898
8899 function Has_Been_Exchanged (E : Entity_Id) return Boolean is
8900 Next : Elmt_Id;
8901
8902 begin
8903 Next := First_Elmt (Exchanged_Views);
8904 while Present (Next) loop
8905 if Full_View (Node (Next)) = E then
8906 return True;
8907 end if;
8908
8909 Next_Elmt (Next);
8910 end loop;
8911
8912 return False;
8913 end Has_Been_Exchanged;
8914
8915 ----------
8916 -- Hash --
8917 ----------
8918
8919 function Hash (F : Entity_Id) return HTable_Range is
8920 begin
8921 return HTable_Range (F mod HTable_Size);
8922 end Hash;
8923
8924 ------------------------
8925 -- Hide_Current_Scope --
8926 ------------------------
8927
8928 procedure Hide_Current_Scope is
8929 C : constant Entity_Id := Current_Scope;
8930 E : Entity_Id;
8931
8932 begin
8933 Set_Is_Hidden_Open_Scope (C);
8934
8935 E := First_Entity (C);
8936 while Present (E) loop
8937 if Is_Immediately_Visible (E) then
8938 Set_Is_Immediately_Visible (E, False);
8939 Append_Elmt (E, Hidden_Entities);
8940 end if;
8941
8942 Next_Entity (E);
8943 end loop;
8944
8945 -- Make the scope name invisible as well. This is necessary, but might
8946 -- conflict with calls to Rtsfind later on, in case the scope is a
8947 -- predefined one. There is no clean solution to this problem, so for
8948 -- now we depend on the user not redefining Standard itself in one of
8949 -- the parent units.
8950
8951 if Is_Immediately_Visible (C) and then C /= Standard_Standard then
8952 Set_Is_Immediately_Visible (C, False);
8953 Append_Elmt (C, Hidden_Entities);
8954 end if;
8955
8956 end Hide_Current_Scope;
8957
8958 --------------
8959 -- Init_Env --
8960 --------------
8961
8962 procedure Init_Env is
8963 Saved : Instance_Env;
8964
8965 begin
8966 Saved.Instantiated_Parent := Current_Instantiated_Parent;
8967 Saved.Exchanged_Views := Exchanged_Views;
8968 Saved.Hidden_Entities := Hidden_Entities;
8969 Saved.Current_Sem_Unit := Current_Sem_Unit;
8970 Saved.Parent_Unit_Visible := Parent_Unit_Visible;
8971 Saved.Instance_Parent_Unit := Instance_Parent_Unit;
8972
8973 -- Save configuration switches. These may be reset if the unit is a
8974 -- predefined unit, and the current mode is not Ada 2005.
8975
8976 Save_Opt_Config_Switches (Saved.Switches);
8977
8978 Instance_Envs.Append (Saved);
8979
8980 Exchanged_Views := New_Elmt_List;
8981 Hidden_Entities := New_Elmt_List;
8982
8983 -- Make dummy entry for Instantiated parent. If generic unit is legal,
8984 -- this is set properly in Set_Instance_Env.
8985
8986 Current_Instantiated_Parent :=
8987 (Current_Scope, Current_Scope, Assoc_Null);
8988 end Init_Env;
8989
8990 ------------------------------
8991 -- In_Same_Declarative_Part --
8992 ------------------------------
8993
8994 function In_Same_Declarative_Part
8995 (F_Node : Node_Id;
8996 Inst : Node_Id) return Boolean
8997 is
8998 Decls : constant Node_Id := Parent (F_Node);
8999 Nod : Node_Id;
9000
9001 begin
9002 Nod := Parent (Inst);
9003 while Present (Nod) loop
9004 if Nod = Decls then
9005 return True;
9006
9007 elsif Nkind_In (Nod, N_Subprogram_Body,
9008 N_Package_Body,
9009 N_Package_Declaration,
9010 N_Task_Body,
9011 N_Protected_Body,
9012 N_Block_Statement)
9013 then
9014 return False;
9015
9016 elsif Nkind (Nod) = N_Subunit then
9017 Nod := Corresponding_Stub (Nod);
9018
9019 elsif Nkind (Nod) = N_Compilation_Unit then
9020 return False;
9021
9022 else
9023 Nod := Parent (Nod);
9024 end if;
9025 end loop;
9026
9027 return False;
9028 end In_Same_Declarative_Part;
9029
9030 ---------------------
9031 -- In_Main_Context --
9032 ---------------------
9033
9034 function In_Main_Context (E : Entity_Id) return Boolean is
9035 Context : List_Id;
9036 Clause : Node_Id;
9037 Nam : Node_Id;
9038
9039 begin
9040 if not Is_Compilation_Unit (E)
9041 or else Ekind (E) /= E_Package
9042 or else In_Private_Part (E)
9043 then
9044 return False;
9045 end if;
9046
9047 Context := Context_Items (Cunit (Main_Unit));
9048
9049 Clause := First (Context);
9050 while Present (Clause) loop
9051 if Nkind (Clause) = N_With_Clause then
9052 Nam := Name (Clause);
9053
9054 -- If the current scope is part of the context of the main unit,
9055 -- analysis of the corresponding with_clause is not complete, and
9056 -- the entity is not set. We use the Chars field directly, which
9057 -- might produce false positives in rare cases, but guarantees
9058 -- that we produce all the instance bodies we will need.
9059
9060 if (Is_Entity_Name (Nam) and then Chars (Nam) = Chars (E))
9061 or else (Nkind (Nam) = N_Selected_Component
9062 and then Chars (Selector_Name (Nam)) = Chars (E))
9063 then
9064 return True;
9065 end if;
9066 end if;
9067
9068 Next (Clause);
9069 end loop;
9070
9071 return False;
9072 end In_Main_Context;
9073
9074 ---------------------
9075 -- Inherit_Context --
9076 ---------------------
9077
9078 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id) is
9079 Current_Context : List_Id;
9080 Current_Unit : Node_Id;
9081 Item : Node_Id;
9082 New_I : Node_Id;
9083
9084 Clause : Node_Id;
9085 OK : Boolean;
9086 Lib_Unit : Node_Id;
9087
9088 begin
9089 if Nkind (Parent (Gen_Decl)) = N_Compilation_Unit then
9090
9091 -- The inherited context is attached to the enclosing compilation
9092 -- unit. This is either the main unit, or the declaration for the
9093 -- main unit (in case the instantiation appears within the package
9094 -- declaration and the main unit is its body).
9095
9096 Current_Unit := Parent (Inst);
9097 while Present (Current_Unit)
9098 and then Nkind (Current_Unit) /= N_Compilation_Unit
9099 loop
9100 Current_Unit := Parent (Current_Unit);
9101 end loop;
9102
9103 Current_Context := Context_Items (Current_Unit);
9104
9105 Item := First (Context_Items (Parent (Gen_Decl)));
9106 while Present (Item) loop
9107 if Nkind (Item) = N_With_Clause then
9108 Lib_Unit := Library_Unit (Item);
9109
9110 -- Take care to prevent direct cyclic with's
9111
9112 if Lib_Unit /= Current_Unit then
9113
9114 -- Do not add a unit if it is already in the context
9115
9116 Clause := First (Current_Context);
9117 OK := True;
9118 while Present (Clause) loop
9119 if Nkind (Clause) = N_With_Clause
9120 and then Library_Unit (Clause) = Lib_Unit
9121 then
9122 OK := False;
9123 exit;
9124 end if;
9125
9126 Next (Clause);
9127 end loop;
9128
9129 if OK then
9130 New_I := New_Copy (Item);
9131 Set_Implicit_With (New_I);
9132
9133 Append (New_I, Current_Context);
9134 end if;
9135 end if;
9136 end if;
9137
9138 Next (Item);
9139 end loop;
9140 end if;
9141 end Inherit_Context;
9142
9143 ----------------
9144 -- Initialize --
9145 ----------------
9146
9147 procedure Initialize is
9148 begin
9149 Generic_Renamings.Init;
9150 Instance_Envs.Init;
9151 Generic_Flags.Init;
9152 Generic_Renamings_HTable.Reset;
9153 Circularity_Detected := False;
9154 Exchanged_Views := No_Elist;
9155 Hidden_Entities := No_Elist;
9156 end Initialize;
9157
9158 -------------------------------------
9159 -- Insert_Freeze_Node_For_Instance --
9160 -------------------------------------
9161
9162 procedure Insert_Freeze_Node_For_Instance
9163 (N : Node_Id;
9164 F_Node : Node_Id)
9165 is
9166 Decl : Node_Id;
9167 Decls : List_Id;
9168 Inst : Entity_Id;
9169 Par_N : Node_Id;
9170
9171 function Enclosing_Body (N : Node_Id) return Node_Id;
9172 -- Find enclosing package or subprogram body, if any. Freeze node may
9173 -- be placed at end of current declarative list if previous instance
9174 -- and current one have different enclosing bodies.
9175
9176 function Previous_Instance (Gen : Entity_Id) return Entity_Id;
9177 -- Find the local instance, if any, that declares the generic that is
9178 -- being instantiated. If present, the freeze node for this instance
9179 -- must follow the freeze node for the previous instance.
9180
9181 --------------------
9182 -- Enclosing_Body --
9183 --------------------
9184
9185 function Enclosing_Body (N : Node_Id) return Node_Id is
9186 P : Node_Id;
9187
9188 begin
9189 P := Parent (N);
9190 while Present (P)
9191 and then Nkind (Parent (P)) /= N_Compilation_Unit
9192 loop
9193 if Nkind_In (P, N_Package_Body, N_Subprogram_Body) then
9194 if Nkind (Parent (P)) = N_Subunit then
9195 return Corresponding_Stub (Parent (P));
9196 else
9197 return P;
9198 end if;
9199 end if;
9200
9201 P := True_Parent (P);
9202 end loop;
9203
9204 return Empty;
9205 end Enclosing_Body;
9206
9207 -----------------------
9208 -- Previous_Instance --
9209 -----------------------
9210
9211 function Previous_Instance (Gen : Entity_Id) return Entity_Id is
9212 S : Entity_Id;
9213
9214 begin
9215 S := Scope (Gen);
9216 while Present (S) and then S /= Standard_Standard loop
9217 if Is_Generic_Instance (S)
9218 and then In_Same_Source_Unit (S, N)
9219 then
9220 return S;
9221 end if;
9222
9223 S := Scope (S);
9224 end loop;
9225
9226 return Empty;
9227 end Previous_Instance;
9228
9229 -- Start of processing for Insert_Freeze_Node_For_Instance
9230
9231 begin
9232 if not Is_List_Member (F_Node) then
9233 Decl := N;
9234 Decls := List_Containing (N);
9235 Inst := Entity (F_Node);
9236 Par_N := Parent (Decls);
9237
9238 -- When processing a subprogram instantiation, utilize the actual
9239 -- subprogram instantiation rather than its package wrapper as it
9240 -- carries all the context information.
9241
9242 if Is_Wrapper_Package (Inst) then
9243 Inst := Related_Instance (Inst);
9244 end if;
9245
9246 -- If this is a package instance, check whether the generic is
9247 -- declared in a previous instance and the current instance is
9248 -- not within the previous one.
9249
9250 if Present (Generic_Parent (Parent (Inst)))
9251 and then Is_In_Main_Unit (N)
9252 then
9253 declare
9254 Enclosing_N : constant Node_Id := Enclosing_Body (N);
9255 Par_I : constant Entity_Id :=
9256 Previous_Instance
9257 (Generic_Parent (Parent (Inst)));
9258 Scop : Entity_Id;
9259
9260 begin
9261 if Present (Par_I)
9262 and then Earlier (N, Freeze_Node (Par_I))
9263 then
9264 Scop := Scope (Inst);
9265
9266 -- If the current instance is within the one that contains
9267 -- the generic, the freeze node for the current one must
9268 -- appear in the current declarative part. Ditto, if the
9269 -- current instance is within another package instance or
9270 -- within a body that does not enclose the current instance.
9271 -- In these three cases the freeze node of the previous
9272 -- instance is not relevant.
9273
9274 while Present (Scop) and then Scop /= Standard_Standard loop
9275 exit when Scop = Par_I
9276 or else
9277 (Is_Generic_Instance (Scop)
9278 and then Scope_Depth (Scop) > Scope_Depth (Par_I));
9279 Scop := Scope (Scop);
9280 end loop;
9281
9282 -- Previous instance encloses current instance
9283
9284 if Scop = Par_I then
9285 null;
9286
9287 -- If the next node is a source body we must freeze in
9288 -- the current scope as well.
9289
9290 elsif Present (Next (N))
9291 and then Nkind_In (Next (N), N_Subprogram_Body,
9292 N_Package_Body)
9293 and then Comes_From_Source (Next (N))
9294 then
9295 null;
9296
9297 -- Current instance is within an unrelated instance
9298
9299 elsif Is_Generic_Instance (Scop) then
9300 null;
9301
9302 -- Current instance is within an unrelated body
9303
9304 elsif Present (Enclosing_N)
9305 and then Enclosing_N /= Enclosing_Body (Par_I)
9306 then
9307 null;
9308
9309 else
9310 Insert_After (Freeze_Node (Par_I), F_Node);
9311 return;
9312 end if;
9313 end if;
9314 end;
9315 end if;
9316
9317 -- When the instantiation occurs in a package declaration, append the
9318 -- freeze node to the private declarations (if any).
9319
9320 if Nkind (Par_N) = N_Package_Specification
9321 and then Decls = Visible_Declarations (Par_N)
9322 and then Present (Private_Declarations (Par_N))
9323 and then not Is_Empty_List (Private_Declarations (Par_N))
9324 then
9325 Decls := Private_Declarations (Par_N);
9326 Decl := First (Decls);
9327 end if;
9328
9329 -- Determine the proper freeze point of a package instantiation. We
9330 -- adhere to the general rule of a package or subprogram body causing
9331 -- freezing of anything before it in the same declarative region. In
9332 -- this case, the proper freeze point of a package instantiation is
9333 -- before the first source body which follows, or before a stub. This
9334 -- ensures that entities coming from the instance are already frozen
9335 -- and usable in source bodies.
9336
9337 if Nkind (Par_N) /= N_Package_Declaration
9338 and then Ekind (Inst) = E_Package
9339 and then Is_Generic_Instance (Inst)
9340 and then
9341 not In_Same_Source_Unit (Generic_Parent (Parent (Inst)), Inst)
9342 then
9343 while Present (Decl) loop
9344 if (Nkind (Decl) in N_Unit_Body
9345 or else
9346 Nkind (Decl) in N_Body_Stub)
9347 and then Comes_From_Source (Decl)
9348 then
9349 Insert_Before (Decl, F_Node);
9350 return;
9351 end if;
9352
9353 Next (Decl);
9354 end loop;
9355 end if;
9356
9357 -- In a package declaration, or if no previous body, insert at end
9358 -- of list.
9359
9360 Set_Sloc (F_Node, Sloc (Last (Decls)));
9361 Insert_After (Last (Decls), F_Node);
9362 end if;
9363 end Insert_Freeze_Node_For_Instance;
9364
9365 ------------------
9366 -- Install_Body --
9367 ------------------
9368
9369 procedure Install_Body
9370 (Act_Body : Node_Id;
9371 N : Node_Id;
9372 Gen_Body : Node_Id;
9373 Gen_Decl : Node_Id)
9374 is
9375 function In_Same_Scope (Gen_Id, Act_Id : Node_Id) return Boolean;
9376 -- Check if the generic definition and the instantiation come from
9377 -- a common scope, in which case the instance must be frozen after
9378 -- the generic body.
9379
9380 function True_Sloc (N, Act_Unit : Node_Id) return Source_Ptr;
9381 -- If the instance is nested inside a generic unit, the Sloc of the
9382 -- instance indicates the place of the original definition, not the
9383 -- point of the current enclosing instance. Pending a better usage of
9384 -- Slocs to indicate instantiation places, we determine the place of
9385 -- origin of a node by finding the maximum sloc of any ancestor node.
9386 -- Why is this not equivalent to Top_Level_Location ???
9387
9388 -------------------
9389 -- In_Same_Scope --
9390 -------------------
9391
9392 function In_Same_Scope (Gen_Id, Act_Id : Node_Id) return Boolean is
9393 Act_Scop : Entity_Id := Scope (Act_Id);
9394 Gen_Scop : Entity_Id := Scope (Gen_Id);
9395
9396 begin
9397 while Act_Scop /= Standard_Standard
9398 and then Gen_Scop /= Standard_Standard
9399 loop
9400 if Act_Scop = Gen_Scop then
9401 return True;
9402 end if;
9403
9404 Act_Scop := Scope (Act_Scop);
9405 Gen_Scop := Scope (Gen_Scop);
9406 end loop;
9407
9408 return False;
9409 end In_Same_Scope;
9410
9411 ---------------
9412 -- True_Sloc --
9413 ---------------
9414
9415 function True_Sloc (N, Act_Unit : Node_Id) return Source_Ptr is
9416 N1 : Node_Id;
9417 Res : Source_Ptr;
9418
9419 begin
9420 Res := Sloc (N);
9421 N1 := N;
9422 while Present (N1) and then N1 /= Act_Unit loop
9423 if Sloc (N1) > Res then
9424 Res := Sloc (N1);
9425 end if;
9426
9427 N1 := Parent (N1);
9428 end loop;
9429
9430 return Res;
9431 end True_Sloc;
9432
9433 Act_Id : constant Entity_Id := Corresponding_Spec (Act_Body);
9434 Act_Unit : constant Node_Id := Unit (Cunit (Get_Source_Unit (N)));
9435 Gen_Id : constant Entity_Id := Corresponding_Spec (Gen_Body);
9436 Par : constant Entity_Id := Scope (Gen_Id);
9437 Gen_Unit : constant Node_Id :=
9438 Unit (Cunit (Get_Source_Unit (Gen_Decl)));
9439
9440 Body_Unit : Node_Id;
9441 F_Node : Node_Id;
9442 Must_Delay : Boolean;
9443 Orig_Body : Node_Id := Gen_Body;
9444
9445 -- Start of processing for Install_Body
9446
9447 begin
9448 -- Handle first the case of an instance with incomplete actual types.
9449 -- The instance body cannot be placed after the declaration because
9450 -- full views have not been seen yet. Any use of the non-limited views
9451 -- in the instance body requires the presence of a regular with_clause
9452 -- in the enclosing unit, and will fail if this with_clause is missing.
9453 -- We place the instance body at the beginning of the enclosing body,
9454 -- which is the unit being compiled. The freeze node for the instance
9455 -- is then placed after the instance body.
9456
9457 if not Is_Empty_Elmt_List (Incomplete_Actuals (Act_Id))
9458 and then Expander_Active
9459 and then Ekind (Scope (Act_Id)) = E_Package
9460 then
9461 declare
9462 Scop : constant Entity_Id := Scope (Act_Id);
9463 Body_Id : constant Node_Id :=
9464 Corresponding_Body (Unit_Declaration_Node (Scop));
9465
9466 begin
9467 Ensure_Freeze_Node (Act_Id);
9468 F_Node := Freeze_Node (Act_Id);
9469 if Present (Body_Id) then
9470 Set_Is_Frozen (Act_Id, False);
9471 Prepend (Act_Body, Declarations (Parent (Body_Id)));
9472 if Is_List_Member (F_Node) then
9473 Remove (F_Node);
9474 end if;
9475
9476 Insert_After (Act_Body, F_Node);
9477 end if;
9478 end;
9479 return;
9480 end if;
9481
9482 -- If the body is a subunit, the freeze point is the corresponding stub
9483 -- in the current compilation, not the subunit itself.
9484
9485 if Nkind (Parent (Gen_Body)) = N_Subunit then
9486 Orig_Body := Corresponding_Stub (Parent (Gen_Body));
9487 else
9488 Orig_Body := Gen_Body;
9489 end if;
9490
9491 Body_Unit := Unit (Cunit (Get_Source_Unit (Orig_Body)));
9492
9493 -- If the instantiation and the generic definition appear in the same
9494 -- package declaration, this is an early instantiation. If they appear
9495 -- in the same declarative part, it is an early instantiation only if
9496 -- the generic body appears textually later, and the generic body is
9497 -- also in the main unit.
9498
9499 -- If instance is nested within a subprogram, and the generic body
9500 -- is not, the instance is delayed because the enclosing body is. If
9501 -- instance and body are within the same scope, or the same subprogram
9502 -- body, indicate explicitly that the instance is delayed.
9503
9504 Must_Delay :=
9505 (Gen_Unit = Act_Unit
9506 and then (Nkind_In (Gen_Unit, N_Generic_Package_Declaration,
9507 N_Package_Declaration)
9508 or else (Gen_Unit = Body_Unit
9509 and then True_Sloc (N, Act_Unit) <
9510 Sloc (Orig_Body)))
9511 and then Is_In_Main_Unit (Original_Node (Gen_Unit))
9512 and then In_Same_Scope (Gen_Id, Act_Id));
9513
9514 -- If this is an early instantiation, the freeze node is placed after
9515 -- the generic body. Otherwise, if the generic appears in an instance,
9516 -- we cannot freeze the current instance until the outer one is frozen.
9517 -- This is only relevant if the current instance is nested within some
9518 -- inner scope not itself within the outer instance. If this scope is
9519 -- a package body in the same declarative part as the outer instance,
9520 -- then that body needs to be frozen after the outer instance. Finally,
9521 -- if no delay is needed, we place the freeze node at the end of the
9522 -- current declarative part.
9523
9524 if Expander_Active
9525 and then (No (Freeze_Node (Act_Id))
9526 or else not Is_List_Member (Freeze_Node (Act_Id)))
9527 then
9528 Ensure_Freeze_Node (Act_Id);
9529 F_Node := Freeze_Node (Act_Id);
9530
9531 if Must_Delay then
9532 Insert_After (Orig_Body, F_Node);
9533
9534 elsif Is_Generic_Instance (Par)
9535 and then Present (Freeze_Node (Par))
9536 and then Scope (Act_Id) /= Par
9537 then
9538 -- Freeze instance of inner generic after instance of enclosing
9539 -- generic.
9540
9541 if In_Same_Declarative_Part (Freeze_Node (Par), N) then
9542
9543 -- Handle the following case:
9544
9545 -- package Parent_Inst is new ...
9546 -- Parent_Inst []
9547
9548 -- procedure P ... -- this body freezes Parent_Inst
9549
9550 -- package Inst is new ...
9551
9552 -- In this particular scenario, the freeze node for Inst must
9553 -- be inserted in the same manner as that of Parent_Inst,
9554 -- before the next source body or at the end of the declarative
9555 -- list (body not available). If body P did not exist and
9556 -- Parent_Inst was frozen after Inst, either by a body
9557 -- following Inst or at the end of the declarative region,
9558 -- the freeze node for Inst must be inserted after that of
9559 -- Parent_Inst. This relation is established by comparing
9560 -- the Slocs of Parent_Inst freeze node and Inst.
9561
9562 if List_Containing (Get_Unit_Instantiation_Node (Par)) =
9563 List_Containing (N)
9564 and then Sloc (Freeze_Node (Par)) < Sloc (N)
9565 then
9566 Insert_Freeze_Node_For_Instance (N, F_Node);
9567 else
9568 Insert_After (Freeze_Node (Par), F_Node);
9569 end if;
9570
9571 -- Freeze package enclosing instance of inner generic after
9572 -- instance of enclosing generic.
9573
9574 elsif Nkind_In (Parent (N), N_Package_Body, N_Subprogram_Body)
9575 and then In_Same_Declarative_Part (Freeze_Node (Par), Parent (N))
9576 then
9577 declare
9578 Enclosing : Entity_Id;
9579
9580 begin
9581 Enclosing := Corresponding_Spec (Parent (N));
9582
9583 if No (Enclosing) then
9584 Enclosing := Defining_Entity (Parent (N));
9585 end if;
9586
9587 Insert_Freeze_Node_For_Instance (N, F_Node);
9588 Ensure_Freeze_Node (Enclosing);
9589
9590 if not Is_List_Member (Freeze_Node (Enclosing)) then
9591
9592 -- The enclosing context is a subunit, insert the freeze
9593 -- node after the stub.
9594
9595 if Nkind (Parent (Parent (N))) = N_Subunit then
9596 Insert_Freeze_Node_For_Instance
9597 (Corresponding_Stub (Parent (Parent (N))),
9598 Freeze_Node (Enclosing));
9599
9600 -- The enclosing context is a package with a stub body
9601 -- which has already been replaced by the real body.
9602 -- Insert the freeze node after the actual body.
9603
9604 elsif Ekind (Enclosing) = E_Package
9605 and then Present (Body_Entity (Enclosing))
9606 and then Was_Originally_Stub
9607 (Parent (Body_Entity (Enclosing)))
9608 then
9609 Insert_Freeze_Node_For_Instance
9610 (Parent (Body_Entity (Enclosing)),
9611 Freeze_Node (Enclosing));
9612
9613 -- The parent instance has been frozen before the body of
9614 -- the enclosing package, insert the freeze node after
9615 -- the body.
9616
9617 elsif List_Containing (Freeze_Node (Par)) =
9618 List_Containing (Parent (N))
9619 and then Sloc (Freeze_Node (Par)) < Sloc (Parent (N))
9620 then
9621 Insert_Freeze_Node_For_Instance
9622 (Parent (N), Freeze_Node (Enclosing));
9623
9624 else
9625 Insert_After
9626 (Freeze_Node (Par), Freeze_Node (Enclosing));
9627 end if;
9628 end if;
9629 end;
9630
9631 else
9632 Insert_Freeze_Node_For_Instance (N, F_Node);
9633 end if;
9634
9635 else
9636 Insert_Freeze_Node_For_Instance (N, F_Node);
9637 end if;
9638 end if;
9639
9640 Set_Is_Frozen (Act_Id);
9641 Insert_Before (N, Act_Body);
9642 Mark_Rewrite_Insertion (Act_Body);
9643 end Install_Body;
9644
9645 -----------------------------
9646 -- Install_Formal_Packages --
9647 -----------------------------
9648
9649 procedure Install_Formal_Packages (Par : Entity_Id) is
9650 E : Entity_Id;
9651 Gen : Entity_Id;
9652 Gen_E : Entity_Id := Empty;
9653
9654 begin
9655 E := First_Entity (Par);
9656
9657 -- If we are installing an instance parent, locate the formal packages
9658 -- of its generic parent.
9659
9660 if Is_Generic_Instance (Par) then
9661 Gen := Generic_Parent (Package_Specification (Par));
9662 Gen_E := First_Entity (Gen);
9663 end if;
9664
9665 while Present (E) loop
9666 if Ekind (E) = E_Package
9667 and then Nkind (Parent (E)) = N_Package_Renaming_Declaration
9668 then
9669 -- If this is the renaming for the parent instance, done
9670
9671 if Renamed_Object (E) = Par then
9672 exit;
9673
9674 -- The visibility of a formal of an enclosing generic is already
9675 -- correct.
9676
9677 elsif Denotes_Formal_Package (E) then
9678 null;
9679
9680 elsif Present (Associated_Formal_Package (E)) then
9681 Check_Generic_Actuals (Renamed_Object (E), True);
9682 Set_Is_Hidden (E, False);
9683
9684 -- Find formal package in generic unit that corresponds to
9685 -- (instance of) formal package in instance.
9686
9687 while Present (Gen_E) and then Chars (Gen_E) /= Chars (E) loop
9688 Next_Entity (Gen_E);
9689 end loop;
9690
9691 if Present (Gen_E) then
9692 Map_Formal_Package_Entities (Gen_E, E);
9693 end if;
9694 end if;
9695 end if;
9696
9697 Next_Entity (E);
9698
9699 if Present (Gen_E) then
9700 Next_Entity (Gen_E);
9701 end if;
9702 end loop;
9703 end Install_Formal_Packages;
9704
9705 --------------------
9706 -- Install_Parent --
9707 --------------------
9708
9709 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False) is
9710 Ancestors : constant Elist_Id := New_Elmt_List;
9711 S : constant Entity_Id := Current_Scope;
9712 Inst_Par : Entity_Id;
9713 First_Par : Entity_Id;
9714 Inst_Node : Node_Id;
9715 Gen_Par : Entity_Id;
9716 First_Gen : Entity_Id;
9717 Elmt : Elmt_Id;
9718
9719 procedure Install_Noninstance_Specs (Par : Entity_Id);
9720 -- Install the scopes of noninstance parent units ending with Par
9721
9722 procedure Install_Spec (Par : Entity_Id);
9723 -- The child unit is within the declarative part of the parent, so the
9724 -- declarations within the parent are immediately visible.
9725
9726 -------------------------------
9727 -- Install_Noninstance_Specs --
9728 -------------------------------
9729
9730 procedure Install_Noninstance_Specs (Par : Entity_Id) is
9731 begin
9732 if Present (Par)
9733 and then Par /= Standard_Standard
9734 and then not In_Open_Scopes (Par)
9735 then
9736 Install_Noninstance_Specs (Scope (Par));
9737 Install_Spec (Par);
9738 end if;
9739 end Install_Noninstance_Specs;
9740
9741 ------------------
9742 -- Install_Spec --
9743 ------------------
9744
9745 procedure Install_Spec (Par : Entity_Id) is
9746 Spec : constant Node_Id := Package_Specification (Par);
9747
9748 begin
9749 -- If this parent of the child instance is a top-level unit,
9750 -- then record the unit and its visibility for later resetting in
9751 -- Remove_Parent. We exclude units that are generic instances, as we
9752 -- only want to record this information for the ultimate top-level
9753 -- noninstance parent (is that always correct???).
9754
9755 if Scope (Par) = Standard_Standard
9756 and then not Is_Generic_Instance (Par)
9757 then
9758 Parent_Unit_Visible := Is_Immediately_Visible (Par);
9759 Instance_Parent_Unit := Par;
9760 end if;
9761
9762 -- Open the parent scope and make it and its declarations visible.
9763 -- If this point is not within a body, then only the visible
9764 -- declarations should be made visible, and installation of the
9765 -- private declarations is deferred until the appropriate point
9766 -- within analysis of the spec being instantiated (see the handling
9767 -- of parent visibility in Analyze_Package_Specification). This is
9768 -- relaxed in the case where the parent unit is Ada.Tags, to avoid
9769 -- private view problems that occur when compiling instantiations of
9770 -- a generic child of that package (Generic_Dispatching_Constructor).
9771 -- If the instance freezes a tagged type, inlinings of operations
9772 -- from Ada.Tags may need the full view of type Tag. If inlining took
9773 -- proper account of establishing visibility of inlined subprograms'
9774 -- parents then it should be possible to remove this
9775 -- special check. ???
9776
9777 Push_Scope (Par);
9778 Set_Is_Immediately_Visible (Par);
9779 Install_Visible_Declarations (Par);
9780 Set_Use (Visible_Declarations (Spec));
9781
9782 if In_Body or else Is_RTU (Par, Ada_Tags) then
9783 Install_Private_Declarations (Par);
9784 Set_Use (Private_Declarations (Spec));
9785 end if;
9786 end Install_Spec;
9787
9788 -- Start of processing for Install_Parent
9789
9790 begin
9791 -- We need to install the parent instance to compile the instantiation
9792 -- of the child, but the child instance must appear in the current
9793 -- scope. Given that we cannot place the parent above the current scope
9794 -- in the scope stack, we duplicate the current scope and unstack both
9795 -- after the instantiation is complete.
9796
9797 -- If the parent is itself the instantiation of a child unit, we must
9798 -- also stack the instantiation of its parent, and so on. Each such
9799 -- ancestor is the prefix of the name in a prior instantiation.
9800
9801 -- If this is a nested instance, the parent unit itself resolves to
9802 -- a renaming of the parent instance, whose declaration we need.
9803
9804 -- Finally, the parent may be a generic (not an instance) when the
9805 -- child unit appears as a formal package.
9806
9807 Inst_Par := P;
9808
9809 if Present (Renamed_Entity (Inst_Par)) then
9810 Inst_Par := Renamed_Entity (Inst_Par);
9811 end if;
9812
9813 First_Par := Inst_Par;
9814
9815 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
9816
9817 First_Gen := Gen_Par;
9818
9819 while Present (Gen_Par) and then Is_Child_Unit (Gen_Par) loop
9820
9821 -- Load grandparent instance as well
9822
9823 Inst_Node := Get_Unit_Instantiation_Node (Inst_Par);
9824
9825 if Nkind (Name (Inst_Node)) = N_Expanded_Name then
9826 Inst_Par := Entity (Prefix (Name (Inst_Node)));
9827
9828 if Present (Renamed_Entity (Inst_Par)) then
9829 Inst_Par := Renamed_Entity (Inst_Par);
9830 end if;
9831
9832 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
9833
9834 if Present (Gen_Par) then
9835 Prepend_Elmt (Inst_Par, Ancestors);
9836
9837 else
9838 -- Parent is not the name of an instantiation
9839
9840 Install_Noninstance_Specs (Inst_Par);
9841 exit;
9842 end if;
9843
9844 else
9845 -- Previous error
9846
9847 exit;
9848 end if;
9849 end loop;
9850
9851 if Present (First_Gen) then
9852 Append_Elmt (First_Par, Ancestors);
9853 else
9854 Install_Noninstance_Specs (First_Par);
9855 end if;
9856
9857 if not Is_Empty_Elmt_List (Ancestors) then
9858 Elmt := First_Elmt (Ancestors);
9859 while Present (Elmt) loop
9860 Install_Spec (Node (Elmt));
9861 Install_Formal_Packages (Node (Elmt));
9862 Next_Elmt (Elmt);
9863 end loop;
9864 end if;
9865
9866 if not In_Body then
9867 Push_Scope (S);
9868 end if;
9869 end Install_Parent;
9870
9871 -------------------------------
9872 -- Install_Hidden_Primitives --
9873 -------------------------------
9874
9875 procedure Install_Hidden_Primitives
9876 (Prims_List : in out Elist_Id;
9877 Gen_T : Entity_Id;
9878 Act_T : Entity_Id)
9879 is
9880 Elmt : Elmt_Id;
9881 List : Elist_Id := No_Elist;
9882 Prim_G_Elmt : Elmt_Id;
9883 Prim_A_Elmt : Elmt_Id;
9884 Prim_G : Node_Id;
9885 Prim_A : Node_Id;
9886
9887 begin
9888 -- No action needed in case of serious errors because we cannot trust
9889 -- in the order of primitives
9890
9891 if Serious_Errors_Detected > 0 then
9892 return;
9893
9894 -- No action possible if we don't have available the list of primitive
9895 -- operations
9896
9897 elsif No (Gen_T)
9898 or else not Is_Record_Type (Gen_T)
9899 or else not Is_Tagged_Type (Gen_T)
9900 or else not Is_Record_Type (Act_T)
9901 or else not Is_Tagged_Type (Act_T)
9902 then
9903 return;
9904
9905 -- There is no need to handle interface types since their primitives
9906 -- cannot be hidden
9907
9908 elsif Is_Interface (Gen_T) then
9909 return;
9910 end if;
9911
9912 Prim_G_Elmt := First_Elmt (Primitive_Operations (Gen_T));
9913
9914 if not Is_Class_Wide_Type (Act_T) then
9915 Prim_A_Elmt := First_Elmt (Primitive_Operations (Act_T));
9916 else
9917 Prim_A_Elmt := First_Elmt (Primitive_Operations (Root_Type (Act_T)));
9918 end if;
9919
9920 loop
9921 -- Skip predefined primitives in the generic formal
9922
9923 while Present (Prim_G_Elmt)
9924 and then Is_Predefined_Dispatching_Operation (Node (Prim_G_Elmt))
9925 loop
9926 Next_Elmt (Prim_G_Elmt);
9927 end loop;
9928
9929 -- Skip predefined primitives in the generic actual
9930
9931 while Present (Prim_A_Elmt)
9932 and then Is_Predefined_Dispatching_Operation (Node (Prim_A_Elmt))
9933 loop
9934 Next_Elmt (Prim_A_Elmt);
9935 end loop;
9936
9937 exit when No (Prim_G_Elmt) or else No (Prim_A_Elmt);
9938
9939 Prim_G := Node (Prim_G_Elmt);
9940 Prim_A := Node (Prim_A_Elmt);
9941
9942 -- There is no need to handle interface primitives because their
9943 -- primitives are not hidden
9944
9945 exit when Present (Interface_Alias (Prim_G));
9946
9947 -- Here we install one hidden primitive
9948
9949 if Chars (Prim_G) /= Chars (Prim_A)
9950 and then Has_Suffix (Prim_A, 'P')
9951 and then Remove_Suffix (Prim_A, 'P') = Chars (Prim_G)
9952 then
9953 Set_Chars (Prim_A, Chars (Prim_G));
9954 Append_New_Elmt (Prim_A, To => List);
9955 end if;
9956
9957 Next_Elmt (Prim_A_Elmt);
9958 Next_Elmt (Prim_G_Elmt);
9959 end loop;
9960
9961 -- Append the elements to the list of temporarily visible primitives
9962 -- avoiding duplicates.
9963
9964 if Present (List) then
9965 if No (Prims_List) then
9966 Prims_List := New_Elmt_List;
9967 end if;
9968
9969 Elmt := First_Elmt (List);
9970 while Present (Elmt) loop
9971 Append_Unique_Elmt (Node (Elmt), Prims_List);
9972 Next_Elmt (Elmt);
9973 end loop;
9974 end if;
9975 end Install_Hidden_Primitives;
9976
9977 -------------------------------
9978 -- Restore_Hidden_Primitives --
9979 -------------------------------
9980
9981 procedure Restore_Hidden_Primitives (Prims_List : in out Elist_Id) is
9982 Prim_Elmt : Elmt_Id;
9983 Prim : Node_Id;
9984
9985 begin
9986 if Prims_List /= No_Elist then
9987 Prim_Elmt := First_Elmt (Prims_List);
9988 while Present (Prim_Elmt) loop
9989 Prim := Node (Prim_Elmt);
9990 Set_Chars (Prim, Add_Suffix (Prim, 'P'));
9991 Next_Elmt (Prim_Elmt);
9992 end loop;
9993
9994 Prims_List := No_Elist;
9995 end if;
9996 end Restore_Hidden_Primitives;
9997
9998 --------------------------------
9999 -- Instantiate_Formal_Package --
10000 --------------------------------
10001
10002 function Instantiate_Formal_Package
10003 (Formal : Node_Id;
10004 Actual : Node_Id;
10005 Analyzed_Formal : Node_Id) return List_Id
10006 is
10007 Loc : constant Source_Ptr := Sloc (Actual);
10008 Actual_Pack : Entity_Id;
10009 Formal_Pack : Entity_Id;
10010 Gen_Parent : Entity_Id;
10011 Decls : List_Id;
10012 Nod : Node_Id;
10013 Parent_Spec : Node_Id;
10014
10015 procedure Find_Matching_Actual
10016 (F : Node_Id;
10017 Act : in out Entity_Id);
10018 -- We need to associate each formal entity in the formal package with
10019 -- the corresponding entity in the actual package. The actual package
10020 -- has been analyzed and possibly expanded, and as a result there is
10021 -- no one-to-one correspondence between the two lists (for example,
10022 -- the actual may include subtypes, itypes, and inherited primitive
10023 -- operations, interspersed among the renaming declarations for the
10024 -- actuals). We retrieve the corresponding actual by name because each
10025 -- actual has the same name as the formal, and they do appear in the
10026 -- same order.
10027
10028 function Get_Formal_Entity (N : Node_Id) return Entity_Id;
10029 -- Retrieve entity of defining entity of generic formal parameter.
10030 -- Only the declarations of formals need to be considered when
10031 -- linking them to actuals, but the declarative list may include
10032 -- internal entities generated during analysis, and those are ignored.
10033
10034 procedure Match_Formal_Entity
10035 (Formal_Node : Node_Id;
10036 Formal_Ent : Entity_Id;
10037 Actual_Ent : Entity_Id);
10038 -- Associates the formal entity with the actual. In the case where
10039 -- Formal_Ent is a formal package, this procedure iterates through all
10040 -- of its formals and enters associations between the actuals occurring
10041 -- in the formal package's corresponding actual package (given by
10042 -- Actual_Ent) and the formal package's formal parameters. This
10043 -- procedure recurses if any of the parameters is itself a package.
10044
10045 function Is_Instance_Of
10046 (Act_Spec : Entity_Id;
10047 Gen_Anc : Entity_Id) return Boolean;
10048 -- The actual can be an instantiation of a generic within another
10049 -- instance, in which case there is no direct link from it to the
10050 -- original generic ancestor. In that case, we recognize that the
10051 -- ultimate ancestor is the same by examining names and scopes.
10052
10053 procedure Process_Nested_Formal (Formal : Entity_Id);
10054 -- If the current formal is declared with a box, its own formals are
10055 -- visible in the instance, as they were in the generic, and their
10056 -- Hidden flag must be reset. If some of these formals are themselves
10057 -- packages declared with a box, the processing must be recursive.
10058
10059 --------------------------
10060 -- Find_Matching_Actual --
10061 --------------------------
10062
10063 procedure Find_Matching_Actual
10064 (F : Node_Id;
10065 Act : in out Entity_Id)
10066 is
10067 Formal_Ent : Entity_Id;
10068
10069 begin
10070 case Nkind (Original_Node (F)) is
10071 when N_Formal_Object_Declaration
10072 | N_Formal_Type_Declaration
10073 =>
10074 Formal_Ent := Defining_Identifier (F);
10075
10076 while Chars (Act) /= Chars (Formal_Ent) loop
10077 Next_Entity (Act);
10078 end loop;
10079
10080 when N_Formal_Package_Declaration
10081 | N_Formal_Subprogram_Declaration
10082 | N_Generic_Package_Declaration
10083 | N_Package_Declaration
10084 =>
10085 Formal_Ent := Defining_Entity (F);
10086
10087 while Chars (Act) /= Chars (Formal_Ent) loop
10088 Next_Entity (Act);
10089 end loop;
10090
10091 when others =>
10092 raise Program_Error;
10093 end case;
10094 end Find_Matching_Actual;
10095
10096 -------------------------
10097 -- Match_Formal_Entity --
10098 -------------------------
10099
10100 procedure Match_Formal_Entity
10101 (Formal_Node : Node_Id;
10102 Formal_Ent : Entity_Id;
10103 Actual_Ent : Entity_Id)
10104 is
10105 Act_Pkg : Entity_Id;
10106
10107 begin
10108 Set_Instance_Of (Formal_Ent, Actual_Ent);
10109
10110 if Ekind (Actual_Ent) = E_Package then
10111
10112 -- Record associations for each parameter
10113
10114 Act_Pkg := Actual_Ent;
10115
10116 declare
10117 A_Ent : Entity_Id := First_Entity (Act_Pkg);
10118 F_Ent : Entity_Id;
10119 F_Node : Node_Id;
10120
10121 Gen_Decl : Node_Id;
10122 Formals : List_Id;
10123 Actual : Entity_Id;
10124
10125 begin
10126 -- Retrieve the actual given in the formal package declaration
10127
10128 Actual := Entity (Name (Original_Node (Formal_Node)));
10129
10130 -- The actual in the formal package declaration may be a
10131 -- renamed generic package, in which case we want to retrieve
10132 -- the original generic in order to traverse its formal part.
10133
10134 if Present (Renamed_Entity (Actual)) then
10135 Gen_Decl := Unit_Declaration_Node (Renamed_Entity (Actual));
10136 else
10137 Gen_Decl := Unit_Declaration_Node (Actual);
10138 end if;
10139
10140 Formals := Generic_Formal_Declarations (Gen_Decl);
10141
10142 if Present (Formals) then
10143 F_Node := First_Non_Pragma (Formals);
10144 else
10145 F_Node := Empty;
10146 end if;
10147
10148 while Present (A_Ent)
10149 and then Present (F_Node)
10150 and then A_Ent /= First_Private_Entity (Act_Pkg)
10151 loop
10152 F_Ent := Get_Formal_Entity (F_Node);
10153
10154 if Present (F_Ent) then
10155
10156 -- This is a formal of the original package. Record
10157 -- association and recurse.
10158
10159 Find_Matching_Actual (F_Node, A_Ent);
10160 Match_Formal_Entity (F_Node, F_Ent, A_Ent);
10161 Next_Entity (A_Ent);
10162 end if;
10163
10164 Next_Non_Pragma (F_Node);
10165 end loop;
10166 end;
10167 end if;
10168 end Match_Formal_Entity;
10169
10170 -----------------------
10171 -- Get_Formal_Entity --
10172 -----------------------
10173
10174 function Get_Formal_Entity (N : Node_Id) return Entity_Id is
10175 Kind : constant Node_Kind := Nkind (Original_Node (N));
10176 begin
10177 case Kind is
10178 when N_Formal_Object_Declaration =>
10179 return Defining_Identifier (N);
10180
10181 when N_Formal_Type_Declaration =>
10182 return Defining_Identifier (N);
10183
10184 when N_Formal_Subprogram_Declaration =>
10185 return Defining_Unit_Name (Specification (N));
10186
10187 when N_Formal_Package_Declaration =>
10188 return Defining_Identifier (Original_Node (N));
10189
10190 when N_Generic_Package_Declaration =>
10191 return Defining_Identifier (Original_Node (N));
10192
10193 -- All other declarations are introduced by semantic analysis and
10194 -- have no match in the actual.
10195
10196 when others =>
10197 return Empty;
10198 end case;
10199 end Get_Formal_Entity;
10200
10201 --------------------
10202 -- Is_Instance_Of --
10203 --------------------
10204
10205 function Is_Instance_Of
10206 (Act_Spec : Entity_Id;
10207 Gen_Anc : Entity_Id) return Boolean
10208 is
10209 Gen_Par : constant Entity_Id := Generic_Parent (Act_Spec);
10210
10211 begin
10212 if No (Gen_Par) then
10213 return False;
10214
10215 -- Simplest case: the generic parent of the actual is the formal
10216
10217 elsif Gen_Par = Gen_Anc then
10218 return True;
10219
10220 elsif Chars (Gen_Par) /= Chars (Gen_Anc) then
10221 return False;
10222
10223 -- The actual may be obtained through several instantiations. Its
10224 -- scope must itself be an instance of a generic declared in the
10225 -- same scope as the formal. Any other case is detected above.
10226
10227 elsif not Is_Generic_Instance (Scope (Gen_Par)) then
10228 return False;
10229
10230 else
10231 return Generic_Parent (Parent (Scope (Gen_Par))) = Scope (Gen_Anc);
10232 end if;
10233 end Is_Instance_Of;
10234
10235 ---------------------------
10236 -- Process_Nested_Formal --
10237 ---------------------------
10238
10239 procedure Process_Nested_Formal (Formal : Entity_Id) is
10240 Ent : Entity_Id;
10241
10242 begin
10243 if Present (Associated_Formal_Package (Formal))
10244 and then Box_Present (Parent (Associated_Formal_Package (Formal)))
10245 then
10246 Ent := First_Entity (Formal);
10247 while Present (Ent) loop
10248 Set_Is_Hidden (Ent, False);
10249 Set_Is_Visible_Formal (Ent);
10250 Set_Is_Potentially_Use_Visible
10251 (Ent, Is_Potentially_Use_Visible (Formal));
10252
10253 if Ekind (Ent) = E_Package then
10254 exit when Renamed_Entity (Ent) = Renamed_Entity (Formal);
10255 Process_Nested_Formal (Ent);
10256 end if;
10257
10258 Next_Entity (Ent);
10259 end loop;
10260 end if;
10261 end Process_Nested_Formal;
10262
10263 -- Start of processing for Instantiate_Formal_Package
10264
10265 begin
10266 Analyze (Actual);
10267
10268 if not Is_Entity_Name (Actual)
10269 or else Ekind (Entity (Actual)) /= E_Package
10270 then
10271 Error_Msg_N
10272 ("expect package instance to instantiate formal", Actual);
10273 Abandon_Instantiation (Actual);
10274 raise Program_Error;
10275
10276 else
10277 Actual_Pack := Entity (Actual);
10278 Set_Is_Instantiated (Actual_Pack);
10279
10280 -- The actual may be a renamed package, or an outer generic formal
10281 -- package whose instantiation is converted into a renaming.
10282
10283 if Present (Renamed_Object (Actual_Pack)) then
10284 Actual_Pack := Renamed_Object (Actual_Pack);
10285 end if;
10286
10287 if Nkind (Analyzed_Formal) = N_Formal_Package_Declaration then
10288 Gen_Parent := Get_Instance_Of (Entity (Name (Analyzed_Formal)));
10289 Formal_Pack := Defining_Identifier (Analyzed_Formal);
10290 else
10291 Gen_Parent :=
10292 Generic_Parent (Specification (Analyzed_Formal));
10293 Formal_Pack :=
10294 Defining_Unit_Name (Specification (Analyzed_Formal));
10295 end if;
10296
10297 if Nkind (Parent (Actual_Pack)) = N_Defining_Program_Unit_Name then
10298 Parent_Spec := Package_Specification (Actual_Pack);
10299 else
10300 Parent_Spec := Parent (Actual_Pack);
10301 end if;
10302
10303 if Gen_Parent = Any_Id then
10304 Error_Msg_N
10305 ("previous error in declaration of formal package", Actual);
10306 Abandon_Instantiation (Actual);
10307
10308 elsif
10309 Is_Instance_Of (Parent_Spec, Get_Instance_Of (Gen_Parent))
10310 then
10311 null;
10312
10313 else
10314 Error_Msg_NE
10315 ("actual parameter must be instance of&", Actual, Gen_Parent);
10316 Abandon_Instantiation (Actual);
10317 end if;
10318
10319 Set_Instance_Of (Defining_Identifier (Formal), Actual_Pack);
10320 Map_Formal_Package_Entities (Formal_Pack, Actual_Pack);
10321
10322 Nod :=
10323 Make_Package_Renaming_Declaration (Loc,
10324 Defining_Unit_Name => New_Copy (Defining_Identifier (Formal)),
10325 Name => New_Occurrence_Of (Actual_Pack, Loc));
10326
10327 Set_Associated_Formal_Package
10328 (Defining_Unit_Name (Nod), Defining_Identifier (Formal));
10329 Decls := New_List (Nod);
10330
10331 -- If the formal F has a box, then the generic declarations are
10332 -- visible in the generic G. In an instance of G, the corresponding
10333 -- entities in the actual for F (which are the actuals for the
10334 -- instantiation of the generic that F denotes) must also be made
10335 -- visible for analysis of the current instance. On exit from the
10336 -- current instance, those entities are made private again. If the
10337 -- actual is currently in use, these entities are also use-visible.
10338
10339 -- The loop through the actual entities also steps through the formal
10340 -- entities and enters associations from formals to actuals into the
10341 -- renaming map. This is necessary to properly handle checking of
10342 -- actual parameter associations for later formals that depend on
10343 -- actuals declared in the formal package.
10344
10345 -- In Ada 2005, partial parameterization requires that we make
10346 -- visible the actuals corresponding to formals that were defaulted
10347 -- in the formal package. There formals are identified because they
10348 -- remain formal generics within the formal package, rather than
10349 -- being renamings of the actuals supplied.
10350
10351 declare
10352 Gen_Decl : constant Node_Id :=
10353 Unit_Declaration_Node (Gen_Parent);
10354 Formals : constant List_Id :=
10355 Generic_Formal_Declarations (Gen_Decl);
10356
10357 Actual_Ent : Entity_Id;
10358 Actual_Of_Formal : Node_Id;
10359 Formal_Node : Node_Id;
10360 Formal_Ent : Entity_Id;
10361
10362 begin
10363 if Present (Formals) then
10364 Formal_Node := First_Non_Pragma (Formals);
10365 else
10366 Formal_Node := Empty;
10367 end if;
10368
10369 Actual_Ent := First_Entity (Actual_Pack);
10370 Actual_Of_Formal :=
10371 First (Visible_Declarations (Specification (Analyzed_Formal)));
10372 while Present (Actual_Ent)
10373 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
10374 loop
10375 if Present (Formal_Node) then
10376 Formal_Ent := Get_Formal_Entity (Formal_Node);
10377
10378 if Present (Formal_Ent) then
10379 Find_Matching_Actual (Formal_Node, Actual_Ent);
10380 Match_Formal_Entity (Formal_Node, Formal_Ent, Actual_Ent);
10381
10382 -- We iterate at the same time over the actuals of the
10383 -- local package created for the formal, to determine
10384 -- which one of the formals of the original generic were
10385 -- defaulted in the formal. The corresponding actual
10386 -- entities are visible in the enclosing instance.
10387
10388 if Box_Present (Formal)
10389 or else
10390 (Present (Actual_Of_Formal)
10391 and then
10392 Is_Generic_Formal
10393 (Get_Formal_Entity (Actual_Of_Formal)))
10394 then
10395 Set_Is_Hidden (Actual_Ent, False);
10396 Set_Is_Visible_Formal (Actual_Ent);
10397 Set_Is_Potentially_Use_Visible
10398 (Actual_Ent, In_Use (Actual_Pack));
10399
10400 if Ekind (Actual_Ent) = E_Package then
10401 Process_Nested_Formal (Actual_Ent);
10402 end if;
10403
10404 else
10405 Set_Is_Hidden (Actual_Ent);
10406 Set_Is_Potentially_Use_Visible (Actual_Ent, False);
10407 end if;
10408 end if;
10409
10410 Next_Non_Pragma (Formal_Node);
10411 Next (Actual_Of_Formal);
10412
10413 else
10414 -- No further formals to match, but the generic part may
10415 -- contain inherited operation that are not hidden in the
10416 -- enclosing instance.
10417
10418 Next_Entity (Actual_Ent);
10419 end if;
10420 end loop;
10421
10422 -- Inherited subprograms generated by formal derived types are
10423 -- also visible if the types are.
10424
10425 Actual_Ent := First_Entity (Actual_Pack);
10426 while Present (Actual_Ent)
10427 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
10428 loop
10429 if Is_Overloadable (Actual_Ent)
10430 and then
10431 Nkind (Parent (Actual_Ent)) = N_Subtype_Declaration
10432 and then
10433 not Is_Hidden (Defining_Identifier (Parent (Actual_Ent)))
10434 then
10435 Set_Is_Hidden (Actual_Ent, False);
10436 Set_Is_Potentially_Use_Visible
10437 (Actual_Ent, In_Use (Actual_Pack));
10438 end if;
10439
10440 Next_Entity (Actual_Ent);
10441 end loop;
10442 end;
10443
10444 -- If the formal is not declared with a box, reanalyze it as an
10445 -- abbreviated instantiation, to verify the matching rules of 12.7.
10446 -- The actual checks are performed after the generic associations
10447 -- have been analyzed, to guarantee the same visibility for this
10448 -- instantiation and for the actuals.
10449
10450 -- In Ada 2005, the generic associations for the formal can include
10451 -- defaulted parameters. These are ignored during check. This
10452 -- internal instantiation is removed from the tree after conformance
10453 -- checking, because it contains formal declarations for those
10454 -- defaulted parameters, and those should not reach the back-end.
10455
10456 if not Box_Present (Formal) then
10457 declare
10458 I_Pack : constant Entity_Id :=
10459 Make_Temporary (Sloc (Actual), 'P');
10460
10461 begin
10462 Set_Is_Internal (I_Pack);
10463
10464 Append_To (Decls,
10465 Make_Package_Instantiation (Sloc (Actual),
10466 Defining_Unit_Name => I_Pack,
10467 Name =>
10468 New_Occurrence_Of
10469 (Get_Instance_Of (Gen_Parent), Sloc (Actual)),
10470 Generic_Associations => Generic_Associations (Formal)));
10471 end;
10472 end if;
10473
10474 return Decls;
10475 end if;
10476 end Instantiate_Formal_Package;
10477
10478 -----------------------------------
10479 -- Instantiate_Formal_Subprogram --
10480 -----------------------------------
10481
10482 function Instantiate_Formal_Subprogram
10483 (Formal : Node_Id;
10484 Actual : Node_Id;
10485 Analyzed_Formal : Node_Id) return Node_Id
10486 is
10487 Analyzed_S : constant Entity_Id :=
10488 Defining_Unit_Name (Specification (Analyzed_Formal));
10489 Formal_Sub : constant Entity_Id :=
10490 Defining_Unit_Name (Specification (Formal));
10491
10492 function From_Parent_Scope (Subp : Entity_Id) return Boolean;
10493 -- If the generic is a child unit, the parent has been installed on the
10494 -- scope stack, but a default subprogram cannot resolve to something
10495 -- on the parent because that parent is not really part of the visible
10496 -- context (it is there to resolve explicit local entities). If the
10497 -- default has resolved in this way, we remove the entity from immediate
10498 -- visibility and analyze the node again to emit an error message or
10499 -- find another visible candidate.
10500
10501 procedure Valid_Actual_Subprogram (Act : Node_Id);
10502 -- Perform legality check and raise exception on failure
10503
10504 -----------------------
10505 -- From_Parent_Scope --
10506 -----------------------
10507
10508 function From_Parent_Scope (Subp : Entity_Id) return Boolean is
10509 Gen_Scope : Node_Id;
10510
10511 begin
10512 Gen_Scope := Scope (Analyzed_S);
10513 while Present (Gen_Scope) and then Is_Child_Unit (Gen_Scope) loop
10514 if Scope (Subp) = Scope (Gen_Scope) then
10515 return True;
10516 end if;
10517
10518 Gen_Scope := Scope (Gen_Scope);
10519 end loop;
10520
10521 return False;
10522 end From_Parent_Scope;
10523
10524 -----------------------------
10525 -- Valid_Actual_Subprogram --
10526 -----------------------------
10527
10528 procedure Valid_Actual_Subprogram (Act : Node_Id) is
10529 Act_E : Entity_Id;
10530
10531 begin
10532 if Is_Entity_Name (Act) then
10533 Act_E := Entity (Act);
10534
10535 elsif Nkind (Act) = N_Selected_Component
10536 and then Is_Entity_Name (Selector_Name (Act))
10537 then
10538 Act_E := Entity (Selector_Name (Act));
10539
10540 else
10541 Act_E := Empty;
10542 end if;
10543
10544 if (Present (Act_E) and then Is_Overloadable (Act_E))
10545 or else Nkind_In (Act, N_Attribute_Reference,
10546 N_Indexed_Component,
10547 N_Character_Literal,
10548 N_Explicit_Dereference)
10549 then
10550 return;
10551 end if;
10552
10553 Error_Msg_NE
10554 ("expect subprogram or entry name in instantiation of &",
10555 Instantiation_Node, Formal_Sub);
10556 Abandon_Instantiation (Instantiation_Node);
10557 end Valid_Actual_Subprogram;
10558
10559 -- Local variables
10560
10561 Decl_Node : Node_Id;
10562 Loc : Source_Ptr;
10563 Nam : Node_Id;
10564 New_Spec : Node_Id;
10565 New_Subp : Entity_Id;
10566
10567 -- Start of processing for Instantiate_Formal_Subprogram
10568
10569 begin
10570 New_Spec := New_Copy_Tree (Specification (Formal));
10571
10572 -- The tree copy has created the proper instantiation sloc for the
10573 -- new specification. Use this location for all other constructed
10574 -- declarations.
10575
10576 Loc := Sloc (Defining_Unit_Name (New_Spec));
10577
10578 -- Create new entity for the actual (New_Copy_Tree does not), and
10579 -- indicate that it is an actual.
10580
10581 New_Subp := Make_Defining_Identifier (Loc, Chars (Formal_Sub));
10582 Set_Ekind (New_Subp, Ekind (Analyzed_S));
10583 Set_Is_Generic_Actual_Subprogram (New_Subp);
10584 Set_Defining_Unit_Name (New_Spec, New_Subp);
10585
10586 -- Create new entities for the each of the formals in the specification
10587 -- of the renaming declaration built for the actual.
10588
10589 if Present (Parameter_Specifications (New_Spec)) then
10590 declare
10591 F : Node_Id;
10592 F_Id : Entity_Id;
10593
10594 begin
10595 F := First (Parameter_Specifications (New_Spec));
10596 while Present (F) loop
10597 F_Id := Defining_Identifier (F);
10598
10599 Set_Defining_Identifier (F,
10600 Make_Defining_Identifier (Sloc (F_Id), Chars (F_Id)));
10601 Next (F);
10602 end loop;
10603 end;
10604 end if;
10605
10606 -- Find entity of actual. If the actual is an attribute reference, it
10607 -- cannot be resolved here (its formal is missing) but is handled
10608 -- instead in Attribute_Renaming. If the actual is overloaded, it is
10609 -- fully resolved subsequently, when the renaming declaration for the
10610 -- formal is analyzed. If it is an explicit dereference, resolve the
10611 -- prefix but not the actual itself, to prevent interpretation as call.
10612
10613 if Present (Actual) then
10614 Loc := Sloc (Actual);
10615 Set_Sloc (New_Spec, Loc);
10616
10617 if Nkind (Actual) = N_Operator_Symbol then
10618 Find_Direct_Name (Actual);
10619
10620 elsif Nkind (Actual) = N_Explicit_Dereference then
10621 Analyze (Prefix (Actual));
10622
10623 elsif Nkind (Actual) /= N_Attribute_Reference then
10624 Analyze (Actual);
10625 end if;
10626
10627 Valid_Actual_Subprogram (Actual);
10628 Nam := Actual;
10629
10630 elsif Present (Default_Name (Formal)) then
10631 if not Nkind_In (Default_Name (Formal), N_Attribute_Reference,
10632 N_Selected_Component,
10633 N_Indexed_Component,
10634 N_Character_Literal)
10635 and then Present (Entity (Default_Name (Formal)))
10636 then
10637 Nam := New_Occurrence_Of (Entity (Default_Name (Formal)), Loc);
10638 else
10639 Nam := New_Copy (Default_Name (Formal));
10640 Set_Sloc (Nam, Loc);
10641 end if;
10642
10643 elsif Box_Present (Formal) then
10644
10645 -- Actual is resolved at the point of instantiation. Create an
10646 -- identifier or operator with the same name as the formal.
10647
10648 if Nkind (Formal_Sub) = N_Defining_Operator_Symbol then
10649 Nam :=
10650 Make_Operator_Symbol (Loc,
10651 Chars => Chars (Formal_Sub),
10652 Strval => No_String);
10653 else
10654 Nam := Make_Identifier (Loc, Chars (Formal_Sub));
10655 end if;
10656
10657 elsif Nkind (Specification (Formal)) = N_Procedure_Specification
10658 and then Null_Present (Specification (Formal))
10659 then
10660 -- Generate null body for procedure, for use in the instance
10661
10662 Decl_Node :=
10663 Make_Subprogram_Body (Loc,
10664 Specification => New_Spec,
10665 Declarations => New_List,
10666 Handled_Statement_Sequence =>
10667 Make_Handled_Sequence_Of_Statements (Loc,
10668 Statements => New_List (Make_Null_Statement (Loc))));
10669
10670 Set_Is_Intrinsic_Subprogram (Defining_Unit_Name (New_Spec));
10671 return Decl_Node;
10672
10673 else
10674 Error_Msg_Sloc := Sloc (Scope (Analyzed_S));
10675 Error_Msg_NE
10676 ("missing actual&", Instantiation_Node, Formal_Sub);
10677 Error_Msg_NE
10678 ("\in instantiation of & declared#",
10679 Instantiation_Node, Scope (Analyzed_S));
10680 Abandon_Instantiation (Instantiation_Node);
10681 end if;
10682
10683 Decl_Node :=
10684 Make_Subprogram_Renaming_Declaration (Loc,
10685 Specification => New_Spec,
10686 Name => Nam);
10687
10688 -- If we do not have an actual and the formal specified <> then set to
10689 -- get proper default.
10690
10691 if No (Actual) and then Box_Present (Formal) then
10692 Set_From_Default (Decl_Node);
10693 end if;
10694
10695 -- Gather possible interpretations for the actual before analyzing the
10696 -- instance. If overloaded, it will be resolved when analyzing the
10697 -- renaming declaration.
10698
10699 if Box_Present (Formal) and then No (Actual) then
10700 Analyze (Nam);
10701
10702 if Is_Child_Unit (Scope (Analyzed_S))
10703 and then Present (Entity (Nam))
10704 then
10705 if not Is_Overloaded (Nam) then
10706 if From_Parent_Scope (Entity (Nam)) then
10707 Set_Is_Immediately_Visible (Entity (Nam), False);
10708 Set_Entity (Nam, Empty);
10709 Set_Etype (Nam, Empty);
10710
10711 Analyze (Nam);
10712 Set_Is_Immediately_Visible (Entity (Nam));
10713 end if;
10714
10715 else
10716 declare
10717 I : Interp_Index;
10718 It : Interp;
10719
10720 begin
10721 Get_First_Interp (Nam, I, It);
10722 while Present (It.Nam) loop
10723 if From_Parent_Scope (It.Nam) then
10724 Remove_Interp (I);
10725 end if;
10726
10727 Get_Next_Interp (I, It);
10728 end loop;
10729 end;
10730 end if;
10731 end if;
10732 end if;
10733
10734 -- The generic instantiation freezes the actual. This can only be done
10735 -- once the actual is resolved, in the analysis of the renaming
10736 -- declaration. To make the formal subprogram entity available, we set
10737 -- Corresponding_Formal_Spec to point to the formal subprogram entity.
10738 -- This is also needed in Analyze_Subprogram_Renaming for the processing
10739 -- of formal abstract subprograms.
10740
10741 Set_Corresponding_Formal_Spec (Decl_Node, Analyzed_S);
10742
10743 -- We cannot analyze the renaming declaration, and thus find the actual,
10744 -- until all the actuals are assembled in the instance. For subsequent
10745 -- checks of other actuals, indicate the node that will hold the
10746 -- instance of this formal.
10747
10748 Set_Instance_Of (Analyzed_S, Nam);
10749
10750 if Nkind (Actual) = N_Selected_Component
10751 and then Is_Task_Type (Etype (Prefix (Actual)))
10752 and then not Is_Frozen (Etype (Prefix (Actual)))
10753 then
10754 -- The renaming declaration will create a body, which must appear
10755 -- outside of the instantiation, We move the renaming declaration
10756 -- out of the instance, and create an additional renaming inside,
10757 -- to prevent freezing anomalies.
10758
10759 declare
10760 Anon_Id : constant Entity_Id := Make_Temporary (Loc, 'E');
10761
10762 begin
10763 Set_Defining_Unit_Name (New_Spec, Anon_Id);
10764 Insert_Before (Instantiation_Node, Decl_Node);
10765 Analyze (Decl_Node);
10766
10767 -- Now create renaming within the instance
10768
10769 Decl_Node :=
10770 Make_Subprogram_Renaming_Declaration (Loc,
10771 Specification => New_Copy_Tree (New_Spec),
10772 Name => New_Occurrence_Of (Anon_Id, Loc));
10773
10774 Set_Defining_Unit_Name (Specification (Decl_Node),
10775 Make_Defining_Identifier (Loc, Chars (Formal_Sub)));
10776 end;
10777 end if;
10778
10779 return Decl_Node;
10780 end Instantiate_Formal_Subprogram;
10781
10782 ------------------------
10783 -- Instantiate_Object --
10784 ------------------------
10785
10786 function Instantiate_Object
10787 (Formal : Node_Id;
10788 Actual : Node_Id;
10789 Analyzed_Formal : Node_Id) return List_Id
10790 is
10791 Gen_Obj : constant Entity_Id := Defining_Identifier (Formal);
10792 A_Gen_Obj : constant Entity_Id :=
10793 Defining_Identifier (Analyzed_Formal);
10794 Acc_Def : Node_Id := Empty;
10795 Act_Assoc : constant Node_Id := Parent (Actual);
10796 Actual_Decl : Node_Id := Empty;
10797 Decl_Node : Node_Id;
10798 Def : Node_Id;
10799 Ftyp : Entity_Id;
10800 List : constant List_Id := New_List;
10801 Loc : constant Source_Ptr := Sloc (Actual);
10802 Orig_Ftyp : constant Entity_Id := Etype (A_Gen_Obj);
10803 Subt_Decl : Node_Id := Empty;
10804 Subt_Mark : Node_Id := Empty;
10805
10806 function Copy_Access_Def return Node_Id;
10807 -- If formal is an anonymous access, copy access definition of formal
10808 -- for generated object declaration.
10809
10810 ---------------------
10811 -- Copy_Access_Def --
10812 ---------------------
10813
10814 function Copy_Access_Def return Node_Id is
10815 begin
10816 Def := New_Copy_Tree (Acc_Def);
10817
10818 -- In addition, if formal is an access to subprogram we need to
10819 -- generate new formals for the signature of the default, so that
10820 -- the tree is properly formatted for ASIS use.
10821
10822 if Present (Access_To_Subprogram_Definition (Acc_Def)) then
10823 declare
10824 Par_Spec : Node_Id;
10825 begin
10826 Par_Spec :=
10827 First (Parameter_Specifications
10828 (Access_To_Subprogram_Definition (Def)));
10829 while Present (Par_Spec) loop
10830 Set_Defining_Identifier (Par_Spec,
10831 Make_Defining_Identifier (Sloc (Acc_Def),
10832 Chars => Chars (Defining_Identifier (Par_Spec))));
10833 Next (Par_Spec);
10834 end loop;
10835 end;
10836 end if;
10837
10838 return Def;
10839 end Copy_Access_Def;
10840
10841 -- Start of processing for Instantiate_Object
10842
10843 begin
10844 -- Formal may be an anonymous access
10845
10846 if Present (Subtype_Mark (Formal)) then
10847 Subt_Mark := Subtype_Mark (Formal);
10848 else
10849 Check_Access_Definition (Formal);
10850 Acc_Def := Access_Definition (Formal);
10851 end if;
10852
10853 -- Sloc for error message on missing actual
10854
10855 Error_Msg_Sloc := Sloc (Scope (A_Gen_Obj));
10856
10857 if Get_Instance_Of (Gen_Obj) /= Gen_Obj then
10858 Error_Msg_N ("duplicate instantiation of generic parameter", Actual);
10859 end if;
10860
10861 Set_Parent (List, Parent (Actual));
10862
10863 -- OUT present
10864
10865 if Out_Present (Formal) then
10866
10867 -- An IN OUT generic actual must be a name. The instantiation is a
10868 -- renaming declaration. The actual is the name being renamed. We
10869 -- use the actual directly, rather than a copy, because it is not
10870 -- used further in the list of actuals, and because a copy or a use
10871 -- of relocate_node is incorrect if the instance is nested within a
10872 -- generic. In order to simplify ASIS searches, the Generic_Parent
10873 -- field links the declaration to the generic association.
10874
10875 if No (Actual) then
10876 Error_Msg_NE
10877 ("missing actual &",
10878 Instantiation_Node, Gen_Obj);
10879 Error_Msg_NE
10880 ("\in instantiation of & declared#",
10881 Instantiation_Node, Scope (A_Gen_Obj));
10882 Abandon_Instantiation (Instantiation_Node);
10883 end if;
10884
10885 if Present (Subt_Mark) then
10886 Decl_Node :=
10887 Make_Object_Renaming_Declaration (Loc,
10888 Defining_Identifier => New_Copy (Gen_Obj),
10889 Subtype_Mark => New_Copy_Tree (Subt_Mark),
10890 Name => Actual);
10891
10892 else pragma Assert (Present (Acc_Def));
10893 Decl_Node :=
10894 Make_Object_Renaming_Declaration (Loc,
10895 Defining_Identifier => New_Copy (Gen_Obj),
10896 Access_Definition => New_Copy_Tree (Acc_Def),
10897 Name => Actual);
10898 end if;
10899
10900 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
10901
10902 -- The analysis of the actual may produce Insert_Action nodes, so
10903 -- the declaration must have a context in which to attach them.
10904
10905 Append (Decl_Node, List);
10906 Analyze (Actual);
10907
10908 -- Return if the analysis of the actual reported some error
10909
10910 if Etype (Actual) = Any_Type then
10911 return List;
10912 end if;
10913
10914 -- This check is performed here because Analyze_Object_Renaming will
10915 -- not check it when Comes_From_Source is False. Note though that the
10916 -- check for the actual being the name of an object will be performed
10917 -- in Analyze_Object_Renaming.
10918
10919 if Is_Object_Reference (Actual)
10920 and then Is_Dependent_Component_Of_Mutable_Object (Actual)
10921 then
10922 Error_Msg_N
10923 ("illegal discriminant-dependent component for in out parameter",
10924 Actual);
10925 end if;
10926
10927 -- The actual has to be resolved in order to check that it is a
10928 -- variable (due to cases such as F (1), where F returns access to
10929 -- an array, and for overloaded prefixes).
10930
10931 Ftyp := Get_Instance_Of (Etype (A_Gen_Obj));
10932
10933 -- If the type of the formal is not itself a formal, and the current
10934 -- unit is a child unit, the formal type must be declared in a
10935 -- parent, and must be retrieved by visibility.
10936
10937 if Ftyp = Orig_Ftyp
10938 and then Is_Generic_Unit (Scope (Ftyp))
10939 and then Is_Child_Unit (Scope (A_Gen_Obj))
10940 then
10941 declare
10942 Temp : constant Node_Id :=
10943 New_Copy_Tree (Subtype_Mark (Analyzed_Formal));
10944 begin
10945 Set_Entity (Temp, Empty);
10946 Find_Type (Temp);
10947 Ftyp := Entity (Temp);
10948 end;
10949 end if;
10950
10951 if Is_Private_Type (Ftyp)
10952 and then not Is_Private_Type (Etype (Actual))
10953 and then (Base_Type (Full_View (Ftyp)) = Base_Type (Etype (Actual))
10954 or else Base_Type (Etype (Actual)) = Ftyp)
10955 then
10956 -- If the actual has the type of the full view of the formal, or
10957 -- else a non-private subtype of the formal, then the visibility
10958 -- of the formal type has changed. Add to the actuals a subtype
10959 -- declaration that will force the exchange of views in the body
10960 -- of the instance as well.
10961
10962 Subt_Decl :=
10963 Make_Subtype_Declaration (Loc,
10964 Defining_Identifier => Make_Temporary (Loc, 'P'),
10965 Subtype_Indication => New_Occurrence_Of (Ftyp, Loc));
10966
10967 Prepend (Subt_Decl, List);
10968
10969 Prepend_Elmt (Full_View (Ftyp), Exchanged_Views);
10970 Exchange_Declarations (Ftyp);
10971 end if;
10972
10973 Resolve (Actual, Ftyp);
10974
10975 if not Denotes_Variable (Actual) then
10976 Error_Msg_NE ("actual for& must be a variable", Actual, Gen_Obj);
10977
10978 elsif Base_Type (Ftyp) /= Base_Type (Etype (Actual)) then
10979
10980 -- Ada 2005 (AI-423): For a generic formal object of mode in out,
10981 -- the type of the actual shall resolve to a specific anonymous
10982 -- access type.
10983
10984 if Ada_Version < Ada_2005
10985 or else Ekind (Base_Type (Ftyp)) /=
10986 E_Anonymous_Access_Type
10987 or else Ekind (Base_Type (Etype (Actual))) /=
10988 E_Anonymous_Access_Type
10989 then
10990 Error_Msg_NE
10991 ("type of actual does not match type of&", Actual, Gen_Obj);
10992 end if;
10993 end if;
10994
10995 Note_Possible_Modification (Actual, Sure => True);
10996
10997 -- Check for instantiation of atomic/volatile actual for
10998 -- non-atomic/volatile formal (RM C.6 (12)).
10999
11000 if Is_Atomic_Object (Actual) and then not Is_Atomic (Orig_Ftyp) then
11001 Error_Msg_N
11002 ("cannot instantiate non-atomic formal object "
11003 & "with atomic actual", Actual);
11004
11005 elsif Is_Volatile_Object (Actual) and then not Is_Volatile (Orig_Ftyp)
11006 then
11007 Error_Msg_N
11008 ("cannot instantiate non-volatile formal object "
11009 & "with volatile actual", Actual);
11010 end if;
11011
11012 -- Formal in-parameter
11013
11014 else
11015 -- The instantiation of a generic formal in-parameter is constant
11016 -- declaration. The actual is the expression for that declaration.
11017 -- Its type is a full copy of the type of the formal. This may be
11018 -- an access to subprogram, for which we need to generate entities
11019 -- for the formals in the new signature.
11020
11021 if Present (Actual) then
11022 if Present (Subt_Mark) then
11023 Def := New_Copy_Tree (Subt_Mark);
11024 else pragma Assert (Present (Acc_Def));
11025 Def := Copy_Access_Def;
11026 end if;
11027
11028 Decl_Node :=
11029 Make_Object_Declaration (Loc,
11030 Defining_Identifier => New_Copy (Gen_Obj),
11031 Constant_Present => True,
11032 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
11033 Object_Definition => Def,
11034 Expression => Actual);
11035
11036 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
11037
11038 -- A generic formal object of a tagged type is defined to be
11039 -- aliased so the new constant must also be treated as aliased.
11040
11041 if Is_Tagged_Type (Etype (A_Gen_Obj)) then
11042 Set_Aliased_Present (Decl_Node);
11043 end if;
11044
11045 Append (Decl_Node, List);
11046
11047 -- No need to repeat (pre-)analysis of some expression nodes
11048 -- already handled in Preanalyze_Actuals.
11049
11050 if Nkind (Actual) /= N_Allocator then
11051 Analyze (Actual);
11052
11053 -- Return if the analysis of the actual reported some error
11054
11055 if Etype (Actual) = Any_Type then
11056 return List;
11057 end if;
11058 end if;
11059
11060 declare
11061 Formal_Type : constant Entity_Id := Etype (A_Gen_Obj);
11062 Typ : Entity_Id;
11063
11064 begin
11065 Typ := Get_Instance_Of (Formal_Type);
11066
11067 -- If the actual appears in the current or an enclosing scope,
11068 -- use its type directly. This is relevant if it has an actual
11069 -- subtype that is distinct from its nominal one. This cannot
11070 -- be done in general because the type of the actual may
11071 -- depend on other actuals, and only be fully determined when
11072 -- the enclosing instance is analyzed.
11073
11074 if Present (Etype (Actual))
11075 and then Is_Constr_Subt_For_U_Nominal (Etype (Actual))
11076 then
11077 Freeze_Before (Instantiation_Node, Etype (Actual));
11078 else
11079 Freeze_Before (Instantiation_Node, Typ);
11080 end if;
11081
11082 -- If the actual is an aggregate, perform name resolution on
11083 -- its components (the analysis of an aggregate does not do it)
11084 -- to capture local names that may be hidden if the generic is
11085 -- a child unit.
11086
11087 if Nkind (Actual) = N_Aggregate then
11088 Preanalyze_And_Resolve (Actual, Typ);
11089 end if;
11090
11091 if Is_Limited_Type (Typ)
11092 and then not OK_For_Limited_Init (Typ, Actual)
11093 then
11094 Error_Msg_N
11095 ("initialization not allowed for limited types", Actual);
11096 Explain_Limited_Type (Typ, Actual);
11097 end if;
11098 end;
11099
11100 elsif Present (Default_Expression (Formal)) then
11101
11102 -- Use default to construct declaration
11103
11104 if Present (Subt_Mark) then
11105 Def := New_Copy (Subt_Mark);
11106 else pragma Assert (Present (Acc_Def));
11107 Def := Copy_Access_Def;
11108 end if;
11109
11110 Decl_Node :=
11111 Make_Object_Declaration (Sloc (Formal),
11112 Defining_Identifier => New_Copy (Gen_Obj),
11113 Constant_Present => True,
11114 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
11115 Object_Definition => Def,
11116 Expression => New_Copy_Tree
11117 (Default_Expression (Formal)));
11118
11119 Append (Decl_Node, List);
11120 Set_Analyzed (Expression (Decl_Node), False);
11121
11122 else
11123 Error_Msg_NE ("missing actual&", Instantiation_Node, Gen_Obj);
11124 Error_Msg_NE ("\in instantiation of & declared#",
11125 Instantiation_Node, Scope (A_Gen_Obj));
11126
11127 if Is_Scalar_Type (Etype (A_Gen_Obj)) then
11128
11129 -- Create dummy constant declaration so that instance can be
11130 -- analyzed, to minimize cascaded visibility errors.
11131
11132 if Present (Subt_Mark) then
11133 Def := Subt_Mark;
11134 else pragma Assert (Present (Acc_Def));
11135 Def := Acc_Def;
11136 end if;
11137
11138 Decl_Node :=
11139 Make_Object_Declaration (Loc,
11140 Defining_Identifier => New_Copy (Gen_Obj),
11141 Constant_Present => True,
11142 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
11143 Object_Definition => New_Copy (Def),
11144 Expression =>
11145 Make_Attribute_Reference (Sloc (Gen_Obj),
11146 Attribute_Name => Name_First,
11147 Prefix => New_Copy (Def)));
11148
11149 Append (Decl_Node, List);
11150
11151 else
11152 Abandon_Instantiation (Instantiation_Node);
11153 end if;
11154 end if;
11155 end if;
11156
11157 if Nkind (Actual) in N_Has_Entity then
11158 Actual_Decl := Parent (Entity (Actual));
11159 end if;
11160
11161 -- Ada 2005 (AI-423): For a formal object declaration with a null
11162 -- exclusion or an access definition that has a null exclusion: If the
11163 -- actual matching the formal object declaration denotes a generic
11164 -- formal object of another generic unit G, and the instantiation
11165 -- containing the actual occurs within the body of G or within the body
11166 -- of a generic unit declared within the declarative region of G, then
11167 -- the declaration of the formal object of G must have a null exclusion.
11168 -- Otherwise, the subtype of the actual matching the formal object
11169 -- declaration shall exclude null.
11170
11171 if Ada_Version >= Ada_2005
11172 and then Present (Actual_Decl)
11173 and then Nkind_In (Actual_Decl, N_Formal_Object_Declaration,
11174 N_Object_Declaration)
11175 and then Nkind (Analyzed_Formal) = N_Formal_Object_Declaration
11176 and then not Has_Null_Exclusion (Actual_Decl)
11177 and then Has_Null_Exclusion (Analyzed_Formal)
11178 then
11179 Error_Msg_Sloc := Sloc (Analyzed_Formal);
11180 Error_Msg_N
11181 ("actual must exclude null to match generic formal#", Actual);
11182 end if;
11183
11184 -- An effectively volatile object cannot be used as an actual in a
11185 -- generic instantiation (SPARK RM 7.1.3(7)). The following check is
11186 -- relevant only when SPARK_Mode is on as it is not a standard Ada
11187 -- legality rule, and also verifies that the actual is an object.
11188
11189 if SPARK_Mode = On
11190 and then Present (Actual)
11191 and then Is_Object_Reference (Actual)
11192 and then Is_Effectively_Volatile_Object (Actual)
11193 then
11194 Error_Msg_N
11195 ("volatile object cannot act as actual in generic instantiation",
11196 Actual);
11197 end if;
11198
11199 return List;
11200 end Instantiate_Object;
11201
11202 ------------------------------
11203 -- Instantiate_Package_Body --
11204 ------------------------------
11205
11206 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
11207 -- must be replaced by gotos which jump to the end of the routine in order
11208 -- to restore the Ghost and SPARK modes.
11209
11210 procedure Instantiate_Package_Body
11211 (Body_Info : Pending_Body_Info;
11212 Inlined_Body : Boolean := False;
11213 Body_Optional : Boolean := False)
11214 is
11215 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
11216 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Decl);
11217 Act_Spec : constant Node_Id := Specification (Act_Decl);
11218 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
11219 Gen_Id : constant Node_Id := Name (Inst_Node);
11220 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
11221 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
11222 Loc : constant Source_Ptr := Sloc (Inst_Node);
11223
11224 Saved_ISMP : constant Boolean :=
11225 Ignore_SPARK_Mode_Pragmas_In_Instance;
11226 Saved_Style_Check : constant Boolean := Style_Check;
11227
11228 procedure Check_Initialized_Types;
11229 -- In a generic package body, an entity of a generic private type may
11230 -- appear uninitialized. This is suspicious, unless the actual is a
11231 -- fully initialized type.
11232
11233 -----------------------------
11234 -- Check_Initialized_Types --
11235 -----------------------------
11236
11237 procedure Check_Initialized_Types is
11238 Decl : Node_Id;
11239 Formal : Entity_Id;
11240 Actual : Entity_Id;
11241 Uninit_Var : Entity_Id;
11242
11243 begin
11244 Decl := First (Generic_Formal_Declarations (Gen_Decl));
11245 while Present (Decl) loop
11246 Uninit_Var := Empty;
11247
11248 if Nkind (Decl) = N_Private_Extension_Declaration then
11249 Uninit_Var := Uninitialized_Variable (Decl);
11250
11251 elsif Nkind (Decl) = N_Formal_Type_Declaration
11252 and then Nkind (Formal_Type_Definition (Decl)) =
11253 N_Formal_Private_Type_Definition
11254 then
11255 Uninit_Var :=
11256 Uninitialized_Variable (Formal_Type_Definition (Decl));
11257 end if;
11258
11259 if Present (Uninit_Var) then
11260 Formal := Defining_Identifier (Decl);
11261 Actual := First_Entity (Act_Decl_Id);
11262
11263 -- For each formal there is a subtype declaration that renames
11264 -- the actual and has the same name as the formal. Locate the
11265 -- formal for warning message about uninitialized variables
11266 -- in the generic, for which the actual type should be a fully
11267 -- initialized type.
11268
11269 while Present (Actual) loop
11270 exit when Ekind (Actual) = E_Package
11271 and then Present (Renamed_Object (Actual));
11272
11273 if Chars (Actual) = Chars (Formal)
11274 and then not Is_Scalar_Type (Actual)
11275 and then not Is_Fully_Initialized_Type (Actual)
11276 and then Warn_On_No_Value_Assigned
11277 then
11278 Error_Msg_Node_2 := Formal;
11279 Error_Msg_NE
11280 ("generic unit has uninitialized variable& of "
11281 & "formal private type &?v?", Actual, Uninit_Var);
11282 Error_Msg_NE
11283 ("actual type for& should be fully initialized type?v?",
11284 Actual, Formal);
11285 exit;
11286 end if;
11287
11288 Next_Entity (Actual);
11289 end loop;
11290 end if;
11291
11292 Next (Decl);
11293 end loop;
11294 end Check_Initialized_Types;
11295
11296 -- Local variables
11297
11298 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
11299 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
11300 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
11301 -- Save the Ghost and SPARK mode-related data to restore on exit
11302
11303 Act_Body : Node_Id;
11304 Act_Body_Id : Entity_Id;
11305 Act_Body_Name : Node_Id;
11306 Gen_Body : Node_Id;
11307 Gen_Body_Id : Node_Id;
11308 Par_Ent : Entity_Id := Empty;
11309 Par_Vis : Boolean := False;
11310 Parent_Installed : Boolean := False;
11311
11312 Vis_Prims_List : Elist_Id := No_Elist;
11313 -- List of primitives made temporarily visible in the instantiation
11314 -- to match the visibility of the formal type.
11315
11316 -- Start of processing for Instantiate_Package_Body
11317
11318 begin
11319 Gen_Body_Id := Corresponding_Body (Gen_Decl);
11320
11321 -- The instance body may already have been processed, as the parent of
11322 -- another instance that is inlined (Load_Parent_Of_Generic).
11323
11324 if Present (Corresponding_Body (Instance_Spec (Inst_Node))) then
11325 return;
11326 end if;
11327
11328 -- The package being instantiated may be subject to pragma Ghost. Set
11329 -- the mode now to ensure that any nodes generated during instantiation
11330 -- are properly marked as Ghost.
11331
11332 Set_Ghost_Mode (Act_Decl_Id);
11333
11334 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
11335
11336 -- Re-establish the state of information on which checks are suppressed.
11337 -- This information was set in Body_Info at the point of instantiation,
11338 -- and now we restore it so that the instance is compiled using the
11339 -- check status at the instantiation (RM 11.5(7.2/2), AI95-00224-01).
11340
11341 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
11342 Scope_Suppress := Body_Info.Scope_Suppress;
11343 Opt.Ada_Version := Body_Info.Version;
11344 Opt.Ada_Version_Pragma := Body_Info.Version_Pragma;
11345 Restore_Warnings (Body_Info.Warnings);
11346
11347 -- Install the SPARK mode which applies to the package body
11348
11349 Install_SPARK_Mode (Body_Info.SPARK_Mode, Body_Info.SPARK_Mode_Pragma);
11350
11351 if No (Gen_Body_Id) then
11352
11353 -- Do not look for parent of generic body if none is required.
11354 -- This may happen when the routine is called as part of the
11355 -- Pending_Instantiations processing, when nested instances
11356 -- may precede the one generated from the main unit.
11357
11358 if not Unit_Requires_Body (Defining_Entity (Gen_Decl))
11359 and then Body_Optional
11360 then
11361 goto Leave;
11362 else
11363 Load_Parent_Of_Generic
11364 (Inst_Node, Specification (Gen_Decl), Body_Optional);
11365 Gen_Body_Id := Corresponding_Body (Gen_Decl);
11366 end if;
11367 end if;
11368
11369 -- Establish global variable for sloc adjustment and for error recovery
11370 -- In the case of an instance body for an instantiation with actuals
11371 -- from a limited view, the instance body is placed at the beginning
11372 -- of the enclosing package body: use the body entity as the source
11373 -- location for nodes of the instance body.
11374
11375 if not Is_Empty_Elmt_List (Incomplete_Actuals (Act_Decl_Id)) then
11376 declare
11377 Scop : constant Entity_Id := Scope (Act_Decl_Id);
11378 Body_Id : constant Node_Id :=
11379 Corresponding_Body (Unit_Declaration_Node (Scop));
11380
11381 begin
11382 Instantiation_Node := Body_Id;
11383 end;
11384 else
11385 Instantiation_Node := Inst_Node;
11386 end if;
11387
11388 if Present (Gen_Body_Id) then
11389 Save_Env (Gen_Unit, Act_Decl_Id);
11390 Style_Check := False;
11391
11392 -- If the context of the instance is subject to SPARK_Mode "off", the
11393 -- annotation is missing, or the body is instantiated at a later pass
11394 -- and its spec ignored SPARK_Mode pragma, set the global flag which
11395 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within the
11396 -- instance.
11397
11398 if SPARK_Mode /= On
11399 or else Ignore_SPARK_Mode_Pragmas (Act_Decl_Id)
11400 then
11401 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
11402 end if;
11403
11404 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
11405 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
11406
11407 Create_Instantiation_Source
11408 (Inst_Node, Gen_Body_Id, S_Adjustment);
11409
11410 Act_Body :=
11411 Copy_Generic_Node
11412 (Original_Node (Gen_Body), Empty, Instantiating => True);
11413
11414 -- Create proper (possibly qualified) defining name for the body, to
11415 -- correspond to the one in the spec.
11416
11417 Act_Body_Id :=
11418 Make_Defining_Identifier (Sloc (Act_Decl_Id), Chars (Act_Decl_Id));
11419 Set_Comes_From_Source (Act_Body_Id, Comes_From_Source (Act_Decl_Id));
11420
11421 -- Some attributes of spec entity are not inherited by body entity
11422
11423 Set_Handler_Records (Act_Body_Id, No_List);
11424
11425 if Nkind (Defining_Unit_Name (Act_Spec)) =
11426 N_Defining_Program_Unit_Name
11427 then
11428 Act_Body_Name :=
11429 Make_Defining_Program_Unit_Name (Loc,
11430 Name =>
11431 New_Copy_Tree (Name (Defining_Unit_Name (Act_Spec))),
11432 Defining_Identifier => Act_Body_Id);
11433 else
11434 Act_Body_Name := Act_Body_Id;
11435 end if;
11436
11437 Set_Defining_Unit_Name (Act_Body, Act_Body_Name);
11438
11439 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
11440 Check_Generic_Actuals (Act_Decl_Id, False);
11441 Check_Initialized_Types;
11442
11443 -- Install primitives hidden at the point of the instantiation but
11444 -- visible when processing the generic formals
11445
11446 declare
11447 E : Entity_Id;
11448
11449 begin
11450 E := First_Entity (Act_Decl_Id);
11451 while Present (E) loop
11452 if Is_Type (E)
11453 and then not Is_Itype (E)
11454 and then Is_Generic_Actual_Type (E)
11455 and then Is_Tagged_Type (E)
11456 then
11457 Install_Hidden_Primitives
11458 (Prims_List => Vis_Prims_List,
11459 Gen_T => Generic_Parent_Type (Parent (E)),
11460 Act_T => E);
11461 end if;
11462
11463 Next_Entity (E);
11464 end loop;
11465 end;
11466
11467 -- If it is a child unit, make the parent instance (which is an
11468 -- instance of the parent of the generic) visible. The parent
11469 -- instance is the prefix of the name of the generic unit.
11470
11471 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
11472 and then Nkind (Gen_Id) = N_Expanded_Name
11473 then
11474 Par_Ent := Entity (Prefix (Gen_Id));
11475 Par_Vis := Is_Immediately_Visible (Par_Ent);
11476 Install_Parent (Par_Ent, In_Body => True);
11477 Parent_Installed := True;
11478
11479 elsif Is_Child_Unit (Gen_Unit) then
11480 Par_Ent := Scope (Gen_Unit);
11481 Par_Vis := Is_Immediately_Visible (Par_Ent);
11482 Install_Parent (Par_Ent, In_Body => True);
11483 Parent_Installed := True;
11484 end if;
11485
11486 -- If the instantiation is a library unit, and this is the main unit,
11487 -- then build the resulting compilation unit nodes for the instance.
11488 -- If this is a compilation unit but it is not the main unit, then it
11489 -- is the body of a unit in the context, that is being compiled
11490 -- because it is encloses some inlined unit or another generic unit
11491 -- being instantiated. In that case, this body is not part of the
11492 -- current compilation, and is not attached to the tree, but its
11493 -- parent must be set for analysis.
11494
11495 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
11496
11497 -- Replace instance node with body of instance, and create new
11498 -- node for corresponding instance declaration.
11499
11500 Build_Instance_Compilation_Unit_Nodes
11501 (Inst_Node, Act_Body, Act_Decl);
11502 Analyze (Inst_Node);
11503
11504 if Parent (Inst_Node) = Cunit (Main_Unit) then
11505
11506 -- If the instance is a child unit itself, then set the scope
11507 -- of the expanded body to be the parent of the instantiation
11508 -- (ensuring that the fully qualified name will be generated
11509 -- for the elaboration subprogram).
11510
11511 if Nkind (Defining_Unit_Name (Act_Spec)) =
11512 N_Defining_Program_Unit_Name
11513 then
11514 Set_Scope (Defining_Entity (Inst_Node), Scope (Act_Decl_Id));
11515 end if;
11516 end if;
11517
11518 -- Case where instantiation is not a library unit
11519
11520 else
11521 -- If this is an early instantiation, i.e. appears textually
11522 -- before the corresponding body and must be elaborated first,
11523 -- indicate that the body instance is to be delayed.
11524
11525 Install_Body (Act_Body, Inst_Node, Gen_Body, Gen_Decl);
11526
11527 -- Now analyze the body. We turn off all checks if this is an
11528 -- internal unit, since there is no reason to have checks on for
11529 -- any predefined run-time library code. All such code is designed
11530 -- to be compiled with checks off.
11531
11532 -- Note that we do NOT apply this criterion to children of GNAT
11533 -- The latter units must suppress checks explicitly if needed.
11534
11535 -- We also do not suppress checks in CodePeer mode where we are
11536 -- interested in finding possible runtime errors.
11537
11538 if not CodePeer_Mode
11539 and then In_Predefined_Unit (Gen_Decl)
11540 then
11541 Analyze (Act_Body, Suppress => All_Checks);
11542 else
11543 Analyze (Act_Body);
11544 end if;
11545 end if;
11546
11547 Inherit_Context (Gen_Body, Inst_Node);
11548
11549 -- Remove the parent instances if they have been placed on the scope
11550 -- stack to compile the body.
11551
11552 if Parent_Installed then
11553 Remove_Parent (In_Body => True);
11554
11555 -- Restore the previous visibility of the parent
11556
11557 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
11558 end if;
11559
11560 Restore_Hidden_Primitives (Vis_Prims_List);
11561 Restore_Private_Views (Act_Decl_Id);
11562
11563 -- Remove the current unit from visibility if this is an instance
11564 -- that is not elaborated on the fly for inlining purposes.
11565
11566 if not Inlined_Body then
11567 Set_Is_Immediately_Visible (Act_Decl_Id, False);
11568 end if;
11569
11570 Restore_Env;
11571
11572 -- If we have no body, and the unit requires a body, then complain. This
11573 -- complaint is suppressed if we have detected other errors (since a
11574 -- common reason for missing the body is that it had errors).
11575 -- In CodePeer mode, a warning has been emitted already, no need for
11576 -- further messages.
11577
11578 elsif Unit_Requires_Body (Gen_Unit)
11579 and then not Body_Optional
11580 then
11581 if CodePeer_Mode then
11582 null;
11583
11584 elsif Serious_Errors_Detected = 0 then
11585 Error_Msg_NE
11586 ("cannot find body of generic package &", Inst_Node, Gen_Unit);
11587
11588 -- Don't attempt to perform any cleanup actions if some other error
11589 -- was already detected, since this can cause blowups.
11590
11591 else
11592 goto Leave;
11593 end if;
11594
11595 -- Case of package that does not need a body
11596
11597 else
11598 -- If the instantiation of the declaration is a library unit, rewrite
11599 -- the original package instantiation as a package declaration in the
11600 -- compilation unit node.
11601
11602 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
11603 Set_Parent_Spec (Act_Decl, Parent_Spec (Inst_Node));
11604 Rewrite (Inst_Node, Act_Decl);
11605
11606 -- Generate elaboration entity, in case spec has elaboration code.
11607 -- This cannot be done when the instance is analyzed, because it
11608 -- is not known yet whether the body exists.
11609
11610 Set_Elaboration_Entity_Required (Act_Decl_Id, False);
11611 Build_Elaboration_Entity (Parent (Inst_Node), Act_Decl_Id);
11612
11613 -- If the instantiation is not a library unit, then append the
11614 -- declaration to the list of implicitly generated entities, unless
11615 -- it is already a list member which means that it was already
11616 -- processed
11617
11618 elsif not Is_List_Member (Act_Decl) then
11619 Mark_Rewrite_Insertion (Act_Decl);
11620 Insert_Before (Inst_Node, Act_Decl);
11621 end if;
11622 end if;
11623
11624 Expander_Mode_Restore;
11625
11626 <<Leave>>
11627 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
11628 Restore_Ghost_Mode (Saved_GM);
11629 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
11630 Style_Check := Saved_Style_Check;
11631 end Instantiate_Package_Body;
11632
11633 ---------------------------------
11634 -- Instantiate_Subprogram_Body --
11635 ---------------------------------
11636
11637 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
11638 -- must be replaced by gotos which jump to the end of the routine in order
11639 -- to restore the Ghost and SPARK modes.
11640
11641 procedure Instantiate_Subprogram_Body
11642 (Body_Info : Pending_Body_Info;
11643 Body_Optional : Boolean := False)
11644 is
11645 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
11646 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Decl);
11647 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
11648 Gen_Id : constant Node_Id := Name (Inst_Node);
11649 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
11650 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
11651 Loc : constant Source_Ptr := Sloc (Inst_Node);
11652 Pack_Id : constant Entity_Id :=
11653 Defining_Unit_Name (Parent (Act_Decl));
11654
11655 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
11656 Saved_ISMP : constant Boolean :=
11657 Ignore_SPARK_Mode_Pragmas_In_Instance;
11658 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
11659 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
11660 -- Save the Ghost and SPARK mode-related data to restore on exit
11661
11662 Saved_Style_Check : constant Boolean := Style_Check;
11663 Saved_Warnings : constant Warning_Record := Save_Warnings;
11664
11665 Act_Body : Node_Id;
11666 Act_Body_Id : Entity_Id;
11667 Gen_Body : Node_Id;
11668 Gen_Body_Id : Node_Id;
11669 Pack_Body : Node_Id;
11670 Par_Ent : Entity_Id := Empty;
11671 Par_Vis : Boolean := False;
11672 Ret_Expr : Node_Id;
11673
11674 Parent_Installed : Boolean := False;
11675
11676 begin
11677 Gen_Body_Id := Corresponding_Body (Gen_Decl);
11678
11679 -- Subprogram body may have been created already because of an inline
11680 -- pragma, or because of multiple elaborations of the enclosing package
11681 -- when several instances of the subprogram appear in the main unit.
11682
11683 if Present (Corresponding_Body (Act_Decl)) then
11684 return;
11685 end if;
11686
11687 -- The subprogram being instantiated may be subject to pragma Ghost. Set
11688 -- the mode now to ensure that any nodes generated during instantiation
11689 -- are properly marked as Ghost.
11690
11691 Set_Ghost_Mode (Act_Decl_Id);
11692
11693 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
11694
11695 -- Re-establish the state of information on which checks are suppressed.
11696 -- This information was set in Body_Info at the point of instantiation,
11697 -- and now we restore it so that the instance is compiled using the
11698 -- check status at the instantiation (RM 11.5(7.2/2), AI95-00224-01).
11699
11700 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
11701 Scope_Suppress := Body_Info.Scope_Suppress;
11702 Opt.Ada_Version := Body_Info.Version;
11703 Opt.Ada_Version_Pragma := Body_Info.Version_Pragma;
11704 Restore_Warnings (Body_Info.Warnings);
11705
11706 -- Install the SPARK mode which applies to the subprogram body from the
11707 -- instantiation context. This may be refined further if an explicit
11708 -- SPARK_Mode pragma applies to the generic body.
11709
11710 Install_SPARK_Mode (Body_Info.SPARK_Mode, Body_Info.SPARK_Mode_Pragma);
11711
11712 if No (Gen_Body_Id) then
11713
11714 -- For imported generic subprogram, no body to compile, complete
11715 -- the spec entity appropriately.
11716
11717 if Is_Imported (Gen_Unit) then
11718 Set_Is_Imported (Act_Decl_Id);
11719 Set_First_Rep_Item (Act_Decl_Id, First_Rep_Item (Gen_Unit));
11720 Set_Interface_Name (Act_Decl_Id, Interface_Name (Gen_Unit));
11721 Set_Convention (Act_Decl_Id, Convention (Gen_Unit));
11722 Set_Has_Completion (Act_Decl_Id);
11723 goto Leave;
11724
11725 -- For other cases, compile the body
11726
11727 else
11728 Load_Parent_Of_Generic
11729 (Inst_Node, Specification (Gen_Decl), Body_Optional);
11730 Gen_Body_Id := Corresponding_Body (Gen_Decl);
11731 end if;
11732 end if;
11733
11734 Instantiation_Node := Inst_Node;
11735
11736 if Present (Gen_Body_Id) then
11737 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
11738
11739 if Nkind (Gen_Body) = N_Subprogram_Body_Stub then
11740
11741 -- Either body is not present, or context is non-expanding, as
11742 -- when compiling a subunit. Mark the instance as completed, and
11743 -- diagnose a missing body when needed.
11744
11745 if Expander_Active
11746 and then Operating_Mode = Generate_Code
11747 then
11748 Error_Msg_N ("missing proper body for instantiation", Gen_Body);
11749 end if;
11750
11751 Set_Has_Completion (Act_Decl_Id);
11752 goto Leave;
11753 end if;
11754
11755 Save_Env (Gen_Unit, Act_Decl_Id);
11756 Style_Check := False;
11757
11758 -- If the context of the instance is subject to SPARK_Mode "off", the
11759 -- annotation is missing, or the body is instantiated at a later pass
11760 -- and its spec ignored SPARK_Mode pragma, set the global flag which
11761 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within the
11762 -- instance.
11763
11764 if SPARK_Mode /= On
11765 or else Ignore_SPARK_Mode_Pragmas (Act_Decl_Id)
11766 then
11767 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
11768 end if;
11769
11770 -- If the context of an instance is not subject to SPARK_Mode "off",
11771 -- and the generic body is subject to an explicit SPARK_Mode pragma,
11772 -- the latter should be the one applicable to the instance.
11773
11774 if not Ignore_SPARK_Mode_Pragmas_In_Instance
11775 and then SPARK_Mode /= Off
11776 and then Present (SPARK_Pragma (Gen_Body_Id))
11777 then
11778 Set_SPARK_Mode (Gen_Body_Id);
11779 end if;
11780
11781 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
11782 Create_Instantiation_Source
11783 (Inst_Node,
11784 Gen_Body_Id,
11785 S_Adjustment);
11786
11787 Act_Body :=
11788 Copy_Generic_Node
11789 (Original_Node (Gen_Body), Empty, Instantiating => True);
11790
11791 -- Create proper defining name for the body, to correspond to the one
11792 -- in the spec.
11793
11794 Act_Body_Id :=
11795 Make_Defining_Identifier (Sloc (Act_Decl_Id), Chars (Act_Decl_Id));
11796
11797 Set_Comes_From_Source (Act_Body_Id, Comes_From_Source (Act_Decl_Id));
11798 Set_Defining_Unit_Name (Specification (Act_Body), Act_Body_Id);
11799
11800 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
11801 Set_Has_Completion (Act_Decl_Id);
11802 Check_Generic_Actuals (Pack_Id, False);
11803
11804 -- Generate a reference to link the visible subprogram instance to
11805 -- the generic body, which for navigation purposes is the only
11806 -- available source for the instance.
11807
11808 Generate_Reference
11809 (Related_Instance (Pack_Id),
11810 Gen_Body_Id, 'b', Set_Ref => False, Force => True);
11811
11812 -- If it is a child unit, make the parent instance (which is an
11813 -- instance of the parent of the generic) visible. The parent
11814 -- instance is the prefix of the name of the generic unit.
11815
11816 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
11817 and then Nkind (Gen_Id) = N_Expanded_Name
11818 then
11819 Par_Ent := Entity (Prefix (Gen_Id));
11820 Par_Vis := Is_Immediately_Visible (Par_Ent);
11821 Install_Parent (Par_Ent, In_Body => True);
11822 Parent_Installed := True;
11823
11824 elsif Is_Child_Unit (Gen_Unit) then
11825 Par_Ent := Scope (Gen_Unit);
11826 Par_Vis := Is_Immediately_Visible (Par_Ent);
11827 Install_Parent (Par_Ent, In_Body => True);
11828 Parent_Installed := True;
11829 end if;
11830
11831 -- Subprogram body is placed in the body of wrapper package,
11832 -- whose spec contains the subprogram declaration as well as
11833 -- the renaming declarations for the generic parameters.
11834
11835 Pack_Body :=
11836 Make_Package_Body (Loc,
11837 Defining_Unit_Name => New_Copy (Pack_Id),
11838 Declarations => New_List (Act_Body));
11839
11840 Set_Corresponding_Spec (Pack_Body, Pack_Id);
11841
11842 -- If the instantiation is a library unit, then build resulting
11843 -- compilation unit nodes for the instance. The declaration of
11844 -- the enclosing package is the grandparent of the subprogram
11845 -- declaration. First replace the instantiation node as the unit
11846 -- of the corresponding compilation.
11847
11848 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
11849 if Parent (Inst_Node) = Cunit (Main_Unit) then
11850 Set_Unit (Parent (Inst_Node), Inst_Node);
11851 Build_Instance_Compilation_Unit_Nodes
11852 (Inst_Node, Pack_Body, Parent (Parent (Act_Decl)));
11853 Analyze (Inst_Node);
11854 else
11855 Set_Parent (Pack_Body, Parent (Inst_Node));
11856 Analyze (Pack_Body);
11857 end if;
11858
11859 else
11860 Insert_Before (Inst_Node, Pack_Body);
11861 Mark_Rewrite_Insertion (Pack_Body);
11862 Analyze (Pack_Body);
11863
11864 if Expander_Active then
11865 Freeze_Subprogram_Body (Inst_Node, Gen_Body, Pack_Id);
11866 end if;
11867 end if;
11868
11869 Inherit_Context (Gen_Body, Inst_Node);
11870
11871 Restore_Private_Views (Pack_Id, False);
11872
11873 if Parent_Installed then
11874 Remove_Parent (In_Body => True);
11875
11876 -- Restore the previous visibility of the parent
11877
11878 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
11879 end if;
11880
11881 Restore_Env;
11882 Restore_Warnings (Saved_Warnings);
11883
11884 -- Body not found. Error was emitted already. If there were no previous
11885 -- errors, this may be an instance whose scope is a premature instance.
11886 -- In that case we must insure that the (legal) program does raise
11887 -- program error if executed. We generate a subprogram body for this
11888 -- purpose. See DEC ac30vso.
11889
11890 -- Should not reference proprietary DEC tests in comments ???
11891
11892 elsif Serious_Errors_Detected = 0
11893 and then Nkind (Parent (Inst_Node)) /= N_Compilation_Unit
11894 then
11895 if Body_Optional then
11896 goto Leave;
11897
11898 elsif Ekind (Act_Decl_Id) = E_Procedure then
11899 Act_Body :=
11900 Make_Subprogram_Body (Loc,
11901 Specification =>
11902 Make_Procedure_Specification (Loc,
11903 Defining_Unit_Name =>
11904 Make_Defining_Identifier (Loc, Chars (Act_Decl_Id)),
11905 Parameter_Specifications =>
11906 New_Copy_List
11907 (Parameter_Specifications (Parent (Act_Decl_Id)))),
11908
11909 Declarations => Empty_List,
11910 Handled_Statement_Sequence =>
11911 Make_Handled_Sequence_Of_Statements (Loc,
11912 Statements => New_List (
11913 Make_Raise_Program_Error (Loc,
11914 Reason => PE_Access_Before_Elaboration))));
11915
11916 else
11917 Ret_Expr :=
11918 Make_Raise_Program_Error (Loc,
11919 Reason => PE_Access_Before_Elaboration);
11920
11921 Set_Etype (Ret_Expr, (Etype (Act_Decl_Id)));
11922 Set_Analyzed (Ret_Expr);
11923
11924 Act_Body :=
11925 Make_Subprogram_Body (Loc,
11926 Specification =>
11927 Make_Function_Specification (Loc,
11928 Defining_Unit_Name =>
11929 Make_Defining_Identifier (Loc, Chars (Act_Decl_Id)),
11930 Parameter_Specifications =>
11931 New_Copy_List
11932 (Parameter_Specifications (Parent (Act_Decl_Id))),
11933 Result_Definition =>
11934 New_Occurrence_Of (Etype (Act_Decl_Id), Loc)),
11935
11936 Declarations => Empty_List,
11937 Handled_Statement_Sequence =>
11938 Make_Handled_Sequence_Of_Statements (Loc,
11939 Statements => New_List (
11940 Make_Simple_Return_Statement (Loc, Ret_Expr))));
11941 end if;
11942
11943 Pack_Body :=
11944 Make_Package_Body (Loc,
11945 Defining_Unit_Name => New_Copy (Pack_Id),
11946 Declarations => New_List (Act_Body));
11947
11948 Insert_After (Inst_Node, Pack_Body);
11949 Set_Corresponding_Spec (Pack_Body, Pack_Id);
11950 Analyze (Pack_Body);
11951 end if;
11952
11953 Expander_Mode_Restore;
11954
11955 <<Leave>>
11956 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
11957 Restore_Ghost_Mode (Saved_GM);
11958 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
11959 Style_Check := Saved_Style_Check;
11960 end Instantiate_Subprogram_Body;
11961
11962 ----------------------
11963 -- Instantiate_Type --
11964 ----------------------
11965
11966 function Instantiate_Type
11967 (Formal : Node_Id;
11968 Actual : Node_Id;
11969 Analyzed_Formal : Node_Id;
11970 Actual_Decls : List_Id) return List_Id
11971 is
11972 A_Gen_T : constant Entity_Id :=
11973 Defining_Identifier (Analyzed_Formal);
11974 Def : constant Node_Id := Formal_Type_Definition (Formal);
11975 Gen_T : constant Entity_Id := Defining_Identifier (Formal);
11976 Act_T : Entity_Id;
11977 Ancestor : Entity_Id := Empty;
11978 Decl_Node : Node_Id;
11979 Decl_Nodes : List_Id;
11980 Loc : Source_Ptr;
11981 Subt : Entity_Id;
11982
11983 procedure Diagnose_Predicated_Actual;
11984 -- There are a number of constructs in which a discrete type with
11985 -- predicates is illegal, e.g. as an index in an array type declaration.
11986 -- If a generic type is used is such a construct in a generic package
11987 -- declaration, it carries the flag No_Predicate_On_Actual. it is part
11988 -- of the generic contract that the actual cannot have predicates.
11989
11990 procedure Validate_Array_Type_Instance;
11991 procedure Validate_Access_Subprogram_Instance;
11992 procedure Validate_Access_Type_Instance;
11993 procedure Validate_Derived_Type_Instance;
11994 procedure Validate_Derived_Interface_Type_Instance;
11995 procedure Validate_Discriminated_Formal_Type;
11996 procedure Validate_Interface_Type_Instance;
11997 procedure Validate_Private_Type_Instance;
11998 procedure Validate_Incomplete_Type_Instance;
11999 -- These procedures perform validation tests for the named case.
12000 -- Validate_Discriminated_Formal_Type is shared by formal private
12001 -- types and Ada 2012 formal incomplete types.
12002
12003 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean;
12004 -- Check that base types are the same and that the subtypes match
12005 -- statically. Used in several of the above.
12006
12007 ---------------------------------
12008 -- Diagnose_Predicated_Actual --
12009 ---------------------------------
12010
12011 procedure Diagnose_Predicated_Actual is
12012 begin
12013 if No_Predicate_On_Actual (A_Gen_T)
12014 and then Has_Predicates (Act_T)
12015 then
12016 Error_Msg_NE
12017 ("actual for& cannot be a type with predicate",
12018 Instantiation_Node, A_Gen_T);
12019
12020 elsif No_Dynamic_Predicate_On_Actual (A_Gen_T)
12021 and then Has_Predicates (Act_T)
12022 and then not Has_Static_Predicate_Aspect (Act_T)
12023 then
12024 Error_Msg_NE
12025 ("actual for& cannot be a type with a dynamic predicate",
12026 Instantiation_Node, A_Gen_T);
12027 end if;
12028 end Diagnose_Predicated_Actual;
12029
12030 --------------------
12031 -- Subtypes_Match --
12032 --------------------
12033
12034 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean is
12035 T : constant Entity_Id := Get_Instance_Of (Gen_T);
12036
12037 begin
12038 -- Some detailed comments would be useful here ???
12039
12040 return ((Base_Type (T) = Act_T
12041 or else Base_Type (T) = Base_Type (Act_T))
12042 and then Subtypes_Statically_Match (T, Act_T))
12043
12044 or else (Is_Class_Wide_Type (Gen_T)
12045 and then Is_Class_Wide_Type (Act_T)
12046 and then Subtypes_Match
12047 (Get_Instance_Of (Root_Type (Gen_T)),
12048 Root_Type (Act_T)))
12049
12050 or else
12051 (Ekind_In (Gen_T, E_Anonymous_Access_Subprogram_Type,
12052 E_Anonymous_Access_Type)
12053 and then Ekind (Act_T) = Ekind (Gen_T)
12054 and then Subtypes_Statically_Match
12055 (Designated_Type (Gen_T), Designated_Type (Act_T)));
12056 end Subtypes_Match;
12057
12058 -----------------------------------------
12059 -- Validate_Access_Subprogram_Instance --
12060 -----------------------------------------
12061
12062 procedure Validate_Access_Subprogram_Instance is
12063 begin
12064 if not Is_Access_Type (Act_T)
12065 or else Ekind (Designated_Type (Act_T)) /= E_Subprogram_Type
12066 then
12067 Error_Msg_NE
12068 ("expect access type in instantiation of &", Actual, Gen_T);
12069 Abandon_Instantiation (Actual);
12070 end if;
12071
12072 -- According to AI05-288, actuals for access_to_subprograms must be
12073 -- subtype conformant with the generic formal. Previous to AI05-288
12074 -- only mode conformance was required.
12075
12076 -- This is a binding interpretation that applies to previous versions
12077 -- of the language, no need to maintain previous weaker checks.
12078
12079 Check_Subtype_Conformant
12080 (Designated_Type (Act_T),
12081 Designated_Type (A_Gen_T),
12082 Actual,
12083 Get_Inst => True);
12084
12085 if Ekind (Base_Type (Act_T)) = E_Access_Protected_Subprogram_Type then
12086 if Ekind (A_Gen_T) = E_Access_Subprogram_Type then
12087 Error_Msg_NE
12088 ("protected access type not allowed for formal &",
12089 Actual, Gen_T);
12090 end if;
12091
12092 elsif Ekind (A_Gen_T) = E_Access_Protected_Subprogram_Type then
12093 Error_Msg_NE
12094 ("expect protected access type for formal &",
12095 Actual, Gen_T);
12096 end if;
12097
12098 -- If the formal has a specified convention (which in most cases
12099 -- will be StdCall) verify that the actual has the same convention.
12100
12101 if Has_Convention_Pragma (A_Gen_T)
12102 and then Convention (A_Gen_T) /= Convention (Act_T)
12103 then
12104 Error_Msg_Name_1 := Get_Convention_Name (Convention (A_Gen_T));
12105 Error_Msg_NE
12106 ("actual for formal & must have convention %", Actual, Gen_T);
12107 end if;
12108 end Validate_Access_Subprogram_Instance;
12109
12110 -----------------------------------
12111 -- Validate_Access_Type_Instance --
12112 -----------------------------------
12113
12114 procedure Validate_Access_Type_Instance is
12115 Desig_Type : constant Entity_Id :=
12116 Find_Actual_Type (Designated_Type (A_Gen_T), A_Gen_T);
12117 Desig_Act : Entity_Id;
12118
12119 begin
12120 if not Is_Access_Type (Act_T) then
12121 Error_Msg_NE
12122 ("expect access type in instantiation of &", Actual, Gen_T);
12123 Abandon_Instantiation (Actual);
12124 end if;
12125
12126 if Is_Access_Constant (A_Gen_T) then
12127 if not Is_Access_Constant (Act_T) then
12128 Error_Msg_N
12129 ("actual type must be access-to-constant type", Actual);
12130 Abandon_Instantiation (Actual);
12131 end if;
12132 else
12133 if Is_Access_Constant (Act_T) then
12134 Error_Msg_N
12135 ("actual type must be access-to-variable type", Actual);
12136 Abandon_Instantiation (Actual);
12137
12138 elsif Ekind (A_Gen_T) = E_General_Access_Type
12139 and then Ekind (Base_Type (Act_T)) /= E_General_Access_Type
12140 then
12141 Error_Msg_N -- CODEFIX
12142 ("actual must be general access type!", Actual);
12143 Error_Msg_NE -- CODEFIX
12144 ("add ALL to }!", Actual, Act_T);
12145 Abandon_Instantiation (Actual);
12146 end if;
12147 end if;
12148
12149 -- The designated subtypes, that is to say the subtypes introduced
12150 -- by an access type declaration (and not by a subtype declaration)
12151 -- must match.
12152
12153 Desig_Act := Designated_Type (Base_Type (Act_T));
12154
12155 -- The designated type may have been introduced through a limited_
12156 -- with clause, in which case retrieve the non-limited view. This
12157 -- applies to incomplete types as well as to class-wide types.
12158
12159 if From_Limited_With (Desig_Act) then
12160 Desig_Act := Available_View (Desig_Act);
12161 end if;
12162
12163 if not Subtypes_Match (Desig_Type, Desig_Act) then
12164 Error_Msg_NE
12165 ("designated type of actual does not match that of formal &",
12166 Actual, Gen_T);
12167
12168 if not Predicates_Match (Desig_Type, Desig_Act) then
12169 Error_Msg_N ("\predicates do not match", Actual);
12170 end if;
12171
12172 Abandon_Instantiation (Actual);
12173
12174 elsif Is_Access_Type (Designated_Type (Act_T))
12175 and then Is_Constrained (Designated_Type (Designated_Type (Act_T)))
12176 /=
12177 Is_Constrained (Designated_Type (Desig_Type))
12178 then
12179 Error_Msg_NE
12180 ("designated type of actual does not match that of formal &",
12181 Actual, Gen_T);
12182
12183 if not Predicates_Match (Desig_Type, Desig_Act) then
12184 Error_Msg_N ("\predicates do not match", Actual);
12185 end if;
12186
12187 Abandon_Instantiation (Actual);
12188 end if;
12189
12190 -- Ada 2005: null-exclusion indicators of the two types must agree
12191
12192 if Can_Never_Be_Null (A_Gen_T) /= Can_Never_Be_Null (Act_T) then
12193 Error_Msg_NE
12194 ("non null exclusion of actual and formal & do not match",
12195 Actual, Gen_T);
12196 end if;
12197 end Validate_Access_Type_Instance;
12198
12199 ----------------------------------
12200 -- Validate_Array_Type_Instance --
12201 ----------------------------------
12202
12203 procedure Validate_Array_Type_Instance is
12204 I1 : Node_Id;
12205 I2 : Node_Id;
12206 T2 : Entity_Id;
12207
12208 function Formal_Dimensions return Nat;
12209 -- Count number of dimensions in array type formal
12210
12211 -----------------------
12212 -- Formal_Dimensions --
12213 -----------------------
12214
12215 function Formal_Dimensions return Nat is
12216 Num : Nat := 0;
12217 Index : Node_Id;
12218
12219 begin
12220 if Nkind (Def) = N_Constrained_Array_Definition then
12221 Index := First (Discrete_Subtype_Definitions (Def));
12222 else
12223 Index := First (Subtype_Marks (Def));
12224 end if;
12225
12226 while Present (Index) loop
12227 Num := Num + 1;
12228 Next_Index (Index);
12229 end loop;
12230
12231 return Num;
12232 end Formal_Dimensions;
12233
12234 -- Start of processing for Validate_Array_Type_Instance
12235
12236 begin
12237 if not Is_Array_Type (Act_T) then
12238 Error_Msg_NE
12239 ("expect array type in instantiation of &", Actual, Gen_T);
12240 Abandon_Instantiation (Actual);
12241
12242 elsif Nkind (Def) = N_Constrained_Array_Definition then
12243 if not (Is_Constrained (Act_T)) then
12244 Error_Msg_NE
12245 ("expect constrained array in instantiation of &",
12246 Actual, Gen_T);
12247 Abandon_Instantiation (Actual);
12248 end if;
12249
12250 else
12251 if Is_Constrained (Act_T) then
12252 Error_Msg_NE
12253 ("expect unconstrained array in instantiation of &",
12254 Actual, Gen_T);
12255 Abandon_Instantiation (Actual);
12256 end if;
12257 end if;
12258
12259 if Formal_Dimensions /= Number_Dimensions (Act_T) then
12260 Error_Msg_NE
12261 ("dimensions of actual do not match formal &", Actual, Gen_T);
12262 Abandon_Instantiation (Actual);
12263 end if;
12264
12265 I1 := First_Index (A_Gen_T);
12266 I2 := First_Index (Act_T);
12267 for J in 1 .. Formal_Dimensions loop
12268
12269 -- If the indexes of the actual were given by a subtype_mark,
12270 -- the index was transformed into a range attribute. Retrieve
12271 -- the original type mark for checking.
12272
12273 if Is_Entity_Name (Original_Node (I2)) then
12274 T2 := Entity (Original_Node (I2));
12275 else
12276 T2 := Etype (I2);
12277 end if;
12278
12279 if not Subtypes_Match
12280 (Find_Actual_Type (Etype (I1), A_Gen_T), T2)
12281 then
12282 Error_Msg_NE
12283 ("index types of actual do not match those of formal &",
12284 Actual, Gen_T);
12285 Abandon_Instantiation (Actual);
12286 end if;
12287
12288 Next_Index (I1);
12289 Next_Index (I2);
12290 end loop;
12291
12292 -- Check matching subtypes. Note that there are complex visibility
12293 -- issues when the generic is a child unit and some aspect of the
12294 -- generic type is declared in a parent unit of the generic. We do
12295 -- the test to handle this special case only after a direct check
12296 -- for static matching has failed. The case where both the component
12297 -- type and the array type are separate formals, and the component
12298 -- type is a private view may also require special checking in
12299 -- Subtypes_Match. Finally, we assume that a child instance where
12300 -- the component type comes from a formal of a parent instance is
12301 -- correct because the generic was correct. A more precise check
12302 -- seems too complex to install???
12303
12304 if Subtypes_Match
12305 (Component_Type (A_Gen_T), Component_Type (Act_T))
12306 or else
12307 Subtypes_Match
12308 (Find_Actual_Type (Component_Type (A_Gen_T), A_Gen_T),
12309 Component_Type (Act_T))
12310 or else
12311 (not Inside_A_Generic
12312 and then Is_Child_Unit (Scope (Component_Type (A_Gen_T))))
12313 then
12314 null;
12315 else
12316 Error_Msg_NE
12317 ("component subtype of actual does not match that of formal &",
12318 Actual, Gen_T);
12319 Abandon_Instantiation (Actual);
12320 end if;
12321
12322 if Has_Aliased_Components (A_Gen_T)
12323 and then not Has_Aliased_Components (Act_T)
12324 then
12325 Error_Msg_NE
12326 ("actual must have aliased components to match formal type &",
12327 Actual, Gen_T);
12328 end if;
12329 end Validate_Array_Type_Instance;
12330
12331 -----------------------------------------------
12332 -- Validate_Derived_Interface_Type_Instance --
12333 -----------------------------------------------
12334
12335 procedure Validate_Derived_Interface_Type_Instance is
12336 Par : constant Entity_Id := Entity (Subtype_Indication (Def));
12337 Elmt : Elmt_Id;
12338
12339 begin
12340 -- First apply interface instance checks
12341
12342 Validate_Interface_Type_Instance;
12343
12344 -- Verify that immediate parent interface is an ancestor of
12345 -- the actual.
12346
12347 if Present (Par)
12348 and then not Interface_Present_In_Ancestor (Act_T, Par)
12349 then
12350 Error_Msg_NE
12351 ("interface actual must include progenitor&", Actual, Par);
12352 end if;
12353
12354 -- Now verify that the actual includes all other ancestors of
12355 -- the formal.
12356
12357 Elmt := First_Elmt (Interfaces (A_Gen_T));
12358 while Present (Elmt) loop
12359 if not Interface_Present_In_Ancestor
12360 (Act_T, Get_Instance_Of (Node (Elmt)))
12361 then
12362 Error_Msg_NE
12363 ("interface actual must include progenitor&",
12364 Actual, Node (Elmt));
12365 end if;
12366
12367 Next_Elmt (Elmt);
12368 end loop;
12369 end Validate_Derived_Interface_Type_Instance;
12370
12371 ------------------------------------
12372 -- Validate_Derived_Type_Instance --
12373 ------------------------------------
12374
12375 procedure Validate_Derived_Type_Instance is
12376 Actual_Discr : Entity_Id;
12377 Ancestor_Discr : Entity_Id;
12378
12379 begin
12380 -- If the parent type in the generic declaration is itself a previous
12381 -- formal type, then it is local to the generic and absent from the
12382 -- analyzed generic definition. In that case the ancestor is the
12383 -- instance of the formal (which must have been instantiated
12384 -- previously), unless the ancestor is itself a formal derived type.
12385 -- In this latter case (which is the subject of Corrigendum 8652/0038
12386 -- (AI-202) the ancestor of the formals is the ancestor of its
12387 -- parent. Otherwise, the analyzed generic carries the parent type.
12388 -- If the parent type is defined in a previous formal package, then
12389 -- the scope of that formal package is that of the generic type
12390 -- itself, and it has already been mapped into the corresponding type
12391 -- in the actual package.
12392
12393 -- Common case: parent type defined outside of the generic
12394
12395 if Is_Entity_Name (Subtype_Mark (Def))
12396 and then Present (Entity (Subtype_Mark (Def)))
12397 then
12398 Ancestor := Get_Instance_Of (Entity (Subtype_Mark (Def)));
12399
12400 -- Check whether parent is defined in a previous formal package
12401
12402 elsif
12403 Scope (Scope (Base_Type (Etype (A_Gen_T)))) = Scope (A_Gen_T)
12404 then
12405 Ancestor :=
12406 Get_Instance_Of (Base_Type (Etype (A_Gen_T)));
12407
12408 -- The type may be a local derivation, or a type extension of a
12409 -- previous formal, or of a formal of a parent package.
12410
12411 elsif Is_Derived_Type (Get_Instance_Of (A_Gen_T))
12412 or else
12413 Ekind (Get_Instance_Of (A_Gen_T)) = E_Record_Type_With_Private
12414 then
12415 -- Check whether the parent is another derived formal type in the
12416 -- same generic unit.
12417
12418 if Etype (A_Gen_T) /= A_Gen_T
12419 and then Is_Generic_Type (Etype (A_Gen_T))
12420 and then Scope (Etype (A_Gen_T)) = Scope (A_Gen_T)
12421 and then Etype (Etype (A_Gen_T)) /= Etype (A_Gen_T)
12422 then
12423 -- Locate ancestor of parent from the subtype declaration
12424 -- created for the actual.
12425
12426 declare
12427 Decl : Node_Id;
12428
12429 begin
12430 Decl := First (Actual_Decls);
12431 while Present (Decl) loop
12432 if Nkind (Decl) = N_Subtype_Declaration
12433 and then Chars (Defining_Identifier (Decl)) =
12434 Chars (Etype (A_Gen_T))
12435 then
12436 Ancestor := Generic_Parent_Type (Decl);
12437 exit;
12438 else
12439 Next (Decl);
12440 end if;
12441 end loop;
12442 end;
12443
12444 pragma Assert (Present (Ancestor));
12445
12446 -- The ancestor itself may be a previous formal that has been
12447 -- instantiated.
12448
12449 Ancestor := Get_Instance_Of (Ancestor);
12450
12451 else
12452 Ancestor :=
12453 Get_Instance_Of (Base_Type (Get_Instance_Of (A_Gen_T)));
12454 end if;
12455
12456 -- Check whether parent is a previous formal of the current generic
12457
12458 elsif Is_Derived_Type (A_Gen_T)
12459 and then Is_Generic_Type (Etype (A_Gen_T))
12460 and then Scope (A_Gen_T) = Scope (Etype (A_Gen_T))
12461 then
12462 Ancestor := Get_Instance_Of (First_Subtype (Etype (A_Gen_T)));
12463
12464 -- An unusual case: the actual is a type declared in a parent unit,
12465 -- but is not a formal type so there is no instance_of for it.
12466 -- Retrieve it by analyzing the record extension.
12467
12468 elsif Is_Child_Unit (Scope (A_Gen_T))
12469 and then In_Open_Scopes (Scope (Act_T))
12470 and then Is_Generic_Instance (Scope (Act_T))
12471 then
12472 Analyze (Subtype_Mark (Def));
12473 Ancestor := Entity (Subtype_Mark (Def));
12474
12475 else
12476 Ancestor := Get_Instance_Of (Etype (Base_Type (A_Gen_T)));
12477 end if;
12478
12479 -- If the formal derived type has pragma Preelaborable_Initialization
12480 -- then the actual type must have preelaborable initialization.
12481
12482 if Known_To_Have_Preelab_Init (A_Gen_T)
12483 and then not Has_Preelaborable_Initialization (Act_T)
12484 then
12485 Error_Msg_NE
12486 ("actual for & must have preelaborable initialization",
12487 Actual, Gen_T);
12488 end if;
12489
12490 -- Ada 2005 (AI-251)
12491
12492 if Ada_Version >= Ada_2005 and then Is_Interface (Ancestor) then
12493 if not Interface_Present_In_Ancestor (Act_T, Ancestor) then
12494 Error_Msg_NE
12495 ("(Ada 2005) expected type implementing & in instantiation",
12496 Actual, Ancestor);
12497 end if;
12498
12499 -- Finally verify that the (instance of) the ancestor is an ancestor
12500 -- of the actual.
12501
12502 elsif not Is_Ancestor (Base_Type (Ancestor), Act_T) then
12503 Error_Msg_NE
12504 ("expect type derived from & in instantiation",
12505 Actual, First_Subtype (Ancestor));
12506 Abandon_Instantiation (Actual);
12507 end if;
12508
12509 -- Ada 2005 (AI-443): Synchronized formal derived type checks. Note
12510 -- that the formal type declaration has been rewritten as a private
12511 -- extension.
12512
12513 if Ada_Version >= Ada_2005
12514 and then Nkind (Parent (A_Gen_T)) = N_Private_Extension_Declaration
12515 and then Synchronized_Present (Parent (A_Gen_T))
12516 then
12517 -- The actual must be a synchronized tagged type
12518
12519 if not Is_Tagged_Type (Act_T) then
12520 Error_Msg_N
12521 ("actual of synchronized type must be tagged", Actual);
12522 Abandon_Instantiation (Actual);
12523
12524 elsif Nkind (Parent (Act_T)) = N_Full_Type_Declaration
12525 and then Nkind (Type_Definition (Parent (Act_T))) =
12526 N_Derived_Type_Definition
12527 and then not Synchronized_Present
12528 (Type_Definition (Parent (Act_T)))
12529 then
12530 Error_Msg_N
12531 ("actual of synchronized type must be synchronized", Actual);
12532 Abandon_Instantiation (Actual);
12533 end if;
12534 end if;
12535
12536 -- Perform atomic/volatile checks (RM C.6(12)). Note that AI05-0218-1
12537 -- removes the second instance of the phrase "or allow pass by copy".
12538
12539 if Is_Atomic (Act_T) and then not Is_Atomic (Ancestor) then
12540 Error_Msg_N
12541 ("cannot have atomic actual type for non-atomic formal type",
12542 Actual);
12543
12544 elsif Is_Volatile (Act_T) and then not Is_Volatile (Ancestor) then
12545 Error_Msg_N
12546 ("cannot have volatile actual type for non-volatile formal type",
12547 Actual);
12548 end if;
12549
12550 -- It should not be necessary to check for unknown discriminants on
12551 -- Formal, but for some reason Has_Unknown_Discriminants is false for
12552 -- A_Gen_T, so Is_Definite_Subtype incorrectly returns True. This
12553 -- needs fixing. ???
12554
12555 if Is_Definite_Subtype (A_Gen_T)
12556 and then not Unknown_Discriminants_Present (Formal)
12557 and then not Is_Definite_Subtype (Act_T)
12558 then
12559 Error_Msg_N ("actual subtype must be constrained", Actual);
12560 Abandon_Instantiation (Actual);
12561 end if;
12562
12563 if not Unknown_Discriminants_Present (Formal) then
12564 if Is_Constrained (Ancestor) then
12565 if not Is_Constrained (Act_T) then
12566 Error_Msg_N ("actual subtype must be constrained", Actual);
12567 Abandon_Instantiation (Actual);
12568 end if;
12569
12570 -- Ancestor is unconstrained, Check if generic formal and actual
12571 -- agree on constrainedness. The check only applies to array types
12572 -- and discriminated types.
12573
12574 elsif Is_Constrained (Act_T) then
12575 if Ekind (Ancestor) = E_Access_Type
12576 or else (not Is_Constrained (A_Gen_T)
12577 and then Is_Composite_Type (A_Gen_T))
12578 then
12579 Error_Msg_N ("actual subtype must be unconstrained", Actual);
12580 Abandon_Instantiation (Actual);
12581 end if;
12582
12583 -- A class-wide type is only allowed if the formal has unknown
12584 -- discriminants.
12585
12586 elsif Is_Class_Wide_Type (Act_T)
12587 and then not Has_Unknown_Discriminants (Ancestor)
12588 then
12589 Error_Msg_NE
12590 ("actual for & cannot be a class-wide type", Actual, Gen_T);
12591 Abandon_Instantiation (Actual);
12592
12593 -- Otherwise, the formal and actual must have the same number
12594 -- of discriminants and each discriminant of the actual must
12595 -- correspond to a discriminant of the formal.
12596
12597 elsif Has_Discriminants (Act_T)
12598 and then not Has_Unknown_Discriminants (Act_T)
12599 and then Has_Discriminants (Ancestor)
12600 then
12601 Actual_Discr := First_Discriminant (Act_T);
12602 Ancestor_Discr := First_Discriminant (Ancestor);
12603 while Present (Actual_Discr)
12604 and then Present (Ancestor_Discr)
12605 loop
12606 if Base_Type (Act_T) /= Base_Type (Ancestor) and then
12607 No (Corresponding_Discriminant (Actual_Discr))
12608 then
12609 Error_Msg_NE
12610 ("discriminant & does not correspond "
12611 & "to ancestor discriminant", Actual, Actual_Discr);
12612 Abandon_Instantiation (Actual);
12613 end if;
12614
12615 Next_Discriminant (Actual_Discr);
12616 Next_Discriminant (Ancestor_Discr);
12617 end loop;
12618
12619 if Present (Actual_Discr) or else Present (Ancestor_Discr) then
12620 Error_Msg_NE
12621 ("actual for & must have same number of discriminants",
12622 Actual, Gen_T);
12623 Abandon_Instantiation (Actual);
12624 end if;
12625
12626 -- This case should be caught by the earlier check for
12627 -- constrainedness, but the check here is added for completeness.
12628
12629 elsif Has_Discriminants (Act_T)
12630 and then not Has_Unknown_Discriminants (Act_T)
12631 then
12632 Error_Msg_NE
12633 ("actual for & must not have discriminants", Actual, Gen_T);
12634 Abandon_Instantiation (Actual);
12635
12636 elsif Has_Discriminants (Ancestor) then
12637 Error_Msg_NE
12638 ("actual for & must have known discriminants", Actual, Gen_T);
12639 Abandon_Instantiation (Actual);
12640 end if;
12641
12642 if not Subtypes_Statically_Compatible
12643 (Act_T, Ancestor, Formal_Derived_Matching => True)
12644 then
12645 Error_Msg_N
12646 ("constraint on actual is incompatible with formal", Actual);
12647 Abandon_Instantiation (Actual);
12648 end if;
12649 end if;
12650
12651 -- If the formal and actual types are abstract, check that there
12652 -- are no abstract primitives of the actual type that correspond to
12653 -- nonabstract primitives of the formal type (second sentence of
12654 -- RM95 3.9.3(9)).
12655
12656 if Is_Abstract_Type (A_Gen_T) and then Is_Abstract_Type (Act_T) then
12657 Check_Abstract_Primitives : declare
12658 Gen_Prims : constant Elist_Id :=
12659 Primitive_Operations (A_Gen_T);
12660 Gen_Elmt : Elmt_Id;
12661 Gen_Subp : Entity_Id;
12662 Anc_Subp : Entity_Id;
12663 Anc_Formal : Entity_Id;
12664 Anc_F_Type : Entity_Id;
12665
12666 Act_Prims : constant Elist_Id := Primitive_Operations (Act_T);
12667 Act_Elmt : Elmt_Id;
12668 Act_Subp : Entity_Id;
12669 Act_Formal : Entity_Id;
12670 Act_F_Type : Entity_Id;
12671
12672 Subprograms_Correspond : Boolean;
12673
12674 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean;
12675 -- Returns true if T2 is derived directly or indirectly from
12676 -- T1, including derivations from interfaces. T1 and T2 are
12677 -- required to be specific tagged base types.
12678
12679 ------------------------
12680 -- Is_Tagged_Ancestor --
12681 ------------------------
12682
12683 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean
12684 is
12685 Intfc_Elmt : Elmt_Id;
12686
12687 begin
12688 -- The predicate is satisfied if the types are the same
12689
12690 if T1 = T2 then
12691 return True;
12692
12693 -- If we've reached the top of the derivation chain then
12694 -- we know that T1 is not an ancestor of T2.
12695
12696 elsif Etype (T2) = T2 then
12697 return False;
12698
12699 -- Proceed to check T2's immediate parent
12700
12701 elsif Is_Ancestor (T1, Base_Type (Etype (T2))) then
12702 return True;
12703
12704 -- Finally, check to see if T1 is an ancestor of any of T2's
12705 -- progenitors.
12706
12707 else
12708 Intfc_Elmt := First_Elmt (Interfaces (T2));
12709 while Present (Intfc_Elmt) loop
12710 if Is_Ancestor (T1, Node (Intfc_Elmt)) then
12711 return True;
12712 end if;
12713
12714 Next_Elmt (Intfc_Elmt);
12715 end loop;
12716 end if;
12717
12718 return False;
12719 end Is_Tagged_Ancestor;
12720
12721 -- Start of processing for Check_Abstract_Primitives
12722
12723 begin
12724 -- Loop over all of the formal derived type's primitives
12725
12726 Gen_Elmt := First_Elmt (Gen_Prims);
12727 while Present (Gen_Elmt) loop
12728 Gen_Subp := Node (Gen_Elmt);
12729
12730 -- If the primitive of the formal is not abstract, then
12731 -- determine whether there is a corresponding primitive of
12732 -- the actual type that's abstract.
12733
12734 if not Is_Abstract_Subprogram (Gen_Subp) then
12735 Act_Elmt := First_Elmt (Act_Prims);
12736 while Present (Act_Elmt) loop
12737 Act_Subp := Node (Act_Elmt);
12738
12739 -- If we find an abstract primitive of the actual,
12740 -- then we need to test whether it corresponds to the
12741 -- subprogram from which the generic formal primitive
12742 -- is inherited.
12743
12744 if Is_Abstract_Subprogram (Act_Subp) then
12745 Anc_Subp := Alias (Gen_Subp);
12746
12747 -- Test whether we have a corresponding primitive
12748 -- by comparing names, kinds, formal types, and
12749 -- result types.
12750
12751 if Chars (Anc_Subp) = Chars (Act_Subp)
12752 and then Ekind (Anc_Subp) = Ekind (Act_Subp)
12753 then
12754 Anc_Formal := First_Formal (Anc_Subp);
12755 Act_Formal := First_Formal (Act_Subp);
12756 while Present (Anc_Formal)
12757 and then Present (Act_Formal)
12758 loop
12759 Anc_F_Type := Etype (Anc_Formal);
12760 Act_F_Type := Etype (Act_Formal);
12761
12762 if Ekind (Anc_F_Type) =
12763 E_Anonymous_Access_Type
12764 then
12765 Anc_F_Type := Designated_Type (Anc_F_Type);
12766
12767 if Ekind (Act_F_Type) =
12768 E_Anonymous_Access_Type
12769 then
12770 Act_F_Type :=
12771 Designated_Type (Act_F_Type);
12772 else
12773 exit;
12774 end if;
12775
12776 elsif
12777 Ekind (Act_F_Type) = E_Anonymous_Access_Type
12778 then
12779 exit;
12780 end if;
12781
12782 Anc_F_Type := Base_Type (Anc_F_Type);
12783 Act_F_Type := Base_Type (Act_F_Type);
12784
12785 -- If the formal is controlling, then the
12786 -- the type of the actual primitive's formal
12787 -- must be derived directly or indirectly
12788 -- from the type of the ancestor primitive's
12789 -- formal.
12790
12791 if Is_Controlling_Formal (Anc_Formal) then
12792 if not Is_Tagged_Ancestor
12793 (Anc_F_Type, Act_F_Type)
12794 then
12795 exit;
12796 end if;
12797
12798 -- Otherwise the types of the formals must
12799 -- be the same.
12800
12801 elsif Anc_F_Type /= Act_F_Type then
12802 exit;
12803 end if;
12804
12805 Next_Entity (Anc_Formal);
12806 Next_Entity (Act_Formal);
12807 end loop;
12808
12809 -- If we traversed through all of the formals
12810 -- then so far the subprograms correspond, so
12811 -- now check that any result types correspond.
12812
12813 if No (Anc_Formal) and then No (Act_Formal) then
12814 Subprograms_Correspond := True;
12815
12816 if Ekind (Act_Subp) = E_Function then
12817 Anc_F_Type := Etype (Anc_Subp);
12818 Act_F_Type := Etype (Act_Subp);
12819
12820 if Ekind (Anc_F_Type) =
12821 E_Anonymous_Access_Type
12822 then
12823 Anc_F_Type :=
12824 Designated_Type (Anc_F_Type);
12825
12826 if Ekind (Act_F_Type) =
12827 E_Anonymous_Access_Type
12828 then
12829 Act_F_Type :=
12830 Designated_Type (Act_F_Type);
12831 else
12832 Subprograms_Correspond := False;
12833 end if;
12834
12835 elsif
12836 Ekind (Act_F_Type)
12837 = E_Anonymous_Access_Type
12838 then
12839 Subprograms_Correspond := False;
12840 end if;
12841
12842 Anc_F_Type := Base_Type (Anc_F_Type);
12843 Act_F_Type := Base_Type (Act_F_Type);
12844
12845 -- Now either the result types must be
12846 -- the same or, if the result type is
12847 -- controlling, the result type of the
12848 -- actual primitive must descend from the
12849 -- result type of the ancestor primitive.
12850
12851 if Subprograms_Correspond
12852 and then Anc_F_Type /= Act_F_Type
12853 and then
12854 Has_Controlling_Result (Anc_Subp)
12855 and then not Is_Tagged_Ancestor
12856 (Anc_F_Type, Act_F_Type)
12857 then
12858 Subprograms_Correspond := False;
12859 end if;
12860 end if;
12861
12862 -- Found a matching subprogram belonging to
12863 -- formal ancestor type, so actual subprogram
12864 -- corresponds and this violates 3.9.3(9).
12865
12866 if Subprograms_Correspond then
12867 Error_Msg_NE
12868 ("abstract subprogram & overrides "
12869 & "nonabstract subprogram of ancestor",
12870 Actual, Act_Subp);
12871 end if;
12872 end if;
12873 end if;
12874 end if;
12875
12876 Next_Elmt (Act_Elmt);
12877 end loop;
12878 end if;
12879
12880 Next_Elmt (Gen_Elmt);
12881 end loop;
12882 end Check_Abstract_Primitives;
12883 end if;
12884
12885 -- Verify that limitedness matches. If parent is a limited
12886 -- interface then the generic formal is not unless declared
12887 -- explicitly so. If not declared limited, the actual cannot be
12888 -- limited (see AI05-0087).
12889
12890 -- Even though this AI is a binding interpretation, we enable the
12891 -- check only in Ada 2012 mode, because this improper construct
12892 -- shows up in user code and in existing B-tests.
12893
12894 if Is_Limited_Type (Act_T)
12895 and then not Is_Limited_Type (A_Gen_T)
12896 and then Ada_Version >= Ada_2012
12897 then
12898 if In_Instance then
12899 null;
12900 else
12901 Error_Msg_NE
12902 ("actual for non-limited & cannot be a limited type",
12903 Actual, Gen_T);
12904 Explain_Limited_Type (Act_T, Actual);
12905 Abandon_Instantiation (Actual);
12906 end if;
12907 end if;
12908 end Validate_Derived_Type_Instance;
12909
12910 ----------------------------------------
12911 -- Validate_Discriminated_Formal_Type --
12912 ----------------------------------------
12913
12914 procedure Validate_Discriminated_Formal_Type is
12915 Formal_Discr : Entity_Id;
12916 Actual_Discr : Entity_Id;
12917 Formal_Subt : Entity_Id;
12918
12919 begin
12920 if Has_Discriminants (A_Gen_T) then
12921 if not Has_Discriminants (Act_T) then
12922 Error_Msg_NE
12923 ("actual for & must have discriminants", Actual, Gen_T);
12924 Abandon_Instantiation (Actual);
12925
12926 elsif Is_Constrained (Act_T) then
12927 Error_Msg_NE
12928 ("actual for & must be unconstrained", Actual, Gen_T);
12929 Abandon_Instantiation (Actual);
12930
12931 else
12932 Formal_Discr := First_Discriminant (A_Gen_T);
12933 Actual_Discr := First_Discriminant (Act_T);
12934 while Formal_Discr /= Empty loop
12935 if Actual_Discr = Empty then
12936 Error_Msg_NE
12937 ("discriminants on actual do not match formal",
12938 Actual, Gen_T);
12939 Abandon_Instantiation (Actual);
12940 end if;
12941
12942 Formal_Subt := Get_Instance_Of (Etype (Formal_Discr));
12943
12944 -- Access discriminants match if designated types do
12945
12946 if Ekind (Base_Type (Formal_Subt)) = E_Anonymous_Access_Type
12947 and then (Ekind (Base_Type (Etype (Actual_Discr)))) =
12948 E_Anonymous_Access_Type
12949 and then
12950 Get_Instance_Of
12951 (Designated_Type (Base_Type (Formal_Subt))) =
12952 Designated_Type (Base_Type (Etype (Actual_Discr)))
12953 then
12954 null;
12955
12956 elsif Base_Type (Formal_Subt) /=
12957 Base_Type (Etype (Actual_Discr))
12958 then
12959 Error_Msg_NE
12960 ("types of actual discriminants must match formal",
12961 Actual, Gen_T);
12962 Abandon_Instantiation (Actual);
12963
12964 elsif not Subtypes_Statically_Match
12965 (Formal_Subt, Etype (Actual_Discr))
12966 and then Ada_Version >= Ada_95
12967 then
12968 Error_Msg_NE
12969 ("subtypes of actual discriminants must match formal",
12970 Actual, Gen_T);
12971 Abandon_Instantiation (Actual);
12972 end if;
12973
12974 Next_Discriminant (Formal_Discr);
12975 Next_Discriminant (Actual_Discr);
12976 end loop;
12977
12978 if Actual_Discr /= Empty then
12979 Error_Msg_NE
12980 ("discriminants on actual do not match formal",
12981 Actual, Gen_T);
12982 Abandon_Instantiation (Actual);
12983 end if;
12984 end if;
12985 end if;
12986 end Validate_Discriminated_Formal_Type;
12987
12988 ---------------------------------------
12989 -- Validate_Incomplete_Type_Instance --
12990 ---------------------------------------
12991
12992 procedure Validate_Incomplete_Type_Instance is
12993 begin
12994 if not Is_Tagged_Type (Act_T)
12995 and then Is_Tagged_Type (A_Gen_T)
12996 then
12997 Error_Msg_NE
12998 ("actual for & must be a tagged type", Actual, Gen_T);
12999 end if;
13000
13001 Validate_Discriminated_Formal_Type;
13002 end Validate_Incomplete_Type_Instance;
13003
13004 --------------------------------------
13005 -- Validate_Interface_Type_Instance --
13006 --------------------------------------
13007
13008 procedure Validate_Interface_Type_Instance is
13009 begin
13010 if not Is_Interface (Act_T) then
13011 Error_Msg_NE
13012 ("actual for formal interface type must be an interface",
13013 Actual, Gen_T);
13014
13015 elsif Is_Limited_Type (Act_T) /= Is_Limited_Type (A_Gen_T)
13016 or else Is_Task_Interface (A_Gen_T) /= Is_Task_Interface (Act_T)
13017 or else Is_Protected_Interface (A_Gen_T) /=
13018 Is_Protected_Interface (Act_T)
13019 or else Is_Synchronized_Interface (A_Gen_T) /=
13020 Is_Synchronized_Interface (Act_T)
13021 then
13022 Error_Msg_NE
13023 ("actual for interface& does not match (RM 12.5.5(4))",
13024 Actual, Gen_T);
13025 end if;
13026 end Validate_Interface_Type_Instance;
13027
13028 ------------------------------------
13029 -- Validate_Private_Type_Instance --
13030 ------------------------------------
13031
13032 procedure Validate_Private_Type_Instance is
13033 begin
13034 if Is_Limited_Type (Act_T)
13035 and then not Is_Limited_Type (A_Gen_T)
13036 then
13037 if In_Instance then
13038 null;
13039 else
13040 Error_Msg_NE
13041 ("actual for non-limited & cannot be a limited type", Actual,
13042 Gen_T);
13043 Explain_Limited_Type (Act_T, Actual);
13044 Abandon_Instantiation (Actual);
13045 end if;
13046
13047 elsif Known_To_Have_Preelab_Init (A_Gen_T)
13048 and then not Has_Preelaborable_Initialization (Act_T)
13049 then
13050 Error_Msg_NE
13051 ("actual for & must have preelaborable initialization", Actual,
13052 Gen_T);
13053
13054 elsif not Is_Definite_Subtype (Act_T)
13055 and then Is_Definite_Subtype (A_Gen_T)
13056 and then Ada_Version >= Ada_95
13057 then
13058 Error_Msg_NE
13059 ("actual for & must be a definite subtype", Actual, Gen_T);
13060
13061 elsif not Is_Tagged_Type (Act_T)
13062 and then Is_Tagged_Type (A_Gen_T)
13063 then
13064 Error_Msg_NE
13065 ("actual for & must be a tagged type", Actual, Gen_T);
13066 end if;
13067
13068 Validate_Discriminated_Formal_Type;
13069 Ancestor := Gen_T;
13070 end Validate_Private_Type_Instance;
13071
13072 -- Start of processing for Instantiate_Type
13073
13074 begin
13075 if Get_Instance_Of (A_Gen_T) /= A_Gen_T then
13076 Error_Msg_N ("duplicate instantiation of generic type", Actual);
13077 return New_List (Error);
13078
13079 elsif not Is_Entity_Name (Actual)
13080 or else not Is_Type (Entity (Actual))
13081 then
13082 Error_Msg_NE
13083 ("expect valid subtype mark to instantiate &", Actual, Gen_T);
13084 Abandon_Instantiation (Actual);
13085
13086 else
13087 Act_T := Entity (Actual);
13088
13089 -- Ada 2005 (AI-216): An Unchecked_Union subtype shall only be passed
13090 -- as a generic actual parameter if the corresponding formal type
13091 -- does not have a known_discriminant_part, or is a formal derived
13092 -- type that is an Unchecked_Union type.
13093
13094 if Is_Unchecked_Union (Base_Type (Act_T)) then
13095 if not Has_Discriminants (A_Gen_T)
13096 or else (Is_Derived_Type (A_Gen_T)
13097 and then Is_Unchecked_Union (A_Gen_T))
13098 then
13099 null;
13100 else
13101 Error_Msg_N ("unchecked union cannot be the actual for a "
13102 & "discriminated formal type", Act_T);
13103
13104 end if;
13105 end if;
13106
13107 -- Deal with fixed/floating restrictions
13108
13109 if Is_Floating_Point_Type (Act_T) then
13110 Check_Restriction (No_Floating_Point, Actual);
13111 elsif Is_Fixed_Point_Type (Act_T) then
13112 Check_Restriction (No_Fixed_Point, Actual);
13113 end if;
13114
13115 -- Deal with error of using incomplete type as generic actual.
13116 -- This includes limited views of a type, even if the non-limited
13117 -- view may be available.
13118
13119 if Ekind (Act_T) = E_Incomplete_Type
13120 or else (Is_Class_Wide_Type (Act_T)
13121 and then Ekind (Root_Type (Act_T)) = E_Incomplete_Type)
13122 then
13123 -- If the formal is an incomplete type, the actual can be
13124 -- incomplete as well.
13125
13126 if Ekind (A_Gen_T) = E_Incomplete_Type then
13127 null;
13128
13129 elsif Is_Class_Wide_Type (Act_T)
13130 or else No (Full_View (Act_T))
13131 then
13132 Error_Msg_N ("premature use of incomplete type", Actual);
13133 Abandon_Instantiation (Actual);
13134 else
13135 Act_T := Full_View (Act_T);
13136 Set_Entity (Actual, Act_T);
13137
13138 if Has_Private_Component (Act_T) then
13139 Error_Msg_N
13140 ("premature use of type with private component", Actual);
13141 end if;
13142 end if;
13143
13144 -- Deal with error of premature use of private type as generic actual
13145
13146 elsif Is_Private_Type (Act_T)
13147 and then Is_Private_Type (Base_Type (Act_T))
13148 and then not Is_Generic_Type (Act_T)
13149 and then not Is_Derived_Type (Act_T)
13150 and then No (Full_View (Root_Type (Act_T)))
13151 then
13152 -- If the formal is an incomplete type, the actual can be
13153 -- private or incomplete as well.
13154
13155 if Ekind (A_Gen_T) = E_Incomplete_Type then
13156 null;
13157 else
13158 Error_Msg_N ("premature use of private type", Actual);
13159 end if;
13160
13161 elsif Has_Private_Component (Act_T) then
13162 Error_Msg_N
13163 ("premature use of type with private component", Actual);
13164 end if;
13165
13166 Set_Instance_Of (A_Gen_T, Act_T);
13167
13168 -- If the type is generic, the class-wide type may also be used
13169
13170 if Is_Tagged_Type (A_Gen_T)
13171 and then Is_Tagged_Type (Act_T)
13172 and then not Is_Class_Wide_Type (A_Gen_T)
13173 then
13174 Set_Instance_Of (Class_Wide_Type (A_Gen_T),
13175 Class_Wide_Type (Act_T));
13176 end if;
13177
13178 if not Is_Abstract_Type (A_Gen_T)
13179 and then Is_Abstract_Type (Act_T)
13180 then
13181 Error_Msg_N
13182 ("actual of non-abstract formal cannot be abstract", Actual);
13183 end if;
13184
13185 -- A generic scalar type is a first subtype for which we generate
13186 -- an anonymous base type. Indicate that the instance of this base
13187 -- is the base type of the actual.
13188
13189 if Is_Scalar_Type (A_Gen_T) then
13190 Set_Instance_Of (Etype (A_Gen_T), Etype (Act_T));
13191 end if;
13192 end if;
13193
13194 if Error_Posted (Act_T) then
13195 null;
13196 else
13197 case Nkind (Def) is
13198 when N_Formal_Private_Type_Definition =>
13199 Validate_Private_Type_Instance;
13200
13201 when N_Formal_Incomplete_Type_Definition =>
13202 Validate_Incomplete_Type_Instance;
13203
13204 when N_Formal_Derived_Type_Definition =>
13205 Validate_Derived_Type_Instance;
13206
13207 when N_Formal_Discrete_Type_Definition =>
13208 if not Is_Discrete_Type (Act_T) then
13209 Error_Msg_NE
13210 ("expect discrete type in instantiation of&",
13211 Actual, Gen_T);
13212 Abandon_Instantiation (Actual);
13213 end if;
13214
13215 Diagnose_Predicated_Actual;
13216
13217 when N_Formal_Signed_Integer_Type_Definition =>
13218 if not Is_Signed_Integer_Type (Act_T) then
13219 Error_Msg_NE
13220 ("expect signed integer type in instantiation of&",
13221 Actual, Gen_T);
13222 Abandon_Instantiation (Actual);
13223 end if;
13224
13225 Diagnose_Predicated_Actual;
13226
13227 when N_Formal_Modular_Type_Definition =>
13228 if not Is_Modular_Integer_Type (Act_T) then
13229 Error_Msg_NE
13230 ("expect modular type in instantiation of &",
13231 Actual, Gen_T);
13232 Abandon_Instantiation (Actual);
13233 end if;
13234
13235 Diagnose_Predicated_Actual;
13236
13237 when N_Formal_Floating_Point_Definition =>
13238 if not Is_Floating_Point_Type (Act_T) then
13239 Error_Msg_NE
13240 ("expect float type in instantiation of &", Actual, Gen_T);
13241 Abandon_Instantiation (Actual);
13242 end if;
13243
13244 when N_Formal_Ordinary_Fixed_Point_Definition =>
13245 if not Is_Ordinary_Fixed_Point_Type (Act_T) then
13246 Error_Msg_NE
13247 ("expect ordinary fixed point type in instantiation of &",
13248 Actual, Gen_T);
13249 Abandon_Instantiation (Actual);
13250 end if;
13251
13252 when N_Formal_Decimal_Fixed_Point_Definition =>
13253 if not Is_Decimal_Fixed_Point_Type (Act_T) then
13254 Error_Msg_NE
13255 ("expect decimal type in instantiation of &",
13256 Actual, Gen_T);
13257 Abandon_Instantiation (Actual);
13258 end if;
13259
13260 when N_Array_Type_Definition =>
13261 Validate_Array_Type_Instance;
13262
13263 when N_Access_To_Object_Definition =>
13264 Validate_Access_Type_Instance;
13265
13266 when N_Access_Function_Definition
13267 | N_Access_Procedure_Definition
13268 =>
13269 Validate_Access_Subprogram_Instance;
13270
13271 when N_Record_Definition =>
13272 Validate_Interface_Type_Instance;
13273
13274 when N_Derived_Type_Definition =>
13275 Validate_Derived_Interface_Type_Instance;
13276
13277 when others =>
13278 raise Program_Error;
13279 end case;
13280 end if;
13281
13282 Subt := New_Copy (Gen_T);
13283
13284 -- Use adjusted sloc of subtype name as the location for other nodes in
13285 -- the subtype declaration.
13286
13287 Loc := Sloc (Subt);
13288
13289 Decl_Node :=
13290 Make_Subtype_Declaration (Loc,
13291 Defining_Identifier => Subt,
13292 Subtype_Indication => New_Occurrence_Of (Act_T, Loc));
13293
13294 if Is_Private_Type (Act_T) then
13295 Set_Has_Private_View (Subtype_Indication (Decl_Node));
13296
13297 elsif Is_Access_Type (Act_T)
13298 and then Is_Private_Type (Designated_Type (Act_T))
13299 then
13300 Set_Has_Private_View (Subtype_Indication (Decl_Node));
13301 end if;
13302
13303 -- In Ada 2012 the actual may be a limited view. Indicate that
13304 -- the local subtype must be treated as such.
13305
13306 if From_Limited_With (Act_T) then
13307 Set_Ekind (Subt, E_Incomplete_Subtype);
13308 Set_From_Limited_With (Subt);
13309 end if;
13310
13311 Decl_Nodes := New_List (Decl_Node);
13312
13313 -- Flag actual derived types so their elaboration produces the
13314 -- appropriate renamings for the primitive operations of the ancestor.
13315 -- Flag actual for formal private types as well, to determine whether
13316 -- operations in the private part may override inherited operations.
13317 -- If the formal has an interface list, the ancestor is not the
13318 -- parent, but the analyzed formal that includes the interface
13319 -- operations of all its progenitors.
13320
13321 -- Same treatment for formal private types, so we can check whether the
13322 -- type is tagged limited when validating derivations in the private
13323 -- part. (See AI05-096).
13324
13325 if Nkind (Def) = N_Formal_Derived_Type_Definition then
13326 if Present (Interface_List (Def)) then
13327 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
13328 else
13329 Set_Generic_Parent_Type (Decl_Node, Ancestor);
13330 end if;
13331
13332 elsif Nkind_In (Def, N_Formal_Private_Type_Definition,
13333 N_Formal_Incomplete_Type_Definition)
13334 then
13335 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
13336 end if;
13337
13338 -- If the actual is a synchronized type that implements an interface,
13339 -- the primitive operations are attached to the corresponding record,
13340 -- and we have to treat it as an additional generic actual, so that its
13341 -- primitive operations become visible in the instance. The task or
13342 -- protected type itself does not carry primitive operations.
13343
13344 if Is_Concurrent_Type (Act_T)
13345 and then Is_Tagged_Type (Act_T)
13346 and then Present (Corresponding_Record_Type (Act_T))
13347 and then Present (Ancestor)
13348 and then Is_Interface (Ancestor)
13349 then
13350 declare
13351 Corr_Rec : constant Entity_Id :=
13352 Corresponding_Record_Type (Act_T);
13353 New_Corr : Entity_Id;
13354 Corr_Decl : Node_Id;
13355
13356 begin
13357 New_Corr := Make_Temporary (Loc, 'S');
13358 Corr_Decl :=
13359 Make_Subtype_Declaration (Loc,
13360 Defining_Identifier => New_Corr,
13361 Subtype_Indication =>
13362 New_Occurrence_Of (Corr_Rec, Loc));
13363 Append_To (Decl_Nodes, Corr_Decl);
13364
13365 if Ekind (Act_T) = E_Task_Type then
13366 Set_Ekind (Subt, E_Task_Subtype);
13367 else
13368 Set_Ekind (Subt, E_Protected_Subtype);
13369 end if;
13370
13371 Set_Corresponding_Record_Type (Subt, Corr_Rec);
13372 Set_Generic_Parent_Type (Corr_Decl, Ancestor);
13373 Set_Generic_Parent_Type (Decl_Node, Empty);
13374 end;
13375 end if;
13376
13377 -- For a floating-point type, capture dimension info if any, because
13378 -- the generated subtype declaration does not come from source and
13379 -- will not process dimensions.
13380
13381 if Is_Floating_Point_Type (Act_T) then
13382 Copy_Dimensions (Act_T, Subt);
13383 end if;
13384
13385 return Decl_Nodes;
13386 end Instantiate_Type;
13387
13388 ---------------------
13389 -- Is_In_Main_Unit --
13390 ---------------------
13391
13392 function Is_In_Main_Unit (N : Node_Id) return Boolean is
13393 Unum : constant Unit_Number_Type := Get_Source_Unit (N);
13394 Current_Unit : Node_Id;
13395
13396 begin
13397 if Unum = Main_Unit then
13398 return True;
13399
13400 -- If the current unit is a subunit then it is either the main unit or
13401 -- is being compiled as part of the main unit.
13402
13403 elsif Nkind (N) = N_Compilation_Unit then
13404 return Nkind (Unit (N)) = N_Subunit;
13405 end if;
13406
13407 Current_Unit := Parent (N);
13408 while Present (Current_Unit)
13409 and then Nkind (Current_Unit) /= N_Compilation_Unit
13410 loop
13411 Current_Unit := Parent (Current_Unit);
13412 end loop;
13413
13414 -- The instantiation node is in the main unit, or else the current node
13415 -- (perhaps as the result of nested instantiations) is in the main unit,
13416 -- or in the declaration of the main unit, which in this last case must
13417 -- be a body.
13418
13419 return
13420 Current_Unit = Cunit (Main_Unit)
13421 or else Current_Unit = Library_Unit (Cunit (Main_Unit))
13422 or else (Present (Current_Unit)
13423 and then Present (Library_Unit (Current_Unit))
13424 and then Is_In_Main_Unit (Library_Unit (Current_Unit)));
13425 end Is_In_Main_Unit;
13426
13427 ----------------------------
13428 -- Load_Parent_Of_Generic --
13429 ----------------------------
13430
13431 procedure Load_Parent_Of_Generic
13432 (N : Node_Id;
13433 Spec : Node_Id;
13434 Body_Optional : Boolean := False)
13435 is
13436 Comp_Unit : constant Node_Id := Cunit (Get_Source_Unit (Spec));
13437 Saved_Style_Check : constant Boolean := Style_Check;
13438 Saved_Warnings : constant Warning_Record := Save_Warnings;
13439 True_Parent : Node_Id;
13440 Inst_Node : Node_Id;
13441 OK : Boolean;
13442 Previous_Instances : constant Elist_Id := New_Elmt_List;
13443
13444 procedure Collect_Previous_Instances (Decls : List_Id);
13445 -- Collect all instantiations in the given list of declarations, that
13446 -- precede the generic that we need to load. If the bodies of these
13447 -- instantiations are available, we must analyze them, to ensure that
13448 -- the public symbols generated are the same when the unit is compiled
13449 -- to generate code, and when it is compiled in the context of a unit
13450 -- that needs a particular nested instance. This process is applied to
13451 -- both package and subprogram instances.
13452
13453 --------------------------------
13454 -- Collect_Previous_Instances --
13455 --------------------------------
13456
13457 procedure Collect_Previous_Instances (Decls : List_Id) is
13458 Decl : Node_Id;
13459
13460 begin
13461 Decl := First (Decls);
13462 while Present (Decl) loop
13463 if Sloc (Decl) >= Sloc (Inst_Node) then
13464 return;
13465
13466 -- If Decl is an instantiation, then record it as requiring
13467 -- instantiation of the corresponding body, except if it is an
13468 -- abbreviated instantiation generated internally for conformance
13469 -- checking purposes only for the case of a formal package
13470 -- declared without a box (see Instantiate_Formal_Package). Such
13471 -- an instantiation does not generate any code (the actual code
13472 -- comes from actual) and thus does not need to be analyzed here.
13473 -- If the instantiation appears with a generic package body it is
13474 -- not analyzed here either.
13475
13476 elsif Nkind (Decl) = N_Package_Instantiation
13477 and then not Is_Internal (Defining_Entity (Decl))
13478 then
13479 Append_Elmt (Decl, Previous_Instances);
13480
13481 -- For a subprogram instantiation, omit instantiations intrinsic
13482 -- operations (Unchecked_Conversions, etc.) that have no bodies.
13483
13484 elsif Nkind_In (Decl, N_Function_Instantiation,
13485 N_Procedure_Instantiation)
13486 and then not Is_Intrinsic_Subprogram (Entity (Name (Decl)))
13487 then
13488 Append_Elmt (Decl, Previous_Instances);
13489
13490 elsif Nkind (Decl) = N_Package_Declaration then
13491 Collect_Previous_Instances
13492 (Visible_Declarations (Specification (Decl)));
13493 Collect_Previous_Instances
13494 (Private_Declarations (Specification (Decl)));
13495
13496 -- Previous non-generic bodies may contain instances as well
13497
13498 elsif Nkind (Decl) = N_Package_Body
13499 and then Ekind (Corresponding_Spec (Decl)) /= E_Generic_Package
13500 then
13501 Collect_Previous_Instances (Declarations (Decl));
13502
13503 elsif Nkind (Decl) = N_Subprogram_Body
13504 and then not Acts_As_Spec (Decl)
13505 and then not Is_Generic_Subprogram (Corresponding_Spec (Decl))
13506 then
13507 Collect_Previous_Instances (Declarations (Decl));
13508 end if;
13509
13510 Next (Decl);
13511 end loop;
13512 end Collect_Previous_Instances;
13513
13514 -- Start of processing for Load_Parent_Of_Generic
13515
13516 begin
13517 if not In_Same_Source_Unit (N, Spec)
13518 or else Nkind (Unit (Comp_Unit)) = N_Package_Declaration
13519 or else (Nkind (Unit (Comp_Unit)) = N_Package_Body
13520 and then not Is_In_Main_Unit (Spec))
13521 then
13522 -- Find body of parent of spec, and analyze it. A special case arises
13523 -- when the parent is an instantiation, that is to say when we are
13524 -- currently instantiating a nested generic. In that case, there is
13525 -- no separate file for the body of the enclosing instance. Instead,
13526 -- the enclosing body must be instantiated as if it were a pending
13527 -- instantiation, in order to produce the body for the nested generic
13528 -- we require now. Note that in that case the generic may be defined
13529 -- in a package body, the instance defined in the same package body,
13530 -- and the original enclosing body may not be in the main unit.
13531
13532 Inst_Node := Empty;
13533
13534 True_Parent := Parent (Spec);
13535 while Present (True_Parent)
13536 and then Nkind (True_Parent) /= N_Compilation_Unit
13537 loop
13538 if Nkind (True_Parent) = N_Package_Declaration
13539 and then
13540 Nkind (Original_Node (True_Parent)) = N_Package_Instantiation
13541 then
13542 -- Parent is a compilation unit that is an instantiation.
13543 -- Instantiation node has been replaced with package decl.
13544
13545 Inst_Node := Original_Node (True_Parent);
13546 exit;
13547
13548 elsif Nkind (True_Parent) = N_Package_Declaration
13549 and then Present (Generic_Parent (Specification (True_Parent)))
13550 and then Nkind (Parent (True_Parent)) /= N_Compilation_Unit
13551 then
13552 -- Parent is an instantiation within another specification.
13553 -- Declaration for instance has been inserted before original
13554 -- instantiation node. A direct link would be preferable?
13555
13556 Inst_Node := Next (True_Parent);
13557 while Present (Inst_Node)
13558 and then Nkind (Inst_Node) /= N_Package_Instantiation
13559 loop
13560 Next (Inst_Node);
13561 end loop;
13562
13563 -- If the instance appears within a generic, and the generic
13564 -- unit is defined within a formal package of the enclosing
13565 -- generic, there is no generic body available, and none
13566 -- needed. A more precise test should be used ???
13567
13568 if No (Inst_Node) then
13569 return;
13570 end if;
13571
13572 exit;
13573
13574 else
13575 True_Parent := Parent (True_Parent);
13576 end if;
13577 end loop;
13578
13579 -- Case where we are currently instantiating a nested generic
13580
13581 if Present (Inst_Node) then
13582 if Nkind (Parent (True_Parent)) = N_Compilation_Unit then
13583
13584 -- Instantiation node and declaration of instantiated package
13585 -- were exchanged when only the declaration was needed.
13586 -- Restore instantiation node before proceeding with body.
13587
13588 Set_Unit (Parent (True_Parent), Inst_Node);
13589 end if;
13590
13591 -- Now complete instantiation of enclosing body, if it appears in
13592 -- some other unit. If it appears in the current unit, the body
13593 -- will have been instantiated already.
13594
13595 if No (Corresponding_Body (Instance_Spec (Inst_Node))) then
13596
13597 -- We need to determine the expander mode to instantiate the
13598 -- enclosing body. Because the generic body we need may use
13599 -- global entities declared in the enclosing package (including
13600 -- aggregates) it is in general necessary to compile this body
13601 -- with expansion enabled, except if we are within a generic
13602 -- package, in which case the usual generic rule applies.
13603
13604 declare
13605 Exp_Status : Boolean := True;
13606 Scop : Entity_Id;
13607
13608 begin
13609 -- Loop through scopes looking for generic package
13610
13611 Scop := Scope (Defining_Entity (Instance_Spec (Inst_Node)));
13612 while Present (Scop)
13613 and then Scop /= Standard_Standard
13614 loop
13615 if Ekind (Scop) = E_Generic_Package then
13616 Exp_Status := False;
13617 exit;
13618 end if;
13619
13620 Scop := Scope (Scop);
13621 end loop;
13622
13623 -- Collect previous instantiations in the unit that contains
13624 -- the desired generic.
13625
13626 if Nkind (Parent (True_Parent)) /= N_Compilation_Unit
13627 and then not Body_Optional
13628 then
13629 declare
13630 Decl : Elmt_Id;
13631 Info : Pending_Body_Info;
13632 Par : Node_Id;
13633
13634 begin
13635 Par := Parent (Inst_Node);
13636 while Present (Par) loop
13637 exit when Nkind (Parent (Par)) = N_Compilation_Unit;
13638 Par := Parent (Par);
13639 end loop;
13640
13641 pragma Assert (Present (Par));
13642
13643 if Nkind (Par) = N_Package_Body then
13644 Collect_Previous_Instances (Declarations (Par));
13645
13646 elsif Nkind (Par) = N_Package_Declaration then
13647 Collect_Previous_Instances
13648 (Visible_Declarations (Specification (Par)));
13649 Collect_Previous_Instances
13650 (Private_Declarations (Specification (Par)));
13651
13652 else
13653 -- Enclosing unit is a subprogram body. In this
13654 -- case all instance bodies are processed in order
13655 -- and there is no need to collect them separately.
13656
13657 null;
13658 end if;
13659
13660 Decl := First_Elmt (Previous_Instances);
13661 while Present (Decl) loop
13662 Info :=
13663 (Inst_Node => Node (Decl),
13664 Act_Decl =>
13665 Instance_Spec (Node (Decl)),
13666 Expander_Status => Exp_Status,
13667 Current_Sem_Unit =>
13668 Get_Code_Unit (Sloc (Node (Decl))),
13669 Scope_Suppress => Scope_Suppress,
13670 Local_Suppress_Stack_Top =>
13671 Local_Suppress_Stack_Top,
13672 Version => Ada_Version,
13673 Version_Pragma => Ada_Version_Pragma,
13674 Warnings => Save_Warnings,
13675 SPARK_Mode => SPARK_Mode,
13676 SPARK_Mode_Pragma => SPARK_Mode_Pragma);
13677
13678 -- Package instance
13679
13680 if Nkind (Node (Decl)) = N_Package_Instantiation
13681 then
13682 Instantiate_Package_Body
13683 (Info, Body_Optional => True);
13684
13685 -- Subprogram instance
13686
13687 else
13688 -- The instance_spec is in the wrapper package,
13689 -- usually followed by its local renaming
13690 -- declaration. See Build_Subprogram_Renaming
13691 -- for details. If the instance carries aspects,
13692 -- these result in the corresponding pragmas,
13693 -- inserted after the subprogram declaration.
13694 -- They must be skipped as well when retrieving
13695 -- the desired spec. Some of them may have been
13696 -- rewritten as null statements.
13697 -- A direct link would be more robust ???
13698
13699 declare
13700 Decl : Node_Id :=
13701 (Last (Visible_Declarations
13702 (Specification (Info.Act_Decl))));
13703 begin
13704 while Nkind_In (Decl,
13705 N_Null_Statement,
13706 N_Pragma,
13707 N_Subprogram_Renaming_Declaration)
13708 loop
13709 Decl := Prev (Decl);
13710 end loop;
13711
13712 Info.Act_Decl := Decl;
13713 end;
13714
13715 Instantiate_Subprogram_Body
13716 (Info, Body_Optional => True);
13717 end if;
13718
13719 Next_Elmt (Decl);
13720 end loop;
13721 end;
13722 end if;
13723
13724 Instantiate_Package_Body
13725 (Body_Info =>
13726 ((Inst_Node => Inst_Node,
13727 Act_Decl => True_Parent,
13728 Expander_Status => Exp_Status,
13729 Current_Sem_Unit => Get_Code_Unit
13730 (Sloc (Inst_Node)),
13731 Scope_Suppress => Scope_Suppress,
13732 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
13733 Version => Ada_Version,
13734 Version_Pragma => Ada_Version_Pragma,
13735 Warnings => Save_Warnings,
13736 SPARK_Mode => SPARK_Mode,
13737 SPARK_Mode_Pragma => SPARK_Mode_Pragma)),
13738 Body_Optional => Body_Optional);
13739 end;
13740 end if;
13741
13742 -- Case where we are not instantiating a nested generic
13743
13744 else
13745 Opt.Style_Check := False;
13746 Expander_Mode_Save_And_Set (True);
13747 Load_Needed_Body (Comp_Unit, OK);
13748 Opt.Style_Check := Saved_Style_Check;
13749 Restore_Warnings (Saved_Warnings);
13750 Expander_Mode_Restore;
13751
13752 if not OK
13753 and then Unit_Requires_Body (Defining_Entity (Spec))
13754 and then not Body_Optional
13755 then
13756 declare
13757 Bname : constant Unit_Name_Type :=
13758 Get_Body_Name (Get_Unit_Name (Unit (Comp_Unit)));
13759
13760 begin
13761 -- In CodePeer mode, the missing body may make the analysis
13762 -- incomplete, but we do not treat it as fatal.
13763
13764 if CodePeer_Mode then
13765 return;
13766
13767 else
13768 Error_Msg_Unit_1 := Bname;
13769 Error_Msg_N ("this instantiation requires$!", N);
13770 Error_Msg_File_1 :=
13771 Get_File_Name (Bname, Subunit => False);
13772 Error_Msg_N ("\but file{ was not found!", N);
13773 raise Unrecoverable_Error;
13774 end if;
13775 end;
13776 end if;
13777 end if;
13778 end if;
13779
13780 -- If loading parent of the generic caused an instantiation circularity,
13781 -- we abandon compilation at this point, because otherwise in some cases
13782 -- we get into trouble with infinite recursions after this point.
13783
13784 if Circularity_Detected then
13785 raise Unrecoverable_Error;
13786 end if;
13787 end Load_Parent_Of_Generic;
13788
13789 ---------------------------------
13790 -- Map_Formal_Package_Entities --
13791 ---------------------------------
13792
13793 procedure Map_Formal_Package_Entities (Form : Entity_Id; Act : Entity_Id) is
13794 E1 : Entity_Id;
13795 E2 : Entity_Id;
13796
13797 begin
13798 Set_Instance_Of (Form, Act);
13799
13800 -- Traverse formal and actual package to map the corresponding entities.
13801 -- We skip over internal entities that may be generated during semantic
13802 -- analysis, and find the matching entities by name, given that they
13803 -- must appear in the same order.
13804
13805 E1 := First_Entity (Form);
13806 E2 := First_Entity (Act);
13807 while Present (E1) and then E1 /= First_Private_Entity (Form) loop
13808 -- Could this test be a single condition??? Seems like it could, and
13809 -- isn't FPE (Form) a constant anyway???
13810
13811 if not Is_Internal (E1)
13812 and then Present (Parent (E1))
13813 and then not Is_Class_Wide_Type (E1)
13814 and then not Is_Internal_Name (Chars (E1))
13815 then
13816 while Present (E2) and then Chars (E2) /= Chars (E1) loop
13817 Next_Entity (E2);
13818 end loop;
13819
13820 if No (E2) then
13821 exit;
13822 else
13823 Set_Instance_Of (E1, E2);
13824
13825 if Is_Type (E1) and then Is_Tagged_Type (E2) then
13826 Set_Instance_Of (Class_Wide_Type (E1), Class_Wide_Type (E2));
13827 end if;
13828
13829 if Is_Constrained (E1) then
13830 Set_Instance_Of (Base_Type (E1), Base_Type (E2));
13831 end if;
13832
13833 if Ekind (E1) = E_Package and then No (Renamed_Object (E1)) then
13834 Map_Formal_Package_Entities (E1, E2);
13835 end if;
13836 end if;
13837 end if;
13838
13839 Next_Entity (E1);
13840 end loop;
13841 end Map_Formal_Package_Entities;
13842
13843 -----------------------
13844 -- Move_Freeze_Nodes --
13845 -----------------------
13846
13847 procedure Move_Freeze_Nodes
13848 (Out_Of : Entity_Id;
13849 After : Node_Id;
13850 L : List_Id)
13851 is
13852 Decl : Node_Id;
13853 Next_Decl : Node_Id;
13854 Next_Node : Node_Id := After;
13855 Spec : Node_Id;
13856
13857 function Is_Outer_Type (T : Entity_Id) return Boolean;
13858 -- Check whether entity is declared in a scope external to that of the
13859 -- generic unit.
13860
13861 -------------------
13862 -- Is_Outer_Type --
13863 -------------------
13864
13865 function Is_Outer_Type (T : Entity_Id) return Boolean is
13866 Scop : Entity_Id := Scope (T);
13867
13868 begin
13869 if Scope_Depth (Scop) < Scope_Depth (Out_Of) then
13870 return True;
13871
13872 else
13873 while Scop /= Standard_Standard loop
13874 if Scop = Out_Of then
13875 return False;
13876 else
13877 Scop := Scope (Scop);
13878 end if;
13879 end loop;
13880
13881 return True;
13882 end if;
13883 end Is_Outer_Type;
13884
13885 -- Start of processing for Move_Freeze_Nodes
13886
13887 begin
13888 if No (L) then
13889 return;
13890 end if;
13891
13892 -- First remove the freeze nodes that may appear before all other
13893 -- declarations.
13894
13895 Decl := First (L);
13896 while Present (Decl)
13897 and then Nkind (Decl) = N_Freeze_Entity
13898 and then Is_Outer_Type (Entity (Decl))
13899 loop
13900 Decl := Remove_Head (L);
13901 Insert_After (Next_Node, Decl);
13902 Set_Analyzed (Decl, False);
13903 Next_Node := Decl;
13904 Decl := First (L);
13905 end loop;
13906
13907 -- Next scan the list of declarations and remove each freeze node that
13908 -- appears ahead of the current node.
13909
13910 while Present (Decl) loop
13911 while Present (Next (Decl))
13912 and then Nkind (Next (Decl)) = N_Freeze_Entity
13913 and then Is_Outer_Type (Entity (Next (Decl)))
13914 loop
13915 Next_Decl := Remove_Next (Decl);
13916 Insert_After (Next_Node, Next_Decl);
13917 Set_Analyzed (Next_Decl, False);
13918 Next_Node := Next_Decl;
13919 end loop;
13920
13921 -- If the declaration is a nested package or concurrent type, then
13922 -- recurse. Nested generic packages will have been processed from the
13923 -- inside out.
13924
13925 case Nkind (Decl) is
13926 when N_Package_Declaration =>
13927 Spec := Specification (Decl);
13928
13929 when N_Task_Type_Declaration =>
13930 Spec := Task_Definition (Decl);
13931
13932 when N_Protected_Type_Declaration =>
13933 Spec := Protected_Definition (Decl);
13934
13935 when others =>
13936 Spec := Empty;
13937 end case;
13938
13939 if Present (Spec) then
13940 Move_Freeze_Nodes (Out_Of, Next_Node, Visible_Declarations (Spec));
13941 Move_Freeze_Nodes (Out_Of, Next_Node, Private_Declarations (Spec));
13942 end if;
13943
13944 Next (Decl);
13945 end loop;
13946 end Move_Freeze_Nodes;
13947
13948 ----------------
13949 -- Next_Assoc --
13950 ----------------
13951
13952 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr is
13953 begin
13954 return Generic_Renamings.Table (E).Next_In_HTable;
13955 end Next_Assoc;
13956
13957 ------------------------
13958 -- Preanalyze_Actuals --
13959 ------------------------
13960
13961 procedure Preanalyze_Actuals (N : Node_Id; Inst : Entity_Id := Empty) is
13962 Assoc : Node_Id;
13963 Act : Node_Id;
13964 Errs : constant Nat := Serious_Errors_Detected;
13965
13966 Cur : Entity_Id := Empty;
13967 -- Current homograph of the instance name
13968
13969 Vis : Boolean := False;
13970 -- Saved visibility status of the current homograph
13971
13972 begin
13973 Assoc := First (Generic_Associations (N));
13974
13975 -- If the instance is a child unit, its name may hide an outer homonym,
13976 -- so make it invisible to perform name resolution on the actuals.
13977
13978 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name
13979 and then Present
13980 (Current_Entity (Defining_Identifier (Defining_Unit_Name (N))))
13981 then
13982 Cur := Current_Entity (Defining_Identifier (Defining_Unit_Name (N)));
13983
13984 if Is_Compilation_Unit (Cur) then
13985 Vis := Is_Immediately_Visible (Cur);
13986 Set_Is_Immediately_Visible (Cur, False);
13987 else
13988 Cur := Empty;
13989 end if;
13990 end if;
13991
13992 while Present (Assoc) loop
13993 if Nkind (Assoc) /= N_Others_Choice then
13994 Act := Explicit_Generic_Actual_Parameter (Assoc);
13995
13996 -- Within a nested instantiation, a defaulted actual is an empty
13997 -- association, so nothing to analyze. If the subprogram actual
13998 -- is an attribute, analyze prefix only, because actual is not a
13999 -- complete attribute reference.
14000
14001 -- If actual is an allocator, analyze expression only. The full
14002 -- analysis can generate code, and if instance is a compilation
14003 -- unit we have to wait until the package instance is installed
14004 -- to have a proper place to insert this code.
14005
14006 -- String literals may be operators, but at this point we do not
14007 -- know whether the actual is a formal subprogram or a string.
14008
14009 if No (Act) then
14010 null;
14011
14012 elsif Nkind (Act) = N_Attribute_Reference then
14013 Analyze (Prefix (Act));
14014
14015 elsif Nkind (Act) = N_Explicit_Dereference then
14016 Analyze (Prefix (Act));
14017
14018 elsif Nkind (Act) = N_Allocator then
14019 declare
14020 Expr : constant Node_Id := Expression (Act);
14021
14022 begin
14023 if Nkind (Expr) = N_Subtype_Indication then
14024 Analyze (Subtype_Mark (Expr));
14025
14026 -- Analyze separately each discriminant constraint, when
14027 -- given with a named association.
14028
14029 declare
14030 Constr : Node_Id;
14031
14032 begin
14033 Constr := First (Constraints (Constraint (Expr)));
14034 while Present (Constr) loop
14035 if Nkind (Constr) = N_Discriminant_Association then
14036 Analyze (Expression (Constr));
14037 else
14038 Analyze (Constr);
14039 end if;
14040
14041 Next (Constr);
14042 end loop;
14043 end;
14044
14045 else
14046 Analyze (Expr);
14047 end if;
14048 end;
14049
14050 elsif Nkind (Act) /= N_Operator_Symbol then
14051 Analyze (Act);
14052
14053 -- Within a package instance, mark actuals that are limited
14054 -- views, so their use can be moved to the body of the
14055 -- enclosing unit.
14056
14057 if Is_Entity_Name (Act)
14058 and then Is_Type (Entity (Act))
14059 and then From_Limited_With (Entity (Act))
14060 and then Present (Inst)
14061 then
14062 Append_Elmt (Entity (Act), Incomplete_Actuals (Inst));
14063 end if;
14064 end if;
14065
14066 if Errs /= Serious_Errors_Detected then
14067
14068 -- Do a minimal analysis of the generic, to prevent spurious
14069 -- warnings complaining about the generic being unreferenced,
14070 -- before abandoning the instantiation.
14071
14072 Analyze (Name (N));
14073
14074 if Is_Entity_Name (Name (N))
14075 and then Etype (Name (N)) /= Any_Type
14076 then
14077 Generate_Reference (Entity (Name (N)), Name (N));
14078 Set_Is_Instantiated (Entity (Name (N)));
14079 end if;
14080
14081 if Present (Cur) then
14082
14083 -- For the case of a child instance hiding an outer homonym,
14084 -- provide additional warning which might explain the error.
14085
14086 Set_Is_Immediately_Visible (Cur, Vis);
14087 Error_Msg_NE
14088 ("& hides outer unit with the same name??",
14089 N, Defining_Unit_Name (N));
14090 end if;
14091
14092 Abandon_Instantiation (Act);
14093 end if;
14094 end if;
14095
14096 Next (Assoc);
14097 end loop;
14098
14099 if Present (Cur) then
14100 Set_Is_Immediately_Visible (Cur, Vis);
14101 end if;
14102 end Preanalyze_Actuals;
14103
14104 -------------------------------
14105 -- Provide_Completing_Bodies --
14106 -------------------------------
14107
14108 procedure Provide_Completing_Bodies (N : Node_Id) is
14109 procedure Build_Completing_Body (Subp_Decl : Node_Id);
14110 -- Generate the completing body for subprogram declaration Subp_Decl
14111
14112 procedure Provide_Completing_Bodies_In (Decls : List_Id);
14113 -- Generating completing bodies for all subprograms found in declarative
14114 -- list Decls.
14115
14116 ---------------------------
14117 -- Build_Completing_Body --
14118 ---------------------------
14119
14120 procedure Build_Completing_Body (Subp_Decl : Node_Id) is
14121 Loc : constant Source_Ptr := Sloc (Subp_Decl);
14122 Subp_Id : constant Entity_Id := Defining_Entity (Subp_Decl);
14123 Spec : Node_Id;
14124
14125 begin
14126 -- Nothing to do if the subprogram already has a completing body
14127
14128 if Present (Corresponding_Body (Subp_Decl)) then
14129 return;
14130
14131 -- Mark the function as having a valid return statement even though
14132 -- the body contains a single raise statement.
14133
14134 elsif Ekind (Subp_Id) = E_Function then
14135 Set_Return_Present (Subp_Id);
14136 end if;
14137
14138 -- Clone the specification to obtain new entities and reset the only
14139 -- semantic field.
14140
14141 Spec := Copy_Subprogram_Spec (Specification (Subp_Decl));
14142 Set_Generic_Parent (Spec, Empty);
14143
14144 -- Generate:
14145 -- function Func ... return ... is
14146 -- <or>
14147 -- procedure Proc ... is
14148 -- begin
14149 -- raise Program_Error with "access before elaboration";
14150 -- edn Proc;
14151
14152 Insert_After_And_Analyze (Subp_Decl,
14153 Make_Subprogram_Body (Loc,
14154 Specification => Spec,
14155 Declarations => New_List,
14156 Handled_Statement_Sequence =>
14157 Make_Handled_Sequence_Of_Statements (Loc,
14158 Statements => New_List (
14159 Make_Raise_Program_Error (Loc,
14160 Reason => PE_Access_Before_Elaboration)))));
14161 end Build_Completing_Body;
14162
14163 ----------------------------------
14164 -- Provide_Completing_Bodies_In --
14165 ----------------------------------
14166
14167 procedure Provide_Completing_Bodies_In (Decls : List_Id) is
14168 Decl : Node_Id;
14169
14170 begin
14171 if Present (Decls) then
14172 Decl := First (Decls);
14173 while Present (Decl) loop
14174 Provide_Completing_Bodies (Decl);
14175 Next (Decl);
14176 end loop;
14177 end if;
14178 end Provide_Completing_Bodies_In;
14179
14180 -- Local variables
14181
14182 Spec : Node_Id;
14183
14184 -- Start of processing for Provide_Completing_Bodies
14185
14186 begin
14187 if Nkind (N) = N_Package_Declaration then
14188 Spec := Specification (N);
14189
14190 Push_Scope (Defining_Entity (N));
14191 Provide_Completing_Bodies_In (Visible_Declarations (Spec));
14192 Provide_Completing_Bodies_In (Private_Declarations (Spec));
14193 Pop_Scope;
14194
14195 elsif Nkind (N) = N_Subprogram_Declaration then
14196 Build_Completing_Body (N);
14197 end if;
14198 end Provide_Completing_Bodies;
14199
14200 -------------------
14201 -- Remove_Parent --
14202 -------------------
14203
14204 procedure Remove_Parent (In_Body : Boolean := False) is
14205 S : Entity_Id := Current_Scope;
14206 -- S is the scope containing the instantiation just completed. The scope
14207 -- stack contains the parent instances of the instantiation, followed by
14208 -- the original S.
14209
14210 Cur_P : Entity_Id;
14211 E : Entity_Id;
14212 P : Entity_Id;
14213 Hidden : Elmt_Id;
14214
14215 begin
14216 -- After child instantiation is complete, remove from scope stack the
14217 -- extra copy of the current scope, and then remove parent instances.
14218
14219 if not In_Body then
14220 Pop_Scope;
14221
14222 while Current_Scope /= S loop
14223 P := Current_Scope;
14224 End_Package_Scope (Current_Scope);
14225
14226 if In_Open_Scopes (P) then
14227 E := First_Entity (P);
14228 while Present (E) loop
14229 Set_Is_Immediately_Visible (E, True);
14230 Next_Entity (E);
14231 end loop;
14232
14233 -- If instantiation is declared in a block, it is the enclosing
14234 -- scope that might be a parent instance. Note that only one
14235 -- block can be involved, because the parent instances have
14236 -- been installed within it.
14237
14238 if Ekind (P) = E_Block then
14239 Cur_P := Scope (P);
14240 else
14241 Cur_P := P;
14242 end if;
14243
14244 if Is_Generic_Instance (Cur_P) and then P /= Current_Scope then
14245 -- We are within an instance of some sibling. Retain
14246 -- visibility of parent, for proper subsequent cleanup, and
14247 -- reinstall private declarations as well.
14248
14249 Set_In_Private_Part (P);
14250 Install_Private_Declarations (P);
14251 end if;
14252
14253 -- If the ultimate parent is a top-level unit recorded in
14254 -- Instance_Parent_Unit, then reset its visibility to what it was
14255 -- before instantiation. (It's not clear what the purpose is of
14256 -- testing whether Scope (P) is In_Open_Scopes, but that test was
14257 -- present before the ultimate parent test was added.???)
14258
14259 elsif not In_Open_Scopes (Scope (P))
14260 or else (P = Instance_Parent_Unit
14261 and then not Parent_Unit_Visible)
14262 then
14263 Set_Is_Immediately_Visible (P, False);
14264
14265 -- If the current scope is itself an instantiation of a generic
14266 -- nested within P, and we are in the private part of body of this
14267 -- instantiation, restore the full views of P, that were removed
14268 -- in End_Package_Scope above. This obscure case can occur when a
14269 -- subunit of a generic contains an instance of a child unit of
14270 -- its generic parent unit.
14271
14272 elsif S = Current_Scope and then Is_Generic_Instance (S) then
14273 declare
14274 Par : constant Entity_Id :=
14275 Generic_Parent (Package_Specification (S));
14276 begin
14277 if Present (Par)
14278 and then P = Scope (Par)
14279 and then (In_Package_Body (S) or else In_Private_Part (S))
14280 then
14281 Set_In_Private_Part (P);
14282 Install_Private_Declarations (P);
14283 end if;
14284 end;
14285 end if;
14286 end loop;
14287
14288 -- Reset visibility of entities in the enclosing scope
14289
14290 Set_Is_Hidden_Open_Scope (Current_Scope, False);
14291
14292 Hidden := First_Elmt (Hidden_Entities);
14293 while Present (Hidden) loop
14294 Set_Is_Immediately_Visible (Node (Hidden), True);
14295 Next_Elmt (Hidden);
14296 end loop;
14297
14298 else
14299 -- Each body is analyzed separately, and there is no context that
14300 -- needs preserving from one body instance to the next, so remove all
14301 -- parent scopes that have been installed.
14302
14303 while Present (S) loop
14304 End_Package_Scope (S);
14305 Set_Is_Immediately_Visible (S, False);
14306 S := Current_Scope;
14307 exit when S = Standard_Standard;
14308 end loop;
14309 end if;
14310 end Remove_Parent;
14311
14312 -----------------
14313 -- Restore_Env --
14314 -----------------
14315
14316 procedure Restore_Env is
14317 Saved : Instance_Env renames Instance_Envs.Table (Instance_Envs.Last);
14318
14319 begin
14320 if No (Current_Instantiated_Parent.Act_Id) then
14321 -- Restore environment after subprogram inlining
14322
14323 Restore_Private_Views (Empty);
14324 end if;
14325
14326 Current_Instantiated_Parent := Saved.Instantiated_Parent;
14327 Exchanged_Views := Saved.Exchanged_Views;
14328 Hidden_Entities := Saved.Hidden_Entities;
14329 Current_Sem_Unit := Saved.Current_Sem_Unit;
14330 Parent_Unit_Visible := Saved.Parent_Unit_Visible;
14331 Instance_Parent_Unit := Saved.Instance_Parent_Unit;
14332
14333 Restore_Opt_Config_Switches (Saved.Switches);
14334
14335 Instance_Envs.Decrement_Last;
14336 end Restore_Env;
14337
14338 ---------------------------
14339 -- Restore_Private_Views --
14340 ---------------------------
14341
14342 procedure Restore_Private_Views
14343 (Pack_Id : Entity_Id;
14344 Is_Package : Boolean := True)
14345 is
14346 M : Elmt_Id;
14347 E : Entity_Id;
14348 Typ : Entity_Id;
14349 Dep_Elmt : Elmt_Id;
14350 Dep_Typ : Node_Id;
14351
14352 procedure Restore_Nested_Formal (Formal : Entity_Id);
14353 -- Hide the generic formals of formal packages declared with box which
14354 -- were reachable in the current instantiation.
14355
14356 ---------------------------
14357 -- Restore_Nested_Formal --
14358 ---------------------------
14359
14360 procedure Restore_Nested_Formal (Formal : Entity_Id) is
14361 Ent : Entity_Id;
14362
14363 begin
14364 if Present (Renamed_Object (Formal))
14365 and then Denotes_Formal_Package (Renamed_Object (Formal), True)
14366 then
14367 return;
14368
14369 elsif Present (Associated_Formal_Package (Formal)) then
14370 Ent := First_Entity (Formal);
14371 while Present (Ent) loop
14372 exit when Ekind (Ent) = E_Package
14373 and then Renamed_Entity (Ent) = Renamed_Entity (Formal);
14374
14375 Set_Is_Hidden (Ent);
14376 Set_Is_Potentially_Use_Visible (Ent, False);
14377
14378 -- If package, then recurse
14379
14380 if Ekind (Ent) = E_Package then
14381 Restore_Nested_Formal (Ent);
14382 end if;
14383
14384 Next_Entity (Ent);
14385 end loop;
14386 end if;
14387 end Restore_Nested_Formal;
14388
14389 -- Start of processing for Restore_Private_Views
14390
14391 begin
14392 M := First_Elmt (Exchanged_Views);
14393 while Present (M) loop
14394 Typ := Node (M);
14395
14396 -- Subtypes of types whose views have been exchanged, and that are
14397 -- defined within the instance, were not on the Private_Dependents
14398 -- list on entry to the instance, so they have to be exchanged
14399 -- explicitly now, in order to remain consistent with the view of the
14400 -- parent type.
14401
14402 if Ekind_In (Typ, E_Private_Type,
14403 E_Limited_Private_Type,
14404 E_Record_Type_With_Private)
14405 then
14406 Dep_Elmt := First_Elmt (Private_Dependents (Typ));
14407 while Present (Dep_Elmt) loop
14408 Dep_Typ := Node (Dep_Elmt);
14409
14410 if Scope (Dep_Typ) = Pack_Id
14411 and then Present (Full_View (Dep_Typ))
14412 then
14413 Replace_Elmt (Dep_Elmt, Full_View (Dep_Typ));
14414 Exchange_Declarations (Dep_Typ);
14415 end if;
14416
14417 Next_Elmt (Dep_Elmt);
14418 end loop;
14419 end if;
14420
14421 Exchange_Declarations (Node (M));
14422 Next_Elmt (M);
14423 end loop;
14424
14425 if No (Pack_Id) then
14426 return;
14427 end if;
14428
14429 -- Make the generic formal parameters private, and make the formal types
14430 -- into subtypes of the actuals again.
14431
14432 E := First_Entity (Pack_Id);
14433 while Present (E) loop
14434 Set_Is_Hidden (E, True);
14435
14436 if Is_Type (E)
14437 and then Nkind (Parent (E)) = N_Subtype_Declaration
14438 then
14439 -- If the actual for E is itself a generic actual type from
14440 -- an enclosing instance, E is still a generic actual type
14441 -- outside of the current instance. This matter when resolving
14442 -- an overloaded call that may be ambiguous in the enclosing
14443 -- instance, when two of its actuals coincide.
14444
14445 if Is_Entity_Name (Subtype_Indication (Parent (E)))
14446 and then Is_Generic_Actual_Type
14447 (Entity (Subtype_Indication (Parent (E))))
14448 then
14449 null;
14450 else
14451 Set_Is_Generic_Actual_Type (E, False);
14452 end if;
14453
14454 -- An unusual case of aliasing: the actual may also be directly
14455 -- visible in the generic, and be private there, while it is fully
14456 -- visible in the context of the instance. The internal subtype
14457 -- is private in the instance but has full visibility like its
14458 -- parent in the enclosing scope. This enforces the invariant that
14459 -- the privacy status of all private dependents of a type coincide
14460 -- with that of the parent type. This can only happen when a
14461 -- generic child unit is instantiated within a sibling.
14462
14463 if Is_Private_Type (E)
14464 and then not Is_Private_Type (Etype (E))
14465 then
14466 Exchange_Declarations (E);
14467 end if;
14468
14469 elsif Ekind (E) = E_Package then
14470
14471 -- The end of the renaming list is the renaming of the generic
14472 -- package itself. If the instance is a subprogram, all entities
14473 -- in the corresponding package are renamings. If this entity is
14474 -- a formal package, make its own formals private as well. The
14475 -- actual in this case is itself the renaming of an instantiation.
14476 -- If the entity is not a package renaming, it is the entity
14477 -- created to validate formal package actuals: ignore it.
14478
14479 -- If the actual is itself a formal package for the enclosing
14480 -- generic, or the actual for such a formal package, it remains
14481 -- visible on exit from the instance, and therefore nothing needs
14482 -- to be done either, except to keep it accessible.
14483
14484 if Is_Package and then Renamed_Object (E) = Pack_Id then
14485 exit;
14486
14487 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
14488 null;
14489
14490 elsif
14491 Denotes_Formal_Package (Renamed_Object (E), True, Pack_Id)
14492 then
14493 Set_Is_Hidden (E, False);
14494
14495 else
14496 declare
14497 Act_P : constant Entity_Id := Renamed_Object (E);
14498 Id : Entity_Id;
14499
14500 begin
14501 Id := First_Entity (Act_P);
14502 while Present (Id)
14503 and then Id /= First_Private_Entity (Act_P)
14504 loop
14505 exit when Ekind (Id) = E_Package
14506 and then Renamed_Object (Id) = Act_P;
14507
14508 Set_Is_Hidden (Id, True);
14509 Set_Is_Potentially_Use_Visible (Id, In_Use (Act_P));
14510
14511 if Ekind (Id) = E_Package then
14512 Restore_Nested_Formal (Id);
14513 end if;
14514
14515 Next_Entity (Id);
14516 end loop;
14517 end;
14518 end if;
14519 end if;
14520
14521 Next_Entity (E);
14522 end loop;
14523 end Restore_Private_Views;
14524
14525 --------------
14526 -- Save_Env --
14527 --------------
14528
14529 procedure Save_Env
14530 (Gen_Unit : Entity_Id;
14531 Act_Unit : Entity_Id)
14532 is
14533 begin
14534 Init_Env;
14535 Set_Instance_Env (Gen_Unit, Act_Unit);
14536 end Save_Env;
14537
14538 ----------------------------
14539 -- Save_Global_References --
14540 ----------------------------
14541
14542 procedure Save_Global_References (Templ : Node_Id) is
14543
14544 -- ??? it is horrible to use global variables in highly recursive code
14545
14546 E : Entity_Id;
14547 -- The entity of the current associated node
14548
14549 Gen_Scope : Entity_Id;
14550 -- The scope of the generic for which references are being saved
14551
14552 N2 : Node_Id;
14553 -- The current associated node
14554
14555 function Is_Global (E : Entity_Id) return Boolean;
14556 -- Check whether entity is defined outside of generic unit. Examine the
14557 -- scope of an entity, and the scope of the scope, etc, until we find
14558 -- either Standard, in which case the entity is global, or the generic
14559 -- unit itself, which indicates that the entity is local. If the entity
14560 -- is the generic unit itself, as in the case of a recursive call, or
14561 -- the enclosing generic unit, if different from the current scope, then
14562 -- it is local as well, because it will be replaced at the point of
14563 -- instantiation. On the other hand, if it is a reference to a child
14564 -- unit of a common ancestor, which appears in an instantiation, it is
14565 -- global because it is used to denote a specific compilation unit at
14566 -- the time the instantiations will be analyzed.
14567
14568 procedure Qualify_Universal_Operands
14569 (Op : Node_Id;
14570 Func_Call : Node_Id);
14571 -- Op denotes a binary or unary operator in generic template Templ. Node
14572 -- Func_Call is the function call alternative of the operator within the
14573 -- the analyzed copy of the template. Change each operand which yields a
14574 -- universal type by wrapping it into a qualified expression
14575 --
14576 -- Actual_Typ'(Operand)
14577 --
14578 -- where Actual_Typ is the type of corresponding actual parameter of
14579 -- Operand in Func_Call.
14580
14581 procedure Reset_Entity (N : Node_Id);
14582 -- Save semantic information on global entity so that it is not resolved
14583 -- again at instantiation time.
14584
14585 procedure Save_Entity_Descendants (N : Node_Id);
14586 -- Apply Save_Global_References to the two syntactic descendants of
14587 -- non-terminal nodes that carry an Associated_Node and are processed
14588 -- through Reset_Entity. Once the global entity (if any) has been
14589 -- captured together with its type, only two syntactic descendants need
14590 -- to be traversed to complete the processing of the tree rooted at N.
14591 -- This applies to Selected_Components, Expanded_Names, and to Operator
14592 -- nodes. N can also be a character literal, identifier, or operator
14593 -- symbol node, but the call has no effect in these cases.
14594
14595 procedure Save_Global_Defaults (N1 : Node_Id; N2 : Node_Id);
14596 -- Default actuals in nested instances must be handled specially
14597 -- because there is no link to them from the original tree. When an
14598 -- actual subprogram is given by a default, we add an explicit generic
14599 -- association for it in the instantiation node. When we save the
14600 -- global references on the name of the instance, we recover the list
14601 -- of generic associations, and add an explicit one to the original
14602 -- generic tree, through which a global actual can be preserved.
14603 -- Similarly, if a child unit is instantiated within a sibling, in the
14604 -- context of the parent, we must preserve the identifier of the parent
14605 -- so that it can be properly resolved in a subsequent instantiation.
14606
14607 procedure Save_Global_Descendant (D : Union_Id);
14608 -- Apply Save_References recursively to the descendants of node D
14609
14610 procedure Save_References (N : Node_Id);
14611 -- This is the recursive procedure that does the work, once the
14612 -- enclosing generic scope has been established.
14613
14614 ---------------
14615 -- Is_Global --
14616 ---------------
14617
14618 function Is_Global (E : Entity_Id) return Boolean is
14619 Se : Entity_Id;
14620
14621 function Is_Instance_Node (Decl : Node_Id) return Boolean;
14622 -- Determine whether the parent node of a reference to a child unit
14623 -- denotes an instantiation or a formal package, in which case the
14624 -- reference to the child unit is global, even if it appears within
14625 -- the current scope (e.g. when the instance appears within the body
14626 -- of an ancestor).
14627
14628 ----------------------
14629 -- Is_Instance_Node --
14630 ----------------------
14631
14632 function Is_Instance_Node (Decl : Node_Id) return Boolean is
14633 begin
14634 return Nkind (Decl) in N_Generic_Instantiation
14635 or else
14636 Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration;
14637 end Is_Instance_Node;
14638
14639 -- Start of processing for Is_Global
14640
14641 begin
14642 if E = Gen_Scope then
14643 return False;
14644
14645 elsif E = Standard_Standard then
14646 return True;
14647
14648 elsif Is_Child_Unit (E)
14649 and then (Is_Instance_Node (Parent (N2))
14650 or else (Nkind (Parent (N2)) = N_Expanded_Name
14651 and then N2 = Selector_Name (Parent (N2))
14652 and then
14653 Is_Instance_Node (Parent (Parent (N2)))))
14654 then
14655 return True;
14656
14657 else
14658 Se := Scope (E);
14659 while Se /= Gen_Scope loop
14660 if Se = Standard_Standard then
14661 return True;
14662 else
14663 Se := Scope (Se);
14664 end if;
14665 end loop;
14666
14667 return False;
14668 end if;
14669 end Is_Global;
14670
14671 --------------------------------
14672 -- Qualify_Universal_Operands --
14673 --------------------------------
14674
14675 procedure Qualify_Universal_Operands
14676 (Op : Node_Id;
14677 Func_Call : Node_Id)
14678 is
14679 procedure Qualify_Operand (Opnd : Node_Id; Actual : Node_Id);
14680 -- Rewrite operand Opnd as a qualified expression of the form
14681 --
14682 -- Actual_Typ'(Opnd)
14683 --
14684 -- where Actual is the corresponding actual parameter of Opnd in
14685 -- function call Func_Call.
14686
14687 function Qualify_Type
14688 (Loc : Source_Ptr;
14689 Typ : Entity_Id) return Node_Id;
14690 -- Qualify type Typ by creating a selected component of the form
14691 --
14692 -- Scope_Of_Typ.Typ
14693
14694 ---------------------
14695 -- Qualify_Operand --
14696 ---------------------
14697
14698 procedure Qualify_Operand (Opnd : Node_Id; Actual : Node_Id) is
14699 Loc : constant Source_Ptr := Sloc (Opnd);
14700 Typ : constant Entity_Id := Etype (Actual);
14701 Mark : Node_Id;
14702 Qual : Node_Id;
14703
14704 begin
14705 -- Qualify the operand when it is of a universal type. Note that
14706 -- the template is unanalyzed and it is not possible to directly
14707 -- query the type. This transformation is not done when the type
14708 -- of the actual is internally generated because the type will be
14709 -- regenerated in the instance.
14710
14711 if Yields_Universal_Type (Opnd)
14712 and then Comes_From_Source (Typ)
14713 and then not Is_Hidden (Typ)
14714 then
14715 -- The type of the actual may be a global reference. Save this
14716 -- information by creating a reference to it.
14717
14718 if Is_Global (Typ) then
14719 Mark := New_Occurrence_Of (Typ, Loc);
14720
14721 -- Otherwise rely on resolution to find the proper type within
14722 -- the instance.
14723
14724 else
14725 Mark := Qualify_Type (Loc, Typ);
14726 end if;
14727
14728 Qual :=
14729 Make_Qualified_Expression (Loc,
14730 Subtype_Mark => Mark,
14731 Expression => Relocate_Node (Opnd));
14732
14733 -- Mark the qualification to distinguish it from other source
14734 -- constructs and signal the instantiation mechanism that this
14735 -- node requires special processing. See Copy_Generic_Node for
14736 -- details.
14737
14738 Set_Is_Qualified_Universal_Literal (Qual);
14739
14740 Rewrite (Opnd, Qual);
14741 end if;
14742 end Qualify_Operand;
14743
14744 ------------------
14745 -- Qualify_Type --
14746 ------------------
14747
14748 function Qualify_Type
14749 (Loc : Source_Ptr;
14750 Typ : Entity_Id) return Node_Id
14751 is
14752 Scop : constant Entity_Id := Scope (Typ);
14753 Result : Node_Id;
14754
14755 begin
14756 Result := Make_Identifier (Loc, Chars (Typ));
14757
14758 if Present (Scop) and then not Is_Generic_Unit (Scop) then
14759 Result :=
14760 Make_Selected_Component (Loc,
14761 Prefix => Make_Identifier (Loc, Chars (Scop)),
14762 Selector_Name => Result);
14763 end if;
14764
14765 return Result;
14766 end Qualify_Type;
14767
14768 -- Local variables
14769
14770 Actuals : constant List_Id := Parameter_Associations (Func_Call);
14771
14772 -- Start of processing for Qualify_Universal_Operands
14773
14774 begin
14775 if Nkind (Op) in N_Binary_Op then
14776 Qualify_Operand (Left_Opnd (Op), First (Actuals));
14777 Qualify_Operand (Right_Opnd (Op), Next (First (Actuals)));
14778
14779 elsif Nkind (Op) in N_Unary_Op then
14780 Qualify_Operand (Right_Opnd (Op), First (Actuals));
14781 end if;
14782 end Qualify_Universal_Operands;
14783
14784 ------------------
14785 -- Reset_Entity --
14786 ------------------
14787
14788 procedure Reset_Entity (N : Node_Id) is
14789 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id);
14790 -- If the type of N2 is global to the generic unit, save the type in
14791 -- the generic node. Just as we perform name capture for explicit
14792 -- references within the generic, we must capture the global types
14793 -- of local entities because they may participate in resolution in
14794 -- the instance.
14795
14796 function Top_Ancestor (E : Entity_Id) return Entity_Id;
14797 -- Find the ultimate ancestor of the current unit. If it is not a
14798 -- generic unit, then the name of the current unit in the prefix of
14799 -- an expanded name must be replaced with its generic homonym to
14800 -- ensure that it will be properly resolved in an instance.
14801
14802 ---------------------
14803 -- Set_Global_Type --
14804 ---------------------
14805
14806 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id) is
14807 Typ : constant Entity_Id := Etype (N2);
14808
14809 begin
14810 Set_Etype (N, Typ);
14811
14812 -- If the entity of N is not the associated node, this is a
14813 -- nested generic and it has an associated node as well, whose
14814 -- type is already the full view (see below). Indicate that the
14815 -- original node has a private view.
14816
14817 if Entity (N) /= N2 and then Has_Private_View (Entity (N)) then
14818 Set_Has_Private_View (N);
14819 end if;
14820
14821 -- If not a private type, nothing else to do
14822
14823 if not Is_Private_Type (Typ) then
14824 if Is_Array_Type (Typ)
14825 and then Is_Private_Type (Component_Type (Typ))
14826 then
14827 Set_Has_Private_View (N);
14828 end if;
14829
14830 -- If it is a derivation of a private type in a context where no
14831 -- full view is needed, nothing to do either.
14832
14833 elsif No (Full_View (Typ)) and then Typ /= Etype (Typ) then
14834 null;
14835
14836 -- Otherwise mark the type for flipping and use the full view when
14837 -- available.
14838
14839 else
14840 Set_Has_Private_View (N);
14841
14842 if Present (Full_View (Typ)) then
14843 Set_Etype (N2, Full_View (Typ));
14844 end if;
14845 end if;
14846
14847 if Is_Floating_Point_Type (Typ)
14848 and then Has_Dimension_System (Typ)
14849 then
14850 Copy_Dimensions (N2, N);
14851 end if;
14852 end Set_Global_Type;
14853
14854 ------------------
14855 -- Top_Ancestor --
14856 ------------------
14857
14858 function Top_Ancestor (E : Entity_Id) return Entity_Id is
14859 Par : Entity_Id;
14860
14861 begin
14862 Par := E;
14863 while Is_Child_Unit (Par) loop
14864 Par := Scope (Par);
14865 end loop;
14866
14867 return Par;
14868 end Top_Ancestor;
14869
14870 -- Start of processing for Reset_Entity
14871
14872 begin
14873 N2 := Get_Associated_Node (N);
14874 E := Entity (N2);
14875
14876 if Present (E) then
14877
14878 -- If the node is an entry call to an entry in an enclosing task,
14879 -- it is rewritten as a selected component. No global entity to
14880 -- preserve in this case, since the expansion will be redone in
14881 -- the instance.
14882
14883 if not Nkind_In (E, N_Defining_Character_Literal,
14884 N_Defining_Identifier,
14885 N_Defining_Operator_Symbol)
14886 then
14887 Set_Associated_Node (N, Empty);
14888 Set_Etype (N, Empty);
14889 return;
14890 end if;
14891
14892 -- If the entity is an itype created as a subtype of an access
14893 -- type with a null exclusion restore source entity for proper
14894 -- visibility. The itype will be created anew in the instance.
14895
14896 if Is_Itype (E)
14897 and then Ekind (E) = E_Access_Subtype
14898 and then Is_Entity_Name (N)
14899 and then Chars (Etype (E)) = Chars (N)
14900 then
14901 E := Etype (E);
14902 Set_Entity (N2, E);
14903 Set_Etype (N2, E);
14904 end if;
14905
14906 if Is_Global (E) then
14907
14908 -- If the entity is a package renaming that is the prefix of
14909 -- an expanded name, it has been rewritten as the renamed
14910 -- package, which is necessary semantically but complicates
14911 -- ASIS tree traversal, so we recover the original entity to
14912 -- expose the renaming. Take into account that the context may
14913 -- be a nested generic, that the original node may itself have
14914 -- an associated node that had better be an entity, and that
14915 -- the current node is still a selected component.
14916
14917 if Ekind (E) = E_Package
14918 and then Nkind (N) = N_Selected_Component
14919 and then Nkind (Parent (N)) = N_Expanded_Name
14920 and then Present (Original_Node (N2))
14921 and then Is_Entity_Name (Original_Node (N2))
14922 and then Present (Entity (Original_Node (N2)))
14923 then
14924 if Is_Global (Entity (Original_Node (N2))) then
14925 N2 := Original_Node (N2);
14926 Set_Associated_Node (N, N2);
14927 Set_Global_Type (N, N2);
14928
14929 -- Renaming is local, and will be resolved in instance
14930
14931 else
14932 Set_Associated_Node (N, Empty);
14933 Set_Etype (N, Empty);
14934 end if;
14935
14936 else
14937 Set_Global_Type (N, N2);
14938 end if;
14939
14940 elsif Nkind (N) = N_Op_Concat
14941 and then Is_Generic_Type (Etype (N2))
14942 and then (Base_Type (Etype (Right_Opnd (N2))) = Etype (N2)
14943 or else
14944 Base_Type (Etype (Left_Opnd (N2))) = Etype (N2))
14945 and then Is_Intrinsic_Subprogram (E)
14946 then
14947 null;
14948
14949 -- Entity is local. Mark generic node as unresolved. Note that now
14950 -- it does not have an entity.
14951
14952 else
14953 Set_Associated_Node (N, Empty);
14954 Set_Etype (N, Empty);
14955 end if;
14956
14957 if Nkind (Parent (N)) in N_Generic_Instantiation
14958 and then N = Name (Parent (N))
14959 then
14960 Save_Global_Defaults (Parent (N), Parent (N2));
14961 end if;
14962
14963 elsif Nkind (Parent (N)) = N_Selected_Component
14964 and then Nkind (Parent (N2)) = N_Expanded_Name
14965 then
14966 if Is_Global (Entity (Parent (N2))) then
14967 Change_Selected_Component_To_Expanded_Name (Parent (N));
14968 Set_Associated_Node (Parent (N), Parent (N2));
14969 Set_Global_Type (Parent (N), Parent (N2));
14970 Save_Entity_Descendants (N);
14971
14972 -- If this is a reference to the current generic entity, replace
14973 -- by the name of the generic homonym of the current package. This
14974 -- is because in an instantiation Par.P.Q will not resolve to the
14975 -- name of the instance, whose enclosing scope is not necessarily
14976 -- Par. We use the generic homonym rather that the name of the
14977 -- generic itself because it may be hidden by a local declaration.
14978
14979 elsif In_Open_Scopes (Entity (Parent (N2)))
14980 and then not
14981 Is_Generic_Unit (Top_Ancestor (Entity (Prefix (Parent (N2)))))
14982 then
14983 if Ekind (Entity (Parent (N2))) = E_Generic_Package then
14984 Rewrite (Parent (N),
14985 Make_Identifier (Sloc (N),
14986 Chars =>
14987 Chars (Generic_Homonym (Entity (Parent (N2))))));
14988 else
14989 Rewrite (Parent (N),
14990 Make_Identifier (Sloc (N),
14991 Chars => Chars (Selector_Name (Parent (N2)))));
14992 end if;
14993 end if;
14994
14995 if Nkind (Parent (Parent (N))) in N_Generic_Instantiation
14996 and then Parent (N) = Name (Parent (Parent (N)))
14997 then
14998 Save_Global_Defaults
14999 (Parent (Parent (N)), Parent (Parent (N2)));
15000 end if;
15001
15002 -- A selected component may denote a static constant that has been
15003 -- folded. If the static constant is global to the generic, capture
15004 -- its value. Otherwise the folding will happen in any instantiation.
15005
15006 elsif Nkind (Parent (N)) = N_Selected_Component
15007 and then Nkind_In (Parent (N2), N_Integer_Literal, N_Real_Literal)
15008 then
15009 if Present (Entity (Original_Node (Parent (N2))))
15010 and then Is_Global (Entity (Original_Node (Parent (N2))))
15011 then
15012 Rewrite (Parent (N), New_Copy (Parent (N2)));
15013 Set_Analyzed (Parent (N), False);
15014 end if;
15015
15016 -- A selected component may be transformed into a parameterless
15017 -- function call. If the called entity is global, rewrite the node
15018 -- appropriately, i.e. as an extended name for the global entity.
15019
15020 elsif Nkind (Parent (N)) = N_Selected_Component
15021 and then Nkind (Parent (N2)) = N_Function_Call
15022 and then N = Selector_Name (Parent (N))
15023 then
15024 if No (Parameter_Associations (Parent (N2))) then
15025 if Is_Global (Entity (Name (Parent (N2)))) then
15026 Change_Selected_Component_To_Expanded_Name (Parent (N));
15027 Set_Associated_Node (Parent (N), Name (Parent (N2)));
15028 Set_Global_Type (Parent (N), Name (Parent (N2)));
15029 Save_Entity_Descendants (N);
15030
15031 else
15032 Set_Is_Prefixed_Call (Parent (N));
15033 Set_Associated_Node (N, Empty);
15034 Set_Etype (N, Empty);
15035 end if;
15036
15037 -- In Ada 2005, X.F may be a call to a primitive operation,
15038 -- rewritten as F (X). This rewriting will be done again in an
15039 -- instance, so keep the original node. Global entities will be
15040 -- captured as for other constructs. Indicate that this must
15041 -- resolve as a call, to prevent accidental overloading in the
15042 -- instance, if both a component and a primitive operation appear
15043 -- as candidates.
15044
15045 else
15046 Set_Is_Prefixed_Call (Parent (N));
15047 end if;
15048
15049 -- Entity is local. Reset in generic unit, so that node is resolved
15050 -- anew at the point of instantiation.
15051
15052 else
15053 Set_Associated_Node (N, Empty);
15054 Set_Etype (N, Empty);
15055 end if;
15056 end Reset_Entity;
15057
15058 -----------------------------
15059 -- Save_Entity_Descendants --
15060 -----------------------------
15061
15062 procedure Save_Entity_Descendants (N : Node_Id) is
15063 begin
15064 case Nkind (N) is
15065 when N_Binary_Op =>
15066 Save_Global_Descendant (Union_Id (Left_Opnd (N)));
15067 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
15068
15069 when N_Unary_Op =>
15070 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
15071
15072 when N_Expanded_Name
15073 | N_Selected_Component
15074 =>
15075 Save_Global_Descendant (Union_Id (Prefix (N)));
15076 Save_Global_Descendant (Union_Id (Selector_Name (N)));
15077
15078 when N_Character_Literal
15079 | N_Identifier
15080 | N_Operator_Symbol
15081 =>
15082 null;
15083
15084 when others =>
15085 raise Program_Error;
15086 end case;
15087 end Save_Entity_Descendants;
15088
15089 --------------------------
15090 -- Save_Global_Defaults --
15091 --------------------------
15092
15093 procedure Save_Global_Defaults (N1 : Node_Id; N2 : Node_Id) is
15094 Loc : constant Source_Ptr := Sloc (N1);
15095 Assoc2 : constant List_Id := Generic_Associations (N2);
15096 Gen_Id : constant Entity_Id := Get_Generic_Entity (N2);
15097 Assoc1 : List_Id;
15098 Act1 : Node_Id;
15099 Act2 : Node_Id;
15100 Def : Node_Id;
15101 Ndec : Node_Id;
15102 Subp : Entity_Id;
15103 Actual : Entity_Id;
15104
15105 begin
15106 Assoc1 := Generic_Associations (N1);
15107
15108 if Present (Assoc1) then
15109 Act1 := First (Assoc1);
15110 else
15111 Act1 := Empty;
15112 Set_Generic_Associations (N1, New_List);
15113 Assoc1 := Generic_Associations (N1);
15114 end if;
15115
15116 if Present (Assoc2) then
15117 Act2 := First (Assoc2);
15118 else
15119 return;
15120 end if;
15121
15122 while Present (Act1) and then Present (Act2) loop
15123 Next (Act1);
15124 Next (Act2);
15125 end loop;
15126
15127 -- Find the associations added for default subprograms
15128
15129 if Present (Act2) then
15130 while Nkind (Act2) /= N_Generic_Association
15131 or else No (Entity (Selector_Name (Act2)))
15132 or else not Is_Overloadable (Entity (Selector_Name (Act2)))
15133 loop
15134 Next (Act2);
15135 end loop;
15136
15137 -- Add a similar association if the default is global. The
15138 -- renaming declaration for the actual has been analyzed, and
15139 -- its alias is the program it renames. Link the actual in the
15140 -- original generic tree with the node in the analyzed tree.
15141
15142 while Present (Act2) loop
15143 Subp := Entity (Selector_Name (Act2));
15144 Def := Explicit_Generic_Actual_Parameter (Act2);
15145
15146 -- Following test is defence against rubbish errors
15147
15148 if No (Alias (Subp)) then
15149 return;
15150 end if;
15151
15152 -- Retrieve the resolved actual from the renaming declaration
15153 -- created for the instantiated formal.
15154
15155 Actual := Entity (Name (Parent (Parent (Subp))));
15156 Set_Entity (Def, Actual);
15157 Set_Etype (Def, Etype (Actual));
15158
15159 if Is_Global (Actual) then
15160 Ndec :=
15161 Make_Generic_Association (Loc,
15162 Selector_Name =>
15163 New_Occurrence_Of (Subp, Loc),
15164 Explicit_Generic_Actual_Parameter =>
15165 New_Occurrence_Of (Actual, Loc));
15166
15167 Set_Associated_Node
15168 (Explicit_Generic_Actual_Parameter (Ndec), Def);
15169
15170 Append (Ndec, Assoc1);
15171
15172 -- If there are other defaults, add a dummy association in case
15173 -- there are other defaulted formals with the same name.
15174
15175 elsif Present (Next (Act2)) then
15176 Ndec :=
15177 Make_Generic_Association (Loc,
15178 Selector_Name =>
15179 New_Occurrence_Of (Subp, Loc),
15180 Explicit_Generic_Actual_Parameter => Empty);
15181
15182 Append (Ndec, Assoc1);
15183 end if;
15184
15185 Next (Act2);
15186 end loop;
15187 end if;
15188
15189 if Nkind (Name (N1)) = N_Identifier
15190 and then Is_Child_Unit (Gen_Id)
15191 and then Is_Global (Gen_Id)
15192 and then Is_Generic_Unit (Scope (Gen_Id))
15193 and then In_Open_Scopes (Scope (Gen_Id))
15194 then
15195 -- This is an instantiation of a child unit within a sibling, so
15196 -- that the generic parent is in scope. An eventual instance must
15197 -- occur within the scope of an instance of the parent. Make name
15198 -- in instance into an expanded name, to preserve the identifier
15199 -- of the parent, so it can be resolved subsequently.
15200
15201 Rewrite (Name (N2),
15202 Make_Expanded_Name (Loc,
15203 Chars => Chars (Gen_Id),
15204 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
15205 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
15206 Set_Entity (Name (N2), Gen_Id);
15207
15208 Rewrite (Name (N1),
15209 Make_Expanded_Name (Loc,
15210 Chars => Chars (Gen_Id),
15211 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
15212 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
15213
15214 Set_Associated_Node (Name (N1), Name (N2));
15215 Set_Associated_Node (Prefix (Name (N1)), Empty);
15216 Set_Associated_Node
15217 (Selector_Name (Name (N1)), Selector_Name (Name (N2)));
15218 Set_Etype (Name (N1), Etype (Gen_Id));
15219 end if;
15220 end Save_Global_Defaults;
15221
15222 ----------------------------
15223 -- Save_Global_Descendant --
15224 ----------------------------
15225
15226 procedure Save_Global_Descendant (D : Union_Id) is
15227 N1 : Node_Id;
15228
15229 begin
15230 if D in Node_Range then
15231 if D = Union_Id (Empty) then
15232 null;
15233
15234 elsif Nkind (Node_Id (D)) /= N_Compilation_Unit then
15235 Save_References (Node_Id (D));
15236 end if;
15237
15238 elsif D in List_Range then
15239 pragma Assert (D /= Union_Id (No_List));
15240 -- Because No_List = Empty, which is in Node_Range above
15241
15242 if Is_Empty_List (List_Id (D)) then
15243 null;
15244
15245 else
15246 N1 := First (List_Id (D));
15247 while Present (N1) loop
15248 Save_References (N1);
15249 Next (N1);
15250 end loop;
15251 end if;
15252
15253 -- Element list or other non-node field, nothing to do
15254
15255 else
15256 null;
15257 end if;
15258 end Save_Global_Descendant;
15259
15260 ---------------------
15261 -- Save_References --
15262 ---------------------
15263
15264 -- This is the recursive procedure that does the work once the enclosing
15265 -- generic scope has been established. We have to treat specially a
15266 -- number of node rewritings that are required by semantic processing
15267 -- and which change the kind of nodes in the generic copy: typically
15268 -- constant-folding, replacing an operator node by a string literal, or
15269 -- a selected component by an expanded name. In each of those cases, the
15270 -- transformation is propagated to the generic unit.
15271
15272 procedure Save_References (N : Node_Id) is
15273 Loc : constant Source_Ptr := Sloc (N);
15274
15275 function Requires_Delayed_Save (Nod : Node_Id) return Boolean;
15276 -- Determine whether arbitrary node Nod requires delayed capture of
15277 -- global references within its aspect specifications.
15278
15279 procedure Save_References_In_Aggregate (N : Node_Id);
15280 -- Save all global references in [extension] aggregate node N
15281
15282 procedure Save_References_In_Char_Lit_Or_Op_Symbol (N : Node_Id);
15283 -- Save all global references in a character literal or operator
15284 -- symbol denoted by N.
15285
15286 procedure Save_References_In_Descendants (N : Node_Id);
15287 -- Save all global references in all descendants of node N
15288
15289 procedure Save_References_In_Identifier (N : Node_Id);
15290 -- Save all global references in identifier node N
15291
15292 procedure Save_References_In_Operator (N : Node_Id);
15293 -- Save all global references in operator node N
15294
15295 procedure Save_References_In_Pragma (Prag : Node_Id);
15296 -- Save all global references found within the expression of pragma
15297 -- Prag.
15298
15299 ---------------------------
15300 -- Requires_Delayed_Save --
15301 ---------------------------
15302
15303 function Requires_Delayed_Save (Nod : Node_Id) return Boolean is
15304 begin
15305 -- Generic packages and subprograms require delayed capture of
15306 -- global references within their aspects due to the timing of
15307 -- annotation analysis.
15308
15309 if Nkind_In (Nod, N_Generic_Package_Declaration,
15310 N_Generic_Subprogram_Declaration,
15311 N_Package_Body,
15312 N_Package_Body_Stub,
15313 N_Subprogram_Body,
15314 N_Subprogram_Body_Stub)
15315 then
15316 -- Since the capture of global references is done on the
15317 -- unanalyzed generic template, there is no information around
15318 -- to infer the context. Use the Associated_Entity linkages to
15319 -- peek into the analyzed generic copy and determine what the
15320 -- template corresponds to.
15321
15322 if Nod = Templ then
15323 return
15324 Is_Generic_Declaration_Or_Body
15325 (Unit_Declaration_Node
15326 (Associated_Entity (Defining_Entity (Nod))));
15327
15328 -- Otherwise the generic unit being processed is not the top
15329 -- level template. It is safe to capture of global references
15330 -- within the generic unit because at this point the top level
15331 -- copy is fully analyzed.
15332
15333 else
15334 return False;
15335 end if;
15336
15337 -- Otherwise capture the global references without interference
15338
15339 else
15340 return False;
15341 end if;
15342 end Requires_Delayed_Save;
15343
15344 ----------------------------------
15345 -- Save_References_In_Aggregate --
15346 ----------------------------------
15347
15348 procedure Save_References_In_Aggregate (N : Node_Id) is
15349 Nam : Node_Id;
15350 Qual : Node_Id := Empty;
15351 Typ : Entity_Id := Empty;
15352
15353 use Atree.Unchecked_Access;
15354 -- This code section is part of implementing an untyped tree
15355 -- traversal, so it needs direct access to node fields.
15356
15357 begin
15358 N2 := Get_Associated_Node (N);
15359
15360 if Present (N2) then
15361 Typ := Etype (N2);
15362
15363 -- In an instance within a generic, use the name of the actual
15364 -- and not the original generic parameter. If the actual is
15365 -- global in the current generic it must be preserved for its
15366 -- instantiation.
15367
15368 if Nkind (Parent (Typ)) = N_Subtype_Declaration
15369 and then Present (Generic_Parent_Type (Parent (Typ)))
15370 then
15371 Typ := Base_Type (Typ);
15372 Set_Etype (N2, Typ);
15373 end if;
15374 end if;
15375
15376 if No (N2) or else No (Typ) or else not Is_Global (Typ) then
15377 Set_Associated_Node (N, Empty);
15378
15379 -- If the aggregate is an actual in a call, it has been
15380 -- resolved in the current context, to some local type. The
15381 -- enclosing call may have been disambiguated by the aggregate,
15382 -- and this disambiguation might fail at instantiation time
15383 -- because the type to which the aggregate did resolve is not
15384 -- preserved. In order to preserve some of this information,
15385 -- wrap the aggregate in a qualified expression, using the id
15386 -- of its type. For further disambiguation we qualify the type
15387 -- name with its scope (if visible and not hidden by a local
15388 -- homograph) because both id's will have corresponding
15389 -- entities in an instance. This resolves most of the problems
15390 -- with missing type information on aggregates in instances.
15391
15392 if Present (N2)
15393 and then Nkind (N2) = Nkind (N)
15394 and then Nkind (Parent (N2)) in N_Subprogram_Call
15395 and then Present (Typ)
15396 and then Comes_From_Source (Typ)
15397 then
15398 Nam := Make_Identifier (Loc, Chars (Typ));
15399
15400 if Is_Immediately_Visible (Scope (Typ))
15401 and then
15402 (not In_Open_Scopes (Scope (Typ))
15403 or else Current_Entity (Scope (Typ)) = Scope (Typ))
15404 then
15405 Nam :=
15406 Make_Selected_Component (Loc,
15407 Prefix =>
15408 Make_Identifier (Loc, Chars (Scope (Typ))),
15409 Selector_Name => Nam);
15410 end if;
15411
15412 Qual :=
15413 Make_Qualified_Expression (Loc,
15414 Subtype_Mark => Nam,
15415 Expression => Relocate_Node (N));
15416 end if;
15417 end if;
15418
15419 Save_Global_Descendant (Field1 (N));
15420 Save_Global_Descendant (Field2 (N));
15421 Save_Global_Descendant (Field3 (N));
15422 Save_Global_Descendant (Field5 (N));
15423
15424 if Present (Qual) then
15425 Rewrite (N, Qual);
15426 end if;
15427 end Save_References_In_Aggregate;
15428
15429 ----------------------------------------------
15430 -- Save_References_In_Char_Lit_Or_Op_Symbol --
15431 ----------------------------------------------
15432
15433 procedure Save_References_In_Char_Lit_Or_Op_Symbol (N : Node_Id) is
15434 begin
15435 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
15436 Reset_Entity (N);
15437
15438 elsif Nkind (N) = N_Operator_Symbol
15439 and then Nkind (Get_Associated_Node (N)) = N_String_Literal
15440 then
15441 Change_Operator_Symbol_To_String_Literal (N);
15442 end if;
15443 end Save_References_In_Char_Lit_Or_Op_Symbol;
15444
15445 ------------------------------------
15446 -- Save_References_In_Descendants --
15447 ------------------------------------
15448
15449 procedure Save_References_In_Descendants (N : Node_Id) is
15450 use Atree.Unchecked_Access;
15451 -- This code section is part of implementing an untyped tree
15452 -- traversal, so it needs direct access to node fields.
15453
15454 begin
15455 Save_Global_Descendant (Field1 (N));
15456 Save_Global_Descendant (Field2 (N));
15457 Save_Global_Descendant (Field3 (N));
15458 Save_Global_Descendant (Field4 (N));
15459 Save_Global_Descendant (Field5 (N));
15460 end Save_References_In_Descendants;
15461
15462 -----------------------------------
15463 -- Save_References_In_Identifier --
15464 -----------------------------------
15465
15466 procedure Save_References_In_Identifier (N : Node_Id) is
15467 begin
15468 -- The node did not undergo a transformation
15469
15470 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
15471 declare
15472 Aux_N2 : constant Node_Id := Get_Associated_Node (N);
15473 Orig_N2_Parent : constant Node_Id :=
15474 Original_Node (Parent (Aux_N2));
15475 begin
15476 -- The parent of this identifier is a selected component
15477 -- which denotes a named number that was constant folded.
15478 -- Preserve the original name for ASIS and link the parent
15479 -- with its expanded name. The constant folding will be
15480 -- repeated in the instance.
15481
15482 if Nkind (Parent (N)) = N_Selected_Component
15483 and then Nkind_In (Parent (Aux_N2), N_Integer_Literal,
15484 N_Real_Literal)
15485 and then Is_Entity_Name (Orig_N2_Parent)
15486 and then Ekind (Entity (Orig_N2_Parent)) in Named_Kind
15487 and then Is_Global (Entity (Orig_N2_Parent))
15488 then
15489 N2 := Aux_N2;
15490 Set_Associated_Node
15491 (Parent (N), Original_Node (Parent (N2)));
15492
15493 -- Common case
15494
15495 else
15496 -- If this is a discriminant reference, always save it.
15497 -- It is used in the instance to find the corresponding
15498 -- discriminant positionally rather than by name.
15499
15500 Set_Original_Discriminant
15501 (N, Original_Discriminant (Get_Associated_Node (N)));
15502 end if;
15503
15504 Reset_Entity (N);
15505 end;
15506
15507 -- The analysis of the generic copy transformed the identifier
15508 -- into another construct. Propagate the changes to the template.
15509
15510 else
15511 N2 := Get_Associated_Node (N);
15512
15513 -- The identifier denotes a call to a parameterless function.
15514 -- Mark the node as resolved when the function is external.
15515
15516 if Nkind (N2) = N_Function_Call then
15517 E := Entity (Name (N2));
15518
15519 if Present (E) and then Is_Global (E) then
15520 Set_Etype (N, Etype (N2));
15521 else
15522 Set_Associated_Node (N, Empty);
15523 Set_Etype (N, Empty);
15524 end if;
15525
15526 -- The identifier denotes a named number that was constant
15527 -- folded. Preserve the original name for ASIS and undo the
15528 -- constant folding which will be repeated in the instance.
15529
15530 elsif Nkind_In (N2, N_Integer_Literal, N_Real_Literal)
15531 and then Is_Entity_Name (Original_Node (N2))
15532 then
15533 Set_Associated_Node (N, Original_Node (N2));
15534 Reset_Entity (N);
15535
15536 -- The identifier resolved to a string literal. Propagate this
15537 -- information to the generic template.
15538
15539 elsif Nkind (N2) = N_String_Literal then
15540 Rewrite (N, New_Copy (N2));
15541
15542 -- The identifier is rewritten as a dereference if it is the
15543 -- prefix of an implicit dereference. Preserve the original
15544 -- tree as the analysis of the instance will expand the node
15545 -- again, but preserve the resolved entity if it is global.
15546
15547 elsif Nkind (N2) = N_Explicit_Dereference then
15548 if Is_Entity_Name (Prefix (N2))
15549 and then Present (Entity (Prefix (N2)))
15550 and then Is_Global (Entity (Prefix (N2)))
15551 then
15552 Set_Associated_Node (N, Prefix (N2));
15553
15554 elsif Nkind (Prefix (N2)) = N_Function_Call
15555 and then Present (Entity (Name (Prefix (N2))))
15556 and then Is_Global (Entity (Name (Prefix (N2))))
15557 then
15558 Rewrite (N,
15559 Make_Explicit_Dereference (Loc,
15560 Prefix =>
15561 Make_Function_Call (Loc,
15562 Name =>
15563 New_Occurrence_Of
15564 (Entity (Name (Prefix (N2))), Loc))));
15565
15566 else
15567 Set_Associated_Node (N, Empty);
15568 Set_Etype (N, Empty);
15569 end if;
15570
15571 -- The subtype mark of a nominally unconstrained object is
15572 -- rewritten as a subtype indication using the bounds of the
15573 -- expression. Recover the original subtype mark.
15574
15575 elsif Nkind (N2) = N_Subtype_Indication
15576 and then Is_Entity_Name (Original_Node (N2))
15577 then
15578 Set_Associated_Node (N, Original_Node (N2));
15579 Reset_Entity (N);
15580 end if;
15581 end if;
15582 end Save_References_In_Identifier;
15583
15584 ---------------------------------
15585 -- Save_References_In_Operator --
15586 ---------------------------------
15587
15588 procedure Save_References_In_Operator (N : Node_Id) is
15589 begin
15590 -- The node did not undergo a transformation
15591
15592 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
15593 if Nkind (N) = N_Op_Concat then
15594 Set_Is_Component_Left_Opnd (N,
15595 Is_Component_Left_Opnd (Get_Associated_Node (N)));
15596
15597 Set_Is_Component_Right_Opnd (N,
15598 Is_Component_Right_Opnd (Get_Associated_Node (N)));
15599 end if;
15600
15601 Reset_Entity (N);
15602
15603 -- The analysis of the generic copy transformed the operator into
15604 -- some other construct. Propagate the changes to the template if
15605 -- applicable.
15606
15607 else
15608 N2 := Get_Associated_Node (N);
15609
15610 -- The operator resoved to a function call
15611
15612 if Nkind (N2) = N_Function_Call then
15613
15614 -- Add explicit qualifications in the generic template for
15615 -- all operands of universal type. This aids resolution by
15616 -- preserving the actual type of a literal or an attribute
15617 -- that yields a universal result.
15618
15619 Qualify_Universal_Operands (N, N2);
15620
15621 E := Entity (Name (N2));
15622
15623 if Present (E) and then Is_Global (E) then
15624 Set_Etype (N, Etype (N2));
15625 else
15626 Set_Associated_Node (N, Empty);
15627 Set_Etype (N, Empty);
15628 end if;
15629
15630 -- The operator was folded into a literal
15631
15632 elsif Nkind_In (N2, N_Integer_Literal,
15633 N_Real_Literal,
15634 N_String_Literal)
15635 then
15636 if Present (Original_Node (N2))
15637 and then Nkind (Original_Node (N2)) = Nkind (N)
15638 then
15639 -- Operation was constant-folded. Whenever possible,
15640 -- recover semantic information from unfolded node,
15641 -- for ASIS use.
15642
15643 Set_Associated_Node (N, Original_Node (N2));
15644
15645 if Nkind (N) = N_Op_Concat then
15646 Set_Is_Component_Left_Opnd (N,
15647 Is_Component_Left_Opnd (Get_Associated_Node (N)));
15648 Set_Is_Component_Right_Opnd (N,
15649 Is_Component_Right_Opnd (Get_Associated_Node (N)));
15650 end if;
15651
15652 Reset_Entity (N);
15653
15654 -- Propagate the constant folding back to the template
15655
15656 else
15657 Rewrite (N, New_Copy (N2));
15658 Set_Analyzed (N, False);
15659 end if;
15660
15661 -- The operator was folded into an enumeration literal. Retain
15662 -- the entity to avoid spurious ambiguities if it is overloaded
15663 -- at the point of instantiation or inlining.
15664
15665 elsif Nkind (N2) = N_Identifier
15666 and then Ekind (Entity (N2)) = E_Enumeration_Literal
15667 then
15668 Rewrite (N, New_Copy (N2));
15669 Set_Analyzed (N, False);
15670 end if;
15671 end if;
15672
15673 -- Complete the operands check if node has not been constant
15674 -- folded.
15675
15676 if Nkind (N) in N_Op then
15677 Save_Entity_Descendants (N);
15678 end if;
15679 end Save_References_In_Operator;
15680
15681 -------------------------------
15682 -- Save_References_In_Pragma --
15683 -------------------------------
15684
15685 procedure Save_References_In_Pragma (Prag : Node_Id) is
15686 Context : Node_Id;
15687 Do_Save : Boolean := True;
15688
15689 use Atree.Unchecked_Access;
15690 -- This code section is part of implementing an untyped tree
15691 -- traversal, so it needs direct access to node fields.
15692
15693 begin
15694 -- Do not save global references in pragmas generated from aspects
15695 -- because the pragmas will be regenerated at instantiation time.
15696
15697 if From_Aspect_Specification (Prag) then
15698 Do_Save := False;
15699
15700 -- The capture of global references within contract-related source
15701 -- pragmas associated with generic packages, subprograms or their
15702 -- respective bodies must be delayed due to timing of annotation
15703 -- analysis. Global references are still captured in routine
15704 -- Save_Global_References_In_Contract.
15705
15706 elsif Is_Generic_Contract_Pragma (Prag) and then Prag /= Templ then
15707 if Is_Package_Contract_Annotation (Prag) then
15708 Context := Find_Related_Package_Or_Body (Prag);
15709 else
15710 pragma Assert (Is_Subprogram_Contract_Annotation (Prag));
15711 Context := Find_Related_Declaration_Or_Body (Prag);
15712 end if;
15713
15714 -- The use of Original_Node accounts for the case when the
15715 -- related context is generic template.
15716
15717 if Requires_Delayed_Save (Original_Node (Context)) then
15718 Do_Save := False;
15719 end if;
15720 end if;
15721
15722 -- For all other cases, save all global references within the
15723 -- descendants, but skip the following semantic fields:
15724
15725 -- Field1 - Next_Pragma
15726 -- Field3 - Corresponding_Aspect
15727 -- Field5 - Next_Rep_Item
15728
15729 if Do_Save then
15730 Save_Global_Descendant (Field2 (Prag));
15731 Save_Global_Descendant (Field4 (Prag));
15732 end if;
15733 end Save_References_In_Pragma;
15734
15735 -- Start of processing for Save_References
15736
15737 begin
15738 if N = Empty then
15739 null;
15740
15741 -- Aggregates
15742
15743 elsif Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
15744 Save_References_In_Aggregate (N);
15745
15746 -- Character literals, operator symbols
15747
15748 elsif Nkind_In (N, N_Character_Literal, N_Operator_Symbol) then
15749 Save_References_In_Char_Lit_Or_Op_Symbol (N);
15750
15751 -- Defining identifiers
15752
15753 elsif Nkind (N) in N_Entity then
15754 null;
15755
15756 -- Identifiers
15757
15758 elsif Nkind (N) = N_Identifier then
15759 Save_References_In_Identifier (N);
15760
15761 -- Operators
15762
15763 elsif Nkind (N) in N_Op then
15764 Save_References_In_Operator (N);
15765
15766 -- Pragmas
15767
15768 elsif Nkind (N) = N_Pragma then
15769 Save_References_In_Pragma (N);
15770
15771 else
15772 Save_References_In_Descendants (N);
15773 end if;
15774
15775 -- Save all global references found within the aspect specifications
15776 -- of the related node.
15777
15778 if Permits_Aspect_Specifications (N) and then Has_Aspects (N) then
15779
15780 -- The capture of global references within aspects associated with
15781 -- generic packages, subprograms or their bodies must be delayed
15782 -- due to timing of annotation analysis. Global references are
15783 -- still captured in routine Save_Global_References_In_Contract.
15784
15785 if Requires_Delayed_Save (N) then
15786 null;
15787
15788 -- Otherwise save all global references within the aspects
15789
15790 else
15791 Save_Global_References_In_Aspects (N);
15792 end if;
15793 end if;
15794 end Save_References;
15795
15796 -- Start of processing for Save_Global_References
15797
15798 begin
15799 Gen_Scope := Current_Scope;
15800
15801 -- If the generic unit is a child unit, references to entities in the
15802 -- parent are treated as local, because they will be resolved anew in
15803 -- the context of the instance of the parent.
15804
15805 while Is_Child_Unit (Gen_Scope)
15806 and then Ekind (Scope (Gen_Scope)) = E_Generic_Package
15807 loop
15808 Gen_Scope := Scope (Gen_Scope);
15809 end loop;
15810
15811 Save_References (Templ);
15812 end Save_Global_References;
15813
15814 ---------------------------------------
15815 -- Save_Global_References_In_Aspects --
15816 ---------------------------------------
15817
15818 procedure Save_Global_References_In_Aspects (N : Node_Id) is
15819 Asp : Node_Id;
15820 Expr : Node_Id;
15821
15822 begin
15823 Asp := First (Aspect_Specifications (N));
15824 while Present (Asp) loop
15825 Expr := Expression (Asp);
15826
15827 if Present (Expr) then
15828 Save_Global_References (Expr);
15829 end if;
15830
15831 Next (Asp);
15832 end loop;
15833 end Save_Global_References_In_Aspects;
15834
15835 ------------------------------------------
15836 -- Set_Copied_Sloc_For_Inherited_Pragma --
15837 ------------------------------------------
15838
15839 procedure Set_Copied_Sloc_For_Inherited_Pragma
15840 (N : Node_Id;
15841 E : Entity_Id)
15842 is
15843 begin
15844 Create_Instantiation_Source (N, E,
15845 Inlined_Body => False,
15846 Inherited_Pragma => True,
15847 Factor => S_Adjustment);
15848 end Set_Copied_Sloc_For_Inherited_Pragma;
15849
15850 --------------------------------------
15851 -- Set_Copied_Sloc_For_Inlined_Body --
15852 --------------------------------------
15853
15854 procedure Set_Copied_Sloc_For_Inlined_Body (N : Node_Id; E : Entity_Id) is
15855 begin
15856 Create_Instantiation_Source (N, E,
15857 Inlined_Body => True,
15858 Inherited_Pragma => False,
15859 Factor => S_Adjustment);
15860 end Set_Copied_Sloc_For_Inlined_Body;
15861
15862 ---------------------
15863 -- Set_Instance_Of --
15864 ---------------------
15865
15866 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id) is
15867 begin
15868 Generic_Renamings.Table (Generic_Renamings.Last) := (A, B, Assoc_Null);
15869 Generic_Renamings_HTable.Set (Generic_Renamings.Last);
15870 Generic_Renamings.Increment_Last;
15871 end Set_Instance_Of;
15872
15873 --------------------
15874 -- Set_Next_Assoc --
15875 --------------------
15876
15877 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr) is
15878 begin
15879 Generic_Renamings.Table (E).Next_In_HTable := Next;
15880 end Set_Next_Assoc;
15881
15882 -------------------
15883 -- Start_Generic --
15884 -------------------
15885
15886 procedure Start_Generic is
15887 begin
15888 -- ??? More things could be factored out in this routine.
15889 -- Should probably be done at a later stage.
15890
15891 Generic_Flags.Append (Inside_A_Generic);
15892 Inside_A_Generic := True;
15893
15894 Expander_Mode_Save_And_Set (False);
15895 end Start_Generic;
15896
15897 ----------------------
15898 -- Set_Instance_Env --
15899 ----------------------
15900
15901 -- WARNING: This routine manages SPARK regions
15902
15903 procedure Set_Instance_Env
15904 (Gen_Unit : Entity_Id;
15905 Act_Unit : Entity_Id)
15906 is
15907 Saved_AE : constant Boolean := Assertions_Enabled;
15908 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
15909 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
15910 -- Save the SPARK mode-related data because utilizing the configuration
15911 -- values of pragmas and switches will eliminate any previously set
15912 -- SPARK_Mode.
15913
15914 begin
15915 -- Regardless of the current mode, predefined units are analyzed in the
15916 -- most current Ada mode, and earlier version Ada checks do not apply
15917 -- to predefined units. Nothing needs to be done for non-internal units.
15918 -- These are always analyzed in the current mode.
15919
15920 if In_Internal_Unit (Gen_Unit) then
15921 Set_Opt_Config_Switches (True, Current_Sem_Unit = Main_Unit);
15922
15923 -- In Ada2012 we may want to enable assertions in an instance of a
15924 -- predefined unit, in which case we need to preserve the current
15925 -- setting for the Assertions_Enabled flag. This will become more
15926 -- critical when pre/postconditions are added to predefined units,
15927 -- as is already the case for some numeric libraries.
15928
15929 if Ada_Version >= Ada_2012 then
15930 Assertions_Enabled := Saved_AE;
15931 end if;
15932
15933 -- Reinstall the SPARK_Mode which was in effect at the point of
15934 -- instantiation.
15935
15936 Install_SPARK_Mode (Saved_SM, Saved_SMP);
15937 end if;
15938
15939 Current_Instantiated_Parent :=
15940 (Gen_Id => Gen_Unit,
15941 Act_Id => Act_Unit,
15942 Next_In_HTable => Assoc_Null);
15943 end Set_Instance_Env;
15944
15945 -----------------
15946 -- Switch_View --
15947 -----------------
15948
15949 procedure Switch_View (T : Entity_Id) is
15950 BT : constant Entity_Id := Base_Type (T);
15951 Priv_Elmt : Elmt_Id := No_Elmt;
15952 Priv_Sub : Entity_Id;
15953
15954 begin
15955 -- T may be private but its base type may have been exchanged through
15956 -- some other occurrence, in which case there is nothing to switch
15957 -- besides T itself. Note that a private dependent subtype of a private
15958 -- type might not have been switched even if the base type has been,
15959 -- because of the last branch of Check_Private_View (see comment there).
15960
15961 if not Is_Private_Type (BT) then
15962 Prepend_Elmt (Full_View (T), Exchanged_Views);
15963 Exchange_Declarations (T);
15964 return;
15965 end if;
15966
15967 Priv_Elmt := First_Elmt (Private_Dependents (BT));
15968
15969 if Present (Full_View (BT)) then
15970 Prepend_Elmt (Full_View (BT), Exchanged_Views);
15971 Exchange_Declarations (BT);
15972 end if;
15973
15974 while Present (Priv_Elmt) loop
15975 Priv_Sub := (Node (Priv_Elmt));
15976
15977 -- We avoid flipping the subtype if the Etype of its full view is
15978 -- private because this would result in a malformed subtype. This
15979 -- occurs when the Etype of the subtype full view is the full view of
15980 -- the base type (and since the base types were just switched, the
15981 -- subtype is pointing to the wrong view). This is currently the case
15982 -- for tagged record types, access types (maybe more?) and needs to
15983 -- be resolved. ???
15984
15985 if Present (Full_View (Priv_Sub))
15986 and then not Is_Private_Type (Etype (Full_View (Priv_Sub)))
15987 then
15988 Prepend_Elmt (Full_View (Priv_Sub), Exchanged_Views);
15989 Exchange_Declarations (Priv_Sub);
15990 end if;
15991
15992 Next_Elmt (Priv_Elmt);
15993 end loop;
15994 end Switch_View;
15995
15996 -----------------
15997 -- True_Parent --
15998 -----------------
15999
16000 function True_Parent (N : Node_Id) return Node_Id is
16001 begin
16002 if Nkind (Parent (N)) = N_Subunit then
16003 return Parent (Corresponding_Stub (Parent (N)));
16004 else
16005 return Parent (N);
16006 end if;
16007 end True_Parent;
16008
16009 -----------------------------
16010 -- Valid_Default_Attribute --
16011 -----------------------------
16012
16013 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id) is
16014 Attr_Id : constant Attribute_Id :=
16015 Get_Attribute_Id (Attribute_Name (Def));
16016 T : constant Entity_Id := Entity (Prefix (Def));
16017 Is_Fun : constant Boolean := (Ekind (Nam) = E_Function);
16018 F : Entity_Id;
16019 Num_F : Nat;
16020 OK : Boolean;
16021
16022 begin
16023 if No (T) or else T = Any_Id then
16024 return;
16025 end if;
16026
16027 Num_F := 0;
16028 F := First_Formal (Nam);
16029 while Present (F) loop
16030 Num_F := Num_F + 1;
16031 Next_Formal (F);
16032 end loop;
16033
16034 case Attr_Id is
16035 when Attribute_Adjacent
16036 | Attribute_Ceiling
16037 | Attribute_Copy_Sign
16038 | Attribute_Floor
16039 | Attribute_Fraction
16040 | Attribute_Machine
16041 | Attribute_Model
16042 | Attribute_Remainder
16043 | Attribute_Rounding
16044 | Attribute_Unbiased_Rounding
16045 =>
16046 OK := Is_Fun
16047 and then Num_F = 1
16048 and then Is_Floating_Point_Type (T);
16049
16050 when Attribute_Image
16051 | Attribute_Pred
16052 | Attribute_Succ
16053 | Attribute_Value
16054 | Attribute_Wide_Image
16055 | Attribute_Wide_Value
16056 =>
16057 OK := Is_Fun and then Num_F = 1 and then Is_Scalar_Type (T);
16058
16059 when Attribute_Max
16060 | Attribute_Min
16061 =>
16062 OK := Is_Fun and then Num_F = 2 and then Is_Scalar_Type (T);
16063
16064 when Attribute_Input =>
16065 OK := (Is_Fun and then Num_F = 1);
16066
16067 when Attribute_Output
16068 | Attribute_Read
16069 | Attribute_Write
16070 =>
16071 OK := not Is_Fun and then Num_F = 2;
16072
16073 when others =>
16074 OK := False;
16075 end case;
16076
16077 if not OK then
16078 Error_Msg_N
16079 ("attribute reference has wrong profile for subprogram", Def);
16080 end if;
16081 end Valid_Default_Attribute;
16082
16083 end Sem_Ch12;