ada: Update copyright notice
[official-gcc.git] / gcc / ada / sem_ch12.adb
blob39ceaf7c16f3c6d2ec6179b5fe0970fa077e4e1c
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-2023, 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 := Empty;
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 : Node_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 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 function Adjust_Aspect_Sloc (N : Node_Id) return Traverse_Result;
1207 -- Adjust sloc so that errors located at N will be reported with
1208 -- information about the instance and not just about the generic.
1210 ------------------------
1211 -- Adjust_Aspect_Sloc --
1212 ------------------------
1214 function Adjust_Aspect_Sloc (N : Node_Id) return Traverse_Result is
1215 begin
1216 Adjust_Instantiation_Sloc (N, S_Adjustment);
1217 return OK;
1218 end Adjust_Aspect_Sloc;
1220 procedure Adjust_Aspect_Slocs is new
1221 Traverse_Proc (Adjust_Aspect_Sloc);
1223 Formal : constant Entity_Id :=
1224 Defining_Unit_Name (Specification (Analyzed_Formal));
1225 Aspect_Spec : Node_Id;
1226 Decl_Node : Node_Id;
1227 Actual_Name : Node_Id;
1229 -- Start of processing for Build_Subprogram_Wrappers
1231 begin
1232 -- Create declaration for wrapper subprogram
1233 -- The actual can be overloaded, in which case it will be
1234 -- resolved when the call in the wrapper body is analyzed.
1235 -- We attach the possible interpretations of the actual to
1236 -- the name to be used in the call in the wrapper body.
1238 if Is_Entity_Name (Match) then
1239 Actual_Name := New_Occurrence_Of (Entity (Match), Sloc (Match));
1241 if Is_Overloaded (Match) then
1242 Save_Interps (Match, Actual_Name);
1243 end if;
1245 else
1246 -- Use renaming declaration created when analyzing actual.
1247 -- This may be incomplete if there are several formal
1248 -- subprograms whose actual is an attribute ???
1250 declare
1251 Renaming_Decl : constant Node_Id := Last (Assoc_List);
1253 begin
1254 Actual_Name := New_Occurrence_Of
1255 (Defining_Entity (Renaming_Decl), Sloc (Match));
1256 Set_Etype (Actual_Name, Get_Instance_Of (Etype (Formal)));
1257 end;
1258 end if;
1260 Decl_Node := Build_Subprogram_Decl_Wrapper (Formal);
1262 -- Transfer aspect specifications from formal subprogram to wrapper
1264 Set_Aspect_Specifications (Decl_Node,
1265 New_Copy_List_Tree (Aspect_Specifications (Analyzed_Formal)));
1267 Aspect_Spec := First (Aspect_Specifications (Decl_Node));
1268 while Present (Aspect_Spec) loop
1269 Adjust_Aspect_Slocs (Aspect_Spec);
1270 Set_Analyzed (Aspect_Spec, False);
1271 Next (Aspect_Spec);
1272 end loop;
1274 Append_To (Assoc_List, Decl_Node);
1276 -- Create corresponding body, and append it to association list
1277 -- that appears at the head of the declarations in the instance.
1278 -- The subprogram may be called in the analysis of subsequent
1279 -- actuals.
1281 Append_To (Assoc_List,
1282 Build_Subprogram_Body_Wrapper (Formal, Actual_Name));
1283 end Build_Subprogram_Wrappers;
1285 ----------------------------------------
1286 -- Check_Overloaded_Formal_Subprogram --
1287 ----------------------------------------
1289 procedure Check_Overloaded_Formal_Subprogram (Formal : Node_Id) is
1290 Temp_Formal : Node_Id;
1292 begin
1293 Temp_Formal := First (Formals);
1294 while Present (Temp_Formal) loop
1295 if Nkind (Temp_Formal) in N_Formal_Subprogram_Declaration
1296 and then Temp_Formal /= Formal
1297 and then
1298 Chars (Defining_Unit_Name (Specification (Formal))) =
1299 Chars (Defining_Unit_Name (Specification (Temp_Formal)))
1300 then
1301 if Present (Found_Assoc) then
1302 Error_Msg_N
1303 ("named association not allowed for overloaded formal",
1304 Found_Assoc);
1306 else
1307 Error_Msg_N
1308 ("named association not allowed for overloaded formal",
1309 Others_Choice);
1310 end if;
1312 Abandon_Instantiation (Instantiation_Node);
1313 end if;
1315 Next (Temp_Formal);
1316 end loop;
1317 end Check_Overloaded_Formal_Subprogram;
1319 -------------------------------
1320 -- Check_Fixed_Point_Actual --
1321 -------------------------------
1323 procedure Check_Fixed_Point_Actual (Actual : Node_Id) is
1324 Typ : constant Entity_Id := Entity (Actual);
1325 Prims : constant Elist_Id := Collect_Primitive_Operations (Typ);
1326 Elem : Elmt_Id;
1327 Formal : Node_Id;
1328 Op : Entity_Id;
1330 begin
1331 -- Locate primitive operations of the type that are arithmetic
1332 -- operations.
1334 Elem := First_Elmt (Prims);
1335 while Present (Elem) loop
1336 if Nkind (Node (Elem)) = N_Defining_Operator_Symbol then
1338 -- Check whether the generic unit has a formal subprogram of
1339 -- the same name. This does not check types but is good enough
1340 -- to justify a warning.
1342 Formal := First_Non_Pragma (Formals);
1343 Op := Alias (Node (Elem));
1345 while Present (Formal) loop
1346 if Nkind (Formal) = N_Formal_Concrete_Subprogram_Declaration
1347 and then Chars (Defining_Entity (Formal)) =
1348 Chars (Node (Elem))
1349 then
1350 exit;
1352 elsif Nkind (Formal) = N_Formal_Package_Declaration then
1353 declare
1354 Assoc : Node_Id;
1355 Ent : Entity_Id;
1357 begin
1358 -- Locate corresponding actual, and check whether it
1359 -- includes a fixed-point type.
1361 Assoc := First (Assoc_List);
1362 while Present (Assoc) loop
1363 exit when
1364 Nkind (Assoc) = N_Package_Renaming_Declaration
1365 and then Chars (Defining_Unit_Name (Assoc)) =
1366 Chars (Defining_Identifier (Formal));
1368 Next (Assoc);
1369 end loop;
1371 if Present (Assoc) then
1373 -- If formal package declares a fixed-point type,
1374 -- and the user-defined operator is derived from
1375 -- a generic instance package, the fixed-point type
1376 -- does not use the corresponding predefined op.
1378 Ent := First_Entity (Entity (Name (Assoc)));
1379 while Present (Ent) loop
1380 if Is_Fixed_Point_Type (Ent)
1381 and then Present (Op)
1382 and then Is_Generic_Instance (Scope (Op))
1383 then
1384 return;
1385 end if;
1387 Next_Entity (Ent);
1388 end loop;
1389 end if;
1390 end;
1391 end if;
1393 Next (Formal);
1394 end loop;
1396 if No (Formal) then
1397 Error_Msg_Sloc := Sloc (Node (Elem));
1398 Error_Msg_NE
1399 ("?instance uses predefined operation, not primitive "
1400 & "operation&#", Actual, Node (Elem));
1401 end if;
1402 end if;
1404 Next_Elmt (Elem);
1405 end loop;
1406 end Check_Fixed_Point_Actual;
1408 -------------------------------
1409 -- Has_Fully_Defined_Profile --
1410 -------------------------------
1412 function Has_Fully_Defined_Profile (Subp : Entity_Id) return Boolean is
1413 function Is_Fully_Defined_Type (Typ : Entity_Id) return Boolean;
1414 -- Determine whethet type Typ is fully defined
1416 ---------------------------
1417 -- Is_Fully_Defined_Type --
1418 ---------------------------
1420 function Is_Fully_Defined_Type (Typ : Entity_Id) return Boolean is
1421 begin
1422 -- A private type without a full view is not fully defined
1424 if Is_Private_Type (Typ)
1425 and then No (Full_View (Typ))
1426 then
1427 return False;
1429 -- An incomplete type is never fully defined
1431 elsif Is_Incomplete_Type (Typ) then
1432 return False;
1434 -- All other types are fully defined
1436 else
1437 return True;
1438 end if;
1439 end Is_Fully_Defined_Type;
1441 -- Local declarations
1443 Param : Entity_Id;
1445 -- Start of processing for Has_Fully_Defined_Profile
1447 begin
1448 -- Check the parameters
1450 Param := First_Formal (Subp);
1451 while Present (Param) loop
1452 if not Is_Fully_Defined_Type (Etype (Param)) then
1453 return False;
1454 end if;
1456 Next_Formal (Param);
1457 end loop;
1459 -- Check the return type
1461 return Is_Fully_Defined_Type (Etype (Subp));
1462 end Has_Fully_Defined_Profile;
1464 ---------------------
1465 -- Matching_Actual --
1466 ---------------------
1468 function Matching_Actual
1469 (F : Entity_Id;
1470 A_F : Entity_Id) return Node_Id
1472 Prev : Node_Id;
1473 Act : Node_Id;
1475 begin
1476 Is_Named_Assoc := False;
1478 -- End of list of purely positional parameters
1480 if No (Actual) or else Nkind (Actual) = N_Others_Choice then
1481 Found_Assoc := Empty;
1482 Act := Empty;
1484 -- Case of positional parameter corresponding to current formal
1486 elsif No (Selector_Name (Actual)) then
1487 Found_Assoc := Actual;
1488 Act := Explicit_Generic_Actual_Parameter (Actual);
1489 Num_Matched := Num_Matched + 1;
1490 Next (Actual);
1492 -- Otherwise scan list of named actuals to find the one with the
1493 -- desired name. All remaining actuals have explicit names.
1495 else
1496 Is_Named_Assoc := True;
1497 Found_Assoc := Empty;
1498 Act := Empty;
1499 Prev := Empty;
1501 while Present (Actual) loop
1502 if Nkind (Actual) = N_Others_Choice then
1503 Found_Assoc := Empty;
1504 Act := Empty;
1506 elsif Chars (Selector_Name (Actual)) = Chars (F) then
1507 Set_Entity (Selector_Name (Actual), A_F);
1508 Set_Etype (Selector_Name (Actual), Etype (A_F));
1509 Generate_Reference (A_F, Selector_Name (Actual));
1511 Found_Assoc := Actual;
1512 Act := Explicit_Generic_Actual_Parameter (Actual);
1513 Num_Matched := Num_Matched + 1;
1514 exit;
1515 end if;
1517 Prev := Actual;
1518 Next (Actual);
1519 end loop;
1521 -- Reset for subsequent searches. In most cases the named
1522 -- associations are in order. If they are not, we reorder them
1523 -- to avoid scanning twice the same actual. This is not just a
1524 -- question of efficiency: there may be multiple defaults with
1525 -- boxes that have the same name. In a nested instantiation we
1526 -- insert actuals for those defaults, and cannot rely on their
1527 -- names to disambiguate them.
1529 if Actual = First_Named then
1530 Next (First_Named);
1532 elsif Present (Actual) then
1533 Insert_Before (First_Named, Remove_Next (Prev));
1534 end if;
1536 Actual := First_Named;
1537 end if;
1539 if Is_Entity_Name (Act) and then Present (Entity (Act)) then
1540 Set_Used_As_Generic_Actual (Entity (Act));
1541 end if;
1543 return Act;
1544 end Matching_Actual;
1546 ------------------------------
1547 -- Partial_Parameterization --
1548 ------------------------------
1550 function Partial_Parameterization return Boolean is
1551 begin
1552 return Others_Present
1553 or else (Present (Found_Assoc) and then Box_Present (Found_Assoc));
1554 end Partial_Parameterization;
1556 ---------------------
1557 -- Process_Default --
1558 ---------------------
1560 procedure Process_Default (Formal : Node_Id) is
1561 Loc : constant Source_Ptr := Sloc (I_Node);
1562 F_Id : constant Entity_Id := Defining_Entity (Formal);
1563 Decl : Node_Id;
1564 Default : Node_Id;
1565 Id : Entity_Id;
1567 begin
1568 -- Append copy of formal declaration to associations, and create new
1569 -- defining identifier for it.
1571 Decl := New_Copy_Tree (Formal);
1572 Id := Make_Defining_Identifier (Sloc (F_Id), Chars (F_Id));
1574 if Nkind (Formal) in N_Formal_Subprogram_Declaration then
1575 Set_Defining_Unit_Name (Specification (Decl), Id);
1577 else
1578 Set_Defining_Identifier (Decl, Id);
1579 end if;
1581 Append (Decl, Assoc_List);
1583 if No (Found_Assoc) then
1584 Default :=
1585 Make_Generic_Association (Loc,
1586 Selector_Name =>
1587 New_Occurrence_Of (Id, Loc),
1588 Explicit_Generic_Actual_Parameter => Empty);
1589 Set_Box_Present (Default);
1590 Append (Default, Default_Formals);
1591 end if;
1592 end Process_Default;
1594 ---------------------------------
1595 -- Renames_Standard_Subprogram --
1596 ---------------------------------
1598 function Renames_Standard_Subprogram (Subp : Entity_Id) return Boolean is
1599 Id : Entity_Id;
1601 begin
1602 Id := Alias (Subp);
1603 while Present (Id) loop
1604 if Scope (Id) = Standard_Standard then
1605 return True;
1606 end if;
1608 Id := Alias (Id);
1609 end loop;
1611 return False;
1612 end Renames_Standard_Subprogram;
1614 -------------------------
1615 -- Set_Analyzed_Formal --
1616 -------------------------
1618 procedure Set_Analyzed_Formal is
1619 Kind : Node_Kind;
1621 begin
1622 while Present (Analyzed_Formal) loop
1623 Kind := Nkind (Analyzed_Formal);
1625 case Nkind (Formal) is
1626 when N_Formal_Subprogram_Declaration =>
1627 exit when Kind in N_Formal_Subprogram_Declaration
1628 and then
1629 Chars
1630 (Defining_Unit_Name (Specification (Formal))) =
1631 Chars
1632 (Defining_Unit_Name (Specification (Analyzed_Formal)));
1634 when N_Formal_Package_Declaration =>
1635 exit when Kind in N_Formal_Package_Declaration
1636 | N_Generic_Package_Declaration
1637 | N_Package_Declaration;
1639 when N_Use_Package_Clause
1640 | N_Use_Type_Clause
1642 exit;
1644 when others =>
1646 -- Skip freeze nodes, and nodes inserted to replace
1647 -- unrecognized pragmas.
1649 exit when
1650 Kind not in N_Formal_Subprogram_Declaration
1651 and then Kind not in N_Subprogram_Declaration
1652 | N_Freeze_Entity
1653 | N_Null_Statement
1654 | N_Itype_Reference
1655 and then Chars (Defining_Identifier (Formal)) =
1656 Chars (Defining_Identifier (Analyzed_Formal));
1657 end case;
1659 Next (Analyzed_Formal);
1660 end loop;
1661 end Set_Analyzed_Formal;
1663 -- Start of processing for Analyze_Associations
1665 begin
1666 Actuals := Generic_Associations (I_Node);
1668 if Present (Actuals) then
1670 -- Check for an Others choice, indicating a partial parameterization
1671 -- for a formal package.
1673 Actual := First (Actuals);
1674 while Present (Actual) loop
1675 if Nkind (Actual) = N_Others_Choice then
1676 Others_Present := True;
1677 Others_Choice := Actual;
1679 if Present (Next (Actual)) then
1680 Error_Msg_N ("OTHERS must be last association", Actual);
1681 end if;
1683 -- This subprogram is used both for formal packages and for
1684 -- instantiations. For the latter, associations must all be
1685 -- explicit.
1687 if Nkind (I_Node) /= N_Formal_Package_Declaration
1688 and then Comes_From_Source (I_Node)
1689 then
1690 Error_Msg_N
1691 ("OTHERS association not allowed in an instance",
1692 Actual);
1693 end if;
1695 -- In any case, nothing to do after the others association
1697 exit;
1699 elsif Box_Present (Actual)
1700 and then Comes_From_Source (I_Node)
1701 and then Nkind (I_Node) /= N_Formal_Package_Declaration
1702 then
1703 Error_Msg_N
1704 ("box association not allowed in an instance", Actual);
1705 end if;
1707 Next (Actual);
1708 end loop;
1710 -- If named associations are present, save first named association
1711 -- (it may of course be Empty) to facilitate subsequent name search.
1713 First_Named := First (Actuals);
1714 while Present (First_Named)
1715 and then Nkind (First_Named) /= N_Others_Choice
1716 and then No (Selector_Name (First_Named))
1717 loop
1718 Num_Actuals := Num_Actuals + 1;
1719 Next (First_Named);
1720 end loop;
1721 end if;
1723 Named := First_Named;
1724 while Present (Named) loop
1725 if Nkind (Named) /= N_Others_Choice
1726 and then No (Selector_Name (Named))
1727 then
1728 Error_Msg_N ("invalid positional actual after named one", Named);
1729 Abandon_Instantiation (Named);
1730 end if;
1732 -- A named association may lack an actual parameter, if it was
1733 -- introduced for a default subprogram that turns out to be local
1734 -- to the outer instantiation. If it has a box association it must
1735 -- correspond to some formal in the generic.
1737 if Nkind (Named) /= N_Others_Choice
1738 and then (Present (Explicit_Generic_Actual_Parameter (Named))
1739 or else Box_Present (Named))
1740 then
1741 Num_Actuals := Num_Actuals + 1;
1742 end if;
1744 Next (Named);
1745 end loop;
1747 if Present (Formals) then
1748 Formal := First_Non_Pragma (Formals);
1749 Analyzed_Formal := First_Non_Pragma (F_Copy);
1751 if Present (Actuals) then
1752 Actual := First (Actuals);
1754 -- All formals should have default values
1756 else
1757 Actual := Empty;
1758 end if;
1760 while Present (Formal) loop
1761 Set_Analyzed_Formal;
1762 Saved_Formal := Next_Non_Pragma (Formal);
1764 case Nkind (Formal) is
1765 when N_Formal_Object_Declaration =>
1766 Match :=
1767 Matching_Actual
1768 (Defining_Identifier (Formal),
1769 Defining_Identifier (Analyzed_Formal));
1771 if No (Match) and then Partial_Parameterization then
1772 Process_Default (Formal);
1774 else
1775 Append_List
1776 (Instantiate_Object (Formal, Match, Analyzed_Formal),
1777 Assoc_List);
1779 -- For a defaulted in_parameter, create an entry in the
1780 -- the list of defaulted actuals, for GNATprove use. Do
1781 -- not included these defaults for an instance nested
1782 -- within a generic, because the defaults are also used
1783 -- in the analysis of the enclosing generic, and only
1784 -- defaulted subprograms are relevant there.
1786 if No (Match) and then not Inside_A_Generic then
1787 Append_To (Default_Actuals,
1788 Make_Generic_Association (Sloc (I_Node),
1789 Selector_Name =>
1790 New_Occurrence_Of
1791 (Defining_Identifier (Formal), Sloc (I_Node)),
1792 Explicit_Generic_Actual_Parameter =>
1793 New_Copy_Tree (Default_Expression (Formal))));
1794 end if;
1795 end if;
1797 -- If the object is a call to an expression function, this
1798 -- is a freezing point for it.
1800 if Is_Entity_Name (Match)
1801 and then Present (Entity (Match))
1802 and then Nkind
1803 (Original_Node (Unit_Declaration_Node (Entity (Match))))
1804 = N_Expression_Function
1805 then
1806 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1807 end if;
1809 when N_Formal_Type_Declaration =>
1810 Match :=
1811 Matching_Actual
1812 (Defining_Identifier (Formal),
1813 Defining_Identifier (Analyzed_Formal));
1815 if No (Match) then
1816 if Partial_Parameterization then
1817 Process_Default (Formal);
1819 elsif Present (Default_Subtype_Mark (Formal)) then
1820 Match := New_Copy (Default_Subtype_Mark (Formal));
1821 Append_List
1822 (Instantiate_Type
1823 (Formal, Match, Analyzed_Formal, Assoc_List),
1824 Assoc_List);
1825 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1827 else
1828 Error_Msg_Sloc := Sloc (Gen_Unit);
1829 Error_Msg_NE
1830 ("missing actual&",
1831 Instantiation_Node, Defining_Identifier (Formal));
1832 Error_Msg_NE
1833 ("\in instantiation of & declared#",
1834 Instantiation_Node, Gen_Unit);
1835 Abandon_Instantiation (Instantiation_Node);
1836 end if;
1838 else
1839 Analyze (Match);
1840 Append_List
1841 (Instantiate_Type
1842 (Formal, Match, Analyzed_Formal, Assoc_List),
1843 Assoc_List);
1845 -- Warn when an actual is a fixed-point with user-
1846 -- defined promitives. The warning is superfluous
1847 -- if the formal is private, because there can be
1848 -- no arithmetic operations in the generic so there
1849 -- no danger of confusion.
1851 if Is_Fixed_Point_Type (Entity (Match))
1852 and then not Is_Private_Type
1853 (Defining_Identifier (Analyzed_Formal))
1854 then
1855 Check_Fixed_Point_Actual (Match);
1856 end if;
1858 -- An instantiation is a freeze point for the actuals,
1859 -- unless this is a rewritten formal package, or the
1860 -- formal is an Ada 2012 formal incomplete type.
1862 if Nkind (I_Node) = N_Formal_Package_Declaration
1863 or else
1864 (Ada_Version >= Ada_2012
1865 and then
1866 Ekind (Defining_Identifier (Analyzed_Formal)) =
1867 E_Incomplete_Type)
1868 then
1869 null;
1871 else
1872 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1873 end if;
1874 end if;
1876 -- A remote access-to-class-wide type is not a legal actual
1877 -- for a generic formal of an access type (E.2.2(17/2)).
1878 -- In GNAT an exception to this rule is introduced when
1879 -- the formal is marked as remote using implementation
1880 -- defined aspect/pragma Remote_Access_Type. In that case
1881 -- the actual must be remote as well.
1883 -- If the current instantiation is the construction of a
1884 -- local copy for a formal package the actuals may be
1885 -- defaulted, and there is no matching actual to check.
1887 if Nkind (Analyzed_Formal) = N_Formal_Type_Declaration
1888 and then
1889 Nkind (Formal_Type_Definition (Analyzed_Formal)) =
1890 N_Access_To_Object_Definition
1891 and then Present (Match)
1892 then
1893 declare
1894 Formal_Ent : constant Entity_Id :=
1895 Defining_Identifier (Analyzed_Formal);
1896 begin
1897 if Is_Remote_Access_To_Class_Wide_Type (Entity (Match))
1898 = Is_Remote_Types (Formal_Ent)
1899 then
1900 -- Remoteness of formal and actual match
1902 null;
1904 elsif Is_Remote_Types (Formal_Ent) then
1906 -- Remote formal, non-remote actual
1908 Error_Msg_NE
1909 ("actual for& must be remote", Match, Formal_Ent);
1911 else
1912 -- Non-remote formal, remote actual
1914 Error_Msg_NE
1915 ("actual for& may not be remote",
1916 Match, Formal_Ent);
1917 end if;
1918 end;
1919 end if;
1921 when N_Formal_Subprogram_Declaration =>
1922 Match :=
1923 Matching_Actual
1924 (Defining_Unit_Name (Specification (Formal)),
1925 Defining_Unit_Name (Specification (Analyzed_Formal)));
1927 -- If the formal subprogram has the same name as another
1928 -- formal subprogram of the generic, then a named
1929 -- association is illegal (12.3(9)). Exclude named
1930 -- associations that are generated for a nested instance.
1932 if Present (Match)
1933 and then Is_Named_Assoc
1934 and then Comes_From_Source (Found_Assoc)
1935 then
1936 Check_Overloaded_Formal_Subprogram (Formal);
1937 end if;
1939 -- If there is no corresponding actual, this may be case
1940 -- of partial parameterization, or else the formal has a
1941 -- default or a box.
1943 if No (Match) and then Partial_Parameterization then
1944 Process_Default (Formal);
1946 if Nkind (I_Node) = N_Formal_Package_Declaration then
1947 Check_Overloaded_Formal_Subprogram (Formal);
1948 end if;
1950 else
1951 Append_To (Assoc_List,
1952 Instantiate_Formal_Subprogram
1953 (Formal, Match, Analyzed_Formal));
1955 -- If formal subprogram has contracts, create wrappers
1956 -- for it. This is an expansion activity that cannot
1957 -- take place e.g. within an enclosing generic unit.
1959 if Has_Contracts (Analyzed_Formal)
1960 and then (Expander_Active or GNATprove_Mode)
1961 then
1962 Build_Subprogram_Wrappers;
1963 end if;
1965 -- An instantiation is a freeze point for the actuals,
1966 -- unless this is a rewritten formal package.
1968 if Nkind (I_Node) /= N_Formal_Package_Declaration
1969 and then Nkind (Match) = N_Identifier
1970 and then Is_Subprogram (Entity (Match))
1972 -- The actual subprogram may rename a routine defined
1973 -- in Standard. Avoid freezing such renamings because
1974 -- subprograms coming from Standard cannot be frozen.
1976 and then
1977 not Renames_Standard_Subprogram (Entity (Match))
1979 -- If the actual subprogram comes from a different
1980 -- unit, it is already frozen, either by a body in
1981 -- that unit or by the end of the declarative part
1982 -- of the unit. This check avoids the freezing of
1983 -- subprograms defined in Standard which are used
1984 -- as generic actuals.
1986 and then In_Same_Code_Unit (Entity (Match), I_Node)
1987 and then Has_Fully_Defined_Profile (Entity (Match))
1988 then
1989 -- Mark the subprogram as having a delayed freeze
1990 -- since this may be an out-of-order action.
1992 Set_Has_Delayed_Freeze (Entity (Match));
1993 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1994 end if;
1995 end if;
1997 -- If this is a nested generic, preserve default for later
1998 -- instantiations. We do this as well for GNATprove use,
1999 -- so that the list of generic associations is complete.
2001 if No (Match) and then Box_Present (Formal) then
2002 declare
2003 Subp : constant Entity_Id :=
2004 Defining_Unit_Name
2005 (Specification (Last (Assoc_List)));
2007 begin
2008 Append_To (Default_Actuals,
2009 Make_Generic_Association (Sloc (I_Node),
2010 Selector_Name =>
2011 New_Occurrence_Of (Subp, Sloc (I_Node)),
2012 Explicit_Generic_Actual_Parameter =>
2013 New_Occurrence_Of (Subp, Sloc (I_Node))));
2014 end;
2015 end if;
2017 when N_Formal_Package_Declaration =>
2018 -- The name of the formal package may be hidden by the
2019 -- formal parameter itself.
2021 if Error_Posted (Analyzed_Formal) then
2022 Abandon_Instantiation (Instantiation_Node);
2024 else
2025 Match :=
2026 Matching_Actual
2027 (Defining_Identifier (Formal),
2028 Defining_Identifier
2029 (Original_Node (Analyzed_Formal)));
2030 end if;
2032 if No (Match) then
2033 if Partial_Parameterization then
2034 Process_Default (Formal);
2036 else
2037 Error_Msg_Sloc := Sloc (Gen_Unit);
2038 Error_Msg_NE
2039 ("missing actual&",
2040 Instantiation_Node, Defining_Identifier (Formal));
2041 Error_Msg_NE
2042 ("\in instantiation of & declared#",
2043 Instantiation_Node, Gen_Unit);
2045 Abandon_Instantiation (Instantiation_Node);
2046 end if;
2048 else
2049 Analyze (Match);
2050 Append_List
2051 (Instantiate_Formal_Package
2052 (Formal, Match, Analyzed_Formal),
2053 Assoc_List);
2055 -- Determine whether the actual package needs an explicit
2056 -- freeze node. This is only the case if the actual is
2057 -- declared in the same unit and has a body. Normally
2058 -- packages do not have explicit freeze nodes, and gigi
2059 -- only uses them to elaborate entities in a package
2060 -- body.
2062 Explicit_Freeze_Check : declare
2063 Actual : constant Entity_Id := Entity (Match);
2064 Gen_Par : Entity_Id;
2066 Needs_Freezing : Boolean;
2067 P : Node_Id;
2069 procedure Check_Generic_Parent;
2070 -- The actual may be an instantiation of a unit
2071 -- declared in a previous instantiation. If that
2072 -- one is also in the current compilation, it must
2073 -- itself be frozen before the actual. The actual
2074 -- may be an instantiation of a generic child unit,
2075 -- in which case the same applies to the instance
2076 -- of the parent which must be frozen before the
2077 -- actual.
2078 -- Should this itself be recursive ???
2080 --------------------------
2081 -- Check_Generic_Parent --
2082 --------------------------
2084 procedure Check_Generic_Parent is
2085 Inst : constant Node_Id :=
2086 Get_Unit_Instantiation_Node (Actual);
2087 Par : Entity_Id;
2089 begin
2090 Par := Empty;
2092 if Nkind (Parent (Actual)) = N_Package_Specification
2093 then
2094 Par := Scope (Generic_Parent (Parent (Actual)));
2096 if Is_Generic_Instance (Par) then
2097 null;
2099 -- If the actual is a child generic unit, check
2100 -- whether the instantiation of the parent is
2101 -- also local and must also be frozen now. We
2102 -- must retrieve the instance node to locate the
2103 -- parent instance if any.
2105 elsif Ekind (Par) = E_Generic_Package
2106 and then Is_Child_Unit (Gen_Par)
2107 and then Ekind (Scope (Gen_Par)) =
2108 E_Generic_Package
2109 then
2110 if Nkind (Inst) = N_Package_Instantiation
2111 and then Nkind (Name (Inst)) =
2112 N_Expanded_Name
2113 then
2114 -- Retrieve entity of parent instance
2116 Par := Entity (Prefix (Name (Inst)));
2117 end if;
2119 else
2120 Par := Empty;
2121 end if;
2122 end if;
2124 if Present (Par)
2125 and then Is_Generic_Instance (Par)
2126 and then Scope (Par) = Current_Scope
2127 and then
2128 (No (Freeze_Node (Par))
2129 or else
2130 not Is_List_Member (Freeze_Node (Par)))
2131 then
2132 Set_Has_Delayed_Freeze (Par);
2133 Append_Elmt (Par, Actuals_To_Freeze);
2134 end if;
2135 end Check_Generic_Parent;
2137 -- Start of processing for Explicit_Freeze_Check
2139 begin
2140 if Present (Renamed_Entity (Actual)) then
2141 Gen_Par :=
2142 Generic_Parent (Specification
2143 (Unit_Declaration_Node
2144 (Renamed_Entity (Actual))));
2145 else
2146 Gen_Par :=
2147 Generic_Parent (Specification
2148 (Unit_Declaration_Node (Actual)));
2149 end if;
2151 if not Expander_Active
2152 or else not Has_Completion (Actual)
2153 or else not In_Same_Source_Unit (I_Node, Actual)
2154 or else Is_Frozen (Actual)
2155 or else
2156 (Present (Renamed_Entity (Actual))
2157 and then
2158 not In_Same_Source_Unit
2159 (I_Node, (Renamed_Entity (Actual))))
2160 then
2161 null;
2163 else
2164 -- Finally we want to exclude such freeze nodes
2165 -- from statement sequences, which freeze
2166 -- everything before them.
2167 -- Is this strictly necessary ???
2169 Needs_Freezing := True;
2171 P := Parent (I_Node);
2172 while Nkind (P) /= N_Compilation_Unit loop
2173 if Nkind (P) = N_Handled_Sequence_Of_Statements
2174 then
2175 Needs_Freezing := False;
2176 exit;
2177 end if;
2179 P := Parent (P);
2180 end loop;
2182 if Needs_Freezing then
2183 Check_Generic_Parent;
2185 -- If the actual is a renaming of a proper
2186 -- instance of the formal package, indicate
2187 -- that it is the instance that must be frozen.
2189 if Nkind (Parent (Actual)) =
2190 N_Package_Renaming_Declaration
2191 then
2192 Set_Has_Delayed_Freeze
2193 (Renamed_Entity (Actual));
2194 Append_Elmt
2195 (Renamed_Entity (Actual),
2196 Actuals_To_Freeze);
2197 else
2198 Set_Has_Delayed_Freeze (Actual);
2199 Append_Elmt (Actual, Actuals_To_Freeze);
2200 end if;
2201 end if;
2202 end if;
2203 end Explicit_Freeze_Check;
2204 end if;
2206 -- For use type and use package appearing in the generic part,
2207 -- we have already copied them, so we can just move them where
2208 -- they belong (we mustn't recopy them since this would mess up
2209 -- the Sloc values).
2211 when N_Use_Package_Clause
2212 | N_Use_Type_Clause
2214 if Nkind (Original_Node (I_Node)) =
2215 N_Formal_Package_Declaration
2216 then
2217 Append (New_Copy_Tree (Formal), Assoc_List);
2218 else
2219 Remove (Formal);
2220 Append (Formal, Assoc_List);
2221 end if;
2223 when others =>
2224 raise Program_Error;
2225 end case;
2227 -- Check here the correct use of Ghost entities in generic
2228 -- instantiations, as now the generic has been resolved and
2229 -- we know which formal generic parameters are ghost (SPARK
2230 -- RM 6.9(10)).
2232 if Nkind (Formal) not in N_Use_Package_Clause
2233 | N_Use_Type_Clause
2234 then
2235 Check_Ghost_Context_In_Generic_Association
2236 (Actual => Match,
2237 Formal => Defining_Entity (Analyzed_Formal));
2238 end if;
2240 Formal := Saved_Formal;
2241 Next_Non_Pragma (Analyzed_Formal);
2242 end loop;
2244 if Num_Actuals > Num_Matched then
2245 Error_Msg_Sloc := Sloc (Gen_Unit);
2247 if Present (Selector_Name (Actual)) then
2248 Error_Msg_NE
2249 ("unmatched actual &", Actual, Selector_Name (Actual));
2250 Error_Msg_NE
2251 ("\in instantiation of & declared#", Actual, Gen_Unit);
2252 else
2253 Error_Msg_NE
2254 ("unmatched actual in instantiation of & declared#",
2255 Actual, Gen_Unit);
2256 end if;
2257 end if;
2259 elsif Present (Actuals) then
2260 Error_Msg_N
2261 ("too many actuals in generic instantiation", Instantiation_Node);
2262 end if;
2264 -- An instantiation freezes all generic actuals. The only exceptions
2265 -- to this are incomplete types and subprograms which are not fully
2266 -- defined at the point of instantiation.
2268 declare
2269 Elmt : Elmt_Id := First_Elmt (Actuals_To_Freeze);
2270 begin
2271 while Present (Elmt) loop
2272 Freeze_Before (I_Node, Node (Elmt));
2273 Next_Elmt (Elmt);
2274 end loop;
2275 end;
2277 -- If there are default subprograms, normalize the tree by adding
2278 -- explicit associations for them. This is required if the instance
2279 -- appears within a generic.
2281 if not Is_Empty_List (Default_Actuals) then
2282 declare
2283 Default : Node_Id;
2285 begin
2286 Default := First (Default_Actuals);
2287 while Present (Default) loop
2288 Mark_Rewrite_Insertion (Default);
2289 Next (Default);
2290 end loop;
2292 if No (Actuals) then
2293 Set_Generic_Associations (I_Node, Default_Actuals);
2294 else
2295 Append_List_To (Actuals, Default_Actuals);
2296 end if;
2297 end;
2298 end if;
2300 -- If this is a formal package, normalize the parameter list by adding
2301 -- explicit box associations for the formals that are covered by an
2302 -- Others_Choice.
2304 Append_List (Default_Formals, Formals);
2306 return Assoc_List;
2307 end Analyze_Associations;
2309 -------------------------------
2310 -- Analyze_Formal_Array_Type --
2311 -------------------------------
2313 procedure Analyze_Formal_Array_Type
2314 (T : in out Entity_Id;
2315 Def : Node_Id)
2317 DSS : Node_Id;
2319 begin
2320 -- Treated like a non-generic array declaration, with additional
2321 -- semantic checks.
2323 Enter_Name (T);
2325 if Nkind (Def) = N_Constrained_Array_Definition then
2326 DSS := First (Discrete_Subtype_Definitions (Def));
2327 while Present (DSS) loop
2328 if Nkind (DSS) in N_Subtype_Indication
2329 | N_Range
2330 | N_Attribute_Reference
2331 then
2332 Error_Msg_N ("only a subtype mark is allowed in a formal", DSS);
2333 end if;
2335 Next (DSS);
2336 end loop;
2337 end if;
2339 Array_Type_Declaration (T, Def);
2340 Set_Is_Generic_Type (Base_Type (T));
2342 if Ekind (Component_Type (T)) = E_Incomplete_Type
2343 and then No (Full_View (Component_Type (T)))
2344 then
2345 Error_Msg_N ("premature usage of incomplete type", Def);
2347 -- Check that range constraint is not allowed on the component type
2348 -- of a generic formal array type (AARM 12.5.3(3))
2350 elsif Is_Internal (Component_Type (T))
2351 and then Present (Subtype_Indication (Component_Definition (Def)))
2352 and then Nkind (Original_Node
2353 (Subtype_Indication (Component_Definition (Def)))) =
2354 N_Subtype_Indication
2355 then
2356 Error_Msg_N
2357 ("in a formal, a subtype indication can only be "
2358 & "a subtype mark (RM 12.5.3(3))",
2359 Subtype_Indication (Component_Definition (Def)));
2360 end if;
2362 end Analyze_Formal_Array_Type;
2364 ---------------------------------------------
2365 -- Analyze_Formal_Decimal_Fixed_Point_Type --
2366 ---------------------------------------------
2368 -- As for other generic types, we create a valid type representation with
2369 -- legal but arbitrary attributes, whose values are never considered
2370 -- static. For all scalar types we introduce an anonymous base type, with
2371 -- the same attributes. We choose the corresponding integer type to be
2372 -- Standard_Integer.
2373 -- Here and in other similar routines, the Sloc of the generated internal
2374 -- type must be the same as the sloc of the defining identifier of the
2375 -- formal type declaration, to provide proper source navigation.
2377 procedure Analyze_Formal_Decimal_Fixed_Point_Type
2378 (T : Entity_Id;
2379 Def : Node_Id)
2381 Loc : constant Source_Ptr := Sloc (Def);
2383 Base : constant Entity_Id :=
2384 New_Internal_Entity
2385 (E_Decimal_Fixed_Point_Type,
2386 Current_Scope,
2387 Sloc (Defining_Identifier (Parent (Def))), 'G');
2389 Int_Base : constant Entity_Id := Standard_Integer;
2390 Delta_Val : constant Ureal := Ureal_1;
2391 Digs_Val : constant Uint := Uint_6;
2393 function Make_Dummy_Bound return Node_Id;
2394 -- Return a properly typed universal real literal to use as a bound
2396 ----------------------
2397 -- Make_Dummy_Bound --
2398 ----------------------
2400 function Make_Dummy_Bound return Node_Id is
2401 Bound : constant Node_Id := Make_Real_Literal (Loc, Ureal_1);
2402 begin
2403 Set_Etype (Bound, Universal_Real);
2404 return Bound;
2405 end Make_Dummy_Bound;
2407 -- Start of processing for Analyze_Formal_Decimal_Fixed_Point_Type
2409 begin
2410 Enter_Name (T);
2412 Set_Etype (Base, Base);
2413 Set_Size_Info (Base, Int_Base);
2414 Set_RM_Size (Base, RM_Size (Int_Base));
2415 Set_First_Rep_Item (Base, First_Rep_Item (Int_Base));
2416 Set_Digits_Value (Base, Digs_Val);
2417 Set_Delta_Value (Base, Delta_Val);
2418 Set_Small_Value (Base, Delta_Val);
2419 Set_Scalar_Range (Base,
2420 Make_Range (Loc,
2421 Low_Bound => Make_Dummy_Bound,
2422 High_Bound => Make_Dummy_Bound));
2424 Set_Is_Generic_Type (Base);
2425 Set_Parent (Base, Parent (Def));
2427 Mutate_Ekind (T, E_Decimal_Fixed_Point_Subtype);
2428 Set_Etype (T, Base);
2429 Set_Size_Info (T, Int_Base);
2430 Set_RM_Size (T, RM_Size (Int_Base));
2431 Set_First_Rep_Item (T, First_Rep_Item (Int_Base));
2432 Set_Digits_Value (T, Digs_Val);
2433 Set_Delta_Value (T, Delta_Val);
2434 Set_Small_Value (T, Delta_Val);
2435 Set_Scalar_Range (T, Scalar_Range (Base));
2436 Set_Is_Constrained (T);
2438 Check_Restriction (No_Fixed_Point, Def);
2439 end Analyze_Formal_Decimal_Fixed_Point_Type;
2441 -------------------------------------------
2442 -- Analyze_Formal_Derived_Interface_Type --
2443 -------------------------------------------
2445 procedure Analyze_Formal_Derived_Interface_Type
2446 (N : Node_Id;
2447 T : Entity_Id;
2448 Def : Node_Id)
2450 Loc : constant Source_Ptr := Sloc (Def);
2452 begin
2453 -- Rewrite as a type declaration of a derived type. This ensures that
2454 -- the interface list and primitive operations are properly captured.
2456 Rewrite (N,
2457 Make_Full_Type_Declaration (Loc,
2458 Defining_Identifier => T,
2459 Type_Definition => Def));
2460 Analyze (N);
2461 Set_Is_Generic_Type (T);
2462 end Analyze_Formal_Derived_Interface_Type;
2464 ---------------------------------
2465 -- Analyze_Formal_Derived_Type --
2466 ---------------------------------
2468 procedure Analyze_Formal_Derived_Type
2469 (N : Node_Id;
2470 T : Entity_Id;
2471 Def : Node_Id)
2473 Loc : constant Source_Ptr := Sloc (Def);
2474 Unk_Disc : constant Boolean := Unknown_Discriminants_Present (N);
2475 New_N : Node_Id;
2477 begin
2478 Set_Is_Generic_Type (T);
2480 if Private_Present (Def) then
2481 New_N :=
2482 Make_Private_Extension_Declaration (Loc,
2483 Defining_Identifier => T,
2484 Discriminant_Specifications => Discriminant_Specifications (N),
2485 Unknown_Discriminants_Present => Unk_Disc,
2486 Subtype_Indication => Subtype_Mark (Def),
2487 Interface_List => Interface_List (Def));
2489 Set_Abstract_Present (New_N, Abstract_Present (Def));
2490 Set_Limited_Present (New_N, Limited_Present (Def));
2491 Set_Synchronized_Present (New_N, Synchronized_Present (Def));
2493 else
2494 New_N :=
2495 Make_Full_Type_Declaration (Loc,
2496 Defining_Identifier => T,
2497 Discriminant_Specifications =>
2498 Discriminant_Specifications (Parent (T)),
2499 Type_Definition =>
2500 Make_Derived_Type_Definition (Loc,
2501 Subtype_Indication => Subtype_Mark (Def)));
2503 Set_Abstract_Present
2504 (Type_Definition (New_N), Abstract_Present (Def));
2505 Set_Limited_Present
2506 (Type_Definition (New_N), Limited_Present (Def));
2507 end if;
2509 Rewrite (N, New_N);
2510 Analyze (N);
2512 if Unk_Disc then
2513 if not Is_Composite_Type (T) then
2514 Error_Msg_N
2515 ("unknown discriminants not allowed for elementary types", N);
2516 else
2517 Set_Has_Unknown_Discriminants (T);
2518 Set_Is_Constrained (T, False);
2519 end if;
2520 end if;
2522 -- If the parent type has a known size, so does the formal, which makes
2523 -- legal representation clauses that involve the formal.
2525 Set_Size_Known_At_Compile_Time
2526 (T, Size_Known_At_Compile_Time (Entity (Subtype_Mark (Def))));
2527 end Analyze_Formal_Derived_Type;
2529 ----------------------------------
2530 -- Analyze_Formal_Discrete_Type --
2531 ----------------------------------
2533 -- The operations defined for a discrete types are those of an enumeration
2534 -- type. The size is set to an arbitrary value, for use in analyzing the
2535 -- generic unit.
2537 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id) is
2538 Loc : constant Source_Ptr := Sloc (Def);
2539 Lo : Node_Id;
2540 Hi : Node_Id;
2542 Base : constant Entity_Id :=
2543 New_Internal_Entity
2544 (E_Floating_Point_Type, Current_Scope,
2545 Sloc (Defining_Identifier (Parent (Def))), 'G');
2547 begin
2548 Enter_Name (T);
2549 Mutate_Ekind (T, E_Enumeration_Subtype);
2550 Set_Etype (T, Base);
2551 Init_Size (T, 8);
2552 Reinit_Alignment (T);
2553 Set_Is_Generic_Type (T);
2554 Set_Is_Constrained (T);
2556 -- For semantic analysis, the bounds of the type must be set to some
2557 -- non-static value. The simplest is to create attribute nodes for those
2558 -- bounds, that refer to the type itself. These bounds are never
2559 -- analyzed but serve as place-holders.
2561 Lo :=
2562 Make_Attribute_Reference (Loc,
2563 Attribute_Name => Name_First,
2564 Prefix => New_Occurrence_Of (T, Loc));
2565 Set_Etype (Lo, T);
2567 Hi :=
2568 Make_Attribute_Reference (Loc,
2569 Attribute_Name => Name_Last,
2570 Prefix => New_Occurrence_Of (T, Loc));
2571 Set_Etype (Hi, T);
2573 Set_Scalar_Range (T,
2574 Make_Range (Loc,
2575 Low_Bound => Lo,
2576 High_Bound => Hi));
2578 Mutate_Ekind (Base, E_Enumeration_Type);
2579 Set_Etype (Base, Base);
2580 Init_Size (Base, 8);
2581 Reinit_Alignment (Base);
2582 Set_Is_Generic_Type (Base);
2583 Set_Scalar_Range (Base, Scalar_Range (T));
2584 Set_Parent (Base, Parent (Def));
2585 end Analyze_Formal_Discrete_Type;
2587 ----------------------------------
2588 -- Analyze_Formal_Floating_Type --
2589 ---------------------------------
2591 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id) is
2592 Base : constant Entity_Id :=
2593 New_Internal_Entity
2594 (E_Floating_Point_Type, Current_Scope,
2595 Sloc (Defining_Identifier (Parent (Def))), 'G');
2597 begin
2598 -- The various semantic attributes are taken from the predefined type
2599 -- Float, just so that all of them are initialized. Their values are
2600 -- never used because no constant folding or expansion takes place in
2601 -- the generic itself.
2603 Enter_Name (T);
2604 Mutate_Ekind (T, E_Floating_Point_Subtype);
2605 Set_Etype (T, Base);
2606 Set_Size_Info (T, (Standard_Float));
2607 Set_RM_Size (T, RM_Size (Standard_Float));
2608 Set_Digits_Value (T, Digits_Value (Standard_Float));
2609 Set_Scalar_Range (T, Scalar_Range (Standard_Float));
2610 Set_Is_Constrained (T);
2612 Set_Is_Generic_Type (Base);
2613 Set_Etype (Base, Base);
2614 Set_Size_Info (Base, (Standard_Float));
2615 Set_RM_Size (Base, RM_Size (Standard_Float));
2616 Set_Digits_Value (Base, Digits_Value (Standard_Float));
2617 Set_Scalar_Range (Base, Scalar_Range (Standard_Float));
2618 Set_Parent (Base, Parent (Def));
2620 Check_Restriction (No_Floating_Point, Def);
2621 end Analyze_Formal_Floating_Type;
2623 -----------------------------------
2624 -- Analyze_Formal_Interface_Type;--
2625 -----------------------------------
2627 procedure Analyze_Formal_Interface_Type
2628 (N : Node_Id;
2629 T : Entity_Id;
2630 Def : Node_Id)
2632 Loc : constant Source_Ptr := Sloc (N);
2633 New_N : Node_Id;
2635 begin
2636 New_N :=
2637 Make_Full_Type_Declaration (Loc,
2638 Defining_Identifier => T,
2639 Type_Definition => Def);
2641 Rewrite (N, New_N);
2642 Analyze (N);
2643 Set_Is_Generic_Type (T);
2644 end Analyze_Formal_Interface_Type;
2646 ---------------------------------
2647 -- Analyze_Formal_Modular_Type --
2648 ---------------------------------
2650 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id) is
2651 begin
2652 -- Apart from their entity kind, generic modular types are treated like
2653 -- signed integer types, and have the same attributes.
2655 Analyze_Formal_Signed_Integer_Type (T, Def);
2656 Mutate_Ekind (T, E_Modular_Integer_Subtype);
2657 Mutate_Ekind (Etype (T), E_Modular_Integer_Type);
2659 end Analyze_Formal_Modular_Type;
2661 ---------------------------------------
2662 -- Analyze_Formal_Object_Declaration --
2663 ---------------------------------------
2665 procedure Analyze_Formal_Object_Declaration (N : Node_Id) is
2666 E : constant Node_Id := Default_Expression (N);
2667 Id : constant Node_Id := Defining_Identifier (N);
2669 K : Entity_Kind;
2670 Parent_Installed : Boolean := False;
2671 T : Node_Id;
2673 begin
2674 Enter_Name (Id);
2676 Check_Abbreviated_Instance (Parent (N), Parent_Installed);
2678 -- Determine the mode of the formal object
2680 if Out_Present (N) then
2681 K := E_Generic_In_Out_Parameter;
2683 if not In_Present (N) then
2684 Error_Msg_N ("formal generic objects cannot have mode OUT", N);
2685 end if;
2687 else
2688 K := E_Generic_In_Parameter;
2689 end if;
2691 if Present (Subtype_Mark (N)) then
2692 Find_Type (Subtype_Mark (N));
2693 T := Entity (Subtype_Mark (N));
2695 -- Verify that there is no redundant null exclusion
2697 if Null_Exclusion_Present (N) then
2698 if not Is_Access_Type (T) then
2699 Error_Msg_N
2700 ("null exclusion can only apply to an access type", N);
2702 elsif Can_Never_Be_Null (T) then
2703 Error_Msg_NE
2704 ("`NOT NULL` not allowed (& already excludes null)", N, T);
2705 end if;
2706 end if;
2708 -- Ada 2005 (AI-423): Formal object with an access definition
2710 else
2711 Check_Access_Definition (N);
2712 T := Access_Definition
2713 (Related_Nod => N,
2714 N => Access_Definition (N));
2715 end if;
2717 if Ekind (T) = E_Incomplete_Type then
2718 declare
2719 Error_Node : Node_Id;
2721 begin
2722 if Present (Subtype_Mark (N)) then
2723 Error_Node := Subtype_Mark (N);
2724 else
2725 Check_Access_Definition (N);
2726 Error_Node := Access_Definition (N);
2727 end if;
2729 Error_Msg_N ("premature usage of incomplete type", Error_Node);
2730 end;
2731 end if;
2733 if K = E_Generic_In_Parameter then
2735 -- Ada 2005 (AI-287): Limited aggregates allowed in generic formals
2737 if Ada_Version < Ada_2005 and then Is_Limited_Type (T) then
2738 Error_Msg_N
2739 ("generic formal of mode IN must not be of limited type", N);
2740 Explain_Limited_Type (T, N);
2741 end if;
2743 if Is_Abstract_Type (T) then
2744 Error_Msg_N
2745 ("generic formal of mode IN must not be of abstract type", N);
2746 end if;
2748 if Present (E) then
2749 Preanalyze_Spec_Expression (E, T);
2751 -- The default for a ghost generic formal IN parameter of
2752 -- access-to-variable type should be a ghost object (SPARK
2753 -- RM 6.9(13)).
2755 if Is_Access_Variable (T) then
2756 Check_Ghost_Formal_Variable
2757 (Actual => E,
2758 Formal => Id,
2759 Is_Default => True);
2760 end if;
2762 if Is_Limited_Type (T) and then not OK_For_Limited_Init (T, E) then
2763 Error_Msg_N
2764 ("initialization not allowed for limited types", E);
2765 Explain_Limited_Type (T, E);
2766 end if;
2767 end if;
2769 Mutate_Ekind (Id, K);
2770 Set_Etype (Id, T);
2772 -- Case of generic IN OUT parameter
2774 else
2775 -- If the formal has an unconstrained type, construct its actual
2776 -- subtype, as is done for subprogram formals. In this fashion, all
2777 -- its uses can refer to specific bounds.
2779 Mutate_Ekind (Id, K);
2780 Set_Etype (Id, T);
2782 if (Is_Array_Type (T) and then not Is_Constrained (T))
2783 or else (Ekind (T) = E_Record_Type and then Has_Discriminants (T))
2784 then
2785 declare
2786 Non_Freezing_Ref : constant Node_Id :=
2787 New_Occurrence_Of (Id, Sloc (Id));
2788 Decl : Node_Id;
2790 begin
2791 -- Make sure the actual subtype doesn't generate bogus freezing
2793 Set_Must_Not_Freeze (Non_Freezing_Ref);
2794 Decl := Build_Actual_Subtype (T, Non_Freezing_Ref);
2795 Insert_Before_And_Analyze (N, Decl);
2796 Set_Actual_Subtype (Id, Defining_Identifier (Decl));
2797 end;
2798 else
2799 Set_Actual_Subtype (Id, T);
2800 end if;
2802 if Present (E) then
2803 Error_Msg_N
2804 ("initialization not allowed for `IN OUT` formals", N);
2805 end if;
2806 end if;
2808 if Has_Aspects (N) then
2809 Analyze_Aspect_Specifications (N, Id);
2810 end if;
2812 if Parent_Installed then
2813 Remove_Parent;
2814 end if;
2815 end Analyze_Formal_Object_Declaration;
2817 ----------------------------------------------
2818 -- Analyze_Formal_Ordinary_Fixed_Point_Type --
2819 ----------------------------------------------
2821 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
2822 (T : Entity_Id;
2823 Def : Node_Id)
2825 Loc : constant Source_Ptr := Sloc (Def);
2826 Base : constant Entity_Id :=
2827 New_Internal_Entity
2828 (E_Ordinary_Fixed_Point_Type, Current_Scope,
2829 Sloc (Defining_Identifier (Parent (Def))), 'G');
2831 begin
2832 -- The semantic attributes are set for completeness only, their values
2833 -- will never be used, since all properties of the type are non-static.
2835 Enter_Name (T);
2836 Mutate_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
2837 Set_Etype (T, Base);
2838 Set_Size_Info (T, Standard_Integer);
2839 Set_RM_Size (T, RM_Size (Standard_Integer));
2840 Set_Small_Value (T, Ureal_1);
2841 Set_Delta_Value (T, Ureal_1);
2842 Set_Scalar_Range (T,
2843 Make_Range (Loc,
2844 Low_Bound => Make_Real_Literal (Loc, Ureal_1),
2845 High_Bound => Make_Real_Literal (Loc, Ureal_1)));
2846 Set_Is_Constrained (T);
2848 Set_Is_Generic_Type (Base);
2849 Set_Etype (Base, Base);
2850 Set_Size_Info (Base, Standard_Integer);
2851 Set_RM_Size (Base, RM_Size (Standard_Integer));
2852 Set_Small_Value (Base, Ureal_1);
2853 Set_Delta_Value (Base, Ureal_1);
2854 Set_Scalar_Range (Base, Scalar_Range (T));
2855 Set_Parent (Base, Parent (Def));
2857 Check_Restriction (No_Fixed_Point, Def);
2858 end Analyze_Formal_Ordinary_Fixed_Point_Type;
2860 ----------------------------------------
2861 -- Analyze_Formal_Package_Declaration --
2862 ----------------------------------------
2864 procedure Analyze_Formal_Package_Declaration (N : Node_Id) is
2865 Gen_Id : constant Node_Id := Name (N);
2866 Loc : constant Source_Ptr := Sloc (N);
2867 Pack_Id : constant Entity_Id := Defining_Identifier (N);
2868 Formal : Entity_Id;
2869 Gen_Decl : Node_Id;
2870 Gen_Unit : Entity_Id;
2871 Renaming : Node_Id;
2873 Vis_Prims_List : Elist_Id := No_Elist;
2874 -- List of primitives made temporarily visible in the instantiation
2875 -- to match the visibility of the formal type.
2877 function Build_Local_Package return Node_Id;
2878 -- The formal package is rewritten so that its parameters are replaced
2879 -- with corresponding declarations. For parameters with bona fide
2880 -- associations these declarations are created by Analyze_Associations
2881 -- as for a regular instantiation. For boxed parameters, we preserve
2882 -- the formal declarations and analyze them, in order to introduce
2883 -- entities of the right kind in the environment of the formal.
2885 -------------------------
2886 -- Build_Local_Package --
2887 -------------------------
2889 function Build_Local_Package return Node_Id is
2890 Decls : List_Id;
2891 Pack_Decl : Node_Id;
2893 begin
2894 -- Within the formal, the name of the generic package is a renaming
2895 -- of the formal (as for a regular instantiation).
2897 Pack_Decl :=
2898 Make_Package_Declaration (Loc,
2899 Specification =>
2900 Copy_Generic_Node
2901 (Specification (Original_Node (Gen_Decl)),
2902 Empty, Instantiating => True));
2904 Renaming :=
2905 Make_Package_Renaming_Declaration (Loc,
2906 Defining_Unit_Name =>
2907 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
2908 Name => New_Occurrence_Of (Formal, Loc));
2910 if Nkind (Gen_Id) = N_Identifier
2911 and then Chars (Gen_Id) = Chars (Pack_Id)
2912 then
2913 Error_Msg_NE
2914 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
2915 end if;
2917 -- If the formal is declared with a box, or with an others choice,
2918 -- create corresponding declarations for all entities in the formal
2919 -- part, so that names with the proper types are available in the
2920 -- specification of the formal package.
2922 -- On the other hand, if there are no associations, then all the
2923 -- formals must have defaults, and this will be checked by the
2924 -- call to Analyze_Associations.
2926 if Box_Present (N)
2927 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2928 then
2929 declare
2930 Formal_Decl : Node_Id;
2932 begin
2933 -- TBA : for a formal package, need to recurse ???
2935 Decls := New_List;
2936 Formal_Decl :=
2937 First
2938 (Generic_Formal_Declarations (Original_Node (Gen_Decl)));
2939 while Present (Formal_Decl) loop
2940 Append_To
2941 (Decls,
2942 Copy_Generic_Node
2943 (Formal_Decl, Empty, Instantiating => True));
2944 Next (Formal_Decl);
2945 end loop;
2946 end;
2948 -- If generic associations are present, use Analyze_Associations to
2949 -- create the proper renaming declarations.
2951 else
2952 declare
2953 Act_Tree : constant Node_Id :=
2954 Copy_Generic_Node
2955 (Original_Node (Gen_Decl), Empty,
2956 Instantiating => True);
2958 begin
2959 Generic_Renamings.Set_Last (0);
2960 Generic_Renamings_HTable.Reset;
2961 Instantiation_Node := N;
2963 Decls :=
2964 Analyze_Associations
2965 (I_Node => Original_Node (N),
2966 Formals => Generic_Formal_Declarations (Act_Tree),
2967 F_Copy => Generic_Formal_Declarations (Gen_Decl));
2969 Vis_Prims_List := Check_Hidden_Primitives (Decls);
2970 end;
2971 end if;
2973 Append (Renaming, To => Decls);
2975 -- Add generated declarations ahead of local declarations in
2976 -- the package.
2978 if No (Visible_Declarations (Specification (Pack_Decl))) then
2979 Set_Visible_Declarations (Specification (Pack_Decl), Decls);
2980 else
2981 Insert_List_Before
2982 (First (Visible_Declarations (Specification (Pack_Decl))),
2983 Decls);
2984 end if;
2986 return Pack_Decl;
2987 end Build_Local_Package;
2989 -- Local variables
2991 Save_ISMP : constant Boolean := Ignore_SPARK_Mode_Pragmas_In_Instance;
2992 -- Save flag Ignore_SPARK_Mode_Pragmas_In_Instance for restore on exit
2994 Associations : Boolean := True;
2995 New_N : Node_Id;
2996 Parent_Installed : Boolean := False;
2997 Parent_Instance : Entity_Id;
2998 Renaming_In_Par : Entity_Id;
3000 -- Start of processing for Analyze_Formal_Package_Declaration
3002 begin
3003 Check_Text_IO_Special_Unit (Gen_Id);
3005 Init_Env;
3006 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
3007 Gen_Unit := Entity (Gen_Id);
3009 -- Check for a formal package that is a package renaming
3011 if Present (Renamed_Entity (Gen_Unit)) then
3013 -- Indicate that unit is used, before replacing it with renamed
3014 -- entity for use below.
3016 if In_Extended_Main_Source_Unit (N) then
3017 Set_Is_Instantiated (Gen_Unit);
3018 Generate_Reference (Gen_Unit, N);
3019 end if;
3021 Gen_Unit := Renamed_Entity (Gen_Unit);
3022 end if;
3024 if Ekind (Gen_Unit) /= E_Generic_Package then
3025 Error_Msg_N ("expect generic package name", Gen_Id);
3026 Restore_Env;
3027 goto Leave;
3029 elsif Gen_Unit = Current_Scope then
3030 Error_Msg_N
3031 ("generic package cannot be used as a formal package of itself",
3032 Gen_Id);
3033 Restore_Env;
3034 goto Leave;
3036 elsif In_Open_Scopes (Gen_Unit) then
3037 if Is_Compilation_Unit (Gen_Unit)
3038 and then Is_Child_Unit (Current_Scope)
3039 then
3040 -- Special-case the error when the formal is a parent, and
3041 -- continue analysis to minimize cascaded errors.
3043 Error_Msg_N
3044 ("generic parent cannot be used as formal package of a child "
3045 & "unit", Gen_Id);
3047 else
3048 Error_Msg_N
3049 ("generic package cannot be used as a formal package within "
3050 & "itself", Gen_Id);
3051 Restore_Env;
3052 goto Leave;
3053 end if;
3054 end if;
3056 -- Check that name of formal package does not hide name of generic,
3057 -- or its leading prefix. This check must be done separately because
3058 -- the name of the generic has already been analyzed.
3060 declare
3061 Gen_Name : Entity_Id;
3063 begin
3064 Gen_Name := Gen_Id;
3065 while Nkind (Gen_Name) = N_Expanded_Name loop
3066 Gen_Name := Prefix (Gen_Name);
3067 end loop;
3069 if Chars (Gen_Name) = Chars (Pack_Id) then
3070 Error_Msg_NE
3071 ("& is hidden within declaration of formal package",
3072 Gen_Id, Gen_Name);
3073 end if;
3074 end;
3076 if Box_Present (N)
3077 or else No (Generic_Associations (N))
3078 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
3079 then
3080 Associations := False;
3081 end if;
3083 -- If there are no generic associations, the generic parameters appear
3084 -- as local entities and are instantiated like them. We copy the generic
3085 -- package declaration as if it were an instantiation, and analyze it
3086 -- like a regular package, except that we treat the formals as
3087 -- additional visible components.
3089 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
3091 if In_Extended_Main_Source_Unit (N) then
3092 Set_Is_Instantiated (Gen_Unit);
3093 Generate_Reference (Gen_Unit, N);
3094 end if;
3096 Formal := New_Copy (Pack_Id);
3097 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
3099 -- Make local generic without formals. The formals will be replaced with
3100 -- internal declarations.
3102 begin
3103 New_N := Build_Local_Package;
3105 -- If there are errors in the parameter list, Analyze_Associations
3106 -- raises Instantiation_Error. Patch the declaration to prevent further
3107 -- exception propagation.
3109 exception
3110 when Instantiation_Error =>
3111 Enter_Name (Formal);
3112 Mutate_Ekind (Formal, E_Variable);
3113 Set_Etype (Formal, Any_Type);
3114 Restore_Hidden_Primitives (Vis_Prims_List);
3116 if Parent_Installed then
3117 Remove_Parent;
3118 end if;
3120 goto Leave;
3121 end;
3123 Rewrite (N, New_N);
3124 Set_Defining_Unit_Name (Specification (New_N), Formal);
3125 Set_Generic_Parent (Specification (N), Gen_Unit);
3126 Set_Instance_Env (Gen_Unit, Formal);
3127 Set_Is_Generic_Instance (Formal);
3129 Enter_Name (Formal);
3130 Mutate_Ekind (Formal, E_Package);
3131 Set_Etype (Formal, Standard_Void_Type);
3132 Set_Inner_Instances (Formal, New_Elmt_List);
3134 -- It is unclear that any aspects can apply to a formal package
3135 -- declaration, given that they look like a hidden conformance
3136 -- requirement on the corresponding actual. However, Abstract_State
3137 -- must be treated specially because it generates declarations that
3138 -- must appear before other declarations in the specification and
3139 -- must be analyzed at once.
3141 if Present (Aspect_Specifications (Gen_Decl)) then
3142 if No (Aspect_Specifications (N)) then
3143 Set_Aspect_Specifications (N, New_List);
3144 end if;
3146 declare
3147 ASN : Node_Id := First (Aspect_Specifications (Gen_Decl));
3148 New_A : Node_Id;
3150 begin
3151 while Present (ASN) loop
3152 if Get_Aspect_Id (ASN) = Aspect_Abstract_State then
3153 New_A :=
3154 Copy_Generic_Node (ASN, Empty, Instantiating => True);
3155 Set_Entity (New_A, Formal);
3156 Set_Analyzed (New_A, False);
3157 Append (New_A, Aspect_Specifications (N));
3158 Analyze_Aspect_Specifications (N, Formal);
3159 exit;
3160 end if;
3162 Next (ASN);
3163 end loop;
3164 end;
3165 end if;
3167 Push_Scope (Formal);
3169 -- Manually set the SPARK_Mode from the context because the package
3170 -- declaration is never analyzed.
3172 Set_SPARK_Pragma (Formal, SPARK_Mode_Pragma);
3173 Set_SPARK_Aux_Pragma (Formal, SPARK_Mode_Pragma);
3174 Set_SPARK_Pragma_Inherited (Formal);
3175 Set_SPARK_Aux_Pragma_Inherited (Formal);
3177 if Is_Child_Unit (Gen_Unit) and then Parent_Installed then
3179 -- Similarly, we have to make the name of the formal visible in the
3180 -- parent instance, to resolve properly fully qualified names that
3181 -- may appear in the generic unit. The parent instance has been
3182 -- placed on the scope stack ahead of the current scope.
3184 Parent_Instance := Scope_Stack.Table (Scope_Stack.Last - 1).Entity;
3186 Renaming_In_Par :=
3187 Make_Defining_Identifier (Loc, Chars (Gen_Unit));
3188 Mutate_Ekind (Renaming_In_Par, E_Package);
3189 Set_Etype (Renaming_In_Par, Standard_Void_Type);
3190 Set_Scope (Renaming_In_Par, Parent_Instance);
3191 Set_Parent (Renaming_In_Par, Parent (Formal));
3192 Set_Renamed_Entity (Renaming_In_Par, Formal);
3193 Append_Entity (Renaming_In_Par, Parent_Instance);
3194 end if;
3196 -- A formal package declaration behaves as a package instantiation with
3197 -- respect to SPARK_Mode "off". If the annotation is "off" or altogether
3198 -- missing, set the global flag which signals Analyze_Pragma to ingnore
3199 -- all SPARK_Mode pragmas within the generic_package_name.
3201 if SPARK_Mode /= On then
3202 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
3204 -- Mark the formal spec in case the body is instantiated at a later
3205 -- pass. This preserves the original context in effect for the body.
3207 Set_Ignore_SPARK_Mode_Pragmas (Formal);
3208 end if;
3210 Analyze (Specification (N));
3212 -- The formals for which associations are provided are not visible
3213 -- outside of the formal package. The others are still declared by a
3214 -- formal parameter declaration.
3216 -- If there are no associations, the only local entity to hide is the
3217 -- generated package renaming itself.
3219 declare
3220 E : Entity_Id;
3222 begin
3223 E := First_Entity (Formal);
3224 while Present (E) loop
3225 if Associations and then not Is_Generic_Formal (E) then
3226 Set_Is_Hidden (E);
3227 end if;
3229 if Ekind (E) = E_Package and then Renamed_Entity (E) = Formal then
3230 Set_Is_Hidden (E);
3231 exit;
3232 end if;
3234 Next_Entity (E);
3235 end loop;
3236 end;
3238 End_Package_Scope (Formal);
3239 Restore_Hidden_Primitives (Vis_Prims_List);
3241 if Parent_Installed then
3242 Remove_Parent;
3243 end if;
3245 Restore_Env;
3247 -- Inside the generic unit, the formal package is a regular package, but
3248 -- no body is needed for it. Note that after instantiation, the defining
3249 -- unit name we need is in the new tree and not in the original (see
3250 -- Package_Instantiation). A generic formal package is an instance, and
3251 -- can be used as an actual for an inner instance.
3253 Set_Has_Completion (Formal, True);
3255 -- Add semantic information to the original defining identifier.
3257 Mutate_Ekind (Pack_Id, E_Package);
3258 Set_Etype (Pack_Id, Standard_Void_Type);
3259 Set_Scope (Pack_Id, Scope (Formal));
3260 Set_Has_Completion (Pack_Id, True);
3262 <<Leave>>
3263 if Has_Aspects (N) then
3264 -- Unclear that any other aspects may appear here, analyze them
3265 -- for completion, given that the grammar allows their appearance.
3267 Analyze_Aspect_Specifications (N, Pack_Id);
3268 end if;
3270 Ignore_SPARK_Mode_Pragmas_In_Instance := Save_ISMP;
3271 end Analyze_Formal_Package_Declaration;
3273 ---------------------------------
3274 -- Analyze_Formal_Private_Type --
3275 ---------------------------------
3277 procedure Analyze_Formal_Private_Type
3278 (N : Node_Id;
3279 T : Entity_Id;
3280 Def : Node_Id)
3282 begin
3283 New_Private_Type (N, T, Def);
3285 -- Set the size to an arbitrary but legal value
3287 Set_Size_Info (T, Standard_Integer);
3288 Set_RM_Size (T, RM_Size (Standard_Integer));
3289 end Analyze_Formal_Private_Type;
3291 ------------------------------------
3292 -- Analyze_Formal_Incomplete_Type --
3293 ------------------------------------
3295 procedure Analyze_Formal_Incomplete_Type
3296 (T : Entity_Id;
3297 Def : Node_Id)
3299 begin
3300 Enter_Name (T);
3301 Mutate_Ekind (T, E_Incomplete_Type);
3302 Set_Etype (T, T);
3303 Set_Private_Dependents (T, New_Elmt_List);
3305 if Tagged_Present (Def) then
3306 Set_Is_Tagged_Type (T);
3307 Make_Class_Wide_Type (T);
3308 Set_Direct_Primitive_Operations (T, New_Elmt_List);
3309 end if;
3310 end Analyze_Formal_Incomplete_Type;
3312 ----------------------------------------
3313 -- Analyze_Formal_Signed_Integer_Type --
3314 ----------------------------------------
3316 procedure Analyze_Formal_Signed_Integer_Type
3317 (T : Entity_Id;
3318 Def : Node_Id)
3320 Base : constant Entity_Id :=
3321 New_Internal_Entity
3322 (E_Signed_Integer_Type,
3323 Current_Scope,
3324 Sloc (Defining_Identifier (Parent (Def))), 'G');
3326 begin
3327 Enter_Name (T);
3329 Mutate_Ekind (T, E_Signed_Integer_Subtype);
3330 Set_Etype (T, Base);
3331 Set_Size_Info (T, Standard_Integer);
3332 Set_RM_Size (T, RM_Size (Standard_Integer));
3333 Set_Scalar_Range (T, Scalar_Range (Standard_Integer));
3334 Set_Is_Constrained (T);
3336 Set_Is_Generic_Type (Base);
3337 Set_Size_Info (Base, Standard_Integer);
3338 Set_RM_Size (Base, RM_Size (Standard_Integer));
3339 Set_Etype (Base, Base);
3340 Set_Scalar_Range (Base, Scalar_Range (Standard_Integer));
3341 Set_Parent (Base, Parent (Def));
3342 end Analyze_Formal_Signed_Integer_Type;
3344 -------------------------------------------
3345 -- Analyze_Formal_Subprogram_Declaration --
3346 -------------------------------------------
3348 procedure Analyze_Formal_Subprogram_Declaration (N : Node_Id) is
3349 Spec : constant Node_Id := Specification (N);
3350 Def : constant Node_Id := Default_Name (N);
3351 Expr : constant Node_Id := Expression (N);
3352 Nam : constant Entity_Id := Defining_Unit_Name (Spec);
3354 Parent_Installed : Boolean := False;
3355 Subp : Entity_Id;
3357 begin
3358 if Nam = Error then
3359 return;
3360 end if;
3362 if Nkind (Nam) = N_Defining_Program_Unit_Name then
3363 Error_Msg_N ("name of formal subprogram must be a direct name", Nam);
3364 goto Leave;
3365 end if;
3367 Check_Abbreviated_Instance (Parent (N), Parent_Installed);
3369 Analyze_Subprogram_Declaration (N);
3370 Set_Is_Formal_Subprogram (Nam);
3371 Set_Has_Completion (Nam);
3373 if Nkind (N) = N_Formal_Abstract_Subprogram_Declaration then
3374 Set_Is_Abstract_Subprogram (Nam);
3376 Set_Is_Dispatching_Operation (Nam);
3378 -- A formal abstract procedure cannot have a null default
3379 -- (RM 12.6(4.1/2)).
3381 if Nkind (Spec) = N_Procedure_Specification
3382 and then Null_Present (Spec)
3383 then
3384 Error_Msg_N
3385 ("a formal abstract subprogram cannot default to null", Spec);
3386 end if;
3388 -- A formal abstract function cannot have an expression default
3389 -- (expression defaults are allowed for nonabstract formal functions
3390 -- when extensions are enabled).
3392 if Nkind (Spec) = N_Function_Specification
3393 and then Present (Expr)
3394 then
3395 Error_Msg_N
3396 ("a formal abstract subprogram cannot default to an expression",
3397 Spec);
3398 end if;
3400 declare
3401 Ctrl_Type : constant Entity_Id := Find_Dispatching_Type (Nam);
3402 begin
3403 if No (Ctrl_Type) then
3404 Error_Msg_N
3405 ("abstract formal subprogram must have a controlling type",
3408 elsif Ada_Version >= Ada_2012
3409 and then Is_Incomplete_Type (Ctrl_Type)
3410 then
3411 Error_Msg_NE
3412 ("controlling type of abstract formal subprogram cannot "
3413 & "be incomplete type", N, Ctrl_Type);
3415 else
3416 Check_Controlling_Formals (Ctrl_Type, Nam);
3417 end if;
3418 end;
3419 end if;
3421 -- Default name is resolved at the point of instantiation
3423 if Box_Present (N) then
3424 null;
3426 -- Default name is bound at the point of generic declaration
3428 elsif Present (Def) then
3429 if Nkind (Def) = N_Operator_Symbol then
3430 Find_Direct_Name (Def);
3432 elsif Nkind (Def) /= N_Attribute_Reference then
3433 Analyze (Def);
3435 else
3436 -- For an attribute reference, analyze the prefix and verify
3437 -- that it has the proper profile for the subprogram.
3439 Analyze (Prefix (Def));
3440 Valid_Default_Attribute (Nam, Def);
3441 goto Leave;
3442 end if;
3444 -- The default for a ghost generic formal procedure should be a ghost
3445 -- procedure (SPARK RM 6.9(13)).
3447 if Ekind (Nam) = E_Procedure then
3448 declare
3449 Def_E : Entity_Id := Empty;
3450 begin
3451 if Nkind (Def) in N_Has_Entity then
3452 Def_E := Entity (Def);
3453 end if;
3455 Check_Ghost_Formal_Procedure_Or_Package
3456 (N => Def,
3457 Actual => Def_E,
3458 Formal => Nam,
3459 Is_Default => True);
3460 end;
3461 end if;
3463 -- Default name may be overloaded, in which case the interpretation
3464 -- with the correct profile must be selected, as for a renaming.
3465 -- If the definition is an indexed component, it must denote a
3466 -- member of an entry family. If it is a selected component, it
3467 -- can be a protected operation.
3469 if Etype (Def) = Any_Type then
3470 goto Leave;
3472 elsif Nkind (Def) = N_Selected_Component then
3473 if not Is_Overloadable (Entity (Selector_Name (Def))) then
3474 Error_Msg_N ("expect valid subprogram name as default", Def);
3475 end if;
3477 elsif Nkind (Def) = N_Indexed_Component then
3478 if Is_Entity_Name (Prefix (Def)) then
3479 if Ekind (Entity (Prefix (Def))) /= E_Entry_Family then
3480 Error_Msg_N ("expect valid subprogram name as default", Def);
3481 end if;
3483 elsif Nkind (Prefix (Def)) = N_Selected_Component then
3484 if Ekind (Entity (Selector_Name (Prefix (Def)))) /=
3485 E_Entry_Family
3486 then
3487 Error_Msg_N ("expect valid subprogram name as default", Def);
3488 end if;
3490 else
3491 Error_Msg_N ("expect valid subprogram name as default", Def);
3492 goto Leave;
3493 end if;
3495 elsif Nkind (Def) = N_Character_Literal then
3497 -- Needs some type checks: subprogram should be parameterless???
3499 Resolve (Def, (Etype (Nam)));
3501 elsif not Is_Entity_Name (Def)
3502 or else not Is_Overloadable (Entity (Def))
3503 then
3504 Error_Msg_N ("expect valid subprogram name as default", Def);
3505 goto Leave;
3507 elsif not Is_Overloaded (Def) then
3508 Subp := Entity (Def);
3510 if Subp = Nam then
3511 Error_Msg_N ("premature usage of formal subprogram", Def);
3513 elsif not Entity_Matches_Spec (Subp, Nam) then
3514 Error_Msg_N ("no visible entity matches specification", Def);
3515 end if;
3517 -- More than one interpretation, so disambiguate as for a renaming
3519 else
3520 declare
3521 I : Interp_Index;
3522 I1 : Interp_Index := 0;
3523 It : Interp;
3524 It1 : Interp;
3526 begin
3527 Subp := Any_Id;
3528 Get_First_Interp (Def, I, It);
3529 while Present (It.Nam) loop
3530 if Entity_Matches_Spec (It.Nam, Nam) then
3531 if Subp /= Any_Id then
3532 It1 := Disambiguate (Def, I1, I, Etype (Subp));
3534 if It1 = No_Interp then
3535 Error_Msg_N ("ambiguous default subprogram", Def);
3536 else
3537 Subp := It1.Nam;
3538 end if;
3540 exit;
3542 else
3543 I1 := I;
3544 Subp := It.Nam;
3545 end if;
3546 end if;
3548 Get_Next_Interp (I, It);
3549 end loop;
3550 end;
3552 if Subp /= Any_Id then
3554 -- Subprogram found, generate reference to it
3556 Set_Entity (Def, Subp);
3557 Generate_Reference (Subp, Def);
3559 if Subp = Nam then
3560 Error_Msg_N ("premature usage of formal subprogram", Def);
3562 elsif Ekind (Subp) /= E_Operator then
3563 Check_Mode_Conformant (Subp, Nam);
3564 end if;
3566 else
3567 Error_Msg_N ("no visible subprogram matches specification", N);
3568 end if;
3569 end if;
3571 -- When extensions are enabled, an expression can be given as default
3572 -- for a formal function. The expression must be of the function result
3573 -- type and can reference formal parameters of the function.
3575 elsif Present (Expr) then
3576 Push_Scope (Nam);
3577 Install_Formals (Nam);
3578 Preanalyze_Spec_Expression (Expr, Etype (Nam));
3579 End_Scope;
3580 end if;
3582 <<Leave>>
3583 if Has_Aspects (N) then
3584 Analyze_Aspect_Specifications (N, Nam);
3585 end if;
3587 if Parent_Installed then
3588 Remove_Parent;
3589 end if;
3590 end Analyze_Formal_Subprogram_Declaration;
3592 -------------------------------------
3593 -- Analyze_Formal_Type_Declaration --
3594 -------------------------------------
3596 procedure Analyze_Formal_Type_Declaration (N : Node_Id) is
3597 Def : constant Node_Id := Formal_Type_Definition (N);
3599 Parent_Installed : Boolean := False;
3600 T : Entity_Id;
3602 begin
3603 T := Defining_Identifier (N);
3605 if Present (Discriminant_Specifications (N))
3606 and then Nkind (Def) /= N_Formal_Private_Type_Definition
3607 then
3608 Error_Msg_N
3609 ("discriminants not allowed for this formal type", T);
3610 end if;
3612 Check_Abbreviated_Instance (Parent (N), Parent_Installed);
3614 -- Enter the new name, and branch to specific routine
3616 case Nkind (Def) is
3617 when N_Formal_Private_Type_Definition =>
3618 Analyze_Formal_Private_Type (N, T, Def);
3620 when N_Formal_Derived_Type_Definition =>
3621 Analyze_Formal_Derived_Type (N, T, Def);
3623 when N_Formal_Incomplete_Type_Definition =>
3624 Analyze_Formal_Incomplete_Type (T, Def);
3626 when N_Formal_Discrete_Type_Definition =>
3627 Analyze_Formal_Discrete_Type (T, Def);
3629 when N_Formal_Signed_Integer_Type_Definition =>
3630 Analyze_Formal_Signed_Integer_Type (T, Def);
3632 when N_Formal_Modular_Type_Definition =>
3633 Analyze_Formal_Modular_Type (T, Def);
3635 when N_Formal_Floating_Point_Definition =>
3636 Analyze_Formal_Floating_Type (T, Def);
3638 when N_Formal_Ordinary_Fixed_Point_Definition =>
3639 Analyze_Formal_Ordinary_Fixed_Point_Type (T, Def);
3641 when N_Formal_Decimal_Fixed_Point_Definition =>
3642 Analyze_Formal_Decimal_Fixed_Point_Type (T, Def);
3644 when N_Array_Type_Definition =>
3645 Analyze_Formal_Array_Type (T, Def);
3647 when N_Access_Function_Definition
3648 | N_Access_Procedure_Definition
3649 | N_Access_To_Object_Definition
3651 Analyze_Generic_Access_Type (T, Def);
3653 -- Ada 2005: a interface declaration is encoded as an abstract
3654 -- record declaration or a abstract type derivation.
3656 when N_Record_Definition =>
3657 Analyze_Formal_Interface_Type (N, T, Def);
3659 when N_Derived_Type_Definition =>
3660 Analyze_Formal_Derived_Interface_Type (N, T, Def);
3662 when N_Error =>
3663 null;
3665 when others =>
3666 raise Program_Error;
3667 end case;
3669 -- A formal type declaration declares a type and its first
3670 -- subtype.
3672 Set_Is_Generic_Type (T);
3673 Set_Is_First_Subtype (T);
3675 if Present (Default_Subtype_Mark (Original_Node (N))) then
3676 Validate_Formal_Type_Default (N);
3677 end if;
3679 if Has_Aspects (N) then
3680 Analyze_Aspect_Specifications (N, T);
3681 end if;
3683 if Parent_Installed then
3684 Remove_Parent;
3685 end if;
3686 end Analyze_Formal_Type_Declaration;
3688 ------------------------------------
3689 -- Analyze_Function_Instantiation --
3690 ------------------------------------
3692 procedure Analyze_Function_Instantiation (N : Node_Id) is
3693 begin
3694 Analyze_Subprogram_Instantiation (N, E_Function);
3695 end Analyze_Function_Instantiation;
3697 ---------------------------------
3698 -- Analyze_Generic_Access_Type --
3699 ---------------------------------
3701 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id) is
3702 begin
3703 Enter_Name (T);
3705 if Nkind (Def) = N_Access_To_Object_Definition then
3706 Access_Type_Declaration (T, Def);
3708 if Is_Incomplete_Or_Private_Type (Designated_Type (T))
3709 and then No (Full_View (Designated_Type (T)))
3710 and then not Is_Generic_Type (Designated_Type (T))
3711 then
3712 Error_Msg_N ("premature usage of incomplete type", Def);
3714 elsif not Is_Entity_Name (Subtype_Indication (Def)) then
3715 Error_Msg_N
3716 ("only a subtype mark is allowed in a formal", Def);
3717 end if;
3719 else
3720 Access_Subprogram_Declaration (T, Def);
3721 end if;
3722 end Analyze_Generic_Access_Type;
3724 ---------------------------------
3725 -- Analyze_Generic_Formal_Part --
3726 ---------------------------------
3728 procedure Analyze_Generic_Formal_Part (N : Node_Id) is
3729 Gen_Parm_Decl : Node_Id;
3731 begin
3732 -- The generic formals are processed in the scope of the generic unit,
3733 -- where they are immediately visible. The scope is installed by the
3734 -- caller.
3736 Gen_Parm_Decl := First (Generic_Formal_Declarations (N));
3737 while Present (Gen_Parm_Decl) loop
3738 Analyze (Gen_Parm_Decl);
3739 Next (Gen_Parm_Decl);
3740 end loop;
3742 Generate_Reference_To_Generic_Formals (Current_Scope);
3744 -- For Ada 2022, some formal parameters can carry aspects, which must
3745 -- be name-resolved at the end of the list of formal parameters (which
3746 -- has the semantics of a declaration list).
3748 Analyze_Contracts (Generic_Formal_Declarations (N));
3749 end Analyze_Generic_Formal_Part;
3751 ------------------------------------------
3752 -- Analyze_Generic_Package_Declaration --
3753 ------------------------------------------
3755 procedure Analyze_Generic_Package_Declaration (N : Node_Id) is
3756 Decls : constant List_Id := Visible_Declarations (Specification (N));
3757 Loc : constant Source_Ptr := Sloc (N);
3759 Decl : Node_Id;
3760 Id : Entity_Id;
3761 New_N : Node_Id;
3762 Renaming : Node_Id;
3763 Save_Parent : Node_Id;
3765 begin
3766 -- A generic may grant access to its private enclosing context depending
3767 -- on the placement of its corresponding body. From elaboration point of
3768 -- view, the flow of execution may enter this private context, and then
3769 -- reach an external unit, thus producing a dependency on that external
3770 -- unit. For such a path to be properly discovered and encoded in the
3771 -- ALI file of the main unit, let the ABE mechanism process the body of
3772 -- the main unit, and encode all relevant invocation constructs and the
3773 -- relations between them.
3775 Mark_Save_Invocation_Graph_Of_Body;
3777 -- We introduce a renaming of the enclosing package, to have a usable
3778 -- entity as the prefix of an expanded name for a local entity of the
3779 -- form Par.P.Q, where P is the generic package. This is because a local
3780 -- entity named P may hide it, so that the usual visibility rules in
3781 -- the instance will not resolve properly.
3783 Renaming :=
3784 Make_Package_Renaming_Declaration (Loc,
3785 Defining_Unit_Name =>
3786 Make_Defining_Identifier (Loc,
3787 Chars => New_External_Name (Chars (Defining_Entity (N)), "GH")),
3788 Name =>
3789 Make_Identifier (Loc, Chars (Defining_Entity (N))));
3791 -- The declaration is inserted before other declarations, but before
3792 -- pragmas that may be library-unit pragmas and must appear before other
3793 -- declarations. The pragma Compile_Time_Error is not in this class, and
3794 -- may contain an expression that includes such a qualified name, so the
3795 -- renaming declaration must appear before it.
3797 -- Are there other pragmas that require this special handling ???
3799 if Present (Decls) then
3800 Decl := First (Decls);
3801 while Present (Decl)
3802 and then Nkind (Decl) = N_Pragma
3803 and then Get_Pragma_Id (Decl) /= Pragma_Compile_Time_Error
3804 loop
3805 Next (Decl);
3806 end loop;
3808 if Present (Decl) then
3809 Insert_Before (Decl, Renaming);
3810 else
3811 Append (Renaming, Visible_Declarations (Specification (N)));
3812 end if;
3814 else
3815 Set_Visible_Declarations (Specification (N), New_List (Renaming));
3816 end if;
3818 -- Create copy of generic unit, and save for instantiation. If the unit
3819 -- is a child unit, do not copy the specifications for the parent, which
3820 -- are not part of the generic tree.
3822 Save_Parent := Parent_Spec (N);
3823 Set_Parent_Spec (N, Empty);
3825 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
3826 Set_Parent_Spec (New_N, Save_Parent);
3827 Rewrite (N, New_N);
3829 -- Once the contents of the generic copy and the template are swapped,
3830 -- do the same for their respective aspect specifications.
3832 Exchange_Aspects (N, New_N);
3834 -- Collect all contract-related source pragmas found within the template
3835 -- and attach them to the contract of the package spec. This contract is
3836 -- used in the capture of global references within annotations.
3838 Create_Generic_Contract (N);
3840 Id := Defining_Entity (N);
3841 Generate_Definition (Id);
3843 -- Expansion is not applied to generic units
3845 Start_Generic;
3847 Enter_Name (Id);
3848 Mutate_Ekind (Id, E_Generic_Package);
3849 Set_Etype (Id, Standard_Void_Type);
3851 -- Set SPARK_Mode from context
3853 Set_SPARK_Pragma (Id, SPARK_Mode_Pragma);
3854 Set_SPARK_Aux_Pragma (Id, SPARK_Mode_Pragma);
3855 Set_SPARK_Pragma_Inherited (Id);
3856 Set_SPARK_Aux_Pragma_Inherited (Id);
3858 -- Preserve relevant elaboration-related attributes of the context which
3859 -- are no longer available or very expensive to recompute once analysis,
3860 -- resolution, and expansion are over.
3862 Mark_Elaboration_Attributes
3863 (N_Id => Id,
3864 Checks => True,
3865 Warnings => True);
3867 -- Analyze aspects now, so that generated pragmas appear in the
3868 -- declarations before building and analyzing the generic copy.
3870 if Has_Aspects (N) then
3871 Analyze_Aspect_Specifications (N, Id);
3872 end if;
3874 Push_Scope (Id);
3875 Enter_Generic_Scope (Id);
3876 Set_Inner_Instances (Id, New_Elmt_List);
3878 Set_Categorization_From_Pragmas (N);
3879 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3881 -- Link the declaration of the generic homonym in the generic copy to
3882 -- the package it renames, so that it is always resolved properly.
3884 Set_Generic_Homonym (Id, Defining_Unit_Name (Renaming));
3885 Set_Entity (Associated_Node (Name (Renaming)), Id);
3887 -- For a library unit, we have reconstructed the entity for the unit,
3888 -- and must reset it in the library tables.
3890 if Nkind (Parent (N)) = N_Compilation_Unit then
3891 Set_Cunit_Entity (Current_Sem_Unit, Id);
3892 end if;
3894 Analyze_Generic_Formal_Part (N);
3896 -- After processing the generic formals, analysis proceeds as for a
3897 -- non-generic package.
3899 Analyze (Specification (N));
3901 Validate_Categorization_Dependency (N, Id);
3903 End_Generic;
3905 End_Package_Scope (Id);
3906 Exit_Generic_Scope (Id);
3908 -- If the generic appears within a package unit, the body of that unit
3909 -- has to be present for instantiation and inlining.
3911 if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Declaration then
3912 Set_Body_Needed_For_Inlining
3913 (Defining_Entity (Unit (Cunit (Current_Sem_Unit))));
3914 end if;
3916 if Nkind (Parent (N)) /= N_Compilation_Unit then
3917 Move_Freeze_Nodes (Id, N, Visible_Declarations (Specification (N)));
3918 Move_Freeze_Nodes (Id, N, Private_Declarations (Specification (N)));
3919 Move_Freeze_Nodes (Id, N, Generic_Formal_Declarations (N));
3921 else
3922 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
3923 Validate_RT_RAT_Component (N);
3925 -- If this is a spec without a body, check that generic parameters
3926 -- are referenced.
3928 if not Body_Required (Parent (N)) then
3929 Check_References (Id);
3930 end if;
3931 end if;
3933 -- If there is a specified storage pool in the context, create an
3934 -- aspect on the package declaration, so that it is used in any
3935 -- instance that does not override it.
3937 if Present (Default_Pool) then
3938 declare
3939 ASN : Node_Id;
3941 begin
3942 ASN :=
3943 Make_Aspect_Specification (Loc,
3944 Identifier => Make_Identifier (Loc, Name_Default_Storage_Pool),
3945 Expression => New_Copy (Default_Pool));
3947 if No (Aspect_Specifications (Specification (N))) then
3948 Set_Aspect_Specifications (Specification (N), New_List (ASN));
3949 else
3950 Append (ASN, Aspect_Specifications (Specification (N)));
3951 end if;
3952 end;
3953 end if;
3954 end Analyze_Generic_Package_Declaration;
3956 --------------------------------------------
3957 -- Analyze_Generic_Subprogram_Declaration --
3958 --------------------------------------------
3960 procedure Analyze_Generic_Subprogram_Declaration (N : Node_Id) is
3961 Formals : List_Id;
3962 Id : Entity_Id;
3963 New_N : Node_Id;
3964 Result_Type : Entity_Id;
3965 Save_Parent : Node_Id;
3966 Spec : Node_Id;
3967 Typ : Entity_Id;
3969 begin
3970 -- A generic may grant access to its private enclosing context depending
3971 -- on the placement of its corresponding body. From elaboration point of
3972 -- view, the flow of execution may enter this private context, and then
3973 -- reach an external unit, thus producing a dependency on that external
3974 -- unit. For such a path to be properly discovered and encoded in the
3975 -- ALI file of the main unit, let the ABE mechanism process the body of
3976 -- the main unit, and encode all relevant invocation constructs and the
3977 -- relations between them.
3979 Mark_Save_Invocation_Graph_Of_Body;
3981 -- Create copy of generic unit, and save for instantiation. If the unit
3982 -- is a child unit, do not copy the specifications for the parent, which
3983 -- are not part of the generic tree.
3985 Save_Parent := Parent_Spec (N);
3986 Set_Parent_Spec (N, Empty);
3988 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
3989 Set_Parent_Spec (New_N, Save_Parent);
3990 Rewrite (N, New_N);
3992 -- Once the contents of the generic copy and the template are swapped,
3993 -- do the same for their respective aspect specifications.
3995 Exchange_Aspects (N, New_N);
3997 -- Collect all contract-related source pragmas found within the template
3998 -- and attach them to the contract of the subprogram spec. This contract
3999 -- is used in the capture of global references within annotations.
4001 Create_Generic_Contract (N);
4003 Spec := Specification (N);
4004 Id := Defining_Entity (Spec);
4005 Generate_Definition (Id);
4007 if Nkind (Id) = N_Defining_Operator_Symbol then
4008 Error_Msg_N
4009 ("operator symbol not allowed for generic subprogram", Id);
4010 end if;
4012 Start_Generic;
4014 Enter_Name (Id);
4015 Set_Scope_Depth_Value (Id, Scope_Depth (Current_Scope) + 1);
4017 Push_Scope (Id);
4018 Enter_Generic_Scope (Id);
4019 Set_Inner_Instances (Id, New_Elmt_List);
4020 Set_Is_Pure (Id, Is_Pure (Current_Scope));
4022 Analyze_Generic_Formal_Part (N);
4024 if Nkind (Spec) = N_Function_Specification then
4025 Mutate_Ekind (Id, E_Generic_Function);
4026 else
4027 Mutate_Ekind (Id, E_Generic_Procedure);
4028 end if;
4030 -- Set SPARK_Mode from context
4032 Set_SPARK_Pragma (Id, SPARK_Mode_Pragma);
4033 Set_SPARK_Pragma_Inherited (Id);
4035 -- Preserve relevant elaboration-related attributes of the context which
4036 -- are no longer available or very expensive to recompute once analysis,
4037 -- resolution, and expansion are over.
4039 Mark_Elaboration_Attributes
4040 (N_Id => Id,
4041 Checks => True,
4042 Warnings => True);
4044 Formals := Parameter_Specifications (Spec);
4046 if Present (Formals) then
4047 Process_Formals (Formals, Spec);
4048 end if;
4050 if Nkind (Spec) = N_Function_Specification then
4051 if Nkind (Result_Definition (Spec)) = N_Access_Definition then
4052 Result_Type := Access_Definition (Spec, Result_Definition (Spec));
4053 Set_Etype (Id, Result_Type);
4055 -- Check restriction imposed by AI05-073: a generic function
4056 -- cannot return an abstract type or an access to such.
4058 if Is_Abstract_Type (Designated_Type (Result_Type)) then
4059 Error_Msg_N
4060 ("generic function cannot have an access result "
4061 & "that designates an abstract type", Spec);
4062 end if;
4064 else
4065 Find_Type (Result_Definition (Spec));
4066 Typ := Entity (Result_Definition (Spec));
4068 if Is_Abstract_Type (Typ)
4069 and then Ada_Version >= Ada_2012
4070 then
4071 Error_Msg_N
4072 ("generic function cannot have abstract result type", Spec);
4073 end if;
4075 -- If a null exclusion is imposed on the result type, then create
4076 -- a null-excluding itype (an access subtype) and use it as the
4077 -- function's Etype.
4079 if Is_Access_Type (Typ)
4080 and then Null_Exclusion_Present (Spec)
4081 then
4082 Set_Etype (Id,
4083 Create_Null_Excluding_Itype
4084 (T => Typ,
4085 Related_Nod => Spec,
4086 Scope_Id => Defining_Unit_Name (Spec)));
4087 else
4088 Set_Etype (Id, Typ);
4089 end if;
4090 end if;
4092 else
4093 Set_Etype (Id, Standard_Void_Type);
4094 end if;
4096 -- Analyze the aspects of the generic copy to ensure that all generated
4097 -- pragmas (if any) perform their semantic effects.
4099 if Has_Aspects (N) then
4100 Analyze_Aspect_Specifications (N, Id);
4101 end if;
4103 -- For a library unit, we have reconstructed the entity for the unit,
4104 -- and must reset it in the library tables. We also make sure that
4105 -- Body_Required is set properly in the original compilation unit node.
4107 if Nkind (Parent (N)) = N_Compilation_Unit then
4108 Set_Cunit_Entity (Current_Sem_Unit, Id);
4109 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
4110 end if;
4112 -- If the generic appears within a package unit, the body of that unit
4113 -- has to be present for instantiation and inlining.
4115 if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Declaration
4116 and then Unit_Requires_Body (Id)
4117 then
4118 Set_Body_Needed_For_Inlining
4119 (Defining_Entity (Unit (Cunit (Current_Sem_Unit))));
4120 end if;
4122 Set_Categorization_From_Pragmas (N);
4123 Validate_Categorization_Dependency (N, Id);
4125 -- Capture all global references that occur within the profile of the
4126 -- generic subprogram. Aspects are not part of this processing because
4127 -- they must be delayed. If processed now, Save_Global_References will
4128 -- destroy the Associated_Node links and prevent the capture of global
4129 -- references when the contract of the generic subprogram is analyzed.
4131 Save_Global_References (Original_Node (N));
4133 End_Generic;
4134 End_Scope;
4135 Exit_Generic_Scope (Id);
4136 Generate_Reference_To_Formals (Id);
4138 List_Inherited_Pre_Post_Aspects (Id);
4139 end Analyze_Generic_Subprogram_Declaration;
4141 -----------------------------------
4142 -- Analyze_Package_Instantiation --
4143 -----------------------------------
4145 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
4146 -- must be replaced by gotos which jump to the end of the routine in order
4147 -- to restore the Ghost and SPARK modes.
4149 procedure Analyze_Package_Instantiation (N : Node_Id) is
4150 Has_Inline_Always : Boolean := False;
4151 -- Set if the generic unit contains any subprograms with Inline_Always.
4152 -- Only relevant when back-end inlining is not enabled.
4154 function Might_Inline_Subp (Gen_Unit : Entity_Id) return Boolean;
4155 -- Return True if inlining is active and Gen_Unit contains inlined
4156 -- subprograms. In this case, we may either instantiate the body when
4157 -- front-end inlining is enabled, or add a pending instantiation when
4158 -- back-end inlining is enabled. In the former case, this may cause
4159 -- superfluous instantiations, but in either case we need to perform
4160 -- the instantiation of the body in the context of the instance and
4161 -- not in that of the point of inlining.
4163 function Needs_Body_Instantiated (Gen_Unit : Entity_Id) return Boolean;
4164 -- Return True if Gen_Unit needs to have its body instantiated in the
4165 -- context of N. This in particular excludes generic contexts.
4167 -----------------------
4168 -- Might_Inline_Subp --
4169 -----------------------
4171 function Might_Inline_Subp (Gen_Unit : Entity_Id) return Boolean is
4172 E : Entity_Id;
4174 begin
4175 if Inline_Processing_Required then
4176 -- No need to recompute the answer if we know it is positive
4177 -- and back-end inlining is enabled.
4179 if Is_Inlined (Gen_Unit) and then Back_End_Inlining then
4180 return True;
4181 end if;
4183 E := First_Entity (Gen_Unit);
4184 while Present (E) loop
4185 if Is_Subprogram (E) and then Is_Inlined (E) then
4186 -- Remember if there are any subprograms with Inline_Always
4188 if Has_Pragma_Inline_Always (E) then
4189 Has_Inline_Always := True;
4190 end if;
4192 Set_Is_Inlined (Gen_Unit);
4193 return True;
4194 end if;
4196 Next_Entity (E);
4197 end loop;
4198 end if;
4200 return False;
4201 end Might_Inline_Subp;
4203 -------------------------------
4204 -- Needs_Body_Instantiated --
4205 -------------------------------
4207 function Needs_Body_Instantiated (Gen_Unit : Entity_Id) return Boolean is
4208 begin
4209 -- No need to instantiate bodies in generic units
4211 if Is_Generic_Unit (Cunit_Entity (Main_Unit)) then
4212 return False;
4213 end if;
4215 -- If the instantiation is in the main unit, then the body is needed
4217 if Is_In_Main_Unit (N) then
4218 return True;
4219 end if;
4221 -- In GNATprove mode, never instantiate bodies outside of the main
4222 -- unit, as it does not use frontend/backend inlining in the way that
4223 -- GNAT does, so does not benefit from such instantiations. On the
4224 -- contrary, such instantiations may bring artificial constraints,
4225 -- as for example such bodies may require preprocessing.
4227 if GNATprove_Mode then
4228 return False;
4229 end if;
4231 -- If not, then again no need to instantiate bodies in generic units
4233 if Is_Generic_Unit (Cunit_Entity (Get_Code_Unit (N))) then
4234 return False;
4235 end if;
4237 -- Here we have a special handling for back-end inlining: if inline
4238 -- processing is required, then we unconditionally want to have the
4239 -- body instantiated. The reason is that Might_Inline_Subp does not
4240 -- catch all the cases (as it does not recurse into nested packages)
4241 -- so this avoids the need to patch things up afterwards. Moreover,
4242 -- these instantiations are only performed on demand when back-end
4243 -- inlining is enabled, so this causes very little extra work.
4245 if Inline_Processing_Required and then Back_End_Inlining then
4246 return True;
4247 end if;
4249 -- We want to have the bodies instantiated in non-main units if
4250 -- they might contribute inlined subprograms.
4252 return Might_Inline_Subp (Gen_Unit);
4253 end Needs_Body_Instantiated;
4255 -- Local declarations
4257 Gen_Id : constant Node_Id := Name (N);
4258 Inst_Id : constant Entity_Id := Defining_Entity (N);
4259 Is_Actual_Pack : constant Boolean := Is_Internal (Inst_Id);
4260 Loc : constant Source_Ptr := Sloc (N);
4262 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
4263 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
4264 Saved_ISMP : constant Boolean :=
4265 Ignore_SPARK_Mode_Pragmas_In_Instance;
4266 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
4267 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
4268 -- Save the Ghost and SPARK mode-related data to restore on exit
4270 Saved_Style_Check : constant Boolean := Style_Check;
4271 -- Save style check mode for restore on exit
4273 Act_Decl : Node_Id;
4274 Act_Decl_Name : Node_Id;
4275 Act_Decl_Id : Entity_Id;
4276 Act_Spec : Node_Id;
4277 Act_Tree : Node_Id;
4278 Env_Installed : Boolean := False;
4279 Gen_Decl : Node_Id;
4280 Gen_Spec : Node_Id;
4281 Gen_Unit : Entity_Id;
4282 Inline_Now : Boolean := False;
4283 Needs_Body : Boolean;
4284 Parent_Installed : Boolean := False;
4285 Renaming_List : List_Id;
4286 Unit_Renaming : Node_Id;
4288 Vis_Prims_List : Elist_Id := No_Elist;
4289 -- List of primitives made temporarily visible in the instantiation
4290 -- to match the visibility of the formal type
4292 -- Start of processing for Analyze_Package_Instantiation
4294 begin
4295 -- Preserve relevant elaboration-related attributes of the context which
4296 -- are no longer available or very expensive to recompute once analysis,
4297 -- resolution, and expansion are over.
4299 Mark_Elaboration_Attributes
4300 (N_Id => N,
4301 Checks => True,
4302 Level => True,
4303 Modes => True,
4304 Warnings => True);
4306 -- Very first thing: check for Text_IO special unit in case we are
4307 -- instantiating one of the children of [[Wide_]Wide_]Text_IO.
4309 Check_Text_IO_Special_Unit (Name (N));
4311 -- Make node global for error reporting
4313 Instantiation_Node := N;
4315 -- Case of instantiation of a generic package
4317 if Nkind (N) = N_Package_Instantiation then
4318 Act_Decl_Id := New_Copy (Defining_Entity (N));
4320 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name then
4321 Act_Decl_Name :=
4322 Make_Defining_Program_Unit_Name (Loc,
4323 Name =>
4324 New_Copy_Tree (Name (Defining_Unit_Name (N))),
4325 Defining_Identifier => Act_Decl_Id);
4326 else
4327 Act_Decl_Name := Act_Decl_Id;
4328 end if;
4330 -- Case of instantiation of a formal package
4332 else
4333 Act_Decl_Id := Defining_Identifier (N);
4334 Act_Decl_Name := Act_Decl_Id;
4335 end if;
4337 Generate_Definition (Act_Decl_Id);
4338 Mutate_Ekind (Act_Decl_Id, E_Package);
4340 -- Initialize list of incomplete actuals before analysis
4342 Set_Incomplete_Actuals (Act_Decl_Id, New_Elmt_List);
4344 Preanalyze_Actuals (N, Act_Decl_Id);
4346 -- Turn off style checking in instances. If the check is enabled on the
4347 -- generic unit, a warning in an instance would just be noise. If not
4348 -- enabled on the generic, then a warning in an instance is just wrong.
4349 -- This must be done after analyzing the actuals, which do come from
4350 -- source and are subject to style checking.
4352 Style_Check := False;
4354 Init_Env;
4355 Env_Installed := True;
4357 -- Reset renaming map for formal types. The mapping is established
4358 -- when analyzing the generic associations, but some mappings are
4359 -- inherited from formal packages of parent units, and these are
4360 -- constructed when the parents are installed.
4362 Generic_Renamings.Set_Last (0);
4363 Generic_Renamings_HTable.Reset;
4365 -- Except for an abbreviated instance created to check a formal package,
4366 -- install the parent if this is a generic child unit.
4368 if not Is_Abbreviated_Instance (Inst_Id) then
4369 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
4370 end if;
4372 Gen_Unit := Entity (Gen_Id);
4374 -- A package instantiation is Ghost when it is subject to pragma Ghost
4375 -- or the generic template is Ghost. Set the mode now to ensure that
4376 -- any nodes generated during analysis and expansion are marked as
4377 -- Ghost.
4379 Mark_And_Set_Ghost_Instantiation (N, Gen_Unit);
4381 -- Verify that it is the name of a generic package
4383 -- A visibility glitch: if the instance is a child unit and the generic
4384 -- is the generic unit of a parent instance (i.e. both the parent and
4385 -- the child units are instances of the same package) the name now
4386 -- denotes the renaming within the parent, not the intended generic
4387 -- unit. See if there is a homonym that is the desired generic. The
4388 -- renaming declaration must be visible inside the instance of the
4389 -- child, but not when analyzing the name in the instantiation itself.
4391 if Ekind (Gen_Unit) = E_Package
4392 and then Present (Renamed_Entity (Gen_Unit))
4393 and then In_Open_Scopes (Renamed_Entity (Gen_Unit))
4394 and then Is_Generic_Instance (Renamed_Entity (Gen_Unit))
4395 and then Present (Homonym (Gen_Unit))
4396 then
4397 Gen_Unit := Homonym (Gen_Unit);
4398 end if;
4400 if Etype (Gen_Unit) = Any_Type then
4401 Restore_Env;
4402 goto Leave;
4404 elsif Ekind (Gen_Unit) /= E_Generic_Package then
4406 -- Ada 2005 (AI-50217): Cannot use instance in limited with_clause
4408 if From_Limited_With (Gen_Unit) then
4409 Error_Msg_N
4410 ("cannot instantiate a limited withed package", Gen_Id);
4411 else
4412 Error_Msg_NE
4413 ("& is not the name of a generic package", Gen_Id, Gen_Unit);
4414 end if;
4416 Restore_Env;
4417 goto Leave;
4418 end if;
4420 if In_Extended_Main_Source_Unit (N) then
4421 Set_Is_Instantiated (Gen_Unit);
4422 Generate_Reference (Gen_Unit, N);
4424 if Present (Renamed_Entity (Gen_Unit)) then
4425 Set_Is_Instantiated (Renamed_Entity (Gen_Unit));
4426 Generate_Reference (Renamed_Entity (Gen_Unit), N);
4427 end if;
4428 end if;
4430 if Nkind (Gen_Id) = N_Identifier
4431 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
4432 then
4433 Error_Msg_NE
4434 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
4436 elsif Nkind (Gen_Id) = N_Expanded_Name
4437 and then Is_Child_Unit (Gen_Unit)
4438 and then Nkind (Prefix (Gen_Id)) = N_Identifier
4439 and then Chars (Act_Decl_Id) = Chars (Prefix (Gen_Id))
4440 then
4441 Error_Msg_N
4442 ("& is hidden within declaration of instance", Prefix (Gen_Id));
4443 end if;
4445 Set_Entity (Gen_Id, Gen_Unit);
4447 -- If generic is a renaming, get original generic unit
4449 if Present (Renamed_Entity (Gen_Unit))
4450 and then Ekind (Renamed_Entity (Gen_Unit)) = E_Generic_Package
4451 then
4452 Gen_Unit := Renamed_Entity (Gen_Unit);
4453 end if;
4455 -- Verify that there are no circular instantiations
4457 if In_Open_Scopes (Gen_Unit) then
4458 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
4459 Restore_Env;
4460 goto Leave;
4462 elsif Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
4463 Error_Msg_Node_2 := Current_Scope;
4464 Error_Msg_NE
4465 ("circular instantiation: & instantiated in &!", N, Gen_Unit);
4466 Circularity_Detected := True;
4467 Restore_Env;
4468 goto Leave;
4470 else
4471 Mutate_Ekind (Inst_Id, E_Package);
4472 Set_Scope (Inst_Id, Current_Scope);
4474 -- If the context of the instance is subject to SPARK_Mode "off" or
4475 -- the annotation is altogether missing, set the global flag which
4476 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
4477 -- the instance.
4479 if SPARK_Mode /= On then
4480 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
4482 -- Mark the instance spec in case the body is instantiated at a
4483 -- later pass. This preserves the original context in effect for
4484 -- the body.
4486 Set_Ignore_SPARK_Mode_Pragmas (Act_Decl_Id);
4487 end if;
4489 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
4490 Gen_Spec := Specification (Gen_Decl);
4492 -- Initialize renamings map, for error checking, and the list that
4493 -- holds private entities whose views have changed between generic
4494 -- definition and instantiation. If this is the instance created to
4495 -- validate an actual package, the instantiation environment is that
4496 -- of the enclosing instance.
4498 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
4500 -- Copy original generic tree, to produce text for instantiation
4502 Act_Tree :=
4503 Copy_Generic_Node
4504 (Original_Node (Gen_Decl), Empty, Instantiating => True);
4506 Act_Spec := Specification (Act_Tree);
4508 -- If this is the instance created to validate an actual package,
4509 -- only the formals matter, do not examine the package spec itself.
4511 if Is_Actual_Pack then
4512 Set_Visible_Declarations (Act_Spec, New_List);
4513 Set_Private_Declarations (Act_Spec, New_List);
4514 end if;
4516 Renaming_List :=
4517 Analyze_Associations
4518 (I_Node => N,
4519 Formals => Generic_Formal_Declarations (Act_Tree),
4520 F_Copy => Generic_Formal_Declarations (Gen_Decl));
4522 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
4524 Set_Instance_Env (Gen_Unit, Act_Decl_Id);
4525 Set_Defining_Unit_Name (Act_Spec, Act_Decl_Name);
4526 Set_Is_Generic_Instance (Act_Decl_Id);
4527 Set_Generic_Parent (Act_Spec, Gen_Unit);
4529 -- References to the generic in its own declaration or its body are
4530 -- references to the instance. Add a renaming declaration for the
4531 -- generic unit itself. This declaration, as well as the renaming
4532 -- declarations for the generic formals, must remain private to the
4533 -- unit: the formals, because this is the language semantics, and
4534 -- the unit because its use is an artifact of the implementation.
4536 Unit_Renaming :=
4537 Make_Package_Renaming_Declaration (Loc,
4538 Defining_Unit_Name =>
4539 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
4540 Name => New_Occurrence_Of (Act_Decl_Id, Loc));
4542 Append (Unit_Renaming, Renaming_List);
4544 -- The renaming declarations are the first local declarations of the
4545 -- new unit.
4547 if Is_Non_Empty_List (Visible_Declarations (Act_Spec)) then
4548 Insert_List_Before
4549 (First (Visible_Declarations (Act_Spec)), Renaming_List);
4550 else
4551 Set_Visible_Declarations (Act_Spec, Renaming_List);
4552 end if;
4554 Act_Decl := Make_Package_Declaration (Loc, Specification => Act_Spec);
4556 -- Propagate the aspect specifications from the package declaration
4557 -- template to the instantiated version of the package declaration.
4559 if Has_Aspects (Act_Tree) then
4560 Set_Aspect_Specifications (Act_Decl,
4561 New_Copy_List_Tree (Aspect_Specifications (Act_Tree)));
4562 end if;
4564 -- The generic may have a generated Default_Storage_Pool aspect,
4565 -- set at the point of generic declaration. If the instance has
4566 -- that aspect, it overrides the one inherited from the generic.
4568 if Has_Aspects (Gen_Spec) then
4569 if No (Aspect_Specifications (N)) then
4570 Set_Aspect_Specifications (N,
4571 (New_Copy_List_Tree
4572 (Aspect_Specifications (Gen_Spec))));
4574 else
4575 declare
4576 Inherited_Aspects : constant List_Id :=
4577 New_Copy_List_Tree
4578 (Aspect_Specifications (Gen_Spec));
4580 ASN1 : Node_Id;
4581 ASN2 : Node_Id;
4582 Pool_Present : Boolean := False;
4584 begin
4585 ASN1 := First (Aspect_Specifications (N));
4586 while Present (ASN1) loop
4587 if Chars (Identifier (ASN1)) =
4588 Name_Default_Storage_Pool
4589 then
4590 Pool_Present := True;
4591 exit;
4592 end if;
4594 Next (ASN1);
4595 end loop;
4597 if Pool_Present then
4599 -- If generic carries a default storage pool, remove it
4600 -- in favor of the instance one.
4602 ASN2 := First (Inherited_Aspects);
4603 while Present (ASN2) loop
4604 if Chars (Identifier (ASN2)) =
4605 Name_Default_Storage_Pool
4606 then
4607 Remove (ASN2);
4608 exit;
4609 end if;
4611 Next (ASN2);
4612 end loop;
4613 end if;
4615 Prepend_List_To
4616 (Aspect_Specifications (N), Inherited_Aspects);
4617 end;
4618 end if;
4619 end if;
4621 -- Save the instantiation node for a subsequent instantiation of the
4622 -- body if there is one and it needs to be instantiated here.
4624 -- We instantiate the body only if we are generating code, or if we
4625 -- are generating cross-reference information, or for GNATprove use.
4627 declare
4628 Enclosing_Body_Present : Boolean := False;
4629 -- If the generic unit is not a compilation unit, then a body may
4630 -- be present in its parent even if none is required. We create a
4631 -- tentative pending instantiation for the body, which will be
4632 -- discarded if none is actually present.
4634 Scop : Entity_Id;
4636 begin
4637 if Scope (Gen_Unit) /= Standard_Standard
4638 and then not Is_Child_Unit (Gen_Unit)
4639 then
4640 Scop := Scope (Gen_Unit);
4641 while Present (Scop) and then Scop /= Standard_Standard loop
4642 if Unit_Requires_Body (Scop) then
4643 Enclosing_Body_Present := True;
4644 exit;
4646 elsif In_Open_Scopes (Scop)
4647 and then In_Package_Body (Scop)
4648 then
4649 Enclosing_Body_Present := True;
4650 exit;
4651 end if;
4653 exit when Is_Compilation_Unit (Scop);
4654 Scop := Scope (Scop);
4655 end loop;
4656 end if;
4658 -- If front-end inlining is enabled or there are any subprograms
4659 -- marked with Inline_Always, and this is a unit for which code
4660 -- will be generated, we instantiate the body at once.
4662 -- This is done if the instance is not the main unit, and if the
4663 -- generic is not a child unit of another generic, to avoid scope
4664 -- problems and the reinstallation of parent instances.
4666 if Expander_Active
4667 and then (not Is_Child_Unit (Gen_Unit)
4668 or else not Is_Generic_Unit (Scope (Gen_Unit)))
4669 and then Might_Inline_Subp (Gen_Unit)
4670 and then not Is_Actual_Pack
4671 then
4672 if not Back_End_Inlining
4673 and then (Front_End_Inlining or else Has_Inline_Always)
4674 and then (Is_In_Main_Unit (N)
4675 or else In_Main_Context (Current_Scope))
4676 and then Nkind (Parent (N)) /= N_Compilation_Unit
4677 then
4678 Inline_Now := True;
4680 -- In configurable_run_time mode we force the inlining of
4681 -- predefined subprograms marked Inline_Always, to minimize
4682 -- the use of the run-time library.
4684 elsif In_Predefined_Unit (Gen_Decl)
4685 and then Configurable_Run_Time_Mode
4686 and then Nkind (Parent (N)) /= N_Compilation_Unit
4687 then
4688 Inline_Now := True;
4689 end if;
4691 -- If the current scope is itself an instance within a child
4692 -- unit, there will be duplications in the scope stack, and the
4693 -- unstacking mechanism in Inline_Instance_Body will fail.
4694 -- This loses some rare cases of optimization.
4696 if Is_Generic_Instance (Current_Scope) then
4697 declare
4698 Curr_Unit : constant Entity_Id :=
4699 Cunit_Entity (Current_Sem_Unit);
4700 begin
4701 if Curr_Unit /= Current_Scope
4702 and then Is_Child_Unit (Curr_Unit)
4703 then
4704 Inline_Now := False;
4705 end if;
4706 end;
4707 end if;
4708 end if;
4710 Needs_Body :=
4711 (Unit_Requires_Body (Gen_Unit)
4712 or else Enclosing_Body_Present
4713 or else Present (Corresponding_Body (Gen_Decl)))
4714 and then Needs_Body_Instantiated (Gen_Unit)
4715 and then not Is_Actual_Pack
4716 and then not Inline_Now
4717 and then (Operating_Mode = Generate_Code
4718 or else (Operating_Mode = Check_Semantics
4719 and then GNATprove_Mode));
4721 -- If front-end inlining is enabled or there are any subprograms
4722 -- marked with Inline_Always, do not instantiate body when within
4723 -- a generic context.
4725 if not Back_End_Inlining
4726 and then (Front_End_Inlining or else Has_Inline_Always)
4727 and then not Expander_Active
4728 then
4729 Needs_Body := False;
4730 end if;
4732 -- If the current context is generic, and the package being
4733 -- instantiated is declared within a formal package, there is no
4734 -- body to instantiate until the enclosing generic is instantiated
4735 -- and there is an actual for the formal package. If the formal
4736 -- package has parameters, we build a regular package instance for
4737 -- it, that precedes the original formal package declaration.
4739 if In_Open_Scopes (Scope (Scope (Gen_Unit))) then
4740 declare
4741 Decl : constant Node_Id :=
4742 Original_Node
4743 (Unit_Declaration_Node (Scope (Gen_Unit)));
4744 begin
4745 if Nkind (Decl) = N_Formal_Package_Declaration
4746 or else (Nkind (Decl) = N_Package_Declaration
4747 and then Is_List_Member (Decl)
4748 and then Present (Next (Decl))
4749 and then
4750 Nkind (Next (Decl)) =
4751 N_Formal_Package_Declaration)
4752 then
4753 Needs_Body := False;
4754 end if;
4755 end;
4756 end if;
4757 end;
4759 -- For RCI unit calling stubs, we omit the instance body if the
4760 -- instance is the RCI library unit itself.
4762 -- However there is a special case for nested instances: in this case
4763 -- we do generate the instance body, as it might be required, e.g.
4764 -- because it provides stream attributes for some type used in the
4765 -- profile of a remote subprogram. This is consistent with 12.3(12),
4766 -- which indicates that the instance body occurs at the place of the
4767 -- instantiation, and thus is part of the RCI declaration, which is
4768 -- present on all client partitions (this is E.2.3(18)).
4770 -- Note that AI12-0002 may make it illegal at some point to have
4771 -- stream attributes defined in an RCI unit, in which case this
4772 -- special case will become unnecessary. In the meantime, there
4773 -- is known application code in production that depends on this
4774 -- being possible, so we definitely cannot eliminate the body in
4775 -- the case of nested instances for the time being.
4777 -- When we generate a nested instance body, calling stubs for any
4778 -- relevant subprogram will be inserted immediately after the
4779 -- subprogram declarations, and will take precedence over the
4780 -- subsequent (original) body. (The stub and original body will be
4781 -- complete homographs, but this is permitted in an instance).
4782 -- (Could we do better and remove the original body???)
4784 if Distribution_Stub_Mode = Generate_Caller_Stub_Body
4785 and then Comes_From_Source (N)
4786 and then Nkind (Parent (N)) = N_Compilation_Unit
4787 then
4788 Needs_Body := False;
4789 end if;
4791 if Needs_Body then
4792 -- Indicate that the enclosing scopes contain an instantiation,
4793 -- and that cleanup actions should be delayed until after the
4794 -- instance body is expanded.
4796 Check_Forward_Instantiation (Gen_Decl);
4797 if Nkind (N) = N_Package_Instantiation then
4798 declare
4799 Enclosing_Master : Entity_Id;
4801 begin
4802 -- Loop to search enclosing masters
4804 Enclosing_Master := Current_Scope;
4805 Scope_Loop : while Enclosing_Master /= Standard_Standard loop
4806 if Ekind (Enclosing_Master) = E_Package then
4807 if Is_Compilation_Unit (Enclosing_Master) then
4808 if In_Package_Body (Enclosing_Master) then
4809 Set_Delay_Subprogram_Descriptors
4810 (Body_Entity (Enclosing_Master));
4811 else
4812 Set_Delay_Subprogram_Descriptors
4813 (Enclosing_Master);
4814 end if;
4816 exit Scope_Loop;
4818 else
4819 Enclosing_Master := Scope (Enclosing_Master);
4820 end if;
4822 elsif Is_Generic_Unit (Enclosing_Master)
4823 or else Ekind (Enclosing_Master) = E_Void
4824 then
4825 -- Cleanup actions will eventually be performed on the
4826 -- enclosing subprogram or package instance, if any.
4827 -- Enclosing scope is void in the formal part of a
4828 -- generic subprogram.
4830 exit Scope_Loop;
4832 else
4833 if Ekind (Enclosing_Master) = E_Entry
4834 and then
4835 Ekind (Scope (Enclosing_Master)) = E_Protected_Type
4836 then
4837 if not Expander_Active then
4838 exit Scope_Loop;
4839 else
4840 Enclosing_Master :=
4841 Protected_Body_Subprogram (Enclosing_Master);
4842 end if;
4843 end if;
4845 Set_Delay_Cleanups (Enclosing_Master);
4847 while Ekind (Enclosing_Master) = E_Block loop
4848 Enclosing_Master := Scope (Enclosing_Master);
4849 end loop;
4851 if Is_Subprogram (Enclosing_Master) then
4852 Set_Delay_Subprogram_Descriptors (Enclosing_Master);
4854 elsif Is_Task_Type (Enclosing_Master) then
4855 declare
4856 TBP : constant Node_Id :=
4857 Get_Task_Body_Procedure
4858 (Enclosing_Master);
4859 begin
4860 if Present (TBP) then
4861 Set_Delay_Subprogram_Descriptors (TBP);
4862 Set_Delay_Cleanups (TBP);
4863 end if;
4864 end;
4865 end if;
4867 exit Scope_Loop;
4868 end if;
4869 end loop Scope_Loop;
4870 end;
4872 -- Make entry in table
4874 Add_Pending_Instantiation (N, Act_Decl);
4875 end if;
4876 end if;
4878 Set_Categorization_From_Pragmas (Act_Decl);
4880 if Parent_Installed then
4881 Hide_Current_Scope;
4882 end if;
4884 Set_Instance_Spec (N, Act_Decl);
4886 -- If not a compilation unit, insert the package declaration before
4887 -- the original instantiation node.
4889 if Nkind (Parent (N)) /= N_Compilation_Unit then
4890 Mark_Rewrite_Insertion (Act_Decl);
4891 Insert_Before (N, Act_Decl);
4893 if Has_Aspects (N) then
4894 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4896 -- The pragma created for a Default_Storage_Pool aspect must
4897 -- appear ahead of the declarations in the instance spec.
4898 -- Analysis has placed it after the instance node, so remove
4899 -- it and reinsert it properly now.
4901 declare
4902 ASN : constant Node_Id := First (Aspect_Specifications (N));
4903 A_Name : constant Name_Id := Chars (Identifier (ASN));
4904 Decl : Node_Id;
4906 begin
4907 if A_Name = Name_Default_Storage_Pool then
4908 if No (Visible_Declarations (Act_Spec)) then
4909 Set_Visible_Declarations (Act_Spec, New_List);
4910 end if;
4912 Decl := Next (N);
4913 while Present (Decl) loop
4914 if Nkind (Decl) = N_Pragma then
4915 Remove (Decl);
4916 Prepend (Decl, Visible_Declarations (Act_Spec));
4917 exit;
4918 end if;
4920 Next (Decl);
4921 end loop;
4922 end if;
4923 end;
4924 end if;
4926 Analyze (Act_Decl);
4928 -- For an instantiation that is a compilation unit, place
4929 -- declaration on current node so context is complete for analysis
4930 -- (including nested instantiations). If this is the main unit,
4931 -- the declaration eventually replaces the instantiation node.
4932 -- If the instance body is created later, it replaces the
4933 -- instance node, and the declaration is attached to it
4934 -- (see Build_Instance_Compilation_Unit_Nodes).
4936 else
4937 if Cunit_Entity (Current_Sem_Unit) = Defining_Entity (N) then
4939 -- The entity for the current unit is the newly created one,
4940 -- and all semantic information is attached to it.
4942 Set_Cunit_Entity (Current_Sem_Unit, Act_Decl_Id);
4944 -- If this is the main unit, replace the main entity as well
4946 if Current_Sem_Unit = Main_Unit then
4947 Main_Unit_Entity := Act_Decl_Id;
4948 end if;
4949 end if;
4951 Set_Unit (Parent (N), Act_Decl);
4952 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
4953 Set_Package_Instantiation (Act_Decl_Id, N);
4955 -- Process aspect specifications of the instance node, if any, to
4956 -- take into account categorization pragmas before analyzing the
4957 -- instance.
4959 if Has_Aspects (N) then
4960 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4961 end if;
4963 Analyze (Act_Decl);
4964 Set_Unit (Parent (N), N);
4965 Set_Body_Required (Parent (N), False);
4967 -- We never need elaboration checks on instantiations, since by
4968 -- definition, the body instantiation is elaborated at the same
4969 -- time as the spec instantiation.
4971 if Legacy_Elaboration_Checks then
4972 Set_Kill_Elaboration_Checks (Act_Decl_Id);
4973 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
4974 end if;
4975 end if;
4977 if Legacy_Elaboration_Checks then
4978 Check_Elab_Instantiation (N);
4979 end if;
4981 -- Save the scenario for later examination by the ABE Processing
4982 -- phase.
4984 Record_Elaboration_Scenario (N);
4986 -- The instantiation results in a guaranteed ABE
4988 if Is_Known_Guaranteed_ABE (N) and then Needs_Body then
4989 -- Do not instantiate the corresponding body because gigi cannot
4990 -- handle certain types of premature instantiations.
4992 Remove_Dead_Instance (N);
4994 -- Create completing bodies for all subprogram declarations since
4995 -- their real bodies will not be instantiated.
4997 Provide_Completing_Bodies (Instance_Spec (N));
4998 end if;
5000 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
5002 Set_First_Private_Entity (Defining_Unit_Name (Unit_Renaming),
5003 First_Private_Entity (Act_Decl_Id));
5005 -- If the instantiation will receive a body, the unit will be
5006 -- transformed into a package body, and receive its own elaboration
5007 -- entity. Otherwise, the nature of the unit is now a package
5008 -- declaration.
5010 if Nkind (Parent (N)) = N_Compilation_Unit
5011 and then not Needs_Body
5012 then
5013 Rewrite (N, Act_Decl);
5014 end if;
5016 if Present (Corresponding_Body (Gen_Decl))
5017 or else Unit_Requires_Body (Gen_Unit)
5018 then
5019 Set_Has_Completion (Act_Decl_Id);
5020 end if;
5022 Check_Formal_Packages (Act_Decl_Id);
5024 Restore_Hidden_Primitives (Vis_Prims_List);
5025 Restore_Private_Views (Act_Decl_Id);
5027 Inherit_Context (Gen_Decl, N);
5029 if Parent_Installed then
5030 Remove_Parent;
5031 end if;
5033 Restore_Env;
5034 Env_Installed := False;
5035 end if;
5037 Validate_Categorization_Dependency (N, Act_Decl_Id);
5039 -- There used to be a check here to prevent instantiations in local
5040 -- contexts if the No_Local_Allocators restriction was active. This
5041 -- check was removed by a binding interpretation in AI-95-00130/07,
5042 -- but we retain the code for documentation purposes.
5044 -- if Ekind (Act_Decl_Id) /= E_Void
5045 -- and then not Is_Library_Level_Entity (Act_Decl_Id)
5046 -- then
5047 -- Check_Restriction (No_Local_Allocators, N);
5048 -- end if;
5050 if Inline_Now then
5051 Inline_Instance_Body (N, Gen_Unit, Act_Decl);
5052 end if;
5054 -- Check that if N is an instantiation of System.Dim_Float_IO or
5055 -- System.Dim_Integer_IO, the formal type has a dimension system.
5057 if Nkind (N) = N_Package_Instantiation
5058 and then Is_Dim_IO_Package_Instantiation (N)
5059 then
5060 declare
5061 Assoc : constant Node_Id := First (Generic_Associations (N));
5062 begin
5063 if not Has_Dimension_System
5064 (Etype (Explicit_Generic_Actual_Parameter (Assoc)))
5065 then
5066 Error_Msg_N ("type with a dimension system expected", Assoc);
5067 end if;
5068 end;
5069 end if;
5071 <<Leave>>
5072 if Has_Aspects (N) and then Nkind (Parent (N)) /= N_Compilation_Unit then
5073 Analyze_Aspect_Specifications (N, Act_Decl_Id);
5074 end if;
5076 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
5077 Restore_Ghost_Region (Saved_GM, Saved_IGR);
5078 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
5079 Style_Check := Saved_Style_Check;
5081 exception
5082 when Instantiation_Error =>
5083 if Parent_Installed then
5084 Remove_Parent;
5085 end if;
5087 if Env_Installed then
5088 Restore_Env;
5089 end if;
5091 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
5092 Restore_Ghost_Region (Saved_GM, Saved_IGR);
5093 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
5094 Style_Check := Saved_Style_Check;
5095 end Analyze_Package_Instantiation;
5097 --------------------------
5098 -- Inline_Instance_Body --
5099 --------------------------
5101 -- WARNING: This routine manages SPARK regions. Return statements must be
5102 -- replaced by gotos which jump to the end of the routine and restore the
5103 -- SPARK mode.
5105 procedure Inline_Instance_Body
5106 (N : Node_Id;
5107 Gen_Unit : Entity_Id;
5108 Act_Decl : Node_Id)
5110 Config_Attrs : constant Config_Switches_Type := Save_Config_Switches;
5112 Curr_Comp : constant Node_Id := Cunit (Current_Sem_Unit);
5113 Curr_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
5114 Gen_Comp : constant Entity_Id :=
5115 Cunit_Entity (Get_Source_Unit (Gen_Unit));
5117 Scope_Stack_Depth : constant Pos :=
5118 Scope_Stack.Last - Scope_Stack.First + 1;
5120 Inner_Scopes : array (1 .. Scope_Stack_Depth) of Entity_Id;
5121 Instances : array (1 .. Scope_Stack_Depth) of Entity_Id;
5122 Use_Clauses : array (1 .. Scope_Stack_Depth) of Node_Id;
5124 Curr_Scope : Entity_Id := Empty;
5125 List : Elist_Id := No_Elist; -- init to avoid warning
5126 N_Instances : Nat := 0;
5127 Num_Inner : Nat := 0;
5128 Num_Scopes : Nat := 0;
5129 Removed : Boolean := False;
5130 S : Entity_Id;
5131 Vis : Boolean;
5133 begin
5134 -- Case of generic unit defined in another unit. We must remove the
5135 -- complete context of the current unit to install that of the generic.
5137 if Gen_Comp /= Cunit_Entity (Current_Sem_Unit) then
5139 -- Loop through enclosing scopes until we reach a generic instance,
5140 -- package body, or subprogram.
5142 S := Current_Scope;
5143 while Present (S) and then S /= Standard_Standard loop
5145 -- Save use clauses from enclosing scopes into Use_Clauses
5147 loop
5148 Num_Scopes := Num_Scopes + 1;
5150 Use_Clauses (Num_Scopes) :=
5151 (Scope_Stack.Table
5152 (Scope_Stack.Last - Num_Scopes + 1).First_Use_Clause);
5153 End_Use_Clauses (Use_Clauses (Num_Scopes));
5155 exit when Scope_Stack.Last - Num_Scopes + 1 = Scope_Stack.First
5156 or else Scope_Stack.Table
5157 (Scope_Stack.Last - Num_Scopes).Entity = Scope (S);
5158 end loop;
5160 exit when Is_Generic_Instance (S)
5161 and then (In_Package_Body (S)
5162 or else Ekind (S) = E_Procedure
5163 or else Ekind (S) = E_Function);
5164 S := Scope (S);
5165 end loop;
5167 Vis := Is_Immediately_Visible (Gen_Comp);
5169 -- Find and save all enclosing instances
5171 S := Current_Scope;
5173 while Present (S)
5174 and then S /= Standard_Standard
5175 loop
5176 if Is_Generic_Instance (S) then
5177 N_Instances := N_Instances + 1;
5178 Instances (N_Instances) := S;
5180 exit when In_Package_Body (S);
5181 end if;
5183 S := Scope (S);
5184 end loop;
5186 -- Remove context of current compilation unit, unless we are within a
5187 -- nested package instantiation, in which case the context has been
5188 -- removed previously.
5190 -- If current scope is the body of a child unit, remove context of
5191 -- spec as well. If an enclosing scope is an instance body, the
5192 -- context has already been removed, but the entities in the body
5193 -- must be made invisible as well.
5195 S := Current_Scope;
5196 while Present (S) and then S /= Standard_Standard loop
5197 if Is_Generic_Instance (S)
5198 and then (In_Package_Body (S)
5199 or else Ekind (S) in E_Procedure | E_Function)
5200 then
5201 -- We still have to remove the entities of the enclosing
5202 -- instance from direct visibility.
5204 declare
5205 E : Entity_Id;
5206 begin
5207 E := First_Entity (S);
5208 while Present (E) loop
5209 Set_Is_Immediately_Visible (E, False);
5210 Next_Entity (E);
5211 end loop;
5212 end;
5214 exit;
5215 end if;
5217 if S = Curr_Unit
5218 or else (Ekind (Curr_Unit) = E_Package_Body
5219 and then S = Spec_Entity (Curr_Unit))
5220 or else (Ekind (Curr_Unit) = E_Subprogram_Body
5221 and then S = Corresponding_Spec
5222 (Unit_Declaration_Node (Curr_Unit)))
5223 then
5224 Removed := True;
5226 -- Remove entities in current scopes from visibility, so that
5227 -- instance body is compiled in a clean environment.
5229 List := Save_Scope_Stack (Handle_Use => False);
5231 if Is_Child_Unit (S) then
5233 -- Remove child unit from stack, as well as inner scopes.
5234 -- Removing the context of a child unit removes parent units
5235 -- as well.
5237 while Current_Scope /= S loop
5238 Num_Inner := Num_Inner + 1;
5239 Inner_Scopes (Num_Inner) := Current_Scope;
5240 Pop_Scope;
5241 end loop;
5243 Pop_Scope;
5244 Remove_Context (Curr_Comp);
5245 Curr_Scope := S;
5247 else
5248 Remove_Context (Curr_Comp);
5249 end if;
5251 if Ekind (Curr_Unit) = E_Package_Body then
5252 Remove_Context (Library_Unit (Curr_Comp));
5253 end if;
5254 end if;
5256 S := Scope (S);
5257 end loop;
5259 pragma Assert (Num_Inner < Num_Scopes);
5261 Push_Scope (Standard_Standard);
5262 Scope_Stack.Table (Scope_Stack.Last).Is_Active_Stack_Base := True;
5264 -- The inlined package body is analyzed with the configuration state
5265 -- of the context prior to the scope manipulations performed above.
5267 -- ??? shouldn't this also use the warning state of the context prior
5268 -- to the scope manipulations?
5270 Instantiate_Package_Body
5271 (Body_Info =>
5272 ((Act_Decl => Act_Decl,
5273 Config_Switches => Config_Attrs,
5274 Current_Sem_Unit => Current_Sem_Unit,
5275 Expander_Status => Expander_Active,
5276 Inst_Node => N,
5277 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
5278 Scope_Suppress => Scope_Suppress,
5279 Warnings => Save_Warnings)),
5280 Inlined_Body => True);
5282 Pop_Scope;
5284 -- Restore context
5286 Set_Is_Immediately_Visible (Gen_Comp, Vis);
5288 -- Reset Generic_Instance flag so that use clauses can be installed
5289 -- in the proper order. (See Use_One_Package for effect of enclosing
5290 -- instances on processing of use clauses).
5292 for J in 1 .. N_Instances loop
5293 Set_Is_Generic_Instance (Instances (J), False);
5294 end loop;
5296 if Removed then
5297 Install_Context (Curr_Comp, Chain => False);
5299 if Present (Curr_Scope)
5300 and then Is_Child_Unit (Curr_Scope)
5301 then
5302 Push_Scope (Curr_Scope);
5303 Set_Is_Immediately_Visible (Curr_Scope);
5305 -- Finally, restore inner scopes as well
5307 for J in reverse 1 .. Num_Inner loop
5308 Push_Scope (Inner_Scopes (J));
5309 end loop;
5310 end if;
5312 Restore_Scope_Stack (List, Handle_Use => False);
5314 if Present (Curr_Scope)
5315 and then
5316 (In_Private_Part (Curr_Scope)
5317 or else In_Package_Body (Curr_Scope))
5318 then
5319 -- Install private declaration of ancestor units, which are
5320 -- currently available. Restore_Scope_Stack and Install_Context
5321 -- only install the visible part of parents.
5323 declare
5324 Par : Entity_Id;
5325 begin
5326 Par := Scope (Curr_Scope);
5327 while (Present (Par)) and then Par /= Standard_Standard loop
5328 Install_Private_Declarations (Par);
5329 Par := Scope (Par);
5330 end loop;
5331 end;
5332 end if;
5333 end if;
5335 -- Restore use clauses. For a child unit, use clauses in the parents
5336 -- are restored when installing the context, so only those in inner
5337 -- scopes (and those local to the child unit itself) need to be
5338 -- installed explicitly.
5340 if Is_Child_Unit (Curr_Unit) and then Removed then
5341 for J in reverse 1 .. Num_Inner + 1 loop
5342 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
5343 Use_Clauses (J);
5344 Install_Use_Clauses (Use_Clauses (J));
5345 end loop;
5347 else
5348 for J in reverse 1 .. Num_Scopes loop
5349 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
5350 Use_Clauses (J);
5351 Install_Use_Clauses (Use_Clauses (J));
5352 end loop;
5353 end if;
5355 -- Restore status of instances. If one of them is a body, make its
5356 -- local entities visible again.
5358 declare
5359 E : Entity_Id;
5360 Inst : Entity_Id;
5362 begin
5363 for J in 1 .. N_Instances loop
5364 Inst := Instances (J);
5365 Set_Is_Generic_Instance (Inst, True);
5367 if In_Package_Body (Inst)
5368 or else Ekind (S) in E_Procedure | E_Function
5369 then
5370 E := First_Entity (Instances (J));
5371 while Present (E) loop
5372 Set_Is_Immediately_Visible (E);
5373 Next_Entity (E);
5374 end loop;
5375 end if;
5376 end loop;
5377 end;
5379 -- If generic unit is in current unit, current context is correct. Note
5380 -- that the context is guaranteed to carry the correct SPARK_Mode as no
5381 -- enclosing scopes were removed.
5383 else
5384 Instantiate_Package_Body
5385 (Body_Info =>
5386 ((Act_Decl => Act_Decl,
5387 Config_Switches => Save_Config_Switches,
5388 Current_Sem_Unit => Current_Sem_Unit,
5389 Expander_Status => Expander_Active,
5390 Inst_Node => N,
5391 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
5392 Scope_Suppress => Scope_Suppress,
5393 Warnings => Save_Warnings)),
5394 Inlined_Body => True);
5395 end if;
5396 end Inline_Instance_Body;
5398 -------------------------------------
5399 -- Analyze_Procedure_Instantiation --
5400 -------------------------------------
5402 procedure Analyze_Procedure_Instantiation (N : Node_Id) is
5403 begin
5404 Analyze_Subprogram_Instantiation (N, E_Procedure);
5405 end Analyze_Procedure_Instantiation;
5407 -----------------------------------
5408 -- Need_Subprogram_Instance_Body --
5409 -----------------------------------
5411 function Need_Subprogram_Instance_Body
5412 (N : Node_Id;
5413 Subp : Entity_Id) return Boolean
5415 function Is_Inlined_Or_Child_Of_Inlined (E : Entity_Id) return Boolean;
5416 -- Return True if E is an inlined subprogram, an inlined renaming or a
5417 -- subprogram nested in an inlined subprogram. The inlining machinery
5418 -- totally disregards nested subprograms since it considers that they
5419 -- will always be compiled if the parent is (see Inline.Is_Nested).
5421 ------------------------------------
5422 -- Is_Inlined_Or_Child_Of_Inlined --
5423 ------------------------------------
5425 function Is_Inlined_Or_Child_Of_Inlined (E : Entity_Id) return Boolean is
5426 Scop : Entity_Id;
5428 begin
5429 if Is_Inlined (E) or else Is_Inlined (Alias (E)) then
5430 return True;
5431 end if;
5433 Scop := Scope (E);
5434 while Scop /= Standard_Standard loop
5435 if Is_Subprogram (Scop) and then Is_Inlined (Scop) then
5436 return True;
5437 end if;
5439 Scop := Scope (Scop);
5440 end loop;
5442 return False;
5443 end Is_Inlined_Or_Child_Of_Inlined;
5445 begin
5446 -- Must be in the main unit or inlined (or child of inlined)
5448 if (Is_In_Main_Unit (N) or else Is_Inlined_Or_Child_Of_Inlined (Subp))
5450 -- Must be generating code or analyzing code in GNATprove mode
5452 and then (Operating_Mode = Generate_Code
5453 or else (Operating_Mode = Check_Semantics
5454 and then GNATprove_Mode))
5456 -- The body is needed when generating code (full expansion) and in
5457 -- in GNATprove mode (special expansion) for formal verification of
5458 -- the body itself.
5460 and then (Expander_Active or GNATprove_Mode)
5462 -- No point in inlining if ABE is inevitable
5464 and then not Is_Known_Guaranteed_ABE (N)
5466 -- Or if subprogram is eliminated
5468 and then not Is_Eliminated (Subp)
5469 then
5470 Add_Pending_Instantiation (N, Unit_Declaration_Node (Subp));
5471 return True;
5473 -- Here if not inlined, or we ignore the inlining
5475 else
5476 return False;
5477 end if;
5478 end Need_Subprogram_Instance_Body;
5480 --------------------------------------
5481 -- Analyze_Subprogram_Instantiation --
5482 --------------------------------------
5484 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
5485 -- must be replaced by gotos which jump to the end of the routine in order
5486 -- to restore the Ghost and SPARK modes.
5488 procedure Analyze_Subprogram_Instantiation
5489 (N : Node_Id;
5490 K : Entity_Kind)
5492 Errs : constant Nat := Serious_Errors_Detected;
5493 Gen_Id : constant Node_Id := Name (N);
5494 Inst_Id : constant Entity_Id := Defining_Entity (N);
5495 Anon_Id : constant Entity_Id :=
5496 Make_Defining_Identifier (Sloc (Inst_Id),
5497 Chars => New_External_Name (Chars (Inst_Id), 'R'));
5498 Loc : constant Source_Ptr := Sloc (N);
5500 Act_Decl_Id : Entity_Id := Empty; -- init to avoid warning
5501 Act_Decl : Node_Id;
5502 Act_Spec : Node_Id;
5503 Act_Tree : Node_Id;
5505 Env_Installed : Boolean := False;
5506 Gen_Unit : Entity_Id;
5507 Gen_Decl : Node_Id;
5508 Pack_Id : Entity_Id;
5509 Parent_Installed : Boolean := False;
5511 Renaming_List : List_Id;
5512 -- The list of declarations that link formals and actuals of the
5513 -- instance. These are subtype declarations for formal types, and
5514 -- renaming declarations for other formals. The subprogram declaration
5515 -- for the instance is then appended to the list, and the last item on
5516 -- the list is the renaming declaration for the instance.
5518 procedure Analyze_Instance_And_Renamings;
5519 -- The instance must be analyzed in a context that includes the mappings
5520 -- of generic parameters into actuals. We create a package declaration
5521 -- for this purpose, and a subprogram with an internal name within the
5522 -- package. The subprogram instance is simply an alias for the internal
5523 -- subprogram, declared in the current scope.
5525 procedure Build_Subprogram_Renaming;
5526 -- If the subprogram is recursive, there are occurrences of the name of
5527 -- the generic within the body, which must resolve to the current
5528 -- instance. We add a renaming declaration after the declaration, which
5529 -- is available in the instance body, as well as in the analysis of
5530 -- aspects that appear in the generic. This renaming declaration is
5531 -- inserted after the instance declaration which it renames.
5533 ------------------------------------
5534 -- Analyze_Instance_And_Renamings --
5535 ------------------------------------
5537 procedure Analyze_Instance_And_Renamings is
5538 Def_Ent : constant Entity_Id := Defining_Entity (N);
5539 Pack_Decl : Node_Id;
5541 begin
5542 if Nkind (Parent (N)) = N_Compilation_Unit then
5544 -- For the case of a compilation unit, the container package has
5545 -- the same name as the instantiation, to insure that the binder
5546 -- calls the elaboration procedure with the right name. Copy the
5547 -- entity of the instance, which may have compilation level flags
5548 -- (e.g. Is_Child_Unit) set.
5550 Pack_Id := New_Copy (Def_Ent);
5552 else
5553 -- Otherwise we use the name of the instantiation concatenated
5554 -- with its source position to ensure uniqueness if there are
5555 -- several instantiations with the same name.
5557 Pack_Id :=
5558 Make_Defining_Identifier (Loc,
5559 Chars => New_External_Name
5560 (Related_Id => Chars (Def_Ent),
5561 Suffix => "GP",
5562 Suffix_Index => Source_Offset (Sloc (Def_Ent))));
5563 end if;
5565 Pack_Decl :=
5566 Make_Package_Declaration (Loc,
5567 Specification => Make_Package_Specification (Loc,
5568 Defining_Unit_Name => Pack_Id,
5569 Visible_Declarations => Renaming_List,
5570 End_Label => Empty));
5572 Set_Instance_Spec (N, Pack_Decl);
5573 Set_Is_Generic_Instance (Pack_Id);
5574 Set_Debug_Info_Needed (Pack_Id);
5576 -- Case of not a compilation unit
5578 if Nkind (Parent (N)) /= N_Compilation_Unit then
5579 Mark_Rewrite_Insertion (Pack_Decl);
5580 Insert_Before (N, Pack_Decl);
5581 Set_Has_Completion (Pack_Id);
5583 -- Case of an instantiation that is a compilation unit
5585 -- Place declaration on current node so context is complete for
5586 -- analysis (including nested instantiations), and for use in a
5587 -- context_clause (see Analyze_With_Clause).
5589 else
5590 Set_Unit (Parent (N), Pack_Decl);
5591 Set_Parent_Spec (Pack_Decl, Parent_Spec (N));
5592 end if;
5594 Analyze (Pack_Decl);
5595 Check_Formal_Packages (Pack_Id);
5597 -- Body of the enclosing package is supplied when instantiating the
5598 -- subprogram body, after semantic analysis is completed.
5600 if Nkind (Parent (N)) = N_Compilation_Unit then
5602 -- Remove package itself from visibility, so it does not
5603 -- conflict with subprogram.
5605 Set_Name_Entity_Id (Chars (Pack_Id), Homonym (Pack_Id));
5607 -- Set name and scope of internal subprogram so that the proper
5608 -- external name will be generated. The proper scope is the scope
5609 -- of the wrapper package. We need to generate debugging info for
5610 -- the internal subprogram, so set flag accordingly.
5612 Set_Chars (Anon_Id, Chars (Defining_Entity (N)));
5613 Set_Scope (Anon_Id, Scope (Pack_Id));
5615 -- Mark wrapper package as referenced, to avoid spurious warnings
5616 -- if the instantiation appears in various with_ clauses of
5617 -- subunits of the main unit.
5619 Set_Referenced (Pack_Id);
5620 end if;
5622 Set_Is_Generic_Instance (Anon_Id);
5623 Set_Debug_Info_Needed (Anon_Id);
5624 Act_Decl_Id := New_Copy (Anon_Id);
5626 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
5627 Set_Chars (Act_Decl_Id, Chars (Defining_Entity (N)));
5628 Set_Sloc (Act_Decl_Id, Sloc (Defining_Entity (N)));
5630 -- Subprogram instance comes from source only if generic does
5632 Preserve_Comes_From_Source (Act_Decl_Id, Gen_Unit);
5634 -- If the instance is a child unit, mark the Id accordingly. Mark
5635 -- the anonymous entity as well, which is the real subprogram and
5636 -- which is used when the instance appears in a context clause.
5637 -- Similarly, propagate the Is_Eliminated flag to handle properly
5638 -- nested eliminated subprograms.
5640 Set_Is_Child_Unit (Act_Decl_Id, Is_Child_Unit (Defining_Entity (N)));
5641 Set_Is_Child_Unit (Anon_Id, Is_Child_Unit (Defining_Entity (N)));
5642 New_Overloaded_Entity (Act_Decl_Id);
5643 Check_Eliminated (Act_Decl_Id);
5644 Set_Is_Eliminated (Anon_Id, Is_Eliminated (Act_Decl_Id));
5646 if Nkind (Parent (N)) = N_Compilation_Unit then
5648 -- In compilation unit case, kill elaboration checks on the
5649 -- instantiation, since they are never needed - the body is
5650 -- instantiated at the same point as the spec.
5652 if Legacy_Elaboration_Checks then
5653 Set_Kill_Elaboration_Checks (Act_Decl_Id);
5654 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
5655 end if;
5657 Set_Is_Compilation_Unit (Anon_Id);
5658 Set_Cunit_Entity (Current_Sem_Unit, Pack_Id);
5659 end if;
5661 -- The instance is not a freezing point for the new subprogram.
5662 -- The anonymous subprogram may have a freeze node, created for
5663 -- some delayed aspects. This freeze node must not be inherited
5664 -- by the visible subprogram entity.
5666 Set_Is_Frozen (Act_Decl_Id, False);
5667 Set_Freeze_Node (Act_Decl_Id, Empty);
5669 if Nkind (Defining_Entity (N)) = N_Defining_Operator_Symbol then
5670 Valid_Operator_Definition (Act_Decl_Id);
5671 end if;
5673 Set_Alias (Act_Decl_Id, Anon_Id);
5674 Set_Has_Completion (Act_Decl_Id);
5675 Set_Related_Instance (Pack_Id, Act_Decl_Id);
5677 if Nkind (Parent (N)) = N_Compilation_Unit then
5678 Set_Body_Required (Parent (N), False);
5679 end if;
5680 end Analyze_Instance_And_Renamings;
5682 -------------------------------
5683 -- Build_Subprogram_Renaming --
5684 -------------------------------
5686 procedure Build_Subprogram_Renaming is
5687 Renaming_Decl : Node_Id;
5688 Unit_Renaming : Node_Id;
5690 begin
5691 Unit_Renaming :=
5692 Make_Subprogram_Renaming_Declaration (Loc,
5693 Specification =>
5694 Copy_Generic_Node
5695 (Specification (Original_Node (Gen_Decl)),
5696 Empty,
5697 Instantiating => True),
5698 Name => New_Occurrence_Of (Anon_Id, Loc));
5700 -- The generic may be a child unit. The renaming needs an identifier
5701 -- with the proper name.
5703 Set_Defining_Unit_Name (Specification (Unit_Renaming),
5704 Make_Defining_Identifier (Loc, Chars (Gen_Unit)));
5706 -- If there is a formal subprogram with the same name as the unit
5707 -- itself, do not add this renaming declaration, to prevent
5708 -- ambiguities when there is a call with that name in the body.
5710 Renaming_Decl := First (Renaming_List);
5711 while Present (Renaming_Decl) loop
5712 if Nkind (Renaming_Decl) = N_Subprogram_Renaming_Declaration
5713 and then
5714 Chars (Defining_Entity (Renaming_Decl)) = Chars (Gen_Unit)
5715 then
5716 exit;
5717 end if;
5719 Next (Renaming_Decl);
5720 end loop;
5722 if No (Renaming_Decl) then
5723 Append (Unit_Renaming, Renaming_List);
5724 end if;
5725 end Build_Subprogram_Renaming;
5727 -- Local variables
5729 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
5730 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
5731 Saved_ISMP : constant Boolean :=
5732 Ignore_SPARK_Mode_Pragmas_In_Instance;
5733 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
5734 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
5735 -- Save the Ghost and SPARK mode-related data to restore on exit
5737 Vis_Prims_List : Elist_Id := No_Elist;
5738 -- List of primitives made temporarily visible in the instantiation
5739 -- to match the visibility of the formal type
5741 -- Start of processing for Analyze_Subprogram_Instantiation
5743 begin
5744 -- Preserve relevant elaboration-related attributes of the context which
5745 -- are no longer available or very expensive to recompute once analysis,
5746 -- resolution, and expansion are over.
5748 Mark_Elaboration_Attributes
5749 (N_Id => N,
5750 Checks => True,
5751 Level => True,
5752 Modes => True,
5753 Warnings => True);
5755 -- Very first thing: check for special Text_IO unit in case we are
5756 -- instantiating one of the children of [[Wide_]Wide_]Text_IO. Of course
5757 -- such an instantiation is bogus (these are packages, not subprograms),
5758 -- but we get a better error message if we do this.
5760 Check_Text_IO_Special_Unit (Gen_Id);
5762 -- Make node global for error reporting
5764 Instantiation_Node := N;
5766 -- For package instantiations we turn off style checks, because they
5767 -- will have been emitted in the generic. For subprogram instantiations
5768 -- we want to apply at least the check on overriding indicators so we
5769 -- do not modify the style check status.
5771 -- The renaming declarations for the actuals do not come from source and
5772 -- will not generate spurious warnings.
5774 Preanalyze_Actuals (N);
5776 Init_Env;
5777 Env_Installed := True;
5778 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
5779 Gen_Unit := Entity (Gen_Id);
5781 -- A subprogram instantiation is Ghost when it is subject to pragma
5782 -- Ghost or the generic template is Ghost. Set the mode now to ensure
5783 -- that any nodes generated during analysis and expansion are marked as
5784 -- Ghost.
5786 Mark_And_Set_Ghost_Instantiation (N, Gen_Unit);
5788 Generate_Reference (Gen_Unit, Gen_Id);
5790 if Nkind (Gen_Id) = N_Identifier
5791 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
5792 then
5793 Error_Msg_NE
5794 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
5795 end if;
5797 if Etype (Gen_Unit) = Any_Type then
5798 Restore_Env;
5799 goto Leave;
5800 end if;
5802 -- Verify that it is a generic subprogram of the right kind, and that
5803 -- it does not lead to a circular instantiation.
5805 if K = E_Procedure and then Ekind (Gen_Unit) /= E_Generic_Procedure then
5806 Error_Msg_NE
5807 ("& is not the name of a generic procedure", Gen_Id, Gen_Unit);
5809 elsif K = E_Function and then Ekind (Gen_Unit) /= E_Generic_Function then
5810 Error_Msg_NE
5811 ("& is not the name of a generic function", Gen_Id, Gen_Unit);
5813 elsif In_Open_Scopes (Gen_Unit) then
5814 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
5816 else
5817 Mutate_Ekind (Inst_Id, K);
5818 Set_Scope (Inst_Id, Current_Scope);
5820 Set_Entity (Gen_Id, Gen_Unit);
5822 if In_Extended_Main_Source_Unit (N) then
5823 Set_Is_Instantiated (Gen_Unit);
5824 Generate_Reference (Gen_Unit, N);
5825 end if;
5827 -- If renaming, get original unit
5829 if Present (Renamed_Entity (Gen_Unit))
5830 and then Is_Generic_Subprogram (Renamed_Entity (Gen_Unit))
5831 then
5832 Gen_Unit := Renamed_Entity (Gen_Unit);
5833 Set_Is_Instantiated (Gen_Unit);
5834 Generate_Reference (Gen_Unit, N);
5835 end if;
5837 if Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
5838 Error_Msg_Node_2 := Current_Scope;
5839 Error_Msg_NE
5840 ("circular instantiation: & instantiated in &!", N, Gen_Unit);
5841 Circularity_Detected := True;
5842 Restore_Hidden_Primitives (Vis_Prims_List);
5843 goto Leave;
5844 end if;
5846 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
5848 -- Initialize renamings map, for error checking
5850 Generic_Renamings.Set_Last (0);
5851 Generic_Renamings_HTable.Reset;
5853 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
5855 -- Copy original generic tree, to produce text for instantiation
5857 Act_Tree :=
5858 Copy_Generic_Node
5859 (Original_Node (Gen_Decl), Empty, Instantiating => True);
5861 -- Inherit overriding indicator from instance node
5863 Act_Spec := Specification (Act_Tree);
5864 Set_Must_Override (Act_Spec, Must_Override (N));
5865 Set_Must_Not_Override (Act_Spec, Must_Not_Override (N));
5867 Renaming_List :=
5868 Analyze_Associations
5869 (I_Node => N,
5870 Formals => Generic_Formal_Declarations (Act_Tree),
5871 F_Copy => Generic_Formal_Declarations (Gen_Decl));
5873 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
5875 -- The subprogram itself cannot contain a nested instance, so the
5876 -- current parent is left empty.
5878 Set_Instance_Env (Gen_Unit, Empty);
5880 -- Build the subprogram declaration, which does not appear in the
5881 -- generic template, and give it a sloc consistent with that of the
5882 -- template.
5884 Set_Defining_Unit_Name (Act_Spec, Anon_Id);
5885 Set_Generic_Parent (Act_Spec, Gen_Unit);
5886 Act_Decl :=
5887 Make_Subprogram_Declaration (Sloc (Act_Spec),
5888 Specification => Act_Spec);
5890 -- The aspects have been copied previously, but they have to be
5891 -- linked explicitly to the new subprogram declaration. Explicit
5892 -- pre/postconditions on the instance are analyzed below, in a
5893 -- separate step.
5895 Move_Aspects (Act_Tree, To => Act_Decl);
5896 Set_Categorization_From_Pragmas (Act_Decl);
5898 if Parent_Installed then
5899 Hide_Current_Scope;
5900 end if;
5902 Append (Act_Decl, Renaming_List);
5904 -- Contract-related source pragmas that follow a generic subprogram
5905 -- must be instantiated explicitly because they are not part of the
5906 -- subprogram template.
5908 Instantiate_Subprogram_Contract
5909 (Original_Node (Gen_Decl), Renaming_List);
5911 Build_Subprogram_Renaming;
5913 -- If the context of the instance is subject to SPARK_Mode "off" or
5914 -- the annotation is altogether missing, set the global flag which
5915 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
5916 -- the instance. This should be done prior to analyzing the instance.
5918 if SPARK_Mode /= On then
5919 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
5920 end if;
5922 -- If the context of an instance is not subject to SPARK_Mode "off",
5923 -- and the generic spec is subject to an explicit SPARK_Mode pragma,
5924 -- the latter should be the one applicable to the instance.
5926 if not Ignore_SPARK_Mode_Pragmas_In_Instance
5927 and then Saved_SM /= Off
5928 and then Present (SPARK_Pragma (Gen_Unit))
5929 then
5930 Set_SPARK_Mode (Gen_Unit);
5931 end if;
5933 -- Need to mark Anon_Id intrinsic before calling
5934 -- Analyze_Instance_And_Renamings because this flag may be propagated
5935 -- to other nodes.
5937 if Is_Intrinsic_Subprogram (Gen_Unit) then
5938 Set_Is_Intrinsic_Subprogram (Anon_Id);
5939 Set_Interface_Name (Anon_Id, Interface_Name (Gen_Unit));
5940 end if;
5942 Analyze_Instance_And_Renamings;
5944 -- Restore SPARK_Mode from the context after analysis of the package
5945 -- declaration, so that the SPARK_Mode on the generic spec does not
5946 -- apply to the pending instance for the instance body.
5948 if not Ignore_SPARK_Mode_Pragmas_In_Instance
5949 and then Saved_SM /= Off
5950 and then Present (SPARK_Pragma (Gen_Unit))
5951 then
5952 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
5953 end if;
5955 -- If the generic is marked Import (Intrinsic), then so is the
5956 -- instance; this indicates that there is no body to instantiate.
5957 -- We also copy the interface name in case this is handled by the
5958 -- back-end and deal with an instance of unchecked conversion.
5960 if Is_Intrinsic_Subprogram (Gen_Unit) then
5961 Set_Is_Intrinsic_Subprogram (Act_Decl_Id);
5962 Set_Interface_Name (Act_Decl_Id, Interface_Name (Gen_Unit));
5964 if Chars (Gen_Unit) = Name_Unchecked_Conversion then
5965 Validate_Unchecked_Conversion (N, Act_Decl_Id);
5966 end if;
5967 end if;
5969 -- Inherit convention from generic unit. Intrinsic convention, as for
5970 -- an instance of unchecked conversion, is not inherited because an
5971 -- explicit Ada instance has been created.
5973 if Has_Convention_Pragma (Gen_Unit)
5974 and then Convention (Gen_Unit) /= Convention_Intrinsic
5975 then
5976 Set_Convention (Act_Decl_Id, Convention (Gen_Unit));
5977 Set_Is_Exported (Act_Decl_Id, Is_Exported (Gen_Unit));
5978 end if;
5980 Generate_Definition (Act_Decl_Id);
5982 -- Inherit all inlining-related flags which apply to the generic in
5983 -- the subprogram and its declaration.
5985 Set_Is_Inlined (Act_Decl_Id, Is_Inlined (Gen_Unit));
5986 Set_Is_Inlined (Anon_Id, Is_Inlined (Gen_Unit));
5988 Set_Has_Pragma_Inline (Act_Decl_Id, Has_Pragma_Inline (Gen_Unit));
5989 Set_Has_Pragma_Inline (Anon_Id, Has_Pragma_Inline (Gen_Unit));
5991 Set_Has_Pragma_Inline_Always
5992 (Act_Decl_Id, Has_Pragma_Inline_Always (Gen_Unit));
5993 Set_Has_Pragma_Inline_Always
5994 (Anon_Id, Has_Pragma_Inline_Always (Gen_Unit));
5996 Set_Has_Pragma_No_Inline
5997 (Act_Decl_Id, Has_Pragma_No_Inline (Gen_Unit));
5998 Set_Has_Pragma_No_Inline
5999 (Anon_Id, Has_Pragma_No_Inline (Gen_Unit));
6001 -- Propagate No_Return if pragma applied to generic unit. This must
6002 -- be done explicitly because pragma does not appear in generic
6003 -- declaration (unlike the aspect case).
6005 if No_Return (Gen_Unit) then
6006 Set_No_Return (Act_Decl_Id);
6007 Set_No_Return (Anon_Id);
6008 end if;
6010 -- Mark both the instance spec and the anonymous package in case the
6011 -- body is instantiated at a later pass. This preserves the original
6012 -- context in effect for the body.
6014 if SPARK_Mode /= On then
6015 Set_Ignore_SPARK_Mode_Pragmas (Act_Decl_Id);
6016 Set_Ignore_SPARK_Mode_Pragmas (Anon_Id);
6017 end if;
6019 if Legacy_Elaboration_Checks
6020 and then not Is_Intrinsic_Subprogram (Gen_Unit)
6021 then
6022 Check_Elab_Instantiation (N);
6023 end if;
6025 -- Save the scenario for later examination by the ABE Processing
6026 -- phase.
6028 Record_Elaboration_Scenario (N);
6030 -- The instantiation results in a guaranteed ABE. Create a completing
6031 -- body for the subprogram declaration because the real body will not
6032 -- be instantiated.
6034 if Is_Known_Guaranteed_ABE (N) then
6035 Provide_Completing_Bodies (Instance_Spec (N));
6036 end if;
6038 if Is_Dispatching_Operation (Act_Decl_Id)
6039 and then Ada_Version >= Ada_2005
6040 then
6041 declare
6042 Formal : Entity_Id;
6044 begin
6045 Formal := First_Formal (Act_Decl_Id);
6046 while Present (Formal) loop
6047 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type
6048 and then Is_Controlling_Formal (Formal)
6049 and then not Can_Never_Be_Null (Formal)
6050 then
6051 Error_Msg_NE
6052 ("access parameter& is controlling,", N, Formal);
6053 Error_Msg_NE
6054 ("\corresponding parameter of & must be explicitly "
6055 & "null-excluding", N, Gen_Id);
6056 end if;
6058 Next_Formal (Formal);
6059 end loop;
6060 end;
6061 end if;
6063 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
6065 Validate_Categorization_Dependency (N, Act_Decl_Id);
6067 if not Is_Intrinsic_Subprogram (Act_Decl_Id) then
6068 Inherit_Context (Gen_Decl, N);
6070 Restore_Private_Views (Pack_Id, False);
6072 -- If the context requires a full instantiation, mark node for
6073 -- subsequent construction of the body.
6075 if Need_Subprogram_Instance_Body (N, Act_Decl_Id) then
6076 Check_Forward_Instantiation (Gen_Decl);
6078 -- The wrapper package is always delayed, because it does not
6079 -- constitute a freeze point, but to insure that the freeze node
6080 -- is placed properly, it is created directly when instantiating
6081 -- the body (otherwise the freeze node might appear to early for
6082 -- nested instantiations).
6084 elsif Nkind (Parent (N)) = N_Compilation_Unit then
6085 Rewrite (N, Unit (Parent (N)));
6086 Set_Unit (Parent (N), N);
6087 end if;
6089 -- Replace instance node for library-level instantiations of
6090 -- intrinsic subprograms.
6092 elsif Nkind (Parent (N)) = N_Compilation_Unit then
6093 Rewrite (N, Unit (Parent (N)));
6094 Set_Unit (Parent (N), N);
6095 end if;
6097 if Parent_Installed then
6098 Remove_Parent;
6099 end if;
6101 Restore_Hidden_Primitives (Vis_Prims_List);
6102 Restore_Env;
6103 Env_Installed := False;
6104 Generic_Renamings.Set_Last (0);
6105 Generic_Renamings_HTable.Reset;
6106 end if;
6108 <<Leave>>
6109 -- Analyze aspects in declaration if no errors appear in the instance.
6111 if Has_Aspects (N) and then Serious_Errors_Detected = Errs then
6112 Analyze_Aspect_Specifications (N, Act_Decl_Id);
6113 end if;
6115 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
6116 Restore_Ghost_Region (Saved_GM, Saved_IGR);
6117 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
6119 exception
6120 when Instantiation_Error =>
6121 if Parent_Installed then
6122 Remove_Parent;
6123 end if;
6125 if Env_Installed then
6126 Restore_Env;
6127 end if;
6129 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
6130 Restore_Ghost_Region (Saved_GM, Saved_IGR);
6131 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
6132 end Analyze_Subprogram_Instantiation;
6134 -------------------------
6135 -- Get_Associated_Node --
6136 -------------------------
6138 function Get_Associated_Node (N : Node_Id) return Node_Id is
6139 Assoc : Node_Id;
6141 begin
6142 Assoc := Associated_Node (N);
6144 if Nkind (Assoc) /= Nkind (N) then
6145 return Assoc;
6147 elsif Nkind (Assoc) in N_Aggregate | N_Extension_Aggregate then
6148 return Assoc;
6150 else
6151 -- If the node is part of an inner generic, it may itself have been
6152 -- remapped into a further generic copy. Associated_Node is otherwise
6153 -- used for the entity of the node, and will be of a different node
6154 -- kind, or else N has been rewritten as a literal or function call.
6156 while Present (Associated_Node (Assoc))
6157 and then Nkind (Associated_Node (Assoc)) = Nkind (Assoc)
6158 loop
6159 Assoc := Associated_Node (Assoc);
6160 end loop;
6162 -- Follow an additional link in case the final node was rewritten.
6163 -- This can only happen with nested generic units.
6165 if (Nkind (Assoc) = N_Identifier or else Nkind (Assoc) in N_Op)
6166 and then Present (Associated_Node (Assoc))
6167 and then Nkind (Associated_Node (Assoc)) in N_Function_Call
6168 | N_Explicit_Dereference
6169 | N_Integer_Literal
6170 | N_Real_Literal
6171 | N_String_Literal
6172 then
6173 Assoc := Associated_Node (Assoc);
6174 end if;
6176 -- An additional special case: an unconstrained type in an object
6177 -- declaration may have been rewritten as a local subtype constrained
6178 -- by the expression in the declaration. We need to recover the
6179 -- original entity, which may be global.
6181 if Present (Original_Node (Assoc))
6182 and then Nkind (Parent (N)) = N_Object_Declaration
6183 then
6184 Assoc := Original_Node (Assoc);
6185 end if;
6187 return Assoc;
6188 end if;
6189 end Get_Associated_Node;
6191 -----------------------------------
6192 -- Build_Subprogram_Decl_Wrapper --
6193 -----------------------------------
6195 function Build_Subprogram_Decl_Wrapper
6196 (Formal_Subp : Entity_Id) return Node_Id
6198 Loc : constant Source_Ptr := Sloc (Current_Scope);
6199 Ret_Type : constant Entity_Id := Get_Instance_Of (Etype (Formal_Subp));
6200 Decl : Node_Id;
6201 Subp : Entity_Id;
6202 Parm_Spec : Node_Id;
6203 Profile : List_Id := New_List;
6204 Spec : Node_Id;
6205 Form_F : Entity_Id;
6206 New_F : Entity_Id;
6208 begin
6210 Subp := Make_Defining_Identifier (Loc, Chars (Formal_Subp));
6211 Mutate_Ekind (Subp, Ekind (Formal_Subp));
6212 Set_Is_Generic_Actual_Subprogram (Subp);
6214 Profile := Parameter_Specifications (
6215 New_Copy_Tree
6216 (Specification (Unit_Declaration_Node (Formal_Subp))));
6218 Form_F := First_Formal (Formal_Subp);
6219 Parm_Spec := First (Profile);
6221 -- Create new entities for the formals. Reset entities so that
6222 -- parameter types are properly resolved when wrapper declaration
6223 -- is analyzed.
6225 while Present (Parm_Spec) loop
6226 New_F := Make_Defining_Identifier (Loc, Chars (Form_F));
6227 Set_Defining_Identifier (Parm_Spec, New_F);
6228 Set_Entity (Parameter_Type (Parm_Spec), Empty);
6229 Next (Parm_Spec);
6230 Next_Formal (Form_F);
6231 end loop;
6233 if Ret_Type = Standard_Void_Type then
6234 Spec :=
6235 Make_Procedure_Specification (Loc,
6236 Defining_Unit_Name => Subp,
6237 Parameter_Specifications => Profile);
6238 else
6239 Spec :=
6240 Make_Function_Specification (Loc,
6241 Defining_Unit_Name => Subp,
6242 Parameter_Specifications => Profile,
6243 Result_Definition => New_Occurrence_Of (Ret_Type, Loc));
6244 end if;
6246 Decl :=
6247 Make_Subprogram_Declaration (Loc, Specification => Spec);
6249 return Decl;
6250 end Build_Subprogram_Decl_Wrapper;
6252 -----------------------------------
6253 -- Build_Subprogram_Body_Wrapper --
6254 -----------------------------------
6256 function Build_Subprogram_Body_Wrapper
6257 (Formal_Subp : Entity_Id;
6258 Actual_Name : Node_Id) return Node_Id
6260 Loc : constant Source_Ptr := Sloc (Current_Scope);
6261 Ret_Type : constant Entity_Id := Get_Instance_Of (Etype (Formal_Subp));
6262 Spec_Node : constant Node_Id :=
6263 Specification
6264 (Build_Subprogram_Decl_Wrapper (Formal_Subp));
6265 Act : Node_Id;
6266 Actuals : List_Id;
6267 Body_Node : Node_Id;
6268 Stmt : Node_Id;
6269 begin
6270 Actuals := New_List;
6271 Act := First (Parameter_Specifications (Spec_Node));
6273 while Present (Act) loop
6274 Append_To (Actuals,
6275 Make_Identifier (Loc, Chars (Defining_Identifier (Act))));
6276 Next (Act);
6277 end loop;
6279 if Ret_Type = Standard_Void_Type then
6280 Stmt := Make_Procedure_Call_Statement (Loc,
6281 Name => Actual_Name,
6282 Parameter_Associations => Actuals);
6284 else
6285 Stmt := Make_Simple_Return_Statement (Loc,
6286 Expression =>
6287 Make_Function_Call (Loc,
6288 Name => Actual_Name,
6289 Parameter_Associations => Actuals));
6290 end if;
6292 Body_Node := Make_Subprogram_Body (Loc,
6293 Specification => Spec_Node,
6294 Declarations => New_List,
6295 Handled_Statement_Sequence =>
6296 Make_Handled_Sequence_Of_Statements (Loc,
6297 Statements => New_List (Stmt)));
6299 return Body_Node;
6300 end Build_Subprogram_Body_Wrapper;
6302 -------------------------------------------
6303 -- Build_Instance_Compilation_Unit_Nodes --
6304 -------------------------------------------
6306 procedure Build_Instance_Compilation_Unit_Nodes
6307 (N : Node_Id;
6308 Act_Body : Node_Id;
6309 Act_Decl : Node_Id)
6311 Decl_Cunit : Node_Id;
6312 Body_Cunit : Node_Id;
6313 Citem : Node_Id;
6314 New_Main : constant Entity_Id := Defining_Entity (Act_Decl);
6315 Old_Main : constant Entity_Id := Cunit_Entity (Main_Unit);
6317 begin
6318 -- A new compilation unit node is built for the instance declaration.
6319 -- It relocates the auxiliary declaration node from the compilation unit
6320 -- where the instance appeared, so that declarations that originally
6321 -- followed the instance will be attached to the spec compilation unit.
6323 Decl_Cunit :=
6324 Make_Compilation_Unit (Sloc (N),
6325 Context_Items => Empty_List,
6326 Unit => Act_Decl,
6327 Aux_Decls_Node => Relocate_Node (Aux_Decls_Node (Parent (N))));
6329 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
6331 -- The new compilation unit is linked to its body, but both share the
6332 -- same file, so we do not set Body_Required on the new unit so as not
6333 -- to create a spurious dependency on a non-existent body in the ali.
6334 -- This simplifies CodePeer unit traversal.
6336 -- We use the original instantiation compilation unit as the resulting
6337 -- compilation unit of the instance, since this is the main unit.
6339 Rewrite (N, Act_Body);
6341 -- Propagate the aspect specifications from the package body template to
6342 -- the instantiated version of the package body.
6344 if Has_Aspects (Act_Body) then
6345 Set_Aspect_Specifications
6346 (N, New_Copy_List_Tree (Aspect_Specifications (Act_Body)));
6347 end if;
6349 Body_Cunit := Parent (N);
6351 -- The two compilation unit nodes are linked by the Library_Unit field
6353 Set_Library_Unit (Decl_Cunit, Body_Cunit);
6354 Set_Library_Unit (Body_Cunit, Decl_Cunit);
6356 -- Preserve the private nature of the package if needed
6358 Set_Private_Present (Decl_Cunit, Private_Present (Body_Cunit));
6360 -- If the instance is not the main unit, its context, categorization
6361 -- and elaboration entity are not relevant to the compilation.
6363 if Body_Cunit /= Cunit (Main_Unit) then
6364 Make_Instance_Unit (Body_Cunit, In_Main => False);
6365 return;
6366 end if;
6368 -- The context clause items on the instantiation, which are now attached
6369 -- to the body compilation unit (since the body overwrote the original
6370 -- instantiation node), semantically belong on the spec, so copy them
6371 -- there. It's harmless to leave them on the body as well. In fact one
6372 -- could argue that they belong in both places.
6374 Citem := First (Context_Items (Body_Cunit));
6375 while Present (Citem) loop
6376 Append (New_Copy (Citem), Context_Items (Decl_Cunit));
6377 Next (Citem);
6378 end loop;
6380 -- Propagate categorization flags on packages, so that they appear in
6381 -- the ali file for the spec of the unit.
6383 if Ekind (New_Main) = E_Package then
6384 Set_Is_Pure (Old_Main, Is_Pure (New_Main));
6385 Set_Is_Preelaborated (Old_Main, Is_Preelaborated (New_Main));
6386 Set_Is_Remote_Types (Old_Main, Is_Remote_Types (New_Main));
6387 Set_Is_Shared_Passive (Old_Main, Is_Shared_Passive (New_Main));
6388 Set_Is_Remote_Call_Interface
6389 (Old_Main, Is_Remote_Call_Interface (New_Main));
6390 end if;
6392 -- Make entry in Units table, so that binder can generate call to
6393 -- elaboration procedure for body, if any.
6395 Make_Instance_Unit (Body_Cunit, In_Main => True);
6396 Main_Unit_Entity := New_Main;
6397 Set_Cunit_Entity (Main_Unit, Main_Unit_Entity);
6399 -- Build elaboration entity, since the instance may certainly generate
6400 -- elaboration code requiring a flag for protection.
6402 Build_Elaboration_Entity (Decl_Cunit, New_Main);
6403 end Build_Instance_Compilation_Unit_Nodes;
6405 --------------------------------
6406 -- Check_Abbreviated_Instance --
6407 --------------------------------
6409 procedure Check_Abbreviated_Instance
6410 (N : Node_Id;
6411 Parent_Installed : in out Boolean)
6413 Inst_Node : Node_Id;
6415 begin
6416 if Nkind (N) = N_Package_Specification
6417 and then Is_Abbreviated_Instance (Defining_Entity (N))
6418 then
6419 Inst_Node := Get_Unit_Instantiation_Node (Defining_Entity (N));
6420 Check_Generic_Child_Unit (Name (Inst_Node), Parent_Installed);
6421 end if;
6422 end Check_Abbreviated_Instance;
6424 -----------------------------
6425 -- Check_Access_Definition --
6426 -----------------------------
6428 procedure Check_Access_Definition (N : Node_Id) is
6429 begin
6430 pragma Assert
6431 (Ada_Version >= Ada_2005 and then Present (Access_Definition (N)));
6432 null;
6433 end Check_Access_Definition;
6435 -----------------------------------
6436 -- Check_Formal_Package_Instance --
6437 -----------------------------------
6439 -- If the formal has specific parameters, they must match those of the
6440 -- actual. Both of them are instances, and the renaming declarations for
6441 -- their formal parameters appear in the same order in both. The analyzed
6442 -- formal has been analyzed in the context of the current instance.
6444 procedure Check_Formal_Package_Instance
6445 (Formal_Pack : Entity_Id;
6446 Actual_Pack : Entity_Id)
6448 E1 : Entity_Id := First_Entity (Actual_Pack);
6449 E2 : Entity_Id := First_Entity (Formal_Pack);
6450 Prev_E1 : Entity_Id;
6452 Expr1 : Node_Id;
6453 Expr2 : Node_Id;
6455 procedure Check_Mismatch (B : Boolean);
6456 -- Common error routine for mismatch between the parameters of the
6457 -- actual instance and those of the formal package.
6459 function Is_Defaulted (Param : Entity_Id) return Boolean;
6460 -- If the formal package has partly box-initialized formals, skip
6461 -- conformance check for these formals. Previously the code assumed
6462 -- that box initialization for a formal package applied to all its
6463 -- formal parameters.
6465 function Same_Instantiated_Constant (E1, E2 : Entity_Id) return Boolean;
6466 -- The formal may come from a nested formal package, and the actual may
6467 -- have been constant-folded. To determine whether the two denote the
6468 -- same entity we may have to traverse several definitions to recover
6469 -- the ultimate entity that they refer to.
6471 function Same_Instantiated_Function (E1, E2 : Entity_Id) return Boolean;
6472 -- The formal and the actual must be identical, but if both are
6473 -- given by attributes they end up renaming different generated bodies,
6474 -- and we must verify that the attributes themselves match.
6476 function Same_Instantiated_Variable (E1, E2 : Entity_Id) return Boolean;
6477 -- Similarly, if the formal comes from a nested formal package, the
6478 -- actual may designate the formal through multiple renamings, which
6479 -- have to be followed to determine the original variable in question.
6481 --------------------
6482 -- Check_Mismatch --
6483 --------------------
6485 procedure Check_Mismatch (B : Boolean) is
6486 -- A Formal_Type_Declaration for a derived private type is rewritten
6487 -- as a private extension decl. (see Analyze_Formal_Derived_Type),
6488 -- which is why we examine the original node.
6490 Kind : constant Node_Kind := Nkind (Original_Node (Parent (E2)));
6492 begin
6493 if Kind = N_Formal_Type_Declaration then
6494 return;
6496 elsif Kind in N_Formal_Object_Declaration
6497 | N_Formal_Package_Declaration
6498 | N_Formal_Subprogram_Declaration
6499 then
6500 null;
6502 -- Ada 2012: If both formal and actual are incomplete types they
6503 -- are conformant.
6505 elsif Is_Incomplete_Type (E1) and then Is_Incomplete_Type (E2) then
6506 null;
6508 elsif B then
6509 Error_Msg_NE
6510 ("actual for & in actual instance does not match formal",
6511 Parent (Actual_Pack), E1);
6512 end if;
6513 end Check_Mismatch;
6515 ------------------
6516 -- Is_Defaulted --
6517 ------------------
6519 function Is_Defaulted (Param : Entity_Id) return Boolean is
6520 Assoc : Node_Id;
6522 begin
6523 Assoc :=
6524 First (Generic_Associations (Parent
6525 (Associated_Formal_Package (Actual_Pack))));
6527 while Present (Assoc) loop
6528 if Nkind (Assoc) = N_Others_Choice then
6529 return True;
6531 elsif Nkind (Assoc) = N_Generic_Association
6532 and then Chars (Selector_Name (Assoc)) = Chars (Param)
6533 then
6534 return Box_Present (Assoc);
6535 end if;
6537 Next (Assoc);
6538 end loop;
6540 return False;
6541 end Is_Defaulted;
6543 --------------------------------
6544 -- Same_Instantiated_Constant --
6545 --------------------------------
6547 function Same_Instantiated_Constant
6548 (E1, E2 : Entity_Id) return Boolean
6550 Ent : Entity_Id;
6552 begin
6553 Ent := E2;
6554 while Present (Ent) loop
6555 if E1 = Ent then
6556 return True;
6558 elsif Ekind (Ent) /= E_Constant then
6559 return False;
6561 elsif Is_Entity_Name (Constant_Value (Ent)) then
6562 if Entity (Constant_Value (Ent)) = E1 then
6563 return True;
6564 else
6565 Ent := Entity (Constant_Value (Ent));
6566 end if;
6568 -- The actual may be a constant that has been folded. Recover
6569 -- original name.
6571 elsif Is_Entity_Name (Original_Node (Constant_Value (Ent))) then
6572 Ent := Entity (Original_Node (Constant_Value (Ent)));
6574 else
6575 return False;
6576 end if;
6577 end loop;
6579 return False;
6580 end Same_Instantiated_Constant;
6582 --------------------------------
6583 -- Same_Instantiated_Function --
6584 --------------------------------
6586 function Same_Instantiated_Function
6587 (E1, E2 : Entity_Id) return Boolean
6589 U1, U2 : Node_Id;
6590 begin
6591 if Alias (E1) = Alias (E2) then
6592 return True;
6594 elsif Present (Alias (E2)) then
6595 U1 := Original_Node (Unit_Declaration_Node (E1));
6596 U2 := Original_Node (Unit_Declaration_Node (Alias (E2)));
6598 return Nkind (U1) = N_Subprogram_Renaming_Declaration
6599 and then Nkind (Name (U1)) = N_Attribute_Reference
6601 and then Nkind (U2) = N_Subprogram_Renaming_Declaration
6602 and then Nkind (Name (U2)) = N_Attribute_Reference
6604 and then
6605 Attribute_Name (Name (U1)) = Attribute_Name (Name (U2));
6606 else
6607 return False;
6608 end if;
6609 end Same_Instantiated_Function;
6611 --------------------------------
6612 -- Same_Instantiated_Variable --
6613 --------------------------------
6615 function Same_Instantiated_Variable
6616 (E1, E2 : Entity_Id) return Boolean
6618 function Original_Entity (E : Entity_Id) return Entity_Id;
6619 -- Follow chain of renamings to the ultimate ancestor
6621 ---------------------
6622 -- Original_Entity --
6623 ---------------------
6625 function Original_Entity (E : Entity_Id) return Entity_Id is
6626 Orig : Entity_Id;
6628 begin
6629 Orig := E;
6630 while Nkind (Parent (Orig)) = N_Object_Renaming_Declaration
6631 and then Present (Renamed_Object (Orig))
6632 and then Is_Entity_Name (Renamed_Object (Orig))
6633 loop
6634 Orig := Entity (Renamed_Object (Orig));
6635 end loop;
6637 return Orig;
6638 end Original_Entity;
6640 -- Start of processing for Same_Instantiated_Variable
6642 begin
6643 return Ekind (E1) = Ekind (E2)
6644 and then Original_Entity (E1) = Original_Entity (E2);
6645 end Same_Instantiated_Variable;
6647 -- Start of processing for Check_Formal_Package_Instance
6649 begin
6650 Prev_E1 := E1;
6651 while Present (E1) and then Present (E2) loop
6652 exit when Ekind (E1) = E_Package
6653 and then Renamed_Entity (E1) = Renamed_Entity (Actual_Pack);
6655 -- If the formal is the renaming of the formal package, this
6656 -- is the end of its formal part, which may occur before the
6657 -- end of the formal part in the actual in the presence of
6658 -- defaulted parameters in the formal package.
6660 exit when Nkind (Parent (E2)) = N_Package_Renaming_Declaration
6661 and then Renamed_Entity (E2) = Scope (E2);
6663 -- The analysis of the actual may generate additional internal
6664 -- entities. If the formal is defaulted, there is no corresponding
6665 -- analysis and the internal entities must be skipped, until we
6666 -- find corresponding entities again.
6668 if Comes_From_Source (E2)
6669 and then not Comes_From_Source (E1)
6670 and then Chars (E1) /= Chars (E2)
6671 then
6672 while Present (E1) and then Chars (E1) /= Chars (E2) loop
6673 Next_Entity (E1);
6674 end loop;
6675 end if;
6677 if No (E1) then
6678 return;
6680 -- Entities may be declared without full declaration, such as
6681 -- itypes and predefined operators (concatenation for arrays, eg).
6682 -- Skip it and keep the formal entity to find a later match for it.
6684 elsif No (Parent (E2)) and then Ekind (E1) /= Ekind (E2) then
6685 E1 := Prev_E1;
6686 goto Next_E;
6688 -- If the formal entity comes from a formal declaration, it was
6689 -- defaulted in the formal package, and no check is needed on it.
6691 elsif Nkind (Original_Node (Parent (E2))) in
6692 N_Formal_Object_Declaration | N_Formal_Type_Declaration
6693 then
6694 -- If the formal is a tagged type the corresponding class-wide
6695 -- type has been generated as well, and it must be skipped.
6697 if Is_Type (E2) and then Is_Tagged_Type (E2) then
6698 Next_Entity (E2);
6699 end if;
6701 goto Next_E;
6703 -- Ditto for defaulted formal subprograms.
6705 elsif Is_Overloadable (E1)
6706 and then Nkind (Unit_Declaration_Node (E2)) in
6707 N_Formal_Subprogram_Declaration
6708 then
6709 goto Next_E;
6711 elsif Is_Defaulted (E1) then
6712 goto Next_E;
6714 elsif Is_Type (E1) then
6716 -- Subtypes must statically match. E1, E2 are the local entities
6717 -- that are subtypes of the actuals. Itypes generated for other
6718 -- parameters need not be checked, the check will be performed
6719 -- on the parameters themselves.
6721 -- If E2 is a formal type declaration, it is a defaulted parameter
6722 -- and needs no checking.
6724 if not Is_Itype (E1) and then not Is_Itype (E2) then
6725 Check_Mismatch
6726 (not Is_Type (E2)
6727 or else Etype (E1) /= Etype (E2)
6728 or else not Subtypes_Statically_Match (E1, E2));
6729 end if;
6731 elsif Ekind (E1) = E_Constant then
6733 -- IN parameters must denote the same static value, or the same
6734 -- constant, or the literal null.
6736 Expr1 := Expression (Parent (E1));
6738 if Ekind (E2) /= E_Constant then
6739 Check_Mismatch (True);
6740 goto Next_E;
6741 else
6742 Expr2 := Expression (Parent (E2));
6743 end if;
6745 if Is_OK_Static_Expression (Expr1) then
6746 if not Is_OK_Static_Expression (Expr2) then
6747 Check_Mismatch (True);
6749 elsif Is_Discrete_Type (Etype (E1)) then
6750 declare
6751 V1 : constant Uint := Expr_Value (Expr1);
6752 V2 : constant Uint := Expr_Value (Expr2);
6753 begin
6754 Check_Mismatch (V1 /= V2);
6755 end;
6757 elsif Is_Real_Type (Etype (E1)) then
6758 declare
6759 V1 : constant Ureal := Expr_Value_R (Expr1);
6760 V2 : constant Ureal := Expr_Value_R (Expr2);
6761 begin
6762 Check_Mismatch (V1 /= V2);
6763 end;
6765 elsif Is_String_Type (Etype (E1))
6766 and then Nkind (Expr1) = N_String_Literal
6767 then
6768 if Nkind (Expr2) /= N_String_Literal then
6769 Check_Mismatch (True);
6770 else
6771 Check_Mismatch
6772 (not String_Equal (Strval (Expr1), Strval (Expr2)));
6773 end if;
6774 end if;
6776 elsif Is_Entity_Name (Expr1) then
6777 if Is_Entity_Name (Expr2) then
6778 if Entity (Expr1) = Entity (Expr2) then
6779 null;
6780 else
6781 Check_Mismatch
6782 (not Same_Instantiated_Constant
6783 (Entity (Expr1), Entity (Expr2)));
6784 end if;
6786 else
6787 Check_Mismatch (True);
6788 end if;
6790 elsif Is_Entity_Name (Original_Node (Expr1))
6791 and then Is_Entity_Name (Expr2)
6792 and then Same_Instantiated_Constant
6793 (Entity (Original_Node (Expr1)), Entity (Expr2))
6794 then
6795 null;
6797 elsif Nkind (Expr1) = N_Null then
6798 Check_Mismatch (Nkind (Expr1) /= N_Null);
6800 else
6801 Check_Mismatch (True);
6802 end if;
6804 elsif Ekind (E1) = E_Variable then
6805 Check_Mismatch (not Same_Instantiated_Variable (E1, E2));
6807 elsif Ekind (E1) = E_Package then
6808 Check_Mismatch
6809 (Ekind (E1) /= Ekind (E2)
6810 or else (Present (Renamed_Entity (E2))
6811 and then Renamed_Entity (E1) /=
6812 Renamed_Entity (E2)));
6814 elsif Is_Overloadable (E1) then
6815 -- Verify that the actual subprograms match. Note that actuals
6816 -- that are attributes are rewritten as subprograms. If the
6817 -- subprogram in the formal package is defaulted, no check is
6818 -- needed. Note that this can only happen in Ada 2005 when the
6819 -- formal package can be partially parameterized.
6821 if Nkind (Unit_Declaration_Node (E1)) =
6822 N_Subprogram_Renaming_Declaration
6823 and then From_Default (Unit_Declaration_Node (E1))
6824 then
6825 null;
6827 -- If the formal package has an "others" box association that
6828 -- covers this formal, there is no need for a check either.
6830 elsif Nkind (Unit_Declaration_Node (E2)) in
6831 N_Formal_Subprogram_Declaration
6832 and then Box_Present (Unit_Declaration_Node (E2))
6833 then
6834 null;
6836 -- No check needed if subprogram is a defaulted null procedure
6838 elsif No (Alias (E2))
6839 and then Ekind (E2) = E_Procedure
6840 and then
6841 Null_Present (Specification (Unit_Declaration_Node (E2)))
6842 then
6843 null;
6845 -- Otherwise the actual in the formal and the actual in the
6846 -- instantiation of the formal must match, up to renamings.
6848 else
6849 Check_Mismatch
6850 (Ekind (E2) /= Ekind (E1)
6851 or else not Same_Instantiated_Function (E1, E2));
6852 end if;
6854 else
6855 raise Program_Error;
6856 end if;
6858 <<Next_E>>
6859 Prev_E1 := E1;
6860 Next_Entity (E1);
6861 Next_Entity (E2);
6862 end loop;
6863 end Check_Formal_Package_Instance;
6865 ---------------------------
6866 -- Check_Formal_Packages --
6867 ---------------------------
6869 procedure Check_Formal_Packages (P_Id : Entity_Id) is
6870 E : Entity_Id;
6871 Formal_P : Entity_Id;
6872 Formal_Decl : Node_Id;
6874 begin
6875 -- Iterate through the declarations in the instance, looking for package
6876 -- renaming declarations that denote instances of formal packages, until
6877 -- we find the renaming of the current package itself. The declaration
6878 -- of a formal package that requires conformance checking is followed by
6879 -- an internal entity that is the abbreviated instance.
6881 E := First_Entity (P_Id);
6882 while Present (E) loop
6883 if Ekind (E) = E_Package then
6884 exit when Renamed_Entity (E) = P_Id;
6886 if Nkind (Parent (E)) = N_Package_Renaming_Declaration then
6887 Formal_Decl := Parent (Associated_Formal_Package (E));
6889 if Requires_Conformance_Checking (Formal_Decl) then
6890 Formal_P := Next_Entity (E);
6892 -- If the instance is within an enclosing instance body
6893 -- there is no need to verify the legality of current formal
6894 -- packages because they were legal in the generic body.
6895 -- This optimization may be applicable elsewhere, and it
6896 -- also removes spurious errors that may arise with
6897 -- on-the-fly inlining and confusion between private and
6898 -- full views.
6900 if not In_Instance_Body then
6901 Check_Formal_Package_Instance (Formal_P, E);
6902 end if;
6904 -- Restore the visibility of formals of the formal instance
6905 -- that are not defaulted, and are hidden within the current
6906 -- generic. These formals may be visible within an enclosing
6907 -- generic.
6909 declare
6910 Elmt : Elmt_Id;
6911 begin
6912 Elmt := First_Elmt (Hidden_In_Formal_Instance (Formal_P));
6913 while Present (Elmt) loop
6914 Set_Is_Hidden (Node (Elmt), False);
6915 Next_Elmt (Elmt);
6916 end loop;
6917 end;
6919 -- After checking, remove the internal validating package.
6920 -- It is only needed for semantic checks, and as it may
6921 -- contain generic formal declarations it should not reach
6922 -- gigi.
6924 Remove (Unit_Declaration_Node (Formal_P));
6925 end if;
6926 end if;
6927 end if;
6929 Next_Entity (E);
6930 end loop;
6931 end Check_Formal_Packages;
6933 ---------------------------------
6934 -- Check_Forward_Instantiation --
6935 ---------------------------------
6937 procedure Check_Forward_Instantiation (Decl : Node_Id) is
6938 S : Entity_Id;
6939 Gen_Comp : Entity_Id := Cunit_Entity (Get_Source_Unit (Decl));
6941 begin
6942 -- The instantiation appears before the generic body if we are in the
6943 -- scope of the unit containing the generic, either in its spec or in
6944 -- the package body, and before the generic body.
6946 if Ekind (Gen_Comp) = E_Package_Body then
6947 Gen_Comp := Spec_Entity (Gen_Comp);
6948 end if;
6950 if In_Open_Scopes (Gen_Comp)
6951 and then No (Corresponding_Body (Decl))
6952 then
6953 S := Current_Scope;
6955 while Present (S)
6956 and then not Is_Compilation_Unit (S)
6957 and then not Is_Child_Unit (S)
6958 loop
6959 if Ekind (S) = E_Package then
6960 Set_Has_Forward_Instantiation (S);
6961 end if;
6963 S := Scope (S);
6964 end loop;
6965 end if;
6966 end Check_Forward_Instantiation;
6968 ---------------------------
6969 -- Check_Generic_Actuals --
6970 ---------------------------
6972 -- The visibility of the actuals may be different between the point of
6973 -- generic instantiation and the instantiation of the body.
6975 procedure Check_Generic_Actuals
6976 (Instance : Entity_Id;
6977 Is_Formal_Box : Boolean)
6979 E : Entity_Id;
6980 Astype : Entity_Id;
6982 begin
6983 E := First_Entity (Instance);
6984 while Present (E) loop
6985 if Is_Type (E)
6986 and then Nkind (Parent (E)) = N_Subtype_Declaration
6987 and then Scope (Etype (E)) /= Instance
6988 and then Is_Entity_Name (Subtype_Indication (Parent (E)))
6989 then
6990 -- Restore the proper view of the actual from the information
6991 -- saved earlier by Instantiate_Type.
6993 Check_Private_View (Subtype_Indication (Parent (E)));
6995 -- If the actual is itself the formal of a parent instance,
6996 -- then also restore the proper view of its actual and so on.
6997 -- That's necessary for nested instantiations of the form
6999 -- generic
7000 -- type Component is private;
7001 -- type Array_Type is array (Positive range <>) of Component;
7002 -- procedure Proc;
7004 -- when the outermost actuals have inconsistent views, because
7005 -- the Component_Type of Array_Type of the inner instantiations
7006 -- is the actual of Component of the outermost one and not that
7007 -- of the corresponding inner instantiations.
7009 Astype := Ancestor_Subtype (E);
7010 while Present (Astype)
7011 and then Nkind (Parent (Astype)) = N_Subtype_Declaration
7012 and then Present (Generic_Parent_Type (Parent (Astype)))
7013 and then Is_Entity_Name (Subtype_Indication (Parent (Astype)))
7014 loop
7015 Check_Private_View (Subtype_Indication (Parent (Astype)));
7016 Astype := Ancestor_Subtype (Astype);
7017 end loop;
7019 Set_Is_Generic_Actual_Type (E);
7021 if Is_Private_Type (E) and then Present (Full_View (E)) then
7022 Set_Is_Generic_Actual_Type (Full_View (E));
7023 end if;
7025 Set_Is_Hidden (E, False);
7026 Set_Is_Potentially_Use_Visible (E, In_Use (Instance));
7028 -- We constructed the generic actual type as a subtype of the
7029 -- supplied type. This means that it normally would not inherit
7030 -- subtype specific attributes of the actual, which is wrong for
7031 -- the generic case.
7033 Astype := Ancestor_Subtype (E);
7035 if No (Astype) then
7037 -- This can happen when E is an itype that is the full view of
7038 -- a private type completed, e.g. with a constrained array. In
7039 -- that case, use the first subtype, which will carry size
7040 -- information. The base type itself is unconstrained and will
7041 -- not carry it.
7043 Astype := First_Subtype (E);
7044 end if;
7046 Set_Size_Info (E, Astype);
7047 Copy_RM_Size (To => E, From => Astype);
7048 Set_First_Rep_Item (E, First_Rep_Item (Astype));
7050 if Is_Discrete_Or_Fixed_Point_Type (E) then
7051 Set_RM_Size (E, RM_Size (Astype));
7052 end if;
7054 elsif Ekind (E) = E_Package then
7056 -- If this is the renaming for the current instance, we're done.
7057 -- Otherwise it is a formal package. If the corresponding formal
7058 -- was declared with a box, the (instantiations of the) generic
7059 -- formal part are also visible. Otherwise, ignore the entity
7060 -- created to validate the actuals.
7062 if Renamed_Entity (E) = Instance then
7063 exit;
7065 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
7066 null;
7068 -- The visibility of a formal of an enclosing generic is already
7069 -- correct.
7071 elsif Denotes_Formal_Package (E) then
7072 null;
7074 elsif Present (Associated_Formal_Package (E))
7075 and then not Is_Generic_Formal (E)
7076 then
7077 Check_Generic_Actuals
7078 (Renamed_Entity (E),
7079 Is_Formal_Box =>
7080 Box_Present (Parent (Associated_Formal_Package (E))));
7082 Set_Is_Hidden (E, False);
7083 end if;
7085 -- If this is a subprogram instance (in a wrapper package) the
7086 -- actual is fully visible.
7088 elsif Is_Wrapper_Package (Instance) then
7089 Set_Is_Hidden (E, False);
7091 -- If the formal package is declared with a box, or if the formal
7092 -- parameter is defaulted, it is visible in the body.
7094 elsif Is_Formal_Box or else Is_Visible_Formal (E) then
7095 Set_Is_Hidden (E, False);
7096 end if;
7098 if Ekind (E) = E_Constant then
7100 -- If the type of the actual is a private type declared in the
7101 -- enclosing scope of the generic unit, the body of the generic
7102 -- sees the full view of the type (because it has to appear in
7103 -- the corresponding package body). If the type is private now,
7104 -- exchange views to restore the proper visiblity in the instance.
7106 declare
7107 Typ : constant Entity_Id := Base_Type (Etype (E));
7108 -- The type of the actual
7110 Gen_Id : Entity_Id;
7111 -- The generic unit
7113 Parent_Scope : Entity_Id;
7114 -- The enclosing scope of the generic unit
7116 begin
7117 if Is_Wrapper_Package (Instance) then
7118 Gen_Id :=
7119 Generic_Parent
7120 (Specification
7121 (Unit_Declaration_Node
7122 (Related_Instance (Instance))));
7123 else
7124 Gen_Id :=
7125 Generic_Parent (Package_Specification (Instance));
7126 end if;
7128 Parent_Scope := Scope (Gen_Id);
7130 -- The exchange is only needed if the generic is defined
7131 -- within a package which is not a common ancestor of the
7132 -- scope of the instance, and is not already in scope.
7134 if Is_Private_Type (Typ)
7135 and then Scope (Typ) = Parent_Scope
7136 and then Scope (Instance) /= Parent_Scope
7137 and then Ekind (Parent_Scope) = E_Package
7138 and then not Is_Child_Unit (Gen_Id)
7139 then
7140 Switch_View (Typ);
7142 -- If the type of the entity is a subtype, it may also have
7143 -- to be made visible, together with the base type of its
7144 -- full view, after exchange.
7146 if Is_Private_Type (Etype (E)) then
7147 Switch_View (Etype (E));
7148 Switch_View (Base_Type (Etype (E)));
7149 end if;
7150 end if;
7151 end;
7152 end if;
7154 Next_Entity (E);
7155 end loop;
7156 end Check_Generic_Actuals;
7158 ------------------------------
7159 -- Check_Generic_Child_Unit --
7160 ------------------------------
7162 procedure Check_Generic_Child_Unit
7163 (Gen_Id : Node_Id;
7164 Parent_Installed : in out Boolean)
7166 Loc : constant Source_Ptr := Sloc (Gen_Id);
7167 Gen_Par : Entity_Id := Empty;
7168 E : Entity_Id;
7169 Inst_Par : Entity_Id;
7170 S : Node_Id;
7172 function Find_Generic_Child
7173 (Scop : Entity_Id;
7174 Id : Node_Id) return Entity_Id;
7175 -- Search generic parent for possible child unit with the given name
7177 function In_Enclosing_Instance return Boolean;
7178 -- Within an instance of the parent, the child unit may be denoted by
7179 -- a simple name, or an abbreviated expanded name. Examine enclosing
7180 -- scopes to locate a possible parent instantiation.
7182 ------------------------
7183 -- Find_Generic_Child --
7184 ------------------------
7186 function Find_Generic_Child
7187 (Scop : Entity_Id;
7188 Id : Node_Id) return Entity_Id
7190 E : Entity_Id;
7192 begin
7193 -- If entity of name is already set, instance has already been
7194 -- resolved, e.g. in an enclosing instantiation.
7196 if Present (Entity (Id)) then
7197 if Scope (Entity (Id)) = Scop then
7198 return Entity (Id);
7199 else
7200 return Empty;
7201 end if;
7203 else
7204 E := First_Entity (Scop);
7205 while Present (E) loop
7206 if Chars (E) = Chars (Id)
7207 and then Is_Child_Unit (E)
7208 then
7209 if Is_Child_Unit (E)
7210 and then not Is_Visible_Lib_Unit (E)
7211 then
7212 Error_Msg_NE
7213 ("generic child unit& is not visible", Gen_Id, E);
7214 end if;
7216 Set_Entity (Id, E);
7217 return E;
7218 end if;
7220 Next_Entity (E);
7221 end loop;
7223 return Empty;
7224 end if;
7225 end Find_Generic_Child;
7227 ---------------------------
7228 -- In_Enclosing_Instance --
7229 ---------------------------
7231 function In_Enclosing_Instance return Boolean is
7232 Enclosing_Instance : Node_Id;
7233 Instance_Decl : Node_Id;
7235 begin
7236 -- We do not inline any call that contains instantiations, except
7237 -- for instantiations of Unchecked_Conversion, so if we are within
7238 -- an inlined body the current instance does not require parents.
7240 if In_Inlined_Body then
7241 pragma Assert (Chars (Gen_Id) = Name_Unchecked_Conversion);
7242 return False;
7243 end if;
7245 -- Loop to check enclosing scopes
7247 Enclosing_Instance := Current_Scope;
7248 while Present (Enclosing_Instance) loop
7249 Instance_Decl := Unit_Declaration_Node (Enclosing_Instance);
7251 if Ekind (Enclosing_Instance) = E_Package
7252 and then Is_Generic_Instance (Enclosing_Instance)
7253 and then Present
7254 (Generic_Parent (Specification (Instance_Decl)))
7255 then
7256 -- Check whether the generic we are looking for is a child of
7257 -- this instance.
7259 E := Find_Generic_Child
7260 (Generic_Parent (Specification (Instance_Decl)), Gen_Id);
7261 exit when Present (E);
7263 else
7264 E := Empty;
7265 end if;
7267 Enclosing_Instance := Scope (Enclosing_Instance);
7268 end loop;
7270 if No (E) then
7272 -- Not a child unit
7274 Analyze (Gen_Id);
7275 return False;
7277 else
7278 Rewrite (Gen_Id,
7279 Make_Expanded_Name (Loc,
7280 Chars => Chars (E),
7281 Prefix => New_Occurrence_Of (Enclosing_Instance, Loc),
7282 Selector_Name => New_Occurrence_Of (E, Loc)));
7284 Set_Entity (Gen_Id, E);
7285 Set_Etype (Gen_Id, Etype (E));
7286 Parent_Installed := False; -- Already in scope.
7287 return True;
7288 end if;
7289 end In_Enclosing_Instance;
7291 -- Start of processing for Check_Generic_Child_Unit
7293 begin
7294 -- If the name of the generic is given by a selected component, it may
7295 -- be the name of a generic child unit, and the prefix is the name of an
7296 -- instance of the parent, in which case the child unit must be visible.
7297 -- If this instance is not in scope, it must be placed there and removed
7298 -- after instantiation, because what is being instantiated is not the
7299 -- original child, but the corresponding child present in the instance
7300 -- of the parent.
7302 -- If the child is instantiated within the parent, it can be given by
7303 -- a simple name. In this case the instance is already in scope, but
7304 -- the child generic must be recovered from the generic parent as well.
7306 if Nkind (Gen_Id) = N_Selected_Component then
7307 S := Selector_Name (Gen_Id);
7308 Analyze (Prefix (Gen_Id));
7309 Inst_Par := Entity (Prefix (Gen_Id));
7311 if Ekind (Inst_Par) = E_Package
7312 and then Present (Renamed_Entity (Inst_Par))
7313 then
7314 Inst_Par := Renamed_Entity (Inst_Par);
7315 end if;
7317 if Ekind (Inst_Par) = E_Package then
7318 if Nkind (Parent (Inst_Par)) = N_Package_Specification then
7319 Gen_Par := Generic_Parent (Parent (Inst_Par));
7321 elsif Nkind (Parent (Inst_Par)) = N_Defining_Program_Unit_Name
7322 and then
7323 Nkind (Parent (Parent (Inst_Par))) = N_Package_Specification
7324 then
7325 Gen_Par := Generic_Parent (Parent (Parent (Inst_Par)));
7326 end if;
7328 elsif Ekind (Inst_Par) = E_Generic_Package
7329 and then Nkind (Parent (Gen_Id)) = N_Formal_Package_Declaration
7330 then
7331 -- A formal package may be a real child package, and not the
7332 -- implicit instance within a parent. In this case the child is
7333 -- not visible and has to be retrieved explicitly as well.
7335 Gen_Par := Inst_Par;
7336 end if;
7338 if Present (Gen_Par) then
7340 -- The prefix denotes an instantiation. The entity itself may be a
7341 -- nested generic, or a child unit.
7343 E := Find_Generic_Child (Gen_Par, S);
7345 if Present (E) then
7346 Change_Selected_Component_To_Expanded_Name (Gen_Id);
7347 Set_Entity (Gen_Id, E);
7348 Set_Etype (Gen_Id, Etype (E));
7349 Set_Entity (S, E);
7350 Set_Etype (S, Etype (E));
7352 -- Indicate that this is a reference to the parent
7354 if In_Extended_Main_Source_Unit (Gen_Id) then
7355 Set_Is_Instantiated (Inst_Par);
7356 end if;
7358 -- A common mistake is to replicate the naming scheme of a
7359 -- hierarchy by instantiating a generic child directly, rather
7360 -- than the implicit child in a parent instance:
7362 -- generic .. package Gpar is ..
7363 -- generic .. package Gpar.Child is ..
7364 -- package Par is new Gpar ();
7366 -- with Gpar.Child;
7367 -- package Par.Child is new Gpar.Child ();
7368 -- rather than Par.Child
7370 -- In this case the instantiation is within Par, which is an
7371 -- instance, but Gpar does not denote Par because we are not IN
7372 -- the instance of Gpar, so this is illegal. The test below
7373 -- recognizes this particular case.
7375 if Is_Child_Unit (E)
7376 and then not Comes_From_Source (Entity (Prefix (Gen_Id)))
7377 and then (not In_Instance
7378 or else Nkind (Parent (Parent (Gen_Id))) =
7379 N_Compilation_Unit)
7380 then
7381 Error_Msg_N
7382 ("prefix of generic child unit must be instance of parent",
7383 Gen_Id);
7384 end if;
7386 if not In_Open_Scopes (Inst_Par)
7387 and then Nkind (Parent (Gen_Id)) not in
7388 N_Generic_Renaming_Declaration
7389 then
7390 Install_Parent (Inst_Par);
7391 Parent_Installed := True;
7393 elsif In_Open_Scopes (Inst_Par) then
7395 -- If the parent is already installed, install the actuals
7396 -- for its formal packages. This is necessary when the child
7397 -- instance is a child of the parent instance: in this case,
7398 -- the parent is placed on the scope stack but the formal
7399 -- packages are not made visible.
7401 Install_Formal_Packages (Inst_Par);
7402 end if;
7404 else
7405 -- If the generic parent does not contain an entity that
7406 -- corresponds to the selector, the instance doesn't either.
7407 -- Analyzing the node will yield the appropriate error message.
7408 -- If the entity is not a child unit, then it is an inner
7409 -- generic in the parent.
7411 Analyze (Gen_Id);
7412 end if;
7414 else
7415 Analyze (Gen_Id);
7417 if Is_Child_Unit (Entity (Gen_Id))
7418 and then
7419 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
7420 and then not In_Open_Scopes (Inst_Par)
7421 then
7422 Install_Parent (Inst_Par);
7423 Parent_Installed := True;
7425 -- The generic unit may be the renaming of the implicit child
7426 -- present in an instance. In that case the parent instance is
7427 -- obtained from the name of the renamed entity.
7429 elsif Ekind (Entity (Gen_Id)) = E_Generic_Package
7430 and then Present (Renamed_Entity (Entity (Gen_Id)))
7431 and then Is_Child_Unit (Renamed_Entity (Entity (Gen_Id)))
7432 then
7433 declare
7434 Renamed_Package : constant Node_Id :=
7435 Name (Parent (Entity (Gen_Id)));
7436 begin
7437 if Nkind (Renamed_Package) = N_Expanded_Name then
7438 Inst_Par := Entity (Prefix (Renamed_Package));
7439 Install_Parent (Inst_Par);
7440 Parent_Installed := True;
7441 end if;
7442 end;
7443 end if;
7444 end if;
7446 elsif Nkind (Gen_Id) = N_Expanded_Name then
7448 -- Entity already present, analyze prefix, whose meaning may be an
7449 -- instance in the current context. If it is an instance of a
7450 -- relative within another, the proper parent may still have to be
7451 -- installed, if they are not of the same generation.
7453 Analyze (Prefix (Gen_Id));
7455 -- Prevent cascaded errors
7457 if Etype (Prefix (Gen_Id)) = Any_Type then
7458 return;
7459 end if;
7461 -- In the unlikely case that a local declaration hides the name of
7462 -- the parent package, locate it on the homonym chain. If the context
7463 -- is an instance of the parent, the renaming entity is flagged as
7464 -- such.
7466 Inst_Par := Entity (Prefix (Gen_Id));
7467 while Present (Inst_Par)
7468 and then not Is_Package_Or_Generic_Package (Inst_Par)
7469 loop
7470 Inst_Par := Homonym (Inst_Par);
7471 end loop;
7473 pragma Assert (Present (Inst_Par));
7474 Set_Entity (Prefix (Gen_Id), Inst_Par);
7476 if In_Enclosing_Instance then
7477 null;
7479 elsif Present (Entity (Gen_Id))
7480 and then No (Renamed_Entity (Entity (Gen_Id)))
7481 and then Is_Child_Unit (Entity (Gen_Id))
7482 and then not In_Open_Scopes (Inst_Par)
7483 then
7484 Install_Parent (Inst_Par);
7485 Parent_Installed := True;
7487 -- Handle renaming of generic child unit
7489 elsif Present (Entity (Gen_Id))
7490 and then Present (Renamed_Entity (Entity (Gen_Id)))
7491 and then Is_Child_Unit (Renamed_Entity (Entity (Gen_Id)))
7492 then
7493 declare
7494 E : Entity_Id;
7495 Ren_Decl : Node_Id;
7497 begin
7498 -- The entity of the renamed generic child unit does not
7499 -- have any reference to the instantiated parent. In order to
7500 -- locate it we traverse the scope containing the renaming
7501 -- declaration; the instance of the parent is available in
7502 -- the prefix of the renaming declaration. For example:
7504 -- package A is
7505 -- package Inst_Par is new ...
7506 -- generic package Ren_Child renames Ins_Par.Child;
7507 -- end;
7509 -- with A;
7510 -- package B is
7511 -- package Inst_Child is new A.Ren_Child;
7512 -- end;
7514 E := First_Entity (Entity (Prefix (Gen_Id)));
7515 while Present (E) loop
7516 if not Is_Object (E)
7517 and then Present (Renamed_Entity (E))
7518 and then
7519 Renamed_Entity (E) = Renamed_Entity (Entity (Gen_Id))
7520 then
7521 Ren_Decl := Parent (E);
7522 Inst_Par := Entity (Prefix (Name (Ren_Decl)));
7524 if not In_Open_Scopes (Inst_Par) then
7525 Install_Parent (Inst_Par);
7526 Parent_Installed := True;
7527 end if;
7529 exit;
7530 end if;
7532 E := Next_Entity (E);
7533 end loop;
7534 end;
7535 end if;
7537 elsif In_Enclosing_Instance then
7539 -- The child unit is found in some enclosing scope
7541 null;
7543 else
7544 Analyze (Gen_Id);
7546 -- If this is the renaming of the implicit child in a parent
7547 -- instance, recover the parent name and install it.
7549 if Is_Entity_Name (Gen_Id) then
7550 E := Entity (Gen_Id);
7552 if Is_Generic_Unit (E)
7553 and then Nkind (Parent (E)) in N_Generic_Renaming_Declaration
7554 and then Is_Child_Unit (Renamed_Entity (E))
7555 and then Is_Generic_Unit (Scope (Renamed_Entity (E)))
7556 and then Nkind (Name (Parent (E))) = N_Expanded_Name
7557 then
7558 Rewrite (Gen_Id, New_Copy_Tree (Name (Parent (E))));
7559 Inst_Par := Entity (Prefix (Gen_Id));
7561 if not In_Open_Scopes (Inst_Par) then
7562 Install_Parent (Inst_Par);
7563 Parent_Installed := True;
7564 end if;
7566 -- If it is a child unit of a non-generic parent, it may be
7567 -- use-visible and given by a direct name. Install parent as
7568 -- for other cases.
7570 elsif Is_Generic_Unit (E)
7571 and then Is_Child_Unit (E)
7572 and then
7573 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
7574 and then not Is_Generic_Unit (Scope (E))
7575 then
7576 if not In_Open_Scopes (Scope (E)) then
7577 Install_Parent (Scope (E));
7578 Parent_Installed := True;
7579 end if;
7580 end if;
7581 end if;
7582 end if;
7583 end Check_Generic_Child_Unit;
7585 -----------------------------
7586 -- Check_Hidden_Child_Unit --
7587 -----------------------------
7589 procedure Check_Hidden_Child_Unit
7590 (N : Node_Id;
7591 Gen_Unit : Entity_Id;
7592 Act_Decl_Id : Entity_Id)
7594 Gen_Id : constant Node_Id := Name (N);
7596 begin
7597 if Is_Child_Unit (Gen_Unit)
7598 and then Is_Child_Unit (Act_Decl_Id)
7599 and then Nkind (Gen_Id) = N_Expanded_Name
7600 and then Entity (Prefix (Gen_Id)) = Scope (Act_Decl_Id)
7601 and then Chars (Gen_Unit) = Chars (Act_Decl_Id)
7602 then
7603 Error_Msg_Node_2 := Scope (Act_Decl_Id);
7604 Error_Msg_NE
7605 ("generic unit & is implicitly declared in &",
7606 Defining_Unit_Name (N), Gen_Unit);
7607 Error_Msg_N ("\instance must have different name",
7608 Defining_Unit_Name (N));
7609 end if;
7610 end Check_Hidden_Child_Unit;
7612 ------------------------
7613 -- Check_Private_View --
7614 ------------------------
7616 procedure Check_Private_View (N : Node_Id) is
7617 T : constant Entity_Id := Etype (N);
7618 BT : Entity_Id;
7620 begin
7621 -- Exchange views if the type was not private in the generic but is
7622 -- private at the point of instantiation. Do not exchange views if
7623 -- the scope of the type is in scope. This can happen if both generic
7624 -- and instance are sibling units, or if type is defined in a parent.
7625 -- In this case the visibility of the type will be correct for all
7626 -- semantic checks.
7628 if Present (T) then
7629 BT := Base_Type (T);
7631 if Is_Private_Type (T)
7632 and then not Has_Private_View (N)
7633 and then Present (Full_View (T))
7634 and then not In_Open_Scopes (Scope (T))
7635 then
7636 -- In the generic, the full declaration was visible
7638 Switch_View (T);
7640 elsif Has_Private_View (N)
7641 and then not Is_Private_Type (T)
7642 and then not Has_Been_Exchanged (T)
7643 and then (not In_Open_Scopes (Scope (T))
7644 or else Nkind (Parent (N)) = N_Subtype_Declaration)
7645 then
7646 -- In the generic, only the private declaration was visible
7648 -- If the type appears in a subtype declaration, the subtype in
7649 -- instance must have a view compatible with that of its parent,
7650 -- which must be exchanged (see corresponding code in Restore_
7651 -- Private_Views) so we make an exception to the open scope rule.
7653 Prepend_Elmt (T, Exchanged_Views);
7654 Exchange_Declarations (Etype (Get_Associated_Node (N)));
7656 -- Finally, a non-private subtype may have a private base type, which
7657 -- must be exchanged for consistency. This can happen when a package
7658 -- body is instantiated, when the scope stack is empty but in fact
7659 -- the subtype and the base type are declared in an enclosing scope.
7661 -- Note that in this case we introduce an inconsistency in the view
7662 -- set, because we switch the base type BT, but there could be some
7663 -- private dependent subtypes of BT which remain unswitched. Such
7664 -- subtypes might need to be switched at a later point (see specific
7665 -- provision for that case in Switch_View).
7667 elsif not Is_Private_Type (T)
7668 and then not Has_Private_View (N)
7669 and then Is_Private_Type (BT)
7670 and then Present (Full_View (BT))
7671 and then not Is_Generic_Type (BT)
7672 and then not In_Open_Scopes (BT)
7673 then
7674 Prepend_Elmt (Full_View (BT), Exchanged_Views);
7675 Exchange_Declarations (BT);
7676 end if;
7677 end if;
7678 end Check_Private_View;
7680 -----------------------------
7681 -- Check_Hidden_Primitives --
7682 -----------------------------
7684 function Check_Hidden_Primitives (Assoc_List : List_Id) return Elist_Id is
7685 Actual : Node_Id;
7686 Gen_T : Entity_Id;
7687 Result : Elist_Id := No_Elist;
7689 begin
7690 if No (Assoc_List) then
7691 return No_Elist;
7692 end if;
7694 -- Traverse the list of associations between formals and actuals
7695 -- searching for renamings of tagged types
7697 Actual := First (Assoc_List);
7698 while Present (Actual) loop
7699 if Nkind (Actual) = N_Subtype_Declaration then
7700 Gen_T := Generic_Parent_Type (Actual);
7702 if Present (Gen_T) and then Is_Tagged_Type (Gen_T) then
7704 -- Traverse the list of primitives of the actual types
7705 -- searching for hidden primitives that are visible in the
7706 -- corresponding generic formal; leave them visible and
7707 -- append them to Result to restore their decoration later.
7709 Install_Hidden_Primitives
7710 (Prims_List => Result,
7711 Gen_T => Gen_T,
7712 Act_T => Entity (Subtype_Indication (Actual)));
7713 end if;
7714 end if;
7716 Next (Actual);
7717 end loop;
7719 return Result;
7720 end Check_Hidden_Primitives;
7722 --------------------------
7723 -- Contains_Instance_Of --
7724 --------------------------
7726 function Contains_Instance_Of
7727 (Inner : Entity_Id;
7728 Outer : Entity_Id;
7729 N : Node_Id) return Boolean
7731 Elmt : Elmt_Id;
7732 Scop : Entity_Id;
7734 begin
7735 Scop := Outer;
7737 -- Verify that there are no circular instantiations. We check whether
7738 -- the unit contains an instance of the current scope or some enclosing
7739 -- scope (in case one of the instances appears in a subunit). Longer
7740 -- circularities involving subunits might seem too pathological to
7741 -- consider, but they were not too pathological for the authors of
7742 -- DEC bc30vsq, so we loop over all enclosing scopes, and mark all
7743 -- enclosing generic scopes as containing an instance.
7745 loop
7746 -- Within a generic subprogram body, the scope is not generic, to
7747 -- allow for recursive subprograms. Use the declaration to determine
7748 -- whether this is a generic unit.
7750 if Ekind (Scop) = E_Generic_Package
7751 or else (Is_Subprogram (Scop)
7752 and then Nkind (Unit_Declaration_Node (Scop)) =
7753 N_Generic_Subprogram_Declaration)
7754 then
7755 Elmt := First_Elmt (Inner_Instances (Inner));
7757 while Present (Elmt) loop
7758 if Node (Elmt) = Scop then
7759 Error_Msg_Node_2 := Inner;
7760 Error_Msg_NE
7761 ("circular instantiation: & instantiated within &!",
7762 N, Scop);
7763 return True;
7765 elsif Node (Elmt) = Inner then
7766 return True;
7768 elsif Contains_Instance_Of (Node (Elmt), Scop, N) then
7769 Error_Msg_Node_2 := Inner;
7770 Error_Msg_NE
7771 ("circular instantiation: & instantiated within &!",
7772 N, Node (Elmt));
7773 return True;
7774 end if;
7776 Next_Elmt (Elmt);
7777 end loop;
7779 -- Indicate that Inner is being instantiated within Scop
7781 Append_Elmt (Inner, Inner_Instances (Scop));
7782 end if;
7784 if Scop = Standard_Standard then
7785 exit;
7786 else
7787 Scop := Scope (Scop);
7788 end if;
7789 end loop;
7791 return False;
7792 end Contains_Instance_Of;
7794 -----------------------
7795 -- Copy_Generic_Node --
7796 -----------------------
7798 function Copy_Generic_Node
7799 (N : Node_Id;
7800 Parent_Id : Node_Id;
7801 Instantiating : Boolean) return Node_Id
7803 Ent : Entity_Id;
7804 New_N : Node_Id;
7806 function Copy_Generic_Descendant (D : Union_Id) return Union_Id;
7807 -- Check the given value of one of the Fields referenced by the current
7808 -- node to determine whether to copy it recursively. The field may hold
7809 -- a Node_Id, a List_Id, or an Elist_Id, or a plain value (Sloc, Uint,
7810 -- Char) in which case it need not be copied.
7812 procedure Copy_Descendants;
7813 -- Common utility for various nodes
7815 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id;
7816 -- Make copy of element list
7818 function Copy_Generic_List
7819 (L : List_Id;
7820 Parent_Id : Node_Id) return List_Id;
7821 -- Apply Copy_Generic_Node recursively to the members of a node list
7823 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean;
7824 -- True if an identifier is part of the defining program unit name of
7825 -- a child unit.
7826 -- Consider removing this subprogram now that ASIS no longer uses it.
7828 ----------------------
7829 -- Copy_Descendants --
7830 ----------------------
7832 procedure Copy_Descendants is
7833 procedure Walk is new
7834 Walk_Sinfo_Fields_Pairwise (Copy_Generic_Descendant);
7835 begin
7836 Walk (New_N, N);
7837 end Copy_Descendants;
7839 -----------------------------
7840 -- Copy_Generic_Descendant --
7841 -----------------------------
7843 function Copy_Generic_Descendant (D : Union_Id) return Union_Id is
7844 begin
7845 if D = Union_Id (Empty) then
7846 return D;
7848 elsif D in Node_Range then
7849 return Union_Id
7850 (Copy_Generic_Node (Node_Id (D), New_N, Instantiating));
7852 elsif D in List_Range then
7853 return Union_Id (Copy_Generic_List (List_Id (D), New_N));
7855 elsif D in Elist_Range then
7856 return Union_Id (Copy_Generic_Elist (Elist_Id (D)));
7858 -- Nothing else is copyable (e.g. Uint values), return as is
7860 else
7861 return D;
7862 end if;
7863 end Copy_Generic_Descendant;
7865 ------------------------
7866 -- Copy_Generic_Elist --
7867 ------------------------
7869 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id is
7870 M : Elmt_Id;
7871 L : Elist_Id;
7873 begin
7874 if Present (E) then
7875 L := New_Elmt_List;
7876 M := First_Elmt (E);
7877 while Present (M) loop
7878 Append_Elmt
7879 (Copy_Generic_Node (Node (M), Empty, Instantiating), L);
7880 Next_Elmt (M);
7881 end loop;
7883 return L;
7885 else
7886 return No_Elist;
7887 end if;
7888 end Copy_Generic_Elist;
7890 -----------------------
7891 -- Copy_Generic_List --
7892 -----------------------
7894 function Copy_Generic_List
7895 (L : List_Id;
7896 Parent_Id : Node_Id) return List_Id
7898 N : Node_Id;
7899 New_L : List_Id;
7901 begin
7902 if Present (L) then
7903 New_L := New_List;
7904 Set_Parent (New_L, Parent_Id);
7906 N := First (L);
7907 while Present (N) loop
7908 Append (Copy_Generic_Node (N, Empty, Instantiating), New_L);
7909 Next (N);
7910 end loop;
7912 return New_L;
7914 else
7915 return No_List;
7916 end if;
7917 end Copy_Generic_List;
7919 ---------------------------
7920 -- In_Defining_Unit_Name --
7921 ---------------------------
7923 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean is
7924 begin
7925 return
7926 Present (Parent (Nam))
7927 and then (Nkind (Parent (Nam)) = N_Defining_Program_Unit_Name
7928 or else
7929 (Nkind (Parent (Nam)) = N_Expanded_Name
7930 and then In_Defining_Unit_Name (Parent (Nam))));
7931 end In_Defining_Unit_Name;
7933 -- Start of processing for Copy_Generic_Node
7935 begin
7936 if N = Empty then
7937 return N;
7938 end if;
7940 New_N := New_Copy (N);
7942 -- Copy aspects if present
7944 if Has_Aspects (N) then
7945 Set_Has_Aspects (New_N, False);
7946 Set_Aspect_Specifications
7947 (New_N, Copy_Generic_List (Aspect_Specifications (N), Parent_Id));
7948 end if;
7950 -- If we are instantiating, we want to adjust the sloc based on the
7951 -- current S_Adjustment. However, if this is the root node of a subunit,
7952 -- we need to defer that adjustment to below (see "elsif Instantiating
7953 -- and Was_Stub"), so it comes after Create_Instantiation_Source has
7954 -- computed the adjustment.
7956 if Instantiating
7957 and then not (Nkind (N) in N_Proper_Body
7958 and then Was_Originally_Stub (N))
7959 then
7960 Adjust_Instantiation_Sloc (New_N, S_Adjustment);
7961 end if;
7963 if not Is_List_Member (N) then
7964 Set_Parent (New_N, Parent_Id);
7965 end if;
7967 -- Special casing for identifiers and other entity names and operators
7969 if Nkind (New_N) in N_Character_Literal
7970 | N_Expanded_Name
7971 | N_Identifier
7972 | N_Operator_Symbol
7973 | N_Op
7974 then
7975 if not Instantiating then
7977 -- Link both nodes in order to assign subsequently the entity of
7978 -- the copy to the original node, in case this is a global
7979 -- reference.
7981 Set_Associated_Node (N, New_N);
7983 -- If we are within an instantiation, this is a nested generic
7984 -- that has already been analyzed at the point of definition.
7985 -- We must preserve references that were global to the enclosing
7986 -- parent at that point. Other occurrences, whether global or
7987 -- local to the current generic, must be resolved anew, so we
7988 -- reset the entity in the generic copy. A global reference has a
7989 -- smaller depth than the parent, or else the same depth in case
7990 -- both are distinct compilation units.
7992 -- A child unit is implicitly declared within the enclosing parent
7993 -- but is in fact global to it, and must be preserved.
7995 -- It is also possible for Current_Instantiated_Parent to be
7996 -- defined, and for this not to be a nested generic, namely if
7997 -- the unit is loaded through Rtsfind. In that case, the entity of
7998 -- New_N is only a link to the associated node, and not a defining
7999 -- occurrence.
8001 -- The entities for parent units in the defining_program_unit of a
8002 -- generic child unit are established when the context of the unit
8003 -- is first analyzed, before the generic copy is made. They are
8004 -- preserved in the copy for use in e.g. ASIS queries.
8006 Ent := Entity (New_N);
8008 if No (Current_Instantiated_Parent.Gen_Id) then
8009 if No (Ent)
8010 or else Nkind (Ent) /= N_Defining_Identifier
8011 or else not In_Defining_Unit_Name (N)
8012 then
8013 Set_Associated_Node (New_N, Empty);
8014 end if;
8016 elsif No (Ent)
8017 or else Nkind (Ent) not in N_Entity
8018 or else No (Scope (Ent))
8019 or else
8020 (Scope (Ent) = Current_Instantiated_Parent.Gen_Id
8021 and then not Is_Child_Unit (Ent))
8022 or else
8023 (Scope_Depth_Set (Scope (Ent))
8024 and then
8025 Scope_Depth (Scope (Ent)) >
8026 Scope_Depth (Current_Instantiated_Parent.Gen_Id)
8027 and then
8028 Get_Source_Unit (Ent) =
8029 Get_Source_Unit (Current_Instantiated_Parent.Gen_Id))
8030 then
8031 Set_Associated_Node (New_N, Empty);
8032 end if;
8034 -- Case of instantiating identifier or some other name or operator
8036 else
8037 -- If the associated node is still defined, the entity in it
8038 -- is global, and must be copied to the instance. If this copy
8039 -- is being made for a body to inline, it is applied to an
8040 -- instantiated tree, and the entity is already present and
8041 -- must be also preserved.
8043 declare
8044 Assoc : constant Node_Id := Get_Associated_Node (N);
8046 begin
8047 if Present (Assoc) then
8048 if Nkind (Assoc) = Nkind (N) then
8049 Set_Entity (New_N, Entity (Assoc));
8050 Check_Private_View (N);
8052 -- Here we deal with a very peculiar case for which the
8053 -- Has_Private_View mechanism is not sufficient, because
8054 -- the reference to the type is implicit in the tree,
8055 -- that is to say, it's not referenced from a node but
8056 -- only from another type, namely through Component_Type.
8058 -- package P is
8060 -- type Pt is private;
8062 -- generic
8063 -- type Ft is array (Positive range <>) of Pt;
8064 -- package G is
8065 -- procedure Check (F1, F2 : Ft; Lt : Boolean);
8066 -- end G;
8068 -- private
8069 -- type Pt is new Boolean;
8070 -- end P;
8072 -- package body P is
8073 -- package body G is
8074 -- procedure Check (F1, F2 : Ft; Lt : Boolean) is
8075 -- begin
8076 -- if (F1 < F2) /= Lt then
8077 -- null;
8078 -- end if;
8079 -- end Check;
8080 -- end G;
8081 -- end P;
8083 -- type Arr is array (Positive range <>) of P.Pt;
8085 -- package Inst is new P.G (Arr);
8087 -- Pt is a global type for the generic package G and it
8088 -- is not referenced in its body, but only as component
8089 -- type of Ft, which is a local type. This means that no
8090 -- references to Pt or Ft are seen during the copy of the
8091 -- body, the only reference to Pt being seen is when the
8092 -- actuals are checked by Check_Generic_Actuals, but Pt
8093 -- is still private at this point. In the end, the views
8094 -- of Pt are not switched in the body and, therefore, the
8095 -- array comparison is rejected because the component is
8096 -- still private.
8098 -- Adding e.g. a dummy variable of type Pt in the body is
8099 -- sufficient to make everything work, so we generate an
8100 -- artificial reference to Pt on the fly and thus force
8101 -- the switching of views on the grounds that, if the
8102 -- comparison was accepted during the semantic analysis
8103 -- of the generic, this means that the component cannot
8104 -- have been private (see Sem_Type.Valid_Comparison_Arg).
8106 if Nkind (Assoc) in N_Op_Compare
8107 and then Present (Etype (Left_Opnd (Assoc)))
8108 and then Is_Array_Type (Etype (Left_Opnd (Assoc)))
8109 and then Present (Etype (Right_Opnd (Assoc)))
8110 and then Is_Array_Type (Etype (Right_Opnd (Assoc)))
8111 then
8112 declare
8113 Ltyp : constant Entity_Id :=
8114 Etype (Left_Opnd (Assoc));
8115 Rtyp : constant Entity_Id :=
8116 Etype (Right_Opnd (Assoc));
8117 begin
8118 if Is_Private_Type (Component_Type (Ltyp)) then
8119 Check_Private_View
8120 (New_Occurrence_Of (Component_Type (Ltyp),
8121 Sloc (N)));
8122 end if;
8123 if Is_Private_Type (Component_Type (Rtyp)) then
8124 Check_Private_View
8125 (New_Occurrence_Of (Component_Type (Rtyp),
8126 Sloc (N)));
8127 end if;
8128 end;
8130 -- Here is a similar case, for the Designated_Type of an
8131 -- access type that is present as target type in a type
8132 -- conversion from another access type. In this case, if
8133 -- the base types of the designated types are different
8134 -- and the conversion was accepted during the semantic
8135 -- analysis of the generic, this means that the target
8136 -- type cannot have been private (see Valid_Conversion).
8138 elsif Nkind (Assoc) = N_Identifier
8139 and then Nkind (Parent (Assoc)) = N_Type_Conversion
8140 and then Subtype_Mark (Parent (Assoc)) = Assoc
8141 and then Present (Etype (Assoc))
8142 and then Is_Access_Type (Etype (Assoc))
8143 and then Present (Etype (Expression (Parent (Assoc))))
8144 and then
8145 Is_Access_Type (Etype (Expression (Parent (Assoc))))
8146 then
8147 declare
8148 Targ_Desig : constant Entity_Id :=
8149 Designated_Type (Etype (Assoc));
8150 Expr_Desig : constant Entity_Id :=
8151 Designated_Type
8152 (Etype (Expression (Parent (Assoc))));
8153 begin
8154 if Base_Type (Targ_Desig) /= Base_Type (Expr_Desig)
8155 and then Is_Private_Type (Targ_Desig)
8156 then
8157 Check_Private_View
8158 (New_Occurrence_Of (Targ_Desig, Sloc (N)));
8159 end if;
8160 end;
8161 end if;
8163 -- The node is a reference to a global type and acts as the
8164 -- subtype mark of a qualified expression created in order
8165 -- to aid resolution of accidental overloading in instances.
8166 -- Since N is a reference to a type, the Associated_Node of
8167 -- N denotes an entity rather than another identifier. See
8168 -- Qualify_Universal_Operands for details.
8170 elsif Nkind (N) = N_Identifier
8171 and then Nkind (Parent (N)) = N_Qualified_Expression
8172 and then Subtype_Mark (Parent (N)) = N
8173 and then Is_Qualified_Universal_Literal (Parent (N))
8174 then
8175 Set_Entity (New_N, Assoc);
8177 -- The name in the call may be a selected component if the
8178 -- call has not been analyzed yet, as may be the case for
8179 -- pre/post conditions in a generic unit.
8181 elsif Nkind (Assoc) = N_Function_Call
8182 and then Is_Entity_Name (Name (Assoc))
8183 then
8184 Set_Entity (New_N, Entity (Name (Assoc)));
8186 elsif Nkind (Assoc) in N_Entity
8187 and then (Expander_Active or
8188 (GNATprove_Mode
8189 and then not In_Spec_Expression
8190 and then not Inside_A_Generic))
8191 then
8192 -- Inlining case: we are copying a tree that contains
8193 -- global entities, which are preserved in the copy to be
8194 -- used for subsequent inlining.
8196 null;
8198 else
8199 Set_Entity (New_N, Empty);
8200 end if;
8201 end if;
8202 end;
8203 end if;
8205 -- For expanded name, we must copy the Prefix and Selector_Name
8207 if Nkind (N) = N_Expanded_Name then
8208 Set_Prefix
8209 (New_N, Copy_Generic_Node (Prefix (N), New_N, Instantiating));
8211 Set_Selector_Name (New_N,
8212 Copy_Generic_Node (Selector_Name (N), New_N, Instantiating));
8214 -- For operators, copy the operands
8216 elsif Nkind (N) in N_Op then
8217 if Nkind (N) in N_Binary_Op then
8218 Set_Left_Opnd (New_N,
8219 Copy_Generic_Node (Left_Opnd (N), New_N, Instantiating));
8220 end if;
8222 Set_Right_Opnd (New_N,
8223 Copy_Generic_Node (Right_Opnd (N), New_N, Instantiating));
8224 end if;
8226 -- Establish a link between an entity from the generic template and the
8227 -- corresponding entity in the generic copy to be analyzed.
8229 elsif Nkind (N) in N_Entity then
8230 if not Instantiating then
8231 Set_Associated_Entity (N, New_N);
8232 end if;
8234 -- Clear any existing link the copy may inherit from the replicated
8235 -- generic template entity.
8237 Set_Associated_Entity (New_N, Empty);
8239 -- Special casing for stubs
8241 elsif Nkind (N) in N_Body_Stub then
8243 -- In any case, we must copy the specification or defining
8244 -- identifier as appropriate.
8246 if Nkind (N) = N_Subprogram_Body_Stub then
8247 Set_Specification (New_N,
8248 Copy_Generic_Node (Specification (N), New_N, Instantiating));
8250 else
8251 Set_Defining_Identifier (New_N,
8252 Copy_Generic_Node
8253 (Defining_Identifier (N), New_N, Instantiating));
8254 end if;
8256 -- If we are not instantiating, then this is where we load and
8257 -- analyze subunits, i.e. at the point where the stub occurs. A
8258 -- more permissive system might defer this analysis to the point
8259 -- of instantiation, but this seems too complicated for now.
8261 if not Instantiating then
8262 declare
8263 Subunit_Name : constant Unit_Name_Type := Get_Unit_Name (N);
8264 Subunit : Node_Id;
8265 Unum : Unit_Number_Type;
8266 New_Body : Node_Id;
8268 begin
8269 -- Make sure that, if it is a subunit of the main unit that is
8270 -- preprocessed and if -gnateG is specified, the preprocessed
8271 -- file will be written.
8273 Lib.Analysing_Subunit_Of_Main :=
8274 Lib.In_Extended_Main_Source_Unit (N);
8275 Unum :=
8276 Load_Unit
8277 (Load_Name => Subunit_Name,
8278 Required => False,
8279 Subunit => True,
8280 Error_Node => N);
8281 Lib.Analysing_Subunit_Of_Main := False;
8283 -- If the proper body is not found, a warning message will be
8284 -- emitted when analyzing the stub, or later at the point of
8285 -- instantiation. Here we just leave the stub as is.
8287 if Unum = No_Unit then
8288 Subunits_Missing := True;
8289 goto Subunit_Not_Found;
8290 end if;
8292 Subunit := Cunit (Unum);
8294 if Nkind (Unit (Subunit)) /= N_Subunit then
8295 Error_Msg_N
8296 ("found child unit instead of expected SEPARATE subunit",
8297 Subunit);
8298 Error_Msg_Sloc := Sloc (N);
8299 Error_Msg_N ("\to complete stub #", Subunit);
8300 goto Subunit_Not_Found;
8301 end if;
8303 -- We must create a generic copy of the subunit, in order to
8304 -- perform semantic analysis on it, and we must replace the
8305 -- stub in the original generic unit with the subunit, in order
8306 -- to preserve non-local references within.
8308 -- Only the proper body needs to be copied. Library_Unit and
8309 -- context clause are simply inherited by the generic copy.
8310 -- Note that the copy (which may be recursive if there are
8311 -- nested subunits) must be done first, before attaching it to
8312 -- the enclosing generic.
8314 New_Body :=
8315 Copy_Generic_Node
8316 (Proper_Body (Unit (Subunit)),
8317 Empty, Instantiating => False);
8319 -- Now place the original proper body in the original generic
8320 -- unit. This is a body, not a compilation unit.
8322 Rewrite (N, Proper_Body (Unit (Subunit)));
8323 Set_Is_Compilation_Unit (Defining_Entity (N), False);
8324 Set_Was_Originally_Stub (N);
8326 -- Finally replace the body of the subunit with its copy, and
8327 -- make this new subunit into the library unit of the generic
8328 -- copy, which does not have stubs any longer.
8330 Set_Proper_Body (Unit (Subunit), New_Body);
8331 Set_Library_Unit (New_N, Subunit);
8332 Inherit_Context (Unit (Subunit), N);
8333 end;
8335 -- If we are instantiating, this must be an error case, since
8336 -- otherwise we would have replaced the stub node by the proper body
8337 -- that corresponds. So just ignore it in the copy (i.e. we have
8338 -- copied it, and that is good enough).
8340 else
8341 null;
8342 end if;
8344 <<Subunit_Not_Found>> null;
8346 -- If the node is a compilation unit, it is the subunit of a stub, which
8347 -- has been loaded already (see code below). In this case, the library
8348 -- unit field of N points to the parent unit (which is a compilation
8349 -- unit) and need not (and cannot) be copied.
8351 -- When the proper body of the stub is analyzed, the library_unit link
8352 -- is used to establish the proper context (see sem_ch10).
8354 -- The other fields of a compilation unit are copied as usual
8356 elsif Nkind (N) = N_Compilation_Unit then
8358 -- This code can only be executed when not instantiating, because in
8359 -- the copy made for an instantiation, the compilation unit node has
8360 -- disappeared at the point that a stub is replaced by its proper
8361 -- body.
8363 pragma Assert (not Instantiating);
8365 Set_Context_Items (New_N,
8366 Copy_Generic_List (Context_Items (N), New_N));
8368 Set_Unit (New_N,
8369 Copy_Generic_Node (Unit (N), New_N, Instantiating => False));
8371 Set_First_Inlined_Subprogram (New_N,
8372 Copy_Generic_Node
8373 (First_Inlined_Subprogram (N), New_N, Instantiating => False));
8375 Set_Aux_Decls_Node
8376 (New_N,
8377 Copy_Generic_Node
8378 (Aux_Decls_Node (N), New_N, Instantiating => False));
8380 -- For an assignment node, the assignment is known to be semantically
8381 -- legal if we are instantiating the template. This avoids incorrect
8382 -- diagnostics in generated code.
8384 elsif Nkind (N) = N_Assignment_Statement then
8386 -- Copy name and expression fields in usual manner
8388 Set_Name (New_N,
8389 Copy_Generic_Node (Name (N), New_N, Instantiating));
8391 Set_Expression (New_N,
8392 Copy_Generic_Node (Expression (N), New_N, Instantiating));
8394 if Instantiating then
8395 Set_Assignment_OK (Name (New_N), True);
8396 end if;
8398 elsif Nkind (N) in N_Aggregate | N_Extension_Aggregate then
8399 if not Instantiating then
8400 Set_Associated_Node (N, New_N);
8402 else
8403 if Present (Get_Associated_Node (N))
8404 and then Nkind (Get_Associated_Node (N)) = Nkind (N)
8405 then
8406 -- In the generic the aggregate has some composite type. If at
8407 -- the point of instantiation the type has a private view,
8408 -- install the full view (and that of its ancestors, if any).
8410 declare
8411 T : Entity_Id := (Etype (Get_Associated_Node (New_N)));
8412 Rt : Entity_Id;
8414 begin
8415 if Present (T) and then Is_Private_Type (T) then
8416 Switch_View (T);
8417 end if;
8419 if Present (T)
8420 and then Is_Tagged_Type (T)
8421 and then Is_Derived_Type (T)
8422 then
8423 Rt := Root_Type (T);
8425 loop
8426 T := Etype (T);
8428 if Is_Private_Type (T) then
8429 Switch_View (T);
8430 end if;
8432 exit when T = Rt;
8433 end loop;
8434 end if;
8435 end;
8436 end if;
8437 end if;
8439 -- Do not copy the associated node, which points to the generic copy
8440 -- of the aggregate.
8442 if Nkind (N) = N_Aggregate then
8443 Set_Aggregate_Bounds
8444 (New_N,
8445 Node_Id (Copy_Generic_Descendant
8446 (Union_Id (Aggregate_Bounds (N)))));
8448 elsif Nkind (N) = N_Extension_Aggregate then
8449 Set_Ancestor_Part
8450 (New_N,
8451 Node_Id (Copy_Generic_Descendant
8452 (Union_Id (Ancestor_Part (N)))));
8454 else
8455 pragma Assert (False);
8456 end if;
8458 Set_Expressions
8459 (New_N,
8460 List_Id (Copy_Generic_Descendant (Union_Id (Expressions (N)))));
8461 Set_Component_Associations
8462 (New_N,
8463 List_Id (Copy_Generic_Descendant
8464 (Union_Id (Component_Associations (N)))));
8465 Set_Etype
8466 (New_N, Node_Id (Copy_Generic_Descendant (Union_Id (Etype (N)))));
8468 -- Allocators do not have an identifier denoting the access type, so we
8469 -- must locate it through the expression to check whether the views are
8470 -- consistent.
8472 elsif Nkind (N) = N_Allocator
8473 and then Nkind (Expression (N)) = N_Qualified_Expression
8474 and then Is_Entity_Name (Subtype_Mark (Expression (N)))
8475 and then Instantiating
8476 then
8477 declare
8478 T : constant Node_Id :=
8479 Get_Associated_Node (Subtype_Mark (Expression (N)));
8480 Acc_T : Entity_Id;
8482 begin
8483 if Present (T) then
8485 -- Retrieve the allocator node in the generic copy
8487 Acc_T := Etype (Parent (Parent (T)));
8489 if Present (Acc_T) and then Is_Private_Type (Acc_T) then
8490 Switch_View (Acc_T);
8491 end if;
8492 end if;
8494 Copy_Descendants;
8495 end;
8497 -- For a proper body, we must catch the case of a proper body that
8498 -- replaces a stub. This represents the point at which a separate
8499 -- compilation unit, and hence template file, may be referenced, so we
8500 -- must make a new source instantiation entry for the template of the
8501 -- subunit, and ensure that all nodes in the subunit are adjusted using
8502 -- this new source instantiation entry.
8504 elsif Nkind (N) in N_Proper_Body then
8505 declare
8506 Save_Adjustment : constant Sloc_Adjustment := S_Adjustment;
8507 begin
8508 if Instantiating and then Was_Originally_Stub (N) then
8509 Create_Instantiation_Source
8510 (Instantiation_Node,
8511 Defining_Entity (N),
8512 S_Adjustment);
8514 Adjust_Instantiation_Sloc (New_N, S_Adjustment);
8515 end if;
8517 -- Now copy the fields of the proper body, using the new
8518 -- adjustment factor if one was needed as per test above.
8520 Copy_Descendants;
8522 -- Restore the original adjustment factor
8524 S_Adjustment := Save_Adjustment;
8525 end;
8527 elsif Nkind (N) = N_Pragma and then Instantiating then
8529 -- Do not copy Comment or Ident pragmas their content is relevant to
8530 -- the generic unit, not to the instantiating unit.
8532 if Pragma_Name_Unmapped (N) in Name_Comment | Name_Ident then
8533 New_N := Make_Null_Statement (Sloc (N));
8535 -- Do not copy pragmas generated from aspects because the pragmas do
8536 -- not carry any semantic information, plus they will be regenerated
8537 -- in the instance.
8539 -- However, generating C we need to copy them since postconditions
8540 -- are inlined by the front end, and the front-end inlining machinery
8541 -- relies on this routine to perform inlining.
8543 elsif From_Aspect_Specification (N)
8544 and then not Modify_Tree_For_C
8545 then
8546 New_N := Make_Null_Statement (Sloc (N));
8548 else
8549 Copy_Descendants;
8550 end if;
8552 elsif Nkind (N) in N_Integer_Literal | N_Real_Literal then
8554 -- No descendant fields need traversing
8556 null;
8558 elsif Nkind (N) = N_String_Literal
8559 and then Present (Etype (N))
8560 and then Instantiating
8561 then
8562 -- If the string is declared in an outer scope, the string_literal
8563 -- subtype created for it may have the wrong scope. Force reanalysis
8564 -- of the constant to generate a new itype in the proper context.
8566 Set_Etype (New_N, Empty);
8567 Set_Analyzed (New_N, False);
8569 -- For the remaining nodes, copy their descendants recursively
8571 else
8572 Copy_Descendants;
8574 if Instantiating and then Nkind (N) = N_Subprogram_Body then
8575 Set_Generic_Parent (Specification (New_N), N);
8577 -- Should preserve Corresponding_Spec??? (12.3(14))
8578 end if;
8579 end if;
8581 -- Propagate dimensions if present, so that they are reflected in the
8582 -- instance.
8584 if Nkind (N) in N_Has_Etype
8585 and then (Nkind (N) in N_Op or else Is_Entity_Name (N))
8586 and then Present (Etype (N))
8587 and then Is_Floating_Point_Type (Etype (N))
8588 and then Has_Dimension_System (Etype (N))
8589 then
8590 Copy_Dimensions (N, New_N);
8591 end if;
8593 return New_N;
8594 end Copy_Generic_Node;
8596 ----------------------------
8597 -- Denotes_Formal_Package --
8598 ----------------------------
8600 function Denotes_Formal_Package
8601 (Pack : Entity_Id;
8602 On_Exit : Boolean := False;
8603 Instance : Entity_Id := Empty) return Boolean
8605 Par : Entity_Id;
8606 Scop : constant Entity_Id := Scope (Pack);
8607 E : Entity_Id;
8609 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean;
8610 -- The package in question may be an actual for a previous formal
8611 -- package P of the current instance, so examine its actuals as well.
8612 -- This must be recursive over other formal packages.
8614 ----------------------------------
8615 -- Is_Actual_Of_Previous_Formal --
8616 ----------------------------------
8618 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean is
8619 E1 : Entity_Id;
8621 begin
8622 E1 := First_Entity (P);
8623 while Present (E1) and then E1 /= Instance loop
8624 if Ekind (E1) = E_Package
8625 and then Nkind (Parent (E1)) = N_Package_Renaming_Declaration
8626 then
8627 if Renamed_Entity (E1) = Pack then
8628 return True;
8630 elsif E1 = P or else Renamed_Entity (E1) = P then
8631 return False;
8633 elsif Is_Actual_Of_Previous_Formal (E1) then
8634 return True;
8635 end if;
8636 end if;
8638 Next_Entity (E1);
8639 end loop;
8641 return False;
8642 end Is_Actual_Of_Previous_Formal;
8644 -- Start of processing for Denotes_Formal_Package
8646 begin
8647 if On_Exit then
8648 Par :=
8649 Instance_Envs.Table
8650 (Instance_Envs.Last).Instantiated_Parent.Act_Id;
8651 else
8652 Par := Current_Instantiated_Parent.Act_Id;
8653 end if;
8655 if Ekind (Scop) = E_Generic_Package
8656 or else Nkind (Unit_Declaration_Node (Scop)) =
8657 N_Generic_Subprogram_Declaration
8658 then
8659 return True;
8661 elsif Nkind (Original_Node (Unit_Declaration_Node (Pack))) =
8662 N_Formal_Package_Declaration
8663 then
8664 return True;
8666 elsif No (Par) then
8667 return False;
8669 else
8670 -- Check whether this package is associated with a formal package of
8671 -- the enclosing instantiation. Iterate over the list of renamings.
8673 E := First_Entity (Par);
8674 while Present (E) loop
8675 if Ekind (E) /= E_Package
8676 or else Nkind (Parent (E)) /= N_Package_Renaming_Declaration
8677 then
8678 null;
8680 elsif Renamed_Entity (E) = Par then
8681 return False;
8683 elsif Renamed_Entity (E) = Pack then
8684 return True;
8686 elsif Is_Actual_Of_Previous_Formal (E) then
8687 return True;
8689 end if;
8691 Next_Entity (E);
8692 end loop;
8694 return False;
8695 end if;
8696 end Denotes_Formal_Package;
8698 -----------------
8699 -- End_Generic --
8700 -----------------
8702 procedure End_Generic is
8703 begin
8704 -- ??? More things could be factored out in this routine. Should
8705 -- probably be done at a later stage.
8707 Inside_A_Generic := Generic_Flags.Table (Generic_Flags.Last);
8708 Generic_Flags.Decrement_Last;
8710 Expander_Mode_Restore;
8711 end End_Generic;
8713 -------------
8714 -- Earlier --
8715 -------------
8717 function Earlier (N1, N2 : Node_Id) return Boolean is
8718 procedure Find_Depth (P : in out Node_Id; D : in out Integer);
8719 -- Find distance from given node to enclosing compilation unit
8721 ----------------
8722 -- Find_Depth --
8723 ----------------
8725 procedure Find_Depth (P : in out Node_Id; D : in out Integer) is
8726 begin
8727 while Present (P)
8728 and then Nkind (P) /= N_Compilation_Unit
8729 loop
8730 P := True_Parent (P);
8731 D := D + 1;
8732 end loop;
8733 end Find_Depth;
8735 -- Local declarations
8737 D1 : Integer := 0;
8738 D2 : Integer := 0;
8739 P1 : Node_Id := N1;
8740 P2 : Node_Id := N2;
8741 T1 : Source_Ptr;
8742 T2 : Source_Ptr;
8744 -- Start of processing for Earlier
8746 begin
8747 Find_Depth (P1, D1);
8748 Find_Depth (P2, D2);
8750 if P1 /= P2 then
8751 return False;
8752 else
8753 P1 := N1;
8754 P2 := N2;
8755 end if;
8757 while D1 > D2 loop
8758 P1 := True_Parent (P1);
8759 D1 := D1 - 1;
8760 end loop;
8762 while D2 > D1 loop
8763 P2 := True_Parent (P2);
8764 D2 := D2 - 1;
8765 end loop;
8767 -- At this point P1 and P2 are at the same distance from the root.
8768 -- We examine their parents until we find a common declarative list.
8769 -- If we reach the root, N1 and N2 do not descend from the same
8770 -- declarative list (e.g. one is nested in the declarative part and
8771 -- the other is in a block in the statement part) and the earlier
8772 -- one is already frozen.
8774 while not Is_List_Member (P1)
8775 or else not Is_List_Member (P2)
8776 or else not In_Same_List (P1, P2)
8777 loop
8778 P1 := True_Parent (P1);
8779 P2 := True_Parent (P2);
8781 if Nkind (Parent (P1)) = N_Subunit then
8782 P1 := Corresponding_Stub (Parent (P1));
8783 end if;
8785 if Nkind (Parent (P2)) = N_Subunit then
8786 P2 := Corresponding_Stub (Parent (P2));
8787 end if;
8789 if P1 = P2 then
8790 return False;
8791 end if;
8792 end loop;
8794 -- Expanded code usually shares the source location of the original
8795 -- construct it was generated for. This however may not necessarily
8796 -- reflect the true location of the code within the tree.
8798 -- Before comparing the slocs of the two nodes, make sure that we are
8799 -- working with correct source locations. Assume that P1 is to the left
8800 -- of P2. If either one does not come from source, traverse the common
8801 -- list heading towards the other node and locate the first source
8802 -- statement.
8804 -- P1 P2
8805 -- ----+===+===+--------------+===+===+----
8806 -- expanded code expanded code
8808 if not Comes_From_Source (P1) then
8809 while Present (P1) loop
8811 -- Neither P2 nor a source statement were located during the
8812 -- search. If we reach the end of the list, then P1 does not
8813 -- occur earlier than P2.
8815 -- ---->
8816 -- start --- P2 ----- P1 --- end
8818 if No (Next (P1)) then
8819 return False;
8821 -- We encounter P2 while going to the right of the list. This
8822 -- means that P1 does indeed appear earlier.
8824 -- ---->
8825 -- start --- P1 ===== P2 --- end
8826 -- expanded code in between
8828 elsif P1 = P2 then
8829 return True;
8831 -- No need to look any further since we have located a source
8832 -- statement.
8834 elsif Comes_From_Source (P1) then
8835 exit;
8836 end if;
8838 -- Keep going right
8840 Next (P1);
8841 end loop;
8842 end if;
8844 if not Comes_From_Source (P2) then
8845 while Present (P2) loop
8847 -- Neither P1 nor a source statement were located during the
8848 -- search. If we reach the start of the list, then P1 does not
8849 -- occur earlier than P2.
8851 -- <----
8852 -- start --- P2 --- P1 --- end
8854 if No (Prev (P2)) then
8855 return False;
8857 -- We encounter P1 while going to the left of the list. This
8858 -- means that P1 does indeed appear earlier.
8860 -- <----
8861 -- start --- P1 ===== P2 --- end
8862 -- expanded code in between
8864 elsif P2 = P1 then
8865 return True;
8867 -- No need to look any further since we have located a source
8868 -- statement.
8870 elsif Comes_From_Source (P2) then
8871 exit;
8872 end if;
8874 -- Keep going left
8876 Prev (P2);
8877 end loop;
8878 end if;
8880 -- At this point either both nodes came from source or we approximated
8881 -- their source locations through neighboring source statements.
8883 T1 := Top_Level_Location (Sloc (P1));
8884 T2 := Top_Level_Location (Sloc (P2));
8886 -- When two nodes come from the same instance, they have identical top
8887 -- level locations. To determine proper relation within the tree, check
8888 -- their locations within the template.
8890 if T1 = T2 then
8891 return Sloc (P1) < Sloc (P2);
8893 -- The two nodes either come from unrelated instances or do not come
8894 -- from instantiated code at all.
8896 else
8897 return T1 < T2;
8898 end if;
8899 end Earlier;
8901 ----------------------
8902 -- Find_Actual_Type --
8903 ----------------------
8905 function Find_Actual_Type
8906 (Typ : Entity_Id;
8907 Gen_Type : Entity_Id) return Entity_Id
8909 Gen_Scope : constant Entity_Id := Scope (Gen_Type);
8910 T : Entity_Id;
8912 begin
8913 -- Special processing only applies to child units
8915 if not Is_Child_Unit (Gen_Scope) then
8916 return Get_Instance_Of (Typ);
8918 -- If designated or component type is itself a formal of the child unit,
8919 -- its instance is available.
8921 elsif Scope (Typ) = Gen_Scope then
8922 return Get_Instance_Of (Typ);
8924 -- If the array or access type is not declared in the parent unit,
8925 -- no special processing needed.
8927 elsif not Is_Generic_Type (Typ)
8928 and then Scope (Gen_Scope) /= Scope (Typ)
8929 then
8930 return Get_Instance_Of (Typ);
8932 -- Otherwise, retrieve designated or component type by visibility
8934 else
8935 T := Current_Entity (Typ);
8936 while Present (T) loop
8937 if In_Open_Scopes (Scope (T)) then
8938 return T;
8939 elsif Is_Generic_Actual_Type (T) then
8940 return T;
8941 end if;
8943 T := Homonym (T);
8944 end loop;
8946 return Typ;
8947 end if;
8948 end Find_Actual_Type;
8950 -----------------------------
8951 -- Freeze_Package_Instance --
8952 -----------------------------
8954 procedure Freeze_Package_Instance
8955 (N : Node_Id;
8956 Gen_Body : Node_Id;
8957 Gen_Decl : Node_Id;
8958 Act_Id : Entity_Id)
8960 function In_Same_Scope (Gen_Id, Act_Id : Node_Id) return Boolean;
8961 -- Check if the generic definition and the instantiation come from
8962 -- a common scope, in which case the instance must be frozen after
8963 -- the generic body.
8965 function True_Sloc (N, Act_Unit : Node_Id) return Source_Ptr;
8966 -- If the instance is nested inside a generic unit, the Sloc of the
8967 -- instance indicates the place of the original definition, not the
8968 -- point of the current enclosing instance. Pending a better usage of
8969 -- Slocs to indicate instantiation places, we determine the place of
8970 -- origin of a node by finding the maximum sloc of any ancestor node.
8972 -- Why is this not equivalent to Top_Level_Location ???
8974 -------------------
8975 -- In_Same_Scope --
8976 -------------------
8978 function In_Same_Scope (Gen_Id, Act_Id : Node_Id) return Boolean is
8979 Act_Scop : Entity_Id := Scope (Act_Id);
8980 Gen_Scop : Entity_Id := Scope (Gen_Id);
8982 begin
8983 while Act_Scop /= Standard_Standard
8984 and then Gen_Scop /= Standard_Standard
8985 loop
8986 if Act_Scop = Gen_Scop then
8987 return True;
8988 end if;
8990 Act_Scop := Scope (Act_Scop);
8991 Gen_Scop := Scope (Gen_Scop);
8992 end loop;
8994 return False;
8995 end In_Same_Scope;
8997 ---------------
8998 -- True_Sloc --
8999 ---------------
9001 function True_Sloc (N, Act_Unit : Node_Id) return Source_Ptr is
9002 N1 : Node_Id;
9003 Res : Source_Ptr;
9005 begin
9006 Res := Sloc (N);
9007 N1 := N;
9008 while Present (N1) and then N1 /= Act_Unit loop
9009 if Sloc (N1) > Res then
9010 Res := Sloc (N1);
9011 end if;
9013 N1 := Parent (N1);
9014 end loop;
9016 return Res;
9017 end True_Sloc;
9019 -- Local variables
9021 Gen_Id : constant Entity_Id := Get_Generic_Entity (N);
9022 Par_Id : constant Entity_Id := Scope (Gen_Id);
9023 Act_Unit : constant Node_Id := Unit (Cunit (Get_Source_Unit (N)));
9024 Gen_Unit : constant Node_Id :=
9025 Unit (Cunit (Get_Source_Unit (Gen_Decl)));
9027 Body_Unit : Node_Id;
9028 F_Node : Node_Id;
9029 Must_Delay : Boolean;
9030 Orig_Body : Node_Id;
9032 -- Start of processing for Freeze_Package_Instance
9034 begin
9035 -- If the body is a subunit, the freeze point is the corresponding stub
9036 -- in the current compilation, not the subunit itself.
9038 if Nkind (Parent (Gen_Body)) = N_Subunit then
9039 Orig_Body := Corresponding_Stub (Parent (Gen_Body));
9040 else
9041 Orig_Body := Gen_Body;
9042 end if;
9044 Body_Unit := Unit (Cunit (Get_Source_Unit (Orig_Body)));
9046 -- If the instantiation and the generic definition appear in the same
9047 -- package declaration, this is an early instantiation. If they appear
9048 -- in the same declarative part, it is an early instantiation only if
9049 -- the generic body appears textually later, and the generic body is
9050 -- also in the main unit.
9052 -- If instance is nested within a subprogram, and the generic body
9053 -- is not, the instance is delayed because the enclosing body is. If
9054 -- instance and body are within the same scope, or the same subprogram
9055 -- body, indicate explicitly that the instance is delayed.
9057 Must_Delay :=
9058 (Gen_Unit = Act_Unit
9059 and then (Nkind (Gen_Unit) in N_Generic_Package_Declaration
9060 | N_Package_Declaration
9061 or else (Gen_Unit = Body_Unit
9062 and then
9063 True_Sloc (N, Act_Unit) < Sloc (Orig_Body)))
9064 and then Is_In_Main_Unit (Original_Node (Gen_Unit))
9065 and then In_Same_Scope (Gen_Id, Act_Id));
9067 -- If this is an early instantiation, the freeze node is placed after
9068 -- the generic body. Otherwise, if the generic appears in an instance,
9069 -- we cannot freeze the current instance until the outer one is frozen.
9070 -- This is only relevant if the current instance is nested within some
9071 -- inner scope not itself within the outer instance. If this scope is
9072 -- a package body in the same declarative part as the outer instance,
9073 -- then that body needs to be frozen after the outer instance. Finally,
9074 -- if no delay is needed, we place the freeze node at the end of the
9075 -- current declarative part.
9077 if No (Freeze_Node (Act_Id))
9078 or else not Is_List_Member (Freeze_Node (Act_Id))
9079 then
9080 Ensure_Freeze_Node (Act_Id);
9081 F_Node := Freeze_Node (Act_Id);
9083 if Must_Delay then
9084 Insert_After (Orig_Body, F_Node);
9086 elsif Is_Generic_Instance (Par_Id)
9087 and then Present (Freeze_Node (Par_Id))
9088 and then Scope (Act_Id) /= Par_Id
9089 then
9090 -- Freeze instance of inner generic after instance of enclosing
9091 -- generic.
9093 if In_Same_Declarative_Part (Parent (Freeze_Node (Par_Id)), N) then
9095 -- Handle the following case:
9097 -- package Parent_Inst is new ...
9098 -- freeze Parent_Inst []
9100 -- procedure P ... -- this body freezes Parent_Inst
9102 -- package Inst is new ...
9104 -- In this particular scenario, the freeze node for Inst must
9105 -- be inserted in the same manner as that of Parent_Inst,
9106 -- before the next source body or at the end of the declarative
9107 -- list (body not available). If body P did not exist and
9108 -- Parent_Inst was frozen after Inst, either by a body
9109 -- following Inst or at the end of the declarative region,
9110 -- the freeze node for Inst must be inserted after that of
9111 -- Parent_Inst. This relation is established by comparing
9112 -- the Slocs of Parent_Inst freeze node and Inst.
9113 -- We examine the parents of the enclosing lists to handle
9114 -- the case where the parent instance is in the visible part
9115 -- of a package declaration, and the inner instance is in
9116 -- the corresponding private part.
9118 if Parent (List_Containing (Freeze_Node (Par_Id)))
9119 = Parent (List_Containing (N))
9120 and then Sloc (Freeze_Node (Par_Id)) <= Sloc (N)
9121 then
9122 Insert_Freeze_Node_For_Instance (N, F_Node);
9123 else
9124 Insert_After (Freeze_Node (Par_Id), F_Node);
9125 end if;
9127 -- Freeze package enclosing instance of inner generic after
9128 -- instance of enclosing generic.
9130 elsif Nkind (Parent (N)) in N_Package_Body | N_Subprogram_Body
9131 and then In_Same_Declarative_Part
9132 (Parent (Freeze_Node (Par_Id)), Parent (N))
9133 then
9134 declare
9135 Enclosing : Entity_Id;
9137 begin
9138 Enclosing := Corresponding_Spec (Parent (N));
9140 if No (Enclosing) then
9141 Enclosing := Defining_Entity (Parent (N));
9142 end if;
9144 Insert_Freeze_Node_For_Instance (N, F_Node);
9145 Ensure_Freeze_Node (Enclosing);
9147 if not Is_List_Member (Freeze_Node (Enclosing)) then
9149 -- The enclosing context is a subunit, insert the freeze
9150 -- node after the stub.
9152 if Nkind (Parent (Parent (N))) = N_Subunit then
9153 Insert_Freeze_Node_For_Instance
9154 (Corresponding_Stub (Parent (Parent (N))),
9155 Freeze_Node (Enclosing));
9157 -- The enclosing context is a package with a stub body
9158 -- which has already been replaced by the real body.
9159 -- Insert the freeze node after the actual body.
9161 elsif Ekind (Enclosing) = E_Package
9162 and then Present (Body_Entity (Enclosing))
9163 and then Was_Originally_Stub
9164 (Parent (Body_Entity (Enclosing)))
9165 then
9166 Insert_Freeze_Node_For_Instance
9167 (Parent (Body_Entity (Enclosing)),
9168 Freeze_Node (Enclosing));
9170 -- The parent instance has been frozen before the body of
9171 -- the enclosing package, insert the freeze node after
9172 -- the body.
9174 elsif In_Same_List (Freeze_Node (Par_Id), Parent (N))
9175 and then
9176 Sloc (Freeze_Node (Par_Id)) <= Sloc (Parent (N))
9177 then
9178 Insert_Freeze_Node_For_Instance
9179 (Parent (N), Freeze_Node (Enclosing));
9181 else
9182 Insert_After
9183 (Freeze_Node (Par_Id), Freeze_Node (Enclosing));
9184 end if;
9185 end if;
9186 end;
9188 else
9189 Insert_Freeze_Node_For_Instance (N, F_Node);
9190 end if;
9192 else
9193 Insert_Freeze_Node_For_Instance (N, F_Node);
9194 end if;
9195 end if;
9196 end Freeze_Package_Instance;
9198 --------------------------------
9199 -- Freeze_Subprogram_Instance --
9200 --------------------------------
9202 procedure Freeze_Subprogram_Instance
9203 (N : Node_Id;
9204 Gen_Body : Node_Id;
9205 Pack_Id : Entity_Id)
9207 function Enclosing_Package_Body (N : Node_Id) return Node_Id;
9208 -- Find innermost package body that encloses the given node, and which
9209 -- is not a compilation unit. Freeze nodes for the instance, or for its
9210 -- enclosing body, may be inserted after the enclosing_body of the
9211 -- generic unit. Used to determine proper placement of freeze node for
9212 -- both package and subprogram instances.
9214 function Package_Freeze_Node (B : Node_Id) return Node_Id;
9215 -- Find entity for given package body, and locate or create a freeze
9216 -- node for it.
9218 ----------------------------
9219 -- Enclosing_Package_Body --
9220 ----------------------------
9222 function Enclosing_Package_Body (N : Node_Id) return Node_Id is
9223 P : Node_Id;
9225 begin
9226 P := Parent (N);
9227 while Present (P)
9228 and then Nkind (Parent (P)) /= N_Compilation_Unit
9229 loop
9230 if Nkind (P) = N_Package_Body then
9231 if Nkind (Parent (P)) = N_Subunit then
9232 return Corresponding_Stub (Parent (P));
9233 else
9234 return P;
9235 end if;
9236 end if;
9238 P := True_Parent (P);
9239 end loop;
9241 return Empty;
9242 end Enclosing_Package_Body;
9244 -------------------------
9245 -- Package_Freeze_Node --
9246 -------------------------
9248 function Package_Freeze_Node (B : Node_Id) return Node_Id is
9249 Id : Entity_Id;
9251 begin
9252 if Nkind (B) = N_Package_Body then
9253 Id := Corresponding_Spec (B);
9254 else pragma Assert (Nkind (B) = N_Package_Body_Stub);
9255 Id := Corresponding_Spec (Proper_Body (Unit (Library_Unit (B))));
9256 end if;
9258 Ensure_Freeze_Node (Id);
9259 return Freeze_Node (Id);
9260 end Package_Freeze_Node;
9262 -- Local variables
9264 Enc_G : constant Node_Id := Enclosing_Package_Body (Gen_Body);
9265 Enc_N : constant Node_Id := Enclosing_Package_Body (N);
9266 Par_Id : constant Entity_Id := Scope (Get_Generic_Entity (N));
9268 Enc_G_F : Node_Id;
9269 F_Node : Node_Id;
9271 -- Start of processing for Freeze_Subprogram_Instance
9273 begin
9274 -- If the instance and the generic body appear within the same unit, and
9275 -- the instance precedes the generic, the freeze node for the instance
9276 -- must appear after that of the generic. If the generic is nested
9277 -- within another instance I2, then current instance must be frozen
9278 -- after I2. In both cases, the freeze nodes are those of enclosing
9279 -- packages. Otherwise, the freeze node is placed at the end of the
9280 -- current declarative part.
9282 Ensure_Freeze_Node (Pack_Id);
9283 F_Node := Freeze_Node (Pack_Id);
9285 if Is_Generic_Instance (Par_Id)
9286 and then Present (Freeze_Node (Par_Id))
9287 and then In_Same_Declarative_Part (Parent (Freeze_Node (Par_Id)), N)
9288 then
9289 -- The parent was a premature instantiation. Insert freeze node at
9290 -- the end the current declarative part.
9292 if Is_Known_Guaranteed_ABE (Get_Unit_Instantiation_Node (Par_Id)) then
9293 Insert_Freeze_Node_For_Instance (N, F_Node);
9295 -- Handle the following case:
9297 -- package Parent_Inst is new ...
9298 -- freeze Parent_Inst []
9300 -- procedure P ... -- this body freezes Parent_Inst
9302 -- procedure Inst is new ...
9304 -- In this particular scenario, the freeze node for Inst must be
9305 -- inserted in the same manner as that of Parent_Inst - before the
9306 -- next source body or at the end of the declarative list (body not
9307 -- available). If body P did not exist and Parent_Inst was frozen
9308 -- after Inst, either by a body following Inst or at the end of the
9309 -- declarative region, the freeze node for Inst must be inserted
9310 -- after that of Parent_Inst. This relation is established by
9311 -- comparing the Slocs of Parent_Inst freeze node and Inst.
9313 elsif In_Same_List (Freeze_Node (Par_Id), N)
9314 and then Sloc (Freeze_Node (Par_Id)) <= Sloc (N)
9315 then
9316 Insert_Freeze_Node_For_Instance (N, F_Node);
9318 else
9319 Insert_After (Freeze_Node (Par_Id), F_Node);
9320 end if;
9322 -- The body enclosing the instance should be frozen after the body that
9323 -- includes the generic, because the body of the instance may make
9324 -- references to entities therein. If the two are not in the same
9325 -- declarative part, or if the one enclosing the instance is frozen
9326 -- already, freeze the instance at the end of the current declarative
9327 -- part.
9329 elsif Is_Generic_Instance (Par_Id)
9330 and then Present (Freeze_Node (Par_Id))
9331 and then Present (Enc_N)
9332 then
9333 if In_Same_Declarative_Part (Parent (Freeze_Node (Par_Id)), Enc_N)
9334 then
9335 -- The enclosing package may contain several instances. Rather
9336 -- than computing the earliest point at which to insert its freeze
9337 -- node, we place it at the end of the declarative part of the
9338 -- parent of the generic.
9340 Insert_Freeze_Node_For_Instance
9341 (Freeze_Node (Par_Id), Package_Freeze_Node (Enc_N));
9342 end if;
9344 Insert_Freeze_Node_For_Instance (N, F_Node);
9346 elsif Present (Enc_G)
9347 and then Present (Enc_N)
9348 and then Enc_G /= Enc_N
9349 and then Earlier (N, Gen_Body)
9350 then
9351 -- Freeze package that encloses instance, and place node after the
9352 -- package that encloses generic. If enclosing package is already
9353 -- frozen we have to assume it is at the proper place. This may be a
9354 -- potential ABE that requires dynamic checking. Do not add a freeze
9355 -- node if the package that encloses the generic is inside the body
9356 -- that encloses the instance, because the freeze node would be in
9357 -- the wrong scope. Additional contortions needed if the bodies are
9358 -- within a subunit.
9360 declare
9361 Enclosing_Body : Node_Id;
9363 begin
9364 if Nkind (Enc_N) = N_Package_Body_Stub then
9365 Enclosing_Body := Proper_Body (Unit (Library_Unit (Enc_N)));
9366 else
9367 Enclosing_Body := Enc_N;
9368 end if;
9370 if Parent (List_Containing (Enc_G)) /= Enclosing_Body then
9371 Insert_Freeze_Node_For_Instance
9372 (Enc_G, Package_Freeze_Node (Enc_N));
9373 end if;
9374 end;
9376 -- Freeze enclosing subunit before instance
9378 Enc_G_F := Package_Freeze_Node (Enc_G);
9380 if not Is_List_Member (Enc_G_F) then
9381 Insert_After (Enc_G, Enc_G_F);
9382 end if;
9384 Insert_Freeze_Node_For_Instance (N, F_Node);
9386 else
9387 -- If none of the above, insert freeze node at the end of the current
9388 -- declarative part.
9390 Insert_Freeze_Node_For_Instance (N, F_Node);
9391 end if;
9392 end Freeze_Subprogram_Instance;
9394 ----------------
9395 -- Get_Gen_Id --
9396 ----------------
9398 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id is
9399 begin
9400 return Generic_Renamings.Table (E).Gen_Id;
9401 end Get_Gen_Id;
9403 ---------------------
9404 -- Get_Instance_Of --
9405 ---------------------
9407 function Get_Instance_Of (A : Entity_Id) return Entity_Id is
9408 Res : constant Assoc_Ptr := Generic_Renamings_HTable.Get (A);
9410 begin
9411 if Res /= Assoc_Null then
9412 return Generic_Renamings.Table (Res).Act_Id;
9414 else
9415 -- On exit, entity is not instantiated: not a generic parameter, or
9416 -- else parameter of an inner generic unit.
9418 return A;
9419 end if;
9420 end Get_Instance_Of;
9422 ---------------------------------
9423 -- Get_Unit_Instantiation_Node --
9424 ---------------------------------
9426 function Get_Unit_Instantiation_Node (A : Entity_Id) return Node_Id is
9427 Decl : Node_Id := Unit_Declaration_Node (A);
9428 Inst : Node_Id;
9430 begin
9431 -- If the Package_Instantiation attribute has been set on the package
9432 -- entity, then use it directly when it (or its Original_Node) refers
9433 -- to an N_Package_Instantiation node. In principle it should be
9434 -- possible to have this field set in all cases, which should be
9435 -- investigated, and would allow this function to be significantly
9436 -- simplified. ???
9438 Inst := Package_Instantiation (A);
9440 if Present (Inst) then
9441 if Nkind (Inst) = N_Package_Instantiation then
9442 return Inst;
9444 elsif Nkind (Original_Node (Inst)) = N_Package_Instantiation then
9445 return Original_Node (Inst);
9446 end if;
9447 end if;
9449 -- If the instantiation is a compilation unit that does not need body
9450 -- then the instantiation node has been rewritten as a package
9451 -- declaration for the instance, and we return the original node.
9453 -- If it is a compilation unit and the instance node has not been
9454 -- rewritten, then it is still the unit of the compilation. Finally, if
9455 -- a body is present, this is a parent of the main unit whose body has
9456 -- been compiled for inlining purposes, and the instantiation node has
9457 -- been rewritten with the instance body.
9459 -- Otherwise the instantiation node appears after the declaration. If
9460 -- the entity is a formal package, the declaration may have been
9461 -- rewritten as a generic declaration (in the case of a formal with box)
9462 -- or left as a formal package declaration if it has actuals, and is
9463 -- found with a forward search.
9465 if Nkind (Parent (Decl)) = N_Compilation_Unit then
9466 if Nkind (Decl) = N_Package_Declaration
9467 and then Present (Corresponding_Body (Decl))
9468 then
9469 Decl := Unit_Declaration_Node (Corresponding_Body (Decl));
9470 end if;
9472 if Nkind (Original_Node (Decl)) in N_Generic_Instantiation then
9473 return Original_Node (Decl);
9474 else
9475 return Unit (Parent (Decl));
9476 end if;
9478 elsif Nkind (Decl) = N_Package_Declaration
9479 and then Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration
9480 then
9481 return Original_Node (Decl);
9483 else
9484 Inst := Next (Decl);
9485 while Nkind (Inst) not in N_Formal_Package_Declaration
9486 | N_Function_Instantiation
9487 | N_Package_Instantiation
9488 | N_Procedure_Instantiation
9489 loop
9490 Next (Inst);
9491 end loop;
9493 return Inst;
9494 end if;
9495 end Get_Unit_Instantiation_Node;
9497 ------------------------
9498 -- Has_Been_Exchanged --
9499 ------------------------
9501 function Has_Been_Exchanged (E : Entity_Id) return Boolean is
9502 Next : Elmt_Id;
9504 begin
9505 Next := First_Elmt (Exchanged_Views);
9506 while Present (Next) loop
9507 if Full_View (Node (Next)) = E then
9508 return True;
9509 end if;
9511 Next_Elmt (Next);
9512 end loop;
9514 return False;
9515 end Has_Been_Exchanged;
9517 -------------------
9518 -- Has_Contracts --
9519 -------------------
9521 function Has_Contracts (Decl : Node_Id) return Boolean is
9522 A_List : constant List_Id := Aspect_Specifications (Decl);
9523 A_Spec : Node_Id;
9524 A_Id : Aspect_Id;
9525 begin
9526 if No (A_List) then
9527 return False;
9528 else
9529 A_Spec := First (A_List);
9530 while Present (A_Spec) loop
9531 A_Id := Get_Aspect_Id (A_Spec);
9532 if A_Id = Aspect_Pre or else A_Id = Aspect_Post then
9533 return True;
9534 end if;
9536 Next (A_Spec);
9537 end loop;
9539 return False;
9540 end if;
9541 end Has_Contracts;
9543 ----------
9544 -- Hash --
9545 ----------
9547 function Hash (F : Entity_Id) return HTable_Range is
9548 begin
9549 return HTable_Range (F mod HTable_Size);
9550 end Hash;
9552 ------------------------
9553 -- Hide_Current_Scope --
9554 ------------------------
9556 procedure Hide_Current_Scope is
9557 C : constant Entity_Id := Current_Scope;
9558 E : Entity_Id;
9560 begin
9561 Set_Is_Hidden_Open_Scope (C);
9563 E := First_Entity (C);
9564 while Present (E) loop
9565 if Is_Immediately_Visible (E) then
9566 Set_Is_Immediately_Visible (E, False);
9567 Append_Elmt (E, Hidden_Entities);
9568 end if;
9570 Next_Entity (E);
9571 end loop;
9573 -- Make the scope name invisible as well. This is necessary, but might
9574 -- conflict with calls to Rtsfind later on, in case the scope is a
9575 -- predefined one. There is no clean solution to this problem, so for
9576 -- now we depend on the user not redefining Standard itself in one of
9577 -- the parent units.
9579 if Is_Immediately_Visible (C) and then C /= Standard_Standard then
9580 Set_Is_Immediately_Visible (C, False);
9581 Append_Elmt (C, Hidden_Entities);
9582 end if;
9584 end Hide_Current_Scope;
9586 --------------
9587 -- Init_Env --
9588 --------------
9590 procedure Init_Env is
9591 Saved : Instance_Env;
9593 begin
9594 Saved.Instantiated_Parent := Current_Instantiated_Parent;
9595 Saved.Exchanged_Views := Exchanged_Views;
9596 Saved.Hidden_Entities := Hidden_Entities;
9597 Saved.Current_Sem_Unit := Current_Sem_Unit;
9598 Saved.Parent_Unit_Visible := Parent_Unit_Visible;
9599 Saved.Instance_Parent_Unit := Instance_Parent_Unit;
9601 -- Save configuration switches. These may be reset if the unit is a
9602 -- predefined unit, and the current mode is not Ada 2005.
9604 Saved.Switches := Save_Config_Switches;
9606 Instance_Envs.Append (Saved);
9608 Exchanged_Views := New_Elmt_List;
9609 Hidden_Entities := New_Elmt_List;
9611 -- Make dummy entry for Instantiated parent. If generic unit is legal,
9612 -- this is set properly in Set_Instance_Env.
9614 Current_Instantiated_Parent :=
9615 (Current_Scope, Current_Scope, Assoc_Null);
9616 end Init_Env;
9618 ---------------------
9619 -- In_Main_Context --
9620 ---------------------
9622 function In_Main_Context (E : Entity_Id) return Boolean is
9623 Context : List_Id;
9624 Clause : Node_Id;
9625 Nam : Node_Id;
9627 begin
9628 if not Is_Compilation_Unit (E)
9629 or else Ekind (E) /= E_Package
9630 or else In_Private_Part (E)
9631 then
9632 return False;
9633 end if;
9635 Context := Context_Items (Cunit (Main_Unit));
9637 Clause := First (Context);
9638 while Present (Clause) loop
9639 if Nkind (Clause) = N_With_Clause then
9640 Nam := Name (Clause);
9642 -- If the current scope is part of the context of the main unit,
9643 -- analysis of the corresponding with_clause is not complete, and
9644 -- the entity is not set. We use the Chars field directly, which
9645 -- might produce false positives in rare cases, but guarantees
9646 -- that we produce all the instance bodies we will need.
9648 if (Is_Entity_Name (Nam) and then Chars (Nam) = Chars (E))
9649 or else (Nkind (Nam) = N_Selected_Component
9650 and then Chars (Selector_Name (Nam)) = Chars (E))
9651 then
9652 return True;
9653 end if;
9654 end if;
9656 Next (Clause);
9657 end loop;
9659 return False;
9660 end In_Main_Context;
9662 ---------------------
9663 -- Inherit_Context --
9664 ---------------------
9666 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id) is
9667 Current_Context : List_Id;
9668 Current_Unit : Node_Id;
9669 Item : Node_Id;
9670 New_I : Node_Id;
9672 Clause : Node_Id;
9673 OK : Boolean;
9674 Lib_Unit : Node_Id;
9676 begin
9677 if Nkind (Parent (Gen_Decl)) = N_Compilation_Unit then
9679 -- The inherited context is attached to the enclosing compilation
9680 -- unit. This is either the main unit, or the declaration for the
9681 -- main unit (in case the instantiation appears within the package
9682 -- declaration and the main unit is its body).
9684 Current_Unit := Parent (Inst);
9685 while Present (Current_Unit)
9686 and then Nkind (Current_Unit) /= N_Compilation_Unit
9687 loop
9688 Current_Unit := Parent (Current_Unit);
9689 end loop;
9691 Current_Context := Context_Items (Current_Unit);
9693 Item := First (Context_Items (Parent (Gen_Decl)));
9694 while Present (Item) loop
9695 if Nkind (Item) = N_With_Clause then
9696 Lib_Unit := Library_Unit (Item);
9698 -- Take care to prevent direct cyclic with's
9700 if Lib_Unit /= Current_Unit then
9702 -- Do not add a unit if it is already in the context
9704 Clause := First (Current_Context);
9705 OK := True;
9706 while Present (Clause) loop
9707 if Nkind (Clause) = N_With_Clause
9708 and then Library_Unit (Clause) = Lib_Unit
9709 then
9710 OK := False;
9711 exit;
9712 end if;
9714 Next (Clause);
9715 end loop;
9717 if OK then
9718 New_I := New_Copy (Item);
9719 Set_Implicit_With (New_I);
9721 Append (New_I, Current_Context);
9722 end if;
9723 end if;
9724 end if;
9726 Next (Item);
9727 end loop;
9728 end if;
9729 end Inherit_Context;
9731 ----------------
9732 -- Initialize --
9733 ----------------
9735 procedure Initialize is
9736 begin
9737 Generic_Renamings.Init;
9738 Instance_Envs.Init;
9739 Generic_Flags.Init;
9740 Generic_Renamings_HTable.Reset;
9741 Circularity_Detected := False;
9742 Exchanged_Views := No_Elist;
9743 Hidden_Entities := No_Elist;
9744 end Initialize;
9746 -------------------------------------
9747 -- Insert_Freeze_Node_For_Instance --
9748 -------------------------------------
9750 procedure Insert_Freeze_Node_For_Instance
9751 (N : Node_Id;
9752 F_Node : Node_Id)
9754 function Enclosing_Body (N : Node_Id) return Node_Id;
9755 -- Find enclosing package or subprogram body, if any. Freeze node may
9756 -- be placed at end of current declarative list if previous instance
9757 -- and current one have different enclosing bodies.
9759 function Previous_Instance (Gen : Entity_Id) return Entity_Id;
9760 -- Find the local instance, if any, that declares the generic that is
9761 -- being instantiated. If present, the freeze node for this instance
9762 -- must follow the freeze node for the previous instance.
9764 --------------------
9765 -- Enclosing_Body --
9766 --------------------
9768 function Enclosing_Body (N : Node_Id) return Node_Id is
9769 P : Node_Id;
9771 begin
9772 P := Parent (N);
9773 while Present (P)
9774 and then Nkind (Parent (P)) /= N_Compilation_Unit
9775 loop
9776 if Nkind (P) in N_Package_Body | N_Subprogram_Body then
9777 if Nkind (Parent (P)) = N_Subunit then
9778 return Corresponding_Stub (Parent (P));
9779 else
9780 return P;
9781 end if;
9782 end if;
9784 P := True_Parent (P);
9785 end loop;
9787 return Empty;
9788 end Enclosing_Body;
9790 -----------------------
9791 -- Previous_Instance --
9792 -----------------------
9794 function Previous_Instance (Gen : Entity_Id) return Entity_Id is
9795 S : Entity_Id;
9797 begin
9798 S := Scope (Gen);
9799 while Present (S) and then S /= Standard_Standard loop
9800 if Is_Generic_Instance (S)
9801 and then In_Same_Source_Unit (S, N)
9802 then
9803 return S;
9804 end if;
9806 S := Scope (S);
9807 end loop;
9809 return Empty;
9810 end Previous_Instance;
9812 -- Local variables
9814 Decl : Node_Id;
9815 Decls : List_Id;
9816 Inst : Entity_Id;
9817 Origin : Entity_Id;
9818 Par_Inst : Node_Id;
9819 Par_N : Node_Id;
9821 -- Start of processing for Insert_Freeze_Node_For_Instance
9823 begin
9824 -- Nothing to do if the freeze node has already been inserted
9826 if Is_List_Member (F_Node) then
9827 return;
9828 end if;
9830 Inst := Entity (F_Node);
9832 -- When processing a subprogram instantiation, utilize the actual
9833 -- subprogram instantiation rather than its package wrapper as it
9834 -- carries all the context information.
9836 if Is_Wrapper_Package (Inst) then
9837 Inst := Related_Instance (Inst);
9838 end if;
9840 Par_Inst := Parent (Inst);
9842 -- If this is a package instance, check whether the generic is declared
9843 -- in a previous instance and the current instance is not within the
9844 -- previous one.
9846 if Present (Generic_Parent (Par_Inst)) and then Is_In_Main_Unit (N) then
9847 declare
9848 Enclosing_N : constant Node_Id := Enclosing_Body (N);
9849 Par_I : constant Entity_Id :=
9850 Previous_Instance (Generic_Parent (Par_Inst));
9851 Scop : Entity_Id;
9853 begin
9854 if Present (Par_I) and then Earlier (N, Freeze_Node (Par_I)) then
9855 Scop := Scope (Inst);
9857 -- If the current instance is within the one that contains
9858 -- the generic, the freeze node for the current one must
9859 -- appear in the current declarative part. Ditto, if the
9860 -- current instance is within another package instance or
9861 -- within a body that does not enclose the current instance.
9862 -- In these three cases the freeze node of the previous
9863 -- instance is not relevant.
9865 while Present (Scop) and then Scop /= Standard_Standard loop
9866 exit when Scop = Par_I
9867 or else
9868 (Is_Generic_Instance (Scop)
9869 and then Scope_Depth (Scop) > Scope_Depth (Par_I));
9870 Scop := Scope (Scop);
9871 end loop;
9873 -- Previous instance encloses current instance
9875 if Scop = Par_I then
9876 null;
9878 -- If the next node is a source body we must freeze in the
9879 -- current scope as well.
9881 elsif Present (Next (N))
9882 and then Nkind (Next (N)) in N_Subprogram_Body
9883 | N_Package_Body
9884 and then Comes_From_Source (Next (N))
9885 then
9886 null;
9888 -- Current instance is within an unrelated instance
9890 elsif Is_Generic_Instance (Scop) then
9891 null;
9893 -- Current instance is within an unrelated body
9895 elsif Present (Enclosing_N)
9896 and then Enclosing_N /= Enclosing_Body (Par_I)
9897 then
9898 null;
9900 else
9901 Insert_After (Freeze_Node (Par_I), F_Node);
9902 return;
9903 end if;
9904 end if;
9905 end;
9906 end if;
9908 Decl := N;
9909 Decls := List_Containing (N);
9910 Par_N := Parent (Decls);
9911 Origin := Empty;
9913 -- Determine the proper freeze point of an instantiation
9915 if Is_Generic_Instance (Inst) then
9916 loop
9917 -- When the instantiation occurs in a package spec, append the
9918 -- freeze node to the private declarations (if any).
9920 if Nkind (Par_N) = N_Package_Specification
9921 and then Decls = Visible_Declarations (Par_N)
9922 and then not Is_Empty_List (Private_Declarations (Par_N))
9923 then
9924 Decls := Private_Declarations (Par_N);
9925 Decl := First (Decls);
9926 end if;
9928 -- We adhere to the general rule of a package or subprogram body
9929 -- causing freezing of anything before it in the same declarative
9930 -- region. In this respect, the proper freeze point of a package
9931 -- instantiation is before the first source body which follows, or
9932 -- before a stub. This ensures that entities from the instance are
9933 -- already frozen and therefore usable in source bodies.
9935 if Nkind (Par_N) /= N_Package_Declaration
9936 and then
9937 not In_Same_Source_Unit (Generic_Parent (Par_Inst), Inst)
9938 then
9939 while Present (Decl) loop
9940 if ((Nkind (Decl) in N_Unit_Body
9941 or else
9942 Nkind (Decl) in N_Body_Stub)
9943 and then Comes_From_Source (Decl))
9944 or else (Present (Origin)
9945 and then Nkind (Decl) in N_Generic_Instantiation
9946 and then Instance_Spec (Decl) /= Origin)
9947 then
9948 Set_Sloc (F_Node, Sloc (Decl));
9949 Insert_Before (Decl, F_Node);
9950 return;
9951 end if;
9953 Next (Decl);
9954 end loop;
9955 end if;
9957 -- When the instantiation occurs in a package spec and there is
9958 -- no source body which follows, and the package has a body but
9959 -- is delayed, then insert immediately before its freeze node.
9961 if Nkind (Par_N) = N_Package_Specification
9962 and then Present (Corresponding_Body (Parent (Par_N)))
9963 and then Present (Freeze_Node (Defining_Entity (Par_N)))
9964 then
9965 Set_Sloc (F_Node, Sloc (Freeze_Node (Defining_Entity (Par_N))));
9966 Insert_Before (Freeze_Node (Defining_Entity (Par_N)), F_Node);
9967 return;
9969 -- When the instantiation occurs in a package spec and there is
9970 -- no source body which follows, not even of the package itself,
9971 -- then insert into the declaration list of the outer level, but
9972 -- do not jump over following instantiations in this list because
9973 -- they may have a body that has not materialized yet, see above.
9975 elsif Nkind (Par_N) = N_Package_Specification
9976 and then No (Corresponding_Body (Parent (Par_N)))
9977 and then Is_List_Member (Parent (Par_N))
9978 then
9979 Decl := Parent (Par_N);
9980 Decls := List_Containing (Decl);
9981 Par_N := Parent (Decls);
9982 Origin := Decl;
9984 -- In a package declaration, or if no source body which follows
9985 -- and at library level, then insert at end of list.
9987 else
9988 exit;
9989 end if;
9990 end loop;
9991 end if;
9993 -- Insert and adjust the Sloc of the freeze node
9995 Set_Sloc (F_Node, Sloc (Last (Decls)));
9996 Insert_After (Last (Decls), F_Node);
9997 end Insert_Freeze_Node_For_Instance;
9999 -----------------------------
10000 -- Install_Formal_Packages --
10001 -----------------------------
10003 procedure Install_Formal_Packages (Par : Entity_Id) is
10004 E : Entity_Id;
10005 Gen : Entity_Id;
10006 Gen_E : Entity_Id := Empty;
10008 begin
10009 E := First_Entity (Par);
10011 -- If we are installing an instance parent, locate the formal packages
10012 -- of its generic parent.
10014 if Is_Generic_Instance (Par) then
10015 Gen := Generic_Parent (Package_Specification (Par));
10016 Gen_E := First_Entity (Gen);
10017 end if;
10019 while Present (E) loop
10020 if Ekind (E) = E_Package
10021 and then Nkind (Parent (E)) = N_Package_Renaming_Declaration
10022 then
10023 -- If this is the renaming for the parent instance, done
10025 if Renamed_Entity (E) = Par then
10026 exit;
10028 -- The visibility of a formal of an enclosing generic is already
10029 -- correct.
10031 elsif Denotes_Formal_Package (E) then
10032 null;
10034 elsif Present (Associated_Formal_Package (E)) then
10035 Check_Generic_Actuals (Renamed_Entity (E), True);
10036 Set_Is_Hidden (E, False);
10038 -- Find formal package in generic unit that corresponds to
10039 -- (instance of) formal package in instance.
10041 while Present (Gen_E) and then Chars (Gen_E) /= Chars (E) loop
10042 Next_Entity (Gen_E);
10043 end loop;
10045 if Present (Gen_E) then
10046 Map_Formal_Package_Entities (Gen_E, E);
10047 end if;
10048 end if;
10049 end if;
10051 Next_Entity (E);
10053 if Present (Gen_E) then
10054 Next_Entity (Gen_E);
10055 end if;
10056 end loop;
10057 end Install_Formal_Packages;
10059 --------------------
10060 -- Install_Parent --
10061 --------------------
10063 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False) is
10064 Ancestors : constant Elist_Id := New_Elmt_List;
10065 S : constant Entity_Id := Current_Scope;
10066 Inst_Par : Entity_Id;
10067 First_Par : Entity_Id;
10068 Inst_Node : Node_Id;
10069 Gen_Par : Entity_Id;
10070 First_Gen : Entity_Id;
10071 Elmt : Elmt_Id;
10073 procedure Install_Noninstance_Specs (Par : Entity_Id);
10074 -- Install the scopes of noninstance parent units ending with Par
10076 procedure Install_Spec (Par : Entity_Id);
10077 -- The child unit is within the declarative part of the parent, so the
10078 -- declarations within the parent are immediately visible.
10080 -------------------------------
10081 -- Install_Noninstance_Specs --
10082 -------------------------------
10084 procedure Install_Noninstance_Specs (Par : Entity_Id) is
10085 begin
10086 if Present (Par)
10087 and then Par /= Standard_Standard
10088 and then not In_Open_Scopes (Par)
10089 then
10090 Install_Noninstance_Specs (Scope (Par));
10091 Install_Spec (Par);
10092 end if;
10093 end Install_Noninstance_Specs;
10095 ------------------
10096 -- Install_Spec --
10097 ------------------
10099 procedure Install_Spec (Par : Entity_Id) is
10100 Spec : constant Node_Id := Package_Specification (Par);
10102 begin
10103 -- If this parent of the child instance is a top-level unit,
10104 -- then record the unit and its visibility for later resetting in
10105 -- Remove_Parent. We exclude units that are generic instances, as we
10106 -- only want to record this information for the ultimate top-level
10107 -- noninstance parent (is that always correct???).
10109 if Scope (Par) = Standard_Standard
10110 and then not Is_Generic_Instance (Par)
10111 then
10112 Parent_Unit_Visible := Is_Immediately_Visible (Par);
10113 Instance_Parent_Unit := Par;
10114 end if;
10116 -- Open the parent scope and make it and its declarations visible.
10117 -- If this point is not within a body, then only the visible
10118 -- declarations should be made visible, and installation of the
10119 -- private declarations is deferred until the appropriate point
10120 -- within analysis of the spec being instantiated (see the handling
10121 -- of parent visibility in Analyze_Package_Specification). This is
10122 -- relaxed in the case where the parent unit is Ada.Tags, to avoid
10123 -- private view problems that occur when compiling instantiations of
10124 -- a generic child of that package (Generic_Dispatching_Constructor).
10125 -- If the instance freezes a tagged type, inlinings of operations
10126 -- from Ada.Tags may need the full view of type Tag. If inlining took
10127 -- proper account of establishing visibility of inlined subprograms'
10128 -- parents then it should be possible to remove this
10129 -- special check. ???
10131 Push_Scope (Par);
10132 Set_Is_Immediately_Visible (Par);
10133 Install_Visible_Declarations (Par);
10134 Set_Use (Visible_Declarations (Spec));
10136 if In_Body or else Is_RTU (Par, Ada_Tags) then
10137 Install_Private_Declarations (Par);
10138 Set_Use (Private_Declarations (Spec));
10139 end if;
10140 end Install_Spec;
10142 -- Start of processing for Install_Parent
10144 begin
10145 -- We need to install the parent instance to compile the instantiation
10146 -- of the child, but the child instance must appear in the current
10147 -- scope. Given that we cannot place the parent above the current scope
10148 -- in the scope stack, we duplicate the current scope and unstack both
10149 -- after the instantiation is complete.
10151 -- If the parent is itself the instantiation of a child unit, we must
10152 -- also stack the instantiation of its parent, and so on. Each such
10153 -- ancestor is the prefix of the name in a prior instantiation.
10155 -- If this is a nested instance, the parent unit itself resolves to
10156 -- a renaming of the parent instance, whose declaration we need.
10158 -- Finally, the parent may be a generic (not an instance) when the
10159 -- child unit appears as a formal package.
10161 Inst_Par := P;
10163 if Present (Renamed_Entity (Inst_Par)) then
10164 Inst_Par := Renamed_Entity (Inst_Par);
10165 end if;
10167 First_Par := Inst_Par;
10169 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
10171 First_Gen := Gen_Par;
10173 while Present (Gen_Par) and then Is_Child_Unit (Gen_Par) loop
10175 -- Load grandparent instance as well
10177 Inst_Node := Get_Unit_Instantiation_Node (Inst_Par);
10179 if Nkind (Name (Inst_Node)) = N_Expanded_Name then
10180 Inst_Par := Entity (Prefix (Name (Inst_Node)));
10182 if Present (Renamed_Entity (Inst_Par)) then
10183 Inst_Par := Renamed_Entity (Inst_Par);
10184 end if;
10186 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
10188 if Present (Gen_Par) then
10189 Prepend_Elmt (Inst_Par, Ancestors);
10191 else
10192 -- Parent is not the name of an instantiation
10194 Install_Noninstance_Specs (Inst_Par);
10195 exit;
10196 end if;
10198 else
10199 -- Previous error
10201 exit;
10202 end if;
10203 end loop;
10205 if Present (First_Gen) then
10206 Append_Elmt (First_Par, Ancestors);
10207 else
10208 Install_Noninstance_Specs (First_Par);
10209 end if;
10211 if not Is_Empty_Elmt_List (Ancestors) then
10212 Elmt := First_Elmt (Ancestors);
10213 while Present (Elmt) loop
10214 Install_Spec (Node (Elmt));
10215 Install_Formal_Packages (Node (Elmt));
10216 Next_Elmt (Elmt);
10217 end loop;
10218 end if;
10220 if not In_Body then
10221 Push_Scope (S);
10222 end if;
10223 end Install_Parent;
10225 -------------------------------
10226 -- Install_Hidden_Primitives --
10227 -------------------------------
10229 procedure Install_Hidden_Primitives
10230 (Prims_List : in out Elist_Id;
10231 Gen_T : Entity_Id;
10232 Act_T : Entity_Id)
10234 Elmt : Elmt_Id;
10235 List : Elist_Id := No_Elist;
10236 Prim_G_Elmt : Elmt_Id;
10237 Prim_A_Elmt : Elmt_Id;
10238 Prim_G : Node_Id;
10239 Prim_A : Node_Id;
10241 begin
10242 -- No action needed in case of serious errors because we cannot trust
10243 -- in the order of primitives
10245 if Serious_Errors_Detected > 0 then
10246 return;
10248 -- No action possible if we don't have available the list of primitive
10249 -- operations
10251 elsif No (Gen_T)
10252 or else not Is_Record_Type (Gen_T)
10253 or else not Is_Tagged_Type (Gen_T)
10254 or else not Is_Record_Type (Act_T)
10255 or else not Is_Tagged_Type (Act_T)
10256 then
10257 return;
10259 -- There is no need to handle interface types since their primitives
10260 -- cannot be hidden
10262 elsif Is_Interface (Gen_T) then
10263 return;
10264 end if;
10266 Prim_G_Elmt := First_Elmt (Primitive_Operations (Gen_T));
10268 if not Is_Class_Wide_Type (Act_T) then
10269 Prim_A_Elmt := First_Elmt (Primitive_Operations (Act_T));
10270 else
10271 Prim_A_Elmt := First_Elmt (Primitive_Operations (Root_Type (Act_T)));
10272 end if;
10274 loop
10275 -- Skip predefined primitives in the generic formal
10277 while Present (Prim_G_Elmt)
10278 and then Is_Predefined_Dispatching_Operation (Node (Prim_G_Elmt))
10279 loop
10280 Next_Elmt (Prim_G_Elmt);
10281 end loop;
10283 -- Skip predefined primitives in the generic actual
10285 while Present (Prim_A_Elmt)
10286 and then Is_Predefined_Dispatching_Operation (Node (Prim_A_Elmt))
10287 loop
10288 Next_Elmt (Prim_A_Elmt);
10289 end loop;
10291 exit when No (Prim_G_Elmt) or else No (Prim_A_Elmt);
10293 Prim_G := Node (Prim_G_Elmt);
10294 Prim_A := Node (Prim_A_Elmt);
10296 -- There is no need to handle interface primitives because their
10297 -- primitives are not hidden
10299 exit when Present (Interface_Alias (Prim_G));
10301 -- Here we install one hidden primitive
10303 if Chars (Prim_G) /= Chars (Prim_A)
10304 and then Has_Suffix (Prim_A, 'P')
10305 and then Remove_Suffix (Prim_A, 'P') = Chars (Prim_G)
10306 then
10307 Set_Chars (Prim_A, Chars (Prim_G));
10308 Append_New_Elmt (Prim_A, To => List);
10309 end if;
10311 Next_Elmt (Prim_A_Elmt);
10312 Next_Elmt (Prim_G_Elmt);
10313 end loop;
10315 -- Append the elements to the list of temporarily visible primitives
10316 -- avoiding duplicates.
10318 if Present (List) then
10319 if No (Prims_List) then
10320 Prims_List := New_Elmt_List;
10321 end if;
10323 Elmt := First_Elmt (List);
10324 while Present (Elmt) loop
10325 Append_Unique_Elmt (Node (Elmt), Prims_List);
10326 Next_Elmt (Elmt);
10327 end loop;
10328 end if;
10329 end Install_Hidden_Primitives;
10331 -------------------------------
10332 -- Restore_Hidden_Primitives --
10333 -------------------------------
10335 procedure Restore_Hidden_Primitives (Prims_List : in out Elist_Id) is
10336 Prim_Elmt : Elmt_Id;
10337 Prim : Node_Id;
10339 begin
10340 if Present (Prims_List) then
10341 Prim_Elmt := First_Elmt (Prims_List);
10342 while Present (Prim_Elmt) loop
10343 Prim := Node (Prim_Elmt);
10344 Set_Chars (Prim, Add_Suffix (Prim, 'P'));
10345 Next_Elmt (Prim_Elmt);
10346 end loop;
10348 Prims_List := No_Elist;
10349 end if;
10350 end Restore_Hidden_Primitives;
10352 --------------------------------
10353 -- Instantiate_Formal_Package --
10354 --------------------------------
10356 function Instantiate_Formal_Package
10357 (Formal : Node_Id;
10358 Actual : Node_Id;
10359 Analyzed_Formal : Node_Id) return List_Id
10361 Loc : constant Source_Ptr := Sloc (Actual);
10362 Hidden_Formals : constant Elist_Id := New_Elmt_List;
10364 Actual_Pack : Entity_Id;
10365 Formal_Pack : Entity_Id;
10366 Gen_Parent : Entity_Id;
10367 Decls : List_Id;
10368 Nod : Node_Id;
10369 Parent_Spec : Node_Id;
10371 procedure Find_Matching_Actual
10372 (F : Node_Id;
10373 Act : in out Entity_Id);
10374 -- We need to associate each formal entity in the formal package with
10375 -- the corresponding entity in the actual package. The actual package
10376 -- has been analyzed and possibly expanded, and as a result there is
10377 -- no one-to-one correspondence between the two lists (for example,
10378 -- the actual may include subtypes, itypes, and inherited primitive
10379 -- operations, interspersed among the renaming declarations for the
10380 -- actuals). We retrieve the corresponding actual by name because each
10381 -- actual has the same name as the formal, and they do appear in the
10382 -- same order.
10384 function Get_Formal_Entity (N : Node_Id) return Entity_Id;
10385 -- Retrieve entity of defining entity of generic formal parameter.
10386 -- Only the declarations of formals need to be considered when
10387 -- linking them to actuals, but the declarative list may include
10388 -- internal entities generated during analysis, and those are ignored.
10390 procedure Match_Formal_Entity
10391 (Formal_Node : Node_Id;
10392 Formal_Ent : Entity_Id;
10393 Actual_Ent : Entity_Id);
10394 -- Associates the formal entity with the actual. In the case where
10395 -- Formal_Ent is a formal package, this procedure iterates through all
10396 -- of its formals and enters associations between the actuals occurring
10397 -- in the formal package's corresponding actual package (given by
10398 -- Actual_Ent) and the formal package's formal parameters. This
10399 -- procedure recurses if any of the parameters is itself a package.
10401 function Is_Instance_Of
10402 (Act_Spec : Entity_Id;
10403 Gen_Anc : Entity_Id) return Boolean;
10404 -- The actual can be an instantiation of a generic within another
10405 -- instance, in which case there is no direct link from it to the
10406 -- original generic ancestor. In that case, we recognize that the
10407 -- ultimate ancestor is the same by examining names and scopes.
10409 procedure Process_Nested_Formal (Formal : Entity_Id);
10410 -- If the current formal is declared with a box, its own formals are
10411 -- visible in the instance, as they were in the generic, and their
10412 -- Hidden flag must be reset. If some of these formals are themselves
10413 -- packages declared with a box, the processing must be recursive.
10415 --------------------------
10416 -- Find_Matching_Actual --
10417 --------------------------
10419 procedure Find_Matching_Actual
10420 (F : Node_Id;
10421 Act : in out Entity_Id)
10423 Formal_Ent : Entity_Id;
10425 begin
10426 case Nkind (Original_Node (F)) is
10427 when N_Formal_Object_Declaration
10428 | N_Formal_Type_Declaration
10430 Formal_Ent := Defining_Identifier (F);
10432 while Present (Act)
10433 and then Chars (Act) /= Chars (Formal_Ent)
10434 loop
10435 Next_Entity (Act);
10436 end loop;
10438 when N_Formal_Package_Declaration
10439 | N_Formal_Subprogram_Declaration
10440 | N_Generic_Package_Declaration
10441 | N_Package_Declaration
10443 Formal_Ent := Defining_Entity (F);
10445 while Present (Act)
10446 and then Chars (Act) /= Chars (Formal_Ent)
10447 loop
10448 Next_Entity (Act);
10449 end loop;
10451 when others =>
10452 raise Program_Error;
10453 end case;
10454 end Find_Matching_Actual;
10456 -------------------------
10457 -- Match_Formal_Entity --
10458 -------------------------
10460 procedure Match_Formal_Entity
10461 (Formal_Node : Node_Id;
10462 Formal_Ent : Entity_Id;
10463 Actual_Ent : Entity_Id)
10465 Act_Pkg : Entity_Id;
10467 begin
10468 Set_Instance_Of (Formal_Ent, Actual_Ent);
10470 if Ekind (Actual_Ent) = E_Package then
10472 -- Record associations for each parameter
10474 Act_Pkg := Actual_Ent;
10476 declare
10477 A_Ent : Entity_Id := First_Entity (Act_Pkg);
10478 F_Ent : Entity_Id;
10479 F_Node : Node_Id;
10481 Gen_Decl : Node_Id;
10482 Formals : List_Id;
10483 Actual : Entity_Id;
10485 begin
10486 -- Retrieve the actual given in the formal package declaration
10488 Actual := Entity (Name (Original_Node (Formal_Node)));
10490 -- The actual in the formal package declaration may be a
10491 -- renamed generic package, in which case we want to retrieve
10492 -- the original generic in order to traverse its formal part.
10494 if Present (Renamed_Entity (Actual)) then
10495 Gen_Decl := Unit_Declaration_Node (Renamed_Entity (Actual));
10496 else
10497 Gen_Decl := Unit_Declaration_Node (Actual);
10498 end if;
10500 Formals := Generic_Formal_Declarations (Gen_Decl);
10502 if Present (Formals) then
10503 F_Node := First_Non_Pragma (Formals);
10504 else
10505 F_Node := Empty;
10506 end if;
10508 while Present (A_Ent)
10509 and then Present (F_Node)
10510 and then A_Ent /= First_Private_Entity (Act_Pkg)
10511 loop
10512 F_Ent := Get_Formal_Entity (F_Node);
10514 if Present (F_Ent) then
10516 -- This is a formal of the original package. Record
10517 -- association and recurse.
10519 Find_Matching_Actual (F_Node, A_Ent);
10520 Match_Formal_Entity (F_Node, F_Ent, A_Ent);
10521 Next_Entity (A_Ent);
10522 end if;
10524 Next_Non_Pragma (F_Node);
10525 end loop;
10526 end;
10527 end if;
10528 end Match_Formal_Entity;
10530 -----------------------
10531 -- Get_Formal_Entity --
10532 -----------------------
10534 function Get_Formal_Entity (N : Node_Id) return Entity_Id is
10535 Kind : constant Node_Kind := Nkind (Original_Node (N));
10536 begin
10537 case Kind is
10538 when N_Formal_Object_Declaration =>
10539 return Defining_Identifier (N);
10541 when N_Formal_Type_Declaration =>
10542 return Defining_Identifier (N);
10544 when N_Formal_Subprogram_Declaration =>
10545 return Defining_Unit_Name (Specification (N));
10547 when N_Formal_Package_Declaration =>
10548 return Defining_Identifier (Original_Node (N));
10550 when N_Generic_Package_Declaration =>
10551 return Defining_Identifier (Original_Node (N));
10553 -- All other declarations are introduced by semantic analysis and
10554 -- have no match in the actual.
10556 when others =>
10557 return Empty;
10558 end case;
10559 end Get_Formal_Entity;
10561 --------------------
10562 -- Is_Instance_Of --
10563 --------------------
10565 function Is_Instance_Of
10566 (Act_Spec : Entity_Id;
10567 Gen_Anc : Entity_Id) return Boolean
10569 Gen_Par : constant Entity_Id := Generic_Parent (Act_Spec);
10571 begin
10572 if No (Gen_Par) then
10573 return False;
10575 -- Simplest case: the generic parent of the actual is the formal
10577 elsif Gen_Par = Gen_Anc then
10578 return True;
10580 elsif Chars (Gen_Par) /= Chars (Gen_Anc) then
10581 return False;
10583 -- The actual may be obtained through several instantiations. Its
10584 -- scope must itself be an instance of a generic declared in the
10585 -- same scope as the formal. Any other case is detected above.
10587 elsif not Is_Generic_Instance (Scope (Gen_Par)) then
10588 return False;
10590 else
10591 return Generic_Parent (Parent (Scope (Gen_Par))) = Scope (Gen_Anc);
10592 end if;
10593 end Is_Instance_Of;
10595 ---------------------------
10596 -- Process_Nested_Formal --
10597 ---------------------------
10599 procedure Process_Nested_Formal (Formal : Entity_Id) is
10600 Ent : Entity_Id;
10602 begin
10603 if Present (Associated_Formal_Package (Formal))
10604 and then Box_Present (Parent (Associated_Formal_Package (Formal)))
10605 then
10606 Ent := First_Entity (Formal);
10607 while Present (Ent) loop
10608 Set_Is_Hidden (Ent, False);
10609 Set_Is_Visible_Formal (Ent);
10610 Set_Is_Potentially_Use_Visible
10611 (Ent, Is_Potentially_Use_Visible (Formal));
10613 if Ekind (Ent) = E_Package then
10614 exit when Renamed_Entity (Ent) = Renamed_Entity (Formal);
10615 Process_Nested_Formal (Ent);
10616 end if;
10618 Next_Entity (Ent);
10619 end loop;
10620 end if;
10621 end Process_Nested_Formal;
10623 -- Start of processing for Instantiate_Formal_Package
10625 begin
10626 Analyze (Actual);
10628 -- The actual must be a package instance, or else a current instance
10629 -- such as a parent generic within the body of a generic child.
10631 if not Is_Entity_Name (Actual)
10632 or else not Is_Package_Or_Generic_Package (Entity (Actual))
10633 then
10634 Error_Msg_N
10635 ("expect package instance to instantiate formal", Actual);
10636 Abandon_Instantiation (Actual);
10638 else
10639 Actual_Pack := Entity (Actual);
10640 Set_Is_Instantiated (Actual_Pack);
10642 -- The actual may be a renamed package, or an outer generic formal
10643 -- package whose instantiation is converted into a renaming.
10645 if Present (Renamed_Entity (Actual_Pack)) then
10646 Actual_Pack := Renamed_Entity (Actual_Pack);
10647 end if;
10649 -- The analyzed formal is expected to be the result of the rewriting
10650 -- of the formal package into a regular package by analysis.
10652 pragma Assert (Nkind (Analyzed_Formal) = N_Package_Declaration
10653 and then Nkind (Original_Node (Analyzed_Formal)) =
10654 N_Formal_Package_Declaration);
10656 Gen_Parent := Generic_Parent (Specification (Analyzed_Formal));
10657 Formal_Pack := Defining_Unit_Name (Specification (Analyzed_Formal));
10659 -- The actual for a ghost generic formal package should be a ghost
10660 -- package (SPARK RM 6.9(14)).
10662 Check_Ghost_Formal_Procedure_Or_Package
10663 (N => Actual,
10664 Actual => Actual_Pack,
10665 Formal => Formal_Pack);
10667 if Nkind (Parent (Actual_Pack)) = N_Defining_Program_Unit_Name then
10668 Parent_Spec := Package_Specification (Actual_Pack);
10669 else
10670 Parent_Spec := Parent (Actual_Pack);
10671 end if;
10673 if Gen_Parent = Any_Id then
10674 Error_Msg_N
10675 ("previous error in declaration of formal package", Actual);
10676 Abandon_Instantiation (Actual);
10678 elsif Is_Instance_Of (Parent_Spec, Get_Instance_Of (Gen_Parent)) then
10679 null;
10681 -- If this is the current instance of an enclosing generic, that unit
10682 -- is the generic package we need.
10684 elsif In_Open_Scopes (Actual_Pack)
10685 and then Ekind (Actual_Pack) = E_Generic_Package
10686 then
10687 null;
10689 else
10690 Error_Msg_NE
10691 ("actual parameter must be instance of&", Actual, Gen_Parent);
10692 Abandon_Instantiation (Actual);
10693 end if;
10695 Set_Instance_Of (Defining_Identifier (Formal), Actual_Pack);
10696 Map_Formal_Package_Entities (Formal_Pack, Actual_Pack);
10698 Nod :=
10699 Make_Package_Renaming_Declaration (Loc,
10700 Defining_Unit_Name => New_Copy (Defining_Identifier (Formal)),
10701 Name => New_Occurrence_Of (Actual_Pack, Loc));
10703 Set_Associated_Formal_Package
10704 (Defining_Unit_Name (Nod), Defining_Identifier (Formal));
10705 Decls := New_List (Nod);
10707 -- If the formal F has a box, then the generic declarations are
10708 -- visible in the generic G. In an instance of G, the corresponding
10709 -- entities in the actual for F (which are the actuals for the
10710 -- instantiation of the generic that F denotes) must also be made
10711 -- visible for analysis of the current instance. On exit from the
10712 -- current instance, those entities are made private again. If the
10713 -- actual is currently in use, these entities are also use-visible.
10715 -- The loop through the actual entities also steps through the formal
10716 -- entities and enters associations from formals to actuals into the
10717 -- renaming map. This is necessary to properly handle checking of
10718 -- actual parameter associations for later formals that depend on
10719 -- actuals declared in the formal package.
10721 -- In Ada 2005, partial parameterization requires that we make
10722 -- visible the actuals corresponding to formals that were defaulted
10723 -- in the formal package. There formals are identified because they
10724 -- remain formal generics within the formal package, rather than
10725 -- being renamings of the actuals supplied.
10727 declare
10728 Gen_Decl : constant Node_Id :=
10729 Unit_Declaration_Node (Gen_Parent);
10730 Formals : constant List_Id :=
10731 Generic_Formal_Declarations (Gen_Decl);
10733 Actual_Ent : Entity_Id;
10734 Actual_Of_Formal : Node_Id;
10735 Formal_Node : Node_Id;
10736 Formal_Ent : Entity_Id;
10738 begin
10739 if Present (Formals) then
10740 Formal_Node := First_Non_Pragma (Formals);
10741 else
10742 Formal_Node := Empty;
10743 end if;
10745 Actual_Ent := First_Entity (Actual_Pack);
10746 Actual_Of_Formal :=
10747 First (Visible_Declarations (Specification (Analyzed_Formal)));
10748 while Present (Actual_Ent)
10749 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
10750 loop
10751 if Present (Formal_Node) then
10752 Formal_Ent := Get_Formal_Entity (Formal_Node);
10754 if Present (Formal_Ent) then
10755 Find_Matching_Actual (Formal_Node, Actual_Ent);
10756 Match_Formal_Entity (Formal_Node, Formal_Ent, Actual_Ent);
10758 -- We iterate at the same time over the actuals of the
10759 -- local package created for the formal, to determine
10760 -- which one of the formals of the original generic were
10761 -- defaulted in the formal. The corresponding actual
10762 -- entities are visible in the enclosing instance.
10764 if Box_Present (Formal)
10765 or else
10766 (Present (Actual_Of_Formal)
10767 and then
10768 Is_Generic_Formal
10769 (Get_Formal_Entity (Actual_Of_Formal)))
10770 then
10771 Set_Is_Hidden (Actual_Ent, False);
10772 Set_Is_Visible_Formal (Actual_Ent);
10773 Set_Is_Potentially_Use_Visible
10774 (Actual_Ent, In_Use (Actual_Pack));
10776 if Ekind (Actual_Ent) = E_Package then
10777 Process_Nested_Formal (Actual_Ent);
10778 end if;
10780 else
10781 if not Is_Hidden (Actual_Ent) then
10782 Append_Elmt (Actual_Ent, Hidden_Formals);
10783 end if;
10785 Set_Is_Hidden (Actual_Ent);
10786 Set_Is_Potentially_Use_Visible (Actual_Ent, False);
10787 end if;
10788 end if;
10790 Next_Non_Pragma (Formal_Node);
10791 Next (Actual_Of_Formal);
10793 -- A formal subprogram may be overloaded, so advance in
10794 -- the list of actuals to make sure we do not match two
10795 -- successive formals to the same actual. This is only
10796 -- relevant for overloadable entities, others have
10797 -- distinct names.
10799 if Is_Overloadable (Actual_Ent) then
10800 Next_Entity (Actual_Ent);
10801 end if;
10803 else
10804 -- No further formals to match, but the generic part may
10805 -- contain inherited operation that are not hidden in the
10806 -- enclosing instance.
10808 Next_Entity (Actual_Ent);
10809 end if;
10810 end loop;
10812 -- Inherited subprograms generated by formal derived types are
10813 -- also visible if the types are.
10815 Actual_Ent := First_Entity (Actual_Pack);
10816 while Present (Actual_Ent)
10817 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
10818 loop
10819 if Is_Overloadable (Actual_Ent)
10820 and then
10821 Nkind (Parent (Actual_Ent)) = N_Subtype_Declaration
10822 and then
10823 not Is_Hidden (Defining_Identifier (Parent (Actual_Ent)))
10824 then
10825 Set_Is_Hidden (Actual_Ent, False);
10826 Set_Is_Potentially_Use_Visible
10827 (Actual_Ent, In_Use (Actual_Pack));
10828 end if;
10830 Next_Entity (Actual_Ent);
10831 end loop;
10832 end;
10834 -- If the formal requires conformance checking, reanalyze it as an
10835 -- abbreviated instantiation, to verify the matching rules of 12.7.
10836 -- The actual checks are performed after the generic associations
10837 -- have been analyzed, to guarantee the same visibility for this
10838 -- instantiation and for the actuals.
10840 -- In Ada 2005, the generic associations for the formal can include
10841 -- defaulted parameters. These are ignored during check. This
10842 -- internal instantiation is removed from the tree after conformance
10843 -- checking, because it contains formal declarations for those
10844 -- defaulted parameters, and those should not reach the back-end.
10846 if Requires_Conformance_Checking (Formal) then
10847 declare
10848 I_Pack : constant Entity_Id := Make_Temporary (Loc, 'P');
10850 I_Nam : Node_Id;
10852 begin
10853 Set_Is_Internal (I_Pack);
10854 Mutate_Ekind (I_Pack, E_Package);
10856 -- Insert the package into the list of its hidden entities so
10857 -- that the list is not empty for Is_Abbreviated_Instance.
10859 Append_Elmt (I_Pack, Hidden_Formals);
10861 Set_Hidden_In_Formal_Instance (I_Pack, Hidden_Formals);
10863 -- If the generic is a child unit, Check_Generic_Child_Unit
10864 -- needs its original name in case it is qualified.
10866 if Is_Child_Unit (Gen_Parent) then
10867 I_Nam :=
10868 New_Copy_Tree (Name (Original_Node (Analyzed_Formal)));
10869 pragma Assert (Entity (I_Nam) = Gen_Parent);
10871 else
10872 I_Nam :=
10873 New_Occurrence_Of (Get_Instance_Of (Gen_Parent), Loc);
10874 end if;
10876 Append_To (Decls,
10877 Make_Package_Instantiation (Loc,
10878 Defining_Unit_Name => I_Pack,
10879 Name => I_Nam,
10880 Generic_Associations => Generic_Associations (Formal)));
10881 end;
10882 end if;
10884 return Decls;
10885 end if;
10886 end Instantiate_Formal_Package;
10888 -----------------------------------
10889 -- Instantiate_Formal_Subprogram --
10890 -----------------------------------
10892 function Instantiate_Formal_Subprogram
10893 (Formal : Node_Id;
10894 Actual : Node_Id;
10895 Analyzed_Formal : Node_Id) return Node_Id
10897 Analyzed_S : constant Entity_Id :=
10898 Defining_Unit_Name (Specification (Analyzed_Formal));
10899 Formal_Sub : constant Entity_Id :=
10900 Defining_Unit_Name (Specification (Formal));
10902 function From_Parent_Scope (Subp : Entity_Id) return Boolean;
10903 -- If the generic is a child unit, the parent has been installed on the
10904 -- scope stack, but a default subprogram cannot resolve to something
10905 -- on the parent because that parent is not really part of the visible
10906 -- context (it is there to resolve explicit local entities). If the
10907 -- default has resolved in this way, we remove the entity from immediate
10908 -- visibility and analyze the node again to emit an error message or
10909 -- find another visible candidate.
10911 procedure Valid_Actual_Subprogram (Act : Node_Id);
10912 -- Perform legality check and raise exception on failure
10914 -----------------------
10915 -- From_Parent_Scope --
10916 -----------------------
10918 function From_Parent_Scope (Subp : Entity_Id) return Boolean is
10919 Gen_Scope : Node_Id;
10921 begin
10922 Gen_Scope := Scope (Analyzed_S);
10923 while Present (Gen_Scope) and then Is_Child_Unit (Gen_Scope) loop
10924 if Scope (Subp) = Scope (Gen_Scope) then
10925 return True;
10926 end if;
10928 Gen_Scope := Scope (Gen_Scope);
10929 end loop;
10931 return False;
10932 end From_Parent_Scope;
10934 -----------------------------
10935 -- Valid_Actual_Subprogram --
10936 -----------------------------
10938 procedure Valid_Actual_Subprogram (Act : Node_Id) is
10939 Act_E : Entity_Id;
10941 begin
10942 if Is_Entity_Name (Act) then
10943 Act_E := Entity (Act);
10945 elsif Nkind (Act) = N_Selected_Component
10946 and then Is_Entity_Name (Selector_Name (Act))
10947 then
10948 Act_E := Entity (Selector_Name (Act));
10950 else
10951 Act_E := Empty;
10952 end if;
10954 -- The actual for a ghost generic formal procedure should be a ghost
10955 -- procedure (SPARK RM 6.9(14)).
10957 if Present (Act_E)
10958 and then Ekind (Act_E) = E_Procedure
10959 then
10960 Check_Ghost_Formal_Procedure_Or_Package
10961 (N => Act,
10962 Actual => Act_E,
10963 Formal => Analyzed_S);
10964 end if;
10966 if (Present (Act_E) and then Is_Overloadable (Act_E))
10967 or else Nkind (Act) in N_Attribute_Reference
10968 | N_Indexed_Component
10969 | N_Character_Literal
10970 | N_Explicit_Dereference
10971 then
10972 return;
10973 end if;
10975 Error_Msg_NE
10976 ("expect subprogram or entry name in instantiation of &",
10977 Instantiation_Node, Formal_Sub);
10978 Abandon_Instantiation (Instantiation_Node);
10979 end Valid_Actual_Subprogram;
10981 -- Local variables
10983 Decl_Node : Node_Id;
10984 Loc : Source_Ptr;
10985 Nam : Node_Id;
10986 New_Spec : Node_Id;
10987 New_Subp : Entity_Id;
10989 -- Start of processing for Instantiate_Formal_Subprogram
10991 begin
10992 New_Spec := New_Copy_Tree (Specification (Formal));
10994 -- The tree copy has created the proper instantiation sloc for the
10995 -- new specification. Use this location for all other constructed
10996 -- declarations.
10998 Loc := Sloc (Defining_Unit_Name (New_Spec));
11000 -- Create new entity for the actual (New_Copy_Tree does not), and
11001 -- indicate that it is an actual.
11003 -- If the actual is not an entity (i.e. an attribute reference)
11004 -- and the formal includes aspect specifications for contracts,
11005 -- we create an internal name for the renaming declaration. The
11006 -- constructed wrapper contains a call to the entity in the renaming.
11007 -- This is an expansion activity, as is the wrapper creation.
11009 if Ada_Version >= Ada_2022
11010 and then Has_Contracts (Analyzed_Formal)
11011 and then not Is_Entity_Name (Actual)
11012 and then Expander_Active
11013 then
11014 New_Subp := Make_Temporary (Sloc (Actual), 'S');
11015 else
11016 New_Subp := Make_Defining_Identifier (Loc, Chars (Formal_Sub));
11017 end if;
11019 Mutate_Ekind (New_Subp, Ekind (Analyzed_S));
11020 Set_Is_Generic_Actual_Subprogram (New_Subp);
11021 Set_Defining_Unit_Name (New_Spec, New_Subp);
11023 -- Create new entities for the each of the formals in the specification
11024 -- of the renaming declaration built for the actual.
11026 if Present (Parameter_Specifications (New_Spec)) then
11027 declare
11028 F : Node_Id;
11029 F_Id : Entity_Id;
11031 begin
11032 F := First (Parameter_Specifications (New_Spec));
11033 while Present (F) loop
11034 F_Id := Defining_Identifier (F);
11036 Set_Defining_Identifier (F,
11037 Make_Defining_Identifier (Sloc (F_Id), Chars (F_Id)));
11038 Next (F);
11039 end loop;
11040 end;
11041 end if;
11043 -- Find entity of actual. If the actual is an attribute reference, it
11044 -- cannot be resolved here (its formal is missing) but is handled
11045 -- instead in Attribute_Renaming. If the actual is overloaded, it is
11046 -- fully resolved subsequently, when the renaming declaration for the
11047 -- formal is analyzed. If it is an explicit dereference, resolve the
11048 -- prefix but not the actual itself, to prevent interpretation as call.
11050 if Present (Actual) then
11051 Loc := Sloc (Actual);
11052 Set_Sloc (New_Spec, Loc);
11054 if Nkind (Actual) = N_Operator_Symbol then
11055 Find_Direct_Name (Actual);
11057 elsif Nkind (Actual) = N_Explicit_Dereference then
11058 Analyze (Prefix (Actual));
11060 elsif Nkind (Actual) /= N_Attribute_Reference then
11061 Analyze (Actual);
11062 end if;
11064 Valid_Actual_Subprogram (Actual);
11065 Nam := Actual;
11067 elsif Present (Default_Name (Formal)) then
11068 if Nkind (Default_Name (Formal)) not in N_Attribute_Reference
11069 | N_Selected_Component
11070 | N_Indexed_Component
11071 | N_Character_Literal
11072 and then Present (Entity (Default_Name (Formal)))
11073 then
11074 Nam := New_Occurrence_Of (Entity (Default_Name (Formal)), Loc);
11075 else
11076 Nam := New_Copy (Default_Name (Formal));
11077 Set_Sloc (Nam, Loc);
11078 end if;
11080 elsif Box_Present (Formal) then
11082 -- Actual is resolved at the point of instantiation. Create an
11083 -- identifier or operator with the same name as the formal.
11085 if Nkind (Formal_Sub) = N_Defining_Operator_Symbol then
11086 Nam :=
11087 Make_Operator_Symbol (Loc,
11088 Chars => Chars (Formal_Sub),
11089 Strval => No_String);
11090 else
11091 Nam := Make_Identifier (Loc, Chars (Formal_Sub));
11092 end if;
11094 elsif Nkind (Specification (Formal)) = N_Procedure_Specification
11095 and then Null_Present (Specification (Formal))
11096 then
11097 -- Generate null body for procedure, for use in the instance
11099 Decl_Node :=
11100 Make_Subprogram_Body (Loc,
11101 Specification => New_Spec,
11102 Declarations => New_List,
11103 Handled_Statement_Sequence =>
11104 Make_Handled_Sequence_Of_Statements (Loc,
11105 Statements => New_List (Make_Null_Statement (Loc))));
11107 -- RM 12.6 (16.2/2): The procedure has convention Intrinsic
11109 Set_Convention (Defining_Unit_Name (New_Spec), Convention_Intrinsic);
11111 Copy_Ghost_Aspect (Formal, To => Decl_Node);
11113 -- Eliminate the calls to it when optimization is enabled
11115 Set_Is_Inlined (Defining_Unit_Name (New_Spec));
11116 return Decl_Node;
11118 -- Handle case of a formal function with an expression default (allowed
11119 -- when extensions are enabled).
11121 elsif Nkind (Specification (Formal)) = N_Function_Specification
11122 and then Present (Expression (Formal))
11123 then
11124 -- Generate body for function, for use in the instance
11126 declare
11127 Expr : constant Node_Id := New_Copy (Expression (Formal));
11128 Stmt : constant Node_Id := Make_Simple_Return_Statement (Loc);
11129 begin
11130 Set_Sloc (Expr, Loc);
11131 Set_Expression (Stmt, Expr);
11133 Decl_Node :=
11134 Make_Subprogram_Body (Loc,
11135 Specification => New_Spec,
11136 Declarations => New_List,
11137 Handled_Statement_Sequence =>
11138 Make_Handled_Sequence_Of_Statements (Loc,
11139 Statements => New_List (Stmt)));
11140 end;
11142 -- RM 12.6 (16.2/2): Like a null procedure default, the function
11143 -- has convention Intrinsic.
11145 Set_Convention (Defining_Unit_Name (New_Spec), Convention_Intrinsic);
11147 -- Inline calls to it when optimization is enabled
11149 Set_Is_Inlined (Defining_Unit_Name (New_Spec));
11150 return Decl_Node;
11152 else
11153 Error_Msg_Sloc := Sloc (Scope (Analyzed_S));
11154 Error_Msg_NE
11155 ("missing actual&", Instantiation_Node, Formal_Sub);
11156 Error_Msg_NE
11157 ("\in instantiation of & declared#",
11158 Instantiation_Node, Scope (Analyzed_S));
11159 Abandon_Instantiation (Instantiation_Node);
11160 end if;
11162 Decl_Node :=
11163 Make_Subprogram_Renaming_Declaration (Loc,
11164 Specification => New_Spec,
11165 Name => Nam);
11167 -- If we do not have an actual and the formal specified <> then set to
11168 -- get proper default.
11170 if No (Actual) and then Box_Present (Formal) then
11171 Set_From_Default (Decl_Node);
11172 end if;
11174 -- Gather possible interpretations for the actual before analyzing the
11175 -- instance. If overloaded, it will be resolved when analyzing the
11176 -- renaming declaration.
11178 if Box_Present (Formal) and then No (Actual) then
11179 Analyze (Nam);
11181 if Is_Child_Unit (Scope (Analyzed_S))
11182 and then Present (Entity (Nam))
11183 then
11184 if not Is_Overloaded (Nam) then
11185 if From_Parent_Scope (Entity (Nam)) then
11186 Set_Is_Immediately_Visible (Entity (Nam), False);
11187 Set_Entity (Nam, Empty);
11188 Set_Etype (Nam, Empty);
11190 Analyze (Nam);
11191 Set_Is_Immediately_Visible (Entity (Nam));
11192 end if;
11194 else
11195 declare
11196 I : Interp_Index;
11197 It : Interp;
11199 begin
11200 Get_First_Interp (Nam, I, It);
11201 while Present (It.Nam) loop
11202 if From_Parent_Scope (It.Nam) then
11203 Remove_Interp (I);
11204 end if;
11206 Get_Next_Interp (I, It);
11207 end loop;
11208 end;
11209 end if;
11210 end if;
11211 end if;
11213 -- The generic instantiation freezes the actual. This can only be done
11214 -- once the actual is resolved, in the analysis of the renaming
11215 -- declaration. To make the formal subprogram entity available, we set
11216 -- Corresponding_Formal_Spec to point to the formal subprogram entity.
11217 -- This is also needed in Analyze_Subprogram_Renaming for the processing
11218 -- of formal abstract subprograms.
11220 Set_Corresponding_Formal_Spec (Decl_Node, Analyzed_S);
11222 -- We cannot analyze the renaming declaration, and thus find the actual,
11223 -- until all the actuals are assembled in the instance. For subsequent
11224 -- checks of other actuals, indicate the node that will hold the
11225 -- instance of this formal.
11227 Set_Instance_Of (Analyzed_S, Nam);
11229 if Nkind (Actual) = N_Selected_Component
11230 and then Is_Task_Type (Etype (Prefix (Actual)))
11231 and then not Is_Frozen (Etype (Prefix (Actual)))
11232 then
11233 -- The renaming declaration will create a body, which must appear
11234 -- outside of the instantiation, We move the renaming declaration
11235 -- out of the instance, and create an additional renaming inside,
11236 -- to prevent freezing anomalies.
11238 declare
11239 Anon_Id : constant Entity_Id := Make_Temporary (Loc, 'E');
11241 begin
11242 Set_Defining_Unit_Name (New_Spec, Anon_Id);
11243 Insert_Before (Instantiation_Node, Decl_Node);
11244 Analyze (Decl_Node);
11246 -- Now create renaming within the instance
11248 Decl_Node :=
11249 Make_Subprogram_Renaming_Declaration (Loc,
11250 Specification => New_Copy_Tree (New_Spec),
11251 Name => New_Occurrence_Of (Anon_Id, Loc));
11253 Set_Defining_Unit_Name (Specification (Decl_Node),
11254 Make_Defining_Identifier (Loc, Chars (Formal_Sub)));
11255 end;
11256 end if;
11258 return Decl_Node;
11259 end Instantiate_Formal_Subprogram;
11261 ------------------------
11262 -- Instantiate_Object --
11263 ------------------------
11265 function Instantiate_Object
11266 (Formal : Node_Id;
11267 Actual : Node_Id;
11268 Analyzed_Formal : Node_Id) return List_Id
11270 Gen_Obj : constant Entity_Id := Defining_Identifier (Formal);
11271 A_Gen_Obj : constant Entity_Id :=
11272 Defining_Identifier (Analyzed_Formal);
11273 Acc_Def : Node_Id := Empty;
11274 Act_Assoc : constant Node_Id :=
11275 (if No (Actual) then Empty else Parent (Actual));
11276 Actual_Decl : Node_Id := Empty;
11277 Decl_Node : Node_Id;
11278 Def : Node_Id;
11279 Ftyp : Entity_Id;
11280 List : constant List_Id := New_List;
11281 Loc : constant Source_Ptr := Sloc (Actual);
11282 Orig_Ftyp : constant Entity_Id := Etype (A_Gen_Obj);
11283 Subt_Decl : Node_Id := Empty;
11284 Subt_Mark : Node_Id := Empty;
11286 -- Start of processing for Instantiate_Object
11288 begin
11289 -- Formal may be an anonymous access
11291 if Present (Subtype_Mark (Formal)) then
11292 Subt_Mark := Subtype_Mark (Formal);
11293 else
11294 Check_Access_Definition (Formal);
11295 Acc_Def := Access_Definition (Formal);
11296 end if;
11298 -- Sloc for error message on missing actual
11300 Error_Msg_Sloc := Sloc (Scope (A_Gen_Obj));
11302 if Get_Instance_Of (Gen_Obj) /= Gen_Obj then
11303 Error_Msg_N ("duplicate instantiation of generic parameter", Actual);
11304 end if;
11306 Set_Parent (List, Act_Assoc);
11308 -- OUT present
11310 if Out_Present (Formal) then
11312 -- An IN OUT generic actual must be a name. The instantiation is a
11313 -- renaming declaration. The actual is the name being renamed. We
11314 -- use the actual directly, rather than a copy, because it is not
11315 -- used further in the list of actuals, and because a copy or a use
11316 -- of relocate_node is incorrect if the instance is nested within a
11317 -- generic. In order to simplify e.g. ASIS queries, the
11318 -- Generic_Parent field links the declaration to the generic
11319 -- association.
11321 if No (Actual) then
11322 Error_Msg_NE
11323 ("missing actual &",
11324 Instantiation_Node, Gen_Obj);
11325 Error_Msg_NE
11326 ("\in instantiation of & declared#",
11327 Instantiation_Node, Scope (A_Gen_Obj));
11328 Abandon_Instantiation (Instantiation_Node);
11329 end if;
11331 if Present (Subt_Mark) then
11332 Decl_Node :=
11333 Make_Object_Renaming_Declaration (Loc,
11334 Defining_Identifier => New_Copy (Gen_Obj),
11335 Subtype_Mark => New_Copy_Tree (Subt_Mark),
11336 Name => Actual);
11338 else pragma Assert (Present (Acc_Def));
11339 Decl_Node :=
11340 Make_Object_Renaming_Declaration (Loc,
11341 Defining_Identifier => New_Copy (Gen_Obj),
11342 Access_Definition => New_Copy_Tree (Acc_Def),
11343 Name => Actual);
11344 end if;
11346 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
11348 -- The analysis of the actual may produce Insert_Action nodes, so
11349 -- the declaration must have a context in which to attach them.
11351 Append (Decl_Node, List);
11352 Analyze (Actual);
11354 -- Return if the analysis of the actual reported some error
11356 if Etype (Actual) = Any_Type then
11357 return List;
11358 end if;
11360 -- This check is performed here because Analyze_Object_Renaming will
11361 -- not check it when Comes_From_Source is False. Note though that the
11362 -- check for the actual being the name of an object will be performed
11363 -- in Analyze_Object_Renaming.
11365 if Is_Object_Reference (Actual)
11366 and then Is_Dependent_Component_Of_Mutable_Object (Actual)
11367 then
11368 Error_Msg_N
11369 ("illegal discriminant-dependent component for in out parameter",
11370 Actual);
11371 end if;
11373 -- The actual has to be resolved in order to check that it is a
11374 -- variable (due to cases such as F (1), where F returns access to
11375 -- an array, and for overloaded prefixes).
11377 Ftyp := Get_Instance_Of (Etype (A_Gen_Obj));
11379 -- If the type of the formal is not itself a formal, and the current
11380 -- unit is a child unit, the formal type must be declared in a
11381 -- parent, and must be retrieved by visibility.
11383 if Ftyp = Orig_Ftyp
11384 and then Is_Generic_Unit (Scope (Ftyp))
11385 and then Is_Child_Unit (Scope (A_Gen_Obj))
11386 then
11387 declare
11388 Temp : constant Node_Id :=
11389 New_Copy_Tree (Subtype_Mark (Analyzed_Formal));
11390 begin
11391 Set_Entity (Temp, Empty);
11392 Find_Type (Temp);
11393 Ftyp := Entity (Temp);
11394 end;
11395 end if;
11397 if Is_Private_Type (Ftyp)
11398 and then not Is_Private_Type (Etype (Actual))
11399 and then (Base_Type (Full_View (Ftyp)) = Base_Type (Etype (Actual))
11400 or else Base_Type (Etype (Actual)) = Ftyp)
11401 then
11402 -- If the actual has the type of the full view of the formal, or
11403 -- else a non-private subtype of the formal, then the visibility
11404 -- of the formal type has changed. Add to the actuals a subtype
11405 -- declaration that will force the exchange of views in the body
11406 -- of the instance as well.
11408 Subt_Decl :=
11409 Make_Subtype_Declaration (Loc,
11410 Defining_Identifier => Make_Temporary (Loc, 'P'),
11411 Subtype_Indication => New_Occurrence_Of (Ftyp, Loc));
11413 Prepend (Subt_Decl, List);
11415 Prepend_Elmt (Full_View (Ftyp), Exchanged_Views);
11416 Exchange_Declarations (Ftyp);
11417 end if;
11419 Resolve (Actual, Ftyp);
11421 if not Denotes_Variable (Actual) then
11422 Error_Msg_NE ("actual for& must be a variable", Actual, Gen_Obj);
11424 elsif Base_Type (Ftyp) /= Base_Type (Etype (Actual)) then
11426 -- Ada 2005 (AI-423): For a generic formal object of mode in out,
11427 -- the type of the actual shall resolve to a specific anonymous
11428 -- access type.
11430 if Ada_Version < Ada_2005
11431 or else not Is_Anonymous_Access_Type (Base_Type (Ftyp))
11432 or else not Is_Anonymous_Access_Type (Base_Type (Etype (Actual)))
11433 then
11434 Error_Msg_NE
11435 ("type of actual does not match type of&", Actual, Gen_Obj);
11436 end if;
11437 end if;
11439 Note_Possible_Modification (Actual, Sure => True);
11441 -- Check for instantiation with atomic/volatile/VFA object actual for
11442 -- nonatomic/nonvolatile/nonVFA formal (RM C.6 (12)).
11444 if Is_Atomic_Object (Actual) and then not Is_Atomic (Orig_Ftyp) then
11445 Error_Msg_NE
11446 ("cannot instantiate nonatomic formal & of mode in out",
11447 Actual, Gen_Obj);
11448 Error_Msg_N ("\with atomic object actual (RM C.6(12))", Actual);
11450 elsif Is_Volatile_Object_Ref (Actual)
11451 and then not Is_Volatile (Orig_Ftyp)
11452 then
11453 Error_Msg_NE
11454 ("cannot instantiate nonvolatile formal & of mode in out",
11455 Actual, Gen_Obj);
11456 Error_Msg_N ("\with volatile object actual (RM C.6(12))", Actual);
11458 elsif Is_Volatile_Full_Access_Object_Ref (Actual)
11459 and then not Is_Volatile_Full_Access (Orig_Ftyp)
11460 then
11461 Error_Msg_NE
11462 ("cannot instantiate nonfull access formal & of mode in out",
11463 Actual, Gen_Obj);
11464 Error_Msg_N
11465 ("\with full access object actual (RM C.6(12))", Actual);
11466 end if;
11468 -- Check for instantiation on nonatomic subcomponent of a full access
11469 -- object in Ada 2022 (RM C.6 (12)).
11471 if Ada_Version >= Ada_2022
11472 and then Is_Subcomponent_Of_Full_Access_Object (Actual)
11473 and then not Is_Atomic_Object (Actual)
11474 then
11475 Error_Msg_NE
11476 ("cannot instantiate formal & of mode in out with actual",
11477 Actual, Gen_Obj);
11478 Error_Msg_N
11479 ("\nonatomic subcomponent of full access object (RM C.6(12))",
11480 Actual);
11481 end if;
11483 -- Check actual/formal compatibility with respect to the four
11484 -- volatility refinement aspects.
11486 declare
11487 Actual_Obj : constant Entity_Id :=
11488 Get_Enclosing_Deep_Object (Actual);
11489 begin
11490 Check_Volatility_Compatibility
11491 (Actual_Obj, A_Gen_Obj, "actual object",
11492 "its corresponding formal object of mode in out",
11493 Srcpos_Bearer => Actual);
11494 end;
11496 -- The actual for a ghost generic formal IN OUT parameter should be a
11497 -- ghost object (SPARK RM 6.9(14)).
11499 Check_Ghost_Formal_Variable
11500 (Actual => Actual,
11501 Formal => A_Gen_Obj);
11503 -- Formal in-parameter
11505 else
11506 -- The instantiation of a generic formal in-parameter is constant
11507 -- declaration. The actual is the expression for that declaration.
11508 -- Its type is a full copy of the type of the formal. This may be
11509 -- an access to subprogram, for which we need to generate entities
11510 -- for the formals in the new signature.
11512 if Present (Actual) then
11513 if Present (Subt_Mark) then
11514 Def := New_Copy_Tree (Subt_Mark);
11515 else
11516 pragma Assert (Present (Acc_Def));
11517 Def := New_Copy_Tree (Acc_Def);
11518 end if;
11520 Decl_Node :=
11521 Make_Object_Declaration (Loc,
11522 Defining_Identifier => New_Copy (Gen_Obj),
11523 Constant_Present => True,
11524 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
11525 Object_Definition => Def,
11526 Expression => Actual);
11528 Copy_Ghost_Aspect (Formal, To => Decl_Node);
11529 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
11531 -- A generic formal object of a tagged type is defined to be
11532 -- aliased so the new constant must also be treated as aliased.
11534 if Is_Tagged_Type (Etype (A_Gen_Obj)) then
11535 Set_Aliased_Present (Decl_Node);
11536 end if;
11538 Append (Decl_Node, List);
11540 -- The actual for a ghost generic formal IN parameter of
11541 -- access-to-variable type should be a ghost object (SPARK
11542 -- RM 6.9(14)).
11544 if Is_Access_Variable (Etype (A_Gen_Obj)) then
11545 Check_Ghost_Formal_Variable
11546 (Actual => Actual,
11547 Formal => A_Gen_Obj);
11548 end if;
11550 -- No need to repeat (pre-)analysis of some expression nodes
11551 -- already handled in Preanalyze_Actuals.
11553 if Nkind (Actual) /= N_Allocator then
11554 Analyze (Actual);
11556 -- Return if the analysis of the actual reported some error
11558 if Etype (Actual) = Any_Type then
11559 return List;
11560 end if;
11561 end if;
11563 declare
11564 Formal_Type : constant Entity_Id := Etype (A_Gen_Obj);
11565 Typ : Entity_Id;
11567 begin
11568 Typ := Get_Instance_Of (Formal_Type);
11570 -- If the actual appears in the current or an enclosing scope,
11571 -- use its type directly. This is relevant if it has an actual
11572 -- subtype that is distinct from its nominal one. This cannot
11573 -- be done in general because the type of the actual may
11574 -- depend on other actuals, and only be fully determined when
11575 -- the enclosing instance is analyzed.
11577 if Present (Etype (Actual))
11578 and then Is_Constr_Subt_For_U_Nominal (Etype (Actual))
11579 then
11580 Freeze_Before (Instantiation_Node, Etype (Actual));
11581 else
11582 Freeze_Before (Instantiation_Node, Typ);
11583 end if;
11585 -- If the actual is an aggregate, perform name resolution on
11586 -- its components (the analysis of an aggregate does not do it)
11587 -- to capture local names that may be hidden if the generic is
11588 -- a child unit.
11590 if Nkind (Actual) = N_Aggregate then
11591 Preanalyze_And_Resolve (Actual, Typ);
11592 end if;
11594 if Is_Limited_Type (Typ)
11595 and then not OK_For_Limited_Init (Typ, Actual)
11596 then
11597 Error_Msg_N
11598 ("initialization not allowed for limited types", Actual);
11599 Explain_Limited_Type (Typ, Actual);
11600 end if;
11601 end;
11603 elsif Present (Default_Expression (Formal)) then
11605 -- Use default to construct declaration
11607 if Present (Subt_Mark) then
11608 Def := New_Copy_Tree (Subt_Mark);
11609 else
11610 pragma Assert (Present (Acc_Def));
11611 Def := New_Copy_Tree (Acc_Def);
11612 end if;
11614 Decl_Node :=
11615 Make_Object_Declaration (Sloc (Formal),
11616 Defining_Identifier => New_Copy (Gen_Obj),
11617 Constant_Present => True,
11618 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
11619 Object_Definition => Def,
11620 Expression => New_Copy_Tree
11621 (Default_Expression (Formal)));
11623 Copy_Ghost_Aspect (Formal, To => Decl_Node);
11624 Set_Corresponding_Generic_Association
11625 (Decl_Node, Expression (Decl_Node));
11627 Append (Decl_Node, List);
11628 Set_Analyzed (Expression (Decl_Node), False);
11630 else
11631 Error_Msg_NE ("missing actual&", Instantiation_Node, Gen_Obj);
11632 Error_Msg_NE ("\in instantiation of & declared#",
11633 Instantiation_Node, Scope (A_Gen_Obj));
11635 if Is_Scalar_Type (Etype (A_Gen_Obj)) then
11637 -- Create dummy constant declaration so that instance can be
11638 -- analyzed, to minimize cascaded visibility errors.
11640 if Present (Subt_Mark) then
11641 Def := Subt_Mark;
11642 else pragma Assert (Present (Acc_Def));
11643 Def := Acc_Def;
11644 end if;
11646 Decl_Node :=
11647 Make_Object_Declaration (Loc,
11648 Defining_Identifier => New_Copy (Gen_Obj),
11649 Constant_Present => True,
11650 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
11651 Object_Definition => New_Copy (Def),
11652 Expression =>
11653 Make_Attribute_Reference (Sloc (Gen_Obj),
11654 Attribute_Name => Name_First,
11655 Prefix => New_Copy (Def)));
11657 Append (Decl_Node, List);
11659 else
11660 Abandon_Instantiation (Instantiation_Node);
11661 end if;
11662 end if;
11663 end if;
11665 if Nkind (Actual) in N_Has_Entity
11666 and then Present (Entity (Actual))
11667 then
11668 Actual_Decl := Parent (Entity (Actual));
11669 end if;
11671 -- Ada 2005 (AI-423) refined by AI12-0287:
11672 -- For an object_renaming_declaration with a null_exclusion or an
11673 -- access_definition that has a null_exclusion, the subtype of the
11674 -- object_name shall exclude null. In addition, if the
11675 -- object_renaming_declaration occurs within the body of a generic unit
11676 -- G or within the body of a generic unit declared within the
11677 -- declarative region of generic unit G, then:
11678 -- * if the object_name statically denotes a generic formal object of
11679 -- mode in out of G, then the declaration of that object shall have a
11680 -- null_exclusion;
11681 -- * if the object_name statically denotes a call of a generic formal
11682 -- function of G, then the declaration of the result of that function
11683 -- shall have a null_exclusion.
11685 if Ada_Version >= Ada_2005
11686 and then Present (Actual_Decl)
11687 and then Nkind (Actual_Decl) in N_Formal_Object_Declaration
11688 | N_Object_Declaration
11689 and then Nkind (Analyzed_Formal) = N_Formal_Object_Declaration
11690 and then not Has_Null_Exclusion (Actual_Decl)
11691 and then Has_Null_Exclusion (Analyzed_Formal)
11692 and then Ekind (Defining_Identifier (Analyzed_Formal))
11693 = E_Generic_In_Out_Parameter
11694 and then ((In_Generic_Scope (Entity (Actual))
11695 and then In_Package_Body (Scope (Entity (Actual))))
11696 or else not Can_Never_Be_Null (Etype (Actual)))
11697 then
11698 Error_Msg_Sloc := Sloc (Analyzed_Formal);
11699 Error_Msg_N
11700 ("actual must exclude null to match generic formal#", Actual);
11701 end if;
11703 -- An effectively volatile object cannot be used as an actual in a
11704 -- generic instantiation (SPARK RM 7.1.3(7)). The following check is
11705 -- relevant only when SPARK_Mode is on as it is not a standard Ada
11706 -- legality rule, and also verifies that the actual is an object.
11708 if SPARK_Mode = On
11709 and then Present (Actual)
11710 and then Is_Object_Reference (Actual)
11711 and then Is_Effectively_Volatile_Object (Actual)
11712 and then not Is_Effectively_Volatile (A_Gen_Obj)
11713 then
11714 Error_Msg_N
11715 ("volatile object cannot act as actual in generic instantiation",
11716 Actual);
11717 end if;
11719 return List;
11720 end Instantiate_Object;
11722 ------------------------------
11723 -- Instantiate_Package_Body --
11724 ------------------------------
11726 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
11727 -- must be replaced by gotos which jump to the end of the routine in order
11728 -- to restore the Ghost and SPARK modes.
11730 procedure Instantiate_Package_Body
11731 (Body_Info : Pending_Body_Info;
11732 Inlined_Body : Boolean := False;
11733 Body_Optional : Boolean := False)
11735 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
11736 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Decl);
11737 Act_Spec : constant Node_Id := Specification (Act_Decl);
11738 Ctx_Parents : Elist_Id := No_Elist;
11739 Ctx_Top : Int := 0;
11740 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
11741 Gen_Id : constant Node_Id := Name (Inst_Node);
11742 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
11743 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
11744 Loc : constant Source_Ptr := Sloc (Inst_Node);
11746 procedure Check_Initialized_Types;
11747 -- In a generic package body, an entity of a generic private type may
11748 -- appear uninitialized. This is suspicious, unless the actual is a
11749 -- fully initialized type.
11751 procedure Install_Parents_Of_Generic_Context
11752 (Inst_Scope : Entity_Id;
11753 Ctx_Parents : out Elist_Id);
11754 -- Inst_Scope is the scope where the instance appears within; when it
11755 -- appears within a generic child package G, this routine collects and
11756 -- installs the enclosing packages of G in the scopes stack; installed
11757 -- packages are returned in Ctx_Parents.
11759 procedure Remove_Parents_Of_Generic_Context (Ctx_Parents : Elist_Id);
11760 -- Reverse effect after instantiation is complete
11762 -----------------------------
11763 -- Check_Initialized_Types --
11764 -----------------------------
11766 procedure Check_Initialized_Types is
11767 Decl : Node_Id;
11768 Formal : Entity_Id;
11769 Actual : Entity_Id;
11770 Uninit_Var : Entity_Id;
11772 begin
11773 Decl := First (Generic_Formal_Declarations (Gen_Decl));
11774 while Present (Decl) loop
11775 Uninit_Var := Empty;
11777 if Nkind (Decl) = N_Private_Extension_Declaration then
11778 Uninit_Var := Uninitialized_Variable (Decl);
11780 elsif Nkind (Decl) = N_Formal_Type_Declaration
11781 and then Nkind (Formal_Type_Definition (Decl)) =
11782 N_Formal_Private_Type_Definition
11783 then
11784 Uninit_Var :=
11785 Uninitialized_Variable (Formal_Type_Definition (Decl));
11786 end if;
11788 if Present (Uninit_Var) then
11789 Formal := Defining_Identifier (Decl);
11790 Actual := First_Entity (Act_Decl_Id);
11792 -- For each formal there is a subtype declaration that renames
11793 -- the actual and has the same name as the formal. Locate the
11794 -- formal for warning message about uninitialized variables
11795 -- in the generic, for which the actual type should be a fully
11796 -- initialized type.
11798 while Present (Actual) loop
11799 exit when Ekind (Actual) = E_Package
11800 and then Present (Renamed_Entity (Actual));
11802 if Chars (Actual) = Chars (Formal)
11803 and then not Is_Scalar_Type (Actual)
11804 and then not Is_Fully_Initialized_Type (Actual)
11805 and then Warn_On_No_Value_Assigned
11806 then
11807 Error_Msg_Node_2 := Formal;
11808 Error_Msg_NE
11809 ("generic unit has uninitialized variable& of "
11810 & "formal private type &?v?", Actual, Uninit_Var);
11811 Error_Msg_NE
11812 ("actual type for& should be fully initialized type?v?",
11813 Actual, Formal);
11814 exit;
11815 end if;
11817 Next_Entity (Actual);
11818 end loop;
11819 end if;
11821 Next (Decl);
11822 end loop;
11823 end Check_Initialized_Types;
11825 ----------------------------------------
11826 -- Install_Parents_Of_Generic_Context --
11827 ----------------------------------------
11829 procedure Install_Parents_Of_Generic_Context
11830 (Inst_Scope : Entity_Id;
11831 Ctx_Parents : out Elist_Id)
11833 Elmt : Elmt_Id;
11834 S : Entity_Id;
11836 begin
11837 Ctx_Parents := New_Elmt_List;
11839 -- Collect context parents (ie. parents where the instantiation
11840 -- appears within).
11842 S := Inst_Scope;
11843 while S /= Standard_Standard loop
11844 Prepend_Elmt (S, Ctx_Parents);
11845 S := Scope (S);
11846 end loop;
11848 -- Install enclosing parents
11850 Elmt := First_Elmt (Ctx_Parents);
11851 while Present (Elmt) loop
11852 Push_Scope (Node (Elmt));
11853 Set_Is_Immediately_Visible (Node (Elmt));
11854 Next_Elmt (Elmt);
11855 end loop;
11856 end Install_Parents_Of_Generic_Context;
11858 ---------------------------------------
11859 -- Remove_Parents_Of_Generic_Context --
11860 ---------------------------------------
11862 procedure Remove_Parents_Of_Generic_Context (Ctx_Parents : Elist_Id) is
11863 Elmt : Elmt_Id;
11865 begin
11866 -- Traverse Ctx_Parents in LIFO order to check the removed scopes
11868 Elmt := Last_Elmt (Ctx_Parents);
11869 while Present (Elmt) loop
11870 pragma Assert (Current_Scope = Node (Elmt));
11871 Set_Is_Immediately_Visible (Current_Scope, False);
11872 Pop_Scope;
11874 Remove_Last_Elmt (Ctx_Parents);
11875 Elmt := Last_Elmt (Ctx_Parents);
11876 end loop;
11877 end Remove_Parents_Of_Generic_Context;
11879 -- Local variables
11881 -- The following constants capture the context prior to instantiating
11882 -- the package body.
11884 Saved_CS : constant Config_Switches_Type := Save_Config_Switches;
11885 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
11886 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
11887 Saved_ISMP : constant Boolean :=
11888 Ignore_SPARK_Mode_Pragmas_In_Instance;
11889 Saved_LSST : constant Suppress_Stack_Entry_Ptr :=
11890 Local_Suppress_Stack_Top;
11891 Saved_SC : constant Boolean := Style_Check;
11892 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
11893 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
11894 Saved_SS : constant Suppress_Record := Scope_Suppress;
11895 Saved_Warn : constant Warnings_State := Save_Warnings;
11897 Act_Body : Node_Id;
11898 Act_Body_Id : Entity_Id;
11899 Act_Body_Name : Node_Id;
11900 Gen_Body : Node_Id;
11901 Gen_Body_Id : Node_Id;
11902 Par_Ent : Entity_Id := Empty;
11903 Par_Installed : Boolean := False;
11904 Par_Vis : Boolean := False;
11906 Scope_Check_Id : Entity_Id;
11907 Scope_Check_Last : Nat;
11908 -- Value of Current_Scope before calls to Install_Parents; used to check
11909 -- that scopes are correctly removed after instantiation.
11911 Vis_Prims_List : Elist_Id := No_Elist;
11912 -- List of primitives made temporarily visible in the instantiation
11913 -- to match the visibility of the formal type.
11915 -- Start of processing for Instantiate_Package_Body
11917 begin
11918 Gen_Body_Id := Corresponding_Body (Gen_Decl);
11920 -- The instance body may already have been processed, as the parent of
11921 -- another instance that is inlined (Load_Parent_Of_Generic).
11923 if Present (Corresponding_Body (Instance_Spec (Inst_Node))) then
11924 return;
11925 end if;
11927 -- The package being instantiated may be subject to pragma Ghost. Set
11928 -- the mode now to ensure that any nodes generated during instantiation
11929 -- are properly marked as Ghost.
11931 Set_Ghost_Mode (Act_Decl_Id);
11933 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
11935 -- Re-establish the state of information on which checks are suppressed.
11936 -- This information was set in Body_Info at the point of instantiation,
11937 -- and now we restore it so that the instance is compiled using the
11938 -- check status at the instantiation (RM 11.5(7.2/2), AI95-00224-01).
11940 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
11941 Scope_Suppress := Body_Info.Scope_Suppress;
11943 Restore_Config_Switches (Body_Info.Config_Switches);
11944 Restore_Warnings (Body_Info.Warnings);
11946 if No (Gen_Body_Id) then
11948 -- Do not look for parent of generic body if none is required.
11949 -- This may happen when the routine is called as part of the
11950 -- Pending_Instantiations processing, when nested instances
11951 -- may precede the one generated from the main unit.
11953 if not Unit_Requires_Body (Defining_Entity (Gen_Decl))
11954 and then Body_Optional
11955 then
11956 goto Leave;
11957 else
11958 Load_Parent_Of_Generic
11959 (Inst_Node, Specification (Gen_Decl), Body_Optional);
11961 -- Surprisingly enough, loading the body of the parent can cause
11962 -- the body to be instantiated and the double instantiation needs
11963 -- to be prevented in order to avoid giving bogus semantic errors.
11965 -- This case can occur because of the Collect_Previous_Instances
11966 -- machinery of Load_Parent_Of_Generic, which will instantiate
11967 -- bodies that are deemed to be ahead of the body of the parent
11968 -- in the compilation unit. But the relative position of these
11969 -- bodies is computed using the mere comparison of their Sloc.
11971 -- Now suppose that you have two generic packages G and H, with
11972 -- G containing a mere instantiation of H:
11974 -- generic
11975 -- package H is
11977 -- generic
11978 -- package Nested_G is
11979 -- ...
11980 -- end Nested_G;
11982 -- end H;
11984 -- with H;
11986 -- generic
11987 -- package G is
11989 -- package My_H is new H;
11991 -- end G;
11993 -- and a third package Q instantiating G and Nested_G:
11995 -- with G;
11997 -- package Q is
11999 -- package My_G is new G;
12001 -- package My_Nested_G is new My_G.My_H.Nested_G;
12003 -- end Q;
12005 -- The body to be instantiated is that of My_Nested_G and its
12006 -- parent is the instance My_G.My_H. This latter instantiation
12007 -- is done when My_G is analyzed, i.e. after the declarations
12008 -- of My_G and My_Nested_G have been parsed; as a result, the
12009 -- Sloc of My_G.My_H is greater than the Sloc of My_Nested_G.
12011 -- Therefore loading the body of My_G.My_H will cause the body
12012 -- of My_Nested_G to be instantiated because it is deemed to be
12013 -- ahead of My_G.My_H. This means that Load_Parent_Of_Generic
12014 -- will again be invoked on My_G.My_H, but this time with the
12015 -- Collect_Previous_Instances machinery disabled, so there is
12016 -- no endless mutual recursion and things are done in order.
12018 if Present (Corresponding_Body (Instance_Spec (Inst_Node))) then
12019 goto Leave;
12020 end if;
12022 Gen_Body_Id := Corresponding_Body (Gen_Decl);
12023 end if;
12024 end if;
12026 -- Establish global variable for sloc adjustment and for error recovery
12027 -- In the case of an instance body for an instantiation with actuals
12028 -- from a limited view, the instance body is placed at the beginning
12029 -- of the enclosing package body: use the body entity as the source
12030 -- location for nodes of the instance body.
12032 if not Is_Empty_Elmt_List (Incomplete_Actuals (Act_Decl_Id)) then
12033 declare
12034 Scop : constant Entity_Id := Scope (Act_Decl_Id);
12035 Body_Id : constant Node_Id :=
12036 Corresponding_Body (Unit_Declaration_Node (Scop));
12038 begin
12039 Instantiation_Node := Body_Id;
12040 end;
12041 else
12042 Instantiation_Node := Inst_Node;
12043 end if;
12045 if Present (Gen_Body_Id) then
12046 Save_Env (Gen_Unit, Act_Decl_Id);
12047 Style_Check := False;
12049 -- If the context of the instance is subject to SPARK_Mode "off", the
12050 -- annotation is missing, or the body is instantiated at a later pass
12051 -- and its spec ignored SPARK_Mode pragma, set the global flag which
12052 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within the
12053 -- instance.
12055 if SPARK_Mode /= On
12056 or else Ignore_SPARK_Mode_Pragmas (Act_Decl_Id)
12057 then
12058 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
12059 end if;
12061 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
12062 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
12064 Create_Instantiation_Source
12065 (Inst_Node, Gen_Body_Id, S_Adjustment);
12067 Act_Body :=
12068 Copy_Generic_Node
12069 (Original_Node (Gen_Body), Empty, Instantiating => True);
12071 -- Create proper (possibly qualified) defining name for the body, to
12072 -- correspond to the one in the spec.
12074 Act_Body_Id :=
12075 Make_Defining_Identifier (Sloc (Act_Decl_Id), Chars (Act_Decl_Id));
12076 Preserve_Comes_From_Source (Act_Body_Id, Act_Decl_Id);
12078 -- Some attributes of spec entity are not inherited by body entity
12080 Set_Handler_Records (Act_Body_Id, No_List);
12082 if Nkind (Defining_Unit_Name (Act_Spec)) =
12083 N_Defining_Program_Unit_Name
12084 then
12085 Act_Body_Name :=
12086 Make_Defining_Program_Unit_Name (Loc,
12087 Name =>
12088 New_Copy_Tree (Name (Defining_Unit_Name (Act_Spec))),
12089 Defining_Identifier => Act_Body_Id);
12090 else
12091 Act_Body_Name := Act_Body_Id;
12092 end if;
12094 Set_Defining_Unit_Name (Act_Body, Act_Body_Name);
12096 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
12097 Check_Generic_Actuals (Act_Decl_Id, False);
12098 Check_Initialized_Types;
12100 -- Install primitives hidden at the point of the instantiation but
12101 -- visible when processing the generic formals
12103 declare
12104 E : Entity_Id;
12106 begin
12107 E := First_Entity (Act_Decl_Id);
12108 while Present (E) loop
12109 if Is_Type (E)
12110 and then not Is_Itype (E)
12111 and then Is_Generic_Actual_Type (E)
12112 and then Is_Tagged_Type (E)
12113 then
12114 Install_Hidden_Primitives
12115 (Prims_List => Vis_Prims_List,
12116 Gen_T => Generic_Parent_Type (Parent (E)),
12117 Act_T => E);
12118 end if;
12120 Next_Entity (E);
12121 end loop;
12122 end;
12124 Scope_Check_Id := Current_Scope;
12125 Scope_Check_Last := Scope_Stack.Last;
12127 -- If the instantiation appears within a generic child some actual
12128 -- parameter may be the current instance of the enclosing generic
12129 -- parent.
12131 declare
12132 Inst_Scope : constant Entity_Id := Scope (Act_Decl_Id);
12134 begin
12135 if Is_Child_Unit (Inst_Scope)
12136 and then Ekind (Inst_Scope) = E_Generic_Package
12137 and then Present (Generic_Associations (Inst_Node))
12138 then
12139 Install_Parents_Of_Generic_Context (Inst_Scope, Ctx_Parents);
12141 -- Hide them from visibility; required to avoid conflicts
12142 -- installing the parent instance.
12144 if Present (Ctx_Parents) then
12145 Push_Scope (Standard_Standard);
12146 Ctx_Top := Scope_Stack.Last;
12147 Scope_Stack.Table (Ctx_Top).Is_Active_Stack_Base := True;
12148 end if;
12149 end if;
12150 end;
12152 -- If it is a child unit, make the parent instance (which is an
12153 -- instance of the parent of the generic) visible.
12155 -- 1) The child unit's parent is an explicit parent instance (the
12156 -- prefix of the name of the generic unit):
12158 -- package Child_Package is new Parent_Instance.Child_Unit;
12160 -- 2) The child unit's parent is an implicit parent instance (e.g.
12161 -- when instantiating a sibling package):
12163 -- generic
12164 -- package Parent.Second_Child is
12165 -- ...
12167 -- generic
12168 -- package Parent.First_Child is
12169 -- package Sibling_Package is new Second_Child;
12171 -- 3) The child unit's parent is not an instance, so the scope is
12172 -- simply the one of the unit.
12174 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
12175 and then Nkind (Gen_Id) = N_Expanded_Name
12176 then
12177 Par_Ent := Entity (Prefix (Gen_Id));
12178 Par_Vis := Is_Immediately_Visible (Par_Ent);
12179 Install_Parent (Par_Ent, In_Body => True);
12180 Par_Installed := True;
12182 elsif Ekind (Scope (Gen_Unit)) = E_Generic_Package
12183 and then Ekind (Scope (Act_Decl_Id)) = E_Package
12184 and then Is_Generic_Instance (Scope (Act_Decl_Id))
12185 and then Nkind
12186 (Name (Get_Unit_Instantiation_Node
12187 (Scope (Act_Decl_Id)))) = N_Expanded_Name
12188 then
12189 Par_Ent := Entity
12190 (Prefix (Name (Get_Unit_Instantiation_Node
12191 (Scope (Act_Decl_Id)))));
12192 Par_Vis := Is_Immediately_Visible (Par_Ent);
12193 Install_Parent (Par_Ent, In_Body => True);
12194 Par_Installed := True;
12196 elsif Is_Child_Unit (Gen_Unit) then
12197 Par_Ent := Scope (Gen_Unit);
12198 Par_Vis := Is_Immediately_Visible (Par_Ent);
12199 Install_Parent (Par_Ent, In_Body => True);
12200 Par_Installed := True;
12201 end if;
12203 -- If the instantiation is a library unit, and this is the main unit,
12204 -- then build the resulting compilation unit nodes for the instance.
12205 -- If this is a compilation unit but it is not the main unit, then it
12206 -- is the body of a unit in the context, that is being compiled
12207 -- because it is encloses some inlined unit or another generic unit
12208 -- being instantiated. In that case, this body is not part of the
12209 -- current compilation, and is not attached to the tree, but its
12210 -- parent must be set for analysis.
12212 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
12214 -- Replace instance node with body of instance, and create new
12215 -- node for corresponding instance declaration.
12217 Build_Instance_Compilation_Unit_Nodes
12218 (Inst_Node, Act_Body, Act_Decl);
12220 -- If the instantiation appears within a generic child package
12221 -- enable visibility of current instance of enclosing generic
12222 -- parents.
12224 if Present (Ctx_Parents) then
12225 Scope_Stack.Table (Ctx_Top).Is_Active_Stack_Base := False;
12226 Analyze (Inst_Node);
12227 Scope_Stack.Table (Ctx_Top).Is_Active_Stack_Base := True;
12228 else
12229 Analyze (Inst_Node);
12230 end if;
12232 if Parent (Inst_Node) = Cunit (Main_Unit) then
12234 -- If the instance is a child unit itself, then set the scope
12235 -- of the expanded body to be the parent of the instantiation
12236 -- (ensuring that the fully qualified name will be generated
12237 -- for the elaboration subprogram).
12239 if Nkind (Defining_Unit_Name (Act_Spec)) =
12240 N_Defining_Program_Unit_Name
12241 then
12242 Set_Scope (Defining_Entity (Inst_Node), Scope (Act_Decl_Id));
12243 end if;
12244 end if;
12246 -- Case where instantiation is not a library unit
12248 else
12249 -- Handle the case of an instance with incomplete actual types.
12250 -- The instance body cannot be placed just after the declaration
12251 -- because full views have not been seen yet. Any use of the non-
12252 -- limited views in the instance body requires the presence of a
12253 -- regular with_clause in the enclosing unit. Therefore we place
12254 -- the instance body at the beginning of the enclosing body, and
12255 -- the freeze node for the instance is then placed after the body.
12257 if not Is_Empty_Elmt_List (Incomplete_Actuals (Act_Decl_Id))
12258 and then Ekind (Scope (Act_Decl_Id)) = E_Package
12259 then
12260 declare
12261 Scop : constant Entity_Id := Scope (Act_Decl_Id);
12262 Body_Id : constant Node_Id :=
12263 Corresponding_Body (Unit_Declaration_Node (Scop));
12265 F_Node : Node_Id;
12267 begin
12268 pragma Assert (Present (Body_Id));
12270 Prepend (Act_Body, Declarations (Parent (Body_Id)));
12272 if Expander_Active then
12273 Ensure_Freeze_Node (Act_Decl_Id);
12274 F_Node := Freeze_Node (Act_Decl_Id);
12275 Set_Is_Frozen (Act_Decl_Id, False);
12276 if Is_List_Member (F_Node) then
12277 Remove (F_Node);
12278 end if;
12280 Insert_After (Act_Body, F_Node);
12281 end if;
12282 end;
12284 else
12285 Insert_Before (Inst_Node, Act_Body);
12286 Mark_Rewrite_Insertion (Act_Body);
12288 -- Insert the freeze node for the instance if need be
12290 if Expander_Active then
12291 Freeze_Package_Instance
12292 (Inst_Node, Gen_Body, Gen_Decl, Act_Decl_Id);
12293 Set_Is_Frozen (Act_Decl_Id);
12294 end if;
12295 end if;
12297 -- If the instantiation appears within a generic child package
12298 -- enable visibility of current instance of enclosing generic
12299 -- parents.
12301 if Present (Ctx_Parents) then
12302 Scope_Stack.Table (Ctx_Top).Is_Active_Stack_Base := False;
12303 Analyze (Act_Body);
12304 Scope_Stack.Table (Ctx_Top).Is_Active_Stack_Base := True;
12305 else
12306 Analyze (Act_Body);
12307 end if;
12308 end if;
12310 Inherit_Context (Gen_Body, Inst_Node);
12312 if Par_Installed then
12313 Remove_Parent (In_Body => True);
12315 -- Restore the previous visibility of the parent
12317 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
12318 end if;
12320 -- Remove the parent instances if they have been placed on the scope
12321 -- stack to compile the body.
12323 if Present (Ctx_Parents) then
12324 pragma Assert (Scope_Stack.Last = Ctx_Top
12325 and then Current_Scope = Standard_Standard);
12326 Pop_Scope;
12328 Remove_Parents_Of_Generic_Context (Ctx_Parents);
12329 end if;
12331 pragma Assert (Current_Scope = Scope_Check_Id);
12332 pragma Assert (Scope_Stack.Last = Scope_Check_Last);
12334 Restore_Hidden_Primitives (Vis_Prims_List);
12336 -- Restore the private views that were made visible when the body of
12337 -- the instantiation was created. Note that, in the case where one of
12338 -- these private views is declared in the parent, there is a nesting
12339 -- issue with the calls to Install_Parent and Remove_Parent made in
12340 -- between above with In_Body set to True, because these calls also
12341 -- want to swap and restore this private view respectively. In this
12342 -- case, the call to Install_Parent does nothing, but the call to
12343 -- Remove_Parent does restore the private view, thus undercutting the
12344 -- call to Restore_Private_Views. That's OK under the condition that
12345 -- the two mechanisms swap exactly the same entities, in particular
12346 -- the private entities dependent on the primary private entities.
12348 Restore_Private_Views (Act_Decl_Id);
12350 -- Remove the current unit from visibility if this is an instance
12351 -- that is not elaborated on the fly for inlining purposes.
12353 if not Inlined_Body then
12354 Set_Is_Immediately_Visible (Act_Decl_Id, False);
12355 end if;
12357 Restore_Env;
12359 -- If we have no body, and the unit requires a body, then complain. This
12360 -- complaint is suppressed if we have detected other errors (since a
12361 -- common reason for missing the body is that it had errors).
12362 -- In CodePeer mode, a warning has been emitted already, no need for
12363 -- further messages.
12365 elsif Unit_Requires_Body (Gen_Unit)
12366 and then not Body_Optional
12367 then
12368 if CodePeer_Mode then
12369 null;
12371 elsif Serious_Errors_Detected = 0 then
12372 Error_Msg_NE
12373 ("cannot find body of generic package &", Inst_Node, Gen_Unit);
12375 -- Don't attempt to perform any cleanup actions if some other error
12376 -- was already detected, since this can cause blowups.
12378 else
12379 goto Leave;
12380 end if;
12382 -- Case of package that does not need a body
12384 else
12385 -- If the instantiation of the declaration is a library unit, rewrite
12386 -- the original package instantiation as a package declaration in the
12387 -- compilation unit node.
12389 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
12390 Set_Parent_Spec (Act_Decl, Parent_Spec (Inst_Node));
12391 Rewrite (Inst_Node, Act_Decl);
12393 -- Generate elaboration entity, in case spec has elaboration code.
12394 -- This cannot be done when the instance is analyzed, because it
12395 -- is not known yet whether the body exists.
12397 Set_Elaboration_Entity_Required (Act_Decl_Id, False);
12398 Build_Elaboration_Entity (Parent (Inst_Node), Act_Decl_Id);
12400 -- If the instantiation is not a library unit, then append the
12401 -- declaration to the list of implicitly generated entities, unless
12402 -- it is already a list member which means that it was already
12403 -- processed
12405 elsif not Is_List_Member (Act_Decl) then
12406 Mark_Rewrite_Insertion (Act_Decl);
12407 Insert_Before (Inst_Node, Act_Decl);
12408 end if;
12409 end if;
12411 <<Leave>>
12413 -- Restore the context that was in effect prior to instantiating the
12414 -- package body.
12416 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
12417 Local_Suppress_Stack_Top := Saved_LSST;
12418 Scope_Suppress := Saved_SS;
12419 Style_Check := Saved_SC;
12421 Expander_Mode_Restore;
12422 Restore_Config_Switches (Saved_CS);
12423 Restore_Ghost_Region (Saved_GM, Saved_IGR);
12424 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
12425 Restore_Warnings (Saved_Warn);
12426 end Instantiate_Package_Body;
12428 ---------------------------------
12429 -- Instantiate_Subprogram_Body --
12430 ---------------------------------
12432 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
12433 -- must be replaced by gotos which jump to the end of the routine in order
12434 -- to restore the Ghost and SPARK modes.
12436 procedure Instantiate_Subprogram_Body
12437 (Body_Info : Pending_Body_Info;
12438 Body_Optional : Boolean := False)
12440 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
12441 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Decl);
12442 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
12443 Gen_Id : constant Node_Id := Name (Inst_Node);
12444 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
12445 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
12446 Loc : constant Source_Ptr := Sloc (Inst_Node);
12447 Pack_Id : constant Entity_Id :=
12448 Defining_Unit_Name (Parent (Act_Decl));
12450 -- The following constants capture the context prior to instantiating
12451 -- the subprogram body.
12453 Saved_CS : constant Config_Switches_Type := Save_Config_Switches;
12454 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
12455 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
12456 Saved_ISMP : constant Boolean :=
12457 Ignore_SPARK_Mode_Pragmas_In_Instance;
12458 Saved_LSST : constant Suppress_Stack_Entry_Ptr :=
12459 Local_Suppress_Stack_Top;
12460 Saved_SC : constant Boolean := Style_Check;
12461 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
12462 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
12463 Saved_SS : constant Suppress_Record := Scope_Suppress;
12464 Saved_Warn : constant Warnings_State := Save_Warnings;
12466 Act_Body : Node_Id;
12467 Act_Body_Id : Entity_Id;
12468 Gen_Body : Node_Id;
12469 Gen_Body_Id : Node_Id;
12470 Pack_Body : Node_Id;
12471 Par_Ent : Entity_Id := Empty;
12472 Par_Installed : Boolean := False;
12473 Par_Vis : Boolean := False;
12474 Ret_Expr : Node_Id;
12476 begin
12477 Gen_Body_Id := Corresponding_Body (Gen_Decl);
12479 -- Subprogram body may have been created already because of an inline
12480 -- pragma, or because of multiple elaborations of the enclosing package
12481 -- when several instances of the subprogram appear in the main unit.
12483 if Present (Corresponding_Body (Act_Decl)) then
12484 return;
12485 end if;
12487 -- The subprogram being instantiated may be subject to pragma Ghost. Set
12488 -- the mode now to ensure that any nodes generated during instantiation
12489 -- are properly marked as Ghost.
12491 Set_Ghost_Mode (Act_Decl_Id);
12493 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
12495 -- Re-establish the state of information on which checks are suppressed.
12496 -- This information was set in Body_Info at the point of instantiation,
12497 -- and now we restore it so that the instance is compiled using the
12498 -- check status at the instantiation (RM 11.5(7.2/2), AI95-00224-01).
12500 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
12501 Scope_Suppress := Body_Info.Scope_Suppress;
12503 Restore_Config_Switches (Body_Info.Config_Switches);
12504 Restore_Warnings (Body_Info.Warnings);
12506 if No (Gen_Body_Id) then
12508 -- For imported generic subprogram, no body to compile, complete
12509 -- the spec entity appropriately.
12511 if Is_Imported (Gen_Unit) then
12512 Set_Is_Imported (Act_Decl_Id);
12513 Set_First_Rep_Item (Act_Decl_Id, First_Rep_Item (Gen_Unit));
12514 Set_Interface_Name (Act_Decl_Id, Interface_Name (Gen_Unit));
12515 Set_Convention (Act_Decl_Id, Convention (Gen_Unit));
12516 Set_Has_Completion (Act_Decl_Id);
12517 goto Leave;
12519 -- For other cases, compile the body
12521 else
12522 Load_Parent_Of_Generic
12523 (Inst_Node, Specification (Gen_Decl), Body_Optional);
12524 Gen_Body_Id := Corresponding_Body (Gen_Decl);
12525 end if;
12526 end if;
12528 Instantiation_Node := Inst_Node;
12530 if Present (Gen_Body_Id) then
12531 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
12533 if Nkind (Gen_Body) = N_Subprogram_Body_Stub then
12535 -- Either body is not present, or context is non-expanding, as
12536 -- when compiling a subunit. Mark the instance as completed, and
12537 -- diagnose a missing body when needed.
12539 if Expander_Active
12540 and then Operating_Mode = Generate_Code
12541 then
12542 Error_Msg_N ("missing proper body for instantiation", Gen_Body);
12543 end if;
12545 Set_Has_Completion (Act_Decl_Id);
12546 goto Leave;
12547 end if;
12549 Save_Env (Gen_Unit, Act_Decl_Id);
12550 Style_Check := False;
12552 -- If the context of the instance is subject to SPARK_Mode "off", the
12553 -- annotation is missing, or the body is instantiated at a later pass
12554 -- and its spec ignored SPARK_Mode pragma, set the global flag which
12555 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within the
12556 -- instance.
12558 if SPARK_Mode /= On
12559 or else Ignore_SPARK_Mode_Pragmas (Act_Decl_Id)
12560 then
12561 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
12562 end if;
12564 -- If the context of an instance is not subject to SPARK_Mode "off",
12565 -- and the generic body is subject to an explicit SPARK_Mode pragma,
12566 -- the latter should be the one applicable to the instance.
12568 if not Ignore_SPARK_Mode_Pragmas_In_Instance
12569 and then SPARK_Mode /= Off
12570 and then Present (SPARK_Pragma (Gen_Body_Id))
12571 then
12572 Set_SPARK_Mode (Gen_Body_Id);
12573 end if;
12575 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
12576 Create_Instantiation_Source
12577 (Inst_Node,
12578 Gen_Body_Id,
12579 S_Adjustment);
12581 Act_Body :=
12582 Copy_Generic_Node
12583 (Original_Node (Gen_Body), Empty, Instantiating => True);
12585 -- Create proper defining name for the body, to correspond to the one
12586 -- in the spec.
12588 Act_Body_Id :=
12589 Make_Defining_Identifier (Sloc (Act_Decl_Id), Chars (Act_Decl_Id));
12591 Preserve_Comes_From_Source (Act_Body_Id, Act_Decl_Id);
12592 Set_Defining_Unit_Name (Specification (Act_Body), Act_Body_Id);
12594 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
12595 Set_Has_Completion (Act_Decl_Id);
12596 Check_Generic_Actuals (Pack_Id, False);
12598 -- Generate a reference to link the visible subprogram instance to
12599 -- the generic body, which for navigation purposes is the only
12600 -- available source for the instance.
12602 Generate_Reference
12603 (Related_Instance (Pack_Id),
12604 Gen_Body_Id, 'b', Set_Ref => False, Force => True);
12606 -- If it is a child unit, make the parent instance (which is an
12607 -- instance of the parent of the generic) visible. The parent
12608 -- instance is the prefix of the name of the generic unit.
12610 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
12611 and then Nkind (Gen_Id) = N_Expanded_Name
12612 then
12613 Par_Ent := Entity (Prefix (Gen_Id));
12614 Par_Vis := Is_Immediately_Visible (Par_Ent);
12615 Install_Parent (Par_Ent, In_Body => True);
12616 Par_Installed := True;
12618 elsif Is_Child_Unit (Gen_Unit) then
12619 Par_Ent := Scope (Gen_Unit);
12620 Par_Vis := Is_Immediately_Visible (Par_Ent);
12621 Install_Parent (Par_Ent, In_Body => True);
12622 Par_Installed := True;
12623 end if;
12625 -- Subprogram body is placed in the body of wrapper package,
12626 -- whose spec contains the subprogram declaration as well as
12627 -- the renaming declarations for the generic parameters.
12629 Pack_Body :=
12630 Make_Package_Body (Loc,
12631 Defining_Unit_Name => New_Copy (Pack_Id),
12632 Declarations => New_List (Act_Body));
12634 Set_Corresponding_Spec (Pack_Body, Pack_Id);
12636 -- If the instantiation is a library unit, then build resulting
12637 -- compilation unit nodes for the instance. The declaration of
12638 -- the enclosing package is the grandparent of the subprogram
12639 -- declaration. First replace the instantiation node as the unit
12640 -- of the corresponding compilation.
12642 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
12643 if Parent (Inst_Node) = Cunit (Main_Unit) then
12644 Set_Unit (Parent (Inst_Node), Inst_Node);
12645 Build_Instance_Compilation_Unit_Nodes
12646 (Inst_Node, Pack_Body, Parent (Parent (Act_Decl)));
12647 Analyze (Inst_Node);
12648 else
12649 Set_Parent (Pack_Body, Parent (Inst_Node));
12650 Analyze (Pack_Body);
12651 end if;
12653 else
12654 Insert_Before (Inst_Node, Pack_Body);
12655 Mark_Rewrite_Insertion (Pack_Body);
12657 -- Insert the freeze node for the instance if need be
12659 if Expander_Active then
12660 Freeze_Subprogram_Instance (Inst_Node, Gen_Body, Pack_Id);
12661 end if;
12663 Analyze (Pack_Body);
12664 end if;
12666 Inherit_Context (Gen_Body, Inst_Node);
12668 Restore_Private_Views (Pack_Id, False);
12670 if Par_Installed then
12671 Remove_Parent (In_Body => True);
12673 -- Restore the previous visibility of the parent
12675 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
12676 end if;
12678 Restore_Env;
12680 -- Body not found. Error was emitted already. If there were no previous
12681 -- errors, this may be an instance whose scope is a premature instance.
12682 -- In that case we must insure that the (legal) program does raise
12683 -- program error if executed. We generate a subprogram body for this
12684 -- purpose.
12686 elsif Serious_Errors_Detected = 0
12687 and then Nkind (Parent (Inst_Node)) /= N_Compilation_Unit
12688 then
12689 if Body_Optional then
12690 goto Leave;
12692 elsif Ekind (Act_Decl_Id) = E_Procedure then
12693 Act_Body :=
12694 Make_Subprogram_Body (Loc,
12695 Specification =>
12696 Make_Procedure_Specification (Loc,
12697 Defining_Unit_Name =>
12698 Make_Defining_Identifier (Loc, Chars (Act_Decl_Id)),
12699 Parameter_Specifications =>
12700 New_Copy_List
12701 (Parameter_Specifications (Parent (Act_Decl_Id)))),
12703 Declarations => Empty_List,
12704 Handled_Statement_Sequence =>
12705 Make_Handled_Sequence_Of_Statements (Loc,
12706 Statements => New_List (
12707 Make_Raise_Program_Error (Loc,
12708 Reason => PE_Access_Before_Elaboration))));
12710 else
12711 Ret_Expr :=
12712 Make_Raise_Program_Error (Loc,
12713 Reason => PE_Access_Before_Elaboration);
12715 Set_Etype (Ret_Expr, (Etype (Act_Decl_Id)));
12716 Set_Analyzed (Ret_Expr);
12718 Act_Body :=
12719 Make_Subprogram_Body (Loc,
12720 Specification =>
12721 Make_Function_Specification (Loc,
12722 Defining_Unit_Name =>
12723 Make_Defining_Identifier (Loc, Chars (Act_Decl_Id)),
12724 Parameter_Specifications =>
12725 New_Copy_List
12726 (Parameter_Specifications (Parent (Act_Decl_Id))),
12727 Result_Definition =>
12728 New_Occurrence_Of (Etype (Act_Decl_Id), Loc)),
12730 Declarations => Empty_List,
12731 Handled_Statement_Sequence =>
12732 Make_Handled_Sequence_Of_Statements (Loc,
12733 Statements => New_List (
12734 Make_Simple_Return_Statement (Loc, Ret_Expr))));
12735 end if;
12737 Pack_Body :=
12738 Make_Package_Body (Loc,
12739 Defining_Unit_Name => New_Copy (Pack_Id),
12740 Declarations => New_List (Act_Body));
12742 Insert_After (Inst_Node, Pack_Body);
12743 Set_Corresponding_Spec (Pack_Body, Pack_Id);
12744 Analyze (Pack_Body);
12745 end if;
12747 <<Leave>>
12749 -- Restore the context that was in effect prior to instantiating the
12750 -- subprogram body.
12752 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
12753 Local_Suppress_Stack_Top := Saved_LSST;
12754 Scope_Suppress := Saved_SS;
12755 Style_Check := Saved_SC;
12757 Expander_Mode_Restore;
12758 Restore_Config_Switches (Saved_CS);
12759 Restore_Ghost_Region (Saved_GM, Saved_IGR);
12760 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
12761 Restore_Warnings (Saved_Warn);
12762 end Instantiate_Subprogram_Body;
12764 ----------------------
12765 -- Instantiate_Type --
12766 ----------------------
12768 function Instantiate_Type
12769 (Formal : Node_Id;
12770 Actual : Node_Id;
12771 Analyzed_Formal : Node_Id;
12772 Actual_Decls : List_Id) return List_Id
12774 A_Gen_T : constant Entity_Id :=
12775 Defining_Identifier (Analyzed_Formal);
12776 Def : constant Node_Id := Formal_Type_Definition (Formal);
12777 Gen_T : constant Entity_Id := Defining_Identifier (Formal);
12778 Act_T : Entity_Id;
12779 Ancestor : Entity_Id := Empty;
12780 Decl_Node : Node_Id;
12781 Decl_Nodes : List_Id;
12782 Loc : Source_Ptr;
12783 Subt : Entity_Id;
12785 procedure Check_Shared_Variable_Control_Aspects;
12786 -- Ada 2022: Verify that shared variable control aspects (RM C.6)
12787 -- that may be specified for a formal type are obeyed by the actual.
12789 procedure Diagnose_Predicated_Actual;
12790 -- There are a number of constructs in which a discrete type with
12791 -- predicates is illegal, e.g. as an index in an array type declaration.
12792 -- If a generic type is used is such a construct in a generic package
12793 -- declaration, it carries the flag No_Predicate_On_Actual. it is part
12794 -- of the generic contract that the actual cannot have predicates.
12796 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean;
12797 -- Check that base types are the same and that the subtypes match
12798 -- statically. Used in several of the validation subprograms for
12799 -- actuals in instantiations.
12801 procedure Validate_Array_Type_Instance;
12802 procedure Validate_Access_Subprogram_Instance;
12803 procedure Validate_Access_Type_Instance;
12804 procedure Validate_Derived_Type_Instance;
12805 procedure Validate_Derived_Interface_Type_Instance;
12806 procedure Validate_Discriminated_Formal_Type;
12807 procedure Validate_Interface_Type_Instance;
12808 procedure Validate_Private_Type_Instance;
12809 procedure Validate_Incomplete_Type_Instance;
12810 -- These procedures perform validation tests for the named case.
12811 -- Validate_Discriminated_Formal_Type is shared by formal private
12812 -- types and Ada 2012 formal incomplete types.
12814 --------------------------------------------
12815 -- Check_Shared_Variable_Control_Aspects --
12816 --------------------------------------------
12818 -- Ada 2022: Verify that shared variable control aspects (RM C.6)
12819 -- that may be specified for the formal are obeyed by the actual.
12820 -- If the formal is a derived type the aspect specifications must match.
12821 -- NOTE: AI12-0282 implies that matching of aspects is required between
12822 -- formal and actual in all cases, but this is too restrictive.
12823 -- In particular it violates a language design rule: a limited private
12824 -- indefinite formal can be matched by any actual. The current code
12825 -- reflects an older and more permissive version of RM C.6 (12/5).
12827 procedure Check_Shared_Variable_Control_Aspects is
12828 begin
12829 if Ada_Version >= Ada_2022 then
12830 if Is_Atomic (A_Gen_T) and then not Is_Atomic (Act_T) then
12831 Error_Msg_NE
12832 ("actual for& must have Atomic aspect", Actual, A_Gen_T);
12834 elsif Is_Derived_Type (A_Gen_T)
12835 and then Is_Atomic (A_Gen_T) /= Is_Atomic (Act_T)
12836 then
12837 Error_Msg_NE
12838 ("actual for& has different Atomic aspect", Actual, A_Gen_T);
12839 end if;
12841 if Is_Volatile (A_Gen_T) and then not Is_Volatile (Act_T) then
12842 Error_Msg_NE
12843 ("actual for& must have Volatile aspect",
12844 Actual, A_Gen_T);
12846 elsif Is_Derived_Type (A_Gen_T)
12847 and then Is_Volatile (A_Gen_T) /= Is_Volatile (Act_T)
12848 then
12849 Error_Msg_NE
12850 ("actual for& has different Volatile aspect",
12851 Actual, A_Gen_T);
12852 end if;
12854 -- We assume that an array type whose atomic component type
12855 -- is Atomic is equivalent to an array type with the explicit
12856 -- aspect Has_Atomic_Components. This is a reasonable inference
12857 -- from the intent of AI12-0282, and makes it legal to use an
12858 -- actual that does not have the identical aspect as the formal.
12859 -- Ditto for volatile components.
12861 declare
12862 Actual_Atomic_Comp : constant Boolean :=
12863 Has_Atomic_Components (Act_T)
12864 or else (Is_Array_Type (Act_T)
12865 and then Is_Atomic (Component_Type (Act_T)));
12866 begin
12867 if Has_Atomic_Components (A_Gen_T) /= Actual_Atomic_Comp then
12868 Error_Msg_NE
12869 ("formal and actual for& must agree on atomic components",
12870 Actual, A_Gen_T);
12871 end if;
12872 end;
12874 declare
12875 Actual_Volatile_Comp : constant Boolean :=
12876 Has_Volatile_Components (Act_T)
12877 or else (Is_Array_Type (Act_T)
12878 and then Is_Volatile (Component_Type (Act_T)));
12879 begin
12880 if Has_Volatile_Components (A_Gen_T) /= Actual_Volatile_Comp
12881 then
12882 Error_Msg_NE
12883 ("actual for& must have volatile components",
12884 Actual, A_Gen_T);
12885 end if;
12886 end;
12888 -- The following two aspects do not require exact matching,
12889 -- but only one-way agreement. See RM C.6.
12891 if Is_Independent (A_Gen_T) and then not Is_Independent (Act_T)
12892 then
12893 Error_Msg_NE
12894 ("actual for& must have Independent aspect specified",
12895 Actual, A_Gen_T);
12896 end if;
12898 if Has_Independent_Components (A_Gen_T)
12899 and then not Has_Independent_Components (Act_T)
12900 then
12901 Error_Msg_NE
12902 ("actual for& must have Independent_Components specified",
12903 Actual, A_Gen_T);
12904 end if;
12906 -- Check actual/formal compatibility with respect to the four
12907 -- volatility refinement aspects.
12909 Check_Volatility_Compatibility
12910 (Act_T, A_Gen_T,
12911 "actual type", "its corresponding formal type",
12912 Srcpos_Bearer => Actual);
12913 end if;
12914 end Check_Shared_Variable_Control_Aspects;
12916 ---------------------------------
12917 -- Diagnose_Predicated_Actual --
12918 ---------------------------------
12920 procedure Diagnose_Predicated_Actual is
12921 begin
12922 if No_Predicate_On_Actual (A_Gen_T)
12923 and then Has_Predicates (Act_T)
12924 then
12925 Error_Msg_NE
12926 ("actual for& cannot be a type with predicate",
12927 Instantiation_Node, A_Gen_T);
12929 elsif No_Dynamic_Predicate_On_Actual (A_Gen_T)
12930 and then Has_Predicates (Act_T)
12931 and then not Has_Static_Predicate_Aspect (Act_T)
12932 then
12933 Error_Msg_NE
12934 ("actual for& cannot be a type with a dynamic predicate",
12935 Instantiation_Node, A_Gen_T);
12936 end if;
12937 end Diagnose_Predicated_Actual;
12939 --------------------
12940 -- Subtypes_Match --
12941 --------------------
12943 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean is
12944 T : constant Entity_Id := Get_Instance_Of (Gen_T);
12946 begin
12947 -- Check that the base types, root types (when dealing with class
12948 -- wide types), or designated types (when dealing with anonymous
12949 -- access types) of Gen_T and Act_T are statically matching subtypes.
12951 return ((Base_Type (T) = Act_T
12952 or else Base_Type (T) = Base_Type (Act_T))
12953 and then Subtypes_Statically_Match (T, Act_T))
12955 or else (Is_Class_Wide_Type (Gen_T)
12956 and then Is_Class_Wide_Type (Act_T)
12957 and then Subtypes_Match
12958 (Get_Instance_Of (Root_Type (Gen_T)),
12959 Root_Type (Act_T)))
12961 or else (Is_Anonymous_Access_Type (Gen_T)
12962 and then Ekind (Act_T) = Ekind (Gen_T)
12963 and then Subtypes_Statically_Match
12964 (Designated_Type (Gen_T), Designated_Type (Act_T)));
12965 end Subtypes_Match;
12967 -----------------------------------------
12968 -- Validate_Access_Subprogram_Instance --
12969 -----------------------------------------
12971 procedure Validate_Access_Subprogram_Instance is
12972 begin
12973 if not Is_Access_Type (Act_T)
12974 or else Ekind (Designated_Type (Act_T)) /= E_Subprogram_Type
12975 then
12976 Error_Msg_NE
12977 ("expect access type in instantiation of &", Actual, Gen_T);
12978 Abandon_Instantiation (Actual);
12979 end if;
12981 -- According to AI05-288, actuals for access_to_subprograms must be
12982 -- subtype conformant with the generic formal. Previous to AI05-288
12983 -- only mode conformance was required.
12985 -- This is a binding interpretation that applies to previous versions
12986 -- of the language, no need to maintain previous weaker checks.
12988 Check_Subtype_Conformant
12989 (Designated_Type (Act_T),
12990 Designated_Type (A_Gen_T),
12991 Actual,
12992 Get_Inst => True);
12994 if Ekind (Base_Type (Act_T)) = E_Access_Protected_Subprogram_Type then
12995 if Ekind (A_Gen_T) = E_Access_Subprogram_Type then
12996 Error_Msg_NE
12997 ("protected access type not allowed for formal &",
12998 Actual, Gen_T);
12999 end if;
13001 elsif Ekind (A_Gen_T) = E_Access_Protected_Subprogram_Type then
13002 Error_Msg_NE
13003 ("expect protected access type for formal &",
13004 Actual, Gen_T);
13005 end if;
13007 -- If the formal has a specified convention (which in most cases
13008 -- will be StdCall) verify that the actual has the same convention.
13010 if Has_Convention_Pragma (A_Gen_T)
13011 and then Convention (A_Gen_T) /= Convention (Act_T)
13012 then
13013 Error_Msg_Name_1 := Get_Convention_Name (Convention (A_Gen_T));
13014 Error_Msg_NE
13015 ("actual for formal & must have convention %", Actual, Gen_T);
13016 end if;
13018 if Can_Never_Be_Null (A_Gen_T) /= Can_Never_Be_Null (Act_T) then
13019 Error_Msg_NE
13020 ("non null exclusion of actual and formal & do not match",
13021 Actual, Gen_T);
13022 end if;
13023 end Validate_Access_Subprogram_Instance;
13025 -----------------------------------
13026 -- Validate_Access_Type_Instance --
13027 -----------------------------------
13029 procedure Validate_Access_Type_Instance is
13030 Desig_Type : constant Entity_Id :=
13031 Find_Actual_Type (Designated_Type (A_Gen_T), A_Gen_T);
13032 Desig_Act : Entity_Id;
13034 begin
13035 if not Is_Access_Type (Act_T) then
13036 Error_Msg_NE
13037 ("expect access type in instantiation of &", Actual, Gen_T);
13038 Abandon_Instantiation (Actual);
13039 end if;
13041 if Is_Access_Constant (A_Gen_T) then
13042 if not Is_Access_Constant (Act_T) then
13043 Error_Msg_N
13044 ("actual type must be access-to-constant type", Actual);
13045 Abandon_Instantiation (Actual);
13046 end if;
13047 else
13048 if Is_Access_Constant (Act_T) then
13049 Error_Msg_N
13050 ("actual type must be access-to-variable type", Actual);
13051 Abandon_Instantiation (Actual);
13053 elsif Ekind (A_Gen_T) = E_General_Access_Type
13054 and then Ekind (Base_Type (Act_T)) /= E_General_Access_Type
13055 then
13056 Error_Msg_N
13057 ("actual must be general access type!", Actual);
13058 Error_Msg_NE -- CODEFIX
13059 ("\add ALL to }!", Actual, Act_T);
13060 Abandon_Instantiation (Actual);
13061 end if;
13062 end if;
13064 -- The designated subtypes, that is to say the subtypes introduced
13065 -- by an access type declaration (and not by a subtype declaration)
13066 -- must match.
13068 Desig_Act := Designated_Type (Base_Type (Act_T));
13070 -- The designated type may have been introduced through a limited_
13071 -- with clause, in which case retrieve the non-limited view. This
13072 -- applies to incomplete types as well as to class-wide types.
13074 if From_Limited_With (Desig_Act) then
13075 Desig_Act := Available_View (Desig_Act);
13076 end if;
13078 if not Subtypes_Match (Desig_Type, Desig_Act) then
13079 Error_Msg_NE
13080 ("designated type of actual does not match that of formal &",
13081 Actual, Gen_T);
13083 if not Predicates_Match (Desig_Type, Desig_Act) then
13084 Error_Msg_N ("\predicates do not match", Actual);
13085 end if;
13087 Abandon_Instantiation (Actual);
13088 end if;
13090 -- Ada 2005: null-exclusion indicators of the two types must agree
13092 if Can_Never_Be_Null (A_Gen_T) /= Can_Never_Be_Null (Act_T) then
13093 Error_Msg_NE
13094 ("non null exclusion of actual and formal & do not match",
13095 Actual, Gen_T);
13096 end if;
13097 end Validate_Access_Type_Instance;
13099 ----------------------------------
13100 -- Validate_Array_Type_Instance --
13101 ----------------------------------
13103 procedure Validate_Array_Type_Instance is
13104 I1 : Node_Id;
13105 I2 : Node_Id;
13106 T2 : Entity_Id;
13108 function Formal_Dimensions return Nat;
13109 -- Count number of dimensions in array type formal
13111 -----------------------
13112 -- Formal_Dimensions --
13113 -----------------------
13115 function Formal_Dimensions return Nat is
13116 Num : Nat := 0;
13117 Index : Node_Id;
13119 begin
13120 if Nkind (Def) = N_Constrained_Array_Definition then
13121 Index := First (Discrete_Subtype_Definitions (Def));
13122 else
13123 Index := First (Subtype_Marks (Def));
13124 end if;
13126 while Present (Index) loop
13127 Num := Num + 1;
13128 Next (Index);
13129 end loop;
13131 return Num;
13132 end Formal_Dimensions;
13134 -- Start of processing for Validate_Array_Type_Instance
13136 begin
13137 if not Is_Array_Type (Act_T) then
13138 Error_Msg_NE
13139 ("expect array type in instantiation of &", Actual, Gen_T);
13140 Abandon_Instantiation (Actual);
13142 elsif Nkind (Def) = N_Constrained_Array_Definition then
13143 if not (Is_Constrained (Act_T)) then
13144 Error_Msg_NE
13145 ("expect constrained array in instantiation of &",
13146 Actual, Gen_T);
13147 Abandon_Instantiation (Actual);
13148 end if;
13150 else
13151 if Is_Constrained (Act_T) then
13152 Error_Msg_NE
13153 ("expect unconstrained array in instantiation of &",
13154 Actual, Gen_T);
13155 Abandon_Instantiation (Actual);
13156 end if;
13157 end if;
13159 if Formal_Dimensions /= Number_Dimensions (Act_T) then
13160 Error_Msg_NE
13161 ("dimensions of actual do not match formal &", Actual, Gen_T);
13162 Abandon_Instantiation (Actual);
13163 end if;
13165 I1 := First_Index (A_Gen_T);
13166 I2 := First_Index (Act_T);
13167 for J in 1 .. Formal_Dimensions loop
13169 -- If the indexes of the actual were given by a subtype_mark,
13170 -- the index was transformed into a range attribute. Retrieve
13171 -- the original type mark for checking.
13173 if Is_Entity_Name (Original_Node (I2)) then
13174 T2 := Entity (Original_Node (I2));
13175 else
13176 T2 := Etype (I2);
13177 end if;
13179 if not Subtypes_Match
13180 (Find_Actual_Type (Etype (I1), A_Gen_T), T2)
13181 then
13182 Error_Msg_NE
13183 ("index types of actual do not match those of formal &",
13184 Actual, Gen_T);
13185 Abandon_Instantiation (Actual);
13186 end if;
13188 Next_Index (I1);
13189 Next_Index (I2);
13190 end loop;
13192 -- Check matching subtypes. Note that there are complex visibility
13193 -- issues when the generic is a child unit and some aspect of the
13194 -- generic type is declared in a parent unit of the generic. We do
13195 -- the test to handle this special case only after a direct check
13196 -- for static matching has failed. The case where both the component
13197 -- type and the array type are separate formals, and the component
13198 -- type is a private view may also require special checking in
13199 -- Subtypes_Match. Finally, we assume that a child instance where
13200 -- the component type comes from a formal of a parent instance is
13201 -- correct because the generic was correct. A more precise check
13202 -- seems too complex to install???
13204 if Subtypes_Match
13205 (Component_Type (A_Gen_T), Component_Type (Act_T))
13206 or else
13207 Subtypes_Match
13208 (Find_Actual_Type (Component_Type (A_Gen_T), A_Gen_T),
13209 Component_Type (Act_T))
13210 or else
13211 (not Inside_A_Generic
13212 and then Is_Child_Unit (Scope (Component_Type (A_Gen_T))))
13213 then
13214 null;
13215 else
13216 Error_Msg_NE
13217 ("component subtype of actual does not match that of formal &",
13218 Actual, Gen_T);
13219 Abandon_Instantiation (Actual);
13220 end if;
13222 if Has_Aliased_Components (A_Gen_T)
13223 and then not Has_Aliased_Components (Act_T)
13224 then
13225 Error_Msg_NE
13226 ("actual must have aliased components to match formal type &",
13227 Actual, Gen_T);
13228 end if;
13229 end Validate_Array_Type_Instance;
13231 -----------------------------------------------
13232 -- Validate_Derived_Interface_Type_Instance --
13233 -----------------------------------------------
13235 procedure Validate_Derived_Interface_Type_Instance is
13236 Par : constant Entity_Id := Entity (Subtype_Indication (Def));
13237 Elmt : Elmt_Id;
13239 begin
13240 -- First apply interface instance checks
13242 Validate_Interface_Type_Instance;
13244 -- Verify that immediate parent interface is an ancestor of
13245 -- the actual.
13247 if Present (Par)
13248 and then not Interface_Present_In_Ancestor (Act_T, Par)
13249 then
13250 Error_Msg_NE
13251 ("interface actual must include progenitor&", Actual, Par);
13252 end if;
13254 -- Now verify that the actual includes all other ancestors of
13255 -- the formal.
13257 Elmt := First_Elmt (Interfaces (A_Gen_T));
13258 while Present (Elmt) loop
13259 if not Interface_Present_In_Ancestor
13260 (Act_T, Get_Instance_Of (Node (Elmt)))
13261 then
13262 Error_Msg_NE
13263 ("interface actual must include progenitor&",
13264 Actual, Node (Elmt));
13265 end if;
13267 Next_Elmt (Elmt);
13268 end loop;
13269 end Validate_Derived_Interface_Type_Instance;
13271 ------------------------------------
13272 -- Validate_Derived_Type_Instance --
13273 ------------------------------------
13275 procedure Validate_Derived_Type_Instance is
13276 Actual_Discr : Entity_Id;
13277 Ancestor_Discr : Entity_Id;
13279 begin
13280 -- Verify that the actual includes the progenitors of the formal,
13281 -- if any. The formal may depend on previous formals and their
13282 -- instance, so we must examine instance of interfaces if present.
13283 -- The actual may be an extension of an interface, in which case
13284 -- it does not appear in the interface list, so this must be
13285 -- checked separately.
13287 if Present (Interface_List (Def)) then
13288 if not Has_Interfaces (Act_T) then
13289 Error_Msg_NE
13290 ("actual must implement all interfaces of formal&",
13291 Actual, A_Gen_T);
13293 else
13294 declare
13295 Act_Iface_List : Elist_Id;
13296 Iface : Node_Id;
13297 Iface_Ent : Entity_Id;
13299 function Instance_Exists (I : Entity_Id) return Boolean;
13300 -- If the interface entity is declared in a generic unit,
13301 -- this can only be legal if we are within an instantiation
13302 -- of a child of that generic. There is currently no
13303 -- mechanism to relate an interface declared within a
13304 -- generic to the corresponding interface in an instance,
13305 -- so we traverse the list of interfaces of the actual,
13306 -- looking for a name match.
13308 ---------------------
13309 -- Instance_Exists --
13310 ---------------------
13312 function Instance_Exists (I : Entity_Id) return Boolean is
13313 Iface_Elmt : Elmt_Id;
13315 begin
13316 Iface_Elmt := First_Elmt (Act_Iface_List);
13317 while Present (Iface_Elmt) loop
13318 if Is_Generic_Instance (Scope (Node (Iface_Elmt)))
13319 and then Chars (Node (Iface_Elmt)) = Chars (I)
13320 then
13321 return True;
13322 end if;
13324 Next_Elmt (Iface_Elmt);
13325 end loop;
13327 return False;
13328 end Instance_Exists;
13330 begin
13331 Iface := First (Abstract_Interface_List (A_Gen_T));
13332 Collect_Interfaces (Act_T, Act_Iface_List);
13334 while Present (Iface) loop
13335 Iface_Ent := Get_Instance_Of (Entity (Iface));
13337 if Is_Ancestor (Iface_Ent, Act_T)
13338 or else Is_Progenitor (Iface_Ent, Act_T)
13339 then
13340 null;
13342 elsif Ekind (Scope (Iface_Ent)) = E_Generic_Package
13343 and then Instance_Exists (Iface_Ent)
13344 then
13345 null;
13347 else
13348 Error_Msg_Name_1 := Chars (Act_T);
13349 Error_Msg_NE
13350 ("actual% must implement interface&",
13351 Actual, Etype (Iface));
13352 end if;
13354 Next (Iface);
13355 end loop;
13356 end;
13357 end if;
13358 end if;
13360 -- If the parent type in the generic declaration is itself a previous
13361 -- formal type, then it is local to the generic and absent from the
13362 -- analyzed generic definition. In that case the ancestor is the
13363 -- instance of the formal (which must have been instantiated
13364 -- previously), unless the ancestor is itself a formal derived type.
13365 -- In this latter case (which is the subject of Corrigendum 8652/0038
13366 -- (AI-202) the ancestor of the formals is the ancestor of its
13367 -- parent. Otherwise, the analyzed generic carries the parent type.
13368 -- If the parent type is defined in a previous formal package, then
13369 -- the scope of that formal package is that of the generic type
13370 -- itself, and it has already been mapped into the corresponding type
13371 -- in the actual package.
13373 -- Common case: parent type defined outside of the generic
13375 if Is_Entity_Name (Subtype_Mark (Def))
13376 and then Present (Entity (Subtype_Mark (Def)))
13377 then
13378 Ancestor := Get_Instance_Of (Entity (Subtype_Mark (Def)));
13380 -- Check whether parent is defined in a previous formal package
13382 elsif
13383 Scope (Scope (Base_Type (Etype (A_Gen_T)))) = Scope (A_Gen_T)
13384 then
13385 Ancestor :=
13386 Get_Instance_Of (Base_Type (Etype (A_Gen_T)));
13388 -- The type may be a local derivation, or a type extension of a
13389 -- previous formal, or of a formal of a parent package.
13391 elsif Is_Derived_Type (Get_Instance_Of (A_Gen_T))
13392 or else
13393 Ekind (Get_Instance_Of (A_Gen_T)) = E_Record_Type_With_Private
13394 then
13395 -- Check whether the parent is another derived formal type in the
13396 -- same generic unit.
13398 if Etype (A_Gen_T) /= A_Gen_T
13399 and then Is_Generic_Type (Etype (A_Gen_T))
13400 and then Scope (Etype (A_Gen_T)) = Scope (A_Gen_T)
13401 and then Etype (Etype (A_Gen_T)) /= Etype (A_Gen_T)
13402 then
13403 -- Locate ancestor of parent from the subtype declaration
13404 -- created for the actual.
13406 declare
13407 Decl : Node_Id;
13409 begin
13410 Decl := First (Actual_Decls);
13411 while Present (Decl) loop
13412 if Nkind (Decl) = N_Subtype_Declaration
13413 and then Chars (Defining_Identifier (Decl)) =
13414 Chars (Etype (A_Gen_T))
13415 then
13416 Ancestor := Generic_Parent_Type (Decl);
13417 exit;
13418 else
13419 Next (Decl);
13420 end if;
13421 end loop;
13422 end;
13424 pragma Assert (Present (Ancestor));
13426 -- The ancestor itself may be a previous formal that has been
13427 -- instantiated.
13429 Ancestor := Get_Instance_Of (Ancestor);
13431 else
13432 Ancestor :=
13433 Get_Instance_Of (Base_Type (Get_Instance_Of (A_Gen_T)));
13434 end if;
13436 -- Check whether parent is a previous formal of the current generic
13438 elsif Is_Derived_Type (A_Gen_T)
13439 and then Is_Generic_Type (Etype (A_Gen_T))
13440 and then Scope (A_Gen_T) = Scope (Etype (A_Gen_T))
13441 then
13442 Ancestor := Get_Instance_Of (First_Subtype (Etype (A_Gen_T)));
13444 -- An unusual case: the actual is a type declared in a parent unit,
13445 -- but is not a formal type so there is no instance_of for it.
13446 -- Retrieve it by analyzing the record extension.
13448 elsif Is_Child_Unit (Scope (A_Gen_T))
13449 and then In_Open_Scopes (Scope (Act_T))
13450 and then Is_Generic_Instance (Scope (Act_T))
13451 then
13452 Analyze (Subtype_Mark (Def));
13453 Ancestor := Entity (Subtype_Mark (Def));
13455 else
13456 Ancestor := Get_Instance_Of (Etype (Base_Type (A_Gen_T)));
13457 end if;
13459 -- If the formal derived type has pragma Preelaborable_Initialization
13460 -- then the actual type must have preelaborable initialization.
13462 if Known_To_Have_Preelab_Init (A_Gen_T)
13463 and then not Has_Preelaborable_Initialization (Act_T)
13464 then
13465 Error_Msg_NE
13466 ("actual for & must have preelaborable initialization",
13467 Actual, Gen_T);
13468 end if;
13470 -- Ada 2005 (AI-251)
13472 if Ada_Version >= Ada_2005 and then Is_Interface (Ancestor) then
13473 if not Interface_Present_In_Ancestor (Act_T, Ancestor) then
13474 Error_Msg_NE
13475 ("(Ada 2005) expected type implementing & in instantiation",
13476 Actual, Ancestor);
13477 end if;
13479 -- Finally verify that the (instance of) the ancestor is an ancestor
13480 -- of the actual.
13482 elsif not Is_Ancestor (Base_Type (Ancestor), Act_T) then
13483 Error_Msg_NE
13484 ("expect type derived from & in instantiation",
13485 Actual, First_Subtype (Ancestor));
13486 Abandon_Instantiation (Actual);
13487 end if;
13489 -- Ada 2005 (AI-443): Synchronized formal derived type checks. Note
13490 -- that the formal type declaration has been rewritten as a private
13491 -- extension.
13493 if Ada_Version >= Ada_2005
13494 and then Nkind (Parent (A_Gen_T)) = N_Private_Extension_Declaration
13495 and then Synchronized_Present (Parent (A_Gen_T))
13496 then
13497 -- The actual must be a synchronized tagged type
13499 if not Is_Tagged_Type (Act_T) then
13500 Error_Msg_N
13501 ("actual of synchronized type must be tagged", Actual);
13502 Abandon_Instantiation (Actual);
13504 elsif Nkind (Parent (Act_T)) = N_Full_Type_Declaration
13505 and then Nkind (Type_Definition (Parent (Act_T))) =
13506 N_Derived_Type_Definition
13507 and then not Synchronized_Present
13508 (Type_Definition (Parent (Act_T)))
13509 then
13510 Error_Msg_N
13511 ("actual of synchronized type must be synchronized", Actual);
13512 Abandon_Instantiation (Actual);
13513 end if;
13514 end if;
13516 -- Perform atomic/volatile checks (RM C.6(12)). Note that AI05-0218-1
13517 -- removes the second instance of the phrase "or allow pass by copy".
13519 -- For Ada 2022, the aspect may be specified explicitly for the
13520 -- formal regardless of whether an ancestor obeys it.
13522 if Is_Atomic (Act_T)
13523 and then not Is_Atomic (Ancestor)
13524 and then not Is_Atomic (A_Gen_T)
13525 then
13526 Error_Msg_N
13527 ("cannot have atomic actual type for non-atomic formal type",
13528 Actual);
13530 elsif Is_Volatile (Act_T)
13531 and then not Is_Volatile (Ancestor)
13532 and then not Is_Volatile (A_Gen_T)
13533 then
13534 Error_Msg_N
13535 ("cannot have volatile actual type for non-volatile formal type",
13536 Actual);
13537 end if;
13539 -- It should not be necessary to check for unknown discriminants on
13540 -- Formal, but for some reason Has_Unknown_Discriminants is false for
13541 -- A_Gen_T, so Is_Definite_Subtype incorrectly returns True. This
13542 -- needs fixing. ???
13544 if Is_Definite_Subtype (A_Gen_T)
13545 and then not Unknown_Discriminants_Present (Formal)
13546 and then not Is_Definite_Subtype (Act_T)
13547 then
13548 Error_Msg_N ("actual subtype must be constrained", Actual);
13549 Abandon_Instantiation (Actual);
13550 end if;
13552 if not Unknown_Discriminants_Present (Formal) then
13553 if Is_Constrained (Ancestor) then
13554 if not Is_Constrained (Act_T) then
13555 Error_Msg_N ("actual subtype must be constrained", Actual);
13556 Abandon_Instantiation (Actual);
13557 end if;
13559 -- Ancestor is unconstrained, Check if generic formal and actual
13560 -- agree on constrainedness. The check only applies to array types
13561 -- and discriminated types.
13563 elsif Is_Constrained (Act_T) then
13564 if Ekind (Ancestor) = E_Access_Type
13565 or else (not Is_Constrained (A_Gen_T)
13566 and then Is_Composite_Type (A_Gen_T))
13567 then
13568 Error_Msg_N ("actual subtype must be unconstrained", Actual);
13569 Abandon_Instantiation (Actual);
13570 end if;
13572 -- A class-wide type is only allowed if the formal has unknown
13573 -- discriminants.
13575 elsif Is_Class_Wide_Type (Act_T)
13576 and then not Has_Unknown_Discriminants (Ancestor)
13577 then
13578 Error_Msg_NE
13579 ("actual for & cannot be a class-wide type", Actual, Gen_T);
13580 Abandon_Instantiation (Actual);
13582 -- Otherwise, the formal and actual must have the same number
13583 -- of discriminants and each discriminant of the actual must
13584 -- correspond to a discriminant of the formal.
13586 elsif Has_Discriminants (Act_T)
13587 and then not Has_Unknown_Discriminants (Act_T)
13588 and then Has_Discriminants (Ancestor)
13589 then
13590 Actual_Discr := First_Discriminant (Act_T);
13591 Ancestor_Discr := First_Discriminant (Ancestor);
13592 while Present (Actual_Discr)
13593 and then Present (Ancestor_Discr)
13594 loop
13595 if Base_Type (Act_T) /= Base_Type (Ancestor) and then
13596 No (Corresponding_Discriminant (Actual_Discr))
13597 then
13598 Error_Msg_NE
13599 ("discriminant & does not correspond "
13600 & "to ancestor discriminant", Actual, Actual_Discr);
13601 Abandon_Instantiation (Actual);
13602 end if;
13604 Next_Discriminant (Actual_Discr);
13605 Next_Discriminant (Ancestor_Discr);
13606 end loop;
13608 if Present (Actual_Discr) or else Present (Ancestor_Discr) then
13609 Error_Msg_NE
13610 ("actual for & must have same number of discriminants",
13611 Actual, Gen_T);
13612 Abandon_Instantiation (Actual);
13613 end if;
13615 -- This case should be caught by the earlier check for
13616 -- constrainedness, but the check here is added for completeness.
13618 elsif Has_Discriminants (Act_T)
13619 and then not Has_Unknown_Discriminants (Act_T)
13620 then
13621 Error_Msg_NE
13622 ("actual for & must not have discriminants", Actual, Gen_T);
13623 Abandon_Instantiation (Actual);
13625 elsif Has_Discriminants (Ancestor) then
13626 Error_Msg_NE
13627 ("actual for & must have known discriminants", Actual, Gen_T);
13628 Abandon_Instantiation (Actual);
13629 end if;
13631 if not Subtypes_Statically_Compatible
13632 (Act_T, Ancestor, Formal_Derived_Matching => True)
13633 then
13634 Error_Msg_NE
13635 ("actual for & must be statically compatible with ancestor",
13636 Actual, Gen_T);
13638 if not Predicates_Compatible (Act_T, Ancestor) then
13639 Error_Msg_N
13640 ("\predicate on actual is not compatible with ancestor",
13641 Actual);
13642 end if;
13644 Abandon_Instantiation (Actual);
13645 end if;
13646 end if;
13648 -- If the formal and actual types are abstract, check that there
13649 -- are no abstract primitives of the actual type that correspond to
13650 -- nonabstract primitives of the formal type (second sentence of
13651 -- RM95 3.9.3(9)).
13653 if Is_Abstract_Type (A_Gen_T) and then Is_Abstract_Type (Act_T) then
13654 Check_Abstract_Primitives : declare
13655 Gen_Prims : constant Elist_Id :=
13656 Primitive_Operations (A_Gen_T);
13657 Gen_Elmt : Elmt_Id;
13658 Gen_Subp : Entity_Id;
13659 Anc_Subp : Entity_Id;
13660 Anc_Formal : Entity_Id;
13661 Anc_F_Type : Entity_Id;
13663 Act_Prims : constant Elist_Id := Primitive_Operations (Act_T);
13664 Act_Elmt : Elmt_Id;
13665 Act_Subp : Entity_Id;
13666 Act_Formal : Entity_Id;
13667 Act_F_Type : Entity_Id;
13669 Subprograms_Correspond : Boolean;
13671 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean;
13672 -- Returns true if T2 is derived directly or indirectly from
13673 -- T1, including derivations from interfaces. T1 and T2 are
13674 -- required to be specific tagged base types.
13676 ------------------------
13677 -- Is_Tagged_Ancestor --
13678 ------------------------
13680 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean
13682 Intfc_Elmt : Elmt_Id;
13684 begin
13685 -- The predicate is satisfied if the types are the same
13687 if T1 = T2 then
13688 return True;
13690 -- If we've reached the top of the derivation chain then
13691 -- we know that T1 is not an ancestor of T2.
13693 elsif Etype (T2) = T2 then
13694 return False;
13696 -- Proceed to check T2's immediate parent
13698 elsif Is_Ancestor (T1, Base_Type (Etype (T2))) then
13699 return True;
13701 -- Finally, check to see if T1 is an ancestor of any of T2's
13702 -- progenitors.
13704 else
13705 Intfc_Elmt := First_Elmt (Interfaces (T2));
13706 while Present (Intfc_Elmt) loop
13707 if Is_Ancestor (T1, Node (Intfc_Elmt)) then
13708 return True;
13709 end if;
13711 Next_Elmt (Intfc_Elmt);
13712 end loop;
13713 end if;
13715 return False;
13716 end Is_Tagged_Ancestor;
13718 -- Start of processing for Check_Abstract_Primitives
13720 begin
13721 -- Loop over all of the formal derived type's primitives
13723 Gen_Elmt := First_Elmt (Gen_Prims);
13724 while Present (Gen_Elmt) loop
13725 Gen_Subp := Node (Gen_Elmt);
13727 -- If the primitive of the formal is not abstract, then
13728 -- determine whether there is a corresponding primitive of
13729 -- the actual type that's abstract.
13731 if not Is_Abstract_Subprogram (Gen_Subp) then
13732 Act_Elmt := First_Elmt (Act_Prims);
13733 while Present (Act_Elmt) loop
13734 Act_Subp := Node (Act_Elmt);
13736 -- If we find an abstract primitive of the actual,
13737 -- then we need to test whether it corresponds to the
13738 -- subprogram from which the generic formal primitive
13739 -- is inherited.
13741 if Is_Abstract_Subprogram (Act_Subp) then
13742 Anc_Subp := Alias (Gen_Subp);
13744 -- Test whether we have a corresponding primitive
13745 -- by comparing names, kinds, formal types, and
13746 -- result types.
13748 if Chars (Anc_Subp) = Chars (Act_Subp)
13749 and then Ekind (Anc_Subp) = Ekind (Act_Subp)
13750 then
13751 Anc_Formal := First_Formal (Anc_Subp);
13752 Act_Formal := First_Formal (Act_Subp);
13753 while Present (Anc_Formal)
13754 and then Present (Act_Formal)
13755 loop
13756 Anc_F_Type := Etype (Anc_Formal);
13757 Act_F_Type := Etype (Act_Formal);
13759 if Ekind (Anc_F_Type) =
13760 E_Anonymous_Access_Type
13761 then
13762 Anc_F_Type := Designated_Type (Anc_F_Type);
13764 if Ekind (Act_F_Type) =
13765 E_Anonymous_Access_Type
13766 then
13767 Act_F_Type :=
13768 Designated_Type (Act_F_Type);
13769 else
13770 exit;
13771 end if;
13773 elsif
13774 Ekind (Act_F_Type) = E_Anonymous_Access_Type
13775 then
13776 exit;
13777 end if;
13779 Anc_F_Type := Base_Type (Anc_F_Type);
13780 Act_F_Type := Base_Type (Act_F_Type);
13782 -- If the formal is controlling, then the
13783 -- the type of the actual primitive's formal
13784 -- must be derived directly or indirectly
13785 -- from the type of the ancestor primitive's
13786 -- formal.
13788 if Is_Controlling_Formal (Anc_Formal) then
13789 if not Is_Tagged_Ancestor
13790 (Anc_F_Type, Act_F_Type)
13791 then
13792 exit;
13793 end if;
13795 -- Otherwise the types of the formals must
13796 -- be the same.
13798 elsif Anc_F_Type /= Act_F_Type then
13799 exit;
13800 end if;
13802 Next_Formal (Anc_Formal);
13803 Next_Formal (Act_Formal);
13804 end loop;
13806 -- If we traversed through all of the formals
13807 -- then so far the subprograms correspond, so
13808 -- now check that any result types correspond.
13810 if No (Anc_Formal) and then No (Act_Formal) then
13811 Subprograms_Correspond := True;
13813 if Ekind (Act_Subp) = E_Function then
13814 Anc_F_Type := Etype (Anc_Subp);
13815 Act_F_Type := Etype (Act_Subp);
13817 if Ekind (Anc_F_Type) =
13818 E_Anonymous_Access_Type
13819 then
13820 Anc_F_Type :=
13821 Designated_Type (Anc_F_Type);
13823 if Ekind (Act_F_Type) =
13824 E_Anonymous_Access_Type
13825 then
13826 Act_F_Type :=
13827 Designated_Type (Act_F_Type);
13828 else
13829 Subprograms_Correspond := False;
13830 end if;
13832 elsif
13833 Ekind (Act_F_Type)
13834 = E_Anonymous_Access_Type
13835 then
13836 Subprograms_Correspond := False;
13837 end if;
13839 Anc_F_Type := Base_Type (Anc_F_Type);
13840 Act_F_Type := Base_Type (Act_F_Type);
13842 -- Now either the result types must be
13843 -- the same or, if the result type is
13844 -- controlling, the result type of the
13845 -- actual primitive must descend from the
13846 -- result type of the ancestor primitive.
13848 if Subprograms_Correspond
13849 and then Anc_F_Type /= Act_F_Type
13850 and then
13851 Has_Controlling_Result (Anc_Subp)
13852 and then not Is_Tagged_Ancestor
13853 (Anc_F_Type, Act_F_Type)
13854 then
13855 Subprograms_Correspond := False;
13856 end if;
13857 end if;
13859 -- Found a matching subprogram belonging to
13860 -- formal ancestor type, so actual subprogram
13861 -- corresponds and this violates 3.9.3(9).
13863 if Subprograms_Correspond then
13864 Error_Msg_NE
13865 ("abstract subprogram & overrides "
13866 & "nonabstract subprogram of ancestor",
13867 Actual, Act_Subp);
13868 end if;
13869 end if;
13870 end if;
13871 end if;
13873 Next_Elmt (Act_Elmt);
13874 end loop;
13875 end if;
13877 Next_Elmt (Gen_Elmt);
13878 end loop;
13879 end Check_Abstract_Primitives;
13880 end if;
13882 -- Verify that limitedness matches. If parent is a limited
13883 -- interface then the generic formal is not unless declared
13884 -- explicitly so. If not declared limited, the actual cannot be
13885 -- limited (see AI05-0087).
13887 if Is_Limited_Type (Act_T) and then not Is_Limited_Type (A_Gen_T) then
13888 if not In_Instance then
13889 Error_Msg_NE
13890 ("actual for non-limited & cannot be a limited type",
13891 Actual, Gen_T);
13892 Explain_Limited_Type (Act_T, Actual);
13893 Abandon_Instantiation (Actual);
13894 end if;
13895 end if;
13897 -- Check for AI12-0036
13899 declare
13900 Formal_Is_Private_Extension : constant Boolean :=
13901 Nkind (Parent (A_Gen_T)) = N_Private_Extension_Declaration;
13903 Actual_Is_Tagged : constant Boolean := Is_Tagged_Type (Act_T);
13905 begin
13906 if Actual_Is_Tagged /= Formal_Is_Private_Extension then
13907 if not In_Instance then
13908 if Actual_Is_Tagged then
13909 Error_Msg_NE
13910 ("actual for & cannot be a tagged type", Actual, Gen_T);
13911 else
13912 Error_Msg_NE
13913 ("actual for & must be a tagged type", Actual, Gen_T);
13914 end if;
13916 Abandon_Instantiation (Actual);
13917 end if;
13918 end if;
13919 end;
13920 end Validate_Derived_Type_Instance;
13922 ----------------------------------------
13923 -- Validate_Discriminated_Formal_Type --
13924 ----------------------------------------
13926 procedure Validate_Discriminated_Formal_Type is
13927 Formal_Discr : Entity_Id;
13928 Actual_Discr : Entity_Id;
13929 Formal_Subt : Entity_Id;
13931 begin
13932 if Has_Discriminants (A_Gen_T) then
13933 if not Has_Discriminants (Act_T) then
13934 Error_Msg_NE
13935 ("actual for & must have discriminants", Actual, Gen_T);
13936 Abandon_Instantiation (Actual);
13938 elsif Is_Constrained (Act_T) then
13939 Error_Msg_NE
13940 ("actual for & must be unconstrained", Actual, Gen_T);
13941 Abandon_Instantiation (Actual);
13943 else
13944 Formal_Discr := First_Discriminant (A_Gen_T);
13945 Actual_Discr := First_Discriminant (Act_T);
13946 while Formal_Discr /= Empty loop
13947 if Actual_Discr = Empty then
13948 Error_Msg_N
13949 ("discriminants on actual do not match formal",
13950 Actual);
13951 Abandon_Instantiation (Actual);
13952 end if;
13954 Formal_Subt := Get_Instance_Of (Etype (Formal_Discr));
13956 -- Access discriminants match if designated types do
13958 if Ekind (Base_Type (Formal_Subt)) = E_Anonymous_Access_Type
13959 and then (Ekind (Base_Type (Etype (Actual_Discr)))) =
13960 E_Anonymous_Access_Type
13961 and then
13962 Get_Instance_Of
13963 (Designated_Type (Base_Type (Formal_Subt))) =
13964 Designated_Type (Base_Type (Etype (Actual_Discr)))
13965 then
13966 null;
13968 elsif Base_Type (Formal_Subt) /=
13969 Base_Type (Etype (Actual_Discr))
13970 then
13971 Error_Msg_N
13972 ("types of actual discriminants must match formal",
13973 Actual);
13974 Abandon_Instantiation (Actual);
13976 elsif not Subtypes_Statically_Match
13977 (Formal_Subt, Etype (Actual_Discr))
13978 and then Ada_Version >= Ada_95
13979 then
13980 Error_Msg_N
13981 ("subtypes of actual discriminants must match formal",
13982 Actual);
13983 Abandon_Instantiation (Actual);
13984 end if;
13986 Next_Discriminant (Formal_Discr);
13987 Next_Discriminant (Actual_Discr);
13988 end loop;
13990 if Actual_Discr /= Empty then
13991 Error_Msg_NE
13992 ("discriminants on actual do not match formal",
13993 Actual, Gen_T);
13994 Abandon_Instantiation (Actual);
13995 end if;
13996 end if;
13997 end if;
13998 end Validate_Discriminated_Formal_Type;
14000 ---------------------------------------
14001 -- Validate_Incomplete_Type_Instance --
14002 ---------------------------------------
14004 procedure Validate_Incomplete_Type_Instance is
14005 begin
14006 if not Is_Tagged_Type (Act_T)
14007 and then Is_Tagged_Type (A_Gen_T)
14008 then
14009 Error_Msg_NE
14010 ("actual for & must be a tagged type", Actual, Gen_T);
14011 end if;
14013 Validate_Discriminated_Formal_Type;
14014 end Validate_Incomplete_Type_Instance;
14016 --------------------------------------
14017 -- Validate_Interface_Type_Instance --
14018 --------------------------------------
14020 procedure Validate_Interface_Type_Instance is
14021 begin
14022 if not Is_Interface (Act_T) then
14023 Error_Msg_NE
14024 ("actual for formal interface type must be an interface",
14025 Actual, Gen_T);
14027 elsif Is_Limited_Type (Act_T) /= Is_Limited_Type (A_Gen_T)
14028 or else Is_Task_Interface (A_Gen_T) /= Is_Task_Interface (Act_T)
14029 or else Is_Protected_Interface (A_Gen_T) /=
14030 Is_Protected_Interface (Act_T)
14031 or else Is_Synchronized_Interface (A_Gen_T) /=
14032 Is_Synchronized_Interface (Act_T)
14033 then
14034 Error_Msg_NE
14035 ("actual for interface& does not match (RM 12.5.5(4))",
14036 Actual, Gen_T);
14037 end if;
14038 end Validate_Interface_Type_Instance;
14040 ------------------------------------
14041 -- Validate_Private_Type_Instance --
14042 ------------------------------------
14044 procedure Validate_Private_Type_Instance is
14045 begin
14046 if Is_Limited_Type (Act_T)
14047 and then not Is_Limited_Type (A_Gen_T)
14048 then
14049 if In_Instance then
14050 null;
14051 else
14052 Error_Msg_NE
14053 ("actual for non-limited & cannot be a limited type", Actual,
14054 Gen_T);
14055 Explain_Limited_Type (Act_T, Actual);
14056 Abandon_Instantiation (Actual);
14057 end if;
14059 elsif Known_To_Have_Preelab_Init (A_Gen_T)
14060 and then not Has_Preelaborable_Initialization (Act_T)
14061 then
14062 Error_Msg_NE
14063 ("actual for & must have preelaborable initialization", Actual,
14064 Gen_T);
14066 elsif not Is_Definite_Subtype (Act_T)
14067 and then Is_Definite_Subtype (A_Gen_T)
14068 and then Ada_Version >= Ada_95
14069 then
14070 Error_Msg_NE
14071 ("actual for & must be a definite subtype", Actual, Gen_T);
14073 elsif not Is_Tagged_Type (Act_T)
14074 and then Is_Tagged_Type (A_Gen_T)
14075 then
14076 Error_Msg_NE
14077 ("actual for & must be a tagged type", Actual, Gen_T);
14078 end if;
14080 Validate_Discriminated_Formal_Type;
14081 Ancestor := Gen_T;
14082 end Validate_Private_Type_Instance;
14084 -- Start of processing for Instantiate_Type
14086 begin
14087 if Get_Instance_Of (A_Gen_T) /= A_Gen_T then
14088 Error_Msg_N ("duplicate instantiation of generic type", Actual);
14089 return New_List (Error);
14091 elsif not Is_Entity_Name (Actual)
14092 or else not Is_Type (Entity (Actual))
14093 then
14094 Error_Msg_NE
14095 ("expect valid subtype mark to instantiate &", Actual, Gen_T);
14096 Abandon_Instantiation (Actual);
14098 else
14099 Act_T := Entity (Actual);
14101 -- Ada 2005 (AI-216): An Unchecked_Union subtype shall only be passed
14102 -- as a generic actual parameter if the corresponding formal type
14103 -- does not have a known_discriminant_part, or is a formal derived
14104 -- type that is an Unchecked_Union type.
14106 if Is_Unchecked_Union (Base_Type (Act_T)) then
14107 if not Has_Discriminants (A_Gen_T)
14108 or else (Is_Derived_Type (A_Gen_T)
14109 and then Is_Unchecked_Union (A_Gen_T))
14110 then
14111 null;
14112 else
14113 Error_Msg_N ("unchecked union cannot be the actual for a "
14114 & "discriminated formal type", Act_T);
14116 end if;
14117 end if;
14119 -- Deal with fixed/floating restrictions
14121 if Is_Floating_Point_Type (Act_T) then
14122 Check_Restriction (No_Floating_Point, Actual);
14123 elsif Is_Fixed_Point_Type (Act_T) then
14124 Check_Restriction (No_Fixed_Point, Actual);
14125 end if;
14127 -- Deal with error of using incomplete type as generic actual.
14128 -- This includes limited views of a type, even if the non-limited
14129 -- view may be available.
14131 if Ekind (Act_T) = E_Incomplete_Type
14132 or else (Is_Class_Wide_Type (Act_T)
14133 and then Ekind (Root_Type (Act_T)) = E_Incomplete_Type)
14134 then
14135 -- If the formal is an incomplete type, the actual can be
14136 -- incomplete as well, but if an actual incomplete type has
14137 -- a full view, then we'll retrieve that.
14139 if Ekind (A_Gen_T) = E_Incomplete_Type
14140 and then No (Full_View (Act_T))
14141 then
14142 null;
14144 elsif Is_Class_Wide_Type (Act_T)
14145 or else No (Full_View (Act_T))
14146 then
14147 Error_Msg_N ("premature use of incomplete type", Actual);
14148 Abandon_Instantiation (Actual);
14150 else
14151 Act_T := Full_View (Act_T);
14152 Set_Entity (Actual, Act_T);
14154 if Has_Private_Component (Act_T) then
14155 Error_Msg_N
14156 ("premature use of type with private component", Actual);
14157 end if;
14158 end if;
14160 -- Deal with error of premature use of private type as generic actual
14162 elsif Is_Private_Type (Act_T)
14163 and then Is_Private_Type (Base_Type (Act_T))
14164 and then not Is_Generic_Type (Act_T)
14165 and then not Is_Derived_Type (Act_T)
14166 and then No (Full_View (Root_Type (Act_T)))
14167 then
14168 -- If the formal is an incomplete type, the actual can be
14169 -- private or incomplete as well.
14171 if Ekind (A_Gen_T) = E_Incomplete_Type then
14172 null;
14173 else
14174 Error_Msg_N ("premature use of private type", Actual);
14175 end if;
14177 elsif Has_Private_Component (Act_T) then
14178 Error_Msg_N
14179 ("premature use of type with private component", Actual);
14180 end if;
14182 Set_Instance_Of (A_Gen_T, Act_T);
14184 -- If the type is generic, the class-wide type may also be used
14186 if Is_Tagged_Type (A_Gen_T)
14187 and then Is_Tagged_Type (Act_T)
14188 and then not Is_Class_Wide_Type (A_Gen_T)
14189 then
14190 Set_Instance_Of (Class_Wide_Type (A_Gen_T),
14191 Class_Wide_Type (Act_T));
14192 end if;
14194 if not Is_Abstract_Type (A_Gen_T)
14195 and then Is_Abstract_Type (Act_T)
14196 then
14197 Error_Msg_N
14198 ("actual of non-abstract formal cannot be abstract", Actual);
14199 end if;
14201 -- A generic scalar type is a first subtype for which we generate
14202 -- an anonymous base type. Indicate that the instance of this base
14203 -- is the base type of the actual.
14205 if Is_Scalar_Type (A_Gen_T) then
14206 Set_Instance_Of (Etype (A_Gen_T), Etype (Act_T));
14207 end if;
14208 end if;
14210 Check_Shared_Variable_Control_Aspects;
14212 if Error_Posted (Act_T) then
14213 null;
14214 else
14215 case Nkind (Def) is
14216 when N_Formal_Private_Type_Definition =>
14217 Validate_Private_Type_Instance;
14219 when N_Formal_Incomplete_Type_Definition =>
14220 Validate_Incomplete_Type_Instance;
14222 when N_Formal_Derived_Type_Definition =>
14223 Validate_Derived_Type_Instance;
14225 when N_Formal_Discrete_Type_Definition =>
14226 if not Is_Discrete_Type (Act_T) then
14227 Error_Msg_NE
14228 ("expect discrete type in instantiation of&",
14229 Actual, Gen_T);
14230 Abandon_Instantiation (Actual);
14231 end if;
14233 Diagnose_Predicated_Actual;
14235 when N_Formal_Signed_Integer_Type_Definition =>
14236 if not Is_Signed_Integer_Type (Act_T) then
14237 Error_Msg_NE
14238 ("expect signed integer type in instantiation of&",
14239 Actual, Gen_T);
14240 Abandon_Instantiation (Actual);
14241 end if;
14243 Diagnose_Predicated_Actual;
14245 when N_Formal_Modular_Type_Definition =>
14246 if not Is_Modular_Integer_Type (Act_T) then
14247 Error_Msg_NE
14248 ("expect modular type in instantiation of &",
14249 Actual, Gen_T);
14250 Abandon_Instantiation (Actual);
14251 end if;
14253 Diagnose_Predicated_Actual;
14255 when N_Formal_Floating_Point_Definition =>
14256 if not Is_Floating_Point_Type (Act_T) then
14257 Error_Msg_NE
14258 ("expect float type in instantiation of &", Actual, Gen_T);
14259 Abandon_Instantiation (Actual);
14260 end if;
14262 when N_Formal_Ordinary_Fixed_Point_Definition =>
14263 if not Is_Ordinary_Fixed_Point_Type (Act_T) then
14264 Error_Msg_NE
14265 ("expect ordinary fixed point type in instantiation of &",
14266 Actual, Gen_T);
14267 Abandon_Instantiation (Actual);
14268 end if;
14270 when N_Formal_Decimal_Fixed_Point_Definition =>
14271 if not Is_Decimal_Fixed_Point_Type (Act_T) then
14272 Error_Msg_NE
14273 ("expect decimal type in instantiation of &",
14274 Actual, Gen_T);
14275 Abandon_Instantiation (Actual);
14276 end if;
14278 when N_Array_Type_Definition =>
14279 Validate_Array_Type_Instance;
14281 when N_Access_To_Object_Definition =>
14282 Validate_Access_Type_Instance;
14284 when N_Access_Function_Definition
14285 | N_Access_Procedure_Definition
14287 Validate_Access_Subprogram_Instance;
14289 when N_Record_Definition =>
14290 Validate_Interface_Type_Instance;
14292 when N_Derived_Type_Definition =>
14293 Validate_Derived_Interface_Type_Instance;
14295 when others =>
14296 raise Program_Error;
14297 end case;
14298 end if;
14300 Subt := New_Copy (Gen_T);
14302 -- Use adjusted sloc of subtype name as the location for other nodes in
14303 -- the subtype declaration.
14305 Loc := Sloc (Subt);
14307 Decl_Node :=
14308 Make_Subtype_Declaration (Loc,
14309 Defining_Identifier => Subt,
14310 Subtype_Indication => New_Occurrence_Of (Act_T, Loc));
14312 Copy_Ghost_Aspect (Formal, To => Decl_Node);
14314 -- Record whether the actual is private at this point, so that
14315 -- Check_Generic_Actuals can restore its proper view before the
14316 -- semantic analysis of the instance.
14318 if Is_Private_Type (Act_T) then
14319 Set_Has_Private_View (Subtype_Indication (Decl_Node));
14320 end if;
14322 -- In Ada 2012 the actual may be a limited view. Indicate that
14323 -- the local subtype must be treated as such.
14325 if From_Limited_With (Act_T) then
14326 Mutate_Ekind (Subt, E_Incomplete_Subtype);
14327 Set_From_Limited_With (Subt);
14328 end if;
14330 Decl_Nodes := New_List (Decl_Node);
14332 -- Flag actual derived types so their elaboration produces the
14333 -- appropriate renamings for the primitive operations of the ancestor.
14334 -- Flag actual for formal private types as well, to determine whether
14335 -- operations in the private part may override inherited operations.
14336 -- If the formal has an interface list, the ancestor is not the
14337 -- parent, but the analyzed formal that includes the interface
14338 -- operations of all its progenitors.
14340 -- Same treatment for formal private types, so we can check whether the
14341 -- type is tagged limited when validating derivations in the private
14342 -- part. (See AI05-096).
14344 if Nkind (Def) = N_Formal_Derived_Type_Definition then
14345 if Present (Interface_List (Def)) then
14346 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
14347 else
14348 Set_Generic_Parent_Type (Decl_Node, Ancestor);
14349 end if;
14351 elsif Nkind (Def) in N_Formal_Private_Type_Definition
14352 | N_Formal_Incomplete_Type_Definition
14353 then
14354 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
14355 end if;
14357 -- If the actual is a synchronized type that implements an interface,
14358 -- the primitive operations are attached to the corresponding record,
14359 -- and we have to treat it as an additional generic actual, so that its
14360 -- primitive operations become visible in the instance. The task or
14361 -- protected type itself does not carry primitive operations.
14363 if Is_Concurrent_Type (Act_T)
14364 and then Is_Tagged_Type (Act_T)
14365 and then Present (Corresponding_Record_Type (Act_T))
14366 and then Present (Ancestor)
14367 and then Is_Interface (Ancestor)
14368 then
14369 declare
14370 Corr_Rec : constant Entity_Id :=
14371 Corresponding_Record_Type (Act_T);
14372 New_Corr : Entity_Id;
14373 Corr_Decl : Node_Id;
14375 begin
14376 New_Corr := Make_Temporary (Loc, 'S');
14377 Corr_Decl :=
14378 Make_Subtype_Declaration (Loc,
14379 Defining_Identifier => New_Corr,
14380 Subtype_Indication =>
14381 New_Occurrence_Of (Corr_Rec, Loc));
14382 Append_To (Decl_Nodes, Corr_Decl);
14384 if Ekind (Act_T) = E_Task_Type then
14385 Mutate_Ekind (Subt, E_Task_Subtype);
14386 else
14387 Mutate_Ekind (Subt, E_Protected_Subtype);
14388 end if;
14390 Set_Corresponding_Record_Type (Subt, Corr_Rec);
14391 Set_Generic_Parent_Type (Corr_Decl, Ancestor);
14392 Set_Generic_Parent_Type (Decl_Node, Empty);
14393 end;
14394 end if;
14396 -- For a floating-point type, capture dimension info if any, because
14397 -- the generated subtype declaration does not come from source and
14398 -- will not process dimensions.
14400 if Is_Floating_Point_Type (Act_T) then
14401 Copy_Dimensions (Act_T, Subt);
14402 end if;
14404 return Decl_Nodes;
14405 end Instantiate_Type;
14407 -----------------------------
14408 -- Is_Abbreviated_Instance --
14409 -----------------------------
14411 function Is_Abbreviated_Instance (E : Entity_Id) return Boolean is
14412 begin
14413 return Ekind (E) = E_Package
14414 and then Present (Hidden_In_Formal_Instance (E));
14415 end Is_Abbreviated_Instance;
14417 ---------------------
14418 -- Is_In_Main_Unit --
14419 ---------------------
14421 function Is_In_Main_Unit (N : Node_Id) return Boolean is
14422 Unum : constant Unit_Number_Type := Get_Source_Unit (N);
14423 Current_Unit : Node_Id;
14425 begin
14426 if Unum = Main_Unit then
14427 return True;
14429 -- If the current unit is a subunit then it is either the main unit or
14430 -- is being compiled as part of the main unit.
14432 elsif Nkind (N) = N_Compilation_Unit then
14433 return Nkind (Unit (N)) = N_Subunit;
14434 end if;
14436 Current_Unit := Parent (N);
14437 while Present (Current_Unit)
14438 and then Nkind (Current_Unit) /= N_Compilation_Unit
14439 loop
14440 Current_Unit := Parent (Current_Unit);
14441 end loop;
14443 -- The instantiation node is in the main unit, or else the current node
14444 -- (perhaps as the result of nested instantiations) is in the main unit,
14445 -- or in the declaration of the main unit, which in this last case must
14446 -- be a body.
14448 return
14449 Current_Unit = Cunit (Main_Unit)
14450 or else Current_Unit = Library_Unit (Cunit (Main_Unit))
14451 or else (Present (Current_Unit)
14452 and then Present (Library_Unit (Current_Unit))
14453 and then Is_In_Main_Unit (Library_Unit (Current_Unit)));
14454 end Is_In_Main_Unit;
14456 ----------------------------
14457 -- Load_Parent_Of_Generic --
14458 ----------------------------
14460 procedure Load_Parent_Of_Generic
14461 (N : Node_Id;
14462 Spec : Node_Id;
14463 Body_Optional : Boolean := False)
14465 Comp_Unit : constant Node_Id := Cunit (Get_Source_Unit (Spec));
14466 Saved_Style_Check : constant Boolean := Style_Check;
14467 Saved_Warn : constant Warnings_State := Save_Warnings;
14468 True_Parent : Node_Id;
14469 Inst_Node : Node_Id;
14470 OK : Boolean;
14471 Previous_Instances : constant Elist_Id := New_Elmt_List;
14473 procedure Collect_Previous_Instances (Decls : List_Id);
14474 -- Collect all instantiations in the given list of declarations, that
14475 -- precede the generic that we need to load. If the bodies of these
14476 -- instantiations are available, we must analyze them, to ensure that
14477 -- the public symbols generated are the same when the unit is compiled
14478 -- to generate code, and when it is compiled in the context of a unit
14479 -- that needs a particular nested instance. This process is applied to
14480 -- both package and subprogram instances.
14482 --------------------------------
14483 -- Collect_Previous_Instances --
14484 --------------------------------
14486 procedure Collect_Previous_Instances (Decls : List_Id) is
14487 Decl : Node_Id;
14489 begin
14490 Decl := First (Decls);
14491 while Present (Decl) loop
14492 if Sloc (Decl) >= Sloc (Inst_Node) then
14493 return;
14495 -- If Decl is an instantiation, then record it as requiring
14496 -- instantiation of the corresponding body, except if it is an
14497 -- abbreviated instantiation generated internally for conformance
14498 -- checking purposes only for the case of a formal package
14499 -- declared without a box (see Instantiate_Formal_Package). Such
14500 -- an instantiation does not generate any code (the actual code
14501 -- comes from actual) and thus does not need to be analyzed here.
14502 -- If the instantiation appears with a generic package body it is
14503 -- not analyzed here either.
14505 elsif Nkind (Decl) = N_Package_Instantiation
14506 and then not Is_Abbreviated_Instance (Defining_Entity (Decl))
14507 then
14508 Append_Elmt (Decl, Previous_Instances);
14510 -- For a subprogram instantiation, omit instantiations intrinsic
14511 -- operations (Unchecked_Conversions, etc.) that have no bodies.
14513 elsif Nkind (Decl) in N_Function_Instantiation
14514 | N_Procedure_Instantiation
14515 and then not Is_Intrinsic_Subprogram (Entity (Name (Decl)))
14516 then
14517 Append_Elmt (Decl, Previous_Instances);
14519 elsif Nkind (Decl) = N_Package_Declaration then
14520 Collect_Previous_Instances
14521 (Visible_Declarations (Specification (Decl)));
14522 Collect_Previous_Instances
14523 (Private_Declarations (Specification (Decl)));
14525 -- Previous non-generic bodies may contain instances as well
14527 elsif Nkind (Decl) = N_Package_Body
14528 and then Ekind (Corresponding_Spec (Decl)) /= E_Generic_Package
14529 then
14530 Collect_Previous_Instances (Declarations (Decl));
14532 elsif Nkind (Decl) = N_Subprogram_Body
14533 and then not Acts_As_Spec (Decl)
14534 and then not Is_Generic_Subprogram (Corresponding_Spec (Decl))
14535 then
14536 Collect_Previous_Instances (Declarations (Decl));
14537 end if;
14539 Next (Decl);
14540 end loop;
14541 end Collect_Previous_Instances;
14543 -- Start of processing for Load_Parent_Of_Generic
14545 begin
14546 if not In_Same_Source_Unit (N, Spec)
14547 or else Nkind (Unit (Comp_Unit)) = N_Package_Declaration
14548 or else (Nkind (Unit (Comp_Unit)) = N_Package_Body
14549 and then not Is_In_Main_Unit (Spec))
14550 then
14551 -- Find body of parent of spec, and analyze it. A special case arises
14552 -- when the parent is an instantiation, that is to say when we are
14553 -- currently instantiating a nested generic. In that case, there is
14554 -- no separate file for the body of the enclosing instance. Instead,
14555 -- the enclosing body must be instantiated as if it were a pending
14556 -- instantiation, in order to produce the body for the nested generic
14557 -- we require now. Note that in that case the generic may be defined
14558 -- in a package body, the instance defined in the same package body,
14559 -- and the original enclosing body may not be in the main unit.
14561 Inst_Node := Empty;
14563 True_Parent := Parent (Spec);
14564 while Present (True_Parent)
14565 and then Nkind (True_Parent) /= N_Compilation_Unit
14566 loop
14567 if Nkind (True_Parent) = N_Package_Declaration
14568 and then
14569 Nkind (Original_Node (True_Parent)) = N_Package_Instantiation
14570 then
14571 -- Parent is a compilation unit that is an instantiation, and
14572 -- instantiation node has been replaced with package decl.
14574 Inst_Node := Original_Node (True_Parent);
14575 exit;
14577 elsif Nkind (True_Parent) = N_Package_Declaration
14578 and then Nkind (Parent (True_Parent)) = N_Compilation_Unit
14579 and then
14580 Nkind (Unit (Parent (True_Parent))) = N_Package_Instantiation
14581 then
14582 -- Parent is a compilation unit that is an instantiation, but
14583 -- instantiation node has not been replaced with package decl.
14585 Inst_Node := Unit (Parent (True_Parent));
14586 exit;
14588 elsif Nkind (True_Parent) = N_Package_Declaration
14589 and then Nkind (Parent (True_Parent)) /= N_Compilation_Unit
14590 and then Present (Generic_Parent (Specification (True_Parent)))
14591 then
14592 -- Parent is an instantiation within another specification.
14593 -- Declaration for instance has been inserted before original
14594 -- instantiation node. A direct link would be preferable?
14596 Inst_Node := Next (True_Parent);
14597 while Present (Inst_Node)
14598 and then Nkind (Inst_Node) /= N_Package_Instantiation
14599 loop
14600 Next (Inst_Node);
14601 end loop;
14603 -- If the instance appears within a generic, and the generic
14604 -- unit is defined within a formal package of the enclosing
14605 -- generic, there is no generic body available, and none
14606 -- needed. A more precise test should be used ???
14608 if No (Inst_Node) then
14609 return;
14610 end if;
14612 exit;
14614 -- If an ancestor of the generic comes from a formal package
14615 -- there is no source for the ancestor body. This is detected
14616 -- by examining the scope of the ancestor and its declaration.
14617 -- The body, if any is needed, will be available when the
14618 -- current unit (containing a formal package) is instantiated.
14620 elsif Nkind (True_Parent) = N_Package_Specification
14621 and then Present (Generic_Parent (True_Parent))
14622 and then Nkind
14623 (Original_Node (Unit_Declaration_Node
14624 (Scope (Generic_Parent (True_Parent)))))
14625 = N_Formal_Package_Declaration
14626 then
14627 return;
14629 else
14630 True_Parent := Parent (True_Parent);
14631 end if;
14632 end loop;
14634 -- Case where we are currently instantiating a nested generic
14636 if Present (Inst_Node) then
14637 if Nkind (Parent (True_Parent)) = N_Compilation_Unit then
14639 -- Instantiation node and declaration of instantiated package
14640 -- were exchanged when only the declaration was needed.
14641 -- Restore instantiation node before proceeding with body.
14643 Set_Unit (Parent (True_Parent), Inst_Node);
14644 end if;
14646 -- Now complete instantiation of enclosing body, if it appears in
14647 -- some other unit. If it appears in the current unit, the body
14648 -- will have been instantiated already.
14650 if No (Corresponding_Body (Instance_Spec (Inst_Node))) then
14652 -- We need to determine the expander mode to instantiate the
14653 -- enclosing body. Because the generic body we need may use
14654 -- global entities declared in the enclosing package (including
14655 -- aggregates) it is in general necessary to compile this body
14656 -- with expansion enabled, except if we are within a generic
14657 -- package, in which case the usual generic rule applies.
14659 declare
14660 Exp_Status : Boolean := True;
14661 Scop : Entity_Id;
14663 begin
14664 -- Loop through scopes looking for generic package
14666 Scop := Scope (Defining_Entity (Instance_Spec (Inst_Node)));
14667 while Present (Scop)
14668 and then Scop /= Standard_Standard
14669 loop
14670 if Ekind (Scop) = E_Generic_Package then
14671 Exp_Status := False;
14672 exit;
14673 end if;
14675 Scop := Scope (Scop);
14676 end loop;
14678 -- Collect previous instantiations in the unit that contains
14679 -- the desired generic.
14681 if Nkind (Parent (True_Parent)) /= N_Compilation_Unit
14682 and then not Body_Optional
14683 then
14684 declare
14685 Decl : Elmt_Id;
14686 Info : Pending_Body_Info;
14687 Par : Node_Id;
14689 begin
14690 Par := Parent (Inst_Node);
14691 while Present (Par) loop
14692 exit when Nkind (Parent (Par)) = N_Compilation_Unit;
14693 Par := Parent (Par);
14694 end loop;
14696 pragma Assert (Present (Par));
14698 if Nkind (Par) = N_Package_Body then
14699 Collect_Previous_Instances (Declarations (Par));
14701 elsif Nkind (Par) = N_Package_Declaration then
14702 Collect_Previous_Instances
14703 (Visible_Declarations (Specification (Par)));
14704 Collect_Previous_Instances
14705 (Private_Declarations (Specification (Par)));
14707 else
14708 -- Enclosing unit is a subprogram body. In this
14709 -- case all instance bodies are processed in order
14710 -- and there is no need to collect them separately.
14712 null;
14713 end if;
14715 Decl := First_Elmt (Previous_Instances);
14716 while Present (Decl) loop
14717 Info :=
14718 (Act_Decl =>
14719 Instance_Spec (Node (Decl)),
14720 Config_Switches => Save_Config_Switches,
14721 Current_Sem_Unit =>
14722 Get_Code_Unit (Sloc (Node (Decl))),
14723 Expander_Status => Exp_Status,
14724 Inst_Node => Node (Decl),
14725 Local_Suppress_Stack_Top =>
14726 Local_Suppress_Stack_Top,
14727 Scope_Suppress => Scope_Suppress,
14728 Warnings => Save_Warnings);
14730 -- Package instance
14732 if Nkind (Node (Decl)) = N_Package_Instantiation
14733 then
14734 Instantiate_Package_Body
14735 (Info, Body_Optional => True);
14737 -- Subprogram instance
14739 else
14740 -- The instance_spec is in the wrapper package,
14741 -- usually followed by its local renaming
14742 -- declaration. See Build_Subprogram_Renaming
14743 -- for details. If the instance carries aspects,
14744 -- these result in the corresponding pragmas,
14745 -- inserted after the subprogram declaration.
14746 -- They must be skipped as well when retrieving
14747 -- the desired spec. Some of them may have been
14748 -- rewritten as null statements.
14749 -- A direct link would be more robust ???
14751 declare
14752 Decl : Node_Id :=
14753 (Last (Visible_Declarations
14754 (Specification (Info.Act_Decl))));
14755 begin
14756 while Nkind (Decl) in
14757 N_Null_Statement |
14758 N_Pragma |
14759 N_Subprogram_Renaming_Declaration
14760 loop
14761 Decl := Prev (Decl);
14762 end loop;
14764 Info.Act_Decl := Decl;
14765 end;
14767 Instantiate_Subprogram_Body
14768 (Info, Body_Optional => True);
14769 end if;
14771 Next_Elmt (Decl);
14772 end loop;
14773 end;
14774 end if;
14776 Instantiate_Package_Body
14777 (Body_Info =>
14778 ((Act_Decl => True_Parent,
14779 Config_Switches => Save_Config_Switches,
14780 Current_Sem_Unit =>
14781 Get_Code_Unit (Sloc (Inst_Node)),
14782 Expander_Status => Exp_Status,
14783 Inst_Node => Inst_Node,
14784 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
14785 Scope_Suppress => Scope_Suppress,
14786 Warnings => Save_Warnings)),
14787 Body_Optional => Body_Optional);
14788 end;
14789 end if;
14791 -- Case where we are not instantiating a nested generic
14793 else
14794 Opt.Style_Check := False;
14795 Expander_Mode_Save_And_Set (True);
14796 Load_Needed_Body (Comp_Unit, OK);
14797 Opt.Style_Check := Saved_Style_Check;
14798 Restore_Warnings (Saved_Warn);
14799 Expander_Mode_Restore;
14801 if not OK
14802 and then Unit_Requires_Body (Defining_Entity (Spec))
14803 and then not Body_Optional
14804 then
14805 declare
14806 Bname : constant Unit_Name_Type :=
14807 Get_Body_Name (Get_Unit_Name (Unit (Comp_Unit)));
14809 begin
14810 -- In CodePeer mode, the missing body may make the analysis
14811 -- incomplete, but we do not treat it as fatal.
14813 if CodePeer_Mode then
14814 return;
14816 else
14817 Error_Msg_Unit_1 := Bname;
14818 Error_Msg_N ("this instantiation requires$!", N);
14819 Error_Msg_File_1 :=
14820 Get_File_Name (Bname, Subunit => False);
14821 Error_Msg_N ("\but file{ was not found!", N);
14822 raise Unrecoverable_Error;
14823 end if;
14824 end;
14825 end if;
14826 end if;
14827 end if;
14829 -- If loading parent of the generic caused an instantiation circularity,
14830 -- we abandon compilation at this point, because otherwise in some cases
14831 -- we get into trouble with infinite recursions after this point.
14833 if Circularity_Detected then
14834 raise Unrecoverable_Error;
14835 end if;
14836 end Load_Parent_Of_Generic;
14838 ---------------------------------
14839 -- Map_Formal_Package_Entities --
14840 ---------------------------------
14842 procedure Map_Formal_Package_Entities (Form : Entity_Id; Act : Entity_Id) is
14843 E1 : Entity_Id;
14844 E2 : Entity_Id;
14846 begin
14847 Set_Instance_Of (Form, Act);
14849 -- Traverse formal and actual package to map the corresponding entities.
14850 -- We skip over internal entities that may be generated during semantic
14851 -- analysis, and find the matching entities by name, given that they
14852 -- must appear in the same order.
14854 E1 := First_Entity (Form);
14855 E2 := First_Entity (Act);
14856 while Present (E1) and then E1 /= First_Private_Entity (Form) loop
14857 -- Could this test be a single condition??? Seems like it could, and
14858 -- isn't FPE (Form) a constant anyway???
14860 if not Is_Internal (E1)
14861 and then Present (Parent (E1))
14862 and then not Is_Class_Wide_Type (E1)
14863 and then not Is_Internal_Name (Chars (E1))
14864 then
14865 while Present (E2) and then Chars (E2) /= Chars (E1) loop
14866 Next_Entity (E2);
14867 end loop;
14869 if No (E2) then
14870 exit;
14871 else
14872 Set_Instance_Of (E1, E2);
14874 if Is_Type (E1) and then Is_Tagged_Type (E2) then
14875 Set_Instance_Of (Class_Wide_Type (E1), Class_Wide_Type (E2));
14876 end if;
14878 if Is_Constrained (E1) then
14879 Set_Instance_Of (Base_Type (E1), Base_Type (E2));
14880 end if;
14882 if Ekind (E1) = E_Package and then No (Renamed_Entity (E1)) then
14883 Map_Formal_Package_Entities (E1, E2);
14884 end if;
14885 end if;
14886 end if;
14888 Next_Entity (E1);
14889 end loop;
14890 end Map_Formal_Package_Entities;
14892 -----------------------
14893 -- Move_Freeze_Nodes --
14894 -----------------------
14896 procedure Move_Freeze_Nodes
14897 (Out_Of : Entity_Id;
14898 After : Node_Id;
14899 L : List_Id)
14901 Decl : Node_Id;
14902 Next_Decl : Node_Id;
14903 Next_Node : Node_Id := After;
14904 Spec : Node_Id;
14906 function Is_Outer_Type (T : Entity_Id) return Boolean;
14907 -- Check whether entity is declared in a scope external to that of the
14908 -- generic unit.
14910 -------------------
14911 -- Is_Outer_Type --
14912 -------------------
14914 function Is_Outer_Type (T : Entity_Id) return Boolean is
14915 Scop : Entity_Id := Scope (T);
14917 begin
14918 if Scope_Depth (Scop) < Scope_Depth (Out_Of) then
14919 return True;
14921 else
14922 while Scop /= Standard_Standard loop
14923 if Scop = Out_Of then
14924 return False;
14925 else
14926 Scop := Scope (Scop);
14927 end if;
14928 end loop;
14930 return True;
14931 end if;
14932 end Is_Outer_Type;
14934 -- Start of processing for Move_Freeze_Nodes
14936 begin
14937 if No (L) then
14938 return;
14939 end if;
14941 -- First remove the freeze nodes that may appear before all other
14942 -- declarations.
14944 Decl := First (L);
14945 while Present (Decl)
14946 and then Nkind (Decl) = N_Freeze_Entity
14947 and then Is_Outer_Type (Entity (Decl))
14948 loop
14949 Decl := Remove_Head (L);
14950 Insert_After (Next_Node, Decl);
14951 Set_Analyzed (Decl, False);
14952 Next_Node := Decl;
14953 Decl := First (L);
14954 end loop;
14956 -- Next scan the list of declarations and remove each freeze node that
14957 -- appears ahead of the current node.
14959 while Present (Decl) loop
14960 while Present (Next (Decl))
14961 and then Nkind (Next (Decl)) = N_Freeze_Entity
14962 and then Is_Outer_Type (Entity (Next (Decl)))
14963 loop
14964 Next_Decl := Remove_Next (Decl);
14965 Insert_After (Next_Node, Next_Decl);
14966 Set_Analyzed (Next_Decl, False);
14967 Next_Node := Next_Decl;
14968 end loop;
14970 -- If the declaration is a nested package or concurrent type, then
14971 -- recurse. Nested generic packages will have been processed from the
14972 -- inside out.
14974 case Nkind (Decl) is
14975 when N_Package_Declaration =>
14976 Spec := Specification (Decl);
14978 when N_Task_Type_Declaration =>
14979 Spec := Task_Definition (Decl);
14981 when N_Protected_Type_Declaration =>
14982 Spec := Protected_Definition (Decl);
14984 when others =>
14985 Spec := Empty;
14986 end case;
14988 if Present (Spec) then
14989 Move_Freeze_Nodes (Out_Of, Next_Node, Visible_Declarations (Spec));
14990 Move_Freeze_Nodes (Out_Of, Next_Node, Private_Declarations (Spec));
14991 end if;
14993 Next (Decl);
14994 end loop;
14995 end Move_Freeze_Nodes;
14997 ----------------
14998 -- Next_Assoc --
14999 ----------------
15001 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr is
15002 begin
15003 return Generic_Renamings.Table (E).Next_In_HTable;
15004 end Next_Assoc;
15006 ------------------------
15007 -- Preanalyze_Actuals --
15008 ------------------------
15010 procedure Preanalyze_Actuals (N : Node_Id; Inst : Entity_Id := Empty) is
15011 procedure Perform_Appropriate_Analysis (N : Node_Id);
15012 -- Determine if the actuals we are analyzing come from a generic
15013 -- instantiation that is a library unit and dispatch accordingly.
15015 ----------------------------------
15016 -- Perform_Appropriate_Analysis --
15017 ----------------------------------
15019 procedure Perform_Appropriate_Analysis (N : Node_Id) is
15020 begin
15021 -- When we have a library instantiation we cannot allow any expansion
15022 -- to occur, since there may be no place to put it. Instead, in that
15023 -- case we perform a preanalysis of the actual.
15025 if Present (Inst) and then Is_Compilation_Unit (Inst) then
15026 Preanalyze (N);
15027 else
15028 Analyze (N);
15029 end if;
15030 end Perform_Appropriate_Analysis;
15032 -- Local variables
15034 Errs : constant Nat := Serious_Errors_Detected;
15036 Assoc : Node_Id;
15037 Act : Node_Id;
15039 Cur : Entity_Id := Empty;
15040 -- Current homograph of the instance name
15042 Vis : Boolean := False;
15043 -- Saved visibility status of the current homograph
15045 -- Start of processing for Preanalyze_Actuals
15047 begin
15048 Assoc := First (Generic_Associations (N));
15050 -- If the instance is a child unit, its name may hide an outer homonym,
15051 -- so make it invisible to perform name resolution on the actuals.
15053 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name
15054 and then Present
15055 (Current_Entity (Defining_Identifier (Defining_Unit_Name (N))))
15056 then
15057 Cur := Current_Entity (Defining_Identifier (Defining_Unit_Name (N)));
15059 if Is_Compilation_Unit (Cur) then
15060 Vis := Is_Immediately_Visible (Cur);
15061 Set_Is_Immediately_Visible (Cur, False);
15062 else
15063 Cur := Empty;
15064 end if;
15065 end if;
15067 while Present (Assoc) loop
15068 if Nkind (Assoc) /= N_Others_Choice then
15069 Act := Explicit_Generic_Actual_Parameter (Assoc);
15071 -- Within a nested instantiation, a defaulted actual is an empty
15072 -- association, so nothing to analyze. If the subprogram actual
15073 -- is an attribute, analyze prefix only, because actual is not a
15074 -- complete attribute reference.
15076 -- If actual is an allocator, analyze expression only. The full
15077 -- analysis can generate code, and if instance is a compilation
15078 -- unit we have to wait until the package instance is installed
15079 -- to have a proper place to insert this code.
15081 -- String literals may be operators, but at this point we do not
15082 -- know whether the actual is a formal subprogram or a string.
15084 if No (Act) then
15085 null;
15087 elsif Nkind (Act) = N_Attribute_Reference then
15088 Perform_Appropriate_Analysis (Prefix (Act));
15090 elsif Nkind (Act) = N_Explicit_Dereference then
15091 Perform_Appropriate_Analysis (Prefix (Act));
15093 elsif Nkind (Act) = N_Allocator then
15094 declare
15095 Expr : constant Node_Id := Expression (Act);
15097 begin
15098 if Nkind (Expr) = N_Subtype_Indication then
15099 Perform_Appropriate_Analysis (Subtype_Mark (Expr));
15101 -- Analyze separately each discriminant constraint, when
15102 -- given with a named association.
15104 declare
15105 Constr : Node_Id;
15107 begin
15108 Constr := First (Constraints (Constraint (Expr)));
15109 while Present (Constr) loop
15110 if Nkind (Constr) = N_Discriminant_Association then
15111 Perform_Appropriate_Analysis
15112 (Expression (Constr));
15113 else
15114 Perform_Appropriate_Analysis (Constr);
15115 end if;
15117 Next (Constr);
15118 end loop;
15119 end;
15121 else
15122 Perform_Appropriate_Analysis (Expr);
15123 end if;
15124 end;
15126 elsif Nkind (Act) /= N_Operator_Symbol then
15127 Perform_Appropriate_Analysis (Act);
15129 -- Within a package instance, mark actuals that are limited
15130 -- views, so their use can be moved to the body of the
15131 -- enclosing unit.
15133 if Is_Entity_Name (Act)
15134 and then Is_Type (Entity (Act))
15135 and then From_Limited_With (Entity (Act))
15136 and then Present (Inst)
15137 then
15138 Append_Elmt (Entity (Act), Incomplete_Actuals (Inst));
15139 end if;
15140 end if;
15142 if Errs /= Serious_Errors_Detected then
15144 -- Do a minimal analysis of the generic, to prevent spurious
15145 -- warnings complaining about the generic being unreferenced,
15146 -- before abandoning the instantiation.
15148 Perform_Appropriate_Analysis (Name (N));
15150 if Is_Entity_Name (Name (N))
15151 and then Etype (Name (N)) /= Any_Type
15152 then
15153 Generate_Reference (Entity (Name (N)), Name (N));
15154 Set_Is_Instantiated (Entity (Name (N)));
15155 end if;
15157 if Present (Cur) then
15159 -- For the case of a child instance hiding an outer homonym,
15160 -- provide additional warning which might explain the error.
15162 Set_Is_Immediately_Visible (Cur, Vis);
15163 Error_Msg_NE
15164 ("& hides outer unit with the same name??",
15165 N, Defining_Unit_Name (N));
15166 end if;
15168 Abandon_Instantiation (Act);
15169 end if;
15170 end if;
15172 Next (Assoc);
15173 end loop;
15175 if Present (Cur) then
15176 Set_Is_Immediately_Visible (Cur, Vis);
15177 end if;
15178 end Preanalyze_Actuals;
15180 -------------------------------
15181 -- Provide_Completing_Bodies --
15182 -------------------------------
15184 procedure Provide_Completing_Bodies (N : Node_Id) is
15185 procedure Build_Completing_Body (Subp_Decl : Node_Id);
15186 -- Generate the completing body for subprogram declaration Subp_Decl
15188 procedure Provide_Completing_Bodies_In (Decls : List_Id);
15189 -- Generating completing bodies for all subprograms found in declarative
15190 -- list Decls.
15192 ---------------------------
15193 -- Build_Completing_Body --
15194 ---------------------------
15196 procedure Build_Completing_Body (Subp_Decl : Node_Id) is
15197 Loc : constant Source_Ptr := Sloc (Subp_Decl);
15198 Subp_Id : constant Entity_Id := Defining_Entity (Subp_Decl);
15199 Spec : Node_Id;
15201 begin
15202 -- Nothing to do if the subprogram already has a completing body
15204 if Present (Corresponding_Body (Subp_Decl)) then
15205 return;
15207 -- Mark the function as having a valid return statement even though
15208 -- the body contains a single raise statement.
15210 elsif Ekind (Subp_Id) = E_Function then
15211 Set_Return_Present (Subp_Id);
15212 end if;
15214 -- Clone the specification to obtain new entities and reset the only
15215 -- semantic field.
15217 Spec := Copy_Subprogram_Spec (Specification (Subp_Decl));
15218 Set_Generic_Parent (Spec, Empty);
15220 -- Generate:
15221 -- function Func ... return ... is
15222 -- <or>
15223 -- procedure Proc ... is
15224 -- begin
15225 -- raise Program_Error with "access before elaboration";
15226 -- edn Proc;
15228 Insert_After_And_Analyze (Subp_Decl,
15229 Make_Subprogram_Body (Loc,
15230 Specification => Spec,
15231 Declarations => New_List,
15232 Handled_Statement_Sequence =>
15233 Make_Handled_Sequence_Of_Statements (Loc,
15234 Statements => New_List (
15235 Make_Raise_Program_Error (Loc,
15236 Reason => PE_Access_Before_Elaboration)))));
15237 end Build_Completing_Body;
15239 ----------------------------------
15240 -- Provide_Completing_Bodies_In --
15241 ----------------------------------
15243 procedure Provide_Completing_Bodies_In (Decls : List_Id) is
15244 Decl : Node_Id;
15246 begin
15247 if Present (Decls) then
15248 Decl := First (Decls);
15249 while Present (Decl) loop
15250 Provide_Completing_Bodies (Decl);
15251 Next (Decl);
15252 end loop;
15253 end if;
15254 end Provide_Completing_Bodies_In;
15256 -- Local variables
15258 Spec : Node_Id;
15260 -- Start of processing for Provide_Completing_Bodies
15262 begin
15263 if Nkind (N) = N_Package_Declaration then
15264 Spec := Specification (N);
15266 Push_Scope (Defining_Entity (N));
15267 Provide_Completing_Bodies_In (Visible_Declarations (Spec));
15268 Provide_Completing_Bodies_In (Private_Declarations (Spec));
15269 Pop_Scope;
15271 elsif Nkind (N) = N_Subprogram_Declaration then
15272 Build_Completing_Body (N);
15273 end if;
15274 end Provide_Completing_Bodies;
15276 -------------------
15277 -- Remove_Parent --
15278 -------------------
15280 procedure Remove_Parent (In_Body : Boolean := False) is
15281 S : Entity_Id := Current_Scope;
15282 -- S is the scope containing the instantiation just completed. The scope
15283 -- stack contains the parent instances of the instantiation, followed by
15284 -- the original S.
15286 Cur_P : Entity_Id;
15287 E : Entity_Id;
15288 P : Entity_Id;
15289 Hidden : Elmt_Id;
15291 begin
15292 -- After child instantiation is complete, remove from scope stack the
15293 -- extra copy of the current scope, and then remove parent instances.
15295 if not In_Body then
15296 Pop_Scope;
15298 while Current_Scope /= S loop
15299 P := Current_Scope;
15300 End_Package_Scope (Current_Scope);
15302 if In_Open_Scopes (P) then
15303 E := First_Entity (P);
15304 while Present (E) loop
15305 Set_Is_Immediately_Visible (E, True);
15306 Next_Entity (E);
15307 end loop;
15309 -- If instantiation is declared in a block, it is the enclosing
15310 -- scope that might be a parent instance. Note that only one
15311 -- block can be involved, because the parent instances have
15312 -- been installed within it.
15314 if Ekind (P) = E_Block then
15315 Cur_P := Scope (P);
15316 else
15317 Cur_P := P;
15318 end if;
15320 if Is_Generic_Instance (Cur_P) and then P /= Current_Scope then
15321 -- We are within an instance of some sibling. Retain
15322 -- visibility of parent, for proper subsequent cleanup, and
15323 -- reinstall private declarations as well.
15325 Set_In_Private_Part (P);
15326 Install_Private_Declarations (P);
15327 end if;
15329 -- If the ultimate parent is a top-level unit recorded in
15330 -- Instance_Parent_Unit, then reset its visibility to what it was
15331 -- before instantiation. (It's not clear what the purpose is of
15332 -- testing whether Scope (P) is In_Open_Scopes, but that test was
15333 -- present before the ultimate parent test was added.???)
15335 elsif not In_Open_Scopes (Scope (P))
15336 or else (P = Instance_Parent_Unit
15337 and then not Parent_Unit_Visible)
15338 then
15339 Set_Is_Immediately_Visible (P, False);
15341 -- If the current scope is itself an instantiation of a generic
15342 -- nested within P, and we are in the private part of body of this
15343 -- instantiation, restore the full views of P, that were removed
15344 -- in End_Package_Scope above. This obscure case can occur when a
15345 -- subunit of a generic contains an instance of a child unit of
15346 -- its generic parent unit.
15348 elsif S = Current_Scope and then Is_Generic_Instance (S)
15349 and then (In_Package_Body (S) or else In_Private_Part (S))
15350 then
15351 declare
15352 Par : constant Entity_Id :=
15353 Generic_Parent (Package_Specification (S));
15354 begin
15355 if Present (Par)
15356 and then P = Scope (Par)
15357 then
15358 Set_In_Private_Part (P);
15359 Install_Private_Declarations (P);
15360 end if;
15361 end;
15362 end if;
15363 end loop;
15365 -- Reset visibility of entities in the enclosing scope
15367 Set_Is_Hidden_Open_Scope (Current_Scope, False);
15369 Hidden := First_Elmt (Hidden_Entities);
15370 while Present (Hidden) loop
15371 Set_Is_Immediately_Visible (Node (Hidden), True);
15372 Next_Elmt (Hidden);
15373 end loop;
15375 else
15376 -- Each body is analyzed separately, and there is no context that
15377 -- needs preserving from one body instance to the next, so remove all
15378 -- parent scopes that have been installed.
15380 while Present (S) loop
15381 End_Package_Scope (S);
15382 Set_Is_Immediately_Visible (S, False);
15383 S := Current_Scope;
15384 exit when S = Standard_Standard;
15385 end loop;
15386 end if;
15387 end Remove_Parent;
15389 -----------------------------------
15390 -- Requires_Conformance_Checking --
15391 -----------------------------------
15393 function Requires_Conformance_Checking (N : Node_Id) return Boolean is
15394 begin
15395 -- No conformance checking required if the generic actual part is empty,
15396 -- or is a box or an others_clause (necessarily with a box).
15398 return Present (Generic_Associations (N))
15399 and then not Box_Present (N)
15400 and then Nkind (First (Generic_Associations (N))) /= N_Others_Choice;
15401 end Requires_Conformance_Checking;
15403 -----------------
15404 -- Restore_Env --
15405 -----------------
15407 procedure Restore_Env is
15408 Saved : Instance_Env renames Instance_Envs.Table (Instance_Envs.Last);
15410 begin
15411 if No (Current_Instantiated_Parent.Act_Id) then
15412 -- Restore environment after subprogram inlining
15414 Restore_Private_Views (Empty);
15415 end if;
15417 Current_Instantiated_Parent := Saved.Instantiated_Parent;
15418 Exchanged_Views := Saved.Exchanged_Views;
15419 Hidden_Entities := Saved.Hidden_Entities;
15420 Current_Sem_Unit := Saved.Current_Sem_Unit;
15421 Parent_Unit_Visible := Saved.Parent_Unit_Visible;
15422 Instance_Parent_Unit := Saved.Instance_Parent_Unit;
15424 Restore_Config_Switches (Saved.Switches);
15426 Instance_Envs.Decrement_Last;
15427 end Restore_Env;
15429 ---------------------------
15430 -- Restore_Private_Views --
15431 ---------------------------
15433 procedure Restore_Private_Views
15434 (Pack_Id : Entity_Id;
15435 Is_Package : Boolean := True)
15437 M : Elmt_Id;
15438 E : Entity_Id;
15439 Typ : Entity_Id;
15440 Dep_Elmt : Elmt_Id;
15441 Dep_Typ : Node_Id;
15443 procedure Restore_Nested_Formal (Formal : Entity_Id);
15444 -- Hide the generic formals of formal packages declared with box which
15445 -- were reachable in the current instantiation.
15447 ---------------------------
15448 -- Restore_Nested_Formal --
15449 ---------------------------
15451 procedure Restore_Nested_Formal (Formal : Entity_Id) is
15452 pragma Assert (Ekind (Formal) = E_Package);
15453 Ent : Entity_Id;
15454 begin
15455 if Present (Renamed_Entity (Formal))
15456 and then Denotes_Formal_Package (Renamed_Entity (Formal), True)
15457 then
15458 return;
15460 elsif Present (Associated_Formal_Package (Formal)) then
15461 Ent := First_Entity (Formal);
15462 while Present (Ent) loop
15463 exit when Ekind (Ent) = E_Package
15464 and then Renamed_Entity (Ent) = Renamed_Entity (Formal);
15466 Set_Is_Hidden (Ent);
15467 Set_Is_Potentially_Use_Visible (Ent, False);
15469 -- If package, then recurse
15471 if Ekind (Ent) = E_Package then
15472 Restore_Nested_Formal (Ent);
15473 end if;
15475 Next_Entity (Ent);
15476 end loop;
15477 end if;
15478 end Restore_Nested_Formal;
15480 -- Start of processing for Restore_Private_Views
15482 begin
15483 M := First_Elmt (Exchanged_Views);
15484 while Present (M) loop
15485 Typ := Node (M);
15487 -- Subtypes of types whose views have been exchanged, and that are
15488 -- defined within the instance, were not on the Private_Dependents
15489 -- list on entry to the instance, so they have to be exchanged
15490 -- explicitly now, in order to remain consistent with the view of the
15491 -- parent type.
15493 if Ekind (Typ) in E_Private_Type
15494 | E_Limited_Private_Type
15495 | E_Record_Type_With_Private
15496 then
15497 Dep_Elmt := First_Elmt (Private_Dependents (Typ));
15498 while Present (Dep_Elmt) loop
15499 Dep_Typ := Node (Dep_Elmt);
15501 if Scope (Dep_Typ) = Pack_Id
15502 and then Present (Full_View (Dep_Typ))
15503 then
15504 Replace_Elmt (Dep_Elmt, Full_View (Dep_Typ));
15505 Exchange_Declarations (Dep_Typ);
15506 end if;
15508 Next_Elmt (Dep_Elmt);
15509 end loop;
15510 end if;
15512 Exchange_Declarations (Typ);
15513 Next_Elmt (M);
15514 end loop;
15516 if No (Pack_Id) then
15517 return;
15518 end if;
15520 -- Make the generic formal parameters private, and make the formal types
15521 -- into subtypes of the actuals again.
15523 E := First_Entity (Pack_Id);
15524 while Present (E) loop
15525 Set_Is_Hidden (E, True);
15527 if Is_Type (E)
15528 and then Nkind (Parent (E)) = N_Subtype_Declaration
15529 then
15530 -- Always preserve the flag Is_Generic_Actual_Type for GNATprove,
15531 -- as it is needed to identify the subtype with the type it
15532 -- renames, when there are conversions between access types
15533 -- to these.
15535 if GNATprove_Mode then
15536 null;
15538 -- If the actual for E is itself a generic actual type from
15539 -- an enclosing instance, E is still a generic actual type
15540 -- outside of the current instance. This matter when resolving
15541 -- an overloaded call that may be ambiguous in the enclosing
15542 -- instance, when two of its actuals coincide.
15544 elsif Is_Entity_Name (Subtype_Indication (Parent (E)))
15545 and then Is_Generic_Actual_Type
15546 (Entity (Subtype_Indication (Parent (E))))
15547 then
15548 null;
15549 else
15550 Set_Is_Generic_Actual_Type (E, False);
15552 -- It might seem reasonable to clear the Is_Generic_Actual_Type
15553 -- flag also on the Full_View if the type is private, since it
15554 -- was set also on this Full_View. However, this flag is relied
15555 -- upon by Covers to spot "types exported from instantiations"
15556 -- which are implicit Full_Views built for instantiations made
15557 -- on private types and we get type mismatches if we do it when
15558 -- the block exchanging the declarations below triggers ???
15560 -- if Is_Private_Type (E) and then Present (Full_View (E)) then
15561 -- Set_Is_Generic_Actual_Type (Full_View (E), False);
15562 -- end if;
15563 end if;
15565 -- An unusual case of aliasing: the actual may also be directly
15566 -- visible in the generic, and be private there, while it is fully
15567 -- visible in the context of the instance. The internal subtype
15568 -- is private in the instance but has full visibility like its
15569 -- parent in the enclosing scope. This enforces the invariant that
15570 -- the privacy status of all private dependents of a type coincide
15571 -- with that of the parent type. This can only happen when a
15572 -- generic child unit is instantiated within a sibling.
15574 if Is_Private_Type (E)
15575 and then not Is_Private_Type (Etype (E))
15576 then
15577 Exchange_Declarations (E);
15578 end if;
15580 elsif Ekind (E) = E_Package then
15582 -- The end of the renaming list is the renaming of the generic
15583 -- package itself. If the instance is a subprogram, all entities
15584 -- in the corresponding package are renamings. If this entity is
15585 -- a formal package, make its own formals private as well. The
15586 -- actual in this case is itself the renaming of an instantiation.
15587 -- If the entity is not a package renaming, it is the entity
15588 -- created to validate formal package actuals: ignore it.
15590 -- If the actual is itself a formal package for the enclosing
15591 -- generic, or the actual for such a formal package, it remains
15592 -- visible on exit from the instance, and therefore nothing needs
15593 -- to be done either, except to keep it accessible.
15595 if Is_Package and then Renamed_Entity (E) = Pack_Id then
15596 exit;
15598 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
15599 null;
15601 elsif
15602 Denotes_Formal_Package (Renamed_Entity (E), True, Pack_Id)
15603 then
15604 Set_Is_Hidden (E, False);
15606 else
15607 declare
15608 Act_P : constant Entity_Id := Renamed_Entity (E);
15609 Id : Entity_Id;
15611 begin
15612 Id := First_Entity (Act_P);
15613 while Present (Id)
15614 and then Id /= First_Private_Entity (Act_P)
15615 loop
15616 exit when Ekind (Id) = E_Package
15617 and then Renamed_Entity (Id) = Act_P;
15619 Set_Is_Hidden (Id, True);
15620 Set_Is_Potentially_Use_Visible (Id, In_Use (Act_P));
15622 if Ekind (Id) = E_Package then
15623 Restore_Nested_Formal (Id);
15624 end if;
15626 Next_Entity (Id);
15627 end loop;
15628 end;
15629 end if;
15630 end if;
15632 Next_Entity (E);
15633 end loop;
15634 end Restore_Private_Views;
15636 --------------
15637 -- Save_Env --
15638 --------------
15640 procedure Save_Env
15641 (Gen_Unit : Entity_Id;
15642 Act_Unit : Entity_Id)
15644 begin
15645 Init_Env;
15646 Set_Instance_Env (Gen_Unit, Act_Unit);
15647 end Save_Env;
15649 ----------------------------
15650 -- Save_Global_References --
15651 ----------------------------
15653 procedure Save_Global_References (Templ : Node_Id) is
15655 -- ??? it is horrible to use global variables in highly recursive code
15657 E : Entity_Id;
15658 -- The entity of the current associated node
15660 Gen_Scope : Entity_Id;
15661 -- The scope of the generic for which references are being saved
15663 N2 : Node_Id;
15664 -- The current associated node
15666 function Is_Global (E : Entity_Id) return Boolean;
15667 -- Check whether entity is defined outside of generic unit. Examine the
15668 -- scope of an entity, and the scope of the scope, etc, until we find
15669 -- either Standard, in which case the entity is global, or the generic
15670 -- unit itself, which indicates that the entity is local. If the entity
15671 -- is the generic unit itself, as in the case of a recursive call, or
15672 -- the enclosing generic unit, if different from the current scope, then
15673 -- it is local as well, because it will be replaced at the point of
15674 -- instantiation. On the other hand, if it is a reference to a child
15675 -- unit of a common ancestor, which appears in an instantiation, it is
15676 -- global because it is used to denote a specific compilation unit at
15677 -- the time the instantiations will be analyzed.
15679 procedure Qualify_Universal_Operands
15680 (Op : Node_Id;
15681 Func_Call : Node_Id);
15682 -- Op denotes a binary or unary operator in generic template Templ. Node
15683 -- Func_Call is the function call alternative of the operator within the
15684 -- the analyzed copy of the template. Change each operand which yields a
15685 -- universal type by wrapping it into a qualified expression
15687 -- Actual_Typ'(Operand)
15689 -- where Actual_Typ is the type of corresponding actual parameter of
15690 -- Operand in Func_Call.
15692 procedure Reset_Entity (N : Node_Id);
15693 -- Save semantic information on global entity so that it is not resolved
15694 -- again at instantiation time.
15696 procedure Save_Entity_Descendants (N : Node_Id);
15697 -- Apply Save_Global_References to the two syntactic descendants of
15698 -- non-terminal nodes that carry an Associated_Node and are processed
15699 -- through Reset_Entity. Once the global entity (if any) has been
15700 -- captured together with its type, only two syntactic descendants need
15701 -- to be traversed to complete the processing of the tree rooted at N.
15702 -- This applies to Selected_Components, Expanded_Names, and to Operator
15703 -- nodes. N can also be a character literal, identifier, or operator
15704 -- symbol node, but the call has no effect in these cases.
15706 procedure Save_Global_Defaults (N1 : Node_Id; N2 : Node_Id);
15707 -- Default actuals in nested instances must be handled specially
15708 -- because there is no link to them from the original tree. When an
15709 -- actual subprogram is given by a default, we add an explicit generic
15710 -- association for it in the instantiation node. When we save the
15711 -- global references on the name of the instance, we recover the list
15712 -- of generic associations, and add an explicit one to the original
15713 -- generic tree, through which a global actual can be preserved.
15714 -- Similarly, if a child unit is instantiated within a sibling, in the
15715 -- context of the parent, we must preserve the identifier of the parent
15716 -- so that it can be properly resolved in a subsequent instantiation.
15718 procedure Save_Global_Descendant (D : Union_Id);
15719 -- Apply Save_References recursively to the descendants of node D
15721 procedure Save_References (N : Node_Id);
15722 -- This is the recursive procedure that does the work, once the
15723 -- enclosing generic scope has been established.
15725 ---------------
15726 -- Is_Global --
15727 ---------------
15729 function Is_Global (E : Entity_Id) return Boolean is
15730 Se : Entity_Id;
15732 function Is_Instance_Node (Decl : Node_Id) return Boolean;
15733 -- Determine whether the parent node of a reference to a child unit
15734 -- denotes an instantiation or a formal package, in which case the
15735 -- reference to the child unit is global, even if it appears within
15736 -- the current scope (e.g. when the instance appears within the body
15737 -- of an ancestor).
15739 ----------------------
15740 -- Is_Instance_Node --
15741 ----------------------
15743 function Is_Instance_Node (Decl : Node_Id) return Boolean is
15744 begin
15745 return Nkind (Decl) in N_Generic_Instantiation
15746 or else
15747 Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration;
15748 end Is_Instance_Node;
15750 -- Start of processing for Is_Global
15752 begin
15753 if E = Gen_Scope then
15754 return False;
15756 elsif E = Standard_Standard then
15757 return True;
15759 -- E should be an entity, but it is not always
15761 elsif Nkind (E) not in N_Entity then
15762 return False;
15764 elsif Nkind (E) /= N_Expanded_Name
15765 and then Is_Child_Unit (E)
15766 and then (Is_Instance_Node (Parent (N2))
15767 or else (Nkind (Parent (N2)) = N_Expanded_Name
15768 and then N2 = Selector_Name (Parent (N2))
15769 and then
15770 Is_Instance_Node (Parent (Parent (N2)))))
15771 then
15772 return True;
15774 else
15775 -- E may be an expanded name - typically an operator - in which
15776 -- case we must find its enclosing scope since expanded names
15777 -- don't have corresponding scopes.
15779 if Nkind (E) = N_Expanded_Name then
15780 Se := Find_Enclosing_Scope (E);
15782 -- Otherwise, E is an entity and will have Scope set
15784 else
15785 Se := Scope (E);
15786 end if;
15788 while Se /= Gen_Scope loop
15789 if Se = Standard_Standard then
15790 return True;
15791 else
15792 Se := Scope (Se);
15793 end if;
15794 end loop;
15796 return False;
15797 end if;
15798 end Is_Global;
15800 --------------------------------
15801 -- Qualify_Universal_Operands --
15802 --------------------------------
15804 procedure Qualify_Universal_Operands
15805 (Op : Node_Id;
15806 Func_Call : Node_Id)
15808 procedure Qualify_Operand (Opnd : Node_Id; Actual : Node_Id);
15809 -- Rewrite operand Opnd as a qualified expression of the form
15811 -- Actual_Typ'(Opnd)
15813 -- where Actual is the corresponding actual parameter of Opnd in
15814 -- function call Func_Call.
15816 function Qualify_Type
15817 (Loc : Source_Ptr;
15818 Typ : Entity_Id) return Node_Id;
15819 -- Qualify type Typ by creating a selected component of the form
15821 -- Scope_Of_Typ.Typ
15823 ---------------------
15824 -- Qualify_Operand --
15825 ---------------------
15827 procedure Qualify_Operand (Opnd : Node_Id; Actual : Node_Id) is
15828 Loc : constant Source_Ptr := Sloc (Opnd);
15829 Typ : constant Entity_Id := Etype (Actual);
15830 Mark : Node_Id;
15831 Qual : Node_Id;
15833 begin
15834 -- Qualify the operand when it is of a universal type. Note that
15835 -- the template is unanalyzed and it is not possible to directly
15836 -- query the type. This transformation is not done when the type
15837 -- of the actual is internally generated because the type will be
15838 -- regenerated in the instance.
15840 if Yields_Universal_Type (Opnd)
15841 and then Comes_From_Source (Typ)
15842 and then not Is_Hidden (Typ)
15843 then
15844 -- The type of the actual may be a global reference. Save this
15845 -- information by creating a reference to it.
15847 if Is_Global (Typ) then
15848 Mark := New_Occurrence_Of (Typ, Loc);
15850 -- Otherwise rely on resolution to find the proper type within
15851 -- the instance.
15853 else
15854 Mark := Qualify_Type (Loc, Typ);
15855 end if;
15857 Qual :=
15858 Make_Qualified_Expression (Loc,
15859 Subtype_Mark => Mark,
15860 Expression => Relocate_Node (Opnd));
15862 -- Mark the qualification to distinguish it from other source
15863 -- constructs and signal the instantiation mechanism that this
15864 -- node requires special processing. See Copy_Generic_Node for
15865 -- details.
15867 Set_Is_Qualified_Universal_Literal (Qual);
15869 Rewrite (Opnd, Qual);
15870 end if;
15871 end Qualify_Operand;
15873 ------------------
15874 -- Qualify_Type --
15875 ------------------
15877 function Qualify_Type
15878 (Loc : Source_Ptr;
15879 Typ : Entity_Id) return Node_Id
15881 Scop : constant Entity_Id := Scope (Typ);
15882 Result : Node_Id;
15884 begin
15885 Result := Make_Identifier (Loc, Chars (Typ));
15887 if Present (Scop) and then not Is_Generic_Unit (Scop) then
15888 Result :=
15889 Make_Selected_Component (Loc,
15890 Prefix => Make_Identifier (Loc, Chars (Scop)),
15891 Selector_Name => Result);
15892 end if;
15894 return Result;
15895 end Qualify_Type;
15897 -- Local variables
15899 Actuals : constant List_Id := Parameter_Associations (Func_Call);
15901 -- Start of processing for Qualify_Universal_Operands
15903 begin
15904 if Nkind (Op) in N_Binary_Op then
15905 Qualify_Operand (Left_Opnd (Op), First (Actuals));
15906 Qualify_Operand (Right_Opnd (Op), Next (First (Actuals)));
15908 elsif Nkind (Op) in N_Unary_Op then
15909 Qualify_Operand (Right_Opnd (Op), First (Actuals));
15910 end if;
15911 end Qualify_Universal_Operands;
15913 ------------------
15914 -- Reset_Entity --
15915 ------------------
15917 procedure Reset_Entity (N : Node_Id) is
15918 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id);
15919 -- If the type of N2 is global to the generic unit, save the type in
15920 -- the generic node. Just as we perform name capture for explicit
15921 -- references within the generic, we must capture the global types
15922 -- of local entities because they may participate in resolution in
15923 -- the instance.
15925 function Top_Ancestor (E : Entity_Id) return Entity_Id;
15926 -- Find the ultimate ancestor of the current unit. If it is not a
15927 -- generic unit, then the name of the current unit in the prefix of
15928 -- an expanded name must be replaced with its generic homonym to
15929 -- ensure that it will be properly resolved in an instance.
15931 ---------------------
15932 -- Set_Global_Type --
15933 ---------------------
15935 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id) is
15936 Typ : constant Entity_Id := Etype (N2);
15938 begin
15939 Set_Etype (N, Typ);
15941 -- If the entity of N is not the associated node, this is a
15942 -- nested generic and it has an associated node as well, whose
15943 -- type is already the full view (see below). Indicate that the
15944 -- original node has a private view.
15946 if Entity (N) /= N2 and then Has_Private_View (Entity (N)) then
15947 Set_Has_Private_View (N);
15948 end if;
15950 -- If not a private type, nothing else to do
15952 if not Is_Private_Type (Typ) then
15953 null;
15955 -- If it is a derivation of a private type in a context where no
15956 -- full view is needed, nothing to do either.
15958 elsif No (Full_View (Typ)) and then Typ /= Etype (Typ) then
15959 null;
15961 -- Otherwise mark the type for flipping and use the full view when
15962 -- available.
15964 else
15965 Set_Has_Private_View (N);
15967 if Present (Full_View (Typ)) then
15968 Set_Etype (N2, Full_View (Typ));
15969 end if;
15970 end if;
15972 if Is_Floating_Point_Type (Typ)
15973 and then Has_Dimension_System (Typ)
15974 then
15975 Copy_Dimensions (N2, N);
15976 end if;
15977 end Set_Global_Type;
15979 ------------------
15980 -- Top_Ancestor --
15981 ------------------
15983 function Top_Ancestor (E : Entity_Id) return Entity_Id is
15984 Par : Entity_Id;
15986 begin
15987 Par := E;
15988 while Is_Child_Unit (Par) loop
15989 Par := Scope (Par);
15990 end loop;
15992 return Par;
15993 end Top_Ancestor;
15995 -- Start of processing for Reset_Entity
15997 begin
15998 N2 := Get_Associated_Node (N);
15999 E := Entity (N2);
16001 if Present (E) then
16003 -- If the node is an entry call to an entry in an enclosing task,
16004 -- it is rewritten as a selected component. No global entity to
16005 -- preserve in this case, since the expansion will be redone in
16006 -- the instance.
16008 if Nkind (E) not in N_Entity then
16009 Set_Associated_Node (N, Empty);
16010 Set_Etype (N, Empty);
16011 return;
16012 end if;
16014 -- If the entity is an itype created as a subtype of an access
16015 -- type with a null exclusion restore source entity for proper
16016 -- visibility. The itype will be created anew in the instance.
16018 if Is_Itype (E)
16019 and then Ekind (E) = E_Access_Subtype
16020 and then Is_Entity_Name (N)
16021 and then Chars (Etype (E)) = Chars (N)
16022 then
16023 E := Etype (E);
16024 Set_Entity (N2, E);
16025 Set_Etype (N2, E);
16026 end if;
16028 if Is_Global (E) then
16029 Set_Global_Type (N, N2);
16031 elsif Nkind (N) = N_Op_Concat
16032 and then Is_Generic_Type (Etype (N2))
16033 and then (Base_Type (Etype (Right_Opnd (N2))) = Etype (N2)
16034 or else
16035 Base_Type (Etype (Left_Opnd (N2))) = Etype (N2))
16036 and then Is_Intrinsic_Subprogram (E)
16037 then
16038 null;
16040 -- Entity is local. Mark generic node as unresolved. Note that now
16041 -- it does not have an entity.
16043 else
16044 Set_Associated_Node (N, Empty);
16045 Set_Etype (N, Empty);
16046 end if;
16048 if Nkind (Parent (N)) in N_Generic_Instantiation
16049 and then N = Name (Parent (N))
16050 then
16051 Save_Global_Defaults (Parent (N), Parent (N2));
16052 end if;
16054 elsif Nkind (Parent (N)) = N_Selected_Component
16055 and then Nkind (Parent (N2)) = N_Expanded_Name
16056 then
16057 -- In case of previous errors, the tree might be malformed
16059 if No (Entity (Parent (N2))) then
16060 null;
16062 elsif Is_Global (Entity (Parent (N2))) then
16063 Change_Selected_Component_To_Expanded_Name (Parent (N));
16064 Set_Associated_Node (Parent (N), Parent (N2));
16065 Set_Global_Type (Parent (N), Parent (N2));
16066 Save_Entity_Descendants (N);
16068 -- If this is a reference to the current generic entity, replace
16069 -- by the name of the generic homonym of the current package. This
16070 -- is because in an instantiation Par.P.Q will not resolve to the
16071 -- name of the instance, whose enclosing scope is not necessarily
16072 -- Par. We use the generic homonym rather that the name of the
16073 -- generic itself because it may be hidden by a local declaration.
16075 elsif In_Open_Scopes (Entity (Parent (N2)))
16076 and then not
16077 Is_Generic_Unit (Top_Ancestor (Entity (Prefix (Parent (N2)))))
16078 then
16079 if Ekind (Entity (Parent (N2))) = E_Generic_Package then
16080 Rewrite (Parent (N),
16081 Make_Identifier (Sloc (N),
16082 Chars =>
16083 Chars (Generic_Homonym (Entity (Parent (N2))))));
16084 else
16085 Rewrite (Parent (N),
16086 Make_Identifier (Sloc (N),
16087 Chars => Chars (Selector_Name (Parent (N2)))));
16088 end if;
16089 end if;
16091 if Nkind (Parent (Parent (N))) in N_Generic_Instantiation
16092 and then Parent (N) = Name (Parent (Parent (N)))
16093 then
16094 Save_Global_Defaults
16095 (Parent (Parent (N)), Parent (Parent (N2)));
16096 end if;
16098 -- A selected component may denote a static constant that has been
16099 -- folded. If the static constant is global to the generic, capture
16100 -- its value. Otherwise the folding will happen in any instantiation.
16102 elsif Nkind (Parent (N)) = N_Selected_Component
16103 and then Nkind (Parent (N2)) in N_Integer_Literal | N_Real_Literal
16104 then
16105 if Present (Entity (Original_Node (Parent (N2))))
16106 and then Is_Global (Entity (Original_Node (Parent (N2))))
16107 then
16108 Rewrite (Parent (N), New_Copy (Parent (N2)));
16109 Set_Analyzed (Parent (N), False);
16110 end if;
16112 -- A selected component may be transformed into a parameterless
16113 -- function call. If the called entity is global, rewrite the node
16114 -- appropriately, i.e. as an extended name for the global entity.
16116 elsif Nkind (Parent (N)) = N_Selected_Component
16117 and then Nkind (Parent (N2)) = N_Function_Call
16118 and then N = Selector_Name (Parent (N))
16119 then
16120 if No (Parameter_Associations (Parent (N2))) then
16121 if Is_Global (Entity (Name (Parent (N2)))) then
16122 Change_Selected_Component_To_Expanded_Name (Parent (N));
16123 Set_Associated_Node (Parent (N), Name (Parent (N2)));
16124 Set_Global_Type (Parent (N), Name (Parent (N2)));
16125 Save_Entity_Descendants (N);
16127 else
16128 Set_Is_Prefixed_Call (Parent (N));
16129 Set_Associated_Node (N, Empty);
16130 Set_Etype (N, Empty);
16131 end if;
16133 -- In Ada 2005, X.F may be a call to a primitive operation,
16134 -- rewritten as F (X). This rewriting will be done again in an
16135 -- instance, so keep the original node. Global entities will be
16136 -- captured as for other constructs. Indicate that this must
16137 -- resolve as a call, to prevent accidental overloading in the
16138 -- instance, if both a component and a primitive operation appear
16139 -- as candidates.
16141 else
16142 Set_Is_Prefixed_Call (Parent (N));
16143 end if;
16145 -- Entity is local. Reset in generic unit, so that node is resolved
16146 -- anew at the point of instantiation.
16148 else
16149 Set_Associated_Node (N, Empty);
16150 Set_Etype (N, Empty);
16151 end if;
16152 end Reset_Entity;
16154 -----------------------------
16155 -- Save_Entity_Descendants --
16156 -----------------------------
16158 procedure Save_Entity_Descendants (N : Node_Id) is
16159 begin
16160 case Nkind (N) is
16161 when N_Binary_Op =>
16162 Save_Global_Descendant (Union_Id (Left_Opnd (N)));
16163 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
16165 when N_Unary_Op =>
16166 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
16168 when N_Expanded_Name
16169 | N_Selected_Component
16171 Save_Global_Descendant (Union_Id (Prefix (N)));
16172 Save_Global_Descendant (Union_Id (Selector_Name (N)));
16174 when N_Character_Literal
16175 | N_Identifier
16176 | N_Operator_Symbol
16178 null;
16180 when others =>
16181 raise Program_Error;
16182 end case;
16183 end Save_Entity_Descendants;
16185 --------------------------
16186 -- Save_Global_Defaults --
16187 --------------------------
16189 procedure Save_Global_Defaults (N1 : Node_Id; N2 : Node_Id) is
16190 Loc : constant Source_Ptr := Sloc (N1);
16191 Assoc2 : constant List_Id := Generic_Associations (N2);
16192 Gen_Id : constant Entity_Id := Get_Generic_Entity (N2);
16193 Assoc1 : List_Id;
16194 Act1 : Node_Id;
16195 Act2 : Node_Id;
16196 Def : Node_Id;
16197 Ndec : Node_Id;
16198 Subp : Entity_Id;
16199 Actual : Entity_Id;
16201 begin
16202 Assoc1 := Generic_Associations (N1);
16204 if Present (Assoc1) then
16205 Act1 := First (Assoc1);
16206 else
16207 Act1 := Empty;
16208 Set_Generic_Associations (N1, New_List);
16209 Assoc1 := Generic_Associations (N1);
16210 end if;
16212 if Present (Assoc2) then
16213 Act2 := First (Assoc2);
16214 else
16215 return;
16216 end if;
16218 while Present (Act1) and then Present (Act2) loop
16219 Next (Act1);
16220 Next (Act2);
16221 end loop;
16223 -- Find the associations added for default subprograms
16225 if Present (Act2) then
16226 while Nkind (Act2) /= N_Generic_Association
16227 or else No (Entity (Selector_Name (Act2)))
16228 or else not Is_Overloadable (Entity (Selector_Name (Act2)))
16229 loop
16230 Next (Act2);
16231 end loop;
16233 -- Add a similar association if the default is global. The
16234 -- renaming declaration for the actual has been analyzed, and
16235 -- its alias is the program it renames. Link the actual in the
16236 -- original generic tree with the node in the analyzed tree.
16238 while Present (Act2) loop
16239 Subp := Entity (Selector_Name (Act2));
16240 Def := Explicit_Generic_Actual_Parameter (Act2);
16242 -- Following test is defence against rubbish errors
16244 if No (Alias (Subp)) then
16245 return;
16246 end if;
16248 -- Retrieve the resolved actual from the renaming declaration
16249 -- created for the instantiated formal.
16251 Actual := Entity (Name (Parent (Parent (Subp))));
16252 Set_Entity (Def, Actual);
16253 Set_Etype (Def, Etype (Actual));
16255 if Is_Global (Actual) then
16256 Ndec :=
16257 Make_Generic_Association (Loc,
16258 Selector_Name =>
16259 New_Occurrence_Of (Subp, Loc),
16260 Explicit_Generic_Actual_Parameter =>
16261 New_Occurrence_Of (Actual, Loc));
16263 Set_Associated_Node
16264 (Explicit_Generic_Actual_Parameter (Ndec), Def);
16266 Append (Ndec, Assoc1);
16268 -- If there are other defaults, add a dummy association in case
16269 -- there are other defaulted formals with the same name.
16271 elsif Present (Next (Act2)) then
16272 Ndec :=
16273 Make_Generic_Association (Loc,
16274 Selector_Name =>
16275 New_Occurrence_Of (Subp, Loc),
16276 Explicit_Generic_Actual_Parameter => Empty);
16278 Append (Ndec, Assoc1);
16279 end if;
16281 Next (Act2);
16282 end loop;
16283 end if;
16285 if Nkind (Name (N1)) = N_Identifier
16286 and then Is_Child_Unit (Gen_Id)
16287 and then Is_Global (Gen_Id)
16288 and then Is_Generic_Unit (Scope (Gen_Id))
16289 and then In_Open_Scopes (Scope (Gen_Id))
16290 then
16291 -- This is an instantiation of a child unit within a sibling, so
16292 -- that the generic parent is in scope. An eventual instance must
16293 -- occur within the scope of an instance of the parent. Make name
16294 -- in instance into an expanded name, to preserve the identifier
16295 -- of the parent, so it can be resolved subsequently.
16297 Rewrite (Name (N2),
16298 Make_Expanded_Name (Loc,
16299 Chars => Chars (Gen_Id),
16300 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
16301 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
16302 Set_Entity (Name (N2), Gen_Id);
16304 Rewrite (Name (N1),
16305 Make_Expanded_Name (Loc,
16306 Chars => Chars (Gen_Id),
16307 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
16308 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
16310 Set_Associated_Node (Name (N1), Name (N2));
16311 Set_Associated_Node (Prefix (Name (N1)), Empty);
16312 Set_Associated_Node
16313 (Selector_Name (Name (N1)), Selector_Name (Name (N2)));
16314 Set_Etype (Name (N1), Etype (Gen_Id));
16315 end if;
16316 end Save_Global_Defaults;
16318 ----------------------------
16319 -- Save_Global_Descendant --
16320 ----------------------------
16322 procedure Save_Global_Descendant (D : Union_Id) is
16323 N1 : Node_Id;
16325 begin
16326 if D in Node_Range then
16327 if D = Union_Id (Empty) then
16328 null;
16330 elsif Nkind (Node_Id (D)) /= N_Compilation_Unit then
16331 Save_References (Node_Id (D));
16332 end if;
16334 elsif D in List_Range then
16335 pragma Assert (D /= Union_Id (No_List));
16336 -- Because No_List = Empty, which is in Node_Range above
16338 N1 := First (List_Id (D));
16339 while Present (N1) loop
16340 Save_References (N1);
16341 Next (N1);
16342 end loop;
16344 -- Element list or other non-node field, nothing to do
16346 else
16347 null;
16348 end if;
16349 end Save_Global_Descendant;
16351 ---------------------
16352 -- Save_References --
16353 ---------------------
16355 -- This is the recursive procedure that does the work once the enclosing
16356 -- generic scope has been established. We have to treat specially a
16357 -- number of node rewritings that are required by semantic processing
16358 -- and which change the kind of nodes in the generic copy: typically
16359 -- constant-folding, replacing an operator node by a string literal, or
16360 -- a selected component by an expanded name. In each of those cases, the
16361 -- transformation is propagated to the generic unit.
16363 procedure Save_References (N : Node_Id) is
16364 Loc : constant Source_Ptr := Sloc (N);
16366 function Requires_Delayed_Save (Nod : Node_Id) return Boolean;
16367 -- Determine whether arbitrary node Nod requires delayed capture of
16368 -- global references within its aspect specifications.
16370 procedure Save_References_In_Aggregate (N : Node_Id);
16371 -- Save all global references in [extension] aggregate node N
16373 procedure Save_References_In_Char_Lit_Or_Op_Symbol (N : Node_Id);
16374 -- Save all global references in a character literal or operator
16375 -- symbol denoted by N.
16377 procedure Save_References_In_Descendants (N : Node_Id);
16378 -- Save all global references in all descendants of node N
16380 procedure Save_References_In_Identifier (N : Node_Id);
16381 -- Save all global references in identifier node N
16383 procedure Save_References_In_Operator (N : Node_Id);
16384 -- Save all global references in operator node N
16386 procedure Save_References_In_Pragma (Prag : Node_Id);
16387 -- Save all global references found within the expression of pragma
16388 -- Prag.
16390 ---------------------------
16391 -- Requires_Delayed_Save --
16392 ---------------------------
16394 function Requires_Delayed_Save (Nod : Node_Id) return Boolean is
16395 begin
16396 -- Generic packages and subprograms require delayed capture of
16397 -- global references within their aspects due to the timing of
16398 -- annotation analysis.
16400 if Nkind (Nod) in N_Generic_Package_Declaration
16401 | N_Generic_Subprogram_Declaration
16402 | N_Package_Body
16403 | N_Package_Body_Stub
16404 | N_Subprogram_Body
16405 | N_Subprogram_Body_Stub
16406 then
16407 -- Since the capture of global references is done on the
16408 -- unanalyzed generic template, there is no information around
16409 -- to infer the context. Use the Associated_Entity linkages to
16410 -- peek into the analyzed generic copy and determine what the
16411 -- template corresponds to.
16413 if Nod = Templ then
16414 return
16415 Is_Generic_Declaration_Or_Body
16416 (Unit_Declaration_Node
16417 (Associated_Entity (Defining_Entity (Nod))));
16419 -- Otherwise the generic unit being processed is not the top
16420 -- level template. It is safe to capture of global references
16421 -- within the generic unit because at this point the top level
16422 -- copy is fully analyzed.
16424 else
16425 return False;
16426 end if;
16428 -- Otherwise capture the global references without interference
16430 else
16431 return False;
16432 end if;
16433 end Requires_Delayed_Save;
16435 ----------------------------------
16436 -- Save_References_In_Aggregate --
16437 ----------------------------------
16439 procedure Save_References_In_Aggregate (N : Node_Id) is
16440 Nam : Node_Id;
16441 Qual : Node_Id := Empty;
16442 Typ : Entity_Id := Empty;
16444 begin
16445 N2 := Get_Associated_Node (N);
16447 if Present (N2) then
16448 Typ := Etype (N2);
16450 -- In an instance within a generic, use the name of the actual
16451 -- and not the original generic parameter. If the actual is
16452 -- global in the current generic it must be preserved for its
16453 -- instantiation.
16455 if Parent_Kind (Typ) = N_Subtype_Declaration
16456 and then Present (Generic_Parent_Type (Parent (Typ)))
16457 then
16458 Typ := Base_Type (Typ);
16459 Set_Etype (N2, Typ);
16460 end if;
16461 end if;
16463 if No (N2) or else No (Typ) or else not Is_Global (Typ) then
16464 Set_Associated_Node (N, Empty);
16466 -- If the aggregate is an actual in a call, it has been
16467 -- resolved in the current context, to some local type. The
16468 -- enclosing call may have been disambiguated by the aggregate,
16469 -- and this disambiguation might fail at instantiation time
16470 -- because the type to which the aggregate did resolve is not
16471 -- preserved. In order to preserve some of this information,
16472 -- wrap the aggregate in a qualified expression, using the id
16473 -- of its type. For further disambiguation we qualify the type
16474 -- name with its scope (if visible and not hidden by a local
16475 -- homograph) because both id's will have corresponding
16476 -- entities in an instance. This resolves most of the problems
16477 -- with missing type information on aggregates in instances.
16479 if Present (N2)
16480 and then Nkind (N2) = Nkind (N)
16481 and then Nkind (Parent (N2)) in N_Subprogram_Call
16482 and then Present (Typ)
16483 and then Comes_From_Source (Typ)
16484 then
16485 Nam := Make_Identifier (Loc, Chars (Typ));
16487 if Is_Immediately_Visible (Scope (Typ))
16488 and then
16489 (not In_Open_Scopes (Scope (Typ))
16490 or else Current_Entity (Scope (Typ)) = Scope (Typ))
16491 then
16492 Nam :=
16493 Make_Selected_Component (Loc,
16494 Prefix =>
16495 Make_Identifier (Loc, Chars (Scope (Typ))),
16496 Selector_Name => Nam);
16497 end if;
16499 Qual :=
16500 Make_Qualified_Expression (Loc,
16501 Subtype_Mark => Nam,
16502 Expression => Relocate_Node (N));
16503 end if;
16504 end if;
16506 if Nkind (N) = N_Aggregate then
16507 Save_Global_Descendant (Union_Id (Aggregate_Bounds (N)));
16509 elsif Nkind (N) = N_Extension_Aggregate then
16510 Save_Global_Descendant (Union_Id (Ancestor_Part (N)));
16512 else
16513 pragma Assert (False);
16514 end if;
16516 Save_Global_Descendant (Union_Id (Expressions (N)));
16517 Save_Global_Descendant (Union_Id (Component_Associations (N)));
16518 Save_Global_Descendant (Union_Id (Etype (N)));
16520 if Present (Qual) then
16521 Rewrite (N, Qual);
16522 end if;
16523 end Save_References_In_Aggregate;
16525 ----------------------------------------------
16526 -- Save_References_In_Char_Lit_Or_Op_Symbol --
16527 ----------------------------------------------
16529 procedure Save_References_In_Char_Lit_Or_Op_Symbol (N : Node_Id) is
16530 begin
16531 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
16532 Reset_Entity (N);
16534 elsif Nkind (N) = N_Operator_Symbol
16535 and then Nkind (Get_Associated_Node (N)) = N_String_Literal
16536 then
16537 Change_Operator_Symbol_To_String_Literal (N);
16538 end if;
16539 end Save_References_In_Char_Lit_Or_Op_Symbol;
16541 ------------------------------------
16542 -- Save_References_In_Descendants --
16543 ------------------------------------
16545 procedure Save_References_In_Descendants (N : Node_Id) is
16546 procedure Walk is new Walk_Sinfo_Fields (Save_Global_Descendant);
16547 begin
16548 Walk (N);
16549 end Save_References_In_Descendants;
16551 -----------------------------------
16552 -- Save_References_In_Identifier --
16553 -----------------------------------
16555 procedure Save_References_In_Identifier (N : Node_Id) is
16556 begin
16557 -- The node did not undergo a transformation
16559 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
16560 -- If this is a discriminant reference, always save it.
16561 -- It is used in the instance to find the corresponding
16562 -- discriminant positionally rather than by name.
16564 Set_Original_Discriminant
16565 (N, Original_Discriminant (Get_Associated_Node (N)));
16567 Reset_Entity (N);
16569 -- The analysis of the generic copy transformed the identifier
16570 -- into another construct. Propagate the changes to the template.
16572 else
16573 N2 := Get_Associated_Node (N);
16575 -- The identifier denotes a call to a parameterless function.
16576 -- Mark the node as resolved when the function is external.
16578 if Nkind (N2) = N_Function_Call then
16579 E := Entity (Name (N2));
16581 if Present (E) and then Is_Global (E) then
16582 Set_Etype (N, Etype (N2));
16583 else
16584 Set_Associated_Node (N, Empty);
16585 Set_Etype (N, Empty);
16586 end if;
16588 -- The identifier denotes a named number that was constant
16589 -- folded. Preserve the original name for ASIS and undo the
16590 -- constant folding which will be repeated in the instance.
16591 -- Is this still needed???
16593 elsif Nkind (N2) in N_Integer_Literal | N_Real_Literal
16594 and then Is_Entity_Name (Original_Node (N2))
16595 then
16596 Set_Associated_Node (N, Original_Node (N2));
16597 Reset_Entity (N);
16599 -- The identifier resolved to a string literal. Propagate this
16600 -- information to the generic template.
16602 elsif Nkind (N2) = N_String_Literal then
16603 Rewrite (N, New_Copy (N2));
16605 -- The identifier is rewritten as a dereference if it is the
16606 -- prefix of an implicit dereference. Preserve the original
16607 -- tree as the analysis of the instance will expand the node
16608 -- again, but preserve the resolved entity if it is global.
16610 elsif Nkind (N2) = N_Explicit_Dereference then
16611 if Is_Entity_Name (Prefix (N2))
16612 and then Present (Entity (Prefix (N2)))
16613 and then Is_Global (Entity (Prefix (N2)))
16614 then
16615 Set_Associated_Node (N, Prefix (N2));
16617 elsif Nkind (Prefix (N2)) = N_Function_Call
16618 and then Present (Entity (Name (Prefix (N2))))
16619 and then Is_Global (Entity (Name (Prefix (N2))))
16620 then
16621 Rewrite (N,
16622 Make_Explicit_Dereference (Loc,
16623 Prefix =>
16624 Make_Function_Call (Loc,
16625 Name =>
16626 New_Occurrence_Of
16627 (Entity (Name (Prefix (N2))), Loc))));
16629 else
16630 Set_Associated_Node (N, Empty);
16631 Set_Etype (N, Empty);
16632 end if;
16634 -- The subtype mark of a nominally unconstrained object is
16635 -- rewritten as a subtype indication using the bounds of the
16636 -- expression. Recover the original subtype mark.
16638 elsif Nkind (N2) = N_Subtype_Indication
16639 and then Is_Entity_Name (Original_Node (N2))
16640 then
16641 Set_Associated_Node (N, Original_Node (N2));
16642 Reset_Entity (N);
16643 end if;
16644 end if;
16645 end Save_References_In_Identifier;
16647 ---------------------------------
16648 -- Save_References_In_Operator --
16649 ---------------------------------
16651 procedure Save_References_In_Operator (N : Node_Id) is
16652 begin
16653 -- The node did not undergo a transformation
16655 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
16656 if Nkind (N) = N_Op_Concat then
16657 Set_Is_Component_Left_Opnd (N,
16658 Is_Component_Left_Opnd (Get_Associated_Node (N)));
16660 Set_Is_Component_Right_Opnd (N,
16661 Is_Component_Right_Opnd (Get_Associated_Node (N)));
16662 end if;
16664 Reset_Entity (N);
16666 -- The analysis of the generic copy transformed the operator into
16667 -- some other construct. Propagate the changes to the template if
16668 -- applicable.
16670 else
16671 N2 := Get_Associated_Node (N);
16673 -- The operator resoved to a function call
16675 if Nkind (N2) = N_Function_Call then
16677 -- Add explicit qualifications in the generic template for
16678 -- all operands of universal type. This aids resolution by
16679 -- preserving the actual type of a literal or an attribute
16680 -- that yields a universal result.
16682 Qualify_Universal_Operands (N, N2);
16684 E := Entity (Name (N2));
16686 if Present (E) and then Is_Global (E) then
16687 Set_Etype (N, Etype (N2));
16688 else
16689 Set_Associated_Node (N, Empty);
16690 Set_Etype (N, Empty);
16691 end if;
16693 -- The operator was folded into a literal
16695 elsif Nkind (N2) in N_Integer_Literal
16696 | N_Real_Literal
16697 | N_String_Literal
16698 then
16699 if Present (Original_Node (N2))
16700 and then Nkind (Original_Node (N2)) = Nkind (N)
16701 then
16702 -- Operation was constant-folded. Whenever possible,
16703 -- recover semantic information from unfolded node.
16704 -- This was initially done for ASIS but is apparently
16705 -- needed also for e.g. compiling a-nbnbin.adb.
16707 Set_Associated_Node (N, Original_Node (N2));
16709 if Nkind (N) = N_Op_Concat then
16710 Set_Is_Component_Left_Opnd (N,
16711 Is_Component_Left_Opnd (Get_Associated_Node (N)));
16712 Set_Is_Component_Right_Opnd (N,
16713 Is_Component_Right_Opnd (Get_Associated_Node (N)));
16714 end if;
16716 Reset_Entity (N);
16718 -- Propagate the constant folding back to the template
16720 else
16721 Rewrite (N, New_Copy (N2));
16722 Set_Analyzed (N, False);
16723 end if;
16725 -- The operator was folded into an enumeration literal. Retain
16726 -- the entity to avoid spurious ambiguities if it is overloaded
16727 -- at the point of instantiation or inlining.
16729 elsif Nkind (N2) = N_Identifier
16730 and then Ekind (Entity (N2)) = E_Enumeration_Literal
16731 then
16732 Rewrite (N, New_Copy (N2));
16733 Set_Analyzed (N, False);
16734 end if;
16735 end if;
16737 -- Complete the operands check if node has not been constant
16738 -- folded.
16740 if Nkind (N) in N_Op then
16741 Save_Entity_Descendants (N);
16742 end if;
16743 end Save_References_In_Operator;
16745 -------------------------------
16746 -- Save_References_In_Pragma --
16747 -------------------------------
16749 procedure Save_References_In_Pragma (Prag : Node_Id) is
16750 Context : Node_Id;
16751 Do_Save : Boolean := True;
16753 begin
16754 -- Do not save global references in pragmas generated from aspects
16755 -- because the pragmas will be regenerated at instantiation time.
16757 if From_Aspect_Specification (Prag) then
16758 Do_Save := False;
16760 -- The capture of global references within contract-related source
16761 -- pragmas associated with generic packages, subprograms or their
16762 -- respective bodies must be delayed due to timing of annotation
16763 -- analysis. Global references are still captured in routine
16764 -- Save_Global_References_In_Contract.
16766 elsif Is_Generic_Contract_Pragma (Prag) and then Prag /= Templ then
16767 if Is_Package_Contract_Annotation (Prag) then
16768 Context := Find_Related_Package_Or_Body (Prag);
16769 else
16770 pragma Assert (Is_Subprogram_Contract_Annotation (Prag));
16771 Context := Find_Related_Declaration_Or_Body (Prag);
16772 end if;
16774 -- The use of Original_Node accounts for the case when the
16775 -- related context is generic template.
16777 if Requires_Delayed_Save (Original_Node (Context)) then
16778 Do_Save := False;
16779 end if;
16780 end if;
16782 -- For all other cases, save all global references within the
16783 -- descendants, but skip the following semantic fields:
16784 -- Next_Pragma, Corresponding_Aspect, Next_Rep_Item.
16786 if Do_Save then
16787 Save_Global_Descendant
16788 (Union_Id (Pragma_Argument_Associations (N)));
16789 Save_Global_Descendant (Union_Id (Pragma_Identifier (N)));
16790 end if;
16791 end Save_References_In_Pragma;
16793 -- Start of processing for Save_References
16795 begin
16796 if N = Empty then
16797 null;
16799 -- Aggregates
16801 elsif Nkind (N) in N_Aggregate | N_Extension_Aggregate then
16802 Save_References_In_Aggregate (N);
16804 -- Character literals, operator symbols
16806 elsif Nkind (N) in N_Character_Literal | N_Operator_Symbol then
16807 Save_References_In_Char_Lit_Or_Op_Symbol (N);
16809 -- Defining identifiers
16811 elsif Nkind (N) in N_Entity then
16812 null;
16814 -- Identifiers
16816 elsif Nkind (N) = N_Identifier then
16817 Save_References_In_Identifier (N);
16819 -- Operators
16821 elsif Nkind (N) in N_Op then
16822 Save_References_In_Operator (N);
16824 -- Pragmas
16826 elsif Nkind (N) = N_Pragma then
16827 Save_References_In_Pragma (N);
16829 else
16830 Save_References_In_Descendants (N);
16831 end if;
16833 -- Save all global references found within the aspect specifications
16834 -- of the related node.
16836 if Permits_Aspect_Specifications (N) and then Has_Aspects (N) then
16838 -- The capture of global references within aspects associated with
16839 -- generic packages, subprograms or their bodies must be delayed
16840 -- due to timing of annotation analysis. Global references are
16841 -- still captured in routine Save_Global_References_In_Contract.
16843 if Requires_Delayed_Save (N) then
16844 null;
16846 -- Otherwise save all global references within the aspects
16848 else
16849 Save_Global_References_In_Aspects (N);
16850 end if;
16851 end if;
16852 end Save_References;
16854 -- Start of processing for Save_Global_References
16856 begin
16857 Gen_Scope := Current_Scope;
16859 -- If the generic unit is a child unit, references to entities in the
16860 -- parent are treated as local, because they will be resolved anew in
16861 -- the context of the instance of the parent.
16863 while Is_Child_Unit (Gen_Scope)
16864 and then Ekind (Scope (Gen_Scope)) = E_Generic_Package
16865 loop
16866 Gen_Scope := Scope (Gen_Scope);
16867 end loop;
16869 Save_References (Templ);
16870 end Save_Global_References;
16872 ---------------------------------------
16873 -- Save_Global_References_In_Aspects --
16874 ---------------------------------------
16876 procedure Save_Global_References_In_Aspects (N : Node_Id) is
16877 Asp : Node_Id;
16878 Expr : Node_Id;
16880 begin
16881 Asp := First (Aspect_Specifications (N));
16882 while Present (Asp) loop
16883 Expr := Expression (Asp);
16885 if Present (Expr) then
16886 Save_Global_References (Expr);
16887 end if;
16889 Next (Asp);
16890 end loop;
16891 end Save_Global_References_In_Aspects;
16893 ------------------------------------------
16894 -- Set_Copied_Sloc_For_Inherited_Pragma --
16895 ------------------------------------------
16897 procedure Set_Copied_Sloc_For_Inherited_Pragma
16898 (N : Node_Id;
16899 E : Entity_Id)
16901 begin
16902 Create_Instantiation_Source (N, E,
16903 Inlined_Body => False,
16904 Inherited_Pragma => True,
16905 Factor => S_Adjustment);
16906 end Set_Copied_Sloc_For_Inherited_Pragma;
16908 --------------------------------------
16909 -- Set_Copied_Sloc_For_Inlined_Body --
16910 --------------------------------------
16912 procedure Set_Copied_Sloc_For_Inlined_Body (N : Node_Id; E : Entity_Id) is
16913 begin
16914 Create_Instantiation_Source (N, E,
16915 Inlined_Body => True,
16916 Inherited_Pragma => False,
16917 Factor => S_Adjustment);
16918 end Set_Copied_Sloc_For_Inlined_Body;
16920 ---------------------
16921 -- Set_Instance_Of --
16922 ---------------------
16924 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id) is
16925 begin
16926 Generic_Renamings.Table (Generic_Renamings.Last) := (A, B, Assoc_Null);
16927 Generic_Renamings_HTable.Set (Generic_Renamings.Last);
16928 Generic_Renamings.Increment_Last;
16929 end Set_Instance_Of;
16931 --------------------
16932 -- Set_Next_Assoc --
16933 --------------------
16935 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr) is
16936 begin
16937 Generic_Renamings.Table (E).Next_In_HTable := Next;
16938 end Set_Next_Assoc;
16940 -------------------
16941 -- Start_Generic --
16942 -------------------
16944 procedure Start_Generic is
16945 begin
16946 -- ??? More things could be factored out in this routine.
16947 -- Should probably be done at a later stage.
16949 Generic_Flags.Append (Inside_A_Generic);
16950 Inside_A_Generic := True;
16952 Expander_Mode_Save_And_Set (False);
16953 end Start_Generic;
16955 ----------------------
16956 -- Set_Instance_Env --
16957 ----------------------
16959 -- WARNING: This routine manages SPARK regions
16961 procedure Set_Instance_Env
16962 (Gen_Unit : Entity_Id;
16963 Act_Unit : Entity_Id)
16965 Saved_AE : constant Boolean := Assertions_Enabled;
16966 Saved_CPL : constant Node_Id := Check_Policy_List;
16967 Saved_DEC : constant Boolean := Dynamic_Elaboration_Checks;
16968 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
16969 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
16971 begin
16972 -- Regardless of the current mode, predefined units are analyzed in the
16973 -- most current Ada mode, and earlier version Ada checks do not apply
16974 -- to predefined units. Nothing needs to be done for non-internal units.
16975 -- These are always analyzed in the current mode.
16977 if In_Internal_Unit (Gen_Unit) then
16979 -- The following call resets all configuration attributes to default
16980 -- or the xxx_Config versions of the attributes when the current sem
16981 -- unit is the main unit. At the same time, internal units must also
16982 -- inherit certain configuration attributes from their context. It
16983 -- is unclear what these two sets are.
16985 Set_Config_Switches (True, Current_Sem_Unit = Main_Unit);
16987 -- Reinstall relevant configuration attributes of the context
16989 Assertions_Enabled := Saved_AE;
16990 Check_Policy_List := Saved_CPL;
16991 Dynamic_Elaboration_Checks := Saved_DEC;
16993 Install_SPARK_Mode (Saved_SM, Saved_SMP);
16994 end if;
16996 Current_Instantiated_Parent :=
16997 (Gen_Id => Gen_Unit,
16998 Act_Id => Act_Unit,
16999 Next_In_HTable => Assoc_Null);
17000 end Set_Instance_Env;
17002 -----------------
17003 -- Switch_View --
17004 -----------------
17006 procedure Switch_View (T : Entity_Id) is
17007 BT : constant Entity_Id := Base_Type (T);
17008 Priv_Elmt : Elmt_Id := No_Elmt;
17009 Priv_Sub : Entity_Id;
17011 begin
17012 -- T may be private but its base type may have been exchanged through
17013 -- some other occurrence, in which case there is nothing to switch
17014 -- besides T itself. Note that a private dependent subtype of a private
17015 -- type might not have been switched even if the base type has been,
17016 -- because of the last branch of Check_Private_View (see comment there).
17018 if not Is_Private_Type (BT) then
17019 Prepend_Elmt (Full_View (T), Exchanged_Views);
17020 Exchange_Declarations (T);
17021 return;
17022 end if;
17024 Priv_Elmt := First_Elmt (Private_Dependents (BT));
17026 if Present (Full_View (BT)) then
17027 Prepend_Elmt (Full_View (BT), Exchanged_Views);
17028 Exchange_Declarations (BT);
17029 end if;
17031 while Present (Priv_Elmt) loop
17032 Priv_Sub := Node (Priv_Elmt);
17034 if Present (Full_View (Priv_Sub)) then
17035 Prepend_Elmt (Full_View (Priv_Sub), Exchanged_Views);
17036 Exchange_Declarations (Priv_Sub);
17037 end if;
17039 Next_Elmt (Priv_Elmt);
17040 end loop;
17041 end Switch_View;
17043 -----------------
17044 -- True_Parent --
17045 -----------------
17047 function True_Parent (N : Node_Id) return Node_Id is
17048 begin
17049 if Nkind (Parent (N)) = N_Subunit then
17050 return Parent (Corresponding_Stub (Parent (N)));
17051 else
17052 return Parent (N);
17053 end if;
17054 end True_Parent;
17056 -----------------------------
17057 -- Valid_Default_Attribute --
17058 -----------------------------
17060 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id) is
17061 Attr_Id : constant Attribute_Id :=
17062 Get_Attribute_Id (Attribute_Name (Def));
17063 T : constant Entity_Id := Entity (Prefix (Def));
17064 Is_Fun : constant Boolean := (Ekind (Nam) = E_Function);
17065 F : Entity_Id;
17066 Num_F : Nat;
17067 OK : Boolean;
17069 begin
17070 if No (T) or else T = Any_Id then
17071 return;
17072 end if;
17074 Num_F := 0;
17075 F := First_Formal (Nam);
17076 while Present (F) loop
17077 Num_F := Num_F + 1;
17078 Next_Formal (F);
17079 end loop;
17081 case Attr_Id is
17082 when Attribute_Adjacent
17083 | Attribute_Ceiling
17084 | Attribute_Copy_Sign
17085 | Attribute_Floor
17086 | Attribute_Fraction
17087 | Attribute_Machine
17088 | Attribute_Model
17089 | Attribute_Remainder
17090 | Attribute_Rounding
17091 | Attribute_Unbiased_Rounding
17093 OK := Is_Fun
17094 and then Num_F = 1
17095 and then Is_Floating_Point_Type (T);
17097 when Attribute_Image
17098 | Attribute_Pred
17099 | Attribute_Succ
17100 | Attribute_Value
17101 | Attribute_Wide_Image
17102 | Attribute_Wide_Value
17104 OK := Is_Fun and then Num_F = 1 and then Is_Scalar_Type (T);
17106 when Attribute_Max
17107 | Attribute_Min
17109 OK := Is_Fun and then Num_F = 2 and then Is_Scalar_Type (T);
17111 when Attribute_Input =>
17112 OK := (Is_Fun and then Num_F = 1);
17114 when Attribute_Output
17115 | Attribute_Put_Image
17116 | Attribute_Read
17117 | Attribute_Write
17119 OK := not Is_Fun and then Num_F = 2;
17121 when others =>
17122 OK := False;
17123 end case;
17125 if not OK then
17126 Error_Msg_N
17127 ("attribute reference has wrong profile for subprogram", Def);
17128 end if;
17129 end Valid_Default_Attribute;
17131 ----------------------------------
17132 -- Validate_Formal_Type_Default --
17133 ----------------------------------
17135 procedure Validate_Formal_Type_Default (Decl : Node_Id) is
17136 Default : constant Node_Id :=
17137 Default_Subtype_Mark (Original_Node (Decl));
17138 Formal : constant Entity_Id := Defining_Identifier (Decl);
17140 Def_Sub : Entity_Id; -- Default subtype mark
17141 Type_Def : Node_Id;
17143 procedure Check_Discriminated_Formal;
17144 -- Check that discriminants of default for private or incomplete
17145 -- type match those of formal type.
17147 function Reference_Formal (N : Node_Id) return Traverse_Result;
17148 -- Check whether formal type definition mentions a previous formal
17149 -- type of the same generic.
17151 ----------------------
17152 -- Reference_Formal --
17153 ----------------------
17155 function Reference_Formal (N : Node_Id) return Traverse_Result is
17156 begin
17157 if Is_Entity_Name (N)
17158 and then Scope (Entity (N)) = Current_Scope
17159 then
17160 return Abandon;
17161 else
17162 return OK;
17163 end if;
17164 end Reference_Formal;
17166 function Depends_On_Other_Formals is
17167 new Traverse_Func (Reference_Formal);
17169 function Default_Subtype_Matches
17170 (Gen_T, Def_T : Entity_Id) return Boolean;
17172 procedure Validate_Array_Type_Default;
17173 -- Verify that dimension, indices, and component types of default
17174 -- are compatible with formal array type definition.
17176 procedure Validate_Derived_Type_Default;
17177 -- Verify that ancestor and progenitor types match.
17179 ---------------------------------
17180 -- Check_Discriminated_Formal --
17181 ---------------------------------
17183 procedure Check_Discriminated_Formal is
17184 Formal_Discr : Entity_Id;
17185 Actual_Discr : Entity_Id;
17186 Formal_Subt : Entity_Id;
17188 begin
17189 if Has_Discriminants (Formal) then
17190 if not Has_Discriminants (Def_Sub) then
17191 Error_Msg_NE
17192 ("default for & must have discriminants", Default, Formal);
17194 elsif Is_Constrained (Def_Sub) then
17195 Error_Msg_NE
17196 ("default for & must be unconstrained", Default, Formal);
17198 else
17199 Formal_Discr := First_Discriminant (Formal);
17200 Actual_Discr := First_Discriminant (Def_Sub);
17201 while Formal_Discr /= Empty loop
17202 if Actual_Discr = Empty then
17203 Error_Msg_N
17204 ("discriminants on Formal do not match formal",
17205 Default);
17206 end if;
17208 Formal_Subt := Etype (Formal_Discr);
17210 -- Access discriminants match if designated types do
17212 if Ekind (Base_Type (Formal_Subt)) = E_Anonymous_Access_Type
17213 and then (Ekind (Base_Type (Etype (Actual_Discr)))) =
17214 E_Anonymous_Access_Type
17215 and then
17216 Designated_Type (Base_Type (Formal_Subt)) =
17217 Designated_Type (Base_Type (Etype (Actual_Discr)))
17218 then
17219 null;
17221 elsif Base_Type (Formal_Subt) /=
17222 Base_Type (Etype (Actual_Discr))
17223 then
17224 Error_Msg_N
17225 ("types of discriminants of default must match formal",
17226 Default);
17228 elsif not Subtypes_Statically_Match
17229 (Formal_Subt, Etype (Actual_Discr))
17230 and then Ada_Version >= Ada_95
17231 then
17232 Error_Msg_N
17233 ("subtypes of discriminants of default "
17234 & "must match formal",
17235 Default);
17236 end if;
17238 Next_Discriminant (Formal_Discr);
17239 Next_Discriminant (Actual_Discr);
17240 end loop;
17242 if Actual_Discr /= Empty then
17243 Error_Msg_NE
17244 ("discriminants on default do not match formal",
17245 Default, Formal);
17246 end if;
17247 end if;
17248 end if;
17249 end Check_Discriminated_Formal;
17251 ---------------------------
17252 -- Default_Subtype_Matches --
17253 ---------------------------
17255 function Default_Subtype_Matches
17256 (Gen_T, Def_T : Entity_Id) return Boolean
17258 begin
17259 -- Check that the base types, root types (when dealing with class
17260 -- wide types), or designated types (when dealing with anonymous
17261 -- access types) of Gen_T and Def_T are statically matching subtypes.
17263 return (Base_Type (Gen_T) = Base_Type (Def_T)
17264 and then Subtypes_Statically_Match (Gen_T, Def_T))
17266 or else (Is_Class_Wide_Type (Gen_T)
17267 and then Is_Class_Wide_Type (Def_T)
17268 and then Default_Subtype_Matches
17269 (Root_Type (Gen_T), Root_Type (Def_T)))
17271 or else (Is_Anonymous_Access_Type (Gen_T)
17272 and then Ekind (Def_T) = Ekind (Gen_T)
17273 and then Subtypes_Statically_Match
17274 (Designated_Type (Gen_T), Designated_Type (Def_T)));
17276 end Default_Subtype_Matches;
17278 ----------------------------------
17279 -- Validate_Array_Type_Default --
17280 ----------------------------------
17282 procedure Validate_Array_Type_Default is
17283 I1, I2 : Node_Id;
17284 T2 : Entity_Id;
17285 begin
17286 if not Is_Array_Type (Def_Sub) then
17287 Error_Msg_NE ("default for& must be an array type ",
17288 Default, Formal);
17289 return;
17291 elsif Number_Dimensions (Def_Sub) /= Number_Dimensions (Formal)
17292 or else Is_Constrained (Def_Sub) /=
17293 Is_Constrained (Formal)
17294 then
17295 Error_Msg_NE ("default array type does not match&",
17296 Default, Formal);
17297 return;
17298 end if;
17300 I1 := First_Index (Formal);
17301 I2 := First_Index (Def_Sub);
17302 for J in 1 .. Number_Dimensions (Formal) loop
17304 -- If the indexes of the actual were given by a subtype_mark,
17305 -- the index was transformed into a range attribute. Retrieve
17306 -- the original type mark for checking.
17308 if Is_Entity_Name (Original_Node (I2)) then
17309 T2 := Entity (Original_Node (I2));
17310 else
17311 T2 := Etype (I2);
17312 end if;
17314 if not Subtypes_Statically_Match (Etype (I1), T2) then
17315 Error_Msg_NE
17316 ("index types of default do not match those of formal &",
17317 Default, Formal);
17318 end if;
17320 Next_Index (I1);
17321 Next_Index (I2);
17322 end loop;
17324 if not Default_Subtype_Matches
17325 (Component_Type (Formal), Component_Type (Def_Sub))
17326 then
17327 Error_Msg_NE
17328 ("component subtype of default does not match that of formal &",
17329 Default, Formal);
17330 end if;
17332 if Has_Aliased_Components (Formal)
17333 and then not Has_Aliased_Components (Default)
17334 then
17335 Error_Msg_NE
17336 ("default must have aliased components to match formal type &",
17337 Default, Formal);
17338 end if;
17339 end Validate_Array_Type_Default;
17341 -----------------------------------
17342 -- Validate_Derived_Type_Default --
17343 -----------------------------------
17345 procedure Validate_Derived_Type_Default is
17346 begin
17347 if not Is_Ancestor (Etype (Formal), Def_Sub) then
17348 Error_Msg_NE ("default must be a descendent of&",
17349 Default, Etype (Formal));
17350 end if;
17352 if Has_Interfaces (Formal) then
17353 if not Has_Interfaces (Def_Sub) then
17354 Error_Msg_NE
17355 ("default must implement all interfaces of formal&",
17356 Default, Formal);
17358 else
17359 declare
17360 Iface : Node_Id;
17361 Iface_Ent : Entity_Id;
17363 begin
17364 Iface := First (Abstract_Interface_List (Formal));
17366 while Present (Iface) loop
17367 Iface_Ent := Entity (Iface);
17369 if Is_Ancestor (Iface_Ent, Def_Sub)
17370 or else Is_Progenitor (Iface_Ent, Def_Sub)
17371 then
17372 null;
17374 else
17375 Error_Msg_NE
17376 ("Default must implement interface&",
17377 Default, Etype (Iface));
17378 end if;
17380 Next (Iface);
17381 end loop;
17382 end;
17383 end if;
17384 end if;
17385 end Validate_Derived_Type_Default;
17387 -- Start of processing for Validate_Formal_Type_Default
17389 begin
17390 Analyze (Default);
17391 if not Is_Entity_Name (Default)
17392 or else not Is_Type (Entity (Default))
17393 then
17394 Error_Msg_N
17395 ("Expect type name for default of formal type", Default);
17396 return;
17397 else
17398 Def_Sub := Entity (Default);
17399 end if;
17401 -- Formal derived_type declarations are transformed into full
17402 -- type declarations or Private_Type_Extensions for ease of processing.
17404 if Nkind (Decl) = N_Full_Type_Declaration then
17405 Type_Def := Type_Definition (Decl);
17407 elsif Nkind (Decl) = N_Private_Extension_Declaration then
17408 Type_Def := Subtype_Indication (Decl);
17410 else
17411 Type_Def := Formal_Type_Definition (Decl);
17412 end if;
17414 if Depends_On_Other_Formals (Type_Def) = Abandon
17415 and then Scope (Def_Sub) /= Current_Scope
17416 then
17417 Error_Msg_N ("default of formal type that depends on "
17418 & "other formals must be a previous formal type", Default);
17419 return;
17421 elsif Def_Sub = Formal then
17422 Error_Msg_N
17423 ("default for formal type cannot be formal itsef", Default);
17424 return;
17425 end if;
17427 case Nkind (Type_Def) is
17429 when N_Formal_Private_Type_Definition =>
17430 if (Is_Abstract_Type (Formal)
17431 and then not Is_Abstract_Type (Def_Sub))
17432 or else (Is_Limited_Type (Formal)
17433 and then not Is_Limited_Type (Def_Sub))
17434 then
17435 Error_Msg_NE
17436 ("default for private type$ does not match",
17437 Default, Formal);
17438 end if;
17440 Check_Discriminated_Formal;
17442 when N_Formal_Derived_Type_Definition =>
17443 Check_Discriminated_Formal;
17444 Validate_Derived_Type_Default;
17446 when N_Formal_Incomplete_Type_Definition =>
17447 if Is_Tagged_Type (Formal)
17448 and then not Is_Tagged_Type (Def_Sub)
17449 then
17450 Error_Msg_NE
17451 ("default for & must be a tagged type", Default, Formal);
17452 end if;
17454 Check_Discriminated_Formal;
17456 when N_Formal_Discrete_Type_Definition =>
17457 if not Is_Discrete_Type (Def_Sub) then
17458 Error_Msg_NE ("default for& must be a discrete type",
17459 Default, Formal);
17460 end if;
17462 when N_Formal_Signed_Integer_Type_Definition =>
17463 if not Is_Integer_Type (Def_Sub) then
17464 Error_Msg_NE ("default for& must be a discrete type",
17465 Default, Formal);
17466 end if;
17468 when N_Formal_Modular_Type_Definition =>
17469 if not Is_Modular_Integer_Type (Def_Sub) then
17470 Error_Msg_NE ("default for& must be a modular_integer Type",
17471 Default, Formal);
17472 end if;
17474 when N_Formal_Floating_Point_Definition =>
17475 if not Is_Floating_Point_Type (Def_Sub) then
17476 Error_Msg_NE ("default for& must be a floating_point type",
17477 Default, Formal);
17478 end if;
17480 when N_Formal_Ordinary_Fixed_Point_Definition =>
17481 if not Is_Ordinary_Fixed_Point_Type (Def_Sub) then
17482 Error_Msg_NE ("default for& must be "
17483 & "an ordinary_fixed_point type ",
17484 Default, Formal);
17485 end if;
17487 when N_Formal_Decimal_Fixed_Point_Definition =>
17488 if not Is_Decimal_Fixed_Point_Type (Def_Sub) then
17489 Error_Msg_NE ("default for& must be "
17490 & "an Decimal_fixed_point type ",
17491 Default, Formal);
17492 end if;
17494 when N_Array_Type_Definition =>
17495 Validate_Array_Type_Default;
17497 when N_Access_Function_Definition |
17498 N_Access_Procedure_Definition =>
17499 if Ekind (Def_Sub) /= E_Access_Subprogram_Type then
17500 Error_Msg_NE ("default for& must be an Access_To_Subprogram",
17501 Default, Formal);
17502 end if;
17503 Check_Subtype_Conformant
17504 (Designated_Type (Formal), Designated_Type (Def_Sub));
17506 when N_Access_To_Object_Definition =>
17507 if not Is_Access_Object_Type (Def_Sub) then
17508 Error_Msg_NE ("default for& must be an Access_To_Object",
17509 Default, Formal);
17511 elsif not Default_Subtype_Matches
17512 (Designated_Type (Formal), Designated_Type (Def_Sub))
17513 then
17514 Error_Msg_NE ("designated type of defaul does not match "
17515 & "designated type of formal type",
17516 Default, Formal);
17517 end if;
17519 when N_Record_Definition => -- Formal interface type
17520 if not Is_Interface (Def_Sub) then
17521 Error_Msg_NE
17522 ("default for formal interface type must be an interface",
17523 Default, Formal);
17525 elsif Is_Limited_Type (Def_Sub) /= Is_Limited_Type (Formal)
17526 or else Is_Task_Interface (Formal) /= Is_Task_Interface (Def_Sub)
17527 or else Is_Protected_Interface (Formal) /=
17528 Is_Protected_Interface (Def_Sub)
17529 or else Is_Synchronized_Interface (Formal) /=
17530 Is_Synchronized_Interface (Def_Sub)
17531 then
17532 Error_Msg_NE
17533 ("default for interface& does not match", Def_Sub, Formal);
17534 end if;
17536 when N_Derived_Type_Definition =>
17537 Validate_Derived_Type_Default;
17539 when N_Identifier => -- case of a private extension
17540 Validate_Derived_Type_Default;
17542 when N_Error =>
17543 null;
17545 when others =>
17546 raise Program_Error;
17547 end case;
17548 end Validate_Formal_Type_Default;
17549 end Sem_Ch12;