[Ada] Fix bogus visibility error with partially parameterized formal package
[official-gcc.git] / gcc / ada / sem_ch12.adb
blobd0bafc6e06f0cfb3e8b37297fc275ab9b0ab2baa
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-2022, 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 ------------------------------------------------------------------------------
26 with Aspects; use Aspects;
27 with Atree; use Atree;
28 with Contracts; use Contracts;
29 with Einfo; use Einfo;
30 with Einfo.Entities; use Einfo.Entities;
31 with Einfo.Utils; use Einfo.Utils;
32 with Elists; use Elists;
33 with Errout; use Errout;
34 with Expander; use Expander;
35 with Fname; use Fname;
36 with Fname.UF; use Fname.UF;
37 with Freeze; use Freeze;
38 with Ghost; use Ghost;
39 with Itypes; use Itypes;
40 with Lib; use Lib;
41 with Lib.Load; use Lib.Load;
42 with Lib.Xref; use Lib.Xref;
43 with Nlists; use Nlists;
44 with Namet; use Namet;
45 with Nmake; use Nmake;
46 with Opt; use Opt;
47 with Rident; use Rident;
48 with Restrict; use Restrict;
49 with Rtsfind; use Rtsfind;
50 with Sem; use Sem;
51 with Sem_Aux; use Sem_Aux;
52 with Sem_Cat; use Sem_Cat;
53 with Sem_Ch3; use Sem_Ch3;
54 with Sem_Ch6; use Sem_Ch6;
55 with Sem_Ch7; use Sem_Ch7;
56 with Sem_Ch8; use Sem_Ch8;
57 with Sem_Ch10; use Sem_Ch10;
58 with Sem_Ch13; use Sem_Ch13;
59 with Sem_Dim; use Sem_Dim;
60 with Sem_Disp; use Sem_Disp;
61 with Sem_Elab; use Sem_Elab;
62 with Sem_Elim; use Sem_Elim;
63 with Sem_Eval; use Sem_Eval;
64 with Sem_Prag; use Sem_Prag;
65 with Sem_Res; use Sem_Res;
66 with Sem_Type; use Sem_Type;
67 with Sem_Util; use Sem_Util;
68 with Sem_Warn; use Sem_Warn;
69 with Stand; use Stand;
70 with Sinfo; use Sinfo;
71 with Sinfo.Nodes; use Sinfo.Nodes;
72 with Sinfo.Utils; use Sinfo.Utils;
73 with Sinfo.CN; use Sinfo.CN;
74 with Sinput; use Sinput;
75 with Sinput.L; use Sinput.L;
76 with Snames; use Snames;
77 with Stringt; use Stringt;
78 with Uname; use Uname;
79 with Table;
80 with Tbuild; use Tbuild;
81 with Uintp; use Uintp;
82 with Urealp; use Urealp;
83 with Warnsw; use Warnsw;
85 with GNAT.HTable;
87 package body Sem_Ch12 is
89 ----------------------------------------------------------
90 -- Implementation of Generic Analysis and Instantiation --
91 ----------------------------------------------------------
93 -- GNAT implements generics by macro expansion. No attempt is made to share
94 -- generic instantiations (for now). Analysis of a generic definition does
95 -- not perform any expansion action, but the expander must be called on the
96 -- tree for each instantiation, because the expansion may of course depend
97 -- on the generic actuals. All of this is best achieved as follows:
99 -- a) Semantic analysis of a generic unit is performed on a copy of the
100 -- tree for the generic unit. All tree modifications that follow analysis
101 -- do not affect the original tree. Links are kept between the original
102 -- tree and the copy, in order to recognize non-local references within
103 -- the generic, and propagate them to each instance (recall that name
104 -- resolution is done on the generic declaration: generics are not really
105 -- macros). This is summarized in the following diagram:
107 -- .-----------. .----------.
108 -- | semantic |<--------------| generic |
109 -- | copy | | unit |
110 -- | |==============>| |
111 -- |___________| global |__________|
112 -- references | | |
113 -- | | |
114 -- .-----|--|.
115 -- | .-----|---.
116 -- | | .----------.
117 -- | | | generic |
118 -- |__| | |
119 -- |__| instance |
120 -- |__________|
122 -- b) Each instantiation copies the original tree, and inserts into it a
123 -- series of declarations that describe the mapping between generic formals
124 -- and actuals. For example, a generic In OUT parameter is an object
125 -- renaming of the corresponding actual, etc. Generic IN parameters are
126 -- constant declarations.
128 -- c) In order to give the right visibility for these renamings, we use
129 -- a different scheme for package and subprogram instantiations. For
130 -- packages, the list of renamings is inserted into the package
131 -- specification, before the visible declarations of the package. The
132 -- renamings are analyzed before any of the text of the instance, and are
133 -- thus visible at the right place. Furthermore, outside of the instance,
134 -- the generic parameters are visible and denote their corresponding
135 -- actuals.
137 -- For subprograms, we create a container package to hold the renamings
138 -- and the subprogram instance itself. Analysis of the package makes the
139 -- renaming declarations visible to the subprogram. After analyzing the
140 -- package, the defining entity for the subprogram is touched-up so that
141 -- it appears declared in the current scope, and not inside the container
142 -- package.
144 -- If the instantiation is a compilation unit, the container package is
145 -- given the same name as the subprogram instance. This ensures that
146 -- the elaboration procedure called by the binder, using the compilation
147 -- unit name, calls in fact the elaboration procedure for the package.
149 -- Not surprisingly, private types complicate this approach. By saving in
150 -- the original generic object the non-local references, we guarantee that
151 -- the proper entities are referenced at the point of instantiation.
152 -- However, for private types, this by itself does not insure that the
153 -- proper VIEW of the entity is used (the full type may be visible at the
154 -- point of generic definition, but not at instantiation, or vice-versa).
155 -- In order to reference the proper view, we special-case any reference
156 -- to private types in the generic object, by saving both views, one in
157 -- the generic and one in the semantic copy. At time of instantiation, we
158 -- check whether the two views are consistent, and exchange declarations if
159 -- necessary, in order to restore the correct visibility. Similarly, if
160 -- the instance view is private when the generic view was not, we perform
161 -- the exchange. After completing the instantiation, we restore the
162 -- current visibility. The flag Has_Private_View marks identifiers in the
163 -- the generic unit that require checking.
165 -- Visibility within nested generic units requires special handling.
166 -- Consider the following scheme:
168 -- type Global is ... -- outside of generic unit.
169 -- generic ...
170 -- package Outer is
171 -- ...
172 -- type Semi_Global is ... -- global to inner.
174 -- generic ... -- 1
175 -- procedure inner (X1 : Global; X2 : Semi_Global);
177 -- procedure in2 is new inner (...); -- 4
178 -- end Outer;
180 -- package New_Outer is new Outer (...); -- 2
181 -- procedure New_Inner is new New_Outer.Inner (...); -- 3
183 -- The semantic analysis of Outer captures all occurrences of Global.
184 -- The semantic analysis of Inner (at 1) captures both occurrences of
185 -- Global and Semi_Global.
187 -- At point 2 (instantiation of Outer), we also produce a generic copy
188 -- of Inner, even though Inner is, at that point, not being instantiated.
189 -- (This is just part of the semantic analysis of New_Outer).
191 -- Critically, references to Global within Inner must be preserved, while
192 -- references to Semi_Global should not preserved, because they must now
193 -- resolve to an entity within New_Outer. To distinguish between these, we
194 -- use a global variable, Current_Instantiated_Parent, which is set when
195 -- performing a generic copy during instantiation (at 2). This variable is
196 -- used when performing a generic copy that is not an instantiation, but
197 -- that is nested within one, as the occurrence of 1 within 2. The analysis
198 -- of a nested generic only preserves references that are global to the
199 -- enclosing Current_Instantiated_Parent. We use the Scope_Depth value to
200 -- determine whether a reference is external to the given parent.
202 -- The instantiation at point 3 requires no special treatment. The method
203 -- works as well for further nestings of generic units, but of course the
204 -- variable Current_Instantiated_Parent must be stacked because nested
205 -- instantiations can occur, e.g. the occurrence of 4 within 2.
207 -- The instantiation of package and subprogram bodies is handled in a
208 -- similar manner, except that it is delayed until after semantic
209 -- analysis is complete. In this fashion complex cross-dependencies
210 -- between several package declarations and bodies containing generics
211 -- can be compiled which otherwise would diagnose spurious circularities.
213 -- For example, it is possible to compile two packages A and B that
214 -- have the following structure:
216 -- package A is package B is
217 -- generic ... generic ...
218 -- package G_A is package G_B is
220 -- with B; with A;
221 -- package body A is package body B is
222 -- package N_B is new G_B (..) package N_A is new G_A (..)
224 -- The table Pending_Instantiations in package Inline is used to keep
225 -- track of body instantiations that are delayed in this manner. Inline
226 -- handles the actual calls to do the body instantiations. This activity
227 -- is part of Inline, since the processing occurs at the same point, and
228 -- for essentially the same reason, as the handling of inlined routines.
230 ----------------------------------------------
231 -- Detection of Instantiation Circularities --
232 ----------------------------------------------
234 -- If we have a chain of instantiations that is circular, this is static
235 -- error which must be detected at compile time. The detection of these
236 -- circularities is carried out at the point that we insert a generic
237 -- instance spec or body. If there is a circularity, then the analysis of
238 -- the offending spec or body will eventually result in trying to load the
239 -- same unit again, and we detect this problem as we analyze the package
240 -- instantiation for the second time.
242 -- At least in some cases after we have detected the circularity, we get
243 -- into trouble if we try to keep going. The following flag is set if a
244 -- circularity is detected, and used to abandon compilation after the
245 -- messages have been posted.
247 Circularity_Detected : Boolean := False;
248 -- It should really be reset upon encountering a new main unit, but in
249 -- practice we do not use multiple main units so this is not critical.
251 -----------------------------------------
252 -- Implementation of Generic Contracts --
253 -----------------------------------------
255 -- A "contract" is a collection of aspects and pragmas that either verify a
256 -- property of a construct at runtime or classify the data flow to and from
257 -- the construct in some fashion.
259 -- Generic packages, subprograms and their respective bodies may be subject
260 -- to the following contract-related aspects or pragmas collectively known
261 -- as annotations:
263 -- package subprogram [body]
264 -- Abstract_State Contract_Cases
265 -- Initial_Condition Depends
266 -- Initializes Extensions_Visible
267 -- Global
268 -- package body Post
269 -- Refined_State Post_Class
270 -- Postcondition
271 -- Pre
272 -- Pre_Class
273 -- Precondition
274 -- Refined_Depends
275 -- Refined_Global
276 -- Refined_Post
277 -- Subprogram_Variant
278 -- Test_Case
280 -- Most package contract annotations utilize forward references to classify
281 -- data declared within the package [body]. Subprogram annotations then use
282 -- the classifications to further refine them. These inter dependencies are
283 -- problematic with respect to the implementation of generics because their
284 -- analysis, capture of global references and instantiation does not mesh
285 -- well with the existing mechanism.
287 -- 1) Analysis of generic contracts is carried out the same way non-generic
288 -- contracts are analyzed:
290 -- 1.1) General rule - a contract is analyzed after all related aspects
291 -- and pragmas are analyzed. This is done by routines
293 -- Analyze_Package_Body_Contract
294 -- Analyze_Package_Contract
295 -- Analyze_Subprogram_Body_Contract
296 -- Analyze_Subprogram_Contract
298 -- 1.2) Compilation unit - the contract is analyzed after Pragmas_After
299 -- are processed.
301 -- 1.3) Compilation unit body - the contract is analyzed at the end of
302 -- the body declaration list.
304 -- 1.4) Package - the contract is analyzed at the end of the private or
305 -- visible declarations, prior to analyzing the contracts of any nested
306 -- packages or subprograms.
308 -- 1.5) Package body - the contract is analyzed at the end of the body
309 -- declaration list, prior to analyzing the contracts of any nested
310 -- packages or subprograms.
312 -- 1.6) Subprogram - if the subprogram is declared inside a block, a
313 -- package or a subprogram, then its contract is analyzed at the end of
314 -- the enclosing declarations, otherwise the subprogram is a compilation
315 -- unit 1.2).
317 -- 1.7) Subprogram body - if the subprogram body is declared inside a
318 -- block, a package body or a subprogram body, then its contract is
319 -- analyzed at the end of the enclosing declarations, otherwise the
320 -- subprogram is a compilation unit 1.3).
322 -- 2) Capture of global references within contracts is done after capturing
323 -- global references within the generic template. There are two reasons for
324 -- this delay - pragma annotations are not part of the generic template in
325 -- the case of a generic subprogram declaration, and analysis of contracts
326 -- is delayed.
328 -- Contract-related source pragmas within generic templates are prepared
329 -- for delayed capture of global references by routine
331 -- Create_Generic_Contract
333 -- The routine associates these pragmas with the contract of the template.
334 -- In the case of a generic subprogram declaration, the routine creates
335 -- generic templates for the pragmas declared after the subprogram because
336 -- they are not part of the template.
338 -- generic -- template starts
339 -- procedure Gen_Proc (Input : Integer); -- template ends
340 -- pragma Precondition (Input > 0); -- requires own template
342 -- 2.1) The capture of global references with aspect specifications and
343 -- source pragmas that apply to a generic unit must be suppressed when
344 -- the generic template is being processed because the contracts have not
345 -- been analyzed yet. Any attempts to capture global references at that
346 -- point will destroy the Associated_Node linkages and leave the template
347 -- undecorated. This delay is controlled by routine
349 -- Requires_Delayed_Save
351 -- 2.2) The real capture of global references within a contract is done
352 -- after the contract has been analyzed, by routine
354 -- Save_Global_References_In_Contract
356 -- 3) The instantiation of a generic contract occurs as part of the
357 -- instantiation of the contract owner. Generic subprogram declarations
358 -- require additional processing when the contract is specified by pragmas
359 -- because the pragmas are not part of the generic template. This is done
360 -- by routine
362 -- Instantiate_Subprogram_Contract
364 --------------------------------------------------
365 -- Formal packages and partial parameterization --
366 --------------------------------------------------
368 -- When compiling a generic, a formal package is a local instantiation. If
369 -- declared with a box, its generic formals are visible in the enclosing
370 -- generic. If declared with a partial list of actuals, those actuals that
371 -- are defaulted (covered by an Others clause, or given an explicit box
372 -- initialization) are also visible in the enclosing generic, while those
373 -- that have a corresponding actual are not.
375 -- In our source model of instantiation, the same visibility must be
376 -- present in the spec and body of an instance: the names of the formals
377 -- that are defaulted must be made visible within the instance, and made
378 -- invisible (hidden) after the instantiation is complete, so that they
379 -- are not accessible outside of the instance.
381 -- In a generic, a formal package is treated like a special instantiation.
382 -- Our Ada 95 compiler handled formals with and without box in different
383 -- ways. With partial parameterization, we use a single model for both.
384 -- We create a package declaration that consists of the specification of
385 -- the generic package, and a set of declarations that map the actuals
386 -- into local renamings, just as we do for bona fide instantiations. For
387 -- defaulted parameters and formals with a box, we copy directly the
388 -- declarations of the formals into this local package. The result is a
389 -- package whose visible declarations may include generic formals. This
390 -- package is only used for type checking and visibility analysis, and
391 -- never reaches the back end, so it can freely violate the placement
392 -- rules for generic formal declarations.
394 -- The list of declarations (renamings and copies of formals) is built
395 -- by Analyze_Associations, just as for regular instantiations.
397 -- At the point of instantiation, conformance checking must be applied only
398 -- to those parameters that were specified in the formals. We perform this
399 -- checking by creating another internal instantiation, this one including
400 -- only the renamings and the formals (the rest of the package spec is not
401 -- relevant to conformance checking). We can then traverse two lists: the
402 -- list of actuals in the instance that corresponds to the formal package,
403 -- and the list of actuals produced for this bogus instantiation. We apply
404 -- the conformance rules to those actuals that are not defaulted, i.e.
405 -- which still appear as generic formals.
407 -- When we compile an instance body we must make the right parameters
408 -- visible again. The predicate Is_Generic_Formal indicates which of the
409 -- formals should have its Is_Hidden flag reset.
411 -----------------------
412 -- Local subprograms --
413 -----------------------
415 procedure Abandon_Instantiation (N : Node_Id);
416 pragma No_Return (Abandon_Instantiation);
417 -- Posts an error message "instantiation abandoned" at the indicated node
418 -- and then raises the exception Instantiation_Error to do it.
420 procedure Analyze_Formal_Array_Type
421 (T : in out Entity_Id;
422 Def : Node_Id);
423 -- A formal array type is treated like an array type declaration, and
424 -- invokes Array_Type_Declaration (sem_ch3) whose first parameter is
425 -- in-out, because in the case of an anonymous type the entity is
426 -- actually created in the procedure.
428 -- The following procedures treat other kinds of formal parameters
430 procedure Analyze_Formal_Derived_Interface_Type
431 (N : Node_Id;
432 T : Entity_Id;
433 Def : Node_Id);
435 procedure Analyze_Formal_Derived_Type
436 (N : Node_Id;
437 T : Entity_Id;
438 Def : Node_Id);
440 procedure Analyze_Formal_Interface_Type
441 (N : Node_Id;
442 T : Entity_Id;
443 Def : Node_Id);
445 -- The following subprograms create abbreviated declarations for formal
446 -- scalar types. We introduce an anonymous base of the proper class for
447 -- each of them, and define the formals as constrained first subtypes of
448 -- their bases. The bounds are expressions that are non-static in the
449 -- generic.
451 procedure Analyze_Formal_Decimal_Fixed_Point_Type
452 (T : Entity_Id; Def : Node_Id);
453 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id);
454 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id);
455 procedure Analyze_Formal_Signed_Integer_Type (T : Entity_Id; Def : Node_Id);
456 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id);
457 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
458 (T : Entity_Id; Def : Node_Id);
460 procedure Analyze_Formal_Private_Type
461 (N : Node_Id;
462 T : Entity_Id;
463 Def : Node_Id);
464 -- Creates a new private type, which does not require completion
466 procedure Analyze_Formal_Incomplete_Type (T : Entity_Id; Def : Node_Id);
467 -- Ada 2012: Creates a new incomplete type whose actual does not freeze
469 procedure Analyze_Generic_Formal_Part (N : Node_Id);
470 -- Analyze generic formal part
472 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id);
473 -- Create a new access type with the given designated type
475 function Analyze_Associations
476 (I_Node : Node_Id;
477 Formals : List_Id;
478 F_Copy : List_Id) return List_Id;
479 -- At instantiation time, build the list of associations between formals
480 -- and actuals. Each association becomes a renaming declaration for the
481 -- formal entity. F_Copy is the analyzed list of formals in the generic
482 -- copy. It is used to apply legality checks to the actuals. I_Node is the
483 -- instantiation node itself.
485 procedure Analyze_Subprogram_Instantiation
486 (N : Node_Id;
487 K : Entity_Kind);
489 procedure Build_Instance_Compilation_Unit_Nodes
490 (N : Node_Id;
491 Act_Body : Node_Id;
492 Act_Decl : Node_Id);
493 -- This procedure is used in the case where the generic instance of a
494 -- subprogram body or package body is a library unit. In this case, the
495 -- original library unit node for the generic instantiation must be
496 -- replaced by the resulting generic body, and a link made to a new
497 -- compilation unit node for the generic declaration. The argument N is
498 -- the original generic instantiation. Act_Body and Act_Decl are the body
499 -- and declaration of the instance (either package body and declaration
500 -- nodes or subprogram body and declaration nodes depending on the case).
501 -- On return, the node N has been rewritten with the actual body.
503 function Build_Subprogram_Decl_Wrapper
504 (Formal_Subp : Entity_Id) return Node_Id;
505 -- Ada 2022 allows formal subprograms to carry pre/postconditions.
506 -- At the point of instantiation these contracts apply to uses of
507 -- the actual subprogram. This is implemented by creating wrapper
508 -- subprograms instead of the renamings previously used to link
509 -- formal subprograms and the corresponding actuals. If the actual
510 -- is not an entity (e.g. an attribute reference) a renaming is
511 -- created to handle the expansion of the attribute.
513 function Build_Subprogram_Body_Wrapper
514 (Formal_Subp : Entity_Id;
515 Actual_Name : Node_Id) return Node_Id;
516 -- The body of the wrapper is a call to the actual, with the generated
517 -- pre/postconditon checks added.
519 procedure Check_Abbreviated_Instance
520 (N : Node_Id;
521 Parent_Installed : in out Boolean);
522 -- If the name of the generic unit in an abbreviated instantiation is an
523 -- expanded name, then the prefix may be an instance and the selector may
524 -- designate a child unit. If the parent is installed as a result of this
525 -- call, then Parent_Installed is set True, otherwise Parent_Installed is
526 -- unchanged by the call.
528 -- This routine needs to be called for declaration nodes of formal objects,
529 -- types and subprograms to check whether they are the copy, present in the
530 -- visible part of the abbreviated instantiation of formal packages, of the
531 -- declaration node of their corresponding formal parameter in the template
532 -- of the formal package, as specified by RM 12.7(10/2), so as to establish
533 -- the proper context for their analysis.
535 procedure Check_Access_Definition (N : Node_Id);
536 -- Subsidiary routine to null exclusion processing. Perform an assertion
537 -- check on Ada version and the presence of an access definition in N.
539 procedure Check_Formal_Packages (P_Id : Entity_Id);
540 -- Apply the following to all formal packages in generic associations.
541 -- Restore the visibility of the formals of the instance that are not
542 -- defaulted (see RM 12.7 (10)). Remove the anonymous package declaration
543 -- created for formal instances that are not defaulted.
545 procedure Check_Formal_Package_Instance
546 (Formal_Pack : Entity_Id;
547 Actual_Pack : Entity_Id);
548 -- Verify that the actuals of the actual instance match the actuals of
549 -- the template for a formal package that is not declared with a box.
551 procedure Check_Forward_Instantiation (Decl : Node_Id);
552 -- If the generic is a local entity and the corresponding body has not
553 -- been seen yet, flag enclosing packages to indicate that it will be
554 -- elaborated after the generic body. Subprograms declared in the same
555 -- package cannot be inlined by the front end because front-end inlining
556 -- requires a strict linear order of elaboration.
558 function Check_Hidden_Primitives (Assoc_List : List_Id) return Elist_Id;
559 -- Check if some association between formals and actuals requires to make
560 -- visible primitives of a tagged type, and make those primitives visible.
561 -- Return the list of primitives whose visibility is modified (to restore
562 -- their visibility later through Restore_Hidden_Primitives). If no
563 -- candidate is found then return No_Elist.
565 procedure Check_Hidden_Child_Unit
566 (N : Node_Id;
567 Gen_Unit : Entity_Id;
568 Act_Decl_Id : Entity_Id);
569 -- If the generic unit is an implicit child instance within a parent
570 -- instance, we need to make an explicit test that it is not hidden by
571 -- a child instance of the same name and parent.
573 procedure Check_Generic_Actuals
574 (Instance : Entity_Id;
575 Is_Formal_Box : Boolean);
576 -- Similar to previous one. Check the actuals in the instantiation,
577 -- whose views can change between the point of instantiation and the point
578 -- of instantiation of the body. In addition, mark the generic renamings
579 -- as generic actuals, so that they are not compatible with other actuals.
580 -- Recurse on an actual that is a formal package whose declaration has
581 -- a box.
583 function Contains_Instance_Of
584 (Inner : Entity_Id;
585 Outer : Entity_Id;
586 N : Node_Id) return Boolean;
587 -- Inner is instantiated within the generic Outer. Check whether Inner
588 -- directly or indirectly contains an instance of Outer or of one of its
589 -- parents, in the case of a subunit. Each generic unit holds a list of
590 -- the entities instantiated within (at any depth). This procedure
591 -- determines whether the set of such lists contains a cycle, i.e. an
592 -- illegal circular instantiation.
594 function Denotes_Formal_Package
595 (Pack : Entity_Id;
596 On_Exit : Boolean := False;
597 Instance : Entity_Id := Empty) return Boolean;
598 -- Returns True if E is a formal package of an enclosing generic, or
599 -- the actual for such a formal in an enclosing instantiation. If such
600 -- a package is used as a formal in an nested generic, or as an actual
601 -- in a nested instantiation, the visibility of ITS formals should not
602 -- be modified. When called from within Restore_Private_Views, the flag
603 -- On_Exit is true, to indicate that the search for a possible enclosing
604 -- instance should ignore the current one. In that case Instance denotes
605 -- the declaration for which this is an actual. This declaration may be
606 -- an instantiation in the source, or the internal instantiation that
607 -- corresponds to the actual for a formal package.
609 function Earlier (N1, N2 : Node_Id) return Boolean;
610 -- Yields True if N1 and N2 appear in the same compilation unit,
611 -- ignoring subunits, and if N1 is to the left of N2 in a left-to-right
612 -- traversal of the tree for the unit. Used to determine the placement
613 -- of freeze nodes for instance bodies that may depend on other instances.
615 function Find_Actual_Type
616 (Typ : Entity_Id;
617 Gen_Type : Entity_Id) return Entity_Id;
618 -- When validating the actual types of a child instance, check whether
619 -- the formal is a formal type of the parent unit, and retrieve the current
620 -- actual for it. Typ is the entity in the analyzed formal type declaration
621 -- (component or index type of an array type, or designated type of an
622 -- access formal) and Gen_Type is the enclosing analyzed formal array
623 -- or access type. The desired actual may be a formal of a parent, or may
624 -- be declared in a formal package of a parent. In both cases it is a
625 -- generic actual type because it appears within a visible instance.
626 -- Finally, it may be declared in a parent unit without being a formal
627 -- of that unit, in which case it must be retrieved by visibility.
628 -- Ambiguities may still arise if two homonyms are declared in two formal
629 -- packages, and the prefix of the formal type may be needed to resolve
630 -- the ambiguity in the instance ???
632 procedure Freeze_Package_Instance
633 (N : Node_Id;
634 Gen_Body : Node_Id;
635 Gen_Decl : Node_Id;
636 Act_Id : Entity_Id);
637 -- If the instantiation happens textually before the body of the generic,
638 -- the instantiation of the body must be analyzed after the generic body,
639 -- and not at the point of instantiation. Such early instantiations can
640 -- happen if the generic and the instance appear in a package declaration
641 -- because the generic body can only appear in the corresponding package
642 -- body. Early instantiations can also appear if generic, instance and
643 -- body are all in the declarative part of a subprogram or entry. Entities
644 -- of packages that are early instantiations are delayed, and their freeze
645 -- node appears after the generic body. This rather complex machinery is
646 -- needed when nested instantiations are present, because the source does
647 -- not carry any indication of where the corresponding instance bodies must
648 -- be installed and frozen.
650 procedure Freeze_Subprogram_Instance
651 (N : Node_Id;
652 Gen_Body : Node_Id;
653 Pack_Id : Entity_Id);
654 -- The generic body may appear textually after the instance, including
655 -- in the proper body of a stub, or within a different package instance.
656 -- Given that the instance can only be elaborated after the generic, we
657 -- place freeze nodes for the instance and/or for packages that may enclose
658 -- the instance and the generic, so that the back-end can establish the
659 -- proper order of elaboration.
661 function Get_Associated_Node (N : Node_Id) return Node_Id;
662 -- In order to propagate semantic information back from the analyzed copy
663 -- to the original generic, we maintain links between selected nodes in the
664 -- generic and their corresponding copies. At the end of generic analysis,
665 -- the routine Save_Global_References traverses the generic tree, examines
666 -- the semantic information, and preserves the links to those nodes that
667 -- contain global information. At instantiation, the information from the
668 -- associated node is placed on the new copy, so that name resolution is
669 -- not repeated.
671 -- Three kinds of source nodes have associated nodes:
673 -- a) those that can reference (denote) entities, that is identifiers,
674 -- character literals, expanded_names, operator symbols, operators,
675 -- and attribute reference nodes. These nodes have an Entity field
676 -- and are the set of nodes that are in N_Has_Entity.
678 -- b) aggregates (N_Aggregate and N_Extension_Aggregate)
680 -- c) selected components (N_Selected_Component)
682 -- For the first class, the associated node preserves the entity if it is
683 -- global. If the generic contains nested instantiations, the associated
684 -- node itself has been recopied, and a chain of them must be followed.
686 -- For aggregates, the associated node allows retrieval of the type, which
687 -- may otherwise not appear in the generic. The view of this type may be
688 -- different between generic and instantiation, and the full view can be
689 -- installed before the instantiation is analyzed. For aggregates of type
690 -- extensions, the same view exchange may have to be performed for some of
691 -- the ancestor types, if their view is private at the point of
692 -- instantiation.
694 -- Nodes that are selected components in the parse tree may be rewritten
695 -- as expanded names after resolution, and must be treated as potential
696 -- entity holders, which is why they also have an Associated_Node.
698 -- Nodes that do not come from source, such as freeze nodes, do not appear
699 -- in the generic tree, and need not have an associated node.
701 -- The associated node is stored in the Associated_Node field. Note that
702 -- this field overlaps Entity, which is fine, because the whole point is
703 -- that we don't need or want the normal Entity field in this situation.
705 function Has_Been_Exchanged (E : Entity_Id) return Boolean;
706 -- Traverse the Exchanged_Views list to see if a type was private
707 -- and has already been flipped during this phase of instantiation.
709 function Has_Contracts (Decl : Node_Id) return Boolean;
710 -- Determine whether a formal subprogram has a Pre- or Postcondition,
711 -- in which case a subprogram wrapper has to be built for the actual.
713 procedure Hide_Current_Scope;
714 -- When instantiating a generic child unit, the parent context must be
715 -- present, but the instance and all entities that may be generated
716 -- must be inserted in the current scope. We leave the current scope
717 -- on the stack, but make its entities invisible to avoid visibility
718 -- problems. This is reversed at the end of the instantiation. This is
719 -- not done for the instantiation of the bodies, which only require the
720 -- instances of the generic parents to be in scope.
722 function In_Main_Context (E : Entity_Id) return Boolean;
723 -- Check whether an instantiation is in the context of the main unit.
724 -- Used to determine whether its body should be elaborated to allow
725 -- front-end inlining.
727 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id);
728 -- Add the context clause of the unit containing a generic unit to a
729 -- compilation unit that is, or contains, an instantiation.
731 procedure Init_Env;
732 -- Establish environment for subsequent instantiation. Separated from
733 -- Save_Env because data-structures for visibility handling must be
734 -- initialized before call to Check_Generic_Child_Unit.
736 procedure Inline_Instance_Body
737 (N : Node_Id;
738 Gen_Unit : Entity_Id;
739 Act_Decl : Node_Id);
740 -- If front-end inlining is requested, instantiate the package body,
741 -- and preserve the visibility of its compilation unit, to insure
742 -- that successive instantiations succeed.
744 procedure Insert_Freeze_Node_For_Instance
745 (N : Node_Id;
746 F_Node : Node_Id);
747 -- N denotes a package or a subprogram instantiation and F_Node is the
748 -- associated freeze node. Insert the freeze node before the first source
749 -- body which follows immediately after N. If no such body is found, the
750 -- freeze node is inserted at the end of the declarative region which
751 -- contains N, unless the instantiation is done in a package spec that is
752 -- not at library level, in which case it is inserted at the outer level.
753 -- This can also be invoked to insert the freeze node of a package that
754 -- encloses an instantiation, in which case N may denote an arbitrary node.
756 procedure Install_Formal_Packages (Par : Entity_Id);
757 -- Install the visible part of any formal of the parent that is a formal
758 -- package. Note that for the case of a formal package with a box, this
759 -- includes the formal part of the formal package (12.7(10/2)).
761 procedure Install_Hidden_Primitives
762 (Prims_List : in out Elist_Id;
763 Gen_T : Entity_Id;
764 Act_T : Entity_Id);
765 -- Remove suffix 'P' from hidden primitives of Act_T to match the
766 -- visibility of primitives of Gen_T. The list of primitives to which
767 -- the suffix is removed is added to Prims_List to restore them later.
769 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False);
770 -- When compiling an instance of a child unit the parent (which is
771 -- itself an instance) is an enclosing scope that must be made
772 -- immediately visible. This procedure is also used to install the non-
773 -- generic parent of a generic child unit when compiling its body, so
774 -- that full views of types in the parent are made visible.
776 -- The functions Instantiate_XXX perform various legality checks and build
777 -- the declarations for instantiated generic parameters. In all of these
778 -- Formal is the entity in the generic unit, Actual is the entity of
779 -- expression in the generic associations, and Analyzed_Formal is the
780 -- formal in the generic copy, which contains the semantic information to
781 -- be used to validate the actual.
783 function Instantiate_Object
784 (Formal : Node_Id;
785 Actual : Node_Id;
786 Analyzed_Formal : Node_Id) return List_Id;
788 function Instantiate_Type
789 (Formal : Node_Id;
790 Actual : Node_Id;
791 Analyzed_Formal : Node_Id;
792 Actual_Decls : List_Id) return List_Id;
794 function Instantiate_Formal_Subprogram
795 (Formal : Node_Id;
796 Actual : Node_Id;
797 Analyzed_Formal : Node_Id) return Node_Id;
799 function Instantiate_Formal_Package
800 (Formal : Node_Id;
801 Actual : Node_Id;
802 Analyzed_Formal : Node_Id) return List_Id;
803 -- If the formal package is declared with a box, special visibility rules
804 -- apply to its formals: they are in the visible part of the package. This
805 -- is true in the declarative region of the formal package, that is to say
806 -- in the enclosing generic or instantiation. For an instantiation, the
807 -- parameters of the formal package are made visible in an explicit step.
808 -- Furthermore, if the actual has a visible USE clause, these formals must
809 -- be made potentially use-visible as well. On exit from the enclosing
810 -- instantiation, the reverse must be done.
812 -- For a formal package declared without a box, there are conformance rules
813 -- that apply to the actuals in the generic declaration and the actuals of
814 -- the actual package in the enclosing instantiation. The simplest way to
815 -- apply these rules is to repeat the instantiation of the formal package
816 -- in the context of the enclosing instance, and compare the generic
817 -- associations of this instantiation with those of the actual package.
818 -- This internal instantiation only needs to contain the renamings of the
819 -- formals: the visible and private declarations themselves need not be
820 -- created.
822 -- In Ada 2005, the formal package may be only partially parameterized.
823 -- In that case the visibility step must make visible those actuals whose
824 -- corresponding formals were given with a box. A final complication
825 -- involves inherited operations from formal derived types, which must
826 -- be visible if the type is.
828 function Is_In_Main_Unit (N : Node_Id) return Boolean;
829 -- Test if given node is in the main unit
831 procedure Load_Parent_Of_Generic
832 (N : Node_Id;
833 Spec : Node_Id;
834 Body_Optional : Boolean := False);
835 -- If the generic appears in a separate non-generic library unit, load the
836 -- corresponding body to retrieve the body of the generic. N is the node
837 -- for the generic instantiation, Spec is the generic package declaration.
839 -- Body_Optional is a flag that indicates that the body is being loaded to
840 -- ensure that temporaries are generated consistently when there are other
841 -- instances in the current declarative part that precede the one being
842 -- loaded. In that case a missing body is acceptable.
844 procedure Map_Formal_Package_Entities (Form : Entity_Id; Act : Entity_Id);
845 -- Within the generic part, entities in the formal package are
846 -- visible. To validate subsequent type declarations, indicate
847 -- the correspondence between the entities in the analyzed formal,
848 -- and the entities in the actual package. There are three packages
849 -- involved in the instantiation of a formal package: the parent
850 -- generic P1 which appears in the generic declaration, the fake
851 -- instantiation P2 which appears in the analyzed generic, and whose
852 -- visible entities may be used in subsequent formals, and the actual
853 -- P3 in the instance. To validate subsequent formals, me indicate
854 -- that the entities in P2 are mapped into those of P3. The mapping of
855 -- entities has to be done recursively for nested packages.
857 procedure Move_Freeze_Nodes
858 (Out_Of : Entity_Id;
859 After : Node_Id;
860 L : List_Id);
861 -- Freeze nodes can be generated in the analysis of a generic unit, but
862 -- will not be seen by the back-end. It is necessary to move those nodes
863 -- to the enclosing scope if they freeze an outer entity. We place them
864 -- at the end of the enclosing generic package, which is semantically
865 -- neutral.
867 procedure Preanalyze_Actuals (N : Node_Id; Inst : Entity_Id := Empty);
868 -- Analyze actuals to perform name resolution. Full resolution is done
869 -- later, when the expected types are known, but names have to be captured
870 -- before installing parents of generics, that are not visible for the
871 -- actuals themselves.
873 -- If Inst is present, it is the entity of the package instance. This
874 -- entity is marked as having a limited_view actual when some actual is
875 -- a limited view. This is used to place the instance body properly.
877 procedure Provide_Completing_Bodies (N : Node_Id);
878 -- Generate completing bodies for all subprograms found within package or
879 -- subprogram declaration N.
881 procedure Remove_Parent (In_Body : Boolean := False);
882 -- Reverse effect after instantiation of child is complete
884 function Requires_Conformance_Checking (N : Node_Id) return Boolean;
885 -- Determine whether the formal package declaration N requires conformance
886 -- checking with actuals in instantiations.
888 procedure Restore_Hidden_Primitives (Prims_List : in out Elist_Id);
889 -- Restore suffix 'P' to primitives of Prims_List and leave Prims_List
890 -- set to No_Elist.
892 procedure Set_Instance_Env
893 (Gen_Unit : Entity_Id;
894 Act_Unit : Entity_Id);
895 -- Save current instance on saved environment, to be used to determine
896 -- the global status of entities in nested instances. Part of Save_Env.
897 -- called after verifying that the generic unit is legal for the instance,
898 -- The procedure also examines whether the generic unit is a predefined
899 -- unit, in order to set configuration switches accordingly. As a result
900 -- the procedure must be called after analyzing and freezing the actuals.
902 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id);
903 -- Associate analyzed generic parameter with corresponding instance. Used
904 -- for semantic checks at instantiation time.
906 function True_Parent (N : Node_Id) return Node_Id;
907 -- For a subunit, return parent of corresponding stub, else return
908 -- parent of node.
910 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id);
911 -- Verify that an attribute that appears as the default for a formal
912 -- subprogram is a function or procedure with the correct profile.
914 procedure Validate_Formal_Type_Default (Decl : Node_Id);
915 -- Ada_2022 AI12-205: if a default subtype_mark is present, verify
916 -- that it is the name of a type in the same class as the formal.
917 -- The treatment parallels what is done in Instantiate_Type but differs
918 -- in a few ways so that this machinery cannot be reused as is: on one
919 -- hand there are no visibility issues for a default, because it is
920 -- analyzed in the same context as the formal type definition; on the
921 -- other hand the check needs to take into acount the use of a previous
922 -- formal type in the current formal type definition (see details in
923 -- AI12-0205).
925 -------------------------------------------
926 -- Data Structures for Generic Renamings --
927 -------------------------------------------
929 -- The map Generic_Renamings associates generic entities with their
930 -- corresponding actuals. Currently used to validate type instances. It
931 -- will eventually be used for all generic parameters to eliminate the
932 -- need for overload resolution in the instance.
934 type Assoc_Ptr is new Int;
936 Assoc_Null : constant Assoc_Ptr := -1;
938 type Assoc is record
939 Gen_Id : Entity_Id;
940 Act_Id : Entity_Id;
941 Next_In_HTable : Assoc_Ptr;
942 end record;
944 package Generic_Renamings is new Table.Table
945 (Table_Component_Type => Assoc,
946 Table_Index_Type => Assoc_Ptr,
947 Table_Low_Bound => 0,
948 Table_Initial => 10,
949 Table_Increment => 100,
950 Table_Name => "Generic_Renamings");
952 -- Variable to hold enclosing instantiation. When the environment is
953 -- saved for a subprogram inlining, the corresponding Act_Id is empty.
955 Current_Instantiated_Parent : Assoc := (Empty, Empty, Assoc_Null);
957 -- Hash table for associations
959 HTable_Size : constant := 37;
960 type HTable_Range is range 0 .. HTable_Size - 1;
962 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr);
963 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr;
964 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id;
965 function Hash (F : Entity_Id) return HTable_Range;
967 package Generic_Renamings_HTable is new GNAT.HTable.Static_HTable (
968 Header_Num => HTable_Range,
969 Element => Assoc,
970 Elmt_Ptr => Assoc_Ptr,
971 Null_Ptr => Assoc_Null,
972 Set_Next => Set_Next_Assoc,
973 Next => Next_Assoc,
974 Key => Entity_Id,
975 Get_Key => Get_Gen_Id,
976 Hash => Hash,
977 Equal => "=");
979 Exchanged_Views : Elist_Id;
980 -- This list holds the private views that have been exchanged during
981 -- instantiation to restore the visibility of the generic declaration.
982 -- (see comments above). After instantiation, the current visibility is
983 -- reestablished by means of a traversal of this list.
985 Hidden_Entities : Elist_Id;
986 -- This list holds the entities of the current scope that are removed
987 -- from immediate visibility when instantiating a child unit. Their
988 -- visibility is restored in Remove_Parent.
990 -- Because instantiations can be recursive, the following must be saved
991 -- on entry and restored on exit from an instantiation (spec or body).
992 -- This is done by the two procedures Save_Env and Restore_Env. For
993 -- package and subprogram instantiations (but not for the body instances)
994 -- the action of Save_Env is done in two steps: Init_Env is called before
995 -- Check_Generic_Child_Unit, because setting the parent instances requires
996 -- that the visibility data structures be properly initialized. Once the
997 -- generic is unit is validated, Set_Instance_Env completes Save_Env.
999 Parent_Unit_Visible : Boolean := False;
1000 -- Parent_Unit_Visible is used when the generic is a child unit, and
1001 -- indicates whether the ultimate parent of the generic is visible in the
1002 -- instantiation environment. It is used to reset the visibility of the
1003 -- parent at the end of the instantiation (see Remove_Parent).
1005 Instance_Parent_Unit : Entity_Id := Empty;
1006 -- This records the ultimate parent unit of an instance of a generic
1007 -- child unit and is used in conjunction with Parent_Unit_Visible to
1008 -- indicate the unit to which the Parent_Unit_Visible flag corresponds.
1010 type Instance_Env is record
1011 Instantiated_Parent : Assoc;
1012 Exchanged_Views : Elist_Id;
1013 Hidden_Entities : Elist_Id;
1014 Current_Sem_Unit : Unit_Number_Type;
1015 Parent_Unit_Visible : Boolean := False;
1016 Instance_Parent_Unit : Entity_Id := Empty;
1017 Switches : Config_Switches_Type;
1018 end record;
1020 package Instance_Envs is new Table.Table (
1021 Table_Component_Type => Instance_Env,
1022 Table_Index_Type => Int,
1023 Table_Low_Bound => 0,
1024 Table_Initial => 32,
1025 Table_Increment => 100,
1026 Table_Name => "Instance_Envs");
1028 procedure Restore_Private_Views
1029 (Pack_Id : Entity_Id;
1030 Is_Package : Boolean := True);
1031 -- Restore the private views of external types, and unmark the generic
1032 -- renamings of actuals, so that they become compatible subtypes again.
1033 -- For subprograms, Pack_Id is the package constructed to hold the
1034 -- renamings.
1036 procedure Switch_View (T : Entity_Id);
1037 -- Switch the partial and full views of a type and its private
1038 -- dependents (i.e. its subtypes and derived types).
1040 ------------------------------------
1041 -- Structures for Error Reporting --
1042 ------------------------------------
1044 Instantiation_Node : Node_Id;
1045 -- Used by subprograms that validate instantiation of formal parameters
1046 -- where there might be no actual on which to place the error message.
1047 -- Also used to locate the instantiation node for generic subunits.
1049 Instantiation_Error : exception;
1050 -- When there is a semantic error in the generic parameter matching,
1051 -- there is no point in continuing the instantiation, because the
1052 -- number of cascaded errors is unpredictable. This exception aborts
1053 -- the instantiation process altogether.
1055 S_Adjustment : Sloc_Adjustment;
1056 -- Offset created for each node in an instantiation, in order to keep
1057 -- track of the source position of the instantiation in each of its nodes.
1058 -- A subsequent semantic error or warning on a construct of the instance
1059 -- points to both places: the original generic node, and the point of
1060 -- instantiation. See Sinput and Sinput.L for additional details.
1062 ------------------------------------------------------------
1063 -- Data structure for keeping track when inside a Generic --
1064 ------------------------------------------------------------
1066 -- The following table is used to save values of the Inside_A_Generic
1067 -- flag (see spec of Sem) when they are saved by Start_Generic.
1069 package Generic_Flags is new Table.Table (
1070 Table_Component_Type => Boolean,
1071 Table_Index_Type => Int,
1072 Table_Low_Bound => 0,
1073 Table_Initial => 32,
1074 Table_Increment => 200,
1075 Table_Name => "Generic_Flags");
1077 ---------------------------
1078 -- Abandon_Instantiation --
1079 ---------------------------
1081 procedure Abandon_Instantiation (N : Node_Id) is
1082 begin
1083 Error_Msg_N ("\instantiation abandoned!", N);
1084 raise Instantiation_Error;
1085 end Abandon_Instantiation;
1087 ----------------------------------
1088 -- Adjust_Inherited_Pragma_Sloc --
1089 ----------------------------------
1091 procedure Adjust_Inherited_Pragma_Sloc (N : Node_Id) is
1092 begin
1093 Adjust_Instantiation_Sloc (N, S_Adjustment);
1094 end Adjust_Inherited_Pragma_Sloc;
1096 --------------------------
1097 -- Analyze_Associations --
1098 --------------------------
1100 function Analyze_Associations
1101 (I_Node : Node_Id;
1102 Formals : List_Id;
1103 F_Copy : List_Id) return List_Id
1105 Actuals_To_Freeze : constant Elist_Id := New_Elmt_List;
1106 Assoc_List : constant List_Id := New_List;
1107 Default_Actuals : constant List_Id := New_List;
1108 Gen_Unit : constant Entity_Id :=
1109 Defining_Entity (Parent (F_Copy));
1111 Actuals : List_Id;
1112 Actual : Node_Id;
1113 Analyzed_Formal : Node_Id;
1114 First_Named : Node_Id := Empty;
1115 Formal : Node_Id;
1116 Match : Node_Id;
1117 Named : Node_Id;
1118 Saved_Formal : Node_Id;
1120 Default_Formals : constant List_Id := New_List;
1121 -- If an Others_Choice is present, some of the formals may be defaulted.
1122 -- To simplify the treatment of visibility in an instance, we introduce
1123 -- individual defaults for each such formal. These defaults are
1124 -- appended to the list of associations and replace the Others_Choice.
1126 Found_Assoc : Node_Id;
1127 -- Association for the current formal being match. Empty if there are
1128 -- no remaining actuals, or if there is no named association with the
1129 -- name of the formal.
1131 Is_Named_Assoc : Boolean;
1132 Num_Matched : Nat := 0;
1133 Num_Actuals : Nat := 0;
1135 Others_Present : Boolean := False;
1136 Others_Choice : Node_Id := Empty;
1137 -- In Ada 2005, indicates partial parameterization of a formal
1138 -- package. As usual an other association must be last in the list.
1140 procedure Build_Subprogram_Wrappers;
1141 -- Ada 2022: AI12-0272 introduces pre/postconditions for formal
1142 -- subprograms. The implementation of making the formal into a renaming
1143 -- of the actual does not work, given that subprogram renaming cannot
1144 -- carry aspect specifications. Instead we must create subprogram
1145 -- wrappers whose body is a call to the actual, and whose declaration
1146 -- carries the aspects of the formal.
1148 procedure Check_Fixed_Point_Actual (Actual : Node_Id);
1149 -- Warn if an actual fixed-point type has user-defined arithmetic
1150 -- operations, but there is no corresponding formal in the generic,
1151 -- in which case the predefined operations will be used. This merits
1152 -- a warning because of the special semantics of fixed point ops.
1154 procedure Check_Overloaded_Formal_Subprogram (Formal : Entity_Id);
1155 -- Apply RM 12.3(9): if a formal subprogram is overloaded, the instance
1156 -- cannot have a named association for it. AI05-0025 extends this rule
1157 -- to formals of formal packages by AI05-0025, and it also applies to
1158 -- box-initialized formals.
1160 function Has_Fully_Defined_Profile (Subp : Entity_Id) return Boolean;
1161 -- Determine whether the parameter types and the return type of Subp
1162 -- are fully defined at the point of instantiation.
1164 function Matching_Actual
1165 (F : Entity_Id;
1166 A_F : Entity_Id) return Node_Id;
1167 -- Find actual that corresponds to a given a formal parameter. If the
1168 -- actuals are positional, return the next one, if any. If the actuals
1169 -- are named, scan the parameter associations to find the right one.
1170 -- A_F is the corresponding entity in the analyzed generic, which is
1171 -- placed on the selector name.
1173 -- In Ada 2005, a named association may be given with a box, in which
1174 -- case Matching_Actual sets Found_Assoc to the generic association,
1175 -- but return Empty for the actual itself. In this case the code below
1176 -- creates a corresponding declaration for the formal.
1178 function Partial_Parameterization return Boolean;
1179 -- Ada 2005: if no match is found for a given formal, check if the
1180 -- association for it includes a box, or whether the associations
1181 -- include an Others clause.
1183 procedure Process_Default (Formal : Node_Id);
1184 -- Add a copy of the declaration of a generic formal to the list of
1185 -- associations, and add an explicit box association for its entity
1186 -- if there is none yet, and the default comes from an Others_Choice.
1188 function Renames_Standard_Subprogram (Subp : Entity_Id) return Boolean;
1189 -- Determine whether Subp renames one of the subprograms defined in the
1190 -- generated package Standard.
1192 procedure Set_Analyzed_Formal;
1193 -- Find the node in the generic copy that corresponds to a given formal.
1194 -- The semantic information on this node is used to perform legality
1195 -- checks on the actuals. Because semantic analysis can introduce some
1196 -- anonymous entities or modify the declaration node itself, the
1197 -- correspondence between the two lists is not one-one. In addition to
1198 -- anonymous types, the presence a formal equality will introduce an
1199 -- implicit declaration for the corresponding inequality.
1201 -------------------------------
1202 -- Build_Subprogram_Wrappers --
1203 -------------------------------
1205 procedure Build_Subprogram_Wrappers is
1206 Formal : constant Entity_Id :=
1207 Defining_Unit_Name (Specification (Analyzed_Formal));
1208 Aspect_Spec : Node_Id;
1209 Decl_Node : Node_Id;
1210 Actual_Name : Node_Id;
1212 begin
1213 -- Create declaration for wrapper subprogram
1214 -- The actual can be overloaded, in which case it will be
1215 -- resolved when the call in the wrapper body is analyzed.
1216 -- We attach the possible interpretations of the actual to
1217 -- the name to be used in the call in the wrapper body.
1219 if Is_Entity_Name (Match) then
1220 Actual_Name := New_Occurrence_Of (Entity (Match), Sloc (Match));
1222 if Is_Overloaded (Match) then
1223 Save_Interps (Match, Actual_Name);
1224 end if;
1226 else
1227 -- Use renaming declaration created when analyzing actual.
1228 -- This may be incomplete if there are several formal
1229 -- subprograms whose actual is an attribute ???
1231 declare
1232 Renaming_Decl : constant Node_Id := Last (Assoc_List);
1234 begin
1235 Actual_Name := New_Occurrence_Of
1236 (Defining_Entity (Renaming_Decl), Sloc (Match));
1237 Set_Etype (Actual_Name, Get_Instance_Of (Etype (Formal)));
1238 end;
1239 end if;
1241 Decl_Node := Build_Subprogram_Decl_Wrapper (Formal);
1243 -- Transfer aspect specifications from formal subprogram to wrapper
1245 Set_Aspect_Specifications (Decl_Node,
1246 New_Copy_List_Tree (Aspect_Specifications (Analyzed_Formal)));
1248 Aspect_Spec := First (Aspect_Specifications (Decl_Node));
1249 while Present (Aspect_Spec) loop
1250 Set_Analyzed (Aspect_Spec, False);
1251 Next (Aspect_Spec);
1252 end loop;
1254 Append_To (Assoc_List, Decl_Node);
1256 -- Create corresponding body, and append it to association list
1257 -- that appears at the head of the declarations in the instance.
1258 -- The subprogram may be called in the analysis of subsequent
1259 -- actuals.
1261 Append_To (Assoc_List,
1262 Build_Subprogram_Body_Wrapper (Formal, Actual_Name));
1263 end Build_Subprogram_Wrappers;
1265 ----------------------------------------
1266 -- Check_Overloaded_Formal_Subprogram --
1267 ----------------------------------------
1269 procedure Check_Overloaded_Formal_Subprogram (Formal : Entity_Id) is
1270 Temp_Formal : Entity_Id;
1272 begin
1273 Temp_Formal := First (Formals);
1274 while Present (Temp_Formal) loop
1275 if Nkind (Temp_Formal) in N_Formal_Subprogram_Declaration
1276 and then Temp_Formal /= Formal
1277 and then
1278 Chars (Defining_Unit_Name (Specification (Formal))) =
1279 Chars (Defining_Unit_Name (Specification (Temp_Formal)))
1280 then
1281 if Present (Found_Assoc) then
1282 Error_Msg_N
1283 ("named association not allowed for overloaded formal",
1284 Found_Assoc);
1286 else
1287 Error_Msg_N
1288 ("named association not allowed for overloaded formal",
1289 Others_Choice);
1290 end if;
1292 Abandon_Instantiation (Instantiation_Node);
1293 end if;
1295 Next (Temp_Formal);
1296 end loop;
1297 end Check_Overloaded_Formal_Subprogram;
1299 -------------------------------
1300 -- Check_Fixed_Point_Actual --
1301 -------------------------------
1303 procedure Check_Fixed_Point_Actual (Actual : Node_Id) is
1304 Typ : constant Entity_Id := Entity (Actual);
1305 Prims : constant Elist_Id := Collect_Primitive_Operations (Typ);
1306 Elem : Elmt_Id;
1307 Formal : Node_Id;
1308 Op : Entity_Id;
1310 begin
1311 -- Locate primitive operations of the type that are arithmetic
1312 -- operations.
1314 Elem := First_Elmt (Prims);
1315 while Present (Elem) loop
1316 if Nkind (Node (Elem)) = N_Defining_Operator_Symbol then
1318 -- Check whether the generic unit has a formal subprogram of
1319 -- the same name. This does not check types but is good enough
1320 -- to justify a warning.
1322 Formal := First_Non_Pragma (Formals);
1323 Op := Alias (Node (Elem));
1325 while Present (Formal) loop
1326 if Nkind (Formal) = N_Formal_Concrete_Subprogram_Declaration
1327 and then Chars (Defining_Entity (Formal)) =
1328 Chars (Node (Elem))
1329 then
1330 exit;
1332 elsif Nkind (Formal) = N_Formal_Package_Declaration then
1333 declare
1334 Assoc : Node_Id;
1335 Ent : Entity_Id;
1337 begin
1338 -- Locate corresponding actual, and check whether it
1339 -- includes a fixed-point type.
1341 Assoc := First (Assoc_List);
1342 while Present (Assoc) loop
1343 exit when
1344 Nkind (Assoc) = N_Package_Renaming_Declaration
1345 and then Chars (Defining_Unit_Name (Assoc)) =
1346 Chars (Defining_Identifier (Formal));
1348 Next (Assoc);
1349 end loop;
1351 if Present (Assoc) then
1353 -- If formal package declares a fixed-point type,
1354 -- and the user-defined operator is derived from
1355 -- a generic instance package, the fixed-point type
1356 -- does not use the corresponding predefined op.
1358 Ent := First_Entity (Entity (Name (Assoc)));
1359 while Present (Ent) loop
1360 if Is_Fixed_Point_Type (Ent)
1361 and then Present (Op)
1362 and then Is_Generic_Instance (Scope (Op))
1363 then
1364 return;
1365 end if;
1367 Next_Entity (Ent);
1368 end loop;
1369 end if;
1370 end;
1371 end if;
1373 Next (Formal);
1374 end loop;
1376 if No (Formal) then
1377 Error_Msg_Sloc := Sloc (Node (Elem));
1378 Error_Msg_NE
1379 ("?instance uses predefined operation, not primitive "
1380 & "operation&#", Actual, Node (Elem));
1381 end if;
1382 end if;
1384 Next_Elmt (Elem);
1385 end loop;
1386 end Check_Fixed_Point_Actual;
1388 -------------------------------
1389 -- Has_Fully_Defined_Profile --
1390 -------------------------------
1392 function Has_Fully_Defined_Profile (Subp : Entity_Id) return Boolean is
1393 function Is_Fully_Defined_Type (Typ : Entity_Id) return Boolean;
1394 -- Determine whethet type Typ is fully defined
1396 ---------------------------
1397 -- Is_Fully_Defined_Type --
1398 ---------------------------
1400 function Is_Fully_Defined_Type (Typ : Entity_Id) return Boolean is
1401 begin
1402 -- A private type without a full view is not fully defined
1404 if Is_Private_Type (Typ)
1405 and then No (Full_View (Typ))
1406 then
1407 return False;
1409 -- An incomplete type is never fully defined
1411 elsif Is_Incomplete_Type (Typ) then
1412 return False;
1414 -- All other types are fully defined
1416 else
1417 return True;
1418 end if;
1419 end Is_Fully_Defined_Type;
1421 -- Local declarations
1423 Param : Entity_Id;
1425 -- Start of processing for Has_Fully_Defined_Profile
1427 begin
1428 -- Check the parameters
1430 Param := First_Formal (Subp);
1431 while Present (Param) loop
1432 if not Is_Fully_Defined_Type (Etype (Param)) then
1433 return False;
1434 end if;
1436 Next_Formal (Param);
1437 end loop;
1439 -- Check the return type
1441 return Is_Fully_Defined_Type (Etype (Subp));
1442 end Has_Fully_Defined_Profile;
1444 ---------------------
1445 -- Matching_Actual --
1446 ---------------------
1448 function Matching_Actual
1449 (F : Entity_Id;
1450 A_F : Entity_Id) return Node_Id
1452 Prev : Node_Id;
1453 Act : Node_Id;
1455 begin
1456 Is_Named_Assoc := False;
1458 -- End of list of purely positional parameters
1460 if No (Actual) or else Nkind (Actual) = N_Others_Choice then
1461 Found_Assoc := Empty;
1462 Act := Empty;
1464 -- Case of positional parameter corresponding to current formal
1466 elsif No (Selector_Name (Actual)) then
1467 Found_Assoc := Actual;
1468 Act := Explicit_Generic_Actual_Parameter (Actual);
1469 Num_Matched := Num_Matched + 1;
1470 Next (Actual);
1472 -- Otherwise scan list of named actuals to find the one with the
1473 -- desired name. All remaining actuals have explicit names.
1475 else
1476 Is_Named_Assoc := True;
1477 Found_Assoc := Empty;
1478 Act := Empty;
1479 Prev := Empty;
1481 while Present (Actual) loop
1482 if Nkind (Actual) = N_Others_Choice then
1483 Found_Assoc := Empty;
1484 Act := Empty;
1486 elsif Chars (Selector_Name (Actual)) = Chars (F) then
1487 Set_Entity (Selector_Name (Actual), A_F);
1488 Set_Etype (Selector_Name (Actual), Etype (A_F));
1489 Generate_Reference (A_F, Selector_Name (Actual));
1491 Found_Assoc := Actual;
1492 Act := Explicit_Generic_Actual_Parameter (Actual);
1493 Num_Matched := Num_Matched + 1;
1494 exit;
1495 end if;
1497 Prev := Actual;
1498 Next (Actual);
1499 end loop;
1501 -- Reset for subsequent searches. In most cases the named
1502 -- associations are in order. If they are not, we reorder them
1503 -- to avoid scanning twice the same actual. This is not just a
1504 -- question of efficiency: there may be multiple defaults with
1505 -- boxes that have the same name. In a nested instantiation we
1506 -- insert actuals for those defaults, and cannot rely on their
1507 -- names to disambiguate them.
1509 if Actual = First_Named then
1510 Next (First_Named);
1512 elsif Present (Actual) then
1513 Insert_Before (First_Named, Remove_Next (Prev));
1514 end if;
1516 Actual := First_Named;
1517 end if;
1519 if Is_Entity_Name (Act) and then Present (Entity (Act)) then
1520 Set_Used_As_Generic_Actual (Entity (Act));
1521 end if;
1523 return Act;
1524 end Matching_Actual;
1526 ------------------------------
1527 -- Partial_Parameterization --
1528 ------------------------------
1530 function Partial_Parameterization return Boolean is
1531 begin
1532 return Others_Present
1533 or else (Present (Found_Assoc) and then Box_Present (Found_Assoc));
1534 end Partial_Parameterization;
1536 ---------------------
1537 -- Process_Default --
1538 ---------------------
1540 procedure Process_Default (Formal : Node_Id) is
1541 Loc : constant Source_Ptr := Sloc (I_Node);
1542 F_Id : constant Entity_Id := Defining_Entity (Formal);
1543 Decl : Node_Id;
1544 Default : Node_Id;
1545 Id : Entity_Id;
1547 begin
1548 -- Append copy of formal declaration to associations, and create new
1549 -- defining identifier for it.
1551 Decl := New_Copy_Tree (Formal);
1552 Id := Make_Defining_Identifier (Sloc (F_Id), Chars (F_Id));
1554 if Nkind (Formal) in N_Formal_Subprogram_Declaration then
1555 Set_Defining_Unit_Name (Specification (Decl), Id);
1557 else
1558 Set_Defining_Identifier (Decl, Id);
1559 end if;
1561 Append (Decl, Assoc_List);
1563 if No (Found_Assoc) then
1564 Default :=
1565 Make_Generic_Association (Loc,
1566 Selector_Name =>
1567 New_Occurrence_Of (Id, Loc),
1568 Explicit_Generic_Actual_Parameter => Empty);
1569 Set_Box_Present (Default);
1570 Append (Default, Default_Formals);
1571 end if;
1572 end Process_Default;
1574 ---------------------------------
1575 -- Renames_Standard_Subprogram --
1576 ---------------------------------
1578 function Renames_Standard_Subprogram (Subp : Entity_Id) return Boolean is
1579 Id : Entity_Id;
1581 begin
1582 Id := Alias (Subp);
1583 while Present (Id) loop
1584 if Scope (Id) = Standard_Standard then
1585 return True;
1586 end if;
1588 Id := Alias (Id);
1589 end loop;
1591 return False;
1592 end Renames_Standard_Subprogram;
1594 -------------------------
1595 -- Set_Analyzed_Formal --
1596 -------------------------
1598 procedure Set_Analyzed_Formal is
1599 Kind : Node_Kind;
1601 begin
1602 while Present (Analyzed_Formal) loop
1603 Kind := Nkind (Analyzed_Formal);
1605 case Nkind (Formal) is
1606 when N_Formal_Subprogram_Declaration =>
1607 exit when Kind in N_Formal_Subprogram_Declaration
1608 and then
1609 Chars
1610 (Defining_Unit_Name (Specification (Formal))) =
1611 Chars
1612 (Defining_Unit_Name (Specification (Analyzed_Formal)));
1614 when N_Formal_Package_Declaration =>
1615 exit when Kind in N_Formal_Package_Declaration
1616 | N_Generic_Package_Declaration
1617 | N_Package_Declaration;
1619 when N_Use_Package_Clause
1620 | N_Use_Type_Clause
1622 exit;
1624 when others =>
1626 -- Skip freeze nodes, and nodes inserted to replace
1627 -- unrecognized pragmas.
1629 exit when
1630 Kind not in N_Formal_Subprogram_Declaration
1631 and then Kind not in N_Subprogram_Declaration
1632 | N_Freeze_Entity
1633 | N_Null_Statement
1634 | N_Itype_Reference
1635 and then Chars (Defining_Identifier (Formal)) =
1636 Chars (Defining_Identifier (Analyzed_Formal));
1637 end case;
1639 Next (Analyzed_Formal);
1640 end loop;
1641 end Set_Analyzed_Formal;
1643 -- Start of processing for Analyze_Associations
1645 begin
1646 Actuals := Generic_Associations (I_Node);
1648 if Present (Actuals) then
1650 -- Check for an Others choice, indicating a partial parameterization
1651 -- for a formal package.
1653 Actual := First (Actuals);
1654 while Present (Actual) loop
1655 if Nkind (Actual) = N_Others_Choice then
1656 Others_Present := True;
1657 Others_Choice := Actual;
1659 if Present (Next (Actual)) then
1660 Error_Msg_N ("OTHERS must be last association", Actual);
1661 end if;
1663 -- This subprogram is used both for formal packages and for
1664 -- instantiations. For the latter, associations must all be
1665 -- explicit.
1667 if Nkind (I_Node) /= N_Formal_Package_Declaration
1668 and then Comes_From_Source (I_Node)
1669 then
1670 Error_Msg_N
1671 ("OTHERS association not allowed in an instance",
1672 Actual);
1673 end if;
1675 -- In any case, nothing to do after the others association
1677 exit;
1679 elsif Box_Present (Actual)
1680 and then Comes_From_Source (I_Node)
1681 and then Nkind (I_Node) /= N_Formal_Package_Declaration
1682 then
1683 Error_Msg_N
1684 ("box association not allowed in an instance", Actual);
1685 end if;
1687 Next (Actual);
1688 end loop;
1690 -- If named associations are present, save first named association
1691 -- (it may of course be Empty) to facilitate subsequent name search.
1693 First_Named := First (Actuals);
1694 while Present (First_Named)
1695 and then Nkind (First_Named) /= N_Others_Choice
1696 and then No (Selector_Name (First_Named))
1697 loop
1698 Num_Actuals := Num_Actuals + 1;
1699 Next (First_Named);
1700 end loop;
1701 end if;
1703 Named := First_Named;
1704 while Present (Named) loop
1705 if Nkind (Named) /= N_Others_Choice
1706 and then No (Selector_Name (Named))
1707 then
1708 Error_Msg_N ("invalid positional actual after named one", Named);
1709 Abandon_Instantiation (Named);
1710 end if;
1712 -- A named association may lack an actual parameter, if it was
1713 -- introduced for a default subprogram that turns out to be local
1714 -- to the outer instantiation. If it has a box association it must
1715 -- correspond to some formal in the generic.
1717 if Nkind (Named) /= N_Others_Choice
1718 and then (Present (Explicit_Generic_Actual_Parameter (Named))
1719 or else Box_Present (Named))
1720 then
1721 Num_Actuals := Num_Actuals + 1;
1722 end if;
1724 Next (Named);
1725 end loop;
1727 if Present (Formals) then
1728 Formal := First_Non_Pragma (Formals);
1729 Analyzed_Formal := First_Non_Pragma (F_Copy);
1731 if Present (Actuals) then
1732 Actual := First (Actuals);
1734 -- All formals should have default values
1736 else
1737 Actual := Empty;
1738 end if;
1740 while Present (Formal) loop
1741 Set_Analyzed_Formal;
1742 Saved_Formal := Next_Non_Pragma (Formal);
1744 case Nkind (Formal) is
1745 when N_Formal_Object_Declaration =>
1746 Match :=
1747 Matching_Actual
1748 (Defining_Identifier (Formal),
1749 Defining_Identifier (Analyzed_Formal));
1751 if No (Match) and then Partial_Parameterization then
1752 Process_Default (Formal);
1754 else
1755 Append_List
1756 (Instantiate_Object (Formal, Match, Analyzed_Formal),
1757 Assoc_List);
1759 -- For a defaulted in_parameter, create an entry in the
1760 -- the list of defaulted actuals, for GNATprove use. Do
1761 -- not included these defaults for an instance nested
1762 -- within a generic, because the defaults are also used
1763 -- in the analysis of the enclosing generic, and only
1764 -- defaulted subprograms are relevant there.
1766 if No (Match) and then not Inside_A_Generic then
1767 Append_To (Default_Actuals,
1768 Make_Generic_Association (Sloc (I_Node),
1769 Selector_Name =>
1770 New_Occurrence_Of
1771 (Defining_Identifier (Formal), Sloc (I_Node)),
1772 Explicit_Generic_Actual_Parameter =>
1773 New_Copy_Tree (Default_Expression (Formal))));
1774 end if;
1775 end if;
1777 -- If the object is a call to an expression function, this
1778 -- is a freezing point for it.
1780 if Is_Entity_Name (Match)
1781 and then Present (Entity (Match))
1782 and then Nkind
1783 (Original_Node (Unit_Declaration_Node (Entity (Match))))
1784 = N_Expression_Function
1785 then
1786 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1787 end if;
1789 when N_Formal_Type_Declaration =>
1790 Match :=
1791 Matching_Actual
1792 (Defining_Identifier (Formal),
1793 Defining_Identifier (Analyzed_Formal));
1795 if No (Match) then
1796 if Partial_Parameterization then
1797 Process_Default (Formal);
1799 elsif Present (Default_Subtype_Mark (Formal)) then
1800 Match := New_Copy (Default_Subtype_Mark (Formal));
1801 Append_List
1802 (Instantiate_Type
1803 (Formal, Match, Analyzed_Formal, Assoc_List),
1804 Assoc_List);
1805 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1807 else
1808 Error_Msg_Sloc := Sloc (Gen_Unit);
1809 Error_Msg_NE
1810 ("missing actual&",
1811 Instantiation_Node, Defining_Identifier (Formal));
1812 Error_Msg_NE
1813 ("\in instantiation of & declared#",
1814 Instantiation_Node, Gen_Unit);
1815 Abandon_Instantiation (Instantiation_Node);
1816 end if;
1818 else
1819 Analyze (Match);
1820 Append_List
1821 (Instantiate_Type
1822 (Formal, Match, Analyzed_Formal, Assoc_List),
1823 Assoc_List);
1825 -- Warn when an actual is a fixed-point with user-
1826 -- defined promitives. The warning is superfluous
1827 -- if the formal is private, because there can be
1828 -- no arithmetic operations in the generic so there
1829 -- no danger of confusion.
1831 if Is_Fixed_Point_Type (Entity (Match))
1832 and then not Is_Private_Type
1833 (Defining_Identifier (Analyzed_Formal))
1834 then
1835 Check_Fixed_Point_Actual (Match);
1836 end if;
1838 -- An instantiation is a freeze point for the actuals,
1839 -- unless this is a rewritten formal package, or the
1840 -- formal is an Ada 2012 formal incomplete type.
1842 if Nkind (I_Node) = N_Formal_Package_Declaration
1843 or else
1844 (Ada_Version >= Ada_2012
1845 and then
1846 Ekind (Defining_Identifier (Analyzed_Formal)) =
1847 E_Incomplete_Type)
1848 then
1849 null;
1851 else
1852 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1853 end if;
1854 end if;
1856 -- A remote access-to-class-wide type is not a legal actual
1857 -- for a generic formal of an access type (E.2.2(17/2)).
1858 -- In GNAT an exception to this rule is introduced when
1859 -- the formal is marked as remote using implementation
1860 -- defined aspect/pragma Remote_Access_Type. In that case
1861 -- the actual must be remote as well.
1863 -- If the current instantiation is the construction of a
1864 -- local copy for a formal package the actuals may be
1865 -- defaulted, and there is no matching actual to check.
1867 if Nkind (Analyzed_Formal) = N_Formal_Type_Declaration
1868 and then
1869 Nkind (Formal_Type_Definition (Analyzed_Formal)) =
1870 N_Access_To_Object_Definition
1871 and then Present (Match)
1872 then
1873 declare
1874 Formal_Ent : constant Entity_Id :=
1875 Defining_Identifier (Analyzed_Formal);
1876 begin
1877 if Is_Remote_Access_To_Class_Wide_Type (Entity (Match))
1878 = Is_Remote_Types (Formal_Ent)
1879 then
1880 -- Remoteness of formal and actual match
1882 null;
1884 elsif Is_Remote_Types (Formal_Ent) then
1886 -- Remote formal, non-remote actual
1888 Error_Msg_NE
1889 ("actual for& must be remote", Match, Formal_Ent);
1891 else
1892 -- Non-remote formal, remote actual
1894 Error_Msg_NE
1895 ("actual for& may not be remote",
1896 Match, Formal_Ent);
1897 end if;
1898 end;
1899 end if;
1901 when N_Formal_Subprogram_Declaration =>
1902 Match :=
1903 Matching_Actual
1904 (Defining_Unit_Name (Specification (Formal)),
1905 Defining_Unit_Name (Specification (Analyzed_Formal)));
1907 -- If the formal subprogram has the same name as another
1908 -- formal subprogram of the generic, then a named
1909 -- association is illegal (12.3(9)). Exclude named
1910 -- associations that are generated for a nested instance.
1912 if Present (Match)
1913 and then Is_Named_Assoc
1914 and then Comes_From_Source (Found_Assoc)
1915 then
1916 Check_Overloaded_Formal_Subprogram (Formal);
1917 end if;
1919 -- If there is no corresponding actual, this may be case
1920 -- of partial parameterization, or else the formal has a
1921 -- default or a box.
1923 if No (Match) and then Partial_Parameterization then
1924 Process_Default (Formal);
1926 if Nkind (I_Node) = N_Formal_Package_Declaration then
1927 Check_Overloaded_Formal_Subprogram (Formal);
1928 end if;
1930 else
1931 Append_To (Assoc_List,
1932 Instantiate_Formal_Subprogram
1933 (Formal, Match, Analyzed_Formal));
1935 -- If formal subprogram has contracts, create wrappers
1936 -- for it. This is an expansion activity that cannot
1937 -- take place e.g. within an enclosing generic unit.
1939 if Has_Contracts (Analyzed_Formal)
1940 and then Expander_Active
1941 then
1942 Build_Subprogram_Wrappers;
1943 end if;
1945 -- An instantiation is a freeze point for the actuals,
1946 -- unless this is a rewritten formal package.
1948 if Nkind (I_Node) /= N_Formal_Package_Declaration
1949 and then Nkind (Match) = N_Identifier
1950 and then Is_Subprogram (Entity (Match))
1952 -- The actual subprogram may rename a routine defined
1953 -- in Standard. Avoid freezing such renamings because
1954 -- subprograms coming from Standard cannot be frozen.
1956 and then
1957 not Renames_Standard_Subprogram (Entity (Match))
1959 -- If the actual subprogram comes from a different
1960 -- unit, it is already frozen, either by a body in
1961 -- that unit or by the end of the declarative part
1962 -- of the unit. This check avoids the freezing of
1963 -- subprograms defined in Standard which are used
1964 -- as generic actuals.
1966 and then In_Same_Code_Unit (Entity (Match), I_Node)
1967 and then Has_Fully_Defined_Profile (Entity (Match))
1968 then
1969 -- Mark the subprogram as having a delayed freeze
1970 -- since this may be an out-of-order action.
1972 Set_Has_Delayed_Freeze (Entity (Match));
1973 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1974 end if;
1975 end if;
1977 -- If this is a nested generic, preserve default for later
1978 -- instantiations. We do this as well for GNATprove use,
1979 -- so that the list of generic associations is complete.
1981 if No (Match) and then Box_Present (Formal) then
1982 declare
1983 Subp : constant Entity_Id :=
1984 Defining_Unit_Name
1985 (Specification (Last (Assoc_List)));
1987 begin
1988 Append_To (Default_Actuals,
1989 Make_Generic_Association (Sloc (I_Node),
1990 Selector_Name =>
1991 New_Occurrence_Of (Subp, Sloc (I_Node)),
1992 Explicit_Generic_Actual_Parameter =>
1993 New_Occurrence_Of (Subp, Sloc (I_Node))));
1994 end;
1995 end if;
1997 when N_Formal_Package_Declaration =>
1998 -- The name of the formal package may be hidden by the
1999 -- formal parameter itself.
2001 if Error_Posted (Analyzed_Formal) then
2002 Abandon_Instantiation (Instantiation_Node);
2004 else
2005 Match :=
2006 Matching_Actual
2007 (Defining_Identifier (Formal),
2008 Defining_Identifier
2009 (Original_Node (Analyzed_Formal)));
2010 end if;
2012 if No (Match) then
2013 if Partial_Parameterization then
2014 Process_Default (Formal);
2016 else
2017 Error_Msg_Sloc := Sloc (Gen_Unit);
2018 Error_Msg_NE
2019 ("missing actual&",
2020 Instantiation_Node, Defining_Identifier (Formal));
2021 Error_Msg_NE
2022 ("\in instantiation of & declared#",
2023 Instantiation_Node, Gen_Unit);
2025 Abandon_Instantiation (Instantiation_Node);
2026 end if;
2028 else
2029 Analyze (Match);
2030 Append_List
2031 (Instantiate_Formal_Package
2032 (Formal, Match, Analyzed_Formal),
2033 Assoc_List);
2035 -- Determine whether the actual package needs an explicit
2036 -- freeze node. This is only the case if the actual is
2037 -- declared in the same unit and has a body. Normally
2038 -- packages do not have explicit freeze nodes, and gigi
2039 -- only uses them to elaborate entities in a package
2040 -- body.
2042 Explicit_Freeze_Check : declare
2043 Actual : constant Entity_Id := Entity (Match);
2044 Gen_Par : Entity_Id;
2046 Needs_Freezing : Boolean;
2047 P : Node_Id;
2049 procedure Check_Generic_Parent;
2050 -- The actual may be an instantiation of a unit
2051 -- declared in a previous instantiation. If that
2052 -- one is also in the current compilation, it must
2053 -- itself be frozen before the actual. The actual
2054 -- may be an instantiation of a generic child unit,
2055 -- in which case the same applies to the instance
2056 -- of the parent which must be frozen before the
2057 -- actual.
2058 -- Should this itself be recursive ???
2060 --------------------------
2061 -- Check_Generic_Parent --
2062 --------------------------
2064 procedure Check_Generic_Parent is
2065 Inst : constant Node_Id :=
2066 Next (Unit_Declaration_Node (Actual));
2067 Par : Entity_Id;
2069 begin
2070 Par := Empty;
2072 if Nkind (Parent (Actual)) = N_Package_Specification
2073 then
2074 Par := Scope (Generic_Parent (Parent (Actual)));
2076 if Is_Generic_Instance (Par) then
2077 null;
2079 -- If the actual is a child generic unit, check
2080 -- whether the instantiation of the parent is
2081 -- also local and must also be frozen now. We
2082 -- must retrieve the instance node to locate the
2083 -- parent instance if any.
2085 elsif Ekind (Par) = E_Generic_Package
2086 and then Is_Child_Unit (Gen_Par)
2087 and then Ekind (Scope (Gen_Par)) =
2088 E_Generic_Package
2089 then
2090 if Nkind (Inst) = N_Package_Instantiation
2091 and then Nkind (Name (Inst)) =
2092 N_Expanded_Name
2093 then
2094 -- Retrieve entity of parent instance
2096 Par := Entity (Prefix (Name (Inst)));
2097 end if;
2099 else
2100 Par := Empty;
2101 end if;
2102 end if;
2104 if Present (Par)
2105 and then Is_Generic_Instance (Par)
2106 and then Scope (Par) = Current_Scope
2107 and then
2108 (No (Freeze_Node (Par))
2109 or else
2110 not Is_List_Member (Freeze_Node (Par)))
2111 then
2112 Set_Has_Delayed_Freeze (Par);
2113 Append_Elmt (Par, Actuals_To_Freeze);
2114 end if;
2115 end Check_Generic_Parent;
2117 -- Start of processing for Explicit_Freeze_Check
2119 begin
2120 if Present (Renamed_Entity (Actual)) then
2121 Gen_Par :=
2122 Generic_Parent (Specification
2123 (Unit_Declaration_Node
2124 (Renamed_Entity (Actual))));
2125 else
2126 Gen_Par :=
2127 Generic_Parent (Specification
2128 (Unit_Declaration_Node (Actual)));
2129 end if;
2131 if not Expander_Active
2132 or else not Has_Completion (Actual)
2133 or else not In_Same_Source_Unit (I_Node, Actual)
2134 or else Is_Frozen (Actual)
2135 or else
2136 (Present (Renamed_Entity (Actual))
2137 and then
2138 not In_Same_Source_Unit
2139 (I_Node, (Renamed_Entity (Actual))))
2140 then
2141 null;
2143 else
2144 -- Finally we want to exclude such freeze nodes
2145 -- from statement sequences, which freeze
2146 -- everything before them.
2147 -- Is this strictly necessary ???
2149 Needs_Freezing := True;
2151 P := Parent (I_Node);
2152 while Nkind (P) /= N_Compilation_Unit loop
2153 if Nkind (P) = N_Handled_Sequence_Of_Statements
2154 then
2155 Needs_Freezing := False;
2156 exit;
2157 end if;
2159 P := Parent (P);
2160 end loop;
2162 if Needs_Freezing then
2163 Check_Generic_Parent;
2165 -- If the actual is a renaming of a proper
2166 -- instance of the formal package, indicate
2167 -- that it is the instance that must be frozen.
2169 if Nkind (Parent (Actual)) =
2170 N_Package_Renaming_Declaration
2171 then
2172 Set_Has_Delayed_Freeze
2173 (Renamed_Entity (Actual));
2174 Append_Elmt
2175 (Renamed_Entity (Actual),
2176 Actuals_To_Freeze);
2177 else
2178 Set_Has_Delayed_Freeze (Actual);
2179 Append_Elmt (Actual, Actuals_To_Freeze);
2180 end if;
2181 end if;
2182 end if;
2183 end Explicit_Freeze_Check;
2184 end if;
2186 -- For use type and use package appearing in the generic part,
2187 -- we have already copied them, so we can just move them where
2188 -- they belong (we mustn't recopy them since this would mess up
2189 -- the Sloc values).
2191 when N_Use_Package_Clause
2192 | N_Use_Type_Clause
2194 if Nkind (Original_Node (I_Node)) =
2195 N_Formal_Package_Declaration
2196 then
2197 Append (New_Copy_Tree (Formal), Assoc_List);
2198 else
2199 Remove (Formal);
2200 Append (Formal, Assoc_List);
2201 end if;
2203 when others =>
2204 raise Program_Error;
2205 end case;
2207 Formal := Saved_Formal;
2208 Next_Non_Pragma (Analyzed_Formal);
2209 end loop;
2211 if Num_Actuals > Num_Matched then
2212 Error_Msg_Sloc := Sloc (Gen_Unit);
2214 if Present (Selector_Name (Actual)) then
2215 Error_Msg_NE
2216 ("unmatched actual &", Actual, Selector_Name (Actual));
2217 Error_Msg_NE
2218 ("\in instantiation of & declared#", Actual, Gen_Unit);
2219 else
2220 Error_Msg_NE
2221 ("unmatched actual in instantiation of & declared#",
2222 Actual, Gen_Unit);
2223 end if;
2224 end if;
2226 elsif Present (Actuals) then
2227 Error_Msg_N
2228 ("too many actuals in generic instantiation", Instantiation_Node);
2229 end if;
2231 -- An instantiation freezes all generic actuals. The only exceptions
2232 -- to this are incomplete types and subprograms which are not fully
2233 -- defined at the point of instantiation.
2235 declare
2236 Elmt : Elmt_Id := First_Elmt (Actuals_To_Freeze);
2237 begin
2238 while Present (Elmt) loop
2239 Freeze_Before (I_Node, Node (Elmt));
2240 Next_Elmt (Elmt);
2241 end loop;
2242 end;
2244 -- If there are default subprograms, normalize the tree by adding
2245 -- explicit associations for them. This is required if the instance
2246 -- appears within a generic.
2248 if not Is_Empty_List (Default_Actuals) then
2249 declare
2250 Default : Node_Id;
2252 begin
2253 Default := First (Default_Actuals);
2254 while Present (Default) loop
2255 Mark_Rewrite_Insertion (Default);
2256 Next (Default);
2257 end loop;
2259 if No (Actuals) then
2260 Set_Generic_Associations (I_Node, Default_Actuals);
2261 else
2262 Append_List_To (Actuals, Default_Actuals);
2263 end if;
2264 end;
2265 end if;
2267 -- If this is a formal package, normalize the parameter list by adding
2268 -- explicit box associations for the formals that are covered by an
2269 -- Others_Choice.
2271 Append_List (Default_Formals, Formals);
2273 return Assoc_List;
2274 end Analyze_Associations;
2276 -------------------------------
2277 -- Analyze_Formal_Array_Type --
2278 -------------------------------
2280 procedure Analyze_Formal_Array_Type
2281 (T : in out Entity_Id;
2282 Def : Node_Id)
2284 DSS : Node_Id;
2286 begin
2287 -- Treated like a non-generic array declaration, with additional
2288 -- semantic checks.
2290 Enter_Name (T);
2292 if Nkind (Def) = N_Constrained_Array_Definition then
2293 DSS := First (Discrete_Subtype_Definitions (Def));
2294 while Present (DSS) loop
2295 if Nkind (DSS) in N_Subtype_Indication
2296 | N_Range
2297 | N_Attribute_Reference
2298 then
2299 Error_Msg_N ("only a subtype mark is allowed in a formal", DSS);
2300 end if;
2302 Next (DSS);
2303 end loop;
2304 end if;
2306 Array_Type_Declaration (T, Def);
2307 Set_Is_Generic_Type (Base_Type (T));
2309 if Ekind (Component_Type (T)) = E_Incomplete_Type
2310 and then No (Full_View (Component_Type (T)))
2311 then
2312 Error_Msg_N ("premature usage of incomplete type", Def);
2314 -- Check that range constraint is not allowed on the component type
2315 -- of a generic formal array type (AARM 12.5.3(3))
2317 elsif Is_Internal (Component_Type (T))
2318 and then Present (Subtype_Indication (Component_Definition (Def)))
2319 and then Nkind (Original_Node
2320 (Subtype_Indication (Component_Definition (Def)))) =
2321 N_Subtype_Indication
2322 then
2323 Error_Msg_N
2324 ("in a formal, a subtype indication can only be "
2325 & "a subtype mark (RM 12.5.3(3))",
2326 Subtype_Indication (Component_Definition (Def)));
2327 end if;
2329 end Analyze_Formal_Array_Type;
2331 ---------------------------------------------
2332 -- Analyze_Formal_Decimal_Fixed_Point_Type --
2333 ---------------------------------------------
2335 -- As for other generic types, we create a valid type representation with
2336 -- legal but arbitrary attributes, whose values are never considered
2337 -- static. For all scalar types we introduce an anonymous base type, with
2338 -- the same attributes. We choose the corresponding integer type to be
2339 -- Standard_Integer.
2340 -- Here and in other similar routines, the Sloc of the generated internal
2341 -- type must be the same as the sloc of the defining identifier of the
2342 -- formal type declaration, to provide proper source navigation.
2344 procedure Analyze_Formal_Decimal_Fixed_Point_Type
2345 (T : Entity_Id;
2346 Def : Node_Id)
2348 Loc : constant Source_Ptr := Sloc (Def);
2350 Base : constant Entity_Id :=
2351 New_Internal_Entity
2352 (E_Decimal_Fixed_Point_Type,
2353 Current_Scope,
2354 Sloc (Defining_Identifier (Parent (Def))), 'G');
2356 Int_Base : constant Entity_Id := Standard_Integer;
2357 Delta_Val : constant Ureal := Ureal_1;
2358 Digs_Val : constant Uint := Uint_6;
2360 function Make_Dummy_Bound return Node_Id;
2361 -- Return a properly typed universal real literal to use as a bound
2363 ----------------------
2364 -- Make_Dummy_Bound --
2365 ----------------------
2367 function Make_Dummy_Bound return Node_Id is
2368 Bound : constant Node_Id := Make_Real_Literal (Loc, Ureal_1);
2369 begin
2370 Set_Etype (Bound, Universal_Real);
2371 return Bound;
2372 end Make_Dummy_Bound;
2374 -- Start of processing for Analyze_Formal_Decimal_Fixed_Point_Type
2376 begin
2377 Enter_Name (T);
2379 Set_Etype (Base, Base);
2380 Set_Size_Info (Base, Int_Base);
2381 Set_RM_Size (Base, RM_Size (Int_Base));
2382 Set_First_Rep_Item (Base, First_Rep_Item (Int_Base));
2383 Set_Digits_Value (Base, Digs_Val);
2384 Set_Delta_Value (Base, Delta_Val);
2385 Set_Small_Value (Base, Delta_Val);
2386 Set_Scalar_Range (Base,
2387 Make_Range (Loc,
2388 Low_Bound => Make_Dummy_Bound,
2389 High_Bound => Make_Dummy_Bound));
2391 Set_Is_Generic_Type (Base);
2392 Set_Parent (Base, Parent (Def));
2394 Mutate_Ekind (T, E_Decimal_Fixed_Point_Subtype);
2395 Set_Etype (T, Base);
2396 Set_Size_Info (T, Int_Base);
2397 Set_RM_Size (T, RM_Size (Int_Base));
2398 Set_First_Rep_Item (T, First_Rep_Item (Int_Base));
2399 Set_Digits_Value (T, Digs_Val);
2400 Set_Delta_Value (T, Delta_Val);
2401 Set_Small_Value (T, Delta_Val);
2402 Set_Scalar_Range (T, Scalar_Range (Base));
2403 Set_Is_Constrained (T);
2405 Check_Restriction (No_Fixed_Point, Def);
2406 end Analyze_Formal_Decimal_Fixed_Point_Type;
2408 -------------------------------------------
2409 -- Analyze_Formal_Derived_Interface_Type --
2410 -------------------------------------------
2412 procedure Analyze_Formal_Derived_Interface_Type
2413 (N : Node_Id;
2414 T : Entity_Id;
2415 Def : Node_Id)
2417 Loc : constant Source_Ptr := Sloc (Def);
2419 begin
2420 -- Rewrite as a type declaration of a derived type. This ensures that
2421 -- the interface list and primitive operations are properly captured.
2423 Rewrite (N,
2424 Make_Full_Type_Declaration (Loc,
2425 Defining_Identifier => T,
2426 Type_Definition => Def));
2427 Analyze (N);
2428 Set_Is_Generic_Type (T);
2429 end Analyze_Formal_Derived_Interface_Type;
2431 ---------------------------------
2432 -- Analyze_Formal_Derived_Type --
2433 ---------------------------------
2435 procedure Analyze_Formal_Derived_Type
2436 (N : Node_Id;
2437 T : Entity_Id;
2438 Def : Node_Id)
2440 Loc : constant Source_Ptr := Sloc (Def);
2441 Unk_Disc : constant Boolean := Unknown_Discriminants_Present (N);
2442 New_N : Node_Id;
2444 begin
2445 Set_Is_Generic_Type (T);
2447 if Private_Present (Def) then
2448 New_N :=
2449 Make_Private_Extension_Declaration (Loc,
2450 Defining_Identifier => T,
2451 Discriminant_Specifications => Discriminant_Specifications (N),
2452 Unknown_Discriminants_Present => Unk_Disc,
2453 Subtype_Indication => Subtype_Mark (Def),
2454 Interface_List => Interface_List (Def));
2456 Set_Abstract_Present (New_N, Abstract_Present (Def));
2457 Set_Limited_Present (New_N, Limited_Present (Def));
2458 Set_Synchronized_Present (New_N, Synchronized_Present (Def));
2460 else
2461 New_N :=
2462 Make_Full_Type_Declaration (Loc,
2463 Defining_Identifier => T,
2464 Discriminant_Specifications =>
2465 Discriminant_Specifications (Parent (T)),
2466 Type_Definition =>
2467 Make_Derived_Type_Definition (Loc,
2468 Subtype_Indication => Subtype_Mark (Def)));
2470 Set_Abstract_Present
2471 (Type_Definition (New_N), Abstract_Present (Def));
2472 Set_Limited_Present
2473 (Type_Definition (New_N), Limited_Present (Def));
2474 end if;
2476 Rewrite (N, New_N);
2477 Analyze (N);
2479 if Unk_Disc then
2480 if not Is_Composite_Type (T) then
2481 Error_Msg_N
2482 ("unknown discriminants not allowed for elementary types", N);
2483 else
2484 Set_Has_Unknown_Discriminants (T);
2485 Set_Is_Constrained (T, False);
2486 end if;
2487 end if;
2489 -- If the parent type has a known size, so does the formal, which makes
2490 -- legal representation clauses that involve the formal.
2492 Set_Size_Known_At_Compile_Time
2493 (T, Size_Known_At_Compile_Time (Entity (Subtype_Mark (Def))));
2494 end Analyze_Formal_Derived_Type;
2496 ----------------------------------
2497 -- Analyze_Formal_Discrete_Type --
2498 ----------------------------------
2500 -- The operations defined for a discrete types are those of an enumeration
2501 -- type. The size is set to an arbitrary value, for use in analyzing the
2502 -- generic unit.
2504 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id) is
2505 Loc : constant Source_Ptr := Sloc (Def);
2506 Lo : Node_Id;
2507 Hi : Node_Id;
2509 Base : constant Entity_Id :=
2510 New_Internal_Entity
2511 (E_Floating_Point_Type, Current_Scope,
2512 Sloc (Defining_Identifier (Parent (Def))), 'G');
2514 begin
2515 Enter_Name (T);
2516 Mutate_Ekind (T, E_Enumeration_Subtype);
2517 Set_Etype (T, Base);
2518 Init_Size (T, 8);
2519 Reinit_Alignment (T);
2520 Set_Is_Generic_Type (T);
2521 Set_Is_Constrained (T);
2523 -- For semantic analysis, the bounds of the type must be set to some
2524 -- non-static value. The simplest is to create attribute nodes for those
2525 -- bounds, that refer to the type itself. These bounds are never
2526 -- analyzed but serve as place-holders.
2528 Lo :=
2529 Make_Attribute_Reference (Loc,
2530 Attribute_Name => Name_First,
2531 Prefix => New_Occurrence_Of (T, Loc));
2532 Set_Etype (Lo, T);
2534 Hi :=
2535 Make_Attribute_Reference (Loc,
2536 Attribute_Name => Name_Last,
2537 Prefix => New_Occurrence_Of (T, Loc));
2538 Set_Etype (Hi, T);
2540 Set_Scalar_Range (T,
2541 Make_Range (Loc,
2542 Low_Bound => Lo,
2543 High_Bound => Hi));
2545 Mutate_Ekind (Base, E_Enumeration_Type);
2546 Set_Etype (Base, Base);
2547 Init_Size (Base, 8);
2548 Reinit_Alignment (Base);
2549 Set_Is_Generic_Type (Base);
2550 Set_Scalar_Range (Base, Scalar_Range (T));
2551 Set_Parent (Base, Parent (Def));
2552 end Analyze_Formal_Discrete_Type;
2554 ----------------------------------
2555 -- Analyze_Formal_Floating_Type --
2556 ---------------------------------
2558 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id) is
2559 Base : constant Entity_Id :=
2560 New_Internal_Entity
2561 (E_Floating_Point_Type, Current_Scope,
2562 Sloc (Defining_Identifier (Parent (Def))), 'G');
2564 begin
2565 -- The various semantic attributes are taken from the predefined type
2566 -- Float, just so that all of them are initialized. Their values are
2567 -- never used because no constant folding or expansion takes place in
2568 -- the generic itself.
2570 Enter_Name (T);
2571 Mutate_Ekind (T, E_Floating_Point_Subtype);
2572 Set_Etype (T, Base);
2573 Set_Size_Info (T, (Standard_Float));
2574 Set_RM_Size (T, RM_Size (Standard_Float));
2575 Set_Digits_Value (T, Digits_Value (Standard_Float));
2576 Set_Scalar_Range (T, Scalar_Range (Standard_Float));
2577 Set_Is_Constrained (T);
2579 Set_Is_Generic_Type (Base);
2580 Set_Etype (Base, Base);
2581 Set_Size_Info (Base, (Standard_Float));
2582 Set_RM_Size (Base, RM_Size (Standard_Float));
2583 Set_Digits_Value (Base, Digits_Value (Standard_Float));
2584 Set_Scalar_Range (Base, Scalar_Range (Standard_Float));
2585 Set_Parent (Base, Parent (Def));
2587 Check_Restriction (No_Floating_Point, Def);
2588 end Analyze_Formal_Floating_Type;
2590 -----------------------------------
2591 -- Analyze_Formal_Interface_Type;--
2592 -----------------------------------
2594 procedure Analyze_Formal_Interface_Type
2595 (N : Node_Id;
2596 T : Entity_Id;
2597 Def : Node_Id)
2599 Loc : constant Source_Ptr := Sloc (N);
2600 New_N : Node_Id;
2602 begin
2603 New_N :=
2604 Make_Full_Type_Declaration (Loc,
2605 Defining_Identifier => T,
2606 Type_Definition => Def);
2608 Rewrite (N, New_N);
2609 Analyze (N);
2610 Set_Is_Generic_Type (T);
2611 end Analyze_Formal_Interface_Type;
2613 ---------------------------------
2614 -- Analyze_Formal_Modular_Type --
2615 ---------------------------------
2617 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id) is
2618 begin
2619 -- Apart from their entity kind, generic modular types are treated like
2620 -- signed integer types, and have the same attributes.
2622 Analyze_Formal_Signed_Integer_Type (T, Def);
2623 Mutate_Ekind (T, E_Modular_Integer_Subtype);
2624 Mutate_Ekind (Etype (T), E_Modular_Integer_Type);
2626 end Analyze_Formal_Modular_Type;
2628 ---------------------------------------
2629 -- Analyze_Formal_Object_Declaration --
2630 ---------------------------------------
2632 procedure Analyze_Formal_Object_Declaration (N : Node_Id) is
2633 E : constant Node_Id := Default_Expression (N);
2634 Id : constant Node_Id := Defining_Identifier (N);
2636 K : Entity_Kind;
2637 Parent_Installed : Boolean := False;
2638 T : Node_Id;
2640 begin
2641 Enter_Name (Id);
2643 Check_Abbreviated_Instance (Parent (N), Parent_Installed);
2645 -- Determine the mode of the formal object
2647 if Out_Present (N) then
2648 K := E_Generic_In_Out_Parameter;
2650 if not In_Present (N) then
2651 Error_Msg_N ("formal generic objects cannot have mode OUT", N);
2652 end if;
2654 else
2655 K := E_Generic_In_Parameter;
2656 end if;
2658 if Present (Subtype_Mark (N)) then
2659 Find_Type (Subtype_Mark (N));
2660 T := Entity (Subtype_Mark (N));
2662 -- Verify that there is no redundant null exclusion
2664 if Null_Exclusion_Present (N) then
2665 if not Is_Access_Type (T) then
2666 Error_Msg_N
2667 ("null exclusion can only apply to an access type", N);
2669 elsif Can_Never_Be_Null (T) then
2670 Error_Msg_NE
2671 ("`NOT NULL` not allowed (& already excludes null)", N, T);
2672 end if;
2673 end if;
2675 -- Ada 2005 (AI-423): Formal object with an access definition
2677 else
2678 Check_Access_Definition (N);
2679 T := Access_Definition
2680 (Related_Nod => N,
2681 N => Access_Definition (N));
2682 end if;
2684 if Ekind (T) = E_Incomplete_Type then
2685 declare
2686 Error_Node : Node_Id;
2688 begin
2689 if Present (Subtype_Mark (N)) then
2690 Error_Node := Subtype_Mark (N);
2691 else
2692 Check_Access_Definition (N);
2693 Error_Node := Access_Definition (N);
2694 end if;
2696 Error_Msg_N ("premature usage of incomplete type", Error_Node);
2697 end;
2698 end if;
2700 if K = E_Generic_In_Parameter then
2702 -- Ada 2005 (AI-287): Limited aggregates allowed in generic formals
2704 if Ada_Version < Ada_2005 and then Is_Limited_Type (T) then
2705 Error_Msg_N
2706 ("generic formal of mode IN must not be of limited type", N);
2707 Explain_Limited_Type (T, N);
2708 end if;
2710 if Is_Abstract_Type (T) then
2711 Error_Msg_N
2712 ("generic formal of mode IN must not be of abstract type", N);
2713 end if;
2715 if Present (E) then
2716 Preanalyze_Spec_Expression (E, T);
2718 if Is_Limited_Type (T) and then not OK_For_Limited_Init (T, E) then
2719 Error_Msg_N
2720 ("initialization not allowed for limited types", E);
2721 Explain_Limited_Type (T, E);
2722 end if;
2723 end if;
2725 Mutate_Ekind (Id, K);
2726 Set_Etype (Id, T);
2728 -- Case of generic IN OUT parameter
2730 else
2731 -- If the formal has an unconstrained type, construct its actual
2732 -- subtype, as is done for subprogram formals. In this fashion, all
2733 -- its uses can refer to specific bounds.
2735 Mutate_Ekind (Id, K);
2736 Set_Etype (Id, T);
2738 if (Is_Array_Type (T) and then not Is_Constrained (T))
2739 or else (Ekind (T) = E_Record_Type and then Has_Discriminants (T))
2740 then
2741 declare
2742 Non_Freezing_Ref : constant Node_Id :=
2743 New_Occurrence_Of (Id, Sloc (Id));
2744 Decl : Node_Id;
2746 begin
2747 -- Make sure the actual subtype doesn't generate bogus freezing
2749 Set_Must_Not_Freeze (Non_Freezing_Ref);
2750 Decl := Build_Actual_Subtype (T, Non_Freezing_Ref);
2751 Insert_Before_And_Analyze (N, Decl);
2752 Set_Actual_Subtype (Id, Defining_Identifier (Decl));
2753 end;
2754 else
2755 Set_Actual_Subtype (Id, T);
2756 end if;
2758 if Present (E) then
2759 Error_Msg_N
2760 ("initialization not allowed for `IN OUT` formals", N);
2761 end if;
2762 end if;
2764 if Has_Aspects (N) then
2765 Analyze_Aspect_Specifications (N, Id);
2766 end if;
2768 if Parent_Installed then
2769 Remove_Parent;
2770 end if;
2771 end Analyze_Formal_Object_Declaration;
2773 ----------------------------------------------
2774 -- Analyze_Formal_Ordinary_Fixed_Point_Type --
2775 ----------------------------------------------
2777 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
2778 (T : Entity_Id;
2779 Def : Node_Id)
2781 Loc : constant Source_Ptr := Sloc (Def);
2782 Base : constant Entity_Id :=
2783 New_Internal_Entity
2784 (E_Ordinary_Fixed_Point_Type, Current_Scope,
2785 Sloc (Defining_Identifier (Parent (Def))), 'G');
2787 begin
2788 -- The semantic attributes are set for completeness only, their values
2789 -- will never be used, since all properties of the type are non-static.
2791 Enter_Name (T);
2792 Mutate_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
2793 Set_Etype (T, Base);
2794 Set_Size_Info (T, Standard_Integer);
2795 Set_RM_Size (T, RM_Size (Standard_Integer));
2796 Set_Small_Value (T, Ureal_1);
2797 Set_Delta_Value (T, Ureal_1);
2798 Set_Scalar_Range (T,
2799 Make_Range (Loc,
2800 Low_Bound => Make_Real_Literal (Loc, Ureal_1),
2801 High_Bound => Make_Real_Literal (Loc, Ureal_1)));
2802 Set_Is_Constrained (T);
2804 Set_Is_Generic_Type (Base);
2805 Set_Etype (Base, Base);
2806 Set_Size_Info (Base, Standard_Integer);
2807 Set_RM_Size (Base, RM_Size (Standard_Integer));
2808 Set_Small_Value (Base, Ureal_1);
2809 Set_Delta_Value (Base, Ureal_1);
2810 Set_Scalar_Range (Base, Scalar_Range (T));
2811 Set_Parent (Base, Parent (Def));
2813 Check_Restriction (No_Fixed_Point, Def);
2814 end Analyze_Formal_Ordinary_Fixed_Point_Type;
2816 ----------------------------------------
2817 -- Analyze_Formal_Package_Declaration --
2818 ----------------------------------------
2820 procedure Analyze_Formal_Package_Declaration (N : Node_Id) is
2821 Gen_Id : constant Node_Id := Name (N);
2822 Loc : constant Source_Ptr := Sloc (N);
2823 Pack_Id : constant Entity_Id := Defining_Identifier (N);
2824 Formal : Entity_Id;
2825 Gen_Decl : Node_Id;
2826 Gen_Unit : Entity_Id;
2827 Renaming : Node_Id;
2829 Vis_Prims_List : Elist_Id := No_Elist;
2830 -- List of primitives made temporarily visible in the instantiation
2831 -- to match the visibility of the formal type.
2833 function Build_Local_Package return Node_Id;
2834 -- The formal package is rewritten so that its parameters are replaced
2835 -- with corresponding declarations. For parameters with bona fide
2836 -- associations these declarations are created by Analyze_Associations
2837 -- as for a regular instantiation. For boxed parameters, we preserve
2838 -- the formal declarations and analyze them, in order to introduce
2839 -- entities of the right kind in the environment of the formal.
2841 -------------------------
2842 -- Build_Local_Package --
2843 -------------------------
2845 function Build_Local_Package return Node_Id is
2846 Decls : List_Id;
2847 Pack_Decl : Node_Id;
2849 begin
2850 -- Within the formal, the name of the generic package is a renaming
2851 -- of the formal (as for a regular instantiation).
2853 Pack_Decl :=
2854 Make_Package_Declaration (Loc,
2855 Specification =>
2856 Copy_Generic_Node
2857 (Specification (Original_Node (Gen_Decl)),
2858 Empty, Instantiating => True));
2860 Renaming :=
2861 Make_Package_Renaming_Declaration (Loc,
2862 Defining_Unit_Name =>
2863 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
2864 Name => New_Occurrence_Of (Formal, Loc));
2866 if Nkind (Gen_Id) = N_Identifier
2867 and then Chars (Gen_Id) = Chars (Pack_Id)
2868 then
2869 Error_Msg_NE
2870 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
2871 end if;
2873 -- If the formal is declared with a box, or with an others choice,
2874 -- create corresponding declarations for all entities in the formal
2875 -- part, so that names with the proper types are available in the
2876 -- specification of the formal package.
2878 -- On the other hand, if there are no associations, then all the
2879 -- formals must have defaults, and this will be checked by the
2880 -- call to Analyze_Associations.
2882 if Box_Present (N)
2883 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2884 then
2885 declare
2886 Formal_Decl : Node_Id;
2888 begin
2889 -- TBA : for a formal package, need to recurse ???
2891 Decls := New_List;
2892 Formal_Decl :=
2893 First
2894 (Generic_Formal_Declarations (Original_Node (Gen_Decl)));
2895 while Present (Formal_Decl) loop
2896 Append_To
2897 (Decls,
2898 Copy_Generic_Node
2899 (Formal_Decl, Empty, Instantiating => True));
2900 Next (Formal_Decl);
2901 end loop;
2902 end;
2904 -- If generic associations are present, use Analyze_Associations to
2905 -- create the proper renaming declarations.
2907 else
2908 declare
2909 Act_Tree : constant Node_Id :=
2910 Copy_Generic_Node
2911 (Original_Node (Gen_Decl), Empty,
2912 Instantiating => True);
2914 begin
2915 Generic_Renamings.Set_Last (0);
2916 Generic_Renamings_HTable.Reset;
2917 Instantiation_Node := N;
2919 Decls :=
2920 Analyze_Associations
2921 (I_Node => Original_Node (N),
2922 Formals => Generic_Formal_Declarations (Act_Tree),
2923 F_Copy => Generic_Formal_Declarations (Gen_Decl));
2925 Vis_Prims_List := Check_Hidden_Primitives (Decls);
2926 end;
2927 end if;
2929 Append (Renaming, To => Decls);
2931 -- Add generated declarations ahead of local declarations in
2932 -- the package.
2934 if No (Visible_Declarations (Specification (Pack_Decl))) then
2935 Set_Visible_Declarations (Specification (Pack_Decl), Decls);
2936 else
2937 Insert_List_Before
2938 (First (Visible_Declarations (Specification (Pack_Decl))),
2939 Decls);
2940 end if;
2942 return Pack_Decl;
2943 end Build_Local_Package;
2945 -- Local variables
2947 Save_ISMP : constant Boolean := Ignore_SPARK_Mode_Pragmas_In_Instance;
2948 -- Save flag Ignore_SPARK_Mode_Pragmas_In_Instance for restore on exit
2950 Associations : Boolean := True;
2951 New_N : Node_Id;
2952 Parent_Installed : Boolean := False;
2953 Parent_Instance : Entity_Id;
2954 Renaming_In_Par : Entity_Id;
2956 -- Start of processing for Analyze_Formal_Package_Declaration
2958 begin
2959 Check_Text_IO_Special_Unit (Gen_Id);
2961 Init_Env;
2962 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
2963 Gen_Unit := Entity (Gen_Id);
2965 -- Check for a formal package that is a package renaming
2967 if Present (Renamed_Entity (Gen_Unit)) then
2969 -- Indicate that unit is used, before replacing it with renamed
2970 -- entity for use below.
2972 if In_Extended_Main_Source_Unit (N) then
2973 Set_Is_Instantiated (Gen_Unit);
2974 Generate_Reference (Gen_Unit, N);
2975 end if;
2977 Gen_Unit := Renamed_Entity (Gen_Unit);
2978 end if;
2980 if Ekind (Gen_Unit) /= E_Generic_Package then
2981 Error_Msg_N ("expect generic package name", Gen_Id);
2982 Restore_Env;
2983 goto Leave;
2985 elsif Gen_Unit = Current_Scope then
2986 Error_Msg_N
2987 ("generic package cannot be used as a formal package of itself",
2988 Gen_Id);
2989 Restore_Env;
2990 goto Leave;
2992 elsif In_Open_Scopes (Gen_Unit) then
2993 if Is_Compilation_Unit (Gen_Unit)
2994 and then Is_Child_Unit (Current_Scope)
2995 then
2996 -- Special-case the error when the formal is a parent, and
2997 -- continue analysis to minimize cascaded errors.
2999 Error_Msg_N
3000 ("generic parent cannot be used as formal package of a child "
3001 & "unit", Gen_Id);
3003 else
3004 Error_Msg_N
3005 ("generic package cannot be used as a formal package within "
3006 & "itself", Gen_Id);
3007 Restore_Env;
3008 goto Leave;
3009 end if;
3010 end if;
3012 -- Check that name of formal package does not hide name of generic,
3013 -- or its leading prefix. This check must be done separately because
3014 -- the name of the generic has already been analyzed.
3016 declare
3017 Gen_Name : Entity_Id;
3019 begin
3020 Gen_Name := Gen_Id;
3021 while Nkind (Gen_Name) = N_Expanded_Name loop
3022 Gen_Name := Prefix (Gen_Name);
3023 end loop;
3025 if Chars (Gen_Name) = Chars (Pack_Id) then
3026 Error_Msg_NE
3027 ("& is hidden within declaration of formal package",
3028 Gen_Id, Gen_Name);
3029 end if;
3030 end;
3032 if Box_Present (N)
3033 or else No (Generic_Associations (N))
3034 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
3035 then
3036 Associations := False;
3037 end if;
3039 -- If there are no generic associations, the generic parameters appear
3040 -- as local entities and are instantiated like them. We copy the generic
3041 -- package declaration as if it were an instantiation, and analyze it
3042 -- like a regular package, except that we treat the formals as
3043 -- additional visible components.
3045 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
3047 if In_Extended_Main_Source_Unit (N) then
3048 Set_Is_Instantiated (Gen_Unit);
3049 Generate_Reference (Gen_Unit, N);
3050 end if;
3052 Formal := New_Copy (Pack_Id);
3053 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
3055 -- Make local generic without formals. The formals will be replaced with
3056 -- internal declarations.
3058 begin
3059 New_N := Build_Local_Package;
3061 -- If there are errors in the parameter list, Analyze_Associations
3062 -- raises Instantiation_Error. Patch the declaration to prevent further
3063 -- exception propagation.
3065 exception
3066 when Instantiation_Error =>
3067 Enter_Name (Formal);
3068 Mutate_Ekind (Formal, E_Variable);
3069 Set_Etype (Formal, Any_Type);
3070 Restore_Hidden_Primitives (Vis_Prims_List);
3072 if Parent_Installed then
3073 Remove_Parent;
3074 end if;
3076 goto Leave;
3077 end;
3079 Rewrite (N, New_N);
3080 Set_Defining_Unit_Name (Specification (New_N), Formal);
3081 Set_Generic_Parent (Specification (N), Gen_Unit);
3082 Set_Instance_Env (Gen_Unit, Formal);
3083 Set_Is_Generic_Instance (Formal);
3085 Enter_Name (Formal);
3086 Mutate_Ekind (Formal, E_Package);
3087 Set_Etype (Formal, Standard_Void_Type);
3088 Set_Inner_Instances (Formal, New_Elmt_List);
3090 -- It is unclear that any aspects can apply to a formal package
3091 -- declaration, given that they look like a hidden conformance
3092 -- requirement on the corresponding actual. However, Abstract_State
3093 -- must be treated specially because it generates declarations that
3094 -- must appear before other declarations in the specification and
3095 -- must be analyzed at once.
3097 if Present (Aspect_Specifications (Gen_Decl)) then
3098 if No (Aspect_Specifications (N)) then
3099 Set_Aspect_Specifications (N, New_List);
3100 Set_Has_Aspects (N);
3101 end if;
3103 declare
3104 ASN : Node_Id := First (Aspect_Specifications (Gen_Decl));
3105 New_A : Node_Id;
3107 begin
3108 while Present (ASN) loop
3109 if Get_Aspect_Id (ASN) = Aspect_Abstract_State then
3110 New_A :=
3111 Copy_Generic_Node (ASN, Empty, Instantiating => True);
3112 Set_Entity (New_A, Formal);
3113 Set_Analyzed (New_A, False);
3114 Append (New_A, Aspect_Specifications (N));
3115 Analyze_Aspect_Specifications (N, Formal);
3116 exit;
3117 end if;
3119 Next (ASN);
3120 end loop;
3121 end;
3122 end if;
3124 Push_Scope (Formal);
3126 -- Manually set the SPARK_Mode from the context because the package
3127 -- declaration is never analyzed.
3129 Set_SPARK_Pragma (Formal, SPARK_Mode_Pragma);
3130 Set_SPARK_Aux_Pragma (Formal, SPARK_Mode_Pragma);
3131 Set_SPARK_Pragma_Inherited (Formal);
3132 Set_SPARK_Aux_Pragma_Inherited (Formal);
3134 if Is_Child_Unit (Gen_Unit) and then Parent_Installed then
3136 -- Similarly, we have to make the name of the formal visible in the
3137 -- parent instance, to resolve properly fully qualified names that
3138 -- may appear in the generic unit. The parent instance has been
3139 -- placed on the scope stack ahead of the current scope.
3141 Parent_Instance := Scope_Stack.Table (Scope_Stack.Last - 1).Entity;
3143 Renaming_In_Par :=
3144 Make_Defining_Identifier (Loc, Chars (Gen_Unit));
3145 Mutate_Ekind (Renaming_In_Par, E_Package);
3146 Set_Etype (Renaming_In_Par, Standard_Void_Type);
3147 Set_Scope (Renaming_In_Par, Parent_Instance);
3148 Set_Parent (Renaming_In_Par, Parent (Formal));
3149 Set_Renamed_Entity (Renaming_In_Par, Formal);
3150 Append_Entity (Renaming_In_Par, Parent_Instance);
3151 end if;
3153 -- A formal package declaration behaves as a package instantiation with
3154 -- respect to SPARK_Mode "off". If the annotation is "off" or altogether
3155 -- missing, set the global flag which signals Analyze_Pragma to ingnore
3156 -- all SPARK_Mode pragmas within the generic_package_name.
3158 if SPARK_Mode /= On then
3159 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
3161 -- Mark the formal spec in case the body is instantiated at a later
3162 -- pass. This preserves the original context in effect for the body.
3164 Set_Ignore_SPARK_Mode_Pragmas (Formal);
3165 end if;
3167 Analyze (Specification (N));
3169 -- The formals for which associations are provided are not visible
3170 -- outside of the formal package. The others are still declared by a
3171 -- formal parameter declaration.
3173 -- If there are no associations, the only local entity to hide is the
3174 -- generated package renaming itself.
3176 declare
3177 E : Entity_Id;
3179 begin
3180 E := First_Entity (Formal);
3181 while Present (E) loop
3182 if Associations and then not Is_Generic_Formal (E) then
3183 Set_Is_Hidden (E);
3184 end if;
3186 if Ekind (E) = E_Package and then Renamed_Entity (E) = Formal then
3187 Set_Is_Hidden (E);
3188 exit;
3189 end if;
3191 Next_Entity (E);
3192 end loop;
3193 end;
3195 End_Package_Scope (Formal);
3196 Restore_Hidden_Primitives (Vis_Prims_List);
3198 if Parent_Installed then
3199 Remove_Parent;
3200 end if;
3202 Restore_Env;
3204 -- Inside the generic unit, the formal package is a regular package, but
3205 -- no body is needed for it. Note that after instantiation, the defining
3206 -- unit name we need is in the new tree and not in the original (see
3207 -- Package_Instantiation). A generic formal package is an instance, and
3208 -- can be used as an actual for an inner instance.
3210 Set_Has_Completion (Formal, True);
3212 -- Add semantic information to the original defining identifier.
3214 Mutate_Ekind (Pack_Id, E_Package);
3215 Set_Etype (Pack_Id, Standard_Void_Type);
3216 Set_Scope (Pack_Id, Scope (Formal));
3217 Set_Has_Completion (Pack_Id, True);
3219 <<Leave>>
3220 if Has_Aspects (N) then
3221 -- Unclear that any other aspects may appear here, analyze them
3222 -- for completion, given that the grammar allows their appearance.
3224 Analyze_Aspect_Specifications (N, Pack_Id);
3225 end if;
3227 Ignore_SPARK_Mode_Pragmas_In_Instance := Save_ISMP;
3228 end Analyze_Formal_Package_Declaration;
3230 ---------------------------------
3231 -- Analyze_Formal_Private_Type --
3232 ---------------------------------
3234 procedure Analyze_Formal_Private_Type
3235 (N : Node_Id;
3236 T : Entity_Id;
3237 Def : Node_Id)
3239 begin
3240 New_Private_Type (N, T, Def);
3242 -- Set the size to an arbitrary but legal value
3244 Set_Size_Info (T, Standard_Integer);
3245 Set_RM_Size (T, RM_Size (Standard_Integer));
3246 end Analyze_Formal_Private_Type;
3248 ------------------------------------
3249 -- Analyze_Formal_Incomplete_Type --
3250 ------------------------------------
3252 procedure Analyze_Formal_Incomplete_Type
3253 (T : Entity_Id;
3254 Def : Node_Id)
3256 begin
3257 Enter_Name (T);
3258 Mutate_Ekind (T, E_Incomplete_Type);
3259 Set_Etype (T, T);
3260 Set_Private_Dependents (T, New_Elmt_List);
3262 if Tagged_Present (Def) then
3263 Set_Is_Tagged_Type (T);
3264 Make_Class_Wide_Type (T);
3265 Set_Direct_Primitive_Operations (T, New_Elmt_List);
3266 end if;
3267 end Analyze_Formal_Incomplete_Type;
3269 ----------------------------------------
3270 -- Analyze_Formal_Signed_Integer_Type --
3271 ----------------------------------------
3273 procedure Analyze_Formal_Signed_Integer_Type
3274 (T : Entity_Id;
3275 Def : Node_Id)
3277 Base : constant Entity_Id :=
3278 New_Internal_Entity
3279 (E_Signed_Integer_Type,
3280 Current_Scope,
3281 Sloc (Defining_Identifier (Parent (Def))), 'G');
3283 begin
3284 Enter_Name (T);
3286 Mutate_Ekind (T, E_Signed_Integer_Subtype);
3287 Set_Etype (T, Base);
3288 Set_Size_Info (T, Standard_Integer);
3289 Set_RM_Size (T, RM_Size (Standard_Integer));
3290 Set_Scalar_Range (T, Scalar_Range (Standard_Integer));
3291 Set_Is_Constrained (T);
3293 Set_Is_Generic_Type (Base);
3294 Set_Size_Info (Base, Standard_Integer);
3295 Set_RM_Size (Base, RM_Size (Standard_Integer));
3296 Set_Etype (Base, Base);
3297 Set_Scalar_Range (Base, Scalar_Range (Standard_Integer));
3298 Set_Parent (Base, Parent (Def));
3299 end Analyze_Formal_Signed_Integer_Type;
3301 -------------------------------------------
3302 -- Analyze_Formal_Subprogram_Declaration --
3303 -------------------------------------------
3305 procedure Analyze_Formal_Subprogram_Declaration (N : Node_Id) is
3306 Spec : constant Node_Id := Specification (N);
3307 Def : constant Node_Id := Default_Name (N);
3308 Expr : constant Node_Id := Expression (N);
3309 Nam : constant Entity_Id := Defining_Unit_Name (Spec);
3311 Parent_Installed : Boolean := False;
3312 Subp : Entity_Id;
3314 begin
3315 if Nam = Error then
3316 return;
3317 end if;
3319 if Nkind (Nam) = N_Defining_Program_Unit_Name then
3320 Error_Msg_N ("name of formal subprogram must be a direct name", Nam);
3321 goto Leave;
3322 end if;
3324 Check_Abbreviated_Instance (Parent (N), Parent_Installed);
3326 Analyze_Subprogram_Declaration (N);
3327 Set_Is_Formal_Subprogram (Nam);
3328 Set_Has_Completion (Nam);
3330 if Nkind (N) = N_Formal_Abstract_Subprogram_Declaration then
3331 Set_Is_Abstract_Subprogram (Nam);
3333 Set_Is_Dispatching_Operation (Nam);
3335 -- A formal abstract procedure cannot have a null default
3336 -- (RM 12.6(4.1/2)).
3338 if Nkind (Spec) = N_Procedure_Specification
3339 and then Null_Present (Spec)
3340 then
3341 Error_Msg_N
3342 ("a formal abstract subprogram cannot default to null", Spec);
3343 end if;
3345 -- A formal abstract function cannot have an expression default
3346 -- (expression defaults are allowed for nonabstract formal functions
3347 -- when extensions are enabled).
3349 if Nkind (Spec) = N_Function_Specification
3350 and then Present (Expr)
3351 then
3352 Error_Msg_N
3353 ("a formal abstract subprogram cannot default to an expression",
3354 Spec);
3355 end if;
3357 declare
3358 Ctrl_Type : constant Entity_Id := Find_Dispatching_Type (Nam);
3359 begin
3360 if No (Ctrl_Type) then
3361 Error_Msg_N
3362 ("abstract formal subprogram must have a controlling type",
3365 elsif Ada_Version >= Ada_2012
3366 and then Is_Incomplete_Type (Ctrl_Type)
3367 then
3368 Error_Msg_NE
3369 ("controlling type of abstract formal subprogram cannot "
3370 & "be incomplete type", N, Ctrl_Type);
3372 else
3373 Check_Controlling_Formals (Ctrl_Type, Nam);
3374 end if;
3375 end;
3376 end if;
3378 -- Default name is resolved at the point of instantiation
3380 if Box_Present (N) then
3381 null;
3383 -- Default name is bound at the point of generic declaration
3385 elsif Present (Def) then
3386 if Nkind (Def) = N_Operator_Symbol then
3387 Find_Direct_Name (Def);
3389 elsif Nkind (Def) /= N_Attribute_Reference then
3390 Analyze (Def);
3392 else
3393 -- For an attribute reference, analyze the prefix and verify
3394 -- that it has the proper profile for the subprogram.
3396 Analyze (Prefix (Def));
3397 Valid_Default_Attribute (Nam, Def);
3398 goto Leave;
3399 end if;
3401 -- Default name may be overloaded, in which case the interpretation
3402 -- with the correct profile must be selected, as for a renaming.
3403 -- If the definition is an indexed component, it must denote a
3404 -- member of an entry family. If it is a selected component, it
3405 -- can be a protected operation.
3407 if Etype (Def) = Any_Type then
3408 goto Leave;
3410 elsif Nkind (Def) = N_Selected_Component then
3411 if not Is_Overloadable (Entity (Selector_Name (Def))) then
3412 Error_Msg_N ("expect valid subprogram name as default", Def);
3413 end if;
3415 elsif Nkind (Def) = N_Indexed_Component then
3416 if Is_Entity_Name (Prefix (Def)) then
3417 if Ekind (Entity (Prefix (Def))) /= E_Entry_Family then
3418 Error_Msg_N ("expect valid subprogram name as default", Def);
3419 end if;
3421 elsif Nkind (Prefix (Def)) = N_Selected_Component then
3422 if Ekind (Entity (Selector_Name (Prefix (Def)))) /=
3423 E_Entry_Family
3424 then
3425 Error_Msg_N ("expect valid subprogram name as default", Def);
3426 end if;
3428 else
3429 Error_Msg_N ("expect valid subprogram name as default", Def);
3430 goto Leave;
3431 end if;
3433 elsif Nkind (Def) = N_Character_Literal then
3435 -- Needs some type checks: subprogram should be parameterless???
3437 Resolve (Def, (Etype (Nam)));
3439 elsif not Is_Entity_Name (Def)
3440 or else not Is_Overloadable (Entity (Def))
3441 then
3442 Error_Msg_N ("expect valid subprogram name as default", Def);
3443 goto Leave;
3445 elsif not Is_Overloaded (Def) then
3446 Subp := Entity (Def);
3448 if Subp = Nam then
3449 Error_Msg_N ("premature usage of formal subprogram", Def);
3451 elsif not Entity_Matches_Spec (Subp, Nam) then
3452 Error_Msg_N ("no visible entity matches specification", Def);
3453 end if;
3455 -- More than one interpretation, so disambiguate as for a renaming
3457 else
3458 declare
3459 I : Interp_Index;
3460 I1 : Interp_Index := 0;
3461 It : Interp;
3462 It1 : Interp;
3464 begin
3465 Subp := Any_Id;
3466 Get_First_Interp (Def, I, It);
3467 while Present (It.Nam) loop
3468 if Entity_Matches_Spec (It.Nam, Nam) then
3469 if Subp /= Any_Id then
3470 It1 := Disambiguate (Def, I1, I, Etype (Subp));
3472 if It1 = No_Interp then
3473 Error_Msg_N ("ambiguous default subprogram", Def);
3474 else
3475 Subp := It1.Nam;
3476 end if;
3478 exit;
3480 else
3481 I1 := I;
3482 Subp := It.Nam;
3483 end if;
3484 end if;
3486 Get_Next_Interp (I, It);
3487 end loop;
3488 end;
3490 if Subp /= Any_Id then
3492 -- Subprogram found, generate reference to it
3494 Set_Entity (Def, Subp);
3495 Generate_Reference (Subp, Def);
3497 if Subp = Nam then
3498 Error_Msg_N ("premature usage of formal subprogram", Def);
3500 elsif Ekind (Subp) /= E_Operator then
3501 Check_Mode_Conformant (Subp, Nam);
3502 end if;
3504 else
3505 Error_Msg_N ("no visible subprogram matches specification", N);
3506 end if;
3507 end if;
3509 -- When extensions are enabled, an expression can be given as default
3510 -- for a formal function. The expression must be of the function result
3511 -- type and can reference formal parameters of the function.
3513 elsif Present (Expr) then
3514 Push_Scope (Nam);
3515 Install_Formals (Nam);
3516 Preanalyze_Spec_Expression (Expr, Etype (Nam));
3517 End_Scope;
3518 end if;
3520 <<Leave>>
3521 if Has_Aspects (N) then
3522 Analyze_Aspect_Specifications (N, Nam);
3523 end if;
3525 if Parent_Installed then
3526 Remove_Parent;
3527 end if;
3528 end Analyze_Formal_Subprogram_Declaration;
3530 -------------------------------------
3531 -- Analyze_Formal_Type_Declaration --
3532 -------------------------------------
3534 procedure Analyze_Formal_Type_Declaration (N : Node_Id) is
3535 Def : constant Node_Id := Formal_Type_Definition (N);
3537 Parent_Installed : Boolean := False;
3538 T : Entity_Id;
3540 begin
3541 T := Defining_Identifier (N);
3543 if Present (Discriminant_Specifications (N))
3544 and then Nkind (Def) /= N_Formal_Private_Type_Definition
3545 then
3546 Error_Msg_N
3547 ("discriminants not allowed for this formal type", T);
3548 end if;
3550 Check_Abbreviated_Instance (Parent (N), Parent_Installed);
3552 -- Enter the new name, and branch to specific routine
3554 case Nkind (Def) is
3555 when N_Formal_Private_Type_Definition =>
3556 Analyze_Formal_Private_Type (N, T, Def);
3558 when N_Formal_Derived_Type_Definition =>
3559 Analyze_Formal_Derived_Type (N, T, Def);
3561 when N_Formal_Incomplete_Type_Definition =>
3562 Analyze_Formal_Incomplete_Type (T, Def);
3564 when N_Formal_Discrete_Type_Definition =>
3565 Analyze_Formal_Discrete_Type (T, Def);
3567 when N_Formal_Signed_Integer_Type_Definition =>
3568 Analyze_Formal_Signed_Integer_Type (T, Def);
3570 when N_Formal_Modular_Type_Definition =>
3571 Analyze_Formal_Modular_Type (T, Def);
3573 when N_Formal_Floating_Point_Definition =>
3574 Analyze_Formal_Floating_Type (T, Def);
3576 when N_Formal_Ordinary_Fixed_Point_Definition =>
3577 Analyze_Formal_Ordinary_Fixed_Point_Type (T, Def);
3579 when N_Formal_Decimal_Fixed_Point_Definition =>
3580 Analyze_Formal_Decimal_Fixed_Point_Type (T, Def);
3582 when N_Array_Type_Definition =>
3583 Analyze_Formal_Array_Type (T, Def);
3585 when N_Access_Function_Definition
3586 | N_Access_Procedure_Definition
3587 | N_Access_To_Object_Definition
3589 Analyze_Generic_Access_Type (T, Def);
3591 -- Ada 2005: a interface declaration is encoded as an abstract
3592 -- record declaration or a abstract type derivation.
3594 when N_Record_Definition =>
3595 Analyze_Formal_Interface_Type (N, T, Def);
3597 when N_Derived_Type_Definition =>
3598 Analyze_Formal_Derived_Interface_Type (N, T, Def);
3600 when N_Error =>
3601 null;
3603 when others =>
3604 raise Program_Error;
3605 end case;
3607 -- A formal type declaration declares a type and its first
3608 -- subtype.
3610 Set_Is_Generic_Type (T);
3611 Set_Is_First_Subtype (T);
3613 if Present (Default_Subtype_Mark (Original_Node (N))) then
3614 Validate_Formal_Type_Default (N);
3615 end if;
3617 if Has_Aspects (N) then
3618 Analyze_Aspect_Specifications (N, T);
3619 end if;
3621 if Parent_Installed then
3622 Remove_Parent;
3623 end if;
3624 end Analyze_Formal_Type_Declaration;
3626 ------------------------------------
3627 -- Analyze_Function_Instantiation --
3628 ------------------------------------
3630 procedure Analyze_Function_Instantiation (N : Node_Id) is
3631 begin
3632 Analyze_Subprogram_Instantiation (N, E_Function);
3633 end Analyze_Function_Instantiation;
3635 ---------------------------------
3636 -- Analyze_Generic_Access_Type --
3637 ---------------------------------
3639 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id) is
3640 begin
3641 Enter_Name (T);
3643 if Nkind (Def) = N_Access_To_Object_Definition then
3644 Access_Type_Declaration (T, Def);
3646 if Is_Incomplete_Or_Private_Type (Designated_Type (T))
3647 and then No (Full_View (Designated_Type (T)))
3648 and then not Is_Generic_Type (Designated_Type (T))
3649 then
3650 Error_Msg_N ("premature usage of incomplete type", Def);
3652 elsif not Is_Entity_Name (Subtype_Indication (Def)) then
3653 Error_Msg_N
3654 ("only a subtype mark is allowed in a formal", Def);
3655 end if;
3657 else
3658 Access_Subprogram_Declaration (T, Def);
3659 end if;
3660 end Analyze_Generic_Access_Type;
3662 ---------------------------------
3663 -- Analyze_Generic_Formal_Part --
3664 ---------------------------------
3666 procedure Analyze_Generic_Formal_Part (N : Node_Id) is
3667 Gen_Parm_Decl : Node_Id;
3669 begin
3670 -- The generic formals are processed in the scope of the generic unit,
3671 -- where they are immediately visible. The scope is installed by the
3672 -- caller.
3674 Gen_Parm_Decl := First (Generic_Formal_Declarations (N));
3675 while Present (Gen_Parm_Decl) loop
3676 Analyze (Gen_Parm_Decl);
3677 Next (Gen_Parm_Decl);
3678 end loop;
3680 Generate_Reference_To_Generic_Formals (Current_Scope);
3682 -- For Ada 2022, some formal parameters can carry aspects, which must
3683 -- be name-resolved at the end of the list of formal parameters (which
3684 -- has the semantics of a declaration list).
3686 Analyze_Contracts (Generic_Formal_Declarations (N));
3687 end Analyze_Generic_Formal_Part;
3689 ------------------------------------------
3690 -- Analyze_Generic_Package_Declaration --
3691 ------------------------------------------
3693 procedure Analyze_Generic_Package_Declaration (N : Node_Id) is
3694 Decls : constant List_Id := Visible_Declarations (Specification (N));
3695 Loc : constant Source_Ptr := Sloc (N);
3697 Decl : Node_Id;
3698 Id : Entity_Id;
3699 New_N : Node_Id;
3700 Renaming : Node_Id;
3701 Save_Parent : Node_Id;
3703 begin
3704 -- A generic may grant access to its private enclosing context depending
3705 -- on the placement of its corresponding body. From elaboration point of
3706 -- view, the flow of execution may enter this private context, and then
3707 -- reach an external unit, thus producing a dependency on that external
3708 -- unit. For such a path to be properly discovered and encoded in the
3709 -- ALI file of the main unit, let the ABE mechanism process the body of
3710 -- the main unit, and encode all relevant invocation constructs and the
3711 -- relations between them.
3713 Mark_Save_Invocation_Graph_Of_Body;
3715 -- We introduce a renaming of the enclosing package, to have a usable
3716 -- entity as the prefix of an expanded name for a local entity of the
3717 -- form Par.P.Q, where P is the generic package. This is because a local
3718 -- entity named P may hide it, so that the usual visibility rules in
3719 -- the instance will not resolve properly.
3721 Renaming :=
3722 Make_Package_Renaming_Declaration (Loc,
3723 Defining_Unit_Name =>
3724 Make_Defining_Identifier (Loc,
3725 Chars => New_External_Name (Chars (Defining_Entity (N)), "GH")),
3726 Name =>
3727 Make_Identifier (Loc, Chars (Defining_Entity (N))));
3729 -- The declaration is inserted before other declarations, but before
3730 -- pragmas that may be library-unit pragmas and must appear before other
3731 -- declarations. The pragma Compile_Time_Error is not in this class, and
3732 -- may contain an expression that includes such a qualified name, so the
3733 -- renaming declaration must appear before it.
3735 -- Are there other pragmas that require this special handling ???
3737 if Present (Decls) then
3738 Decl := First (Decls);
3739 while Present (Decl)
3740 and then Nkind (Decl) = N_Pragma
3741 and then Get_Pragma_Id (Decl) /= Pragma_Compile_Time_Error
3742 loop
3743 Next (Decl);
3744 end loop;
3746 if Present (Decl) then
3747 Insert_Before (Decl, Renaming);
3748 else
3749 Append (Renaming, Visible_Declarations (Specification (N)));
3750 end if;
3752 else
3753 Set_Visible_Declarations (Specification (N), New_List (Renaming));
3754 end if;
3756 -- Create copy of generic unit, and save for instantiation. If the unit
3757 -- is a child unit, do not copy the specifications for the parent, which
3758 -- are not part of the generic tree.
3760 Save_Parent := Parent_Spec (N);
3761 Set_Parent_Spec (N, Empty);
3763 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
3764 Set_Parent_Spec (New_N, Save_Parent);
3765 Rewrite (N, New_N);
3767 -- Once the contents of the generic copy and the template are swapped,
3768 -- do the same for their respective aspect specifications.
3770 Exchange_Aspects (N, New_N);
3772 -- Collect all contract-related source pragmas found within the template
3773 -- and attach them to the contract of the package spec. This contract is
3774 -- used in the capture of global references within annotations.
3776 Create_Generic_Contract (N);
3778 Id := Defining_Entity (N);
3779 Generate_Definition (Id);
3781 -- Expansion is not applied to generic units
3783 Start_Generic;
3785 Enter_Name (Id);
3786 Mutate_Ekind (Id, E_Generic_Package);
3787 Set_Etype (Id, Standard_Void_Type);
3789 -- Set SPARK_Mode from context
3791 Set_SPARK_Pragma (Id, SPARK_Mode_Pragma);
3792 Set_SPARK_Aux_Pragma (Id, SPARK_Mode_Pragma);
3793 Set_SPARK_Pragma_Inherited (Id);
3794 Set_SPARK_Aux_Pragma_Inherited (Id);
3796 -- Preserve relevant elaboration-related attributes of the context which
3797 -- are no longer available or very expensive to recompute once analysis,
3798 -- resolution, and expansion are over.
3800 Mark_Elaboration_Attributes
3801 (N_Id => Id,
3802 Checks => True,
3803 Warnings => True);
3805 -- Analyze aspects now, so that generated pragmas appear in the
3806 -- declarations before building and analyzing the generic copy.
3808 if Has_Aspects (N) then
3809 Analyze_Aspect_Specifications (N, Id);
3810 end if;
3812 Push_Scope (Id);
3813 Enter_Generic_Scope (Id);
3814 Set_Inner_Instances (Id, New_Elmt_List);
3816 Set_Categorization_From_Pragmas (N);
3817 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3819 -- Link the declaration of the generic homonym in the generic copy to
3820 -- the package it renames, so that it is always resolved properly.
3822 Set_Generic_Homonym (Id, Defining_Unit_Name (Renaming));
3823 Set_Entity (Associated_Node (Name (Renaming)), Id);
3825 -- For a library unit, we have reconstructed the entity for the unit,
3826 -- and must reset it in the library tables.
3828 if Nkind (Parent (N)) = N_Compilation_Unit then
3829 Set_Cunit_Entity (Current_Sem_Unit, Id);
3830 end if;
3832 Analyze_Generic_Formal_Part (N);
3834 -- After processing the generic formals, analysis proceeds as for a
3835 -- non-generic package.
3837 Analyze (Specification (N));
3839 Validate_Categorization_Dependency (N, Id);
3841 End_Generic;
3843 End_Package_Scope (Id);
3844 Exit_Generic_Scope (Id);
3846 -- If the generic appears within a package unit, the body of that unit
3847 -- has to be present for instantiation and inlining.
3849 if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Declaration then
3850 Set_Body_Needed_For_Inlining
3851 (Defining_Entity (Unit (Cunit (Current_Sem_Unit))));
3852 end if;
3854 if Nkind (Parent (N)) /= N_Compilation_Unit then
3855 Move_Freeze_Nodes (Id, N, Visible_Declarations (Specification (N)));
3856 Move_Freeze_Nodes (Id, N, Private_Declarations (Specification (N)));
3857 Move_Freeze_Nodes (Id, N, Generic_Formal_Declarations (N));
3859 else
3860 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
3861 Validate_RT_RAT_Component (N);
3863 -- If this is a spec without a body, check that generic parameters
3864 -- are referenced.
3866 if not Body_Required (Parent (N)) then
3867 Check_References (Id);
3868 end if;
3869 end if;
3871 -- If there is a specified storage pool in the context, create an
3872 -- aspect on the package declaration, so that it is used in any
3873 -- instance that does not override it.
3875 if Present (Default_Pool) then
3876 declare
3877 ASN : Node_Id;
3879 begin
3880 ASN :=
3881 Make_Aspect_Specification (Loc,
3882 Identifier => Make_Identifier (Loc, Name_Default_Storage_Pool),
3883 Expression => New_Copy (Default_Pool));
3885 if No (Aspect_Specifications (Specification (N))) then
3886 Set_Aspect_Specifications (Specification (N), New_List (ASN));
3887 else
3888 Append (ASN, Aspect_Specifications (Specification (N)));
3889 end if;
3890 end;
3891 end if;
3892 end Analyze_Generic_Package_Declaration;
3894 --------------------------------------------
3895 -- Analyze_Generic_Subprogram_Declaration --
3896 --------------------------------------------
3898 procedure Analyze_Generic_Subprogram_Declaration (N : Node_Id) is
3899 Formals : List_Id;
3900 Id : Entity_Id;
3901 New_N : Node_Id;
3902 Result_Type : Entity_Id;
3903 Save_Parent : Node_Id;
3904 Spec : Node_Id;
3905 Typ : Entity_Id;
3907 begin
3908 -- A generic may grant access to its private enclosing context depending
3909 -- on the placement of its corresponding body. From elaboration point of
3910 -- view, the flow of execution may enter this private context, and then
3911 -- reach an external unit, thus producing a dependency on that external
3912 -- unit. For such a path to be properly discovered and encoded in the
3913 -- ALI file of the main unit, let the ABE mechanism process the body of
3914 -- the main unit, and encode all relevant invocation constructs and the
3915 -- relations between them.
3917 Mark_Save_Invocation_Graph_Of_Body;
3919 -- Create copy of generic unit, and save for instantiation. If the unit
3920 -- is a child unit, do not copy the specifications for the parent, which
3921 -- are not part of the generic tree.
3923 Save_Parent := Parent_Spec (N);
3924 Set_Parent_Spec (N, Empty);
3926 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
3927 Set_Parent_Spec (New_N, Save_Parent);
3928 Rewrite (N, New_N);
3930 -- Once the contents of the generic copy and the template are swapped,
3931 -- do the same for their respective aspect specifications.
3933 Exchange_Aspects (N, New_N);
3935 -- Collect all contract-related source pragmas found within the template
3936 -- and attach them to the contract of the subprogram spec. This contract
3937 -- is used in the capture of global references within annotations.
3939 Create_Generic_Contract (N);
3941 Spec := Specification (N);
3942 Id := Defining_Entity (Spec);
3943 Generate_Definition (Id);
3945 if Nkind (Id) = N_Defining_Operator_Symbol then
3946 Error_Msg_N
3947 ("operator symbol not allowed for generic subprogram", Id);
3948 end if;
3950 Start_Generic;
3952 Enter_Name (Id);
3953 Set_Scope_Depth_Value (Id, Scope_Depth (Current_Scope) + 1);
3955 Push_Scope (Id);
3956 Enter_Generic_Scope (Id);
3957 Set_Inner_Instances (Id, New_Elmt_List);
3958 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3960 Analyze_Generic_Formal_Part (N);
3962 if Nkind (Spec) = N_Function_Specification then
3963 Mutate_Ekind (Id, E_Generic_Function);
3964 else
3965 Mutate_Ekind (Id, E_Generic_Procedure);
3966 end if;
3968 -- Set SPARK_Mode from context
3970 Set_SPARK_Pragma (Id, SPARK_Mode_Pragma);
3971 Set_SPARK_Pragma_Inherited (Id);
3973 -- Preserve relevant elaboration-related attributes of the context which
3974 -- are no longer available or very expensive to recompute once analysis,
3975 -- resolution, and expansion are over.
3977 Mark_Elaboration_Attributes
3978 (N_Id => Id,
3979 Checks => True,
3980 Warnings => True);
3982 Formals := Parameter_Specifications (Spec);
3984 if Present (Formals) then
3985 Process_Formals (Formals, Spec);
3986 end if;
3988 if Nkind (Spec) = N_Function_Specification then
3989 if Nkind (Result_Definition (Spec)) = N_Access_Definition then
3990 Result_Type := Access_Definition (Spec, Result_Definition (Spec));
3991 Set_Etype (Id, Result_Type);
3993 -- Check restriction imposed by AI05-073: a generic function
3994 -- cannot return an abstract type or an access to such.
3996 if Is_Abstract_Type (Designated_Type (Result_Type)) then
3997 Error_Msg_N
3998 ("generic function cannot have an access result "
3999 & "that designates an abstract type", Spec);
4000 end if;
4002 else
4003 Find_Type (Result_Definition (Spec));
4004 Typ := Entity (Result_Definition (Spec));
4006 if Is_Abstract_Type (Typ)
4007 and then Ada_Version >= Ada_2012
4008 then
4009 Error_Msg_N
4010 ("generic function cannot have abstract result type", Spec);
4011 end if;
4013 -- If a null exclusion is imposed on the result type, then create
4014 -- a null-excluding itype (an access subtype) and use it as the
4015 -- function's Etype.
4017 if Is_Access_Type (Typ)
4018 and then Null_Exclusion_Present (Spec)
4019 then
4020 Set_Etype (Id,
4021 Create_Null_Excluding_Itype
4022 (T => Typ,
4023 Related_Nod => Spec,
4024 Scope_Id => Defining_Unit_Name (Spec)));
4025 else
4026 Set_Etype (Id, Typ);
4027 end if;
4028 end if;
4030 else
4031 Set_Etype (Id, Standard_Void_Type);
4032 end if;
4034 -- Analyze the aspects of the generic copy to ensure that all generated
4035 -- pragmas (if any) perform their semantic effects.
4037 if Has_Aspects (N) then
4038 Analyze_Aspect_Specifications (N, Id);
4039 end if;
4041 -- For a library unit, we have reconstructed the entity for the unit,
4042 -- and must reset it in the library tables. We also make sure that
4043 -- Body_Required is set properly in the original compilation unit node.
4045 if Nkind (Parent (N)) = N_Compilation_Unit then
4046 Set_Cunit_Entity (Current_Sem_Unit, Id);
4047 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
4048 end if;
4050 -- If the generic appears within a package unit, the body of that unit
4051 -- has to be present for instantiation and inlining.
4053 if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Declaration
4054 and then Unit_Requires_Body (Id)
4055 then
4056 Set_Body_Needed_For_Inlining
4057 (Defining_Entity (Unit (Cunit (Current_Sem_Unit))));
4058 end if;
4060 Set_Categorization_From_Pragmas (N);
4061 Validate_Categorization_Dependency (N, Id);
4063 -- Capture all global references that occur within the profile of the
4064 -- generic subprogram. Aspects are not part of this processing because
4065 -- they must be delayed. If processed now, Save_Global_References will
4066 -- destroy the Associated_Node links and prevent the capture of global
4067 -- references when the contract of the generic subprogram is analyzed.
4069 Save_Global_References (Original_Node (N));
4071 End_Generic;
4072 End_Scope;
4073 Exit_Generic_Scope (Id);
4074 Generate_Reference_To_Formals (Id);
4076 List_Inherited_Pre_Post_Aspects (Id);
4077 end Analyze_Generic_Subprogram_Declaration;
4079 -----------------------------------
4080 -- Analyze_Package_Instantiation --
4081 -----------------------------------
4083 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
4084 -- must be replaced by gotos which jump to the end of the routine in order
4085 -- to restore the Ghost and SPARK modes.
4087 procedure Analyze_Package_Instantiation (N : Node_Id) is
4088 Has_Inline_Always : Boolean := False;
4089 -- Set if the generic unit contains any subprograms with Inline_Always.
4090 -- Only relevant when back-end inlining is not enabled.
4092 function Might_Inline_Subp (Gen_Unit : Entity_Id) return Boolean;
4093 -- Return True if inlining is active and Gen_Unit contains inlined
4094 -- subprograms. In this case, we may either instantiate the body when
4095 -- front-end inlining is enabled, or add a pending instantiation when
4096 -- back-end inlining is enabled. In the former case, this may cause
4097 -- superfluous instantiations, but in either case we need to perform
4098 -- the instantiation of the body in the context of the instance and
4099 -- not in that of the point of inlining.
4101 function Needs_Body_Instantiated (Gen_Unit : Entity_Id) return Boolean;
4102 -- Return True if Gen_Unit needs to have its body instantiated in the
4103 -- context of N. This in particular excludes generic contexts.
4105 -----------------------
4106 -- Might_Inline_Subp --
4107 -----------------------
4109 function Might_Inline_Subp (Gen_Unit : Entity_Id) return Boolean is
4110 E : Entity_Id;
4112 begin
4113 if Inline_Processing_Required then
4114 -- No need to recompute the answer if we know it is positive
4115 -- and back-end inlining is enabled.
4117 if Is_Inlined (Gen_Unit) and then Back_End_Inlining then
4118 return True;
4119 end if;
4121 E := First_Entity (Gen_Unit);
4122 while Present (E) loop
4123 if Is_Subprogram (E) and then Is_Inlined (E) then
4124 -- Remember if there are any subprograms with Inline_Always
4126 if Has_Pragma_Inline_Always (E) then
4127 Has_Inline_Always := True;
4128 end if;
4130 Set_Is_Inlined (Gen_Unit);
4131 return True;
4132 end if;
4134 Next_Entity (E);
4135 end loop;
4136 end if;
4138 return False;
4139 end Might_Inline_Subp;
4141 -------------------------------
4142 -- Needs_Body_Instantiated --
4143 -------------------------------
4145 function Needs_Body_Instantiated (Gen_Unit : Entity_Id) return Boolean is
4146 begin
4147 -- No need to instantiate bodies in generic units
4149 if Is_Generic_Unit (Cunit_Entity (Main_Unit)) then
4150 return False;
4151 end if;
4153 -- If the instantiation is in the main unit, then the body is needed
4155 if Is_In_Main_Unit (N) then
4156 return True;
4157 end if;
4159 -- In GNATprove mode, never instantiate bodies outside of the main
4160 -- unit, as it does not use frontend/backend inlining in the way that
4161 -- GNAT does, so does not benefit from such instantiations. On the
4162 -- contrary, such instantiations may bring artificial constraints,
4163 -- as for example such bodies may require preprocessing.
4165 if GNATprove_Mode then
4166 return False;
4167 end if;
4169 -- If not, then again no need to instantiate bodies in generic units
4171 if Is_Generic_Unit (Cunit_Entity (Get_Code_Unit (N))) then
4172 return False;
4173 end if;
4175 -- Here we have a special handling for back-end inlining: if inline
4176 -- processing is required, then we unconditionally want to have the
4177 -- body instantiated. The reason is that Might_Inline_Subp does not
4178 -- catch all the cases (as it does not recurse into nested packages)
4179 -- so this avoids the need to patch things up afterwards. Moreover,
4180 -- these instantiations are only performed on demand when back-end
4181 -- inlining is enabled, so this causes very little extra work.
4183 if Inline_Processing_Required and then Back_End_Inlining then
4184 return True;
4185 end if;
4187 -- We want to have the bodies instantiated in non-main units if
4188 -- they might contribute inlined subprograms.
4190 return Might_Inline_Subp (Gen_Unit);
4191 end Needs_Body_Instantiated;
4193 -- Local declarations
4195 Gen_Id : constant Node_Id := Name (N);
4196 Inst_Id : constant Entity_Id := Defining_Entity (N);
4197 Is_Actual_Pack : constant Boolean := Is_Internal (Inst_Id);
4198 Loc : constant Source_Ptr := Sloc (N);
4200 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
4201 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
4202 Saved_ISMP : constant Boolean :=
4203 Ignore_SPARK_Mode_Pragmas_In_Instance;
4204 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
4205 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
4206 -- Save the Ghost and SPARK mode-related data to restore on exit
4208 Saved_Style_Check : constant Boolean := Style_Check;
4209 -- Save style check mode for restore on exit
4211 Act_Decl : Node_Id;
4212 Act_Decl_Name : Node_Id;
4213 Act_Decl_Id : Entity_Id;
4214 Act_Spec : Node_Id;
4215 Act_Tree : Node_Id;
4216 Env_Installed : Boolean := False;
4217 Gen_Decl : Node_Id;
4218 Gen_Spec : Node_Id;
4219 Gen_Unit : Entity_Id;
4220 Inline_Now : Boolean := False;
4221 Needs_Body : Boolean;
4222 Parent_Installed : Boolean := False;
4223 Renaming_List : List_Id;
4224 Unit_Renaming : Node_Id;
4226 Vis_Prims_List : Elist_Id := No_Elist;
4227 -- List of primitives made temporarily visible in the instantiation
4228 -- to match the visibility of the formal type
4230 -- Start of processing for Analyze_Package_Instantiation
4232 begin
4233 -- Preserve relevant elaboration-related attributes of the context which
4234 -- are no longer available or very expensive to recompute once analysis,
4235 -- resolution, and expansion are over.
4237 Mark_Elaboration_Attributes
4238 (N_Id => N,
4239 Checks => True,
4240 Level => True,
4241 Modes => True,
4242 Warnings => True);
4244 -- Very first thing: check for Text_IO special unit in case we are
4245 -- instantiating one of the children of [[Wide_]Wide_]Text_IO.
4247 Check_Text_IO_Special_Unit (Name (N));
4249 -- Make node global for error reporting
4251 Instantiation_Node := N;
4253 -- Case of instantiation of a generic package
4255 if Nkind (N) = N_Package_Instantiation then
4256 Act_Decl_Id := New_Copy (Defining_Entity (N));
4257 Set_Comes_From_Source (Act_Decl_Id, True);
4259 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name then
4260 Act_Decl_Name :=
4261 Make_Defining_Program_Unit_Name (Loc,
4262 Name =>
4263 New_Copy_Tree (Name (Defining_Unit_Name (N))),
4264 Defining_Identifier => Act_Decl_Id);
4265 else
4266 Act_Decl_Name := Act_Decl_Id;
4267 end if;
4269 -- Case of instantiation of a formal package
4271 else
4272 Act_Decl_Id := Defining_Identifier (N);
4273 Act_Decl_Name := Act_Decl_Id;
4274 end if;
4276 Generate_Definition (Act_Decl_Id);
4277 Mutate_Ekind (Act_Decl_Id, E_Package);
4279 -- Initialize list of incomplete actuals before analysis
4281 Set_Incomplete_Actuals (Act_Decl_Id, New_Elmt_List);
4283 Preanalyze_Actuals (N, Act_Decl_Id);
4285 -- Turn off style checking in instances. If the check is enabled on the
4286 -- generic unit, a warning in an instance would just be noise. If not
4287 -- enabled on the generic, then a warning in an instance is just wrong.
4288 -- This must be done after analyzing the actuals, which do come from
4289 -- source and are subject to style checking.
4291 Style_Check := False;
4293 Init_Env;
4294 Env_Installed := True;
4296 -- Reset renaming map for formal types. The mapping is established
4297 -- when analyzing the generic associations, but some mappings are
4298 -- inherited from formal packages of parent units, and these are
4299 -- constructed when the parents are installed.
4301 Generic_Renamings.Set_Last (0);
4302 Generic_Renamings_HTable.Reset;
4304 -- Except for an abbreviated instance created to check a formal package,
4305 -- install the parent if this is a generic child unit.
4307 if not Is_Abbreviated_Instance (Inst_Id) then
4308 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
4309 end if;
4311 Gen_Unit := Entity (Gen_Id);
4313 -- A package instantiation is Ghost when it is subject to pragma Ghost
4314 -- or the generic template is Ghost. Set the mode now to ensure that
4315 -- any nodes generated during analysis and expansion are marked as
4316 -- Ghost.
4318 Mark_And_Set_Ghost_Instantiation (N, Gen_Unit);
4320 -- Verify that it is the name of a generic package
4322 -- A visibility glitch: if the instance is a child unit and the generic
4323 -- is the generic unit of a parent instance (i.e. both the parent and
4324 -- the child units are instances of the same package) the name now
4325 -- denotes the renaming within the parent, not the intended generic
4326 -- unit. See if there is a homonym that is the desired generic. The
4327 -- renaming declaration must be visible inside the instance of the
4328 -- child, but not when analyzing the name in the instantiation itself.
4330 if Ekind (Gen_Unit) = E_Package
4331 and then Present (Renamed_Entity (Gen_Unit))
4332 and then In_Open_Scopes (Renamed_Entity (Gen_Unit))
4333 and then Is_Generic_Instance (Renamed_Entity (Gen_Unit))
4334 and then Present (Homonym (Gen_Unit))
4335 then
4336 Gen_Unit := Homonym (Gen_Unit);
4337 end if;
4339 if Etype (Gen_Unit) = Any_Type then
4340 Restore_Env;
4341 goto Leave;
4343 elsif Ekind (Gen_Unit) /= E_Generic_Package then
4345 -- Ada 2005 (AI-50217): Cannot use instance in limited with_clause
4347 if From_Limited_With (Gen_Unit) then
4348 Error_Msg_N
4349 ("cannot instantiate a limited withed package", Gen_Id);
4350 else
4351 Error_Msg_NE
4352 ("& is not the name of a generic package", Gen_Id, Gen_Unit);
4353 end if;
4355 Restore_Env;
4356 goto Leave;
4357 end if;
4359 if In_Extended_Main_Source_Unit (N) then
4360 Set_Is_Instantiated (Gen_Unit);
4361 Generate_Reference (Gen_Unit, N);
4363 if Present (Renamed_Entity (Gen_Unit)) then
4364 Set_Is_Instantiated (Renamed_Entity (Gen_Unit));
4365 Generate_Reference (Renamed_Entity (Gen_Unit), N);
4366 end if;
4367 end if;
4369 if Nkind (Gen_Id) = N_Identifier
4370 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
4371 then
4372 Error_Msg_NE
4373 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
4375 elsif Nkind (Gen_Id) = N_Expanded_Name
4376 and then Is_Child_Unit (Gen_Unit)
4377 and then Nkind (Prefix (Gen_Id)) = N_Identifier
4378 and then Chars (Act_Decl_Id) = Chars (Prefix (Gen_Id))
4379 then
4380 Error_Msg_N
4381 ("& is hidden within declaration of instance", Prefix (Gen_Id));
4382 end if;
4384 Set_Entity (Gen_Id, Gen_Unit);
4386 -- If generic is a renaming, get original generic unit
4388 if Present (Renamed_Entity (Gen_Unit))
4389 and then Ekind (Renamed_Entity (Gen_Unit)) = E_Generic_Package
4390 then
4391 Gen_Unit := Renamed_Entity (Gen_Unit);
4392 end if;
4394 -- Verify that there are no circular instantiations
4396 if In_Open_Scopes (Gen_Unit) then
4397 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
4398 Restore_Env;
4399 goto Leave;
4401 elsif Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
4402 Error_Msg_Node_2 := Current_Scope;
4403 Error_Msg_NE
4404 ("circular instantiation: & instantiated in &!", N, Gen_Unit);
4405 Circularity_Detected := True;
4406 Restore_Env;
4407 goto Leave;
4409 else
4410 Mutate_Ekind (Inst_Id, E_Package);
4411 Set_Scope (Inst_Id, Current_Scope);
4413 -- If the context of the instance is subject to SPARK_Mode "off" or
4414 -- the annotation is altogether missing, set the global flag which
4415 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
4416 -- the instance.
4418 if SPARK_Mode /= On then
4419 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
4421 -- Mark the instance spec in case the body is instantiated at a
4422 -- later pass. This preserves the original context in effect for
4423 -- the body.
4425 Set_Ignore_SPARK_Mode_Pragmas (Act_Decl_Id);
4426 end if;
4428 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
4429 Gen_Spec := Specification (Gen_Decl);
4431 -- Initialize renamings map, for error checking, and the list that
4432 -- holds private entities whose views have changed between generic
4433 -- definition and instantiation. If this is the instance created to
4434 -- validate an actual package, the instantiation environment is that
4435 -- of the enclosing instance.
4437 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
4439 -- Copy original generic tree, to produce text for instantiation
4441 Act_Tree :=
4442 Copy_Generic_Node
4443 (Original_Node (Gen_Decl), Empty, Instantiating => True);
4445 Act_Spec := Specification (Act_Tree);
4447 -- If this is the instance created to validate an actual package,
4448 -- only the formals matter, do not examine the package spec itself.
4450 if Is_Actual_Pack then
4451 Set_Visible_Declarations (Act_Spec, New_List);
4452 Set_Private_Declarations (Act_Spec, New_List);
4453 end if;
4455 Renaming_List :=
4456 Analyze_Associations
4457 (I_Node => N,
4458 Formals => Generic_Formal_Declarations (Act_Tree),
4459 F_Copy => Generic_Formal_Declarations (Gen_Decl));
4461 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
4463 Set_Instance_Env (Gen_Unit, Act_Decl_Id);
4464 Set_Defining_Unit_Name (Act_Spec, Act_Decl_Name);
4465 Set_Is_Generic_Instance (Act_Decl_Id);
4466 Set_Generic_Parent (Act_Spec, Gen_Unit);
4468 -- References to the generic in its own declaration or its body are
4469 -- references to the instance. Add a renaming declaration for the
4470 -- generic unit itself. This declaration, as well as the renaming
4471 -- declarations for the generic formals, must remain private to the
4472 -- unit: the formals, because this is the language semantics, and
4473 -- the unit because its use is an artifact of the implementation.
4475 Unit_Renaming :=
4476 Make_Package_Renaming_Declaration (Loc,
4477 Defining_Unit_Name =>
4478 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
4479 Name => New_Occurrence_Of (Act_Decl_Id, Loc));
4481 Append (Unit_Renaming, Renaming_List);
4483 -- The renaming declarations are the first local declarations of the
4484 -- new unit.
4486 if Is_Non_Empty_List (Visible_Declarations (Act_Spec)) then
4487 Insert_List_Before
4488 (First (Visible_Declarations (Act_Spec)), Renaming_List);
4489 else
4490 Set_Visible_Declarations (Act_Spec, Renaming_List);
4491 end if;
4493 Act_Decl := Make_Package_Declaration (Loc, Specification => Act_Spec);
4495 -- Propagate the aspect specifications from the package declaration
4496 -- template to the instantiated version of the package declaration.
4498 if Has_Aspects (Act_Tree) then
4499 Set_Aspect_Specifications (Act_Decl,
4500 New_Copy_List_Tree (Aspect_Specifications (Act_Tree)));
4501 end if;
4503 -- The generic may have a generated Default_Storage_Pool aspect,
4504 -- set at the point of generic declaration. If the instance has
4505 -- that aspect, it overrides the one inherited from the generic.
4507 if Has_Aspects (Gen_Spec) then
4508 if No (Aspect_Specifications (N)) then
4509 Set_Aspect_Specifications (N,
4510 (New_Copy_List_Tree
4511 (Aspect_Specifications (Gen_Spec))));
4513 else
4514 declare
4515 Inherited_Aspects : constant List_Id :=
4516 New_Copy_List_Tree
4517 (Aspect_Specifications (Gen_Spec));
4519 ASN1 : Node_Id;
4520 ASN2 : Node_Id;
4521 Pool_Present : Boolean := False;
4523 begin
4524 ASN1 := First (Aspect_Specifications (N));
4525 while Present (ASN1) loop
4526 if Chars (Identifier (ASN1)) =
4527 Name_Default_Storage_Pool
4528 then
4529 Pool_Present := True;
4530 exit;
4531 end if;
4533 Next (ASN1);
4534 end loop;
4536 if Pool_Present then
4538 -- If generic carries a default storage pool, remove it
4539 -- in favor of the instance one.
4541 ASN2 := First (Inherited_Aspects);
4542 while Present (ASN2) loop
4543 if Chars (Identifier (ASN2)) =
4544 Name_Default_Storage_Pool
4545 then
4546 Remove (ASN2);
4547 exit;
4548 end if;
4550 Next (ASN2);
4551 end loop;
4552 end if;
4554 Prepend_List_To
4555 (Aspect_Specifications (N), Inherited_Aspects);
4556 end;
4557 end if;
4558 end if;
4560 -- Save the instantiation node for a subsequent instantiation of the
4561 -- body if there is one and it needs to be instantiated here.
4563 -- We instantiate the body only if we are generating code, or if we
4564 -- are generating cross-reference information, or for GNATprove use.
4566 declare
4567 Enclosing_Body_Present : Boolean := False;
4568 -- If the generic unit is not a compilation unit, then a body may
4569 -- be present in its parent even if none is required. We create a
4570 -- tentative pending instantiation for the body, which will be
4571 -- discarded if none is actually present.
4573 Scop : Entity_Id;
4575 begin
4576 if Scope (Gen_Unit) /= Standard_Standard
4577 and then not Is_Child_Unit (Gen_Unit)
4578 then
4579 Scop := Scope (Gen_Unit);
4580 while Present (Scop) and then Scop /= Standard_Standard loop
4581 if Unit_Requires_Body (Scop) then
4582 Enclosing_Body_Present := True;
4583 exit;
4585 elsif In_Open_Scopes (Scop)
4586 and then In_Package_Body (Scop)
4587 then
4588 Enclosing_Body_Present := True;
4589 exit;
4590 end if;
4592 exit when Is_Compilation_Unit (Scop);
4593 Scop := Scope (Scop);
4594 end loop;
4595 end if;
4597 -- If front-end inlining is enabled or there are any subprograms
4598 -- marked with Inline_Always, and this is a unit for which code
4599 -- will be generated, we instantiate the body at once.
4601 -- This is done if the instance is not the main unit, and if the
4602 -- generic is not a child unit of another generic, to avoid scope
4603 -- problems and the reinstallation of parent instances.
4605 if Expander_Active
4606 and then (not Is_Child_Unit (Gen_Unit)
4607 or else not Is_Generic_Unit (Scope (Gen_Unit)))
4608 and then Might_Inline_Subp (Gen_Unit)
4609 and then not Is_Actual_Pack
4610 then
4611 if not Back_End_Inlining
4612 and then (Front_End_Inlining or else Has_Inline_Always)
4613 and then (Is_In_Main_Unit (N)
4614 or else In_Main_Context (Current_Scope))
4615 and then Nkind (Parent (N)) /= N_Compilation_Unit
4616 then
4617 Inline_Now := True;
4619 -- In configurable_run_time mode we force the inlining of
4620 -- predefined subprograms marked Inline_Always, to minimize
4621 -- the use of the run-time library.
4623 elsif In_Predefined_Unit (Gen_Decl)
4624 and then Configurable_Run_Time_Mode
4625 and then Nkind (Parent (N)) /= N_Compilation_Unit
4626 then
4627 Inline_Now := True;
4628 end if;
4630 -- If the current scope is itself an instance within a child
4631 -- unit, there will be duplications in the scope stack, and the
4632 -- unstacking mechanism in Inline_Instance_Body will fail.
4633 -- This loses some rare cases of optimization.
4635 if Is_Generic_Instance (Current_Scope) then
4636 declare
4637 Curr_Unit : constant Entity_Id :=
4638 Cunit_Entity (Current_Sem_Unit);
4639 begin
4640 if Curr_Unit /= Current_Scope
4641 and then Is_Child_Unit (Curr_Unit)
4642 then
4643 Inline_Now := False;
4644 end if;
4645 end;
4646 end if;
4647 end if;
4649 Needs_Body :=
4650 (Unit_Requires_Body (Gen_Unit)
4651 or else Enclosing_Body_Present
4652 or else Present (Corresponding_Body (Gen_Decl)))
4653 and then Needs_Body_Instantiated (Gen_Unit)
4654 and then not Is_Actual_Pack
4655 and then not Inline_Now
4656 and then (Operating_Mode = Generate_Code
4657 or else (Operating_Mode = Check_Semantics
4658 and then GNATprove_Mode));
4660 -- If front-end inlining is enabled or there are any subprograms
4661 -- marked with Inline_Always, do not instantiate body when within
4662 -- a generic context.
4664 if not Back_End_Inlining
4665 and then (Front_End_Inlining or else Has_Inline_Always)
4666 and then not Expander_Active
4667 then
4668 Needs_Body := False;
4669 end if;
4671 -- If the current context is generic, and the package being
4672 -- instantiated is declared within a formal package, there is no
4673 -- body to instantiate until the enclosing generic is instantiated
4674 -- and there is an actual for the formal package. If the formal
4675 -- package has parameters, we build a regular package instance for
4676 -- it, that precedes the original formal package declaration.
4678 if In_Open_Scopes (Scope (Scope (Gen_Unit))) then
4679 declare
4680 Decl : constant Node_Id :=
4681 Original_Node
4682 (Unit_Declaration_Node (Scope (Gen_Unit)));
4683 begin
4684 if Nkind (Decl) = N_Formal_Package_Declaration
4685 or else (Nkind (Decl) = N_Package_Declaration
4686 and then Is_List_Member (Decl)
4687 and then Present (Next (Decl))
4688 and then
4689 Nkind (Next (Decl)) =
4690 N_Formal_Package_Declaration)
4691 then
4692 Needs_Body := False;
4693 end if;
4694 end;
4695 end if;
4696 end;
4698 -- For RCI unit calling stubs, we omit the instance body if the
4699 -- instance is the RCI library unit itself.
4701 -- However there is a special case for nested instances: in this case
4702 -- we do generate the instance body, as it might be required, e.g.
4703 -- because it provides stream attributes for some type used in the
4704 -- profile of a remote subprogram. This is consistent with 12.3(12),
4705 -- which indicates that the instance body occurs at the place of the
4706 -- instantiation, and thus is part of the RCI declaration, which is
4707 -- present on all client partitions (this is E.2.3(18)).
4709 -- Note that AI12-0002 may make it illegal at some point to have
4710 -- stream attributes defined in an RCI unit, in which case this
4711 -- special case will become unnecessary. In the meantime, there
4712 -- is known application code in production that depends on this
4713 -- being possible, so we definitely cannot eliminate the body in
4714 -- the case of nested instances for the time being.
4716 -- When we generate a nested instance body, calling stubs for any
4717 -- relevant subprogram will be inserted immediately after the
4718 -- subprogram declarations, and will take precedence over the
4719 -- subsequent (original) body. (The stub and original body will be
4720 -- complete homographs, but this is permitted in an instance).
4721 -- (Could we do better and remove the original body???)
4723 if Distribution_Stub_Mode = Generate_Caller_Stub_Body
4724 and then Comes_From_Source (N)
4725 and then Nkind (Parent (N)) = N_Compilation_Unit
4726 then
4727 Needs_Body := False;
4728 end if;
4730 if Needs_Body then
4731 -- Indicate that the enclosing scopes contain an instantiation,
4732 -- and that cleanup actions should be delayed until after the
4733 -- instance body is expanded.
4735 Check_Forward_Instantiation (Gen_Decl);
4736 if Nkind (N) = N_Package_Instantiation then
4737 declare
4738 Enclosing_Master : Entity_Id;
4740 begin
4741 -- Loop to search enclosing masters
4743 Enclosing_Master := Current_Scope;
4744 Scope_Loop : while Enclosing_Master /= Standard_Standard loop
4745 if Ekind (Enclosing_Master) = E_Package then
4746 if Is_Compilation_Unit (Enclosing_Master) then
4747 if In_Package_Body (Enclosing_Master) then
4748 Set_Delay_Subprogram_Descriptors
4749 (Body_Entity (Enclosing_Master));
4750 else
4751 Set_Delay_Subprogram_Descriptors
4752 (Enclosing_Master);
4753 end if;
4755 exit Scope_Loop;
4757 else
4758 Enclosing_Master := Scope (Enclosing_Master);
4759 end if;
4761 elsif Is_Generic_Unit (Enclosing_Master)
4762 or else Ekind (Enclosing_Master) = E_Void
4763 then
4764 -- Cleanup actions will eventually be performed on the
4765 -- enclosing subprogram or package instance, if any.
4766 -- Enclosing scope is void in the formal part of a
4767 -- generic subprogram.
4769 exit Scope_Loop;
4771 else
4772 if Ekind (Enclosing_Master) = E_Entry
4773 and then
4774 Ekind (Scope (Enclosing_Master)) = E_Protected_Type
4775 then
4776 if not Expander_Active then
4777 exit Scope_Loop;
4778 else
4779 Enclosing_Master :=
4780 Protected_Body_Subprogram (Enclosing_Master);
4781 end if;
4782 end if;
4784 Set_Delay_Cleanups (Enclosing_Master);
4786 while Ekind (Enclosing_Master) = E_Block loop
4787 Enclosing_Master := Scope (Enclosing_Master);
4788 end loop;
4790 if Is_Subprogram (Enclosing_Master) then
4791 Set_Delay_Subprogram_Descriptors (Enclosing_Master);
4793 elsif Is_Task_Type (Enclosing_Master) then
4794 declare
4795 TBP : constant Node_Id :=
4796 Get_Task_Body_Procedure
4797 (Enclosing_Master);
4798 begin
4799 if Present (TBP) then
4800 Set_Delay_Subprogram_Descriptors (TBP);
4801 Set_Delay_Cleanups (TBP);
4802 end if;
4803 end;
4804 end if;
4806 exit Scope_Loop;
4807 end if;
4808 end loop Scope_Loop;
4809 end;
4811 -- Make entry in table
4813 Add_Pending_Instantiation (N, Act_Decl);
4814 end if;
4815 end if;
4817 Set_Categorization_From_Pragmas (Act_Decl);
4819 if Parent_Installed then
4820 Hide_Current_Scope;
4821 end if;
4823 Set_Instance_Spec (N, Act_Decl);
4825 -- If not a compilation unit, insert the package declaration before
4826 -- the original instantiation node.
4828 if Nkind (Parent (N)) /= N_Compilation_Unit then
4829 Mark_Rewrite_Insertion (Act_Decl);
4830 Insert_Before (N, Act_Decl);
4832 if Has_Aspects (N) then
4833 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4835 -- The pragma created for a Default_Storage_Pool aspect must
4836 -- appear ahead of the declarations in the instance spec.
4837 -- Analysis has placed it after the instance node, so remove
4838 -- it and reinsert it properly now.
4840 declare
4841 ASN : constant Node_Id := First (Aspect_Specifications (N));
4842 A_Name : constant Name_Id := Chars (Identifier (ASN));
4843 Decl : Node_Id;
4845 begin
4846 if A_Name = Name_Default_Storage_Pool then
4847 if No (Visible_Declarations (Act_Spec)) then
4848 Set_Visible_Declarations (Act_Spec, New_List);
4849 end if;
4851 Decl := Next (N);
4852 while Present (Decl) loop
4853 if Nkind (Decl) = N_Pragma then
4854 Remove (Decl);
4855 Prepend (Decl, Visible_Declarations (Act_Spec));
4856 exit;
4857 end if;
4859 Next (Decl);
4860 end loop;
4861 end if;
4862 end;
4863 end if;
4865 Analyze (Act_Decl);
4867 -- For an instantiation that is a compilation unit, place
4868 -- declaration on current node so context is complete for analysis
4869 -- (including nested instantiations). If this is the main unit,
4870 -- the declaration eventually replaces the instantiation node.
4871 -- If the instance body is created later, it replaces the
4872 -- instance node, and the declaration is attached to it
4873 -- (see Build_Instance_Compilation_Unit_Nodes).
4875 else
4876 if Cunit_Entity (Current_Sem_Unit) = Defining_Entity (N) then
4878 -- The entity for the current unit is the newly created one,
4879 -- and all semantic information is attached to it.
4881 Set_Cunit_Entity (Current_Sem_Unit, Act_Decl_Id);
4883 -- If this is the main unit, replace the main entity as well
4885 if Current_Sem_Unit = Main_Unit then
4886 Main_Unit_Entity := Act_Decl_Id;
4887 end if;
4888 end if;
4890 Set_Unit (Parent (N), Act_Decl);
4891 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
4892 Set_Package_Instantiation (Act_Decl_Id, N);
4894 -- Process aspect specifications of the instance node, if any, to
4895 -- take into account categorization pragmas before analyzing the
4896 -- instance.
4898 if Has_Aspects (N) then
4899 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4900 end if;
4902 Analyze (Act_Decl);
4903 Set_Unit (Parent (N), N);
4904 Set_Body_Required (Parent (N), False);
4906 -- We never need elaboration checks on instantiations, since by
4907 -- definition, the body instantiation is elaborated at the same
4908 -- time as the spec instantiation.
4910 if Legacy_Elaboration_Checks then
4911 Set_Kill_Elaboration_Checks (Act_Decl_Id);
4912 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
4913 end if;
4914 end if;
4916 if Legacy_Elaboration_Checks then
4917 Check_Elab_Instantiation (N);
4918 end if;
4920 -- Save the scenario for later examination by the ABE Processing
4921 -- phase.
4923 Record_Elaboration_Scenario (N);
4925 -- The instantiation results in a guaranteed ABE
4927 if Is_Known_Guaranteed_ABE (N) and then Needs_Body then
4928 -- Do not instantiate the corresponding body because gigi cannot
4929 -- handle certain types of premature instantiations.
4931 Remove_Dead_Instance (N);
4933 -- Create completing bodies for all subprogram declarations since
4934 -- their real bodies will not be instantiated.
4936 Provide_Completing_Bodies (Instance_Spec (N));
4937 end if;
4939 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
4941 Set_First_Private_Entity (Defining_Unit_Name (Unit_Renaming),
4942 First_Private_Entity (Act_Decl_Id));
4944 -- If the instantiation will receive a body, the unit will be
4945 -- transformed into a package body, and receive its own elaboration
4946 -- entity. Otherwise, the nature of the unit is now a package
4947 -- declaration.
4949 if Nkind (Parent (N)) = N_Compilation_Unit
4950 and then not Needs_Body
4951 then
4952 Rewrite (N, Act_Decl);
4953 end if;
4955 if Present (Corresponding_Body (Gen_Decl))
4956 or else Unit_Requires_Body (Gen_Unit)
4957 then
4958 Set_Has_Completion (Act_Decl_Id);
4959 end if;
4961 Check_Formal_Packages (Act_Decl_Id);
4963 Restore_Hidden_Primitives (Vis_Prims_List);
4964 Restore_Private_Views (Act_Decl_Id);
4966 Inherit_Context (Gen_Decl, N);
4968 if Parent_Installed then
4969 Remove_Parent;
4970 end if;
4972 Restore_Env;
4973 Env_Installed := False;
4974 end if;
4976 Validate_Categorization_Dependency (N, Act_Decl_Id);
4978 -- There used to be a check here to prevent instantiations in local
4979 -- contexts if the No_Local_Allocators restriction was active. This
4980 -- check was removed by a binding interpretation in AI-95-00130/07,
4981 -- but we retain the code for documentation purposes.
4983 -- if Ekind (Act_Decl_Id) /= E_Void
4984 -- and then not Is_Library_Level_Entity (Act_Decl_Id)
4985 -- then
4986 -- Check_Restriction (No_Local_Allocators, N);
4987 -- end if;
4989 if Inline_Now then
4990 Inline_Instance_Body (N, Gen_Unit, Act_Decl);
4991 end if;
4993 -- Check that if N is an instantiation of System.Dim_Float_IO or
4994 -- System.Dim_Integer_IO, the formal type has a dimension system.
4996 if Nkind (N) = N_Package_Instantiation
4997 and then Is_Dim_IO_Package_Instantiation (N)
4998 then
4999 declare
5000 Assoc : constant Node_Id := First (Generic_Associations (N));
5001 begin
5002 if not Has_Dimension_System
5003 (Etype (Explicit_Generic_Actual_Parameter (Assoc)))
5004 then
5005 Error_Msg_N ("type with a dimension system expected", Assoc);
5006 end if;
5007 end;
5008 end if;
5010 <<Leave>>
5011 if Has_Aspects (N) and then Nkind (Parent (N)) /= N_Compilation_Unit then
5012 Analyze_Aspect_Specifications (N, Act_Decl_Id);
5013 end if;
5015 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
5016 Restore_Ghost_Region (Saved_GM, Saved_IGR);
5017 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
5018 Style_Check := Saved_Style_Check;
5020 exception
5021 when Instantiation_Error =>
5022 if Parent_Installed then
5023 Remove_Parent;
5024 end if;
5026 if Env_Installed then
5027 Restore_Env;
5028 end if;
5030 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
5031 Restore_Ghost_Region (Saved_GM, Saved_IGR);
5032 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
5033 Style_Check := Saved_Style_Check;
5034 end Analyze_Package_Instantiation;
5036 --------------------------
5037 -- Inline_Instance_Body --
5038 --------------------------
5040 -- WARNING: This routine manages SPARK regions. Return statements must be
5041 -- replaced by gotos which jump to the end of the routine and restore the
5042 -- SPARK mode.
5044 procedure Inline_Instance_Body
5045 (N : Node_Id;
5046 Gen_Unit : Entity_Id;
5047 Act_Decl : Node_Id)
5049 Config_Attrs : constant Config_Switches_Type := Save_Config_Switches;
5051 Curr_Comp : constant Node_Id := Cunit (Current_Sem_Unit);
5052 Curr_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
5053 Gen_Comp : constant Entity_Id :=
5054 Cunit_Entity (Get_Source_Unit (Gen_Unit));
5056 Scope_Stack_Depth : constant Pos :=
5057 Scope_Stack.Last - Scope_Stack.First + 1;
5059 Inner_Scopes : array (1 .. Scope_Stack_Depth) of Entity_Id;
5060 Instances : array (1 .. Scope_Stack_Depth) of Entity_Id;
5061 Use_Clauses : array (1 .. Scope_Stack_Depth) of Node_Id;
5063 Curr_Scope : Entity_Id := Empty;
5064 List : Elist_Id := No_Elist; -- init to avoid warning
5065 N_Instances : Nat := 0;
5066 Num_Inner : Nat := 0;
5067 Num_Scopes : Nat := 0;
5068 Removed : Boolean := False;
5069 S : Entity_Id;
5070 Vis : Boolean;
5072 begin
5073 -- Case of generic unit defined in another unit. We must remove the
5074 -- complete context of the current unit to install that of the generic.
5076 if Gen_Comp /= Cunit_Entity (Current_Sem_Unit) then
5078 -- Loop through enclosing scopes until we reach a generic instance,
5079 -- package body, or subprogram.
5081 S := Current_Scope;
5082 while Present (S) and then S /= Standard_Standard loop
5084 -- Save use clauses from enclosing scopes into Use_Clauses
5086 loop
5087 Num_Scopes := Num_Scopes + 1;
5089 Use_Clauses (Num_Scopes) :=
5090 (Scope_Stack.Table
5091 (Scope_Stack.Last - Num_Scopes + 1).First_Use_Clause);
5092 End_Use_Clauses (Use_Clauses (Num_Scopes));
5094 exit when Scope_Stack.Last - Num_Scopes + 1 = Scope_Stack.First
5095 or else Scope_Stack.Table
5096 (Scope_Stack.Last - Num_Scopes).Entity = Scope (S);
5097 end loop;
5099 exit when Is_Generic_Instance (S)
5100 and then (In_Package_Body (S)
5101 or else Ekind (S) = E_Procedure
5102 or else Ekind (S) = E_Function);
5103 S := Scope (S);
5104 end loop;
5106 Vis := Is_Immediately_Visible (Gen_Comp);
5108 -- Find and save all enclosing instances
5110 S := Current_Scope;
5112 while Present (S)
5113 and then S /= Standard_Standard
5114 loop
5115 if Is_Generic_Instance (S) then
5116 N_Instances := N_Instances + 1;
5117 Instances (N_Instances) := S;
5119 exit when In_Package_Body (S);
5120 end if;
5122 S := Scope (S);
5123 end loop;
5125 -- Remove context of current compilation unit, unless we are within a
5126 -- nested package instantiation, in which case the context has been
5127 -- removed previously.
5129 -- If current scope is the body of a child unit, remove context of
5130 -- spec as well. If an enclosing scope is an instance body, the
5131 -- context has already been removed, but the entities in the body
5132 -- must be made invisible as well.
5134 S := Current_Scope;
5135 while Present (S) and then S /= Standard_Standard loop
5136 if Is_Generic_Instance (S)
5137 and then (In_Package_Body (S)
5138 or else Ekind (S) in E_Procedure | E_Function)
5139 then
5140 -- We still have to remove the entities of the enclosing
5141 -- instance from direct visibility.
5143 declare
5144 E : Entity_Id;
5145 begin
5146 E := First_Entity (S);
5147 while Present (E) loop
5148 Set_Is_Immediately_Visible (E, False);
5149 Next_Entity (E);
5150 end loop;
5151 end;
5153 exit;
5154 end if;
5156 if S = Curr_Unit
5157 or else (Ekind (Curr_Unit) = E_Package_Body
5158 and then S = Spec_Entity (Curr_Unit))
5159 or else (Ekind (Curr_Unit) = E_Subprogram_Body
5160 and then S = Corresponding_Spec
5161 (Unit_Declaration_Node (Curr_Unit)))
5162 then
5163 Removed := True;
5165 -- Remove entities in current scopes from visibility, so that
5166 -- instance body is compiled in a clean environment.
5168 List := Save_Scope_Stack (Handle_Use => False);
5170 if Is_Child_Unit (S) then
5172 -- Remove child unit from stack, as well as inner scopes.
5173 -- Removing the context of a child unit removes parent units
5174 -- as well.
5176 while Current_Scope /= S loop
5177 Num_Inner := Num_Inner + 1;
5178 Inner_Scopes (Num_Inner) := Current_Scope;
5179 Pop_Scope;
5180 end loop;
5182 Pop_Scope;
5183 Remove_Context (Curr_Comp);
5184 Curr_Scope := S;
5186 else
5187 Remove_Context (Curr_Comp);
5188 end if;
5190 if Ekind (Curr_Unit) = E_Package_Body then
5191 Remove_Context (Library_Unit (Curr_Comp));
5192 end if;
5193 end if;
5195 S := Scope (S);
5196 end loop;
5198 pragma Assert (Num_Inner < Num_Scopes);
5200 Push_Scope (Standard_Standard);
5201 Scope_Stack.Table (Scope_Stack.Last).Is_Active_Stack_Base := True;
5203 -- The inlined package body is analyzed with the configuration state
5204 -- of the context prior to the scope manipulations performed above.
5206 -- ??? shouldn't this also use the warning state of the context prior
5207 -- to the scope manipulations?
5209 Instantiate_Package_Body
5210 (Body_Info =>
5211 ((Act_Decl => Act_Decl,
5212 Config_Switches => Config_Attrs,
5213 Current_Sem_Unit => Current_Sem_Unit,
5214 Expander_Status => Expander_Active,
5215 Inst_Node => N,
5216 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
5217 Scope_Suppress => Scope_Suppress,
5218 Warnings => Save_Warnings)),
5219 Inlined_Body => True);
5221 Pop_Scope;
5223 -- Restore context
5225 Set_Is_Immediately_Visible (Gen_Comp, Vis);
5227 -- Reset Generic_Instance flag so that use clauses can be installed
5228 -- in the proper order. (See Use_One_Package for effect of enclosing
5229 -- instances on processing of use clauses).
5231 for J in 1 .. N_Instances loop
5232 Set_Is_Generic_Instance (Instances (J), False);
5233 end loop;
5235 if Removed then
5236 Install_Context (Curr_Comp, Chain => False);
5238 if Present (Curr_Scope)
5239 and then Is_Child_Unit (Curr_Scope)
5240 then
5241 Push_Scope (Curr_Scope);
5242 Set_Is_Immediately_Visible (Curr_Scope);
5244 -- Finally, restore inner scopes as well
5246 for J in reverse 1 .. Num_Inner loop
5247 Push_Scope (Inner_Scopes (J));
5248 end loop;
5249 end if;
5251 Restore_Scope_Stack (List, Handle_Use => False);
5253 if Present (Curr_Scope)
5254 and then
5255 (In_Private_Part (Curr_Scope)
5256 or else In_Package_Body (Curr_Scope))
5257 then
5258 -- Install private declaration of ancestor units, which are
5259 -- currently available. Restore_Scope_Stack and Install_Context
5260 -- only install the visible part of parents.
5262 declare
5263 Par : Entity_Id;
5264 begin
5265 Par := Scope (Curr_Scope);
5266 while (Present (Par)) and then Par /= Standard_Standard loop
5267 Install_Private_Declarations (Par);
5268 Par := Scope (Par);
5269 end loop;
5270 end;
5271 end if;
5272 end if;
5274 -- Restore use clauses. For a child unit, use clauses in the parents
5275 -- are restored when installing the context, so only those in inner
5276 -- scopes (and those local to the child unit itself) need to be
5277 -- installed explicitly.
5279 if Is_Child_Unit (Curr_Unit) and then Removed then
5280 for J in reverse 1 .. Num_Inner + 1 loop
5281 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
5282 Use_Clauses (J);
5283 Install_Use_Clauses (Use_Clauses (J));
5284 end loop;
5286 else
5287 for J in reverse 1 .. Num_Scopes loop
5288 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
5289 Use_Clauses (J);
5290 Install_Use_Clauses (Use_Clauses (J));
5291 end loop;
5292 end if;
5294 -- Restore status of instances. If one of them is a body, make its
5295 -- local entities visible again.
5297 declare
5298 E : Entity_Id;
5299 Inst : Entity_Id;
5301 begin
5302 for J in 1 .. N_Instances loop
5303 Inst := Instances (J);
5304 Set_Is_Generic_Instance (Inst, True);
5306 if In_Package_Body (Inst)
5307 or else Ekind (S) in E_Procedure | E_Function
5308 then
5309 E := First_Entity (Instances (J));
5310 while Present (E) loop
5311 Set_Is_Immediately_Visible (E);
5312 Next_Entity (E);
5313 end loop;
5314 end if;
5315 end loop;
5316 end;
5318 -- If generic unit is in current unit, current context is correct. Note
5319 -- that the context is guaranteed to carry the correct SPARK_Mode as no
5320 -- enclosing scopes were removed.
5322 else
5323 Instantiate_Package_Body
5324 (Body_Info =>
5325 ((Act_Decl => Act_Decl,
5326 Config_Switches => Save_Config_Switches,
5327 Current_Sem_Unit => Current_Sem_Unit,
5328 Expander_Status => Expander_Active,
5329 Inst_Node => N,
5330 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
5331 Scope_Suppress => Scope_Suppress,
5332 Warnings => Save_Warnings)),
5333 Inlined_Body => True);
5334 end if;
5335 end Inline_Instance_Body;
5337 -------------------------------------
5338 -- Analyze_Procedure_Instantiation --
5339 -------------------------------------
5341 procedure Analyze_Procedure_Instantiation (N : Node_Id) is
5342 begin
5343 Analyze_Subprogram_Instantiation (N, E_Procedure);
5344 end Analyze_Procedure_Instantiation;
5346 -----------------------------------
5347 -- Need_Subprogram_Instance_Body --
5348 -----------------------------------
5350 function Need_Subprogram_Instance_Body
5351 (N : Node_Id;
5352 Subp : Entity_Id) return Boolean
5354 function Is_Inlined_Or_Child_Of_Inlined (E : Entity_Id) return Boolean;
5355 -- Return True if E is an inlined subprogram, an inlined renaming or a
5356 -- subprogram nested in an inlined subprogram. The inlining machinery
5357 -- totally disregards nested subprograms since it considers that they
5358 -- will always be compiled if the parent is (see Inline.Is_Nested).
5360 ------------------------------------
5361 -- Is_Inlined_Or_Child_Of_Inlined --
5362 ------------------------------------
5364 function Is_Inlined_Or_Child_Of_Inlined (E : Entity_Id) return Boolean is
5365 Scop : Entity_Id;
5367 begin
5368 if Is_Inlined (E) or else Is_Inlined (Alias (E)) then
5369 return True;
5370 end if;
5372 Scop := Scope (E);
5373 while Scop /= Standard_Standard loop
5374 if Is_Subprogram (Scop) and then Is_Inlined (Scop) then
5375 return True;
5376 end if;
5378 Scop := Scope (Scop);
5379 end loop;
5381 return False;
5382 end Is_Inlined_Or_Child_Of_Inlined;
5384 begin
5385 -- Must be in the main unit or inlined (or child of inlined)
5387 if (Is_In_Main_Unit (N) or else Is_Inlined_Or_Child_Of_Inlined (Subp))
5389 -- Must be generating code or analyzing code in GNATprove mode
5391 and then (Operating_Mode = Generate_Code
5392 or else (Operating_Mode = Check_Semantics
5393 and then GNATprove_Mode))
5395 -- The body is needed when generating code (full expansion) and in
5396 -- in GNATprove mode (special expansion) for formal verification of
5397 -- the body itself.
5399 and then (Expander_Active or GNATprove_Mode)
5401 -- No point in inlining if ABE is inevitable
5403 and then not Is_Known_Guaranteed_ABE (N)
5405 -- Or if subprogram is eliminated
5407 and then not Is_Eliminated (Subp)
5408 then
5409 Add_Pending_Instantiation (N, Unit_Declaration_Node (Subp));
5410 return True;
5412 -- Here if not inlined, or we ignore the inlining
5414 else
5415 return False;
5416 end if;
5417 end Need_Subprogram_Instance_Body;
5419 --------------------------------------
5420 -- Analyze_Subprogram_Instantiation --
5421 --------------------------------------
5423 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
5424 -- must be replaced by gotos which jump to the end of the routine in order
5425 -- to restore the Ghost and SPARK modes.
5427 procedure Analyze_Subprogram_Instantiation
5428 (N : Node_Id;
5429 K : Entity_Kind)
5431 Errs : constant Nat := Serious_Errors_Detected;
5432 Gen_Id : constant Node_Id := Name (N);
5433 Inst_Id : constant Entity_Id := Defining_Entity (N);
5434 Anon_Id : constant Entity_Id :=
5435 Make_Defining_Identifier (Sloc (Inst_Id),
5436 Chars => New_External_Name (Chars (Inst_Id), 'R'));
5437 Loc : constant Source_Ptr := Sloc (N);
5439 Act_Decl_Id : Entity_Id := Empty; -- init to avoid warning
5440 Act_Decl : Node_Id;
5441 Act_Spec : Node_Id;
5442 Act_Tree : Node_Id;
5444 Env_Installed : Boolean := False;
5445 Gen_Unit : Entity_Id;
5446 Gen_Decl : Node_Id;
5447 Pack_Id : Entity_Id;
5448 Parent_Installed : Boolean := False;
5450 Renaming_List : List_Id;
5451 -- The list of declarations that link formals and actuals of the
5452 -- instance. These are subtype declarations for formal types, and
5453 -- renaming declarations for other formals. The subprogram declaration
5454 -- for the instance is then appended to the list, and the last item on
5455 -- the list is the renaming declaration for the instance.
5457 procedure Analyze_Instance_And_Renamings;
5458 -- The instance must be analyzed in a context that includes the mappings
5459 -- of generic parameters into actuals. We create a package declaration
5460 -- for this purpose, and a subprogram with an internal name within the
5461 -- package. The subprogram instance is simply an alias for the internal
5462 -- subprogram, declared in the current scope.
5464 procedure Build_Subprogram_Renaming;
5465 -- If the subprogram is recursive, there are occurrences of the name of
5466 -- the generic within the body, which must resolve to the current
5467 -- instance. We add a renaming declaration after the declaration, which
5468 -- is available in the instance body, as well as in the analysis of
5469 -- aspects that appear in the generic. This renaming declaration is
5470 -- inserted after the instance declaration which it renames.
5472 ------------------------------------
5473 -- Analyze_Instance_And_Renamings --
5474 ------------------------------------
5476 procedure Analyze_Instance_And_Renamings is
5477 Def_Ent : constant Entity_Id := Defining_Entity (N);
5478 Pack_Decl : Node_Id;
5480 begin
5481 if Nkind (Parent (N)) = N_Compilation_Unit then
5483 -- For the case of a compilation unit, the container package has
5484 -- the same name as the instantiation, to insure that the binder
5485 -- calls the elaboration procedure with the right name. Copy the
5486 -- entity of the instance, which may have compilation level flags
5487 -- (e.g. Is_Child_Unit) set.
5489 Pack_Id := New_Copy (Def_Ent);
5491 else
5492 -- Otherwise we use the name of the instantiation concatenated
5493 -- with its source position to ensure uniqueness if there are
5494 -- several instantiations with the same name.
5496 Pack_Id :=
5497 Make_Defining_Identifier (Loc,
5498 Chars => New_External_Name
5499 (Related_Id => Chars (Def_Ent),
5500 Suffix => "GP",
5501 Suffix_Index => Source_Offset (Sloc (Def_Ent))));
5502 end if;
5504 Pack_Decl :=
5505 Make_Package_Declaration (Loc,
5506 Specification => Make_Package_Specification (Loc,
5507 Defining_Unit_Name => Pack_Id,
5508 Visible_Declarations => Renaming_List,
5509 End_Label => Empty));
5511 Set_Instance_Spec (N, Pack_Decl);
5512 Set_Is_Generic_Instance (Pack_Id);
5513 Set_Debug_Info_Needed (Pack_Id);
5515 -- Case of not a compilation unit
5517 if Nkind (Parent (N)) /= N_Compilation_Unit then
5518 Mark_Rewrite_Insertion (Pack_Decl);
5519 Insert_Before (N, Pack_Decl);
5520 Set_Has_Completion (Pack_Id);
5522 -- Case of an instantiation that is a compilation unit
5524 -- Place declaration on current node so context is complete for
5525 -- analysis (including nested instantiations), and for use in a
5526 -- context_clause (see Analyze_With_Clause).
5528 else
5529 Set_Unit (Parent (N), Pack_Decl);
5530 Set_Parent_Spec (Pack_Decl, Parent_Spec (N));
5531 end if;
5533 Analyze (Pack_Decl);
5534 Check_Formal_Packages (Pack_Id);
5536 -- Body of the enclosing package is supplied when instantiating the
5537 -- subprogram body, after semantic analysis is completed.
5539 if Nkind (Parent (N)) = N_Compilation_Unit then
5541 -- Remove package itself from visibility, so it does not
5542 -- conflict with subprogram.
5544 Set_Name_Entity_Id (Chars (Pack_Id), Homonym (Pack_Id));
5546 -- Set name and scope of internal subprogram so that the proper
5547 -- external name will be generated. The proper scope is the scope
5548 -- of the wrapper package. We need to generate debugging info for
5549 -- the internal subprogram, so set flag accordingly.
5551 Set_Chars (Anon_Id, Chars (Defining_Entity (N)));
5552 Set_Scope (Anon_Id, Scope (Pack_Id));
5554 -- Mark wrapper package as referenced, to avoid spurious warnings
5555 -- if the instantiation appears in various with_ clauses of
5556 -- subunits of the main unit.
5558 Set_Referenced (Pack_Id);
5559 end if;
5561 Set_Is_Generic_Instance (Anon_Id);
5562 Set_Debug_Info_Needed (Anon_Id);
5563 Act_Decl_Id := New_Copy (Anon_Id);
5565 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
5566 Set_Chars (Act_Decl_Id, Chars (Defining_Entity (N)));
5567 Set_Sloc (Act_Decl_Id, Sloc (Defining_Entity (N)));
5569 -- Subprogram instance comes from source only if generic does
5571 Preserve_Comes_From_Source (Act_Decl_Id, Gen_Unit);
5573 -- If the instance is a child unit, mark the Id accordingly. Mark
5574 -- the anonymous entity as well, which is the real subprogram and
5575 -- which is used when the instance appears in a context clause.
5576 -- Similarly, propagate the Is_Eliminated flag to handle properly
5577 -- nested eliminated subprograms.
5579 Set_Is_Child_Unit (Act_Decl_Id, Is_Child_Unit (Defining_Entity (N)));
5580 Set_Is_Child_Unit (Anon_Id, Is_Child_Unit (Defining_Entity (N)));
5581 New_Overloaded_Entity (Act_Decl_Id);
5582 Check_Eliminated (Act_Decl_Id);
5583 Set_Is_Eliminated (Anon_Id, Is_Eliminated (Act_Decl_Id));
5585 if Nkind (Parent (N)) = N_Compilation_Unit then
5587 -- In compilation unit case, kill elaboration checks on the
5588 -- instantiation, since they are never needed - the body is
5589 -- instantiated at the same point as the spec.
5591 if Legacy_Elaboration_Checks then
5592 Set_Kill_Elaboration_Checks (Act_Decl_Id);
5593 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
5594 end if;
5596 Set_Is_Compilation_Unit (Anon_Id);
5597 Set_Cunit_Entity (Current_Sem_Unit, Pack_Id);
5598 end if;
5600 -- The instance is not a freezing point for the new subprogram.
5601 -- The anonymous subprogram may have a freeze node, created for
5602 -- some delayed aspects. This freeze node must not be inherited
5603 -- by the visible subprogram entity.
5605 Set_Is_Frozen (Act_Decl_Id, False);
5606 Set_Freeze_Node (Act_Decl_Id, Empty);
5608 if Nkind (Defining_Entity (N)) = N_Defining_Operator_Symbol then
5609 Valid_Operator_Definition (Act_Decl_Id);
5610 end if;
5612 Set_Alias (Act_Decl_Id, Anon_Id);
5613 Set_Has_Completion (Act_Decl_Id);
5614 Set_Related_Instance (Pack_Id, Act_Decl_Id);
5616 if Nkind (Parent (N)) = N_Compilation_Unit then
5617 Set_Body_Required (Parent (N), False);
5618 end if;
5619 end Analyze_Instance_And_Renamings;
5621 -------------------------------
5622 -- Build_Subprogram_Renaming --
5623 -------------------------------
5625 procedure Build_Subprogram_Renaming is
5626 Renaming_Decl : Node_Id;
5627 Unit_Renaming : Node_Id;
5629 begin
5630 Unit_Renaming :=
5631 Make_Subprogram_Renaming_Declaration (Loc,
5632 Specification =>
5633 Copy_Generic_Node
5634 (Specification (Original_Node (Gen_Decl)),
5635 Empty,
5636 Instantiating => True),
5637 Name => New_Occurrence_Of (Anon_Id, Loc));
5639 -- The generic may be a child unit. The renaming needs an identifier
5640 -- with the proper name.
5642 Set_Defining_Unit_Name (Specification (Unit_Renaming),
5643 Make_Defining_Identifier (Loc, Chars (Gen_Unit)));
5645 -- If there is a formal subprogram with the same name as the unit
5646 -- itself, do not add this renaming declaration, to prevent
5647 -- ambiguities when there is a call with that name in the body.
5649 Renaming_Decl := First (Renaming_List);
5650 while Present (Renaming_Decl) loop
5651 if Nkind (Renaming_Decl) = N_Subprogram_Renaming_Declaration
5652 and then
5653 Chars (Defining_Entity (Renaming_Decl)) = Chars (Gen_Unit)
5654 then
5655 exit;
5656 end if;
5658 Next (Renaming_Decl);
5659 end loop;
5661 if No (Renaming_Decl) then
5662 Append (Unit_Renaming, Renaming_List);
5663 end if;
5664 end Build_Subprogram_Renaming;
5666 -- Local variables
5668 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
5669 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
5670 Saved_ISMP : constant Boolean :=
5671 Ignore_SPARK_Mode_Pragmas_In_Instance;
5672 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
5673 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
5674 -- Save the Ghost and SPARK mode-related data to restore on exit
5676 Vis_Prims_List : Elist_Id := No_Elist;
5677 -- List of primitives made temporarily visible in the instantiation
5678 -- to match the visibility of the formal type
5680 -- Start of processing for Analyze_Subprogram_Instantiation
5682 begin
5683 -- Preserve relevant elaboration-related attributes of the context which
5684 -- are no longer available or very expensive to recompute once analysis,
5685 -- resolution, and expansion are over.
5687 Mark_Elaboration_Attributes
5688 (N_Id => N,
5689 Checks => True,
5690 Level => True,
5691 Modes => True,
5692 Warnings => True);
5694 -- Very first thing: check for special Text_IO unit in case we are
5695 -- instantiating one of the children of [[Wide_]Wide_]Text_IO. Of course
5696 -- such an instantiation is bogus (these are packages, not subprograms),
5697 -- but we get a better error message if we do this.
5699 Check_Text_IO_Special_Unit (Gen_Id);
5701 -- Make node global for error reporting
5703 Instantiation_Node := N;
5705 -- For package instantiations we turn off style checks, because they
5706 -- will have been emitted in the generic. For subprogram instantiations
5707 -- we want to apply at least the check on overriding indicators so we
5708 -- do not modify the style check status.
5710 -- The renaming declarations for the actuals do not come from source and
5711 -- will not generate spurious warnings.
5713 Preanalyze_Actuals (N);
5715 Init_Env;
5716 Env_Installed := True;
5717 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
5718 Gen_Unit := Entity (Gen_Id);
5720 -- A subprogram instantiation is Ghost when it is subject to pragma
5721 -- Ghost or the generic template is Ghost. Set the mode now to ensure
5722 -- that any nodes generated during analysis and expansion are marked as
5723 -- Ghost.
5725 Mark_And_Set_Ghost_Instantiation (N, Gen_Unit);
5727 Generate_Reference (Gen_Unit, Gen_Id);
5729 if Nkind (Gen_Id) = N_Identifier
5730 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
5731 then
5732 Error_Msg_NE
5733 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
5734 end if;
5736 if Etype (Gen_Unit) = Any_Type then
5737 Restore_Env;
5738 goto Leave;
5739 end if;
5741 -- Verify that it is a generic subprogram of the right kind, and that
5742 -- it does not lead to a circular instantiation.
5744 if K = E_Procedure and then Ekind (Gen_Unit) /= E_Generic_Procedure then
5745 Error_Msg_NE
5746 ("& is not the name of a generic procedure", Gen_Id, Gen_Unit);
5748 elsif K = E_Function and then Ekind (Gen_Unit) /= E_Generic_Function then
5749 Error_Msg_NE
5750 ("& is not the name of a generic function", Gen_Id, Gen_Unit);
5752 elsif In_Open_Scopes (Gen_Unit) then
5753 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
5755 else
5756 Mutate_Ekind (Inst_Id, K);
5757 Set_Scope (Inst_Id, Current_Scope);
5759 Set_Entity (Gen_Id, Gen_Unit);
5761 if In_Extended_Main_Source_Unit (N) then
5762 Set_Is_Instantiated (Gen_Unit);
5763 Generate_Reference (Gen_Unit, N);
5764 end if;
5766 -- If renaming, get original unit
5768 if Present (Renamed_Entity (Gen_Unit))
5769 and then Is_Generic_Subprogram (Renamed_Entity (Gen_Unit))
5770 then
5771 Gen_Unit := Renamed_Entity (Gen_Unit);
5772 Set_Is_Instantiated (Gen_Unit);
5773 Generate_Reference (Gen_Unit, N);
5774 end if;
5776 if Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
5777 Error_Msg_Node_2 := Current_Scope;
5778 Error_Msg_NE
5779 ("circular instantiation: & instantiated in &!", N, Gen_Unit);
5780 Circularity_Detected := True;
5781 Restore_Hidden_Primitives (Vis_Prims_List);
5782 goto Leave;
5783 end if;
5785 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
5787 -- Initialize renamings map, for error checking
5789 Generic_Renamings.Set_Last (0);
5790 Generic_Renamings_HTable.Reset;
5792 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
5794 -- Copy original generic tree, to produce text for instantiation
5796 Act_Tree :=
5797 Copy_Generic_Node
5798 (Original_Node (Gen_Decl), Empty, Instantiating => True);
5800 -- Inherit overriding indicator from instance node
5802 Act_Spec := Specification (Act_Tree);
5803 Set_Must_Override (Act_Spec, Must_Override (N));
5804 Set_Must_Not_Override (Act_Spec, Must_Not_Override (N));
5806 Renaming_List :=
5807 Analyze_Associations
5808 (I_Node => N,
5809 Formals => Generic_Formal_Declarations (Act_Tree),
5810 F_Copy => Generic_Formal_Declarations (Gen_Decl));
5812 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
5814 -- The subprogram itself cannot contain a nested instance, so the
5815 -- current parent is left empty.
5817 Set_Instance_Env (Gen_Unit, Empty);
5819 -- Build the subprogram declaration, which does not appear in the
5820 -- generic template, and give it a sloc consistent with that of the
5821 -- template.
5823 Set_Defining_Unit_Name (Act_Spec, Anon_Id);
5824 Set_Generic_Parent (Act_Spec, Gen_Unit);
5825 Act_Decl :=
5826 Make_Subprogram_Declaration (Sloc (Act_Spec),
5827 Specification => Act_Spec);
5829 -- The aspects have been copied previously, but they have to be
5830 -- linked explicitly to the new subprogram declaration. Explicit
5831 -- pre/postconditions on the instance are analyzed below, in a
5832 -- separate step.
5834 Move_Aspects (Act_Tree, To => Act_Decl);
5835 Set_Categorization_From_Pragmas (Act_Decl);
5837 if Parent_Installed then
5838 Hide_Current_Scope;
5839 end if;
5841 Append (Act_Decl, Renaming_List);
5843 -- Contract-related source pragmas that follow a generic subprogram
5844 -- must be instantiated explicitly because they are not part of the
5845 -- subprogram template.
5847 Instantiate_Subprogram_Contract
5848 (Original_Node (Gen_Decl), Renaming_List);
5850 Build_Subprogram_Renaming;
5852 -- If the context of the instance is subject to SPARK_Mode "off" or
5853 -- the annotation is altogether missing, set the global flag which
5854 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
5855 -- the instance. This should be done prior to analyzing the instance.
5857 if SPARK_Mode /= On then
5858 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
5859 end if;
5861 -- If the context of an instance is not subject to SPARK_Mode "off",
5862 -- and the generic spec is subject to an explicit SPARK_Mode pragma,
5863 -- the latter should be the one applicable to the instance.
5865 if not Ignore_SPARK_Mode_Pragmas_In_Instance
5866 and then Saved_SM /= Off
5867 and then Present (SPARK_Pragma (Gen_Unit))
5868 then
5869 Set_SPARK_Mode (Gen_Unit);
5870 end if;
5872 -- Need to mark Anon_Id intrinsic before calling
5873 -- Analyze_Instance_And_Renamings because this flag may be propagated
5874 -- to other nodes.
5876 if Is_Intrinsic_Subprogram (Gen_Unit) then
5877 Set_Is_Intrinsic_Subprogram (Anon_Id);
5878 Set_Interface_Name (Anon_Id, Interface_Name (Gen_Unit));
5879 end if;
5881 Analyze_Instance_And_Renamings;
5883 -- Restore SPARK_Mode from the context after analysis of the package
5884 -- declaration, so that the SPARK_Mode on the generic spec does not
5885 -- apply to the pending instance for the instance body.
5887 if not Ignore_SPARK_Mode_Pragmas_In_Instance
5888 and then Saved_SM /= Off
5889 and then Present (SPARK_Pragma (Gen_Unit))
5890 then
5891 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
5892 end if;
5894 -- If the generic is marked Import (Intrinsic), then so is the
5895 -- instance; this indicates that there is no body to instantiate.
5896 -- We also copy the interface name in case this is handled by the
5897 -- back-end and deal with an instance of unchecked conversion.
5899 if Is_Intrinsic_Subprogram (Gen_Unit) then
5900 Set_Is_Intrinsic_Subprogram (Act_Decl_Id);
5901 Set_Interface_Name (Act_Decl_Id, Interface_Name (Gen_Unit));
5903 if Chars (Gen_Unit) = Name_Unchecked_Conversion then
5904 Validate_Unchecked_Conversion (N, Act_Decl_Id);
5905 end if;
5906 end if;
5908 -- Inherit convention from generic unit. Intrinsic convention, as for
5909 -- an instance of unchecked conversion, is not inherited because an
5910 -- explicit Ada instance has been created.
5912 if Has_Convention_Pragma (Gen_Unit)
5913 and then Convention (Gen_Unit) /= Convention_Intrinsic
5914 then
5915 Set_Convention (Act_Decl_Id, Convention (Gen_Unit));
5916 Set_Is_Exported (Act_Decl_Id, Is_Exported (Gen_Unit));
5917 end if;
5919 Generate_Definition (Act_Decl_Id);
5921 -- Inherit all inlining-related flags which apply to the generic in
5922 -- the subprogram and its declaration.
5924 Set_Is_Inlined (Act_Decl_Id, Is_Inlined (Gen_Unit));
5925 Set_Is_Inlined (Anon_Id, Is_Inlined (Gen_Unit));
5927 Set_Has_Pragma_Inline (Act_Decl_Id, Has_Pragma_Inline (Gen_Unit));
5928 Set_Has_Pragma_Inline (Anon_Id, Has_Pragma_Inline (Gen_Unit));
5930 Set_Has_Pragma_Inline_Always
5931 (Act_Decl_Id, Has_Pragma_Inline_Always (Gen_Unit));
5932 Set_Has_Pragma_Inline_Always
5933 (Anon_Id, Has_Pragma_Inline_Always (Gen_Unit));
5935 Set_Has_Pragma_No_Inline
5936 (Act_Decl_Id, Has_Pragma_No_Inline (Gen_Unit));
5937 Set_Has_Pragma_No_Inline
5938 (Anon_Id, Has_Pragma_No_Inline (Gen_Unit));
5940 -- Propagate No_Return if pragma applied to generic unit. This must
5941 -- be done explicitly because pragma does not appear in generic
5942 -- declaration (unlike the aspect case).
5944 if No_Return (Gen_Unit) then
5945 Set_No_Return (Act_Decl_Id);
5946 Set_No_Return (Anon_Id);
5947 end if;
5949 -- Mark both the instance spec and the anonymous package in case the
5950 -- body is instantiated at a later pass. This preserves the original
5951 -- context in effect for the body.
5953 if SPARK_Mode /= On then
5954 Set_Ignore_SPARK_Mode_Pragmas (Act_Decl_Id);
5955 Set_Ignore_SPARK_Mode_Pragmas (Anon_Id);
5956 end if;
5958 if Legacy_Elaboration_Checks
5959 and then not Is_Intrinsic_Subprogram (Gen_Unit)
5960 then
5961 Check_Elab_Instantiation (N);
5962 end if;
5964 -- Save the scenario for later examination by the ABE Processing
5965 -- phase.
5967 Record_Elaboration_Scenario (N);
5969 -- The instantiation results in a guaranteed ABE. Create a completing
5970 -- body for the subprogram declaration because the real body will not
5971 -- be instantiated.
5973 if Is_Known_Guaranteed_ABE (N) then
5974 Provide_Completing_Bodies (Instance_Spec (N));
5975 end if;
5977 if Is_Dispatching_Operation (Act_Decl_Id)
5978 and then Ada_Version >= Ada_2005
5979 then
5980 declare
5981 Formal : Entity_Id;
5983 begin
5984 Formal := First_Formal (Act_Decl_Id);
5985 while Present (Formal) loop
5986 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type
5987 and then Is_Controlling_Formal (Formal)
5988 and then not Can_Never_Be_Null (Formal)
5989 then
5990 Error_Msg_NE
5991 ("access parameter& is controlling,", N, Formal);
5992 Error_Msg_NE
5993 ("\corresponding parameter of & must be explicitly "
5994 & "null-excluding", N, Gen_Id);
5995 end if;
5997 Next_Formal (Formal);
5998 end loop;
5999 end;
6000 end if;
6002 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
6004 Validate_Categorization_Dependency (N, Act_Decl_Id);
6006 if not Is_Intrinsic_Subprogram (Act_Decl_Id) then
6007 Inherit_Context (Gen_Decl, N);
6009 Restore_Private_Views (Pack_Id, False);
6011 -- If the context requires a full instantiation, mark node for
6012 -- subsequent construction of the body.
6014 if Need_Subprogram_Instance_Body (N, Act_Decl_Id) then
6015 Check_Forward_Instantiation (Gen_Decl);
6017 -- The wrapper package is always delayed, because it does not
6018 -- constitute a freeze point, but to insure that the freeze node
6019 -- is placed properly, it is created directly when instantiating
6020 -- the body (otherwise the freeze node might appear to early for
6021 -- nested instantiations).
6023 elsif Nkind (Parent (N)) = N_Compilation_Unit then
6024 Rewrite (N, Unit (Parent (N)));
6025 Set_Unit (Parent (N), N);
6026 end if;
6028 -- Replace instance node for library-level instantiations of
6029 -- intrinsic subprograms.
6031 elsif Nkind (Parent (N)) = N_Compilation_Unit then
6032 Rewrite (N, Unit (Parent (N)));
6033 Set_Unit (Parent (N), N);
6034 end if;
6036 if Parent_Installed then
6037 Remove_Parent;
6038 end if;
6040 Restore_Hidden_Primitives (Vis_Prims_List);
6041 Restore_Env;
6042 Env_Installed := False;
6043 Generic_Renamings.Set_Last (0);
6044 Generic_Renamings_HTable.Reset;
6045 end if;
6047 <<Leave>>
6048 -- Analyze aspects in declaration if no errors appear in the instance.
6050 if Has_Aspects (N) and then Serious_Errors_Detected = Errs then
6051 Analyze_Aspect_Specifications (N, Act_Decl_Id);
6052 end if;
6054 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
6055 Restore_Ghost_Region (Saved_GM, Saved_IGR);
6056 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
6058 exception
6059 when Instantiation_Error =>
6060 if Parent_Installed then
6061 Remove_Parent;
6062 end if;
6064 if Env_Installed then
6065 Restore_Env;
6066 end if;
6068 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
6069 Restore_Ghost_Region (Saved_GM, Saved_IGR);
6070 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
6071 end Analyze_Subprogram_Instantiation;
6073 -------------------------
6074 -- Get_Associated_Node --
6075 -------------------------
6077 function Get_Associated_Node (N : Node_Id) return Node_Id is
6078 Assoc : Node_Id;
6080 begin
6081 Assoc := Associated_Node (N);
6083 if Nkind (Assoc) /= Nkind (N) then
6084 return Assoc;
6086 elsif Nkind (Assoc) in N_Aggregate | N_Extension_Aggregate then
6087 return Assoc;
6089 else
6090 -- If the node is part of an inner generic, it may itself have been
6091 -- remapped into a further generic copy. Associated_Node is otherwise
6092 -- used for the entity of the node, and will be of a different node
6093 -- kind, or else N has been rewritten as a literal or function call.
6095 while Present (Associated_Node (Assoc))
6096 and then Nkind (Associated_Node (Assoc)) = Nkind (Assoc)
6097 loop
6098 Assoc := Associated_Node (Assoc);
6099 end loop;
6101 -- Follow an additional link in case the final node was rewritten.
6102 -- This can only happen with nested generic units.
6104 if (Nkind (Assoc) = N_Identifier or else Nkind (Assoc) in N_Op)
6105 and then Present (Associated_Node (Assoc))
6106 and then Nkind (Associated_Node (Assoc)) in N_Function_Call
6107 | N_Explicit_Dereference
6108 | N_Integer_Literal
6109 | N_Real_Literal
6110 | N_String_Literal
6111 then
6112 Assoc := Associated_Node (Assoc);
6113 end if;
6115 -- An additional special case: an unconstrained type in an object
6116 -- declaration may have been rewritten as a local subtype constrained
6117 -- by the expression in the declaration. We need to recover the
6118 -- original entity, which may be global.
6120 if Present (Original_Node (Assoc))
6121 and then Nkind (Parent (N)) = N_Object_Declaration
6122 then
6123 Assoc := Original_Node (Assoc);
6124 end if;
6126 return Assoc;
6127 end if;
6128 end Get_Associated_Node;
6130 -----------------------------------
6131 -- Build_Subprogram_Decl_Wrapper --
6132 -----------------------------------
6134 function Build_Subprogram_Decl_Wrapper
6135 (Formal_Subp : Entity_Id) return Node_Id
6137 Loc : constant Source_Ptr := Sloc (Current_Scope);
6138 Ret_Type : constant Entity_Id := Get_Instance_Of (Etype (Formal_Subp));
6139 Decl : Node_Id;
6140 Subp : Entity_Id;
6141 Parm_Spec : Node_Id;
6142 Profile : List_Id := New_List;
6143 Spec : Node_Id;
6144 Form_F : Entity_Id;
6145 New_F : Entity_Id;
6147 begin
6149 Subp := Make_Defining_Identifier (Loc, Chars (Formal_Subp));
6150 Mutate_Ekind (Subp, Ekind (Formal_Subp));
6151 Set_Is_Generic_Actual_Subprogram (Subp);
6153 Profile := Parameter_Specifications (
6154 New_Copy_Tree
6155 (Specification (Unit_Declaration_Node (Formal_Subp))));
6157 Form_F := First_Formal (Formal_Subp);
6158 Parm_Spec := First (Profile);
6160 -- Create new entities for the formals. Reset entities so that
6161 -- parameter types are properly resolved when wrapper declaration
6162 -- is analyzed.
6164 while Present (Parm_Spec) loop
6165 New_F := Make_Defining_Identifier (Loc, Chars (Form_F));
6166 Set_Defining_Identifier (Parm_Spec, New_F);
6167 Set_Entity (Parameter_Type (Parm_Spec), Empty);
6168 Next (Parm_Spec);
6169 Next_Formal (Form_F);
6170 end loop;
6172 if Ret_Type = Standard_Void_Type then
6173 Spec :=
6174 Make_Procedure_Specification (Loc,
6175 Defining_Unit_Name => Subp,
6176 Parameter_Specifications => Profile);
6177 else
6178 Spec :=
6179 Make_Function_Specification (Loc,
6180 Defining_Unit_Name => Subp,
6181 Parameter_Specifications => Profile,
6182 Result_Definition => New_Occurrence_Of (Ret_Type, Loc));
6183 end if;
6185 Decl :=
6186 Make_Subprogram_Declaration (Loc, Specification => Spec);
6188 return Decl;
6189 end Build_Subprogram_Decl_Wrapper;
6191 -----------------------------------
6192 -- Build_Subprogram_Body_Wrapper --
6193 -----------------------------------
6195 function Build_Subprogram_Body_Wrapper
6196 (Formal_Subp : Entity_Id;
6197 Actual_Name : Node_Id) return Node_Id
6199 Loc : constant Source_Ptr := Sloc (Current_Scope);
6200 Ret_Type : constant Entity_Id := Get_Instance_Of (Etype (Formal_Subp));
6201 Spec_Node : constant Node_Id :=
6202 Specification
6203 (Build_Subprogram_Decl_Wrapper (Formal_Subp));
6204 Act : Node_Id;
6205 Actuals : List_Id;
6206 Body_Node : Node_Id;
6207 Stmt : Node_Id;
6208 begin
6209 Actuals := New_List;
6210 Act := First (Parameter_Specifications (Spec_Node));
6212 while Present (Act) loop
6213 Append_To (Actuals,
6214 Make_Identifier (Loc, Chars (Defining_Identifier (Act))));
6215 Next (Act);
6216 end loop;
6218 if Ret_Type = Standard_Void_Type then
6219 Stmt := Make_Procedure_Call_Statement (Loc,
6220 Name => Actual_Name,
6221 Parameter_Associations => Actuals);
6223 else
6224 Stmt := Make_Simple_Return_Statement (Loc,
6225 Expression =>
6226 Make_Function_Call (Loc,
6227 Name => Actual_Name,
6228 Parameter_Associations => Actuals));
6229 end if;
6231 Body_Node := Make_Subprogram_Body (Loc,
6232 Specification => Spec_Node,
6233 Declarations => New_List,
6234 Handled_Statement_Sequence =>
6235 Make_Handled_Sequence_Of_Statements (Loc,
6236 Statements => New_List (Stmt)));
6238 return Body_Node;
6239 end Build_Subprogram_Body_Wrapper;
6241 -------------------------------------------
6242 -- Build_Instance_Compilation_Unit_Nodes --
6243 -------------------------------------------
6245 procedure Build_Instance_Compilation_Unit_Nodes
6246 (N : Node_Id;
6247 Act_Body : Node_Id;
6248 Act_Decl : Node_Id)
6250 Decl_Cunit : Node_Id;
6251 Body_Cunit : Node_Id;
6252 Citem : Node_Id;
6253 New_Main : constant Entity_Id := Defining_Entity (Act_Decl);
6254 Old_Main : constant Entity_Id := Cunit_Entity (Main_Unit);
6256 begin
6257 -- A new compilation unit node is built for the instance declaration
6259 Decl_Cunit :=
6260 Make_Compilation_Unit (Sloc (N),
6261 Context_Items => Empty_List,
6262 Unit => Act_Decl,
6263 Aux_Decls_Node => Make_Compilation_Unit_Aux (Sloc (N)));
6265 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
6267 -- The new compilation unit is linked to its body, but both share the
6268 -- same file, so we do not set Body_Required on the new unit so as not
6269 -- to create a spurious dependency on a non-existent body in the ali.
6270 -- This simplifies CodePeer unit traversal.
6272 -- We use the original instantiation compilation unit as the resulting
6273 -- compilation unit of the instance, since this is the main unit.
6275 Rewrite (N, Act_Body);
6277 -- Propagate the aspect specifications from the package body template to
6278 -- the instantiated version of the package body.
6280 if Has_Aspects (Act_Body) then
6281 Set_Aspect_Specifications
6282 (N, New_Copy_List_Tree (Aspect_Specifications (Act_Body)));
6283 end if;
6285 Body_Cunit := Parent (N);
6287 -- The two compilation unit nodes are linked by the Library_Unit field
6289 Set_Library_Unit (Decl_Cunit, Body_Cunit);
6290 Set_Library_Unit (Body_Cunit, Decl_Cunit);
6292 -- Preserve the private nature of the package if needed
6294 Set_Private_Present (Decl_Cunit, Private_Present (Body_Cunit));
6296 -- If the instance is not the main unit, its context, categorization
6297 -- and elaboration entity are not relevant to the compilation.
6299 if Body_Cunit /= Cunit (Main_Unit) then
6300 Make_Instance_Unit (Body_Cunit, In_Main => False);
6301 return;
6302 end if;
6304 -- The context clause items on the instantiation, which are now attached
6305 -- to the body compilation unit (since the body overwrote the original
6306 -- instantiation node), semantically belong on the spec, so copy them
6307 -- there. It's harmless to leave them on the body as well. In fact one
6308 -- could argue that they belong in both places.
6310 Citem := First (Context_Items (Body_Cunit));
6311 while Present (Citem) loop
6312 Append (New_Copy (Citem), Context_Items (Decl_Cunit));
6313 Next (Citem);
6314 end loop;
6316 -- Propagate categorization flags on packages, so that they appear in
6317 -- the ali file for the spec of the unit.
6319 if Ekind (New_Main) = E_Package then
6320 Set_Is_Pure (Old_Main, Is_Pure (New_Main));
6321 Set_Is_Preelaborated (Old_Main, Is_Preelaborated (New_Main));
6322 Set_Is_Remote_Types (Old_Main, Is_Remote_Types (New_Main));
6323 Set_Is_Shared_Passive (Old_Main, Is_Shared_Passive (New_Main));
6324 Set_Is_Remote_Call_Interface
6325 (Old_Main, Is_Remote_Call_Interface (New_Main));
6326 end if;
6328 -- Make entry in Units table, so that binder can generate call to
6329 -- elaboration procedure for body, if any.
6331 Make_Instance_Unit (Body_Cunit, In_Main => True);
6332 Main_Unit_Entity := New_Main;
6333 Set_Cunit_Entity (Main_Unit, Main_Unit_Entity);
6335 -- Build elaboration entity, since the instance may certainly generate
6336 -- elaboration code requiring a flag for protection.
6338 Build_Elaboration_Entity (Decl_Cunit, New_Main);
6339 end Build_Instance_Compilation_Unit_Nodes;
6341 --------------------------------
6342 -- Check_Abbreviated_Instance --
6343 --------------------------------
6345 procedure Check_Abbreviated_Instance
6346 (N : Node_Id;
6347 Parent_Installed : in out Boolean)
6349 Inst_Node : Node_Id;
6351 begin
6352 if Nkind (N) = N_Package_Specification
6353 and then Is_Abbreviated_Instance (Defining_Entity (N))
6354 then
6355 Inst_Node := Get_Unit_Instantiation_Node (Defining_Entity (N));
6356 Check_Generic_Child_Unit (Name (Inst_Node), Parent_Installed);
6357 end if;
6358 end Check_Abbreviated_Instance;
6360 -----------------------------
6361 -- Check_Access_Definition --
6362 -----------------------------
6364 procedure Check_Access_Definition (N : Node_Id) is
6365 begin
6366 pragma Assert
6367 (Ada_Version >= Ada_2005 and then Present (Access_Definition (N)));
6368 null;
6369 end Check_Access_Definition;
6371 -----------------------------------
6372 -- Check_Formal_Package_Instance --
6373 -----------------------------------
6375 -- If the formal has specific parameters, they must match those of the
6376 -- actual. Both of them are instances, and the renaming declarations for
6377 -- their formal parameters appear in the same order in both. The analyzed
6378 -- formal has been analyzed in the context of the current instance.
6380 procedure Check_Formal_Package_Instance
6381 (Formal_Pack : Entity_Id;
6382 Actual_Pack : Entity_Id)
6384 E1 : Entity_Id := First_Entity (Actual_Pack);
6385 E2 : Entity_Id := First_Entity (Formal_Pack);
6386 Prev_E1 : Entity_Id;
6388 Expr1 : Node_Id;
6389 Expr2 : Node_Id;
6391 procedure Check_Mismatch (B : Boolean);
6392 -- Common error routine for mismatch between the parameters of the
6393 -- actual instance and those of the formal package.
6395 function Is_Defaulted (Param : Entity_Id) return Boolean;
6396 -- If the formal package has partly box-initialized formals, skip
6397 -- conformance check for these formals. Previously the code assumed
6398 -- that box initialization for a formal package applied to all its
6399 -- formal parameters.
6401 function Same_Instantiated_Constant (E1, E2 : Entity_Id) return Boolean;
6402 -- The formal may come from a nested formal package, and the actual may
6403 -- have been constant-folded. To determine whether the two denote the
6404 -- same entity we may have to traverse several definitions to recover
6405 -- the ultimate entity that they refer to.
6407 function Same_Instantiated_Function (E1, E2 : Entity_Id) return Boolean;
6408 -- The formal and the actual must be identical, but if both are
6409 -- given by attributes they end up renaming different generated bodies,
6410 -- and we must verify that the attributes themselves match.
6412 function Same_Instantiated_Variable (E1, E2 : Entity_Id) return Boolean;
6413 -- Similarly, if the formal comes from a nested formal package, the
6414 -- actual may designate the formal through multiple renamings, which
6415 -- have to be followed to determine the original variable in question.
6417 --------------------
6418 -- Check_Mismatch --
6419 --------------------
6421 procedure Check_Mismatch (B : Boolean) is
6422 -- A Formal_Type_Declaration for a derived private type is rewritten
6423 -- as a private extension decl. (see Analyze_Formal_Derived_Type),
6424 -- which is why we examine the original node.
6426 Kind : constant Node_Kind := Nkind (Original_Node (Parent (E2)));
6428 begin
6429 if Kind = N_Formal_Type_Declaration then
6430 return;
6432 elsif Kind in N_Formal_Object_Declaration
6433 | N_Formal_Package_Declaration
6434 | N_Formal_Subprogram_Declaration
6435 then
6436 null;
6438 -- Ada 2012: If both formal and actual are incomplete types they
6439 -- are conformant.
6441 elsif Is_Incomplete_Type (E1) and then Is_Incomplete_Type (E2) then
6442 null;
6444 elsif B then
6445 Error_Msg_NE
6446 ("actual for & in actual instance does not match formal",
6447 Parent (Actual_Pack), E1);
6448 end if;
6449 end Check_Mismatch;
6451 ------------------
6452 -- Is_Defaulted --
6453 ------------------
6455 function Is_Defaulted (Param : Entity_Id) return Boolean is
6456 Assoc : Node_Id;
6458 begin
6459 Assoc :=
6460 First (Generic_Associations (Parent
6461 (Associated_Formal_Package (Actual_Pack))));
6463 while Present (Assoc) loop
6464 if Nkind (Assoc) = N_Others_Choice then
6465 return True;
6467 elsif Nkind (Assoc) = N_Generic_Association
6468 and then Chars (Selector_Name (Assoc)) = Chars (Param)
6469 then
6470 return Box_Present (Assoc);
6471 end if;
6473 Next (Assoc);
6474 end loop;
6476 return False;
6477 end Is_Defaulted;
6479 --------------------------------
6480 -- Same_Instantiated_Constant --
6481 --------------------------------
6483 function Same_Instantiated_Constant
6484 (E1, E2 : Entity_Id) return Boolean
6486 Ent : Entity_Id;
6488 begin
6489 Ent := E2;
6490 while Present (Ent) loop
6491 if E1 = Ent then
6492 return True;
6494 elsif Ekind (Ent) /= E_Constant then
6495 return False;
6497 elsif Is_Entity_Name (Constant_Value (Ent)) then
6498 if Entity (Constant_Value (Ent)) = E1 then
6499 return True;
6500 else
6501 Ent := Entity (Constant_Value (Ent));
6502 end if;
6504 -- The actual may be a constant that has been folded. Recover
6505 -- original name.
6507 elsif Is_Entity_Name (Original_Node (Constant_Value (Ent))) then
6508 Ent := Entity (Original_Node (Constant_Value (Ent)));
6510 else
6511 return False;
6512 end if;
6513 end loop;
6515 return False;
6516 end Same_Instantiated_Constant;
6518 --------------------------------
6519 -- Same_Instantiated_Function --
6520 --------------------------------
6522 function Same_Instantiated_Function
6523 (E1, E2 : Entity_Id) return Boolean
6525 U1, U2 : Node_Id;
6526 begin
6527 if Alias (E1) = Alias (E2) then
6528 return True;
6530 elsif Present (Alias (E2)) then
6531 U1 := Original_Node (Unit_Declaration_Node (E1));
6532 U2 := Original_Node (Unit_Declaration_Node (Alias (E2)));
6534 return Nkind (U1) = N_Subprogram_Renaming_Declaration
6535 and then Nkind (Name (U1)) = N_Attribute_Reference
6537 and then Nkind (U2) = N_Subprogram_Renaming_Declaration
6538 and then Nkind (Name (U2)) = N_Attribute_Reference
6540 and then
6541 Attribute_Name (Name (U1)) = Attribute_Name (Name (U2));
6542 else
6543 return False;
6544 end if;
6545 end Same_Instantiated_Function;
6547 --------------------------------
6548 -- Same_Instantiated_Variable --
6549 --------------------------------
6551 function Same_Instantiated_Variable
6552 (E1, E2 : Entity_Id) return Boolean
6554 function Original_Entity (E : Entity_Id) return Entity_Id;
6555 -- Follow chain of renamings to the ultimate ancestor
6557 ---------------------
6558 -- Original_Entity --
6559 ---------------------
6561 function Original_Entity (E : Entity_Id) return Entity_Id is
6562 Orig : Entity_Id;
6564 begin
6565 Orig := E;
6566 while Nkind (Parent (Orig)) = N_Object_Renaming_Declaration
6567 and then Present (Renamed_Object (Orig))
6568 and then Is_Entity_Name (Renamed_Object (Orig))
6569 loop
6570 Orig := Entity (Renamed_Object (Orig));
6571 end loop;
6573 return Orig;
6574 end Original_Entity;
6576 -- Start of processing for Same_Instantiated_Variable
6578 begin
6579 return Ekind (E1) = Ekind (E2)
6580 and then Original_Entity (E1) = Original_Entity (E2);
6581 end Same_Instantiated_Variable;
6583 -- Start of processing for Check_Formal_Package_Instance
6585 begin
6586 Prev_E1 := E1;
6587 while Present (E1) and then Present (E2) loop
6588 exit when Ekind (E1) = E_Package
6589 and then Renamed_Entity (E1) = Renamed_Entity (Actual_Pack);
6591 -- If the formal is the renaming of the formal package, this
6592 -- is the end of its formal part, which may occur before the
6593 -- end of the formal part in the actual in the presence of
6594 -- defaulted parameters in the formal package.
6596 exit when Nkind (Parent (E2)) = N_Package_Renaming_Declaration
6597 and then Renamed_Entity (E2) = Scope (E2);
6599 -- The analysis of the actual may generate additional internal
6600 -- entities. If the formal is defaulted, there is no corresponding
6601 -- analysis and the internal entities must be skipped, until we
6602 -- find corresponding entities again.
6604 if Comes_From_Source (E2)
6605 and then not Comes_From_Source (E1)
6606 and then Chars (E1) /= Chars (E2)
6607 then
6608 while Present (E1) and then Chars (E1) /= Chars (E2) loop
6609 Next_Entity (E1);
6610 end loop;
6611 end if;
6613 if No (E1) then
6614 return;
6616 -- Entities may be declared without full declaration, such as
6617 -- itypes and predefined operators (concatenation for arrays, eg).
6618 -- Skip it and keep the formal entity to find a later match for it.
6620 elsif No (Parent (E2)) and then Ekind (E1) /= Ekind (E2) then
6621 E1 := Prev_E1;
6622 goto Next_E;
6624 -- If the formal entity comes from a formal declaration, it was
6625 -- defaulted in the formal package, and no check is needed on it.
6627 elsif Nkind (Original_Node (Parent (E2))) in
6628 N_Formal_Object_Declaration | N_Formal_Type_Declaration
6629 then
6630 -- If the formal is a tagged type the corresponding class-wide
6631 -- type has been generated as well, and it must be skipped.
6633 if Is_Type (E2) and then Is_Tagged_Type (E2) then
6634 Next_Entity (E2);
6635 end if;
6637 goto Next_E;
6639 -- Ditto for defaulted formal subprograms.
6641 elsif Is_Overloadable (E1)
6642 and then Nkind (Unit_Declaration_Node (E2)) in
6643 N_Formal_Subprogram_Declaration
6644 then
6645 goto Next_E;
6647 elsif Is_Defaulted (E1) then
6648 goto Next_E;
6650 elsif Is_Type (E1) then
6652 -- Subtypes must statically match. E1, E2 are the local entities
6653 -- that are subtypes of the actuals. Itypes generated for other
6654 -- parameters need not be checked, the check will be performed
6655 -- on the parameters themselves.
6657 -- If E2 is a formal type declaration, it is a defaulted parameter
6658 -- and needs no checking.
6660 if not Is_Itype (E1) and then not Is_Itype (E2) then
6661 Check_Mismatch
6662 (not Is_Type (E2)
6663 or else Etype (E1) /= Etype (E2)
6664 or else not Subtypes_Statically_Match (E1, E2));
6665 end if;
6667 elsif Ekind (E1) = E_Constant then
6669 -- IN parameters must denote the same static value, or the same
6670 -- constant, or the literal null.
6672 Expr1 := Expression (Parent (E1));
6674 if Ekind (E2) /= E_Constant then
6675 Check_Mismatch (True);
6676 goto Next_E;
6677 else
6678 Expr2 := Expression (Parent (E2));
6679 end if;
6681 if Is_OK_Static_Expression (Expr1) then
6682 if not Is_OK_Static_Expression (Expr2) then
6683 Check_Mismatch (True);
6685 elsif Is_Discrete_Type (Etype (E1)) then
6686 declare
6687 V1 : constant Uint := Expr_Value (Expr1);
6688 V2 : constant Uint := Expr_Value (Expr2);
6689 begin
6690 Check_Mismatch (V1 /= V2);
6691 end;
6693 elsif Is_Real_Type (Etype (E1)) then
6694 declare
6695 V1 : constant Ureal := Expr_Value_R (Expr1);
6696 V2 : constant Ureal := Expr_Value_R (Expr2);
6697 begin
6698 Check_Mismatch (V1 /= V2);
6699 end;
6701 elsif Is_String_Type (Etype (E1))
6702 and then Nkind (Expr1) = N_String_Literal
6703 then
6704 if Nkind (Expr2) /= N_String_Literal then
6705 Check_Mismatch (True);
6706 else
6707 Check_Mismatch
6708 (not String_Equal (Strval (Expr1), Strval (Expr2)));
6709 end if;
6710 end if;
6712 elsif Is_Entity_Name (Expr1) then
6713 if Is_Entity_Name (Expr2) then
6714 if Entity (Expr1) = Entity (Expr2) then
6715 null;
6716 else
6717 Check_Mismatch
6718 (not Same_Instantiated_Constant
6719 (Entity (Expr1), Entity (Expr2)));
6720 end if;
6722 else
6723 Check_Mismatch (True);
6724 end if;
6726 elsif Is_Entity_Name (Original_Node (Expr1))
6727 and then Is_Entity_Name (Expr2)
6728 and then Same_Instantiated_Constant
6729 (Entity (Original_Node (Expr1)), Entity (Expr2))
6730 then
6731 null;
6733 elsif Nkind (Expr1) = N_Null then
6734 Check_Mismatch (Nkind (Expr1) /= N_Null);
6736 else
6737 Check_Mismatch (True);
6738 end if;
6740 elsif Ekind (E1) = E_Variable then
6741 Check_Mismatch (not Same_Instantiated_Variable (E1, E2));
6743 elsif Ekind (E1) = E_Package then
6744 Check_Mismatch
6745 (Ekind (E1) /= Ekind (E2)
6746 or else (Present (Renamed_Entity (E2))
6747 and then Renamed_Entity (E1) /=
6748 Renamed_Entity (E2)));
6750 elsif Is_Overloadable (E1) then
6751 -- Verify that the actual subprograms match. Note that actuals
6752 -- that are attributes are rewritten as subprograms. If the
6753 -- subprogram in the formal package is defaulted, no check is
6754 -- needed. Note that this can only happen in Ada 2005 when the
6755 -- formal package can be partially parameterized.
6757 if Nkind (Unit_Declaration_Node (E1)) =
6758 N_Subprogram_Renaming_Declaration
6759 and then From_Default (Unit_Declaration_Node (E1))
6760 then
6761 null;
6763 -- If the formal package has an "others" box association that
6764 -- covers this formal, there is no need for a check either.
6766 elsif Nkind (Unit_Declaration_Node (E2)) in
6767 N_Formal_Subprogram_Declaration
6768 and then Box_Present (Unit_Declaration_Node (E2))
6769 then
6770 null;
6772 -- No check needed if subprogram is a defaulted null procedure
6774 elsif No (Alias (E2))
6775 and then Ekind (E2) = E_Procedure
6776 and then
6777 Null_Present (Specification (Unit_Declaration_Node (E2)))
6778 then
6779 null;
6781 -- Otherwise the actual in the formal and the actual in the
6782 -- instantiation of the formal must match, up to renamings.
6784 else
6785 Check_Mismatch
6786 (Ekind (E2) /= Ekind (E1)
6787 or else not Same_Instantiated_Function (E1, E2));
6788 end if;
6790 else
6791 raise Program_Error;
6792 end if;
6794 <<Next_E>>
6795 Prev_E1 := E1;
6796 Next_Entity (E1);
6797 Next_Entity (E2);
6798 end loop;
6799 end Check_Formal_Package_Instance;
6801 ---------------------------
6802 -- Check_Formal_Packages --
6803 ---------------------------
6805 procedure Check_Formal_Packages (P_Id : Entity_Id) is
6806 E : Entity_Id;
6807 Formal_P : Entity_Id;
6808 Formal_Decl : Node_Id;
6810 begin
6811 -- Iterate through the declarations in the instance, looking for package
6812 -- renaming declarations that denote instances of formal packages, until
6813 -- we find the renaming of the current package itself. The declaration
6814 -- of a formal package that requires conformance checking is followed by
6815 -- an internal entity that is the abbreviated instance.
6817 E := First_Entity (P_Id);
6818 while Present (E) loop
6819 if Ekind (E) = E_Package then
6820 exit when Renamed_Entity (E) = P_Id;
6822 if Nkind (Parent (E)) = N_Package_Renaming_Declaration then
6823 Formal_Decl := Parent (Associated_Formal_Package (E));
6825 if Requires_Conformance_Checking (Formal_Decl) then
6826 Formal_P := Next_Entity (E);
6828 -- If the instance is within an enclosing instance body
6829 -- there is no need to verify the legality of current formal
6830 -- packages because they were legal in the generic body.
6831 -- This optimization may be applicable elsewhere, and it
6832 -- also removes spurious errors that may arise with
6833 -- on-the-fly inlining and confusion between private and
6834 -- full views.
6836 if not In_Instance_Body then
6837 Check_Formal_Package_Instance (Formal_P, E);
6838 end if;
6840 -- Restore the visibility of formals of the formal instance
6841 -- that are not defaulted, and are hidden within the current
6842 -- generic. These formals may be visible within an enclosing
6843 -- generic.
6845 declare
6846 Elmt : Elmt_Id;
6847 begin
6848 Elmt := First_Elmt (Hidden_In_Formal_Instance (Formal_P));
6849 while Present (Elmt) loop
6850 Set_Is_Hidden (Node (Elmt), False);
6851 Next_Elmt (Elmt);
6852 end loop;
6853 end;
6855 -- After checking, remove the internal validating package.
6856 -- It is only needed for semantic checks, and as it may
6857 -- contain generic formal declarations it should not reach
6858 -- gigi.
6860 Remove (Unit_Declaration_Node (Formal_P));
6861 end if;
6862 end if;
6863 end if;
6865 Next_Entity (E);
6866 end loop;
6867 end Check_Formal_Packages;
6869 ---------------------------------
6870 -- Check_Forward_Instantiation --
6871 ---------------------------------
6873 procedure Check_Forward_Instantiation (Decl : Node_Id) is
6874 S : Entity_Id;
6875 Gen_Comp : Entity_Id := Cunit_Entity (Get_Source_Unit (Decl));
6877 begin
6878 -- The instantiation appears before the generic body if we are in the
6879 -- scope of the unit containing the generic, either in its spec or in
6880 -- the package body, and before the generic body.
6882 if Ekind (Gen_Comp) = E_Package_Body then
6883 Gen_Comp := Spec_Entity (Gen_Comp);
6884 end if;
6886 if In_Open_Scopes (Gen_Comp)
6887 and then No (Corresponding_Body (Decl))
6888 then
6889 S := Current_Scope;
6891 while Present (S)
6892 and then not Is_Compilation_Unit (S)
6893 and then not Is_Child_Unit (S)
6894 loop
6895 if Ekind (S) = E_Package then
6896 Set_Has_Forward_Instantiation (S);
6897 end if;
6899 S := Scope (S);
6900 end loop;
6901 end if;
6902 end Check_Forward_Instantiation;
6904 ---------------------------
6905 -- Check_Generic_Actuals --
6906 ---------------------------
6908 -- The visibility of the actuals may be different between the point of
6909 -- generic instantiation and the instantiation of the body.
6911 procedure Check_Generic_Actuals
6912 (Instance : Entity_Id;
6913 Is_Formal_Box : Boolean)
6915 E : Entity_Id;
6916 Astype : Entity_Id;
6918 begin
6919 E := First_Entity (Instance);
6920 while Present (E) loop
6921 if Is_Type (E)
6922 and then Nkind (Parent (E)) = N_Subtype_Declaration
6923 and then Scope (Etype (E)) /= Instance
6924 and then Is_Entity_Name (Subtype_Indication (Parent (E)))
6925 then
6926 -- Restore the proper view of the actual from the information
6927 -- saved earlier by Instantiate_Type.
6929 Check_Private_View (Subtype_Indication (Parent (E)));
6931 -- If the actual is itself the formal of a parent instance,
6932 -- then also restore the proper view of its actual and so on.
6933 -- That's necessary for nested instantiations of the form
6935 -- generic
6936 -- type Component is private;
6937 -- type Array_Type is array (Positive range <>) of Component;
6938 -- procedure Proc;
6940 -- when the outermost actuals have inconsistent views, because
6941 -- the Component_Type of Array_Type of the inner instantiations
6942 -- is the actual of Component of the outermost one and not that
6943 -- of the corresponding inner instantiations.
6945 Astype := Ancestor_Subtype (E);
6946 while Present (Astype)
6947 and then Nkind (Parent (Astype)) = N_Subtype_Declaration
6948 and then Present (Generic_Parent_Type (Parent (Astype)))
6949 and then Is_Entity_Name (Subtype_Indication (Parent (Astype)))
6950 loop
6951 Check_Private_View (Subtype_Indication (Parent (Astype)));
6952 Astype := Ancestor_Subtype (Astype);
6953 end loop;
6955 Set_Is_Generic_Actual_Type (E);
6957 if Is_Private_Type (E) and then Present (Full_View (E)) then
6958 Set_Is_Generic_Actual_Type (Full_View (E));
6959 end if;
6961 Set_Is_Hidden (E, False);
6962 Set_Is_Potentially_Use_Visible (E, In_Use (Instance));
6964 -- We constructed the generic actual type as a subtype of the
6965 -- supplied type. This means that it normally would not inherit
6966 -- subtype specific attributes of the actual, which is wrong for
6967 -- the generic case.
6969 Astype := Ancestor_Subtype (E);
6971 if No (Astype) then
6973 -- This can happen when E is an itype that is the full view of
6974 -- a private type completed, e.g. with a constrained array. In
6975 -- that case, use the first subtype, which will carry size
6976 -- information. The base type itself is unconstrained and will
6977 -- not carry it.
6979 Astype := First_Subtype (E);
6980 end if;
6982 Set_Size_Info (E, (Astype));
6983 Copy_RM_Size (To => E, From => Astype);
6984 Set_First_Rep_Item (E, First_Rep_Item (Astype));
6986 if Is_Discrete_Or_Fixed_Point_Type (E) then
6987 Set_RM_Size (E, RM_Size (Astype));
6988 end if;
6990 elsif Ekind (E) = E_Package then
6992 -- If this is the renaming for the current instance, we're done.
6993 -- Otherwise it is a formal package. If the corresponding formal
6994 -- was declared with a box, the (instantiations of the) generic
6995 -- formal part are also visible. Otherwise, ignore the entity
6996 -- created to validate the actuals.
6998 if Renamed_Entity (E) = Instance then
6999 exit;
7001 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
7002 null;
7004 -- The visibility of a formal of an enclosing generic is already
7005 -- correct.
7007 elsif Denotes_Formal_Package (E) then
7008 null;
7010 elsif Present (Associated_Formal_Package (E))
7011 and then not Is_Generic_Formal (E)
7012 then
7013 if Box_Present (Parent (Associated_Formal_Package (E))) then
7014 Check_Generic_Actuals (Renamed_Entity (E), True);
7016 else
7017 Check_Generic_Actuals (Renamed_Entity (E), False);
7018 end if;
7020 Set_Is_Hidden (E, False);
7021 end if;
7023 -- If this is a subprogram instance (in a wrapper package) the
7024 -- actual is fully visible.
7026 elsif Is_Wrapper_Package (Instance) then
7027 Set_Is_Hidden (E, False);
7029 -- If the formal package is declared with a box, or if the formal
7030 -- parameter is defaulted, it is visible in the body.
7032 elsif Is_Formal_Box or else Is_Visible_Formal (E) then
7033 Set_Is_Hidden (E, False);
7034 end if;
7036 if Ekind (E) = E_Constant then
7038 -- If the type of the actual is a private type declared in the
7039 -- enclosing scope of the generic unit, the body of the generic
7040 -- sees the full view of the type (because it has to appear in
7041 -- the corresponding package body). If the type is private now,
7042 -- exchange views to restore the proper visiblity in the instance.
7044 declare
7045 Typ : constant Entity_Id := Base_Type (Etype (E));
7046 -- The type of the actual
7048 Gen_Id : Entity_Id;
7049 -- The generic unit
7051 Parent_Scope : Entity_Id;
7052 -- The enclosing scope of the generic unit
7054 begin
7055 if Is_Wrapper_Package (Instance) then
7056 Gen_Id :=
7057 Generic_Parent
7058 (Specification
7059 (Unit_Declaration_Node
7060 (Related_Instance (Instance))));
7061 else
7062 Gen_Id :=
7063 Generic_Parent (Package_Specification (Instance));
7064 end if;
7066 Parent_Scope := Scope (Gen_Id);
7068 -- The exchange is only needed if the generic is defined
7069 -- within a package which is not a common ancestor of the
7070 -- scope of the instance, and is not already in scope.
7072 if Is_Private_Type (Typ)
7073 and then Scope (Typ) = Parent_Scope
7074 and then Scope (Instance) /= Parent_Scope
7075 and then Ekind (Parent_Scope) = E_Package
7076 and then not Is_Child_Unit (Gen_Id)
7077 then
7078 Switch_View (Typ);
7080 -- If the type of the entity is a subtype, it may also have
7081 -- to be made visible, together with the base type of its
7082 -- full view, after exchange.
7084 if Is_Private_Type (Etype (E)) then
7085 Switch_View (Etype (E));
7086 Switch_View (Base_Type (Etype (E)));
7087 end if;
7088 end if;
7089 end;
7090 end if;
7092 Next_Entity (E);
7093 end loop;
7094 end Check_Generic_Actuals;
7096 ------------------------------
7097 -- Check_Generic_Child_Unit --
7098 ------------------------------
7100 procedure Check_Generic_Child_Unit
7101 (Gen_Id : Node_Id;
7102 Parent_Installed : in out Boolean)
7104 Loc : constant Source_Ptr := Sloc (Gen_Id);
7105 Gen_Par : Entity_Id := Empty;
7106 E : Entity_Id;
7107 Inst_Par : Entity_Id;
7108 S : Node_Id;
7110 function Find_Generic_Child
7111 (Scop : Entity_Id;
7112 Id : Node_Id) return Entity_Id;
7113 -- Search generic parent for possible child unit with the given name
7115 function In_Enclosing_Instance return Boolean;
7116 -- Within an instance of the parent, the child unit may be denoted by
7117 -- a simple name, or an abbreviated expanded name. Examine enclosing
7118 -- scopes to locate a possible parent instantiation.
7120 ------------------------
7121 -- Find_Generic_Child --
7122 ------------------------
7124 function Find_Generic_Child
7125 (Scop : Entity_Id;
7126 Id : Node_Id) return Entity_Id
7128 E : Entity_Id;
7130 begin
7131 -- If entity of name is already set, instance has already been
7132 -- resolved, e.g. in an enclosing instantiation.
7134 if Present (Entity (Id)) then
7135 if Scope (Entity (Id)) = Scop then
7136 return Entity (Id);
7137 else
7138 return Empty;
7139 end if;
7141 else
7142 E := First_Entity (Scop);
7143 while Present (E) loop
7144 if Chars (E) = Chars (Id)
7145 and then Is_Child_Unit (E)
7146 then
7147 if Is_Child_Unit (E)
7148 and then not Is_Visible_Lib_Unit (E)
7149 then
7150 Error_Msg_NE
7151 ("generic child unit& is not visible", Gen_Id, E);
7152 end if;
7154 Set_Entity (Id, E);
7155 return E;
7156 end if;
7158 Next_Entity (E);
7159 end loop;
7161 return Empty;
7162 end if;
7163 end Find_Generic_Child;
7165 ---------------------------
7166 -- In_Enclosing_Instance --
7167 ---------------------------
7169 function In_Enclosing_Instance return Boolean is
7170 Enclosing_Instance : Node_Id;
7171 Instance_Decl : Node_Id;
7173 begin
7174 -- We do not inline any call that contains instantiations, except
7175 -- for instantiations of Unchecked_Conversion, so if we are within
7176 -- an inlined body the current instance does not require parents.
7178 if In_Inlined_Body then
7179 pragma Assert (Chars (Gen_Id) = Name_Unchecked_Conversion);
7180 return False;
7181 end if;
7183 -- Loop to check enclosing scopes
7185 Enclosing_Instance := Current_Scope;
7186 while Present (Enclosing_Instance) loop
7187 Instance_Decl := Unit_Declaration_Node (Enclosing_Instance);
7189 if Ekind (Enclosing_Instance) = E_Package
7190 and then Is_Generic_Instance (Enclosing_Instance)
7191 and then Present
7192 (Generic_Parent (Specification (Instance_Decl)))
7193 then
7194 -- Check whether the generic we are looking for is a child of
7195 -- this instance.
7197 E := Find_Generic_Child
7198 (Generic_Parent (Specification (Instance_Decl)), Gen_Id);
7199 exit when Present (E);
7201 else
7202 E := Empty;
7203 end if;
7205 Enclosing_Instance := Scope (Enclosing_Instance);
7206 end loop;
7208 if No (E) then
7210 -- Not a child unit
7212 Analyze (Gen_Id);
7213 return False;
7215 else
7216 Rewrite (Gen_Id,
7217 Make_Expanded_Name (Loc,
7218 Chars => Chars (E),
7219 Prefix => New_Occurrence_Of (Enclosing_Instance, Loc),
7220 Selector_Name => New_Occurrence_Of (E, Loc)));
7222 Set_Entity (Gen_Id, E);
7223 Set_Etype (Gen_Id, Etype (E));
7224 Parent_Installed := False; -- Already in scope.
7225 return True;
7226 end if;
7227 end In_Enclosing_Instance;
7229 -- Start of processing for Check_Generic_Child_Unit
7231 begin
7232 -- If the name of the generic is given by a selected component, it may
7233 -- be the name of a generic child unit, and the prefix is the name of an
7234 -- instance of the parent, in which case the child unit must be visible.
7235 -- If this instance is not in scope, it must be placed there and removed
7236 -- after instantiation, because what is being instantiated is not the
7237 -- original child, but the corresponding child present in the instance
7238 -- of the parent.
7240 -- If the child is instantiated within the parent, it can be given by
7241 -- a simple name. In this case the instance is already in scope, but
7242 -- the child generic must be recovered from the generic parent as well.
7244 if Nkind (Gen_Id) = N_Selected_Component then
7245 S := Selector_Name (Gen_Id);
7246 Analyze (Prefix (Gen_Id));
7247 Inst_Par := Entity (Prefix (Gen_Id));
7249 if Ekind (Inst_Par) = E_Package
7250 and then Present (Renamed_Entity (Inst_Par))
7251 then
7252 Inst_Par := Renamed_Entity (Inst_Par);
7253 end if;
7255 if Ekind (Inst_Par) = E_Package then
7256 if Nkind (Parent (Inst_Par)) = N_Package_Specification then
7257 Gen_Par := Generic_Parent (Parent (Inst_Par));
7259 elsif Nkind (Parent (Inst_Par)) = N_Defining_Program_Unit_Name
7260 and then
7261 Nkind (Parent (Parent (Inst_Par))) = N_Package_Specification
7262 then
7263 Gen_Par := Generic_Parent (Parent (Parent (Inst_Par)));
7264 end if;
7266 elsif Ekind (Inst_Par) = E_Generic_Package
7267 and then Nkind (Parent (Gen_Id)) = N_Formal_Package_Declaration
7268 then
7269 -- A formal package may be a real child package, and not the
7270 -- implicit instance within a parent. In this case the child is
7271 -- not visible and has to be retrieved explicitly as well.
7273 Gen_Par := Inst_Par;
7274 end if;
7276 if Present (Gen_Par) then
7278 -- The prefix denotes an instantiation. The entity itself may be a
7279 -- nested generic, or a child unit.
7281 E := Find_Generic_Child (Gen_Par, S);
7283 if Present (E) then
7284 Change_Selected_Component_To_Expanded_Name (Gen_Id);
7285 Set_Entity (Gen_Id, E);
7286 Set_Etype (Gen_Id, Etype (E));
7287 Set_Entity (S, E);
7288 Set_Etype (S, Etype (E));
7290 -- Indicate that this is a reference to the parent
7292 if In_Extended_Main_Source_Unit (Gen_Id) then
7293 Set_Is_Instantiated (Inst_Par);
7294 end if;
7296 -- A common mistake is to replicate the naming scheme of a
7297 -- hierarchy by instantiating a generic child directly, rather
7298 -- than the implicit child in a parent instance:
7300 -- generic .. package Gpar is ..
7301 -- generic .. package Gpar.Child is ..
7302 -- package Par is new Gpar ();
7304 -- with Gpar.Child;
7305 -- package Par.Child is new Gpar.Child ();
7306 -- rather than Par.Child
7308 -- In this case the instantiation is within Par, which is an
7309 -- instance, but Gpar does not denote Par because we are not IN
7310 -- the instance of Gpar, so this is illegal. The test below
7311 -- recognizes this particular case.
7313 if Is_Child_Unit (E)
7314 and then not Comes_From_Source (Entity (Prefix (Gen_Id)))
7315 and then (not In_Instance
7316 or else Nkind (Parent (Parent (Gen_Id))) =
7317 N_Compilation_Unit)
7318 then
7319 Error_Msg_N
7320 ("prefix of generic child unit must be instance of parent",
7321 Gen_Id);
7322 end if;
7324 if not In_Open_Scopes (Inst_Par)
7325 and then Nkind (Parent (Gen_Id)) not in
7326 N_Generic_Renaming_Declaration
7327 then
7328 Install_Parent (Inst_Par);
7329 Parent_Installed := True;
7331 elsif In_Open_Scopes (Inst_Par) then
7333 -- If the parent is already installed, install the actuals
7334 -- for its formal packages. This is necessary when the child
7335 -- instance is a child of the parent instance: in this case,
7336 -- the parent is placed on the scope stack but the formal
7337 -- packages are not made visible.
7339 Install_Formal_Packages (Inst_Par);
7340 end if;
7342 else
7343 -- If the generic parent does not contain an entity that
7344 -- corresponds to the selector, the instance doesn't either.
7345 -- Analyzing the node will yield the appropriate error message.
7346 -- If the entity is not a child unit, then it is an inner
7347 -- generic in the parent.
7349 Analyze (Gen_Id);
7350 end if;
7352 else
7353 Analyze (Gen_Id);
7355 if Is_Child_Unit (Entity (Gen_Id))
7356 and then
7357 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
7358 and then not In_Open_Scopes (Inst_Par)
7359 then
7360 Install_Parent (Inst_Par);
7361 Parent_Installed := True;
7363 -- The generic unit may be the renaming of the implicit child
7364 -- present in an instance. In that case the parent instance is
7365 -- obtained from the name of the renamed entity.
7367 elsif Ekind (Entity (Gen_Id)) = E_Generic_Package
7368 and then Present (Renamed_Entity (Entity (Gen_Id)))
7369 and then Is_Child_Unit (Renamed_Entity (Entity (Gen_Id)))
7370 then
7371 declare
7372 Renamed_Package : constant Node_Id :=
7373 Name (Parent (Entity (Gen_Id)));
7374 begin
7375 if Nkind (Renamed_Package) = N_Expanded_Name then
7376 Inst_Par := Entity (Prefix (Renamed_Package));
7377 Install_Parent (Inst_Par);
7378 Parent_Installed := True;
7379 end if;
7380 end;
7381 end if;
7382 end if;
7384 elsif Nkind (Gen_Id) = N_Expanded_Name then
7386 -- Entity already present, analyze prefix, whose meaning may be an
7387 -- instance in the current context. If it is an instance of a
7388 -- relative within another, the proper parent may still have to be
7389 -- installed, if they are not of the same generation.
7391 Analyze (Prefix (Gen_Id));
7393 -- Prevent cascaded errors
7395 if Etype (Prefix (Gen_Id)) = Any_Type then
7396 return;
7397 end if;
7399 -- In the unlikely case that a local declaration hides the name of
7400 -- the parent package, locate it on the homonym chain. If the context
7401 -- is an instance of the parent, the renaming entity is flagged as
7402 -- such.
7404 Inst_Par := Entity (Prefix (Gen_Id));
7405 while Present (Inst_Par)
7406 and then not Is_Package_Or_Generic_Package (Inst_Par)
7407 loop
7408 Inst_Par := Homonym (Inst_Par);
7409 end loop;
7411 pragma Assert (Present (Inst_Par));
7412 Set_Entity (Prefix (Gen_Id), Inst_Par);
7414 if In_Enclosing_Instance then
7415 null;
7417 elsif Present (Entity (Gen_Id))
7418 and then No (Renamed_Entity (Entity (Gen_Id)))
7419 and then Is_Child_Unit (Entity (Gen_Id))
7420 and then not In_Open_Scopes (Inst_Par)
7421 then
7422 Install_Parent (Inst_Par);
7423 Parent_Installed := True;
7425 -- Handle renaming of generic child unit
7427 elsif Present (Entity (Gen_Id))
7428 and then Present (Renamed_Entity (Entity (Gen_Id)))
7429 and then Is_Child_Unit (Renamed_Entity (Entity (Gen_Id)))
7430 then
7431 declare
7432 E : Entity_Id;
7433 Ren_Decl : Node_Id;
7435 begin
7436 -- The entity of the renamed generic child unit does not
7437 -- have any reference to the instantiated parent. In order to
7438 -- locate it we traverse the scope containing the renaming
7439 -- declaration; the instance of the parent is available in
7440 -- the prefix of the renaming declaration. For example:
7442 -- package A is
7443 -- package Inst_Par is new ...
7444 -- generic package Ren_Child renames Ins_Par.Child;
7445 -- end;
7447 -- with A;
7448 -- package B is
7449 -- package Inst_Child is new A.Ren_Child;
7450 -- end;
7452 E := First_Entity (Entity (Prefix (Gen_Id)));
7453 while Present (E) loop
7454 if not Is_Object (E)
7455 and then Present (Renamed_Entity (E))
7456 and then
7457 Renamed_Entity (E) = Renamed_Entity (Entity (Gen_Id))
7458 then
7459 Ren_Decl := Parent (E);
7460 Inst_Par := Entity (Prefix (Name (Ren_Decl)));
7462 if not In_Open_Scopes (Inst_Par) then
7463 Install_Parent (Inst_Par);
7464 Parent_Installed := True;
7465 end if;
7467 exit;
7468 end if;
7470 E := Next_Entity (E);
7471 end loop;
7472 end;
7473 end if;
7475 elsif In_Enclosing_Instance then
7477 -- The child unit is found in some enclosing scope
7479 null;
7481 else
7482 Analyze (Gen_Id);
7484 -- If this is the renaming of the implicit child in a parent
7485 -- instance, recover the parent name and install it.
7487 if Is_Entity_Name (Gen_Id) then
7488 E := Entity (Gen_Id);
7490 if Is_Generic_Unit (E)
7491 and then Nkind (Parent (E)) in N_Generic_Renaming_Declaration
7492 and then Is_Child_Unit (Renamed_Entity (E))
7493 and then Is_Generic_Unit (Scope (Renamed_Entity (E)))
7494 and then Nkind (Name (Parent (E))) = N_Expanded_Name
7495 then
7496 Rewrite (Gen_Id, New_Copy_Tree (Name (Parent (E))));
7497 Inst_Par := Entity (Prefix (Gen_Id));
7499 if not In_Open_Scopes (Inst_Par) then
7500 Install_Parent (Inst_Par);
7501 Parent_Installed := True;
7502 end if;
7504 -- If it is a child unit of a non-generic parent, it may be
7505 -- use-visible and given by a direct name. Install parent as
7506 -- for other cases.
7508 elsif Is_Generic_Unit (E)
7509 and then Is_Child_Unit (E)
7510 and then
7511 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
7512 and then not Is_Generic_Unit (Scope (E))
7513 then
7514 if not In_Open_Scopes (Scope (E)) then
7515 Install_Parent (Scope (E));
7516 Parent_Installed := True;
7517 end if;
7518 end if;
7519 end if;
7520 end if;
7521 end Check_Generic_Child_Unit;
7523 -----------------------------
7524 -- Check_Hidden_Child_Unit --
7525 -----------------------------
7527 procedure Check_Hidden_Child_Unit
7528 (N : Node_Id;
7529 Gen_Unit : Entity_Id;
7530 Act_Decl_Id : Entity_Id)
7532 Gen_Id : constant Node_Id := Name (N);
7534 begin
7535 if Is_Child_Unit (Gen_Unit)
7536 and then Is_Child_Unit (Act_Decl_Id)
7537 and then Nkind (Gen_Id) = N_Expanded_Name
7538 and then Entity (Prefix (Gen_Id)) = Scope (Act_Decl_Id)
7539 and then Chars (Gen_Unit) = Chars (Act_Decl_Id)
7540 then
7541 Error_Msg_Node_2 := Scope (Act_Decl_Id);
7542 Error_Msg_NE
7543 ("generic unit & is implicitly declared in &",
7544 Defining_Unit_Name (N), Gen_Unit);
7545 Error_Msg_N ("\instance must have different name",
7546 Defining_Unit_Name (N));
7547 end if;
7548 end Check_Hidden_Child_Unit;
7550 ------------------------
7551 -- Check_Private_View --
7552 ------------------------
7554 procedure Check_Private_View (N : Node_Id) is
7555 T : constant Entity_Id := Etype (N);
7556 BT : Entity_Id;
7558 begin
7559 -- Exchange views if the type was not private in the generic but is
7560 -- private at the point of instantiation. Do not exchange views if
7561 -- the scope of the type is in scope. This can happen if both generic
7562 -- and instance are sibling units, or if type is defined in a parent.
7563 -- In this case the visibility of the type will be correct for all
7564 -- semantic checks.
7566 if Present (T) then
7567 BT := Base_Type (T);
7569 if Is_Private_Type (T)
7570 and then not Has_Private_View (N)
7571 and then Present (Full_View (T))
7572 and then not In_Open_Scopes (Scope (T))
7573 then
7574 -- In the generic, the full declaration was visible
7576 Switch_View (T);
7578 elsif Has_Private_View (N)
7579 and then not Is_Private_Type (T)
7580 and then not Has_Been_Exchanged (T)
7581 and then (not In_Open_Scopes (Scope (T))
7582 or else Nkind (Parent (N)) = N_Subtype_Declaration)
7583 then
7584 -- In the generic, only the private declaration was visible
7586 -- If the type appears in a subtype declaration, the subtype in
7587 -- instance must have a view compatible with that of its parent,
7588 -- which must be exchanged (see corresponding code in Restore_
7589 -- Private_Views) so we make an exception to the open scope rule.
7591 Prepend_Elmt (T, Exchanged_Views);
7592 Exchange_Declarations (Etype (Get_Associated_Node (N)));
7594 -- Finally, a non-private subtype may have a private base type, which
7595 -- must be exchanged for consistency. This can happen when a package
7596 -- body is instantiated, when the scope stack is empty but in fact
7597 -- the subtype and the base type are declared in an enclosing scope.
7599 -- Note that in this case we introduce an inconsistency in the view
7600 -- set, because we switch the base type BT, but there could be some
7601 -- private dependent subtypes of BT which remain unswitched. Such
7602 -- subtypes might need to be switched at a later point (see specific
7603 -- provision for that case in Switch_View).
7605 elsif not Is_Private_Type (T)
7606 and then not Has_Private_View (N)
7607 and then Is_Private_Type (BT)
7608 and then Present (Full_View (BT))
7609 and then not Is_Generic_Type (BT)
7610 and then not In_Open_Scopes (BT)
7611 then
7612 Prepend_Elmt (Full_View (BT), Exchanged_Views);
7613 Exchange_Declarations (BT);
7614 end if;
7615 end if;
7616 end Check_Private_View;
7618 -----------------------------
7619 -- Check_Hidden_Primitives --
7620 -----------------------------
7622 function Check_Hidden_Primitives (Assoc_List : List_Id) return Elist_Id is
7623 Actual : Node_Id;
7624 Gen_T : Entity_Id;
7625 Result : Elist_Id := No_Elist;
7627 begin
7628 if No (Assoc_List) then
7629 return No_Elist;
7630 end if;
7632 -- Traverse the list of associations between formals and actuals
7633 -- searching for renamings of tagged types
7635 Actual := First (Assoc_List);
7636 while Present (Actual) loop
7637 if Nkind (Actual) = N_Subtype_Declaration then
7638 Gen_T := Generic_Parent_Type (Actual);
7640 if Present (Gen_T) and then Is_Tagged_Type (Gen_T) then
7642 -- Traverse the list of primitives of the actual types
7643 -- searching for hidden primitives that are visible in the
7644 -- corresponding generic formal; leave them visible and
7645 -- append them to Result to restore their decoration later.
7647 Install_Hidden_Primitives
7648 (Prims_List => Result,
7649 Gen_T => Gen_T,
7650 Act_T => Entity (Subtype_Indication (Actual)));
7651 end if;
7652 end if;
7654 Next (Actual);
7655 end loop;
7657 return Result;
7658 end Check_Hidden_Primitives;
7660 --------------------------
7661 -- Contains_Instance_Of --
7662 --------------------------
7664 function Contains_Instance_Of
7665 (Inner : Entity_Id;
7666 Outer : Entity_Id;
7667 N : Node_Id) return Boolean
7669 Elmt : Elmt_Id;
7670 Scop : Entity_Id;
7672 begin
7673 Scop := Outer;
7675 -- Verify that there are no circular instantiations. We check whether
7676 -- the unit contains an instance of the current scope or some enclosing
7677 -- scope (in case one of the instances appears in a subunit). Longer
7678 -- circularities involving subunits might seem too pathological to
7679 -- consider, but they were not too pathological for the authors of
7680 -- DEC bc30vsq, so we loop over all enclosing scopes, and mark all
7681 -- enclosing generic scopes as containing an instance.
7683 loop
7684 -- Within a generic subprogram body, the scope is not generic, to
7685 -- allow for recursive subprograms. Use the declaration to determine
7686 -- whether this is a generic unit.
7688 if Ekind (Scop) = E_Generic_Package
7689 or else (Is_Subprogram (Scop)
7690 and then Nkind (Unit_Declaration_Node (Scop)) =
7691 N_Generic_Subprogram_Declaration)
7692 then
7693 Elmt := First_Elmt (Inner_Instances (Inner));
7695 while Present (Elmt) loop
7696 if Node (Elmt) = Scop then
7697 Error_Msg_Node_2 := Inner;
7698 Error_Msg_NE
7699 ("circular instantiation: & instantiated within &!",
7700 N, Scop);
7701 return True;
7703 elsif Node (Elmt) = Inner then
7704 return True;
7706 elsif Contains_Instance_Of (Node (Elmt), Scop, N) then
7707 Error_Msg_Node_2 := Inner;
7708 Error_Msg_NE
7709 ("circular instantiation: & instantiated within &!",
7710 N, Node (Elmt));
7711 return True;
7712 end if;
7714 Next_Elmt (Elmt);
7715 end loop;
7717 -- Indicate that Inner is being instantiated within Scop
7719 Append_Elmt (Inner, Inner_Instances (Scop));
7720 end if;
7722 if Scop = Standard_Standard then
7723 exit;
7724 else
7725 Scop := Scope (Scop);
7726 end if;
7727 end loop;
7729 return False;
7730 end Contains_Instance_Of;
7732 -----------------------
7733 -- Copy_Generic_Node --
7734 -----------------------
7736 function Copy_Generic_Node
7737 (N : Node_Id;
7738 Parent_Id : Node_Id;
7739 Instantiating : Boolean) return Node_Id
7741 Ent : Entity_Id;
7742 New_N : Node_Id;
7744 function Copy_Generic_Descendant (D : Union_Id) return Union_Id;
7745 -- Check the given value of one of the Fields referenced by the current
7746 -- node to determine whether to copy it recursively. The field may hold
7747 -- a Node_Id, a List_Id, or an Elist_Id, or a plain value (Sloc, Uint,
7748 -- Char) in which case it need not be copied.
7750 procedure Copy_Descendants;
7751 -- Common utility for various nodes
7753 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id;
7754 -- Make copy of element list
7756 function Copy_Generic_List
7757 (L : List_Id;
7758 Parent_Id : Node_Id) return List_Id;
7759 -- Apply Copy_Generic_Node recursively to the members of a node list
7761 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean;
7762 -- True if an identifier is part of the defining program unit name of
7763 -- a child unit.
7764 -- Consider removing this subprogram now that ASIS no longer uses it.
7766 ----------------------
7767 -- Copy_Descendants --
7768 ----------------------
7770 procedure Copy_Descendants is
7771 procedure Walk is new
7772 Walk_Sinfo_Fields_Pairwise (Copy_Generic_Descendant);
7773 begin
7774 Walk (New_N, N);
7775 end Copy_Descendants;
7777 -----------------------------
7778 -- Copy_Generic_Descendant --
7779 -----------------------------
7781 function Copy_Generic_Descendant (D : Union_Id) return Union_Id is
7782 begin
7783 if D = Union_Id (Empty) then
7784 return D;
7786 elsif D in Node_Range then
7787 return Union_Id
7788 (Copy_Generic_Node (Node_Id (D), New_N, Instantiating));
7790 elsif D in List_Range then
7791 return Union_Id (Copy_Generic_List (List_Id (D), New_N));
7793 elsif D in Elist_Range then
7794 return Union_Id (Copy_Generic_Elist (Elist_Id (D)));
7796 -- Nothing else is copyable (e.g. Uint values), return as is
7798 else
7799 return D;
7800 end if;
7801 end Copy_Generic_Descendant;
7803 ------------------------
7804 -- Copy_Generic_Elist --
7805 ------------------------
7807 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id is
7808 M : Elmt_Id;
7809 L : Elist_Id;
7811 begin
7812 if Present (E) then
7813 L := New_Elmt_List;
7814 M := First_Elmt (E);
7815 while Present (M) loop
7816 Append_Elmt
7817 (Copy_Generic_Node (Node (M), Empty, Instantiating), L);
7818 Next_Elmt (M);
7819 end loop;
7821 return L;
7823 else
7824 return No_Elist;
7825 end if;
7826 end Copy_Generic_Elist;
7828 -----------------------
7829 -- Copy_Generic_List --
7830 -----------------------
7832 function Copy_Generic_List
7833 (L : List_Id;
7834 Parent_Id : Node_Id) return List_Id
7836 N : Node_Id;
7837 New_L : List_Id;
7839 begin
7840 if Present (L) then
7841 New_L := New_List;
7842 Set_Parent (New_L, Parent_Id);
7844 N := First (L);
7845 while Present (N) loop
7846 Append (Copy_Generic_Node (N, Empty, Instantiating), New_L);
7847 Next (N);
7848 end loop;
7850 return New_L;
7852 else
7853 return No_List;
7854 end if;
7855 end Copy_Generic_List;
7857 ---------------------------
7858 -- In_Defining_Unit_Name --
7859 ---------------------------
7861 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean is
7862 begin
7863 return
7864 Present (Parent (Nam))
7865 and then (Nkind (Parent (Nam)) = N_Defining_Program_Unit_Name
7866 or else
7867 (Nkind (Parent (Nam)) = N_Expanded_Name
7868 and then In_Defining_Unit_Name (Parent (Nam))));
7869 end In_Defining_Unit_Name;
7871 -- Start of processing for Copy_Generic_Node
7873 begin
7874 if N = Empty then
7875 return N;
7876 end if;
7878 New_N := New_Copy (N);
7880 -- Copy aspects if present
7882 if Has_Aspects (N) then
7883 Set_Has_Aspects (New_N, False);
7884 Set_Aspect_Specifications
7885 (New_N, Copy_Generic_List (Aspect_Specifications (N), Parent_Id));
7886 end if;
7888 -- If we are instantiating, we want to adjust the sloc based on the
7889 -- current S_Adjustment. However, if this is the root node of a subunit,
7890 -- we need to defer that adjustment to below (see "elsif Instantiating
7891 -- and Was_Stub"), so it comes after Create_Instantiation_Source has
7892 -- computed the adjustment.
7894 if Instantiating
7895 and then not (Nkind (N) in N_Proper_Body
7896 and then Was_Originally_Stub (N))
7897 then
7898 Adjust_Instantiation_Sloc (New_N, S_Adjustment);
7899 end if;
7901 if not Is_List_Member (N) then
7902 Set_Parent (New_N, Parent_Id);
7903 end if;
7905 -- Special casing for identifiers and other entity names and operators
7907 if Nkind (New_N) in N_Character_Literal
7908 | N_Expanded_Name
7909 | N_Identifier
7910 | N_Operator_Symbol
7911 | N_Op
7912 then
7913 if not Instantiating then
7915 -- Link both nodes in order to assign subsequently the entity of
7916 -- the copy to the original node, in case this is a global
7917 -- reference.
7919 Set_Associated_Node (N, New_N);
7921 -- If we are within an instantiation, this is a nested generic
7922 -- that has already been analyzed at the point of definition.
7923 -- We must preserve references that were global to the enclosing
7924 -- parent at that point. Other occurrences, whether global or
7925 -- local to the current generic, must be resolved anew, so we
7926 -- reset the entity in the generic copy. A global reference has a
7927 -- smaller depth than the parent, or else the same depth in case
7928 -- both are distinct compilation units.
7930 -- A child unit is implicitly declared within the enclosing parent
7931 -- but is in fact global to it, and must be preserved.
7933 -- It is also possible for Current_Instantiated_Parent to be
7934 -- defined, and for this not to be a nested generic, namely if
7935 -- the unit is loaded through Rtsfind. In that case, the entity of
7936 -- New_N is only a link to the associated node, and not a defining
7937 -- occurrence.
7939 -- The entities for parent units in the defining_program_unit of a
7940 -- generic child unit are established when the context of the unit
7941 -- is first analyzed, before the generic copy is made. They are
7942 -- preserved in the copy for use in e.g. ASIS queries.
7944 Ent := Entity (New_N);
7946 if No (Current_Instantiated_Parent.Gen_Id) then
7947 if No (Ent)
7948 or else Nkind (Ent) /= N_Defining_Identifier
7949 or else not In_Defining_Unit_Name (N)
7950 then
7951 Set_Associated_Node (New_N, Empty);
7952 end if;
7954 elsif No (Ent)
7955 or else Nkind (Ent) not in N_Entity
7956 or else No (Scope (Ent))
7957 or else
7958 (Scope (Ent) = Current_Instantiated_Parent.Gen_Id
7959 and then not Is_Child_Unit (Ent))
7960 or else
7961 (Scope_Depth_Set (Scope (Ent))
7962 and then
7963 Scope_Depth (Scope (Ent)) >
7964 Scope_Depth (Current_Instantiated_Parent.Gen_Id)
7965 and then
7966 Get_Source_Unit (Ent) =
7967 Get_Source_Unit (Current_Instantiated_Parent.Gen_Id))
7968 then
7969 Set_Associated_Node (New_N, Empty);
7970 end if;
7972 -- Case of instantiating identifier or some other name or operator
7974 else
7975 -- If the associated node is still defined, the entity in it
7976 -- is global, and must be copied to the instance. If this copy
7977 -- is being made for a body to inline, it is applied to an
7978 -- instantiated tree, and the entity is already present and
7979 -- must be also preserved.
7981 declare
7982 Assoc : constant Node_Id := Get_Associated_Node (N);
7984 begin
7985 if Present (Assoc) then
7986 if Nkind (Assoc) = Nkind (N) then
7987 Set_Entity (New_N, Entity (Assoc));
7988 Check_Private_View (N);
7990 -- Here we deal with a very peculiar case for which the
7991 -- Has_Private_View mechanism is not sufficient, because
7992 -- the reference to the type is implicit in the tree,
7993 -- that is to say, it's not referenced from a node but
7994 -- only from another type, namely through Component_Type.
7996 -- package P is
7998 -- type Pt is private;
8000 -- generic
8001 -- type Ft is array (Positive range <>) of Pt;
8002 -- package G is
8003 -- procedure Check (F1, F2 : Ft; Lt : Boolean);
8004 -- end G;
8006 -- private
8007 -- type Pt is new Boolean;
8008 -- end P;
8010 -- package body P is
8011 -- package body G is
8012 -- procedure Check (F1, F2 : Ft; Lt : Boolean) is
8013 -- begin
8014 -- if (F1 < F2) /= Lt then
8015 -- null;
8016 -- end if;
8017 -- end Check;
8018 -- end G;
8019 -- end P;
8021 -- type Arr is array (Positive range <>) of P.Pt;
8023 -- package Inst is new P.G (Arr);
8025 -- Pt is a global type for the generic package G and it
8026 -- is not referenced in its body, but only as component
8027 -- type of Ft, which is a local type. This means that no
8028 -- references to Pt or Ft are seen during the copy of the
8029 -- body, the only reference to Pt being seen is when the
8030 -- actuals are checked by Check_Generic_Actuals, but Pt
8031 -- is still private at this point. In the end, the views
8032 -- of Pt are not switched in the body and, therefore, the
8033 -- array comparison is rejected because the component is
8034 -- still private.
8036 -- Adding e.g. a dummy variable of type Pt in the body is
8037 -- sufficient to make everything work, so we generate an
8038 -- artificial reference to Pt on the fly and thus force
8039 -- the switching of views on the grounds that, if the
8040 -- comparison was accepted during the semantic analysis
8041 -- of the generic, this means that the component cannot
8042 -- have been private (see Sem_Type.Valid_Comparison_Arg).
8044 if Nkind (Assoc) in N_Op_Compare
8045 and then Present (Etype (Left_Opnd (Assoc)))
8046 and then Is_Array_Type (Etype (Left_Opnd (Assoc)))
8047 and then Present (Etype (Right_Opnd (Assoc)))
8048 and then Is_Array_Type (Etype (Right_Opnd (Assoc)))
8049 then
8050 declare
8051 Ltyp : constant Entity_Id :=
8052 Etype (Left_Opnd (Assoc));
8053 Rtyp : constant Entity_Id :=
8054 Etype (Right_Opnd (Assoc));
8055 begin
8056 if Is_Private_Type (Component_Type (Ltyp)) then
8057 Check_Private_View
8058 (New_Occurrence_Of (Component_Type (Ltyp),
8059 Sloc (N)));
8060 end if;
8061 if Is_Private_Type (Component_Type (Rtyp)) then
8062 Check_Private_View
8063 (New_Occurrence_Of (Component_Type (Rtyp),
8064 Sloc (N)));
8065 end if;
8066 end;
8068 -- Here is a similar case, for the Designated_Type of an
8069 -- access type that is present as target type in a type
8070 -- conversion from another access type. In this case, if
8071 -- the base types of the designated types are different
8072 -- and the conversion was accepted during the semantic
8073 -- analysis of the generic, this means that the target
8074 -- type cannot have been private (see Valid_Conversion).
8076 elsif Nkind (Assoc) = N_Identifier
8077 and then Nkind (Parent (Assoc)) = N_Type_Conversion
8078 and then Subtype_Mark (Parent (Assoc)) = Assoc
8079 and then Present (Etype (Assoc))
8080 and then Is_Access_Type (Etype (Assoc))
8081 and then Present (Etype (Expression (Parent (Assoc))))
8082 and then
8083 Is_Access_Type (Etype (Expression (Parent (Assoc))))
8084 then
8085 declare
8086 Targ_Desig : constant Entity_Id :=
8087 Designated_Type (Etype (Assoc));
8088 Expr_Desig : constant Entity_Id :=
8089 Designated_Type
8090 (Etype (Expression (Parent (Assoc))));
8091 begin
8092 if Base_Type (Targ_Desig) /= Base_Type (Expr_Desig)
8093 and then Is_Private_Type (Targ_Desig)
8094 then
8095 Check_Private_View
8096 (New_Occurrence_Of (Targ_Desig, Sloc (N)));
8097 end if;
8098 end;
8099 end if;
8101 -- The node is a reference to a global type and acts as the
8102 -- subtype mark of a qualified expression created in order
8103 -- to aid resolution of accidental overloading in instances.
8104 -- Since N is a reference to a type, the Associated_Node of
8105 -- N denotes an entity rather than another identifier. See
8106 -- Qualify_Universal_Operands for details.
8108 elsif Nkind (N) = N_Identifier
8109 and then Nkind (Parent (N)) = N_Qualified_Expression
8110 and then Subtype_Mark (Parent (N)) = N
8111 and then Is_Qualified_Universal_Literal (Parent (N))
8112 then
8113 Set_Entity (New_N, Assoc);
8115 -- The name in the call may be a selected component if the
8116 -- call has not been analyzed yet, as may be the case for
8117 -- pre/post conditions in a generic unit.
8119 elsif Nkind (Assoc) = N_Function_Call
8120 and then Is_Entity_Name (Name (Assoc))
8121 then
8122 Set_Entity (New_N, Entity (Name (Assoc)));
8124 elsif Nkind (Assoc) in N_Entity
8125 and then Expander_Active
8126 then
8127 -- Inlining case: we are copying a tree that contains
8128 -- global entities, which are preserved in the copy to be
8129 -- used for subsequent inlining.
8131 null;
8133 else
8134 Set_Entity (New_N, Empty);
8135 end if;
8136 end if;
8137 end;
8138 end if;
8140 -- For expanded name, we must copy the Prefix and Selector_Name
8142 if Nkind (N) = N_Expanded_Name then
8143 Set_Prefix
8144 (New_N, Copy_Generic_Node (Prefix (N), New_N, Instantiating));
8146 Set_Selector_Name (New_N,
8147 Copy_Generic_Node (Selector_Name (N), New_N, Instantiating));
8149 -- For operators, copy the operands
8151 elsif Nkind (N) in N_Op then
8152 if Nkind (N) in N_Binary_Op then
8153 Set_Left_Opnd (New_N,
8154 Copy_Generic_Node (Left_Opnd (N), New_N, Instantiating));
8155 end if;
8157 Set_Right_Opnd (New_N,
8158 Copy_Generic_Node (Right_Opnd (N), New_N, Instantiating));
8159 end if;
8161 -- Establish a link between an entity from the generic template and the
8162 -- corresponding entity in the generic copy to be analyzed.
8164 elsif Nkind (N) in N_Entity then
8165 if not Instantiating then
8166 Set_Associated_Entity (N, New_N);
8167 end if;
8169 -- Clear any existing link the copy may inherit from the replicated
8170 -- generic template entity.
8172 Set_Associated_Entity (New_N, Empty);
8174 -- Special casing for stubs
8176 elsif Nkind (N) in N_Body_Stub then
8178 -- In any case, we must copy the specification or defining
8179 -- identifier as appropriate.
8181 if Nkind (N) = N_Subprogram_Body_Stub then
8182 Set_Specification (New_N,
8183 Copy_Generic_Node (Specification (N), New_N, Instantiating));
8185 else
8186 Set_Defining_Identifier (New_N,
8187 Copy_Generic_Node
8188 (Defining_Identifier (N), New_N, Instantiating));
8189 end if;
8191 -- If we are not instantiating, then this is where we load and
8192 -- analyze subunits, i.e. at the point where the stub occurs. A
8193 -- more permissive system might defer this analysis to the point
8194 -- of instantiation, but this seems too complicated for now.
8196 if not Instantiating then
8197 declare
8198 Subunit_Name : constant Unit_Name_Type := Get_Unit_Name (N);
8199 Subunit : Node_Id;
8200 Unum : Unit_Number_Type;
8201 New_Body : Node_Id;
8203 begin
8204 -- Make sure that, if it is a subunit of the main unit that is
8205 -- preprocessed and if -gnateG is specified, the preprocessed
8206 -- file will be written.
8208 Lib.Analysing_Subunit_Of_Main :=
8209 Lib.In_Extended_Main_Source_Unit (N);
8210 Unum :=
8211 Load_Unit
8212 (Load_Name => Subunit_Name,
8213 Required => False,
8214 Subunit => True,
8215 Error_Node => N);
8216 Lib.Analysing_Subunit_Of_Main := False;
8218 -- If the proper body is not found, a warning message will be
8219 -- emitted when analyzing the stub, or later at the point of
8220 -- instantiation. Here we just leave the stub as is.
8222 if Unum = No_Unit then
8223 Subunits_Missing := True;
8224 goto Subunit_Not_Found;
8225 end if;
8227 Subunit := Cunit (Unum);
8229 if Nkind (Unit (Subunit)) /= N_Subunit then
8230 Error_Msg_N
8231 ("found child unit instead of expected SEPARATE subunit",
8232 Subunit);
8233 Error_Msg_Sloc := Sloc (N);
8234 Error_Msg_N ("\to complete stub #", Subunit);
8235 goto Subunit_Not_Found;
8236 end if;
8238 -- We must create a generic copy of the subunit, in order to
8239 -- perform semantic analysis on it, and we must replace the
8240 -- stub in the original generic unit with the subunit, in order
8241 -- to preserve non-local references within.
8243 -- Only the proper body needs to be copied. Library_Unit and
8244 -- context clause are simply inherited by the generic copy.
8245 -- Note that the copy (which may be recursive if there are
8246 -- nested subunits) must be done first, before attaching it to
8247 -- the enclosing generic.
8249 New_Body :=
8250 Copy_Generic_Node
8251 (Proper_Body (Unit (Subunit)),
8252 Empty, Instantiating => False);
8254 -- Now place the original proper body in the original generic
8255 -- unit. This is a body, not a compilation unit.
8257 Rewrite (N, Proper_Body (Unit (Subunit)));
8258 Set_Is_Compilation_Unit (Defining_Entity (N), False);
8259 Set_Was_Originally_Stub (N);
8261 -- Finally replace the body of the subunit with its copy, and
8262 -- make this new subunit into the library unit of the generic
8263 -- copy, which does not have stubs any longer.
8265 Set_Proper_Body (Unit (Subunit), New_Body);
8266 Set_Library_Unit (New_N, Subunit);
8267 Inherit_Context (Unit (Subunit), N);
8268 end;
8270 -- If we are instantiating, this must be an error case, since
8271 -- otherwise we would have replaced the stub node by the proper body
8272 -- that corresponds. So just ignore it in the copy (i.e. we have
8273 -- copied it, and that is good enough).
8275 else
8276 null;
8277 end if;
8279 <<Subunit_Not_Found>> null;
8281 -- If the node is a compilation unit, it is the subunit of a stub, which
8282 -- has been loaded already (see code below). In this case, the library
8283 -- unit field of N points to the parent unit (which is a compilation
8284 -- unit) and need not (and cannot) be copied.
8286 -- When the proper body of the stub is analyzed, the library_unit link
8287 -- is used to establish the proper context (see sem_ch10).
8289 -- The other fields of a compilation unit are copied as usual
8291 elsif Nkind (N) = N_Compilation_Unit then
8293 -- This code can only be executed when not instantiating, because in
8294 -- the copy made for an instantiation, the compilation unit node has
8295 -- disappeared at the point that a stub is replaced by its proper
8296 -- body.
8298 pragma Assert (not Instantiating);
8300 Set_Context_Items (New_N,
8301 Copy_Generic_List (Context_Items (N), New_N));
8303 Set_Unit (New_N,
8304 Copy_Generic_Node (Unit (N), New_N, Instantiating => False));
8306 Set_First_Inlined_Subprogram (New_N,
8307 Copy_Generic_Node
8308 (First_Inlined_Subprogram (N), New_N, Instantiating => False));
8310 Set_Aux_Decls_Node
8311 (New_N,
8312 Copy_Generic_Node
8313 (Aux_Decls_Node (N), New_N, Instantiating => False));
8315 -- For an assignment node, the assignment is known to be semantically
8316 -- legal if we are instantiating the template. This avoids incorrect
8317 -- diagnostics in generated code.
8319 elsif Nkind (N) = N_Assignment_Statement then
8321 -- Copy name and expression fields in usual manner
8323 Set_Name (New_N,
8324 Copy_Generic_Node (Name (N), New_N, Instantiating));
8326 Set_Expression (New_N,
8327 Copy_Generic_Node (Expression (N), New_N, Instantiating));
8329 if Instantiating then
8330 Set_Assignment_OK (Name (New_N), True);
8331 end if;
8333 elsif Nkind (N) in N_Aggregate | N_Extension_Aggregate then
8334 if not Instantiating then
8335 Set_Associated_Node (N, New_N);
8337 else
8338 if Present (Get_Associated_Node (N))
8339 and then Nkind (Get_Associated_Node (N)) = Nkind (N)
8340 then
8341 -- In the generic the aggregate has some composite type. If at
8342 -- the point of instantiation the type has a private view,
8343 -- install the full view (and that of its ancestors, if any).
8345 declare
8346 T : Entity_Id := (Etype (Get_Associated_Node (New_N)));
8347 Rt : Entity_Id;
8349 begin
8350 if Present (T) and then Is_Private_Type (T) then
8351 Switch_View (T);
8352 end if;
8354 if Present (T)
8355 and then Is_Tagged_Type (T)
8356 and then Is_Derived_Type (T)
8357 then
8358 Rt := Root_Type (T);
8360 loop
8361 T := Etype (T);
8363 if Is_Private_Type (T) then
8364 Switch_View (T);
8365 end if;
8367 exit when T = Rt;
8368 end loop;
8369 end if;
8370 end;
8371 end if;
8372 end if;
8374 -- Do not copy the associated node, which points to the generic copy
8375 -- of the aggregate.
8377 if Nkind (N) = N_Aggregate then
8378 Set_Aggregate_Bounds
8379 (New_N,
8380 Node_Id (Copy_Generic_Descendant
8381 (Union_Id (Aggregate_Bounds (N)))));
8383 elsif Nkind (N) = N_Extension_Aggregate then
8384 Set_Ancestor_Part
8385 (New_N,
8386 Node_Id (Copy_Generic_Descendant
8387 (Union_Id (Ancestor_Part (N)))));
8389 else
8390 pragma Assert (False);
8391 end if;
8393 Set_Expressions
8394 (New_N,
8395 List_Id (Copy_Generic_Descendant (Union_Id (Expressions (N)))));
8396 Set_Component_Associations
8397 (New_N,
8398 List_Id (Copy_Generic_Descendant
8399 (Union_Id (Component_Associations (N)))));
8400 Set_Etype
8401 (New_N, Node_Id (Copy_Generic_Descendant (Union_Id (Etype (N)))));
8403 -- Allocators do not have an identifier denoting the access type, so we
8404 -- must locate it through the expression to check whether the views are
8405 -- consistent.
8407 elsif Nkind (N) = N_Allocator
8408 and then Nkind (Expression (N)) = N_Qualified_Expression
8409 and then Is_Entity_Name (Subtype_Mark (Expression (N)))
8410 and then Instantiating
8411 then
8412 declare
8413 T : constant Node_Id :=
8414 Get_Associated_Node (Subtype_Mark (Expression (N)));
8415 Acc_T : Entity_Id;
8417 begin
8418 if Present (T) then
8420 -- Retrieve the allocator node in the generic copy
8422 Acc_T := Etype (Parent (Parent (T)));
8424 if Present (Acc_T) and then Is_Private_Type (Acc_T) then
8425 Switch_View (Acc_T);
8426 end if;
8427 end if;
8429 Copy_Descendants;
8430 end;
8432 -- For a proper body, we must catch the case of a proper body that
8433 -- replaces a stub. This represents the point at which a separate
8434 -- compilation unit, and hence template file, may be referenced, so we
8435 -- must make a new source instantiation entry for the template of the
8436 -- subunit, and ensure that all nodes in the subunit are adjusted using
8437 -- this new source instantiation entry.
8439 elsif Nkind (N) in N_Proper_Body then
8440 declare
8441 Save_Adjustment : constant Sloc_Adjustment := S_Adjustment;
8442 begin
8443 if Instantiating and then Was_Originally_Stub (N) then
8444 Create_Instantiation_Source
8445 (Instantiation_Node,
8446 Defining_Entity (N),
8447 S_Adjustment);
8449 Adjust_Instantiation_Sloc (New_N, S_Adjustment);
8450 end if;
8452 -- Now copy the fields of the proper body, using the new
8453 -- adjustment factor if one was needed as per test above.
8455 Copy_Descendants;
8457 -- Restore the original adjustment factor
8459 S_Adjustment := Save_Adjustment;
8460 end;
8462 elsif Nkind (N) = N_Pragma and then Instantiating then
8464 -- Do not copy Comment or Ident pragmas their content is relevant to
8465 -- the generic unit, not to the instantiating unit.
8467 if Pragma_Name_Unmapped (N) in Name_Comment | Name_Ident then
8468 New_N := Make_Null_Statement (Sloc (N));
8470 -- Do not copy pragmas generated from aspects because the pragmas do
8471 -- not carry any semantic information, plus they will be regenerated
8472 -- in the instance.
8474 -- However, generating C we need to copy them since postconditions
8475 -- are inlined by the front end, and the front-end inlining machinery
8476 -- relies on this routine to perform inlining.
8478 elsif From_Aspect_Specification (N)
8479 and then not Modify_Tree_For_C
8480 then
8481 New_N := Make_Null_Statement (Sloc (N));
8483 else
8484 Copy_Descendants;
8485 end if;
8487 elsif Nkind (N) in N_Integer_Literal | N_Real_Literal then
8489 -- No descendant fields need traversing
8491 null;
8493 elsif Nkind (N) = N_String_Literal
8494 and then Present (Etype (N))
8495 and then Instantiating
8496 then
8497 -- If the string is declared in an outer scope, the string_literal
8498 -- subtype created for it may have the wrong scope. Force reanalysis
8499 -- of the constant to generate a new itype in the proper context.
8501 Set_Etype (New_N, Empty);
8502 Set_Analyzed (New_N, False);
8504 -- For the remaining nodes, copy their descendants recursively
8506 else
8507 Copy_Descendants;
8509 if Instantiating and then Nkind (N) = N_Subprogram_Body then
8510 Set_Generic_Parent (Specification (New_N), N);
8512 -- Should preserve Corresponding_Spec??? (12.3(14))
8513 end if;
8514 end if;
8516 -- Propagate dimensions if present, so that they are reflected in the
8517 -- instance.
8519 if Nkind (N) in N_Has_Etype
8520 and then (Nkind (N) in N_Op or else Is_Entity_Name (N))
8521 and then Present (Etype (N))
8522 and then Is_Floating_Point_Type (Etype (N))
8523 and then Has_Dimension_System (Etype (N))
8524 then
8525 Copy_Dimensions (N, New_N);
8526 end if;
8528 return New_N;
8529 end Copy_Generic_Node;
8531 ----------------------------
8532 -- Denotes_Formal_Package --
8533 ----------------------------
8535 function Denotes_Formal_Package
8536 (Pack : Entity_Id;
8537 On_Exit : Boolean := False;
8538 Instance : Entity_Id := Empty) return Boolean
8540 Par : Entity_Id;
8541 Scop : constant Entity_Id := Scope (Pack);
8542 E : Entity_Id;
8544 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean;
8545 -- The package in question may be an actual for a previous formal
8546 -- package P of the current instance, so examine its actuals as well.
8547 -- This must be recursive over other formal packages.
8549 ----------------------------------
8550 -- Is_Actual_Of_Previous_Formal --
8551 ----------------------------------
8553 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean is
8554 E1 : Entity_Id;
8556 begin
8557 E1 := First_Entity (P);
8558 while Present (E1) and then E1 /= Instance loop
8559 if Ekind (E1) = E_Package
8560 and then Nkind (Parent (E1)) = N_Package_Renaming_Declaration
8561 then
8562 if Renamed_Entity (E1) = Pack then
8563 return True;
8565 elsif E1 = P or else Renamed_Entity (E1) = P then
8566 return False;
8568 elsif Is_Actual_Of_Previous_Formal (E1) then
8569 return True;
8570 end if;
8571 end if;
8573 Next_Entity (E1);
8574 end loop;
8576 return False;
8577 end Is_Actual_Of_Previous_Formal;
8579 -- Start of processing for Denotes_Formal_Package
8581 begin
8582 if On_Exit then
8583 Par :=
8584 Instance_Envs.Table
8585 (Instance_Envs.Last).Instantiated_Parent.Act_Id;
8586 else
8587 Par := Current_Instantiated_Parent.Act_Id;
8588 end if;
8590 if Ekind (Scop) = E_Generic_Package
8591 or else Nkind (Unit_Declaration_Node (Scop)) =
8592 N_Generic_Subprogram_Declaration
8593 then
8594 return True;
8596 elsif Nkind (Original_Node (Unit_Declaration_Node (Pack))) =
8597 N_Formal_Package_Declaration
8598 then
8599 return True;
8601 elsif No (Par) then
8602 return False;
8604 else
8605 -- Check whether this package is associated with a formal package of
8606 -- the enclosing instantiation. Iterate over the list of renamings.
8608 E := First_Entity (Par);
8609 while Present (E) loop
8610 if Ekind (E) /= E_Package
8611 or else Nkind (Parent (E)) /= N_Package_Renaming_Declaration
8612 then
8613 null;
8615 elsif Renamed_Entity (E) = Par then
8616 return False;
8618 elsif Renamed_Entity (E) = Pack then
8619 return True;
8621 elsif Is_Actual_Of_Previous_Formal (E) then
8622 return True;
8624 end if;
8626 Next_Entity (E);
8627 end loop;
8629 return False;
8630 end if;
8631 end Denotes_Formal_Package;
8633 -----------------
8634 -- End_Generic --
8635 -----------------
8637 procedure End_Generic is
8638 begin
8639 -- ??? More things could be factored out in this routine. Should
8640 -- probably be done at a later stage.
8642 Inside_A_Generic := Generic_Flags.Table (Generic_Flags.Last);
8643 Generic_Flags.Decrement_Last;
8645 Expander_Mode_Restore;
8646 end End_Generic;
8648 -------------
8649 -- Earlier --
8650 -------------
8652 function Earlier (N1, N2 : Node_Id) return Boolean is
8653 procedure Find_Depth (P : in out Node_Id; D : in out Integer);
8654 -- Find distance from given node to enclosing compilation unit
8656 ----------------
8657 -- Find_Depth --
8658 ----------------
8660 procedure Find_Depth (P : in out Node_Id; D : in out Integer) is
8661 begin
8662 while Present (P)
8663 and then Nkind (P) /= N_Compilation_Unit
8664 loop
8665 P := True_Parent (P);
8666 D := D + 1;
8667 end loop;
8668 end Find_Depth;
8670 -- Local declarations
8672 D1 : Integer := 0;
8673 D2 : Integer := 0;
8674 P1 : Node_Id := N1;
8675 P2 : Node_Id := N2;
8676 T1 : Source_Ptr;
8677 T2 : Source_Ptr;
8679 -- Start of processing for Earlier
8681 begin
8682 Find_Depth (P1, D1);
8683 Find_Depth (P2, D2);
8685 if P1 /= P2 then
8686 return False;
8687 else
8688 P1 := N1;
8689 P2 := N2;
8690 end if;
8692 while D1 > D2 loop
8693 P1 := True_Parent (P1);
8694 D1 := D1 - 1;
8695 end loop;
8697 while D2 > D1 loop
8698 P2 := True_Parent (P2);
8699 D2 := D2 - 1;
8700 end loop;
8702 -- At this point P1 and P2 are at the same distance from the root.
8703 -- We examine their parents until we find a common declarative list.
8704 -- If we reach the root, N1 and N2 do not descend from the same
8705 -- declarative list (e.g. one is nested in the declarative part and
8706 -- the other is in a block in the statement part) and the earlier
8707 -- one is already frozen.
8709 while not Is_List_Member (P1)
8710 or else not Is_List_Member (P2)
8711 or else not In_Same_List (P1, P2)
8712 loop
8713 P1 := True_Parent (P1);
8714 P2 := True_Parent (P2);
8716 if Nkind (Parent (P1)) = N_Subunit then
8717 P1 := Corresponding_Stub (Parent (P1));
8718 end if;
8720 if Nkind (Parent (P2)) = N_Subunit then
8721 P2 := Corresponding_Stub (Parent (P2));
8722 end if;
8724 if P1 = P2 then
8725 return False;
8726 end if;
8727 end loop;
8729 -- Expanded code usually shares the source location of the original
8730 -- construct it was generated for. This however may not necessarily
8731 -- reflect the true location of the code within the tree.
8733 -- Before comparing the slocs of the two nodes, make sure that we are
8734 -- working with correct source locations. Assume that P1 is to the left
8735 -- of P2. If either one does not come from source, traverse the common
8736 -- list heading towards the other node and locate the first source
8737 -- statement.
8739 -- P1 P2
8740 -- ----+===+===+--------------+===+===+----
8741 -- expanded code expanded code
8743 if not Comes_From_Source (P1) then
8744 while Present (P1) loop
8746 -- Neither P2 nor a source statement were located during the
8747 -- search. If we reach the end of the list, then P1 does not
8748 -- occur earlier than P2.
8750 -- ---->
8751 -- start --- P2 ----- P1 --- end
8753 if No (Next (P1)) then
8754 return False;
8756 -- We encounter P2 while going to the right of the list. This
8757 -- means that P1 does indeed appear earlier.
8759 -- ---->
8760 -- start --- P1 ===== P2 --- end
8761 -- expanded code in between
8763 elsif P1 = P2 then
8764 return True;
8766 -- No need to look any further since we have located a source
8767 -- statement.
8769 elsif Comes_From_Source (P1) then
8770 exit;
8771 end if;
8773 -- Keep going right
8775 Next (P1);
8776 end loop;
8777 end if;
8779 if not Comes_From_Source (P2) then
8780 while Present (P2) loop
8782 -- Neither P1 nor a source statement were located during the
8783 -- search. If we reach the start of the list, then P1 does not
8784 -- occur earlier than P2.
8786 -- <----
8787 -- start --- P2 --- P1 --- end
8789 if No (Prev (P2)) then
8790 return False;
8792 -- We encounter P1 while going to the left of the list. This
8793 -- means that P1 does indeed appear earlier.
8795 -- <----
8796 -- start --- P1 ===== P2 --- end
8797 -- expanded code in between
8799 elsif P2 = P1 then
8800 return True;
8802 -- No need to look any further since we have located a source
8803 -- statement.
8805 elsif Comes_From_Source (P2) then
8806 exit;
8807 end if;
8809 -- Keep going left
8811 Prev (P2);
8812 end loop;
8813 end if;
8815 -- At this point either both nodes came from source or we approximated
8816 -- their source locations through neighboring source statements.
8818 T1 := Top_Level_Location (Sloc (P1));
8819 T2 := Top_Level_Location (Sloc (P2));
8821 -- When two nodes come from the same instance, they have identical top
8822 -- level locations. To determine proper relation within the tree, check
8823 -- their locations within the template.
8825 if T1 = T2 then
8826 return Sloc (P1) < Sloc (P2);
8828 -- The two nodes either come from unrelated instances or do not come
8829 -- from instantiated code at all.
8831 else
8832 return T1 < T2;
8833 end if;
8834 end Earlier;
8836 ----------------------
8837 -- Find_Actual_Type --
8838 ----------------------
8840 function Find_Actual_Type
8841 (Typ : Entity_Id;
8842 Gen_Type : Entity_Id) return Entity_Id
8844 Gen_Scope : constant Entity_Id := Scope (Gen_Type);
8845 T : Entity_Id;
8847 begin
8848 -- Special processing only applies to child units
8850 if not Is_Child_Unit (Gen_Scope) then
8851 return Get_Instance_Of (Typ);
8853 -- If designated or component type is itself a formal of the child unit,
8854 -- its instance is available.
8856 elsif Scope (Typ) = Gen_Scope then
8857 return Get_Instance_Of (Typ);
8859 -- If the array or access type is not declared in the parent unit,
8860 -- no special processing needed.
8862 elsif not Is_Generic_Type (Typ)
8863 and then Scope (Gen_Scope) /= Scope (Typ)
8864 then
8865 return Get_Instance_Of (Typ);
8867 -- Otherwise, retrieve designated or component type by visibility
8869 else
8870 T := Current_Entity (Typ);
8871 while Present (T) loop
8872 if In_Open_Scopes (Scope (T)) then
8873 return T;
8874 elsif Is_Generic_Actual_Type (T) then
8875 return T;
8876 end if;
8878 T := Homonym (T);
8879 end loop;
8881 return Typ;
8882 end if;
8883 end Find_Actual_Type;
8885 -----------------------------
8886 -- Freeze_Package_Instance --
8887 -----------------------------
8889 procedure Freeze_Package_Instance
8890 (N : Node_Id;
8891 Gen_Body : Node_Id;
8892 Gen_Decl : Node_Id;
8893 Act_Id : Entity_Id)
8895 function In_Same_Scope (Gen_Id, Act_Id : Node_Id) return Boolean;
8896 -- Check if the generic definition and the instantiation come from
8897 -- a common scope, in which case the instance must be frozen after
8898 -- the generic body.
8900 function True_Sloc (N, Act_Unit : Node_Id) return Source_Ptr;
8901 -- If the instance is nested inside a generic unit, the Sloc of the
8902 -- instance indicates the place of the original definition, not the
8903 -- point of the current enclosing instance. Pending a better usage of
8904 -- Slocs to indicate instantiation places, we determine the place of
8905 -- origin of a node by finding the maximum sloc of any ancestor node.
8907 -- Why is this not equivalent to Top_Level_Location ???
8909 -------------------
8910 -- In_Same_Scope --
8911 -------------------
8913 function In_Same_Scope (Gen_Id, Act_Id : Node_Id) return Boolean is
8914 Act_Scop : Entity_Id := Scope (Act_Id);
8915 Gen_Scop : Entity_Id := Scope (Gen_Id);
8917 begin
8918 while Act_Scop /= Standard_Standard
8919 and then Gen_Scop /= Standard_Standard
8920 loop
8921 if Act_Scop = Gen_Scop then
8922 return True;
8923 end if;
8925 Act_Scop := Scope (Act_Scop);
8926 Gen_Scop := Scope (Gen_Scop);
8927 end loop;
8929 return False;
8930 end In_Same_Scope;
8932 ---------------
8933 -- True_Sloc --
8934 ---------------
8936 function True_Sloc (N, Act_Unit : Node_Id) return Source_Ptr is
8937 N1 : Node_Id;
8938 Res : Source_Ptr;
8940 begin
8941 Res := Sloc (N);
8942 N1 := N;
8943 while Present (N1) and then N1 /= Act_Unit loop
8944 if Sloc (N1) > Res then
8945 Res := Sloc (N1);
8946 end if;
8948 N1 := Parent (N1);
8949 end loop;
8951 return Res;
8952 end True_Sloc;
8954 -- Local variables
8956 Gen_Id : constant Entity_Id := Get_Generic_Entity (N);
8957 Par_Id : constant Entity_Id := Scope (Gen_Id);
8958 Act_Unit : constant Node_Id := Unit (Cunit (Get_Source_Unit (N)));
8959 Gen_Unit : constant Node_Id :=
8960 Unit (Cunit (Get_Source_Unit (Gen_Decl)));
8962 Body_Unit : Node_Id;
8963 F_Node : Node_Id;
8964 Must_Delay : Boolean;
8965 Orig_Body : Node_Id;
8967 -- Start of processing for Freeze_Package_Instance
8969 begin
8970 -- If the body is a subunit, the freeze point is the corresponding stub
8971 -- in the current compilation, not the subunit itself.
8973 if Nkind (Parent (Gen_Body)) = N_Subunit then
8974 Orig_Body := Corresponding_Stub (Parent (Gen_Body));
8975 else
8976 Orig_Body := Gen_Body;
8977 end if;
8979 Body_Unit := Unit (Cunit (Get_Source_Unit (Orig_Body)));
8981 -- If the instantiation and the generic definition appear in the same
8982 -- package declaration, this is an early instantiation. If they appear
8983 -- in the same declarative part, it is an early instantiation only if
8984 -- the generic body appears textually later, and the generic body is
8985 -- also in the main unit.
8987 -- If instance is nested within a subprogram, and the generic body
8988 -- is not, the instance is delayed because the enclosing body is. If
8989 -- instance and body are within the same scope, or the same subprogram
8990 -- body, indicate explicitly that the instance is delayed.
8992 Must_Delay :=
8993 (Gen_Unit = Act_Unit
8994 and then (Nkind (Gen_Unit) in N_Generic_Package_Declaration
8995 | N_Package_Declaration
8996 or else (Gen_Unit = Body_Unit
8997 and then
8998 True_Sloc (N, Act_Unit) < Sloc (Orig_Body)))
8999 and then Is_In_Main_Unit (Original_Node (Gen_Unit))
9000 and then In_Same_Scope (Gen_Id, Act_Id));
9002 -- If this is an early instantiation, the freeze node is placed after
9003 -- the generic body. Otherwise, if the generic appears in an instance,
9004 -- we cannot freeze the current instance until the outer one is frozen.
9005 -- This is only relevant if the current instance is nested within some
9006 -- inner scope not itself within the outer instance. If this scope is
9007 -- a package body in the same declarative part as the outer instance,
9008 -- then that body needs to be frozen after the outer instance. Finally,
9009 -- if no delay is needed, we place the freeze node at the end of the
9010 -- current declarative part.
9012 if No (Freeze_Node (Act_Id))
9013 or else not Is_List_Member (Freeze_Node (Act_Id))
9014 then
9015 Ensure_Freeze_Node (Act_Id);
9016 F_Node := Freeze_Node (Act_Id);
9018 if Must_Delay then
9019 Insert_After (Orig_Body, F_Node);
9021 elsif Is_Generic_Instance (Par_Id)
9022 and then Present (Freeze_Node (Par_Id))
9023 and then Scope (Act_Id) /= Par_Id
9024 then
9025 -- Freeze instance of inner generic after instance of enclosing
9026 -- generic.
9028 if In_Same_Declarative_Part (Parent (Freeze_Node (Par_Id)), N) then
9030 -- Handle the following case:
9032 -- package Parent_Inst is new ...
9033 -- freeze Parent_Inst []
9035 -- procedure P ... -- this body freezes Parent_Inst
9037 -- package Inst is new ...
9039 -- In this particular scenario, the freeze node for Inst must
9040 -- be inserted in the same manner as that of Parent_Inst,
9041 -- before the next source body or at the end of the declarative
9042 -- list (body not available). If body P did not exist and
9043 -- Parent_Inst was frozen after Inst, either by a body
9044 -- following Inst or at the end of the declarative region,
9045 -- the freeze node for Inst must be inserted after that of
9046 -- Parent_Inst. This relation is established by comparing
9047 -- the Slocs of Parent_Inst freeze node and Inst.
9048 -- We examine the parents of the enclosing lists to handle
9049 -- the case where the parent instance is in the visible part
9050 -- of a package declaration, and the inner instance is in
9051 -- the corresponding private part.
9053 if Parent (List_Containing (Freeze_Node (Par_Id)))
9054 = Parent (List_Containing (N))
9055 and then Sloc (Freeze_Node (Par_Id)) <= Sloc (N)
9056 then
9057 Insert_Freeze_Node_For_Instance (N, F_Node);
9058 else
9059 Insert_After (Freeze_Node (Par_Id), F_Node);
9060 end if;
9062 -- Freeze package enclosing instance of inner generic after
9063 -- instance of enclosing generic.
9065 elsif Nkind (Parent (N)) in N_Package_Body | N_Subprogram_Body
9066 and then In_Same_Declarative_Part
9067 (Parent (Freeze_Node (Par_Id)), Parent (N))
9068 then
9069 declare
9070 Enclosing : Entity_Id;
9072 begin
9073 Enclosing := Corresponding_Spec (Parent (N));
9075 if No (Enclosing) then
9076 Enclosing := Defining_Entity (Parent (N));
9077 end if;
9079 Insert_Freeze_Node_For_Instance (N, F_Node);
9080 Ensure_Freeze_Node (Enclosing);
9082 if not Is_List_Member (Freeze_Node (Enclosing)) then
9084 -- The enclosing context is a subunit, insert the freeze
9085 -- node after the stub.
9087 if Nkind (Parent (Parent (N))) = N_Subunit then
9088 Insert_Freeze_Node_For_Instance
9089 (Corresponding_Stub (Parent (Parent (N))),
9090 Freeze_Node (Enclosing));
9092 -- The enclosing context is a package with a stub body
9093 -- which has already been replaced by the real body.
9094 -- Insert the freeze node after the actual body.
9096 elsif Ekind (Enclosing) = E_Package
9097 and then Present (Body_Entity (Enclosing))
9098 and then Was_Originally_Stub
9099 (Parent (Body_Entity (Enclosing)))
9100 then
9101 Insert_Freeze_Node_For_Instance
9102 (Parent (Body_Entity (Enclosing)),
9103 Freeze_Node (Enclosing));
9105 -- The parent instance has been frozen before the body of
9106 -- the enclosing package, insert the freeze node after
9107 -- the body.
9109 elsif In_Same_List (Freeze_Node (Par_Id), Parent (N))
9110 and then
9111 Sloc (Freeze_Node (Par_Id)) <= Sloc (Parent (N))
9112 then
9113 Insert_Freeze_Node_For_Instance
9114 (Parent (N), Freeze_Node (Enclosing));
9116 else
9117 Insert_After
9118 (Freeze_Node (Par_Id), Freeze_Node (Enclosing));
9119 end if;
9120 end if;
9121 end;
9123 else
9124 Insert_Freeze_Node_For_Instance (N, F_Node);
9125 end if;
9127 else
9128 Insert_Freeze_Node_For_Instance (N, F_Node);
9129 end if;
9130 end if;
9131 end Freeze_Package_Instance;
9133 --------------------------------
9134 -- Freeze_Subprogram_Instance --
9135 --------------------------------
9137 procedure Freeze_Subprogram_Instance
9138 (N : Node_Id;
9139 Gen_Body : Node_Id;
9140 Pack_Id : Entity_Id)
9142 function Enclosing_Package_Body (N : Node_Id) return Node_Id;
9143 -- Find innermost package body that encloses the given node, and which
9144 -- is not a compilation unit. Freeze nodes for the instance, or for its
9145 -- enclosing body, may be inserted after the enclosing_body of the
9146 -- generic unit. Used to determine proper placement of freeze node for
9147 -- both package and subprogram instances.
9149 function Package_Freeze_Node (B : Node_Id) return Node_Id;
9150 -- Find entity for given package body, and locate or create a freeze
9151 -- node for it.
9153 ----------------------------
9154 -- Enclosing_Package_Body --
9155 ----------------------------
9157 function Enclosing_Package_Body (N : Node_Id) return Node_Id is
9158 P : Node_Id;
9160 begin
9161 P := Parent (N);
9162 while Present (P)
9163 and then Nkind (Parent (P)) /= N_Compilation_Unit
9164 loop
9165 if Nkind (P) = N_Package_Body then
9166 if Nkind (Parent (P)) = N_Subunit then
9167 return Corresponding_Stub (Parent (P));
9168 else
9169 return P;
9170 end if;
9171 end if;
9173 P := True_Parent (P);
9174 end loop;
9176 return Empty;
9177 end Enclosing_Package_Body;
9179 -------------------------
9180 -- Package_Freeze_Node --
9181 -------------------------
9183 function Package_Freeze_Node (B : Node_Id) return Node_Id is
9184 Id : Entity_Id;
9186 begin
9187 if Nkind (B) = N_Package_Body then
9188 Id := Corresponding_Spec (B);
9189 else pragma Assert (Nkind (B) = N_Package_Body_Stub);
9190 Id := Corresponding_Spec (Proper_Body (Unit (Library_Unit (B))));
9191 end if;
9193 Ensure_Freeze_Node (Id);
9194 return Freeze_Node (Id);
9195 end Package_Freeze_Node;
9197 -- Local variables
9199 Enc_G : constant Node_Id := Enclosing_Package_Body (Gen_Body);
9200 Enc_N : constant Node_Id := Enclosing_Package_Body (N);
9201 Par_Id : constant Entity_Id := Scope (Get_Generic_Entity (N));
9203 Enc_G_F : Node_Id;
9204 F_Node : Node_Id;
9206 -- Start of processing for Freeze_Subprogram_Instance
9208 begin
9209 -- If the instance and the generic body appear within the same unit, and
9210 -- the instance precedes the generic, the freeze node for the instance
9211 -- must appear after that of the generic. If the generic is nested
9212 -- within another instance I2, then current instance must be frozen
9213 -- after I2. In both cases, the freeze nodes are those of enclosing
9214 -- packages. Otherwise, the freeze node is placed at the end of the
9215 -- current declarative part.
9217 Ensure_Freeze_Node (Pack_Id);
9218 F_Node := Freeze_Node (Pack_Id);
9220 if Is_Generic_Instance (Par_Id)
9221 and then Present (Freeze_Node (Par_Id))
9222 and then In_Same_Declarative_Part (Parent (Freeze_Node (Par_Id)), N)
9223 then
9224 -- The parent was a premature instantiation. Insert freeze node at
9225 -- the end the current declarative part.
9227 if Is_Known_Guaranteed_ABE (Get_Unit_Instantiation_Node (Par_Id)) then
9228 Insert_Freeze_Node_For_Instance (N, F_Node);
9230 -- Handle the following case:
9232 -- package Parent_Inst is new ...
9233 -- freeze Parent_Inst []
9235 -- procedure P ... -- this body freezes Parent_Inst
9237 -- procedure Inst is new ...
9239 -- In this particular scenario, the freeze node for Inst must be
9240 -- inserted in the same manner as that of Parent_Inst - before the
9241 -- next source body or at the end of the declarative list (body not
9242 -- available). If body P did not exist and Parent_Inst was frozen
9243 -- after Inst, either by a body following Inst or at the end of the
9244 -- declarative region, the freeze node for Inst must be inserted
9245 -- after that of Parent_Inst. This relation is established by
9246 -- comparing the Slocs of Parent_Inst freeze node and Inst.
9248 elsif In_Same_List (Freeze_Node (Par_Id), N)
9249 and then Sloc (Freeze_Node (Par_Id)) <= Sloc (N)
9250 then
9251 Insert_Freeze_Node_For_Instance (N, F_Node);
9253 else
9254 Insert_After (Freeze_Node (Par_Id), F_Node);
9255 end if;
9257 -- The body enclosing the instance should be frozen after the body that
9258 -- includes the generic, because the body of the instance may make
9259 -- references to entities therein. If the two are not in the same
9260 -- declarative part, or if the one enclosing the instance is frozen
9261 -- already, freeze the instance at the end of the current declarative
9262 -- part.
9264 elsif Is_Generic_Instance (Par_Id)
9265 and then Present (Freeze_Node (Par_Id))
9266 and then Present (Enc_N)
9267 then
9268 if In_Same_Declarative_Part (Parent (Freeze_Node (Par_Id)), Enc_N)
9269 then
9270 -- The enclosing package may contain several instances. Rather
9271 -- than computing the earliest point at which to insert its freeze
9272 -- node, we place it at the end of the declarative part of the
9273 -- parent of the generic.
9275 Insert_Freeze_Node_For_Instance
9276 (Freeze_Node (Par_Id), Package_Freeze_Node (Enc_N));
9277 end if;
9279 Insert_Freeze_Node_For_Instance (N, F_Node);
9281 elsif Present (Enc_G)
9282 and then Present (Enc_N)
9283 and then Enc_G /= Enc_N
9284 and then Earlier (N, Gen_Body)
9285 then
9286 -- Freeze package that encloses instance, and place node after the
9287 -- package that encloses generic. If enclosing package is already
9288 -- frozen we have to assume it is at the proper place. This may be a
9289 -- potential ABE that requires dynamic checking. Do not add a freeze
9290 -- node if the package that encloses the generic is inside the body
9291 -- that encloses the instance, because the freeze node would be in
9292 -- the wrong scope. Additional contortions needed if the bodies are
9293 -- within a subunit.
9295 declare
9296 Enclosing_Body : Node_Id;
9298 begin
9299 if Nkind (Enc_N) = N_Package_Body_Stub then
9300 Enclosing_Body := Proper_Body (Unit (Library_Unit (Enc_N)));
9301 else
9302 Enclosing_Body := Enc_N;
9303 end if;
9305 if Parent (List_Containing (Enc_G)) /= Enclosing_Body then
9306 Insert_Freeze_Node_For_Instance
9307 (Enc_G, Package_Freeze_Node (Enc_N));
9308 end if;
9309 end;
9311 -- Freeze enclosing subunit before instance
9313 Enc_G_F := Package_Freeze_Node (Enc_G);
9315 if not Is_List_Member (Enc_G_F) then
9316 Insert_After (Enc_G, Enc_G_F);
9317 end if;
9319 Insert_Freeze_Node_For_Instance (N, F_Node);
9321 else
9322 -- If none of the above, insert freeze node at the end of the current
9323 -- declarative part.
9325 Insert_Freeze_Node_For_Instance (N, F_Node);
9326 end if;
9327 end Freeze_Subprogram_Instance;
9329 ----------------
9330 -- Get_Gen_Id --
9331 ----------------
9333 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id is
9334 begin
9335 return Generic_Renamings.Table (E).Gen_Id;
9336 end Get_Gen_Id;
9338 ---------------------
9339 -- Get_Instance_Of --
9340 ---------------------
9342 function Get_Instance_Of (A : Entity_Id) return Entity_Id is
9343 Res : constant Assoc_Ptr := Generic_Renamings_HTable.Get (A);
9345 begin
9346 if Res /= Assoc_Null then
9347 return Generic_Renamings.Table (Res).Act_Id;
9349 else
9350 -- On exit, entity is not instantiated: not a generic parameter, or
9351 -- else parameter of an inner generic unit.
9353 return A;
9354 end if;
9355 end Get_Instance_Of;
9357 ---------------------------------
9358 -- Get_Unit_Instantiation_Node --
9359 ---------------------------------
9361 function Get_Unit_Instantiation_Node (A : Entity_Id) return Node_Id is
9362 Decl : Node_Id := Unit_Declaration_Node (A);
9363 Inst : Node_Id;
9365 begin
9366 -- If the Package_Instantiation attribute has been set on the package
9367 -- entity, then use it directly when it (or its Original_Node) refers
9368 -- to an N_Package_Instantiation node. In principle it should be
9369 -- possible to have this field set in all cases, which should be
9370 -- investigated, and would allow this function to be significantly
9371 -- simplified. ???
9373 Inst := Package_Instantiation (A);
9375 if Present (Inst) then
9376 if Nkind (Inst) = N_Package_Instantiation then
9377 return Inst;
9379 elsif Nkind (Original_Node (Inst)) = N_Package_Instantiation then
9380 return Original_Node (Inst);
9381 end if;
9382 end if;
9384 -- If the instantiation is a compilation unit that does not need body
9385 -- then the instantiation node has been rewritten as a package
9386 -- declaration for the instance, and we return the original node.
9388 -- If it is a compilation unit and the instance node has not been
9389 -- rewritten, then it is still the unit of the compilation. Finally, if
9390 -- a body is present, this is a parent of the main unit whose body has
9391 -- been compiled for inlining purposes, and the instantiation node has
9392 -- been rewritten with the instance body.
9394 -- Otherwise the instantiation node appears after the declaration. If
9395 -- the entity is a formal package, the declaration may have been
9396 -- rewritten as a generic declaration (in the case of a formal with box)
9397 -- or left as a formal package declaration if it has actuals, and is
9398 -- found with a forward search.
9400 if Nkind (Parent (Decl)) = N_Compilation_Unit then
9401 if Nkind (Decl) = N_Package_Declaration
9402 and then Present (Corresponding_Body (Decl))
9403 then
9404 Decl := Unit_Declaration_Node (Corresponding_Body (Decl));
9405 end if;
9407 if Nkind (Original_Node (Decl)) in N_Generic_Instantiation then
9408 return Original_Node (Decl);
9409 else
9410 return Unit (Parent (Decl));
9411 end if;
9413 elsif Nkind (Decl) = N_Package_Declaration
9414 and then Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration
9415 then
9416 return Original_Node (Decl);
9418 else
9419 Inst := Next (Decl);
9420 while Nkind (Inst) not in N_Formal_Package_Declaration
9421 | N_Function_Instantiation
9422 | N_Package_Instantiation
9423 | N_Procedure_Instantiation
9424 loop
9425 Next (Inst);
9426 end loop;
9428 return Inst;
9429 end if;
9430 end Get_Unit_Instantiation_Node;
9432 ------------------------
9433 -- Has_Been_Exchanged --
9434 ------------------------
9436 function Has_Been_Exchanged (E : Entity_Id) return Boolean is
9437 Next : Elmt_Id;
9439 begin
9440 Next := First_Elmt (Exchanged_Views);
9441 while Present (Next) loop
9442 if Full_View (Node (Next)) = E then
9443 return True;
9444 end if;
9446 Next_Elmt (Next);
9447 end loop;
9449 return False;
9450 end Has_Been_Exchanged;
9452 -------------------
9453 -- Has_Contracts --
9454 -------------------
9456 function Has_Contracts (Decl : Node_Id) return Boolean is
9457 A_List : constant List_Id := Aspect_Specifications (Decl);
9458 A_Spec : Node_Id;
9459 A_Id : Aspect_Id;
9460 begin
9461 if No (A_List) then
9462 return False;
9463 else
9464 A_Spec := First (A_List);
9465 while Present (A_Spec) loop
9466 A_Id := Get_Aspect_Id (A_Spec);
9467 if A_Id = Aspect_Pre or else A_Id = Aspect_Post then
9468 return True;
9469 end if;
9471 Next (A_Spec);
9472 end loop;
9474 return False;
9475 end if;
9476 end Has_Contracts;
9478 ----------
9479 -- Hash --
9480 ----------
9482 function Hash (F : Entity_Id) return HTable_Range is
9483 begin
9484 return HTable_Range (F mod HTable_Size);
9485 end Hash;
9487 ------------------------
9488 -- Hide_Current_Scope --
9489 ------------------------
9491 procedure Hide_Current_Scope is
9492 C : constant Entity_Id := Current_Scope;
9493 E : Entity_Id;
9495 begin
9496 Set_Is_Hidden_Open_Scope (C);
9498 E := First_Entity (C);
9499 while Present (E) loop
9500 if Is_Immediately_Visible (E) then
9501 Set_Is_Immediately_Visible (E, False);
9502 Append_Elmt (E, Hidden_Entities);
9503 end if;
9505 Next_Entity (E);
9506 end loop;
9508 -- Make the scope name invisible as well. This is necessary, but might
9509 -- conflict with calls to Rtsfind later on, in case the scope is a
9510 -- predefined one. There is no clean solution to this problem, so for
9511 -- now we depend on the user not redefining Standard itself in one of
9512 -- the parent units.
9514 if Is_Immediately_Visible (C) and then C /= Standard_Standard then
9515 Set_Is_Immediately_Visible (C, False);
9516 Append_Elmt (C, Hidden_Entities);
9517 end if;
9519 end Hide_Current_Scope;
9521 --------------
9522 -- Init_Env --
9523 --------------
9525 procedure Init_Env is
9526 Saved : Instance_Env;
9528 begin
9529 Saved.Instantiated_Parent := Current_Instantiated_Parent;
9530 Saved.Exchanged_Views := Exchanged_Views;
9531 Saved.Hidden_Entities := Hidden_Entities;
9532 Saved.Current_Sem_Unit := Current_Sem_Unit;
9533 Saved.Parent_Unit_Visible := Parent_Unit_Visible;
9534 Saved.Instance_Parent_Unit := Instance_Parent_Unit;
9536 -- Save configuration switches. These may be reset if the unit is a
9537 -- predefined unit, and the current mode is not Ada 2005.
9539 Saved.Switches := Save_Config_Switches;
9541 Instance_Envs.Append (Saved);
9543 Exchanged_Views := New_Elmt_List;
9544 Hidden_Entities := New_Elmt_List;
9546 -- Make dummy entry for Instantiated parent. If generic unit is legal,
9547 -- this is set properly in Set_Instance_Env.
9549 Current_Instantiated_Parent :=
9550 (Current_Scope, Current_Scope, Assoc_Null);
9551 end Init_Env;
9553 ---------------------
9554 -- In_Main_Context --
9555 ---------------------
9557 function In_Main_Context (E : Entity_Id) return Boolean is
9558 Context : List_Id;
9559 Clause : Node_Id;
9560 Nam : Node_Id;
9562 begin
9563 if not Is_Compilation_Unit (E)
9564 or else Ekind (E) /= E_Package
9565 or else In_Private_Part (E)
9566 then
9567 return False;
9568 end if;
9570 Context := Context_Items (Cunit (Main_Unit));
9572 Clause := First (Context);
9573 while Present (Clause) loop
9574 if Nkind (Clause) = N_With_Clause then
9575 Nam := Name (Clause);
9577 -- If the current scope is part of the context of the main unit,
9578 -- analysis of the corresponding with_clause is not complete, and
9579 -- the entity is not set. We use the Chars field directly, which
9580 -- might produce false positives in rare cases, but guarantees
9581 -- that we produce all the instance bodies we will need.
9583 if (Is_Entity_Name (Nam) and then Chars (Nam) = Chars (E))
9584 or else (Nkind (Nam) = N_Selected_Component
9585 and then Chars (Selector_Name (Nam)) = Chars (E))
9586 then
9587 return True;
9588 end if;
9589 end if;
9591 Next (Clause);
9592 end loop;
9594 return False;
9595 end In_Main_Context;
9597 ---------------------
9598 -- Inherit_Context --
9599 ---------------------
9601 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id) is
9602 Current_Context : List_Id;
9603 Current_Unit : Node_Id;
9604 Item : Node_Id;
9605 New_I : Node_Id;
9607 Clause : Node_Id;
9608 OK : Boolean;
9609 Lib_Unit : Node_Id;
9611 begin
9612 if Nkind (Parent (Gen_Decl)) = N_Compilation_Unit then
9614 -- The inherited context is attached to the enclosing compilation
9615 -- unit. This is either the main unit, or the declaration for the
9616 -- main unit (in case the instantiation appears within the package
9617 -- declaration and the main unit is its body).
9619 Current_Unit := Parent (Inst);
9620 while Present (Current_Unit)
9621 and then Nkind (Current_Unit) /= N_Compilation_Unit
9622 loop
9623 Current_Unit := Parent (Current_Unit);
9624 end loop;
9626 Current_Context := Context_Items (Current_Unit);
9628 Item := First (Context_Items (Parent (Gen_Decl)));
9629 while Present (Item) loop
9630 if Nkind (Item) = N_With_Clause then
9631 Lib_Unit := Library_Unit (Item);
9633 -- Take care to prevent direct cyclic with's
9635 if Lib_Unit /= Current_Unit then
9637 -- Do not add a unit if it is already in the context
9639 Clause := First (Current_Context);
9640 OK := True;
9641 while Present (Clause) loop
9642 if Nkind (Clause) = N_With_Clause
9643 and then Library_Unit (Clause) = Lib_Unit
9644 then
9645 OK := False;
9646 exit;
9647 end if;
9649 Next (Clause);
9650 end loop;
9652 if OK then
9653 New_I := New_Copy (Item);
9654 Set_Implicit_With (New_I);
9656 Append (New_I, Current_Context);
9657 end if;
9658 end if;
9659 end if;
9661 Next (Item);
9662 end loop;
9663 end if;
9664 end Inherit_Context;
9666 ----------------
9667 -- Initialize --
9668 ----------------
9670 procedure Initialize is
9671 begin
9672 Generic_Renamings.Init;
9673 Instance_Envs.Init;
9674 Generic_Flags.Init;
9675 Generic_Renamings_HTable.Reset;
9676 Circularity_Detected := False;
9677 Exchanged_Views := No_Elist;
9678 Hidden_Entities := No_Elist;
9679 end Initialize;
9681 -------------------------------------
9682 -- Insert_Freeze_Node_For_Instance --
9683 -------------------------------------
9685 procedure Insert_Freeze_Node_For_Instance
9686 (N : Node_Id;
9687 F_Node : Node_Id)
9689 function Enclosing_Body (N : Node_Id) return Node_Id;
9690 -- Find enclosing package or subprogram body, if any. Freeze node may
9691 -- be placed at end of current declarative list if previous instance
9692 -- and current one have different enclosing bodies.
9694 function Previous_Instance (Gen : Entity_Id) return Entity_Id;
9695 -- Find the local instance, if any, that declares the generic that is
9696 -- being instantiated. If present, the freeze node for this instance
9697 -- must follow the freeze node for the previous instance.
9699 --------------------
9700 -- Enclosing_Body --
9701 --------------------
9703 function Enclosing_Body (N : Node_Id) return Node_Id is
9704 P : Node_Id;
9706 begin
9707 P := Parent (N);
9708 while Present (P)
9709 and then Nkind (Parent (P)) /= N_Compilation_Unit
9710 loop
9711 if Nkind (P) in N_Package_Body | N_Subprogram_Body then
9712 if Nkind (Parent (P)) = N_Subunit then
9713 return Corresponding_Stub (Parent (P));
9714 else
9715 return P;
9716 end if;
9717 end if;
9719 P := True_Parent (P);
9720 end loop;
9722 return Empty;
9723 end Enclosing_Body;
9725 -----------------------
9726 -- Previous_Instance --
9727 -----------------------
9729 function Previous_Instance (Gen : Entity_Id) return Entity_Id is
9730 S : Entity_Id;
9732 begin
9733 S := Scope (Gen);
9734 while Present (S) and then S /= Standard_Standard loop
9735 if Is_Generic_Instance (S)
9736 and then In_Same_Source_Unit (S, N)
9737 then
9738 return S;
9739 end if;
9741 S := Scope (S);
9742 end loop;
9744 return Empty;
9745 end Previous_Instance;
9747 -- Local variables
9749 Decl : Node_Id;
9750 Decls : List_Id;
9751 Inst : Entity_Id;
9752 Origin : Entity_Id;
9753 Par_Inst : Node_Id;
9754 Par_N : Node_Id;
9756 -- Start of processing for Insert_Freeze_Node_For_Instance
9758 begin
9759 -- Nothing to do if the freeze node has already been inserted
9761 if Is_List_Member (F_Node) then
9762 return;
9763 end if;
9765 Inst := Entity (F_Node);
9767 -- When processing a subprogram instantiation, utilize the actual
9768 -- subprogram instantiation rather than its package wrapper as it
9769 -- carries all the context information.
9771 if Is_Wrapper_Package (Inst) then
9772 Inst := Related_Instance (Inst);
9773 end if;
9775 Par_Inst := Parent (Inst);
9777 -- If this is a package instance, check whether the generic is declared
9778 -- in a previous instance and the current instance is not within the
9779 -- previous one.
9781 if Present (Generic_Parent (Par_Inst)) and then Is_In_Main_Unit (N) then
9782 declare
9783 Enclosing_N : constant Node_Id := Enclosing_Body (N);
9784 Par_I : constant Entity_Id :=
9785 Previous_Instance (Generic_Parent (Par_Inst));
9786 Scop : Entity_Id;
9788 begin
9789 if Present (Par_I) and then Earlier (N, Freeze_Node (Par_I)) then
9790 Scop := Scope (Inst);
9792 -- If the current instance is within the one that contains
9793 -- the generic, the freeze node for the current one must
9794 -- appear in the current declarative part. Ditto, if the
9795 -- current instance is within another package instance or
9796 -- within a body that does not enclose the current instance.
9797 -- In these three cases the freeze node of the previous
9798 -- instance is not relevant.
9800 while Present (Scop) and then Scop /= Standard_Standard loop
9801 exit when Scop = Par_I
9802 or else
9803 (Is_Generic_Instance (Scop)
9804 and then Scope_Depth (Scop) > Scope_Depth (Par_I));
9805 Scop := Scope (Scop);
9806 end loop;
9808 -- Previous instance encloses current instance
9810 if Scop = Par_I then
9811 null;
9813 -- If the next node is a source body we must freeze in the
9814 -- current scope as well.
9816 elsif Present (Next (N))
9817 and then Nkind (Next (N)) in N_Subprogram_Body
9818 | N_Package_Body
9819 and then Comes_From_Source (Next (N))
9820 then
9821 null;
9823 -- Current instance is within an unrelated instance
9825 elsif Is_Generic_Instance (Scop) then
9826 null;
9828 -- Current instance is within an unrelated body
9830 elsif Present (Enclosing_N)
9831 and then Enclosing_N /= Enclosing_Body (Par_I)
9832 then
9833 null;
9835 else
9836 Insert_After (Freeze_Node (Par_I), F_Node);
9837 return;
9838 end if;
9839 end if;
9840 end;
9841 end if;
9843 Decl := N;
9844 Decls := List_Containing (N);
9845 Par_N := Parent (Decls);
9846 Origin := Empty;
9848 -- Determine the proper freeze point of an instantiation
9850 if Is_Generic_Instance (Inst) then
9851 loop
9852 -- When the instantiation occurs in a package spec, append the
9853 -- freeze node to the private declarations (if any).
9855 if Nkind (Par_N) = N_Package_Specification
9856 and then Decls = Visible_Declarations (Par_N)
9857 and then not Is_Empty_List (Private_Declarations (Par_N))
9858 then
9859 Decls := Private_Declarations (Par_N);
9860 Decl := First (Decls);
9861 end if;
9863 -- We adhere to the general rule of a package or subprogram body
9864 -- causing freezing of anything before it in the same declarative
9865 -- region. In this respect, the proper freeze point of a package
9866 -- instantiation is before the first source body which follows, or
9867 -- before a stub. This ensures that entities from the instance are
9868 -- already frozen and therefore usable in source bodies.
9870 if Nkind (Par_N) /= N_Package_Declaration
9871 and then
9872 not In_Same_Source_Unit (Generic_Parent (Par_Inst), Inst)
9873 then
9874 while Present (Decl) loop
9875 if ((Nkind (Decl) in N_Unit_Body
9876 or else
9877 Nkind (Decl) in N_Body_Stub)
9878 and then Comes_From_Source (Decl))
9879 or else (Present (Origin)
9880 and then Nkind (Decl) in N_Generic_Instantiation
9881 and then Instance_Spec (Decl) /= Origin)
9882 then
9883 Set_Sloc (F_Node, Sloc (Decl));
9884 Insert_Before (Decl, F_Node);
9885 return;
9886 end if;
9888 Next (Decl);
9889 end loop;
9890 end if;
9892 -- When the instantiation occurs in a package spec and there is
9893 -- no source body which follows, and the package has a body but
9894 -- is delayed, then insert immediately before its freeze node.
9896 if Nkind (Par_N) = N_Package_Specification
9897 and then Present (Corresponding_Body (Parent (Par_N)))
9898 and then Present (Freeze_Node (Defining_Entity (Par_N)))
9899 then
9900 Set_Sloc (F_Node, Sloc (Freeze_Node (Defining_Entity (Par_N))));
9901 Insert_Before (Freeze_Node (Defining_Entity (Par_N)), F_Node);
9902 return;
9904 -- When the instantiation occurs in a package spec and there is
9905 -- no source body which follows, not even of the package itself,
9906 -- then insert into the declaration list of the outer level, but
9907 -- do not jump over following instantiations in this list because
9908 -- they may have a body that has not materialized yet, see above.
9910 elsif Nkind (Par_N) = N_Package_Specification
9911 and then No (Corresponding_Body (Parent (Par_N)))
9912 and then Is_List_Member (Parent (Par_N))
9913 then
9914 Decl := Parent (Par_N);
9915 Decls := List_Containing (Decl);
9916 Par_N := Parent (Decls);
9917 Origin := Decl;
9919 -- In a package declaration, or if no source body which follows
9920 -- and at library level, then insert at end of list.
9922 else
9923 exit;
9924 end if;
9925 end loop;
9926 end if;
9928 -- Insert and adjust the Sloc of the freeze node
9930 Set_Sloc (F_Node, Sloc (Last (Decls)));
9931 Insert_After (Last (Decls), F_Node);
9932 end Insert_Freeze_Node_For_Instance;
9934 -----------------------------
9935 -- Install_Formal_Packages --
9936 -----------------------------
9938 procedure Install_Formal_Packages (Par : Entity_Id) is
9939 E : Entity_Id;
9940 Gen : Entity_Id;
9941 Gen_E : Entity_Id := Empty;
9943 begin
9944 E := First_Entity (Par);
9946 -- If we are installing an instance parent, locate the formal packages
9947 -- of its generic parent.
9949 if Is_Generic_Instance (Par) then
9950 Gen := Generic_Parent (Package_Specification (Par));
9951 Gen_E := First_Entity (Gen);
9952 end if;
9954 while Present (E) loop
9955 if Ekind (E) = E_Package
9956 and then Nkind (Parent (E)) = N_Package_Renaming_Declaration
9957 then
9958 -- If this is the renaming for the parent instance, done
9960 if Renamed_Entity (E) = Par then
9961 exit;
9963 -- The visibility of a formal of an enclosing generic is already
9964 -- correct.
9966 elsif Denotes_Formal_Package (E) then
9967 null;
9969 elsif Present (Associated_Formal_Package (E)) then
9970 Check_Generic_Actuals (Renamed_Entity (E), True);
9971 Set_Is_Hidden (E, False);
9973 -- Find formal package in generic unit that corresponds to
9974 -- (instance of) formal package in instance.
9976 while Present (Gen_E) and then Chars (Gen_E) /= Chars (E) loop
9977 Next_Entity (Gen_E);
9978 end loop;
9980 if Present (Gen_E) then
9981 Map_Formal_Package_Entities (Gen_E, E);
9982 end if;
9983 end if;
9984 end if;
9986 Next_Entity (E);
9988 if Present (Gen_E) then
9989 Next_Entity (Gen_E);
9990 end if;
9991 end loop;
9992 end Install_Formal_Packages;
9994 --------------------
9995 -- Install_Parent --
9996 --------------------
9998 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False) is
9999 Ancestors : constant Elist_Id := New_Elmt_List;
10000 S : constant Entity_Id := Current_Scope;
10001 Inst_Par : Entity_Id;
10002 First_Par : Entity_Id;
10003 Inst_Node : Node_Id;
10004 Gen_Par : Entity_Id;
10005 First_Gen : Entity_Id;
10006 Elmt : Elmt_Id;
10008 procedure Install_Noninstance_Specs (Par : Entity_Id);
10009 -- Install the scopes of noninstance parent units ending with Par
10011 procedure Install_Spec (Par : Entity_Id);
10012 -- The child unit is within the declarative part of the parent, so the
10013 -- declarations within the parent are immediately visible.
10015 -------------------------------
10016 -- Install_Noninstance_Specs --
10017 -------------------------------
10019 procedure Install_Noninstance_Specs (Par : Entity_Id) is
10020 begin
10021 if Present (Par)
10022 and then Par /= Standard_Standard
10023 and then not In_Open_Scopes (Par)
10024 then
10025 Install_Noninstance_Specs (Scope (Par));
10026 Install_Spec (Par);
10027 end if;
10028 end Install_Noninstance_Specs;
10030 ------------------
10031 -- Install_Spec --
10032 ------------------
10034 procedure Install_Spec (Par : Entity_Id) is
10035 Spec : constant Node_Id := Package_Specification (Par);
10037 begin
10038 -- If this parent of the child instance is a top-level unit,
10039 -- then record the unit and its visibility for later resetting in
10040 -- Remove_Parent. We exclude units that are generic instances, as we
10041 -- only want to record this information for the ultimate top-level
10042 -- noninstance parent (is that always correct???).
10044 if Scope (Par) = Standard_Standard
10045 and then not Is_Generic_Instance (Par)
10046 then
10047 Parent_Unit_Visible := Is_Immediately_Visible (Par);
10048 Instance_Parent_Unit := Par;
10049 end if;
10051 -- Open the parent scope and make it and its declarations visible.
10052 -- If this point is not within a body, then only the visible
10053 -- declarations should be made visible, and installation of the
10054 -- private declarations is deferred until the appropriate point
10055 -- within analysis of the spec being instantiated (see the handling
10056 -- of parent visibility in Analyze_Package_Specification). This is
10057 -- relaxed in the case where the parent unit is Ada.Tags, to avoid
10058 -- private view problems that occur when compiling instantiations of
10059 -- a generic child of that package (Generic_Dispatching_Constructor).
10060 -- If the instance freezes a tagged type, inlinings of operations
10061 -- from Ada.Tags may need the full view of type Tag. If inlining took
10062 -- proper account of establishing visibility of inlined subprograms'
10063 -- parents then it should be possible to remove this
10064 -- special check. ???
10066 Push_Scope (Par);
10067 Set_Is_Immediately_Visible (Par);
10068 Install_Visible_Declarations (Par);
10069 Set_Use (Visible_Declarations (Spec));
10071 if In_Body or else Is_RTU (Par, Ada_Tags) then
10072 Install_Private_Declarations (Par);
10073 Set_Use (Private_Declarations (Spec));
10074 end if;
10075 end Install_Spec;
10077 -- Start of processing for Install_Parent
10079 begin
10080 -- We need to install the parent instance to compile the instantiation
10081 -- of the child, but the child instance must appear in the current
10082 -- scope. Given that we cannot place the parent above the current scope
10083 -- in the scope stack, we duplicate the current scope and unstack both
10084 -- after the instantiation is complete.
10086 -- If the parent is itself the instantiation of a child unit, we must
10087 -- also stack the instantiation of its parent, and so on. Each such
10088 -- ancestor is the prefix of the name in a prior instantiation.
10090 -- If this is a nested instance, the parent unit itself resolves to
10091 -- a renaming of the parent instance, whose declaration we need.
10093 -- Finally, the parent may be a generic (not an instance) when the
10094 -- child unit appears as a formal package.
10096 Inst_Par := P;
10098 if Present (Renamed_Entity (Inst_Par)) then
10099 Inst_Par := Renamed_Entity (Inst_Par);
10100 end if;
10102 First_Par := Inst_Par;
10104 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
10106 First_Gen := Gen_Par;
10108 while Present (Gen_Par) and then Is_Child_Unit (Gen_Par) loop
10110 -- Load grandparent instance as well
10112 Inst_Node := Get_Unit_Instantiation_Node (Inst_Par);
10114 if Nkind (Name (Inst_Node)) = N_Expanded_Name then
10115 Inst_Par := Entity (Prefix (Name (Inst_Node)));
10117 if Present (Renamed_Entity (Inst_Par)) then
10118 Inst_Par := Renamed_Entity (Inst_Par);
10119 end if;
10121 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
10123 if Present (Gen_Par) then
10124 Prepend_Elmt (Inst_Par, Ancestors);
10126 else
10127 -- Parent is not the name of an instantiation
10129 Install_Noninstance_Specs (Inst_Par);
10130 exit;
10131 end if;
10133 else
10134 -- Previous error
10136 exit;
10137 end if;
10138 end loop;
10140 if Present (First_Gen) then
10141 Append_Elmt (First_Par, Ancestors);
10142 else
10143 Install_Noninstance_Specs (First_Par);
10144 end if;
10146 if not Is_Empty_Elmt_List (Ancestors) then
10147 Elmt := First_Elmt (Ancestors);
10148 while Present (Elmt) loop
10149 Install_Spec (Node (Elmt));
10150 Install_Formal_Packages (Node (Elmt));
10151 Next_Elmt (Elmt);
10152 end loop;
10153 end if;
10155 if not In_Body then
10156 Push_Scope (S);
10157 end if;
10158 end Install_Parent;
10160 -------------------------------
10161 -- Install_Hidden_Primitives --
10162 -------------------------------
10164 procedure Install_Hidden_Primitives
10165 (Prims_List : in out Elist_Id;
10166 Gen_T : Entity_Id;
10167 Act_T : Entity_Id)
10169 Elmt : Elmt_Id;
10170 List : Elist_Id := No_Elist;
10171 Prim_G_Elmt : Elmt_Id;
10172 Prim_A_Elmt : Elmt_Id;
10173 Prim_G : Node_Id;
10174 Prim_A : Node_Id;
10176 begin
10177 -- No action needed in case of serious errors because we cannot trust
10178 -- in the order of primitives
10180 if Serious_Errors_Detected > 0 then
10181 return;
10183 -- No action possible if we don't have available the list of primitive
10184 -- operations
10186 elsif No (Gen_T)
10187 or else not Is_Record_Type (Gen_T)
10188 or else not Is_Tagged_Type (Gen_T)
10189 or else not Is_Record_Type (Act_T)
10190 or else not Is_Tagged_Type (Act_T)
10191 then
10192 return;
10194 -- There is no need to handle interface types since their primitives
10195 -- cannot be hidden
10197 elsif Is_Interface (Gen_T) then
10198 return;
10199 end if;
10201 Prim_G_Elmt := First_Elmt (Primitive_Operations (Gen_T));
10203 if not Is_Class_Wide_Type (Act_T) then
10204 Prim_A_Elmt := First_Elmt (Primitive_Operations (Act_T));
10205 else
10206 Prim_A_Elmt := First_Elmt (Primitive_Operations (Root_Type (Act_T)));
10207 end if;
10209 loop
10210 -- Skip predefined primitives in the generic formal
10212 while Present (Prim_G_Elmt)
10213 and then Is_Predefined_Dispatching_Operation (Node (Prim_G_Elmt))
10214 loop
10215 Next_Elmt (Prim_G_Elmt);
10216 end loop;
10218 -- Skip predefined primitives in the generic actual
10220 while Present (Prim_A_Elmt)
10221 and then Is_Predefined_Dispatching_Operation (Node (Prim_A_Elmt))
10222 loop
10223 Next_Elmt (Prim_A_Elmt);
10224 end loop;
10226 exit when No (Prim_G_Elmt) or else No (Prim_A_Elmt);
10228 Prim_G := Node (Prim_G_Elmt);
10229 Prim_A := Node (Prim_A_Elmt);
10231 -- There is no need to handle interface primitives because their
10232 -- primitives are not hidden
10234 exit when Present (Interface_Alias (Prim_G));
10236 -- Here we install one hidden primitive
10238 if Chars (Prim_G) /= Chars (Prim_A)
10239 and then Has_Suffix (Prim_A, 'P')
10240 and then Remove_Suffix (Prim_A, 'P') = Chars (Prim_G)
10241 then
10242 Set_Chars (Prim_A, Chars (Prim_G));
10243 Append_New_Elmt (Prim_A, To => List);
10244 end if;
10246 Next_Elmt (Prim_A_Elmt);
10247 Next_Elmt (Prim_G_Elmt);
10248 end loop;
10250 -- Append the elements to the list of temporarily visible primitives
10251 -- avoiding duplicates.
10253 if Present (List) then
10254 if No (Prims_List) then
10255 Prims_List := New_Elmt_List;
10256 end if;
10258 Elmt := First_Elmt (List);
10259 while Present (Elmt) loop
10260 Append_Unique_Elmt (Node (Elmt), Prims_List);
10261 Next_Elmt (Elmt);
10262 end loop;
10263 end if;
10264 end Install_Hidden_Primitives;
10266 -------------------------------
10267 -- Restore_Hidden_Primitives --
10268 -------------------------------
10270 procedure Restore_Hidden_Primitives (Prims_List : in out Elist_Id) is
10271 Prim_Elmt : Elmt_Id;
10272 Prim : Node_Id;
10274 begin
10275 if Present (Prims_List) then
10276 Prim_Elmt := First_Elmt (Prims_List);
10277 while Present (Prim_Elmt) loop
10278 Prim := Node (Prim_Elmt);
10279 Set_Chars (Prim, Add_Suffix (Prim, 'P'));
10280 Next_Elmt (Prim_Elmt);
10281 end loop;
10283 Prims_List := No_Elist;
10284 end if;
10285 end Restore_Hidden_Primitives;
10287 --------------------------------
10288 -- Instantiate_Formal_Package --
10289 --------------------------------
10291 function Instantiate_Formal_Package
10292 (Formal : Node_Id;
10293 Actual : Node_Id;
10294 Analyzed_Formal : Node_Id) return List_Id
10296 Loc : constant Source_Ptr := Sloc (Actual);
10297 Hidden_Formals : constant Elist_Id := New_Elmt_List;
10299 Actual_Pack : Entity_Id;
10300 Formal_Pack : Entity_Id;
10301 Gen_Parent : Entity_Id;
10302 Decls : List_Id;
10303 Nod : Node_Id;
10304 Parent_Spec : Node_Id;
10306 procedure Find_Matching_Actual
10307 (F : Node_Id;
10308 Act : in out Entity_Id);
10309 -- We need to associate each formal entity in the formal package with
10310 -- the corresponding entity in the actual package. The actual package
10311 -- has been analyzed and possibly expanded, and as a result there is
10312 -- no one-to-one correspondence between the two lists (for example,
10313 -- the actual may include subtypes, itypes, and inherited primitive
10314 -- operations, interspersed among the renaming declarations for the
10315 -- actuals). We retrieve the corresponding actual by name because each
10316 -- actual has the same name as the formal, and they do appear in the
10317 -- same order.
10319 function Get_Formal_Entity (N : Node_Id) return Entity_Id;
10320 -- Retrieve entity of defining entity of generic formal parameter.
10321 -- Only the declarations of formals need to be considered when
10322 -- linking them to actuals, but the declarative list may include
10323 -- internal entities generated during analysis, and those are ignored.
10325 procedure Match_Formal_Entity
10326 (Formal_Node : Node_Id;
10327 Formal_Ent : Entity_Id;
10328 Actual_Ent : Entity_Id);
10329 -- Associates the formal entity with the actual. In the case where
10330 -- Formal_Ent is a formal package, this procedure iterates through all
10331 -- of its formals and enters associations between the actuals occurring
10332 -- in the formal package's corresponding actual package (given by
10333 -- Actual_Ent) and the formal package's formal parameters. This
10334 -- procedure recurses if any of the parameters is itself a package.
10336 function Is_Instance_Of
10337 (Act_Spec : Entity_Id;
10338 Gen_Anc : Entity_Id) return Boolean;
10339 -- The actual can be an instantiation of a generic within another
10340 -- instance, in which case there is no direct link from it to the
10341 -- original generic ancestor. In that case, we recognize that the
10342 -- ultimate ancestor is the same by examining names and scopes.
10344 procedure Process_Nested_Formal (Formal : Entity_Id);
10345 -- If the current formal is declared with a box, its own formals are
10346 -- visible in the instance, as they were in the generic, and their
10347 -- Hidden flag must be reset. If some of these formals are themselves
10348 -- packages declared with a box, the processing must be recursive.
10350 --------------------------
10351 -- Find_Matching_Actual --
10352 --------------------------
10354 procedure Find_Matching_Actual
10355 (F : Node_Id;
10356 Act : in out Entity_Id)
10358 Formal_Ent : Entity_Id;
10360 begin
10361 case Nkind (Original_Node (F)) is
10362 when N_Formal_Object_Declaration
10363 | N_Formal_Type_Declaration
10365 Formal_Ent := Defining_Identifier (F);
10367 while Present (Act)
10368 and then Chars (Act) /= Chars (Formal_Ent)
10369 loop
10370 Next_Entity (Act);
10371 end loop;
10373 when N_Formal_Package_Declaration
10374 | N_Formal_Subprogram_Declaration
10375 | N_Generic_Package_Declaration
10376 | N_Package_Declaration
10378 Formal_Ent := Defining_Entity (F);
10380 while Present (Act)
10381 and then Chars (Act) /= Chars (Formal_Ent)
10382 loop
10383 Next_Entity (Act);
10384 end loop;
10386 when others =>
10387 raise Program_Error;
10388 end case;
10389 end Find_Matching_Actual;
10391 -------------------------
10392 -- Match_Formal_Entity --
10393 -------------------------
10395 procedure Match_Formal_Entity
10396 (Formal_Node : Node_Id;
10397 Formal_Ent : Entity_Id;
10398 Actual_Ent : Entity_Id)
10400 Act_Pkg : Entity_Id;
10402 begin
10403 Set_Instance_Of (Formal_Ent, Actual_Ent);
10405 if Ekind (Actual_Ent) = E_Package then
10407 -- Record associations for each parameter
10409 Act_Pkg := Actual_Ent;
10411 declare
10412 A_Ent : Entity_Id := First_Entity (Act_Pkg);
10413 F_Ent : Entity_Id;
10414 F_Node : Node_Id;
10416 Gen_Decl : Node_Id;
10417 Formals : List_Id;
10418 Actual : Entity_Id;
10420 begin
10421 -- Retrieve the actual given in the formal package declaration
10423 Actual := Entity (Name (Original_Node (Formal_Node)));
10425 -- The actual in the formal package declaration may be a
10426 -- renamed generic package, in which case we want to retrieve
10427 -- the original generic in order to traverse its formal part.
10429 if Present (Renamed_Entity (Actual)) then
10430 Gen_Decl := Unit_Declaration_Node (Renamed_Entity (Actual));
10431 else
10432 Gen_Decl := Unit_Declaration_Node (Actual);
10433 end if;
10435 Formals := Generic_Formal_Declarations (Gen_Decl);
10437 if Present (Formals) then
10438 F_Node := First_Non_Pragma (Formals);
10439 else
10440 F_Node := Empty;
10441 end if;
10443 while Present (A_Ent)
10444 and then Present (F_Node)
10445 and then A_Ent /= First_Private_Entity (Act_Pkg)
10446 loop
10447 F_Ent := Get_Formal_Entity (F_Node);
10449 if Present (F_Ent) then
10451 -- This is a formal of the original package. Record
10452 -- association and recurse.
10454 Find_Matching_Actual (F_Node, A_Ent);
10455 Match_Formal_Entity (F_Node, F_Ent, A_Ent);
10456 Next_Entity (A_Ent);
10457 end if;
10459 Next_Non_Pragma (F_Node);
10460 end loop;
10461 end;
10462 end if;
10463 end Match_Formal_Entity;
10465 -----------------------
10466 -- Get_Formal_Entity --
10467 -----------------------
10469 function Get_Formal_Entity (N : Node_Id) return Entity_Id is
10470 Kind : constant Node_Kind := Nkind (Original_Node (N));
10471 begin
10472 case Kind is
10473 when N_Formal_Object_Declaration =>
10474 return Defining_Identifier (N);
10476 when N_Formal_Type_Declaration =>
10477 return Defining_Identifier (N);
10479 when N_Formal_Subprogram_Declaration =>
10480 return Defining_Unit_Name (Specification (N));
10482 when N_Formal_Package_Declaration =>
10483 return Defining_Identifier (Original_Node (N));
10485 when N_Generic_Package_Declaration =>
10486 return Defining_Identifier (Original_Node (N));
10488 -- All other declarations are introduced by semantic analysis and
10489 -- have no match in the actual.
10491 when others =>
10492 return Empty;
10493 end case;
10494 end Get_Formal_Entity;
10496 --------------------
10497 -- Is_Instance_Of --
10498 --------------------
10500 function Is_Instance_Of
10501 (Act_Spec : Entity_Id;
10502 Gen_Anc : Entity_Id) return Boolean
10504 Gen_Par : constant Entity_Id := Generic_Parent (Act_Spec);
10506 begin
10507 if No (Gen_Par) then
10508 return False;
10510 -- Simplest case: the generic parent of the actual is the formal
10512 elsif Gen_Par = Gen_Anc then
10513 return True;
10515 elsif Chars (Gen_Par) /= Chars (Gen_Anc) then
10516 return False;
10518 -- The actual may be obtained through several instantiations. Its
10519 -- scope must itself be an instance of a generic declared in the
10520 -- same scope as the formal. Any other case is detected above.
10522 elsif not Is_Generic_Instance (Scope (Gen_Par)) then
10523 return False;
10525 else
10526 return Generic_Parent (Parent (Scope (Gen_Par))) = Scope (Gen_Anc);
10527 end if;
10528 end Is_Instance_Of;
10530 ---------------------------
10531 -- Process_Nested_Formal --
10532 ---------------------------
10534 procedure Process_Nested_Formal (Formal : Entity_Id) is
10535 Ent : Entity_Id;
10537 begin
10538 if Present (Associated_Formal_Package (Formal))
10539 and then Box_Present (Parent (Associated_Formal_Package (Formal)))
10540 then
10541 Ent := First_Entity (Formal);
10542 while Present (Ent) loop
10543 Set_Is_Hidden (Ent, False);
10544 Set_Is_Visible_Formal (Ent);
10545 Set_Is_Potentially_Use_Visible
10546 (Ent, Is_Potentially_Use_Visible (Formal));
10548 if Ekind (Ent) = E_Package then
10549 exit when Renamed_Entity (Ent) = Renamed_Entity (Formal);
10550 Process_Nested_Formal (Ent);
10551 end if;
10553 Next_Entity (Ent);
10554 end loop;
10555 end if;
10556 end Process_Nested_Formal;
10558 -- Start of processing for Instantiate_Formal_Package
10560 begin
10561 Analyze (Actual);
10563 -- The actual must be a package instance, or else a current instance
10564 -- such as a parent generic within the body of a generic child.
10566 if not Is_Entity_Name (Actual)
10567 or else not Is_Package_Or_Generic_Package (Entity (Actual))
10568 then
10569 Error_Msg_N
10570 ("expect package instance to instantiate formal", Actual);
10571 Abandon_Instantiation (Actual);
10572 raise Program_Error;
10574 else
10575 Actual_Pack := Entity (Actual);
10576 Set_Is_Instantiated (Actual_Pack);
10578 -- The actual may be a renamed package, or an outer generic formal
10579 -- package whose instantiation is converted into a renaming.
10581 if Present (Renamed_Entity (Actual_Pack)) then
10582 Actual_Pack := Renamed_Entity (Actual_Pack);
10583 end if;
10585 -- The analyzed formal is expected to be the result of the rewriting
10586 -- of the formal package into a regular package by analysis.
10588 pragma Assert (Nkind (Analyzed_Formal) = N_Package_Declaration
10589 and then Nkind (Original_Node (Analyzed_Formal)) =
10590 N_Formal_Package_Declaration);
10592 Gen_Parent := Generic_Parent (Specification (Analyzed_Formal));
10593 Formal_Pack := Defining_Unit_Name (Specification (Analyzed_Formal));
10595 if Nkind (Parent (Actual_Pack)) = N_Defining_Program_Unit_Name then
10596 Parent_Spec := Package_Specification (Actual_Pack);
10597 else
10598 Parent_Spec := Parent (Actual_Pack);
10599 end if;
10601 if Gen_Parent = Any_Id then
10602 Error_Msg_N
10603 ("previous error in declaration of formal package", Actual);
10604 Abandon_Instantiation (Actual);
10606 elsif Is_Instance_Of (Parent_Spec, Get_Instance_Of (Gen_Parent)) then
10607 null;
10609 -- If this is the current instance of an enclosing generic, that unit
10610 -- is the generic package we need.
10612 elsif In_Open_Scopes (Actual_Pack)
10613 and then Ekind (Actual_Pack) = E_Generic_Package
10614 then
10615 null;
10617 else
10618 Error_Msg_NE
10619 ("actual parameter must be instance of&", Actual, Gen_Parent);
10620 Abandon_Instantiation (Actual);
10621 end if;
10623 Set_Instance_Of (Defining_Identifier (Formal), Actual_Pack);
10624 Map_Formal_Package_Entities (Formal_Pack, Actual_Pack);
10626 Nod :=
10627 Make_Package_Renaming_Declaration (Loc,
10628 Defining_Unit_Name => New_Copy (Defining_Identifier (Formal)),
10629 Name => New_Occurrence_Of (Actual_Pack, Loc));
10631 Set_Associated_Formal_Package
10632 (Defining_Unit_Name (Nod), Defining_Identifier (Formal));
10633 Decls := New_List (Nod);
10635 -- If the formal F has a box, then the generic declarations are
10636 -- visible in the generic G. In an instance of G, the corresponding
10637 -- entities in the actual for F (which are the actuals for the
10638 -- instantiation of the generic that F denotes) must also be made
10639 -- visible for analysis of the current instance. On exit from the
10640 -- current instance, those entities are made private again. If the
10641 -- actual is currently in use, these entities are also use-visible.
10643 -- The loop through the actual entities also steps through the formal
10644 -- entities and enters associations from formals to actuals into the
10645 -- renaming map. This is necessary to properly handle checking of
10646 -- actual parameter associations for later formals that depend on
10647 -- actuals declared in the formal package.
10649 -- In Ada 2005, partial parameterization requires that we make
10650 -- visible the actuals corresponding to formals that were defaulted
10651 -- in the formal package. There formals are identified because they
10652 -- remain formal generics within the formal package, rather than
10653 -- being renamings of the actuals supplied.
10655 declare
10656 Gen_Decl : constant Node_Id :=
10657 Unit_Declaration_Node (Gen_Parent);
10658 Formals : constant List_Id :=
10659 Generic_Formal_Declarations (Gen_Decl);
10661 Actual_Ent : Entity_Id;
10662 Actual_Of_Formal : Node_Id;
10663 Formal_Node : Node_Id;
10664 Formal_Ent : Entity_Id;
10666 begin
10667 if Present (Formals) then
10668 Formal_Node := First_Non_Pragma (Formals);
10669 else
10670 Formal_Node := Empty;
10671 end if;
10673 Actual_Ent := First_Entity (Actual_Pack);
10674 Actual_Of_Formal :=
10675 First (Visible_Declarations (Specification (Analyzed_Formal)));
10676 while Present (Actual_Ent)
10677 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
10678 loop
10679 if Present (Formal_Node) then
10680 Formal_Ent := Get_Formal_Entity (Formal_Node);
10682 if Present (Formal_Ent) then
10683 Find_Matching_Actual (Formal_Node, Actual_Ent);
10684 Match_Formal_Entity (Formal_Node, Formal_Ent, Actual_Ent);
10686 -- We iterate at the same time over the actuals of the
10687 -- local package created for the formal, to determine
10688 -- which one of the formals of the original generic were
10689 -- defaulted in the formal. The corresponding actual
10690 -- entities are visible in the enclosing instance.
10692 if Box_Present (Formal)
10693 or else
10694 (Present (Actual_Of_Formal)
10695 and then
10696 Is_Generic_Formal
10697 (Get_Formal_Entity (Actual_Of_Formal)))
10698 then
10699 Set_Is_Hidden (Actual_Ent, False);
10700 Set_Is_Visible_Formal (Actual_Ent);
10701 Set_Is_Potentially_Use_Visible
10702 (Actual_Ent, In_Use (Actual_Pack));
10704 if Ekind (Actual_Ent) = E_Package then
10705 Process_Nested_Formal (Actual_Ent);
10706 end if;
10708 else
10709 if not Is_Hidden (Actual_Ent) then
10710 Append_Elmt (Actual_Ent, Hidden_Formals);
10711 end if;
10713 Set_Is_Hidden (Actual_Ent);
10714 Set_Is_Potentially_Use_Visible (Actual_Ent, False);
10715 end if;
10716 end if;
10718 Next_Non_Pragma (Formal_Node);
10719 Next (Actual_Of_Formal);
10721 -- A formal subprogram may be overloaded, so advance in
10722 -- the list of actuals to make sure we do not match two
10723 -- successive formals to the same actual. This is only
10724 -- relevant for overloadable entities, others have
10725 -- distinct names.
10727 if Is_Overloadable (Actual_Ent) then
10728 Next_Entity (Actual_Ent);
10729 end if;
10731 else
10732 -- No further formals to match, but the generic part may
10733 -- contain inherited operation that are not hidden in the
10734 -- enclosing instance.
10736 Next_Entity (Actual_Ent);
10737 end if;
10738 end loop;
10740 -- Inherited subprograms generated by formal derived types are
10741 -- also visible if the types are.
10743 Actual_Ent := First_Entity (Actual_Pack);
10744 while Present (Actual_Ent)
10745 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
10746 loop
10747 if Is_Overloadable (Actual_Ent)
10748 and then
10749 Nkind (Parent (Actual_Ent)) = N_Subtype_Declaration
10750 and then
10751 not Is_Hidden (Defining_Identifier (Parent (Actual_Ent)))
10752 then
10753 Set_Is_Hidden (Actual_Ent, False);
10754 Set_Is_Potentially_Use_Visible
10755 (Actual_Ent, In_Use (Actual_Pack));
10756 end if;
10758 Next_Entity (Actual_Ent);
10759 end loop;
10760 end;
10762 -- If the formal requires conformance checking, reanalyze it as an
10763 -- abbreviated instantiation, to verify the matching rules of 12.7.
10764 -- The actual checks are performed after the generic associations
10765 -- have been analyzed, to guarantee the same visibility for this
10766 -- instantiation and for the actuals.
10768 -- In Ada 2005, the generic associations for the formal can include
10769 -- defaulted parameters. These are ignored during check. This
10770 -- internal instantiation is removed from the tree after conformance
10771 -- checking, because it contains formal declarations for those
10772 -- defaulted parameters, and those should not reach the back-end.
10774 if Requires_Conformance_Checking (Formal) then
10775 declare
10776 I_Pack : constant Entity_Id := Make_Temporary (Loc, 'P');
10778 I_Nam : Node_Id;
10780 begin
10781 Set_Is_Internal (I_Pack);
10782 Mutate_Ekind (I_Pack, E_Package);
10784 -- Insert the package into the list of its hidden entities so
10785 -- that the list is not empty for Is_Abbreviated_Instance.
10787 Append_Elmt (I_Pack, Hidden_Formals);
10789 Set_Hidden_In_Formal_Instance (I_Pack, Hidden_Formals);
10791 -- If the generic is a child unit, Check_Generic_Child_Unit
10792 -- needs its original name in case it is qualified.
10794 if Is_Child_Unit (Gen_Parent) then
10795 I_Nam :=
10796 New_Copy_Tree (Name (Original_Node (Analyzed_Formal)));
10797 pragma Assert (Entity (I_Nam) = Gen_Parent);
10799 else
10800 I_Nam :=
10801 New_Occurrence_Of (Get_Instance_Of (Gen_Parent), Loc);
10802 end if;
10804 Append_To (Decls,
10805 Make_Package_Instantiation (Loc,
10806 Defining_Unit_Name => I_Pack,
10807 Name => I_Nam,
10808 Generic_Associations => Generic_Associations (Formal)));
10809 end;
10810 end if;
10812 return Decls;
10813 end if;
10814 end Instantiate_Formal_Package;
10816 -----------------------------------
10817 -- Instantiate_Formal_Subprogram --
10818 -----------------------------------
10820 function Instantiate_Formal_Subprogram
10821 (Formal : Node_Id;
10822 Actual : Node_Id;
10823 Analyzed_Formal : Node_Id) return Node_Id
10825 Analyzed_S : constant Entity_Id :=
10826 Defining_Unit_Name (Specification (Analyzed_Formal));
10827 Formal_Sub : constant Entity_Id :=
10828 Defining_Unit_Name (Specification (Formal));
10830 function From_Parent_Scope (Subp : Entity_Id) return Boolean;
10831 -- If the generic is a child unit, the parent has been installed on the
10832 -- scope stack, but a default subprogram cannot resolve to something
10833 -- on the parent because that parent is not really part of the visible
10834 -- context (it is there to resolve explicit local entities). If the
10835 -- default has resolved in this way, we remove the entity from immediate
10836 -- visibility and analyze the node again to emit an error message or
10837 -- find another visible candidate.
10839 procedure Valid_Actual_Subprogram (Act : Node_Id);
10840 -- Perform legality check and raise exception on failure
10842 -----------------------
10843 -- From_Parent_Scope --
10844 -----------------------
10846 function From_Parent_Scope (Subp : Entity_Id) return Boolean is
10847 Gen_Scope : Node_Id;
10849 begin
10850 Gen_Scope := Scope (Analyzed_S);
10851 while Present (Gen_Scope) and then Is_Child_Unit (Gen_Scope) loop
10852 if Scope (Subp) = Scope (Gen_Scope) then
10853 return True;
10854 end if;
10856 Gen_Scope := Scope (Gen_Scope);
10857 end loop;
10859 return False;
10860 end From_Parent_Scope;
10862 -----------------------------
10863 -- Valid_Actual_Subprogram --
10864 -----------------------------
10866 procedure Valid_Actual_Subprogram (Act : Node_Id) is
10867 Act_E : Entity_Id;
10869 begin
10870 if Is_Entity_Name (Act) then
10871 Act_E := Entity (Act);
10873 elsif Nkind (Act) = N_Selected_Component
10874 and then Is_Entity_Name (Selector_Name (Act))
10875 then
10876 Act_E := Entity (Selector_Name (Act));
10878 else
10879 Act_E := Empty;
10880 end if;
10882 if (Present (Act_E) and then Is_Overloadable (Act_E))
10883 or else Nkind (Act) in N_Attribute_Reference
10884 | N_Indexed_Component
10885 | N_Character_Literal
10886 | N_Explicit_Dereference
10887 then
10888 return;
10889 end if;
10891 Error_Msg_NE
10892 ("expect subprogram or entry name in instantiation of &",
10893 Instantiation_Node, Formal_Sub);
10894 Abandon_Instantiation (Instantiation_Node);
10895 end Valid_Actual_Subprogram;
10897 -- Local variables
10899 Decl_Node : Node_Id;
10900 Loc : Source_Ptr;
10901 Nam : Node_Id;
10902 New_Spec : Node_Id;
10903 New_Subp : Entity_Id;
10905 -- Start of processing for Instantiate_Formal_Subprogram
10907 begin
10908 New_Spec := New_Copy_Tree (Specification (Formal));
10910 -- The tree copy has created the proper instantiation sloc for the
10911 -- new specification. Use this location for all other constructed
10912 -- declarations.
10914 Loc := Sloc (Defining_Unit_Name (New_Spec));
10916 -- Create new entity for the actual (New_Copy_Tree does not), and
10917 -- indicate that it is an actual.
10919 -- If the actual is not an entity (i.e. an attribute reference)
10920 -- and the formal includes aspect specifications for contracts,
10921 -- we create an internal name for the renaming declaration. The
10922 -- constructed wrapper contains a call to the entity in the renaming.
10923 -- This is an expansion activity, as is the wrapper creation.
10925 if Ada_Version >= Ada_2022
10926 and then Has_Contracts (Analyzed_Formal)
10927 and then not Is_Entity_Name (Actual)
10928 and then Expander_Active
10929 then
10930 New_Subp := Make_Temporary (Sloc (Actual), 'S');
10931 else
10932 New_Subp := Make_Defining_Identifier (Loc, Chars (Formal_Sub));
10933 end if;
10935 Mutate_Ekind (New_Subp, Ekind (Analyzed_S));
10936 Set_Is_Generic_Actual_Subprogram (New_Subp);
10937 Set_Defining_Unit_Name (New_Spec, New_Subp);
10939 -- Create new entities for the each of the formals in the specification
10940 -- of the renaming declaration built for the actual.
10942 if Present (Parameter_Specifications (New_Spec)) then
10943 declare
10944 F : Node_Id;
10945 F_Id : Entity_Id;
10947 begin
10948 F := First (Parameter_Specifications (New_Spec));
10949 while Present (F) loop
10950 F_Id := Defining_Identifier (F);
10952 Set_Defining_Identifier (F,
10953 Make_Defining_Identifier (Sloc (F_Id), Chars (F_Id)));
10954 Next (F);
10955 end loop;
10956 end;
10957 end if;
10959 -- Find entity of actual. If the actual is an attribute reference, it
10960 -- cannot be resolved here (its formal is missing) but is handled
10961 -- instead in Attribute_Renaming. If the actual is overloaded, it is
10962 -- fully resolved subsequently, when the renaming declaration for the
10963 -- formal is analyzed. If it is an explicit dereference, resolve the
10964 -- prefix but not the actual itself, to prevent interpretation as call.
10966 if Present (Actual) then
10967 Loc := Sloc (Actual);
10968 Set_Sloc (New_Spec, Loc);
10970 if Nkind (Actual) = N_Operator_Symbol then
10971 Find_Direct_Name (Actual);
10973 elsif Nkind (Actual) = N_Explicit_Dereference then
10974 Analyze (Prefix (Actual));
10976 elsif Nkind (Actual) /= N_Attribute_Reference then
10977 Analyze (Actual);
10978 end if;
10980 Valid_Actual_Subprogram (Actual);
10981 Nam := Actual;
10983 elsif Present (Default_Name (Formal)) then
10984 if Nkind (Default_Name (Formal)) not in N_Attribute_Reference
10985 | N_Selected_Component
10986 | N_Indexed_Component
10987 | N_Character_Literal
10988 and then Present (Entity (Default_Name (Formal)))
10989 then
10990 Nam := New_Occurrence_Of (Entity (Default_Name (Formal)), Loc);
10991 else
10992 Nam := New_Copy (Default_Name (Formal));
10993 Set_Sloc (Nam, Loc);
10994 end if;
10996 elsif Box_Present (Formal) then
10998 -- Actual is resolved at the point of instantiation. Create an
10999 -- identifier or operator with the same name as the formal.
11001 if Nkind (Formal_Sub) = N_Defining_Operator_Symbol then
11002 Nam :=
11003 Make_Operator_Symbol (Loc,
11004 Chars => Chars (Formal_Sub),
11005 Strval => No_String);
11006 else
11007 Nam := Make_Identifier (Loc, Chars (Formal_Sub));
11008 end if;
11010 elsif Nkind (Specification (Formal)) = N_Procedure_Specification
11011 and then Null_Present (Specification (Formal))
11012 then
11013 -- Generate null body for procedure, for use in the instance
11015 Decl_Node :=
11016 Make_Subprogram_Body (Loc,
11017 Specification => New_Spec,
11018 Declarations => New_List,
11019 Handled_Statement_Sequence =>
11020 Make_Handled_Sequence_Of_Statements (Loc,
11021 Statements => New_List (Make_Null_Statement (Loc))));
11023 -- RM 12.6 (16.2/2): The procedure has convention Intrinsic
11025 Set_Convention (Defining_Unit_Name (New_Spec), Convention_Intrinsic);
11027 -- Eliminate the calls to it when optimization is enabled
11029 Set_Is_Inlined (Defining_Unit_Name (New_Spec));
11030 return Decl_Node;
11032 -- Handle case of a formal function with an expression default (allowed
11033 -- when extensions are enabled).
11035 elsif Nkind (Specification (Formal)) = N_Function_Specification
11036 and then Present (Expression (Formal))
11037 then
11038 -- Generate body for function, for use in the instance
11040 declare
11041 Expr : constant Node_Id := New_Copy (Expression (Formal));
11042 Stmt : constant Node_Id := Make_Simple_Return_Statement (Loc);
11043 begin
11044 Set_Sloc (Expr, Loc);
11045 Set_Expression (Stmt, Expr);
11047 Decl_Node :=
11048 Make_Subprogram_Body (Loc,
11049 Specification => New_Spec,
11050 Declarations => New_List,
11051 Handled_Statement_Sequence =>
11052 Make_Handled_Sequence_Of_Statements (Loc,
11053 Statements => New_List (Stmt)));
11054 end;
11056 -- RM 12.6 (16.2/2): Like a null procedure default, the function
11057 -- has convention Intrinsic.
11059 Set_Convention (Defining_Unit_Name (New_Spec), Convention_Intrinsic);
11061 -- Inline calls to it when optimization is enabled
11063 Set_Is_Inlined (Defining_Unit_Name (New_Spec));
11064 return Decl_Node;
11066 else
11067 Error_Msg_Sloc := Sloc (Scope (Analyzed_S));
11068 Error_Msg_NE
11069 ("missing actual&", Instantiation_Node, Formal_Sub);
11070 Error_Msg_NE
11071 ("\in instantiation of & declared#",
11072 Instantiation_Node, Scope (Analyzed_S));
11073 Abandon_Instantiation (Instantiation_Node);
11074 end if;
11076 Decl_Node :=
11077 Make_Subprogram_Renaming_Declaration (Loc,
11078 Specification => New_Spec,
11079 Name => Nam);
11081 -- If we do not have an actual and the formal specified <> then set to
11082 -- get proper default.
11084 if No (Actual) and then Box_Present (Formal) then
11085 Set_From_Default (Decl_Node);
11086 end if;
11088 -- Gather possible interpretations for the actual before analyzing the
11089 -- instance. If overloaded, it will be resolved when analyzing the
11090 -- renaming declaration.
11092 if Box_Present (Formal) and then No (Actual) then
11093 Analyze (Nam);
11095 if Is_Child_Unit (Scope (Analyzed_S))
11096 and then Present (Entity (Nam))
11097 then
11098 if not Is_Overloaded (Nam) then
11099 if From_Parent_Scope (Entity (Nam)) then
11100 Set_Is_Immediately_Visible (Entity (Nam), False);
11101 Set_Entity (Nam, Empty);
11102 Set_Etype (Nam, Empty);
11104 Analyze (Nam);
11105 Set_Is_Immediately_Visible (Entity (Nam));
11106 end if;
11108 else
11109 declare
11110 I : Interp_Index;
11111 It : Interp;
11113 begin
11114 Get_First_Interp (Nam, I, It);
11115 while Present (It.Nam) loop
11116 if From_Parent_Scope (It.Nam) then
11117 Remove_Interp (I);
11118 end if;
11120 Get_Next_Interp (I, It);
11121 end loop;
11122 end;
11123 end if;
11124 end if;
11125 end if;
11127 -- The generic instantiation freezes the actual. This can only be done
11128 -- once the actual is resolved, in the analysis of the renaming
11129 -- declaration. To make the formal subprogram entity available, we set
11130 -- Corresponding_Formal_Spec to point to the formal subprogram entity.
11131 -- This is also needed in Analyze_Subprogram_Renaming for the processing
11132 -- of formal abstract subprograms.
11134 Set_Corresponding_Formal_Spec (Decl_Node, Analyzed_S);
11136 -- We cannot analyze the renaming declaration, and thus find the actual,
11137 -- until all the actuals are assembled in the instance. For subsequent
11138 -- checks of other actuals, indicate the node that will hold the
11139 -- instance of this formal.
11141 Set_Instance_Of (Analyzed_S, Nam);
11143 if Nkind (Actual) = N_Selected_Component
11144 and then Is_Task_Type (Etype (Prefix (Actual)))
11145 and then not Is_Frozen (Etype (Prefix (Actual)))
11146 then
11147 -- The renaming declaration will create a body, which must appear
11148 -- outside of the instantiation, We move the renaming declaration
11149 -- out of the instance, and create an additional renaming inside,
11150 -- to prevent freezing anomalies.
11152 declare
11153 Anon_Id : constant Entity_Id := Make_Temporary (Loc, 'E');
11155 begin
11156 Set_Defining_Unit_Name (New_Spec, Anon_Id);
11157 Insert_Before (Instantiation_Node, Decl_Node);
11158 Analyze (Decl_Node);
11160 -- Now create renaming within the instance
11162 Decl_Node :=
11163 Make_Subprogram_Renaming_Declaration (Loc,
11164 Specification => New_Copy_Tree (New_Spec),
11165 Name => New_Occurrence_Of (Anon_Id, Loc));
11167 Set_Defining_Unit_Name (Specification (Decl_Node),
11168 Make_Defining_Identifier (Loc, Chars (Formal_Sub)));
11169 end;
11170 end if;
11172 return Decl_Node;
11173 end Instantiate_Formal_Subprogram;
11175 ------------------------
11176 -- Instantiate_Object --
11177 ------------------------
11179 function Instantiate_Object
11180 (Formal : Node_Id;
11181 Actual : Node_Id;
11182 Analyzed_Formal : Node_Id) return List_Id
11184 Gen_Obj : constant Entity_Id := Defining_Identifier (Formal);
11185 A_Gen_Obj : constant Entity_Id :=
11186 Defining_Identifier (Analyzed_Formal);
11187 Acc_Def : Node_Id := Empty;
11188 Act_Assoc : constant Node_Id :=
11189 (if No (Actual) then Empty else Parent (Actual));
11190 Actual_Decl : Node_Id := Empty;
11191 Decl_Node : Node_Id;
11192 Def : Node_Id;
11193 Ftyp : Entity_Id;
11194 List : constant List_Id := New_List;
11195 Loc : constant Source_Ptr := Sloc (Actual);
11196 Orig_Ftyp : constant Entity_Id := Etype (A_Gen_Obj);
11197 Subt_Decl : Node_Id := Empty;
11198 Subt_Mark : Node_Id := Empty;
11200 -- Start of processing for Instantiate_Object
11202 begin
11203 -- Formal may be an anonymous access
11205 if Present (Subtype_Mark (Formal)) then
11206 Subt_Mark := Subtype_Mark (Formal);
11207 else
11208 Check_Access_Definition (Formal);
11209 Acc_Def := Access_Definition (Formal);
11210 end if;
11212 -- Sloc for error message on missing actual
11214 Error_Msg_Sloc := Sloc (Scope (A_Gen_Obj));
11216 if Get_Instance_Of (Gen_Obj) /= Gen_Obj then
11217 Error_Msg_N ("duplicate instantiation of generic parameter", Actual);
11218 end if;
11220 Set_Parent (List, Act_Assoc);
11222 -- OUT present
11224 if Out_Present (Formal) then
11226 -- An IN OUT generic actual must be a name. The instantiation is a
11227 -- renaming declaration. The actual is the name being renamed. We
11228 -- use the actual directly, rather than a copy, because it is not
11229 -- used further in the list of actuals, and because a copy or a use
11230 -- of relocate_node is incorrect if the instance is nested within a
11231 -- generic. In order to simplify e.g. ASIS queries, the
11232 -- Generic_Parent field links the declaration to the generic
11233 -- association.
11235 if No (Actual) then
11236 Error_Msg_NE
11237 ("missing actual &",
11238 Instantiation_Node, Gen_Obj);
11239 Error_Msg_NE
11240 ("\in instantiation of & declared#",
11241 Instantiation_Node, Scope (A_Gen_Obj));
11242 Abandon_Instantiation (Instantiation_Node);
11243 end if;
11245 if Present (Subt_Mark) then
11246 Decl_Node :=
11247 Make_Object_Renaming_Declaration (Loc,
11248 Defining_Identifier => New_Copy (Gen_Obj),
11249 Subtype_Mark => New_Copy_Tree (Subt_Mark),
11250 Name => Actual);
11252 else pragma Assert (Present (Acc_Def));
11253 Decl_Node :=
11254 Make_Object_Renaming_Declaration (Loc,
11255 Defining_Identifier => New_Copy (Gen_Obj),
11256 Access_Definition => New_Copy_Tree (Acc_Def),
11257 Name => Actual);
11258 end if;
11260 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
11262 -- The analysis of the actual may produce Insert_Action nodes, so
11263 -- the declaration must have a context in which to attach them.
11265 Append (Decl_Node, List);
11266 Analyze (Actual);
11268 -- Return if the analysis of the actual reported some error
11270 if Etype (Actual) = Any_Type then
11271 return List;
11272 end if;
11274 -- This check is performed here because Analyze_Object_Renaming will
11275 -- not check it when Comes_From_Source is False. Note though that the
11276 -- check for the actual being the name of an object will be performed
11277 -- in Analyze_Object_Renaming.
11279 if Is_Object_Reference (Actual)
11280 and then Is_Dependent_Component_Of_Mutable_Object (Actual)
11281 then
11282 Error_Msg_N
11283 ("illegal discriminant-dependent component for in out parameter",
11284 Actual);
11285 end if;
11287 -- The actual has to be resolved in order to check that it is a
11288 -- variable (due to cases such as F (1), where F returns access to
11289 -- an array, and for overloaded prefixes).
11291 Ftyp := Get_Instance_Of (Etype (A_Gen_Obj));
11293 -- If the type of the formal is not itself a formal, and the current
11294 -- unit is a child unit, the formal type must be declared in a
11295 -- parent, and must be retrieved by visibility.
11297 if Ftyp = Orig_Ftyp
11298 and then Is_Generic_Unit (Scope (Ftyp))
11299 and then Is_Child_Unit (Scope (A_Gen_Obj))
11300 then
11301 declare
11302 Temp : constant Node_Id :=
11303 New_Copy_Tree (Subtype_Mark (Analyzed_Formal));
11304 begin
11305 Set_Entity (Temp, Empty);
11306 Find_Type (Temp);
11307 Ftyp := Entity (Temp);
11308 end;
11309 end if;
11311 if Is_Private_Type (Ftyp)
11312 and then not Is_Private_Type (Etype (Actual))
11313 and then (Base_Type (Full_View (Ftyp)) = Base_Type (Etype (Actual))
11314 or else Base_Type (Etype (Actual)) = Ftyp)
11315 then
11316 -- If the actual has the type of the full view of the formal, or
11317 -- else a non-private subtype of the formal, then the visibility
11318 -- of the formal type has changed. Add to the actuals a subtype
11319 -- declaration that will force the exchange of views in the body
11320 -- of the instance as well.
11322 Subt_Decl :=
11323 Make_Subtype_Declaration (Loc,
11324 Defining_Identifier => Make_Temporary (Loc, 'P'),
11325 Subtype_Indication => New_Occurrence_Of (Ftyp, Loc));
11327 Prepend (Subt_Decl, List);
11329 Prepend_Elmt (Full_View (Ftyp), Exchanged_Views);
11330 Exchange_Declarations (Ftyp);
11331 end if;
11333 Resolve (Actual, Ftyp);
11335 if not Denotes_Variable (Actual) then
11336 Error_Msg_NE ("actual for& must be a variable", Actual, Gen_Obj);
11338 elsif Base_Type (Ftyp) /= Base_Type (Etype (Actual)) then
11340 -- Ada 2005 (AI-423): For a generic formal object of mode in out,
11341 -- the type of the actual shall resolve to a specific anonymous
11342 -- access type.
11344 if Ada_Version < Ada_2005
11345 or else not Is_Anonymous_Access_Type (Base_Type (Ftyp))
11346 or else not Is_Anonymous_Access_Type (Base_Type (Etype (Actual)))
11347 then
11348 Error_Msg_NE
11349 ("type of actual does not match type of&", Actual, Gen_Obj);
11350 end if;
11351 end if;
11353 Note_Possible_Modification (Actual, Sure => True);
11355 -- Check for instantiation with atomic/volatile/VFA object actual for
11356 -- nonatomic/nonvolatile/nonVFA formal (RM C.6 (12)).
11358 if Is_Atomic_Object (Actual) and then not Is_Atomic (Orig_Ftyp) then
11359 Error_Msg_NE
11360 ("cannot instantiate nonatomic formal & of mode in out",
11361 Actual, Gen_Obj);
11362 Error_Msg_N ("\with atomic object actual (RM C.6(12))", Actual);
11364 elsif Is_Volatile_Object_Ref (Actual)
11365 and then not Is_Volatile (Orig_Ftyp)
11366 then
11367 Error_Msg_NE
11368 ("cannot instantiate nonvolatile formal & of mode in out",
11369 Actual, Gen_Obj);
11370 Error_Msg_N ("\with volatile object actual (RM C.6(12))", Actual);
11372 elsif Is_Volatile_Full_Access_Object_Ref (Actual)
11373 and then not Is_Volatile_Full_Access (Orig_Ftyp)
11374 then
11375 Error_Msg_NE
11376 ("cannot instantiate nonfull access formal & of mode in out",
11377 Actual, Gen_Obj);
11378 Error_Msg_N
11379 ("\with full access object actual (RM C.6(12))", Actual);
11380 end if;
11382 -- Check for instantiation on nonatomic subcomponent of a full access
11383 -- object in Ada 2022 (RM C.6 (12)).
11385 if Ada_Version >= Ada_2022
11386 and then Is_Subcomponent_Of_Full_Access_Object (Actual)
11387 and then not Is_Atomic_Object (Actual)
11388 then
11389 Error_Msg_NE
11390 ("cannot instantiate formal & of mode in out with actual",
11391 Actual, Gen_Obj);
11392 Error_Msg_N
11393 ("\nonatomic subcomponent of full access object (RM C.6(12))",
11394 Actual);
11395 end if;
11397 -- Check actual/formal compatibility with respect to the four
11398 -- volatility refinement aspects.
11400 declare
11401 Actual_Obj : Entity_Id;
11402 N : Node_Id := Actual;
11403 begin
11404 -- Similar to Sem_Util.Get_Enclosing_Object, but treat
11405 -- pointer dereference like component selection.
11406 loop
11407 if Is_Entity_Name (N) then
11408 Actual_Obj := Entity (N);
11409 exit;
11410 end if;
11412 case Nkind (N) is
11413 when N_Indexed_Component
11414 | N_Selected_Component
11415 | N_Slice
11416 | N_Explicit_Dereference
11418 N := Prefix (N);
11420 when N_Type_Conversion =>
11421 N := Expression (N);
11423 when others =>
11424 Actual_Obj := Etype (N);
11425 exit;
11426 end case;
11427 end loop;
11429 Check_Volatility_Compatibility
11430 (Actual_Obj, A_Gen_Obj, "actual object",
11431 "its corresponding formal object of mode in out",
11432 Srcpos_Bearer => Actual);
11433 end;
11435 -- Formal in-parameter
11437 else
11438 -- The instantiation of a generic formal in-parameter is constant
11439 -- declaration. The actual is the expression for that declaration.
11440 -- Its type is a full copy of the type of the formal. This may be
11441 -- an access to subprogram, for which we need to generate entities
11442 -- for the formals in the new signature.
11444 if Present (Actual) then
11445 if Present (Subt_Mark) then
11446 Def := New_Copy_Tree (Subt_Mark);
11447 else
11448 pragma Assert (Present (Acc_Def));
11449 Def := New_Copy_Tree (Acc_Def);
11450 end if;
11452 Decl_Node :=
11453 Make_Object_Declaration (Loc,
11454 Defining_Identifier => New_Copy (Gen_Obj),
11455 Constant_Present => True,
11456 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
11457 Object_Definition => Def,
11458 Expression => Actual);
11460 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
11462 -- A generic formal object of a tagged type is defined to be
11463 -- aliased so the new constant must also be treated as aliased.
11465 if Is_Tagged_Type (Etype (A_Gen_Obj)) then
11466 Set_Aliased_Present (Decl_Node);
11467 end if;
11469 Append (Decl_Node, List);
11471 -- No need to repeat (pre-)analysis of some expression nodes
11472 -- already handled in Preanalyze_Actuals.
11474 if Nkind (Actual) /= N_Allocator then
11475 Analyze (Actual);
11477 -- Return if the analysis of the actual reported some error
11479 if Etype (Actual) = Any_Type then
11480 return List;
11481 end if;
11482 end if;
11484 declare
11485 Formal_Type : constant Entity_Id := Etype (A_Gen_Obj);
11486 Typ : Entity_Id;
11488 begin
11489 Typ := Get_Instance_Of (Formal_Type);
11491 -- If the actual appears in the current or an enclosing scope,
11492 -- use its type directly. This is relevant if it has an actual
11493 -- subtype that is distinct from its nominal one. This cannot
11494 -- be done in general because the type of the actual may
11495 -- depend on other actuals, and only be fully determined when
11496 -- the enclosing instance is analyzed.
11498 if Present (Etype (Actual))
11499 and then Is_Constr_Subt_For_U_Nominal (Etype (Actual))
11500 then
11501 Freeze_Before (Instantiation_Node, Etype (Actual));
11502 else
11503 Freeze_Before (Instantiation_Node, Typ);
11504 end if;
11506 -- If the actual is an aggregate, perform name resolution on
11507 -- its components (the analysis of an aggregate does not do it)
11508 -- to capture local names that may be hidden if the generic is
11509 -- a child unit.
11511 if Nkind (Actual) = N_Aggregate then
11512 Preanalyze_And_Resolve (Actual, Typ);
11513 end if;
11515 if Is_Limited_Type (Typ)
11516 and then not OK_For_Limited_Init (Typ, Actual)
11517 then
11518 Error_Msg_N
11519 ("initialization not allowed for limited types", Actual);
11520 Explain_Limited_Type (Typ, Actual);
11521 end if;
11522 end;
11524 elsif Present (Default_Expression (Formal)) then
11526 -- Use default to construct declaration
11528 if Present (Subt_Mark) then
11529 Def := New_Copy_Tree (Subt_Mark);
11530 else
11531 pragma Assert (Present (Acc_Def));
11532 Def := New_Copy_Tree (Acc_Def);
11533 end if;
11535 Decl_Node :=
11536 Make_Object_Declaration (Sloc (Formal),
11537 Defining_Identifier => New_Copy (Gen_Obj),
11538 Constant_Present => True,
11539 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
11540 Object_Definition => Def,
11541 Expression => New_Copy_Tree
11542 (Default_Expression (Formal)));
11544 Set_Corresponding_Generic_Association
11545 (Decl_Node, Expression (Decl_Node));
11547 Append (Decl_Node, List);
11548 Set_Analyzed (Expression (Decl_Node), False);
11550 else
11551 Error_Msg_NE ("missing actual&", Instantiation_Node, Gen_Obj);
11552 Error_Msg_NE ("\in instantiation of & declared#",
11553 Instantiation_Node, Scope (A_Gen_Obj));
11555 if Is_Scalar_Type (Etype (A_Gen_Obj)) then
11557 -- Create dummy constant declaration so that instance can be
11558 -- analyzed, to minimize cascaded visibility errors.
11560 if Present (Subt_Mark) then
11561 Def := Subt_Mark;
11562 else pragma Assert (Present (Acc_Def));
11563 Def := Acc_Def;
11564 end if;
11566 Decl_Node :=
11567 Make_Object_Declaration (Loc,
11568 Defining_Identifier => New_Copy (Gen_Obj),
11569 Constant_Present => True,
11570 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
11571 Object_Definition => New_Copy (Def),
11572 Expression =>
11573 Make_Attribute_Reference (Sloc (Gen_Obj),
11574 Attribute_Name => Name_First,
11575 Prefix => New_Copy (Def)));
11577 Append (Decl_Node, List);
11579 else
11580 Abandon_Instantiation (Instantiation_Node);
11581 end if;
11582 end if;
11583 end if;
11585 if Nkind (Actual) in N_Has_Entity
11586 and then Present (Entity (Actual))
11587 then
11588 Actual_Decl := Parent (Entity (Actual));
11589 end if;
11591 -- Ada 2005 (AI-423) refined by AI12-0287:
11592 -- For an object_renaming_declaration with a null_exclusion or an
11593 -- access_definition that has a null_exclusion, the subtype of the
11594 -- object_name shall exclude null. In addition, if the
11595 -- object_renaming_declaration occurs within the body of a generic unit
11596 -- G or within the body of a generic unit declared within the
11597 -- declarative region of generic unit G, then:
11598 -- * if the object_name statically denotes a generic formal object of
11599 -- mode in out of G, then the declaration of that object shall have a
11600 -- null_exclusion;
11601 -- * if the object_name statically denotes a call of a generic formal
11602 -- function of G, then the declaration of the result of that function
11603 -- shall have a null_exclusion.
11605 if Ada_Version >= Ada_2005
11606 and then Present (Actual_Decl)
11607 and then Nkind (Actual_Decl) in N_Formal_Object_Declaration
11608 | N_Object_Declaration
11609 and then Nkind (Analyzed_Formal) = N_Formal_Object_Declaration
11610 and then not Has_Null_Exclusion (Actual_Decl)
11611 and then Has_Null_Exclusion (Analyzed_Formal)
11612 and then Ekind (Defining_Identifier (Analyzed_Formal))
11613 = E_Generic_In_Out_Parameter
11614 and then ((In_Generic_Scope (Entity (Actual))
11615 and then In_Package_Body (Scope (Entity (Actual))))
11616 or else not Can_Never_Be_Null (Etype (Actual)))
11617 then
11618 Error_Msg_Sloc := Sloc (Analyzed_Formal);
11619 Error_Msg_N
11620 ("actual must exclude null to match generic formal#", Actual);
11621 end if;
11623 -- An effectively volatile object cannot be used as an actual in a
11624 -- generic instantiation (SPARK RM 7.1.3(7)). The following check is
11625 -- relevant only when SPARK_Mode is on as it is not a standard Ada
11626 -- legality rule, and also verifies that the actual is an object.
11628 if SPARK_Mode = On
11629 and then Present (Actual)
11630 and then Is_Object_Reference (Actual)
11631 and then Is_Effectively_Volatile_Object (Actual)
11632 and then not Is_Effectively_Volatile (A_Gen_Obj)
11633 then
11634 Error_Msg_N
11635 ("volatile object cannot act as actual in generic instantiation",
11636 Actual);
11637 end if;
11639 return List;
11640 end Instantiate_Object;
11642 ------------------------------
11643 -- Instantiate_Package_Body --
11644 ------------------------------
11646 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
11647 -- must be replaced by gotos which jump to the end of the routine in order
11648 -- to restore the Ghost and SPARK modes.
11650 procedure Instantiate_Package_Body
11651 (Body_Info : Pending_Body_Info;
11652 Inlined_Body : Boolean := False;
11653 Body_Optional : Boolean := False)
11655 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
11656 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Decl);
11657 Act_Spec : constant Node_Id := Specification (Act_Decl);
11658 Ctx_Parents : Elist_Id := No_Elist;
11659 Ctx_Top : Int := 0;
11660 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
11661 Gen_Id : constant Node_Id := Name (Inst_Node);
11662 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
11663 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
11664 Loc : constant Source_Ptr := Sloc (Inst_Node);
11666 procedure Check_Initialized_Types;
11667 -- In a generic package body, an entity of a generic private type may
11668 -- appear uninitialized. This is suspicious, unless the actual is a
11669 -- fully initialized type.
11671 procedure Install_Parents_Of_Generic_Context
11672 (Inst_Scope : Entity_Id;
11673 Ctx_Parents : out Elist_Id);
11674 -- Inst_Scope is the scope where the instance appears within; when it
11675 -- appears within a generic child package G, this routine collects and
11676 -- installs the enclosing packages of G in the scopes stack; installed
11677 -- packages are returned in Ctx_Parents.
11679 procedure Remove_Parents_Of_Generic_Context (Ctx_Parents : Elist_Id);
11680 -- Reverse effect after instantiation is complete
11682 -----------------------------
11683 -- Check_Initialized_Types --
11684 -----------------------------
11686 procedure Check_Initialized_Types is
11687 Decl : Node_Id;
11688 Formal : Entity_Id;
11689 Actual : Entity_Id;
11690 Uninit_Var : Entity_Id;
11692 begin
11693 Decl := First (Generic_Formal_Declarations (Gen_Decl));
11694 while Present (Decl) loop
11695 Uninit_Var := Empty;
11697 if Nkind (Decl) = N_Private_Extension_Declaration then
11698 Uninit_Var := Uninitialized_Variable (Decl);
11700 elsif Nkind (Decl) = N_Formal_Type_Declaration
11701 and then Nkind (Formal_Type_Definition (Decl)) =
11702 N_Formal_Private_Type_Definition
11703 then
11704 Uninit_Var :=
11705 Uninitialized_Variable (Formal_Type_Definition (Decl));
11706 end if;
11708 if Present (Uninit_Var) then
11709 Formal := Defining_Identifier (Decl);
11710 Actual := First_Entity (Act_Decl_Id);
11712 -- For each formal there is a subtype declaration that renames
11713 -- the actual and has the same name as the formal. Locate the
11714 -- formal for warning message about uninitialized variables
11715 -- in the generic, for which the actual type should be a fully
11716 -- initialized type.
11718 while Present (Actual) loop
11719 exit when Ekind (Actual) = E_Package
11720 and then Present (Renamed_Entity (Actual));
11722 if Chars (Actual) = Chars (Formal)
11723 and then not Is_Scalar_Type (Actual)
11724 and then not Is_Fully_Initialized_Type (Actual)
11725 and then Warn_On_No_Value_Assigned
11726 then
11727 Error_Msg_Node_2 := Formal;
11728 Error_Msg_NE
11729 ("generic unit has uninitialized variable& of "
11730 & "formal private type &?v?", Actual, Uninit_Var);
11731 Error_Msg_NE
11732 ("actual type for& should be fully initialized type?v?",
11733 Actual, Formal);
11734 exit;
11735 end if;
11737 Next_Entity (Actual);
11738 end loop;
11739 end if;
11741 Next (Decl);
11742 end loop;
11743 end Check_Initialized_Types;
11745 ----------------------------------------
11746 -- Install_Parents_Of_Generic_Context --
11747 ----------------------------------------
11749 procedure Install_Parents_Of_Generic_Context
11750 (Inst_Scope : Entity_Id;
11751 Ctx_Parents : out Elist_Id)
11753 Elmt : Elmt_Id;
11754 S : Entity_Id;
11756 begin
11757 Ctx_Parents := New_Elmt_List;
11759 -- Collect context parents (ie. parents where the instantiation
11760 -- appears within).
11762 S := Inst_Scope;
11763 while S /= Standard_Standard loop
11764 Prepend_Elmt (S, Ctx_Parents);
11765 S := Scope (S);
11766 end loop;
11768 -- Install enclosing parents
11770 Elmt := First_Elmt (Ctx_Parents);
11771 while Present (Elmt) loop
11772 Push_Scope (Node (Elmt));
11773 Set_Is_Immediately_Visible (Node (Elmt));
11774 Next_Elmt (Elmt);
11775 end loop;
11776 end Install_Parents_Of_Generic_Context;
11778 ---------------------------------------
11779 -- Remove_Parents_Of_Generic_Context --
11780 ---------------------------------------
11782 procedure Remove_Parents_Of_Generic_Context (Ctx_Parents : Elist_Id) is
11783 Elmt : Elmt_Id;
11785 begin
11786 -- Traverse Ctx_Parents in LIFO order to check the removed scopes
11788 Elmt := Last_Elmt (Ctx_Parents);
11789 while Present (Elmt) loop
11790 pragma Assert (Current_Scope = Node (Elmt));
11791 Set_Is_Immediately_Visible (Current_Scope, False);
11792 Pop_Scope;
11794 Remove_Last_Elmt (Ctx_Parents);
11795 Elmt := Last_Elmt (Ctx_Parents);
11796 end loop;
11797 end Remove_Parents_Of_Generic_Context;
11799 -- Local variables
11801 -- The following constants capture the context prior to instantiating
11802 -- the package body.
11804 Saved_CS : constant Config_Switches_Type := Save_Config_Switches;
11805 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
11806 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
11807 Saved_ISMP : constant Boolean :=
11808 Ignore_SPARK_Mode_Pragmas_In_Instance;
11809 Saved_LSST : constant Suppress_Stack_Entry_Ptr :=
11810 Local_Suppress_Stack_Top;
11811 Saved_SC : constant Boolean := Style_Check;
11812 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
11813 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
11814 Saved_SS : constant Suppress_Record := Scope_Suppress;
11815 Saved_Warn : constant Warning_Record := Save_Warnings;
11817 Act_Body : Node_Id;
11818 Act_Body_Id : Entity_Id;
11819 Act_Body_Name : Node_Id;
11820 Gen_Body : Node_Id;
11821 Gen_Body_Id : Node_Id;
11822 Par_Ent : Entity_Id := Empty;
11823 Par_Installed : Boolean := False;
11824 Par_Vis : Boolean := False;
11826 Scope_Check_Id : Entity_Id;
11827 Scope_Check_Last : Nat;
11828 -- Value of Current_Scope before calls to Install_Parents; used to check
11829 -- that scopes are correctly removed after instantiation.
11831 Vis_Prims_List : Elist_Id := No_Elist;
11832 -- List of primitives made temporarily visible in the instantiation
11833 -- to match the visibility of the formal type.
11835 -- Start of processing for Instantiate_Package_Body
11837 begin
11838 Gen_Body_Id := Corresponding_Body (Gen_Decl);
11840 -- The instance body may already have been processed, as the parent of
11841 -- another instance that is inlined (Load_Parent_Of_Generic).
11843 if Present (Corresponding_Body (Instance_Spec (Inst_Node))) then
11844 return;
11845 end if;
11847 -- The package being instantiated may be subject to pragma Ghost. Set
11848 -- the mode now to ensure that any nodes generated during instantiation
11849 -- are properly marked as Ghost.
11851 Set_Ghost_Mode (Act_Decl_Id);
11853 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
11855 -- Re-establish the state of information on which checks are suppressed.
11856 -- This information was set in Body_Info at the point of instantiation,
11857 -- and now we restore it so that the instance is compiled using the
11858 -- check status at the instantiation (RM 11.5(7.2/2), AI95-00224-01).
11860 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
11861 Scope_Suppress := Body_Info.Scope_Suppress;
11863 Restore_Config_Switches (Body_Info.Config_Switches);
11864 Restore_Warnings (Body_Info.Warnings);
11866 if No (Gen_Body_Id) then
11868 -- Do not look for parent of generic body if none is required.
11869 -- This may happen when the routine is called as part of the
11870 -- Pending_Instantiations processing, when nested instances
11871 -- may precede the one generated from the main unit.
11873 if not Unit_Requires_Body (Defining_Entity (Gen_Decl))
11874 and then Body_Optional
11875 then
11876 goto Leave;
11877 else
11878 Load_Parent_Of_Generic
11879 (Inst_Node, Specification (Gen_Decl), Body_Optional);
11881 -- Surprisingly enough, loading the body of the parent can cause
11882 -- the body to be instantiated and the double instantiation needs
11883 -- to be prevented in order to avoid giving bogus semantic errors.
11885 -- This case can occur because of the Collect_Previous_Instances
11886 -- machinery of Load_Parent_Of_Generic, which will instantiate
11887 -- bodies that are deemed to be ahead of the body of the parent
11888 -- in the compilation unit. But the relative position of these
11889 -- bodies is computed using the mere comparison of their Sloc.
11891 -- Now suppose that you have two generic packages G and H, with
11892 -- G containing a mere instantiation of H:
11894 -- generic
11895 -- package H is
11897 -- generic
11898 -- package Nested_G is
11899 -- ...
11900 -- end Nested_G;
11902 -- end H;
11904 -- with H;
11906 -- generic
11907 -- package G is
11909 -- package My_H is new H;
11911 -- end G;
11913 -- and a third package Q instantiating G and Nested_G:
11915 -- with G;
11917 -- package Q is
11919 -- package My_G is new G;
11921 -- package My_Nested_G is new My_G.My_H.Nested_G;
11923 -- end Q;
11925 -- The body to be instantiated is that of My_Nested_G and its
11926 -- parent is the instance My_G.My_H. This latter instantiation
11927 -- is done when My_G is analyzed, i.e. after the declarations
11928 -- of My_G and My_Nested_G have been parsed; as a result, the
11929 -- Sloc of My_G.My_H is greater than the Sloc of My_Nested_G.
11931 -- Therefore loading the body of My_G.My_H will cause the body
11932 -- of My_Nested_G to be instantiated because it is deemed to be
11933 -- ahead of My_G.My_H. This means that Load_Parent_Of_Generic
11934 -- will again be invoked on My_G.My_H, but this time with the
11935 -- Collect_Previous_Instances machinery disabled, so there is
11936 -- no endless mutual recursion and things are done in order.
11938 if Present (Corresponding_Body (Instance_Spec (Inst_Node))) then
11939 goto Leave;
11940 end if;
11942 Gen_Body_Id := Corresponding_Body (Gen_Decl);
11943 end if;
11944 end if;
11946 -- Establish global variable for sloc adjustment and for error recovery
11947 -- In the case of an instance body for an instantiation with actuals
11948 -- from a limited view, the instance body is placed at the beginning
11949 -- of the enclosing package body: use the body entity as the source
11950 -- location for nodes of the instance body.
11952 if not Is_Empty_Elmt_List (Incomplete_Actuals (Act_Decl_Id)) then
11953 declare
11954 Scop : constant Entity_Id := Scope (Act_Decl_Id);
11955 Body_Id : constant Node_Id :=
11956 Corresponding_Body (Unit_Declaration_Node (Scop));
11958 begin
11959 Instantiation_Node := Body_Id;
11960 end;
11961 else
11962 Instantiation_Node := Inst_Node;
11963 end if;
11965 if Present (Gen_Body_Id) then
11966 Save_Env (Gen_Unit, Act_Decl_Id);
11967 Style_Check := False;
11969 -- If the context of the instance is subject to SPARK_Mode "off", the
11970 -- annotation is missing, or the body is instantiated at a later pass
11971 -- and its spec ignored SPARK_Mode pragma, set the global flag which
11972 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within the
11973 -- instance.
11975 if SPARK_Mode /= On
11976 or else Ignore_SPARK_Mode_Pragmas (Act_Decl_Id)
11977 then
11978 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
11979 end if;
11981 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
11982 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
11984 Create_Instantiation_Source
11985 (Inst_Node, Gen_Body_Id, S_Adjustment);
11987 Act_Body :=
11988 Copy_Generic_Node
11989 (Original_Node (Gen_Body), Empty, Instantiating => True);
11991 -- Create proper (possibly qualified) defining name for the body, to
11992 -- correspond to the one in the spec.
11994 Act_Body_Id :=
11995 Make_Defining_Identifier (Sloc (Act_Decl_Id), Chars (Act_Decl_Id));
11996 Preserve_Comes_From_Source (Act_Body_Id, Act_Decl_Id);
11998 -- Some attributes of spec entity are not inherited by body entity
12000 Set_Handler_Records (Act_Body_Id, No_List);
12002 if Nkind (Defining_Unit_Name (Act_Spec)) =
12003 N_Defining_Program_Unit_Name
12004 then
12005 Act_Body_Name :=
12006 Make_Defining_Program_Unit_Name (Loc,
12007 Name =>
12008 New_Copy_Tree (Name (Defining_Unit_Name (Act_Spec))),
12009 Defining_Identifier => Act_Body_Id);
12010 else
12011 Act_Body_Name := Act_Body_Id;
12012 end if;
12014 Set_Defining_Unit_Name (Act_Body, Act_Body_Name);
12016 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
12017 Check_Generic_Actuals (Act_Decl_Id, False);
12018 Check_Initialized_Types;
12020 -- Install primitives hidden at the point of the instantiation but
12021 -- visible when processing the generic formals
12023 declare
12024 E : Entity_Id;
12026 begin
12027 E := First_Entity (Act_Decl_Id);
12028 while Present (E) loop
12029 if Is_Type (E)
12030 and then not Is_Itype (E)
12031 and then Is_Generic_Actual_Type (E)
12032 and then Is_Tagged_Type (E)
12033 then
12034 Install_Hidden_Primitives
12035 (Prims_List => Vis_Prims_List,
12036 Gen_T => Generic_Parent_Type (Parent (E)),
12037 Act_T => E);
12038 end if;
12040 Next_Entity (E);
12041 end loop;
12042 end;
12044 Scope_Check_Id := Current_Scope;
12045 Scope_Check_Last := Scope_Stack.Last;
12047 -- If the instantiation appears within a generic child some actual
12048 -- parameter may be the current instance of the enclosing generic
12049 -- parent.
12051 declare
12052 Inst_Scope : constant Entity_Id := Scope (Act_Decl_Id);
12054 begin
12055 if Is_Child_Unit (Inst_Scope)
12056 and then Ekind (Inst_Scope) = E_Generic_Package
12057 and then Present (Generic_Associations (Inst_Node))
12058 then
12059 Install_Parents_Of_Generic_Context (Inst_Scope, Ctx_Parents);
12061 -- Hide them from visibility; required to avoid conflicts
12062 -- installing the parent instance.
12064 if Present (Ctx_Parents) then
12065 Push_Scope (Standard_Standard);
12066 Ctx_Top := Scope_Stack.Last;
12067 Scope_Stack.Table (Ctx_Top).Is_Active_Stack_Base := True;
12068 end if;
12069 end if;
12070 end;
12072 -- If it is a child unit, make the parent instance (which is an
12073 -- instance of the parent of the generic) visible. The parent
12074 -- instance is the prefix of the name of the generic unit.
12076 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
12077 and then Nkind (Gen_Id) = N_Expanded_Name
12078 then
12079 Par_Ent := Entity (Prefix (Gen_Id));
12080 Par_Vis := Is_Immediately_Visible (Par_Ent);
12081 Install_Parent (Par_Ent, In_Body => True);
12082 Par_Installed := True;
12084 elsif Is_Child_Unit (Gen_Unit) then
12085 Par_Ent := Scope (Gen_Unit);
12086 Par_Vis := Is_Immediately_Visible (Par_Ent);
12087 Install_Parent (Par_Ent, In_Body => True);
12088 Par_Installed := True;
12089 end if;
12091 -- If the instantiation is a library unit, and this is the main unit,
12092 -- then build the resulting compilation unit nodes for the instance.
12093 -- If this is a compilation unit but it is not the main unit, then it
12094 -- is the body of a unit in the context, that is being compiled
12095 -- because it is encloses some inlined unit or another generic unit
12096 -- being instantiated. In that case, this body is not part of the
12097 -- current compilation, and is not attached to the tree, but its
12098 -- parent must be set for analysis.
12100 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
12102 -- Replace instance node with body of instance, and create new
12103 -- node for corresponding instance declaration.
12105 Build_Instance_Compilation_Unit_Nodes
12106 (Inst_Node, Act_Body, Act_Decl);
12108 -- If the instantiation appears within a generic child package
12109 -- enable visibility of current instance of enclosing generic
12110 -- parents.
12112 if Present (Ctx_Parents) then
12113 Scope_Stack.Table (Ctx_Top).Is_Active_Stack_Base := False;
12114 Analyze (Inst_Node);
12115 Scope_Stack.Table (Ctx_Top).Is_Active_Stack_Base := True;
12116 else
12117 Analyze (Inst_Node);
12118 end if;
12120 if Parent (Inst_Node) = Cunit (Main_Unit) then
12122 -- If the instance is a child unit itself, then set the scope
12123 -- of the expanded body to be the parent of the instantiation
12124 -- (ensuring that the fully qualified name will be generated
12125 -- for the elaboration subprogram).
12127 if Nkind (Defining_Unit_Name (Act_Spec)) =
12128 N_Defining_Program_Unit_Name
12129 then
12130 Set_Scope (Defining_Entity (Inst_Node), Scope (Act_Decl_Id));
12131 end if;
12132 end if;
12134 -- Case where instantiation is not a library unit
12136 else
12137 -- Handle the case of an instance with incomplete actual types.
12138 -- The instance body cannot be placed just after the declaration
12139 -- because full views have not been seen yet. Any use of the non-
12140 -- limited views in the instance body requires the presence of a
12141 -- regular with_clause in the enclosing unit. Therefore we place
12142 -- the instance body at the beginning of the enclosing body, and
12143 -- the freeze node for the instance is then placed after the body.
12145 if not Is_Empty_Elmt_List (Incomplete_Actuals (Act_Decl_Id))
12146 and then Ekind (Scope (Act_Decl_Id)) = E_Package
12147 then
12148 declare
12149 Scop : constant Entity_Id := Scope (Act_Decl_Id);
12150 Body_Id : constant Node_Id :=
12151 Corresponding_Body (Unit_Declaration_Node (Scop));
12153 F_Node : Node_Id;
12155 begin
12156 pragma Assert (Present (Body_Id));
12158 Prepend (Act_Body, Declarations (Parent (Body_Id)));
12160 if Expander_Active then
12161 Ensure_Freeze_Node (Act_Decl_Id);
12162 F_Node := Freeze_Node (Act_Decl_Id);
12163 Set_Is_Frozen (Act_Decl_Id, False);
12164 if Is_List_Member (F_Node) then
12165 Remove (F_Node);
12166 end if;
12168 Insert_After (Act_Body, F_Node);
12169 end if;
12170 end;
12172 else
12173 Insert_Before (Inst_Node, Act_Body);
12174 Mark_Rewrite_Insertion (Act_Body);
12176 -- Insert the freeze node for the instance if need be
12178 if Expander_Active then
12179 Freeze_Package_Instance
12180 (Inst_Node, Gen_Body, Gen_Decl, Act_Decl_Id);
12181 Set_Is_Frozen (Act_Decl_Id);
12182 end if;
12183 end if;
12185 -- If the instantiation appears within a generic child package
12186 -- enable visibility of current instance of enclosing generic
12187 -- parents.
12189 if Present (Ctx_Parents) then
12190 Scope_Stack.Table (Ctx_Top).Is_Active_Stack_Base := False;
12191 Analyze (Act_Body);
12192 Scope_Stack.Table (Ctx_Top).Is_Active_Stack_Base := True;
12193 else
12194 Analyze (Act_Body);
12195 end if;
12196 end if;
12198 Inherit_Context (Gen_Body, Inst_Node);
12200 if Par_Installed then
12201 Remove_Parent (In_Body => True);
12203 -- Restore the previous visibility of the parent
12205 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
12206 end if;
12208 -- Remove the parent instances if they have been placed on the scope
12209 -- stack to compile the body.
12211 if Present (Ctx_Parents) then
12212 pragma Assert (Scope_Stack.Last = Ctx_Top
12213 and then Current_Scope = Standard_Standard);
12214 Pop_Scope;
12216 Remove_Parents_Of_Generic_Context (Ctx_Parents);
12217 end if;
12219 pragma Assert (Current_Scope = Scope_Check_Id);
12220 pragma Assert (Scope_Stack.Last = Scope_Check_Last);
12222 Restore_Hidden_Primitives (Vis_Prims_List);
12224 -- Restore the private views that were made visible when the body of
12225 -- the instantiation was created. Note that, in the case where one of
12226 -- these private views is declared in the parent, there is a nesting
12227 -- issue with the calls to Install_Parent and Remove_Parent made in
12228 -- between above with In_Body set to True, because these calls also
12229 -- want to swap and restore this private view respectively. In this
12230 -- case, the call to Install_Parent does nothing, but the call to
12231 -- Remove_Parent does restore the private view, thus undercutting the
12232 -- call to Restore_Private_Views. That's OK under the condition that
12233 -- the two mechanisms swap exactly the same entities, in particular
12234 -- the private entities dependent on the primary private entities.
12236 Restore_Private_Views (Act_Decl_Id);
12238 -- Remove the current unit from visibility if this is an instance
12239 -- that is not elaborated on the fly for inlining purposes.
12241 if not Inlined_Body then
12242 Set_Is_Immediately_Visible (Act_Decl_Id, False);
12243 end if;
12245 Restore_Env;
12247 -- If we have no body, and the unit requires a body, then complain. This
12248 -- complaint is suppressed if we have detected other errors (since a
12249 -- common reason for missing the body is that it had errors).
12250 -- In CodePeer mode, a warning has been emitted already, no need for
12251 -- further messages.
12253 elsif Unit_Requires_Body (Gen_Unit)
12254 and then not Body_Optional
12255 then
12256 if CodePeer_Mode then
12257 null;
12259 elsif Serious_Errors_Detected = 0 then
12260 Error_Msg_NE
12261 ("cannot find body of generic package &", Inst_Node, Gen_Unit);
12263 -- Don't attempt to perform any cleanup actions if some other error
12264 -- was already detected, since this can cause blowups.
12266 else
12267 goto Leave;
12268 end if;
12270 -- Case of package that does not need a body
12272 else
12273 -- If the instantiation of the declaration is a library unit, rewrite
12274 -- the original package instantiation as a package declaration in the
12275 -- compilation unit node.
12277 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
12278 Set_Parent_Spec (Act_Decl, Parent_Spec (Inst_Node));
12279 Rewrite (Inst_Node, Act_Decl);
12281 -- Generate elaboration entity, in case spec has elaboration code.
12282 -- This cannot be done when the instance is analyzed, because it
12283 -- is not known yet whether the body exists.
12285 Set_Elaboration_Entity_Required (Act_Decl_Id, False);
12286 Build_Elaboration_Entity (Parent (Inst_Node), Act_Decl_Id);
12288 -- If the instantiation is not a library unit, then append the
12289 -- declaration to the list of implicitly generated entities, unless
12290 -- it is already a list member which means that it was already
12291 -- processed
12293 elsif not Is_List_Member (Act_Decl) then
12294 Mark_Rewrite_Insertion (Act_Decl);
12295 Insert_Before (Inst_Node, Act_Decl);
12296 end if;
12297 end if;
12299 <<Leave>>
12301 -- Restore the context that was in effect prior to instantiating the
12302 -- package body.
12304 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
12305 Local_Suppress_Stack_Top := Saved_LSST;
12306 Scope_Suppress := Saved_SS;
12307 Style_Check := Saved_SC;
12309 Expander_Mode_Restore;
12310 Restore_Config_Switches (Saved_CS);
12311 Restore_Ghost_Region (Saved_GM, Saved_IGR);
12312 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
12313 Restore_Warnings (Saved_Warn);
12314 end Instantiate_Package_Body;
12316 ---------------------------------
12317 -- Instantiate_Subprogram_Body --
12318 ---------------------------------
12320 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
12321 -- must be replaced by gotos which jump to the end of the routine in order
12322 -- to restore the Ghost and SPARK modes.
12324 procedure Instantiate_Subprogram_Body
12325 (Body_Info : Pending_Body_Info;
12326 Body_Optional : Boolean := False)
12328 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
12329 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Decl);
12330 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
12331 Gen_Id : constant Node_Id := Name (Inst_Node);
12332 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
12333 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
12334 Loc : constant Source_Ptr := Sloc (Inst_Node);
12335 Pack_Id : constant Entity_Id :=
12336 Defining_Unit_Name (Parent (Act_Decl));
12338 -- The following constants capture the context prior to instantiating
12339 -- the subprogram body.
12341 Saved_CS : constant Config_Switches_Type := Save_Config_Switches;
12342 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
12343 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
12344 Saved_ISMP : constant Boolean :=
12345 Ignore_SPARK_Mode_Pragmas_In_Instance;
12346 Saved_LSST : constant Suppress_Stack_Entry_Ptr :=
12347 Local_Suppress_Stack_Top;
12348 Saved_SC : constant Boolean := Style_Check;
12349 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
12350 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
12351 Saved_SS : constant Suppress_Record := Scope_Suppress;
12352 Saved_Warn : constant Warning_Record := Save_Warnings;
12354 Act_Body : Node_Id;
12355 Act_Body_Id : Entity_Id;
12356 Gen_Body : Node_Id;
12357 Gen_Body_Id : Node_Id;
12358 Pack_Body : Node_Id;
12359 Par_Ent : Entity_Id := Empty;
12360 Par_Installed : Boolean := False;
12361 Par_Vis : Boolean := False;
12362 Ret_Expr : Node_Id;
12364 begin
12365 Gen_Body_Id := Corresponding_Body (Gen_Decl);
12367 -- Subprogram body may have been created already because of an inline
12368 -- pragma, or because of multiple elaborations of the enclosing package
12369 -- when several instances of the subprogram appear in the main unit.
12371 if Present (Corresponding_Body (Act_Decl)) then
12372 return;
12373 end if;
12375 -- The subprogram being instantiated may be subject to pragma Ghost. Set
12376 -- the mode now to ensure that any nodes generated during instantiation
12377 -- are properly marked as Ghost.
12379 Set_Ghost_Mode (Act_Decl_Id);
12381 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
12383 -- Re-establish the state of information on which checks are suppressed.
12384 -- This information was set in Body_Info at the point of instantiation,
12385 -- and now we restore it so that the instance is compiled using the
12386 -- check status at the instantiation (RM 11.5(7.2/2), AI95-00224-01).
12388 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
12389 Scope_Suppress := Body_Info.Scope_Suppress;
12391 Restore_Config_Switches (Body_Info.Config_Switches);
12392 Restore_Warnings (Body_Info.Warnings);
12394 if No (Gen_Body_Id) then
12396 -- For imported generic subprogram, no body to compile, complete
12397 -- the spec entity appropriately.
12399 if Is_Imported (Gen_Unit) then
12400 Set_Is_Imported (Act_Decl_Id);
12401 Set_First_Rep_Item (Act_Decl_Id, First_Rep_Item (Gen_Unit));
12402 Set_Interface_Name (Act_Decl_Id, Interface_Name (Gen_Unit));
12403 Set_Convention (Act_Decl_Id, Convention (Gen_Unit));
12404 Set_Has_Completion (Act_Decl_Id);
12405 goto Leave;
12407 -- For other cases, compile the body
12409 else
12410 Load_Parent_Of_Generic
12411 (Inst_Node, Specification (Gen_Decl), Body_Optional);
12412 Gen_Body_Id := Corresponding_Body (Gen_Decl);
12413 end if;
12414 end if;
12416 Instantiation_Node := Inst_Node;
12418 if Present (Gen_Body_Id) then
12419 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
12421 if Nkind (Gen_Body) = N_Subprogram_Body_Stub then
12423 -- Either body is not present, or context is non-expanding, as
12424 -- when compiling a subunit. Mark the instance as completed, and
12425 -- diagnose a missing body when needed.
12427 if Expander_Active
12428 and then Operating_Mode = Generate_Code
12429 then
12430 Error_Msg_N ("missing proper body for instantiation", Gen_Body);
12431 end if;
12433 Set_Has_Completion (Act_Decl_Id);
12434 goto Leave;
12435 end if;
12437 Save_Env (Gen_Unit, Act_Decl_Id);
12438 Style_Check := False;
12440 -- If the context of the instance is subject to SPARK_Mode "off", the
12441 -- annotation is missing, or the body is instantiated at a later pass
12442 -- and its spec ignored SPARK_Mode pragma, set the global flag which
12443 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within the
12444 -- instance.
12446 if SPARK_Mode /= On
12447 or else Ignore_SPARK_Mode_Pragmas (Act_Decl_Id)
12448 then
12449 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
12450 end if;
12452 -- If the context of an instance is not subject to SPARK_Mode "off",
12453 -- and the generic body is subject to an explicit SPARK_Mode pragma,
12454 -- the latter should be the one applicable to the instance.
12456 if not Ignore_SPARK_Mode_Pragmas_In_Instance
12457 and then SPARK_Mode /= Off
12458 and then Present (SPARK_Pragma (Gen_Body_Id))
12459 then
12460 Set_SPARK_Mode (Gen_Body_Id);
12461 end if;
12463 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
12464 Create_Instantiation_Source
12465 (Inst_Node,
12466 Gen_Body_Id,
12467 S_Adjustment);
12469 Act_Body :=
12470 Copy_Generic_Node
12471 (Original_Node (Gen_Body), Empty, Instantiating => True);
12473 -- Create proper defining name for the body, to correspond to the one
12474 -- in the spec.
12476 Act_Body_Id :=
12477 Make_Defining_Identifier (Sloc (Act_Decl_Id), Chars (Act_Decl_Id));
12479 Preserve_Comes_From_Source (Act_Body_Id, Act_Decl_Id);
12480 Set_Defining_Unit_Name (Specification (Act_Body), Act_Body_Id);
12482 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
12483 Set_Has_Completion (Act_Decl_Id);
12484 Check_Generic_Actuals (Pack_Id, False);
12486 -- Generate a reference to link the visible subprogram instance to
12487 -- the generic body, which for navigation purposes is the only
12488 -- available source for the instance.
12490 Generate_Reference
12491 (Related_Instance (Pack_Id),
12492 Gen_Body_Id, 'b', Set_Ref => False, Force => True);
12494 -- If it is a child unit, make the parent instance (which is an
12495 -- instance of the parent of the generic) visible. The parent
12496 -- instance is the prefix of the name of the generic unit.
12498 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
12499 and then Nkind (Gen_Id) = N_Expanded_Name
12500 then
12501 Par_Ent := Entity (Prefix (Gen_Id));
12502 Par_Vis := Is_Immediately_Visible (Par_Ent);
12503 Install_Parent (Par_Ent, In_Body => True);
12504 Par_Installed := True;
12506 elsif Is_Child_Unit (Gen_Unit) then
12507 Par_Ent := Scope (Gen_Unit);
12508 Par_Vis := Is_Immediately_Visible (Par_Ent);
12509 Install_Parent (Par_Ent, In_Body => True);
12510 Par_Installed := True;
12511 end if;
12513 -- Subprogram body is placed in the body of wrapper package,
12514 -- whose spec contains the subprogram declaration as well as
12515 -- the renaming declarations for the generic parameters.
12517 Pack_Body :=
12518 Make_Package_Body (Loc,
12519 Defining_Unit_Name => New_Copy (Pack_Id),
12520 Declarations => New_List (Act_Body));
12522 Set_Corresponding_Spec (Pack_Body, Pack_Id);
12524 -- If the instantiation is a library unit, then build resulting
12525 -- compilation unit nodes for the instance. The declaration of
12526 -- the enclosing package is the grandparent of the subprogram
12527 -- declaration. First replace the instantiation node as the unit
12528 -- of the corresponding compilation.
12530 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
12531 if Parent (Inst_Node) = Cunit (Main_Unit) then
12532 Set_Unit (Parent (Inst_Node), Inst_Node);
12533 Build_Instance_Compilation_Unit_Nodes
12534 (Inst_Node, Pack_Body, Parent (Parent (Act_Decl)));
12535 Analyze (Inst_Node);
12536 else
12537 Set_Parent (Pack_Body, Parent (Inst_Node));
12538 Analyze (Pack_Body);
12539 end if;
12541 else
12542 Insert_Before (Inst_Node, Pack_Body);
12543 Mark_Rewrite_Insertion (Pack_Body);
12545 -- Insert the freeze node for the instance if need be
12547 if Expander_Active then
12548 Freeze_Subprogram_Instance (Inst_Node, Gen_Body, Pack_Id);
12549 end if;
12551 Analyze (Pack_Body);
12552 end if;
12554 Inherit_Context (Gen_Body, Inst_Node);
12556 Restore_Private_Views (Pack_Id, False);
12558 if Par_Installed then
12559 Remove_Parent (In_Body => True);
12561 -- Restore the previous visibility of the parent
12563 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
12564 end if;
12566 Restore_Env;
12568 -- Body not found. Error was emitted already. If there were no previous
12569 -- errors, this may be an instance whose scope is a premature instance.
12570 -- In that case we must insure that the (legal) program does raise
12571 -- program error if executed. We generate a subprogram body for this
12572 -- purpose.
12574 elsif Serious_Errors_Detected = 0
12575 and then Nkind (Parent (Inst_Node)) /= N_Compilation_Unit
12576 then
12577 if Body_Optional then
12578 goto Leave;
12580 elsif Ekind (Act_Decl_Id) = E_Procedure then
12581 Act_Body :=
12582 Make_Subprogram_Body (Loc,
12583 Specification =>
12584 Make_Procedure_Specification (Loc,
12585 Defining_Unit_Name =>
12586 Make_Defining_Identifier (Loc, Chars (Act_Decl_Id)),
12587 Parameter_Specifications =>
12588 New_Copy_List
12589 (Parameter_Specifications (Parent (Act_Decl_Id)))),
12591 Declarations => Empty_List,
12592 Handled_Statement_Sequence =>
12593 Make_Handled_Sequence_Of_Statements (Loc,
12594 Statements => New_List (
12595 Make_Raise_Program_Error (Loc,
12596 Reason => PE_Access_Before_Elaboration))));
12598 else
12599 Ret_Expr :=
12600 Make_Raise_Program_Error (Loc,
12601 Reason => PE_Access_Before_Elaboration);
12603 Set_Etype (Ret_Expr, (Etype (Act_Decl_Id)));
12604 Set_Analyzed (Ret_Expr);
12606 Act_Body :=
12607 Make_Subprogram_Body (Loc,
12608 Specification =>
12609 Make_Function_Specification (Loc,
12610 Defining_Unit_Name =>
12611 Make_Defining_Identifier (Loc, Chars (Act_Decl_Id)),
12612 Parameter_Specifications =>
12613 New_Copy_List
12614 (Parameter_Specifications (Parent (Act_Decl_Id))),
12615 Result_Definition =>
12616 New_Occurrence_Of (Etype (Act_Decl_Id), Loc)),
12618 Declarations => Empty_List,
12619 Handled_Statement_Sequence =>
12620 Make_Handled_Sequence_Of_Statements (Loc,
12621 Statements => New_List (
12622 Make_Simple_Return_Statement (Loc, Ret_Expr))));
12623 end if;
12625 Pack_Body :=
12626 Make_Package_Body (Loc,
12627 Defining_Unit_Name => New_Copy (Pack_Id),
12628 Declarations => New_List (Act_Body));
12630 Insert_After (Inst_Node, Pack_Body);
12631 Set_Corresponding_Spec (Pack_Body, Pack_Id);
12632 Analyze (Pack_Body);
12633 end if;
12635 <<Leave>>
12637 -- Restore the context that was in effect prior to instantiating the
12638 -- subprogram body.
12640 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
12641 Local_Suppress_Stack_Top := Saved_LSST;
12642 Scope_Suppress := Saved_SS;
12643 Style_Check := Saved_SC;
12645 Expander_Mode_Restore;
12646 Restore_Config_Switches (Saved_CS);
12647 Restore_Ghost_Region (Saved_GM, Saved_IGR);
12648 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
12649 Restore_Warnings (Saved_Warn);
12650 end Instantiate_Subprogram_Body;
12652 ----------------------
12653 -- Instantiate_Type --
12654 ----------------------
12656 function Instantiate_Type
12657 (Formal : Node_Id;
12658 Actual : Node_Id;
12659 Analyzed_Formal : Node_Id;
12660 Actual_Decls : List_Id) return List_Id
12662 A_Gen_T : constant Entity_Id :=
12663 Defining_Identifier (Analyzed_Formal);
12664 Def : constant Node_Id := Formal_Type_Definition (Formal);
12665 Gen_T : constant Entity_Id := Defining_Identifier (Formal);
12666 Act_T : Entity_Id;
12667 Ancestor : Entity_Id := Empty;
12668 Decl_Node : Node_Id;
12669 Decl_Nodes : List_Id;
12670 Loc : Source_Ptr;
12671 Subt : Entity_Id;
12673 procedure Check_Shared_Variable_Control_Aspects;
12674 -- Ada 2022: Verify that shared variable control aspects (RM C.6)
12675 -- that may be specified for a formal type are obeyed by the actual.
12677 procedure Diagnose_Predicated_Actual;
12678 -- There are a number of constructs in which a discrete type with
12679 -- predicates is illegal, e.g. as an index in an array type declaration.
12680 -- If a generic type is used is such a construct in a generic package
12681 -- declaration, it carries the flag No_Predicate_On_Actual. it is part
12682 -- of the generic contract that the actual cannot have predicates.
12684 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean;
12685 -- Check that base types are the same and that the subtypes match
12686 -- statically. Used in several of the validation subprograms for
12687 -- actuals in instantiations.
12689 procedure Validate_Array_Type_Instance;
12690 procedure Validate_Access_Subprogram_Instance;
12691 procedure Validate_Access_Type_Instance;
12692 procedure Validate_Derived_Type_Instance;
12693 procedure Validate_Derived_Interface_Type_Instance;
12694 procedure Validate_Discriminated_Formal_Type;
12695 procedure Validate_Interface_Type_Instance;
12696 procedure Validate_Private_Type_Instance;
12697 procedure Validate_Incomplete_Type_Instance;
12698 -- These procedures perform validation tests for the named case.
12699 -- Validate_Discriminated_Formal_Type is shared by formal private
12700 -- types and Ada 2012 formal incomplete types.
12702 --------------------------------------------
12703 -- Check_Shared_Variable_Control_Aspects --
12704 --------------------------------------------
12706 -- Ada 2022: Verify that shared variable control aspects (RM C.6)
12707 -- that may be specified for the formal are obeyed by the actual.
12708 -- If the formal is a derived type the aspect specifications must match.
12709 -- NOTE: AI12-0282 implies that matching of aspects is required between
12710 -- formal and actual in all cases, but this is too restrictive.
12711 -- In particular it violates a language design rule: a limited private
12712 -- indefinite formal can be matched by any actual. The current code
12713 -- reflects an older and more permissive version of RM C.6 (12/5).
12715 procedure Check_Shared_Variable_Control_Aspects is
12716 begin
12717 if Ada_Version >= Ada_2022 then
12718 if Is_Atomic (A_Gen_T) and then not Is_Atomic (Act_T) then
12719 Error_Msg_NE
12720 ("actual for& must have Atomic aspect", Actual, A_Gen_T);
12722 elsif Is_Derived_Type (A_Gen_T)
12723 and then Is_Atomic (A_Gen_T) /= Is_Atomic (Act_T)
12724 then
12725 Error_Msg_NE
12726 ("actual for& has different Atomic aspect", Actual, A_Gen_T);
12727 end if;
12729 if Is_Volatile (A_Gen_T) and then not Is_Volatile (Act_T) then
12730 Error_Msg_NE
12731 ("actual for& must have Volatile aspect",
12732 Actual, A_Gen_T);
12734 elsif Is_Derived_Type (A_Gen_T)
12735 and then Is_Volatile (A_Gen_T) /= Is_Volatile (Act_T)
12736 then
12737 Error_Msg_NE
12738 ("actual for& has different Volatile aspect",
12739 Actual, A_Gen_T);
12740 end if;
12742 -- We assume that an array type whose atomic component type
12743 -- is Atomic is equivalent to an array type with the explicit
12744 -- aspect Has_Atomic_Components. This is a reasonable inference
12745 -- from the intent of AI12-0282, and makes it legal to use an
12746 -- actual that does not have the identical aspect as the formal.
12747 -- Ditto for volatile components.
12749 declare
12750 Actual_Atomic_Comp : constant Boolean :=
12751 Has_Atomic_Components (Act_T)
12752 or else (Is_Array_Type (Act_T)
12753 and then Is_Atomic (Component_Type (Act_T)));
12754 begin
12755 if Has_Atomic_Components (A_Gen_T) /= Actual_Atomic_Comp then
12756 Error_Msg_NE
12757 ("formal and actual for& must agree on atomic components",
12758 Actual, A_Gen_T);
12759 end if;
12760 end;
12762 declare
12763 Actual_Volatile_Comp : constant Boolean :=
12764 Has_Volatile_Components (Act_T)
12765 or else (Is_Array_Type (Act_T)
12766 and then Is_Volatile (Component_Type (Act_T)));
12767 begin
12768 if Has_Volatile_Components (A_Gen_T) /= Actual_Volatile_Comp
12769 then
12770 Error_Msg_NE
12771 ("actual for& must have volatile components",
12772 Actual, A_Gen_T);
12773 end if;
12774 end;
12776 -- The following two aspects do not require exact matching,
12777 -- but only one-way agreement. See RM C.6.
12779 if Is_Independent (A_Gen_T) and then not Is_Independent (Act_T)
12780 then
12781 Error_Msg_NE
12782 ("actual for& must have Independent aspect specified",
12783 Actual, A_Gen_T);
12784 end if;
12786 if Has_Independent_Components (A_Gen_T)
12787 and then not Has_Independent_Components (Act_T)
12788 then
12789 Error_Msg_NE
12790 ("actual for& must have Independent_Components specified",
12791 Actual, A_Gen_T);
12792 end if;
12794 -- Check actual/formal compatibility with respect to the four
12795 -- volatility refinement aspects.
12797 Check_Volatility_Compatibility
12798 (Act_T, A_Gen_T,
12799 "actual type", "its corresponding formal type",
12800 Srcpos_Bearer => Actual);
12801 end if;
12802 end Check_Shared_Variable_Control_Aspects;
12804 ---------------------------------
12805 -- Diagnose_Predicated_Actual --
12806 ---------------------------------
12808 procedure Diagnose_Predicated_Actual is
12809 begin
12810 if No_Predicate_On_Actual (A_Gen_T)
12811 and then Has_Predicates (Act_T)
12812 then
12813 Error_Msg_NE
12814 ("actual for& cannot be a type with predicate",
12815 Instantiation_Node, A_Gen_T);
12817 elsif No_Dynamic_Predicate_On_Actual (A_Gen_T)
12818 and then Has_Predicates (Act_T)
12819 and then not Has_Static_Predicate_Aspect (Act_T)
12820 then
12821 Error_Msg_NE
12822 ("actual for& cannot be a type with a dynamic predicate",
12823 Instantiation_Node, A_Gen_T);
12824 end if;
12825 end Diagnose_Predicated_Actual;
12827 --------------------
12828 -- Subtypes_Match --
12829 --------------------
12831 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean is
12832 T : constant Entity_Id := Get_Instance_Of (Gen_T);
12834 begin
12835 -- Check that the base types, root types (when dealing with class
12836 -- wide types), or designated types (when dealing with anonymous
12837 -- access types) of Gen_T and Act_T are statically matching subtypes.
12839 return ((Base_Type (T) = Act_T
12840 or else Base_Type (T) = Base_Type (Act_T))
12841 and then Subtypes_Statically_Match (T, Act_T))
12843 or else (Is_Class_Wide_Type (Gen_T)
12844 and then Is_Class_Wide_Type (Act_T)
12845 and then Subtypes_Match
12846 (Get_Instance_Of (Root_Type (Gen_T)),
12847 Root_Type (Act_T)))
12849 or else (Is_Anonymous_Access_Type (Gen_T)
12850 and then Ekind (Act_T) = Ekind (Gen_T)
12851 and then Subtypes_Statically_Match
12852 (Designated_Type (Gen_T), Designated_Type (Act_T)));
12853 end Subtypes_Match;
12855 -----------------------------------------
12856 -- Validate_Access_Subprogram_Instance --
12857 -----------------------------------------
12859 procedure Validate_Access_Subprogram_Instance is
12860 begin
12861 if not Is_Access_Type (Act_T)
12862 or else Ekind (Designated_Type (Act_T)) /= E_Subprogram_Type
12863 then
12864 Error_Msg_NE
12865 ("expect access type in instantiation of &", Actual, Gen_T);
12866 Abandon_Instantiation (Actual);
12867 end if;
12869 -- According to AI05-288, actuals for access_to_subprograms must be
12870 -- subtype conformant with the generic formal. Previous to AI05-288
12871 -- only mode conformance was required.
12873 -- This is a binding interpretation that applies to previous versions
12874 -- of the language, no need to maintain previous weaker checks.
12876 Check_Subtype_Conformant
12877 (Designated_Type (Act_T),
12878 Designated_Type (A_Gen_T),
12879 Actual,
12880 Get_Inst => True);
12882 if Ekind (Base_Type (Act_T)) = E_Access_Protected_Subprogram_Type then
12883 if Ekind (A_Gen_T) = E_Access_Subprogram_Type then
12884 Error_Msg_NE
12885 ("protected access type not allowed for formal &",
12886 Actual, Gen_T);
12887 end if;
12889 elsif Ekind (A_Gen_T) = E_Access_Protected_Subprogram_Type then
12890 Error_Msg_NE
12891 ("expect protected access type for formal &",
12892 Actual, Gen_T);
12893 end if;
12895 -- If the formal has a specified convention (which in most cases
12896 -- will be StdCall) verify that the actual has the same convention.
12898 if Has_Convention_Pragma (A_Gen_T)
12899 and then Convention (A_Gen_T) /= Convention (Act_T)
12900 then
12901 Error_Msg_Name_1 := Get_Convention_Name (Convention (A_Gen_T));
12902 Error_Msg_NE
12903 ("actual for formal & must have convention %", Actual, Gen_T);
12904 end if;
12906 if Can_Never_Be_Null (A_Gen_T) /= Can_Never_Be_Null (Act_T) then
12907 Error_Msg_NE
12908 ("non null exclusion of actual and formal & do not match",
12909 Actual, Gen_T);
12910 end if;
12911 end Validate_Access_Subprogram_Instance;
12913 -----------------------------------
12914 -- Validate_Access_Type_Instance --
12915 -----------------------------------
12917 procedure Validate_Access_Type_Instance is
12918 Desig_Type : constant Entity_Id :=
12919 Find_Actual_Type (Designated_Type (A_Gen_T), A_Gen_T);
12920 Desig_Act : Entity_Id;
12922 begin
12923 if not Is_Access_Type (Act_T) then
12924 Error_Msg_NE
12925 ("expect access type in instantiation of &", Actual, Gen_T);
12926 Abandon_Instantiation (Actual);
12927 end if;
12929 if Is_Access_Constant (A_Gen_T) then
12930 if not Is_Access_Constant (Act_T) then
12931 Error_Msg_N
12932 ("actual type must be access-to-constant type", Actual);
12933 Abandon_Instantiation (Actual);
12934 end if;
12935 else
12936 if Is_Access_Constant (Act_T) then
12937 Error_Msg_N
12938 ("actual type must be access-to-variable type", Actual);
12939 Abandon_Instantiation (Actual);
12941 elsif Ekind (A_Gen_T) = E_General_Access_Type
12942 and then Ekind (Base_Type (Act_T)) /= E_General_Access_Type
12943 then
12944 Error_Msg_N
12945 ("actual must be general access type!", Actual);
12946 Error_Msg_NE -- CODEFIX
12947 ("\add ALL to }!", Actual, Act_T);
12948 Abandon_Instantiation (Actual);
12949 end if;
12950 end if;
12952 -- The designated subtypes, that is to say the subtypes introduced
12953 -- by an access type declaration (and not by a subtype declaration)
12954 -- must match.
12956 Desig_Act := Designated_Type (Base_Type (Act_T));
12958 -- The designated type may have been introduced through a limited_
12959 -- with clause, in which case retrieve the non-limited view. This
12960 -- applies to incomplete types as well as to class-wide types.
12962 if From_Limited_With (Desig_Act) then
12963 Desig_Act := Available_View (Desig_Act);
12964 end if;
12966 if not Subtypes_Match (Desig_Type, Desig_Act) then
12967 Error_Msg_NE
12968 ("designated type of actual does not match that of formal &",
12969 Actual, Gen_T);
12971 if not Predicates_Match (Desig_Type, Desig_Act) then
12972 Error_Msg_N ("\predicates do not match", Actual);
12973 end if;
12975 Abandon_Instantiation (Actual);
12976 end if;
12978 -- Ada 2005: null-exclusion indicators of the two types must agree
12980 if Can_Never_Be_Null (A_Gen_T) /= Can_Never_Be_Null (Act_T) then
12981 Error_Msg_NE
12982 ("non null exclusion of actual and formal & do not match",
12983 Actual, Gen_T);
12984 end if;
12985 end Validate_Access_Type_Instance;
12987 ----------------------------------
12988 -- Validate_Array_Type_Instance --
12989 ----------------------------------
12991 procedure Validate_Array_Type_Instance is
12992 I1 : Node_Id;
12993 I2 : Node_Id;
12994 T2 : Entity_Id;
12996 function Formal_Dimensions return Nat;
12997 -- Count number of dimensions in array type formal
12999 -----------------------
13000 -- Formal_Dimensions --
13001 -----------------------
13003 function Formal_Dimensions return Nat is
13004 Num : Nat := 0;
13005 Index : Node_Id;
13007 begin
13008 if Nkind (Def) = N_Constrained_Array_Definition then
13009 Index := First (Discrete_Subtype_Definitions (Def));
13010 else
13011 Index := First (Subtype_Marks (Def));
13012 end if;
13014 while Present (Index) loop
13015 Num := Num + 1;
13016 Next (Index);
13017 end loop;
13019 return Num;
13020 end Formal_Dimensions;
13022 -- Start of processing for Validate_Array_Type_Instance
13024 begin
13025 if not Is_Array_Type (Act_T) then
13026 Error_Msg_NE
13027 ("expect array type in instantiation of &", Actual, Gen_T);
13028 Abandon_Instantiation (Actual);
13030 elsif Nkind (Def) = N_Constrained_Array_Definition then
13031 if not (Is_Constrained (Act_T)) then
13032 Error_Msg_NE
13033 ("expect constrained array in instantiation of &",
13034 Actual, Gen_T);
13035 Abandon_Instantiation (Actual);
13036 end if;
13038 else
13039 if Is_Constrained (Act_T) then
13040 Error_Msg_NE
13041 ("expect unconstrained array in instantiation of &",
13042 Actual, Gen_T);
13043 Abandon_Instantiation (Actual);
13044 end if;
13045 end if;
13047 if Formal_Dimensions /= Number_Dimensions (Act_T) then
13048 Error_Msg_NE
13049 ("dimensions of actual do not match formal &", Actual, Gen_T);
13050 Abandon_Instantiation (Actual);
13051 end if;
13053 I1 := First_Index (A_Gen_T);
13054 I2 := First_Index (Act_T);
13055 for J in 1 .. Formal_Dimensions loop
13057 -- If the indexes of the actual were given by a subtype_mark,
13058 -- the index was transformed into a range attribute. Retrieve
13059 -- the original type mark for checking.
13061 if Is_Entity_Name (Original_Node (I2)) then
13062 T2 := Entity (Original_Node (I2));
13063 else
13064 T2 := Etype (I2);
13065 end if;
13067 if not Subtypes_Match
13068 (Find_Actual_Type (Etype (I1), A_Gen_T), T2)
13069 then
13070 Error_Msg_NE
13071 ("index types of actual do not match those of formal &",
13072 Actual, Gen_T);
13073 Abandon_Instantiation (Actual);
13074 end if;
13076 Next_Index (I1);
13077 Next_Index (I2);
13078 end loop;
13080 -- Check matching subtypes. Note that there are complex visibility
13081 -- issues when the generic is a child unit and some aspect of the
13082 -- generic type is declared in a parent unit of the generic. We do
13083 -- the test to handle this special case only after a direct check
13084 -- for static matching has failed. The case where both the component
13085 -- type and the array type are separate formals, and the component
13086 -- type is a private view may also require special checking in
13087 -- Subtypes_Match. Finally, we assume that a child instance where
13088 -- the component type comes from a formal of a parent instance is
13089 -- correct because the generic was correct. A more precise check
13090 -- seems too complex to install???
13092 if Subtypes_Match
13093 (Component_Type (A_Gen_T), Component_Type (Act_T))
13094 or else
13095 Subtypes_Match
13096 (Find_Actual_Type (Component_Type (A_Gen_T), A_Gen_T),
13097 Component_Type (Act_T))
13098 or else
13099 (not Inside_A_Generic
13100 and then Is_Child_Unit (Scope (Component_Type (A_Gen_T))))
13101 then
13102 null;
13103 else
13104 Error_Msg_NE
13105 ("component subtype of actual does not match that of formal &",
13106 Actual, Gen_T);
13107 Abandon_Instantiation (Actual);
13108 end if;
13110 if Has_Aliased_Components (A_Gen_T)
13111 and then not Has_Aliased_Components (Act_T)
13112 then
13113 Error_Msg_NE
13114 ("actual must have aliased components to match formal type &",
13115 Actual, Gen_T);
13116 end if;
13117 end Validate_Array_Type_Instance;
13119 -----------------------------------------------
13120 -- Validate_Derived_Interface_Type_Instance --
13121 -----------------------------------------------
13123 procedure Validate_Derived_Interface_Type_Instance is
13124 Par : constant Entity_Id := Entity (Subtype_Indication (Def));
13125 Elmt : Elmt_Id;
13127 begin
13128 -- First apply interface instance checks
13130 Validate_Interface_Type_Instance;
13132 -- Verify that immediate parent interface is an ancestor of
13133 -- the actual.
13135 if Present (Par)
13136 and then not Interface_Present_In_Ancestor (Act_T, Par)
13137 then
13138 Error_Msg_NE
13139 ("interface actual must include progenitor&", Actual, Par);
13140 end if;
13142 -- Now verify that the actual includes all other ancestors of
13143 -- the formal.
13145 Elmt := First_Elmt (Interfaces (A_Gen_T));
13146 while Present (Elmt) loop
13147 if not Interface_Present_In_Ancestor
13148 (Act_T, Get_Instance_Of (Node (Elmt)))
13149 then
13150 Error_Msg_NE
13151 ("interface actual must include progenitor&",
13152 Actual, Node (Elmt));
13153 end if;
13155 Next_Elmt (Elmt);
13156 end loop;
13157 end Validate_Derived_Interface_Type_Instance;
13159 ------------------------------------
13160 -- Validate_Derived_Type_Instance --
13161 ------------------------------------
13163 procedure Validate_Derived_Type_Instance is
13164 Actual_Discr : Entity_Id;
13165 Ancestor_Discr : Entity_Id;
13167 begin
13168 -- Verify that the actual includes the progenitors of the formal,
13169 -- if any. The formal may depend on previous formals and their
13170 -- instance, so we must examine instance of interfaces if present.
13171 -- The actual may be an extension of an interface, in which case
13172 -- it does not appear in the interface list, so this must be
13173 -- checked separately.
13175 if Present (Interface_List (Def)) then
13176 if not Has_Interfaces (Act_T) then
13177 Error_Msg_NE
13178 ("actual must implement all interfaces of formal&",
13179 Actual, A_Gen_T);
13181 else
13182 declare
13183 Act_Iface_List : Elist_Id;
13184 Iface : Node_Id;
13185 Iface_Ent : Entity_Id;
13187 function Instance_Exists (I : Entity_Id) return Boolean;
13188 -- If the interface entity is declared in a generic unit,
13189 -- this can only be legal if we are within an instantiation
13190 -- of a child of that generic. There is currently no
13191 -- mechanism to relate an interface declared within a
13192 -- generic to the corresponding interface in an instance,
13193 -- so we traverse the list of interfaces of the actual,
13194 -- looking for a name match.
13196 ---------------------
13197 -- Instance_Exists --
13198 ---------------------
13200 function Instance_Exists (I : Entity_Id) return Boolean is
13201 Iface_Elmt : Elmt_Id;
13203 begin
13204 Iface_Elmt := First_Elmt (Act_Iface_List);
13205 while Present (Iface_Elmt) loop
13206 if Is_Generic_Instance (Scope (Node (Iface_Elmt)))
13207 and then Chars (Node (Iface_Elmt)) = Chars (I)
13208 then
13209 return True;
13210 end if;
13212 Next_Elmt (Iface_Elmt);
13213 end loop;
13215 return False;
13216 end Instance_Exists;
13218 begin
13219 Iface := First (Abstract_Interface_List (A_Gen_T));
13220 Collect_Interfaces (Act_T, Act_Iface_List);
13222 while Present (Iface) loop
13223 Iface_Ent := Get_Instance_Of (Entity (Iface));
13225 if Is_Ancestor (Iface_Ent, Act_T)
13226 or else Is_Progenitor (Iface_Ent, Act_T)
13227 then
13228 null;
13230 elsif Ekind (Scope (Iface_Ent)) = E_Generic_Package
13231 and then Instance_Exists (Iface_Ent)
13232 then
13233 null;
13235 else
13236 Error_Msg_Name_1 := Chars (Act_T);
13237 Error_Msg_NE
13238 ("actual% must implement interface&",
13239 Actual, Etype (Iface));
13240 end if;
13242 Next (Iface);
13243 end loop;
13244 end;
13245 end if;
13246 end if;
13248 -- If the parent type in the generic declaration is itself a previous
13249 -- formal type, then it is local to the generic and absent from the
13250 -- analyzed generic definition. In that case the ancestor is the
13251 -- instance of the formal (which must have been instantiated
13252 -- previously), unless the ancestor is itself a formal derived type.
13253 -- In this latter case (which is the subject of Corrigendum 8652/0038
13254 -- (AI-202) the ancestor of the formals is the ancestor of its
13255 -- parent. Otherwise, the analyzed generic carries the parent type.
13256 -- If the parent type is defined in a previous formal package, then
13257 -- the scope of that formal package is that of the generic type
13258 -- itself, and it has already been mapped into the corresponding type
13259 -- in the actual package.
13261 -- Common case: parent type defined outside of the generic
13263 if Is_Entity_Name (Subtype_Mark (Def))
13264 and then Present (Entity (Subtype_Mark (Def)))
13265 then
13266 Ancestor := Get_Instance_Of (Entity (Subtype_Mark (Def)));
13268 -- Check whether parent is defined in a previous formal package
13270 elsif
13271 Scope (Scope (Base_Type (Etype (A_Gen_T)))) = Scope (A_Gen_T)
13272 then
13273 Ancestor :=
13274 Get_Instance_Of (Base_Type (Etype (A_Gen_T)));
13276 -- The type may be a local derivation, or a type extension of a
13277 -- previous formal, or of a formal of a parent package.
13279 elsif Is_Derived_Type (Get_Instance_Of (A_Gen_T))
13280 or else
13281 Ekind (Get_Instance_Of (A_Gen_T)) = E_Record_Type_With_Private
13282 then
13283 -- Check whether the parent is another derived formal type in the
13284 -- same generic unit.
13286 if Etype (A_Gen_T) /= A_Gen_T
13287 and then Is_Generic_Type (Etype (A_Gen_T))
13288 and then Scope (Etype (A_Gen_T)) = Scope (A_Gen_T)
13289 and then Etype (Etype (A_Gen_T)) /= Etype (A_Gen_T)
13290 then
13291 -- Locate ancestor of parent from the subtype declaration
13292 -- created for the actual.
13294 declare
13295 Decl : Node_Id;
13297 begin
13298 Decl := First (Actual_Decls);
13299 while Present (Decl) loop
13300 if Nkind (Decl) = N_Subtype_Declaration
13301 and then Chars (Defining_Identifier (Decl)) =
13302 Chars (Etype (A_Gen_T))
13303 then
13304 Ancestor := Generic_Parent_Type (Decl);
13305 exit;
13306 else
13307 Next (Decl);
13308 end if;
13309 end loop;
13310 end;
13312 pragma Assert (Present (Ancestor));
13314 -- The ancestor itself may be a previous formal that has been
13315 -- instantiated.
13317 Ancestor := Get_Instance_Of (Ancestor);
13319 else
13320 Ancestor :=
13321 Get_Instance_Of (Base_Type (Get_Instance_Of (A_Gen_T)));
13322 end if;
13324 -- Check whether parent is a previous formal of the current generic
13326 elsif Is_Derived_Type (A_Gen_T)
13327 and then Is_Generic_Type (Etype (A_Gen_T))
13328 and then Scope (A_Gen_T) = Scope (Etype (A_Gen_T))
13329 then
13330 Ancestor := Get_Instance_Of (First_Subtype (Etype (A_Gen_T)));
13332 -- An unusual case: the actual is a type declared in a parent unit,
13333 -- but is not a formal type so there is no instance_of for it.
13334 -- Retrieve it by analyzing the record extension.
13336 elsif Is_Child_Unit (Scope (A_Gen_T))
13337 and then In_Open_Scopes (Scope (Act_T))
13338 and then Is_Generic_Instance (Scope (Act_T))
13339 then
13340 Analyze (Subtype_Mark (Def));
13341 Ancestor := Entity (Subtype_Mark (Def));
13343 else
13344 Ancestor := Get_Instance_Of (Etype (Base_Type (A_Gen_T)));
13345 end if;
13347 -- If the formal derived type has pragma Preelaborable_Initialization
13348 -- then the actual type must have preelaborable initialization.
13350 if Known_To_Have_Preelab_Init (A_Gen_T)
13351 and then not Has_Preelaborable_Initialization (Act_T)
13352 then
13353 Error_Msg_NE
13354 ("actual for & must have preelaborable initialization",
13355 Actual, Gen_T);
13356 end if;
13358 -- Ada 2005 (AI-251)
13360 if Ada_Version >= Ada_2005 and then Is_Interface (Ancestor) then
13361 if not Interface_Present_In_Ancestor (Act_T, Ancestor) then
13362 Error_Msg_NE
13363 ("(Ada 2005) expected type implementing & in instantiation",
13364 Actual, Ancestor);
13365 end if;
13367 -- Finally verify that the (instance of) the ancestor is an ancestor
13368 -- of the actual.
13370 elsif not Is_Ancestor (Base_Type (Ancestor), Act_T) then
13371 Error_Msg_NE
13372 ("expect type derived from & in instantiation",
13373 Actual, First_Subtype (Ancestor));
13374 Abandon_Instantiation (Actual);
13375 end if;
13377 -- Ada 2005 (AI-443): Synchronized formal derived type checks. Note
13378 -- that the formal type declaration has been rewritten as a private
13379 -- extension.
13381 if Ada_Version >= Ada_2005
13382 and then Nkind (Parent (A_Gen_T)) = N_Private_Extension_Declaration
13383 and then Synchronized_Present (Parent (A_Gen_T))
13384 then
13385 -- The actual must be a synchronized tagged type
13387 if not Is_Tagged_Type (Act_T) then
13388 Error_Msg_N
13389 ("actual of synchronized type must be tagged", Actual);
13390 Abandon_Instantiation (Actual);
13392 elsif Nkind (Parent (Act_T)) = N_Full_Type_Declaration
13393 and then Nkind (Type_Definition (Parent (Act_T))) =
13394 N_Derived_Type_Definition
13395 and then not Synchronized_Present
13396 (Type_Definition (Parent (Act_T)))
13397 then
13398 Error_Msg_N
13399 ("actual of synchronized type must be synchronized", Actual);
13400 Abandon_Instantiation (Actual);
13401 end if;
13402 end if;
13404 -- Perform atomic/volatile checks (RM C.6(12)). Note that AI05-0218-1
13405 -- removes the second instance of the phrase "or allow pass by copy".
13407 -- For Ada 2022, the aspect may be specified explicitly for the
13408 -- formal regardless of whether an ancestor obeys it.
13410 if Is_Atomic (Act_T)
13411 and then not Is_Atomic (Ancestor)
13412 and then not Is_Atomic (A_Gen_T)
13413 then
13414 Error_Msg_N
13415 ("cannot have atomic actual type for non-atomic formal type",
13416 Actual);
13418 elsif Is_Volatile (Act_T)
13419 and then not Is_Volatile (Ancestor)
13420 and then not Is_Volatile (A_Gen_T)
13421 then
13422 Error_Msg_N
13423 ("cannot have volatile actual type for non-volatile formal type",
13424 Actual);
13425 end if;
13427 -- It should not be necessary to check for unknown discriminants on
13428 -- Formal, but for some reason Has_Unknown_Discriminants is false for
13429 -- A_Gen_T, so Is_Definite_Subtype incorrectly returns True. This
13430 -- needs fixing. ???
13432 if Is_Definite_Subtype (A_Gen_T)
13433 and then not Unknown_Discriminants_Present (Formal)
13434 and then not Is_Definite_Subtype (Act_T)
13435 then
13436 Error_Msg_N ("actual subtype must be constrained", Actual);
13437 Abandon_Instantiation (Actual);
13438 end if;
13440 if not Unknown_Discriminants_Present (Formal) then
13441 if Is_Constrained (Ancestor) then
13442 if not Is_Constrained (Act_T) then
13443 Error_Msg_N ("actual subtype must be constrained", Actual);
13444 Abandon_Instantiation (Actual);
13445 end if;
13447 -- Ancestor is unconstrained, Check if generic formal and actual
13448 -- agree on constrainedness. The check only applies to array types
13449 -- and discriminated types.
13451 elsif Is_Constrained (Act_T) then
13452 if Ekind (Ancestor) = E_Access_Type
13453 or else (not Is_Constrained (A_Gen_T)
13454 and then Is_Composite_Type (A_Gen_T))
13455 then
13456 Error_Msg_N ("actual subtype must be unconstrained", Actual);
13457 Abandon_Instantiation (Actual);
13458 end if;
13460 -- A class-wide type is only allowed if the formal has unknown
13461 -- discriminants.
13463 elsif Is_Class_Wide_Type (Act_T)
13464 and then not Has_Unknown_Discriminants (Ancestor)
13465 then
13466 Error_Msg_NE
13467 ("actual for & cannot be a class-wide type", Actual, Gen_T);
13468 Abandon_Instantiation (Actual);
13470 -- Otherwise, the formal and actual must have the same number
13471 -- of discriminants and each discriminant of the actual must
13472 -- correspond to a discriminant of the formal.
13474 elsif Has_Discriminants (Act_T)
13475 and then not Has_Unknown_Discriminants (Act_T)
13476 and then Has_Discriminants (Ancestor)
13477 then
13478 Actual_Discr := First_Discriminant (Act_T);
13479 Ancestor_Discr := First_Discriminant (Ancestor);
13480 while Present (Actual_Discr)
13481 and then Present (Ancestor_Discr)
13482 loop
13483 if Base_Type (Act_T) /= Base_Type (Ancestor) and then
13484 No (Corresponding_Discriminant (Actual_Discr))
13485 then
13486 Error_Msg_NE
13487 ("discriminant & does not correspond "
13488 & "to ancestor discriminant", Actual, Actual_Discr);
13489 Abandon_Instantiation (Actual);
13490 end if;
13492 Next_Discriminant (Actual_Discr);
13493 Next_Discriminant (Ancestor_Discr);
13494 end loop;
13496 if Present (Actual_Discr) or else Present (Ancestor_Discr) then
13497 Error_Msg_NE
13498 ("actual for & must have same number of discriminants",
13499 Actual, Gen_T);
13500 Abandon_Instantiation (Actual);
13501 end if;
13503 -- This case should be caught by the earlier check for
13504 -- constrainedness, but the check here is added for completeness.
13506 elsif Has_Discriminants (Act_T)
13507 and then not Has_Unknown_Discriminants (Act_T)
13508 then
13509 Error_Msg_NE
13510 ("actual for & must not have discriminants", Actual, Gen_T);
13511 Abandon_Instantiation (Actual);
13513 elsif Has_Discriminants (Ancestor) then
13514 Error_Msg_NE
13515 ("actual for & must have known discriminants", Actual, Gen_T);
13516 Abandon_Instantiation (Actual);
13517 end if;
13519 if not Subtypes_Statically_Compatible
13520 (Act_T, Ancestor, Formal_Derived_Matching => True)
13521 then
13522 Error_Msg_NE
13523 ("actual for & must be statically compatible with ancestor",
13524 Actual, Gen_T);
13526 if not Predicates_Compatible (Act_T, Ancestor) then
13527 Error_Msg_N
13528 ("\predicate on actual is not compatible with ancestor",
13529 Actual);
13530 end if;
13532 Abandon_Instantiation (Actual);
13533 end if;
13534 end if;
13536 -- If the formal and actual types are abstract, check that there
13537 -- are no abstract primitives of the actual type that correspond to
13538 -- nonabstract primitives of the formal type (second sentence of
13539 -- RM95 3.9.3(9)).
13541 if Is_Abstract_Type (A_Gen_T) and then Is_Abstract_Type (Act_T) then
13542 Check_Abstract_Primitives : declare
13543 Gen_Prims : constant Elist_Id :=
13544 Primitive_Operations (A_Gen_T);
13545 Gen_Elmt : Elmt_Id;
13546 Gen_Subp : Entity_Id;
13547 Anc_Subp : Entity_Id;
13548 Anc_Formal : Entity_Id;
13549 Anc_F_Type : Entity_Id;
13551 Act_Prims : constant Elist_Id := Primitive_Operations (Act_T);
13552 Act_Elmt : Elmt_Id;
13553 Act_Subp : Entity_Id;
13554 Act_Formal : Entity_Id;
13555 Act_F_Type : Entity_Id;
13557 Subprograms_Correspond : Boolean;
13559 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean;
13560 -- Returns true if T2 is derived directly or indirectly from
13561 -- T1, including derivations from interfaces. T1 and T2 are
13562 -- required to be specific tagged base types.
13564 ------------------------
13565 -- Is_Tagged_Ancestor --
13566 ------------------------
13568 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean
13570 Intfc_Elmt : Elmt_Id;
13572 begin
13573 -- The predicate is satisfied if the types are the same
13575 if T1 = T2 then
13576 return True;
13578 -- If we've reached the top of the derivation chain then
13579 -- we know that T1 is not an ancestor of T2.
13581 elsif Etype (T2) = T2 then
13582 return False;
13584 -- Proceed to check T2's immediate parent
13586 elsif Is_Ancestor (T1, Base_Type (Etype (T2))) then
13587 return True;
13589 -- Finally, check to see if T1 is an ancestor of any of T2's
13590 -- progenitors.
13592 else
13593 Intfc_Elmt := First_Elmt (Interfaces (T2));
13594 while Present (Intfc_Elmt) loop
13595 if Is_Ancestor (T1, Node (Intfc_Elmt)) then
13596 return True;
13597 end if;
13599 Next_Elmt (Intfc_Elmt);
13600 end loop;
13601 end if;
13603 return False;
13604 end Is_Tagged_Ancestor;
13606 -- Start of processing for Check_Abstract_Primitives
13608 begin
13609 -- Loop over all of the formal derived type's primitives
13611 Gen_Elmt := First_Elmt (Gen_Prims);
13612 while Present (Gen_Elmt) loop
13613 Gen_Subp := Node (Gen_Elmt);
13615 -- If the primitive of the formal is not abstract, then
13616 -- determine whether there is a corresponding primitive of
13617 -- the actual type that's abstract.
13619 if not Is_Abstract_Subprogram (Gen_Subp) then
13620 Act_Elmt := First_Elmt (Act_Prims);
13621 while Present (Act_Elmt) loop
13622 Act_Subp := Node (Act_Elmt);
13624 -- If we find an abstract primitive of the actual,
13625 -- then we need to test whether it corresponds to the
13626 -- subprogram from which the generic formal primitive
13627 -- is inherited.
13629 if Is_Abstract_Subprogram (Act_Subp) then
13630 Anc_Subp := Alias (Gen_Subp);
13632 -- Test whether we have a corresponding primitive
13633 -- by comparing names, kinds, formal types, and
13634 -- result types.
13636 if Chars (Anc_Subp) = Chars (Act_Subp)
13637 and then Ekind (Anc_Subp) = Ekind (Act_Subp)
13638 then
13639 Anc_Formal := First_Formal (Anc_Subp);
13640 Act_Formal := First_Formal (Act_Subp);
13641 while Present (Anc_Formal)
13642 and then Present (Act_Formal)
13643 loop
13644 Anc_F_Type := Etype (Anc_Formal);
13645 Act_F_Type := Etype (Act_Formal);
13647 if Ekind (Anc_F_Type) =
13648 E_Anonymous_Access_Type
13649 then
13650 Anc_F_Type := Designated_Type (Anc_F_Type);
13652 if Ekind (Act_F_Type) =
13653 E_Anonymous_Access_Type
13654 then
13655 Act_F_Type :=
13656 Designated_Type (Act_F_Type);
13657 else
13658 exit;
13659 end if;
13661 elsif
13662 Ekind (Act_F_Type) = E_Anonymous_Access_Type
13663 then
13664 exit;
13665 end if;
13667 Anc_F_Type := Base_Type (Anc_F_Type);
13668 Act_F_Type := Base_Type (Act_F_Type);
13670 -- If the formal is controlling, then the
13671 -- the type of the actual primitive's formal
13672 -- must be derived directly or indirectly
13673 -- from the type of the ancestor primitive's
13674 -- formal.
13676 if Is_Controlling_Formal (Anc_Formal) then
13677 if not Is_Tagged_Ancestor
13678 (Anc_F_Type, Act_F_Type)
13679 then
13680 exit;
13681 end if;
13683 -- Otherwise the types of the formals must
13684 -- be the same.
13686 elsif Anc_F_Type /= Act_F_Type then
13687 exit;
13688 end if;
13690 Next_Formal (Anc_Formal);
13691 Next_Formal (Act_Formal);
13692 end loop;
13694 -- If we traversed through all of the formals
13695 -- then so far the subprograms correspond, so
13696 -- now check that any result types correspond.
13698 if No (Anc_Formal) and then No (Act_Formal) then
13699 Subprograms_Correspond := True;
13701 if Ekind (Act_Subp) = E_Function then
13702 Anc_F_Type := Etype (Anc_Subp);
13703 Act_F_Type := Etype (Act_Subp);
13705 if Ekind (Anc_F_Type) =
13706 E_Anonymous_Access_Type
13707 then
13708 Anc_F_Type :=
13709 Designated_Type (Anc_F_Type);
13711 if Ekind (Act_F_Type) =
13712 E_Anonymous_Access_Type
13713 then
13714 Act_F_Type :=
13715 Designated_Type (Act_F_Type);
13716 else
13717 Subprograms_Correspond := False;
13718 end if;
13720 elsif
13721 Ekind (Act_F_Type)
13722 = E_Anonymous_Access_Type
13723 then
13724 Subprograms_Correspond := False;
13725 end if;
13727 Anc_F_Type := Base_Type (Anc_F_Type);
13728 Act_F_Type := Base_Type (Act_F_Type);
13730 -- Now either the result types must be
13731 -- the same or, if the result type is
13732 -- controlling, the result type of the
13733 -- actual primitive must descend from the
13734 -- result type of the ancestor primitive.
13736 if Subprograms_Correspond
13737 and then Anc_F_Type /= Act_F_Type
13738 and then
13739 Has_Controlling_Result (Anc_Subp)
13740 and then not Is_Tagged_Ancestor
13741 (Anc_F_Type, Act_F_Type)
13742 then
13743 Subprograms_Correspond := False;
13744 end if;
13745 end if;
13747 -- Found a matching subprogram belonging to
13748 -- formal ancestor type, so actual subprogram
13749 -- corresponds and this violates 3.9.3(9).
13751 if Subprograms_Correspond then
13752 Error_Msg_NE
13753 ("abstract subprogram & overrides "
13754 & "nonabstract subprogram of ancestor",
13755 Actual, Act_Subp);
13756 end if;
13757 end if;
13758 end if;
13759 end if;
13761 Next_Elmt (Act_Elmt);
13762 end loop;
13763 end if;
13765 Next_Elmt (Gen_Elmt);
13766 end loop;
13767 end Check_Abstract_Primitives;
13768 end if;
13770 -- Verify that limitedness matches. If parent is a limited
13771 -- interface then the generic formal is not unless declared
13772 -- explicitly so. If not declared limited, the actual cannot be
13773 -- limited (see AI05-0087).
13775 if Is_Limited_Type (Act_T) and then not Is_Limited_Type (A_Gen_T) then
13776 if not In_Instance then
13777 Error_Msg_NE
13778 ("actual for non-limited & cannot be a limited type",
13779 Actual, Gen_T);
13780 Explain_Limited_Type (Act_T, Actual);
13781 Abandon_Instantiation (Actual);
13782 end if;
13783 end if;
13785 -- Check for AI12-0036
13787 declare
13788 Formal_Is_Private_Extension : constant Boolean :=
13789 Nkind (Parent (A_Gen_T)) = N_Private_Extension_Declaration;
13791 Actual_Is_Tagged : constant Boolean := Is_Tagged_Type (Act_T);
13793 begin
13794 if Actual_Is_Tagged /= Formal_Is_Private_Extension then
13795 if not In_Instance then
13796 if Actual_Is_Tagged then
13797 Error_Msg_NE
13798 ("actual for & cannot be a tagged type", Actual, Gen_T);
13799 else
13800 Error_Msg_NE
13801 ("actual for & must be a tagged type", Actual, Gen_T);
13802 end if;
13804 Abandon_Instantiation (Actual);
13805 end if;
13806 end if;
13807 end;
13808 end Validate_Derived_Type_Instance;
13810 ----------------------------------------
13811 -- Validate_Discriminated_Formal_Type --
13812 ----------------------------------------
13814 procedure Validate_Discriminated_Formal_Type is
13815 Formal_Discr : Entity_Id;
13816 Actual_Discr : Entity_Id;
13817 Formal_Subt : Entity_Id;
13819 begin
13820 if Has_Discriminants (A_Gen_T) then
13821 if not Has_Discriminants (Act_T) then
13822 Error_Msg_NE
13823 ("actual for & must have discriminants", Actual, Gen_T);
13824 Abandon_Instantiation (Actual);
13826 elsif Is_Constrained (Act_T) then
13827 Error_Msg_NE
13828 ("actual for & must be unconstrained", Actual, Gen_T);
13829 Abandon_Instantiation (Actual);
13831 else
13832 Formal_Discr := First_Discriminant (A_Gen_T);
13833 Actual_Discr := First_Discriminant (Act_T);
13834 while Formal_Discr /= Empty loop
13835 if Actual_Discr = Empty then
13836 Error_Msg_N
13837 ("discriminants on actual do not match formal",
13838 Actual);
13839 Abandon_Instantiation (Actual);
13840 end if;
13842 Formal_Subt := Get_Instance_Of (Etype (Formal_Discr));
13844 -- Access discriminants match if designated types do
13846 if Ekind (Base_Type (Formal_Subt)) = E_Anonymous_Access_Type
13847 and then (Ekind (Base_Type (Etype (Actual_Discr)))) =
13848 E_Anonymous_Access_Type
13849 and then
13850 Get_Instance_Of
13851 (Designated_Type (Base_Type (Formal_Subt))) =
13852 Designated_Type (Base_Type (Etype (Actual_Discr)))
13853 then
13854 null;
13856 elsif Base_Type (Formal_Subt) /=
13857 Base_Type (Etype (Actual_Discr))
13858 then
13859 Error_Msg_N
13860 ("types of actual discriminants must match formal",
13861 Actual);
13862 Abandon_Instantiation (Actual);
13864 elsif not Subtypes_Statically_Match
13865 (Formal_Subt, Etype (Actual_Discr))
13866 and then Ada_Version >= Ada_95
13867 then
13868 Error_Msg_N
13869 ("subtypes of actual discriminants must match formal",
13870 Actual);
13871 Abandon_Instantiation (Actual);
13872 end if;
13874 Next_Discriminant (Formal_Discr);
13875 Next_Discriminant (Actual_Discr);
13876 end loop;
13878 if Actual_Discr /= Empty then
13879 Error_Msg_NE
13880 ("discriminants on actual do not match formal",
13881 Actual, Gen_T);
13882 Abandon_Instantiation (Actual);
13883 end if;
13884 end if;
13885 end if;
13886 end Validate_Discriminated_Formal_Type;
13888 ---------------------------------------
13889 -- Validate_Incomplete_Type_Instance --
13890 ---------------------------------------
13892 procedure Validate_Incomplete_Type_Instance is
13893 begin
13894 if not Is_Tagged_Type (Act_T)
13895 and then Is_Tagged_Type (A_Gen_T)
13896 then
13897 Error_Msg_NE
13898 ("actual for & must be a tagged type", Actual, Gen_T);
13899 end if;
13901 Validate_Discriminated_Formal_Type;
13902 end Validate_Incomplete_Type_Instance;
13904 --------------------------------------
13905 -- Validate_Interface_Type_Instance --
13906 --------------------------------------
13908 procedure Validate_Interface_Type_Instance is
13909 begin
13910 if not Is_Interface (Act_T) then
13911 Error_Msg_NE
13912 ("actual for formal interface type must be an interface",
13913 Actual, Gen_T);
13915 elsif Is_Limited_Type (Act_T) /= Is_Limited_Type (A_Gen_T)
13916 or else Is_Task_Interface (A_Gen_T) /= Is_Task_Interface (Act_T)
13917 or else Is_Protected_Interface (A_Gen_T) /=
13918 Is_Protected_Interface (Act_T)
13919 or else Is_Synchronized_Interface (A_Gen_T) /=
13920 Is_Synchronized_Interface (Act_T)
13921 then
13922 Error_Msg_NE
13923 ("actual for interface& does not match (RM 12.5.5(4))",
13924 Actual, Gen_T);
13925 end if;
13926 end Validate_Interface_Type_Instance;
13928 ------------------------------------
13929 -- Validate_Private_Type_Instance --
13930 ------------------------------------
13932 procedure Validate_Private_Type_Instance is
13933 begin
13934 if Is_Limited_Type (Act_T)
13935 and then not Is_Limited_Type (A_Gen_T)
13936 then
13937 if In_Instance then
13938 null;
13939 else
13940 Error_Msg_NE
13941 ("actual for non-limited & cannot be a limited type", Actual,
13942 Gen_T);
13943 Explain_Limited_Type (Act_T, Actual);
13944 Abandon_Instantiation (Actual);
13945 end if;
13947 elsif Known_To_Have_Preelab_Init (A_Gen_T)
13948 and then not Has_Preelaborable_Initialization (Act_T)
13949 then
13950 Error_Msg_NE
13951 ("actual for & must have preelaborable initialization", Actual,
13952 Gen_T);
13954 elsif not Is_Definite_Subtype (Act_T)
13955 and then Is_Definite_Subtype (A_Gen_T)
13956 and then Ada_Version >= Ada_95
13957 then
13958 Error_Msg_NE
13959 ("actual for & must be a definite subtype", Actual, Gen_T);
13961 elsif not Is_Tagged_Type (Act_T)
13962 and then Is_Tagged_Type (A_Gen_T)
13963 then
13964 Error_Msg_NE
13965 ("actual for & must be a tagged type", Actual, Gen_T);
13966 end if;
13968 Validate_Discriminated_Formal_Type;
13969 Ancestor := Gen_T;
13970 end Validate_Private_Type_Instance;
13972 -- Start of processing for Instantiate_Type
13974 begin
13975 if Get_Instance_Of (A_Gen_T) /= A_Gen_T then
13976 Error_Msg_N ("duplicate instantiation of generic type", Actual);
13977 return New_List (Error);
13979 elsif not Is_Entity_Name (Actual)
13980 or else not Is_Type (Entity (Actual))
13981 then
13982 Error_Msg_NE
13983 ("expect valid subtype mark to instantiate &", Actual, Gen_T);
13984 Abandon_Instantiation (Actual);
13986 else
13987 Act_T := Entity (Actual);
13989 -- Ada 2005 (AI-216): An Unchecked_Union subtype shall only be passed
13990 -- as a generic actual parameter if the corresponding formal type
13991 -- does not have a known_discriminant_part, or is a formal derived
13992 -- type that is an Unchecked_Union type.
13994 if Is_Unchecked_Union (Base_Type (Act_T)) then
13995 if not Has_Discriminants (A_Gen_T)
13996 or else (Is_Derived_Type (A_Gen_T)
13997 and then Is_Unchecked_Union (A_Gen_T))
13998 then
13999 null;
14000 else
14001 Error_Msg_N ("unchecked union cannot be the actual for a "
14002 & "discriminated formal type", Act_T);
14004 end if;
14005 end if;
14007 -- Deal with fixed/floating restrictions
14009 if Is_Floating_Point_Type (Act_T) then
14010 Check_Restriction (No_Floating_Point, Actual);
14011 elsif Is_Fixed_Point_Type (Act_T) then
14012 Check_Restriction (No_Fixed_Point, Actual);
14013 end if;
14015 -- Deal with error of using incomplete type as generic actual.
14016 -- This includes limited views of a type, even if the non-limited
14017 -- view may be available.
14019 if Ekind (Act_T) = E_Incomplete_Type
14020 or else (Is_Class_Wide_Type (Act_T)
14021 and then Ekind (Root_Type (Act_T)) = E_Incomplete_Type)
14022 then
14023 -- If the formal is an incomplete type, the actual can be
14024 -- incomplete as well, but if an actual incomplete type has
14025 -- a full view, then we'll retrieve that.
14027 if Ekind (A_Gen_T) = E_Incomplete_Type
14028 and then not Present (Full_View (Act_T))
14029 then
14030 null;
14032 elsif Is_Class_Wide_Type (Act_T)
14033 or else No (Full_View (Act_T))
14034 then
14035 Error_Msg_N ("premature use of incomplete type", Actual);
14036 Abandon_Instantiation (Actual);
14038 else
14039 Act_T := Full_View (Act_T);
14040 Set_Entity (Actual, Act_T);
14042 if Has_Private_Component (Act_T) then
14043 Error_Msg_N
14044 ("premature use of type with private component", Actual);
14045 end if;
14046 end if;
14048 -- Deal with error of premature use of private type as generic actual
14050 elsif Is_Private_Type (Act_T)
14051 and then Is_Private_Type (Base_Type (Act_T))
14052 and then not Is_Generic_Type (Act_T)
14053 and then not Is_Derived_Type (Act_T)
14054 and then No (Full_View (Root_Type (Act_T)))
14055 then
14056 -- If the formal is an incomplete type, the actual can be
14057 -- private or incomplete as well.
14059 if Ekind (A_Gen_T) = E_Incomplete_Type then
14060 null;
14061 else
14062 Error_Msg_N ("premature use of private type", Actual);
14063 end if;
14065 elsif Has_Private_Component (Act_T) then
14066 Error_Msg_N
14067 ("premature use of type with private component", Actual);
14068 end if;
14070 Set_Instance_Of (A_Gen_T, Act_T);
14072 -- If the type is generic, the class-wide type may also be used
14074 if Is_Tagged_Type (A_Gen_T)
14075 and then Is_Tagged_Type (Act_T)
14076 and then not Is_Class_Wide_Type (A_Gen_T)
14077 then
14078 Set_Instance_Of (Class_Wide_Type (A_Gen_T),
14079 Class_Wide_Type (Act_T));
14080 end if;
14082 if not Is_Abstract_Type (A_Gen_T)
14083 and then Is_Abstract_Type (Act_T)
14084 then
14085 Error_Msg_N
14086 ("actual of non-abstract formal cannot be abstract", Actual);
14087 end if;
14089 -- A generic scalar type is a first subtype for which we generate
14090 -- an anonymous base type. Indicate that the instance of this base
14091 -- is the base type of the actual.
14093 if Is_Scalar_Type (A_Gen_T) then
14094 Set_Instance_Of (Etype (A_Gen_T), Etype (Act_T));
14095 end if;
14096 end if;
14098 Check_Shared_Variable_Control_Aspects;
14100 if Error_Posted (Act_T) then
14101 null;
14102 else
14103 case Nkind (Def) is
14104 when N_Formal_Private_Type_Definition =>
14105 Validate_Private_Type_Instance;
14107 when N_Formal_Incomplete_Type_Definition =>
14108 Validate_Incomplete_Type_Instance;
14110 when N_Formal_Derived_Type_Definition =>
14111 Validate_Derived_Type_Instance;
14113 when N_Formal_Discrete_Type_Definition =>
14114 if not Is_Discrete_Type (Act_T) then
14115 Error_Msg_NE
14116 ("expect discrete type in instantiation of&",
14117 Actual, Gen_T);
14118 Abandon_Instantiation (Actual);
14119 end if;
14121 Diagnose_Predicated_Actual;
14123 when N_Formal_Signed_Integer_Type_Definition =>
14124 if not Is_Signed_Integer_Type (Act_T) then
14125 Error_Msg_NE
14126 ("expect signed integer type in instantiation of&",
14127 Actual, Gen_T);
14128 Abandon_Instantiation (Actual);
14129 end if;
14131 Diagnose_Predicated_Actual;
14133 when N_Formal_Modular_Type_Definition =>
14134 if not Is_Modular_Integer_Type (Act_T) then
14135 Error_Msg_NE
14136 ("expect modular type in instantiation of &",
14137 Actual, Gen_T);
14138 Abandon_Instantiation (Actual);
14139 end if;
14141 Diagnose_Predicated_Actual;
14143 when N_Formal_Floating_Point_Definition =>
14144 if not Is_Floating_Point_Type (Act_T) then
14145 Error_Msg_NE
14146 ("expect float type in instantiation of &", Actual, Gen_T);
14147 Abandon_Instantiation (Actual);
14148 end if;
14150 when N_Formal_Ordinary_Fixed_Point_Definition =>
14151 if not Is_Ordinary_Fixed_Point_Type (Act_T) then
14152 Error_Msg_NE
14153 ("expect ordinary fixed point type in instantiation of &",
14154 Actual, Gen_T);
14155 Abandon_Instantiation (Actual);
14156 end if;
14158 when N_Formal_Decimal_Fixed_Point_Definition =>
14159 if not Is_Decimal_Fixed_Point_Type (Act_T) then
14160 Error_Msg_NE
14161 ("expect decimal type in instantiation of &",
14162 Actual, Gen_T);
14163 Abandon_Instantiation (Actual);
14164 end if;
14166 when N_Array_Type_Definition =>
14167 Validate_Array_Type_Instance;
14169 when N_Access_To_Object_Definition =>
14170 Validate_Access_Type_Instance;
14172 when N_Access_Function_Definition
14173 | N_Access_Procedure_Definition
14175 Validate_Access_Subprogram_Instance;
14177 when N_Record_Definition =>
14178 Validate_Interface_Type_Instance;
14180 when N_Derived_Type_Definition =>
14181 Validate_Derived_Interface_Type_Instance;
14183 when others =>
14184 raise Program_Error;
14185 end case;
14186 end if;
14188 Subt := New_Copy (Gen_T);
14190 -- Use adjusted sloc of subtype name as the location for other nodes in
14191 -- the subtype declaration.
14193 Loc := Sloc (Subt);
14195 Decl_Node :=
14196 Make_Subtype_Declaration (Loc,
14197 Defining_Identifier => Subt,
14198 Subtype_Indication => New_Occurrence_Of (Act_T, Loc));
14200 -- Record whether the actual is private at this point, so that
14201 -- Check_Generic_Actuals can restore its proper view before the
14202 -- semantic analysis of the instance.
14204 if Is_Private_Type (Act_T) then
14205 Set_Has_Private_View (Subtype_Indication (Decl_Node));
14206 end if;
14208 -- In Ada 2012 the actual may be a limited view. Indicate that
14209 -- the local subtype must be treated as such.
14211 if From_Limited_With (Act_T) then
14212 Mutate_Ekind (Subt, E_Incomplete_Subtype);
14213 Set_From_Limited_With (Subt);
14214 end if;
14216 Decl_Nodes := New_List (Decl_Node);
14218 -- Flag actual derived types so their elaboration produces the
14219 -- appropriate renamings for the primitive operations of the ancestor.
14220 -- Flag actual for formal private types as well, to determine whether
14221 -- operations in the private part may override inherited operations.
14222 -- If the formal has an interface list, the ancestor is not the
14223 -- parent, but the analyzed formal that includes the interface
14224 -- operations of all its progenitors.
14226 -- Same treatment for formal private types, so we can check whether the
14227 -- type is tagged limited when validating derivations in the private
14228 -- part. (See AI05-096).
14230 if Nkind (Def) = N_Formal_Derived_Type_Definition then
14231 if Present (Interface_List (Def)) then
14232 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
14233 else
14234 Set_Generic_Parent_Type (Decl_Node, Ancestor);
14235 end if;
14237 elsif Nkind (Def) in N_Formal_Private_Type_Definition
14238 | N_Formal_Incomplete_Type_Definition
14239 then
14240 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
14241 end if;
14243 -- If the actual is a synchronized type that implements an interface,
14244 -- the primitive operations are attached to the corresponding record,
14245 -- and we have to treat it as an additional generic actual, so that its
14246 -- primitive operations become visible in the instance. The task or
14247 -- protected type itself does not carry primitive operations.
14249 if Is_Concurrent_Type (Act_T)
14250 and then Is_Tagged_Type (Act_T)
14251 and then Present (Corresponding_Record_Type (Act_T))
14252 and then Present (Ancestor)
14253 and then Is_Interface (Ancestor)
14254 then
14255 declare
14256 Corr_Rec : constant Entity_Id :=
14257 Corresponding_Record_Type (Act_T);
14258 New_Corr : Entity_Id;
14259 Corr_Decl : Node_Id;
14261 begin
14262 New_Corr := Make_Temporary (Loc, 'S');
14263 Corr_Decl :=
14264 Make_Subtype_Declaration (Loc,
14265 Defining_Identifier => New_Corr,
14266 Subtype_Indication =>
14267 New_Occurrence_Of (Corr_Rec, Loc));
14268 Append_To (Decl_Nodes, Corr_Decl);
14270 if Ekind (Act_T) = E_Task_Type then
14271 Mutate_Ekind (Subt, E_Task_Subtype);
14272 else
14273 Mutate_Ekind (Subt, E_Protected_Subtype);
14274 end if;
14276 Set_Corresponding_Record_Type (Subt, Corr_Rec);
14277 Set_Generic_Parent_Type (Corr_Decl, Ancestor);
14278 Set_Generic_Parent_Type (Decl_Node, Empty);
14279 end;
14280 end if;
14282 -- For a floating-point type, capture dimension info if any, because
14283 -- the generated subtype declaration does not come from source and
14284 -- will not process dimensions.
14286 if Is_Floating_Point_Type (Act_T) then
14287 Copy_Dimensions (Act_T, Subt);
14288 end if;
14290 return Decl_Nodes;
14291 end Instantiate_Type;
14293 -----------------------------
14294 -- Is_Abbreviated_Instance --
14295 -----------------------------
14297 function Is_Abbreviated_Instance (E : Entity_Id) return Boolean is
14298 begin
14299 return Ekind (E) = E_Package
14300 and then Present (Hidden_In_Formal_Instance (E));
14301 end Is_Abbreviated_Instance;
14303 ---------------------
14304 -- Is_In_Main_Unit --
14305 ---------------------
14307 function Is_In_Main_Unit (N : Node_Id) return Boolean is
14308 Unum : constant Unit_Number_Type := Get_Source_Unit (N);
14309 Current_Unit : Node_Id;
14311 begin
14312 if Unum = Main_Unit then
14313 return True;
14315 -- If the current unit is a subunit then it is either the main unit or
14316 -- is being compiled as part of the main unit.
14318 elsif Nkind (N) = N_Compilation_Unit then
14319 return Nkind (Unit (N)) = N_Subunit;
14320 end if;
14322 Current_Unit := Parent (N);
14323 while Present (Current_Unit)
14324 and then Nkind (Current_Unit) /= N_Compilation_Unit
14325 loop
14326 Current_Unit := Parent (Current_Unit);
14327 end loop;
14329 -- The instantiation node is in the main unit, or else the current node
14330 -- (perhaps as the result of nested instantiations) is in the main unit,
14331 -- or in the declaration of the main unit, which in this last case must
14332 -- be a body.
14334 return
14335 Current_Unit = Cunit (Main_Unit)
14336 or else Current_Unit = Library_Unit (Cunit (Main_Unit))
14337 or else (Present (Current_Unit)
14338 and then Present (Library_Unit (Current_Unit))
14339 and then Is_In_Main_Unit (Library_Unit (Current_Unit)));
14340 end Is_In_Main_Unit;
14342 ----------------------------
14343 -- Load_Parent_Of_Generic --
14344 ----------------------------
14346 procedure Load_Parent_Of_Generic
14347 (N : Node_Id;
14348 Spec : Node_Id;
14349 Body_Optional : Boolean := False)
14351 Comp_Unit : constant Node_Id := Cunit (Get_Source_Unit (Spec));
14352 Saved_Style_Check : constant Boolean := Style_Check;
14353 Saved_Warnings : constant Warning_Record := Save_Warnings;
14354 True_Parent : Node_Id;
14355 Inst_Node : Node_Id;
14356 OK : Boolean;
14357 Previous_Instances : constant Elist_Id := New_Elmt_List;
14359 procedure Collect_Previous_Instances (Decls : List_Id);
14360 -- Collect all instantiations in the given list of declarations, that
14361 -- precede the generic that we need to load. If the bodies of these
14362 -- instantiations are available, we must analyze them, to ensure that
14363 -- the public symbols generated are the same when the unit is compiled
14364 -- to generate code, and when it is compiled in the context of a unit
14365 -- that needs a particular nested instance. This process is applied to
14366 -- both package and subprogram instances.
14368 --------------------------------
14369 -- Collect_Previous_Instances --
14370 --------------------------------
14372 procedure Collect_Previous_Instances (Decls : List_Id) is
14373 Decl : Node_Id;
14375 begin
14376 Decl := First (Decls);
14377 while Present (Decl) loop
14378 if Sloc (Decl) >= Sloc (Inst_Node) then
14379 return;
14381 -- If Decl is an instantiation, then record it as requiring
14382 -- instantiation of the corresponding body, except if it is an
14383 -- abbreviated instantiation generated internally for conformance
14384 -- checking purposes only for the case of a formal package
14385 -- declared without a box (see Instantiate_Formal_Package). Such
14386 -- an instantiation does not generate any code (the actual code
14387 -- comes from actual) and thus does not need to be analyzed here.
14388 -- If the instantiation appears with a generic package body it is
14389 -- not analyzed here either.
14391 elsif Nkind (Decl) = N_Package_Instantiation
14392 and then not Is_Abbreviated_Instance (Defining_Entity (Decl))
14393 then
14394 Append_Elmt (Decl, Previous_Instances);
14396 -- For a subprogram instantiation, omit instantiations intrinsic
14397 -- operations (Unchecked_Conversions, etc.) that have no bodies.
14399 elsif Nkind (Decl) in N_Function_Instantiation
14400 | N_Procedure_Instantiation
14401 and then not Is_Intrinsic_Subprogram (Entity (Name (Decl)))
14402 then
14403 Append_Elmt (Decl, Previous_Instances);
14405 elsif Nkind (Decl) = N_Package_Declaration then
14406 Collect_Previous_Instances
14407 (Visible_Declarations (Specification (Decl)));
14408 Collect_Previous_Instances
14409 (Private_Declarations (Specification (Decl)));
14411 -- Previous non-generic bodies may contain instances as well
14413 elsif Nkind (Decl) = N_Package_Body
14414 and then Ekind (Corresponding_Spec (Decl)) /= E_Generic_Package
14415 then
14416 Collect_Previous_Instances (Declarations (Decl));
14418 elsif Nkind (Decl) = N_Subprogram_Body
14419 and then not Acts_As_Spec (Decl)
14420 and then not Is_Generic_Subprogram (Corresponding_Spec (Decl))
14421 then
14422 Collect_Previous_Instances (Declarations (Decl));
14423 end if;
14425 Next (Decl);
14426 end loop;
14427 end Collect_Previous_Instances;
14429 -- Start of processing for Load_Parent_Of_Generic
14431 begin
14432 if not In_Same_Source_Unit (N, Spec)
14433 or else Nkind (Unit (Comp_Unit)) = N_Package_Declaration
14434 or else (Nkind (Unit (Comp_Unit)) = N_Package_Body
14435 and then not Is_In_Main_Unit (Spec))
14436 then
14437 -- Find body of parent of spec, and analyze it. A special case arises
14438 -- when the parent is an instantiation, that is to say when we are
14439 -- currently instantiating a nested generic. In that case, there is
14440 -- no separate file for the body of the enclosing instance. Instead,
14441 -- the enclosing body must be instantiated as if it were a pending
14442 -- instantiation, in order to produce the body for the nested generic
14443 -- we require now. Note that in that case the generic may be defined
14444 -- in a package body, the instance defined in the same package body,
14445 -- and the original enclosing body may not be in the main unit.
14447 Inst_Node := Empty;
14449 True_Parent := Parent (Spec);
14450 while Present (True_Parent)
14451 and then Nkind (True_Parent) /= N_Compilation_Unit
14452 loop
14453 if Nkind (True_Parent) = N_Package_Declaration
14454 and then
14455 Nkind (Original_Node (True_Parent)) = N_Package_Instantiation
14456 then
14457 -- Parent is a compilation unit that is an instantiation, and
14458 -- instantiation node has been replaced with package decl.
14460 Inst_Node := Original_Node (True_Parent);
14461 exit;
14463 elsif Nkind (True_Parent) = N_Package_Declaration
14464 and then Nkind (Parent (True_Parent)) = N_Compilation_Unit
14465 and then
14466 Nkind (Unit (Parent (True_Parent))) = N_Package_Instantiation
14467 then
14468 -- Parent is a compilation unit that is an instantiation, but
14469 -- instantiation node has not been replaced with package decl.
14471 Inst_Node := Unit (Parent (True_Parent));
14472 exit;
14474 elsif Nkind (True_Parent) = N_Package_Declaration
14475 and then Nkind (Parent (True_Parent)) /= N_Compilation_Unit
14476 and then Present (Generic_Parent (Specification (True_Parent)))
14477 then
14478 -- Parent is an instantiation within another specification.
14479 -- Declaration for instance has been inserted before original
14480 -- instantiation node. A direct link would be preferable?
14482 Inst_Node := Next (True_Parent);
14483 while Present (Inst_Node)
14484 and then Nkind (Inst_Node) /= N_Package_Instantiation
14485 loop
14486 Next (Inst_Node);
14487 end loop;
14489 -- If the instance appears within a generic, and the generic
14490 -- unit is defined within a formal package of the enclosing
14491 -- generic, there is no generic body available, and none
14492 -- needed. A more precise test should be used ???
14494 if No (Inst_Node) then
14495 return;
14496 end if;
14498 exit;
14500 -- If an ancestor of the generic comes from a formal package
14501 -- there is no source for the ancestor body. This is detected
14502 -- by examining the scope of the ancestor and its declaration.
14503 -- The body, if any is needed, will be available when the
14504 -- current unit (containing a formal package) is instantiated.
14506 elsif Nkind (True_Parent) = N_Package_Specification
14507 and then Present (Generic_Parent (True_Parent))
14508 and then Nkind
14509 (Original_Node (Unit_Declaration_Node
14510 (Scope (Generic_Parent (True_Parent)))))
14511 = N_Formal_Package_Declaration
14512 then
14513 return;
14515 else
14516 True_Parent := Parent (True_Parent);
14517 end if;
14518 end loop;
14520 -- Case where we are currently instantiating a nested generic
14522 if Present (Inst_Node) then
14523 if Nkind (Parent (True_Parent)) = N_Compilation_Unit then
14525 -- Instantiation node and declaration of instantiated package
14526 -- were exchanged when only the declaration was needed.
14527 -- Restore instantiation node before proceeding with body.
14529 Set_Unit (Parent (True_Parent), Inst_Node);
14530 end if;
14532 -- Now complete instantiation of enclosing body, if it appears in
14533 -- some other unit. If it appears in the current unit, the body
14534 -- will have been instantiated already.
14536 if No (Corresponding_Body (Instance_Spec (Inst_Node))) then
14538 -- We need to determine the expander mode to instantiate the
14539 -- enclosing body. Because the generic body we need may use
14540 -- global entities declared in the enclosing package (including
14541 -- aggregates) it is in general necessary to compile this body
14542 -- with expansion enabled, except if we are within a generic
14543 -- package, in which case the usual generic rule applies.
14545 declare
14546 Exp_Status : Boolean := True;
14547 Scop : Entity_Id;
14549 begin
14550 -- Loop through scopes looking for generic package
14552 Scop := Scope (Defining_Entity (Instance_Spec (Inst_Node)));
14553 while Present (Scop)
14554 and then Scop /= Standard_Standard
14555 loop
14556 if Ekind (Scop) = E_Generic_Package then
14557 Exp_Status := False;
14558 exit;
14559 end if;
14561 Scop := Scope (Scop);
14562 end loop;
14564 -- Collect previous instantiations in the unit that contains
14565 -- the desired generic.
14567 if Nkind (Parent (True_Parent)) /= N_Compilation_Unit
14568 and then not Body_Optional
14569 then
14570 declare
14571 Decl : Elmt_Id;
14572 Info : Pending_Body_Info;
14573 Par : Node_Id;
14575 begin
14576 Par := Parent (Inst_Node);
14577 while Present (Par) loop
14578 exit when Nkind (Parent (Par)) = N_Compilation_Unit;
14579 Par := Parent (Par);
14580 end loop;
14582 pragma Assert (Present (Par));
14584 if Nkind (Par) = N_Package_Body then
14585 Collect_Previous_Instances (Declarations (Par));
14587 elsif Nkind (Par) = N_Package_Declaration then
14588 Collect_Previous_Instances
14589 (Visible_Declarations (Specification (Par)));
14590 Collect_Previous_Instances
14591 (Private_Declarations (Specification (Par)));
14593 else
14594 -- Enclosing unit is a subprogram body. In this
14595 -- case all instance bodies are processed in order
14596 -- and there is no need to collect them separately.
14598 null;
14599 end if;
14601 Decl := First_Elmt (Previous_Instances);
14602 while Present (Decl) loop
14603 Info :=
14604 (Act_Decl =>
14605 Instance_Spec (Node (Decl)),
14606 Config_Switches => Save_Config_Switches,
14607 Current_Sem_Unit =>
14608 Get_Code_Unit (Sloc (Node (Decl))),
14609 Expander_Status => Exp_Status,
14610 Inst_Node => Node (Decl),
14611 Local_Suppress_Stack_Top =>
14612 Local_Suppress_Stack_Top,
14613 Scope_Suppress => Scope_Suppress,
14614 Warnings => Save_Warnings);
14616 -- Package instance
14618 if Nkind (Node (Decl)) = N_Package_Instantiation
14619 then
14620 Instantiate_Package_Body
14621 (Info, Body_Optional => True);
14623 -- Subprogram instance
14625 else
14626 -- The instance_spec is in the wrapper package,
14627 -- usually followed by its local renaming
14628 -- declaration. See Build_Subprogram_Renaming
14629 -- for details. If the instance carries aspects,
14630 -- these result in the corresponding pragmas,
14631 -- inserted after the subprogram declaration.
14632 -- They must be skipped as well when retrieving
14633 -- the desired spec. Some of them may have been
14634 -- rewritten as null statements.
14635 -- A direct link would be more robust ???
14637 declare
14638 Decl : Node_Id :=
14639 (Last (Visible_Declarations
14640 (Specification (Info.Act_Decl))));
14641 begin
14642 while Nkind (Decl) in
14643 N_Null_Statement |
14644 N_Pragma |
14645 N_Subprogram_Renaming_Declaration
14646 loop
14647 Decl := Prev (Decl);
14648 end loop;
14650 Info.Act_Decl := Decl;
14651 end;
14653 Instantiate_Subprogram_Body
14654 (Info, Body_Optional => True);
14655 end if;
14657 Next_Elmt (Decl);
14658 end loop;
14659 end;
14660 end if;
14662 Instantiate_Package_Body
14663 (Body_Info =>
14664 ((Act_Decl => True_Parent,
14665 Config_Switches => Save_Config_Switches,
14666 Current_Sem_Unit =>
14667 Get_Code_Unit (Sloc (Inst_Node)),
14668 Expander_Status => Exp_Status,
14669 Inst_Node => Inst_Node,
14670 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
14671 Scope_Suppress => Scope_Suppress,
14672 Warnings => Save_Warnings)),
14673 Body_Optional => Body_Optional);
14674 end;
14675 end if;
14677 -- Case where we are not instantiating a nested generic
14679 else
14680 Opt.Style_Check := False;
14681 Expander_Mode_Save_And_Set (True);
14682 Load_Needed_Body (Comp_Unit, OK);
14683 Opt.Style_Check := Saved_Style_Check;
14684 Restore_Warnings (Saved_Warnings);
14685 Expander_Mode_Restore;
14687 if not OK
14688 and then Unit_Requires_Body (Defining_Entity (Spec))
14689 and then not Body_Optional
14690 then
14691 declare
14692 Bname : constant Unit_Name_Type :=
14693 Get_Body_Name (Get_Unit_Name (Unit (Comp_Unit)));
14695 begin
14696 -- In CodePeer mode, the missing body may make the analysis
14697 -- incomplete, but we do not treat it as fatal.
14699 if CodePeer_Mode then
14700 return;
14702 else
14703 Error_Msg_Unit_1 := Bname;
14704 Error_Msg_N ("this instantiation requires$!", N);
14705 Error_Msg_File_1 :=
14706 Get_File_Name (Bname, Subunit => False);
14707 Error_Msg_N ("\but file{ was not found!", N);
14708 raise Unrecoverable_Error;
14709 end if;
14710 end;
14711 end if;
14712 end if;
14713 end if;
14715 -- If loading parent of the generic caused an instantiation circularity,
14716 -- we abandon compilation at this point, because otherwise in some cases
14717 -- we get into trouble with infinite recursions after this point.
14719 if Circularity_Detected then
14720 raise Unrecoverable_Error;
14721 end if;
14722 end Load_Parent_Of_Generic;
14724 ---------------------------------
14725 -- Map_Formal_Package_Entities --
14726 ---------------------------------
14728 procedure Map_Formal_Package_Entities (Form : Entity_Id; Act : Entity_Id) is
14729 E1 : Entity_Id;
14730 E2 : Entity_Id;
14732 begin
14733 Set_Instance_Of (Form, Act);
14735 -- Traverse formal and actual package to map the corresponding entities.
14736 -- We skip over internal entities that may be generated during semantic
14737 -- analysis, and find the matching entities by name, given that they
14738 -- must appear in the same order.
14740 E1 := First_Entity (Form);
14741 E2 := First_Entity (Act);
14742 while Present (E1) and then E1 /= First_Private_Entity (Form) loop
14743 -- Could this test be a single condition??? Seems like it could, and
14744 -- isn't FPE (Form) a constant anyway???
14746 if not Is_Internal (E1)
14747 and then Present (Parent (E1))
14748 and then not Is_Class_Wide_Type (E1)
14749 and then not Is_Internal_Name (Chars (E1))
14750 then
14751 while Present (E2) and then Chars (E2) /= Chars (E1) loop
14752 Next_Entity (E2);
14753 end loop;
14755 if No (E2) then
14756 exit;
14757 else
14758 Set_Instance_Of (E1, E2);
14760 if Is_Type (E1) and then Is_Tagged_Type (E2) then
14761 Set_Instance_Of (Class_Wide_Type (E1), Class_Wide_Type (E2));
14762 end if;
14764 if Is_Constrained (E1) then
14765 Set_Instance_Of (Base_Type (E1), Base_Type (E2));
14766 end if;
14768 if Ekind (E1) = E_Package and then No (Renamed_Entity (E1)) then
14769 Map_Formal_Package_Entities (E1, E2);
14770 end if;
14771 end if;
14772 end if;
14774 Next_Entity (E1);
14775 end loop;
14776 end Map_Formal_Package_Entities;
14778 -----------------------
14779 -- Move_Freeze_Nodes --
14780 -----------------------
14782 procedure Move_Freeze_Nodes
14783 (Out_Of : Entity_Id;
14784 After : Node_Id;
14785 L : List_Id)
14787 Decl : Node_Id;
14788 Next_Decl : Node_Id;
14789 Next_Node : Node_Id := After;
14790 Spec : Node_Id;
14792 function Is_Outer_Type (T : Entity_Id) return Boolean;
14793 -- Check whether entity is declared in a scope external to that of the
14794 -- generic unit.
14796 -------------------
14797 -- Is_Outer_Type --
14798 -------------------
14800 function Is_Outer_Type (T : Entity_Id) return Boolean is
14801 Scop : Entity_Id := Scope (T);
14803 begin
14804 if Scope_Depth (Scop) < Scope_Depth (Out_Of) then
14805 return True;
14807 else
14808 while Scop /= Standard_Standard loop
14809 if Scop = Out_Of then
14810 return False;
14811 else
14812 Scop := Scope (Scop);
14813 end if;
14814 end loop;
14816 return True;
14817 end if;
14818 end Is_Outer_Type;
14820 -- Start of processing for Move_Freeze_Nodes
14822 begin
14823 if No (L) then
14824 return;
14825 end if;
14827 -- First remove the freeze nodes that may appear before all other
14828 -- declarations.
14830 Decl := First (L);
14831 while Present (Decl)
14832 and then Nkind (Decl) = N_Freeze_Entity
14833 and then Is_Outer_Type (Entity (Decl))
14834 loop
14835 Decl := Remove_Head (L);
14836 Insert_After (Next_Node, Decl);
14837 Set_Analyzed (Decl, False);
14838 Next_Node := Decl;
14839 Decl := First (L);
14840 end loop;
14842 -- Next scan the list of declarations and remove each freeze node that
14843 -- appears ahead of the current node.
14845 while Present (Decl) loop
14846 while Present (Next (Decl))
14847 and then Nkind (Next (Decl)) = N_Freeze_Entity
14848 and then Is_Outer_Type (Entity (Next (Decl)))
14849 loop
14850 Next_Decl := Remove_Next (Decl);
14851 Insert_After (Next_Node, Next_Decl);
14852 Set_Analyzed (Next_Decl, False);
14853 Next_Node := Next_Decl;
14854 end loop;
14856 -- If the declaration is a nested package or concurrent type, then
14857 -- recurse. Nested generic packages will have been processed from the
14858 -- inside out.
14860 case Nkind (Decl) is
14861 when N_Package_Declaration =>
14862 Spec := Specification (Decl);
14864 when N_Task_Type_Declaration =>
14865 Spec := Task_Definition (Decl);
14867 when N_Protected_Type_Declaration =>
14868 Spec := Protected_Definition (Decl);
14870 when others =>
14871 Spec := Empty;
14872 end case;
14874 if Present (Spec) then
14875 Move_Freeze_Nodes (Out_Of, Next_Node, Visible_Declarations (Spec));
14876 Move_Freeze_Nodes (Out_Of, Next_Node, Private_Declarations (Spec));
14877 end if;
14879 Next (Decl);
14880 end loop;
14881 end Move_Freeze_Nodes;
14883 ----------------
14884 -- Next_Assoc --
14885 ----------------
14887 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr is
14888 begin
14889 return Generic_Renamings.Table (E).Next_In_HTable;
14890 end Next_Assoc;
14892 ------------------------
14893 -- Preanalyze_Actuals --
14894 ------------------------
14896 procedure Preanalyze_Actuals (N : Node_Id; Inst : Entity_Id := Empty) is
14897 procedure Perform_Appropriate_Analysis (N : Node_Id);
14898 -- Determine if the actuals we are analyzing come from a generic
14899 -- instantiation that is a library unit and dispatch accordingly.
14901 ----------------------------------
14902 -- Perform_Appropriate_Analysis --
14903 ----------------------------------
14905 procedure Perform_Appropriate_Analysis (N : Node_Id) is
14906 begin
14907 -- When we have a library instantiation we cannot allow any expansion
14908 -- to occur, since there may be no place to put it. Instead, in that
14909 -- case we perform a preanalysis of the actual.
14911 if Present (Inst) and then Is_Compilation_Unit (Inst) then
14912 Preanalyze (N);
14913 else
14914 Analyze (N);
14915 end if;
14916 end Perform_Appropriate_Analysis;
14918 -- Local variables
14920 Errs : constant Nat := Serious_Errors_Detected;
14922 Assoc : Node_Id;
14923 Act : Node_Id;
14925 Cur : Entity_Id := Empty;
14926 -- Current homograph of the instance name
14928 Vis : Boolean := False;
14929 -- Saved visibility status of the current homograph
14931 -- Start of processing for Preanalyze_Actuals
14933 begin
14934 Assoc := First (Generic_Associations (N));
14936 -- If the instance is a child unit, its name may hide an outer homonym,
14937 -- so make it invisible to perform name resolution on the actuals.
14939 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name
14940 and then Present
14941 (Current_Entity (Defining_Identifier (Defining_Unit_Name (N))))
14942 then
14943 Cur := Current_Entity (Defining_Identifier (Defining_Unit_Name (N)));
14945 if Is_Compilation_Unit (Cur) then
14946 Vis := Is_Immediately_Visible (Cur);
14947 Set_Is_Immediately_Visible (Cur, False);
14948 else
14949 Cur := Empty;
14950 end if;
14951 end if;
14953 while Present (Assoc) loop
14954 if Nkind (Assoc) /= N_Others_Choice then
14955 Act := Explicit_Generic_Actual_Parameter (Assoc);
14957 -- Within a nested instantiation, a defaulted actual is an empty
14958 -- association, so nothing to analyze. If the subprogram actual
14959 -- is an attribute, analyze prefix only, because actual is not a
14960 -- complete attribute reference.
14962 -- If actual is an allocator, analyze expression only. The full
14963 -- analysis can generate code, and if instance is a compilation
14964 -- unit we have to wait until the package instance is installed
14965 -- to have a proper place to insert this code.
14967 -- String literals may be operators, but at this point we do not
14968 -- know whether the actual is a formal subprogram or a string.
14970 if No (Act) then
14971 null;
14973 elsif Nkind (Act) = N_Attribute_Reference then
14974 Perform_Appropriate_Analysis (Prefix (Act));
14976 elsif Nkind (Act) = N_Explicit_Dereference then
14977 Perform_Appropriate_Analysis (Prefix (Act));
14979 elsif Nkind (Act) = N_Allocator then
14980 declare
14981 Expr : constant Node_Id := Expression (Act);
14983 begin
14984 if Nkind (Expr) = N_Subtype_Indication then
14985 Perform_Appropriate_Analysis (Subtype_Mark (Expr));
14987 -- Analyze separately each discriminant constraint, when
14988 -- given with a named association.
14990 declare
14991 Constr : Node_Id;
14993 begin
14994 Constr := First (Constraints (Constraint (Expr)));
14995 while Present (Constr) loop
14996 if Nkind (Constr) = N_Discriminant_Association then
14997 Perform_Appropriate_Analysis
14998 (Expression (Constr));
14999 else
15000 Perform_Appropriate_Analysis (Constr);
15001 end if;
15003 Next (Constr);
15004 end loop;
15005 end;
15007 else
15008 Perform_Appropriate_Analysis (Expr);
15009 end if;
15010 end;
15012 elsif Nkind (Act) /= N_Operator_Symbol then
15013 Perform_Appropriate_Analysis (Act);
15015 -- Within a package instance, mark actuals that are limited
15016 -- views, so their use can be moved to the body of the
15017 -- enclosing unit.
15019 if Is_Entity_Name (Act)
15020 and then Is_Type (Entity (Act))
15021 and then From_Limited_With (Entity (Act))
15022 and then Present (Inst)
15023 then
15024 Append_Elmt (Entity (Act), Incomplete_Actuals (Inst));
15025 end if;
15026 end if;
15028 if Errs /= Serious_Errors_Detected then
15030 -- Do a minimal analysis of the generic, to prevent spurious
15031 -- warnings complaining about the generic being unreferenced,
15032 -- before abandoning the instantiation.
15034 Perform_Appropriate_Analysis (Name (N));
15036 if Is_Entity_Name (Name (N))
15037 and then Etype (Name (N)) /= Any_Type
15038 then
15039 Generate_Reference (Entity (Name (N)), Name (N));
15040 Set_Is_Instantiated (Entity (Name (N)));
15041 end if;
15043 if Present (Cur) then
15045 -- For the case of a child instance hiding an outer homonym,
15046 -- provide additional warning which might explain the error.
15048 Set_Is_Immediately_Visible (Cur, Vis);
15049 Error_Msg_NE
15050 ("& hides outer unit with the same name??",
15051 N, Defining_Unit_Name (N));
15052 end if;
15054 Abandon_Instantiation (Act);
15055 end if;
15056 end if;
15058 Next (Assoc);
15059 end loop;
15061 if Present (Cur) then
15062 Set_Is_Immediately_Visible (Cur, Vis);
15063 end if;
15064 end Preanalyze_Actuals;
15066 -------------------------------
15067 -- Provide_Completing_Bodies --
15068 -------------------------------
15070 procedure Provide_Completing_Bodies (N : Node_Id) is
15071 procedure Build_Completing_Body (Subp_Decl : Node_Id);
15072 -- Generate the completing body for subprogram declaration Subp_Decl
15074 procedure Provide_Completing_Bodies_In (Decls : List_Id);
15075 -- Generating completing bodies for all subprograms found in declarative
15076 -- list Decls.
15078 ---------------------------
15079 -- Build_Completing_Body --
15080 ---------------------------
15082 procedure Build_Completing_Body (Subp_Decl : Node_Id) is
15083 Loc : constant Source_Ptr := Sloc (Subp_Decl);
15084 Subp_Id : constant Entity_Id := Defining_Entity (Subp_Decl);
15085 Spec : Node_Id;
15087 begin
15088 -- Nothing to do if the subprogram already has a completing body
15090 if Present (Corresponding_Body (Subp_Decl)) then
15091 return;
15093 -- Mark the function as having a valid return statement even though
15094 -- the body contains a single raise statement.
15096 elsif Ekind (Subp_Id) = E_Function then
15097 Set_Return_Present (Subp_Id);
15098 end if;
15100 -- Clone the specification to obtain new entities and reset the only
15101 -- semantic field.
15103 Spec := Copy_Subprogram_Spec (Specification (Subp_Decl));
15104 Set_Generic_Parent (Spec, Empty);
15106 -- Generate:
15107 -- function Func ... return ... is
15108 -- <or>
15109 -- procedure Proc ... is
15110 -- begin
15111 -- raise Program_Error with "access before elaboration";
15112 -- edn Proc;
15114 Insert_After_And_Analyze (Subp_Decl,
15115 Make_Subprogram_Body (Loc,
15116 Specification => Spec,
15117 Declarations => New_List,
15118 Handled_Statement_Sequence =>
15119 Make_Handled_Sequence_Of_Statements (Loc,
15120 Statements => New_List (
15121 Make_Raise_Program_Error (Loc,
15122 Reason => PE_Access_Before_Elaboration)))));
15123 end Build_Completing_Body;
15125 ----------------------------------
15126 -- Provide_Completing_Bodies_In --
15127 ----------------------------------
15129 procedure Provide_Completing_Bodies_In (Decls : List_Id) is
15130 Decl : Node_Id;
15132 begin
15133 if Present (Decls) then
15134 Decl := First (Decls);
15135 while Present (Decl) loop
15136 Provide_Completing_Bodies (Decl);
15137 Next (Decl);
15138 end loop;
15139 end if;
15140 end Provide_Completing_Bodies_In;
15142 -- Local variables
15144 Spec : Node_Id;
15146 -- Start of processing for Provide_Completing_Bodies
15148 begin
15149 if Nkind (N) = N_Package_Declaration then
15150 Spec := Specification (N);
15152 Push_Scope (Defining_Entity (N));
15153 Provide_Completing_Bodies_In (Visible_Declarations (Spec));
15154 Provide_Completing_Bodies_In (Private_Declarations (Spec));
15155 Pop_Scope;
15157 elsif Nkind (N) = N_Subprogram_Declaration then
15158 Build_Completing_Body (N);
15159 end if;
15160 end Provide_Completing_Bodies;
15162 -------------------
15163 -- Remove_Parent --
15164 -------------------
15166 procedure Remove_Parent (In_Body : Boolean := False) is
15167 S : Entity_Id := Current_Scope;
15168 -- S is the scope containing the instantiation just completed. The scope
15169 -- stack contains the parent instances of the instantiation, followed by
15170 -- the original S.
15172 Cur_P : Entity_Id;
15173 E : Entity_Id;
15174 P : Entity_Id;
15175 Hidden : Elmt_Id;
15177 begin
15178 -- After child instantiation is complete, remove from scope stack the
15179 -- extra copy of the current scope, and then remove parent instances.
15181 if not In_Body then
15182 Pop_Scope;
15184 while Current_Scope /= S loop
15185 P := Current_Scope;
15186 End_Package_Scope (Current_Scope);
15188 if In_Open_Scopes (P) then
15189 E := First_Entity (P);
15190 while Present (E) loop
15191 Set_Is_Immediately_Visible (E, True);
15192 Next_Entity (E);
15193 end loop;
15195 -- If instantiation is declared in a block, it is the enclosing
15196 -- scope that might be a parent instance. Note that only one
15197 -- block can be involved, because the parent instances have
15198 -- been installed within it.
15200 if Ekind (P) = E_Block then
15201 Cur_P := Scope (P);
15202 else
15203 Cur_P := P;
15204 end if;
15206 if Is_Generic_Instance (Cur_P) and then P /= Current_Scope then
15207 -- We are within an instance of some sibling. Retain
15208 -- visibility of parent, for proper subsequent cleanup, and
15209 -- reinstall private declarations as well.
15211 Set_In_Private_Part (P);
15212 Install_Private_Declarations (P);
15213 end if;
15215 -- If the ultimate parent is a top-level unit recorded in
15216 -- Instance_Parent_Unit, then reset its visibility to what it was
15217 -- before instantiation. (It's not clear what the purpose is of
15218 -- testing whether Scope (P) is In_Open_Scopes, but that test was
15219 -- present before the ultimate parent test was added.???)
15221 elsif not In_Open_Scopes (Scope (P))
15222 or else (P = Instance_Parent_Unit
15223 and then not Parent_Unit_Visible)
15224 then
15225 Set_Is_Immediately_Visible (P, False);
15227 -- If the current scope is itself an instantiation of a generic
15228 -- nested within P, and we are in the private part of body of this
15229 -- instantiation, restore the full views of P, that were removed
15230 -- in End_Package_Scope above. This obscure case can occur when a
15231 -- subunit of a generic contains an instance of a child unit of
15232 -- its generic parent unit.
15234 elsif S = Current_Scope and then Is_Generic_Instance (S)
15235 and then (In_Package_Body (S) or else In_Private_Part (S))
15236 then
15237 declare
15238 Par : constant Entity_Id :=
15239 Generic_Parent (Package_Specification (S));
15240 begin
15241 if Present (Par)
15242 and then P = Scope (Par)
15243 then
15244 Set_In_Private_Part (P);
15245 Install_Private_Declarations (P);
15246 end if;
15247 end;
15248 end if;
15249 end loop;
15251 -- Reset visibility of entities in the enclosing scope
15253 Set_Is_Hidden_Open_Scope (Current_Scope, False);
15255 Hidden := First_Elmt (Hidden_Entities);
15256 while Present (Hidden) loop
15257 Set_Is_Immediately_Visible (Node (Hidden), True);
15258 Next_Elmt (Hidden);
15259 end loop;
15261 else
15262 -- Each body is analyzed separately, and there is no context that
15263 -- needs preserving from one body instance to the next, so remove all
15264 -- parent scopes that have been installed.
15266 while Present (S) loop
15267 End_Package_Scope (S);
15268 Set_Is_Immediately_Visible (S, False);
15269 S := Current_Scope;
15270 exit when S = Standard_Standard;
15271 end loop;
15272 end if;
15273 end Remove_Parent;
15275 -----------------------------------
15276 -- Requires_Conformance_Checking --
15277 -----------------------------------
15279 function Requires_Conformance_Checking (N : Node_Id) return Boolean is
15280 begin
15281 -- No conformance checking required if the generic actual part is empty,
15282 -- or is a box or an others_clause (necessarily with a box).
15284 return Present (Generic_Associations (N))
15285 and then not Box_Present (N)
15286 and then Nkind (First (Generic_Associations (N))) /= N_Others_Choice;
15287 end Requires_Conformance_Checking;
15289 -----------------
15290 -- Restore_Env --
15291 -----------------
15293 procedure Restore_Env is
15294 Saved : Instance_Env renames Instance_Envs.Table (Instance_Envs.Last);
15296 begin
15297 if No (Current_Instantiated_Parent.Act_Id) then
15298 -- Restore environment after subprogram inlining
15300 Restore_Private_Views (Empty);
15301 end if;
15303 Current_Instantiated_Parent := Saved.Instantiated_Parent;
15304 Exchanged_Views := Saved.Exchanged_Views;
15305 Hidden_Entities := Saved.Hidden_Entities;
15306 Current_Sem_Unit := Saved.Current_Sem_Unit;
15307 Parent_Unit_Visible := Saved.Parent_Unit_Visible;
15308 Instance_Parent_Unit := Saved.Instance_Parent_Unit;
15310 Restore_Config_Switches (Saved.Switches);
15312 Instance_Envs.Decrement_Last;
15313 end Restore_Env;
15315 ---------------------------
15316 -- Restore_Private_Views --
15317 ---------------------------
15319 procedure Restore_Private_Views
15320 (Pack_Id : Entity_Id;
15321 Is_Package : Boolean := True)
15323 M : Elmt_Id;
15324 E : Entity_Id;
15325 Typ : Entity_Id;
15326 Dep_Elmt : Elmt_Id;
15327 Dep_Typ : Node_Id;
15329 procedure Restore_Nested_Formal (Formal : Entity_Id);
15330 -- Hide the generic formals of formal packages declared with box which
15331 -- were reachable in the current instantiation.
15333 ---------------------------
15334 -- Restore_Nested_Formal --
15335 ---------------------------
15337 procedure Restore_Nested_Formal (Formal : Entity_Id) is
15338 pragma Assert (Ekind (Formal) = E_Package);
15339 Ent : Entity_Id;
15340 begin
15341 if Present (Renamed_Entity (Formal))
15342 and then Denotes_Formal_Package (Renamed_Entity (Formal), True)
15343 then
15344 return;
15346 elsif Present (Associated_Formal_Package (Formal)) then
15347 Ent := First_Entity (Formal);
15348 while Present (Ent) loop
15349 exit when Ekind (Ent) = E_Package
15350 and then Renamed_Entity (Ent) = Renamed_Entity (Formal);
15352 Set_Is_Hidden (Ent);
15353 Set_Is_Potentially_Use_Visible (Ent, False);
15355 -- If package, then recurse
15357 if Ekind (Ent) = E_Package then
15358 Restore_Nested_Formal (Ent);
15359 end if;
15361 Next_Entity (Ent);
15362 end loop;
15363 end if;
15364 end Restore_Nested_Formal;
15366 -- Start of processing for Restore_Private_Views
15368 begin
15369 M := First_Elmt (Exchanged_Views);
15370 while Present (M) loop
15371 Typ := Node (M);
15373 -- Subtypes of types whose views have been exchanged, and that are
15374 -- defined within the instance, were not on the Private_Dependents
15375 -- list on entry to the instance, so they have to be exchanged
15376 -- explicitly now, in order to remain consistent with the view of the
15377 -- parent type.
15379 if Ekind (Typ) in E_Private_Type
15380 | E_Limited_Private_Type
15381 | E_Record_Type_With_Private
15382 then
15383 Dep_Elmt := First_Elmt (Private_Dependents (Typ));
15384 while Present (Dep_Elmt) loop
15385 Dep_Typ := Node (Dep_Elmt);
15387 if Scope (Dep_Typ) = Pack_Id
15388 and then Present (Full_View (Dep_Typ))
15389 then
15390 Replace_Elmt (Dep_Elmt, Full_View (Dep_Typ));
15391 Exchange_Declarations (Dep_Typ);
15392 end if;
15394 Next_Elmt (Dep_Elmt);
15395 end loop;
15396 end if;
15398 Exchange_Declarations (Node (M));
15399 Next_Elmt (M);
15400 end loop;
15402 if No (Pack_Id) then
15403 return;
15404 end if;
15406 -- Make the generic formal parameters private, and make the formal types
15407 -- into subtypes of the actuals again.
15409 E := First_Entity (Pack_Id);
15410 while Present (E) loop
15411 Set_Is_Hidden (E, True);
15413 if Is_Type (E)
15414 and then Nkind (Parent (E)) = N_Subtype_Declaration
15415 then
15416 -- Always preserve the flag Is_Generic_Actual_Type for GNATprove,
15417 -- as it is needed to identify the subtype with the type it
15418 -- renames, when there are conversions between access types
15419 -- to these.
15421 if GNATprove_Mode then
15422 null;
15424 -- If the actual for E is itself a generic actual type from
15425 -- an enclosing instance, E is still a generic actual type
15426 -- outside of the current instance. This matter when resolving
15427 -- an overloaded call that may be ambiguous in the enclosing
15428 -- instance, when two of its actuals coincide.
15430 elsif Is_Entity_Name (Subtype_Indication (Parent (E)))
15431 and then Is_Generic_Actual_Type
15432 (Entity (Subtype_Indication (Parent (E))))
15433 then
15434 null;
15435 else
15436 Set_Is_Generic_Actual_Type (E, False);
15438 -- It might seem reasonable to clear the Is_Generic_Actual_Type
15439 -- flag also on the Full_View if the type is private, since it
15440 -- was set also on this Full_View. However, this flag is relied
15441 -- upon by Covers to spot "types exported from instantiations"
15442 -- which are implicit Full_Views built for instantiations made
15443 -- on private types and we get type mismatches if we do it when
15444 -- the block exchanging the declarations below triggers ???
15446 -- if Is_Private_Type (E) and then Present (Full_View (E)) then
15447 -- Set_Is_Generic_Actual_Type (Full_View (E), False);
15448 -- end if;
15449 end if;
15451 -- An unusual case of aliasing: the actual may also be directly
15452 -- visible in the generic, and be private there, while it is fully
15453 -- visible in the context of the instance. The internal subtype
15454 -- is private in the instance but has full visibility like its
15455 -- parent in the enclosing scope. This enforces the invariant that
15456 -- the privacy status of all private dependents of a type coincide
15457 -- with that of the parent type. This can only happen when a
15458 -- generic child unit is instantiated within a sibling.
15460 if Is_Private_Type (E)
15461 and then not Is_Private_Type (Etype (E))
15462 then
15463 Exchange_Declarations (E);
15464 end if;
15466 elsif Ekind (E) = E_Package then
15468 -- The end of the renaming list is the renaming of the generic
15469 -- package itself. If the instance is a subprogram, all entities
15470 -- in the corresponding package are renamings. If this entity is
15471 -- a formal package, make its own formals private as well. The
15472 -- actual in this case is itself the renaming of an instantiation.
15473 -- If the entity is not a package renaming, it is the entity
15474 -- created to validate formal package actuals: ignore it.
15476 -- If the actual is itself a formal package for the enclosing
15477 -- generic, or the actual for such a formal package, it remains
15478 -- visible on exit from the instance, and therefore nothing needs
15479 -- to be done either, except to keep it accessible.
15481 if Is_Package and then Renamed_Entity (E) = Pack_Id then
15482 exit;
15484 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
15485 null;
15487 elsif
15488 Denotes_Formal_Package (Renamed_Entity (E), True, Pack_Id)
15489 then
15490 Set_Is_Hidden (E, False);
15492 else
15493 declare
15494 Act_P : constant Entity_Id := Renamed_Entity (E);
15495 Id : Entity_Id;
15497 begin
15498 Id := First_Entity (Act_P);
15499 while Present (Id)
15500 and then Id /= First_Private_Entity (Act_P)
15501 loop
15502 exit when Ekind (Id) = E_Package
15503 and then Renamed_Entity (Id) = Act_P;
15505 Set_Is_Hidden (Id, True);
15506 Set_Is_Potentially_Use_Visible (Id, In_Use (Act_P));
15508 if Ekind (Id) = E_Package then
15509 Restore_Nested_Formal (Id);
15510 end if;
15512 Next_Entity (Id);
15513 end loop;
15514 end;
15515 end if;
15516 end if;
15518 Next_Entity (E);
15519 end loop;
15520 end Restore_Private_Views;
15522 --------------
15523 -- Save_Env --
15524 --------------
15526 procedure Save_Env
15527 (Gen_Unit : Entity_Id;
15528 Act_Unit : Entity_Id)
15530 begin
15531 Init_Env;
15532 Set_Instance_Env (Gen_Unit, Act_Unit);
15533 end Save_Env;
15535 ----------------------------
15536 -- Save_Global_References --
15537 ----------------------------
15539 procedure Save_Global_References (Templ : Node_Id) is
15541 -- ??? it is horrible to use global variables in highly recursive code
15543 E : Entity_Id;
15544 -- The entity of the current associated node
15546 Gen_Scope : Entity_Id;
15547 -- The scope of the generic for which references are being saved
15549 N2 : Node_Id;
15550 -- The current associated node
15552 function Is_Global (E : Entity_Id) return Boolean;
15553 -- Check whether entity is defined outside of generic unit. Examine the
15554 -- scope of an entity, and the scope of the scope, etc, until we find
15555 -- either Standard, in which case the entity is global, or the generic
15556 -- unit itself, which indicates that the entity is local. If the entity
15557 -- is the generic unit itself, as in the case of a recursive call, or
15558 -- the enclosing generic unit, if different from the current scope, then
15559 -- it is local as well, because it will be replaced at the point of
15560 -- instantiation. On the other hand, if it is a reference to a child
15561 -- unit of a common ancestor, which appears in an instantiation, it is
15562 -- global because it is used to denote a specific compilation unit at
15563 -- the time the instantiations will be analyzed.
15565 procedure Qualify_Universal_Operands
15566 (Op : Node_Id;
15567 Func_Call : Node_Id);
15568 -- Op denotes a binary or unary operator in generic template Templ. Node
15569 -- Func_Call is the function call alternative of the operator within the
15570 -- the analyzed copy of the template. Change each operand which yields a
15571 -- universal type by wrapping it into a qualified expression
15573 -- Actual_Typ'(Operand)
15575 -- where Actual_Typ is the type of corresponding actual parameter of
15576 -- Operand in Func_Call.
15578 procedure Reset_Entity (N : Node_Id);
15579 -- Save semantic information on global entity so that it is not resolved
15580 -- again at instantiation time.
15582 procedure Save_Entity_Descendants (N : Node_Id);
15583 -- Apply Save_Global_References to the two syntactic descendants of
15584 -- non-terminal nodes that carry an Associated_Node and are processed
15585 -- through Reset_Entity. Once the global entity (if any) has been
15586 -- captured together with its type, only two syntactic descendants need
15587 -- to be traversed to complete the processing of the tree rooted at N.
15588 -- This applies to Selected_Components, Expanded_Names, and to Operator
15589 -- nodes. N can also be a character literal, identifier, or operator
15590 -- symbol node, but the call has no effect in these cases.
15592 procedure Save_Global_Defaults (N1 : Node_Id; N2 : Node_Id);
15593 -- Default actuals in nested instances must be handled specially
15594 -- because there is no link to them from the original tree. When an
15595 -- actual subprogram is given by a default, we add an explicit generic
15596 -- association for it in the instantiation node. When we save the
15597 -- global references on the name of the instance, we recover the list
15598 -- of generic associations, and add an explicit one to the original
15599 -- generic tree, through which a global actual can be preserved.
15600 -- Similarly, if a child unit is instantiated within a sibling, in the
15601 -- context of the parent, we must preserve the identifier of the parent
15602 -- so that it can be properly resolved in a subsequent instantiation.
15604 procedure Save_Global_Descendant (D : Union_Id);
15605 -- Apply Save_References recursively to the descendants of node D
15607 procedure Save_References (N : Node_Id);
15608 -- This is the recursive procedure that does the work, once the
15609 -- enclosing generic scope has been established.
15611 ---------------
15612 -- Is_Global --
15613 ---------------
15615 function Is_Global (E : Entity_Id) return Boolean is
15616 Se : Entity_Id;
15618 function Is_Instance_Node (Decl : Node_Id) return Boolean;
15619 -- Determine whether the parent node of a reference to a child unit
15620 -- denotes an instantiation or a formal package, in which case the
15621 -- reference to the child unit is global, even if it appears within
15622 -- the current scope (e.g. when the instance appears within the body
15623 -- of an ancestor).
15625 ----------------------
15626 -- Is_Instance_Node --
15627 ----------------------
15629 function Is_Instance_Node (Decl : Node_Id) return Boolean is
15630 begin
15631 return Nkind (Decl) in N_Generic_Instantiation
15632 or else
15633 Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration;
15634 end Is_Instance_Node;
15636 -- Start of processing for Is_Global
15638 begin
15639 if E = Gen_Scope then
15640 return False;
15642 elsif E = Standard_Standard then
15643 return True;
15645 -- E should be an entity, but it is not always
15647 elsif Nkind (E) not in N_Entity then
15648 return False;
15650 elsif Nkind (E) /= N_Expanded_Name
15651 and then Is_Child_Unit (E)
15652 and then (Is_Instance_Node (Parent (N2))
15653 or else (Nkind (Parent (N2)) = N_Expanded_Name
15654 and then N2 = Selector_Name (Parent (N2))
15655 and then
15656 Is_Instance_Node (Parent (Parent (N2)))))
15657 then
15658 return True;
15660 else
15661 -- E may be an expanded name - typically an operator - in which
15662 -- case we must find its enclosing scope since expanded names
15663 -- don't have corresponding scopes.
15665 if Nkind (E) = N_Expanded_Name then
15666 Se := Find_Enclosing_Scope (E);
15668 -- Otherwise, E is an entity and will have Scope set
15670 else
15671 Se := Scope (E);
15672 end if;
15674 while Se /= Gen_Scope loop
15675 if Se = Standard_Standard then
15676 return True;
15677 else
15678 Se := Scope (Se);
15679 end if;
15680 end loop;
15682 return False;
15683 end if;
15684 end Is_Global;
15686 --------------------------------
15687 -- Qualify_Universal_Operands --
15688 --------------------------------
15690 procedure Qualify_Universal_Operands
15691 (Op : Node_Id;
15692 Func_Call : Node_Id)
15694 procedure Qualify_Operand (Opnd : Node_Id; Actual : Node_Id);
15695 -- Rewrite operand Opnd as a qualified expression of the form
15697 -- Actual_Typ'(Opnd)
15699 -- where Actual is the corresponding actual parameter of Opnd in
15700 -- function call Func_Call.
15702 function Qualify_Type
15703 (Loc : Source_Ptr;
15704 Typ : Entity_Id) return Node_Id;
15705 -- Qualify type Typ by creating a selected component of the form
15707 -- Scope_Of_Typ.Typ
15709 ---------------------
15710 -- Qualify_Operand --
15711 ---------------------
15713 procedure Qualify_Operand (Opnd : Node_Id; Actual : Node_Id) is
15714 Loc : constant Source_Ptr := Sloc (Opnd);
15715 Typ : constant Entity_Id := Etype (Actual);
15716 Mark : Node_Id;
15717 Qual : Node_Id;
15719 begin
15720 -- Qualify the operand when it is of a universal type. Note that
15721 -- the template is unanalyzed and it is not possible to directly
15722 -- query the type. This transformation is not done when the type
15723 -- of the actual is internally generated because the type will be
15724 -- regenerated in the instance.
15726 if Yields_Universal_Type (Opnd)
15727 and then Comes_From_Source (Typ)
15728 and then not Is_Hidden (Typ)
15729 then
15730 -- The type of the actual may be a global reference. Save this
15731 -- information by creating a reference to it.
15733 if Is_Global (Typ) then
15734 Mark := New_Occurrence_Of (Typ, Loc);
15736 -- Otherwise rely on resolution to find the proper type within
15737 -- the instance.
15739 else
15740 Mark := Qualify_Type (Loc, Typ);
15741 end if;
15743 Qual :=
15744 Make_Qualified_Expression (Loc,
15745 Subtype_Mark => Mark,
15746 Expression => Relocate_Node (Opnd));
15748 -- Mark the qualification to distinguish it from other source
15749 -- constructs and signal the instantiation mechanism that this
15750 -- node requires special processing. See Copy_Generic_Node for
15751 -- details.
15753 Set_Is_Qualified_Universal_Literal (Qual);
15755 Rewrite (Opnd, Qual);
15756 end if;
15757 end Qualify_Operand;
15759 ------------------
15760 -- Qualify_Type --
15761 ------------------
15763 function Qualify_Type
15764 (Loc : Source_Ptr;
15765 Typ : Entity_Id) return Node_Id
15767 Scop : constant Entity_Id := Scope (Typ);
15768 Result : Node_Id;
15770 begin
15771 Result := Make_Identifier (Loc, Chars (Typ));
15773 if Present (Scop) and then not Is_Generic_Unit (Scop) then
15774 Result :=
15775 Make_Selected_Component (Loc,
15776 Prefix => Make_Identifier (Loc, Chars (Scop)),
15777 Selector_Name => Result);
15778 end if;
15780 return Result;
15781 end Qualify_Type;
15783 -- Local variables
15785 Actuals : constant List_Id := Parameter_Associations (Func_Call);
15787 -- Start of processing for Qualify_Universal_Operands
15789 begin
15790 if Nkind (Op) in N_Binary_Op then
15791 Qualify_Operand (Left_Opnd (Op), First (Actuals));
15792 Qualify_Operand (Right_Opnd (Op), Next (First (Actuals)));
15794 elsif Nkind (Op) in N_Unary_Op then
15795 Qualify_Operand (Right_Opnd (Op), First (Actuals));
15796 end if;
15797 end Qualify_Universal_Operands;
15799 ------------------
15800 -- Reset_Entity --
15801 ------------------
15803 procedure Reset_Entity (N : Node_Id) is
15804 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id);
15805 -- If the type of N2 is global to the generic unit, save the type in
15806 -- the generic node. Just as we perform name capture for explicit
15807 -- references within the generic, we must capture the global types
15808 -- of local entities because they may participate in resolution in
15809 -- the instance.
15811 function Top_Ancestor (E : Entity_Id) return Entity_Id;
15812 -- Find the ultimate ancestor of the current unit. If it is not a
15813 -- generic unit, then the name of the current unit in the prefix of
15814 -- an expanded name must be replaced with its generic homonym to
15815 -- ensure that it will be properly resolved in an instance.
15817 ---------------------
15818 -- Set_Global_Type --
15819 ---------------------
15821 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id) is
15822 Typ : constant Entity_Id := Etype (N2);
15824 begin
15825 Set_Etype (N, Typ);
15827 -- If the entity of N is not the associated node, this is a
15828 -- nested generic and it has an associated node as well, whose
15829 -- type is already the full view (see below). Indicate that the
15830 -- original node has a private view.
15832 if Entity (N) /= N2 and then Has_Private_View (Entity (N)) then
15833 Set_Has_Private_View (N);
15834 end if;
15836 -- If not a private type, nothing else to do
15838 if not Is_Private_Type (Typ) then
15839 null;
15841 -- If it is a derivation of a private type in a context where no
15842 -- full view is needed, nothing to do either.
15844 elsif No (Full_View (Typ)) and then Typ /= Etype (Typ) then
15845 null;
15847 -- Otherwise mark the type for flipping and use the full view when
15848 -- available.
15850 else
15851 Set_Has_Private_View (N);
15853 if Present (Full_View (Typ)) then
15854 Set_Etype (N2, Full_View (Typ));
15855 end if;
15856 end if;
15858 if Is_Floating_Point_Type (Typ)
15859 and then Has_Dimension_System (Typ)
15860 then
15861 Copy_Dimensions (N2, N);
15862 end if;
15863 end Set_Global_Type;
15865 ------------------
15866 -- Top_Ancestor --
15867 ------------------
15869 function Top_Ancestor (E : Entity_Id) return Entity_Id is
15870 Par : Entity_Id;
15872 begin
15873 Par := E;
15874 while Is_Child_Unit (Par) loop
15875 Par := Scope (Par);
15876 end loop;
15878 return Par;
15879 end Top_Ancestor;
15881 -- Start of processing for Reset_Entity
15883 begin
15884 N2 := Get_Associated_Node (N);
15885 E := Entity (N2);
15887 if Present (E) then
15889 -- If the node is an entry call to an entry in an enclosing task,
15890 -- it is rewritten as a selected component. No global entity to
15891 -- preserve in this case, since the expansion will be redone in
15892 -- the instance.
15894 if Nkind (E) not in N_Entity then
15895 Set_Associated_Node (N, Empty);
15896 Set_Etype (N, Empty);
15897 return;
15898 end if;
15900 -- If the entity is an itype created as a subtype of an access
15901 -- type with a null exclusion restore source entity for proper
15902 -- visibility. The itype will be created anew in the instance.
15904 if Is_Itype (E)
15905 and then Ekind (E) = E_Access_Subtype
15906 and then Is_Entity_Name (N)
15907 and then Chars (Etype (E)) = Chars (N)
15908 then
15909 E := Etype (E);
15910 Set_Entity (N2, E);
15911 Set_Etype (N2, E);
15912 end if;
15914 if Is_Global (E) then
15915 Set_Global_Type (N, N2);
15917 elsif Nkind (N) = N_Op_Concat
15918 and then Is_Generic_Type (Etype (N2))
15919 and then (Base_Type (Etype (Right_Opnd (N2))) = Etype (N2)
15920 or else
15921 Base_Type (Etype (Left_Opnd (N2))) = Etype (N2))
15922 and then Is_Intrinsic_Subprogram (E)
15923 then
15924 null;
15926 -- Entity is local. Mark generic node as unresolved. Note that now
15927 -- it does not have an entity.
15929 else
15930 Set_Associated_Node (N, Empty);
15931 Set_Etype (N, Empty);
15932 end if;
15934 if Nkind (Parent (N)) in N_Generic_Instantiation
15935 and then N = Name (Parent (N))
15936 then
15937 Save_Global_Defaults (Parent (N), Parent (N2));
15938 end if;
15940 elsif Nkind (Parent (N)) = N_Selected_Component
15941 and then Nkind (Parent (N2)) = N_Expanded_Name
15942 then
15943 -- In case of previous errors, the tree might be malformed
15945 if No (Entity (Parent (N2))) then
15946 null;
15948 elsif Is_Global (Entity (Parent (N2))) then
15949 Change_Selected_Component_To_Expanded_Name (Parent (N));
15950 Set_Associated_Node (Parent (N), Parent (N2));
15951 Set_Global_Type (Parent (N), Parent (N2));
15952 Save_Entity_Descendants (N);
15954 -- If this is a reference to the current generic entity, replace
15955 -- by the name of the generic homonym of the current package. This
15956 -- is because in an instantiation Par.P.Q will not resolve to the
15957 -- name of the instance, whose enclosing scope is not necessarily
15958 -- Par. We use the generic homonym rather that the name of the
15959 -- generic itself because it may be hidden by a local declaration.
15961 elsif In_Open_Scopes (Entity (Parent (N2)))
15962 and then not
15963 Is_Generic_Unit (Top_Ancestor (Entity (Prefix (Parent (N2)))))
15964 then
15965 if Ekind (Entity (Parent (N2))) = E_Generic_Package then
15966 Rewrite (Parent (N),
15967 Make_Identifier (Sloc (N),
15968 Chars =>
15969 Chars (Generic_Homonym (Entity (Parent (N2))))));
15970 else
15971 Rewrite (Parent (N),
15972 Make_Identifier (Sloc (N),
15973 Chars => Chars (Selector_Name (Parent (N2)))));
15974 end if;
15975 end if;
15977 if Nkind (Parent (Parent (N))) in N_Generic_Instantiation
15978 and then Parent (N) = Name (Parent (Parent (N)))
15979 then
15980 Save_Global_Defaults
15981 (Parent (Parent (N)), Parent (Parent (N2)));
15982 end if;
15984 -- A selected component may denote a static constant that has been
15985 -- folded. If the static constant is global to the generic, capture
15986 -- its value. Otherwise the folding will happen in any instantiation.
15988 elsif Nkind (Parent (N)) = N_Selected_Component
15989 and then Nkind (Parent (N2)) in N_Integer_Literal | N_Real_Literal
15990 then
15991 if Present (Entity (Original_Node (Parent (N2))))
15992 and then Is_Global (Entity (Original_Node (Parent (N2))))
15993 then
15994 Rewrite (Parent (N), New_Copy (Parent (N2)));
15995 Set_Analyzed (Parent (N), False);
15996 end if;
15998 -- A selected component may be transformed into a parameterless
15999 -- function call. If the called entity is global, rewrite the node
16000 -- appropriately, i.e. as an extended name for the global entity.
16002 elsif Nkind (Parent (N)) = N_Selected_Component
16003 and then Nkind (Parent (N2)) = N_Function_Call
16004 and then N = Selector_Name (Parent (N))
16005 then
16006 if No (Parameter_Associations (Parent (N2))) then
16007 if Is_Global (Entity (Name (Parent (N2)))) then
16008 Change_Selected_Component_To_Expanded_Name (Parent (N));
16009 Set_Associated_Node (Parent (N), Name (Parent (N2)));
16010 Set_Global_Type (Parent (N), Name (Parent (N2)));
16011 Save_Entity_Descendants (N);
16013 else
16014 Set_Is_Prefixed_Call (Parent (N));
16015 Set_Associated_Node (N, Empty);
16016 Set_Etype (N, Empty);
16017 end if;
16019 -- In Ada 2005, X.F may be a call to a primitive operation,
16020 -- rewritten as F (X). This rewriting will be done again in an
16021 -- instance, so keep the original node. Global entities will be
16022 -- captured as for other constructs. Indicate that this must
16023 -- resolve as a call, to prevent accidental overloading in the
16024 -- instance, if both a component and a primitive operation appear
16025 -- as candidates.
16027 else
16028 Set_Is_Prefixed_Call (Parent (N));
16029 end if;
16031 -- Entity is local. Reset in generic unit, so that node is resolved
16032 -- anew at the point of instantiation.
16034 else
16035 Set_Associated_Node (N, Empty);
16036 Set_Etype (N, Empty);
16037 end if;
16038 end Reset_Entity;
16040 -----------------------------
16041 -- Save_Entity_Descendants --
16042 -----------------------------
16044 procedure Save_Entity_Descendants (N : Node_Id) is
16045 begin
16046 case Nkind (N) is
16047 when N_Binary_Op =>
16048 Save_Global_Descendant (Union_Id (Left_Opnd (N)));
16049 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
16051 when N_Unary_Op =>
16052 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
16054 when N_Expanded_Name
16055 | N_Selected_Component
16057 Save_Global_Descendant (Union_Id (Prefix (N)));
16058 Save_Global_Descendant (Union_Id (Selector_Name (N)));
16060 when N_Character_Literal
16061 | N_Identifier
16062 | N_Operator_Symbol
16064 null;
16066 when others =>
16067 raise Program_Error;
16068 end case;
16069 end Save_Entity_Descendants;
16071 --------------------------
16072 -- Save_Global_Defaults --
16073 --------------------------
16075 procedure Save_Global_Defaults (N1 : Node_Id; N2 : Node_Id) is
16076 Loc : constant Source_Ptr := Sloc (N1);
16077 Assoc2 : constant List_Id := Generic_Associations (N2);
16078 Gen_Id : constant Entity_Id := Get_Generic_Entity (N2);
16079 Assoc1 : List_Id;
16080 Act1 : Node_Id;
16081 Act2 : Node_Id;
16082 Def : Node_Id;
16083 Ndec : Node_Id;
16084 Subp : Entity_Id;
16085 Actual : Entity_Id;
16087 begin
16088 Assoc1 := Generic_Associations (N1);
16090 if Present (Assoc1) then
16091 Act1 := First (Assoc1);
16092 else
16093 Act1 := Empty;
16094 Set_Generic_Associations (N1, New_List);
16095 Assoc1 := Generic_Associations (N1);
16096 end if;
16098 if Present (Assoc2) then
16099 Act2 := First (Assoc2);
16100 else
16101 return;
16102 end if;
16104 while Present (Act1) and then Present (Act2) loop
16105 Next (Act1);
16106 Next (Act2);
16107 end loop;
16109 -- Find the associations added for default subprograms
16111 if Present (Act2) then
16112 while Nkind (Act2) /= N_Generic_Association
16113 or else No (Entity (Selector_Name (Act2)))
16114 or else not Is_Overloadable (Entity (Selector_Name (Act2)))
16115 loop
16116 Next (Act2);
16117 end loop;
16119 -- Add a similar association if the default is global. The
16120 -- renaming declaration for the actual has been analyzed, and
16121 -- its alias is the program it renames. Link the actual in the
16122 -- original generic tree with the node in the analyzed tree.
16124 while Present (Act2) loop
16125 Subp := Entity (Selector_Name (Act2));
16126 Def := Explicit_Generic_Actual_Parameter (Act2);
16128 -- Following test is defence against rubbish errors
16130 if No (Alias (Subp)) then
16131 return;
16132 end if;
16134 -- Retrieve the resolved actual from the renaming declaration
16135 -- created for the instantiated formal.
16137 Actual := Entity (Name (Parent (Parent (Subp))));
16138 Set_Entity (Def, Actual);
16139 Set_Etype (Def, Etype (Actual));
16141 if Is_Global (Actual) then
16142 Ndec :=
16143 Make_Generic_Association (Loc,
16144 Selector_Name =>
16145 New_Occurrence_Of (Subp, Loc),
16146 Explicit_Generic_Actual_Parameter =>
16147 New_Occurrence_Of (Actual, Loc));
16149 Set_Associated_Node
16150 (Explicit_Generic_Actual_Parameter (Ndec), Def);
16152 Append (Ndec, Assoc1);
16154 -- If there are other defaults, add a dummy association in case
16155 -- there are other defaulted formals with the same name.
16157 elsif Present (Next (Act2)) then
16158 Ndec :=
16159 Make_Generic_Association (Loc,
16160 Selector_Name =>
16161 New_Occurrence_Of (Subp, Loc),
16162 Explicit_Generic_Actual_Parameter => Empty);
16164 Append (Ndec, Assoc1);
16165 end if;
16167 Next (Act2);
16168 end loop;
16169 end if;
16171 if Nkind (Name (N1)) = N_Identifier
16172 and then Is_Child_Unit (Gen_Id)
16173 and then Is_Global (Gen_Id)
16174 and then Is_Generic_Unit (Scope (Gen_Id))
16175 and then In_Open_Scopes (Scope (Gen_Id))
16176 then
16177 -- This is an instantiation of a child unit within a sibling, so
16178 -- that the generic parent is in scope. An eventual instance must
16179 -- occur within the scope of an instance of the parent. Make name
16180 -- in instance into an expanded name, to preserve the identifier
16181 -- of the parent, so it can be resolved subsequently.
16183 Rewrite (Name (N2),
16184 Make_Expanded_Name (Loc,
16185 Chars => Chars (Gen_Id),
16186 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
16187 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
16188 Set_Entity (Name (N2), Gen_Id);
16190 Rewrite (Name (N1),
16191 Make_Expanded_Name (Loc,
16192 Chars => Chars (Gen_Id),
16193 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
16194 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
16196 Set_Associated_Node (Name (N1), Name (N2));
16197 Set_Associated_Node (Prefix (Name (N1)), Empty);
16198 Set_Associated_Node
16199 (Selector_Name (Name (N1)), Selector_Name (Name (N2)));
16200 Set_Etype (Name (N1), Etype (Gen_Id));
16201 end if;
16202 end Save_Global_Defaults;
16204 ----------------------------
16205 -- Save_Global_Descendant --
16206 ----------------------------
16208 procedure Save_Global_Descendant (D : Union_Id) is
16209 N1 : Node_Id;
16211 begin
16212 if D in Node_Range then
16213 if D = Union_Id (Empty) then
16214 null;
16216 elsif Nkind (Node_Id (D)) /= N_Compilation_Unit then
16217 Save_References (Node_Id (D));
16218 end if;
16220 elsif D in List_Range then
16221 pragma Assert (D /= Union_Id (No_List));
16222 -- Because No_List = Empty, which is in Node_Range above
16224 N1 := First (List_Id (D));
16225 while Present (N1) loop
16226 Save_References (N1);
16227 Next (N1);
16228 end loop;
16230 -- Element list or other non-node field, nothing to do
16232 else
16233 null;
16234 end if;
16235 end Save_Global_Descendant;
16237 ---------------------
16238 -- Save_References --
16239 ---------------------
16241 -- This is the recursive procedure that does the work once the enclosing
16242 -- generic scope has been established. We have to treat specially a
16243 -- number of node rewritings that are required by semantic processing
16244 -- and which change the kind of nodes in the generic copy: typically
16245 -- constant-folding, replacing an operator node by a string literal, or
16246 -- a selected component by an expanded name. In each of those cases, the
16247 -- transformation is propagated to the generic unit.
16249 procedure Save_References (N : Node_Id) is
16250 Loc : constant Source_Ptr := Sloc (N);
16252 function Requires_Delayed_Save (Nod : Node_Id) return Boolean;
16253 -- Determine whether arbitrary node Nod requires delayed capture of
16254 -- global references within its aspect specifications.
16256 procedure Save_References_In_Aggregate (N : Node_Id);
16257 -- Save all global references in [extension] aggregate node N
16259 procedure Save_References_In_Char_Lit_Or_Op_Symbol (N : Node_Id);
16260 -- Save all global references in a character literal or operator
16261 -- symbol denoted by N.
16263 procedure Save_References_In_Descendants (N : Node_Id);
16264 -- Save all global references in all descendants of node N
16266 procedure Save_References_In_Identifier (N : Node_Id);
16267 -- Save all global references in identifier node N
16269 procedure Save_References_In_Operator (N : Node_Id);
16270 -- Save all global references in operator node N
16272 procedure Save_References_In_Pragma (Prag : Node_Id);
16273 -- Save all global references found within the expression of pragma
16274 -- Prag.
16276 ---------------------------
16277 -- Requires_Delayed_Save --
16278 ---------------------------
16280 function Requires_Delayed_Save (Nod : Node_Id) return Boolean is
16281 begin
16282 -- Generic packages and subprograms require delayed capture of
16283 -- global references within their aspects due to the timing of
16284 -- annotation analysis.
16286 if Nkind (Nod) in N_Generic_Package_Declaration
16287 | N_Generic_Subprogram_Declaration
16288 | N_Package_Body
16289 | N_Package_Body_Stub
16290 | N_Subprogram_Body
16291 | N_Subprogram_Body_Stub
16292 then
16293 -- Since the capture of global references is done on the
16294 -- unanalyzed generic template, there is no information around
16295 -- to infer the context. Use the Associated_Entity linkages to
16296 -- peek into the analyzed generic copy and determine what the
16297 -- template corresponds to.
16299 if Nod = Templ then
16300 return
16301 Is_Generic_Declaration_Or_Body
16302 (Unit_Declaration_Node
16303 (Associated_Entity (Defining_Entity (Nod))));
16305 -- Otherwise the generic unit being processed is not the top
16306 -- level template. It is safe to capture of global references
16307 -- within the generic unit because at this point the top level
16308 -- copy is fully analyzed.
16310 else
16311 return False;
16312 end if;
16314 -- Otherwise capture the global references without interference
16316 else
16317 return False;
16318 end if;
16319 end Requires_Delayed_Save;
16321 ----------------------------------
16322 -- Save_References_In_Aggregate --
16323 ----------------------------------
16325 procedure Save_References_In_Aggregate (N : Node_Id) is
16326 Nam : Node_Id;
16327 Qual : Node_Id := Empty;
16328 Typ : Entity_Id := Empty;
16330 begin
16331 N2 := Get_Associated_Node (N);
16333 if Present (N2) then
16334 Typ := Etype (N2);
16336 -- In an instance within a generic, use the name of the actual
16337 -- and not the original generic parameter. If the actual is
16338 -- global in the current generic it must be preserved for its
16339 -- instantiation.
16341 if Parent_Kind (Typ) = N_Subtype_Declaration
16342 and then Present (Generic_Parent_Type (Parent (Typ)))
16343 then
16344 Typ := Base_Type (Typ);
16345 Set_Etype (N2, Typ);
16346 end if;
16347 end if;
16349 if No (N2) or else No (Typ) or else not Is_Global (Typ) then
16350 Set_Associated_Node (N, Empty);
16352 -- If the aggregate is an actual in a call, it has been
16353 -- resolved in the current context, to some local type. The
16354 -- enclosing call may have been disambiguated by the aggregate,
16355 -- and this disambiguation might fail at instantiation time
16356 -- because the type to which the aggregate did resolve is not
16357 -- preserved. In order to preserve some of this information,
16358 -- wrap the aggregate in a qualified expression, using the id
16359 -- of its type. For further disambiguation we qualify the type
16360 -- name with its scope (if visible and not hidden by a local
16361 -- homograph) because both id's will have corresponding
16362 -- entities in an instance. This resolves most of the problems
16363 -- with missing type information on aggregates in instances.
16365 if Present (N2)
16366 and then Nkind (N2) = Nkind (N)
16367 and then Nkind (Parent (N2)) in N_Subprogram_Call
16368 and then Present (Typ)
16369 and then Comes_From_Source (Typ)
16370 then
16371 Nam := Make_Identifier (Loc, Chars (Typ));
16373 if Is_Immediately_Visible (Scope (Typ))
16374 and then
16375 (not In_Open_Scopes (Scope (Typ))
16376 or else Current_Entity (Scope (Typ)) = Scope (Typ))
16377 then
16378 Nam :=
16379 Make_Selected_Component (Loc,
16380 Prefix =>
16381 Make_Identifier (Loc, Chars (Scope (Typ))),
16382 Selector_Name => Nam);
16383 end if;
16385 Qual :=
16386 Make_Qualified_Expression (Loc,
16387 Subtype_Mark => Nam,
16388 Expression => Relocate_Node (N));
16389 end if;
16390 end if;
16392 if Nkind (N) = N_Aggregate then
16393 Save_Global_Descendant (Union_Id (Aggregate_Bounds (N)));
16395 elsif Nkind (N) = N_Extension_Aggregate then
16396 Save_Global_Descendant (Union_Id (Ancestor_Part (N)));
16398 else
16399 pragma Assert (False);
16400 end if;
16402 Save_Global_Descendant (Union_Id (Expressions (N)));
16403 Save_Global_Descendant (Union_Id (Component_Associations (N)));
16404 Save_Global_Descendant (Union_Id (Etype (N)));
16406 if Present (Qual) then
16407 Rewrite (N, Qual);
16408 end if;
16409 end Save_References_In_Aggregate;
16411 ----------------------------------------------
16412 -- Save_References_In_Char_Lit_Or_Op_Symbol --
16413 ----------------------------------------------
16415 procedure Save_References_In_Char_Lit_Or_Op_Symbol (N : Node_Id) is
16416 begin
16417 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
16418 Reset_Entity (N);
16420 elsif Nkind (N) = N_Operator_Symbol
16421 and then Nkind (Get_Associated_Node (N)) = N_String_Literal
16422 then
16423 Change_Operator_Symbol_To_String_Literal (N);
16424 end if;
16425 end Save_References_In_Char_Lit_Or_Op_Symbol;
16427 ------------------------------------
16428 -- Save_References_In_Descendants --
16429 ------------------------------------
16431 procedure Save_References_In_Descendants (N : Node_Id) is
16432 procedure Walk is new Walk_Sinfo_Fields (Save_Global_Descendant);
16433 begin
16434 Walk (N);
16435 end Save_References_In_Descendants;
16437 -----------------------------------
16438 -- Save_References_In_Identifier --
16439 -----------------------------------
16441 procedure Save_References_In_Identifier (N : Node_Id) is
16442 begin
16443 -- The node did not undergo a transformation
16445 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
16446 -- If this is a discriminant reference, always save it.
16447 -- It is used in the instance to find the corresponding
16448 -- discriminant positionally rather than by name.
16450 Set_Original_Discriminant
16451 (N, Original_Discriminant (Get_Associated_Node (N)));
16453 Reset_Entity (N);
16455 -- The analysis of the generic copy transformed the identifier
16456 -- into another construct. Propagate the changes to the template.
16458 else
16459 N2 := Get_Associated_Node (N);
16461 -- The identifier denotes a call to a parameterless function.
16462 -- Mark the node as resolved when the function is external.
16464 if Nkind (N2) = N_Function_Call then
16465 E := Entity (Name (N2));
16467 if Present (E) and then Is_Global (E) then
16468 Set_Etype (N, Etype (N2));
16469 else
16470 Set_Associated_Node (N, Empty);
16471 Set_Etype (N, Empty);
16472 end if;
16474 -- The identifier denotes a named number that was constant
16475 -- folded. Preserve the original name for ASIS and undo the
16476 -- constant folding which will be repeated in the instance.
16477 -- Is this still needed???
16479 elsif Nkind (N2) in N_Integer_Literal | N_Real_Literal
16480 and then Is_Entity_Name (Original_Node (N2))
16481 then
16482 Set_Associated_Node (N, Original_Node (N2));
16483 Reset_Entity (N);
16485 -- The identifier resolved to a string literal. Propagate this
16486 -- information to the generic template.
16488 elsif Nkind (N2) = N_String_Literal then
16489 Rewrite (N, New_Copy (N2));
16491 -- The identifier is rewritten as a dereference if it is the
16492 -- prefix of an implicit dereference. Preserve the original
16493 -- tree as the analysis of the instance will expand the node
16494 -- again, but preserve the resolved entity if it is global.
16496 elsif Nkind (N2) = N_Explicit_Dereference then
16497 if Is_Entity_Name (Prefix (N2))
16498 and then Present (Entity (Prefix (N2)))
16499 and then Is_Global (Entity (Prefix (N2)))
16500 then
16501 Set_Associated_Node (N, Prefix (N2));
16503 elsif Nkind (Prefix (N2)) = N_Function_Call
16504 and then Present (Entity (Name (Prefix (N2))))
16505 and then Is_Global (Entity (Name (Prefix (N2))))
16506 then
16507 Rewrite (N,
16508 Make_Explicit_Dereference (Loc,
16509 Prefix =>
16510 Make_Function_Call (Loc,
16511 Name =>
16512 New_Occurrence_Of
16513 (Entity (Name (Prefix (N2))), Loc))));
16515 else
16516 Set_Associated_Node (N, Empty);
16517 Set_Etype (N, Empty);
16518 end if;
16520 -- The subtype mark of a nominally unconstrained object is
16521 -- rewritten as a subtype indication using the bounds of the
16522 -- expression. Recover the original subtype mark.
16524 elsif Nkind (N2) = N_Subtype_Indication
16525 and then Is_Entity_Name (Original_Node (N2))
16526 then
16527 Set_Associated_Node (N, Original_Node (N2));
16528 Reset_Entity (N);
16529 end if;
16530 end if;
16531 end Save_References_In_Identifier;
16533 ---------------------------------
16534 -- Save_References_In_Operator --
16535 ---------------------------------
16537 procedure Save_References_In_Operator (N : Node_Id) is
16538 begin
16539 -- The node did not undergo a transformation
16541 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
16542 if Nkind (N) = N_Op_Concat then
16543 Set_Is_Component_Left_Opnd (N,
16544 Is_Component_Left_Opnd (Get_Associated_Node (N)));
16546 Set_Is_Component_Right_Opnd (N,
16547 Is_Component_Right_Opnd (Get_Associated_Node (N)));
16548 end if;
16550 Reset_Entity (N);
16552 -- The analysis of the generic copy transformed the operator into
16553 -- some other construct. Propagate the changes to the template if
16554 -- applicable.
16556 else
16557 N2 := Get_Associated_Node (N);
16559 -- The operator resoved to a function call
16561 if Nkind (N2) = N_Function_Call then
16563 -- Add explicit qualifications in the generic template for
16564 -- all operands of universal type. This aids resolution by
16565 -- preserving the actual type of a literal or an attribute
16566 -- that yields a universal result.
16568 Qualify_Universal_Operands (N, N2);
16570 E := Entity (Name (N2));
16572 if Present (E) and then Is_Global (E) then
16573 Set_Etype (N, Etype (N2));
16574 else
16575 Set_Associated_Node (N, Empty);
16576 Set_Etype (N, Empty);
16577 end if;
16579 -- The operator was folded into a literal
16581 elsif Nkind (N2) in N_Integer_Literal
16582 | N_Real_Literal
16583 | N_String_Literal
16584 then
16585 if Present (Original_Node (N2))
16586 and then Nkind (Original_Node (N2)) = Nkind (N)
16587 then
16588 -- Operation was constant-folded. Whenever possible,
16589 -- recover semantic information from unfolded node.
16590 -- This was initially done for ASIS but is apparently
16591 -- needed also for e.g. compiling a-nbnbin.adb.
16593 Set_Associated_Node (N, Original_Node (N2));
16595 if Nkind (N) = N_Op_Concat then
16596 Set_Is_Component_Left_Opnd (N,
16597 Is_Component_Left_Opnd (Get_Associated_Node (N)));
16598 Set_Is_Component_Right_Opnd (N,
16599 Is_Component_Right_Opnd (Get_Associated_Node (N)));
16600 end if;
16602 Reset_Entity (N);
16604 -- Propagate the constant folding back to the template
16606 else
16607 Rewrite (N, New_Copy (N2));
16608 Set_Analyzed (N, False);
16609 end if;
16611 -- The operator was folded into an enumeration literal. Retain
16612 -- the entity to avoid spurious ambiguities if it is overloaded
16613 -- at the point of instantiation or inlining.
16615 elsif Nkind (N2) = N_Identifier
16616 and then Ekind (Entity (N2)) = E_Enumeration_Literal
16617 then
16618 Rewrite (N, New_Copy (N2));
16619 Set_Analyzed (N, False);
16620 end if;
16621 end if;
16623 -- Complete the operands check if node has not been constant
16624 -- folded.
16626 if Nkind (N) in N_Op then
16627 Save_Entity_Descendants (N);
16628 end if;
16629 end Save_References_In_Operator;
16631 -------------------------------
16632 -- Save_References_In_Pragma --
16633 -------------------------------
16635 procedure Save_References_In_Pragma (Prag : Node_Id) is
16636 Context : Node_Id;
16637 Do_Save : Boolean := True;
16639 begin
16640 -- Do not save global references in pragmas generated from aspects
16641 -- because the pragmas will be regenerated at instantiation time.
16643 if From_Aspect_Specification (Prag) then
16644 Do_Save := False;
16646 -- The capture of global references within contract-related source
16647 -- pragmas associated with generic packages, subprograms or their
16648 -- respective bodies must be delayed due to timing of annotation
16649 -- analysis. Global references are still captured in routine
16650 -- Save_Global_References_In_Contract.
16652 elsif Is_Generic_Contract_Pragma (Prag) and then Prag /= Templ then
16653 if Is_Package_Contract_Annotation (Prag) then
16654 Context := Find_Related_Package_Or_Body (Prag);
16655 else
16656 pragma Assert (Is_Subprogram_Contract_Annotation (Prag));
16657 Context := Find_Related_Declaration_Or_Body (Prag);
16658 end if;
16660 -- The use of Original_Node accounts for the case when the
16661 -- related context is generic template.
16663 if Requires_Delayed_Save (Original_Node (Context)) then
16664 Do_Save := False;
16665 end if;
16666 end if;
16668 -- For all other cases, save all global references within the
16669 -- descendants, but skip the following semantic fields:
16670 -- Next_Pragma, Corresponding_Aspect, Next_Rep_Item.
16672 if Do_Save then
16673 Save_Global_Descendant
16674 (Union_Id (Pragma_Argument_Associations (N)));
16675 Save_Global_Descendant (Union_Id (Pragma_Identifier (N)));
16676 end if;
16677 end Save_References_In_Pragma;
16679 -- Start of processing for Save_References
16681 begin
16682 if N = Empty then
16683 null;
16685 -- Aggregates
16687 elsif Nkind (N) in N_Aggregate | N_Extension_Aggregate then
16688 Save_References_In_Aggregate (N);
16690 -- Character literals, operator symbols
16692 elsif Nkind (N) in N_Character_Literal | N_Operator_Symbol then
16693 Save_References_In_Char_Lit_Or_Op_Symbol (N);
16695 -- Defining identifiers
16697 elsif Nkind (N) in N_Entity then
16698 null;
16700 -- Identifiers
16702 elsif Nkind (N) = N_Identifier then
16703 Save_References_In_Identifier (N);
16705 -- Operators
16707 elsif Nkind (N) in N_Op then
16708 Save_References_In_Operator (N);
16710 -- Pragmas
16712 elsif Nkind (N) = N_Pragma then
16713 Save_References_In_Pragma (N);
16715 else
16716 Save_References_In_Descendants (N);
16717 end if;
16719 -- Save all global references found within the aspect specifications
16720 -- of the related node.
16722 if Permits_Aspect_Specifications (N) and then Has_Aspects (N) then
16724 -- The capture of global references within aspects associated with
16725 -- generic packages, subprograms or their bodies must be delayed
16726 -- due to timing of annotation analysis. Global references are
16727 -- still captured in routine Save_Global_References_In_Contract.
16729 if Requires_Delayed_Save (N) then
16730 null;
16732 -- Otherwise save all global references within the aspects
16734 else
16735 Save_Global_References_In_Aspects (N);
16736 end if;
16737 end if;
16738 end Save_References;
16740 -- Start of processing for Save_Global_References
16742 begin
16743 Gen_Scope := Current_Scope;
16745 -- If the generic unit is a child unit, references to entities in the
16746 -- parent are treated as local, because they will be resolved anew in
16747 -- the context of the instance of the parent.
16749 while Is_Child_Unit (Gen_Scope)
16750 and then Ekind (Scope (Gen_Scope)) = E_Generic_Package
16751 loop
16752 Gen_Scope := Scope (Gen_Scope);
16753 end loop;
16755 Save_References (Templ);
16756 end Save_Global_References;
16758 ---------------------------------------
16759 -- Save_Global_References_In_Aspects --
16760 ---------------------------------------
16762 procedure Save_Global_References_In_Aspects (N : Node_Id) is
16763 Asp : Node_Id;
16764 Expr : Node_Id;
16766 begin
16767 Asp := First (Aspect_Specifications (N));
16768 while Present (Asp) loop
16769 Expr := Expression (Asp);
16771 if Present (Expr) then
16772 Save_Global_References (Expr);
16773 end if;
16775 Next (Asp);
16776 end loop;
16777 end Save_Global_References_In_Aspects;
16779 ------------------------------------------
16780 -- Set_Copied_Sloc_For_Inherited_Pragma --
16781 ------------------------------------------
16783 procedure Set_Copied_Sloc_For_Inherited_Pragma
16784 (N : Node_Id;
16785 E : Entity_Id)
16787 begin
16788 Create_Instantiation_Source (N, E,
16789 Inlined_Body => False,
16790 Inherited_Pragma => True,
16791 Factor => S_Adjustment);
16792 end Set_Copied_Sloc_For_Inherited_Pragma;
16794 --------------------------------------
16795 -- Set_Copied_Sloc_For_Inlined_Body --
16796 --------------------------------------
16798 procedure Set_Copied_Sloc_For_Inlined_Body (N : Node_Id; E : Entity_Id) is
16799 begin
16800 Create_Instantiation_Source (N, E,
16801 Inlined_Body => True,
16802 Inherited_Pragma => False,
16803 Factor => S_Adjustment);
16804 end Set_Copied_Sloc_For_Inlined_Body;
16806 ---------------------
16807 -- Set_Instance_Of --
16808 ---------------------
16810 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id) is
16811 begin
16812 Generic_Renamings.Table (Generic_Renamings.Last) := (A, B, Assoc_Null);
16813 Generic_Renamings_HTable.Set (Generic_Renamings.Last);
16814 Generic_Renamings.Increment_Last;
16815 end Set_Instance_Of;
16817 --------------------
16818 -- Set_Next_Assoc --
16819 --------------------
16821 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr) is
16822 begin
16823 Generic_Renamings.Table (E).Next_In_HTable := Next;
16824 end Set_Next_Assoc;
16826 -------------------
16827 -- Start_Generic --
16828 -------------------
16830 procedure Start_Generic is
16831 begin
16832 -- ??? More things could be factored out in this routine.
16833 -- Should probably be done at a later stage.
16835 Generic_Flags.Append (Inside_A_Generic);
16836 Inside_A_Generic := True;
16838 Expander_Mode_Save_And_Set (False);
16839 end Start_Generic;
16841 ----------------------
16842 -- Set_Instance_Env --
16843 ----------------------
16845 -- WARNING: This routine manages SPARK regions
16847 procedure Set_Instance_Env
16848 (Gen_Unit : Entity_Id;
16849 Act_Unit : Entity_Id)
16851 Saved_AE : constant Boolean := Assertions_Enabled;
16852 Saved_CPL : constant Node_Id := Check_Policy_List;
16853 Saved_DEC : constant Boolean := Dynamic_Elaboration_Checks;
16854 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
16855 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
16857 begin
16858 -- Regardless of the current mode, predefined units are analyzed in the
16859 -- most current Ada mode, and earlier version Ada checks do not apply
16860 -- to predefined units. Nothing needs to be done for non-internal units.
16861 -- These are always analyzed in the current mode.
16863 if In_Internal_Unit (Gen_Unit) then
16865 -- The following call resets all configuration attributes to default
16866 -- or the xxx_Config versions of the attributes when the current sem
16867 -- unit is the main unit. At the same time, internal units must also
16868 -- inherit certain configuration attributes from their context. It
16869 -- is unclear what these two sets are.
16871 Set_Config_Switches (True, Current_Sem_Unit = Main_Unit);
16873 -- Reinstall relevant configuration attributes of the context
16875 Assertions_Enabled := Saved_AE;
16876 Check_Policy_List := Saved_CPL;
16877 Dynamic_Elaboration_Checks := Saved_DEC;
16879 Install_SPARK_Mode (Saved_SM, Saved_SMP);
16880 end if;
16882 Current_Instantiated_Parent :=
16883 (Gen_Id => Gen_Unit,
16884 Act_Id => Act_Unit,
16885 Next_In_HTable => Assoc_Null);
16886 end Set_Instance_Env;
16888 -----------------
16889 -- Switch_View --
16890 -----------------
16892 procedure Switch_View (T : Entity_Id) is
16893 BT : constant Entity_Id := Base_Type (T);
16894 Priv_Elmt : Elmt_Id := No_Elmt;
16895 Priv_Sub : Entity_Id;
16897 begin
16898 -- T may be private but its base type may have been exchanged through
16899 -- some other occurrence, in which case there is nothing to switch
16900 -- besides T itself. Note that a private dependent subtype of a private
16901 -- type might not have been switched even if the base type has been,
16902 -- because of the last branch of Check_Private_View (see comment there).
16904 if not Is_Private_Type (BT) then
16905 Prepend_Elmt (Full_View (T), Exchanged_Views);
16906 Exchange_Declarations (T);
16907 return;
16908 end if;
16910 Priv_Elmt := First_Elmt (Private_Dependents (BT));
16912 if Present (Full_View (BT)) then
16913 Prepend_Elmt (Full_View (BT), Exchanged_Views);
16914 Exchange_Declarations (BT);
16915 end if;
16917 while Present (Priv_Elmt) loop
16918 Priv_Sub := Node (Priv_Elmt);
16920 if Present (Full_View (Priv_Sub)) then
16921 Prepend_Elmt (Full_View (Priv_Sub), Exchanged_Views);
16922 Exchange_Declarations (Priv_Sub);
16923 end if;
16925 Next_Elmt (Priv_Elmt);
16926 end loop;
16927 end Switch_View;
16929 -----------------
16930 -- True_Parent --
16931 -----------------
16933 function True_Parent (N : Node_Id) return Node_Id is
16934 begin
16935 if Nkind (Parent (N)) = N_Subunit then
16936 return Parent (Corresponding_Stub (Parent (N)));
16937 else
16938 return Parent (N);
16939 end if;
16940 end True_Parent;
16942 -----------------------------
16943 -- Valid_Default_Attribute --
16944 -----------------------------
16946 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id) is
16947 Attr_Id : constant Attribute_Id :=
16948 Get_Attribute_Id (Attribute_Name (Def));
16949 T : constant Entity_Id := Entity (Prefix (Def));
16950 Is_Fun : constant Boolean := (Ekind (Nam) = E_Function);
16951 F : Entity_Id;
16952 Num_F : Nat;
16953 OK : Boolean;
16955 begin
16956 if No (T) or else T = Any_Id then
16957 return;
16958 end if;
16960 Num_F := 0;
16961 F := First_Formal (Nam);
16962 while Present (F) loop
16963 Num_F := Num_F + 1;
16964 Next_Formal (F);
16965 end loop;
16967 case Attr_Id is
16968 when Attribute_Adjacent
16969 | Attribute_Ceiling
16970 | Attribute_Copy_Sign
16971 | Attribute_Floor
16972 | Attribute_Fraction
16973 | Attribute_Machine
16974 | Attribute_Model
16975 | Attribute_Remainder
16976 | Attribute_Rounding
16977 | Attribute_Unbiased_Rounding
16979 OK := Is_Fun
16980 and then Num_F = 1
16981 and then Is_Floating_Point_Type (T);
16983 when Attribute_Image
16984 | Attribute_Pred
16985 | Attribute_Succ
16986 | Attribute_Value
16987 | Attribute_Wide_Image
16988 | Attribute_Wide_Value
16990 OK := Is_Fun and then Num_F = 1 and then Is_Scalar_Type (T);
16992 when Attribute_Max
16993 | Attribute_Min
16995 OK := Is_Fun and then Num_F = 2 and then Is_Scalar_Type (T);
16997 when Attribute_Input =>
16998 OK := (Is_Fun and then Num_F = 1);
17000 when Attribute_Output
17001 | Attribute_Put_Image
17002 | Attribute_Read
17003 | Attribute_Write
17005 OK := not Is_Fun and then Num_F = 2;
17007 when others =>
17008 OK := False;
17009 end case;
17011 if not OK then
17012 Error_Msg_N
17013 ("attribute reference has wrong profile for subprogram", Def);
17014 end if;
17015 end Valid_Default_Attribute;
17017 ----------------------------------
17018 -- Validate_Formal_Type_Default --
17019 ----------------------------------
17021 procedure Validate_Formal_Type_Default (Decl : Node_Id) is
17022 Default : constant Node_Id :=
17023 Default_Subtype_Mark (Original_Node (Decl));
17024 Formal : constant Entity_Id := Defining_Identifier (Decl);
17026 Def_Sub : Entity_Id; -- Default subtype mark
17027 Type_Def : Node_Id;
17029 procedure Check_Discriminated_Formal;
17030 -- Check that discriminants of default for private or incomplete
17031 -- type match those of formal type.
17033 function Reference_Formal (N : Node_Id) return Traverse_Result;
17034 -- Check whether formal type definition mentions a previous formal
17035 -- type of the same generic.
17037 ----------------------
17038 -- Reference_Formal --
17039 ----------------------
17041 function Reference_Formal (N : Node_Id) return Traverse_Result is
17042 begin
17043 if Is_Entity_Name (N)
17044 and then Scope (Entity (N)) = Current_Scope
17045 then
17046 return Abandon;
17047 else
17048 return OK;
17049 end if;
17050 end Reference_Formal;
17052 function Depends_On_Other_Formals is
17053 new Traverse_Func (Reference_Formal);
17055 function Default_Subtype_Matches
17056 (Gen_T, Def_T : Entity_Id) return Boolean;
17058 procedure Validate_Array_Type_Default;
17059 -- Verify that dimension, indices, and component types of default
17060 -- are compatible with formal array type definition.
17062 procedure Validate_Derived_Type_Default;
17063 -- Verify that ancestor and progenitor types match.
17065 ---------------------------------
17066 -- Check_Discriminated_Formal --
17067 ---------------------------------
17069 procedure Check_Discriminated_Formal is
17070 Formal_Discr : Entity_Id;
17071 Actual_Discr : Entity_Id;
17072 Formal_Subt : Entity_Id;
17074 begin
17075 if Has_Discriminants (Formal) then
17076 if not Has_Discriminants (Def_Sub) then
17077 Error_Msg_NE
17078 ("default for & must have discriminants", Default, Formal);
17080 elsif Is_Constrained (Def_Sub) then
17081 Error_Msg_NE
17082 ("default for & must be unconstrained", Default, Formal);
17084 else
17085 Formal_Discr := First_Discriminant (Formal);
17086 Actual_Discr := First_Discriminant (Def_Sub);
17087 while Formal_Discr /= Empty loop
17088 if Actual_Discr = Empty then
17089 Error_Msg_N
17090 ("discriminants on Formal do not match formal",
17091 Default);
17092 end if;
17094 Formal_Subt := Etype (Formal_Discr);
17096 -- Access discriminants match if designated types do
17098 if Ekind (Base_Type (Formal_Subt)) = E_Anonymous_Access_Type
17099 and then (Ekind (Base_Type (Etype (Actual_Discr)))) =
17100 E_Anonymous_Access_Type
17101 and then
17102 Designated_Type (Base_Type (Formal_Subt)) =
17103 Designated_Type (Base_Type (Etype (Actual_Discr)))
17104 then
17105 null;
17107 elsif Base_Type (Formal_Subt) /=
17108 Base_Type (Etype (Actual_Discr))
17109 then
17110 Error_Msg_N
17111 ("types of discriminants of default must match formal",
17112 Default);
17114 elsif not Subtypes_Statically_Match
17115 (Formal_Subt, Etype (Actual_Discr))
17116 and then Ada_Version >= Ada_95
17117 then
17118 Error_Msg_N
17119 ("subtypes of discriminants of default "
17120 & "must match formal",
17121 Default);
17122 end if;
17124 Next_Discriminant (Formal_Discr);
17125 Next_Discriminant (Actual_Discr);
17126 end loop;
17128 if Actual_Discr /= Empty then
17129 Error_Msg_NE
17130 ("discriminants on default do not match formal",
17131 Default, Formal);
17132 end if;
17133 end if;
17134 end if;
17135 end Check_Discriminated_Formal;
17137 ---------------------------
17138 -- Default_Subtype_Matches --
17139 ---------------------------
17141 function Default_Subtype_Matches
17142 (Gen_T, Def_T : Entity_Id) return Boolean
17144 begin
17145 -- Check that the base types, root types (when dealing with class
17146 -- wide types), or designated types (when dealing with anonymous
17147 -- access types) of Gen_T and Def_T are statically matching subtypes.
17149 return (Base_Type (Gen_T) = Base_Type (Def_T)
17150 and then Subtypes_Statically_Match (Gen_T, Def_T))
17152 or else (Is_Class_Wide_Type (Gen_T)
17153 and then Is_Class_Wide_Type (Def_T)
17154 and then Default_Subtype_Matches
17155 (Root_Type (Gen_T), Root_Type (Def_T)))
17157 or else (Is_Anonymous_Access_Type (Gen_T)
17158 and then Ekind (Def_T) = Ekind (Gen_T)
17159 and then Subtypes_Statically_Match
17160 (Designated_Type (Gen_T), Designated_Type (Def_T)));
17162 end Default_Subtype_Matches;
17164 ----------------------------------
17165 -- Validate_Array_Type_Default --
17166 ----------------------------------
17168 procedure Validate_Array_Type_Default is
17169 I1, I2 : Node_Id;
17170 T2 : Entity_Id;
17171 begin
17172 if not Is_Array_Type (Def_Sub) then
17173 Error_Msg_NE ("default for& must be an array type ",
17174 Default, Formal);
17175 return;
17177 elsif Number_Dimensions (Def_Sub) /= Number_Dimensions (Formal)
17178 or else Is_Constrained (Def_Sub) /=
17179 Is_Constrained (Formal)
17180 then
17181 Error_Msg_NE ("default array type does not match&",
17182 Default, Formal);
17183 return;
17184 end if;
17186 I1 := First_Index (Formal);
17187 I2 := First_Index (Def_Sub);
17188 for J in 1 .. Number_Dimensions (Formal) loop
17190 -- If the indexes of the actual were given by a subtype_mark,
17191 -- the index was transformed into a range attribute. Retrieve
17192 -- the original type mark for checking.
17194 if Is_Entity_Name (Original_Node (I2)) then
17195 T2 := Entity (Original_Node (I2));
17196 else
17197 T2 := Etype (I2);
17198 end if;
17200 if not Subtypes_Statically_Match (Etype (I1), T2) then
17201 Error_Msg_NE
17202 ("index types of default do not match those of formal &",
17203 Default, Formal);
17204 end if;
17206 Next_Index (I1);
17207 Next_Index (I2);
17208 end loop;
17210 if not Default_Subtype_Matches
17211 (Component_Type (Formal), Component_Type (Def_Sub))
17212 then
17213 Error_Msg_NE
17214 ("component subtype of default does not match that of formal &",
17215 Default, Formal);
17216 end if;
17218 if Has_Aliased_Components (Formal)
17219 and then not Has_Aliased_Components (Default)
17220 then
17221 Error_Msg_NE
17222 ("default must have aliased components to match formal type &",
17223 Default, Formal);
17224 end if;
17225 end Validate_Array_Type_Default;
17227 -----------------------------------
17228 -- Validate_Derived_Type_Default --
17229 -----------------------------------
17231 procedure Validate_Derived_Type_Default is
17232 begin
17233 if not Is_Ancestor (Etype (Formal), Def_Sub) then
17234 Error_Msg_NE ("default must be a descendent of&",
17235 Default, Etype (Formal));
17236 end if;
17238 if Has_Interfaces (Formal) then
17239 if not Has_Interfaces (Def_Sub) then
17240 Error_Msg_NE
17241 ("default must implement all interfaces of formal&",
17242 Default, Formal);
17244 else
17245 declare
17246 Act_Iface_List : Elist_Id;
17247 Iface : Node_Id;
17248 Iface_Ent : Entity_Id;
17250 begin
17251 Iface := First (Abstract_Interface_List (Formal));
17252 Collect_Interfaces (Def_Sub, Act_Iface_List);
17254 while Present (Iface) loop
17255 Iface_Ent := Entity (Iface);
17257 if Is_Ancestor (Iface_Ent, Def_Sub)
17258 or else Is_Progenitor (Iface_Ent, Def_Sub)
17259 then
17260 null;
17262 else
17263 Error_Msg_NE
17264 ("Default must implement interface&",
17265 Default, Etype (Iface));
17266 end if;
17268 Next (Iface);
17269 end loop;
17270 end;
17271 end if;
17272 end if;
17273 end Validate_Derived_Type_Default;
17275 -- Start of processing for Validate_Formal_Type_Default
17277 begin
17278 Analyze (Default);
17279 if not Is_Entity_Name (Default)
17280 or else not Is_Type (Entity (Default))
17281 then
17282 Error_Msg_N
17283 ("Expect type name for default of formal type", Default);
17284 return;
17285 else
17286 Def_Sub := Entity (Default);
17287 end if;
17289 -- Formal derived_type declarations are transformed into full
17290 -- type declarations or Private_Type_Extensions for ease of processing.
17292 if Nkind (Decl) = N_Full_Type_Declaration then
17293 Type_Def := Type_Definition (Decl);
17295 elsif Nkind (Decl) = N_Private_Extension_Declaration then
17296 Type_Def := Subtype_Indication (Decl);
17298 else
17299 Type_Def := Formal_Type_Definition (Decl);
17300 end if;
17302 if Depends_On_Other_Formals (Type_Def) = Abandon
17303 and then Scope (Def_Sub) /= Current_Scope
17304 then
17305 Error_Msg_N ("default of formal type that depends on "
17306 & "other formals must be a previous formal type", Default);
17307 return;
17309 elsif Def_Sub = Formal then
17310 Error_Msg_N
17311 ("default for formal type cannot be formal itsef", Default);
17312 return;
17313 end if;
17315 case Nkind (Type_Def) is
17317 when N_Formal_Private_Type_Definition =>
17318 if (Is_Abstract_Type (Formal)
17319 and then not Is_Abstract_Type (Def_Sub))
17320 or else (Is_Limited_Type (Formal)
17321 and then not Is_Limited_Type (Def_Sub))
17322 then
17323 Error_Msg_NE
17324 ("default for private type$ does not match",
17325 Default, Formal);
17326 end if;
17328 Check_Discriminated_Formal;
17330 when N_Formal_Derived_Type_Definition =>
17331 Check_Discriminated_Formal;
17332 Validate_Derived_Type_Default;
17334 when N_Formal_Incomplete_Type_Definition =>
17335 if Is_Tagged_Type (Formal)
17336 and then not Is_Tagged_Type (Def_Sub)
17337 then
17338 Error_Msg_NE
17339 ("default for & must be a tagged type", Default, Formal);
17340 end if;
17342 Check_Discriminated_Formal;
17344 when N_Formal_Discrete_Type_Definition =>
17345 if not Is_Discrete_Type (Def_Sub) then
17346 Error_Msg_NE ("default for& must be a discrete type",
17347 Default, Formal);
17348 end if;
17350 when N_Formal_Signed_Integer_Type_Definition =>
17351 if not Is_Integer_Type (Def_Sub) then
17352 Error_Msg_NE ("default for& must be a discrete type",
17353 Default, Formal);
17354 end if;
17356 when N_Formal_Modular_Type_Definition =>
17357 if not Is_Modular_Integer_Type (Def_Sub) then
17358 Error_Msg_NE ("default for& must be a modular_integer Type",
17359 Default, Formal);
17360 end if;
17362 when N_Formal_Floating_Point_Definition =>
17363 if not Is_Floating_Point_Type (Def_Sub) then
17364 Error_Msg_NE ("default for& must be a floating_point type",
17365 Default, Formal);
17366 end if;
17368 when N_Formal_Ordinary_Fixed_Point_Definition =>
17369 if not Is_Ordinary_Fixed_Point_Type (Def_Sub) then
17370 Error_Msg_NE ("default for& must be "
17371 & "an ordinary_fixed_point type ",
17372 Default, Formal);
17373 end if;
17375 when N_Formal_Decimal_Fixed_Point_Definition =>
17376 if not Is_Decimal_Fixed_Point_Type (Def_Sub) then
17377 Error_Msg_NE ("default for& must be "
17378 & "an Decimal_fixed_point type ",
17379 Default, Formal);
17380 end if;
17382 when N_Array_Type_Definition =>
17383 Validate_Array_Type_Default;
17385 when N_Access_Function_Definition |
17386 N_Access_Procedure_Definition =>
17387 if Ekind (Def_Sub) /= E_Access_Subprogram_Type then
17388 Error_Msg_NE ("default for& must be an Access_To_Subprogram",
17389 Default, Formal);
17390 end if;
17391 Check_Subtype_Conformant
17392 (Designated_Type (Formal), Designated_Type (Def_Sub));
17394 when N_Access_To_Object_Definition =>
17395 if not Is_Access_Object_Type (Def_Sub) then
17396 Error_Msg_NE ("default for& must be an Access_To_Object",
17397 Default, Formal);
17399 elsif not Default_Subtype_Matches
17400 (Designated_Type (Formal), Designated_Type (Def_Sub))
17401 then
17402 Error_Msg_NE ("designated type of defaul does not match "
17403 & "designated type of formal type",
17404 Default, Formal);
17405 end if;
17407 when N_Record_Definition => -- Formal interface type
17408 if not Is_Interface (Def_Sub) then
17409 Error_Msg_NE
17410 ("default for formal interface type must be an interface",
17411 Default, Formal);
17413 elsif Is_Limited_Type (Def_Sub) /= Is_Limited_Type (Formal)
17414 or else Is_Task_Interface (Formal) /= Is_Task_Interface (Def_Sub)
17415 or else Is_Protected_Interface (Formal) /=
17416 Is_Protected_Interface (Def_Sub)
17417 or else Is_Synchronized_Interface (Formal) /=
17418 Is_Synchronized_Interface (Def_Sub)
17419 then
17420 Error_Msg_NE
17421 ("default for interface& does not match", Def_Sub, Formal);
17422 end if;
17424 when N_Derived_Type_Definition =>
17425 Validate_Derived_Type_Default;
17427 when N_Identifier => -- case of a private extension
17428 Validate_Derived_Type_Default;
17430 when N_Error =>
17431 null;
17433 when others =>
17434 raise Program_Error;
17435 end case;
17436 end Validate_Formal_Type_Default;
17437 end Sem_Ch12;