]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/ada/sem_ch12.adb
[multiple changes]
[thirdparty/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-2015, 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 -- Analyze_Associations --
1032 --------------------------
1033
1034 function Analyze_Associations
1035 (I_Node : Node_Id;
1036 Formals : List_Id;
1037 F_Copy : List_Id) return List_Id
1038 is
1039 Actuals_To_Freeze : constant Elist_Id := New_Elmt_List;
1040 Assoc : constant List_Id := New_List;
1041 Default_Actuals : constant List_Id := New_List;
1042 Gen_Unit : constant Entity_Id :=
1043 Defining_Entity (Parent (F_Copy));
1044
1045 Actuals : List_Id;
1046 Actual : Node_Id;
1047 Analyzed_Formal : Node_Id;
1048 First_Named : Node_Id := Empty;
1049 Formal : Node_Id;
1050 Match : Node_Id;
1051 Named : Node_Id;
1052 Saved_Formal : Node_Id;
1053
1054 Default_Formals : constant List_Id := New_List;
1055 -- If an Others_Choice is present, some of the formals may be defaulted.
1056 -- To simplify the treatment of visibility in an instance, we introduce
1057 -- individual defaults for each such formal. These defaults are
1058 -- appended to the list of associations and replace the Others_Choice.
1059
1060 Found_Assoc : Node_Id;
1061 -- Association for the current formal being match. Empty if there are
1062 -- no remaining actuals, or if there is no named association with the
1063 -- name of the formal.
1064
1065 Is_Named_Assoc : Boolean;
1066 Num_Matched : Int := 0;
1067 Num_Actuals : Int := 0;
1068
1069 Others_Present : Boolean := False;
1070 Others_Choice : Node_Id := Empty;
1071 -- In Ada 2005, indicates partial parameterization of a formal
1072 -- package. As usual an other association must be last in the list.
1073
1074 procedure Check_Overloaded_Formal_Subprogram (Formal : Entity_Id);
1075 -- Apply RM 12.3(9): if a formal subprogram is overloaded, the instance
1076 -- cannot have a named association for it. AI05-0025 extends this rule
1077 -- to formals of formal packages by AI05-0025, and it also applies to
1078 -- box-initialized formals.
1079
1080 function Has_Fully_Defined_Profile (Subp : Entity_Id) return Boolean;
1081 -- Determine whether the parameter types and the return type of Subp
1082 -- are fully defined at the point of instantiation.
1083
1084 function Matching_Actual
1085 (F : Entity_Id;
1086 A_F : Entity_Id) return Node_Id;
1087 -- Find actual that corresponds to a given a formal parameter. If the
1088 -- actuals are positional, return the next one, if any. If the actuals
1089 -- are named, scan the parameter associations to find the right one.
1090 -- A_F is the corresponding entity in the analyzed generic,which is
1091 -- placed on the selector name for ASIS use.
1092 --
1093 -- In Ada 2005, a named association may be given with a box, in which
1094 -- case Matching_Actual sets Found_Assoc to the generic association,
1095 -- but return Empty for the actual itself. In this case the code below
1096 -- creates a corresponding declaration for the formal.
1097
1098 function Partial_Parameterization return Boolean;
1099 -- Ada 2005: if no match is found for a given formal, check if the
1100 -- association for it includes a box, or whether the associations
1101 -- include an Others clause.
1102
1103 procedure Process_Default (F : Entity_Id);
1104 -- Add a copy of the declaration of generic formal F to the list of
1105 -- associations, and add an explicit box association for F if there
1106 -- is none yet, and the default comes from an Others_Choice.
1107
1108 function Renames_Standard_Subprogram (Subp : Entity_Id) return Boolean;
1109 -- Determine whether Subp renames one of the subprograms defined in the
1110 -- generated package Standard.
1111
1112 procedure Set_Analyzed_Formal;
1113 -- Find the node in the generic copy that corresponds to a given formal.
1114 -- The semantic information on this node is used to perform legality
1115 -- checks on the actuals. Because semantic analysis can introduce some
1116 -- anonymous entities or modify the declaration node itself, the
1117 -- correspondence between the two lists is not one-one. In addition to
1118 -- anonymous types, the presence a formal equality will introduce an
1119 -- implicit declaration for the corresponding inequality.
1120
1121 ----------------------------------------
1122 -- Check_Overloaded_Formal_Subprogram --
1123 ----------------------------------------
1124
1125 procedure Check_Overloaded_Formal_Subprogram (Formal : Entity_Id) is
1126 Temp_Formal : Entity_Id;
1127
1128 begin
1129 Temp_Formal := First (Formals);
1130 while Present (Temp_Formal) loop
1131 if Nkind (Temp_Formal) in N_Formal_Subprogram_Declaration
1132 and then Temp_Formal /= Formal
1133 and then
1134 Chars (Defining_Unit_Name (Specification (Formal))) =
1135 Chars (Defining_Unit_Name (Specification (Temp_Formal)))
1136 then
1137 if Present (Found_Assoc) then
1138 Error_Msg_N
1139 ("named association not allowed for overloaded formal",
1140 Found_Assoc);
1141
1142 else
1143 Error_Msg_N
1144 ("named association not allowed for overloaded formal",
1145 Others_Choice);
1146 end if;
1147
1148 Abandon_Instantiation (Instantiation_Node);
1149 end if;
1150
1151 Next (Temp_Formal);
1152 end loop;
1153 end Check_Overloaded_Formal_Subprogram;
1154
1155 -------------------------------
1156 -- Has_Fully_Defined_Profile --
1157 -------------------------------
1158
1159 function Has_Fully_Defined_Profile (Subp : Entity_Id) return Boolean is
1160 function Is_Fully_Defined_Type (Typ : Entity_Id) return Boolean;
1161 -- Determine whethet type Typ is fully defined
1162
1163 ---------------------------
1164 -- Is_Fully_Defined_Type --
1165 ---------------------------
1166
1167 function Is_Fully_Defined_Type (Typ : Entity_Id) return Boolean is
1168 begin
1169 -- A private type without a full view is not fully defined
1170
1171 if Is_Private_Type (Typ)
1172 and then No (Full_View (Typ))
1173 then
1174 return False;
1175
1176 -- An incomplete type is never fully defined
1177
1178 elsif Is_Incomplete_Type (Typ) then
1179 return False;
1180
1181 -- All other types are fully defined
1182
1183 else
1184 return True;
1185 end if;
1186 end Is_Fully_Defined_Type;
1187
1188 -- Local declarations
1189
1190 Param : Entity_Id;
1191
1192 -- Start of processing for Has_Fully_Defined_Profile
1193
1194 begin
1195 -- Check the parameters
1196
1197 Param := First_Formal (Subp);
1198 while Present (Param) loop
1199 if not Is_Fully_Defined_Type (Etype (Param)) then
1200 return False;
1201 end if;
1202
1203 Next_Formal (Param);
1204 end loop;
1205
1206 -- Check the return type
1207
1208 return Is_Fully_Defined_Type (Etype (Subp));
1209 end Has_Fully_Defined_Profile;
1210
1211 ---------------------
1212 -- Matching_Actual --
1213 ---------------------
1214
1215 function Matching_Actual
1216 (F : Entity_Id;
1217 A_F : Entity_Id) return Node_Id
1218 is
1219 Prev : Node_Id;
1220 Act : Node_Id;
1221
1222 begin
1223 Is_Named_Assoc := False;
1224
1225 -- End of list of purely positional parameters
1226
1227 if No (Actual) or else Nkind (Actual) = N_Others_Choice then
1228 Found_Assoc := Empty;
1229 Act := Empty;
1230
1231 -- Case of positional parameter corresponding to current formal
1232
1233 elsif No (Selector_Name (Actual)) then
1234 Found_Assoc := Actual;
1235 Act := Explicit_Generic_Actual_Parameter (Actual);
1236 Num_Matched := Num_Matched + 1;
1237 Next (Actual);
1238
1239 -- Otherwise scan list of named actuals to find the one with the
1240 -- desired name. All remaining actuals have explicit names.
1241
1242 else
1243 Is_Named_Assoc := True;
1244 Found_Assoc := Empty;
1245 Act := Empty;
1246 Prev := Empty;
1247
1248 while Present (Actual) loop
1249 if Chars (Selector_Name (Actual)) = Chars (F) then
1250 Set_Entity (Selector_Name (Actual), A_F);
1251 Set_Etype (Selector_Name (Actual), Etype (A_F));
1252 Generate_Reference (A_F, Selector_Name (Actual));
1253 Found_Assoc := Actual;
1254 Act := Explicit_Generic_Actual_Parameter (Actual);
1255 Num_Matched := Num_Matched + 1;
1256 exit;
1257 end if;
1258
1259 Prev := Actual;
1260 Next (Actual);
1261 end loop;
1262
1263 -- Reset for subsequent searches. In most cases the named
1264 -- associations are in order. If they are not, we reorder them
1265 -- to avoid scanning twice the same actual. This is not just a
1266 -- question of efficiency: there may be multiple defaults with
1267 -- boxes that have the same name. In a nested instantiation we
1268 -- insert actuals for those defaults, and cannot rely on their
1269 -- names to disambiguate them.
1270
1271 if Actual = First_Named then
1272 Next (First_Named);
1273
1274 elsif Present (Actual) then
1275 Insert_Before (First_Named, Remove_Next (Prev));
1276 end if;
1277
1278 Actual := First_Named;
1279 end if;
1280
1281 if Is_Entity_Name (Act) and then Present (Entity (Act)) then
1282 Set_Used_As_Generic_Actual (Entity (Act));
1283 end if;
1284
1285 return Act;
1286 end Matching_Actual;
1287
1288 ------------------------------
1289 -- Partial_Parameterization --
1290 ------------------------------
1291
1292 function Partial_Parameterization return Boolean is
1293 begin
1294 return Others_Present
1295 or else (Present (Found_Assoc) and then Box_Present (Found_Assoc));
1296 end Partial_Parameterization;
1297
1298 ---------------------
1299 -- Process_Default --
1300 ---------------------
1301
1302 procedure Process_Default (F : Entity_Id) is
1303 Loc : constant Source_Ptr := Sloc (I_Node);
1304 F_Id : constant Entity_Id := Defining_Entity (F);
1305 Decl : Node_Id;
1306 Default : Node_Id;
1307 Id : Entity_Id;
1308
1309 begin
1310 -- Append copy of formal declaration to associations, and create new
1311 -- defining identifier for it.
1312
1313 Decl := New_Copy_Tree (F);
1314 Id := Make_Defining_Identifier (Sloc (F_Id), Chars (F_Id));
1315
1316 if Nkind (F) in N_Formal_Subprogram_Declaration then
1317 Set_Defining_Unit_Name (Specification (Decl), Id);
1318
1319 else
1320 Set_Defining_Identifier (Decl, Id);
1321 end if;
1322
1323 Append (Decl, Assoc);
1324
1325 if No (Found_Assoc) then
1326 Default :=
1327 Make_Generic_Association (Loc,
1328 Selector_Name =>
1329 New_Occurrence_Of (Id, Loc),
1330 Explicit_Generic_Actual_Parameter => Empty);
1331 Set_Box_Present (Default);
1332 Append (Default, Default_Formals);
1333 end if;
1334 end Process_Default;
1335
1336 ---------------------------------
1337 -- Renames_Standard_Subprogram --
1338 ---------------------------------
1339
1340 function Renames_Standard_Subprogram (Subp : Entity_Id) return Boolean is
1341 Id : Entity_Id;
1342
1343 begin
1344 Id := Alias (Subp);
1345 while Present (Id) loop
1346 if Scope (Id) = Standard_Standard then
1347 return True;
1348 end if;
1349
1350 Id := Alias (Id);
1351 end loop;
1352
1353 return False;
1354 end Renames_Standard_Subprogram;
1355
1356 -------------------------
1357 -- Set_Analyzed_Formal --
1358 -------------------------
1359
1360 procedure Set_Analyzed_Formal is
1361 Kind : Node_Kind;
1362
1363 begin
1364 while Present (Analyzed_Formal) loop
1365 Kind := Nkind (Analyzed_Formal);
1366
1367 case Nkind (Formal) is
1368
1369 when N_Formal_Subprogram_Declaration =>
1370 exit when Kind in N_Formal_Subprogram_Declaration
1371 and then
1372 Chars
1373 (Defining_Unit_Name (Specification (Formal))) =
1374 Chars
1375 (Defining_Unit_Name (Specification (Analyzed_Formal)));
1376
1377 when N_Formal_Package_Declaration =>
1378 exit when Nkind_In (Kind, N_Formal_Package_Declaration,
1379 N_Generic_Package_Declaration,
1380 N_Package_Declaration);
1381
1382 when N_Use_Package_Clause | N_Use_Type_Clause => exit;
1383
1384 when others =>
1385
1386 -- Skip freeze nodes, and nodes inserted to replace
1387 -- unrecognized pragmas.
1388
1389 exit when
1390 Kind not in N_Formal_Subprogram_Declaration
1391 and then not Nkind_In (Kind, N_Subprogram_Declaration,
1392 N_Freeze_Entity,
1393 N_Null_Statement,
1394 N_Itype_Reference)
1395 and then Chars (Defining_Identifier (Formal)) =
1396 Chars (Defining_Identifier (Analyzed_Formal));
1397 end case;
1398
1399 Next (Analyzed_Formal);
1400 end loop;
1401 end Set_Analyzed_Formal;
1402
1403 -- Start of processing for Analyze_Associations
1404
1405 begin
1406 Actuals := Generic_Associations (I_Node);
1407
1408 if Present (Actuals) then
1409
1410 -- Check for an Others choice, indicating a partial parameterization
1411 -- for a formal package.
1412
1413 Actual := First (Actuals);
1414 while Present (Actual) loop
1415 if Nkind (Actual) = N_Others_Choice then
1416 Others_Present := True;
1417 Others_Choice := Actual;
1418
1419 if Present (Next (Actual)) then
1420 Error_Msg_N ("others must be last association", Actual);
1421 end if;
1422
1423 -- This subprogram is used both for formal packages and for
1424 -- instantiations. For the latter, associations must all be
1425 -- explicit.
1426
1427 if Nkind (I_Node) /= N_Formal_Package_Declaration
1428 and then Comes_From_Source (I_Node)
1429 then
1430 Error_Msg_N
1431 ("others association not allowed in an instance",
1432 Actual);
1433 end if;
1434
1435 -- In any case, nothing to do after the others association
1436
1437 exit;
1438
1439 elsif Box_Present (Actual)
1440 and then Comes_From_Source (I_Node)
1441 and then Nkind (I_Node) /= N_Formal_Package_Declaration
1442 then
1443 Error_Msg_N
1444 ("box association not allowed in an instance", Actual);
1445 end if;
1446
1447 Next (Actual);
1448 end loop;
1449
1450 -- If named associations are present, save first named association
1451 -- (it may of course be Empty) to facilitate subsequent name search.
1452
1453 First_Named := First (Actuals);
1454 while Present (First_Named)
1455 and then Nkind (First_Named) /= N_Others_Choice
1456 and then No (Selector_Name (First_Named))
1457 loop
1458 Num_Actuals := Num_Actuals + 1;
1459 Next (First_Named);
1460 end loop;
1461 end if;
1462
1463 Named := First_Named;
1464 while Present (Named) loop
1465 if Nkind (Named) /= N_Others_Choice
1466 and then No (Selector_Name (Named))
1467 then
1468 Error_Msg_N ("invalid positional actual after named one", Named);
1469 Abandon_Instantiation (Named);
1470 end if;
1471
1472 -- A named association may lack an actual parameter, if it was
1473 -- introduced for a default subprogram that turns out to be local
1474 -- to the outer instantiation.
1475
1476 if Nkind (Named) /= N_Others_Choice
1477 and then Present (Explicit_Generic_Actual_Parameter (Named))
1478 then
1479 Num_Actuals := Num_Actuals + 1;
1480 end if;
1481
1482 Next (Named);
1483 end loop;
1484
1485 if Present (Formals) then
1486 Formal := First_Non_Pragma (Formals);
1487 Analyzed_Formal := First_Non_Pragma (F_Copy);
1488
1489 if Present (Actuals) then
1490 Actual := First (Actuals);
1491
1492 -- All formals should have default values
1493
1494 else
1495 Actual := Empty;
1496 end if;
1497
1498 while Present (Formal) loop
1499 Set_Analyzed_Formal;
1500 Saved_Formal := Next_Non_Pragma (Formal);
1501
1502 case Nkind (Formal) is
1503 when N_Formal_Object_Declaration =>
1504 Match :=
1505 Matching_Actual
1506 (Defining_Identifier (Formal),
1507 Defining_Identifier (Analyzed_Formal));
1508
1509 if No (Match) and then Partial_Parameterization then
1510 Process_Default (Formal);
1511
1512 else
1513 Append_List
1514 (Instantiate_Object (Formal, Match, Analyzed_Formal),
1515 Assoc);
1516
1517 -- For a defaulted in_parameter, create an entry in the
1518 -- the list of defaulted actuals, for GNATProve use. Do
1519 -- not included these defaults for an instance nested
1520 -- within a generic, because the defaults are also used
1521 -- in the analysis of the enclosing generic, and only
1522 -- defaulted subprograms are relevant there.
1523
1524 if No (Match) and then not Inside_A_Generic then
1525 Append_To (Default_Actuals,
1526 Make_Generic_Association (Sloc (I_Node),
1527 Selector_Name =>
1528 New_Occurrence_Of
1529 (Defining_Identifier (Formal), Sloc (I_Node)),
1530 Explicit_Generic_Actual_Parameter =>
1531 New_Copy_Tree (Default_Expression (Formal))));
1532 end if;
1533 end if;
1534
1535 -- If the object is a call to an expression function, this
1536 -- is a freezing point for it.
1537
1538 if Is_Entity_Name (Match)
1539 and then Present (Entity (Match))
1540 and then Nkind
1541 (Original_Node (Unit_Declaration_Node (Entity (Match))))
1542 = N_Expression_Function
1543 then
1544 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1545 end if;
1546
1547 when N_Formal_Type_Declaration =>
1548 Match :=
1549 Matching_Actual
1550 (Defining_Identifier (Formal),
1551 Defining_Identifier (Analyzed_Formal));
1552
1553 if No (Match) then
1554 if Partial_Parameterization then
1555 Process_Default (Formal);
1556
1557 else
1558 Error_Msg_Sloc := Sloc (Gen_Unit);
1559 Error_Msg_NE
1560 ("missing actual&",
1561 Instantiation_Node, Defining_Identifier (Formal));
1562 Error_Msg_NE
1563 ("\in instantiation of & declared#",
1564 Instantiation_Node, Gen_Unit);
1565 Abandon_Instantiation (Instantiation_Node);
1566 end if;
1567
1568 else
1569 Analyze (Match);
1570 Append_List
1571 (Instantiate_Type
1572 (Formal, Match, Analyzed_Formal, Assoc),
1573 Assoc);
1574
1575 -- An instantiation is a freeze point for the actuals,
1576 -- unless this is a rewritten formal package, or the
1577 -- formal is an Ada 2012 formal incomplete type.
1578
1579 if Nkind (I_Node) = N_Formal_Package_Declaration
1580 or else
1581 (Ada_Version >= Ada_2012
1582 and then
1583 Ekind (Defining_Identifier (Analyzed_Formal)) =
1584 E_Incomplete_Type)
1585 then
1586 null;
1587
1588 else
1589 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1590 end if;
1591 end if;
1592
1593 -- A remote access-to-class-wide type is not a legal actual
1594 -- for a generic formal of an access type (E.2.2(17/2)).
1595 -- In GNAT an exception to this rule is introduced when
1596 -- the formal is marked as remote using implementation
1597 -- defined aspect/pragma Remote_Access_Type. In that case
1598 -- the actual must be remote as well.
1599
1600 -- If the current instantiation is the construction of a
1601 -- local copy for a formal package the actuals may be
1602 -- defaulted, and there is no matching actual to check.
1603
1604 if Nkind (Analyzed_Formal) = N_Formal_Type_Declaration
1605 and then
1606 Nkind (Formal_Type_Definition (Analyzed_Formal)) =
1607 N_Access_To_Object_Definition
1608 and then Present (Match)
1609 then
1610 declare
1611 Formal_Ent : constant Entity_Id :=
1612 Defining_Identifier (Analyzed_Formal);
1613 begin
1614 if Is_Remote_Access_To_Class_Wide_Type (Entity (Match))
1615 = Is_Remote_Types (Formal_Ent)
1616 then
1617 -- Remoteness of formal and actual match
1618
1619 null;
1620
1621 elsif Is_Remote_Types (Formal_Ent) then
1622
1623 -- Remote formal, non-remote actual
1624
1625 Error_Msg_NE
1626 ("actual for& must be remote", Match, Formal_Ent);
1627
1628 else
1629 -- Non-remote formal, remote actual
1630
1631 Error_Msg_NE
1632 ("actual for& may not be remote",
1633 Match, Formal_Ent);
1634 end if;
1635 end;
1636 end if;
1637
1638 when N_Formal_Subprogram_Declaration =>
1639 Match :=
1640 Matching_Actual
1641 (Defining_Unit_Name (Specification (Formal)),
1642 Defining_Unit_Name (Specification (Analyzed_Formal)));
1643
1644 -- If the formal subprogram has the same name as another
1645 -- formal subprogram of the generic, then a named
1646 -- association is illegal (12.3(9)). Exclude named
1647 -- associations that are generated for a nested instance.
1648
1649 if Present (Match)
1650 and then Is_Named_Assoc
1651 and then Comes_From_Source (Found_Assoc)
1652 then
1653 Check_Overloaded_Formal_Subprogram (Formal);
1654 end if;
1655
1656 -- If there is no corresponding actual, this may be case
1657 -- of partial parameterization, or else the formal has a
1658 -- default or a box.
1659
1660 if No (Match) and then Partial_Parameterization then
1661 Process_Default (Formal);
1662
1663 if Nkind (I_Node) = N_Formal_Package_Declaration then
1664 Check_Overloaded_Formal_Subprogram (Formal);
1665 end if;
1666
1667 else
1668 Append_To (Assoc,
1669 Instantiate_Formal_Subprogram
1670 (Formal, Match, Analyzed_Formal));
1671
1672 -- An instantiation is a freeze point for the actuals,
1673 -- unless this is a rewritten formal package.
1674
1675 if Nkind (I_Node) /= N_Formal_Package_Declaration
1676 and then Nkind (Match) = N_Identifier
1677 and then Is_Subprogram (Entity (Match))
1678
1679 -- The actual subprogram may rename a routine defined
1680 -- in Standard. Avoid freezing such renamings because
1681 -- subprograms coming from Standard cannot be frozen.
1682
1683 and then
1684 not Renames_Standard_Subprogram (Entity (Match))
1685
1686 -- If the actual subprogram comes from a different
1687 -- unit, it is already frozen, either by a body in
1688 -- that unit or by the end of the declarative part
1689 -- of the unit. This check avoids the freezing of
1690 -- subprograms defined in Standard which are used
1691 -- as generic actuals.
1692
1693 and then In_Same_Code_Unit (Entity (Match), I_Node)
1694 and then Has_Fully_Defined_Profile (Entity (Match))
1695 then
1696 -- Mark the subprogram as having a delayed freeze
1697 -- since this may be an out-of-order action.
1698
1699 Set_Has_Delayed_Freeze (Entity (Match));
1700 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1701 end if;
1702 end if;
1703
1704 -- If this is a nested generic, preserve default for later
1705 -- instantiations. We do this as well for GNATProve use,
1706 -- so that the list of generic associations is complete.
1707
1708 if No (Match) and then Box_Present (Formal) then
1709 declare
1710 Subp : constant Entity_Id :=
1711 Defining_Unit_Name (Specification (Last (Assoc)));
1712
1713 begin
1714 Append_To (Default_Actuals,
1715 Make_Generic_Association (Sloc (I_Node),
1716 Selector_Name =>
1717 New_Occurrence_Of (Subp, Sloc (I_Node)),
1718 Explicit_Generic_Actual_Parameter =>
1719 New_Occurrence_Of (Subp, Sloc (I_Node))));
1720 end;
1721 end if;
1722
1723 when N_Formal_Package_Declaration =>
1724 Match :=
1725 Matching_Actual
1726 (Defining_Identifier (Formal),
1727 Defining_Identifier (Original_Node (Analyzed_Formal)));
1728
1729 if No (Match) then
1730 if Partial_Parameterization then
1731 Process_Default (Formal);
1732
1733 else
1734 Error_Msg_Sloc := Sloc (Gen_Unit);
1735 Error_Msg_NE
1736 ("missing actual&",
1737 Instantiation_Node, Defining_Identifier (Formal));
1738 Error_Msg_NE
1739 ("\in instantiation of & declared#",
1740 Instantiation_Node, Gen_Unit);
1741
1742 Abandon_Instantiation (Instantiation_Node);
1743 end if;
1744
1745 else
1746 Analyze (Match);
1747 Append_List
1748 (Instantiate_Formal_Package
1749 (Formal, Match, Analyzed_Formal),
1750 Assoc);
1751 end if;
1752
1753 -- For use type and use package appearing in the generic part,
1754 -- we have already copied them, so we can just move them where
1755 -- they belong (we mustn't recopy them since this would mess up
1756 -- the Sloc values).
1757
1758 when N_Use_Package_Clause |
1759 N_Use_Type_Clause =>
1760 if Nkind (Original_Node (I_Node)) =
1761 N_Formal_Package_Declaration
1762 then
1763 Append (New_Copy_Tree (Formal), Assoc);
1764 else
1765 Remove (Formal);
1766 Append (Formal, Assoc);
1767 end if;
1768
1769 when others =>
1770 raise Program_Error;
1771
1772 end case;
1773
1774 Formal := Saved_Formal;
1775 Next_Non_Pragma (Analyzed_Formal);
1776 end loop;
1777
1778 if Num_Actuals > Num_Matched then
1779 Error_Msg_Sloc := Sloc (Gen_Unit);
1780
1781 if Present (Selector_Name (Actual)) then
1782 Error_Msg_NE
1783 ("unmatched actual &", Actual, Selector_Name (Actual));
1784 Error_Msg_NE
1785 ("\in instantiation of & declared#", Actual, Gen_Unit);
1786 else
1787 Error_Msg_NE
1788 ("unmatched actual in instantiation of & declared#",
1789 Actual, Gen_Unit);
1790 end if;
1791 end if;
1792
1793 elsif Present (Actuals) then
1794 Error_Msg_N
1795 ("too many actuals in generic instantiation", Instantiation_Node);
1796 end if;
1797
1798 -- An instantiation freezes all generic actuals. The only exceptions
1799 -- to this are incomplete types and subprograms which are not fully
1800 -- defined at the point of instantiation.
1801
1802 declare
1803 Elmt : Elmt_Id := First_Elmt (Actuals_To_Freeze);
1804 begin
1805 while Present (Elmt) loop
1806 Freeze_Before (I_Node, Node (Elmt));
1807 Next_Elmt (Elmt);
1808 end loop;
1809 end;
1810
1811 -- If there are default subprograms, normalize the tree by adding
1812 -- explicit associations for them. This is required if the instance
1813 -- appears within a generic.
1814
1815 if not Is_Empty_List (Default_Actuals) then
1816 declare
1817 Default : Node_Id;
1818
1819 begin
1820 Default := First (Default_Actuals);
1821 while Present (Default) loop
1822 Mark_Rewrite_Insertion (Default);
1823 Next (Default);
1824 end loop;
1825
1826 if No (Actuals) then
1827 Set_Generic_Associations (I_Node, Default_Actuals);
1828 else
1829 Append_List_To (Actuals, Default_Actuals);
1830 end if;
1831 end;
1832 end if;
1833
1834 -- If this is a formal package, normalize the parameter list by adding
1835 -- explicit box associations for the formals that are covered by an
1836 -- Others_Choice.
1837
1838 if not Is_Empty_List (Default_Formals) then
1839 Append_List (Default_Formals, Formals);
1840 end if;
1841
1842 return Assoc;
1843 end Analyze_Associations;
1844
1845 -------------------------------
1846 -- Analyze_Formal_Array_Type --
1847 -------------------------------
1848
1849 procedure Analyze_Formal_Array_Type
1850 (T : in out Entity_Id;
1851 Def : Node_Id)
1852 is
1853 DSS : Node_Id;
1854
1855 begin
1856 -- Treated like a non-generic array declaration, with additional
1857 -- semantic checks.
1858
1859 Enter_Name (T);
1860
1861 if Nkind (Def) = N_Constrained_Array_Definition then
1862 DSS := First (Discrete_Subtype_Definitions (Def));
1863 while Present (DSS) loop
1864 if Nkind_In (DSS, N_Subtype_Indication,
1865 N_Range,
1866 N_Attribute_Reference)
1867 then
1868 Error_Msg_N ("only a subtype mark is allowed in a formal", DSS);
1869 end if;
1870
1871 Next (DSS);
1872 end loop;
1873 end if;
1874
1875 Array_Type_Declaration (T, Def);
1876 Set_Is_Generic_Type (Base_Type (T));
1877
1878 if Ekind (Component_Type (T)) = E_Incomplete_Type
1879 and then No (Full_View (Component_Type (T)))
1880 then
1881 Error_Msg_N ("premature usage of incomplete type", Def);
1882
1883 -- Check that range constraint is not allowed on the component type
1884 -- of a generic formal array type (AARM 12.5.3(3))
1885
1886 elsif Is_Internal (Component_Type (T))
1887 and then Present (Subtype_Indication (Component_Definition (Def)))
1888 and then Nkind (Original_Node
1889 (Subtype_Indication (Component_Definition (Def)))) =
1890 N_Subtype_Indication
1891 then
1892 Error_Msg_N
1893 ("in a formal, a subtype indication can only be "
1894 & "a subtype mark (RM 12.5.3(3))",
1895 Subtype_Indication (Component_Definition (Def)));
1896 end if;
1897
1898 end Analyze_Formal_Array_Type;
1899
1900 ---------------------------------------------
1901 -- Analyze_Formal_Decimal_Fixed_Point_Type --
1902 ---------------------------------------------
1903
1904 -- As for other generic types, we create a valid type representation with
1905 -- legal but arbitrary attributes, whose values are never considered
1906 -- static. For all scalar types we introduce an anonymous base type, with
1907 -- the same attributes. We choose the corresponding integer type to be
1908 -- Standard_Integer.
1909 -- Here and in other similar routines, the Sloc of the generated internal
1910 -- type must be the same as the sloc of the defining identifier of the
1911 -- formal type declaration, to provide proper source navigation.
1912
1913 procedure Analyze_Formal_Decimal_Fixed_Point_Type
1914 (T : Entity_Id;
1915 Def : Node_Id)
1916 is
1917 Loc : constant Source_Ptr := Sloc (Def);
1918
1919 Base : constant Entity_Id :=
1920 New_Internal_Entity
1921 (E_Decimal_Fixed_Point_Type,
1922 Current_Scope,
1923 Sloc (Defining_Identifier (Parent (Def))), 'G');
1924
1925 Int_Base : constant Entity_Id := Standard_Integer;
1926 Delta_Val : constant Ureal := Ureal_1;
1927 Digs_Val : constant Uint := Uint_6;
1928
1929 function Make_Dummy_Bound return Node_Id;
1930 -- Return a properly typed universal real literal to use as a bound
1931
1932 ----------------------
1933 -- Make_Dummy_Bound --
1934 ----------------------
1935
1936 function Make_Dummy_Bound return Node_Id is
1937 Bound : constant Node_Id := Make_Real_Literal (Loc, Ureal_1);
1938 begin
1939 Set_Etype (Bound, Universal_Real);
1940 return Bound;
1941 end Make_Dummy_Bound;
1942
1943 -- Start of processing for Analyze_Formal_Decimal_Fixed_Point_Type
1944
1945 begin
1946 Enter_Name (T);
1947
1948 Set_Etype (Base, Base);
1949 Set_Size_Info (Base, Int_Base);
1950 Set_RM_Size (Base, RM_Size (Int_Base));
1951 Set_First_Rep_Item (Base, First_Rep_Item (Int_Base));
1952 Set_Digits_Value (Base, Digs_Val);
1953 Set_Delta_Value (Base, Delta_Val);
1954 Set_Small_Value (Base, Delta_Val);
1955 Set_Scalar_Range (Base,
1956 Make_Range (Loc,
1957 Low_Bound => Make_Dummy_Bound,
1958 High_Bound => Make_Dummy_Bound));
1959
1960 Set_Is_Generic_Type (Base);
1961 Set_Parent (Base, Parent (Def));
1962
1963 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
1964 Set_Etype (T, Base);
1965 Set_Size_Info (T, Int_Base);
1966 Set_RM_Size (T, RM_Size (Int_Base));
1967 Set_First_Rep_Item (T, First_Rep_Item (Int_Base));
1968 Set_Digits_Value (T, Digs_Val);
1969 Set_Delta_Value (T, Delta_Val);
1970 Set_Small_Value (T, Delta_Val);
1971 Set_Scalar_Range (T, Scalar_Range (Base));
1972 Set_Is_Constrained (T);
1973
1974 Check_Restriction (No_Fixed_Point, Def);
1975 end Analyze_Formal_Decimal_Fixed_Point_Type;
1976
1977 -------------------------------------------
1978 -- Analyze_Formal_Derived_Interface_Type --
1979 -------------------------------------------
1980
1981 procedure Analyze_Formal_Derived_Interface_Type
1982 (N : Node_Id;
1983 T : Entity_Id;
1984 Def : Node_Id)
1985 is
1986 Loc : constant Source_Ptr := Sloc (Def);
1987
1988 begin
1989 -- Rewrite as a type declaration of a derived type. This ensures that
1990 -- the interface list and primitive operations are properly captured.
1991
1992 Rewrite (N,
1993 Make_Full_Type_Declaration (Loc,
1994 Defining_Identifier => T,
1995 Type_Definition => Def));
1996 Analyze (N);
1997 Set_Is_Generic_Type (T);
1998 end Analyze_Formal_Derived_Interface_Type;
1999
2000 ---------------------------------
2001 -- Analyze_Formal_Derived_Type --
2002 ---------------------------------
2003
2004 procedure Analyze_Formal_Derived_Type
2005 (N : Node_Id;
2006 T : Entity_Id;
2007 Def : Node_Id)
2008 is
2009 Loc : constant Source_Ptr := Sloc (Def);
2010 Unk_Disc : constant Boolean := Unknown_Discriminants_Present (N);
2011 New_N : Node_Id;
2012
2013 begin
2014 Set_Is_Generic_Type (T);
2015
2016 if Private_Present (Def) then
2017 New_N :=
2018 Make_Private_Extension_Declaration (Loc,
2019 Defining_Identifier => T,
2020 Discriminant_Specifications => Discriminant_Specifications (N),
2021 Unknown_Discriminants_Present => Unk_Disc,
2022 Subtype_Indication => Subtype_Mark (Def),
2023 Interface_List => Interface_List (Def));
2024
2025 Set_Abstract_Present (New_N, Abstract_Present (Def));
2026 Set_Limited_Present (New_N, Limited_Present (Def));
2027 Set_Synchronized_Present (New_N, Synchronized_Present (Def));
2028
2029 else
2030 New_N :=
2031 Make_Full_Type_Declaration (Loc,
2032 Defining_Identifier => T,
2033 Discriminant_Specifications =>
2034 Discriminant_Specifications (Parent (T)),
2035 Type_Definition =>
2036 Make_Derived_Type_Definition (Loc,
2037 Subtype_Indication => Subtype_Mark (Def)));
2038
2039 Set_Abstract_Present
2040 (Type_Definition (New_N), Abstract_Present (Def));
2041 Set_Limited_Present
2042 (Type_Definition (New_N), Limited_Present (Def));
2043 end if;
2044
2045 Rewrite (N, New_N);
2046 Analyze (N);
2047
2048 if Unk_Disc then
2049 if not Is_Composite_Type (T) then
2050 Error_Msg_N
2051 ("unknown discriminants not allowed for elementary types", N);
2052 else
2053 Set_Has_Unknown_Discriminants (T);
2054 Set_Is_Constrained (T, False);
2055 end if;
2056 end if;
2057
2058 -- If the parent type has a known size, so does the formal, which makes
2059 -- legal representation clauses that involve the formal.
2060
2061 Set_Size_Known_At_Compile_Time
2062 (T, Size_Known_At_Compile_Time (Entity (Subtype_Mark (Def))));
2063 end Analyze_Formal_Derived_Type;
2064
2065 ----------------------------------
2066 -- Analyze_Formal_Discrete_Type --
2067 ----------------------------------
2068
2069 -- The operations defined for a discrete types are those of an enumeration
2070 -- type. The size is set to an arbitrary value, for use in analyzing the
2071 -- generic unit.
2072
2073 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id) is
2074 Loc : constant Source_Ptr := Sloc (Def);
2075 Lo : Node_Id;
2076 Hi : Node_Id;
2077
2078 Base : constant Entity_Id :=
2079 New_Internal_Entity
2080 (E_Floating_Point_Type, Current_Scope,
2081 Sloc (Defining_Identifier (Parent (Def))), 'G');
2082
2083 begin
2084 Enter_Name (T);
2085 Set_Ekind (T, E_Enumeration_Subtype);
2086 Set_Etype (T, Base);
2087 Init_Size (T, 8);
2088 Init_Alignment (T);
2089 Set_Is_Generic_Type (T);
2090 Set_Is_Constrained (T);
2091
2092 -- For semantic analysis, the bounds of the type must be set to some
2093 -- non-static value. The simplest is to create attribute nodes for those
2094 -- bounds, that refer to the type itself. These bounds are never
2095 -- analyzed but serve as place-holders.
2096
2097 Lo :=
2098 Make_Attribute_Reference (Loc,
2099 Attribute_Name => Name_First,
2100 Prefix => New_Occurrence_Of (T, Loc));
2101 Set_Etype (Lo, T);
2102
2103 Hi :=
2104 Make_Attribute_Reference (Loc,
2105 Attribute_Name => Name_Last,
2106 Prefix => New_Occurrence_Of (T, Loc));
2107 Set_Etype (Hi, T);
2108
2109 Set_Scalar_Range (T,
2110 Make_Range (Loc,
2111 Low_Bound => Lo,
2112 High_Bound => Hi));
2113
2114 Set_Ekind (Base, E_Enumeration_Type);
2115 Set_Etype (Base, Base);
2116 Init_Size (Base, 8);
2117 Init_Alignment (Base);
2118 Set_Is_Generic_Type (Base);
2119 Set_Scalar_Range (Base, Scalar_Range (T));
2120 Set_Parent (Base, Parent (Def));
2121 end Analyze_Formal_Discrete_Type;
2122
2123 ----------------------------------
2124 -- Analyze_Formal_Floating_Type --
2125 ---------------------------------
2126
2127 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id) is
2128 Base : constant Entity_Id :=
2129 New_Internal_Entity
2130 (E_Floating_Point_Type, Current_Scope,
2131 Sloc (Defining_Identifier (Parent (Def))), 'G');
2132
2133 begin
2134 -- The various semantic attributes are taken from the predefined type
2135 -- Float, just so that all of them are initialized. Their values are
2136 -- never used because no constant folding or expansion takes place in
2137 -- the generic itself.
2138
2139 Enter_Name (T);
2140 Set_Ekind (T, E_Floating_Point_Subtype);
2141 Set_Etype (T, Base);
2142 Set_Size_Info (T, (Standard_Float));
2143 Set_RM_Size (T, RM_Size (Standard_Float));
2144 Set_Digits_Value (T, Digits_Value (Standard_Float));
2145 Set_Scalar_Range (T, Scalar_Range (Standard_Float));
2146 Set_Is_Constrained (T);
2147
2148 Set_Is_Generic_Type (Base);
2149 Set_Etype (Base, Base);
2150 Set_Size_Info (Base, (Standard_Float));
2151 Set_RM_Size (Base, RM_Size (Standard_Float));
2152 Set_Digits_Value (Base, Digits_Value (Standard_Float));
2153 Set_Scalar_Range (Base, Scalar_Range (Standard_Float));
2154 Set_Parent (Base, Parent (Def));
2155
2156 Check_Restriction (No_Floating_Point, Def);
2157 end Analyze_Formal_Floating_Type;
2158
2159 -----------------------------------
2160 -- Analyze_Formal_Interface_Type;--
2161 -----------------------------------
2162
2163 procedure Analyze_Formal_Interface_Type
2164 (N : Node_Id;
2165 T : Entity_Id;
2166 Def : Node_Id)
2167 is
2168 Loc : constant Source_Ptr := Sloc (N);
2169 New_N : Node_Id;
2170
2171 begin
2172 New_N :=
2173 Make_Full_Type_Declaration (Loc,
2174 Defining_Identifier => T,
2175 Type_Definition => Def);
2176
2177 Rewrite (N, New_N);
2178 Analyze (N);
2179 Set_Is_Generic_Type (T);
2180 end Analyze_Formal_Interface_Type;
2181
2182 ---------------------------------
2183 -- Analyze_Formal_Modular_Type --
2184 ---------------------------------
2185
2186 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id) is
2187 begin
2188 -- Apart from their entity kind, generic modular types are treated like
2189 -- signed integer types, and have the same attributes.
2190
2191 Analyze_Formal_Signed_Integer_Type (T, Def);
2192 Set_Ekind (T, E_Modular_Integer_Subtype);
2193 Set_Ekind (Etype (T), E_Modular_Integer_Type);
2194
2195 end Analyze_Formal_Modular_Type;
2196
2197 ---------------------------------------
2198 -- Analyze_Formal_Object_Declaration --
2199 ---------------------------------------
2200
2201 procedure Analyze_Formal_Object_Declaration (N : Node_Id) is
2202 E : constant Node_Id := Default_Expression (N);
2203 Id : constant Node_Id := Defining_Identifier (N);
2204 K : Entity_Kind;
2205 T : Node_Id;
2206
2207 begin
2208 Enter_Name (Id);
2209
2210 -- Determine the mode of the formal object
2211
2212 if Out_Present (N) then
2213 K := E_Generic_In_Out_Parameter;
2214
2215 if not In_Present (N) then
2216 Error_Msg_N ("formal generic objects cannot have mode OUT", N);
2217 end if;
2218
2219 else
2220 K := E_Generic_In_Parameter;
2221 end if;
2222
2223 if Present (Subtype_Mark (N)) then
2224 Find_Type (Subtype_Mark (N));
2225 T := Entity (Subtype_Mark (N));
2226
2227 -- Verify that there is no redundant null exclusion
2228
2229 if Null_Exclusion_Present (N) then
2230 if not Is_Access_Type (T) then
2231 Error_Msg_N
2232 ("null exclusion can only apply to an access type", N);
2233
2234 elsif Can_Never_Be_Null (T) then
2235 Error_Msg_NE
2236 ("`NOT NULL` not allowed (& already excludes null)", N, T);
2237 end if;
2238 end if;
2239
2240 -- Ada 2005 (AI-423): Formal object with an access definition
2241
2242 else
2243 Check_Access_Definition (N);
2244 T := Access_Definition
2245 (Related_Nod => N,
2246 N => Access_Definition (N));
2247 end if;
2248
2249 if Ekind (T) = E_Incomplete_Type then
2250 declare
2251 Error_Node : Node_Id;
2252
2253 begin
2254 if Present (Subtype_Mark (N)) then
2255 Error_Node := Subtype_Mark (N);
2256 else
2257 Check_Access_Definition (N);
2258 Error_Node := Access_Definition (N);
2259 end if;
2260
2261 Error_Msg_N ("premature usage of incomplete type", Error_Node);
2262 end;
2263 end if;
2264
2265 if K = E_Generic_In_Parameter then
2266
2267 -- Ada 2005 (AI-287): Limited aggregates allowed in generic formals
2268
2269 if Ada_Version < Ada_2005 and then Is_Limited_Type (T) then
2270 Error_Msg_N
2271 ("generic formal of mode IN must not be of limited type", N);
2272 Explain_Limited_Type (T, N);
2273 end if;
2274
2275 if Is_Abstract_Type (T) then
2276 Error_Msg_N
2277 ("generic formal of mode IN must not be of abstract type", N);
2278 end if;
2279
2280 if Present (E) then
2281 Preanalyze_Spec_Expression (E, T);
2282
2283 if Is_Limited_Type (T) and then not OK_For_Limited_Init (T, E) then
2284 Error_Msg_N
2285 ("initialization not allowed for limited types", E);
2286 Explain_Limited_Type (T, E);
2287 end if;
2288 end if;
2289
2290 Set_Ekind (Id, K);
2291 Set_Etype (Id, T);
2292
2293 -- Case of generic IN OUT parameter
2294
2295 else
2296 -- If the formal has an unconstrained type, construct its actual
2297 -- subtype, as is done for subprogram formals. In this fashion, all
2298 -- its uses can refer to specific bounds.
2299
2300 Set_Ekind (Id, K);
2301 Set_Etype (Id, T);
2302
2303 if (Is_Array_Type (T) and then not Is_Constrained (T))
2304 or else (Ekind (T) = E_Record_Type and then Has_Discriminants (T))
2305 then
2306 declare
2307 Non_Freezing_Ref : constant Node_Id :=
2308 New_Occurrence_Of (Id, Sloc (Id));
2309 Decl : Node_Id;
2310
2311 begin
2312 -- Make sure the actual subtype doesn't generate bogus freezing
2313
2314 Set_Must_Not_Freeze (Non_Freezing_Ref);
2315 Decl := Build_Actual_Subtype (T, Non_Freezing_Ref);
2316 Insert_Before_And_Analyze (N, Decl);
2317 Set_Actual_Subtype (Id, Defining_Identifier (Decl));
2318 end;
2319 else
2320 Set_Actual_Subtype (Id, T);
2321 end if;
2322
2323 if Present (E) then
2324 Error_Msg_N
2325 ("initialization not allowed for `IN OUT` formals", N);
2326 end if;
2327 end if;
2328
2329 if Has_Aspects (N) then
2330 Analyze_Aspect_Specifications (N, Id);
2331 end if;
2332 end Analyze_Formal_Object_Declaration;
2333
2334 ----------------------------------------------
2335 -- Analyze_Formal_Ordinary_Fixed_Point_Type --
2336 ----------------------------------------------
2337
2338 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
2339 (T : Entity_Id;
2340 Def : Node_Id)
2341 is
2342 Loc : constant Source_Ptr := Sloc (Def);
2343 Base : constant Entity_Id :=
2344 New_Internal_Entity
2345 (E_Ordinary_Fixed_Point_Type, Current_Scope,
2346 Sloc (Defining_Identifier (Parent (Def))), 'G');
2347
2348 begin
2349 -- The semantic attributes are set for completeness only, their values
2350 -- will never be used, since all properties of the type are non-static.
2351
2352 Enter_Name (T);
2353 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
2354 Set_Etype (T, Base);
2355 Set_Size_Info (T, Standard_Integer);
2356 Set_RM_Size (T, RM_Size (Standard_Integer));
2357 Set_Small_Value (T, Ureal_1);
2358 Set_Delta_Value (T, Ureal_1);
2359 Set_Scalar_Range (T,
2360 Make_Range (Loc,
2361 Low_Bound => Make_Real_Literal (Loc, Ureal_1),
2362 High_Bound => Make_Real_Literal (Loc, Ureal_1)));
2363 Set_Is_Constrained (T);
2364
2365 Set_Is_Generic_Type (Base);
2366 Set_Etype (Base, Base);
2367 Set_Size_Info (Base, Standard_Integer);
2368 Set_RM_Size (Base, RM_Size (Standard_Integer));
2369 Set_Small_Value (Base, Ureal_1);
2370 Set_Delta_Value (Base, Ureal_1);
2371 Set_Scalar_Range (Base, Scalar_Range (T));
2372 Set_Parent (Base, Parent (Def));
2373
2374 Check_Restriction (No_Fixed_Point, Def);
2375 end Analyze_Formal_Ordinary_Fixed_Point_Type;
2376
2377 ----------------------------------------
2378 -- Analyze_Formal_Package_Declaration --
2379 ----------------------------------------
2380
2381 procedure Analyze_Formal_Package_Declaration (N : Node_Id) is
2382 Loc : constant Source_Ptr := Sloc (N);
2383 Pack_Id : constant Entity_Id := Defining_Identifier (N);
2384 Formal : Entity_Id;
2385 Gen_Id : constant Node_Id := Name (N);
2386 Gen_Decl : Node_Id;
2387 Gen_Unit : Entity_Id;
2388 New_N : Node_Id;
2389 Parent_Installed : Boolean := False;
2390 Renaming : Node_Id;
2391 Parent_Instance : Entity_Id;
2392 Renaming_In_Par : Entity_Id;
2393 Associations : Boolean := True;
2394
2395 Vis_Prims_List : Elist_Id := No_Elist;
2396 -- List of primitives made temporarily visible in the instantiation
2397 -- to match the visibility of the formal type
2398
2399 function Build_Local_Package return Node_Id;
2400 -- The formal package is rewritten so that its parameters are replaced
2401 -- with corresponding declarations. For parameters with bona fide
2402 -- associations these declarations are created by Analyze_Associations
2403 -- as for a regular instantiation. For boxed parameters, we preserve
2404 -- the formal declarations and analyze them, in order to introduce
2405 -- entities of the right kind in the environment of the formal.
2406
2407 -------------------------
2408 -- Build_Local_Package --
2409 -------------------------
2410
2411 function Build_Local_Package return Node_Id is
2412 Decls : List_Id;
2413 Pack_Decl : Node_Id;
2414
2415 begin
2416 -- Within the formal, the name of the generic package is a renaming
2417 -- of the formal (as for a regular instantiation).
2418
2419 Pack_Decl :=
2420 Make_Package_Declaration (Loc,
2421 Specification =>
2422 Copy_Generic_Node
2423 (Specification (Original_Node (Gen_Decl)),
2424 Empty, Instantiating => True));
2425
2426 Renaming :=
2427 Make_Package_Renaming_Declaration (Loc,
2428 Defining_Unit_Name =>
2429 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
2430 Name => New_Occurrence_Of (Formal, Loc));
2431
2432 if Nkind (Gen_Id) = N_Identifier
2433 and then Chars (Gen_Id) = Chars (Pack_Id)
2434 then
2435 Error_Msg_NE
2436 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
2437 end if;
2438
2439 -- If the formal is declared with a box, or with an others choice,
2440 -- create corresponding declarations for all entities in the formal
2441 -- part, so that names with the proper types are available in the
2442 -- specification of the formal package.
2443
2444 -- On the other hand, if there are no associations, then all the
2445 -- formals must have defaults, and this will be checked by the
2446 -- call to Analyze_Associations.
2447
2448 if Box_Present (N)
2449 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2450 then
2451 declare
2452 Formal_Decl : Node_Id;
2453
2454 begin
2455 -- TBA : for a formal package, need to recurse ???
2456
2457 Decls := New_List;
2458 Formal_Decl :=
2459 First
2460 (Generic_Formal_Declarations (Original_Node (Gen_Decl)));
2461 while Present (Formal_Decl) loop
2462 Append_To
2463 (Decls, Copy_Generic_Node (Formal_Decl, Empty, True));
2464 Next (Formal_Decl);
2465 end loop;
2466 end;
2467
2468 -- If generic associations are present, use Analyze_Associations to
2469 -- create the proper renaming declarations.
2470
2471 else
2472 declare
2473 Act_Tree : constant Node_Id :=
2474 Copy_Generic_Node
2475 (Original_Node (Gen_Decl), Empty,
2476 Instantiating => True);
2477
2478 begin
2479 Generic_Renamings.Set_Last (0);
2480 Generic_Renamings_HTable.Reset;
2481 Instantiation_Node := N;
2482
2483 Decls :=
2484 Analyze_Associations
2485 (I_Node => Original_Node (N),
2486 Formals => Generic_Formal_Declarations (Act_Tree),
2487 F_Copy => Generic_Formal_Declarations (Gen_Decl));
2488
2489 Vis_Prims_List := Check_Hidden_Primitives (Decls);
2490 end;
2491 end if;
2492
2493 Append (Renaming, To => Decls);
2494
2495 -- Add generated declarations ahead of local declarations in
2496 -- the package.
2497
2498 if No (Visible_Declarations (Specification (Pack_Decl))) then
2499 Set_Visible_Declarations (Specification (Pack_Decl), Decls);
2500 else
2501 Insert_List_Before
2502 (First (Visible_Declarations (Specification (Pack_Decl))),
2503 Decls);
2504 end if;
2505
2506 return Pack_Decl;
2507 end Build_Local_Package;
2508
2509 -- Start of processing for Analyze_Formal_Package_Declaration
2510
2511 begin
2512 Check_Text_IO_Special_Unit (Gen_Id);
2513
2514 Init_Env;
2515 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
2516 Gen_Unit := Entity (Gen_Id);
2517
2518 -- Check for a formal package that is a package renaming
2519
2520 if Present (Renamed_Object (Gen_Unit)) then
2521
2522 -- Indicate that unit is used, before replacing it with renamed
2523 -- entity for use below.
2524
2525 if In_Extended_Main_Source_Unit (N) then
2526 Set_Is_Instantiated (Gen_Unit);
2527 Generate_Reference (Gen_Unit, N);
2528 end if;
2529
2530 Gen_Unit := Renamed_Object (Gen_Unit);
2531 end if;
2532
2533 if Ekind (Gen_Unit) /= E_Generic_Package then
2534 Error_Msg_N ("expect generic package name", Gen_Id);
2535 Restore_Env;
2536 goto Leave;
2537
2538 elsif Gen_Unit = Current_Scope then
2539 Error_Msg_N
2540 ("generic package cannot be used as a formal package of itself",
2541 Gen_Id);
2542 Restore_Env;
2543 goto Leave;
2544
2545 elsif In_Open_Scopes (Gen_Unit) then
2546 if Is_Compilation_Unit (Gen_Unit)
2547 and then Is_Child_Unit (Current_Scope)
2548 then
2549 -- Special-case the error when the formal is a parent, and
2550 -- continue analysis to minimize cascaded errors.
2551
2552 Error_Msg_N
2553 ("generic parent cannot be used as formal package "
2554 & "of a child unit", Gen_Id);
2555
2556 else
2557 Error_Msg_N
2558 ("generic package cannot be used as a formal package "
2559 & "within itself", Gen_Id);
2560 Restore_Env;
2561 goto Leave;
2562 end if;
2563 end if;
2564
2565 -- Check that name of formal package does not hide name of generic,
2566 -- or its leading prefix. This check must be done separately because
2567 -- the name of the generic has already been analyzed.
2568
2569 declare
2570 Gen_Name : Entity_Id;
2571
2572 begin
2573 Gen_Name := Gen_Id;
2574 while Nkind (Gen_Name) = N_Expanded_Name loop
2575 Gen_Name := Prefix (Gen_Name);
2576 end loop;
2577
2578 if Chars (Gen_Name) = Chars (Pack_Id) then
2579 Error_Msg_NE
2580 ("& is hidden within declaration of formal package",
2581 Gen_Id, Gen_Name);
2582 end if;
2583 end;
2584
2585 if Box_Present (N)
2586 or else No (Generic_Associations (N))
2587 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2588 then
2589 Associations := False;
2590 end if;
2591
2592 -- If there are no generic associations, the generic parameters appear
2593 -- as local entities and are instantiated like them. We copy the generic
2594 -- package declaration as if it were an instantiation, and analyze it
2595 -- like a regular package, except that we treat the formals as
2596 -- additional visible components.
2597
2598 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
2599
2600 if In_Extended_Main_Source_Unit (N) then
2601 Set_Is_Instantiated (Gen_Unit);
2602 Generate_Reference (Gen_Unit, N);
2603 end if;
2604
2605 Formal := New_Copy (Pack_Id);
2606 Create_Instantiation_Source (N, Gen_Unit, False, S_Adjustment);
2607
2608 begin
2609 -- Make local generic without formals. The formals will be replaced
2610 -- with internal declarations.
2611
2612 New_N := Build_Local_Package;
2613
2614 -- If there are errors in the parameter list, Analyze_Associations
2615 -- raises Instantiation_Error. Patch the declaration to prevent
2616 -- further exception propagation.
2617
2618 exception
2619 when Instantiation_Error =>
2620
2621 Enter_Name (Formal);
2622 Set_Ekind (Formal, E_Variable);
2623 Set_Etype (Formal, Any_Type);
2624 Restore_Hidden_Primitives (Vis_Prims_List);
2625
2626 if Parent_Installed then
2627 Remove_Parent;
2628 end if;
2629
2630 goto Leave;
2631 end;
2632
2633 Rewrite (N, New_N);
2634 Set_Defining_Unit_Name (Specification (New_N), Formal);
2635 Set_Generic_Parent (Specification (N), Gen_Unit);
2636 Set_Instance_Env (Gen_Unit, Formal);
2637 Set_Is_Generic_Instance (Formal);
2638
2639 Enter_Name (Formal);
2640 Set_Ekind (Formal, E_Package);
2641 Set_Etype (Formal, Standard_Void_Type);
2642 Set_Inner_Instances (Formal, New_Elmt_List);
2643 Push_Scope (Formal);
2644
2645 -- Manually set the SPARK_Mode from the context because the package
2646 -- declaration is never analyzed.
2647
2648 Set_SPARK_Pragma (Formal, SPARK_Mode_Pragma);
2649 Set_SPARK_Aux_Pragma (Formal, SPARK_Mode_Pragma);
2650 Set_SPARK_Pragma_Inherited (Formal);
2651 Set_SPARK_Aux_Pragma_Inherited (Formal);
2652
2653 if Is_Child_Unit (Gen_Unit) and then Parent_Installed then
2654
2655 -- Similarly, we have to make the name of the formal visible in the
2656 -- parent instance, to resolve properly fully qualified names that
2657 -- may appear in the generic unit. The parent instance has been
2658 -- placed on the scope stack ahead of the current scope.
2659
2660 Parent_Instance := Scope_Stack.Table (Scope_Stack.Last - 1).Entity;
2661
2662 Renaming_In_Par :=
2663 Make_Defining_Identifier (Loc, Chars (Gen_Unit));
2664 Set_Ekind (Renaming_In_Par, E_Package);
2665 Set_Etype (Renaming_In_Par, Standard_Void_Type);
2666 Set_Scope (Renaming_In_Par, Parent_Instance);
2667 Set_Parent (Renaming_In_Par, Parent (Formal));
2668 Set_Renamed_Object (Renaming_In_Par, Formal);
2669 Append_Entity (Renaming_In_Par, Parent_Instance);
2670 end if;
2671
2672 Analyze (Specification (N));
2673
2674 -- The formals for which associations are provided are not visible
2675 -- outside of the formal package. The others are still declared by a
2676 -- formal parameter declaration.
2677
2678 -- If there are no associations, the only local entity to hide is the
2679 -- generated package renaming itself.
2680
2681 declare
2682 E : Entity_Id;
2683
2684 begin
2685 E := First_Entity (Formal);
2686 while Present (E) loop
2687 if Associations and then not Is_Generic_Formal (E) then
2688 Set_Is_Hidden (E);
2689 end if;
2690
2691 if Ekind (E) = E_Package and then Renamed_Entity (E) = Formal then
2692 Set_Is_Hidden (E);
2693 exit;
2694 end if;
2695
2696 Next_Entity (E);
2697 end loop;
2698 end;
2699
2700 End_Package_Scope (Formal);
2701 Restore_Hidden_Primitives (Vis_Prims_List);
2702
2703 if Parent_Installed then
2704 Remove_Parent;
2705 end if;
2706
2707 Restore_Env;
2708
2709 -- Inside the generic unit, the formal package is a regular package, but
2710 -- no body is needed for it. Note that after instantiation, the defining
2711 -- unit name we need is in the new tree and not in the original (see
2712 -- Package_Instantiation). A generic formal package is an instance, and
2713 -- can be used as an actual for an inner instance.
2714
2715 Set_Has_Completion (Formal, True);
2716
2717 -- Add semantic information to the original defining identifier.
2718 -- for ASIS use.
2719
2720 Set_Ekind (Pack_Id, E_Package);
2721 Set_Etype (Pack_Id, Standard_Void_Type);
2722 Set_Scope (Pack_Id, Scope (Formal));
2723 Set_Has_Completion (Pack_Id, True);
2724
2725 <<Leave>>
2726 if Has_Aspects (N) then
2727 Analyze_Aspect_Specifications (N, Pack_Id);
2728 end if;
2729 end Analyze_Formal_Package_Declaration;
2730
2731 ---------------------------------
2732 -- Analyze_Formal_Private_Type --
2733 ---------------------------------
2734
2735 procedure Analyze_Formal_Private_Type
2736 (N : Node_Id;
2737 T : Entity_Id;
2738 Def : Node_Id)
2739 is
2740 begin
2741 New_Private_Type (N, T, Def);
2742
2743 -- Set the size to an arbitrary but legal value
2744
2745 Set_Size_Info (T, Standard_Integer);
2746 Set_RM_Size (T, RM_Size (Standard_Integer));
2747 end Analyze_Formal_Private_Type;
2748
2749 ------------------------------------
2750 -- Analyze_Formal_Incomplete_Type --
2751 ------------------------------------
2752
2753 procedure Analyze_Formal_Incomplete_Type
2754 (T : Entity_Id;
2755 Def : Node_Id)
2756 is
2757 begin
2758 Enter_Name (T);
2759 Set_Ekind (T, E_Incomplete_Type);
2760 Set_Etype (T, T);
2761 Set_Private_Dependents (T, New_Elmt_List);
2762
2763 if Tagged_Present (Def) then
2764 Set_Is_Tagged_Type (T);
2765 Make_Class_Wide_Type (T);
2766 Set_Direct_Primitive_Operations (T, New_Elmt_List);
2767 end if;
2768 end Analyze_Formal_Incomplete_Type;
2769
2770 ----------------------------------------
2771 -- Analyze_Formal_Signed_Integer_Type --
2772 ----------------------------------------
2773
2774 procedure Analyze_Formal_Signed_Integer_Type
2775 (T : Entity_Id;
2776 Def : Node_Id)
2777 is
2778 Base : constant Entity_Id :=
2779 New_Internal_Entity
2780 (E_Signed_Integer_Type,
2781 Current_Scope,
2782 Sloc (Defining_Identifier (Parent (Def))), 'G');
2783
2784 begin
2785 Enter_Name (T);
2786
2787 Set_Ekind (T, E_Signed_Integer_Subtype);
2788 Set_Etype (T, Base);
2789 Set_Size_Info (T, Standard_Integer);
2790 Set_RM_Size (T, RM_Size (Standard_Integer));
2791 Set_Scalar_Range (T, Scalar_Range (Standard_Integer));
2792 Set_Is_Constrained (T);
2793
2794 Set_Is_Generic_Type (Base);
2795 Set_Size_Info (Base, Standard_Integer);
2796 Set_RM_Size (Base, RM_Size (Standard_Integer));
2797 Set_Etype (Base, Base);
2798 Set_Scalar_Range (Base, Scalar_Range (Standard_Integer));
2799 Set_Parent (Base, Parent (Def));
2800 end Analyze_Formal_Signed_Integer_Type;
2801
2802 -------------------------------------------
2803 -- Analyze_Formal_Subprogram_Declaration --
2804 -------------------------------------------
2805
2806 procedure Analyze_Formal_Subprogram_Declaration (N : Node_Id) is
2807 Spec : constant Node_Id := Specification (N);
2808 Def : constant Node_Id := Default_Name (N);
2809 Nam : constant Entity_Id := Defining_Unit_Name (Spec);
2810 Subp : Entity_Id;
2811
2812 begin
2813 if Nam = Error then
2814 return;
2815 end if;
2816
2817 if Nkind (Nam) = N_Defining_Program_Unit_Name then
2818 Error_Msg_N ("name of formal subprogram must be a direct name", Nam);
2819 goto Leave;
2820 end if;
2821
2822 Analyze_Subprogram_Declaration (N);
2823 Set_Is_Formal_Subprogram (Nam);
2824 Set_Has_Completion (Nam);
2825
2826 if Nkind (N) = N_Formal_Abstract_Subprogram_Declaration then
2827 Set_Is_Abstract_Subprogram (Nam);
2828
2829 Set_Is_Dispatching_Operation (Nam);
2830
2831 -- A formal abstract procedure cannot have a null default
2832 -- (RM 12.6(4.1/2)).
2833
2834 if Nkind (Spec) = N_Procedure_Specification
2835 and then Null_Present (Spec)
2836 then
2837 Error_Msg_N
2838 ("a formal abstract subprogram cannot default to null", Spec);
2839 end if;
2840
2841 declare
2842 Ctrl_Type : constant Entity_Id := Find_Dispatching_Type (Nam);
2843 begin
2844 if No (Ctrl_Type) then
2845 Error_Msg_N
2846 ("abstract formal subprogram must have a controlling type",
2847 N);
2848
2849 elsif Ada_Version >= Ada_2012
2850 and then Is_Incomplete_Type (Ctrl_Type)
2851 then
2852 Error_Msg_NE
2853 ("controlling type of abstract formal subprogram cannot "
2854 & "be incomplete type", N, Ctrl_Type);
2855
2856 else
2857 Check_Controlling_Formals (Ctrl_Type, Nam);
2858 end if;
2859 end;
2860 end if;
2861
2862 -- Default name is resolved at the point of instantiation
2863
2864 if Box_Present (N) then
2865 null;
2866
2867 -- Else default is bound at the point of generic declaration
2868
2869 elsif Present (Def) then
2870 if Nkind (Def) = N_Operator_Symbol then
2871 Find_Direct_Name (Def);
2872
2873 elsif Nkind (Def) /= N_Attribute_Reference then
2874 Analyze (Def);
2875
2876 else
2877 -- For an attribute reference, analyze the prefix and verify
2878 -- that it has the proper profile for the subprogram.
2879
2880 Analyze (Prefix (Def));
2881 Valid_Default_Attribute (Nam, Def);
2882 goto Leave;
2883 end if;
2884
2885 -- Default name may be overloaded, in which case the interpretation
2886 -- with the correct profile must be selected, as for a renaming.
2887 -- If the definition is an indexed component, it must denote a
2888 -- member of an entry family. If it is a selected component, it
2889 -- can be a protected operation.
2890
2891 if Etype (Def) = Any_Type then
2892 goto Leave;
2893
2894 elsif Nkind (Def) = N_Selected_Component then
2895 if not Is_Overloadable (Entity (Selector_Name (Def))) then
2896 Error_Msg_N ("expect valid subprogram name as default", Def);
2897 end if;
2898
2899 elsif Nkind (Def) = N_Indexed_Component then
2900 if Is_Entity_Name (Prefix (Def)) then
2901 if Ekind (Entity (Prefix (Def))) /= E_Entry_Family then
2902 Error_Msg_N ("expect valid subprogram name as default", Def);
2903 end if;
2904
2905 elsif Nkind (Prefix (Def)) = N_Selected_Component then
2906 if Ekind (Entity (Selector_Name (Prefix (Def)))) /=
2907 E_Entry_Family
2908 then
2909 Error_Msg_N ("expect valid subprogram name as default", Def);
2910 end if;
2911
2912 else
2913 Error_Msg_N ("expect valid subprogram name as default", Def);
2914 goto Leave;
2915 end if;
2916
2917 elsif Nkind (Def) = N_Character_Literal then
2918
2919 -- Needs some type checks: subprogram should be parameterless???
2920
2921 Resolve (Def, (Etype (Nam)));
2922
2923 elsif not Is_Entity_Name (Def)
2924 or else not Is_Overloadable (Entity (Def))
2925 then
2926 Error_Msg_N ("expect valid subprogram name as default", Def);
2927 goto Leave;
2928
2929 elsif not Is_Overloaded (Def) then
2930 Subp := Entity (Def);
2931
2932 if Subp = Nam then
2933 Error_Msg_N ("premature usage of formal subprogram", Def);
2934
2935 elsif not Entity_Matches_Spec (Subp, Nam) then
2936 Error_Msg_N ("no visible entity matches specification", Def);
2937 end if;
2938
2939 -- More than one interpretation, so disambiguate as for a renaming
2940
2941 else
2942 declare
2943 I : Interp_Index;
2944 I1 : Interp_Index := 0;
2945 It : Interp;
2946 It1 : Interp;
2947
2948 begin
2949 Subp := Any_Id;
2950 Get_First_Interp (Def, I, It);
2951 while Present (It.Nam) loop
2952 if Entity_Matches_Spec (It.Nam, Nam) then
2953 if Subp /= Any_Id then
2954 It1 := Disambiguate (Def, I1, I, Etype (Subp));
2955
2956 if It1 = No_Interp then
2957 Error_Msg_N ("ambiguous default subprogram", Def);
2958 else
2959 Subp := It1.Nam;
2960 end if;
2961
2962 exit;
2963
2964 else
2965 I1 := I;
2966 Subp := It.Nam;
2967 end if;
2968 end if;
2969
2970 Get_Next_Interp (I, It);
2971 end loop;
2972 end;
2973
2974 if Subp /= Any_Id then
2975
2976 -- Subprogram found, generate reference to it
2977
2978 Set_Entity (Def, Subp);
2979 Generate_Reference (Subp, Def);
2980
2981 if Subp = Nam then
2982 Error_Msg_N ("premature usage of formal subprogram", Def);
2983
2984 elsif Ekind (Subp) /= E_Operator then
2985 Check_Mode_Conformant (Subp, Nam);
2986 end if;
2987
2988 else
2989 Error_Msg_N ("no visible subprogram matches specification", N);
2990 end if;
2991 end if;
2992 end if;
2993
2994 <<Leave>>
2995 if Has_Aspects (N) then
2996 Analyze_Aspect_Specifications (N, Nam);
2997 end if;
2998
2999 end Analyze_Formal_Subprogram_Declaration;
3000
3001 -------------------------------------
3002 -- Analyze_Formal_Type_Declaration --
3003 -------------------------------------
3004
3005 procedure Analyze_Formal_Type_Declaration (N : Node_Id) is
3006 Def : constant Node_Id := Formal_Type_Definition (N);
3007 T : Entity_Id;
3008
3009 begin
3010 T := Defining_Identifier (N);
3011
3012 if Present (Discriminant_Specifications (N))
3013 and then Nkind (Def) /= N_Formal_Private_Type_Definition
3014 then
3015 Error_Msg_N
3016 ("discriminants not allowed for this formal type", T);
3017 end if;
3018
3019 -- Enter the new name, and branch to specific routine
3020
3021 case Nkind (Def) is
3022 when N_Formal_Private_Type_Definition =>
3023 Analyze_Formal_Private_Type (N, T, Def);
3024
3025 when N_Formal_Derived_Type_Definition =>
3026 Analyze_Formal_Derived_Type (N, T, Def);
3027
3028 when N_Formal_Incomplete_Type_Definition =>
3029 Analyze_Formal_Incomplete_Type (T, Def);
3030
3031 when N_Formal_Discrete_Type_Definition =>
3032 Analyze_Formal_Discrete_Type (T, Def);
3033
3034 when N_Formal_Signed_Integer_Type_Definition =>
3035 Analyze_Formal_Signed_Integer_Type (T, Def);
3036
3037 when N_Formal_Modular_Type_Definition =>
3038 Analyze_Formal_Modular_Type (T, Def);
3039
3040 when N_Formal_Floating_Point_Definition =>
3041 Analyze_Formal_Floating_Type (T, Def);
3042
3043 when N_Formal_Ordinary_Fixed_Point_Definition =>
3044 Analyze_Formal_Ordinary_Fixed_Point_Type (T, Def);
3045
3046 when N_Formal_Decimal_Fixed_Point_Definition =>
3047 Analyze_Formal_Decimal_Fixed_Point_Type (T, Def);
3048
3049 when N_Array_Type_Definition =>
3050 Analyze_Formal_Array_Type (T, Def);
3051
3052 when N_Access_To_Object_Definition |
3053 N_Access_Function_Definition |
3054 N_Access_Procedure_Definition =>
3055 Analyze_Generic_Access_Type (T, Def);
3056
3057 -- Ada 2005: a interface declaration is encoded as an abstract
3058 -- record declaration or a abstract type derivation.
3059
3060 when N_Record_Definition =>
3061 Analyze_Formal_Interface_Type (N, T, Def);
3062
3063 when N_Derived_Type_Definition =>
3064 Analyze_Formal_Derived_Interface_Type (N, T, Def);
3065
3066 when N_Error =>
3067 null;
3068
3069 when others =>
3070 raise Program_Error;
3071
3072 end case;
3073
3074 Set_Is_Generic_Type (T);
3075
3076 if Has_Aspects (N) then
3077 Analyze_Aspect_Specifications (N, T);
3078 end if;
3079 end Analyze_Formal_Type_Declaration;
3080
3081 ------------------------------------
3082 -- Analyze_Function_Instantiation --
3083 ------------------------------------
3084
3085 procedure Analyze_Function_Instantiation (N : Node_Id) is
3086 begin
3087 Analyze_Subprogram_Instantiation (N, E_Function);
3088 end Analyze_Function_Instantiation;
3089
3090 ---------------------------------
3091 -- Analyze_Generic_Access_Type --
3092 ---------------------------------
3093
3094 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id) is
3095 begin
3096 Enter_Name (T);
3097
3098 if Nkind (Def) = N_Access_To_Object_Definition then
3099 Access_Type_Declaration (T, Def);
3100
3101 if Is_Incomplete_Or_Private_Type (Designated_Type (T))
3102 and then No (Full_View (Designated_Type (T)))
3103 and then not Is_Generic_Type (Designated_Type (T))
3104 then
3105 Error_Msg_N ("premature usage of incomplete type", Def);
3106
3107 elsif not Is_Entity_Name (Subtype_Indication (Def)) then
3108 Error_Msg_N
3109 ("only a subtype mark is allowed in a formal", Def);
3110 end if;
3111
3112 else
3113 Access_Subprogram_Declaration (T, Def);
3114 end if;
3115 end Analyze_Generic_Access_Type;
3116
3117 ---------------------------------
3118 -- Analyze_Generic_Formal_Part --
3119 ---------------------------------
3120
3121 procedure Analyze_Generic_Formal_Part (N : Node_Id) is
3122 Gen_Parm_Decl : Node_Id;
3123
3124 begin
3125 -- The generic formals are processed in the scope of the generic unit,
3126 -- where they are immediately visible. The scope is installed by the
3127 -- caller.
3128
3129 Gen_Parm_Decl := First (Generic_Formal_Declarations (N));
3130 while Present (Gen_Parm_Decl) loop
3131 Analyze (Gen_Parm_Decl);
3132 Next (Gen_Parm_Decl);
3133 end loop;
3134
3135 Generate_Reference_To_Generic_Formals (Current_Scope);
3136 end Analyze_Generic_Formal_Part;
3137
3138 ------------------------------------------
3139 -- Analyze_Generic_Package_Declaration --
3140 ------------------------------------------
3141
3142 procedure Analyze_Generic_Package_Declaration (N : Node_Id) is
3143 Loc : constant Source_Ptr := Sloc (N);
3144 Decls : constant List_Id :=
3145 Visible_Declarations (Specification (N));
3146 Decl : Node_Id;
3147 Id : Entity_Id;
3148 New_N : Node_Id;
3149 Renaming : Node_Id;
3150 Save_Parent : Node_Id;
3151
3152 begin
3153 Check_SPARK_05_Restriction ("generic is not allowed", N);
3154
3155 -- We introduce a renaming of the enclosing package, to have a usable
3156 -- entity as the prefix of an expanded name for a local entity of the
3157 -- form Par.P.Q, where P is the generic package. This is because a local
3158 -- entity named P may hide it, so that the usual visibility rules in
3159 -- the instance will not resolve properly.
3160
3161 Renaming :=
3162 Make_Package_Renaming_Declaration (Loc,
3163 Defining_Unit_Name =>
3164 Make_Defining_Identifier (Loc,
3165 Chars => New_External_Name (Chars (Defining_Entity (N)), "GH")),
3166 Name =>
3167 Make_Identifier (Loc, Chars (Defining_Entity (N))));
3168
3169 if Present (Decls) then
3170 Decl := First (Decls);
3171 while Present (Decl) and then Nkind (Decl) = N_Pragma loop
3172 Next (Decl);
3173 end loop;
3174
3175 if Present (Decl) then
3176 Insert_Before (Decl, Renaming);
3177 else
3178 Append (Renaming, Visible_Declarations (Specification (N)));
3179 end if;
3180
3181 else
3182 Set_Visible_Declarations (Specification (N), New_List (Renaming));
3183 end if;
3184
3185 -- Create copy of generic unit, and save for instantiation. If the unit
3186 -- is a child unit, do not copy the specifications for the parent, which
3187 -- are not part of the generic tree.
3188
3189 Save_Parent := Parent_Spec (N);
3190 Set_Parent_Spec (N, Empty);
3191
3192 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
3193 Set_Parent_Spec (New_N, Save_Parent);
3194 Rewrite (N, New_N);
3195
3196 -- Once the contents of the generic copy and the template are swapped,
3197 -- do the same for their respective aspect specifications.
3198
3199 Exchange_Aspects (N, New_N);
3200
3201 -- Collect all contract-related source pragmas found within the template
3202 -- and attach them to the contract of the package spec. This contract is
3203 -- used in the capture of global references within annotations.
3204
3205 Create_Generic_Contract (N);
3206
3207 Id := Defining_Entity (N);
3208 Generate_Definition (Id);
3209
3210 -- Expansion is not applied to generic units
3211
3212 Start_Generic;
3213
3214 Enter_Name (Id);
3215 Set_Ekind (Id, E_Generic_Package);
3216 Set_Etype (Id, Standard_Void_Type);
3217
3218 -- A generic package declared within a Ghost region is rendered Ghost
3219 -- (SPARK RM 6.9(2)).
3220
3221 if Ghost_Mode > None then
3222 Set_Is_Ghost_Entity (Id);
3223 end if;
3224
3225 -- Analyze aspects now, so that generated pragmas appear in the
3226 -- declarations before building and analyzing the generic copy.
3227
3228 if Has_Aspects (N) then
3229 Analyze_Aspect_Specifications (N, Id);
3230 end if;
3231
3232 Push_Scope (Id);
3233 Enter_Generic_Scope (Id);
3234 Set_Inner_Instances (Id, New_Elmt_List);
3235
3236 Set_Categorization_From_Pragmas (N);
3237 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3238
3239 -- Link the declaration of the generic homonym in the generic copy to
3240 -- the package it renames, so that it is always resolved properly.
3241
3242 Set_Generic_Homonym (Id, Defining_Unit_Name (Renaming));
3243 Set_Entity (Associated_Node (Name (Renaming)), Id);
3244
3245 -- For a library unit, we have reconstructed the entity for the unit,
3246 -- and must reset it in the library tables.
3247
3248 if Nkind (Parent (N)) = N_Compilation_Unit then
3249 Set_Cunit_Entity (Current_Sem_Unit, Id);
3250 end if;
3251
3252 Analyze_Generic_Formal_Part (N);
3253
3254 -- After processing the generic formals, analysis proceeds as for a
3255 -- non-generic package.
3256
3257 Analyze (Specification (N));
3258
3259 Validate_Categorization_Dependency (N, Id);
3260
3261 End_Generic;
3262
3263 End_Package_Scope (Id);
3264 Exit_Generic_Scope (Id);
3265
3266 if Nkind (Parent (N)) /= N_Compilation_Unit then
3267 Move_Freeze_Nodes (Id, N, Visible_Declarations (Specification (N)));
3268 Move_Freeze_Nodes (Id, N, Private_Declarations (Specification (N)));
3269 Move_Freeze_Nodes (Id, N, Generic_Formal_Declarations (N));
3270
3271 else
3272 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
3273 Validate_RT_RAT_Component (N);
3274
3275 -- If this is a spec without a body, check that generic parameters
3276 -- are referenced.
3277
3278 if not Body_Required (Parent (N)) then
3279 Check_References (Id);
3280 end if;
3281 end if;
3282
3283 -- If there is a specified storage pool in the context, create an
3284 -- aspect on the package declaration, so that it is used in any
3285 -- instance that does not override it.
3286
3287 if Present (Default_Pool) then
3288 declare
3289 ASN : Node_Id;
3290
3291 begin
3292 ASN :=
3293 Make_Aspect_Specification (Loc,
3294 Identifier => Make_Identifier (Loc, Name_Default_Storage_Pool),
3295 Expression => New_Copy (Default_Pool));
3296
3297 if No (Aspect_Specifications (Specification (N))) then
3298 Set_Aspect_Specifications (Specification (N), New_List (ASN));
3299 else
3300 Append (ASN, Aspect_Specifications (Specification (N)));
3301 end if;
3302 end;
3303 end if;
3304 end Analyze_Generic_Package_Declaration;
3305
3306 --------------------------------------------
3307 -- Analyze_Generic_Subprogram_Declaration --
3308 --------------------------------------------
3309
3310 procedure Analyze_Generic_Subprogram_Declaration (N : Node_Id) is
3311 Formals : List_Id;
3312 Id : Entity_Id;
3313 New_N : Node_Id;
3314 Result_Type : Entity_Id;
3315 Save_Parent : Node_Id;
3316 Spec : Node_Id;
3317 Typ : Entity_Id;
3318
3319 begin
3320 Check_SPARK_05_Restriction ("generic is not allowed", N);
3321
3322 -- Create copy of generic unit, and save for instantiation. If the unit
3323 -- is a child unit, do not copy the specifications for the parent, which
3324 -- are not part of the generic tree.
3325
3326 Save_Parent := Parent_Spec (N);
3327 Set_Parent_Spec (N, Empty);
3328
3329 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
3330 Set_Parent_Spec (New_N, Save_Parent);
3331 Rewrite (N, New_N);
3332
3333 -- Once the contents of the generic copy and the template are swapped,
3334 -- do the same for their respective aspect specifications.
3335
3336 Exchange_Aspects (N, New_N);
3337
3338 -- Collect all contract-related source pragmas found within the template
3339 -- and attach them to the contract of the subprogram spec. This contract
3340 -- is used in the capture of global references within annotations.
3341
3342 Create_Generic_Contract (N);
3343
3344 Spec := Specification (N);
3345 Id := Defining_Entity (Spec);
3346 Generate_Definition (Id);
3347
3348 if Nkind (Id) = N_Defining_Operator_Symbol then
3349 Error_Msg_N
3350 ("operator symbol not allowed for generic subprogram", Id);
3351 end if;
3352
3353 Start_Generic;
3354
3355 Enter_Name (Id);
3356 Set_Scope_Depth_Value (Id, Scope_Depth (Current_Scope) + 1);
3357
3358 -- Analyze the aspects of the generic copy to ensure that all generated
3359 -- pragmas (if any) perform their semantic effects.
3360
3361 if Has_Aspects (N) then
3362 Analyze_Aspect_Specifications (N, Id);
3363 end if;
3364
3365 Push_Scope (Id);
3366 Enter_Generic_Scope (Id);
3367 Set_Inner_Instances (Id, New_Elmt_List);
3368 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3369
3370 Analyze_Generic_Formal_Part (N);
3371
3372 Formals := Parameter_Specifications (Spec);
3373
3374 if Nkind (Spec) = N_Function_Specification then
3375 Set_Ekind (Id, E_Generic_Function);
3376 else
3377 Set_Ekind (Id, E_Generic_Procedure);
3378 end if;
3379
3380 if Present (Formals) then
3381 Process_Formals (Formals, Spec);
3382 end if;
3383
3384 if Nkind (Spec) = N_Function_Specification then
3385 if Nkind (Result_Definition (Spec)) = N_Access_Definition then
3386 Result_Type := Access_Definition (Spec, Result_Definition (Spec));
3387 Set_Etype (Id, Result_Type);
3388
3389 -- Check restriction imposed by AI05-073: a generic function
3390 -- cannot return an abstract type or an access to such.
3391
3392 -- This is a binding interpretation should it apply to earlier
3393 -- versions of Ada as well as Ada 2012???
3394
3395 if Is_Abstract_Type (Designated_Type (Result_Type))
3396 and then Ada_Version >= Ada_2012
3397 then
3398 Error_Msg_N
3399 ("generic function cannot have an access result "
3400 & "that designates an abstract type", Spec);
3401 end if;
3402
3403 else
3404 Find_Type (Result_Definition (Spec));
3405 Typ := Entity (Result_Definition (Spec));
3406
3407 if Is_Abstract_Type (Typ)
3408 and then Ada_Version >= Ada_2012
3409 then
3410 Error_Msg_N
3411 ("generic function cannot have abstract result type", Spec);
3412 end if;
3413
3414 -- If a null exclusion is imposed on the result type, then create
3415 -- a null-excluding itype (an access subtype) and use it as the
3416 -- function's Etype.
3417
3418 if Is_Access_Type (Typ)
3419 and then Null_Exclusion_Present (Spec)
3420 then
3421 Set_Etype (Id,
3422 Create_Null_Excluding_Itype
3423 (T => Typ,
3424 Related_Nod => Spec,
3425 Scope_Id => Defining_Unit_Name (Spec)));
3426 else
3427 Set_Etype (Id, Typ);
3428 end if;
3429 end if;
3430
3431 else
3432 Set_Etype (Id, Standard_Void_Type);
3433 end if;
3434
3435 -- A generic subprogram declared within a Ghost region is rendered Ghost
3436 -- (SPARK RM 6.9(2)).
3437
3438 if Ghost_Mode > None then
3439 Set_Is_Ghost_Entity (Id);
3440 end if;
3441
3442 -- For a library unit, we have reconstructed the entity for the unit,
3443 -- and must reset it in the library tables. We also make sure that
3444 -- Body_Required is set properly in the original compilation unit node.
3445
3446 if Nkind (Parent (N)) = N_Compilation_Unit then
3447 Set_Cunit_Entity (Current_Sem_Unit, Id);
3448 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
3449 end if;
3450
3451 Set_Categorization_From_Pragmas (N);
3452 Validate_Categorization_Dependency (N, Id);
3453
3454 -- Capture all global references that occur within the profile of the
3455 -- generic subprogram. Aspects are not part of this processing because
3456 -- they must be delayed. If processed now, Save_Global_References will
3457 -- destroy the Associated_Node links and prevent the capture of global
3458 -- references when the contract of the generic subprogram is analyzed.
3459
3460 Save_Global_References (Original_Node (N));
3461
3462 End_Generic;
3463 End_Scope;
3464 Exit_Generic_Scope (Id);
3465 Generate_Reference_To_Formals (Id);
3466
3467 List_Inherited_Pre_Post_Aspects (Id);
3468 end Analyze_Generic_Subprogram_Declaration;
3469
3470 -----------------------------------
3471 -- Analyze_Package_Instantiation --
3472 -----------------------------------
3473
3474 procedure Analyze_Package_Instantiation (N : Node_Id) is
3475 Loc : constant Source_Ptr := Sloc (N);
3476 Gen_Id : constant Node_Id := Name (N);
3477
3478 Act_Decl : Node_Id;
3479 Act_Decl_Name : Node_Id;
3480 Act_Decl_Id : Entity_Id;
3481 Act_Spec : Node_Id;
3482 Act_Tree : Node_Id;
3483
3484 Gen_Decl : Node_Id;
3485 Gen_Spec : Node_Id;
3486 Gen_Unit : Entity_Id;
3487
3488 Is_Actual_Pack : constant Boolean :=
3489 Is_Internal (Defining_Entity (N));
3490
3491 Env_Installed : Boolean := False;
3492 Parent_Installed : Boolean := False;
3493 Renaming_List : List_Id;
3494 Unit_Renaming : Node_Id;
3495 Needs_Body : Boolean;
3496 Inline_Now : Boolean := False;
3497 Has_Inline_Always : Boolean := False;
3498
3499 Save_IPSM : constant Boolean := Ignore_Pragma_SPARK_Mode;
3500 -- Save flag Ignore_Pragma_SPARK_Mode for restore on exit
3501
3502 Save_SM : constant SPARK_Mode_Type := SPARK_Mode;
3503 Save_SMP : constant Node_Id := SPARK_Mode_Pragma;
3504 -- Save the SPARK_Mode-related data for restore on exit
3505
3506 Save_Style_Check : constant Boolean := Style_Check;
3507 -- Save style check mode for restore on exit
3508
3509 procedure Delay_Descriptors (E : Entity_Id);
3510 -- Delay generation of subprogram descriptors for given entity
3511
3512 function Might_Inline_Subp return Boolean;
3513 -- If inlining is active and the generic contains inlined subprograms,
3514 -- we instantiate the body. This may cause superfluous instantiations,
3515 -- but it is simpler than detecting the need for the body at the point
3516 -- of inlining, when the context of the instance is not available.
3517
3518 -----------------------
3519 -- Delay_Descriptors --
3520 -----------------------
3521
3522 procedure Delay_Descriptors (E : Entity_Id) is
3523 begin
3524 if not Delay_Subprogram_Descriptors (E) then
3525 Set_Delay_Subprogram_Descriptors (E);
3526 Pending_Descriptor.Append (E);
3527 end if;
3528 end Delay_Descriptors;
3529
3530 -----------------------
3531 -- Might_Inline_Subp --
3532 -----------------------
3533
3534 function Might_Inline_Subp return Boolean is
3535 E : Entity_Id;
3536
3537 begin
3538 if not Inline_Processing_Required then
3539 return False;
3540
3541 else
3542 E := First_Entity (Gen_Unit);
3543 while Present (E) loop
3544 if Is_Subprogram (E) and then Is_Inlined (E) then
3545 -- Remember if there are any subprograms with Inline_Always
3546
3547 if Has_Pragma_Inline_Always (E) then
3548 Has_Inline_Always := True;
3549 end if;
3550
3551 return True;
3552 end if;
3553
3554 Next_Entity (E);
3555 end loop;
3556 end if;
3557
3558 return False;
3559 end Might_Inline_Subp;
3560
3561 -- Local declarations
3562
3563 Vis_Prims_List : Elist_Id := No_Elist;
3564 -- List of primitives made temporarily visible in the instantiation
3565 -- to match the visibility of the formal type
3566
3567 -- Start of processing for Analyze_Package_Instantiation
3568
3569 begin
3570 Check_SPARK_05_Restriction ("generic is not allowed", N);
3571
3572 -- Very first thing: check for Text_IO sp[ecial unit in case we are
3573 -- instantiating one of the children of [[Wide_]Wide_]Text_IO.
3574
3575 Check_Text_IO_Special_Unit (Name (N));
3576
3577 -- Make node global for error reporting
3578
3579 Instantiation_Node := N;
3580
3581 -- Turn off style checking in instances. If the check is enabled on the
3582 -- generic unit, a warning in an instance would just be noise. If not
3583 -- enabled on the generic, then a warning in an instance is just wrong.
3584
3585 Style_Check := False;
3586
3587 -- Case of instantiation of a generic package
3588
3589 if Nkind (N) = N_Package_Instantiation then
3590 Act_Decl_Id := New_Copy (Defining_Entity (N));
3591 Set_Comes_From_Source (Act_Decl_Id, True);
3592
3593 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name then
3594 Act_Decl_Name :=
3595 Make_Defining_Program_Unit_Name (Loc,
3596 Name =>
3597 New_Copy_Tree (Name (Defining_Unit_Name (N))),
3598 Defining_Identifier => Act_Decl_Id);
3599 else
3600 Act_Decl_Name := Act_Decl_Id;
3601 end if;
3602
3603 -- Case of instantiation of a formal package
3604
3605 else
3606 Act_Decl_Id := Defining_Identifier (N);
3607 Act_Decl_Name := Act_Decl_Id;
3608 end if;
3609
3610 Generate_Definition (Act_Decl_Id);
3611 Set_Ekind (Act_Decl_Id, E_Package);
3612
3613 -- Initialize list of incomplete actuals before analysis
3614
3615 Set_Incomplete_Actuals (Act_Decl_Id, New_Elmt_List);
3616
3617 Preanalyze_Actuals (N, Act_Decl_Id);
3618
3619 Init_Env;
3620 Env_Installed := True;
3621
3622 -- Reset renaming map for formal types. The mapping is established
3623 -- when analyzing the generic associations, but some mappings are
3624 -- inherited from formal packages of parent units, and these are
3625 -- constructed when the parents are installed.
3626
3627 Generic_Renamings.Set_Last (0);
3628 Generic_Renamings_HTable.Reset;
3629
3630 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
3631 Gen_Unit := Entity (Gen_Id);
3632
3633 -- Verify that it is the name of a generic package
3634
3635 -- A visibility glitch: if the instance is a child unit and the generic
3636 -- is the generic unit of a parent instance (i.e. both the parent and
3637 -- the child units are instances of the same package) the name now
3638 -- denotes the renaming within the parent, not the intended generic
3639 -- unit. See if there is a homonym that is the desired generic. The
3640 -- renaming declaration must be visible inside the instance of the
3641 -- child, but not when analyzing the name in the instantiation itself.
3642
3643 if Ekind (Gen_Unit) = E_Package
3644 and then Present (Renamed_Entity (Gen_Unit))
3645 and then In_Open_Scopes (Renamed_Entity (Gen_Unit))
3646 and then Is_Generic_Instance (Renamed_Entity (Gen_Unit))
3647 and then Present (Homonym (Gen_Unit))
3648 then
3649 Gen_Unit := Homonym (Gen_Unit);
3650 end if;
3651
3652 if Etype (Gen_Unit) = Any_Type then
3653 Restore_Env;
3654 goto Leave;
3655
3656 elsif Ekind (Gen_Unit) /= E_Generic_Package then
3657
3658 -- Ada 2005 (AI-50217): Cannot use instance in limited with_clause
3659
3660 if From_Limited_With (Gen_Unit) then
3661 Error_Msg_N
3662 ("cannot instantiate a limited withed package", Gen_Id);
3663 else
3664 Error_Msg_NE
3665 ("& is not the name of a generic package", Gen_Id, Gen_Unit);
3666 end if;
3667
3668 Restore_Env;
3669 goto Leave;
3670 end if;
3671
3672 if In_Extended_Main_Source_Unit (N) then
3673 Set_Is_Instantiated (Gen_Unit);
3674 Generate_Reference (Gen_Unit, N);
3675
3676 if Present (Renamed_Object (Gen_Unit)) then
3677 Set_Is_Instantiated (Renamed_Object (Gen_Unit));
3678 Generate_Reference (Renamed_Object (Gen_Unit), N);
3679 end if;
3680 end if;
3681
3682 if Nkind (Gen_Id) = N_Identifier
3683 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
3684 then
3685 Error_Msg_NE
3686 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
3687
3688 elsif Nkind (Gen_Id) = N_Expanded_Name
3689 and then Is_Child_Unit (Gen_Unit)
3690 and then Nkind (Prefix (Gen_Id)) = N_Identifier
3691 and then Chars (Act_Decl_Id) = Chars (Prefix (Gen_Id))
3692 then
3693 Error_Msg_N
3694 ("& is hidden within declaration of instance ", Prefix (Gen_Id));
3695 end if;
3696
3697 Set_Entity (Gen_Id, Gen_Unit);
3698
3699 -- If generic is a renaming, get original generic unit
3700
3701 if Present (Renamed_Object (Gen_Unit))
3702 and then Ekind (Renamed_Object (Gen_Unit)) = E_Generic_Package
3703 then
3704 Gen_Unit := Renamed_Object (Gen_Unit);
3705 end if;
3706
3707 -- Verify that there are no circular instantiations
3708
3709 if In_Open_Scopes (Gen_Unit) then
3710 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
3711 Restore_Env;
3712 goto Leave;
3713
3714 elsif Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
3715 Error_Msg_Node_2 := Current_Scope;
3716 Error_Msg_NE
3717 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
3718 Circularity_Detected := True;
3719 Restore_Env;
3720 goto Leave;
3721
3722 else
3723 -- If the context of the instance is subject to SPARK_Mode "off" or
3724 -- the annotation is altogether missing, set the global flag which
3725 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
3726 -- the instance.
3727
3728 if SPARK_Mode /= On then
3729 Ignore_Pragma_SPARK_Mode := True;
3730 end if;
3731
3732 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
3733 Gen_Spec := Specification (Gen_Decl);
3734
3735 -- Initialize renamings map, for error checking, and the list that
3736 -- holds private entities whose views have changed between generic
3737 -- definition and instantiation. If this is the instance created to
3738 -- validate an actual package, the instantiation environment is that
3739 -- of the enclosing instance.
3740
3741 Create_Instantiation_Source (N, Gen_Unit, False, S_Adjustment);
3742
3743 -- Copy original generic tree, to produce text for instantiation
3744
3745 Act_Tree :=
3746 Copy_Generic_Node
3747 (Original_Node (Gen_Decl), Empty, Instantiating => True);
3748
3749 Act_Spec := Specification (Act_Tree);
3750
3751 -- If this is the instance created to validate an actual package,
3752 -- only the formals matter, do not examine the package spec itself.
3753
3754 if Is_Actual_Pack then
3755 Set_Visible_Declarations (Act_Spec, New_List);
3756 Set_Private_Declarations (Act_Spec, New_List);
3757 end if;
3758
3759 Renaming_List :=
3760 Analyze_Associations
3761 (I_Node => N,
3762 Formals => Generic_Formal_Declarations (Act_Tree),
3763 F_Copy => Generic_Formal_Declarations (Gen_Decl));
3764
3765 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
3766
3767 Set_Instance_Env (Gen_Unit, Act_Decl_Id);
3768 Set_Defining_Unit_Name (Act_Spec, Act_Decl_Name);
3769 Set_Is_Generic_Instance (Act_Decl_Id);
3770 Set_Generic_Parent (Act_Spec, Gen_Unit);
3771
3772 -- References to the generic in its own declaration or its body are
3773 -- references to the instance. Add a renaming declaration for the
3774 -- generic unit itself. This declaration, as well as the renaming
3775 -- declarations for the generic formals, must remain private to the
3776 -- unit: the formals, because this is the language semantics, and
3777 -- the unit because its use is an artifact of the implementation.
3778
3779 Unit_Renaming :=
3780 Make_Package_Renaming_Declaration (Loc,
3781 Defining_Unit_Name =>
3782 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
3783 Name => New_Occurrence_Of (Act_Decl_Id, Loc));
3784
3785 Append (Unit_Renaming, Renaming_List);
3786
3787 -- The renaming declarations are the first local declarations of the
3788 -- new unit.
3789
3790 if Is_Non_Empty_List (Visible_Declarations (Act_Spec)) then
3791 Insert_List_Before
3792 (First (Visible_Declarations (Act_Spec)), Renaming_List);
3793 else
3794 Set_Visible_Declarations (Act_Spec, Renaming_List);
3795 end if;
3796
3797 Act_Decl := Make_Package_Declaration (Loc, Specification => Act_Spec);
3798
3799 -- Propagate the aspect specifications from the package declaration
3800 -- template to the instantiated version of the package declaration.
3801
3802 if Has_Aspects (Act_Tree) then
3803 Set_Aspect_Specifications (Act_Decl,
3804 New_Copy_List_Tree (Aspect_Specifications (Act_Tree)));
3805 end if;
3806
3807 -- The generic may have a generated Default_Storage_Pool aspect,
3808 -- set at the point of generic declaration. If the instance has
3809 -- that aspect, it overrides the one inherited from the generic.
3810
3811 if Has_Aspects (Gen_Spec) then
3812 if No (Aspect_Specifications (N)) then
3813 Set_Aspect_Specifications (N,
3814 (New_Copy_List_Tree
3815 (Aspect_Specifications (Gen_Spec))));
3816
3817 else
3818 declare
3819 ASN1, ASN2 : Node_Id;
3820
3821 begin
3822 ASN1 := First (Aspect_Specifications (N));
3823 while Present (ASN1) loop
3824 if Chars (Identifier (ASN1)) = Name_Default_Storage_Pool
3825 then
3826 -- If generic carries a default storage pool, remove
3827 -- it in favor of the instance one.
3828
3829 ASN2 := First (Aspect_Specifications (Gen_Spec));
3830 while Present (ASN2) loop
3831 if Chars (Identifier (ASN2)) =
3832 Name_Default_Storage_Pool
3833 then
3834 Remove (ASN2);
3835 exit;
3836 end if;
3837
3838 Next (ASN2);
3839 end loop;
3840 end if;
3841
3842 Next (ASN1);
3843 end loop;
3844
3845 Prepend_List_To (Aspect_Specifications (N),
3846 (New_Copy_List_Tree
3847 (Aspect_Specifications (Gen_Spec))));
3848 end;
3849 end if;
3850 end if;
3851
3852 -- Save the instantiation node, for subsequent instantiation of the
3853 -- body, if there is one and we are generating code for the current
3854 -- unit. Mark unit as having a body (avoids premature error message).
3855
3856 -- We instantiate the body if we are generating code, if we are
3857 -- generating cross-reference information, or if we are building
3858 -- trees for ASIS use or GNATprove use.
3859
3860 declare
3861 Enclosing_Body_Present : Boolean := False;
3862 -- If the generic unit is not a compilation unit, then a body may
3863 -- be present in its parent even if none is required. We create a
3864 -- tentative pending instantiation for the body, which will be
3865 -- discarded if none is actually present.
3866
3867 Scop : Entity_Id;
3868
3869 begin
3870 if Scope (Gen_Unit) /= Standard_Standard
3871 and then not Is_Child_Unit (Gen_Unit)
3872 then
3873 Scop := Scope (Gen_Unit);
3874 while Present (Scop) and then Scop /= Standard_Standard loop
3875 if Unit_Requires_Body (Scop) then
3876 Enclosing_Body_Present := True;
3877 exit;
3878
3879 elsif In_Open_Scopes (Scop)
3880 and then In_Package_Body (Scop)
3881 then
3882 Enclosing_Body_Present := True;
3883 exit;
3884 end if;
3885
3886 exit when Is_Compilation_Unit (Scop);
3887 Scop := Scope (Scop);
3888 end loop;
3889 end if;
3890
3891 -- If front-end inlining is enabled or there are any subprograms
3892 -- marked with Inline_Always, and this is a unit for which code
3893 -- will be generated, we instantiate the body at once.
3894
3895 -- This is done if the instance is not the main unit, and if the
3896 -- generic is not a child unit of another generic, to avoid scope
3897 -- problems and the reinstallation of parent instances.
3898
3899 if Expander_Active
3900 and then (not Is_Child_Unit (Gen_Unit)
3901 or else not Is_Generic_Unit (Scope (Gen_Unit)))
3902 and then Might_Inline_Subp
3903 and then not Is_Actual_Pack
3904 then
3905 if not Back_End_Inlining
3906 and then (Front_End_Inlining or else Has_Inline_Always)
3907 and then (Is_In_Main_Unit (N)
3908 or else In_Main_Context (Current_Scope))
3909 and then Nkind (Parent (N)) /= N_Compilation_Unit
3910 then
3911 Inline_Now := True;
3912
3913 -- In configurable_run_time mode we force the inlining of
3914 -- predefined subprograms marked Inline_Always, to minimize
3915 -- the use of the run-time library.
3916
3917 elsif Is_Predefined_File_Name
3918 (Unit_File_Name (Get_Source_Unit (Gen_Decl)))
3919 and then Configurable_Run_Time_Mode
3920 and then Nkind (Parent (N)) /= N_Compilation_Unit
3921 then
3922 Inline_Now := True;
3923 end if;
3924
3925 -- If the current scope is itself an instance within a child
3926 -- unit, there will be duplications in the scope stack, and the
3927 -- unstacking mechanism in Inline_Instance_Body will fail.
3928 -- This loses some rare cases of optimization, and might be
3929 -- improved some day, if we can find a proper abstraction for
3930 -- "the complete compilation context" that can be saved and
3931 -- restored. ???
3932
3933 if Is_Generic_Instance (Current_Scope) then
3934 declare
3935 Curr_Unit : constant Entity_Id :=
3936 Cunit_Entity (Current_Sem_Unit);
3937 begin
3938 if Curr_Unit /= Current_Scope
3939 and then Is_Child_Unit (Curr_Unit)
3940 then
3941 Inline_Now := False;
3942 end if;
3943 end;
3944 end if;
3945 end if;
3946
3947 Needs_Body :=
3948 (Unit_Requires_Body (Gen_Unit)
3949 or else Enclosing_Body_Present
3950 or else Present (Corresponding_Body (Gen_Decl)))
3951 and then (Is_In_Main_Unit (N) or else Might_Inline_Subp)
3952 and then not Is_Actual_Pack
3953 and then not Inline_Now
3954 and then (Operating_Mode = Generate_Code
3955
3956 -- Need comment for this check ???
3957
3958 or else (Operating_Mode = Check_Semantics
3959 and then (ASIS_Mode or GNATprove_Mode)));
3960
3961 -- If front-end inlining is enabled or there are any subprograms
3962 -- marked with Inline_Always, do not instantiate body when within
3963 -- a generic context.
3964
3965 if ((Front_End_Inlining or else Has_Inline_Always)
3966 and then not Expander_Active)
3967 or else Is_Generic_Unit (Cunit_Entity (Main_Unit))
3968 then
3969 Needs_Body := False;
3970 end if;
3971
3972 -- If the current context is generic, and the package being
3973 -- instantiated is declared within a formal package, there is no
3974 -- body to instantiate until the enclosing generic is instantiated
3975 -- and there is an actual for the formal package. If the formal
3976 -- package has parameters, we build a regular package instance for
3977 -- it, that precedes the original formal package declaration.
3978
3979 if In_Open_Scopes (Scope (Scope (Gen_Unit))) then
3980 declare
3981 Decl : constant Node_Id :=
3982 Original_Node
3983 (Unit_Declaration_Node (Scope (Gen_Unit)));
3984 begin
3985 if Nkind (Decl) = N_Formal_Package_Declaration
3986 or else (Nkind (Decl) = N_Package_Declaration
3987 and then Is_List_Member (Decl)
3988 and then Present (Next (Decl))
3989 and then
3990 Nkind (Next (Decl)) =
3991 N_Formal_Package_Declaration)
3992 then
3993 Needs_Body := False;
3994 end if;
3995 end;
3996 end if;
3997 end;
3998
3999 -- For RCI unit calling stubs, we omit the instance body if the
4000 -- instance is the RCI library unit itself.
4001
4002 -- However there is a special case for nested instances: in this case
4003 -- we do generate the instance body, as it might be required, e.g.
4004 -- because it provides stream attributes for some type used in the
4005 -- profile of a remote subprogram. This is consistent with 12.3(12),
4006 -- which indicates that the instance body occurs at the place of the
4007 -- instantiation, and thus is part of the RCI declaration, which is
4008 -- present on all client partitions (this is E.2.3(18)).
4009
4010 -- Note that AI12-0002 may make it illegal at some point to have
4011 -- stream attributes defined in an RCI unit, in which case this
4012 -- special case will become unnecessary. In the meantime, there
4013 -- is known application code in production that depends on this
4014 -- being possible, so we definitely cannot eliminate the body in
4015 -- the case of nested instances for the time being.
4016
4017 -- When we generate a nested instance body, calling stubs for any
4018 -- relevant subprogram will be be inserted immediately after the
4019 -- subprogram declarations, and will take precedence over the
4020 -- subsequent (original) body. (The stub and original body will be
4021 -- complete homographs, but this is permitted in an instance).
4022 -- (Could we do better and remove the original body???)
4023
4024 if Distribution_Stub_Mode = Generate_Caller_Stub_Body
4025 and then Comes_From_Source (N)
4026 and then Nkind (Parent (N)) = N_Compilation_Unit
4027 then
4028 Needs_Body := False;
4029 end if;
4030
4031 if Needs_Body then
4032
4033 -- Here is a defence against a ludicrous number of instantiations
4034 -- caused by a circular set of instantiation attempts.
4035
4036 if Pending_Instantiations.Last > Maximum_Instantiations then
4037 Error_Msg_Uint_1 := UI_From_Int (Maximum_Instantiations);
4038 Error_Msg_N ("too many instantiations, exceeds max of^", N);
4039 Error_Msg_N ("\limit can be changed using -gnateinn switch", N);
4040 raise Unrecoverable_Error;
4041 end if;
4042
4043 -- Indicate that the enclosing scopes contain an instantiation,
4044 -- and that cleanup actions should be delayed until after the
4045 -- instance body is expanded.
4046
4047 Check_Forward_Instantiation (Gen_Decl);
4048 if Nkind (N) = N_Package_Instantiation then
4049 declare
4050 Enclosing_Master : Entity_Id;
4051
4052 begin
4053 -- Loop to search enclosing masters
4054
4055 Enclosing_Master := Current_Scope;
4056 Scope_Loop : while Enclosing_Master /= Standard_Standard loop
4057 if Ekind (Enclosing_Master) = E_Package then
4058 if Is_Compilation_Unit (Enclosing_Master) then
4059 if In_Package_Body (Enclosing_Master) then
4060 Delay_Descriptors
4061 (Body_Entity (Enclosing_Master));
4062 else
4063 Delay_Descriptors
4064 (Enclosing_Master);
4065 end if;
4066
4067 exit Scope_Loop;
4068
4069 else
4070 Enclosing_Master := Scope (Enclosing_Master);
4071 end if;
4072
4073 elsif Is_Generic_Unit (Enclosing_Master)
4074 or else Ekind (Enclosing_Master) = E_Void
4075 then
4076 -- Cleanup actions will eventually be performed on the
4077 -- enclosing subprogram or package instance, if any.
4078 -- Enclosing scope is void in the formal part of a
4079 -- generic subprogram.
4080
4081 exit Scope_Loop;
4082
4083 else
4084 if Ekind (Enclosing_Master) = E_Entry
4085 and then
4086 Ekind (Scope (Enclosing_Master)) = E_Protected_Type
4087 then
4088 if not Expander_Active then
4089 exit Scope_Loop;
4090 else
4091 Enclosing_Master :=
4092 Protected_Body_Subprogram (Enclosing_Master);
4093 end if;
4094 end if;
4095
4096 Set_Delay_Cleanups (Enclosing_Master);
4097
4098 while Ekind (Enclosing_Master) = E_Block loop
4099 Enclosing_Master := Scope (Enclosing_Master);
4100 end loop;
4101
4102 if Is_Subprogram (Enclosing_Master) then
4103 Delay_Descriptors (Enclosing_Master);
4104
4105 elsif Is_Task_Type (Enclosing_Master) then
4106 declare
4107 TBP : constant Node_Id :=
4108 Get_Task_Body_Procedure
4109 (Enclosing_Master);
4110 begin
4111 if Present (TBP) then
4112 Delay_Descriptors (TBP);
4113 Set_Delay_Cleanups (TBP);
4114 end if;
4115 end;
4116 end if;
4117
4118 exit Scope_Loop;
4119 end if;
4120 end loop Scope_Loop;
4121 end;
4122
4123 -- Make entry in table
4124
4125 Pending_Instantiations.Append
4126 ((Inst_Node => N,
4127 Act_Decl => Act_Decl,
4128 Expander_Status => Expander_Active,
4129 Current_Sem_Unit => Current_Sem_Unit,
4130 Scope_Suppress => Scope_Suppress,
4131 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
4132 Version => Ada_Version,
4133 Version_Pragma => Ada_Version_Pragma,
4134 Warnings => Save_Warnings,
4135 SPARK_Mode => SPARK_Mode,
4136 SPARK_Mode_Pragma => SPARK_Mode_Pragma));
4137 end if;
4138 end if;
4139
4140 Set_Categorization_From_Pragmas (Act_Decl);
4141
4142 if Parent_Installed then
4143 Hide_Current_Scope;
4144 end if;
4145
4146 Set_Instance_Spec (N, Act_Decl);
4147
4148 -- If not a compilation unit, insert the package declaration before
4149 -- the original instantiation node.
4150
4151 if Nkind (Parent (N)) /= N_Compilation_Unit then
4152 Mark_Rewrite_Insertion (Act_Decl);
4153 Insert_Before (N, Act_Decl);
4154
4155 if Has_Aspects (N) then
4156 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4157
4158 -- The pragma created for a Default_Storage_Pool aspect must
4159 -- appear ahead of the declarations in the instance spec.
4160 -- Analysis has placed it after the instance node, so remove
4161 -- it and reinsert it properly now.
4162
4163 declare
4164 ASN : constant Node_Id := First (Aspect_Specifications (N));
4165 A_Name : constant Name_Id := Chars (Identifier (ASN));
4166 Decl : Node_Id;
4167
4168 begin
4169 if A_Name = Name_Default_Storage_Pool then
4170 if No (Visible_Declarations (Act_Spec)) then
4171 Set_Visible_Declarations (Act_Spec, New_List);
4172 end if;
4173
4174 Decl := Next (N);
4175 while Present (Decl) loop
4176 if Nkind (Decl) = N_Pragma then
4177 Remove (Decl);
4178 Prepend (Decl, Visible_Declarations (Act_Spec));
4179 exit;
4180 end if;
4181
4182 Next (Decl);
4183 end loop;
4184 end if;
4185 end;
4186 end if;
4187
4188 Analyze (Act_Decl);
4189
4190 -- For an instantiation that is a compilation unit, place
4191 -- declaration on current node so context is complete for analysis
4192 -- (including nested instantiations). If this is the main unit,
4193 -- the declaration eventually replaces the instantiation node.
4194 -- If the instance body is created later, it replaces the
4195 -- instance node, and the declaration is attached to it
4196 -- (see Build_Instance_Compilation_Unit_Nodes).
4197
4198 else
4199 if Cunit_Entity (Current_Sem_Unit) = Defining_Entity (N) then
4200
4201 -- The entity for the current unit is the newly created one,
4202 -- and all semantic information is attached to it.
4203
4204 Set_Cunit_Entity (Current_Sem_Unit, Act_Decl_Id);
4205
4206 -- If this is the main unit, replace the main entity as well
4207
4208 if Current_Sem_Unit = Main_Unit then
4209 Main_Unit_Entity := Act_Decl_Id;
4210 end if;
4211 end if;
4212
4213 Set_Unit (Parent (N), Act_Decl);
4214 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
4215 Set_Package_Instantiation (Act_Decl_Id, N);
4216
4217 -- Process aspect specifications of the instance node, if any, to
4218 -- take into account categorization pragmas before analyzing the
4219 -- instance.
4220
4221 if Has_Aspects (N) then
4222 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4223 end if;
4224
4225 Analyze (Act_Decl);
4226 Set_Unit (Parent (N), N);
4227 Set_Body_Required (Parent (N), False);
4228
4229 -- We never need elaboration checks on instantiations, since by
4230 -- definition, the body instantiation is elaborated at the same
4231 -- time as the spec instantiation.
4232
4233 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
4234 Set_Kill_Elaboration_Checks (Act_Decl_Id);
4235 end if;
4236
4237 Check_Elab_Instantiation (N);
4238
4239 if ABE_Is_Certain (N) and then Needs_Body then
4240 Pending_Instantiations.Decrement_Last;
4241 end if;
4242
4243 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
4244
4245 Set_First_Private_Entity (Defining_Unit_Name (Unit_Renaming),
4246 First_Private_Entity (Act_Decl_Id));
4247
4248 -- If the instantiation will receive a body, the unit will be
4249 -- transformed into a package body, and receive its own elaboration
4250 -- entity. Otherwise, the nature of the unit is now a package
4251 -- declaration.
4252
4253 if Nkind (Parent (N)) = N_Compilation_Unit
4254 and then not Needs_Body
4255 then
4256 Rewrite (N, Act_Decl);
4257 end if;
4258
4259 if Present (Corresponding_Body (Gen_Decl))
4260 or else Unit_Requires_Body (Gen_Unit)
4261 then
4262 Set_Has_Completion (Act_Decl_Id);
4263 end if;
4264
4265 Check_Formal_Packages (Act_Decl_Id);
4266
4267 Restore_Hidden_Primitives (Vis_Prims_List);
4268 Restore_Private_Views (Act_Decl_Id);
4269
4270 Inherit_Context (Gen_Decl, N);
4271
4272 if Parent_Installed then
4273 Remove_Parent;
4274 end if;
4275
4276 Restore_Env;
4277 Env_Installed := False;
4278 end if;
4279
4280 Validate_Categorization_Dependency (N, Act_Decl_Id);
4281
4282 -- There used to be a check here to prevent instantiations in local
4283 -- contexts if the No_Local_Allocators restriction was active. This
4284 -- check was removed by a binding interpretation in AI-95-00130/07,
4285 -- but we retain the code for documentation purposes.
4286
4287 -- if Ekind (Act_Decl_Id) /= E_Void
4288 -- and then not Is_Library_Level_Entity (Act_Decl_Id)
4289 -- then
4290 -- Check_Restriction (No_Local_Allocators, N);
4291 -- end if;
4292
4293 if Inline_Now then
4294 Inline_Instance_Body (N, Gen_Unit, Act_Decl);
4295 end if;
4296
4297 -- The following is a tree patch for ASIS: ASIS needs separate nodes to
4298 -- be used as defining identifiers for a formal package and for the
4299 -- corresponding expanded package.
4300
4301 if Nkind (N) = N_Formal_Package_Declaration then
4302 Act_Decl_Id := New_Copy (Defining_Entity (N));
4303 Set_Comes_From_Source (Act_Decl_Id, True);
4304 Set_Is_Generic_Instance (Act_Decl_Id, False);
4305 Set_Defining_Identifier (N, Act_Decl_Id);
4306 end if;
4307
4308 Ignore_Pragma_SPARK_Mode := Save_IPSM;
4309 SPARK_Mode := Save_SM;
4310 SPARK_Mode_Pragma := Save_SMP;
4311 Style_Check := Save_Style_Check;
4312
4313 if SPARK_Mode = On then
4314 Dynamic_Elaboration_Checks := False;
4315 end if;
4316
4317 -- Check that if N is an instantiation of System.Dim_Float_IO or
4318 -- System.Dim_Integer_IO, the formal type has a dimension system.
4319
4320 if Nkind (N) = N_Package_Instantiation
4321 and then Is_Dim_IO_Package_Instantiation (N)
4322 then
4323 declare
4324 Assoc : constant Node_Id := First (Generic_Associations (N));
4325 begin
4326 if not Has_Dimension_System
4327 (Etype (Explicit_Generic_Actual_Parameter (Assoc)))
4328 then
4329 Error_Msg_N ("type with a dimension system expected", Assoc);
4330 end if;
4331 end;
4332 end if;
4333
4334 <<Leave>>
4335 if Has_Aspects (N) and then Nkind (Parent (N)) /= N_Compilation_Unit then
4336 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4337 end if;
4338
4339 exception
4340 when Instantiation_Error =>
4341 if Parent_Installed then
4342 Remove_Parent;
4343 end if;
4344
4345 if Env_Installed then
4346 Restore_Env;
4347 end if;
4348
4349 Ignore_Pragma_SPARK_Mode := Save_IPSM;
4350 SPARK_Mode := Save_SM;
4351 SPARK_Mode_Pragma := Save_SMP;
4352 Style_Check := Save_Style_Check;
4353
4354 if SPARK_Mode = On then
4355 Dynamic_Elaboration_Checks := False;
4356 end if;
4357 end Analyze_Package_Instantiation;
4358
4359 --------------------------
4360 -- Inline_Instance_Body --
4361 --------------------------
4362
4363 procedure Inline_Instance_Body
4364 (N : Node_Id;
4365 Gen_Unit : Entity_Id;
4366 Act_Decl : Node_Id)
4367 is
4368 Curr_Comp : constant Node_Id := Cunit (Current_Sem_Unit);
4369 Curr_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
4370 Gen_Comp : constant Entity_Id :=
4371 Cunit_Entity (Get_Source_Unit (Gen_Unit));
4372
4373 Save_SM : constant SPARK_Mode_Type := SPARK_Mode;
4374 Save_SMP : constant Node_Id := SPARK_Mode_Pragma;
4375 -- Save all SPARK_Mode-related attributes as removing enclosing scopes
4376 -- to provide a clean environment for analysis of the inlined body will
4377 -- eliminate any previously set SPARK_Mode.
4378
4379 Scope_Stack_Depth : constant Int :=
4380 Scope_Stack.Last - Scope_Stack.First + 1;
4381
4382 Use_Clauses : array (1 .. Scope_Stack_Depth) of Node_Id;
4383 Instances : array (1 .. Scope_Stack_Depth) of Entity_Id;
4384 Inner_Scopes : array (1 .. Scope_Stack_Depth) of Entity_Id;
4385 Curr_Scope : Entity_Id := Empty;
4386 List : Elist_Id;
4387 Num_Inner : Int := 0;
4388 Num_Scopes : Int := 0;
4389 N_Instances : Int := 0;
4390 Removed : Boolean := False;
4391 S : Entity_Id;
4392 Vis : Boolean;
4393
4394 begin
4395 -- Case of generic unit defined in another unit. We must remove the
4396 -- complete context of the current unit to install that of the generic.
4397
4398 if Gen_Comp /= Cunit_Entity (Current_Sem_Unit) then
4399
4400 -- Add some comments for the following two loops ???
4401
4402 S := Current_Scope;
4403 while Present (S) and then S /= Standard_Standard loop
4404 loop
4405 Num_Scopes := Num_Scopes + 1;
4406
4407 Use_Clauses (Num_Scopes) :=
4408 (Scope_Stack.Table
4409 (Scope_Stack.Last - Num_Scopes + 1).
4410 First_Use_Clause);
4411 End_Use_Clauses (Use_Clauses (Num_Scopes));
4412
4413 exit when Scope_Stack.Last - Num_Scopes + 1 = Scope_Stack.First
4414 or else Scope_Stack.Table
4415 (Scope_Stack.Last - Num_Scopes).Entity = Scope (S);
4416 end loop;
4417
4418 exit when Is_Generic_Instance (S)
4419 and then (In_Package_Body (S)
4420 or else Ekind (S) = E_Procedure
4421 or else Ekind (S) = E_Function);
4422 S := Scope (S);
4423 end loop;
4424
4425 Vis := Is_Immediately_Visible (Gen_Comp);
4426
4427 -- Find and save all enclosing instances
4428
4429 S := Current_Scope;
4430
4431 while Present (S)
4432 and then S /= Standard_Standard
4433 loop
4434 if Is_Generic_Instance (S) then
4435 N_Instances := N_Instances + 1;
4436 Instances (N_Instances) := S;
4437
4438 exit when In_Package_Body (S);
4439 end if;
4440
4441 S := Scope (S);
4442 end loop;
4443
4444 -- Remove context of current compilation unit, unless we are within a
4445 -- nested package instantiation, in which case the context has been
4446 -- removed previously.
4447
4448 -- If current scope is the body of a child unit, remove context of
4449 -- spec as well. If an enclosing scope is an instance body, the
4450 -- context has already been removed, but the entities in the body
4451 -- must be made invisible as well.
4452
4453 S := Current_Scope;
4454 while Present (S) and then S /= Standard_Standard loop
4455 if Is_Generic_Instance (S)
4456 and then (In_Package_Body (S)
4457 or else Ekind_In (S, E_Procedure, E_Function))
4458 then
4459 -- We still have to remove the entities of the enclosing
4460 -- instance from direct visibility.
4461
4462 declare
4463 E : Entity_Id;
4464 begin
4465 E := First_Entity (S);
4466 while Present (E) loop
4467 Set_Is_Immediately_Visible (E, False);
4468 Next_Entity (E);
4469 end loop;
4470 end;
4471
4472 exit;
4473 end if;
4474
4475 if S = Curr_Unit
4476 or else (Ekind (Curr_Unit) = E_Package_Body
4477 and then S = Spec_Entity (Curr_Unit))
4478 or else (Ekind (Curr_Unit) = E_Subprogram_Body
4479 and then S = Corresponding_Spec
4480 (Unit_Declaration_Node (Curr_Unit)))
4481 then
4482 Removed := True;
4483
4484 -- Remove entities in current scopes from visibility, so that
4485 -- instance body is compiled in a clean environment.
4486
4487 List := Save_Scope_Stack (Handle_Use => False);
4488
4489 if Is_Child_Unit (S) then
4490
4491 -- Remove child unit from stack, as well as inner scopes.
4492 -- Removing the context of a child unit removes parent units
4493 -- as well.
4494
4495 while Current_Scope /= S loop
4496 Num_Inner := Num_Inner + 1;
4497 Inner_Scopes (Num_Inner) := Current_Scope;
4498 Pop_Scope;
4499 end loop;
4500
4501 Pop_Scope;
4502 Remove_Context (Curr_Comp);
4503 Curr_Scope := S;
4504
4505 else
4506 Remove_Context (Curr_Comp);
4507 end if;
4508
4509 if Ekind (Curr_Unit) = E_Package_Body then
4510 Remove_Context (Library_Unit (Curr_Comp));
4511 end if;
4512 end if;
4513
4514 S := Scope (S);
4515 end loop;
4516
4517 pragma Assert (Num_Inner < Num_Scopes);
4518
4519 -- The inlined package body must be analyzed with the SPARK_Mode of
4520 -- the enclosing context, otherwise the body may cause bogus errors
4521 -- if a configuration SPARK_Mode pragma in in effect.
4522
4523 Push_Scope (Standard_Standard);
4524 Scope_Stack.Table (Scope_Stack.Last).Is_Active_Stack_Base := True;
4525 Instantiate_Package_Body
4526 (Body_Info =>
4527 ((Inst_Node => N,
4528 Act_Decl => Act_Decl,
4529 Expander_Status => Expander_Active,
4530 Current_Sem_Unit => Current_Sem_Unit,
4531 Scope_Suppress => Scope_Suppress,
4532 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
4533 Version => Ada_Version,
4534 Version_Pragma => Ada_Version_Pragma,
4535 Warnings => Save_Warnings,
4536 SPARK_Mode => Save_SM,
4537 SPARK_Mode_Pragma => Save_SMP)),
4538 Inlined_Body => True);
4539
4540 Pop_Scope;
4541
4542 -- Restore context
4543
4544 Set_Is_Immediately_Visible (Gen_Comp, Vis);
4545
4546 -- Reset Generic_Instance flag so that use clauses can be installed
4547 -- in the proper order. (See Use_One_Package for effect of enclosing
4548 -- instances on processing of use clauses).
4549
4550 for J in 1 .. N_Instances loop
4551 Set_Is_Generic_Instance (Instances (J), False);
4552 end loop;
4553
4554 if Removed then
4555 Install_Context (Curr_Comp);
4556
4557 if Present (Curr_Scope)
4558 and then Is_Child_Unit (Curr_Scope)
4559 then
4560 Push_Scope (Curr_Scope);
4561 Set_Is_Immediately_Visible (Curr_Scope);
4562
4563 -- Finally, restore inner scopes as well
4564
4565 for J in reverse 1 .. Num_Inner loop
4566 Push_Scope (Inner_Scopes (J));
4567 end loop;
4568 end if;
4569
4570 Restore_Scope_Stack (List, Handle_Use => False);
4571
4572 if Present (Curr_Scope)
4573 and then
4574 (In_Private_Part (Curr_Scope)
4575 or else In_Package_Body (Curr_Scope))
4576 then
4577 -- Install private declaration of ancestor units, which are
4578 -- currently available. Restore_Scope_Stack and Install_Context
4579 -- only install the visible part of parents.
4580
4581 declare
4582 Par : Entity_Id;
4583 begin
4584 Par := Scope (Curr_Scope);
4585 while (Present (Par)) and then Par /= Standard_Standard loop
4586 Install_Private_Declarations (Par);
4587 Par := Scope (Par);
4588 end loop;
4589 end;
4590 end if;
4591 end if;
4592
4593 -- Restore use clauses. For a child unit, use clauses in the parents
4594 -- are restored when installing the context, so only those in inner
4595 -- scopes (and those local to the child unit itself) need to be
4596 -- installed explicitly.
4597
4598 if Is_Child_Unit (Curr_Unit) and then Removed then
4599 for J in reverse 1 .. Num_Inner + 1 loop
4600 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
4601 Use_Clauses (J);
4602 Install_Use_Clauses (Use_Clauses (J));
4603 end loop;
4604
4605 else
4606 for J in reverse 1 .. Num_Scopes loop
4607 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
4608 Use_Clauses (J);
4609 Install_Use_Clauses (Use_Clauses (J));
4610 end loop;
4611 end if;
4612
4613 -- Restore status of instances. If one of them is a body, make its
4614 -- local entities visible again.
4615
4616 declare
4617 E : Entity_Id;
4618 Inst : Entity_Id;
4619
4620 begin
4621 for J in 1 .. N_Instances loop
4622 Inst := Instances (J);
4623 Set_Is_Generic_Instance (Inst, True);
4624
4625 if In_Package_Body (Inst)
4626 or else Ekind_In (S, E_Procedure, E_Function)
4627 then
4628 E := First_Entity (Instances (J));
4629 while Present (E) loop
4630 Set_Is_Immediately_Visible (E);
4631 Next_Entity (E);
4632 end loop;
4633 end if;
4634 end loop;
4635 end;
4636
4637 -- If generic unit is in current unit, current context is correct. Note
4638 -- that the context is guaranteed to carry the correct SPARK_Mode as no
4639 -- enclosing scopes were removed.
4640
4641 else
4642 Instantiate_Package_Body
4643 (Body_Info =>
4644 ((Inst_Node => N,
4645 Act_Decl => Act_Decl,
4646 Expander_Status => Expander_Active,
4647 Current_Sem_Unit => Current_Sem_Unit,
4648 Scope_Suppress => Scope_Suppress,
4649 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
4650 Version => Ada_Version,
4651 Version_Pragma => Ada_Version_Pragma,
4652 Warnings => Save_Warnings,
4653 SPARK_Mode => SPARK_Mode,
4654 SPARK_Mode_Pragma => SPARK_Mode_Pragma)),
4655 Inlined_Body => True);
4656 end if;
4657 end Inline_Instance_Body;
4658
4659 -------------------------------------
4660 -- Analyze_Procedure_Instantiation --
4661 -------------------------------------
4662
4663 procedure Analyze_Procedure_Instantiation (N : Node_Id) is
4664 begin
4665 Analyze_Subprogram_Instantiation (N, E_Procedure);
4666 end Analyze_Procedure_Instantiation;
4667
4668 -----------------------------------
4669 -- Need_Subprogram_Instance_Body --
4670 -----------------------------------
4671
4672 function Need_Subprogram_Instance_Body
4673 (N : Node_Id;
4674 Subp : Entity_Id) return Boolean
4675 is
4676
4677 function Is_Inlined_Or_Child_Of_Inlined (E : Entity_Id) return Boolean;
4678 -- Return True if E is an inlined subprogram, an inlined renaming or a
4679 -- subprogram nested in an inlined subprogram. The inlining machinery
4680 -- totally disregards nested subprograms since it considers that they
4681 -- will always be compiled if the parent is (see Inline.Is_Nested).
4682
4683 ------------------------------------
4684 -- Is_Inlined_Or_Child_Of_Inlined --
4685 ------------------------------------
4686
4687 function Is_Inlined_Or_Child_Of_Inlined (E : Entity_Id) return Boolean is
4688 Scop : Entity_Id;
4689
4690 begin
4691 if Is_Inlined (E) or else Is_Inlined (Alias (E)) then
4692 return True;
4693 end if;
4694
4695 Scop := Scope (E);
4696 while Scop /= Standard_Standard loop
4697 if Ekind (Scop) in Subprogram_Kind and then Is_Inlined (Scop) then
4698 return True;
4699 end if;
4700
4701 Scop := Scope (Scop);
4702 end loop;
4703
4704 return False;
4705 end Is_Inlined_Or_Child_Of_Inlined;
4706
4707 begin
4708 -- Must be in the main unit or inlined (or child of inlined)
4709
4710 if (Is_In_Main_Unit (N) or else Is_Inlined_Or_Child_Of_Inlined (Subp))
4711
4712 -- Must be generating code or analyzing code in ASIS/GNATprove mode
4713
4714 and then (Operating_Mode = Generate_Code
4715 or else (Operating_Mode = Check_Semantics
4716 and then (ASIS_Mode or GNATprove_Mode)))
4717
4718 -- The body is needed when generating code (full expansion), in ASIS
4719 -- mode for other tools, and in GNATprove mode (special expansion) for
4720 -- formal verification of the body itself.
4721
4722 and then (Expander_Active or ASIS_Mode or GNATprove_Mode)
4723
4724 -- No point in inlining if ABE is inevitable
4725
4726 and then not ABE_Is_Certain (N)
4727
4728 -- Or if subprogram is eliminated
4729
4730 and then not Is_Eliminated (Subp)
4731 then
4732 Pending_Instantiations.Append
4733 ((Inst_Node => N,
4734 Act_Decl => Unit_Declaration_Node (Subp),
4735 Expander_Status => Expander_Active,
4736 Current_Sem_Unit => Current_Sem_Unit,
4737 Scope_Suppress => Scope_Suppress,
4738 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
4739 Version => Ada_Version,
4740 Version_Pragma => Ada_Version_Pragma,
4741 Warnings => Save_Warnings,
4742 SPARK_Mode => SPARK_Mode,
4743 SPARK_Mode_Pragma => SPARK_Mode_Pragma));
4744 return True;
4745
4746 -- Here if not inlined, or we ignore the inlining
4747
4748 else
4749 return False;
4750 end if;
4751 end Need_Subprogram_Instance_Body;
4752
4753 --------------------------------------
4754 -- Analyze_Subprogram_Instantiation --
4755 --------------------------------------
4756
4757 procedure Analyze_Subprogram_Instantiation
4758 (N : Node_Id;
4759 K : Entity_Kind)
4760 is
4761 Loc : constant Source_Ptr := Sloc (N);
4762 Gen_Id : constant Node_Id := Name (N);
4763
4764 Anon_Id : constant Entity_Id :=
4765 Make_Defining_Identifier (Sloc (Defining_Entity (N)),
4766 Chars => New_External_Name
4767 (Chars (Defining_Entity (N)), 'R'));
4768
4769 Act_Decl_Id : Entity_Id;
4770 Act_Decl : Node_Id;
4771 Act_Spec : Node_Id;
4772 Act_Tree : Node_Id;
4773
4774 Env_Installed : Boolean := False;
4775 Gen_Unit : Entity_Id;
4776 Gen_Decl : Node_Id;
4777 Pack_Id : Entity_Id;
4778 Parent_Installed : Boolean := False;
4779
4780 Renaming_List : List_Id;
4781 -- The list of declarations that link formals and actuals of the
4782 -- instance. These are subtype declarations for formal types, and
4783 -- renaming declarations for other formals. The subprogram declaration
4784 -- for the instance is then appended to the list, and the last item on
4785 -- the list is the renaming declaration for the instance.
4786
4787 procedure Analyze_Instance_And_Renamings;
4788 -- The instance must be analyzed in a context that includes the mappings
4789 -- of generic parameters into actuals. We create a package declaration
4790 -- for this purpose, and a subprogram with an internal name within the
4791 -- package. The subprogram instance is simply an alias for the internal
4792 -- subprogram, declared in the current scope.
4793
4794 procedure Build_Subprogram_Renaming;
4795 -- If the subprogram is recursive, there are occurrences of the name of
4796 -- the generic within the body, which must resolve to the current
4797 -- instance. We add a renaming declaration after the declaration, which
4798 -- is available in the instance body, as well as in the analysis of
4799 -- aspects that appear in the generic. This renaming declaration is
4800 -- inserted after the instance declaration which it renames.
4801
4802 ------------------------------------
4803 -- Analyze_Instance_And_Renamings --
4804 ------------------------------------
4805
4806 procedure Analyze_Instance_And_Renamings is
4807 Def_Ent : constant Entity_Id := Defining_Entity (N);
4808 Pack_Decl : Node_Id;
4809
4810 begin
4811 if Nkind (Parent (N)) = N_Compilation_Unit then
4812
4813 -- For the case of a compilation unit, the container package has
4814 -- the same name as the instantiation, to insure that the binder
4815 -- calls the elaboration procedure with the right name. Copy the
4816 -- entity of the instance, which may have compilation level flags
4817 -- (e.g. Is_Child_Unit) set.
4818
4819 Pack_Id := New_Copy (Def_Ent);
4820
4821 else
4822 -- Otherwise we use the name of the instantiation concatenated
4823 -- with its source position to ensure uniqueness if there are
4824 -- several instantiations with the same name.
4825
4826 Pack_Id :=
4827 Make_Defining_Identifier (Loc,
4828 Chars => New_External_Name
4829 (Related_Id => Chars (Def_Ent),
4830 Suffix => "GP",
4831 Suffix_Index => Source_Offset (Sloc (Def_Ent))));
4832 end if;
4833
4834 Pack_Decl :=
4835 Make_Package_Declaration (Loc,
4836 Specification => Make_Package_Specification (Loc,
4837 Defining_Unit_Name => Pack_Id,
4838 Visible_Declarations => Renaming_List,
4839 End_Label => Empty));
4840
4841 Set_Instance_Spec (N, Pack_Decl);
4842 Set_Is_Generic_Instance (Pack_Id);
4843 Set_Debug_Info_Needed (Pack_Id);
4844
4845 -- Case of not a compilation unit
4846
4847 if Nkind (Parent (N)) /= N_Compilation_Unit then
4848 Mark_Rewrite_Insertion (Pack_Decl);
4849 Insert_Before (N, Pack_Decl);
4850 Set_Has_Completion (Pack_Id);
4851
4852 -- Case of an instantiation that is a compilation unit
4853
4854 -- Place declaration on current node so context is complete for
4855 -- analysis (including nested instantiations), and for use in a
4856 -- context_clause (see Analyze_With_Clause).
4857
4858 else
4859 Set_Unit (Parent (N), Pack_Decl);
4860 Set_Parent_Spec (Pack_Decl, Parent_Spec (N));
4861 end if;
4862
4863 Analyze (Pack_Decl);
4864 Check_Formal_Packages (Pack_Id);
4865 Set_Is_Generic_Instance (Pack_Id, False);
4866
4867 -- Why do we clear Is_Generic_Instance??? We set it 20 lines
4868 -- above???
4869
4870 -- Body of the enclosing package is supplied when instantiating the
4871 -- subprogram body, after semantic analysis is completed.
4872
4873 if Nkind (Parent (N)) = N_Compilation_Unit then
4874
4875 -- Remove package itself from visibility, so it does not
4876 -- conflict with subprogram.
4877
4878 Set_Name_Entity_Id (Chars (Pack_Id), Homonym (Pack_Id));
4879
4880 -- Set name and scope of internal subprogram so that the proper
4881 -- external name will be generated. The proper scope is the scope
4882 -- of the wrapper package. We need to generate debugging info for
4883 -- the internal subprogram, so set flag accordingly.
4884
4885 Set_Chars (Anon_Id, Chars (Defining_Entity (N)));
4886 Set_Scope (Anon_Id, Scope (Pack_Id));
4887
4888 -- Mark wrapper package as referenced, to avoid spurious warnings
4889 -- if the instantiation appears in various with_ clauses of
4890 -- subunits of the main unit.
4891
4892 Set_Referenced (Pack_Id);
4893 end if;
4894
4895 Set_Is_Generic_Instance (Anon_Id);
4896 Set_Debug_Info_Needed (Anon_Id);
4897 Act_Decl_Id := New_Copy (Anon_Id);
4898
4899 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
4900 Set_Chars (Act_Decl_Id, Chars (Defining_Entity (N)));
4901 Set_Sloc (Act_Decl_Id, Sloc (Defining_Entity (N)));
4902
4903 -- Subprogram instance comes from source only if generic does
4904
4905 Set_Comes_From_Source (Act_Decl_Id, Comes_From_Source (Gen_Unit));
4906
4907 -- The signature may involve types that are not frozen yet, but the
4908 -- subprogram will be frozen at the point the wrapper package is
4909 -- frozen, so it does not need its own freeze node. In fact, if one
4910 -- is created, it might conflict with the freezing actions from the
4911 -- wrapper package.
4912
4913 Set_Has_Delayed_Freeze (Anon_Id, False);
4914
4915 -- If the instance is a child unit, mark the Id accordingly. Mark
4916 -- the anonymous entity as well, which is the real subprogram and
4917 -- which is used when the instance appears in a context clause.
4918 -- Similarly, propagate the Is_Eliminated flag to handle properly
4919 -- nested eliminated subprograms.
4920
4921 Set_Is_Child_Unit (Act_Decl_Id, Is_Child_Unit (Defining_Entity (N)));
4922 Set_Is_Child_Unit (Anon_Id, Is_Child_Unit (Defining_Entity (N)));
4923 New_Overloaded_Entity (Act_Decl_Id);
4924 Check_Eliminated (Act_Decl_Id);
4925 Set_Is_Eliminated (Anon_Id, Is_Eliminated (Act_Decl_Id));
4926
4927 -- In compilation unit case, kill elaboration checks on the
4928 -- instantiation, since they are never needed -- the body is
4929 -- instantiated at the same point as the spec.
4930
4931 if Nkind (Parent (N)) = N_Compilation_Unit then
4932 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
4933 Set_Kill_Elaboration_Checks (Act_Decl_Id);
4934 Set_Is_Compilation_Unit (Anon_Id);
4935
4936 Set_Cunit_Entity (Current_Sem_Unit, Pack_Id);
4937 end if;
4938
4939 -- The instance is not a freezing point for the new subprogram
4940
4941 Set_Is_Frozen (Act_Decl_Id, False);
4942
4943 if Nkind (Defining_Entity (N)) = N_Defining_Operator_Symbol then
4944 Valid_Operator_Definition (Act_Decl_Id);
4945 end if;
4946
4947 Set_Alias (Act_Decl_Id, Anon_Id);
4948 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
4949 Set_Has_Completion (Act_Decl_Id);
4950 Set_Related_Instance (Pack_Id, Act_Decl_Id);
4951
4952 if Nkind (Parent (N)) = N_Compilation_Unit then
4953 Set_Body_Required (Parent (N), False);
4954 end if;
4955 end Analyze_Instance_And_Renamings;
4956
4957 -------------------------------
4958 -- Build_Subprogram_Renaming --
4959 -------------------------------
4960
4961 procedure Build_Subprogram_Renaming is
4962 Renaming_Decl : Node_Id;
4963 Unit_Renaming : Node_Id;
4964
4965 begin
4966 Unit_Renaming :=
4967 Make_Subprogram_Renaming_Declaration (Loc,
4968 Specification =>
4969 Copy_Generic_Node
4970 (Specification (Original_Node (Gen_Decl)),
4971 Empty,
4972 Instantiating => True),
4973 Name => New_Occurrence_Of (Anon_Id, Loc));
4974
4975 -- The generic may be a a child unit. The renaming needs an
4976 -- identifier with the proper name.
4977
4978 Set_Defining_Unit_Name (Specification (Unit_Renaming),
4979 Make_Defining_Identifier (Loc, Chars (Gen_Unit)));
4980
4981 -- If there is a formal subprogram with the same name as the unit
4982 -- itself, do not add this renaming declaration, to prevent
4983 -- ambiguities when there is a call with that name in the body.
4984 -- This is a partial and ugly fix for one ACATS test. ???
4985
4986 Renaming_Decl := First (Renaming_List);
4987 while Present (Renaming_Decl) loop
4988 if Nkind (Renaming_Decl) = N_Subprogram_Renaming_Declaration
4989 and then
4990 Chars (Defining_Entity (Renaming_Decl)) = Chars (Gen_Unit)
4991 then
4992 exit;
4993 end if;
4994
4995 Next (Renaming_Decl);
4996 end loop;
4997
4998 if No (Renaming_Decl) then
4999 Append (Unit_Renaming, Renaming_List);
5000 end if;
5001 end Build_Subprogram_Renaming;
5002
5003 -- Local variables
5004
5005 Save_IPSM : constant Boolean := Ignore_Pragma_SPARK_Mode;
5006 -- Save flag Ignore_Pragma_SPARK_Mode for restore on exit
5007
5008 Save_SM : constant SPARK_Mode_Type := SPARK_Mode;
5009 Save_SMP : constant Node_Id := SPARK_Mode_Pragma;
5010 -- Save the SPARK_Mode-related data for restore on exit
5011
5012 Vis_Prims_List : Elist_Id := No_Elist;
5013 -- List of primitives made temporarily visible in the instantiation
5014 -- to match the visibility of the formal type
5015
5016 -- Start of processing for Analyze_Subprogram_Instantiation
5017
5018 begin
5019 Check_SPARK_05_Restriction ("generic is not allowed", N);
5020
5021 -- Very first thing: check for special Text_IO unit in case we are
5022 -- instantiating one of the children of [[Wide_]Wide_]Text_IO. Of course
5023 -- such an instantiation is bogus (these are packages, not subprograms),
5024 -- but we get a better error message if we do this.
5025
5026 Check_Text_IO_Special_Unit (Gen_Id);
5027
5028 -- Make node global for error reporting
5029
5030 Instantiation_Node := N;
5031
5032 -- For package instantiations we turn off style checks, because they
5033 -- will have been emitted in the generic. For subprogram instantiations
5034 -- we want to apply at least the check on overriding indicators so we
5035 -- do not modify the style check status.
5036
5037 -- The renaming declarations for the actuals do not come from source and
5038 -- will not generate spurious warnings.
5039
5040 Preanalyze_Actuals (N);
5041
5042 Init_Env;
5043 Env_Installed := True;
5044 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
5045 Gen_Unit := Entity (Gen_Id);
5046
5047 Generate_Reference (Gen_Unit, Gen_Id);
5048
5049 if Nkind (Gen_Id) = N_Identifier
5050 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
5051 then
5052 Error_Msg_NE
5053 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
5054 end if;
5055
5056 if Etype (Gen_Unit) = Any_Type then
5057 Restore_Env;
5058 return;
5059 end if;
5060
5061 -- Verify that it is a generic subprogram of the right kind, and that
5062 -- it does not lead to a circular instantiation.
5063
5064 if K = E_Procedure and then Ekind (Gen_Unit) /= E_Generic_Procedure then
5065 Error_Msg_NE
5066 ("& is not the name of a generic procedure", Gen_Id, Gen_Unit);
5067
5068 elsif K = E_Function and then Ekind (Gen_Unit) /= E_Generic_Function then
5069 Error_Msg_NE
5070 ("& is not the name of a generic function", Gen_Id, Gen_Unit);
5071
5072 elsif In_Open_Scopes (Gen_Unit) then
5073 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
5074
5075 else
5076 -- If the context of the instance is subject to SPARK_Mode "off" or
5077 -- the annotation is altogether missing, set the global flag which
5078 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
5079 -- the instance.
5080
5081 if SPARK_Mode /= On then
5082 Ignore_Pragma_SPARK_Mode := True;
5083 end if;
5084
5085 Set_Entity (Gen_Id, Gen_Unit);
5086 Set_Is_Instantiated (Gen_Unit);
5087
5088 if In_Extended_Main_Source_Unit (N) then
5089 Generate_Reference (Gen_Unit, N);
5090 end if;
5091
5092 -- If renaming, get original unit
5093
5094 if Present (Renamed_Object (Gen_Unit))
5095 and then Ekind_In (Renamed_Object (Gen_Unit), E_Generic_Procedure,
5096 E_Generic_Function)
5097 then
5098 Gen_Unit := Renamed_Object (Gen_Unit);
5099 Set_Is_Instantiated (Gen_Unit);
5100 Generate_Reference (Gen_Unit, N);
5101 end if;
5102
5103 if Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
5104 Error_Msg_Node_2 := Current_Scope;
5105 Error_Msg_NE
5106 ("circular Instantiation: & instantiated in &!", N, Gen_Unit);
5107 Circularity_Detected := True;
5108 Restore_Hidden_Primitives (Vis_Prims_List);
5109 goto Leave;
5110 end if;
5111
5112 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
5113
5114 -- Initialize renamings map, for error checking
5115
5116 Generic_Renamings.Set_Last (0);
5117 Generic_Renamings_HTable.Reset;
5118
5119 Create_Instantiation_Source (N, Gen_Unit, False, S_Adjustment);
5120
5121 -- Copy original generic tree, to produce text for instantiation
5122
5123 Act_Tree :=
5124 Copy_Generic_Node
5125 (Original_Node (Gen_Decl), Empty, Instantiating => True);
5126
5127 -- Inherit overriding indicator from instance node
5128
5129 Act_Spec := Specification (Act_Tree);
5130 Set_Must_Override (Act_Spec, Must_Override (N));
5131 Set_Must_Not_Override (Act_Spec, Must_Not_Override (N));
5132
5133 Renaming_List :=
5134 Analyze_Associations
5135 (I_Node => N,
5136 Formals => Generic_Formal_Declarations (Act_Tree),
5137 F_Copy => Generic_Formal_Declarations (Gen_Decl));
5138
5139 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
5140
5141 -- The subprogram itself cannot contain a nested instance, so the
5142 -- current parent is left empty.
5143
5144 Set_Instance_Env (Gen_Unit, Empty);
5145
5146 -- Build the subprogram declaration, which does not appear in the
5147 -- generic template, and give it a sloc consistent with that of the
5148 -- template.
5149
5150 Set_Defining_Unit_Name (Act_Spec, Anon_Id);
5151 Set_Generic_Parent (Act_Spec, Gen_Unit);
5152 Act_Decl :=
5153 Make_Subprogram_Declaration (Sloc (Act_Spec),
5154 Specification => Act_Spec);
5155
5156 -- The aspects have been copied previously, but they have to be
5157 -- linked explicitly to the new subprogram declaration. Explicit
5158 -- pre/postconditions on the instance are analyzed below, in a
5159 -- separate step.
5160
5161 Move_Aspects (Act_Tree, To => Act_Decl);
5162 Set_Categorization_From_Pragmas (Act_Decl);
5163
5164 if Parent_Installed then
5165 Hide_Current_Scope;
5166 end if;
5167
5168 Append (Act_Decl, Renaming_List);
5169
5170 -- Contract-related source pragmas that follow a generic subprogram
5171 -- must be instantiated explicitly because they are not part of the
5172 -- subprogram template.
5173
5174 Instantiate_Subprogram_Contract
5175 (Original_Node (Gen_Decl), Renaming_List);
5176
5177 Build_Subprogram_Renaming;
5178 Analyze_Instance_And_Renamings;
5179
5180 -- If the generic is marked Import (Intrinsic), then so is the
5181 -- instance. This indicates that there is no body to instantiate. If
5182 -- generic is marked inline, so it the instance, and the anonymous
5183 -- subprogram it renames. If inlined, or else if inlining is enabled
5184 -- for the compilation, we generate the instance body even if it is
5185 -- not within the main unit.
5186
5187 if Is_Intrinsic_Subprogram (Gen_Unit) then
5188 Set_Is_Intrinsic_Subprogram (Anon_Id);
5189 Set_Is_Intrinsic_Subprogram (Act_Decl_Id);
5190
5191 if Chars (Gen_Unit) = Name_Unchecked_Conversion then
5192 Validate_Unchecked_Conversion (N, Act_Decl_Id);
5193 end if;
5194 end if;
5195
5196 -- Inherit convention from generic unit. Intrinsic convention, as for
5197 -- an instance of unchecked conversion, is not inherited because an
5198 -- explicit Ada instance has been created.
5199
5200 if Has_Convention_Pragma (Gen_Unit)
5201 and then Convention (Gen_Unit) /= Convention_Intrinsic
5202 then
5203 Set_Convention (Act_Decl_Id, Convention (Gen_Unit));
5204 Set_Is_Exported (Act_Decl_Id, Is_Exported (Gen_Unit));
5205 end if;
5206
5207 Generate_Definition (Act_Decl_Id);
5208
5209 -- Inherit all inlining-related flags which apply to the generic in
5210 -- the subprogram and its declaration.
5211
5212 Set_Is_Inlined (Act_Decl_Id, Is_Inlined (Gen_Unit));
5213 Set_Is_Inlined (Anon_Id, Is_Inlined (Gen_Unit));
5214
5215 Set_Has_Pragma_Inline (Act_Decl_Id, Has_Pragma_Inline (Gen_Unit));
5216 Set_Has_Pragma_Inline (Anon_Id, Has_Pragma_Inline (Gen_Unit));
5217
5218 Set_Has_Pragma_Inline_Always
5219 (Act_Decl_Id, Has_Pragma_Inline_Always (Gen_Unit));
5220 Set_Has_Pragma_Inline_Always
5221 (Anon_Id, Has_Pragma_Inline_Always (Gen_Unit));
5222
5223 if not Is_Intrinsic_Subprogram (Gen_Unit) then
5224 Check_Elab_Instantiation (N);
5225 end if;
5226
5227 if Is_Dispatching_Operation (Act_Decl_Id)
5228 and then Ada_Version >= Ada_2005
5229 then
5230 declare
5231 Formal : Entity_Id;
5232
5233 begin
5234 Formal := First_Formal (Act_Decl_Id);
5235 while Present (Formal) loop
5236 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type
5237 and then Is_Controlling_Formal (Formal)
5238 and then not Can_Never_Be_Null (Formal)
5239 then
5240 Error_Msg_NE
5241 ("access parameter& is controlling,", N, Formal);
5242 Error_Msg_NE
5243 ("\corresponding parameter of & must be "
5244 & "explicitly null-excluding", N, Gen_Id);
5245 end if;
5246
5247 Next_Formal (Formal);
5248 end loop;
5249 end;
5250 end if;
5251
5252 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
5253
5254 Validate_Categorization_Dependency (N, Act_Decl_Id);
5255
5256 if not Is_Intrinsic_Subprogram (Act_Decl_Id) then
5257 Inherit_Context (Gen_Decl, N);
5258
5259 Restore_Private_Views (Pack_Id, False);
5260
5261 -- If the context requires a full instantiation, mark node for
5262 -- subsequent construction of the body.
5263
5264 if Need_Subprogram_Instance_Body (N, Act_Decl_Id) then
5265 Check_Forward_Instantiation (Gen_Decl);
5266
5267 -- The wrapper package is always delayed, because it does not
5268 -- constitute a freeze point, but to insure that the freeze node
5269 -- is placed properly, it is created directly when instantiating
5270 -- the body (otherwise the freeze node might appear to early for
5271 -- nested instantiations). For ASIS purposes, indicate that the
5272 -- wrapper package has replaced the instantiation node.
5273
5274 elsif Nkind (Parent (N)) = N_Compilation_Unit then
5275 Rewrite (N, Unit (Parent (N)));
5276 Set_Unit (Parent (N), N);
5277 end if;
5278
5279 -- Replace instance node for library-level instantiations of
5280 -- intrinsic subprograms, for ASIS use.
5281
5282 elsif Nkind (Parent (N)) = N_Compilation_Unit then
5283 Rewrite (N, Unit (Parent (N)));
5284 Set_Unit (Parent (N), N);
5285 end if;
5286
5287 if Parent_Installed then
5288 Remove_Parent;
5289 end if;
5290
5291 Restore_Hidden_Primitives (Vis_Prims_List);
5292 Restore_Env;
5293 Env_Installed := False;
5294 Generic_Renamings.Set_Last (0);
5295 Generic_Renamings_HTable.Reset;
5296
5297 Ignore_Pragma_SPARK_Mode := Save_IPSM;
5298 SPARK_Mode := Save_SM;
5299 SPARK_Mode_Pragma := Save_SMP;
5300
5301 if SPARK_Mode = On then
5302 Dynamic_Elaboration_Checks := False;
5303 end if;
5304 end if;
5305
5306 <<Leave>>
5307 if Has_Aspects (N) then
5308 Analyze_Aspect_Specifications (N, Act_Decl_Id);
5309 end if;
5310
5311 exception
5312 when Instantiation_Error =>
5313 if Parent_Installed then
5314 Remove_Parent;
5315 end if;
5316
5317 if Env_Installed then
5318 Restore_Env;
5319 end if;
5320
5321 Ignore_Pragma_SPARK_Mode := Save_IPSM;
5322 SPARK_Mode := Save_SM;
5323 SPARK_Mode_Pragma := Save_SMP;
5324
5325 if SPARK_Mode = On then
5326 Dynamic_Elaboration_Checks := False;
5327 end if;
5328 end Analyze_Subprogram_Instantiation;
5329
5330 -------------------------
5331 -- Get_Associated_Node --
5332 -------------------------
5333
5334 function Get_Associated_Node (N : Node_Id) return Node_Id is
5335 Assoc : Node_Id;
5336
5337 begin
5338 Assoc := Associated_Node (N);
5339
5340 if Nkind (Assoc) /= Nkind (N) then
5341 return Assoc;
5342
5343 elsif Nkind_In (Assoc, N_Aggregate, N_Extension_Aggregate) then
5344 return Assoc;
5345
5346 else
5347 -- If the node is part of an inner generic, it may itself have been
5348 -- remapped into a further generic copy. Associated_Node is otherwise
5349 -- used for the entity of the node, and will be of a different node
5350 -- kind, or else N has been rewritten as a literal or function call.
5351
5352 while Present (Associated_Node (Assoc))
5353 and then Nkind (Associated_Node (Assoc)) = Nkind (Assoc)
5354 loop
5355 Assoc := Associated_Node (Assoc);
5356 end loop;
5357
5358 -- Follow and additional link in case the final node was rewritten.
5359 -- This can only happen with nested generic units.
5360
5361 if (Nkind (Assoc) = N_Identifier or else Nkind (Assoc) in N_Op)
5362 and then Present (Associated_Node (Assoc))
5363 and then (Nkind_In (Associated_Node (Assoc), N_Function_Call,
5364 N_Explicit_Dereference,
5365 N_Integer_Literal,
5366 N_Real_Literal,
5367 N_String_Literal))
5368 then
5369 Assoc := Associated_Node (Assoc);
5370 end if;
5371
5372 -- An additional special case: an unconstrained type in an object
5373 -- declaration may have been rewritten as a local subtype constrained
5374 -- by the expression in the declaration. We need to recover the
5375 -- original entity which may be global.
5376
5377 if Present (Original_Node (Assoc))
5378 and then Nkind (Parent (N)) = N_Object_Declaration
5379 then
5380 Assoc := Original_Node (Assoc);
5381 end if;
5382
5383 return Assoc;
5384 end if;
5385 end Get_Associated_Node;
5386
5387 ----------------------------
5388 -- Build_Function_Wrapper --
5389 ----------------------------
5390
5391 function Build_Function_Wrapper
5392 (Formal_Subp : Entity_Id;
5393 Actual_Subp : Entity_Id) return Node_Id
5394 is
5395 Loc : constant Source_Ptr := Sloc (Current_Scope);
5396 Ret_Type : constant Entity_Id := Get_Instance_Of (Etype (Formal_Subp));
5397 Actuals : List_Id;
5398 Decl : Node_Id;
5399 Func_Name : Node_Id;
5400 Func : Entity_Id;
5401 Parm_Type : Node_Id;
5402 Profile : List_Id := New_List;
5403 Spec : Node_Id;
5404 Act_F : Entity_Id;
5405 Form_F : Entity_Id;
5406 New_F : Entity_Id;
5407
5408 begin
5409 Func_Name := New_Occurrence_Of (Actual_Subp, Loc);
5410
5411 Func := Make_Defining_Identifier (Loc, Chars (Formal_Subp));
5412 Set_Ekind (Func, E_Function);
5413 Set_Is_Generic_Actual_Subprogram (Func);
5414
5415 Actuals := New_List;
5416 Profile := New_List;
5417
5418 Act_F := First_Formal (Actual_Subp);
5419 Form_F := First_Formal (Formal_Subp);
5420 while Present (Form_F) loop
5421
5422 -- Create new formal for profile of wrapper, and add a reference
5423 -- to it in the list of actuals for the enclosing call. The name
5424 -- must be that of the formal in the formal subprogram, because
5425 -- calls to it in the generic body may use named associations.
5426
5427 New_F := Make_Defining_Identifier (Loc, Chars (Form_F));
5428
5429 Parm_Type :=
5430 New_Occurrence_Of (Get_Instance_Of (Etype (Form_F)), Loc);
5431
5432 Append_To (Profile,
5433 Make_Parameter_Specification (Loc,
5434 Defining_Identifier => New_F,
5435 Parameter_Type => Parm_Type));
5436
5437 Append_To (Actuals, New_Occurrence_Of (New_F, Loc));
5438 Next_Formal (Form_F);
5439
5440 if Present (Act_F) then
5441 Next_Formal (Act_F);
5442 end if;
5443 end loop;
5444
5445 Spec :=
5446 Make_Function_Specification (Loc,
5447 Defining_Unit_Name => Func,
5448 Parameter_Specifications => Profile,
5449 Result_Definition => New_Occurrence_Of (Ret_Type, Loc));
5450
5451 Decl :=
5452 Make_Expression_Function (Loc,
5453 Specification => Spec,
5454 Expression =>
5455 Make_Function_Call (Loc,
5456 Name => Func_Name,
5457 Parameter_Associations => Actuals));
5458
5459 return Decl;
5460 end Build_Function_Wrapper;
5461
5462 ----------------------------
5463 -- Build_Operator_Wrapper --
5464 ----------------------------
5465
5466 function Build_Operator_Wrapper
5467 (Formal_Subp : Entity_Id;
5468 Actual_Subp : Entity_Id) return Node_Id
5469 is
5470 Loc : constant Source_Ptr := Sloc (Current_Scope);
5471 Ret_Type : constant Entity_Id :=
5472 Get_Instance_Of (Etype (Formal_Subp));
5473 Op_Type : constant Entity_Id :=
5474 Get_Instance_Of (Etype (First_Formal (Formal_Subp)));
5475 Is_Binary : constant Boolean :=
5476 Present (Next_Formal (First_Formal (Formal_Subp)));
5477
5478 Decl : Node_Id;
5479 Expr : Node_Id;
5480 F1, F2 : Entity_Id;
5481 Func : Entity_Id;
5482 Op_Name : Name_Id;
5483 Spec : Node_Id;
5484 L, R : Node_Id;
5485
5486 begin
5487 Op_Name := Chars (Actual_Subp);
5488
5489 -- Create entities for wrapper function and its formals
5490
5491 F1 := Make_Temporary (Loc, 'A');
5492 F2 := Make_Temporary (Loc, 'B');
5493 L := New_Occurrence_Of (F1, Loc);
5494 R := New_Occurrence_Of (F2, Loc);
5495
5496 Func := Make_Defining_Identifier (Loc, Chars (Formal_Subp));
5497 Set_Ekind (Func, E_Function);
5498 Set_Is_Generic_Actual_Subprogram (Func);
5499
5500 Spec :=
5501 Make_Function_Specification (Loc,
5502 Defining_Unit_Name => Func,
5503 Parameter_Specifications => New_List (
5504 Make_Parameter_Specification (Loc,
5505 Defining_Identifier => F1,
5506 Parameter_Type => New_Occurrence_Of (Op_Type, Loc))),
5507 Result_Definition => New_Occurrence_Of (Ret_Type, Loc));
5508
5509 if Is_Binary then
5510 Append_To (Parameter_Specifications (Spec),
5511 Make_Parameter_Specification (Loc,
5512 Defining_Identifier => F2,
5513 Parameter_Type => New_Occurrence_Of (Op_Type, Loc)));
5514 end if;
5515
5516 -- Build expression as a function call, or as an operator node
5517 -- that corresponds to the name of the actual, starting with
5518 -- binary operators.
5519
5520 if Op_Name not in Any_Operator_Name then
5521 Expr :=
5522 Make_Function_Call (Loc,
5523 Name =>
5524 New_Occurrence_Of (Actual_Subp, Loc),
5525 Parameter_Associations => New_List (L));
5526
5527 if Is_Binary then
5528 Append_To (Parameter_Associations (Expr), R);
5529 end if;
5530
5531 -- Binary operators
5532
5533 elsif Is_Binary then
5534 if Op_Name = Name_Op_And then
5535 Expr := Make_Op_And (Loc, Left_Opnd => L, Right_Opnd => R);
5536 elsif Op_Name = Name_Op_Or then
5537 Expr := Make_Op_Or (Loc, Left_Opnd => L, Right_Opnd => R);
5538 elsif Op_Name = Name_Op_Xor then
5539 Expr := Make_Op_Xor (Loc, Left_Opnd => L, Right_Opnd => R);
5540 elsif Op_Name = Name_Op_Eq then
5541 Expr := Make_Op_Eq (Loc, Left_Opnd => L, Right_Opnd => R);
5542 elsif Op_Name = Name_Op_Ne then
5543 Expr := Make_Op_Ne (Loc, Left_Opnd => L, Right_Opnd => R);
5544 elsif Op_Name = Name_Op_Le then
5545 Expr := Make_Op_Le (Loc, Left_Opnd => L, Right_Opnd => R);
5546 elsif Op_Name = Name_Op_Gt then
5547 Expr := Make_Op_Gt (Loc, Left_Opnd => L, Right_Opnd => R);
5548 elsif Op_Name = Name_Op_Ge then
5549 Expr := Make_Op_Ge (Loc, Left_Opnd => L, Right_Opnd => R);
5550 elsif Op_Name = Name_Op_Lt then
5551 Expr := Make_Op_Lt (Loc, Left_Opnd => L, Right_Opnd => R);
5552 elsif Op_Name = Name_Op_Add then
5553 Expr := Make_Op_Add (Loc, Left_Opnd => L, Right_Opnd => R);
5554 elsif Op_Name = Name_Op_Subtract then
5555 Expr := Make_Op_Subtract (Loc, Left_Opnd => L, Right_Opnd => R);
5556 elsif Op_Name = Name_Op_Concat then
5557 Expr := Make_Op_Concat (Loc, Left_Opnd => L, Right_Opnd => R);
5558 elsif Op_Name = Name_Op_Multiply then
5559 Expr := Make_Op_Multiply (Loc, Left_Opnd => L, Right_Opnd => R);
5560 elsif Op_Name = Name_Op_Divide then
5561 Expr := Make_Op_Divide (Loc, Left_Opnd => L, Right_Opnd => R);
5562 elsif Op_Name = Name_Op_Mod then
5563 Expr := Make_Op_Mod (Loc, Left_Opnd => L, Right_Opnd => R);
5564 elsif Op_Name = Name_Op_Rem then
5565 Expr := Make_Op_Rem (Loc, Left_Opnd => L, Right_Opnd => R);
5566 elsif Op_Name = Name_Op_Expon then
5567 Expr := Make_Op_Expon (Loc, Left_Opnd => L, Right_Opnd => R);
5568 end if;
5569
5570 -- Unary operators
5571
5572 else
5573 if Op_Name = Name_Op_Add then
5574 Expr := Make_Op_Plus (Loc, Right_Opnd => L);
5575 elsif Op_Name = Name_Op_Subtract then
5576 Expr := Make_Op_Minus (Loc, Right_Opnd => L);
5577 elsif Op_Name = Name_Op_Abs then
5578 Expr := Make_Op_Abs (Loc, Right_Opnd => L);
5579 elsif Op_Name = Name_Op_Not then
5580 Expr := Make_Op_Not (Loc, Right_Opnd => L);
5581 end if;
5582 end if;
5583
5584 Decl :=
5585 Make_Expression_Function (Loc,
5586 Specification => Spec,
5587 Expression => Expr);
5588
5589 return Decl;
5590 end Build_Operator_Wrapper;
5591
5592 -------------------------------------------
5593 -- Build_Instance_Compilation_Unit_Nodes --
5594 -------------------------------------------
5595
5596 procedure Build_Instance_Compilation_Unit_Nodes
5597 (N : Node_Id;
5598 Act_Body : Node_Id;
5599 Act_Decl : Node_Id)
5600 is
5601 Decl_Cunit : Node_Id;
5602 Body_Cunit : Node_Id;
5603 Citem : Node_Id;
5604 New_Main : constant Entity_Id := Defining_Entity (Act_Decl);
5605 Old_Main : constant Entity_Id := Cunit_Entity (Main_Unit);
5606
5607 begin
5608 -- A new compilation unit node is built for the instance declaration
5609
5610 Decl_Cunit :=
5611 Make_Compilation_Unit (Sloc (N),
5612 Context_Items => Empty_List,
5613 Unit => Act_Decl,
5614 Aux_Decls_Node => Make_Compilation_Unit_Aux (Sloc (N)));
5615
5616 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
5617
5618 -- The new compilation unit is linked to its body, but both share the
5619 -- same file, so we do not set Body_Required on the new unit so as not
5620 -- to create a spurious dependency on a non-existent body in the ali.
5621 -- This simplifies CodePeer unit traversal.
5622
5623 -- We use the original instantiation compilation unit as the resulting
5624 -- compilation unit of the instance, since this is the main unit.
5625
5626 Rewrite (N, Act_Body);
5627
5628 -- Propagate the aspect specifications from the package body template to
5629 -- the instantiated version of the package body.
5630
5631 if Has_Aspects (Act_Body) then
5632 Set_Aspect_Specifications
5633 (N, New_Copy_List_Tree (Aspect_Specifications (Act_Body)));
5634 end if;
5635
5636 Body_Cunit := Parent (N);
5637
5638 -- The two compilation unit nodes are linked by the Library_Unit field
5639
5640 Set_Library_Unit (Decl_Cunit, Body_Cunit);
5641 Set_Library_Unit (Body_Cunit, Decl_Cunit);
5642
5643 -- Preserve the private nature of the package if needed
5644
5645 Set_Private_Present (Decl_Cunit, Private_Present (Body_Cunit));
5646
5647 -- If the instance is not the main unit, its context, categorization
5648 -- and elaboration entity are not relevant to the compilation.
5649
5650 if Body_Cunit /= Cunit (Main_Unit) then
5651 Make_Instance_Unit (Body_Cunit, In_Main => False);
5652 return;
5653 end if;
5654
5655 -- The context clause items on the instantiation, which are now attached
5656 -- to the body compilation unit (since the body overwrote the original
5657 -- instantiation node), semantically belong on the spec, so copy them
5658 -- there. It's harmless to leave them on the body as well. In fact one
5659 -- could argue that they belong in both places.
5660
5661 Citem := First (Context_Items (Body_Cunit));
5662 while Present (Citem) loop
5663 Append (New_Copy (Citem), Context_Items (Decl_Cunit));
5664 Next (Citem);
5665 end loop;
5666
5667 -- Propagate categorization flags on packages, so that they appear in
5668 -- the ali file for the spec of the unit.
5669
5670 if Ekind (New_Main) = E_Package then
5671 Set_Is_Pure (Old_Main, Is_Pure (New_Main));
5672 Set_Is_Preelaborated (Old_Main, Is_Preelaborated (New_Main));
5673 Set_Is_Remote_Types (Old_Main, Is_Remote_Types (New_Main));
5674 Set_Is_Shared_Passive (Old_Main, Is_Shared_Passive (New_Main));
5675 Set_Is_Remote_Call_Interface
5676 (Old_Main, Is_Remote_Call_Interface (New_Main));
5677 end if;
5678
5679 -- Make entry in Units table, so that binder can generate call to
5680 -- elaboration procedure for body, if any.
5681
5682 Make_Instance_Unit (Body_Cunit, In_Main => True);
5683 Main_Unit_Entity := New_Main;
5684 Set_Cunit_Entity (Main_Unit, Main_Unit_Entity);
5685
5686 -- Build elaboration entity, since the instance may certainly generate
5687 -- elaboration code requiring a flag for protection.
5688
5689 Build_Elaboration_Entity (Decl_Cunit, New_Main);
5690 end Build_Instance_Compilation_Unit_Nodes;
5691
5692 -----------------------------
5693 -- Check_Access_Definition --
5694 -----------------------------
5695
5696 procedure Check_Access_Definition (N : Node_Id) is
5697 begin
5698 pragma Assert
5699 (Ada_Version >= Ada_2005 and then Present (Access_Definition (N)));
5700 null;
5701 end Check_Access_Definition;
5702
5703 -----------------------------------
5704 -- Check_Formal_Package_Instance --
5705 -----------------------------------
5706
5707 -- If the formal has specific parameters, they must match those of the
5708 -- actual. Both of them are instances, and the renaming declarations for
5709 -- their formal parameters appear in the same order in both. The analyzed
5710 -- formal has been analyzed in the context of the current instance.
5711
5712 procedure Check_Formal_Package_Instance
5713 (Formal_Pack : Entity_Id;
5714 Actual_Pack : Entity_Id)
5715 is
5716 E1 : Entity_Id := First_Entity (Actual_Pack);
5717 E2 : Entity_Id := First_Entity (Formal_Pack);
5718
5719 Expr1 : Node_Id;
5720 Expr2 : Node_Id;
5721
5722 procedure Check_Mismatch (B : Boolean);
5723 -- Common error routine for mismatch between the parameters of the
5724 -- actual instance and those of the formal package.
5725
5726 function Same_Instantiated_Constant (E1, E2 : Entity_Id) return Boolean;
5727 -- The formal may come from a nested formal package, and the actual may
5728 -- have been constant-folded. To determine whether the two denote the
5729 -- same entity we may have to traverse several definitions to recover
5730 -- the ultimate entity that they refer to.
5731
5732 function Same_Instantiated_Function (E1, E2 : Entity_Id) return Boolean;
5733 -- The formal and the actual must be identical, but if both are
5734 -- given by attributes they end up renaming different generated bodies,
5735 -- and we must verify that the attributes themselves match.
5736
5737 function Same_Instantiated_Variable (E1, E2 : Entity_Id) return Boolean;
5738 -- Similarly, if the formal comes from a nested formal package, the
5739 -- actual may designate the formal through multiple renamings, which
5740 -- have to be followed to determine the original variable in question.
5741
5742 --------------------
5743 -- Check_Mismatch --
5744 --------------------
5745
5746 procedure Check_Mismatch (B : Boolean) is
5747 Kind : constant Node_Kind := Nkind (Parent (E2));
5748
5749 begin
5750 if Kind = N_Formal_Type_Declaration then
5751 return;
5752
5753 elsif Nkind_In (Kind, N_Formal_Object_Declaration,
5754 N_Formal_Package_Declaration)
5755 or else Kind in N_Formal_Subprogram_Declaration
5756 then
5757 null;
5758
5759 -- Ada 2012: If both formal and actual are incomplete types they
5760 -- are conformant.
5761
5762 elsif Is_Incomplete_Type (E1) and then Is_Incomplete_Type (E2) then
5763 null;
5764
5765 elsif B then
5766 Error_Msg_NE
5767 ("actual for & in actual instance does not match formal",
5768 Parent (Actual_Pack), E1);
5769 end if;
5770 end Check_Mismatch;
5771
5772 --------------------------------
5773 -- Same_Instantiated_Constant --
5774 --------------------------------
5775
5776 function Same_Instantiated_Constant
5777 (E1, E2 : Entity_Id) return Boolean
5778 is
5779 Ent : Entity_Id;
5780
5781 begin
5782 Ent := E2;
5783 while Present (Ent) loop
5784 if E1 = Ent then
5785 return True;
5786
5787 elsif Ekind (Ent) /= E_Constant then
5788 return False;
5789
5790 elsif Is_Entity_Name (Constant_Value (Ent)) then
5791 if Entity (Constant_Value (Ent)) = E1 then
5792 return True;
5793 else
5794 Ent := Entity (Constant_Value (Ent));
5795 end if;
5796
5797 -- The actual may be a constant that has been folded. Recover
5798 -- original name.
5799
5800 elsif Is_Entity_Name (Original_Node (Constant_Value (Ent))) then
5801 Ent := Entity (Original_Node (Constant_Value (Ent)));
5802
5803 else
5804 return False;
5805 end if;
5806 end loop;
5807
5808 return False;
5809 end Same_Instantiated_Constant;
5810
5811 --------------------------------
5812 -- Same_Instantiated_Function --
5813 --------------------------------
5814
5815 function Same_Instantiated_Function
5816 (E1, E2 : Entity_Id) return Boolean
5817 is
5818 U1, U2 : Node_Id;
5819 begin
5820 if Alias (E1) = Alias (E2) then
5821 return True;
5822
5823 elsif Present (Alias (E2)) then
5824 U1 := Original_Node (Unit_Declaration_Node (E1));
5825 U2 := Original_Node (Unit_Declaration_Node (Alias (E2)));
5826
5827 return Nkind (U1) = N_Subprogram_Renaming_Declaration
5828 and then Nkind (Name (U1)) = N_Attribute_Reference
5829
5830 and then Nkind (U2) = N_Subprogram_Renaming_Declaration
5831 and then Nkind (Name (U2)) = N_Attribute_Reference
5832
5833 and then
5834 Attribute_Name (Name (U1)) = Attribute_Name (Name (U2));
5835 else
5836 return False;
5837 end if;
5838 end Same_Instantiated_Function;
5839
5840 --------------------------------
5841 -- Same_Instantiated_Variable --
5842 --------------------------------
5843
5844 function Same_Instantiated_Variable
5845 (E1, E2 : Entity_Id) return Boolean
5846 is
5847 function Original_Entity (E : Entity_Id) return Entity_Id;
5848 -- Follow chain of renamings to the ultimate ancestor
5849
5850 ---------------------
5851 -- Original_Entity --
5852 ---------------------
5853
5854 function Original_Entity (E : Entity_Id) return Entity_Id is
5855 Orig : Entity_Id;
5856
5857 begin
5858 Orig := E;
5859 while Nkind (Parent (Orig)) = N_Object_Renaming_Declaration
5860 and then Present (Renamed_Object (Orig))
5861 and then Is_Entity_Name (Renamed_Object (Orig))
5862 loop
5863 Orig := Entity (Renamed_Object (Orig));
5864 end loop;
5865
5866 return Orig;
5867 end Original_Entity;
5868
5869 -- Start of processing for Same_Instantiated_Variable
5870
5871 begin
5872 return Ekind (E1) = Ekind (E2)
5873 and then Original_Entity (E1) = Original_Entity (E2);
5874 end Same_Instantiated_Variable;
5875
5876 -- Start of processing for Check_Formal_Package_Instance
5877
5878 begin
5879 while Present (E1) and then Present (E2) loop
5880 exit when Ekind (E1) = E_Package
5881 and then Renamed_Entity (E1) = Renamed_Entity (Actual_Pack);
5882
5883 -- If the formal is the renaming of the formal package, this
5884 -- is the end of its formal part, which may occur before the
5885 -- end of the formal part in the actual in the presence of
5886 -- defaulted parameters in the formal package.
5887
5888 exit when Nkind (Parent (E2)) = N_Package_Renaming_Declaration
5889 and then Renamed_Entity (E2) = Scope (E2);
5890
5891 -- The analysis of the actual may generate additional internal
5892 -- entities. If the formal is defaulted, there is no corresponding
5893 -- analysis and the internal entities must be skipped, until we
5894 -- find corresponding entities again.
5895
5896 if Comes_From_Source (E2)
5897 and then not Comes_From_Source (E1)
5898 and then Chars (E1) /= Chars (E2)
5899 then
5900 while Present (E1) and then Chars (E1) /= Chars (E2) loop
5901 Next_Entity (E1);
5902 end loop;
5903 end if;
5904
5905 if No (E1) then
5906 return;
5907
5908 -- If the formal entity comes from a formal declaration, it was
5909 -- defaulted in the formal package, and no check is needed on it.
5910
5911 elsif Nkind (Parent (E2)) = N_Formal_Object_Declaration then
5912 goto Next_E;
5913
5914 -- Ditto for defaulted formal subprograms.
5915
5916 elsif Is_Overloadable (E1)
5917 and then Nkind (Unit_Declaration_Node (E2)) in
5918 N_Formal_Subprogram_Declaration
5919 then
5920 goto Next_E;
5921
5922 elsif Is_Type (E1) then
5923
5924 -- Subtypes must statically match. E1, E2 are the local entities
5925 -- that are subtypes of the actuals. Itypes generated for other
5926 -- parameters need not be checked, the check will be performed
5927 -- on the parameters themselves.
5928
5929 -- If E2 is a formal type declaration, it is a defaulted parameter
5930 -- and needs no checking.
5931
5932 if not Is_Itype (E1) and then not Is_Itype (E2) then
5933 Check_Mismatch
5934 (not Is_Type (E2)
5935 or else Etype (E1) /= Etype (E2)
5936 or else not Subtypes_Statically_Match (E1, E2));
5937 end if;
5938
5939 elsif Ekind (E1) = E_Constant then
5940
5941 -- IN parameters must denote the same static value, or the same
5942 -- constant, or the literal null.
5943
5944 Expr1 := Expression (Parent (E1));
5945
5946 if Ekind (E2) /= E_Constant then
5947 Check_Mismatch (True);
5948 goto Next_E;
5949 else
5950 Expr2 := Expression (Parent (E2));
5951 end if;
5952
5953 if Is_OK_Static_Expression (Expr1) then
5954 if not Is_OK_Static_Expression (Expr2) then
5955 Check_Mismatch (True);
5956
5957 elsif Is_Discrete_Type (Etype (E1)) then
5958 declare
5959 V1 : constant Uint := Expr_Value (Expr1);
5960 V2 : constant Uint := Expr_Value (Expr2);
5961 begin
5962 Check_Mismatch (V1 /= V2);
5963 end;
5964
5965 elsif Is_Real_Type (Etype (E1)) then
5966 declare
5967 V1 : constant Ureal := Expr_Value_R (Expr1);
5968 V2 : constant Ureal := Expr_Value_R (Expr2);
5969 begin
5970 Check_Mismatch (V1 /= V2);
5971 end;
5972
5973 elsif Is_String_Type (Etype (E1))
5974 and then Nkind (Expr1) = N_String_Literal
5975 then
5976 if Nkind (Expr2) /= N_String_Literal then
5977 Check_Mismatch (True);
5978 else
5979 Check_Mismatch
5980 (not String_Equal (Strval (Expr1), Strval (Expr2)));
5981 end if;
5982 end if;
5983
5984 elsif Is_Entity_Name (Expr1) then
5985 if Is_Entity_Name (Expr2) then
5986 if Entity (Expr1) = Entity (Expr2) then
5987 null;
5988 else
5989 Check_Mismatch
5990 (not Same_Instantiated_Constant
5991 (Entity (Expr1), Entity (Expr2)));
5992 end if;
5993
5994 else
5995 Check_Mismatch (True);
5996 end if;
5997
5998 elsif Is_Entity_Name (Original_Node (Expr1))
5999 and then Is_Entity_Name (Expr2)
6000 and then Same_Instantiated_Constant
6001 (Entity (Original_Node (Expr1)), Entity (Expr2))
6002 then
6003 null;
6004
6005 elsif Nkind (Expr1) = N_Null then
6006 Check_Mismatch (Nkind (Expr1) /= N_Null);
6007
6008 else
6009 Check_Mismatch (True);
6010 end if;
6011
6012 elsif Ekind (E1) = E_Variable then
6013 Check_Mismatch (not Same_Instantiated_Variable (E1, E2));
6014
6015 elsif Ekind (E1) = E_Package then
6016 Check_Mismatch
6017 (Ekind (E1) /= Ekind (E2)
6018 or else Renamed_Object (E1) /= Renamed_Object (E2));
6019
6020 elsif Is_Overloadable (E1) then
6021
6022 -- Verify that the actual subprograms match. Note that actuals
6023 -- that are attributes are rewritten as subprograms. If the
6024 -- subprogram in the formal package is defaulted, no check is
6025 -- needed. Note that this can only happen in Ada 2005 when the
6026 -- formal package can be partially parameterized.
6027
6028 if Nkind (Unit_Declaration_Node (E1)) =
6029 N_Subprogram_Renaming_Declaration
6030 and then From_Default (Unit_Declaration_Node (E1))
6031 then
6032 null;
6033
6034 -- If the formal package has an "others" box association that
6035 -- covers this formal, there is no need for a check either.
6036
6037 elsif Nkind (Unit_Declaration_Node (E2)) in
6038 N_Formal_Subprogram_Declaration
6039 and then Box_Present (Unit_Declaration_Node (E2))
6040 then
6041 null;
6042
6043 -- No check needed if subprogram is a defaulted null procedure
6044
6045 elsif No (Alias (E2))
6046 and then Ekind (E2) = E_Procedure
6047 and then
6048 Null_Present (Specification (Unit_Declaration_Node (E2)))
6049 then
6050 null;
6051
6052 -- Otherwise the actual in the formal and the actual in the
6053 -- instantiation of the formal must match, up to renamings.
6054
6055 else
6056 Check_Mismatch
6057 (Ekind (E2) /= Ekind (E1)
6058 or else not Same_Instantiated_Function (E1, E2));
6059 end if;
6060
6061 else
6062 raise Program_Error;
6063 end if;
6064
6065 <<Next_E>>
6066 Next_Entity (E1);
6067 Next_Entity (E2);
6068 end loop;
6069 end Check_Formal_Package_Instance;
6070
6071 ---------------------------
6072 -- Check_Formal_Packages --
6073 ---------------------------
6074
6075 procedure Check_Formal_Packages (P_Id : Entity_Id) is
6076 E : Entity_Id;
6077 Formal_P : Entity_Id;
6078 Formal_Decl : Node_Id;
6079
6080 begin
6081 -- Iterate through the declarations in the instance, looking for package
6082 -- renaming declarations that denote instances of formal packages. Stop
6083 -- when we find the renaming of the current package itself. The
6084 -- declaration for a formal package without a box is followed by an
6085 -- internal entity that repeats the instantiation.
6086
6087 E := First_Entity (P_Id);
6088 while Present (E) loop
6089 if Ekind (E) = E_Package then
6090 if Renamed_Object (E) = P_Id then
6091 exit;
6092
6093 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
6094 null;
6095
6096 else
6097 Formal_Decl := Parent (Associated_Formal_Package (E));
6098
6099 -- Nothing to check if the formal has a box or an others_clause
6100 -- (necessarily with a box).
6101
6102 if Box_Present (Formal_Decl) then
6103 null;
6104
6105 elsif Nkind (First (Generic_Associations (Formal_Decl))) =
6106 N_Others_Choice
6107 then
6108 -- The internal validating package was generated but formal
6109 -- and instance are known to be compatible.
6110
6111 Formal_P := Next_Entity (E);
6112 Remove (Unit_Declaration_Node (Formal_P));
6113
6114 else
6115 Formal_P := Next_Entity (E);
6116 Check_Formal_Package_Instance (Formal_P, E);
6117
6118 -- After checking, remove the internal validating package.
6119 -- It is only needed for semantic checks, and as it may
6120 -- contain generic formal declarations it should not reach
6121 -- gigi.
6122
6123 Remove (Unit_Declaration_Node (Formal_P));
6124 end if;
6125 end if;
6126 end if;
6127
6128 Next_Entity (E);
6129 end loop;
6130 end Check_Formal_Packages;
6131
6132 ---------------------------------
6133 -- Check_Forward_Instantiation --
6134 ---------------------------------
6135
6136 procedure Check_Forward_Instantiation (Decl : Node_Id) is
6137 S : Entity_Id;
6138 Gen_Comp : Entity_Id := Cunit_Entity (Get_Source_Unit (Decl));
6139
6140 begin
6141 -- The instantiation appears before the generic body if we are in the
6142 -- scope of the unit containing the generic, either in its spec or in
6143 -- the package body, and before the generic body.
6144
6145 if Ekind (Gen_Comp) = E_Package_Body then
6146 Gen_Comp := Spec_Entity (Gen_Comp);
6147 end if;
6148
6149 if In_Open_Scopes (Gen_Comp)
6150 and then No (Corresponding_Body (Decl))
6151 then
6152 S := Current_Scope;
6153
6154 while Present (S)
6155 and then not Is_Compilation_Unit (S)
6156 and then not Is_Child_Unit (S)
6157 loop
6158 if Ekind (S) = E_Package then
6159 Set_Has_Forward_Instantiation (S);
6160 end if;
6161
6162 S := Scope (S);
6163 end loop;
6164 end if;
6165 end Check_Forward_Instantiation;
6166
6167 ---------------------------
6168 -- Check_Generic_Actuals --
6169 ---------------------------
6170
6171 -- The visibility of the actuals may be different between the point of
6172 -- generic instantiation and the instantiation of the body.
6173
6174 procedure Check_Generic_Actuals
6175 (Instance : Entity_Id;
6176 Is_Formal_Box : Boolean)
6177 is
6178 E : Entity_Id;
6179 Astype : Entity_Id;
6180
6181 function Denotes_Previous_Actual (Typ : Entity_Id) return Boolean;
6182 -- For a formal that is an array type, the component type is often a
6183 -- previous formal in the same unit. The privacy status of the component
6184 -- type will have been examined earlier in the traversal of the
6185 -- corresponding actuals, and this status should not be modified for
6186 -- the array (sub)type itself. However, if the base type of the array
6187 -- (sub)type is private, its full view must be restored in the body to
6188 -- be consistent with subsequent index subtypes, etc.
6189 --
6190 -- To detect this case we have to rescan the list of formals, which is
6191 -- usually short enough to ignore the resulting inefficiency.
6192
6193 -----------------------------
6194 -- Denotes_Previous_Actual --
6195 -----------------------------
6196
6197 function Denotes_Previous_Actual (Typ : Entity_Id) return Boolean is
6198 Prev : Entity_Id;
6199
6200 begin
6201 Prev := First_Entity (Instance);
6202 while Present (Prev) loop
6203 if Is_Type (Prev)
6204 and then Nkind (Parent (Prev)) = N_Subtype_Declaration
6205 and then Is_Entity_Name (Subtype_Indication (Parent (Prev)))
6206 and then Entity (Subtype_Indication (Parent (Prev))) = Typ
6207 then
6208 return True;
6209
6210 elsif Prev = E then
6211 return False;
6212
6213 else
6214 Next_Entity (Prev);
6215 end if;
6216 end loop;
6217
6218 return False;
6219 end Denotes_Previous_Actual;
6220
6221 -- Start of processing for Check_Generic_Actuals
6222
6223 begin
6224 E := First_Entity (Instance);
6225 while Present (E) loop
6226 if Is_Type (E)
6227 and then Nkind (Parent (E)) = N_Subtype_Declaration
6228 and then Scope (Etype (E)) /= Instance
6229 and then Is_Entity_Name (Subtype_Indication (Parent (E)))
6230 then
6231 if Is_Array_Type (E)
6232 and then not Is_Private_Type (Etype (E))
6233 and then Denotes_Previous_Actual (Component_Type (E))
6234 then
6235 null;
6236 else
6237 Check_Private_View (Subtype_Indication (Parent (E)));
6238 end if;
6239
6240 Set_Is_Generic_Actual_Type (E, True);
6241 Set_Is_Hidden (E, False);
6242 Set_Is_Potentially_Use_Visible (E,
6243 In_Use (Instance));
6244
6245 -- We constructed the generic actual type as a subtype of the
6246 -- supplied type. This means that it normally would not inherit
6247 -- subtype specific attributes of the actual, which is wrong for
6248 -- the generic case.
6249
6250 Astype := Ancestor_Subtype (E);
6251
6252 if No (Astype) then
6253
6254 -- This can happen when E is an itype that is the full view of
6255 -- a private type completed, e.g. with a constrained array. In
6256 -- that case, use the first subtype, which will carry size
6257 -- information. The base type itself is unconstrained and will
6258 -- not carry it.
6259
6260 Astype := First_Subtype (E);
6261 end if;
6262
6263 Set_Size_Info (E, (Astype));
6264 Set_RM_Size (E, RM_Size (Astype));
6265 Set_First_Rep_Item (E, First_Rep_Item (Astype));
6266
6267 if Is_Discrete_Or_Fixed_Point_Type (E) then
6268 Set_RM_Size (E, RM_Size (Astype));
6269
6270 -- In nested instances, the base type of an access actual may
6271 -- itself be private, and need to be exchanged.
6272
6273 elsif Is_Access_Type (E)
6274 and then Is_Private_Type (Etype (E))
6275 then
6276 Check_Private_View
6277 (New_Occurrence_Of (Etype (E), Sloc (Instance)));
6278 end if;
6279
6280 elsif Ekind (E) = E_Package then
6281
6282 -- If this is the renaming for the current instance, we're done.
6283 -- Otherwise it is a formal package. If the corresponding formal
6284 -- was declared with a box, the (instantiations of the) generic
6285 -- formal part are also visible. Otherwise, ignore the entity
6286 -- created to validate the actuals.
6287
6288 if Renamed_Object (E) = Instance then
6289 exit;
6290
6291 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
6292 null;
6293
6294 -- The visibility of a formal of an enclosing generic is already
6295 -- correct.
6296
6297 elsif Denotes_Formal_Package (E) then
6298 null;
6299
6300 elsif Present (Associated_Formal_Package (E))
6301 and then not Is_Generic_Formal (E)
6302 then
6303 if Box_Present (Parent (Associated_Formal_Package (E))) then
6304 Check_Generic_Actuals (Renamed_Object (E), True);
6305
6306 else
6307 Check_Generic_Actuals (Renamed_Object (E), False);
6308 end if;
6309
6310 Set_Is_Hidden (E, False);
6311 end if;
6312
6313 -- If this is a subprogram instance (in a wrapper package) the
6314 -- actual is fully visible.
6315
6316 elsif Is_Wrapper_Package (Instance) then
6317 Set_Is_Hidden (E, False);
6318
6319 -- If the formal package is declared with a box, or if the formal
6320 -- parameter is defaulted, it is visible in the body.
6321
6322 elsif Is_Formal_Box or else Is_Visible_Formal (E) then
6323 Set_Is_Hidden (E, False);
6324 end if;
6325
6326 if Ekind (E) = E_Constant then
6327
6328 -- If the type of the actual is a private type declared in the
6329 -- enclosing scope of the generic unit, the body of the generic
6330 -- sees the full view of the type (because it has to appear in
6331 -- the corresponding package body). If the type is private now,
6332 -- exchange views to restore the proper visiblity in the instance.
6333
6334 declare
6335 Typ : constant Entity_Id := Base_Type (Etype (E));
6336 -- The type of the actual
6337
6338 Gen_Id : Entity_Id;
6339 -- The generic unit
6340
6341 Parent_Scope : Entity_Id;
6342 -- The enclosing scope of the generic unit
6343
6344 begin
6345 if Is_Wrapper_Package (Instance) then
6346 Gen_Id :=
6347 Generic_Parent
6348 (Specification
6349 (Unit_Declaration_Node
6350 (Related_Instance (Instance))));
6351 else
6352 Gen_Id :=
6353 Generic_Parent (Package_Specification (Instance));
6354 end if;
6355
6356 Parent_Scope := Scope (Gen_Id);
6357
6358 -- The exchange is only needed if the generic is defined
6359 -- within a package which is not a common ancestor of the
6360 -- scope of the instance, and is not already in scope.
6361
6362 if Is_Private_Type (Typ)
6363 and then Scope (Typ) = Parent_Scope
6364 and then Scope (Instance) /= Parent_Scope
6365 and then Ekind (Parent_Scope) = E_Package
6366 and then not Is_Child_Unit (Gen_Id)
6367 then
6368 Switch_View (Typ);
6369
6370 -- If the type of the entity is a subtype, it may also have
6371 -- to be made visible, together with the base type of its
6372 -- full view, after exchange.
6373
6374 if Is_Private_Type (Etype (E)) then
6375 Switch_View (Etype (E));
6376 Switch_View (Base_Type (Etype (E)));
6377 end if;
6378 end if;
6379 end;
6380 end if;
6381
6382 Next_Entity (E);
6383 end loop;
6384 end Check_Generic_Actuals;
6385
6386 ------------------------------
6387 -- Check_Generic_Child_Unit --
6388 ------------------------------
6389
6390 procedure Check_Generic_Child_Unit
6391 (Gen_Id : Node_Id;
6392 Parent_Installed : in out Boolean)
6393 is
6394 Loc : constant Source_Ptr := Sloc (Gen_Id);
6395 Gen_Par : Entity_Id := Empty;
6396 E : Entity_Id;
6397 Inst_Par : Entity_Id;
6398 S : Node_Id;
6399
6400 function Find_Generic_Child
6401 (Scop : Entity_Id;
6402 Id : Node_Id) return Entity_Id;
6403 -- Search generic parent for possible child unit with the given name
6404
6405 function In_Enclosing_Instance return Boolean;
6406 -- Within an instance of the parent, the child unit may be denoted by
6407 -- a simple name, or an abbreviated expanded name. Examine enclosing
6408 -- scopes to locate a possible parent instantiation.
6409
6410 ------------------------
6411 -- Find_Generic_Child --
6412 ------------------------
6413
6414 function Find_Generic_Child
6415 (Scop : Entity_Id;
6416 Id : Node_Id) return Entity_Id
6417 is
6418 E : Entity_Id;
6419
6420 begin
6421 -- If entity of name is already set, instance has already been
6422 -- resolved, e.g. in an enclosing instantiation.
6423
6424 if Present (Entity (Id)) then
6425 if Scope (Entity (Id)) = Scop then
6426 return Entity (Id);
6427 else
6428 return Empty;
6429 end if;
6430
6431 else
6432 E := First_Entity (Scop);
6433 while Present (E) loop
6434 if Chars (E) = Chars (Id)
6435 and then Is_Child_Unit (E)
6436 then
6437 if Is_Child_Unit (E)
6438 and then not Is_Visible_Lib_Unit (E)
6439 then
6440 Error_Msg_NE
6441 ("generic child unit& is not visible", Gen_Id, E);
6442 end if;
6443
6444 Set_Entity (Id, E);
6445 return E;
6446 end if;
6447
6448 Next_Entity (E);
6449 end loop;
6450
6451 return Empty;
6452 end if;
6453 end Find_Generic_Child;
6454
6455 ---------------------------
6456 -- In_Enclosing_Instance --
6457 ---------------------------
6458
6459 function In_Enclosing_Instance return Boolean is
6460 Enclosing_Instance : Node_Id;
6461 Instance_Decl : Node_Id;
6462
6463 begin
6464 -- We do not inline any call that contains instantiations, except
6465 -- for instantiations of Unchecked_Conversion, so if we are within
6466 -- an inlined body the current instance does not require parents.
6467
6468 if In_Inlined_Body then
6469 pragma Assert (Chars (Gen_Id) = Name_Unchecked_Conversion);
6470 return False;
6471 end if;
6472
6473 -- Loop to check enclosing scopes
6474
6475 Enclosing_Instance := Current_Scope;
6476 while Present (Enclosing_Instance) loop
6477 Instance_Decl := Unit_Declaration_Node (Enclosing_Instance);
6478
6479 if Ekind (Enclosing_Instance) = E_Package
6480 and then Is_Generic_Instance (Enclosing_Instance)
6481 and then Present
6482 (Generic_Parent (Specification (Instance_Decl)))
6483 then
6484 -- Check whether the generic we are looking for is a child of
6485 -- this instance.
6486
6487 E := Find_Generic_Child
6488 (Generic_Parent (Specification (Instance_Decl)), Gen_Id);
6489 exit when Present (E);
6490
6491 else
6492 E := Empty;
6493 end if;
6494
6495 Enclosing_Instance := Scope (Enclosing_Instance);
6496 end loop;
6497
6498 if No (E) then
6499
6500 -- Not a child unit
6501
6502 Analyze (Gen_Id);
6503 return False;
6504
6505 else
6506 Rewrite (Gen_Id,
6507 Make_Expanded_Name (Loc,
6508 Chars => Chars (E),
6509 Prefix => New_Occurrence_Of (Enclosing_Instance, Loc),
6510 Selector_Name => New_Occurrence_Of (E, Loc)));
6511
6512 Set_Entity (Gen_Id, E);
6513 Set_Etype (Gen_Id, Etype (E));
6514 Parent_Installed := False; -- Already in scope.
6515 return True;
6516 end if;
6517 end In_Enclosing_Instance;
6518
6519 -- Start of processing for Check_Generic_Child_Unit
6520
6521 begin
6522 -- If the name of the generic is given by a selected component, it may
6523 -- be the name of a generic child unit, and the prefix is the name of an
6524 -- instance of the parent, in which case the child unit must be visible.
6525 -- If this instance is not in scope, it must be placed there and removed
6526 -- after instantiation, because what is being instantiated is not the
6527 -- original child, but the corresponding child present in the instance
6528 -- of the parent.
6529
6530 -- If the child is instantiated within the parent, it can be given by
6531 -- a simple name. In this case the instance is already in scope, but
6532 -- the child generic must be recovered from the generic parent as well.
6533
6534 if Nkind (Gen_Id) = N_Selected_Component then
6535 S := Selector_Name (Gen_Id);
6536 Analyze (Prefix (Gen_Id));
6537 Inst_Par := Entity (Prefix (Gen_Id));
6538
6539 if Ekind (Inst_Par) = E_Package
6540 and then Present (Renamed_Object (Inst_Par))
6541 then
6542 Inst_Par := Renamed_Object (Inst_Par);
6543 end if;
6544
6545 if Ekind (Inst_Par) = E_Package then
6546 if Nkind (Parent (Inst_Par)) = N_Package_Specification then
6547 Gen_Par := Generic_Parent (Parent (Inst_Par));
6548
6549 elsif Nkind (Parent (Inst_Par)) = N_Defining_Program_Unit_Name
6550 and then
6551 Nkind (Parent (Parent (Inst_Par))) = N_Package_Specification
6552 then
6553 Gen_Par := Generic_Parent (Parent (Parent (Inst_Par)));
6554 end if;
6555
6556 elsif Ekind (Inst_Par) = E_Generic_Package
6557 and then Nkind (Parent (Gen_Id)) = N_Formal_Package_Declaration
6558 then
6559 -- A formal package may be a real child package, and not the
6560 -- implicit instance within a parent. In this case the child is
6561 -- not visible and has to be retrieved explicitly as well.
6562
6563 Gen_Par := Inst_Par;
6564 end if;
6565
6566 if Present (Gen_Par) then
6567
6568 -- The prefix denotes an instantiation. The entity itself may be a
6569 -- nested generic, or a child unit.
6570
6571 E := Find_Generic_Child (Gen_Par, S);
6572
6573 if Present (E) then
6574 Change_Selected_Component_To_Expanded_Name (Gen_Id);
6575 Set_Entity (Gen_Id, E);
6576 Set_Etype (Gen_Id, Etype (E));
6577 Set_Entity (S, E);
6578 Set_Etype (S, Etype (E));
6579
6580 -- Indicate that this is a reference to the parent
6581
6582 if In_Extended_Main_Source_Unit (Gen_Id) then
6583 Set_Is_Instantiated (Inst_Par);
6584 end if;
6585
6586 -- A common mistake is to replicate the naming scheme of a
6587 -- hierarchy by instantiating a generic child directly, rather
6588 -- than the implicit child in a parent instance:
6589
6590 -- generic .. package Gpar is ..
6591 -- generic .. package Gpar.Child is ..
6592 -- package Par is new Gpar ();
6593
6594 -- with Gpar.Child;
6595 -- package Par.Child is new Gpar.Child ();
6596 -- rather than Par.Child
6597
6598 -- In this case the instantiation is within Par, which is an
6599 -- instance, but Gpar does not denote Par because we are not IN
6600 -- the instance of Gpar, so this is illegal. The test below
6601 -- recognizes this particular case.
6602
6603 if Is_Child_Unit (E)
6604 and then not Comes_From_Source (Entity (Prefix (Gen_Id)))
6605 and then (not In_Instance
6606 or else Nkind (Parent (Parent (Gen_Id))) =
6607 N_Compilation_Unit)
6608 then
6609 Error_Msg_N
6610 ("prefix of generic child unit must be instance of parent",
6611 Gen_Id);
6612 end if;
6613
6614 if not In_Open_Scopes (Inst_Par)
6615 and then Nkind (Parent (Gen_Id)) not in
6616 N_Generic_Renaming_Declaration
6617 then
6618 Install_Parent (Inst_Par);
6619 Parent_Installed := True;
6620
6621 elsif In_Open_Scopes (Inst_Par) then
6622
6623 -- If the parent is already installed, install the actuals
6624 -- for its formal packages. This is necessary when the child
6625 -- instance is a child of the parent instance: in this case,
6626 -- the parent is placed on the scope stack but the formal
6627 -- packages are not made visible.
6628
6629 Install_Formal_Packages (Inst_Par);
6630 end if;
6631
6632 else
6633 -- If the generic parent does not contain an entity that
6634 -- corresponds to the selector, the instance doesn't either.
6635 -- Analyzing the node will yield the appropriate error message.
6636 -- If the entity is not a child unit, then it is an inner
6637 -- generic in the parent.
6638
6639 Analyze (Gen_Id);
6640 end if;
6641
6642 else
6643 Analyze (Gen_Id);
6644
6645 if Is_Child_Unit (Entity (Gen_Id))
6646 and then
6647 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
6648 and then not In_Open_Scopes (Inst_Par)
6649 then
6650 Install_Parent (Inst_Par);
6651 Parent_Installed := True;
6652
6653 -- The generic unit may be the renaming of the implicit child
6654 -- present in an instance. In that case the parent instance is
6655 -- obtained from the name of the renamed entity.
6656
6657 elsif Ekind (Entity (Gen_Id)) = E_Generic_Package
6658 and then Present (Renamed_Entity (Entity (Gen_Id)))
6659 and then Is_Child_Unit (Renamed_Entity (Entity (Gen_Id)))
6660 then
6661 declare
6662 Renamed_Package : constant Node_Id :=
6663 Name (Parent (Entity (Gen_Id)));
6664 begin
6665 if Nkind (Renamed_Package) = N_Expanded_Name then
6666 Inst_Par := Entity (Prefix (Renamed_Package));
6667 Install_Parent (Inst_Par);
6668 Parent_Installed := True;
6669 end if;
6670 end;
6671 end if;
6672 end if;
6673
6674 elsif Nkind (Gen_Id) = N_Expanded_Name then
6675
6676 -- Entity already present, analyze prefix, whose meaning may be
6677 -- an instance in the current context. If it is an instance of
6678 -- a relative within another, the proper parent may still have
6679 -- to be installed, if they are not of the same generation.
6680
6681 Analyze (Prefix (Gen_Id));
6682
6683 -- In the unlikely case that a local declaration hides the name
6684 -- of the parent package, locate it on the homonym chain. If the
6685 -- context is an instance of the parent, the renaming entity is
6686 -- flagged as such.
6687
6688 Inst_Par := Entity (Prefix (Gen_Id));
6689 while Present (Inst_Par)
6690 and then not Is_Package_Or_Generic_Package (Inst_Par)
6691 loop
6692 Inst_Par := Homonym (Inst_Par);
6693 end loop;
6694
6695 pragma Assert (Present (Inst_Par));
6696 Set_Entity (Prefix (Gen_Id), Inst_Par);
6697
6698 if In_Enclosing_Instance then
6699 null;
6700
6701 elsif Present (Entity (Gen_Id))
6702 and then Is_Child_Unit (Entity (Gen_Id))
6703 and then not In_Open_Scopes (Inst_Par)
6704 then
6705 Install_Parent (Inst_Par);
6706 Parent_Installed := True;
6707 end if;
6708
6709 elsif In_Enclosing_Instance then
6710
6711 -- The child unit is found in some enclosing scope
6712
6713 null;
6714
6715 else
6716 Analyze (Gen_Id);
6717
6718 -- If this is the renaming of the implicit child in a parent
6719 -- instance, recover the parent name and install it.
6720
6721 if Is_Entity_Name (Gen_Id) then
6722 E := Entity (Gen_Id);
6723
6724 if Is_Generic_Unit (E)
6725 and then Nkind (Parent (E)) in N_Generic_Renaming_Declaration
6726 and then Is_Child_Unit (Renamed_Object (E))
6727 and then Is_Generic_Unit (Scope (Renamed_Object (E)))
6728 and then Nkind (Name (Parent (E))) = N_Expanded_Name
6729 then
6730 Rewrite (Gen_Id, New_Copy_Tree (Name (Parent (E))));
6731 Inst_Par := Entity (Prefix (Gen_Id));
6732
6733 if not In_Open_Scopes (Inst_Par) then
6734 Install_Parent (Inst_Par);
6735 Parent_Installed := True;
6736 end if;
6737
6738 -- If it is a child unit of a non-generic parent, it may be
6739 -- use-visible and given by a direct name. Install parent as
6740 -- for other cases.
6741
6742 elsif Is_Generic_Unit (E)
6743 and then Is_Child_Unit (E)
6744 and then
6745 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
6746 and then not Is_Generic_Unit (Scope (E))
6747 then
6748 if not In_Open_Scopes (Scope (E)) then
6749 Install_Parent (Scope (E));
6750 Parent_Installed := True;
6751 end if;
6752 end if;
6753 end if;
6754 end if;
6755 end Check_Generic_Child_Unit;
6756
6757 -----------------------------
6758 -- Check_Hidden_Child_Unit --
6759 -----------------------------
6760
6761 procedure Check_Hidden_Child_Unit
6762 (N : Node_Id;
6763 Gen_Unit : Entity_Id;
6764 Act_Decl_Id : Entity_Id)
6765 is
6766 Gen_Id : constant Node_Id := Name (N);
6767
6768 begin
6769 if Is_Child_Unit (Gen_Unit)
6770 and then Is_Child_Unit (Act_Decl_Id)
6771 and then Nkind (Gen_Id) = N_Expanded_Name
6772 and then Entity (Prefix (Gen_Id)) = Scope (Act_Decl_Id)
6773 and then Chars (Gen_Unit) = Chars (Act_Decl_Id)
6774 then
6775 Error_Msg_Node_2 := Scope (Act_Decl_Id);
6776 Error_Msg_NE
6777 ("generic unit & is implicitly declared in &",
6778 Defining_Unit_Name (N), Gen_Unit);
6779 Error_Msg_N ("\instance must have different name",
6780 Defining_Unit_Name (N));
6781 end if;
6782 end Check_Hidden_Child_Unit;
6783
6784 ------------------------
6785 -- Check_Private_View --
6786 ------------------------
6787
6788 procedure Check_Private_View (N : Node_Id) is
6789 T : constant Entity_Id := Etype (N);
6790 BT : Entity_Id;
6791
6792 begin
6793 -- Exchange views if the type was not private in the generic but is
6794 -- private at the point of instantiation. Do not exchange views if
6795 -- the scope of the type is in scope. This can happen if both generic
6796 -- and instance are sibling units, or if type is defined in a parent.
6797 -- In this case the visibility of the type will be correct for all
6798 -- semantic checks.
6799
6800 if Present (T) then
6801 BT := Base_Type (T);
6802
6803 if Is_Private_Type (T)
6804 and then not Has_Private_View (N)
6805 and then Present (Full_View (T))
6806 and then not In_Open_Scopes (Scope (T))
6807 then
6808 -- In the generic, the full type was visible. Save the private
6809 -- entity, for subsequent exchange.
6810
6811 Switch_View (T);
6812
6813 elsif Has_Private_View (N)
6814 and then not Is_Private_Type (T)
6815 and then not Has_Been_Exchanged (T)
6816 and then Etype (Get_Associated_Node (N)) /= T
6817 then
6818 -- Only the private declaration was visible in the generic. If
6819 -- the type appears in a subtype declaration, the subtype in the
6820 -- instance must have a view compatible with that of its parent,
6821 -- which must be exchanged (see corresponding code in Restore_
6822 -- Private_Views). Otherwise, if the type is defined in a parent
6823 -- unit, leave full visibility within instance, which is safe.
6824
6825 if In_Open_Scopes (Scope (Base_Type (T)))
6826 and then not Is_Private_Type (Base_Type (T))
6827 and then Comes_From_Source (Base_Type (T))
6828 then
6829 null;
6830
6831 elsif Nkind (Parent (N)) = N_Subtype_Declaration
6832 or else not In_Private_Part (Scope (Base_Type (T)))
6833 then
6834 Prepend_Elmt (T, Exchanged_Views);
6835 Exchange_Declarations (Etype (Get_Associated_Node (N)));
6836 end if;
6837
6838 -- For composite types with inconsistent representation exchange
6839 -- component types accordingly.
6840
6841 elsif Is_Access_Type (T)
6842 and then Is_Private_Type (Designated_Type (T))
6843 and then not Has_Private_View (N)
6844 and then Present (Full_View (Designated_Type (T)))
6845 then
6846 Switch_View (Designated_Type (T));
6847
6848 elsif Is_Array_Type (T) then
6849 if Is_Private_Type (Component_Type (T))
6850 and then not Has_Private_View (N)
6851 and then Present (Full_View (Component_Type (T)))
6852 then
6853 Switch_View (Component_Type (T));
6854 end if;
6855
6856 -- The normal exchange mechanism relies on the setting of a
6857 -- flag on the reference in the generic. However, an additional
6858 -- mechanism is needed for types that are not explicitly
6859 -- mentioned in the generic, but may be needed in expanded code
6860 -- in the instance. This includes component types of arrays and
6861 -- designated types of access types. This processing must also
6862 -- include the index types of arrays which we take care of here.
6863
6864 declare
6865 Indx : Node_Id;
6866 Typ : Entity_Id;
6867
6868 begin
6869 Indx := First_Index (T);
6870 while Present (Indx) loop
6871 Typ := Base_Type (Etype (Indx));
6872
6873 if Is_Private_Type (Typ)
6874 and then Present (Full_View (Typ))
6875 then
6876 Switch_View (Typ);
6877 end if;
6878
6879 Next_Index (Indx);
6880 end loop;
6881 end;
6882
6883 elsif Is_Private_Type (T)
6884 and then Present (Full_View (T))
6885 and then Is_Array_Type (Full_View (T))
6886 and then Is_Private_Type (Component_Type (Full_View (T)))
6887 then
6888 Switch_View (T);
6889
6890 -- Finally, a non-private subtype may have a private base type, which
6891 -- must be exchanged for consistency. This can happen when a package
6892 -- body is instantiated, when the scope stack is empty but in fact
6893 -- the subtype and the base type are declared in an enclosing scope.
6894
6895 -- Note that in this case we introduce an inconsistency in the view
6896 -- set, because we switch the base type BT, but there could be some
6897 -- private dependent subtypes of BT which remain unswitched. Such
6898 -- subtypes might need to be switched at a later point (see specific
6899 -- provision for that case in Switch_View).
6900
6901 elsif not Is_Private_Type (T)
6902 and then not Has_Private_View (N)
6903 and then Is_Private_Type (BT)
6904 and then Present (Full_View (BT))
6905 and then not Is_Generic_Type (BT)
6906 and then not In_Open_Scopes (BT)
6907 then
6908 Prepend_Elmt (Full_View (BT), Exchanged_Views);
6909 Exchange_Declarations (BT);
6910 end if;
6911 end if;
6912 end Check_Private_View;
6913
6914 -----------------------------
6915 -- Check_Hidden_Primitives --
6916 -----------------------------
6917
6918 function Check_Hidden_Primitives (Assoc_List : List_Id) return Elist_Id is
6919 Actual : Node_Id;
6920 Gen_T : Entity_Id;
6921 Result : Elist_Id := No_Elist;
6922
6923 begin
6924 if No (Assoc_List) then
6925 return No_Elist;
6926 end if;
6927
6928 -- Traverse the list of associations between formals and actuals
6929 -- searching for renamings of tagged types
6930
6931 Actual := First (Assoc_List);
6932 while Present (Actual) loop
6933 if Nkind (Actual) = N_Subtype_Declaration then
6934 Gen_T := Generic_Parent_Type (Actual);
6935
6936 if Present (Gen_T) and then Is_Tagged_Type (Gen_T) then
6937
6938 -- Traverse the list of primitives of the actual types
6939 -- searching for hidden primitives that are visible in the
6940 -- corresponding generic formal; leave them visible and
6941 -- append them to Result to restore their decoration later.
6942
6943 Install_Hidden_Primitives
6944 (Prims_List => Result,
6945 Gen_T => Gen_T,
6946 Act_T => Entity (Subtype_Indication (Actual)));
6947 end if;
6948 end if;
6949
6950 Next (Actual);
6951 end loop;
6952
6953 return Result;
6954 end Check_Hidden_Primitives;
6955
6956 --------------------------
6957 -- Contains_Instance_Of --
6958 --------------------------
6959
6960 function Contains_Instance_Of
6961 (Inner : Entity_Id;
6962 Outer : Entity_Id;
6963 N : Node_Id) return Boolean
6964 is
6965 Elmt : Elmt_Id;
6966 Scop : Entity_Id;
6967
6968 begin
6969 Scop := Outer;
6970
6971 -- Verify that there are no circular instantiations. We check whether
6972 -- the unit contains an instance of the current scope or some enclosing
6973 -- scope (in case one of the instances appears in a subunit). Longer
6974 -- circularities involving subunits might seem too pathological to
6975 -- consider, but they were not too pathological for the authors of
6976 -- DEC bc30vsq, so we loop over all enclosing scopes, and mark all
6977 -- enclosing generic scopes as containing an instance.
6978
6979 loop
6980 -- Within a generic subprogram body, the scope is not generic, to
6981 -- allow for recursive subprograms. Use the declaration to determine
6982 -- whether this is a generic unit.
6983
6984 if Ekind (Scop) = E_Generic_Package
6985 or else (Is_Subprogram (Scop)
6986 and then Nkind (Unit_Declaration_Node (Scop)) =
6987 N_Generic_Subprogram_Declaration)
6988 then
6989 Elmt := First_Elmt (Inner_Instances (Inner));
6990
6991 while Present (Elmt) loop
6992 if Node (Elmt) = Scop then
6993 Error_Msg_Node_2 := Inner;
6994 Error_Msg_NE
6995 ("circular Instantiation: & instantiated within &!",
6996 N, Scop);
6997 return True;
6998
6999 elsif Node (Elmt) = Inner then
7000 return True;
7001
7002 elsif Contains_Instance_Of (Node (Elmt), Scop, N) then
7003 Error_Msg_Node_2 := Inner;
7004 Error_Msg_NE
7005 ("circular Instantiation: & instantiated within &!",
7006 N, Node (Elmt));
7007 return True;
7008 end if;
7009
7010 Next_Elmt (Elmt);
7011 end loop;
7012
7013 -- Indicate that Inner is being instantiated within Scop
7014
7015 Append_Elmt (Inner, Inner_Instances (Scop));
7016 end if;
7017
7018 if Scop = Standard_Standard then
7019 exit;
7020 else
7021 Scop := Scope (Scop);
7022 end if;
7023 end loop;
7024
7025 return False;
7026 end Contains_Instance_Of;
7027
7028 -----------------------
7029 -- Copy_Generic_Node --
7030 -----------------------
7031
7032 function Copy_Generic_Node
7033 (N : Node_Id;
7034 Parent_Id : Node_Id;
7035 Instantiating : Boolean) return Node_Id
7036 is
7037 Ent : Entity_Id;
7038 New_N : Node_Id;
7039
7040 function Copy_Generic_Descendant (D : Union_Id) return Union_Id;
7041 -- Check the given value of one of the Fields referenced by the current
7042 -- node to determine whether to copy it recursively. The field may hold
7043 -- a Node_Id, a List_Id, or an Elist_Id, or a plain value (Sloc, Uint,
7044 -- Char) in which case it need not be copied.
7045
7046 procedure Copy_Descendants;
7047 -- Common utility for various nodes
7048
7049 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id;
7050 -- Make copy of element list
7051
7052 function Copy_Generic_List
7053 (L : List_Id;
7054 Parent_Id : Node_Id) return List_Id;
7055 -- Apply Copy_Node recursively to the members of a node list
7056
7057 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean;
7058 -- True if an identifier is part of the defining program unit name of
7059 -- a child unit. The entity of such an identifier must be kept (for
7060 -- ASIS use) even though as the name of an enclosing generic it would
7061 -- otherwise not be preserved in the generic tree.
7062
7063 ----------------------
7064 -- Copy_Descendants --
7065 ----------------------
7066
7067 procedure Copy_Descendants is
7068 use Atree.Unchecked_Access;
7069 -- This code section is part of the implementation of an untyped
7070 -- tree traversal, so it needs direct access to node fields.
7071
7072 begin
7073 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
7074 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
7075 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
7076 Set_Field4 (New_N, Copy_Generic_Descendant (Field4 (N)));
7077 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
7078 end Copy_Descendants;
7079
7080 -----------------------------
7081 -- Copy_Generic_Descendant --
7082 -----------------------------
7083
7084 function Copy_Generic_Descendant (D : Union_Id) return Union_Id is
7085 begin
7086 if D = Union_Id (Empty) then
7087 return D;
7088
7089 elsif D in Node_Range then
7090 return Union_Id
7091 (Copy_Generic_Node (Node_Id (D), New_N, Instantiating));
7092
7093 elsif D in List_Range then
7094 return Union_Id (Copy_Generic_List (List_Id (D), New_N));
7095
7096 elsif D in Elist_Range then
7097 return Union_Id (Copy_Generic_Elist (Elist_Id (D)));
7098
7099 -- Nothing else is copyable (e.g. Uint values), return as is
7100
7101 else
7102 return D;
7103 end if;
7104 end Copy_Generic_Descendant;
7105
7106 ------------------------
7107 -- Copy_Generic_Elist --
7108 ------------------------
7109
7110 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id is
7111 M : Elmt_Id;
7112 L : Elist_Id;
7113
7114 begin
7115 if Present (E) then
7116 L := New_Elmt_List;
7117 M := First_Elmt (E);
7118 while Present (M) loop
7119 Append_Elmt
7120 (Copy_Generic_Node (Node (M), Empty, Instantiating), L);
7121 Next_Elmt (M);
7122 end loop;
7123
7124 return L;
7125
7126 else
7127 return No_Elist;
7128 end if;
7129 end Copy_Generic_Elist;
7130
7131 -----------------------
7132 -- Copy_Generic_List --
7133 -----------------------
7134
7135 function Copy_Generic_List
7136 (L : List_Id;
7137 Parent_Id : Node_Id) return List_Id
7138 is
7139 N : Node_Id;
7140 New_L : List_Id;
7141
7142 begin
7143 if Present (L) then
7144 New_L := New_List;
7145 Set_Parent (New_L, Parent_Id);
7146
7147 N := First (L);
7148 while Present (N) loop
7149 Append (Copy_Generic_Node (N, Empty, Instantiating), New_L);
7150 Next (N);
7151 end loop;
7152
7153 return New_L;
7154
7155 else
7156 return No_List;
7157 end if;
7158 end Copy_Generic_List;
7159
7160 ---------------------------
7161 -- In_Defining_Unit_Name --
7162 ---------------------------
7163
7164 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean is
7165 begin
7166 return
7167 Present (Parent (Nam))
7168 and then (Nkind (Parent (Nam)) = N_Defining_Program_Unit_Name
7169 or else
7170 (Nkind (Parent (Nam)) = N_Expanded_Name
7171 and then In_Defining_Unit_Name (Parent (Nam))));
7172 end In_Defining_Unit_Name;
7173
7174 -- Start of processing for Copy_Generic_Node
7175
7176 begin
7177 if N = Empty then
7178 return N;
7179 end if;
7180
7181 New_N := New_Copy (N);
7182
7183 -- Copy aspects if present
7184
7185 if Has_Aspects (N) then
7186 Set_Has_Aspects (New_N, False);
7187 Set_Aspect_Specifications
7188 (New_N, Copy_Generic_List (Aspect_Specifications (N), Parent_Id));
7189 end if;
7190
7191 if Instantiating then
7192 Adjust_Instantiation_Sloc (New_N, S_Adjustment);
7193 end if;
7194
7195 if not Is_List_Member (N) then
7196 Set_Parent (New_N, Parent_Id);
7197 end if;
7198
7199 -- Special casing for identifiers and other entity names and operators
7200
7201 if Nkind_In (New_N, N_Character_Literal,
7202 N_Expanded_Name,
7203 N_Identifier,
7204 N_Operator_Symbol)
7205 or else Nkind (New_N) in N_Op
7206 then
7207 if not Instantiating then
7208
7209 -- Link both nodes in order to assign subsequently the entity of
7210 -- the copy to the original node, in case this is a global
7211 -- reference.
7212
7213 Set_Associated_Node (N, New_N);
7214
7215 -- If we are within an instantiation, this is a nested generic
7216 -- that has already been analyzed at the point of definition.
7217 -- We must preserve references that were global to the enclosing
7218 -- parent at that point. Other occurrences, whether global or
7219 -- local to the current generic, must be resolved anew, so we
7220 -- reset the entity in the generic copy. A global reference has a
7221 -- smaller depth than the parent, or else the same depth in case
7222 -- both are distinct compilation units.
7223
7224 -- A child unit is implicitly declared within the enclosing parent
7225 -- but is in fact global to it, and must be preserved.
7226
7227 -- It is also possible for Current_Instantiated_Parent to be
7228 -- defined, and for this not to be a nested generic, namely if
7229 -- the unit is loaded through Rtsfind. In that case, the entity of
7230 -- New_N is only a link to the associated node, and not a defining
7231 -- occurrence.
7232
7233 -- The entities for parent units in the defining_program_unit of a
7234 -- generic child unit are established when the context of the unit
7235 -- is first analyzed, before the generic copy is made. They are
7236 -- preserved in the copy for use in ASIS queries.
7237
7238 Ent := Entity (New_N);
7239
7240 if No (Current_Instantiated_Parent.Gen_Id) then
7241 if No (Ent)
7242 or else Nkind (Ent) /= N_Defining_Identifier
7243 or else not In_Defining_Unit_Name (N)
7244 then
7245 Set_Associated_Node (New_N, Empty);
7246 end if;
7247
7248 elsif No (Ent)
7249 or else
7250 not Nkind_In (Ent, N_Defining_Identifier,
7251 N_Defining_Character_Literal,
7252 N_Defining_Operator_Symbol)
7253 or else No (Scope (Ent))
7254 or else
7255 (Scope (Ent) = Current_Instantiated_Parent.Gen_Id
7256 and then not Is_Child_Unit (Ent))
7257 or else
7258 (Scope_Depth (Scope (Ent)) >
7259 Scope_Depth (Current_Instantiated_Parent.Gen_Id)
7260 and then
7261 Get_Source_Unit (Ent) =
7262 Get_Source_Unit (Current_Instantiated_Parent.Gen_Id))
7263 then
7264 Set_Associated_Node (New_N, Empty);
7265 end if;
7266
7267 -- Case of instantiating identifier or some other name or operator
7268
7269 else
7270 -- If the associated node is still defined, the entity in it
7271 -- is global, and must be copied to the instance. If this copy
7272 -- is being made for a body to inline, it is applied to an
7273 -- instantiated tree, and the entity is already present and
7274 -- must be also preserved.
7275
7276 declare
7277 Assoc : constant Node_Id := Get_Associated_Node (N);
7278
7279 begin
7280 if Present (Assoc) then
7281 if Nkind (Assoc) = Nkind (N) then
7282 Set_Entity (New_N, Entity (Assoc));
7283 Check_Private_View (N);
7284
7285 -- The name in the call may be a selected component if the
7286 -- call has not been analyzed yet, as may be the case for
7287 -- pre/post conditions in a generic unit.
7288
7289 elsif Nkind (Assoc) = N_Function_Call
7290 and then Is_Entity_Name (Name (Assoc))
7291 then
7292 Set_Entity (New_N, Entity (Name (Assoc)));
7293
7294 elsif Nkind_In (Assoc, N_Defining_Identifier,
7295 N_Defining_Character_Literal,
7296 N_Defining_Operator_Symbol)
7297 and then Expander_Active
7298 then
7299 -- Inlining case: we are copying a tree that contains
7300 -- global entities, which are preserved in the copy to be
7301 -- used for subsequent inlining.
7302
7303 null;
7304
7305 else
7306 Set_Entity (New_N, Empty);
7307 end if;
7308 end if;
7309 end;
7310 end if;
7311
7312 -- For expanded name, we must copy the Prefix and Selector_Name
7313
7314 if Nkind (N) = N_Expanded_Name then
7315 Set_Prefix
7316 (New_N, Copy_Generic_Node (Prefix (N), New_N, Instantiating));
7317
7318 Set_Selector_Name (New_N,
7319 Copy_Generic_Node (Selector_Name (N), New_N, Instantiating));
7320
7321 -- For operators, we must copy the right operand
7322
7323 elsif Nkind (N) in N_Op then
7324 Set_Right_Opnd (New_N,
7325 Copy_Generic_Node (Right_Opnd (N), New_N, Instantiating));
7326
7327 -- And for binary operators, the left operand as well
7328
7329 if Nkind (N) in N_Binary_Op then
7330 Set_Left_Opnd (New_N,
7331 Copy_Generic_Node (Left_Opnd (N), New_N, Instantiating));
7332 end if;
7333 end if;
7334
7335 -- Establish a link between an entity from the generic template and the
7336 -- corresponding entity in the generic copy to be analyzed.
7337
7338 elsif Nkind (N) in N_Entity then
7339 if not Instantiating then
7340 Set_Associated_Entity (N, New_N);
7341 end if;
7342
7343 -- Clear any existing link the copy may inherit from the replicated
7344 -- generic template entity.
7345
7346 Set_Associated_Entity (New_N, Empty);
7347
7348 -- Special casing for stubs
7349
7350 elsif Nkind (N) in N_Body_Stub then
7351
7352 -- In any case, we must copy the specification or defining
7353 -- identifier as appropriate.
7354
7355 if Nkind (N) = N_Subprogram_Body_Stub then
7356 Set_Specification (New_N,
7357 Copy_Generic_Node (Specification (N), New_N, Instantiating));
7358
7359 else
7360 Set_Defining_Identifier (New_N,
7361 Copy_Generic_Node
7362 (Defining_Identifier (N), New_N, Instantiating));
7363 end if;
7364
7365 -- If we are not instantiating, then this is where we load and
7366 -- analyze subunits, i.e. at the point where the stub occurs. A
7367 -- more permissive system might defer this analysis to the point
7368 -- of instantiation, but this seems too complicated for now.
7369
7370 if not Instantiating then
7371 declare
7372 Subunit_Name : constant Unit_Name_Type := Get_Unit_Name (N);
7373 Subunit : Node_Id;
7374 Unum : Unit_Number_Type;
7375 New_Body : Node_Id;
7376
7377 begin
7378 -- Make sure that, if it is a subunit of the main unit that is
7379 -- preprocessed and if -gnateG is specified, the preprocessed
7380 -- file will be written.
7381
7382 Lib.Analysing_Subunit_Of_Main :=
7383 Lib.In_Extended_Main_Source_Unit (N);
7384 Unum :=
7385 Load_Unit
7386 (Load_Name => Subunit_Name,
7387 Required => False,
7388 Subunit => True,
7389 Error_Node => N);
7390 Lib.Analysing_Subunit_Of_Main := False;
7391
7392 -- If the proper body is not found, a warning message will be
7393 -- emitted when analyzing the stub, or later at the point of
7394 -- instantiation. Here we just leave the stub as is.
7395
7396 if Unum = No_Unit then
7397 Subunits_Missing := True;
7398 goto Subunit_Not_Found;
7399 end if;
7400
7401 Subunit := Cunit (Unum);
7402
7403 if Nkind (Unit (Subunit)) /= N_Subunit then
7404 Error_Msg_N
7405 ("found child unit instead of expected SEPARATE subunit",
7406 Subunit);
7407 Error_Msg_Sloc := Sloc (N);
7408 Error_Msg_N ("\to complete stub #", Subunit);
7409 goto Subunit_Not_Found;
7410 end if;
7411
7412 -- We must create a generic copy of the subunit, in order to
7413 -- perform semantic analysis on it, and we must replace the
7414 -- stub in the original generic unit with the subunit, in order
7415 -- to preserve non-local references within.
7416
7417 -- Only the proper body needs to be copied. Library_Unit and
7418 -- context clause are simply inherited by the generic copy.
7419 -- Note that the copy (which may be recursive if there are
7420 -- nested subunits) must be done first, before attaching it to
7421 -- the enclosing generic.
7422
7423 New_Body :=
7424 Copy_Generic_Node
7425 (Proper_Body (Unit (Subunit)),
7426 Empty, Instantiating => False);
7427
7428 -- Now place the original proper body in the original generic
7429 -- unit. This is a body, not a compilation unit.
7430
7431 Rewrite (N, Proper_Body (Unit (Subunit)));
7432 Set_Is_Compilation_Unit (Defining_Entity (N), False);
7433 Set_Was_Originally_Stub (N);
7434
7435 -- Finally replace the body of the subunit with its copy, and
7436 -- make this new subunit into the library unit of the generic
7437 -- copy, which does not have stubs any longer.
7438
7439 Set_Proper_Body (Unit (Subunit), New_Body);
7440 Set_Library_Unit (New_N, Subunit);
7441 Inherit_Context (Unit (Subunit), N);
7442 end;
7443
7444 -- If we are instantiating, this must be an error case, since
7445 -- otherwise we would have replaced the stub node by the proper body
7446 -- that corresponds. So just ignore it in the copy (i.e. we have
7447 -- copied it, and that is good enough).
7448
7449 else
7450 null;
7451 end if;
7452
7453 <<Subunit_Not_Found>> null;
7454
7455 -- If the node is a compilation unit, it is the subunit of a stub, which
7456 -- has been loaded already (see code below). In this case, the library
7457 -- unit field of N points to the parent unit (which is a compilation
7458 -- unit) and need not (and cannot) be copied.
7459
7460 -- When the proper body of the stub is analyzed, the library_unit link
7461 -- is used to establish the proper context (see sem_ch10).
7462
7463 -- The other fields of a compilation unit are copied as usual
7464
7465 elsif Nkind (N) = N_Compilation_Unit then
7466
7467 -- This code can only be executed when not instantiating, because in
7468 -- the copy made for an instantiation, the compilation unit node has
7469 -- disappeared at the point that a stub is replaced by its proper
7470 -- body.
7471
7472 pragma Assert (not Instantiating);
7473
7474 Set_Context_Items (New_N,
7475 Copy_Generic_List (Context_Items (N), New_N));
7476
7477 Set_Unit (New_N,
7478 Copy_Generic_Node (Unit (N), New_N, False));
7479
7480 Set_First_Inlined_Subprogram (New_N,
7481 Copy_Generic_Node
7482 (First_Inlined_Subprogram (N), New_N, False));
7483
7484 Set_Aux_Decls_Node (New_N,
7485 Copy_Generic_Node (Aux_Decls_Node (N), New_N, False));
7486
7487 -- For an assignment node, the assignment is known to be semantically
7488 -- legal if we are instantiating the template. This avoids incorrect
7489 -- diagnostics in generated code.
7490
7491 elsif Nkind (N) = N_Assignment_Statement then
7492
7493 -- Copy name and expression fields in usual manner
7494
7495 Set_Name (New_N,
7496 Copy_Generic_Node (Name (N), New_N, Instantiating));
7497
7498 Set_Expression (New_N,
7499 Copy_Generic_Node (Expression (N), New_N, Instantiating));
7500
7501 if Instantiating then
7502 Set_Assignment_OK (Name (New_N), True);
7503 end if;
7504
7505 elsif Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
7506 if not Instantiating then
7507 Set_Associated_Node (N, New_N);
7508
7509 else
7510 if Present (Get_Associated_Node (N))
7511 and then Nkind (Get_Associated_Node (N)) = Nkind (N)
7512 then
7513 -- In the generic the aggregate has some composite type. If at
7514 -- the point of instantiation the type has a private view,
7515 -- install the full view (and that of its ancestors, if any).
7516
7517 declare
7518 T : Entity_Id := (Etype (Get_Associated_Node (New_N)));
7519 Rt : Entity_Id;
7520
7521 begin
7522 if Present (T) and then Is_Private_Type (T) then
7523 Switch_View (T);
7524 end if;
7525
7526 if Present (T)
7527 and then Is_Tagged_Type (T)
7528 and then Is_Derived_Type (T)
7529 then
7530 Rt := Root_Type (T);
7531
7532 loop
7533 T := Etype (T);
7534
7535 if Is_Private_Type (T) then
7536 Switch_View (T);
7537 end if;
7538
7539 exit when T = Rt;
7540 end loop;
7541 end if;
7542 end;
7543 end if;
7544 end if;
7545
7546 -- Do not copy the associated node, which points to the generic copy
7547 -- of the aggregate.
7548
7549 declare
7550 use Atree.Unchecked_Access;
7551 -- This code section is part of the implementation of an untyped
7552 -- tree traversal, so it needs direct access to node fields.
7553
7554 begin
7555 Set_Field1 (New_N, Copy_Generic_Descendant (Field1 (N)));
7556 Set_Field2 (New_N, Copy_Generic_Descendant (Field2 (N)));
7557 Set_Field3 (New_N, Copy_Generic_Descendant (Field3 (N)));
7558 Set_Field5 (New_N, Copy_Generic_Descendant (Field5 (N)));
7559 end;
7560
7561 -- Allocators do not have an identifier denoting the access type, so we
7562 -- must locate it through the expression to check whether the views are
7563 -- consistent.
7564
7565 elsif Nkind (N) = N_Allocator
7566 and then Nkind (Expression (N)) = N_Qualified_Expression
7567 and then Is_Entity_Name (Subtype_Mark (Expression (N)))
7568 and then Instantiating
7569 then
7570 declare
7571 T : constant Node_Id :=
7572 Get_Associated_Node (Subtype_Mark (Expression (N)));
7573 Acc_T : Entity_Id;
7574
7575 begin
7576 if Present (T) then
7577
7578 -- Retrieve the allocator node in the generic copy
7579
7580 Acc_T := Etype (Parent (Parent (T)));
7581
7582 if Present (Acc_T) and then Is_Private_Type (Acc_T) then
7583 Switch_View (Acc_T);
7584 end if;
7585 end if;
7586
7587 Copy_Descendants;
7588 end;
7589
7590 -- For a proper body, we must catch the case of a proper body that
7591 -- replaces a stub. This represents the point at which a separate
7592 -- compilation unit, and hence template file, may be referenced, so we
7593 -- must make a new source instantiation entry for the template of the
7594 -- subunit, and ensure that all nodes in the subunit are adjusted using
7595 -- this new source instantiation entry.
7596
7597 elsif Nkind (N) in N_Proper_Body then
7598 declare
7599 Save_Adjustment : constant Sloc_Adjustment := S_Adjustment;
7600
7601 begin
7602 if Instantiating and then Was_Originally_Stub (N) then
7603 Create_Instantiation_Source
7604 (Instantiation_Node,
7605 Defining_Entity (N),
7606 False,
7607 S_Adjustment);
7608 end if;
7609
7610 -- Now copy the fields of the proper body, using the new
7611 -- adjustment factor if one was needed as per test above.
7612
7613 Copy_Descendants;
7614
7615 -- Restore the original adjustment factor in case changed
7616
7617 S_Adjustment := Save_Adjustment;
7618 end;
7619
7620 elsif Nkind (N) = N_Pragma and then Instantiating then
7621
7622 -- Do not copy Comment or Ident pragmas their content is relevant to
7623 -- the generic unit, not to the instantiating unit.
7624
7625 if Nam_In (Pragma_Name (N), Name_Comment, Name_Ident) then
7626 New_N := Make_Null_Statement (Sloc (N));
7627
7628 -- Do not copy pragmas generated from aspects because the pragmas do
7629 -- not carry any semantic information, plus they will be regenerated
7630 -- in the instance.
7631
7632 elsif From_Aspect_Specification (N) then
7633 New_N := Make_Null_Statement (Sloc (N));
7634
7635 else
7636 Copy_Descendants;
7637 end if;
7638
7639 elsif Nkind_In (N, N_Integer_Literal, N_Real_Literal) then
7640
7641 -- No descendant fields need traversing
7642
7643 null;
7644
7645 elsif Nkind (N) = N_String_Literal
7646 and then Present (Etype (N))
7647 and then Instantiating
7648 then
7649 -- If the string is declared in an outer scope, the string_literal
7650 -- subtype created for it may have the wrong scope. Force reanalysis
7651 -- of the constant to generate a new itype in the proper context.
7652
7653 Set_Etype (New_N, Empty);
7654 Set_Analyzed (New_N, False);
7655
7656 -- For the remaining nodes, copy their descendants recursively
7657
7658 else
7659 Copy_Descendants;
7660
7661 if Instantiating and then Nkind (N) = N_Subprogram_Body then
7662 Set_Generic_Parent (Specification (New_N), N);
7663
7664 -- Should preserve Corresponding_Spec??? (12.3(14))
7665 end if;
7666 end if;
7667
7668 return New_N;
7669 end Copy_Generic_Node;
7670
7671 ----------------------------
7672 -- Denotes_Formal_Package --
7673 ----------------------------
7674
7675 function Denotes_Formal_Package
7676 (Pack : Entity_Id;
7677 On_Exit : Boolean := False;
7678 Instance : Entity_Id := Empty) return Boolean
7679 is
7680 Par : Entity_Id;
7681 Scop : constant Entity_Id := Scope (Pack);
7682 E : Entity_Id;
7683
7684 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean;
7685 -- The package in question may be an actual for a previous formal
7686 -- package P of the current instance, so examine its actuals as well.
7687 -- This must be recursive over other formal packages.
7688
7689 ----------------------------------
7690 -- Is_Actual_Of_Previous_Formal --
7691 ----------------------------------
7692
7693 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean is
7694 E1 : Entity_Id;
7695
7696 begin
7697 E1 := First_Entity (P);
7698 while Present (E1) and then E1 /= Instance loop
7699 if Ekind (E1) = E_Package
7700 and then Nkind (Parent (E1)) = N_Package_Renaming_Declaration
7701 then
7702 if Renamed_Object (E1) = Pack then
7703 return True;
7704
7705 elsif E1 = P or else Renamed_Object (E1) = P then
7706 return False;
7707
7708 elsif Is_Actual_Of_Previous_Formal (E1) then
7709 return True;
7710 end if;
7711 end if;
7712
7713 Next_Entity (E1);
7714 end loop;
7715
7716 return False;
7717 end Is_Actual_Of_Previous_Formal;
7718
7719 -- Start of processing for Denotes_Formal_Package
7720
7721 begin
7722 if On_Exit then
7723 Par :=
7724 Instance_Envs.Table
7725 (Instance_Envs.Last).Instantiated_Parent.Act_Id;
7726 else
7727 Par := Current_Instantiated_Parent.Act_Id;
7728 end if;
7729
7730 if Ekind (Scop) = E_Generic_Package
7731 or else Nkind (Unit_Declaration_Node (Scop)) =
7732 N_Generic_Subprogram_Declaration
7733 then
7734 return True;
7735
7736 elsif Nkind (Original_Node (Unit_Declaration_Node (Pack))) =
7737 N_Formal_Package_Declaration
7738 then
7739 return True;
7740
7741 elsif No (Par) then
7742 return False;
7743
7744 else
7745 -- Check whether this package is associated with a formal package of
7746 -- the enclosing instantiation. Iterate over the list of renamings.
7747
7748 E := First_Entity (Par);
7749 while Present (E) loop
7750 if Ekind (E) /= E_Package
7751 or else Nkind (Parent (E)) /= N_Package_Renaming_Declaration
7752 then
7753 null;
7754
7755 elsif Renamed_Object (E) = Par then
7756 return False;
7757
7758 elsif Renamed_Object (E) = Pack then
7759 return True;
7760
7761 elsif Is_Actual_Of_Previous_Formal (E) then
7762 return True;
7763
7764 end if;
7765
7766 Next_Entity (E);
7767 end loop;
7768
7769 return False;
7770 end if;
7771 end Denotes_Formal_Package;
7772
7773 -----------------
7774 -- End_Generic --
7775 -----------------
7776
7777 procedure End_Generic is
7778 begin
7779 -- ??? More things could be factored out in this routine. Should
7780 -- probably be done at a later stage.
7781
7782 Inside_A_Generic := Generic_Flags.Table (Generic_Flags.Last);
7783 Generic_Flags.Decrement_Last;
7784
7785 Expander_Mode_Restore;
7786 end End_Generic;
7787
7788 -------------
7789 -- Earlier --
7790 -------------
7791
7792 function Earlier (N1, N2 : Node_Id) return Boolean is
7793 procedure Find_Depth (P : in out Node_Id; D : in out Integer);
7794 -- Find distance from given node to enclosing compilation unit
7795
7796 ----------------
7797 -- Find_Depth --
7798 ----------------
7799
7800 procedure Find_Depth (P : in out Node_Id; D : in out Integer) is
7801 begin
7802 while Present (P)
7803 and then Nkind (P) /= N_Compilation_Unit
7804 loop
7805 P := True_Parent (P);
7806 D := D + 1;
7807 end loop;
7808 end Find_Depth;
7809
7810 -- Local declarations
7811
7812 D1 : Integer := 0;
7813 D2 : Integer := 0;
7814 P1 : Node_Id := N1;
7815 P2 : Node_Id := N2;
7816 T1 : Source_Ptr;
7817 T2 : Source_Ptr;
7818
7819 -- Start of processing for Earlier
7820
7821 begin
7822 Find_Depth (P1, D1);
7823 Find_Depth (P2, D2);
7824
7825 if P1 /= P2 then
7826 return False;
7827 else
7828 P1 := N1;
7829 P2 := N2;
7830 end if;
7831
7832 while D1 > D2 loop
7833 P1 := True_Parent (P1);
7834 D1 := D1 - 1;
7835 end loop;
7836
7837 while D2 > D1 loop
7838 P2 := True_Parent (P2);
7839 D2 := D2 - 1;
7840 end loop;
7841
7842 -- At this point P1 and P2 are at the same distance from the root.
7843 -- We examine their parents until we find a common declarative list.
7844 -- If we reach the root, N1 and N2 do not descend from the same
7845 -- declarative list (e.g. one is nested in the declarative part and
7846 -- the other is in a block in the statement part) and the earlier
7847 -- one is already frozen.
7848
7849 while not Is_List_Member (P1)
7850 or else not Is_List_Member (P2)
7851 or else List_Containing (P1) /= List_Containing (P2)
7852 loop
7853 P1 := True_Parent (P1);
7854 P2 := True_Parent (P2);
7855
7856 if Nkind (Parent (P1)) = N_Subunit then
7857 P1 := Corresponding_Stub (Parent (P1));
7858 end if;
7859
7860 if Nkind (Parent (P2)) = N_Subunit then
7861 P2 := Corresponding_Stub (Parent (P2));
7862 end if;
7863
7864 if P1 = P2 then
7865 return False;
7866 end if;
7867 end loop;
7868
7869 -- Expanded code usually shares the source location of the original
7870 -- construct it was generated for. This however may not necessarely
7871 -- reflect the true location of the code within the tree.
7872
7873 -- Before comparing the slocs of the two nodes, make sure that we are
7874 -- working with correct source locations. Assume that P1 is to the left
7875 -- of P2. If either one does not come from source, traverse the common
7876 -- list heading towards the other node and locate the first source
7877 -- statement.
7878
7879 -- P1 P2
7880 -- ----+===+===+--------------+===+===+----
7881 -- expanded code expanded code
7882
7883 if not Comes_From_Source (P1) then
7884 while Present (P1) loop
7885
7886 -- Neither P2 nor a source statement were located during the
7887 -- search. If we reach the end of the list, then P1 does not
7888 -- occur earlier than P2.
7889
7890 -- ---->
7891 -- start --- P2 ----- P1 --- end
7892
7893 if No (Next (P1)) then
7894 return False;
7895
7896 -- We encounter P2 while going to the right of the list. This
7897 -- means that P1 does indeed appear earlier.
7898
7899 -- ---->
7900 -- start --- P1 ===== P2 --- end
7901 -- expanded code in between
7902
7903 elsif P1 = P2 then
7904 return True;
7905
7906 -- No need to look any further since we have located a source
7907 -- statement.
7908
7909 elsif Comes_From_Source (P1) then
7910 exit;
7911 end if;
7912
7913 -- Keep going right
7914
7915 Next (P1);
7916 end loop;
7917 end if;
7918
7919 if not Comes_From_Source (P2) then
7920 while Present (P2) loop
7921
7922 -- Neither P1 nor a source statement were located during the
7923 -- search. If we reach the start of the list, then P1 does not
7924 -- occur earlier than P2.
7925
7926 -- <----
7927 -- start --- P2 --- P1 --- end
7928
7929 if No (Prev (P2)) then
7930 return False;
7931
7932 -- We encounter P1 while going to the left of the list. This
7933 -- means that P1 does indeed appear earlier.
7934
7935 -- <----
7936 -- start --- P1 ===== P2 --- end
7937 -- expanded code in between
7938
7939 elsif P2 = P1 then
7940 return True;
7941
7942 -- No need to look any further since we have located a source
7943 -- statement.
7944
7945 elsif Comes_From_Source (P2) then
7946 exit;
7947 end if;
7948
7949 -- Keep going left
7950
7951 Prev (P2);
7952 end loop;
7953 end if;
7954
7955 -- At this point either both nodes came from source or we approximated
7956 -- their source locations through neighbouring source statements.
7957
7958 T1 := Top_Level_Location (Sloc (P1));
7959 T2 := Top_Level_Location (Sloc (P2));
7960
7961 -- When two nodes come from the same instance, they have identical top
7962 -- level locations. To determine proper relation within the tree, check
7963 -- their locations within the template.
7964
7965 if T1 = T2 then
7966 return Sloc (P1) < Sloc (P2);
7967
7968 -- The two nodes either come from unrelated instances or do not come
7969 -- from instantiated code at all.
7970
7971 else
7972 return T1 < T2;
7973 end if;
7974 end Earlier;
7975
7976 ----------------------
7977 -- Find_Actual_Type --
7978 ----------------------
7979
7980 function Find_Actual_Type
7981 (Typ : Entity_Id;
7982 Gen_Type : Entity_Id) return Entity_Id
7983 is
7984 Gen_Scope : constant Entity_Id := Scope (Gen_Type);
7985 T : Entity_Id;
7986
7987 begin
7988 -- Special processing only applies to child units
7989
7990 if not Is_Child_Unit (Gen_Scope) then
7991 return Get_Instance_Of (Typ);
7992
7993 -- If designated or component type is itself a formal of the child unit,
7994 -- its instance is available.
7995
7996 elsif Scope (Typ) = Gen_Scope then
7997 return Get_Instance_Of (Typ);
7998
7999 -- If the array or access type is not declared in the parent unit,
8000 -- no special processing needed.
8001
8002 elsif not Is_Generic_Type (Typ)
8003 and then Scope (Gen_Scope) /= Scope (Typ)
8004 then
8005 return Get_Instance_Of (Typ);
8006
8007 -- Otherwise, retrieve designated or component type by visibility
8008
8009 else
8010 T := Current_Entity (Typ);
8011 while Present (T) loop
8012 if In_Open_Scopes (Scope (T)) then
8013 return T;
8014 elsif Is_Generic_Actual_Type (T) then
8015 return T;
8016 end if;
8017
8018 T := Homonym (T);
8019 end loop;
8020
8021 return Typ;
8022 end if;
8023 end Find_Actual_Type;
8024
8025 ----------------------------
8026 -- Freeze_Subprogram_Body --
8027 ----------------------------
8028
8029 procedure Freeze_Subprogram_Body
8030 (Inst_Node : Node_Id;
8031 Gen_Body : Node_Id;
8032 Pack_Id : Entity_Id)
8033 is
8034 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
8035 Par : constant Entity_Id := Scope (Gen_Unit);
8036 E_G_Id : Entity_Id;
8037 Enc_G : Entity_Id;
8038 Enc_I : Node_Id;
8039 F_Node : Node_Id;
8040
8041 function Enclosing_Package_Body (N : Node_Id) return Node_Id;
8042 -- Find innermost package body that encloses the given node, and which
8043 -- is not a compilation unit. Freeze nodes for the instance, or for its
8044 -- enclosing body, may be inserted after the enclosing_body of the
8045 -- generic unit. Used to determine proper placement of freeze node for
8046 -- both package and subprogram instances.
8047
8048 function Package_Freeze_Node (B : Node_Id) return Node_Id;
8049 -- Find entity for given package body, and locate or create a freeze
8050 -- node for it.
8051
8052 ----------------------------
8053 -- Enclosing_Package_Body --
8054 ----------------------------
8055
8056 function Enclosing_Package_Body (N : Node_Id) return Node_Id is
8057 P : Node_Id;
8058
8059 begin
8060 P := Parent (N);
8061 while Present (P)
8062 and then Nkind (Parent (P)) /= N_Compilation_Unit
8063 loop
8064 if Nkind (P) = N_Package_Body then
8065 if Nkind (Parent (P)) = N_Subunit then
8066 return Corresponding_Stub (Parent (P));
8067 else
8068 return P;
8069 end if;
8070 end if;
8071
8072 P := True_Parent (P);
8073 end loop;
8074
8075 return Empty;
8076 end Enclosing_Package_Body;
8077
8078 -------------------------
8079 -- Package_Freeze_Node --
8080 -------------------------
8081
8082 function Package_Freeze_Node (B : Node_Id) return Node_Id is
8083 Id : Entity_Id;
8084
8085 begin
8086 if Nkind (B) = N_Package_Body then
8087 Id := Corresponding_Spec (B);
8088 else pragma Assert (Nkind (B) = N_Package_Body_Stub);
8089 Id := Corresponding_Spec (Proper_Body (Unit (Library_Unit (B))));
8090 end if;
8091
8092 Ensure_Freeze_Node (Id);
8093 return Freeze_Node (Id);
8094 end Package_Freeze_Node;
8095
8096 -- Start of processing for Freeze_Subprogram_Body
8097
8098 begin
8099 -- If the instance and the generic body appear within the same unit, and
8100 -- the instance precedes the generic, the freeze node for the instance
8101 -- must appear after that of the generic. If the generic is nested
8102 -- within another instance I2, then current instance must be frozen
8103 -- after I2. In both cases, the freeze nodes are those of enclosing
8104 -- packages. Otherwise, the freeze node is placed at the end of the
8105 -- current declarative part.
8106
8107 Enc_G := Enclosing_Package_Body (Gen_Body);
8108 Enc_I := Enclosing_Package_Body (Inst_Node);
8109 Ensure_Freeze_Node (Pack_Id);
8110 F_Node := Freeze_Node (Pack_Id);
8111
8112 if Is_Generic_Instance (Par)
8113 and then Present (Freeze_Node (Par))
8114 and then In_Same_Declarative_Part (Freeze_Node (Par), Inst_Node)
8115 then
8116 -- The parent was a premature instantiation. Insert freeze node at
8117 -- the end the current declarative part.
8118
8119 if ABE_Is_Certain (Get_Package_Instantiation_Node (Par)) then
8120 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8121
8122 -- Handle the following case:
8123 --
8124 -- package Parent_Inst is new ...
8125 -- Parent_Inst []
8126 --
8127 -- procedure P ... -- this body freezes Parent_Inst
8128 --
8129 -- package Inst is new ...
8130 --
8131 -- In this particular scenario, the freeze node for Inst must be
8132 -- inserted in the same manner as that of Parent_Inst - before the
8133 -- next source body or at the end of the declarative list (body not
8134 -- available). If body P did not exist and Parent_Inst was frozen
8135 -- after Inst, either by a body following Inst or at the end of the
8136 -- declarative region, the freeze node for Inst must be inserted
8137 -- after that of Parent_Inst. This relation is established by
8138 -- comparing the Slocs of Parent_Inst freeze node and Inst.
8139
8140 elsif List_Containing (Get_Package_Instantiation_Node (Par)) =
8141 List_Containing (Inst_Node)
8142 and then Sloc (Freeze_Node (Par)) < Sloc (Inst_Node)
8143 then
8144 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8145
8146 else
8147 Insert_After (Freeze_Node (Par), F_Node);
8148 end if;
8149
8150 -- The body enclosing the instance should be frozen after the body that
8151 -- includes the generic, because the body of the instance may make
8152 -- references to entities therein. If the two are not in the same
8153 -- declarative part, or if the one enclosing the instance is frozen
8154 -- already, freeze the instance at the end of the current declarative
8155 -- part.
8156
8157 elsif Is_Generic_Instance (Par)
8158 and then Present (Freeze_Node (Par))
8159 and then Present (Enc_I)
8160 then
8161 if In_Same_Declarative_Part (Freeze_Node (Par), Enc_I)
8162 or else
8163 (Nkind (Enc_I) = N_Package_Body
8164 and then
8165 In_Same_Declarative_Part (Freeze_Node (Par), Parent (Enc_I)))
8166 then
8167 -- The enclosing package may contain several instances. Rather
8168 -- than computing the earliest point at which to insert its freeze
8169 -- node, we place it at the end of the declarative part of the
8170 -- parent of the generic.
8171
8172 Insert_Freeze_Node_For_Instance
8173 (Freeze_Node (Par), Package_Freeze_Node (Enc_I));
8174 end if;
8175
8176 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8177
8178 elsif Present (Enc_G)
8179 and then Present (Enc_I)
8180 and then Enc_G /= Enc_I
8181 and then Earlier (Inst_Node, Gen_Body)
8182 then
8183 if Nkind (Enc_G) = N_Package_Body then
8184 E_G_Id :=
8185 Corresponding_Spec (Enc_G);
8186 else pragma Assert (Nkind (Enc_G) = N_Package_Body_Stub);
8187 E_G_Id :=
8188 Corresponding_Spec (Proper_Body (Unit (Library_Unit (Enc_G))));
8189 end if;
8190
8191 -- Freeze package that encloses instance, and place node after the
8192 -- package that encloses generic. If enclosing package is already
8193 -- frozen we have to assume it is at the proper place. This may be a
8194 -- potential ABE that requires dynamic checking. Do not add a freeze
8195 -- node if the package that encloses the generic is inside the body
8196 -- that encloses the instance, because the freeze node would be in
8197 -- the wrong scope. Additional contortions needed if the bodies are
8198 -- within a subunit.
8199
8200 declare
8201 Enclosing_Body : Node_Id;
8202
8203 begin
8204 if Nkind (Enc_I) = N_Package_Body_Stub then
8205 Enclosing_Body := Proper_Body (Unit (Library_Unit (Enc_I)));
8206 else
8207 Enclosing_Body := Enc_I;
8208 end if;
8209
8210 if Parent (List_Containing (Enc_G)) /= Enclosing_Body then
8211 Insert_Freeze_Node_For_Instance
8212 (Enc_G, Package_Freeze_Node (Enc_I));
8213 end if;
8214 end;
8215
8216 -- Freeze enclosing subunit before instance
8217
8218 Ensure_Freeze_Node (E_G_Id);
8219
8220 if not Is_List_Member (Freeze_Node (E_G_Id)) then
8221 Insert_After (Enc_G, Freeze_Node (E_G_Id));
8222 end if;
8223
8224 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8225
8226 else
8227 -- If none of the above, insert freeze node at the end of the current
8228 -- declarative part.
8229
8230 Insert_Freeze_Node_For_Instance (Inst_Node, F_Node);
8231 end if;
8232 end Freeze_Subprogram_Body;
8233
8234 ----------------
8235 -- Get_Gen_Id --
8236 ----------------
8237
8238 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id is
8239 begin
8240 return Generic_Renamings.Table (E).Gen_Id;
8241 end Get_Gen_Id;
8242
8243 ---------------------
8244 -- Get_Instance_Of --
8245 ---------------------
8246
8247 function Get_Instance_Of (A : Entity_Id) return Entity_Id is
8248 Res : constant Assoc_Ptr := Generic_Renamings_HTable.Get (A);
8249
8250 begin
8251 if Res /= Assoc_Null then
8252 return Generic_Renamings.Table (Res).Act_Id;
8253
8254 else
8255 -- On exit, entity is not instantiated: not a generic parameter, or
8256 -- else parameter of an inner generic unit.
8257
8258 return A;
8259 end if;
8260 end Get_Instance_Of;
8261
8262 ------------------------------------
8263 -- Get_Package_Instantiation_Node --
8264 ------------------------------------
8265
8266 function Get_Package_Instantiation_Node (A : Entity_Id) return Node_Id is
8267 Decl : Node_Id := Unit_Declaration_Node (A);
8268 Inst : Node_Id;
8269
8270 begin
8271 -- If the Package_Instantiation attribute has been set on the package
8272 -- entity, then use it directly when it (or its Original_Node) refers
8273 -- to an N_Package_Instantiation node. In principle it should be
8274 -- possible to have this field set in all cases, which should be
8275 -- investigated, and would allow this function to be significantly
8276 -- simplified. ???
8277
8278 Inst := Package_Instantiation (A);
8279
8280 if Present (Inst) then
8281 if Nkind (Inst) = N_Package_Instantiation then
8282 return Inst;
8283
8284 elsif Nkind (Original_Node (Inst)) = N_Package_Instantiation then
8285 return Original_Node (Inst);
8286 end if;
8287 end if;
8288
8289 -- If the instantiation is a compilation unit that does not need body
8290 -- then the instantiation node has been rewritten as a package
8291 -- declaration for the instance, and we return the original node.
8292
8293 -- If it is a compilation unit and the instance node has not been
8294 -- rewritten, then it is still the unit of the compilation. Finally, if
8295 -- a body is present, this is a parent of the main unit whose body has
8296 -- been compiled for inlining purposes, and the instantiation node has
8297 -- been rewritten with the instance body.
8298
8299 -- Otherwise the instantiation node appears after the declaration. If
8300 -- the entity is a formal package, the declaration may have been
8301 -- rewritten as a generic declaration (in the case of a formal with box)
8302 -- or left as a formal package declaration if it has actuals, and is
8303 -- found with a forward search.
8304
8305 if Nkind (Parent (Decl)) = N_Compilation_Unit then
8306 if Nkind (Decl) = N_Package_Declaration
8307 and then Present (Corresponding_Body (Decl))
8308 then
8309 Decl := Unit_Declaration_Node (Corresponding_Body (Decl));
8310 end if;
8311
8312 if Nkind (Original_Node (Decl)) = N_Package_Instantiation then
8313 return Original_Node (Decl);
8314 else
8315 return Unit (Parent (Decl));
8316 end if;
8317
8318 elsif Nkind (Decl) = N_Package_Declaration
8319 and then Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration
8320 then
8321 return Original_Node (Decl);
8322
8323 else
8324 Inst := Next (Decl);
8325 while not Nkind_In (Inst, N_Package_Instantiation,
8326 N_Formal_Package_Declaration)
8327 loop
8328 Next (Inst);
8329 end loop;
8330
8331 return Inst;
8332 end if;
8333 end Get_Package_Instantiation_Node;
8334
8335 ------------------------
8336 -- Has_Been_Exchanged --
8337 ------------------------
8338
8339 function Has_Been_Exchanged (E : Entity_Id) return Boolean is
8340 Next : Elmt_Id;
8341
8342 begin
8343 Next := First_Elmt (Exchanged_Views);
8344 while Present (Next) loop
8345 if Full_View (Node (Next)) = E then
8346 return True;
8347 end if;
8348
8349 Next_Elmt (Next);
8350 end loop;
8351
8352 return False;
8353 end Has_Been_Exchanged;
8354
8355 ----------
8356 -- Hash --
8357 ----------
8358
8359 function Hash (F : Entity_Id) return HTable_Range is
8360 begin
8361 return HTable_Range (F mod HTable_Size);
8362 end Hash;
8363
8364 ------------------------
8365 -- Hide_Current_Scope --
8366 ------------------------
8367
8368 procedure Hide_Current_Scope is
8369 C : constant Entity_Id := Current_Scope;
8370 E : Entity_Id;
8371
8372 begin
8373 Set_Is_Hidden_Open_Scope (C);
8374
8375 E := First_Entity (C);
8376 while Present (E) loop
8377 if Is_Immediately_Visible (E) then
8378 Set_Is_Immediately_Visible (E, False);
8379 Append_Elmt (E, Hidden_Entities);
8380 end if;
8381
8382 Next_Entity (E);
8383 end loop;
8384
8385 -- Make the scope name invisible as well. This is necessary, but might
8386 -- conflict with calls to Rtsfind later on, in case the scope is a
8387 -- predefined one. There is no clean solution to this problem, so for
8388 -- now we depend on the user not redefining Standard itself in one of
8389 -- the parent units.
8390
8391 if Is_Immediately_Visible (C) and then C /= Standard_Standard then
8392 Set_Is_Immediately_Visible (C, False);
8393 Append_Elmt (C, Hidden_Entities);
8394 end if;
8395
8396 end Hide_Current_Scope;
8397
8398 --------------
8399 -- Init_Env --
8400 --------------
8401
8402 procedure Init_Env is
8403 Saved : Instance_Env;
8404
8405 begin
8406 Saved.Instantiated_Parent := Current_Instantiated_Parent;
8407 Saved.Exchanged_Views := Exchanged_Views;
8408 Saved.Hidden_Entities := Hidden_Entities;
8409 Saved.Current_Sem_Unit := Current_Sem_Unit;
8410 Saved.Parent_Unit_Visible := Parent_Unit_Visible;
8411 Saved.Instance_Parent_Unit := Instance_Parent_Unit;
8412
8413 -- Save configuration switches. These may be reset if the unit is a
8414 -- predefined unit, and the current mode is not Ada 2005.
8415
8416 Save_Opt_Config_Switches (Saved.Switches);
8417
8418 Instance_Envs.Append (Saved);
8419
8420 Exchanged_Views := New_Elmt_List;
8421 Hidden_Entities := New_Elmt_List;
8422
8423 -- Make dummy entry for Instantiated parent. If generic unit is legal,
8424 -- this is set properly in Set_Instance_Env.
8425
8426 Current_Instantiated_Parent :=
8427 (Current_Scope, Current_Scope, Assoc_Null);
8428 end Init_Env;
8429
8430 ------------------------------
8431 -- In_Same_Declarative_Part --
8432 ------------------------------
8433
8434 function In_Same_Declarative_Part
8435 (F_Node : Node_Id;
8436 Inst : Node_Id) return Boolean
8437 is
8438 Decls : constant Node_Id := Parent (F_Node);
8439 Nod : Node_Id;
8440
8441 begin
8442 Nod := Parent (Inst);
8443 while Present (Nod) loop
8444 if Nod = Decls then
8445 return True;
8446
8447 elsif Nkind_In (Nod, N_Subprogram_Body,
8448 N_Package_Body,
8449 N_Package_Declaration,
8450 N_Task_Body,
8451 N_Protected_Body,
8452 N_Block_Statement)
8453 then
8454 return False;
8455
8456 elsif Nkind (Nod) = N_Subunit then
8457 Nod := Corresponding_Stub (Nod);
8458
8459 elsif Nkind (Nod) = N_Compilation_Unit then
8460 return False;
8461
8462 else
8463 Nod := Parent (Nod);
8464 end if;
8465 end loop;
8466
8467 return False;
8468 end In_Same_Declarative_Part;
8469
8470 ---------------------
8471 -- In_Main_Context --
8472 ---------------------
8473
8474 function In_Main_Context (E : Entity_Id) return Boolean is
8475 Context : List_Id;
8476 Clause : Node_Id;
8477 Nam : Node_Id;
8478
8479 begin
8480 if not Is_Compilation_Unit (E)
8481 or else Ekind (E) /= E_Package
8482 or else In_Private_Part (E)
8483 then
8484 return False;
8485 end if;
8486
8487 Context := Context_Items (Cunit (Main_Unit));
8488
8489 Clause := First (Context);
8490 while Present (Clause) loop
8491 if Nkind (Clause) = N_With_Clause then
8492 Nam := Name (Clause);
8493
8494 -- If the current scope is part of the context of the main unit,
8495 -- analysis of the corresponding with_clause is not complete, and
8496 -- the entity is not set. We use the Chars field directly, which
8497 -- might produce false positives in rare cases, but guarantees
8498 -- that we produce all the instance bodies we will need.
8499
8500 if (Is_Entity_Name (Nam) and then Chars (Nam) = Chars (E))
8501 or else (Nkind (Nam) = N_Selected_Component
8502 and then Chars (Selector_Name (Nam)) = Chars (E))
8503 then
8504 return True;
8505 end if;
8506 end if;
8507
8508 Next (Clause);
8509 end loop;
8510
8511 return False;
8512 end In_Main_Context;
8513
8514 ---------------------
8515 -- Inherit_Context --
8516 ---------------------
8517
8518 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id) is
8519 Current_Context : List_Id;
8520 Current_Unit : Node_Id;
8521 Item : Node_Id;
8522 New_I : Node_Id;
8523
8524 Clause : Node_Id;
8525 OK : Boolean;
8526 Lib_Unit : Node_Id;
8527
8528 begin
8529 if Nkind (Parent (Gen_Decl)) = N_Compilation_Unit then
8530
8531 -- The inherited context is attached to the enclosing compilation
8532 -- unit. This is either the main unit, or the declaration for the
8533 -- main unit (in case the instantiation appears within the package
8534 -- declaration and the main unit is its body).
8535
8536 Current_Unit := Parent (Inst);
8537 while Present (Current_Unit)
8538 and then Nkind (Current_Unit) /= N_Compilation_Unit
8539 loop
8540 Current_Unit := Parent (Current_Unit);
8541 end loop;
8542
8543 Current_Context := Context_Items (Current_Unit);
8544
8545 Item := First (Context_Items (Parent (Gen_Decl)));
8546 while Present (Item) loop
8547 if Nkind (Item) = N_With_Clause then
8548 Lib_Unit := Library_Unit (Item);
8549
8550 -- Take care to prevent direct cyclic with's
8551
8552 if Lib_Unit /= Current_Unit then
8553
8554 -- Do not add a unit if it is already in the context
8555
8556 Clause := First (Current_Context);
8557 OK := True;
8558 while Present (Clause) loop
8559 if Nkind (Clause) = N_With_Clause and then
8560 Library_Unit (Clause) = Lib_Unit
8561 then
8562 OK := False;
8563 exit;
8564 end if;
8565
8566 Next (Clause);
8567 end loop;
8568
8569 if OK then
8570 New_I := New_Copy (Item);
8571 Set_Implicit_With (New_I, True);
8572 Set_Implicit_With_From_Instantiation (New_I, True);
8573 Append (New_I, Current_Context);
8574 end if;
8575 end if;
8576 end if;
8577
8578 Next (Item);
8579 end loop;
8580 end if;
8581 end Inherit_Context;
8582
8583 ----------------
8584 -- Initialize --
8585 ----------------
8586
8587 procedure Initialize is
8588 begin
8589 Generic_Renamings.Init;
8590 Instance_Envs.Init;
8591 Generic_Flags.Init;
8592 Generic_Renamings_HTable.Reset;
8593 Circularity_Detected := False;
8594 Exchanged_Views := No_Elist;
8595 Hidden_Entities := No_Elist;
8596 end Initialize;
8597
8598 -------------------------------------
8599 -- Insert_Freeze_Node_For_Instance --
8600 -------------------------------------
8601
8602 procedure Insert_Freeze_Node_For_Instance
8603 (N : Node_Id;
8604 F_Node : Node_Id)
8605 is
8606 Decl : Node_Id;
8607 Decls : List_Id;
8608 Inst : Entity_Id;
8609 Par_N : Node_Id;
8610
8611 function Enclosing_Body (N : Node_Id) return Node_Id;
8612 -- Find enclosing package or subprogram body, if any. Freeze node may
8613 -- be placed at end of current declarative list if previous instance
8614 -- and current one have different enclosing bodies.
8615
8616 function Previous_Instance (Gen : Entity_Id) return Entity_Id;
8617 -- Find the local instance, if any, that declares the generic that is
8618 -- being instantiated. If present, the freeze node for this instance
8619 -- must follow the freeze node for the previous instance.
8620
8621 --------------------
8622 -- Enclosing_Body --
8623 --------------------
8624
8625 function Enclosing_Body (N : Node_Id) return Node_Id is
8626 P : Node_Id;
8627
8628 begin
8629 P := Parent (N);
8630 while Present (P)
8631 and then Nkind (Parent (P)) /= N_Compilation_Unit
8632 loop
8633 if Nkind_In (P, N_Package_Body, N_Subprogram_Body) then
8634 if Nkind (Parent (P)) = N_Subunit then
8635 return Corresponding_Stub (Parent (P));
8636 else
8637 return P;
8638 end if;
8639 end if;
8640
8641 P := True_Parent (P);
8642 end loop;
8643
8644 return Empty;
8645 end Enclosing_Body;
8646
8647 -----------------------
8648 -- Previous_Instance --
8649 -----------------------
8650
8651 function Previous_Instance (Gen : Entity_Id) return Entity_Id is
8652 S : Entity_Id;
8653
8654 begin
8655 S := Scope (Gen);
8656 while Present (S) and then S /= Standard_Standard loop
8657 if Is_Generic_Instance (S)
8658 and then In_Same_Source_Unit (S, N)
8659 then
8660 return S;
8661 end if;
8662
8663 S := Scope (S);
8664 end loop;
8665
8666 return Empty;
8667 end Previous_Instance;
8668
8669 -- Start of processing for Insert_Freeze_Node_For_Instance
8670
8671 begin
8672 if not Is_List_Member (F_Node) then
8673 Decl := N;
8674 Decls := List_Containing (N);
8675 Inst := Entity (F_Node);
8676 Par_N := Parent (Decls);
8677
8678 -- When processing a subprogram instantiation, utilize the actual
8679 -- subprogram instantiation rather than its package wrapper as it
8680 -- carries all the context information.
8681
8682 if Is_Wrapper_Package (Inst) then
8683 Inst := Related_Instance (Inst);
8684 end if;
8685
8686 -- If this is a package instance, check whether the generic is
8687 -- declared in a previous instance and the current instance is
8688 -- not within the previous one.
8689
8690 if Present (Generic_Parent (Parent (Inst)))
8691 and then Is_In_Main_Unit (N)
8692 then
8693 declare
8694 Enclosing_N : constant Node_Id := Enclosing_Body (N);
8695 Par_I : constant Entity_Id :=
8696 Previous_Instance
8697 (Generic_Parent (Parent (Inst)));
8698 Scop : Entity_Id;
8699
8700 begin
8701 if Present (Par_I)
8702 and then Earlier (N, Freeze_Node (Par_I))
8703 then
8704 Scop := Scope (Inst);
8705
8706 -- If the current instance is within the one that contains
8707 -- the generic, the freeze node for the current one must
8708 -- appear in the current declarative part. Ditto, if the
8709 -- current instance is within another package instance or
8710 -- within a body that does not enclose the current instance.
8711 -- In these three cases the freeze node of the previous
8712 -- instance is not relevant.
8713
8714 while Present (Scop) and then Scop /= Standard_Standard loop
8715 exit when Scop = Par_I
8716 or else
8717 (Is_Generic_Instance (Scop)
8718 and then Scope_Depth (Scop) > Scope_Depth (Par_I));
8719 Scop := Scope (Scop);
8720 end loop;
8721
8722 -- Previous instance encloses current instance
8723
8724 if Scop = Par_I then
8725 null;
8726
8727 -- If the next node is a source body we must freeze in
8728 -- the current scope as well.
8729
8730 elsif Present (Next (N))
8731 and then Nkind_In (Next (N), N_Subprogram_Body,
8732 N_Package_Body)
8733 and then Comes_From_Source (Next (N))
8734 then
8735 null;
8736
8737 -- Current instance is within an unrelated instance
8738
8739 elsif Is_Generic_Instance (Scop) then
8740 null;
8741
8742 -- Current instance is within an unrelated body
8743
8744 elsif Present (Enclosing_N)
8745 and then Enclosing_N /= Enclosing_Body (Par_I)
8746 then
8747 null;
8748
8749 else
8750 Insert_After (Freeze_Node (Par_I), F_Node);
8751 return;
8752 end if;
8753 end if;
8754 end;
8755 end if;
8756
8757 -- When the instantiation occurs in a package declaration, append the
8758 -- freeze node to the private declarations (if any).
8759
8760 if Nkind (Par_N) = N_Package_Specification
8761 and then Decls = Visible_Declarations (Par_N)
8762 and then Present (Private_Declarations (Par_N))
8763 and then not Is_Empty_List (Private_Declarations (Par_N))
8764 then
8765 Decls := Private_Declarations (Par_N);
8766 Decl := First (Decls);
8767 end if;
8768
8769 -- Determine the proper freeze point of a package instantiation. We
8770 -- adhere to the general rule of a package or subprogram body causing
8771 -- freezing of anything before it in the same declarative region. In
8772 -- this case, the proper freeze point of a package instantiation is
8773 -- before the first source body which follows, or before a stub. This
8774 -- ensures that entities coming from the instance are already frozen
8775 -- and usable in source bodies.
8776
8777 if Nkind (Par_N) /= N_Package_Declaration
8778 and then Ekind (Inst) = E_Package
8779 and then Is_Generic_Instance (Inst)
8780 and then
8781 not In_Same_Source_Unit (Generic_Parent (Parent (Inst)), Inst)
8782 then
8783 while Present (Decl) loop
8784 if (Nkind (Decl) in N_Unit_Body
8785 or else
8786 Nkind (Decl) in N_Body_Stub)
8787 and then Comes_From_Source (Decl)
8788 then
8789 Insert_Before (Decl, F_Node);
8790 return;
8791 end if;
8792
8793 Next (Decl);
8794 end loop;
8795 end if;
8796
8797 -- In a package declaration, or if no previous body, insert at end
8798 -- of list.
8799
8800 Set_Sloc (F_Node, Sloc (Last (Decls)));
8801 Insert_After (Last (Decls), F_Node);
8802 end if;
8803 end Insert_Freeze_Node_For_Instance;
8804
8805 ------------------
8806 -- Install_Body --
8807 ------------------
8808
8809 procedure Install_Body
8810 (Act_Body : Node_Id;
8811 N : Node_Id;
8812 Gen_Body : Node_Id;
8813 Gen_Decl : Node_Id)
8814 is
8815 Act_Id : constant Entity_Id := Corresponding_Spec (Act_Body);
8816 Act_Unit : constant Node_Id := Unit (Cunit (Get_Source_Unit (N)));
8817 Gen_Id : constant Entity_Id := Corresponding_Spec (Gen_Body);
8818 Par : constant Entity_Id := Scope (Gen_Id);
8819 Gen_Unit : constant Node_Id :=
8820 Unit (Cunit (Get_Source_Unit (Gen_Decl)));
8821 Orig_Body : Node_Id := Gen_Body;
8822 F_Node : Node_Id;
8823 Body_Unit : Node_Id;
8824
8825 Must_Delay : Boolean;
8826
8827 function In_Same_Enclosing_Subp return Boolean;
8828 -- Check whether instance and generic body are within same subprogram.
8829
8830 function True_Sloc (N : Node_Id) return Source_Ptr;
8831 -- If the instance is nested inside a generic unit, the Sloc of the
8832 -- instance indicates the place of the original definition, not the
8833 -- point of the current enclosing instance. Pending a better usage of
8834 -- Slocs to indicate instantiation places, we determine the place of
8835 -- origin of a node by finding the maximum sloc of any ancestor node.
8836 -- Why is this not equivalent to Top_Level_Location ???
8837
8838 ----------------------------
8839 -- In_Same_Enclosing_Subp --
8840 ----------------------------
8841
8842 function In_Same_Enclosing_Subp return Boolean is
8843 Scop : Entity_Id;
8844 Subp : Entity_Id;
8845
8846 begin
8847 Scop := Scope (Act_Id);
8848 while Scop /= Standard_Standard
8849 and then not Is_Overloadable (Scop)
8850 loop
8851 Scop := Scope (Scop);
8852 end loop;
8853
8854 if Scop = Standard_Standard then
8855 return False;
8856 else
8857 Subp := Scop;
8858 end if;
8859
8860 Scop := Scope (Gen_Id);
8861 while Scop /= Standard_Standard loop
8862 if Scop = Subp then
8863 return True;
8864 else
8865 Scop := Scope (Scop);
8866 end if;
8867 end loop;
8868
8869 return False;
8870 end In_Same_Enclosing_Subp;
8871
8872 ---------------
8873 -- True_Sloc --
8874 ---------------
8875
8876 function True_Sloc (N : Node_Id) return Source_Ptr is
8877 Res : Source_Ptr;
8878 N1 : Node_Id;
8879
8880 begin
8881 Res := Sloc (N);
8882 N1 := N;
8883 while Present (N1) and then N1 /= Act_Unit loop
8884 if Sloc (N1) > Res then
8885 Res := Sloc (N1);
8886 end if;
8887
8888 N1 := Parent (N1);
8889 end loop;
8890
8891 return Res;
8892 end True_Sloc;
8893
8894 -- Start of processing for Install_Body
8895
8896 begin
8897 -- Handle first the case of an instance with incomplete actual types.
8898 -- The instance body cannot be placed after the declaration because
8899 -- full views have not been seen yet. Any use of the non-limited views
8900 -- in the instance body requires the presence of a regular with_clause
8901 -- in the enclosing unit, and will fail if this with_clause is missing.
8902 -- We place the instance body at the beginning of the enclosing body,
8903 -- which is the unit being compiled. The freeze node for the instance
8904 -- is then placed after the instance body.
8905
8906 if not Is_Empty_Elmt_List (Incomplete_Actuals (Act_Id))
8907 and then Expander_Active
8908 and then Ekind (Scope (Act_Id)) = E_Package
8909 then
8910 declare
8911 Scop : constant Entity_Id := Scope (Act_Id);
8912 Body_Id : constant Node_Id :=
8913 Corresponding_Body (Unit_Declaration_Node (Scop));
8914
8915 begin
8916 Ensure_Freeze_Node (Act_Id);
8917 F_Node := Freeze_Node (Act_Id);
8918 if Present (Body_Id) then
8919 Set_Is_Frozen (Act_Id, False);
8920 Prepend (Act_Body, Declarations (Parent (Body_Id)));
8921 if Is_List_Member (F_Node) then
8922 Remove (F_Node);
8923 end if;
8924
8925 Insert_After (Act_Body, F_Node);
8926 end if;
8927 end;
8928 return;
8929 end if;
8930
8931 -- If the body is a subunit, the freeze point is the corresponding stub
8932 -- in the current compilation, not the subunit itself.
8933
8934 if Nkind (Parent (Gen_Body)) = N_Subunit then
8935 Orig_Body := Corresponding_Stub (Parent (Gen_Body));
8936 else
8937 Orig_Body := Gen_Body;
8938 end if;
8939
8940 Body_Unit := Unit (Cunit (Get_Source_Unit (Orig_Body)));
8941
8942 -- If the instantiation and the generic definition appear in the same
8943 -- package declaration, this is an early instantiation. If they appear
8944 -- in the same declarative part, it is an early instantiation only if
8945 -- the generic body appears textually later, and the generic body is
8946 -- also in the main unit.
8947
8948 -- If instance is nested within a subprogram, and the generic body
8949 -- is not, the instance is delayed because the enclosing body is. If
8950 -- instance and body are within the same scope, or the same subprogram
8951 -- body, indicate explicitly that the instance is delayed.
8952
8953 Must_Delay :=
8954 (Gen_Unit = Act_Unit
8955 and then (Nkind_In (Gen_Unit, N_Package_Declaration,
8956 N_Generic_Package_Declaration)
8957 or else (Gen_Unit = Body_Unit
8958 and then True_Sloc (N) < Sloc (Orig_Body)))
8959 and then Is_In_Main_Unit (Gen_Unit)
8960 and then (Scope (Act_Id) = Scope (Gen_Id)
8961 or else In_Same_Enclosing_Subp));
8962
8963 -- If this is an early instantiation, the freeze node is placed after
8964 -- the generic body. Otherwise, if the generic appears in an instance,
8965 -- we cannot freeze the current instance until the outer one is frozen.
8966 -- This is only relevant if the current instance is nested within some
8967 -- inner scope not itself within the outer instance. If this scope is
8968 -- a package body in the same declarative part as the outer instance,
8969 -- then that body needs to be frozen after the outer instance. Finally,
8970 -- if no delay is needed, we place the freeze node at the end of the
8971 -- current declarative part.
8972
8973 if Expander_Active then
8974 Ensure_Freeze_Node (Act_Id);
8975 F_Node := Freeze_Node (Act_Id);
8976
8977 if Must_Delay then
8978 Insert_After (Orig_Body, F_Node);
8979
8980 elsif Is_Generic_Instance (Par)
8981 and then Present (Freeze_Node (Par))
8982 and then Scope (Act_Id) /= Par
8983 then
8984 -- Freeze instance of inner generic after instance of enclosing
8985 -- generic.
8986
8987 if In_Same_Declarative_Part (Freeze_Node (Par), N) then
8988
8989 -- Handle the following case:
8990
8991 -- package Parent_Inst is new ...
8992 -- Parent_Inst []
8993
8994 -- procedure P ... -- this body freezes Parent_Inst
8995
8996 -- package Inst is new ...
8997
8998 -- In this particular scenario, the freeze node for Inst must
8999 -- be inserted in the same manner as that of Parent_Inst,
9000 -- before the next source body or at the end of the declarative
9001 -- list (body not available). If body P did not exist and
9002 -- Parent_Inst was frozen after Inst, either by a body
9003 -- following Inst or at the end of the declarative region,
9004 -- the freeze node for Inst must be inserted after that of
9005 -- Parent_Inst. This relation is established by comparing
9006 -- the Slocs of Parent_Inst freeze node and Inst.
9007
9008 if List_Containing (Get_Package_Instantiation_Node (Par)) =
9009 List_Containing (N)
9010 and then Sloc (Freeze_Node (Par)) < Sloc (N)
9011 then
9012 Insert_Freeze_Node_For_Instance (N, F_Node);
9013 else
9014 Insert_After (Freeze_Node (Par), F_Node);
9015 end if;
9016
9017 -- Freeze package enclosing instance of inner generic after
9018 -- instance of enclosing generic.
9019
9020 elsif Nkind_In (Parent (N), N_Package_Body, N_Subprogram_Body)
9021 and then In_Same_Declarative_Part (Freeze_Node (Par), Parent (N))
9022 then
9023 declare
9024 Enclosing : Entity_Id;
9025
9026 begin
9027 Enclosing := Corresponding_Spec (Parent (N));
9028
9029 if No (Enclosing) then
9030 Enclosing := Defining_Entity (Parent (N));
9031 end if;
9032
9033 Insert_Freeze_Node_For_Instance (N, F_Node);
9034 Ensure_Freeze_Node (Enclosing);
9035
9036 if not Is_List_Member (Freeze_Node (Enclosing)) then
9037
9038 -- The enclosing context is a subunit, insert the freeze
9039 -- node after the stub.
9040
9041 if Nkind (Parent (Parent (N))) = N_Subunit then
9042 Insert_Freeze_Node_For_Instance
9043 (Corresponding_Stub (Parent (Parent (N))),
9044 Freeze_Node (Enclosing));
9045
9046 -- The enclosing context is a package with a stub body
9047 -- which has already been replaced by the real body.
9048 -- Insert the freeze node after the actual body.
9049
9050 elsif Ekind (Enclosing) = E_Package
9051 and then Present (Body_Entity (Enclosing))
9052 and then Was_Originally_Stub
9053 (Parent (Body_Entity (Enclosing)))
9054 then
9055 Insert_Freeze_Node_For_Instance
9056 (Parent (Body_Entity (Enclosing)),
9057 Freeze_Node (Enclosing));
9058
9059 -- The parent instance has been frozen before the body of
9060 -- the enclosing package, insert the freeze node after
9061 -- the body.
9062
9063 elsif List_Containing (Freeze_Node (Par)) =
9064 List_Containing (Parent (N))
9065 and then Sloc (Freeze_Node (Par)) < Sloc (Parent (N))
9066 then
9067 Insert_Freeze_Node_For_Instance
9068 (Parent (N), Freeze_Node (Enclosing));
9069
9070 else
9071 Insert_After
9072 (Freeze_Node (Par), Freeze_Node (Enclosing));
9073 end if;
9074 end if;
9075 end;
9076
9077 else
9078 Insert_Freeze_Node_For_Instance (N, F_Node);
9079 end if;
9080
9081 else
9082 Insert_Freeze_Node_For_Instance (N, F_Node);
9083 end if;
9084 end if;
9085
9086 Set_Is_Frozen (Act_Id);
9087 Insert_Before (N, Act_Body);
9088 Mark_Rewrite_Insertion (Act_Body);
9089 end Install_Body;
9090
9091 -----------------------------
9092 -- Install_Formal_Packages --
9093 -----------------------------
9094
9095 procedure Install_Formal_Packages (Par : Entity_Id) is
9096 E : Entity_Id;
9097 Gen : Entity_Id;
9098 Gen_E : Entity_Id := Empty;
9099
9100 begin
9101 E := First_Entity (Par);
9102
9103 -- If we are installing an instance parent, locate the formal packages
9104 -- of its generic parent.
9105
9106 if Is_Generic_Instance (Par) then
9107 Gen := Generic_Parent (Package_Specification (Par));
9108 Gen_E := First_Entity (Gen);
9109 end if;
9110
9111 while Present (E) loop
9112 if Ekind (E) = E_Package
9113 and then Nkind (Parent (E)) = N_Package_Renaming_Declaration
9114 then
9115 -- If this is the renaming for the parent instance, done
9116
9117 if Renamed_Object (E) = Par then
9118 exit;
9119
9120 -- The visibility of a formal of an enclosing generic is already
9121 -- correct.
9122
9123 elsif Denotes_Formal_Package (E) then
9124 null;
9125
9126 elsif Present (Associated_Formal_Package (E)) then
9127 Check_Generic_Actuals (Renamed_Object (E), True);
9128 Set_Is_Hidden (E, False);
9129
9130 -- Find formal package in generic unit that corresponds to
9131 -- (instance of) formal package in instance.
9132
9133 while Present (Gen_E) and then Chars (Gen_E) /= Chars (E) loop
9134 Next_Entity (Gen_E);
9135 end loop;
9136
9137 if Present (Gen_E) then
9138 Map_Formal_Package_Entities (Gen_E, E);
9139 end if;
9140 end if;
9141 end if;
9142
9143 Next_Entity (E);
9144
9145 if Present (Gen_E) then
9146 Next_Entity (Gen_E);
9147 end if;
9148 end loop;
9149 end Install_Formal_Packages;
9150
9151 --------------------
9152 -- Install_Parent --
9153 --------------------
9154
9155 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False) is
9156 Ancestors : constant Elist_Id := New_Elmt_List;
9157 S : constant Entity_Id := Current_Scope;
9158 Inst_Par : Entity_Id;
9159 First_Par : Entity_Id;
9160 Inst_Node : Node_Id;
9161 Gen_Par : Entity_Id;
9162 First_Gen : Entity_Id;
9163 Elmt : Elmt_Id;
9164
9165 procedure Install_Noninstance_Specs (Par : Entity_Id);
9166 -- Install the scopes of noninstance parent units ending with Par
9167
9168 procedure Install_Spec (Par : Entity_Id);
9169 -- The child unit is within the declarative part of the parent, so the
9170 -- declarations within the parent are immediately visible.
9171
9172 -------------------------------
9173 -- Install_Noninstance_Specs --
9174 -------------------------------
9175
9176 procedure Install_Noninstance_Specs (Par : Entity_Id) is
9177 begin
9178 if Present (Par)
9179 and then Par /= Standard_Standard
9180 and then not In_Open_Scopes (Par)
9181 then
9182 Install_Noninstance_Specs (Scope (Par));
9183 Install_Spec (Par);
9184 end if;
9185 end Install_Noninstance_Specs;
9186
9187 ------------------
9188 -- Install_Spec --
9189 ------------------
9190
9191 procedure Install_Spec (Par : Entity_Id) is
9192 Spec : constant Node_Id := Package_Specification (Par);
9193
9194 begin
9195 -- If this parent of the child instance is a top-level unit,
9196 -- then record the unit and its visibility for later resetting in
9197 -- Remove_Parent. We exclude units that are generic instances, as we
9198 -- only want to record this information for the ultimate top-level
9199 -- noninstance parent (is that always correct???).
9200
9201 if Scope (Par) = Standard_Standard
9202 and then not Is_Generic_Instance (Par)
9203 then
9204 Parent_Unit_Visible := Is_Immediately_Visible (Par);
9205 Instance_Parent_Unit := Par;
9206 end if;
9207
9208 -- Open the parent scope and make it and its declarations visible.
9209 -- If this point is not within a body, then only the visible
9210 -- declarations should be made visible, and installation of the
9211 -- private declarations is deferred until the appropriate point
9212 -- within analysis of the spec being instantiated (see the handling
9213 -- of parent visibility in Analyze_Package_Specification). This is
9214 -- relaxed in the case where the parent unit is Ada.Tags, to avoid
9215 -- private view problems that occur when compiling instantiations of
9216 -- a generic child of that package (Generic_Dispatching_Constructor).
9217 -- If the instance freezes a tagged type, inlinings of operations
9218 -- from Ada.Tags may need the full view of type Tag. If inlining took
9219 -- proper account of establishing visibility of inlined subprograms'
9220 -- parents then it should be possible to remove this
9221 -- special check. ???
9222
9223 Push_Scope (Par);
9224 Set_Is_Immediately_Visible (Par);
9225 Install_Visible_Declarations (Par);
9226 Set_Use (Visible_Declarations (Spec));
9227
9228 if In_Body or else Is_RTU (Par, Ada_Tags) then
9229 Install_Private_Declarations (Par);
9230 Set_Use (Private_Declarations (Spec));
9231 end if;
9232 end Install_Spec;
9233
9234 -- Start of processing for Install_Parent
9235
9236 begin
9237 -- We need to install the parent instance to compile the instantiation
9238 -- of the child, but the child instance must appear in the current
9239 -- scope. Given that we cannot place the parent above the current scope
9240 -- in the scope stack, we duplicate the current scope and unstack both
9241 -- after the instantiation is complete.
9242
9243 -- If the parent is itself the instantiation of a child unit, we must
9244 -- also stack the instantiation of its parent, and so on. Each such
9245 -- ancestor is the prefix of the name in a prior instantiation.
9246
9247 -- If this is a nested instance, the parent unit itself resolves to
9248 -- a renaming of the parent instance, whose declaration we need.
9249
9250 -- Finally, the parent may be a generic (not an instance) when the
9251 -- child unit appears as a formal package.
9252
9253 Inst_Par := P;
9254
9255 if Present (Renamed_Entity (Inst_Par)) then
9256 Inst_Par := Renamed_Entity (Inst_Par);
9257 end if;
9258
9259 First_Par := Inst_Par;
9260
9261 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
9262
9263 First_Gen := Gen_Par;
9264
9265 while Present (Gen_Par) and then Is_Child_Unit (Gen_Par) loop
9266
9267 -- Load grandparent instance as well
9268
9269 Inst_Node := Get_Package_Instantiation_Node (Inst_Par);
9270
9271 if Nkind (Name (Inst_Node)) = N_Expanded_Name then
9272 Inst_Par := Entity (Prefix (Name (Inst_Node)));
9273
9274 if Present (Renamed_Entity (Inst_Par)) then
9275 Inst_Par := Renamed_Entity (Inst_Par);
9276 end if;
9277
9278 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
9279
9280 if Present (Gen_Par) then
9281 Prepend_Elmt (Inst_Par, Ancestors);
9282
9283 else
9284 -- Parent is not the name of an instantiation
9285
9286 Install_Noninstance_Specs (Inst_Par);
9287 exit;
9288 end if;
9289
9290 else
9291 -- Previous error
9292
9293 exit;
9294 end if;
9295 end loop;
9296
9297 if Present (First_Gen) then
9298 Append_Elmt (First_Par, Ancestors);
9299 else
9300 Install_Noninstance_Specs (First_Par);
9301 end if;
9302
9303 if not Is_Empty_Elmt_List (Ancestors) then
9304 Elmt := First_Elmt (Ancestors);
9305 while Present (Elmt) loop
9306 Install_Spec (Node (Elmt));
9307 Install_Formal_Packages (Node (Elmt));
9308 Next_Elmt (Elmt);
9309 end loop;
9310 end if;
9311
9312 if not In_Body then
9313 Push_Scope (S);
9314 end if;
9315 end Install_Parent;
9316
9317 -------------------------------
9318 -- Install_Hidden_Primitives --
9319 -------------------------------
9320
9321 procedure Install_Hidden_Primitives
9322 (Prims_List : in out Elist_Id;
9323 Gen_T : Entity_Id;
9324 Act_T : Entity_Id)
9325 is
9326 Elmt : Elmt_Id;
9327 List : Elist_Id := No_Elist;
9328 Prim_G_Elmt : Elmt_Id;
9329 Prim_A_Elmt : Elmt_Id;
9330 Prim_G : Node_Id;
9331 Prim_A : Node_Id;
9332
9333 begin
9334 -- No action needed in case of serious errors because we cannot trust
9335 -- in the order of primitives
9336
9337 if Serious_Errors_Detected > 0 then
9338 return;
9339
9340 -- No action possible if we don't have available the list of primitive
9341 -- operations
9342
9343 elsif No (Gen_T)
9344 or else not Is_Record_Type (Gen_T)
9345 or else not Is_Tagged_Type (Gen_T)
9346 or else not Is_Record_Type (Act_T)
9347 or else not Is_Tagged_Type (Act_T)
9348 then
9349 return;
9350
9351 -- There is no need to handle interface types since their primitives
9352 -- cannot be hidden
9353
9354 elsif Is_Interface (Gen_T) then
9355 return;
9356 end if;
9357
9358 Prim_G_Elmt := First_Elmt (Primitive_Operations (Gen_T));
9359
9360 if not Is_Class_Wide_Type (Act_T) then
9361 Prim_A_Elmt := First_Elmt (Primitive_Operations (Act_T));
9362 else
9363 Prim_A_Elmt := First_Elmt (Primitive_Operations (Root_Type (Act_T)));
9364 end if;
9365
9366 loop
9367 -- Skip predefined primitives in the generic formal
9368
9369 while Present (Prim_G_Elmt)
9370 and then Is_Predefined_Dispatching_Operation (Node (Prim_G_Elmt))
9371 loop
9372 Next_Elmt (Prim_G_Elmt);
9373 end loop;
9374
9375 -- Skip predefined primitives in the generic actual
9376
9377 while Present (Prim_A_Elmt)
9378 and then Is_Predefined_Dispatching_Operation (Node (Prim_A_Elmt))
9379 loop
9380 Next_Elmt (Prim_A_Elmt);
9381 end loop;
9382
9383 exit when No (Prim_G_Elmt) or else No (Prim_A_Elmt);
9384
9385 Prim_G := Node (Prim_G_Elmt);
9386 Prim_A := Node (Prim_A_Elmt);
9387
9388 -- There is no need to handle interface primitives because their
9389 -- primitives are not hidden
9390
9391 exit when Present (Interface_Alias (Prim_G));
9392
9393 -- Here we install one hidden primitive
9394
9395 if Chars (Prim_G) /= Chars (Prim_A)
9396 and then Has_Suffix (Prim_A, 'P')
9397 and then Remove_Suffix (Prim_A, 'P') = Chars (Prim_G)
9398 then
9399 Set_Chars (Prim_A, Chars (Prim_G));
9400 Append_New_Elmt (Prim_A, To => List);
9401 end if;
9402
9403 Next_Elmt (Prim_A_Elmt);
9404 Next_Elmt (Prim_G_Elmt);
9405 end loop;
9406
9407 -- Append the elements to the list of temporarily visible primitives
9408 -- avoiding duplicates.
9409
9410 if Present (List) then
9411 if No (Prims_List) then
9412 Prims_List := New_Elmt_List;
9413 end if;
9414
9415 Elmt := First_Elmt (List);
9416 while Present (Elmt) loop
9417 Append_Unique_Elmt (Node (Elmt), Prims_List);
9418 Next_Elmt (Elmt);
9419 end loop;
9420 end if;
9421 end Install_Hidden_Primitives;
9422
9423 -------------------------------
9424 -- Restore_Hidden_Primitives --
9425 -------------------------------
9426
9427 procedure Restore_Hidden_Primitives (Prims_List : in out Elist_Id) is
9428 Prim_Elmt : Elmt_Id;
9429 Prim : Node_Id;
9430
9431 begin
9432 if Prims_List /= No_Elist then
9433 Prim_Elmt := First_Elmt (Prims_List);
9434 while Present (Prim_Elmt) loop
9435 Prim := Node (Prim_Elmt);
9436 Set_Chars (Prim, Add_Suffix (Prim, 'P'));
9437 Next_Elmt (Prim_Elmt);
9438 end loop;
9439
9440 Prims_List := No_Elist;
9441 end if;
9442 end Restore_Hidden_Primitives;
9443
9444 --------------------------------
9445 -- Instantiate_Formal_Package --
9446 --------------------------------
9447
9448 function Instantiate_Formal_Package
9449 (Formal : Node_Id;
9450 Actual : Node_Id;
9451 Analyzed_Formal : Node_Id) return List_Id
9452 is
9453 Loc : constant Source_Ptr := Sloc (Actual);
9454 Actual_Pack : Entity_Id;
9455 Formal_Pack : Entity_Id;
9456 Gen_Parent : Entity_Id;
9457 Decls : List_Id;
9458 Nod : Node_Id;
9459 Parent_Spec : Node_Id;
9460
9461 procedure Find_Matching_Actual
9462 (F : Node_Id;
9463 Act : in out Entity_Id);
9464 -- We need to associate each formal entity in the formal package with
9465 -- the corresponding entity in the actual package. The actual package
9466 -- has been analyzed and possibly expanded, and as a result there is
9467 -- no one-to-one correspondence between the two lists (for example,
9468 -- the actual may include subtypes, itypes, and inherited primitive
9469 -- operations, interspersed among the renaming declarations for the
9470 -- actuals). We retrieve the corresponding actual by name because each
9471 -- actual has the same name as the formal, and they do appear in the
9472 -- same order.
9473
9474 function Get_Formal_Entity (N : Node_Id) return Entity_Id;
9475 -- Retrieve entity of defining entity of generic formal parameter.
9476 -- Only the declarations of formals need to be considered when
9477 -- linking them to actuals, but the declarative list may include
9478 -- internal entities generated during analysis, and those are ignored.
9479
9480 procedure Match_Formal_Entity
9481 (Formal_Node : Node_Id;
9482 Formal_Ent : Entity_Id;
9483 Actual_Ent : Entity_Id);
9484 -- Associates the formal entity with the actual. In the case where
9485 -- Formal_Ent is a formal package, this procedure iterates through all
9486 -- of its formals and enters associations between the actuals occurring
9487 -- in the formal package's corresponding actual package (given by
9488 -- Actual_Ent) and the formal package's formal parameters. This
9489 -- procedure recurses if any of the parameters is itself a package.
9490
9491 function Is_Instance_Of
9492 (Act_Spec : Entity_Id;
9493 Gen_Anc : Entity_Id) return Boolean;
9494 -- The actual can be an instantiation of a generic within another
9495 -- instance, in which case there is no direct link from it to the
9496 -- original generic ancestor. In that case, we recognize that the
9497 -- ultimate ancestor is the same by examining names and scopes.
9498
9499 procedure Process_Nested_Formal (Formal : Entity_Id);
9500 -- If the current formal is declared with a box, its own formals are
9501 -- visible in the instance, as they were in the generic, and their
9502 -- Hidden flag must be reset. If some of these formals are themselves
9503 -- packages declared with a box, the processing must be recursive.
9504
9505 --------------------------
9506 -- Find_Matching_Actual --
9507 --------------------------
9508
9509 procedure Find_Matching_Actual
9510 (F : Node_Id;
9511 Act : in out Entity_Id)
9512 is
9513 Formal_Ent : Entity_Id;
9514
9515 begin
9516 case Nkind (Original_Node (F)) is
9517 when N_Formal_Object_Declaration |
9518 N_Formal_Type_Declaration =>
9519 Formal_Ent := Defining_Identifier (F);
9520
9521 while Chars (Act) /= Chars (Formal_Ent) loop
9522 Next_Entity (Act);
9523 end loop;
9524
9525 when N_Formal_Subprogram_Declaration |
9526 N_Formal_Package_Declaration |
9527 N_Package_Declaration |
9528 N_Generic_Package_Declaration =>
9529 Formal_Ent := Defining_Entity (F);
9530
9531 while Chars (Act) /= Chars (Formal_Ent) loop
9532 Next_Entity (Act);
9533 end loop;
9534
9535 when others =>
9536 raise Program_Error;
9537 end case;
9538 end Find_Matching_Actual;
9539
9540 -------------------------
9541 -- Match_Formal_Entity --
9542 -------------------------
9543
9544 procedure Match_Formal_Entity
9545 (Formal_Node : Node_Id;
9546 Formal_Ent : Entity_Id;
9547 Actual_Ent : Entity_Id)
9548 is
9549 Act_Pkg : Entity_Id;
9550
9551 begin
9552 Set_Instance_Of (Formal_Ent, Actual_Ent);
9553
9554 if Ekind (Actual_Ent) = E_Package then
9555
9556 -- Record associations for each parameter
9557
9558 Act_Pkg := Actual_Ent;
9559
9560 declare
9561 A_Ent : Entity_Id := First_Entity (Act_Pkg);
9562 F_Ent : Entity_Id;
9563 F_Node : Node_Id;
9564
9565 Gen_Decl : Node_Id;
9566 Formals : List_Id;
9567 Actual : Entity_Id;
9568
9569 begin
9570 -- Retrieve the actual given in the formal package declaration
9571
9572 Actual := Entity (Name (Original_Node (Formal_Node)));
9573
9574 -- The actual in the formal package declaration may be a
9575 -- renamed generic package, in which case we want to retrieve
9576 -- the original generic in order to traverse its formal part.
9577
9578 if Present (Renamed_Entity (Actual)) then
9579 Gen_Decl := Unit_Declaration_Node (Renamed_Entity (Actual));
9580 else
9581 Gen_Decl := Unit_Declaration_Node (Actual);
9582 end if;
9583
9584 Formals := Generic_Formal_Declarations (Gen_Decl);
9585
9586 if Present (Formals) then
9587 F_Node := First_Non_Pragma (Formals);
9588 else
9589 F_Node := Empty;
9590 end if;
9591
9592 while Present (A_Ent)
9593 and then Present (F_Node)
9594 and then A_Ent /= First_Private_Entity (Act_Pkg)
9595 loop
9596 F_Ent := Get_Formal_Entity (F_Node);
9597
9598 if Present (F_Ent) then
9599
9600 -- This is a formal of the original package. Record
9601 -- association and recurse.
9602
9603 Find_Matching_Actual (F_Node, A_Ent);
9604 Match_Formal_Entity (F_Node, F_Ent, A_Ent);
9605 Next_Entity (A_Ent);
9606 end if;
9607
9608 Next_Non_Pragma (F_Node);
9609 end loop;
9610 end;
9611 end if;
9612 end Match_Formal_Entity;
9613
9614 -----------------------
9615 -- Get_Formal_Entity --
9616 -----------------------
9617
9618 function Get_Formal_Entity (N : Node_Id) return Entity_Id is
9619 Kind : constant Node_Kind := Nkind (Original_Node (N));
9620 begin
9621 case Kind is
9622 when N_Formal_Object_Declaration =>
9623 return Defining_Identifier (N);
9624
9625 when N_Formal_Type_Declaration =>
9626 return Defining_Identifier (N);
9627
9628 when N_Formal_Subprogram_Declaration =>
9629 return Defining_Unit_Name (Specification (N));
9630
9631 when N_Formal_Package_Declaration =>
9632 return Defining_Identifier (Original_Node (N));
9633
9634 when N_Generic_Package_Declaration =>
9635 return Defining_Identifier (Original_Node (N));
9636
9637 -- All other declarations are introduced by semantic analysis and
9638 -- have no match in the actual.
9639
9640 when others =>
9641 return Empty;
9642 end case;
9643 end Get_Formal_Entity;
9644
9645 --------------------
9646 -- Is_Instance_Of --
9647 --------------------
9648
9649 function Is_Instance_Of
9650 (Act_Spec : Entity_Id;
9651 Gen_Anc : Entity_Id) return Boolean
9652 is
9653 Gen_Par : constant Entity_Id := Generic_Parent (Act_Spec);
9654
9655 begin
9656 if No (Gen_Par) then
9657 return False;
9658
9659 -- Simplest case: the generic parent of the actual is the formal
9660
9661 elsif Gen_Par = Gen_Anc then
9662 return True;
9663
9664 elsif Chars (Gen_Par) /= Chars (Gen_Anc) then
9665 return False;
9666
9667 -- The actual may be obtained through several instantiations. Its
9668 -- scope must itself be an instance of a generic declared in the
9669 -- same scope as the formal. Any other case is detected above.
9670
9671 elsif not Is_Generic_Instance (Scope (Gen_Par)) then
9672 return False;
9673
9674 else
9675 return Generic_Parent (Parent (Scope (Gen_Par))) = Scope (Gen_Anc);
9676 end if;
9677 end Is_Instance_Of;
9678
9679 ---------------------------
9680 -- Process_Nested_Formal --
9681 ---------------------------
9682
9683 procedure Process_Nested_Formal (Formal : Entity_Id) is
9684 Ent : Entity_Id;
9685
9686 begin
9687 if Present (Associated_Formal_Package (Formal))
9688 and then Box_Present (Parent (Associated_Formal_Package (Formal)))
9689 then
9690 Ent := First_Entity (Formal);
9691 while Present (Ent) loop
9692 Set_Is_Hidden (Ent, False);
9693 Set_Is_Visible_Formal (Ent);
9694 Set_Is_Potentially_Use_Visible
9695 (Ent, Is_Potentially_Use_Visible (Formal));
9696
9697 if Ekind (Ent) = E_Package then
9698 exit when Renamed_Entity (Ent) = Renamed_Entity (Formal);
9699 Process_Nested_Formal (Ent);
9700 end if;
9701
9702 Next_Entity (Ent);
9703 end loop;
9704 end if;
9705 end Process_Nested_Formal;
9706
9707 -- Start of processing for Instantiate_Formal_Package
9708
9709 begin
9710 Analyze (Actual);
9711
9712 if not Is_Entity_Name (Actual)
9713 or else Ekind (Entity (Actual)) /= E_Package
9714 then
9715 Error_Msg_N
9716 ("expect package instance to instantiate formal", Actual);
9717 Abandon_Instantiation (Actual);
9718 raise Program_Error;
9719
9720 else
9721 Actual_Pack := Entity (Actual);
9722 Set_Is_Instantiated (Actual_Pack);
9723
9724 -- The actual may be a renamed package, or an outer generic formal
9725 -- package whose instantiation is converted into a renaming.
9726
9727 if Present (Renamed_Object (Actual_Pack)) then
9728 Actual_Pack := Renamed_Object (Actual_Pack);
9729 end if;
9730
9731 if Nkind (Analyzed_Formal) = N_Formal_Package_Declaration then
9732 Gen_Parent := Get_Instance_Of (Entity (Name (Analyzed_Formal)));
9733 Formal_Pack := Defining_Identifier (Analyzed_Formal);
9734 else
9735 Gen_Parent :=
9736 Generic_Parent (Specification (Analyzed_Formal));
9737 Formal_Pack :=
9738 Defining_Unit_Name (Specification (Analyzed_Formal));
9739 end if;
9740
9741 if Nkind (Parent (Actual_Pack)) = N_Defining_Program_Unit_Name then
9742 Parent_Spec := Package_Specification (Actual_Pack);
9743 else
9744 Parent_Spec := Parent (Actual_Pack);
9745 end if;
9746
9747 if Gen_Parent = Any_Id then
9748 Error_Msg_N
9749 ("previous error in declaration of formal package", Actual);
9750 Abandon_Instantiation (Actual);
9751
9752 elsif
9753 Is_Instance_Of (Parent_Spec, Get_Instance_Of (Gen_Parent))
9754 then
9755 null;
9756
9757 else
9758 Error_Msg_NE
9759 ("actual parameter must be instance of&", Actual, Gen_Parent);
9760 Abandon_Instantiation (Actual);
9761 end if;
9762
9763 Set_Instance_Of (Defining_Identifier (Formal), Actual_Pack);
9764 Map_Formal_Package_Entities (Formal_Pack, Actual_Pack);
9765
9766 Nod :=
9767 Make_Package_Renaming_Declaration (Loc,
9768 Defining_Unit_Name => New_Copy (Defining_Identifier (Formal)),
9769 Name => New_Occurrence_Of (Actual_Pack, Loc));
9770
9771 Set_Associated_Formal_Package
9772 (Defining_Unit_Name (Nod), Defining_Identifier (Formal));
9773 Decls := New_List (Nod);
9774
9775 -- If the formal F has a box, then the generic declarations are
9776 -- visible in the generic G. In an instance of G, the corresponding
9777 -- entities in the actual for F (which are the actuals for the
9778 -- instantiation of the generic that F denotes) must also be made
9779 -- visible for analysis of the current instance. On exit from the
9780 -- current instance, those entities are made private again. If the
9781 -- actual is currently in use, these entities are also use-visible.
9782
9783 -- The loop through the actual entities also steps through the formal
9784 -- entities and enters associations from formals to actuals into the
9785 -- renaming map. This is necessary to properly handle checking of
9786 -- actual parameter associations for later formals that depend on
9787 -- actuals declared in the formal package.
9788
9789 -- In Ada 2005, partial parameterization requires that we make
9790 -- visible the actuals corresponding to formals that were defaulted
9791 -- in the formal package. There formals are identified because they
9792 -- remain formal generics within the formal package, rather than
9793 -- being renamings of the actuals supplied.
9794
9795 declare
9796 Gen_Decl : constant Node_Id :=
9797 Unit_Declaration_Node (Gen_Parent);
9798 Formals : constant List_Id :=
9799 Generic_Formal_Declarations (Gen_Decl);
9800
9801 Actual_Ent : Entity_Id;
9802 Actual_Of_Formal : Node_Id;
9803 Formal_Node : Node_Id;
9804 Formal_Ent : Entity_Id;
9805
9806 begin
9807 if Present (Formals) then
9808 Formal_Node := First_Non_Pragma (Formals);
9809 else
9810 Formal_Node := Empty;
9811 end if;
9812
9813 Actual_Ent := First_Entity (Actual_Pack);
9814 Actual_Of_Formal :=
9815 First (Visible_Declarations (Specification (Analyzed_Formal)));
9816 while Present (Actual_Ent)
9817 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
9818 loop
9819 if Present (Formal_Node) then
9820 Formal_Ent := Get_Formal_Entity (Formal_Node);
9821
9822 if Present (Formal_Ent) then
9823 Find_Matching_Actual (Formal_Node, Actual_Ent);
9824 Match_Formal_Entity (Formal_Node, Formal_Ent, Actual_Ent);
9825
9826 -- We iterate at the same time over the actuals of the
9827 -- local package created for the formal, to determine
9828 -- which one of the formals of the original generic were
9829 -- defaulted in the formal. The corresponding actual
9830 -- entities are visible in the enclosing instance.
9831
9832 if Box_Present (Formal)
9833 or else
9834 (Present (Actual_Of_Formal)
9835 and then
9836 Is_Generic_Formal
9837 (Get_Formal_Entity (Actual_Of_Formal)))
9838 then
9839 Set_Is_Hidden (Actual_Ent, False);
9840 Set_Is_Visible_Formal (Actual_Ent);
9841 Set_Is_Potentially_Use_Visible
9842 (Actual_Ent, In_Use (Actual_Pack));
9843
9844 if Ekind (Actual_Ent) = E_Package then
9845 Process_Nested_Formal (Actual_Ent);
9846 end if;
9847
9848 else
9849 Set_Is_Hidden (Actual_Ent);
9850 Set_Is_Potentially_Use_Visible (Actual_Ent, False);
9851 end if;
9852 end if;
9853
9854 Next_Non_Pragma (Formal_Node);
9855 Next (Actual_Of_Formal);
9856
9857 else
9858 -- No further formals to match, but the generic part may
9859 -- contain inherited operation that are not hidden in the
9860 -- enclosing instance.
9861
9862 Next_Entity (Actual_Ent);
9863 end if;
9864 end loop;
9865
9866 -- Inherited subprograms generated by formal derived types are
9867 -- also visible if the types are.
9868
9869 Actual_Ent := First_Entity (Actual_Pack);
9870 while Present (Actual_Ent)
9871 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
9872 loop
9873 if Is_Overloadable (Actual_Ent)
9874 and then
9875 Nkind (Parent (Actual_Ent)) = N_Subtype_Declaration
9876 and then
9877 not Is_Hidden (Defining_Identifier (Parent (Actual_Ent)))
9878 then
9879 Set_Is_Hidden (Actual_Ent, False);
9880 Set_Is_Potentially_Use_Visible
9881 (Actual_Ent, In_Use (Actual_Pack));
9882 end if;
9883
9884 Next_Entity (Actual_Ent);
9885 end loop;
9886 end;
9887
9888 -- If the formal is not declared with a box, reanalyze it as an
9889 -- abbreviated instantiation, to verify the matching rules of 12.7.
9890 -- The actual checks are performed after the generic associations
9891 -- have been analyzed, to guarantee the same visibility for this
9892 -- instantiation and for the actuals.
9893
9894 -- In Ada 2005, the generic associations for the formal can include
9895 -- defaulted parameters. These are ignored during check. This
9896 -- internal instantiation is removed from the tree after conformance
9897 -- checking, because it contains formal declarations for those
9898 -- defaulted parameters, and those should not reach the back-end.
9899
9900 if not Box_Present (Formal) then
9901 declare
9902 I_Pack : constant Entity_Id :=
9903 Make_Temporary (Sloc (Actual), 'P');
9904
9905 begin
9906 Set_Is_Internal (I_Pack);
9907
9908 Append_To (Decls,
9909 Make_Package_Instantiation (Sloc (Actual),
9910 Defining_Unit_Name => I_Pack,
9911 Name =>
9912 New_Occurrence_Of
9913 (Get_Instance_Of (Gen_Parent), Sloc (Actual)),
9914 Generic_Associations => Generic_Associations (Formal)));
9915 end;
9916 end if;
9917
9918 return Decls;
9919 end if;
9920 end Instantiate_Formal_Package;
9921
9922 -----------------------------------
9923 -- Instantiate_Formal_Subprogram --
9924 -----------------------------------
9925
9926 function Instantiate_Formal_Subprogram
9927 (Formal : Node_Id;
9928 Actual : Node_Id;
9929 Analyzed_Formal : Node_Id) return Node_Id
9930 is
9931 Analyzed_S : constant Entity_Id :=
9932 Defining_Unit_Name (Specification (Analyzed_Formal));
9933 Formal_Sub : constant Entity_Id :=
9934 Defining_Unit_Name (Specification (Formal));
9935
9936 function From_Parent_Scope (Subp : Entity_Id) return Boolean;
9937 -- If the generic is a child unit, the parent has been installed on the
9938 -- scope stack, but a default subprogram cannot resolve to something
9939 -- on the parent because that parent is not really part of the visible
9940 -- context (it is there to resolve explicit local entities). If the
9941 -- default has resolved in this way, we remove the entity from immediate
9942 -- visibility and analyze the node again to emit an error message or
9943 -- find another visible candidate.
9944
9945 procedure Valid_Actual_Subprogram (Act : Node_Id);
9946 -- Perform legality check and raise exception on failure
9947
9948 -----------------------
9949 -- From_Parent_Scope --
9950 -----------------------
9951
9952 function From_Parent_Scope (Subp : Entity_Id) return Boolean is
9953 Gen_Scope : Node_Id;
9954
9955 begin
9956 Gen_Scope := Scope (Analyzed_S);
9957 while Present (Gen_Scope) and then Is_Child_Unit (Gen_Scope) loop
9958 if Scope (Subp) = Scope (Gen_Scope) then
9959 return True;
9960 end if;
9961
9962 Gen_Scope := Scope (Gen_Scope);
9963 end loop;
9964
9965 return False;
9966 end From_Parent_Scope;
9967
9968 -----------------------------
9969 -- Valid_Actual_Subprogram --
9970 -----------------------------
9971
9972 procedure Valid_Actual_Subprogram (Act : Node_Id) is
9973 Act_E : Entity_Id;
9974
9975 begin
9976 if Is_Entity_Name (Act) then
9977 Act_E := Entity (Act);
9978
9979 elsif Nkind (Act) = N_Selected_Component
9980 and then Is_Entity_Name (Selector_Name (Act))
9981 then
9982 Act_E := Entity (Selector_Name (Act));
9983
9984 else
9985 Act_E := Empty;
9986 end if;
9987
9988 if (Present (Act_E) and then Is_Overloadable (Act_E))
9989 or else Nkind_In (Act, N_Attribute_Reference,
9990 N_Indexed_Component,
9991 N_Character_Literal,
9992 N_Explicit_Dereference)
9993 then
9994 return;
9995 end if;
9996
9997 Error_Msg_NE
9998 ("expect subprogram or entry name in instantiation of &",
9999 Instantiation_Node, Formal_Sub);
10000 Abandon_Instantiation (Instantiation_Node);
10001 end Valid_Actual_Subprogram;
10002
10003 -- Local variables
10004
10005 Decl_Node : Node_Id;
10006 Loc : Source_Ptr;
10007 Nam : Node_Id;
10008 New_Spec : Node_Id;
10009 New_Subp : Entity_Id;
10010
10011 -- Start of processing for Instantiate_Formal_Subprogram
10012
10013 begin
10014 New_Spec := New_Copy_Tree (Specification (Formal));
10015
10016 -- The tree copy has created the proper instantiation sloc for the
10017 -- new specification. Use this location for all other constructed
10018 -- declarations.
10019
10020 Loc := Sloc (Defining_Unit_Name (New_Spec));
10021
10022 -- Create new entity for the actual (New_Copy_Tree does not), and
10023 -- indicate that it is an actual.
10024
10025 New_Subp := Make_Defining_Identifier (Loc, Chars (Formal_Sub));
10026 Set_Ekind (New_Subp, Ekind (Analyzed_S));
10027 Set_Is_Generic_Actual_Subprogram (New_Subp);
10028 Set_Defining_Unit_Name (New_Spec, New_Subp);
10029
10030 -- Create new entities for the each of the formals in the specification
10031 -- of the renaming declaration built for the actual.
10032
10033 if Present (Parameter_Specifications (New_Spec)) then
10034 declare
10035 F : Node_Id;
10036 F_Id : Entity_Id;
10037
10038 begin
10039 F := First (Parameter_Specifications (New_Spec));
10040 while Present (F) loop
10041 F_Id := Defining_Identifier (F);
10042
10043 Set_Defining_Identifier (F,
10044 Make_Defining_Identifier (Sloc (F_Id), Chars (F_Id)));
10045 Next (F);
10046 end loop;
10047 end;
10048 end if;
10049
10050 -- Find entity of actual. If the actual is an attribute reference, it
10051 -- cannot be resolved here (its formal is missing) but is handled
10052 -- instead in Attribute_Renaming. If the actual is overloaded, it is
10053 -- fully resolved subsequently, when the renaming declaration for the
10054 -- formal is analyzed. If it is an explicit dereference, resolve the
10055 -- prefix but not the actual itself, to prevent interpretation as call.
10056
10057 if Present (Actual) then
10058 Loc := Sloc (Actual);
10059 Set_Sloc (New_Spec, Loc);
10060
10061 if Nkind (Actual) = N_Operator_Symbol then
10062 Find_Direct_Name (Actual);
10063
10064 elsif Nkind (Actual) = N_Explicit_Dereference then
10065 Analyze (Prefix (Actual));
10066
10067 elsif Nkind (Actual) /= N_Attribute_Reference then
10068 Analyze (Actual);
10069 end if;
10070
10071 Valid_Actual_Subprogram (Actual);
10072 Nam := Actual;
10073
10074 elsif Present (Default_Name (Formal)) then
10075 if not Nkind_In (Default_Name (Formal), N_Attribute_Reference,
10076 N_Selected_Component,
10077 N_Indexed_Component,
10078 N_Character_Literal)
10079 and then Present (Entity (Default_Name (Formal)))
10080 then
10081 Nam := New_Occurrence_Of (Entity (Default_Name (Formal)), Loc);
10082 else
10083 Nam := New_Copy (Default_Name (Formal));
10084 Set_Sloc (Nam, Loc);
10085 end if;
10086
10087 elsif Box_Present (Formal) then
10088
10089 -- Actual is resolved at the point of instantiation. Create an
10090 -- identifier or operator with the same name as the formal.
10091
10092 if Nkind (Formal_Sub) = N_Defining_Operator_Symbol then
10093 Nam :=
10094 Make_Operator_Symbol (Loc,
10095 Chars => Chars (Formal_Sub),
10096 Strval => No_String);
10097 else
10098 Nam := Make_Identifier (Loc, Chars (Formal_Sub));
10099 end if;
10100
10101 elsif Nkind (Specification (Formal)) = N_Procedure_Specification
10102 and then Null_Present (Specification (Formal))
10103 then
10104 -- Generate null body for procedure, for use in the instance
10105
10106 Decl_Node :=
10107 Make_Subprogram_Body (Loc,
10108 Specification => New_Spec,
10109 Declarations => New_List,
10110 Handled_Statement_Sequence =>
10111 Make_Handled_Sequence_Of_Statements (Loc,
10112 Statements => New_List (Make_Null_Statement (Loc))));
10113
10114 Set_Is_Intrinsic_Subprogram (Defining_Unit_Name (New_Spec));
10115 return Decl_Node;
10116
10117 else
10118 Error_Msg_Sloc := Sloc (Scope (Analyzed_S));
10119 Error_Msg_NE
10120 ("missing actual&", Instantiation_Node, Formal_Sub);
10121 Error_Msg_NE
10122 ("\in instantiation of & declared#",
10123 Instantiation_Node, Scope (Analyzed_S));
10124 Abandon_Instantiation (Instantiation_Node);
10125 end if;
10126
10127 Decl_Node :=
10128 Make_Subprogram_Renaming_Declaration (Loc,
10129 Specification => New_Spec,
10130 Name => Nam);
10131
10132 -- If we do not have an actual and the formal specified <> then set to
10133 -- get proper default.
10134
10135 if No (Actual) and then Box_Present (Formal) then
10136 Set_From_Default (Decl_Node);
10137 end if;
10138
10139 -- Gather possible interpretations for the actual before analyzing the
10140 -- instance. If overloaded, it will be resolved when analyzing the
10141 -- renaming declaration.
10142
10143 if Box_Present (Formal) and then No (Actual) then
10144 Analyze (Nam);
10145
10146 if Is_Child_Unit (Scope (Analyzed_S))
10147 and then Present (Entity (Nam))
10148 then
10149 if not Is_Overloaded (Nam) then
10150 if From_Parent_Scope (Entity (Nam)) then
10151 Set_Is_Immediately_Visible (Entity (Nam), False);
10152 Set_Entity (Nam, Empty);
10153 Set_Etype (Nam, Empty);
10154
10155 Analyze (Nam);
10156 Set_Is_Immediately_Visible (Entity (Nam));
10157 end if;
10158
10159 else
10160 declare
10161 I : Interp_Index;
10162 It : Interp;
10163
10164 begin
10165 Get_First_Interp (Nam, I, It);
10166 while Present (It.Nam) loop
10167 if From_Parent_Scope (It.Nam) then
10168 Remove_Interp (I);
10169 end if;
10170
10171 Get_Next_Interp (I, It);
10172 end loop;
10173 end;
10174 end if;
10175 end if;
10176 end if;
10177
10178 -- The generic instantiation freezes the actual. This can only be done
10179 -- once the actual is resolved, in the analysis of the renaming
10180 -- declaration. To make the formal subprogram entity available, we set
10181 -- Corresponding_Formal_Spec to point to the formal subprogram entity.
10182 -- This is also needed in Analyze_Subprogram_Renaming for the processing
10183 -- of formal abstract subprograms.
10184
10185 Set_Corresponding_Formal_Spec (Decl_Node, Analyzed_S);
10186
10187 -- We cannot analyze the renaming declaration, and thus find the actual,
10188 -- until all the actuals are assembled in the instance. For subsequent
10189 -- checks of other actuals, indicate the node that will hold the
10190 -- instance of this formal.
10191
10192 Set_Instance_Of (Analyzed_S, Nam);
10193
10194 if Nkind (Actual) = N_Selected_Component
10195 and then Is_Task_Type (Etype (Prefix (Actual)))
10196 and then not Is_Frozen (Etype (Prefix (Actual)))
10197 then
10198 -- The renaming declaration will create a body, which must appear
10199 -- outside of the instantiation, We move the renaming declaration
10200 -- out of the instance, and create an additional renaming inside,
10201 -- to prevent freezing anomalies.
10202
10203 declare
10204 Anon_Id : constant Entity_Id := Make_Temporary (Loc, 'E');
10205
10206 begin
10207 Set_Defining_Unit_Name (New_Spec, Anon_Id);
10208 Insert_Before (Instantiation_Node, Decl_Node);
10209 Analyze (Decl_Node);
10210
10211 -- Now create renaming within the instance
10212
10213 Decl_Node :=
10214 Make_Subprogram_Renaming_Declaration (Loc,
10215 Specification => New_Copy_Tree (New_Spec),
10216 Name => New_Occurrence_Of (Anon_Id, Loc));
10217
10218 Set_Defining_Unit_Name (Specification (Decl_Node),
10219 Make_Defining_Identifier (Loc, Chars (Formal_Sub)));
10220 end;
10221 end if;
10222
10223 return Decl_Node;
10224 end Instantiate_Formal_Subprogram;
10225
10226 ------------------------
10227 -- Instantiate_Object --
10228 ------------------------
10229
10230 function Instantiate_Object
10231 (Formal : Node_Id;
10232 Actual : Node_Id;
10233 Analyzed_Formal : Node_Id) return List_Id
10234 is
10235 Gen_Obj : constant Entity_Id := Defining_Identifier (Formal);
10236 A_Gen_Obj : constant Entity_Id :=
10237 Defining_Identifier (Analyzed_Formal);
10238 Acc_Def : Node_Id := Empty;
10239 Act_Assoc : constant Node_Id := Parent (Actual);
10240 Actual_Decl : Node_Id := Empty;
10241 Decl_Node : Node_Id;
10242 Def : Node_Id;
10243 Ftyp : Entity_Id;
10244 List : constant List_Id := New_List;
10245 Loc : constant Source_Ptr := Sloc (Actual);
10246 Orig_Ftyp : constant Entity_Id := Etype (A_Gen_Obj);
10247 Subt_Decl : Node_Id := Empty;
10248 Subt_Mark : Node_Id := Empty;
10249
10250 function Copy_Access_Def return Node_Id;
10251 -- If formal is an anonymous access, copy access definition of formal
10252 -- for generated object declaration.
10253
10254 ---------------------
10255 -- Copy_Access_Def --
10256 ---------------------
10257
10258 function Copy_Access_Def return Node_Id is
10259 begin
10260 Def := New_Copy_Tree (Acc_Def);
10261
10262 -- In addition, if formal is an access to subprogram we need to
10263 -- generate new formals for the signature of the default, so that
10264 -- the tree is properly formatted for ASIS use.
10265
10266 if Present (Access_To_Subprogram_Definition (Acc_Def)) then
10267 declare
10268 Par_Spec : Node_Id;
10269 begin
10270 Par_Spec :=
10271 First (Parameter_Specifications
10272 (Access_To_Subprogram_Definition (Def)));
10273 while Present (Par_Spec) loop
10274 Set_Defining_Identifier (Par_Spec,
10275 Make_Defining_Identifier (Sloc (Acc_Def),
10276 Chars => Chars (Defining_Identifier (Par_Spec))));
10277 Next (Par_Spec);
10278 end loop;
10279 end;
10280 end if;
10281
10282 return Def;
10283 end Copy_Access_Def;
10284
10285 -- Start of processing for Instantiate_Object
10286
10287 begin
10288 -- Formal may be an anonymous access
10289
10290 if Present (Subtype_Mark (Formal)) then
10291 Subt_Mark := Subtype_Mark (Formal);
10292 else
10293 Check_Access_Definition (Formal);
10294 Acc_Def := Access_Definition (Formal);
10295 end if;
10296
10297 -- Sloc for error message on missing actual
10298
10299 Error_Msg_Sloc := Sloc (Scope (A_Gen_Obj));
10300
10301 if Get_Instance_Of (Gen_Obj) /= Gen_Obj then
10302 Error_Msg_N ("duplicate instantiation of generic parameter", Actual);
10303 end if;
10304
10305 Set_Parent (List, Parent (Actual));
10306
10307 -- OUT present
10308
10309 if Out_Present (Formal) then
10310
10311 -- An IN OUT generic actual must be a name. The instantiation is a
10312 -- renaming declaration. The actual is the name being renamed. We
10313 -- use the actual directly, rather than a copy, because it is not
10314 -- used further in the list of actuals, and because a copy or a use
10315 -- of relocate_node is incorrect if the instance is nested within a
10316 -- generic. In order to simplify ASIS searches, the Generic_Parent
10317 -- field links the declaration to the generic association.
10318
10319 if No (Actual) then
10320 Error_Msg_NE
10321 ("missing actual &",
10322 Instantiation_Node, Gen_Obj);
10323 Error_Msg_NE
10324 ("\in instantiation of & declared#",
10325 Instantiation_Node, Scope (A_Gen_Obj));
10326 Abandon_Instantiation (Instantiation_Node);
10327 end if;
10328
10329 if Present (Subt_Mark) then
10330 Decl_Node :=
10331 Make_Object_Renaming_Declaration (Loc,
10332 Defining_Identifier => New_Copy (Gen_Obj),
10333 Subtype_Mark => New_Copy_Tree (Subt_Mark),
10334 Name => Actual);
10335
10336 else pragma Assert (Present (Acc_Def));
10337 Decl_Node :=
10338 Make_Object_Renaming_Declaration (Loc,
10339 Defining_Identifier => New_Copy (Gen_Obj),
10340 Access_Definition => New_Copy_Tree (Acc_Def),
10341 Name => Actual);
10342 end if;
10343
10344 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
10345
10346 -- The analysis of the actual may produce Insert_Action nodes, so
10347 -- the declaration must have a context in which to attach them.
10348
10349 Append (Decl_Node, List);
10350 Analyze (Actual);
10351
10352 -- Return if the analysis of the actual reported some error
10353
10354 if Etype (Actual) = Any_Type then
10355 return List;
10356 end if;
10357
10358 -- This check is performed here because Analyze_Object_Renaming will
10359 -- not check it when Comes_From_Source is False. Note though that the
10360 -- check for the actual being the name of an object will be performed
10361 -- in Analyze_Object_Renaming.
10362
10363 if Is_Object_Reference (Actual)
10364 and then Is_Dependent_Component_Of_Mutable_Object (Actual)
10365 then
10366 Error_Msg_N
10367 ("illegal discriminant-dependent component for in out parameter",
10368 Actual);
10369 end if;
10370
10371 -- The actual has to be resolved in order to check that it is a
10372 -- variable (due to cases such as F (1), where F returns access to
10373 -- an array, and for overloaded prefixes).
10374
10375 Ftyp := Get_Instance_Of (Etype (A_Gen_Obj));
10376
10377 -- If the type of the formal is not itself a formal, and the current
10378 -- unit is a child unit, the formal type must be declared in a
10379 -- parent, and must be retrieved by visibility.
10380
10381 if Ftyp = Orig_Ftyp
10382 and then Is_Generic_Unit (Scope (Ftyp))
10383 and then Is_Child_Unit (Scope (A_Gen_Obj))
10384 then
10385 declare
10386 Temp : constant Node_Id :=
10387 New_Copy_Tree (Subtype_Mark (Analyzed_Formal));
10388 begin
10389 Set_Entity (Temp, Empty);
10390 Find_Type (Temp);
10391 Ftyp := Entity (Temp);
10392 end;
10393 end if;
10394
10395 if Is_Private_Type (Ftyp)
10396 and then not Is_Private_Type (Etype (Actual))
10397 and then (Base_Type (Full_View (Ftyp)) = Base_Type (Etype (Actual))
10398 or else Base_Type (Etype (Actual)) = Ftyp)
10399 then
10400 -- If the actual has the type of the full view of the formal, or
10401 -- else a non-private subtype of the formal, then the visibility
10402 -- of the formal type has changed. Add to the actuals a subtype
10403 -- declaration that will force the exchange of views in the body
10404 -- of the instance as well.
10405
10406 Subt_Decl :=
10407 Make_Subtype_Declaration (Loc,
10408 Defining_Identifier => Make_Temporary (Loc, 'P'),
10409 Subtype_Indication => New_Occurrence_Of (Ftyp, Loc));
10410
10411 Prepend (Subt_Decl, List);
10412
10413 Prepend_Elmt (Full_View (Ftyp), Exchanged_Views);
10414 Exchange_Declarations (Ftyp);
10415 end if;
10416
10417 Resolve (Actual, Ftyp);
10418
10419 if not Denotes_Variable (Actual) then
10420 Error_Msg_NE ("actual for& must be a variable", Actual, Gen_Obj);
10421
10422 elsif Base_Type (Ftyp) /= Base_Type (Etype (Actual)) then
10423
10424 -- Ada 2005 (AI-423): For a generic formal object of mode in out,
10425 -- the type of the actual shall resolve to a specific anonymous
10426 -- access type.
10427
10428 if Ada_Version < Ada_2005
10429 or else Ekind (Base_Type (Ftyp)) /=
10430 E_Anonymous_Access_Type
10431 or else Ekind (Base_Type (Etype (Actual))) /=
10432 E_Anonymous_Access_Type
10433 then
10434 Error_Msg_NE
10435 ("type of actual does not match type of&", Actual, Gen_Obj);
10436 end if;
10437 end if;
10438
10439 Note_Possible_Modification (Actual, Sure => True);
10440
10441 -- Check for instantiation of atomic/volatile actual for
10442 -- non-atomic/volatile formal (RM C.6 (12)).
10443
10444 if Is_Atomic_Object (Actual) and then not Is_Atomic (Orig_Ftyp) then
10445 Error_Msg_N
10446 ("cannot instantiate non-atomic formal object "
10447 & "with atomic actual", Actual);
10448
10449 elsif Is_Volatile_Object (Actual) and then not Is_Volatile (Orig_Ftyp)
10450 then
10451 Error_Msg_N
10452 ("cannot instantiate non-volatile formal object "
10453 & "with volatile actual", Actual);
10454 end if;
10455
10456 -- Formal in-parameter
10457
10458 else
10459 -- The instantiation of a generic formal in-parameter is constant
10460 -- declaration. The actual is the expression for that declaration.
10461 -- Its type is a full copy of the type of the formal. This may be
10462 -- an access to subprogram, for which we need to generate entities
10463 -- for the formals in the new signature.
10464
10465 if Present (Actual) then
10466 if Present (Subt_Mark) then
10467 Def := New_Copy_Tree (Subt_Mark);
10468 else pragma Assert (Present (Acc_Def));
10469 Def := Copy_Access_Def;
10470 end if;
10471
10472 Decl_Node :=
10473 Make_Object_Declaration (Loc,
10474 Defining_Identifier => New_Copy (Gen_Obj),
10475 Constant_Present => True,
10476 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
10477 Object_Definition => Def,
10478 Expression => Actual);
10479
10480 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
10481
10482 -- A generic formal object of a tagged type is defined to be
10483 -- aliased so the new constant must also be treated as aliased.
10484
10485 if Is_Tagged_Type (Etype (A_Gen_Obj)) then
10486 Set_Aliased_Present (Decl_Node);
10487 end if;
10488
10489 Append (Decl_Node, List);
10490
10491 -- No need to repeat (pre-)analysis of some expression nodes
10492 -- already handled in Preanalyze_Actuals.
10493
10494 if Nkind (Actual) /= N_Allocator then
10495 Analyze (Actual);
10496
10497 -- Return if the analysis of the actual reported some error
10498
10499 if Etype (Actual) = Any_Type then
10500 return List;
10501 end if;
10502 end if;
10503
10504 declare
10505 Formal_Type : constant Entity_Id := Etype (A_Gen_Obj);
10506 Typ : Entity_Id;
10507
10508 begin
10509 Typ := Get_Instance_Of (Formal_Type);
10510
10511 -- If the actual appears in the current or an enclosing scope,
10512 -- use its type directly. This is relevant if it has an actual
10513 -- subtype that is distinct from its nominal one. This cannot
10514 -- be done in general because the type of the actual may
10515 -- depend on other actuals, and only be fully determined when
10516 -- the enclosing instance is analyzed.
10517
10518 if Present (Etype (Actual))
10519 and then Is_Constr_Subt_For_U_Nominal (Etype (Actual))
10520 then
10521 Freeze_Before (Instantiation_Node, Etype (Actual));
10522 else
10523 Freeze_Before (Instantiation_Node, Typ);
10524 end if;
10525
10526 -- If the actual is an aggregate, perform name resolution on
10527 -- its components (the analysis of an aggregate does not do it)
10528 -- to capture local names that may be hidden if the generic is
10529 -- a child unit.
10530
10531 if Nkind (Actual) = N_Aggregate then
10532 Preanalyze_And_Resolve (Actual, Typ);
10533 end if;
10534
10535 if Is_Limited_Type (Typ)
10536 and then not OK_For_Limited_Init (Typ, Actual)
10537 then
10538 Error_Msg_N
10539 ("initialization not allowed for limited types", Actual);
10540 Explain_Limited_Type (Typ, Actual);
10541 end if;
10542 end;
10543
10544 elsif Present (Default_Expression (Formal)) then
10545
10546 -- Use default to construct declaration
10547
10548 if Present (Subt_Mark) then
10549 Def := New_Copy (Subt_Mark);
10550 else pragma Assert (Present (Acc_Def));
10551 Def := Copy_Access_Def;
10552 end if;
10553
10554 Decl_Node :=
10555 Make_Object_Declaration (Sloc (Formal),
10556 Defining_Identifier => New_Copy (Gen_Obj),
10557 Constant_Present => True,
10558 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
10559 Object_Definition => Def,
10560 Expression => New_Copy_Tree
10561 (Default_Expression (Formal)));
10562
10563 Append (Decl_Node, List);
10564 Set_Analyzed (Expression (Decl_Node), False);
10565
10566 else
10567 Error_Msg_NE ("missing actual&", Instantiation_Node, Gen_Obj);
10568 Error_Msg_NE ("\in instantiation of & declared#",
10569 Instantiation_Node, Scope (A_Gen_Obj));
10570
10571 if Is_Scalar_Type (Etype (A_Gen_Obj)) then
10572
10573 -- Create dummy constant declaration so that instance can be
10574 -- analyzed, to minimize cascaded visibility errors.
10575
10576 if Present (Subt_Mark) then
10577 Def := Subt_Mark;
10578 else pragma Assert (Present (Acc_Def));
10579 Def := Acc_Def;
10580 end if;
10581
10582 Decl_Node :=
10583 Make_Object_Declaration (Loc,
10584 Defining_Identifier => New_Copy (Gen_Obj),
10585 Constant_Present => True,
10586 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
10587 Object_Definition => New_Copy (Def),
10588 Expression =>
10589 Make_Attribute_Reference (Sloc (Gen_Obj),
10590 Attribute_Name => Name_First,
10591 Prefix => New_Copy (Def)));
10592
10593 Append (Decl_Node, List);
10594
10595 else
10596 Abandon_Instantiation (Instantiation_Node);
10597 end if;
10598 end if;
10599 end if;
10600
10601 if Nkind (Actual) in N_Has_Entity then
10602 Actual_Decl := Parent (Entity (Actual));
10603 end if;
10604
10605 -- Ada 2005 (AI-423): For a formal object declaration with a null
10606 -- exclusion or an access definition that has a null exclusion: If the
10607 -- actual matching the formal object declaration denotes a generic
10608 -- formal object of another generic unit G, and the instantiation
10609 -- containing the actual occurs within the body of G or within the body
10610 -- of a generic unit declared within the declarative region of G, then
10611 -- the declaration of the formal object of G must have a null exclusion.
10612 -- Otherwise, the subtype of the actual matching the formal object
10613 -- declaration shall exclude null.
10614
10615 if Ada_Version >= Ada_2005
10616 and then Present (Actual_Decl)
10617 and then Nkind_In (Actual_Decl, N_Formal_Object_Declaration,
10618 N_Object_Declaration)
10619 and then Nkind (Analyzed_Formal) = N_Formal_Object_Declaration
10620 and then not Has_Null_Exclusion (Actual_Decl)
10621 and then Has_Null_Exclusion (Analyzed_Formal)
10622 then
10623 Error_Msg_Sloc := Sloc (Analyzed_Formal);
10624 Error_Msg_N
10625 ("actual must exclude null to match generic formal#", Actual);
10626 end if;
10627
10628 -- An effectively volatile object cannot be used as an actual in a
10629 -- generic instantiation (SPARK RM 7.1.3(7)). The following check is
10630 -- relevant only when SPARK_Mode is on as it is not a standard Ada
10631 -- legality rule.
10632
10633 if SPARK_Mode = On
10634 and then Present (Actual)
10635 and then Is_Effectively_Volatile_Object (Actual)
10636 then
10637 Error_Msg_N
10638 ("volatile object cannot act as actual in generic instantiation",
10639 Actual);
10640 end if;
10641
10642 return List;
10643 end Instantiate_Object;
10644
10645 ------------------------------
10646 -- Instantiate_Package_Body --
10647 ------------------------------
10648
10649 procedure Instantiate_Package_Body
10650 (Body_Info : Pending_Body_Info;
10651 Inlined_Body : Boolean := False;
10652 Body_Optional : Boolean := False)
10653 is
10654 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
10655 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
10656 Loc : constant Source_Ptr := Sloc (Inst_Node);
10657
10658 Gen_Id : constant Node_Id := Name (Inst_Node);
10659 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
10660 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
10661 Act_Spec : constant Node_Id := Specification (Act_Decl);
10662 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Spec);
10663
10664 Save_IPSM : constant Boolean := Ignore_Pragma_SPARK_Mode;
10665 Save_Style_Check : constant Boolean := Style_Check;
10666
10667 Act_Body : Node_Id;
10668 Act_Body_Id : Entity_Id;
10669 Act_Body_Name : Node_Id;
10670 Gen_Body : Node_Id;
10671 Gen_Body_Id : Node_Id;
10672 Par_Ent : Entity_Id := Empty;
10673 Par_Vis : Boolean := False;
10674
10675 Parent_Installed : Boolean := False;
10676
10677 Vis_Prims_List : Elist_Id := No_Elist;
10678 -- List of primitives made temporarily visible in the instantiation
10679 -- to match the visibility of the formal type
10680
10681 procedure Check_Initialized_Types;
10682 -- In a generic package body, an entity of a generic private type may
10683 -- appear uninitialized. This is suspicious, unless the actual is a
10684 -- fully initialized type.
10685
10686 -----------------------------
10687 -- Check_Initialized_Types --
10688 -----------------------------
10689
10690 procedure Check_Initialized_Types is
10691 Decl : Node_Id;
10692 Formal : Entity_Id;
10693 Actual : Entity_Id;
10694 Uninit_Var : Entity_Id;
10695
10696 begin
10697 Decl := First (Generic_Formal_Declarations (Gen_Decl));
10698 while Present (Decl) loop
10699 Uninit_Var := Empty;
10700
10701 if Nkind (Decl) = N_Private_Extension_Declaration then
10702 Uninit_Var := Uninitialized_Variable (Decl);
10703
10704 elsif Nkind (Decl) = N_Formal_Type_Declaration
10705 and then Nkind (Formal_Type_Definition (Decl)) =
10706 N_Formal_Private_Type_Definition
10707 then
10708 Uninit_Var :=
10709 Uninitialized_Variable (Formal_Type_Definition (Decl));
10710 end if;
10711
10712 if Present (Uninit_Var) then
10713 Formal := Defining_Identifier (Decl);
10714 Actual := First_Entity (Act_Decl_Id);
10715
10716 -- For each formal there is a subtype declaration that renames
10717 -- the actual and has the same name as the formal. Locate the
10718 -- formal for warning message about uninitialized variables
10719 -- in the generic, for which the actual type should be a fully
10720 -- initialized type.
10721
10722 while Present (Actual) loop
10723 exit when Ekind (Actual) = E_Package
10724 and then Present (Renamed_Object (Actual));
10725
10726 if Chars (Actual) = Chars (Formal)
10727 and then not Is_Scalar_Type (Actual)
10728 and then not Is_Fully_Initialized_Type (Actual)
10729 and then Warn_On_No_Value_Assigned
10730 then
10731 Error_Msg_Node_2 := Formal;
10732 Error_Msg_NE
10733 ("generic unit has uninitialized variable& of "
10734 & "formal private type &?v?", Actual, Uninit_Var);
10735 Error_Msg_NE
10736 ("actual type for& should be fully initialized type?v?",
10737 Actual, Formal);
10738 exit;
10739 end if;
10740
10741 Next_Entity (Actual);
10742 end loop;
10743 end if;
10744
10745 Next (Decl);
10746 end loop;
10747 end Check_Initialized_Types;
10748
10749 -- Start of processing for Instantiate_Package_Body
10750
10751 begin
10752 Gen_Body_Id := Corresponding_Body (Gen_Decl);
10753
10754 -- The instance body may already have been processed, as the parent of
10755 -- another instance that is inlined (Load_Parent_Of_Generic).
10756
10757 if Present (Corresponding_Body (Instance_Spec (Inst_Node))) then
10758 return;
10759 end if;
10760
10761 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
10762
10763 -- Re-establish the state of information on which checks are suppressed.
10764 -- This information was set in Body_Info at the point of instantiation,
10765 -- and now we restore it so that the instance is compiled using the
10766 -- check status at the instantiation (RM 11.5(7.2/2), AI95-00224-01).
10767
10768 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
10769 Scope_Suppress := Body_Info.Scope_Suppress;
10770 Opt.Ada_Version := Body_Info.Version;
10771 Opt.Ada_Version_Pragma := Body_Info.Version_Pragma;
10772 Restore_Warnings (Body_Info.Warnings);
10773 Opt.SPARK_Mode := Body_Info.SPARK_Mode;
10774 Opt.SPARK_Mode_Pragma := Body_Info.SPARK_Mode_Pragma;
10775
10776 if No (Gen_Body_Id) then
10777
10778 -- Do not look for parent of generic body if none is required.
10779 -- This may happen when the routine is called as part of the
10780 -- Pending_Instantiations processing, when nested instances
10781 -- may precede the one generated from the main unit.
10782
10783 if not Unit_Requires_Body (Defining_Entity (Gen_Decl))
10784 and then Body_Optional
10785 then
10786 return;
10787 else
10788 Load_Parent_Of_Generic
10789 (Inst_Node, Specification (Gen_Decl), Body_Optional);
10790 Gen_Body_Id := Corresponding_Body (Gen_Decl);
10791 end if;
10792 end if;
10793
10794 -- Establish global variable for sloc adjustment and for error recovery
10795 -- In the case of an instance body for an instantiation with actuals
10796 -- from a limited view, the instance body is placed at the beginning
10797 -- of the enclosing package body: use the body entity as the source
10798 -- location for nodes of the instance body.
10799
10800 if not Is_Empty_Elmt_List (Incomplete_Actuals (Act_Decl_Id)) then
10801 declare
10802 Scop : constant Entity_Id := Scope (Act_Decl_Id);
10803 Body_Id : constant Node_Id :=
10804 Corresponding_Body (Unit_Declaration_Node (Scop));
10805
10806 begin
10807 Instantiation_Node := Body_Id;
10808 end;
10809 else
10810 Instantiation_Node := Inst_Node;
10811 end if;
10812
10813 if Present (Gen_Body_Id) then
10814 Save_Env (Gen_Unit, Act_Decl_Id);
10815 Style_Check := False;
10816
10817 -- If the context of the instance is subject to SPARK_Mode "off" or
10818 -- the annotation is altogether missing, set the global flag which
10819 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
10820 -- the instance.
10821
10822 if SPARK_Mode /= On then
10823 Ignore_Pragma_SPARK_Mode := True;
10824 end if;
10825
10826 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
10827 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
10828
10829 Create_Instantiation_Source
10830 (Inst_Node, Gen_Body_Id, False, S_Adjustment);
10831
10832 Act_Body :=
10833 Copy_Generic_Node
10834 (Original_Node (Gen_Body), Empty, Instantiating => True);
10835
10836 -- Create proper (possibly qualified) defining name for the body, to
10837 -- correspond to the one in the spec.
10838
10839 Act_Body_Id :=
10840 Make_Defining_Identifier (Sloc (Act_Decl_Id), Chars (Act_Decl_Id));
10841 Set_Comes_From_Source (Act_Body_Id, Comes_From_Source (Act_Decl_Id));
10842
10843 -- Some attributes of spec entity are not inherited by body entity
10844
10845 Set_Handler_Records (Act_Body_Id, No_List);
10846
10847 if Nkind (Defining_Unit_Name (Act_Spec)) =
10848 N_Defining_Program_Unit_Name
10849 then
10850 Act_Body_Name :=
10851 Make_Defining_Program_Unit_Name (Loc,
10852 Name =>
10853 New_Copy_Tree (Name (Defining_Unit_Name (Act_Spec))),
10854 Defining_Identifier => Act_Body_Id);
10855 else
10856 Act_Body_Name := Act_Body_Id;
10857 end if;
10858
10859 Set_Defining_Unit_Name (Act_Body, Act_Body_Name);
10860
10861 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
10862 Check_Generic_Actuals (Act_Decl_Id, False);
10863 Check_Initialized_Types;
10864
10865 -- Install primitives hidden at the point of the instantiation but
10866 -- visible when processing the generic formals
10867
10868 declare
10869 E : Entity_Id;
10870
10871 begin
10872 E := First_Entity (Act_Decl_Id);
10873 while Present (E) loop
10874 if Is_Type (E)
10875 and then Is_Generic_Actual_Type (E)
10876 and then Is_Tagged_Type (E)
10877 then
10878 Install_Hidden_Primitives
10879 (Prims_List => Vis_Prims_List,
10880 Gen_T => Generic_Parent_Type (Parent (E)),
10881 Act_T => E);
10882 end if;
10883
10884 Next_Entity (E);
10885 end loop;
10886 end;
10887
10888 -- If it is a child unit, make the parent instance (which is an
10889 -- instance of the parent of the generic) visible. The parent
10890 -- instance is the prefix of the name of the generic unit.
10891
10892 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
10893 and then Nkind (Gen_Id) = N_Expanded_Name
10894 then
10895 Par_Ent := Entity (Prefix (Gen_Id));
10896 Par_Vis := Is_Immediately_Visible (Par_Ent);
10897 Install_Parent (Par_Ent, In_Body => True);
10898 Parent_Installed := True;
10899
10900 elsif Is_Child_Unit (Gen_Unit) then
10901 Par_Ent := Scope (Gen_Unit);
10902 Par_Vis := Is_Immediately_Visible (Par_Ent);
10903 Install_Parent (Par_Ent, In_Body => True);
10904 Parent_Installed := True;
10905 end if;
10906
10907 -- If the instantiation is a library unit, and this is the main unit,
10908 -- then build the resulting compilation unit nodes for the instance.
10909 -- If this is a compilation unit but it is not the main unit, then it
10910 -- is the body of a unit in the context, that is being compiled
10911 -- because it is encloses some inlined unit or another generic unit
10912 -- being instantiated. In that case, this body is not part of the
10913 -- current compilation, and is not attached to the tree, but its
10914 -- parent must be set for analysis.
10915
10916 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
10917
10918 -- Replace instance node with body of instance, and create new
10919 -- node for corresponding instance declaration.
10920
10921 Build_Instance_Compilation_Unit_Nodes
10922 (Inst_Node, Act_Body, Act_Decl);
10923 Analyze (Inst_Node);
10924
10925 if Parent (Inst_Node) = Cunit (Main_Unit) then
10926
10927 -- If the instance is a child unit itself, then set the scope
10928 -- of the expanded body to be the parent of the instantiation
10929 -- (ensuring that the fully qualified name will be generated
10930 -- for the elaboration subprogram).
10931
10932 if Nkind (Defining_Unit_Name (Act_Spec)) =
10933 N_Defining_Program_Unit_Name
10934 then
10935 Set_Scope (Defining_Entity (Inst_Node), Scope (Act_Decl_Id));
10936 end if;
10937 end if;
10938
10939 -- Case where instantiation is not a library unit
10940
10941 else
10942 -- If this is an early instantiation, i.e. appears textually
10943 -- before the corresponding body and must be elaborated first,
10944 -- indicate that the body instance is to be delayed.
10945
10946 Install_Body (Act_Body, Inst_Node, Gen_Body, Gen_Decl);
10947
10948 -- Now analyze the body. We turn off all checks if this is an
10949 -- internal unit, since there is no reason to have checks on for
10950 -- any predefined run-time library code. All such code is designed
10951 -- to be compiled with checks off.
10952
10953 -- Note that we do NOT apply this criterion to children of GNAT
10954 -- The latter units must suppress checks explicitly if needed.
10955
10956 if Is_Predefined_File_Name
10957 (Unit_File_Name (Get_Source_Unit (Gen_Decl)))
10958 then
10959 Analyze (Act_Body, Suppress => All_Checks);
10960 else
10961 Analyze (Act_Body);
10962 end if;
10963 end if;
10964
10965 Inherit_Context (Gen_Body, Inst_Node);
10966
10967 -- Remove the parent instances if they have been placed on the scope
10968 -- stack to compile the body.
10969
10970 if Parent_Installed then
10971 Remove_Parent (In_Body => True);
10972
10973 -- Restore the previous visibility of the parent
10974
10975 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
10976 end if;
10977
10978 Restore_Hidden_Primitives (Vis_Prims_List);
10979 Restore_Private_Views (Act_Decl_Id);
10980
10981 -- Remove the current unit from visibility if this is an instance
10982 -- that is not elaborated on the fly for inlining purposes.
10983
10984 if not Inlined_Body then
10985 Set_Is_Immediately_Visible (Act_Decl_Id, False);
10986 end if;
10987
10988 Restore_Env;
10989 Ignore_Pragma_SPARK_Mode := Save_IPSM;
10990 Style_Check := Save_Style_Check;
10991
10992 -- If we have no body, and the unit requires a body, then complain. This
10993 -- complaint is suppressed if we have detected other errors (since a
10994 -- common reason for missing the body is that it had errors).
10995 -- In CodePeer mode, a warning has been emitted already, no need for
10996 -- further messages.
10997
10998 elsif Unit_Requires_Body (Gen_Unit)
10999 and then not Body_Optional
11000 then
11001 if CodePeer_Mode then
11002 null;
11003
11004 elsif Serious_Errors_Detected = 0 then
11005 Error_Msg_NE
11006 ("cannot find body of generic package &", Inst_Node, Gen_Unit);
11007
11008 -- Don't attempt to perform any cleanup actions if some other error
11009 -- was already detected, since this can cause blowups.
11010
11011 else
11012 return;
11013 end if;
11014
11015 -- Case of package that does not need a body
11016
11017 else
11018 -- If the instantiation of the declaration is a library unit, rewrite
11019 -- the original package instantiation as a package declaration in the
11020 -- compilation unit node.
11021
11022 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
11023 Set_Parent_Spec (Act_Decl, Parent_Spec (Inst_Node));
11024 Rewrite (Inst_Node, Act_Decl);
11025
11026 -- Generate elaboration entity, in case spec has elaboration code.
11027 -- This cannot be done when the instance is analyzed, because it
11028 -- is not known yet whether the body exists.
11029
11030 Set_Elaboration_Entity_Required (Act_Decl_Id, False);
11031 Build_Elaboration_Entity (Parent (Inst_Node), Act_Decl_Id);
11032
11033 -- If the instantiation is not a library unit, then append the
11034 -- declaration to the list of implicitly generated entities, unless
11035 -- it is already a list member which means that it was already
11036 -- processed
11037
11038 elsif not Is_List_Member (Act_Decl) then
11039 Mark_Rewrite_Insertion (Act_Decl);
11040 Insert_Before (Inst_Node, Act_Decl);
11041 end if;
11042 end if;
11043
11044 Expander_Mode_Restore;
11045 end Instantiate_Package_Body;
11046
11047 ---------------------------------
11048 -- Instantiate_Subprogram_Body --
11049 ---------------------------------
11050
11051 procedure Instantiate_Subprogram_Body
11052 (Body_Info : Pending_Body_Info;
11053 Body_Optional : Boolean := False)
11054 is
11055 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
11056 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
11057 Loc : constant Source_Ptr := Sloc (Inst_Node);
11058 Gen_Id : constant Node_Id := Name (Inst_Node);
11059 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
11060 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
11061 Act_Decl_Id : constant Entity_Id :=
11062 Defining_Unit_Name (Specification (Act_Decl));
11063 Pack_Id : constant Entity_Id :=
11064 Defining_Unit_Name (Parent (Act_Decl));
11065
11066 Saved_IPSM : constant Boolean := Ignore_Pragma_SPARK_Mode;
11067 Saved_Style_Check : constant Boolean := Style_Check;
11068 Saved_Warnings : constant Warning_Record := Save_Warnings;
11069
11070 Act_Body : Node_Id;
11071 Act_Body_Id : Entity_Id;
11072 Gen_Body : Node_Id;
11073 Gen_Body_Id : Node_Id;
11074 Pack_Body : Node_Id;
11075 Par_Ent : Entity_Id := Empty;
11076 Par_Vis : Boolean := False;
11077 Ret_Expr : Node_Id;
11078
11079 Parent_Installed : Boolean := False;
11080
11081 begin
11082 Gen_Body_Id := Corresponding_Body (Gen_Decl);
11083
11084 -- Subprogram body may have been created already because of an inline
11085 -- pragma, or because of multiple elaborations of the enclosing package
11086 -- when several instances of the subprogram appear in the main unit.
11087
11088 if Present (Corresponding_Body (Act_Decl)) then
11089 return;
11090 end if;
11091
11092 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
11093
11094 -- Re-establish the state of information on which checks are suppressed.
11095 -- This information was set in Body_Info at the point of instantiation,
11096 -- and now we restore it so that the instance is compiled using the
11097 -- check status at the instantiation (RM 11.5(7.2/2), AI95-00224-01).
11098
11099 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
11100 Scope_Suppress := Body_Info.Scope_Suppress;
11101 Opt.Ada_Version := Body_Info.Version;
11102 Opt.Ada_Version_Pragma := Body_Info.Version_Pragma;
11103 Restore_Warnings (Body_Info.Warnings);
11104 Opt.SPARK_Mode := Body_Info.SPARK_Mode;
11105 Opt.SPARK_Mode_Pragma := Body_Info.SPARK_Mode_Pragma;
11106
11107 if No (Gen_Body_Id) then
11108
11109 -- For imported generic subprogram, no body to compile, complete
11110 -- the spec entity appropriately.
11111
11112 if Is_Imported (Gen_Unit) then
11113 Set_Is_Imported (Act_Decl_Id);
11114 Set_First_Rep_Item (Act_Decl_Id, First_Rep_Item (Gen_Unit));
11115 Set_Interface_Name (Act_Decl_Id, Interface_Name (Gen_Unit));
11116 Set_Convention (Act_Decl_Id, Convention (Gen_Unit));
11117 Set_Has_Completion (Act_Decl_Id);
11118 return;
11119
11120 -- For other cases, compile the body
11121
11122 else
11123 Load_Parent_Of_Generic
11124 (Inst_Node, Specification (Gen_Decl), Body_Optional);
11125 Gen_Body_Id := Corresponding_Body (Gen_Decl);
11126 end if;
11127 end if;
11128
11129 Instantiation_Node := Inst_Node;
11130
11131 if Present (Gen_Body_Id) then
11132 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
11133
11134 if Nkind (Gen_Body) = N_Subprogram_Body_Stub then
11135
11136 -- Either body is not present, or context is non-expanding, as
11137 -- when compiling a subunit. Mark the instance as completed, and
11138 -- diagnose a missing body when needed.
11139
11140 if Expander_Active
11141 and then Operating_Mode = Generate_Code
11142 then
11143 Error_Msg_N
11144 ("missing proper body for instantiation", Gen_Body);
11145 end if;
11146
11147 Set_Has_Completion (Act_Decl_Id);
11148 return;
11149 end if;
11150
11151 Save_Env (Gen_Unit, Act_Decl_Id);
11152 Style_Check := False;
11153
11154 -- If the context of the instance is subject to SPARK_Mode "off" or
11155 -- the annotation is altogether missing, set the global flag which
11156 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
11157 -- the instance.
11158
11159 if SPARK_Mode /= On then
11160 Ignore_Pragma_SPARK_Mode := True;
11161 end if;
11162
11163 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
11164 Create_Instantiation_Source
11165 (Inst_Node,
11166 Gen_Body_Id,
11167 False,
11168 S_Adjustment);
11169
11170 Act_Body :=
11171 Copy_Generic_Node
11172 (Original_Node (Gen_Body), Empty, Instantiating => True);
11173
11174 -- Create proper defining name for the body, to correspond to the one
11175 -- in the spec.
11176
11177 Act_Body_Id :=
11178 Make_Defining_Identifier (Sloc (Act_Decl_Id), Chars (Act_Decl_Id));
11179
11180 Set_Comes_From_Source (Act_Body_Id, Comes_From_Source (Act_Decl_Id));
11181 Set_Defining_Unit_Name (Specification (Act_Body), Act_Body_Id);
11182
11183 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
11184 Set_Has_Completion (Act_Decl_Id);
11185 Check_Generic_Actuals (Pack_Id, False);
11186
11187 -- Generate a reference to link the visible subprogram instance to
11188 -- the generic body, which for navigation purposes is the only
11189 -- available source for the instance.
11190
11191 Generate_Reference
11192 (Related_Instance (Pack_Id),
11193 Gen_Body_Id, 'b', Set_Ref => False, Force => True);
11194
11195 -- If it is a child unit, make the parent instance (which is an
11196 -- instance of the parent of the generic) visible. The parent
11197 -- instance is the prefix of the name of the generic unit.
11198
11199 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
11200 and then Nkind (Gen_Id) = N_Expanded_Name
11201 then
11202 Par_Ent := Entity (Prefix (Gen_Id));
11203 Par_Vis := Is_Immediately_Visible (Par_Ent);
11204 Install_Parent (Par_Ent, In_Body => True);
11205 Parent_Installed := True;
11206
11207 elsif Is_Child_Unit (Gen_Unit) then
11208 Par_Ent := Scope (Gen_Unit);
11209 Par_Vis := Is_Immediately_Visible (Par_Ent);
11210 Install_Parent (Par_Ent, In_Body => True);
11211 Parent_Installed := True;
11212 end if;
11213
11214 -- Subprogram body is placed in the body of wrapper package,
11215 -- whose spec contains the subprogram declaration as well as
11216 -- the renaming declarations for the generic parameters.
11217
11218 Pack_Body :=
11219 Make_Package_Body (Loc,
11220 Defining_Unit_Name => New_Copy (Pack_Id),
11221 Declarations => New_List (Act_Body));
11222
11223 Set_Corresponding_Spec (Pack_Body, Pack_Id);
11224
11225 -- If the instantiation is a library unit, then build resulting
11226 -- compilation unit nodes for the instance. The declaration of
11227 -- the enclosing package is the grandparent of the subprogram
11228 -- declaration. First replace the instantiation node as the unit
11229 -- of the corresponding compilation.
11230
11231 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
11232 if Parent (Inst_Node) = Cunit (Main_Unit) then
11233 Set_Unit (Parent (Inst_Node), Inst_Node);
11234 Build_Instance_Compilation_Unit_Nodes
11235 (Inst_Node, Pack_Body, Parent (Parent (Act_Decl)));
11236 Analyze (Inst_Node);
11237 else
11238 Set_Parent (Pack_Body, Parent (Inst_Node));
11239 Analyze (Pack_Body);
11240 end if;
11241
11242 else
11243 Insert_Before (Inst_Node, Pack_Body);
11244 Mark_Rewrite_Insertion (Pack_Body);
11245 Analyze (Pack_Body);
11246
11247 if Expander_Active then
11248 Freeze_Subprogram_Body (Inst_Node, Gen_Body, Pack_Id);
11249 end if;
11250 end if;
11251
11252 Inherit_Context (Gen_Body, Inst_Node);
11253
11254 Restore_Private_Views (Pack_Id, False);
11255
11256 if Parent_Installed then
11257 Remove_Parent (In_Body => True);
11258
11259 -- Restore the previous visibility of the parent
11260
11261 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
11262 end if;
11263
11264 Restore_Env;
11265 Ignore_Pragma_SPARK_Mode := Saved_IPSM;
11266 Style_Check := Saved_Style_Check;
11267 Restore_Warnings (Saved_Warnings);
11268
11269 -- Body not found. Error was emitted already. If there were no previous
11270 -- errors, this may be an instance whose scope is a premature instance.
11271 -- In that case we must insure that the (legal) program does raise
11272 -- program error if executed. We generate a subprogram body for this
11273 -- purpose. See DEC ac30vso.
11274
11275 -- Should not reference proprietary DEC tests in comments ???
11276
11277 elsif Serious_Errors_Detected = 0
11278 and then Nkind (Parent (Inst_Node)) /= N_Compilation_Unit
11279 then
11280 if Body_Optional then
11281 return;
11282
11283 elsif Ekind (Act_Decl_Id) = E_Procedure then
11284 Act_Body :=
11285 Make_Subprogram_Body (Loc,
11286 Specification =>
11287 Make_Procedure_Specification (Loc,
11288 Defining_Unit_Name =>
11289 Make_Defining_Identifier (Loc, Chars (Act_Decl_Id)),
11290 Parameter_Specifications =>
11291 New_Copy_List
11292 (Parameter_Specifications (Parent (Act_Decl_Id)))),
11293
11294 Declarations => Empty_List,
11295 Handled_Statement_Sequence =>
11296 Make_Handled_Sequence_Of_Statements (Loc,
11297 Statements =>
11298 New_List (
11299 Make_Raise_Program_Error (Loc,
11300 Reason =>
11301 PE_Access_Before_Elaboration))));
11302
11303 else
11304 Ret_Expr :=
11305 Make_Raise_Program_Error (Loc,
11306 Reason => PE_Access_Before_Elaboration);
11307
11308 Set_Etype (Ret_Expr, (Etype (Act_Decl_Id)));
11309 Set_Analyzed (Ret_Expr);
11310
11311 Act_Body :=
11312 Make_Subprogram_Body (Loc,
11313 Specification =>
11314 Make_Function_Specification (Loc,
11315 Defining_Unit_Name =>
11316 Make_Defining_Identifier (Loc, Chars (Act_Decl_Id)),
11317 Parameter_Specifications =>
11318 New_Copy_List
11319 (Parameter_Specifications (Parent (Act_Decl_Id))),
11320 Result_Definition =>
11321 New_Occurrence_Of (Etype (Act_Decl_Id), Loc)),
11322
11323 Declarations => Empty_List,
11324 Handled_Statement_Sequence =>
11325 Make_Handled_Sequence_Of_Statements (Loc,
11326 Statements =>
11327 New_List
11328 (Make_Simple_Return_Statement (Loc, Ret_Expr))));
11329 end if;
11330
11331 Pack_Body :=
11332 Make_Package_Body (Loc,
11333 Defining_Unit_Name => New_Copy (Pack_Id),
11334 Declarations => New_List (Act_Body));
11335
11336 Insert_After (Inst_Node, Pack_Body);
11337 Set_Corresponding_Spec (Pack_Body, Pack_Id);
11338 Analyze (Pack_Body);
11339 end if;
11340
11341 Expander_Mode_Restore;
11342 end Instantiate_Subprogram_Body;
11343
11344 ----------------------
11345 -- Instantiate_Type --
11346 ----------------------
11347
11348 function Instantiate_Type
11349 (Formal : Node_Id;
11350 Actual : Node_Id;
11351 Analyzed_Formal : Node_Id;
11352 Actual_Decls : List_Id) return List_Id
11353 is
11354 Gen_T : constant Entity_Id := Defining_Identifier (Formal);
11355 A_Gen_T : constant Entity_Id :=
11356 Defining_Identifier (Analyzed_Formal);
11357 Ancestor : Entity_Id := Empty;
11358 Def : constant Node_Id := Formal_Type_Definition (Formal);
11359 Act_T : Entity_Id;
11360 Decl_Node : Node_Id;
11361 Decl_Nodes : List_Id;
11362 Loc : Source_Ptr;
11363 Subt : Entity_Id;
11364
11365 procedure Diagnose_Predicated_Actual;
11366 -- There are a number of constructs in which a discrete type with
11367 -- predicates is illegal, e.g. as an index in an array type declaration.
11368 -- If a generic type is used is such a construct in a generic package
11369 -- declaration, it carries the flag No_Predicate_On_Actual. it is part
11370 -- of the generic contract that the actual cannot have predicates.
11371
11372 procedure Validate_Array_Type_Instance;
11373 procedure Validate_Access_Subprogram_Instance;
11374 procedure Validate_Access_Type_Instance;
11375 procedure Validate_Derived_Type_Instance;
11376 procedure Validate_Derived_Interface_Type_Instance;
11377 procedure Validate_Discriminated_Formal_Type;
11378 procedure Validate_Interface_Type_Instance;
11379 procedure Validate_Private_Type_Instance;
11380 procedure Validate_Incomplete_Type_Instance;
11381 -- These procedures perform validation tests for the named case.
11382 -- Validate_Discriminated_Formal_Type is shared by formal private
11383 -- types and Ada 2012 formal incomplete types.
11384
11385 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean;
11386 -- Check that base types are the same and that the subtypes match
11387 -- statically. Used in several of the above.
11388
11389 ---------------------------------
11390 -- Diagnose_Predicated_Actual --
11391 ---------------------------------
11392
11393 procedure Diagnose_Predicated_Actual is
11394 begin
11395 if No_Predicate_On_Actual (A_Gen_T)
11396 and then Has_Predicates (Act_T)
11397 then
11398 Error_Msg_NE
11399 ("actual for& cannot be a type with predicate",
11400 Instantiation_Node, A_Gen_T);
11401
11402 elsif No_Dynamic_Predicate_On_Actual (A_Gen_T)
11403 and then Has_Predicates (Act_T)
11404 and then not Has_Static_Predicate_Aspect (Act_T)
11405 then
11406 Error_Msg_NE
11407 ("actual for& cannot be a type with a dynamic predicate",
11408 Instantiation_Node, A_Gen_T);
11409 end if;
11410 end Diagnose_Predicated_Actual;
11411
11412 --------------------
11413 -- Subtypes_Match --
11414 --------------------
11415
11416 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean is
11417 T : constant Entity_Id := Get_Instance_Of (Gen_T);
11418
11419 begin
11420 -- Some detailed comments would be useful here ???
11421
11422 return ((Base_Type (T) = Act_T
11423 or else Base_Type (T) = Base_Type (Act_T))
11424 and then Subtypes_Statically_Match (T, Act_T))
11425
11426 or else (Is_Class_Wide_Type (Gen_T)
11427 and then Is_Class_Wide_Type (Act_T)
11428 and then Subtypes_Match
11429 (Get_Instance_Of (Root_Type (Gen_T)),
11430 Root_Type (Act_T)))
11431
11432 or else
11433 (Ekind_In (Gen_T, E_Anonymous_Access_Subprogram_Type,
11434 E_Anonymous_Access_Type)
11435 and then Ekind (Act_T) = Ekind (Gen_T)
11436 and then Subtypes_Statically_Match
11437 (Designated_Type (Gen_T), Designated_Type (Act_T)));
11438 end Subtypes_Match;
11439
11440 -----------------------------------------
11441 -- Validate_Access_Subprogram_Instance --
11442 -----------------------------------------
11443
11444 procedure Validate_Access_Subprogram_Instance is
11445 begin
11446 if not Is_Access_Type (Act_T)
11447 or else Ekind (Designated_Type (Act_T)) /= E_Subprogram_Type
11448 then
11449 Error_Msg_NE
11450 ("expect access type in instantiation of &", Actual, Gen_T);
11451 Abandon_Instantiation (Actual);
11452 end if;
11453
11454 -- According to AI05-288, actuals for access_to_subprograms must be
11455 -- subtype conformant with the generic formal. Previous to AI05-288
11456 -- only mode conformance was required.
11457
11458 -- This is a binding interpretation that applies to previous versions
11459 -- of the language, no need to maintain previous weaker checks.
11460
11461 Check_Subtype_Conformant
11462 (Designated_Type (Act_T),
11463 Designated_Type (A_Gen_T),
11464 Actual,
11465 Get_Inst => True);
11466
11467 if Ekind (Base_Type (Act_T)) = E_Access_Protected_Subprogram_Type then
11468 if Ekind (A_Gen_T) = E_Access_Subprogram_Type then
11469 Error_Msg_NE
11470 ("protected access type not allowed for formal &",
11471 Actual, Gen_T);
11472 end if;
11473
11474 elsif Ekind (A_Gen_T) = E_Access_Protected_Subprogram_Type then
11475 Error_Msg_NE
11476 ("expect protected access type for formal &",
11477 Actual, Gen_T);
11478 end if;
11479
11480 -- If the formal has a specified convention (which in most cases
11481 -- will be StdCall) verify that the actual has the same convention.
11482
11483 if Has_Convention_Pragma (A_Gen_T)
11484 and then Convention (A_Gen_T) /= Convention (Act_T)
11485 then
11486 Error_Msg_Name_1 := Get_Convention_Name (Convention (A_Gen_T));
11487 Error_Msg_NE
11488 ("actual for formal & must have convention %", Actual, Gen_T);
11489 end if;
11490 end Validate_Access_Subprogram_Instance;
11491
11492 -----------------------------------
11493 -- Validate_Access_Type_Instance --
11494 -----------------------------------
11495
11496 procedure Validate_Access_Type_Instance is
11497 Desig_Type : constant Entity_Id :=
11498 Find_Actual_Type (Designated_Type (A_Gen_T), A_Gen_T);
11499 Desig_Act : Entity_Id;
11500
11501 begin
11502 if not Is_Access_Type (Act_T) then
11503 Error_Msg_NE
11504 ("expect access type in instantiation of &", Actual, Gen_T);
11505 Abandon_Instantiation (Actual);
11506 end if;
11507
11508 if Is_Access_Constant (A_Gen_T) then
11509 if not Is_Access_Constant (Act_T) then
11510 Error_Msg_N
11511 ("actual type must be access-to-constant type", Actual);
11512 Abandon_Instantiation (Actual);
11513 end if;
11514 else
11515 if Is_Access_Constant (Act_T) then
11516 Error_Msg_N
11517 ("actual type must be access-to-variable type", Actual);
11518 Abandon_Instantiation (Actual);
11519
11520 elsif Ekind (A_Gen_T) = E_General_Access_Type
11521 and then Ekind (Base_Type (Act_T)) /= E_General_Access_Type
11522 then
11523 Error_Msg_N -- CODEFIX
11524 ("actual must be general access type!", Actual);
11525 Error_Msg_NE -- CODEFIX
11526 ("add ALL to }!", Actual, Act_T);
11527 Abandon_Instantiation (Actual);
11528 end if;
11529 end if;
11530
11531 -- The designated subtypes, that is to say the subtypes introduced
11532 -- by an access type declaration (and not by a subtype declaration)
11533 -- must match.
11534
11535 Desig_Act := Designated_Type (Base_Type (Act_T));
11536
11537 -- The designated type may have been introduced through a limited_
11538 -- with clause, in which case retrieve the non-limited view. This
11539 -- applies to incomplete types as well as to class-wide types.
11540
11541 if From_Limited_With (Desig_Act) then
11542 Desig_Act := Available_View (Desig_Act);
11543 end if;
11544
11545 if not Subtypes_Match (Desig_Type, Desig_Act) then
11546 Error_Msg_NE
11547 ("designated type of actual does not match that of formal &",
11548 Actual, Gen_T);
11549
11550 if not Predicates_Match (Desig_Type, Desig_Act) then
11551 Error_Msg_N ("\predicates do not match", Actual);
11552 end if;
11553
11554 Abandon_Instantiation (Actual);
11555
11556 elsif Is_Access_Type (Designated_Type (Act_T))
11557 and then Is_Constrained (Designated_Type (Designated_Type (Act_T)))
11558 /=
11559 Is_Constrained (Designated_Type (Desig_Type))
11560 then
11561 Error_Msg_NE
11562 ("designated type of actual does not match that of formal &",
11563 Actual, Gen_T);
11564
11565 if not Predicates_Match (Desig_Type, Desig_Act) then
11566 Error_Msg_N ("\predicates do not match", Actual);
11567 end if;
11568
11569 Abandon_Instantiation (Actual);
11570 end if;
11571
11572 -- Ada 2005: null-exclusion indicators of the two types must agree
11573
11574 if Can_Never_Be_Null (A_Gen_T) /= Can_Never_Be_Null (Act_T) then
11575 Error_Msg_NE
11576 ("non null exclusion of actual and formal & do not match",
11577 Actual, Gen_T);
11578 end if;
11579 end Validate_Access_Type_Instance;
11580
11581 ----------------------------------
11582 -- Validate_Array_Type_Instance --
11583 ----------------------------------
11584
11585 procedure Validate_Array_Type_Instance is
11586 I1 : Node_Id;
11587 I2 : Node_Id;
11588 T2 : Entity_Id;
11589
11590 function Formal_Dimensions return Int;
11591 -- Count number of dimensions in array type formal
11592
11593 -----------------------
11594 -- Formal_Dimensions --
11595 -----------------------
11596
11597 function Formal_Dimensions return Int is
11598 Num : Int := 0;
11599 Index : Node_Id;
11600
11601 begin
11602 if Nkind (Def) = N_Constrained_Array_Definition then
11603 Index := First (Discrete_Subtype_Definitions (Def));
11604 else
11605 Index := First (Subtype_Marks (Def));
11606 end if;
11607
11608 while Present (Index) loop
11609 Num := Num + 1;
11610 Next_Index (Index);
11611 end loop;
11612
11613 return Num;
11614 end Formal_Dimensions;
11615
11616 -- Start of processing for Validate_Array_Type_Instance
11617
11618 begin
11619 if not Is_Array_Type (Act_T) then
11620 Error_Msg_NE
11621 ("expect array type in instantiation of &", Actual, Gen_T);
11622 Abandon_Instantiation (Actual);
11623
11624 elsif Nkind (Def) = N_Constrained_Array_Definition then
11625 if not (Is_Constrained (Act_T)) then
11626 Error_Msg_NE
11627 ("expect constrained array in instantiation of &",
11628 Actual, Gen_T);
11629 Abandon_Instantiation (Actual);
11630 end if;
11631
11632 else
11633 if Is_Constrained (Act_T) then
11634 Error_Msg_NE
11635 ("expect unconstrained array in instantiation of &",
11636 Actual, Gen_T);
11637 Abandon_Instantiation (Actual);
11638 end if;
11639 end if;
11640
11641 if Formal_Dimensions /= Number_Dimensions (Act_T) then
11642 Error_Msg_NE
11643 ("dimensions of actual do not match formal &", Actual, Gen_T);
11644 Abandon_Instantiation (Actual);
11645 end if;
11646
11647 I1 := First_Index (A_Gen_T);
11648 I2 := First_Index (Act_T);
11649 for J in 1 .. Formal_Dimensions loop
11650
11651 -- If the indexes of the actual were given by a subtype_mark,
11652 -- the index was transformed into a range attribute. Retrieve
11653 -- the original type mark for checking.
11654
11655 if Is_Entity_Name (Original_Node (I2)) then
11656 T2 := Entity (Original_Node (I2));
11657 else
11658 T2 := Etype (I2);
11659 end if;
11660
11661 if not Subtypes_Match
11662 (Find_Actual_Type (Etype (I1), A_Gen_T), T2)
11663 then
11664 Error_Msg_NE
11665 ("index types of actual do not match those of formal &",
11666 Actual, Gen_T);
11667 Abandon_Instantiation (Actual);
11668 end if;
11669
11670 Next_Index (I1);
11671 Next_Index (I2);
11672 end loop;
11673
11674 -- Check matching subtypes. Note that there are complex visibility
11675 -- issues when the generic is a child unit and some aspect of the
11676 -- generic type is declared in a parent unit of the generic. We do
11677 -- the test to handle this special case only after a direct check
11678 -- for static matching has failed. The case where both the component
11679 -- type and the array type are separate formals, and the component
11680 -- type is a private view may also require special checking in
11681 -- Subtypes_Match.
11682
11683 if Subtypes_Match
11684 (Component_Type (A_Gen_T), Component_Type (Act_T))
11685 or else
11686 Subtypes_Match
11687 (Find_Actual_Type (Component_Type (A_Gen_T), A_Gen_T),
11688 Component_Type (Act_T))
11689 then
11690 null;
11691 else
11692 Error_Msg_NE
11693 ("component subtype of actual does not match that of formal &",
11694 Actual, Gen_T);
11695 Abandon_Instantiation (Actual);
11696 end if;
11697
11698 if Has_Aliased_Components (A_Gen_T)
11699 and then not Has_Aliased_Components (Act_T)
11700 then
11701 Error_Msg_NE
11702 ("actual must have aliased components to match formal type &",
11703 Actual, Gen_T);
11704 end if;
11705 end Validate_Array_Type_Instance;
11706
11707 -----------------------------------------------
11708 -- Validate_Derived_Interface_Type_Instance --
11709 -----------------------------------------------
11710
11711 procedure Validate_Derived_Interface_Type_Instance is
11712 Par : constant Entity_Id := Entity (Subtype_Indication (Def));
11713 Elmt : Elmt_Id;
11714
11715 begin
11716 -- First apply interface instance checks
11717
11718 Validate_Interface_Type_Instance;
11719
11720 -- Verify that immediate parent interface is an ancestor of
11721 -- the actual.
11722
11723 if Present (Par)
11724 and then not Interface_Present_In_Ancestor (Act_T, Par)
11725 then
11726 Error_Msg_NE
11727 ("interface actual must include progenitor&", Actual, Par);
11728 end if;
11729
11730 -- Now verify that the actual includes all other ancestors of
11731 -- the formal.
11732
11733 Elmt := First_Elmt (Interfaces (A_Gen_T));
11734 while Present (Elmt) loop
11735 if not Interface_Present_In_Ancestor
11736 (Act_T, Get_Instance_Of (Node (Elmt)))
11737 then
11738 Error_Msg_NE
11739 ("interface actual must include progenitor&",
11740 Actual, Node (Elmt));
11741 end if;
11742
11743 Next_Elmt (Elmt);
11744 end loop;
11745 end Validate_Derived_Interface_Type_Instance;
11746
11747 ------------------------------------
11748 -- Validate_Derived_Type_Instance --
11749 ------------------------------------
11750
11751 procedure Validate_Derived_Type_Instance is
11752 Actual_Discr : Entity_Id;
11753 Ancestor_Discr : Entity_Id;
11754
11755 begin
11756 -- If the parent type in the generic declaration is itself a previous
11757 -- formal type, then it is local to the generic and absent from the
11758 -- analyzed generic definition. In that case the ancestor is the
11759 -- instance of the formal (which must have been instantiated
11760 -- previously), unless the ancestor is itself a formal derived type.
11761 -- In this latter case (which is the subject of Corrigendum 8652/0038
11762 -- (AI-202) the ancestor of the formals is the ancestor of its
11763 -- parent. Otherwise, the analyzed generic carries the parent type.
11764 -- If the parent type is defined in a previous formal package, then
11765 -- the scope of that formal package is that of the generic type
11766 -- itself, and it has already been mapped into the corresponding type
11767 -- in the actual package.
11768
11769 -- Common case: parent type defined outside of the generic
11770
11771 if Is_Entity_Name (Subtype_Mark (Def))
11772 and then Present (Entity (Subtype_Mark (Def)))
11773 then
11774 Ancestor := Get_Instance_Of (Entity (Subtype_Mark (Def)));
11775
11776 -- Check whether parent is defined in a previous formal package
11777
11778 elsif
11779 Scope (Scope (Base_Type (Etype (A_Gen_T)))) = Scope (A_Gen_T)
11780 then
11781 Ancestor :=
11782 Get_Instance_Of (Base_Type (Etype (A_Gen_T)));
11783
11784 -- The type may be a local derivation, or a type extension of a
11785 -- previous formal, or of a formal of a parent package.
11786
11787 elsif Is_Derived_Type (Get_Instance_Of (A_Gen_T))
11788 or else
11789 Ekind (Get_Instance_Of (A_Gen_T)) = E_Record_Type_With_Private
11790 then
11791 -- Check whether the parent is another derived formal type in the
11792 -- same generic unit.
11793
11794 if Etype (A_Gen_T) /= A_Gen_T
11795 and then Is_Generic_Type (Etype (A_Gen_T))
11796 and then Scope (Etype (A_Gen_T)) = Scope (A_Gen_T)
11797 and then Etype (Etype (A_Gen_T)) /= Etype (A_Gen_T)
11798 then
11799 -- Locate ancestor of parent from the subtype declaration
11800 -- created for the actual.
11801
11802 declare
11803 Decl : Node_Id;
11804
11805 begin
11806 Decl := First (Actual_Decls);
11807 while Present (Decl) loop
11808 if Nkind (Decl) = N_Subtype_Declaration
11809 and then Chars (Defining_Identifier (Decl)) =
11810 Chars (Etype (A_Gen_T))
11811 then
11812 Ancestor := Generic_Parent_Type (Decl);
11813 exit;
11814 else
11815 Next (Decl);
11816 end if;
11817 end loop;
11818 end;
11819
11820 pragma Assert (Present (Ancestor));
11821
11822 -- The ancestor itself may be a previous formal that has been
11823 -- instantiated.
11824
11825 Ancestor := Get_Instance_Of (Ancestor);
11826
11827 else
11828 Ancestor :=
11829 Get_Instance_Of (Base_Type (Get_Instance_Of (A_Gen_T)));
11830 end if;
11831
11832 -- Check whether parent is a previous formal of the current generic
11833
11834 elsif Is_Derived_Type (A_Gen_T)
11835 and then Is_Generic_Type (Etype (A_Gen_T))
11836 and then Scope (A_Gen_T) = Scope (Etype (A_Gen_T))
11837 then
11838 Ancestor := Get_Instance_Of (First_Subtype (Etype (A_Gen_T)));
11839
11840 -- An unusual case: the actual is a type declared in a parent unit,
11841 -- but is not a formal type so there is no instance_of for it.
11842 -- Retrieve it by analyzing the record extension.
11843
11844 elsif Is_Child_Unit (Scope (A_Gen_T))
11845 and then In_Open_Scopes (Scope (Act_T))
11846 and then Is_Generic_Instance (Scope (Act_T))
11847 then
11848 Analyze (Subtype_Mark (Def));
11849 Ancestor := Entity (Subtype_Mark (Def));
11850
11851 else
11852 Ancestor := Get_Instance_Of (Etype (Base_Type (A_Gen_T)));
11853 end if;
11854
11855 -- If the formal derived type has pragma Preelaborable_Initialization
11856 -- then the actual type must have preelaborable initialization.
11857
11858 if Known_To_Have_Preelab_Init (A_Gen_T)
11859 and then not Has_Preelaborable_Initialization (Act_T)
11860 then
11861 Error_Msg_NE
11862 ("actual for & must have preelaborable initialization",
11863 Actual, Gen_T);
11864 end if;
11865
11866 -- Ada 2005 (AI-251)
11867
11868 if Ada_Version >= Ada_2005 and then Is_Interface (Ancestor) then
11869 if not Interface_Present_In_Ancestor (Act_T, Ancestor) then
11870 Error_Msg_NE
11871 ("(Ada 2005) expected type implementing & in instantiation",
11872 Actual, Ancestor);
11873 end if;
11874
11875 -- Finally verify that the (instance of) the ancestor is an ancestor
11876 -- of the actual.
11877
11878 elsif not Is_Ancestor (Base_Type (Ancestor), Act_T) then
11879 Error_Msg_NE
11880 ("expect type derived from & in instantiation",
11881 Actual, First_Subtype (Ancestor));
11882 Abandon_Instantiation (Actual);
11883 end if;
11884
11885 -- Ada 2005 (AI-443): Synchronized formal derived type checks. Note
11886 -- that the formal type declaration has been rewritten as a private
11887 -- extension.
11888
11889 if Ada_Version >= Ada_2005
11890 and then Nkind (Parent (A_Gen_T)) = N_Private_Extension_Declaration
11891 and then Synchronized_Present (Parent (A_Gen_T))
11892 then
11893 -- The actual must be a synchronized tagged type
11894
11895 if not Is_Tagged_Type (Act_T) then
11896 Error_Msg_N
11897 ("actual of synchronized type must be tagged", Actual);
11898 Abandon_Instantiation (Actual);
11899
11900 elsif Nkind (Parent (Act_T)) = N_Full_Type_Declaration
11901 and then Nkind (Type_Definition (Parent (Act_T))) =
11902 N_Derived_Type_Definition
11903 and then not Synchronized_Present
11904 (Type_Definition (Parent (Act_T)))
11905 then
11906 Error_Msg_N
11907 ("actual of synchronized type must be synchronized", Actual);
11908 Abandon_Instantiation (Actual);
11909 end if;
11910 end if;
11911
11912 -- Perform atomic/volatile checks (RM C.6(12)). Note that AI05-0218-1
11913 -- removes the second instance of the phrase "or allow pass by copy".
11914
11915 if Is_Atomic (Act_T) and then not Is_Atomic (Ancestor) then
11916 Error_Msg_N
11917 ("cannot have atomic actual type for non-atomic formal type",
11918 Actual);
11919
11920 elsif Is_Volatile (Act_T) and then not Is_Volatile (Ancestor) then
11921 Error_Msg_N
11922 ("cannot have volatile actual type for non-volatile formal type",
11923 Actual);
11924 end if;
11925
11926 -- It should not be necessary to check for unknown discriminants on
11927 -- Formal, but for some reason Has_Unknown_Discriminants is false for
11928 -- A_Gen_T, so Is_Definite_Subtype incorrectly returns True. This
11929 -- needs fixing. ???
11930
11931 if Is_Definite_Subtype (A_Gen_T)
11932 and then not Unknown_Discriminants_Present (Formal)
11933 and then not Is_Definite_Subtype (Act_T)
11934 then
11935 Error_Msg_N ("actual subtype must be constrained", Actual);
11936 Abandon_Instantiation (Actual);
11937 end if;
11938
11939 if not Unknown_Discriminants_Present (Formal) then
11940 if Is_Constrained (Ancestor) then
11941 if not Is_Constrained (Act_T) then
11942 Error_Msg_N ("actual subtype must be constrained", Actual);
11943 Abandon_Instantiation (Actual);
11944 end if;
11945
11946 -- Ancestor is unconstrained, Check if generic formal and actual
11947 -- agree on constrainedness. The check only applies to array types
11948 -- and discriminated types.
11949
11950 elsif Is_Constrained (Act_T) then
11951 if Ekind (Ancestor) = E_Access_Type
11952 or else (not Is_Constrained (A_Gen_T)
11953 and then Is_Composite_Type (A_Gen_T))
11954 then
11955 Error_Msg_N ("actual subtype must be unconstrained", Actual);
11956 Abandon_Instantiation (Actual);
11957 end if;
11958
11959 -- A class-wide type is only allowed if the formal has unknown
11960 -- discriminants.
11961
11962 elsif Is_Class_Wide_Type (Act_T)
11963 and then not Has_Unknown_Discriminants (Ancestor)
11964 then
11965 Error_Msg_NE
11966 ("actual for & cannot be a class-wide type", Actual, Gen_T);
11967 Abandon_Instantiation (Actual);
11968
11969 -- Otherwise, the formal and actual must have the same number
11970 -- of discriminants and each discriminant of the actual must
11971 -- correspond to a discriminant of the formal.
11972
11973 elsif Has_Discriminants (Act_T)
11974 and then not Has_Unknown_Discriminants (Act_T)
11975 and then Has_Discriminants (Ancestor)
11976 then
11977 Actual_Discr := First_Discriminant (Act_T);
11978 Ancestor_Discr := First_Discriminant (Ancestor);
11979 while Present (Actual_Discr)
11980 and then Present (Ancestor_Discr)
11981 loop
11982 if Base_Type (Act_T) /= Base_Type (Ancestor) and then
11983 No (Corresponding_Discriminant (Actual_Discr))
11984 then
11985 Error_Msg_NE
11986 ("discriminant & does not correspond "
11987 & "to ancestor discriminant", Actual, Actual_Discr);
11988 Abandon_Instantiation (Actual);
11989 end if;
11990
11991 Next_Discriminant (Actual_Discr);
11992 Next_Discriminant (Ancestor_Discr);
11993 end loop;
11994
11995 if Present (Actual_Discr) or else Present (Ancestor_Discr) then
11996 Error_Msg_NE
11997 ("actual for & must have same number of discriminants",
11998 Actual, Gen_T);
11999 Abandon_Instantiation (Actual);
12000 end if;
12001
12002 -- This case should be caught by the earlier check for
12003 -- constrainedness, but the check here is added for completeness.
12004
12005 elsif Has_Discriminants (Act_T)
12006 and then not Has_Unknown_Discriminants (Act_T)
12007 then
12008 Error_Msg_NE
12009 ("actual for & must not have discriminants", Actual, Gen_T);
12010 Abandon_Instantiation (Actual);
12011
12012 elsif Has_Discriminants (Ancestor) then
12013 Error_Msg_NE
12014 ("actual for & must have known discriminants", Actual, Gen_T);
12015 Abandon_Instantiation (Actual);
12016 end if;
12017
12018 if not Subtypes_Statically_Compatible
12019 (Act_T, Ancestor, Formal_Derived_Matching => True)
12020 then
12021 Error_Msg_N
12022 ("constraint on actual is incompatible with formal", Actual);
12023 Abandon_Instantiation (Actual);
12024 end if;
12025 end if;
12026
12027 -- If the formal and actual types are abstract, check that there
12028 -- are no abstract primitives of the actual type that correspond to
12029 -- nonabstract primitives of the formal type (second sentence of
12030 -- RM95 3.9.3(9)).
12031
12032 if Is_Abstract_Type (A_Gen_T) and then Is_Abstract_Type (Act_T) then
12033 Check_Abstract_Primitives : declare
12034 Gen_Prims : constant Elist_Id :=
12035 Primitive_Operations (A_Gen_T);
12036 Gen_Elmt : Elmt_Id;
12037 Gen_Subp : Entity_Id;
12038 Anc_Subp : Entity_Id;
12039 Anc_Formal : Entity_Id;
12040 Anc_F_Type : Entity_Id;
12041
12042 Act_Prims : constant Elist_Id := Primitive_Operations (Act_T);
12043 Act_Elmt : Elmt_Id;
12044 Act_Subp : Entity_Id;
12045 Act_Formal : Entity_Id;
12046 Act_F_Type : Entity_Id;
12047
12048 Subprograms_Correspond : Boolean;
12049
12050 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean;
12051 -- Returns true if T2 is derived directly or indirectly from
12052 -- T1, including derivations from interfaces. T1 and T2 are
12053 -- required to be specific tagged base types.
12054
12055 ------------------------
12056 -- Is_Tagged_Ancestor --
12057 ------------------------
12058
12059 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean
12060 is
12061 Intfc_Elmt : Elmt_Id;
12062
12063 begin
12064 -- The predicate is satisfied if the types are the same
12065
12066 if T1 = T2 then
12067 return True;
12068
12069 -- If we've reached the top of the derivation chain then
12070 -- we know that T1 is not an ancestor of T2.
12071
12072 elsif Etype (T2) = T2 then
12073 return False;
12074
12075 -- Proceed to check T2's immediate parent
12076
12077 elsif Is_Ancestor (T1, Base_Type (Etype (T2))) then
12078 return True;
12079
12080 -- Finally, check to see if T1 is an ancestor of any of T2's
12081 -- progenitors.
12082
12083 else
12084 Intfc_Elmt := First_Elmt (Interfaces (T2));
12085 while Present (Intfc_Elmt) loop
12086 if Is_Ancestor (T1, Node (Intfc_Elmt)) then
12087 return True;
12088 end if;
12089
12090 Next_Elmt (Intfc_Elmt);
12091 end loop;
12092 end if;
12093
12094 return False;
12095 end Is_Tagged_Ancestor;
12096
12097 -- Start of processing for Check_Abstract_Primitives
12098
12099 begin
12100 -- Loop over all of the formal derived type's primitives
12101
12102 Gen_Elmt := First_Elmt (Gen_Prims);
12103 while Present (Gen_Elmt) loop
12104 Gen_Subp := Node (Gen_Elmt);
12105
12106 -- If the primitive of the formal is not abstract, then
12107 -- determine whether there is a corresponding primitive of
12108 -- the actual type that's abstract.
12109
12110 if not Is_Abstract_Subprogram (Gen_Subp) then
12111 Act_Elmt := First_Elmt (Act_Prims);
12112 while Present (Act_Elmt) loop
12113 Act_Subp := Node (Act_Elmt);
12114
12115 -- If we find an abstract primitive of the actual,
12116 -- then we need to test whether it corresponds to the
12117 -- subprogram from which the generic formal primitive
12118 -- is inherited.
12119
12120 if Is_Abstract_Subprogram (Act_Subp) then
12121 Anc_Subp := Alias (Gen_Subp);
12122
12123 -- Test whether we have a corresponding primitive
12124 -- by comparing names, kinds, formal types, and
12125 -- result types.
12126
12127 if Chars (Anc_Subp) = Chars (Act_Subp)
12128 and then Ekind (Anc_Subp) = Ekind (Act_Subp)
12129 then
12130 Anc_Formal := First_Formal (Anc_Subp);
12131 Act_Formal := First_Formal (Act_Subp);
12132 while Present (Anc_Formal)
12133 and then Present (Act_Formal)
12134 loop
12135 Anc_F_Type := Etype (Anc_Formal);
12136 Act_F_Type := Etype (Act_Formal);
12137
12138 if Ekind (Anc_F_Type) =
12139 E_Anonymous_Access_Type
12140 then
12141 Anc_F_Type := Designated_Type (Anc_F_Type);
12142
12143 if Ekind (Act_F_Type) =
12144 E_Anonymous_Access_Type
12145 then
12146 Act_F_Type :=
12147 Designated_Type (Act_F_Type);
12148 else
12149 exit;
12150 end if;
12151
12152 elsif
12153 Ekind (Act_F_Type) = E_Anonymous_Access_Type
12154 then
12155 exit;
12156 end if;
12157
12158 Anc_F_Type := Base_Type (Anc_F_Type);
12159 Act_F_Type := Base_Type (Act_F_Type);
12160
12161 -- If the formal is controlling, then the
12162 -- the type of the actual primitive's formal
12163 -- must be derived directly or indirectly
12164 -- from the type of the ancestor primitive's
12165 -- formal.
12166
12167 if Is_Controlling_Formal (Anc_Formal) then
12168 if not Is_Tagged_Ancestor
12169 (Anc_F_Type, Act_F_Type)
12170 then
12171 exit;
12172 end if;
12173
12174 -- Otherwise the types of the formals must
12175 -- be the same.
12176
12177 elsif Anc_F_Type /= Act_F_Type then
12178 exit;
12179 end if;
12180
12181 Next_Entity (Anc_Formal);
12182 Next_Entity (Act_Formal);
12183 end loop;
12184
12185 -- If we traversed through all of the formals
12186 -- then so far the subprograms correspond, so
12187 -- now check that any result types correspond.
12188
12189 if No (Anc_Formal) and then No (Act_Formal) then
12190 Subprograms_Correspond := True;
12191
12192 if Ekind (Act_Subp) = E_Function then
12193 Anc_F_Type := Etype (Anc_Subp);
12194 Act_F_Type := Etype (Act_Subp);
12195
12196 if Ekind (Anc_F_Type) =
12197 E_Anonymous_Access_Type
12198 then
12199 Anc_F_Type :=
12200 Designated_Type (Anc_F_Type);
12201
12202 if Ekind (Act_F_Type) =
12203 E_Anonymous_Access_Type
12204 then
12205 Act_F_Type :=
12206 Designated_Type (Act_F_Type);
12207 else
12208 Subprograms_Correspond := False;
12209 end if;
12210
12211 elsif
12212 Ekind (Act_F_Type)
12213 = E_Anonymous_Access_Type
12214 then
12215 Subprograms_Correspond := False;
12216 end if;
12217
12218 Anc_F_Type := Base_Type (Anc_F_Type);
12219 Act_F_Type := Base_Type (Act_F_Type);
12220
12221 -- Now either the result types must be
12222 -- the same or, if the result type is
12223 -- controlling, the result type of the
12224 -- actual primitive must descend from the
12225 -- result type of the ancestor primitive.
12226
12227 if Subprograms_Correspond
12228 and then Anc_F_Type /= Act_F_Type
12229 and then
12230 Has_Controlling_Result (Anc_Subp)
12231 and then not Is_Tagged_Ancestor
12232 (Anc_F_Type, Act_F_Type)
12233 then
12234 Subprograms_Correspond := False;
12235 end if;
12236 end if;
12237
12238 -- Found a matching subprogram belonging to
12239 -- formal ancestor type, so actual subprogram
12240 -- corresponds and this violates 3.9.3(9).
12241
12242 if Subprograms_Correspond then
12243 Error_Msg_NE
12244 ("abstract subprogram & overrides "
12245 & "nonabstract subprogram of ancestor",
12246 Actual, Act_Subp);
12247 end if;
12248 end if;
12249 end if;
12250 end if;
12251
12252 Next_Elmt (Act_Elmt);
12253 end loop;
12254 end if;
12255
12256 Next_Elmt (Gen_Elmt);
12257 end loop;
12258 end Check_Abstract_Primitives;
12259 end if;
12260
12261 -- Verify that limitedness matches. If parent is a limited
12262 -- interface then the generic formal is not unless declared
12263 -- explicitly so. If not declared limited, the actual cannot be
12264 -- limited (see AI05-0087).
12265
12266 -- Even though this AI is a binding interpretation, we enable the
12267 -- check only in Ada 2012 mode, because this improper construct
12268 -- shows up in user code and in existing B-tests.
12269
12270 if Is_Limited_Type (Act_T)
12271 and then not Is_Limited_Type (A_Gen_T)
12272 and then Ada_Version >= Ada_2012
12273 then
12274 if In_Instance then
12275 null;
12276 else
12277 Error_Msg_NE
12278 ("actual for non-limited & cannot be a limited type",
12279 Actual, Gen_T);
12280 Explain_Limited_Type (Act_T, Actual);
12281 Abandon_Instantiation (Actual);
12282 end if;
12283 end if;
12284 end Validate_Derived_Type_Instance;
12285
12286 ----------------------------------------
12287 -- Validate_Discriminated_Formal_Type --
12288 ----------------------------------------
12289
12290 procedure Validate_Discriminated_Formal_Type is
12291 Formal_Discr : Entity_Id;
12292 Actual_Discr : Entity_Id;
12293 Formal_Subt : Entity_Id;
12294
12295 begin
12296 if Has_Discriminants (A_Gen_T) then
12297 if not Has_Discriminants (Act_T) then
12298 Error_Msg_NE
12299 ("actual for & must have discriminants", Actual, Gen_T);
12300 Abandon_Instantiation (Actual);
12301
12302 elsif Is_Constrained (Act_T) then
12303 Error_Msg_NE
12304 ("actual for & must be unconstrained", Actual, Gen_T);
12305 Abandon_Instantiation (Actual);
12306
12307 else
12308 Formal_Discr := First_Discriminant (A_Gen_T);
12309 Actual_Discr := First_Discriminant (Act_T);
12310 while Formal_Discr /= Empty loop
12311 if Actual_Discr = Empty then
12312 Error_Msg_NE
12313 ("discriminants on actual do not match formal",
12314 Actual, Gen_T);
12315 Abandon_Instantiation (Actual);
12316 end if;
12317
12318 Formal_Subt := Get_Instance_Of (Etype (Formal_Discr));
12319
12320 -- Access discriminants match if designated types do
12321
12322 if Ekind (Base_Type (Formal_Subt)) = E_Anonymous_Access_Type
12323 and then (Ekind (Base_Type (Etype (Actual_Discr)))) =
12324 E_Anonymous_Access_Type
12325 and then
12326 Get_Instance_Of
12327 (Designated_Type (Base_Type (Formal_Subt))) =
12328 Designated_Type (Base_Type (Etype (Actual_Discr)))
12329 then
12330 null;
12331
12332 elsif Base_Type (Formal_Subt) /=
12333 Base_Type (Etype (Actual_Discr))
12334 then
12335 Error_Msg_NE
12336 ("types of actual discriminants must match formal",
12337 Actual, Gen_T);
12338 Abandon_Instantiation (Actual);
12339
12340 elsif not Subtypes_Statically_Match
12341 (Formal_Subt, Etype (Actual_Discr))
12342 and then Ada_Version >= Ada_95
12343 then
12344 Error_Msg_NE
12345 ("subtypes of actual discriminants must match formal",
12346 Actual, Gen_T);
12347 Abandon_Instantiation (Actual);
12348 end if;
12349
12350 Next_Discriminant (Formal_Discr);
12351 Next_Discriminant (Actual_Discr);
12352 end loop;
12353
12354 if Actual_Discr /= Empty then
12355 Error_Msg_NE
12356 ("discriminants on actual do not match formal",
12357 Actual, Gen_T);
12358 Abandon_Instantiation (Actual);
12359 end if;
12360 end if;
12361 end if;
12362 end Validate_Discriminated_Formal_Type;
12363
12364 ---------------------------------------
12365 -- Validate_Incomplete_Type_Instance --
12366 ---------------------------------------
12367
12368 procedure Validate_Incomplete_Type_Instance is
12369 begin
12370 if not Is_Tagged_Type (Act_T)
12371 and then Is_Tagged_Type (A_Gen_T)
12372 then
12373 Error_Msg_NE
12374 ("actual for & must be a tagged type", Actual, Gen_T);
12375 end if;
12376
12377 Validate_Discriminated_Formal_Type;
12378 end Validate_Incomplete_Type_Instance;
12379
12380 --------------------------------------
12381 -- Validate_Interface_Type_Instance --
12382 --------------------------------------
12383
12384 procedure Validate_Interface_Type_Instance is
12385 begin
12386 if not Is_Interface (Act_T) then
12387 Error_Msg_NE
12388 ("actual for formal interface type must be an interface",
12389 Actual, Gen_T);
12390
12391 elsif Is_Limited_Type (Act_T) /= Is_Limited_Type (A_Gen_T)
12392 or else Is_Task_Interface (A_Gen_T) /= Is_Task_Interface (Act_T)
12393 or else Is_Protected_Interface (A_Gen_T) /=
12394 Is_Protected_Interface (Act_T)
12395 or else Is_Synchronized_Interface (A_Gen_T) /=
12396 Is_Synchronized_Interface (Act_T)
12397 then
12398 Error_Msg_NE
12399 ("actual for interface& does not match (RM 12.5.5(4))",
12400 Actual, Gen_T);
12401 end if;
12402 end Validate_Interface_Type_Instance;
12403
12404 ------------------------------------
12405 -- Validate_Private_Type_Instance --
12406 ------------------------------------
12407
12408 procedure Validate_Private_Type_Instance is
12409 begin
12410 if Is_Limited_Type (Act_T)
12411 and then not Is_Limited_Type (A_Gen_T)
12412 then
12413 if In_Instance then
12414 null;
12415 else
12416 Error_Msg_NE
12417 ("actual for non-limited & cannot be a limited type", Actual,
12418 Gen_T);
12419 Explain_Limited_Type (Act_T, Actual);
12420 Abandon_Instantiation (Actual);
12421 end if;
12422
12423 elsif Known_To_Have_Preelab_Init (A_Gen_T)
12424 and then not Has_Preelaborable_Initialization (Act_T)
12425 then
12426 Error_Msg_NE
12427 ("actual for & must have preelaborable initialization", Actual,
12428 Gen_T);
12429
12430 elsif not Is_Definite_Subtype (Act_T)
12431 and then Is_Definite_Subtype (A_Gen_T)
12432 and then Ada_Version >= Ada_95
12433 then
12434 Error_Msg_NE
12435 ("actual for & must be a definite subtype", Actual, Gen_T);
12436
12437 elsif not Is_Tagged_Type (Act_T)
12438 and then Is_Tagged_Type (A_Gen_T)
12439 then
12440 Error_Msg_NE
12441 ("actual for & must be a tagged type", Actual, Gen_T);
12442 end if;
12443
12444 Validate_Discriminated_Formal_Type;
12445 Ancestor := Gen_T;
12446 end Validate_Private_Type_Instance;
12447
12448 -- Start of processing for Instantiate_Type
12449
12450 begin
12451 if Get_Instance_Of (A_Gen_T) /= A_Gen_T then
12452 Error_Msg_N ("duplicate instantiation of generic type", Actual);
12453 return New_List (Error);
12454
12455 elsif not Is_Entity_Name (Actual)
12456 or else not Is_Type (Entity (Actual))
12457 then
12458 Error_Msg_NE
12459 ("expect valid subtype mark to instantiate &", Actual, Gen_T);
12460 Abandon_Instantiation (Actual);
12461
12462 else
12463 Act_T := Entity (Actual);
12464
12465 -- Ada 2005 (AI-216): An Unchecked_Union subtype shall only be passed
12466 -- as a generic actual parameter if the corresponding formal type
12467 -- does not have a known_discriminant_part, or is a formal derived
12468 -- type that is an Unchecked_Union type.
12469
12470 if Is_Unchecked_Union (Base_Type (Act_T)) then
12471 if not Has_Discriminants (A_Gen_T)
12472 or else (Is_Derived_Type (A_Gen_T)
12473 and then Is_Unchecked_Union (A_Gen_T))
12474 then
12475 null;
12476 else
12477 Error_Msg_N ("unchecked union cannot be the actual for a "
12478 & "discriminated formal type", Act_T);
12479
12480 end if;
12481 end if;
12482
12483 -- Deal with fixed/floating restrictions
12484
12485 if Is_Floating_Point_Type (Act_T) then
12486 Check_Restriction (No_Floating_Point, Actual);
12487 elsif Is_Fixed_Point_Type (Act_T) then
12488 Check_Restriction (No_Fixed_Point, Actual);
12489 end if;
12490
12491 -- Deal with error of using incomplete type as generic actual.
12492 -- This includes limited views of a type, even if the non-limited
12493 -- view may be available.
12494
12495 if Ekind (Act_T) = E_Incomplete_Type
12496 or else (Is_Class_Wide_Type (Act_T)
12497 and then Ekind (Root_Type (Act_T)) = E_Incomplete_Type)
12498 then
12499 -- If the formal is an incomplete type, the actual can be
12500 -- incomplete as well.
12501
12502 if Ekind (A_Gen_T) = E_Incomplete_Type then
12503 null;
12504
12505 elsif Is_Class_Wide_Type (Act_T)
12506 or else No (Full_View (Act_T))
12507 then
12508 Error_Msg_N ("premature use of incomplete type", Actual);
12509 Abandon_Instantiation (Actual);
12510 else
12511 Act_T := Full_View (Act_T);
12512 Set_Entity (Actual, Act_T);
12513
12514 if Has_Private_Component (Act_T) then
12515 Error_Msg_N
12516 ("premature use of type with private component", Actual);
12517 end if;
12518 end if;
12519
12520 -- Deal with error of premature use of private type as generic actual
12521
12522 elsif Is_Private_Type (Act_T)
12523 and then Is_Private_Type (Base_Type (Act_T))
12524 and then not Is_Generic_Type (Act_T)
12525 and then not Is_Derived_Type (Act_T)
12526 and then No (Full_View (Root_Type (Act_T)))
12527 then
12528 -- If the formal is an incomplete type, the actual can be
12529 -- private or incomplete as well.
12530
12531 if Ekind (A_Gen_T) = E_Incomplete_Type then
12532 null;
12533 else
12534 Error_Msg_N ("premature use of private type", Actual);
12535 end if;
12536
12537 elsif Has_Private_Component (Act_T) then
12538 Error_Msg_N
12539 ("premature use of type with private component", Actual);
12540 end if;
12541
12542 Set_Instance_Of (A_Gen_T, Act_T);
12543
12544 -- If the type is generic, the class-wide type may also be used
12545
12546 if Is_Tagged_Type (A_Gen_T)
12547 and then Is_Tagged_Type (Act_T)
12548 and then not Is_Class_Wide_Type (A_Gen_T)
12549 then
12550 Set_Instance_Of (Class_Wide_Type (A_Gen_T),
12551 Class_Wide_Type (Act_T));
12552 end if;
12553
12554 if not Is_Abstract_Type (A_Gen_T)
12555 and then Is_Abstract_Type (Act_T)
12556 then
12557 Error_Msg_N
12558 ("actual of non-abstract formal cannot be abstract", Actual);
12559 end if;
12560
12561 -- A generic scalar type is a first subtype for which we generate
12562 -- an anonymous base type. Indicate that the instance of this base
12563 -- is the base type of the actual.
12564
12565 if Is_Scalar_Type (A_Gen_T) then
12566 Set_Instance_Of (Etype (A_Gen_T), Etype (Act_T));
12567 end if;
12568 end if;
12569
12570 if Error_Posted (Act_T) then
12571 null;
12572 else
12573 case Nkind (Def) is
12574 when N_Formal_Private_Type_Definition =>
12575 Validate_Private_Type_Instance;
12576
12577 when N_Formal_Incomplete_Type_Definition =>
12578 Validate_Incomplete_Type_Instance;
12579
12580 when N_Formal_Derived_Type_Definition =>
12581 Validate_Derived_Type_Instance;
12582
12583 when N_Formal_Discrete_Type_Definition =>
12584 if not Is_Discrete_Type (Act_T) then
12585 Error_Msg_NE
12586 ("expect discrete type in instantiation of&",
12587 Actual, Gen_T);
12588 Abandon_Instantiation (Actual);
12589 end if;
12590
12591 Diagnose_Predicated_Actual;
12592
12593 when N_Formal_Signed_Integer_Type_Definition =>
12594 if not Is_Signed_Integer_Type (Act_T) then
12595 Error_Msg_NE
12596 ("expect signed integer type in instantiation of&",
12597 Actual, Gen_T);
12598 Abandon_Instantiation (Actual);
12599 end if;
12600
12601 Diagnose_Predicated_Actual;
12602
12603 when N_Formal_Modular_Type_Definition =>
12604 if not Is_Modular_Integer_Type (Act_T) then
12605 Error_Msg_NE
12606 ("expect modular type in instantiation of &",
12607 Actual, Gen_T);
12608 Abandon_Instantiation (Actual);
12609 end if;
12610
12611 Diagnose_Predicated_Actual;
12612
12613 when N_Formal_Floating_Point_Definition =>
12614 if not Is_Floating_Point_Type (Act_T) then
12615 Error_Msg_NE
12616 ("expect float type in instantiation of &", Actual, Gen_T);
12617 Abandon_Instantiation (Actual);
12618 end if;
12619
12620 when N_Formal_Ordinary_Fixed_Point_Definition =>
12621 if not Is_Ordinary_Fixed_Point_Type (Act_T) then
12622 Error_Msg_NE
12623 ("expect ordinary fixed point type in instantiation of &",
12624 Actual, Gen_T);
12625 Abandon_Instantiation (Actual);
12626 end if;
12627
12628 when N_Formal_Decimal_Fixed_Point_Definition =>
12629 if not Is_Decimal_Fixed_Point_Type (Act_T) then
12630 Error_Msg_NE
12631 ("expect decimal type in instantiation of &",
12632 Actual, Gen_T);
12633 Abandon_Instantiation (Actual);
12634 end if;
12635
12636 when N_Array_Type_Definition =>
12637 Validate_Array_Type_Instance;
12638
12639 when N_Access_To_Object_Definition =>
12640 Validate_Access_Type_Instance;
12641
12642 when N_Access_Function_Definition |
12643 N_Access_Procedure_Definition =>
12644 Validate_Access_Subprogram_Instance;
12645
12646 when N_Record_Definition =>
12647 Validate_Interface_Type_Instance;
12648
12649 when N_Derived_Type_Definition =>
12650 Validate_Derived_Interface_Type_Instance;
12651
12652 when others =>
12653 raise Program_Error;
12654
12655 end case;
12656 end if;
12657
12658 Subt := New_Copy (Gen_T);
12659
12660 -- Use adjusted sloc of subtype name as the location for other nodes in
12661 -- the subtype declaration.
12662
12663 Loc := Sloc (Subt);
12664
12665 Decl_Node :=
12666 Make_Subtype_Declaration (Loc,
12667 Defining_Identifier => Subt,
12668 Subtype_Indication => New_Occurrence_Of (Act_T, Loc));
12669
12670 if Is_Private_Type (Act_T) then
12671 Set_Has_Private_View (Subtype_Indication (Decl_Node));
12672
12673 elsif Is_Access_Type (Act_T)
12674 and then Is_Private_Type (Designated_Type (Act_T))
12675 then
12676 Set_Has_Private_View (Subtype_Indication (Decl_Node));
12677 end if;
12678
12679 -- In Ada 2012 the actual may be a limited view. Indicate that
12680 -- the local subtype must be treated as such.
12681
12682 if From_Limited_With (Act_T) then
12683 Set_Ekind (Subt, E_Incomplete_Subtype);
12684 Set_From_Limited_With (Subt);
12685 end if;
12686
12687 Decl_Nodes := New_List (Decl_Node);
12688
12689 -- Flag actual derived types so their elaboration produces the
12690 -- appropriate renamings for the primitive operations of the ancestor.
12691 -- Flag actual for formal private types as well, to determine whether
12692 -- operations in the private part may override inherited operations.
12693 -- If the formal has an interface list, the ancestor is not the
12694 -- parent, but the analyzed formal that includes the interface
12695 -- operations of all its progenitors.
12696
12697 -- Same treatment for formal private types, so we can check whether the
12698 -- type is tagged limited when validating derivations in the private
12699 -- part. (See AI05-096).
12700
12701 if Nkind (Def) = N_Formal_Derived_Type_Definition then
12702 if Present (Interface_List (Def)) then
12703 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
12704 else
12705 Set_Generic_Parent_Type (Decl_Node, Ancestor);
12706 end if;
12707
12708 elsif Nkind_In (Def, N_Formal_Private_Type_Definition,
12709 N_Formal_Incomplete_Type_Definition)
12710 then
12711 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
12712 end if;
12713
12714 -- If the actual is a synchronized type that implements an interface,
12715 -- the primitive operations are attached to the corresponding record,
12716 -- and we have to treat it as an additional generic actual, so that its
12717 -- primitive operations become visible in the instance. The task or
12718 -- protected type itself does not carry primitive operations.
12719
12720 if Is_Concurrent_Type (Act_T)
12721 and then Is_Tagged_Type (Act_T)
12722 and then Present (Corresponding_Record_Type (Act_T))
12723 and then Present (Ancestor)
12724 and then Is_Interface (Ancestor)
12725 then
12726 declare
12727 Corr_Rec : constant Entity_Id :=
12728 Corresponding_Record_Type (Act_T);
12729 New_Corr : Entity_Id;
12730 Corr_Decl : Node_Id;
12731
12732 begin
12733 New_Corr := Make_Temporary (Loc, 'S');
12734 Corr_Decl :=
12735 Make_Subtype_Declaration (Loc,
12736 Defining_Identifier => New_Corr,
12737 Subtype_Indication =>
12738 New_Occurrence_Of (Corr_Rec, Loc));
12739 Append_To (Decl_Nodes, Corr_Decl);
12740
12741 if Ekind (Act_T) = E_Task_Type then
12742 Set_Ekind (Subt, E_Task_Subtype);
12743 else
12744 Set_Ekind (Subt, E_Protected_Subtype);
12745 end if;
12746
12747 Set_Corresponding_Record_Type (Subt, Corr_Rec);
12748 Set_Generic_Parent_Type (Corr_Decl, Ancestor);
12749 Set_Generic_Parent_Type (Decl_Node, Empty);
12750 end;
12751 end if;
12752
12753 -- For a floating-point type, capture dimension info if any, because
12754 -- the generated subtype declaration does not come from source and
12755 -- will not process dimensions.
12756
12757 if Is_Floating_Point_Type (Act_T) then
12758 Copy_Dimensions (Act_T, Subt);
12759 end if;
12760
12761 return Decl_Nodes;
12762 end Instantiate_Type;
12763
12764 ---------------------
12765 -- Is_In_Main_Unit --
12766 ---------------------
12767
12768 function Is_In_Main_Unit (N : Node_Id) return Boolean is
12769 Unum : constant Unit_Number_Type := Get_Source_Unit (N);
12770 Current_Unit : Node_Id;
12771
12772 begin
12773 if Unum = Main_Unit then
12774 return True;
12775
12776 -- If the current unit is a subunit then it is either the main unit or
12777 -- is being compiled as part of the main unit.
12778
12779 elsif Nkind (N) = N_Compilation_Unit then
12780 return Nkind (Unit (N)) = N_Subunit;
12781 end if;
12782
12783 Current_Unit := Parent (N);
12784 while Present (Current_Unit)
12785 and then Nkind (Current_Unit) /= N_Compilation_Unit
12786 loop
12787 Current_Unit := Parent (Current_Unit);
12788 end loop;
12789
12790 -- The instantiation node is in the main unit, or else the current node
12791 -- (perhaps as the result of nested instantiations) is in the main unit,
12792 -- or in the declaration of the main unit, which in this last case must
12793 -- be a body.
12794
12795 return Unum = Main_Unit
12796 or else Current_Unit = Cunit (Main_Unit)
12797 or else Current_Unit = Library_Unit (Cunit (Main_Unit))
12798 or else (Present (Library_Unit (Current_Unit))
12799 and then Is_In_Main_Unit (Library_Unit (Current_Unit)));
12800 end Is_In_Main_Unit;
12801
12802 ----------------------------
12803 -- Load_Parent_Of_Generic --
12804 ----------------------------
12805
12806 procedure Load_Parent_Of_Generic
12807 (N : Node_Id;
12808 Spec : Node_Id;
12809 Body_Optional : Boolean := False)
12810 is
12811 Comp_Unit : constant Node_Id := Cunit (Get_Source_Unit (Spec));
12812 Saved_Style_Check : constant Boolean := Style_Check;
12813 Saved_Warnings : constant Warning_Record := Save_Warnings;
12814 True_Parent : Node_Id;
12815 Inst_Node : Node_Id;
12816 OK : Boolean;
12817 Previous_Instances : constant Elist_Id := New_Elmt_List;
12818
12819 procedure Collect_Previous_Instances (Decls : List_Id);
12820 -- Collect all instantiations in the given list of declarations, that
12821 -- precede the generic that we need to load. If the bodies of these
12822 -- instantiations are available, we must analyze them, to ensure that
12823 -- the public symbols generated are the same when the unit is compiled
12824 -- to generate code, and when it is compiled in the context of a unit
12825 -- that needs a particular nested instance. This process is applied to
12826 -- both package and subprogram instances.
12827
12828 --------------------------------
12829 -- Collect_Previous_Instances --
12830 --------------------------------
12831
12832 procedure Collect_Previous_Instances (Decls : List_Id) is
12833 Decl : Node_Id;
12834
12835 begin
12836 Decl := First (Decls);
12837 while Present (Decl) loop
12838 if Sloc (Decl) >= Sloc (Inst_Node) then
12839 return;
12840
12841 -- If Decl is an instantiation, then record it as requiring
12842 -- instantiation of the corresponding body, except if it is an
12843 -- abbreviated instantiation generated internally for conformance
12844 -- checking purposes only for the case of a formal package
12845 -- declared without a box (see Instantiate_Formal_Package). Such
12846 -- an instantiation does not generate any code (the actual code
12847 -- comes from actual) and thus does not need to be analyzed here.
12848 -- If the instantiation appears with a generic package body it is
12849 -- not analyzed here either.
12850
12851 elsif Nkind (Decl) = N_Package_Instantiation
12852 and then not Is_Internal (Defining_Entity (Decl))
12853 then
12854 Append_Elmt (Decl, Previous_Instances);
12855
12856 -- For a subprogram instantiation, omit instantiations intrinsic
12857 -- operations (Unchecked_Conversions, etc.) that have no bodies.
12858
12859 elsif Nkind_In (Decl, N_Function_Instantiation,
12860 N_Procedure_Instantiation)
12861 and then not Is_Intrinsic_Subprogram (Entity (Name (Decl)))
12862 then
12863 Append_Elmt (Decl, Previous_Instances);
12864
12865 elsif Nkind (Decl) = N_Package_Declaration then
12866 Collect_Previous_Instances
12867 (Visible_Declarations (Specification (Decl)));
12868 Collect_Previous_Instances
12869 (Private_Declarations (Specification (Decl)));
12870
12871 -- Previous non-generic bodies may contain instances as well
12872
12873 elsif Nkind (Decl) = N_Package_Body
12874 and then Ekind (Corresponding_Spec (Decl)) /= E_Generic_Package
12875 then
12876 Collect_Previous_Instances (Declarations (Decl));
12877
12878 elsif Nkind (Decl) = N_Subprogram_Body
12879 and then not Acts_As_Spec (Decl)
12880 and then not Is_Generic_Subprogram (Corresponding_Spec (Decl))
12881 then
12882 Collect_Previous_Instances (Declarations (Decl));
12883 end if;
12884
12885 Next (Decl);
12886 end loop;
12887 end Collect_Previous_Instances;
12888
12889 -- Start of processing for Load_Parent_Of_Generic
12890
12891 begin
12892 if not In_Same_Source_Unit (N, Spec)
12893 or else Nkind (Unit (Comp_Unit)) = N_Package_Declaration
12894 or else (Nkind (Unit (Comp_Unit)) = N_Package_Body
12895 and then not Is_In_Main_Unit (Spec))
12896 then
12897 -- Find body of parent of spec, and analyze it. A special case arises
12898 -- when the parent is an instantiation, that is to say when we are
12899 -- currently instantiating a nested generic. In that case, there is
12900 -- no separate file for the body of the enclosing instance. Instead,
12901 -- the enclosing body must be instantiated as if it were a pending
12902 -- instantiation, in order to produce the body for the nested generic
12903 -- we require now. Note that in that case the generic may be defined
12904 -- in a package body, the instance defined in the same package body,
12905 -- and the original enclosing body may not be in the main unit.
12906
12907 Inst_Node := Empty;
12908
12909 True_Parent := Parent (Spec);
12910 while Present (True_Parent)
12911 and then Nkind (True_Parent) /= N_Compilation_Unit
12912 loop
12913 if Nkind (True_Parent) = N_Package_Declaration
12914 and then
12915 Nkind (Original_Node (True_Parent)) = N_Package_Instantiation
12916 then
12917 -- Parent is a compilation unit that is an instantiation.
12918 -- Instantiation node has been replaced with package decl.
12919
12920 Inst_Node := Original_Node (True_Parent);
12921 exit;
12922
12923 elsif Nkind (True_Parent) = N_Package_Declaration
12924 and then Present (Generic_Parent (Specification (True_Parent)))
12925 and then Nkind (Parent (True_Parent)) /= N_Compilation_Unit
12926 then
12927 -- Parent is an instantiation within another specification.
12928 -- Declaration for instance has been inserted before original
12929 -- instantiation node. A direct link would be preferable?
12930
12931 Inst_Node := Next (True_Parent);
12932 while Present (Inst_Node)
12933 and then Nkind (Inst_Node) /= N_Package_Instantiation
12934 loop
12935 Next (Inst_Node);
12936 end loop;
12937
12938 -- If the instance appears within a generic, and the generic
12939 -- unit is defined within a formal package of the enclosing
12940 -- generic, there is no generic body available, and none
12941 -- needed. A more precise test should be used ???
12942
12943 if No (Inst_Node) then
12944 return;
12945 end if;
12946
12947 exit;
12948
12949 else
12950 True_Parent := Parent (True_Parent);
12951 end if;
12952 end loop;
12953
12954 -- Case where we are currently instantiating a nested generic
12955
12956 if Present (Inst_Node) then
12957 if Nkind (Parent (True_Parent)) = N_Compilation_Unit then
12958
12959 -- Instantiation node and declaration of instantiated package
12960 -- were exchanged when only the declaration was needed.
12961 -- Restore instantiation node before proceeding with body.
12962
12963 Set_Unit (Parent (True_Parent), Inst_Node);
12964 end if;
12965
12966 -- Now complete instantiation of enclosing body, if it appears in
12967 -- some other unit. If it appears in the current unit, the body
12968 -- will have been instantiated already.
12969
12970 if No (Corresponding_Body (Instance_Spec (Inst_Node))) then
12971
12972 -- We need to determine the expander mode to instantiate the
12973 -- enclosing body. Because the generic body we need may use
12974 -- global entities declared in the enclosing package (including
12975 -- aggregates) it is in general necessary to compile this body
12976 -- with expansion enabled, except if we are within a generic
12977 -- package, in which case the usual generic rule applies.
12978
12979 declare
12980 Exp_Status : Boolean := True;
12981 Scop : Entity_Id;
12982
12983 begin
12984 -- Loop through scopes looking for generic package
12985
12986 Scop := Scope (Defining_Entity (Instance_Spec (Inst_Node)));
12987 while Present (Scop)
12988 and then Scop /= Standard_Standard
12989 loop
12990 if Ekind (Scop) = E_Generic_Package then
12991 Exp_Status := False;
12992 exit;
12993 end if;
12994
12995 Scop := Scope (Scop);
12996 end loop;
12997
12998 -- Collect previous instantiations in the unit that contains
12999 -- the desired generic.
13000
13001 if Nkind (Parent (True_Parent)) /= N_Compilation_Unit
13002 and then not Body_Optional
13003 then
13004 declare
13005 Decl : Elmt_Id;
13006 Info : Pending_Body_Info;
13007 Par : Node_Id;
13008
13009 begin
13010 Par := Parent (Inst_Node);
13011 while Present (Par) loop
13012 exit when Nkind (Parent (Par)) = N_Compilation_Unit;
13013 Par := Parent (Par);
13014 end loop;
13015
13016 pragma Assert (Present (Par));
13017
13018 if Nkind (Par) = N_Package_Body then
13019 Collect_Previous_Instances (Declarations (Par));
13020
13021 elsif Nkind (Par) = N_Package_Declaration then
13022 Collect_Previous_Instances
13023 (Visible_Declarations (Specification (Par)));
13024 Collect_Previous_Instances
13025 (Private_Declarations (Specification (Par)));
13026
13027 else
13028 -- Enclosing unit is a subprogram body. In this
13029 -- case all instance bodies are processed in order
13030 -- and there is no need to collect them separately.
13031
13032 null;
13033 end if;
13034
13035 Decl := First_Elmt (Previous_Instances);
13036 while Present (Decl) loop
13037 Info :=
13038 (Inst_Node => Node (Decl),
13039 Act_Decl =>
13040 Instance_Spec (Node (Decl)),
13041 Expander_Status => Exp_Status,
13042 Current_Sem_Unit =>
13043 Get_Code_Unit (Sloc (Node (Decl))),
13044 Scope_Suppress => Scope_Suppress,
13045 Local_Suppress_Stack_Top =>
13046 Local_Suppress_Stack_Top,
13047 Version => Ada_Version,
13048 Version_Pragma => Ada_Version_Pragma,
13049 Warnings => Save_Warnings,
13050 SPARK_Mode => SPARK_Mode,
13051 SPARK_Mode_Pragma => SPARK_Mode_Pragma);
13052
13053 -- Package instance
13054
13055 if
13056 Nkind (Node (Decl)) = N_Package_Instantiation
13057 then
13058 Instantiate_Package_Body
13059 (Info, Body_Optional => True);
13060
13061 -- Subprogram instance
13062
13063 else
13064 -- The instance_spec is in the wrapper package,
13065 -- usually followed by its local renaming
13066 -- declaration. See Build_Subprogram_Renaming
13067 -- for details.
13068
13069 declare
13070 Decl : Node_Id :=
13071 (Last (Visible_Declarations
13072 (Specification (Info.Act_Decl))));
13073 begin
13074 if Nkind (Decl) =
13075 N_Subprogram_Renaming_Declaration
13076 then
13077 Decl := Prev (Decl);
13078 end if;
13079
13080 Info.Act_Decl := Decl;
13081 end;
13082
13083 Instantiate_Subprogram_Body
13084 (Info, Body_Optional => True);
13085 end if;
13086
13087 Next_Elmt (Decl);
13088 end loop;
13089 end;
13090 end if;
13091
13092 Instantiate_Package_Body
13093 (Body_Info =>
13094 ((Inst_Node => Inst_Node,
13095 Act_Decl => True_Parent,
13096 Expander_Status => Exp_Status,
13097 Current_Sem_Unit => Get_Code_Unit
13098 (Sloc (Inst_Node)),
13099 Scope_Suppress => Scope_Suppress,
13100 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
13101 Version => Ada_Version,
13102 Version_Pragma => Ada_Version_Pragma,
13103 Warnings => Save_Warnings,
13104 SPARK_Mode => SPARK_Mode,
13105 SPARK_Mode_Pragma => SPARK_Mode_Pragma)),
13106 Body_Optional => Body_Optional);
13107 end;
13108 end if;
13109
13110 -- Case where we are not instantiating a nested generic
13111
13112 else
13113 Opt.Style_Check := False;
13114 Expander_Mode_Save_And_Set (True);
13115 Load_Needed_Body (Comp_Unit, OK);
13116 Opt.Style_Check := Saved_Style_Check;
13117 Restore_Warnings (Saved_Warnings);
13118 Expander_Mode_Restore;
13119
13120 if not OK
13121 and then Unit_Requires_Body (Defining_Entity (Spec))
13122 and then not Body_Optional
13123 then
13124 declare
13125 Bname : constant Unit_Name_Type :=
13126 Get_Body_Name (Get_Unit_Name (Unit (Comp_Unit)));
13127
13128 begin
13129 -- In CodePeer mode, the missing body may make the analysis
13130 -- incomplete, but we do not treat it as fatal.
13131
13132 if CodePeer_Mode then
13133 return;
13134
13135 else
13136 Error_Msg_Unit_1 := Bname;
13137 Error_Msg_N ("this instantiation requires$!", N);
13138 Error_Msg_File_1 :=
13139 Get_File_Name (Bname, Subunit => False);
13140 Error_Msg_N ("\but file{ was not found!", N);
13141 raise Unrecoverable_Error;
13142 end if;
13143 end;
13144 end if;
13145 end if;
13146 end if;
13147
13148 -- If loading parent of the generic caused an instantiation circularity,
13149 -- we abandon compilation at this point, because otherwise in some cases
13150 -- we get into trouble with infinite recursions after this point.
13151
13152 if Circularity_Detected then
13153 raise Unrecoverable_Error;
13154 end if;
13155 end Load_Parent_Of_Generic;
13156
13157 ---------------------------------
13158 -- Map_Formal_Package_Entities --
13159 ---------------------------------
13160
13161 procedure Map_Formal_Package_Entities (Form : Entity_Id; Act : Entity_Id) is
13162 E1 : Entity_Id;
13163 E2 : Entity_Id;
13164
13165 begin
13166 Set_Instance_Of (Form, Act);
13167
13168 -- Traverse formal and actual package to map the corresponding entities.
13169 -- We skip over internal entities that may be generated during semantic
13170 -- analysis, and find the matching entities by name, given that they
13171 -- must appear in the same order.
13172
13173 E1 := First_Entity (Form);
13174 E2 := First_Entity (Act);
13175 while Present (E1) and then E1 /= First_Private_Entity (Form) loop
13176 -- Could this test be a single condition??? Seems like it could, and
13177 -- isn't FPE (Form) a constant anyway???
13178
13179 if not Is_Internal (E1)
13180 and then Present (Parent (E1))
13181 and then not Is_Class_Wide_Type (E1)
13182 and then not Is_Internal_Name (Chars (E1))
13183 then
13184 while Present (E2) and then Chars (E2) /= Chars (E1) loop
13185 Next_Entity (E2);
13186 end loop;
13187
13188 if No (E2) then
13189 exit;
13190 else
13191 Set_Instance_Of (E1, E2);
13192
13193 if Is_Type (E1) and then Is_Tagged_Type (E2) then
13194 Set_Instance_Of (Class_Wide_Type (E1), Class_Wide_Type (E2));
13195 end if;
13196
13197 if Is_Constrained (E1) then
13198 Set_Instance_Of (Base_Type (E1), Base_Type (E2));
13199 end if;
13200
13201 if Ekind (E1) = E_Package and then No (Renamed_Object (E1)) then
13202 Map_Formal_Package_Entities (E1, E2);
13203 end if;
13204 end if;
13205 end if;
13206
13207 Next_Entity (E1);
13208 end loop;
13209 end Map_Formal_Package_Entities;
13210
13211 -----------------------
13212 -- Move_Freeze_Nodes --
13213 -----------------------
13214
13215 procedure Move_Freeze_Nodes
13216 (Out_Of : Entity_Id;
13217 After : Node_Id;
13218 L : List_Id)
13219 is
13220 Decl : Node_Id;
13221 Next_Decl : Node_Id;
13222 Next_Node : Node_Id := After;
13223 Spec : Node_Id;
13224
13225 function Is_Outer_Type (T : Entity_Id) return Boolean;
13226 -- Check whether entity is declared in a scope external to that of the
13227 -- generic unit.
13228
13229 -------------------
13230 -- Is_Outer_Type --
13231 -------------------
13232
13233 function Is_Outer_Type (T : Entity_Id) return Boolean is
13234 Scop : Entity_Id := Scope (T);
13235
13236 begin
13237 if Scope_Depth (Scop) < Scope_Depth (Out_Of) then
13238 return True;
13239
13240 else
13241 while Scop /= Standard_Standard loop
13242 if Scop = Out_Of then
13243 return False;
13244 else
13245 Scop := Scope (Scop);
13246 end if;
13247 end loop;
13248
13249 return True;
13250 end if;
13251 end Is_Outer_Type;
13252
13253 -- Start of processing for Move_Freeze_Nodes
13254
13255 begin
13256 if No (L) then
13257 return;
13258 end if;
13259
13260 -- First remove the freeze nodes that may appear before all other
13261 -- declarations.
13262
13263 Decl := First (L);
13264 while Present (Decl)
13265 and then Nkind (Decl) = N_Freeze_Entity
13266 and then Is_Outer_Type (Entity (Decl))
13267 loop
13268 Decl := Remove_Head (L);
13269 Insert_After (Next_Node, Decl);
13270 Set_Analyzed (Decl, False);
13271 Next_Node := Decl;
13272 Decl := First (L);
13273 end loop;
13274
13275 -- Next scan the list of declarations and remove each freeze node that
13276 -- appears ahead of the current node.
13277
13278 while Present (Decl) loop
13279 while Present (Next (Decl))
13280 and then Nkind (Next (Decl)) = N_Freeze_Entity
13281 and then Is_Outer_Type (Entity (Next (Decl)))
13282 loop
13283 Next_Decl := Remove_Next (Decl);
13284 Insert_After (Next_Node, Next_Decl);
13285 Set_Analyzed (Next_Decl, False);
13286 Next_Node := Next_Decl;
13287 end loop;
13288
13289 -- If the declaration is a nested package or concurrent type, then
13290 -- recurse. Nested generic packages will have been processed from the
13291 -- inside out.
13292
13293 case Nkind (Decl) is
13294 when N_Package_Declaration =>
13295 Spec := Specification (Decl);
13296
13297 when N_Task_Type_Declaration =>
13298 Spec := Task_Definition (Decl);
13299
13300 when N_Protected_Type_Declaration =>
13301 Spec := Protected_Definition (Decl);
13302
13303 when others =>
13304 Spec := Empty;
13305 end case;
13306
13307 if Present (Spec) then
13308 Move_Freeze_Nodes (Out_Of, Next_Node, Visible_Declarations (Spec));
13309 Move_Freeze_Nodes (Out_Of, Next_Node, Private_Declarations (Spec));
13310 end if;
13311
13312 Next (Decl);
13313 end loop;
13314 end Move_Freeze_Nodes;
13315
13316 ----------------
13317 -- Next_Assoc --
13318 ----------------
13319
13320 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr is
13321 begin
13322 return Generic_Renamings.Table (E).Next_In_HTable;
13323 end Next_Assoc;
13324
13325 ------------------------
13326 -- Preanalyze_Actuals --
13327 ------------------------
13328
13329 procedure Preanalyze_Actuals (N : Node_Id; Inst : Entity_Id := Empty) is
13330 Assoc : Node_Id;
13331 Act : Node_Id;
13332 Errs : constant Int := Serious_Errors_Detected;
13333
13334 Cur : Entity_Id := Empty;
13335 -- Current homograph of the instance name
13336
13337 Vis : Boolean;
13338 -- Saved visibility status of the current homograph
13339
13340 begin
13341 Assoc := First (Generic_Associations (N));
13342
13343 -- If the instance is a child unit, its name may hide an outer homonym,
13344 -- so make it invisible to perform name resolution on the actuals.
13345
13346 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name
13347 and then Present
13348 (Current_Entity (Defining_Identifier (Defining_Unit_Name (N))))
13349 then
13350 Cur := Current_Entity (Defining_Identifier (Defining_Unit_Name (N)));
13351
13352 if Is_Compilation_Unit (Cur) then
13353 Vis := Is_Immediately_Visible (Cur);
13354 Set_Is_Immediately_Visible (Cur, False);
13355 else
13356 Cur := Empty;
13357 end if;
13358 end if;
13359
13360 while Present (Assoc) loop
13361 if Nkind (Assoc) /= N_Others_Choice then
13362 Act := Explicit_Generic_Actual_Parameter (Assoc);
13363
13364 -- Within a nested instantiation, a defaulted actual is an empty
13365 -- association, so nothing to analyze. If the subprogram actual
13366 -- is an attribute, analyze prefix only, because actual is not a
13367 -- complete attribute reference.
13368
13369 -- If actual is an allocator, analyze expression only. The full
13370 -- analysis can generate code, and if instance is a compilation
13371 -- unit we have to wait until the package instance is installed
13372 -- to have a proper place to insert this code.
13373
13374 -- String literals may be operators, but at this point we do not
13375 -- know whether the actual is a formal subprogram or a string.
13376
13377 if No (Act) then
13378 null;
13379
13380 elsif Nkind (Act) = N_Attribute_Reference then
13381 Analyze (Prefix (Act));
13382
13383 elsif Nkind (Act) = N_Explicit_Dereference then
13384 Analyze (Prefix (Act));
13385
13386 elsif Nkind (Act) = N_Allocator then
13387 declare
13388 Expr : constant Node_Id := Expression (Act);
13389
13390 begin
13391 if Nkind (Expr) = N_Subtype_Indication then
13392 Analyze (Subtype_Mark (Expr));
13393
13394 -- Analyze separately each discriminant constraint, when
13395 -- given with a named association.
13396
13397 declare
13398 Constr : Node_Id;
13399
13400 begin
13401 Constr := First (Constraints (Constraint (Expr)));
13402 while Present (Constr) loop
13403 if Nkind (Constr) = N_Discriminant_Association then
13404 Analyze (Expression (Constr));
13405 else
13406 Analyze (Constr);
13407 end if;
13408
13409 Next (Constr);
13410 end loop;
13411 end;
13412
13413 else
13414 Analyze (Expr);
13415 end if;
13416 end;
13417
13418 elsif Nkind (Act) /= N_Operator_Symbol then
13419 Analyze (Act);
13420
13421 if Is_Entity_Name (Act)
13422 and then Is_Type (Entity (Act))
13423 and then From_Limited_With (Entity (Act))
13424 then
13425 Append_Elmt (Entity (Act), Incomplete_Actuals (Inst));
13426 end if;
13427 end if;
13428
13429 if Errs /= Serious_Errors_Detected then
13430
13431 -- Do a minimal analysis of the generic, to prevent spurious
13432 -- warnings complaining about the generic being unreferenced,
13433 -- before abandoning the instantiation.
13434
13435 Analyze (Name (N));
13436
13437 if Is_Entity_Name (Name (N))
13438 and then Etype (Name (N)) /= Any_Type
13439 then
13440 Generate_Reference (Entity (Name (N)), Name (N));
13441 Set_Is_Instantiated (Entity (Name (N)));
13442 end if;
13443
13444 if Present (Cur) then
13445
13446 -- For the case of a child instance hiding an outer homonym,
13447 -- provide additional warning which might explain the error.
13448
13449 Set_Is_Immediately_Visible (Cur, Vis);
13450 Error_Msg_NE
13451 ("& hides outer unit with the same name??",
13452 N, Defining_Unit_Name (N));
13453 end if;
13454
13455 Abandon_Instantiation (Act);
13456 end if;
13457 end if;
13458
13459 Next (Assoc);
13460 end loop;
13461
13462 if Present (Cur) then
13463 Set_Is_Immediately_Visible (Cur, Vis);
13464 end if;
13465 end Preanalyze_Actuals;
13466
13467 -------------------
13468 -- Remove_Parent --
13469 -------------------
13470
13471 procedure Remove_Parent (In_Body : Boolean := False) is
13472 S : Entity_Id := Current_Scope;
13473 -- S is the scope containing the instantiation just completed. The scope
13474 -- stack contains the parent instances of the instantiation, followed by
13475 -- the original S.
13476
13477 Cur_P : Entity_Id;
13478 E : Entity_Id;
13479 P : Entity_Id;
13480 Hidden : Elmt_Id;
13481
13482 begin
13483 -- After child instantiation is complete, remove from scope stack the
13484 -- extra copy of the current scope, and then remove parent instances.
13485
13486 if not In_Body then
13487 Pop_Scope;
13488
13489 while Current_Scope /= S loop
13490 P := Current_Scope;
13491 End_Package_Scope (Current_Scope);
13492
13493 if In_Open_Scopes (P) then
13494 E := First_Entity (P);
13495 while Present (E) loop
13496 Set_Is_Immediately_Visible (E, True);
13497 Next_Entity (E);
13498 end loop;
13499
13500 -- If instantiation is declared in a block, it is the enclosing
13501 -- scope that might be a parent instance. Note that only one
13502 -- block can be involved, because the parent instances have
13503 -- been installed within it.
13504
13505 if Ekind (P) = E_Block then
13506 Cur_P := Scope (P);
13507 else
13508 Cur_P := P;
13509 end if;
13510
13511 if Is_Generic_Instance (Cur_P) and then P /= Current_Scope then
13512 -- We are within an instance of some sibling. Retain
13513 -- visibility of parent, for proper subsequent cleanup, and
13514 -- reinstall private declarations as well.
13515
13516 Set_In_Private_Part (P);
13517 Install_Private_Declarations (P);
13518 end if;
13519
13520 -- If the ultimate parent is a top-level unit recorded in
13521 -- Instance_Parent_Unit, then reset its visibility to what it was
13522 -- before instantiation. (It's not clear what the purpose is of
13523 -- testing whether Scope (P) is In_Open_Scopes, but that test was
13524 -- present before the ultimate parent test was added.???)
13525
13526 elsif not In_Open_Scopes (Scope (P))
13527 or else (P = Instance_Parent_Unit
13528 and then not Parent_Unit_Visible)
13529 then
13530 Set_Is_Immediately_Visible (P, False);
13531
13532 -- If the current scope is itself an instantiation of a generic
13533 -- nested within P, and we are in the private part of body of this
13534 -- instantiation, restore the full views of P, that were removed
13535 -- in End_Package_Scope above. This obscure case can occur when a
13536 -- subunit of a generic contains an instance of a child unit of
13537 -- its generic parent unit.
13538
13539 elsif S = Current_Scope and then Is_Generic_Instance (S) then
13540 declare
13541 Par : constant Entity_Id :=
13542 Generic_Parent (Package_Specification (S));
13543 begin
13544 if Present (Par)
13545 and then P = Scope (Par)
13546 and then (In_Package_Body (S) or else In_Private_Part (S))
13547 then
13548 Set_In_Private_Part (P);
13549 Install_Private_Declarations (P);
13550 end if;
13551 end;
13552 end if;
13553 end loop;
13554
13555 -- Reset visibility of entities in the enclosing scope
13556
13557 Set_Is_Hidden_Open_Scope (Current_Scope, False);
13558
13559 Hidden := First_Elmt (Hidden_Entities);
13560 while Present (Hidden) loop
13561 Set_Is_Immediately_Visible (Node (Hidden), True);
13562 Next_Elmt (Hidden);
13563 end loop;
13564
13565 else
13566 -- Each body is analyzed separately, and there is no context that
13567 -- needs preserving from one body instance to the next, so remove all
13568 -- parent scopes that have been installed.
13569
13570 while Present (S) loop
13571 End_Package_Scope (S);
13572 Set_Is_Immediately_Visible (S, False);
13573 S := Current_Scope;
13574 exit when S = Standard_Standard;
13575 end loop;
13576 end if;
13577 end Remove_Parent;
13578
13579 -----------------
13580 -- Restore_Env --
13581 -----------------
13582
13583 procedure Restore_Env is
13584 Saved : Instance_Env renames Instance_Envs.Table (Instance_Envs.Last);
13585
13586 begin
13587 if No (Current_Instantiated_Parent.Act_Id) then
13588 -- Restore environment after subprogram inlining
13589
13590 Restore_Private_Views (Empty);
13591 end if;
13592
13593 Current_Instantiated_Parent := Saved.Instantiated_Parent;
13594 Exchanged_Views := Saved.Exchanged_Views;
13595 Hidden_Entities := Saved.Hidden_Entities;
13596 Current_Sem_Unit := Saved.Current_Sem_Unit;
13597 Parent_Unit_Visible := Saved.Parent_Unit_Visible;
13598 Instance_Parent_Unit := Saved.Instance_Parent_Unit;
13599
13600 Restore_Opt_Config_Switches (Saved.Switches);
13601
13602 Instance_Envs.Decrement_Last;
13603 end Restore_Env;
13604
13605 ---------------------------
13606 -- Restore_Private_Views --
13607 ---------------------------
13608
13609 procedure Restore_Private_Views
13610 (Pack_Id : Entity_Id;
13611 Is_Package : Boolean := True)
13612 is
13613 M : Elmt_Id;
13614 E : Entity_Id;
13615 Typ : Entity_Id;
13616 Dep_Elmt : Elmt_Id;
13617 Dep_Typ : Node_Id;
13618
13619 procedure Restore_Nested_Formal (Formal : Entity_Id);
13620 -- Hide the generic formals of formal packages declared with box which
13621 -- were reachable in the current instantiation.
13622
13623 ---------------------------
13624 -- Restore_Nested_Formal --
13625 ---------------------------
13626
13627 procedure Restore_Nested_Formal (Formal : Entity_Id) is
13628 Ent : Entity_Id;
13629
13630 begin
13631 if Present (Renamed_Object (Formal))
13632 and then Denotes_Formal_Package (Renamed_Object (Formal), True)
13633 then
13634 return;
13635
13636 elsif Present (Associated_Formal_Package (Formal)) then
13637 Ent := First_Entity (Formal);
13638 while Present (Ent) loop
13639 exit when Ekind (Ent) = E_Package
13640 and then Renamed_Entity (Ent) = Renamed_Entity (Formal);
13641
13642 Set_Is_Hidden (Ent);
13643 Set_Is_Potentially_Use_Visible (Ent, False);
13644
13645 -- If package, then recurse
13646
13647 if Ekind (Ent) = E_Package then
13648 Restore_Nested_Formal (Ent);
13649 end if;
13650
13651 Next_Entity (Ent);
13652 end loop;
13653 end if;
13654 end Restore_Nested_Formal;
13655
13656 -- Start of processing for Restore_Private_Views
13657
13658 begin
13659 M := First_Elmt (Exchanged_Views);
13660 while Present (M) loop
13661 Typ := Node (M);
13662
13663 -- Subtypes of types whose views have been exchanged, and that are
13664 -- defined within the instance, were not on the Private_Dependents
13665 -- list on entry to the instance, so they have to be exchanged
13666 -- explicitly now, in order to remain consistent with the view of the
13667 -- parent type.
13668
13669 if Ekind_In (Typ, E_Private_Type,
13670 E_Limited_Private_Type,
13671 E_Record_Type_With_Private)
13672 then
13673 Dep_Elmt := First_Elmt (Private_Dependents (Typ));
13674 while Present (Dep_Elmt) loop
13675 Dep_Typ := Node (Dep_Elmt);
13676
13677 if Scope (Dep_Typ) = Pack_Id
13678 and then Present (Full_View (Dep_Typ))
13679 then
13680 Replace_Elmt (Dep_Elmt, Full_View (Dep_Typ));
13681 Exchange_Declarations (Dep_Typ);
13682 end if;
13683
13684 Next_Elmt (Dep_Elmt);
13685 end loop;
13686 end if;
13687
13688 Exchange_Declarations (Node (M));
13689 Next_Elmt (M);
13690 end loop;
13691
13692 if No (Pack_Id) then
13693 return;
13694 end if;
13695
13696 -- Make the generic formal parameters private, and make the formal types
13697 -- into subtypes of the actuals again.
13698
13699 E := First_Entity (Pack_Id);
13700 while Present (E) loop
13701 Set_Is_Hidden (E, True);
13702
13703 if Is_Type (E)
13704 and then Nkind (Parent (E)) = N_Subtype_Declaration
13705 then
13706 -- If the actual for E is itself a generic actual type from
13707 -- an enclosing instance, E is still a generic actual type
13708 -- outside of the current instance. This matter when resolving
13709 -- an overloaded call that may be ambiguous in the enclosing
13710 -- instance, when two of its actuals coincide.
13711
13712 if Is_Entity_Name (Subtype_Indication (Parent (E)))
13713 and then Is_Generic_Actual_Type
13714 (Entity (Subtype_Indication (Parent (E))))
13715 then
13716 null;
13717 else
13718 Set_Is_Generic_Actual_Type (E, False);
13719 end if;
13720
13721 -- An unusual case of aliasing: the actual may also be directly
13722 -- visible in the generic, and be private there, while it is fully
13723 -- visible in the context of the instance. The internal subtype
13724 -- is private in the instance but has full visibility like its
13725 -- parent in the enclosing scope. This enforces the invariant that
13726 -- the privacy status of all private dependents of a type coincide
13727 -- with that of the parent type. This can only happen when a
13728 -- generic child unit is instantiated within a sibling.
13729
13730 if Is_Private_Type (E)
13731 and then not Is_Private_Type (Etype (E))
13732 then
13733 Exchange_Declarations (E);
13734 end if;
13735
13736 elsif Ekind (E) = E_Package then
13737
13738 -- The end of the renaming list is the renaming of the generic
13739 -- package itself. If the instance is a subprogram, all entities
13740 -- in the corresponding package are renamings. If this entity is
13741 -- a formal package, make its own formals private as well. The
13742 -- actual in this case is itself the renaming of an instantiation.
13743 -- If the entity is not a package renaming, it is the entity
13744 -- created to validate formal package actuals: ignore it.
13745
13746 -- If the actual is itself a formal package for the enclosing
13747 -- generic, or the actual for such a formal package, it remains
13748 -- visible on exit from the instance, and therefore nothing needs
13749 -- to be done either, except to keep it accessible.
13750
13751 if Is_Package and then Renamed_Object (E) = Pack_Id then
13752 exit;
13753
13754 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
13755 null;
13756
13757 elsif
13758 Denotes_Formal_Package (Renamed_Object (E), True, Pack_Id)
13759 then
13760 Set_Is_Hidden (E, False);
13761
13762 else
13763 declare
13764 Act_P : constant Entity_Id := Renamed_Object (E);
13765 Id : Entity_Id;
13766
13767 begin
13768 Id := First_Entity (Act_P);
13769 while Present (Id)
13770 and then Id /= First_Private_Entity (Act_P)
13771 loop
13772 exit when Ekind (Id) = E_Package
13773 and then Renamed_Object (Id) = Act_P;
13774
13775 Set_Is_Hidden (Id, True);
13776 Set_Is_Potentially_Use_Visible (Id, In_Use (Act_P));
13777
13778 if Ekind (Id) = E_Package then
13779 Restore_Nested_Formal (Id);
13780 end if;
13781
13782 Next_Entity (Id);
13783 end loop;
13784 end;
13785 end if;
13786 end if;
13787
13788 Next_Entity (E);
13789 end loop;
13790 end Restore_Private_Views;
13791
13792 --------------
13793 -- Save_Env --
13794 --------------
13795
13796 procedure Save_Env
13797 (Gen_Unit : Entity_Id;
13798 Act_Unit : Entity_Id)
13799 is
13800 begin
13801 Init_Env;
13802 Set_Instance_Env (Gen_Unit, Act_Unit);
13803 end Save_Env;
13804
13805 ----------------------------
13806 -- Save_Global_References --
13807 ----------------------------
13808
13809 procedure Save_Global_References (Templ : Node_Id) is
13810
13811 -- ??? it is horrible to use global variables in highly recursive code
13812
13813 E : Entity_Id;
13814 -- The entity of the current associated node
13815
13816 Gen_Scope : Entity_Id;
13817 -- The scope of the generic for which references are being saved
13818
13819 N2 : Node_Id;
13820 -- The current associated node
13821
13822 function Is_Global (E : Entity_Id) return Boolean;
13823 -- Check whether entity is defined outside of generic unit. Examine the
13824 -- scope of an entity, and the scope of the scope, etc, until we find
13825 -- either Standard, in which case the entity is global, or the generic
13826 -- unit itself, which indicates that the entity is local. If the entity
13827 -- is the generic unit itself, as in the case of a recursive call, or
13828 -- the enclosing generic unit, if different from the current scope, then
13829 -- it is local as well, because it will be replaced at the point of
13830 -- instantiation. On the other hand, if it is a reference to a child
13831 -- unit of a common ancestor, which appears in an instantiation, it is
13832 -- global because it is used to denote a specific compilation unit at
13833 -- the time the instantiations will be analyzed.
13834
13835 procedure Reset_Entity (N : Node_Id);
13836 -- Save semantic information on global entity so that it is not resolved
13837 -- again at instantiation time.
13838
13839 procedure Save_Entity_Descendants (N : Node_Id);
13840 -- Apply Save_Global_References to the two syntactic descendants of
13841 -- non-terminal nodes that carry an Associated_Node and are processed
13842 -- through Reset_Entity. Once the global entity (if any) has been
13843 -- captured together with its type, only two syntactic descendants need
13844 -- to be traversed to complete the processing of the tree rooted at N.
13845 -- This applies to Selected_Components, Expanded_Names, and to Operator
13846 -- nodes. N can also be a character literal, identifier, or operator
13847 -- symbol node, but the call has no effect in these cases.
13848
13849 procedure Save_Global_Defaults (N1 : Node_Id; N2 : Node_Id);
13850 -- Default actuals in nested instances must be handled specially
13851 -- because there is no link to them from the original tree. When an
13852 -- actual subprogram is given by a default, we add an explicit generic
13853 -- association for it in the instantiation node. When we save the
13854 -- global references on the name of the instance, we recover the list
13855 -- of generic associations, and add an explicit one to the original
13856 -- generic tree, through which a global actual can be preserved.
13857 -- Similarly, if a child unit is instantiated within a sibling, in the
13858 -- context of the parent, we must preserve the identifier of the parent
13859 -- so that it can be properly resolved in a subsequent instantiation.
13860
13861 procedure Save_Global_Descendant (D : Union_Id);
13862 -- Apply Save_References recursively to the descendents of node D
13863
13864 procedure Save_References (N : Node_Id);
13865 -- This is the recursive procedure that does the work, once the
13866 -- enclosing generic scope has been established.
13867
13868 ---------------
13869 -- Is_Global --
13870 ---------------
13871
13872 function Is_Global (E : Entity_Id) return Boolean is
13873 Se : Entity_Id;
13874
13875 function Is_Instance_Node (Decl : Node_Id) return Boolean;
13876 -- Determine whether the parent node of a reference to a child unit
13877 -- denotes an instantiation or a formal package, in which case the
13878 -- reference to the child unit is global, even if it appears within
13879 -- the current scope (e.g. when the instance appears within the body
13880 -- of an ancestor).
13881
13882 ----------------------
13883 -- Is_Instance_Node --
13884 ----------------------
13885
13886 function Is_Instance_Node (Decl : Node_Id) return Boolean is
13887 begin
13888 return Nkind (Decl) in N_Generic_Instantiation
13889 or else
13890 Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration;
13891 end Is_Instance_Node;
13892
13893 -- Start of processing for Is_Global
13894
13895 begin
13896 if E = Gen_Scope then
13897 return False;
13898
13899 elsif E = Standard_Standard then
13900 return True;
13901
13902 elsif Is_Child_Unit (E)
13903 and then (Is_Instance_Node (Parent (N2))
13904 or else (Nkind (Parent (N2)) = N_Expanded_Name
13905 and then N2 = Selector_Name (Parent (N2))
13906 and then
13907 Is_Instance_Node (Parent (Parent (N2)))))
13908 then
13909 return True;
13910
13911 else
13912 Se := Scope (E);
13913 while Se /= Gen_Scope loop
13914 if Se = Standard_Standard then
13915 return True;
13916 else
13917 Se := Scope (Se);
13918 end if;
13919 end loop;
13920
13921 return False;
13922 end if;
13923 end Is_Global;
13924
13925 ------------------
13926 -- Reset_Entity --
13927 ------------------
13928
13929 procedure Reset_Entity (N : Node_Id) is
13930 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id);
13931 -- If the type of N2 is global to the generic unit, save the type in
13932 -- the generic node. Just as we perform name capture for explicit
13933 -- references within the generic, we must capture the global types
13934 -- of local entities because they may participate in resolution in
13935 -- the instance.
13936
13937 function Top_Ancestor (E : Entity_Id) return Entity_Id;
13938 -- Find the ultimate ancestor of the current unit. If it is not a
13939 -- generic unit, then the name of the current unit in the prefix of
13940 -- an expanded name must be replaced with its generic homonym to
13941 -- ensure that it will be properly resolved in an instance.
13942
13943 ---------------------
13944 -- Set_Global_Type --
13945 ---------------------
13946
13947 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id) is
13948 Typ : constant Entity_Id := Etype (N2);
13949
13950 begin
13951 Set_Etype (N, Typ);
13952
13953 -- If the entity of N is not the associated node, this is a
13954 -- nested generic and it has an associated node as well, whose
13955 -- type is already the full view (see below). Indicate that the
13956 -- original node has a private view.
13957
13958 if Entity (N) /= N2 and then Has_Private_View (Entity (N)) then
13959 Set_Has_Private_View (N);
13960 end if;
13961
13962 -- If not a private type, nothing else to do
13963
13964 if not Is_Private_Type (Typ) then
13965 if Is_Array_Type (Typ)
13966 and then Is_Private_Type (Component_Type (Typ))
13967 then
13968 Set_Has_Private_View (N);
13969 end if;
13970
13971 -- If it is a derivation of a private type in a context where no
13972 -- full view is needed, nothing to do either.
13973
13974 elsif No (Full_View (Typ)) and then Typ /= Etype (Typ) then
13975 null;
13976
13977 -- Otherwise mark the type for flipping and use the full view when
13978 -- available.
13979
13980 else
13981 Set_Has_Private_View (N);
13982
13983 if Present (Full_View (Typ)) then
13984 Set_Etype (N2, Full_View (Typ));
13985 end if;
13986 end if;
13987 end Set_Global_Type;
13988
13989 ------------------
13990 -- Top_Ancestor --
13991 ------------------
13992
13993 function Top_Ancestor (E : Entity_Id) return Entity_Id is
13994 Par : Entity_Id;
13995
13996 begin
13997 Par := E;
13998 while Is_Child_Unit (Par) loop
13999 Par := Scope (Par);
14000 end loop;
14001
14002 return Par;
14003 end Top_Ancestor;
14004
14005 -- Start of processing for Reset_Entity
14006
14007 begin
14008 N2 := Get_Associated_Node (N);
14009 E := Entity (N2);
14010
14011 if Present (E) then
14012
14013 -- If the node is an entry call to an entry in an enclosing task,
14014 -- it is rewritten as a selected component. No global entity to
14015 -- preserve in this case, since the expansion will be redone in
14016 -- the instance.
14017
14018 if not Nkind_In (E, N_Defining_Character_Literal,
14019 N_Defining_Identifier,
14020 N_Defining_Operator_Symbol)
14021 then
14022 Set_Associated_Node (N, Empty);
14023 Set_Etype (N, Empty);
14024 return;
14025 end if;
14026
14027 -- If the entity is an itype created as a subtype of an access
14028 -- type with a null exclusion restore source entity for proper
14029 -- visibility. The itype will be created anew in the instance.
14030
14031 if Is_Itype (E)
14032 and then Ekind (E) = E_Access_Subtype
14033 and then Is_Entity_Name (N)
14034 and then Chars (Etype (E)) = Chars (N)
14035 then
14036 E := Etype (E);
14037 Set_Entity (N2, E);
14038 Set_Etype (N2, E);
14039 end if;
14040
14041 if Is_Global (E) then
14042
14043 -- If the entity is a package renaming that is the prefix of
14044 -- an expanded name, it has been rewritten as the renamed
14045 -- package, which is necessary semantically but complicates
14046 -- ASIS tree traversal, so we recover the original entity to
14047 -- expose the renaming. Take into account that the context may
14048 -- be a nested generic, that the original node may itself have
14049 -- an associated node that had better be an entity, and that
14050 -- the current node is still a selected component.
14051
14052 if Ekind (E) = E_Package
14053 and then Nkind (N) = N_Selected_Component
14054 and then Nkind (Parent (N)) = N_Expanded_Name
14055 and then Present (Original_Node (N2))
14056 and then Is_Entity_Name (Original_Node (N2))
14057 and then Present (Entity (Original_Node (N2)))
14058 then
14059 if Is_Global (Entity (Original_Node (N2))) then
14060 N2 := Original_Node (N2);
14061 Set_Associated_Node (N, N2);
14062 Set_Global_Type (N, N2);
14063
14064 -- Renaming is local, and will be resolved in instance
14065
14066 else
14067 Set_Associated_Node (N, Empty);
14068 Set_Etype (N, Empty);
14069 end if;
14070
14071 else
14072 Set_Global_Type (N, N2);
14073 end if;
14074
14075 elsif Nkind (N) = N_Op_Concat
14076 and then Is_Generic_Type (Etype (N2))
14077 and then (Base_Type (Etype (Right_Opnd (N2))) = Etype (N2)
14078 or else
14079 Base_Type (Etype (Left_Opnd (N2))) = Etype (N2))
14080 and then Is_Intrinsic_Subprogram (E)
14081 then
14082 null;
14083
14084 -- Entity is local. Mark generic node as unresolved. Note that now
14085 -- it does not have an entity.
14086
14087 else
14088 Set_Associated_Node (N, Empty);
14089 Set_Etype (N, Empty);
14090 end if;
14091
14092 if Nkind (Parent (N)) in N_Generic_Instantiation
14093 and then N = Name (Parent (N))
14094 then
14095 Save_Global_Defaults (Parent (N), Parent (N2));
14096 end if;
14097
14098 elsif Nkind (Parent (N)) = N_Selected_Component
14099 and then Nkind (Parent (N2)) = N_Expanded_Name
14100 then
14101 if Is_Global (Entity (Parent (N2))) then
14102 Change_Selected_Component_To_Expanded_Name (Parent (N));
14103 Set_Associated_Node (Parent (N), Parent (N2));
14104 Set_Global_Type (Parent (N), Parent (N2));
14105 Save_Entity_Descendants (N);
14106
14107 -- If this is a reference to the current generic entity, replace
14108 -- by the name of the generic homonym of the current package. This
14109 -- is because in an instantiation Par.P.Q will not resolve to the
14110 -- name of the instance, whose enclosing scope is not necessarily
14111 -- Par. We use the generic homonym rather that the name of the
14112 -- generic itself because it may be hidden by a local declaration.
14113
14114 elsif In_Open_Scopes (Entity (Parent (N2)))
14115 and then not
14116 Is_Generic_Unit (Top_Ancestor (Entity (Prefix (Parent (N2)))))
14117 then
14118 if Ekind (Entity (Parent (N2))) = E_Generic_Package then
14119 Rewrite (Parent (N),
14120 Make_Identifier (Sloc (N),
14121 Chars =>
14122 Chars (Generic_Homonym (Entity (Parent (N2))))));
14123 else
14124 Rewrite (Parent (N),
14125 Make_Identifier (Sloc (N),
14126 Chars => Chars (Selector_Name (Parent (N2)))));
14127 end if;
14128 end if;
14129
14130 if Nkind (Parent (Parent (N))) in N_Generic_Instantiation
14131 and then Parent (N) = Name (Parent (Parent (N)))
14132 then
14133 Save_Global_Defaults
14134 (Parent (Parent (N)), Parent (Parent (N2)));
14135 end if;
14136
14137 -- A selected component may denote a static constant that has been
14138 -- folded. If the static constant is global to the generic, capture
14139 -- its value. Otherwise the folding will happen in any instantiation.
14140
14141 elsif Nkind (Parent (N)) = N_Selected_Component
14142 and then Nkind_In (Parent (N2), N_Integer_Literal, N_Real_Literal)
14143 then
14144 if Present (Entity (Original_Node (Parent (N2))))
14145 and then Is_Global (Entity (Original_Node (Parent (N2))))
14146 then
14147 Rewrite (Parent (N), New_Copy (Parent (N2)));
14148 Set_Analyzed (Parent (N), False);
14149 end if;
14150
14151 -- A selected component may be transformed into a parameterless
14152 -- function call. If the called entity is global, rewrite the node
14153 -- appropriately, i.e. as an extended name for the global entity.
14154
14155 elsif Nkind (Parent (N)) = N_Selected_Component
14156 and then Nkind (Parent (N2)) = N_Function_Call
14157 and then N = Selector_Name (Parent (N))
14158 then
14159 if No (Parameter_Associations (Parent (N2))) then
14160 if Is_Global (Entity (Name (Parent (N2)))) then
14161 Change_Selected_Component_To_Expanded_Name (Parent (N));
14162 Set_Associated_Node (Parent (N), Name (Parent (N2)));
14163 Set_Global_Type (Parent (N), Name (Parent (N2)));
14164 Save_Entity_Descendants (N);
14165
14166 else
14167 Set_Is_Prefixed_Call (Parent (N));
14168 Set_Associated_Node (N, Empty);
14169 Set_Etype (N, Empty);
14170 end if;
14171
14172 -- In Ada 2005, X.F may be a call to a primitive operation,
14173 -- rewritten as F (X). This rewriting will be done again in an
14174 -- instance, so keep the original node. Global entities will be
14175 -- captured as for other constructs. Indicate that this must
14176 -- resolve as a call, to prevent accidental overloading in the
14177 -- instance, if both a component and a primitive operation appear
14178 -- as candidates.
14179
14180 else
14181 Set_Is_Prefixed_Call (Parent (N));
14182 end if;
14183
14184 -- Entity is local. Reset in generic unit, so that node is resolved
14185 -- anew at the point of instantiation.
14186
14187 else
14188 Set_Associated_Node (N, Empty);
14189 Set_Etype (N, Empty);
14190 end if;
14191 end Reset_Entity;
14192
14193 -----------------------------
14194 -- Save_Entity_Descendants --
14195 -----------------------------
14196
14197 procedure Save_Entity_Descendants (N : Node_Id) is
14198 begin
14199 case Nkind (N) is
14200 when N_Binary_Op =>
14201 Save_Global_Descendant (Union_Id (Left_Opnd (N)));
14202 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
14203
14204 when N_Unary_Op =>
14205 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
14206
14207 when N_Expanded_Name |
14208 N_Selected_Component =>
14209 Save_Global_Descendant (Union_Id (Prefix (N)));
14210 Save_Global_Descendant (Union_Id (Selector_Name (N)));
14211
14212 when N_Identifier |
14213 N_Character_Literal |
14214 N_Operator_Symbol =>
14215 null;
14216
14217 when others =>
14218 raise Program_Error;
14219 end case;
14220 end Save_Entity_Descendants;
14221
14222 --------------------------
14223 -- Save_Global_Defaults --
14224 --------------------------
14225
14226 procedure Save_Global_Defaults (N1 : Node_Id; N2 : Node_Id) is
14227 Loc : constant Source_Ptr := Sloc (N1);
14228 Assoc2 : constant List_Id := Generic_Associations (N2);
14229 Gen_Id : constant Entity_Id := Get_Generic_Entity (N2);
14230 Assoc1 : List_Id;
14231 Act1 : Node_Id;
14232 Act2 : Node_Id;
14233 Def : Node_Id;
14234 Ndec : Node_Id;
14235 Subp : Entity_Id;
14236 Actual : Entity_Id;
14237
14238 begin
14239 Assoc1 := Generic_Associations (N1);
14240
14241 if Present (Assoc1) then
14242 Act1 := First (Assoc1);
14243 else
14244 Act1 := Empty;
14245 Set_Generic_Associations (N1, New_List);
14246 Assoc1 := Generic_Associations (N1);
14247 end if;
14248
14249 if Present (Assoc2) then
14250 Act2 := First (Assoc2);
14251 else
14252 return;
14253 end if;
14254
14255 while Present (Act1) and then Present (Act2) loop
14256 Next (Act1);
14257 Next (Act2);
14258 end loop;
14259
14260 -- Find the associations added for default subprograms
14261
14262 if Present (Act2) then
14263 while Nkind (Act2) /= N_Generic_Association
14264 or else No (Entity (Selector_Name (Act2)))
14265 or else not Is_Overloadable (Entity (Selector_Name (Act2)))
14266 loop
14267 Next (Act2);
14268 end loop;
14269
14270 -- Add a similar association if the default is global. The
14271 -- renaming declaration for the actual has been analyzed, and
14272 -- its alias is the program it renames. Link the actual in the
14273 -- original generic tree with the node in the analyzed tree.
14274
14275 while Present (Act2) loop
14276 Subp := Entity (Selector_Name (Act2));
14277 Def := Explicit_Generic_Actual_Parameter (Act2);
14278
14279 -- Following test is defence against rubbish errors
14280
14281 if No (Alias (Subp)) then
14282 return;
14283 end if;
14284
14285 -- Retrieve the resolved actual from the renaming declaration
14286 -- created for the instantiated formal.
14287
14288 Actual := Entity (Name (Parent (Parent (Subp))));
14289 Set_Entity (Def, Actual);
14290 Set_Etype (Def, Etype (Actual));
14291
14292 if Is_Global (Actual) then
14293 Ndec :=
14294 Make_Generic_Association (Loc,
14295 Selector_Name =>
14296 New_Occurrence_Of (Subp, Loc),
14297 Explicit_Generic_Actual_Parameter =>
14298 New_Occurrence_Of (Actual, Loc));
14299
14300 Set_Associated_Node
14301 (Explicit_Generic_Actual_Parameter (Ndec), Def);
14302
14303 Append (Ndec, Assoc1);
14304
14305 -- If there are other defaults, add a dummy association in case
14306 -- there are other defaulted formals with the same name.
14307
14308 elsif Present (Next (Act2)) then
14309 Ndec :=
14310 Make_Generic_Association (Loc,
14311 Selector_Name =>
14312 New_Occurrence_Of (Subp, Loc),
14313 Explicit_Generic_Actual_Parameter => Empty);
14314
14315 Append (Ndec, Assoc1);
14316 end if;
14317
14318 Next (Act2);
14319 end loop;
14320 end if;
14321
14322 if Nkind (Name (N1)) = N_Identifier
14323 and then Is_Child_Unit (Gen_Id)
14324 and then Is_Global (Gen_Id)
14325 and then Is_Generic_Unit (Scope (Gen_Id))
14326 and then In_Open_Scopes (Scope (Gen_Id))
14327 then
14328 -- This is an instantiation of a child unit within a sibling, so
14329 -- that the generic parent is in scope. An eventual instance must
14330 -- occur within the scope of an instance of the parent. Make name
14331 -- in instance into an expanded name, to preserve the identifier
14332 -- of the parent, so it can be resolved subsequently.
14333
14334 Rewrite (Name (N2),
14335 Make_Expanded_Name (Loc,
14336 Chars => Chars (Gen_Id),
14337 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
14338 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
14339 Set_Entity (Name (N2), Gen_Id);
14340
14341 Rewrite (Name (N1),
14342 Make_Expanded_Name (Loc,
14343 Chars => Chars (Gen_Id),
14344 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
14345 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
14346
14347 Set_Associated_Node (Name (N1), Name (N2));
14348 Set_Associated_Node (Prefix (Name (N1)), Empty);
14349 Set_Associated_Node
14350 (Selector_Name (Name (N1)), Selector_Name (Name (N2)));
14351 Set_Etype (Name (N1), Etype (Gen_Id));
14352 end if;
14353 end Save_Global_Defaults;
14354
14355 ----------------------------
14356 -- Save_Global_Descendant --
14357 ----------------------------
14358
14359 procedure Save_Global_Descendant (D : Union_Id) is
14360 N1 : Node_Id;
14361
14362 begin
14363 if D in Node_Range then
14364 if D = Union_Id (Empty) then
14365 null;
14366
14367 elsif Nkind (Node_Id (D)) /= N_Compilation_Unit then
14368 Save_References (Node_Id (D));
14369 end if;
14370
14371 elsif D in List_Range then
14372 if D = Union_Id (No_List) or else Is_Empty_List (List_Id (D)) then
14373 null;
14374
14375 else
14376 N1 := First (List_Id (D));
14377 while Present (N1) loop
14378 Save_References (N1);
14379 Next (N1);
14380 end loop;
14381 end if;
14382
14383 -- Element list or other non-node field, nothing to do
14384
14385 else
14386 null;
14387 end if;
14388 end Save_Global_Descendant;
14389
14390 ---------------------
14391 -- Save_References --
14392 ---------------------
14393
14394 -- This is the recursive procedure that does the work once the enclosing
14395 -- generic scope has been established. We have to treat specially a
14396 -- number of node rewritings that are required by semantic processing
14397 -- and which change the kind of nodes in the generic copy: typically
14398 -- constant-folding, replacing an operator node by a string literal, or
14399 -- a selected component by an expanded name. In each of those cases, the
14400 -- transformation is propagated to the generic unit.
14401
14402 procedure Save_References (N : Node_Id) is
14403 Loc : constant Source_Ptr := Sloc (N);
14404
14405 function Requires_Delayed_Save (Nod : Node_Id) return Boolean;
14406 -- Determine whether arbitrary node Nod requires delayed capture of
14407 -- global references within its aspect specifications.
14408
14409 procedure Save_References_In_Aggregate (N : Node_Id);
14410 -- Save all global references in [extension] aggregate node N
14411
14412 procedure Save_References_In_Char_Lit_Or_Op_Symbol (N : Node_Id);
14413 -- Save all global references in a character literal or operator
14414 -- symbol denoted by N.
14415
14416 procedure Save_References_In_Descendants (N : Node_Id);
14417 -- Save all global references in all descendants of node N
14418
14419 procedure Save_References_In_Identifier (N : Node_Id);
14420 -- Save all global references in identifier node N
14421
14422 procedure Save_References_In_Operator (N : Node_Id);
14423 -- Save all global references in operator node N
14424
14425 procedure Save_References_In_Pragma (Prag : Node_Id);
14426 -- Save all global references found within the expression of pragma
14427 -- Prag.
14428
14429 ---------------------------
14430 -- Requires_Delayed_Save --
14431 ---------------------------
14432
14433 function Requires_Delayed_Save (Nod : Node_Id) return Boolean is
14434 begin
14435 -- Generic packages and subprograms require delayed capture of
14436 -- global references within their aspects due to the timing of
14437 -- annotation analysis.
14438
14439 if Nkind_In (Nod, N_Generic_Package_Declaration,
14440 N_Generic_Subprogram_Declaration,
14441 N_Package_Body,
14442 N_Package_Body_Stub,
14443 N_Subprogram_Body,
14444 N_Subprogram_Body_Stub)
14445 then
14446 -- Since the capture of global references is done on the
14447 -- unanalyzed generic template, there is no information around
14448 -- to infer the context. Use the Associated_Entity linkages to
14449 -- peek into the analyzed generic copy and determine what the
14450 -- template corresponds to.
14451
14452 if Nod = Templ then
14453 return
14454 Is_Generic_Declaration_Or_Body
14455 (Unit_Declaration_Node
14456 (Associated_Entity (Defining_Entity (Nod))));
14457
14458 -- Otherwise the generic unit being processed is not the top
14459 -- level template. It is safe to capture of global references
14460 -- within the generic unit because at this point the top level
14461 -- copy is fully analyzed.
14462
14463 else
14464 return False;
14465 end if;
14466
14467 -- Otherwise capture the global references without interference
14468
14469 else
14470 return False;
14471 end if;
14472 end Requires_Delayed_Save;
14473
14474 ----------------------------------
14475 -- Save_References_In_Aggregate --
14476 ----------------------------------
14477
14478 procedure Save_References_In_Aggregate (N : Node_Id) is
14479 Nam : Node_Id;
14480 Qual : Node_Id := Empty;
14481 Typ : Entity_Id := Empty;
14482
14483 use Atree.Unchecked_Access;
14484 -- This code section is part of implementing an untyped tree
14485 -- traversal, so it needs direct access to node fields.
14486
14487 begin
14488 N2 := Get_Associated_Node (N);
14489
14490 if Present (N2) then
14491 Typ := Etype (N2);
14492
14493 -- In an instance within a generic, use the name of the actual
14494 -- and not the original generic parameter. If the actual is
14495 -- global in the current generic it must be preserved for its
14496 -- instantiation.
14497
14498 if Nkind (Parent (Typ)) = N_Subtype_Declaration
14499 and then Present (Generic_Parent_Type (Parent (Typ)))
14500 then
14501 Typ := Base_Type (Typ);
14502 Set_Etype (N2, Typ);
14503 end if;
14504 end if;
14505
14506 if No (N2) or else No (Typ) or else not Is_Global (Typ) then
14507 Set_Associated_Node (N, Empty);
14508
14509 -- If the aggregate is an actual in a call, it has been
14510 -- resolved in the current context, to some local type. The
14511 -- enclosing call may have been disambiguated by the aggregate,
14512 -- and this disambiguation might fail at instantiation time
14513 -- because the type to which the aggregate did resolve is not
14514 -- preserved. In order to preserve some of this information,
14515 -- wrap the aggregate in a qualified expression, using the id
14516 -- of its type. For further disambiguation we qualify the type
14517 -- name with its scope (if visible) because both id's will have
14518 -- corresponding entities in an instance. This resolves most of
14519 -- the problems with missing type information on aggregates in
14520 -- instances.
14521
14522 if Present (N2)
14523 and then Nkind (N2) = Nkind (N)
14524 and then Nkind (Parent (N2)) in N_Subprogram_Call
14525 and then Present (Typ)
14526 and then Comes_From_Source (Typ)
14527 then
14528 Nam := Make_Identifier (Loc, Chars (Typ));
14529
14530 if Is_Immediately_Visible (Scope (Typ)) then
14531 Nam :=
14532 Make_Selected_Component (Loc,
14533 Prefix =>
14534 Make_Identifier (Loc, Chars (Scope (Typ))),
14535 Selector_Name => Nam);
14536 end if;
14537
14538 Qual :=
14539 Make_Qualified_Expression (Loc,
14540 Subtype_Mark => Nam,
14541 Expression => Relocate_Node (N));
14542 end if;
14543 end if;
14544
14545 Save_Global_Descendant (Field1 (N));
14546 Save_Global_Descendant (Field2 (N));
14547 Save_Global_Descendant (Field3 (N));
14548 Save_Global_Descendant (Field5 (N));
14549
14550 if Present (Qual) then
14551 Rewrite (N, Qual);
14552 end if;
14553 end Save_References_In_Aggregate;
14554
14555 ----------------------------------------------
14556 -- Save_References_In_Char_Lit_Or_Op_Symbol --
14557 ----------------------------------------------
14558
14559 procedure Save_References_In_Char_Lit_Or_Op_Symbol (N : Node_Id) is
14560 begin
14561 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
14562 Reset_Entity (N);
14563
14564 elsif Nkind (N) = N_Operator_Symbol
14565 and then Nkind (Get_Associated_Node (N)) = N_String_Literal
14566 then
14567 Change_Operator_Symbol_To_String_Literal (N);
14568 end if;
14569 end Save_References_In_Char_Lit_Or_Op_Symbol;
14570
14571 ------------------------------------
14572 -- Save_References_In_Descendants --
14573 ------------------------------------
14574
14575 procedure Save_References_In_Descendants (N : Node_Id) is
14576 use Atree.Unchecked_Access;
14577 -- This code section is part of implementing an untyped tree
14578 -- traversal, so it needs direct access to node fields.
14579
14580 begin
14581 Save_Global_Descendant (Field1 (N));
14582 Save_Global_Descendant (Field2 (N));
14583 Save_Global_Descendant (Field3 (N));
14584 Save_Global_Descendant (Field4 (N));
14585 Save_Global_Descendant (Field5 (N));
14586 end Save_References_In_Descendants;
14587
14588 -----------------------------------
14589 -- Save_References_In_Identifier --
14590 -----------------------------------
14591
14592 procedure Save_References_In_Identifier (N : Node_Id) is
14593 begin
14594 -- The node did not undergo a transformation
14595
14596 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
14597
14598 -- If this is a discriminant reference, always save it. It is
14599 -- used in the instance to find the corresponding discriminant
14600 -- positionally rather than by name.
14601
14602 Set_Original_Discriminant
14603 (N, Original_Discriminant (Get_Associated_Node (N)));
14604 Reset_Entity (N);
14605
14606 -- The analysis of the generic copy transformed the identifier
14607 -- into another construct. Propagate the changes to the template.
14608
14609 else
14610 N2 := Get_Associated_Node (N);
14611
14612 -- The identifier denotes a call to a parameterless function.
14613 -- Mark the node as resolved when the function is external.
14614
14615 if Nkind (N2) = N_Function_Call then
14616 E := Entity (Name (N2));
14617
14618 if Present (E) and then Is_Global (E) then
14619 Set_Etype (N, Etype (N2));
14620 else
14621 Set_Associated_Node (N, Empty);
14622 Set_Etype (N, Empty);
14623 end if;
14624
14625 -- The identifier denotes a named number that was constant
14626 -- folded. Preserve the original name for ASIS and undo the
14627 -- constant folding which will be repeated in the instance.
14628
14629 elsif Nkind_In (N2, N_Integer_Literal, N_Real_Literal)
14630 and then Is_Entity_Name (Original_Node (N2))
14631 then
14632 Set_Associated_Node (N, Original_Node (N2));
14633 Reset_Entity (N);
14634
14635 -- The identifier resolved to a string literal. Propagate this
14636 -- information to the generic template.
14637
14638 elsif Nkind (N2) = N_String_Literal then
14639 Rewrite (N, New_Copy (N2));
14640
14641 -- The identifier is rewritten as a dereference if it is the
14642 -- prefix of an implicit dereference. Preserve the original
14643 -- tree as the analysis of the instance will expand the node
14644 -- again, but preserve the resolved entity if it is global.
14645
14646 elsif Nkind (N2) = N_Explicit_Dereference then
14647 if Is_Entity_Name (Prefix (N2))
14648 and then Present (Entity (Prefix (N2)))
14649 and then Is_Global (Entity (Prefix (N2)))
14650 then
14651 Set_Associated_Node (N, Prefix (N2));
14652
14653 elsif Nkind (Prefix (N2)) = N_Function_Call
14654 and then Present (Entity (Name (Prefix (N2))))
14655 and then Is_Global (Entity (Name (Prefix (N2))))
14656 then
14657 Rewrite (N,
14658 Make_Explicit_Dereference (Loc,
14659 Prefix =>
14660 Make_Function_Call (Loc,
14661 Name =>
14662 New_Occurrence_Of
14663 (Entity (Name (Prefix (N2))), Loc))));
14664
14665 else
14666 Set_Associated_Node (N, Empty);
14667 Set_Etype (N, Empty);
14668 end if;
14669
14670 -- The subtype mark of a nominally unconstrained object is
14671 -- rewritten as a subtype indication using the bounds of the
14672 -- expression. Recover the original subtype mark.
14673
14674 elsif Nkind (N2) = N_Subtype_Indication
14675 and then Is_Entity_Name (Original_Node (N2))
14676 then
14677 Set_Associated_Node (N, Original_Node (N2));
14678 Reset_Entity (N);
14679 end if;
14680 end if;
14681 end Save_References_In_Identifier;
14682
14683 ---------------------------------
14684 -- Save_References_In_Operator --
14685 ---------------------------------
14686
14687 procedure Save_References_In_Operator (N : Node_Id) is
14688 begin
14689 -- The node did not undergo a transformation
14690
14691 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
14692 if Nkind (N) = N_Op_Concat then
14693 Set_Is_Component_Left_Opnd (N,
14694 Is_Component_Left_Opnd (Get_Associated_Node (N)));
14695
14696 Set_Is_Component_Right_Opnd (N,
14697 Is_Component_Right_Opnd (Get_Associated_Node (N)));
14698 end if;
14699
14700 Reset_Entity (N);
14701
14702 -- The analysis of the generic copy transformed the operator into
14703 -- some other construct. Propagate the changes to the template.
14704
14705 else
14706 N2 := Get_Associated_Node (N);
14707
14708 -- The operator resoved to a function call
14709
14710 if Nkind (N2) = N_Function_Call then
14711 E := Entity (Name (N2));
14712
14713 if Present (E) and then Is_Global (E) then
14714 Set_Etype (N, Etype (N2));
14715 else
14716 Set_Associated_Node (N, Empty);
14717 Set_Etype (N, Empty);
14718 end if;
14719
14720 -- The operator was folded into a literal
14721
14722 elsif Nkind_In (N2, N_Integer_Literal,
14723 N_Real_Literal,
14724 N_String_Literal)
14725 then
14726 if Present (Original_Node (N2))
14727 and then Nkind (Original_Node (N2)) = Nkind (N)
14728 then
14729 -- Operation was constant-folded. Whenever possible,
14730 -- recover semantic information from unfolded node,
14731 -- for ASIS use.
14732
14733 Set_Associated_Node (N, Original_Node (N2));
14734
14735 if Nkind (N) = N_Op_Concat then
14736 Set_Is_Component_Left_Opnd (N,
14737 Is_Component_Left_Opnd (Get_Associated_Node (N)));
14738 Set_Is_Component_Right_Opnd (N,
14739 Is_Component_Right_Opnd (Get_Associated_Node (N)));
14740 end if;
14741
14742 Reset_Entity (N);
14743
14744 -- Propagate the constant folding back to the template
14745
14746 else
14747 Rewrite (N, New_Copy (N2));
14748 Set_Analyzed (N, False);
14749 end if;
14750
14751 -- The operator was folded into an enumeration literal. Retain
14752 -- the entity to avoid spurious ambiguities if it is overloaded
14753 -- at the point of instantiation or inlining.
14754
14755 elsif Nkind (N2) = N_Identifier
14756 and then Ekind (Entity (N2)) = E_Enumeration_Literal
14757 then
14758 Rewrite (N, New_Copy (N2));
14759 Set_Analyzed (N, False);
14760 end if;
14761 end if;
14762
14763 -- Complete the operands check if node has not been constant
14764 -- folded.
14765
14766 if Nkind (N) in N_Op then
14767 Save_Entity_Descendants (N);
14768 end if;
14769 end Save_References_In_Operator;
14770
14771 -------------------------------
14772 -- Save_References_In_Pragma --
14773 -------------------------------
14774
14775 procedure Save_References_In_Pragma (Prag : Node_Id) is
14776 Context : Node_Id;
14777 Do_Save : Boolean := True;
14778
14779 use Atree.Unchecked_Access;
14780 -- This code section is part of implementing an untyped tree
14781 -- traversal, so it needs direct access to node fields.
14782
14783 begin
14784 -- Do not save global references in pragmas generated from aspects
14785 -- because the pragmas will be regenerated at instantiation time.
14786
14787 if From_Aspect_Specification (Prag) then
14788 Do_Save := False;
14789
14790 -- The capture of global references within contract-related source
14791 -- pragmas associated with generic packages, subprograms or their
14792 -- respective bodies must be delayed due to timing of annotation
14793 -- analysis. Global references are still captured in routine
14794 -- Save_Global_References_In_Contract.
14795
14796 elsif Is_Generic_Contract_Pragma (Prag) and then Prag /= Templ then
14797 if Is_Package_Contract_Annotation (Prag) then
14798 Context := Find_Related_Package_Or_Body (Prag);
14799
14800 else
14801 pragma Assert (Is_Subprogram_Contract_Annotation (Prag));
14802 Context := Find_Related_Subprogram_Or_Body (Prag);
14803 end if;
14804
14805 -- The use of Original_Node accounts for the case when the
14806 -- related context is generic template.
14807
14808 if Requires_Delayed_Save (Original_Node (Context)) then
14809 Do_Save := False;
14810 end if;
14811 end if;
14812
14813 -- For all other cases, save all global references within the
14814 -- descendants, but skip the following semantic fields:
14815
14816 -- Field1 - Next_Pragma
14817 -- Field3 - Corresponding_Aspect
14818 -- Field5 - Next_Rep_Item
14819
14820 if Do_Save then
14821 Save_Global_Descendant (Field2 (Prag));
14822 Save_Global_Descendant (Field4 (Prag));
14823 end if;
14824 end Save_References_In_Pragma;
14825
14826 -- Start of processing for Save_References
14827
14828 begin
14829 if N = Empty then
14830 null;
14831
14832 -- Aggregates
14833
14834 elsif Nkind_In (N, N_Aggregate, N_Extension_Aggregate) then
14835 Save_References_In_Aggregate (N);
14836
14837 -- Character literals, operator symbols
14838
14839 elsif Nkind_In (N, N_Character_Literal, N_Operator_Symbol) then
14840 Save_References_In_Char_Lit_Or_Op_Symbol (N);
14841
14842 -- Defining identifiers
14843
14844 elsif Nkind (N) in N_Entity then
14845 null;
14846
14847 -- Identifiers
14848
14849 elsif Nkind (N) = N_Identifier then
14850 Save_References_In_Identifier (N);
14851
14852 -- Operators
14853
14854 elsif Nkind (N) in N_Op then
14855 Save_References_In_Operator (N);
14856
14857 -- Pragmas
14858
14859 elsif Nkind (N) = N_Pragma then
14860 Save_References_In_Pragma (N);
14861
14862 else
14863 Save_References_In_Descendants (N);
14864 end if;
14865
14866 -- Save all global references found within the aspect specifications
14867 -- of the related node.
14868
14869 if Permits_Aspect_Specifications (N) and then Has_Aspects (N) then
14870
14871 -- The capture of global references within aspects associated with
14872 -- generic packages, subprograms or their bodies must be delayed
14873 -- due to timing of annotation analysis. Global references are
14874 -- still captured in routine Save_Global_References_In_Contract.
14875
14876 if Requires_Delayed_Save (N) then
14877 null;
14878
14879 -- Otherwise save all global references within the aspects
14880
14881 else
14882 Save_Global_References_In_Aspects (N);
14883 end if;
14884 end if;
14885 end Save_References;
14886
14887 -- Start of processing for Save_Global_References
14888
14889 begin
14890 Gen_Scope := Current_Scope;
14891
14892 -- If the generic unit is a child unit, references to entities in the
14893 -- parent are treated as local, because they will be resolved anew in
14894 -- the context of the instance of the parent.
14895
14896 while Is_Child_Unit (Gen_Scope)
14897 and then Ekind (Scope (Gen_Scope)) = E_Generic_Package
14898 loop
14899 Gen_Scope := Scope (Gen_Scope);
14900 end loop;
14901
14902 Save_References (Templ);
14903 end Save_Global_References;
14904
14905 ---------------------------------------
14906 -- Save_Global_References_In_Aspects --
14907 ---------------------------------------
14908
14909 procedure Save_Global_References_In_Aspects (N : Node_Id) is
14910 Asp : Node_Id;
14911 Expr : Node_Id;
14912
14913 begin
14914 Asp := First (Aspect_Specifications (N));
14915 while Present (Asp) loop
14916 Expr := Expression (Asp);
14917
14918 if Present (Expr) then
14919 Save_Global_References (Expr);
14920 end if;
14921
14922 Next (Asp);
14923 end loop;
14924 end Save_Global_References_In_Aspects;
14925
14926 --------------------------------------
14927 -- Set_Copied_Sloc_For_Inlined_Body --
14928 --------------------------------------
14929
14930 procedure Set_Copied_Sloc_For_Inlined_Body (N : Node_Id; E : Entity_Id) is
14931 begin
14932 Create_Instantiation_Source (N, E, True, S_Adjustment);
14933 end Set_Copied_Sloc_For_Inlined_Body;
14934
14935 ---------------------
14936 -- Set_Instance_Of --
14937 ---------------------
14938
14939 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id) is
14940 begin
14941 Generic_Renamings.Table (Generic_Renamings.Last) := (A, B, Assoc_Null);
14942 Generic_Renamings_HTable.Set (Generic_Renamings.Last);
14943 Generic_Renamings.Increment_Last;
14944 end Set_Instance_Of;
14945
14946 --------------------
14947 -- Set_Next_Assoc --
14948 --------------------
14949
14950 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr) is
14951 begin
14952 Generic_Renamings.Table (E).Next_In_HTable := Next;
14953 end Set_Next_Assoc;
14954
14955 -------------------
14956 -- Start_Generic --
14957 -------------------
14958
14959 procedure Start_Generic is
14960 begin
14961 -- ??? More things could be factored out in this routine.
14962 -- Should probably be done at a later stage.
14963
14964 Generic_Flags.Append (Inside_A_Generic);
14965 Inside_A_Generic := True;
14966
14967 Expander_Mode_Save_And_Set (False);
14968 end Start_Generic;
14969
14970 ----------------------
14971 -- Set_Instance_Env --
14972 ----------------------
14973
14974 procedure Set_Instance_Env
14975 (Gen_Unit : Entity_Id;
14976 Act_Unit : Entity_Id)
14977 is
14978 Assertion_Status : constant Boolean := Assertions_Enabled;
14979 Save_SPARK_Mode : constant SPARK_Mode_Type := SPARK_Mode;
14980 Save_SPARK_Mode_Pragma : constant Node_Id := SPARK_Mode_Pragma;
14981
14982 begin
14983 -- Regardless of the current mode, predefined units are analyzed in the
14984 -- most current Ada mode, and earlier version Ada checks do not apply
14985 -- to predefined units. Nothing needs to be done for non-internal units.
14986 -- These are always analyzed in the current mode.
14987
14988 if Is_Internal_File_Name
14989 (Fname => Unit_File_Name (Get_Source_Unit (Gen_Unit)),
14990 Renamings_Included => True)
14991 then
14992 Set_Opt_Config_Switches (True, Current_Sem_Unit = Main_Unit);
14993
14994 -- In Ada2012 we may want to enable assertions in an instance of a
14995 -- predefined unit, in which case we need to preserve the current
14996 -- setting for the Assertions_Enabled flag. This will become more
14997 -- critical when pre/postconditions are added to predefined units,
14998 -- as is already the case for some numeric libraries.
14999
15000 if Ada_Version >= Ada_2012 then
15001 Assertions_Enabled := Assertion_Status;
15002 end if;
15003
15004 -- SPARK_Mode for an instance is the one applicable at the point of
15005 -- instantiation.
15006
15007 SPARK_Mode := Save_SPARK_Mode;
15008 SPARK_Mode_Pragma := Save_SPARK_Mode_Pragma;
15009
15010 -- Make sure dynamic elaboration checks are off in SPARK Mode
15011
15012 if SPARK_Mode = On then
15013 Dynamic_Elaboration_Checks := False;
15014 end if;
15015 end if;
15016
15017 Current_Instantiated_Parent :=
15018 (Gen_Id => Gen_Unit,
15019 Act_Id => Act_Unit,
15020 Next_In_HTable => Assoc_Null);
15021 end Set_Instance_Env;
15022
15023 -----------------
15024 -- Switch_View --
15025 -----------------
15026
15027 procedure Switch_View (T : Entity_Id) is
15028 BT : constant Entity_Id := Base_Type (T);
15029 Priv_Elmt : Elmt_Id := No_Elmt;
15030 Priv_Sub : Entity_Id;
15031
15032 begin
15033 -- T may be private but its base type may have been exchanged through
15034 -- some other occurrence, in which case there is nothing to switch
15035 -- besides T itself. Note that a private dependent subtype of a private
15036 -- type might not have been switched even if the base type has been,
15037 -- because of the last branch of Check_Private_View (see comment there).
15038
15039 if not Is_Private_Type (BT) then
15040 Prepend_Elmt (Full_View (T), Exchanged_Views);
15041 Exchange_Declarations (T);
15042 return;
15043 end if;
15044
15045 Priv_Elmt := First_Elmt (Private_Dependents (BT));
15046
15047 if Present (Full_View (BT)) then
15048 Prepend_Elmt (Full_View (BT), Exchanged_Views);
15049 Exchange_Declarations (BT);
15050 end if;
15051
15052 while Present (Priv_Elmt) loop
15053 Priv_Sub := (Node (Priv_Elmt));
15054
15055 -- We avoid flipping the subtype if the Etype of its full view is
15056 -- private because this would result in a malformed subtype. This
15057 -- occurs when the Etype of the subtype full view is the full view of
15058 -- the base type (and since the base types were just switched, the
15059 -- subtype is pointing to the wrong view). This is currently the case
15060 -- for tagged record types, access types (maybe more?) and needs to
15061 -- be resolved. ???
15062
15063 if Present (Full_View (Priv_Sub))
15064 and then not Is_Private_Type (Etype (Full_View (Priv_Sub)))
15065 then
15066 Prepend_Elmt (Full_View (Priv_Sub), Exchanged_Views);
15067 Exchange_Declarations (Priv_Sub);
15068 end if;
15069
15070 Next_Elmt (Priv_Elmt);
15071 end loop;
15072 end Switch_View;
15073
15074 -----------------
15075 -- True_Parent --
15076 -----------------
15077
15078 function True_Parent (N : Node_Id) return Node_Id is
15079 begin
15080 if Nkind (Parent (N)) = N_Subunit then
15081 return Parent (Corresponding_Stub (Parent (N)));
15082 else
15083 return Parent (N);
15084 end if;
15085 end True_Parent;
15086
15087 -----------------------------
15088 -- Valid_Default_Attribute --
15089 -----------------------------
15090
15091 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id) is
15092 Attr_Id : constant Attribute_Id :=
15093 Get_Attribute_Id (Attribute_Name (Def));
15094 T : constant Entity_Id := Entity (Prefix (Def));
15095 Is_Fun : constant Boolean := (Ekind (Nam) = E_Function);
15096 F : Entity_Id;
15097 Num_F : Int;
15098 OK : Boolean;
15099
15100 begin
15101 if No (T) or else T = Any_Id then
15102 return;
15103 end if;
15104
15105 Num_F := 0;
15106 F := First_Formal (Nam);
15107 while Present (F) loop
15108 Num_F := Num_F + 1;
15109 Next_Formal (F);
15110 end loop;
15111
15112 case Attr_Id is
15113 when Attribute_Adjacent | Attribute_Ceiling | Attribute_Copy_Sign |
15114 Attribute_Floor | Attribute_Fraction | Attribute_Machine |
15115 Attribute_Model | Attribute_Remainder | Attribute_Rounding |
15116 Attribute_Unbiased_Rounding =>
15117 OK := Is_Fun
15118 and then Num_F = 1
15119 and then Is_Floating_Point_Type (T);
15120
15121 when Attribute_Image | Attribute_Pred | Attribute_Succ |
15122 Attribute_Value | Attribute_Wide_Image |
15123 Attribute_Wide_Value =>
15124 OK := (Is_Fun and then Num_F = 1 and then Is_Scalar_Type (T));
15125
15126 when Attribute_Max | Attribute_Min =>
15127 OK := (Is_Fun and then Num_F = 2 and then Is_Scalar_Type (T));
15128
15129 when Attribute_Input =>
15130 OK := (Is_Fun and then Num_F = 1);
15131
15132 when Attribute_Output | Attribute_Read | Attribute_Write =>
15133 OK := (not Is_Fun and then Num_F = 2);
15134
15135 when others =>
15136 OK := False;
15137 end case;
15138
15139 if not OK then
15140 Error_Msg_N
15141 ("attribute reference has wrong profile for subprogram", Def);
15142 end if;
15143 end Valid_Default_Attribute;
15144
15145 end Sem_Ch12;