File : 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-2016, 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.
717
718 procedure Install_Formal_Packages (Par : Entity_Id);
719 -- Install the visible part of any formal of the parent that is a formal
720 -- package. Note that for the case of a formal package with a box, this
721 -- includes the formal part of the formal package (12.7(10/2)).
722
723 procedure Install_Hidden_Primitives
724 (Prims_List : in out Elist_Id;
725 Gen_T : Entity_Id;
726 Act_T : Entity_Id);
727 -- Remove suffix 'P' from hidden primitives of Act_T to match the
728 -- visibility of primitives of Gen_T. The list of primitives to which
729 -- the suffix is removed is added to Prims_List to restore them later.
730
731 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False);
732 -- When compiling an instance of a child unit the parent (which is
733 -- itself an instance) is an enclosing scope that must be made
734 -- immediately visible. This procedure is also used to install the non-
735 -- generic parent of a generic child unit when compiling its body, so
736 -- that full views of types in the parent are made visible.
737
738 -- The functions Instantiate_XXX perform various legality checks and build
739 -- the declarations for instantiated generic parameters. In all of these
740 -- Formal is the entity in the generic unit, Actual is the entity of
741 -- expression in the generic associations, and Analyzed_Formal is the
742 -- formal in the generic copy, which contains the semantic information to
743 -- be used to validate the actual.
744
745 function Instantiate_Object
746 (Formal : Node_Id;
747 Actual : Node_Id;
748 Analyzed_Formal : Node_Id) return List_Id;
749
750 function Instantiate_Type
751 (Formal : Node_Id;
752 Actual : Node_Id;
753 Analyzed_Formal : Node_Id;
754 Actual_Decls : List_Id) return List_Id;
755
756 function Instantiate_Formal_Subprogram
757 (Formal : Node_Id;
758 Actual : Node_Id;
759 Analyzed_Formal : Node_Id) return Node_Id;
760
761 function Instantiate_Formal_Package
762 (Formal : Node_Id;
763 Actual : Node_Id;
764 Analyzed_Formal : Node_Id) return List_Id;
765 -- If the formal package is declared with a box, special visibility rules
766 -- apply to its formals: they are in the visible part of the package. This
767 -- is true in the declarative region of the formal package, that is to say
768 -- in the enclosing generic or instantiation. For an instantiation, the
769 -- parameters of the formal package are made visible in an explicit step.
770 -- Furthermore, if the actual has a visible USE clause, these formals must
771 -- be made potentially use-visible as well. On exit from the enclosing
772 -- instantiation, the reverse must be done.
773
774 -- For a formal package declared without a box, there are conformance rules
775 -- that apply to the actuals in the generic declaration and the actuals of
776 -- the actual package in the enclosing instantiation. The simplest way to
777 -- apply these rules is to repeat the instantiation of the formal package
778 -- in the context of the enclosing instance, and compare the generic
779 -- associations of this instantiation with those of the actual package.
780 -- This internal instantiation only needs to contain the renamings of the
781 -- formals: the visible and private declarations themselves need not be
782 -- created.
783
784 -- In Ada 2005, the formal package may be only partially parameterized.
785 -- In that case the visibility step must make visible those actuals whose
786 -- corresponding formals were given with a box. A final complication
787 -- involves inherited operations from formal derived types, which must
788 -- be visible if the type is.
789
790 function Is_In_Main_Unit (N : Node_Id) return Boolean;
791 -- Test if given node is in the main unit
792
793 procedure Load_Parent_Of_Generic
794 (N : Node_Id;
795 Spec : Node_Id;
796 Body_Optional : Boolean := False);
797 -- If the generic appears in a separate non-generic library unit, load the
798 -- corresponding body to retrieve the body of the generic. N is the node
799 -- for the generic instantiation, Spec is the generic package declaration.
800 --
801 -- Body_Optional is a flag that indicates that the body is being loaded to
802 -- ensure that temporaries are generated consistently when there are other
803 -- instances in the current declarative part that precede the one being
804 -- loaded. In that case a missing body is acceptable.
805
806 procedure Map_Formal_Package_Entities (Form : Entity_Id; Act : Entity_Id);
807 -- Within the generic part, entities in the formal package are
808 -- visible. To validate subsequent type declarations, indicate
809 -- the correspondence between the entities in the analyzed formal,
810 -- and the entities in the actual package. There are three packages
811 -- involved in the instantiation of a formal package: the parent
812 -- generic P1 which appears in the generic declaration, the fake
813 -- instantiation P2 which appears in the analyzed generic, and whose
814 -- visible entities may be used in subsequent formals, and the actual
815 -- P3 in the instance. To validate subsequent formals, me indicate
816 -- that the entities in P2 are mapped into those of P3. The mapping of
817 -- entities has to be done recursively for nested packages.
818
819 procedure Move_Freeze_Nodes
820 (Out_Of : Entity_Id;
821 After : Node_Id;
822 L : List_Id);
823 -- Freeze nodes can be generated in the analysis of a generic unit, but
824 -- will not be seen by the back-end. It is necessary to move those nodes
825 -- to the enclosing scope if they freeze an outer entity. We place them
826 -- at the end of the enclosing generic package, which is semantically
827 -- neutral.
828
829 procedure Preanalyze_Actuals (N : Node_Id; Inst : Entity_Id := Empty);
830 -- Analyze actuals to perform name resolution. Full resolution is done
831 -- later, when the expected types are known, but names have to be captured
832 -- before installing parents of generics, that are not visible for the
833 -- actuals themselves.
834 --
835 -- If Inst is present, it is the entity of the package instance. This
836 -- entity is marked as having a limited_view actual when some actual is
837 -- a limited view. This is used to place the instance body properly.
838
839 procedure Remove_Parent (In_Body : Boolean := False);
840 -- Reverse effect after instantiation of child is complete
841
842 procedure Restore_Hidden_Primitives (Prims_List : in out Elist_Id);
843 -- Restore suffix 'P' to primitives of Prims_List and leave Prims_List
844 -- set to No_Elist.
845
846 procedure Set_Instance_Env
847 (Gen_Unit : Entity_Id;
848 Act_Unit : Entity_Id);
849 -- Save current instance on saved environment, to be used to determine
850 -- the global status of entities in nested instances. Part of Save_Env.
851 -- called after verifying that the generic unit is legal for the instance,
852 -- The procedure also examines whether the generic unit is a predefined
853 -- unit, in order to set configuration switches accordingly. As a result
854 -- the procedure must be called after analyzing and freezing the actuals.
855
856 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id);
857 -- Associate analyzed generic parameter with corresponding instance. Used
858 -- for semantic checks at instantiation time.
859
860 function True_Parent (N : Node_Id) return Node_Id;
861 -- For a subunit, return parent of corresponding stub, else return
862 -- parent of node.
863
864 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id);
865 -- Verify that an attribute that appears as the default for a formal
866 -- subprogram is a function or procedure with the correct profile.
867
868 -------------------------------------------
869 -- Data Structures for Generic Renamings --
870 -------------------------------------------
871
872 -- The map Generic_Renamings associates generic entities with their
873 -- corresponding actuals. Currently used to validate type instances. It
874 -- will eventually be used for all generic parameters to eliminate the
875 -- need for overload resolution in the instance.
876
877 type Assoc_Ptr is new Int;
878
879 Assoc_Null : constant Assoc_Ptr := -1;
880
881 type Assoc is record
882 Gen_Id : Entity_Id;
883 Act_Id : Entity_Id;
884 Next_In_HTable : Assoc_Ptr;
885 end record;
886
887 package Generic_Renamings is new Table.Table
888 (Table_Component_Type => Assoc,
889 Table_Index_Type => Assoc_Ptr,
890 Table_Low_Bound => 0,
891 Table_Initial => 10,
892 Table_Increment => 100,
893 Table_Name => "Generic_Renamings");
894
895 -- Variable to hold enclosing instantiation. When the environment is
896 -- saved for a subprogram inlining, the corresponding Act_Id is empty.
897
898 Current_Instantiated_Parent : Assoc := (Empty, Empty, Assoc_Null);
899
900 -- Hash table for associations
901
902 HTable_Size : constant := 37;
903 type HTable_Range is range 0 .. HTable_Size - 1;
904
905 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr);
906 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr;
907 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id;
908 function Hash (F : Entity_Id) return HTable_Range;
909
910 package Generic_Renamings_HTable is new GNAT.HTable.Static_HTable (
911 Header_Num => HTable_Range,
912 Element => Assoc,
913 Elmt_Ptr => Assoc_Ptr,
914 Null_Ptr => Assoc_Null,
915 Set_Next => Set_Next_Assoc,
916 Next => Next_Assoc,
917 Key => Entity_Id,
918 Get_Key => Get_Gen_Id,
919 Hash => Hash,
920 Equal => "=");
921
922 Exchanged_Views : Elist_Id;
923 -- This list holds the private views that have been exchanged during
924 -- instantiation to restore the visibility of the generic declaration.
925 -- (see comments above). After instantiation, the current visibility is
926 -- reestablished by means of a traversal of this list.
927
928 Hidden_Entities : Elist_Id;
929 -- This list holds the entities of the current scope that are removed
930 -- from immediate visibility when instantiating a child unit. Their
931 -- visibility is restored in Remove_Parent.
932
933 -- Because instantiations can be recursive, the following must be saved
934 -- on entry and restored on exit from an instantiation (spec or body).
935 -- This is done by the two procedures Save_Env and Restore_Env. For
936 -- package and subprogram instantiations (but not for the body instances)
937 -- the action of Save_Env is done in two steps: Init_Env is called before
938 -- Check_Generic_Child_Unit, because setting the parent instances requires
939 -- that the visibility data structures be properly initialized. Once the
940 -- generic is unit is validated, Set_Instance_Env completes Save_Env.
941
942 Parent_Unit_Visible : Boolean := False;
943 -- Parent_Unit_Visible is used when the generic is a child unit, and
944 -- indicates whether the ultimate parent of the generic is visible in the
945 -- instantiation environment. It is used to reset the visibility of the
946 -- parent at the end of the instantiation (see Remove_Parent).
947
948 Instance_Parent_Unit : Entity_Id := Empty;
949 -- This records the ultimate parent unit of an instance of a generic
950 -- child unit and is used in conjunction with Parent_Unit_Visible to
951 -- indicate the unit to which the Parent_Unit_Visible flag corresponds.
952
953 type Instance_Env is record
954 Instantiated_Parent : Assoc;
955 Exchanged_Views : Elist_Id;
956 Hidden_Entities : Elist_Id;
957 Current_Sem_Unit : Unit_Number_Type;
958 Parent_Unit_Visible : Boolean := False;
959 Instance_Parent_Unit : Entity_Id := Empty;
960 Switches : Config_Switches_Type;
961 end record;
962
963 package Instance_Envs is new Table.Table (
964 Table_Component_Type => Instance_Env,
965 Table_Index_Type => Int,
966 Table_Low_Bound => 0,
967 Table_Initial => 32,
968 Table_Increment => 100,
969 Table_Name => "Instance_Envs");
970
971 procedure Restore_Private_Views
972 (Pack_Id : Entity_Id;
973 Is_Package : Boolean := True);
974 -- Restore the private views of external types, and unmark the generic
975 -- renamings of actuals, so that they become compatible subtypes again.
976 -- For subprograms, Pack_Id is the package constructed to hold the
977 -- renamings.
978
979 procedure Switch_View (T : Entity_Id);
980 -- Switch the partial and full views of a type and its private
981 -- dependents (i.e. its subtypes and derived types).
982
983 ------------------------------------
984 -- Structures for Error Reporting --
985 ------------------------------------
986
987 Instantiation_Node : Node_Id;
988 -- Used by subprograms that validate instantiation of formal parameters
989 -- where there might be no actual on which to place the error message.
990 -- Also used to locate the instantiation node for generic subunits.
991
992 Instantiation_Error : exception;
993 -- When there is a semantic error in the generic parameter matching,
994 -- there is no point in continuing the instantiation, because the
995 -- number of cascaded errors is unpredictable. This exception aborts
996 -- the instantiation process altogether.
997
998 S_Adjustment : Sloc_Adjustment;
999 -- Offset created for each node in an instantiation, in order to keep
1000 -- track of the source position of the instantiation in each of its nodes.
1001 -- A subsequent semantic error or warning on a construct of the instance
1002 -- points to both places: the original generic node, and the point of
1003 -- instantiation. See Sinput and Sinput.L for additional details.
1004
1005 ------------------------------------------------------------
1006 -- Data structure for keeping track when inside a Generic --
1007 ------------------------------------------------------------
1008
1009 -- The following table is used to save values of the Inside_A_Generic
1010 -- flag (see spec of Sem) when they are saved by Start_Generic.
1011
1012 package Generic_Flags is new Table.Table (
1013 Table_Component_Type => Boolean,
1014 Table_Index_Type => Int,
1015 Table_Low_Bound => 0,
1016 Table_Initial => 32,
1017 Table_Increment => 200,
1018 Table_Name => "Generic_Flags");
1019
1020 ---------------------------
1021 -- Abandon_Instantiation --
1022 ---------------------------
1023
1024 procedure Abandon_Instantiation (N : Node_Id) is
1025 begin
1026 Error_Msg_N ("\instantiation abandoned!", N);
1027 raise Instantiation_Error;
1028 end Abandon_Instantiation;
1029
1030 --------------------------------
1031 -- Add_Pending_Instantiation --
1032 --------------------------------
1033
1034 procedure Add_Pending_Instantiation (Inst : Node_Id; Act_Decl : Node_Id) is
1035 begin
1036
1037 -- Add to the instantiation node and the corresponding unit declaration
1038 -- the current values of global flags to be used when analyzing the
1039 -- instance body.
1040
1041 Pending_Instantiations.Append
1042 ((Inst_Node => Inst,
1043 Act_Decl => Act_Decl,
1044 Expander_Status => Expander_Active,
1045 Current_Sem_Unit => Current_Sem_Unit,
1046 Scope_Suppress => Scope_Suppress,
1047 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
1048 Version => Ada_Version,
1049 Version_Pragma => Ada_Version_Pragma,
1050 Warnings => Save_Warnings,
1051 SPARK_Mode => SPARK_Mode,
1052 SPARK_Mode_Pragma => SPARK_Mode_Pragma));
1053 end Add_Pending_Instantiation;
1054
1055 ----------------------------------
1056 -- Adjust_Inherited_Pragma_Sloc --
1057 ----------------------------------
1058
1059 procedure Adjust_Inherited_Pragma_Sloc (N : Node_Id) is
1060 begin
1061 Adjust_Instantiation_Sloc (N, S_Adjustment);
1062 end Adjust_Inherited_Pragma_Sloc;
1063
1064 --------------------------
1065 -- Analyze_Associations --
1066 --------------------------
1067
1068 function Analyze_Associations
1069 (I_Node : Node_Id;
1070 Formals : List_Id;
1071 F_Copy : List_Id) return List_Id
1072 is
1073 Actuals_To_Freeze : constant Elist_Id := New_Elmt_List;
1074 Assoc : constant List_Id := New_List;
1075 Default_Actuals : constant List_Id := New_List;
1076 Gen_Unit : constant Entity_Id :=
1077 Defining_Entity (Parent (F_Copy));
1078
1079 Actuals : List_Id;
1080 Actual : Node_Id;
1081 Analyzed_Formal : Node_Id;
1082 First_Named : Node_Id := Empty;
1083 Formal : Node_Id;
1084 Match : Node_Id;
1085 Named : Node_Id;
1086 Saved_Formal : Node_Id;
1087
1088 Default_Formals : constant List_Id := New_List;
1089 -- If an Others_Choice is present, some of the formals may be defaulted.
1090 -- To simplify the treatment of visibility in an instance, we introduce
1091 -- individual defaults for each such formal. These defaults are
1092 -- appended to the list of associations and replace the Others_Choice.
1093
1094 Found_Assoc : Node_Id;
1095 -- Association for the current formal being match. Empty if there are
1096 -- no remaining actuals, or if there is no named association with the
1097 -- name of the formal.
1098
1099 Is_Named_Assoc : Boolean;
1100 Num_Matched : Nat := 0;
1101 Num_Actuals : Nat := 0;
1102
1103 Others_Present : Boolean := False;
1104 Others_Choice : Node_Id := Empty;
1105 -- In Ada 2005, indicates partial parameterization of a formal
1106 -- package. As usual an other association must be last in the list.
1107
1108 procedure Check_Overloaded_Formal_Subprogram (Formal : Entity_Id);
1109 -- Apply RM 12.3(9): if a formal subprogram is overloaded, the instance
1110 -- cannot have a named association for it. AI05-0025 extends this rule
1111 -- to formals of formal packages by AI05-0025, and it also applies to
1112 -- box-initialized formals.
1113
1114 function Has_Fully_Defined_Profile (Subp : Entity_Id) return Boolean;
1115 -- Determine whether the parameter types and the return type of Subp
1116 -- are fully defined at the point of instantiation.
1117
1118 function Matching_Actual
1119 (F : Entity_Id;
1120 A_F : Entity_Id) return Node_Id;
1121 -- Find actual that corresponds to a given a formal parameter. If the
1122 -- actuals are positional, return the next one, if any. If the actuals
1123 -- are named, scan the parameter associations to find the right one.
1124 -- A_F is the corresponding entity in the analyzed generic, which is
1125 -- placed on the selector name for ASIS use.
1126 --
1127 -- In Ada 2005, a named association may be given with a box, in which
1128 -- case Matching_Actual sets Found_Assoc to the generic association,
1129 -- but return Empty for the actual itself. In this case the code below
1130 -- creates a corresponding declaration for the formal.
1131
1132 function Partial_Parameterization return Boolean;
1133 -- Ada 2005: if no match is found for a given formal, check if the
1134 -- association for it includes a box, or whether the associations
1135 -- include an Others clause.
1136
1137 procedure Process_Default (F : Entity_Id);
1138 -- Add a copy of the declaration of generic formal F to the list of
1139 -- associations, and add an explicit box association for F if there
1140 -- is none yet, and the default comes from an Others_Choice.
1141
1142 function Renames_Standard_Subprogram (Subp : Entity_Id) return Boolean;
1143 -- Determine whether Subp renames one of the subprograms defined in the
1144 -- generated package Standard.
1145
1146 procedure Set_Analyzed_Formal;
1147 -- Find the node in the generic copy that corresponds to a given formal.
1148 -- The semantic information on this node is used to perform legality
1149 -- checks on the actuals. Because semantic analysis can introduce some
1150 -- anonymous entities or modify the declaration node itself, the
1151 -- correspondence between the two lists is not one-one. In addition to
1152 -- anonymous types, the presence a formal equality will introduce an
1153 -- implicit declaration for the corresponding inequality.
1154
1155 ----------------------------------------
1156 -- Check_Overloaded_Formal_Subprogram --
1157 ----------------------------------------
1158
1159 procedure Check_Overloaded_Formal_Subprogram (Formal : Entity_Id) is
1160 Temp_Formal : Entity_Id;
1161
1162 begin
1163 Temp_Formal := First (Formals);
1164 while Present (Temp_Formal) loop
1165 if Nkind (Temp_Formal) in N_Formal_Subprogram_Declaration
1166 and then Temp_Formal /= Formal
1167 and then
1168 Chars (Defining_Unit_Name (Specification (Formal))) =
1169 Chars (Defining_Unit_Name (Specification (Temp_Formal)))
1170 then
1171 if Present (Found_Assoc) then
1172 Error_Msg_N
1173 ("named association not allowed for overloaded formal",
1174 Found_Assoc);
1175
1176 else
1177 Error_Msg_N
1178 ("named association not allowed for overloaded formal",
1179 Others_Choice);
1180 end if;
1181
1182 Abandon_Instantiation (Instantiation_Node);
1183 end if;
1184
1185 Next (Temp_Formal);
1186 end loop;
1187 end Check_Overloaded_Formal_Subprogram;
1188
1189 -------------------------------
1190 -- Has_Fully_Defined_Profile --
1191 -------------------------------
1192
1193 function Has_Fully_Defined_Profile (Subp : Entity_Id) return Boolean is
1194 function Is_Fully_Defined_Type (Typ : Entity_Id) return Boolean;
1195 -- Determine whethet type Typ is fully defined
1196
1197 ---------------------------
1198 -- Is_Fully_Defined_Type --
1199 ---------------------------
1200
1201 function Is_Fully_Defined_Type (Typ : Entity_Id) return Boolean is
1202 begin
1203 -- A private type without a full view is not fully defined
1204
1205 if Is_Private_Type (Typ)
1206 and then No (Full_View (Typ))
1207 then
1208 return False;
1209
1210 -- An incomplete type is never fully defined
1211
1212 elsif Is_Incomplete_Type (Typ) then
1213 return False;
1214
1215 -- All other types are fully defined
1216
1217 else
1218 return True;
1219 end if;
1220 end Is_Fully_Defined_Type;
1221
1222 -- Local declarations
1223
1224 Param : Entity_Id;
1225
1226 -- Start of processing for Has_Fully_Defined_Profile
1227
1228 begin
1229 -- Check the parameters
1230
1231 Param := First_Formal (Subp);
1232 while Present (Param) loop
1233 if not Is_Fully_Defined_Type (Etype (Param)) then
1234 return False;
1235 end if;
1236
1237 Next_Formal (Param);
1238 end loop;
1239
1240 -- Check the return type
1241
1242 return Is_Fully_Defined_Type (Etype (Subp));
1243 end Has_Fully_Defined_Profile;
1244
1245 ---------------------
1246 -- Matching_Actual --
1247 ---------------------
1248
1249 function Matching_Actual
1250 (F : Entity_Id;
1251 A_F : Entity_Id) return Node_Id
1252 is
1253 Prev : Node_Id;
1254 Act : Node_Id;
1255
1256 begin
1257 Is_Named_Assoc := False;
1258
1259 -- End of list of purely positional parameters
1260
1261 if No (Actual) or else Nkind (Actual) = N_Others_Choice then
1262 Found_Assoc := Empty;
1263 Act := Empty;
1264
1265 -- Case of positional parameter corresponding to current formal
1266
1267 elsif No (Selector_Name (Actual)) then
1268 Found_Assoc := Actual;
1269 Act := Explicit_Generic_Actual_Parameter (Actual);
1270 Num_Matched := Num_Matched + 1;
1271 Next (Actual);
1272
1273 -- Otherwise scan list of named actuals to find the one with the
1274 -- desired name. All remaining actuals have explicit names.
1275
1276 else
1277 Is_Named_Assoc := True;
1278 Found_Assoc := Empty;
1279 Act := Empty;
1280 Prev := Empty;
1281
1282 while Present (Actual) loop
1283 if Nkind (Actual) = N_Others_Choice then
1284 Found_Assoc := Empty;
1285 Act := Empty;
1286
1287 elsif Chars (Selector_Name (Actual)) = Chars (F) then
1288 Set_Entity (Selector_Name (Actual), A_F);
1289 Set_Etype (Selector_Name (Actual), Etype (A_F));
1290 Generate_Reference (A_F, Selector_Name (Actual));
1291
1292 Found_Assoc := Actual;
1293 Act := Explicit_Generic_Actual_Parameter (Actual);
1294 Num_Matched := Num_Matched + 1;
1295 exit;
1296 end if;
1297
1298 Prev := Actual;
1299 Next (Actual);
1300 end loop;
1301
1302 -- Reset for subsequent searches. In most cases the named
1303 -- associations are in order. If they are not, we reorder them
1304 -- to avoid scanning twice the same actual. This is not just a
1305 -- question of efficiency: there may be multiple defaults with
1306 -- boxes that have the same name. In a nested instantiation we
1307 -- insert actuals for those defaults, and cannot rely on their
1308 -- names to disambiguate them.
1309
1310 if Actual = First_Named then
1311 Next (First_Named);
1312
1313 elsif Present (Actual) then
1314 Insert_Before (First_Named, Remove_Next (Prev));
1315 end if;
1316
1317 Actual := First_Named;
1318 end if;
1319
1320 if Is_Entity_Name (Act) and then Present (Entity (Act)) then
1321 Set_Used_As_Generic_Actual (Entity (Act));
1322 end if;
1323
1324 return Act;
1325 end Matching_Actual;
1326
1327 ------------------------------
1328 -- Partial_Parameterization --
1329 ------------------------------
1330
1331 function Partial_Parameterization return Boolean is
1332 begin
1333 return Others_Present
1334 or else (Present (Found_Assoc) and then Box_Present (Found_Assoc));
1335 end Partial_Parameterization;
1336
1337 ---------------------
1338 -- Process_Default --
1339 ---------------------
1340
1341 procedure Process_Default (F : Entity_Id) is
1342 Loc : constant Source_Ptr := Sloc (I_Node);
1343 F_Id : constant Entity_Id := Defining_Entity (F);
1344 Decl : Node_Id;
1345 Default : Node_Id;
1346 Id : Entity_Id;
1347
1348 begin
1349 -- Append copy of formal declaration to associations, and create new
1350 -- defining identifier for it.
1351
1352 Decl := New_Copy_Tree (F);
1353 Id := Make_Defining_Identifier (Sloc (F_Id), Chars (F_Id));
1354
1355 if Nkind (F) in N_Formal_Subprogram_Declaration then
1356 Set_Defining_Unit_Name (Specification (Decl), Id);
1357
1358 else
1359 Set_Defining_Identifier (Decl, Id);
1360 end if;
1361
1362 Append (Decl, Assoc);
1363
1364 if No (Found_Assoc) then
1365 Default :=
1366 Make_Generic_Association (Loc,
1367 Selector_Name =>
1368 New_Occurrence_Of (Id, Loc),
1369 Explicit_Generic_Actual_Parameter => Empty);
1370 Set_Box_Present (Default);
1371 Append (Default, Default_Formals);
1372 end if;
1373 end Process_Default;
1374
1375 ---------------------------------
1376 -- Renames_Standard_Subprogram --
1377 ---------------------------------
1378
1379 function Renames_Standard_Subprogram (Subp : Entity_Id) return Boolean is
1380 Id : Entity_Id;
1381
1382 begin
1383 Id := Alias (Subp);
1384 while Present (Id) loop
1385 if Scope (Id) = Standard_Standard then
1386 return True;
1387 end if;
1388
1389 Id := Alias (Id);
1390 end loop;
1391
1392 return False;
1393 end Renames_Standard_Subprogram;
1394
1395 -------------------------
1396 -- Set_Analyzed_Formal --
1397 -------------------------
1398
1399 procedure Set_Analyzed_Formal is
1400 Kind : Node_Kind;
1401
1402 begin
1403 while Present (Analyzed_Formal) loop
1404 Kind := Nkind (Analyzed_Formal);
1405
1406 case Nkind (Formal) is
1407
1408 when N_Formal_Subprogram_Declaration =>
1409 exit when Kind in N_Formal_Subprogram_Declaration
1410 and then
1411 Chars
1412 (Defining_Unit_Name (Specification (Formal))) =
1413 Chars
1414 (Defining_Unit_Name (Specification (Analyzed_Formal)));
1415
1416 when N_Formal_Package_Declaration =>
1417 exit when Nkind_In (Kind, N_Formal_Package_Declaration,
1418 N_Generic_Package_Declaration,
1419 N_Package_Declaration);
1420
1421 when N_Use_Package_Clause | N_Use_Type_Clause => exit;
1422
1423 when others =>
1424
1425 -- Skip freeze nodes, and nodes inserted to replace
1426 -- unrecognized pragmas.
1427
1428 exit when
1429 Kind not in N_Formal_Subprogram_Declaration
1430 and then not Nkind_In (Kind, N_Subprogram_Declaration,
1431 N_Freeze_Entity,
1432 N_Null_Statement,
1433 N_Itype_Reference)
1434 and then Chars (Defining_Identifier (Formal)) =
1435 Chars (Defining_Identifier (Analyzed_Formal));
1436 end case;
1437
1438 Next (Analyzed_Formal);
1439 end loop;
1440 end Set_Analyzed_Formal;
1441
1442 -- Start of processing for Analyze_Associations
1443
1444 begin
1445 Actuals := Generic_Associations (I_Node);
1446
1447 if Present (Actuals) then
1448
1449 -- Check for an Others choice, indicating a partial parameterization
1450 -- for a formal package.
1451
1452 Actual := First (Actuals);
1453 while Present (Actual) loop
1454 if Nkind (Actual) = N_Others_Choice then
1455 Others_Present := True;
1456 Others_Choice := Actual;
1457
1458 if Present (Next (Actual)) then
1459 Error_Msg_N ("others must be last association", Actual);
1460 end if;
1461
1462 -- This subprogram is used both for formal packages and for
1463 -- instantiations. For the latter, associations must all be
1464 -- explicit.
1465
1466 if Nkind (I_Node) /= N_Formal_Package_Declaration
1467 and then Comes_From_Source (I_Node)
1468 then
1469 Error_Msg_N
1470 ("others association not allowed in an instance",
1471 Actual);
1472 end if;
1473
1474 -- In any case, nothing to do after the others association
1475
1476 exit;
1477
1478 elsif Box_Present (Actual)
1479 and then Comes_From_Source (I_Node)
1480 and then Nkind (I_Node) /= N_Formal_Package_Declaration
1481 then
1482 Error_Msg_N
1483 ("box association not allowed in an instance", Actual);
1484 end if;
1485
1486 Next (Actual);
1487 end loop;
1488
1489 -- If named associations are present, save first named association
1490 -- (it may of course be Empty) to facilitate subsequent name search.
1491
1492 First_Named := First (Actuals);
1493 while Present (First_Named)
1494 and then Nkind (First_Named) /= N_Others_Choice
1495 and then No (Selector_Name (First_Named))
1496 loop
1497 Num_Actuals := Num_Actuals + 1;
1498 Next (First_Named);
1499 end loop;
1500 end if;
1501
1502 Named := First_Named;
1503 while Present (Named) loop
1504 if Nkind (Named) /= N_Others_Choice
1505 and then No (Selector_Name (Named))
1506 then
1507 Error_Msg_N ("invalid positional actual after named one", Named);
1508 Abandon_Instantiation (Named);
1509 end if;
1510
1511 -- A named association may lack an actual parameter, if it was
1512 -- introduced for a default subprogram that turns out to be local
1513 -- to the outer instantiation. If it has a box association it must
1514 -- correspond to some formal in the generic.
1515
1516 if Nkind (Named) /= N_Others_Choice
1517 and then (Present (Explicit_Generic_Actual_Parameter (Named))
1518 or else Box_Present (Named))
1519 then
1520 Num_Actuals := Num_Actuals + 1;
1521 end if;
1522
1523 Next (Named);
1524 end loop;
1525
1526 if Present (Formals) then
1527 Formal := First_Non_Pragma (Formals);
1528 Analyzed_Formal := First_Non_Pragma (F_Copy);
1529
1530 if Present (Actuals) then
1531 Actual := First (Actuals);
1532
1533 -- All formals should have default values
1534
1535 else
1536 Actual := Empty;
1537 end if;
1538
1539 while Present (Formal) loop
1540 Set_Analyzed_Formal;
1541 Saved_Formal := Next_Non_Pragma (Formal);
1542
1543 case Nkind (Formal) is
1544 when N_Formal_Object_Declaration =>
1545 Match :=
1546 Matching_Actual
1547 (Defining_Identifier (Formal),
1548 Defining_Identifier (Analyzed_Formal));
1549
1550 if No (Match) and then Partial_Parameterization then
1551 Process_Default (Formal);
1552
1553 else
1554 Append_List
1555 (Instantiate_Object (Formal, Match, Analyzed_Formal),
1556 Assoc);
1557
1558 -- For a defaulted in_parameter, create an entry in the
1559 -- the list of defaulted actuals, for GNATProve use. Do
1560 -- not included these defaults for an instance nested
1561 -- within a generic, because the defaults are also used
1562 -- in the analysis of the enclosing generic, and only
1563 -- defaulted subprograms are relevant there.
1564
1565 if No (Match) and then not Inside_A_Generic then
1566 Append_To (Default_Actuals,
1567 Make_Generic_Association (Sloc (I_Node),
1568 Selector_Name =>
1569 New_Occurrence_Of
1570 (Defining_Identifier (Formal), Sloc (I_Node)),
1571 Explicit_Generic_Actual_Parameter =>
1572 New_Copy_Tree (Default_Expression (Formal))));
1573 end if;
1574 end if;
1575
1576 -- If the object is a call to an expression function, this
1577 -- is a freezing point for it.
1578
1579 if Is_Entity_Name (Match)
1580 and then Present (Entity (Match))
1581 and then Nkind
1582 (Original_Node (Unit_Declaration_Node (Entity (Match))))
1583 = N_Expression_Function
1584 then
1585 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1586 end if;
1587
1588 when N_Formal_Type_Declaration =>
1589 Match :=
1590 Matching_Actual
1591 (Defining_Identifier (Formal),
1592 Defining_Identifier (Analyzed_Formal));
1593
1594 if No (Match) then
1595 if Partial_Parameterization then
1596 Process_Default (Formal);
1597
1598 else
1599 Error_Msg_Sloc := Sloc (Gen_Unit);
1600 Error_Msg_NE
1601 ("missing actual&",
1602 Instantiation_Node, Defining_Identifier (Formal));
1603 Error_Msg_NE
1604 ("\in instantiation of & declared#",
1605 Instantiation_Node, Gen_Unit);
1606 Abandon_Instantiation (Instantiation_Node);
1607 end if;
1608
1609 else
1610 Analyze (Match);
1611 Append_List
1612 (Instantiate_Type
1613 (Formal, Match, Analyzed_Formal, Assoc),
1614 Assoc);
1615
1616 -- An instantiation is a freeze point for the actuals,
1617 -- unless this is a rewritten formal package, or the
1618 -- formal is an Ada 2012 formal incomplete type.
1619
1620 if Nkind (I_Node) = N_Formal_Package_Declaration
1621 or else
1622 (Ada_Version >= Ada_2012
1623 and then
1624 Ekind (Defining_Identifier (Analyzed_Formal)) =
1625 E_Incomplete_Type)
1626 then
1627 null;
1628
1629 else
1630 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1631 end if;
1632 end if;
1633
1634 -- A remote access-to-class-wide type is not a legal actual
1635 -- for a generic formal of an access type (E.2.2(17/2)).
1636 -- In GNAT an exception to this rule is introduced when
1637 -- the formal is marked as remote using implementation
1638 -- defined aspect/pragma Remote_Access_Type. In that case
1639 -- the actual must be remote as well.
1640
1641 -- If the current instantiation is the construction of a
1642 -- local copy for a formal package the actuals may be
1643 -- defaulted, and there is no matching actual to check.
1644
1645 if Nkind (Analyzed_Formal) = N_Formal_Type_Declaration
1646 and then
1647 Nkind (Formal_Type_Definition (Analyzed_Formal)) =
1648 N_Access_To_Object_Definition
1649 and then Present (Match)
1650 then
1651 declare
1652 Formal_Ent : constant Entity_Id :=
1653 Defining_Identifier (Analyzed_Formal);
1654 begin
1655 if Is_Remote_Access_To_Class_Wide_Type (Entity (Match))
1656 = Is_Remote_Types (Formal_Ent)
1657 then
1658 -- Remoteness of formal and actual match
1659
1660 null;
1661
1662 elsif Is_Remote_Types (Formal_Ent) then
1663
1664 -- Remote formal, non-remote actual
1665
1666 Error_Msg_NE
1667 ("actual for& must be remote", Match, Formal_Ent);
1668
1669 else
1670 -- Non-remote formal, remote actual
1671
1672 Error_Msg_NE
1673 ("actual for& may not be remote",
1674 Match, Formal_Ent);
1675 end if;
1676 end;
1677 end if;
1678
1679 when N_Formal_Subprogram_Declaration =>
1680 Match :=
1681 Matching_Actual
1682 (Defining_Unit_Name (Specification (Formal)),
1683 Defining_Unit_Name (Specification (Analyzed_Formal)));
1684
1685 -- If the formal subprogram has the same name as another
1686 -- formal subprogram of the generic, then a named
1687 -- association is illegal (12.3(9)). Exclude named
1688 -- associations that are generated for a nested instance.
1689
1690 if Present (Match)
1691 and then Is_Named_Assoc
1692 and then Comes_From_Source (Found_Assoc)
1693 then
1694 Check_Overloaded_Formal_Subprogram (Formal);
1695 end if;
1696
1697 -- If there is no corresponding actual, this may be case
1698 -- of partial parameterization, or else the formal has a
1699 -- default or a box.
1700
1701 if No (Match) and then Partial_Parameterization then
1702 Process_Default (Formal);
1703
1704 if Nkind (I_Node) = N_Formal_Package_Declaration then
1705 Check_Overloaded_Formal_Subprogram (Formal);
1706 end if;
1707
1708 else
1709 Append_To (Assoc,
1710 Instantiate_Formal_Subprogram
1711 (Formal, Match, Analyzed_Formal));
1712
1713 -- An instantiation is a freeze point for the actuals,
1714 -- unless this is a rewritten formal package.
1715
1716 if Nkind (I_Node) /= N_Formal_Package_Declaration
1717 and then Nkind (Match) = N_Identifier
1718 and then Is_Subprogram (Entity (Match))
1719
1720 -- The actual subprogram may rename a routine defined
1721 -- in Standard. Avoid freezing such renamings because
1722 -- subprograms coming from Standard cannot be frozen.
1723
1724 and then
1725 not Renames_Standard_Subprogram (Entity (Match))
1726
1727 -- If the actual subprogram comes from a different
1728 -- unit, it is already frozen, either by a body in
1729 -- that unit or by the end of the declarative part
1730 -- of the unit. This check avoids the freezing of
1731 -- subprograms defined in Standard which are used
1732 -- as generic actuals.
1733
1734 and then In_Same_Code_Unit (Entity (Match), I_Node)
1735 and then Has_Fully_Defined_Profile (Entity (Match))
1736 then
1737 -- Mark the subprogram as having a delayed freeze
1738 -- since this may be an out-of-order action.
1739
1740 Set_Has_Delayed_Freeze (Entity (Match));
1741 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1742 end if;
1743 end if;
1744
1745 -- If this is a nested generic, preserve default for later
1746 -- instantiations. We do this as well for GNATProve use,
1747 -- so that the list of generic associations is complete.
1748
1749 if No (Match) and then Box_Present (Formal) then
1750 declare
1751 Subp : constant Entity_Id :=
1752 Defining_Unit_Name (Specification (Last (Assoc)));
1753
1754 begin
1755 Append_To (Default_Actuals,
1756 Make_Generic_Association (Sloc (I_Node),
1757 Selector_Name =>
1758 New_Occurrence_Of (Subp, Sloc (I_Node)),
1759 Explicit_Generic_Actual_Parameter =>
1760 New_Occurrence_Of (Subp, Sloc (I_Node))));
1761 end;
1762 end if;
1763
1764 when N_Formal_Package_Declaration =>
1765 Match :=
1766 Matching_Actual
1767 (Defining_Identifier (Formal),
1768 Defining_Identifier (Original_Node (Analyzed_Formal)));
1769
1770 if No (Match) then
1771 if Partial_Parameterization then
1772 Process_Default (Formal);
1773
1774 else
1775 Error_Msg_Sloc := Sloc (Gen_Unit);
1776 Error_Msg_NE
1777 ("missing actual&",
1778 Instantiation_Node, Defining_Identifier (Formal));
1779 Error_Msg_NE
1780 ("\in instantiation of & declared#",
1781 Instantiation_Node, Gen_Unit);
1782
1783 Abandon_Instantiation (Instantiation_Node);
1784 end if;
1785
1786 else
1787 Analyze (Match);
1788 Append_List
1789 (Instantiate_Formal_Package
1790 (Formal, Match, Analyzed_Formal),
1791 Assoc);
1792 end if;
1793
1794 -- For use type and use package appearing in the generic part,
1795 -- we have already copied them, so we can just move them where
1796 -- they belong (we mustn't recopy them since this would mess up
1797 -- the Sloc values).
1798
1799 when N_Use_Package_Clause |
1800 N_Use_Type_Clause =>
1801 if Nkind (Original_Node (I_Node)) =
1802 N_Formal_Package_Declaration
1803 then
1804 Append (New_Copy_Tree (Formal), Assoc);
1805 else
1806 Remove (Formal);
1807 Append (Formal, Assoc);
1808 end if;
1809
1810 when others =>
1811 raise Program_Error;
1812
1813 end case;
1814
1815 Formal := Saved_Formal;
1816 Next_Non_Pragma (Analyzed_Formal);
1817 end loop;
1818
1819 if Num_Actuals > Num_Matched then
1820 Error_Msg_Sloc := Sloc (Gen_Unit);
1821
1822 if Present (Selector_Name (Actual)) then
1823 Error_Msg_NE
1824 ("unmatched actual &", Actual, Selector_Name (Actual));
1825 Error_Msg_NE
1826 ("\in instantiation of & declared#", Actual, Gen_Unit);
1827 else
1828 Error_Msg_NE
1829 ("unmatched actual in instantiation of & declared#",
1830 Actual, Gen_Unit);
1831 end if;
1832 end if;
1833
1834 elsif Present (Actuals) then
1835 Error_Msg_N
1836 ("too many actuals in generic instantiation", Instantiation_Node);
1837 end if;
1838
1839 -- An instantiation freezes all generic actuals. The only exceptions
1840 -- to this are incomplete types and subprograms which are not fully
1841 -- defined at the point of instantiation.
1842
1843 declare
1844 Elmt : Elmt_Id := First_Elmt (Actuals_To_Freeze);
1845 begin
1846 while Present (Elmt) loop
1847 Freeze_Before (I_Node, Node (Elmt));
1848 Next_Elmt (Elmt);
1849 end loop;
1850 end;
1851
1852 -- If there are default subprograms, normalize the tree by adding
1853 -- explicit associations for them. This is required if the instance
1854 -- appears within a generic.
1855
1856 if not Is_Empty_List (Default_Actuals) then
1857 declare
1858 Default : Node_Id;
1859
1860 begin
1861 Default := First (Default_Actuals);
1862 while Present (Default) loop
1863 Mark_Rewrite_Insertion (Default);
1864 Next (Default);
1865 end loop;
1866
1867 if No (Actuals) then
1868 Set_Generic_Associations (I_Node, Default_Actuals);
1869 else
1870 Append_List_To (Actuals, Default_Actuals);
1871 end if;
1872 end;
1873 end if;
1874
1875 -- If this is a formal package, normalize the parameter list by adding
1876 -- explicit box associations for the formals that are covered by an
1877 -- Others_Choice.
1878
1879 if not Is_Empty_List (Default_Formals) then
1880 Append_List (Default_Formals, Formals);
1881 end if;
1882
1883 return Assoc;
1884 end Analyze_Associations;
1885
1886 -------------------------------
1887 -- Analyze_Formal_Array_Type --
1888 -------------------------------
1889
1890 procedure Analyze_Formal_Array_Type
1891 (T : in out Entity_Id;
1892 Def : Node_Id)
1893 is
1894 DSS : Node_Id;
1895
1896 begin
1897 -- Treated like a non-generic array declaration, with additional
1898 -- semantic checks.
1899
1900 Enter_Name (T);
1901
1902 if Nkind (Def) = N_Constrained_Array_Definition then
1903 DSS := First (Discrete_Subtype_Definitions (Def));
1904 while Present (DSS) loop
1905 if Nkind_In (DSS, N_Subtype_Indication,
1906 N_Range,
1907 N_Attribute_Reference)
1908 then
1909 Error_Msg_N ("only a subtype mark is allowed in a formal", DSS);
1910 end if;
1911
1912 Next (DSS);
1913 end loop;
1914 end if;
1915
1916 Array_Type_Declaration (T, Def);
1917 Set_Is_Generic_Type (Base_Type (T));
1918
1919 if Ekind (Component_Type (T)) = E_Incomplete_Type
1920 and then No (Full_View (Component_Type (T)))
1921 then
1922 Error_Msg_N ("premature usage of incomplete type", Def);
1923
1924 -- Check that range constraint is not allowed on the component type
1925 -- of a generic formal array type (AARM 12.5.3(3))
1926
1927 elsif Is_Internal (Component_Type (T))
1928 and then Present (Subtype_Indication (Component_Definition (Def)))
1929 and then Nkind (Original_Node
1930 (Subtype_Indication (Component_Definition (Def)))) =
1931 N_Subtype_Indication
1932 then
1933 Error_Msg_N
1934 ("in a formal, a subtype indication can only be "
1935 & "a subtype mark (RM 12.5.3(3))",
1936 Subtype_Indication (Component_Definition (Def)));
1937 end if;
1938
1939 end Analyze_Formal_Array_Type;
1940
1941 ---------------------------------------------
1942 -- Analyze_Formal_Decimal_Fixed_Point_Type --
1943 ---------------------------------------------
1944
1945 -- As for other generic types, we create a valid type representation with
1946 -- legal but arbitrary attributes, whose values are never considered
1947 -- static. For all scalar types we introduce an anonymous base type, with
1948 -- the same attributes. We choose the corresponding integer type to be
1949 -- Standard_Integer.
1950 -- Here and in other similar routines, the Sloc of the generated internal
1951 -- type must be the same as the sloc of the defining identifier of the
1952 -- formal type declaration, to provide proper source navigation.
1953
1954 procedure Analyze_Formal_Decimal_Fixed_Point_Type
1955 (T : Entity_Id;
1956 Def : Node_Id)
1957 is
1958 Loc : constant Source_Ptr := Sloc (Def);
1959
1960 Base : constant Entity_Id :=
1961 New_Internal_Entity
1962 (E_Decimal_Fixed_Point_Type,
1963 Current_Scope,
1964 Sloc (Defining_Identifier (Parent (Def))), 'G');
1965
1966 Int_Base : constant Entity_Id := Standard_Integer;
1967 Delta_Val : constant Ureal := Ureal_1;
1968 Digs_Val : constant Uint := Uint_6;
1969
1970 function Make_Dummy_Bound return Node_Id;
1971 -- Return a properly typed universal real literal to use as a bound
1972
1973 ----------------------
1974 -- Make_Dummy_Bound --
1975 ----------------------
1976
1977 function Make_Dummy_Bound return Node_Id is
1978 Bound : constant Node_Id := Make_Real_Literal (Loc, Ureal_1);
1979 begin
1980 Set_Etype (Bound, Universal_Real);
1981 return Bound;
1982 end Make_Dummy_Bound;
1983
1984 -- Start of processing for Analyze_Formal_Decimal_Fixed_Point_Type
1985
1986 begin
1987 Enter_Name (T);
1988
1989 Set_Etype (Base, Base);
1990 Set_Size_Info (Base, Int_Base);
1991 Set_RM_Size (Base, RM_Size (Int_Base));
1992 Set_First_Rep_Item (Base, First_Rep_Item (Int_Base));
1993 Set_Digits_Value (Base, Digs_Val);
1994 Set_Delta_Value (Base, Delta_Val);
1995 Set_Small_Value (Base, Delta_Val);
1996 Set_Scalar_Range (Base,
1997 Make_Range (Loc,
1998 Low_Bound => Make_Dummy_Bound,
1999 High_Bound => Make_Dummy_Bound));
2000
2001 Set_Is_Generic_Type (Base);
2002 Set_Parent (Base, Parent (Def));
2003
2004 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
2005 Set_Etype (T, Base);
2006 Set_Size_Info (T, Int_Base);
2007 Set_RM_Size (T, RM_Size (Int_Base));
2008 Set_First_Rep_Item (T, First_Rep_Item (Int_Base));
2009 Set_Digits_Value (T, Digs_Val);
2010 Set_Delta_Value (T, Delta_Val);
2011 Set_Small_Value (T, Delta_Val);
2012 Set_Scalar_Range (T, Scalar_Range (Base));
2013 Set_Is_Constrained (T);
2014
2015 Check_Restriction (No_Fixed_Point, Def);
2016 end Analyze_Formal_Decimal_Fixed_Point_Type;
2017
2018 -------------------------------------------
2019 -- Analyze_Formal_Derived_Interface_Type --
2020 -------------------------------------------
2021
2022 procedure Analyze_Formal_Derived_Interface_Type
2023 (N : Node_Id;
2024 T : Entity_Id;
2025 Def : Node_Id)
2026 is
2027 Loc : constant Source_Ptr := Sloc (Def);
2028
2029 begin
2030 -- Rewrite as a type declaration of a derived type. This ensures that
2031 -- the interface list and primitive operations are properly captured.
2032
2033 Rewrite (N,
2034 Make_Full_Type_Declaration (Loc,
2035 Defining_Identifier => T,
2036 Type_Definition => Def));
2037 Analyze (N);
2038 Set_Is_Generic_Type (T);
2039 end Analyze_Formal_Derived_Interface_Type;
2040
2041 ---------------------------------
2042 -- Analyze_Formal_Derived_Type --
2043 ---------------------------------
2044
2045 procedure Analyze_Formal_Derived_Type
2046 (N : Node_Id;
2047 T : Entity_Id;
2048 Def : Node_Id)
2049 is
2050 Loc : constant Source_Ptr := Sloc (Def);
2051 Unk_Disc : constant Boolean := Unknown_Discriminants_Present (N);
2052 New_N : Node_Id;
2053
2054 begin
2055 Set_Is_Generic_Type (T);
2056
2057 if Private_Present (Def) then
2058 New_N :=
2059 Make_Private_Extension_Declaration (Loc,
2060 Defining_Identifier => T,
2061 Discriminant_Specifications => Discriminant_Specifications (N),
2062 Unknown_Discriminants_Present => Unk_Disc,
2063 Subtype_Indication => Subtype_Mark (Def),
2064 Interface_List => Interface_List (Def));
2065
2066 Set_Abstract_Present (New_N, Abstract_Present (Def));
2067 Set_Limited_Present (New_N, Limited_Present (Def));
2068 Set_Synchronized_Present (New_N, Synchronized_Present (Def));
2069
2070 else
2071 New_N :=
2072 Make_Full_Type_Declaration (Loc,
2073 Defining_Identifier => T,
2074 Discriminant_Specifications =>
2075 Discriminant_Specifications (Parent (T)),
2076 Type_Definition =>
2077 Make_Derived_Type_Definition (Loc,
2078 Subtype_Indication => Subtype_Mark (Def)));
2079
2080 Set_Abstract_Present
2081 (Type_Definition (New_N), Abstract_Present (Def));
2082 Set_Limited_Present
2083 (Type_Definition (New_N), Limited_Present (Def));
2084 end if;
2085
2086 Rewrite (N, New_N);
2087 Analyze (N);
2088
2089 if Unk_Disc then
2090 if not Is_Composite_Type (T) then
2091 Error_Msg_N
2092 ("unknown discriminants not allowed for elementary types", N);
2093 else
2094 Set_Has_Unknown_Discriminants (T);
2095 Set_Is_Constrained (T, False);
2096 end if;
2097 end if;
2098
2099 -- If the parent type has a known size, so does the formal, which makes
2100 -- legal representation clauses that involve the formal.
2101
2102 Set_Size_Known_At_Compile_Time
2103 (T, Size_Known_At_Compile_Time (Entity (Subtype_Mark (Def))));
2104 end Analyze_Formal_Derived_Type;
2105
2106 ----------------------------------
2107 -- Analyze_Formal_Discrete_Type --
2108 ----------------------------------
2109
2110 -- The operations defined for a discrete types are those of an enumeration
2111 -- type. The size is set to an arbitrary value, for use in analyzing the
2112 -- generic unit.
2113
2114 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id) is
2115 Loc : constant Source_Ptr := Sloc (Def);
2116 Lo : Node_Id;
2117 Hi : Node_Id;
2118
2119 Base : constant Entity_Id :=
2120 New_Internal_Entity
2121 (E_Floating_Point_Type, Current_Scope,
2122 Sloc (Defining_Identifier (Parent (Def))), 'G');
2123
2124 begin
2125 Enter_Name (T);
2126 Set_Ekind (T, E_Enumeration_Subtype);
2127 Set_Etype (T, Base);
2128 Init_Size (T, 8);
2129 Init_Alignment (T);
2130 Set_Is_Generic_Type (T);
2131 Set_Is_Constrained (T);
2132
2133 -- For semantic analysis, the bounds of the type must be set to some
2134 -- non-static value. The simplest is to create attribute nodes for those
2135 -- bounds, that refer to the type itself. These bounds are never
2136 -- analyzed but serve as place-holders.
2137
2138 Lo :=
2139 Make_Attribute_Reference (Loc,
2140 Attribute_Name => Name_First,
2141 Prefix => New_Occurrence_Of (T, Loc));
2142 Set_Etype (Lo, T);
2143
2144 Hi :=
2145 Make_Attribute_Reference (Loc,
2146 Attribute_Name => Name_Last,
2147 Prefix => New_Occurrence_Of (T, Loc));
2148 Set_Etype (Hi, T);
2149
2150 Set_Scalar_Range (T,
2151 Make_Range (Loc,
2152 Low_Bound => Lo,
2153 High_Bound => Hi));
2154
2155 Set_Ekind (Base, E_Enumeration_Type);
2156 Set_Etype (Base, Base);
2157 Init_Size (Base, 8);
2158 Init_Alignment (Base);
2159 Set_Is_Generic_Type (Base);
2160 Set_Scalar_Range (Base, Scalar_Range (T));
2161 Set_Parent (Base, Parent (Def));
2162 end Analyze_Formal_Discrete_Type;
2163
2164 ----------------------------------
2165 -- Analyze_Formal_Floating_Type --
2166 ---------------------------------
2167
2168 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id) is
2169 Base : constant Entity_Id :=
2170 New_Internal_Entity
2171 (E_Floating_Point_Type, Current_Scope,
2172 Sloc (Defining_Identifier (Parent (Def))), 'G');
2173
2174 begin
2175 -- The various semantic attributes are taken from the predefined type
2176 -- Float, just so that all of them are initialized. Their values are
2177 -- never used because no constant folding or expansion takes place in
2178 -- the generic itself.
2179
2180 Enter_Name (T);
2181 Set_Ekind (T, E_Floating_Point_Subtype);
2182 Set_Etype (T, Base);
2183 Set_Size_Info (T, (Standard_Float));
2184 Set_RM_Size (T, RM_Size (Standard_Float));
2185 Set_Digits_Value (T, Digits_Value (Standard_Float));
2186 Set_Scalar_Range (T, Scalar_Range (Standard_Float));
2187 Set_Is_Constrained (T);
2188
2189 Set_Is_Generic_Type (Base);
2190 Set_Etype (Base, Base);
2191 Set_Size_Info (Base, (Standard_Float));
2192 Set_RM_Size (Base, RM_Size (Standard_Float));
2193 Set_Digits_Value (Base, Digits_Value (Standard_Float));
2194 Set_Scalar_Range (Base, Scalar_Range (Standard_Float));
2195 Set_Parent (Base, Parent (Def));
2196
2197 Check_Restriction (No_Floating_Point, Def);
2198 end Analyze_Formal_Floating_Type;
2199
2200 -----------------------------------
2201 -- Analyze_Formal_Interface_Type;--
2202 -----------------------------------
2203
2204 procedure Analyze_Formal_Interface_Type
2205 (N : Node_Id;
2206 T : Entity_Id;
2207 Def : Node_Id)
2208 is
2209 Loc : constant Source_Ptr := Sloc (N);
2210 New_N : Node_Id;
2211
2212 begin
2213 New_N :=
2214 Make_Full_Type_Declaration (Loc,
2215 Defining_Identifier => T,
2216 Type_Definition => Def);
2217
2218 Rewrite (N, New_N);
2219 Analyze (N);
2220 Set_Is_Generic_Type (T);
2221 end Analyze_Formal_Interface_Type;
2222
2223 ---------------------------------
2224 -- Analyze_Formal_Modular_Type --
2225 ---------------------------------
2226
2227 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id) is
2228 begin
2229 -- Apart from their entity kind, generic modular types are treated like
2230 -- signed integer types, and have the same attributes.
2231
2232 Analyze_Formal_Signed_Integer_Type (T, Def);
2233 Set_Ekind (T, E_Modular_Integer_Subtype);
2234 Set_Ekind (Etype (T), E_Modular_Integer_Type);
2235
2236 end Analyze_Formal_Modular_Type;
2237
2238 ---------------------------------------
2239 -- Analyze_Formal_Object_Declaration --
2240 ---------------------------------------
2241
2242 procedure Analyze_Formal_Object_Declaration (N : Node_Id) is
2243 E : constant Node_Id := Default_Expression (N);
2244 Id : constant Node_Id := Defining_Identifier (N);
2245 K : Entity_Kind;
2246 T : Node_Id;
2247
2248 begin
2249 Enter_Name (Id);
2250
2251 -- Determine the mode of the formal object
2252
2253 if Out_Present (N) then
2254 K := E_Generic_In_Out_Parameter;
2255
2256 if not In_Present (N) then
2257 Error_Msg_N ("formal generic objects cannot have mode OUT", N);
2258 end if;
2259
2260 else
2261 K := E_Generic_In_Parameter;
2262 end if;
2263
2264 if Present (Subtype_Mark (N)) then
2265 Find_Type (Subtype_Mark (N));
2266 T := Entity (Subtype_Mark (N));
2267
2268 -- Verify that there is no redundant null exclusion
2269
2270 if Null_Exclusion_Present (N) then
2271 if not Is_Access_Type (T) then
2272 Error_Msg_N
2273 ("null exclusion can only apply to an access type", N);
2274
2275 elsif Can_Never_Be_Null (T) then
2276 Error_Msg_NE
2277 ("`NOT NULL` not allowed (& already excludes null)", N, T);
2278 end if;
2279 end if;
2280
2281 -- Ada 2005 (AI-423): Formal object with an access definition
2282
2283 else
2284 Check_Access_Definition (N);
2285 T := Access_Definition
2286 (Related_Nod => N,
2287 N => Access_Definition (N));
2288 end if;
2289
2290 if Ekind (T) = E_Incomplete_Type then
2291 declare
2292 Error_Node : Node_Id;
2293
2294 begin
2295 if Present (Subtype_Mark (N)) then
2296 Error_Node := Subtype_Mark (N);
2297 else
2298 Check_Access_Definition (N);
2299 Error_Node := Access_Definition (N);
2300 end if;
2301
2302 Error_Msg_N ("premature usage of incomplete type", Error_Node);
2303 end;
2304 end if;
2305
2306 if K = E_Generic_In_Parameter then
2307
2308 -- Ada 2005 (AI-287): Limited aggregates allowed in generic formals
2309
2310 if Ada_Version < Ada_2005 and then Is_Limited_Type (T) then
2311 Error_Msg_N
2312 ("generic formal of mode IN must not be of limited type", N);
2313 Explain_Limited_Type (T, N);
2314 end if;
2315
2316 if Is_Abstract_Type (T) then
2317 Error_Msg_N
2318 ("generic formal of mode IN must not be of abstract type", N);
2319 end if;
2320
2321 if Present (E) then
2322 Preanalyze_Spec_Expression (E, T);
2323
2324 if Is_Limited_Type (T) and then not OK_For_Limited_Init (T, E) then
2325 Error_Msg_N
2326 ("initialization not allowed for limited types", E);
2327 Explain_Limited_Type (T, E);
2328 end if;
2329 end if;
2330
2331 Set_Ekind (Id, K);
2332 Set_Etype (Id, T);
2333
2334 -- Case of generic IN OUT parameter
2335
2336 else
2337 -- If the formal has an unconstrained type, construct its actual
2338 -- subtype, as is done for subprogram formals. In this fashion, all
2339 -- its uses can refer to specific bounds.
2340
2341 Set_Ekind (Id, K);
2342 Set_Etype (Id, T);
2343
2344 if (Is_Array_Type (T) and then not Is_Constrained (T))
2345 or else (Ekind (T) = E_Record_Type and then Has_Discriminants (T))
2346 then
2347 declare
2348 Non_Freezing_Ref : constant Node_Id :=
2349 New_Occurrence_Of (Id, Sloc (Id));
2350 Decl : Node_Id;
2351
2352 begin
2353 -- Make sure the actual subtype doesn't generate bogus freezing
2354
2355 Set_Must_Not_Freeze (Non_Freezing_Ref);
2356 Decl := Build_Actual_Subtype (T, Non_Freezing_Ref);
2357 Insert_Before_And_Analyze (N, Decl);
2358 Set_Actual_Subtype (Id, Defining_Identifier (Decl));
2359 end;
2360 else
2361 Set_Actual_Subtype (Id, T);
2362 end if;
2363
2364 if Present (E) then
2365 Error_Msg_N
2366 ("initialization not allowed for `IN OUT` formals", N);
2367 end if;
2368 end if;
2369
2370 if Has_Aspects (N) then
2371 Analyze_Aspect_Specifications (N, Id);
2372 end if;
2373 end Analyze_Formal_Object_Declaration;
2374
2375 ----------------------------------------------
2376 -- Analyze_Formal_Ordinary_Fixed_Point_Type --
2377 ----------------------------------------------
2378
2379 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
2380 (T : Entity_Id;
2381 Def : Node_Id)
2382 is
2383 Loc : constant Source_Ptr := Sloc (Def);
2384 Base : constant Entity_Id :=
2385 New_Internal_Entity
2386 (E_Ordinary_Fixed_Point_Type, Current_Scope,
2387 Sloc (Defining_Identifier (Parent (Def))), 'G');
2388
2389 begin
2390 -- The semantic attributes are set for completeness only, their values
2391 -- will never be used, since all properties of the type are non-static.
2392
2393 Enter_Name (T);
2394 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
2395 Set_Etype (T, Base);
2396 Set_Size_Info (T, Standard_Integer);
2397 Set_RM_Size (T, RM_Size (Standard_Integer));
2398 Set_Small_Value (T, Ureal_1);
2399 Set_Delta_Value (T, Ureal_1);
2400 Set_Scalar_Range (T,
2401 Make_Range (Loc,
2402 Low_Bound => Make_Real_Literal (Loc, Ureal_1),
2403 High_Bound => Make_Real_Literal (Loc, Ureal_1)));
2404 Set_Is_Constrained (T);
2405
2406 Set_Is_Generic_Type (Base);
2407 Set_Etype (Base, Base);
2408 Set_Size_Info (Base, Standard_Integer);
2409 Set_RM_Size (Base, RM_Size (Standard_Integer));
2410 Set_Small_Value (Base, Ureal_1);
2411 Set_Delta_Value (Base, Ureal_1);
2412 Set_Scalar_Range (Base, Scalar_Range (T));
2413 Set_Parent (Base, Parent (Def));
2414
2415 Check_Restriction (No_Fixed_Point, Def);
2416 end Analyze_Formal_Ordinary_Fixed_Point_Type;
2417
2418 ----------------------------------------
2419 -- Analyze_Formal_Package_Declaration --
2420 ----------------------------------------
2421
2422 procedure Analyze_Formal_Package_Declaration (N : Node_Id) is
2423 Gen_Id : constant Node_Id := Name (N);
2424 Loc : constant Source_Ptr := Sloc (N);
2425 Pack_Id : constant Entity_Id := Defining_Identifier (N);
2426 Formal : Entity_Id;
2427 Gen_Decl : Node_Id;
2428 Gen_Unit : Entity_Id;
2429 Renaming : Node_Id;
2430
2431 Vis_Prims_List : Elist_Id := No_Elist;
2432 -- List of primitives made temporarily visible in the instantiation
2433 -- to match the visibility of the formal type.
2434
2435 function Build_Local_Package return Node_Id;
2436 -- The formal package is rewritten so that its parameters are replaced
2437 -- with corresponding declarations. For parameters with bona fide
2438 -- associations these declarations are created by Analyze_Associations
2439 -- as for a regular instantiation. For boxed parameters, we preserve
2440 -- the formal declarations and analyze them, in order to introduce
2441 -- entities of the right kind in the environment of the formal.
2442
2443 -------------------------
2444 -- Build_Local_Package --
2445 -------------------------
2446
2447 function Build_Local_Package return Node_Id is
2448 Decls : List_Id;
2449 Pack_Decl : Node_Id;
2450
2451 begin
2452 -- Within the formal, the name of the generic package is a renaming
2453 -- of the formal (as for a regular instantiation).
2454
2455 Pack_Decl :=
2456 Make_Package_Declaration (Loc,
2457 Specification =>
2458 Copy_Generic_Node
2459 (Specification (Original_Node (Gen_Decl)),
2460 Empty, Instantiating => True));
2461
2462 Renaming :=
2463 Make_Package_Renaming_Declaration (Loc,
2464 Defining_Unit_Name =>
2465 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
2466 Name => New_Occurrence_Of (Formal, Loc));
2467
2468 if Nkind (Gen_Id) = N_Identifier
2469 and then Chars (Gen_Id) = Chars (Pack_Id)
2470 then
2471 Error_Msg_NE
2472 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
2473 end if;
2474
2475 -- If the formal is declared with a box, or with an others choice,
2476 -- create corresponding declarations for all entities in the formal
2477 -- part, so that names with the proper types are available in the
2478 -- specification of the formal package.
2479
2480 -- On the other hand, if there are no associations, then all the
2481 -- formals must have defaults, and this will be checked by the
2482 -- call to Analyze_Associations.
2483
2484 if Box_Present (N)
2485 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2486 then
2487 declare
2488 Formal_Decl : Node_Id;
2489
2490 begin
2491 -- TBA : for a formal package, need to recurse ???
2492
2493 Decls := New_List;
2494 Formal_Decl :=
2495 First
2496 (Generic_Formal_Declarations (Original_Node (Gen_Decl)));
2497 while Present (Formal_Decl) loop
2498 Append_To
2499 (Decls, Copy_Generic_Node (Formal_Decl, Empty, True));
2500 Next (Formal_Decl);
2501 end loop;
2502 end;
2503
2504 -- If generic associations are present, use Analyze_Associations to
2505 -- create the proper renaming declarations.
2506
2507 else
2508 declare
2509 Act_Tree : constant Node_Id :=
2510 Copy_Generic_Node
2511 (Original_Node (Gen_Decl), Empty,
2512 Instantiating => True);
2513
2514 begin
2515 Generic_Renamings.Set_Last (0);
2516 Generic_Renamings_HTable.Reset;
2517 Instantiation_Node := N;
2518
2519 Decls :=
2520 Analyze_Associations
2521 (I_Node => Original_Node (N),
2522 Formals => Generic_Formal_Declarations (Act_Tree),
2523 F_Copy => Generic_Formal_Declarations (Gen_Decl));
2524
2525 Vis_Prims_List := Check_Hidden_Primitives (Decls);
2526 end;
2527 end if;
2528
2529 Append (Renaming, To => Decls);
2530
2531 -- Add generated declarations ahead of local declarations in
2532 -- the package.
2533
2534 if No (Visible_Declarations (Specification (Pack_Decl))) then
2535 Set_Visible_Declarations (Specification (Pack_Decl), Decls);
2536 else
2537 Insert_List_Before
2538 (First (Visible_Declarations (Specification (Pack_Decl))),
2539 Decls);
2540 end if;
2541
2542 return Pack_Decl;
2543 end Build_Local_Package;
2544
2545 -- Local variables
2546
2547 Save_IPSM : constant Boolean := Ignore_Pragma_SPARK_Mode;
2548 -- Save flag Ignore_Pragma_SPARK_Mode for restore on exit
2549
2550 Associations : Boolean := True;
2551 New_N : Node_Id;
2552 Parent_Installed : Boolean := False;
2553 Parent_Instance : Entity_Id;
2554 Renaming_In_Par : Entity_Id;
2555
2556 -- Start of processing for Analyze_Formal_Package_Declaration
2557
2558 begin
2559 Check_Text_IO_Special_Unit (Gen_Id);
2560
2561 Init_Env;
2562 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
2563 Gen_Unit := Entity (Gen_Id);
2564
2565 -- Check for a formal package that is a package renaming
2566
2567 if Present (Renamed_Object (Gen_Unit)) then
2568
2569 -- Indicate that unit is used, before replacing it with renamed
2570 -- entity for use below.
2571
2572 if In_Extended_Main_Source_Unit (N) then
2573 Set_Is_Instantiated (Gen_Unit);
2574 Generate_Reference (Gen_Unit, N);
2575 end if;
2576
2577 Gen_Unit := Renamed_Object (Gen_Unit);
2578 end if;
2579
2580 if Ekind (Gen_Unit) /= E_Generic_Package then
2581 Error_Msg_N ("expect generic package name", Gen_Id);
2582 Restore_Env;
2583 goto Leave;
2584
2585 elsif Gen_Unit = Current_Scope then
2586 Error_Msg_N
2587 ("generic package cannot be used as a formal package of itself",
2588 Gen_Id);
2589 Restore_Env;
2590 goto Leave;
2591
2592 elsif In_Open_Scopes (Gen_Unit) then
2593 if Is_Compilation_Unit (Gen_Unit)
2594 and then Is_Child_Unit (Current_Scope)
2595 then
2596 -- Special-case the error when the formal is a parent, and
2597 -- continue analysis to minimize cascaded errors.
2598
2599 Error_Msg_N
2600 ("generic parent cannot be used as formal package "
2601 & "of a child unit", Gen_Id);
2602
2603 else
2604 Error_Msg_N
2605 ("generic package cannot be used as a formal package "
2606 & "within itself", Gen_Id);
2607 Restore_Env;
2608 goto Leave;
2609 end if;
2610 end if;
2611
2612 -- Check that name of formal package does not hide name of generic,
2613 -- or its leading prefix. This check must be done separately because
2614 -- the name of the generic has already been analyzed.
2615
2616 declare
2617 Gen_Name : Entity_Id;
2618
2619 begin
2620 Gen_Name := Gen_Id;
2621 while Nkind (Gen_Name) = N_Expanded_Name loop
2622 Gen_Name := Prefix (Gen_Name);
2623 end loop;
2624
2625 if Chars (Gen_Name) = Chars (Pack_Id) then
2626 Error_Msg_NE
2627 ("& is hidden within declaration of formal package",
2628 Gen_Id, Gen_Name);
2629 end if;
2630 end;
2631
2632 if Box_Present (N)
2633 or else No (Generic_Associations (N))
2634 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2635 then
2636 Associations := False;
2637 end if;
2638
2639 -- If there are no generic associations, the generic parameters appear
2640 -- as local entities and are instantiated like them. We copy the generic
2641 -- package declaration as if it were an instantiation, and analyze it
2642 -- like a regular package, except that we treat the formals as
2643 -- additional visible components.
2644
2645 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
2646
2647 if In_Extended_Main_Source_Unit (N) then
2648 Set_Is_Instantiated (Gen_Unit);
2649 Generate_Reference (Gen_Unit, N);
2650 end if;
2651
2652 Formal := New_Copy (Pack_Id);
2653 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
2654
2655 -- Make local generic without formals. The formals will be replaced with
2656 -- internal declarations.
2657
2658 begin
2659 New_N := Build_Local_Package;
2660
2661 -- If there are errors in the parameter list, Analyze_Associations
2662 -- raises Instantiation_Error. Patch the declaration to prevent further
2663 -- exception propagation.
2664
2665 exception
2666 when Instantiation_Error =>
2667 Enter_Name (Formal);
2668 Set_Ekind (Formal, E_Variable);
2669 Set_Etype (Formal, Any_Type);
2670 Restore_Hidden_Primitives (Vis_Prims_List);
2671
2672 if Parent_Installed then
2673 Remove_Parent;
2674 end if;
2675
2676 goto Leave;
2677 end;
2678
2679 Rewrite (N, New_N);
2680 Set_Defining_Unit_Name (Specification (New_N), Formal);
2681 Set_Generic_Parent (Specification (N), Gen_Unit);
2682 Set_Instance_Env (Gen_Unit, Formal);
2683 Set_Is_Generic_Instance (Formal);
2684
2685 Enter_Name (Formal);
2686 Set_Ekind (Formal, E_Package);
2687 Set_Etype (Formal, Standard_Void_Type);
2688 Set_Inner_Instances (Formal, New_Elmt_List);
2689 Push_Scope (Formal);
2690
2691 -- Manually set the SPARK_Mode from the context because the package
2692 -- declaration is never analyzed.
2693
2694 Set_SPARK_Pragma (Formal, SPARK_Mode_Pragma);
2695 Set_SPARK_Aux_Pragma (Formal, SPARK_Mode_Pragma);
2696 Set_SPARK_Pragma_Inherited (Formal);
2697 Set_SPARK_Aux_Pragma_Inherited (Formal);
2698
2699 if Is_Child_Unit (Gen_Unit) and then Parent_Installed then
2700
2701 -- Similarly, we have to make the name of the formal visible in the
2702 -- parent instance, to resolve properly fully qualified names that
2703 -- may appear in the generic unit. The parent instance has been
2704 -- placed on the scope stack ahead of the current scope.
2705
2706 Parent_Instance := Scope_Stack.Table (Scope_Stack.Last - 1).Entity;
2707
2708 Renaming_In_Par :=
2709 Make_Defining_Identifier (Loc, Chars (Gen_Unit));
2710 Set_Ekind (Renaming_In_Par, E_Package);
2711 Set_Etype (Renaming_In_Par, Standard_Void_Type);
2712 Set_Scope (Renaming_In_Par, Parent_Instance);
2713 Set_Parent (Renaming_In_Par, Parent (Formal));
2714 Set_Renamed_Object (Renaming_In_Par, Formal);
2715 Append_Entity (Renaming_In_Par, Parent_Instance);
2716 end if;
2717
2718 -- A formal package declaration behaves as a package instantiation with
2719 -- respect to SPARK_Mode "off". If the annotation is "off" or altogether
2720 -- missing, set the global flag which signals Analyze_Pragma to ingnore
2721 -- all SPARK_Mode pragmas within the generic_package_name.
2722
2723 if SPARK_Mode /= On then
2724 Ignore_Pragma_SPARK_Mode := True;
2725 end if;
2726
2727 Analyze (Specification (N));
2728
2729 -- The formals for which associations are provided are not visible
2730 -- outside of the formal package. The others are still declared by a
2731 -- formal parameter declaration.
2732
2733 -- If there are no associations, the only local entity to hide is the
2734 -- generated package renaming itself.
2735
2736 declare
2737 E : Entity_Id;
2738
2739 begin
2740 E := First_Entity (Formal);
2741 while Present (E) loop
2742 if Associations and then not Is_Generic_Formal (E) then
2743 Set_Is_Hidden (E);
2744 end if;
2745
2746 if Ekind (E) = E_Package and then Renamed_Entity (E) = Formal then
2747 Set_Is_Hidden (E);
2748 exit;
2749 end if;
2750
2751 Next_Entity (E);
2752 end loop;
2753 end;
2754
2755 End_Package_Scope (Formal);
2756 Restore_Hidden_Primitives (Vis_Prims_List);
2757
2758 if Parent_Installed then
2759 Remove_Parent;
2760 end if;
2761
2762 Restore_Env;
2763
2764 -- Inside the generic unit, the formal package is a regular package, but
2765 -- no body is needed for it. Note that after instantiation, the defining
2766 -- unit name we need is in the new tree and not in the original (see
2767 -- Package_Instantiation). A generic formal package is an instance, and
2768 -- can be used as an actual for an inner instance.
2769
2770 Set_Has_Completion (Formal, True);
2771
2772 -- Add semantic information to the original defining identifier for ASIS
2773 -- use.
2774
2775 Set_Ekind (Pack_Id, E_Package);
2776 Set_Etype (Pack_Id, Standard_Void_Type);
2777 Set_Scope (Pack_Id, Scope (Formal));
2778 Set_Has_Completion (Pack_Id, True);
2779
2780 <<Leave>>
2781 if Has_Aspects (N) then
2782 Analyze_Aspect_Specifications (N, Pack_Id);
2783 end if;
2784
2785 Ignore_Pragma_SPARK_Mode := Save_IPSM;
2786 end Analyze_Formal_Package_Declaration;
2787
2788 ---------------------------------
2789 -- Analyze_Formal_Private_Type --
2790 ---------------------------------
2791
2792 procedure Analyze_Formal_Private_Type
2793 (N : Node_Id;
2794 T : Entity_Id;
2795 Def : Node_Id)
2796 is
2797 begin
2798 New_Private_Type (N, T, Def);
2799
2800 -- Set the size to an arbitrary but legal value
2801
2802 Set_Size_Info (T, Standard_Integer);
2803 Set_RM_Size (T, RM_Size (Standard_Integer));
2804 end Analyze_Formal_Private_Type;
2805
2806 ------------------------------------
2807 -- Analyze_Formal_Incomplete_Type --
2808 ------------------------------------
2809
2810 procedure Analyze_Formal_Incomplete_Type
2811 (T : Entity_Id;
2812 Def : Node_Id)
2813 is
2814 begin
2815 Enter_Name (T);
2816 Set_Ekind (T, E_Incomplete_Type);
2817 Set_Etype (T, T);
2818 Set_Private_Dependents (T, New_Elmt_List);
2819
2820 if Tagged_Present (Def) then
2821 Set_Is_Tagged_Type (T);
2822 Make_Class_Wide_Type (T);
2823 Set_Direct_Primitive_Operations (T, New_Elmt_List);
2824 end if;
2825 end Analyze_Formal_Incomplete_Type;
2826
2827 ----------------------------------------
2828 -- Analyze_Formal_Signed_Integer_Type --
2829 ----------------------------------------
2830
2831 procedure Analyze_Formal_Signed_Integer_Type
2832 (T : Entity_Id;
2833 Def : Node_Id)
2834 is
2835 Base : constant Entity_Id :=
2836 New_Internal_Entity
2837 (E_Signed_Integer_Type,
2838 Current_Scope,
2839 Sloc (Defining_Identifier (Parent (Def))), 'G');
2840
2841 begin
2842 Enter_Name (T);
2843
2844 Set_Ekind (T, E_Signed_Integer_Subtype);
2845 Set_Etype (T, Base);
2846 Set_Size_Info (T, Standard_Integer);
2847 Set_RM_Size (T, RM_Size (Standard_Integer));
2848 Set_Scalar_Range (T, Scalar_Range (Standard_Integer));
2849 Set_Is_Constrained (T);
2850
2851 Set_Is_Generic_Type (Base);
2852 Set_Size_Info (Base, Standard_Integer);
2853 Set_RM_Size (Base, RM_Size (Standard_Integer));
2854 Set_Etype (Base, Base);
2855 Set_Scalar_Range (Base, Scalar_Range (Standard_Integer));
2856 Set_Parent (Base, Parent (Def));
2857 end Analyze_Formal_Signed_Integer_Type;
2858
2859 -------------------------------------------
2860 -- Analyze_Formal_Subprogram_Declaration --
2861 -------------------------------------------
2862
2863 procedure Analyze_Formal_Subprogram_Declaration (N : Node_Id) is
2864 Spec : constant Node_Id := Specification (N);
2865 Def : constant Node_Id := Default_Name (N);
2866 Nam : constant Entity_Id := Defining_Unit_Name (Spec);
2867 Subp : Entity_Id;
2868
2869 begin
2870 if Nam = Error then
2871 return;
2872 end if;
2873
2874 if Nkind (Nam) = N_Defining_Program_Unit_Name then
2875 Error_Msg_N ("name of formal subprogram must be a direct name", Nam);
2876 goto Leave;
2877 end if;
2878
2879 Analyze_Subprogram_Declaration (N);
2880 Set_Is_Formal_Subprogram (Nam);
2881 Set_Has_Completion (Nam);
2882
2883 if Nkind (N) = N_Formal_Abstract_Subprogram_Declaration then
2884 Set_Is_Abstract_Subprogram (Nam);
2885
2886 Set_Is_Dispatching_Operation (Nam);
2887
2888 -- A formal abstract procedure cannot have a null default
2889 -- (RM 12.6(4.1/2)).
2890
2891 if Nkind (Spec) = N_Procedure_Specification
2892 and then Null_Present (Spec)
2893 then
2894 Error_Msg_N
2895 ("a formal abstract subprogram cannot default to null", Spec);
2896 end if;
2897
2898 declare
2899 Ctrl_Type : constant Entity_Id := Find_Dispatching_Type (Nam);
2900 begin
2901 if No (Ctrl_Type) then
2902 Error_Msg_N
2903 ("abstract formal subprogram must have a controlling type",
2904 N);
2905
2906 elsif Ada_Version >= Ada_2012
2907 and then Is_Incomplete_Type (Ctrl_Type)
2908 then
2909 Error_Msg_NE
2910 ("controlling type of abstract formal subprogram cannot "
2911 & "be incomplete type", N, Ctrl_Type);
2912
2913 else
2914 Check_Controlling_Formals (Ctrl_Type, Nam);
2915 end if;
2916 end;
2917 end if;
2918
2919 -- Default name is resolved at the point of instantiation
2920
2921 if Box_Present (N) then
2922 null;
2923
2924 -- Else default is bound at the point of generic declaration
2925
2926 elsif Present (Def) then
2927 if Nkind (Def) = N_Operator_Symbol then
2928 Find_Direct_Name (Def);
2929
2930 elsif Nkind (Def) /= N_Attribute_Reference then
2931 Analyze (Def);
2932
2933 else
2934 -- For an attribute reference, analyze the prefix and verify
2935 -- that it has the proper profile for the subprogram.
2936
2937 Analyze (Prefix (Def));
2938 Valid_Default_Attribute (Nam, Def);
2939 goto Leave;
2940 end if;
2941
2942 -- Default name may be overloaded, in which case the interpretation
2943 -- with the correct profile must be selected, as for a renaming.
2944 -- If the definition is an indexed component, it must denote a
2945 -- member of an entry family. If it is a selected component, it
2946 -- can be a protected operation.
2947
2948 if Etype (Def) = Any_Type then
2949 goto Leave;
2950
2951 elsif Nkind (Def) = N_Selected_Component then
2952 if not Is_Overloadable (Entity (Selector_Name (Def))) then
2953 Error_Msg_N ("expect valid subprogram name as default", Def);
2954 end if;
2955
2956 elsif Nkind (Def) = N_Indexed_Component then
2957 if Is_Entity_Name (Prefix (Def)) then
2958 if Ekind (Entity (Prefix (Def))) /= E_Entry_Family then
2959 Error_Msg_N ("expect valid subprogram name as default", Def);
2960 end if;
2961
2962 elsif Nkind (Prefix (Def)) = N_Selected_Component then
2963 if Ekind (Entity (Selector_Name (Prefix (Def)))) /=
2964 E_Entry_Family
2965 then
2966 Error_Msg_N ("expect valid subprogram name as default", Def);
2967 end if;
2968
2969 else
2970 Error_Msg_N ("expect valid subprogram name as default", Def);
2971 goto Leave;
2972 end if;
2973
2974 elsif Nkind (Def) = N_Character_Literal then
2975
2976 -- Needs some type checks: subprogram should be parameterless???
2977
2978 Resolve (Def, (Etype (Nam)));
2979
2980 elsif not Is_Entity_Name (Def)
2981 or else not Is_Overloadable (Entity (Def))
2982 then
2983 Error_Msg_N ("expect valid subprogram name as default", Def);
2984 goto Leave;
2985
2986 elsif not Is_Overloaded (Def) then
2987 Subp := Entity (Def);
2988
2989 if Subp = Nam then
2990 Error_Msg_N ("premature usage of formal subprogram", Def);
2991
2992 elsif not Entity_Matches_Spec (Subp, Nam) then
2993 Error_Msg_N ("no visible entity matches specification", Def);
2994 end if;
2995
2996 -- More than one interpretation, so disambiguate as for a renaming
2997
2998 else
2999 declare
3000 I : Interp_Index;
3001 I1 : Interp_Index := 0;
3002 It : Interp;
3003 It1 : Interp;
3004
3005 begin
3006 Subp := Any_Id;
3007 Get_First_Interp (Def, I, It);
3008 while Present (It.Nam) loop
3009 if Entity_Matches_Spec (It.Nam, Nam) then
3010 if Subp /= Any_Id then
3011 It1 := Disambiguate (Def, I1, I, Etype (Subp));
3012
3013 if It1 = No_Interp then
3014 Error_Msg_N ("ambiguous default subprogram", Def);
3015 else
3016 Subp := It1.Nam;
3017 end if;
3018
3019 exit;
3020
3021 else
3022 I1 := I;
3023 Subp := It.Nam;
3024 end if;
3025 end if;
3026
3027 Get_Next_Interp (I, It);
3028 end loop;
3029 end;
3030
3031 if Subp /= Any_Id then
3032
3033 -- Subprogram found, generate reference to it
3034
3035 Set_Entity (Def, Subp);
3036 Generate_Reference (Subp, Def);
3037
3038 if Subp = Nam then
3039 Error_Msg_N ("premature usage of formal subprogram", Def);
3040
3041 elsif Ekind (Subp) /= E_Operator then
3042 Check_Mode_Conformant (Subp, Nam);
3043 end if;
3044
3045 else
3046 Error_Msg_N ("no visible subprogram matches specification", N);
3047 end if;
3048 end if;
3049 end if;
3050
3051 <<Leave>>
3052 if Has_Aspects (N) then
3053 Analyze_Aspect_Specifications (N, Nam);
3054 end if;
3055
3056 end Analyze_Formal_Subprogram_Declaration;
3057
3058 -------------------------------------
3059 -- Analyze_Formal_Type_Declaration --
3060 -------------------------------------
3061
3062 procedure Analyze_Formal_Type_Declaration (N : Node_Id) is
3063 Def : constant Node_Id := Formal_Type_Definition (N);
3064 T : Entity_Id;
3065
3066 begin
3067 T := Defining_Identifier (N);
3068
3069 if Present (Discriminant_Specifications (N))
3070 and then Nkind (Def) /= N_Formal_Private_Type_Definition
3071 then
3072 Error_Msg_N
3073 ("discriminants not allowed for this formal type", T);
3074 end if;
3075
3076 -- Enter the new name, and branch to specific routine
3077
3078 case Nkind (Def) is
3079 when N_Formal_Private_Type_Definition =>
3080 Analyze_Formal_Private_Type (N, T, Def);
3081
3082 when N_Formal_Derived_Type_Definition =>
3083 Analyze_Formal_Derived_Type (N, T, Def);
3084
3085 when N_Formal_Incomplete_Type_Definition =>
3086 Analyze_Formal_Incomplete_Type (T, Def);
3087
3088 when N_Formal_Discrete_Type_Definition =>
3089 Analyze_Formal_Discrete_Type (T, Def);
3090
3091 when N_Formal_Signed_Integer_Type_Definition =>
3092 Analyze_Formal_Signed_Integer_Type (T, Def);
3093
3094 when N_Formal_Modular_Type_Definition =>
3095 Analyze_Formal_Modular_Type (T, Def);
3096
3097 when N_Formal_Floating_Point_Definition =>
3098 Analyze_Formal_Floating_Type (T, Def);
3099
3100 when N_Formal_Ordinary_Fixed_Point_Definition =>
3101 Analyze_Formal_Ordinary_Fixed_Point_Type (T, Def);
3102
3103 when N_Formal_Decimal_Fixed_Point_Definition =>
3104 Analyze_Formal_Decimal_Fixed_Point_Type (T, Def);
3105
3106 when N_Array_Type_Definition =>
3107 Analyze_Formal_Array_Type (T, Def);
3108
3109 when N_Access_To_Object_Definition |
3110 N_Access_Function_Definition |
3111 N_Access_Procedure_Definition =>
3112 Analyze_Generic_Access_Type (T, Def);
3113
3114 -- Ada 2005: a interface declaration is encoded as an abstract
3115 -- record declaration or a abstract type derivation.
3116
3117 when N_Record_Definition =>
3118 Analyze_Formal_Interface_Type (N, T, Def);
3119
3120 when N_Derived_Type_Definition =>
3121 Analyze_Formal_Derived_Interface_Type (N, T, Def);
3122
3123 when N_Error =>
3124 null;
3125
3126 when others =>
3127 raise Program_Error;
3128
3129 end case;
3130
3131 Set_Is_Generic_Type (T);
3132
3133 if Has_Aspects (N) then
3134 Analyze_Aspect_Specifications (N, T);
3135 end if;
3136 end Analyze_Formal_Type_Declaration;
3137
3138 ------------------------------------
3139 -- Analyze_Function_Instantiation --
3140 ------------------------------------
3141
3142 procedure Analyze_Function_Instantiation (N : Node_Id) is
3143 begin
3144 Analyze_Subprogram_Instantiation (N, E_Function);
3145 end Analyze_Function_Instantiation;
3146
3147 ---------------------------------
3148 -- Analyze_Generic_Access_Type --
3149 ---------------------------------
3150
3151 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id) is
3152 begin
3153 Enter_Name (T);
3154
3155 if Nkind (Def) = N_Access_To_Object_Definition then
3156 Access_Type_Declaration (T, Def);
3157
3158 if Is_Incomplete_Or_Private_Type (Designated_Type (T))
3159 and then No (Full_View (Designated_Type (T)))
3160 and then not Is_Generic_Type (Designated_Type (T))
3161 then
3162 Error_Msg_N ("premature usage of incomplete type", Def);
3163
3164 elsif not Is_Entity_Name (Subtype_Indication (Def)) then
3165 Error_Msg_N
3166 ("only a subtype mark is allowed in a formal", Def);
3167 end if;
3168
3169 else
3170 Access_Subprogram_Declaration (T, Def);
3171 end if;
3172 end Analyze_Generic_Access_Type;
3173
3174 ---------------------------------
3175 -- Analyze_Generic_Formal_Part --
3176 ---------------------------------
3177
3178 procedure Analyze_Generic_Formal_Part (N : Node_Id) is
3179 Gen_Parm_Decl : Node_Id;
3180
3181 begin
3182 -- The generic formals are processed in the scope of the generic unit,
3183 -- where they are immediately visible. The scope is installed by the
3184 -- caller.
3185
3186 Gen_Parm_Decl := First (Generic_Formal_Declarations (N));
3187 while Present (Gen_Parm_Decl) loop
3188 Analyze (Gen_Parm_Decl);
3189 Next (Gen_Parm_Decl);
3190 end loop;
3191
3192 Generate_Reference_To_Generic_Formals (Current_Scope);
3193 end Analyze_Generic_Formal_Part;
3194
3195 ------------------------------------------
3196 -- Analyze_Generic_Package_Declaration --
3197 ------------------------------------------
3198
3199 procedure Analyze_Generic_Package_Declaration (N : Node_Id) is
3200 Loc : constant Source_Ptr := Sloc (N);
3201 Decls : constant List_Id :=
3202 Visible_Declarations (Specification (N));
3203 Decl : Node_Id;
3204 Id : Entity_Id;
3205 New_N : Node_Id;
3206 Renaming : Node_Id;
3207 Save_Parent : Node_Id;
3208
3209 begin
3210 Check_SPARK_05_Restriction ("generic is not allowed", N);
3211
3212 -- We introduce a renaming of the enclosing package, to have a usable
3213 -- entity as the prefix of an expanded name for a local entity of the
3214 -- form Par.P.Q, where P is the generic package. This is because a local
3215 -- entity named P may hide it, so that the usual visibility rules in
3216 -- the instance will not resolve properly.
3217
3218 Renaming :=
3219 Make_Package_Renaming_Declaration (Loc,
3220 Defining_Unit_Name =>
3221 Make_Defining_Identifier (Loc,
3222 Chars => New_External_Name (Chars (Defining_Entity (N)), "GH")),
3223 Name =>
3224 Make_Identifier (Loc, Chars (Defining_Entity (N))));
3225
3226 if Present (Decls) then
3227 Decl := First (Decls);
3228 while Present (Decl) and then Nkind (Decl) = N_Pragma loop
3229 Next (Decl);
3230 end loop;
3231
3232 if Present (Decl) then
3233 Insert_Before (Decl, Renaming);
3234 else
3235 Append (Renaming, Visible_Declarations (Specification (N)));
3236 end if;
3237
3238 else
3239 Set_Visible_Declarations (Specification (N), New_List (Renaming));
3240 end if;
3241
3242 -- Create copy of generic unit, and save for instantiation. If the unit
3243 -- is a child unit, do not copy the specifications for the parent, which
3244 -- are not part of the generic tree.
3245
3246 Save_Parent := Parent_Spec (N);
3247 Set_Parent_Spec (N, Empty);
3248
3249 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
3250 Set_Parent_Spec (New_N, Save_Parent);
3251 Rewrite (N, New_N);
3252
3253 -- Once the contents of the generic copy and the template are swapped,
3254 -- do the same for their respective aspect specifications.
3255
3256 Exchange_Aspects (N, New_N);
3257
3258 -- Collect all contract-related source pragmas found within the template
3259 -- and attach them to the contract of the package spec. This contract is
3260 -- used in the capture of global references within annotations.
3261
3262 Create_Generic_Contract (N);
3263
3264 Id := Defining_Entity (N);
3265 Generate_Definition (Id);
3266
3267 -- Expansion is not applied to generic units
3268
3269 Start_Generic;
3270
3271 Enter_Name (Id);
3272 Set_Ekind (Id, E_Generic_Package);
3273 Set_Etype (Id, Standard_Void_Type);
3274
3275 -- A generic package declared within a Ghost region is rendered Ghost
3276 -- (SPARK RM 6.9(2)).
3277
3278 if Ghost_Mode > None then
3279 Set_Is_Ghost_Entity (Id);
3280 end if;
3281
3282 -- Analyze aspects now, so that generated pragmas appear in the
3283 -- declarations before building and analyzing the generic copy.
3284
3285 if Has_Aspects (N) then
3286 Analyze_Aspect_Specifications (N, Id);
3287 end if;
3288
3289 Push_Scope (Id);
3290 Enter_Generic_Scope (Id);
3291 Set_Inner_Instances (Id, New_Elmt_List);
3292
3293 Set_Categorization_From_Pragmas (N);
3294 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3295
3296 -- Link the declaration of the generic homonym in the generic copy to
3297 -- the package it renames, so that it is always resolved properly.
3298
3299 Set_Generic_Homonym (Id, Defining_Unit_Name (Renaming));
3300 Set_Entity (Associated_Node (Name (Renaming)), Id);
3301
3302 -- For a library unit, we have reconstructed the entity for the unit,
3303 -- and must reset it in the library tables.
3304
3305 if Nkind (Parent (N)) = N_Compilation_Unit then
3306 Set_Cunit_Entity (Current_Sem_Unit, Id);
3307 end if;
3308
3309 Analyze_Generic_Formal_Part (N);
3310
3311 -- After processing the generic formals, analysis proceeds as for a
3312 -- non-generic package.
3313
3314 Analyze (Specification (N));
3315
3316 Validate_Categorization_Dependency (N, Id);
3317
3318 End_Generic;
3319
3320 End_Package_Scope (Id);
3321 Exit_Generic_Scope (Id);
3322
3323 if Nkind (Parent (N)) /= N_Compilation_Unit then
3324 Move_Freeze_Nodes (Id, N, Visible_Declarations (Specification (N)));
3325 Move_Freeze_Nodes (Id, N, Private_Declarations (Specification (N)));
3326 Move_Freeze_Nodes (Id, N, Generic_Formal_Declarations (N));
3327
3328 else
3329 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
3330 Validate_RT_RAT_Component (N);
3331
3332 -- If this is a spec without a body, check that generic parameters
3333 -- are referenced.
3334
3335 if not Body_Required (Parent (N)) then
3336 Check_References (Id);
3337 end if;
3338 end if;
3339
3340 -- If there is a specified storage pool in the context, create an
3341 -- aspect on the package declaration, so that it is used in any
3342 -- instance that does not override it.
3343
3344 if Present (Default_Pool) then
3345 declare
3346 ASN : Node_Id;
3347
3348 begin
3349 ASN :=
3350 Make_Aspect_Specification (Loc,
3351 Identifier => Make_Identifier (Loc, Name_Default_Storage_Pool),
3352 Expression => New_Copy (Default_Pool));
3353
3354 if No (Aspect_Specifications (Specification (N))) then
3355 Set_Aspect_Specifications (Specification (N), New_List (ASN));
3356 else
3357 Append (ASN, Aspect_Specifications (Specification (N)));
3358 end if;
3359 end;
3360 end if;
3361 end Analyze_Generic_Package_Declaration;
3362
3363 --------------------------------------------
3364 -- Analyze_Generic_Subprogram_Declaration --
3365 --------------------------------------------
3366
3367 procedure Analyze_Generic_Subprogram_Declaration (N : Node_Id) is
3368 Formals : List_Id;
3369 Id : Entity_Id;
3370 New_N : Node_Id;
3371 Result_Type : Entity_Id;
3372 Save_Parent : Node_Id;
3373 Spec : Node_Id;
3374 Typ : Entity_Id;
3375
3376 begin
3377 Check_SPARK_05_Restriction ("generic is not allowed", N);
3378
3379 -- Create copy of generic unit, and save for instantiation. If the unit
3380 -- is a child unit, do not copy the specifications for the parent, which
3381 -- are not part of the generic tree.
3382
3383 Save_Parent := Parent_Spec (N);
3384 Set_Parent_Spec (N, Empty);
3385
3386 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
3387 Set_Parent_Spec (New_N, Save_Parent);
3388 Rewrite (N, New_N);
3389
3390 -- Once the contents of the generic copy and the template are swapped,
3391 -- do the same for their respective aspect specifications.
3392
3393 Exchange_Aspects (N, New_N);
3394
3395 -- Collect all contract-related source pragmas found within the template
3396 -- and attach them to the contract of the subprogram spec. This contract
3397 -- is used in the capture of global references within annotations.
3398
3399 Create_Generic_Contract (N);
3400
3401 Spec := Specification (N);
3402 Id := Defining_Entity (Spec);
3403 Generate_Definition (Id);
3404
3405 if Nkind (Id) = N_Defining_Operator_Symbol then
3406 Error_Msg_N
3407 ("operator symbol not allowed for generic subprogram", Id);
3408 end if;
3409
3410 Start_Generic;
3411
3412 Enter_Name (Id);
3413 Set_Scope_Depth_Value (Id, Scope_Depth (Current_Scope) + 1);
3414
3415 -- Analyze the aspects of the generic copy to ensure that all generated
3416 -- pragmas (if any) perform their semantic effects.
3417
3418 if Has_Aspects (N) then
3419 Analyze_Aspect_Specifications (N, Id);
3420 end if;
3421
3422 Push_Scope (Id);
3423 Enter_Generic_Scope (Id);
3424 Set_Inner_Instances (Id, New_Elmt_List);
3425 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3426
3427 Analyze_Generic_Formal_Part (N);
3428
3429 Formals := Parameter_Specifications (Spec);
3430
3431 if Nkind (Spec) = N_Function_Specification then
3432 Set_Ekind (Id, E_Generic_Function);
3433 else
3434 Set_Ekind (Id, E_Generic_Procedure);
3435 end if;
3436
3437 if Present (Formals) then
3438 Process_Formals (Formals, Spec);
3439 end if;
3440
3441 if Nkind (Spec) = N_Function_Specification then
3442 if Nkind (Result_Definition (Spec)) = N_Access_Definition then
3443 Result_Type := Access_Definition (Spec, Result_Definition (Spec));
3444 Set_Etype (Id, Result_Type);
3445
3446 -- Check restriction imposed by AI05-073: a generic function
3447 -- cannot return an abstract type or an access to such.
3448
3449 -- This is a binding interpretation should it apply to earlier
3450 -- versions of Ada as well as Ada 2012???
3451
3452 if Is_Abstract_Type (Designated_Type (Result_Type))
3453 and then Ada_Version >= Ada_2012
3454 then
3455 Error_Msg_N
3456 ("generic function cannot have an access result "
3457 & "that designates an abstract type", Spec);
3458 end if;
3459
3460 else
3461 Find_Type (Result_Definition (Spec));
3462 Typ := Entity (Result_Definition (Spec));
3463
3464 if Is_Abstract_Type (Typ)
3465 and then Ada_Version >= Ada_2012
3466 then
3467 Error_Msg_N
3468 ("generic function cannot have abstract result type", Spec);
3469 end if;
3470
3471 -- If a null exclusion is imposed on the result type, then create
3472 -- a null-excluding itype (an access subtype) and use it as the
3473 -- function's Etype.
3474
3475 if Is_Access_Type (Typ)
3476 and then Null_Exclusion_Present (Spec)
3477 then
3478 Set_Etype (Id,
3479 Create_Null_Excluding_Itype
3480 (T => Typ,
3481 Related_Nod => Spec,
3482 Scope_Id => Defining_Unit_Name (Spec)));
3483 else
3484 Set_Etype (Id, Typ);
3485 end if;
3486 end if;
3487
3488 else
3489 Set_Etype (Id, Standard_Void_Type);
3490 end if;
3491
3492 -- A generic subprogram declared within a Ghost region is rendered Ghost
3493 -- (SPARK RM 6.9(2)).
3494
3495 if Ghost_Mode > None then
3496 Set_Is_Ghost_Entity (Id);
3497 end if;
3498
3499 -- For a library unit, we have reconstructed the entity for the unit,
3500 -- and must reset it in the library tables. We also make sure that
3501 -- Body_Required is set properly in the original compilation unit node.
3502
3503 if Nkind (Parent (N)) = N_Compilation_Unit then
3504 Set_Cunit_Entity (Current_Sem_Unit, Id);
3505 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
3506 end if;
3507
3508 Set_Categorization_From_Pragmas (N);
3509 Validate_Categorization_Dependency (N, Id);
3510
3511 -- Capture all global references that occur within the profile of the
3512 -- generic subprogram. Aspects are not part of this processing because
3513 -- they must be delayed. If processed now, Save_Global_References will
3514 -- destroy the Associated_Node links and prevent the capture of global
3515 -- references when the contract of the generic subprogram is analyzed.
3516
3517 Save_Global_References (Original_Node (N));
3518
3519 End_Generic;
3520 End_Scope;
3521 Exit_Generic_Scope (Id);
3522 Generate_Reference_To_Formals (Id);
3523
3524 List_Inherited_Pre_Post_Aspects (Id);
3525 end Analyze_Generic_Subprogram_Declaration;
3526
3527 -----------------------------------
3528 -- Analyze_Package_Instantiation --
3529 -----------------------------------
3530
3531 procedure Analyze_Package_Instantiation (N : Node_Id) is
3532 Loc : constant Source_Ptr := Sloc (N);
3533 Gen_Id : constant Node_Id := Name (N);
3534
3535 Act_Decl : Node_Id;
3536 Act_Decl_Name : Node_Id;
3537 Act_Decl_Id : Entity_Id;
3538 Act_Spec : Node_Id;
3539 Act_Tree : Node_Id;
3540
3541 Gen_Decl : Node_Id;
3542 Gen_Spec : Node_Id;
3543 Gen_Unit : Entity_Id;
3544
3545 Is_Actual_Pack : constant Boolean :=
3546 Is_Internal (Defining_Entity (N));
3547
3548 Env_Installed : Boolean := False;
3549 Parent_Installed : Boolean := False;
3550 Renaming_List : List_Id;
3551 Unit_Renaming : Node_Id;
3552 Needs_Body : Boolean;
3553 Inline_Now : Boolean := False;
3554 Has_Inline_Always : Boolean := False;
3555
3556 Save_IPSM : constant Boolean := Ignore_Pragma_SPARK_Mode;
3557 -- Save flag Ignore_Pragma_SPARK_Mode for restore on exit
3558
3559 Save_SM : constant SPARK_Mode_Type := SPARK_Mode;
3560 Save_SMP : constant Node_Id := SPARK_Mode_Pragma;
3561 -- Save the SPARK_Mode-related data for restore on exit
3562
3563 Save_Style_Check : constant Boolean := Style_Check;
3564 -- Save style check mode for restore on exit
3565
3566 procedure Delay_Descriptors (E : Entity_Id);
3567 -- Delay generation of subprogram descriptors for given entity
3568
3569 function Might_Inline_Subp return Boolean;
3570 -- If inlining is active and the generic contains inlined subprograms,
3571 -- we instantiate the body. This may cause superfluous instantiations,
3572 -- but it is simpler than detecting the need for the body at the point
3573 -- of inlining, when the context of the instance is not available.
3574
3575 -----------------------
3576 -- Delay_Descriptors --
3577 -----------------------
3578
3579 procedure Delay_Descriptors (E : Entity_Id) is
3580 begin
3581 if not Delay_Subprogram_Descriptors (E) then
3582 Set_Delay_Subprogram_Descriptors (E);
3583 Pending_Descriptor.Append (E);
3584 end if;
3585 end Delay_Descriptors;
3586
3587 -----------------------
3588 -- Might_Inline_Subp --
3589 -----------------------
3590
3591 function Might_Inline_Subp return Boolean is
3592 E : Entity_Id;
3593
3594 begin
3595 if not Inline_Processing_Required then
3596 return False;
3597
3598 else
3599 E := First_Entity (Gen_Unit);
3600 while Present (E) loop
3601 if Is_Subprogram (E) and then Is_Inlined (E) then
3602 -- Remember if there are any subprograms with Inline_Always
3603
3604 if Has_Pragma_Inline_Always (E) then
3605 Has_Inline_Always := True;
3606 end if;
3607
3608 return True;
3609 end if;
3610
3611 Next_Entity (E);
3612 end loop;
3613 end if;
3614
3615 return False;
3616 end Might_Inline_Subp;
3617
3618 -- Local declarations
3619
3620 Vis_Prims_List : Elist_Id := No_Elist;
3621 -- List of primitives made temporarily visible in the instantiation
3622 -- to match the visibility of the formal type
3623
3624 -- Start of processing for Analyze_Package_Instantiation
3625
3626 begin
3627 Check_SPARK_05_Restriction ("generic is not allowed", N);
3628
3629 -- Very first thing: check for Text_IO special unit in case we are
3630 -- instantiating one of the children of [[Wide_]Wide_]Text_IO.
3631
3632 Check_Text_IO_Special_Unit (Name (N));
3633
3634 -- Make node global for error reporting
3635
3636 Instantiation_Node := N;
3637
3638 -- Turn off style checking in instances. If the check is enabled on the
3639 -- generic unit, a warning in an instance would just be noise. If not
3640 -- enabled on the generic, then a warning in an instance is just wrong.
3641
3642 Style_Check := False;
3643
3644 -- Case of instantiation of a generic package
3645
3646 if Nkind (N) = N_Package_Instantiation then
3647 Act_Decl_Id := New_Copy (Defining_Entity (N));
3648 Set_Comes_From_Source (Act_Decl_Id, True);
3649
3650 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name then
3651 Act_Decl_Name :=
3652 Make_Defining_Program_Unit_Name (Loc,
3653 Name =>
3654 New_Copy_Tree (Name (Defining_Unit_Name (N))),
3655 Defining_Identifier => Act_Decl_Id);
3656 else
3657 Act_Decl_Name := Act_Decl_Id;
3658 end if;
3659
3660 -- Case of instantiation of a formal package
3661
3662 else
3663 Act_Decl_Id := Defining_Identifier (N);
3664 Act_Decl_Name := Act_Decl_Id;
3665 end if;
3666
3667 Generate_Definition (Act_Decl_Id);
3668 Set_Ekind (Act_Decl_Id, E_Package);
3669
3670 -- Initialize list of incomplete actuals before analysis
3671
3672 Set_Incomplete_Actuals (Act_Decl_Id, New_Elmt_List);
3673
3674 Preanalyze_Actuals (N, Act_Decl_Id);
3675
3676 Init_Env;
3677 Env_Installed := True;
3678
3679 -- Reset renaming map for formal types. The mapping is established
3680 -- when analyzing the generic associations, but some mappings are
3681 -- inherited from formal packages of parent units, and these are
3682 -- constructed when the parents are installed.
3683
3684 Generic_Renamings.Set_Last (0);
3685 Generic_Renamings_HTable.Reset;
3686
3687 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
3688 Gen_Unit := Entity (Gen_Id);
3689
3690 -- Verify that it is the name of a generic package
3691
3692 -- A visibility glitch: if the instance is a child unit and the generic
3693 -- is the generic unit of a parent instance (i.e. both the parent and
3694 -- the child units are instances of the same package) the name now
3695 -- denotes the renaming within the parent, not the intended generic
3696 -- unit. See if there is a homonym that is the desired generic. The
3697 -- renaming declaration must be visible inside the instance of the
3698 -- child, but not when analyzing the name in the instantiation itself.
3699
3700 if Ekind (Gen_Unit) = E_Package
3701 and then Present (Renamed_Entity (Gen_Unit))
3702 and then In_Open_Scopes (Renamed_Entity (Gen_Unit))
3703 and then Is_Generic_Instance (Renamed_Entity (Gen_Unit))
3704 and then Present (Homonym (Gen_Unit))
3705 then
3706 Gen_Unit := Homonym (Gen_Unit);
3707 end if;
3708
3709 if Etype (Gen_Unit) = Any_Type then
3710 Restore_Env;
3711 goto Leave;
3712
3713 elsif Ekind (Gen_Unit) /= E_Generic_Package then
3714
3715 -- Ada 2005 (AI-50217): Cannot use instance in limited with_clause
3716
3717 if From_Limited_With (Gen_Unit) then
3718 Error_Msg_N
3719 ("cannot instantiate a limited withed package", Gen_Id);
3720 else
3721 Error_Msg_NE
3722 ("& is not the name of a generic package", Gen_Id, Gen_Unit);
3723 end if;
3724
3725 Restore_Env;
3726 goto Leave;
3727 end if;
3728
3729 if In_Extended_Main_Source_Unit (N) then
3730 Set_Is_Instantiated (Gen_Unit);
3731 Generate_Reference (Gen_Unit, N);
3732
3733 if Present (Renamed_Object (Gen_Unit)) then
3734 Set_Is_Instantiated (Renamed_Object (Gen_Unit));
3735 Generate_Reference (Renamed_Object (Gen_Unit), N);
3736 end if;
3737 end if;
3738
3739 if Nkind (Gen_Id) = N_Identifier
3740 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
3741 then
3742 Error_Msg_NE
3743 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
3744
3745 elsif Nkind (Gen_Id) = N_Expanded_Name
3746 and then Is_Child_Unit (Gen_Unit)
3747 and then Nkind (Prefix (Gen_Id)) = N_Identifier
3748 and then Chars (Act_Decl_Id) = Chars (Prefix (Gen_Id))
3749 then
3750 Error_Msg_N
3751 ("& is hidden within declaration of instance ", Prefix (Gen_Id));
3752 end if;
3753
3754 Set_Entity (Gen_Id, Gen_Unit);
3755
3756 -- If generic is a renaming, get original generic unit
3757
3758 if Present (Renamed_Object (Gen_Unit))
3759 and then Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Package
3760 then
3761 Gen_Unit := Renamed_Object (Gen_Unit);
3762 end if;
3763
3764 -- Verify that there are no circular instantiations
3765
3766 if In_Open_Scopes (Gen_Unit) then
3767 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
3768 Restore_Env;
3769 goto Leave;
3770
3771 elsif Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
3772 Error_Msg_Node_2 := Current_Scope;
3773 Error_Msg_NE
3774 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
3775 Circularity_Detected := True;
3776 Restore_Env;
3777 goto Leave;
3778
3779 else
3780 -- If the context of the instance is subject to SPARK_Mode "off" or
3781 -- the annotation is altogether missing, set the global flag which
3782 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
3783 -- the instance.
3784
3785 if SPARK_Mode /= On then
3786 Ignore_Pragma_SPARK_Mode := True;
3787 end if;
3788
3789 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
3790 Gen_Spec := Specification (Gen_Decl);
3791
3792 -- Initialize renamings map, for error checking, and the list that
3793 -- holds private entities whose views have changed between generic
3794 -- definition and instantiation. If this is the instance created to
3795 -- validate an actual package, the instantiation environment is that
3796 -- of the enclosing instance.
3797
3798 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
3799
3800 -- Copy original generic tree, to produce text for instantiation
3801
3802 Act_Tree :=
3803 Copy_Generic_Node
3804 (Original_Node (Gen_Decl), Empty, Instantiating => True);
3805
3806 Act_Spec := Specification (Act_Tree);
3807
3808 -- If this is the instance created to validate an actual package,
3809 -- only the formals matter, do not examine the package spec itself.
3810
3811 if Is_Actual_Pack then
3812 Set_Visible_Declarations (Act_Spec, New_List);
3813 Set_Private_Declarations (Act_Spec, New_List);
3814 end if;
3815
3816 Renaming_List :=
3817 Analyze_Associations
3818 (I_Node => N,
3819 Formals => Generic_Formal_Declarations (Act_Tree),
3820 F_Copy => Generic_Formal_Declarations (Gen_Decl));
3821
3822 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
3823
3824 Set_Instance_Env (Gen_Unit, Act_Decl_Id);
3825 Set_Defining_Unit_Name (Act_Spec, Act_Decl_Name);
3826 Set_Is_Generic_Instance (Act_Decl_Id);
3827 Set_Generic_Parent (Act_Spec, Gen_Unit);
3828
3829 -- References to the generic in its own declaration or its body are
3830 -- references to the instance. Add a renaming declaration for the
3831 -- generic unit itself. This declaration, as well as the renaming
3832 -- declarations for the generic formals, must remain private to the
3833 -- unit: the formals, because this is the language semantics, and
3834 -- the unit because its use is an artifact of the implementation.
3835
3836 Unit_Renaming :=
3837 Make_Package_Renaming_Declaration (Loc,
3838 Defining_Unit_Name =>
3839 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
3840 Name => New_Occurrence_Of (Act_Decl_Id, Loc));
3841
3842 Append (Unit_Renaming, Renaming_List);
3843
3844 -- The renaming declarations are the first local declarations of the
3845 -- new unit.
3846
3847 if Is_Non_Empty_List (Visible_Declarations (Act_Spec)) then
3848 Insert_List_Before
3849 (First (Visible_Declarations (Act_Spec)), Renaming_List);
3850 else
3851 Set_Visible_Declarations (Act_Spec, Renaming_List);
3852 end if;
3853
3854 Act_Decl := Make_Package_Declaration (Loc, Specification => Act_Spec);
3855
3856 -- Propagate the aspect specifications from the package declaration
3857 -- template to the instantiated version of the package declaration.
3858
3859 if Has_Aspects (Act_Tree) then
3860 Set_Aspect_Specifications (Act_Decl,
3861 New_Copy_List_Tree (Aspect_Specifications (Act_Tree)));
3862 end if;
3863
3864 -- The generic may have a generated Default_Storage_Pool aspect,
3865 -- set at the point of generic declaration. If the instance has
3866 -- that aspect, it overrides the one inherited from the generic.
3867
3868 if Has_Aspects (Gen_Spec) then
3869 if No (Aspect_Specifications (N)) then
3870 Set_Aspect_Specifications (N,
3871 (New_Copy_List_Tree
3872 (Aspect_Specifications (Gen_Spec))));
3873
3874 else
3875 declare
3876 ASN1, ASN2 : Node_Id;
3877
3878 begin
3879 ASN1 := First (Aspect_Specifications (N));
3880 while Present (ASN1) loop
3881 if Chars (Identifier (ASN1)) = Name_Default_Storage_Pool
3882 then
3883 -- If generic carries a default storage pool, remove
3884 -- it in favor of the instance one.
3885
3886 ASN2 := First (Aspect_Specifications (Gen_Spec));
3887 while Present (ASN2) loop
3888 if Chars (Identifier (ASN2)) =
3889 Name_Default_Storage_Pool
3890 then
3891 Remove (ASN2);
3892 exit;
3893 end if;
3894
3895 Next (ASN2);
3896 end loop;
3897 end if;
3898
3899 Next (ASN1);
3900 end loop;
3901
3902 Prepend_List_To (Aspect_Specifications (N),
3903 (New_Copy_List_Tree
3904 (Aspect_Specifications (Gen_Spec))));
3905 end;
3906 end if;
3907 end if;
3908
3909 -- Save the instantiation node, for subsequent instantiation of the
3910 -- body, if there is one and we are generating code for the current
3911 -- unit. Mark unit as having a body (avoids premature error message).
3912
3913 -- We instantiate the body if we are generating code, if we are
3914 -- generating cross-reference information, or if we are building
3915 -- trees for ASIS use or GNATprove use.
3916
3917 declare
3918 Enclosing_Body_Present : Boolean := False;
3919 -- If the generic unit is not a compilation unit, then a body may
3920 -- be present in its parent even if none is required. We create a
3921 -- tentative pending instantiation for the body, which will be
3922 -- discarded if none is actually present.
3923
3924 Scop : Entity_Id;
3925
3926 begin
3927 if Scope (Gen_Unit) /= Standard_Standard
3928 and then not Is_Child_Unit (Gen_Unit)
3929 then
3930 Scop := Scope (Gen_Unit);
3931 while Present (Scop) and then Scop /= Standard_Standard loop
3932 if Unit_Requires_Body (Scop) then
3933 Enclosing_Body_Present := True;
3934 exit;
3935
3936 elsif In_Open_Scopes (Scop)
3937 and then In_Package_Body (Scop)
3938 then
3939 Enclosing_Body_Present := True;
3940 exit;
3941 end if;
3942
3943 exit when Is_Compilation_Unit (Scop);
3944 Scop := Scope (Scop);
3945 end loop;
3946 end if;
3947
3948 -- If front-end inlining is enabled or there are any subprograms
3949 -- marked with Inline_Always, and this is a unit for which code
3950 -- will be generated, we instantiate the body at once.
3951
3952 -- This is done if the instance is not the main unit, and if the
3953 -- generic is not a child unit of another generic, to avoid scope
3954 -- problems and the reinstallation of parent instances.
3955
3956 if Expander_Active
3957 and then (not Is_Child_Unit (Gen_Unit)
3958 or else not Is_Generic_Unit (Scope (Gen_Unit)))
3959 and then Might_Inline_Subp
3960 and then not Is_Actual_Pack
3961 then
3962 if not Back_End_Inlining
3963 and then (Front_End_Inlining or else Has_Inline_Always)
3964 and then (Is_In_Main_Unit (N)
3965 or else In_Main_Context (Current_Scope))
3966 and then Nkind (Parent (N)) /= N_Compilation_Unit
3967 then
3968 Inline_Now := True;
3969
3970 -- In configurable_run_time mode we force the inlining of
3971 -- predefined subprograms marked Inline_Always, to minimize
3972 -- the use of the run-time library.
3973
3974 elsif Is_Predefined_File_Name
3975 (Unit_File_Name (Get_Source_Unit (Gen_Decl)))
3976 and then Configurable_Run_Time_Mode
3977 and then Nkind (Parent (N)) /= N_Compilation_Unit
3978 then
3979 Inline_Now := True;
3980 end if;
3981
3982 -- If the current scope is itself an instance within a child
3983 -- unit, there will be duplications in the scope stack, and the
3984 -- unstacking mechanism in Inline_Instance_Body will fail.
3985 -- This loses some rare cases of optimization, and might be
3986 -- improved some day, if we can find a proper abstraction for
3987 -- "the complete compilation context" that can be saved and
3988 -- restored. ???
3989
3990 if Is_Generic_Instance (Current_Scope) then
3991 declare
3992 Curr_Unit : constant Entity_Id :=
3993 Cunit_Entity (Current_Sem_Unit);
3994 begin
3995 if Curr_Unit /= Current_Scope
3996 and then Is_Child_Unit (Curr_Unit)
3997 then
3998 Inline_Now := False;
3999 end if;
4000 end;
4001 end if;
4002 end if;
4003
4004 Needs_Body :=
4005 (Unit_Requires_Body (Gen_Unit)
4006 or else Enclosing_Body_Present
4007 or else Present (Corresponding_Body (Gen_Decl)))
4008 and then (Is_In_Main_Unit (N) or else Might_Inline_Subp)
4009 and then not Is_Actual_Pack
4010 and then not Inline_Now
4011 and then (Operating_Mode = Generate_Code
4012
4013 -- Need comment for this check ???
4014
4015 or else (Operating_Mode = Check_Semantics
4016 and then (ASIS_Mode or GNATprove_Mode)));
4017
4018 -- If front-end inlining is enabled or there are any subprograms
4019 -- marked with Inline_Always, do not instantiate body when within
4020 -- a generic context.
4021
4022 if ((Front_End_Inlining or else Has_Inline_Always)
4023 and then not Expander_Active)
4024 or else Is_Generic_Unit (Cunit_Entity (Main_Unit))
4025 then
4026 Needs_Body := False;
4027 end if;
4028
4029 -- If the current context is generic, and the package being
4030 -- instantiated is declared within a formal package, there is no
4031 -- body to instantiate until the enclosing generic is instantiated
4032 -- and there is an actual for the formal package. If the formal
4033 -- package has parameters, we build a regular package instance for
4034 -- it, that precedes the original formal package declaration.
4035
4036 if In_Open_Scopes (Scope (Scope (Gen_Unit))) then
4037 declare
4038 Decl : constant Node_Id :=
4039 Original_Node
4040 (Unit_Declaration_Node (Scope (Gen_Unit)));
4041 begin
4042 if Nkind (Decl) = N_Formal_Package_Declaration
4043 or else (Nkind (Decl) = N_Package_Declaration
4044 and then Is_List_Member (Decl)
4045 and then Present (Next (Decl))
4046 and then
4047 Nkind (Next (Decl)) =
4048 N_Formal_Package_Declaration)
4049 then
4050 Needs_Body := False;
4051 end if;
4052 end;
4053 end if;
4054 end;
4055
4056 -- For RCI unit calling stubs, we omit the instance body if the
4057 -- instance is the RCI library unit itself.
4058
4059 -- However there is a special case for nested instances: in this case
4060 -- we do generate the instance body, as it might be required, e.g.
4061 -- because it provides stream attributes for some type used in the
4062 -- profile of a remote subprogram. This is consistent with 12.3(12),
4063 -- which indicates that the instance body occurs at the place of the
4064 -- instantiation, and thus is part of the RCI declaration, which is
4065 -- present on all client partitions (this is E.2.3(18)).
4066
4067 -- Note that AI12-0002 may make it illegal at some point to have
4068 -- stream attributes defined in an RCI unit, in which case this
4069 -- special case will become unnecessary. In the meantime, there
4070 -- is known application code in production that depends on this
4071 -- being possible, so we definitely cannot eliminate the body in
4072 -- the case of nested instances for the time being.
4073
4074 -- When we generate a nested instance body, calling stubs for any
4075 -- relevant subprogram will be be inserted immediately after the
4076 -- subprogram declarations, and will take precedence over the
4077 -- subsequent (original) body. (The stub and original body will be
4078 -- complete homographs, but this is permitted in an instance).
4079 -- (Could we do better and remove the original body???)
4080
4081 if Distribution_Stub_Mode = Generate_Caller_Stub_Body
4082 and then Comes_From_Source (N)
4083 and then Nkind (Parent (N)) = N_Compilation_Unit
4084 then
4085 Needs_Body := False;
4086 end if;
4087
4088 if Needs_Body then
4089
4090 -- Here is a defence against a ludicrous number of instantiations
4091 -- caused by a circular set of instantiation attempts.
4092
4093 if Pending_Instantiations.Last > Maximum_Instantiations then
4094 Error_Msg_Uint_1 := UI_From_Int (Maximum_Instantiations);
4095 Error_Msg_N ("too many instantiations, exceeds max of^", N);
4096 Error_Msg_N ("\limit can be changed using -gnateinn switch", N);
4097 raise Unrecoverable_Error;
4098 end if;
4099
4100 -- Indicate that the enclosing scopes contain an instantiation,
4101 -- and that cleanup actions should be delayed until after the
4102 -- instance body is expanded.
4103
4104 Check_Forward_Instantiation (Gen_Decl);
4105 if Nkind (N) = N_Package_Instantiation then
4106 declare
4107 Enclosing_Master : Entity_Id;
4108
4109 begin
4110 -- Loop to search enclosing masters
4111
4112 Enclosing_Master := Current_Scope;
4113 Scope_Loop : while Enclosing_Master /= Standard_Standard loop
4114 if Ekind (Enclosing_Master) = E_Package then
4115 if Is_Compilation_Unit (Enclosing_Master) then
4116 if In_Package_Body (Enclosing_Master) then
4117 Delay_Descriptors
4118 (Body_Entity (Enclosing_Master));
4119 else
4120 Delay_Descriptors
4121 (Enclosing_Master);
4122 end if;
4123
4124 exit Scope_Loop;
4125
4126 else
4127 Enclosing_Master := Scope (Enclosing_Master);
4128 end if;
4129
4130 elsif Is_Generic_Unit (Enclosing_Master)
4131 or else Ekind (Enclosing_Master) = E_Void
4132 then
4133 -- Cleanup actions will eventually be performed on the
4134 -- enclosing subprogram or package instance, if any.
4135 -- Enclosing scope is void in the formal part of a
4136 -- generic subprogram.
4137
4138 exit Scope_Loop;
4139
4140 else
4141 if Ekind (Enclosing_Master) = E_Entry
4142 and then
4143 Ekind (Scope (Enclosing_Master)) = E_Protected_Type
4144 then
4145 if not Expander_Active then
4146 exit Scope_Loop;
4147 else
4148 Enclosing_Master :=
4149 Protected_Body_Subprogram (Enclosing_Master);
4150 end if;
4151 end if;
4152
4153 Set_Delay_Cleanups (Enclosing_Master);
4154
4155 while Ekind (Enclosing_Master) = E_Block loop
4156 Enclosing_Master := Scope (Enclosing_Master);
4157 end loop;
4158
4159 if Is_Subprogram (Enclosing_Master) then
4160 Delay_Descriptors (Enclosing_Master);
4161
4162 elsif Is_Task_Type (Enclosing_Master) then
4163 declare
4164 TBP : constant Node_Id :=
4165 Get_Task_Body_Procedure
4166 (Enclosing_Master);
4167 begin
4168 if Present (TBP) then
4169 Delay_Descriptors (TBP);
4170 Set_Delay_Cleanups (TBP);
4171 end if;
4172 end;
4173 end if;
4174
4175 exit Scope_Loop;
4176 end if;
4177 end loop Scope_Loop;
4178 end;
4179
4180 -- Make entry in table
4181
4182 Add_Pending_Instantiation (N, Act_Decl);
4183 end if;
4184 end if;
4185
4186 Set_Categorization_From_Pragmas (Act_Decl);
4187
4188 if Parent_Installed then
4189 Hide_Current_Scope;
4190 end if;
4191
4192 Set_Instance_Spec (N, Act_Decl);
4193
4194 -- If not a compilation unit, insert the package declaration before
4195 -- the original instantiation node.
4196
4197 if Nkind (Parent (N)) /= N_Compilation_Unit then
4198 Mark_Rewrite_Insertion (Act_Decl);
4199 Insert_Before (N, Act_Decl);
4200
4201 if Has_Aspects (N) then
4202 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4203
4204 -- The pragma created for a Default_Storage_Pool aspect must
4205 -- appear ahead of the declarations in the instance spec.
4206 -- Analysis has placed it after the instance node, so remove
4207 -- it and reinsert it properly now.
4208
4209 declare
4210 ASN : constant Node_Id := First (Aspect_Specifications (N));
4211 A_Name : constant Name_Id := Chars (Identifier (ASN));
4212 Decl : Node_Id;
4213
4214 begin
4215 if A_Name = Name_Default_Storage_Pool then
4216 if No (Visible_Declarations (Act_Spec)) then
4217 Set_Visible_Declarations (Act_Spec, New_List);
4218 end if;
4219
4220 Decl := Next (N);
4221 while Present (Decl) loop
4222 if Nkind (Decl) = N_Pragma then
4223 Remove (Decl);
4224 Prepend (Decl, Visible_Declarations (Act_Spec));
4225 exit;
4226 end if;
4227
4228 Next (Decl);
4229 end loop;
4230 end if;
4231 end;
4232 end if;
4233
4234 Analyze (Act_Decl);
4235
4236 -- For an instantiation that is a compilation unit, place
4237 -- declaration on current node so context is complete for analysis
4238 -- (including nested instantiations). If this is the main unit,
4239 -- the declaration eventually replaces the instantiation node.
4240 -- If the instance body is created later, it replaces the
4241 -- instance node, and the declaration is attached to it
4242 -- (see Build_Instance_Compilation_Unit_Nodes).
4243
4244 else
4245 if Cunit_Entity (Current_Sem_Unit) = Defining_Entity (N) then
4246
4247 -- The entity for the current unit is the newly created one,
4248 -- and all semantic information is attached to it.
4249
4250 Set_Cunit_Entity (Current_Sem_Unit, Act_Decl_Id);
4251
4252 -- If this is the main unit, replace the main entity as well
4253
4254 if Current_Sem_Unit = Main_Unit then
4255 Main_Unit_Entity := Act_Decl_Id;
4256 end if;
4257 end if;
4258
4259 Set_Unit (Parent (N), Act_Decl);
4260 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
4261 Set_Package_Instantiation (Act_Decl_Id, N);
4262
4263 -- Process aspect specifications of the instance node, if any, to
4264 -- take into account categorization pragmas before analyzing the
4265 -- instance.
4266
4267 if Has_Aspects (N) then
4268 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4269 end if;
4270
4271 Analyze (Act_Decl);
4272 Set_Unit (Parent (N), N);
4273 Set_Body_Required (Parent (N), False);
4274
4275 -- We never need elaboration checks on instantiations, since by
4276 -- definition, the body instantiation is elaborated at the same
4277 -- time as the spec instantiation.
4278
4279 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
4280 Set_Kill_Elaboration_Checks (Act_Decl_Id);
4281 end if;
4282
4283 Check_Elab_Instantiation (N);
4284
4285 if ABE_Is_Certain (N) and then Needs_Body then
4286 Pending_Instantiations.Decrement_Last;
4287 end if;
4288
4289 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
4290
4291 Set_First_Private_Entity (Defining_Unit_Name (Unit_Renaming),
4292 First_Private_Entity (Act_Decl_Id));
4293
4294 -- If the instantiation will receive a body, the unit will be
4295 -- transformed into a package body, and receive its own elaboration
4296 -- entity. Otherwise, the nature of the unit is now a package
4297 -- declaration.
4298
4299 if Nkind (Parent (N)) = N_Compilation_Unit
4300 and then not Needs_Body
4301 then
4302 Rewrite (N, Act_Decl);
4303 end if;
4304
4305 if Present (Corresponding_Body (Gen_Decl))
4306 or else Unit_Requires_Body (Gen_Unit)
4307 then
4308 Set_Has_Completion (Act_Decl_Id);
4309 end if;
4310
4311 Check_Formal_Packages (Act_Decl_Id);
4312
4313 Restore_Hidden_Primitives (Vis_Prims_List);
4314 Restore_Private_Views (Act_Decl_Id);
4315
4316 Inherit_Context (Gen_Decl, N);
4317
4318 if Parent_Installed then
4319 Remove_Parent;
4320 end if;
4321
4322 Restore_Env;
4323 Env_Installed := False;
4324 end if;
4325
4326 Validate_Categorization_Dependency (N, Act_Decl_Id);
4327
4328 -- There used to be a check here to prevent instantiations in local
4329 -- contexts if the No_Local_Allocators restriction was active. This
4330 -- check was removed by a binding interpretation in AI-95-00130/07,
4331 -- but we retain the code for documentation purposes.
4332
4333 -- if Ekind (Act_Decl_Id) /= E_Void
4334 -- and then not Is_Library_Level_Entity (Act_Decl_Id)
4335 -- then
4336 -- Check_Restriction (No_Local_Allocators, N);
4337 -- end if;
4338
4339 if Inline_Now then
4340 Inline_Instance_Body (N, Gen_Unit, Act_Decl);
4341 end if;
4342
4343 -- The following is a tree patch for ASIS: ASIS needs separate nodes to
4344 -- be used as defining identifiers for a formal package and for the
4345 -- corresponding expanded package.
4346
4347 if Nkind (N) = N_Formal_Package_Declaration then
4348 Act_Decl_Id := New_Copy (Defining_Entity (N));
4349 Set_Comes_From_Source (Act_Decl_Id, True);
4350 Set_Is_Generic_Instance (Act_Decl_Id, False);
4351 Set_Defining_Identifier (N, Act_Decl_Id);
4352 end if;
4353
4354 Ignore_Pragma_SPARK_Mode := Save_IPSM;
4355 SPARK_Mode := Save_SM;
4356 SPARK_Mode_Pragma := Save_SMP;
4357 Style_Check := Save_Style_Check;
4358
4359 -- Check that if N is an instantiation of System.Dim_Float_IO or
4360 -- System.Dim_Integer_IO, the formal type has a dimension system.
4361
4362 if Nkind (N) = N_Package_Instantiation
4363 and then Is_Dim_IO_Package_Instantiation (N)
4364 then
4365 declare
4366 Assoc : constant Node_Id := First (Generic_Associations (N));
4367 begin
4368 if not Has_Dimension_System
4369 (Etype (Explicit_Generic_Actual_Parameter (Assoc)))
4370 then
4371 Error_Msg_N ("type with a dimension system expected", Assoc);
4372 end if;
4373 end;
4374 end if;
4375
4376 <<Leave>>
4377 if Has_Aspects (N) and then Nkind (Parent (N)) /= N_Compilation_Unit then
4378 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4379 end if;
4380
4381 exception
4382 when Instantiation_Error =>
4383 if Parent_Installed then
4384 Remove_Parent;
4385 end if;
4386
4387 if Env_Installed then
4388 Restore_Env;
4389 end if;
4390
4391 Ignore_Pragma_SPARK_Mode := Save_IPSM;
4392 SPARK_Mode := Save_SM;
4393 SPARK_Mode_Pragma := Save_SMP;
4394 Style_Check := Save_Style_Check;
4395 end Analyze_Package_Instantiation;
4396
4397 --------------------------
4398 -- Inline_Instance_Body --
4399 --------------------------
4400
4401 procedure Inline_Instance_Body
4402 (N : Node_Id;
4403 Gen_Unit : Entity_Id;
4404 Act_Decl : Node_Id)
4405 is
4406 Curr_Comp : constant Node_Id := Cunit (Current_Sem_Unit);
4407 Curr_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
4408 Gen_Comp : constant Entity_Id :=
4409 Cunit_Entity (Get_Source_Unit (Gen_Unit));
4410
4411 Save_SM : constant SPARK_Mode_Type := SPARK_Mode;
4412 Save_SMP : constant Node_Id := SPARK_Mode_Pragma;
4413 -- Save all SPARK_Mode-related attributes as removing enclosing scopes
4414 -- to provide a clean environment for analysis of the inlined body will
4415 -- eliminate any previously set SPARK_Mode.
4416
4417 Scope_Stack_Depth : constant Pos :=
4418 Scope_Stack.Last - Scope_Stack.First + 1;
4419
4420 Use_Clauses : array (1 .. Scope_Stack_Depth) of Node_Id;
4421 Instances : array (1 .. Scope_Stack_Depth) of Entity_Id;
4422 Inner_Scopes : array (1 .. Scope_Stack_Depth) of Entity_Id;
4423 Curr_Scope : Entity_Id := Empty;
4424 List : Elist_Id;
4425 Num_Inner : Nat := 0;
4426 Num_Scopes : Nat := 0;
4427 N_Instances : Nat := 0;
4428 Removed : Boolean := False;
4429 S : Entity_Id;
4430 Vis : Boolean;
4431
4432 begin
4433 -- Case of generic unit defined in another unit. We must remove the
4434 -- complete context of the current unit to install that of the generic.
4435
4436 if Gen_Comp /= Cunit_Entity (Current_Sem_Unit) then
4437
4438 -- Add some comments for the following two loops ???
4439
4440 S := Current_Scope;
4441 while Present (S) and then S /= Standard_Standard loop
4442 loop
4443 Num_Scopes := Num_Scopes + 1;
4444
4445 Use_Clauses (Num_Scopes) :=
4446 (Scope_Stack.Table
4447 (Scope_Stack.Last - Num_Scopes + 1).
4448 First_Use_Clause);
4449 End_Use_Clauses (Use_Clauses (Num_Scopes));
4450
4451 exit when Scope_Stack.Last - Num_Scopes + 1 = Scope_Stack.First
4452 or else Scope_Stack.Table
4453 (Scope_Stack.Last - Num_Scopes).Entity = Scope (S);
4454 end loop;
4455
4456 exit when Is_Generic_Instance (S)
4457 and then (In_Package_Body (S)
4458 or else Ekind (S) = E_Procedure
4459 or else Ekind (S) = E_Function);
4460 S := Scope (S);
4461 end loop;
4462
4463 Vis := Is_Immediately_Visible (Gen_Comp);
4464
4465 -- Find and save all enclosing instances
4466
4467 S := Current_Scope;
4468
4469 while Present (S)
4470 and then S /= Standard_Standard
4471 loop
4472 if Is_Generic_Instance (S) then
4473 N_Instances := N_Instances + 1;
4474 Instances (N_Instances) := S;
4475
4476 exit when In_Package_Body (S);
4477 end if;
4478
4479 S := Scope (S);
4480 end loop;
4481
4482 -- Remove context of current compilation unit, unless we are within a
4483 -- nested package instantiation, in which case the context has been
4484 -- removed previously.
4485
4486 -- If current scope is the body of a child unit, remove context of
4487 -- spec as well. If an enclosing scope is an instance body, the
4488 -- context has already been removed, but the entities in the body
4489 -- must be made invisible as well.
4490
4491 S := Current_Scope;
4492 while Present (S) and then S /= Standard_Standard loop
4493 if Is_Generic_Instance (S)
4494 and then (In_Package_Body (S)
4495 or else Ekind_In (S, E_Procedure, E_Function))
4496 then
4497 -- We still have to remove the entities of the enclosing
4498 -- instance from direct visibility.
4499
4500 declare
4501 E : Entity_Id;
4502 begin
4503 E := First_Entity (S);
4504 while Present (E) loop
4505 Set_Is_Immediately_Visible (E, False);
4506 Next_Entity (E);
4507 end loop;
4508 end;
4509
4510 exit;
4511 end if;
4512
4513 if S = Curr_Unit
4514 or else (Ekind (Curr_Unit) = E_Package_Body
4515 and then S = Spec_Entity (Curr_Unit))
4516 or else (Ekind (Curr_Unit) = E_Subprogram_Body
4517 and then S = Corresponding_Spec
4518 (Unit_Declaration_Node (Curr_Unit)))
4519 then
4520 Removed := True;
4521
4522 -- Remove entities in current scopes from visibility, so that
4523 -- instance body is compiled in a clean environment.
4524
4525 List := Save_Scope_Stack (Handle_Use => False);
4526
4527 if Is_Child_Unit (S) then
4528
4529 -- Remove child unit from stack, as well as inner scopes.
4530 -- Removing the context of a child unit removes parent units
4531 -- as well.
4532
4533 while Current_Scope /= S loop
4534 Num_Inner := Num_Inner + 1;
4535 Inner_Scopes (Num_Inner) := Current_Scope;
4536 Pop_Scope;
4537 end loop;
4538
4539 Pop_Scope;
4540 Remove_Context (Curr_Comp);
4541 Curr_Scope := S;
4542
4543 else
4544 Remove_Context (Curr_Comp);
4545 end if;
4546
4547 if Ekind (Curr_Unit) = E_Package_Body then
4548 Remove_Context (Library_Unit (Curr_Comp));
4549 end if;
4550 end if;
4551
4552 S := Scope (S);
4553 end loop;
4554
4555 pragma Assert (Num_Inner < Num_Scopes);
4556
4557 -- The inlined package body must be analyzed with the SPARK_Mode of
4558 -- the enclosing context, otherwise the body may cause bogus errors
4559 -- if a configuration SPARK_Mode pragma in in effect.
4560
4561 Push_Scope (Standard_Standard);
4562 Scope_Stack.Table (Scope_Stack.Last).Is_Active_Stack_Base := True;
4563 Instantiate_Package_Body
4564 (Body_Info =>
4565 ((Inst_Node => N,
4566 Act_Decl => Act_Decl,
4567 Expander_Status => Expander_Active,
4568 Current_Sem_Unit => Current_Sem_Unit,
4569 Scope_Suppress => Scope_Suppress,
4570 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
4571 Version => Ada_Version,
4572 Version_Pragma => Ada_Version_Pragma,
4573 Warnings => Save_Warnings,
4574 SPARK_Mode => Save_SM,
4575 SPARK_Mode_Pragma => Save_SMP)),
4576 Inlined_Body => True);
4577
4578 Pop_Scope;
4579
4580 -- Restore context
4581
4582 Set_Is_Immediately_Visible (Gen_Comp, Vis);
4583
4584 -- Reset Generic_Instance flag so that use clauses can be installed
4585 -- in the proper order. (See Use_One_Package for effect of enclosing
4586 -- instances on processing of use clauses).
4587
4588 for J in 1 .. N_Instances loop
4589 Set_Is_Generic_Instance (Instances (J), False);
4590 end loop;
4591
4592 if Removed then
4593 Install_Context (Curr_Comp);
4594
4595 if Present (Curr_Scope)
4596 and then Is_Child_Unit (Curr_Scope)
4597 then
4598 Push_Scope (Curr_Scope);
4599 Set_Is_Immediately_Visible (Curr_Scope);
4600
4601 -- Finally, restore inner scopes as well
4602
4603 for J in reverse 1 .. Num_Inner loop
4604 Push_Scope (Inner_Scopes (J));
4605 end loop;
4606 end if;
4607
4608 Restore_Scope_Stack (List, Handle_Use => False);
4609
4610 if Present (Curr_Scope)
4611 and then
4612 (In_Private_Part (Curr_Scope)
4613 or else In_Package_Body (Curr_Scope))
4614 then
4615 -- Install private declaration of ancestor units, which are
4616 -- currently available. Restore_Scope_Stack and Install_Context
4617 -- only install the visible part of parents.
4618
4619 declare
4620 Par : Entity_Id;
4621 begin
4622 Par := Scope (Curr_Scope);
4623 while (Present (Par)) and then Par /= Standard_Standard loop
4624 Install_Private_Declarations (Par);
4625 Par := Scope (Par);
4626 end loop;
4627 end;
4628 end if;
4629 end if;
4630
4631 -- Restore use clauses. For a child unit, use clauses in the parents
4632 -- are restored when installing the context, so only those in inner
4633 -- scopes (and those local to the child unit itself) need to be
4634 -- installed explicitly.
4635
4636 if Is_Child_Unit (Curr_Unit) and then Removed then
4637 for J in reverse 1 .. Num_Inner + 1 loop
4638 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
4639 Use_Clauses (J);
4640 Install_Use_Clauses (Use_Clauses (J));
4641 end loop;
4642
4643 else
4644 for J in reverse 1 .. Num_Scopes loop
4645 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
4646 Use_Clauses (J);
4647 Install_Use_Clauses (Use_Clauses (J));
4648 end loop;
4649 end if;
4650
4651 -- Restore status of instances. If one of them is a body, make its
4652 -- local entities visible again.
4653
4654 declare
4655 E : Entity_Id;
4656 Inst : Entity_Id;
4657
4658 begin
4659 for J in 1 .. N_Instances loop
4660 Inst := Instances (J);
4661 Set_Is_Generic_Instance (Inst, True);
4662
4663 if In_Package_Body (Inst)
4664 or else Ekind_In (S, E_Procedure, E_Function)
4665 then
4666 E := First_Entity (Instances (J));
4667 while Present (E) loop
4668 Set_Is_Immediately_Visible (E);
4669 Next_Entity (E);
4670 end loop;
4671 end if;
4672 end loop;
4673 end;
4674
4675 -- If generic unit is in current unit, current context is correct. Note
4676 -- that the context is guaranteed to carry the correct SPARK_Mode as no
4677 -- enclosing scopes were removed.
4678
4679 else
4680 Instantiate_Package_Body
4681 (Body_Info =>
4682 ((Inst_Node => N,
4683 Act_Decl => Act_Decl,
4684 Expander_Status => Expander_Active,
4685 Current_Sem_Unit => Current_Sem_Unit,
4686 Scope_Suppress => Scope_Suppress,
4687 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
4688 Version => Ada_Version,
4689 Version_Pragma => Ada_Version_Pragma,
4690 Warnings => Save_Warnings,
4691 SPARK_Mode => SPARK_Mode,
4692 SPARK_Mode_Pragma => SPARK_Mode_Pragma)),
4693 Inlined_Body => True);
4694 end if;
4695 end Inline_Instance_Body;
4696
4697 -------------------------------------
4698 -- Analyze_Procedure_Instantiation --
4699 -------------------------------------
4700
4701 procedure Analyze_Procedure_Instantiation (N : Node_Id) is
4702 begin
4703 Analyze_Subprogram_Instantiation (N, E_Procedure);
4704 end Analyze_Procedure_Instantiation;
4705
4706 -----------------------------------
4707 -- Need_Subprogram_Instance_Body --
4708 -----------------------------------
4709
4710 function Need_Subprogram_Instance_Body
4711 (N : Node_Id;
4712 Subp : Entity_Id) return Boolean
4713 is
4714
4715 function Is_Inlined_Or_Child_Of_Inlined (E : Entity_Id) return Boolean;
4716 -- Return True if E is an inlined subprogram, an inlined renaming or a
4717 -- subprogram nested in an inlined subprogram. The inlining machinery
4718 -- totally disregards nested subprograms since it considers that they
4719 -- will always be compiled if the parent is (see Inline.Is_Nested).
4720
4721 ------------------------------------
4722 -- Is_Inlined_Or_Child_Of_Inlined --
4723 ------------------------------------
4724
4725 function Is_Inlined_Or_Child_Of_Inlined (E : Entity_Id) return Boolean is
4726 Scop : Entity_Id;
4727
4728 begin
4729 if Is_Inlined (E) or else Is_Inlined (Alias (E)) then
4730 return True;
4731 end if;
4732
4733 Scop := Scope (E);
4734 while Scop /= Standard_Standard loop
4735 if Ekind (Scop) in Subprogram_Kind and then Is_Inlined (Scop) then
4736 return True;
4737 end if;
4738
4739 Scop := Scope (Scop);
4740 end loop;
4741
4742 return False;
4743 end Is_Inlined_Or_Child_Of_Inlined;
4744
4745 begin
4746 -- Must be in the main unit or inlined (or child of inlined)
4747
4748 if (Is_In_Main_Unit (N) or else Is_Inlined_Or_Child_Of_Inlined (Subp))
4749
4750 -- Must be generating code or analyzing code in ASIS/GNATprove mode
4751
4752 and then (Operating_Mode = Generate_Code
4753 or else (Operating_Mode = Check_Semantics
4754 and then (ASIS_Mode or GNATprove_Mode)))
4755
4756 -- The body is needed when generating code (full expansion), in ASIS
4757 -- mode for other tools, and in GNATprove mode (special expansion) for
4758 -- formal verification of the body itself.
4759
4760 and then (Expander_Active or ASIS_Mode or GNATprove_Mode)
4761
4762 -- No point in inlining if ABE is inevitable
4763
4764 and then not ABE_Is_Certain (N)
4765
4766 -- Or if subprogram is eliminated
4767
4768 and then not Is_Eliminated (Subp)
4769 then
4770 Add_Pending_Instantiation (N, Unit_Declaration_Node (Subp));
4771 return True;
4772
4773 -- Here if not inlined, or we ignore the inlining
4774
4775 else
4776 return False;
4777 end if;
4778 end Need_Subprogram_Instance_Body;
4779
4780 --------------------------------------
4781 -- Analyze_Subprogram_Instantiation --
4782 --------------------------------------
4783
4784 procedure Analyze_Subprogram_Instantiation
4785 (N : Node_Id;
4786 K : Entity_Kind)
4787 is
4788 Loc : constant Source_Ptr := Sloc (N);
4789 Gen_Id : constant Node_Id := Name (N);
4790
4791 Anon_Id : constant Entity_Id :=
4792 Make_Defining_Identifier (Sloc (Defining_Entity (N)),
4793 Chars => New_External_Name
4794 (Chars (Defining_Entity (N)), 'R'));
4795
4796 Act_Decl_Id : Entity_Id;
4797 Act_Decl : Node_Id;
4798 Act_Spec : Node_Id;
4799 Act_Tree : Node_Id;
4800
4801 Env_Installed : Boolean := False;
4802 Gen_Unit : Entity_Id;
4803 Gen_Decl : Node_Id;
4804 Pack_Id : Entity_Id;
4805 Parent_Installed : Boolean := False;
4806
4807 Renaming_List : List_Id;
4808 -- The list of declarations that link formals and actuals of the
4809 -- instance. These are subtype declarations for formal types, and
4810 -- renaming declarations for other formals. The subprogram declaration
4811 -- for the instance is then appended to the list, and the last item on
4812 -- the list is the renaming declaration for the instance.
4813
4814 procedure Analyze_Instance_And_Renamings;
4815 -- The instance must be analyzed in a context that includes the mappings
4816 -- of generic parameters into actuals. We create a package declaration
4817 -- for this purpose, and a subprogram with an internal name within the
4818 -- package. The subprogram instance is simply an alias for the internal
4819 -- subprogram, declared in the current scope.
4820
4821 procedure Build_Subprogram_Renaming;
4822 -- If the subprogram is recursive, there are occurrences of the name of
4823 -- the generic within the body, which must resolve to the current
4824 -- instance. We add a renaming declaration after the declaration, which
4825 -- is available in the instance body, as well as in the analysis of
4826 -- aspects that appear in the generic. This renaming declaration is
4827 -- inserted after the instance declaration which it renames.
4828
4829 ------------------------------------
4830 -- Analyze_Instance_And_Renamings --
4831 ------------------------------------
4832
4833 procedure Analyze_Instance_And_Renamings is
4834 Def_Ent : constant Entity_Id := Defining_Entity (N);
4835 Pack_Decl : Node_Id;
4836
4837 begin
4838 if Nkind (Parent (N)) = N_Compilation_Unit then
4839
4840 -- For the case of a compilation unit, the container package has
4841 -- the same name as the instantiation, to insure that the binder
4842 -- calls the elaboration procedure with the right name. Copy the
4843 -- entity of the instance, which may have compilation level flags
4844 -- (e.g. Is_Child_Unit) set.
4845
4846 Pack_Id := New_Copy (Def_Ent);
4847
4848 else
4849 -- Otherwise we use the name of the instantiation concatenated
4850 -- with its source position to ensure uniqueness if there are
4851 -- several instantiations with the same name.
4852
4853 Pack_Id :=
4854 Make_Defining_Identifier (Loc,
4855 Chars => New_External_Name
4856 (Related_Id => Chars (Def_Ent),
4857 Suffix => "GP",
4858 Suffix_Index => Source_Offset (Sloc (Def_Ent))));
4859 end if;
4860
4861 Pack_Decl :=
4862 Make_Package_Declaration (Loc,
4863 Specification => Make_Package_Specification (Loc,
4864 Defining_Unit_Name => Pack_Id,
4865 Visible_Declarations => Renaming_List,
4866 End_Label => Empty));
4867
4868 Set_Instance_Spec (N, Pack_Decl);
4869 Set_Is_Generic_Instance (Pack_Id);
4870 Set_Debug_Info_Needed (Pack_Id);
4871
4872 -- Case of not a compilation unit
4873
4874 if Nkind (Parent (N)) /= N_Compilation_Unit then
4875 Mark_Rewrite_Insertion (Pack_Decl);
4876 Insert_Before (N, Pack_Decl);
4877 Set_Has_Completion (Pack_Id);
4878
4879 -- Case of an instantiation that is a compilation unit
4880
4881 -- Place declaration on current node so context is complete for
4882 -- analysis (including nested instantiations), and for use in a
4883 -- context_clause (see Analyze_With_Clause).
4884
4885 else
4886 Set_Unit (Parent (N), Pack_Decl);
4887 Set_Parent_Spec (Pack_Decl, Parent_Spec (N));
4888 end if;
4889
4890 Analyze (Pack_Decl);
4891 Check_Formal_Packages (Pack_Id);
4892 Set_Is_Generic_Instance (Pack_Id, False);
4893
4894 -- Why do we clear Is_Generic_Instance??? We set it 20 lines
4895 -- above???
4896
4897 -- Body of the enclosing package is supplied when instantiating the
4898 -- subprogram body, after semantic analysis is completed.
4899
4900 if Nkind (Parent (N)) = N_Compilation_Unit then
4901
4902 -- Remove package itself from visibility, so it does not
4903 -- conflict with subprogram.
4904
4905 Set_Name_Entity_Id (Chars (Pack_Id), Homonym (Pack_Id));
4906
4907 -- Set name and scope of internal subprogram so that the proper
4908 -- external name will be generated. The proper scope is the scope
4909 -- of the wrapper package. We need to generate debugging info for
4910 -- the internal subprogram, so set flag accordingly.
4911
4912 Set_Chars (Anon_Id, Chars (Defining_Entity (N)));
4913 Set_Scope (Anon_Id, Scope (Pack_Id));
4914
4915 -- Mark wrapper package as referenced, to avoid spurious warnings
4916 -- if the instantiation appears in various with_ clauses of
4917 -- subunits of the main unit.
4918
4919 Set_Referenced (Pack_Id);
4920 end if;
4921
4922 Set_Is_Generic_Instance (Anon_Id);
4923 Set_Debug_Info_Needed (Anon_Id);
4924 Act_Decl_Id := New_Copy (Anon_Id);
4925
4926 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
4927 Set_Chars (Act_Decl_Id, Chars (Defining_Entity (N)));
4928 Set_Sloc (Act_Decl_Id, Sloc (Defining_Entity (N)));
4929
4930 -- Subprogram instance comes from source only if generic does
4931
4932 Set_Comes_From_Source (Act_Decl_Id, Comes_From_Source (Gen_Unit));
4933
4934 -- If the instance is a child unit, mark the Id accordingly. Mark
4935 -- the anonymous entity as well, which is the real subprogram and
4936 -- which is used when the instance appears in a context clause.
4937 -- Similarly, propagate the Is_Eliminated flag to handle properly
4938 -- nested eliminated subprograms.
4939
4940 Set_Is_Child_Unit (Act_Decl_Id, Is_Child_Unit (Defining_Entity (N)));
4941 Set_Is_Child_Unit (Anon_Id, Is_Child_Unit (Defining_Entity (N)));
4942 New_Overloaded_Entity (Act_Decl_Id);
4943 Check_Eliminated (Act_Decl_Id);
4944 Set_Is_Eliminated (Anon_Id, Is_Eliminated (Act_Decl_Id));
4945
4946 -- In compilation unit case, kill elaboration checks on the
4947 -- instantiation, since they are never needed -- the body is
4948 -- instantiated at the same point as the spec.
4949
4950 if Nkind (Parent (N)) = N_Compilation_Unit then
4951 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
4952 Set_Kill_Elaboration_Checks (Act_Decl_Id);
4953 Set_Is_Compilation_Unit (Anon_Id);
4954
4955 Set_Cunit_Entity (Current_Sem_Unit, Pack_Id);
4956 end if;
4957
4958 -- The instance is not a freezing point for the new subprogram.
4959 -- The anonymous subprogram may have a freeze node, created for
4960 -- some delayed aspects. This freeze node must not be inherited
4961 -- by the visible subprogram entity.
4962
4963 Set_Is_Frozen (Act_Decl_Id, False);
4964 Set_Freeze_Node (Act_Decl_Id, Empty);
4965
4966 if Nkind (Defining_Entity (N)) = N_Defining_Operator_Symbol then
4967 Valid_Operator_Definition (Act_Decl_Id);
4968 end if;
4969
4970 Set_Alias (Act_Decl_Id, Anon_Id);
4971 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
4972 Set_Has_Completion (Act_Decl_Id);
4973 Set_Related_Instance (Pack_Id, Act_Decl_Id);
4974
4975 if Nkind (Parent (N)) = N_Compilation_Unit then
4976 Set_Body_Required (Parent (N), False);
4977 end if;
4978 end Analyze_Instance_And_Renamings;
4979
4980 -------------------------------
4981 -- Build_Subprogram_Renaming --
4982 -------------------------------
4983
4984 procedure Build_Subprogram_Renaming is
4985 Renaming_Decl : Node_Id;
4986 Unit_Renaming : Node_Id;
4987
4988 begin
4989 Unit_Renaming :=
4990 Make_Subprogram_Renaming_Declaration (Loc,
4991 Specification =>
4992 Copy_Generic_Node
4993 (Specification (Original_Node (Gen_Decl)),
4994 Empty,
4995 Instantiating => True),
4996 Name => New_Occurrence_Of (Anon_Id, Loc));
4997
4998 -- The generic may be a a child unit. The renaming needs an
4999 -- identifier with the proper name.
5000
5001 Set_Defining_Unit_Name (Specification (Unit_Renaming),
5002 Make_Defining_Identifier (Loc, Chars (Gen_Unit)));
5003
5004 -- If there is a formal subprogram with the same name as the unit
5005 -- itself, do not add this renaming declaration, to prevent
5006 -- ambiguities when there is a call with that name in the body.
5007 -- This is a partial and ugly fix for one ACATS test. ???
5008
5009 Renaming_Decl := First (Renaming_List);
5010 while Present (Renaming_Decl) loop
5011 if Nkind (Renaming_Decl) = N_Subprogram_Renaming_Declaration
5012 and then
5013 Chars (Defining_Entity (Renaming_Decl)) = Chars (Gen_Unit)
5014 then
5015 exit;
5016 end if;
5017
5018 Next (Renaming_Decl);
5019 end loop;
5020
5021 if No (Renaming_Decl) then
5022 Append (Unit_Renaming, Renaming_List);
5023 end if;
5024 end Build_Subprogram_Renaming;
5025
5026 -- Local variables
5027
5028 Save_IPSM : constant Boolean := Ignore_Pragma_SPARK_Mode;
5029 -- Save flag Ignore_Pragma_SPARK_Mode for restore on exit
5030
5031 Save_SM : constant SPARK_Mode_Type := SPARK_Mode;
5032 Save_SMP : constant Node_Id := SPARK_Mode_Pragma;
5033 -- Save the SPARK_Mode-related data for restore on exit
5034
5035 Vis_Prims_List : Elist_Id := No_Elist;
5036 -- List of primitives made temporarily visible in the instantiation
5037 -- to match the visibility of the formal type
5038
5039 -- Start of processing for Analyze_Subprogram_Instantiation
5040
5041 begin
5042 Check_SPARK_05_Restriction ("generic is not allowed", N);
5043
5044 -- Very first thing: check for special Text_IO unit in case we are
5045 -- instantiating one of the children of [[Wide_]Wide_]Text_IO. Of course
5046 -- such an instantiation is bogus (these are packages, not subprograms),
5047 -- but we get a better error message if we do this.
5048
5049 Check_Text_IO_Special_Unit (Gen_Id);
5050
5051 -- Make node global for error reporting
5052
5053 Instantiation_Node := N;
5054
5055 -- For package instantiations we turn off style checks, because they
5056 -- will have been emitted in the generic. For subprogram instantiations
5057 -- we want to apply at least the check on overriding indicators so we
5058 -- do not modify the style check status.
5059
5060 -- The renaming declarations for the actuals do not come from source and
5061 -- will not generate spurious warnings.
5062
5063 Preanalyze_Actuals (N);
5064
5065 Init_Env;
5066 Env_Installed := True;
5067 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
5068 Gen_Unit := Entity (Gen_Id);
5069
5070 Generate_Reference (Gen_Unit, Gen_Id);
5071
5072 if Nkind (Gen_Id) = N_Identifier
5073 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
5074 then
5075 Error_Msg_NE
5076 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
5077 end if;
5078
5079 if Etype (Gen_Unit) = Any_Type then
5080 Restore_Env;
5081 return;
5082 end if;
5083
5084 -- Verify that it is a generic subprogram of the right kind, and that
5085 -- it does not lead to a circular instantiation.
5086
5087 if K = E_Procedure and then Ekind (Gen_Unit) /= E_Generic_Procedure then
5088 Error_Msg_NE
5089 ("& is not the name of a generic procedure", Gen_Id, Gen_Unit);
5090
5091 elsif K = E_Function and then Ekind (Gen_Unit) /= E_Generic_Function then
5092 Error_Msg_NE
5093 ("& is not the name of a generic function", Gen_Id, Gen_Unit);
5094
5095 elsif In_Open_Scopes (Gen_Unit) then
5096 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
5097
5098 else
5099 -- If the context of the instance is subject to SPARK_Mode "off" or
5100 -- the annotation is altogether missing, set the global flag which
5101 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
5102 -- the instance.
5103
5104 if SPARK_Mode /= On then
5105 Ignore_Pragma_SPARK_Mode := True;
5106 end if;
5107
5108 Set_Entity (Gen_Id, Gen_Unit);
5109 Set_Is_Instantiated (Gen_Unit);
5110
5111 if In_Extended_Main_Source_Unit (N) then
5112 Generate_Reference (Gen_Unit, N);
5113 end if;
5114
5115 -- If renaming, get original unit
5116
5117 if Present (Renamed_Object (Gen_Unit))
5118 and then Ekind_In (Renamed_Object (Gen_Unit), E_Generic_Procedure,
5119 E_Generic_Function)
5120 then
5121 Gen_Unit := Renamed_Object (Gen_Unit);
5122 Set_Is_Instantiated (Gen_Unit);
5123 Generate_Reference (Gen_Unit, N);
5124 end if;
5125
5126 if Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
5127 Error_Msg_Node_2 := Current_Scope;
5128 Error_Msg_NE
5129 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
5130 Circularity_Detected := True;
5131 Restore_Hidden_Primitives (Vis_Prims_List);
5132 goto Leave;
5133 end if;
5134
5135 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
5136
5137 -- Initialize renamings map, for error checking
5138
5139 Generic_Renamings.Set_Last (0);
5140 Generic_Renamings_HTable.Reset;
5141
5142 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
5143
5144 -- Copy original generic tree, to produce text for instantiation
5145
5146 Act_Tree :=
5147 Copy_Generic_Node
5148 (Original_Node (Gen_Decl), Empty, Instantiating => True);
5149
5150 -- Inherit overriding indicator from instance node
5151
5152 Act_Spec := Specification (Act_Tree);
5153 Set_Must_Override (Act_Spec, Must_Override (N));
5154 Set_Must_Not_Override (Act_Spec, Must_Not_Override (N));
5155
5156 Renaming_List :=
5157 Analyze_Associations
5158 (I_Node => N,
5159 Formals => Generic_Formal_Declarations (Act_Tree),
5160 F_Copy => Generic_Formal_Declarations (Gen_Decl));
5161
5162 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
5163
5164 -- The subprogram itself cannot contain a nested instance, so the
5165 -- current parent is left empty.
5166
5167 Set_Instance_Env (Gen_Unit, Empty);
5168
5169 -- Build the subprogram declaration, which does not appear in the
5170 -- generic template, and give it a sloc consistent with that of the
5171 -- template.
5172
5173 Set_Defining_Unit_Name (Act_Spec, Anon_Id);
5174 Set_Generic_Parent (Act_Spec, Gen_Unit);
5175 Act_Decl :=
5176 Make_Subprogram_Declaration (Sloc (Act_Spec),
5177 Specification => Act_Spec);
5178
5179 -- The aspects have been copied previously, but they have to be
5180 -- linked explicitly to the new subprogram declaration. Explicit
5181 -- pre/postconditions on the instance are analyzed below, in a
5182 -- separate step.
5183
5184 Move_Aspects (Act_Tree, To => Act_Decl);
5185 Set_Categorization_From_Pragmas (Act_Decl);
5186
5187 if Parent_Installed then
5188 Hide_Current_Scope;
5189 end if;
5190
5191 Append (Act_Decl, Renaming_List);
5192
5193 -- Contract-related source pragmas that follow a generic subprogram
5194 -- must be instantiated explicitly because they are not part of the
5195 -- subprogram template.
5196
5197 Instantiate_Subprogram_Contract
5198 (Original_Node (Gen_Decl), Renaming_List);
5199
5200 Build_Subprogram_Renaming;
5201 Analyze_Instance_And_Renamings;
5202
5203 -- If the generic is marked Import (Intrinsic), then so is the
5204 -- instance. This indicates that there is no body to instantiate. If
5205 -- generic is marked inline, so it the instance, and the anonymous
5206 -- subprogram it renames. If inlined, or else if inlining is enabled
5207 -- for the compilation, we generate the instance body even if it is
5208 -- not within the main unit.
5209
5210 if Is_Intrinsic_Subprogram (Gen_Unit) then
5211 Set_Is_Intrinsic_Subprogram (Anon_Id);
5212 Set_Is_Intrinsic_Subprogram (Act_Decl_Id);
5213
5214 if Chars (Gen_Unit) = Name_Unchecked_Conversion then
5215 Validate_Unchecked_Conversion (N, Act_Decl_Id);
5216 end if;
5217 end if;
5218
5219 -- Inherit convention from generic unit. Intrinsic convention, as for
5220 -- an instance of unchecked conversion, is not inherited because an
5221 -- explicit Ada instance has been created.
5222
5223 if Has_Convention_Pragma (Gen_Unit)
5224 and then Convention (Gen_Unit) /= Convention_Intrinsic
5225 then
5226 Set_Convention (Act_Decl_Id, Convention (Gen_Unit));
5227 Set_Is_Exported (Act_Decl_Id, Is_Exported (Gen_Unit));
5228 end if;
5229
5230 Generate_Definition (Act_Decl_Id);
5231
5232 -- Inherit all inlining-related flags which apply to the generic in
5233 -- the subprogram and its declaration.
5234
5235 Set_Is_Inlined (Act_Decl_Id, Is_Inlined (Gen_Unit));
5236 Set_Is_Inlined (Anon_Id, Is_Inlined (Gen_Unit));
5237
5238 Set_Has_Pragma_Inline (Act_Decl_Id, Has_Pragma_Inline (Gen_Unit));
5239 Set_Has_Pragma_Inline (Anon_Id, Has_Pragma_Inline (Gen_Unit));
5240
5241 Set_Has_Pragma_Inline_Always
5242 (Act_Decl_Id, Has_Pragma_Inline_Always (Gen_Unit));
5243 Set_Has_Pragma_Inline_Always
5244 (Anon_Id, Has_Pragma_Inline_Always (Gen_Unit));
5245
5246 if not Is_Intrinsic_Subprogram (Gen_Unit) then
5247 Check_Elab_Instantiation (N);
5248 end if;
5249
5250 if Is_Dispatching_Operation (Act_Decl_Id)
5251 and then Ada_Version >= Ada_2005
5252 then
5253 declare
5254 Formal : Entity_Id;
5255
5256 begin
5257 Formal := First_Formal (Act_Decl_Id);
5258 while Present (Formal) loop
5259 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type
5260 and then Is_Controlling_Formal (Formal)
5261 and then not Can_Never_Be_Null (Formal)
5262 then
5263 Error_Msg_NE
5264 ("access parameter& is controlling,", N, Formal);
5265 Error_Msg_NE
5266 ("\corresponding parameter of & must be "
5267 & "explicitly null-excluding", N, Gen_Id);
5268 end if;
5269
5270 Next_Formal (Formal);
5271 end loop;
5272 end;
5273 end if;
5274
5275 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
5276
5277 Validate_Categorization_Dependency (N, Act_Decl_Id);
5278
5279 if not Is_Intrinsic_Subprogram (Act_Decl_Id) then
5280 Inherit_Context (Gen_Decl, N);
5281
5282 Restore_Private_Views (Pack_Id, False);
5283
5284 -- If the context requires a full instantiation, mark node for
5285 -- subsequent construction of the body.
5286
5287 if Need_Subprogram_Instance_Body (N, Act_Decl_Id) then
5288 Check_Forward_Instantiation (Gen_Decl);
5289
5290 -- The wrapper package is always delayed, because it does not
5291 -- constitute a freeze point, but to insure that the freeze node
5292 -- is placed properly, it is created directly when instantiating
5293 -- the body (otherwise the freeze node might appear to early for
5294 -- nested instantiations). For ASIS purposes, indicate that the
5295 -- wrapper package has replaced the instantiation node.
5296
5297 elsif Nkind (Parent (N)) = N_Compilation_Unit then
5298 Rewrite (N, Unit (Parent (N)));
5299 Set_Unit (Parent (N), N);
5300 end if;
5301
5302 -- Replace instance node for library-level instantiations of
5303 -- intrinsic subprograms, for ASIS use.
5304
5305 elsif Nkind (Parent (N)) = N_Compilation_Unit then
5306 Rewrite (N, Unit (Parent (N)));
5307 Set_Unit (Parent (N), N);
5308 end if;
5309
5310 if Parent_Installed then
5311 Remove_Parent;
5312 end if;
5313
5314 Restore_Hidden_Primitives (Vis_Prims_List);
5315 Restore_Env;
5316 Env_Installed := False;
5317 Generic_Renamings.Set_Last (0);
5318 Generic_Renamings_HTable.Reset;
5319
5320 Ignore_Pragma_SPARK_Mode := Save_IPSM;
5321 SPARK_Mode := Save_SM;
5322 SPARK_Mode_Pragma := Save_SMP;
5323 end if;
5324
5325 <<Leave>>
5326 if Has_Aspects (N) then
5327 Analyze_Aspect_Specifications (N, Act_Decl_Id);
5328 end if;
5329
5330 exception
5331 when Instantiation_Error =>
5332 if Parent_Installed then
5333 Remove_Parent;
5334 end if;
5335
5336 if Env_Installed then
5337 Restore_Env;
5338 end if;
5339
5340 Ignore_Pragma_SPARK_Mode := Save_IPSM;
5341 SPARK_Mode := Save_SM;
5342 SPARK_Mode_Pragma := Save_SMP;
5343 end Analyze_Subprogram_Instantiation;
5344
5345 -------------------------
5346 -- Get_Associated_Node --
5347 -------------------------
5348
5349 function Get_Associated_Node (N : Node_Id) return Node_Id is
5350 Assoc : Node_Id;
5351
5352 begin
5353 Assoc := Associated_Node (N);
5354
5355 if Nkind (Assoc) /= Nkind (N) then
5356 return Assoc;
5357
5358 elsif Nkind_In (Assoc, N_Aggregate, N_Extension_Aggregate) then
5359 return Assoc;
5360
5361 else
5362 -- If the node is part of an inner generic, it may itself have been
5363 -- remapped into a further generic copy. Associated_Node is otherwise
5364 -- used for the entity of the node, and will be of a different node
5365 -- kind, or else N has been rewritten as a literal or function call.
5366
5367 while Present (Associated_Node (Assoc))
5368 and then Nkind (Associated_Node (Assoc)) = Nkind (Assoc)
5369 loop
5370 Assoc := Associated_Node (Assoc);
5371 end loop;
5372
5373 -- Follow and additional link in case the final node was rewritten.
5374 -- This can only happen with nested generic units.
5375
5376 if (Nkind (Assoc) = N_Identifier or else Nkind (Assoc) in N_Op)
5377 and then Present (Associated_Node (Assoc))
5378 and then (Nkind_In (Associated_Node (Assoc), N_Function_Call,
5379 N_Explicit_Dereference,
5380 N_Integer_Literal,
5381 N_Real_Literal,
5382 N_String_Literal))
5383 then
5384 Assoc := Associated_Node (Assoc);
5385 end if;
5386
5387 -- An additional special case: an unconstrained type in an object
5388 -- declaration may have been rewritten as a local subtype constrained
5389 -- by the expression in the declaration. We need to recover the
5390 -- original entity which may be global.
5391
5392 if Present (Original_Node (Assoc))
5393 and then Nkind (Parent (N)) = N_Object_Declaration
5394 then
5395 Assoc := Original_Node (Assoc);
5396 end if;
5397
5398 return Assoc;
5399 end if;
5400 end Get_Associated_Node;
5401
5402 ----------------------------
5403 -- Build_Function_Wrapper --
5404 ----------------------------
5405
5406 function Build_Function_Wrapper
5407 (Formal_Subp : Entity_Id;
5408 Actual_Subp : Entity_Id) return Node_Id
5409 is
5410 Loc : constant Source_Ptr := Sloc (Current_Scope);
5411 Ret_Type : constant Entity_Id := Get_Instance_Of (Etype (Formal_Subp));
5412 Actuals : List_Id;
5413 Decl : Node_Id;
5414 Func_Name : Node_Id;
5415 Func : Entity_Id;
5416 Parm_Type : Node_Id;
5417 Profile : List_Id := New_List;
5418 Spec : Node_Id;
5419 Act_F : Entity_Id;
5420 Form_F : Entity_Id;
5421 New_F : Entity_Id;
5422
5423 begin
5424 Func_Name := New_Occurrence_Of (Actual_Subp, Loc);
5425
5426 Func := Make_Defining_Identifier (Loc, Chars (Formal_Subp));
5427 Set_Ekind (Func, E_Function);
5428 Set_Is_Generic_Actual_Subprogram (Func);
5429
5430 Actuals := New_List;
5431 Profile := New_List;
5432
5433 Act_F := First_Formal (Actual_Subp);
5434 Form_F := First_Formal (Formal_Subp);
5435 while Present (Form_F) loop
5436
5437 -- Create new formal for profile of wrapper, and add a reference
5438 -- to it in the list of actuals for the enclosing call. The name
5439 -- must be that of the formal in the formal subprogram, because
5440 -- calls to it in the generic body may use named associations.
5441
5442 New_F := Make_Defining_Identifier (Loc, Chars (Form_F));
5443
5444 Parm_Type :=
5445 New_Occurrence_Of (Get_Instance_Of (Etype (Form_F)), Loc);
5446
5447 Append_To (Profile,
5448 Make_Parameter_Specification (Loc,
5449 Defining_Identifier => New_F,
5450 Parameter_Type => Parm_Type));
5451
5452 Append_To (Actuals, New_Occurrence_Of (New_F, Loc));
5453 Next_Formal (Form_F);
5454
5455 if Present (Act_F) then
5456 Next_Formal (Act_F);
5457 end if;
5458 end loop;
5459
5460 Spec :=
5461 Make_Function_Specification (Loc,
5462 Defining_Unit_Name => Func,
5463 Parameter_Specifications => Profile,
5464 Result_Definition => New_Occurrence_Of (Ret_Type, Loc));
5465
5466 Decl :=
5467 Make_Expression_Function (Loc,
5468 Specification => Spec,
5469 Expression =>
5470 Make_Function_Call (Loc,
5471 Name => Func_Name,
5472 Parameter_Associations => Actuals));
5473
5474 return Decl;
5475 end Build_Function_Wrapper;
5476
5477 ----------------------------
5478 -- Build_Operator_Wrapper --
5479 ----------------------------
5480
5481 function Build_Operator_Wrapper
5482 (Formal_Subp : Entity_Id;
5483 Actual_Subp : Entity_Id) return Node_Id
5484 is
5485 Loc : constant Source_Ptr := Sloc (Current_Scope);
5486 Ret_Type : constant Entity_Id :=
5487 Get_Instance_Of (Etype (Formal_Subp));
5488 Op_Type : constant Entity_Id :=
5489 Get_Instance_Of (Etype (First_Formal (Formal_Subp)));
5490 Is_Binary : constant Boolean :=
5491 Present (Next_Formal (First_Formal (Formal_Subp)));
5492
5493 Decl : Node_Id;
5494 Expr : Node_Id;
5495 F1, F2 : Entity_Id;
5496 Func : Entity_Id;
5497 Op_Name : Name_Id;
5498 Spec : Node_Id;
5499 L, R : Node_Id;
5500
5501 begin
5502 Op_Name := Chars (Actual_Subp);
5503
5504 -- Create entities for wrapper function and its formals
5505
5506 F1 := Make_Temporary (Loc, 'A');
5507 F2 := Make_Temporary (Loc, 'B');
5508 L := New_Occurrence_Of (F1, Loc);
5509 R := New_Occurrence_Of (F2, Loc);
5510
5511 Func := Make_Defining_Identifier (Loc, Chars (Formal_Subp));
5512 Set_Ekind (Func, E_Function);
5513 Set_Is_Generic_Actual_Subprogram (Func);
5514
5515 Spec :=
5516 Make_Function_Specification (Loc,
5517 Defining_Unit_Name => Func,
5518 Parameter_Specifications => New_List (
5519 Make_Parameter_Specification (Loc,
5520 Defining_Identifier => F1,
5521 Parameter_Type => New_Occurrence_Of (Op_Type, Loc))),
5522 Result_Definition => New_Occurrence_Of (Ret_Type, Loc));
5523
5524 if Is_Binary then
5525 Append_To (Parameter_Specifications (Spec),
5526 Make_Parameter_Specification (Loc,
5527 Defining_Identifier => F2,
5528 Parameter_Type => New_Occurrence_Of (Op_Type, Loc)));
5529 end if;
5530
5531 -- Build expression as a function call, or as an operator node
5532 -- that corresponds to the name of the actual, starting with
5533 -- binary operators.
5534
5535 if Op_Name not in Any_Operator_Name then
5536 Expr :=
5537 Make_Function_Call (Loc,
5538 Name =>
5539 New_Occurrence_Of (Actual_Subp, Loc),
5540 Parameter_Associations => New_List (L));
5541
5542 if Is_Binary then
5543 Append_To (Parameter_Associations (Expr), R);
5544 end if;
5545
5546 -- Binary operators
5547
5548 elsif Is_Binary then
5549 if Op_Name = Name_Op_And then
5550 Expr := Make_Op_And (Loc, Left_Opnd => L, Right_Opnd => R);
5551 elsif Op_Name = Name_Op_Or then
5552 Expr := Make_Op_Or (Loc, Left_Opnd => L, Right_Opnd => R);
5553 elsif Op_Name = Name_Op_Xor then
5554 Expr := Make_Op_Xor (Loc, Left_Opnd => L, Right_Opnd => R);
5555 elsif Op_Name = Name_Op_Eq then
5556 Expr := Make_Op_Eq (Loc, Left_Opnd => L, Right_Opnd => R);
5557 elsif Op_Name = Name_Op_Ne then
5558 Expr := Make_Op_Ne (Loc, Left_Opnd => L, Right_Opnd => R);
5559 elsif Op_Name = Name_Op_Le then
5560 Expr := Make_Op_Le (Loc, Left_Opnd => L, Right_Opnd => R);
5561 elsif Op_Name = Name_Op_Gt then
5562 Expr := Make_Op_Gt (Loc, Left_Opnd => L, Right_Opnd => R);
5563 elsif Op_Name = Name_Op_Ge then
5564 Expr := Make_Op_Ge (Loc, Left_Opnd => L, Right_Opnd => R);
5565 elsif Op_Name = Name_Op_Lt then
5566 Expr := Make_Op_Lt (Loc, Left_Opnd => L, Right_Opnd => R);
5567 elsif Op_Name = Name_Op_Add then
5568 Expr := Make_Op_Add (Loc, Left_Opnd => L, Right_Opnd => R);
5569 elsif Op_Name = Name_Op_Subtract then
5570 Expr := Make_Op_Subtract (Loc, Left_Opnd => L, Right_Opnd => R);
5571 elsif Op_Name = Name_Op_Concat then
5572 Expr := Make_Op_Concat (Loc, Left_Opnd => L, Right_Opnd => R);
5573 elsif Op_Name = Name_Op_Multiply then
5574 Expr := Make_Op_Multiply (Loc, Left_Opnd => L, Right_Opnd => R);
5575 elsif Op_Name = Name_Op_Divide then
5576 Expr := Make_Op_Divide (Loc, Left_Opnd => L, Right_Opnd => R);
5577 elsif Op_Name = Name_Op_Mod then
5578 Expr := Make_Op_Mod (Loc, Left_Opnd => L, Right_Opnd => R);
5579 elsif Op_Name = Name_Op_Rem then
5580 Expr := Make_Op_Rem (Loc, Left_Opnd => L, Right_Opnd => R);
5581 elsif Op_Name = Name_Op_Expon then
5582 Expr := Make_Op_Expon (Loc, Left_Opnd => L, Right_Opnd => R);
5583 end if;
5584
5585 -- Unary operators
5586
5587 else
5588 if Op_Name = Name_Op_Add then
5589 Expr := Make_Op_Plus (Loc, Right_Opnd => L);
5590 elsif Op_Name = Name_Op_Subtract then
5591 Expr := Make_Op_Minus (Loc, Right_Opnd => L);
5592 elsif Op_Name = Name_Op_Abs then
5593 Expr := Make_Op_Abs (Loc, Right_Opnd => L);
5594 elsif Op_Name = Name_Op_Not then
5595 Expr := Make_Op_Not (Loc, Right_Opnd => L);
5596 end if;
5597 end if;
5598
5599 Decl :=
5600 Make_Expression_Function (Loc,
5601 Specification => Spec,
5602 Expression => Expr);
5603
5604 return Decl;
5605 end Build_Operator_Wrapper;
5606
5607 -------------------------------------------
5608 -- Build_Instance_Compilation_Unit_Nodes --
5609 -------------------------------------------
5610
5611 procedure Build_Instance_Compilation_Unit_Nodes
5612 (N : Node_Id;
5613 Act_Body : Node_Id;
5614 Act_Decl : Node_Id)
5615 is
5616 Decl_Cunit : Node_Id;
5617 Body_Cunit : Node_Id;
5618 Citem : Node_Id;
5619 New_Main : constant Entity_Id := Defining_Entity (Act_Decl);
5620 Old_Main : constant Entity_Id := Cunit_Entity (Main_Unit);
5621
5622 begin
5623 -- A new compilation unit node is built for the instance declaration
5624
5625 Decl_Cunit :=
5626 Make_Compilation_Unit (Sloc (N),
5627 Context_Items => Empty_List,
5628 Unit => Act_Decl,
5629 Aux_Decls_Node => Make_Compilation_Unit_Aux (Sloc (N)));
5630
5631 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
5632
5633 -- The new compilation unit is linked to its body, but both share the
5634 -- same file, so we do not set Body_Required on the new unit so as not
5635 -- to create a spurious dependency on a non-existent body in the ali.
5636 -- This simplifies CodePeer unit traversal.
5637
5638 -- We use the original instantiation compilation unit as the resulting
5639 -- compilation unit of the instance, since this is the main unit.
5640
5641 Rewrite (N, Act_Body);
5642
5643 -- Propagate the aspect specifications from the package body template to
5644 -- the instantiated version of the package body.
5645
5646 if Has_Aspects (Act_Body) then
5647 Set_Aspect_Specifications
5648 (N, New_Copy_List_Tree (Aspect_Specifications (Act_Body)));
5649 end if;
5650
5651 Body_Cunit := Parent (N);
5652
5653 -- The two compilation unit nodes are linked by the Library_Unit field
5654
5655 Set_Library_Unit (Decl_Cunit, Body_Cunit);
5656 Set_Library_Unit (Body_Cunit, Decl_Cunit);
5657
5658 -- Preserve the private nature of the package if needed
5659
5660 Set_Private_Present (Decl_Cunit, Private_Present (Body_Cunit));
5661
5662 -- If the instance is not the main unit, its context, categorization
5663 -- and elaboration entity are not relevant to the compilation.
5664
5665 if Body_Cunit /= Cunit (Main_Unit) then
5666 Make_Instance_Unit (Body_Cunit, In_Main => False);
5667 return;
5668 end if;
5669
5670 -- The context clause items on the instantiation, which are now attached
5671 -- to the body compilation unit (since the body overwrote the original
5672 -- instantiation node), semantically belong on the spec, so copy them
5673 -- there. It's harmless to leave them on the body as well. In fact one
5674 -- could argue that they belong in both places.
5675
5676 Citem := First (Context_Items (Body_Cunit));
5677 while Present (Citem) loop
5678 Append (New_Copy (Citem), Context_Items (Decl_Cunit));
5679 Next (Citem);
5680 end loop;
5681
5682 -- Propagate categorization flags on packages, so that they appear in
5683 -- the ali file for the spec of the unit.
5684
5685 if Ekind (New_Main) = E_Package then
5686 Set_Is_Pure (Old_Main, Is_Pure (New_Main));
5687 Set_Is_Preelaborated (Old_Main, Is_Preelaborated (New_Main));
5688 Set_Is_Remote_Types (Old_Main, Is_Remote_Types (New_Main));
5689 Set_Is_Shared_Passive (Old_Main, Is_Shared_Passive (New_Main));
5690 Set_Is_Remote_Call_Interface
5691 (Old_Main, Is_Remote_Call_Interface (New_Main));
5692 end if;
5693
5694 -- Make entry in Units table, so that binder can generate call to
5695 -- elaboration procedure for body, if any.
5696
5697 Make_Instance_Unit (Body_Cunit, In_Main => True);
5698 Main_Unit_Entity := New_Main;
5699 Set_Cunit_Entity (Main_Unit, Main_Unit_Entity);
5700
5701 -- Build elaboration entity, since the instance may certainly generate
5702 -- elaboration code requiring a flag for protection.
5703
5704 Build_Elaboration_Entity (Decl_Cunit, New_Main);
5705 end Build_Instance_Compilation_Unit_Nodes;
5706
5707 -----------------------------
5708 -- Check_Access_Definition --
5709 -----------------------------
5710
5711 procedure Check_Access_Definition (N : Node_Id) is
5712 begin
5713 pragma Assert
5714 (Ada_Version >= Ada_2005 and then Present (Access_Definition (N)));
5715 null;
5716 end Check_Access_Definition;
5717
5718 -----------------------------------
5719 -- Check_Formal_Package_Instance --
5720 -----------------------------------
5721
5722 -- If the formal has specific parameters, they must match those of the
5723 -- actual. Both of them are instances, and the renaming declarations for
5724 -- their formal parameters appear in the same order in both. The analyzed
5725 -- formal has been analyzed in the context of the current instance.
5726
5727 procedure Check_Formal_Package_Instance
5728 (Formal_Pack : Entity_Id;
5729 Actual_Pack : Entity_Id)
5730 is
5731 E1 : Entity_Id := First_Entity (Actual_Pack);
5732 E2 : Entity_Id := First_Entity (Formal_Pack);
5733
5734 Expr1 : Node_Id;
5735 Expr2 : Node_Id;
5736
5737 procedure Check_Mismatch (B : Boolean);
5738 -- Common error routine for mismatch between the parameters of the
5739 -- actual instance and those of the formal package.
5740
5741 function Same_Instantiated_Constant (E1, E2 : Entity_Id) return Boolean;
5742 -- The formal may come from a nested formal package, and the actual may
5743 -- have been constant-folded. To determine whether the two denote the
5744 -- same entity we may have to traverse several definitions to recover
5745 -- the ultimate entity that they refer to.
5746
5747 function Same_Instantiated_Function (E1, E2 : Entity_Id) return Boolean;
5748 -- The formal and the actual must be identical, but if both are
5749 -- given by attributes they end up renaming different generated bodies,
5750 -- and we must verify that the attributes themselves match.
5751
5752 function Same_Instantiated_Variable (E1, E2 : Entity_Id) return Boolean;
5753 -- Similarly, if the formal comes from a nested formal package, the
5754 -- actual may designate the formal through multiple renamings, which
5755 -- have to be followed to determine the original variable in question.
5756
5757 --------------------
5758 -- Check_Mismatch --
5759 --------------------
5760
5761 procedure Check_Mismatch (B : Boolean) is
5762 -- A Formal_Type_Declaration for a derived private type is rewritten
5763 -- as a private extension decl. (see Analyze_Formal_Derived_Type),
5764 -- which is why we examine the original node.
5765
5766 Kind : constant Node_Kind := Nkind (Original_Node (Parent (E2)));
5767
5768 begin
5769 if Kind = N_Formal_Type_Declaration then
5770 return;
5771
5772 elsif Nkind_In (Kind, N_Formal_Object_Declaration,
5773 N_Formal_Package_Declaration)
5774 or else Kind in N_Formal_Subprogram_Declaration
5775 then
5776 null;
5777
5778 -- Ada 2012: If both formal and actual are incomplete types they
5779 -- are conformant.
5780
5781 elsif Is_Incomplete_Type (E1) and then Is_Incomplete_Type (E2) then
5782 null;
5783
5784 elsif B then
5785 Error_Msg_NE
5786 ("actual for & in actual instance does not match formal",
5787 Parent (Actual_Pack), E1);
5788 end if;
5789 end Check_Mismatch;
5790
5791 --------------------------------
5792 -- Same_Instantiated_Constant --
5793 --------------------------------
5794
5795 function Same_Instantiated_Constant
5796 (E1, E2 : Entity_Id) return Boolean
5797 is
5798 Ent : Entity_Id;
5799
5800 begin
5801 Ent := E2;
5802 while Present (Ent) loop
5803 if E1 = Ent then
5804 return True;
5805
5806 elsif Ekind (Ent) /= E_Constant then
5807 return False;
5808
5809 elsif Is_Entity_Name (Constant_Value (Ent)) then
5810 if Entity (Constant_Value (Ent)) = E1 then
5811 return True;
5812 else
5813 Ent := Entity (Constant_Value (Ent));
5814 end if;
5815
5816 -- The actual may be a constant that has been folded. Recover
5817 -- original name.
5818
5819 elsif Is_Entity_Name (Original_Node (Constant_Value (Ent))) then
5820 Ent := Entity (Original_Node (Constant_Value (Ent)));
5821
5822 else
5823 return False;
5824 end if;
5825 end loop;
5826
5827 return False;
5828 end Same_Instantiated_Constant;
5829
5830 --------------------------------
5831 -- Same_Instantiated_Function --
5832 --------------------------------
5833
5834 function Same_Instantiated_Function
5835 (E1, E2 : Entity_Id) return Boolean
5836 is
5837 U1, U2 : Node_Id;
5838 begin
5839 if Alias (E1) = Alias (E2) then
5840 return True;
5841
5842 elsif Present (Alias (E2)) then
5843 U1 := Original_Node (Unit_Declaration_Node (E1));
5844 U2 := Original_Node (Unit_Declaration_Node (Alias (E2)));
5845
5846 return Nkind (U1) = N_Subprogram_Renaming_Declaration
5847 and then Nkind (Name (U1)) = N_Attribute_Reference
5848
5849 and then Nkind (U2) = N_Subprogram_Renaming_Declaration
5850 and then Nkind (Name (U2)) = N_Attribute_Reference
5851
5852 and then
5853 Attribute_Name (Name (U1)) = Attribute_Name (Name (U2));
5854 else
5855 return False;
5856 end if;
5857 end Same_Instantiated_Function;
5858
5859 --------------------------------
5860 -- Same_Instantiated_Variable --
5861 --------------------------------
5862
5863 function Same_Instantiated_Variable
5864 (E1, E2 : Entity_Id) return Boolean
5865 is
5866 function Original_Entity (E : Entity_Id) return Entity_Id;
5867 -- Follow chain of renamings to the ultimate ancestor
5868
5869 ---------------------
5870 -- Original_Entity --
5871 ---------------------
5872
5873 function Original_Entity (E : Entity_Id) return Entity_Id is
5874 Orig : Entity_Id;
5875
5876 begin
5877 Orig := E;
5878 while Nkind (Parent (Orig)) = N_Object_Renaming_Declaration
5879 and then Present (Renamed_Object (Orig))
5880 and then Is_Entity_Name (Renamed_Object (Orig))
5881 loop
5882 Orig := Entity (Renamed_Object (Orig));
5883 end loop;
5884
5885 return Orig;
5886 end Original_Entity;
5887
5888 -- Start of processing for Same_Instantiated_Variable
5889
5890 begin
5891 return Ekind (E1) = Ekind (E2)
5892 and then Original_Entity (E1) = Original_Entity (E2);
5893 end Same_Instantiated_Variable;
5894
5895 -- Start of processing for Check_Formal_Package_Instance
5896
5897 begin
5898 while Present (E1) and then Present (E2) loop
5899 exit when Ekind (E1) = E_Package
5900 and then Renamed_Entity (E1) = Renamed_Entity (Actual_Pack);
5901
5902 -- If the formal is the renaming of the formal package, this
5903 -- is the end of its formal part, which may occur before the
5904 -- end of the formal part in the actual in the presence of
5905 -- defaulted parameters in the formal package.
5906
5907 exit when Nkind (Parent (E2)) = N_Package_Renaming_Declaration
5908 and then Renamed_Entity (E2) = Scope (E2);
5909
5910 -- The analysis of the actual may generate additional internal
5911 -- entities. If the formal is defaulted, there is no corresponding
5912 -- analysis and the internal entities must be skipped, until we
5913 -- find corresponding entities again.
5914
5915 if Comes_From_Source (E2)
5916 and then not Comes_From_Source (E1)
5917 and then Chars (E1) /= Chars (E2)
5918 then
5919 while Present (E1) and then Chars (E1) /= Chars (E2) loop
5920 Next_Entity (E1);
5921 end loop;
5922 end if;
5923
5924 if No (E1) then
5925 return;
5926
5927 -- If the formal entity comes from a formal declaration, it was
5928 -- defaulted in the formal package, and no check is needed on it.
5929
5930 elsif Nkind_In (Original_Node (Parent (E2)),
5931 N_Formal_Object_Declaration,
5932 N_Formal_Type_Declaration)
5933 then
5934 goto Next_E;
5935
5936 -- Ditto for defaulted formal subprograms.
5937
5938 elsif Is_Overloadable (E1)
5939 and then Nkind (Unit_Declaration_Node (E2)) in
5940 N_Formal_Subprogram_Declaration
5941 then
5942 goto Next_E;
5943
5944 elsif Is_Type (E1) then
5945
5946 -- Subtypes must statically match. E1, E2 are the local entities
5947 -- that are subtypes of the actuals. Itypes generated for other
5948 -- parameters need not be checked, the check will be performed
5949 -- on the parameters themselves.
5950
5951 -- If E2 is a formal type declaration, it is a defaulted parameter
5952 -- and needs no checking.
5953
5954 if not Is_Itype (E1) and then not Is_Itype (E2) then
5955 Check_Mismatch
5956 (not Is_Type (E2)
5957 or else Etype (E1) /= Etype (E2)
5958 or else not Subtypes_Statically_Match (E1, E2));
5959 end if;
5960
5961 elsif Ekind (E1) = E_Constant then
5962
5963 -- IN parameters must denote the same static value, or the same
5964 -- constant, or the literal null.
5965
5966 Expr1 := Expression (Parent (E1));
5967
5968 if Ekind (E2) /= E_Constant then
5969 Check_Mismatch (True);
5970 goto Next_E;
5971 else
5972 Expr2 := Expression (Parent (E2));
5973 end if;
5974
5975 if Is_OK_Static_Expression (Expr1) then
5976 if not Is_OK_Static_Expression (Expr2) then
5977 Check_Mismatch (True);
5978
5979 elsif Is_Discrete_Type (Etype (E1)) then
5980 declare
5981 V1 : constant Uint := Expr_Value (Expr1);
5982 V2 : constant Uint := Expr_Value (Expr2);
5983 begin
5984 Check_Mismatch (V1 /= V2);
5985 end;
5986
5987 elsif Is_Real_Type (Etype (E1)) then
5988 declare
5989 V1 : constant Ureal := Expr_Value_R (Expr1);
5990 V2 : constant Ureal := Expr_Value_R (Expr2);
5991 begin
5992 Check_Mismatch (V1 /= V2);
5993 end;
5994
5995 elsif Is_String_Type (Etype (E1))
5996 and then Nkind (Expr1) = N_String_Literal
5997 then
5998 if Nkind (Expr2) /= N_String_Literal then
5999 Check_Mismatch (True);
6000 else
6001 Check_Mismatch
6002 (not String_Equal (Strval (Expr1), Strval (Expr2)));
6003 end if;
6004 end if;
6005
6006 elsif Is_Entity_Name (Expr1) then
6007 if Is_Entity_Name (Expr2) then
6008 if Entity (Expr1) = Entity (Expr2) then
6009 null;
6010 else
6011 Check_Mismatch
6012 (not Same_Instantiated_Constant
6013 (Entity (Expr1), Entity (Expr2)));
6014 end if;
6015
6016 else
6017 Check_Mismatch (True);
6018 end if;
6019
6020 elsif Is_Entity_Name (Original_Node (Expr1))
6021 and then Is_Entity_Name (Expr2)
6022 and then Same_Instantiated_Constant
6023 (Entity (Original_Node (Expr1)), Entity (Expr2))
6024 then
6025 null;
6026
6027 elsif Nkind (Expr1) = N_Null then
6028 Check_Mismatch (Nkind (Expr1) /= N_Null);
6029
6030 else
6031 Check_Mismatch (True);
6032 end if;
6033
6034 elsif Ekind (E1) = E_Variable then
6035 Check_Mismatch (not Same_Instantiated_Variable (E1, E2));
6036
6037 elsif Ekind (E1) = E_Package then
6038 Check_Mismatch
6039 (Ekind (E1) /= Ekind (E2)
6040 or else Renamed_Object (E1) /= Renamed_Object (E2));
6041
6042 elsif Is_Overloadable (E1) then
6043
6044 -- Verify that the actual subprograms match. Note that actuals
6045 -- that are attributes are rewritten as subprograms. If the
6046 -- subprogram in the formal package is defaulted, no check is
6047 -- needed. Note that this can only happen in Ada 2005 when the
6048 -- formal package can be partially parameterized.
6049
6050 if Nkind (Unit_Declaration_Node (E1)) =
6051 N_Subprogram_Renaming_Declaration
6052 and then From_Default (Unit_Declaration_Node (E1))
6053 then
6054 null;
6055
6056 -- If the formal package has an "others" box association that
6057 -- covers this formal, there is no need for a check either.
6058
6059 elsif Nkind (Unit_Declaration_Node (E2)) in
6060 N_Formal_Subprogram_Declaration
6061 and then Box_Present (Unit_Declaration_Node (E2))
6062 then
6063 null;
6064
6065 -- No check needed if subprogram is a defaulted null procedure
6066
6067 elsif No (Alias (E2))
6068 and then Ekind (E2) = E_Procedure
6069 and then
6070 Null_Present (Specification (Unit_Declaration_Node (E2)))
6071 then
6072 null;
6073
6074 -- Otherwise the actual in the formal and the actual in the
6075 -- instantiation of the formal must match, up to renamings.
6076
6077 else
6078 Check_Mismatch
6079 (Ekind (E2) /= Ekind (E1)
6080 or else not Same_Instantiated_Function (E1, E2));
6081 end if;
6082
6083 else
6084 raise Program_Error;
6085 end if;
6086
6087 <<Next_E>>
6088 Next_Entity (E1);
6089 Next_Entity (E2);
6090 end loop;
6091 end Check_Formal_Package_Instance;
6092
6093 ---------------------------
6094 -- Check_Formal_Packages --
6095 ---------------------------
6096
6097 procedure Check_Formal_Packages (P_Id : Entity_Id) is
6098 E : Entity_Id;
6099 Formal_P : Entity_Id;
6100 Formal_Decl : Node_Id;
6101
6102 begin
6103 -- Iterate through the declarations in the instance, looking for package
6104 -- renaming declarations that denote instances of formal packages. Stop
6105 -- when we find the renaming of the current package itself. The
6106 -- declaration for a formal package without a box is followed by an
6107 -- internal entity that repeats the instantiation.
6108
6109 E := First_Entity (P_Id);
6110 while Present (E) loop
6111 if Ekind (E) = E_Package then
6112 if Renamed_Object (E) = P_Id then
6113 exit;
6114
6115 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
6116 null;
6117
6118 else
6119 Formal_Decl := Parent (Associated_Formal_Package (E));
6120
6121 -- Nothing to check if the formal has a box or an others_clause
6122 -- (necessarily with a box).
6123
6124 if Box_Present (Formal_Decl) then
6125 null;
6126
6127 elsif Nkind (First (Generic_Associations (Formal_Decl))) =
6128 N_Others_Choice
6129 then
6130 -- The internal validating package was generated but formal
6131 -- and instance are known to be compatible.
6132
6133 Formal_P := Next_Entity (E);
6134 Remove (Unit_Declaration_Node (Formal_P));
6135
6136 else
6137 Formal_P := Next_Entity (E);
6138 Check_Formal_Package_Instance (Formal_P, E);
6139
6140 -- After checking, remove the internal validating package.
6141 -- It is only needed for semantic checks, and as it may
6142 -- contain generic formal declarations it should not reach
6143 -- gigi.
6144
6145 Remove (Unit_Declaration_Node (Formal_P));
6146 end if;
6147 end if;
6148 end if;
6149
6150 Next_Entity (E);
6151 end loop;
6152 end Check_Formal_Packages;
6153
6154 ---------------------------------
6155 -- Check_Forward_Instantiation --
6156 ---------------------------------
6157
6158 procedure Check_Forward_Instantiation (Decl : Node_Id) is
6159 S : Entity_Id;
6160 Gen_Comp : Entity_Id := Cunit_Entity (Get_Source_Unit (Decl));
6161
6162 begin
6163 -- The instantiation appears before the generic body if we are in the
6164 -- scope of the unit containing the generic, either in its spec or in
6165 -- the package body, and before the generic body.
6166
6167 if Ekind (Gen_Comp) = E_Package_Body then
6168 Gen_Comp := Spec_Entity (Gen_Comp);
6169 end if;
6170
6171 if In_Open_Scopes (Gen_Comp)
6172 and then No (Corresponding_Body (Decl))
6173 then
6174 S := Current_Scope;
6175
6176 while Present (S)
6177 and then not Is_Compilation_Unit (S)
6178 and then not Is_Child_Unit (S)
6179 loop
6180 if Ekind (S) = E_Package then
6181 Set_Has_Forward_Instantiation (S);
6182 end if;
6183
6184 S := Scope (S);
6185 end loop;
6186 end if;
6187 end Check_Forward_Instantiation;
6188
6189 ---------------------------
6190 -- Check_Generic_Actuals --
6191 ---------------------------
6192
6193 -- The visibility of the actuals may be different between the point of
6194 -- generic instantiation and the instantiation of the body.
6195
6196 procedure Check_Generic_Actuals
6197 (Instance : Entity_Id;
6198 Is_Formal_Box : Boolean)
6199 is
6200 E : Entity_Id;
6201 Astype : Entity_Id;
6202
6203 function Denotes_Previous_Actual (Typ : Entity_Id) return Boolean;
6204 -- For a formal that is an array type, the component type is often a
6205 -- previous formal in the same unit. The privacy status of the component
6206 -- type will have been examined earlier in the traversal of the
6207 -- corresponding actuals, and this status should not be modified for
6208 -- the array (sub)type itself. However, if the base type of the array
6209 -- (sub)type is private, its full view must be restored in the body to
6210 -- be consistent with subsequent index subtypes, etc.
6211 --
6212 -- To detect this case we have to rescan the list of formals, which is
6213 -- usually short enough to ignore the resulting inefficiency.
6214
6215 -----------------------------
6216 -- Denotes_Previous_Actual --
6217 -----------------------------
6218
6219 function Denotes_Previous_Actual (Typ : Entity_Id) return Boolean is
6220 Prev : Entity_Id;
6221
6222 begin
6223 Prev := First_Entity (Instance);
6224 while Present (Prev) loop
6225 if Is_Type (Prev)
6226 and then Nkind (Parent (Prev)) = N_Subtype_Declaration
6227 and then Is_Entity_Name (Subtype_Indication (Parent (Prev)))
6228 and then Entity (Subtype_Indication (Parent (Prev))) = Typ
6229 then
6230 return True;
6231
6232 elsif Prev = E then
6233 return False;
6234
6235 else
6236 Next_Entity (Prev);
6237 end if;
6238 end loop;
6239
6240 return False;
6241 end Denotes_Previous_Actual;
6242
6243 -- Start of processing for Check_Generic_Actuals
6244
6245 begin
6246 E := First_Entity (Instance);
6247 while Present (E) loop
6248 if Is_Type (E)
6249 and then Nkind (Parent (E)) = N_Subtype_Declaration
6250 and then Scope (Etype (E)) /= Instance
6251 and then Is_Entity_Name (Subtype_Indication (Parent (E)))
6252 then
6253 if Is_Array_Type (E)
6254 and then not Is_Private_Type (Etype (E))
6255 and then Denotes_Previous_Actual (Component_Type (E))
6256 then
6257 null;
6258 else
6259 Check_Private_View (Subtype_Indication (Parent (E)));
6260 end if;
6261
6262 Set_Is_Generic_Actual_Type (E, True);
6263 Set_Is_Hidden (E, False);
6264 Set_Is_Potentially_Use_Visible (E,
6265 In_Use (Instance));
6266
6267 -- We constructed the generic actual type as a subtype of the
6268 -- supplied type. This means that it normally would not inherit
6269 -- subtype specific attributes of the actual, which is wrong for
6270 -- the generic case.
6271
6272 Astype := Ancestor_Subtype (E);
6273
6274 if No (Astype) then
6275
6276 -- This can happen when E is an itype that is the full view of
6277 -- a private type completed, e.g. with a constrained array. In
6278 -- that case, use the first subtype, which will carry size
6279 -- information. The base type itself is unconstrained and will
6280 -- not carry it.
6281
6282 Astype := First_Subtype (E);
6283 end if;
6284
6285 Set_Size_Info (E, (Astype));
6286 Set_RM_Size (E, RM_Size (Astype));
6287 Set_First_Rep_Item (E, First_Rep_Item (Astype));
6288
6289 if Is_Discrete_Or_Fixed_Point_Type (E) then
6290 Set_RM_Size (E, RM_Size (Astype));
6291
6292 -- In nested instances, the base type of an access actual may
6293 -- itself be private, and need to be exchanged.
6294
6295 elsif Is_Access_Type (E)
6296 and then Is_Private_Type (Etype (E))
6297 then
6298 Check_Private_View
6299 (New_Occurrence_Of (Etype (E), Sloc (Instance)));
6300 end if;
6301
6302 elsif Ekind (E) = E_Package then
6303
6304 -- If this is the renaming for the current instance, we're done.
6305 -- Otherwise it is a formal package. If the corresponding formal
6306 -- was declared with a box, the (instantiations of the) generic
6307 -- formal part are also visible. Otherwise, ignore the entity
6308 -- created to validate the actuals.
6309
6310 if Renamed_Object (E) = Instance then
6311 exit;
6312
6313 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
6314 null;
6315
6316 -- The visibility of a formal of an enclosing generic is already
6317 -- correct.
6318
6319 elsif Denotes_Formal_Package (E) then
6320 null;
6321
6322 elsif Present (Associated_Formal_Package (E))
6323 and then not Is_Generic_Formal (E)
6324 then
6325 if Box_Present (Parent (Associated_Formal_Package (E))) then
6326 Check_Generic_Actuals (Renamed_Object (E), True);
6327
6328 else
6329 Check_Generic_Actuals (Renamed_Object (E), False);
6330 end if;
6331
6332 Set_Is_Hidden (E, False);
6333 end if;
6334
6335 -- If this is a subprogram instance (in a wrapper package) the
6336 -- actual is fully visible.
6337
6338 elsif Is_Wrapper_Package (Instance) then
6339 Set_Is_Hidden (E, False);
6340
6341 -- If the formal package is declared with a box, or if the formal
6342 -- parameter is defaulted, it is visible in the body.
6343
6344 elsif Is_Formal_Box or else Is_Visible_Formal (E) then
6345 Set_Is_Hidden (E, False);
6346 end if;
6347
6348 if Ekind (E) = E_Constant then
6349
6350 -- If the type of the actual is a private type declared in the
6351 -- enclosing scope of the generic unit, the body of the generic
6352 -- sees the full view of the type (because it has to appear in
6353 -- the corresponding package body). If the type is private now,
6354 -- exchange views to restore the proper visiblity in the instance.
6355
6356 declare
6357 Typ : constant Entity_Id := Base_Type (Etype (E));
6358 -- The type of the actual
6359
6360 Gen_Id : Entity_Id;
6361 -- The generic unit
6362
6363 Parent_Scope : Entity_Id;
6364 -- The enclosing scope of the generic unit
6365
6366 begin
6367 if Is_Wrapper_Package (Instance) then
6368 Gen_Id :=
6369 Generic_Parent
6370 (Specification
6371 (Unit_Declaration_Node
6372 (Related_Instance (Instance))));
6373 else
6374 Gen_Id :=
6375 Generic_Parent (Package_Specification (Instance));
6376 end if;
6377
6378 Parent_Scope := Scope (Gen_Id);
6379
6380 -- The exchange is only needed if the generic is defined
6381 -- within a package which is not a common ancestor of the
6382 -- scope of the instance, and is not already in scope.
6383
6384 if Is_Private_Type (Typ)
6385 and then Scope (Typ) = Parent_Scope
6386 and then Scope (Instance) /= Parent_Scope
6387 and then Ekind (Parent_Scope) = E_Package
6388 and then not Is_Child_Unit (Gen_Id)
6389 then
6390 Switch_View (Typ);
6391
6392 -- If the type of the entity is a subtype, it may also have
6393 -- to be made visible, together with the base type of its
6394 -- full view, after exchange.
6395
6396 if Is_Private_Type (Etype (E)) then
6397 Switch_View (Etype (E));
6398 Switch_View (Base_Type (Etype (E)));
6399 end if;
6400 end if;
6401 end;
6402 end if;
6403
6404 Next_Entity (E);
6405 end loop;
6406 end Check_Generic_Actuals;
6407
6408 ------------------------------
6409 -- Check_Generic_Child_Unit --
6410 ------------------------------
6411
6412 procedure Check_Generic_Child_Unit
6413 (Gen_Id : Node_Id;
6414 Parent_Installed : in out Boolean)
6415 is
6416 Loc : constant Source_Ptr := Sloc (Gen_Id);
6417 Gen_Par : Entity_Id := Empty;
6418 E : Entity_Id;
6419 Inst_Par : Entity_Id;
6420 S : Node_Id;
6421
6422 function Find_Generic_Child
6423 (Scop : Entity_Id;
6424 Id : Node_Id) return Entity_Id;
6425 -- Search generic parent for possible child unit with the given name
6426
6427 function In_Enclosing_Instance return Boolean;
6428 -- Within an instance of the parent, the child unit may be denoted by
6429 -- a simple name, or an abbreviated expanded name. Examine enclosing
6430 -- scopes to locate a possible parent instantiation.
6431
6432 ------------------------
6433 -- Find_Generic_Child --
6434 ------------------------
6435
6436 function Find_Generic_Child
6437 (Scop : Entity_Id;
6438 Id : Node_Id) return Entity_Id
6439 is
6440 E : Entity_Id;
6441
6442 begin
6443 -- If entity of name is already set, instance has already been
6444 -- resolved, e.g. in an enclosing instantiation.
6445
6446 if Present (Entity (Id)) then
6447 if Scope (Entity (Id)) = Scop then
6448 return Entity (Id);
6449 else
6450 return Empty;
6451 end if;
6452
6453 else
6454 E := First_Entity (Scop);
6455 while Present (E) loop
6456 if Chars (E) = Chars (Id)
6457 and then Is_Child_Unit (E)
6458 then
6459 if Is_Child_Unit (E)
6460 and then not Is_Visible_Lib_Unit (E)
6461 then
6462 Error_Msg_NE
6463 ("generic child unit& is not visible", Gen_Id, E);
6464 end if;
6465
6466 Set_Entity (Id, E);
6467 return E;
6468 end if;
6469
6470 Next_Entity (E);
6471 end loop;
6472
6473 return Empty;
6474 end if;
6475 end Find_Generic_Child;
6476
6477 ---------------------------
6478 -- In_Enclosing_Instance --
6479 ---------------------------
6480
6481 function In_Enclosing_Instance return Boolean is
6482 Enclosing_Instance : Node_Id;
6483 Instance_Decl : Node_Id;
6484
6485 begin
6486 -- We do not inline any call that contains instantiations, except
6487 -- for instantiations of Unchecked_Conversion, so if we are within
6488 -- an inlined body the current instance does not require parents.
6489
6490 if In_Inlined_Body then
6491 pragma Assert (Chars (Gen_Id) = Name_Unchecked_Conversion);
6492 return False;
6493 end if;
6494
6495 -- Loop to check enclosing scopes
6496
6497 Enclosing_Instance := Current_Scope;
6498 while Present (Enclosing_Instance) loop
6499 Instance_Decl := Unit_Declaration_Node (Enclosing_Instance);
6500
6501 if Ekind (Enclosing_Instance) = E_Package
6502 and then Is_Generic_Instance (Enclosing_Instance)
6503 and then Present
6504 (Generic_Parent (Specification (Instance_Decl)))
6505 then
6506 -- Check whether the generic we are looking for is a child of
6507 -- this instance.
6508
6509 E := Find_Generic_Child
6510 (Generic_Parent (Specification (Instance_Decl)), Gen_Id);
6511 exit when Present (E);
6512
6513 else
6514 E := Empty;
6515 end if;
6516
6517 Enclosing_Instance := Scope (Enclosing_Instance);
6518 end loop;
6519
6520 if No (E) then
6521
6522 -- Not a child unit
6523
6524 Analyze (Gen_Id);
6525 return False;
6526
6527 else
6528 Rewrite (Gen_Id,
6529 Make_Expanded_Name (Loc,
6530 Chars => Chars (E),
6531 Prefix => New_Occurrence_Of (Enclosing_Instance, Loc),
6532 Selector_Name => New_Occurrence_Of (E, Loc)));
6533
6534 Set_Entity (Gen_Id, E);
6535 Set_Etype (Gen_Id, Etype (E));
6536 Parent_Installed := False; -- Already in scope.
6537 return True;
6538 end if;
6539 end In_Enclosing_Instance;
6540
6541 -- Start of processing for Check_Generic_Child_Unit
6542
6543 begin
6544 -- If the name of the generic is given by a selected component, it may
6545 -- be the name of a generic child unit, and the prefix is the name of an
6546 -- instance of the parent, in which case the child unit must be visible.
6547 -- If this instance is not in scope, it must be placed there and removed
6548 -- after instantiation, because what is being instantiated is not the
6549 -- original child, but the corresponding child present in the instance
6550 -- of the parent.
6551
6552 -- If the child is instantiated within the parent, it can be given by
6553 -- a simple name. In this case the instance is already in scope, but
6554 -- the child generic must be recovered from the generic parent as well.
6555
6556 if Nkind (Gen_Id) = N_Selected_Component then
6557 S := Selector_Name (Gen_Id);
6558 Analyze (Prefix (Gen_Id));
6559 Inst_Par := Entity (Prefix (Gen_Id));
6560
6561 if Ekind (Inst_Par) = E_Package
6562 and then Present (Renamed_Object (Inst_Par))
6563 then
6564 Inst_Par := Renamed_Object (Inst_Par);
6565 end if;
6566
6567 if Ekind (Inst_Par) = E_Package then
6568 if Nkind (Parent (Inst_Par)) = N_Package_Specification then
6569 Gen_Par := Generic_Parent (Parent (Inst_Par));
6570
6571 elsif Nkind (Parent (Inst_Par)) = N_Defining_Program_Unit_Name
6572 and then
6573 Nkind (Parent (Parent (Inst_Par))) = N_Package_Specification
6574 then
6575 Gen_Par := Generic_Parent (Parent (Parent (Inst_Par)));
6576 end if;
6577
6578 elsif Ekind (Inst_Par) = E_Generic_Package
6579 and then Nkind (Parent (Gen_Id)) = N_Formal_Package_Declaration
6580 then
6581 -- A formal package may be a real child package, and not the
6582 -- implicit instance within a parent. In this case the child is
6583 -- not visible and has to be retrieved explicitly as well.
6584
6585 Gen_Par := Inst_Par;
6586 end if;
6587
6588 if Present (Gen_Par) then
6589
6590 -- The prefix denotes an instantiation. The entity itself may be a
6591 -- nested generic, or a child unit.
6592
6593 E := Find_Generic_Child (Gen_Par, S);
6594
6595 if Present (E) then
6596 Change_Selected_Component_To_Expanded_Name (Gen_Id);
6597 Set_Entity (Gen_Id, E);
6598 Set_Etype (Gen_Id, Etype (E));
6599 Set_Entity (S, E);
6600 Set_Etype (S, Etype (E));
6601
6602 -- Indicate that this is a reference to the parent
6603
6604 if In_Extended_Main_Source_Unit (Gen_Id) then
6605 Set_Is_Instantiated (Inst_Par);
6606 end if;
6607
6608 -- A common mistake is to replicate the naming scheme of a
6609 -- hierarchy by instantiating a generic child directly, rather
6610 -- than the implicit child in a parent instance:
6611
6612 -- generic .. package Gpar is ..
6613 -- generic .. package Gpar.Child is ..
6614 -- package Par is new Gpar ();
6615
6616 -- with Gpar.Child;
6617 -- package Par.Child is new Gpar.Child ();
6618 -- rather than Par.Child
6619
6620 -- In this case the instantiation is within Par, which is an
6621 -- instance, but Gpar does not denote Par because we are not IN
6622 -- the instance of Gpar, so this is illegal. The test below
6623 -- recognizes this particular case.
6624
6625 if Is_Child_Unit (E)
6626 and then not Comes_From_Source (Entity (Prefix (Gen_Id)))
6627 and then (not In_Instance
6628 or else Nkind (Parent (Parent (Gen_Id))) =
6629 N_Compilation_Unit)
6630 then
6631 Error_Msg_N
6632 ("prefix of generic child unit must be instance of parent",
6633 Gen_Id);
6634 end if;
6635
6636 if not In_Open_Scopes (Inst_Par)
6637 and then Nkind (Parent (Gen_Id)) not in
6638 N_Generic_Renaming_Declaration
6639 then
6640 Install_Parent (Inst_Par);
6641 Parent_Installed := True;
6642
6643 elsif In_Open_Scopes (Inst_Par) then
6644
6645 -- If the parent is already installed, install the actuals
6646 -- for its formal packages. This is necessary when the child
6647 -- instance is a child of the parent instance: in this case,
6648 -- the parent is placed on the scope stack but the formal
6649 -- packages are not made visible.
6650
6651 Install_Formal_Packages (Inst_Par);
6652 end if;
6653
6654 else
6655 -- If the generic parent does not contain an entity that
6656 -- corresponds to the selector, the instance doesn't either.
6657 -- Analyzing the node will yield the appropriate error message.
6658 -- If the entity is not a child unit, then it is an inner
6659 -- generic in the parent.
6660
6661 Analyze (Gen_Id);
6662 end if;
6663
6664 else
6665 Analyze (Gen_Id);
6666
6667 if Is_Child_Unit (Entity (Gen_Id))
6668 and then
6669 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
6670 and then not In_Open_Scopes (Inst_Par)
6671 then
6672 Install_Parent (Inst_Par);
6673 Parent_Installed := True;
6674
6675 -- The generic unit may be the renaming of the implicit child
6676 -- present in an instance. In that case the parent instance is
6677 -- obtained from the name of the renamed entity.
6678
6679 elsif Ekind (Entity (Gen_Id)) = E_Generic_Package
6680 and then Present (Renamed_Entity (Entity (Gen_Id)))
6681 and then Is_Child_Unit (Renamed_Entity (Entity (Gen_Id)))
6682 then
6683 declare
6684 Renamed_Package : constant Node_Id :=
6685 Name (Parent (Entity (Gen_Id)));
6686 begin
6687 if Nkind (Renamed_Package) = N_Expanded_Name then
6688 Inst_Par := Entity (Prefix (Renamed_Package));
6689 Install_Parent (Inst_Par);
6690 Parent_Installed := True;
6691 end if;
6692 end;
6693 end if;
6694 end if;
6695
6696 elsif Nkind (Gen_Id) = N_Expanded_Name then
6697
6698 -- Entity already present, analyze prefix, whose meaning may be an
6699 -- instance in the current context. If it is an instance of a
6700 -- relative within another, the proper parent may still have to be
6701 -- installed, if they are not of the same generation.
6702
6703 Analyze (Prefix (Gen_Id));
6704
6705 -- Prevent cascaded errors
6706
6707 if Etype (Prefix (Gen_Id)) = Any_Type then
6708 return;
6709 end if;
6710
6711 -- In the unlikely case that a local declaration hides the name of
6712 -- the parent package, locate it on the homonym chain. If the context
6713 -- is an instance of the parent, the renaming entity is flagged as
6714 -- such.
6715
6716 Inst_Par := Entity (Prefix (Gen_Id));
6717 while Present (Inst_Par)
6718 and then not Is_Package_Or_Generic_Package (Inst_Par)
6719 loop
6720 Inst_Par := Homonym (Inst_Par);
6721 end loop;
6722
6723 pragma Assert (Present (Inst_Par));
6724 Set_Entity (Prefix (Gen_Id), Inst_Par);
6725
6726 if In_Enclosing_Instance then
6727 null;
6728
6729 elsif Present (Entity (Gen_Id))
6730 and then Is_Child_Unit (Entity (Gen_Id))
6731 and then not In_Open_Scopes (Inst_Par)
6732 then
6733 Install_Parent (Inst_Par);
6734 Parent_Installed := True;
6735 end if;
6736
6737 elsif In_Enclosing_Instance then
6738
6739 -- The child unit is found in some enclosing scope
6740
6741 null;
6742
6743 else
6744 Analyze (Gen_Id);
6745
6746 -- If this is the renaming of the implicit child in a parent
6747 -- instance, recover the parent name and install it.
6748
6749 if Is_Entity_Name (Gen_Id) then
6750 E := Entity (Gen_Id);
6751
6752 if Is_Generic_Unit (E)
6753 and then Nkind (Parent (E)) in N_Generic_Renaming_Declaration
6754 and then Is_Child_Unit (Renamed_Object (E))
6755 and then Is_Generic_Unit (Scope (Renamed_Object (E)))
6756 and then Nkind (Name (Parent (E))) = N_Expanded_Name
6757 then
6758 Rewrite (Gen_Id, New_Copy_Tree (Name (Parent (E))));
6759 Inst_Par := Entity (Prefix (Gen_Id));
6760
6761 if not In_Open_Scopes (Inst_Par) then
6762 Install_Parent (Inst_Par);
6763 Parent_Installed := True;
6764 end if;
6765
6766 -- If it is a child unit of a non-generic parent, it may be
6767 -- use-visible and given by a direct name. Install parent as
6768 -- for other cases.
6769
6770 elsif Is_Generic_Unit (E)
6771 and then Is_Child_Unit (E)
6772 and then
6773 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
6774 and then not Is_Generic_Unit (Scope (E))
6775 then
6776 if not In_Open_Scopes (Scope (E)) then
6777 Install_Parent (Scope (E));
6778 Parent_Installed := True;
6779 end if;
6780 end if;
6781 end if;
6782 end if;
6783 end Check_Generic_Child_Unit;
6784
6785 -----------------------------
6786 -- Check_Hidden_Child_Unit --
6787 -----------------------------
6788
6789 procedure Check_Hidden_Child_Unit
6790 (N : Node_Id;
6791 Gen_Unit : Entity_Id;
6792 Act_Decl_Id : Entity_Id)
6793 is
6794 Gen_Id : constant Node_Id := Name (N);
6795
6796 begin
6797 if Is_Child_Unit (Gen_Unit)
6798 and then Is_Child_Unit (Act_Decl_Id)
6799 and then Nkind (Gen_Id) = N_Expanded_Name
6800 and then Entity (Prefix (Gen_Id)) = Scope (Act_Decl_Id)
6801 and then Chars (Gen_Unit) = Chars (Act_Decl_Id)
6802 then
6803 Error_Msg_Node_2 := Scope (Act_Decl_Id);
6804 Error_Msg_NE
6805 ("generic unit & is implicitly declared in &",
6806 Defining_Unit_Name (N), Gen_Unit);
6807 Error_Msg_N ("\instance must have different name",
6808 Defining_Unit_Name (N));
6809 end if;
6810 end Check_Hidden_Child_Unit;
6811
6812 ------------------------
6813 -- Check_Private_View --
6814 ------------------------
6815
6816 procedure Check_Private_View (N : Node_Id) is
6817 T : constant Entity_Id := Etype (N);
6818 BT : Entity_Id;
6819
6820 begin
6821 -- Exchange views if the type was not private in the generic but is
6822 -- private at the point of instantiation. Do not exchange views if
6823 -- the scope of the type is in scope. This can happen if both generic
6824 -- and instance are sibling units, or if type is defined in a parent.
6825 -- In this case the visibility of the type will be correct for all
6826 -- semantic checks.
6827
6828 if Present (T) then
6829 BT := Base_Type (T);
6830
6831 if Is_Private_Type (T)
6832 and then not Has_Private_View (N)
6833 and then Present (Full_View (T))
6834 and then not In_Open_Scopes (Scope (T))
6835 then
6836 -- In the generic, the full type was visible. Save the private
6837 -- entity, for subsequent exchange.
6838
6839 Switch_View (T);
6840
6841 elsif Has_Private_View (N)
6842 and then not Is_Private_Type (T)
6843 and then not Has_Been_Exchanged (T)
6844 and then Etype (Get_Associated_Node (N)) /= T
6845 then
6846 -- Only the private declaration was visible in the generic. If
6847 -- the type appears in a subtype declaration, the subtype in the
6848 -- instance must have a view compatible with that of its parent,
6849 -- which must be exchanged (see corresponding code in Restore_
6850 -- Private_Views). Otherwise, if the type is defined in a parent
6851 -- unit, leave full visibility within instance, which is safe.
6852
6853 if In_Open_Scopes (Scope (Base_Type (T)))
6854 and then not Is_Private_Type (Base_Type (T))
6855 and then Comes_From_Source (Base_Type (T))
6856 then
6857 null;
6858
6859 elsif Nkind (Parent (N)) = N_Subtype_Declaration
6860 or else not In_Private_Part (Scope (Base_Type (T)))
6861 then
6862 Prepend_Elmt (T, Exchanged_Views);
6863 Exchange_Declarations (Etype (Get_Associated_Node (N)));
6864 end if;
6865
6866 -- For composite types with inconsistent representation exchange
6867 -- component types accordingly.
6868
6869 elsif Is_Access_Type (T)
6870 and then Is_Private_Type (Designated_Type (T))
6871 and then not Has_Private_View (N)
6872 and then Present (Full_View (Designated_Type (T)))
6873 then
6874 Switch_View (Designated_Type (T));
6875
6876 elsif Is_Array_Type (T) then
6877 if Is_Private_Type (Component_Type (T))
6878 and then not Has_Private_View (N)
6879 and then Present (Full_View (Component_Type (T)))
6880 then
6881 Switch_View (Component_Type (T));
6882 end if;
6883
6884 -- The normal exchange mechanism relies on the setting of a
6885 -- flag on the reference in the generic. However, an additional
6886 -- mechanism is needed for types that are not explicitly
6887 -- mentioned in the generic, but may be needed in expanded code
6888 -- in the instance. This includes component types of arrays and
6889 -- designated types of access types. This processing must also
6890 -- include the index types of arrays which we take care of here.
6891
6892 declare
6893 Indx : Node_Id;
6894 Typ : Entity_Id;
6895
6896 begin
6897 Indx := First_Index (T);
6898 while Present (Indx) loop
6899 Typ := Base_Type (Etype (Indx));
6900
6901 if Is_Private_Type (Typ)
6902 and then Present (Full_View (Typ))
6903 then
6904 Switch_View (Typ);
6905 end if;
6906
6907 Next_Index (Indx);
6908 end loop;
6909 end;
6910
6911 elsif Is_Private_Type (T)
6912 and then Present (Full_View (T))
6913 and then Is_Array_Type (Full_View (T))
6914 and then Is_Private_Type (Component_Type (Full_View (T)))
6915 then
6916 Switch_View (T);
6917
6918 -- Finally, a non-private subtype may have a private base type, which
6919 -- must be exchanged for consistency. This can happen when a package
6920 -- body is instantiated, when the scope stack is empty but in fact
6921 -- the subtype and the base type are declared in an enclosing scope.
6922
6923 -- Note that in this case we introduce an inconsistency in the view
6924 -- set, because we switch the base type BT, but there could be some
6925 -- private dependent subtypes of BT which remain unswitched. Such
6926 -- subtypes might need to be switched at a later point (see specific
6927 -- provision for that case in Switch_View).
6928
6929 elsif not Is_Private_Type (T)
6930 and then not Has_Private_View (N)
6931 and then Is_Private_Type (BT)
6932 and then Present (Full_View (BT))
6933 and then not Is_Generic_Type (BT)
6934 and then not In_Open_Scopes (BT)
6935 then
6936 Prepend_Elmt (Full_View (BT), Exchanged_Views);
6937 Exchange_Declarations (BT);
6938 end if;
6939 end if;
6940 end Check_Private_View;
6941
6942 -----------------------------
6943 -- Check_Hidden_Primitives --
6944 -----------------------------
6945
6946 function Check_Hidden_Primitives (Assoc_List : List_Id) return Elist_Id is
6947 Actual : Node_Id;
6948 Gen_T : Entity_Id;
6949 Result : Elist_Id := No_Elist;
6950
6951 begin
6952 if No (Assoc_List) then
6953 return No_Elist;
6954 end if;
6955
6956 -- Traverse the list of associations between formals and actuals
6957 -- searching for renamings of tagged types
6958
6959 Actual := First (Assoc_List);
6960 while Present (Actual) loop
6961 if Nkind (Actual) = N_Subtype_Declaration then
6962 Gen_T := Generic_Parent_Type (Actual);
6963
6964 if Present (Gen_T) and then Is_Tagged_Type (Gen_T) then
6965
6966 -- Traverse the list of primitives of the actual types
6967 -- searching for hidden primitives that are visible in the
6968 -- corresponding generic formal; leave them visible and
6969 -- append them to Result to restore their decoration later.
6970
6971 Install_Hidden_Primitives
6972 (Prims_List => Result,
6973 Gen_T => Gen_T,
6974 Act_T => Entity (Subtype_Indication (Actual)));
6975 end if;
6976 end if;
6977
6978 Next (Actual);
6979 end loop;
6980
6981 return Result;
6982 end Check_Hidden_Primitives;
6983
6984 --------------------------
6985 -- Contains_Instance_Of --
6986 --------------------------
6987
6988 function Contains_Instance_Of
6989 (Inner : Entity_Id;
6990 Outer : Entity_Id;
6991 N : Node_Id) return Boolean
6992 is
6993 Elmt : Elmt_Id;
6994 Scop : Entity_Id;
6995
6996 begin
6997 Scop := Outer;
6998
6999 -- Verify that there are no circular instantiations. We check whether
7000 -- the unit contains an instance of the current scope or some enclosing
7001 -- scope (in case one of the instances appears in a subunit). Longer
7002 -- circularities involving subunits might seem too pathological to
7003 -- consider, but they were not too pathological for the authors of
7004 -- DEC bc30vsq, so we loop over all enclosing scopes, and mark all
7005 -- enclosing generic scopes as containing an instance.
7006
7007 loop
7008 -- Within a generic subprogram body, the scope is not generic, to
7009 -- allow for recursive subprograms. Use the declaration to determine
7010 -- whether this is a generic unit.
7011
7012 if Ekind (Scop) = E_Generic_Package
7013 or else (Is_Subprogram (Scop)
7014 and then Nkind (Unit_Declaration_Node (Scop)) =
7015 N_Generic_Subprogram_Declaration)
7016 then
7017 Elmt := First_Elmt (Inner_Instances (Inner));
7018
7019 while Present (Elmt) loop
7020 if Node (Elmt) = Scop then
7021 Error_Msg_Node_2 := Inner;
7022 Error_Msg_NE
7023 ("circular Instantiation: & instantiated within &!",
7024 N, Scop);
7025 return True;
7026
7027 elsif Node (Elmt) = Inner then
7028 return True;
7029
7030 elsif Contains_Instance_Of (Node (Elmt), Scop, N) then
7031 Error_Msg_Node_2 := Inner;
7032 Error_Msg_NE
7033 ("circular Instantiation: & instantiated within &!",
7034 N, Node (Elmt));
7035 return True;
7036 end if;
7037
7038 Next_Elmt (Elmt);
7039 end loop;
7040
7041 -- Indicate that Inner is being instantiated within Scop
7042
7043 Append_Elmt (Inner, Inner_Instances (Scop));
7044 end if;
7045
7046 if Scop = Standard_Standard then
7047 exit;
7048 else
7049 Scop := Scope (Scop);
7050 end if;
7051 end loop;
7052
7053 return False;
7054 end Contains_Instance_Of;
7055
7056 -----------------------
7057 -- Copy_Generic_Node --
7058 -----------------------
7059
7060 function Copy_Generic_Node
7061 (N : Node_Id;
7062 Parent_Id : Node_Id;
7063 Instantiating : Boolean) return Node_Id
7064 is
7065 Ent : Entity_Id;
7066 New_N : Node_Id;
7067
7068 function Copy_Generic_Descendant (D : Union_Id) return Union_Id;
7069 -- Check the given value of one of the Fields referenced by the current
7070 -- node to determine whether to copy it recursively. The field may hold
7071 -- a Node_Id, a List_Id, or an Elist_Id, or a plain value (Sloc, Uint,
7072 -- Char) in which case it need not be copied.
7073
7074 procedure Copy_Descendants;
7075 -- Common utility for various nodes
7076
7077 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id;
7078 -- Make copy of element list
7079
7080 function Copy_Generic_List
7081 (L : List_Id;
7082 Parent_Id : Node_Id) return List_Id;
7083 -- Apply Copy_Node recursively to the members of a node list
7084
7085 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean;
7086 -- True if an identifier is part of the defining program unit name of
7087 -- a child unit. The entity of such an identifier must be kept (for
7088 -- ASIS use) even though as the name of an enclosing generic it would
7089 -- otherwise not be preserved in the generic tree.
7090
7091 ----------------------
7092 -- Copy_Descendants --
7093 ----------------------
7094
7095 procedure Copy_Descendants is
7096 use Atree.Unchecked_Access;
7097 -- This code section is part of the implementation of an untyped
7098 -- tree traversal, so it needs direct access to node fields.
7099
7100 begin
7101 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
7102 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
7103 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
7104 Set_Field4 (New_N, Copy_Generic_Descendant (Field4 (N)));
7105 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
7106 end Copy_Descendants;
7107
7108 -----------------------------
7109 -- Copy_Generic_Descendant --
7110 -----------------------------
7111
7112 function Copy_Generic_Descendant (D : Union_Id) return Union_Id is
7113 begin
7114 if D = Union_Id (Empty) then
7115 return D;
7116
7117 elsif D in Node_Range then
7118 return Union_Id
7119 (Copy_Generic_Node (Node_Id (D), New_N, Instantiating));
7120
7121 elsif D in List_Range then
7122 return Union_Id (Copy_Generic_List (List_Id (D), New_N));
7123
7124 elsif D in Elist_Range then
7125 return Union_Id (Copy_Generic_Elist (Elist_Id (D)));
7126
7127 -- Nothing else is copyable (e.g. Uint values), return as is
7128
7129 else
7130 return D;
7131 end if;
7132 end Copy_Generic_Descendant;
7133
7134 ------------------------
7135 -- Copy_Generic_Elist --
7136 ------------------------
7137
7138 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id is
7139 M : Elmt_Id;
7140 L : Elist_Id;
7141
7142 begin
7143 if Present (E) then
7144 L := New_Elmt_List;
7145 M := First_Elmt (E);
7146 while Present (M) loop
7147 Append_Elmt
7148 (Copy_Generic_Node (Node (M), Empty, Instantiating), L);
7149 Next_Elmt (M);
7150 end loop;
7151
7152 return L;
7153
7154 else
7155 return No_Elist;
7156 end if;
7157 end Copy_Generic_Elist;
7158
7159 -----------------------
7160 -- Copy_Generic_List --
7161 -----------------------
7162
7163 function Copy_Generic_List
7164 (L : List_Id;
7165 Parent_Id : Node_Id) return List_Id
7166 is
7167 N : Node_Id;
7168 New_L : List_Id;
7169
7170 begin
7171 if Present (L) then
7172 New_L := New_List;
7173 Set_Parent (New_L, Parent_Id);
7174
7175 N := First (L);
7176 while Present (N) loop
7177 Append (Copy_Generic_Node (N, Empty, Instantiating), New_L);
7178 Next (N);
7179 end loop;
7180
7181 return New_L;
7182
7183 else
7184 return No_List;
7185 end if;
7186 end Copy_Generic_List;
7187
7188 ---------------------------
7189 -- In_Defining_Unit_Name --
7190 ---------------------------
7191
7192 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean is
7193 begin
7194 return
7195 Present (Parent (Nam))
7196 and then (Nkind (Parent (Nam)) = N_Defining_Program_Unit_Name
7197 or else
7198 (Nkind (Parent (Nam)) = N_Expanded_Name
7199 and then In_Defining_Unit_Name (Parent (Nam))));
7200 end In_Defining_Unit_Name;
7201
7202 -- Start of processing for Copy_Generic_Node
7203
7204 begin
7205 if N = Empty then
7206 return N;
7207 end if;
7208
7209 New_N := New_Copy (N);
7210
7211 -- Copy aspects if present
7212
7213 if Has_Aspects (N) then
7214 Set_Has_Aspects (New_N, False);
7215 Set_Aspect_Specifications
7216 (New_N, Copy_Generic_List (Aspect_Specifications (N), Parent_Id));
7217 end if;
7218
7219 if Instantiating then
7220 Adjust_Instantiation_Sloc (New_N, S_Adjustment);
7221 end if;
7222
7223 if not Is_List_Member (N) then
7224 Set_Parent (New_N, Parent_Id);
7225 end if;
7226
7227 -- Special casing for identifiers and other entity names and operators
7228
7229 if Nkind_In (New_N, N_Character_Literal,
7230 N_Expanded_Name,
7231 N_Identifier,
7232 N_Operator_Symbol)
7233 or else Nkind (New_N) in N_Op
7234 then
7235 if not Instantiating then
7236
7237 -- Link both nodes in order to assign subsequently the entity of
7238 -- the copy to the original node, in case this is a global
7239 -- reference.
7240
7241 Set_Associated_Node (N, New_N);
7242
7243 -- If we are within an instantiation, this is a nested generic
7244 -- that has already been analyzed at the point of definition.
7245 -- We must preserve references that were global to the enclosing
7246 -- parent at that point. Other occurrences, whether global or
7247 -- local to the current generic, must be resolved anew, so we
7248 -- reset the entity in the generic copy. A global reference has a
7249 -- smaller depth than the parent, or else the same depth in case
7250 -- both are distinct compilation units.
7251
7252 -- A child unit is implicitly declared within the enclosing parent
7253 -- but is in fact global to it, and must be preserved.
7254
7255 -- It is also possible for Current_Instantiated_Parent to be
7256 -- defined, and for this not to be a nested generic, namely if
7257 -- the unit is loaded through Rtsfind. In that case, the entity of
7258 -- New_N is only a link to the associated node, and not a defining
7259 -- occurrence.
7260
7261 -- The entities for parent units in the defining_program_unit of a
7262 -- generic child unit are established when the context of the unit
7263 -- is first analyzed, before the generic copy is made. They are
7264 -- preserved in the copy for use in ASIS queries.
7265
7266 Ent := Entity (New_N);
7267
7268 if No (Current_Instantiated_Parent.Gen_Id) then
7269 if No (Ent)
7270 or else Nkind (Ent) /= N_Defining_Identifier
7271 or else not In_Defining_Unit_Name (N)
7272 then
7273 Set_Associated_Node (New_N, Empty);
7274 end if;
7275
7276 elsif No (Ent)
7277 or else
7278 not Nkind_In (Ent, N_Defining_Identifier,
7279 N_Defining_Character_Literal,
7280 N_Defining_Operator_Symbol)
7281 or else No (Scope (Ent))
7282 or else
7283 (Scope (Ent) = Current_Instantiated_Parent.Gen_Id
7284 and then not Is_Child_Unit (Ent))
7285 or else
7286 (Scope_Depth (Scope (Ent)) >
7287 Scope_Depth (Current_Instantiated_Parent.Gen_Id)
7288 and then
7289 Get_Source_Unit (Ent) =
7290 Get_Source_Unit (Current_Instantiated_Parent.Gen_Id))
7291 then
7292 Set_Associated_Node (New_N, Empty);
7293 end if;
7294
7295 -- Case of instantiating identifier or some other name or operator
7296
7297 else
7298 -- If the associated node is still defined, the entity in it
7299 -- is global, and must be copied to the instance. If this copy
7300 -- is being made for a body to inline, it is applied to an
7301 -- instantiated tree, and the entity is already present and
7302 -- must be also preserved.
7303
7304 declare
7305 Assoc : constant Node_Id := Get_Associated_Node (N);
7306
7307 begin
7308 if Present (Assoc) then
7309 if Nkind (Assoc) = Nkind (N) then
7310 Set_Entity (New_N, Entity (Assoc));
7311 Check_Private_View (N);
7312
7313 -- The node is a reference to a global type and acts as the
7314 -- subtype mark of a qualified expression created in order
7315 -- to aid resolution of accidental overloading in instances.
7316 -- Since N is a reference to a type, the Associated_Node of
7317 -- N denotes an entity rather than another identifier. See
7318 -- Qualify_Universal_Operands for details.
7319
7320 elsif Nkind (N) = N_Identifier
7321 and then Nkind (Parent (N)) = N_Qualified_Expression
7322 and then Subtype_Mark (Parent (N)) = N
7323 and then Is_Qualified_Universal_Literal (Parent (N))
7324 then
7325 Set_Entity (New_N, Assoc);
7326
7327 -- The name in the call may be a selected component if the
7328 -- call has not been analyzed yet, as may be the case for
7329 -- pre/post conditions in a generic unit.
7330
7331 elsif Nkind (Assoc) = N_Function_Call
7332 and then Is_Entity_Name (Name (Assoc))
7333 then
7334 Set_Entity (New_N, Entity (Name (Assoc)));
7335
7336 elsif Nkind_In (Assoc, N_Defining_Identifier,
7337 N_Defining_Character_Literal,
7338 N_Defining_Operator_Symbol)
7339 and then Expander_Active
7340 then
7341 -- Inlining case: we are copying a tree that contains
7342 -- global entities, which are preserved in the copy to be
7343 -- used for subsequent inlining.
7344
7345 null;
7346
7347 else
7348 Set_Entity (New_N, Empty);
7349 end if;
7350 end if;
7351 end;
7352 end if;
7353
7354 -- For expanded name, we must copy the Prefix and Selector_Name
7355
7356 if Nkind (N) = N_Expanded_Name then
7357 Set_Prefix
7358 (New_N, Copy_Generic_Node (Prefix (N), New_N, Instantiating));
7359
7360 Set_Selector_Name (New_N,
7361 Copy_Generic_Node (Selector_Name (N), New_N, Instantiating));
7362
7363 -- For operators, we must copy the right operand
7364
7365 elsif Nkind (N) in N_Op then
7366 Set_Right_Opnd (New_N,
7367 Copy_Generic_Node (Right_Opnd (N), New_N, Instantiating));
7368
7369 -- And for binary operators, the left operand as well
7370
7371 if Nkind (N) in N_Binary_Op then
7372 Set_Left_Opnd (New_N,
7373 Copy_Generic_Node (Left_Opnd (N), New_N, Instantiating));
7374 end if;
7375 end if;
7376
7377 -- Establish a link between an entity from the generic template and the
7378 -- corresponding entity in the generic copy to be analyzed.
7379
7380 elsif Nkind (N) in N_Entity then
7381 if not Instantiating then
7382 Set_Associated_Entity (N, New_N);
7383 end if;
7384
7385 -- Clear any existing link the copy may inherit from the replicated
7386 -- generic template entity.
7387
7388 Set_Associated_Entity (New_N, Empty);
7389
7390 -- Special casing for stubs
7391
7392 elsif Nkind (N) in N_Body_Stub then
7393
7394 -- In any case, we must copy the specification or defining
7395 -- identifier as appropriate.
7396
7397 if Nkind (N) = N_Subprogram_Body_Stub then
7398 Set_Specification (New_N,
7399 Copy_Generic_Node (Specification (N), New_N, Instantiating));
7400
7401 else
7402 Set_Defining_Identifier (New_N,
7403 Copy_Generic_Node
7404 (Defining_Identifier (N), New_N, Instantiating));
7405 end if;
7406
7407 -- If we are not instantiating, then this is where we load and
7408 -- analyze subunits, i.e. at the point where the stub occurs. A
7409 -- more permissive system might defer this analysis to the point
7410 -- of instantiation, but this seems too complicated for now.
7411
7412 if not Instantiating then
7413 declare
7414 Subunit_Name : constant Unit_Name_Type := Get_Unit_Name (N);
7415 Subunit : Node_Id;
7416 Unum : Unit_Number_Type;
7417 New_Body : Node_Id;
7418
7419 begin
7420 -- Make sure that, if it is a subunit of the main unit that is
7421 -- preprocessed and if -gnateG is specified, the preprocessed
7422 -- file will be written.
7423
7424 Lib.Analysing_Subunit_Of_Main :=
7425 Lib.In_Extended_Main_Source_Unit (N);
7426 Unum :=
7427 Load_Unit
7428 (Load_Name => Subunit_Name,
7429 Required => False,
7430 Subunit => True,
7431 Error_Node => N);
7432 Lib.Analysing_Subunit_Of_Main := False;
7433
7434 -- If the proper body is not found, a warning message will be
7435 -- emitted when analyzing the stub, or later at the point of
7436 -- instantiation. Here we just leave the stub as is.
7437
7438 if Unum = No_Unit then
7439 Subunits_Missing := True;
7440 goto Subunit_Not_Found;
7441 end if;
7442
7443 Subunit := Cunit (Unum);
7444
7445 if Nkind (Unit (Subunit)) /= N_Subunit then
7446 Error_Msg_N
7447 ("found child unit instead of expected SEPARATE subunit",
7448 Subunit);
7449 Error_Msg_Sloc := Sloc (N);
7450 Error_Msg_N ("\to complete stub #", Subunit);
7451 goto Subunit_Not_Found;
7452 end if;
7453
7454 -- We must create a generic copy of the subunit, in order to
7455 -- perform semantic analysis on it, and we must replace the
7456 -- stub in the original generic unit with the subunit, in order
7457 -- to preserve non-local references within.
7458
7459 -- Only the proper body needs to be copied. Library_Unit and
7460 -- context clause are simply inherited by the generic copy.
7461 -- Note that the copy (which may be recursive if there are
7462 -- nested subunits) must be done first, before attaching it to
7463 -- the enclosing generic.
7464
7465 New_Body :=
7466 Copy_Generic_Node
7467 (Proper_Body (Unit (Subunit)),
7468 Empty, Instantiating => False);
7469
7470 -- Now place the original proper body in the original generic
7471 -- unit. This is a body, not a compilation unit.
7472
7473 Rewrite (N, Proper_Body (Unit (Subunit)));
7474 Set_Is_Compilation_Unit (Defining_Entity (N), False);
7475 Set_Was_Originally_Stub (N);
7476
7477 -- Finally replace the body of the subunit with its copy, and
7478 -- make this new subunit into the library unit of the generic
7479 -- copy, which does not have stubs any longer.
7480
7481 Set_Proper_Body (Unit (Subunit), New_Body);
7482 Set_Library_Unit (New_N, Subunit);
7483 Inherit_Context (Unit (Subunit), N);
7484 end;
7485
7486 -- If we are instantiating, this must be an error case, since
7487 -- otherwise we would have replaced the stub node by the proper body
7488 -- that corresponds. So just ignore it in the copy (i.e. we have
7489 -- copied it, and that is good enough).
7490
7491 else
7492 null;
7493 end if;
7494
7495 <<Subunit_Not_Found>> null;
7496
7497 -- If the node is a compilation unit, it is the subunit of a stub, which
7498 -- has been loaded already (see code below). In this case, the library
7499 -- unit field of N points to the parent unit (which is a compilation
7500 -- unit) and need not (and cannot) be copied.
7501
7502 -- When the proper body of the stub is analyzed, the library_unit link
7503 -- is used to establish the proper context (see sem_ch10).
7504
7505 -- The other fields of a compilation unit are copied as usual
7506
7507 elsif Nkind (N) = N_Compilation_Unit then
7508
7509 -- This code can only be executed when not instantiating, because in
7510 -- the copy made for an instantiation, the compilation unit node has
7511 -- disappeared at the point that a stub is replaced by its proper
7512 -- body.
7513
7514 pragma Assert (not Instantiating);
7515
7516 Set_Context_Items (New_N,
7517 Copy_Generic_List (Context_Items (N), New_N));
7518
7519 Set_Unit (New_N,
7520 Copy_Generic_Node (Unit (N), New_N, False));
7521
7522 Set_First_Inlined_Subprogram (New_N,
7523 Copy_Generic_Node
7524 (First_Inlined_Subprogram (N), New_N, False));
7525
7526 Set_Aux_Decls_Node (New_N,
7527 Copy_Generic_Node (Aux_Decls_Node (N), New_N, False));
7528
7529 -- For an assignment node, the assignment is known to be semantically
7530 -- legal if we are instantiating the template. This avoids incorrect
7531 -- diagnostics in generated code.
7532
7533 elsif Nkind (N) = N_Assignment_Statement then
7534
7535 -- Copy name and expression fields in usual manner
7536
7537 Set_Name (New_N,
7538 Copy_Generic_Node (Name (N), New_N, Instantiating));
7539
7540 Set_Expression (New_N,
7541 Copy_Generic_Node (Expression (N), New_N, Instantiating));
7542
7543 if Instantiating then
7544 Set_Assignment_OK (Name (New_N), True);
7545 end if;
7546
7547 elsif Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
7548 if not Instantiating then
7549 Set_Associated_Node (N, New_N);
7550
7551 else
7552 if Present (Get_Associated_Node (N))
7553 and then Nkind (Get_Associated_Node (N)) = Nkind (N)
7554 then
7555 -- In the generic the aggregate has some composite type. If at
7556 -- the point of instantiation the type has a private view,
7557 -- install the full view (and that of its ancestors, if any).
7558
7559 declare
7560 T : Entity_Id := (Etype (Get_Associated_Node (New_N)));
7561 Rt : Entity_Id;
7562
7563 begin
7564 if Present (T) and then Is_Private_Type (T) then
7565 Switch_View (T);
7566 end if;
7567
7568 if Present (T)
7569 and then Is_Tagged_Type (T)
7570 and then Is_Derived_Type (T)
7571 then
7572 Rt := Root_Type (T);
7573
7574 loop
7575 T := Etype (T);
7576
7577 if Is_Private_Type (T) then
7578 Switch_View (T);
7579 end if;
7580
7581 exit when T = Rt;
7582 end loop;
7583 end if;
7584 end;
7585 end if;
7586 end if;
7587
7588 -- Do not copy the associated node, which points to the generic copy
7589 -- of the aggregate.
7590
7591 declare
7592 use Atree.Unchecked_Access;
7593 -- This code section is part of the implementation of an untyped
7594 -- tree traversal, so it needs direct access to node fields.
7595
7596 begin
7597 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
7598 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
7599 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
7600 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
7601 end;
7602
7603 -- Allocators do not have an identifier denoting the access type, so we
7604 -- must locate it through the expression to check whether the views are
7605 -- consistent.
7606
7607 elsif Nkind (N) = N_Allocator
7608 and then Nkind (Expression (N)) = N_Qualified_Expression
7609 and then Is_Entity_Name (Subtype_Mark (Expression (N)))
7610 and then Instantiating
7611 then
7612 declare
7613 T : constant Node_Id :=
7614 Get_Associated_Node (Subtype_Mark (Expression (N)));
7615 Acc_T : Entity_Id;
7616
7617 begin
7618 if Present (T) then
7619
7620 -- Retrieve the allocator node in the generic copy
7621
7622 Acc_T := Etype (Parent (Parent (T)));
7623
7624 if Present (Acc_T) and then Is_Private_Type (Acc_T) then
7625 Switch_View (Acc_T);
7626 end if;
7627 end if;
7628
7629 Copy_Descendants;
7630 end;
7631
7632 -- For a proper body, we must catch the case of a proper body that
7633 -- replaces a stub. This represents the point at which a separate
7634 -- compilation unit, and hence template file, may be referenced, so we
7635 -- must make a new source instantiation entry for the template of the
7636 -- subunit, and ensure that all nodes in the subunit are adjusted using
7637 -- this new source instantiation entry.
7638
7639 elsif Nkind (N) in N_Proper_Body then
7640 declare
7641 Save_Adjustment : constant Sloc_Adjustment := S_Adjustment;
7642
7643 begin
7644 if Instantiating and then Was_Originally_Stub (N) then
7645 Create_Instantiation_Source
7646 (Instantiation_Node,
7647 Defining_Entity (N),
7648 S_Adjustment);
7649 end if;
7650
7651 -- Now copy the fields of the proper body, using the new
7652 -- adjustment factor if one was needed as per test above.
7653
7654 Copy_Descendants;
7655
7656 -- Restore the original adjustment factor in case changed
7657
7658 S_Adjustment := Save_Adjustment;
7659 end;
7660
7661 elsif Nkind (N) = N_Pragma and then Instantiating then
7662
7663 -- Do not copy Comment or Ident pragmas their content is relevant to
7664 -- the generic unit, not to the instantiating unit.
7665
7666 if Nam_In (Pragma_Name (N), Name_Comment, Name_Ident) then
7667 New_N := Make_Null_Statement (Sloc (N));
7668
7669 -- Do not copy pragmas generated from aspects because the pragmas do
7670 -- not carry any semantic information, plus they will be regenerated
7671 -- in the instance.
7672
7673 -- However, generating C we need to copy them since postconditions
7674 -- are inlined by the front end, and the front-end inlining machinery
7675 -- relies on this routine to perform inlining.
7676
7677 elsif From_Aspect_Specification (N)
7678 and then not Modify_Tree_For_C
7679 then
7680 New_N := Make_Null_Statement (Sloc (N));
7681
7682 else
7683 Copy_Descendants;
7684 end if;
7685
7686 elsif Nkind_In (N, N_Integer_Literal, N_Real_Literal) then
7687
7688 -- No descendant fields need traversing
7689
7690 null;
7691
7692 elsif Nkind (N) = N_String_Literal
7693 and then Present (Etype (N))
7694 and then Instantiating
7695 then
7696 -- If the string is declared in an outer scope, the string_literal
7697 -- subtype created for it may have the wrong scope. Force reanalysis
7698 -- of the constant to generate a new itype in the proper context.
7699
7700 Set_Etype (New_N, Empty);
7701 Set_Analyzed (New_N, False);
7702
7703 -- For the remaining nodes, copy their descendants recursively
7704
7705 else
7706 Copy_Descendants;
7707
7708 if Instantiating and then Nkind (N) = N_Subprogram_Body then
7709 Set_Generic_Parent (Specification (New_N), N);
7710
7711 -- Should preserve Corresponding_Spec??? (12.3(14))
7712 end if;
7713 end if;
7714
7715 -- Propagate dimensions if present, so that they are reflected in the
7716 -- instance.
7717
7718 if Nkind (N) in N_Has_Etype
7719 and then (Nkind (N) in N_Op or else Is_Entity_Name (N))
7720 and then Present (Etype (N))
7721 and then Is_Floating_Point_Type (Etype (N))
7722 and then Has_Dimension_System (Etype (N))
7723 then
7724 Copy_Dimensions (N, New_N);
7725 end if;
7726
7727 return New_N;
7728 end Copy_Generic_Node;
7729
7730 ----------------------------
7731 -- Denotes_Formal_Package --
7732 ----------------------------
7733
7734 function Denotes_Formal_Package
7735 (Pack : Entity_Id;
7736 On_Exit : Boolean := False;
7737 Instance : Entity_Id := Empty) return Boolean
7738 is
7739 Par : Entity_Id;
7740 Scop : constant Entity_Id := Scope (Pack);
7741 E : Entity_Id;
7742
7743 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean;
7744 -- The package in question may be an actual for a previous formal
7745 -- package P of the current instance, so examine its actuals as well.
7746 -- This must be recursive over other formal packages.
7747
7748 ----------------------------------
7749 -- Is_Actual_Of_Previous_Formal --
7750 ----------------------------------
7751
7752 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean is
7753 E1 : Entity_Id;
7754
7755 begin
7756 E1 := First_Entity (P);
7757 while Present (E1) and then E1 /= Instance loop
7758 if Ekind (E1) = E_Package
7759 and then Nkind (Parent (E1)) = N_Package_Renaming_Declaration
7760 then
7761 if Renamed_Object (E1) = Pack then
7762 return True;
7763
7764 elsif E1 = P or else Renamed_Object (E1) = P then
7765 return False;
7766
7767 elsif Is_Actual_Of_Previous_Formal (E1) then
7768 return True;
7769 end if;
7770 end if;
7771
7772 Next_Entity (E1);
7773 end loop;
7774
7775 return False;
7776 end Is_Actual_Of_Previous_Formal;
7777
7778 -- Start of processing for Denotes_Formal_Package
7779
7780 begin
7781 if On_Exit then
7782 Par :=
7783 Instance_Envs.Table
7784 (Instance_Envs.Last).Instantiated_Parent.Act_Id;
7785 else
7786 Par := Current_Instantiated_Parent.Act_Id;
7787 end if;
7788
7789 if Ekind (Scop) = E_Generic_Package
7790 or else Nkind (Unit_Declaration_Node (Scop)) =
7791 N_Generic_Subprogram_Declaration
7792 then
7793 return True;
7794
7795 elsif Nkind (Original_Node (Unit_Declaration_Node (Pack))) =
7796 N_Formal_Package_Declaration
7797 then
7798 return True;
7799
7800 elsif No (Par) then
7801 return False;
7802
7803 else
7804 -- Check whether this package is associated with a formal package of
7805 -- the enclosing instantiation. Iterate over the list of renamings.
7806
7807 E := First_Entity (Par);
7808 while Present (E) loop
7809 if Ekind (E) /= E_Package
7810 or else Nkind (Parent (E)) /= N_Package_Renaming_Declaration
7811 then
7812 null;
7813
7814 elsif Renamed_Object (E) = Par then
7815 return False;
7816
7817 elsif Renamed_Object (E) = Pack then
7818 return True;
7819
7820 elsif Is_Actual_Of_Previous_Formal (E) then
7821 return True;
7822
7823 end if;
7824
7825 Next_Entity (E);
7826 end loop;
7827
7828 return False;
7829 end if;
7830 end Denotes_Formal_Package;
7831
7832 -----------------
7833 -- End_Generic --
7834 -----------------
7835
7836 procedure End_Generic is
7837 begin
7838 -- ??? More things could be factored out in this routine. Should
7839 -- probably be done at a later stage.
7840
7841 Inside_A_Generic := Generic_Flags.Table (Generic_Flags.Last);
7842 Generic_Flags.Decrement_Last;
7843
7844 Expander_Mode_Restore;
7845 end End_Generic;
7846
7847 -------------
7848 -- Earlier --
7849 -------------
7850
7851 function Earlier (N1, N2 : Node_Id) return Boolean is
7852 procedure Find_Depth (P : in out Node_Id; D : in out Integer);
7853 -- Find distance from given node to enclosing compilation unit
7854
7855 ----------------
7856 -- Find_Depth --
7857 ----------------
7858
7859 procedure Find_Depth (P : in out Node_Id; D : in out Integer) is
7860 begin
7861 while Present (P)
7862 and then Nkind (P) /= N_Compilation_Unit
7863 loop
7864 P := True_Parent (P);
7865 D := D + 1;
7866 end loop;
7867 end Find_Depth;
7868
7869 -- Local declarations
7870
7871 D1 : Integer := 0;
7872 D2 : Integer := 0;
7873 P1 : Node_Id := N1;
7874 P2 : Node_Id := N2;
7875 T1 : Source_Ptr;
7876 T2 : Source_Ptr;
7877
7878 -- Start of processing for Earlier
7879
7880 begin
7881 Find_Depth (P1, D1);
7882 Find_Depth (P2, D2);
7883
7884 if P1 /= P2 then
7885 return False;
7886 else
7887 P1 := N1;
7888 P2 := N2;
7889 end if;
7890
7891 while D1 > D2 loop
7892 P1 := True_Parent (P1);
7893 D1 := D1 - 1;
7894 end loop;
7895
7896 while D2 > D1 loop
7897 P2 := True_Parent (P2);
7898 D2 := D2 - 1;
7899 end loop;
7900
7901 -- At this point P1 and P2 are at the same distance from the root.
7902 -- We examine their parents until we find a common declarative list.
7903 -- If we reach the root, N1 and N2 do not descend from the same
7904 -- declarative list (e.g. one is nested in the declarative part and
7905 -- the other is in a block in the statement part) and the earlier
7906 -- one is already frozen.
7907
7908 while not Is_List_Member (P1)
7909 or else not Is_List_Member (P2)
7910 or else List_Containing (P1) /= List_Containing (P2)
7911 loop
7912 P1 := True_Parent (P1);
7913 P2 := True_Parent (P2);
7914
7915 if Nkind (Parent (P1)) = N_Subunit then
7916 P1 := Corresponding_Stub (Parent (P1));
7917 end if;
7918
7919 if Nkind (Parent (P2)) = N_Subunit then
7920 P2 := Corresponding_Stub (Parent (P2));
7921 end if;
7922
7923 if P1 = P2 then
7924 return False;
7925 end if;
7926 end loop;
7927
7928 -- Expanded code usually shares the source location of the original
7929 -- construct it was generated for. This however may not necessarily
7930 -- reflect the true location of the code within the tree.
7931
7932 -- Before comparing the slocs of the two nodes, make sure that we are
7933 -- working with correct source locations. Assume that P1 is to the left
7934 -- of P2. If either one does not come from source, traverse the common
7935 -- list heading towards the other node and locate the first source
7936 -- statement.
7937
7938 -- P1 P2
7939 -- ----+===+===+--------------+===+===+----
7940 -- expanded code expanded code
7941
7942 if not Comes_From_Source (P1) then
7943 while Present (P1) loop
7944
7945 -- Neither P2 nor a source statement were located during the
7946 -- search. If we reach the end of the list, then P1 does not
7947 -- occur earlier than P2.
7948
7949 -- ---->
7950 -- start --- P2 ----- P1 --- end
7951
7952 if No (Next (P1)) then
7953 return False;
7954
7955 -- We encounter P2 while going to the right of the list. This
7956 -- means that P1 does indeed appear earlier.
7957
7958 -- ---->
7959 -- start --- P1 ===== P2 --- end
7960 -- expanded code in between
7961
7962 elsif P1 = P2 then
7963 return True;
7964
7965 -- No need to look any further since we have located a source
7966 -- statement.
7967
7968 elsif Comes_From_Source (P1) then
7969 exit;
7970 end if;
7971
7972 -- Keep going right
7973
7974 Next (P1);
7975 end loop;
7976 end if;
7977
7978 if not Comes_From_Source (P2) then
7979 while Present (P2) loop
7980
7981 -- Neither P1 nor a source statement were located during the
7982 -- search. If we reach the start of the list, then P1 does not
7983 -- occur earlier than P2.
7984
7985 -- <----
7986 -- start --- P2 --- P1 --- end
7987
7988 if No (Prev (P2)) then
7989 return False;
7990
7991 -- We encounter P1 while going to the left of the list. This
7992 -- means that P1 does indeed appear earlier.
7993
7994 -- <----
7995 -- start --- P1 ===== P2 --- end
7996 -- expanded code in between
7997
7998 elsif P2 = P1 then
7999 return True;
8000
8001 -- No need to look any further since we have located a source
8002 -- statement.
8003
8004 elsif Comes_From_Source (P2) then
8005 exit;
8006 end if;
8007
8008 -- Keep going left
8009
8010 Prev (P2);
8011 end loop;
8012 end if;
8013
8014 -- At this point either both nodes came from source or we approximated
8015 -- their source locations through neighbouring source statements.
8016
8017 T1 := Top_Level_Location (Sloc (P1));
8018 T2 := Top_Level_Location (Sloc (P2));
8019
8020 -- When two nodes come from the same instance, they have identical top
8021 -- level locations. To determine proper relation within the tree, check
8022 -- their locations within the template.
8023
8024 if T1 = T2 then
8025 return Sloc (P1) < Sloc (P2);
8026
8027 -- The two nodes either come from unrelated instances or do not come
8028 -- from instantiated code at all.
8029
8030 else
8031 return T1 < T2;
8032 end if;
8033 end Earlier;
8034
8035 ----------------------
8036 -- Find_Actual_Type --
8037 ----------------------
8038
8039 function Find_Actual_Type
8040 (Typ : Entity_Id;
8041 Gen_Type : Entity_Id) return Entity_Id
8042 is
8043 Gen_Scope : constant Entity_Id := Scope (Gen_Type);
8044 T : Entity_Id;
8045
8046 begin
8047 -- Special processing only applies to child units
8048
8049 if not Is_Child_Unit (Gen_Scope) then
8050 return Get_Instance_Of (Typ);
8051
8052 -- If designated or component type is itself a formal of the child unit,
8053 -- its instance is available.
8054
8055 elsif Scope (Typ) = Gen_Scope then
8056 return Get_Instance_Of (Typ);
8057
8058 -- If the array or access type is not declared in the parent unit,
8059 -- no special processing needed.
8060
8061 elsif not Is_Generic_Type (Typ)
8062 and then Scope (Gen_Scope) /= Scope (Typ)
8063 then
8064 return Get_Instance_Of (Typ);
8065
8066 -- Otherwise, retrieve designated or component type by visibility
8067
8068 else
8069 T := Current_Entity (Typ);
8070 while Present (T) loop
8071 if In_Open_Scopes (Scope (T)) then
8072 return T;
8073 elsif Is_Generic_Actual_Type (T) then
8074 return T;
8075 end if;
8076
8077 T := Homonym (T);
8078 end loop;
8079
8080 return Typ;
8081 end if;
8082 end Find_Actual_Type;
8083
8084 ----------------------------
8085 -- Freeze_Subprogram_Body --
8086 ----------------------------
8087
8088 procedure Freeze_Subprogram_Body
8089 (Inst_Node : Node_Id;
8090 Gen_Body : Node_Id;
8091 Pack_Id : Entity_Id)
8092 is
8093 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
8094 Par : constant Entity_Id := Scope (Gen_Unit);
8095 E_G_Id : Entity_Id;
8096 Enc_G : Entity_Id;
8097 Enc_I : Node_Id;
8098 F_Node : Node_Id;
8099
8100 function Enclosing_Package_Body (N : Node_Id) return Node_Id;
8101 -- Find innermost package body that encloses the given node, and which
8102 -- is not a compilation unit. Freeze nodes for the instance, or for its
8103 -- enclosing body, may be inserted after the enclosing_body of the
8104 -- generic unit. Used to determine proper placement of freeze node for
8105 -- both package and subprogram instances.
8106
8107 function Package_Freeze_Node (B : Node_Id) return Node_Id;
8108 -- Find entity for given package body, and locate or create a freeze
8109 -- node for it.
8110
8111 ----------------------------
8112 -- Enclosing_Package_Body --
8113 ----------------------------
8114
8115 function Enclosing_Package_Body (N : Node_Id) return Node_Id is
8116 P : Node_Id;
8117
8118 begin
8119 P := Parent (N);
8120 while Present (P)
8121 and then Nkind (Parent (P)) /= N_Compilation_Unit
8122 loop
8123 if Nkind (P) = N_Package_Body then
8124 if Nkind (Parent (P)) = N_Subunit then
8125 return Corresponding_Stub (Parent (P));
8126 else
8127 return P;
8128 end if;
8129 end if;
8130
8131 P := True_Parent (P);
8132 end loop;
8133
8134 return Empty;
8135 end Enclosing_Package_Body;
8136
8137 -------------------------
8138 -- Package_Freeze_Node --
8139 -------------------------
8140
8141 function Package_Freeze_Node (B : Node_Id) return Node_Id is
8142 Id : Entity_Id;
8143
8144 begin
8145 if Nkind (B) = N_Package_Body then
8146 Id := Corresponding_Spec (B);
8147 else pragma Assert (Nkind (B) = N_Package_Body_Stub);
8148 Id := Corresponding_Spec (Proper_Body (Unit (Library_Unit (B))));
8149 end if;
8150
8151 Ensure_Freeze_Node (Id);
8152 return Freeze_Node (Id);
8153 end Package_Freeze_Node;
8154
8155 -- Start of processing for Freeze_Subprogram_Body
8156
8157 begin
8158 -- If the instance and the generic body appear within the same unit, and
8159 -- the instance precedes the generic, the freeze node for the instance
8160 -- must appear after that of the generic. If the generic is nested
8161 -- within another instance I2, then current instance must be frozen
8162 -- after I2. In both cases, the freeze nodes are those of enclosing
8163 -- packages. Otherwise, the freeze node is placed at the end of the
8164 -- current declarative part.
8165
8166 Enc_G := Enclosing_Package_Body (Gen_Body);
8167 Enc_I := Enclosing_Package_Body (Inst_Node);
8168 Ensure_Freeze_Node (Pack_Id);
8169 F_Node := Freeze_Node (Pack_Id);
8170
8171 if Is_Generic_Instance (Par)
8172 and then Present (Freeze_Node (Par))
8173 and then In_Same_Declarative_Part (Freeze_Node (Par), Inst_Node)
8174 then
8175 -- The parent was a premature instantiation. Insert freeze node at
8176 -- the end the current declarative part.
8177
8178 if ABE_Is_Certain (Get_Package_Instantiation_Node (Par)) then
8179 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8180
8181 -- Handle the following case:
8182 --
8183 -- package Parent_Inst is new ...
8184 -- Parent_Inst []
8185 --
8186 -- procedure P ... -- this body freezes Parent_Inst
8187 --
8188 -- package Inst is new ...
8189 --
8190 -- In this particular scenario, the freeze node for Inst must be
8191 -- inserted in the same manner as that of Parent_Inst - before the
8192 -- next source body or at the end of the declarative list (body not
8193 -- available). If body P did not exist and Parent_Inst was frozen
8194 -- after Inst, either by a body following Inst or at the end of the
8195 -- declarative region, the freeze node for Inst must be inserted
8196 -- after that of Parent_Inst. This relation is established by
8197 -- comparing the Slocs of Parent_Inst freeze node and Inst.
8198
8199 elsif List_Containing (Get_Package_Instantiation_Node (Par)) =
8200 List_Containing (Inst_Node)
8201 and then Sloc (Freeze_Node (Par)) < Sloc (Inst_Node)
8202 then
8203 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8204
8205 else
8206 Insert_After (Freeze_Node (Par), F_Node);
8207 end if;
8208
8209 -- The body enclosing the instance should be frozen after the body that
8210 -- includes the generic, because the body of the instance may make
8211 -- references to entities therein. If the two are not in the same
8212 -- declarative part, or if the one enclosing the instance is frozen
8213 -- already, freeze the instance at the end of the current declarative
8214 -- part.
8215
8216 elsif Is_Generic_Instance (Par)
8217 and then Present (Freeze_Node (Par))
8218 and then Present (Enc_I)
8219 then
8220 if In_Same_Declarative_Part (Freeze_Node (Par), Enc_I)
8221 or else
8222 (Nkind (Enc_I) = N_Package_Body
8223 and then
8224 In_Same_Declarative_Part (Freeze_Node (Par), Parent (Enc_I)))
8225 then
8226 -- The enclosing package may contain several instances. Rather
8227 -- than computing the earliest point at which to insert its freeze
8228 -- node, we place it at the end of the declarative part of the
8229 -- parent of the generic.
8230
8231 Insert_Freeze_Node_For_Instance
8232 (Freeze_Node (Par), Package_Freeze_Node (Enc_I));
8233 end if;
8234
8235 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8236
8237 elsif Present (Enc_G)
8238 and then Present (Enc_I)
8239 and then Enc_G /= Enc_I
8240 and then Earlier (Inst_Node, Gen_Body)
8241 then
8242 if Nkind (Enc_G) = N_Package_Body then
8243 E_G_Id :=
8244 Corresponding_Spec (Enc_G);
8245 else pragma Assert (Nkind (Enc_G) = N_Package_Body_Stub);
8246 E_G_Id :=
8247 Corresponding_Spec (Proper_Body (Unit (Library_Unit (Enc_G))));
8248 end if;
8249
8250 -- Freeze package that encloses instance, and place node after the
8251 -- package that encloses generic. If enclosing package is already
8252 -- frozen we have to assume it is at the proper place. This may be a
8253 -- potential ABE that requires dynamic checking. Do not add a freeze
8254 -- node if the package that encloses the generic is inside the body
8255 -- that encloses the instance, because the freeze node would be in
8256 -- the wrong scope. Additional contortions needed if the bodies are
8257 -- within a subunit.
8258
8259 declare
8260 Enclosing_Body : Node_Id;
8261
8262 begin
8263 if Nkind (Enc_I) = N_Package_Body_Stub then
8264 Enclosing_Body := Proper_Body (Unit (Library_Unit (Enc_I)));
8265 else
8266 Enclosing_Body := Enc_I;
8267 end if;
8268
8269 if Parent (List_Containing (Enc_G)) /= Enclosing_Body then
8270 Insert_Freeze_Node_For_Instance
8271 (Enc_G, Package_Freeze_Node (Enc_I));
8272 end if;
8273 end;
8274
8275 -- Freeze enclosing subunit before instance
8276
8277 Ensure_Freeze_Node (E_G_Id);
8278
8279 if not Is_List_Member (Freeze_Node (E_G_Id)) then
8280 Insert_After (Enc_G, Freeze_Node (E_G_Id));
8281 end if;
8282
8283 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8284
8285 else
8286 -- If none of the above, insert freeze node at the end of the current
8287 -- declarative part.
8288
8289 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8290 end if;
8291 end Freeze_Subprogram_Body;
8292
8293 ----------------
8294 -- Get_Gen_Id --
8295 ----------------
8296
8297 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id is
8298 begin
8299 return Generic_Renamings.Table (E).Gen_Id;
8300 end Get_Gen_Id;
8301
8302 ---------------------
8303 -- Get_Instance_Of --
8304 ---------------------
8305
8306 function Get_Instance_Of (A : Entity_Id) return Entity_Id is
8307 Res : constant Assoc_Ptr := Generic_Renamings_HTable.Get (A);
8308
8309 begin
8310 if Res /= Assoc_Null then
8311 return Generic_Renamings.Table (Res).Act_Id;
8312
8313 else
8314 -- On exit, entity is not instantiated: not a generic parameter, or
8315 -- else parameter of an inner generic unit.
8316
8317 return A;
8318 end if;
8319 end Get_Instance_Of;
8320
8321 ------------------------------------
8322 -- Get_Package_Instantiation_Node --
8323 ------------------------------------
8324
8325 function Get_Package_Instantiation_Node (A : Entity_Id) return Node_Id is
8326 Decl : Node_Id := Unit_Declaration_Node (A);
8327 Inst : Node_Id;
8328
8329 begin
8330 -- If the Package_Instantiation attribute has been set on the package
8331 -- entity, then use it directly when it (or its Original_Node) refers
8332 -- to an N_Package_Instantiation node. In principle it should be
8333 -- possible to have this field set in all cases, which should be
8334 -- investigated, and would allow this function to be significantly
8335 -- simplified. ???
8336
8337 Inst := Package_Instantiation (A);
8338
8339 if Present (Inst) then
8340 if Nkind (Inst) = N_Package_Instantiation then
8341 return Inst;
8342
8343 elsif Nkind (Original_Node (Inst)) = N_Package_Instantiation then
8344 return Original_Node (Inst);
8345 end if;
8346 end if;
8347
8348 -- If the instantiation is a compilation unit that does not need body
8349 -- then the instantiation node has been rewritten as a package
8350 -- declaration for the instance, and we return the original node.
8351
8352 -- If it is a compilation unit and the instance node has not been
8353 -- rewritten, then it is still the unit of the compilation. Finally, if
8354 -- a body is present, this is a parent of the main unit whose body has
8355 -- been compiled for inlining purposes, and the instantiation node has
8356 -- been rewritten with the instance body.
8357
8358 -- Otherwise the instantiation node appears after the declaration. If
8359 -- the entity is a formal package, the declaration may have been
8360 -- rewritten as a generic declaration (in the case of a formal with box)
8361 -- or left as a formal package declaration if it has actuals, and is
8362 -- found with a forward search.
8363
8364 if Nkind (Parent (Decl)) = N_Compilation_Unit then
8365 if Nkind (Decl) = N_Package_Declaration
8366 and then Present (Corresponding_Body (Decl))
8367 then
8368 Decl := Unit_Declaration_Node (Corresponding_Body (Decl));
8369 end if;
8370
8371 if Nkind (Original_Node (Decl)) = N_Package_Instantiation then
8372 return Original_Node (Decl);
8373 else
8374 return Unit (Parent (Decl));
8375 end if;
8376
8377 elsif Nkind (Decl) = N_Package_Declaration
8378 and then Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration
8379 then
8380 return Original_Node (Decl);
8381
8382 else
8383 Inst := Next (Decl);
8384 while not Nkind_In (Inst, N_Package_Instantiation,
8385 N_Formal_Package_Declaration)
8386 loop
8387 Next (Inst);
8388 end loop;
8389
8390 return Inst;
8391 end if;
8392 end Get_Package_Instantiation_Node;
8393
8394 ------------------------
8395 -- Has_Been_Exchanged --
8396 ------------------------
8397
8398 function Has_Been_Exchanged (E : Entity_Id) return Boolean is
8399 Next : Elmt_Id;
8400
8401 begin
8402 Next := First_Elmt (Exchanged_Views);
8403 while Present (Next) loop
8404 if Full_View (Node (Next)) = E then
8405 return True;
8406 end if;
8407
8408 Next_Elmt (Next);
8409 end loop;
8410
8411 return False;
8412 end Has_Been_Exchanged;
8413
8414 ----------
8415 -- Hash --
8416 ----------
8417
8418 function Hash (F : Entity_Id) return HTable_Range is
8419 begin
8420 return HTable_Range (F mod HTable_Size);
8421 end Hash;
8422
8423 ------------------------
8424 -- Hide_Current_Scope --
8425 ------------------------
8426
8427 procedure Hide_Current_Scope is
8428 C : constant Entity_Id := Current_Scope;
8429 E : Entity_Id;
8430
8431 begin
8432 Set_Is_Hidden_Open_Scope (C);
8433
8434 E := First_Entity (C);
8435 while Present (E) loop
8436 if Is_Immediately_Visible (E) then
8437 Set_Is_Immediately_Visible (E, False);
8438 Append_Elmt (E, Hidden_Entities);
8439 end if;
8440
8441 Next_Entity (E);
8442 end loop;
8443
8444 -- Make the scope name invisible as well. This is necessary, but might
8445 -- conflict with calls to Rtsfind later on, in case the scope is a
8446 -- predefined one. There is no clean solution to this problem, so for
8447 -- now we depend on the user not redefining Standard itself in one of
8448 -- the parent units.
8449
8450 if Is_Immediately_Visible (C) and then C /= Standard_Standard then
8451 Set_Is_Immediately_Visible (C, False);
8452 Append_Elmt (C, Hidden_Entities);
8453 end if;
8454
8455 end Hide_Current_Scope;
8456
8457 --------------
8458 -- Init_Env --
8459 --------------
8460
8461 procedure Init_Env is
8462 Saved : Instance_Env;
8463
8464 begin
8465 Saved.Instantiated_Parent := Current_Instantiated_Parent;
8466 Saved.Exchanged_Views := Exchanged_Views;
8467 Saved.Hidden_Entities := Hidden_Entities;
8468 Saved.Current_Sem_Unit := Current_Sem_Unit;
8469 Saved.Parent_Unit_Visible := Parent_Unit_Visible;
8470 Saved.Instance_Parent_Unit := Instance_Parent_Unit;
8471
8472 -- Save configuration switches. These may be reset if the unit is a
8473 -- predefined unit, and the current mode is not Ada 2005.
8474
8475 Save_Opt_Config_Switches (Saved.Switches);
8476
8477 Instance_Envs.Append (Saved);
8478
8479 Exchanged_Views := New_Elmt_List;
8480 Hidden_Entities := New_Elmt_List;
8481
8482 -- Make dummy entry for Instantiated parent. If generic unit is legal,
8483 -- this is set properly in Set_Instance_Env.
8484
8485 Current_Instantiated_Parent :=
8486 (Current_Scope, Current_Scope, Assoc_Null);
8487 end Init_Env;
8488
8489 ------------------------------
8490 -- In_Same_Declarative_Part --
8491 ------------------------------
8492
8493 function In_Same_Declarative_Part
8494 (F_Node : Node_Id;
8495 Inst : Node_Id) return Boolean
8496 is
8497 Decls : constant Node_Id := Parent (F_Node);
8498 Nod : Node_Id;
8499
8500 begin
8501 Nod := Parent (Inst);
8502 while Present (Nod) loop
8503 if Nod = Decls then
8504 return True;
8505
8506 elsif Nkind_In (Nod, N_Subprogram_Body,
8507 N_Package_Body,
8508 N_Package_Declaration,
8509 N_Task_Body,
8510 N_Protected_Body,
8511 N_Block_Statement)
8512 then
8513 return False;
8514
8515 elsif Nkind (Nod) = N_Subunit then
8516 Nod := Corresponding_Stub (Nod);
8517
8518 elsif Nkind (Nod) = N_Compilation_Unit then
8519 return False;
8520
8521 else
8522 Nod := Parent (Nod);
8523 end if;
8524 end loop;
8525
8526 return False;
8527 end In_Same_Declarative_Part;
8528
8529 ---------------------
8530 -- In_Main_Context --
8531 ---------------------
8532
8533 function In_Main_Context (E : Entity_Id) return Boolean is
8534 Context : List_Id;
8535 Clause : Node_Id;
8536 Nam : Node_Id;
8537
8538 begin
8539 if not Is_Compilation_Unit (E)
8540 or else Ekind (E) /= E_Package
8541 or else In_Private_Part (E)
8542 then
8543 return False;
8544 end if;
8545
8546 Context := Context_Items (Cunit (Main_Unit));
8547
8548 Clause := First (Context);
8549 while Present (Clause) loop
8550 if Nkind (Clause) = N_With_Clause then
8551 Nam := Name (Clause);
8552
8553 -- If the current scope is part of the context of the main unit,
8554 -- analysis of the corresponding with_clause is not complete, and
8555 -- the entity is not set. We use the Chars field directly, which
8556 -- might produce false positives in rare cases, but guarantees
8557 -- that we produce all the instance bodies we will need.
8558
8559 if (Is_Entity_Name (Nam) and then Chars (Nam) = Chars (E))
8560 or else (Nkind (Nam) = N_Selected_Component
8561 and then Chars (Selector_Name (Nam)) = Chars (E))
8562 then
8563 return True;
8564 end if;
8565 end if;
8566
8567 Next (Clause);
8568 end loop;
8569
8570 return False;
8571 end In_Main_Context;
8572
8573 ---------------------
8574 -- Inherit_Context --
8575 ---------------------
8576
8577 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id) is
8578 Current_Context : List_Id;
8579 Current_Unit : Node_Id;
8580 Item : Node_Id;
8581 New_I : Node_Id;
8582
8583 Clause : Node_Id;
8584 OK : Boolean;
8585 Lib_Unit : Node_Id;
8586
8587 begin
8588 if Nkind (Parent (Gen_Decl)) = N_Compilation_Unit then
8589
8590 -- The inherited context is attached to the enclosing compilation
8591 -- unit. This is either the main unit, or the declaration for the
8592 -- main unit (in case the instantiation appears within the package
8593 -- declaration and the main unit is its body).
8594
8595 Current_Unit := Parent (Inst);
8596 while Present (Current_Unit)
8597 and then Nkind (Current_Unit) /= N_Compilation_Unit
8598 loop
8599 Current_Unit := Parent (Current_Unit);
8600 end loop;
8601
8602 Current_Context := Context_Items (Current_Unit);
8603
8604 Item := First (Context_Items (Parent (Gen_Decl)));
8605 while Present (Item) loop
8606 if Nkind (Item) = N_With_Clause then
8607 Lib_Unit := Library_Unit (Item);
8608
8609 -- Take care to prevent direct cyclic with's
8610
8611 if Lib_Unit /= Current_Unit then
8612
8613 -- Do not add a unit if it is already in the context
8614
8615 Clause := First (Current_Context);
8616 OK := True;
8617 while Present (Clause) loop
8618 if Nkind (Clause) = N_With_Clause and then
8619 Library_Unit (Clause) = Lib_Unit
8620 then
8621 OK := False;
8622 exit;
8623 end if;
8624
8625 Next (Clause);
8626 end loop;
8627
8628 if OK then
8629 New_I := New_Copy (Item);
8630 Set_Implicit_With (New_I, True);
8631 Set_Implicit_With_From_Instantiation (New_I, True);
8632 Append (New_I, Current_Context);
8633 end if;
8634 end if;
8635 end if;
8636
8637 Next (Item);
8638 end loop;
8639 end if;
8640 end Inherit_Context;
8641
8642 ----------------
8643 -- Initialize --
8644 ----------------
8645
8646 procedure Initialize is
8647 begin
8648 Generic_Renamings.Init;
8649 Instance_Envs.Init;
8650 Generic_Flags.Init;
8651 Generic_Renamings_HTable.Reset;
8652 Circularity_Detected := False;
8653 Exchanged_Views := No_Elist;
8654 Hidden_Entities := No_Elist;
8655 end Initialize;
8656
8657 -------------------------------------
8658 -- Insert_Freeze_Node_For_Instance --
8659 -------------------------------------
8660
8661 procedure Insert_Freeze_Node_For_Instance
8662 (N : Node_Id;
8663 F_Node : Node_Id)
8664 is
8665 Decl : Node_Id;
8666 Decls : List_Id;
8667 Inst : Entity_Id;
8668 Par_N : Node_Id;
8669
8670 function Enclosing_Body (N : Node_Id) return Node_Id;
8671 -- Find enclosing package or subprogram body, if any. Freeze node may
8672 -- be placed at end of current declarative list if previous instance
8673 -- and current one have different enclosing bodies.
8674
8675 function Previous_Instance (Gen : Entity_Id) return Entity_Id;
8676 -- Find the local instance, if any, that declares the generic that is
8677 -- being instantiated. If present, the freeze node for this instance
8678 -- must follow the freeze node for the previous instance.
8679
8680 --------------------
8681 -- Enclosing_Body --
8682 --------------------
8683
8684 function Enclosing_Body (N : Node_Id) return Node_Id is
8685 P : Node_Id;
8686
8687 begin
8688 P := Parent (N);
8689 while Present (P)
8690 and then Nkind (Parent (P)) /= N_Compilation_Unit
8691 loop
8692 if Nkind_In (P, N_Package_Body, N_Subprogram_Body) then
8693 if Nkind (Parent (P)) = N_Subunit then
8694 return Corresponding_Stub (Parent (P));
8695 else
8696 return P;
8697 end if;
8698 end if;
8699
8700 P := True_Parent (P);
8701 end loop;
8702
8703 return Empty;
8704 end Enclosing_Body;
8705
8706 -----------------------
8707 -- Previous_Instance --
8708 -----------------------
8709
8710 function Previous_Instance (Gen : Entity_Id) return Entity_Id is
8711 S : Entity_Id;
8712
8713 begin
8714 S := Scope (Gen);
8715 while Present (S) and then S /= Standard_Standard loop
8716 if Is_Generic_Instance (S)
8717 and then In_Same_Source_Unit (S, N)
8718 then
8719 return S;
8720 end if;
8721
8722 S := Scope (S);
8723 end loop;
8724
8725 return Empty;
8726 end Previous_Instance;
8727
8728 -- Start of processing for Insert_Freeze_Node_For_Instance
8729
8730 begin
8731 if not Is_List_Member (F_Node) then
8732 Decl := N;
8733 Decls := List_Containing (N);
8734 Inst := Entity (F_Node);
8735 Par_N := Parent (Decls);
8736
8737 -- When processing a subprogram instantiation, utilize the actual
8738 -- subprogram instantiation rather than its package wrapper as it
8739 -- carries all the context information.
8740
8741 if Is_Wrapper_Package (Inst) then
8742 Inst := Related_Instance (Inst);
8743 end if;
8744
8745 -- If this is a package instance, check whether the generic is
8746 -- declared in a previous instance and the current instance is
8747 -- not within the previous one.
8748
8749 if Present (Generic_Parent (Parent (Inst)))
8750 and then Is_In_Main_Unit (N)
8751 then
8752 declare
8753 Enclosing_N : constant Node_Id := Enclosing_Body (N);
8754 Par_I : constant Entity_Id :=
8755 Previous_Instance
8756 (Generic_Parent (Parent (Inst)));
8757 Scop : Entity_Id;
8758
8759 begin
8760 if Present (Par_I)
8761 and then Earlier (N, Freeze_Node (Par_I))
8762 then
8763 Scop := Scope (Inst);
8764
8765 -- If the current instance is within the one that contains
8766 -- the generic, the freeze node for the current one must
8767 -- appear in the current declarative part. Ditto, if the
8768 -- current instance is within another package instance or
8769 -- within a body that does not enclose the current instance.
8770 -- In these three cases the freeze node of the previous
8771 -- instance is not relevant.
8772
8773 while Present (Scop) and then Scop /= Standard_Standard loop
8774 exit when Scop = Par_I
8775 or else
8776 (Is_Generic_Instance (Scop)
8777 and then Scope_Depth (Scop) > Scope_Depth (Par_I));
8778 Scop := Scope (Scop);
8779 end loop;
8780
8781 -- Previous instance encloses current instance
8782
8783 if Scop = Par_I then
8784 null;
8785
8786 -- If the next node is a source body we must freeze in
8787 -- the current scope as well.
8788
8789 elsif Present (Next (N))
8790 and then Nkind_In (Next (N), N_Subprogram_Body,
8791 N_Package_Body)
8792 and then Comes_From_Source (Next (N))
8793 then
8794 null;
8795
8796 -- Current instance is within an unrelated instance
8797
8798 elsif Is_Generic_Instance (Scop) then
8799 null;
8800
8801 -- Current instance is within an unrelated body
8802
8803 elsif Present (Enclosing_N)
8804 and then Enclosing_N /= Enclosing_Body (Par_I)
8805 then
8806 null;
8807
8808 else
8809 Insert_After (Freeze_Node (Par_I), F_Node);
8810 return;
8811 end if;
8812 end if;
8813 end;
8814 end if;
8815
8816 -- When the instantiation occurs in a package declaration, append the
8817 -- freeze node to the private declarations (if any).
8818
8819 if Nkind (Par_N) = N_Package_Specification
8820 and then Decls = Visible_Declarations (Par_N)
8821 and then Present (Private_Declarations (Par_N))
8822 and then not Is_Empty_List (Private_Declarations (Par_N))
8823 then
8824 Decls := Private_Declarations (Par_N);
8825 Decl := First (Decls);
8826 end if;
8827
8828 -- Determine the proper freeze point of a package instantiation. We
8829 -- adhere to the general rule of a package or subprogram body causing
8830 -- freezing of anything before it in the same declarative region. In
8831 -- this case, the proper freeze point of a package instantiation is
8832 -- before the first source body which follows, or before a stub. This
8833 -- ensures that entities coming from the instance are already frozen
8834 -- and usable in source bodies.
8835
8836 if Nkind (Par_N) /= N_Package_Declaration
8837 and then Ekind (Inst) = E_Package
8838 and then Is_Generic_Instance (Inst)
8839 and then
8840 not In_Same_Source_Unit (Generic_Parent (Parent (Inst)), Inst)
8841 then
8842 while Present (Decl) loop
8843 if (Nkind (Decl) in N_Unit_Body
8844 or else
8845 Nkind (Decl) in N_Body_Stub)
8846 and then Comes_From_Source (Decl)
8847 then
8848 Insert_Before (Decl, F_Node);
8849 return;
8850 end if;
8851
8852 Next (Decl);
8853 end loop;
8854 end if;
8855
8856 -- In a package declaration, or if no previous body, insert at end
8857 -- of list.
8858
8859 Set_Sloc (F_Node, Sloc (Last (Decls)));
8860 Insert_After (Last (Decls), F_Node);
8861 end if;
8862 end Insert_Freeze_Node_For_Instance;
8863
8864 ------------------
8865 -- Install_Body --
8866 ------------------
8867
8868 procedure Install_Body
8869 (Act_Body : Node_Id;
8870 N : Node_Id;
8871 Gen_Body : Node_Id;
8872 Gen_Decl : Node_Id)
8873 is
8874 Act_Id : constant Entity_Id := Corresponding_Spec (Act_Body);
8875 Act_Unit : constant Node_Id := Unit (Cunit (Get_Source_Unit (N)));
8876 Gen_Id : constant Entity_Id := Corresponding_Spec (Gen_Body);
8877 Par : constant Entity_Id := Scope (Gen_Id);
8878 Gen_Unit : constant Node_Id :=
8879 Unit (Cunit (Get_Source_Unit (Gen_Decl)));
8880 Orig_Body : Node_Id := Gen_Body;
8881 F_Node : Node_Id;
8882 Body_Unit : Node_Id;
8883
8884 Must_Delay : Boolean;
8885
8886 function In_Same_Enclosing_Subp return Boolean;
8887 -- Check whether instance and generic body are within same subprogram.
8888
8889 function True_Sloc (N : Node_Id) return Source_Ptr;
8890 -- If the instance is nested inside a generic unit, the Sloc of the
8891 -- instance indicates the place of the original definition, not the
8892 -- point of the current enclosing instance. Pending a better usage of
8893 -- Slocs to indicate instantiation places, we determine the place of
8894 -- origin of a node by finding the maximum sloc of any ancestor node.
8895 -- Why is this not equivalent to Top_Level_Location ???
8896
8897 ----------------------------
8898 -- In_Same_Enclosing_Subp --
8899 ----------------------------
8900
8901 function In_Same_Enclosing_Subp return Boolean is
8902 Scop : Entity_Id;
8903 Subp : Entity_Id;
8904
8905 begin
8906 Scop := Scope (Act_Id);
8907 while Scop /= Standard_Standard
8908 and then not Is_Overloadable (Scop)
8909 loop
8910 Scop := Scope (Scop);
8911 end loop;
8912
8913 if Scop = Standard_Standard then
8914 return False;
8915 else
8916 Subp := Scop;
8917 end if;
8918
8919 Scop := Scope (Gen_Id);
8920 while Scop /= Standard_Standard loop
8921 if Scop = Subp then
8922 return True;
8923 else
8924 Scop := Scope (Scop);
8925 end if;
8926 end loop;
8927
8928 return False;
8929 end In_Same_Enclosing_Subp;
8930
8931 ---------------
8932 -- True_Sloc --
8933 ---------------
8934
8935 function True_Sloc (N : Node_Id) return Source_Ptr is
8936 Res : Source_Ptr;
8937 N1 : Node_Id;
8938
8939 begin
8940 Res := Sloc (N);
8941 N1 := N;
8942 while Present (N1) and then N1 /= Act_Unit loop
8943 if Sloc (N1) > Res then
8944 Res := Sloc (N1);
8945 end if;
8946
8947 N1 := Parent (N1);
8948 end loop;
8949
8950 return Res;
8951 end True_Sloc;
8952
8953 -- Start of processing for Install_Body
8954
8955 begin
8956 -- Handle first the case of an instance with incomplete actual types.
8957 -- The instance body cannot be placed after the declaration because
8958 -- full views have not been seen yet. Any use of the non-limited views
8959 -- in the instance body requires the presence of a regular with_clause
8960 -- in the enclosing unit, and will fail if this with_clause is missing.
8961 -- We place the instance body at the beginning of the enclosing body,
8962 -- which is the unit being compiled. The freeze node for the instance
8963 -- is then placed after the instance body.
8964
8965 if not Is_Empty_Elmt_List (Incomplete_Actuals (Act_Id))
8966 and then Expander_Active
8967 and then Ekind (Scope (Act_Id)) = E_Package
8968 then
8969 declare
8970 Scop : constant Entity_Id := Scope (Act_Id);
8971 Body_Id : constant Node_Id :=
8972 Corresponding_Body (Unit_Declaration_Node (Scop));
8973
8974 begin
8975 Ensure_Freeze_Node (Act_Id);
8976 F_Node := Freeze_Node (Act_Id);
8977 if Present (Body_Id) then
8978 Set_Is_Frozen (Act_Id, False);
8979 Prepend (Act_Body, Declarations (Parent (Body_Id)));
8980 if Is_List_Member (F_Node) then
8981 Remove (F_Node);
8982 end if;
8983
8984 Insert_After (Act_Body, F_Node);
8985 end if;
8986 end;
8987 return;
8988 end if;
8989
8990 -- If the body is a subunit, the freeze point is the corresponding stub
8991 -- in the current compilation, not the subunit itself.
8992
8993 if Nkind (Parent (Gen_Body)) = N_Subunit then
8994 Orig_Body := Corresponding_Stub (Parent (Gen_Body));
8995 else
8996 Orig_Body := Gen_Body;
8997 end if;
8998
8999 Body_Unit := Unit (Cunit (Get_Source_Unit (Orig_Body)));
9000
9001 -- If the instantiation and the generic definition appear in the same
9002 -- package declaration, this is an early instantiation. If they appear
9003 -- in the same declarative part, it is an early instantiation only if
9004 -- the generic body appears textually later, and the generic body is
9005 -- also in the main unit.
9006
9007 -- If instance is nested within a subprogram, and the generic body
9008 -- is not, the instance is delayed because the enclosing body is. If
9009 -- instance and body are within the same scope, or the same subprogram
9010 -- body, indicate explicitly that the instance is delayed.
9011
9012 Must_Delay :=
9013 (Gen_Unit = Act_Unit
9014 and then (Nkind_In (Gen_Unit, N_Package_Declaration,
9015 N_Generic_Package_Declaration)
9016 or else (Gen_Unit = Body_Unit
9017 and then True_Sloc (N) < Sloc (Orig_Body)))
9018 and then Is_In_Main_Unit (Gen_Unit)
9019 and then (Scope (Act_Id) = Scope (Gen_Id)
9020 or else In_Same_Enclosing_Subp));
9021
9022 -- If this is an early instantiation, the freeze node is placed after
9023 -- the generic body. Otherwise, if the generic appears in an instance,
9024 -- we cannot freeze the current instance until the outer one is frozen.
9025 -- This is only relevant if the current instance is nested within some
9026 -- inner scope not itself within the outer instance. If this scope is
9027 -- a package body in the same declarative part as the outer instance,
9028 -- then that body needs to be frozen after the outer instance. Finally,
9029 -- if no delay is needed, we place the freeze node at the end of the
9030 -- current declarative part.
9031
9032 if Expander_Active then
9033 Ensure_Freeze_Node (Act_Id);
9034 F_Node := Freeze_Node (Act_Id);
9035
9036 if Must_Delay then
9037 Insert_After (Orig_Body, F_Node);
9038
9039 elsif Is_Generic_Instance (Par)
9040 and then Present (Freeze_Node (Par))
9041 and then Scope (Act_Id) /= Par
9042 then
9043 -- Freeze instance of inner generic after instance of enclosing
9044 -- generic.
9045
9046 if In_Same_Declarative_Part (Freeze_Node (Par), N) then
9047
9048 -- Handle the following case:
9049
9050 -- package Parent_Inst is new ...
9051 -- Parent_Inst []
9052
9053 -- procedure P ... -- this body freezes Parent_Inst
9054
9055 -- package Inst is new ...
9056
9057 -- In this particular scenario, the freeze node for Inst must
9058 -- be inserted in the same manner as that of Parent_Inst,
9059 -- before the next source body or at the end of the declarative
9060 -- list (body not available). If body P did not exist and
9061 -- Parent_Inst was frozen after Inst, either by a body
9062 -- following Inst or at the end of the declarative region,
9063 -- the freeze node for Inst must be inserted after that of
9064 -- Parent_Inst. This relation is established by comparing
9065 -- the Slocs of Parent_Inst freeze node and Inst.
9066
9067 if List_Containing (Get_Package_Instantiation_Node (Par)) =
9068 List_Containing (N)
9069 and then Sloc (Freeze_Node (Par)) < Sloc (N)
9070 then
9071 Insert_Freeze_Node_For_Instance (N, F_Node);
9072 else
9073 Insert_After (Freeze_Node (Par), F_Node);
9074 end if;
9075
9076 -- Freeze package enclosing instance of inner generic after
9077 -- instance of enclosing generic.
9078
9079 elsif Nkind_In (Parent (N), N_Package_Body, N_Subprogram_Body)
9080 and then In_Same_Declarative_Part (Freeze_Node (Par), Parent (N))
9081 then
9082 declare
9083 Enclosing : Entity_Id;
9084
9085 begin
9086 Enclosing := Corresponding_Spec (Parent (N));
9087
9088 if No (Enclosing) then
9089 Enclosing := Defining_Entity (Parent (N));
9090 end if;
9091
9092 Insert_Freeze_Node_For_Instance (N, F_Node);
9093 Ensure_Freeze_Node (Enclosing);
9094
9095 if not Is_List_Member (Freeze_Node (Enclosing)) then
9096
9097 -- The enclosing context is a subunit, insert the freeze
9098 -- node after the stub.
9099
9100 if Nkind (Parent (Parent (N))) = N_Subunit then
9101 Insert_Freeze_Node_For_Instance
9102 (Corresponding_Stub (Parent (Parent (N))),
9103 Freeze_Node (Enclosing));
9104
9105 -- The enclosing context is a package with a stub body
9106 -- which has already been replaced by the real body.
9107 -- Insert the freeze node after the actual body.
9108
9109 elsif Ekind (Enclosing) = E_Package
9110 and then Present (Body_Entity (Enclosing))
9111 and then Was_Originally_Stub
9112 (Parent (Body_Entity (Enclosing)))
9113 then
9114 Insert_Freeze_Node_For_Instance
9115 (Parent (Body_Entity (Enclosing)),
9116 Freeze_Node (Enclosing));
9117
9118 -- The parent instance has been frozen before the body of
9119 -- the enclosing package, insert the freeze node after
9120 -- the body.
9121
9122 elsif List_Containing (Freeze_Node (Par)) =
9123 List_Containing (Parent (N))
9124 and then Sloc (Freeze_Node (Par)) < Sloc (Parent (N))
9125 then
9126 Insert_Freeze_Node_For_Instance
9127 (Parent (N), Freeze_Node (Enclosing));
9128
9129 else
9130 Insert_After
9131 (Freeze_Node (Par), Freeze_Node (Enclosing));
9132 end if;
9133 end if;
9134 end;
9135
9136 else
9137 Insert_Freeze_Node_For_Instance (N, F_Node);
9138 end if;
9139
9140 else
9141 Insert_Freeze_Node_For_Instance (N, F_Node);
9142 end if;
9143 end if;
9144
9145 Set_Is_Frozen (Act_Id);
9146 Insert_Before (N, Act_Body);
9147 Mark_Rewrite_Insertion (Act_Body);
9148 end Install_Body;
9149
9150 -----------------------------
9151 -- Install_Formal_Packages --
9152 -----------------------------
9153
9154 procedure Install_Formal_Packages (Par : Entity_Id) is
9155 E : Entity_Id;
9156 Gen : Entity_Id;
9157 Gen_E : Entity_Id := Empty;
9158
9159 begin
9160 E := First_Entity (Par);
9161
9162 -- If we are installing an instance parent, locate the formal packages
9163 -- of its generic parent.
9164
9165 if Is_Generic_Instance (Par) then
9166 Gen := Generic_Parent (Package_Specification (Par));
9167 Gen_E := First_Entity (Gen);
9168 end if;
9169
9170 while Present (E) loop
9171 if Ekind (E) = E_Package
9172 and then Nkind (Parent (E)) = N_Package_Renaming_Declaration
9173 then
9174 -- If this is the renaming for the parent instance, done
9175
9176 if Renamed_Object (E) = Par then
9177 exit;
9178
9179 -- The visibility of a formal of an enclosing generic is already
9180 -- correct.
9181
9182 elsif Denotes_Formal_Package (E) then
9183 null;
9184
9185 elsif Present (Associated_Formal_Package (E)) then
9186 Check_Generic_Actuals (Renamed_Object (E), True);
9187 Set_Is_Hidden (E, False);
9188
9189 -- Find formal package in generic unit that corresponds to
9190 -- (instance of) formal package in instance.
9191
9192 while Present (Gen_E) and then Chars (Gen_E) /= Chars (E) loop
9193 Next_Entity (Gen_E);
9194 end loop;
9195
9196 if Present (Gen_E) then
9197 Map_Formal_Package_Entities (Gen_E, E);
9198 end if;
9199 end if;
9200 end if;
9201
9202 Next_Entity (E);
9203
9204 if Present (Gen_E) then
9205 Next_Entity (Gen_E);
9206 end if;
9207 end loop;
9208 end Install_Formal_Packages;
9209
9210 --------------------
9211 -- Install_Parent --
9212 --------------------
9213
9214 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False) is
9215 Ancestors : constant Elist_Id := New_Elmt_List;
9216 S : constant Entity_Id := Current_Scope;
9217 Inst_Par : Entity_Id;
9218 First_Par : Entity_Id;
9219 Inst_Node : Node_Id;
9220 Gen_Par : Entity_Id;
9221 First_Gen : Entity_Id;
9222 Elmt : Elmt_Id;
9223
9224 procedure Install_Noninstance_Specs (Par : Entity_Id);
9225 -- Install the scopes of noninstance parent units ending with Par
9226
9227 procedure Install_Spec (Par : Entity_Id);
9228 -- The child unit is within the declarative part of the parent, so the
9229 -- declarations within the parent are immediately visible.
9230
9231 -------------------------------
9232 -- Install_Noninstance_Specs --
9233 -------------------------------
9234
9235 procedure Install_Noninstance_Specs (Par : Entity_Id) is
9236 begin
9237 if Present (Par)
9238 and then Par /= Standard_Standard
9239 and then not In_Open_Scopes (Par)
9240 then
9241 Install_Noninstance_Specs (Scope (Par));
9242 Install_Spec (Par);
9243 end if;
9244 end Install_Noninstance_Specs;
9245
9246 ------------------
9247 -- Install_Spec --
9248 ------------------
9249
9250 procedure Install_Spec (Par : Entity_Id) is
9251 Spec : constant Node_Id := Package_Specification (Par);
9252
9253 begin
9254 -- If this parent of the child instance is a top-level unit,
9255 -- then record the unit and its visibility for later resetting in
9256 -- Remove_Parent. We exclude units that are generic instances, as we
9257 -- only want to record this information for the ultimate top-level
9258 -- noninstance parent (is that always correct???).
9259
9260 if Scope (Par) = Standard_Standard
9261 and then not Is_Generic_Instance (Par)
9262 then
9263 Parent_Unit_Visible := Is_Immediately_Visible (Par);
9264 Instance_Parent_Unit := Par;
9265 end if;
9266
9267 -- Open the parent scope and make it and its declarations visible.
9268 -- If this point is not within a body, then only the visible
9269 -- declarations should be made visible, and installation of the
9270 -- private declarations is deferred until the appropriate point
9271 -- within analysis of the spec being instantiated (see the handling
9272 -- of parent visibility in Analyze_Package_Specification). This is
9273 -- relaxed in the case where the parent unit is Ada.Tags, to avoid
9274 -- private view problems that occur when compiling instantiations of
9275 -- a generic child of that package (Generic_Dispatching_Constructor).
9276 -- If the instance freezes a tagged type, inlinings of operations
9277 -- from Ada.Tags may need the full view of type Tag. If inlining took
9278 -- proper account of establishing visibility of inlined subprograms'
9279 -- parents then it should be possible to remove this
9280 -- special check. ???
9281
9282 Push_Scope (Par);
9283 Set_Is_Immediately_Visible (Par);
9284 Install_Visible_Declarations (Par);
9285 Set_Use (Visible_Declarations (Spec));
9286
9287 if In_Body or else Is_RTU (Par, Ada_Tags) then
9288 Install_Private_Declarations (Par);
9289 Set_Use (Private_Declarations (Spec));
9290 end if;
9291 end Install_Spec;
9292
9293 -- Start of processing for Install_Parent
9294
9295 begin
9296 -- We need to install the parent instance to compile the instantiation
9297 -- of the child, but the child instance must appear in the current
9298 -- scope. Given that we cannot place the parent above the current scope
9299 -- in the scope stack, we duplicate the current scope and unstack both
9300 -- after the instantiation is complete.
9301
9302 -- If the parent is itself the instantiation of a child unit, we must
9303 -- also stack the instantiation of its parent, and so on. Each such
9304 -- ancestor is the prefix of the name in a prior instantiation.
9305
9306 -- If this is a nested instance, the parent unit itself resolves to
9307 -- a renaming of the parent instance, whose declaration we need.
9308
9309 -- Finally, the parent may be a generic (not an instance) when the
9310 -- child unit appears as a formal package.
9311
9312 Inst_Par := P;
9313
9314 if Present (Renamed_Entity (Inst_Par)) then
9315 Inst_Par := Renamed_Entity (Inst_Par);
9316 end if;
9317
9318 First_Par := Inst_Par;
9319
9320 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
9321
9322 First_Gen := Gen_Par;
9323
9324 while Present (Gen_Par) and then Is_Child_Unit (Gen_Par) loop
9325
9326 -- Load grandparent instance as well
9327
9328 Inst_Node := Get_Package_Instantiation_Node (Inst_Par);
9329
9330 if Nkind (Name (Inst_Node)) = N_Expanded_Name then
9331 Inst_Par := Entity (Prefix (Name (Inst_Node)));
9332
9333 if Present (Renamed_Entity (Inst_Par)) then
9334 Inst_Par := Renamed_Entity (Inst_Par);
9335 end if;
9336
9337 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
9338
9339 if Present (Gen_Par) then
9340 Prepend_Elmt (Inst_Par, Ancestors);
9341
9342 else
9343 -- Parent is not the name of an instantiation
9344
9345 Install_Noninstance_Specs (Inst_Par);
9346 exit;
9347 end if;
9348
9349 else
9350 -- Previous error
9351
9352 exit;
9353 end if;
9354 end loop;
9355
9356 if Present (First_Gen) then
9357 Append_Elmt (First_Par, Ancestors);
9358 else
9359 Install_Noninstance_Specs (First_Par);
9360 end if;
9361
9362 if not Is_Empty_Elmt_List (Ancestors) then
9363 Elmt := First_Elmt (Ancestors);
9364 while Present (Elmt) loop
9365 Install_Spec (Node (Elmt));
9366 Install_Formal_Packages (Node (Elmt));
9367 Next_Elmt (Elmt);
9368 end loop;
9369 end if;
9370
9371 if not In_Body then
9372 Push_Scope (S);
9373 end if;
9374 end Install_Parent;
9375
9376 -------------------------------
9377 -- Install_Hidden_Primitives --
9378 -------------------------------
9379
9380 procedure Install_Hidden_Primitives
9381 (Prims_List : in out Elist_Id;
9382 Gen_T : Entity_Id;
9383 Act_T : Entity_Id)
9384 is
9385 Elmt : Elmt_Id;
9386 List : Elist_Id := No_Elist;
9387 Prim_G_Elmt : Elmt_Id;
9388 Prim_A_Elmt : Elmt_Id;
9389 Prim_G : Node_Id;
9390 Prim_A : Node_Id;
9391
9392 begin
9393 -- No action needed in case of serious errors because we cannot trust
9394 -- in the order of primitives
9395
9396 if Serious_Errors_Detected > 0 then
9397 return;
9398
9399 -- No action possible if we don't have available the list of primitive
9400 -- operations
9401
9402 elsif No (Gen_T)
9403 or else not Is_Record_Type (Gen_T)
9404 or else not Is_Tagged_Type (Gen_T)
9405 or else not Is_Record_Type (Act_T)
9406 or else not Is_Tagged_Type (Act_T)
9407 then
9408 return;
9409
9410 -- There is no need to handle interface types since their primitives
9411 -- cannot be hidden
9412
9413 elsif Is_Interface (Gen_T) then
9414 return;
9415 end if;
9416
9417 Prim_G_Elmt := First_Elmt (Primitive_Operations (Gen_T));
9418
9419 if not Is_Class_Wide_Type (Act_T) then
9420 Prim_A_Elmt := First_Elmt (Primitive_Operations (Act_T));
9421 else
9422 Prim_A_Elmt := First_Elmt (Primitive_Operations (Root_Type (Act_T)));
9423 end if;
9424
9425 loop
9426 -- Skip predefined primitives in the generic formal
9427
9428 while Present (Prim_G_Elmt)
9429 and then Is_Predefined_Dispatching_Operation (Node (Prim_G_Elmt))
9430 loop
9431 Next_Elmt (Prim_G_Elmt);
9432 end loop;
9433
9434 -- Skip predefined primitives in the generic actual
9435
9436 while Present (Prim_A_Elmt)
9437 and then Is_Predefined_Dispatching_Operation (Node (Prim_A_Elmt))
9438 loop
9439 Next_Elmt (Prim_A_Elmt);
9440 end loop;
9441
9442 exit when No (Prim_G_Elmt) or else No (Prim_A_Elmt);
9443
9444 Prim_G := Node (Prim_G_Elmt);
9445 Prim_A := Node (Prim_A_Elmt);
9446
9447 -- There is no need to handle interface primitives because their
9448 -- primitives are not hidden
9449
9450 exit when Present (Interface_Alias (Prim_G));
9451
9452 -- Here we install one hidden primitive
9453
9454 if Chars (Prim_G) /= Chars (Prim_A)
9455 and then Has_Suffix (Prim_A, 'P')
9456 and then Remove_Suffix (Prim_A, 'P') = Chars (Prim_G)
9457 then
9458 Set_Chars (Prim_A, Chars (Prim_G));
9459 Append_New_Elmt (Prim_A, To => List);
9460 end if;
9461
9462 Next_Elmt (Prim_A_Elmt);
9463 Next_Elmt (Prim_G_Elmt);
9464 end loop;
9465
9466 -- Append the elements to the list of temporarily visible primitives
9467 -- avoiding duplicates.
9468
9469 if Present (List) then
9470 if No (Prims_List) then
9471 Prims_List := New_Elmt_List;
9472 end if;
9473
9474 Elmt := First_Elmt (List);
9475 while Present (Elmt) loop
9476 Append_Unique_Elmt (Node (Elmt), Prims_List);
9477 Next_Elmt (Elmt);
9478 end loop;
9479 end if;
9480 end Install_Hidden_Primitives;
9481
9482 -------------------------------
9483 -- Restore_Hidden_Primitives --
9484 -------------------------------
9485
9486 procedure Restore_Hidden_Primitives (Prims_List : in out Elist_Id) is
9487 Prim_Elmt : Elmt_Id;
9488 Prim : Node_Id;
9489
9490 begin
9491 if Prims_List /= No_Elist then
9492 Prim_Elmt := First_Elmt (Prims_List);
9493 while Present (Prim_Elmt) loop
9494 Prim := Node (Prim_Elmt);
9495 Set_Chars (Prim, Add_Suffix (Prim, 'P'));
9496 Next_Elmt (Prim_Elmt);
9497 end loop;
9498
9499 Prims_List := No_Elist;
9500 end if;
9501 end Restore_Hidden_Primitives;
9502
9503 --------------------------------
9504 -- Instantiate_Formal_Package --
9505 --------------------------------
9506
9507 function Instantiate_Formal_Package
9508 (Formal : Node_Id;
9509 Actual : Node_Id;
9510 Analyzed_Formal : Node_Id) return List_Id
9511 is
9512 Loc : constant Source_Ptr := Sloc (Actual);
9513 Actual_Pack : Entity_Id;
9514 Formal_Pack : Entity_Id;
9515 Gen_Parent : Entity_Id;
9516 Decls : List_Id;
9517 Nod : Node_Id;
9518 Parent_Spec : Node_Id;
9519
9520 procedure Find_Matching_Actual
9521 (F : Node_Id;
9522 Act : in out Entity_Id);
9523 -- We need to associate each formal entity in the formal package with
9524 -- the corresponding entity in the actual package. The actual package
9525 -- has been analyzed and possibly expanded, and as a result there is
9526 -- no one-to-one correspondence between the two lists (for example,
9527 -- the actual may include subtypes, itypes, and inherited primitive
9528 -- operations, interspersed among the renaming declarations for the
9529 -- actuals). We retrieve the corresponding actual by name because each
9530 -- actual has the same name as the formal, and they do appear in the
9531 -- same order.
9532
9533 function Get_Formal_Entity (N : Node_Id) return Entity_Id;
9534 -- Retrieve entity of defining entity of generic formal parameter.
9535 -- Only the declarations of formals need to be considered when
9536 -- linking them to actuals, but the declarative list may include
9537 -- internal entities generated during analysis, and those are ignored.
9538
9539 procedure Match_Formal_Entity
9540 (Formal_Node : Node_Id;
9541 Formal_Ent : Entity_Id;
9542 Actual_Ent : Entity_Id);
9543 -- Associates the formal entity with the actual. In the case where
9544 -- Formal_Ent is a formal package, this procedure iterates through all
9545 -- of its formals and enters associations between the actuals occurring
9546 -- in the formal package's corresponding actual package (given by
9547 -- Actual_Ent) and the formal package's formal parameters. This
9548 -- procedure recurses if any of the parameters is itself a package.
9549
9550 function Is_Instance_Of
9551 (Act_Spec : Entity_Id;
9552 Gen_Anc : Entity_Id) return Boolean;
9553 -- The actual can be an instantiation of a generic within another
9554 -- instance, in which case there is no direct link from it to the
9555 -- original generic ancestor. In that case, we recognize that the
9556 -- ultimate ancestor is the same by examining names and scopes.
9557
9558 procedure Process_Nested_Formal (Formal : Entity_Id);
9559 -- If the current formal is declared with a box, its own formals are
9560 -- visible in the instance, as they were in the generic, and their
9561 -- Hidden flag must be reset. If some of these formals are themselves
9562 -- packages declared with a box, the processing must be recursive.
9563
9564 --------------------------
9565 -- Find_Matching_Actual --
9566 --------------------------
9567
9568 procedure Find_Matching_Actual
9569 (F : Node_Id;
9570 Act : in out Entity_Id)
9571 is
9572 Formal_Ent : Entity_Id;
9573
9574 begin
9575 case Nkind (Original_Node (F)) is
9576 when N_Formal_Object_Declaration |
9577 N_Formal_Type_Declaration =>
9578 Formal_Ent := Defining_Identifier (F);
9579
9580 while Chars (Act) /= Chars (Formal_Ent) loop
9581 Next_Entity (Act);
9582 end loop;
9583
9584 when N_Formal_Subprogram_Declaration |
9585 N_Formal_Package_Declaration |
9586 N_Package_Declaration |
9587 N_Generic_Package_Declaration =>
9588 Formal_Ent := Defining_Entity (F);
9589
9590 while Chars (Act) /= Chars (Formal_Ent) loop
9591 Next_Entity (Act);
9592 end loop;
9593
9594 when others =>
9595 raise Program_Error;
9596 end case;
9597 end Find_Matching_Actual;
9598
9599 -------------------------
9600 -- Match_Formal_Entity --
9601 -------------------------
9602
9603 procedure Match_Formal_Entity
9604 (Formal_Node : Node_Id;
9605 Formal_Ent : Entity_Id;
9606 Actual_Ent : Entity_Id)
9607 is
9608 Act_Pkg : Entity_Id;
9609
9610 begin
9611 Set_Instance_Of (Formal_Ent, Actual_Ent);
9612
9613 if Ekind (Actual_Ent) = E_Package then
9614
9615 -- Record associations for each parameter
9616
9617 Act_Pkg := Actual_Ent;
9618
9619 declare
9620 A_Ent : Entity_Id := First_Entity (Act_Pkg);
9621 F_Ent : Entity_Id;
9622 F_Node : Node_Id;
9623
9624 Gen_Decl : Node_Id;
9625 Formals : List_Id;
9626 Actual : Entity_Id;
9627
9628 begin
9629 -- Retrieve the actual given in the formal package declaration
9630
9631 Actual := Entity (Name (Original_Node (Formal_Node)));
9632
9633 -- The actual in the formal package declaration may be a
9634 -- renamed generic package, in which case we want to retrieve
9635 -- the original generic in order to traverse its formal part.
9636
9637 if Present (Renamed_Entity (Actual)) then
9638 Gen_Decl := Unit_Declaration_Node (Renamed_Entity (Actual));
9639 else
9640 Gen_Decl := Unit_Declaration_Node (Actual);
9641 end if;
9642
9643 Formals := Generic_Formal_Declarations (Gen_Decl);
9644
9645 if Present (Formals) then
9646 F_Node := First_Non_Pragma (Formals);
9647 else
9648 F_Node := Empty;
9649 end if;
9650
9651 while Present (A_Ent)
9652 and then Present (F_Node)
9653 and then A_Ent /= First_Private_Entity (Act_Pkg)
9654 loop
9655 F_Ent := Get_Formal_Entity (F_Node);
9656
9657 if Present (F_Ent) then
9658
9659 -- This is a formal of the original package. Record
9660 -- association and recurse.
9661
9662 Find_Matching_Actual (F_Node, A_Ent);
9663 Match_Formal_Entity (F_Node, F_Ent, A_Ent);
9664 Next_Entity (A_Ent);
9665 end if;
9666
9667 Next_Non_Pragma (F_Node);
9668 end loop;
9669 end;
9670 end if;
9671 end Match_Formal_Entity;
9672
9673 -----------------------
9674 -- Get_Formal_Entity --
9675 -----------------------
9676
9677 function Get_Formal_Entity (N : Node_Id) return Entity_Id is
9678 Kind : constant Node_Kind := Nkind (Original_Node (N));
9679 begin
9680 case Kind is
9681 when N_Formal_Object_Declaration =>
9682 return Defining_Identifier (N);
9683
9684 when N_Formal_Type_Declaration =>
9685 return Defining_Identifier (N);
9686
9687 when N_Formal_Subprogram_Declaration =>
9688 return Defining_Unit_Name (Specification (N));
9689
9690 when N_Formal_Package_Declaration =>
9691 return Defining_Identifier (Original_Node (N));
9692
9693 when N_Generic_Package_Declaration =>
9694 return Defining_Identifier (Original_Node (N));
9695
9696 -- All other declarations are introduced by semantic analysis and
9697 -- have no match in the actual.
9698
9699 when others =>
9700 return Empty;
9701 end case;
9702 end Get_Formal_Entity;
9703
9704 --------------------
9705 -- Is_Instance_Of --
9706 --------------------
9707
9708 function Is_Instance_Of
9709 (Act_Spec : Entity_Id;
9710 Gen_Anc : Entity_Id) return Boolean
9711 is
9712 Gen_Par : constant Entity_Id := Generic_Parent (Act_Spec);
9713
9714 begin
9715 if No (Gen_Par) then
9716 return False;
9717
9718 -- Simplest case: the generic parent of the actual is the formal
9719
9720 elsif Gen_Par = Gen_Anc then
9721 return True;
9722
9723 elsif Chars (Gen_Par) /= Chars (Gen_Anc) then
9724 return False;
9725
9726 -- The actual may be obtained through several instantiations. Its
9727 -- scope must itself be an instance of a generic declared in the
9728 -- same scope as the formal. Any other case is detected above.
9729
9730 elsif not Is_Generic_Instance (Scope (Gen_Par)) then
9731 return False;
9732
9733 else
9734 return Generic_Parent (Parent (Scope (Gen_Par))) = Scope (Gen_Anc);
9735 end if;
9736 end Is_Instance_Of;
9737
9738 ---------------------------
9739 -- Process_Nested_Formal --
9740 ---------------------------
9741
9742 procedure Process_Nested_Formal (Formal : Entity_Id) is
9743 Ent : Entity_Id;
9744
9745 begin
9746 if Present (Associated_Formal_Package (Formal))
9747 and then Box_Present (Parent (Associated_Formal_Package (Formal)))
9748 then
9749 Ent := First_Entity (Formal);
9750 while Present (Ent) loop
9751 Set_Is_Hidden (Ent, False);
9752 Set_Is_Visible_Formal (Ent);
9753 Set_Is_Potentially_Use_Visible
9754 (Ent, Is_Potentially_Use_Visible (Formal));
9755
9756 if Ekind (Ent) = E_Package then
9757 exit when Renamed_Entity (Ent) = Renamed_Entity (Formal);
9758 Process_Nested_Formal (Ent);
9759 end if;
9760
9761 Next_Entity (Ent);
9762 end loop;
9763 end if;
9764 end Process_Nested_Formal;
9765
9766 -- Start of processing for Instantiate_Formal_Package
9767
9768 begin
9769 Analyze (Actual);
9770
9771 if not Is_Entity_Name (Actual)
9772 or else Ekind (Entity (Actual)) /= E_Package
9773 then
9774 Error_Msg_N
9775 ("expect package instance to instantiate formal", Actual);
9776 Abandon_Instantiation (Actual);
9777 raise Program_Error;
9778
9779 else
9780 Actual_Pack := Entity (Actual);
9781 Set_Is_Instantiated (Actual_Pack);
9782
9783 -- The actual may be a renamed package, or an outer generic formal
9784 -- package whose instantiation is converted into a renaming.
9785
9786 if Present (Renamed_Object (Actual_Pack)) then
9787 Actual_Pack := Renamed_Object (Actual_Pack);
9788 end if;
9789
9790 if Nkind (Analyzed_Formal) = N_Formal_Package_Declaration then
9791 Gen_Parent := Get_Instance_Of (Entity (Name (Analyzed_Formal)));
9792 Formal_Pack := Defining_Identifier (Analyzed_Formal);
9793 else
9794 Gen_Parent :=
9795 Generic_Parent (Specification (Analyzed_Formal));
9796 Formal_Pack :=
9797 Defining_Unit_Name (Specification (Analyzed_Formal));
9798 end if;
9799
9800 if Nkind (Parent (Actual_Pack)) = N_Defining_Program_Unit_Name then
9801 Parent_Spec := Package_Specification (Actual_Pack);
9802 else
9803 Parent_Spec := Parent (Actual_Pack);
9804 end if;
9805
9806 if Gen_Parent = Any_Id then
9807 Error_Msg_N
9808 ("previous error in declaration of formal package", Actual);
9809 Abandon_Instantiation (Actual);
9810
9811 elsif
9812 Is_Instance_Of (Parent_Spec, Get_Instance_Of (Gen_Parent))
9813 then
9814 null;
9815
9816 else
9817 Error_Msg_NE
9818 ("actual parameter must be instance of&", Actual, Gen_Parent);
9819 Abandon_Instantiation (Actual);
9820 end if;
9821
9822 Set_Instance_Of (Defining_Identifier (Formal), Actual_Pack);
9823 Map_Formal_Package_Entities (Formal_Pack, Actual_Pack);
9824
9825 Nod :=
9826 Make_Package_Renaming_Declaration (Loc,
9827 Defining_Unit_Name => New_Copy (Defining_Identifier (Formal)),
9828 Name => New_Occurrence_Of (Actual_Pack, Loc));
9829
9830 Set_Associated_Formal_Package
9831 (Defining_Unit_Name (Nod), Defining_Identifier (Formal));
9832 Decls := New_List (Nod);
9833
9834 -- If the formal F has a box, then the generic declarations are
9835 -- visible in the generic G. In an instance of G, the corresponding
9836 -- entities in the actual for F (which are the actuals for the
9837 -- instantiation of the generic that F denotes) must also be made
9838 -- visible for analysis of the current instance. On exit from the
9839 -- current instance, those entities are made private again. If the
9840 -- actual is currently in use, these entities are also use-visible.
9841
9842 -- The loop through the actual entities also steps through the formal
9843 -- entities and enters associations from formals to actuals into the
9844 -- renaming map. This is necessary to properly handle checking of
9845 -- actual parameter associations for later formals that depend on
9846 -- actuals declared in the formal package.
9847
9848 -- In Ada 2005, partial parameterization requires that we make
9849 -- visible the actuals corresponding to formals that were defaulted
9850 -- in the formal package. There formals are identified because they
9851 -- remain formal generics within the formal package, rather than
9852 -- being renamings of the actuals supplied.
9853
9854 declare
9855 Gen_Decl : constant Node_Id :=
9856 Unit_Declaration_Node (Gen_Parent);
9857 Formals : constant List_Id :=
9858 Generic_Formal_Declarations (Gen_Decl);
9859
9860 Actual_Ent : Entity_Id;
9861 Actual_Of_Formal : Node_Id;
9862 Formal_Node : Node_Id;
9863 Formal_Ent : Entity_Id;
9864
9865 begin
9866 if Present (Formals) then
9867 Formal_Node := First_Non_Pragma (Formals);
9868 else
9869 Formal_Node := Empty;
9870 end if;
9871
9872 Actual_Ent := First_Entity (Actual_Pack);
9873 Actual_Of_Formal :=
9874 First (Visible_Declarations (Specification (Analyzed_Formal)));
9875 while Present (Actual_Ent)
9876 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
9877 loop
9878 if Present (Formal_Node) then
9879 Formal_Ent := Get_Formal_Entity (Formal_Node);
9880
9881 if Present (Formal_Ent) then
9882 Find_Matching_Actual (Formal_Node, Actual_Ent);
9883 Match_Formal_Entity (Formal_Node, Formal_Ent, Actual_Ent);
9884
9885 -- We iterate at the same time over the actuals of the
9886 -- local package created for the formal, to determine
9887 -- which one of the formals of the original generic were
9888 -- defaulted in the formal. The corresponding actual
9889 -- entities are visible in the enclosing instance.
9890
9891 if Box_Present (Formal)
9892 or else
9893 (Present (Actual_Of_Formal)
9894 and then
9895 Is_Generic_Formal
9896 (Get_Formal_Entity (Actual_Of_Formal)))
9897 then
9898 Set_Is_Hidden (Actual_Ent, False);
9899 Set_Is_Visible_Formal (Actual_Ent);
9900 Set_Is_Potentially_Use_Visible
9901 (Actual_Ent, In_Use (Actual_Pack));
9902
9903 if Ekind (Actual_Ent) = E_Package then
9904 Process_Nested_Formal (Actual_Ent);
9905 end if;
9906
9907 else
9908 Set_Is_Hidden (Actual_Ent);
9909 Set_Is_Potentially_Use_Visible (Actual_Ent, False);
9910 end if;
9911 end if;
9912
9913 Next_Non_Pragma (Formal_Node);
9914 Next (Actual_Of_Formal);
9915
9916 else
9917 -- No further formals to match, but the generic part may
9918 -- contain inherited operation that are not hidden in the
9919 -- enclosing instance.
9920
9921 Next_Entity (Actual_Ent);
9922 end if;
9923 end loop;
9924
9925 -- Inherited subprograms generated by formal derived types are
9926 -- also visible if the types are.
9927
9928 Actual_Ent := First_Entity (Actual_Pack);
9929 while Present (Actual_Ent)
9930 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
9931 loop
9932 if Is_Overloadable (Actual_Ent)
9933 and then
9934 Nkind (Parent (Actual_Ent)) = N_Subtype_Declaration
9935 and then
9936 not Is_Hidden (Defining_Identifier (Parent (Actual_Ent)))
9937 then
9938 Set_Is_Hidden (Actual_Ent, False);
9939 Set_Is_Potentially_Use_Visible
9940 (Actual_Ent, In_Use (Actual_Pack));
9941 end if;
9942
9943 Next_Entity (Actual_Ent);
9944 end loop;
9945 end;
9946
9947 -- If the formal is not declared with a box, reanalyze it as an
9948 -- abbreviated instantiation, to verify the matching rules of 12.7.
9949 -- The actual checks are performed after the generic associations
9950 -- have been analyzed, to guarantee the same visibility for this
9951 -- instantiation and for the actuals.
9952
9953 -- In Ada 2005, the generic associations for the formal can include
9954 -- defaulted parameters. These are ignored during check. This
9955 -- internal instantiation is removed from the tree after conformance
9956 -- checking, because it contains formal declarations for those
9957 -- defaulted parameters, and those should not reach the back-end.
9958
9959 if not Box_Present (Formal) then
9960 declare
9961 I_Pack : constant Entity_Id :=
9962 Make_Temporary (Sloc (Actual), 'P');
9963
9964 begin
9965 Set_Is_Internal (I_Pack);
9966
9967 Append_To (Decls,
9968 Make_Package_Instantiation (Sloc (Actual),
9969 Defining_Unit_Name => I_Pack,
9970 Name =>
9971 New_Occurrence_Of
9972 (Get_Instance_Of (Gen_Parent), Sloc (Actual)),
9973 Generic_Associations => Generic_Associations (Formal)));
9974 end;
9975 end if;
9976
9977 return Decls;
9978 end if;
9979 end Instantiate_Formal_Package;
9980
9981 -----------------------------------
9982 -- Instantiate_Formal_Subprogram --
9983 -----------------------------------
9984
9985 function Instantiate_Formal_Subprogram
9986 (Formal : Node_Id;
9987 Actual : Node_Id;
9988 Analyzed_Formal : Node_Id) return Node_Id
9989 is
9990 Analyzed_S : constant Entity_Id :=
9991 Defining_Unit_Name (Specification (Analyzed_Formal));
9992 Formal_Sub : constant Entity_Id :=
9993 Defining_Unit_Name (Specification (Formal));
9994
9995 function From_Parent_Scope (Subp : Entity_Id) return Boolean;
9996 -- If the generic is a child unit, the parent has been installed on the
9997 -- scope stack, but a default subprogram cannot resolve to something
9998 -- on the parent because that parent is not really part of the visible
9999 -- context (it is there to resolve explicit local entities). If the
10000 -- default has resolved in this way, we remove the entity from immediate
10001 -- visibility and analyze the node again to emit an error message or
10002 -- find another visible candidate.
10003
10004 procedure Valid_Actual_Subprogram (Act : Node_Id);
10005 -- Perform legality check and raise exception on failure
10006
10007 -----------------------
10008 -- From_Parent_Scope --
10009 -----------------------
10010
10011 function From_Parent_Scope (Subp : Entity_Id) return Boolean is
10012 Gen_Scope : Node_Id;
10013
10014 begin
10015 Gen_Scope := Scope (Analyzed_S);
10016 while Present (Gen_Scope) and then Is_Child_Unit (Gen_Scope) loop
10017 if Scope (Subp) = Scope (Gen_Scope) then
10018 return True;
10019 end if;
10020
10021 Gen_Scope := Scope (Gen_Scope);
10022 end loop;
10023
10024 return False;
10025 end From_Parent_Scope;
10026
10027 -----------------------------
10028 -- Valid_Actual_Subprogram --
10029 -----------------------------
10030
10031 procedure Valid_Actual_Subprogram (Act : Node_Id) is
10032 Act_E : Entity_Id;
10033
10034 begin
10035 if Is_Entity_Name (Act) then
10036 Act_E := Entity (Act);
10037
10038 elsif Nkind (Act) = N_Selected_Component
10039 and then Is_Entity_Name (Selector_Name (Act))
10040 then
10041 Act_E := Entity (Selector_Name (Act));
10042
10043 else
10044 Act_E := Empty;
10045 end if;
10046
10047 if (Present (Act_E) and then Is_Overloadable (Act_E))
10048 or else Nkind_In (Act, N_Attribute_Reference,
10049 N_Indexed_Component,
10050 N_Character_Literal,
10051 N_Explicit_Dereference)
10052 then
10053 return;
10054 end if;
10055
10056 Error_Msg_NE
10057 ("expect subprogram or entry name in instantiation of &",
10058 Instantiation_Node, Formal_Sub);
10059 Abandon_Instantiation (Instantiation_Node);
10060 end Valid_Actual_Subprogram;
10061
10062 -- Local variables
10063
10064 Decl_Node : Node_Id;
10065 Loc : Source_Ptr;
10066 Nam : Node_Id;
10067 New_Spec : Node_Id;
10068 New_Subp : Entity_Id;
10069
10070 -- Start of processing for Instantiate_Formal_Subprogram
10071
10072 begin
10073 New_Spec := New_Copy_Tree (Specification (Formal));
10074
10075 -- The tree copy has created the proper instantiation sloc for the
10076 -- new specification. Use this location for all other constructed
10077 -- declarations.
10078
10079 Loc := Sloc (Defining_Unit_Name (New_Spec));
10080
10081 -- Create new entity for the actual (New_Copy_Tree does not), and
10082 -- indicate that it is an actual.
10083
10084 New_Subp := Make_Defining_Identifier (Loc, Chars (Formal_Sub));
10085 Set_Ekind (New_Subp, Ekind (Analyzed_S));
10086 Set_Is_Generic_Actual_Subprogram (New_Subp);
10087 Set_Defining_Unit_Name (New_Spec, New_Subp);
10088
10089 -- Create new entities for the each of the formals in the specification
10090 -- of the renaming declaration built for the actual.
10091
10092 if Present (Parameter_Specifications (New_Spec)) then
10093 declare
10094 F : Node_Id;
10095 F_Id : Entity_Id;
10096
10097 begin
10098 F := First (Parameter_Specifications (New_Spec));
10099 while Present (F) loop
10100 F_Id := Defining_Identifier (F);
10101
10102 Set_Defining_Identifier (F,
10103 Make_Defining_Identifier (Sloc (F_Id), Chars (F_Id)));
10104 Next (F);
10105 end loop;
10106 end;
10107 end if;
10108
10109 -- Find entity of actual. If the actual is an attribute reference, it
10110 -- cannot be resolved here (its formal is missing) but is handled
10111 -- instead in Attribute_Renaming. If the actual is overloaded, it is
10112 -- fully resolved subsequently, when the renaming declaration for the
10113 -- formal is analyzed. If it is an explicit dereference, resolve the
10114 -- prefix but not the actual itself, to prevent interpretation as call.
10115
10116 if Present (Actual) then
10117 Loc := Sloc (Actual);
10118 Set_Sloc (New_Spec, Loc);
10119
10120 if Nkind (Actual) = N_Operator_Symbol then
10121 Find_Direct_Name (Actual);
10122
10123 elsif Nkind (Actual) = N_Explicit_Dereference then
10124 Analyze (Prefix (Actual));
10125
10126 elsif Nkind (Actual) /= N_Attribute_Reference then
10127 Analyze (Actual);
10128 end if;
10129
10130 Valid_Actual_Subprogram (Actual);
10131 Nam := Actual;
10132
10133 elsif Present (Default_Name (Formal)) then
10134 if not Nkind_In (Default_Name (Formal), N_Attribute_Reference,
10135 N_Selected_Component,
10136 N_Indexed_Component,
10137 N_Character_Literal)
10138 and then Present (Entity (Default_Name (Formal)))
10139 then
10140 Nam := New_Occurrence_Of (Entity (Default_Name (Formal)), Loc);
10141 else
10142 Nam := New_Copy (Default_Name (Formal));
10143 Set_Sloc (Nam, Loc);
10144 end if;
10145
10146 elsif Box_Present (Formal) then
10147
10148 -- Actual is resolved at the point of instantiation. Create an
10149 -- identifier or operator with the same name as the formal.
10150
10151 if Nkind (Formal_Sub) = N_Defining_Operator_Symbol then
10152 Nam :=
10153 Make_Operator_Symbol (Loc,
10154 Chars => Chars (Formal_Sub),
10155 Strval => No_String);
10156 else
10157 Nam := Make_Identifier (Loc, Chars (Formal_Sub));
10158 end if;
10159
10160 elsif Nkind (Specification (Formal)) = N_Procedure_Specification
10161 and then Null_Present (Specification (Formal))
10162 then
10163 -- Generate null body for procedure, for use in the instance
10164
10165 Decl_Node :=
10166 Make_Subprogram_Body (Loc,
10167 Specification => New_Spec,
10168 Declarations => New_List,
10169 Handled_Statement_Sequence =>
10170 Make_Handled_Sequence_Of_Statements (Loc,
10171 Statements => New_List (Make_Null_Statement (Loc))));
10172
10173 Set_Is_Intrinsic_Subprogram (Defining_Unit_Name (New_Spec));
10174 return Decl_Node;
10175
10176 else
10177 Error_Msg_Sloc := Sloc (Scope (Analyzed_S));
10178 Error_Msg_NE
10179 ("missing actual&", Instantiation_Node, Formal_Sub);
10180 Error_Msg_NE
10181 ("\in instantiation of & declared#",
10182 Instantiation_Node, Scope (Analyzed_S));
10183 Abandon_Instantiation (Instantiation_Node);
10184 end if;
10185
10186 Decl_Node :=
10187 Make_Subprogram_Renaming_Declaration (Loc,
10188 Specification => New_Spec,
10189 Name => Nam);
10190
10191 -- If we do not have an actual and the formal specified <> then set to
10192 -- get proper default.
10193
10194 if No (Actual) and then Box_Present (Formal) then
10195 Set_From_Default (Decl_Node);
10196 end if;
10197
10198 -- Gather possible interpretations for the actual before analyzing the
10199 -- instance. If overloaded, it will be resolved when analyzing the
10200 -- renaming declaration.
10201
10202 if Box_Present (Formal) and then No (Actual) then
10203 Analyze (Nam);
10204
10205 if Is_Child_Unit (Scope (Analyzed_S))
10206 and then Present (Entity (Nam))
10207 then
10208 if not Is_Overloaded (Nam) then
10209 if From_Parent_Scope (Entity (Nam)) then
10210 Set_Is_Immediately_Visible (Entity (Nam), False);
10211 Set_Entity (Nam, Empty);
10212 Set_Etype (Nam, Empty);
10213
10214 Analyze (Nam);
10215 Set_Is_Immediately_Visible (Entity (Nam));
10216 end if;
10217
10218 else
10219 declare
10220 I : Interp_Index;
10221 It : Interp;
10222
10223 begin
10224 Get_First_Interp (Nam, I, It);
10225 while Present (It.Nam) loop
10226 if From_Parent_Scope (It.Nam) then
10227 Remove_Interp (I);
10228 end if;
10229
10230 Get_Next_Interp (I, It);
10231 end loop;
10232 end;
10233 end if;
10234 end if;
10235 end if;
10236
10237 -- The generic instantiation freezes the actual. This can only be done
10238 -- once the actual is resolved, in the analysis of the renaming
10239 -- declaration. To make the formal subprogram entity available, we set
10240 -- Corresponding_Formal_Spec to point to the formal subprogram entity.
10241 -- This is also needed in Analyze_Subprogram_Renaming for the processing
10242 -- of formal abstract subprograms.
10243
10244 Set_Corresponding_Formal_Spec (Decl_Node, Analyzed_S);
10245
10246 -- We cannot analyze the renaming declaration, and thus find the actual,
10247 -- until all the actuals are assembled in the instance. For subsequent
10248 -- checks of other actuals, indicate the node that will hold the
10249 -- instance of this formal.
10250
10251 Set_Instance_Of (Analyzed_S, Nam);
10252
10253 if Nkind (Actual) = N_Selected_Component
10254 and then Is_Task_Type (Etype (Prefix (Actual)))
10255 and then not Is_Frozen (Etype (Prefix (Actual)))
10256 then
10257 -- The renaming declaration will create a body, which must appear
10258 -- outside of the instantiation, We move the renaming declaration
10259 -- out of the instance, and create an additional renaming inside,
10260 -- to prevent freezing anomalies.
10261
10262 declare
10263 Anon_Id : constant Entity_Id := Make_Temporary (Loc, 'E');
10264
10265 begin
10266 Set_Defining_Unit_Name (New_Spec, Anon_Id);
10267 Insert_Before (Instantiation_Node, Decl_Node);
10268 Analyze (Decl_Node);
10269
10270 -- Now create renaming within the instance
10271
10272 Decl_Node :=
10273 Make_Subprogram_Renaming_Declaration (Loc,
10274 Specification => New_Copy_Tree (New_Spec),
10275 Name => New_Occurrence_Of (Anon_Id, Loc));
10276
10277 Set_Defining_Unit_Name (Specification (Decl_Node),
10278 Make_Defining_Identifier (Loc, Chars (Formal_Sub)));
10279 end;
10280 end if;
10281
10282 return Decl_Node;
10283 end Instantiate_Formal_Subprogram;
10284
10285 ------------------------
10286 -- Instantiate_Object --
10287 ------------------------
10288
10289 function Instantiate_Object
10290 (Formal : Node_Id;
10291 Actual : Node_Id;
10292 Analyzed_Formal : Node_Id) return List_Id
10293 is
10294 Gen_Obj : constant Entity_Id := Defining_Identifier (Formal);
10295 A_Gen_Obj : constant Entity_Id :=
10296 Defining_Identifier (Analyzed_Formal);
10297 Acc_Def : Node_Id := Empty;
10298 Act_Assoc : constant Node_Id := Parent (Actual);
10299 Actual_Decl : Node_Id := Empty;
10300 Decl_Node : Node_Id;
10301 Def : Node_Id;
10302 Ftyp : Entity_Id;
10303 List : constant List_Id := New_List;
10304 Loc : constant Source_Ptr := Sloc (Actual);
10305 Orig_Ftyp : constant Entity_Id := Etype (A_Gen_Obj);
10306 Subt_Decl : Node_Id := Empty;
10307 Subt_Mark : Node_Id := Empty;
10308
10309 function Copy_Access_Def return Node_Id;
10310 -- If formal is an anonymous access, copy access definition of formal
10311 -- for generated object declaration.
10312
10313 ---------------------
10314 -- Copy_Access_Def --
10315 ---------------------
10316
10317 function Copy_Access_Def return Node_Id is
10318 begin
10319 Def := New_Copy_Tree (Acc_Def);
10320
10321 -- In addition, if formal is an access to subprogram we need to
10322 -- generate new formals for the signature of the default, so that
10323 -- the tree is properly formatted for ASIS use.
10324
10325 if Present (Access_To_Subprogram_Definition (Acc_Def)) then
10326 declare
10327 Par_Spec : Node_Id;
10328 begin
10329 Par_Spec :=
10330 First (Parameter_Specifications
10331 (Access_To_Subprogram_Definition (Def)));
10332 while Present (Par_Spec) loop
10333 Set_Defining_Identifier (Par_Spec,
10334 Make_Defining_Identifier (Sloc (Acc_Def),
10335 Chars => Chars (Defining_Identifier (Par_Spec))));
10336 Next (Par_Spec);
10337 end loop;
10338 end;
10339 end if;
10340
10341 return Def;
10342 end Copy_Access_Def;
10343
10344 -- Start of processing for Instantiate_Object
10345
10346 begin
10347 -- Formal may be an anonymous access
10348
10349 if Present (Subtype_Mark (Formal)) then
10350 Subt_Mark := Subtype_Mark (Formal);
10351 else
10352 Check_Access_Definition (Formal);
10353 Acc_Def := Access_Definition (Formal);
10354 end if;
10355
10356 -- Sloc for error message on missing actual
10357
10358 Error_Msg_Sloc := Sloc (Scope (A_Gen_Obj));
10359
10360 if Get_Instance_Of (Gen_Obj) /= Gen_Obj then
10361 Error_Msg_N ("duplicate instantiation of generic parameter", Actual);
10362 end if;
10363
10364 Set_Parent (List, Parent (Actual));
10365
10366 -- OUT present
10367
10368 if Out_Present (Formal) then
10369
10370 -- An IN OUT generic actual must be a name. The instantiation is a
10371 -- renaming declaration. The actual is the name being renamed. We
10372 -- use the actual directly, rather than a copy, because it is not
10373 -- used further in the list of actuals, and because a copy or a use
10374 -- of relocate_node is incorrect if the instance is nested within a
10375 -- generic. In order to simplify ASIS searches, the Generic_Parent
10376 -- field links the declaration to the generic association.
10377
10378 if No (Actual) then
10379 Error_Msg_NE
10380 ("missing actual &",
10381 Instantiation_Node, Gen_Obj);
10382 Error_Msg_NE
10383 ("\in instantiation of & declared#",
10384 Instantiation_Node, Scope (A_Gen_Obj));
10385 Abandon_Instantiation (Instantiation_Node);
10386 end if;
10387
10388 if Present (Subt_Mark) then
10389 Decl_Node :=
10390 Make_Object_Renaming_Declaration (Loc,
10391 Defining_Identifier => New_Copy (Gen_Obj),
10392 Subtype_Mark => New_Copy_Tree (Subt_Mark),
10393 Name => Actual);
10394
10395 else pragma Assert (Present (Acc_Def));
10396 Decl_Node :=
10397 Make_Object_Renaming_Declaration (Loc,
10398 Defining_Identifier => New_Copy (Gen_Obj),
10399 Access_Definition => New_Copy_Tree (Acc_Def),
10400 Name => Actual);
10401 end if;
10402
10403 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
10404
10405 -- The analysis of the actual may produce Insert_Action nodes, so
10406 -- the declaration must have a context in which to attach them.
10407
10408 Append (Decl_Node, List);
10409 Analyze (Actual);
10410
10411 -- Return if the analysis of the actual reported some error
10412
10413 if Etype (Actual) = Any_Type then
10414 return List;
10415 end if;
10416
10417 -- This check is performed here because Analyze_Object_Renaming will
10418 -- not check it when Comes_From_Source is False. Note though that the
10419 -- check for the actual being the name of an object will be performed
10420 -- in Analyze_Object_Renaming.
10421
10422 if Is_Object_Reference (Actual)
10423 and then Is_Dependent_Component_Of_Mutable_Object (Actual)
10424 then
10425 Error_Msg_N
10426 ("illegal discriminant-dependent component for in out parameter",
10427 Actual);
10428 end if;
10429
10430 -- The actual has to be resolved in order to check that it is a
10431 -- variable (due to cases such as F (1), where F returns access to
10432 -- an array, and for overloaded prefixes).
10433
10434 Ftyp := Get_Instance_Of (Etype (A_Gen_Obj));
10435
10436 -- If the type of the formal is not itself a formal, and the current
10437 -- unit is a child unit, the formal type must be declared in a
10438 -- parent, and must be retrieved by visibility.
10439
10440 if Ftyp = Orig_Ftyp
10441 and then Is_Generic_Unit (Scope (Ftyp))
10442 and then Is_Child_Unit (Scope (A_Gen_Obj))
10443 then
10444 declare
10445 Temp : constant Node_Id :=
10446 New_Copy_Tree (Subtype_Mark (Analyzed_Formal));
10447 begin
10448 Set_Entity (Temp, Empty);
10449 Find_Type (Temp);
10450 Ftyp := Entity (Temp);
10451 end;
10452 end if;
10453
10454 if Is_Private_Type (Ftyp)
10455 and then not Is_Private_Type (Etype (Actual))
10456 and then (Base_Type (Full_View (Ftyp)) = Base_Type (Etype (Actual))
10457 or else Base_Type (Etype (Actual)) = Ftyp)
10458 then
10459 -- If the actual has the type of the full view of the formal, or
10460 -- else a non-private subtype of the formal, then the visibility
10461 -- of the formal type has changed. Add to the actuals a subtype
10462 -- declaration that will force the exchange of views in the body
10463 -- of the instance as well.
10464
10465 Subt_Decl :=
10466 Make_Subtype_Declaration (Loc,
10467 Defining_Identifier => Make_Temporary (Loc, 'P'),
10468 Subtype_Indication => New_Occurrence_Of (Ftyp, Loc));
10469
10470 Prepend (Subt_Decl, List);
10471
10472 Prepend_Elmt (Full_View (Ftyp), Exchanged_Views);
10473 Exchange_Declarations (Ftyp);
10474 end if;
10475
10476 Resolve (Actual, Ftyp);
10477
10478 if not Denotes_Variable (Actual) then
10479 Error_Msg_NE ("actual for& must be a variable", Actual, Gen_Obj);
10480
10481 elsif Base_Type (Ftyp) /= Base_Type (Etype (Actual)) then
10482
10483 -- Ada 2005 (AI-423): For a generic formal object of mode in out,
10484 -- the type of the actual shall resolve to a specific anonymous
10485 -- access type.
10486
10487 if Ada_Version < Ada_2005
10488 or else Ekind (Base_Type (Ftyp)) /=
10489 E_Anonymous_Access_Type
10490 or else Ekind (Base_Type (Etype (Actual))) /=
10491 E_Anonymous_Access_Type
10492 then
10493 Error_Msg_NE
10494 ("type of actual does not match type of&", Actual, Gen_Obj);
10495 end if;
10496 end if;
10497
10498 Note_Possible_Modification (Actual, Sure => True);
10499
10500 -- Check for instantiation of atomic/volatile actual for
10501 -- non-atomic/volatile formal (RM C.6 (12)).
10502
10503 if Is_Atomic_Object (Actual) and then not Is_Atomic (Orig_Ftyp) then
10504 Error_Msg_N
10505 ("cannot instantiate non-atomic formal object "
10506 & "with atomic actual", Actual);
10507
10508 elsif Is_Volatile_Object (Actual) and then not Is_Volatile (Orig_Ftyp)
10509 then
10510 Error_Msg_N
10511 ("cannot instantiate non-volatile formal object "
10512 & "with volatile actual", Actual);
10513 end if;
10514
10515 -- Formal in-parameter
10516
10517 else
10518 -- The instantiation of a generic formal in-parameter is constant
10519 -- declaration. The actual is the expression for that declaration.
10520 -- Its type is a full copy of the type of the formal. This may be
10521 -- an access to subprogram, for which we need to generate entities
10522 -- for the formals in the new signature.
10523
10524 if Present (Actual) then
10525 if Present (Subt_Mark) then
10526 Def := New_Copy_Tree (Subt_Mark);
10527 else pragma Assert (Present (Acc_Def));
10528 Def := Copy_Access_Def;
10529 end if;
10530
10531 Decl_Node :=
10532 Make_Object_Declaration (Loc,
10533 Defining_Identifier => New_Copy (Gen_Obj),
10534 Constant_Present => True,
10535 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
10536 Object_Definition => Def,
10537 Expression => Actual);
10538
10539 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
10540
10541 -- A generic formal object of a tagged type is defined to be
10542 -- aliased so the new constant must also be treated as aliased.
10543
10544 if Is_Tagged_Type (Etype (A_Gen_Obj)) then
10545 Set_Aliased_Present (Decl_Node);
10546 end if;
10547
10548 Append (Decl_Node, List);
10549
10550 -- No need to repeat (pre-)analysis of some expression nodes
10551 -- already handled in Preanalyze_Actuals.
10552
10553 if Nkind (Actual) /= N_Allocator then
10554 Analyze (Actual);
10555
10556 -- Return if the analysis of the actual reported some error
10557
10558 if Etype (Actual) = Any_Type then
10559 return List;
10560 end if;
10561 end if;
10562
10563 declare
10564 Formal_Type : constant Entity_Id := Etype (A_Gen_Obj);
10565 Typ : Entity_Id;
10566
10567 begin
10568 Typ := Get_Instance_Of (Formal_Type);
10569
10570 -- If the actual appears in the current or an enclosing scope,
10571 -- use its type directly. This is relevant if it has an actual
10572 -- subtype that is distinct from its nominal one. This cannot
10573 -- be done in general because the type of the actual may
10574 -- depend on other actuals, and only be fully determined when
10575 -- the enclosing instance is analyzed.
10576
10577 if Present (Etype (Actual))
10578 and then Is_Constr_Subt_For_U_Nominal (Etype (Actual))
10579 then
10580 Freeze_Before (Instantiation_Node, Etype (Actual));
10581 else
10582 Freeze_Before (Instantiation_Node, Typ);
10583 end if;
10584
10585 -- If the actual is an aggregate, perform name resolution on
10586 -- its components (the analysis of an aggregate does not do it)
10587 -- to capture local names that may be hidden if the generic is
10588 -- a child unit.
10589
10590 if Nkind (Actual) = N_Aggregate then
10591 Preanalyze_And_Resolve (Actual, Typ);
10592 end if;
10593
10594 if Is_Limited_Type (Typ)
10595 and then not OK_For_Limited_Init (Typ, Actual)
10596 then
10597 Error_Msg_N
10598 ("initialization not allowed for limited types", Actual);
10599 Explain_Limited_Type (Typ, Actual);
10600 end if;
10601 end;
10602
10603 elsif Present (Default_Expression (Formal)) then
10604
10605 -- Use default to construct declaration
10606
10607 if Present (Subt_Mark) then
10608 Def := New_Copy (Subt_Mark);
10609 else pragma Assert (Present (Acc_Def));
10610 Def := Copy_Access_Def;
10611 end if;
10612
10613 Decl_Node :=
10614 Make_Object_Declaration (Sloc (Formal),
10615 Defining_Identifier => New_Copy (Gen_Obj),
10616 Constant_Present => True,
10617 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
10618 Object_Definition => Def,
10619 Expression => New_Copy_Tree
10620 (Default_Expression (Formal)));
10621
10622 Append (Decl_Node, List);
10623 Set_Analyzed (Expression (Decl_Node), False);
10624
10625 else
10626 Error_Msg_NE ("missing actual&", Instantiation_Node, Gen_Obj);
10627 Error_Msg_NE ("\in instantiation of & declared#",
10628 Instantiation_Node, Scope (A_Gen_Obj));
10629
10630 if Is_Scalar_Type (Etype (A_Gen_Obj)) then
10631
10632 -- Create dummy constant declaration so that instance can be
10633 -- analyzed, to minimize cascaded visibility errors.
10634
10635 if Present (Subt_Mark) then
10636 Def := Subt_Mark;
10637 else pragma Assert (Present (Acc_Def));
10638 Def := Acc_Def;
10639 end if;
10640
10641 Decl_Node :=
10642 Make_Object_Declaration (Loc,
10643 Defining_Identifier => New_Copy (Gen_Obj),
10644 Constant_Present => True,
10645 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
10646 Object_Definition => New_Copy (Def),
10647 Expression =>
10648 Make_Attribute_Reference (Sloc (Gen_Obj),
10649 Attribute_Name => Name_First,
10650 Prefix => New_Copy (Def)));
10651
10652 Append (Decl_Node, List);
10653
10654 else
10655 Abandon_Instantiation (Instantiation_Node);
10656 end if;
10657 end if;
10658 end if;
10659
10660 if Nkind (Actual) in N_Has_Entity then
10661 Actual_Decl := Parent (Entity (Actual));
10662 end if;
10663
10664 -- Ada 2005 (AI-423): For a formal object declaration with a null
10665 -- exclusion or an access definition that has a null exclusion: If the
10666 -- actual matching the formal object declaration denotes a generic
10667 -- formal object of another generic unit G, and the instantiation
10668 -- containing the actual occurs within the body of G or within the body
10669 -- of a generic unit declared within the declarative region of G, then
10670 -- the declaration of the formal object of G must have a null exclusion.
10671 -- Otherwise, the subtype of the actual matching the formal object
10672 -- declaration shall exclude null.
10673
10674 if Ada_Version >= Ada_2005
10675 and then Present (Actual_Decl)
10676 and then Nkind_In (Actual_Decl, N_Formal_Object_Declaration,
10677 N_Object_Declaration)
10678 and then Nkind (Analyzed_Formal) = N_Formal_Object_Declaration
10679 and then not Has_Null_Exclusion (Actual_Decl)
10680 and then Has_Null_Exclusion (Analyzed_Formal)
10681 then
10682 Error_Msg_Sloc := Sloc (Analyzed_Formal);
10683 Error_Msg_N
10684 ("actual must exclude null to match generic formal#", Actual);
10685 end if;
10686
10687 -- An effectively volatile object cannot be used as an actual in a
10688 -- generic instantiation (SPARK RM 7.1.3(7)). The following check is
10689 -- relevant only when SPARK_Mode is on as it is not a standard Ada
10690 -- legality rule, and also verifies that the actual is an object.
10691
10692 if SPARK_Mode = On
10693 and then Present (Actual)
10694 and then Is_Object_Reference (Actual)
10695 and then Is_Effectively_Volatile_Object (Actual)
10696 then
10697 Error_Msg_N
10698 ("volatile object cannot act as actual in generic instantiation",
10699 Actual);
10700 end if;
10701
10702 return List;
10703 end Instantiate_Object;
10704
10705 ------------------------------
10706 -- Instantiate_Package_Body --
10707 ------------------------------
10708
10709 procedure Instantiate_Package_Body
10710 (Body_Info : Pending_Body_Info;
10711 Inlined_Body : Boolean := False;
10712 Body_Optional : Boolean := False)
10713 is
10714 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
10715 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
10716 Loc : constant Source_Ptr := Sloc (Inst_Node);
10717
10718 Gen_Id : constant Node_Id := Name (Inst_Node);
10719 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
10720 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
10721 Act_Spec : constant Node_Id := Specification (Act_Decl);
10722 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Spec);
10723
10724 Save_IPSM : constant Boolean := Ignore_Pragma_SPARK_Mode;
10725 Save_Style_Check : constant Boolean := Style_Check;
10726
10727 Act_Body : Node_Id;
10728 Act_Body_Id : Entity_Id;
10729 Act_Body_Name : Node_Id;
10730 Gen_Body : Node_Id;
10731 Gen_Body_Id : Node_Id;
10732 Par_Ent : Entity_Id := Empty;
10733 Par_Vis : Boolean := False;
10734
10735 Parent_Installed : Boolean := False;
10736
10737 Vis_Prims_List : Elist_Id := No_Elist;
10738 -- List of primitives made temporarily visible in the instantiation
10739 -- to match the visibility of the formal type
10740
10741 procedure Check_Initialized_Types;
10742 -- In a generic package body, an entity of a generic private type may
10743 -- appear uninitialized. This is suspicious, unless the actual is a
10744 -- fully initialized type.
10745
10746 -----------------------------
10747 -- Check_Initialized_Types --
10748 -----------------------------
10749
10750 procedure Check_Initialized_Types is
10751 Decl : Node_Id;
10752 Formal : Entity_Id;
10753 Actual : Entity_Id;
10754 Uninit_Var : Entity_Id;
10755
10756 begin
10757 Decl := First (Generic_Formal_Declarations (Gen_Decl));
10758 while Present (Decl) loop
10759 Uninit_Var := Empty;
10760
10761 if Nkind (Decl) = N_Private_Extension_Declaration then
10762 Uninit_Var := Uninitialized_Variable (Decl);
10763
10764 elsif Nkind (Decl) = N_Formal_Type_Declaration
10765 and then Nkind (Formal_Type_Definition (Decl)) =
10766 N_Formal_Private_Type_Definition
10767 then
10768 Uninit_Var :=
10769 Uninitialized_Variable (Formal_Type_Definition (Decl));
10770 end if;
10771
10772 if Present (Uninit_Var) then
10773 Formal := Defining_Identifier (Decl);
10774 Actual := First_Entity (Act_Decl_Id);
10775
10776 -- For each formal there is a subtype declaration that renames
10777 -- the actual and has the same name as the formal. Locate the
10778 -- formal for warning message about uninitialized variables
10779 -- in the generic, for which the actual type should be a fully
10780 -- initialized type.
10781
10782 while Present (Actual) loop
10783 exit when Ekind (Actual) = E_Package
10784 and then Present (Renamed_Object (Actual));
10785
10786 if Chars (Actual) = Chars (Formal)
10787 and then not Is_Scalar_Type (Actual)
10788 and then not Is_Fully_Initialized_Type (Actual)
10789 and then Warn_On_No_Value_Assigned
10790 then
10791 Error_Msg_Node_2 := Formal;
10792 Error_Msg_NE
10793 ("generic unit has uninitialized variable& of "
10794 & "formal private type &?v?", Actual, Uninit_Var);
10795 Error_Msg_NE
10796 ("actual type for& should be fully initialized type?v?",
10797 Actual, Formal);
10798 exit;
10799 end if;
10800
10801 Next_Entity (Actual);
10802 end loop;
10803 end if;
10804
10805 Next (Decl);
10806 end loop;
10807 end Check_Initialized_Types;
10808
10809 -- Start of processing for Instantiate_Package_Body
10810
10811 begin
10812 Gen_Body_Id := Corresponding_Body (Gen_Decl);
10813
10814 -- The instance body may already have been processed, as the parent of
10815 -- another instance that is inlined (Load_Parent_Of_Generic).
10816
10817 if Present (Corresponding_Body (Instance_Spec (Inst_Node))) then
10818 return;
10819 end if;
10820
10821 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
10822
10823 -- Re-establish the state of information on which checks are suppressed.
10824 -- This information was set in Body_Info at the point of instantiation,
10825 -- and now we restore it so that the instance is compiled using the
10826 -- check status at the instantiation (RM 11.5(7.2/2), AI95-00224-01).
10827
10828 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
10829 Scope_Suppress := Body_Info.Scope_Suppress;
10830 Opt.Ada_Version := Body_Info.Version;
10831 Opt.Ada_Version_Pragma := Body_Info.Version_Pragma;
10832 Restore_Warnings (Body_Info.Warnings);
10833 Opt.SPARK_Mode := Body_Info.SPARK_Mode;
10834 Opt.SPARK_Mode_Pragma := Body_Info.SPARK_Mode_Pragma;
10835
10836 if No (Gen_Body_Id) then
10837
10838 -- Do not look for parent of generic body if none is required.
10839 -- This may happen when the routine is called as part of the
10840 -- Pending_Instantiations processing, when nested instances
10841 -- may precede the one generated from the main unit.
10842
10843 if not Unit_Requires_Body (Defining_Entity (Gen_Decl))
10844 and then Body_Optional
10845 then
10846 return;
10847 else
10848 Load_Parent_Of_Generic
10849 (Inst_Node, Specification (Gen_Decl), Body_Optional);
10850 Gen_Body_Id := Corresponding_Body (Gen_Decl);
10851 end if;
10852 end if;
10853
10854 -- Establish global variable for sloc adjustment and for error recovery
10855 -- In the case of an instance body for an instantiation with actuals
10856 -- from a limited view, the instance body is placed at the beginning
10857 -- of the enclosing package body: use the body entity as the source
10858 -- location for nodes of the instance body.
10859
10860 if not Is_Empty_Elmt_List (Incomplete_Actuals (Act_Decl_Id)) then
10861 declare
10862 Scop : constant Entity_Id := Scope (Act_Decl_Id);
10863 Body_Id : constant Node_Id :=
10864 Corresponding_Body (Unit_Declaration_Node (Scop));
10865
10866 begin
10867 Instantiation_Node := Body_Id;
10868 end;
10869 else
10870 Instantiation_Node := Inst_Node;
10871 end if;
10872
10873 if Present (Gen_Body_Id) then
10874 Save_Env (Gen_Unit, Act_Decl_Id);
10875 Style_Check := False;
10876
10877 -- If the context of the instance is subject to SPARK_Mode "off" or
10878 -- the annotation is altogether missing, set the global flag which
10879 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
10880 -- the instance.
10881
10882 if SPARK_Mode /= On then
10883 Ignore_Pragma_SPARK_Mode := True;
10884 end if;
10885
10886 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
10887 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
10888
10889 Create_Instantiation_Source
10890 (Inst_Node, Gen_Body_Id, S_Adjustment);
10891
10892 Act_Body :=
10893 Copy_Generic_Node
10894 (Original_Node (Gen_Body), Empty, Instantiating => True);
10895
10896 -- Create proper (possibly qualified) defining name for the body, to
10897 -- correspond to the one in the spec.
10898
10899 Act_Body_Id :=
10900 Make_Defining_Identifier (Sloc (Act_Decl_Id), Chars (Act_Decl_Id));
10901 Set_Comes_From_Source (Act_Body_Id, Comes_From_Source (Act_Decl_Id));
10902
10903 -- Some attributes of spec entity are not inherited by body entity
10904
10905 Set_Handler_Records (Act_Body_Id, No_List);
10906
10907 if Nkind (Defining_Unit_Name (Act_Spec)) =
10908 N_Defining_Program_Unit_Name
10909 then
10910 Act_Body_Name :=
10911 Make_Defining_Program_Unit_Name (Loc,
10912 Name =>
10913 New_Copy_Tree (Name (Defining_Unit_Name (Act_Spec))),
10914 Defining_Identifier => Act_Body_Id);
10915 else
10916 Act_Body_Name := Act_Body_Id;
10917 end if;
10918
10919 Set_Defining_Unit_Name (Act_Body, Act_Body_Name);
10920
10921 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
10922 Check_Generic_Actuals (Act_Decl_Id, False);
10923 Check_Initialized_Types;
10924
10925 -- Install primitives hidden at the point of the instantiation but
10926 -- visible when processing the generic formals
10927
10928 declare
10929 E : Entity_Id;
10930
10931 begin
10932 E := First_Entity (Act_Decl_Id);
10933 while Present (E) loop
10934 if Is_Type (E)
10935 and then not Is_Itype (E)
10936 and then Is_Generic_Actual_Type (E)
10937 and then Is_Tagged_Type (E)
10938 then
10939 Install_Hidden_Primitives
10940 (Prims_List => Vis_Prims_List,
10941 Gen_T => Generic_Parent_Type (Parent (E)),
10942 Act_T => E);
10943 end if;
10944
10945 Next_Entity (E);
10946 end loop;
10947 end;
10948
10949 -- If it is a child unit, make the parent instance (which is an
10950 -- instance of the parent of the generic) visible. The parent
10951 -- instance is the prefix of the name of the generic unit.
10952
10953 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
10954 and then Nkind (Gen_Id) = N_Expanded_Name
10955 then
10956 Par_Ent := Entity (Prefix (Gen_Id));
10957 Par_Vis := Is_Immediately_Visible (Par_Ent);
10958 Install_Parent (Par_Ent, In_Body => True);
10959 Parent_Installed := True;
10960
10961 elsif Is_Child_Unit (Gen_Unit) then
10962 Par_Ent := Scope (Gen_Unit);
10963 Par_Vis := Is_Immediately_Visible (Par_Ent);
10964 Install_Parent (Par_Ent, In_Body => True);
10965 Parent_Installed := True;
10966 end if;
10967
10968 -- If the instantiation is a library unit, and this is the main unit,
10969 -- then build the resulting compilation unit nodes for the instance.
10970 -- If this is a compilation unit but it is not the main unit, then it
10971 -- is the body of a unit in the context, that is being compiled
10972 -- because it is encloses some inlined unit or another generic unit
10973 -- being instantiated. In that case, this body is not part of the
10974 -- current compilation, and is not attached to the tree, but its
10975 -- parent must be set for analysis.
10976
10977 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
10978
10979 -- Replace instance node with body of instance, and create new
10980 -- node for corresponding instance declaration.
10981
10982 Build_Instance_Compilation_Unit_Nodes
10983 (Inst_Node, Act_Body, Act_Decl);
10984 Analyze (Inst_Node);
10985
10986 if Parent (Inst_Node) = Cunit (Main_Unit) then
10987
10988 -- If the instance is a child unit itself, then set the scope
10989 -- of the expanded body to be the parent of the instantiation
10990 -- (ensuring that the fully qualified name will be generated
10991 -- for the elaboration subprogram).
10992
10993 if Nkind (Defining_Unit_Name (Act_Spec)) =
10994 N_Defining_Program_Unit_Name
10995 then
10996 Set_Scope (Defining_Entity (Inst_Node), Scope (Act_Decl_Id));
10997 end if;
10998 end if;
10999
11000 -- Case where instantiation is not a library unit
11001
11002 else
11003 -- If this is an early instantiation, i.e. appears textually
11004 -- before the corresponding body and must be elaborated first,
11005 -- indicate that the body instance is to be delayed.
11006
11007 Install_Body (Act_Body, Inst_Node, Gen_Body, Gen_Decl);
11008
11009 -- Now analyze the body. We turn off all checks if this is an
11010 -- internal unit, since there is no reason to have checks on for
11011 -- any predefined run-time library code. All such code is designed
11012 -- to be compiled with checks off.
11013
11014 -- Note that we do NOT apply this criterion to children of GNAT
11015 -- The latter units must suppress checks explicitly if needed.
11016
11017 -- We also do not suppress checks in CodePeer mode where we are
11018 -- interested in finding possible runtime errors.
11019
11020 if not CodePeer_Mode
11021 and then Is_Predefined_File_Name
11022 (Unit_File_Name (Get_Source_Unit (Gen_Decl)))
11023 then
11024 Analyze (Act_Body, Suppress => All_Checks);
11025 else
11026 Analyze (Act_Body);
11027 end if;
11028 end if;
11029
11030 Inherit_Context (Gen_Body, Inst_Node);
11031
11032 -- Remove the parent instances if they have been placed on the scope
11033 -- stack to compile the body.
11034
11035 if Parent_Installed then
11036 Remove_Parent (In_Body => True);
11037
11038 -- Restore the previous visibility of the parent
11039
11040 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
11041 end if;
11042
11043 Restore_Hidden_Primitives (Vis_Prims_List);
11044 Restore_Private_Views (Act_Decl_Id);
11045
11046 -- Remove the current unit from visibility if this is an instance
11047 -- that is not elaborated on the fly for inlining purposes.
11048
11049 if not Inlined_Body then
11050 Set_Is_Immediately_Visible (Act_Decl_Id, False);
11051 end if;
11052
11053 Restore_Env;
11054 Ignore_Pragma_SPARK_Mode := Save_IPSM;
11055 Style_Check := Save_Style_Check;
11056
11057 -- If we have no body, and the unit requires a body, then complain. This
11058 -- complaint is suppressed if we have detected other errors (since a
11059 -- common reason for missing the body is that it had errors).
11060 -- In CodePeer mode, a warning has been emitted already, no need for
11061 -- further messages.
11062
11063 elsif Unit_Requires_Body (Gen_Unit)
11064 and then not Body_Optional
11065 then
11066 if CodePeer_Mode then
11067 null;
11068
11069 elsif Serious_Errors_Detected = 0 then
11070 Error_Msg_NE
11071 ("cannot find body of generic package &", Inst_Node, Gen_Unit);
11072
11073 -- Don't attempt to perform any cleanup actions if some other error
11074 -- was already detected, since this can cause blowups.
11075
11076 else
11077 return;
11078 end if;
11079
11080 -- Case of package that does not need a body
11081
11082 else
11083 -- If the instantiation of the declaration is a library unit, rewrite
11084 -- the original package instantiation as a package declaration in the
11085 -- compilation unit node.
11086
11087 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
11088 Set_Parent_Spec (Act_Decl, Parent_Spec (Inst_Node));
11089 Rewrite (Inst_Node, Act_Decl);
11090
11091 -- Generate elaboration entity, in case spec has elaboration code.
11092 -- This cannot be done when the instance is analyzed, because it
11093 -- is not known yet whether the body exists.
11094
11095 Set_Elaboration_Entity_Required (Act_Decl_Id, False);
11096 Build_Elaboration_Entity (Parent (Inst_Node), Act_Decl_Id);
11097
11098 -- If the instantiation is not a library unit, then append the
11099 -- declaration to the list of implicitly generated entities, unless
11100 -- it is already a list member which means that it was already
11101 -- processed
11102
11103 elsif not Is_List_Member (Act_Decl) then
11104 Mark_Rewrite_Insertion (Act_Decl);
11105 Insert_Before (Inst_Node, Act_Decl);
11106 end if;
11107 end if;
11108
11109 Expander_Mode_Restore;
11110 end Instantiate_Package_Body;
11111
11112 ---------------------------------
11113 -- Instantiate_Subprogram_Body --
11114 ---------------------------------
11115
11116 procedure Instantiate_Subprogram_Body
11117 (Body_Info : Pending_Body_Info;
11118 Body_Optional : Boolean := False)
11119 is
11120 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
11121 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
11122 Loc : constant Source_Ptr := Sloc (Inst_Node);
11123 Gen_Id : constant Node_Id := Name (Inst_Node);
11124 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
11125 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
11126 Act_Decl_Id : constant Entity_Id :=
11127 Defining_Unit_Name (Specification (Act_Decl));
11128 Pack_Id : constant Entity_Id :=
11129 Defining_Unit_Name (Parent (Act_Decl));
11130
11131 Saved_IPSM : constant Boolean := Ignore_Pragma_SPARK_Mode;
11132 Saved_Style_Check : constant Boolean := Style_Check;
11133 Saved_Warnings : constant Warning_Record := Save_Warnings;
11134
11135 Act_Body : Node_Id;
11136 Act_Body_Id : Entity_Id;
11137 Gen_Body : Node_Id;
11138 Gen_Body_Id : Node_Id;
11139 Pack_Body : Node_Id;
11140 Par_Ent : Entity_Id := Empty;
11141 Par_Vis : Boolean := False;
11142 Ret_Expr : Node_Id;
11143
11144 Parent_Installed : Boolean := False;
11145
11146 begin
11147 Gen_Body_Id := Corresponding_Body (Gen_Decl);
11148
11149 -- Subprogram body may have been created already because of an inline
11150 -- pragma, or because of multiple elaborations of the enclosing package
11151 -- when several instances of the subprogram appear in the main unit.
11152
11153 if Present (Corresponding_Body (Act_Decl)) then
11154 return;
11155 end if;
11156
11157 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
11158
11159 -- Re-establish the state of information on which checks are suppressed.
11160 -- This information was set in Body_Info at the point of instantiation,
11161 -- and now we restore it so that the instance is compiled using the
11162 -- check status at the instantiation (RM 11.5(7.2/2), AI95-00224-01).
11163
11164 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
11165 Scope_Suppress := Body_Info.Scope_Suppress;
11166 Opt.Ada_Version := Body_Info.Version;
11167 Opt.Ada_Version_Pragma := Body_Info.Version_Pragma;
11168 Restore_Warnings (Body_Info.Warnings);
11169 Opt.SPARK_Mode := Body_Info.SPARK_Mode;
11170 Opt.SPARK_Mode_Pragma := Body_Info.SPARK_Mode_Pragma;
11171
11172 if No (Gen_Body_Id) then
11173
11174 -- For imported generic subprogram, no body to compile, complete
11175 -- the spec entity appropriately.
11176
11177 if Is_Imported (Gen_Unit) then
11178 Set_Is_Imported (Act_Decl_Id);
11179 Set_First_Rep_Item (Act_Decl_Id, First_Rep_Item (Gen_Unit));
11180 Set_Interface_Name (Act_Decl_Id, Interface_Name (Gen_Unit));
11181 Set_Convention (Act_Decl_Id, Convention (Gen_Unit));
11182 Set_Has_Completion (Act_Decl_Id);
11183 return;
11184
11185 -- For other cases, compile the body
11186
11187 else
11188 Load_Parent_Of_Generic
11189 (Inst_Node, Specification (Gen_Decl), Body_Optional);
11190 Gen_Body_Id := Corresponding_Body (Gen_Decl);
11191 end if;
11192 end if;
11193
11194 Instantiation_Node := Inst_Node;
11195
11196 if Present (Gen_Body_Id) then
11197 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
11198
11199 if Nkind (Gen_Body) = N_Subprogram_Body_Stub then
11200
11201 -- Either body is not present, or context is non-expanding, as
11202 -- when compiling a subunit. Mark the instance as completed, and
11203 -- diagnose a missing body when needed.
11204
11205 if Expander_Active
11206 and then Operating_Mode = Generate_Code
11207 then
11208 Error_Msg_N
11209 ("missing proper body for instantiation", Gen_Body);
11210 end if;
11211
11212 Set_Has_Completion (Act_Decl_Id);
11213 return;
11214 end if;
11215
11216 Save_Env (Gen_Unit, Act_Decl_Id);
11217 Style_Check := False;
11218
11219 -- If the context of the instance is subject to SPARK_Mode "off" or
11220 -- the annotation is altogether missing, set the global flag which
11221 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
11222 -- the instance.
11223
11224 if SPARK_Mode /= On then
11225 Ignore_Pragma_SPARK_Mode := True;
11226 end if;
11227
11228 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
11229 Create_Instantiation_Source
11230 (Inst_Node,
11231 Gen_Body_Id,
11232 S_Adjustment);
11233
11234 Act_Body :=
11235 Copy_Generic_Node
11236 (Original_Node (Gen_Body), Empty, Instantiating => True);
11237
11238 -- Create proper defining name for the body, to correspond to the one
11239 -- in the spec.
11240
11241 Act_Body_Id :=
11242 Make_Defining_Identifier (Sloc (Act_Decl_Id), Chars (Act_Decl_Id));
11243
11244 Set_Comes_From_Source (Act_Body_Id, Comes_From_Source (Act_Decl_Id));
11245 Set_Defining_Unit_Name (Specification (Act_Body), Act_Body_Id);
11246
11247 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
11248 Set_Has_Completion (Act_Decl_Id);
11249 Check_Generic_Actuals (Pack_Id, False);
11250
11251 -- Generate a reference to link the visible subprogram instance to
11252 -- the generic body, which for navigation purposes is the only
11253 -- available source for the instance.
11254
11255 Generate_Reference
11256 (Related_Instance (Pack_Id),
11257 Gen_Body_Id, 'b', Set_Ref => False, Force => True);
11258
11259 -- If it is a child unit, make the parent instance (which is an
11260 -- instance of the parent of the generic) visible. The parent
11261 -- instance is the prefix of the name of the generic unit.
11262
11263 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
11264 and then Nkind (Gen_Id) = N_Expanded_Name
11265 then
11266 Par_Ent := Entity (Prefix (Gen_Id));
11267 Par_Vis := Is_Immediately_Visible (Par_Ent);
11268 Install_Parent (Par_Ent, In_Body => True);
11269 Parent_Installed := True;
11270
11271 elsif Is_Child_Unit (Gen_Unit) then
11272 Par_Ent := Scope (Gen_Unit);
11273 Par_Vis := Is_Immediately_Visible (Par_Ent);
11274 Install_Parent (Par_Ent, In_Body => True);
11275 Parent_Installed := True;
11276 end if;
11277
11278 -- Subprogram body is placed in the body of wrapper package,
11279 -- whose spec contains the subprogram declaration as well as
11280 -- the renaming declarations for the generic parameters.
11281
11282 Pack_Body :=
11283 Make_Package_Body (Loc,
11284 Defining_Unit_Name => New_Copy (Pack_Id),
11285 Declarations => New_List (Act_Body));
11286
11287 Set_Corresponding_Spec (Pack_Body, Pack_Id);
11288
11289 -- If the instantiation is a library unit, then build resulting
11290 -- compilation unit nodes for the instance. The declaration of
11291 -- the enclosing package is the grandparent of the subprogram
11292 -- declaration. First replace the instantiation node as the unit
11293 -- of the corresponding compilation.
11294
11295 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
11296 if Parent (Inst_Node) = Cunit (Main_Unit) then
11297 Set_Unit (Parent (Inst_Node), Inst_Node);
11298 Build_Instance_Compilation_Unit_Nodes
11299 (Inst_Node, Pack_Body, Parent (Parent (Act_Decl)));
11300 Analyze (Inst_Node);
11301 else
11302 Set_Parent (Pack_Body, Parent (Inst_Node));
11303 Analyze (Pack_Body);
11304 end if;
11305
11306 else
11307 Insert_Before (Inst_Node, Pack_Body);
11308 Mark_Rewrite_Insertion (Pack_Body);
11309 Analyze (Pack_Body);
11310
11311 if Expander_Active then
11312 Freeze_Subprogram_Body (Inst_Node, Gen_Body, Pack_Id);
11313 end if;
11314 end if;
11315
11316 Inherit_Context (Gen_Body, Inst_Node);
11317
11318 Restore_Private_Views (Pack_Id, False);
11319
11320 if Parent_Installed then
11321 Remove_Parent (In_Body => True);
11322
11323 -- Restore the previous visibility of the parent
11324
11325 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
11326 end if;
11327
11328 Restore_Env;
11329 Ignore_Pragma_SPARK_Mode := Saved_IPSM;
11330 Style_Check := Saved_Style_Check;
11331 Restore_Warnings (Saved_Warnings);
11332
11333 -- Body not found. Error was emitted already. If there were no previous
11334 -- errors, this may be an instance whose scope is a premature instance.
11335 -- In that case we must insure that the (legal) program does raise
11336 -- program error if executed. We generate a subprogram body for this
11337 -- purpose. See DEC ac30vso.
11338
11339 -- Should not reference proprietary DEC tests in comments ???
11340
11341 elsif Serious_Errors_Detected = 0
11342 and then Nkind (Parent (Inst_Node)) /= N_Compilation_Unit
11343 then
11344 if Body_Optional then
11345 return;
11346
11347 elsif Ekind (Act_Decl_Id) = E_Procedure then
11348 Act_Body :=
11349 Make_Subprogram_Body (Loc,
11350 Specification =>
11351 Make_Procedure_Specification (Loc,
11352 Defining_Unit_Name =>
11353 Make_Defining_Identifier (Loc, Chars (Act_Decl_Id)),
11354 Parameter_Specifications =>
11355 New_Copy_List
11356 (Parameter_Specifications (Parent (Act_Decl_Id)))),
11357
11358 Declarations => Empty_List,
11359 Handled_Statement_Sequence =>
11360 Make_Handled_Sequence_Of_Statements (Loc,
11361 Statements =>
11362 New_List (
11363 Make_Raise_Program_Error (Loc,
11364 Reason =>
11365 PE_Access_Before_Elaboration))));
11366
11367 else
11368 Ret_Expr :=
11369 Make_Raise_Program_Error (Loc,
11370 Reason => PE_Access_Before_Elaboration);
11371
11372 Set_Etype (Ret_Expr, (Etype (Act_Decl_Id)));
11373 Set_Analyzed (Ret_Expr);
11374
11375 Act_Body :=
11376 Make_Subprogram_Body (Loc,
11377 Specification =>
11378 Make_Function_Specification (Loc,
11379 Defining_Unit_Name =>
11380 Make_Defining_Identifier (Loc, Chars (Act_Decl_Id)),
11381 Parameter_Specifications =>
11382 New_Copy_List
11383 (Parameter_Specifications (Parent (Act_Decl_Id))),
11384 Result_Definition =>
11385 New_Occurrence_Of (Etype (Act_Decl_Id), Loc)),
11386
11387 Declarations => Empty_List,
11388 Handled_Statement_Sequence =>
11389 Make_Handled_Sequence_Of_Statements (Loc,
11390 Statements =>
11391 New_List
11392 (Make_Simple_Return_Statement (Loc, Ret_Expr))));
11393 end if;
11394
11395 Pack_Body :=
11396 Make_Package_Body (Loc,
11397 Defining_Unit_Name => New_Copy (Pack_Id),
11398 Declarations => New_List (Act_Body));
11399
11400 Insert_After (Inst_Node, Pack_Body);
11401 Set_Corresponding_Spec (Pack_Body, Pack_Id);
11402 Analyze (Pack_Body);
11403 end if;
11404
11405 Expander_Mode_Restore;
11406 end Instantiate_Subprogram_Body;
11407
11408 ----------------------
11409 -- Instantiate_Type --
11410 ----------------------
11411
11412 function Instantiate_Type
11413 (Formal : Node_Id;
11414 Actual : Node_Id;
11415 Analyzed_Formal : Node_Id;
11416 Actual_Decls : List_Id) return List_Id
11417 is
11418 Gen_T : constant Entity_Id := Defining_Identifier (Formal);
11419 A_Gen_T : constant Entity_Id :=
11420 Defining_Identifier (Analyzed_Formal);
11421 Ancestor : Entity_Id := Empty;
11422 Def : constant Node_Id := Formal_Type_Definition (Formal);
11423 Act_T : Entity_Id;
11424 Decl_Node : Node_Id;
11425 Decl_Nodes : List_Id;
11426 Loc : Source_Ptr;
11427 Subt : Entity_Id;
11428
11429 procedure Diagnose_Predicated_Actual;
11430 -- There are a number of constructs in which a discrete type with
11431 -- predicates is illegal, e.g. as an index in an array type declaration.
11432 -- If a generic type is used is such a construct in a generic package
11433 -- declaration, it carries the flag No_Predicate_On_Actual. it is part
11434 -- of the generic contract that the actual cannot have predicates.
11435
11436 procedure Validate_Array_Type_Instance;
11437 procedure Validate_Access_Subprogram_Instance;
11438 procedure Validate_Access_Type_Instance;
11439 procedure Validate_Derived_Type_Instance;
11440 procedure Validate_Derived_Interface_Type_Instance;
11441 procedure Validate_Discriminated_Formal_Type;
11442 procedure Validate_Interface_Type_Instance;
11443 procedure Validate_Private_Type_Instance;
11444 procedure Validate_Incomplete_Type_Instance;
11445 -- These procedures perform validation tests for the named case.
11446 -- Validate_Discriminated_Formal_Type is shared by formal private
11447 -- types and Ada 2012 formal incomplete types.
11448
11449 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean;
11450 -- Check that base types are the same and that the subtypes match
11451 -- statically. Used in several of the above.
11452
11453 ---------------------------------
11454 -- Diagnose_Predicated_Actual --
11455 ---------------------------------
11456
11457 procedure Diagnose_Predicated_Actual is
11458 begin
11459 if No_Predicate_On_Actual (A_Gen_T)
11460 and then Has_Predicates (Act_T)
11461 then
11462 Error_Msg_NE
11463 ("actual for& cannot be a type with predicate",
11464 Instantiation_Node, A_Gen_T);
11465
11466 elsif No_Dynamic_Predicate_On_Actual (A_Gen_T)
11467 and then Has_Predicates (Act_T)
11468 and then not Has_Static_Predicate_Aspect (Act_T)
11469 then
11470 Error_Msg_NE
11471 ("actual for& cannot be a type with a dynamic predicate",
11472 Instantiation_Node, A_Gen_T);
11473 end if;
11474 end Diagnose_Predicated_Actual;
11475
11476 --------------------
11477 -- Subtypes_Match --
11478 --------------------
11479
11480 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean is
11481 T : constant Entity_Id := Get_Instance_Of (Gen_T);
11482
11483 begin
11484 -- Some detailed comments would be useful here ???
11485
11486 return ((Base_Type (T) = Act_T
11487 or else Base_Type (T) = Base_Type (Act_T))
11488 and then Subtypes_Statically_Match (T, Act_T))
11489
11490 or else (Is_Class_Wide_Type (Gen_T)
11491 and then Is_Class_Wide_Type (Act_T)
11492 and then Subtypes_Match
11493 (Get_Instance_Of (Root_Type (Gen_T)),
11494 Root_Type (Act_T)))
11495
11496 or else
11497 (Ekind_In (Gen_T, E_Anonymous_Access_Subprogram_Type,
11498 E_Anonymous_Access_Type)
11499 and then Ekind (Act_T) = Ekind (Gen_T)
11500 and then Subtypes_Statically_Match
11501 (Designated_Type (Gen_T), Designated_Type (Act_T)));
11502 end Subtypes_Match;
11503
11504 -----------------------------------------
11505 -- Validate_Access_Subprogram_Instance --
11506 -----------------------------------------
11507
11508 procedure Validate_Access_Subprogram_Instance is
11509 begin
11510 if not Is_Access_Type (Act_T)
11511 or else Ekind (Designated_Type (Act_T)) /= E_Subprogram_Type
11512 then
11513 Error_Msg_NE
11514 ("expect access type in instantiation of &", Actual, Gen_T);
11515 Abandon_Instantiation (Actual);
11516 end if;
11517
11518 -- According to AI05-288, actuals for access_to_subprograms must be
11519 -- subtype conformant with the generic formal. Previous to AI05-288
11520 -- only mode conformance was required.
11521
11522 -- This is a binding interpretation that applies to previous versions
11523 -- of the language, no need to maintain previous weaker checks.
11524
11525 Check_Subtype_Conformant
11526 (Designated_Type (Act_T),
11527 Designated_Type (A_Gen_T),
11528 Actual,
11529 Get_Inst => True);
11530
11531 if Ekind (Base_Type (Act_T)) = E_Access_Protected_Subprogram_Type then
11532 if Ekind (A_Gen_T) = E_Access_Subprogram_Type then
11533 Error_Msg_NE
11534 ("protected access type not allowed for formal &",
11535 Actual, Gen_T);
11536 end if;
11537
11538 elsif Ekind (A_Gen_T) = E_Access_Protected_Subprogram_Type then
11539 Error_Msg_NE
11540 ("expect protected access type for formal &",
11541 Actual, Gen_T);
11542 end if;
11543
11544 -- If the formal has a specified convention (which in most cases
11545 -- will be StdCall) verify that the actual has the same convention.
11546
11547 if Has_Convention_Pragma (A_Gen_T)
11548 and then Convention (A_Gen_T) /= Convention (Act_T)
11549 then
11550 Error_Msg_Name_1 := Get_Convention_Name (Convention (A_Gen_T));
11551 Error_Msg_NE
11552 ("actual for formal & must have convention %", Actual, Gen_T);
11553 end if;
11554 end Validate_Access_Subprogram_Instance;
11555
11556 -----------------------------------
11557 -- Validate_Access_Type_Instance --
11558 -----------------------------------
11559
11560 procedure Validate_Access_Type_Instance is
11561 Desig_Type : constant Entity_Id :=
11562 Find_Actual_Type (Designated_Type (A_Gen_T), A_Gen_T);
11563 Desig_Act : Entity_Id;
11564
11565 begin
11566 if not Is_Access_Type (Act_T) then
11567 Error_Msg_NE
11568 ("expect access type in instantiation of &", Actual, Gen_T);
11569 Abandon_Instantiation (Actual);
11570 end if;
11571
11572 if Is_Access_Constant (A_Gen_T) then
11573 if not Is_Access_Constant (Act_T) then
11574 Error_Msg_N
11575 ("actual type must be access-to-constant type", Actual);
11576 Abandon_Instantiation (Actual);
11577 end if;
11578 else
11579 if Is_Access_Constant (Act_T) then
11580 Error_Msg_N
11581 ("actual type must be access-to-variable type", Actual);
11582 Abandon_Instantiation (Actual);
11583
11584 elsif Ekind (A_Gen_T) = E_General_Access_Type
11585 and then Ekind (Base_Type (Act_T)) /= E_General_Access_Type
11586 then
11587 Error_Msg_N -- CODEFIX
11588 ("actual must be general access type!", Actual);
11589 Error_Msg_NE -- CODEFIX
11590 ("add ALL to }!", Actual, Act_T);
11591 Abandon_Instantiation (Actual);
11592 end if;
11593 end if;
11594
11595 -- The designated subtypes, that is to say the subtypes introduced
11596 -- by an access type declaration (and not by a subtype declaration)
11597 -- must match.
11598
11599 Desig_Act := Designated_Type (Base_Type (Act_T));
11600
11601 -- The designated type may have been introduced through a limited_
11602 -- with clause, in which case retrieve the non-limited view. This
11603 -- applies to incomplete types as well as to class-wide types.
11604
11605 if From_Limited_With (Desig_Act) then
11606 Desig_Act := Available_View (Desig_Act);
11607 end if;
11608
11609 if not Subtypes_Match (Desig_Type, Desig_Act) then
11610 Error_Msg_NE
11611 ("designated type of actual does not match that of formal &",
11612 Actual, Gen_T);
11613
11614 if not Predicates_Match (Desig_Type, Desig_Act) then
11615 Error_Msg_N ("\predicates do not match", Actual);
11616 end if;
11617
11618 Abandon_Instantiation (Actual);
11619
11620 elsif Is_Access_Type (Designated_Type (Act_T))
11621 and then Is_Constrained (Designated_Type (Designated_Type (Act_T)))
11622 /=
11623 Is_Constrained (Designated_Type (Desig_Type))
11624 then
11625 Error_Msg_NE
11626 ("designated type of actual does not match that of formal &",
11627 Actual, Gen_T);
11628
11629 if not Predicates_Match (Desig_Type, Desig_Act) then
11630 Error_Msg_N ("\predicates do not match", Actual);
11631 end if;
11632
11633 Abandon_Instantiation (Actual);
11634 end if;
11635
11636 -- Ada 2005: null-exclusion indicators of the two types must agree
11637
11638 if Can_Never_Be_Null (A_Gen_T) /= Can_Never_Be_Null (Act_T) then
11639 Error_Msg_NE
11640 ("non null exclusion of actual and formal & do not match",
11641 Actual, Gen_T);
11642 end if;
11643 end Validate_Access_Type_Instance;
11644
11645 ----------------------------------
11646 -- Validate_Array_Type_Instance --
11647 ----------------------------------
11648
11649 procedure Validate_Array_Type_Instance is
11650 I1 : Node_Id;
11651 I2 : Node_Id;
11652 T2 : Entity_Id;
11653
11654 function Formal_Dimensions return Nat;
11655 -- Count number of dimensions in array type formal
11656
11657 -----------------------
11658 -- Formal_Dimensions --
11659 -----------------------
11660
11661 function Formal_Dimensions return Nat is
11662 Num : Nat := 0;
11663 Index : Node_Id;
11664
11665 begin
11666 if Nkind (Def) = N_Constrained_Array_Definition then
11667 Index := First (Discrete_Subtype_Definitions (Def));
11668 else
11669 Index := First (Subtype_Marks (Def));
11670 end if;
11671
11672 while Present (Index) loop
11673 Num := Num + 1;
11674 Next_Index (Index);
11675 end loop;
11676
11677 return Num;
11678 end Formal_Dimensions;
11679
11680 -- Start of processing for Validate_Array_Type_Instance
11681
11682 begin
11683 if not Is_Array_Type (Act_T) then
11684 Error_Msg_NE
11685 ("expect array type in instantiation of &", Actual, Gen_T);
11686 Abandon_Instantiation (Actual);
11687
11688 elsif Nkind (Def) = N_Constrained_Array_Definition then
11689 if not (Is_Constrained (Act_T)) then
11690 Error_Msg_NE
11691 ("expect constrained array in instantiation of &",
11692 Actual, Gen_T);
11693 Abandon_Instantiation (Actual);
11694 end if;
11695
11696 else
11697 if Is_Constrained (Act_T) then
11698 Error_Msg_NE
11699 ("expect unconstrained array in instantiation of &",
11700 Actual, Gen_T);
11701 Abandon_Instantiation (Actual);
11702 end if;
11703 end if;
11704
11705 if Formal_Dimensions /= Number_Dimensions (Act_T) then
11706 Error_Msg_NE
11707 ("dimensions of actual do not match formal &", Actual, Gen_T);
11708 Abandon_Instantiation (Actual);
11709 end if;
11710
11711 I1 := First_Index (A_Gen_T);
11712 I2 := First_Index (Act_T);
11713 for J in 1 .. Formal_Dimensions loop
11714
11715 -- If the indexes of the actual were given by a subtype_mark,
11716 -- the index was transformed into a range attribute. Retrieve
11717 -- the original type mark for checking.
11718
11719 if Is_Entity_Name (Original_Node (I2)) then
11720 T2 := Entity (Original_Node (I2));
11721 else
11722 T2 := Etype (I2);
11723 end if;
11724
11725 if not Subtypes_Match
11726 (Find_Actual_Type (Etype (I1), A_Gen_T), T2)
11727 then
11728 Error_Msg_NE
11729 ("index types of actual do not match those of formal &",
11730 Actual, Gen_T);
11731 Abandon_Instantiation (Actual);
11732 end if;
11733
11734 Next_Index (I1);
11735 Next_Index (I2);
11736 end loop;
11737
11738 -- Check matching subtypes. Note that there are complex visibility
11739 -- issues when the generic is a child unit and some aspect of the
11740 -- generic type is declared in a parent unit of the generic. We do
11741 -- the test to handle this special case only after a direct check
11742 -- for static matching has failed. The case where both the component
11743 -- type and the array type are separate formals, and the component
11744 -- type is a private view may also require special checking in
11745 -- Subtypes_Match.
11746
11747 if Subtypes_Match
11748 (Component_Type (A_Gen_T), Component_Type (Act_T))
11749 or else
11750 Subtypes_Match
11751 (Find_Actual_Type (Component_Type (A_Gen_T), A_Gen_T),
11752 Component_Type (Act_T))
11753 then
11754 null;
11755 else
11756 Error_Msg_NE
11757 ("component subtype of actual does not match that of formal &",
11758 Actual, Gen_T);
11759 Abandon_Instantiation (Actual);
11760 end if;
11761
11762 if Has_Aliased_Components (A_Gen_T)
11763 and then not Has_Aliased_Components (Act_T)
11764 then
11765 Error_Msg_NE
11766 ("actual must have aliased components to match formal type &",
11767 Actual, Gen_T);
11768 end if;
11769 end Validate_Array_Type_Instance;
11770
11771 -----------------------------------------------
11772 -- Validate_Derived_Interface_Type_Instance --
11773 -----------------------------------------------
11774
11775 procedure Validate_Derived_Interface_Type_Instance is
11776 Par : constant Entity_Id := Entity (Subtype_Indication (Def));
11777 Elmt : Elmt_Id;
11778
11779 begin
11780 -- First apply interface instance checks
11781
11782 Validate_Interface_Type_Instance;
11783
11784 -- Verify that immediate parent interface is an ancestor of
11785 -- the actual.
11786
11787 if Present (Par)
11788 and then not Interface_Present_In_Ancestor (Act_T, Par)
11789 then
11790 Error_Msg_NE
11791 ("interface actual must include progenitor&", Actual, Par);
11792 end if;
11793
11794 -- Now verify that the actual includes all other ancestors of
11795 -- the formal.
11796
11797 Elmt := First_Elmt (Interfaces (A_Gen_T));
11798 while Present (Elmt) loop
11799 if not Interface_Present_In_Ancestor
11800 (Act_T, Get_Instance_Of (Node (Elmt)))
11801 then
11802 Error_Msg_NE
11803 ("interface actual must include progenitor&",
11804 Actual, Node (Elmt));
11805 end if;
11806
11807 Next_Elmt (Elmt);
11808 end loop;
11809 end Validate_Derived_Interface_Type_Instance;
11810
11811 ------------------------------------
11812 -- Validate_Derived_Type_Instance --
11813 ------------------------------------
11814
11815 procedure Validate_Derived_Type_Instance is
11816 Actual_Discr : Entity_Id;
11817 Ancestor_Discr : Entity_Id;
11818
11819 begin
11820 -- If the parent type in the generic declaration is itself a previous
11821 -- formal type, then it is local to the generic and absent from the
11822 -- analyzed generic definition. In that case the ancestor is the
11823 -- instance of the formal (which must have been instantiated
11824 -- previously), unless the ancestor is itself a formal derived type.
11825 -- In this latter case (which is the subject of Corrigendum 8652/0038
11826 -- (AI-202) the ancestor of the formals is the ancestor of its
11827 -- parent. Otherwise, the analyzed generic carries the parent type.
11828 -- If the parent type is defined in a previous formal package, then
11829 -- the scope of that formal package is that of the generic type
11830 -- itself, and it has already been mapped into the corresponding type
11831 -- in the actual package.
11832
11833 -- Common case: parent type defined outside of the generic
11834
11835 if Is_Entity_Name (Subtype_Mark (Def))
11836 and then Present (Entity (Subtype_Mark (Def)))
11837 then
11838 Ancestor := Get_Instance_Of (Entity (Subtype_Mark (Def)));
11839
11840 -- Check whether parent is defined in a previous formal package
11841
11842 elsif
11843 Scope (Scope (Base_Type (Etype (A_Gen_T)))) = Scope (A_Gen_T)
11844 then
11845 Ancestor :=
11846 Get_Instance_Of (Base_Type (Etype (A_Gen_T)));
11847
11848 -- The type may be a local derivation, or a type extension of a
11849 -- previous formal, or of a formal of a parent package.
11850
11851 elsif Is_Derived_Type (Get_Instance_Of (A_Gen_T))
11852 or else
11853 Ekind (Get_Instance_Of (A_Gen_T)) = E_Record_Type_With_Private
11854 then
11855 -- Check whether the parent is another derived formal type in the
11856 -- same generic unit.
11857
11858 if Etype (A_Gen_T) /= A_Gen_T
11859 and then Is_Generic_Type (Etype (A_Gen_T))
11860 and then Scope (Etype (A_Gen_T)) = Scope (A_Gen_T)
11861 and then Etype (Etype (A_Gen_T)) /= Etype (A_Gen_T)
11862 then
11863 -- Locate ancestor of parent from the subtype declaration
11864 -- created for the actual.
11865
11866 declare
11867 Decl : Node_Id;
11868
11869 begin
11870 Decl := First (Actual_Decls);
11871 while Present (Decl) loop
11872 if Nkind (Decl) = N_Subtype_Declaration
11873 and then Chars (Defining_Identifier (Decl)) =
11874 Chars (Etype (A_Gen_T))
11875 then
11876 Ancestor := Generic_Parent_Type (Decl);
11877 exit;
11878 else
11879 Next (Decl);
11880 end if;
11881 end loop;
11882 end;
11883
11884 pragma Assert (Present (Ancestor));
11885
11886 -- The ancestor itself may be a previous formal that has been
11887 -- instantiated.
11888
11889 Ancestor := Get_Instance_Of (Ancestor);
11890
11891 else
11892 Ancestor :=
11893 Get_Instance_Of (Base_Type (Get_Instance_Of (A_Gen_T)));
11894 end if;
11895
11896 -- Check whether parent is a previous formal of the current generic
11897
11898 elsif Is_Derived_Type (A_Gen_T)
11899 and then Is_Generic_Type (Etype (A_Gen_T))
11900 and then Scope (A_Gen_T) = Scope (Etype (A_Gen_T))
11901 then
11902 Ancestor := Get_Instance_Of (First_Subtype (Etype (A_Gen_T)));
11903
11904 -- An unusual case: the actual is a type declared in a parent unit,
11905 -- but is not a formal type so there is no instance_of for it.
11906 -- Retrieve it by analyzing the record extension.
11907
11908 elsif Is_Child_Unit (Scope (A_Gen_T))
11909 and then In_Open_Scopes (Scope (Act_T))
11910 and then Is_Generic_Instance (Scope (Act_T))
11911 then
11912 Analyze (Subtype_Mark (Def));
11913 Ancestor := Entity (Subtype_Mark (Def));
11914
11915 else
11916 Ancestor := Get_Instance_Of (Etype (Base_Type (A_Gen_T)));
11917 end if;
11918
11919 -- If the formal derived type has pragma Preelaborable_Initialization
11920 -- then the actual type must have preelaborable initialization.
11921
11922 if Known_To_Have_Preelab_Init (A_Gen_T)
11923 and then not Has_Preelaborable_Initialization (Act_T)
11924 then
11925 Error_Msg_NE
11926 ("actual for & must have preelaborable initialization",
11927 Actual, Gen_T);
11928 end if;
11929
11930 -- Ada 2005 (AI-251)
11931
11932 if Ada_Version >= Ada_2005 and then Is_Interface (Ancestor) then
11933 if not Interface_Present_In_Ancestor (Act_T, Ancestor) then
11934 Error_Msg_NE
11935 ("(Ada 2005) expected type implementing & in instantiation",
11936 Actual, Ancestor);
11937 end if;
11938
11939 -- Finally verify that the (instance of) the ancestor is an ancestor
11940 -- of the actual.
11941
11942 elsif not Is_Ancestor (Base_Type (Ancestor), Act_T) then
11943 Error_Msg_NE
11944 ("expect type derived from & in instantiation",
11945 Actual, First_Subtype (Ancestor));
11946 Abandon_Instantiation (Actual);
11947 end if;
11948
11949 -- Ada 2005 (AI-443): Synchronized formal derived type checks. Note
11950 -- that the formal type declaration has been rewritten as a private
11951 -- extension.
11952
11953 if Ada_Version >= Ada_2005
11954 and then Nkind (Parent (A_Gen_T)) = N_Private_Extension_Declaration
11955 and then Synchronized_Present (Parent (A_Gen_T))
11956 then
11957 -- The actual must be a synchronized tagged type
11958
11959 if not Is_Tagged_Type (Act_T) then
11960 Error_Msg_N
11961 ("actual of synchronized type must be tagged", Actual);
11962 Abandon_Instantiation (Actual);
11963
11964 elsif Nkind (Parent (Act_T)) = N_Full_Type_Declaration
11965 and then Nkind (Type_Definition (Parent (Act_T))) =
11966 N_Derived_Type_Definition
11967 and then not Synchronized_Present
11968 (Type_Definition (Parent (Act_T)))
11969 then
11970 Error_Msg_N
11971 ("actual of synchronized type must be synchronized", Actual);
11972 Abandon_Instantiation (Actual);
11973 end if;
11974 end if;
11975
11976 -- Perform atomic/volatile checks (RM C.6(12)). Note that AI05-0218-1
11977 -- removes the second instance of the phrase "or allow pass by copy".
11978
11979 if Is_Atomic (Act_T) and then not Is_Atomic (Ancestor) then
11980 Error_Msg_N
11981 ("cannot have atomic actual type for non-atomic formal type",
11982 Actual);
11983
11984 elsif Is_Volatile (Act_T) and then not Is_Volatile (Ancestor) then
11985 Error_Msg_N
11986 ("cannot have volatile actual type for non-volatile formal type",
11987 Actual);
11988 end if;
11989
11990 -- It should not be necessary to check for unknown discriminants on
11991 -- Formal, but for some reason Has_Unknown_Discriminants is false for
11992 -- A_Gen_T, so Is_Definite_Subtype incorrectly returns True. This
11993 -- needs fixing. ???
11994
11995 if Is_Definite_Subtype (A_Gen_T)
11996 and then not Unknown_Discriminants_Present (Formal)
11997 and then not Is_Definite_Subtype (Act_T)
11998 then
11999 Error_Msg_N ("actual subtype must be constrained", Actual);
12000 Abandon_Instantiation (Actual);
12001 end if;
12002
12003 if not Unknown_Discriminants_Present (Formal) then
12004 if Is_Constrained (Ancestor) then
12005 if not Is_Constrained (Act_T) then
12006 Error_Msg_N ("actual subtype must be constrained", Actual);
12007 Abandon_Instantiation (Actual);
12008 end if;
12009
12010 -- Ancestor is unconstrained, Check if generic formal and actual
12011 -- agree on constrainedness. The check only applies to array types
12012 -- and discriminated types.
12013
12014 elsif Is_Constrained (Act_T) then
12015 if Ekind (Ancestor) = E_Access_Type
12016 or else (not Is_Constrained (A_Gen_T)
12017 and then Is_Composite_Type (A_Gen_T))
12018 then
12019 Error_Msg_N ("actual subtype must be unconstrained", Actual);
12020 Abandon_Instantiation (Actual);
12021 end if;
12022
12023 -- A class-wide type is only allowed if the formal has unknown
12024 -- discriminants.
12025
12026 elsif Is_Class_Wide_Type (Act_T)
12027 and then not Has_Unknown_Discriminants (Ancestor)
12028 then
12029 Error_Msg_NE
12030 ("actual for & cannot be a class-wide type", Actual, Gen_T);
12031 Abandon_Instantiation (Actual);
12032
12033 -- Otherwise, the formal and actual must have the same number
12034 -- of discriminants and each discriminant of the actual must
12035 -- correspond to a discriminant of the formal.
12036
12037 elsif Has_Discriminants (Act_T)
12038 and then not Has_Unknown_Discriminants (Act_T)
12039 and then Has_Discriminants (Ancestor)
12040 then
12041 Actual_Discr := First_Discriminant (Act_T);
12042 Ancestor_Discr := First_Discriminant (Ancestor);
12043 while Present (Actual_Discr)
12044 and then Present (Ancestor_Discr)
12045 loop
12046 if Base_Type (Act_T) /= Base_Type (Ancestor) and then
12047 No (Corresponding_Discriminant (Actual_Discr))
12048 then
12049 Error_Msg_NE
12050 ("discriminant & does not correspond "
12051 & "to ancestor discriminant", Actual, Actual_Discr);
12052 Abandon_Instantiation (Actual);
12053 end if;
12054
12055 Next_Discriminant (Actual_Discr);
12056 Next_Discriminant (Ancestor_Discr);
12057 end loop;
12058
12059 if Present (Actual_Discr) or else Present (Ancestor_Discr) then
12060 Error_Msg_NE
12061 ("actual for & must have same number of discriminants",
12062 Actual, Gen_T);
12063 Abandon_Instantiation (Actual);
12064 end if;
12065
12066 -- This case should be caught by the earlier check for
12067 -- constrainedness, but the check here is added for completeness.
12068
12069 elsif Has_Discriminants (Act_T)
12070 and then not Has_Unknown_Discriminants (Act_T)
12071 then
12072 Error_Msg_NE
12073 ("actual for & must not have discriminants", Actual, Gen_T);
12074 Abandon_Instantiation (Actual);
12075
12076 elsif Has_Discriminants (Ancestor) then
12077 Error_Msg_NE
12078 ("actual for & must have known discriminants", Actual, Gen_T);
12079 Abandon_Instantiation (Actual);
12080 end if;
12081
12082 if not Subtypes_Statically_Compatible
12083 (Act_T, Ancestor, Formal_Derived_Matching => True)
12084 then
12085 Error_Msg_N
12086 ("constraint on actual is incompatible with formal", Actual);
12087 Abandon_Instantiation (Actual);
12088 end if;
12089 end if;
12090
12091 -- If the formal and actual types are abstract, check that there
12092 -- are no abstract primitives of the actual type that correspond to
12093 -- nonabstract primitives of the formal type (second sentence of
12094 -- RM95 3.9.3(9)).
12095
12096 if Is_Abstract_Type (A_Gen_T) and then Is_Abstract_Type (Act_T) then
12097 Check_Abstract_Primitives : declare
12098 Gen_Prims : constant Elist_Id :=
12099 Primitive_Operations (A_Gen_T);
12100 Gen_Elmt : Elmt_Id;
12101 Gen_Subp : Entity_Id;
12102 Anc_Subp : Entity_Id;
12103 Anc_Formal : Entity_Id;
12104 Anc_F_Type : Entity_Id;
12105
12106 Act_Prims : constant Elist_Id := Primitive_Operations (Act_T);
12107 Act_Elmt : Elmt_Id;
12108 Act_Subp : Entity_Id;
12109 Act_Formal : Entity_Id;
12110 Act_F_Type : Entity_Id;
12111
12112 Subprograms_Correspond : Boolean;
12113
12114 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean;
12115 -- Returns true if T2 is derived directly or indirectly from
12116 -- T1, including derivations from interfaces. T1 and T2 are
12117 -- required to be specific tagged base types.
12118
12119 ------------------------
12120 -- Is_Tagged_Ancestor --
12121 ------------------------
12122
12123 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean
12124 is
12125 Intfc_Elmt : Elmt_Id;
12126
12127 begin
12128 -- The predicate is satisfied if the types are the same
12129
12130 if T1 = T2 then
12131 return True;
12132
12133 -- If we've reached the top of the derivation chain then
12134 -- we know that T1 is not an ancestor of T2.
12135
12136 elsif Etype (T2) = T2 then
12137 return False;
12138
12139 -- Proceed to check T2's immediate parent
12140
12141 elsif Is_Ancestor (T1, Base_Type (Etype (T2))) then
12142 return True;
12143
12144 -- Finally, check to see if T1 is an ancestor of any of T2's
12145 -- progenitors.
12146
12147 else
12148 Intfc_Elmt := First_Elmt (Interfaces (T2));
12149 while Present (Intfc_Elmt) loop
12150 if Is_Ancestor (T1, Node (Intfc_Elmt)) then
12151 return True;
12152 end if;
12153
12154 Next_Elmt (Intfc_Elmt);
12155 end loop;
12156 end if;
12157
12158 return False;
12159 end Is_Tagged_Ancestor;
12160
12161 -- Start of processing for Check_Abstract_Primitives
12162
12163 begin
12164 -- Loop over all of the formal derived type's primitives
12165
12166 Gen_Elmt := First_Elmt (Gen_Prims);
12167 while Present (Gen_Elmt) loop
12168 Gen_Subp := Node (Gen_Elmt);
12169
12170 -- If the primitive of the formal is not abstract, then
12171 -- determine whether there is a corresponding primitive of
12172 -- the actual type that's abstract.
12173
12174 if not Is_Abstract_Subprogram (Gen_Subp) then
12175 Act_Elmt := First_Elmt (Act_Prims);
12176 while Present (Act_Elmt) loop
12177 Act_Subp := Node (Act_Elmt);
12178
12179 -- If we find an abstract primitive of the actual,
12180 -- then we need to test whether it corresponds to the
12181 -- subprogram from which the generic formal primitive
12182 -- is inherited.
12183
12184 if Is_Abstract_Subprogram (Act_Subp) then
12185 Anc_Subp := Alias (Gen_Subp);
12186
12187 -- Test whether we have a corresponding primitive
12188 -- by comparing names, kinds, formal types, and
12189 -- result types.
12190
12191 if Chars (Anc_Subp) = Chars (Act_Subp)
12192 and then Ekind (Anc_Subp) = Ekind (Act_Subp)
12193 then
12194 Anc_Formal := First_Formal (Anc_Subp);
12195 Act_Formal := First_Formal (Act_Subp);
12196 while Present (Anc_Formal)
12197 and then Present (Act_Formal)
12198 loop
12199 Anc_F_Type := Etype (Anc_Formal);
12200 Act_F_Type := Etype (Act_Formal);
12201
12202 if Ekind (Anc_F_Type) =
12203 E_Anonymous_Access_Type
12204 then
12205 Anc_F_Type := Designated_Type (Anc_F_Type);
12206
12207 if Ekind (Act_F_Type) =
12208 E_Anonymous_Access_Type
12209 then
12210 Act_F_Type :=
12211 Designated_Type (Act_F_Type);
12212 else
12213 exit;
12214 end if;
12215
12216 elsif
12217 Ekind (Act_F_Type) = E_Anonymous_Access_Type
12218 then
12219 exit;
12220 end if;
12221
12222 Anc_F_Type := Base_Type (Anc_F_Type);
12223 Act_F_Type := Base_Type (Act_F_Type);
12224
12225 -- If the formal is controlling, then the
12226 -- the type of the actual primitive's formal
12227 -- must be derived directly or indirectly
12228 -- from the type of the ancestor primitive's
12229 -- formal.
12230
12231 if Is_Controlling_Formal (Anc_Formal) then
12232 if not Is_Tagged_Ancestor
12233 (Anc_F_Type, Act_F_Type)
12234 then
12235 exit;
12236 end if;
12237
12238 -- Otherwise the types of the formals must
12239 -- be the same.
12240
12241 elsif Anc_F_Type /= Act_F_Type then
12242 exit;
12243 end if;
12244
12245 Next_Entity (Anc_Formal);
12246 Next_Entity (Act_Formal);
12247 end loop;
12248
12249 -- If we traversed through all of the formals
12250 -- then so far the subprograms correspond, so
12251 -- now check that any result types correspond.
12252
12253 if No (Anc_Formal) and then No (Act_Formal) then
12254 Subprograms_Correspond := True;
12255
12256 if Ekind (Act_Subp) = E_Function then
12257 Anc_F_Type := Etype (Anc_Subp);
12258 Act_F_Type := Etype (Act_Subp);
12259
12260 if Ekind (Anc_F_Type) =
12261 E_Anonymous_Access_Type
12262 then
12263 Anc_F_Type :=
12264 Designated_Type (Anc_F_Type);
12265
12266 if Ekind (Act_F_Type) =
12267 E_Anonymous_Access_Type
12268 then
12269 Act_F_Type :=
12270 Designated_Type (Act_F_Type);
12271 else
12272 Subprograms_Correspond := False;
12273 end if;
12274
12275 elsif
12276 Ekind (Act_F_Type)
12277 = E_Anonymous_Access_Type
12278 then
12279 Subprograms_Correspond := False;
12280 end if;
12281
12282 Anc_F_Type := Base_Type (Anc_F_Type);
12283 Act_F_Type := Base_Type (Act_F_Type);
12284
12285 -- Now either the result types must be
12286 -- the same or, if the result type is
12287 -- controlling, the result type of the
12288 -- actual primitive must descend from the
12289 -- result type of the ancestor primitive.
12290
12291 if Subprograms_Correspond
12292 and then Anc_F_Type /= Act_F_Type
12293 and then
12294 Has_Controlling_Result (Anc_Subp)
12295 and then not Is_Tagged_Ancestor
12296 (Anc_F_Type, Act_F_Type)
12297 then
12298 Subprograms_Correspond := False;
12299 end if;
12300 end if;
12301
12302 -- Found a matching subprogram belonging to
12303 -- formal ancestor type, so actual subprogram
12304 -- corresponds and this violates 3.9.3(9).
12305
12306 if Subprograms_Correspond then
12307 Error_Msg_NE
12308 ("abstract subprogram & overrides "
12309 & "nonabstract subprogram of ancestor",
12310 Actual, Act_Subp);
12311 end if;
12312 end if;
12313 end if;
12314 end if;
12315
12316 Next_Elmt (Act_Elmt);
12317 end loop;
12318 end if;
12319
12320 Next_Elmt (Gen_Elmt);
12321 end loop;
12322 end Check_Abstract_Primitives;
12323 end if;
12324
12325 -- Verify that limitedness matches. If parent is a limited
12326 -- interface then the generic formal is not unless declared
12327 -- explicitly so. If not declared limited, the actual cannot be
12328 -- limited (see AI05-0087).
12329
12330 -- Even though this AI is a binding interpretation, we enable the
12331 -- check only in Ada 2012 mode, because this improper construct
12332 -- shows up in user code and in existing B-tests.
12333
12334 if Is_Limited_Type (Act_T)
12335 and then not Is_Limited_Type (A_Gen_T)
12336 and then Ada_Version >= Ada_2012
12337 then
12338 if In_Instance then
12339 null;
12340 else
12341 Error_Msg_NE
12342 ("actual for non-limited & cannot be a limited type",
12343 Actual, Gen_T);
12344 Explain_Limited_Type (Act_T, Actual);
12345 Abandon_Instantiation (Actual);
12346 end if;
12347 end if;
12348 end Validate_Derived_Type_Instance;
12349
12350 ----------------------------------------
12351 -- Validate_Discriminated_Formal_Type --
12352 ----------------------------------------
12353
12354 procedure Validate_Discriminated_Formal_Type is
12355 Formal_Discr : Entity_Id;
12356 Actual_Discr : Entity_Id;
12357 Formal_Subt : Entity_Id;
12358
12359 begin
12360 if Has_Discriminants (A_Gen_T) then
12361 if not Has_Discriminants (Act_T) then
12362 Error_Msg_NE
12363 ("actual for & must have discriminants", Actual, Gen_T);
12364 Abandon_Instantiation (Actual);
12365
12366 elsif Is_Constrained (Act_T) then
12367 Error_Msg_NE
12368 ("actual for & must be unconstrained", Actual, Gen_T);
12369 Abandon_Instantiation (Actual);
12370
12371 else
12372 Formal_Discr := First_Discriminant (A_Gen_T);
12373 Actual_Discr := First_Discriminant (Act_T);
12374 while Formal_Discr /= Empty loop
12375 if Actual_Discr = Empty then
12376 Error_Msg_NE
12377 ("discriminants on actual do not match formal",
12378 Actual, Gen_T);
12379 Abandon_Instantiation (Actual);
12380 end if;
12381
12382 Formal_Subt := Get_Instance_Of (Etype (Formal_Discr));
12383
12384 -- Access discriminants match if designated types do
12385
12386 if Ekind (Base_Type (Formal_Subt)) = E_Anonymous_Access_Type
12387 and then (Ekind (Base_Type (Etype (Actual_Discr)))) =
12388 E_Anonymous_Access_Type
12389 and then
12390 Get_Instance_Of
12391 (Designated_Type (Base_Type (Formal_Subt))) =
12392 Designated_Type (Base_Type (Etype (Actual_Discr)))
12393 then
12394 null;
12395
12396 elsif Base_Type (Formal_Subt) /=
12397 Base_Type (Etype (Actual_Discr))
12398 then
12399 Error_Msg_NE
12400 ("types of actual discriminants must match formal",
12401 Actual, Gen_T);
12402 Abandon_Instantiation (Actual);
12403
12404 elsif not Subtypes_Statically_Match
12405 (Formal_Subt, Etype (Actual_Discr))
12406 and then Ada_Version >= Ada_95
12407 then
12408 Error_Msg_NE
12409 ("subtypes of actual discriminants must match formal",
12410 Actual, Gen_T);
12411 Abandon_Instantiation (Actual);
12412 end if;
12413
12414 Next_Discriminant (Formal_Discr);
12415 Next_Discriminant (Actual_Discr);
12416 end loop;
12417
12418 if Actual_Discr /= Empty then
12419 Error_Msg_NE
12420 ("discriminants on actual do not match formal",
12421 Actual, Gen_T);
12422 Abandon_Instantiation (Actual);
12423 end if;
12424 end if;
12425 end if;
12426 end Validate_Discriminated_Formal_Type;
12427
12428 ---------------------------------------
12429 -- Validate_Incomplete_Type_Instance --
12430 ---------------------------------------
12431
12432 procedure Validate_Incomplete_Type_Instance is
12433 begin
12434 if not Is_Tagged_Type (Act_T)
12435 and then Is_Tagged_Type (A_Gen_T)
12436 then
12437 Error_Msg_NE
12438 ("actual for & must be a tagged type", Actual, Gen_T);
12439 end if;
12440
12441 Validate_Discriminated_Formal_Type;
12442 end Validate_Incomplete_Type_Instance;
12443
12444 --------------------------------------
12445 -- Validate_Interface_Type_Instance --
12446 --------------------------------------
12447
12448 procedure Validate_Interface_Type_Instance is
12449 begin
12450 if not Is_Interface (Act_T) then
12451 Error_Msg_NE
12452 ("actual for formal interface type must be an interface",
12453 Actual, Gen_T);
12454
12455 elsif Is_Limited_Type (Act_T) /= Is_Limited_Type (A_Gen_T)
12456 or else Is_Task_Interface (A_Gen_T) /= Is_Task_Interface (Act_T)
12457 or else Is_Protected_Interface (A_Gen_T) /=
12458 Is_Protected_Interface (Act_T)
12459 or else Is_Synchronized_Interface (A_Gen_T) /=
12460 Is_Synchronized_Interface (Act_T)
12461 then
12462 Error_Msg_NE
12463 ("actual for interface& does not match (RM 12.5.5(4))",
12464 Actual, Gen_T);
12465 end if;
12466 end Validate_Interface_Type_Instance;
12467
12468 ------------------------------------
12469 -- Validate_Private_Type_Instance --
12470 ------------------------------------
12471
12472 procedure Validate_Private_Type_Instance is
12473 begin
12474 if Is_Limited_Type (Act_T)
12475 and then not Is_Limited_Type (A_Gen_T)
12476 then
12477 if In_Instance then
12478 null;
12479 else
12480 Error_Msg_NE
12481 ("actual for non-limited & cannot be a limited type", Actual,
12482 Gen_T);
12483 Explain_Limited_Type (Act_T, Actual);
12484 Abandon_Instantiation (Actual);
12485 end if;
12486
12487 elsif Known_To_Have_Preelab_Init (A_Gen_T)
12488 and then not Has_Preelaborable_Initialization (Act_T)
12489 then
12490 Error_Msg_NE
12491 ("actual for & must have preelaborable initialization", Actual,
12492 Gen_T);
12493
12494 elsif not Is_Definite_Subtype (Act_T)
12495 and then Is_Definite_Subtype (A_Gen_T)
12496 and then Ada_Version >= Ada_95
12497 then
12498 Error_Msg_NE
12499 ("actual for & must be a definite subtype", Actual, Gen_T);
12500
12501 elsif not Is_Tagged_Type (Act_T)
12502 and then Is_Tagged_Type (A_Gen_T)
12503 then
12504 Error_Msg_NE
12505 ("actual for & must be a tagged type", Actual, Gen_T);
12506 end if;
12507
12508 Validate_Discriminated_Formal_Type;
12509 Ancestor := Gen_T;
12510 end Validate_Private_Type_Instance;
12511
12512 -- Start of processing for Instantiate_Type
12513
12514 begin
12515 if Get_Instance_Of (A_Gen_T) /= A_Gen_T then
12516 Error_Msg_N ("duplicate instantiation of generic type", Actual);
12517 return New_List (Error);
12518
12519 elsif not Is_Entity_Name (Actual)
12520 or else not Is_Type (Entity (Actual))
12521 then
12522 Error_Msg_NE
12523 ("expect valid subtype mark to instantiate &", Actual, Gen_T);
12524 Abandon_Instantiation (Actual);
12525
12526 else
12527 Act_T := Entity (Actual);
12528
12529 -- Ada 2005 (AI-216): An Unchecked_Union subtype shall only be passed
12530 -- as a generic actual parameter if the corresponding formal type
12531 -- does not have a known_discriminant_part, or is a formal derived
12532 -- type that is an Unchecked_Union type.
12533
12534 if Is_Unchecked_Union (Base_Type (Act_T)) then
12535 if not Has_Discriminants (A_Gen_T)
12536 or else (Is_Derived_Type (A_Gen_T)
12537 and then Is_Unchecked_Union (A_Gen_T))
12538 then
12539 null;
12540 else
12541 Error_Msg_N ("unchecked union cannot be the actual for a "
12542 & "discriminated formal type", Act_T);
12543
12544 end if;
12545 end if;
12546
12547 -- Deal with fixed/floating restrictions
12548
12549 if Is_Floating_Point_Type (Act_T) then
12550 Check_Restriction (No_Floating_Point, Actual);
12551 elsif Is_Fixed_Point_Type (Act_T) then
12552 Check_Restriction (No_Fixed_Point, Actual);
12553 end if;
12554
12555 -- Deal with error of using incomplete type as generic actual.
12556 -- This includes limited views of a type, even if the non-limited
12557 -- view may be available.
12558
12559 if Ekind (Act_T) = E_Incomplete_Type
12560 or else (Is_Class_Wide_Type (Act_T)
12561 and then Ekind (Root_Type (Act_T)) = E_Incomplete_Type)
12562 then
12563 -- If the formal is an incomplete type, the actual can be
12564 -- incomplete as well.
12565
12566 if Ekind (A_Gen_T) = E_Incomplete_Type then
12567 null;
12568
12569 elsif Is_Class_Wide_Type (Act_T)
12570 or else No (Full_View (Act_T))
12571 then
12572 Error_Msg_N ("premature use of incomplete type", Actual);
12573 Abandon_Instantiation (Actual);
12574 else
12575 Act_T := Full_View (Act_T);
12576 Set_Entity (Actual, Act_T);
12577
12578 if Has_Private_Component (Act_T) then
12579 Error_Msg_N
12580 ("premature use of type with private component", Actual);
12581 end if;
12582 end if;
12583
12584 -- Deal with error of premature use of private type as generic actual
12585
12586 elsif Is_Private_Type (Act_T)
12587 and then Is_Private_Type (Base_Type (Act_T))
12588 and then not Is_Generic_Type (Act_T)
12589 and then not Is_Derived_Type (Act_T)
12590 and then No (Full_View (Root_Type (Act_T)))
12591 then
12592 -- If the formal is an incomplete type, the actual can be
12593 -- private or incomplete as well.
12594
12595 if Ekind (A_Gen_T) = E_Incomplete_Type then
12596 null;
12597 else
12598 Error_Msg_N ("premature use of private type", Actual);
12599 end if;
12600
12601 elsif Has_Private_Component (Act_T) then
12602 Error_Msg_N
12603 ("premature use of type with private component", Actual);
12604 end if;
12605
12606 Set_Instance_Of (A_Gen_T, Act_T);
12607
12608 -- If the type is generic, the class-wide type may also be used
12609
12610 if Is_Tagged_Type (A_Gen_T)
12611 and then Is_Tagged_Type (Act_T)
12612 and then not Is_Class_Wide_Type (A_Gen_T)
12613 then
12614 Set_Instance_Of (Class_Wide_Type (A_Gen_T),
12615 Class_Wide_Type (Act_T));
12616 end if;
12617
12618 if not Is_Abstract_Type (A_Gen_T)
12619 and then Is_Abstract_Type (Act_T)
12620 then
12621 Error_Msg_N
12622 ("actual of non-abstract formal cannot be abstract", Actual);
12623 end if;
12624
12625 -- A generic scalar type is a first subtype for which we generate
12626 -- an anonymous base type. Indicate that the instance of this base
12627 -- is the base type of the actual.
12628
12629 if Is_Scalar_Type (A_Gen_T) then
12630 Set_Instance_Of (Etype (A_Gen_T), Etype (Act_T));
12631 end if;
12632 end if;
12633
12634 if Error_Posted (Act_T) then
12635 null;
12636 else
12637 case Nkind (Def) is
12638 when N_Formal_Private_Type_Definition =>
12639 Validate_Private_Type_Instance;
12640
12641 when N_Formal_Incomplete_Type_Definition =>
12642 Validate_Incomplete_Type_Instance;
12643
12644 when N_Formal_Derived_Type_Definition =>
12645 Validate_Derived_Type_Instance;
12646
12647 when N_Formal_Discrete_Type_Definition =>
12648 if not Is_Discrete_Type (Act_T) then
12649 Error_Msg_NE
12650 ("expect discrete type in instantiation of&",
12651 Actual, Gen_T);
12652 Abandon_Instantiation (Actual);
12653 end if;
12654
12655 Diagnose_Predicated_Actual;
12656
12657 when N_Formal_Signed_Integer_Type_Definition =>
12658 if not Is_Signed_Integer_Type (Act_T) then
12659 Error_Msg_NE
12660 ("expect signed integer type in instantiation of&",
12661 Actual, Gen_T);
12662 Abandon_Instantiation (Actual);
12663 end if;
12664
12665 Diagnose_Predicated_Actual;
12666
12667 when N_Formal_Modular_Type_Definition =>
12668 if not Is_Modular_Integer_Type (Act_T) then
12669 Error_Msg_NE
12670 ("expect modular type in instantiation of &",
12671 Actual, Gen_T);
12672 Abandon_Instantiation (Actual);
12673 end if;
12674
12675 Diagnose_Predicated_Actual;
12676
12677 when N_Formal_Floating_Point_Definition =>
12678 if not Is_Floating_Point_Type (Act_T) then
12679 Error_Msg_NE
12680 ("expect float type in instantiation of &", Actual, Gen_T);
12681 Abandon_Instantiation (Actual);
12682 end if;
12683
12684 when N_Formal_Ordinary_Fixed_Point_Definition =>
12685 if not Is_Ordinary_Fixed_Point_Type (Act_T) then
12686 Error_Msg_NE
12687 ("expect ordinary fixed point type in instantiation of &",
12688 Actual, Gen_T);
12689 Abandon_Instantiation (Actual);
12690 end if;
12691
12692 when N_Formal_Decimal_Fixed_Point_Definition =>
12693 if not Is_Decimal_Fixed_Point_Type (Act_T) then
12694 Error_Msg_NE
12695 ("expect decimal type in instantiation of &",
12696 Actual, Gen_T);
12697 Abandon_Instantiation (Actual);
12698 end if;
12699
12700 when N_Array_Type_Definition =>
12701 Validate_Array_Type_Instance;
12702
12703 when N_Access_To_Object_Definition =>
12704 Validate_Access_Type_Instance;
12705
12706 when N_Access_Function_Definition |
12707 N_Access_Procedure_Definition =>
12708 Validate_Access_Subprogram_Instance;
12709
12710 when N_Record_Definition =>
12711 Validate_Interface_Type_Instance;
12712
12713 when N_Derived_Type_Definition =>
12714 Validate_Derived_Interface_Type_Instance;
12715
12716 when others =>
12717 raise Program_Error;
12718
12719 end case;
12720 end if;
12721
12722 Subt := New_Copy (Gen_T);
12723
12724 -- Use adjusted sloc of subtype name as the location for other nodes in
12725 -- the subtype declaration.
12726
12727 Loc := Sloc (Subt);
12728
12729 Decl_Node :=
12730 Make_Subtype_Declaration (Loc,
12731 Defining_Identifier => Subt,
12732 Subtype_Indication => New_Occurrence_Of (Act_T, Loc));
12733
12734 if Is_Private_Type (Act_T) then
12735 Set_Has_Private_View (Subtype_Indication (Decl_Node));
12736
12737 elsif Is_Access_Type (Act_T)
12738 and then Is_Private_Type (Designated_Type (Act_T))
12739 then
12740 Set_Has_Private_View (Subtype_Indication (Decl_Node));
12741 end if;
12742
12743 -- In Ada 2012 the actual may be a limited view. Indicate that
12744 -- the local subtype must be treated as such.
12745
12746 if From_Limited_With (Act_T) then
12747 Set_Ekind (Subt, E_Incomplete_Subtype);
12748 Set_From_Limited_With (Subt);
12749 end if;
12750
12751 Decl_Nodes := New_List (Decl_Node);
12752
12753 -- Flag actual derived types so their elaboration produces the
12754 -- appropriate renamings for the primitive operations of the ancestor.
12755 -- Flag actual for formal private types as well, to determine whether
12756 -- operations in the private part may override inherited operations.
12757 -- If the formal has an interface list, the ancestor is not the
12758 -- parent, but the analyzed formal that includes the interface
12759 -- operations of all its progenitors.
12760
12761 -- Same treatment for formal private types, so we can check whether the
12762 -- type is tagged limited when validating derivations in the private
12763 -- part. (See AI05-096).
12764
12765 if Nkind (Def) = N_Formal_Derived_Type_Definition then
12766 if Present (Interface_List (Def)) then
12767 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
12768 else
12769 Set_Generic_Parent_Type (Decl_Node, Ancestor);
12770 end if;
12771
12772 elsif Nkind_In (Def, N_Formal_Private_Type_Definition,
12773 N_Formal_Incomplete_Type_Definition)
12774 then
12775 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
12776 end if;
12777
12778 -- If the actual is a synchronized type that implements an interface,
12779 -- the primitive operations are attached to the corresponding record,
12780 -- and we have to treat it as an additional generic actual, so that its
12781 -- primitive operations become visible in the instance. The task or
12782 -- protected type itself does not carry primitive operations.
12783
12784 if Is_Concurrent_Type (Act_T)
12785 and then Is_Tagged_Type (Act_T)
12786 and then Present (Corresponding_Record_Type (Act_T))
12787 and then Present (Ancestor)
12788 and then Is_Interface (Ancestor)
12789 then
12790 declare
12791 Corr_Rec : constant Entity_Id :=
12792 Corresponding_Record_Type (Act_T);
12793 New_Corr : Entity_Id;
12794 Corr_Decl : Node_Id;
12795
12796 begin
12797 New_Corr := Make_Temporary (Loc, 'S');
12798 Corr_Decl :=
12799 Make_Subtype_Declaration (Loc,
12800 Defining_Identifier => New_Corr,
12801 Subtype_Indication =>
12802 New_Occurrence_Of (Corr_Rec, Loc));
12803 Append_To (Decl_Nodes, Corr_Decl);
12804
12805 if Ekind (Act_T) = E_Task_Type then
12806 Set_Ekind (Subt, E_Task_Subtype);
12807 else
12808 Set_Ekind (Subt, E_Protected_Subtype);
12809 end if;
12810
12811 Set_Corresponding_Record_Type (Subt, Corr_Rec);
12812 Set_Generic_Parent_Type (Corr_Decl, Ancestor);
12813 Set_Generic_Parent_Type (Decl_Node, Empty);
12814 end;
12815 end if;
12816
12817 -- For a floating-point type, capture dimension info if any, because
12818 -- the generated subtype declaration does not come from source and
12819 -- will not process dimensions.
12820
12821 if Is_Floating_Point_Type (Act_T) then
12822 Copy_Dimensions (Act_T, Subt);
12823 end if;
12824
12825 return Decl_Nodes;
12826 end Instantiate_Type;
12827
12828 ---------------------
12829 -- Is_In_Main_Unit --
12830 ---------------------
12831
12832 function Is_In_Main_Unit (N : Node_Id) return Boolean is
12833 Unum : constant Unit_Number_Type := Get_Source_Unit (N);
12834 Current_Unit : Node_Id;
12835
12836 begin
12837 if Unum = Main_Unit then
12838 return True;
12839
12840 -- If the current unit is a subunit then it is either the main unit or
12841 -- is being compiled as part of the main unit.
12842
12843 elsif Nkind (N) = N_Compilation_Unit then
12844 return Nkind (Unit (N)) = N_Subunit;
12845 end if;
12846
12847 Current_Unit := Parent (N);
12848 while Present (Current_Unit)
12849 and then Nkind (Current_Unit) /= N_Compilation_Unit
12850 loop
12851 Current_Unit := Parent (Current_Unit);
12852 end loop;
12853
12854 -- The instantiation node is in the main unit, or else the current node
12855 -- (perhaps as the result of nested instantiations) is in the main unit,
12856 -- or in the declaration of the main unit, which in this last case must
12857 -- be a body.
12858
12859 return
12860 Current_Unit = Cunit (Main_Unit)
12861 or else Current_Unit = Library_Unit (Cunit (Main_Unit))
12862 or else (Present (Library_Unit (Current_Unit))
12863 and then Is_In_Main_Unit (Library_Unit (Current_Unit)));
12864 end Is_In_Main_Unit;
12865
12866 ----------------------------
12867 -- Load_Parent_Of_Generic --
12868 ----------------------------
12869
12870 procedure Load_Parent_Of_Generic
12871 (N : Node_Id;
12872 Spec : Node_Id;
12873 Body_Optional : Boolean := False)
12874 is
12875 Comp_Unit : constant Node_Id := Cunit (Get_Source_Unit (Spec));
12876 Saved_Style_Check : constant Boolean := Style_Check;
12877 Saved_Warnings : constant Warning_Record := Save_Warnings;
12878 True_Parent : Node_Id;
12879 Inst_Node : Node_Id;
12880 OK : Boolean;
12881 Previous_Instances : constant Elist_Id := New_Elmt_List;
12882
12883 procedure Collect_Previous_Instances (Decls : List_Id);
12884 -- Collect all instantiations in the given list of declarations, that
12885 -- precede the generic that we need to load. If the bodies of these
12886 -- instantiations are available, we must analyze them, to ensure that
12887 -- the public symbols generated are the same when the unit is compiled
12888 -- to generate code, and when it is compiled in the context of a unit
12889 -- that needs a particular nested instance. This process is applied to
12890 -- both package and subprogram instances.
12891
12892 --------------------------------
12893 -- Collect_Previous_Instances --
12894 --------------------------------
12895
12896 procedure Collect_Previous_Instances (Decls : List_Id) is
12897 Decl : Node_Id;
12898
12899 begin
12900 Decl := First (Decls);
12901 while Present (Decl) loop
12902 if Sloc (Decl) >= Sloc (Inst_Node) then
12903 return;
12904
12905 -- If Decl is an instantiation, then record it as requiring
12906 -- instantiation of the corresponding body, except if it is an
12907 -- abbreviated instantiation generated internally for conformance
12908 -- checking purposes only for the case of a formal package
12909 -- declared without a box (see Instantiate_Formal_Package). Such
12910 -- an instantiation does not generate any code (the actual code
12911 -- comes from actual) and thus does not need to be analyzed here.
12912 -- If the instantiation appears with a generic package body it is
12913 -- not analyzed here either.
12914
12915 elsif Nkind (Decl) = N_Package_Instantiation
12916 and then not Is_Internal (Defining_Entity (Decl))
12917 then
12918 Append_Elmt (Decl, Previous_Instances);
12919
12920 -- For a subprogram instantiation, omit instantiations intrinsic
12921 -- operations (Unchecked_Conversions, etc.) that have no bodies.
12922
12923 elsif Nkind_In (Decl, N_Function_Instantiation,
12924 N_Procedure_Instantiation)
12925 and then not Is_Intrinsic_Subprogram (Entity (Name (Decl)))
12926 then
12927 Append_Elmt (Decl, Previous_Instances);
12928
12929 elsif Nkind (Decl) = N_Package_Declaration then
12930 Collect_Previous_Instances
12931 (Visible_Declarations (Specification (Decl)));
12932 Collect_Previous_Instances
12933 (Private_Declarations (Specification (Decl)));
12934
12935 -- Previous non-generic bodies may contain instances as well
12936
12937 elsif Nkind (Decl) = N_Package_Body
12938 and then Ekind (Corresponding_Spec (Decl)) /= E_Generic_Package
12939 then
12940 Collect_Previous_Instances (Declarations (Decl));
12941
12942 elsif Nkind (Decl) = N_Subprogram_Body
12943 and then not Acts_As_Spec (Decl)
12944 and then not Is_Generic_Subprogram (Corresponding_Spec (Decl))
12945 then
12946 Collect_Previous_Instances (Declarations (Decl));
12947 end if;
12948
12949 Next (Decl);
12950 end loop;
12951 end Collect_Previous_Instances;
12952
12953 -- Start of processing for Load_Parent_Of_Generic
12954
12955 begin
12956 if not In_Same_Source_Unit (N, Spec)
12957 or else Nkind (Unit (Comp_Unit)) = N_Package_Declaration
12958 or else (Nkind (Unit (Comp_Unit)) = N_Package_Body
12959 and then not Is_In_Main_Unit (Spec))
12960 then
12961 -- Find body of parent of spec, and analyze it. A special case arises
12962 -- when the parent is an instantiation, that is to say when we are
12963 -- currently instantiating a nested generic. In that case, there is
12964 -- no separate file for the body of the enclosing instance. Instead,
12965 -- the enclosing body must be instantiated as if it were a pending
12966 -- instantiation, in order to produce the body for the nested generic
12967 -- we require now. Note that in that case the generic may be defined
12968 -- in a package body, the instance defined in the same package body,
12969 -- and the original enclosing body may not be in the main unit.
12970
12971 Inst_Node := Empty;
12972
12973 True_Parent := Parent (Spec);
12974 while Present (True_Parent)
12975 and then Nkind (True_Parent) /= N_Compilation_Unit
12976 loop
12977 if Nkind (True_Parent) = N_Package_Declaration
12978 and then
12979 Nkind (Original_Node (True_Parent)) = N_Package_Instantiation
12980 then
12981 -- Parent is a compilation unit that is an instantiation.
12982 -- Instantiation node has been replaced with package decl.
12983
12984 Inst_Node := Original_Node (True_Parent);
12985 exit;
12986
12987 elsif Nkind (True_Parent) = N_Package_Declaration
12988 and then Present (Generic_Parent (Specification (True_Parent)))
12989 and then Nkind (Parent (True_Parent)) /= N_Compilation_Unit
12990 then
12991 -- Parent is an instantiation within another specification.
12992 -- Declaration for instance has been inserted before original
12993 -- instantiation node. A direct link would be preferable?
12994
12995 Inst_Node := Next (True_Parent);
12996 while Present (Inst_Node)
12997 and then Nkind (Inst_Node) /= N_Package_Instantiation
12998 loop
12999 Next (Inst_Node);
13000 end loop;
13001
13002 -- If the instance appears within a generic, and the generic
13003 -- unit is defined within a formal package of the enclosing
13004 -- generic, there is no generic body available, and none
13005 -- needed. A more precise test should be used ???
13006
13007 if No (Inst_Node) then
13008 return;
13009 end if;
13010
13011 exit;
13012
13013 else
13014 True_Parent := Parent (True_Parent);
13015 end if;
13016 end loop;
13017
13018 -- Case where we are currently instantiating a nested generic
13019
13020 if Present (Inst_Node) then
13021 if Nkind (Parent (True_Parent)) = N_Compilation_Unit then
13022
13023 -- Instantiation node and declaration of instantiated package
13024 -- were exchanged when only the declaration was needed.
13025 -- Restore instantiation node before proceeding with body.
13026
13027 Set_Unit (Parent (True_Parent), Inst_Node);
13028 end if;
13029
13030 -- Now complete instantiation of enclosing body, if it appears in
13031 -- some other unit. If it appears in the current unit, the body
13032 -- will have been instantiated already.
13033
13034 if No (Corresponding_Body (Instance_Spec (Inst_Node))) then
13035
13036 -- We need to determine the expander mode to instantiate the
13037 -- enclosing body. Because the generic body we need may use
13038 -- global entities declared in the enclosing package (including
13039 -- aggregates) it is in general necessary to compile this body
13040 -- with expansion enabled, except if we are within a generic
13041 -- package, in which case the usual generic rule applies.
13042
13043 declare
13044 Exp_Status : Boolean := True;
13045 Scop : Entity_Id;
13046
13047 begin
13048 -- Loop through scopes looking for generic package
13049
13050 Scop := Scope (Defining_Entity (Instance_Spec (Inst_Node)));
13051 while Present (Scop)
13052 and then Scop /= Standard_Standard
13053 loop
13054 if Ekind (Scop) = E_Generic_Package then
13055 Exp_Status := False;
13056 exit;
13057 end if;
13058
13059 Scop := Scope (Scop);
13060 end loop;
13061
13062 -- Collect previous instantiations in the unit that contains
13063 -- the desired generic.
13064
13065 if Nkind (Parent (True_Parent)) /= N_Compilation_Unit
13066 and then not Body_Optional
13067 then
13068 declare
13069 Decl : Elmt_Id;
13070 Info : Pending_Body_Info;
13071 Par : Node_Id;
13072
13073 begin
13074 Par := Parent (Inst_Node);
13075 while Present (Par) loop
13076 exit when Nkind (Parent (Par)) = N_Compilation_Unit;
13077 Par := Parent (Par);
13078 end loop;
13079
13080 pragma Assert (Present (Par));
13081
13082 if Nkind (Par) = N_Package_Body then
13083 Collect_Previous_Instances (Declarations (Par));
13084
13085 elsif Nkind (Par) = N_Package_Declaration then
13086 Collect_Previous_Instances
13087 (Visible_Declarations (Specification (Par)));
13088 Collect_Previous_Instances
13089 (Private_Declarations (Specification (Par)));
13090
13091 else
13092 -- Enclosing unit is a subprogram body. In this
13093 -- case all instance bodies are processed in order
13094 -- and there is no need to collect them separately.
13095
13096 null;
13097 end if;
13098
13099 Decl := First_Elmt (Previous_Instances);
13100 while Present (Decl) loop
13101 Info :=
13102 (Inst_Node => Node (Decl),
13103 Act_Decl =>
13104 Instance_Spec (Node (Decl)),
13105 Expander_Status => Exp_Status,
13106 Current_Sem_Unit =>
13107 Get_Code_Unit (Sloc (Node (Decl))),
13108 Scope_Suppress => Scope_Suppress,
13109 Local_Suppress_Stack_Top =>
13110 Local_Suppress_Stack_Top,
13111 Version => Ada_Version,
13112 Version_Pragma => Ada_Version_Pragma,
13113 Warnings => Save_Warnings,
13114 SPARK_Mode => SPARK_Mode,
13115 SPARK_Mode_Pragma => SPARK_Mode_Pragma);
13116
13117 -- Package instance
13118
13119 if
13120 Nkind (Node (Decl)) = N_Package_Instantiation
13121 then
13122 Instantiate_Package_Body
13123 (Info, Body_Optional => True);
13124
13125 -- Subprogram instance
13126
13127 else
13128 -- The instance_spec is in the wrapper package,
13129 -- usually followed by its local renaming
13130 -- declaration. See Build_Subprogram_Renaming
13131 -- for details. If the instance carries aspects,
13132 -- these result in the corresponding pragmas,
13133 -- inserted after the subprogram declaration.
13134 -- They must be skipped as well when retrieving
13135 -- the desired spec. A direct link would be
13136 -- more robust ???
13137
13138 declare
13139 Decl : Node_Id :=
13140 (Last (Visible_Declarations
13141 (Specification (Info.Act_Decl))));
13142 begin
13143 while Nkind_In (Decl,
13144 N_Subprogram_Renaming_Declaration, N_Pragma)
13145 loop
13146 Decl := Prev (Decl);
13147 end loop;
13148
13149 Info.Act_Decl := Decl;
13150 end;
13151
13152 Instantiate_Subprogram_Body
13153 (Info, Body_Optional => True);
13154 end if;
13155
13156 Next_Elmt (Decl);
13157 end loop;
13158 end;
13159 end if;
13160
13161 Instantiate_Package_Body
13162 (Body_Info =>
13163 ((Inst_Node => Inst_Node,
13164 Act_Decl => True_Parent,
13165 Expander_Status => Exp_Status,
13166 Current_Sem_Unit => Get_Code_Unit
13167 (Sloc (Inst_Node)),
13168 Scope_Suppress => Scope_Suppress,
13169 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
13170 Version => Ada_Version,
13171 Version_Pragma => Ada_Version_Pragma,
13172 Warnings => Save_Warnings,
13173 SPARK_Mode => SPARK_Mode,
13174 SPARK_Mode_Pragma => SPARK_Mode_Pragma)),
13175 Body_Optional => Body_Optional);
13176 end;
13177 end if;
13178
13179 -- Case where we are not instantiating a nested generic
13180
13181 else
13182 Opt.Style_Check := False;
13183 Expander_Mode_Save_And_Set (True);
13184 Load_Needed_Body (Comp_Unit, OK);
13185 Opt.Style_Check := Saved_Style_Check;
13186 Restore_Warnings (Saved_Warnings);
13187 Expander_Mode_Restore;
13188
13189 if not OK
13190 and then Unit_Requires_Body (Defining_Entity (Spec))
13191 and then not Body_Optional
13192 then
13193 declare
13194 Bname : constant Unit_Name_Type :=
13195 Get_Body_Name (Get_Unit_Name (Unit (Comp_Unit)));
13196
13197 begin
13198 -- In CodePeer mode, the missing body may make the analysis
13199 -- incomplete, but we do not treat it as fatal.
13200
13201 if CodePeer_Mode then
13202 return;
13203
13204 else
13205 Error_Msg_Unit_1 := Bname;
13206 Error_Msg_N ("this instantiation requires$!", N);
13207 Error_Msg_File_1 :=
13208 Get_File_Name (Bname, Subunit => False);
13209 Error_Msg_N ("\but file{ was not found!", N);
13210 raise Unrecoverable_Error;
13211 end if;
13212 end;
13213 end if;
13214 end if;
13215 end if;
13216
13217 -- If loading parent of the generic caused an instantiation circularity,
13218 -- we abandon compilation at this point, because otherwise in some cases
13219 -- we get into trouble with infinite recursions after this point.
13220
13221 if Circularity_Detected then
13222 raise Unrecoverable_Error;
13223 end if;
13224 end Load_Parent_Of_Generic;
13225
13226 ---------------------------------
13227 -- Map_Formal_Package_Entities --
13228 ---------------------------------
13229
13230 procedure Map_Formal_Package_Entities (Form : Entity_Id; Act : Entity_Id) is
13231 E1 : Entity_Id;
13232 E2 : Entity_Id;
13233
13234 begin
13235 Set_Instance_Of (Form, Act);
13236
13237 -- Traverse formal and actual package to map the corresponding entities.
13238 -- We skip over internal entities that may be generated during semantic
13239 -- analysis, and find the matching entities by name, given that they
13240 -- must appear in the same order.
13241
13242 E1 := First_Entity (Form);
13243 E2 := First_Entity (Act);
13244 while Present (E1) and then E1 /= First_Private_Entity (Form) loop
13245 -- Could this test be a single condition??? Seems like it could, and
13246 -- isn't FPE (Form) a constant anyway???
13247
13248 if not Is_Internal (E1)
13249 and then Present (Parent (E1))
13250 and then not Is_Class_Wide_Type (E1)
13251 and then not Is_Internal_Name (Chars (E1))
13252 then
13253 while Present (E2) and then Chars (E2) /= Chars (E1) loop
13254 Next_Entity (E2);
13255 end loop;
13256
13257 if No (E2) then
13258 exit;
13259 else
13260 Set_Instance_Of (E1, E2);
13261
13262 if Is_Type (E1) and then Is_Tagged_Type (E2) then
13263 Set_Instance_Of (Class_Wide_Type (E1), Class_Wide_Type (E2));
13264 end if;
13265
13266 if Is_Constrained (E1) then
13267 Set_Instance_Of (Base_Type (E1), Base_Type (E2));
13268 end if;
13269
13270 if Ekind (E1) = E_Package and then No (Renamed_Object (E1)) then
13271 Map_Formal_Package_Entities (E1, E2);
13272 end if;
13273 end if;
13274 end if;
13275
13276 Next_Entity (E1);
13277 end loop;
13278 end Map_Formal_Package_Entities;
13279
13280 -----------------------
13281 -- Move_Freeze_Nodes --
13282 -----------------------
13283
13284 procedure Move_Freeze_Nodes
13285 (Out_Of : Entity_Id;
13286 After : Node_Id;
13287 L : List_Id)
13288 is
13289 Decl : Node_Id;
13290 Next_Decl : Node_Id;
13291 Next_Node : Node_Id := After;
13292 Spec : Node_Id;
13293
13294 function Is_Outer_Type (T : Entity_Id) return Boolean;
13295 -- Check whether entity is declared in a scope external to that of the
13296 -- generic unit.
13297
13298 -------------------
13299 -- Is_Outer_Type --
13300 -------------------
13301
13302 function Is_Outer_Type (T : Entity_Id) return Boolean is
13303 Scop : Entity_Id := Scope (T);
13304
13305 begin
13306 if Scope_Depth (Scop) < Scope_Depth (Out_Of) then
13307 return True;
13308
13309 else
13310 while Scop /= Standard_Standard loop
13311 if Scop = Out_Of then
13312 return False;
13313 else
13314 Scop := Scope (Scop);
13315 end if;
13316 end loop;
13317
13318 return True;
13319 end if;
13320 end Is_Outer_Type;
13321
13322 -- Start of processing for Move_Freeze_Nodes
13323
13324 begin
13325 if No (L) then
13326 return;
13327 end if;
13328
13329 -- First remove the freeze nodes that may appear before all other
13330 -- declarations.
13331
13332 Decl := First (L);
13333 while Present (Decl)
13334 and then Nkind (Decl) = N_Freeze_Entity
13335 and then Is_Outer_Type (Entity (Decl))
13336 loop
13337 Decl := Remove_Head (L);
13338 Insert_After (Next_Node, Decl);
13339 Set_Analyzed (Decl, False);
13340 Next_Node := Decl;
13341 Decl := First (L);
13342 end loop;
13343
13344 -- Next scan the list of declarations and remove each freeze node that
13345 -- appears ahead of the current node.
13346
13347 while Present (Decl) loop
13348 while Present (Next (Decl))
13349 and then Nkind (Next (Decl)) = N_Freeze_Entity
13350 and then Is_Outer_Type (Entity (Next (Decl)))
13351 loop
13352 Next_Decl := Remove_Next (Decl);
13353 Insert_After (Next_Node, Next_Decl);
13354 Set_Analyzed (Next_Decl, False);
13355 Next_Node := Next_Decl;
13356 end loop;
13357
13358 -- If the declaration is a nested package or concurrent type, then
13359 -- recurse. Nested generic packages will have been processed from the
13360 -- inside out.
13361
13362 case Nkind (Decl) is
13363 when N_Package_Declaration =>
13364 Spec := Specification (Decl);
13365
13366 when N_Task_Type_Declaration =>
13367 Spec := Task_Definition (Decl);
13368
13369 when N_Protected_Type_Declaration =>
13370 Spec := Protected_Definition (Decl);
13371
13372 when others =>
13373 Spec := Empty;
13374 end case;
13375
13376 if Present (Spec) then
13377 Move_Freeze_Nodes (Out_Of, Next_Node, Visible_Declarations (Spec));
13378 Move_Freeze_Nodes (Out_Of, Next_Node, Private_Declarations (Spec));
13379 end if;
13380
13381 Next (Decl);
13382 end loop;
13383 end Move_Freeze_Nodes;
13384
13385 ----------------
13386 -- Next_Assoc --
13387 ----------------
13388
13389 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr is
13390 begin
13391 return Generic_Renamings.Table (E).Next_In_HTable;
13392 end Next_Assoc;
13393
13394 ------------------------
13395 -- Preanalyze_Actuals --
13396 ------------------------
13397
13398 procedure Preanalyze_Actuals (N : Node_Id; Inst : Entity_Id := Empty) is
13399 Assoc : Node_Id;
13400 Act : Node_Id;
13401 Errs : constant Nat := Serious_Errors_Detected;
13402
13403 Cur : Entity_Id := Empty;
13404 -- Current homograph of the instance name
13405
13406 Vis : Boolean;
13407 -- Saved visibility status of the current homograph
13408
13409 begin
13410 Assoc := First (Generic_Associations (N));
13411
13412 -- If the instance is a child unit, its name may hide an outer homonym,
13413 -- so make it invisible to perform name resolution on the actuals.
13414
13415 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name
13416 and then Present
13417 (Current_Entity (Defining_Identifier (Defining_Unit_Name (N))))
13418 then
13419 Cur := Current_Entity (Defining_Identifier (Defining_Unit_Name (N)));
13420
13421 if Is_Compilation_Unit (Cur) then
13422 Vis := Is_Immediately_Visible (Cur);
13423 Set_Is_Immediately_Visible (Cur, False);
13424 else
13425 Cur := Empty;
13426 end if;
13427 end if;
13428
13429 while Present (Assoc) loop
13430 if Nkind (Assoc) /= N_Others_Choice then
13431 Act := Explicit_Generic_Actual_Parameter (Assoc);
13432
13433 -- Within a nested instantiation, a defaulted actual is an empty
13434 -- association, so nothing to analyze. If the subprogram actual
13435 -- is an attribute, analyze prefix only, because actual is not a
13436 -- complete attribute reference.
13437
13438 -- If actual is an allocator, analyze expression only. The full
13439 -- analysis can generate code, and if instance is a compilation
13440 -- unit we have to wait until the package instance is installed
13441 -- to have a proper place to insert this code.
13442
13443 -- String literals may be operators, but at this point we do not
13444 -- know whether the actual is a formal subprogram or a string.
13445
13446 if No (Act) then
13447 null;
13448
13449 elsif Nkind (Act) = N_Attribute_Reference then
13450 Analyze (Prefix (Act));
13451
13452 elsif Nkind (Act) = N_Explicit_Dereference then
13453 Analyze (Prefix (Act));
13454
13455 elsif Nkind (Act) = N_Allocator then
13456 declare
13457 Expr : constant Node_Id := Expression (Act);
13458
13459 begin
13460 if Nkind (Expr) = N_Subtype_Indication then
13461 Analyze (Subtype_Mark (Expr));
13462
13463 -- Analyze separately each discriminant constraint, when
13464 -- given with a named association.
13465
13466 declare
13467 Constr : Node_Id;
13468
13469 begin
13470 Constr := First (Constraints (Constraint (Expr)));
13471 while Present (Constr) loop
13472 if Nkind (Constr) = N_Discriminant_Association then
13473 Analyze (Expression (Constr));
13474 else
13475 Analyze (Constr);
13476 end if;
13477
13478 Next (Constr);
13479 end loop;
13480 end;
13481
13482 else
13483 Analyze (Expr);
13484 end if;
13485 end;
13486
13487 elsif Nkind (Act) /= N_Operator_Symbol then
13488 Analyze (Act);
13489
13490 -- Within a package instance, mark actuals that are limited
13491 -- views, so their use can be moved to the body of the
13492 -- enclosing unit.
13493
13494 if Is_Entity_Name (Act)
13495 and then Is_Type (Entity (Act))
13496 and then From_Limited_With (Entity (Act))
13497 and then Present (Inst)
13498 then
13499 Append_Elmt (Entity (Act), Incomplete_Actuals (Inst));
13500 end if;
13501 end if;
13502
13503 if Errs /= Serious_Errors_Detected then
13504
13505 -- Do a minimal analysis of the generic, to prevent spurious
13506 -- warnings complaining about the generic being unreferenced,
13507 -- before abandoning the instantiation.
13508
13509 Analyze (Name (N));
13510
13511 if Is_Entity_Name (Name (N))
13512 and then Etype (Name (N)) /= Any_Type
13513 then
13514 Generate_Reference (Entity (Name (N)), Name (N));
13515 Set_Is_Instantiated (Entity (Name (N)));
13516 end if;
13517
13518 if Present (Cur) then
13519
13520 -- For the case of a child instance hiding an outer homonym,
13521 -- provide additional warning which might explain the error.
13522
13523 Set_Is_Immediately_Visible (Cur, Vis);
13524 Error_Msg_NE
13525 ("& hides outer unit with the same name??",
13526 N, Defining_Unit_Name (N));
13527 end if;
13528
13529 Abandon_Instantiation (Act);
13530 end if;
13531 end if;
13532
13533 Next (Assoc);
13534 end loop;
13535
13536 if Present (Cur) then
13537 Set_Is_Immediately_Visible (Cur, Vis);
13538 end if;
13539 end Preanalyze_Actuals;
13540
13541 -------------------
13542 -- Remove_Parent --
13543 -------------------
13544
13545 procedure Remove_Parent (In_Body : Boolean := False) is
13546 S : Entity_Id := Current_Scope;
13547 -- S is the scope containing the instantiation just completed. The scope
13548 -- stack contains the parent instances of the instantiation, followed by
13549 -- the original S.
13550
13551 Cur_P : Entity_Id;
13552 E : Entity_Id;
13553 P : Entity_Id;
13554 Hidden : Elmt_Id;
13555
13556 begin
13557 -- After child instantiation is complete, remove from scope stack the
13558 -- extra copy of the current scope, and then remove parent instances.
13559
13560 if not In_Body then
13561 Pop_Scope;
13562
13563 while Current_Scope /= S loop
13564 P := Current_Scope;
13565 End_Package_Scope (Current_Scope);
13566
13567 if In_Open_Scopes (P) then
13568 E := First_Entity (P);
13569 while Present (E) loop
13570 Set_Is_Immediately_Visible (E, True);
13571 Next_Entity (E);
13572 end loop;
13573
13574 -- If instantiation is declared in a block, it is the enclosing
13575 -- scope that might be a parent instance. Note that only one
13576 -- block can be involved, because the parent instances have
13577 -- been installed within it.
13578
13579 if Ekind (P) = E_Block then
13580 Cur_P := Scope (P);
13581 else
13582 Cur_P := P;
13583 end if;
13584
13585 if Is_Generic_Instance (Cur_P) and then P /= Current_Scope then
13586 -- We are within an instance of some sibling. Retain
13587 -- visibility of parent, for proper subsequent cleanup, and
13588 -- reinstall private declarations as well.
13589
13590 Set_In_Private_Part (P);
13591 Install_Private_Declarations (P);
13592 end if;
13593
13594 -- If the ultimate parent is a top-level unit recorded in
13595 -- Instance_Parent_Unit, then reset its visibility to what it was
13596 -- before instantiation. (It's not clear what the purpose is of
13597 -- testing whether Scope (P) is In_Open_Scopes, but that test was
13598 -- present before the ultimate parent test was added.???)
13599
13600 elsif not In_Open_Scopes (Scope (P))
13601 or else (P = Instance_Parent_Unit
13602 and then not Parent_Unit_Visible)
13603 then
13604 Set_Is_Immediately_Visible (P, False);
13605
13606 -- If the current scope is itself an instantiation of a generic
13607 -- nested within P, and we are in the private part of body of this
13608 -- instantiation, restore the full views of P, that were removed
13609 -- in End_Package_Scope above. This obscure case can occur when a
13610 -- subunit of a generic contains an instance of a child unit of
13611 -- its generic parent unit.
13612
13613 elsif S = Current_Scope and then Is_Generic_Instance (S) then
13614 declare
13615 Par : constant Entity_Id :=
13616 Generic_Parent (Package_Specification (S));
13617 begin
13618 if Present (Par)
13619 and then P = Scope (Par)
13620 and then (In_Package_Body (S) or else In_Private_Part (S))
13621 then
13622 Set_In_Private_Part (P);
13623 Install_Private_Declarations (P);
13624 end if;
13625 end;
13626 end if;
13627 end loop;
13628
13629 -- Reset visibility of entities in the enclosing scope
13630
13631 Set_Is_Hidden_Open_Scope (Current_Scope, False);
13632
13633 Hidden := First_Elmt (Hidden_Entities);
13634 while Present (Hidden) loop
13635 Set_Is_Immediately_Visible (Node (Hidden), True);
13636 Next_Elmt (Hidden);
13637 end loop;
13638
13639 else
13640 -- Each body is analyzed separately, and there is no context that
13641 -- needs preserving from one body instance to the next, so remove all
13642 -- parent scopes that have been installed.
13643
13644 while Present (S) loop
13645 End_Package_Scope (S);
13646 Set_Is_Immediately_Visible (S, False);
13647 S := Current_Scope;
13648 exit when S = Standard_Standard;
13649 end loop;
13650 end if;
13651 end Remove_Parent;
13652
13653 -----------------
13654 -- Restore_Env --
13655 -----------------
13656
13657 procedure Restore_Env is
13658 Saved : Instance_Env renames Instance_Envs.Table (Instance_Envs.Last);
13659
13660 begin
13661 if No (Current_Instantiated_Parent.Act_Id) then
13662 -- Restore environment after subprogram inlining
13663
13664 Restore_Private_Views (Empty);
13665 end if;
13666
13667 Current_Instantiated_Parent := Saved.Instantiated_Parent;
13668 Exchanged_Views := Saved.Exchanged_Views;
13669 Hidden_Entities := Saved.Hidden_Entities;
13670 Current_Sem_Unit := Saved.Current_Sem_Unit;
13671 Parent_Unit_Visible := Saved.Parent_Unit_Visible;
13672 Instance_Parent_Unit := Saved.Instance_Parent_Unit;
13673
13674 Restore_Opt_Config_Switches (Saved.Switches);
13675
13676 Instance_Envs.Decrement_Last;
13677 end Restore_Env;
13678
13679 ---------------------------
13680 -- Restore_Private_Views --
13681 ---------------------------
13682
13683 procedure Restore_Private_Views
13684 (Pack_Id : Entity_Id;
13685 Is_Package : Boolean := True)
13686 is
13687 M : Elmt_Id;
13688 E : Entity_Id;
13689 Typ : Entity_Id;
13690 Dep_Elmt : Elmt_Id;
13691 Dep_Typ : Node_Id;
13692
13693 procedure Restore_Nested_Formal (Formal : Entity_Id);
13694 -- Hide the generic formals of formal packages declared with box which
13695 -- were reachable in the current instantiation.
13696
13697 ---------------------------
13698 -- Restore_Nested_Formal --
13699 ---------------------------
13700
13701 procedure Restore_Nested_Formal (Formal : Entity_Id) is
13702 Ent : Entity_Id;
13703
13704 begin
13705 if Present (Renamed_Object (Formal))
13706 and then Denotes_Formal_Package (Renamed_Object (Formal), True)
13707 then
13708 return;
13709
13710 elsif Present (Associated_Formal_Package (Formal)) then
13711 Ent := First_Entity (Formal);
13712 while Present (Ent) loop
13713 exit when Ekind (Ent) = E_Package
13714 and then Renamed_Entity (Ent) = Renamed_Entity (Formal);
13715
13716 Set_Is_Hidden (Ent);
13717 Set_Is_Potentially_Use_Visible (Ent, False);
13718
13719 -- If package, then recurse
13720
13721 if Ekind (Ent) = E_Package then
13722 Restore_Nested_Formal (Ent);
13723 end if;
13724
13725 Next_Entity (Ent);
13726 end loop;
13727 end if;
13728 end Restore_Nested_Formal;
13729
13730 -- Start of processing for Restore_Private_Views
13731
13732 begin
13733 M := First_Elmt (Exchanged_Views);
13734 while Present (M) loop
13735 Typ := Node (M);
13736
13737 -- Subtypes of types whose views have been exchanged, and that are
13738 -- defined within the instance, were not on the Private_Dependents
13739 -- list on entry to the instance, so they have to be exchanged
13740 -- explicitly now, in order to remain consistent with the view of the
13741 -- parent type.
13742
13743 if Ekind_In (Typ, E_Private_Type,
13744 E_Limited_Private_Type,
13745 E_Record_Type_With_Private)
13746 then
13747 Dep_Elmt := First_Elmt (Private_Dependents (Typ));
13748 while Present (Dep_Elmt) loop
13749 Dep_Typ := Node (Dep_Elmt);
13750
13751 if Scope (Dep_Typ) = Pack_Id
13752 and then Present (Full_View (Dep_Typ))
13753 then
13754 Replace_Elmt (Dep_Elmt, Full_View (Dep_Typ));
13755 Exchange_Declarations (Dep_Typ);
13756 end if;
13757
13758 Next_Elmt (Dep_Elmt);
13759 end loop;
13760 end if;
13761
13762 Exchange_Declarations (Node (M));
13763 Next_Elmt (M);
13764 end loop;
13765
13766 if No (Pack_Id) then
13767 return;
13768 end if;
13769
13770 -- Make the generic formal parameters private, and make the formal types
13771 -- into subtypes of the actuals again.
13772
13773 E := First_Entity (Pack_Id);
13774 while Present (E) loop
13775 Set_Is_Hidden (E, True);
13776
13777 if Is_Type (E)
13778 and then Nkind (Parent (E)) = N_Subtype_Declaration
13779 then
13780 -- If the actual for E is itself a generic actual type from
13781 -- an enclosing instance, E is still a generic actual type
13782 -- outside of the current instance. This matter when resolving
13783 -- an overloaded call that may be ambiguous in the enclosing
13784 -- instance, when two of its actuals coincide.
13785
13786 if Is_Entity_Name (Subtype_Indication (Parent (E)))
13787 and then Is_Generic_Actual_Type
13788 (Entity (Subtype_Indication (Parent (E))))
13789 then
13790 null;
13791 else
13792 Set_Is_Generic_Actual_Type (E, False);
13793 end if;
13794
13795 -- An unusual case of aliasing: the actual may also be directly
13796 -- visible in the generic, and be private there, while it is fully
13797 -- visible in the context of the instance. The internal subtype
13798 -- is private in the instance but has full visibility like its
13799 -- parent in the enclosing scope. This enforces the invariant that
13800 -- the privacy status of all private dependents of a type coincide
13801 -- with that of the parent type. This can only happen when a
13802 -- generic child unit is instantiated within a sibling.
13803
13804 if Is_Private_Type (E)
13805 and then not Is_Private_Type (Etype (E))
13806 then
13807 Exchange_Declarations (E);
13808 end if;
13809
13810 elsif Ekind (E) = E_Package then
13811
13812 -- The end of the renaming list is the renaming of the generic
13813 -- package itself. If the instance is a subprogram, all entities
13814 -- in the corresponding package are renamings. If this entity is
13815 -- a formal package, make its own formals private as well. The
13816 -- actual in this case is itself the renaming of an instantiation.
13817 -- If the entity is not a package renaming, it is the entity
13818 -- created to validate formal package actuals: ignore it.
13819
13820 -- If the actual is itself a formal package for the enclosing
13821 -- generic, or the actual for such a formal package, it remains
13822 -- visible on exit from the instance, and therefore nothing needs
13823 -- to be done either, except to keep it accessible.
13824
13825 if Is_Package and then Renamed_Object (E) = Pack_Id then
13826 exit;
13827
13828 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
13829 null;
13830
13831 elsif
13832 Denotes_Formal_Package (Renamed_Object (E), True, Pack_Id)
13833 then
13834 Set_Is_Hidden (E, False);
13835
13836 else
13837 declare
13838 Act_P : constant Entity_Id := Renamed_Object (E);
13839 Id : Entity_Id;
13840
13841 begin
13842 Id := First_Entity (Act_P);
13843 while Present (Id)
13844 and then Id /= First_Private_Entity (Act_P)
13845 loop
13846 exit when Ekind (Id) = E_Package
13847 and then Renamed_Object (Id) = Act_P;
13848
13849 Set_Is_Hidden (Id, True);
13850 Set_Is_Potentially_Use_Visible (Id, In_Use (Act_P));
13851
13852 if Ekind (Id) = E_Package then
13853 Restore_Nested_Formal (Id);
13854 end if;
13855
13856 Next_Entity (Id);
13857 end loop;
13858 end;
13859 end if;
13860 end if;
13861
13862 Next_Entity (E);
13863 end loop;
13864 end Restore_Private_Views;
13865
13866 --------------
13867 -- Save_Env --
13868 --------------
13869
13870 procedure Save_Env
13871 (Gen_Unit : Entity_Id;
13872 Act_Unit : Entity_Id)
13873 is
13874 begin
13875 Init_Env;
13876 Set_Instance_Env (Gen_Unit, Act_Unit);
13877 end Save_Env;
13878
13879 ----------------------------
13880 -- Save_Global_References --
13881 ----------------------------
13882
13883 procedure Save_Global_References (Templ : Node_Id) is
13884
13885 -- ??? it is horrible to use global variables in highly recursive code
13886
13887 E : Entity_Id;
13888 -- The entity of the current associated node
13889
13890 Gen_Scope : Entity_Id;
13891 -- The scope of the generic for which references are being saved
13892
13893 N2 : Node_Id;
13894 -- The current associated node
13895
13896 function Is_Global (E : Entity_Id) return Boolean;
13897 -- Check whether entity is defined outside of generic unit. Examine the
13898 -- scope of an entity, and the scope of the scope, etc, until we find
13899 -- either Standard, in which case the entity is global, or the generic
13900 -- unit itself, which indicates that the entity is local. If the entity
13901 -- is the generic unit itself, as in the case of a recursive call, or
13902 -- the enclosing generic unit, if different from the current scope, then
13903 -- it is local as well, because it will be replaced at the point of
13904 -- instantiation. On the other hand, if it is a reference to a child
13905 -- unit of a common ancestor, which appears in an instantiation, it is
13906 -- global because it is used to denote a specific compilation unit at
13907 -- the time the instantiations will be analyzed.
13908
13909 procedure Qualify_Universal_Operands
13910 (Op : Node_Id;
13911 Func_Call : Node_Id);
13912 -- Op denotes a binary or unary operator in generic template Templ. Node
13913 -- Func_Call is the function call alternative of the operator within the
13914 -- the analyzed copy of the template. Change each operand which yields a
13915 -- universal type by wrapping it into a qualified expression
13916 --
13917 -- Actual_Typ'(Operand)
13918 --
13919 -- where Actual_Typ is the type of corresponding actual parameter of
13920 -- Operand in Func_Call.
13921
13922 procedure Reset_Entity (N : Node_Id);
13923 -- Save semantic information on global entity so that it is not resolved
13924 -- again at instantiation time.
13925
13926 procedure Save_Entity_Descendants (N : Node_Id);
13927 -- Apply Save_Global_References to the two syntactic descendants of
13928 -- non-terminal nodes that carry an Associated_Node and are processed
13929 -- through Reset_Entity. Once the global entity (if any) has been
13930 -- captured together with its type, only two syntactic descendants need
13931 -- to be traversed to complete the processing of the tree rooted at N.
13932 -- This applies to Selected_Components, Expanded_Names, and to Operator
13933 -- nodes. N can also be a character literal, identifier, or operator
13934 -- symbol node, but the call has no effect in these cases.
13935
13936 procedure Save_Global_Defaults (N1 : Node_Id; N2 : Node_Id);
13937 -- Default actuals in nested instances must be handled specially
13938 -- because there is no link to them from the original tree. When an
13939 -- actual subprogram is given by a default, we add an explicit generic
13940 -- association for it in the instantiation node. When we save the
13941 -- global references on the name of the instance, we recover the list
13942 -- of generic associations, and add an explicit one to the original
13943 -- generic tree, through which a global actual can be preserved.
13944 -- Similarly, if a child unit is instantiated within a sibling, in the
13945 -- context of the parent, we must preserve the identifier of the parent
13946 -- so that it can be properly resolved in a subsequent instantiation.
13947
13948 procedure Save_Global_Descendant (D : Union_Id);
13949 -- Apply Save_References recursively to the descendants of node D
13950
13951 procedure Save_References (N : Node_Id);
13952 -- This is the recursive procedure that does the work, once the
13953 -- enclosing generic scope has been established.
13954
13955 ---------------
13956 -- Is_Global --
13957 ---------------
13958
13959 function Is_Global (E : Entity_Id) return Boolean is
13960 Se : Entity_Id;
13961
13962 function Is_Instance_Node (Decl : Node_Id) return Boolean;
13963 -- Determine whether the parent node of a reference to a child unit
13964 -- denotes an instantiation or a formal package, in which case the
13965 -- reference to the child unit is global, even if it appears within
13966 -- the current scope (e.g. when the instance appears within the body
13967 -- of an ancestor).
13968
13969 ----------------------
13970 -- Is_Instance_Node --
13971 ----------------------
13972
13973 function Is_Instance_Node (Decl : Node_Id) return Boolean is
13974 begin
13975 return Nkind (Decl) in N_Generic_Instantiation
13976 or else
13977 Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration;
13978 end Is_Instance_Node;
13979
13980 -- Start of processing for Is_Global
13981
13982 begin
13983 if E = Gen_Scope then
13984 return False;
13985
13986 elsif E = Standard_Standard then
13987 return True;
13988
13989 elsif Is_Child_Unit (E)
13990 and then (Is_Instance_Node (Parent (N2))
13991 or else (Nkind (Parent (N2)) = N_Expanded_Name
13992 and then N2 = Selector_Name (Parent (N2))
13993 and then
13994 Is_Instance_Node (Parent (Parent (N2)))))
13995 then
13996 return True;
13997
13998 else
13999 Se := Scope (E);
14000 while Se /= Gen_Scope loop
14001 if Se = Standard_Standard then
14002 return True;
14003 else
14004 Se := Scope (Se);
14005 end if;
14006 end loop;
14007
14008 return False;
14009 end if;
14010 end Is_Global;
14011
14012 --------------------------------
14013 -- Qualify_Universal_Operands --
14014 --------------------------------
14015
14016 procedure Qualify_Universal_Operands
14017 (Op : Node_Id;
14018 Func_Call : Node_Id)
14019 is
14020 procedure Qualify_Operand (Opnd : Node_Id; Actual : Node_Id);
14021 -- Rewrite operand Opnd as a qualified expression of the form
14022 --
14023 -- Actual_Typ'(Opnd)
14024 --
14025 -- where Actual is the corresponding actual parameter of Opnd in
14026 -- function call Func_Call.
14027
14028 function Qualify_Type
14029 (Loc : Source_Ptr;
14030 Typ : Entity_Id) return Node_Id;
14031 -- Qualify type Typ by creating a selected component of the form
14032 --
14033 -- Scope_Of_Typ.Typ
14034
14035 ---------------------
14036 -- Qualify_Operand --
14037 ---------------------
14038
14039 procedure Qualify_Operand (Opnd : Node_Id; Actual : Node_Id) is
14040 Loc : constant Source_Ptr := Sloc (Opnd);
14041 Typ : constant Entity_Id := Etype (Actual);
14042 Mark : Node_Id;
14043 Qual : Node_Id;
14044
14045 begin
14046 -- Qualify the operand when it is of a universal type. Note that
14047 -- the template is unanalyzed and it is not possible to directly
14048 -- query the type. This transformation is not done when the type
14049 -- of the actual is internally generated because the type will be
14050 -- regenerated in the instance.
14051
14052 if Yields_Universal_Type (Opnd)
14053 and then Comes_From_Source (Typ)
14054 and then not Is_Hidden (Typ)
14055 then
14056 -- The type of the actual may be a global reference. Save this
14057 -- information by creating a reference to it.
14058
14059 if Is_Global (Typ) then
14060 Mark := New_Occurrence_Of (Typ, Loc);
14061
14062 -- Otherwise rely on resolution to find the proper type within
14063 -- the instance.
14064
14065 else
14066 Mark := Qualify_Type (Loc, Typ);
14067 end if;
14068
14069 Qual :=
14070 Make_Qualified_Expression (Loc,
14071 Subtype_Mark => Mark,
14072 Expression => Relocate_Node (Opnd));
14073
14074 -- Mark the qualification to distinguish it from other source
14075 -- constructs and signal the instantiation mechanism that this
14076 -- node requires special processing. See Copy_Generic_Node for
14077 -- details.
14078
14079 Set_Is_Qualified_Universal_Literal (Qual);
14080
14081 Rewrite (Opnd, Qual);
14082 end if;
14083 end Qualify_Operand;
14084
14085 ------------------
14086 -- Qualify_Type --
14087 ------------------
14088
14089 function Qualify_Type
14090 (Loc : Source_Ptr;
14091 Typ : Entity_Id) return Node_Id
14092 is
14093 Scop : constant Entity_Id := Scope (Typ);
14094 Result : Node_Id;
14095
14096 begin
14097 Result := Make_Identifier (Loc, Chars (Typ));
14098
14099 if Present (Scop) and then not Is_Generic_Unit (Scop) then
14100 Result :=
14101 Make_Selected_Component (Loc,
14102 Prefix => Make_Identifier (Loc, Chars (Scop)),
14103 Selector_Name => Result);
14104 end if;
14105
14106 return Result;
14107 end Qualify_Type;
14108
14109 -- Local variables
14110
14111 Actuals : constant List_Id := Parameter_Associations (Func_Call);
14112
14113 -- Start of processing for Qualify_Universal_Operands
14114
14115 begin
14116 if Nkind (Op) in N_Binary_Op then
14117 Qualify_Operand (Left_Opnd (Op), First (Actuals));
14118 Qualify_Operand (Right_Opnd (Op), Next (First (Actuals)));
14119
14120 elsif Nkind (Op) in N_Unary_Op then
14121 Qualify_Operand (Right_Opnd (Op), First (Actuals));
14122 end if;
14123 end Qualify_Universal_Operands;
14124
14125 ------------------
14126 -- Reset_Entity --
14127 ------------------
14128
14129 procedure Reset_Entity (N : Node_Id) is
14130 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id);
14131 -- If the type of N2 is global to the generic unit, save the type in
14132 -- the generic node. Just as we perform name capture for explicit
14133 -- references within the generic, we must capture the global types
14134 -- of local entities because they may participate in resolution in
14135 -- the instance.
14136
14137 function Top_Ancestor (E : Entity_Id) return Entity_Id;
14138 -- Find the ultimate ancestor of the current unit. If it is not a
14139 -- generic unit, then the name of the current unit in the prefix of
14140 -- an expanded name must be replaced with its generic homonym to
14141 -- ensure that it will be properly resolved in an instance.
14142
14143 ---------------------
14144 -- Set_Global_Type --
14145 ---------------------
14146
14147 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id) is
14148 Typ : constant Entity_Id := Etype (N2);
14149
14150 begin
14151 Set_Etype (N, Typ);
14152
14153 -- If the entity of N is not the associated node, this is a
14154 -- nested generic and it has an associated node as well, whose
14155 -- type is already the full view (see below). Indicate that the
14156 -- original node has a private view.
14157
14158 if Entity (N) /= N2 and then Has_Private_View (Entity (N)) then
14159 Set_Has_Private_View (N);
14160 end if;
14161
14162 -- If not a private type, nothing else to do
14163
14164 if not Is_Private_Type (Typ) then
14165 if Is_Array_Type (Typ)
14166 and then Is_Private_Type (Component_Type (Typ))
14167 then
14168 Set_Has_Private_View (N);
14169 end if;
14170
14171 -- If it is a derivation of a private type in a context where no
14172 -- full view is needed, nothing to do either.
14173
14174 elsif No (Full_View (Typ)) and then Typ /= Etype (Typ) then
14175 null;
14176
14177 -- Otherwise mark the type for flipping and use the full view when
14178 -- available.
14179
14180 else
14181 Set_Has_Private_View (N);
14182
14183 if Present (Full_View (Typ)) then
14184 Set_Etype (N2, Full_View (Typ));
14185 end if;
14186 end if;
14187
14188 if Is_Floating_Point_Type (Typ)
14189 and then Has_Dimension_System (Typ)
14190 then
14191 Copy_Dimensions (N2, N);
14192 end if;
14193
14194 end Set_Global_Type;
14195
14196 ------------------
14197 -- Top_Ancestor --
14198 ------------------
14199
14200 function Top_Ancestor (E : Entity_Id) return Entity_Id is
14201 Par : Entity_Id;
14202
14203 begin
14204 Par := E;
14205 while Is_Child_Unit (Par) loop
14206 Par := Scope (Par);
14207 end loop;
14208
14209 return Par;
14210 end Top_Ancestor;
14211
14212 -- Start of processing for Reset_Entity
14213
14214 begin
14215 N2 := Get_Associated_Node (N);
14216 E := Entity (N2);
14217
14218 if Present (E) then
14219
14220 -- If the node is an entry call to an entry in an enclosing task,
14221 -- it is rewritten as a selected component. No global entity to
14222 -- preserve in this case, since the expansion will be redone in
14223 -- the instance.
14224
14225 if not Nkind_In (E, N_Defining_Character_Literal,
14226 N_Defining_Identifier,
14227 N_Defining_Operator_Symbol)
14228 then
14229 Set_Associated_Node (N, Empty);
14230 Set_Etype (N, Empty);
14231 return;
14232 end if;
14233
14234 -- If the entity is an itype created as a subtype of an access
14235 -- type with a null exclusion restore source entity for proper
14236 -- visibility. The itype will be created anew in the instance.
14237
14238 if Is_Itype (E)
14239 and then Ekind (E) = E_Access_Subtype
14240 and then Is_Entity_Name (N)
14241 and then Chars (Etype (E)) = Chars (N)
14242 then
14243 E := Etype (E);
14244 Set_Entity (N2, E);
14245 Set_Etype (N2, E);
14246 end if;
14247
14248 if Is_Global (E) then
14249
14250 -- If the entity is a package renaming that is the prefix of
14251 -- an expanded name, it has been rewritten as the renamed
14252 -- package, which is necessary semantically but complicates
14253 -- ASIS tree traversal, so we recover the original entity to
14254 -- expose the renaming. Take into account that the context may
14255 -- be a nested generic, that the original node may itself have
14256 -- an associated node that had better be an entity, and that
14257 -- the current node is still a selected component.
14258
14259 if Ekind (E) = E_Package
14260 and then Nkind (N) = N_Selected_Component
14261 and then Nkind (Parent (N)) = N_Expanded_Name
14262 and then Present (Original_Node (N2))
14263 and then Is_Entity_Name (Original_Node (N2))
14264 and then Present (Entity (Original_Node (N2)))
14265 then
14266 if Is_Global (Entity (Original_Node (N2))) then
14267 N2 := Original_Node (N2);
14268 Set_Associated_Node (N, N2);
14269 Set_Global_Type (N, N2);
14270
14271 -- Renaming is local, and will be resolved in instance
14272
14273 else
14274 Set_Associated_Node (N, Empty);
14275 Set_Etype (N, Empty);
14276 end if;
14277
14278 else
14279 Set_Global_Type (N, N2);
14280 end if;
14281
14282 elsif Nkind (N) = N_Op_Concat
14283 and then Is_Generic_Type (Etype (N2))
14284 and then (Base_Type (Etype (Right_Opnd (N2))) = Etype (N2)
14285 or else
14286 Base_Type (Etype (Left_Opnd (N2))) = Etype (N2))
14287 and then Is_Intrinsic_Subprogram (E)
14288 then
14289 null;
14290
14291 -- Entity is local. Mark generic node as unresolved. Note that now
14292 -- it does not have an entity.
14293
14294 else
14295 Set_Associated_Node (N, Empty);
14296 Set_Etype (N, Empty);
14297 end if;
14298
14299 if Nkind (Parent (N)) in N_Generic_Instantiation
14300 and then N = Name (Parent (N))
14301 then
14302 Save_Global_Defaults (Parent (N), Parent (N2));
14303 end if;
14304
14305 elsif Nkind (Parent (N)) = N_Selected_Component
14306 and then Nkind (Parent (N2)) = N_Expanded_Name
14307 then
14308 if Is_Global (Entity (Parent (N2))) then
14309 Change_Selected_Component_To_Expanded_Name (Parent (N));
14310 Set_Associated_Node (Parent (N), Parent (N2));
14311 Set_Global_Type (Parent (N), Parent (N2));
14312 Save_Entity_Descendants (N);
14313
14314 -- If this is a reference to the current generic entity, replace
14315 -- by the name of the generic homonym of the current package. This
14316 -- is because in an instantiation Par.P.Q will not resolve to the
14317 -- name of the instance, whose enclosing scope is not necessarily
14318 -- Par. We use the generic homonym rather that the name of the
14319 -- generic itself because it may be hidden by a local declaration.
14320
14321 elsif In_Open_Scopes (Entity (Parent (N2)))
14322 and then not
14323 Is_Generic_Unit (Top_Ancestor (Entity (Prefix (Parent (N2)))))
14324 then
14325 if Ekind (Entity (Parent (N2))) = E_Generic_Package then
14326 Rewrite (Parent (N),
14327 Make_Identifier (Sloc (N),
14328 Chars =>
14329 Chars (Generic_Homonym (Entity (Parent (N2))))));
14330 else
14331 Rewrite (Parent (N),
14332 Make_Identifier (Sloc (N),
14333 Chars => Chars (Selector_Name (Parent (N2)))));
14334 end if;
14335 end if;
14336
14337 if Nkind (Parent (Parent (N))) in N_Generic_Instantiation
14338 and then Parent (N) = Name (Parent (Parent (N)))
14339 then
14340 Save_Global_Defaults
14341 (Parent (Parent (N)), Parent (Parent (N2)));
14342 end if;
14343
14344 -- A selected component may denote a static constant that has been
14345 -- folded. If the static constant is global to the generic, capture
14346 -- its value. Otherwise the folding will happen in any instantiation.
14347
14348 elsif Nkind (Parent (N)) = N_Selected_Component
14349 and then Nkind_In (Parent (N2), N_Integer_Literal, N_Real_Literal)
14350 then
14351 if Present (Entity (Original_Node (Parent (N2))))
14352 and then Is_Global (Entity (Original_Node (Parent (N2))))
14353 then
14354 Rewrite (Parent (N), New_Copy (Parent (N2)));
14355 Set_Analyzed (Parent (N), False);
14356 end if;
14357
14358 -- A selected component may be transformed into a parameterless
14359 -- function call. If the called entity is global, rewrite the node
14360 -- appropriately, i.e. as an extended name for the global entity.
14361
14362 elsif Nkind (Parent (N)) = N_Selected_Component
14363 and then Nkind (Parent (N2)) = N_Function_Call
14364 and then N = Selector_Name (Parent (N))
14365 then
14366 if No (Parameter_Associations (Parent (N2))) then
14367 if Is_Global (Entity (Name (Parent (N2)))) then
14368 Change_Selected_Component_To_Expanded_Name (Parent (N));
14369 Set_Associated_Node (Parent (N), Name (Parent (N2)));
14370 Set_Global_Type (Parent (N), Name (Parent (N2)));
14371 Save_Entity_Descendants (N);
14372
14373 else
14374 Set_Is_Prefixed_Call (Parent (N));
14375 Set_Associated_Node (N, Empty);
14376 Set_Etype (N, Empty);
14377 end if;
14378
14379 -- In Ada 2005, X.F may be a call to a primitive operation,
14380 -- rewritten as F (X). This rewriting will be done again in an
14381 -- instance, so keep the original node. Global entities will be
14382 -- captured as for other constructs. Indicate that this must
14383 -- resolve as a call, to prevent accidental overloading in the
14384 -- instance, if both a component and a primitive operation appear
14385 -- as candidates.
14386
14387 else
14388 Set_Is_Prefixed_Call (Parent (N));
14389 end if;
14390
14391 -- Entity is local. Reset in generic unit, so that node is resolved
14392 -- anew at the point of instantiation.
14393
14394 else
14395 Set_Associated_Node (N, Empty);
14396 Set_Etype (N, Empty);
14397 end if;
14398 end Reset_Entity;
14399
14400 -----------------------------
14401 -- Save_Entity_Descendants --
14402 -----------------------------
14403
14404 procedure Save_Entity_Descendants (N : Node_Id) is
14405 begin
14406 case Nkind (N) is
14407 when N_Binary_Op =>
14408 Save_Global_Descendant (Union_Id (Left_Opnd (N)));
14409 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
14410
14411 when N_Unary_Op =>
14412 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
14413
14414 when N_Expanded_Name |
14415 N_Selected_Component =>
14416 Save_Global_Descendant (Union_Id (Prefix (N)));
14417 Save_Global_Descendant (Union_Id (Selector_Name (N)));
14418
14419 when N_Identifier |
14420 N_Character_Literal |
14421 N_Operator_Symbol =>
14422 null;
14423
14424 when others =>
14425 raise Program_Error;
14426 end case;
14427 end Save_Entity_Descendants;
14428
14429 --------------------------
14430 -- Save_Global_Defaults --
14431 --------------------------
14432
14433 procedure Save_Global_Defaults (N1 : Node_Id; N2 : Node_Id) is
14434 Loc : constant Source_Ptr := Sloc (N1);
14435 Assoc2 : constant List_Id := Generic_Associations (N2);
14436 Gen_Id : constant Entity_Id := Get_Generic_Entity (N2);
14437 Assoc1 : List_Id;
14438 Act1 : Node_Id;
14439 Act2 : Node_Id;
14440 Def : Node_Id;
14441 Ndec : Node_Id;
14442 Subp : Entity_Id;
14443 Actual : Entity_Id;
14444
14445 begin
14446 Assoc1 := Generic_Associations (N1);
14447
14448 if Present (Assoc1) then
14449 Act1 := First (Assoc1);
14450 else
14451 Act1 := Empty;
14452 Set_Generic_Associations (N1, New_List);
14453 Assoc1 := Generic_Associations (N1);
14454 end if;
14455
14456 if Present (Assoc2) then
14457 Act2 := First (Assoc2);
14458 else
14459 return;
14460 end if;
14461
14462 while Present (Act1) and then Present (Act2) loop
14463 Next (Act1);
14464 Next (Act2);
14465 end loop;
14466
14467 -- Find the associations added for default subprograms
14468
14469 if Present (Act2) then
14470 while Nkind (Act2) /= N_Generic_Association
14471 or else No (Entity (Selector_Name (Act2)))
14472 or else not Is_Overloadable (Entity (Selector_Name (Act2)))
14473 loop
14474 Next (Act2);
14475 end loop;
14476
14477 -- Add a similar association if the default is global. The
14478 -- renaming declaration for the actual has been analyzed, and
14479 -- its alias is the program it renames. Link the actual in the
14480 -- original generic tree with the node in the analyzed tree.
14481
14482 while Present (Act2) loop
14483 Subp := Entity (Selector_Name (Act2));
14484 Def := Explicit_Generic_Actual_Parameter (Act2);
14485
14486 -- Following test is defence against rubbish errors
14487
14488 if No (Alias (Subp)) then
14489 return;
14490 end if;
14491
14492 -- Retrieve the resolved actual from the renaming declaration
14493 -- created for the instantiated formal.
14494
14495 Actual := Entity (Name (Parent (Parent (Subp))));
14496 Set_Entity (Def, Actual);
14497 Set_Etype (Def, Etype (Actual));
14498
14499 if Is_Global (Actual) then
14500 Ndec :=
14501 Make_Generic_Association (Loc,
14502 Selector_Name =>
14503 New_Occurrence_Of (Subp, Loc),
14504 Explicit_Generic_Actual_Parameter =>
14505 New_Occurrence_Of (Actual, Loc));
14506
14507 Set_Associated_Node
14508 (Explicit_Generic_Actual_Parameter (Ndec), Def);
14509
14510 Append (Ndec, Assoc1);
14511
14512 -- If there are other defaults, add a dummy association in case
14513 -- there are other defaulted formals with the same name.
14514
14515 elsif Present (Next (Act2)) then
14516 Ndec :=
14517 Make_Generic_Association (Loc,
14518 Selector_Name =>
14519 New_Occurrence_Of (Subp, Loc),
14520 Explicit_Generic_Actual_Parameter => Empty);
14521
14522 Append (Ndec, Assoc1);
14523 end if;
14524
14525 Next (Act2);
14526 end loop;
14527 end if;
14528
14529 if Nkind (Name (N1)) = N_Identifier
14530 and then Is_Child_Unit (Gen_Id)
14531 and then Is_Global (Gen_Id)
14532 and then Is_Generic_Unit (Scope (Gen_Id))
14533 and then In_Open_Scopes (Scope (Gen_Id))
14534 then
14535 -- This is an instantiation of a child unit within a sibling, so
14536 -- that the generic parent is in scope. An eventual instance must
14537 -- occur within the scope of an instance of the parent. Make name
14538 -- in instance into an expanded name, to preserve the identifier
14539 -- of the parent, so it can be resolved subsequently.
14540
14541 Rewrite (Name (N2),
14542 Make_Expanded_Name (Loc,
14543 Chars => Chars (Gen_Id),
14544 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
14545 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
14546 Set_Entity (Name (N2), Gen_Id);
14547
14548 Rewrite (Name (N1),
14549 Make_Expanded_Name (Loc,
14550 Chars => Chars (Gen_Id),
14551 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
14552 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
14553
14554 Set_Associated_Node (Name (N1), Name (N2));
14555 Set_Associated_Node (Prefix (Name (N1)), Empty);
14556 Set_Associated_Node
14557 (Selector_Name (Name (N1)), Selector_Name (Name (N2)));
14558 Set_Etype (Name (N1), Etype (Gen_Id));
14559 end if;
14560 end Save_Global_Defaults;
14561
14562 ----------------------------
14563 -- Save_Global_Descendant --
14564 ----------------------------
14565
14566 procedure Save_Global_Descendant (D : Union_Id) is
14567 N1 : Node_Id;
14568
14569 begin
14570 if D in Node_Range then
14571 if D = Union_Id (Empty) then
14572 null;
14573
14574 elsif Nkind (Node_Id (D)) /= N_Compilation_Unit then
14575 Save_References (Node_Id (D));
14576 end if;
14577
14578 elsif D in List_Range then
14579 pragma Assert (D /= Union_Id (No_List));
14580 -- Because No_List = Empty, which is in Node_Range above
14581
14582 if Is_Empty_List (List_Id (D)) then
14583 null;
14584
14585 else
14586 N1 := First (List_Id (D));
14587 while Present (N1) loop
14588 Save_References (N1);
14589 Next (N1);
14590 end loop;
14591 end if;
14592
14593 -- Element list or other non-node field, nothing to do
14594
14595 else
14596 null;
14597 end if;
14598 end Save_Global_Descendant;
14599
14600 ---------------------
14601 -- Save_References --
14602 ---------------------
14603
14604 -- This is the recursive procedure that does the work once the enclosing
14605 -- generic scope has been established. We have to treat specially a
14606 -- number of node rewritings that are required by semantic processing
14607 -- and which change the kind of nodes in the generic copy: typically
14608 -- constant-folding, replacing an operator node by a string literal, or
14609 -- a selected component by an expanded name. In each of those cases, the
14610 -- transformation is propagated to the generic unit.
14611
14612 procedure Save_References (N : Node_Id) is
14613 Loc : constant Source_Ptr := Sloc (N);
14614
14615 function Requires_Delayed_Save (Nod : Node_Id) return Boolean;
14616 -- Determine whether arbitrary node Nod requires delayed capture of
14617 -- global references within its aspect specifications.
14618
14619 procedure Save_References_In_Aggregate (N : Node_Id);
14620 -- Save all global references in [extension] aggregate node N
14621
14622 procedure Save_References_In_Char_Lit_Or_Op_Symbol (N : Node_Id);
14623 -- Save all global references in a character literal or operator
14624 -- symbol denoted by N.
14625
14626 procedure Save_References_In_Descendants (N : Node_Id);
14627 -- Save all global references in all descendants of node N
14628
14629 procedure Save_References_In_Identifier (N : Node_Id);
14630 -- Save all global references in identifier node N
14631
14632 procedure Save_References_In_Operator (N : Node_Id);
14633 -- Save all global references in operator node N
14634
14635 procedure Save_References_In_Pragma (Prag : Node_Id);
14636 -- Save all global references found within the expression of pragma
14637 -- Prag.
14638
14639 ---------------------------
14640 -- Requires_Delayed_Save --
14641 ---------------------------
14642
14643 function Requires_Delayed_Save (Nod : Node_Id) return Boolean is
14644 begin
14645 -- Generic packages and subprograms require delayed capture of
14646 -- global references within their aspects due to the timing of
14647 -- annotation analysis.
14648
14649 if Nkind_In (Nod, N_Generic_Package_Declaration,
14650 N_Generic_Subprogram_Declaration,
14651 N_Package_Body,
14652 N_Package_Body_Stub,
14653 N_Subprogram_Body,
14654 N_Subprogram_Body_Stub)
14655 then
14656 -- Since the capture of global references is done on the
14657 -- unanalyzed generic template, there is no information around
14658 -- to infer the context. Use the Associated_Entity linkages to
14659 -- peek into the analyzed generic copy and determine what the
14660 -- template corresponds to.
14661
14662 if Nod = Templ then
14663 return
14664 Is_Generic_Declaration_Or_Body
14665 (Unit_Declaration_Node
14666 (Associated_Entity (Defining_Entity (Nod))));
14667
14668 -- Otherwise the generic unit being processed is not the top
14669 -- level template. It is safe to capture of global references
14670 -- within the generic unit because at this point the top level
14671 -- copy is fully analyzed.
14672
14673 else
14674 return False;
14675 end if;
14676
14677 -- Otherwise capture the global references without interference
14678
14679 else
14680 return False;
14681 end if;
14682 end Requires_Delayed_Save;
14683
14684 ----------------------------------
14685 -- Save_References_In_Aggregate --
14686 ----------------------------------
14687
14688 procedure Save_References_In_Aggregate (N : Node_Id) is
14689 Nam : Node_Id;
14690 Qual : Node_Id := Empty;
14691 Typ : Entity_Id := Empty;
14692
14693 use Atree.Unchecked_Access;
14694 -- This code section is part of implementing an untyped tree
14695 -- traversal, so it needs direct access to node fields.
14696
14697 begin
14698 N2 := Get_Associated_Node (N);
14699
14700 if Present (N2) then
14701 Typ := Etype (N2);
14702
14703 -- In an instance within a generic, use the name of the actual
14704 -- and not the original generic parameter. If the actual is
14705 -- global in the current generic it must be preserved for its
14706 -- instantiation.
14707
14708 if Nkind (Parent (Typ)) = N_Subtype_Declaration
14709 and then Present (Generic_Parent_Type (Parent (Typ)))
14710 then
14711 Typ := Base_Type (Typ);
14712 Set_Etype (N2, Typ);
14713 end if;
14714 end if;
14715
14716 if No (N2) or else No (Typ) or else not Is_Global (Typ) then
14717 Set_Associated_Node (N, Empty);
14718
14719 -- If the aggregate is an actual in a call, it has been
14720 -- resolved in the current context, to some local type. The
14721 -- enclosing call may have been disambiguated by the aggregate,
14722 -- and this disambiguation might fail at instantiation time
14723 -- because the type to which the aggregate did resolve is not
14724 -- preserved. In order to preserve some of this information,
14725 -- wrap the aggregate in a qualified expression, using the id
14726 -- of its type. For further disambiguation we qualify the type
14727 -- name with its scope (if visible) because both id's will have
14728 -- corresponding entities in an instance. This resolves most of
14729 -- the problems with missing type information on aggregates in
14730 -- instances.
14731
14732 if Present (N2)
14733 and then Nkind (N2) = Nkind (N)
14734 and then Nkind (Parent (N2)) in N_Subprogram_Call
14735 and then Present (Typ)
14736 and then Comes_From_Source (Typ)
14737 then
14738 Nam := Make_Identifier (Loc, Chars (Typ));
14739
14740 if Is_Immediately_Visible (Scope (Typ)) then
14741 Nam :=
14742 Make_Selected_Component (Loc,
14743 Prefix =>
14744 Make_Identifier (Loc, Chars (Scope (Typ))),
14745 Selector_Name => Nam);
14746 end if;
14747
14748 Qual :=
14749 Make_Qualified_Expression (Loc,
14750 Subtype_Mark => Nam,
14751 Expression => Relocate_Node (N));
14752 end if;
14753 end if;
14754
14755 Save_Global_Descendant (Field1 (N));
14756 Save_Global_Descendant (Field2 (N));
14757 Save_Global_Descendant (Field3 (N));
14758 Save_Global_Descendant (Field5 (N));
14759
14760 if Present (Qual) then
14761 Rewrite (N, Qual);
14762 end if;
14763 end Save_References_In_Aggregate;
14764
14765 ----------------------------------------------
14766 -- Save_References_In_Char_Lit_Or_Op_Symbol --
14767 ----------------------------------------------
14768
14769 procedure Save_References_In_Char_Lit_Or_Op_Symbol (N : Node_Id) is
14770 begin
14771 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
14772 Reset_Entity (N);
14773
14774 elsif Nkind (N) = N_Operator_Symbol
14775 and then Nkind (Get_Associated_Node (N)) = N_String_Literal
14776 then
14777 Change_Operator_Symbol_To_String_Literal (N);
14778 end if;
14779 end Save_References_In_Char_Lit_Or_Op_Symbol;
14780
14781 ------------------------------------
14782 -- Save_References_In_Descendants --
14783 ------------------------------------
14784
14785 procedure Save_References_In_Descendants (N : Node_Id) is
14786 use Atree.Unchecked_Access;
14787 -- This code section is part of implementing an untyped tree
14788 -- traversal, so it needs direct access to node fields.
14789
14790 begin
14791 Save_Global_Descendant (Field1 (N));
14792 Save_Global_Descendant (Field2 (N));
14793 Save_Global_Descendant (Field3 (N));
14794 Save_Global_Descendant (Field4 (N));
14795 Save_Global_Descendant (Field5 (N));
14796 end Save_References_In_Descendants;
14797
14798 -----------------------------------
14799 -- Save_References_In_Identifier --
14800 -----------------------------------
14801
14802 procedure Save_References_In_Identifier (N : Node_Id) is
14803 begin
14804 -- The node did not undergo a transformation
14805
14806 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
14807
14808 -- If this is a discriminant reference, always save it. It is
14809 -- used in the instance to find the corresponding discriminant
14810 -- positionally rather than by name.
14811
14812 Set_Original_Discriminant
14813 (N, Original_Discriminant (Get_Associated_Node (N)));
14814 Reset_Entity (N);
14815
14816 -- The analysis of the generic copy transformed the identifier
14817 -- into another construct. Propagate the changes to the template.
14818
14819 else
14820 N2 := Get_Associated_Node (N);
14821
14822 -- The identifier denotes a call to a parameterless function.
14823 -- Mark the node as resolved when the function is external.
14824
14825 if Nkind (N2) = N_Function_Call then
14826 E := Entity (Name (N2));
14827
14828 if Present (E) and then Is_Global (E) then
14829 Set_Etype (N, Etype (N2));
14830 else
14831 Set_Associated_Node (N, Empty);
14832 Set_Etype (N, Empty);
14833 end if;
14834
14835 -- The identifier denotes a named number that was constant
14836 -- folded. Preserve the original name for ASIS and undo the
14837 -- constant folding which will be repeated in the instance.
14838
14839 elsif Nkind_In (N2, N_Integer_Literal, N_Real_Literal)
14840 and then Is_Entity_Name (Original_Node (N2))
14841 then
14842 Set_Associated_Node (N, Original_Node (N2));
14843 Reset_Entity (N);
14844
14845 -- The identifier resolved to a string literal. Propagate this
14846 -- information to the generic template.
14847
14848 elsif Nkind (N2) = N_String_Literal then
14849 Rewrite (N, New_Copy (N2));
14850
14851 -- The identifier is rewritten as a dereference if it is the
14852 -- prefix of an implicit dereference. Preserve the original
14853 -- tree as the analysis of the instance will expand the node
14854 -- again, but preserve the resolved entity if it is global.
14855
14856 elsif Nkind (N2) = N_Explicit_Dereference then
14857 if Is_Entity_Name (Prefix (N2))
14858 and then Present (Entity (Prefix (N2)))
14859 and then Is_Global (Entity (Prefix (N2)))
14860 then
14861 Set_Associated_Node (N, Prefix (N2));
14862
14863 elsif Nkind (Prefix (N2)) = N_Function_Call
14864 and then Present (Entity (Name (Prefix (N2))))
14865 and then Is_Global (Entity (Name (Prefix (N2))))
14866 then
14867 Rewrite (N,
14868 Make_Explicit_Dereference (Loc,
14869 Prefix =>
14870 Make_Function_Call (Loc,
14871 Name =>
14872 New_Occurrence_Of
14873 (Entity (Name (Prefix (N2))), Loc))));
14874
14875 else
14876 Set_Associated_Node (N, Empty);
14877 Set_Etype (N, Empty);
14878 end if;
14879
14880 -- The subtype mark of a nominally unconstrained object is
14881 -- rewritten as a subtype indication using the bounds of the
14882 -- expression. Recover the original subtype mark.
14883
14884 elsif Nkind (N2) = N_Subtype_Indication
14885 and then Is_Entity_Name (Original_Node (N2))
14886 then
14887 Set_Associated_Node (N, Original_Node (N2));
14888 Reset_Entity (N);
14889 end if;
14890 end if;
14891 end Save_References_In_Identifier;
14892
14893 ---------------------------------
14894 -- Save_References_In_Operator --
14895 ---------------------------------
14896
14897 procedure Save_References_In_Operator (N : Node_Id) is
14898 begin
14899 -- The node did not undergo a transformation
14900
14901 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
14902 if Nkind (N) = N_Op_Concat then
14903 Set_Is_Component_Left_Opnd (N,
14904 Is_Component_Left_Opnd (Get_Associated_Node (N)));
14905
14906 Set_Is_Component_Right_Opnd (N,
14907 Is_Component_Right_Opnd (Get_Associated_Node (N)));
14908 end if;
14909
14910 Reset_Entity (N);
14911
14912 -- The analysis of the generic copy transformed the operator into
14913 -- some other construct. Propagate the changes to the template if
14914 -- applicable.
14915
14916 else
14917 N2 := Get_Associated_Node (N);
14918
14919 -- The operator resoved to a function call
14920
14921 if Nkind (N2) = N_Function_Call then
14922
14923 -- Add explicit qualifications in the generic template for
14924 -- all operands of universal type. This aids resolution by
14925 -- preserving the actual type of a literal or an attribute
14926 -- that yields a universal result.
14927
14928 Qualify_Universal_Operands (N, N2);
14929
14930 E := Entity (Name (N2));
14931
14932 if Present (E) and then Is_Global (E) then
14933 Set_Etype (N, Etype (N2));
14934 else
14935 Set_Associated_Node (N, Empty);
14936 Set_Etype (N, Empty);
14937 end if;
14938
14939 -- The operator was folded into a literal
14940
14941 elsif Nkind_In (N2, N_Integer_Literal,
14942 N_Real_Literal,
14943 N_String_Literal)
14944 then
14945 if Present (Original_Node (N2))
14946 and then Nkind (Original_Node (N2)) = Nkind (N)
14947 then
14948 -- Operation was constant-folded. Whenever possible,
14949 -- recover semantic information from unfolded node,
14950 -- for ASIS use.
14951
14952 Set_Associated_Node (N, Original_Node (N2));
14953
14954 if Nkind (N) = N_Op_Concat then
14955 Set_Is_Component_Left_Opnd (N,
14956 Is_Component_Left_Opnd (Get_Associated_Node (N)));
14957 Set_Is_Component_Right_Opnd (N,
14958 Is_Component_Right_Opnd (Get_Associated_Node (N)));
14959 end if;
14960
14961 Reset_Entity (N);
14962
14963 -- Propagate the constant folding back to the template
14964
14965 else
14966 Rewrite (N, New_Copy (N2));
14967 Set_Analyzed (N, False);
14968 end if;
14969
14970 -- The operator was folded into an enumeration literal. Retain
14971 -- the entity to avoid spurious ambiguities if it is overloaded
14972 -- at the point of instantiation or inlining.
14973
14974 elsif Nkind (N2) = N_Identifier
14975 and then Ekind (Entity (N2)) = E_Enumeration_Literal
14976 then
14977 Rewrite (N, New_Copy (N2));
14978 Set_Analyzed (N, False);
14979 end if;
14980 end if;
14981
14982 -- Complete the operands check if node has not been constant
14983 -- folded.
14984
14985 if Nkind (N) in N_Op then
14986 Save_Entity_Descendants (N);
14987 end if;
14988 end Save_References_In_Operator;
14989
14990 -------------------------------
14991 -- Save_References_In_Pragma --
14992 -------------------------------
14993
14994 procedure Save_References_In_Pragma (Prag : Node_Id) is
14995 Context : Node_Id;
14996 Do_Save : Boolean := True;
14997
14998 use Atree.Unchecked_Access;
14999 -- This code section is part of implementing an untyped tree
15000 -- traversal, so it needs direct access to node fields.
15001
15002 begin
15003 -- Do not save global references in pragmas generated from aspects
15004 -- because the pragmas will be regenerated at instantiation time.
15005
15006 if From_Aspect_Specification (Prag) then
15007 Do_Save := False;
15008
15009 -- The capture of global references within contract-related source
15010 -- pragmas associated with generic packages, subprograms or their
15011 -- respective bodies must be delayed due to timing of annotation
15012 -- analysis. Global references are still captured in routine
15013 -- Save_Global_References_In_Contract.
15014
15015 elsif Is_Generic_Contract_Pragma (Prag) and then Prag /= Templ then
15016 if Is_Package_Contract_Annotation (Prag) then
15017 Context := Find_Related_Package_Or_Body (Prag);
15018 else
15019 pragma Assert (Is_Subprogram_Contract_Annotation (Prag));
15020 Context := Find_Related_Declaration_Or_Body (Prag);
15021 end if;
15022
15023 -- The use of Original_Node accounts for the case when the
15024 -- related context is generic template.
15025
15026 if Requires_Delayed_Save (Original_Node (Context)) then
15027 Do_Save := False;
15028 end if;
15029 end if;
15030
15031 -- For all other cases, save all global references within the
15032 -- descendants, but skip the following semantic fields:
15033
15034 -- Field1 - Next_Pragma
15035 -- Field3 - Corresponding_Aspect
15036 -- Field5 - Next_Rep_Item
15037
15038 if Do_Save then
15039 Save_Global_Descendant (Field2 (Prag));
15040 Save_Global_Descendant (Field4 (Prag));
15041 end if;
15042 end Save_References_In_Pragma;
15043
15044 -- Start of processing for Save_References
15045
15046 begin
15047 if N = Empty then
15048 null;
15049
15050 -- Aggregates
15051
15052 elsif Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
15053 Save_References_In_Aggregate (N);
15054
15055 -- Character literals, operator symbols
15056
15057 elsif Nkind_In (N, N_Character_Literal, N_Operator_Symbol) then
15058 Save_References_In_Char_Lit_Or_Op_Symbol (N);
15059
15060 -- Defining identifiers
15061
15062 elsif Nkind (N) in N_Entity then
15063 null;
15064
15065 -- Identifiers
15066
15067 elsif Nkind (N) = N_Identifier then
15068 Save_References_In_Identifier (N);
15069
15070 -- Operators
15071
15072 elsif Nkind (N) in N_Op then
15073 Save_References_In_Operator (N);
15074
15075 -- Pragmas
15076
15077 elsif Nkind (N) = N_Pragma then
15078 Save_References_In_Pragma (N);
15079
15080 else
15081 Save_References_In_Descendants (N);
15082 end if;
15083
15084 -- Save all global references found within the aspect specifications
15085 -- of the related node.
15086
15087 if Permits_Aspect_Specifications (N) and then Has_Aspects (N) then
15088
15089 -- The capture of global references within aspects associated with
15090 -- generic packages, subprograms or their bodies must be delayed
15091 -- due to timing of annotation analysis. Global references are
15092 -- still captured in routine Save_Global_References_In_Contract.
15093
15094 if Requires_Delayed_Save (N) then
15095 null;
15096
15097 -- Otherwise save all global references within the aspects
15098
15099 else
15100 Save_Global_References_In_Aspects (N);
15101 end if;
15102 end if;
15103 end Save_References;
15104
15105 -- Start of processing for Save_Global_References
15106
15107 begin
15108 Gen_Scope := Current_Scope;
15109
15110 -- If the generic unit is a child unit, references to entities in the
15111 -- parent are treated as local, because they will be resolved anew in
15112 -- the context of the instance of the parent.
15113
15114 while Is_Child_Unit (Gen_Scope)
15115 and then Ekind (Scope (Gen_Scope)) = E_Generic_Package
15116 loop
15117 Gen_Scope := Scope (Gen_Scope);
15118 end loop;
15119
15120 Save_References (Templ);
15121 end Save_Global_References;
15122
15123 ---------------------------------------
15124 -- Save_Global_References_In_Aspects --
15125 ---------------------------------------
15126
15127 procedure Save_Global_References_In_Aspects (N : Node_Id) is
15128 Asp : Node_Id;
15129 Expr : Node_Id;
15130
15131 begin
15132 Asp := First (Aspect_Specifications (N));
15133 while Present (Asp) loop
15134 Expr := Expression (Asp);
15135
15136 if Present (Expr) then
15137 Save_Global_References (Expr);
15138 end if;
15139
15140 Next (Asp);
15141 end loop;
15142 end Save_Global_References_In_Aspects;
15143
15144 ------------------------------------------
15145 -- Set_Copied_Sloc_For_Inherited_Pragma --
15146 ------------------------------------------
15147
15148 procedure Set_Copied_Sloc_For_Inherited_Pragma
15149 (N : Node_Id;
15150 E : Entity_Id)
15151 is
15152 begin
15153 Create_Instantiation_Source (N, E,
15154 Inlined_Body => False,
15155 Inherited_Pragma => True,
15156 Factor => S_Adjustment);
15157 end Set_Copied_Sloc_For_Inherited_Pragma;
15158
15159 --------------------------------------
15160 -- Set_Copied_Sloc_For_Inlined_Body --
15161 --------------------------------------
15162
15163 procedure Set_Copied_Sloc_For_Inlined_Body (N : Node_Id; E : Entity_Id) is
15164 begin
15165 Create_Instantiation_Source (N, E,
15166 Inlined_Body => True,
15167 Inherited_Pragma => False,
15168 Factor => S_Adjustment);
15169 end Set_Copied_Sloc_For_Inlined_Body;
15170
15171 ---------------------
15172 -- Set_Instance_Of --
15173 ---------------------
15174
15175 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id) is
15176 begin
15177 Generic_Renamings.Table (Generic_Renamings.Last) := (A, B, Assoc_Null);
15178 Generic_Renamings_HTable.Set (Generic_Renamings.Last);
15179 Generic_Renamings.Increment_Last;
15180 end Set_Instance_Of;
15181
15182 --------------------
15183 -- Set_Next_Assoc --
15184 --------------------
15185
15186 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr) is
15187 begin
15188 Generic_Renamings.Table (E).Next_In_HTable := Next;
15189 end Set_Next_Assoc;
15190
15191 -------------------
15192 -- Start_Generic --
15193 -------------------
15194
15195 procedure Start_Generic is
15196 begin
15197 -- ??? More things could be factored out in this routine.
15198 -- Should probably be done at a later stage.
15199
15200 Generic_Flags.Append (Inside_A_Generic);
15201 Inside_A_Generic := True;
15202
15203 Expander_Mode_Save_And_Set (False);
15204 end Start_Generic;
15205
15206 ----------------------
15207 -- Set_Instance_Env --
15208 ----------------------
15209
15210 procedure Set_Instance_Env
15211 (Gen_Unit : Entity_Id;
15212 Act_Unit : Entity_Id)
15213 is
15214 Assertion_Status : constant Boolean := Assertions_Enabled;
15215 Save_SPARK_Mode : constant SPARK_Mode_Type := SPARK_Mode;
15216 Save_SPARK_Mode_Pragma : constant Node_Id := SPARK_Mode_Pragma;
15217
15218 begin
15219 -- Regardless of the current mode, predefined units are analyzed in the
15220 -- most current Ada mode, and earlier version Ada checks do not apply
15221 -- to predefined units. Nothing needs to be done for non-internal units.
15222 -- These are always analyzed in the current mode.
15223
15224 if Is_Internal_File_Name
15225 (Fname => Unit_File_Name (Get_Source_Unit (Gen_Unit)),
15226 Renamings_Included => True)
15227 then
15228 Set_Opt_Config_Switches (True, Current_Sem_Unit = Main_Unit);
15229
15230 -- In Ada2012 we may want to enable assertions in an instance of a
15231 -- predefined unit, in which case we need to preserve the current
15232 -- setting for the Assertions_Enabled flag. This will become more
15233 -- critical when pre/postconditions are added to predefined units,
15234 -- as is already the case for some numeric libraries.
15235
15236 if Ada_Version >= Ada_2012 then
15237 Assertions_Enabled := Assertion_Status;
15238 end if;
15239
15240 -- SPARK_Mode for an instance is the one applicable at the point of
15241 -- instantiation.
15242
15243 SPARK_Mode := Save_SPARK_Mode;
15244 SPARK_Mode_Pragma := Save_SPARK_Mode_Pragma;
15245 end if;
15246
15247 Current_Instantiated_Parent :=
15248 (Gen_Id => Gen_Unit,
15249 Act_Id => Act_Unit,
15250 Next_In_HTable => Assoc_Null);
15251 end Set_Instance_Env;
15252
15253 -----------------
15254 -- Switch_View --
15255 -----------------
15256
15257 procedure Switch_View (T : Entity_Id) is
15258 BT : constant Entity_Id := Base_Type (T);
15259 Priv_Elmt : Elmt_Id := No_Elmt;
15260 Priv_Sub : Entity_Id;
15261
15262 begin
15263 -- T may be private but its base type may have been exchanged through
15264 -- some other occurrence, in which case there is nothing to switch
15265 -- besides T itself. Note that a private dependent subtype of a private
15266 -- type might not have been switched even if the base type has been,
15267 -- because of the last branch of Check_Private_View (see comment there).
15268
15269 if not Is_Private_Type (BT) then
15270 Prepend_Elmt (Full_View (T), Exchanged_Views);
15271 Exchange_Declarations (T);
15272 return;
15273 end if;
15274
15275 Priv_Elmt := First_Elmt (Private_Dependents (BT));
15276
15277 if Present (Full_View (BT)) then
15278 Prepend_Elmt (Full_View (BT), Exchanged_Views);
15279 Exchange_Declarations (BT);
15280 end if;
15281
15282 while Present (Priv_Elmt) loop
15283 Priv_Sub := (Node (Priv_Elmt));
15284
15285 -- We avoid flipping the subtype if the Etype of its full view is
15286 -- private because this would result in a malformed subtype. This
15287 -- occurs when the Etype of the subtype full view is the full view of
15288 -- the base type (and since the base types were just switched, the
15289 -- subtype is pointing to the wrong view). This is currently the case
15290 -- for tagged record types, access types (maybe more?) and needs to
15291 -- be resolved. ???
15292
15293 if Present (Full_View (Priv_Sub))
15294 and then not Is_Private_Type (Etype (Full_View (Priv_Sub)))
15295 then
15296 Prepend_Elmt (Full_View (Priv_Sub), Exchanged_Views);
15297 Exchange_Declarations (Priv_Sub);
15298 end if;
15299
15300 Next_Elmt (Priv_Elmt);
15301 end loop;
15302 end Switch_View;
15303
15304 -----------------
15305 -- True_Parent --
15306 -----------------
15307
15308 function True_Parent (N : Node_Id) return Node_Id is
15309 begin
15310 if Nkind (Parent (N)) = N_Subunit then
15311 return Parent (Corresponding_Stub (Parent (N)));
15312 else
15313 return Parent (N);
15314 end if;
15315 end True_Parent;
15316
15317 -----------------------------
15318 -- Valid_Default_Attribute --
15319 -----------------------------
15320
15321 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id) is
15322 Attr_Id : constant Attribute_Id :=
15323 Get_Attribute_Id (Attribute_Name (Def));
15324 T : constant Entity_Id := Entity (Prefix (Def));
15325 Is_Fun : constant Boolean := (Ekind (Nam) = E_Function);
15326 F : Entity_Id;
15327 Num_F : Nat;
15328 OK : Boolean;
15329
15330 begin
15331 if No (T) or else T = Any_Id then
15332 return;
15333 end if;
15334
15335 Num_F := 0;
15336 F := First_Formal (Nam);
15337 while Present (F) loop
15338 Num_F := Num_F + 1;
15339 Next_Formal (F);
15340 end loop;
15341
15342 case Attr_Id is
15343 when Attribute_Adjacent | Attribute_Ceiling | Attribute_Copy_Sign |
15344 Attribute_Floor | Attribute_Fraction | Attribute_Machine |
15345 Attribute_Model | Attribute_Remainder | Attribute_Rounding |
15346 Attribute_Unbiased_Rounding =>
15347 OK := Is_Fun
15348 and then Num_F = 1
15349 and then Is_Floating_Point_Type (T);
15350
15351 when Attribute_Image | Attribute_Pred | Attribute_Succ |
15352 Attribute_Value | Attribute_Wide_Image |
15353 Attribute_Wide_Value =>
15354 OK := (Is_Fun and then Num_F = 1 and then Is_Scalar_Type (T));
15355
15356 when Attribute_Max | Attribute_Min =>
15357 OK := (Is_Fun and then Num_F = 2 and then Is_Scalar_Type (T));
15358
15359 when Attribute_Input =>
15360 OK := (Is_Fun and then Num_F = 1);
15361
15362 when Attribute_Output | Attribute_Read | Attribute_Write =>
15363 OK := (not Is_Fun and then Num_F = 2);
15364
15365 when others =>
15366 OK := False;
15367 end case;
15368
15369 if not OK then
15370 Error_Msg_N
15371 ("attribute reference has wrong profile for subprogram", Def);
15372 end if;
15373 end Valid_Default_Attribute;
15374
15375 end Sem_Ch12;