1d4d5c0bdf678d19afe735c9302781c0cb89a8eb
[gcc.git] / gcc / ada / sem_ch12.adb
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S E M _ C H 1 2 --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-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 -- Analyze_Associations --
1057 --------------------------
1058
1059 function Analyze_Associations
1060 (I_Node : Node_Id;
1061 Formals : List_Id;
1062 F_Copy : List_Id) return List_Id
1063 is
1064 Actuals_To_Freeze : constant Elist_Id := New_Elmt_List;
1065 Assoc : constant List_Id := New_List;
1066 Default_Actuals : constant List_Id := New_List;
1067 Gen_Unit : constant Entity_Id :=
1068 Defining_Entity (Parent (F_Copy));
1069
1070 Actuals : List_Id;
1071 Actual : Node_Id;
1072 Analyzed_Formal : Node_Id;
1073 First_Named : Node_Id := Empty;
1074 Formal : Node_Id;
1075 Match : Node_Id;
1076 Named : Node_Id;
1077 Saved_Formal : Node_Id;
1078
1079 Default_Formals : constant List_Id := New_List;
1080 -- If an Others_Choice is present, some of the formals may be defaulted.
1081 -- To simplify the treatment of visibility in an instance, we introduce
1082 -- individual defaults for each such formal. These defaults are
1083 -- appended to the list of associations and replace the Others_Choice.
1084
1085 Found_Assoc : Node_Id;
1086 -- Association for the current formal being match. Empty if there are
1087 -- no remaining actuals, or if there is no named association with the
1088 -- name of the formal.
1089
1090 Is_Named_Assoc : Boolean;
1091 Num_Matched : Nat := 0;
1092 Num_Actuals : Nat := 0;
1093
1094 Others_Present : Boolean := False;
1095 Others_Choice : Node_Id := Empty;
1096 -- In Ada 2005, indicates partial parameterization of a formal
1097 -- package. As usual an other association must be last in the list.
1098
1099 procedure Check_Overloaded_Formal_Subprogram (Formal : Entity_Id);
1100 -- Apply RM 12.3(9): if a formal subprogram is overloaded, the instance
1101 -- cannot have a named association for it. AI05-0025 extends this rule
1102 -- to formals of formal packages by AI05-0025, and it also applies to
1103 -- box-initialized formals.
1104
1105 function Has_Fully_Defined_Profile (Subp : Entity_Id) return Boolean;
1106 -- Determine whether the parameter types and the return type of Subp
1107 -- are fully defined at the point of instantiation.
1108
1109 function Matching_Actual
1110 (F : Entity_Id;
1111 A_F : Entity_Id) return Node_Id;
1112 -- Find actual that corresponds to a given a formal parameter. If the
1113 -- actuals are positional, return the next one, if any. If the actuals
1114 -- are named, scan the parameter associations to find the right one.
1115 -- A_F is the corresponding entity in the analyzed generic,which is
1116 -- placed on the selector name for ASIS use.
1117 --
1118 -- In Ada 2005, a named association may be given with a box, in which
1119 -- case Matching_Actual sets Found_Assoc to the generic association,
1120 -- but return Empty for the actual itself. In this case the code below
1121 -- creates a corresponding declaration for the formal.
1122
1123 function Partial_Parameterization return Boolean;
1124 -- Ada 2005: if no match is found for a given formal, check if the
1125 -- association for it includes a box, or whether the associations
1126 -- include an Others clause.
1127
1128 procedure Process_Default (F : Entity_Id);
1129 -- Add a copy of the declaration of generic formal F to the list of
1130 -- associations, and add an explicit box association for F if there
1131 -- is none yet, and the default comes from an Others_Choice.
1132
1133 function Renames_Standard_Subprogram (Subp : Entity_Id) return Boolean;
1134 -- Determine whether Subp renames one of the subprograms defined in the
1135 -- generated package Standard.
1136
1137 procedure Set_Analyzed_Formal;
1138 -- Find the node in the generic copy that corresponds to a given formal.
1139 -- The semantic information on this node is used to perform legality
1140 -- checks on the actuals. Because semantic analysis can introduce some
1141 -- anonymous entities or modify the declaration node itself, the
1142 -- correspondence between the two lists is not one-one. In addition to
1143 -- anonymous types, the presence a formal equality will introduce an
1144 -- implicit declaration for the corresponding inequality.
1145
1146 ----------------------------------------
1147 -- Check_Overloaded_Formal_Subprogram --
1148 ----------------------------------------
1149
1150 procedure Check_Overloaded_Formal_Subprogram (Formal : Entity_Id) is
1151 Temp_Formal : Entity_Id;
1152
1153 begin
1154 Temp_Formal := First (Formals);
1155 while Present (Temp_Formal) loop
1156 if Nkind (Temp_Formal) in N_Formal_Subprogram_Declaration
1157 and then Temp_Formal /= Formal
1158 and then
1159 Chars (Defining_Unit_Name (Specification (Formal))) =
1160 Chars (Defining_Unit_Name (Specification (Temp_Formal)))
1161 then
1162 if Present (Found_Assoc) then
1163 Error_Msg_N
1164 ("named association not allowed for overloaded formal",
1165 Found_Assoc);
1166
1167 else
1168 Error_Msg_N
1169 ("named association not allowed for overloaded formal",
1170 Others_Choice);
1171 end if;
1172
1173 Abandon_Instantiation (Instantiation_Node);
1174 end if;
1175
1176 Next (Temp_Formal);
1177 end loop;
1178 end Check_Overloaded_Formal_Subprogram;
1179
1180 -------------------------------
1181 -- Has_Fully_Defined_Profile --
1182 -------------------------------
1183
1184 function Has_Fully_Defined_Profile (Subp : Entity_Id) return Boolean is
1185 function Is_Fully_Defined_Type (Typ : Entity_Id) return Boolean;
1186 -- Determine whethet type Typ is fully defined
1187
1188 ---------------------------
1189 -- Is_Fully_Defined_Type --
1190 ---------------------------
1191
1192 function Is_Fully_Defined_Type (Typ : Entity_Id) return Boolean is
1193 begin
1194 -- A private type without a full view is not fully defined
1195
1196 if Is_Private_Type (Typ)
1197 and then No (Full_View (Typ))
1198 then
1199 return False;
1200
1201 -- An incomplete type is never fully defined
1202
1203 elsif Is_Incomplete_Type (Typ) then
1204 return False;
1205
1206 -- All other types are fully defined
1207
1208 else
1209 return True;
1210 end if;
1211 end Is_Fully_Defined_Type;
1212
1213 -- Local declarations
1214
1215 Param : Entity_Id;
1216
1217 -- Start of processing for Has_Fully_Defined_Profile
1218
1219 begin
1220 -- Check the parameters
1221
1222 Param := First_Formal (Subp);
1223 while Present (Param) loop
1224 if not Is_Fully_Defined_Type (Etype (Param)) then
1225 return False;
1226 end if;
1227
1228 Next_Formal (Param);
1229 end loop;
1230
1231 -- Check the return type
1232
1233 return Is_Fully_Defined_Type (Etype (Subp));
1234 end Has_Fully_Defined_Profile;
1235
1236 ---------------------
1237 -- Matching_Actual --
1238 ---------------------
1239
1240 function Matching_Actual
1241 (F : Entity_Id;
1242 A_F : Entity_Id) return Node_Id
1243 is
1244 Prev : Node_Id;
1245 Act : Node_Id;
1246
1247 begin
1248 Is_Named_Assoc := False;
1249
1250 -- End of list of purely positional parameters
1251
1252 if No (Actual) or else Nkind (Actual) = N_Others_Choice then
1253 Found_Assoc := Empty;
1254 Act := Empty;
1255
1256 -- Case of positional parameter corresponding to current formal
1257
1258 elsif No (Selector_Name (Actual)) then
1259 Found_Assoc := Actual;
1260 Act := Explicit_Generic_Actual_Parameter (Actual);
1261 Num_Matched := Num_Matched + 1;
1262 Next (Actual);
1263
1264 -- Otherwise scan list of named actuals to find the one with the
1265 -- desired name. All remaining actuals have explicit names.
1266
1267 else
1268 Is_Named_Assoc := True;
1269 Found_Assoc := Empty;
1270 Act := Empty;
1271 Prev := Empty;
1272
1273 while Present (Actual) loop
1274 if Chars (Selector_Name (Actual)) = Chars (F) then
1275 Set_Entity (Selector_Name (Actual), A_F);
1276 Set_Etype (Selector_Name (Actual), Etype (A_F));
1277 Generate_Reference (A_F, Selector_Name (Actual));
1278 Found_Assoc := Actual;
1279 Act := Explicit_Generic_Actual_Parameter (Actual);
1280 Num_Matched := Num_Matched + 1;
1281 exit;
1282 end if;
1283
1284 Prev := Actual;
1285 Next (Actual);
1286 end loop;
1287
1288 -- Reset for subsequent searches. In most cases the named
1289 -- associations are in order. If they are not, we reorder them
1290 -- to avoid scanning twice the same actual. This is not just a
1291 -- question of efficiency: there may be multiple defaults with
1292 -- boxes that have the same name. In a nested instantiation we
1293 -- insert actuals for those defaults, and cannot rely on their
1294 -- names to disambiguate them.
1295
1296 if Actual = First_Named then
1297 Next (First_Named);
1298
1299 elsif Present (Actual) then
1300 Insert_Before (First_Named, Remove_Next (Prev));
1301 end if;
1302
1303 Actual := First_Named;
1304 end if;
1305
1306 if Is_Entity_Name (Act) and then Present (Entity (Act)) then
1307 Set_Used_As_Generic_Actual (Entity (Act));
1308 end if;
1309
1310 return Act;
1311 end Matching_Actual;
1312
1313 ------------------------------
1314 -- Partial_Parameterization --
1315 ------------------------------
1316
1317 function Partial_Parameterization return Boolean is
1318 begin
1319 return Others_Present
1320 or else (Present (Found_Assoc) and then Box_Present (Found_Assoc));
1321 end Partial_Parameterization;
1322
1323 ---------------------
1324 -- Process_Default --
1325 ---------------------
1326
1327 procedure Process_Default (F : Entity_Id) is
1328 Loc : constant Source_Ptr := Sloc (I_Node);
1329 F_Id : constant Entity_Id := Defining_Entity (F);
1330 Decl : Node_Id;
1331 Default : Node_Id;
1332 Id : Entity_Id;
1333
1334 begin
1335 -- Append copy of formal declaration to associations, and create new
1336 -- defining identifier for it.
1337
1338 Decl := New_Copy_Tree (F);
1339 Id := Make_Defining_Identifier (Sloc (F_Id), Chars (F_Id));
1340
1341 if Nkind (F) in N_Formal_Subprogram_Declaration then
1342 Set_Defining_Unit_Name (Specification (Decl), Id);
1343
1344 else
1345 Set_Defining_Identifier (Decl, Id);
1346 end if;
1347
1348 Append (Decl, Assoc);
1349
1350 if No (Found_Assoc) then
1351 Default :=
1352 Make_Generic_Association (Loc,
1353 Selector_Name =>
1354 New_Occurrence_Of (Id, Loc),
1355 Explicit_Generic_Actual_Parameter => Empty);
1356 Set_Box_Present (Default);
1357 Append (Default, Default_Formals);
1358 end if;
1359 end Process_Default;
1360
1361 ---------------------------------
1362 -- Renames_Standard_Subprogram --
1363 ---------------------------------
1364
1365 function Renames_Standard_Subprogram (Subp : Entity_Id) return Boolean is
1366 Id : Entity_Id;
1367
1368 begin
1369 Id := Alias (Subp);
1370 while Present (Id) loop
1371 if Scope (Id) = Standard_Standard then
1372 return True;
1373 end if;
1374
1375 Id := Alias (Id);
1376 end loop;
1377
1378 return False;
1379 end Renames_Standard_Subprogram;
1380
1381 -------------------------
1382 -- Set_Analyzed_Formal --
1383 -------------------------
1384
1385 procedure Set_Analyzed_Formal is
1386 Kind : Node_Kind;
1387
1388 begin
1389 while Present (Analyzed_Formal) loop
1390 Kind := Nkind (Analyzed_Formal);
1391
1392 case Nkind (Formal) is
1393
1394 when N_Formal_Subprogram_Declaration =>
1395 exit when Kind in N_Formal_Subprogram_Declaration
1396 and then
1397 Chars
1398 (Defining_Unit_Name (Specification (Formal))) =
1399 Chars
1400 (Defining_Unit_Name (Specification (Analyzed_Formal)));
1401
1402 when N_Formal_Package_Declaration =>
1403 exit when Nkind_In (Kind, N_Formal_Package_Declaration,
1404 N_Generic_Package_Declaration,
1405 N_Package_Declaration);
1406
1407 when N_Use_Package_Clause | N_Use_Type_Clause => exit;
1408
1409 when others =>
1410
1411 -- Skip freeze nodes, and nodes inserted to replace
1412 -- unrecognized pragmas.
1413
1414 exit when
1415 Kind not in N_Formal_Subprogram_Declaration
1416 and then not Nkind_In (Kind, N_Subprogram_Declaration,
1417 N_Freeze_Entity,
1418 N_Null_Statement,
1419 N_Itype_Reference)
1420 and then Chars (Defining_Identifier (Formal)) =
1421 Chars (Defining_Identifier (Analyzed_Formal));
1422 end case;
1423
1424 Next (Analyzed_Formal);
1425 end loop;
1426 end Set_Analyzed_Formal;
1427
1428 -- Start of processing for Analyze_Associations
1429
1430 begin
1431 Actuals := Generic_Associations (I_Node);
1432
1433 if Present (Actuals) then
1434
1435 -- Check for an Others choice, indicating a partial parameterization
1436 -- for a formal package.
1437
1438 Actual := First (Actuals);
1439 while Present (Actual) loop
1440 if Nkind (Actual) = N_Others_Choice then
1441 Others_Present := True;
1442 Others_Choice := Actual;
1443
1444 if Present (Next (Actual)) then
1445 Error_Msg_N ("others must be last association", Actual);
1446 end if;
1447
1448 -- This subprogram is used both for formal packages and for
1449 -- instantiations. For the latter, associations must all be
1450 -- explicit.
1451
1452 if Nkind (I_Node) /= N_Formal_Package_Declaration
1453 and then Comes_From_Source (I_Node)
1454 then
1455 Error_Msg_N
1456 ("others association not allowed in an instance",
1457 Actual);
1458 end if;
1459
1460 -- In any case, nothing to do after the others association
1461
1462 exit;
1463
1464 elsif Box_Present (Actual)
1465 and then Comes_From_Source (I_Node)
1466 and then Nkind (I_Node) /= N_Formal_Package_Declaration
1467 then
1468 Error_Msg_N
1469 ("box association not allowed in an instance", Actual);
1470 end if;
1471
1472 Next (Actual);
1473 end loop;
1474
1475 -- If named associations are present, save first named association
1476 -- (it may of course be Empty) to facilitate subsequent name search.
1477
1478 First_Named := First (Actuals);
1479 while Present (First_Named)
1480 and then Nkind (First_Named) /= N_Others_Choice
1481 and then No (Selector_Name (First_Named))
1482 loop
1483 Num_Actuals := Num_Actuals + 1;
1484 Next (First_Named);
1485 end loop;
1486 end if;
1487
1488 Named := First_Named;
1489 while Present (Named) loop
1490 if Nkind (Named) /= N_Others_Choice
1491 and then No (Selector_Name (Named))
1492 then
1493 Error_Msg_N ("invalid positional actual after named one", Named);
1494 Abandon_Instantiation (Named);
1495 end if;
1496
1497 -- A named association may lack an actual parameter, if it was
1498 -- introduced for a default subprogram that turns out to be local
1499 -- to the outer instantiation.
1500
1501 if Nkind (Named) /= N_Others_Choice
1502 and then Present (Explicit_Generic_Actual_Parameter (Named))
1503 then
1504 Num_Actuals := Num_Actuals + 1;
1505 end if;
1506
1507 Next (Named);
1508 end loop;
1509
1510 if Present (Formals) then
1511 Formal := First_Non_Pragma (Formals);
1512 Analyzed_Formal := First_Non_Pragma (F_Copy);
1513
1514 if Present (Actuals) then
1515 Actual := First (Actuals);
1516
1517 -- All formals should have default values
1518
1519 else
1520 Actual := Empty;
1521 end if;
1522
1523 while Present (Formal) loop
1524 Set_Analyzed_Formal;
1525 Saved_Formal := Next_Non_Pragma (Formal);
1526
1527 case Nkind (Formal) is
1528 when N_Formal_Object_Declaration =>
1529 Match :=
1530 Matching_Actual
1531 (Defining_Identifier (Formal),
1532 Defining_Identifier (Analyzed_Formal));
1533
1534 if No (Match) and then Partial_Parameterization then
1535 Process_Default (Formal);
1536
1537 else
1538 Append_List
1539 (Instantiate_Object (Formal, Match, Analyzed_Formal),
1540 Assoc);
1541
1542 -- For a defaulted in_parameter, create an entry in the
1543 -- the list of defaulted actuals, for GNATProve use. Do
1544 -- not included these defaults for an instance nested
1545 -- within a generic, because the defaults are also used
1546 -- in the analysis of the enclosing generic, and only
1547 -- defaulted subprograms are relevant there.
1548
1549 if No (Match) and then not Inside_A_Generic then
1550 Append_To (Default_Actuals,
1551 Make_Generic_Association (Sloc (I_Node),
1552 Selector_Name =>
1553 New_Occurrence_Of
1554 (Defining_Identifier (Formal), Sloc (I_Node)),
1555 Explicit_Generic_Actual_Parameter =>
1556 New_Copy_Tree (Default_Expression (Formal))));
1557 end if;
1558 end if;
1559
1560 -- If the object is a call to an expression function, this
1561 -- is a freezing point for it.
1562
1563 if Is_Entity_Name (Match)
1564 and then Present (Entity (Match))
1565 and then Nkind
1566 (Original_Node (Unit_Declaration_Node (Entity (Match))))
1567 = N_Expression_Function
1568 then
1569 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1570 end if;
1571
1572 when N_Formal_Type_Declaration =>
1573 Match :=
1574 Matching_Actual
1575 (Defining_Identifier (Formal),
1576 Defining_Identifier (Analyzed_Formal));
1577
1578 if No (Match) then
1579 if Partial_Parameterization then
1580 Process_Default (Formal);
1581
1582 else
1583 Error_Msg_Sloc := Sloc (Gen_Unit);
1584 Error_Msg_NE
1585 ("missing actual&",
1586 Instantiation_Node, Defining_Identifier (Formal));
1587 Error_Msg_NE
1588 ("\in instantiation of & declared#",
1589 Instantiation_Node, Gen_Unit);
1590 Abandon_Instantiation (Instantiation_Node);
1591 end if;
1592
1593 else
1594 Analyze (Match);
1595 Append_List
1596 (Instantiate_Type
1597 (Formal, Match, Analyzed_Formal, Assoc),
1598 Assoc);
1599
1600 -- An instantiation is a freeze point for the actuals,
1601 -- unless this is a rewritten formal package, or the
1602 -- formal is an Ada 2012 formal incomplete type.
1603
1604 if Nkind (I_Node) = N_Formal_Package_Declaration
1605 or else
1606 (Ada_Version >= Ada_2012
1607 and then
1608 Ekind (Defining_Identifier (Analyzed_Formal)) =
1609 E_Incomplete_Type)
1610 then
1611 null;
1612
1613 else
1614 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1615 end if;
1616 end if;
1617
1618 -- A remote access-to-class-wide type is not a legal actual
1619 -- for a generic formal of an access type (E.2.2(17/2)).
1620 -- In GNAT an exception to this rule is introduced when
1621 -- the formal is marked as remote using implementation
1622 -- defined aspect/pragma Remote_Access_Type. In that case
1623 -- the actual must be remote as well.
1624
1625 -- If the current instantiation is the construction of a
1626 -- local copy for a formal package the actuals may be
1627 -- defaulted, and there is no matching actual to check.
1628
1629 if Nkind (Analyzed_Formal) = N_Formal_Type_Declaration
1630 and then
1631 Nkind (Formal_Type_Definition (Analyzed_Formal)) =
1632 N_Access_To_Object_Definition
1633 and then Present (Match)
1634 then
1635 declare
1636 Formal_Ent : constant Entity_Id :=
1637 Defining_Identifier (Analyzed_Formal);
1638 begin
1639 if Is_Remote_Access_To_Class_Wide_Type (Entity (Match))
1640 = Is_Remote_Types (Formal_Ent)
1641 then
1642 -- Remoteness of formal and actual match
1643
1644 null;
1645
1646 elsif Is_Remote_Types (Formal_Ent) then
1647
1648 -- Remote formal, non-remote actual
1649
1650 Error_Msg_NE
1651 ("actual for& must be remote", Match, Formal_Ent);
1652
1653 else
1654 -- Non-remote formal, remote actual
1655
1656 Error_Msg_NE
1657 ("actual for& may not be remote",
1658 Match, Formal_Ent);
1659 end if;
1660 end;
1661 end if;
1662
1663 when N_Formal_Subprogram_Declaration =>
1664 Match :=
1665 Matching_Actual
1666 (Defining_Unit_Name (Specification (Formal)),
1667 Defining_Unit_Name (Specification (Analyzed_Formal)));
1668
1669 -- If the formal subprogram has the same name as another
1670 -- formal subprogram of the generic, then a named
1671 -- association is illegal (12.3(9)). Exclude named
1672 -- associations that are generated for a nested instance.
1673
1674 if Present (Match)
1675 and then Is_Named_Assoc
1676 and then Comes_From_Source (Found_Assoc)
1677 then
1678 Check_Overloaded_Formal_Subprogram (Formal);
1679 end if;
1680
1681 -- If there is no corresponding actual, this may be case
1682 -- of partial parameterization, or else the formal has a
1683 -- default or a box.
1684
1685 if No (Match) and then Partial_Parameterization then
1686 Process_Default (Formal);
1687
1688 if Nkind (I_Node) = N_Formal_Package_Declaration then
1689 Check_Overloaded_Formal_Subprogram (Formal);
1690 end if;
1691
1692 else
1693 Append_To (Assoc,
1694 Instantiate_Formal_Subprogram
1695 (Formal, Match, Analyzed_Formal));
1696
1697 -- An instantiation is a freeze point for the actuals,
1698 -- unless this is a rewritten formal package.
1699
1700 if Nkind (I_Node) /= N_Formal_Package_Declaration
1701 and then Nkind (Match) = N_Identifier
1702 and then Is_Subprogram (Entity (Match))
1703
1704 -- The actual subprogram may rename a routine defined
1705 -- in Standard. Avoid freezing such renamings because
1706 -- subprograms coming from Standard cannot be frozen.
1707
1708 and then
1709 not Renames_Standard_Subprogram (Entity (Match))
1710
1711 -- If the actual subprogram comes from a different
1712 -- unit, it is already frozen, either by a body in
1713 -- that unit or by the end of the declarative part
1714 -- of the unit. This check avoids the freezing of
1715 -- subprograms defined in Standard which are used
1716 -- as generic actuals.
1717
1718 and then In_Same_Code_Unit (Entity (Match), I_Node)
1719 and then Has_Fully_Defined_Profile (Entity (Match))
1720 then
1721 -- Mark the subprogram as having a delayed freeze
1722 -- since this may be an out-of-order action.
1723
1724 Set_Has_Delayed_Freeze (Entity (Match));
1725 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1726 end if;
1727 end if;
1728
1729 -- If this is a nested generic, preserve default for later
1730 -- instantiations. We do this as well for GNATProve use,
1731 -- so that the list of generic associations is complete.
1732
1733 if No (Match) and then Box_Present (Formal) then
1734 declare
1735 Subp : constant Entity_Id :=
1736 Defining_Unit_Name (Specification (Last (Assoc)));
1737
1738 begin
1739 Append_To (Default_Actuals,
1740 Make_Generic_Association (Sloc (I_Node),
1741 Selector_Name =>
1742 New_Occurrence_Of (Subp, Sloc (I_Node)),
1743 Explicit_Generic_Actual_Parameter =>
1744 New_Occurrence_Of (Subp, Sloc (I_Node))));
1745 end;
1746 end if;
1747
1748 when N_Formal_Package_Declaration =>
1749 Match :=
1750 Matching_Actual
1751 (Defining_Identifier (Formal),
1752 Defining_Identifier (Original_Node (Analyzed_Formal)));
1753
1754 if No (Match) then
1755 if Partial_Parameterization then
1756 Process_Default (Formal);
1757
1758 else
1759 Error_Msg_Sloc := Sloc (Gen_Unit);
1760 Error_Msg_NE
1761 ("missing actual&",
1762 Instantiation_Node, Defining_Identifier (Formal));
1763 Error_Msg_NE
1764 ("\in instantiation of & declared#",
1765 Instantiation_Node, Gen_Unit);
1766
1767 Abandon_Instantiation (Instantiation_Node);
1768 end if;
1769
1770 else
1771 Analyze (Match);
1772 Append_List
1773 (Instantiate_Formal_Package
1774 (Formal, Match, Analyzed_Formal),
1775 Assoc);
1776 end if;
1777
1778 -- For use type and use package appearing in the generic part,
1779 -- we have already copied them, so we can just move them where
1780 -- they belong (we mustn't recopy them since this would mess up
1781 -- the Sloc values).
1782
1783 when N_Use_Package_Clause |
1784 N_Use_Type_Clause =>
1785 if Nkind (Original_Node (I_Node)) =
1786 N_Formal_Package_Declaration
1787 then
1788 Append (New_Copy_Tree (Formal), Assoc);
1789 else
1790 Remove (Formal);
1791 Append (Formal, Assoc);
1792 end if;
1793
1794 when others =>
1795 raise Program_Error;
1796
1797 end case;
1798
1799 Formal := Saved_Formal;
1800 Next_Non_Pragma (Analyzed_Formal);
1801 end loop;
1802
1803 if Num_Actuals > Num_Matched then
1804 Error_Msg_Sloc := Sloc (Gen_Unit);
1805
1806 if Present (Selector_Name (Actual)) then
1807 Error_Msg_NE
1808 ("unmatched actual &", Actual, Selector_Name (Actual));
1809 Error_Msg_NE
1810 ("\in instantiation of & declared#", Actual, Gen_Unit);
1811 else
1812 Error_Msg_NE
1813 ("unmatched actual in instantiation of & declared#",
1814 Actual, Gen_Unit);
1815 end if;
1816 end if;
1817
1818 elsif Present (Actuals) then
1819 Error_Msg_N
1820 ("too many actuals in generic instantiation", Instantiation_Node);
1821 end if;
1822
1823 -- An instantiation freezes all generic actuals. The only exceptions
1824 -- to this are incomplete types and subprograms which are not fully
1825 -- defined at the point of instantiation.
1826
1827 declare
1828 Elmt : Elmt_Id := First_Elmt (Actuals_To_Freeze);
1829 begin
1830 while Present (Elmt) loop
1831 Freeze_Before (I_Node, Node (Elmt));
1832 Next_Elmt (Elmt);
1833 end loop;
1834 end;
1835
1836 -- If there are default subprograms, normalize the tree by adding
1837 -- explicit associations for them. This is required if the instance
1838 -- appears within a generic.
1839
1840 if not Is_Empty_List (Default_Actuals) then
1841 declare
1842 Default : Node_Id;
1843
1844 begin
1845 Default := First (Default_Actuals);
1846 while Present (Default) loop
1847 Mark_Rewrite_Insertion (Default);
1848 Next (Default);
1849 end loop;
1850
1851 if No (Actuals) then
1852 Set_Generic_Associations (I_Node, Default_Actuals);
1853 else
1854 Append_List_To (Actuals, Default_Actuals);
1855 end if;
1856 end;
1857 end if;
1858
1859 -- If this is a formal package, normalize the parameter list by adding
1860 -- explicit box associations for the formals that are covered by an
1861 -- Others_Choice.
1862
1863 if not Is_Empty_List (Default_Formals) then
1864 Append_List (Default_Formals, Formals);
1865 end if;
1866
1867 return Assoc;
1868 end Analyze_Associations;
1869
1870 -------------------------------
1871 -- Analyze_Formal_Array_Type --
1872 -------------------------------
1873
1874 procedure Analyze_Formal_Array_Type
1875 (T : in out Entity_Id;
1876 Def : Node_Id)
1877 is
1878 DSS : Node_Id;
1879
1880 begin
1881 -- Treated like a non-generic array declaration, with additional
1882 -- semantic checks.
1883
1884 Enter_Name (T);
1885
1886 if Nkind (Def) = N_Constrained_Array_Definition then
1887 DSS := First (Discrete_Subtype_Definitions (Def));
1888 while Present (DSS) loop
1889 if Nkind_In (DSS, N_Subtype_Indication,
1890 N_Range,
1891 N_Attribute_Reference)
1892 then
1893 Error_Msg_N ("only a subtype mark is allowed in a formal", DSS);
1894 end if;
1895
1896 Next (DSS);
1897 end loop;
1898 end if;
1899
1900 Array_Type_Declaration (T, Def);
1901 Set_Is_Generic_Type (Base_Type (T));
1902
1903 if Ekind (Component_Type (T)) = E_Incomplete_Type
1904 and then No (Full_View (Component_Type (T)))
1905 then
1906 Error_Msg_N ("premature usage of incomplete type", Def);
1907
1908 -- Check that range constraint is not allowed on the component type
1909 -- of a generic formal array type (AARM 12.5.3(3))
1910
1911 elsif Is_Internal (Component_Type (T))
1912 and then Present (Subtype_Indication (Component_Definition (Def)))
1913 and then Nkind (Original_Node
1914 (Subtype_Indication (Component_Definition (Def)))) =
1915 N_Subtype_Indication
1916 then
1917 Error_Msg_N
1918 ("in a formal, a subtype indication can only be "
1919 & "a subtype mark (RM 12.5.3(3))",
1920 Subtype_Indication (Component_Definition (Def)));
1921 end if;
1922
1923 end Analyze_Formal_Array_Type;
1924
1925 ---------------------------------------------
1926 -- Analyze_Formal_Decimal_Fixed_Point_Type --
1927 ---------------------------------------------
1928
1929 -- As for other generic types, we create a valid type representation with
1930 -- legal but arbitrary attributes, whose values are never considered
1931 -- static. For all scalar types we introduce an anonymous base type, with
1932 -- the same attributes. We choose the corresponding integer type to be
1933 -- Standard_Integer.
1934 -- Here and in other similar routines, the Sloc of the generated internal
1935 -- type must be the same as the sloc of the defining identifier of the
1936 -- formal type declaration, to provide proper source navigation.
1937
1938 procedure Analyze_Formal_Decimal_Fixed_Point_Type
1939 (T : Entity_Id;
1940 Def : Node_Id)
1941 is
1942 Loc : constant Source_Ptr := Sloc (Def);
1943
1944 Base : constant Entity_Id :=
1945 New_Internal_Entity
1946 (E_Decimal_Fixed_Point_Type,
1947 Current_Scope,
1948 Sloc (Defining_Identifier (Parent (Def))), 'G');
1949
1950 Int_Base : constant Entity_Id := Standard_Integer;
1951 Delta_Val : constant Ureal := Ureal_1;
1952 Digs_Val : constant Uint := Uint_6;
1953
1954 function Make_Dummy_Bound return Node_Id;
1955 -- Return a properly typed universal real literal to use as a bound
1956
1957 ----------------------
1958 -- Make_Dummy_Bound --
1959 ----------------------
1960
1961 function Make_Dummy_Bound return Node_Id is
1962 Bound : constant Node_Id := Make_Real_Literal (Loc, Ureal_1);
1963 begin
1964 Set_Etype (Bound, Universal_Real);
1965 return Bound;
1966 end Make_Dummy_Bound;
1967
1968 -- Start of processing for Analyze_Formal_Decimal_Fixed_Point_Type
1969
1970 begin
1971 Enter_Name (T);
1972
1973 Set_Etype (Base, Base);
1974 Set_Size_Info (Base, Int_Base);
1975 Set_RM_Size (Base, RM_Size (Int_Base));
1976 Set_First_Rep_Item (Base, First_Rep_Item (Int_Base));
1977 Set_Digits_Value (Base, Digs_Val);
1978 Set_Delta_Value (Base, Delta_Val);
1979 Set_Small_Value (Base, Delta_Val);
1980 Set_Scalar_Range (Base,
1981 Make_Range (Loc,
1982 Low_Bound => Make_Dummy_Bound,
1983 High_Bound => Make_Dummy_Bound));
1984
1985 Set_Is_Generic_Type (Base);
1986 Set_Parent (Base, Parent (Def));
1987
1988 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
1989 Set_Etype (T, Base);
1990 Set_Size_Info (T, Int_Base);
1991 Set_RM_Size (T, RM_Size (Int_Base));
1992 Set_First_Rep_Item (T, First_Rep_Item (Int_Base));
1993 Set_Digits_Value (T, Digs_Val);
1994 Set_Delta_Value (T, Delta_Val);
1995 Set_Small_Value (T, Delta_Val);
1996 Set_Scalar_Range (T, Scalar_Range (Base));
1997 Set_Is_Constrained (T);
1998
1999 Check_Restriction (No_Fixed_Point, Def);
2000 end Analyze_Formal_Decimal_Fixed_Point_Type;
2001
2002 -------------------------------------------
2003 -- Analyze_Formal_Derived_Interface_Type --
2004 -------------------------------------------
2005
2006 procedure Analyze_Formal_Derived_Interface_Type
2007 (N : Node_Id;
2008 T : Entity_Id;
2009 Def : Node_Id)
2010 is
2011 Loc : constant Source_Ptr := Sloc (Def);
2012
2013 begin
2014 -- Rewrite as a type declaration of a derived type. This ensures that
2015 -- the interface list and primitive operations are properly captured.
2016
2017 Rewrite (N,
2018 Make_Full_Type_Declaration (Loc,
2019 Defining_Identifier => T,
2020 Type_Definition => Def));
2021 Analyze (N);
2022 Set_Is_Generic_Type (T);
2023 end Analyze_Formal_Derived_Interface_Type;
2024
2025 ---------------------------------
2026 -- Analyze_Formal_Derived_Type --
2027 ---------------------------------
2028
2029 procedure Analyze_Formal_Derived_Type
2030 (N : Node_Id;
2031 T : Entity_Id;
2032 Def : Node_Id)
2033 is
2034 Loc : constant Source_Ptr := Sloc (Def);
2035 Unk_Disc : constant Boolean := Unknown_Discriminants_Present (N);
2036 New_N : Node_Id;
2037
2038 begin
2039 Set_Is_Generic_Type (T);
2040
2041 if Private_Present (Def) then
2042 New_N :=
2043 Make_Private_Extension_Declaration (Loc,
2044 Defining_Identifier => T,
2045 Discriminant_Specifications => Discriminant_Specifications (N),
2046 Unknown_Discriminants_Present => Unk_Disc,
2047 Subtype_Indication => Subtype_Mark (Def),
2048 Interface_List => Interface_List (Def));
2049
2050 Set_Abstract_Present (New_N, Abstract_Present (Def));
2051 Set_Limited_Present (New_N, Limited_Present (Def));
2052 Set_Synchronized_Present (New_N, Synchronized_Present (Def));
2053
2054 else
2055 New_N :=
2056 Make_Full_Type_Declaration (Loc,
2057 Defining_Identifier => T,
2058 Discriminant_Specifications =>
2059 Discriminant_Specifications (Parent (T)),
2060 Type_Definition =>
2061 Make_Derived_Type_Definition (Loc,
2062 Subtype_Indication => Subtype_Mark (Def)));
2063
2064 Set_Abstract_Present
2065 (Type_Definition (New_N), Abstract_Present (Def));
2066 Set_Limited_Present
2067 (Type_Definition (New_N), Limited_Present (Def));
2068 end if;
2069
2070 Rewrite (N, New_N);
2071 Analyze (N);
2072
2073 if Unk_Disc then
2074 if not Is_Composite_Type (T) then
2075 Error_Msg_N
2076 ("unknown discriminants not allowed for elementary types", N);
2077 else
2078 Set_Has_Unknown_Discriminants (T);
2079 Set_Is_Constrained (T, False);
2080 end if;
2081 end if;
2082
2083 -- If the parent type has a known size, so does the formal, which makes
2084 -- legal representation clauses that involve the formal.
2085
2086 Set_Size_Known_At_Compile_Time
2087 (T, Size_Known_At_Compile_Time (Entity (Subtype_Mark (Def))));
2088 end Analyze_Formal_Derived_Type;
2089
2090 ----------------------------------
2091 -- Analyze_Formal_Discrete_Type --
2092 ----------------------------------
2093
2094 -- The operations defined for a discrete types are those of an enumeration
2095 -- type. The size is set to an arbitrary value, for use in analyzing the
2096 -- generic unit.
2097
2098 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id) is
2099 Loc : constant Source_Ptr := Sloc (Def);
2100 Lo : Node_Id;
2101 Hi : Node_Id;
2102
2103 Base : constant Entity_Id :=
2104 New_Internal_Entity
2105 (E_Floating_Point_Type, Current_Scope,
2106 Sloc (Defining_Identifier (Parent (Def))), 'G');
2107
2108 begin
2109 Enter_Name (T);
2110 Set_Ekind (T, E_Enumeration_Subtype);
2111 Set_Etype (T, Base);
2112 Init_Size (T, 8);
2113 Init_Alignment (T);
2114 Set_Is_Generic_Type (T);
2115 Set_Is_Constrained (T);
2116
2117 -- For semantic analysis, the bounds of the type must be set to some
2118 -- non-static value. The simplest is to create attribute nodes for those
2119 -- bounds, that refer to the type itself. These bounds are never
2120 -- analyzed but serve as place-holders.
2121
2122 Lo :=
2123 Make_Attribute_Reference (Loc,
2124 Attribute_Name => Name_First,
2125 Prefix => New_Occurrence_Of (T, Loc));
2126 Set_Etype (Lo, T);
2127
2128 Hi :=
2129 Make_Attribute_Reference (Loc,
2130 Attribute_Name => Name_Last,
2131 Prefix => New_Occurrence_Of (T, Loc));
2132 Set_Etype (Hi, T);
2133
2134 Set_Scalar_Range (T,
2135 Make_Range (Loc,
2136 Low_Bound => Lo,
2137 High_Bound => Hi));
2138
2139 Set_Ekind (Base, E_Enumeration_Type);
2140 Set_Etype (Base, Base);
2141 Init_Size (Base, 8);
2142 Init_Alignment (Base);
2143 Set_Is_Generic_Type (Base);
2144 Set_Scalar_Range (Base, Scalar_Range (T));
2145 Set_Parent (Base, Parent (Def));
2146 end Analyze_Formal_Discrete_Type;
2147
2148 ----------------------------------
2149 -- Analyze_Formal_Floating_Type --
2150 ---------------------------------
2151
2152 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id) is
2153 Base : constant Entity_Id :=
2154 New_Internal_Entity
2155 (E_Floating_Point_Type, Current_Scope,
2156 Sloc (Defining_Identifier (Parent (Def))), 'G');
2157
2158 begin
2159 -- The various semantic attributes are taken from the predefined type
2160 -- Float, just so that all of them are initialized. Their values are
2161 -- never used because no constant folding or expansion takes place in
2162 -- the generic itself.
2163
2164 Enter_Name (T);
2165 Set_Ekind (T, E_Floating_Point_Subtype);
2166 Set_Etype (T, Base);
2167 Set_Size_Info (T, (Standard_Float));
2168 Set_RM_Size (T, RM_Size (Standard_Float));
2169 Set_Digits_Value (T, Digits_Value (Standard_Float));
2170 Set_Scalar_Range (T, Scalar_Range (Standard_Float));
2171 Set_Is_Constrained (T);
2172
2173 Set_Is_Generic_Type (Base);
2174 Set_Etype (Base, Base);
2175 Set_Size_Info (Base, (Standard_Float));
2176 Set_RM_Size (Base, RM_Size (Standard_Float));
2177 Set_Digits_Value (Base, Digits_Value (Standard_Float));
2178 Set_Scalar_Range (Base, Scalar_Range (Standard_Float));
2179 Set_Parent (Base, Parent (Def));
2180
2181 Check_Restriction (No_Floating_Point, Def);
2182 end Analyze_Formal_Floating_Type;
2183
2184 -----------------------------------
2185 -- Analyze_Formal_Interface_Type;--
2186 -----------------------------------
2187
2188 procedure Analyze_Formal_Interface_Type
2189 (N : Node_Id;
2190 T : Entity_Id;
2191 Def : Node_Id)
2192 is
2193 Loc : constant Source_Ptr := Sloc (N);
2194 New_N : Node_Id;
2195
2196 begin
2197 New_N :=
2198 Make_Full_Type_Declaration (Loc,
2199 Defining_Identifier => T,
2200 Type_Definition => Def);
2201
2202 Rewrite (N, New_N);
2203 Analyze (N);
2204 Set_Is_Generic_Type (T);
2205 end Analyze_Formal_Interface_Type;
2206
2207 ---------------------------------
2208 -- Analyze_Formal_Modular_Type --
2209 ---------------------------------
2210
2211 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id) is
2212 begin
2213 -- Apart from their entity kind, generic modular types are treated like
2214 -- signed integer types, and have the same attributes.
2215
2216 Analyze_Formal_Signed_Integer_Type (T, Def);
2217 Set_Ekind (T, E_Modular_Integer_Subtype);
2218 Set_Ekind (Etype (T), E_Modular_Integer_Type);
2219
2220 end Analyze_Formal_Modular_Type;
2221
2222 ---------------------------------------
2223 -- Analyze_Formal_Object_Declaration --
2224 ---------------------------------------
2225
2226 procedure Analyze_Formal_Object_Declaration (N : Node_Id) is
2227 E : constant Node_Id := Default_Expression (N);
2228 Id : constant Node_Id := Defining_Identifier (N);
2229 K : Entity_Kind;
2230 T : Node_Id;
2231
2232 begin
2233 Enter_Name (Id);
2234
2235 -- Determine the mode of the formal object
2236
2237 if Out_Present (N) then
2238 K := E_Generic_In_Out_Parameter;
2239
2240 if not In_Present (N) then
2241 Error_Msg_N ("formal generic objects cannot have mode OUT", N);
2242 end if;
2243
2244 else
2245 K := E_Generic_In_Parameter;
2246 end if;
2247
2248 if Present (Subtype_Mark (N)) then
2249 Find_Type (Subtype_Mark (N));
2250 T := Entity (Subtype_Mark (N));
2251
2252 -- Verify that there is no redundant null exclusion
2253
2254 if Null_Exclusion_Present (N) then
2255 if not Is_Access_Type (T) then
2256 Error_Msg_N
2257 ("null exclusion can only apply to an access type", N);
2258
2259 elsif Can_Never_Be_Null (T) then
2260 Error_Msg_NE
2261 ("`NOT NULL` not allowed (& already excludes null)", N, T);
2262 end if;
2263 end if;
2264
2265 -- Ada 2005 (AI-423): Formal object with an access definition
2266
2267 else
2268 Check_Access_Definition (N);
2269 T := Access_Definition
2270 (Related_Nod => N,
2271 N => Access_Definition (N));
2272 end if;
2273
2274 if Ekind (T) = E_Incomplete_Type then
2275 declare
2276 Error_Node : Node_Id;
2277
2278 begin
2279 if Present (Subtype_Mark (N)) then
2280 Error_Node := Subtype_Mark (N);
2281 else
2282 Check_Access_Definition (N);
2283 Error_Node := Access_Definition (N);
2284 end if;
2285
2286 Error_Msg_N ("premature usage of incomplete type", Error_Node);
2287 end;
2288 end if;
2289
2290 if K = E_Generic_In_Parameter then
2291
2292 -- Ada 2005 (AI-287): Limited aggregates allowed in generic formals
2293
2294 if Ada_Version < Ada_2005 and then Is_Limited_Type (T) then
2295 Error_Msg_N
2296 ("generic formal of mode IN must not be of limited type", N);
2297 Explain_Limited_Type (T, N);
2298 end if;
2299
2300 if Is_Abstract_Type (T) then
2301 Error_Msg_N
2302 ("generic formal of mode IN must not be of abstract type", N);
2303 end if;
2304
2305 if Present (E) then
2306 Preanalyze_Spec_Expression (E, T);
2307
2308 if Is_Limited_Type (T) and then not OK_For_Limited_Init (T, E) then
2309 Error_Msg_N
2310 ("initialization not allowed for limited types", E);
2311 Explain_Limited_Type (T, E);
2312 end if;
2313 end if;
2314
2315 Set_Ekind (Id, K);
2316 Set_Etype (Id, T);
2317
2318 -- Case of generic IN OUT parameter
2319
2320 else
2321 -- If the formal has an unconstrained type, construct its actual
2322 -- subtype, as is done for subprogram formals. In this fashion, all
2323 -- its uses can refer to specific bounds.
2324
2325 Set_Ekind (Id, K);
2326 Set_Etype (Id, T);
2327
2328 if (Is_Array_Type (T) and then not Is_Constrained (T))
2329 or else (Ekind (T) = E_Record_Type and then Has_Discriminants (T))
2330 then
2331 declare
2332 Non_Freezing_Ref : constant Node_Id :=
2333 New_Occurrence_Of (Id, Sloc (Id));
2334 Decl : Node_Id;
2335
2336 begin
2337 -- Make sure the actual subtype doesn't generate bogus freezing
2338
2339 Set_Must_Not_Freeze (Non_Freezing_Ref);
2340 Decl := Build_Actual_Subtype (T, Non_Freezing_Ref);
2341 Insert_Before_And_Analyze (N, Decl);
2342 Set_Actual_Subtype (Id, Defining_Identifier (Decl));
2343 end;
2344 else
2345 Set_Actual_Subtype (Id, T);
2346 end if;
2347
2348 if Present (E) then
2349 Error_Msg_N
2350 ("initialization not allowed for `IN OUT` formals", N);
2351 end if;
2352 end if;
2353
2354 if Has_Aspects (N) then
2355 Analyze_Aspect_Specifications (N, Id);
2356 end if;
2357 end Analyze_Formal_Object_Declaration;
2358
2359 ----------------------------------------------
2360 -- Analyze_Formal_Ordinary_Fixed_Point_Type --
2361 ----------------------------------------------
2362
2363 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
2364 (T : Entity_Id;
2365 Def : Node_Id)
2366 is
2367 Loc : constant Source_Ptr := Sloc (Def);
2368 Base : constant Entity_Id :=
2369 New_Internal_Entity
2370 (E_Ordinary_Fixed_Point_Type, Current_Scope,
2371 Sloc (Defining_Identifier (Parent (Def))), 'G');
2372
2373 begin
2374 -- The semantic attributes are set for completeness only, their values
2375 -- will never be used, since all properties of the type are non-static.
2376
2377 Enter_Name (T);
2378 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
2379 Set_Etype (T, Base);
2380 Set_Size_Info (T, Standard_Integer);
2381 Set_RM_Size (T, RM_Size (Standard_Integer));
2382 Set_Small_Value (T, Ureal_1);
2383 Set_Delta_Value (T, Ureal_1);
2384 Set_Scalar_Range (T,
2385 Make_Range (Loc,
2386 Low_Bound => Make_Real_Literal (Loc, Ureal_1),
2387 High_Bound => Make_Real_Literal (Loc, Ureal_1)));
2388 Set_Is_Constrained (T);
2389
2390 Set_Is_Generic_Type (Base);
2391 Set_Etype (Base, Base);
2392 Set_Size_Info (Base, Standard_Integer);
2393 Set_RM_Size (Base, RM_Size (Standard_Integer));
2394 Set_Small_Value (Base, Ureal_1);
2395 Set_Delta_Value (Base, Ureal_1);
2396 Set_Scalar_Range (Base, Scalar_Range (T));
2397 Set_Parent (Base, Parent (Def));
2398
2399 Check_Restriction (No_Fixed_Point, Def);
2400 end Analyze_Formal_Ordinary_Fixed_Point_Type;
2401
2402 ----------------------------------------
2403 -- Analyze_Formal_Package_Declaration --
2404 ----------------------------------------
2405
2406 procedure Analyze_Formal_Package_Declaration (N : Node_Id) is
2407 Gen_Id : constant Node_Id := Name (N);
2408 Loc : constant Source_Ptr := Sloc (N);
2409 Pack_Id : constant Entity_Id := Defining_Identifier (N);
2410 Formal : Entity_Id;
2411 Gen_Decl : Node_Id;
2412 Gen_Unit : Entity_Id;
2413 Renaming : Node_Id;
2414
2415 Vis_Prims_List : Elist_Id := No_Elist;
2416 -- List of primitives made temporarily visible in the instantiation
2417 -- to match the visibility of the formal type.
2418
2419 function Build_Local_Package return Node_Id;
2420 -- The formal package is rewritten so that its parameters are replaced
2421 -- with corresponding declarations. For parameters with bona fide
2422 -- associations these declarations are created by Analyze_Associations
2423 -- as for a regular instantiation. For boxed parameters, we preserve
2424 -- the formal declarations and analyze them, in order to introduce
2425 -- entities of the right kind in the environment of the formal.
2426
2427 -------------------------
2428 -- Build_Local_Package --
2429 -------------------------
2430
2431 function Build_Local_Package return Node_Id is
2432 Decls : List_Id;
2433 Pack_Decl : Node_Id;
2434
2435 begin
2436 -- Within the formal, the name of the generic package is a renaming
2437 -- of the formal (as for a regular instantiation).
2438
2439 Pack_Decl :=
2440 Make_Package_Declaration (Loc,
2441 Specification =>
2442 Copy_Generic_Node
2443 (Specification (Original_Node (Gen_Decl)),
2444 Empty, Instantiating => True));
2445
2446 Renaming :=
2447 Make_Package_Renaming_Declaration (Loc,
2448 Defining_Unit_Name =>
2449 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
2450 Name => New_Occurrence_Of (Formal, Loc));
2451
2452 if Nkind (Gen_Id) = N_Identifier
2453 and then Chars (Gen_Id) = Chars (Pack_Id)
2454 then
2455 Error_Msg_NE
2456 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
2457 end if;
2458
2459 -- If the formal is declared with a box, or with an others choice,
2460 -- create corresponding declarations for all entities in the formal
2461 -- part, so that names with the proper types are available in the
2462 -- specification of the formal package.
2463
2464 -- On the other hand, if there are no associations, then all the
2465 -- formals must have defaults, and this will be checked by the
2466 -- call to Analyze_Associations.
2467
2468 if Box_Present (N)
2469 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2470 then
2471 declare
2472 Formal_Decl : Node_Id;
2473
2474 begin
2475 -- TBA : for a formal package, need to recurse ???
2476
2477 Decls := New_List;
2478 Formal_Decl :=
2479 First
2480 (Generic_Formal_Declarations (Original_Node (Gen_Decl)));
2481 while Present (Formal_Decl) loop
2482 Append_To
2483 (Decls, Copy_Generic_Node (Formal_Decl, Empty, True));
2484 Next (Formal_Decl);
2485 end loop;
2486 end;
2487
2488 -- If generic associations are present, use Analyze_Associations to
2489 -- create the proper renaming declarations.
2490
2491 else
2492 declare
2493 Act_Tree : constant Node_Id :=
2494 Copy_Generic_Node
2495 (Original_Node (Gen_Decl), Empty,
2496 Instantiating => True);
2497
2498 begin
2499 Generic_Renamings.Set_Last (0);
2500 Generic_Renamings_HTable.Reset;
2501 Instantiation_Node := N;
2502
2503 Decls :=
2504 Analyze_Associations
2505 (I_Node => Original_Node (N),
2506 Formals => Generic_Formal_Declarations (Act_Tree),
2507 F_Copy => Generic_Formal_Declarations (Gen_Decl));
2508
2509 Vis_Prims_List := Check_Hidden_Primitives (Decls);
2510 end;
2511 end if;
2512
2513 Append (Renaming, To => Decls);
2514
2515 -- Add generated declarations ahead of local declarations in
2516 -- the package.
2517
2518 if No (Visible_Declarations (Specification (Pack_Decl))) then
2519 Set_Visible_Declarations (Specification (Pack_Decl), Decls);
2520 else
2521 Insert_List_Before
2522 (First (Visible_Declarations (Specification (Pack_Decl))),
2523 Decls);
2524 end if;
2525
2526 return Pack_Decl;
2527 end Build_Local_Package;
2528
2529 -- Local variables
2530
2531 Save_IPSM : constant Boolean := Ignore_Pragma_SPARK_Mode;
2532 -- Save flag Ignore_Pragma_SPARK_Mode for restore on exit
2533
2534 Associations : Boolean := True;
2535 New_N : Node_Id;
2536 Parent_Installed : Boolean := False;
2537 Parent_Instance : Entity_Id;
2538 Renaming_In_Par : Entity_Id;
2539
2540 -- Start of processing for Analyze_Formal_Package_Declaration
2541
2542 begin
2543 Check_Text_IO_Special_Unit (Gen_Id);
2544
2545 Init_Env;
2546 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
2547 Gen_Unit := Entity (Gen_Id);
2548
2549 -- Check for a formal package that is a package renaming
2550
2551 if Present (Renamed_Object (Gen_Unit)) then
2552
2553 -- Indicate that unit is used, before replacing it with renamed
2554 -- entity for use below.
2555
2556 if In_Extended_Main_Source_Unit (N) then
2557 Set_Is_Instantiated (Gen_Unit);
2558 Generate_Reference (Gen_Unit, N);
2559 end if;
2560
2561 Gen_Unit := Renamed_Object (Gen_Unit);
2562 end if;
2563
2564 if Ekind (Gen_Unit) /= E_Generic_Package then
2565 Error_Msg_N ("expect generic package name", Gen_Id);
2566 Restore_Env;
2567 goto Leave;
2568
2569 elsif Gen_Unit = Current_Scope then
2570 Error_Msg_N
2571 ("generic package cannot be used as a formal package of itself",
2572 Gen_Id);
2573 Restore_Env;
2574 goto Leave;
2575
2576 elsif In_Open_Scopes (Gen_Unit) then
2577 if Is_Compilation_Unit (Gen_Unit)
2578 and then Is_Child_Unit (Current_Scope)
2579 then
2580 -- Special-case the error when the formal is a parent, and
2581 -- continue analysis to minimize cascaded errors.
2582
2583 Error_Msg_N
2584 ("generic parent cannot be used as formal package "
2585 & "of a child unit", Gen_Id);
2586
2587 else
2588 Error_Msg_N
2589 ("generic package cannot be used as a formal package "
2590 & "within itself", Gen_Id);
2591 Restore_Env;
2592 goto Leave;
2593 end if;
2594 end if;
2595
2596 -- Check that name of formal package does not hide name of generic,
2597 -- or its leading prefix. This check must be done separately because
2598 -- the name of the generic has already been analyzed.
2599
2600 declare
2601 Gen_Name : Entity_Id;
2602
2603 begin
2604 Gen_Name := Gen_Id;
2605 while Nkind (Gen_Name) = N_Expanded_Name loop
2606 Gen_Name := Prefix (Gen_Name);
2607 end loop;
2608
2609 if Chars (Gen_Name) = Chars (Pack_Id) then
2610 Error_Msg_NE
2611 ("& is hidden within declaration of formal package",
2612 Gen_Id, Gen_Name);
2613 end if;
2614 end;
2615
2616 if Box_Present (N)
2617 or else No (Generic_Associations (N))
2618 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2619 then
2620 Associations := False;
2621 end if;
2622
2623 -- If there are no generic associations, the generic parameters appear
2624 -- as local entities and are instantiated like them. We copy the generic
2625 -- package declaration as if it were an instantiation, and analyze it
2626 -- like a regular package, except that we treat the formals as
2627 -- additional visible components.
2628
2629 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
2630
2631 if In_Extended_Main_Source_Unit (N) then
2632 Set_Is_Instantiated (Gen_Unit);
2633 Generate_Reference (Gen_Unit, N);
2634 end if;
2635
2636 Formal := New_Copy (Pack_Id);
2637 Create_Instantiation_Source (N, Gen_Unit, False, S_Adjustment);
2638
2639 -- Make local generic without formals. The formals will be replaced with
2640 -- internal declarations.
2641
2642 begin
2643 New_N := Build_Local_Package;
2644
2645 -- If there are errors in the parameter list, Analyze_Associations
2646 -- raises Instantiation_Error. Patch the declaration to prevent further
2647 -- exception propagation.
2648
2649 exception
2650 when Instantiation_Error =>
2651 Enter_Name (Formal);
2652 Set_Ekind (Formal, E_Variable);
2653 Set_Etype (Formal, Any_Type);
2654 Restore_Hidden_Primitives (Vis_Prims_List);
2655
2656 if Parent_Installed then
2657 Remove_Parent;
2658 end if;
2659
2660 goto Leave;
2661 end;
2662
2663 Rewrite (N, New_N);
2664 Set_Defining_Unit_Name (Specification (New_N), Formal);
2665 Set_Generic_Parent (Specification (N), Gen_Unit);
2666 Set_Instance_Env (Gen_Unit, Formal);
2667 Set_Is_Generic_Instance (Formal);
2668
2669 Enter_Name (Formal);
2670 Set_Ekind (Formal, E_Package);
2671 Set_Etype (Formal, Standard_Void_Type);
2672 Set_Inner_Instances (Formal, New_Elmt_List);
2673 Push_Scope (Formal);
2674
2675 -- Manually set the SPARK_Mode from the context because the package
2676 -- declaration is never analyzed.
2677
2678 Set_SPARK_Pragma (Formal, SPARK_Mode_Pragma);
2679 Set_SPARK_Aux_Pragma (Formal, SPARK_Mode_Pragma);
2680 Set_SPARK_Pragma_Inherited (Formal);
2681 Set_SPARK_Aux_Pragma_Inherited (Formal);
2682
2683 if Is_Child_Unit (Gen_Unit) and then Parent_Installed then
2684
2685 -- Similarly, we have to make the name of the formal visible in the
2686 -- parent instance, to resolve properly fully qualified names that
2687 -- may appear in the generic unit. The parent instance has been
2688 -- placed on the scope stack ahead of the current scope.
2689
2690 Parent_Instance := Scope_Stack.Table (Scope_Stack.Last - 1).Entity;
2691
2692 Renaming_In_Par :=
2693 Make_Defining_Identifier (Loc, Chars (Gen_Unit));
2694 Set_Ekind (Renaming_In_Par, E_Package);
2695 Set_Etype (Renaming_In_Par, Standard_Void_Type);
2696 Set_Scope (Renaming_In_Par, Parent_Instance);
2697 Set_Parent (Renaming_In_Par, Parent (Formal));
2698 Set_Renamed_Object (Renaming_In_Par, Formal);
2699 Append_Entity (Renaming_In_Par, Parent_Instance);
2700 end if;
2701
2702 -- A formal package declaration behaves as a package instantiation with
2703 -- respect to SPARK_Mode "off". If the annotation is "off" or altogether
2704 -- missing, set the global flag which signals Analyze_Pragma to ingnore
2705 -- all SPARK_Mode pragmas within the generic_package_name.
2706
2707 if SPARK_Mode /= On then
2708 Ignore_Pragma_SPARK_Mode := True;
2709 end if;
2710
2711 Analyze (Specification (N));
2712
2713 -- The formals for which associations are provided are not visible
2714 -- outside of the formal package. The others are still declared by a
2715 -- formal parameter declaration.
2716
2717 -- If there are no associations, the only local entity to hide is the
2718 -- generated package renaming itself.
2719
2720 declare
2721 E : Entity_Id;
2722
2723 begin
2724 E := First_Entity (Formal);
2725 while Present (E) loop
2726 if Associations and then not Is_Generic_Formal (E) then
2727 Set_Is_Hidden (E);
2728 end if;
2729
2730 if Ekind (E) = E_Package and then Renamed_Entity (E) = Formal then
2731 Set_Is_Hidden (E);
2732 exit;
2733 end if;
2734
2735 Next_Entity (E);
2736 end loop;
2737 end;
2738
2739 End_Package_Scope (Formal);
2740 Restore_Hidden_Primitives (Vis_Prims_List);
2741
2742 if Parent_Installed then
2743 Remove_Parent;
2744 end if;
2745
2746 Restore_Env;
2747
2748 -- Inside the generic unit, the formal package is a regular package, but
2749 -- no body is needed for it. Note that after instantiation, the defining
2750 -- unit name we need is in the new tree and not in the original (see
2751 -- Package_Instantiation). A generic formal package is an instance, and
2752 -- can be used as an actual for an inner instance.
2753
2754 Set_Has_Completion (Formal, True);
2755
2756 -- Add semantic information to the original defining identifier for ASIS
2757 -- use.
2758
2759 Set_Ekind (Pack_Id, E_Package);
2760 Set_Etype (Pack_Id, Standard_Void_Type);
2761 Set_Scope (Pack_Id, Scope (Formal));
2762 Set_Has_Completion (Pack_Id, True);
2763
2764 <<Leave>>
2765 if Has_Aspects (N) then
2766 Analyze_Aspect_Specifications (N, Pack_Id);
2767 end if;
2768
2769 Ignore_Pragma_SPARK_Mode := Save_IPSM;
2770 end Analyze_Formal_Package_Declaration;
2771
2772 ---------------------------------
2773 -- Analyze_Formal_Private_Type --
2774 ---------------------------------
2775
2776 procedure Analyze_Formal_Private_Type
2777 (N : Node_Id;
2778 T : Entity_Id;
2779 Def : Node_Id)
2780 is
2781 begin
2782 New_Private_Type (N, T, Def);
2783
2784 -- Set the size to an arbitrary but legal value
2785
2786 Set_Size_Info (T, Standard_Integer);
2787 Set_RM_Size (T, RM_Size (Standard_Integer));
2788 end Analyze_Formal_Private_Type;
2789
2790 ------------------------------------
2791 -- Analyze_Formal_Incomplete_Type --
2792 ------------------------------------
2793
2794 procedure Analyze_Formal_Incomplete_Type
2795 (T : Entity_Id;
2796 Def : Node_Id)
2797 is
2798 begin
2799 Enter_Name (T);
2800 Set_Ekind (T, E_Incomplete_Type);
2801 Set_Etype (T, T);
2802 Set_Private_Dependents (T, New_Elmt_List);
2803
2804 if Tagged_Present (Def) then
2805 Set_Is_Tagged_Type (T);
2806 Make_Class_Wide_Type (T);
2807 Set_Direct_Primitive_Operations (T, New_Elmt_List);
2808 end if;
2809 end Analyze_Formal_Incomplete_Type;
2810
2811 ----------------------------------------
2812 -- Analyze_Formal_Signed_Integer_Type --
2813 ----------------------------------------
2814
2815 procedure Analyze_Formal_Signed_Integer_Type
2816 (T : Entity_Id;
2817 Def : Node_Id)
2818 is
2819 Base : constant Entity_Id :=
2820 New_Internal_Entity
2821 (E_Signed_Integer_Type,
2822 Current_Scope,
2823 Sloc (Defining_Identifier (Parent (Def))), 'G');
2824
2825 begin
2826 Enter_Name (T);
2827
2828 Set_Ekind (T, E_Signed_Integer_Subtype);
2829 Set_Etype (T, Base);
2830 Set_Size_Info (T, Standard_Integer);
2831 Set_RM_Size (T, RM_Size (Standard_Integer));
2832 Set_Scalar_Range (T, Scalar_Range (Standard_Integer));
2833 Set_Is_Constrained (T);
2834
2835 Set_Is_Generic_Type (Base);
2836 Set_Size_Info (Base, Standard_Integer);
2837 Set_RM_Size (Base, RM_Size (Standard_Integer));
2838 Set_Etype (Base, Base);
2839 Set_Scalar_Range (Base, Scalar_Range (Standard_Integer));
2840 Set_Parent (Base, Parent (Def));
2841 end Analyze_Formal_Signed_Integer_Type;
2842
2843 -------------------------------------------
2844 -- Analyze_Formal_Subprogram_Declaration --
2845 -------------------------------------------
2846
2847 procedure Analyze_Formal_Subprogram_Declaration (N : Node_Id) is
2848 Spec : constant Node_Id := Specification (N);
2849 Def : constant Node_Id := Default_Name (N);
2850 Nam : constant Entity_Id := Defining_Unit_Name (Spec);
2851 Subp : Entity_Id;
2852
2853 begin
2854 if Nam = Error then
2855 return;
2856 end if;
2857
2858 if Nkind (Nam) = N_Defining_Program_Unit_Name then
2859 Error_Msg_N ("name of formal subprogram must be a direct name", Nam);
2860 goto Leave;
2861 end if;
2862
2863 Analyze_Subprogram_Declaration (N);
2864 Set_Is_Formal_Subprogram (Nam);
2865 Set_Has_Completion (Nam);
2866
2867 if Nkind (N) = N_Formal_Abstract_Subprogram_Declaration then
2868 Set_Is_Abstract_Subprogram (Nam);
2869
2870 Set_Is_Dispatching_Operation (Nam);
2871
2872 -- A formal abstract procedure cannot have a null default
2873 -- (RM 12.6(4.1/2)).
2874
2875 if Nkind (Spec) = N_Procedure_Specification
2876 and then Null_Present (Spec)
2877 then
2878 Error_Msg_N
2879 ("a formal abstract subprogram cannot default to null", Spec);
2880 end if;
2881
2882 declare
2883 Ctrl_Type : constant Entity_Id := Find_Dispatching_Type (Nam);
2884 begin
2885 if No (Ctrl_Type) then
2886 Error_Msg_N
2887 ("abstract formal subprogram must have a controlling type",
2888 N);
2889
2890 elsif Ada_Version >= Ada_2012
2891 and then Is_Incomplete_Type (Ctrl_Type)
2892 then
2893 Error_Msg_NE
2894 ("controlling type of abstract formal subprogram cannot "
2895 & "be incomplete type", N, Ctrl_Type);
2896
2897 else
2898 Check_Controlling_Formals (Ctrl_Type, Nam);
2899 end if;
2900 end;
2901 end if;
2902
2903 -- Default name is resolved at the point of instantiation
2904
2905 if Box_Present (N) then
2906 null;
2907
2908 -- Else default is bound at the point of generic declaration
2909
2910 elsif Present (Def) then
2911 if Nkind (Def) = N_Operator_Symbol then
2912 Find_Direct_Name (Def);
2913
2914 elsif Nkind (Def) /= N_Attribute_Reference then
2915 Analyze (Def);
2916
2917 else
2918 -- For an attribute reference, analyze the prefix and verify
2919 -- that it has the proper profile for the subprogram.
2920
2921 Analyze (Prefix (Def));
2922 Valid_Default_Attribute (Nam, Def);
2923 goto Leave;
2924 end if;
2925
2926 -- Default name may be overloaded, in which case the interpretation
2927 -- with the correct profile must be selected, as for a renaming.
2928 -- If the definition is an indexed component, it must denote a
2929 -- member of an entry family. If it is a selected component, it
2930 -- can be a protected operation.
2931
2932 if Etype (Def) = Any_Type then
2933 goto Leave;
2934
2935 elsif Nkind (Def) = N_Selected_Component then
2936 if not Is_Overloadable (Entity (Selector_Name (Def))) then
2937 Error_Msg_N ("expect valid subprogram name as default", Def);
2938 end if;
2939
2940 elsif Nkind (Def) = N_Indexed_Component then
2941 if Is_Entity_Name (Prefix (Def)) then
2942 if Ekind (Entity (Prefix (Def))) /= E_Entry_Family then
2943 Error_Msg_N ("expect valid subprogram name as default", Def);
2944 end if;
2945
2946 elsif Nkind (Prefix (Def)) = N_Selected_Component then
2947 if Ekind (Entity (Selector_Name (Prefix (Def)))) /=
2948 E_Entry_Family
2949 then
2950 Error_Msg_N ("expect valid subprogram name as default", Def);
2951 end if;
2952
2953 else
2954 Error_Msg_N ("expect valid subprogram name as default", Def);
2955 goto Leave;
2956 end if;
2957
2958 elsif Nkind (Def) = N_Character_Literal then
2959
2960 -- Needs some type checks: subprogram should be parameterless???
2961
2962 Resolve (Def, (Etype (Nam)));
2963
2964 elsif not Is_Entity_Name (Def)
2965 or else not Is_Overloadable (Entity (Def))
2966 then
2967 Error_Msg_N ("expect valid subprogram name as default", Def);
2968 goto Leave;
2969
2970 elsif not Is_Overloaded (Def) then
2971 Subp := Entity (Def);
2972
2973 if Subp = Nam then
2974 Error_Msg_N ("premature usage of formal subprogram", Def);
2975
2976 elsif not Entity_Matches_Spec (Subp, Nam) then
2977 Error_Msg_N ("no visible entity matches specification", Def);
2978 end if;
2979
2980 -- More than one interpretation, so disambiguate as for a renaming
2981
2982 else
2983 declare
2984 I : Interp_Index;
2985 I1 : Interp_Index := 0;
2986 It : Interp;
2987 It1 : Interp;
2988
2989 begin
2990 Subp := Any_Id;
2991 Get_First_Interp (Def, I, It);
2992 while Present (It.Nam) loop
2993 if Entity_Matches_Spec (It.Nam, Nam) then
2994 if Subp /= Any_Id then
2995 It1 := Disambiguate (Def, I1, I, Etype (Subp));
2996
2997 if It1 = No_Interp then
2998 Error_Msg_N ("ambiguous default subprogram", Def);
2999 else
3000 Subp := It1.Nam;
3001 end if;
3002
3003 exit;
3004
3005 else
3006 I1 := I;
3007 Subp := It.Nam;
3008 end if;
3009 end if;
3010
3011 Get_Next_Interp (I, It);
3012 end loop;
3013 end;
3014
3015 if Subp /= Any_Id then
3016
3017 -- Subprogram found, generate reference to it
3018
3019 Set_Entity (Def, Subp);
3020 Generate_Reference (Subp, Def);
3021
3022 if Subp = Nam then
3023 Error_Msg_N ("premature usage of formal subprogram", Def);
3024
3025 elsif Ekind (Subp) /= E_Operator then
3026 Check_Mode_Conformant (Subp, Nam);
3027 end if;
3028
3029 else
3030 Error_Msg_N ("no visible subprogram matches specification", N);
3031 end if;
3032 end if;
3033 end if;
3034
3035 <<Leave>>
3036 if Has_Aspects (N) then
3037 Analyze_Aspect_Specifications (N, Nam);
3038 end if;
3039
3040 end Analyze_Formal_Subprogram_Declaration;
3041
3042 -------------------------------------
3043 -- Analyze_Formal_Type_Declaration --
3044 -------------------------------------
3045
3046 procedure Analyze_Formal_Type_Declaration (N : Node_Id) is
3047 Def : constant Node_Id := Formal_Type_Definition (N);
3048 T : Entity_Id;
3049
3050 begin
3051 T := Defining_Identifier (N);
3052
3053 if Present (Discriminant_Specifications (N))
3054 and then Nkind (Def) /= N_Formal_Private_Type_Definition
3055 then
3056 Error_Msg_N
3057 ("discriminants not allowed for this formal type", T);
3058 end if;
3059
3060 -- Enter the new name, and branch to specific routine
3061
3062 case Nkind (Def) is
3063 when N_Formal_Private_Type_Definition =>
3064 Analyze_Formal_Private_Type (N, T, Def);
3065
3066 when N_Formal_Derived_Type_Definition =>
3067 Analyze_Formal_Derived_Type (N, T, Def);
3068
3069 when N_Formal_Incomplete_Type_Definition =>
3070 Analyze_Formal_Incomplete_Type (T, Def);
3071
3072 when N_Formal_Discrete_Type_Definition =>
3073 Analyze_Formal_Discrete_Type (T, Def);
3074
3075 when N_Formal_Signed_Integer_Type_Definition =>
3076 Analyze_Formal_Signed_Integer_Type (T, Def);
3077
3078 when N_Formal_Modular_Type_Definition =>
3079 Analyze_Formal_Modular_Type (T, Def);
3080
3081 when N_Formal_Floating_Point_Definition =>
3082 Analyze_Formal_Floating_Type (T, Def);
3083
3084 when N_Formal_Ordinary_Fixed_Point_Definition =>
3085 Analyze_Formal_Ordinary_Fixed_Point_Type (T, Def);
3086
3087 when N_Formal_Decimal_Fixed_Point_Definition =>
3088 Analyze_Formal_Decimal_Fixed_Point_Type (T, Def);
3089
3090 when N_Array_Type_Definition =>
3091 Analyze_Formal_Array_Type (T, Def);
3092
3093 when N_Access_To_Object_Definition |
3094 N_Access_Function_Definition |
3095 N_Access_Procedure_Definition =>
3096 Analyze_Generic_Access_Type (T, Def);
3097
3098 -- Ada 2005: a interface declaration is encoded as an abstract
3099 -- record declaration or a abstract type derivation.
3100
3101 when N_Record_Definition =>
3102 Analyze_Formal_Interface_Type (N, T, Def);
3103
3104 when N_Derived_Type_Definition =>
3105 Analyze_Formal_Derived_Interface_Type (N, T, Def);
3106
3107 when N_Error =>
3108 null;
3109
3110 when others =>
3111 raise Program_Error;
3112
3113 end case;
3114
3115 Set_Is_Generic_Type (T);
3116
3117 if Has_Aspects (N) then
3118 Analyze_Aspect_Specifications (N, T);
3119 end if;
3120 end Analyze_Formal_Type_Declaration;
3121
3122 ------------------------------------
3123 -- Analyze_Function_Instantiation --
3124 ------------------------------------
3125
3126 procedure Analyze_Function_Instantiation (N : Node_Id) is
3127 begin
3128 Analyze_Subprogram_Instantiation (N, E_Function);
3129 end Analyze_Function_Instantiation;
3130
3131 ---------------------------------
3132 -- Analyze_Generic_Access_Type --
3133 ---------------------------------
3134
3135 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id) is
3136 begin
3137 Enter_Name (T);
3138
3139 if Nkind (Def) = N_Access_To_Object_Definition then
3140 Access_Type_Declaration (T, Def);
3141
3142 if Is_Incomplete_Or_Private_Type (Designated_Type (T))
3143 and then No (Full_View (Designated_Type (T)))
3144 and then not Is_Generic_Type (Designated_Type (T))
3145 then
3146 Error_Msg_N ("premature usage of incomplete type", Def);
3147
3148 elsif not Is_Entity_Name (Subtype_Indication (Def)) then
3149 Error_Msg_N
3150 ("only a subtype mark is allowed in a formal", Def);
3151 end if;
3152
3153 else
3154 Access_Subprogram_Declaration (T, Def);
3155 end if;
3156 end Analyze_Generic_Access_Type;
3157
3158 ---------------------------------
3159 -- Analyze_Generic_Formal_Part --
3160 ---------------------------------
3161
3162 procedure Analyze_Generic_Formal_Part (N : Node_Id) is
3163 Gen_Parm_Decl : Node_Id;
3164
3165 begin
3166 -- The generic formals are processed in the scope of the generic unit,
3167 -- where they are immediately visible. The scope is installed by the
3168 -- caller.
3169
3170 Gen_Parm_Decl := First (Generic_Formal_Declarations (N));
3171 while Present (Gen_Parm_Decl) loop
3172 Analyze (Gen_Parm_Decl);
3173 Next (Gen_Parm_Decl);
3174 end loop;
3175
3176 Generate_Reference_To_Generic_Formals (Current_Scope);
3177 end Analyze_Generic_Formal_Part;
3178
3179 ------------------------------------------
3180 -- Analyze_Generic_Package_Declaration --
3181 ------------------------------------------
3182
3183 procedure Analyze_Generic_Package_Declaration (N : Node_Id) is
3184 Loc : constant Source_Ptr := Sloc (N);
3185 Decls : constant List_Id :=
3186 Visible_Declarations (Specification (N));
3187 Decl : Node_Id;
3188 Id : Entity_Id;
3189 New_N : Node_Id;
3190 Renaming : Node_Id;
3191 Save_Parent : Node_Id;
3192
3193 begin
3194 Check_SPARK_05_Restriction ("generic is not allowed", N);
3195
3196 -- We introduce a renaming of the enclosing package, to have a usable
3197 -- entity as the prefix of an expanded name for a local entity of the
3198 -- form Par.P.Q, where P is the generic package. This is because a local
3199 -- entity named P may hide it, so that the usual visibility rules in
3200 -- the instance will not resolve properly.
3201
3202 Renaming :=
3203 Make_Package_Renaming_Declaration (Loc,
3204 Defining_Unit_Name =>
3205 Make_Defining_Identifier (Loc,
3206 Chars => New_External_Name (Chars (Defining_Entity (N)), "GH")),
3207 Name =>
3208 Make_Identifier (Loc, Chars (Defining_Entity (N))));
3209
3210 if Present (Decls) then
3211 Decl := First (Decls);
3212 while Present (Decl) and then Nkind (Decl) = N_Pragma loop
3213 Next (Decl);
3214 end loop;
3215
3216 if Present (Decl) then
3217 Insert_Before (Decl, Renaming);
3218 else
3219 Append (Renaming, Visible_Declarations (Specification (N)));
3220 end if;
3221
3222 else
3223 Set_Visible_Declarations (Specification (N), New_List (Renaming));
3224 end if;
3225
3226 -- Create copy of generic unit, and save for instantiation. If the unit
3227 -- is a child unit, do not copy the specifications for the parent, which
3228 -- are not part of the generic tree.
3229
3230 Save_Parent := Parent_Spec (N);
3231 Set_Parent_Spec (N, Empty);
3232
3233 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
3234 Set_Parent_Spec (New_N, Save_Parent);
3235 Rewrite (N, New_N);
3236
3237 -- Once the contents of the generic copy and the template are swapped,
3238 -- do the same for their respective aspect specifications.
3239
3240 Exchange_Aspects (N, New_N);
3241
3242 -- Collect all contract-related source pragmas found within the template
3243 -- and attach them to the contract of the package spec. This contract is
3244 -- used in the capture of global references within annotations.
3245
3246 Create_Generic_Contract (N);
3247
3248 Id := Defining_Entity (N);
3249 Generate_Definition (Id);
3250
3251 -- Expansion is not applied to generic units
3252
3253 Start_Generic;
3254
3255 Enter_Name (Id);
3256 Set_Ekind (Id, E_Generic_Package);
3257 Set_Etype (Id, Standard_Void_Type);
3258
3259 -- A generic package declared within a Ghost region is rendered Ghost
3260 -- (SPARK RM 6.9(2)).
3261
3262 if Ghost_Mode > None then
3263 Set_Is_Ghost_Entity (Id);
3264 end if;
3265
3266 -- Analyze aspects now, so that generated pragmas appear in the
3267 -- declarations before building and analyzing the generic copy.
3268
3269 if Has_Aspects (N) then
3270 Analyze_Aspect_Specifications (N, Id);
3271 end if;
3272
3273 Push_Scope (Id);
3274 Enter_Generic_Scope (Id);
3275 Set_Inner_Instances (Id, New_Elmt_List);
3276
3277 Set_Categorization_From_Pragmas (N);
3278 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3279
3280 -- Link the declaration of the generic homonym in the generic copy to
3281 -- the package it renames, so that it is always resolved properly.
3282
3283 Set_Generic_Homonym (Id, Defining_Unit_Name (Renaming));
3284 Set_Entity (Associated_Node (Name (Renaming)), Id);
3285
3286 -- For a library unit, we have reconstructed the entity for the unit,
3287 -- and must reset it in the library tables.
3288
3289 if Nkind (Parent (N)) = N_Compilation_Unit then
3290 Set_Cunit_Entity (Current_Sem_Unit, Id);
3291 end if;
3292
3293 Analyze_Generic_Formal_Part (N);
3294
3295 -- After processing the generic formals, analysis proceeds as for a
3296 -- non-generic package.
3297
3298 Analyze (Specification (N));
3299
3300 Validate_Categorization_Dependency (N, Id);
3301
3302 End_Generic;
3303
3304 End_Package_Scope (Id);
3305 Exit_Generic_Scope (Id);
3306
3307 if Nkind (Parent (N)) /= N_Compilation_Unit then
3308 Move_Freeze_Nodes (Id, N, Visible_Declarations (Specification (N)));
3309 Move_Freeze_Nodes (Id, N, Private_Declarations (Specification (N)));
3310 Move_Freeze_Nodes (Id, N, Generic_Formal_Declarations (N));
3311
3312 else
3313 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
3314 Validate_RT_RAT_Component (N);
3315
3316 -- If this is a spec without a body, check that generic parameters
3317 -- are referenced.
3318
3319 if not Body_Required (Parent (N)) then
3320 Check_References (Id);
3321 end if;
3322 end if;
3323
3324 -- If there is a specified storage pool in the context, create an
3325 -- aspect on the package declaration, so that it is used in any
3326 -- instance that does not override it.
3327
3328 if Present (Default_Pool) then
3329 declare
3330 ASN : Node_Id;
3331
3332 begin
3333 ASN :=
3334 Make_Aspect_Specification (Loc,
3335 Identifier => Make_Identifier (Loc, Name_Default_Storage_Pool),
3336 Expression => New_Copy (Default_Pool));
3337
3338 if No (Aspect_Specifications (Specification (N))) then
3339 Set_Aspect_Specifications (Specification (N), New_List (ASN));
3340 else
3341 Append (ASN, Aspect_Specifications (Specification (N)));
3342 end if;
3343 end;
3344 end if;
3345 end Analyze_Generic_Package_Declaration;
3346
3347 --------------------------------------------
3348 -- Analyze_Generic_Subprogram_Declaration --
3349 --------------------------------------------
3350
3351 procedure Analyze_Generic_Subprogram_Declaration (N : Node_Id) is
3352 Formals : List_Id;
3353 Id : Entity_Id;
3354 New_N : Node_Id;
3355 Result_Type : Entity_Id;
3356 Save_Parent : Node_Id;
3357 Spec : Node_Id;
3358 Typ : Entity_Id;
3359
3360 begin
3361 Check_SPARK_05_Restriction ("generic is not allowed", N);
3362
3363 -- Create copy of generic unit, and save for instantiation. If the unit
3364 -- is a child unit, do not copy the specifications for the parent, which
3365 -- are not part of the generic tree.
3366
3367 Save_Parent := Parent_Spec (N);
3368 Set_Parent_Spec (N, Empty);
3369
3370 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
3371 Set_Parent_Spec (New_N, Save_Parent);
3372 Rewrite (N, New_N);
3373
3374 -- Once the contents of the generic copy and the template are swapped,
3375 -- do the same for their respective aspect specifications.
3376
3377 Exchange_Aspects (N, New_N);
3378
3379 -- Collect all contract-related source pragmas found within the template
3380 -- and attach them to the contract of the subprogram spec. This contract
3381 -- is used in the capture of global references within annotations.
3382
3383 Create_Generic_Contract (N);
3384
3385 Spec := Specification (N);
3386 Id := Defining_Entity (Spec);
3387 Generate_Definition (Id);
3388
3389 if Nkind (Id) = N_Defining_Operator_Symbol then
3390 Error_Msg_N
3391 ("operator symbol not allowed for generic subprogram", Id);
3392 end if;
3393
3394 Start_Generic;
3395
3396 Enter_Name (Id);
3397 Set_Scope_Depth_Value (Id, Scope_Depth (Current_Scope) + 1);
3398
3399 -- Analyze the aspects of the generic copy to ensure that all generated
3400 -- pragmas (if any) perform their semantic effects.
3401
3402 if Has_Aspects (N) then
3403 Analyze_Aspect_Specifications (N, Id);
3404 end if;
3405
3406 Push_Scope (Id);
3407 Enter_Generic_Scope (Id);
3408 Set_Inner_Instances (Id, New_Elmt_List);
3409 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3410
3411 Analyze_Generic_Formal_Part (N);
3412
3413 Formals := Parameter_Specifications (Spec);
3414
3415 if Nkind (Spec) = N_Function_Specification then
3416 Set_Ekind (Id, E_Generic_Function);
3417 else
3418 Set_Ekind (Id, E_Generic_Procedure);
3419 end if;
3420
3421 if Present (Formals) then
3422 Process_Formals (Formals, Spec);
3423 end if;
3424
3425 if Nkind (Spec) = N_Function_Specification then
3426 if Nkind (Result_Definition (Spec)) = N_Access_Definition then
3427 Result_Type := Access_Definition (Spec, Result_Definition (Spec));
3428 Set_Etype (Id, Result_Type);
3429
3430 -- Check restriction imposed by AI05-073: a generic function
3431 -- cannot return an abstract type or an access to such.
3432
3433 -- This is a binding interpretation should it apply to earlier
3434 -- versions of Ada as well as Ada 2012???
3435
3436 if Is_Abstract_Type (Designated_Type (Result_Type))
3437 and then Ada_Version >= Ada_2012
3438 then
3439 Error_Msg_N
3440 ("generic function cannot have an access result "
3441 & "that designates an abstract type", Spec);
3442 end if;
3443
3444 else
3445 Find_Type (Result_Definition (Spec));
3446 Typ := Entity (Result_Definition (Spec));
3447
3448 if Is_Abstract_Type (Typ)
3449 and then Ada_Version >= Ada_2012
3450 then
3451 Error_Msg_N
3452 ("generic function cannot have abstract result type", Spec);
3453 end if;
3454
3455 -- If a null exclusion is imposed on the result type, then create
3456 -- a null-excluding itype (an access subtype) and use it as the
3457 -- function's Etype.
3458
3459 if Is_Access_Type (Typ)
3460 and then Null_Exclusion_Present (Spec)
3461 then
3462 Set_Etype (Id,
3463 Create_Null_Excluding_Itype
3464 (T => Typ,
3465 Related_Nod => Spec,
3466 Scope_Id => Defining_Unit_Name (Spec)));
3467 else
3468 Set_Etype (Id, Typ);
3469 end if;
3470 end if;
3471
3472 else
3473 Set_Etype (Id, Standard_Void_Type);
3474 end if;
3475
3476 -- A generic subprogram declared within a Ghost region is rendered Ghost
3477 -- (SPARK RM 6.9(2)).
3478
3479 if Ghost_Mode > None then
3480 Set_Is_Ghost_Entity (Id);
3481 end if;
3482
3483 -- For a library unit, we have reconstructed the entity for the unit,
3484 -- and must reset it in the library tables. We also make sure that
3485 -- Body_Required is set properly in the original compilation unit node.
3486
3487 if Nkind (Parent (N)) = N_Compilation_Unit then
3488 Set_Cunit_Entity (Current_Sem_Unit, Id);
3489 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
3490 end if;
3491
3492 Set_Categorization_From_Pragmas (N);
3493 Validate_Categorization_Dependency (N, Id);
3494
3495 -- Capture all global references that occur within the profile of the
3496 -- generic subprogram. Aspects are not part of this processing because
3497 -- they must be delayed. If processed now, Save_Global_References will
3498 -- destroy the Associated_Node links and prevent the capture of global
3499 -- references when the contract of the generic subprogram is analyzed.
3500
3501 Save_Global_References (Original_Node (N));
3502
3503 End_Generic;
3504 End_Scope;
3505 Exit_Generic_Scope (Id);
3506 Generate_Reference_To_Formals (Id);
3507
3508 List_Inherited_Pre_Post_Aspects (Id);
3509 end Analyze_Generic_Subprogram_Declaration;
3510
3511 -----------------------------------
3512 -- Analyze_Package_Instantiation --
3513 -----------------------------------
3514
3515 procedure Analyze_Package_Instantiation (N : Node_Id) is
3516 Loc : constant Source_Ptr := Sloc (N);
3517 Gen_Id : constant Node_Id := Name (N);
3518
3519 Act_Decl : Node_Id;
3520 Act_Decl_Name : Node_Id;
3521 Act_Decl_Id : Entity_Id;
3522 Act_Spec : Node_Id;
3523 Act_Tree : Node_Id;
3524
3525 Gen_Decl : Node_Id;
3526 Gen_Spec : Node_Id;
3527 Gen_Unit : Entity_Id;
3528
3529 Is_Actual_Pack : constant Boolean :=
3530 Is_Internal (Defining_Entity (N));
3531
3532 Env_Installed : Boolean := False;
3533 Parent_Installed : Boolean := False;
3534 Renaming_List : List_Id;
3535 Unit_Renaming : Node_Id;
3536 Needs_Body : Boolean;
3537 Inline_Now : Boolean := False;
3538 Has_Inline_Always : Boolean := False;
3539
3540 Save_IPSM : constant Boolean := Ignore_Pragma_SPARK_Mode;
3541 -- Save flag Ignore_Pragma_SPARK_Mode for restore on exit
3542
3543 Save_SM : constant SPARK_Mode_Type := SPARK_Mode;
3544 Save_SMP : constant Node_Id := SPARK_Mode_Pragma;
3545 -- Save the SPARK_Mode-related data for restore on exit
3546
3547 Save_Style_Check : constant Boolean := Style_Check;
3548 -- Save style check mode for restore on exit
3549
3550 procedure Delay_Descriptors (E : Entity_Id);
3551 -- Delay generation of subprogram descriptors for given entity
3552
3553 function Might_Inline_Subp return Boolean;
3554 -- If inlining is active and the generic contains inlined subprograms,
3555 -- we instantiate the body. This may cause superfluous instantiations,
3556 -- but it is simpler than detecting the need for the body at the point
3557 -- of inlining, when the context of the instance is not available.
3558
3559 -----------------------
3560 -- Delay_Descriptors --
3561 -----------------------
3562
3563 procedure Delay_Descriptors (E : Entity_Id) is
3564 begin
3565 if not Delay_Subprogram_Descriptors (E) then
3566 Set_Delay_Subprogram_Descriptors (E);
3567 Pending_Descriptor.Append (E);
3568 end if;
3569 end Delay_Descriptors;
3570
3571 -----------------------
3572 -- Might_Inline_Subp --
3573 -----------------------
3574
3575 function Might_Inline_Subp return Boolean is
3576 E : Entity_Id;
3577
3578 begin
3579 if not Inline_Processing_Required then
3580 return False;
3581
3582 else
3583 E := First_Entity (Gen_Unit);
3584 while Present (E) loop
3585 if Is_Subprogram (E) and then Is_Inlined (E) then
3586 -- Remember if there are any subprograms with Inline_Always
3587
3588 if Has_Pragma_Inline_Always (E) then
3589 Has_Inline_Always := True;
3590 end if;
3591
3592 return True;
3593 end if;
3594
3595 Next_Entity (E);
3596 end loop;
3597 end if;
3598
3599 return False;
3600 end Might_Inline_Subp;
3601
3602 -- Local declarations
3603
3604 Vis_Prims_List : Elist_Id := No_Elist;
3605 -- List of primitives made temporarily visible in the instantiation
3606 -- to match the visibility of the formal type
3607
3608 -- Start of processing for Analyze_Package_Instantiation
3609
3610 begin
3611 Check_SPARK_05_Restriction ("generic is not allowed", N);
3612
3613 -- Very first thing: check for Text_IO special unit in case we are
3614 -- instantiating one of the children of [[Wide_]Wide_]Text_IO.
3615
3616 Check_Text_IO_Special_Unit (Name (N));
3617
3618 -- Make node global for error reporting
3619
3620 Instantiation_Node := N;
3621
3622 -- Turn off style checking in instances. If the check is enabled on the
3623 -- generic unit, a warning in an instance would just be noise. If not
3624 -- enabled on the generic, then a warning in an instance is just wrong.
3625
3626 Style_Check := False;
3627
3628 -- Case of instantiation of a generic package
3629
3630 if Nkind (N) = N_Package_Instantiation then
3631 Act_Decl_Id := New_Copy (Defining_Entity (N));
3632 Set_Comes_From_Source (Act_Decl_Id, True);
3633
3634 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name then
3635 Act_Decl_Name :=
3636 Make_Defining_Program_Unit_Name (Loc,
3637 Name =>
3638 New_Copy_Tree (Name (Defining_Unit_Name (N))),
3639 Defining_Identifier => Act_Decl_Id);
3640 else
3641 Act_Decl_Name := Act_Decl_Id;
3642 end if;
3643
3644 -- Case of instantiation of a formal package
3645
3646 else
3647 Act_Decl_Id := Defining_Identifier (N);
3648 Act_Decl_Name := Act_Decl_Id;
3649 end if;
3650
3651 Generate_Definition (Act_Decl_Id);
3652 Set_Ekind (Act_Decl_Id, E_Package);
3653
3654 -- Initialize list of incomplete actuals before analysis
3655
3656 Set_Incomplete_Actuals (Act_Decl_Id, New_Elmt_List);
3657
3658 Preanalyze_Actuals (N, Act_Decl_Id);
3659
3660 Init_Env;
3661 Env_Installed := True;
3662
3663 -- Reset renaming map for formal types. The mapping is established
3664 -- when analyzing the generic associations, but some mappings are
3665 -- inherited from formal packages of parent units, and these are
3666 -- constructed when the parents are installed.
3667
3668 Generic_Renamings.Set_Last (0);
3669 Generic_Renamings_HTable.Reset;
3670
3671 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
3672 Gen_Unit := Entity (Gen_Id);
3673
3674 -- Verify that it is the name of a generic package
3675
3676 -- A visibility glitch: if the instance is a child unit and the generic
3677 -- is the generic unit of a parent instance (i.e. both the parent and
3678 -- the child units are instances of the same package) the name now
3679 -- denotes the renaming within the parent, not the intended generic
3680 -- unit. See if there is a homonym that is the desired generic. The
3681 -- renaming declaration must be visible inside the instance of the
3682 -- child, but not when analyzing the name in the instantiation itself.
3683
3684 if Ekind (Gen_Unit) = E_Package
3685 and then Present (Renamed_Entity (Gen_Unit))
3686 and then In_Open_Scopes (Renamed_Entity (Gen_Unit))
3687 and then Is_Generic_Instance (Renamed_Entity (Gen_Unit))
3688 and then Present (Homonym (Gen_Unit))
3689 then
3690 Gen_Unit := Homonym (Gen_Unit);
3691 end if;
3692
3693 if Etype (Gen_Unit) = Any_Type then
3694 Restore_Env;
3695 goto Leave;
3696
3697 elsif Ekind (Gen_Unit) /= E_Generic_Package then
3698
3699 -- Ada 2005 (AI-50217): Cannot use instance in limited with_clause
3700
3701 if From_Limited_With (Gen_Unit) then
3702 Error_Msg_N
3703 ("cannot instantiate a limited withed package", Gen_Id);
3704 else
3705 Error_Msg_NE
3706 ("& is not the name of a generic package", Gen_Id, Gen_Unit);
3707 end if;
3708
3709 Restore_Env;
3710 goto Leave;
3711 end if;
3712
3713 if In_Extended_Main_Source_Unit (N) then
3714 Set_Is_Instantiated (Gen_Unit);
3715 Generate_Reference (Gen_Unit, N);
3716
3717 if Present (Renamed_Object (Gen_Unit)) then
3718 Set_Is_Instantiated (Renamed_Object (Gen_Unit));
3719 Generate_Reference (Renamed_Object (Gen_Unit), N);
3720 end if;
3721 end if;
3722
3723 if Nkind (Gen_Id) = N_Identifier
3724 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
3725 then
3726 Error_Msg_NE
3727 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
3728
3729 elsif Nkind (Gen_Id) = N_Expanded_Name
3730 and then Is_Child_Unit (Gen_Unit)
3731 and then Nkind (Prefix (Gen_Id)) = N_Identifier
3732 and then Chars (Act_Decl_Id) = Chars (Prefix (Gen_Id))
3733 then
3734 Error_Msg_N
3735 ("& is hidden within declaration of instance ", Prefix (Gen_Id));
3736 end if;
3737
3738 Set_Entity (Gen_Id, Gen_Unit);
3739
3740 -- If generic is a renaming, get original generic unit
3741
3742 if Present (Renamed_Object (Gen_Unit))
3743 and then Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Package
3744 then
3745 Gen_Unit := Renamed_Object (Gen_Unit);
3746 end if;
3747
3748 -- Verify that there are no circular instantiations
3749
3750 if In_Open_Scopes (Gen_Unit) then
3751 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
3752 Restore_Env;
3753 goto Leave;
3754
3755 elsif Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
3756 Error_Msg_Node_2 := Current_Scope;
3757 Error_Msg_NE
3758 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
3759 Circularity_Detected := True;
3760 Restore_Env;
3761 goto Leave;
3762
3763 else
3764 -- If the context of the instance is subject to SPARK_Mode "off" or
3765 -- the annotation is altogether missing, set the global flag which
3766 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
3767 -- the instance.
3768
3769 if SPARK_Mode /= On then
3770 Ignore_Pragma_SPARK_Mode := True;
3771 end if;
3772
3773 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
3774 Gen_Spec := Specification (Gen_Decl);
3775
3776 -- Initialize renamings map, for error checking, and the list that
3777 -- holds private entities whose views have changed between generic
3778 -- definition and instantiation. If this is the instance created to
3779 -- validate an actual package, the instantiation environment is that
3780 -- of the enclosing instance.
3781
3782 Create_Instantiation_Source (N, Gen_Unit, False, S_Adjustment);
3783
3784 -- Copy original generic tree, to produce text for instantiation
3785
3786 Act_Tree :=
3787 Copy_Generic_Node
3788 (Original_Node (Gen_Decl), Empty, Instantiating => True);
3789
3790 Act_Spec := Specification (Act_Tree);
3791
3792 -- If this is the instance created to validate an actual package,
3793 -- only the formals matter, do not examine the package spec itself.
3794
3795 if Is_Actual_Pack then
3796 Set_Visible_Declarations (Act_Spec, New_List);
3797 Set_Private_Declarations (Act_Spec, New_List);
3798 end if;
3799
3800 Renaming_List :=
3801 Analyze_Associations
3802 (I_Node => N,
3803 Formals => Generic_Formal_Declarations (Act_Tree),
3804 F_Copy => Generic_Formal_Declarations (Gen_Decl));
3805
3806 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
3807
3808 Set_Instance_Env (Gen_Unit, Act_Decl_Id);
3809 Set_Defining_Unit_Name (Act_Spec, Act_Decl_Name);
3810 Set_Is_Generic_Instance (Act_Decl_Id);
3811 Set_Generic_Parent (Act_Spec, Gen_Unit);
3812
3813 -- References to the generic in its own declaration or its body are
3814 -- references to the instance. Add a renaming declaration for the
3815 -- generic unit itself. This declaration, as well as the renaming
3816 -- declarations for the generic formals, must remain private to the
3817 -- unit: the formals, because this is the language semantics, and
3818 -- the unit because its use is an artifact of the implementation.
3819
3820 Unit_Renaming :=
3821 Make_Package_Renaming_Declaration (Loc,
3822 Defining_Unit_Name =>
3823 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
3824 Name => New_Occurrence_Of (Act_Decl_Id, Loc));
3825
3826 Append (Unit_Renaming, Renaming_List);
3827
3828 -- The renaming declarations are the first local declarations of the
3829 -- new unit.
3830
3831 if Is_Non_Empty_List (Visible_Declarations (Act_Spec)) then
3832 Insert_List_Before
3833 (First (Visible_Declarations (Act_Spec)), Renaming_List);
3834 else
3835 Set_Visible_Declarations (Act_Spec, Renaming_List);
3836 end if;
3837
3838 Act_Decl := Make_Package_Declaration (Loc, Specification => Act_Spec);
3839
3840 -- Propagate the aspect specifications from the package declaration
3841 -- template to the instantiated version of the package declaration.
3842
3843 if Has_Aspects (Act_Tree) then
3844 Set_Aspect_Specifications (Act_Decl,
3845 New_Copy_List_Tree (Aspect_Specifications (Act_Tree)));
3846 end if;
3847
3848 -- The generic may have a generated Default_Storage_Pool aspect,
3849 -- set at the point of generic declaration. If the instance has
3850 -- that aspect, it overrides the one inherited from the generic.
3851
3852 if Has_Aspects (Gen_Spec) then
3853 if No (Aspect_Specifications (N)) then
3854 Set_Aspect_Specifications (N,
3855 (New_Copy_List_Tree
3856 (Aspect_Specifications (Gen_Spec))));
3857
3858 else
3859 declare
3860 ASN1, ASN2 : Node_Id;
3861
3862 begin
3863 ASN1 := First (Aspect_Specifications (N));
3864 while Present (ASN1) loop
3865 if Chars (Identifier (ASN1)) = Name_Default_Storage_Pool
3866 then
3867 -- If generic carries a default storage pool, remove
3868 -- it in favor of the instance one.
3869
3870 ASN2 := First (Aspect_Specifications (Gen_Spec));
3871 while Present (ASN2) loop
3872 if Chars (Identifier (ASN2)) =
3873 Name_Default_Storage_Pool
3874 then
3875 Remove (ASN2);
3876 exit;
3877 end if;
3878
3879 Next (ASN2);
3880 end loop;
3881 end if;
3882
3883 Next (ASN1);
3884 end loop;
3885
3886 Prepend_List_To (Aspect_Specifications (N),
3887 (New_Copy_List_Tree
3888 (Aspect_Specifications (Gen_Spec))));
3889 end;
3890 end if;
3891 end if;
3892
3893 -- Save the instantiation node, for subsequent instantiation of the
3894 -- body, if there is one and we are generating code for the current
3895 -- unit. Mark unit as having a body (avoids premature error message).
3896
3897 -- We instantiate the body if we are generating code, if we are
3898 -- generating cross-reference information, or if we are building
3899 -- trees for ASIS use or GNATprove use.
3900
3901 declare
3902 Enclosing_Body_Present : Boolean := False;
3903 -- If the generic unit is not a compilation unit, then a body may
3904 -- be present in its parent even if none is required. We create a
3905 -- tentative pending instantiation for the body, which will be
3906 -- discarded if none is actually present.
3907
3908 Scop : Entity_Id;
3909
3910 begin
3911 if Scope (Gen_Unit) /= Standard_Standard
3912 and then not Is_Child_Unit (Gen_Unit)
3913 then
3914 Scop := Scope (Gen_Unit);
3915 while Present (Scop) and then Scop /= Standard_Standard loop
3916 if Unit_Requires_Body (Scop) then
3917 Enclosing_Body_Present := True;
3918 exit;
3919
3920 elsif In_Open_Scopes (Scop)
3921 and then In_Package_Body (Scop)
3922 then
3923 Enclosing_Body_Present := True;
3924 exit;
3925 end if;
3926
3927 exit when Is_Compilation_Unit (Scop);
3928 Scop := Scope (Scop);
3929 end loop;
3930 end if;
3931
3932 -- If front-end inlining is enabled or there are any subprograms
3933 -- marked with Inline_Always, and this is a unit for which code
3934 -- will be generated, we instantiate the body at once.
3935
3936 -- This is done if the instance is not the main unit, and if the
3937 -- generic is not a child unit of another generic, to avoid scope
3938 -- problems and the reinstallation of parent instances.
3939
3940 if Expander_Active
3941 and then (not Is_Child_Unit (Gen_Unit)
3942 or else not Is_Generic_Unit (Scope (Gen_Unit)))
3943 and then Might_Inline_Subp
3944 and then not Is_Actual_Pack
3945 then
3946 if not Back_End_Inlining
3947 and then (Front_End_Inlining or else Has_Inline_Always)
3948 and then (Is_In_Main_Unit (N)
3949 or else In_Main_Context (Current_Scope))
3950 and then Nkind (Parent (N)) /= N_Compilation_Unit
3951 then
3952 Inline_Now := True;
3953
3954 -- In configurable_run_time mode we force the inlining of
3955 -- predefined subprograms marked Inline_Always, to minimize
3956 -- the use of the run-time library.
3957
3958 elsif Is_Predefined_File_Name
3959 (Unit_File_Name (Get_Source_Unit (Gen_Decl)))
3960 and then Configurable_Run_Time_Mode
3961 and then Nkind (Parent (N)) /= N_Compilation_Unit
3962 then
3963 Inline_Now := True;
3964 end if;
3965
3966 -- If the current scope is itself an instance within a child
3967 -- unit, there will be duplications in the scope stack, and the
3968 -- unstacking mechanism in Inline_Instance_Body will fail.
3969 -- This loses some rare cases of optimization, and might be
3970 -- improved some day, if we can find a proper abstraction for
3971 -- "the complete compilation context" that can be saved and
3972 -- restored. ???
3973
3974 if Is_Generic_Instance (Current_Scope) then
3975 declare
3976 Curr_Unit : constant Entity_Id :=
3977 Cunit_Entity (Current_Sem_Unit);
3978 begin
3979 if Curr_Unit /= Current_Scope
3980 and then Is_Child_Unit (Curr_Unit)
3981 then
3982 Inline_Now := False;
3983 end if;
3984 end;
3985 end if;
3986 end if;
3987
3988 Needs_Body :=
3989 (Unit_Requires_Body (Gen_Unit)
3990 or else Enclosing_Body_Present
3991 or else Present (Corresponding_Body (Gen_Decl)))
3992 and then (Is_In_Main_Unit (N) or else Might_Inline_Subp)
3993 and then not Is_Actual_Pack
3994 and then not Inline_Now
3995 and then (Operating_Mode = Generate_Code
3996
3997 -- Need comment for this check ???
3998
3999 or else (Operating_Mode = Check_Semantics
4000 and then (ASIS_Mode or GNATprove_Mode)));
4001
4002 -- If front-end inlining is enabled or there are any subprograms
4003 -- marked with Inline_Always, do not instantiate body when within
4004 -- a generic context.
4005
4006 if ((Front_End_Inlining or else Has_Inline_Always)
4007 and then not Expander_Active)
4008 or else Is_Generic_Unit (Cunit_Entity (Main_Unit))
4009 then
4010 Needs_Body := False;
4011 end if;
4012
4013 -- If the current context is generic, and the package being
4014 -- instantiated is declared within a formal package, there is no
4015 -- body to instantiate until the enclosing generic is instantiated
4016 -- and there is an actual for the formal package. If the formal
4017 -- package has parameters, we build a regular package instance for
4018 -- it, that precedes the original formal package declaration.
4019
4020 if In_Open_Scopes (Scope (Scope (Gen_Unit))) then
4021 declare
4022 Decl : constant Node_Id :=
4023 Original_Node
4024 (Unit_Declaration_Node (Scope (Gen_Unit)));
4025 begin
4026 if Nkind (Decl) = N_Formal_Package_Declaration
4027 or else (Nkind (Decl) = N_Package_Declaration
4028 and then Is_List_Member (Decl)
4029 and then Present (Next (Decl))
4030 and then
4031 Nkind (Next (Decl)) =
4032 N_Formal_Package_Declaration)
4033 then
4034 Needs_Body := False;
4035 end if;
4036 end;
4037 end if;
4038 end;
4039
4040 -- For RCI unit calling stubs, we omit the instance body if the
4041 -- instance is the RCI library unit itself.
4042
4043 -- However there is a special case for nested instances: in this case
4044 -- we do generate the instance body, as it might be required, e.g.
4045 -- because it provides stream attributes for some type used in the
4046 -- profile of a remote subprogram. This is consistent with 12.3(12),
4047 -- which indicates that the instance body occurs at the place of the
4048 -- instantiation, and thus is part of the RCI declaration, which is
4049 -- present on all client partitions (this is E.2.3(18)).
4050
4051 -- Note that AI12-0002 may make it illegal at some point to have
4052 -- stream attributes defined in an RCI unit, in which case this
4053 -- special case will become unnecessary. In the meantime, there
4054 -- is known application code in production that depends on this
4055 -- being possible, so we definitely cannot eliminate the body in
4056 -- the case of nested instances for the time being.
4057
4058 -- When we generate a nested instance body, calling stubs for any
4059 -- relevant subprogram will be be inserted immediately after the
4060 -- subprogram declarations, and will take precedence over the
4061 -- subsequent (original) body. (The stub and original body will be
4062 -- complete homographs, but this is permitted in an instance).
4063 -- (Could we do better and remove the original body???)
4064
4065 if Distribution_Stub_Mode = Generate_Caller_Stub_Body
4066 and then Comes_From_Source (N)
4067 and then Nkind (Parent (N)) = N_Compilation_Unit
4068 then
4069 Needs_Body := False;
4070 end if;
4071
4072 if Needs_Body then
4073
4074 -- Here is a defence against a ludicrous number of instantiations
4075 -- caused by a circular set of instantiation attempts.
4076
4077 if Pending_Instantiations.Last > Maximum_Instantiations then
4078 Error_Msg_Uint_1 := UI_From_Int (Maximum_Instantiations);
4079 Error_Msg_N ("too many instantiations, exceeds max of^", N);
4080 Error_Msg_N ("\limit can be changed using -gnateinn switch", N);
4081 raise Unrecoverable_Error;
4082 end if;
4083
4084 -- Indicate that the enclosing scopes contain an instantiation,
4085 -- and that cleanup actions should be delayed until after the
4086 -- instance body is expanded.
4087
4088 Check_Forward_Instantiation (Gen_Decl);
4089 if Nkind (N) = N_Package_Instantiation then
4090 declare
4091 Enclosing_Master : Entity_Id;
4092
4093 begin
4094 -- Loop to search enclosing masters
4095
4096 Enclosing_Master := Current_Scope;
4097 Scope_Loop : while Enclosing_Master /= Standard_Standard loop
4098 if Ekind (Enclosing_Master) = E_Package then
4099 if Is_Compilation_Unit (Enclosing_Master) then
4100 if In_Package_Body (Enclosing_Master) then
4101 Delay_Descriptors
4102 (Body_Entity (Enclosing_Master));
4103 else
4104 Delay_Descriptors
4105 (Enclosing_Master);
4106 end if;
4107
4108 exit Scope_Loop;
4109
4110 else
4111 Enclosing_Master := Scope (Enclosing_Master);
4112 end if;
4113
4114 elsif Is_Generic_Unit (Enclosing_Master)
4115 or else Ekind (Enclosing_Master) = E_Void
4116 then
4117 -- Cleanup actions will eventually be performed on the
4118 -- enclosing subprogram or package instance, if any.
4119 -- Enclosing scope is void in the formal part of a
4120 -- generic subprogram.
4121
4122 exit Scope_Loop;
4123
4124 else
4125 if Ekind (Enclosing_Master) = E_Entry
4126 and then
4127 Ekind (Scope (Enclosing_Master)) = E_Protected_Type
4128 then
4129 if not Expander_Active then
4130 exit Scope_Loop;
4131 else
4132 Enclosing_Master :=
4133 Protected_Body_Subprogram (Enclosing_Master);
4134 end if;
4135 end if;
4136
4137 Set_Delay_Cleanups (Enclosing_Master);
4138
4139 while Ekind (Enclosing_Master) = E_Block loop
4140 Enclosing_Master := Scope (Enclosing_Master);
4141 end loop;
4142
4143 if Is_Subprogram (Enclosing_Master) then
4144 Delay_Descriptors (Enclosing_Master);
4145
4146 elsif Is_Task_Type (Enclosing_Master) then
4147 declare
4148 TBP : constant Node_Id :=
4149 Get_Task_Body_Procedure
4150 (Enclosing_Master);
4151 begin
4152 if Present (TBP) then
4153 Delay_Descriptors (TBP);
4154 Set_Delay_Cleanups (TBP);
4155 end if;
4156 end;
4157 end if;
4158
4159 exit Scope_Loop;
4160 end if;
4161 end loop Scope_Loop;
4162 end;
4163
4164 -- Make entry in table
4165
4166 Add_Pending_Instantiation (N, Act_Decl);
4167 end if;
4168 end if;
4169
4170 Set_Categorization_From_Pragmas (Act_Decl);
4171
4172 if Parent_Installed then
4173 Hide_Current_Scope;
4174 end if;
4175
4176 Set_Instance_Spec (N, Act_Decl);
4177
4178 -- If not a compilation unit, insert the package declaration before
4179 -- the original instantiation node.
4180
4181 if Nkind (Parent (N)) /= N_Compilation_Unit then
4182 Mark_Rewrite_Insertion (Act_Decl);
4183 Insert_Before (N, Act_Decl);
4184
4185 if Has_Aspects (N) then
4186 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4187
4188 -- The pragma created for a Default_Storage_Pool aspect must
4189 -- appear ahead of the declarations in the instance spec.
4190 -- Analysis has placed it after the instance node, so remove
4191 -- it and reinsert it properly now.
4192
4193 declare
4194 ASN : constant Node_Id := First (Aspect_Specifications (N));
4195 A_Name : constant Name_Id := Chars (Identifier (ASN));
4196 Decl : Node_Id;
4197
4198 begin
4199 if A_Name = Name_Default_Storage_Pool then
4200 if No (Visible_Declarations (Act_Spec)) then
4201 Set_Visible_Declarations (Act_Spec, New_List);
4202 end if;
4203
4204 Decl := Next (N);
4205 while Present (Decl) loop
4206 if Nkind (Decl) = N_Pragma then
4207 Remove (Decl);
4208 Prepend (Decl, Visible_Declarations (Act_Spec));
4209 exit;
4210 end if;
4211
4212 Next (Decl);
4213 end loop;
4214 end if;
4215 end;
4216 end if;
4217
4218 Analyze (Act_Decl);
4219
4220 -- For an instantiation that is a compilation unit, place
4221 -- declaration on current node so context is complete for analysis
4222 -- (including nested instantiations). If this is the main unit,
4223 -- the declaration eventually replaces the instantiation node.
4224 -- If the instance body is created later, it replaces the
4225 -- instance node, and the declaration is attached to it
4226 -- (see Build_Instance_Compilation_Unit_Nodes).
4227
4228 else
4229 if Cunit_Entity (Current_Sem_Unit) = Defining_Entity (N) then
4230
4231 -- The entity for the current unit is the newly created one,
4232 -- and all semantic information is attached to it.
4233
4234 Set_Cunit_Entity (Current_Sem_Unit, Act_Decl_Id);
4235
4236 -- If this is the main unit, replace the main entity as well
4237
4238 if Current_Sem_Unit = Main_Unit then
4239 Main_Unit_Entity := Act_Decl_Id;
4240 end if;
4241 end if;
4242
4243 Set_Unit (Parent (N), Act_Decl);
4244 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
4245 Set_Package_Instantiation (Act_Decl_Id, N);
4246
4247 -- Process aspect specifications of the instance node, if any, to
4248 -- take into account categorization pragmas before analyzing the
4249 -- instance.
4250
4251 if Has_Aspects (N) then
4252 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4253 end if;
4254
4255 Analyze (Act_Decl);
4256 Set_Unit (Parent (N), N);
4257 Set_Body_Required (Parent (N), False);
4258
4259 -- We never need elaboration checks on instantiations, since by
4260 -- definition, the body instantiation is elaborated at the same
4261 -- time as the spec instantiation.
4262
4263 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
4264 Set_Kill_Elaboration_Checks (Act_Decl_Id);
4265 end if;
4266
4267 Check_Elab_Instantiation (N);
4268
4269 if ABE_Is_Certain (N) and then Needs_Body then
4270 Pending_Instantiations.Decrement_Last;
4271 end if;
4272
4273 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
4274
4275 Set_First_Private_Entity (Defining_Unit_Name (Unit_Renaming),
4276 First_Private_Entity (Act_Decl_Id));
4277
4278 -- If the instantiation will receive a body, the unit will be
4279 -- transformed into a package body, and receive its own elaboration
4280 -- entity. Otherwise, the nature of the unit is now a package
4281 -- declaration.
4282
4283 if Nkind (Parent (N)) = N_Compilation_Unit
4284 and then not Needs_Body
4285 then
4286 Rewrite (N, Act_Decl);
4287 end if;
4288
4289 if Present (Corresponding_Body (Gen_Decl))
4290 or else Unit_Requires_Body (Gen_Unit)
4291 then
4292 Set_Has_Completion (Act_Decl_Id);
4293 end if;
4294
4295 Check_Formal_Packages (Act_Decl_Id);
4296
4297 Restore_Hidden_Primitives (Vis_Prims_List);
4298 Restore_Private_Views (Act_Decl_Id);
4299
4300 Inherit_Context (Gen_Decl, N);
4301
4302 if Parent_Installed then
4303 Remove_Parent;
4304 end if;
4305
4306 Restore_Env;
4307 Env_Installed := False;
4308 end if;
4309
4310 Validate_Categorization_Dependency (N, Act_Decl_Id);
4311
4312 -- There used to be a check here to prevent instantiations in local
4313 -- contexts if the No_Local_Allocators restriction was active. This
4314 -- check was removed by a binding interpretation in AI-95-00130/07,
4315 -- but we retain the code for documentation purposes.
4316
4317 -- if Ekind (Act_Decl_Id) /= E_Void
4318 -- and then not Is_Library_Level_Entity (Act_Decl_Id)
4319 -- then
4320 -- Check_Restriction (No_Local_Allocators, N);
4321 -- end if;
4322
4323 if Inline_Now then
4324 Inline_Instance_Body (N, Gen_Unit, Act_Decl);
4325 end if;
4326
4327 -- The following is a tree patch for ASIS: ASIS needs separate nodes to
4328 -- be used as defining identifiers for a formal package and for the
4329 -- corresponding expanded package.
4330
4331 if Nkind (N) = N_Formal_Package_Declaration then
4332 Act_Decl_Id := New_Copy (Defining_Entity (N));
4333 Set_Comes_From_Source (Act_Decl_Id, True);
4334 Set_Is_Generic_Instance (Act_Decl_Id, False);
4335 Set_Defining_Identifier (N, Act_Decl_Id);
4336 end if;
4337
4338 Ignore_Pragma_SPARK_Mode := Save_IPSM;
4339 SPARK_Mode := Save_SM;
4340 SPARK_Mode_Pragma := Save_SMP;
4341 Style_Check := Save_Style_Check;
4342
4343 if SPARK_Mode = On then
4344 Dynamic_Elaboration_Checks := False;
4345 end if;
4346
4347 -- Check that if N is an instantiation of System.Dim_Float_IO or
4348 -- System.Dim_Integer_IO, the formal type has a dimension system.
4349
4350 if Nkind (N) = N_Package_Instantiation
4351 and then Is_Dim_IO_Package_Instantiation (N)
4352 then
4353 declare
4354 Assoc : constant Node_Id := First (Generic_Associations (N));
4355 begin
4356 if not Has_Dimension_System
4357 (Etype (Explicit_Generic_Actual_Parameter (Assoc)))
4358 then
4359 Error_Msg_N ("type with a dimension system expected", Assoc);
4360 end if;
4361 end;
4362 end if;
4363
4364 <<Leave>>
4365 if Has_Aspects (N) and then Nkind (Parent (N)) /= N_Compilation_Unit then
4366 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4367 end if;
4368
4369 exception
4370 when Instantiation_Error =>
4371 if Parent_Installed then
4372 Remove_Parent;
4373 end if;
4374
4375 if Env_Installed then
4376 Restore_Env;
4377 end if;
4378
4379 Ignore_Pragma_SPARK_Mode := Save_IPSM;
4380 SPARK_Mode := Save_SM;
4381 SPARK_Mode_Pragma := Save_SMP;
4382 Style_Check := Save_Style_Check;
4383
4384 if SPARK_Mode = On then
4385 Dynamic_Elaboration_Checks := False;
4386 end if;
4387 end Analyze_Package_Instantiation;
4388
4389 --------------------------
4390 -- Inline_Instance_Body --
4391 --------------------------
4392
4393 procedure Inline_Instance_Body
4394 (N : Node_Id;
4395 Gen_Unit : Entity_Id;
4396 Act_Decl : Node_Id)
4397 is
4398 Curr_Comp : constant Node_Id := Cunit (Current_Sem_Unit);
4399 Curr_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
4400 Gen_Comp : constant Entity_Id :=
4401 Cunit_Entity (Get_Source_Unit (Gen_Unit));
4402
4403 Save_SM : constant SPARK_Mode_Type := SPARK_Mode;
4404 Save_SMP : constant Node_Id := SPARK_Mode_Pragma;
4405 -- Save all SPARK_Mode-related attributes as removing enclosing scopes
4406 -- to provide a clean environment for analysis of the inlined body will
4407 -- eliminate any previously set SPARK_Mode.
4408
4409 Scope_Stack_Depth : constant Pos :=
4410 Scope_Stack.Last - Scope_Stack.First + 1;
4411
4412 Use_Clauses : array (1 .. Scope_Stack_Depth) of Node_Id;
4413 Instances : array (1 .. Scope_Stack_Depth) of Entity_Id;
4414 Inner_Scopes : array (1 .. Scope_Stack_Depth) of Entity_Id;
4415 Curr_Scope : Entity_Id := Empty;
4416 List : Elist_Id;
4417 Num_Inner : Nat := 0;
4418 Num_Scopes : Nat := 0;
4419 N_Instances : Nat := 0;
4420 Removed : Boolean := False;
4421 S : Entity_Id;
4422 Vis : Boolean;
4423
4424 begin
4425 -- Case of generic unit defined in another unit. We must remove the
4426 -- complete context of the current unit to install that of the generic.
4427
4428 if Gen_Comp /= Cunit_Entity (Current_Sem_Unit) then
4429
4430 -- Add some comments for the following two loops ???
4431
4432 S := Current_Scope;
4433 while Present (S) and then S /= Standard_Standard loop
4434 loop
4435 Num_Scopes := Num_Scopes + 1;
4436
4437 Use_Clauses (Num_Scopes) :=
4438 (Scope_Stack.Table
4439 (Scope_Stack.Last - Num_Scopes + 1).
4440 First_Use_Clause);
4441 End_Use_Clauses (Use_Clauses (Num_Scopes));
4442
4443 exit when Scope_Stack.Last - Num_Scopes + 1 = Scope_Stack.First
4444 or else Scope_Stack.Table
4445 (Scope_Stack.Last - Num_Scopes).Entity = Scope (S);
4446 end loop;
4447
4448 exit when Is_Generic_Instance (S)
4449 and then (In_Package_Body (S)
4450 or else Ekind (S) = E_Procedure
4451 or else Ekind (S) = E_Function);
4452 S := Scope (S);
4453 end loop;
4454
4455 Vis := Is_Immediately_Visible (Gen_Comp);
4456
4457 -- Find and save all enclosing instances
4458
4459 S := Current_Scope;
4460
4461 while Present (S)
4462 and then S /= Standard_Standard
4463 loop
4464 if Is_Generic_Instance (S) then
4465 N_Instances := N_Instances + 1;
4466 Instances (N_Instances) := S;
4467
4468 exit when In_Package_Body (S);
4469 end if;
4470
4471 S := Scope (S);
4472 end loop;
4473
4474 -- Remove context of current compilation unit, unless we are within a
4475 -- nested package instantiation, in which case the context has been
4476 -- removed previously.
4477
4478 -- If current scope is the body of a child unit, remove context of
4479 -- spec as well. If an enclosing scope is an instance body, the
4480 -- context has already been removed, but the entities in the body
4481 -- must be made invisible as well.
4482
4483 S := Current_Scope;
4484 while Present (S) and then S /= Standard_Standard loop
4485 if Is_Generic_Instance (S)
4486 and then (In_Package_Body (S)
4487 or else Ekind_In (S, E_Procedure, E_Function))
4488 then
4489 -- We still have to remove the entities of the enclosing
4490 -- instance from direct visibility.
4491
4492 declare
4493 E : Entity_Id;
4494 begin
4495 E := First_Entity (S);
4496 while Present (E) loop
4497 Set_Is_Immediately_Visible (E, False);
4498 Next_Entity (E);
4499 end loop;
4500 end;
4501
4502 exit;
4503 end if;
4504
4505 if S = Curr_Unit
4506 or else (Ekind (Curr_Unit) = E_Package_Body
4507 and then S = Spec_Entity (Curr_Unit))
4508 or else (Ekind (Curr_Unit) = E_Subprogram_Body
4509 and then S = Corresponding_Spec
4510 (Unit_Declaration_Node (Curr_Unit)))
4511 then
4512 Removed := True;
4513
4514 -- Remove entities in current scopes from visibility, so that
4515 -- instance body is compiled in a clean environment.
4516
4517 List := Save_Scope_Stack (Handle_Use => False);
4518
4519 if Is_Child_Unit (S) then
4520
4521 -- Remove child unit from stack, as well as inner scopes.
4522 -- Removing the context of a child unit removes parent units
4523 -- as well.
4524
4525 while Current_Scope /= S loop
4526 Num_Inner := Num_Inner + 1;
4527 Inner_Scopes (Num_Inner) := Current_Scope;
4528 Pop_Scope;
4529 end loop;
4530
4531 Pop_Scope;
4532 Remove_Context (Curr_Comp);
4533 Curr_Scope := S;
4534
4535 else
4536 Remove_Context (Curr_Comp);
4537 end if;
4538
4539 if Ekind (Curr_Unit) = E_Package_Body then
4540 Remove_Context (Library_Unit (Curr_Comp));
4541 end if;
4542 end if;
4543
4544 S := Scope (S);
4545 end loop;
4546
4547 pragma Assert (Num_Inner < Num_Scopes);
4548
4549 -- The inlined package body must be analyzed with the SPARK_Mode of
4550 -- the enclosing context, otherwise the body may cause bogus errors
4551 -- if a configuration SPARK_Mode pragma in in effect.
4552
4553 Push_Scope (Standard_Standard);
4554 Scope_Stack.Table (Scope_Stack.Last).Is_Active_Stack_Base := True;
4555 Instantiate_Package_Body
4556 (Body_Info =>
4557 ((Inst_Node => N,
4558 Act_Decl => Act_Decl,
4559 Expander_Status => Expander_Active,
4560 Current_Sem_Unit => Current_Sem_Unit,
4561 Scope_Suppress => Scope_Suppress,
4562 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
4563 Version => Ada_Version,
4564 Version_Pragma => Ada_Version_Pragma,
4565 Warnings => Save_Warnings,
4566 SPARK_Mode => Save_SM,
4567 SPARK_Mode_Pragma => Save_SMP)),
4568 Inlined_Body => True);
4569
4570 Pop_Scope;
4571
4572 -- Restore context
4573
4574 Set_Is_Immediately_Visible (Gen_Comp, Vis);
4575
4576 -- Reset Generic_Instance flag so that use clauses can be installed
4577 -- in the proper order. (See Use_One_Package for effect of enclosing
4578 -- instances on processing of use clauses).
4579
4580 for J in 1 .. N_Instances loop
4581 Set_Is_Generic_Instance (Instances (J), False);
4582 end loop;
4583
4584 if Removed then
4585 Install_Context (Curr_Comp);
4586
4587 if Present (Curr_Scope)
4588 and then Is_Child_Unit (Curr_Scope)
4589 then
4590 Push_Scope (Curr_Scope);
4591 Set_Is_Immediately_Visible (Curr_Scope);
4592
4593 -- Finally, restore inner scopes as well
4594
4595 for J in reverse 1 .. Num_Inner loop
4596 Push_Scope (Inner_Scopes (J));
4597 end loop;
4598 end if;
4599
4600 Restore_Scope_Stack (List, Handle_Use => False);
4601
4602 if Present (Curr_Scope)
4603 and then
4604 (In_Private_Part (Curr_Scope)
4605 or else In_Package_Body (Curr_Scope))
4606 then
4607 -- Install private declaration of ancestor units, which are
4608 -- currently available. Restore_Scope_Stack and Install_Context
4609 -- only install the visible part of parents.
4610
4611 declare
4612 Par : Entity_Id;
4613 begin
4614 Par := Scope (Curr_Scope);
4615 while (Present (Par)) and then Par /= Standard_Standard loop
4616 Install_Private_Declarations (Par);
4617 Par := Scope (Par);
4618 end loop;
4619 end;
4620 end if;
4621 end if;
4622
4623 -- Restore use clauses. For a child unit, use clauses in the parents
4624 -- are restored when installing the context, so only those in inner
4625 -- scopes (and those local to the child unit itself) need to be
4626 -- installed explicitly.
4627
4628 if Is_Child_Unit (Curr_Unit) and then Removed then
4629 for J in reverse 1 .. Num_Inner + 1 loop
4630 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
4631 Use_Clauses (J);
4632 Install_Use_Clauses (Use_Clauses (J));
4633 end loop;
4634
4635 else
4636 for J in reverse 1 .. Num_Scopes loop
4637 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
4638 Use_Clauses (J);
4639 Install_Use_Clauses (Use_Clauses (J));
4640 end loop;
4641 end if;
4642
4643 -- Restore status of instances. If one of them is a body, make its
4644 -- local entities visible again.
4645
4646 declare
4647 E : Entity_Id;
4648 Inst : Entity_Id;
4649
4650 begin
4651 for J in 1 .. N_Instances loop
4652 Inst := Instances (J);
4653 Set_Is_Generic_Instance (Inst, True);
4654
4655 if In_Package_Body (Inst)
4656 or else Ekind_In (S, E_Procedure, E_Function)
4657 then
4658 E := First_Entity (Instances (J));
4659 while Present (E) loop
4660 Set_Is_Immediately_Visible (E);
4661 Next_Entity (E);
4662 end loop;
4663 end if;
4664 end loop;
4665 end;
4666
4667 -- If generic unit is in current unit, current context is correct. Note
4668 -- that the context is guaranteed to carry the correct SPARK_Mode as no
4669 -- enclosing scopes were removed.
4670
4671 else
4672 Instantiate_Package_Body
4673 (Body_Info =>
4674 ((Inst_Node => N,
4675 Act_Decl => Act_Decl,
4676 Expander_Status => Expander_Active,
4677 Current_Sem_Unit => Current_Sem_Unit,
4678 Scope_Suppress => Scope_Suppress,
4679 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
4680 Version => Ada_Version,
4681 Version_Pragma => Ada_Version_Pragma,
4682 Warnings => Save_Warnings,
4683 SPARK_Mode => SPARK_Mode,
4684 SPARK_Mode_Pragma => SPARK_Mode_Pragma)),
4685 Inlined_Body => True);
4686 end if;
4687 end Inline_Instance_Body;
4688
4689 -------------------------------------
4690 -- Analyze_Procedure_Instantiation --
4691 -------------------------------------
4692
4693 procedure Analyze_Procedure_Instantiation (N : Node_Id) is
4694 begin
4695 Analyze_Subprogram_Instantiation (N, E_Procedure);
4696 end Analyze_Procedure_Instantiation;
4697
4698 -----------------------------------
4699 -- Need_Subprogram_Instance_Body --
4700 -----------------------------------
4701
4702 function Need_Subprogram_Instance_Body
4703 (N : Node_Id;
4704 Subp : Entity_Id) return Boolean
4705 is
4706
4707 function Is_Inlined_Or_Child_Of_Inlined (E : Entity_Id) return Boolean;
4708 -- Return True if E is an inlined subprogram, an inlined renaming or a
4709 -- subprogram nested in an inlined subprogram. The inlining machinery
4710 -- totally disregards nested subprograms since it considers that they
4711 -- will always be compiled if the parent is (see Inline.Is_Nested).
4712
4713 ------------------------------------
4714 -- Is_Inlined_Or_Child_Of_Inlined --
4715 ------------------------------------
4716
4717 function Is_Inlined_Or_Child_Of_Inlined (E : Entity_Id) return Boolean is
4718 Scop : Entity_Id;
4719
4720 begin
4721 if Is_Inlined (E) or else Is_Inlined (Alias (E)) then
4722 return True;
4723 end if;
4724
4725 Scop := Scope (E);
4726 while Scop /= Standard_Standard loop
4727 if Ekind (Scop) in Subprogram_Kind and then Is_Inlined (Scop) then
4728 return True;
4729 end if;
4730
4731 Scop := Scope (Scop);
4732 end loop;
4733
4734 return False;
4735 end Is_Inlined_Or_Child_Of_Inlined;
4736
4737 begin
4738 -- Must be in the main unit or inlined (or child of inlined)
4739
4740 if (Is_In_Main_Unit (N) or else Is_Inlined_Or_Child_Of_Inlined (Subp))
4741
4742 -- Must be generating code or analyzing code in ASIS/GNATprove mode
4743
4744 and then (Operating_Mode = Generate_Code
4745 or else (Operating_Mode = Check_Semantics
4746 and then (ASIS_Mode or GNATprove_Mode)))
4747
4748 -- The body is needed when generating code (full expansion), in ASIS
4749 -- mode for other tools, and in GNATprove mode (special expansion) for
4750 -- formal verification of the body itself.
4751
4752 and then (Expander_Active or ASIS_Mode or GNATprove_Mode)
4753
4754 -- No point in inlining if ABE is inevitable
4755
4756 and then not ABE_Is_Certain (N)
4757
4758 -- Or if subprogram is eliminated
4759
4760 and then not Is_Eliminated (Subp)
4761 then
4762 Add_Pending_Instantiation (N, Unit_Declaration_Node (Subp));
4763 return True;
4764
4765 -- Here if not inlined, or we ignore the inlining
4766
4767 else
4768 return False;
4769 end if;
4770 end Need_Subprogram_Instance_Body;
4771
4772 --------------------------------------
4773 -- Analyze_Subprogram_Instantiation --
4774 --------------------------------------
4775
4776 procedure Analyze_Subprogram_Instantiation
4777 (N : Node_Id;
4778 K : Entity_Kind)
4779 is
4780 Loc : constant Source_Ptr := Sloc (N);
4781 Gen_Id : constant Node_Id := Name (N);
4782
4783 Anon_Id : constant Entity_Id :=
4784 Make_Defining_Identifier (Sloc (Defining_Entity (N)),
4785 Chars => New_External_Name
4786 (Chars (Defining_Entity (N)), 'R'));
4787
4788 Act_Decl_Id : Entity_Id;
4789 Act_Decl : Node_Id;
4790 Act_Spec : Node_Id;
4791 Act_Tree : Node_Id;
4792
4793 Env_Installed : Boolean := False;
4794 Gen_Unit : Entity_Id;
4795 Gen_Decl : Node_Id;
4796 Pack_Id : Entity_Id;
4797 Parent_Installed : Boolean := False;
4798
4799 Renaming_List : List_Id;
4800 -- The list of declarations that link formals and actuals of the
4801 -- instance. These are subtype declarations for formal types, and
4802 -- renaming declarations for other formals. The subprogram declaration
4803 -- for the instance is then appended to the list, and the last item on
4804 -- the list is the renaming declaration for the instance.
4805
4806 procedure Analyze_Instance_And_Renamings;
4807 -- The instance must be analyzed in a context that includes the mappings
4808 -- of generic parameters into actuals. We create a package declaration
4809 -- for this purpose, and a subprogram with an internal name within the
4810 -- package. The subprogram instance is simply an alias for the internal
4811 -- subprogram, declared in the current scope.
4812
4813 procedure Build_Subprogram_Renaming;
4814 -- If the subprogram is recursive, there are occurrences of the name of
4815 -- the generic within the body, which must resolve to the current
4816 -- instance. We add a renaming declaration after the declaration, which
4817 -- is available in the instance body, as well as in the analysis of
4818 -- aspects that appear in the generic. This renaming declaration is
4819 -- inserted after the instance declaration which it renames.
4820
4821 ------------------------------------
4822 -- Analyze_Instance_And_Renamings --
4823 ------------------------------------
4824
4825 procedure Analyze_Instance_And_Renamings is
4826 Def_Ent : constant Entity_Id := Defining_Entity (N);
4827 Pack_Decl : Node_Id;
4828
4829 begin
4830 if Nkind (Parent (N)) = N_Compilation_Unit then
4831
4832 -- For the case of a compilation unit, the container package has
4833 -- the same name as the instantiation, to insure that the binder
4834 -- calls the elaboration procedure with the right name. Copy the
4835 -- entity of the instance, which may have compilation level flags
4836 -- (e.g. Is_Child_Unit) set.
4837
4838 Pack_Id := New_Copy (Def_Ent);
4839
4840 else
4841 -- Otherwise we use the name of the instantiation concatenated
4842 -- with its source position to ensure uniqueness if there are
4843 -- several instantiations with the same name.
4844
4845 Pack_Id :=
4846 Make_Defining_Identifier (Loc,
4847 Chars => New_External_Name
4848 (Related_Id => Chars (Def_Ent),
4849 Suffix => "GP",
4850 Suffix_Index => Source_Offset (Sloc (Def_Ent))));
4851 end if;
4852
4853 Pack_Decl :=
4854 Make_Package_Declaration (Loc,
4855 Specification => Make_Package_Specification (Loc,
4856 Defining_Unit_Name => Pack_Id,
4857 Visible_Declarations => Renaming_List,
4858 End_Label => Empty));
4859
4860 Set_Instance_Spec (N, Pack_Decl);
4861 Set_Is_Generic_Instance (Pack_Id);
4862 Set_Debug_Info_Needed (Pack_Id);
4863
4864 -- Case of not a compilation unit
4865
4866 if Nkind (Parent (N)) /= N_Compilation_Unit then
4867 Mark_Rewrite_Insertion (Pack_Decl);
4868 Insert_Before (N, Pack_Decl);
4869 Set_Has_Completion (Pack_Id);
4870
4871 -- Case of an instantiation that is a compilation unit
4872
4873 -- Place declaration on current node so context is complete for
4874 -- analysis (including nested instantiations), and for use in a
4875 -- context_clause (see Analyze_With_Clause).
4876
4877 else
4878 Set_Unit (Parent (N), Pack_Decl);
4879 Set_Parent_Spec (Pack_Decl, Parent_Spec (N));
4880 end if;
4881
4882 Analyze (Pack_Decl);
4883 Check_Formal_Packages (Pack_Id);
4884 Set_Is_Generic_Instance (Pack_Id, False);
4885
4886 -- Why do we clear Is_Generic_Instance??? We set it 20 lines
4887 -- above???
4888
4889 -- Body of the enclosing package is supplied when instantiating the
4890 -- subprogram body, after semantic analysis is completed.
4891
4892 if Nkind (Parent (N)) = N_Compilation_Unit then
4893
4894 -- Remove package itself from visibility, so it does not
4895 -- conflict with subprogram.
4896
4897 Set_Name_Entity_Id (Chars (Pack_Id), Homonym (Pack_Id));
4898
4899 -- Set name and scope of internal subprogram so that the proper
4900 -- external name will be generated. The proper scope is the scope
4901 -- of the wrapper package. We need to generate debugging info for
4902 -- the internal subprogram, so set flag accordingly.
4903
4904 Set_Chars (Anon_Id, Chars (Defining_Entity (N)));
4905 Set_Scope (Anon_Id, Scope (Pack_Id));
4906
4907 -- Mark wrapper package as referenced, to avoid spurious warnings
4908 -- if the instantiation appears in various with_ clauses of
4909 -- subunits of the main unit.
4910
4911 Set_Referenced (Pack_Id);
4912 end if;
4913
4914 Set_Is_Generic_Instance (Anon_Id);
4915 Set_Debug_Info_Needed (Anon_Id);
4916 Act_Decl_Id := New_Copy (Anon_Id);
4917
4918 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
4919 Set_Chars (Act_Decl_Id, Chars (Defining_Entity (N)));
4920 Set_Sloc (Act_Decl_Id, Sloc (Defining_Entity (N)));
4921
4922 -- Subprogram instance comes from source only if generic does
4923
4924 Set_Comes_From_Source (Act_Decl_Id, Comes_From_Source (Gen_Unit));
4925
4926 -- If the instance is a child unit, mark the Id accordingly. Mark
4927 -- the anonymous entity as well, which is the real subprogram and
4928 -- which is used when the instance appears in a context clause.
4929 -- Similarly, propagate the Is_Eliminated flag to handle properly
4930 -- nested eliminated subprograms.
4931
4932 Set_Is_Child_Unit (Act_Decl_Id, Is_Child_Unit (Defining_Entity (N)));
4933 Set_Is_Child_Unit (Anon_Id, Is_Child_Unit (Defining_Entity (N)));
4934 New_Overloaded_Entity (Act_Decl_Id);
4935 Check_Eliminated (Act_Decl_Id);
4936 Set_Is_Eliminated (Anon_Id, Is_Eliminated (Act_Decl_Id));
4937
4938 -- In compilation unit case, kill elaboration checks on the
4939 -- instantiation, since they are never needed -- the body is
4940 -- instantiated at the same point as the spec.
4941
4942 if Nkind (Parent (N)) = N_Compilation_Unit then
4943 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
4944 Set_Kill_Elaboration_Checks (Act_Decl_Id);
4945 Set_Is_Compilation_Unit (Anon_Id);
4946
4947 Set_Cunit_Entity (Current_Sem_Unit, Pack_Id);
4948 end if;
4949
4950 -- The instance is not a freezing point for the new subprogram.
4951 -- The anonymous subprogram may have a freeze node, created for
4952 -- some delayed aspects. This freeze node must not be inherited
4953 -- by the visible subprogram entity.
4954
4955 Set_Is_Frozen (Act_Decl_Id, False);
4956 Set_Freeze_Node (Act_Decl_Id, Empty);
4957
4958 if Nkind (Defining_Entity (N)) = N_Defining_Operator_Symbol then
4959 Valid_Operator_Definition (Act_Decl_Id);
4960 end if;
4961
4962 Set_Alias (Act_Decl_Id, Anon_Id);
4963 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
4964 Set_Has_Completion (Act_Decl_Id);
4965 Set_Related_Instance (Pack_Id, Act_Decl_Id);
4966
4967 if Nkind (Parent (N)) = N_Compilation_Unit then
4968 Set_Body_Required (Parent (N), False);
4969 end if;
4970 end Analyze_Instance_And_Renamings;
4971
4972 -------------------------------
4973 -- Build_Subprogram_Renaming --
4974 -------------------------------
4975
4976 procedure Build_Subprogram_Renaming is
4977 Renaming_Decl : Node_Id;
4978 Unit_Renaming : Node_Id;
4979
4980 begin
4981 Unit_Renaming :=
4982 Make_Subprogram_Renaming_Declaration (Loc,
4983 Specification =>
4984 Copy_Generic_Node
4985 (Specification (Original_Node (Gen_Decl)),
4986 Empty,
4987 Instantiating => True),
4988 Name => New_Occurrence_Of (Anon_Id, Loc));
4989
4990 -- The generic may be a a child unit. The renaming needs an
4991 -- identifier with the proper name.
4992
4993 Set_Defining_Unit_Name (Specification (Unit_Renaming),
4994 Make_Defining_Identifier (Loc, Chars (Gen_Unit)));
4995
4996 -- If there is a formal subprogram with the same name as the unit
4997 -- itself, do not add this renaming declaration, to prevent
4998 -- ambiguities when there is a call with that name in the body.
4999 -- This is a partial and ugly fix for one ACATS test. ???
5000
5001 Renaming_Decl := First (Renaming_List);
5002 while Present (Renaming_Decl) loop
5003 if Nkind (Renaming_Decl) = N_Subprogram_Renaming_Declaration
5004 and then
5005 Chars (Defining_Entity (Renaming_Decl)) = Chars (Gen_Unit)
5006 then
5007 exit;
5008 end if;
5009
5010 Next (Renaming_Decl);
5011 end loop;
5012
5013 if No (Renaming_Decl) then
5014 Append (Unit_Renaming, Renaming_List);
5015 end if;
5016 end Build_Subprogram_Renaming;
5017
5018 -- Local variables
5019
5020 Save_IPSM : constant Boolean := Ignore_Pragma_SPARK_Mode;
5021 -- Save flag Ignore_Pragma_SPARK_Mode for restore on exit
5022
5023 Save_SM : constant SPARK_Mode_Type := SPARK_Mode;
5024 Save_SMP : constant Node_Id := SPARK_Mode_Pragma;
5025 -- Save the SPARK_Mode-related data for restore on exit
5026
5027 Vis_Prims_List : Elist_Id := No_Elist;
5028 -- List of primitives made temporarily visible in the instantiation
5029 -- to match the visibility of the formal type
5030
5031 -- Start of processing for Analyze_Subprogram_Instantiation
5032
5033 begin
5034 Check_SPARK_05_Restriction ("generic is not allowed", N);
5035
5036 -- Very first thing: check for special Text_IO unit in case we are
5037 -- instantiating one of the children of [[Wide_]Wide_]Text_IO. Of course
5038 -- such an instantiation is bogus (these are packages, not subprograms),
5039 -- but we get a better error message if we do this.
5040
5041 Check_Text_IO_Special_Unit (Gen_Id);
5042
5043 -- Make node global for error reporting
5044
5045 Instantiation_Node := N;
5046
5047 -- For package instantiations we turn off style checks, because they
5048 -- will have been emitted in the generic. For subprogram instantiations
5049 -- we want to apply at least the check on overriding indicators so we
5050 -- do not modify the style check status.
5051
5052 -- The renaming declarations for the actuals do not come from source and
5053 -- will not generate spurious warnings.
5054
5055 Preanalyze_Actuals (N);
5056
5057 Init_Env;
5058 Env_Installed := True;
5059 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
5060 Gen_Unit := Entity (Gen_Id);
5061
5062 Generate_Reference (Gen_Unit, Gen_Id);
5063
5064 if Nkind (Gen_Id) = N_Identifier
5065 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
5066 then
5067 Error_Msg_NE
5068 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
5069 end if;
5070
5071 if Etype (Gen_Unit) = Any_Type then
5072 Restore_Env;
5073 return;
5074 end if;
5075
5076 -- Verify that it is a generic subprogram of the right kind, and that
5077 -- it does not lead to a circular instantiation.
5078
5079 if K = E_Procedure and then Ekind (Gen_Unit) /= E_Generic_Procedure then
5080 Error_Msg_NE
5081 ("& is not the name of a generic procedure", Gen_Id, Gen_Unit);
5082
5083 elsif K = E_Function and then Ekind (Gen_Unit) /= E_Generic_Function then
5084 Error_Msg_NE
5085 ("& is not the name of a generic function", Gen_Id, Gen_Unit);
5086
5087 elsif In_Open_Scopes (Gen_Unit) then
5088 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
5089
5090 else
5091 -- If the context of the instance is subject to SPARK_Mode "off" or
5092 -- the annotation is altogether missing, set the global flag which
5093 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
5094 -- the instance.
5095
5096 if SPARK_Mode /= On then
5097 Ignore_Pragma_SPARK_Mode := True;
5098 end if;
5099
5100 Set_Entity (Gen_Id, Gen_Unit);
5101 Set_Is_Instantiated (Gen_Unit);
5102
5103 if In_Extended_Main_Source_Unit (N) then
5104 Generate_Reference (Gen_Unit, N);
5105 end if;
5106
5107 -- If renaming, get original unit
5108
5109 if Present (Renamed_Object (Gen_Unit))
5110 and then Ekind_In (Renamed_Object (Gen_Unit), E_Generic_Procedure,
5111 E_Generic_Function)
5112 then
5113 Gen_Unit := Renamed_Object (Gen_Unit);
5114 Set_Is_Instantiated (Gen_Unit);
5115 Generate_Reference (Gen_Unit, N);
5116 end if;
5117
5118 if Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
5119 Error_Msg_Node_2 := Current_Scope;
5120 Error_Msg_NE
5121 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
5122 Circularity_Detected := True;
5123 Restore_Hidden_Primitives (Vis_Prims_List);
5124 goto Leave;
5125 end if;
5126
5127 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
5128
5129 -- Initialize renamings map, for error checking
5130
5131 Generic_Renamings.Set_Last (0);
5132 Generic_Renamings_HTable.Reset;
5133
5134 Create_Instantiation_Source (N, Gen_Unit, False, S_Adjustment);
5135
5136 -- Copy original generic tree, to produce text for instantiation
5137
5138 Act_Tree :=
5139 Copy_Generic_Node
5140 (Original_Node (Gen_Decl), Empty, Instantiating => True);
5141
5142 -- Inherit overriding indicator from instance node
5143
5144 Act_Spec := Specification (Act_Tree);
5145 Set_Must_Override (Act_Spec, Must_Override (N));
5146 Set_Must_Not_Override (Act_Spec, Must_Not_Override (N));
5147
5148 Renaming_List :=
5149 Analyze_Associations
5150 (I_Node => N,
5151 Formals => Generic_Formal_Declarations (Act_Tree),
5152 F_Copy => Generic_Formal_Declarations (Gen_Decl));
5153
5154 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
5155
5156 -- The subprogram itself cannot contain a nested instance, so the
5157 -- current parent is left empty.
5158
5159 Set_Instance_Env (Gen_Unit, Empty);
5160
5161 -- Build the subprogram declaration, which does not appear in the
5162 -- generic template, and give it a sloc consistent with that of the
5163 -- template.
5164
5165 Set_Defining_Unit_Name (Act_Spec, Anon_Id);
5166 Set_Generic_Parent (Act_Spec, Gen_Unit);
5167 Act_Decl :=
5168 Make_Subprogram_Declaration (Sloc (Act_Spec),
5169 Specification => Act_Spec);
5170
5171 -- The aspects have been copied previously, but they have to be
5172 -- linked explicitly to the new subprogram declaration. Explicit
5173 -- pre/postconditions on the instance are analyzed below, in a
5174 -- separate step.
5175
5176 Move_Aspects (Act_Tree, To => Act_Decl);
5177 Set_Categorization_From_Pragmas (Act_Decl);
5178
5179 if Parent_Installed then
5180 Hide_Current_Scope;
5181 end if;
5182
5183 Append (Act_Decl, Renaming_List);
5184
5185 -- Contract-related source pragmas that follow a generic subprogram
5186 -- must be instantiated explicitly because they are not part of the
5187 -- subprogram template.
5188
5189 Instantiate_Subprogram_Contract
5190 (Original_Node (Gen_Decl), Renaming_List);
5191
5192 Build_Subprogram_Renaming;
5193 Analyze_Instance_And_Renamings;
5194
5195 -- If the generic is marked Import (Intrinsic), then so is the
5196 -- instance. This indicates that there is no body to instantiate. If
5197 -- generic is marked inline, so it the instance, and the anonymous
5198 -- subprogram it renames. If inlined, or else if inlining is enabled
5199 -- for the compilation, we generate the instance body even if it is
5200 -- not within the main unit.
5201
5202 if Is_Intrinsic_Subprogram (Gen_Unit) then
5203 Set_Is_Intrinsic_Subprogram (Anon_Id);
5204 Set_Is_Intrinsic_Subprogram (Act_Decl_Id);
5205
5206 if Chars (Gen_Unit) = Name_Unchecked_Conversion then
5207 Validate_Unchecked_Conversion (N, Act_Decl_Id);
5208 end if;
5209 end if;
5210
5211 -- Inherit convention from generic unit. Intrinsic convention, as for
5212 -- an instance of unchecked conversion, is not inherited because an
5213 -- explicit Ada instance has been created.
5214
5215 if Has_Convention_Pragma (Gen_Unit)
5216 and then Convention (Gen_Unit) /= Convention_Intrinsic
5217 then
5218 Set_Convention (Act_Decl_Id, Convention (Gen_Unit));
5219 Set_Is_Exported (Act_Decl_Id, Is_Exported (Gen_Unit));
5220 end if;
5221
5222 Generate_Definition (Act_Decl_Id);
5223
5224 -- Inherit all inlining-related flags which apply to the generic in
5225 -- the subprogram and its declaration.
5226
5227 Set_Is_Inlined (Act_Decl_Id, Is_Inlined (Gen_Unit));
5228 Set_Is_Inlined (Anon_Id, Is_Inlined (Gen_Unit));
5229
5230 Set_Has_Pragma_Inline (Act_Decl_Id, Has_Pragma_Inline (Gen_Unit));
5231 Set_Has_Pragma_Inline (Anon_Id, Has_Pragma_Inline (Gen_Unit));
5232
5233 Set_Has_Pragma_Inline_Always
5234 (Act_Decl_Id, Has_Pragma_Inline_Always (Gen_Unit));
5235 Set_Has_Pragma_Inline_Always
5236 (Anon_Id, Has_Pragma_Inline_Always (Gen_Unit));
5237
5238 if not Is_Intrinsic_Subprogram (Gen_Unit) then
5239 Check_Elab_Instantiation (N);
5240 end if;
5241
5242 if Is_Dispatching_Operation (Act_Decl_Id)
5243 and then Ada_Version >= Ada_2005
5244 then
5245 declare
5246 Formal : Entity_Id;
5247
5248 begin
5249 Formal := First_Formal (Act_Decl_Id);
5250 while Present (Formal) loop
5251 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type
5252 and then Is_Controlling_Formal (Formal)
5253 and then not Can_Never_Be_Null (Formal)
5254 then
5255 Error_Msg_NE
5256 ("access parameter& is controlling,", N, Formal);
5257 Error_Msg_NE
5258 ("\corresponding parameter of & must be "
5259 & "explicitly null-excluding", N, Gen_Id);
5260 end if;
5261
5262 Next_Formal (Formal);
5263 end loop;
5264 end;
5265 end if;
5266
5267 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
5268
5269 Validate_Categorization_Dependency (N, Act_Decl_Id);
5270
5271 if not Is_Intrinsic_Subprogram (Act_Decl_Id) then
5272 Inherit_Context (Gen_Decl, N);
5273
5274 Restore_Private_Views (Pack_Id, False);
5275
5276 -- If the context requires a full instantiation, mark node for
5277 -- subsequent construction of the body.
5278
5279 if Need_Subprogram_Instance_Body (N, Act_Decl_Id) then
5280 Check_Forward_Instantiation (Gen_Decl);
5281
5282 -- The wrapper package is always delayed, because it does not
5283 -- constitute a freeze point, but to insure that the freeze node
5284 -- is placed properly, it is created directly when instantiating
5285 -- the body (otherwise the freeze node might appear to early for
5286 -- nested instantiations). For ASIS purposes, indicate that the
5287 -- wrapper package has replaced the instantiation node.
5288
5289 elsif Nkind (Parent (N)) = N_Compilation_Unit then
5290 Rewrite (N, Unit (Parent (N)));
5291 Set_Unit (Parent (N), N);
5292 end if;
5293
5294 -- Replace instance node for library-level instantiations of
5295 -- intrinsic subprograms, for ASIS use.
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 if Parent_Installed then
5303 Remove_Parent;
5304 end if;
5305
5306 Restore_Hidden_Primitives (Vis_Prims_List);
5307 Restore_Env;
5308 Env_Installed := False;
5309 Generic_Renamings.Set_Last (0);
5310 Generic_Renamings_HTable.Reset;
5311
5312 Ignore_Pragma_SPARK_Mode := Save_IPSM;
5313 SPARK_Mode := Save_SM;
5314 SPARK_Mode_Pragma := Save_SMP;
5315
5316 if SPARK_Mode = On then
5317 Dynamic_Elaboration_Checks := False;
5318 end if;
5319 end if;
5320
5321 <<Leave>>
5322 if Has_Aspects (N) then
5323 Analyze_Aspect_Specifications (N, Act_Decl_Id);
5324 end if;
5325
5326 exception
5327 when Instantiation_Error =>
5328 if Parent_Installed then
5329 Remove_Parent;
5330 end if;
5331
5332 if Env_Installed then
5333 Restore_Env;
5334 end if;
5335
5336 Ignore_Pragma_SPARK_Mode := Save_IPSM;
5337 SPARK_Mode := Save_SM;
5338 SPARK_Mode_Pragma := Save_SMP;
5339
5340 if SPARK_Mode = On then
5341 Dynamic_Elaboration_Checks := False;
5342 end if;
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 Kind : constant Node_Kind := Nkind (Parent (E2));
5763
5764 begin
5765 if Kind = N_Formal_Type_Declaration then
5766 return;
5767
5768 elsif Nkind_In (Kind, N_Formal_Object_Declaration,
5769 N_Formal_Package_Declaration)
5770 or else Kind in N_Formal_Subprogram_Declaration
5771 then
5772 null;
5773
5774 -- Ada 2012: If both formal and actual are incomplete types they
5775 -- are conformant.
5776
5777 elsif Is_Incomplete_Type (E1) and then Is_Incomplete_Type (E2) then
5778 null;
5779
5780 elsif B then
5781 Error_Msg_NE
5782 ("actual for & in actual instance does not match formal",
5783 Parent (Actual_Pack), E1);
5784 end if;
5785 end Check_Mismatch;
5786
5787 --------------------------------
5788 -- Same_Instantiated_Constant --
5789 --------------------------------
5790
5791 function Same_Instantiated_Constant
5792 (E1, E2 : Entity_Id) return Boolean
5793 is
5794 Ent : Entity_Id;
5795
5796 begin
5797 Ent := E2;
5798 while Present (Ent) loop
5799 if E1 = Ent then
5800 return True;
5801
5802 elsif Ekind (Ent) /= E_Constant then
5803 return False;
5804
5805 elsif Is_Entity_Name (Constant_Value (Ent)) then
5806 if Entity (Constant_Value (Ent)) = E1 then
5807 return True;
5808 else
5809 Ent := Entity (Constant_Value (Ent));
5810 end if;
5811
5812 -- The actual may be a constant that has been folded. Recover
5813 -- original name.
5814
5815 elsif Is_Entity_Name (Original_Node (Constant_Value (Ent))) then
5816 Ent := Entity (Original_Node (Constant_Value (Ent)));
5817
5818 else
5819 return False;
5820 end if;
5821 end loop;
5822
5823 return False;
5824 end Same_Instantiated_Constant;
5825
5826 --------------------------------
5827 -- Same_Instantiated_Function --
5828 --------------------------------
5829
5830 function Same_Instantiated_Function
5831 (E1, E2 : Entity_Id) return Boolean
5832 is
5833 U1, U2 : Node_Id;
5834 begin
5835 if Alias (E1) = Alias (E2) then
5836 return True;
5837
5838 elsif Present (Alias (E2)) then
5839 U1 := Original_Node (Unit_Declaration_Node (E1));
5840 U2 := Original_Node (Unit_Declaration_Node (Alias (E2)));
5841
5842 return Nkind (U1) = N_Subprogram_Renaming_Declaration
5843 and then Nkind (Name (U1)) = N_Attribute_Reference
5844
5845 and then Nkind (U2) = N_Subprogram_Renaming_Declaration
5846 and then Nkind (Name (U2)) = N_Attribute_Reference
5847
5848 and then
5849 Attribute_Name (Name (U1)) = Attribute_Name (Name (U2));
5850 else
5851 return False;
5852 end if;
5853 end Same_Instantiated_Function;
5854
5855 --------------------------------
5856 -- Same_Instantiated_Variable --
5857 --------------------------------
5858
5859 function Same_Instantiated_Variable
5860 (E1, E2 : Entity_Id) return Boolean
5861 is
5862 function Original_Entity (E : Entity_Id) return Entity_Id;
5863 -- Follow chain of renamings to the ultimate ancestor
5864
5865 ---------------------
5866 -- Original_Entity --
5867 ---------------------
5868
5869 function Original_Entity (E : Entity_Id) return Entity_Id is
5870 Orig : Entity_Id;
5871
5872 begin
5873 Orig := E;
5874 while Nkind (Parent (Orig)) = N_Object_Renaming_Declaration
5875 and then Present (Renamed_Object (Orig))
5876 and then Is_Entity_Name (Renamed_Object (Orig))
5877 loop
5878 Orig := Entity (Renamed_Object (Orig));
5879 end loop;
5880
5881 return Orig;
5882 end Original_Entity;
5883
5884 -- Start of processing for Same_Instantiated_Variable
5885
5886 begin
5887 return Ekind (E1) = Ekind (E2)
5888 and then Original_Entity (E1) = Original_Entity (E2);
5889 end Same_Instantiated_Variable;
5890
5891 -- Start of processing for Check_Formal_Package_Instance
5892
5893 begin
5894 while Present (E1) and then Present (E2) loop
5895 exit when Ekind (E1) = E_Package
5896 and then Renamed_Entity (E1) = Renamed_Entity (Actual_Pack);
5897
5898 -- If the formal is the renaming of the formal package, this
5899 -- is the end of its formal part, which may occur before the
5900 -- end of the formal part in the actual in the presence of
5901 -- defaulted parameters in the formal package.
5902
5903 exit when Nkind (Parent (E2)) = N_Package_Renaming_Declaration
5904 and then Renamed_Entity (E2) = Scope (E2);
5905
5906 -- The analysis of the actual may generate additional internal
5907 -- entities. If the formal is defaulted, there is no corresponding
5908 -- analysis and the internal entities must be skipped, until we
5909 -- find corresponding entities again.
5910
5911 if Comes_From_Source (E2)
5912 and then not Comes_From_Source (E1)
5913 and then Chars (E1) /= Chars (E2)
5914 then
5915 while Present (E1) and then Chars (E1) /= Chars (E2) loop
5916 Next_Entity (E1);
5917 end loop;
5918 end if;
5919
5920 if No (E1) then
5921 return;
5922
5923 -- If the formal entity comes from a formal declaration, it was
5924 -- defaulted in the formal package, and no check is needed on it.
5925
5926 elsif Nkind (Parent (E2)) = N_Formal_Object_Declaration then
5927 goto Next_E;
5928
5929 -- Ditto for defaulted formal subprograms.
5930
5931 elsif Is_Overloadable (E1)
5932 and then Nkind (Unit_Declaration_Node (E2)) in
5933 N_Formal_Subprogram_Declaration
5934 then
5935 goto Next_E;
5936
5937 elsif Is_Type (E1) then
5938
5939 -- Subtypes must statically match. E1, E2 are the local entities
5940 -- that are subtypes of the actuals. Itypes generated for other
5941 -- parameters need not be checked, the check will be performed
5942 -- on the parameters themselves.
5943
5944 -- If E2 is a formal type declaration, it is a defaulted parameter
5945 -- and needs no checking.
5946
5947 if not Is_Itype (E1) and then not Is_Itype (E2) then
5948 Check_Mismatch
5949 (not Is_Type (E2)
5950 or else Etype (E1) /= Etype (E2)
5951 or else not Subtypes_Statically_Match (E1, E2));
5952 end if;
5953
5954 elsif Ekind (E1) = E_Constant then
5955
5956 -- IN parameters must denote the same static value, or the same
5957 -- constant, or the literal null.
5958
5959 Expr1 := Expression (Parent (E1));
5960
5961 if Ekind (E2) /= E_Constant then
5962 Check_Mismatch (True);
5963 goto Next_E;
5964 else
5965 Expr2 := Expression (Parent (E2));
5966 end if;
5967
5968 if Is_OK_Static_Expression (Expr1) then
5969 if not Is_OK_Static_Expression (Expr2) then
5970 Check_Mismatch (True);
5971
5972 elsif Is_Discrete_Type (Etype (E1)) then
5973 declare
5974 V1 : constant Uint := Expr_Value (Expr1);
5975 V2 : constant Uint := Expr_Value (Expr2);
5976 begin
5977 Check_Mismatch (V1 /= V2);
5978 end;
5979
5980 elsif Is_Real_Type (Etype (E1)) then
5981 declare
5982 V1 : constant Ureal := Expr_Value_R (Expr1);
5983 V2 : constant Ureal := Expr_Value_R (Expr2);
5984 begin
5985 Check_Mismatch (V1 /= V2);
5986 end;
5987
5988 elsif Is_String_Type (Etype (E1))
5989 and then Nkind (Expr1) = N_String_Literal
5990 then
5991 if Nkind (Expr2) /= N_String_Literal then
5992 Check_Mismatch (True);
5993 else
5994 Check_Mismatch
5995 (not String_Equal (Strval (Expr1), Strval (Expr2)));
5996 end if;
5997 end if;
5998
5999 elsif Is_Entity_Name (Expr1) then
6000 if Is_Entity_Name (Expr2) then
6001 if Entity (Expr1) = Entity (Expr2) then
6002 null;
6003 else
6004 Check_Mismatch
6005 (not Same_Instantiated_Constant
6006 (Entity (Expr1), Entity (Expr2)));
6007 end if;
6008
6009 else
6010 Check_Mismatch (True);
6011 end if;
6012
6013 elsif Is_Entity_Name (Original_Node (Expr1))
6014 and then Is_Entity_Name (Expr2)
6015 and then Same_Instantiated_Constant
6016 (Entity (Original_Node (Expr1)), Entity (Expr2))
6017 then
6018 null;
6019
6020 elsif Nkind (Expr1) = N_Null then
6021 Check_Mismatch (Nkind (Expr1) /= N_Null);
6022
6023 else
6024 Check_Mismatch (True);
6025 end if;
6026
6027 elsif Ekind (E1) = E_Variable then
6028 Check_Mismatch (not Same_Instantiated_Variable (E1, E2));
6029
6030 elsif Ekind (E1) = E_Package then
6031 Check_Mismatch
6032 (Ekind (E1) /= Ekind (E2)
6033 or else Renamed_Object (E1) /= Renamed_Object (E2));
6034
6035 elsif Is_Overloadable (E1) then
6036
6037 -- Verify that the actual subprograms match. Note that actuals
6038 -- that are attributes are rewritten as subprograms. If the
6039 -- subprogram in the formal package is defaulted, no check is
6040 -- needed. Note that this can only happen in Ada 2005 when the
6041 -- formal package can be partially parameterized.
6042
6043 if Nkind (Unit_Declaration_Node (E1)) =
6044 N_Subprogram_Renaming_Declaration
6045 and then From_Default (Unit_Declaration_Node (E1))
6046 then
6047 null;
6048
6049 -- If the formal package has an "others" box association that
6050 -- covers this formal, there is no need for a check either.
6051
6052 elsif Nkind (Unit_Declaration_Node (E2)) in
6053 N_Formal_Subprogram_Declaration
6054 and then Box_Present (Unit_Declaration_Node (E2))
6055 then
6056 null;
6057
6058 -- No check needed if subprogram is a defaulted null procedure
6059
6060 elsif No (Alias (E2))
6061 and then Ekind (E2) = E_Procedure
6062 and then
6063 Null_Present (Specification (Unit_Declaration_Node (E2)))
6064 then
6065 null;
6066
6067 -- Otherwise the actual in the formal and the actual in the
6068 -- instantiation of the formal must match, up to renamings.
6069
6070 else
6071 Check_Mismatch
6072 (Ekind (E2) /= Ekind (E1)
6073 or else not Same_Instantiated_Function (E1, E2));
6074 end if;
6075
6076 else
6077 raise Program_Error;
6078 end if;
6079
6080 <<Next_E>>
6081 Next_Entity (E1);
6082 Next_Entity (E2);
6083 end loop;
6084 end Check_Formal_Package_Instance;
6085
6086 ---------------------------
6087 -- Check_Formal_Packages --
6088 ---------------------------
6089
6090 procedure Check_Formal_Packages (P_Id : Entity_Id) is
6091 E : Entity_Id;
6092 Formal_P : Entity_Id;
6093 Formal_Decl : Node_Id;
6094
6095 begin
6096 -- Iterate through the declarations in the instance, looking for package
6097 -- renaming declarations that denote instances of formal packages. Stop
6098 -- when we find the renaming of the current package itself. The
6099 -- declaration for a formal package without a box is followed by an
6100 -- internal entity that repeats the instantiation.
6101
6102 E := First_Entity (P_Id);
6103 while Present (E) loop
6104 if Ekind (E) = E_Package then
6105 if Renamed_Object (E) = P_Id then
6106 exit;
6107
6108 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
6109 null;
6110
6111 else
6112 Formal_Decl := Parent (Associated_Formal_Package (E));
6113
6114 -- Nothing to check if the formal has a box or an others_clause
6115 -- (necessarily with a box).
6116
6117 if Box_Present (Formal_Decl) then
6118 null;
6119
6120 elsif Nkind (First (Generic_Associations (Formal_Decl))) =
6121 N_Others_Choice
6122 then
6123 -- The internal validating package was generated but formal
6124 -- and instance are known to be compatible.
6125
6126 Formal_P := Next_Entity (E);
6127 Remove (Unit_Declaration_Node (Formal_P));
6128
6129 else
6130 Formal_P := Next_Entity (E);
6131 Check_Formal_Package_Instance (Formal_P, E);
6132
6133 -- After checking, remove the internal validating package.
6134 -- It is only needed for semantic checks, and as it may
6135 -- contain generic formal declarations it should not reach
6136 -- gigi.
6137
6138 Remove (Unit_Declaration_Node (Formal_P));
6139 end if;
6140 end if;
6141 end if;
6142
6143 Next_Entity (E);
6144 end loop;
6145 end Check_Formal_Packages;
6146
6147 ---------------------------------
6148 -- Check_Forward_Instantiation --
6149 ---------------------------------
6150
6151 procedure Check_Forward_Instantiation (Decl : Node_Id) is
6152 S : Entity_Id;
6153 Gen_Comp : Entity_Id := Cunit_Entity (Get_Source_Unit (Decl));
6154
6155 begin
6156 -- The instantiation appears before the generic body if we are in the
6157 -- scope of the unit containing the generic, either in its spec or in
6158 -- the package body, and before the generic body.
6159
6160 if Ekind (Gen_Comp) = E_Package_Body then
6161 Gen_Comp := Spec_Entity (Gen_Comp);
6162 end if;
6163
6164 if In_Open_Scopes (Gen_Comp)
6165 and then No (Corresponding_Body (Decl))
6166 then
6167 S := Current_Scope;
6168
6169 while Present (S)
6170 and then not Is_Compilation_Unit (S)
6171 and then not Is_Child_Unit (S)
6172 loop
6173 if Ekind (S) = E_Package then
6174 Set_Has_Forward_Instantiation (S);
6175 end if;
6176
6177 S := Scope (S);
6178 end loop;
6179 end if;
6180 end Check_Forward_Instantiation;
6181
6182 ---------------------------
6183 -- Check_Generic_Actuals --
6184 ---------------------------
6185
6186 -- The visibility of the actuals may be different between the point of
6187 -- generic instantiation and the instantiation of the body.
6188
6189 procedure Check_Generic_Actuals
6190 (Instance : Entity_Id;
6191 Is_Formal_Box : Boolean)
6192 is
6193 E : Entity_Id;
6194 Astype : Entity_Id;
6195
6196 function Denotes_Previous_Actual (Typ : Entity_Id) return Boolean;
6197 -- For a formal that is an array type, the component type is often a
6198 -- previous formal in the same unit. The privacy status of the component
6199 -- type will have been examined earlier in the traversal of the
6200 -- corresponding actuals, and this status should not be modified for
6201 -- the array (sub)type itself. However, if the base type of the array
6202 -- (sub)type is private, its full view must be restored in the body to
6203 -- be consistent with subsequent index subtypes, etc.
6204 --
6205 -- To detect this case we have to rescan the list of formals, which is
6206 -- usually short enough to ignore the resulting inefficiency.
6207
6208 -----------------------------
6209 -- Denotes_Previous_Actual --
6210 -----------------------------
6211
6212 function Denotes_Previous_Actual (Typ : Entity_Id) return Boolean is
6213 Prev : Entity_Id;
6214
6215 begin
6216 Prev := First_Entity (Instance);
6217 while Present (Prev) loop
6218 if Is_Type (Prev)
6219 and then Nkind (Parent (Prev)) = N_Subtype_Declaration
6220 and then Is_Entity_Name (Subtype_Indication (Parent (Prev)))
6221 and then Entity (Subtype_Indication (Parent (Prev))) = Typ
6222 then
6223 return True;
6224
6225 elsif Prev = E then
6226 return False;
6227
6228 else
6229 Next_Entity (Prev);
6230 end if;
6231 end loop;
6232
6233 return False;
6234 end Denotes_Previous_Actual;
6235
6236 -- Start of processing for Check_Generic_Actuals
6237
6238 begin
6239 E := First_Entity (Instance);
6240 while Present (E) loop
6241 if Is_Type (E)
6242 and then Nkind (Parent (E)) = N_Subtype_Declaration
6243 and then Scope (Etype (E)) /= Instance
6244 and then Is_Entity_Name (Subtype_Indication (Parent (E)))
6245 then
6246 if Is_Array_Type (E)
6247 and then not Is_Private_Type (Etype (E))
6248 and then Denotes_Previous_Actual (Component_Type (E))
6249 then
6250 null;
6251 else
6252 Check_Private_View (Subtype_Indication (Parent (E)));
6253 end if;
6254
6255 Set_Is_Generic_Actual_Type (E, True);
6256 Set_Is_Hidden (E, False);
6257 Set_Is_Potentially_Use_Visible (E,
6258 In_Use (Instance));
6259
6260 -- We constructed the generic actual type as a subtype of the
6261 -- supplied type. This means that it normally would not inherit
6262 -- subtype specific attributes of the actual, which is wrong for
6263 -- the generic case.
6264
6265 Astype := Ancestor_Subtype (E);
6266
6267 if No (Astype) then
6268
6269 -- This can happen when E is an itype that is the full view of
6270 -- a private type completed, e.g. with a constrained array. In
6271 -- that case, use the first subtype, which will carry size
6272 -- information. The base type itself is unconstrained and will
6273 -- not carry it.
6274
6275 Astype := First_Subtype (E);
6276 end if;
6277
6278 Set_Size_Info (E, (Astype));
6279 Set_RM_Size (E, RM_Size (Astype));
6280 Set_First_Rep_Item (E, First_Rep_Item (Astype));
6281
6282 if Is_Discrete_Or_Fixed_Point_Type (E) then
6283 Set_RM_Size (E, RM_Size (Astype));
6284
6285 -- In nested instances, the base type of an access actual may
6286 -- itself be private, and need to be exchanged.
6287
6288 elsif Is_Access_Type (E)
6289 and then Is_Private_Type (Etype (E))
6290 then
6291 Check_Private_View
6292 (New_Occurrence_Of (Etype (E), Sloc (Instance)));
6293 end if;
6294
6295 elsif Ekind (E) = E_Package then
6296
6297 -- If this is the renaming for the current instance, we're done.
6298 -- Otherwise it is a formal package. If the corresponding formal
6299 -- was declared with a box, the (instantiations of the) generic
6300 -- formal part are also visible. Otherwise, ignore the entity
6301 -- created to validate the actuals.
6302
6303 if Renamed_Object (E) = Instance then
6304 exit;
6305
6306 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
6307 null;
6308
6309 -- The visibility of a formal of an enclosing generic is already
6310 -- correct.
6311
6312 elsif Denotes_Formal_Package (E) then
6313 null;
6314
6315 elsif Present (Associated_Formal_Package (E))
6316 and then not Is_Generic_Formal (E)
6317 then
6318 if Box_Present (Parent (Associated_Formal_Package (E))) then
6319 Check_Generic_Actuals (Renamed_Object (E), True);
6320
6321 else
6322 Check_Generic_Actuals (Renamed_Object (E), False);
6323 end if;
6324
6325 Set_Is_Hidden (E, False);
6326 end if;
6327
6328 -- If this is a subprogram instance (in a wrapper package) the
6329 -- actual is fully visible.
6330
6331 elsif Is_Wrapper_Package (Instance) then
6332 Set_Is_Hidden (E, False);
6333
6334 -- If the formal package is declared with a box, or if the formal
6335 -- parameter is defaulted, it is visible in the body.
6336
6337 elsif Is_Formal_Box or else Is_Visible_Formal (E) then
6338 Set_Is_Hidden (E, False);
6339 end if;
6340
6341 if Ekind (E) = E_Constant then
6342
6343 -- If the type of the actual is a private type declared in the
6344 -- enclosing scope of the generic unit, the body of the generic
6345 -- sees the full view of the type (because it has to appear in
6346 -- the corresponding package body). If the type is private now,
6347 -- exchange views to restore the proper visiblity in the instance.
6348
6349 declare
6350 Typ : constant Entity_Id := Base_Type (Etype (E));
6351 -- The type of the actual
6352
6353 Gen_Id : Entity_Id;
6354 -- The generic unit
6355
6356 Parent_Scope : Entity_Id;
6357 -- The enclosing scope of the generic unit
6358
6359 begin
6360 if Is_Wrapper_Package (Instance) then
6361 Gen_Id :=
6362 Generic_Parent
6363 (Specification
6364 (Unit_Declaration_Node
6365 (Related_Instance (Instance))));
6366 else
6367 Gen_Id :=
6368 Generic_Parent (Package_Specification (Instance));
6369 end if;
6370
6371 Parent_Scope := Scope (Gen_Id);
6372
6373 -- The exchange is only needed if the generic is defined
6374 -- within a package which is not a common ancestor of the
6375 -- scope of the instance, and is not already in scope.
6376
6377 if Is_Private_Type (Typ)
6378 and then Scope (Typ) = Parent_Scope
6379 and then Scope (Instance) /= Parent_Scope
6380 and then Ekind (Parent_Scope) = E_Package
6381 and then not Is_Child_Unit (Gen_Id)
6382 then
6383 Switch_View (Typ);
6384
6385 -- If the type of the entity is a subtype, it may also have
6386 -- to be made visible, together with the base type of its
6387 -- full view, after exchange.
6388
6389 if Is_Private_Type (Etype (E)) then
6390 Switch_View (Etype (E));
6391 Switch_View (Base_Type (Etype (E)));
6392 end if;
6393 end if;
6394 end;
6395 end if;
6396
6397 Next_Entity (E);
6398 end loop;
6399 end Check_Generic_Actuals;
6400
6401 ------------------------------
6402 -- Check_Generic_Child_Unit --
6403 ------------------------------
6404
6405 procedure Check_Generic_Child_Unit
6406 (Gen_Id : Node_Id;
6407 Parent_Installed : in out Boolean)
6408 is
6409 Loc : constant Source_Ptr := Sloc (Gen_Id);
6410 Gen_Par : Entity_Id := Empty;
6411 E : Entity_Id;
6412 Inst_Par : Entity_Id;
6413 S : Node_Id;
6414
6415 function Find_Generic_Child
6416 (Scop : Entity_Id;
6417 Id : Node_Id) return Entity_Id;
6418 -- Search generic parent for possible child unit with the given name
6419
6420 function In_Enclosing_Instance return Boolean;
6421 -- Within an instance of the parent, the child unit may be denoted by
6422 -- a simple name, or an abbreviated expanded name. Examine enclosing
6423 -- scopes to locate a possible parent instantiation.
6424
6425 ------------------------
6426 -- Find_Generic_Child --
6427 ------------------------
6428
6429 function Find_Generic_Child
6430 (Scop : Entity_Id;
6431 Id : Node_Id) return Entity_Id
6432 is
6433 E : Entity_Id;
6434
6435 begin
6436 -- If entity of name is already set, instance has already been
6437 -- resolved, e.g. in an enclosing instantiation.
6438
6439 if Present (Entity (Id)) then
6440 if Scope (Entity (Id)) = Scop then
6441 return Entity (Id);
6442 else
6443 return Empty;
6444 end if;
6445
6446 else
6447 E := First_Entity (Scop);
6448 while Present (E) loop
6449 if Chars (E) = Chars (Id)
6450 and then Is_Child_Unit (E)
6451 then
6452 if Is_Child_Unit (E)
6453 and then not Is_Visible_Lib_Unit (E)
6454 then
6455 Error_Msg_NE
6456 ("generic child unit& is not visible", Gen_Id, E);
6457 end if;
6458
6459 Set_Entity (Id, E);
6460 return E;
6461 end if;
6462
6463 Next_Entity (E);
6464 end loop;
6465
6466 return Empty;
6467 end if;
6468 end Find_Generic_Child;
6469
6470 ---------------------------
6471 -- In_Enclosing_Instance --
6472 ---------------------------
6473
6474 function In_Enclosing_Instance return Boolean is
6475 Enclosing_Instance : Node_Id;
6476 Instance_Decl : Node_Id;
6477
6478 begin
6479 -- We do not inline any call that contains instantiations, except
6480 -- for instantiations of Unchecked_Conversion, so if we are within
6481 -- an inlined body the current instance does not require parents.
6482
6483 if In_Inlined_Body then
6484 pragma Assert (Chars (Gen_Id) = Name_Unchecked_Conversion);
6485 return False;
6486 end if;
6487
6488 -- Loop to check enclosing scopes
6489
6490 Enclosing_Instance := Current_Scope;
6491 while Present (Enclosing_Instance) loop
6492 Instance_Decl := Unit_Declaration_Node (Enclosing_Instance);
6493
6494 if Ekind (Enclosing_Instance) = E_Package
6495 and then Is_Generic_Instance (Enclosing_Instance)
6496 and then Present
6497 (Generic_Parent (Specification (Instance_Decl)))
6498 then
6499 -- Check whether the generic we are looking for is a child of
6500 -- this instance.
6501
6502 E := Find_Generic_Child
6503 (Generic_Parent (Specification (Instance_Decl)), Gen_Id);
6504 exit when Present (E);
6505
6506 else
6507 E := Empty;
6508 end if;
6509
6510 Enclosing_Instance := Scope (Enclosing_Instance);
6511 end loop;
6512
6513 if No (E) then
6514
6515 -- Not a child unit
6516
6517 Analyze (Gen_Id);
6518 return False;
6519
6520 else
6521 Rewrite (Gen_Id,
6522 Make_Expanded_Name (Loc,
6523 Chars => Chars (E),
6524 Prefix => New_Occurrence_Of (Enclosing_Instance, Loc),
6525 Selector_Name => New_Occurrence_Of (E, Loc)));
6526
6527 Set_Entity (Gen_Id, E);
6528 Set_Etype (Gen_Id, Etype (E));
6529 Parent_Installed := False; -- Already in scope.
6530 return True;
6531 end if;
6532 end In_Enclosing_Instance;
6533
6534 -- Start of processing for Check_Generic_Child_Unit
6535
6536 begin
6537 -- If the name of the generic is given by a selected component, it may
6538 -- be the name of a generic child unit, and the prefix is the name of an
6539 -- instance of the parent, in which case the child unit must be visible.
6540 -- If this instance is not in scope, it must be placed there and removed
6541 -- after instantiation, because what is being instantiated is not the
6542 -- original child, but the corresponding child present in the instance
6543 -- of the parent.
6544
6545 -- If the child is instantiated within the parent, it can be given by
6546 -- a simple name. In this case the instance is already in scope, but
6547 -- the child generic must be recovered from the generic parent as well.
6548
6549 if Nkind (Gen_Id) = N_Selected_Component then
6550 S := Selector_Name (Gen_Id);
6551 Analyze (Prefix (Gen_Id));
6552 Inst_Par := Entity (Prefix (Gen_Id));
6553
6554 if Ekind (Inst_Par) = E_Package
6555 and then Present (Renamed_Object (Inst_Par))
6556 then
6557 Inst_Par := Renamed_Object (Inst_Par);
6558 end if;
6559
6560 if Ekind (Inst_Par) = E_Package then
6561 if Nkind (Parent (Inst_Par)) = N_Package_Specification then
6562 Gen_Par := Generic_Parent (Parent (Inst_Par));
6563
6564 elsif Nkind (Parent (Inst_Par)) = N_Defining_Program_Unit_Name
6565 and then
6566 Nkind (Parent (Parent (Inst_Par))) = N_Package_Specification
6567 then
6568 Gen_Par := Generic_Parent (Parent (Parent (Inst_Par)));
6569 end if;
6570
6571 elsif Ekind (Inst_Par) = E_Generic_Package
6572 and then Nkind (Parent (Gen_Id)) = N_Formal_Package_Declaration
6573 then
6574 -- A formal package may be a real child package, and not the
6575 -- implicit instance within a parent. In this case the child is
6576 -- not visible and has to be retrieved explicitly as well.
6577
6578 Gen_Par := Inst_Par;
6579 end if;
6580
6581 if Present (Gen_Par) then
6582
6583 -- The prefix denotes an instantiation. The entity itself may be a
6584 -- nested generic, or a child unit.
6585
6586 E := Find_Generic_Child (Gen_Par, S);
6587
6588 if Present (E) then
6589 Change_Selected_Component_To_Expanded_Name (Gen_Id);
6590 Set_Entity (Gen_Id, E);
6591 Set_Etype (Gen_Id, Etype (E));
6592 Set_Entity (S, E);
6593 Set_Etype (S, Etype (E));
6594
6595 -- Indicate that this is a reference to the parent
6596
6597 if In_Extended_Main_Source_Unit (Gen_Id) then
6598 Set_Is_Instantiated (Inst_Par);
6599 end if;
6600
6601 -- A common mistake is to replicate the naming scheme of a
6602 -- hierarchy by instantiating a generic child directly, rather
6603 -- than the implicit child in a parent instance:
6604
6605 -- generic .. package Gpar is ..
6606 -- generic .. package Gpar.Child is ..
6607 -- package Par is new Gpar ();
6608
6609 -- with Gpar.Child;
6610 -- package Par.Child is new Gpar.Child ();
6611 -- rather than Par.Child
6612
6613 -- In this case the instantiation is within Par, which is an
6614 -- instance, but Gpar does not denote Par because we are not IN
6615 -- the instance of Gpar, so this is illegal. The test below
6616 -- recognizes this particular case.
6617
6618 if Is_Child_Unit (E)
6619 and then not Comes_From_Source (Entity (Prefix (Gen_Id)))
6620 and then (not In_Instance
6621 or else Nkind (Parent (Parent (Gen_Id))) =
6622 N_Compilation_Unit)
6623 then
6624 Error_Msg_N
6625 ("prefix of generic child unit must be instance of parent",
6626 Gen_Id);
6627 end if;
6628
6629 if not In_Open_Scopes (Inst_Par)
6630 and then Nkind (Parent (Gen_Id)) not in
6631 N_Generic_Renaming_Declaration
6632 then
6633 Install_Parent (Inst_Par);
6634 Parent_Installed := True;
6635
6636 elsif In_Open_Scopes (Inst_Par) then
6637
6638 -- If the parent is already installed, install the actuals
6639 -- for its formal packages. This is necessary when the child
6640 -- instance is a child of the parent instance: in this case,
6641 -- the parent is placed on the scope stack but the formal
6642 -- packages are not made visible.
6643
6644 Install_Formal_Packages (Inst_Par);
6645 end if;
6646
6647 else
6648 -- If the generic parent does not contain an entity that
6649 -- corresponds to the selector, the instance doesn't either.
6650 -- Analyzing the node will yield the appropriate error message.
6651 -- If the entity is not a child unit, then it is an inner
6652 -- generic in the parent.
6653
6654 Analyze (Gen_Id);
6655 end if;
6656
6657 else
6658 Analyze (Gen_Id);
6659
6660 if Is_Child_Unit (Entity (Gen_Id))
6661 and then
6662 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
6663 and then not In_Open_Scopes (Inst_Par)
6664 then
6665 Install_Parent (Inst_Par);
6666 Parent_Installed := True;
6667
6668 -- The generic unit may be the renaming of the implicit child
6669 -- present in an instance. In that case the parent instance is
6670 -- obtained from the name of the renamed entity.
6671
6672 elsif Ekind (Entity (Gen_Id)) = E_Generic_Package
6673 and then Present (Renamed_Entity (Entity (Gen_Id)))
6674 and then Is_Child_Unit (Renamed_Entity (Entity (Gen_Id)))
6675 then
6676 declare
6677 Renamed_Package : constant Node_Id :=
6678 Name (Parent (Entity (Gen_Id)));
6679 begin
6680 if Nkind (Renamed_Package) = N_Expanded_Name then
6681 Inst_Par := Entity (Prefix (Renamed_Package));
6682 Install_Parent (Inst_Par);
6683 Parent_Installed := True;
6684 end if;
6685 end;
6686 end if;
6687 end if;
6688
6689 elsif Nkind (Gen_Id) = N_Expanded_Name then
6690
6691 -- Entity already present, analyze prefix, whose meaning may be
6692 -- an instance in the current context. If it is an instance of
6693 -- a relative within another, the proper parent may still have
6694 -- to be installed, if they are not of the same generation.
6695
6696 Analyze (Prefix (Gen_Id));
6697
6698 -- In the unlikely case that a local declaration hides the name
6699 -- of the parent package, locate it on the homonym chain. If the
6700 -- context is an instance of the parent, the renaming entity is
6701 -- flagged as such.
6702
6703 Inst_Par := Entity (Prefix (Gen_Id));
6704 while Present (Inst_Par)
6705 and then not Is_Package_Or_Generic_Package (Inst_Par)
6706 loop
6707 Inst_Par := Homonym (Inst_Par);
6708 end loop;
6709
6710 pragma Assert (Present (Inst_Par));
6711 Set_Entity (Prefix (Gen_Id), Inst_Par);
6712
6713 if In_Enclosing_Instance then
6714 null;
6715
6716 elsif Present (Entity (Gen_Id))
6717 and then Is_Child_Unit (Entity (Gen_Id))
6718 and then not In_Open_Scopes (Inst_Par)
6719 then
6720 Install_Parent (Inst_Par);
6721 Parent_Installed := True;
6722 end if;
6723
6724 elsif In_Enclosing_Instance then
6725
6726 -- The child unit is found in some enclosing scope
6727
6728 null;
6729
6730 else
6731 Analyze (Gen_Id);
6732
6733 -- If this is the renaming of the implicit child in a parent
6734 -- instance, recover the parent name and install it.
6735
6736 if Is_Entity_Name (Gen_Id) then
6737 E := Entity (Gen_Id);
6738
6739 if Is_Generic_Unit (E)
6740 and then Nkind (Parent (E)) in N_Generic_Renaming_Declaration
6741 and then Is_Child_Unit (Renamed_Object (E))
6742 and then Is_Generic_Unit (Scope (Renamed_Object (E)))
6743 and then Nkind (Name (Parent (E))) = N_Expanded_Name
6744 then
6745 Rewrite (Gen_Id, New_Copy_Tree (Name (Parent (E))));
6746 Inst_Par := Entity (Prefix (Gen_Id));
6747
6748 if not In_Open_Scopes (Inst_Par) then
6749 Install_Parent (Inst_Par);
6750 Parent_Installed := True;
6751 end if;
6752
6753 -- If it is a child unit of a non-generic parent, it may be
6754 -- use-visible and given by a direct name. Install parent as
6755 -- for other cases.
6756
6757 elsif Is_Generic_Unit (E)
6758 and then Is_Child_Unit (E)
6759 and then
6760 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
6761 and then not Is_Generic_Unit (Scope (E))
6762 then
6763 if not In_Open_Scopes (Scope (E)) then
6764 Install_Parent (Scope (E));
6765 Parent_Installed := True;
6766 end if;
6767 end if;
6768 end if;
6769 end if;
6770 end Check_Generic_Child_Unit;
6771
6772 -----------------------------
6773 -- Check_Hidden_Child_Unit --
6774 -----------------------------
6775
6776 procedure Check_Hidden_Child_Unit
6777 (N : Node_Id;
6778 Gen_Unit : Entity_Id;
6779 Act_Decl_Id : Entity_Id)
6780 is
6781 Gen_Id : constant Node_Id := Name (N);
6782
6783 begin
6784 if Is_Child_Unit (Gen_Unit)
6785 and then Is_Child_Unit (Act_Decl_Id)
6786 and then Nkind (Gen_Id) = N_Expanded_Name
6787 and then Entity (Prefix (Gen_Id)) = Scope (Act_Decl_Id)
6788 and then Chars (Gen_Unit) = Chars (Act_Decl_Id)
6789 then
6790 Error_Msg_Node_2 := Scope (Act_Decl_Id);
6791 Error_Msg_NE
6792 ("generic unit & is implicitly declared in &",
6793 Defining_Unit_Name (N), Gen_Unit);
6794 Error_Msg_N ("\instance must have different name",
6795 Defining_Unit_Name (N));
6796 end if;
6797 end Check_Hidden_Child_Unit;
6798
6799 ------------------------
6800 -- Check_Private_View --
6801 ------------------------
6802
6803 procedure Check_Private_View (N : Node_Id) is
6804 T : constant Entity_Id := Etype (N);
6805 BT : Entity_Id;
6806
6807 begin
6808 -- Exchange views if the type was not private in the generic but is
6809 -- private at the point of instantiation. Do not exchange views if
6810 -- the scope of the type is in scope. This can happen if both generic
6811 -- and instance are sibling units, or if type is defined in a parent.
6812 -- In this case the visibility of the type will be correct for all
6813 -- semantic checks.
6814
6815 if Present (T) then
6816 BT := Base_Type (T);
6817
6818 if Is_Private_Type (T)
6819 and then not Has_Private_View (N)
6820 and then Present (Full_View (T))
6821 and then not In_Open_Scopes (Scope (T))
6822 then
6823 -- In the generic, the full type was visible. Save the private
6824 -- entity, for subsequent exchange.
6825
6826 Switch_View (T);
6827
6828 elsif Has_Private_View (N)
6829 and then not Is_Private_Type (T)
6830 and then not Has_Been_Exchanged (T)
6831 and then Etype (Get_Associated_Node (N)) /= T
6832 then
6833 -- Only the private declaration was visible in the generic. If
6834 -- the type appears in a subtype declaration, the subtype in the
6835 -- instance must have a view compatible with that of its parent,
6836 -- which must be exchanged (see corresponding code in Restore_
6837 -- Private_Views). Otherwise, if the type is defined in a parent
6838 -- unit, leave full visibility within instance, which is safe.
6839
6840 if In_Open_Scopes (Scope (Base_Type (T)))
6841 and then not Is_Private_Type (Base_Type (T))
6842 and then Comes_From_Source (Base_Type (T))
6843 then
6844 null;
6845
6846 elsif Nkind (Parent (N)) = N_Subtype_Declaration
6847 or else not In_Private_Part (Scope (Base_Type (T)))
6848 then
6849 Prepend_Elmt (T, Exchanged_Views);
6850 Exchange_Declarations (Etype (Get_Associated_Node (N)));
6851 end if;
6852
6853 -- For composite types with inconsistent representation exchange
6854 -- component types accordingly.
6855
6856 elsif Is_Access_Type (T)
6857 and then Is_Private_Type (Designated_Type (T))
6858 and then not Has_Private_View (N)
6859 and then Present (Full_View (Designated_Type (T)))
6860 then
6861 Switch_View (Designated_Type (T));
6862
6863 elsif Is_Array_Type (T) then
6864 if Is_Private_Type (Component_Type (T))
6865 and then not Has_Private_View (N)
6866 and then Present (Full_View (Component_Type (T)))
6867 then
6868 Switch_View (Component_Type (T));
6869 end if;
6870
6871 -- The normal exchange mechanism relies on the setting of a
6872 -- flag on the reference in the generic. However, an additional
6873 -- mechanism is needed for types that are not explicitly
6874 -- mentioned in the generic, but may be needed in expanded code
6875 -- in the instance. This includes component types of arrays and
6876 -- designated types of access types. This processing must also
6877 -- include the index types of arrays which we take care of here.
6878
6879 declare
6880 Indx : Node_Id;
6881 Typ : Entity_Id;
6882
6883 begin
6884 Indx := First_Index (T);
6885 while Present (Indx) loop
6886 Typ := Base_Type (Etype (Indx));
6887
6888 if Is_Private_Type (Typ)
6889 and then Present (Full_View (Typ))
6890 then
6891 Switch_View (Typ);
6892 end if;
6893
6894 Next_Index (Indx);
6895 end loop;
6896 end;
6897
6898 elsif Is_Private_Type (T)
6899 and then Present (Full_View (T))
6900 and then Is_Array_Type (Full_View (T))
6901 and then Is_Private_Type (Component_Type (Full_View (T)))
6902 then
6903 Switch_View (T);
6904
6905 -- Finally, a non-private subtype may have a private base type, which
6906 -- must be exchanged for consistency. This can happen when a package
6907 -- body is instantiated, when the scope stack is empty but in fact
6908 -- the subtype and the base type are declared in an enclosing scope.
6909
6910 -- Note that in this case we introduce an inconsistency in the view
6911 -- set, because we switch the base type BT, but there could be some
6912 -- private dependent subtypes of BT which remain unswitched. Such
6913 -- subtypes might need to be switched at a later point (see specific
6914 -- provision for that case in Switch_View).
6915
6916 elsif not Is_Private_Type (T)
6917 and then not Has_Private_View (N)
6918 and then Is_Private_Type (BT)
6919 and then Present (Full_View (BT))
6920 and then not Is_Generic_Type (BT)
6921 and then not In_Open_Scopes (BT)
6922 then
6923 Prepend_Elmt (Full_View (BT), Exchanged_Views);
6924 Exchange_Declarations (BT);
6925 end if;
6926 end if;
6927 end Check_Private_View;
6928
6929 -----------------------------
6930 -- Check_Hidden_Primitives --
6931 -----------------------------
6932
6933 function Check_Hidden_Primitives (Assoc_List : List_Id) return Elist_Id is
6934 Actual : Node_Id;
6935 Gen_T : Entity_Id;
6936 Result : Elist_Id := No_Elist;
6937
6938 begin
6939 if No (Assoc_List) then
6940 return No_Elist;
6941 end if;
6942
6943 -- Traverse the list of associations between formals and actuals
6944 -- searching for renamings of tagged types
6945
6946 Actual := First (Assoc_List);
6947 while Present (Actual) loop
6948 if Nkind (Actual) = N_Subtype_Declaration then
6949 Gen_T := Generic_Parent_Type (Actual);
6950
6951 if Present (Gen_T) and then Is_Tagged_Type (Gen_T) then
6952
6953 -- Traverse the list of primitives of the actual types
6954 -- searching for hidden primitives that are visible in the
6955 -- corresponding generic formal; leave them visible and
6956 -- append them to Result to restore their decoration later.
6957
6958 Install_Hidden_Primitives
6959 (Prims_List => Result,
6960 Gen_T => Gen_T,
6961 Act_T => Entity (Subtype_Indication (Actual)));
6962 end if;
6963 end if;
6964
6965 Next (Actual);
6966 end loop;
6967
6968 return Result;
6969 end Check_Hidden_Primitives;
6970
6971 --------------------------
6972 -- Contains_Instance_Of --
6973 --------------------------
6974
6975 function Contains_Instance_Of
6976 (Inner : Entity_Id;
6977 Outer : Entity_Id;
6978 N : Node_Id) return Boolean
6979 is
6980 Elmt : Elmt_Id;
6981 Scop : Entity_Id;
6982
6983 begin
6984 Scop := Outer;
6985
6986 -- Verify that there are no circular instantiations. We check whether
6987 -- the unit contains an instance of the current scope or some enclosing
6988 -- scope (in case one of the instances appears in a subunit). Longer
6989 -- circularities involving subunits might seem too pathological to
6990 -- consider, but they were not too pathological for the authors of
6991 -- DEC bc30vsq, so we loop over all enclosing scopes, and mark all
6992 -- enclosing generic scopes as containing an instance.
6993
6994 loop
6995 -- Within a generic subprogram body, the scope is not generic, to
6996 -- allow for recursive subprograms. Use the declaration to determine
6997 -- whether this is a generic unit.
6998
6999 if Ekind (Scop) = E_Generic_Package
7000 or else (Is_Subprogram (Scop)
7001 and then Nkind (Unit_Declaration_Node (Scop)) =
7002 N_Generic_Subprogram_Declaration)
7003 then
7004 Elmt := First_Elmt (Inner_Instances (Inner));
7005
7006 while Present (Elmt) loop
7007 if Node (Elmt) = Scop then
7008 Error_Msg_Node_2 := Inner;
7009 Error_Msg_NE
7010 ("circular Instantiation: & instantiated within &!",
7011 N, Scop);
7012 return True;
7013
7014 elsif Node (Elmt) = Inner then
7015 return True;
7016
7017 elsif Contains_Instance_Of (Node (Elmt), Scop, N) then
7018 Error_Msg_Node_2 := Inner;
7019 Error_Msg_NE
7020 ("circular Instantiation: & instantiated within &!",
7021 N, Node (Elmt));
7022 return True;
7023 end if;
7024
7025 Next_Elmt (Elmt);
7026 end loop;
7027
7028 -- Indicate that Inner is being instantiated within Scop
7029
7030 Append_Elmt (Inner, Inner_Instances (Scop));
7031 end if;
7032
7033 if Scop = Standard_Standard then
7034 exit;
7035 else
7036 Scop := Scope (Scop);
7037 end if;
7038 end loop;
7039
7040 return False;
7041 end Contains_Instance_Of;
7042
7043 -----------------------
7044 -- Copy_Generic_Node --
7045 -----------------------
7046
7047 function Copy_Generic_Node
7048 (N : Node_Id;
7049 Parent_Id : Node_Id;
7050 Instantiating : Boolean) return Node_Id
7051 is
7052 Ent : Entity_Id;
7053 New_N : Node_Id;
7054
7055 function Copy_Generic_Descendant (D : Union_Id) return Union_Id;
7056 -- Check the given value of one of the Fields referenced by the current
7057 -- node to determine whether to copy it recursively. The field may hold
7058 -- a Node_Id, a List_Id, or an Elist_Id, or a plain value (Sloc, Uint,
7059 -- Char) in which case it need not be copied.
7060
7061 procedure Copy_Descendants;
7062 -- Common utility for various nodes
7063
7064 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id;
7065 -- Make copy of element list
7066
7067 function Copy_Generic_List
7068 (L : List_Id;
7069 Parent_Id : Node_Id) return List_Id;
7070 -- Apply Copy_Node recursively to the members of a node list
7071
7072 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean;
7073 -- True if an identifier is part of the defining program unit name of
7074 -- a child unit. The entity of such an identifier must be kept (for
7075 -- ASIS use) even though as the name of an enclosing generic it would
7076 -- otherwise not be preserved in the generic tree.
7077
7078 ----------------------
7079 -- Copy_Descendants --
7080 ----------------------
7081
7082 procedure Copy_Descendants is
7083 use Atree.Unchecked_Access;
7084 -- This code section is part of the implementation of an untyped
7085 -- tree traversal, so it needs direct access to node fields.
7086
7087 begin
7088 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
7089 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
7090 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
7091 Set_Field4 (New_N, Copy_Generic_Descendant (Field4 (N)));
7092 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
7093 end Copy_Descendants;
7094
7095 -----------------------------
7096 -- Copy_Generic_Descendant --
7097 -----------------------------
7098
7099 function Copy_Generic_Descendant (D : Union_Id) return Union_Id is
7100 begin
7101 if D = Union_Id (Empty) then
7102 return D;
7103
7104 elsif D in Node_Range then
7105 return Union_Id
7106 (Copy_Generic_Node (Node_Id (D), New_N, Instantiating));
7107
7108 elsif D in List_Range then
7109 return Union_Id (Copy_Generic_List (List_Id (D), New_N));
7110
7111 elsif D in Elist_Range then
7112 return Union_Id (Copy_Generic_Elist (Elist_Id (D)));
7113
7114 -- Nothing else is copyable (e.g. Uint values), return as is
7115
7116 else
7117 return D;
7118 end if;
7119 end Copy_Generic_Descendant;
7120
7121 ------------------------
7122 -- Copy_Generic_Elist --
7123 ------------------------
7124
7125 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id is
7126 M : Elmt_Id;
7127 L : Elist_Id;
7128
7129 begin
7130 if Present (E) then
7131 L := New_Elmt_List;
7132 M := First_Elmt (E);
7133 while Present (M) loop
7134 Append_Elmt
7135 (Copy_Generic_Node (Node (M), Empty, Instantiating), L);
7136 Next_Elmt (M);
7137 end loop;
7138
7139 return L;
7140
7141 else
7142 return No_Elist;
7143 end if;
7144 end Copy_Generic_Elist;
7145
7146 -----------------------
7147 -- Copy_Generic_List --
7148 -----------------------
7149
7150 function Copy_Generic_List
7151 (L : List_Id;
7152 Parent_Id : Node_Id) return List_Id
7153 is
7154 N : Node_Id;
7155 New_L : List_Id;
7156
7157 begin
7158 if Present (L) then
7159 New_L := New_List;
7160 Set_Parent (New_L, Parent_Id);
7161
7162 N := First (L);
7163 while Present (N) loop
7164 Append (Copy_Generic_Node (N, Empty, Instantiating), New_L);
7165 Next (N);
7166 end loop;
7167
7168 return New_L;
7169
7170 else
7171 return No_List;
7172 end if;
7173 end Copy_Generic_List;
7174
7175 ---------------------------
7176 -- In_Defining_Unit_Name --
7177 ---------------------------
7178
7179 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean is
7180 begin
7181 return
7182 Present (Parent (Nam))
7183 and then (Nkind (Parent (Nam)) = N_Defining_Program_Unit_Name
7184 or else
7185 (Nkind (Parent (Nam)) = N_Expanded_Name
7186 and then In_Defining_Unit_Name (Parent (Nam))));
7187 end In_Defining_Unit_Name;
7188
7189 -- Start of processing for Copy_Generic_Node
7190
7191 begin
7192 if N = Empty then
7193 return N;
7194 end if;
7195
7196 New_N := New_Copy (N);
7197
7198 -- Copy aspects if present
7199
7200 if Has_Aspects (N) then
7201 Set_Has_Aspects (New_N, False);
7202 Set_Aspect_Specifications
7203 (New_N, Copy_Generic_List (Aspect_Specifications (N), Parent_Id));
7204 end if;
7205
7206 if Instantiating then
7207 Adjust_Instantiation_Sloc (New_N, S_Adjustment);
7208 end if;
7209
7210 if not Is_List_Member (N) then
7211 Set_Parent (New_N, Parent_Id);
7212 end if;
7213
7214 -- Special casing for identifiers and other entity names and operators
7215
7216 if Nkind_In (New_N, N_Character_Literal,
7217 N_Expanded_Name,
7218 N_Identifier,
7219 N_Operator_Symbol)
7220 or else Nkind (New_N) in N_Op
7221 then
7222 if not Instantiating then
7223
7224 -- Link both nodes in order to assign subsequently the entity of
7225 -- the copy to the original node, in case this is a global
7226 -- reference.
7227
7228 Set_Associated_Node (N, New_N);
7229
7230 -- If we are within an instantiation, this is a nested generic
7231 -- that has already been analyzed at the point of definition.
7232 -- We must preserve references that were global to the enclosing
7233 -- parent at that point. Other occurrences, whether global or
7234 -- local to the current generic, must be resolved anew, so we
7235 -- reset the entity in the generic copy. A global reference has a
7236 -- smaller depth than the parent, or else the same depth in case
7237 -- both are distinct compilation units.
7238
7239 -- A child unit is implicitly declared within the enclosing parent
7240 -- but is in fact global to it, and must be preserved.
7241
7242 -- It is also possible for Current_Instantiated_Parent to be
7243 -- defined, and for this not to be a nested generic, namely if
7244 -- the unit is loaded through Rtsfind. In that case, the entity of
7245 -- New_N is only a link to the associated node, and not a defining
7246 -- occurrence.
7247
7248 -- The entities for parent units in the defining_program_unit of a
7249 -- generic child unit are established when the context of the unit
7250 -- is first analyzed, before the generic copy is made. They are
7251 -- preserved in the copy for use in ASIS queries.
7252
7253 Ent := Entity (New_N);
7254
7255 if No (Current_Instantiated_Parent.Gen_Id) then
7256 if No (Ent)
7257 or else Nkind (Ent) /= N_Defining_Identifier
7258 or else not In_Defining_Unit_Name (N)
7259 then
7260 Set_Associated_Node (New_N, Empty);
7261 end if;
7262
7263 elsif No (Ent)
7264 or else
7265 not Nkind_In (Ent, N_Defining_Identifier,
7266 N_Defining_Character_Literal,
7267 N_Defining_Operator_Symbol)
7268 or else No (Scope (Ent))
7269 or else
7270 (Scope (Ent) = Current_Instantiated_Parent.Gen_Id
7271 and then not Is_Child_Unit (Ent))
7272 or else
7273 (Scope_Depth (Scope (Ent)) >
7274 Scope_Depth (Current_Instantiated_Parent.Gen_Id)
7275 and then
7276 Get_Source_Unit (Ent) =
7277 Get_Source_Unit (Current_Instantiated_Parent.Gen_Id))
7278 then
7279 Set_Associated_Node (New_N, Empty);
7280 end if;
7281
7282 -- Case of instantiating identifier or some other name or operator
7283
7284 else
7285 -- If the associated node is still defined, the entity in it
7286 -- is global, and must be copied to the instance. If this copy
7287 -- is being made for a body to inline, it is applied to an
7288 -- instantiated tree, and the entity is already present and
7289 -- must be also preserved.
7290
7291 declare
7292 Assoc : constant Node_Id := Get_Associated_Node (N);
7293
7294 begin
7295 if Present (Assoc) then
7296 if Nkind (Assoc) = Nkind (N) then
7297 Set_Entity (New_N, Entity (Assoc));
7298 Check_Private_View (N);
7299
7300 -- The node is a reference to a global type and acts as the
7301 -- subtype mark of a qualified expression created in order
7302 -- to aid resolution of accidental overloading in instances.
7303 -- Since N is a reference to a type, the Associated_Node of
7304 -- N denotes an entity rather than another identifier. See
7305 -- Qualify_Universal_Operands for details.
7306
7307 elsif Nkind (N) = N_Identifier
7308 and then Nkind (Parent (N)) = N_Qualified_Expression
7309 and then Subtype_Mark (Parent (N)) = N
7310 and then Is_Qualified_Universal_Literal (Parent (N))
7311 then
7312 Set_Entity (New_N, Assoc);
7313
7314 -- The name in the call may be a selected component if the
7315 -- call has not been analyzed yet, as may be the case for
7316 -- pre/post conditions in a generic unit.
7317
7318 elsif Nkind (Assoc) = N_Function_Call
7319 and then Is_Entity_Name (Name (Assoc))
7320 then
7321 Set_Entity (New_N, Entity (Name (Assoc)));
7322
7323 elsif Nkind_In (Assoc, N_Defining_Identifier,
7324 N_Defining_Character_Literal,
7325 N_Defining_Operator_Symbol)
7326 and then Expander_Active
7327 then
7328 -- Inlining case: we are copying a tree that contains
7329 -- global entities, which are preserved in the copy to be
7330 -- used for subsequent inlining.
7331
7332 null;
7333
7334 else
7335 Set_Entity (New_N, Empty);
7336 end if;
7337 end if;
7338 end;
7339 end if;
7340
7341 -- For expanded name, we must copy the Prefix and Selector_Name
7342
7343 if Nkind (N) = N_Expanded_Name then
7344 Set_Prefix
7345 (New_N, Copy_Generic_Node (Prefix (N), New_N, Instantiating));
7346
7347 Set_Selector_Name (New_N,
7348 Copy_Generic_Node (Selector_Name (N), New_N, Instantiating));
7349
7350 -- For operators, we must copy the right operand
7351
7352 elsif Nkind (N) in N_Op then
7353 Set_Right_Opnd (New_N,
7354 Copy_Generic_Node (Right_Opnd (N), New_N, Instantiating));
7355
7356 -- And for binary operators, the left operand as well
7357
7358 if Nkind (N) in N_Binary_Op then
7359 Set_Left_Opnd (New_N,
7360 Copy_Generic_Node (Left_Opnd (N), New_N, Instantiating));
7361 end if;
7362 end if;
7363
7364 -- Establish a link between an entity from the generic template and the
7365 -- corresponding entity in the generic copy to be analyzed.
7366
7367 elsif Nkind (N) in N_Entity then
7368 if not Instantiating then
7369 Set_Associated_Entity (N, New_N);
7370 end if;
7371
7372 -- Clear any existing link the copy may inherit from the replicated
7373 -- generic template entity.
7374
7375 Set_Associated_Entity (New_N, Empty);
7376
7377 -- Special casing for stubs
7378
7379 elsif Nkind (N) in N_Body_Stub then
7380
7381 -- In any case, we must copy the specification or defining
7382 -- identifier as appropriate.
7383
7384 if Nkind (N) = N_Subprogram_Body_Stub then
7385 Set_Specification (New_N,
7386 Copy_Generic_Node (Specification (N), New_N, Instantiating));
7387
7388 else
7389 Set_Defining_Identifier (New_N,
7390 Copy_Generic_Node
7391 (Defining_Identifier (N), New_N, Instantiating));
7392 end if;
7393
7394 -- If we are not instantiating, then this is where we load and
7395 -- analyze subunits, i.e. at the point where the stub occurs. A
7396 -- more permissive system might defer this analysis to the point
7397 -- of instantiation, but this seems too complicated for now.
7398
7399 if not Instantiating then
7400 declare
7401 Subunit_Name : constant Unit_Name_Type := Get_Unit_Name (N);
7402 Subunit : Node_Id;
7403 Unum : Unit_Number_Type;
7404 New_Body : Node_Id;
7405
7406 begin
7407 -- Make sure that, if it is a subunit of the main unit that is
7408 -- preprocessed and if -gnateG is specified, the preprocessed
7409 -- file will be written.
7410
7411 Lib.Analysing_Subunit_Of_Main :=
7412 Lib.In_Extended_Main_Source_Unit (N);
7413 Unum :=
7414 Load_Unit
7415 (Load_Name => Subunit_Name,
7416 Required => False,
7417 Subunit => True,
7418 Error_Node => N);
7419 Lib.Analysing_Subunit_Of_Main := False;
7420
7421 -- If the proper body is not found, a warning message will be
7422 -- emitted when analyzing the stub, or later at the point of
7423 -- instantiation. Here we just leave the stub as is.
7424
7425 if Unum = No_Unit then
7426 Subunits_Missing := True;
7427 goto Subunit_Not_Found;
7428 end if;
7429
7430 Subunit := Cunit (Unum);
7431
7432 if Nkind (Unit (Subunit)) /= N_Subunit then
7433 Error_Msg_N
7434 ("found child unit instead of expected SEPARATE subunit",
7435 Subunit);
7436 Error_Msg_Sloc := Sloc (N);
7437 Error_Msg_N ("\to complete stub #", Subunit);
7438 goto Subunit_Not_Found;
7439 end if;
7440
7441 -- We must create a generic copy of the subunit, in order to
7442 -- perform semantic analysis on it, and we must replace the
7443 -- stub in the original generic unit with the subunit, in order
7444 -- to preserve non-local references within.
7445
7446 -- Only the proper body needs to be copied. Library_Unit and
7447 -- context clause are simply inherited by the generic copy.
7448 -- Note that the copy (which may be recursive if there are
7449 -- nested subunits) must be done first, before attaching it to
7450 -- the enclosing generic.
7451
7452 New_Body :=
7453 Copy_Generic_Node
7454 (Proper_Body (Unit (Subunit)),
7455 Empty, Instantiating => False);
7456
7457 -- Now place the original proper body in the original generic
7458 -- unit. This is a body, not a compilation unit.
7459
7460 Rewrite (N, Proper_Body (Unit (Subunit)));
7461 Set_Is_Compilation_Unit (Defining_Entity (N), False);
7462 Set_Was_Originally_Stub (N);
7463
7464 -- Finally replace the body of the subunit with its copy, and
7465 -- make this new subunit into the library unit of the generic
7466 -- copy, which does not have stubs any longer.
7467
7468 Set_Proper_Body (Unit (Subunit), New_Body);
7469 Set_Library_Unit (New_N, Subunit);
7470 Inherit_Context (Unit (Subunit), N);
7471 end;
7472
7473 -- If we are instantiating, this must be an error case, since
7474 -- otherwise we would have replaced the stub node by the proper body
7475 -- that corresponds. So just ignore it in the copy (i.e. we have
7476 -- copied it, and that is good enough).
7477
7478 else
7479 null;
7480 end if;
7481
7482 <<Subunit_Not_Found>> null;
7483
7484 -- If the node is a compilation unit, it is the subunit of a stub, which
7485 -- has been loaded already (see code below). In this case, the library
7486 -- unit field of N points to the parent unit (which is a compilation
7487 -- unit) and need not (and cannot) be copied.
7488
7489 -- When the proper body of the stub is analyzed, the library_unit link
7490 -- is used to establish the proper context (see sem_ch10).
7491
7492 -- The other fields of a compilation unit are copied as usual
7493
7494 elsif Nkind (N) = N_Compilation_Unit then
7495
7496 -- This code can only be executed when not instantiating, because in
7497 -- the copy made for an instantiation, the compilation unit node has
7498 -- disappeared at the point that a stub is replaced by its proper
7499 -- body.
7500
7501 pragma Assert (not Instantiating);
7502
7503 Set_Context_Items (New_N,
7504 Copy_Generic_List (Context_Items (N), New_N));
7505
7506 Set_Unit (New_N,
7507 Copy_Generic_Node (Unit (N), New_N, False));
7508
7509 Set_First_Inlined_Subprogram (New_N,
7510 Copy_Generic_Node
7511 (First_Inlined_Subprogram (N), New_N, False));
7512
7513 Set_Aux_Decls_Node (New_N,
7514 Copy_Generic_Node (Aux_Decls_Node (N), New_N, False));
7515
7516 -- For an assignment node, the assignment is known to be semantically
7517 -- legal if we are instantiating the template. This avoids incorrect
7518 -- diagnostics in generated code.
7519
7520 elsif Nkind (N) = N_Assignment_Statement then
7521
7522 -- Copy name and expression fields in usual manner
7523
7524 Set_Name (New_N,
7525 Copy_Generic_Node (Name (N), New_N, Instantiating));
7526
7527 Set_Expression (New_N,
7528 Copy_Generic_Node (Expression (N), New_N, Instantiating));
7529
7530 if Instantiating then
7531 Set_Assignment_OK (Name (New_N), True);
7532 end if;
7533
7534 elsif Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
7535 if not Instantiating then
7536 Set_Associated_Node (N, New_N);
7537
7538 else
7539 if Present (Get_Associated_Node (N))
7540 and then Nkind (Get_Associated_Node (N)) = Nkind (N)
7541 then
7542 -- In the generic the aggregate has some composite type. If at
7543 -- the point of instantiation the type has a private view,
7544 -- install the full view (and that of its ancestors, if any).
7545
7546 declare
7547 T : Entity_Id := (Etype (Get_Associated_Node (New_N)));
7548 Rt : Entity_Id;
7549
7550 begin
7551 if Present (T) and then Is_Private_Type (T) then
7552 Switch_View (T);
7553 end if;
7554
7555 if Present (T)
7556 and then Is_Tagged_Type (T)
7557 and then Is_Derived_Type (T)
7558 then
7559 Rt := Root_Type (T);
7560
7561 loop
7562 T := Etype (T);
7563
7564 if Is_Private_Type (T) then
7565 Switch_View (T);
7566 end if;
7567
7568 exit when T = Rt;
7569 end loop;
7570 end if;
7571 end;
7572 end if;
7573 end if;
7574
7575 -- Do not copy the associated node, which points to the generic copy
7576 -- of the aggregate.
7577
7578 declare
7579 use Atree.Unchecked_Access;
7580 -- This code section is part of the implementation of an untyped
7581 -- tree traversal, so it needs direct access to node fields.
7582
7583 begin
7584 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
7585 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
7586 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
7587 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
7588 end;
7589
7590 -- Allocators do not have an identifier denoting the access type, so we
7591 -- must locate it through the expression to check whether the views are
7592 -- consistent.
7593
7594 elsif Nkind (N) = N_Allocator
7595 and then Nkind (Expression (N)) = N_Qualified_Expression
7596 and then Is_Entity_Name (Subtype_Mark (Expression (N)))
7597 and then Instantiating
7598 then
7599 declare
7600 T : constant Node_Id :=
7601 Get_Associated_Node (Subtype_Mark (Expression (N)));
7602 Acc_T : Entity_Id;
7603
7604 begin
7605 if Present (T) then
7606
7607 -- Retrieve the allocator node in the generic copy
7608
7609 Acc_T := Etype (Parent (Parent (T)));
7610
7611 if Present (Acc_T) and then Is_Private_Type (Acc_T) then
7612 Switch_View (Acc_T);
7613 end if;
7614 end if;
7615
7616 Copy_Descendants;
7617 end;
7618
7619 -- For a proper body, we must catch the case of a proper body that
7620 -- replaces a stub. This represents the point at which a separate
7621 -- compilation unit, and hence template file, may be referenced, so we
7622 -- must make a new source instantiation entry for the template of the
7623 -- subunit, and ensure that all nodes in the subunit are adjusted using
7624 -- this new source instantiation entry.
7625
7626 elsif Nkind (N) in N_Proper_Body then
7627 declare
7628 Save_Adjustment : constant Sloc_Adjustment := S_Adjustment;
7629
7630 begin
7631 if Instantiating and then Was_Originally_Stub (N) then
7632 Create_Instantiation_Source
7633 (Instantiation_Node,
7634 Defining_Entity (N),
7635 False,
7636 S_Adjustment);
7637 end if;
7638
7639 -- Now copy the fields of the proper body, using the new
7640 -- adjustment factor if one was needed as per test above.
7641
7642 Copy_Descendants;
7643
7644 -- Restore the original adjustment factor in case changed
7645
7646 S_Adjustment := Save_Adjustment;
7647 end;
7648
7649 elsif Nkind (N) = N_Pragma and then Instantiating then
7650
7651 -- Do not copy Comment or Ident pragmas their content is relevant to
7652 -- the generic unit, not to the instantiating unit.
7653
7654 if Nam_In (Pragma_Name (N), Name_Comment, Name_Ident) then
7655 New_N := Make_Null_Statement (Sloc (N));
7656
7657 -- Do not copy pragmas generated from aspects because the pragmas do
7658 -- not carry any semantic information, plus they will be regenerated
7659 -- in the instance.
7660
7661 elsif From_Aspect_Specification (N) then
7662 New_N := Make_Null_Statement (Sloc (N));
7663
7664 else
7665 Copy_Descendants;
7666 end if;
7667
7668 elsif Nkind_In (N, N_Integer_Literal, N_Real_Literal) then
7669
7670 -- No descendant fields need traversing
7671
7672 null;
7673
7674 elsif Nkind (N) = N_String_Literal
7675 and then Present (Etype (N))
7676 and then Instantiating
7677 then
7678 -- If the string is declared in an outer scope, the string_literal
7679 -- subtype created for it may have the wrong scope. Force reanalysis
7680 -- of the constant to generate a new itype in the proper context.
7681
7682 Set_Etype (New_N, Empty);
7683 Set_Analyzed (New_N, False);
7684
7685 -- For the remaining nodes, copy their descendants recursively
7686
7687 else
7688 Copy_Descendants;
7689
7690 if Instantiating and then Nkind (N) = N_Subprogram_Body then
7691 Set_Generic_Parent (Specification (New_N), N);
7692
7693 -- Should preserve Corresponding_Spec??? (12.3(14))
7694 end if;
7695 end if;
7696
7697 -- Propagate dimensions if present, so that they are reflected in the
7698 -- instance.
7699
7700 if Nkind (N) in N_Has_Etype
7701 and then (Nkind (N) in N_Op or else Is_Entity_Name (N))
7702 and then Present (Etype (N))
7703 and then Is_Floating_Point_Type (Etype (N))
7704 and then Has_Dimension_System (Etype (N))
7705 then
7706 Copy_Dimensions (N, New_N);
7707 end if;
7708
7709 return New_N;
7710 end Copy_Generic_Node;
7711
7712 ----------------------------
7713 -- Denotes_Formal_Package --
7714 ----------------------------
7715
7716 function Denotes_Formal_Package
7717 (Pack : Entity_Id;
7718 On_Exit : Boolean := False;
7719 Instance : Entity_Id := Empty) return Boolean
7720 is
7721 Par : Entity_Id;
7722 Scop : constant Entity_Id := Scope (Pack);
7723 E : Entity_Id;
7724
7725 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean;
7726 -- The package in question may be an actual for a previous formal
7727 -- package P of the current instance, so examine its actuals as well.
7728 -- This must be recursive over other formal packages.
7729
7730 ----------------------------------
7731 -- Is_Actual_Of_Previous_Formal --
7732 ----------------------------------
7733
7734 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean is
7735 E1 : Entity_Id;
7736
7737 begin
7738 E1 := First_Entity (P);
7739 while Present (E1) and then E1 /= Instance loop
7740 if Ekind (E1) = E_Package
7741 and then Nkind (Parent (E1)) = N_Package_Renaming_Declaration
7742 then
7743 if Renamed_Object (E1) = Pack then
7744 return True;
7745
7746 elsif E1 = P or else Renamed_Object (E1) = P then
7747 return False;
7748
7749 elsif Is_Actual_Of_Previous_Formal (E1) then
7750 return True;
7751 end if;
7752 end if;
7753
7754 Next_Entity (E1);
7755 end loop;
7756
7757 return False;
7758 end Is_Actual_Of_Previous_Formal;
7759
7760 -- Start of processing for Denotes_Formal_Package
7761
7762 begin
7763 if On_Exit then
7764 Par :=
7765 Instance_Envs.Table
7766 (Instance_Envs.Last).Instantiated_Parent.Act_Id;
7767 else
7768 Par := Current_Instantiated_Parent.Act_Id;
7769 end if;
7770
7771 if Ekind (Scop) = E_Generic_Package
7772 or else Nkind (Unit_Declaration_Node (Scop)) =
7773 N_Generic_Subprogram_Declaration
7774 then
7775 return True;
7776
7777 elsif Nkind (Original_Node (Unit_Declaration_Node (Pack))) =
7778 N_Formal_Package_Declaration
7779 then
7780 return True;
7781
7782 elsif No (Par) then
7783 return False;
7784
7785 else
7786 -- Check whether this package is associated with a formal package of
7787 -- the enclosing instantiation. Iterate over the list of renamings.
7788
7789 E := First_Entity (Par);
7790 while Present (E) loop
7791 if Ekind (E) /= E_Package
7792 or else Nkind (Parent (E)) /= N_Package_Renaming_Declaration
7793 then
7794 null;
7795
7796 elsif Renamed_Object (E) = Par then
7797 return False;
7798
7799 elsif Renamed_Object (E) = Pack then
7800 return True;
7801
7802 elsif Is_Actual_Of_Previous_Formal (E) then
7803 return True;
7804
7805 end if;
7806
7807 Next_Entity (E);
7808 end loop;
7809
7810 return False;
7811 end if;
7812 end Denotes_Formal_Package;
7813
7814 -----------------
7815 -- End_Generic --
7816 -----------------
7817
7818 procedure End_Generic is
7819 begin
7820 -- ??? More things could be factored out in this routine. Should
7821 -- probably be done at a later stage.
7822
7823 Inside_A_Generic := Generic_Flags.Table (Generic_Flags.Last);
7824 Generic_Flags.Decrement_Last;
7825
7826 Expander_Mode_Restore;
7827 end End_Generic;
7828
7829 -------------
7830 -- Earlier --
7831 -------------
7832
7833 function Earlier (N1, N2 : Node_Id) return Boolean is
7834 procedure Find_Depth (P : in out Node_Id; D : in out Integer);
7835 -- Find distance from given node to enclosing compilation unit
7836
7837 ----------------
7838 -- Find_Depth --
7839 ----------------
7840
7841 procedure Find_Depth (P : in out Node_Id; D : in out Integer) is
7842 begin
7843 while Present (P)
7844 and then Nkind (P) /= N_Compilation_Unit
7845 loop
7846 P := True_Parent (P);
7847 D := D + 1;
7848 end loop;
7849 end Find_Depth;
7850
7851 -- Local declarations
7852
7853 D1 : Integer := 0;
7854 D2 : Integer := 0;
7855 P1 : Node_Id := N1;
7856 P2 : Node_Id := N2;
7857 T1 : Source_Ptr;
7858 T2 : Source_Ptr;
7859
7860 -- Start of processing for Earlier
7861
7862 begin
7863 Find_Depth (P1, D1);
7864 Find_Depth (P2, D2);
7865
7866 if P1 /= P2 then
7867 return False;
7868 else
7869 P1 := N1;
7870 P2 := N2;
7871 end if;
7872
7873 while D1 > D2 loop
7874 P1 := True_Parent (P1);
7875 D1 := D1 - 1;
7876 end loop;
7877
7878 while D2 > D1 loop
7879 P2 := True_Parent (P2);
7880 D2 := D2 - 1;
7881 end loop;
7882
7883 -- At this point P1 and P2 are at the same distance from the root.
7884 -- We examine their parents until we find a common declarative list.
7885 -- If we reach the root, N1 and N2 do not descend from the same
7886 -- declarative list (e.g. one is nested in the declarative part and
7887 -- the other is in a block in the statement part) and the earlier
7888 -- one is already frozen.
7889
7890 while not Is_List_Member (P1)
7891 or else not Is_List_Member (P2)
7892 or else List_Containing (P1) /= List_Containing (P2)
7893 loop
7894 P1 := True_Parent (P1);
7895 P2 := True_Parent (P2);
7896
7897 if Nkind (Parent (P1)) = N_Subunit then
7898 P1 := Corresponding_Stub (Parent (P1));
7899 end if;
7900
7901 if Nkind (Parent (P2)) = N_Subunit then
7902 P2 := Corresponding_Stub (Parent (P2));
7903 end if;
7904
7905 if P1 = P2 then
7906 return False;
7907 end if;
7908 end loop;
7909
7910 -- Expanded code usually shares the source location of the original
7911 -- construct it was generated for. This however may not necessarely
7912 -- reflect the true location of the code within the tree.
7913
7914 -- Before comparing the slocs of the two nodes, make sure that we are
7915 -- working with correct source locations. Assume that P1 is to the left
7916 -- of P2. If either one does not come from source, traverse the common
7917 -- list heading towards the other node and locate the first source
7918 -- statement.
7919
7920 -- P1 P2
7921 -- ----+===+===+--------------+===+===+----
7922 -- expanded code expanded code
7923
7924 if not Comes_From_Source (P1) then
7925 while Present (P1) loop
7926
7927 -- Neither P2 nor a source statement were located during the
7928 -- search. If we reach the end of the list, then P1 does not
7929 -- occur earlier than P2.
7930
7931 -- ---->
7932 -- start --- P2 ----- P1 --- end
7933
7934 if No (Next (P1)) then
7935 return False;
7936
7937 -- We encounter P2 while going to the right of the list. This
7938 -- means that P1 does indeed appear earlier.
7939
7940 -- ---->
7941 -- start --- P1 ===== P2 --- end
7942 -- expanded code in between
7943
7944 elsif P1 = P2 then
7945 return True;
7946
7947 -- No need to look any further since we have located a source
7948 -- statement.
7949
7950 elsif Comes_From_Source (P1) then
7951 exit;
7952 end if;
7953
7954 -- Keep going right
7955
7956 Next (P1);
7957 end loop;
7958 end if;
7959
7960 if not Comes_From_Source (P2) then
7961 while Present (P2) loop
7962
7963 -- Neither P1 nor a source statement were located during the
7964 -- search. If we reach the start of the list, then P1 does not
7965 -- occur earlier than P2.
7966
7967 -- <----
7968 -- start --- P2 --- P1 --- end
7969
7970 if No (Prev (P2)) then
7971 return False;
7972
7973 -- We encounter P1 while going to the left of the list. This
7974 -- means that P1 does indeed appear earlier.
7975
7976 -- <----
7977 -- start --- P1 ===== P2 --- end
7978 -- expanded code in between
7979
7980 elsif P2 = P1 then
7981 return True;
7982
7983 -- No need to look any further since we have located a source
7984 -- statement.
7985
7986 elsif Comes_From_Source (P2) then
7987 exit;
7988 end if;
7989
7990 -- Keep going left
7991
7992 Prev (P2);
7993 end loop;
7994 end if;
7995
7996 -- At this point either both nodes came from source or we approximated
7997 -- their source locations through neighboring source statements.
7998
7999 T1 := Top_Level_Location (Sloc (P1));
8000 T2 := Top_Level_Location (Sloc (P2));
8001
8002 -- When two nodes come from the same instance, they have identical top
8003 -- level locations. To determine proper relation within the tree, check
8004 -- their locations within the template.
8005
8006 if T1 = T2 then
8007 return Sloc (P1) < Sloc (P2);
8008
8009 -- The two nodes either come from unrelated instances or do not come
8010 -- from instantiated code at all.
8011
8012 else
8013 return T1 < T2;
8014 end if;
8015 end Earlier;
8016
8017 ----------------------
8018 -- Find_Actual_Type --
8019 ----------------------
8020
8021 function Find_Actual_Type
8022 (Typ : Entity_Id;
8023 Gen_Type : Entity_Id) return Entity_Id
8024 is
8025 Gen_Scope : constant Entity_Id := Scope (Gen_Type);
8026 T : Entity_Id;
8027
8028 begin
8029 -- Special processing only applies to child units
8030
8031 if not Is_Child_Unit (Gen_Scope) then
8032 return Get_Instance_Of (Typ);
8033
8034 -- If designated or component type is itself a formal of the child unit,
8035 -- its instance is available.
8036
8037 elsif Scope (Typ) = Gen_Scope then
8038 return Get_Instance_Of (Typ);
8039
8040 -- If the array or access type is not declared in the parent unit,
8041 -- no special processing needed.
8042
8043 elsif not Is_Generic_Type (Typ)
8044 and then Scope (Gen_Scope) /= Scope (Typ)
8045 then
8046 return Get_Instance_Of (Typ);
8047
8048 -- Otherwise, retrieve designated or component type by visibility
8049
8050 else
8051 T := Current_Entity (Typ);
8052 while Present (T) loop
8053 if In_Open_Scopes (Scope (T)) then
8054 return T;
8055 elsif Is_Generic_Actual_Type (T) then
8056 return T;
8057 end if;
8058
8059 T := Homonym (T);
8060 end loop;
8061
8062 return Typ;
8063 end if;
8064 end Find_Actual_Type;
8065
8066 ----------------------------
8067 -- Freeze_Subprogram_Body --
8068 ----------------------------
8069
8070 procedure Freeze_Subprogram_Body
8071 (Inst_Node : Node_Id;
8072 Gen_Body : Node_Id;
8073 Pack_Id : Entity_Id)
8074 is
8075 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
8076 Par : constant Entity_Id := Scope (Gen_Unit);
8077 E_G_Id : Entity_Id;
8078 Enc_G : Entity_Id;
8079 Enc_I : Node_Id;
8080 F_Node : Node_Id;
8081
8082 function Enclosing_Package_Body (N : Node_Id) return Node_Id;
8083 -- Find innermost package body that encloses the given node, and which
8084 -- is not a compilation unit. Freeze nodes for the instance, or for its
8085 -- enclosing body, may be inserted after the enclosing_body of the
8086 -- generic unit. Used to determine proper placement of freeze node for
8087 -- both package and subprogram instances.
8088
8089 function Package_Freeze_Node (B : Node_Id) return Node_Id;
8090 -- Find entity for given package body, and locate or create a freeze
8091 -- node for it.
8092
8093 ----------------------------
8094 -- Enclosing_Package_Body --
8095 ----------------------------
8096
8097 function Enclosing_Package_Body (N : Node_Id) return Node_Id is
8098 P : Node_Id;
8099
8100 begin
8101 P := Parent (N);
8102 while Present (P)
8103 and then Nkind (Parent (P)) /= N_Compilation_Unit
8104 loop
8105 if Nkind (P) = N_Package_Body then
8106 if Nkind (Parent (P)) = N_Subunit then
8107 return Corresponding_Stub (Parent (P));
8108 else
8109 return P;
8110 end if;
8111 end if;
8112
8113 P := True_Parent (P);
8114 end loop;
8115
8116 return Empty;
8117 end Enclosing_Package_Body;
8118
8119 -------------------------
8120 -- Package_Freeze_Node --
8121 -------------------------
8122
8123 function Package_Freeze_Node (B : Node_Id) return Node_Id is
8124 Id : Entity_Id;
8125
8126 begin
8127 if Nkind (B) = N_Package_Body then
8128 Id := Corresponding_Spec (B);
8129 else pragma Assert (Nkind (B) = N_Package_Body_Stub);
8130 Id := Corresponding_Spec (Proper_Body (Unit (Library_Unit (B))));
8131 end if;
8132
8133 Ensure_Freeze_Node (Id);
8134 return Freeze_Node (Id);
8135 end Package_Freeze_Node;
8136
8137 -- Start of processing for Freeze_Subprogram_Body
8138
8139 begin
8140 -- If the instance and the generic body appear within the same unit, and
8141 -- the instance precedes the generic, the freeze node for the instance
8142 -- must appear after that of the generic. If the generic is nested
8143 -- within another instance I2, then current instance must be frozen
8144 -- after I2. In both cases, the freeze nodes are those of enclosing
8145 -- packages. Otherwise, the freeze node is placed at the end of the
8146 -- current declarative part.
8147
8148 Enc_G := Enclosing_Package_Body (Gen_Body);
8149 Enc_I := Enclosing_Package_Body (Inst_Node);
8150 Ensure_Freeze_Node (Pack_Id);
8151 F_Node := Freeze_Node (Pack_Id);
8152
8153 if Is_Generic_Instance (Par)
8154 and then Present (Freeze_Node (Par))
8155 and then In_Same_Declarative_Part (Freeze_Node (Par), Inst_Node)
8156 then
8157 -- The parent was a premature instantiation. Insert freeze node at
8158 -- the end the current declarative part.
8159
8160 if ABE_Is_Certain (Get_Package_Instantiation_Node (Par)) then
8161 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8162
8163 -- Handle the following case:
8164 --
8165 -- package Parent_Inst is new ...
8166 -- Parent_Inst []
8167 --
8168 -- procedure P ... -- this body freezes Parent_Inst
8169 --
8170 -- package Inst is new ...
8171 --
8172 -- In this particular scenario, the freeze node for Inst must be
8173 -- inserted in the same manner as that of Parent_Inst - before the
8174 -- next source body or at the end of the declarative list (body not
8175 -- available). If body P did not exist and Parent_Inst was frozen
8176 -- after Inst, either by a body following Inst or at the end of the
8177 -- declarative region, the freeze node for Inst must be inserted
8178 -- after that of Parent_Inst. This relation is established by
8179 -- comparing the Slocs of Parent_Inst freeze node and Inst.
8180
8181 elsif List_Containing (Get_Package_Instantiation_Node (Par)) =
8182 List_Containing (Inst_Node)
8183 and then Sloc (Freeze_Node (Par)) < Sloc (Inst_Node)
8184 then
8185 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8186
8187 else
8188 Insert_After (Freeze_Node (Par), F_Node);
8189 end if;
8190
8191 -- The body enclosing the instance should be frozen after the body that
8192 -- includes the generic, because the body of the instance may make
8193 -- references to entities therein. If the two are not in the same
8194 -- declarative part, or if the one enclosing the instance is frozen
8195 -- already, freeze the instance at the end of the current declarative
8196 -- part.
8197
8198 elsif Is_Generic_Instance (Par)
8199 and then Present (Freeze_Node (Par))
8200 and then Present (Enc_I)
8201 then
8202 if In_Same_Declarative_Part (Freeze_Node (Par), Enc_I)
8203 or else
8204 (Nkind (Enc_I) = N_Package_Body
8205 and then
8206 In_Same_Declarative_Part (Freeze_Node (Par), Parent (Enc_I)))
8207 then
8208 -- The enclosing package may contain several instances. Rather
8209 -- than computing the earliest point at which to insert its freeze
8210 -- node, we place it at the end of the declarative part of the
8211 -- parent of the generic.
8212
8213 Insert_Freeze_Node_For_Instance
8214 (Freeze_Node (Par), Package_Freeze_Node (Enc_I));
8215 end if;
8216
8217 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8218
8219 elsif Present (Enc_G)
8220 and then Present (Enc_I)
8221 and then Enc_G /= Enc_I
8222 and then Earlier (Inst_Node, Gen_Body)
8223 then
8224 if Nkind (Enc_G) = N_Package_Body then
8225 E_G_Id :=
8226 Corresponding_Spec (Enc_G);
8227 else pragma Assert (Nkind (Enc_G) = N_Package_Body_Stub);
8228 E_G_Id :=
8229 Corresponding_Spec (Proper_Body (Unit (Library_Unit (Enc_G))));
8230 end if;
8231
8232 -- Freeze package that encloses instance, and place node after the
8233 -- package that encloses generic. If enclosing package is already
8234 -- frozen we have to assume it is at the proper place. This may be a
8235 -- potential ABE that requires dynamic checking. Do not add a freeze
8236 -- node if the package that encloses the generic is inside the body
8237 -- that encloses the instance, because the freeze node would be in
8238 -- the wrong scope. Additional contortions needed if the bodies are
8239 -- within a subunit.
8240
8241 declare
8242 Enclosing_Body : Node_Id;
8243
8244 begin
8245 if Nkind (Enc_I) = N_Package_Body_Stub then
8246 Enclosing_Body := Proper_Body (Unit (Library_Unit (Enc_I)));
8247 else
8248 Enclosing_Body := Enc_I;
8249 end if;
8250
8251 if Parent (List_Containing (Enc_G)) /= Enclosing_Body then
8252 Insert_Freeze_Node_For_Instance
8253 (Enc_G, Package_Freeze_Node (Enc_I));
8254 end if;
8255 end;
8256
8257 -- Freeze enclosing subunit before instance
8258
8259 Ensure_Freeze_Node (E_G_Id);
8260
8261 if not Is_List_Member (Freeze_Node (E_G_Id)) then
8262 Insert_After (Enc_G, Freeze_Node (E_G_Id));
8263 end if;
8264
8265 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8266
8267 else
8268 -- If none of the above, insert freeze node at the end of the current
8269 -- declarative part.
8270
8271 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8272 end if;
8273 end Freeze_Subprogram_Body;
8274
8275 ----------------
8276 -- Get_Gen_Id --
8277 ----------------
8278
8279 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id is
8280 begin
8281 return Generic_Renamings.Table (E).Gen_Id;
8282 end Get_Gen_Id;
8283
8284 ---------------------
8285 -- Get_Instance_Of --
8286 ---------------------
8287
8288 function Get_Instance_Of (A : Entity_Id) return Entity_Id is
8289 Res : constant Assoc_Ptr := Generic_Renamings_HTable.Get (A);
8290
8291 begin
8292 if Res /= Assoc_Null then
8293 return Generic_Renamings.Table (Res).Act_Id;
8294
8295 else
8296 -- On exit, entity is not instantiated: not a generic parameter, or
8297 -- else parameter of an inner generic unit.
8298
8299 return A;
8300 end if;
8301 end Get_Instance_Of;
8302
8303 ------------------------------------
8304 -- Get_Package_Instantiation_Node --
8305 ------------------------------------
8306
8307 function Get_Package_Instantiation_Node (A : Entity_Id) return Node_Id is
8308 Decl : Node_Id := Unit_Declaration_Node (A);
8309 Inst : Node_Id;
8310
8311 begin
8312 -- If the Package_Instantiation attribute has been set on the package
8313 -- entity, then use it directly when it (or its Original_Node) refers
8314 -- to an N_Package_Instantiation node. In principle it should be
8315 -- possible to have this field set in all cases, which should be
8316 -- investigated, and would allow this function to be significantly
8317 -- simplified. ???
8318
8319 Inst := Package_Instantiation (A);
8320
8321 if Present (Inst) then
8322 if Nkind (Inst) = N_Package_Instantiation then
8323 return Inst;
8324
8325 elsif Nkind (Original_Node (Inst)) = N_Package_Instantiation then
8326 return Original_Node (Inst);
8327 end if;
8328 end if;
8329
8330 -- If the instantiation is a compilation unit that does not need body
8331 -- then the instantiation node has been rewritten as a package
8332 -- declaration for the instance, and we return the original node.
8333
8334 -- If it is a compilation unit and the instance node has not been
8335 -- rewritten, then it is still the unit of the compilation. Finally, if
8336 -- a body is present, this is a parent of the main unit whose body has
8337 -- been compiled for inlining purposes, and the instantiation node has
8338 -- been rewritten with the instance body.
8339
8340 -- Otherwise the instantiation node appears after the declaration. If
8341 -- the entity is a formal package, the declaration may have been
8342 -- rewritten as a generic declaration (in the case of a formal with box)
8343 -- or left as a formal package declaration if it has actuals, and is
8344 -- found with a forward search.
8345
8346 if Nkind (Parent (Decl)) = N_Compilation_Unit then
8347 if Nkind (Decl) = N_Package_Declaration
8348 and then Present (Corresponding_Body (Decl))
8349 then
8350 Decl := Unit_Declaration_Node (Corresponding_Body (Decl));
8351 end if;
8352
8353 if Nkind (Original_Node (Decl)) = N_Package_Instantiation then
8354 return Original_Node (Decl);
8355 else
8356 return Unit (Parent (Decl));
8357 end if;
8358
8359 elsif Nkind (Decl) = N_Package_Declaration
8360 and then Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration
8361 then
8362 return Original_Node (Decl);
8363
8364 else
8365 Inst := Next (Decl);
8366 while not Nkind_In (Inst, N_Package_Instantiation,
8367 N_Formal_Package_Declaration)
8368 loop
8369 Next (Inst);
8370 end loop;
8371
8372 return Inst;
8373 end if;
8374 end Get_Package_Instantiation_Node;
8375
8376 ------------------------
8377 -- Has_Been_Exchanged --
8378 ------------------------
8379
8380 function Has_Been_Exchanged (E : Entity_Id) return Boolean is
8381 Next : Elmt_Id;
8382
8383 begin
8384 Next := First_Elmt (Exchanged_Views);
8385 while Present (Next) loop
8386 if Full_View (Node (Next)) = E then
8387 return True;
8388 end if;
8389
8390 Next_Elmt (Next);
8391 end loop;
8392
8393 return False;
8394 end Has_Been_Exchanged;
8395
8396 ----------
8397 -- Hash --
8398 ----------
8399
8400 function Hash (F : Entity_Id) return HTable_Range is
8401 begin
8402 return HTable_Range (F mod HTable_Size);
8403 end Hash;
8404
8405 ------------------------
8406 -- Hide_Current_Scope --
8407 ------------------------
8408
8409 procedure Hide_Current_Scope is
8410 C : constant Entity_Id := Current_Scope;
8411 E : Entity_Id;
8412
8413 begin
8414 Set_Is_Hidden_Open_Scope (C);
8415
8416 E := First_Entity (C);
8417 while Present (E) loop
8418 if Is_Immediately_Visible (E) then
8419 Set_Is_Immediately_Visible (E, False);
8420 Append_Elmt (E, Hidden_Entities);
8421 end if;
8422
8423 Next_Entity (E);
8424 end loop;
8425
8426 -- Make the scope name invisible as well. This is necessary, but might
8427 -- conflict with calls to Rtsfind later on, in case the scope is a
8428 -- predefined one. There is no clean solution to this problem, so for
8429 -- now we depend on the user not redefining Standard itself in one of
8430 -- the parent units.
8431
8432 if Is_Immediately_Visible (C) and then C /= Standard_Standard then
8433 Set_Is_Immediately_Visible (C, False);
8434 Append_Elmt (C, Hidden_Entities);
8435 end if;
8436
8437 end Hide_Current_Scope;
8438
8439 --------------
8440 -- Init_Env --
8441 --------------
8442
8443 procedure Init_Env is
8444 Saved : Instance_Env;
8445
8446 begin
8447 Saved.Instantiated_Parent := Current_Instantiated_Parent;
8448 Saved.Exchanged_Views := Exchanged_Views;
8449 Saved.Hidden_Entities := Hidden_Entities;
8450 Saved.Current_Sem_Unit := Current_Sem_Unit;
8451 Saved.Parent_Unit_Visible := Parent_Unit_Visible;
8452 Saved.Instance_Parent_Unit := Instance_Parent_Unit;
8453
8454 -- Save configuration switches. These may be reset if the unit is a
8455 -- predefined unit, and the current mode is not Ada 2005.
8456
8457 Save_Opt_Config_Switches (Saved.Switches);
8458
8459 Instance_Envs.Append (Saved);
8460
8461 Exchanged_Views := New_Elmt_List;
8462 Hidden_Entities := New_Elmt_List;
8463
8464 -- Make dummy entry for Instantiated parent. If generic unit is legal,
8465 -- this is set properly in Set_Instance_Env.
8466
8467 Current_Instantiated_Parent :=
8468 (Current_Scope, Current_Scope, Assoc_Null);
8469 end Init_Env;
8470
8471 ------------------------------
8472 -- In_Same_Declarative_Part --
8473 ------------------------------
8474
8475 function In_Same_Declarative_Part
8476 (F_Node : Node_Id;
8477 Inst : Node_Id) return Boolean
8478 is
8479 Decls : constant Node_Id := Parent (F_Node);
8480 Nod : Node_Id;
8481
8482 begin
8483 Nod := Parent (Inst);
8484 while Present (Nod) loop
8485 if Nod = Decls then
8486 return True;
8487
8488 elsif Nkind_In (Nod, N_Subprogram_Body,
8489 N_Package_Body,
8490 N_Package_Declaration,
8491 N_Task_Body,
8492 N_Protected_Body,
8493 N_Block_Statement)
8494 then
8495 return False;
8496
8497 elsif Nkind (Nod) = N_Subunit then
8498 Nod := Corresponding_Stub (Nod);
8499
8500 elsif Nkind (Nod) = N_Compilation_Unit then
8501 return False;
8502
8503 else
8504 Nod := Parent (Nod);
8505 end if;
8506 end loop;
8507
8508 return False;
8509 end In_Same_Declarative_Part;
8510
8511 ---------------------
8512 -- In_Main_Context --
8513 ---------------------
8514
8515 function In_Main_Context (E : Entity_Id) return Boolean is
8516 Context : List_Id;
8517 Clause : Node_Id;
8518 Nam : Node_Id;
8519
8520 begin
8521 if not Is_Compilation_Unit (E)
8522 or else Ekind (E) /= E_Package
8523 or else In_Private_Part (E)
8524 then
8525 return False;
8526 end if;
8527
8528 Context := Context_Items (Cunit (Main_Unit));
8529
8530 Clause := First (Context);
8531 while Present (Clause) loop
8532 if Nkind (Clause) = N_With_Clause then
8533 Nam := Name (Clause);
8534
8535 -- If the current scope is part of the context of the main unit,
8536 -- analysis of the corresponding with_clause is not complete, and
8537 -- the entity is not set. We use the Chars field directly, which
8538 -- might produce false positives in rare cases, but guarantees
8539 -- that we produce all the instance bodies we will need.
8540
8541 if (Is_Entity_Name (Nam) and then Chars (Nam) = Chars (E))
8542 or else (Nkind (Nam) = N_Selected_Component
8543 and then Chars (Selector_Name (Nam)) = Chars (E))
8544 then
8545 return True;
8546 end if;
8547 end if;
8548
8549 Next (Clause);
8550 end loop;
8551
8552 return False;
8553 end In_Main_Context;
8554
8555 ---------------------
8556 -- Inherit_Context --
8557 ---------------------
8558
8559 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id) is
8560 Current_Context : List_Id;
8561 Current_Unit : Node_Id;
8562 Item : Node_Id;
8563 New_I : Node_Id;
8564
8565 Clause : Node_Id;
8566 OK : Boolean;
8567 Lib_Unit : Node_Id;
8568
8569 begin
8570 if Nkind (Parent (Gen_Decl)) = N_Compilation_Unit then
8571
8572 -- The inherited context is attached to the enclosing compilation
8573 -- unit. This is either the main unit, or the declaration for the
8574 -- main unit (in case the instantiation appears within the package
8575 -- declaration and the main unit is its body).
8576
8577 Current_Unit := Parent (Inst);
8578 while Present (Current_Unit)
8579 and then Nkind (Current_Unit) /= N_Compilation_Unit
8580 loop
8581 Current_Unit := Parent (Current_Unit);
8582 end loop;
8583
8584 Current_Context := Context_Items (Current_Unit);
8585
8586 Item := First (Context_Items (Parent (Gen_Decl)));
8587 while Present (Item) loop
8588 if Nkind (Item) = N_With_Clause then
8589 Lib_Unit := Library_Unit (Item);
8590
8591 -- Take care to prevent direct cyclic with's
8592
8593 if Lib_Unit /= Current_Unit then
8594
8595 -- Do not add a unit if it is already in the context
8596
8597 Clause := First (Current_Context);
8598 OK := True;
8599 while Present (Clause) loop
8600 if Nkind (Clause) = N_With_Clause and then
8601 Library_Unit (Clause) = Lib_Unit
8602 then
8603 OK := False;
8604 exit;
8605 end if;
8606
8607 Next (Clause);
8608 end loop;
8609
8610 if OK then
8611 New_I := New_Copy (Item);
8612 Set_Implicit_With (New_I, True);
8613 Set_Implicit_With_From_Instantiation (New_I, True);
8614 Append (New_I, Current_Context);
8615 end if;
8616 end if;
8617 end if;
8618
8619 Next (Item);
8620 end loop;
8621 end if;
8622 end Inherit_Context;
8623
8624 ----------------
8625 -- Initialize --
8626 ----------------
8627
8628 procedure Initialize is
8629 begin
8630 Generic_Renamings.Init;
8631 Instance_Envs.Init;
8632 Generic_Flags.Init;
8633 Generic_Renamings_HTable.Reset;
8634 Circularity_Detected := False;
8635 Exchanged_Views := No_Elist;
8636 Hidden_Entities := No_Elist;
8637 end Initialize;
8638
8639 -------------------------------------
8640 -- Insert_Freeze_Node_For_Instance --
8641 -------------------------------------
8642
8643 procedure Insert_Freeze_Node_For_Instance
8644 (N : Node_Id;
8645 F_Node : Node_Id)
8646 is
8647 Decl : Node_Id;
8648 Decls : List_Id;
8649 Inst : Entity_Id;
8650 Par_N : Node_Id;
8651
8652 function Enclosing_Body (N : Node_Id) return Node_Id;
8653 -- Find enclosing package or subprogram body, if any. Freeze node may
8654 -- be placed at end of current declarative list if previous instance
8655 -- and current one have different enclosing bodies.
8656
8657 function Previous_Instance (Gen : Entity_Id) return Entity_Id;
8658 -- Find the local instance, if any, that declares the generic that is
8659 -- being instantiated. If present, the freeze node for this instance
8660 -- must follow the freeze node for the previous instance.
8661
8662 --------------------
8663 -- Enclosing_Body --
8664 --------------------
8665
8666 function Enclosing_Body (N : Node_Id) return Node_Id is
8667 P : Node_Id;
8668
8669 begin
8670 P := Parent (N);
8671 while Present (P)
8672 and then Nkind (Parent (P)) /= N_Compilation_Unit
8673 loop
8674 if Nkind_In (P, N_Package_Body, N_Subprogram_Body) then
8675 if Nkind (Parent (P)) = N_Subunit then
8676 return Corresponding_Stub (Parent (P));
8677 else
8678 return P;
8679 end if;
8680 end if;
8681
8682 P := True_Parent (P);
8683 end loop;
8684
8685 return Empty;
8686 end Enclosing_Body;
8687
8688 -----------------------
8689 -- Previous_Instance --
8690 -----------------------
8691
8692 function Previous_Instance (Gen : Entity_Id) return Entity_Id is
8693 S : Entity_Id;
8694
8695 begin
8696 S := Scope (Gen);
8697 while Present (S) and then S /= Standard_Standard loop
8698 if Is_Generic_Instance (S)
8699 and then In_Same_Source_Unit (S, N)
8700 then
8701 return S;
8702 end if;
8703
8704 S := Scope (S);
8705 end loop;
8706
8707 return Empty;
8708 end Previous_Instance;
8709
8710 -- Start of processing for Insert_Freeze_Node_For_Instance
8711
8712 begin
8713 if not Is_List_Member (F_Node) then
8714 Decl := N;
8715 Decls := List_Containing (N);
8716 Inst := Entity (F_Node);
8717 Par_N := Parent (Decls);
8718
8719 -- When processing a subprogram instantiation, utilize the actual
8720 -- subprogram instantiation rather than its package wrapper as it
8721 -- carries all the context information.
8722
8723 if Is_Wrapper_Package (Inst) then
8724 Inst := Related_Instance (Inst);
8725 end if;
8726
8727 -- If this is a package instance, check whether the generic is
8728 -- declared in a previous instance and the current instance is
8729 -- not within the previous one.
8730
8731 if Present (Generic_Parent (Parent (Inst)))
8732 and then Is_In_Main_Unit (N)
8733 then
8734 declare
8735 Enclosing_N : constant Node_Id := Enclosing_Body (N);
8736 Par_I : constant Entity_Id :=
8737 Previous_Instance
8738 (Generic_Parent (Parent (Inst)));
8739 Scop : Entity_Id;
8740
8741 begin
8742 if Present (Par_I)
8743 and then Earlier (N, Freeze_Node (Par_I))
8744 then
8745 Scop := Scope (Inst);
8746
8747 -- If the current instance is within the one that contains
8748 -- the generic, the freeze node for the current one must
8749 -- appear in the current declarative part. Ditto, if the
8750 -- current instance is within another package instance or
8751 -- within a body that does not enclose the current instance.
8752 -- In these three cases the freeze node of the previous
8753 -- instance is not relevant.
8754
8755 while Present (Scop) and then Scop /= Standard_Standard loop
8756 exit when Scop = Par_I
8757 or else
8758 (Is_Generic_Instance (Scop)
8759 and then Scope_Depth (Scop) > Scope_Depth (Par_I));
8760 Scop := Scope (Scop);
8761 end loop;
8762
8763 -- Previous instance encloses current instance
8764
8765 if Scop = Par_I then
8766 null;
8767
8768 -- If the next node is a source body we must freeze in
8769 -- the current scope as well.
8770
8771 elsif Present (Next (N))
8772 and then Nkind_In (Next (N), N_Subprogram_Body,
8773 N_Package_Body)
8774 and then Comes_From_Source (Next (N))
8775 then
8776 null;
8777
8778 -- Current instance is within an unrelated instance
8779
8780 elsif Is_Generic_Instance (Scop) then
8781 null;
8782
8783 -- Current instance is within an unrelated body
8784
8785 elsif Present (Enclosing_N)
8786 and then Enclosing_N /= Enclosing_Body (Par_I)
8787 then
8788 null;
8789
8790 else
8791 Insert_After (Freeze_Node (Par_I), F_Node);
8792 return;
8793 end if;
8794 end if;
8795 end;
8796 end if;
8797
8798 -- When the instantiation occurs in a package declaration, append the
8799 -- freeze node to the private declarations (if any).
8800
8801 if Nkind (Par_N) = N_Package_Specification
8802 and then Decls = Visible_Declarations (Par_N)
8803 and then Present (Private_Declarations (Par_N))
8804 and then not Is_Empty_List (Private_Declarations (Par_N))
8805 then
8806 Decls := Private_Declarations (Par_N);
8807 Decl := First (Decls);
8808 end if;
8809
8810 -- Determine the proper freeze point of a package instantiation. We
8811 -- adhere to the general rule of a package or subprogram body causing
8812 -- freezing of anything before it in the same declarative region. In
8813 -- this case, the proper freeze point of a package instantiation is
8814 -- before the first source body which follows, or before a stub. This
8815 -- ensures that entities coming from the instance are already frozen
8816 -- and usable in source bodies.
8817
8818 if Nkind (Par_N) /= N_Package_Declaration
8819 and then Ekind (Inst) = E_Package
8820 and then Is_Generic_Instance (Inst)
8821 and then
8822 not In_Same_Source_Unit (Generic_Parent (Parent (Inst)), Inst)
8823 then
8824 while Present (Decl) loop
8825 if (Nkind (Decl) in N_Unit_Body
8826 or else
8827 Nkind (Decl) in N_Body_Stub)
8828 and then Comes_From_Source (Decl)
8829 then
8830 Insert_Before (Decl, F_Node);
8831 return;
8832 end if;
8833
8834 Next (Decl);
8835 end loop;
8836 end if;
8837
8838 -- In a package declaration, or if no previous body, insert at end
8839 -- of list.
8840
8841 Set_Sloc (F_Node, Sloc (Last (Decls)));
8842 Insert_After (Last (Decls), F_Node);
8843 end if;
8844 end Insert_Freeze_Node_For_Instance;
8845
8846 ------------------
8847 -- Install_Body --
8848 ------------------
8849
8850 procedure Install_Body
8851 (Act_Body : Node_Id;
8852 N : Node_Id;
8853 Gen_Body : Node_Id;
8854 Gen_Decl : Node_Id)
8855 is
8856 Act_Id : constant Entity_Id := Corresponding_Spec (Act_Body);
8857 Act_Unit : constant Node_Id := Unit (Cunit (Get_Source_Unit (N)));
8858 Gen_Id : constant Entity_Id := Corresponding_Spec (Gen_Body);
8859 Par : constant Entity_Id := Scope (Gen_Id);
8860 Gen_Unit : constant Node_Id :=
8861 Unit (Cunit (Get_Source_Unit (Gen_Decl)));
8862 Orig_Body : Node_Id := Gen_Body;
8863 F_Node : Node_Id;
8864 Body_Unit : Node_Id;
8865
8866 Must_Delay : Boolean;
8867
8868 function In_Same_Enclosing_Subp return Boolean;
8869 -- Check whether instance and generic body are within same subprogram.
8870
8871 function True_Sloc (N : Node_Id) return Source_Ptr;
8872 -- If the instance is nested inside a generic unit, the Sloc of the
8873 -- instance indicates the place of the original definition, not the
8874 -- point of the current enclosing instance. Pending a better usage of
8875 -- Slocs to indicate instantiation places, we determine the place of
8876 -- origin of a node by finding the maximum sloc of any ancestor node.
8877 -- Why is this not equivalent to Top_Level_Location ???
8878
8879 ----------------------------
8880 -- In_Same_Enclosing_Subp --
8881 ----------------------------
8882
8883 function In_Same_Enclosing_Subp return Boolean is
8884 Scop : Entity_Id;
8885 Subp : Entity_Id;
8886
8887 begin
8888 Scop := Scope (Act_Id);
8889 while Scop /= Standard_Standard
8890 and then not Is_Overloadable (Scop)
8891 loop
8892 Scop := Scope (Scop);
8893 end loop;
8894
8895 if Scop = Standard_Standard then
8896 return False;
8897 else
8898 Subp := Scop;
8899 end if;
8900
8901 Scop := Scope (Gen_Id);
8902 while Scop /= Standard_Standard loop
8903 if Scop = Subp then
8904 return True;
8905 else
8906 Scop := Scope (Scop);
8907 end if;
8908 end loop;
8909
8910 return False;
8911 end In_Same_Enclosing_Subp;
8912
8913 ---------------
8914 -- True_Sloc --
8915 ---------------
8916
8917 function True_Sloc (N : Node_Id) return Source_Ptr is
8918 Res : Source_Ptr;
8919 N1 : Node_Id;
8920
8921 begin
8922 Res := Sloc (N);
8923 N1 := N;
8924 while Present (N1) and then N1 /= Act_Unit loop
8925 if Sloc (N1) > Res then
8926 Res := Sloc (N1);
8927 end if;
8928
8929 N1 := Parent (N1);
8930 end loop;
8931
8932 return Res;
8933 end True_Sloc;
8934
8935 -- Start of processing for Install_Body
8936
8937 begin
8938 -- Handle first the case of an instance with incomplete actual types.
8939 -- The instance body cannot be placed after the declaration because
8940 -- full views have not been seen yet. Any use of the non-limited views
8941 -- in the instance body requires the presence of a regular with_clause
8942 -- in the enclosing unit, and will fail if this with_clause is missing.
8943 -- We place the instance body at the beginning of the enclosing body,
8944 -- which is the unit being compiled. The freeze node for the instance
8945 -- is then placed after the instance body.
8946
8947 if not Is_Empty_Elmt_List (Incomplete_Actuals (Act_Id))
8948 and then Expander_Active
8949 and then Ekind (Scope (Act_Id)) = E_Package
8950 then
8951 declare
8952 Scop : constant Entity_Id := Scope (Act_Id);
8953 Body_Id : constant Node_Id :=
8954 Corresponding_Body (Unit_Declaration_Node (Scop));
8955
8956 begin
8957 Ensure_Freeze_Node (Act_Id);
8958 F_Node := Freeze_Node (Act_Id);
8959 if Present (Body_Id) then
8960 Set_Is_Frozen (Act_Id, False);
8961 Prepend (Act_Body, Declarations (Parent (Body_Id)));
8962 if Is_List_Member (F_Node) then
8963 Remove (F_Node);
8964 end if;
8965
8966 Insert_After (Act_Body, F_Node);
8967 end if;
8968 end;
8969 return;
8970 end if;
8971
8972 -- If the body is a subunit, the freeze point is the corresponding stub
8973 -- in the current compilation, not the subunit itself.
8974
8975 if Nkind (Parent (Gen_Body)) = N_Subunit then
8976 Orig_Body := Corresponding_Stub (Parent (Gen_Body));
8977 else
8978 Orig_Body := Gen_Body;
8979 end if;
8980
8981 Body_Unit := Unit (Cunit (Get_Source_Unit (Orig_Body)));
8982
8983 -- If the instantiation and the generic definition appear in the same
8984 -- package declaration, this is an early instantiation. If they appear
8985 -- in the same declarative part, it is an early instantiation only if
8986 -- the generic body appears textually later, and the generic body is
8987 -- also in the main unit.
8988
8989 -- If instance is nested within a subprogram, and the generic body
8990 -- is not, the instance is delayed because the enclosing body is. If
8991 -- instance and body are within the same scope, or the same subprogram
8992 -- body, indicate explicitly that the instance is delayed.
8993
8994 Must_Delay :=
8995 (Gen_Unit = Act_Unit
8996 and then (Nkind_In (Gen_Unit, N_Package_Declaration,
8997 N_Generic_Package_Declaration)
8998 or else (Gen_Unit = Body_Unit
8999 and then True_Sloc (N) < Sloc (Orig_Body)))
9000 and then Is_In_Main_Unit (Gen_Unit)
9001 and then (Scope (Act_Id) = Scope (Gen_Id)
9002 or else In_Same_Enclosing_Subp));
9003
9004 -- If this is an early instantiation, the freeze node is placed after
9005 -- the generic body. Otherwise, if the generic appears in an instance,
9006 -- we cannot freeze the current instance until the outer one is frozen.
9007 -- This is only relevant if the current instance is nested within some
9008 -- inner scope not itself within the outer instance. If this scope is
9009 -- a package body in the same declarative part as the outer instance,
9010 -- then that body needs to be frozen after the outer instance. Finally,
9011 -- if no delay is needed, we place the freeze node at the end of the
9012 -- current declarative part.
9013
9014 if Expander_Active then
9015 Ensure_Freeze_Node (Act_Id);
9016 F_Node := Freeze_Node (Act_Id);
9017
9018 if Must_Delay then
9019 Insert_After (Orig_Body, F_Node);
9020
9021 elsif Is_Generic_Instance (Par)
9022 and then Present (Freeze_Node (Par))
9023 and then Scope (Act_Id) /= Par
9024 then
9025 -- Freeze instance of inner generic after instance of enclosing
9026 -- generic.
9027
9028 if In_Same_Declarative_Part (Freeze_Node (Par), N) then
9029
9030 -- Handle the following case:
9031
9032 -- package Parent_Inst is new ...
9033 -- Parent_Inst []
9034
9035 -- procedure P ... -- this body freezes Parent_Inst
9036
9037 -- package Inst is new ...
9038
9039 -- In this particular scenario, the freeze node for Inst must
9040 -- be inserted in the same manner as that of Parent_Inst,
9041 -- before the next source body or at the end of the declarative
9042 -- list (body not available). If body P did not exist and
9043 -- Parent_Inst was frozen after Inst, either by a body
9044 -- following Inst or at the end of the declarative region,
9045 -- the freeze node for Inst must be inserted after that of
9046 -- Parent_Inst. This relation is established by comparing
9047 -- the Slocs of Parent_Inst freeze node and Inst.
9048
9049 if List_Containing (Get_Package_Instantiation_Node (Par)) =
9050 List_Containing (N)
9051 and then Sloc (Freeze_Node (Par)) < Sloc (N)
9052 then
9053 Insert_Freeze_Node_For_Instance (N, F_Node);
9054 else
9055 Insert_After (Freeze_Node (Par), F_Node);
9056 end if;
9057
9058 -- Freeze package enclosing instance of inner generic after
9059 -- instance of enclosing generic.
9060
9061 elsif Nkind_In (Parent (N), N_Package_Body, N_Subprogram_Body)
9062 and then In_Same_Declarative_Part (Freeze_Node (Par), Parent (N))
9063 then
9064 declare
9065 Enclosing : Entity_Id;
9066
9067 begin
9068 Enclosing := Corresponding_Spec (Parent (N));
9069
9070 if No (Enclosing) then
9071 Enclosing := Defining_Entity (Parent (N));
9072 end if;
9073
9074 Insert_Freeze_Node_For_Instance (N, F_Node);
9075 Ensure_Freeze_Node (Enclosing);
9076
9077 if not Is_List_Member (Freeze_Node (Enclosing)) then
9078
9079 -- The enclosing context is a subunit, insert the freeze
9080 -- node after the stub.
9081
9082 if Nkind (Parent (Parent (N))) = N_Subunit then
9083 Insert_Freeze_Node_For_Instance
9084 (Corresponding_Stub (Parent (Parent (N))),
9085 Freeze_Node (Enclosing));
9086
9087 -- The enclosing context is a package with a stub body
9088 -- which has already been replaced by the real body.
9089 -- Insert the freeze node after the actual body.
9090
9091 elsif Ekind (Enclosing) = E_Package
9092 and then Present (Body_Entity (Enclosing))
9093 and then Was_Originally_Stub
9094 (Parent (Body_Entity (Enclosing)))
9095 then
9096 Insert_Freeze_Node_For_Instance
9097 (Parent (Body_Entity (Enclosing)),
9098 Freeze_Node (Enclosing));
9099
9100 -- The parent instance has been frozen before the body of
9101 -- the enclosing package, insert the freeze node after
9102 -- the body.
9103
9104 elsif List_Containing (Freeze_Node (Par)) =
9105 List_Containing (Parent (N))
9106 and then Sloc (Freeze_Node (Par)) < Sloc (Parent (N))
9107 then
9108 Insert_Freeze_Node_For_Instance
9109 (Parent (N), Freeze_Node (Enclosing));
9110
9111 else
9112 Insert_After
9113 (Freeze_Node (Par), Freeze_Node (Enclosing));
9114 end if;
9115 end if;
9116 end;
9117
9118 else
9119 Insert_Freeze_Node_For_Instance (N, F_Node);
9120 end if;
9121
9122 else
9123 Insert_Freeze_Node_For_Instance (N, F_Node);
9124 end if;
9125 end if;
9126
9127 Set_Is_Frozen (Act_Id);
9128 Insert_Before (N, Act_Body);
9129 Mark_Rewrite_Insertion (Act_Body);
9130 end Install_Body;
9131
9132 -----------------------------
9133 -- Install_Formal_Packages --
9134 -----------------------------
9135
9136 procedure Install_Formal_Packages (Par : Entity_Id) is
9137 E : Entity_Id;
9138 Gen : Entity_Id;
9139 Gen_E : Entity_Id := Empty;
9140
9141 begin
9142 E := First_Entity (Par);
9143
9144 -- If we are installing an instance parent, locate the formal packages
9145 -- of its generic parent.
9146
9147 if Is_Generic_Instance (Par) then
9148 Gen := Generic_Parent (Package_Specification (Par));
9149 Gen_E := First_Entity (Gen);
9150 end if;
9151
9152 while Present (E) loop
9153 if Ekind (E) = E_Package
9154 and then Nkind (Parent (E)) = N_Package_Renaming_Declaration
9155 then
9156 -- If this is the renaming for the parent instance, done
9157
9158 if Renamed_Object (E) = Par then
9159 exit;
9160
9161 -- The visibility of a formal of an enclosing generic is already
9162 -- correct.
9163
9164 elsif Denotes_Formal_Package (E) then
9165 null;
9166
9167 elsif Present (Associated_Formal_Package (E)) then
9168 Check_Generic_Actuals (Renamed_Object (E), True);
9169 Set_Is_Hidden (E, False);
9170
9171 -- Find formal package in generic unit that corresponds to
9172 -- (instance of) formal package in instance.
9173
9174 while Present (Gen_E) and then Chars (Gen_E) /= Chars (E) loop
9175 Next_Entity (Gen_E);
9176 end loop;
9177
9178 if Present (Gen_E) then
9179 Map_Formal_Package_Entities (Gen_E, E);
9180 end if;
9181 end if;
9182 end if;
9183
9184 Next_Entity (E);
9185
9186 if Present (Gen_E) then
9187 Next_Entity (Gen_E);
9188 end if;
9189 end loop;
9190 end Install_Formal_Packages;
9191
9192 --------------------
9193 -- Install_Parent --
9194 --------------------
9195
9196 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False) is
9197 Ancestors : constant Elist_Id := New_Elmt_List;
9198 S : constant Entity_Id := Current_Scope;
9199 Inst_Par : Entity_Id;
9200 First_Par : Entity_Id;
9201 Inst_Node : Node_Id;
9202 Gen_Par : Entity_Id;
9203 First_Gen : Entity_Id;
9204 Elmt : Elmt_Id;
9205
9206 procedure Install_Noninstance_Specs (Par : Entity_Id);
9207 -- Install the scopes of noninstance parent units ending with Par
9208
9209 procedure Install_Spec (Par : Entity_Id);
9210 -- The child unit is within the declarative part of the parent, so the
9211 -- declarations within the parent are immediately visible.
9212
9213 -------------------------------
9214 -- Install_Noninstance_Specs --
9215 -------------------------------
9216
9217 procedure Install_Noninstance_Specs (Par : Entity_Id) is
9218 begin
9219 if Present (Par)
9220 and then Par /= Standard_Standard
9221 and then not In_Open_Scopes (Par)
9222 then
9223 Install_Noninstance_Specs (Scope (Par));
9224 Install_Spec (Par);
9225 end if;
9226 end Install_Noninstance_Specs;
9227
9228 ------------------
9229 -- Install_Spec --
9230 ------------------
9231
9232 procedure Install_Spec (Par : Entity_Id) is
9233 Spec : constant Node_Id := Package_Specification (Par);
9234
9235 begin
9236 -- If this parent of the child instance is a top-level unit,
9237 -- then record the unit and its visibility for later resetting in
9238 -- Remove_Parent. We exclude units that are generic instances, as we
9239 -- only want to record this information for the ultimate top-level
9240 -- noninstance parent (is that always correct???).
9241
9242 if Scope (Par) = Standard_Standard
9243 and then not Is_Generic_Instance (Par)
9244 then
9245 Parent_Unit_Visible := Is_Immediately_Visible (Par);
9246 Instance_Parent_Unit := Par;
9247 end if;
9248
9249 -- Open the parent scope and make it and its declarations visible.
9250 -- If this point is not within a body, then only the visible
9251 -- declarations should be made visible, and installation of the
9252 -- private declarations is deferred until the appropriate point
9253 -- within analysis of the spec being instantiated (see the handling
9254 -- of parent visibility in Analyze_Package_Specification). This is
9255 -- relaxed in the case where the parent unit is Ada.Tags, to avoid
9256 -- private view problems that occur when compiling instantiations of
9257 -- a generic child of that package (Generic_Dispatching_Constructor).
9258 -- If the instance freezes a tagged type, inlinings of operations
9259 -- from Ada.Tags may need the full view of type Tag. If inlining took
9260 -- proper account of establishing visibility of inlined subprograms'
9261 -- parents then it should be possible to remove this
9262 -- special check. ???
9263
9264 Push_Scope (Par);
9265 Set_Is_Immediately_Visible (Par);
9266 Install_Visible_Declarations (Par);
9267 Set_Use (Visible_Declarations (Spec));
9268
9269 if In_Body or else Is_RTU (Par, Ada_Tags) then
9270 Install_Private_Declarations (Par);
9271 Set_Use (Private_Declarations (Spec));
9272 end if;
9273 end Install_Spec;
9274
9275 -- Start of processing for Install_Parent
9276
9277 begin
9278 -- We need to install the parent instance to compile the instantiation
9279 -- of the child, but the child instance must appear in the current
9280 -- scope. Given that we cannot place the parent above the current scope
9281 -- in the scope stack, we duplicate the current scope and unstack both
9282 -- after the instantiation is complete.
9283
9284 -- If the parent is itself the instantiation of a child unit, we must
9285 -- also stack the instantiation of its parent, and so on. Each such
9286 -- ancestor is the prefix of the name in a prior instantiation.
9287
9288 -- If this is a nested instance, the parent unit itself resolves to
9289 -- a renaming of the parent instance, whose declaration we need.
9290
9291 -- Finally, the parent may be a generic (not an instance) when the
9292 -- child unit appears as a formal package.
9293
9294 Inst_Par := P;
9295
9296 if Present (Renamed_Entity (Inst_Par)) then
9297 Inst_Par := Renamed_Entity (Inst_Par);
9298 end if;
9299
9300 First_Par := Inst_Par;
9301
9302 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
9303
9304 First_Gen := Gen_Par;
9305
9306 while Present (Gen_Par) and then Is_Child_Unit (Gen_Par) loop
9307
9308 -- Load grandparent instance as well
9309
9310 Inst_Node := Get_Package_Instantiation_Node (Inst_Par);
9311
9312 if Nkind (Name (Inst_Node)) = N_Expanded_Name then
9313 Inst_Par := Entity (Prefix (Name (Inst_Node)));
9314
9315 if Present (Renamed_Entity (Inst_Par)) then
9316 Inst_Par := Renamed_Entity (Inst_Par);
9317 end if;
9318
9319 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
9320
9321 if Present (Gen_Par) then
9322 Prepend_Elmt (Inst_Par, Ancestors);
9323
9324 else
9325 -- Parent is not the name of an instantiation
9326
9327 Install_Noninstance_Specs (Inst_Par);
9328 exit;
9329 end if;
9330
9331 else
9332 -- Previous error
9333
9334 exit;
9335 end if;
9336 end loop;
9337
9338 if Present (First_Gen) then
9339 Append_Elmt (First_Par, Ancestors);
9340 else
9341 Install_Noninstance_Specs (First_Par);
9342 end if;
9343
9344 if not Is_Empty_Elmt_List (Ancestors) then
9345 Elmt := First_Elmt (Ancestors);
9346 while Present (Elmt) loop
9347 Install_Spec (Node (Elmt));
9348 Install_Formal_Packages (Node (Elmt));
9349 Next_Elmt (Elmt);
9350 end loop;
9351 end if;
9352
9353 if not In_Body then
9354 Push_Scope (S);
9355 end if;
9356 end Install_Parent;
9357
9358 -------------------------------
9359 -- Install_Hidden_Primitives --
9360 -------------------------------
9361
9362 procedure Install_Hidden_Primitives
9363 (Prims_List : in out Elist_Id;
9364 Gen_T : Entity_Id;
9365 Act_T : Entity_Id)
9366 is
9367 Elmt : Elmt_Id;
9368 List : Elist_Id := No_Elist;
9369 Prim_G_Elmt : Elmt_Id;
9370 Prim_A_Elmt : Elmt_Id;
9371 Prim_G : Node_Id;
9372 Prim_A : Node_Id;
9373
9374 begin
9375 -- No action needed in case of serious errors because we cannot trust
9376 -- in the order of primitives
9377
9378 if Serious_Errors_Detected > 0 then
9379 return;
9380
9381 -- No action possible if we don't have available the list of primitive
9382 -- operations
9383
9384 elsif No (Gen_T)
9385 or else not Is_Record_Type (Gen_T)
9386 or else not Is_Tagged_Type (Gen_T)
9387 or else not Is_Record_Type (Act_T)
9388 or else not Is_Tagged_Type (Act_T)
9389 then
9390 return;
9391
9392 -- There is no need to handle interface types since their primitives
9393 -- cannot be hidden
9394
9395 elsif Is_Interface (Gen_T) then
9396 return;
9397 end if;
9398
9399 Prim_G_Elmt := First_Elmt (Primitive_Operations (Gen_T));
9400
9401 if not Is_Class_Wide_Type (Act_T) then
9402 Prim_A_Elmt := First_Elmt (Primitive_Operations (Act_T));
9403 else
9404 Prim_A_Elmt := First_Elmt (Primitive_Operations (Root_Type (Act_T)));
9405 end if;
9406
9407 loop
9408 -- Skip predefined primitives in the generic formal
9409
9410 while Present (Prim_G_Elmt)
9411 and then Is_Predefined_Dispatching_Operation (Node (Prim_G_Elmt))
9412 loop
9413 Next_Elmt (Prim_G_Elmt);
9414 end loop;
9415
9416 -- Skip predefined primitives in the generic actual
9417
9418 while Present (Prim_A_Elmt)
9419 and then Is_Predefined_Dispatching_Operation (Node (Prim_A_Elmt))
9420 loop
9421 Next_Elmt (Prim_A_Elmt);
9422 end loop;
9423
9424 exit when No (Prim_G_Elmt) or else No (Prim_A_Elmt);
9425
9426 Prim_G := Node (Prim_G_Elmt);
9427 Prim_A := Node (Prim_A_Elmt);
9428
9429 -- There is no need to handle interface primitives because their
9430 -- primitives are not hidden
9431
9432 exit when Present (Interface_Alias (Prim_G));
9433
9434 -- Here we install one hidden primitive
9435
9436 if Chars (Prim_G) /= Chars (Prim_A)
9437 and then Has_Suffix (Prim_A, 'P')
9438 and then Remove_Suffix (Prim_A, 'P') = Chars (Prim_G)
9439 then
9440 Set_Chars (Prim_A, Chars (Prim_G));
9441 Append_New_Elmt (Prim_A, To => List);
9442 end if;
9443
9444 Next_Elmt (Prim_A_Elmt);
9445 Next_Elmt (Prim_G_Elmt);
9446 end loop;
9447
9448 -- Append the elements to the list of temporarily visible primitives
9449 -- avoiding duplicates.
9450
9451 if Present (List) then
9452 if No (Prims_List) then
9453 Prims_List := New_Elmt_List;
9454 end if;
9455
9456 Elmt := First_Elmt (List);
9457 while Present (Elmt) loop
9458 Append_Unique_Elmt (Node (Elmt), Prims_List);
9459 Next_Elmt (Elmt);
9460 end loop;
9461 end if;
9462 end Install_Hidden_Primitives;
9463
9464 -------------------------------
9465 -- Restore_Hidden_Primitives --
9466 -------------------------------
9467
9468 procedure Restore_Hidden_Primitives (Prims_List : in out Elist_Id) is
9469 Prim_Elmt : Elmt_Id;
9470 Prim : Node_Id;
9471
9472 begin
9473 if Prims_List /= No_Elist then
9474 Prim_Elmt := First_Elmt (Prims_List);
9475 while Present (Prim_Elmt) loop
9476 Prim := Node (Prim_Elmt);
9477 Set_Chars (Prim, Add_Suffix (Prim, 'P'));
9478 Next_Elmt (Prim_Elmt);
9479 end loop;
9480
9481 Prims_List := No_Elist;
9482 end if;
9483 end Restore_Hidden_Primitives;
9484
9485 --------------------------------
9486 -- Instantiate_Formal_Package --
9487 --------------------------------
9488
9489 function Instantiate_Formal_Package
9490 (Formal : Node_Id;
9491 Actual : Node_Id;
9492 Analyzed_Formal : Node_Id) return List_Id
9493 is
9494 Loc : constant Source_Ptr := Sloc (Actual);
9495 Actual_Pack : Entity_Id;
9496 Formal_Pack : Entity_Id;
9497 Gen_Parent : Entity_Id;
9498 Decls : List_Id;
9499 Nod : Node_Id;
9500 Parent_Spec : Node_Id;
9501
9502 procedure Find_Matching_Actual
9503 (F : Node_Id;
9504 Act : in out Entity_Id);
9505 -- We need to associate each formal entity in the formal package with
9506 -- the corresponding entity in the actual package. The actual package
9507 -- has been analyzed and possibly expanded, and as a result there is
9508 -- no one-to-one correspondence between the two lists (for example,
9509 -- the actual may include subtypes, itypes, and inherited primitive
9510 -- operations, interspersed among the renaming declarations for the
9511 -- actuals). We retrieve the corresponding actual by name because each
9512 -- actual has the same name as the formal, and they do appear in the
9513 -- same order.
9514
9515 function Get_Formal_Entity (N : Node_Id) return Entity_Id;
9516 -- Retrieve entity of defining entity of generic formal parameter.
9517 -- Only the declarations of formals need to be considered when
9518 -- linking them to actuals, but the declarative list may include
9519 -- internal entities generated during analysis, and those are ignored.
9520
9521 procedure Match_Formal_Entity
9522 (Formal_Node : Node_Id;
9523 Formal_Ent : Entity_Id;
9524 Actual_Ent : Entity_Id);
9525 -- Associates the formal entity with the actual. In the case where
9526 -- Formal_Ent is a formal package, this procedure iterates through all
9527 -- of its formals and enters associations between the actuals occurring
9528 -- in the formal package's corresponding actual package (given by
9529 -- Actual_Ent) and the formal package's formal parameters. This
9530 -- procedure recurses if any of the parameters is itself a package.
9531
9532 function Is_Instance_Of
9533 (Act_Spec : Entity_Id;
9534 Gen_Anc : Entity_Id) return Boolean;
9535 -- The actual can be an instantiation of a generic within another
9536 -- instance, in which case there is no direct link from it to the
9537 -- original generic ancestor. In that case, we recognize that the
9538 -- ultimate ancestor is the same by examining names and scopes.
9539
9540 procedure Process_Nested_Formal (Formal : Entity_Id);
9541 -- If the current formal is declared with a box, its own formals are
9542 -- visible in the instance, as they were in the generic, and their
9543 -- Hidden flag must be reset. If some of these formals are themselves
9544 -- packages declared with a box, the processing must be recursive.
9545
9546 --------------------------
9547 -- Find_Matching_Actual --
9548 --------------------------
9549
9550 procedure Find_Matching_Actual
9551 (F : Node_Id;
9552 Act : in out Entity_Id)
9553 is
9554 Formal_Ent : Entity_Id;
9555
9556 begin
9557 case Nkind (Original_Node (F)) is
9558 when N_Formal_Object_Declaration |
9559 N_Formal_Type_Declaration =>
9560 Formal_Ent := Defining_Identifier (F);
9561
9562 while Chars (Act) /= Chars (Formal_Ent) loop
9563 Next_Entity (Act);
9564 end loop;
9565
9566 when N_Formal_Subprogram_Declaration |
9567 N_Formal_Package_Declaration |
9568 N_Package_Declaration |
9569 N_Generic_Package_Declaration =>
9570 Formal_Ent := Defining_Entity (F);
9571
9572 while Chars (Act) /= Chars (Formal_Ent) loop
9573 Next_Entity (Act);
9574 end loop;
9575
9576 when others =>
9577 raise Program_Error;
9578 end case;
9579 end Find_Matching_Actual;
9580
9581 -------------------------
9582 -- Match_Formal_Entity --
9583 -------------------------
9584
9585 procedure Match_Formal_Entity
9586 (Formal_Node : Node_Id;
9587 Formal_Ent : Entity_Id;
9588 Actual_Ent : Entity_Id)
9589 is
9590 Act_Pkg : Entity_Id;
9591
9592 begin
9593 Set_Instance_Of (Formal_Ent, Actual_Ent);
9594
9595 if Ekind (Actual_Ent) = E_Package then
9596
9597 -- Record associations for each parameter
9598
9599 Act_Pkg := Actual_Ent;
9600
9601 declare
9602 A_Ent : Entity_Id := First_Entity (Act_Pkg);
9603 F_Ent : Entity_Id;
9604 F_Node : Node_Id;
9605
9606 Gen_Decl : Node_Id;
9607 Formals : List_Id;
9608 Actual : Entity_Id;
9609
9610 begin
9611 -- Retrieve the actual given in the formal package declaration
9612
9613 Actual := Entity (Name (Original_Node (Formal_Node)));
9614
9615 -- The actual in the formal package declaration may be a
9616 -- renamed generic package, in which case we want to retrieve
9617 -- the original generic in order to traverse its formal part.
9618
9619 if Present (Renamed_Entity (Actual)) then
9620 Gen_Decl := Unit_Declaration_Node (Renamed_Entity (Actual));
9621 else
9622 Gen_Decl := Unit_Declaration_Node (Actual);
9623 end if;
9624
9625 Formals := Generic_Formal_Declarations (Gen_Decl);
9626
9627 if Present (Formals) then
9628 F_Node := First_Non_Pragma (Formals);
9629 else
9630 F_Node := Empty;
9631 end if;
9632
9633 while Present (A_Ent)
9634 and then Present (F_Node)
9635 and then A_Ent /= First_Private_Entity (Act_Pkg)
9636 loop
9637 F_Ent := Get_Formal_Entity (F_Node);
9638
9639 if Present (F_Ent) then
9640
9641 -- This is a formal of the original package. Record
9642 -- association and recurse.
9643
9644 Find_Matching_Actual (F_Node, A_Ent);
9645 Match_Formal_Entity (F_Node, F_Ent, A_Ent);
9646 Next_Entity (A_Ent);
9647 end if;
9648
9649 Next_Non_Pragma (F_Node);
9650 end loop;
9651 end;
9652 end if;
9653 end Match_Formal_Entity;
9654
9655 -----------------------
9656 -- Get_Formal_Entity --
9657 -----------------------
9658
9659 function Get_Formal_Entity (N : Node_Id) return Entity_Id is
9660 Kind : constant Node_Kind := Nkind (Original_Node (N));
9661 begin
9662 case Kind is
9663 when N_Formal_Object_Declaration =>
9664 return Defining_Identifier (N);
9665
9666 when N_Formal_Type_Declaration =>
9667 return Defining_Identifier (N);
9668
9669 when N_Formal_Subprogram_Declaration =>
9670 return Defining_Unit_Name (Specification (N));
9671
9672 when N_Formal_Package_Declaration =>
9673 return Defining_Identifier (Original_Node (N));
9674
9675 when N_Generic_Package_Declaration =>
9676 return Defining_Identifier (Original_Node (N));
9677
9678 -- All other declarations are introduced by semantic analysis and
9679 -- have no match in the actual.
9680
9681 when others =>
9682 return Empty;
9683 end case;
9684 end Get_Formal_Entity;
9685
9686 --------------------
9687 -- Is_Instance_Of --
9688 --------------------
9689
9690 function Is_Instance_Of
9691 (Act_Spec : Entity_Id;
9692 Gen_Anc : Entity_Id) return Boolean
9693 is
9694 Gen_Par : constant Entity_Id := Generic_Parent (Act_Spec);
9695
9696 begin
9697 if No (Gen_Par) then
9698 return False;
9699
9700 -- Simplest case: the generic parent of the actual is the formal
9701
9702 elsif Gen_Par = Gen_Anc then
9703 return True;
9704
9705 elsif Chars (Gen_Par) /= Chars (Gen_Anc) then
9706 return False;
9707
9708 -- The actual may be obtained through several instantiations. Its
9709 -- scope must itself be an instance of a generic declared in the
9710 -- same scope as the formal. Any other case is detected above.
9711
9712 elsif not Is_Generic_Instance (Scope (Gen_Par)) then
9713 return False;
9714
9715 else
9716 return Generic_Parent (Parent (Scope (Gen_Par))) = Scope (Gen_Anc);
9717 end if;
9718 end Is_Instance_Of;
9719
9720 ---------------------------
9721 -- Process_Nested_Formal --
9722 ---------------------------
9723
9724 procedure Process_Nested_Formal (Formal : Entity_Id) is
9725 Ent : Entity_Id;
9726
9727 begin
9728 if Present (Associated_Formal_Package (Formal))
9729 and then Box_Present (Parent (Associated_Formal_Package (Formal)))
9730 then
9731 Ent := First_Entity (Formal);
9732 while Present (Ent) loop
9733 Set_Is_Hidden (Ent, False);
9734 Set_Is_Visible_Formal (Ent);
9735 Set_Is_Potentially_Use_Visible
9736 (Ent, Is_Potentially_Use_Visible (Formal));
9737
9738 if Ekind (Ent) = E_Package then
9739 exit when Renamed_Entity (Ent) = Renamed_Entity (Formal);
9740 Process_Nested_Formal (Ent);
9741 end if;
9742
9743 Next_Entity (Ent);
9744 end loop;
9745 end if;
9746 end Process_Nested_Formal;
9747
9748 -- Start of processing for Instantiate_Formal_Package
9749
9750 begin
9751 Analyze (Actual);
9752
9753 if not Is_Entity_Name (Actual)
9754 or else Ekind (Entity (Actual)) /= E_Package
9755 then
9756 Error_Msg_N
9757 ("expect package instance to instantiate formal", Actual);
9758 Abandon_Instantiation (Actual);
9759 raise Program_Error;
9760
9761 else
9762 Actual_Pack := Entity (Actual);
9763 Set_Is_Instantiated (Actual_Pack);
9764
9765 -- The actual may be a renamed package, or an outer generic formal
9766 -- package whose instantiation is converted into a renaming.
9767
9768 if Present (Renamed_Object (Actual_Pack)) then
9769 Actual_Pack := Renamed_Object (Actual_Pack);
9770 end if;
9771
9772 if Nkind (Analyzed_Formal) = N_Formal_Package_Declaration then
9773 Gen_Parent := Get_Instance_Of (Entity (Name (Analyzed_Formal)));
9774 Formal_Pack := Defining_Identifier (Analyzed_Formal);
9775 else
9776 Gen_Parent :=
9777 Generic_Parent (Specification (Analyzed_Formal));
9778 Formal_Pack :=
9779 Defining_Unit_Name (Specification (Analyzed_Formal));
9780 end if;
9781
9782 if Nkind (Parent (Actual_Pack)) = N_Defining_Program_Unit_Name then
9783 Parent_Spec := Package_Specification (Actual_Pack);
9784 else
9785 Parent_Spec := Parent (Actual_Pack);
9786 end if;
9787
9788 if Gen_Parent = Any_Id then
9789 Error_Msg_N
9790 ("previous error in declaration of formal package", Actual);
9791 Abandon_Instantiation (Actual);
9792
9793 elsif
9794 Is_Instance_Of (Parent_Spec, Get_Instance_Of (Gen_Parent))
9795 then
9796 null;
9797
9798 else
9799 Error_Msg_NE
9800 ("actual parameter must be instance of&", Actual, Gen_Parent);
9801 Abandon_Instantiation (Actual);
9802 end if;
9803
9804 Set_Instance_Of (Defining_Identifier (Formal), Actual_Pack);
9805 Map_Formal_Package_Entities (Formal_Pack, Actual_Pack);
9806
9807 Nod :=
9808 Make_Package_Renaming_Declaration (Loc,
9809 Defining_Unit_Name => New_Copy (Defining_Identifier (Formal)),
9810 Name => New_Occurrence_Of (Actual_Pack, Loc));
9811
9812 Set_Associated_Formal_Package
9813 (Defining_Unit_Name (Nod), Defining_Identifier (Formal));
9814 Decls := New_List (Nod);
9815
9816 -- If the formal F has a box, then the generic declarations are
9817 -- visible in the generic G. In an instance of G, the corresponding
9818 -- entities in the actual for F (which are the actuals for the
9819 -- instantiation of the generic that F denotes) must also be made
9820 -- visible for analysis of the current instance. On exit from the
9821 -- current instance, those entities are made private again. If the
9822 -- actual is currently in use, these entities are also use-visible.
9823
9824 -- The loop through the actual entities also steps through the formal
9825 -- entities and enters associations from formals to actuals into the
9826 -- renaming map. This is necessary to properly handle checking of
9827 -- actual parameter associations for later formals that depend on
9828 -- actuals declared in the formal package.
9829
9830 -- In Ada 2005, partial parameterization requires that we make
9831 -- visible the actuals corresponding to formals that were defaulted
9832 -- in the formal package. There formals are identified because they
9833 -- remain formal generics within the formal package, rather than
9834 -- being renamings of the actuals supplied.
9835
9836 declare
9837 Gen_Decl : constant Node_Id :=
9838 Unit_Declaration_Node (Gen_Parent);
9839 Formals : constant List_Id :=
9840 Generic_Formal_Declarations (Gen_Decl);
9841
9842 Actual_Ent : Entity_Id;
9843 Actual_Of_Formal : Node_Id;
9844 Formal_Node : Node_Id;
9845 Formal_Ent : Entity_Id;
9846
9847 begin
9848 if Present (Formals) then
9849 Formal_Node := First_Non_Pragma (Formals);
9850 else
9851 Formal_Node := Empty;
9852 end if;
9853
9854 Actual_Ent := First_Entity (Actual_Pack);
9855 Actual_Of_Formal :=
9856 First (Visible_Declarations (Specification (Analyzed_Formal)));
9857 while Present (Actual_Ent)
9858 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
9859 loop
9860 if Present (Formal_Node) then
9861 Formal_Ent := Get_Formal_Entity (Formal_Node);
9862
9863 if Present (Formal_Ent) then
9864 Find_Matching_Actual (Formal_Node, Actual_Ent);
9865 Match_Formal_Entity (Formal_Node, Formal_Ent, Actual_Ent);
9866
9867 -- We iterate at the same time over the actuals of the
9868 -- local package created for the formal, to determine
9869 -- which one of the formals of the original generic were
9870 -- defaulted in the formal. The corresponding actual
9871 -- entities are visible in the enclosing instance.
9872
9873 if Box_Present (Formal)
9874 or else
9875 (Present (Actual_Of_Formal)
9876 and then
9877 Is_Generic_Formal
9878 (Get_Formal_Entity (Actual_Of_Formal)))
9879 then
9880 Set_Is_Hidden (Actual_Ent, False);
9881 Set_Is_Visible_Formal (Actual_Ent);
9882 Set_Is_Potentially_Use_Visible
9883 (Actual_Ent, In_Use (Actual_Pack));
9884
9885 if Ekind (Actual_Ent) = E_Package then
9886 Process_Nested_Formal (Actual_Ent);
9887 end if;
9888
9889 else
9890 Set_Is_Hidden (Actual_Ent);
9891 Set_Is_Potentially_Use_Visible (Actual_Ent, False);
9892 end if;
9893 end if;
9894
9895 Next_Non_Pragma (Formal_Node);
9896 Next (Actual_Of_Formal);
9897
9898 else
9899 -- No further formals to match, but the generic part may
9900 -- contain inherited operation that are not hidden in the
9901 -- enclosing instance.
9902
9903 Next_Entity (Actual_Ent);
9904 end if;
9905 end loop;
9906
9907 -- Inherited subprograms generated by formal derived types are
9908 -- also visible if the types are.
9909
9910 Actual_Ent := First_Entity (Actual_Pack);
9911 while Present (Actual_Ent)
9912 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
9913 loop
9914 if Is_Overloadable (Actual_Ent)
9915 and then
9916 Nkind (Parent (Actual_Ent)) = N_Subtype_Declaration
9917 and then
9918 not Is_Hidden (Defining_Identifier (Parent (Actual_Ent)))
9919 then
9920 Set_Is_Hidden (Actual_Ent, False);
9921 Set_Is_Potentially_Use_Visible
9922 (Actual_Ent, In_Use (Actual_Pack));
9923 end if;
9924
9925 Next_Entity (Actual_Ent);
9926 end loop;
9927 end;
9928
9929 -- If the formal is not declared with a box, reanalyze it as an
9930 -- abbreviated instantiation, to verify the matching rules of 12.7.
9931 -- The actual checks are performed after the generic associations
9932 -- have been analyzed, to guarantee the same visibility for this
9933 -- instantiation and for the actuals.
9934
9935 -- In Ada 2005, the generic associations for the formal can include
9936 -- defaulted parameters. These are ignored during check. This
9937 -- internal instantiation is removed from the tree after conformance
9938 -- checking, because it contains formal declarations for those
9939 -- defaulted parameters, and those should not reach the back-end.
9940
9941 if not Box_Present (Formal) then
9942 declare
9943 I_Pack : constant Entity_Id :=
9944 Make_Temporary (Sloc (Actual), 'P');
9945
9946 begin
9947 Set_Is_Internal (I_Pack);
9948
9949 Append_To (Decls,
9950 Make_Package_Instantiation (Sloc (Actual),
9951 Defining_Unit_Name => I_Pack,
9952 Name =>
9953 New_Occurrence_Of
9954 (Get_Instance_Of (Gen_Parent), Sloc (Actual)),
9955 Generic_Associations => Generic_Associations (Formal)));
9956 end;
9957 end if;
9958
9959 return Decls;
9960 end if;
9961 end Instantiate_Formal_Package;
9962
9963 -----------------------------------
9964 -- Instantiate_Formal_Subprogram --
9965 -----------------------------------
9966
9967 function Instantiate_Formal_Subprogram
9968 (Formal : Node_Id;
9969 Actual : Node_Id;
9970 Analyzed_Formal : Node_Id) return Node_Id
9971 is
9972 Analyzed_S : constant Entity_Id :=
9973 Defining_Unit_Name (Specification (Analyzed_Formal));
9974 Formal_Sub : constant Entity_Id :=
9975 Defining_Unit_Name (Specification (Formal));
9976
9977 function From_Parent_Scope (Subp : Entity_Id) return Boolean;
9978 -- If the generic is a child unit, the parent has been installed on the
9979 -- scope stack, but a default subprogram cannot resolve to something
9980 -- on the parent because that parent is not really part of the visible
9981 -- context (it is there to resolve explicit local entities). If the
9982 -- default has resolved in this way, we remove the entity from immediate
9983 -- visibility and analyze the node again to emit an error message or
9984 -- find another visible candidate.
9985
9986 procedure Valid_Actual_Subprogram (Act : Node_Id);
9987 -- Perform legality check and raise exception on failure
9988
9989 -----------------------
9990 -- From_Parent_Scope --
9991 -----------------------
9992
9993 function From_Parent_Scope (Subp : Entity_Id) return Boolean is
9994 Gen_Scope : Node_Id;
9995
9996 begin
9997 Gen_Scope := Scope (Analyzed_S);
9998 while Present (Gen_Scope) and then Is_Child_Unit (Gen_Scope) loop
9999 if Scope (Subp) = Scope (Gen_Scope) then
10000 return True;
10001 end if;
10002
10003 Gen_Scope := Scope (Gen_Scope);
10004 end loop;
10005
10006 return False;
10007 end From_Parent_Scope;
10008
10009 -----------------------------
10010 -- Valid_Actual_Subprogram --
10011 -----------------------------
10012
10013 procedure Valid_Actual_Subprogram (Act : Node_Id) is
10014 Act_E : Entity_Id;
10015
10016 begin
10017 if Is_Entity_Name (Act) then
10018 Act_E := Entity (Act);
10019
10020 elsif Nkind (Act) = N_Selected_Component
10021 and then Is_Entity_Name (Selector_Name (Act))
10022 then
10023 Act_E := Entity (Selector_Name (Act));
10024
10025 else
10026 Act_E := Empty;
10027 end if;
10028
10029 if (Present (Act_E) and then Is_Overloadable (Act_E))
10030 or else Nkind_In (Act, N_Attribute_Reference,
10031 N_Indexed_Component,
10032 N_Character_Literal,
10033 N_Explicit_Dereference)
10034 then
10035 return;
10036 end if;
10037
10038 Error_Msg_NE
10039 ("expect subprogram or entry name in instantiation of &",
10040 Instantiation_Node, Formal_Sub);
10041 Abandon_Instantiation (Instantiation_Node);
10042 end Valid_Actual_Subprogram;
10043
10044 -- Local variables
10045
10046 Decl_Node : Node_Id;
10047 Loc : Source_Ptr;
10048 Nam : Node_Id;
10049 New_Spec : Node_Id;
10050 New_Subp : Entity_Id;
10051
10052 -- Start of processing for Instantiate_Formal_Subprogram
10053
10054 begin
10055 New_Spec := New_Copy_Tree (Specification (Formal));
10056
10057 -- The tree copy has created the proper instantiation sloc for the
10058 -- new specification. Use this location for all other constructed
10059 -- declarations.
10060
10061 Loc := Sloc (Defining_Unit_Name (New_Spec));
10062
10063 -- Create new entity for the actual (New_Copy_Tree does not), and
10064 -- indicate that it is an actual.
10065
10066 New_Subp := Make_Defining_Identifier (Loc, Chars (Formal_Sub));
10067 Set_Ekind (New_Subp, Ekind (Analyzed_S));
10068 Set_Is_Generic_Actual_Subprogram (New_Subp);
10069 Set_Defining_Unit_Name (New_Spec, New_Subp);
10070
10071 -- Create new entities for the each of the formals in the specification
10072 -- of the renaming declaration built for the actual.
10073
10074 if Present (Parameter_Specifications (New_Spec)) then
10075 declare
10076 F : Node_Id;
10077 F_Id : Entity_Id;
10078
10079 begin
10080 F := First (Parameter_Specifications (New_Spec));
10081 while Present (F) loop
10082 F_Id := Defining_Identifier (F);
10083
10084 Set_Defining_Identifier (F,
10085 Make_Defining_Identifier (Sloc (F_Id), Chars (F_Id)));
10086 Next (F);
10087 end loop;
10088 end;
10089 end if;
10090
10091 -- Find entity of actual. If the actual is an attribute reference, it
10092 -- cannot be resolved here (its formal is missing) but is handled
10093 -- instead in Attribute_Renaming. If the actual is overloaded, it is
10094 -- fully resolved subsequently, when the renaming declaration for the
10095 -- formal is analyzed. If it is an explicit dereference, resolve the
10096 -- prefix but not the actual itself, to prevent interpretation as call.
10097
10098 if Present (Actual) then
10099 Loc := Sloc (Actual);
10100 Set_Sloc (New_Spec, Loc);
10101
10102 if Nkind (Actual) = N_Operator_Symbol then
10103 Find_Direct_Name (Actual);
10104
10105 elsif Nkind (Actual) = N_Explicit_Dereference then
10106 Analyze (Prefix (Actual));
10107
10108 elsif Nkind (Actual) /= N_Attribute_Reference then
10109 Analyze (Actual);
10110 end if;
10111
10112 Valid_Actual_Subprogram (Actual);
10113 Nam := Actual;
10114
10115 elsif Present (Default_Name (Formal)) then
10116 if not Nkind_In (Default_Name (Formal), N_Attribute_Reference,
10117 N_Selected_Component,
10118 N_Indexed_Component,
10119 N_Character_Literal)
10120 and then Present (Entity (Default_Name (Formal)))
10121 then
10122 Nam := New_Occurrence_Of (Entity (Default_Name (Formal)), Loc);
10123 else
10124 Nam := New_Copy (Default_Name (Formal));
10125 Set_Sloc (Nam, Loc);
10126 end if;
10127
10128 elsif Box_Present (Formal) then
10129
10130 -- Actual is resolved at the point of instantiation. Create an
10131 -- identifier or operator with the same name as the formal.
10132
10133 if Nkind (Formal_Sub) = N_Defining_Operator_Symbol then
10134 Nam :=
10135 Make_Operator_Symbol (Loc,
10136 Chars => Chars (Formal_Sub),
10137 Strval => No_String);
10138 else
10139 Nam := Make_Identifier (Loc, Chars (Formal_Sub));
10140 end if;
10141
10142 elsif Nkind (Specification (Formal)) = N_Procedure_Specification
10143 and then Null_Present (Specification (Formal))
10144 then
10145 -- Generate null body for procedure, for use in the instance
10146
10147 Decl_Node :=
10148 Make_Subprogram_Body (Loc,
10149 Specification => New_Spec,
10150 Declarations => New_List,
10151 Handled_Statement_Sequence =>
10152 Make_Handled_Sequence_Of_Statements (Loc,
10153 Statements => New_List (Make_Null_Statement (Loc))));
10154
10155 Set_Is_Intrinsic_Subprogram (Defining_Unit_Name (New_Spec));
10156 return Decl_Node;
10157
10158 else
10159 Error_Msg_Sloc := Sloc (Scope (Analyzed_S));
10160 Error_Msg_NE
10161 ("missing actual&", Instantiation_Node, Formal_Sub);
10162 Error_Msg_NE
10163 ("\in instantiation of & declared#",
10164 Instantiation_Node, Scope (Analyzed_S));
10165 Abandon_Instantiation (Instantiation_Node);
10166 end if;
10167
10168 Decl_Node :=
10169 Make_Subprogram_Renaming_Declaration (Loc,
10170 Specification => New_Spec,
10171 Name => Nam);
10172
10173 -- If we do not have an actual and the formal specified <> then set to
10174 -- get proper default.
10175
10176 if No (Actual) and then Box_Present (Formal) then
10177 Set_From_Default (Decl_Node);
10178 end if;
10179
10180 -- Gather possible interpretations for the actual before analyzing the
10181 -- instance. If overloaded, it will be resolved when analyzing the
10182 -- renaming declaration.
10183
10184 if Box_Present (Formal) and then No (Actual) then
10185 Analyze (Nam);
10186
10187 if Is_Child_Unit (Scope (Analyzed_S))
10188 and then Present (Entity (Nam))
10189 then
10190 if not Is_Overloaded (Nam) then
10191 if From_Parent_Scope (Entity (Nam)) then
10192 Set_Is_Immediately_Visible (Entity (Nam), False);
10193 Set_Entity (Nam, Empty);
10194 Set_Etype (Nam, Empty);
10195
10196 Analyze (Nam);
10197 Set_Is_Immediately_Visible (Entity (Nam));
10198 end if;
10199
10200 else
10201 declare
10202 I : Interp_Index;
10203 It : Interp;
10204
10205 begin
10206 Get_First_Interp (Nam, I, It);
10207 while Present (It.Nam) loop
10208 if From_Parent_Scope (It.Nam) then
10209 Remove_Interp (I);
10210 end if;
10211
10212 Get_Next_Interp (I, It);
10213 end loop;
10214 end;
10215 end if;
10216 end if;
10217 end if;
10218
10219 -- The generic instantiation freezes the actual. This can only be done
10220 -- once the actual is resolved, in the analysis of the renaming
10221 -- declaration. To make the formal subprogram entity available, we set
10222 -- Corresponding_Formal_Spec to point to the formal subprogram entity.
10223 -- This is also needed in Analyze_Subprogram_Renaming for the processing
10224 -- of formal abstract subprograms.
10225
10226 Set_Corresponding_Formal_Spec (Decl_Node, Analyzed_S);
10227
10228 -- We cannot analyze the renaming declaration, and thus find the actual,
10229 -- until all the actuals are assembled in the instance. For subsequent
10230 -- checks of other actuals, indicate the node that will hold the
10231 -- instance of this formal.
10232
10233 Set_Instance_Of (Analyzed_S, Nam);
10234
10235 if Nkind (Actual) = N_Selected_Component
10236 and then Is_Task_Type (Etype (Prefix (Actual)))
10237 and then not Is_Frozen (Etype (Prefix (Actual)))
10238 then
10239 -- The renaming declaration will create a body, which must appear
10240 -- outside of the instantiation, We move the renaming declaration
10241 -- out of the instance, and create an additional renaming inside,
10242 -- to prevent freezing anomalies.
10243
10244 declare
10245 Anon_Id : constant Entity_Id := Make_Temporary (Loc, 'E');
10246
10247 begin
10248 Set_Defining_Unit_Name (New_Spec, Anon_Id);
10249 Insert_Before (Instantiation_Node, Decl_Node);
10250 Analyze (Decl_Node);
10251
10252 -- Now create renaming within the instance
10253
10254 Decl_Node :=
10255 Make_Subprogram_Renaming_Declaration (Loc,
10256 Specification => New_Copy_Tree (New_Spec),
10257 Name => New_Occurrence_Of (Anon_Id, Loc));
10258
10259 Set_Defining_Unit_Name (Specification (Decl_Node),
10260 Make_Defining_Identifier (Loc, Chars (Formal_Sub)));
10261 end;
10262 end if;
10263
10264 return Decl_Node;
10265 end Instantiate_Formal_Subprogram;
10266
10267 ------------------------
10268 -- Instantiate_Object --
10269 ------------------------
10270
10271 function Instantiate_Object
10272 (Formal : Node_Id;
10273 Actual : Node_Id;
10274 Analyzed_Formal : Node_Id) return List_Id
10275 is
10276 Gen_Obj : constant Entity_Id := Defining_Identifier (Formal);
10277 A_Gen_Obj : constant Entity_Id :=
10278 Defining_Identifier (Analyzed_Formal);
10279 Acc_Def : Node_Id := Empty;
10280 Act_Assoc : constant Node_Id := Parent (Actual);
10281 Actual_Decl : Node_Id := Empty;
10282 Decl_Node : Node_Id;
10283 Def : Node_Id;
10284 Ftyp : Entity_Id;
10285 List : constant List_Id := New_List;
10286 Loc : constant Source_Ptr := Sloc (Actual);
10287 Orig_Ftyp : constant Entity_Id := Etype (A_Gen_Obj);
10288 Subt_Decl : Node_Id := Empty;
10289 Subt_Mark : Node_Id := Empty;
10290
10291 function Copy_Access_Def return Node_Id;
10292 -- If formal is an anonymous access, copy access definition of formal
10293 -- for generated object declaration.
10294
10295 ---------------------
10296 -- Copy_Access_Def --
10297 ---------------------
10298
10299 function Copy_Access_Def return Node_Id is
10300 begin
10301 Def := New_Copy_Tree (Acc_Def);
10302
10303 -- In addition, if formal is an access to subprogram we need to
10304 -- generate new formals for the signature of the default, so that
10305 -- the tree is properly formatted for ASIS use.
10306
10307 if Present (Access_To_Subprogram_Definition (Acc_Def)) then
10308 declare
10309 Par_Spec : Node_Id;
10310 begin
10311 Par_Spec :=
10312 First (Parameter_Specifications
10313 (Access_To_Subprogram_Definition (Def)));
10314 while Present (Par_Spec) loop
10315 Set_Defining_Identifier (Par_Spec,
10316 Make_Defining_Identifier (Sloc (Acc_Def),
10317 Chars => Chars (Defining_Identifier (Par_Spec))));
10318 Next (Par_Spec);
10319 end loop;
10320 end;
10321 end if;
10322
10323 return Def;
10324 end Copy_Access_Def;
10325
10326 -- Start of processing for Instantiate_Object
10327
10328 begin
10329 -- Formal may be an anonymous access
10330
10331 if Present (Subtype_Mark (Formal)) then
10332 Subt_Mark := Subtype_Mark (Formal);
10333 else
10334 Check_Access_Definition (Formal);
10335 Acc_Def := Access_Definition (Formal);
10336 end if;
10337
10338 -- Sloc for error message on missing actual
10339
10340 Error_Msg_Sloc := Sloc (Scope (A_Gen_Obj));
10341
10342 if Get_Instance_Of (Gen_Obj) /= Gen_Obj then
10343 Error_Msg_N ("duplicate instantiation of generic parameter", Actual);
10344 end if;
10345
10346 Set_Parent (List, Parent (Actual));
10347
10348 -- OUT present
10349
10350 if Out_Present (Formal) then
10351
10352 -- An IN OUT generic actual must be a name. The instantiation is a
10353 -- renaming declaration. The actual is the name being renamed. We
10354 -- use the actual directly, rather than a copy, because it is not
10355 -- used further in the list of actuals, and because a copy or a use
10356 -- of relocate_node is incorrect if the instance is nested within a
10357 -- generic. In order to simplify ASIS searches, the Generic_Parent
10358 -- field links the declaration to the generic association.
10359
10360 if No (Actual) then
10361 Error_Msg_NE
10362 ("missing actual &",
10363 Instantiation_Node, Gen_Obj);
10364 Error_Msg_NE
10365 ("\in instantiation of & declared#",
10366 Instantiation_Node, Scope (A_Gen_Obj));
10367 Abandon_Instantiation (Instantiation_Node);
10368 end if;
10369
10370 if Present (Subt_Mark) then
10371 Decl_Node :=
10372 Make_Object_Renaming_Declaration (Loc,
10373 Defining_Identifier => New_Copy (Gen_Obj),
10374 Subtype_Mark => New_Copy_Tree (Subt_Mark),
10375 Name => Actual);
10376
10377 else pragma Assert (Present (Acc_Def));
10378 Decl_Node :=
10379 Make_Object_Renaming_Declaration (Loc,
10380 Defining_Identifier => New_Copy (Gen_Obj),
10381 Access_Definition => New_Copy_Tree (Acc_Def),
10382 Name => Actual);
10383 end if;
10384
10385 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
10386
10387 -- The analysis of the actual may produce Insert_Action nodes, so
10388 -- the declaration must have a context in which to attach them.
10389
10390 Append (Decl_Node, List);
10391 Analyze (Actual);
10392
10393 -- Return if the analysis of the actual reported some error
10394
10395 if Etype (Actual) = Any_Type then
10396 return List;
10397 end if;
10398
10399 -- This check is performed here because Analyze_Object_Renaming will
10400 -- not check it when Comes_From_Source is False. Note though that the
10401 -- check for the actual being the name of an object will be performed
10402 -- in Analyze_Object_Renaming.
10403
10404 if Is_Object_Reference (Actual)
10405 and then Is_Dependent_Component_Of_Mutable_Object (Actual)
10406 then
10407 Error_Msg_N
10408 ("illegal discriminant-dependent component for in out parameter",
10409 Actual);
10410 end if;
10411
10412 -- The actual has to be resolved in order to check that it is a
10413 -- variable (due to cases such as F (1), where F returns access to
10414 -- an array, and for overloaded prefixes).
10415
10416 Ftyp := Get_Instance_Of (Etype (A_Gen_Obj));
10417
10418 -- If the type of the formal is not itself a formal, and the current
10419 -- unit is a child unit, the formal type must be declared in a
10420 -- parent, and must be retrieved by visibility.
10421
10422 if Ftyp = Orig_Ftyp
10423 and then Is_Generic_Unit (Scope (Ftyp))
10424 and then Is_Child_Unit (Scope (A_Gen_Obj))
10425 then
10426 declare
10427 Temp : constant Node_Id :=
10428 New_Copy_Tree (Subtype_Mark (Analyzed_Formal));
10429 begin
10430 Set_Entity (Temp, Empty);
10431 Find_Type (Temp);
10432 Ftyp := Entity (Temp);
10433 end;
10434 end if;
10435
10436 if Is_Private_Type (Ftyp)
10437 and then not Is_Private_Type (Etype (Actual))
10438 and then (Base_Type (Full_View (Ftyp)) = Base_Type (Etype (Actual))
10439 or else Base_Type (Etype (Actual)) = Ftyp)
10440 then
10441 -- If the actual has the type of the full view of the formal, or
10442 -- else a non-private subtype of the formal, then the visibility
10443 -- of the formal type has changed. Add to the actuals a subtype
10444 -- declaration that will force the exchange of views in the body
10445 -- of the instance as well.
10446
10447 Subt_Decl :=
10448 Make_Subtype_Declaration (Loc,
10449 Defining_Identifier => Make_Temporary (Loc, 'P'),
10450 Subtype_Indication => New_Occurrence_Of (Ftyp, Loc));
10451
10452 Prepend (Subt_Decl, List);
10453
10454 Prepend_Elmt (Full_View (Ftyp), Exchanged_Views);
10455 Exchange_Declarations (Ftyp);
10456 end if;
10457
10458 Resolve (Actual, Ftyp);
10459
10460 if not Denotes_Variable (Actual) then
10461 Error_Msg_NE ("actual for& must be a variable", Actual, Gen_Obj);
10462
10463 elsif Base_Type (Ftyp) /= Base_Type (Etype (Actual)) then
10464
10465 -- Ada 2005 (AI-423): For a generic formal object of mode in out,
10466 -- the type of the actual shall resolve to a specific anonymous
10467 -- access type.
10468
10469 if Ada_Version < Ada_2005
10470 or else Ekind (Base_Type (Ftyp)) /=
10471 E_Anonymous_Access_Type
10472 or else Ekind (Base_Type (Etype (Actual))) /=
10473 E_Anonymous_Access_Type
10474 then
10475 Error_Msg_NE
10476 ("type of actual does not match type of&", Actual, Gen_Obj);
10477 end if;
10478 end if;
10479
10480 Note_Possible_Modification (Actual, Sure => True);
10481
10482 -- Check for instantiation of atomic/volatile actual for
10483 -- non-atomic/volatile formal (RM C.6 (12)).
10484
10485 if Is_Atomic_Object (Actual) and then not Is_Atomic (Orig_Ftyp) then
10486 Error_Msg_N
10487 ("cannot instantiate non-atomic formal object "
10488 & "with atomic actual", Actual);
10489
10490 elsif Is_Volatile_Object (Actual) and then not Is_Volatile (Orig_Ftyp)
10491 then
10492 Error_Msg_N
10493 ("cannot instantiate non-volatile formal object "
10494 & "with volatile actual", Actual);
10495 end if;
10496
10497 -- Formal in-parameter
10498
10499 else
10500 -- The instantiation of a generic formal in-parameter is constant
10501 -- declaration. The actual is the expression for that declaration.
10502 -- Its type is a full copy of the type of the formal. This may be
10503 -- an access to subprogram, for which we need to generate entities
10504 -- for the formals in the new signature.
10505
10506 if Present (Actual) then
10507 if Present (Subt_Mark) then
10508 Def := New_Copy_Tree (Subt_Mark);
10509 else pragma Assert (Present (Acc_Def));
10510 Def := Copy_Access_Def;
10511 end if;
10512
10513 Decl_Node :=
10514 Make_Object_Declaration (Loc,
10515 Defining_Identifier => New_Copy (Gen_Obj),
10516 Constant_Present => True,
10517 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
10518 Object_Definition => Def,
10519 Expression => Actual);
10520
10521 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
10522
10523 -- A generic formal object of a tagged type is defined to be
10524 -- aliased so the new constant must also be treated as aliased.
10525
10526 if Is_Tagged_Type (Etype (A_Gen_Obj)) then
10527 Set_Aliased_Present (Decl_Node);
10528 end if;
10529
10530 Append (Decl_Node, List);
10531
10532 -- No need to repeat (pre-)analysis of some expression nodes
10533 -- already handled in Preanalyze_Actuals.
10534
10535 if Nkind (Actual) /= N_Allocator then
10536 Analyze (Actual);
10537
10538 -- Return if the analysis of the actual reported some error
10539
10540 if Etype (Actual) = Any_Type then
10541 return List;
10542 end if;
10543 end if;
10544
10545 declare
10546 Formal_Type : constant Entity_Id := Etype (A_Gen_Obj);
10547 Typ : Entity_Id;
10548
10549 begin
10550 Typ := Get_Instance_Of (Formal_Type);
10551
10552 -- If the actual appears in the current or an enclosing scope,
10553 -- use its type directly. This is relevant if it has an actual
10554 -- subtype that is distinct from its nominal one. This cannot
10555 -- be done in general because the type of the actual may
10556 -- depend on other actuals, and only be fully determined when
10557 -- the enclosing instance is analyzed.
10558
10559 if Present (Etype (Actual))
10560 and then Is_Constr_Subt_For_U_Nominal (Etype (Actual))
10561 then
10562 Freeze_Before (Instantiation_Node, Etype (Actual));
10563 else
10564 Freeze_Before (Instantiation_Node, Typ);
10565 end if;
10566
10567 -- If the actual is an aggregate, perform name resolution on
10568 -- its components (the analysis of an aggregate does not do it)
10569 -- to capture local names that may be hidden if the generic is
10570 -- a child unit.
10571
10572 if Nkind (Actual) = N_Aggregate then
10573 Preanalyze_And_Resolve (Actual, Typ);
10574 end if;
10575
10576 if Is_Limited_Type (Typ)
10577 and then not OK_For_Limited_Init (Typ, Actual)
10578 then
10579 Error_Msg_N
10580 ("initialization not allowed for limited types", Actual);
10581 Explain_Limited_Type (Typ, Actual);
10582 end if;
10583 end;
10584
10585 elsif Present (Default_Expression (Formal)) then
10586
10587 -- Use default to construct declaration
10588
10589 if Present (Subt_Mark) then
10590 Def := New_Copy (Subt_Mark);
10591 else pragma Assert (Present (Acc_Def));
10592 Def := Copy_Access_Def;
10593 end if;
10594
10595 Decl_Node :=
10596 Make_Object_Declaration (Sloc (Formal),
10597 Defining_Identifier => New_Copy (Gen_Obj),
10598 Constant_Present => True,
10599 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
10600 Object_Definition => Def,
10601 Expression => New_Copy_Tree
10602 (Default_Expression (Formal)));
10603
10604 Append (Decl_Node, List);
10605 Set_Analyzed (Expression (Decl_Node), False);
10606
10607 else
10608 Error_Msg_NE ("missing actual&", Instantiation_Node, Gen_Obj);
10609 Error_Msg_NE ("\in instantiation of & declared#",
10610 Instantiation_Node, Scope (A_Gen_Obj));
10611
10612 if Is_Scalar_Type (Etype (A_Gen_Obj)) then
10613
10614 -- Create dummy constant declaration so that instance can be
10615 -- analyzed, to minimize cascaded visibility errors.
10616
10617 if Present (Subt_Mark) then
10618 Def := Subt_Mark;
10619 else pragma Assert (Present (Acc_Def));
10620 Def := Acc_Def;
10621 end if;
10622
10623 Decl_Node :=
10624 Make_Object_Declaration (Loc,
10625 Defining_Identifier => New_Copy (Gen_Obj),
10626 Constant_Present => True,
10627 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
10628 Object_Definition => New_Copy (Def),
10629 Expression =>
10630 Make_Attribute_Reference (Sloc (Gen_Obj),
10631 Attribute_Name => Name_First,
10632 Prefix => New_Copy (Def)));
10633
10634 Append (Decl_Node, List);
10635
10636 else
10637 Abandon_Instantiation (Instantiation_Node);
10638 end if;
10639 end if;
10640 end if;
10641
10642 if Nkind (Actual) in N_Has_Entity then
10643 Actual_Decl := Parent (Entity (Actual));
10644 end if;
10645
10646 -- Ada 2005 (AI-423): For a formal object declaration with a null
10647 -- exclusion or an access definition that has a null exclusion: If the
10648 -- actual matching the formal object declaration denotes a generic
10649 -- formal object of another generic unit G, and the instantiation
10650 -- containing the actual occurs within the body of G or within the body
10651 -- of a generic unit declared within the declarative region of G, then
10652 -- the declaration of the formal object of G must have a null exclusion.
10653 -- Otherwise, the subtype of the actual matching the formal object
10654 -- declaration shall exclude null.
10655
10656 if Ada_Version >= Ada_2005
10657 and then Present (Actual_Decl)
10658 and then Nkind_In (Actual_Decl, N_Formal_Object_Declaration,
10659 N_Object_Declaration)
10660 and then Nkind (Analyzed_Formal) = N_Formal_Object_Declaration
10661 and then not Has_Null_Exclusion (Actual_Decl)
10662 and then Has_Null_Exclusion (Analyzed_Formal)
10663 then
10664 Error_Msg_Sloc := Sloc (Analyzed_Formal);
10665 Error_Msg_N
10666 ("actual must exclude null to match generic formal#", Actual);
10667 end if;
10668
10669 -- An effectively volatile object cannot be used as an actual in a
10670 -- generic instantiation (SPARK RM 7.1.3(7)). The following check is
10671 -- relevant only when SPARK_Mode is on as it is not a standard Ada
10672 -- legality rule.
10673
10674 if SPARK_Mode = On
10675 and then Present (Actual)
10676 and then Is_Effectively_Volatile_Object (Actual)
10677 then
10678 Error_Msg_N
10679 ("volatile object cannot act as actual in generic instantiation",
10680 Actual);
10681 end if;
10682
10683 return List;
10684 end Instantiate_Object;
10685
10686 ------------------------------
10687 -- Instantiate_Package_Body --
10688 ------------------------------
10689
10690 procedure Instantiate_Package_Body
10691 (Body_Info : Pending_Body_Info;
10692 Inlined_Body : Boolean := False;
10693 Body_Optional : Boolean := False)
10694 is
10695 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
10696 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
10697 Loc : constant Source_Ptr := Sloc (Inst_Node);
10698
10699 Gen_Id : constant Node_Id := Name (Inst_Node);
10700 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
10701 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
10702 Act_Spec : constant Node_Id := Specification (Act_Decl);
10703 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Spec);
10704
10705 Save_IPSM : constant Boolean := Ignore_Pragma_SPARK_Mode;
10706 Save_Style_Check : constant Boolean := Style_Check;
10707
10708 Act_Body : Node_Id;
10709 Act_Body_Id : Entity_Id;
10710 Act_Body_Name : Node_Id;
10711 Gen_Body : Node_Id;
10712 Gen_Body_Id : Node_Id;
10713 Par_Ent : Entity_Id := Empty;
10714 Par_Vis : Boolean := False;
10715
10716 Parent_Installed : Boolean := False;
10717
10718 Vis_Prims_List : Elist_Id := No_Elist;
10719 -- List of primitives made temporarily visible in the instantiation
10720 -- to match the visibility of the formal type
10721
10722 procedure Check_Initialized_Types;
10723 -- In a generic package body, an entity of a generic private type may
10724 -- appear uninitialized. This is suspicious, unless the actual is a
10725 -- fully initialized type.
10726
10727 -----------------------------
10728 -- Check_Initialized_Types --
10729 -----------------------------
10730
10731 procedure Check_Initialized_Types is
10732 Decl : Node_Id;
10733 Formal : Entity_Id;
10734 Actual : Entity_Id;
10735 Uninit_Var : Entity_Id;
10736
10737 begin
10738 Decl := First (Generic_Formal_Declarations (Gen_Decl));
10739 while Present (Decl) loop
10740 Uninit_Var := Empty;
10741
10742 if Nkind (Decl) = N_Private_Extension_Declaration then
10743 Uninit_Var := Uninitialized_Variable (Decl);
10744
10745 elsif Nkind (Decl) = N_Formal_Type_Declaration
10746 and then Nkind (Formal_Type_Definition (Decl)) =
10747 N_Formal_Private_Type_Definition
10748 then
10749 Uninit_Var :=
10750 Uninitialized_Variable (Formal_Type_Definition (Decl));
10751 end if;
10752
10753 if Present (Uninit_Var) then
10754 Formal := Defining_Identifier (Decl);
10755 Actual := First_Entity (Act_Decl_Id);
10756
10757 -- For each formal there is a subtype declaration that renames
10758 -- the actual and has the same name as the formal. Locate the
10759 -- formal for warning message about uninitialized variables
10760 -- in the generic, for which the actual type should be a fully
10761 -- initialized type.
10762
10763 while Present (Actual) loop
10764 exit when Ekind (Actual) = E_Package
10765 and then Present (Renamed_Object (Actual));
10766
10767 if Chars (Actual) = Chars (Formal)
10768 and then not Is_Scalar_Type (Actual)
10769 and then not Is_Fully_Initialized_Type (Actual)
10770 and then Warn_On_No_Value_Assigned
10771 then
10772 Error_Msg_Node_2 := Formal;
10773 Error_Msg_NE
10774 ("generic unit has uninitialized variable& of "
10775 & "formal private type &?v?", Actual, Uninit_Var);
10776 Error_Msg_NE
10777 ("actual type for& should be fully initialized type?v?",
10778 Actual, Formal);
10779 exit;
10780 end if;
10781
10782 Next_Entity (Actual);
10783 end loop;
10784 end if;
10785
10786 Next (Decl);
10787 end loop;
10788 end Check_Initialized_Types;
10789
10790 -- Start of processing for Instantiate_Package_Body
10791
10792 begin
10793 Gen_Body_Id := Corresponding_Body (Gen_Decl);
10794
10795 -- The instance body may already have been processed, as the parent of
10796 -- another instance that is inlined (Load_Parent_Of_Generic).
10797
10798 if Present (Corresponding_Body (Instance_Spec (Inst_Node))) then
10799 return;
10800 end if;
10801
10802 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
10803
10804 -- Re-establish the state of information on which checks are suppressed.
10805 -- This information was set in Body_Info at the point of instantiation,
10806 -- and now we restore it so that the instance is compiled using the
10807 -- check status at the instantiation (RM 11.5(7.2/2), AI95-00224-01).
10808
10809 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
10810 Scope_Suppress := Body_Info.Scope_Suppress;
10811 Opt.Ada_Version := Body_Info.Version;
10812 Opt.Ada_Version_Pragma := Body_Info.Version_Pragma;
10813 Restore_Warnings (Body_Info.Warnings);
10814 Opt.SPARK_Mode := Body_Info.SPARK_Mode;
10815 Opt.SPARK_Mode_Pragma := Body_Info.SPARK_Mode_Pragma;
10816
10817 if No (Gen_Body_Id) then
10818
10819 -- Do not look for parent of generic body if none is required.
10820 -- This may happen when the routine is called as part of the
10821 -- Pending_Instantiations processing, when nested instances
10822 -- may precede the one generated from the main unit.
10823
10824 if not Unit_Requires_Body (Defining_Entity (Gen_Decl))
10825 and then Body_Optional
10826 then
10827 return;
10828 else
10829 Load_Parent_Of_Generic
10830 (Inst_Node, Specification (Gen_Decl), Body_Optional);
10831 Gen_Body_Id := Corresponding_Body (Gen_Decl);
10832 end if;
10833 end if;
10834
10835 -- Establish global variable for sloc adjustment and for error recovery
10836 -- In the case of an instance body for an instantiation with actuals
10837 -- from a limited view, the instance body is placed at the beginning
10838 -- of the enclosing package body: use the body entity as the source
10839 -- location for nodes of the instance body.
10840
10841 if not Is_Empty_Elmt_List (Incomplete_Actuals (Act_Decl_Id)) then
10842 declare
10843 Scop : constant Entity_Id := Scope (Act_Decl_Id);
10844 Body_Id : constant Node_Id :=
10845 Corresponding_Body (Unit_Declaration_Node (Scop));
10846
10847 begin
10848 Instantiation_Node := Body_Id;
10849 end;
10850 else
10851 Instantiation_Node := Inst_Node;
10852 end if;
10853
10854 if Present (Gen_Body_Id) then
10855 Save_Env (Gen_Unit, Act_Decl_Id);
10856 Style_Check := False;
10857
10858 -- If the context of the instance is subject to SPARK_Mode "off" or
10859 -- the annotation is altogether missing, set the global flag which
10860 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
10861 -- the instance.
10862
10863 if SPARK_Mode /= On then
10864 Ignore_Pragma_SPARK_Mode := True;
10865 end if;
10866
10867 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
10868 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
10869
10870 Create_Instantiation_Source
10871 (Inst_Node, Gen_Body_Id, False, S_Adjustment);
10872
10873 Act_Body :=
10874 Copy_Generic_Node
10875 (Original_Node (Gen_Body), Empty, Instantiating => True);
10876
10877 -- Create proper (possibly qualified) defining name for the body, to
10878 -- correspond to the one in the spec.
10879
10880 Act_Body_Id :=
10881 Make_Defining_Identifier (Sloc (Act_Decl_Id), Chars (Act_Decl_Id));
10882 Set_Comes_From_Source (Act_Body_Id, Comes_From_Source (Act_Decl_Id));
10883
10884 -- Some attributes of spec entity are not inherited by body entity
10885
10886 Set_Handler_Records (Act_Body_Id, No_List);
10887
10888 if Nkind (Defining_Unit_Name (Act_Spec)) =
10889 N_Defining_Program_Unit_Name
10890 then
10891 Act_Body_Name :=
10892 Make_Defining_Program_Unit_Name (Loc,
10893 Name =>
10894 New_Copy_Tree (Name (Defining_Unit_Name (Act_Spec))),
10895 Defining_Identifier => Act_Body_Id);
10896 else
10897 Act_Body_Name := Act_Body_Id;
10898 end if;
10899
10900 Set_Defining_Unit_Name (Act_Body, Act_Body_Name);
10901
10902 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
10903 Check_Generic_Actuals (Act_Decl_Id, False);
10904 Check_Initialized_Types;
10905
10906 -- Install primitives hidden at the point of the instantiation but
10907 -- visible when processing the generic formals
10908
10909 declare
10910 E : Entity_Id;
10911
10912 begin
10913 E := First_Entity (Act_Decl_Id);
10914 while Present (E) loop
10915 if Is_Type (E)
10916 and then Is_Generic_Actual_Type (E)
10917 and then Is_Tagged_Type (E)
10918 then
10919 Install_Hidden_Primitives
10920 (Prims_List => Vis_Prims_List,
10921 Gen_T => Generic_Parent_Type (Parent (E)),
10922 Act_T => E);
10923 end if;
10924
10925 Next_Entity (E);
10926 end loop;
10927 end;
10928
10929 -- If it is a child unit, make the parent instance (which is an
10930 -- instance of the parent of the generic) visible. The parent
10931 -- instance is the prefix of the name of the generic unit.
10932
10933 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
10934 and then Nkind (Gen_Id) = N_Expanded_Name
10935 then
10936 Par_Ent := Entity (Prefix (Gen_Id));
10937 Par_Vis := Is_Immediately_Visible (Par_Ent);
10938 Install_Parent (Par_Ent, In_Body => True);
10939 Parent_Installed := True;
10940
10941 elsif Is_Child_Unit (Gen_Unit) then
10942 Par_Ent := Scope (Gen_Unit);
10943 Par_Vis := Is_Immediately_Visible (Par_Ent);
10944 Install_Parent (Par_Ent, In_Body => True);
10945 Parent_Installed := True;
10946 end if;
10947
10948 -- If the instantiation is a library unit, and this is the main unit,
10949 -- then build the resulting compilation unit nodes for the instance.
10950 -- If this is a compilation unit but it is not the main unit, then it
10951 -- is the body of a unit in the context, that is being compiled
10952 -- because it is encloses some inlined unit or another generic unit
10953 -- being instantiated. In that case, this body is not part of the
10954 -- current compilation, and is not attached to the tree, but its
10955 -- parent must be set for analysis.
10956
10957 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
10958
10959 -- Replace instance node with body of instance, and create new
10960 -- node for corresponding instance declaration.
10961
10962 Build_Instance_Compilation_Unit_Nodes
10963 (Inst_Node, Act_Body, Act_Decl);
10964 Analyze (Inst_Node);
10965
10966 if Parent (Inst_Node) = Cunit (Main_Unit) then
10967
10968 -- If the instance is a child unit itself, then set the scope
10969 -- of the expanded body to be the parent of the instantiation
10970 -- (ensuring that the fully qualified name will be generated
10971 -- for the elaboration subprogram).
10972
10973 if Nkind (Defining_Unit_Name (Act_Spec)) =
10974 N_Defining_Program_Unit_Name
10975 then
10976 Set_Scope (Defining_Entity (Inst_Node), Scope (Act_Decl_Id));
10977 end if;
10978 end if;
10979
10980 -- Case where instantiation is not a library unit
10981
10982 else
10983 -- If this is an early instantiation, i.e. appears textually
10984 -- before the corresponding body and must be elaborated first,
10985 -- indicate that the body instance is to be delayed.
10986
10987 Install_Body (Act_Body, Inst_Node, Gen_Body, Gen_Decl);
10988
10989 -- Now analyze the body. We turn off all checks if this is an
10990 -- internal unit, since there is no reason to have checks on for
10991 -- any predefined run-time library code. All such code is designed
10992 -- to be compiled with checks off.
10993
10994 -- Note that we do NOT apply this criterion to children of GNAT
10995 -- The latter units must suppress checks explicitly if needed.
10996
10997 if Is_Predefined_File_Name
10998 (Unit_File_Name (Get_Source_Unit (Gen_Decl)))
10999 then
11000 Analyze (Act_Body, Suppress => All_Checks);
11001 else
11002 Analyze (Act_Body);
11003 end if;
11004 end if;
11005
11006 Inherit_Context (Gen_Body, Inst_Node);
11007
11008 -- Remove the parent instances if they have been placed on the scope
11009 -- stack to compile the body.
11010
11011 if Parent_Installed then
11012 Remove_Parent (In_Body => True);
11013
11014 -- Restore the previous visibility of the parent
11015
11016 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
11017 end if;
11018
11019 Restore_Hidden_Primitives (Vis_Prims_List);
11020 Restore_Private_Views (Act_Decl_Id);
11021
11022 -- Remove the current unit from visibility if this is an instance
11023 -- that is not elaborated on the fly for inlining purposes.
11024
11025 if not Inlined_Body then
11026 Set_Is_Immediately_Visible (Act_Decl_Id, False);
11027 end if;
11028
11029 Restore_Env;
11030 Ignore_Pragma_SPARK_Mode := Save_IPSM;
11031 Style_Check := Save_Style_Check;
11032
11033 -- If we have no body, and the unit requires a body, then complain. This
11034 -- complaint is suppressed if we have detected other errors (since a
11035 -- common reason for missing the body is that it had errors).
11036 -- In CodePeer mode, a warning has been emitted already, no need for
11037 -- further messages.
11038
11039 elsif Unit_Requires_Body (Gen_Unit)
11040 and then not Body_Optional
11041 then
11042 if CodePeer_Mode then
11043 null;
11044
11045 elsif Serious_Errors_Detected = 0 then
11046 Error_Msg_NE
11047 ("cannot find body of generic package &", Inst_Node, Gen_Unit);
11048
11049 -- Don't attempt to perform any cleanup actions if some other error
11050 -- was already detected, since this can cause blowups.
11051
11052 else
11053 return;
11054 end if;
11055
11056 -- Case of package that does not need a body
11057
11058 else
11059 -- If the instantiation of the declaration is a library unit, rewrite
11060 -- the original package instantiation as a package declaration in the
11061 -- compilation unit node.
11062
11063 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
11064 Set_Parent_Spec (Act_Decl, Parent_Spec (Inst_Node));
11065 Rewrite (Inst_Node, Act_Decl);
11066
11067 -- Generate elaboration entity, in case spec has elaboration code.
11068 -- This cannot be done when the instance is analyzed, because it
11069 -- is not known yet whether the body exists.
11070
11071 Set_Elaboration_Entity_Required (Act_Decl_Id, False);
11072 Build_Elaboration_Entity (Parent (Inst_Node), Act_Decl_Id);
11073
11074 -- If the instantiation is not a library unit, then append the
11075 -- declaration to the list of implicitly generated entities, unless
11076 -- it is already a list member which means that it was already
11077 -- processed
11078
11079 elsif not Is_List_Member (Act_Decl) then
11080 Mark_Rewrite_Insertion (Act_Decl);
11081 Insert_Before (Inst_Node, Act_Decl);
11082 end if;
11083 end if;
11084
11085 Expander_Mode_Restore;
11086 end Instantiate_Package_Body;
11087
11088 ---------------------------------
11089 -- Instantiate_Subprogram_Body --
11090 ---------------------------------
11091
11092 procedure Instantiate_Subprogram_Body
11093 (Body_Info : Pending_Body_Info;
11094 Body_Optional : Boolean := False)
11095 is
11096 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
11097 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
11098 Loc : constant Source_Ptr := Sloc (Inst_Node);
11099 Gen_Id : constant Node_Id := Name (Inst_Node);
11100 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
11101 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
11102 Act_Decl_Id : constant Entity_Id :=
11103 Defining_Unit_Name (Specification (Act_Decl));
11104 Pack_Id : constant Entity_Id :=
11105 Defining_Unit_Name (Parent (Act_Decl));
11106
11107 Saved_IPSM : constant Boolean := Ignore_Pragma_SPARK_Mode;
11108 Saved_Style_Check : constant Boolean := Style_Check;
11109 Saved_Warnings : constant Warning_Record := Save_Warnings;
11110
11111 Act_Body : Node_Id;
11112 Act_Body_Id : Entity_Id;
11113 Gen_Body : Node_Id;
11114 Gen_Body_Id : Node_Id;
11115 Pack_Body : Node_Id;
11116 Par_Ent : Entity_Id := Empty;
11117 Par_Vis : Boolean := False;
11118 Ret_Expr : Node_Id;
11119
11120 Parent_Installed : Boolean := False;
11121
11122 begin
11123 Gen_Body_Id := Corresponding_Body (Gen_Decl);
11124
11125 -- Subprogram body may have been created already because of an inline
11126 -- pragma, or because of multiple elaborations of the enclosing package
11127 -- when several instances of the subprogram appear in the main unit.
11128
11129 if Present (Corresponding_Body (Act_Decl)) then
11130 return;
11131 end if;
11132
11133 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
11134
11135 -- Re-establish the state of information on which checks are suppressed.
11136 -- This information was set in Body_Info at the point of instantiation,
11137 -- and now we restore it so that the instance is compiled using the
11138 -- check status at the instantiation (RM 11.5(7.2/2), AI95-00224-01).
11139
11140 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
11141 Scope_Suppress := Body_Info.Scope_Suppress;
11142 Opt.Ada_Version := Body_Info.Version;
11143 Opt.Ada_Version_Pragma := Body_Info.Version_Pragma;
11144 Restore_Warnings (Body_Info.Warnings);
11145 Opt.SPARK_Mode := Body_Info.SPARK_Mode;
11146 Opt.SPARK_Mode_Pragma := Body_Info.SPARK_Mode_Pragma;
11147
11148 if No (Gen_Body_Id) then
11149
11150 -- For imported generic subprogram, no body to compile, complete
11151 -- the spec entity appropriately.
11152
11153 if Is_Imported (Gen_Unit) then
11154 Set_Is_Imported (Act_Decl_Id);
11155 Set_First_Rep_Item (Act_Decl_Id, First_Rep_Item (Gen_Unit));
11156 Set_Interface_Name (Act_Decl_Id, Interface_Name (Gen_Unit));
11157 Set_Convention (Act_Decl_Id, Convention (Gen_Unit));
11158 Set_Has_Completion (Act_Decl_Id);
11159 return;
11160
11161 -- For other cases, compile the body
11162
11163 else
11164 Load_Parent_Of_Generic
11165 (Inst_Node, Specification (Gen_Decl), Body_Optional);
11166 Gen_Body_Id := Corresponding_Body (Gen_Decl);
11167 end if;
11168 end if;
11169
11170 Instantiation_Node := Inst_Node;
11171
11172 if Present (Gen_Body_Id) then
11173 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
11174
11175 if Nkind (Gen_Body) = N_Subprogram_Body_Stub then
11176
11177 -- Either body is not present, or context is non-expanding, as
11178 -- when compiling a subunit. Mark the instance as completed, and
11179 -- diagnose a missing body when needed.
11180
11181 if Expander_Active
11182 and then Operating_Mode = Generate_Code
11183 then
11184 Error_Msg_N
11185 ("missing proper body for instantiation", Gen_Body);
11186 end if;
11187
11188 Set_Has_Completion (Act_Decl_Id);
11189 return;
11190 end if;
11191
11192 Save_Env (Gen_Unit, Act_Decl_Id);
11193 Style_Check := False;
11194
11195 -- If the context of the instance is subject to SPARK_Mode "off" or
11196 -- the annotation is altogether missing, set the global flag which
11197 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
11198 -- the instance.
11199
11200 if SPARK_Mode /= On then
11201 Ignore_Pragma_SPARK_Mode := True;
11202 end if;
11203
11204 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
11205 Create_Instantiation_Source
11206 (Inst_Node,
11207 Gen_Body_Id,
11208 False,
11209 S_Adjustment);
11210
11211 Act_Body :=
11212 Copy_Generic_Node
11213 (Original_Node (Gen_Body), Empty, Instantiating => True);
11214
11215 -- Create proper defining name for the body, to correspond to the one
11216 -- in the spec.
11217
11218 Act_Body_Id :=
11219 Make_Defining_Identifier (Sloc (Act_Decl_Id), Chars (Act_Decl_Id));
11220
11221 Set_Comes_From_Source (Act_Body_Id, Comes_From_Source (Act_Decl_Id));
11222 Set_Defining_Unit_Name (Specification (Act_Body), Act_Body_Id);
11223
11224 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
11225 Set_Has_Completion (Act_Decl_Id);
11226 Check_Generic_Actuals (Pack_Id, False);
11227
11228 -- Generate a reference to link the visible subprogram instance to
11229 -- the generic body, which for navigation purposes is the only
11230 -- available source for the instance.
11231
11232 Generate_Reference
11233 (Related_Instance (Pack_Id),
11234 Gen_Body_Id, 'b', Set_Ref => False, Force => True);
11235
11236 -- If it is a child unit, make the parent instance (which is an
11237 -- instance of the parent of the generic) visible. The parent
11238 -- instance is the prefix of the name of the generic unit.
11239
11240 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
11241 and then Nkind (Gen_Id) = N_Expanded_Name
11242 then
11243 Par_Ent := Entity (Prefix (Gen_Id));
11244 Par_Vis := Is_Immediately_Visible (Par_Ent);
11245 Install_Parent (Par_Ent, In_Body => True);
11246 Parent_Installed := True;
11247
11248 elsif Is_Child_Unit (Gen_Unit) then
11249 Par_Ent := Scope (Gen_Unit);
11250 Par_Vis := Is_Immediately_Visible (Par_Ent);
11251 Install_Parent (Par_Ent, In_Body => True);
11252 Parent_Installed := True;
11253 end if;
11254
11255 -- Subprogram body is placed in the body of wrapper package,
11256 -- whose spec contains the subprogram declaration as well as
11257 -- the renaming declarations for the generic parameters.
11258
11259 Pack_Body :=
11260 Make_Package_Body (Loc,
11261 Defining_Unit_Name => New_Copy (Pack_Id),
11262 Declarations => New_List (Act_Body));
11263
11264 Set_Corresponding_Spec (Pack_Body, Pack_Id);
11265
11266 -- If the instantiation is a library unit, then build resulting
11267 -- compilation unit nodes for the instance. The declaration of
11268 -- the enclosing package is the grandparent of the subprogram
11269 -- declaration. First replace the instantiation node as the unit
11270 -- of the corresponding compilation.
11271
11272 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
11273 if Parent (Inst_Node) = Cunit (Main_Unit) then
11274 Set_Unit (Parent (Inst_Node), Inst_Node);
11275 Build_Instance_Compilation_Unit_Nodes
11276 (Inst_Node, Pack_Body, Parent (Parent (Act_Decl)));
11277 Analyze (Inst_Node);
11278 else
11279 Set_Parent (Pack_Body, Parent (Inst_Node));
11280 Analyze (Pack_Body);
11281 end if;
11282
11283 else
11284 Insert_Before (Inst_Node, Pack_Body);
11285 Mark_Rewrite_Insertion (Pack_Body);
11286 Analyze (Pack_Body);
11287
11288 if Expander_Active then
11289 Freeze_Subprogram_Body (Inst_Node, Gen_Body, Pack_Id);
11290 end if;
11291 end if;
11292
11293 Inherit_Context (Gen_Body, Inst_Node);
11294
11295 Restore_Private_Views (Pack_Id, False);
11296
11297 if Parent_Installed then
11298 Remove_Parent (In_Body => True);
11299
11300 -- Restore the previous visibility of the parent
11301
11302 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
11303 end if;
11304
11305 Restore_Env;
11306 Ignore_Pragma_SPARK_Mode := Saved_IPSM;
11307 Style_Check := Saved_Style_Check;
11308 Restore_Warnings (Saved_Warnings);
11309
11310 -- Body not found. Error was emitted already. If there were no previous
11311 -- errors, this may be an instance whose scope is a premature instance.
11312 -- In that case we must insure that the (legal) program does raise
11313 -- program error if executed. We generate a subprogram body for this
11314 -- purpose. See DEC ac30vso.
11315
11316 -- Should not reference proprietary DEC tests in comments ???
11317
11318 elsif Serious_Errors_Detected = 0
11319 and then Nkind (Parent (Inst_Node)) /= N_Compilation_Unit
11320 then
11321 if Body_Optional then
11322 return;
11323
11324 elsif Ekind (Act_Decl_Id) = E_Procedure then
11325 Act_Body :=
11326 Make_Subprogram_Body (Loc,
11327 Specification =>
11328 Make_Procedure_Specification (Loc,
11329 Defining_Unit_Name =>
11330 Make_Defining_Identifier (Loc, Chars (Act_Decl_Id)),
11331 Parameter_Specifications =>
11332 New_Copy_List
11333 (Parameter_Specifications (Parent (Act_Decl_Id)))),
11334
11335 Declarations => Empty_List,
11336 Handled_Statement_Sequence =>
11337 Make_Handled_Sequence_Of_Statements (Loc,
11338 Statements =>
11339 New_List (
11340 Make_Raise_Program_Error (Loc,
11341 Reason =>
11342 PE_Access_Before_Elaboration))));
11343
11344 else
11345 Ret_Expr :=
11346 Make_Raise_Program_Error (Loc,
11347 Reason => PE_Access_Before_Elaboration);
11348
11349 Set_Etype (Ret_Expr, (Etype (Act_Decl_Id)));
11350 Set_Analyzed (Ret_Expr);
11351
11352 Act_Body :=
11353 Make_Subprogram_Body (Loc,
11354 Specification =>
11355 Make_Function_Specification (Loc,
11356 Defining_Unit_Name =>
11357 Make_Defining_Identifier (Loc, Chars (Act_Decl_Id)),
11358 Parameter_Specifications =>
11359 New_Copy_List
11360 (Parameter_Specifications (Parent (Act_Decl_Id))),
11361 Result_Definition =>
11362 New_Occurrence_Of (Etype (Act_Decl_Id), Loc)),
11363
11364 Declarations => Empty_List,
11365 Handled_Statement_Sequence =>
11366 Make_Handled_Sequence_Of_Statements (Loc,
11367 Statements =>
11368 New_List
11369 (Make_Simple_Return_Statement (Loc, Ret_Expr))));
11370 end if;
11371
11372 Pack_Body :=
11373 Make_Package_Body (Loc,
11374 Defining_Unit_Name => New_Copy (Pack_Id),
11375 Declarations => New_List (Act_Body));
11376
11377 Insert_After (Inst_Node, Pack_Body);
11378 Set_Corresponding_Spec (Pack_Body, Pack_Id);
11379 Analyze (Pack_Body);
11380 end if;
11381
11382 Expander_Mode_Restore;
11383 end Instantiate_Subprogram_Body;
11384
11385 ----------------------
11386 -- Instantiate_Type --
11387 ----------------------
11388
11389 function Instantiate_Type
11390 (Formal : Node_Id;
11391 Actual : Node_Id;
11392 Analyzed_Formal : Node_Id;
11393 Actual_Decls : List_Id) return List_Id
11394 is
11395 Gen_T : constant Entity_Id := Defining_Identifier (Formal);
11396 A_Gen_T : constant Entity_Id :=
11397 Defining_Identifier (Analyzed_Formal);
11398 Ancestor : Entity_Id := Empty;
11399 Def : constant Node_Id := Formal_Type_Definition (Formal);
11400 Act_T : Entity_Id;
11401 Decl_Node : Node_Id;
11402 Decl_Nodes : List_Id;
11403 Loc : Source_Ptr;
11404 Subt : Entity_Id;
11405
11406 procedure Diagnose_Predicated_Actual;
11407 -- There are a number of constructs in which a discrete type with
11408 -- predicates is illegal, e.g. as an index in an array type declaration.
11409 -- If a generic type is used is such a construct in a generic package
11410 -- declaration, it carries the flag No_Predicate_On_Actual. it is part
11411 -- of the generic contract that the actual cannot have predicates.
11412
11413 procedure Validate_Array_Type_Instance;
11414 procedure Validate_Access_Subprogram_Instance;
11415 procedure Validate_Access_Type_Instance;
11416 procedure Validate_Derived_Type_Instance;
11417 procedure Validate_Derived_Interface_Type_Instance;
11418 procedure Validate_Discriminated_Formal_Type;
11419 procedure Validate_Interface_Type_Instance;
11420 procedure Validate_Private_Type_Instance;
11421 procedure Validate_Incomplete_Type_Instance;
11422 -- These procedures perform validation tests for the named case.
11423 -- Validate_Discriminated_Formal_Type is shared by formal private
11424 -- types and Ada 2012 formal incomplete types.
11425
11426 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean;
11427 -- Check that base types are the same and that the subtypes match
11428 -- statically. Used in several of the above.
11429
11430 ---------------------------------
11431 -- Diagnose_Predicated_Actual --
11432 ---------------------------------
11433
11434 procedure Diagnose_Predicated_Actual is
11435 begin
11436 if No_Predicate_On_Actual (A_Gen_T)
11437 and then Has_Predicates (Act_T)
11438 then
11439 Error_Msg_NE
11440 ("actual for& cannot be a type with predicate",
11441 Instantiation_Node, A_Gen_T);
11442
11443 elsif No_Dynamic_Predicate_On_Actual (A_Gen_T)
11444 and then Has_Predicates (Act_T)
11445 and then not Has_Static_Predicate_Aspect (Act_T)
11446 then
11447 Error_Msg_NE
11448 ("actual for& cannot be a type with a dynamic predicate",
11449 Instantiation_Node, A_Gen_T);
11450 end if;
11451 end Diagnose_Predicated_Actual;
11452
11453 --------------------
11454 -- Subtypes_Match --
11455 --------------------
11456
11457 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean is
11458 T : constant Entity_Id := Get_Instance_Of (Gen_T);
11459
11460 begin
11461 -- Some detailed comments would be useful here ???
11462
11463 return ((Base_Type (T) = Act_T
11464 or else Base_Type (T) = Base_Type (Act_T))
11465 and then Subtypes_Statically_Match (T, Act_T))
11466
11467 or else (Is_Class_Wide_Type (Gen_T)
11468 and then Is_Class_Wide_Type (Act_T)
11469 and then Subtypes_Match
11470 (Get_Instance_Of (Root_Type (Gen_T)),
11471 Root_Type (Act_T)))
11472
11473 or else
11474 (Ekind_In (Gen_T, E_Anonymous_Access_Subprogram_Type,
11475 E_Anonymous_Access_Type)
11476 and then Ekind (Act_T) = Ekind (Gen_T)
11477 and then Subtypes_Statically_Match
11478 (Designated_Type (Gen_T), Designated_Type (Act_T)));
11479 end Subtypes_Match;
11480
11481 -----------------------------------------
11482 -- Validate_Access_Subprogram_Instance --
11483 -----------------------------------------
11484
11485 procedure Validate_Access_Subprogram_Instance is
11486 begin
11487 if not Is_Access_Type (Act_T)
11488 or else Ekind (Designated_Type (Act_T)) /= E_Subprogram_Type
11489 then
11490 Error_Msg_NE
11491 ("expect access type in instantiation of &", Actual, Gen_T);
11492 Abandon_Instantiation (Actual);
11493 end if;
11494
11495 -- According to AI05-288, actuals for access_to_subprograms must be
11496 -- subtype conformant with the generic formal. Previous to AI05-288
11497 -- only mode conformance was required.
11498
11499 -- This is a binding interpretation that applies to previous versions
11500 -- of the language, no need to maintain previous weaker checks.
11501
11502 Check_Subtype_Conformant
11503 (Designated_Type (Act_T),
11504 Designated_Type (A_Gen_T),
11505 Actual,
11506 Get_Inst => True);
11507
11508 if Ekind (Base_Type (Act_T)) = E_Access_Protected_Subprogram_Type then
11509 if Ekind (A_Gen_T) = E_Access_Subprogram_Type then
11510 Error_Msg_NE
11511 ("protected access type not allowed for formal &",
11512 Actual, Gen_T);
11513 end if;
11514
11515 elsif Ekind (A_Gen_T) = E_Access_Protected_Subprogram_Type then
11516 Error_Msg_NE
11517 ("expect protected access type for formal &",
11518 Actual, Gen_T);
11519 end if;
11520
11521 -- If the formal has a specified convention (which in most cases
11522 -- will be StdCall) verify that the actual has the same convention.
11523
11524 if Has_Convention_Pragma (A_Gen_T)
11525 and then Convention (A_Gen_T) /= Convention (Act_T)
11526 then
11527 Error_Msg_Name_1 := Get_Convention_Name (Convention (A_Gen_T));
11528 Error_Msg_NE
11529 ("actual for formal & must have convention %", Actual, Gen_T);
11530 end if;
11531 end Validate_Access_Subprogram_Instance;
11532
11533 -----------------------------------
11534 -- Validate_Access_Type_Instance --
11535 -----------------------------------
11536
11537 procedure Validate_Access_Type_Instance is
11538 Desig_Type : constant Entity_Id :=
11539 Find_Actual_Type (Designated_Type (A_Gen_T), A_Gen_T);
11540 Desig_Act : Entity_Id;
11541
11542 begin
11543 if not Is_Access_Type (Act_T) then
11544 Error_Msg_NE
11545 ("expect access type in instantiation of &", Actual, Gen_T);
11546 Abandon_Instantiation (Actual);
11547 end if;
11548
11549 if Is_Access_Constant (A_Gen_T) then
11550 if not Is_Access_Constant (Act_T) then
11551 Error_Msg_N
11552 ("actual type must be access-to-constant type", Actual);
11553 Abandon_Instantiation (Actual);
11554 end if;
11555 else
11556 if Is_Access_Constant (Act_T) then
11557 Error_Msg_N
11558 ("actual type must be access-to-variable type", Actual);
11559 Abandon_Instantiation (Actual);
11560
11561 elsif Ekind (A_Gen_T) = E_General_Access_Type
11562 and then Ekind (Base_Type (Act_T)) /= E_General_Access_Type
11563 then
11564 Error_Msg_N -- CODEFIX
11565 ("actual must be general access type!", Actual);
11566 Error_Msg_NE -- CODEFIX
11567 ("add ALL to }!", Actual, Act_T);
11568 Abandon_Instantiation (Actual);
11569 end if;
11570 end if;
11571
11572 -- The designated subtypes, that is to say the subtypes introduced
11573 -- by an access type declaration (and not by a subtype declaration)
11574 -- must match.
11575
11576 Desig_Act := Designated_Type (Base_Type (Act_T));
11577
11578 -- The designated type may have been introduced through a limited_
11579 -- with clause, in which case retrieve the non-limited view. This
11580 -- applies to incomplete types as well as to class-wide types.
11581
11582 if From_Limited_With (Desig_Act) then
11583 Desig_Act := Available_View (Desig_Act);
11584 end if;
11585
11586 if not Subtypes_Match (Desig_Type, Desig_Act) then
11587 Error_Msg_NE
11588 ("designated type of actual does not match that of formal &",
11589 Actual, Gen_T);
11590
11591 if not Predicates_Match (Desig_Type, Desig_Act) then
11592 Error_Msg_N ("\predicates do not match", Actual);
11593 end if;
11594
11595 Abandon_Instantiation (Actual);
11596
11597 elsif Is_Access_Type (Designated_Type (Act_T))
11598 and then Is_Constrained (Designated_Type (Designated_Type (Act_T)))
11599 /=
11600 Is_Constrained (Designated_Type (Desig_Type))
11601 then
11602 Error_Msg_NE
11603 ("designated type of actual does not match that of formal &",
11604 Actual, Gen_T);
11605
11606 if not Predicates_Match (Desig_Type, Desig_Act) then
11607 Error_Msg_N ("\predicates do not match", Actual);
11608 end if;
11609
11610 Abandon_Instantiation (Actual);
11611 end if;
11612
11613 -- Ada 2005: null-exclusion indicators of the two types must agree
11614
11615 if Can_Never_Be_Null (A_Gen_T) /= Can_Never_Be_Null (Act_T) then
11616 Error_Msg_NE
11617 ("non null exclusion of actual and formal & do not match",
11618 Actual, Gen_T);
11619 end if;
11620 end Validate_Access_Type_Instance;
11621
11622 ----------------------------------
11623 -- Validate_Array_Type_Instance --
11624 ----------------------------------
11625
11626 procedure Validate_Array_Type_Instance is
11627 I1 : Node_Id;
11628 I2 : Node_Id;
11629 T2 : Entity_Id;
11630
11631 function Formal_Dimensions return Nat;
11632 -- Count number of dimensions in array type formal
11633
11634 -----------------------
11635 -- Formal_Dimensions --
11636 -----------------------
11637
11638 function Formal_Dimensions return Nat is
11639 Num : Nat := 0;
11640 Index : Node_Id;
11641
11642 begin
11643 if Nkind (Def) = N_Constrained_Array_Definition then
11644 Index := First (Discrete_Subtype_Definitions (Def));
11645 else
11646 Index := First (Subtype_Marks (Def));
11647 end if;
11648
11649 while Present (Index) loop
11650 Num := Num + 1;
11651 Next_Index (Index);
11652 end loop;
11653
11654 return Num;
11655 end Formal_Dimensions;
11656
11657 -- Start of processing for Validate_Array_Type_Instance
11658
11659 begin
11660 if not Is_Array_Type (Act_T) then
11661 Error_Msg_NE
11662 ("expect array type in instantiation of &", Actual, Gen_T);
11663 Abandon_Instantiation (Actual);
11664
11665 elsif Nkind (Def) = N_Constrained_Array_Definition then
11666 if not (Is_Constrained (Act_T)) then
11667 Error_Msg_NE
11668 ("expect constrained array in instantiation of &",
11669 Actual, Gen_T);
11670 Abandon_Instantiation (Actual);
11671 end if;
11672
11673 else
11674 if Is_Constrained (Act_T) then
11675 Error_Msg_NE
11676 ("expect unconstrained array in instantiation of &",
11677 Actual, Gen_T);
11678 Abandon_Instantiation (Actual);
11679 end if;
11680 end if;
11681
11682 if Formal_Dimensions /= Number_Dimensions (Act_T) then
11683 Error_Msg_NE
11684 ("dimensions of actual do not match formal &", Actual, Gen_T);
11685 Abandon_Instantiation (Actual);
11686 end if;
11687
11688 I1 := First_Index (A_Gen_T);
11689 I2 := First_Index (Act_T);
11690 for J in 1 .. Formal_Dimensions loop
11691
11692 -- If the indexes of the actual were given by a subtype_mark,
11693 -- the index was transformed into a range attribute. Retrieve
11694 -- the original type mark for checking.
11695
11696 if Is_Entity_Name (Original_Node (I2)) then
11697 T2 := Entity (Original_Node (I2));
11698 else
11699 T2 := Etype (I2);
11700 end if;
11701
11702 if not Subtypes_Match
11703 (Find_Actual_Type (Etype (I1), A_Gen_T), T2)
11704 then
11705 Error_Msg_NE
11706 ("index types of actual do not match those of formal &",
11707 Actual, Gen_T);
11708 Abandon_Instantiation (Actual);
11709 end if;
11710
11711 Next_Index (I1);
11712 Next_Index (I2);
11713 end loop;
11714
11715 -- Check matching subtypes. Note that there are complex visibility
11716 -- issues when the generic is a child unit and some aspect of the
11717 -- generic type is declared in a parent unit of the generic. We do
11718 -- the test to handle this special case only after a direct check
11719 -- for static matching has failed. The case where both the component
11720 -- type and the array type are separate formals, and the component
11721 -- type is a private view may also require special checking in
11722 -- Subtypes_Match.
11723
11724 if Subtypes_Match
11725 (Component_Type (A_Gen_T), Component_Type (Act_T))
11726 or else
11727 Subtypes_Match
11728 (Find_Actual_Type (Component_Type (A_Gen_T), A_Gen_T),
11729 Component_Type (Act_T))
11730 then
11731 null;
11732 else
11733 Error_Msg_NE
11734 ("component subtype of actual does not match that of formal &",
11735 Actual, Gen_T);
11736 Abandon_Instantiation (Actual);
11737 end if;
11738
11739 if Has_Aliased_Components (A_Gen_T)
11740 and then not Has_Aliased_Components (Act_T)
11741 then
11742 Error_Msg_NE
11743 ("actual must have aliased components to match formal type &",
11744 Actual, Gen_T);
11745 end if;
11746 end Validate_Array_Type_Instance;
11747
11748 -----------------------------------------------
11749 -- Validate_Derived_Interface_Type_Instance --
11750 -----------------------------------------------
11751
11752 procedure Validate_Derived_Interface_Type_Instance is
11753 Par : constant Entity_Id := Entity (Subtype_Indication (Def));
11754 Elmt : Elmt_Id;
11755
11756 begin
11757 -- First apply interface instance checks
11758
11759 Validate_Interface_Type_Instance;
11760
11761 -- Verify that immediate parent interface is an ancestor of
11762 -- the actual.
11763
11764 if Present (Par)
11765 and then not Interface_Present_In_Ancestor (Act_T, Par)
11766 then
11767 Error_Msg_NE
11768 ("interface actual must include progenitor&", Actual, Par);
11769 end if;
11770
11771 -- Now verify that the actual includes all other ancestors of
11772 -- the formal.
11773
11774 Elmt := First_Elmt (Interfaces (A_Gen_T));
11775 while Present (Elmt) loop
11776 if not Interface_Present_In_Ancestor
11777 (Act_T, Get_Instance_Of (Node (Elmt)))
11778 then
11779 Error_Msg_NE
11780 ("interface actual must include progenitor&",
11781 Actual, Node (Elmt));
11782 end if;
11783
11784 Next_Elmt (Elmt);
11785 end loop;
11786 end Validate_Derived_Interface_Type_Instance;
11787
11788 ------------------------------------
11789 -- Validate_Derived_Type_Instance --
11790 ------------------------------------
11791
11792 procedure Validate_Derived_Type_Instance is
11793 Actual_Discr : Entity_Id;
11794 Ancestor_Discr : Entity_Id;
11795
11796 begin
11797 -- If the parent type in the generic declaration is itself a previous
11798 -- formal type, then it is local to the generic and absent from the
11799 -- analyzed generic definition. In that case the ancestor is the
11800 -- instance of the formal (which must have been instantiated
11801 -- previously), unless the ancestor is itself a formal derived type.
11802 -- In this latter case (which is the subject of Corrigendum 8652/0038
11803 -- (AI-202) the ancestor of the formals is the ancestor of its
11804 -- parent. Otherwise, the analyzed generic carries the parent type.
11805 -- If the parent type is defined in a previous formal package, then
11806 -- the scope of that formal package is that of the generic type
11807 -- itself, and it has already been mapped into the corresponding type
11808 -- in the actual package.
11809
11810 -- Common case: parent type defined outside of the generic
11811
11812 if Is_Entity_Name (Subtype_Mark (Def))
11813 and then Present (Entity (Subtype_Mark (Def)))
11814 then
11815 Ancestor := Get_Instance_Of (Entity (Subtype_Mark (Def)));
11816
11817 -- Check whether parent is defined in a previous formal package
11818
11819 elsif
11820 Scope (Scope (Base_Type (Etype (A_Gen_T)))) = Scope (A_Gen_T)
11821 then
11822 Ancestor :=
11823 Get_Instance_Of (Base_Type (Etype (A_Gen_T)));
11824
11825 -- The type may be a local derivation, or a type extension of a
11826 -- previous formal, or of a formal of a parent package.
11827
11828 elsif Is_Derived_Type (Get_Instance_Of (A_Gen_T))
11829 or else
11830 Ekind (Get_Instance_Of (A_Gen_T)) = E_Record_Type_With_Private
11831 then
11832 -- Check whether the parent is another derived formal type in the
11833 -- same generic unit.
11834
11835 if Etype (A_Gen_T) /= A_Gen_T
11836 and then Is_Generic_Type (Etype (A_Gen_T))
11837 and then Scope (Etype (A_Gen_T)) = Scope (A_Gen_T)
11838 and then Etype (Etype (A_Gen_T)) /= Etype (A_Gen_T)
11839 then
11840 -- Locate ancestor of parent from the subtype declaration
11841 -- created for the actual.
11842
11843 declare
11844 Decl : Node_Id;
11845
11846 begin
11847 Decl := First (Actual_Decls);
11848 while Present (Decl) loop
11849 if Nkind (Decl) = N_Subtype_Declaration
11850 and then Chars (Defining_Identifier (Decl)) =
11851 Chars (Etype (A_Gen_T))
11852 then
11853 Ancestor := Generic_Parent_Type (Decl);
11854 exit;
11855 else
11856 Next (Decl);
11857 end if;
11858 end loop;
11859 end;
11860
11861 pragma Assert (Present (Ancestor));
11862
11863 -- The ancestor itself may be a previous formal that has been
11864 -- instantiated.
11865
11866 Ancestor := Get_Instance_Of (Ancestor);
11867
11868 else
11869 Ancestor :=
11870 Get_Instance_Of (Base_Type (Get_Instance_Of (A_Gen_T)));
11871 end if;
11872
11873 -- Check whether parent is a previous formal of the current generic
11874
11875 elsif Is_Derived_Type (A_Gen_T)
11876 and then Is_Generic_Type (Etype (A_Gen_T))
11877 and then Scope (A_Gen_T) = Scope (Etype (A_Gen_T))
11878 then
11879 Ancestor := Get_Instance_Of (First_Subtype (Etype (A_Gen_T)));
11880
11881 -- An unusual case: the actual is a type declared in a parent unit,
11882 -- but is not a formal type so there is no instance_of for it.
11883 -- Retrieve it by analyzing the record extension.
11884
11885 elsif Is_Child_Unit (Scope (A_Gen_T))
11886 and then In_Open_Scopes (Scope (Act_T))
11887 and then Is_Generic_Instance (Scope (Act_T))
11888 then
11889 Analyze (Subtype_Mark (Def));
11890 Ancestor := Entity (Subtype_Mark (Def));
11891
11892 else
11893 Ancestor := Get_Instance_Of (Etype (Base_Type (A_Gen_T)));
11894 end if;
11895
11896 -- If the formal derived type has pragma Preelaborable_Initialization
11897 -- then the actual type must have preelaborable initialization.
11898
11899 if Known_To_Have_Preelab_Init (A_Gen_T)
11900 and then not Has_Preelaborable_Initialization (Act_T)
11901 then
11902 Error_Msg_NE
11903 ("actual for & must have preelaborable initialization",
11904 Actual, Gen_T);
11905 end if;
11906
11907 -- Ada 2005 (AI-251)
11908
11909 if Ada_Version >= Ada_2005 and then Is_Interface (Ancestor) then
11910 if not Interface_Present_In_Ancestor (Act_T, Ancestor) then
11911 Error_Msg_NE
11912 ("(Ada 2005) expected type implementing & in instantiation",
11913 Actual, Ancestor);
11914 end if;
11915
11916 -- Finally verify that the (instance of) the ancestor is an ancestor
11917 -- of the actual.
11918
11919 elsif not Is_Ancestor (Base_Type (Ancestor), Act_T) then
11920 Error_Msg_NE
11921 ("expect type derived from & in instantiation",
11922 Actual, First_Subtype (Ancestor));
11923 Abandon_Instantiation (Actual);
11924 end if;
11925
11926 -- Ada 2005 (AI-443): Synchronized formal derived type checks. Note
11927 -- that the formal type declaration has been rewritten as a private
11928 -- extension.
11929
11930 if Ada_Version >= Ada_2005
11931 and then Nkind (Parent (A_Gen_T)) = N_Private_Extension_Declaration
11932 and then Synchronized_Present (Parent (A_Gen_T))
11933 then
11934 -- The actual must be a synchronized tagged type
11935
11936 if not Is_Tagged_Type (Act_T) then
11937 Error_Msg_N
11938 ("actual of synchronized type must be tagged", Actual);
11939 Abandon_Instantiation (Actual);
11940
11941 elsif Nkind (Parent (Act_T)) = N_Full_Type_Declaration
11942 and then Nkind (Type_Definition (Parent (Act_T))) =
11943 N_Derived_Type_Definition
11944 and then not Synchronized_Present
11945 (Type_Definition (Parent (Act_T)))
11946 then
11947 Error_Msg_N
11948 ("actual of synchronized type must be synchronized", Actual);
11949 Abandon_Instantiation (Actual);
11950 end if;
11951 end if;
11952
11953 -- Perform atomic/volatile checks (RM C.6(12)). Note that AI05-0218-1
11954 -- removes the second instance of the phrase "or allow pass by copy".
11955
11956 if Is_Atomic (Act_T) and then not Is_Atomic (Ancestor) then
11957 Error_Msg_N
11958 ("cannot have atomic actual type for non-atomic formal type",
11959 Actual);
11960
11961 elsif Is_Volatile (Act_T) and then not Is_Volatile (Ancestor) then
11962 Error_Msg_N
11963 ("cannot have volatile actual type for non-volatile formal type",
11964 Actual);
11965 end if;
11966
11967 -- It should not be necessary to check for unknown discriminants on
11968 -- Formal, but for some reason Has_Unknown_Discriminants is false for
11969 -- A_Gen_T, so Is_Definite_Subtype incorrectly returns True. This
11970 -- needs fixing. ???
11971
11972 if Is_Definite_Subtype (A_Gen_T)
11973 and then not Unknown_Discriminants_Present (Formal)
11974 and then not Is_Definite_Subtype (Act_T)
11975 then
11976 Error_Msg_N ("actual subtype must be constrained", Actual);
11977 Abandon_Instantiation (Actual);
11978 end if;
11979
11980 if not Unknown_Discriminants_Present (Formal) then
11981 if Is_Constrained (Ancestor) then
11982 if not Is_Constrained (Act_T) then
11983 Error_Msg_N ("actual subtype must be constrained", Actual);
11984 Abandon_Instantiation (Actual);
11985 end if;
11986
11987 -- Ancestor is unconstrained, Check if generic formal and actual
11988 -- agree on constrainedness. The check only applies to array types
11989 -- and discriminated types.
11990
11991 elsif Is_Constrained (Act_T) then
11992 if Ekind (Ancestor) = E_Access_Type
11993 or else (not Is_Constrained (A_Gen_T)
11994 and then Is_Composite_Type (A_Gen_T))
11995 then
11996 Error_Msg_N ("actual subtype must be unconstrained", Actual);
11997 Abandon_Instantiation (Actual);
11998 end if;
11999
12000 -- A class-wide type is only allowed if the formal has unknown
12001 -- discriminants.
12002
12003 elsif Is_Class_Wide_Type (Act_T)
12004 and then not Has_Unknown_Discriminants (Ancestor)
12005 then
12006 Error_Msg_NE
12007 ("actual for & cannot be a class-wide type", Actual, Gen_T);
12008 Abandon_Instantiation (Actual);
12009
12010 -- Otherwise, the formal and actual must have the same number
12011 -- of discriminants and each discriminant of the actual must
12012 -- correspond to a discriminant of the formal.
12013
12014 elsif Has_Discriminants (Act_T)
12015 and then not Has_Unknown_Discriminants (Act_T)
12016 and then Has_Discriminants (Ancestor)
12017 then
12018 Actual_Discr := First_Discriminant (Act_T);
12019 Ancestor_Discr := First_Discriminant (Ancestor);
12020 while Present (Actual_Discr)
12021 and then Present (Ancestor_Discr)
12022 loop
12023 if Base_Type (Act_T) /= Base_Type (Ancestor) and then
12024 No (Corresponding_Discriminant (Actual_Discr))
12025 then
12026 Error_Msg_NE
12027 ("discriminant & does not correspond "
12028 & "to ancestor discriminant", Actual, Actual_Discr);
12029 Abandon_Instantiation (Actual);
12030 end if;
12031
12032 Next_Discriminant (Actual_Discr);
12033 Next_Discriminant (Ancestor_Discr);
12034 end loop;
12035
12036 if Present (Actual_Discr) or else Present (Ancestor_Discr) then
12037 Error_Msg_NE
12038 ("actual for & must have same number of discriminants",
12039 Actual, Gen_T);
12040 Abandon_Instantiation (Actual);
12041 end if;
12042
12043 -- This case should be caught by the earlier check for
12044 -- constrainedness, but the check here is added for completeness.
12045
12046 elsif Has_Discriminants (Act_T)
12047 and then not Has_Unknown_Discriminants (Act_T)
12048 then
12049 Error_Msg_NE
12050 ("actual for & must not have discriminants", Actual, Gen_T);
12051 Abandon_Instantiation (Actual);
12052
12053 elsif Has_Discriminants (Ancestor) then
12054 Error_Msg_NE
12055 ("actual for & must have known discriminants", Actual, Gen_T);
12056 Abandon_Instantiation (Actual);
12057 end if;
12058
12059 if not Subtypes_Statically_Compatible
12060 (Act_T, Ancestor, Formal_Derived_Matching => True)
12061 then
12062 Error_Msg_N
12063 ("constraint on actual is incompatible with formal", Actual);
12064 Abandon_Instantiation (Actual);
12065 end if;
12066 end if;
12067
12068 -- If the formal and actual types are abstract, check that there
12069 -- are no abstract primitives of the actual type that correspond to
12070 -- nonabstract primitives of the formal type (second sentence of
12071 -- RM95 3.9.3(9)).
12072
12073 if Is_Abstract_Type (A_Gen_T) and then Is_Abstract_Type (Act_T) then
12074 Check_Abstract_Primitives : declare
12075 Gen_Prims : constant Elist_Id :=
12076 Primitive_Operations (A_Gen_T);
12077 Gen_Elmt : Elmt_Id;
12078 Gen_Subp : Entity_Id;
12079 Anc_Subp : Entity_Id;
12080 Anc_Formal : Entity_Id;
12081 Anc_F_Type : Entity_Id;
12082
12083 Act_Prims : constant Elist_Id := Primitive_Operations (Act_T);
12084 Act_Elmt : Elmt_Id;
12085 Act_Subp : Entity_Id;
12086 Act_Formal : Entity_Id;
12087 Act_F_Type : Entity_Id;
12088
12089 Subprograms_Correspond : Boolean;
12090
12091 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean;
12092 -- Returns true if T2 is derived directly or indirectly from
12093 -- T1, including derivations from interfaces. T1 and T2 are
12094 -- required to be specific tagged base types.
12095
12096 ------------------------
12097 -- Is_Tagged_Ancestor --
12098 ------------------------
12099
12100 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean
12101 is
12102 Intfc_Elmt : Elmt_Id;
12103
12104 begin
12105 -- The predicate is satisfied if the types are the same
12106
12107 if T1 = T2 then
12108 return True;
12109
12110 -- If we've reached the top of the derivation chain then
12111 -- we know that T1 is not an ancestor of T2.
12112
12113 elsif Etype (T2) = T2 then
12114 return False;
12115
12116 -- Proceed to check T2's immediate parent
12117
12118 elsif Is_Ancestor (T1, Base_Type (Etype (T2))) then
12119 return True;
12120
12121 -- Finally, check to see if T1 is an ancestor of any of T2's
12122 -- progenitors.
12123
12124 else
12125 Intfc_Elmt := First_Elmt (Interfaces (T2));
12126 while Present (Intfc_Elmt) loop
12127 if Is_Ancestor (T1, Node (Intfc_Elmt)) then
12128 return True;
12129 end if;
12130
12131 Next_Elmt (Intfc_Elmt);
12132 end loop;
12133 end if;
12134
12135 return False;
12136 end Is_Tagged_Ancestor;
12137
12138 -- Start of processing for Check_Abstract_Primitives
12139
12140 begin
12141 -- Loop over all of the formal derived type's primitives
12142
12143 Gen_Elmt := First_Elmt (Gen_Prims);
12144 while Present (Gen_Elmt) loop
12145 Gen_Subp := Node (Gen_Elmt);
12146
12147 -- If the primitive of the formal is not abstract, then
12148 -- determine whether there is a corresponding primitive of
12149 -- the actual type that's abstract.
12150
12151 if not Is_Abstract_Subprogram (Gen_Subp) then
12152 Act_Elmt := First_Elmt (Act_Prims);
12153 while Present (Act_Elmt) loop
12154 Act_Subp := Node (Act_Elmt);
12155
12156 -- If we find an abstract primitive of the actual,
12157 -- then we need to test whether it corresponds to the
12158 -- subprogram from which the generic formal primitive
12159 -- is inherited.
12160
12161 if Is_Abstract_Subprogram (Act_Subp) then
12162 Anc_Subp := Alias (Gen_Subp);
12163
12164 -- Test whether we have a corresponding primitive
12165 -- by comparing names, kinds, formal types, and
12166 -- result types.
12167
12168 if Chars (Anc_Subp) = Chars (Act_Subp)
12169 and then Ekind (Anc_Subp) = Ekind (Act_Subp)
12170 then
12171 Anc_Formal := First_Formal (Anc_Subp);
12172 Act_Formal := First_Formal (Act_Subp);
12173 while Present (Anc_Formal)
12174 and then Present (Act_Formal)
12175 loop
12176 Anc_F_Type := Etype (Anc_Formal);
12177 Act_F_Type := Etype (Act_Formal);
12178
12179 if Ekind (Anc_F_Type) =
12180 E_Anonymous_Access_Type
12181 then
12182 Anc_F_Type := Designated_Type (Anc_F_Type);
12183
12184 if Ekind (Act_F_Type) =
12185 E_Anonymous_Access_Type
12186 then
12187 Act_F_Type :=
12188 Designated_Type (Act_F_Type);
12189 else
12190 exit;
12191 end if;
12192
12193 elsif
12194 Ekind (Act_F_Type) = E_Anonymous_Access_Type
12195 then
12196 exit;
12197 end if;
12198
12199 Anc_F_Type := Base_Type (Anc_F_Type);
12200 Act_F_Type := Base_Type (Act_F_Type);
12201
12202 -- If the formal is controlling, then the
12203 -- the type of the actual primitive's formal
12204 -- must be derived directly or indirectly
12205 -- from the type of the ancestor primitive's
12206 -- formal.
12207
12208 if Is_Controlling_Formal (Anc_Formal) then
12209 if not Is_Tagged_Ancestor
12210 (Anc_F_Type, Act_F_Type)
12211 then
12212 exit;
12213 end if;
12214
12215 -- Otherwise the types of the formals must
12216 -- be the same.
12217
12218 elsif Anc_F_Type /= Act_F_Type then
12219 exit;
12220 end if;
12221
12222 Next_Entity (Anc_Formal);
12223 Next_Entity (Act_Formal);
12224 end loop;
12225
12226 -- If we traversed through all of the formals
12227 -- then so far the subprograms correspond, so
12228 -- now check that any result types correspond.
12229
12230 if No (Anc_Formal) and then No (Act_Formal) then
12231 Subprograms_Correspond := True;
12232
12233 if Ekind (Act_Subp) = E_Function then
12234 Anc_F_Type := Etype (Anc_Subp);
12235 Act_F_Type := Etype (Act_Subp);
12236
12237 if Ekind (Anc_F_Type) =
12238 E_Anonymous_Access_Type
12239 then
12240 Anc_F_Type :=
12241 Designated_Type (Anc_F_Type);
12242
12243 if Ekind (Act_F_Type) =
12244 E_Anonymous_Access_Type
12245 then
12246 Act_F_Type :=
12247 Designated_Type (Act_F_Type);
12248 else
12249 Subprograms_Correspond := False;
12250 end if;
12251
12252 elsif
12253 Ekind (Act_F_Type)
12254 = E_Anonymous_Access_Type
12255 then
12256 Subprograms_Correspond := False;
12257 end if;
12258
12259 Anc_F_Type := Base_Type (Anc_F_Type);
12260 Act_F_Type := Base_Type (Act_F_Type);
12261
12262 -- Now either the result types must be
12263 -- the same or, if the result type is
12264 -- controlling, the result type of the
12265 -- actual primitive must descend from the
12266 -- result type of the ancestor primitive.
12267
12268 if Subprograms_Correspond
12269 and then Anc_F_Type /= Act_F_Type
12270 and then
12271 Has_Controlling_Result (Anc_Subp)
12272 and then not Is_Tagged_Ancestor
12273 (Anc_F_Type, Act_F_Type)
12274 then
12275 Subprograms_Correspond := False;
12276 end if;
12277 end if;
12278
12279 -- Found a matching subprogram belonging to
12280 -- formal ancestor type, so actual subprogram
12281 -- corresponds and this violates 3.9.3(9).
12282
12283 if Subprograms_Correspond then
12284 Error_Msg_NE
12285 ("abstract subprogram & overrides "
12286 & "nonabstract subprogram of ancestor",
12287 Actual, Act_Subp);
12288 end if;
12289 end if;
12290 end if;
12291 end if;
12292
12293 Next_Elmt (Act_Elmt);
12294 end loop;
12295 end if;
12296
12297 Next_Elmt (Gen_Elmt);
12298 end loop;
12299 end Check_Abstract_Primitives;
12300 end if;
12301
12302 -- Verify that limitedness matches. If parent is a limited
12303 -- interface then the generic formal is not unless declared
12304 -- explicitly so. If not declared limited, the actual cannot be
12305 -- limited (see AI05-0087).
12306
12307 -- Even though this AI is a binding interpretation, we enable the
12308 -- check only in Ada 2012 mode, because this improper construct
12309 -- shows up in user code and in existing B-tests.
12310
12311 if Is_Limited_Type (Act_T)
12312 and then not Is_Limited_Type (A_Gen_T)
12313 and then Ada_Version >= Ada_2012
12314 then
12315 if In_Instance then
12316 null;
12317 else
12318 Error_Msg_NE
12319 ("actual for non-limited & cannot be a limited type",
12320 Actual, Gen_T);
12321 Explain_Limited_Type (Act_T, Actual);
12322 Abandon_Instantiation (Actual);
12323 end if;
12324 end if;
12325 end Validate_Derived_Type_Instance;
12326
12327 ----------------------------------------
12328 -- Validate_Discriminated_Formal_Type --
12329 ----------------------------------------
12330
12331 procedure Validate_Discriminated_Formal_Type is
12332 Formal_Discr : Entity_Id;
12333 Actual_Discr : Entity_Id;
12334 Formal_Subt : Entity_Id;
12335
12336 begin
12337 if Has_Discriminants (A_Gen_T) then
12338 if not Has_Discriminants (Act_T) then
12339 Error_Msg_NE
12340 ("actual for & must have discriminants", Actual, Gen_T);
12341 Abandon_Instantiation (Actual);
12342
12343 elsif Is_Constrained (Act_T) then
12344 Error_Msg_NE
12345 ("actual for & must be unconstrained", Actual, Gen_T);
12346 Abandon_Instantiation (Actual);
12347
12348 else
12349 Formal_Discr := First_Discriminant (A_Gen_T);
12350 Actual_Discr := First_Discriminant (Act_T);
12351 while Formal_Discr /= Empty loop
12352 if Actual_Discr = Empty then
12353 Error_Msg_NE
12354 ("discriminants on actual do not match formal",
12355 Actual, Gen_T);
12356 Abandon_Instantiation (Actual);
12357 end if;
12358
12359 Formal_Subt := Get_Instance_Of (Etype (Formal_Discr));
12360
12361 -- Access discriminants match if designated types do
12362
12363 if Ekind (Base_Type (Formal_Subt)) = E_Anonymous_Access_Type
12364 and then (Ekind (Base_Type (Etype (Actual_Discr)))) =
12365 E_Anonymous_Access_Type
12366 and then
12367 Get_Instance_Of
12368 (Designated_Type (Base_Type (Formal_Subt))) =
12369 Designated_Type (Base_Type (Etype (Actual_Discr)))
12370 then
12371 null;
12372
12373 elsif Base_Type (Formal_Subt) /=
12374 Base_Type (Etype (Actual_Discr))
12375 then
12376 Error_Msg_NE
12377 ("types of actual discriminants must match formal",
12378 Actual, Gen_T);
12379 Abandon_Instantiation (Actual);
12380
12381 elsif not Subtypes_Statically_Match
12382 (Formal_Subt, Etype (Actual_Discr))
12383 and then Ada_Version >= Ada_95
12384 then
12385 Error_Msg_NE
12386 ("subtypes of actual discriminants must match formal",
12387 Actual, Gen_T);
12388 Abandon_Instantiation (Actual);
12389 end if;
12390
12391 Next_Discriminant (Formal_Discr);
12392 Next_Discriminant (Actual_Discr);
12393 end loop;
12394
12395 if Actual_Discr /= Empty then
12396 Error_Msg_NE
12397 ("discriminants on actual do not match formal",
12398 Actual, Gen_T);
12399 Abandon_Instantiation (Actual);
12400 end if;
12401 end if;
12402 end if;
12403 end Validate_Discriminated_Formal_Type;
12404
12405 ---------------------------------------
12406 -- Validate_Incomplete_Type_Instance --
12407 ---------------------------------------
12408
12409 procedure Validate_Incomplete_Type_Instance is
12410 begin
12411 if not Is_Tagged_Type (Act_T)
12412 and then Is_Tagged_Type (A_Gen_T)
12413 then
12414 Error_Msg_NE
12415 ("actual for & must be a tagged type", Actual, Gen_T);
12416 end if;
12417
12418 Validate_Discriminated_Formal_Type;
12419 end Validate_Incomplete_Type_Instance;
12420
12421 --------------------------------------
12422 -- Validate_Interface_Type_Instance --
12423 --------------------------------------
12424
12425 procedure Validate_Interface_Type_Instance is
12426 begin
12427 if not Is_Interface (Act_T) then
12428 Error_Msg_NE
12429 ("actual for formal interface type must be an interface",
12430 Actual, Gen_T);
12431
12432 elsif Is_Limited_Type (Act_T) /= Is_Limited_Type (A_Gen_T)
12433 or else Is_Task_Interface (A_Gen_T) /= Is_Task_Interface (Act_T)
12434 or else Is_Protected_Interface (A_Gen_T) /=
12435 Is_Protected_Interface (Act_T)
12436 or else Is_Synchronized_Interface (A_Gen_T) /=
12437 Is_Synchronized_Interface (Act_T)
12438 then
12439 Error_Msg_NE
12440 ("actual for interface& does not match (RM 12.5.5(4))",
12441 Actual, Gen_T);
12442 end if;
12443 end Validate_Interface_Type_Instance;
12444
12445 ------------------------------------
12446 -- Validate_Private_Type_Instance --
12447 ------------------------------------
12448
12449 procedure Validate_Private_Type_Instance is
12450 begin
12451 if Is_Limited_Type (Act_T)
12452 and then not Is_Limited_Type (A_Gen_T)
12453 then
12454 if In_Instance then
12455 null;
12456 else
12457 Error_Msg_NE
12458 ("actual for non-limited & cannot be a limited type", Actual,
12459 Gen_T);
12460 Explain_Limited_Type (Act_T, Actual);
12461 Abandon_Instantiation (Actual);
12462 end if;
12463
12464 elsif Known_To_Have_Preelab_Init (A_Gen_T)
12465 and then not Has_Preelaborable_Initialization (Act_T)
12466 then
12467 Error_Msg_NE
12468 ("actual for & must have preelaborable initialization", Actual,
12469 Gen_T);
12470
12471 elsif not Is_Definite_Subtype (Act_T)
12472 and then Is_Definite_Subtype (A_Gen_T)
12473 and then Ada_Version >= Ada_95
12474 then
12475 Error_Msg_NE
12476 ("actual for & must be a definite subtype", Actual, Gen_T);
12477
12478 elsif not Is_Tagged_Type (Act_T)
12479 and then Is_Tagged_Type (A_Gen_T)
12480 then
12481 Error_Msg_NE
12482 ("actual for & must be a tagged type", Actual, Gen_T);
12483 end if;
12484
12485 Validate_Discriminated_Formal_Type;
12486 Ancestor := Gen_T;
12487 end Validate_Private_Type_Instance;
12488
12489 -- Start of processing for Instantiate_Type
12490
12491 begin
12492 if Get_Instance_Of (A_Gen_T) /= A_Gen_T then
12493 Error_Msg_N ("duplicate instantiation of generic type", Actual);
12494 return New_List (Error);
12495
12496 elsif not Is_Entity_Name (Actual)
12497 or else not Is_Type (Entity (Actual))
12498 then
12499 Error_Msg_NE
12500 ("expect valid subtype mark to instantiate &", Actual, Gen_T);
12501 Abandon_Instantiation (Actual);
12502
12503 else
12504 Act_T := Entity (Actual);
12505
12506 -- Ada 2005 (AI-216): An Unchecked_Union subtype shall only be passed
12507 -- as a generic actual parameter if the corresponding formal type
12508 -- does not have a known_discriminant_part, or is a formal derived
12509 -- type that is an Unchecked_Union type.
12510
12511 if Is_Unchecked_Union (Base_Type (Act_T)) then
12512 if not Has_Discriminants (A_Gen_T)
12513 or else (Is_Derived_Type (A_Gen_T)
12514 and then Is_Unchecked_Union (A_Gen_T))
12515 then
12516 null;
12517 else
12518 Error_Msg_N ("unchecked union cannot be the actual for a "
12519 & "discriminated formal type", Act_T);
12520
12521 end if;
12522 end if;
12523
12524 -- Deal with fixed/floating restrictions
12525
12526 if Is_Floating_Point_Type (Act_T) then
12527 Check_Restriction (No_Floating_Point, Actual);
12528 elsif Is_Fixed_Point_Type (Act_T) then
12529 Check_Restriction (No_Fixed_Point, Actual);
12530 end if;
12531
12532 -- Deal with error of using incomplete type as generic actual.
12533 -- This includes limited views of a type, even if the non-limited
12534 -- view may be available.
12535
12536 if Ekind (Act_T) = E_Incomplete_Type
12537 or else (Is_Class_Wide_Type (Act_T)
12538 and then Ekind (Root_Type (Act_T)) = E_Incomplete_Type)
12539 then
12540 -- If the formal is an incomplete type, the actual can be
12541 -- incomplete as well.
12542
12543 if Ekind (A_Gen_T) = E_Incomplete_Type then
12544 null;
12545
12546 elsif Is_Class_Wide_Type (Act_T)
12547 or else No (Full_View (Act_T))
12548 then
12549 Error_Msg_N ("premature use of incomplete type", Actual);
12550 Abandon_Instantiation (Actual);
12551 else
12552 Act_T := Full_View (Act_T);
12553 Set_Entity (Actual, Act_T);
12554
12555 if Has_Private_Component (Act_T) then
12556 Error_Msg_N
12557 ("premature use of type with private component", Actual);
12558 end if;
12559 end if;
12560
12561 -- Deal with error of premature use of private type as generic actual
12562
12563 elsif Is_Private_Type (Act_T)
12564 and then Is_Private_Type (Base_Type (Act_T))
12565 and then not Is_Generic_Type (Act_T)
12566 and then not Is_Derived_Type (Act_T)
12567 and then No (Full_View (Root_Type (Act_T)))
12568 then
12569 -- If the formal is an incomplete type, the actual can be
12570 -- private or incomplete as well.
12571
12572 if Ekind (A_Gen_T) = E_Incomplete_Type then
12573 null;
12574 else
12575 Error_Msg_N ("premature use of private type", Actual);
12576 end if;
12577
12578 elsif Has_Private_Component (Act_T) then
12579 Error_Msg_N
12580 ("premature use of type with private component", Actual);
12581 end if;
12582
12583 Set_Instance_Of (A_Gen_T, Act_T);
12584
12585 -- If the type is generic, the class-wide type may also be used
12586
12587 if Is_Tagged_Type (A_Gen_T)
12588 and then Is_Tagged_Type (Act_T)
12589 and then not Is_Class_Wide_Type (A_Gen_T)
12590 then
12591 Set_Instance_Of (Class_Wide_Type (A_Gen_T),
12592 Class_Wide_Type (Act_T));
12593 end if;
12594
12595 if not Is_Abstract_Type (A_Gen_T)
12596 and then Is_Abstract_Type (Act_T)
12597 then
12598 Error_Msg_N
12599 ("actual of non-abstract formal cannot be abstract", Actual);
12600 end if;
12601
12602 -- A generic scalar type is a first subtype for which we generate
12603 -- an anonymous base type. Indicate that the instance of this base
12604 -- is the base type of the actual.
12605
12606 if Is_Scalar_Type (A_Gen_T) then
12607 Set_Instance_Of (Etype (A_Gen_T), Etype (Act_T));
12608 end if;
12609 end if;
12610
12611 if Error_Posted (Act_T) then
12612 null;
12613 else
12614 case Nkind (Def) is
12615 when N_Formal_Private_Type_Definition =>
12616 Validate_Private_Type_Instance;
12617
12618 when N_Formal_Incomplete_Type_Definition =>
12619 Validate_Incomplete_Type_Instance;
12620
12621 when N_Formal_Derived_Type_Definition =>
12622 Validate_Derived_Type_Instance;
12623
12624 when N_Formal_Discrete_Type_Definition =>
12625 if not Is_Discrete_Type (Act_T) then
12626 Error_Msg_NE
12627 ("expect discrete type in instantiation of&",
12628 Actual, Gen_T);
12629 Abandon_Instantiation (Actual);
12630 end if;
12631
12632 Diagnose_Predicated_Actual;
12633
12634 when N_Formal_Signed_Integer_Type_Definition =>
12635 if not Is_Signed_Integer_Type (Act_T) then
12636 Error_Msg_NE
12637 ("expect signed integer type in instantiation of&",
12638 Actual, Gen_T);
12639 Abandon_Instantiation (Actual);
12640 end if;
12641
12642 Diagnose_Predicated_Actual;
12643
12644 when N_Formal_Modular_Type_Definition =>
12645 if not Is_Modular_Integer_Type (Act_T) then
12646 Error_Msg_NE
12647 ("expect modular type in instantiation of &",
12648 Actual, Gen_T);
12649 Abandon_Instantiation (Actual);
12650 end if;
12651
12652 Diagnose_Predicated_Actual;
12653
12654 when N_Formal_Floating_Point_Definition =>
12655 if not Is_Floating_Point_Type (Act_T) then
12656 Error_Msg_NE
12657 ("expect float type in instantiation of &", Actual, Gen_T);
12658 Abandon_Instantiation (Actual);
12659 end if;
12660
12661 when N_Formal_Ordinary_Fixed_Point_Definition =>
12662 if not Is_Ordinary_Fixed_Point_Type (Act_T) then
12663 Error_Msg_NE
12664 ("expect ordinary fixed point type in instantiation of &",
12665 Actual, Gen_T);
12666 Abandon_Instantiation (Actual);
12667 end if;
12668
12669 when N_Formal_Decimal_Fixed_Point_Definition =>
12670 if not Is_Decimal_Fixed_Point_Type (Act_T) then
12671 Error_Msg_NE
12672 ("expect decimal type in instantiation of &",
12673 Actual, Gen_T);
12674 Abandon_Instantiation (Actual);
12675 end if;
12676
12677 when N_Array_Type_Definition =>
12678 Validate_Array_Type_Instance;
12679
12680 when N_Access_To_Object_Definition =>
12681 Validate_Access_Type_Instance;
12682
12683 when N_Access_Function_Definition |
12684 N_Access_Procedure_Definition =>
12685 Validate_Access_Subprogram_Instance;
12686
12687 when N_Record_Definition =>
12688 Validate_Interface_Type_Instance;
12689
12690 when N_Derived_Type_Definition =>
12691 Validate_Derived_Interface_Type_Instance;
12692
12693 when others =>
12694 raise Program_Error;
12695
12696 end case;
12697 end if;
12698
12699 Subt := New_Copy (Gen_T);
12700
12701 -- Use adjusted sloc of subtype name as the location for other nodes in
12702 -- the subtype declaration.
12703
12704 Loc := Sloc (Subt);
12705
12706 Decl_Node :=
12707 Make_Subtype_Declaration (Loc,
12708 Defining_Identifier => Subt,
12709 Subtype_Indication => New_Occurrence_Of (Act_T, Loc));
12710
12711 if Is_Private_Type (Act_T) then
12712 Set_Has_Private_View (Subtype_Indication (Decl_Node));
12713
12714 elsif Is_Access_Type (Act_T)
12715 and then Is_Private_Type (Designated_Type (Act_T))
12716 then
12717 Set_Has_Private_View (Subtype_Indication (Decl_Node));
12718 end if;
12719
12720 -- In Ada 2012 the actual may be a limited view. Indicate that
12721 -- the local subtype must be treated as such.
12722
12723 if From_Limited_With (Act_T) then
12724 Set_Ekind (Subt, E_Incomplete_Subtype);
12725 Set_From_Limited_With (Subt);
12726 end if;
12727
12728 Decl_Nodes := New_List (Decl_Node);
12729
12730 -- Flag actual derived types so their elaboration produces the
12731 -- appropriate renamings for the primitive operations of the ancestor.
12732 -- Flag actual for formal private types as well, to determine whether
12733 -- operations in the private part may override inherited operations.
12734 -- If the formal has an interface list, the ancestor is not the
12735 -- parent, but the analyzed formal that includes the interface
12736 -- operations of all its progenitors.
12737
12738 -- Same treatment for formal private types, so we can check whether the
12739 -- type is tagged limited when validating derivations in the private
12740 -- part. (See AI05-096).
12741
12742 if Nkind (Def) = N_Formal_Derived_Type_Definition then
12743 if Present (Interface_List (Def)) then
12744 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
12745 else
12746 Set_Generic_Parent_Type (Decl_Node, Ancestor);
12747 end if;
12748
12749 elsif Nkind_In (Def, N_Formal_Private_Type_Definition,
12750 N_Formal_Incomplete_Type_Definition)
12751 then
12752 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
12753 end if;
12754
12755 -- If the actual is a synchronized type that implements an interface,
12756 -- the primitive operations are attached to the corresponding record,
12757 -- and we have to treat it as an additional generic actual, so that its
12758 -- primitive operations become visible in the instance. The task or
12759 -- protected type itself does not carry primitive operations.
12760
12761 if Is_Concurrent_Type (Act_T)
12762 and then Is_Tagged_Type (Act_T)
12763 and then Present (Corresponding_Record_Type (Act_T))
12764 and then Present (Ancestor)
12765 and then Is_Interface (Ancestor)
12766 then
12767 declare
12768 Corr_Rec : constant Entity_Id :=
12769 Corresponding_Record_Type (Act_T);
12770 New_Corr : Entity_Id;
12771 Corr_Decl : Node_Id;
12772
12773 begin
12774 New_Corr := Make_Temporary (Loc, 'S');
12775 Corr_Decl :=
12776 Make_Subtype_Declaration (Loc,
12777 Defining_Identifier => New_Corr,
12778 Subtype_Indication =>
12779 New_Occurrence_Of (Corr_Rec, Loc));
12780 Append_To (Decl_Nodes, Corr_Decl);
12781
12782 if Ekind (Act_T) = E_Task_Type then
12783 Set_Ekind (Subt, E_Task_Subtype);
12784 else
12785 Set_Ekind (Subt, E_Protected_Subtype);
12786 end if;
12787
12788 Set_Corresponding_Record_Type (Subt, Corr_Rec);
12789 Set_Generic_Parent_Type (Corr_Decl, Ancestor);
12790 Set_Generic_Parent_Type (Decl_Node, Empty);
12791 end;
12792 end if;
12793
12794 -- For a floating-point type, capture dimension info if any, because
12795 -- the generated subtype declaration does not come from source and
12796 -- will not process dimensions.
12797
12798 if Is_Floating_Point_Type (Act_T) then
12799 Copy_Dimensions (Act_T, Subt);
12800 end if;
12801
12802 return Decl_Nodes;
12803 end Instantiate_Type;
12804
12805 ---------------------
12806 -- Is_In_Main_Unit --
12807 ---------------------
12808
12809 function Is_In_Main_Unit (N : Node_Id) return Boolean is
12810 Unum : constant Unit_Number_Type := Get_Source_Unit (N);
12811 Current_Unit : Node_Id;
12812
12813 begin
12814 if Unum = Main_Unit then
12815 return True;
12816
12817 -- If the current unit is a subunit then it is either the main unit or
12818 -- is being compiled as part of the main unit.
12819
12820 elsif Nkind (N) = N_Compilation_Unit then
12821 return Nkind (Unit (N)) = N_Subunit;
12822 end if;
12823
12824 Current_Unit := Parent (N);
12825 while Present (Current_Unit)
12826 and then Nkind (Current_Unit) /= N_Compilation_Unit
12827 loop
12828 Current_Unit := Parent (Current_Unit);
12829 end loop;
12830
12831 -- The instantiation node is in the main unit, or else the current node
12832 -- (perhaps as the result of nested instantiations) is in the main unit,
12833 -- or in the declaration of the main unit, which in this last case must
12834 -- be a body.
12835
12836 return Unum = Main_Unit
12837 or else Current_Unit = Cunit (Main_Unit)
12838 or else Current_Unit = Library_Unit (Cunit (Main_Unit))
12839 or else (Present (Library_Unit (Current_Unit))
12840 and then Is_In_Main_Unit (Library_Unit (Current_Unit)));
12841 end Is_In_Main_Unit;
12842
12843 ----------------------------
12844 -- Load_Parent_Of_Generic --
12845 ----------------------------
12846
12847 procedure Load_Parent_Of_Generic
12848 (N : Node_Id;
12849 Spec : Node_Id;
12850 Body_Optional : Boolean := False)
12851 is
12852 Comp_Unit : constant Node_Id := Cunit (Get_Source_Unit (Spec));
12853 Saved_Style_Check : constant Boolean := Style_Check;
12854 Saved_Warnings : constant Warning_Record := Save_Warnings;
12855 True_Parent : Node_Id;
12856 Inst_Node : Node_Id;
12857 OK : Boolean;
12858 Previous_Instances : constant Elist_Id := New_Elmt_List;
12859
12860 procedure Collect_Previous_Instances (Decls : List_Id);
12861 -- Collect all instantiations in the given list of declarations, that
12862 -- precede the generic that we need to load. If the bodies of these
12863 -- instantiations are available, we must analyze them, to ensure that
12864 -- the public symbols generated are the same when the unit is compiled
12865 -- to generate code, and when it is compiled in the context of a unit
12866 -- that needs a particular nested instance. This process is applied to
12867 -- both package and subprogram instances.
12868
12869 --------------------------------
12870 -- Collect_Previous_Instances --
12871 --------------------------------
12872
12873 procedure Collect_Previous_Instances (Decls : List_Id) is
12874 Decl : Node_Id;
12875
12876 begin
12877 Decl := First (Decls);
12878 while Present (Decl) loop
12879 if Sloc (Decl) >= Sloc (Inst_Node) then
12880 return;
12881
12882 -- If Decl is an instantiation, then record it as requiring
12883 -- instantiation of the corresponding body, except if it is an
12884 -- abbreviated instantiation generated internally for conformance
12885 -- checking purposes only for the case of a formal package
12886 -- declared without a box (see Instantiate_Formal_Package). Such
12887 -- an instantiation does not generate any code (the actual code
12888 -- comes from actual) and thus does not need to be analyzed here.
12889 -- If the instantiation appears with a generic package body it is
12890 -- not analyzed here either.
12891
12892 elsif Nkind (Decl) = N_Package_Instantiation
12893 and then not Is_Internal (Defining_Entity (Decl))
12894 then
12895 Append_Elmt (Decl, Previous_Instances);
12896
12897 -- For a subprogram instantiation, omit instantiations intrinsic
12898 -- operations (Unchecked_Conversions, etc.) that have no bodies.
12899
12900 elsif Nkind_In (Decl, N_Function_Instantiation,
12901 N_Procedure_Instantiation)
12902 and then not Is_Intrinsic_Subprogram (Entity (Name (Decl)))
12903 then
12904 Append_Elmt (Decl, Previous_Instances);
12905
12906 elsif Nkind (Decl) = N_Package_Declaration then
12907 Collect_Previous_Instances
12908 (Visible_Declarations (Specification (Decl)));
12909 Collect_Previous_Instances
12910 (Private_Declarations (Specification (Decl)));
12911
12912 -- Previous non-generic bodies may contain instances as well
12913
12914 elsif Nkind (Decl) = N_Package_Body
12915 and then Ekind (Corresponding_Spec (Decl)) /= E_Generic_Package
12916 then
12917 Collect_Previous_Instances (Declarations (Decl));
12918
12919 elsif Nkind (Decl) = N_Subprogram_Body
12920 and then not Acts_As_Spec (Decl)
12921 and then not Is_Generic_Subprogram (Corresponding_Spec (Decl))
12922 then
12923 Collect_Previous_Instances (Declarations (Decl));
12924 end if;
12925
12926 Next (Decl);
12927 end loop;
12928 end Collect_Previous_Instances;
12929
12930 -- Start of processing for Load_Parent_Of_Generic
12931
12932 begin
12933 if not In_Same_Source_Unit (N, Spec)
12934 or else Nkind (Unit (Comp_Unit)) = N_Package_Declaration
12935 or else (Nkind (Unit (Comp_Unit)) = N_Package_Body
12936 and then not Is_In_Main_Unit (Spec))
12937 then
12938 -- Find body of parent of spec, and analyze it. A special case arises
12939 -- when the parent is an instantiation, that is to say when we are
12940 -- currently instantiating a nested generic. In that case, there is
12941 -- no separate file for the body of the enclosing instance. Instead,
12942 -- the enclosing body must be instantiated as if it were a pending
12943 -- instantiation, in order to produce the body for the nested generic
12944 -- we require now. Note that in that case the generic may be defined
12945 -- in a package body, the instance defined in the same package body,
12946 -- and the original enclosing body may not be in the main unit.
12947
12948 Inst_Node := Empty;
12949
12950 True_Parent := Parent (Spec);
12951 while Present (True_Parent)
12952 and then Nkind (True_Parent) /= N_Compilation_Unit
12953 loop
12954 if Nkind (True_Parent) = N_Package_Declaration
12955 and then
12956 Nkind (Original_Node (True_Parent)) = N_Package_Instantiation
12957 then
12958 -- Parent is a compilation unit that is an instantiation.
12959 -- Instantiation node has been replaced with package decl.
12960
12961 Inst_Node := Original_Node (True_Parent);
12962 exit;
12963
12964 elsif Nkind (True_Parent) = N_Package_Declaration
12965 and then Present (Generic_Parent (Specification (True_Parent)))
12966 and then Nkind (Parent (True_Parent)) /= N_Compilation_Unit
12967 then
12968 -- Parent is an instantiation within another specification.
12969 -- Declaration for instance has been inserted before original
12970 -- instantiation node. A direct link would be preferable?
12971
12972 Inst_Node := Next (True_Parent);
12973 while Present (Inst_Node)
12974 and then Nkind (Inst_Node) /= N_Package_Instantiation
12975 loop
12976 Next (Inst_Node);
12977 end loop;
12978
12979 -- If the instance appears within a generic, and the generic
12980 -- unit is defined within a formal package of the enclosing
12981 -- generic, there is no generic body available, and none
12982 -- needed. A more precise test should be used ???
12983
12984 if No (Inst_Node) then
12985 return;
12986 end if;
12987
12988 exit;
12989
12990 else
12991 True_Parent := Parent (True_Parent);
12992 end if;
12993 end loop;
12994
12995 -- Case where we are currently instantiating a nested generic
12996
12997 if Present (Inst_Node) then
12998 if Nkind (Parent (True_Parent)) = N_Compilation_Unit then
12999
13000 -- Instantiation node and declaration of instantiated package
13001 -- were exchanged when only the declaration was needed.
13002 -- Restore instantiation node before proceeding with body.
13003
13004 Set_Unit (Parent (True_Parent), Inst_Node);
13005 end if;
13006
13007 -- Now complete instantiation of enclosing body, if it appears in
13008 -- some other unit. If it appears in the current unit, the body
13009 -- will have been instantiated already.
13010
13011 if No (Corresponding_Body (Instance_Spec (Inst_Node))) then
13012
13013 -- We need to determine the expander mode to instantiate the
13014 -- enclosing body. Because the generic body we need may use
13015 -- global entities declared in the enclosing package (including
13016 -- aggregates) it is in general necessary to compile this body
13017 -- with expansion enabled, except if we are within a generic
13018 -- package, in which case the usual generic rule applies.
13019
13020 declare
13021 Exp_Status : Boolean := True;
13022 Scop : Entity_Id;
13023
13024 begin
13025 -- Loop through scopes looking for generic package
13026
13027 Scop := Scope (Defining_Entity (Instance_Spec (Inst_Node)));
13028 while Present (Scop)
13029 and then Scop /= Standard_Standard
13030 loop
13031 if Ekind (Scop) = E_Generic_Package then
13032 Exp_Status := False;
13033 exit;
13034 end if;
13035
13036 Scop := Scope (Scop);
13037 end loop;
13038
13039 -- Collect previous instantiations in the unit that contains
13040 -- the desired generic.
13041
13042 if Nkind (Parent (True_Parent)) /= N_Compilation_Unit
13043 and then not Body_Optional
13044 then
13045 declare
13046 Decl : Elmt_Id;
13047 Info : Pending_Body_Info;
13048 Par : Node_Id;
13049
13050 begin
13051 Par := Parent (Inst_Node);
13052 while Present (Par) loop
13053 exit when Nkind (Parent (Par)) = N_Compilation_Unit;
13054 Par := Parent (Par);
13055 end loop;
13056
13057 pragma Assert (Present (Par));
13058
13059 if Nkind (Par) = N_Package_Body then
13060 Collect_Previous_Instances (Declarations (Par));
13061
13062 elsif Nkind (Par) = N_Package_Declaration then
13063 Collect_Previous_Instances
13064 (Visible_Declarations (Specification (Par)));
13065 Collect_Previous_Instances
13066 (Private_Declarations (Specification (Par)));
13067
13068 else
13069 -- Enclosing unit is a subprogram body. In this
13070 -- case all instance bodies are processed in order
13071 -- and there is no need to collect them separately.
13072
13073 null;
13074 end if;
13075
13076 Decl := First_Elmt (Previous_Instances);
13077 while Present (Decl) loop
13078 Info :=
13079 (Inst_Node => Node (Decl),
13080 Act_Decl =>
13081 Instance_Spec (Node (Decl)),
13082 Expander_Status => Exp_Status,
13083 Current_Sem_Unit =>
13084 Get_Code_Unit (Sloc (Node (Decl))),
13085 Scope_Suppress => Scope_Suppress,
13086 Local_Suppress_Stack_Top =>
13087 Local_Suppress_Stack_Top,
13088 Version => Ada_Version,
13089 Version_Pragma => Ada_Version_Pragma,
13090 Warnings => Save_Warnings,
13091 SPARK_Mode => SPARK_Mode,
13092 SPARK_Mode_Pragma => SPARK_Mode_Pragma);
13093
13094 -- Package instance
13095
13096 if
13097 Nkind (Node (Decl)) = N_Package_Instantiation
13098 then
13099 Instantiate_Package_Body
13100 (Info, Body_Optional => True);
13101
13102 -- Subprogram instance
13103
13104 else
13105 -- The instance_spec is in the wrapper package,
13106 -- usually followed by its local renaming
13107 -- declaration. See Build_Subprogram_Renaming
13108 -- for details.
13109
13110 declare
13111 Decl : Node_Id :=
13112 (Last (Visible_Declarations
13113 (Specification (Info.Act_Decl))));
13114 begin
13115 if Nkind (Decl) =
13116 N_Subprogram_Renaming_Declaration
13117 then
13118 Decl := Prev (Decl);
13119 end if;
13120
13121 Info.Act_Decl := Decl;
13122 end;
13123
13124 Instantiate_Subprogram_Body
13125 (Info, Body_Optional => True);
13126 end if;
13127
13128 Next_Elmt (Decl);
13129 end loop;
13130 end;
13131 end if;
13132
13133 Instantiate_Package_Body
13134 (Body_Info =>
13135 ((Inst_Node => Inst_Node,
13136 Act_Decl => True_Parent,
13137 Expander_Status => Exp_Status,
13138 Current_Sem_Unit => Get_Code_Unit
13139 (Sloc (Inst_Node)),
13140 Scope_Suppress => Scope_Suppress,
13141 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
13142 Version => Ada_Version,
13143 Version_Pragma => Ada_Version_Pragma,
13144 Warnings => Save_Warnings,
13145 SPARK_Mode => SPARK_Mode,
13146 SPARK_Mode_Pragma => SPARK_Mode_Pragma)),
13147 Body_Optional => Body_Optional);
13148 end;
13149 end if;
13150
13151 -- Case where we are not instantiating a nested generic
13152
13153 else
13154 Opt.Style_Check := False;
13155 Expander_Mode_Save_And_Set (True);
13156 Load_Needed_Body (Comp_Unit, OK);
13157 Opt.Style_Check := Saved_Style_Check;
13158 Restore_Warnings (Saved_Warnings);
13159 Expander_Mode_Restore;
13160
13161 if not OK
13162 and then Unit_Requires_Body (Defining_Entity (Spec))
13163 and then not Body_Optional
13164 then
13165 declare
13166 Bname : constant Unit_Name_Type :=
13167 Get_Body_Name (Get_Unit_Name (Unit (Comp_Unit)));
13168
13169 begin
13170 -- In CodePeer mode, the missing body may make the analysis
13171 -- incomplete, but we do not treat it as fatal.
13172
13173 if CodePeer_Mode then
13174 return;
13175
13176 else
13177 Error_Msg_Unit_1 := Bname;
13178 Error_Msg_N ("this instantiation requires$!", N);
13179 Error_Msg_File_1 :=
13180 Get_File_Name (Bname, Subunit => False);
13181 Error_Msg_N ("\but file{ was not found!", N);
13182 raise Unrecoverable_Error;
13183 end if;
13184 end;
13185 end if;
13186 end if;
13187 end if;
13188
13189 -- If loading parent of the generic caused an instantiation circularity,
13190 -- we abandon compilation at this point, because otherwise in some cases
13191 -- we get into trouble with infinite recursions after this point.
13192
13193 if Circularity_Detected then
13194 raise Unrecoverable_Error;
13195 end if;
13196 end Load_Parent_Of_Generic;
13197
13198 ---------------------------------
13199 -- Map_Formal_Package_Entities --
13200 ---------------------------------
13201
13202 procedure Map_Formal_Package_Entities (Form : Entity_Id; Act : Entity_Id) is
13203 E1 : Entity_Id;
13204 E2 : Entity_Id;
13205
13206 begin
13207 Set_Instance_Of (Form, Act);
13208
13209 -- Traverse formal and actual package to map the corresponding entities.
13210 -- We skip over internal entities that may be generated during semantic
13211 -- analysis, and find the matching entities by name, given that they
13212 -- must appear in the same order.
13213
13214 E1 := First_Entity (Form);
13215 E2 := First_Entity (Act);
13216 while Present (E1) and then E1 /= First_Private_Entity (Form) loop
13217 -- Could this test be a single condition??? Seems like it could, and
13218 -- isn't FPE (Form) a constant anyway???
13219
13220 if not Is_Internal (E1)
13221 and then Present (Parent (E1))
13222 and then not Is_Class_Wide_Type (E1)
13223 and then not Is_Internal_Name (Chars (E1))
13224 then
13225 while Present (E2) and then Chars (E2) /= Chars (E1) loop
13226 Next_Entity (E2);
13227 end loop;
13228
13229 if No (E2) then
13230 exit;
13231 else
13232 Set_Instance_Of (E1, E2);
13233
13234 if Is_Type (E1) and then Is_Tagged_Type (E2) then
13235 Set_Instance_Of (Class_Wide_Type (E1), Class_Wide_Type (E2));
13236 end if;
13237
13238 if Is_Constrained (E1) then
13239 Set_Instance_Of (Base_Type (E1), Base_Type (E2));
13240 end if;
13241
13242 if Ekind (E1) = E_Package and then No (Renamed_Object (E1)) then
13243 Map_Formal_Package_Entities (E1, E2);
13244 end if;
13245 end if;
13246 end if;
13247
13248 Next_Entity (E1);
13249 end loop;
13250 end Map_Formal_Package_Entities;
13251
13252 -----------------------
13253 -- Move_Freeze_Nodes --
13254 -----------------------
13255
13256 procedure Move_Freeze_Nodes
13257 (Out_Of : Entity_Id;
13258 After : Node_Id;
13259 L : List_Id)
13260 is
13261 Decl : Node_Id;
13262 Next_Decl : Node_Id;
13263 Next_Node : Node_Id := After;
13264 Spec : Node_Id;
13265
13266 function Is_Outer_Type (T : Entity_Id) return Boolean;
13267 -- Check whether entity is declared in a scope external to that of the
13268 -- generic unit.
13269
13270 -------------------
13271 -- Is_Outer_Type --
13272 -------------------
13273
13274 function Is_Outer_Type (T : Entity_Id) return Boolean is
13275 Scop : Entity_Id := Scope (T);
13276
13277 begin
13278 if Scope_Depth (Scop) < Scope_Depth (Out_Of) then
13279 return True;
13280
13281 else
13282 while Scop /= Standard_Standard loop
13283 if Scop = Out_Of then
13284 return False;
13285 else
13286 Scop := Scope (Scop);
13287 end if;
13288 end loop;
13289
13290 return True;
13291 end if;
13292 end Is_Outer_Type;
13293
13294 -- Start of processing for Move_Freeze_Nodes
13295
13296 begin
13297 if No (L) then
13298 return;
13299 end if;
13300
13301 -- First remove the freeze nodes that may appear before all other
13302 -- declarations.
13303
13304 Decl := First (L);
13305 while Present (Decl)
13306 and then Nkind (Decl) = N_Freeze_Entity
13307 and then Is_Outer_Type (Entity (Decl))
13308 loop
13309 Decl := Remove_Head (L);
13310 Insert_After (Next_Node, Decl);
13311 Set_Analyzed (Decl, False);
13312 Next_Node := Decl;
13313 Decl := First (L);
13314 end loop;
13315
13316 -- Next scan the list of declarations and remove each freeze node that
13317 -- appears ahead of the current node.
13318
13319 while Present (Decl) loop
13320 while Present (Next (Decl))
13321 and then Nkind (Next (Decl)) = N_Freeze_Entity
13322 and then Is_Outer_Type (Entity (Next (Decl)))
13323 loop
13324 Next_Decl := Remove_Next (Decl);
13325 Insert_After (Next_Node, Next_Decl);
13326 Set_Analyzed (Next_Decl, False);
13327 Next_Node := Next_Decl;
13328 end loop;
13329
13330 -- If the declaration is a nested package or concurrent type, then
13331 -- recurse. Nested generic packages will have been processed from the
13332 -- inside out.
13333
13334 case Nkind (Decl) is
13335 when N_Package_Declaration =>
13336 Spec := Specification (Decl);
13337
13338 when N_Task_Type_Declaration =>
13339 Spec := Task_Definition (Decl);
13340
13341 when N_Protected_Type_Declaration =>
13342 Spec := Protected_Definition (Decl);
13343
13344 when others =>
13345 Spec := Empty;
13346 end case;
13347
13348 if Present (Spec) then
13349 Move_Freeze_Nodes (Out_Of, Next_Node, Visible_Declarations (Spec));
13350 Move_Freeze_Nodes (Out_Of, Next_Node, Private_Declarations (Spec));
13351 end if;
13352
13353 Next (Decl);
13354 end loop;
13355 end Move_Freeze_Nodes;
13356
13357 ----------------
13358 -- Next_Assoc --
13359 ----------------
13360
13361 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr is
13362 begin
13363 return Generic_Renamings.Table (E).Next_In_HTable;
13364 end Next_Assoc;
13365
13366 ------------------------
13367 -- Preanalyze_Actuals --
13368 ------------------------
13369
13370 procedure Preanalyze_Actuals (N : Node_Id; Inst : Entity_Id := Empty) is
13371 Assoc : Node_Id;
13372 Act : Node_Id;
13373 Errs : constant Nat := Serious_Errors_Detected;
13374
13375 Cur : Entity_Id := Empty;
13376 -- Current homograph of the instance name
13377
13378 Vis : Boolean;
13379 -- Saved visibility status of the current homograph
13380
13381 begin
13382 Assoc := First (Generic_Associations (N));
13383
13384 -- If the instance is a child unit, its name may hide an outer homonym,
13385 -- so make it invisible to perform name resolution on the actuals.
13386
13387 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name
13388 and then Present
13389 (Current_Entity (Defining_Identifier (Defining_Unit_Name (N))))
13390 then
13391 Cur := Current_Entity (Defining_Identifier (Defining_Unit_Name (N)));
13392
13393 if Is_Compilation_Unit (Cur) then
13394 Vis := Is_Immediately_Visible (Cur);
13395 Set_Is_Immediately_Visible (Cur, False);
13396 else
13397 Cur := Empty;
13398 end if;
13399 end if;
13400
13401 while Present (Assoc) loop
13402 if Nkind (Assoc) /= N_Others_Choice then
13403 Act := Explicit_Generic_Actual_Parameter (Assoc);
13404
13405 -- Within a nested instantiation, a defaulted actual is an empty
13406 -- association, so nothing to analyze. If the subprogram actual
13407 -- is an attribute, analyze prefix only, because actual is not a
13408 -- complete attribute reference.
13409
13410 -- If actual is an allocator, analyze expression only. The full
13411 -- analysis can generate code, and if instance is a compilation
13412 -- unit we have to wait until the package instance is installed
13413 -- to have a proper place to insert this code.
13414
13415 -- String literals may be operators, but at this point we do not
13416 -- know whether the actual is a formal subprogram or a string.
13417
13418 if No (Act) then
13419 null;
13420
13421 elsif Nkind (Act) = N_Attribute_Reference then
13422 Analyze (Prefix (Act));
13423
13424 elsif Nkind (Act) = N_Explicit_Dereference then
13425 Analyze (Prefix (Act));
13426
13427 elsif Nkind (Act) = N_Allocator then
13428 declare
13429 Expr : constant Node_Id := Expression (Act);
13430
13431 begin
13432 if Nkind (Expr) = N_Subtype_Indication then
13433 Analyze (Subtype_Mark (Expr));
13434
13435 -- Analyze separately each discriminant constraint, when
13436 -- given with a named association.
13437
13438 declare
13439 Constr : Node_Id;
13440
13441 begin
13442 Constr := First (Constraints (Constraint (Expr)));
13443 while Present (Constr) loop
13444 if Nkind (Constr) = N_Discriminant_Association then
13445 Analyze (Expression (Constr));
13446 else
13447 Analyze (Constr);
13448 end if;
13449
13450 Next (Constr);
13451 end loop;
13452 end;
13453
13454 else
13455 Analyze (Expr);
13456 end if;
13457 end;
13458
13459 elsif Nkind (Act) /= N_Operator_Symbol then
13460 Analyze (Act);
13461
13462 -- Within a package instance, mark actuals that are limited
13463 -- views, so their use can be moved to the body of the
13464 -- enclosing unit.
13465
13466 if Is_Entity_Name (Act)
13467 and then Is_Type (Entity (Act))
13468 and then From_Limited_With (Entity (Act))
13469 and then Present (Inst)
13470 then
13471 Append_Elmt (Entity (Act), Incomplete_Actuals (Inst));
13472 end if;
13473 end if;
13474
13475 if Errs /= Serious_Errors_Detected then
13476
13477 -- Do a minimal analysis of the generic, to prevent spurious
13478 -- warnings complaining about the generic being unreferenced,
13479 -- before abandoning the instantiation.
13480
13481 Analyze (Name (N));
13482
13483 if Is_Entity_Name (Name (N))
13484 and then Etype (Name (N)) /= Any_Type
13485 then
13486 Generate_Reference (Entity (Name (N)), Name (N));
13487 Set_Is_Instantiated (Entity (Name (N)));
13488 end if;
13489
13490 if Present (Cur) then
13491
13492 -- For the case of a child instance hiding an outer homonym,
13493 -- provide additional warning which might explain the error.
13494
13495 Set_Is_Immediately_Visible (Cur, Vis);
13496 Error_Msg_NE
13497 ("& hides outer unit with the same name??",
13498 N, Defining_Unit_Name (N));
13499 end if;
13500
13501 Abandon_Instantiation (Act);
13502 end if;
13503 end if;
13504
13505 Next (Assoc);
13506 end loop;
13507
13508 if Present (Cur) then
13509 Set_Is_Immediately_Visible (Cur, Vis);
13510 end if;
13511 end Preanalyze_Actuals;
13512
13513 -------------------
13514 -- Remove_Parent --
13515 -------------------
13516
13517 procedure Remove_Parent (In_Body : Boolean := False) is
13518 S : Entity_Id := Current_Scope;
13519 -- S is the scope containing the instantiation just completed. The scope
13520 -- stack contains the parent instances of the instantiation, followed by
13521 -- the original S.
13522
13523 Cur_P : Entity_Id;
13524 E : Entity_Id;
13525 P : Entity_Id;
13526 Hidden : Elmt_Id;
13527
13528 begin
13529 -- After child instantiation is complete, remove from scope stack the
13530 -- extra copy of the current scope, and then remove parent instances.
13531
13532 if not In_Body then
13533 Pop_Scope;
13534
13535 while Current_Scope /= S loop
13536 P := Current_Scope;
13537 End_Package_Scope (Current_Scope);
13538
13539 if In_Open_Scopes (P) then
13540 E := First_Entity (P);
13541 while Present (E) loop
13542 Set_Is_Immediately_Visible (E, True);
13543 Next_Entity (E);
13544 end loop;
13545
13546 -- If instantiation is declared in a block, it is the enclosing
13547 -- scope that might be a parent instance. Note that only one
13548 -- block can be involved, because the parent instances have
13549 -- been installed within it.
13550
13551 if Ekind (P) = E_Block then
13552 Cur_P := Scope (P);
13553 else
13554 Cur_P := P;
13555 end if;
13556
13557 if Is_Generic_Instance (Cur_P) and then P /= Current_Scope then
13558 -- We are within an instance of some sibling. Retain
13559 -- visibility of parent, for proper subsequent cleanup, and
13560 -- reinstall private declarations as well.
13561
13562 Set_In_Private_Part (P);
13563 Install_Private_Declarations (P);
13564 end if;
13565
13566 -- If the ultimate parent is a top-level unit recorded in
13567 -- Instance_Parent_Unit, then reset its visibility to what it was
13568 -- before instantiation. (It's not clear what the purpose is of
13569 -- testing whether Scope (P) is In_Open_Scopes, but that test was
13570 -- present before the ultimate parent test was added.???)
13571
13572 elsif not In_Open_Scopes (Scope (P))
13573 or else (P = Instance_Parent_Unit
13574 and then not Parent_Unit_Visible)
13575 then
13576 Set_Is_Immediately_Visible (P, False);
13577
13578 -- If the current scope is itself an instantiation of a generic
13579 -- nested within P, and we are in the private part of body of this
13580 -- instantiation, restore the full views of P, that were removed
13581 -- in End_Package_Scope above. This obscure case can occur when a
13582 -- subunit of a generic contains an instance of a child unit of
13583 -- its generic parent unit.
13584
13585 elsif S = Current_Scope and then Is_Generic_Instance (S) then
13586 declare
13587 Par : constant Entity_Id :=
13588 Generic_Parent (Package_Specification (S));
13589 begin
13590 if Present (Par)
13591 and then P = Scope (Par)
13592 and then (In_Package_Body (S) or else In_Private_Part (S))
13593 then
13594 Set_In_Private_Part (P);
13595 Install_Private_Declarations (P);
13596 end if;
13597 end;
13598 end if;
13599 end loop;
13600
13601 -- Reset visibility of entities in the enclosing scope
13602
13603 Set_Is_Hidden_Open_Scope (Current_Scope, False);
13604
13605 Hidden := First_Elmt (Hidden_Entities);
13606 while Present (Hidden) loop
13607 Set_Is_Immediately_Visible (Node (Hidden), True);
13608 Next_Elmt (Hidden);
13609 end loop;
13610
13611 else
13612 -- Each body is analyzed separately, and there is no context that
13613 -- needs preserving from one body instance to the next, so remove all
13614 -- parent scopes that have been installed.
13615
13616 while Present (S) loop
13617 End_Package_Scope (S);
13618 Set_Is_Immediately_Visible (S, False);
13619 S := Current_Scope;
13620 exit when S = Standard_Standard;
13621 end loop;
13622 end if;
13623 end Remove_Parent;
13624
13625 -----------------
13626 -- Restore_Env --
13627 -----------------
13628
13629 procedure Restore_Env is
13630 Saved : Instance_Env renames Instance_Envs.Table (Instance_Envs.Last);
13631
13632 begin
13633 if No (Current_Instantiated_Parent.Act_Id) then
13634 -- Restore environment after subprogram inlining
13635
13636 Restore_Private_Views (Empty);
13637 end if;
13638
13639 Current_Instantiated_Parent := Saved.Instantiated_Parent;
13640 Exchanged_Views := Saved.Exchanged_Views;
13641 Hidden_Entities := Saved.Hidden_Entities;
13642 Current_Sem_Unit := Saved.Current_Sem_Unit;
13643 Parent_Unit_Visible := Saved.Parent_Unit_Visible;
13644 Instance_Parent_Unit := Saved.Instance_Parent_Unit;
13645
13646 Restore_Opt_Config_Switches (Saved.Switches);
13647
13648 Instance_Envs.Decrement_Last;
13649 end Restore_Env;
13650
13651 ---------------------------
13652 -- Restore_Private_Views --
13653 ---------------------------
13654
13655 procedure Restore_Private_Views
13656 (Pack_Id : Entity_Id;
13657 Is_Package : Boolean := True)
13658 is
13659 M : Elmt_Id;
13660 E : Entity_Id;
13661 Typ : Entity_Id;
13662 Dep_Elmt : Elmt_Id;
13663 Dep_Typ : Node_Id;
13664
13665 procedure Restore_Nested_Formal (Formal : Entity_Id);
13666 -- Hide the generic formals of formal packages declared with box which
13667 -- were reachable in the current instantiation.
13668
13669 ---------------------------
13670 -- Restore_Nested_Formal --
13671 ---------------------------
13672
13673 procedure Restore_Nested_Formal (Formal : Entity_Id) is
13674 Ent : Entity_Id;
13675
13676 begin
13677 if Present (Renamed_Object (Formal))
13678 and then Denotes_Formal_Package (Renamed_Object (Formal), True)
13679 then
13680 return;
13681
13682 elsif Present (Associated_Formal_Package (Formal)) then
13683 Ent := First_Entity (Formal);
13684 while Present (Ent) loop
13685 exit when Ekind (Ent) = E_Package
13686 and then Renamed_Entity (Ent) = Renamed_Entity (Formal);
13687
13688 Set_Is_Hidden (Ent);
13689 Set_Is_Potentially_Use_Visible (Ent, False);
13690
13691 -- If package, then recurse
13692
13693 if Ekind (Ent) = E_Package then
13694 Restore_Nested_Formal (Ent);
13695 end if;
13696
13697 Next_Entity (Ent);
13698 end loop;
13699 end if;
13700 end Restore_Nested_Formal;
13701
13702 -- Start of processing for Restore_Private_Views
13703
13704 begin
13705 M := First_Elmt (Exchanged_Views);
13706 while Present (M) loop
13707 Typ := Node (M);
13708
13709 -- Subtypes of types whose views have been exchanged, and that are
13710 -- defined within the instance, were not on the Private_Dependents
13711 -- list on entry to the instance, so they have to be exchanged
13712 -- explicitly now, in order to remain consistent with the view of the
13713 -- parent type.
13714
13715 if Ekind_In (Typ, E_Private_Type,
13716 E_Limited_Private_Type,
13717 E_Record_Type_With_Private)
13718 then
13719 Dep_Elmt := First_Elmt (Private_Dependents (Typ));
13720 while Present (Dep_Elmt) loop
13721 Dep_Typ := Node (Dep_Elmt);
13722
13723 if Scope (Dep_Typ) = Pack_Id
13724 and then Present (Full_View (Dep_Typ))
13725 then
13726 Replace_Elmt (Dep_Elmt, Full_View (Dep_Typ));
13727 Exchange_Declarations (Dep_Typ);
13728 end if;
13729
13730 Next_Elmt (Dep_Elmt);
13731 end loop;
13732 end if;
13733
13734 Exchange_Declarations (Node (M));
13735 Next_Elmt (M);
13736 end loop;
13737
13738 if No (Pack_Id) then
13739 return;
13740 end if;
13741
13742 -- Make the generic formal parameters private, and make the formal types
13743 -- into subtypes of the actuals again.
13744
13745 E := First_Entity (Pack_Id);
13746 while Present (E) loop
13747 Set_Is_Hidden (E, True);
13748
13749 if Is_Type (E)
13750 and then Nkind (Parent (E)) = N_Subtype_Declaration
13751 then
13752 -- If the actual for E is itself a generic actual type from
13753 -- an enclosing instance, E is still a generic actual type
13754 -- outside of the current instance. This matter when resolving
13755 -- an overloaded call that may be ambiguous in the enclosing
13756 -- instance, when two of its actuals coincide.
13757
13758 if Is_Entity_Name (Subtype_Indication (Parent (E)))
13759 and then Is_Generic_Actual_Type
13760 (Entity (Subtype_Indication (Parent (E))))
13761 then
13762 null;
13763 else
13764 Set_Is_Generic_Actual_Type (E, False);
13765 end if;
13766
13767 -- An unusual case of aliasing: the actual may also be directly
13768 -- visible in the generic, and be private there, while it is fully
13769 -- visible in the context of the instance. The internal subtype
13770 -- is private in the instance but has full visibility like its
13771 -- parent in the enclosing scope. This enforces the invariant that
13772 -- the privacy status of all private dependents of a type coincide
13773 -- with that of the parent type. This can only happen when a
13774 -- generic child unit is instantiated within a sibling.
13775
13776 if Is_Private_Type (E)
13777 and then not Is_Private_Type (Etype (E))
13778 then
13779 Exchange_Declarations (E);
13780 end if;
13781
13782 elsif Ekind (E) = E_Package then
13783
13784 -- The end of the renaming list is the renaming of the generic
13785 -- package itself. If the instance is a subprogram, all entities
13786 -- in the corresponding package are renamings. If this entity is
13787 -- a formal package, make its own formals private as well. The
13788 -- actual in this case is itself the renaming of an instantiation.
13789 -- If the entity is not a package renaming, it is the entity
13790 -- created to validate formal package actuals: ignore it.
13791
13792 -- If the actual is itself a formal package for the enclosing
13793 -- generic, or the actual for such a formal package, it remains
13794 -- visible on exit from the instance, and therefore nothing needs
13795 -- to be done either, except to keep it accessible.
13796
13797 if Is_Package and then Renamed_Object (E) = Pack_Id then
13798 exit;
13799
13800 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
13801 null;
13802
13803 elsif
13804 Denotes_Formal_Package (Renamed_Object (E), True, Pack_Id)
13805 then
13806 Set_Is_Hidden (E, False);
13807
13808 else
13809 declare
13810 Act_P : constant Entity_Id := Renamed_Object (E);
13811 Id : Entity_Id;
13812
13813 begin
13814 Id := First_Entity (Act_P);
13815 while Present (Id)
13816 and then Id /= First_Private_Entity (Act_P)
13817 loop
13818 exit when Ekind (Id) = E_Package
13819 and then Renamed_Object (Id) = Act_P;
13820
13821 Set_Is_Hidden (Id, True);
13822 Set_Is_Potentially_Use_Visible (Id, In_Use (Act_P));
13823
13824 if Ekind (Id) = E_Package then
13825 Restore_Nested_Formal (Id);
13826 end if;
13827
13828 Next_Entity (Id);
13829 end loop;
13830 end;
13831 end if;
13832 end if;
13833
13834 Next_Entity (E);
13835 end loop;
13836 end Restore_Private_Views;
13837
13838 --------------
13839 -- Save_Env --
13840 --------------
13841
13842 procedure Save_Env
13843 (Gen_Unit : Entity_Id;
13844 Act_Unit : Entity_Id)
13845 is
13846 begin
13847 Init_Env;
13848 Set_Instance_Env (Gen_Unit, Act_Unit);
13849 end Save_Env;
13850
13851 ----------------------------
13852 -- Save_Global_References --
13853 ----------------------------
13854
13855 procedure Save_Global_References (Templ : Node_Id) is
13856
13857 -- ??? it is horrible to use global variables in highly recursive code
13858
13859 E : Entity_Id;
13860 -- The entity of the current associated node
13861
13862 Gen_Scope : Entity_Id;
13863 -- The scope of the generic for which references are being saved
13864
13865 N2 : Node_Id;
13866 -- The current associated node
13867
13868 function Is_Global (E : Entity_Id) return Boolean;
13869 -- Check whether entity is defined outside of generic unit. Examine the
13870 -- scope of an entity, and the scope of the scope, etc, until we find
13871 -- either Standard, in which case the entity is global, or the generic
13872 -- unit itself, which indicates that the entity is local. If the entity
13873 -- is the generic unit itself, as in the case of a recursive call, or
13874 -- the enclosing generic unit, if different from the current scope, then
13875 -- it is local as well, because it will be replaced at the point of
13876 -- instantiation. On the other hand, if it is a reference to a child
13877 -- unit of a common ancestor, which appears in an instantiation, it is
13878 -- global because it is used to denote a specific compilation unit at
13879 -- the time the instantiations will be analyzed.
13880
13881 procedure Qualify_Universal_Operands
13882 (Op : Node_Id;
13883 Func_Call : Node_Id);
13884 -- Op denotes a binary or unary operator in generic template Templ. Node
13885 -- Func_Call is the function call alternative of the operator within the
13886 -- the analyzed copy of the template. Change each operand which yields a
13887 -- universal type by wrapping it into a qualified expression
13888 --
13889 -- Actual_Typ'(Operand)
13890 --
13891 -- where Actual_Typ is the type of corresponding actual parameter of
13892 -- Operand in Func_Call.
13893
13894 procedure Reset_Entity (N : Node_Id);
13895 -- Save semantic information on global entity so that it is not resolved
13896 -- again at instantiation time.
13897
13898 procedure Save_Entity_Descendants (N : Node_Id);
13899 -- Apply Save_Global_References to the two syntactic descendants of
13900 -- non-terminal nodes that carry an Associated_Node and are processed
13901 -- through Reset_Entity. Once the global entity (if any) has been
13902 -- captured together with its type, only two syntactic descendants need
13903 -- to be traversed to complete the processing of the tree rooted at N.
13904 -- This applies to Selected_Components, Expanded_Names, and to Operator
13905 -- nodes. N can also be a character literal, identifier, or operator
13906 -- symbol node, but the call has no effect in these cases.
13907
13908 procedure Save_Global_Defaults (N1 : Node_Id; N2 : Node_Id);
13909 -- Default actuals in nested instances must be handled specially
13910 -- because there is no link to them from the original tree. When an
13911 -- actual subprogram is given by a default, we add an explicit generic
13912 -- association for it in the instantiation node. When we save the
13913 -- global references on the name of the instance, we recover the list
13914 -- of generic associations, and add an explicit one to the original
13915 -- generic tree, through which a global actual can be preserved.
13916 -- Similarly, if a child unit is instantiated within a sibling, in the
13917 -- context of the parent, we must preserve the identifier of the parent
13918 -- so that it can be properly resolved in a subsequent instantiation.
13919
13920 procedure Save_Global_Descendant (D : Union_Id);
13921 -- Apply Save_References recursively to the descendants of node D
13922
13923 procedure Save_References (N : Node_Id);
13924 -- This is the recursive procedure that does the work, once the
13925 -- enclosing generic scope has been established.
13926
13927 ---------------
13928 -- Is_Global --
13929 ---------------
13930
13931 function Is_Global (E : Entity_Id) return Boolean is
13932 Se : Entity_Id;
13933
13934 function Is_Instance_Node (Decl : Node_Id) return Boolean;
13935 -- Determine whether the parent node of a reference to a child unit
13936 -- denotes an instantiation or a formal package, in which case the
13937 -- reference to the child unit is global, even if it appears within
13938 -- the current scope (e.g. when the instance appears within the body
13939 -- of an ancestor).
13940
13941 ----------------------
13942 -- Is_Instance_Node --
13943 ----------------------
13944
13945 function Is_Instance_Node (Decl : Node_Id) return Boolean is
13946 begin
13947 return Nkind (Decl) in N_Generic_Instantiation
13948 or else
13949 Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration;
13950 end Is_Instance_Node;
13951
13952 -- Start of processing for Is_Global
13953
13954 begin
13955 if E = Gen_Scope then
13956 return False;
13957
13958 elsif E = Standard_Standard then
13959 return True;
13960
13961 elsif Is_Child_Unit (E)
13962 and then (Is_Instance_Node (Parent (N2))
13963 or else (Nkind (Parent (N2)) = N_Expanded_Name
13964 and then N2 = Selector_Name (Parent (N2))
13965 and then
13966 Is_Instance_Node (Parent (Parent (N2)))))
13967 then
13968 return True;
13969
13970 else
13971 Se := Scope (E);
13972 while Se /= Gen_Scope loop
13973 if Se = Standard_Standard then
13974 return True;
13975 else
13976 Se := Scope (Se);
13977 end if;
13978 end loop;
13979
13980 return False;
13981 end if;
13982 end Is_Global;
13983
13984 --------------------------------
13985 -- Qualify_Universal_Operands --
13986 --------------------------------
13987
13988 procedure Qualify_Universal_Operands
13989 (Op : Node_Id;
13990 Func_Call : Node_Id)
13991 is
13992 procedure Qualify_Operand (Opnd : Node_Id; Actual : Node_Id);
13993 -- Rewrite operand Opnd as a qualified expression of the form
13994 --
13995 -- Actual_Typ'(Opnd)
13996 --
13997 -- where Actual is the corresponding actual parameter of Opnd in
13998 -- function call Func_Call.
13999
14000 function Qualify_Type
14001 (Loc : Source_Ptr;
14002 Typ : Entity_Id) return Node_Id;
14003 -- Qualify type Typ by creating a selected component of the form
14004 --
14005 -- Scope_Of_Typ.Typ
14006
14007 ---------------------
14008 -- Qualify_Operand --
14009 ---------------------
14010
14011 procedure Qualify_Operand (Opnd : Node_Id; Actual : Node_Id) is
14012 Loc : constant Source_Ptr := Sloc (Opnd);
14013 Typ : constant Entity_Id := Etype (Actual);
14014 Mark : Node_Id;
14015 Qual : Node_Id;
14016
14017 begin
14018 -- Qualify the operand when it is of a universal type. Note that
14019 -- the template is unanalyzed and it is not possible to directly
14020 -- query the type. This transformation is not done when the type
14021 -- of the actual is internally generated because the type will be
14022 -- regenerated in the instance.
14023
14024 if Yields_Universal_Type (Opnd)
14025 and then Comes_From_Source (Typ)
14026 and then not Is_Hidden (Typ)
14027 then
14028 -- The type of the actual may be a global reference. Save this
14029 -- information by creating a reference to it.
14030
14031 if Is_Global (Typ) then
14032 Mark := New_Occurrence_Of (Typ, Loc);
14033
14034 -- Otherwise rely on resolution to find the proper type within
14035 -- the instance.
14036
14037 else
14038 Mark := Qualify_Type (Loc, Typ);
14039 end if;
14040
14041 Qual :=
14042 Make_Qualified_Expression (Loc,
14043 Subtype_Mark => Mark,
14044 Expression => Relocate_Node (Opnd));
14045
14046 -- Mark the qualification to distinguish it from other source
14047 -- constructs and signal the instantiation mechanism that this
14048 -- node requires special processing. See Copy_Generic_Node for
14049 -- details.
14050
14051 Set_Is_Qualified_Universal_Literal (Qual);
14052
14053 Rewrite (Opnd, Qual);
14054 end if;
14055 end Qualify_Operand;
14056
14057 ------------------
14058 -- Qualify_Type --
14059 ------------------
14060
14061 function Qualify_Type
14062 (Loc : Source_Ptr;
14063 Typ : Entity_Id) return Node_Id
14064 is
14065 Scop : constant Entity_Id := Scope (Typ);
14066 Result : Node_Id;
14067
14068 begin
14069 Result := Make_Identifier (Loc, Chars (Typ));
14070
14071 if Present (Scop) and then not Is_Generic_Unit (Scop) then
14072 Result :=
14073 Make_Selected_Component (Loc,
14074 Prefix => Make_Identifier (Loc, Chars (Scop)),
14075 Selector_Name => Result);
14076 end if;
14077
14078 return Result;
14079 end Qualify_Type;
14080
14081 -- Local variables
14082
14083 Actuals : constant List_Id := Parameter_Associations (Func_Call);
14084
14085 -- Start of processing for Qualify_Universal_Operands
14086
14087 begin
14088 if Nkind (Op) in N_Binary_Op then
14089 Qualify_Operand (Left_Opnd (Op), First (Actuals));
14090 Qualify_Operand (Right_Opnd (Op), Next (First (Actuals)));
14091
14092 elsif Nkind (Op) in N_Unary_Op then
14093 Qualify_Operand (Right_Opnd (Op), First (Actuals));
14094 end if;
14095 end Qualify_Universal_Operands;
14096
14097 ------------------
14098 -- Reset_Entity --
14099 ------------------
14100
14101 procedure Reset_Entity (N : Node_Id) is
14102 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id);
14103 -- If the type of N2 is global to the generic unit, save the type in
14104 -- the generic node. Just as we perform name capture for explicit
14105 -- references within the generic, we must capture the global types
14106 -- of local entities because they may participate in resolution in
14107 -- the instance.
14108
14109 function Top_Ancestor (E : Entity_Id) return Entity_Id;
14110 -- Find the ultimate ancestor of the current unit. If it is not a
14111 -- generic unit, then the name of the current unit in the prefix of
14112 -- an expanded name must be replaced with its generic homonym to
14113 -- ensure that it will be properly resolved in an instance.
14114
14115 ---------------------
14116 -- Set_Global_Type --
14117 ---------------------
14118
14119 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id) is
14120 Typ : constant Entity_Id := Etype (N2);
14121
14122 begin
14123 Set_Etype (N, Typ);
14124
14125 -- If the entity of N is not the associated node, this is a
14126 -- nested generic and it has an associated node as well, whose
14127 -- type is already the full view (see below). Indicate that the
14128 -- original node has a private view.
14129
14130 if Entity (N) /= N2 and then Has_Private_View (Entity (N)) then
14131 Set_Has_Private_View (N);
14132 end if;
14133
14134 -- If not a private type, nothing else to do
14135
14136 if not Is_Private_Type (Typ) then
14137 if Is_Array_Type (Typ)
14138 and then Is_Private_Type (Component_Type (Typ))
14139 then
14140 Set_Has_Private_View (N);
14141 end if;
14142
14143 -- If it is a derivation of a private type in a context where no
14144 -- full view is needed, nothing to do either.
14145
14146 elsif No (Full_View (Typ)) and then Typ /= Etype (Typ) then
14147 null;
14148
14149 -- Otherwise mark the type for flipping and use the full view when
14150 -- available.
14151
14152 else
14153 Set_Has_Private_View (N);
14154
14155 if Present (Full_View (Typ)) then
14156 Set_Etype (N2, Full_View (Typ));
14157 end if;
14158 end if;
14159
14160 if Is_Floating_Point_Type (Typ)
14161 and then Has_Dimension_System (Typ)
14162 then
14163 Copy_Dimensions (N2, N);
14164 end if;
14165
14166 end Set_Global_Type;
14167
14168 ------------------
14169 -- Top_Ancestor --
14170 ------------------
14171
14172 function Top_Ancestor (E : Entity_Id) return Entity_Id is
14173 Par : Entity_Id;
14174
14175 begin
14176 Par := E;
14177 while Is_Child_Unit (Par) loop
14178 Par := Scope (Par);
14179 end loop;
14180
14181 return Par;
14182 end Top_Ancestor;
14183
14184 -- Start of processing for Reset_Entity
14185
14186 begin
14187 N2 := Get_Associated_Node (N);
14188 E := Entity (N2);
14189
14190 if Present (E) then
14191
14192 -- If the node is an entry call to an entry in an enclosing task,
14193 -- it is rewritten as a selected component. No global entity to
14194 -- preserve in this case, since the expansion will be redone in
14195 -- the instance.
14196
14197 if not Nkind_In (E, N_Defining_Character_Literal,
14198 N_Defining_Identifier,
14199 N_Defining_Operator_Symbol)
14200 then
14201 Set_Associated_Node (N, Empty);
14202 Set_Etype (N, Empty);
14203 return;
14204 end if;
14205
14206 -- If the entity is an itype created as a subtype of an access
14207 -- type with a null exclusion restore source entity for proper
14208 -- visibility. The itype will be created anew in the instance.
14209
14210 if Is_Itype (E)
14211 and then Ekind (E) = E_Access_Subtype
14212 and then Is_Entity_Name (N)
14213 and then Chars (Etype (E)) = Chars (N)
14214 then
14215 E := Etype (E);
14216 Set_Entity (N2, E);
14217 Set_Etype (N2, E);
14218 end if;
14219
14220 if Is_Global (E) then
14221
14222 -- If the entity is a package renaming that is the prefix of
14223 -- an expanded name, it has been rewritten as the renamed
14224 -- package, which is necessary semantically but complicates
14225 -- ASIS tree traversal, so we recover the original entity to
14226 -- expose the renaming. Take into account that the context may
14227 -- be a nested generic, that the original node may itself have
14228 -- an associated node that had better be an entity, and that
14229 -- the current node is still a selected component.
14230
14231 if Ekind (E) = E_Package
14232 and then Nkind (N) = N_Selected_Component
14233 and then Nkind (Parent (N)) = N_Expanded_Name
14234 and then Present (Original_Node (N2))
14235 and then Is_Entity_Name (Original_Node (N2))
14236 and then Present (Entity (Original_Node (N2)))
14237 then
14238 if Is_Global (Entity (Original_Node (N2))) then
14239 N2 := Original_Node (N2);
14240 Set_Associated_Node (N, N2);
14241 Set_Global_Type (N, N2);
14242
14243 -- Renaming is local, and will be resolved in instance
14244
14245 else
14246 Set_Associated_Node (N, Empty);
14247 Set_Etype (N, Empty);
14248 end if;
14249
14250 else
14251 Set_Global_Type (N, N2);
14252 end if;
14253
14254 elsif Nkind (N) = N_Op_Concat
14255 and then Is_Generic_Type (Etype (N2))
14256 and then (Base_Type (Etype (Right_Opnd (N2))) = Etype (N2)
14257 or else
14258 Base_Type (Etype (Left_Opnd (N2))) = Etype (N2))
14259 and then Is_Intrinsic_Subprogram (E)
14260 then
14261 null;
14262
14263 -- Entity is local. Mark generic node as unresolved. Note that now
14264 -- it does not have an entity.
14265
14266 else
14267 Set_Associated_Node (N, Empty);
14268 Set_Etype (N, Empty);
14269 end if;
14270
14271 if Nkind (Parent (N)) in N_Generic_Instantiation
14272 and then N = Name (Parent (N))
14273 then
14274 Save_Global_Defaults (Parent (N), Parent (N2));
14275 end if;
14276
14277 elsif Nkind (Parent (N)) = N_Selected_Component
14278 and then Nkind (Parent (N2)) = N_Expanded_Name
14279 then
14280 if Is_Global (Entity (Parent (N2))) then
14281 Change_Selected_Component_To_Expanded_Name (Parent (N));
14282 Set_Associated_Node (Parent (N), Parent (N2));
14283 Set_Global_Type (Parent (N), Parent (N2));
14284 Save_Entity_Descendants (N);
14285
14286 -- If this is a reference to the current generic entity, replace
14287 -- by the name of the generic homonym of the current package. This
14288 -- is because in an instantiation Par.P.Q will not resolve to the
14289 -- name of the instance, whose enclosing scope is not necessarily
14290 -- Par. We use the generic homonym rather that the name of the
14291 -- generic itself because it may be hidden by a local declaration.
14292
14293 elsif In_Open_Scopes (Entity (Parent (N2)))
14294 and then not
14295 Is_Generic_Unit (Top_Ancestor (Entity (Prefix (Parent (N2)))))
14296 then
14297 if Ekind (Entity (Parent (N2))) = E_Generic_Package then
14298 Rewrite (Parent (N),
14299 Make_Identifier (Sloc (N),
14300 Chars =>
14301 Chars (Generic_Homonym (Entity (Parent (N2))))));
14302 else
14303 Rewrite (Parent (N),
14304 Make_Identifier (Sloc (N),
14305 Chars => Chars (Selector_Name (Parent (N2)))));
14306 end if;
14307 end if;
14308
14309 if Nkind (Parent (Parent (N))) in N_Generic_Instantiation
14310 and then Parent (N) = Name (Parent (Parent (N)))
14311 then
14312 Save_Global_Defaults
14313 (Parent (Parent (N)), Parent (Parent (N2)));
14314 end if;
14315
14316 -- A selected component may denote a static constant that has been
14317 -- folded. If the static constant is global to the generic, capture
14318 -- its value. Otherwise the folding will happen in any instantiation.
14319
14320 elsif Nkind (Parent (N)) = N_Selected_Component
14321 and then Nkind_In (Parent (N2), N_Integer_Literal, N_Real_Literal)
14322 then
14323 if Present (Entity (Original_Node (Parent (N2))))
14324 and then Is_Global (Entity (Original_Node (Parent (N2))))
14325 then
14326 Rewrite (Parent (N), New_Copy (Parent (N2)));
14327 Set_Analyzed (Parent (N), False);
14328 end if;
14329
14330 -- A selected component may be transformed into a parameterless
14331 -- function call. If the called entity is global, rewrite the node
14332 -- appropriately, i.e. as an extended name for the global entity.
14333
14334 elsif Nkind (Parent (N)) = N_Selected_Component
14335 and then Nkind (Parent (N2)) = N_Function_Call
14336 and then N = Selector_Name (Parent (N))
14337 then
14338 if No (Parameter_Associations (Parent (N2))) then
14339 if Is_Global (Entity (Name (Parent (N2)))) then
14340 Change_Selected_Component_To_Expanded_Name (Parent (N));
14341 Set_Associated_Node (Parent (N), Name (Parent (N2)));
14342 Set_Global_Type (Parent (N), Name (Parent (N2)));
14343 Save_Entity_Descendants (N);
14344
14345 else
14346 Set_Is_Prefixed_Call (Parent (N));
14347 Set_Associated_Node (N, Empty);
14348 Set_Etype (N, Empty);
14349 end if;
14350
14351 -- In Ada 2005, X.F may be a call to a primitive operation,
14352 -- rewritten as F (X). This rewriting will be done again in an
14353 -- instance, so keep the original node. Global entities will be
14354 -- captured as for other constructs. Indicate that this must
14355 -- resolve as a call, to prevent accidental overloading in the
14356 -- instance, if both a component and a primitive operation appear
14357 -- as candidates.
14358
14359 else
14360 Set_Is_Prefixed_Call (Parent (N));
14361 end if;
14362
14363 -- Entity is local. Reset in generic unit, so that node is resolved
14364 -- anew at the point of instantiation.
14365
14366 else
14367 Set_Associated_Node (N, Empty);
14368 Set_Etype (N, Empty);
14369 end if;
14370 end Reset_Entity;
14371
14372 -----------------------------
14373 -- Save_Entity_Descendants --
14374 -----------------------------
14375
14376 procedure Save_Entity_Descendants (N : Node_Id) is
14377 begin
14378 case Nkind (N) is
14379 when N_Binary_Op =>
14380 Save_Global_Descendant (Union_Id (Left_Opnd (N)));
14381 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
14382
14383 when N_Unary_Op =>
14384 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
14385
14386 when N_Expanded_Name |
14387 N_Selected_Component =>
14388 Save_Global_Descendant (Union_Id (Prefix (N)));
14389 Save_Global_Descendant (Union_Id (Selector_Name (N)));
14390
14391 when N_Identifier |
14392 N_Character_Literal |
14393 N_Operator_Symbol =>
14394 null;
14395
14396 when others =>
14397 raise Program_Error;
14398 end case;
14399 end Save_Entity_Descendants;
14400
14401 --------------------------
14402 -- Save_Global_Defaults --
14403 --------------------------
14404
14405 procedure Save_Global_Defaults (N1 : Node_Id; N2 : Node_Id) is
14406 Loc : constant Source_Ptr := Sloc (N1);
14407 Assoc2 : constant List_Id := Generic_Associations (N2);
14408 Gen_Id : constant Entity_Id := Get_Generic_Entity (N2);
14409 Assoc1 : List_Id;
14410 Act1 : Node_Id;
14411 Act2 : Node_Id;
14412 Def : Node_Id;
14413 Ndec : Node_Id;
14414 Subp : Entity_Id;
14415 Actual : Entity_Id;
14416
14417 begin
14418 Assoc1 := Generic_Associations (N1);
14419
14420 if Present (Assoc1) then
14421 Act1 := First (Assoc1);
14422 else
14423 Act1 := Empty;
14424 Set_Generic_Associations (N1, New_List);
14425 Assoc1 := Generic_Associations (N1);
14426 end if;
14427
14428 if Present (Assoc2) then
14429 Act2 := First (Assoc2);
14430 else
14431 return;
14432 end if;
14433
14434 while Present (Act1) and then Present (Act2) loop
14435 Next (Act1);
14436 Next (Act2);
14437 end loop;
14438
14439 -- Find the associations added for default subprograms
14440
14441 if Present (Act2) then
14442 while Nkind (Act2) /= N_Generic_Association
14443 or else No (Entity (Selector_Name (Act2)))
14444 or else not Is_Overloadable (Entity (Selector_Name (Act2)))
14445 loop
14446 Next (Act2);
14447 end loop;
14448
14449 -- Add a similar association if the default is global. The
14450 -- renaming declaration for the actual has been analyzed, and
14451 -- its alias is the program it renames. Link the actual in the
14452 -- original generic tree with the node in the analyzed tree.
14453
14454 while Present (Act2) loop
14455 Subp := Entity (Selector_Name (Act2));
14456 Def := Explicit_Generic_Actual_Parameter (Act2);
14457
14458 -- Following test is defence against rubbish errors
14459
14460 if No (Alias (Subp)) then
14461 return;
14462 end if;
14463
14464 -- Retrieve the resolved actual from the renaming declaration
14465 -- created for the instantiated formal.
14466
14467 Actual := Entity (Name (Parent (Parent (Subp))));
14468 Set_Entity (Def, Actual);
14469 Set_Etype (Def, Etype (Actual));
14470
14471 if Is_Global (Actual) then
14472 Ndec :=
14473 Make_Generic_Association (Loc,
14474 Selector_Name =>
14475 New_Occurrence_Of (Subp, Loc),
14476 Explicit_Generic_Actual_Parameter =>
14477 New_Occurrence_Of (Actual, Loc));
14478
14479 Set_Associated_Node
14480 (Explicit_Generic_Actual_Parameter (Ndec), Def);
14481
14482 Append (Ndec, Assoc1);
14483
14484 -- If there are other defaults, add a dummy association in case
14485 -- there are other defaulted formals with the same name.
14486
14487 elsif Present (Next (Act2)) then
14488 Ndec :=
14489 Make_Generic_Association (Loc,
14490 Selector_Name =>
14491 New_Occurrence_Of (Subp, Loc),
14492 Explicit_Generic_Actual_Parameter => Empty);
14493
14494 Append (Ndec, Assoc1);
14495 end if;
14496
14497 Next (Act2);
14498 end loop;
14499 end if;
14500
14501 if Nkind (Name (N1)) = N_Identifier
14502 and then Is_Child_Unit (Gen_Id)
14503 and then Is_Global (Gen_Id)
14504 and then Is_Generic_Unit (Scope (Gen_Id))
14505 and then In_Open_Scopes (Scope (Gen_Id))
14506 then
14507 -- This is an instantiation of a child unit within a sibling, so
14508 -- that the generic parent is in scope. An eventual instance must
14509 -- occur within the scope of an instance of the parent. Make name
14510 -- in instance into an expanded name, to preserve the identifier
14511 -- of the parent, so it can be resolved subsequently.
14512
14513 Rewrite (Name (N2),
14514 Make_Expanded_Name (Loc,
14515 Chars => Chars (Gen_Id),
14516 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
14517 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
14518 Set_Entity (Name (N2), Gen_Id);
14519
14520 Rewrite (Name (N1),
14521 Make_Expanded_Name (Loc,
14522 Chars => Chars (Gen_Id),
14523 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
14524 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
14525
14526 Set_Associated_Node (Name (N1), Name (N2));
14527 Set_Associated_Node (Prefix (Name (N1)), Empty);
14528 Set_Associated_Node
14529 (Selector_Name (Name (N1)), Selector_Name (Name (N2)));
14530 Set_Etype (Name (N1), Etype (Gen_Id));
14531 end if;
14532 end Save_Global_Defaults;
14533
14534 ----------------------------
14535 -- Save_Global_Descendant --
14536 ----------------------------
14537
14538 procedure Save_Global_Descendant (D : Union_Id) is
14539 N1 : Node_Id;
14540
14541 begin
14542 if D in Node_Range then
14543 if D = Union_Id (Empty) then
14544 null;
14545
14546 elsif Nkind (Node_Id (D)) /= N_Compilation_Unit then
14547 Save_References (Node_Id (D));
14548 end if;
14549
14550 elsif D in List_Range then
14551 if D = Union_Id (No_List) or else Is_Empty_List (List_Id (D)) then
14552 null;
14553
14554 else
14555 N1 := First (List_Id (D));
14556 while Present (N1) loop
14557 Save_References (N1);
14558 Next (N1);
14559 end loop;
14560 end if;
14561
14562 -- Element list or other non-node field, nothing to do
14563
14564 else
14565 null;
14566 end if;
14567 end Save_Global_Descendant;
14568
14569 ---------------------
14570 -- Save_References --
14571 ---------------------
14572
14573 -- This is the recursive procedure that does the work once the enclosing
14574 -- generic scope has been established. We have to treat specially a
14575 -- number of node rewritings that are required by semantic processing
14576 -- and which change the kind of nodes in the generic copy: typically
14577 -- constant-folding, replacing an operator node by a string literal, or
14578 -- a selected component by an expanded name. In each of those cases, the
14579 -- transformation is propagated to the generic unit.
14580
14581 procedure Save_References (N : Node_Id) is
14582 Loc : constant Source_Ptr := Sloc (N);
14583
14584 function Requires_Delayed_Save (Nod : Node_Id) return Boolean;
14585 -- Determine whether arbitrary node Nod requires delayed capture of
14586 -- global references within its aspect specifications.
14587
14588 procedure Save_References_In_Aggregate (N : Node_Id);
14589 -- Save all global references in [extension] aggregate node N
14590
14591 procedure Save_References_In_Char_Lit_Or_Op_Symbol (N : Node_Id);
14592 -- Save all global references in a character literal or operator
14593 -- symbol denoted by N.
14594
14595 procedure Save_References_In_Descendants (N : Node_Id);
14596 -- Save all global references in all descendants of node N
14597
14598 procedure Save_References_In_Identifier (N : Node_Id);
14599 -- Save all global references in identifier node N
14600
14601 procedure Save_References_In_Operator (N : Node_Id);
14602 -- Save all global references in operator node N
14603
14604 procedure Save_References_In_Pragma (Prag : Node_Id);
14605 -- Save all global references found within the expression of pragma
14606 -- Prag.
14607
14608 ---------------------------
14609 -- Requires_Delayed_Save --
14610 ---------------------------
14611
14612 function Requires_Delayed_Save (Nod : Node_Id) return Boolean is
14613 begin
14614 -- Generic packages and subprograms require delayed capture of
14615 -- global references within their aspects due to the timing of
14616 -- annotation analysis.
14617
14618 if Nkind_In (Nod, N_Generic_Package_Declaration,
14619 N_Generic_Subprogram_Declaration,
14620 N_Package_Body,
14621 N_Package_Body_Stub,
14622 N_Subprogram_Body,
14623 N_Subprogram_Body_Stub)
14624 then
14625 -- Since the capture of global references is done on the
14626 -- unanalyzed generic template, there is no information around
14627 -- to infer the context. Use the Associated_Entity linkages to
14628 -- peek into the analyzed generic copy and determine what the
14629 -- template corresponds to.
14630
14631 if Nod = Templ then
14632 return
14633 Is_Generic_Declaration_Or_Body
14634 (Unit_Declaration_Node
14635 (Associated_Entity (Defining_Entity (Nod))));
14636
14637 -- Otherwise the generic unit being processed is not the top
14638 -- level template. It is safe to capture of global references
14639 -- within the generic unit because at this point the top level
14640 -- copy is fully analyzed.
14641
14642 else
14643 return False;
14644 end if;
14645
14646 -- Otherwise capture the global references without interference
14647
14648 else
14649 return False;
14650 end if;
14651 end Requires_Delayed_Save;
14652
14653 ----------------------------------
14654 -- Save_References_In_Aggregate --
14655 ----------------------------------
14656
14657 procedure Save_References_In_Aggregate (N : Node_Id) is
14658 Nam : Node_Id;
14659 Qual : Node_Id := Empty;
14660 Typ : Entity_Id := Empty;
14661
14662 use Atree.Unchecked_Access;
14663 -- This code section is part of implementing an untyped tree
14664 -- traversal, so it needs direct access to node fields.
14665
14666 begin
14667 N2 := Get_Associated_Node (N);
14668
14669 if Present (N2) then
14670 Typ := Etype (N2);
14671
14672 -- In an instance within a generic, use the name of the actual
14673 -- and not the original generic parameter. If the actual is
14674 -- global in the current generic it must be preserved for its
14675 -- instantiation.
14676
14677 if Nkind (Parent (Typ)) = N_Subtype_Declaration
14678 and then Present (Generic_Parent_Type (Parent (Typ)))
14679 then
14680 Typ := Base_Type (Typ);
14681 Set_Etype (N2, Typ);
14682 end if;
14683 end if;
14684
14685 if No (N2) or else No (Typ) or else not Is_Global (Typ) then
14686 Set_Associated_Node (N, Empty);
14687
14688 -- If the aggregate is an actual in a call, it has been
14689 -- resolved in the current context, to some local type. The
14690 -- enclosing call may have been disambiguated by the aggregate,
14691 -- and this disambiguation might fail at instantiation time
14692 -- because the type to which the aggregate did resolve is not
14693 -- preserved. In order to preserve some of this information,
14694 -- wrap the aggregate in a qualified expression, using the id
14695 -- of its type. For further disambiguation we qualify the type
14696 -- name with its scope (if visible) because both id's will have
14697 -- corresponding entities in an instance. This resolves most of
14698 -- the problems with missing type information on aggregates in
14699 -- instances.
14700
14701 if Present (N2)
14702 and then Nkind (N2) = Nkind (N)
14703 and then Nkind (Parent (N2)) in N_Subprogram_Call
14704 and then Present (Typ)
14705 and then Comes_From_Source (Typ)
14706 then
14707 Nam := Make_Identifier (Loc, Chars (Typ));
14708
14709 if Is_Immediately_Visible (Scope (Typ)) then
14710 Nam :=
14711 Make_Selected_Component (Loc,
14712 Prefix =>
14713 Make_Identifier (Loc, Chars (Scope (Typ))),
14714 Selector_Name => Nam);
14715 end if;
14716
14717 Qual :=
14718 Make_Qualified_Expression (Loc,
14719 Subtype_Mark => Nam,
14720 Expression => Relocate_Node (N));
14721 end if;
14722 end if;
14723
14724 Save_Global_Descendant (Field1 (N));
14725 Save_Global_Descendant (Field2 (N));
14726 Save_Global_Descendant (Field3 (N));
14727 Save_Global_Descendant (Field5 (N));
14728
14729 if Present (Qual) then
14730 Rewrite (N, Qual);
14731 end if;
14732 end Save_References_In_Aggregate;
14733
14734 ----------------------------------------------
14735 -- Save_References_In_Char_Lit_Or_Op_Symbol --
14736 ----------------------------------------------
14737
14738 procedure Save_References_In_Char_Lit_Or_Op_Symbol (N : Node_Id) is
14739 begin
14740 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
14741 Reset_Entity (N);
14742
14743 elsif Nkind (N) = N_Operator_Symbol
14744 and then Nkind (Get_Associated_Node (N)) = N_String_Literal
14745 then
14746 Change_Operator_Symbol_To_String_Literal (N);
14747 end if;
14748 end Save_References_In_Char_Lit_Or_Op_Symbol;
14749
14750 ------------------------------------
14751 -- Save_References_In_Descendants --
14752 ------------------------------------
14753
14754 procedure Save_References_In_Descendants (N : Node_Id) is
14755 use Atree.Unchecked_Access;
14756 -- This code section is part of implementing an untyped tree
14757 -- traversal, so it needs direct access to node fields.
14758
14759 begin
14760 Save_Global_Descendant (Field1 (N));
14761 Save_Global_Descendant (Field2 (N));
14762 Save_Global_Descendant (Field3 (N));
14763 Save_Global_Descendant (Field4 (N));
14764 Save_Global_Descendant (Field5 (N));
14765 end Save_References_In_Descendants;
14766
14767 -----------------------------------
14768 -- Save_References_In_Identifier --
14769 -----------------------------------
14770
14771 procedure Save_References_In_Identifier (N : Node_Id) is
14772 begin
14773 -- The node did not undergo a transformation
14774
14775 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
14776
14777 -- If this is a discriminant reference, always save it. It is
14778 -- used in the instance to find the corresponding discriminant
14779 -- positionally rather than by name.
14780
14781 Set_Original_Discriminant
14782 (N, Original_Discriminant (Get_Associated_Node (N)));
14783 Reset_Entity (N);
14784
14785 -- The analysis of the generic copy transformed the identifier
14786 -- into another construct. Propagate the changes to the template.
14787
14788 else
14789 N2 := Get_Associated_Node (N);
14790
14791 -- The identifier denotes a call to a parameterless function.
14792 -- Mark the node as resolved when the function is external.
14793
14794 if Nkind (N2) = N_Function_Call then
14795 E := Entity (Name (N2));
14796
14797 if Present (E) and then Is_Global (E) then
14798 Set_Etype (N, Etype (N2));
14799 else
14800 Set_Associated_Node (N, Empty);
14801 Set_Etype (N, Empty);
14802 end if;
14803
14804 -- The identifier denotes a named number that was constant
14805 -- folded. Preserve the original name for ASIS and undo the
14806 -- constant folding which will be repeated in the instance.
14807
14808 elsif Nkind_In (N2, N_Integer_Literal, N_Real_Literal)
14809 and then Is_Entity_Name (Original_Node (N2))
14810 then
14811 Set_Associated_Node (N, Original_Node (N2));
14812 Reset_Entity (N);
14813
14814 -- The identifier resolved to a string literal. Propagate this
14815 -- information to the generic template.
14816
14817 elsif Nkind (N2) = N_String_Literal then
14818 Rewrite (N, New_Copy (N2));
14819
14820 -- The identifier is rewritten as a dereference if it is the
14821 -- prefix of an implicit dereference. Preserve the original
14822 -- tree as the analysis of the instance will expand the node
14823 -- again, but preserve the resolved entity if it is global.
14824
14825 elsif Nkind (N2) = N_Explicit_Dereference then
14826 if Is_Entity_Name (Prefix (N2))
14827 and then Present (Entity (Prefix (N2)))
14828 and then Is_Global (Entity (Prefix (N2)))
14829 then
14830 Set_Associated_Node (N, Prefix (N2));
14831
14832 elsif Nkind (Prefix (N2)) = N_Function_Call
14833 and then Present (Entity (Name (Prefix (N2))))
14834 and then Is_Global (Entity (Name (Prefix (N2))))
14835 then
14836 Rewrite (N,
14837 Make_Explicit_Dereference (Loc,
14838 Prefix =>
14839 Make_Function_Call (Loc,
14840 Name =>
14841 New_Occurrence_Of
14842 (Entity (Name (Prefix (N2))), Loc))));
14843
14844 else
14845 Set_Associated_Node (N, Empty);
14846 Set_Etype (N, Empty);
14847 end if;
14848
14849 -- The subtype mark of a nominally unconstrained object is
14850 -- rewritten as a subtype indication using the bounds of the
14851 -- expression. Recover the original subtype mark.
14852
14853 elsif Nkind (N2) = N_Subtype_Indication
14854 and then Is_Entity_Name (Original_Node (N2))
14855 then
14856 Set_Associated_Node (N, Original_Node (N2));
14857 Reset_Entity (N);
14858 end if;
14859 end if;
14860 end Save_References_In_Identifier;
14861
14862 ---------------------------------
14863 -- Save_References_In_Operator --
14864 ---------------------------------
14865
14866 procedure Save_References_In_Operator (N : Node_Id) is
14867 begin
14868 -- The node did not undergo a transformation
14869
14870 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
14871 if Nkind (N) = N_Op_Concat then
14872 Set_Is_Component_Left_Opnd (N,
14873 Is_Component_Left_Opnd (Get_Associated_Node (N)));
14874
14875 Set_Is_Component_Right_Opnd (N,
14876 Is_Component_Right_Opnd (Get_Associated_Node (N)));
14877 end if;
14878
14879 Reset_Entity (N);
14880
14881 -- The analysis of the generic copy transformed the operator into
14882 -- some other construct. Propagate the changes to the template if
14883 -- applicable.
14884
14885 else
14886 N2 := Get_Associated_Node (N);
14887
14888 -- The operator resoved to a function call
14889
14890 if Nkind (N2) = N_Function_Call then
14891
14892 -- Add explicit qualifications in the generic template for
14893 -- all operands of universal type. This aids resolution by
14894 -- preserving the actual type of a literal or an attribute
14895 -- that yields a universal result.
14896
14897 Qualify_Universal_Operands (N, N2);
14898
14899 E := Entity (Name (N2));
14900
14901 if Present (E) and then Is_Global (E) then
14902 Set_Etype (N, Etype (N2));
14903 else
14904 Set_Associated_Node (N, Empty);
14905 Set_Etype (N, Empty);
14906 end if;
14907
14908 -- The operator was folded into a literal
14909
14910 elsif Nkind_In (N2, N_Integer_Literal,
14911 N_Real_Literal,
14912 N_String_Literal)
14913 then
14914 if Present (Original_Node (N2))
14915 and then Nkind (Original_Node (N2)) = Nkind (N)
14916 then
14917 -- Operation was constant-folded. Whenever possible,
14918 -- recover semantic information from unfolded node,
14919 -- for ASIS use.
14920
14921 Set_Associated_Node (N, Original_Node (N2));
14922
14923 if Nkind (N) = N_Op_Concat then
14924 Set_Is_Component_Left_Opnd (N,
14925 Is_Component_Left_Opnd (Get_Associated_Node (N)));
14926 Set_Is_Component_Right_Opnd (N,
14927 Is_Component_Right_Opnd (Get_Associated_Node (N)));
14928 end if;
14929
14930 Reset_Entity (N);
14931
14932 -- Propagate the constant folding back to the template
14933
14934 else
14935 Rewrite (N, New_Copy (N2));
14936 Set_Analyzed (N, False);
14937 end if;
14938
14939 -- The operator was folded into an enumeration literal. Retain
14940 -- the entity to avoid spurious ambiguities if it is overloaded
14941 -- at the point of instantiation or inlining.
14942
14943 elsif Nkind (N2) = N_Identifier
14944 and then Ekind (Entity (N2)) = E_Enumeration_Literal
14945 then
14946 Rewrite (N, New_Copy (N2));
14947 Set_Analyzed (N, False);
14948 end if;
14949 end if;
14950
14951 -- Complete the operands check if node has not been constant
14952 -- folded.
14953
14954 if Nkind (N) in N_Op then
14955 Save_Entity_Descendants (N);
14956 end if;
14957 end Save_References_In_Operator;
14958
14959 -------------------------------
14960 -- Save_References_In_Pragma --
14961 -------------------------------
14962
14963 procedure Save_References_In_Pragma (Prag : Node_Id) is
14964 Context : Node_Id;
14965 Do_Save : Boolean := True;
14966
14967 use Atree.Unchecked_Access;
14968 -- This code section is part of implementing an untyped tree
14969 -- traversal, so it needs direct access to node fields.
14970
14971 begin
14972 -- Do not save global references in pragmas generated from aspects
14973 -- because the pragmas will be regenerated at instantiation time.
14974
14975 if From_Aspect_Specification (Prag) then
14976 Do_Save := False;
14977
14978 -- The capture of global references within contract-related source
14979 -- pragmas associated with generic packages, subprograms or their
14980 -- respective bodies must be delayed due to timing of annotation
14981 -- analysis. Global references are still captured in routine
14982 -- Save_Global_References_In_Contract.
14983
14984 elsif Is_Generic_Contract_Pragma (Prag) and then Prag /= Templ then
14985 if Is_Package_Contract_Annotation (Prag) then
14986 Context := Find_Related_Package_Or_Body (Prag);
14987 else
14988 pragma Assert (Is_Subprogram_Contract_Annotation (Prag));
14989 Context := Find_Related_Declaration_Or_Body (Prag);
14990 end if;
14991
14992 -- The use of Original_Node accounts for the case when the
14993 -- related context is generic template.
14994
14995 if Requires_Delayed_Save (Original_Node (Context)) then
14996 Do_Save := False;
14997 end if;
14998 end if;
14999
15000 -- For all other cases, save all global references within the
15001 -- descendants, but skip the following semantic fields:
15002
15003 -- Field1 - Next_Pragma
15004 -- Field3 - Corresponding_Aspect
15005 -- Field5 - Next_Rep_Item
15006
15007 if Do_Save then
15008 Save_Global_Descendant (Field2 (Prag));
15009 Save_Global_Descendant (Field4 (Prag));
15010 end if;
15011 end Save_References_In_Pragma;
15012
15013 -- Start of processing for Save_References
15014
15015 begin
15016 if N = Empty then
15017 null;
15018
15019 -- Aggregates
15020
15021 elsif Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
15022 Save_References_In_Aggregate (N);
15023
15024 -- Character literals, operator symbols
15025
15026 elsif Nkind_In (N, N_Character_Literal, N_Operator_Symbol) then
15027 Save_References_In_Char_Lit_Or_Op_Symbol (N);
15028
15029 -- Defining identifiers
15030
15031 elsif Nkind (N) in N_Entity then
15032 null;
15033
15034 -- Identifiers
15035
15036 elsif Nkind (N) = N_Identifier then
15037 Save_References_In_Identifier (N);
15038
15039 -- Operators
15040
15041 elsif Nkind (N) in N_Op then
15042 Save_References_In_Operator (N);
15043
15044 -- Pragmas
15045
15046 elsif Nkind (N) = N_Pragma then
15047 Save_References_In_Pragma (N);
15048
15049 else
15050 Save_References_In_Descendants (N);
15051 end if;
15052
15053 -- Save all global references found within the aspect specifications
15054 -- of the related node.
15055
15056 if Permits_Aspect_Specifications (N) and then Has_Aspects (N) then
15057
15058 -- The capture of global references within aspects associated with
15059 -- generic packages, subprograms or their bodies must be delayed
15060 -- due to timing of annotation analysis. Global references are
15061 -- still captured in routine Save_Global_References_In_Contract.
15062
15063 if Requires_Delayed_Save (N) then
15064 null;
15065
15066 -- Otherwise save all global references within the aspects
15067
15068 else
15069 Save_Global_References_In_Aspects (N);
15070 end if;
15071 end if;
15072 end Save_References;
15073
15074 -- Start of processing for Save_Global_References
15075
15076 begin
15077 Gen_Scope := Current_Scope;
15078
15079 -- If the generic unit is a child unit, references to entities in the
15080 -- parent are treated as local, because they will be resolved anew in
15081 -- the context of the instance of the parent.
15082
15083 while Is_Child_Unit (Gen_Scope)
15084 and then Ekind (Scope (Gen_Scope)) = E_Generic_Package
15085 loop
15086 Gen_Scope := Scope (Gen_Scope);
15087 end loop;
15088
15089 Save_References (Templ);
15090 end Save_Global_References;
15091
15092 ---------------------------------------
15093 -- Save_Global_References_In_Aspects --
15094 ---------------------------------------
15095
15096 procedure Save_Global_References_In_Aspects (N : Node_Id) is
15097 Asp : Node_Id;
15098 Expr : Node_Id;
15099
15100 begin
15101 Asp := First (Aspect_Specifications (N));
15102 while Present (Asp) loop
15103 Expr := Expression (Asp);
15104
15105 if Present (Expr) then
15106 Save_Global_References (Expr);
15107 end if;
15108
15109 Next (Asp);
15110 end loop;
15111 end Save_Global_References_In_Aspects;
15112
15113 --------------------------------------
15114 -- Set_Copied_Sloc_For_Inlined_Body --
15115 --------------------------------------
15116
15117 procedure Set_Copied_Sloc_For_Inlined_Body (N : Node_Id; E : Entity_Id) is
15118 begin
15119 Create_Instantiation_Source (N, E, True, S_Adjustment);
15120 end Set_Copied_Sloc_For_Inlined_Body;
15121
15122 ---------------------
15123 -- Set_Instance_Of --
15124 ---------------------
15125
15126 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id) is
15127 begin
15128 Generic_Renamings.Table (Generic_Renamings.Last) := (A, B, Assoc_Null);
15129 Generic_Renamings_HTable.Set (Generic_Renamings.Last);
15130 Generic_Renamings.Increment_Last;
15131 end Set_Instance_Of;
15132
15133 --------------------
15134 -- Set_Next_Assoc --
15135 --------------------
15136
15137 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr) is
15138 begin
15139 Generic_Renamings.Table (E).Next_In_HTable := Next;
15140 end Set_Next_Assoc;
15141
15142 -------------------
15143 -- Start_Generic --
15144 -------------------
15145
15146 procedure Start_Generic is
15147 begin
15148 -- ??? More things could be factored out in this routine.
15149 -- Should probably be done at a later stage.
15150
15151 Generic_Flags.Append (Inside_A_Generic);
15152 Inside_A_Generic := True;
15153
15154 Expander_Mode_Save_And_Set (False);
15155 end Start_Generic;
15156
15157 ----------------------
15158 -- Set_Instance_Env --
15159 ----------------------
15160
15161 procedure Set_Instance_Env
15162 (Gen_Unit : Entity_Id;
15163 Act_Unit : Entity_Id)
15164 is
15165 Assertion_Status : constant Boolean := Assertions_Enabled;
15166 Save_SPARK_Mode : constant SPARK_Mode_Type := SPARK_Mode;
15167 Save_SPARK_Mode_Pragma : constant Node_Id := SPARK_Mode_Pragma;
15168
15169 begin
15170 -- Regardless of the current mode, predefined units are analyzed in the
15171 -- most current Ada mode, and earlier version Ada checks do not apply
15172 -- to predefined units. Nothing needs to be done for non-internal units.
15173 -- These are always analyzed in the current mode.
15174
15175 if Is_Internal_File_Name
15176 (Fname => Unit_File_Name (Get_Source_Unit (Gen_Unit)),
15177 Renamings_Included => True)
15178 then
15179 Set_Opt_Config_Switches (True, Current_Sem_Unit = Main_Unit);
15180
15181 -- In Ada2012 we may want to enable assertions in an instance of a
15182 -- predefined unit, in which case we need to preserve the current
15183 -- setting for the Assertions_Enabled flag. This will become more
15184 -- critical when pre/postconditions are added to predefined units,
15185 -- as is already the case for some numeric libraries.
15186
15187 if Ada_Version >= Ada_2012 then
15188 Assertions_Enabled := Assertion_Status;
15189 end if;
15190
15191 -- SPARK_Mode for an instance is the one applicable at the point of
15192 -- instantiation.
15193
15194 SPARK_Mode := Save_SPARK_Mode;
15195 SPARK_Mode_Pragma := Save_SPARK_Mode_Pragma;
15196
15197 -- Make sure dynamic elaboration checks are off in SPARK Mode
15198
15199 if SPARK_Mode = On then
15200 Dynamic_Elaboration_Checks := False;
15201 end if;
15202 end if;
15203
15204 Current_Instantiated_Parent :=
15205 (Gen_Id => Gen_Unit,
15206 Act_Id => Act_Unit,
15207 Next_In_HTable => Assoc_Null);
15208 end Set_Instance_Env;
15209
15210 -----------------
15211 -- Switch_View --
15212 -----------------
15213
15214 procedure Switch_View (T : Entity_Id) is
15215 BT : constant Entity_Id := Base_Type (T);
15216 Priv_Elmt : Elmt_Id := No_Elmt;
15217 Priv_Sub : Entity_Id;
15218
15219 begin
15220 -- T may be private but its base type may have been exchanged through
15221 -- some other occurrence, in which case there is nothing to switch
15222 -- besides T itself. Note that a private dependent subtype of a private
15223 -- type might not have been switched even if the base type has been,
15224 -- because of the last branch of Check_Private_View (see comment there).
15225
15226 if not Is_Private_Type (BT) then
15227 Prepend_Elmt (Full_View (T), Exchanged_Views);
15228 Exchange_Declarations (T);
15229 return;
15230 end if;
15231
15232 Priv_Elmt := First_Elmt (Private_Dependents (BT));
15233
15234 if Present (Full_View (BT)) then
15235 Prepend_Elmt (Full_View (BT), Exchanged_Views);
15236 Exchange_Declarations (BT);
15237 end if;
15238
15239 while Present (Priv_Elmt) loop
15240 Priv_Sub := (Node (Priv_Elmt));
15241
15242 -- We avoid flipping the subtype if the Etype of its full view is
15243 -- private because this would result in a malformed subtype. This
15244 -- occurs when the Etype of the subtype full view is the full view of
15245 -- the base type (and since the base types were just switched, the
15246 -- subtype is pointing to the wrong view). This is currently the case
15247 -- for tagged record types, access types (maybe more?) and needs to
15248 -- be resolved. ???
15249
15250 if Present (Full_View (Priv_Sub))
15251 and then not Is_Private_Type (Etype (Full_View (Priv_Sub)))
15252 then
15253 Prepend_Elmt (Full_View (Priv_Sub), Exchanged_Views);
15254 Exchange_Declarations (Priv_Sub);
15255 end if;
15256
15257 Next_Elmt (Priv_Elmt);
15258 end loop;
15259 end Switch_View;
15260
15261 -----------------
15262 -- True_Parent --
15263 -----------------
15264
15265 function True_Parent (N : Node_Id) return Node_Id is
15266 begin
15267 if Nkind (Parent (N)) = N_Subunit then
15268 return Parent (Corresponding_Stub (Parent (N)));
15269 else
15270 return Parent (N);
15271 end if;
15272 end True_Parent;
15273
15274 -----------------------------
15275 -- Valid_Default_Attribute --
15276 -----------------------------
15277
15278 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id) is
15279 Attr_Id : constant Attribute_Id :=
15280 Get_Attribute_Id (Attribute_Name (Def));
15281 T : constant Entity_Id := Entity (Prefix (Def));
15282 Is_Fun : constant Boolean := (Ekind (Nam) = E_Function);
15283 F : Entity_Id;
15284 Num_F : Nat;
15285 OK : Boolean;
15286
15287 begin
15288 if No (T) or else T = Any_Id then
15289 return;
15290 end if;
15291
15292 Num_F := 0;
15293 F := First_Formal (Nam);
15294 while Present (F) loop
15295 Num_F := Num_F + 1;
15296 Next_Formal (F);
15297 end loop;
15298
15299 case Attr_Id is
15300 when Attribute_Adjacent | Attribute_Ceiling | Attribute_Copy_Sign |
15301 Attribute_Floor | Attribute_Fraction | Attribute_Machine |
15302 Attribute_Model | Attribute_Remainder | Attribute_Rounding |
15303 Attribute_Unbiased_Rounding =>
15304 OK := Is_Fun
15305 and then Num_F = 1
15306 and then Is_Floating_Point_Type (T);
15307
15308 when Attribute_Image | Attribute_Pred | Attribute_Succ |
15309 Attribute_Value | Attribute_Wide_Image |
15310 Attribute_Wide_Value =>
15311 OK := (Is_Fun and then Num_F = 1 and then Is_Scalar_Type (T));
15312
15313 when Attribute_Max | Attribute_Min =>
15314 OK := (Is_Fun and then Num_F = 2 and then Is_Scalar_Type (T));
15315
15316 when Attribute_Input =>
15317 OK := (Is_Fun and then Num_F = 1);
15318
15319 when Attribute_Output | Attribute_Read | Attribute_Write =>
15320 OK := (not Is_Fun and then Num_F = 2);
15321
15322 when others =>
15323 OK := False;
15324 end case;
15325
15326 if not OK then
15327 Error_Msg_N
15328 ("attribute reference has wrong profile for subprogram", Def);
15329 end if;
15330 end Valid_Default_Attribute;
15331
15332 end Sem_Ch12;