Daily bump.
[official-gcc.git] / gcc / ada / sem_ch12.adb
blob83ae95e9386e021eaa1479a6e64d500dbdbcb353
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-2021, 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_Access_Definition (N : Node_Id);
520 -- Subsidiary routine to null exclusion processing. Perform an assertion
521 -- check on Ada version and the presence of an access definition in N.
523 procedure Check_Formal_Packages (P_Id : Entity_Id);
524 -- Apply the following to all formal packages in generic associations.
525 -- Restore the visibility of the formals of the instance that are not
526 -- defaulted (see RM 12.7 (10)). Remove the anonymous package declaration
527 -- created for formal instances that are not defaulted.
529 procedure Check_Formal_Package_Instance
530 (Formal_Pack : Entity_Id;
531 Actual_Pack : Entity_Id);
532 -- Verify that the actuals of the actual instance match the actuals of
533 -- the template for a formal package that is not declared with a box.
535 procedure Check_Forward_Instantiation (Decl : Node_Id);
536 -- If the generic is a local entity and the corresponding body has not
537 -- been seen yet, flag enclosing packages to indicate that it will be
538 -- elaborated after the generic body. Subprograms declared in the same
539 -- package cannot be inlined by the front end because front-end inlining
540 -- requires a strict linear order of elaboration.
542 function Check_Hidden_Primitives (Assoc_List : List_Id) return Elist_Id;
543 -- Check if some association between formals and actuals requires to make
544 -- visible primitives of a tagged type, and make those primitives visible.
545 -- Return the list of primitives whose visibility is modified (to restore
546 -- their visibility later through Restore_Hidden_Primitives). If no
547 -- candidate is found then return No_Elist.
549 procedure Check_Hidden_Child_Unit
550 (N : Node_Id;
551 Gen_Unit : Entity_Id;
552 Act_Decl_Id : Entity_Id);
553 -- If the generic unit is an implicit child instance within a parent
554 -- instance, we need to make an explicit test that it is not hidden by
555 -- a child instance of the same name and parent.
557 procedure Check_Generic_Actuals
558 (Instance : Entity_Id;
559 Is_Formal_Box : Boolean);
560 -- Similar to previous one. Check the actuals in the instantiation,
561 -- whose views can change between the point of instantiation and the point
562 -- of instantiation of the body. In addition, mark the generic renamings
563 -- as generic actuals, so that they are not compatible with other actuals.
564 -- Recurse on an actual that is a formal package whose declaration has
565 -- a box.
567 function Contains_Instance_Of
568 (Inner : Entity_Id;
569 Outer : Entity_Id;
570 N : Node_Id) return Boolean;
571 -- Inner is instantiated within the generic Outer. Check whether Inner
572 -- directly or indirectly contains an instance of Outer or of one of its
573 -- parents, in the case of a subunit. Each generic unit holds a list of
574 -- the entities instantiated within (at any depth). This procedure
575 -- determines whether the set of such lists contains a cycle, i.e. an
576 -- illegal circular instantiation.
578 function Denotes_Formal_Package
579 (Pack : Entity_Id;
580 On_Exit : Boolean := False;
581 Instance : Entity_Id := Empty) return Boolean;
582 -- Returns True if E is a formal package of an enclosing generic, or
583 -- the actual for such a formal in an enclosing instantiation. If such
584 -- a package is used as a formal in an nested generic, or as an actual
585 -- in a nested instantiation, the visibility of ITS formals should not
586 -- be modified. When called from within Restore_Private_Views, the flag
587 -- On_Exit is true, to indicate that the search for a possible enclosing
588 -- instance should ignore the current one. In that case Instance denotes
589 -- the declaration for which this is an actual. This declaration may be
590 -- an instantiation in the source, or the internal instantiation that
591 -- corresponds to the actual for a formal package.
593 function Earlier (N1, N2 : Node_Id) return Boolean;
594 -- Yields True if N1 and N2 appear in the same compilation unit,
595 -- ignoring subunits, and if N1 is to the left of N2 in a left-to-right
596 -- traversal of the tree for the unit. Used to determine the placement
597 -- of freeze nodes for instance bodies that may depend on other instances.
599 function Find_Actual_Type
600 (Typ : Entity_Id;
601 Gen_Type : Entity_Id) return Entity_Id;
602 -- When validating the actual types of a child instance, check whether
603 -- the formal is a formal type of the parent unit, and retrieve the current
604 -- actual for it. Typ is the entity in the analyzed formal type declaration
605 -- (component or index type of an array type, or designated type of an
606 -- access formal) and Gen_Type is the enclosing analyzed formal array
607 -- or access type. The desired actual may be a formal of a parent, or may
608 -- be declared in a formal package of a parent. In both cases it is a
609 -- generic actual type because it appears within a visible instance.
610 -- Finally, it may be declared in a parent unit without being a formal
611 -- of that unit, in which case it must be retrieved by visibility.
612 -- Ambiguities may still arise if two homonyms are declared in two formal
613 -- packages, and the prefix of the formal type may be needed to resolve
614 -- the ambiguity in the instance ???
616 procedure Freeze_Package_Instance
617 (N : Node_Id;
618 Gen_Body : Node_Id;
619 Gen_Decl : Node_Id;
620 Act_Id : Entity_Id);
621 -- If the instantiation happens textually before the body of the generic,
622 -- the instantiation of the body must be analyzed after the generic body,
623 -- and not at the point of instantiation. Such early instantiations can
624 -- happen if the generic and the instance appear in a package declaration
625 -- because the generic body can only appear in the corresponding package
626 -- body. Early instantiations can also appear if generic, instance and
627 -- body are all in the declarative part of a subprogram or entry. Entities
628 -- of packages that are early instantiations are delayed, and their freeze
629 -- node appears after the generic body. This rather complex machinery is
630 -- needed when nested instantiations are present, because the source does
631 -- not carry any indication of where the corresponding instance bodies must
632 -- be installed and frozen.
634 procedure Freeze_Subprogram_Instance
635 (N : Node_Id;
636 Gen_Body : Node_Id;
637 Pack_Id : Entity_Id);
638 -- The generic body may appear textually after the instance, including
639 -- in the proper body of a stub, or within a different package instance.
640 -- Given that the instance can only be elaborated after the generic, we
641 -- place freeze nodes for the instance and/or for packages that may enclose
642 -- the instance and the generic, so that the back-end can establish the
643 -- proper order of elaboration.
645 function Get_Associated_Node (N : Node_Id) return Node_Id;
646 -- In order to propagate semantic information back from the analyzed copy
647 -- to the original generic, we maintain links between selected nodes in the
648 -- generic and their corresponding copies. At the end of generic analysis,
649 -- the routine Save_Global_References traverses the generic tree, examines
650 -- the semantic information, and preserves the links to those nodes that
651 -- contain global information. At instantiation, the information from the
652 -- associated node is placed on the new copy, so that name resolution is
653 -- not repeated.
655 -- Three kinds of source nodes have associated nodes:
657 -- a) those that can reference (denote) entities, that is identifiers,
658 -- character literals, expanded_names, operator symbols, operators,
659 -- and attribute reference nodes. These nodes have an Entity field
660 -- and are the set of nodes that are in N_Has_Entity.
662 -- b) aggregates (N_Aggregate and N_Extension_Aggregate)
664 -- c) selected components (N_Selected_Component)
666 -- For the first class, the associated node preserves the entity if it is
667 -- global. If the generic contains nested instantiations, the associated
668 -- node itself has been recopied, and a chain of them must be followed.
670 -- For aggregates, the associated node allows retrieval of the type, which
671 -- may otherwise not appear in the generic. The view of this type may be
672 -- different between generic and instantiation, and the full view can be
673 -- installed before the instantiation is analyzed. For aggregates of type
674 -- extensions, the same view exchange may have to be performed for some of
675 -- the ancestor types, if their view is private at the point of
676 -- instantiation.
678 -- Nodes that are selected components in the parse tree may be rewritten
679 -- as expanded names after resolution, and must be treated as potential
680 -- entity holders, which is why they also have an Associated_Node.
682 -- Nodes that do not come from source, such as freeze nodes, do not appear
683 -- in the generic tree, and need not have an associated node.
685 -- The associated node is stored in the Associated_Node field. Note that
686 -- this field overlaps Entity, which is fine, because the whole point is
687 -- that we don't need or want the normal Entity field in this situation.
689 function Has_Been_Exchanged (E : Entity_Id) return Boolean;
690 -- Traverse the Exchanged_Views list to see if a type was private
691 -- and has already been flipped during this phase of instantiation.
693 function Has_Contracts (Decl : Node_Id) return Boolean;
694 -- Determine whether a formal subprogram has a Pre- or Postcondition,
695 -- in which case a subprogram wrapper has to be built for the actual.
697 procedure Hide_Current_Scope;
698 -- When instantiating a generic child unit, the parent context must be
699 -- present, but the instance and all entities that may be generated
700 -- must be inserted in the current scope. We leave the current scope
701 -- on the stack, but make its entities invisible to avoid visibility
702 -- problems. This is reversed at the end of the instantiation. This is
703 -- not done for the instantiation of the bodies, which only require the
704 -- instances of the generic parents to be in scope.
706 function In_Main_Context (E : Entity_Id) return Boolean;
707 -- Check whether an instantiation is in the context of the main unit.
708 -- Used to determine whether its body should be elaborated to allow
709 -- front-end inlining.
711 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id);
712 -- Add the context clause of the unit containing a generic unit to a
713 -- compilation unit that is, or contains, an instantiation.
715 procedure Init_Env;
716 -- Establish environment for subsequent instantiation. Separated from
717 -- Save_Env because data-structures for visibility handling must be
718 -- initialized before call to Check_Generic_Child_Unit.
720 procedure Inline_Instance_Body
721 (N : Node_Id;
722 Gen_Unit : Entity_Id;
723 Act_Decl : Node_Id);
724 -- If front-end inlining is requested, instantiate the package body,
725 -- and preserve the visibility of its compilation unit, to insure
726 -- that successive instantiations succeed.
728 procedure Insert_Freeze_Node_For_Instance
729 (N : Node_Id;
730 F_Node : Node_Id);
731 -- N denotes a package or a subprogram instantiation and F_Node is the
732 -- associated freeze node. Insert the freeze node before the first source
733 -- body which follows immediately after N. If no such body is found, the
734 -- freeze node is inserted at the end of the declarative region which
735 -- contains N, unless the instantiation is done in a package spec that is
736 -- not at library level, in which case it is inserted at the outer level.
737 -- This can also be invoked to insert the freeze node of a package that
738 -- encloses an instantiation, in which case N may denote an arbitrary node.
740 procedure Install_Formal_Packages (Par : Entity_Id);
741 -- Install the visible part of any formal of the parent that is a formal
742 -- package. Note that for the case of a formal package with a box, this
743 -- includes the formal part of the formal package (12.7(10/2)).
745 procedure Install_Hidden_Primitives
746 (Prims_List : in out Elist_Id;
747 Gen_T : Entity_Id;
748 Act_T : Entity_Id);
749 -- Remove suffix 'P' from hidden primitives of Act_T to match the
750 -- visibility of primitives of Gen_T. The list of primitives to which
751 -- the suffix is removed is added to Prims_List to restore them later.
753 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False);
754 -- When compiling an instance of a child unit the parent (which is
755 -- itself an instance) is an enclosing scope that must be made
756 -- immediately visible. This procedure is also used to install the non-
757 -- generic parent of a generic child unit when compiling its body, so
758 -- that full views of types in the parent are made visible.
760 -- The functions Instantiate_XXX perform various legality checks and build
761 -- the declarations for instantiated generic parameters. In all of these
762 -- Formal is the entity in the generic unit, Actual is the entity of
763 -- expression in the generic associations, and Analyzed_Formal is the
764 -- formal in the generic copy, which contains the semantic information to
765 -- be used to validate the actual.
767 function Instantiate_Object
768 (Formal : Node_Id;
769 Actual : Node_Id;
770 Analyzed_Formal : Node_Id) return List_Id;
772 function Instantiate_Type
773 (Formal : Node_Id;
774 Actual : Node_Id;
775 Analyzed_Formal : Node_Id;
776 Actual_Decls : List_Id) return List_Id;
778 function Instantiate_Formal_Subprogram
779 (Formal : Node_Id;
780 Actual : Node_Id;
781 Analyzed_Formal : Node_Id) return Node_Id;
783 function Instantiate_Formal_Package
784 (Formal : Node_Id;
785 Actual : Node_Id;
786 Analyzed_Formal : Node_Id) return List_Id;
787 -- If the formal package is declared with a box, special visibility rules
788 -- apply to its formals: they are in the visible part of the package. This
789 -- is true in the declarative region of the formal package, that is to say
790 -- in the enclosing generic or instantiation. For an instantiation, the
791 -- parameters of the formal package are made visible in an explicit step.
792 -- Furthermore, if the actual has a visible USE clause, these formals must
793 -- be made potentially use-visible as well. On exit from the enclosing
794 -- instantiation, the reverse must be done.
796 -- For a formal package declared without a box, there are conformance rules
797 -- that apply to the actuals in the generic declaration and the actuals of
798 -- the actual package in the enclosing instantiation. The simplest way to
799 -- apply these rules is to repeat the instantiation of the formal package
800 -- in the context of the enclosing instance, and compare the generic
801 -- associations of this instantiation with those of the actual package.
802 -- This internal instantiation only needs to contain the renamings of the
803 -- formals: the visible and private declarations themselves need not be
804 -- created.
806 -- In Ada 2005, the formal package may be only partially parameterized.
807 -- In that case the visibility step must make visible those actuals whose
808 -- corresponding formals were given with a box. A final complication
809 -- involves inherited operations from formal derived types, which must
810 -- be visible if the type is.
812 function Is_In_Main_Unit (N : Node_Id) return Boolean;
813 -- Test if given node is in the main unit
815 procedure Load_Parent_Of_Generic
816 (N : Node_Id;
817 Spec : Node_Id;
818 Body_Optional : Boolean := False);
819 -- If the generic appears in a separate non-generic library unit, load the
820 -- corresponding body to retrieve the body of the generic. N is the node
821 -- for the generic instantiation, Spec is the generic package declaration.
823 -- Body_Optional is a flag that indicates that the body is being loaded to
824 -- ensure that temporaries are generated consistently when there are other
825 -- instances in the current declarative part that precede the one being
826 -- loaded. In that case a missing body is acceptable.
828 procedure Map_Formal_Package_Entities (Form : Entity_Id; Act : Entity_Id);
829 -- Within the generic part, entities in the formal package are
830 -- visible. To validate subsequent type declarations, indicate
831 -- the correspondence between the entities in the analyzed formal,
832 -- and the entities in the actual package. There are three packages
833 -- involved in the instantiation of a formal package: the parent
834 -- generic P1 which appears in the generic declaration, the fake
835 -- instantiation P2 which appears in the analyzed generic, and whose
836 -- visible entities may be used in subsequent formals, and the actual
837 -- P3 in the instance. To validate subsequent formals, me indicate
838 -- that the entities in P2 are mapped into those of P3. The mapping of
839 -- entities has to be done recursively for nested packages.
841 procedure Move_Freeze_Nodes
842 (Out_Of : Entity_Id;
843 After : Node_Id;
844 L : List_Id);
845 -- Freeze nodes can be generated in the analysis of a generic unit, but
846 -- will not be seen by the back-end. It is necessary to move those nodes
847 -- to the enclosing scope if they freeze an outer entity. We place them
848 -- at the end of the enclosing generic package, which is semantically
849 -- neutral.
851 procedure Preanalyze_Actuals (N : Node_Id; Inst : Entity_Id := Empty);
852 -- Analyze actuals to perform name resolution. Full resolution is done
853 -- later, when the expected types are known, but names have to be captured
854 -- before installing parents of generics, that are not visible for the
855 -- actuals themselves.
857 -- If Inst is present, it is the entity of the package instance. This
858 -- entity is marked as having a limited_view actual when some actual is
859 -- a limited view. This is used to place the instance body properly.
861 procedure Provide_Completing_Bodies (N : Node_Id);
862 -- Generate completing bodies for all subprograms found within package or
863 -- subprogram declaration N.
865 procedure Remove_Parent (In_Body : Boolean := False);
866 -- Reverse effect after instantiation of child is complete
868 procedure Restore_Hidden_Primitives (Prims_List : in out Elist_Id);
869 -- Restore suffix 'P' to primitives of Prims_List and leave Prims_List
870 -- set to No_Elist.
872 procedure Set_Instance_Env
873 (Gen_Unit : Entity_Id;
874 Act_Unit : Entity_Id);
875 -- Save current instance on saved environment, to be used to determine
876 -- the global status of entities in nested instances. Part of Save_Env.
877 -- called after verifying that the generic unit is legal for the instance,
878 -- The procedure also examines whether the generic unit is a predefined
879 -- unit, in order to set configuration switches accordingly. As a result
880 -- the procedure must be called after analyzing and freezing the actuals.
882 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id);
883 -- Associate analyzed generic parameter with corresponding instance. Used
884 -- for semantic checks at instantiation time.
886 function True_Parent (N : Node_Id) return Node_Id;
887 -- For a subunit, return parent of corresponding stub, else return
888 -- parent of node.
890 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id);
891 -- Verify that an attribute that appears as the default for a formal
892 -- subprogram is a function or procedure with the correct profile.
894 procedure Validate_Formal_Type_Default (Decl : Node_Id);
895 -- Ada_2022 AI12-205: if a default subtype_mark is present, verify
896 -- that it is the name of a type in the same class as the formal.
897 -- The treatment parallels what is done in Instantiate_Type but differs
898 -- in a few ways so that this machinery cannot be reused as is: on one
899 -- hand there are no visibility issues for a default, because it is
900 -- analyzed in the same context as the formal type definition; on the
901 -- other hand the check needs to take into acount the use of a previous
902 -- formal type in the current formal type definition (see details in
903 -- AI12-0205).
905 -------------------------------------------
906 -- Data Structures for Generic Renamings --
907 -------------------------------------------
909 -- The map Generic_Renamings associates generic entities with their
910 -- corresponding actuals. Currently used to validate type instances. It
911 -- will eventually be used for all generic parameters to eliminate the
912 -- need for overload resolution in the instance.
914 type Assoc_Ptr is new Int;
916 Assoc_Null : constant Assoc_Ptr := -1;
918 type Assoc is record
919 Gen_Id : Entity_Id;
920 Act_Id : Entity_Id;
921 Next_In_HTable : Assoc_Ptr;
922 end record;
924 package Generic_Renamings is new Table.Table
925 (Table_Component_Type => Assoc,
926 Table_Index_Type => Assoc_Ptr,
927 Table_Low_Bound => 0,
928 Table_Initial => 10,
929 Table_Increment => 100,
930 Table_Name => "Generic_Renamings");
932 -- Variable to hold enclosing instantiation. When the environment is
933 -- saved for a subprogram inlining, the corresponding Act_Id is empty.
935 Current_Instantiated_Parent : Assoc := (Empty, Empty, Assoc_Null);
937 -- Hash table for associations
939 HTable_Size : constant := 37;
940 type HTable_Range is range 0 .. HTable_Size - 1;
942 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr);
943 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr;
944 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id;
945 function Hash (F : Entity_Id) return HTable_Range;
947 package Generic_Renamings_HTable is new GNAT.HTable.Static_HTable (
948 Header_Num => HTable_Range,
949 Element => Assoc,
950 Elmt_Ptr => Assoc_Ptr,
951 Null_Ptr => Assoc_Null,
952 Set_Next => Set_Next_Assoc,
953 Next => Next_Assoc,
954 Key => Entity_Id,
955 Get_Key => Get_Gen_Id,
956 Hash => Hash,
957 Equal => "=");
959 Exchanged_Views : Elist_Id;
960 -- This list holds the private views that have been exchanged during
961 -- instantiation to restore the visibility of the generic declaration.
962 -- (see comments above). After instantiation, the current visibility is
963 -- reestablished by means of a traversal of this list.
965 Hidden_Entities : Elist_Id;
966 -- This list holds the entities of the current scope that are removed
967 -- from immediate visibility when instantiating a child unit. Their
968 -- visibility is restored in Remove_Parent.
970 -- Because instantiations can be recursive, the following must be saved
971 -- on entry and restored on exit from an instantiation (spec or body).
972 -- This is done by the two procedures Save_Env and Restore_Env. For
973 -- package and subprogram instantiations (but not for the body instances)
974 -- the action of Save_Env is done in two steps: Init_Env is called before
975 -- Check_Generic_Child_Unit, because setting the parent instances requires
976 -- that the visibility data structures be properly initialized. Once the
977 -- generic is unit is validated, Set_Instance_Env completes Save_Env.
979 Parent_Unit_Visible : Boolean := False;
980 -- Parent_Unit_Visible is used when the generic is a child unit, and
981 -- indicates whether the ultimate parent of the generic is visible in the
982 -- instantiation environment. It is used to reset the visibility of the
983 -- parent at the end of the instantiation (see Remove_Parent).
985 Instance_Parent_Unit : Entity_Id := Empty;
986 -- This records the ultimate parent unit of an instance of a generic
987 -- child unit and is used in conjunction with Parent_Unit_Visible to
988 -- indicate the unit to which the Parent_Unit_Visible flag corresponds.
990 type Instance_Env is record
991 Instantiated_Parent : Assoc;
992 Exchanged_Views : Elist_Id;
993 Hidden_Entities : Elist_Id;
994 Current_Sem_Unit : Unit_Number_Type;
995 Parent_Unit_Visible : Boolean := False;
996 Instance_Parent_Unit : Entity_Id := Empty;
997 Switches : Config_Switches_Type;
998 end record;
1000 package Instance_Envs is new Table.Table (
1001 Table_Component_Type => Instance_Env,
1002 Table_Index_Type => Int,
1003 Table_Low_Bound => 0,
1004 Table_Initial => 32,
1005 Table_Increment => 100,
1006 Table_Name => "Instance_Envs");
1008 procedure Restore_Private_Views
1009 (Pack_Id : Entity_Id;
1010 Is_Package : Boolean := True);
1011 -- Restore the private views of external types, and unmark the generic
1012 -- renamings of actuals, so that they become compatible subtypes again.
1013 -- For subprograms, Pack_Id is the package constructed to hold the
1014 -- renamings.
1016 procedure Switch_View (T : Entity_Id);
1017 -- Switch the partial and full views of a type and its private
1018 -- dependents (i.e. its subtypes and derived types).
1020 ------------------------------------
1021 -- Structures for Error Reporting --
1022 ------------------------------------
1024 Instantiation_Node : Node_Id;
1025 -- Used by subprograms that validate instantiation of formal parameters
1026 -- where there might be no actual on which to place the error message.
1027 -- Also used to locate the instantiation node for generic subunits.
1029 Instantiation_Error : exception;
1030 -- When there is a semantic error in the generic parameter matching,
1031 -- there is no point in continuing the instantiation, because the
1032 -- number of cascaded errors is unpredictable. This exception aborts
1033 -- the instantiation process altogether.
1035 S_Adjustment : Sloc_Adjustment;
1036 -- Offset created for each node in an instantiation, in order to keep
1037 -- track of the source position of the instantiation in each of its nodes.
1038 -- A subsequent semantic error or warning on a construct of the instance
1039 -- points to both places: the original generic node, and the point of
1040 -- instantiation. See Sinput and Sinput.L for additional details.
1042 ------------------------------------------------------------
1043 -- Data structure for keeping track when inside a Generic --
1044 ------------------------------------------------------------
1046 -- The following table is used to save values of the Inside_A_Generic
1047 -- flag (see spec of Sem) when they are saved by Start_Generic.
1049 package Generic_Flags is new Table.Table (
1050 Table_Component_Type => Boolean,
1051 Table_Index_Type => Int,
1052 Table_Low_Bound => 0,
1053 Table_Initial => 32,
1054 Table_Increment => 200,
1055 Table_Name => "Generic_Flags");
1057 ---------------------------
1058 -- Abandon_Instantiation --
1059 ---------------------------
1061 procedure Abandon_Instantiation (N : Node_Id) is
1062 begin
1063 Error_Msg_N ("\instantiation abandoned!", N);
1064 raise Instantiation_Error;
1065 end Abandon_Instantiation;
1067 ----------------------------------
1068 -- Adjust_Inherited_Pragma_Sloc --
1069 ----------------------------------
1071 procedure Adjust_Inherited_Pragma_Sloc (N : Node_Id) is
1072 begin
1073 Adjust_Instantiation_Sloc (N, S_Adjustment);
1074 end Adjust_Inherited_Pragma_Sloc;
1076 --------------------------
1077 -- Analyze_Associations --
1078 --------------------------
1080 function Analyze_Associations
1081 (I_Node : Node_Id;
1082 Formals : List_Id;
1083 F_Copy : List_Id) return List_Id
1085 Actuals_To_Freeze : constant Elist_Id := New_Elmt_List;
1086 Assoc_List : constant List_Id := New_List;
1087 Default_Actuals : constant List_Id := New_List;
1088 Gen_Unit : constant Entity_Id :=
1089 Defining_Entity (Parent (F_Copy));
1091 Actuals : List_Id;
1092 Actual : Node_Id;
1093 Analyzed_Formal : Node_Id;
1094 First_Named : Node_Id := Empty;
1095 Formal : Node_Id;
1096 Match : Node_Id;
1097 Named : Node_Id;
1098 Saved_Formal : Node_Id;
1100 Default_Formals : constant List_Id := New_List;
1101 -- If an Others_Choice is present, some of the formals may be defaulted.
1102 -- To simplify the treatment of visibility in an instance, we introduce
1103 -- individual defaults for each such formal. These defaults are
1104 -- appended to the list of associations and replace the Others_Choice.
1106 Found_Assoc : Node_Id;
1107 -- Association for the current formal being match. Empty if there are
1108 -- no remaining actuals, or if there is no named association with the
1109 -- name of the formal.
1111 Is_Named_Assoc : Boolean;
1112 Num_Matched : Nat := 0;
1113 Num_Actuals : Nat := 0;
1115 Others_Present : Boolean := False;
1116 Others_Choice : Node_Id := Empty;
1117 -- In Ada 2005, indicates partial parameterization of a formal
1118 -- package. As usual an other association must be last in the list.
1120 procedure Build_Subprogram_Wrappers;
1121 -- Ada 2022: AI12-0272 introduces pre/postconditions for formal
1122 -- subprograms. The implementation of making the formal into a renaming
1123 -- of the actual does not work, given that subprogram renaming cannot
1124 -- carry aspect specifications. Instead we must create subprogram
1125 -- wrappers whose body is a call to the actual, and whose declaration
1126 -- carries the aspects of the formal.
1128 procedure Check_Fixed_Point_Actual (Actual : Node_Id);
1129 -- Warn if an actual fixed-point type has user-defined arithmetic
1130 -- operations, but there is no corresponding formal in the generic,
1131 -- in which case the predefined operations will be used. This merits
1132 -- a warning because of the special semantics of fixed point ops.
1134 procedure Check_Overloaded_Formal_Subprogram (Formal : Entity_Id);
1135 -- Apply RM 12.3(9): if a formal subprogram is overloaded, the instance
1136 -- cannot have a named association for it. AI05-0025 extends this rule
1137 -- to formals of formal packages by AI05-0025, and it also applies to
1138 -- box-initialized formals.
1140 function Has_Fully_Defined_Profile (Subp : Entity_Id) return Boolean;
1141 -- Determine whether the parameter types and the return type of Subp
1142 -- are fully defined at the point of instantiation.
1144 function Matching_Actual
1145 (F : Entity_Id;
1146 A_F : Entity_Id) return Node_Id;
1147 -- Find actual that corresponds to a given a formal parameter. If the
1148 -- actuals are positional, return the next one, if any. If the actuals
1149 -- are named, scan the parameter associations to find the right one.
1150 -- A_F is the corresponding entity in the analyzed generic, which is
1151 -- placed on the selector name.
1153 -- In Ada 2005, a named association may be given with a box, in which
1154 -- case Matching_Actual sets Found_Assoc to the generic association,
1155 -- but return Empty for the actual itself. In this case the code below
1156 -- creates a corresponding declaration for the formal.
1158 function Partial_Parameterization return Boolean;
1159 -- Ada 2005: if no match is found for a given formal, check if the
1160 -- association for it includes a box, or whether the associations
1161 -- include an Others clause.
1163 procedure Process_Default (F : Entity_Id);
1164 -- Add a copy of the declaration of generic formal F to the list of
1165 -- associations, and add an explicit box association for F if there
1166 -- is none yet, and the default comes from an Others_Choice.
1168 function Renames_Standard_Subprogram (Subp : Entity_Id) return Boolean;
1169 -- Determine whether Subp renames one of the subprograms defined in the
1170 -- generated package Standard.
1172 procedure Set_Analyzed_Formal;
1173 -- Find the node in the generic copy that corresponds to a given formal.
1174 -- The semantic information on this node is used to perform legality
1175 -- checks on the actuals. Because semantic analysis can introduce some
1176 -- anonymous entities or modify the declaration node itself, the
1177 -- correspondence between the two lists is not one-one. In addition to
1178 -- anonymous types, the presence a formal equality will introduce an
1179 -- implicit declaration for the corresponding inequality.
1181 -----------------------------------------
1182 -- procedure Build_Subprogram_Wrappers --
1183 -----------------------------------------
1185 procedure Build_Subprogram_Wrappers is
1186 Formal : constant Entity_Id :=
1187 Defining_Unit_Name (Specification (Analyzed_Formal));
1188 Aspect_Spec : Node_Id;
1189 Decl_Node : Node_Id;
1190 Actual_Name : Node_Id;
1192 begin
1193 -- Create declaration for wrapper subprogram
1194 -- The actual can be overloaded, in which case it will be
1195 -- resolved when the call in the wrapper body is analyzed.
1196 -- We attach the possible interpretations of the actual to
1197 -- the name to be used in the call in the wrapper body.
1199 if Is_Entity_Name (Match) then
1200 Actual_Name := New_Occurrence_Of (Entity (Match), Sloc (Match));
1202 if Is_Overloaded (Match) then
1203 Save_Interps (Match, Actual_Name);
1204 end if;
1206 else
1207 -- Use renaming declaration created when analyzing actual.
1208 -- This may be incomplete if there are several formal
1209 -- subprograms whose actual is an attribute ???
1211 declare
1212 Renaming_Decl : constant Node_Id := Last (Assoc_List);
1214 begin
1215 Actual_Name := New_Occurrence_Of
1216 (Defining_Entity (Renaming_Decl), Sloc (Match));
1217 Set_Etype (Actual_Name, Get_Instance_Of (Etype (Formal)));
1218 end;
1219 end if;
1221 Decl_Node := Build_Subprogram_Decl_Wrapper (Formal);
1223 -- Transfer aspect specifications from formal subprogram to wrapper
1225 Set_Aspect_Specifications (Decl_Node,
1226 New_Copy_List_Tree (Aspect_Specifications (Analyzed_Formal)));
1228 Aspect_Spec := First (Aspect_Specifications (Decl_Node));
1229 while Present (Aspect_Spec) loop
1230 Set_Analyzed (Aspect_Spec, False);
1231 Next (Aspect_Spec);
1232 end loop;
1234 Append_To (Assoc_List, Decl_Node);
1236 -- Create corresponding body, and append it to association list
1237 -- that appears at the head of the declarations in the instance.
1238 -- The subprogram may be called in the analysis of subsequent
1239 -- actuals.
1241 Append_To (Assoc_List,
1242 Build_Subprogram_Body_Wrapper (Formal, Actual_Name));
1243 end Build_Subprogram_Wrappers;
1245 ----------------------------------------
1246 -- Check_Overloaded_Formal_Subprogram --
1247 ----------------------------------------
1249 procedure Check_Overloaded_Formal_Subprogram (Formal : Entity_Id) is
1250 Temp_Formal : Entity_Id;
1252 begin
1253 Temp_Formal := First (Formals);
1254 while Present (Temp_Formal) loop
1255 if Nkind (Temp_Formal) in N_Formal_Subprogram_Declaration
1256 and then Temp_Formal /= Formal
1257 and then
1258 Chars (Defining_Unit_Name (Specification (Formal))) =
1259 Chars (Defining_Unit_Name (Specification (Temp_Formal)))
1260 then
1261 if Present (Found_Assoc) then
1262 Error_Msg_N
1263 ("named association not allowed for overloaded formal",
1264 Found_Assoc);
1266 else
1267 Error_Msg_N
1268 ("named association not allowed for overloaded formal",
1269 Others_Choice);
1270 end if;
1272 Abandon_Instantiation (Instantiation_Node);
1273 end if;
1275 Next (Temp_Formal);
1276 end loop;
1277 end Check_Overloaded_Formal_Subprogram;
1279 -------------------------------
1280 -- Check_Fixed_Point_Actual --
1281 -------------------------------
1283 procedure Check_Fixed_Point_Actual (Actual : Node_Id) is
1284 Typ : constant Entity_Id := Entity (Actual);
1285 Prims : constant Elist_Id := Collect_Primitive_Operations (Typ);
1286 Elem : Elmt_Id;
1287 Formal : Node_Id;
1288 Op : Entity_Id;
1290 begin
1291 -- Locate primitive operations of the type that are arithmetic
1292 -- operations.
1294 Elem := First_Elmt (Prims);
1295 while Present (Elem) loop
1296 if Nkind (Node (Elem)) = N_Defining_Operator_Symbol then
1298 -- Check whether the generic unit has a formal subprogram of
1299 -- the same name. This does not check types but is good enough
1300 -- to justify a warning.
1302 Formal := First_Non_Pragma (Formals);
1303 Op := Alias (Node (Elem));
1305 while Present (Formal) loop
1306 if Nkind (Formal) = N_Formal_Concrete_Subprogram_Declaration
1307 and then Chars (Defining_Entity (Formal)) =
1308 Chars (Node (Elem))
1309 then
1310 exit;
1312 elsif Nkind (Formal) = N_Formal_Package_Declaration then
1313 declare
1314 Assoc : Node_Id;
1315 Ent : Entity_Id;
1317 begin
1318 -- Locate corresponding actual, and check whether it
1319 -- includes a fixed-point type.
1321 Assoc := First (Assoc_List);
1322 while Present (Assoc) loop
1323 exit when
1324 Nkind (Assoc) = N_Package_Renaming_Declaration
1325 and then Chars (Defining_Unit_Name (Assoc)) =
1326 Chars (Defining_Identifier (Formal));
1328 Next (Assoc);
1329 end loop;
1331 if Present (Assoc) then
1333 -- If formal package declares a fixed-point type,
1334 -- and the user-defined operator is derived from
1335 -- a generic instance package, the fixed-point type
1336 -- does not use the corresponding predefined op.
1338 Ent := First_Entity (Entity (Name (Assoc)));
1339 while Present (Ent) loop
1340 if Is_Fixed_Point_Type (Ent)
1341 and then Present (Op)
1342 and then Is_Generic_Instance (Scope (Op))
1343 then
1344 return;
1345 end if;
1347 Next_Entity (Ent);
1348 end loop;
1349 end if;
1350 end;
1351 end if;
1353 Next (Formal);
1354 end loop;
1356 if No (Formal) then
1357 Error_Msg_Sloc := Sloc (Node (Elem));
1358 Error_Msg_NE
1359 ("?instance uses predefined operation, not primitive "
1360 & "operation&#", Actual, Node (Elem));
1361 end if;
1362 end if;
1364 Next_Elmt (Elem);
1365 end loop;
1366 end Check_Fixed_Point_Actual;
1368 -------------------------------
1369 -- Has_Fully_Defined_Profile --
1370 -------------------------------
1372 function Has_Fully_Defined_Profile (Subp : Entity_Id) return Boolean is
1373 function Is_Fully_Defined_Type (Typ : Entity_Id) return Boolean;
1374 -- Determine whethet type Typ is fully defined
1376 ---------------------------
1377 -- Is_Fully_Defined_Type --
1378 ---------------------------
1380 function Is_Fully_Defined_Type (Typ : Entity_Id) return Boolean is
1381 begin
1382 -- A private type without a full view is not fully defined
1384 if Is_Private_Type (Typ)
1385 and then No (Full_View (Typ))
1386 then
1387 return False;
1389 -- An incomplete type is never fully defined
1391 elsif Is_Incomplete_Type (Typ) then
1392 return False;
1394 -- All other types are fully defined
1396 else
1397 return True;
1398 end if;
1399 end Is_Fully_Defined_Type;
1401 -- Local declarations
1403 Param : Entity_Id;
1405 -- Start of processing for Has_Fully_Defined_Profile
1407 begin
1408 -- Check the parameters
1410 Param := First_Formal (Subp);
1411 while Present (Param) loop
1412 if not Is_Fully_Defined_Type (Etype (Param)) then
1413 return False;
1414 end if;
1416 Next_Formal (Param);
1417 end loop;
1419 -- Check the return type
1421 return Is_Fully_Defined_Type (Etype (Subp));
1422 end Has_Fully_Defined_Profile;
1424 ---------------------
1425 -- Matching_Actual --
1426 ---------------------
1428 function Matching_Actual
1429 (F : Entity_Id;
1430 A_F : Entity_Id) return Node_Id
1432 Prev : Node_Id;
1433 Act : Node_Id;
1435 begin
1436 Is_Named_Assoc := False;
1438 -- End of list of purely positional parameters
1440 if No (Actual) or else Nkind (Actual) = N_Others_Choice then
1441 Found_Assoc := Empty;
1442 Act := Empty;
1444 -- Case of positional parameter corresponding to current formal
1446 elsif No (Selector_Name (Actual)) then
1447 Found_Assoc := Actual;
1448 Act := Explicit_Generic_Actual_Parameter (Actual);
1449 Num_Matched := Num_Matched + 1;
1450 Next (Actual);
1452 -- Otherwise scan list of named actuals to find the one with the
1453 -- desired name. All remaining actuals have explicit names.
1455 else
1456 Is_Named_Assoc := True;
1457 Found_Assoc := Empty;
1458 Act := Empty;
1459 Prev := Empty;
1461 while Present (Actual) loop
1462 if Nkind (Actual) = N_Others_Choice then
1463 Found_Assoc := Empty;
1464 Act := Empty;
1466 elsif Chars (Selector_Name (Actual)) = Chars (F) then
1467 Set_Entity (Selector_Name (Actual), A_F);
1468 Set_Etype (Selector_Name (Actual), Etype (A_F));
1469 Generate_Reference (A_F, Selector_Name (Actual));
1471 Found_Assoc := Actual;
1472 Act := Explicit_Generic_Actual_Parameter (Actual);
1473 Num_Matched := Num_Matched + 1;
1474 exit;
1475 end if;
1477 Prev := Actual;
1478 Next (Actual);
1479 end loop;
1481 -- Reset for subsequent searches. In most cases the named
1482 -- associations are in order. If they are not, we reorder them
1483 -- to avoid scanning twice the same actual. This is not just a
1484 -- question of efficiency: there may be multiple defaults with
1485 -- boxes that have the same name. In a nested instantiation we
1486 -- insert actuals for those defaults, and cannot rely on their
1487 -- names to disambiguate them.
1489 if Actual = First_Named then
1490 Next (First_Named);
1492 elsif Present (Actual) then
1493 Insert_Before (First_Named, Remove_Next (Prev));
1494 end if;
1496 Actual := First_Named;
1497 end if;
1499 if Is_Entity_Name (Act) and then Present (Entity (Act)) then
1500 Set_Used_As_Generic_Actual (Entity (Act));
1501 end if;
1503 return Act;
1504 end Matching_Actual;
1506 ------------------------------
1507 -- Partial_Parameterization --
1508 ------------------------------
1510 function Partial_Parameterization return Boolean is
1511 begin
1512 return Others_Present
1513 or else (Present (Found_Assoc) and then Box_Present (Found_Assoc));
1514 end Partial_Parameterization;
1516 ---------------------
1517 -- Process_Default --
1518 ---------------------
1520 procedure Process_Default (F : Entity_Id) is
1521 Loc : constant Source_Ptr := Sloc (I_Node);
1522 F_Id : constant Entity_Id := Defining_Entity (F);
1523 Decl : Node_Id;
1524 Default : Node_Id;
1525 Id : Entity_Id;
1527 begin
1528 -- Append copy of formal declaration to associations, and create new
1529 -- defining identifier for it.
1531 Decl := New_Copy_Tree (F);
1532 Id := Make_Defining_Identifier (Sloc (F_Id), Chars (F_Id));
1534 if Nkind (F) in N_Formal_Subprogram_Declaration then
1535 Set_Defining_Unit_Name (Specification (Decl), Id);
1537 else
1538 Set_Defining_Identifier (Decl, Id);
1539 end if;
1541 Append (Decl, Assoc_List);
1543 if No (Found_Assoc) then
1544 Default :=
1545 Make_Generic_Association (Loc,
1546 Selector_Name =>
1547 New_Occurrence_Of (Id, Loc),
1548 Explicit_Generic_Actual_Parameter => Empty);
1549 Set_Box_Present (Default);
1550 Append (Default, Default_Formals);
1551 end if;
1552 end Process_Default;
1554 ---------------------------------
1555 -- Renames_Standard_Subprogram --
1556 ---------------------------------
1558 function Renames_Standard_Subprogram (Subp : Entity_Id) return Boolean is
1559 Id : Entity_Id;
1561 begin
1562 Id := Alias (Subp);
1563 while Present (Id) loop
1564 if Scope (Id) = Standard_Standard then
1565 return True;
1566 end if;
1568 Id := Alias (Id);
1569 end loop;
1571 return False;
1572 end Renames_Standard_Subprogram;
1574 -------------------------
1575 -- Set_Analyzed_Formal --
1576 -------------------------
1578 procedure Set_Analyzed_Formal is
1579 Kind : Node_Kind;
1581 begin
1582 while Present (Analyzed_Formal) loop
1583 Kind := Nkind (Analyzed_Formal);
1585 case Nkind (Formal) is
1586 when N_Formal_Subprogram_Declaration =>
1587 exit when Kind in N_Formal_Subprogram_Declaration
1588 and then
1589 Chars
1590 (Defining_Unit_Name (Specification (Formal))) =
1591 Chars
1592 (Defining_Unit_Name (Specification (Analyzed_Formal)));
1594 when N_Formal_Package_Declaration =>
1595 exit when Kind in N_Formal_Package_Declaration
1596 | N_Generic_Package_Declaration
1597 | N_Package_Declaration;
1599 when N_Use_Package_Clause
1600 | N_Use_Type_Clause
1602 exit;
1604 when others =>
1606 -- Skip freeze nodes, and nodes inserted to replace
1607 -- unrecognized pragmas.
1609 exit when
1610 Kind not in N_Formal_Subprogram_Declaration
1611 and then Kind not in N_Subprogram_Declaration
1612 | N_Freeze_Entity
1613 | N_Null_Statement
1614 | N_Itype_Reference
1615 and then Chars (Defining_Identifier (Formal)) =
1616 Chars (Defining_Identifier (Analyzed_Formal));
1617 end case;
1619 Next (Analyzed_Formal);
1620 end loop;
1621 end Set_Analyzed_Formal;
1623 -- Start of processing for Analyze_Associations
1625 begin
1626 Actuals := Generic_Associations (I_Node);
1628 if Present (Actuals) then
1630 -- Check for an Others choice, indicating a partial parameterization
1631 -- for a formal package.
1633 Actual := First (Actuals);
1634 while Present (Actual) loop
1635 if Nkind (Actual) = N_Others_Choice then
1636 Others_Present := True;
1637 Others_Choice := Actual;
1639 if Present (Next (Actual)) then
1640 Error_Msg_N ("OTHERS must be last association", Actual);
1641 end if;
1643 -- This subprogram is used both for formal packages and for
1644 -- instantiations. For the latter, associations must all be
1645 -- explicit.
1647 if Nkind (I_Node) /= N_Formal_Package_Declaration
1648 and then Comes_From_Source (I_Node)
1649 then
1650 Error_Msg_N
1651 ("OTHERS association not allowed in an instance",
1652 Actual);
1653 end if;
1655 -- In any case, nothing to do after the others association
1657 exit;
1659 elsif Box_Present (Actual)
1660 and then Comes_From_Source (I_Node)
1661 and then Nkind (I_Node) /= N_Formal_Package_Declaration
1662 then
1663 Error_Msg_N
1664 ("box association not allowed in an instance", Actual);
1665 end if;
1667 Next (Actual);
1668 end loop;
1670 -- If named associations are present, save first named association
1671 -- (it may of course be Empty) to facilitate subsequent name search.
1673 First_Named := First (Actuals);
1674 while Present (First_Named)
1675 and then Nkind (First_Named) /= N_Others_Choice
1676 and then No (Selector_Name (First_Named))
1677 loop
1678 Num_Actuals := Num_Actuals + 1;
1679 Next (First_Named);
1680 end loop;
1681 end if;
1683 Named := First_Named;
1684 while Present (Named) loop
1685 if Nkind (Named) /= N_Others_Choice
1686 and then No (Selector_Name (Named))
1687 then
1688 Error_Msg_N ("invalid positional actual after named one", Named);
1689 Abandon_Instantiation (Named);
1690 end if;
1692 -- A named association may lack an actual parameter, if it was
1693 -- introduced for a default subprogram that turns out to be local
1694 -- to the outer instantiation. If it has a box association it must
1695 -- correspond to some formal in the generic.
1697 if Nkind (Named) /= N_Others_Choice
1698 and then (Present (Explicit_Generic_Actual_Parameter (Named))
1699 or else Box_Present (Named))
1700 then
1701 Num_Actuals := Num_Actuals + 1;
1702 end if;
1704 Next (Named);
1705 end loop;
1707 if Present (Formals) then
1708 Formal := First_Non_Pragma (Formals);
1709 Analyzed_Formal := First_Non_Pragma (F_Copy);
1711 if Present (Actuals) then
1712 Actual := First (Actuals);
1714 -- All formals should have default values
1716 else
1717 Actual := Empty;
1718 end if;
1720 while Present (Formal) loop
1721 Set_Analyzed_Formal;
1722 Saved_Formal := Next_Non_Pragma (Formal);
1724 case Nkind (Formal) is
1725 when N_Formal_Object_Declaration =>
1726 Match :=
1727 Matching_Actual
1728 (Defining_Identifier (Formal),
1729 Defining_Identifier (Analyzed_Formal));
1731 if No (Match) and then Partial_Parameterization then
1732 Process_Default (Formal);
1734 else
1735 Append_List
1736 (Instantiate_Object (Formal, Match, Analyzed_Formal),
1737 Assoc_List);
1739 -- For a defaulted in_parameter, create an entry in the
1740 -- the list of defaulted actuals, for GNATprove use. Do
1741 -- not included these defaults for an instance nested
1742 -- within a generic, because the defaults are also used
1743 -- in the analysis of the enclosing generic, and only
1744 -- defaulted subprograms are relevant there.
1746 if No (Match) and then not Inside_A_Generic then
1747 Append_To (Default_Actuals,
1748 Make_Generic_Association (Sloc (I_Node),
1749 Selector_Name =>
1750 New_Occurrence_Of
1751 (Defining_Identifier (Formal), Sloc (I_Node)),
1752 Explicit_Generic_Actual_Parameter =>
1753 New_Copy_Tree (Default_Expression (Formal))));
1754 end if;
1755 end if;
1757 -- If the object is a call to an expression function, this
1758 -- is a freezing point for it.
1760 if Is_Entity_Name (Match)
1761 and then Present (Entity (Match))
1762 and then Nkind
1763 (Original_Node (Unit_Declaration_Node (Entity (Match))))
1764 = N_Expression_Function
1765 then
1766 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1767 end if;
1769 when N_Formal_Type_Declaration =>
1770 Match :=
1771 Matching_Actual
1772 (Defining_Identifier (Formal),
1773 Defining_Identifier (Analyzed_Formal));
1775 if No (Match) then
1776 if Partial_Parameterization then
1777 Process_Default (Formal);
1779 elsif Present (Default_Subtype_Mark (Formal)) then
1780 Match := New_Copy (Default_Subtype_Mark (Formal));
1781 Append_List
1782 (Instantiate_Type
1783 (Formal, Match, Analyzed_Formal, Assoc_List),
1784 Assoc_List);
1785 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1787 else
1788 Error_Msg_Sloc := Sloc (Gen_Unit);
1789 Error_Msg_NE
1790 ("missing actual&",
1791 Instantiation_Node, Defining_Identifier (Formal));
1792 Error_Msg_NE
1793 ("\in instantiation of & declared#",
1794 Instantiation_Node, Gen_Unit);
1795 Abandon_Instantiation (Instantiation_Node);
1796 end if;
1798 else
1799 Analyze (Match);
1800 Append_List
1801 (Instantiate_Type
1802 (Formal, Match, Analyzed_Formal, Assoc_List),
1803 Assoc_List);
1805 -- Warn when an actual is a fixed-point with user-
1806 -- defined promitives. The warning is superfluous
1807 -- if the formal is private, because there can be
1808 -- no arithmetic operations in the generic so there
1809 -- no danger of confusion.
1811 if Is_Fixed_Point_Type (Entity (Match))
1812 and then not Is_Private_Type
1813 (Defining_Identifier (Analyzed_Formal))
1814 then
1815 Check_Fixed_Point_Actual (Match);
1816 end if;
1818 -- An instantiation is a freeze point for the actuals,
1819 -- unless this is a rewritten formal package, or the
1820 -- formal is an Ada 2012 formal incomplete type.
1822 if Nkind (I_Node) = N_Formal_Package_Declaration
1823 or else
1824 (Ada_Version >= Ada_2012
1825 and then
1826 Ekind (Defining_Identifier (Analyzed_Formal)) =
1827 E_Incomplete_Type)
1828 then
1829 null;
1831 else
1832 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1833 end if;
1834 end if;
1836 -- A remote access-to-class-wide type is not a legal actual
1837 -- for a generic formal of an access type (E.2.2(17/2)).
1838 -- In GNAT an exception to this rule is introduced when
1839 -- the formal is marked as remote using implementation
1840 -- defined aspect/pragma Remote_Access_Type. In that case
1841 -- the actual must be remote as well.
1843 -- If the current instantiation is the construction of a
1844 -- local copy for a formal package the actuals may be
1845 -- defaulted, and there is no matching actual to check.
1847 if Nkind (Analyzed_Formal) = N_Formal_Type_Declaration
1848 and then
1849 Nkind (Formal_Type_Definition (Analyzed_Formal)) =
1850 N_Access_To_Object_Definition
1851 and then Present (Match)
1852 then
1853 declare
1854 Formal_Ent : constant Entity_Id :=
1855 Defining_Identifier (Analyzed_Formal);
1856 begin
1857 if Is_Remote_Access_To_Class_Wide_Type (Entity (Match))
1858 = Is_Remote_Types (Formal_Ent)
1859 then
1860 -- Remoteness of formal and actual match
1862 null;
1864 elsif Is_Remote_Types (Formal_Ent) then
1866 -- Remote formal, non-remote actual
1868 Error_Msg_NE
1869 ("actual for& must be remote", Match, Formal_Ent);
1871 else
1872 -- Non-remote formal, remote actual
1874 Error_Msg_NE
1875 ("actual for& may not be remote",
1876 Match, Formal_Ent);
1877 end if;
1878 end;
1879 end if;
1881 when N_Formal_Subprogram_Declaration =>
1882 Match :=
1883 Matching_Actual
1884 (Defining_Unit_Name (Specification (Formal)),
1885 Defining_Unit_Name (Specification (Analyzed_Formal)));
1887 -- If the formal subprogram has the same name as another
1888 -- formal subprogram of the generic, then a named
1889 -- association is illegal (12.3(9)). Exclude named
1890 -- associations that are generated for a nested instance.
1892 if Present (Match)
1893 and then Is_Named_Assoc
1894 and then Comes_From_Source (Found_Assoc)
1895 then
1896 Check_Overloaded_Formal_Subprogram (Formal);
1897 end if;
1899 -- If there is no corresponding actual, this may be case
1900 -- of partial parameterization, or else the formal has a
1901 -- default or a box.
1903 if No (Match) and then Partial_Parameterization then
1904 Process_Default (Formal);
1906 if Nkind (I_Node) = N_Formal_Package_Declaration then
1907 Check_Overloaded_Formal_Subprogram (Formal);
1908 end if;
1910 else
1911 Append_To (Assoc_List,
1912 Instantiate_Formal_Subprogram
1913 (Formal, Match, Analyzed_Formal));
1915 -- If formal subprogram has contracts, create wrappers
1916 -- for it. This is an expansion activity that cannot
1917 -- take place e.g. within an enclosing generic unit.
1919 if Has_Contracts (Analyzed_Formal)
1920 and then Expander_Active
1921 then
1922 Build_Subprogram_Wrappers;
1923 end if;
1925 -- An instantiation is a freeze point for the actuals,
1926 -- unless this is a rewritten formal package.
1928 if Nkind (I_Node) /= N_Formal_Package_Declaration
1929 and then Nkind (Match) = N_Identifier
1930 and then Is_Subprogram (Entity (Match))
1932 -- The actual subprogram may rename a routine defined
1933 -- in Standard. Avoid freezing such renamings because
1934 -- subprograms coming from Standard cannot be frozen.
1936 and then
1937 not Renames_Standard_Subprogram (Entity (Match))
1939 -- If the actual subprogram comes from a different
1940 -- unit, it is already frozen, either by a body in
1941 -- that unit or by the end of the declarative part
1942 -- of the unit. This check avoids the freezing of
1943 -- subprograms defined in Standard which are used
1944 -- as generic actuals.
1946 and then In_Same_Code_Unit (Entity (Match), I_Node)
1947 and then Has_Fully_Defined_Profile (Entity (Match))
1948 then
1949 -- Mark the subprogram as having a delayed freeze
1950 -- since this may be an out-of-order action.
1952 Set_Has_Delayed_Freeze (Entity (Match));
1953 Append_Elmt (Entity (Match), Actuals_To_Freeze);
1954 end if;
1955 end if;
1957 -- If this is a nested generic, preserve default for later
1958 -- instantiations. We do this as well for GNATprove use,
1959 -- so that the list of generic associations is complete.
1961 if No (Match) and then Box_Present (Formal) then
1962 declare
1963 Subp : constant Entity_Id :=
1964 Defining_Unit_Name
1965 (Specification (Last (Assoc_List)));
1967 begin
1968 Append_To (Default_Actuals,
1969 Make_Generic_Association (Sloc (I_Node),
1970 Selector_Name =>
1971 New_Occurrence_Of (Subp, Sloc (I_Node)),
1972 Explicit_Generic_Actual_Parameter =>
1973 New_Occurrence_Of (Subp, Sloc (I_Node))));
1974 end;
1975 end if;
1977 when N_Formal_Package_Declaration =>
1978 -- The name of the formal package may be hidden by the
1979 -- formal parameter itself.
1981 if Error_Posted (Analyzed_Formal) then
1982 Abandon_Instantiation (Instantiation_Node);
1984 else
1985 Match :=
1986 Matching_Actual
1987 (Defining_Identifier (Formal),
1988 Defining_Identifier
1989 (Original_Node (Analyzed_Formal)));
1990 end if;
1992 if No (Match) then
1993 if Partial_Parameterization then
1994 Process_Default (Formal);
1996 else
1997 Error_Msg_Sloc := Sloc (Gen_Unit);
1998 Error_Msg_NE
1999 ("missing actual&",
2000 Instantiation_Node, Defining_Identifier (Formal));
2001 Error_Msg_NE
2002 ("\in instantiation of & declared#",
2003 Instantiation_Node, Gen_Unit);
2005 Abandon_Instantiation (Instantiation_Node);
2006 end if;
2008 else
2009 Analyze (Match);
2010 Append_List
2011 (Instantiate_Formal_Package
2012 (Formal, Match, Analyzed_Formal),
2013 Assoc_List);
2015 -- Determine whether the actual package needs an explicit
2016 -- freeze node. This is only the case if the actual is
2017 -- declared in the same unit and has a body. Normally
2018 -- packages do not have explicit freeze nodes, and gigi
2019 -- only uses them to elaborate entities in a package
2020 -- body.
2022 Explicit_Freeze_Check : declare
2023 Actual : constant Entity_Id := Entity (Match);
2024 Gen_Par : Entity_Id;
2026 Needs_Freezing : Boolean;
2027 P : Node_Id;
2029 procedure Check_Generic_Parent;
2030 -- The actual may be an instantiation of a unit
2031 -- declared in a previous instantiation. If that
2032 -- one is also in the current compilation, it must
2033 -- itself be frozen before the actual. The actual
2034 -- may be an instantiation of a generic child unit,
2035 -- in which case the same applies to the instance
2036 -- of the parent which must be frozen before the
2037 -- actual.
2038 -- Should this itself be recursive ???
2040 --------------------------
2041 -- Check_Generic_Parent --
2042 --------------------------
2044 procedure Check_Generic_Parent is
2045 Inst : constant Node_Id :=
2046 Next (Unit_Declaration_Node (Actual));
2047 Par : Entity_Id;
2049 begin
2050 Par := Empty;
2052 if Nkind (Parent (Actual)) = N_Package_Specification
2053 then
2054 Par := Scope (Generic_Parent (Parent (Actual)));
2056 if Is_Generic_Instance (Par) then
2057 null;
2059 -- If the actual is a child generic unit, check
2060 -- whether the instantiation of the parent is
2061 -- also local and must also be frozen now. We
2062 -- must retrieve the instance node to locate the
2063 -- parent instance if any.
2065 elsif Ekind (Par) = E_Generic_Package
2066 and then Is_Child_Unit (Gen_Par)
2067 and then Ekind (Scope (Gen_Par)) =
2068 E_Generic_Package
2069 then
2070 if Nkind (Inst) = N_Package_Instantiation
2071 and then Nkind (Name (Inst)) =
2072 N_Expanded_Name
2073 then
2074 -- Retrieve entity of parent instance
2076 Par := Entity (Prefix (Name (Inst)));
2077 end if;
2079 else
2080 Par := Empty;
2081 end if;
2082 end if;
2084 if Present (Par)
2085 and then Is_Generic_Instance (Par)
2086 and then Scope (Par) = Current_Scope
2087 and then
2088 (No (Freeze_Node (Par))
2089 or else
2090 not Is_List_Member (Freeze_Node (Par)))
2091 then
2092 Set_Has_Delayed_Freeze (Par);
2093 Append_Elmt (Par, Actuals_To_Freeze);
2094 end if;
2095 end Check_Generic_Parent;
2097 -- Start of processing for Explicit_Freeze_Check
2099 begin
2100 if Present (Renamed_Entity (Actual)) then
2101 Gen_Par :=
2102 Generic_Parent (Specification
2103 (Unit_Declaration_Node
2104 (Renamed_Entity (Actual))));
2105 else
2106 Gen_Par :=
2107 Generic_Parent (Specification
2108 (Unit_Declaration_Node (Actual)));
2109 end if;
2111 if not Expander_Active
2112 or else not Has_Completion (Actual)
2113 or else not In_Same_Source_Unit (I_Node, Actual)
2114 or else Is_Frozen (Actual)
2115 or else
2116 (Present (Renamed_Entity (Actual))
2117 and then
2118 not In_Same_Source_Unit
2119 (I_Node, (Renamed_Entity (Actual))))
2120 then
2121 null;
2123 else
2124 -- Finally we want to exclude such freeze nodes
2125 -- from statement sequences, which freeze
2126 -- everything before them.
2127 -- Is this strictly necessary ???
2129 Needs_Freezing := True;
2131 P := Parent (I_Node);
2132 while Nkind (P) /= N_Compilation_Unit loop
2133 if Nkind (P) = N_Handled_Sequence_Of_Statements
2134 then
2135 Needs_Freezing := False;
2136 exit;
2137 end if;
2139 P := Parent (P);
2140 end loop;
2142 if Needs_Freezing then
2143 Check_Generic_Parent;
2145 -- If the actual is a renaming of a proper
2146 -- instance of the formal package, indicate
2147 -- that it is the instance that must be frozen.
2149 if Nkind (Parent (Actual)) =
2150 N_Package_Renaming_Declaration
2151 then
2152 Set_Has_Delayed_Freeze
2153 (Renamed_Entity (Actual));
2154 Append_Elmt
2155 (Renamed_Entity (Actual),
2156 Actuals_To_Freeze);
2157 else
2158 Set_Has_Delayed_Freeze (Actual);
2159 Append_Elmt (Actual, Actuals_To_Freeze);
2160 end if;
2161 end if;
2162 end if;
2163 end Explicit_Freeze_Check;
2164 end if;
2166 -- For use type and use package appearing in the generic part,
2167 -- we have already copied them, so we can just move them where
2168 -- they belong (we mustn't recopy them since this would mess up
2169 -- the Sloc values).
2171 when N_Use_Package_Clause
2172 | N_Use_Type_Clause
2174 if Nkind (Original_Node (I_Node)) =
2175 N_Formal_Package_Declaration
2176 then
2177 Append (New_Copy_Tree (Formal), Assoc_List);
2178 else
2179 Remove (Formal);
2180 Append (Formal, Assoc_List);
2181 end if;
2183 when others =>
2184 raise Program_Error;
2185 end case;
2187 Formal := Saved_Formal;
2188 Next_Non_Pragma (Analyzed_Formal);
2189 end loop;
2191 if Num_Actuals > Num_Matched then
2192 Error_Msg_Sloc := Sloc (Gen_Unit);
2194 if Present (Selector_Name (Actual)) then
2195 Error_Msg_NE
2196 ("unmatched actual &", Actual, Selector_Name (Actual));
2197 Error_Msg_NE
2198 ("\in instantiation of & declared#", Actual, Gen_Unit);
2199 else
2200 Error_Msg_NE
2201 ("unmatched actual in instantiation of & declared#",
2202 Actual, Gen_Unit);
2203 end if;
2204 end if;
2206 elsif Present (Actuals) then
2207 Error_Msg_N
2208 ("too many actuals in generic instantiation", Instantiation_Node);
2209 end if;
2211 -- An instantiation freezes all generic actuals. The only exceptions
2212 -- to this are incomplete types and subprograms which are not fully
2213 -- defined at the point of instantiation.
2215 declare
2216 Elmt : Elmt_Id := First_Elmt (Actuals_To_Freeze);
2217 begin
2218 while Present (Elmt) loop
2219 Freeze_Before (I_Node, Node (Elmt));
2220 Next_Elmt (Elmt);
2221 end loop;
2222 end;
2224 -- If there are default subprograms, normalize the tree by adding
2225 -- explicit associations for them. This is required if the instance
2226 -- appears within a generic.
2228 if not Is_Empty_List (Default_Actuals) then
2229 declare
2230 Default : Node_Id;
2232 begin
2233 Default := First (Default_Actuals);
2234 while Present (Default) loop
2235 Mark_Rewrite_Insertion (Default);
2236 Next (Default);
2237 end loop;
2239 if No (Actuals) then
2240 Set_Generic_Associations (I_Node, Default_Actuals);
2241 else
2242 Append_List_To (Actuals, Default_Actuals);
2243 end if;
2244 end;
2245 end if;
2247 -- If this is a formal package, normalize the parameter list by adding
2248 -- explicit box associations for the formals that are covered by an
2249 -- Others_Choice.
2251 if not Is_Empty_List (Default_Formals) then
2252 Append_List (Default_Formals, Formals);
2253 end if;
2255 return Assoc_List;
2256 end Analyze_Associations;
2258 -------------------------------
2259 -- Analyze_Formal_Array_Type --
2260 -------------------------------
2262 procedure Analyze_Formal_Array_Type
2263 (T : in out Entity_Id;
2264 Def : Node_Id)
2266 DSS : Node_Id;
2268 begin
2269 -- Treated like a non-generic array declaration, with additional
2270 -- semantic checks.
2272 Enter_Name (T);
2274 if Nkind (Def) = N_Constrained_Array_Definition then
2275 DSS := First (Discrete_Subtype_Definitions (Def));
2276 while Present (DSS) loop
2277 if Nkind (DSS) in N_Subtype_Indication
2278 | N_Range
2279 | N_Attribute_Reference
2280 then
2281 Error_Msg_N ("only a subtype mark is allowed in a formal", DSS);
2282 end if;
2284 Next (DSS);
2285 end loop;
2286 end if;
2288 Array_Type_Declaration (T, Def);
2289 Set_Is_Generic_Type (Base_Type (T));
2291 if Ekind (Component_Type (T)) = E_Incomplete_Type
2292 and then No (Full_View (Component_Type (T)))
2293 then
2294 Error_Msg_N ("premature usage of incomplete type", Def);
2296 -- Check that range constraint is not allowed on the component type
2297 -- of a generic formal array type (AARM 12.5.3(3))
2299 elsif Is_Internal (Component_Type (T))
2300 and then Present (Subtype_Indication (Component_Definition (Def)))
2301 and then Nkind (Original_Node
2302 (Subtype_Indication (Component_Definition (Def)))) =
2303 N_Subtype_Indication
2304 then
2305 Error_Msg_N
2306 ("in a formal, a subtype indication can only be "
2307 & "a subtype mark (RM 12.5.3(3))",
2308 Subtype_Indication (Component_Definition (Def)));
2309 end if;
2311 end Analyze_Formal_Array_Type;
2313 ---------------------------------------------
2314 -- Analyze_Formal_Decimal_Fixed_Point_Type --
2315 ---------------------------------------------
2317 -- As for other generic types, we create a valid type representation with
2318 -- legal but arbitrary attributes, whose values are never considered
2319 -- static. For all scalar types we introduce an anonymous base type, with
2320 -- the same attributes. We choose the corresponding integer type to be
2321 -- Standard_Integer.
2322 -- Here and in other similar routines, the Sloc of the generated internal
2323 -- type must be the same as the sloc of the defining identifier of the
2324 -- formal type declaration, to provide proper source navigation.
2326 procedure Analyze_Formal_Decimal_Fixed_Point_Type
2327 (T : Entity_Id;
2328 Def : Node_Id)
2330 Loc : constant Source_Ptr := Sloc (Def);
2332 Base : constant Entity_Id :=
2333 New_Internal_Entity
2334 (E_Decimal_Fixed_Point_Type,
2335 Current_Scope,
2336 Sloc (Defining_Identifier (Parent (Def))), 'G');
2338 Int_Base : constant Entity_Id := Standard_Integer;
2339 Delta_Val : constant Ureal := Ureal_1;
2340 Digs_Val : constant Uint := Uint_6;
2342 function Make_Dummy_Bound return Node_Id;
2343 -- Return a properly typed universal real literal to use as a bound
2345 ----------------------
2346 -- Make_Dummy_Bound --
2347 ----------------------
2349 function Make_Dummy_Bound return Node_Id is
2350 Bound : constant Node_Id := Make_Real_Literal (Loc, Ureal_1);
2351 begin
2352 Set_Etype (Bound, Universal_Real);
2353 return Bound;
2354 end Make_Dummy_Bound;
2356 -- Start of processing for Analyze_Formal_Decimal_Fixed_Point_Type
2358 begin
2359 Enter_Name (T);
2361 Set_Etype (Base, Base);
2362 Set_Size_Info (Base, Int_Base);
2363 Set_RM_Size (Base, RM_Size (Int_Base));
2364 Set_First_Rep_Item (Base, First_Rep_Item (Int_Base));
2365 Set_Digits_Value (Base, Digs_Val);
2366 Set_Delta_Value (Base, Delta_Val);
2367 Set_Small_Value (Base, Delta_Val);
2368 Set_Scalar_Range (Base,
2369 Make_Range (Loc,
2370 Low_Bound => Make_Dummy_Bound,
2371 High_Bound => Make_Dummy_Bound));
2373 Set_Is_Generic_Type (Base);
2374 Set_Parent (Base, Parent (Def));
2376 Mutate_Ekind (T, E_Decimal_Fixed_Point_Subtype);
2377 Set_Etype (T, Base);
2378 Set_Size_Info (T, Int_Base);
2379 Set_RM_Size (T, RM_Size (Int_Base));
2380 Set_First_Rep_Item (T, First_Rep_Item (Int_Base));
2381 Set_Digits_Value (T, Digs_Val);
2382 Set_Delta_Value (T, Delta_Val);
2383 Set_Small_Value (T, Delta_Val);
2384 Set_Scalar_Range (T, Scalar_Range (Base));
2385 Set_Is_Constrained (T);
2387 Check_Restriction (No_Fixed_Point, Def);
2388 end Analyze_Formal_Decimal_Fixed_Point_Type;
2390 -------------------------------------------
2391 -- Analyze_Formal_Derived_Interface_Type --
2392 -------------------------------------------
2394 procedure Analyze_Formal_Derived_Interface_Type
2395 (N : Node_Id;
2396 T : Entity_Id;
2397 Def : Node_Id)
2399 Loc : constant Source_Ptr := Sloc (Def);
2401 begin
2402 -- Rewrite as a type declaration of a derived type. This ensures that
2403 -- the interface list and primitive operations are properly captured.
2405 Rewrite (N,
2406 Make_Full_Type_Declaration (Loc,
2407 Defining_Identifier => T,
2408 Type_Definition => Def));
2409 Analyze (N);
2410 Set_Is_Generic_Type (T);
2411 end Analyze_Formal_Derived_Interface_Type;
2413 ---------------------------------
2414 -- Analyze_Formal_Derived_Type --
2415 ---------------------------------
2417 procedure Analyze_Formal_Derived_Type
2418 (N : Node_Id;
2419 T : Entity_Id;
2420 Def : Node_Id)
2422 Loc : constant Source_Ptr := Sloc (Def);
2423 Unk_Disc : constant Boolean := Unknown_Discriminants_Present (N);
2424 New_N : Node_Id;
2426 begin
2427 Set_Is_Generic_Type (T);
2429 if Private_Present (Def) then
2430 New_N :=
2431 Make_Private_Extension_Declaration (Loc,
2432 Defining_Identifier => T,
2433 Discriminant_Specifications => Discriminant_Specifications (N),
2434 Unknown_Discriminants_Present => Unk_Disc,
2435 Subtype_Indication => Subtype_Mark (Def),
2436 Interface_List => Interface_List (Def));
2438 Set_Abstract_Present (New_N, Abstract_Present (Def));
2439 Set_Limited_Present (New_N, Limited_Present (Def));
2440 Set_Synchronized_Present (New_N, Synchronized_Present (Def));
2442 else
2443 New_N :=
2444 Make_Full_Type_Declaration (Loc,
2445 Defining_Identifier => T,
2446 Discriminant_Specifications =>
2447 Discriminant_Specifications (Parent (T)),
2448 Type_Definition =>
2449 Make_Derived_Type_Definition (Loc,
2450 Subtype_Indication => Subtype_Mark (Def)));
2452 Set_Abstract_Present
2453 (Type_Definition (New_N), Abstract_Present (Def));
2454 Set_Limited_Present
2455 (Type_Definition (New_N), Limited_Present (Def));
2456 end if;
2458 Rewrite (N, New_N);
2459 Analyze (N);
2461 if Unk_Disc then
2462 if not Is_Composite_Type (T) then
2463 Error_Msg_N
2464 ("unknown discriminants not allowed for elementary types", N);
2465 else
2466 Set_Has_Unknown_Discriminants (T);
2467 Set_Is_Constrained (T, False);
2468 end if;
2469 end if;
2471 -- If the parent type has a known size, so does the formal, which makes
2472 -- legal representation clauses that involve the formal.
2474 Set_Size_Known_At_Compile_Time
2475 (T, Size_Known_At_Compile_Time (Entity (Subtype_Mark (Def))));
2476 end Analyze_Formal_Derived_Type;
2478 ----------------------------------
2479 -- Analyze_Formal_Discrete_Type --
2480 ----------------------------------
2482 -- The operations defined for a discrete types are those of an enumeration
2483 -- type. The size is set to an arbitrary value, for use in analyzing the
2484 -- generic unit.
2486 procedure Analyze_Formal_Discrete_Type (T : Entity_Id; Def : Node_Id) is
2487 Loc : constant Source_Ptr := Sloc (Def);
2488 Lo : Node_Id;
2489 Hi : Node_Id;
2491 Base : constant Entity_Id :=
2492 New_Internal_Entity
2493 (E_Floating_Point_Type, Current_Scope,
2494 Sloc (Defining_Identifier (Parent (Def))), 'G');
2496 begin
2497 Enter_Name (T);
2498 Mutate_Ekind (T, E_Enumeration_Subtype);
2499 Set_Etype (T, Base);
2500 Init_Size (T, 8);
2501 Reinit_Alignment (T);
2502 Set_Is_Generic_Type (T);
2503 Set_Is_Constrained (T);
2505 -- For semantic analysis, the bounds of the type must be set to some
2506 -- non-static value. The simplest is to create attribute nodes for those
2507 -- bounds, that refer to the type itself. These bounds are never
2508 -- analyzed but serve as place-holders.
2510 Lo :=
2511 Make_Attribute_Reference (Loc,
2512 Attribute_Name => Name_First,
2513 Prefix => New_Occurrence_Of (T, Loc));
2514 Set_Etype (Lo, T);
2516 Hi :=
2517 Make_Attribute_Reference (Loc,
2518 Attribute_Name => Name_Last,
2519 Prefix => New_Occurrence_Of (T, Loc));
2520 Set_Etype (Hi, T);
2522 Set_Scalar_Range (T,
2523 Make_Range (Loc,
2524 Low_Bound => Lo,
2525 High_Bound => Hi));
2527 Mutate_Ekind (Base, E_Enumeration_Type);
2528 Set_Etype (Base, Base);
2529 Init_Size (Base, 8);
2530 Reinit_Alignment (Base);
2531 Set_Is_Generic_Type (Base);
2532 Set_Scalar_Range (Base, Scalar_Range (T));
2533 Set_Parent (Base, Parent (Def));
2534 end Analyze_Formal_Discrete_Type;
2536 ----------------------------------
2537 -- Analyze_Formal_Floating_Type --
2538 ---------------------------------
2540 procedure Analyze_Formal_Floating_Type (T : Entity_Id; Def : Node_Id) is
2541 Base : constant Entity_Id :=
2542 New_Internal_Entity
2543 (E_Floating_Point_Type, Current_Scope,
2544 Sloc (Defining_Identifier (Parent (Def))), 'G');
2546 begin
2547 -- The various semantic attributes are taken from the predefined type
2548 -- Float, just so that all of them are initialized. Their values are
2549 -- never used because no constant folding or expansion takes place in
2550 -- the generic itself.
2552 Enter_Name (T);
2553 Mutate_Ekind (T, E_Floating_Point_Subtype);
2554 Set_Etype (T, Base);
2555 Set_Size_Info (T, (Standard_Float));
2556 Set_RM_Size (T, RM_Size (Standard_Float));
2557 Set_Digits_Value (T, Digits_Value (Standard_Float));
2558 Set_Scalar_Range (T, Scalar_Range (Standard_Float));
2559 Set_Is_Constrained (T);
2561 Set_Is_Generic_Type (Base);
2562 Set_Etype (Base, Base);
2563 Set_Size_Info (Base, (Standard_Float));
2564 Set_RM_Size (Base, RM_Size (Standard_Float));
2565 Set_Digits_Value (Base, Digits_Value (Standard_Float));
2566 Set_Scalar_Range (Base, Scalar_Range (Standard_Float));
2567 Set_Parent (Base, Parent (Def));
2569 Check_Restriction (No_Floating_Point, Def);
2570 end Analyze_Formal_Floating_Type;
2572 -----------------------------------
2573 -- Analyze_Formal_Interface_Type;--
2574 -----------------------------------
2576 procedure Analyze_Formal_Interface_Type
2577 (N : Node_Id;
2578 T : Entity_Id;
2579 Def : Node_Id)
2581 Loc : constant Source_Ptr := Sloc (N);
2582 New_N : Node_Id;
2584 begin
2585 New_N :=
2586 Make_Full_Type_Declaration (Loc,
2587 Defining_Identifier => T,
2588 Type_Definition => Def);
2590 Rewrite (N, New_N);
2591 Analyze (N);
2592 Set_Is_Generic_Type (T);
2593 end Analyze_Formal_Interface_Type;
2595 ---------------------------------
2596 -- Analyze_Formal_Modular_Type --
2597 ---------------------------------
2599 procedure Analyze_Formal_Modular_Type (T : Entity_Id; Def : Node_Id) is
2600 begin
2601 -- Apart from their entity kind, generic modular types are treated like
2602 -- signed integer types, and have the same attributes.
2604 Analyze_Formal_Signed_Integer_Type (T, Def);
2605 Mutate_Ekind (T, E_Modular_Integer_Subtype);
2606 Mutate_Ekind (Etype (T), E_Modular_Integer_Type);
2608 end Analyze_Formal_Modular_Type;
2610 ---------------------------------------
2611 -- Analyze_Formal_Object_Declaration --
2612 ---------------------------------------
2614 procedure Analyze_Formal_Object_Declaration (N : Node_Id) is
2615 E : constant Node_Id := Default_Expression (N);
2616 Id : constant Node_Id := Defining_Identifier (N);
2617 K : Entity_Kind;
2618 T : Node_Id;
2620 begin
2621 Enter_Name (Id);
2623 -- Determine the mode of the formal object
2625 if Out_Present (N) then
2626 K := E_Generic_In_Out_Parameter;
2628 if not In_Present (N) then
2629 Error_Msg_N ("formal generic objects cannot have mode OUT", N);
2630 end if;
2632 else
2633 K := E_Generic_In_Parameter;
2634 end if;
2636 if Present (Subtype_Mark (N)) then
2637 Find_Type (Subtype_Mark (N));
2638 T := Entity (Subtype_Mark (N));
2640 -- Verify that there is no redundant null exclusion
2642 if Null_Exclusion_Present (N) then
2643 if not Is_Access_Type (T) then
2644 Error_Msg_N
2645 ("null exclusion can only apply to an access type", N);
2647 elsif Can_Never_Be_Null (T) then
2648 Error_Msg_NE
2649 ("`NOT NULL` not allowed (& already excludes null)", N, T);
2650 end if;
2651 end if;
2653 -- Ada 2005 (AI-423): Formal object with an access definition
2655 else
2656 Check_Access_Definition (N);
2657 T := Access_Definition
2658 (Related_Nod => N,
2659 N => Access_Definition (N));
2660 end if;
2662 if Ekind (T) = E_Incomplete_Type then
2663 declare
2664 Error_Node : Node_Id;
2666 begin
2667 if Present (Subtype_Mark (N)) then
2668 Error_Node := Subtype_Mark (N);
2669 else
2670 Check_Access_Definition (N);
2671 Error_Node := Access_Definition (N);
2672 end if;
2674 Error_Msg_N ("premature usage of incomplete type", Error_Node);
2675 end;
2676 end if;
2678 if K = E_Generic_In_Parameter then
2680 -- Ada 2005 (AI-287): Limited aggregates allowed in generic formals
2682 if Ada_Version < Ada_2005 and then Is_Limited_Type (T) then
2683 Error_Msg_N
2684 ("generic formal of mode IN must not be of limited type", N);
2685 Explain_Limited_Type (T, N);
2686 end if;
2688 if Is_Abstract_Type (T) then
2689 Error_Msg_N
2690 ("generic formal of mode IN must not be of abstract type", N);
2691 end if;
2693 if Present (E) then
2694 Preanalyze_Spec_Expression (E, T);
2696 if Is_Limited_Type (T) and then not OK_For_Limited_Init (T, E) then
2697 Error_Msg_N
2698 ("initialization not allowed for limited types", E);
2699 Explain_Limited_Type (T, E);
2700 end if;
2701 end if;
2703 Mutate_Ekind (Id, K);
2704 Set_Etype (Id, T);
2706 -- Case of generic IN OUT parameter
2708 else
2709 -- If the formal has an unconstrained type, construct its actual
2710 -- subtype, as is done for subprogram formals. In this fashion, all
2711 -- its uses can refer to specific bounds.
2713 Mutate_Ekind (Id, K);
2714 Set_Etype (Id, T);
2716 if (Is_Array_Type (T) and then not Is_Constrained (T))
2717 or else (Ekind (T) = E_Record_Type and then Has_Discriminants (T))
2718 then
2719 declare
2720 Non_Freezing_Ref : constant Node_Id :=
2721 New_Occurrence_Of (Id, Sloc (Id));
2722 Decl : Node_Id;
2724 begin
2725 -- Make sure the actual subtype doesn't generate bogus freezing
2727 Set_Must_Not_Freeze (Non_Freezing_Ref);
2728 Decl := Build_Actual_Subtype (T, Non_Freezing_Ref);
2729 Insert_Before_And_Analyze (N, Decl);
2730 Set_Actual_Subtype (Id, Defining_Identifier (Decl));
2731 end;
2732 else
2733 Set_Actual_Subtype (Id, T);
2734 end if;
2736 if Present (E) then
2737 Error_Msg_N
2738 ("initialization not allowed for `IN OUT` formals", N);
2739 end if;
2740 end if;
2742 if Has_Aspects (N) then
2743 Analyze_Aspect_Specifications (N, Id);
2744 end if;
2745 end Analyze_Formal_Object_Declaration;
2747 ----------------------------------------------
2748 -- Analyze_Formal_Ordinary_Fixed_Point_Type --
2749 ----------------------------------------------
2751 procedure Analyze_Formal_Ordinary_Fixed_Point_Type
2752 (T : Entity_Id;
2753 Def : Node_Id)
2755 Loc : constant Source_Ptr := Sloc (Def);
2756 Base : constant Entity_Id :=
2757 New_Internal_Entity
2758 (E_Ordinary_Fixed_Point_Type, Current_Scope,
2759 Sloc (Defining_Identifier (Parent (Def))), 'G');
2761 begin
2762 -- The semantic attributes are set for completeness only, their values
2763 -- will never be used, since all properties of the type are non-static.
2765 Enter_Name (T);
2766 Mutate_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
2767 Set_Etype (T, Base);
2768 Set_Size_Info (T, Standard_Integer);
2769 Set_RM_Size (T, RM_Size (Standard_Integer));
2770 Set_Small_Value (T, Ureal_1);
2771 Set_Delta_Value (T, Ureal_1);
2772 Set_Scalar_Range (T,
2773 Make_Range (Loc,
2774 Low_Bound => Make_Real_Literal (Loc, Ureal_1),
2775 High_Bound => Make_Real_Literal (Loc, Ureal_1)));
2776 Set_Is_Constrained (T);
2778 Set_Is_Generic_Type (Base);
2779 Set_Etype (Base, Base);
2780 Set_Size_Info (Base, Standard_Integer);
2781 Set_RM_Size (Base, RM_Size (Standard_Integer));
2782 Set_Small_Value (Base, Ureal_1);
2783 Set_Delta_Value (Base, Ureal_1);
2784 Set_Scalar_Range (Base, Scalar_Range (T));
2785 Set_Parent (Base, Parent (Def));
2787 Check_Restriction (No_Fixed_Point, Def);
2788 end Analyze_Formal_Ordinary_Fixed_Point_Type;
2790 ----------------------------------------
2791 -- Analyze_Formal_Package_Declaration --
2792 ----------------------------------------
2794 procedure Analyze_Formal_Package_Declaration (N : Node_Id) is
2795 Gen_Id : constant Node_Id := Name (N);
2796 Loc : constant Source_Ptr := Sloc (N);
2797 Pack_Id : constant Entity_Id := Defining_Identifier (N);
2798 Formal : Entity_Id;
2799 Gen_Decl : Node_Id;
2800 Gen_Unit : Entity_Id;
2801 Renaming : Node_Id;
2803 Vis_Prims_List : Elist_Id := No_Elist;
2804 -- List of primitives made temporarily visible in the instantiation
2805 -- to match the visibility of the formal type.
2807 function Build_Local_Package return Node_Id;
2808 -- The formal package is rewritten so that its parameters are replaced
2809 -- with corresponding declarations. For parameters with bona fide
2810 -- associations these declarations are created by Analyze_Associations
2811 -- as for a regular instantiation. For boxed parameters, we preserve
2812 -- the formal declarations and analyze them, in order to introduce
2813 -- entities of the right kind in the environment of the formal.
2815 -------------------------
2816 -- Build_Local_Package --
2817 -------------------------
2819 function Build_Local_Package return Node_Id is
2820 Decls : List_Id;
2821 Pack_Decl : Node_Id;
2823 begin
2824 -- Within the formal, the name of the generic package is a renaming
2825 -- of the formal (as for a regular instantiation).
2827 Pack_Decl :=
2828 Make_Package_Declaration (Loc,
2829 Specification =>
2830 Copy_Generic_Node
2831 (Specification (Original_Node (Gen_Decl)),
2832 Empty, Instantiating => True));
2834 Renaming :=
2835 Make_Package_Renaming_Declaration (Loc,
2836 Defining_Unit_Name =>
2837 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
2838 Name => New_Occurrence_Of (Formal, Loc));
2840 if Nkind (Gen_Id) = N_Identifier
2841 and then Chars (Gen_Id) = Chars (Pack_Id)
2842 then
2843 Error_Msg_NE
2844 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
2845 end if;
2847 -- If the formal is declared with a box, or with an others choice,
2848 -- create corresponding declarations for all entities in the formal
2849 -- part, so that names with the proper types are available in the
2850 -- specification of the formal package.
2852 -- On the other hand, if there are no associations, then all the
2853 -- formals must have defaults, and this will be checked by the
2854 -- call to Analyze_Associations.
2856 if Box_Present (N)
2857 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
2858 then
2859 declare
2860 Formal_Decl : Node_Id;
2862 begin
2863 -- TBA : for a formal package, need to recurse ???
2865 Decls := New_List;
2866 Formal_Decl :=
2867 First
2868 (Generic_Formal_Declarations (Original_Node (Gen_Decl)));
2869 while Present (Formal_Decl) loop
2870 Append_To
2871 (Decls,
2872 Copy_Generic_Node
2873 (Formal_Decl, Empty, Instantiating => True));
2874 Next (Formal_Decl);
2875 end loop;
2876 end;
2878 -- If generic associations are present, use Analyze_Associations to
2879 -- create the proper renaming declarations.
2881 else
2882 declare
2883 Act_Tree : constant Node_Id :=
2884 Copy_Generic_Node
2885 (Original_Node (Gen_Decl), Empty,
2886 Instantiating => True);
2888 begin
2889 Generic_Renamings.Set_Last (0);
2890 Generic_Renamings_HTable.Reset;
2891 Instantiation_Node := N;
2893 Decls :=
2894 Analyze_Associations
2895 (I_Node => Original_Node (N),
2896 Formals => Generic_Formal_Declarations (Act_Tree),
2897 F_Copy => Generic_Formal_Declarations (Gen_Decl));
2899 Vis_Prims_List := Check_Hidden_Primitives (Decls);
2900 end;
2901 end if;
2903 Append (Renaming, To => Decls);
2905 -- Add generated declarations ahead of local declarations in
2906 -- the package.
2908 if No (Visible_Declarations (Specification (Pack_Decl))) then
2909 Set_Visible_Declarations (Specification (Pack_Decl), Decls);
2910 else
2911 Insert_List_Before
2912 (First (Visible_Declarations (Specification (Pack_Decl))),
2913 Decls);
2914 end if;
2916 return Pack_Decl;
2917 end Build_Local_Package;
2919 -- Local variables
2921 Save_ISMP : constant Boolean := Ignore_SPARK_Mode_Pragmas_In_Instance;
2922 -- Save flag Ignore_SPARK_Mode_Pragmas_In_Instance for restore on exit
2924 Associations : Boolean := True;
2925 New_N : Node_Id;
2926 Parent_Installed : Boolean := False;
2927 Parent_Instance : Entity_Id;
2928 Renaming_In_Par : Entity_Id;
2930 -- Start of processing for Analyze_Formal_Package_Declaration
2932 begin
2933 Check_Text_IO_Special_Unit (Gen_Id);
2935 Init_Env;
2936 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
2937 Gen_Unit := Entity (Gen_Id);
2939 -- Check for a formal package that is a package renaming
2941 if Present (Renamed_Entity (Gen_Unit)) then
2943 -- Indicate that unit is used, before replacing it with renamed
2944 -- entity for use below.
2946 if In_Extended_Main_Source_Unit (N) then
2947 Set_Is_Instantiated (Gen_Unit);
2948 Generate_Reference (Gen_Unit, N);
2949 end if;
2951 Gen_Unit := Renamed_Entity (Gen_Unit);
2952 end if;
2954 if Ekind (Gen_Unit) /= E_Generic_Package then
2955 Error_Msg_N ("expect generic package name", Gen_Id);
2956 Restore_Env;
2957 goto Leave;
2959 elsif Gen_Unit = Current_Scope then
2960 Error_Msg_N
2961 ("generic package cannot be used as a formal package of itself",
2962 Gen_Id);
2963 Restore_Env;
2964 goto Leave;
2966 elsif In_Open_Scopes (Gen_Unit) then
2967 if Is_Compilation_Unit (Gen_Unit)
2968 and then Is_Child_Unit (Current_Scope)
2969 then
2970 -- Special-case the error when the formal is a parent, and
2971 -- continue analysis to minimize cascaded errors.
2973 Error_Msg_N
2974 ("generic parent cannot be used as formal package of a child "
2975 & "unit", Gen_Id);
2977 else
2978 Error_Msg_N
2979 ("generic package cannot be used as a formal package within "
2980 & "itself", Gen_Id);
2981 Restore_Env;
2982 goto Leave;
2983 end if;
2984 end if;
2986 -- Check that name of formal package does not hide name of generic,
2987 -- or its leading prefix. This check must be done separately because
2988 -- the name of the generic has already been analyzed.
2990 declare
2991 Gen_Name : Entity_Id;
2993 begin
2994 Gen_Name := Gen_Id;
2995 while Nkind (Gen_Name) = N_Expanded_Name loop
2996 Gen_Name := Prefix (Gen_Name);
2997 end loop;
2999 if Chars (Gen_Name) = Chars (Pack_Id) then
3000 Error_Msg_NE
3001 ("& is hidden within declaration of formal package",
3002 Gen_Id, Gen_Name);
3003 end if;
3004 end;
3006 if Box_Present (N)
3007 or else No (Generic_Associations (N))
3008 or else Nkind (First (Generic_Associations (N))) = N_Others_Choice
3009 then
3010 Associations := False;
3011 end if;
3013 -- If there are no generic associations, the generic parameters appear
3014 -- as local entities and are instantiated like them. We copy the generic
3015 -- package declaration as if it were an instantiation, and analyze it
3016 -- like a regular package, except that we treat the formals as
3017 -- additional visible components.
3019 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
3021 if In_Extended_Main_Source_Unit (N) then
3022 Set_Is_Instantiated (Gen_Unit);
3023 Generate_Reference (Gen_Unit, N);
3024 end if;
3026 Formal := New_Copy (Pack_Id);
3027 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
3029 -- Make local generic without formals. The formals will be replaced with
3030 -- internal declarations.
3032 begin
3033 New_N := Build_Local_Package;
3035 -- If there are errors in the parameter list, Analyze_Associations
3036 -- raises Instantiation_Error. Patch the declaration to prevent further
3037 -- exception propagation.
3039 exception
3040 when Instantiation_Error =>
3041 Enter_Name (Formal);
3042 Mutate_Ekind (Formal, E_Variable);
3043 Set_Etype (Formal, Any_Type);
3044 Restore_Hidden_Primitives (Vis_Prims_List);
3046 if Parent_Installed then
3047 Remove_Parent;
3048 end if;
3050 goto Leave;
3051 end;
3053 Rewrite (N, New_N);
3054 Set_Defining_Unit_Name (Specification (New_N), Formal);
3055 Set_Generic_Parent (Specification (N), Gen_Unit);
3056 Set_Instance_Env (Gen_Unit, Formal);
3057 Set_Is_Generic_Instance (Formal);
3059 Enter_Name (Formal);
3060 Mutate_Ekind (Formal, E_Package);
3061 Set_Etype (Formal, Standard_Void_Type);
3062 Set_Inner_Instances (Formal, New_Elmt_List);
3064 -- It is unclear that any aspects can apply to a formal package
3065 -- declaration, given that they look like a hidden conformance
3066 -- requirement on the corresponding actual. However, Abstract_State
3067 -- must be treated specially because it generates declarations that
3068 -- must appear before other declarations in the specification and
3069 -- must be analyzed at once.
3071 if Present (Aspect_Specifications (Gen_Decl)) then
3072 if No (Aspect_Specifications (N)) then
3073 Set_Aspect_Specifications (N, New_List);
3074 Set_Has_Aspects (N);
3075 end if;
3077 declare
3078 ASN : Node_Id := First (Aspect_Specifications (Gen_Decl));
3079 New_A : Node_Id;
3081 begin
3082 while Present (ASN) loop
3083 if Get_Aspect_Id (ASN) = Aspect_Abstract_State then
3084 New_A :=
3085 Copy_Generic_Node (ASN, Empty, Instantiating => True);
3086 Set_Entity (New_A, Formal);
3087 Set_Analyzed (New_A, False);
3088 Append (New_A, Aspect_Specifications (N));
3089 Analyze_Aspect_Specifications (N, Formal);
3090 exit;
3091 end if;
3093 Next (ASN);
3094 end loop;
3095 end;
3096 end if;
3098 Push_Scope (Formal);
3100 -- Manually set the SPARK_Mode from the context because the package
3101 -- declaration is never analyzed.
3103 Set_SPARK_Pragma (Formal, SPARK_Mode_Pragma);
3104 Set_SPARK_Aux_Pragma (Formal, SPARK_Mode_Pragma);
3105 Set_SPARK_Pragma_Inherited (Formal);
3106 Set_SPARK_Aux_Pragma_Inherited (Formal);
3108 if Is_Child_Unit (Gen_Unit) and then Parent_Installed then
3110 -- Similarly, we have to make the name of the formal visible in the
3111 -- parent instance, to resolve properly fully qualified names that
3112 -- may appear in the generic unit. The parent instance has been
3113 -- placed on the scope stack ahead of the current scope.
3115 Parent_Instance := Scope_Stack.Table (Scope_Stack.Last - 1).Entity;
3117 Renaming_In_Par :=
3118 Make_Defining_Identifier (Loc, Chars (Gen_Unit));
3119 Mutate_Ekind (Renaming_In_Par, E_Package);
3120 Set_Etype (Renaming_In_Par, Standard_Void_Type);
3121 Set_Scope (Renaming_In_Par, Parent_Instance);
3122 Set_Parent (Renaming_In_Par, Parent (Formal));
3123 Set_Renamed_Entity (Renaming_In_Par, Formal);
3124 Append_Entity (Renaming_In_Par, Parent_Instance);
3125 end if;
3127 -- A formal package declaration behaves as a package instantiation with
3128 -- respect to SPARK_Mode "off". If the annotation is "off" or altogether
3129 -- missing, set the global flag which signals Analyze_Pragma to ingnore
3130 -- all SPARK_Mode pragmas within the generic_package_name.
3132 if SPARK_Mode /= On then
3133 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
3135 -- Mark the formal spec in case the body is instantiated at a later
3136 -- pass. This preserves the original context in effect for the body.
3138 Set_Ignore_SPARK_Mode_Pragmas (Formal);
3139 end if;
3141 Analyze (Specification (N));
3143 -- The formals for which associations are provided are not visible
3144 -- outside of the formal package. The others are still declared by a
3145 -- formal parameter declaration.
3147 -- If there are no associations, the only local entity to hide is the
3148 -- generated package renaming itself.
3150 declare
3151 E : Entity_Id;
3153 begin
3154 E := First_Entity (Formal);
3155 while Present (E) loop
3156 if Associations and then not Is_Generic_Formal (E) then
3157 Set_Is_Hidden (E);
3158 end if;
3160 if Ekind (E) = E_Package and then Renamed_Entity (E) = Formal then
3161 Set_Is_Hidden (E);
3162 exit;
3163 end if;
3165 Next_Entity (E);
3166 end loop;
3167 end;
3169 End_Package_Scope (Formal);
3170 Restore_Hidden_Primitives (Vis_Prims_List);
3172 if Parent_Installed then
3173 Remove_Parent;
3174 end if;
3176 Restore_Env;
3178 -- Inside the generic unit, the formal package is a regular package, but
3179 -- no body is needed for it. Note that after instantiation, the defining
3180 -- unit name we need is in the new tree and not in the original (see
3181 -- Package_Instantiation). A generic formal package is an instance, and
3182 -- can be used as an actual for an inner instance.
3184 Set_Has_Completion (Formal, True);
3186 -- Add semantic information to the original defining identifier.
3188 Mutate_Ekind (Pack_Id, E_Package);
3189 Set_Etype (Pack_Id, Standard_Void_Type);
3190 Set_Scope (Pack_Id, Scope (Formal));
3191 Set_Has_Completion (Pack_Id, True);
3193 <<Leave>>
3194 if Has_Aspects (N) then
3195 -- Unclear that any other aspects may appear here, snalyze them
3196 -- for completion, given that the grammar allows their appearance.
3198 Analyze_Aspect_Specifications (N, Pack_Id);
3199 end if;
3201 Ignore_SPARK_Mode_Pragmas_In_Instance := Save_ISMP;
3202 end Analyze_Formal_Package_Declaration;
3204 ---------------------------------
3205 -- Analyze_Formal_Private_Type --
3206 ---------------------------------
3208 procedure Analyze_Formal_Private_Type
3209 (N : Node_Id;
3210 T : Entity_Id;
3211 Def : Node_Id)
3213 begin
3214 New_Private_Type (N, T, Def);
3216 -- Set the size to an arbitrary but legal value
3218 Set_Size_Info (T, Standard_Integer);
3219 Set_RM_Size (T, RM_Size (Standard_Integer));
3220 end Analyze_Formal_Private_Type;
3222 ------------------------------------
3223 -- Analyze_Formal_Incomplete_Type --
3224 ------------------------------------
3226 procedure Analyze_Formal_Incomplete_Type
3227 (T : Entity_Id;
3228 Def : Node_Id)
3230 begin
3231 Enter_Name (T);
3232 Mutate_Ekind (T, E_Incomplete_Type);
3233 Set_Etype (T, T);
3234 Set_Private_Dependents (T, New_Elmt_List);
3236 if Tagged_Present (Def) then
3237 Set_Is_Tagged_Type (T);
3238 Make_Class_Wide_Type (T);
3239 Set_Direct_Primitive_Operations (T, New_Elmt_List);
3240 end if;
3241 end Analyze_Formal_Incomplete_Type;
3243 ----------------------------------------
3244 -- Analyze_Formal_Signed_Integer_Type --
3245 ----------------------------------------
3247 procedure Analyze_Formal_Signed_Integer_Type
3248 (T : Entity_Id;
3249 Def : Node_Id)
3251 Base : constant Entity_Id :=
3252 New_Internal_Entity
3253 (E_Signed_Integer_Type,
3254 Current_Scope,
3255 Sloc (Defining_Identifier (Parent (Def))), 'G');
3257 begin
3258 Enter_Name (T);
3260 Mutate_Ekind (T, E_Signed_Integer_Subtype);
3261 Set_Etype (T, Base);
3262 Set_Size_Info (T, Standard_Integer);
3263 Set_RM_Size (T, RM_Size (Standard_Integer));
3264 Set_Scalar_Range (T, Scalar_Range (Standard_Integer));
3265 Set_Is_Constrained (T);
3267 Set_Is_Generic_Type (Base);
3268 Set_Size_Info (Base, Standard_Integer);
3269 Set_RM_Size (Base, RM_Size (Standard_Integer));
3270 Set_Etype (Base, Base);
3271 Set_Scalar_Range (Base, Scalar_Range (Standard_Integer));
3272 Set_Parent (Base, Parent (Def));
3273 end Analyze_Formal_Signed_Integer_Type;
3275 -------------------------------------------
3276 -- Analyze_Formal_Subprogram_Declaration --
3277 -------------------------------------------
3279 procedure Analyze_Formal_Subprogram_Declaration (N : Node_Id) is
3280 Spec : constant Node_Id := Specification (N);
3281 Def : constant Node_Id := Default_Name (N);
3282 Expr : constant Node_Id := Expression (N);
3283 Nam : constant Entity_Id := Defining_Unit_Name (Spec);
3284 Subp : Entity_Id;
3286 begin
3287 if Nam = Error then
3288 return;
3289 end if;
3291 if Nkind (Nam) = N_Defining_Program_Unit_Name then
3292 Error_Msg_N ("name of formal subprogram must be a direct name", Nam);
3293 goto Leave;
3294 end if;
3296 Analyze_Subprogram_Declaration (N);
3297 Set_Is_Formal_Subprogram (Nam);
3298 Set_Has_Completion (Nam);
3300 if Nkind (N) = N_Formal_Abstract_Subprogram_Declaration then
3301 Set_Is_Abstract_Subprogram (Nam);
3303 Set_Is_Dispatching_Operation (Nam);
3305 -- A formal abstract procedure cannot have a null default
3306 -- (RM 12.6(4.1/2)).
3308 if Nkind (Spec) = N_Procedure_Specification
3309 and then Null_Present (Spec)
3310 then
3311 Error_Msg_N
3312 ("a formal abstract subprogram cannot default to null", Spec);
3313 end if;
3315 -- A formal abstract function cannot have an expression default
3316 -- (expression defaults are allowed for nonabstract formal functions
3317 -- when extensions are enabled).
3319 if Nkind (Spec) = N_Function_Specification
3320 and then Present (Expr)
3321 then
3322 Error_Msg_N
3323 ("a formal abstract subprogram cannot default to an expression",
3324 Spec);
3325 end if;
3327 declare
3328 Ctrl_Type : constant Entity_Id := Find_Dispatching_Type (Nam);
3329 begin
3330 if No (Ctrl_Type) then
3331 Error_Msg_N
3332 ("abstract formal subprogram must have a controlling type",
3335 elsif Ada_Version >= Ada_2012
3336 and then Is_Incomplete_Type (Ctrl_Type)
3337 then
3338 Error_Msg_NE
3339 ("controlling type of abstract formal subprogram cannot "
3340 & "be incomplete type", N, Ctrl_Type);
3342 else
3343 Check_Controlling_Formals (Ctrl_Type, Nam);
3344 end if;
3345 end;
3346 end if;
3348 -- Default name is resolved at the point of instantiation
3350 if Box_Present (N) then
3351 null;
3353 -- Default name is bound at the point of generic declaration
3355 elsif Present (Def) then
3356 if Nkind (Def) = N_Operator_Symbol then
3357 Find_Direct_Name (Def);
3359 elsif Nkind (Def) /= N_Attribute_Reference then
3360 Analyze (Def);
3362 else
3363 -- For an attribute reference, analyze the prefix and verify
3364 -- that it has the proper profile for the subprogram.
3366 Analyze (Prefix (Def));
3367 Valid_Default_Attribute (Nam, Def);
3368 goto Leave;
3369 end if;
3371 -- Default name may be overloaded, in which case the interpretation
3372 -- with the correct profile must be selected, as for a renaming.
3373 -- If the definition is an indexed component, it must denote a
3374 -- member of an entry family. If it is a selected component, it
3375 -- can be a protected operation.
3377 if Etype (Def) = Any_Type then
3378 goto Leave;
3380 elsif Nkind (Def) = N_Selected_Component then
3381 if not Is_Overloadable (Entity (Selector_Name (Def))) then
3382 Error_Msg_N ("expect valid subprogram name as default", Def);
3383 end if;
3385 elsif Nkind (Def) = N_Indexed_Component then
3386 if Is_Entity_Name (Prefix (Def)) then
3387 if Ekind (Entity (Prefix (Def))) /= E_Entry_Family then
3388 Error_Msg_N ("expect valid subprogram name as default", Def);
3389 end if;
3391 elsif Nkind (Prefix (Def)) = N_Selected_Component then
3392 if Ekind (Entity (Selector_Name (Prefix (Def)))) /=
3393 E_Entry_Family
3394 then
3395 Error_Msg_N ("expect valid subprogram name as default", Def);
3396 end if;
3398 else
3399 Error_Msg_N ("expect valid subprogram name as default", Def);
3400 goto Leave;
3401 end if;
3403 elsif Nkind (Def) = N_Character_Literal then
3405 -- Needs some type checks: subprogram should be parameterless???
3407 Resolve (Def, (Etype (Nam)));
3409 elsif not Is_Entity_Name (Def)
3410 or else not Is_Overloadable (Entity (Def))
3411 then
3412 Error_Msg_N ("expect valid subprogram name as default", Def);
3413 goto Leave;
3415 elsif not Is_Overloaded (Def) then
3416 Subp := Entity (Def);
3418 if Subp = Nam then
3419 Error_Msg_N ("premature usage of formal subprogram", Def);
3421 elsif not Entity_Matches_Spec (Subp, Nam) then
3422 Error_Msg_N ("no visible entity matches specification", Def);
3423 end if;
3425 -- More than one interpretation, so disambiguate as for a renaming
3427 else
3428 declare
3429 I : Interp_Index;
3430 I1 : Interp_Index := 0;
3431 It : Interp;
3432 It1 : Interp;
3434 begin
3435 Subp := Any_Id;
3436 Get_First_Interp (Def, I, It);
3437 while Present (It.Nam) loop
3438 if Entity_Matches_Spec (It.Nam, Nam) then
3439 if Subp /= Any_Id then
3440 It1 := Disambiguate (Def, I1, I, Etype (Subp));
3442 if It1 = No_Interp then
3443 Error_Msg_N ("ambiguous default subprogram", Def);
3444 else
3445 Subp := It1.Nam;
3446 end if;
3448 exit;
3450 else
3451 I1 := I;
3452 Subp := It.Nam;
3453 end if;
3454 end if;
3456 Get_Next_Interp (I, It);
3457 end loop;
3458 end;
3460 if Subp /= Any_Id then
3462 -- Subprogram found, generate reference to it
3464 Set_Entity (Def, Subp);
3465 Generate_Reference (Subp, Def);
3467 if Subp = Nam then
3468 Error_Msg_N ("premature usage of formal subprogram", Def);
3470 elsif Ekind (Subp) /= E_Operator then
3471 Check_Mode_Conformant (Subp, Nam);
3472 end if;
3474 else
3475 Error_Msg_N ("no visible subprogram matches specification", N);
3476 end if;
3477 end if;
3479 -- When extensions are enabled, an expression can be given as default
3480 -- for a formal function. The expression must be of the function result
3481 -- type and can reference formal parameters of the function.
3483 elsif Present (Expr) then
3484 Push_Scope (Nam);
3485 Install_Formals (Nam);
3486 Preanalyze_Spec_Expression (Expr, Etype (Nam));
3487 End_Scope;
3488 end if;
3490 <<Leave>>
3491 if Has_Aspects (N) then
3492 Analyze_Aspect_Specifications (N, Nam);
3493 end if;
3495 end Analyze_Formal_Subprogram_Declaration;
3497 -------------------------------------
3498 -- Analyze_Formal_Type_Declaration --
3499 -------------------------------------
3501 procedure Analyze_Formal_Type_Declaration (N : Node_Id) is
3502 Def : constant Node_Id := Formal_Type_Definition (N);
3503 T : Entity_Id;
3505 begin
3506 T := Defining_Identifier (N);
3508 if Present (Discriminant_Specifications (N))
3509 and then Nkind (Def) /= N_Formal_Private_Type_Definition
3510 then
3511 Error_Msg_N
3512 ("discriminants not allowed for this formal type", T);
3513 end if;
3515 -- Enter the new name, and branch to specific routine
3517 case Nkind (Def) is
3518 when N_Formal_Private_Type_Definition =>
3519 Analyze_Formal_Private_Type (N, T, Def);
3521 when N_Formal_Derived_Type_Definition =>
3522 Analyze_Formal_Derived_Type (N, T, Def);
3524 when N_Formal_Incomplete_Type_Definition =>
3525 Analyze_Formal_Incomplete_Type (T, Def);
3527 when N_Formal_Discrete_Type_Definition =>
3528 Analyze_Formal_Discrete_Type (T, Def);
3530 when N_Formal_Signed_Integer_Type_Definition =>
3531 Analyze_Formal_Signed_Integer_Type (T, Def);
3533 when N_Formal_Modular_Type_Definition =>
3534 Analyze_Formal_Modular_Type (T, Def);
3536 when N_Formal_Floating_Point_Definition =>
3537 Analyze_Formal_Floating_Type (T, Def);
3539 when N_Formal_Ordinary_Fixed_Point_Definition =>
3540 Analyze_Formal_Ordinary_Fixed_Point_Type (T, Def);
3542 when N_Formal_Decimal_Fixed_Point_Definition =>
3543 Analyze_Formal_Decimal_Fixed_Point_Type (T, Def);
3545 when N_Array_Type_Definition =>
3546 Analyze_Formal_Array_Type (T, Def);
3548 when N_Access_Function_Definition
3549 | N_Access_Procedure_Definition
3550 | N_Access_To_Object_Definition
3552 Analyze_Generic_Access_Type (T, Def);
3554 -- Ada 2005: a interface declaration is encoded as an abstract
3555 -- record declaration or a abstract type derivation.
3557 when N_Record_Definition =>
3558 Analyze_Formal_Interface_Type (N, T, Def);
3560 when N_Derived_Type_Definition =>
3561 Analyze_Formal_Derived_Interface_Type (N, T, Def);
3563 when N_Error =>
3564 null;
3566 when others =>
3567 raise Program_Error;
3568 end case;
3570 -- A formal type declaration declares a type and its first
3571 -- subtype.
3573 Set_Is_Generic_Type (T);
3574 Set_Is_First_Subtype (T);
3576 if Present (Default_Subtype_Mark (Original_Node (N))) then
3577 Validate_Formal_Type_Default (N);
3578 end if;
3580 if Has_Aspects (N) then
3581 Analyze_Aspect_Specifications (N, T);
3582 end if;
3583 end Analyze_Formal_Type_Declaration;
3585 ------------------------------------
3586 -- Analyze_Function_Instantiation --
3587 ------------------------------------
3589 procedure Analyze_Function_Instantiation (N : Node_Id) is
3590 begin
3591 Analyze_Subprogram_Instantiation (N, E_Function);
3592 end Analyze_Function_Instantiation;
3594 ---------------------------------
3595 -- Analyze_Generic_Access_Type --
3596 ---------------------------------
3598 procedure Analyze_Generic_Access_Type (T : Entity_Id; Def : Node_Id) is
3599 begin
3600 Enter_Name (T);
3602 if Nkind (Def) = N_Access_To_Object_Definition then
3603 Access_Type_Declaration (T, Def);
3605 if Is_Incomplete_Or_Private_Type (Designated_Type (T))
3606 and then No (Full_View (Designated_Type (T)))
3607 and then not Is_Generic_Type (Designated_Type (T))
3608 then
3609 Error_Msg_N ("premature usage of incomplete type", Def);
3611 elsif not Is_Entity_Name (Subtype_Indication (Def)) then
3612 Error_Msg_N
3613 ("only a subtype mark is allowed in a formal", Def);
3614 end if;
3616 else
3617 Access_Subprogram_Declaration (T, Def);
3618 end if;
3619 end Analyze_Generic_Access_Type;
3621 ---------------------------------
3622 -- Analyze_Generic_Formal_Part --
3623 ---------------------------------
3625 procedure Analyze_Generic_Formal_Part (N : Node_Id) is
3626 Gen_Parm_Decl : Node_Id;
3628 begin
3629 -- The generic formals are processed in the scope of the generic unit,
3630 -- where they are immediately visible. The scope is installed by the
3631 -- caller.
3633 Gen_Parm_Decl := First (Generic_Formal_Declarations (N));
3634 while Present (Gen_Parm_Decl) loop
3635 Analyze (Gen_Parm_Decl);
3636 Next (Gen_Parm_Decl);
3637 end loop;
3639 Generate_Reference_To_Generic_Formals (Current_Scope);
3641 -- For Ada 2022, some formal parameters can carry aspects, which must
3642 -- be name-resolved at the end of the list of formal parameters (which
3643 -- has the semantics of a declaration list).
3645 Analyze_Contracts (Generic_Formal_Declarations (N));
3646 end Analyze_Generic_Formal_Part;
3648 ------------------------------------------
3649 -- Analyze_Generic_Package_Declaration --
3650 ------------------------------------------
3652 procedure Analyze_Generic_Package_Declaration (N : Node_Id) is
3653 Decls : constant List_Id := Visible_Declarations (Specification (N));
3654 Loc : constant Source_Ptr := Sloc (N);
3656 Decl : Node_Id;
3657 Id : Entity_Id;
3658 New_N : Node_Id;
3659 Renaming : Node_Id;
3660 Save_Parent : Node_Id;
3662 begin
3663 -- A generic may grant access to its private enclosing context depending
3664 -- on the placement of its corresponding body. From elaboration point of
3665 -- view, the flow of execution may enter this private context, and then
3666 -- reach an external unit, thus producing a dependency on that external
3667 -- unit. For such a path to be properly discovered and encoded in the
3668 -- ALI file of the main unit, let the ABE mechanism process the body of
3669 -- the main unit, and encode all relevant invocation constructs and the
3670 -- relations between them.
3672 Mark_Save_Invocation_Graph_Of_Body;
3674 -- We introduce a renaming of the enclosing package, to have a usable
3675 -- entity as the prefix of an expanded name for a local entity of the
3676 -- form Par.P.Q, where P is the generic package. This is because a local
3677 -- entity named P may hide it, so that the usual visibility rules in
3678 -- the instance will not resolve properly.
3680 Renaming :=
3681 Make_Package_Renaming_Declaration (Loc,
3682 Defining_Unit_Name =>
3683 Make_Defining_Identifier (Loc,
3684 Chars => New_External_Name (Chars (Defining_Entity (N)), "GH")),
3685 Name =>
3686 Make_Identifier (Loc, Chars (Defining_Entity (N))));
3688 -- The declaration is inserted before other declarations, but before
3689 -- pragmas that may be library-unit pragmas and must appear before other
3690 -- declarations. The pragma Compile_Time_Error is not in this class, and
3691 -- may contain an expression that includes such a qualified name, so the
3692 -- renaming declaration must appear before it.
3694 -- Are there other pragmas that require this special handling ???
3696 if Present (Decls) then
3697 Decl := First (Decls);
3698 while Present (Decl)
3699 and then Nkind (Decl) = N_Pragma
3700 and then Get_Pragma_Id (Decl) /= Pragma_Compile_Time_Error
3701 loop
3702 Next (Decl);
3703 end loop;
3705 if Present (Decl) then
3706 Insert_Before (Decl, Renaming);
3707 else
3708 Append (Renaming, Visible_Declarations (Specification (N)));
3709 end if;
3711 else
3712 Set_Visible_Declarations (Specification (N), New_List (Renaming));
3713 end if;
3715 -- Create copy of generic unit, and save for instantiation. If the unit
3716 -- is a child unit, do not copy the specifications for the parent, which
3717 -- are not part of the generic tree.
3719 Save_Parent := Parent_Spec (N);
3720 Set_Parent_Spec (N, Empty);
3722 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
3723 Set_Parent_Spec (New_N, Save_Parent);
3724 Rewrite (N, New_N);
3726 -- Once the contents of the generic copy and the template are swapped,
3727 -- do the same for their respective aspect specifications.
3729 Exchange_Aspects (N, New_N);
3731 -- Collect all contract-related source pragmas found within the template
3732 -- and attach them to the contract of the package spec. This contract is
3733 -- used in the capture of global references within annotations.
3735 Create_Generic_Contract (N);
3737 Id := Defining_Entity (N);
3738 Generate_Definition (Id);
3740 -- Expansion is not applied to generic units
3742 Start_Generic;
3744 Enter_Name (Id);
3745 Mutate_Ekind (Id, E_Generic_Package);
3746 Set_Etype (Id, Standard_Void_Type);
3748 -- Set SPARK_Mode from context
3750 Set_SPARK_Pragma (Id, SPARK_Mode_Pragma);
3751 Set_SPARK_Aux_Pragma (Id, SPARK_Mode_Pragma);
3752 Set_SPARK_Pragma_Inherited (Id);
3753 Set_SPARK_Aux_Pragma_Inherited (Id);
3755 -- Preserve relevant elaboration-related attributes of the context which
3756 -- are no longer available or very expensive to recompute once analysis,
3757 -- resolution, and expansion are over.
3759 Mark_Elaboration_Attributes
3760 (N_Id => Id,
3761 Checks => True,
3762 Warnings => True);
3764 -- Analyze aspects now, so that generated pragmas appear in the
3765 -- declarations before building and analyzing the generic copy.
3767 if Has_Aspects (N) then
3768 Analyze_Aspect_Specifications (N, Id);
3769 end if;
3771 Push_Scope (Id);
3772 Enter_Generic_Scope (Id);
3773 Set_Inner_Instances (Id, New_Elmt_List);
3775 Set_Categorization_From_Pragmas (N);
3776 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3778 -- Link the declaration of the generic homonym in the generic copy to
3779 -- the package it renames, so that it is always resolved properly.
3781 Set_Generic_Homonym (Id, Defining_Unit_Name (Renaming));
3782 Set_Entity (Associated_Node (Name (Renaming)), Id);
3784 -- For a library unit, we have reconstructed the entity for the unit,
3785 -- and must reset it in the library tables.
3787 if Nkind (Parent (N)) = N_Compilation_Unit then
3788 Set_Cunit_Entity (Current_Sem_Unit, Id);
3789 end if;
3791 Analyze_Generic_Formal_Part (N);
3793 -- After processing the generic formals, analysis proceeds as for a
3794 -- non-generic package.
3796 Analyze (Specification (N));
3798 Validate_Categorization_Dependency (N, Id);
3800 End_Generic;
3802 End_Package_Scope (Id);
3803 Exit_Generic_Scope (Id);
3805 -- If the generic appears within a package unit, the body of that unit
3806 -- has to be present for instantiation and inlining.
3808 if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Declaration then
3809 Set_Body_Needed_For_Inlining
3810 (Defining_Entity (Unit (Cunit (Current_Sem_Unit))));
3811 end if;
3813 if Nkind (Parent (N)) /= N_Compilation_Unit then
3814 Move_Freeze_Nodes (Id, N, Visible_Declarations (Specification (N)));
3815 Move_Freeze_Nodes (Id, N, Private_Declarations (Specification (N)));
3816 Move_Freeze_Nodes (Id, N, Generic_Formal_Declarations (N));
3818 else
3819 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
3820 Validate_RT_RAT_Component (N);
3822 -- If this is a spec without a body, check that generic parameters
3823 -- are referenced.
3825 if not Body_Required (Parent (N)) then
3826 Check_References (Id);
3827 end if;
3828 end if;
3830 -- If there is a specified storage pool in the context, create an
3831 -- aspect on the package declaration, so that it is used in any
3832 -- instance that does not override it.
3834 if Present (Default_Pool) then
3835 declare
3836 ASN : Node_Id;
3838 begin
3839 ASN :=
3840 Make_Aspect_Specification (Loc,
3841 Identifier => Make_Identifier (Loc, Name_Default_Storage_Pool),
3842 Expression => New_Copy (Default_Pool));
3844 if No (Aspect_Specifications (Specification (N))) then
3845 Set_Aspect_Specifications (Specification (N), New_List (ASN));
3846 else
3847 Append (ASN, Aspect_Specifications (Specification (N)));
3848 end if;
3849 end;
3850 end if;
3851 end Analyze_Generic_Package_Declaration;
3853 --------------------------------------------
3854 -- Analyze_Generic_Subprogram_Declaration --
3855 --------------------------------------------
3857 procedure Analyze_Generic_Subprogram_Declaration (N : Node_Id) is
3858 Formals : List_Id;
3859 Id : Entity_Id;
3860 New_N : Node_Id;
3861 Result_Type : Entity_Id;
3862 Save_Parent : Node_Id;
3863 Spec : Node_Id;
3864 Typ : Entity_Id;
3866 begin
3867 -- A generic may grant access to its private enclosing context depending
3868 -- on the placement of its corresponding body. From elaboration point of
3869 -- view, the flow of execution may enter this private context, and then
3870 -- reach an external unit, thus producing a dependency on that external
3871 -- unit. For such a path to be properly discovered and encoded in the
3872 -- ALI file of the main unit, let the ABE mechanism process the body of
3873 -- the main unit, and encode all relevant invocation constructs and the
3874 -- relations between them.
3876 Mark_Save_Invocation_Graph_Of_Body;
3878 -- Create copy of generic unit, and save for instantiation. If the unit
3879 -- is a child unit, do not copy the specifications for the parent, which
3880 -- are not part of the generic tree.
3882 Save_Parent := Parent_Spec (N);
3883 Set_Parent_Spec (N, Empty);
3885 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
3886 Set_Parent_Spec (New_N, Save_Parent);
3887 Rewrite (N, New_N);
3889 -- Once the contents of the generic copy and the template are swapped,
3890 -- do the same for their respective aspect specifications.
3892 Exchange_Aspects (N, New_N);
3894 -- Collect all contract-related source pragmas found within the template
3895 -- and attach them to the contract of the subprogram spec. This contract
3896 -- is used in the capture of global references within annotations.
3898 Create_Generic_Contract (N);
3900 Spec := Specification (N);
3901 Id := Defining_Entity (Spec);
3902 Generate_Definition (Id);
3904 if Nkind (Id) = N_Defining_Operator_Symbol then
3905 Error_Msg_N
3906 ("operator symbol not allowed for generic subprogram", Id);
3907 end if;
3909 Start_Generic;
3911 Enter_Name (Id);
3912 Set_Scope_Depth_Value (Id, Scope_Depth (Current_Scope) + 1);
3914 Push_Scope (Id);
3915 Enter_Generic_Scope (Id);
3916 Set_Inner_Instances (Id, New_Elmt_List);
3917 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3919 Analyze_Generic_Formal_Part (N);
3921 if Nkind (Spec) = N_Function_Specification then
3922 Mutate_Ekind (Id, E_Generic_Function);
3923 else
3924 Mutate_Ekind (Id, E_Generic_Procedure);
3925 end if;
3927 -- Set SPARK_Mode from context
3929 Set_SPARK_Pragma (Id, SPARK_Mode_Pragma);
3930 Set_SPARK_Pragma_Inherited (Id);
3932 -- Preserve relevant elaboration-related attributes of the context which
3933 -- are no longer available or very expensive to recompute once analysis,
3934 -- resolution, and expansion are over.
3936 Mark_Elaboration_Attributes
3937 (N_Id => Id,
3938 Checks => True,
3939 Warnings => True);
3941 Formals := Parameter_Specifications (Spec);
3943 if Present (Formals) then
3944 Process_Formals (Formals, Spec);
3945 end if;
3947 if Nkind (Spec) = N_Function_Specification then
3948 if Nkind (Result_Definition (Spec)) = N_Access_Definition then
3949 Result_Type := Access_Definition (Spec, Result_Definition (Spec));
3950 Set_Etype (Id, Result_Type);
3952 -- Check restriction imposed by AI05-073: a generic function
3953 -- cannot return an abstract type or an access to such.
3955 if Is_Abstract_Type (Designated_Type (Result_Type)) then
3956 Error_Msg_N
3957 ("generic function cannot have an access result "
3958 & "that designates an abstract type", Spec);
3959 end if;
3961 else
3962 Find_Type (Result_Definition (Spec));
3963 Typ := Entity (Result_Definition (Spec));
3965 if Is_Abstract_Type (Typ)
3966 and then Ada_Version >= Ada_2012
3967 then
3968 Error_Msg_N
3969 ("generic function cannot have abstract result type", Spec);
3970 end if;
3972 -- If a null exclusion is imposed on the result type, then create
3973 -- a null-excluding itype (an access subtype) and use it as the
3974 -- function's Etype.
3976 if Is_Access_Type (Typ)
3977 and then Null_Exclusion_Present (Spec)
3978 then
3979 Set_Etype (Id,
3980 Create_Null_Excluding_Itype
3981 (T => Typ,
3982 Related_Nod => Spec,
3983 Scope_Id => Defining_Unit_Name (Spec)));
3984 else
3985 Set_Etype (Id, Typ);
3986 end if;
3987 end if;
3989 else
3990 Set_Etype (Id, Standard_Void_Type);
3991 end if;
3993 -- Analyze the aspects of the generic copy to ensure that all generated
3994 -- pragmas (if any) perform their semantic effects.
3996 if Has_Aspects (N) then
3997 Analyze_Aspect_Specifications (N, Id);
3998 end if;
4000 -- For a library unit, we have reconstructed the entity for the unit,
4001 -- and must reset it in the library tables. We also make sure that
4002 -- Body_Required is set properly in the original compilation unit node.
4004 if Nkind (Parent (N)) = N_Compilation_Unit then
4005 Set_Cunit_Entity (Current_Sem_Unit, Id);
4006 Set_Body_Required (Parent (N), Unit_Requires_Body (Id));
4007 end if;
4009 -- If the generic appears within a package unit, the body of that unit
4010 -- has to be present for instantiation and inlining.
4012 if Nkind (Unit (Cunit (Current_Sem_Unit))) = N_Package_Declaration
4013 and then Unit_Requires_Body (Id)
4014 then
4015 Set_Body_Needed_For_Inlining
4016 (Defining_Entity (Unit (Cunit (Current_Sem_Unit))));
4017 end if;
4019 Set_Categorization_From_Pragmas (N);
4020 Validate_Categorization_Dependency (N, Id);
4022 -- Capture all global references that occur within the profile of the
4023 -- generic subprogram. Aspects are not part of this processing because
4024 -- they must be delayed. If processed now, Save_Global_References will
4025 -- destroy the Associated_Node links and prevent the capture of global
4026 -- references when the contract of the generic subprogram is analyzed.
4028 Save_Global_References (Original_Node (N));
4030 End_Generic;
4031 End_Scope;
4032 Exit_Generic_Scope (Id);
4033 Generate_Reference_To_Formals (Id);
4035 List_Inherited_Pre_Post_Aspects (Id);
4036 end Analyze_Generic_Subprogram_Declaration;
4038 -----------------------------------
4039 -- Analyze_Package_Instantiation --
4040 -----------------------------------
4042 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
4043 -- must be replaced by gotos which jump to the end of the routine in order
4044 -- to restore the Ghost and SPARK modes.
4046 procedure Analyze_Package_Instantiation (N : Node_Id) is
4047 Has_Inline_Always : Boolean := False;
4048 -- Set if the generic unit contains any subprograms with Inline_Always.
4049 -- Only relevant when back-end inlining is not enabled.
4051 function Might_Inline_Subp (Gen_Unit : Entity_Id) return Boolean;
4052 -- Return True if inlining is active and Gen_Unit contains inlined
4053 -- subprograms. In this case, we may either instantiate the body when
4054 -- front-end inlining is enabled, or add a pending instantiation when
4055 -- back-end inlining is enabled. In the former case, this may cause
4056 -- superfluous instantiations, but in either case we need to perform
4057 -- the instantiation of the body in the context of the instance and
4058 -- not in that of the point of inlining.
4060 function Needs_Body_Instantiated (Gen_Unit : Entity_Id) return Boolean;
4061 -- Return True if Gen_Unit needs to have its body instantiated in the
4062 -- context of N. This in particular excludes generic contexts.
4064 -----------------------
4065 -- Might_Inline_Subp --
4066 -----------------------
4068 function Might_Inline_Subp (Gen_Unit : Entity_Id) return Boolean is
4069 E : Entity_Id;
4071 begin
4072 if Inline_Processing_Required then
4073 -- No need to recompute the answer if we know it is positive
4074 -- and back-end inlining is enabled.
4076 if Is_Inlined (Gen_Unit) and then Back_End_Inlining then
4077 return True;
4078 end if;
4080 E := First_Entity (Gen_Unit);
4081 while Present (E) loop
4082 if Is_Subprogram (E) and then Is_Inlined (E) then
4083 -- Remember if there are any subprograms with Inline_Always
4085 if Has_Pragma_Inline_Always (E) then
4086 Has_Inline_Always := True;
4087 end if;
4089 Set_Is_Inlined (Gen_Unit);
4090 return True;
4091 end if;
4093 Next_Entity (E);
4094 end loop;
4095 end if;
4097 return False;
4098 end Might_Inline_Subp;
4100 -------------------------------
4101 -- Needs_Body_Instantiated --
4102 -------------------------------
4104 function Needs_Body_Instantiated (Gen_Unit : Entity_Id) return Boolean is
4105 begin
4106 -- No need to instantiate bodies in generic units
4108 if Is_Generic_Unit (Cunit_Entity (Main_Unit)) then
4109 return False;
4110 end if;
4112 -- If the instantiation is in the main unit, then the body is needed
4114 if Is_In_Main_Unit (N) then
4115 return True;
4116 end if;
4118 -- In GNATprove mode, never instantiate bodies outside of the main
4119 -- unit, as it does not use frontend/backend inlining in the way that
4120 -- GNAT does, so does not benefit from such instantiations. On the
4121 -- contrary, such instantiations may bring artificial constraints,
4122 -- as for example such bodies may require preprocessing.
4124 if GNATprove_Mode then
4125 return False;
4126 end if;
4128 -- If not, then again no need to instantiate bodies in generic units
4130 if Is_Generic_Unit (Cunit_Entity (Get_Code_Unit (N))) then
4131 return False;
4132 end if;
4134 -- Here we have a special handling for back-end inlining: if inline
4135 -- processing is required, then we unconditionally want to have the
4136 -- body instantiated. The reason is that Might_Inline_Subp does not
4137 -- catch all the cases (as it does not recurse into nested packages)
4138 -- so this avoids the need to patch things up afterwards. Moreover,
4139 -- these instantiations are only performed on demand when back-end
4140 -- inlining is enabled, so this causes very little extra work.
4142 if Inline_Processing_Required and then Back_End_Inlining then
4143 return True;
4144 end if;
4146 -- We want to have the bodies instantiated in non-main units if
4147 -- they might contribute inlined subprograms.
4149 return Might_Inline_Subp (Gen_Unit);
4150 end Needs_Body_Instantiated;
4152 -- Local declarations
4154 Gen_Id : constant Node_Id := Name (N);
4155 Inst_Id : constant Entity_Id := Defining_Entity (N);
4156 Is_Actual_Pack : constant Boolean := Is_Internal (Inst_Id);
4157 Loc : constant Source_Ptr := Sloc (N);
4159 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
4160 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
4161 Saved_ISMP : constant Boolean :=
4162 Ignore_SPARK_Mode_Pragmas_In_Instance;
4163 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
4164 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
4165 -- Save the Ghost and SPARK mode-related data to restore on exit
4167 Saved_Style_Check : constant Boolean := Style_Check;
4168 -- Save style check mode for restore on exit
4170 Act_Decl : Node_Id;
4171 Act_Decl_Name : Node_Id;
4172 Act_Decl_Id : Entity_Id;
4173 Act_Spec : Node_Id;
4174 Act_Tree : Node_Id;
4175 Env_Installed : Boolean := False;
4176 Gen_Decl : Node_Id;
4177 Gen_Spec : Node_Id;
4178 Gen_Unit : Entity_Id;
4179 Inline_Now : Boolean := False;
4180 Needs_Body : Boolean;
4181 Parent_Installed : Boolean := False;
4182 Renaming_List : List_Id;
4183 Unit_Renaming : Node_Id;
4185 Vis_Prims_List : Elist_Id := No_Elist;
4186 -- List of primitives made temporarily visible in the instantiation
4187 -- to match the visibility of the formal type
4189 -- Start of processing for Analyze_Package_Instantiation
4191 begin
4192 -- Preserve relevant elaboration-related attributes of the context which
4193 -- are no longer available or very expensive to recompute once analysis,
4194 -- resolution, and expansion are over.
4196 Mark_Elaboration_Attributes
4197 (N_Id => N,
4198 Checks => True,
4199 Level => True,
4200 Modes => True,
4201 Warnings => True);
4203 -- Very first thing: check for Text_IO special unit in case we are
4204 -- instantiating one of the children of [[Wide_]Wide_]Text_IO.
4206 Check_Text_IO_Special_Unit (Name (N));
4208 -- Make node global for error reporting
4210 Instantiation_Node := N;
4212 -- Case of instantiation of a generic package
4214 if Nkind (N) = N_Package_Instantiation then
4215 Act_Decl_Id := New_Copy (Defining_Entity (N));
4216 Set_Comes_From_Source (Act_Decl_Id, True);
4218 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name then
4219 Act_Decl_Name :=
4220 Make_Defining_Program_Unit_Name (Loc,
4221 Name =>
4222 New_Copy_Tree (Name (Defining_Unit_Name (N))),
4223 Defining_Identifier => Act_Decl_Id);
4224 else
4225 Act_Decl_Name := Act_Decl_Id;
4226 end if;
4228 -- Case of instantiation of a formal package
4230 else
4231 Act_Decl_Id := Defining_Identifier (N);
4232 Act_Decl_Name := Act_Decl_Id;
4233 end if;
4235 Generate_Definition (Act_Decl_Id);
4236 Mutate_Ekind (Act_Decl_Id, E_Package);
4238 -- Initialize list of incomplete actuals before analysis
4240 Set_Incomplete_Actuals (Act_Decl_Id, New_Elmt_List);
4242 Preanalyze_Actuals (N, Act_Decl_Id);
4244 -- Turn off style checking in instances. If the check is enabled on the
4245 -- generic unit, a warning in an instance would just be noise. If not
4246 -- enabled on the generic, then a warning in an instance is just wrong.
4247 -- This must be done after analyzing the actuals, which do come from
4248 -- source and are subject to style checking.
4250 Style_Check := False;
4252 Init_Env;
4253 Env_Installed := True;
4255 -- Reset renaming map for formal types. The mapping is established
4256 -- when analyzing the generic associations, but some mappings are
4257 -- inherited from formal packages of parent units, and these are
4258 -- constructed when the parents are installed.
4260 Generic_Renamings.Set_Last (0);
4261 Generic_Renamings_HTable.Reset;
4263 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
4264 Gen_Unit := Entity (Gen_Id);
4266 -- A package instantiation is Ghost when it is subject to pragma Ghost
4267 -- or the generic template is Ghost. Set the mode now to ensure that
4268 -- any nodes generated during analysis and expansion are marked as
4269 -- Ghost.
4271 Mark_And_Set_Ghost_Instantiation (N, Gen_Unit);
4273 -- Verify that it is the name of a generic package
4275 -- A visibility glitch: if the instance is a child unit and the generic
4276 -- is the generic unit of a parent instance (i.e. both the parent and
4277 -- the child units are instances of the same package) the name now
4278 -- denotes the renaming within the parent, not the intended generic
4279 -- unit. See if there is a homonym that is the desired generic. The
4280 -- renaming declaration must be visible inside the instance of the
4281 -- child, but not when analyzing the name in the instantiation itself.
4283 if Ekind (Gen_Unit) = E_Package
4284 and then Present (Renamed_Entity (Gen_Unit))
4285 and then In_Open_Scopes (Renamed_Entity (Gen_Unit))
4286 and then Is_Generic_Instance (Renamed_Entity (Gen_Unit))
4287 and then Present (Homonym (Gen_Unit))
4288 then
4289 Gen_Unit := Homonym (Gen_Unit);
4290 end if;
4292 if Etype (Gen_Unit) = Any_Type then
4293 Restore_Env;
4294 goto Leave;
4296 elsif Ekind (Gen_Unit) /= E_Generic_Package then
4298 -- Ada 2005 (AI-50217): Cannot use instance in limited with_clause
4300 if From_Limited_With (Gen_Unit) then
4301 Error_Msg_N
4302 ("cannot instantiate a limited withed package", Gen_Id);
4303 else
4304 Error_Msg_NE
4305 ("& is not the name of a generic package", Gen_Id, Gen_Unit);
4306 end if;
4308 Restore_Env;
4309 goto Leave;
4310 end if;
4312 if In_Extended_Main_Source_Unit (N) then
4313 Set_Is_Instantiated (Gen_Unit);
4314 Generate_Reference (Gen_Unit, N);
4316 if Present (Renamed_Entity (Gen_Unit)) then
4317 Set_Is_Instantiated (Renamed_Entity (Gen_Unit));
4318 Generate_Reference (Renamed_Entity (Gen_Unit), N);
4319 end if;
4320 end if;
4322 if Nkind (Gen_Id) = N_Identifier
4323 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
4324 then
4325 Error_Msg_NE
4326 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
4328 elsif Nkind (Gen_Id) = N_Expanded_Name
4329 and then Is_Child_Unit (Gen_Unit)
4330 and then Nkind (Prefix (Gen_Id)) = N_Identifier
4331 and then Chars (Act_Decl_Id) = Chars (Prefix (Gen_Id))
4332 then
4333 Error_Msg_N
4334 ("& is hidden within declaration of instance", Prefix (Gen_Id));
4335 end if;
4337 Set_Entity (Gen_Id, Gen_Unit);
4339 -- If generic is a renaming, get original generic unit
4341 if Present (Renamed_Entity (Gen_Unit))
4342 and then Ekind (Renamed_Entity (Gen_Unit)) = E_Generic_Package
4343 then
4344 Gen_Unit := Renamed_Entity (Gen_Unit);
4345 end if;
4347 -- Verify that there are no circular instantiations
4349 if In_Open_Scopes (Gen_Unit) then
4350 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
4351 Restore_Env;
4352 goto Leave;
4354 elsif Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
4355 Error_Msg_Node_2 := Current_Scope;
4356 Error_Msg_NE
4357 ("circular instantiation: & instantiated in &!", N, Gen_Unit);
4358 Circularity_Detected := True;
4359 Restore_Env;
4360 goto Leave;
4362 else
4363 Mutate_Ekind (Inst_Id, E_Package);
4364 Set_Scope (Inst_Id, Current_Scope);
4366 -- If the context of the instance is subject to SPARK_Mode "off" or
4367 -- the annotation is altogether missing, set the global flag which
4368 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
4369 -- the instance.
4371 if SPARK_Mode /= On then
4372 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
4374 -- Mark the instance spec in case the body is instantiated at a
4375 -- later pass. This preserves the original context in effect for
4376 -- the body.
4378 Set_Ignore_SPARK_Mode_Pragmas (Act_Decl_Id);
4379 end if;
4381 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
4382 Gen_Spec := Specification (Gen_Decl);
4384 -- Initialize renamings map, for error checking, and the list that
4385 -- holds private entities whose views have changed between generic
4386 -- definition and instantiation. If this is the instance created to
4387 -- validate an actual package, the instantiation environment is that
4388 -- of the enclosing instance.
4390 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
4392 -- Copy original generic tree, to produce text for instantiation
4394 Act_Tree :=
4395 Copy_Generic_Node
4396 (Original_Node (Gen_Decl), Empty, Instantiating => True);
4398 Act_Spec := Specification (Act_Tree);
4400 -- If this is the instance created to validate an actual package,
4401 -- only the formals matter, do not examine the package spec itself.
4403 if Is_Actual_Pack then
4404 Set_Visible_Declarations (Act_Spec, New_List);
4405 Set_Private_Declarations (Act_Spec, New_List);
4406 end if;
4408 Renaming_List :=
4409 Analyze_Associations
4410 (I_Node => N,
4411 Formals => Generic_Formal_Declarations (Act_Tree),
4412 F_Copy => Generic_Formal_Declarations (Gen_Decl));
4414 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
4416 Set_Instance_Env (Gen_Unit, Act_Decl_Id);
4417 Set_Defining_Unit_Name (Act_Spec, Act_Decl_Name);
4418 Set_Is_Generic_Instance (Act_Decl_Id);
4419 Set_Generic_Parent (Act_Spec, Gen_Unit);
4421 -- References to the generic in its own declaration or its body are
4422 -- references to the instance. Add a renaming declaration for the
4423 -- generic unit itself. This declaration, as well as the renaming
4424 -- declarations for the generic formals, must remain private to the
4425 -- unit: the formals, because this is the language semantics, and
4426 -- the unit because its use is an artifact of the implementation.
4428 Unit_Renaming :=
4429 Make_Package_Renaming_Declaration (Loc,
4430 Defining_Unit_Name =>
4431 Make_Defining_Identifier (Loc, Chars (Gen_Unit)),
4432 Name => New_Occurrence_Of (Act_Decl_Id, Loc));
4434 Append (Unit_Renaming, Renaming_List);
4436 -- The renaming declarations are the first local declarations of the
4437 -- new unit.
4439 if Is_Non_Empty_List (Visible_Declarations (Act_Spec)) then
4440 Insert_List_Before
4441 (First (Visible_Declarations (Act_Spec)), Renaming_List);
4442 else
4443 Set_Visible_Declarations (Act_Spec, Renaming_List);
4444 end if;
4446 Act_Decl := Make_Package_Declaration (Loc, Specification => Act_Spec);
4448 -- Propagate the aspect specifications from the package declaration
4449 -- template to the instantiated version of the package declaration.
4451 if Has_Aspects (Act_Tree) then
4452 Set_Aspect_Specifications (Act_Decl,
4453 New_Copy_List_Tree (Aspect_Specifications (Act_Tree)));
4454 end if;
4456 -- The generic may have a generated Default_Storage_Pool aspect,
4457 -- set at the point of generic declaration. If the instance has
4458 -- that aspect, it overrides the one inherited from the generic.
4460 if Has_Aspects (Gen_Spec) then
4461 if No (Aspect_Specifications (N)) then
4462 Set_Aspect_Specifications (N,
4463 (New_Copy_List_Tree
4464 (Aspect_Specifications (Gen_Spec))));
4466 else
4467 declare
4468 Inherited_Aspects : constant List_Id :=
4469 New_Copy_List_Tree
4470 (Aspect_Specifications (Gen_Spec));
4472 ASN1 : Node_Id;
4473 ASN2 : Node_Id;
4474 Pool_Present : Boolean := False;
4476 begin
4477 ASN1 := First (Aspect_Specifications (N));
4478 while Present (ASN1) loop
4479 if Chars (Identifier (ASN1)) =
4480 Name_Default_Storage_Pool
4481 then
4482 Pool_Present := True;
4483 exit;
4484 end if;
4486 Next (ASN1);
4487 end loop;
4489 if Pool_Present then
4491 -- If generic carries a default storage pool, remove it
4492 -- in favor of the instance one.
4494 ASN2 := First (Inherited_Aspects);
4495 while Present (ASN2) loop
4496 if Chars (Identifier (ASN2)) =
4497 Name_Default_Storage_Pool
4498 then
4499 Remove (ASN2);
4500 exit;
4501 end if;
4503 Next (ASN2);
4504 end loop;
4505 end if;
4507 Prepend_List_To
4508 (Aspect_Specifications (N), Inherited_Aspects);
4509 end;
4510 end if;
4511 end if;
4513 -- Save the instantiation node for a subsequent instantiation of the
4514 -- body if there is one and it needs to be instantiated here.
4516 -- We instantiate the body only if we are generating code, or if we
4517 -- are generating cross-reference information, or for GNATprove use.
4519 declare
4520 Enclosing_Body_Present : Boolean := False;
4521 -- If the generic unit is not a compilation unit, then a body may
4522 -- be present in its parent even if none is required. We create a
4523 -- tentative pending instantiation for the body, which will be
4524 -- discarded if none is actually present.
4526 Scop : Entity_Id;
4528 begin
4529 if Scope (Gen_Unit) /= Standard_Standard
4530 and then not Is_Child_Unit (Gen_Unit)
4531 then
4532 Scop := Scope (Gen_Unit);
4533 while Present (Scop) and then Scop /= Standard_Standard loop
4534 if Unit_Requires_Body (Scop) then
4535 Enclosing_Body_Present := True;
4536 exit;
4538 elsif In_Open_Scopes (Scop)
4539 and then In_Package_Body (Scop)
4540 then
4541 Enclosing_Body_Present := True;
4542 exit;
4543 end if;
4545 exit when Is_Compilation_Unit (Scop);
4546 Scop := Scope (Scop);
4547 end loop;
4548 end if;
4550 -- If front-end inlining is enabled or there are any subprograms
4551 -- marked with Inline_Always, and this is a unit for which code
4552 -- will be generated, we instantiate the body at once.
4554 -- This is done if the instance is not the main unit, and if the
4555 -- generic is not a child unit of another generic, to avoid scope
4556 -- problems and the reinstallation of parent instances.
4558 if Expander_Active
4559 and then (not Is_Child_Unit (Gen_Unit)
4560 or else not Is_Generic_Unit (Scope (Gen_Unit)))
4561 and then Might_Inline_Subp (Gen_Unit)
4562 and then not Is_Actual_Pack
4563 then
4564 if not Back_End_Inlining
4565 and then (Front_End_Inlining or else Has_Inline_Always)
4566 and then (Is_In_Main_Unit (N)
4567 or else In_Main_Context (Current_Scope))
4568 and then Nkind (Parent (N)) /= N_Compilation_Unit
4569 then
4570 Inline_Now := True;
4572 -- In configurable_run_time mode we force the inlining of
4573 -- predefined subprograms marked Inline_Always, to minimize
4574 -- the use of the run-time library.
4576 elsif In_Predefined_Unit (Gen_Decl)
4577 and then Configurable_Run_Time_Mode
4578 and then Nkind (Parent (N)) /= N_Compilation_Unit
4579 then
4580 Inline_Now := True;
4581 end if;
4583 -- If the current scope is itself an instance within a child
4584 -- unit, there will be duplications in the scope stack, and the
4585 -- unstacking mechanism in Inline_Instance_Body will fail.
4586 -- This loses some rare cases of optimization.
4588 if Is_Generic_Instance (Current_Scope) then
4589 declare
4590 Curr_Unit : constant Entity_Id :=
4591 Cunit_Entity (Current_Sem_Unit);
4592 begin
4593 if Curr_Unit /= Current_Scope
4594 and then Is_Child_Unit (Curr_Unit)
4595 then
4596 Inline_Now := False;
4597 end if;
4598 end;
4599 end if;
4600 end if;
4602 Needs_Body :=
4603 (Unit_Requires_Body (Gen_Unit)
4604 or else Enclosing_Body_Present
4605 or else Present (Corresponding_Body (Gen_Decl)))
4606 and then Needs_Body_Instantiated (Gen_Unit)
4607 and then not Is_Actual_Pack
4608 and then not Inline_Now
4609 and then (Operating_Mode = Generate_Code
4610 or else (Operating_Mode = Check_Semantics
4611 and then GNATprove_Mode));
4613 -- If front-end inlining is enabled or there are any subprograms
4614 -- marked with Inline_Always, do not instantiate body when within
4615 -- a generic context.
4617 if not Back_End_Inlining
4618 and then (Front_End_Inlining or else Has_Inline_Always)
4619 and then not Expander_Active
4620 then
4621 Needs_Body := False;
4622 end if;
4624 -- If the current context is generic, and the package being
4625 -- instantiated is declared within a formal package, there is no
4626 -- body to instantiate until the enclosing generic is instantiated
4627 -- and there is an actual for the formal package. If the formal
4628 -- package has parameters, we build a regular package instance for
4629 -- it, that precedes the original formal package declaration.
4631 if In_Open_Scopes (Scope (Scope (Gen_Unit))) then
4632 declare
4633 Decl : constant Node_Id :=
4634 Original_Node
4635 (Unit_Declaration_Node (Scope (Gen_Unit)));
4636 begin
4637 if Nkind (Decl) = N_Formal_Package_Declaration
4638 or else (Nkind (Decl) = N_Package_Declaration
4639 and then Is_List_Member (Decl)
4640 and then Present (Next (Decl))
4641 and then
4642 Nkind (Next (Decl)) =
4643 N_Formal_Package_Declaration)
4644 then
4645 Needs_Body := False;
4646 end if;
4647 end;
4648 end if;
4649 end;
4651 -- For RCI unit calling stubs, we omit the instance body if the
4652 -- instance is the RCI library unit itself.
4654 -- However there is a special case for nested instances: in this case
4655 -- we do generate the instance body, as it might be required, e.g.
4656 -- because it provides stream attributes for some type used in the
4657 -- profile of a remote subprogram. This is consistent with 12.3(12),
4658 -- which indicates that the instance body occurs at the place of the
4659 -- instantiation, and thus is part of the RCI declaration, which is
4660 -- present on all client partitions (this is E.2.3(18)).
4662 -- Note that AI12-0002 may make it illegal at some point to have
4663 -- stream attributes defined in an RCI unit, in which case this
4664 -- special case will become unnecessary. In the meantime, there
4665 -- is known application code in production that depends on this
4666 -- being possible, so we definitely cannot eliminate the body in
4667 -- the case of nested instances for the time being.
4669 -- When we generate a nested instance body, calling stubs for any
4670 -- relevant subprogram will be inserted immediately after the
4671 -- subprogram declarations, and will take precedence over the
4672 -- subsequent (original) body. (The stub and original body will be
4673 -- complete homographs, but this is permitted in an instance).
4674 -- (Could we do better and remove the original body???)
4676 if Distribution_Stub_Mode = Generate_Caller_Stub_Body
4677 and then Comes_From_Source (N)
4678 and then Nkind (Parent (N)) = N_Compilation_Unit
4679 then
4680 Needs_Body := False;
4681 end if;
4683 if Needs_Body then
4684 -- Indicate that the enclosing scopes contain an instantiation,
4685 -- and that cleanup actions should be delayed until after the
4686 -- instance body is expanded.
4688 Check_Forward_Instantiation (Gen_Decl);
4689 if Nkind (N) = N_Package_Instantiation then
4690 declare
4691 Enclosing_Master : Entity_Id;
4693 begin
4694 -- Loop to search enclosing masters
4696 Enclosing_Master := Current_Scope;
4697 Scope_Loop : while Enclosing_Master /= Standard_Standard loop
4698 if Ekind (Enclosing_Master) = E_Package then
4699 if Is_Compilation_Unit (Enclosing_Master) then
4700 if In_Package_Body (Enclosing_Master) then
4701 Set_Delay_Subprogram_Descriptors
4702 (Body_Entity (Enclosing_Master));
4703 else
4704 Set_Delay_Subprogram_Descriptors
4705 (Enclosing_Master);
4706 end if;
4708 exit Scope_Loop;
4710 else
4711 Enclosing_Master := Scope (Enclosing_Master);
4712 end if;
4714 elsif Is_Generic_Unit (Enclosing_Master)
4715 or else Ekind (Enclosing_Master) = E_Void
4716 then
4717 -- Cleanup actions will eventually be performed on the
4718 -- enclosing subprogram or package instance, if any.
4719 -- Enclosing scope is void in the formal part of a
4720 -- generic subprogram.
4722 exit Scope_Loop;
4724 else
4725 if Ekind (Enclosing_Master) = E_Entry
4726 and then
4727 Ekind (Scope (Enclosing_Master)) = E_Protected_Type
4728 then
4729 if not Expander_Active then
4730 exit Scope_Loop;
4731 else
4732 Enclosing_Master :=
4733 Protected_Body_Subprogram (Enclosing_Master);
4734 end if;
4735 end if;
4737 Set_Delay_Cleanups (Enclosing_Master);
4739 while Ekind (Enclosing_Master) = E_Block loop
4740 Enclosing_Master := Scope (Enclosing_Master);
4741 end loop;
4743 if Is_Subprogram (Enclosing_Master) then
4744 Set_Delay_Subprogram_Descriptors (Enclosing_Master);
4746 elsif Is_Task_Type (Enclosing_Master) then
4747 declare
4748 TBP : constant Node_Id :=
4749 Get_Task_Body_Procedure
4750 (Enclosing_Master);
4751 begin
4752 if Present (TBP) then
4753 Set_Delay_Subprogram_Descriptors (TBP);
4754 Set_Delay_Cleanups (TBP);
4755 end if;
4756 end;
4757 end if;
4759 exit Scope_Loop;
4760 end if;
4761 end loop Scope_Loop;
4762 end;
4764 -- Make entry in table
4766 Add_Pending_Instantiation (N, Act_Decl);
4767 end if;
4768 end if;
4770 Set_Categorization_From_Pragmas (Act_Decl);
4772 if Parent_Installed then
4773 Hide_Current_Scope;
4774 end if;
4776 Set_Instance_Spec (N, Act_Decl);
4778 -- If not a compilation unit, insert the package declaration before
4779 -- the original instantiation node.
4781 if Nkind (Parent (N)) /= N_Compilation_Unit then
4782 Mark_Rewrite_Insertion (Act_Decl);
4783 Insert_Before (N, Act_Decl);
4785 if Has_Aspects (N) then
4786 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4788 -- The pragma created for a Default_Storage_Pool aspect must
4789 -- appear ahead of the declarations in the instance spec.
4790 -- Analysis has placed it after the instance node, so remove
4791 -- it and reinsert it properly now.
4793 declare
4794 ASN : constant Node_Id := First (Aspect_Specifications (N));
4795 A_Name : constant Name_Id := Chars (Identifier (ASN));
4796 Decl : Node_Id;
4798 begin
4799 if A_Name = Name_Default_Storage_Pool then
4800 if No (Visible_Declarations (Act_Spec)) then
4801 Set_Visible_Declarations (Act_Spec, New_List);
4802 end if;
4804 Decl := Next (N);
4805 while Present (Decl) loop
4806 if Nkind (Decl) = N_Pragma then
4807 Remove (Decl);
4808 Prepend (Decl, Visible_Declarations (Act_Spec));
4809 exit;
4810 end if;
4812 Next (Decl);
4813 end loop;
4814 end if;
4815 end;
4816 end if;
4818 Analyze (Act_Decl);
4820 -- For an instantiation that is a compilation unit, place
4821 -- declaration on current node so context is complete for analysis
4822 -- (including nested instantiations). If this is the main unit,
4823 -- the declaration eventually replaces the instantiation node.
4824 -- If the instance body is created later, it replaces the
4825 -- instance node, and the declaration is attached to it
4826 -- (see Build_Instance_Compilation_Unit_Nodes).
4828 else
4829 if Cunit_Entity (Current_Sem_Unit) = Defining_Entity (N) then
4831 -- The entity for the current unit is the newly created one,
4832 -- and all semantic information is attached to it.
4834 Set_Cunit_Entity (Current_Sem_Unit, Act_Decl_Id);
4836 -- If this is the main unit, replace the main entity as well
4838 if Current_Sem_Unit = Main_Unit then
4839 Main_Unit_Entity := Act_Decl_Id;
4840 end if;
4841 end if;
4843 Set_Unit (Parent (N), Act_Decl);
4844 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
4845 Set_Package_Instantiation (Act_Decl_Id, N);
4847 -- Process aspect specifications of the instance node, if any, to
4848 -- take into account categorization pragmas before analyzing the
4849 -- instance.
4851 if Has_Aspects (N) then
4852 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4853 end if;
4855 Analyze (Act_Decl);
4856 Set_Unit (Parent (N), N);
4857 Set_Body_Required (Parent (N), False);
4859 -- We never need elaboration checks on instantiations, since by
4860 -- definition, the body instantiation is elaborated at the same
4861 -- time as the spec instantiation.
4863 if Legacy_Elaboration_Checks then
4864 Set_Kill_Elaboration_Checks (Act_Decl_Id);
4865 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
4866 end if;
4867 end if;
4869 if Legacy_Elaboration_Checks then
4870 Check_Elab_Instantiation (N);
4871 end if;
4873 -- Save the scenario for later examination by the ABE Processing
4874 -- phase.
4876 Record_Elaboration_Scenario (N);
4878 -- The instantiation results in a guaranteed ABE
4880 if Is_Known_Guaranteed_ABE (N) and then Needs_Body then
4881 -- Do not instantiate the corresponding body because gigi cannot
4882 -- handle certain types of premature instantiations.
4884 Remove_Dead_Instance (N);
4886 -- Create completing bodies for all subprogram declarations since
4887 -- their real bodies will not be instantiated.
4889 Provide_Completing_Bodies (Instance_Spec (N));
4890 end if;
4892 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
4894 Set_First_Private_Entity (Defining_Unit_Name (Unit_Renaming),
4895 First_Private_Entity (Act_Decl_Id));
4897 -- If the instantiation will receive a body, the unit will be
4898 -- transformed into a package body, and receive its own elaboration
4899 -- entity. Otherwise, the nature of the unit is now a package
4900 -- declaration.
4902 if Nkind (Parent (N)) = N_Compilation_Unit
4903 and then not Needs_Body
4904 then
4905 Rewrite (N, Act_Decl);
4906 end if;
4908 if Present (Corresponding_Body (Gen_Decl))
4909 or else Unit_Requires_Body (Gen_Unit)
4910 then
4911 Set_Has_Completion (Act_Decl_Id);
4912 end if;
4914 Check_Formal_Packages (Act_Decl_Id);
4916 Restore_Hidden_Primitives (Vis_Prims_List);
4917 Restore_Private_Views (Act_Decl_Id);
4919 Inherit_Context (Gen_Decl, N);
4921 if Parent_Installed then
4922 Remove_Parent;
4923 end if;
4925 Restore_Env;
4926 Env_Installed := False;
4927 end if;
4929 Validate_Categorization_Dependency (N, Act_Decl_Id);
4931 -- There used to be a check here to prevent instantiations in local
4932 -- contexts if the No_Local_Allocators restriction was active. This
4933 -- check was removed by a binding interpretation in AI-95-00130/07,
4934 -- but we retain the code for documentation purposes.
4936 -- if Ekind (Act_Decl_Id) /= E_Void
4937 -- and then not Is_Library_Level_Entity (Act_Decl_Id)
4938 -- then
4939 -- Check_Restriction (No_Local_Allocators, N);
4940 -- end if;
4942 if Inline_Now then
4943 Inline_Instance_Body (N, Gen_Unit, Act_Decl);
4944 end if;
4946 -- Check that if N is an instantiation of System.Dim_Float_IO or
4947 -- System.Dim_Integer_IO, the formal type has a dimension system.
4949 if Nkind (N) = N_Package_Instantiation
4950 and then Is_Dim_IO_Package_Instantiation (N)
4951 then
4952 declare
4953 Assoc : constant Node_Id := First (Generic_Associations (N));
4954 begin
4955 if not Has_Dimension_System
4956 (Etype (Explicit_Generic_Actual_Parameter (Assoc)))
4957 then
4958 Error_Msg_N ("type with a dimension system expected", Assoc);
4959 end if;
4960 end;
4961 end if;
4963 <<Leave>>
4964 if Has_Aspects (N) and then Nkind (Parent (N)) /= N_Compilation_Unit then
4965 Analyze_Aspect_Specifications (N, Act_Decl_Id);
4966 end if;
4968 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
4969 Restore_Ghost_Region (Saved_GM, Saved_IGR);
4970 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
4971 Style_Check := Saved_Style_Check;
4973 exception
4974 when Instantiation_Error =>
4975 if Parent_Installed then
4976 Remove_Parent;
4977 end if;
4979 if Env_Installed then
4980 Restore_Env;
4981 end if;
4983 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
4984 Restore_Ghost_Region (Saved_GM, Saved_IGR);
4985 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
4986 Style_Check := Saved_Style_Check;
4987 end Analyze_Package_Instantiation;
4989 --------------------------
4990 -- Inline_Instance_Body --
4991 --------------------------
4993 -- WARNING: This routine manages SPARK regions. Return statements must be
4994 -- replaced by gotos which jump to the end of the routine and restore the
4995 -- SPARK mode.
4997 procedure Inline_Instance_Body
4998 (N : Node_Id;
4999 Gen_Unit : Entity_Id;
5000 Act_Decl : Node_Id)
5002 Config_Attrs : constant Config_Switches_Type := Save_Config_Switches;
5004 Curr_Comp : constant Node_Id := Cunit (Current_Sem_Unit);
5005 Curr_Unit : constant Entity_Id := Cunit_Entity (Current_Sem_Unit);
5006 Gen_Comp : constant Entity_Id :=
5007 Cunit_Entity (Get_Source_Unit (Gen_Unit));
5009 Scope_Stack_Depth : constant Pos :=
5010 Scope_Stack.Last - Scope_Stack.First + 1;
5012 Inner_Scopes : array (1 .. Scope_Stack_Depth) of Entity_Id;
5013 Instances : array (1 .. Scope_Stack_Depth) of Entity_Id;
5014 Use_Clauses : array (1 .. Scope_Stack_Depth) of Node_Id;
5016 Curr_Scope : Entity_Id := Empty;
5017 List : Elist_Id := No_Elist; -- init to avoid warning
5018 N_Instances : Nat := 0;
5019 Num_Inner : Nat := 0;
5020 Num_Scopes : Nat := 0;
5021 Removed : Boolean := False;
5022 S : Entity_Id;
5023 Vis : Boolean;
5025 begin
5026 -- Case of generic unit defined in another unit. We must remove the
5027 -- complete context of the current unit to install that of the generic.
5029 if Gen_Comp /= Cunit_Entity (Current_Sem_Unit) then
5031 -- Loop through enclosing scopes until we reach a generic instance,
5032 -- package body, or subprogram.
5034 S := Current_Scope;
5035 while Present (S) and then S /= Standard_Standard loop
5037 -- Save use clauses from enclosing scopes into Use_Clauses
5039 loop
5040 Num_Scopes := Num_Scopes + 1;
5042 Use_Clauses (Num_Scopes) :=
5043 (Scope_Stack.Table
5044 (Scope_Stack.Last - Num_Scopes + 1).First_Use_Clause);
5045 End_Use_Clauses (Use_Clauses (Num_Scopes));
5047 exit when Scope_Stack.Last - Num_Scopes + 1 = Scope_Stack.First
5048 or else Scope_Stack.Table
5049 (Scope_Stack.Last - Num_Scopes).Entity = Scope (S);
5050 end loop;
5052 exit when Is_Generic_Instance (S)
5053 and then (In_Package_Body (S)
5054 or else Ekind (S) = E_Procedure
5055 or else Ekind (S) = E_Function);
5056 S := Scope (S);
5057 end loop;
5059 Vis := Is_Immediately_Visible (Gen_Comp);
5061 -- Find and save all enclosing instances
5063 S := Current_Scope;
5065 while Present (S)
5066 and then S /= Standard_Standard
5067 loop
5068 if Is_Generic_Instance (S) then
5069 N_Instances := N_Instances + 1;
5070 Instances (N_Instances) := S;
5072 exit when In_Package_Body (S);
5073 end if;
5075 S := Scope (S);
5076 end loop;
5078 -- Remove context of current compilation unit, unless we are within a
5079 -- nested package instantiation, in which case the context has been
5080 -- removed previously.
5082 -- If current scope is the body of a child unit, remove context of
5083 -- spec as well. If an enclosing scope is an instance body, the
5084 -- context has already been removed, but the entities in the body
5085 -- must be made invisible as well.
5087 S := Current_Scope;
5088 while Present (S) and then S /= Standard_Standard loop
5089 if Is_Generic_Instance (S)
5090 and then (In_Package_Body (S)
5091 or else Ekind (S) in E_Procedure | E_Function)
5092 then
5093 -- We still have to remove the entities of the enclosing
5094 -- instance from direct visibility.
5096 declare
5097 E : Entity_Id;
5098 begin
5099 E := First_Entity (S);
5100 while Present (E) loop
5101 Set_Is_Immediately_Visible (E, False);
5102 Next_Entity (E);
5103 end loop;
5104 end;
5106 exit;
5107 end if;
5109 if S = Curr_Unit
5110 or else (Ekind (Curr_Unit) = E_Package_Body
5111 and then S = Spec_Entity (Curr_Unit))
5112 or else (Ekind (Curr_Unit) = E_Subprogram_Body
5113 and then S = Corresponding_Spec
5114 (Unit_Declaration_Node (Curr_Unit)))
5115 then
5116 Removed := True;
5118 -- Remove entities in current scopes from visibility, so that
5119 -- instance body is compiled in a clean environment.
5121 List := Save_Scope_Stack (Handle_Use => False);
5123 if Is_Child_Unit (S) then
5125 -- Remove child unit from stack, as well as inner scopes.
5126 -- Removing the context of a child unit removes parent units
5127 -- as well.
5129 while Current_Scope /= S loop
5130 Num_Inner := Num_Inner + 1;
5131 Inner_Scopes (Num_Inner) := Current_Scope;
5132 Pop_Scope;
5133 end loop;
5135 Pop_Scope;
5136 Remove_Context (Curr_Comp);
5137 Curr_Scope := S;
5139 else
5140 Remove_Context (Curr_Comp);
5141 end if;
5143 if Ekind (Curr_Unit) = E_Package_Body then
5144 Remove_Context (Library_Unit (Curr_Comp));
5145 end if;
5146 end if;
5148 S := Scope (S);
5149 end loop;
5151 pragma Assert (Num_Inner < Num_Scopes);
5153 Push_Scope (Standard_Standard);
5154 Scope_Stack.Table (Scope_Stack.Last).Is_Active_Stack_Base := True;
5156 -- The inlined package body is analyzed with the configuration state
5157 -- of the context prior to the scope manipulations performed above.
5159 -- ??? shouldn't this also use the warning state of the context prior
5160 -- to the scope manipulations?
5162 Instantiate_Package_Body
5163 (Body_Info =>
5164 ((Act_Decl => Act_Decl,
5165 Config_Switches => Config_Attrs,
5166 Current_Sem_Unit => Current_Sem_Unit,
5167 Expander_Status => Expander_Active,
5168 Inst_Node => N,
5169 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
5170 Scope_Suppress => Scope_Suppress,
5171 Warnings => Save_Warnings)),
5172 Inlined_Body => True);
5174 Pop_Scope;
5176 -- Restore context
5178 Set_Is_Immediately_Visible (Gen_Comp, Vis);
5180 -- Reset Generic_Instance flag so that use clauses can be installed
5181 -- in the proper order. (See Use_One_Package for effect of enclosing
5182 -- instances on processing of use clauses).
5184 for J in 1 .. N_Instances loop
5185 Set_Is_Generic_Instance (Instances (J), False);
5186 end loop;
5188 if Removed then
5189 Install_Context (Curr_Comp, Chain => False);
5191 if Present (Curr_Scope)
5192 and then Is_Child_Unit (Curr_Scope)
5193 then
5194 Push_Scope (Curr_Scope);
5195 Set_Is_Immediately_Visible (Curr_Scope);
5197 -- Finally, restore inner scopes as well
5199 for J in reverse 1 .. Num_Inner loop
5200 Push_Scope (Inner_Scopes (J));
5201 end loop;
5202 end if;
5204 Restore_Scope_Stack (List, Handle_Use => False);
5206 if Present (Curr_Scope)
5207 and then
5208 (In_Private_Part (Curr_Scope)
5209 or else In_Package_Body (Curr_Scope))
5210 then
5211 -- Install private declaration of ancestor units, which are
5212 -- currently available. Restore_Scope_Stack and Install_Context
5213 -- only install the visible part of parents.
5215 declare
5216 Par : Entity_Id;
5217 begin
5218 Par := Scope (Curr_Scope);
5219 while (Present (Par)) and then Par /= Standard_Standard loop
5220 Install_Private_Declarations (Par);
5221 Par := Scope (Par);
5222 end loop;
5223 end;
5224 end if;
5225 end if;
5227 -- Restore use clauses. For a child unit, use clauses in the parents
5228 -- are restored when installing the context, so only those in inner
5229 -- scopes (and those local to the child unit itself) need to be
5230 -- installed explicitly.
5232 if Is_Child_Unit (Curr_Unit) and then Removed then
5233 for J in reverse 1 .. Num_Inner + 1 loop
5234 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
5235 Use_Clauses (J);
5236 Install_Use_Clauses (Use_Clauses (J));
5237 end loop;
5239 else
5240 for J in reverse 1 .. Num_Scopes loop
5241 Scope_Stack.Table (Scope_Stack.Last - J + 1).First_Use_Clause :=
5242 Use_Clauses (J);
5243 Install_Use_Clauses (Use_Clauses (J));
5244 end loop;
5245 end if;
5247 -- Restore status of instances. If one of them is a body, make its
5248 -- local entities visible again.
5250 declare
5251 E : Entity_Id;
5252 Inst : Entity_Id;
5254 begin
5255 for J in 1 .. N_Instances loop
5256 Inst := Instances (J);
5257 Set_Is_Generic_Instance (Inst, True);
5259 if In_Package_Body (Inst)
5260 or else Ekind (S) in E_Procedure | E_Function
5261 then
5262 E := First_Entity (Instances (J));
5263 while Present (E) loop
5264 Set_Is_Immediately_Visible (E);
5265 Next_Entity (E);
5266 end loop;
5267 end if;
5268 end loop;
5269 end;
5271 -- If generic unit is in current unit, current context is correct. Note
5272 -- that the context is guaranteed to carry the correct SPARK_Mode as no
5273 -- enclosing scopes were removed.
5275 else
5276 Instantiate_Package_Body
5277 (Body_Info =>
5278 ((Act_Decl => Act_Decl,
5279 Config_Switches => Save_Config_Switches,
5280 Current_Sem_Unit => Current_Sem_Unit,
5281 Expander_Status => Expander_Active,
5282 Inst_Node => N,
5283 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
5284 Scope_Suppress => Scope_Suppress,
5285 Warnings => Save_Warnings)),
5286 Inlined_Body => True);
5287 end if;
5288 end Inline_Instance_Body;
5290 -------------------------------------
5291 -- Analyze_Procedure_Instantiation --
5292 -------------------------------------
5294 procedure Analyze_Procedure_Instantiation (N : Node_Id) is
5295 begin
5296 Analyze_Subprogram_Instantiation (N, E_Procedure);
5297 end Analyze_Procedure_Instantiation;
5299 -----------------------------------
5300 -- Need_Subprogram_Instance_Body --
5301 -----------------------------------
5303 function Need_Subprogram_Instance_Body
5304 (N : Node_Id;
5305 Subp : Entity_Id) return Boolean
5307 function Is_Inlined_Or_Child_Of_Inlined (E : Entity_Id) return Boolean;
5308 -- Return True if E is an inlined subprogram, an inlined renaming or a
5309 -- subprogram nested in an inlined subprogram. The inlining machinery
5310 -- totally disregards nested subprograms since it considers that they
5311 -- will always be compiled if the parent is (see Inline.Is_Nested).
5313 ------------------------------------
5314 -- Is_Inlined_Or_Child_Of_Inlined --
5315 ------------------------------------
5317 function Is_Inlined_Or_Child_Of_Inlined (E : Entity_Id) return Boolean is
5318 Scop : Entity_Id;
5320 begin
5321 if Is_Inlined (E) or else Is_Inlined (Alias (E)) then
5322 return True;
5323 end if;
5325 Scop := Scope (E);
5326 while Scop /= Standard_Standard loop
5327 if Is_Subprogram (Scop) and then Is_Inlined (Scop) then
5328 return True;
5329 end if;
5331 Scop := Scope (Scop);
5332 end loop;
5334 return False;
5335 end Is_Inlined_Or_Child_Of_Inlined;
5337 begin
5338 -- Must be in the main unit or inlined (or child of inlined)
5340 if (Is_In_Main_Unit (N) or else Is_Inlined_Or_Child_Of_Inlined (Subp))
5342 -- Must be generating code or analyzing code in GNATprove mode
5344 and then (Operating_Mode = Generate_Code
5345 or else (Operating_Mode = Check_Semantics
5346 and then GNATprove_Mode))
5348 -- The body is needed when generating code (full expansion) and in
5349 -- in GNATprove mode (special expansion) for formal verification of
5350 -- the body itself.
5352 and then (Expander_Active or GNATprove_Mode)
5354 -- No point in inlining if ABE is inevitable
5356 and then not Is_Known_Guaranteed_ABE (N)
5358 -- Or if subprogram is eliminated
5360 and then not Is_Eliminated (Subp)
5361 then
5362 Add_Pending_Instantiation (N, Unit_Declaration_Node (Subp));
5363 return True;
5365 -- Here if not inlined, or we ignore the inlining
5367 else
5368 return False;
5369 end if;
5370 end Need_Subprogram_Instance_Body;
5372 --------------------------------------
5373 -- Analyze_Subprogram_Instantiation --
5374 --------------------------------------
5376 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
5377 -- must be replaced by gotos which jump to the end of the routine in order
5378 -- to restore the Ghost and SPARK modes.
5380 procedure Analyze_Subprogram_Instantiation
5381 (N : Node_Id;
5382 K : Entity_Kind)
5384 Errs : constant Nat := Serious_Errors_Detected;
5385 Gen_Id : constant Node_Id := Name (N);
5386 Inst_Id : constant Entity_Id := Defining_Entity (N);
5387 Anon_Id : constant Entity_Id :=
5388 Make_Defining_Identifier (Sloc (Inst_Id),
5389 Chars => New_External_Name (Chars (Inst_Id), 'R'));
5390 Loc : constant Source_Ptr := Sloc (N);
5392 Act_Decl_Id : Entity_Id := Empty; -- init to avoid warning
5393 Act_Decl : Node_Id;
5394 Act_Spec : Node_Id;
5395 Act_Tree : Node_Id;
5397 Env_Installed : Boolean := False;
5398 Gen_Unit : Entity_Id;
5399 Gen_Decl : Node_Id;
5400 Pack_Id : Entity_Id;
5401 Parent_Installed : Boolean := False;
5403 Renaming_List : List_Id;
5404 -- The list of declarations that link formals and actuals of the
5405 -- instance. These are subtype declarations for formal types, and
5406 -- renaming declarations for other formals. The subprogram declaration
5407 -- for the instance is then appended to the list, and the last item on
5408 -- the list is the renaming declaration for the instance.
5410 procedure Analyze_Instance_And_Renamings;
5411 -- The instance must be analyzed in a context that includes the mappings
5412 -- of generic parameters into actuals. We create a package declaration
5413 -- for this purpose, and a subprogram with an internal name within the
5414 -- package. The subprogram instance is simply an alias for the internal
5415 -- subprogram, declared in the current scope.
5417 procedure Build_Subprogram_Renaming;
5418 -- If the subprogram is recursive, there are occurrences of the name of
5419 -- the generic within the body, which must resolve to the current
5420 -- instance. We add a renaming declaration after the declaration, which
5421 -- is available in the instance body, as well as in the analysis of
5422 -- aspects that appear in the generic. This renaming declaration is
5423 -- inserted after the instance declaration which it renames.
5425 ------------------------------------
5426 -- Analyze_Instance_And_Renamings --
5427 ------------------------------------
5429 procedure Analyze_Instance_And_Renamings is
5430 Def_Ent : constant Entity_Id := Defining_Entity (N);
5431 Pack_Decl : Node_Id;
5433 begin
5434 if Nkind (Parent (N)) = N_Compilation_Unit then
5436 -- For the case of a compilation unit, the container package has
5437 -- the same name as the instantiation, to insure that the binder
5438 -- calls the elaboration procedure with the right name. Copy the
5439 -- entity of the instance, which may have compilation level flags
5440 -- (e.g. Is_Child_Unit) set.
5442 Pack_Id := New_Copy (Def_Ent);
5444 else
5445 -- Otherwise we use the name of the instantiation concatenated
5446 -- with its source position to ensure uniqueness if there are
5447 -- several instantiations with the same name.
5449 Pack_Id :=
5450 Make_Defining_Identifier (Loc,
5451 Chars => New_External_Name
5452 (Related_Id => Chars (Def_Ent),
5453 Suffix => "GP",
5454 Suffix_Index => Source_Offset (Sloc (Def_Ent))));
5455 end if;
5457 Pack_Decl :=
5458 Make_Package_Declaration (Loc,
5459 Specification => Make_Package_Specification (Loc,
5460 Defining_Unit_Name => Pack_Id,
5461 Visible_Declarations => Renaming_List,
5462 End_Label => Empty));
5464 Set_Instance_Spec (N, Pack_Decl);
5465 Set_Is_Generic_Instance (Pack_Id);
5466 Set_Debug_Info_Needed (Pack_Id);
5468 -- Case of not a compilation unit
5470 if Nkind (Parent (N)) /= N_Compilation_Unit then
5471 Mark_Rewrite_Insertion (Pack_Decl);
5472 Insert_Before (N, Pack_Decl);
5473 Set_Has_Completion (Pack_Id);
5475 -- Case of an instantiation that is a compilation unit
5477 -- Place declaration on current node so context is complete for
5478 -- analysis (including nested instantiations), and for use in a
5479 -- context_clause (see Analyze_With_Clause).
5481 else
5482 Set_Unit (Parent (N), Pack_Decl);
5483 Set_Parent_Spec (Pack_Decl, Parent_Spec (N));
5484 end if;
5486 Analyze (Pack_Decl);
5487 Check_Formal_Packages (Pack_Id);
5489 -- Body of the enclosing package is supplied when instantiating the
5490 -- subprogram body, after semantic analysis is completed.
5492 if Nkind (Parent (N)) = N_Compilation_Unit then
5494 -- Remove package itself from visibility, so it does not
5495 -- conflict with subprogram.
5497 Set_Name_Entity_Id (Chars (Pack_Id), Homonym (Pack_Id));
5499 -- Set name and scope of internal subprogram so that the proper
5500 -- external name will be generated. The proper scope is the scope
5501 -- of the wrapper package. We need to generate debugging info for
5502 -- the internal subprogram, so set flag accordingly.
5504 Set_Chars (Anon_Id, Chars (Defining_Entity (N)));
5505 Set_Scope (Anon_Id, Scope (Pack_Id));
5507 -- Mark wrapper package as referenced, to avoid spurious warnings
5508 -- if the instantiation appears in various with_ clauses of
5509 -- subunits of the main unit.
5511 Set_Referenced (Pack_Id);
5512 end if;
5514 Set_Is_Generic_Instance (Anon_Id);
5515 Set_Debug_Info_Needed (Anon_Id);
5516 Act_Decl_Id := New_Copy (Anon_Id);
5518 Set_Parent (Act_Decl_Id, Parent (Anon_Id));
5519 Set_Chars (Act_Decl_Id, Chars (Defining_Entity (N)));
5520 Set_Sloc (Act_Decl_Id, Sloc (Defining_Entity (N)));
5522 -- Subprogram instance comes from source only if generic does
5524 Preserve_Comes_From_Source (Act_Decl_Id, Gen_Unit);
5526 -- If the instance is a child unit, mark the Id accordingly. Mark
5527 -- the anonymous entity as well, which is the real subprogram and
5528 -- which is used when the instance appears in a context clause.
5529 -- Similarly, propagate the Is_Eliminated flag to handle properly
5530 -- nested eliminated subprograms.
5532 Set_Is_Child_Unit (Act_Decl_Id, Is_Child_Unit (Defining_Entity (N)));
5533 Set_Is_Child_Unit (Anon_Id, Is_Child_Unit (Defining_Entity (N)));
5534 New_Overloaded_Entity (Act_Decl_Id);
5535 Check_Eliminated (Act_Decl_Id);
5536 Set_Is_Eliminated (Anon_Id, Is_Eliminated (Act_Decl_Id));
5538 if Nkind (Parent (N)) = N_Compilation_Unit then
5540 -- In compilation unit case, kill elaboration checks on the
5541 -- instantiation, since they are never needed - the body is
5542 -- instantiated at the same point as the spec.
5544 if Legacy_Elaboration_Checks then
5545 Set_Kill_Elaboration_Checks (Act_Decl_Id);
5546 Set_Suppress_Elaboration_Warnings (Act_Decl_Id);
5547 end if;
5549 Set_Is_Compilation_Unit (Anon_Id);
5550 Set_Cunit_Entity (Current_Sem_Unit, Pack_Id);
5551 end if;
5553 -- The instance is not a freezing point for the new subprogram.
5554 -- The anonymous subprogram may have a freeze node, created for
5555 -- some delayed aspects. This freeze node must not be inherited
5556 -- by the visible subprogram entity.
5558 Set_Is_Frozen (Act_Decl_Id, False);
5559 Set_Freeze_Node (Act_Decl_Id, Empty);
5561 if Nkind (Defining_Entity (N)) = N_Defining_Operator_Symbol then
5562 Valid_Operator_Definition (Act_Decl_Id);
5563 end if;
5565 Set_Alias (Act_Decl_Id, Anon_Id);
5566 Set_Has_Completion (Act_Decl_Id);
5567 Set_Related_Instance (Pack_Id, Act_Decl_Id);
5569 if Nkind (Parent (N)) = N_Compilation_Unit then
5570 Set_Body_Required (Parent (N), False);
5571 end if;
5572 end Analyze_Instance_And_Renamings;
5574 -------------------------------
5575 -- Build_Subprogram_Renaming --
5576 -------------------------------
5578 procedure Build_Subprogram_Renaming is
5579 Renaming_Decl : Node_Id;
5580 Unit_Renaming : Node_Id;
5582 begin
5583 Unit_Renaming :=
5584 Make_Subprogram_Renaming_Declaration (Loc,
5585 Specification =>
5586 Copy_Generic_Node
5587 (Specification (Original_Node (Gen_Decl)),
5588 Empty,
5589 Instantiating => True),
5590 Name => New_Occurrence_Of (Anon_Id, Loc));
5592 -- The generic may be a child unit. The renaming needs an identifier
5593 -- with the proper name.
5595 Set_Defining_Unit_Name (Specification (Unit_Renaming),
5596 Make_Defining_Identifier (Loc, Chars (Gen_Unit)));
5598 -- If there is a formal subprogram with the same name as the unit
5599 -- itself, do not add this renaming declaration, to prevent
5600 -- ambiguities when there is a call with that name in the body.
5602 Renaming_Decl := First (Renaming_List);
5603 while Present (Renaming_Decl) loop
5604 if Nkind (Renaming_Decl) = N_Subprogram_Renaming_Declaration
5605 and then
5606 Chars (Defining_Entity (Renaming_Decl)) = Chars (Gen_Unit)
5607 then
5608 exit;
5609 end if;
5611 Next (Renaming_Decl);
5612 end loop;
5614 if No (Renaming_Decl) then
5615 Append (Unit_Renaming, Renaming_List);
5616 end if;
5617 end Build_Subprogram_Renaming;
5619 -- Local variables
5621 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
5622 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
5623 Saved_ISMP : constant Boolean :=
5624 Ignore_SPARK_Mode_Pragmas_In_Instance;
5625 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
5626 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
5627 -- Save the Ghost and SPARK mode-related data to restore on exit
5629 Vis_Prims_List : Elist_Id := No_Elist;
5630 -- List of primitives made temporarily visible in the instantiation
5631 -- to match the visibility of the formal type
5633 -- Start of processing for Analyze_Subprogram_Instantiation
5635 begin
5636 -- Preserve relevant elaboration-related attributes of the context which
5637 -- are no longer available or very expensive to recompute once analysis,
5638 -- resolution, and expansion are over.
5640 Mark_Elaboration_Attributes
5641 (N_Id => N,
5642 Checks => True,
5643 Level => True,
5644 Modes => True,
5645 Warnings => True);
5647 -- Very first thing: check for special Text_IO unit in case we are
5648 -- instantiating one of the children of [[Wide_]Wide_]Text_IO. Of course
5649 -- such an instantiation is bogus (these are packages, not subprograms),
5650 -- but we get a better error message if we do this.
5652 Check_Text_IO_Special_Unit (Gen_Id);
5654 -- Make node global for error reporting
5656 Instantiation_Node := N;
5658 -- For package instantiations we turn off style checks, because they
5659 -- will have been emitted in the generic. For subprogram instantiations
5660 -- we want to apply at least the check on overriding indicators so we
5661 -- do not modify the style check status.
5663 -- The renaming declarations for the actuals do not come from source and
5664 -- will not generate spurious warnings.
5666 Preanalyze_Actuals (N);
5668 Init_Env;
5669 Env_Installed := True;
5670 Check_Generic_Child_Unit (Gen_Id, Parent_Installed);
5671 Gen_Unit := Entity (Gen_Id);
5673 -- A subprogram instantiation is Ghost when it is subject to pragma
5674 -- Ghost or the generic template is Ghost. Set the mode now to ensure
5675 -- that any nodes generated during analysis and expansion are marked as
5676 -- Ghost.
5678 Mark_And_Set_Ghost_Instantiation (N, Gen_Unit);
5680 Generate_Reference (Gen_Unit, Gen_Id);
5682 if Nkind (Gen_Id) = N_Identifier
5683 and then Chars (Gen_Unit) = Chars (Defining_Entity (N))
5684 then
5685 Error_Msg_NE
5686 ("& is hidden within declaration of instance", Gen_Id, Gen_Unit);
5687 end if;
5689 if Etype (Gen_Unit) = Any_Type then
5690 Restore_Env;
5691 goto Leave;
5692 end if;
5694 -- Verify that it is a generic subprogram of the right kind, and that
5695 -- it does not lead to a circular instantiation.
5697 if K = E_Procedure and then Ekind (Gen_Unit) /= E_Generic_Procedure then
5698 Error_Msg_NE
5699 ("& is not the name of a generic procedure", Gen_Id, Gen_Unit);
5701 elsif K = E_Function and then Ekind (Gen_Unit) /= E_Generic_Function then
5702 Error_Msg_NE
5703 ("& is not the name of a generic function", Gen_Id, Gen_Unit);
5705 elsif In_Open_Scopes (Gen_Unit) then
5706 Error_Msg_NE ("instantiation of & within itself", N, Gen_Unit);
5708 else
5709 Mutate_Ekind (Inst_Id, K);
5710 Set_Scope (Inst_Id, Current_Scope);
5712 Set_Entity (Gen_Id, Gen_Unit);
5713 Set_Is_Instantiated (Gen_Unit);
5715 if In_Extended_Main_Source_Unit (N) then
5716 Generate_Reference (Gen_Unit, N);
5717 end if;
5719 -- If renaming, get original unit
5721 if Present (Renamed_Entity (Gen_Unit))
5722 and then Is_Generic_Subprogram (Renamed_Entity (Gen_Unit))
5723 then
5724 Gen_Unit := Renamed_Entity (Gen_Unit);
5725 Set_Is_Instantiated (Gen_Unit);
5726 Generate_Reference (Gen_Unit, N);
5727 end if;
5729 if Contains_Instance_Of (Gen_Unit, Current_Scope, Gen_Id) then
5730 Error_Msg_Node_2 := Current_Scope;
5731 Error_Msg_NE
5732 ("circular instantiation: & instantiated in &!", N, Gen_Unit);
5733 Circularity_Detected := True;
5734 Restore_Hidden_Primitives (Vis_Prims_List);
5735 goto Leave;
5736 end if;
5738 Gen_Decl := Unit_Declaration_Node (Gen_Unit);
5740 -- Initialize renamings map, for error checking
5742 Generic_Renamings.Set_Last (0);
5743 Generic_Renamings_HTable.Reset;
5745 Create_Instantiation_Source (N, Gen_Unit, S_Adjustment);
5747 -- Copy original generic tree, to produce text for instantiation
5749 Act_Tree :=
5750 Copy_Generic_Node
5751 (Original_Node (Gen_Decl), Empty, Instantiating => True);
5753 -- Inherit overriding indicator from instance node
5755 Act_Spec := Specification (Act_Tree);
5756 Set_Must_Override (Act_Spec, Must_Override (N));
5757 Set_Must_Not_Override (Act_Spec, Must_Not_Override (N));
5759 Renaming_List :=
5760 Analyze_Associations
5761 (I_Node => N,
5762 Formals => Generic_Formal_Declarations (Act_Tree),
5763 F_Copy => Generic_Formal_Declarations (Gen_Decl));
5765 Vis_Prims_List := Check_Hidden_Primitives (Renaming_List);
5767 -- The subprogram itself cannot contain a nested instance, so the
5768 -- current parent is left empty.
5770 Set_Instance_Env (Gen_Unit, Empty);
5772 -- Build the subprogram declaration, which does not appear in the
5773 -- generic template, and give it a sloc consistent with that of the
5774 -- template.
5776 Set_Defining_Unit_Name (Act_Spec, Anon_Id);
5777 Set_Generic_Parent (Act_Spec, Gen_Unit);
5778 Act_Decl :=
5779 Make_Subprogram_Declaration (Sloc (Act_Spec),
5780 Specification => Act_Spec);
5782 -- The aspects have been copied previously, but they have to be
5783 -- linked explicitly to the new subprogram declaration. Explicit
5784 -- pre/postconditions on the instance are analyzed below, in a
5785 -- separate step.
5787 Move_Aspects (Act_Tree, To => Act_Decl);
5788 Set_Categorization_From_Pragmas (Act_Decl);
5790 if Parent_Installed then
5791 Hide_Current_Scope;
5792 end if;
5794 Append (Act_Decl, Renaming_List);
5796 -- Contract-related source pragmas that follow a generic subprogram
5797 -- must be instantiated explicitly because they are not part of the
5798 -- subprogram template.
5800 Instantiate_Subprogram_Contract
5801 (Original_Node (Gen_Decl), Renaming_List);
5803 Build_Subprogram_Renaming;
5805 -- If the context of the instance is subject to SPARK_Mode "off" or
5806 -- the annotation is altogether missing, set the global flag which
5807 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within
5808 -- the instance. This should be done prior to analyzing the instance.
5810 if SPARK_Mode /= On then
5811 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
5812 end if;
5814 -- If the context of an instance is not subject to SPARK_Mode "off",
5815 -- and the generic spec is subject to an explicit SPARK_Mode pragma,
5816 -- the latter should be the one applicable to the instance.
5818 if not Ignore_SPARK_Mode_Pragmas_In_Instance
5819 and then Saved_SM /= Off
5820 and then Present (SPARK_Pragma (Gen_Unit))
5821 then
5822 Set_SPARK_Mode (Gen_Unit);
5823 end if;
5825 -- Need to mark Anon_Id intrinsic before calling
5826 -- Analyze_Instance_And_Renamings because this flag may be propagated
5827 -- to other nodes.
5829 if Is_Intrinsic_Subprogram (Gen_Unit) then
5830 Set_Is_Intrinsic_Subprogram (Anon_Id);
5831 Set_Interface_Name (Anon_Id, Interface_Name (Gen_Unit));
5832 end if;
5834 Analyze_Instance_And_Renamings;
5836 -- Restore SPARK_Mode from the context after analysis of the package
5837 -- declaration, so that the SPARK_Mode on the generic spec does not
5838 -- apply to the pending instance for the instance body.
5840 if not Ignore_SPARK_Mode_Pragmas_In_Instance
5841 and then Saved_SM /= Off
5842 and then Present (SPARK_Pragma (Gen_Unit))
5843 then
5844 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
5845 end if;
5847 -- If the generic is marked Import (Intrinsic), then so is the
5848 -- instance; this indicates that there is no body to instantiate.
5849 -- We also copy the interface name in case this is handled by the
5850 -- back-end and deal with an instance of unchecked conversion.
5852 if Is_Intrinsic_Subprogram (Gen_Unit) then
5853 Set_Is_Intrinsic_Subprogram (Act_Decl_Id);
5854 Set_Interface_Name (Act_Decl_Id, Interface_Name (Gen_Unit));
5856 if Chars (Gen_Unit) = Name_Unchecked_Conversion then
5857 Validate_Unchecked_Conversion (N, Act_Decl_Id);
5858 end if;
5859 end if;
5861 -- Inherit convention from generic unit. Intrinsic convention, as for
5862 -- an instance of unchecked conversion, is not inherited because an
5863 -- explicit Ada instance has been created.
5865 if Has_Convention_Pragma (Gen_Unit)
5866 and then Convention (Gen_Unit) /= Convention_Intrinsic
5867 then
5868 Set_Convention (Act_Decl_Id, Convention (Gen_Unit));
5869 Set_Is_Exported (Act_Decl_Id, Is_Exported (Gen_Unit));
5870 end if;
5872 Generate_Definition (Act_Decl_Id);
5874 -- Inherit all inlining-related flags which apply to the generic in
5875 -- the subprogram and its declaration.
5877 Set_Is_Inlined (Act_Decl_Id, Is_Inlined (Gen_Unit));
5878 Set_Is_Inlined (Anon_Id, Is_Inlined (Gen_Unit));
5880 Set_Has_Pragma_Inline (Act_Decl_Id, Has_Pragma_Inline (Gen_Unit));
5881 Set_Has_Pragma_Inline (Anon_Id, Has_Pragma_Inline (Gen_Unit));
5883 Set_Has_Pragma_Inline_Always
5884 (Act_Decl_Id, Has_Pragma_Inline_Always (Gen_Unit));
5885 Set_Has_Pragma_Inline_Always
5886 (Anon_Id, Has_Pragma_Inline_Always (Gen_Unit));
5888 Set_Has_Pragma_No_Inline
5889 (Act_Decl_Id, Has_Pragma_No_Inline (Gen_Unit));
5890 Set_Has_Pragma_No_Inline
5891 (Anon_Id, Has_Pragma_No_Inline (Gen_Unit));
5893 -- Propagate No_Return if pragma applied to generic unit. This must
5894 -- be done explicitly because pragma does not appear in generic
5895 -- declaration (unlike the aspect case).
5897 if No_Return (Gen_Unit) then
5898 Set_No_Return (Act_Decl_Id);
5899 Set_No_Return (Anon_Id);
5900 end if;
5902 -- Mark both the instance spec and the anonymous package in case the
5903 -- body is instantiated at a later pass. This preserves the original
5904 -- context in effect for the body.
5906 if SPARK_Mode /= On then
5907 Set_Ignore_SPARK_Mode_Pragmas (Act_Decl_Id);
5908 Set_Ignore_SPARK_Mode_Pragmas (Anon_Id);
5909 end if;
5911 if Legacy_Elaboration_Checks
5912 and then not Is_Intrinsic_Subprogram (Gen_Unit)
5913 then
5914 Check_Elab_Instantiation (N);
5915 end if;
5917 -- Save the scenario for later examination by the ABE Processing
5918 -- phase.
5920 Record_Elaboration_Scenario (N);
5922 -- The instantiation results in a guaranteed ABE. Create a completing
5923 -- body for the subprogram declaration because the real body will not
5924 -- be instantiated.
5926 if Is_Known_Guaranteed_ABE (N) then
5927 Provide_Completing_Bodies (Instance_Spec (N));
5928 end if;
5930 if Is_Dispatching_Operation (Act_Decl_Id)
5931 and then Ada_Version >= Ada_2005
5932 then
5933 declare
5934 Formal : Entity_Id;
5936 begin
5937 Formal := First_Formal (Act_Decl_Id);
5938 while Present (Formal) loop
5939 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type
5940 and then Is_Controlling_Formal (Formal)
5941 and then not Can_Never_Be_Null (Formal)
5942 then
5943 Error_Msg_NE
5944 ("access parameter& is controlling,", N, Formal);
5945 Error_Msg_NE
5946 ("\corresponding parameter of & must be explicitly "
5947 & "null-excluding", N, Gen_Id);
5948 end if;
5950 Next_Formal (Formal);
5951 end loop;
5952 end;
5953 end if;
5955 Check_Hidden_Child_Unit (N, Gen_Unit, Act_Decl_Id);
5957 Validate_Categorization_Dependency (N, Act_Decl_Id);
5959 if not Is_Intrinsic_Subprogram (Act_Decl_Id) then
5960 Inherit_Context (Gen_Decl, N);
5962 Restore_Private_Views (Pack_Id, False);
5964 -- If the context requires a full instantiation, mark node for
5965 -- subsequent construction of the body.
5967 if Need_Subprogram_Instance_Body (N, Act_Decl_Id) then
5968 Check_Forward_Instantiation (Gen_Decl);
5970 -- The wrapper package is always delayed, because it does not
5971 -- constitute a freeze point, but to insure that the freeze node
5972 -- is placed properly, it is created directly when instantiating
5973 -- the body (otherwise the freeze node might appear to early for
5974 -- nested instantiations).
5976 elsif Nkind (Parent (N)) = N_Compilation_Unit then
5977 Rewrite (N, Unit (Parent (N)));
5978 Set_Unit (Parent (N), N);
5979 end if;
5981 -- Replace instance node for library-level instantiations of
5982 -- intrinsic subprograms.
5984 elsif Nkind (Parent (N)) = N_Compilation_Unit then
5985 Rewrite (N, Unit (Parent (N)));
5986 Set_Unit (Parent (N), N);
5987 end if;
5989 if Parent_Installed then
5990 Remove_Parent;
5991 end if;
5993 Restore_Hidden_Primitives (Vis_Prims_List);
5994 Restore_Env;
5995 Env_Installed := False;
5996 Generic_Renamings.Set_Last (0);
5997 Generic_Renamings_HTable.Reset;
5998 end if;
6000 <<Leave>>
6001 -- Analyze aspects in declaration if no errors appear in the instance.
6003 if Has_Aspects (N) and then Serious_Errors_Detected = Errs then
6004 Analyze_Aspect_Specifications (N, Act_Decl_Id);
6005 end if;
6007 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
6008 Restore_Ghost_Region (Saved_GM, Saved_IGR);
6009 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
6011 exception
6012 when Instantiation_Error =>
6013 if Parent_Installed then
6014 Remove_Parent;
6015 end if;
6017 if Env_Installed then
6018 Restore_Env;
6019 end if;
6021 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
6022 Restore_Ghost_Region (Saved_GM, Saved_IGR);
6023 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
6024 end Analyze_Subprogram_Instantiation;
6026 -------------------------
6027 -- Get_Associated_Node --
6028 -------------------------
6030 function Get_Associated_Node (N : Node_Id) return Node_Id is
6031 Assoc : Node_Id;
6033 begin
6034 Assoc := Associated_Node (N);
6036 if Nkind (Assoc) /= Nkind (N) then
6037 return Assoc;
6039 elsif Nkind (Assoc) in N_Aggregate | N_Extension_Aggregate then
6040 return Assoc;
6042 else
6043 -- If the node is part of an inner generic, it may itself have been
6044 -- remapped into a further generic copy. Associated_Node is otherwise
6045 -- used for the entity of the node, and will be of a different node
6046 -- kind, or else N has been rewritten as a literal or function call.
6048 while Present (Associated_Node (Assoc))
6049 and then Nkind (Associated_Node (Assoc)) = Nkind (Assoc)
6050 loop
6051 Assoc := Associated_Node (Assoc);
6052 end loop;
6054 -- Follow an additional link in case the final node was rewritten.
6055 -- This can only happen with nested generic units.
6057 if (Nkind (Assoc) = N_Identifier or else Nkind (Assoc) in N_Op)
6058 and then Present (Associated_Node (Assoc))
6059 and then Nkind (Associated_Node (Assoc)) in N_Function_Call
6060 | N_Explicit_Dereference
6061 | N_Integer_Literal
6062 | N_Real_Literal
6063 | N_String_Literal
6064 then
6065 Assoc := Associated_Node (Assoc);
6066 end if;
6068 -- An additional special case: an unconstrained type in an object
6069 -- declaration may have been rewritten as a local subtype constrained
6070 -- by the expression in the declaration. We need to recover the
6071 -- original entity, which may be global.
6073 if Present (Original_Node (Assoc))
6074 and then Nkind (Parent (N)) = N_Object_Declaration
6075 then
6076 Assoc := Original_Node (Assoc);
6077 end if;
6079 return Assoc;
6080 end if;
6081 end Get_Associated_Node;
6083 ----------------------------
6084 -- Build_Function_Wrapper --
6085 ----------------------------
6087 function Build_Function_Wrapper
6088 (Formal_Subp : Entity_Id;
6089 Actual_Subp : Entity_Id) return Node_Id
6091 Loc : constant Source_Ptr := Sloc (Current_Scope);
6092 Ret_Type : constant Entity_Id := Get_Instance_Of (Etype (Formal_Subp));
6093 Actuals : List_Id;
6094 Decl : Node_Id;
6095 Func_Name : Node_Id;
6096 Func : Entity_Id;
6097 Parm_Type : Node_Id;
6098 Profile : List_Id := New_List;
6099 Spec : Node_Id;
6100 Act_F : Entity_Id;
6101 Form_F : Entity_Id;
6102 New_F : Entity_Id;
6104 begin
6105 Func_Name := New_Occurrence_Of (Actual_Subp, Loc);
6107 Func := Make_Defining_Identifier (Loc, Chars (Formal_Subp));
6108 Mutate_Ekind (Func, E_Function);
6109 Set_Is_Generic_Actual_Subprogram (Func);
6111 Actuals := New_List;
6112 Profile := New_List;
6114 Act_F := First_Formal (Actual_Subp);
6115 Form_F := First_Formal (Formal_Subp);
6116 while Present (Form_F) loop
6118 -- Create new formal for profile of wrapper, and add a reference
6119 -- to it in the list of actuals for the enclosing call. The name
6120 -- must be that of the formal in the formal subprogram, because
6121 -- calls to it in the generic body may use named associations.
6123 New_F := Make_Defining_Identifier (Loc, Chars (Form_F));
6125 Parm_Type :=
6126 New_Occurrence_Of (Get_Instance_Of (Etype (Form_F)), Loc);
6128 Append_To (Profile,
6129 Make_Parameter_Specification (Loc,
6130 Defining_Identifier => New_F,
6131 Parameter_Type => Parm_Type));
6133 Append_To (Actuals, New_Occurrence_Of (New_F, Loc));
6134 Next_Formal (Form_F);
6136 if Present (Act_F) then
6137 Next_Formal (Act_F);
6138 end if;
6139 end loop;
6141 Spec :=
6142 Make_Function_Specification (Loc,
6143 Defining_Unit_Name => Func,
6144 Parameter_Specifications => Profile,
6145 Result_Definition => New_Occurrence_Of (Ret_Type, Loc));
6147 Decl :=
6148 Make_Expression_Function (Loc,
6149 Specification => Spec,
6150 Expression =>
6151 Make_Function_Call (Loc,
6152 Name => Func_Name,
6153 Parameter_Associations => Actuals));
6155 return Decl;
6156 end Build_Function_Wrapper;
6158 ----------------------------
6159 -- Build_Operator_Wrapper --
6160 ----------------------------
6162 function Build_Operator_Wrapper
6163 (Formal_Subp : Entity_Id;
6164 Actual_Subp : Entity_Id) return Node_Id
6166 Loc : constant Source_Ptr := Sloc (Current_Scope);
6167 Ret_Type : constant Entity_Id :=
6168 Get_Instance_Of (Etype (Formal_Subp));
6169 Op_Type : constant Entity_Id :=
6170 Get_Instance_Of (Etype (First_Formal (Formal_Subp)));
6171 Is_Binary : constant Boolean :=
6172 Present (Next_Formal (First_Formal (Formal_Subp)));
6174 Decl : Node_Id;
6175 Expr : Node_Id := Empty;
6176 F1, F2 : Entity_Id;
6177 Func : Entity_Id;
6178 Op_Name : Name_Id;
6179 Spec : Node_Id;
6180 L, R : Node_Id;
6182 begin
6183 Op_Name := Chars (Actual_Subp);
6185 -- Create entities for wrapper function and its formals
6187 F1 := Make_Temporary (Loc, 'A');
6188 F2 := Make_Temporary (Loc, 'B');
6189 L := New_Occurrence_Of (F1, Loc);
6190 R := New_Occurrence_Of (F2, Loc);
6192 Func := Make_Defining_Identifier (Loc, Chars (Formal_Subp));
6193 Mutate_Ekind (Func, E_Function);
6194 Set_Is_Generic_Actual_Subprogram (Func);
6196 Spec :=
6197 Make_Function_Specification (Loc,
6198 Defining_Unit_Name => Func,
6199 Parameter_Specifications => New_List (
6200 Make_Parameter_Specification (Loc,
6201 Defining_Identifier => F1,
6202 Parameter_Type => New_Occurrence_Of (Op_Type, Loc))),
6203 Result_Definition => New_Occurrence_Of (Ret_Type, Loc));
6205 if Is_Binary then
6206 Append_To (Parameter_Specifications (Spec),
6207 Make_Parameter_Specification (Loc,
6208 Defining_Identifier => F2,
6209 Parameter_Type => New_Occurrence_Of (Op_Type, Loc)));
6210 end if;
6212 -- Build expression as a function call, or as an operator node
6213 -- that corresponds to the name of the actual, starting with
6214 -- binary operators.
6216 if Op_Name not in Any_Operator_Name then
6217 Expr :=
6218 Make_Function_Call (Loc,
6219 Name =>
6220 New_Occurrence_Of (Actual_Subp, Loc),
6221 Parameter_Associations => New_List (L));
6223 if Is_Binary then
6224 Append_To (Parameter_Associations (Expr), R);
6225 end if;
6227 -- Binary operators
6229 elsif Is_Binary then
6230 if Op_Name = Name_Op_And then
6231 Expr := Make_Op_And (Loc, Left_Opnd => L, Right_Opnd => R);
6232 elsif Op_Name = Name_Op_Or then
6233 Expr := Make_Op_Or (Loc, Left_Opnd => L, Right_Opnd => R);
6234 elsif Op_Name = Name_Op_Xor then
6235 Expr := Make_Op_Xor (Loc, Left_Opnd => L, Right_Opnd => R);
6236 elsif Op_Name = Name_Op_Eq then
6237 Expr := Make_Op_Eq (Loc, Left_Opnd => L, Right_Opnd => R);
6238 elsif Op_Name = Name_Op_Ne then
6239 Expr := Make_Op_Ne (Loc, Left_Opnd => L, Right_Opnd => R);
6240 elsif Op_Name = Name_Op_Le then
6241 Expr := Make_Op_Le (Loc, Left_Opnd => L, Right_Opnd => R);
6242 elsif Op_Name = Name_Op_Gt then
6243 Expr := Make_Op_Gt (Loc, Left_Opnd => L, Right_Opnd => R);
6244 elsif Op_Name = Name_Op_Ge then
6245 Expr := Make_Op_Ge (Loc, Left_Opnd => L, Right_Opnd => R);
6246 elsif Op_Name = Name_Op_Lt then
6247 Expr := Make_Op_Lt (Loc, Left_Opnd => L, Right_Opnd => R);
6248 elsif Op_Name = Name_Op_Add then
6249 Expr := Make_Op_Add (Loc, Left_Opnd => L, Right_Opnd => R);
6250 elsif Op_Name = Name_Op_Subtract then
6251 Expr := Make_Op_Subtract (Loc, Left_Opnd => L, Right_Opnd => R);
6252 elsif Op_Name = Name_Op_Concat then
6253 Expr := Make_Op_Concat (Loc, Left_Opnd => L, Right_Opnd => R);
6254 elsif Op_Name = Name_Op_Multiply then
6255 Expr := Make_Op_Multiply (Loc, Left_Opnd => L, Right_Opnd => R);
6256 elsif Op_Name = Name_Op_Divide then
6257 Expr := Make_Op_Divide (Loc, Left_Opnd => L, Right_Opnd => R);
6258 elsif Op_Name = Name_Op_Mod then
6259 Expr := Make_Op_Mod (Loc, Left_Opnd => L, Right_Opnd => R);
6260 elsif Op_Name = Name_Op_Rem then
6261 Expr := Make_Op_Rem (Loc, Left_Opnd => L, Right_Opnd => R);
6262 elsif Op_Name = Name_Op_Expon then
6263 Expr := Make_Op_Expon (Loc, Left_Opnd => L, Right_Opnd => R);
6264 end if;
6266 -- Unary operators
6268 else
6269 if Op_Name = Name_Op_Add then
6270 Expr := Make_Op_Plus (Loc, Right_Opnd => L);
6271 elsif Op_Name = Name_Op_Subtract then
6272 Expr := Make_Op_Minus (Loc, Right_Opnd => L);
6273 elsif Op_Name = Name_Op_Abs then
6274 Expr := Make_Op_Abs (Loc, Right_Opnd => L);
6275 elsif Op_Name = Name_Op_Not then
6276 Expr := Make_Op_Not (Loc, Right_Opnd => L);
6277 end if;
6278 end if;
6280 Decl :=
6281 Make_Expression_Function (Loc,
6282 Specification => Spec,
6283 Expression => Expr);
6285 return Decl;
6286 end Build_Operator_Wrapper;
6288 -----------------------------------
6289 -- Build_Subprogram_Decl_Wrapper --
6290 -----------------------------------
6292 function Build_Subprogram_Decl_Wrapper
6293 (Formal_Subp : Entity_Id) return Node_Id
6295 Loc : constant Source_Ptr := Sloc (Current_Scope);
6296 Ret_Type : constant Entity_Id := Get_Instance_Of (Etype (Formal_Subp));
6297 Decl : Node_Id;
6298 Subp : Entity_Id;
6299 Parm_Spec : Node_Id;
6300 Profile : List_Id := New_List;
6301 Spec : Node_Id;
6302 Form_F : Entity_Id;
6303 New_F : Entity_Id;
6305 begin
6307 Subp := Make_Defining_Identifier (Loc, Chars (Formal_Subp));
6308 Mutate_Ekind (Subp, Ekind (Formal_Subp));
6309 Set_Is_Generic_Actual_Subprogram (Subp);
6311 Profile := Parameter_Specifications (
6312 New_Copy_Tree
6313 (Specification (Unit_Declaration_Node (Formal_Subp))));
6315 Form_F := First_Formal (Formal_Subp);
6316 Parm_Spec := First (Profile);
6318 -- Create new entities for the formals. Reset entities so that
6319 -- parameter types are properly resolved when wrapper declaration
6320 -- is analyzed.
6322 while Present (Parm_Spec) loop
6323 New_F := Make_Defining_Identifier (Loc, Chars (Form_F));
6324 Set_Defining_Identifier (Parm_Spec, New_F);
6325 Set_Entity (Parameter_Type (Parm_Spec), Empty);
6326 Next (Parm_Spec);
6327 Next_Formal (Form_F);
6328 end loop;
6330 if Ret_Type = Standard_Void_Type then
6331 Spec :=
6332 Make_Procedure_Specification (Loc,
6333 Defining_Unit_Name => Subp,
6334 Parameter_Specifications => Profile);
6335 else
6336 Spec :=
6337 Make_Function_Specification (Loc,
6338 Defining_Unit_Name => Subp,
6339 Parameter_Specifications => Profile,
6340 Result_Definition => New_Occurrence_Of (Ret_Type, Loc));
6341 end if;
6343 Decl :=
6344 Make_Subprogram_Declaration (Loc, Specification => Spec);
6346 return Decl;
6347 end Build_Subprogram_Decl_Wrapper;
6349 -----------------------------------
6350 -- Build_Subprogram_Body_Wrapper --
6351 -----------------------------------
6353 function Build_Subprogram_Body_Wrapper
6354 (Formal_Subp : Entity_Id;
6355 Actual_Name : Node_Id) return Node_Id
6357 Loc : constant Source_Ptr := Sloc (Current_Scope);
6358 Ret_Type : constant Entity_Id := Get_Instance_Of (Etype (Formal_Subp));
6359 Spec_Node : constant Node_Id :=
6360 Specification
6361 (Build_Subprogram_Decl_Wrapper (Formal_Subp));
6362 Act : Node_Id;
6363 Actuals : List_Id;
6364 Body_Node : Node_Id;
6365 Stmt : Node_Id;
6366 begin
6367 Actuals := New_List;
6368 Act := First (Parameter_Specifications (Spec_Node));
6370 while Present (Act) loop
6371 Append_To (Actuals,
6372 Make_Identifier (Loc, Chars (Defining_Identifier (Act))));
6373 Next (Act);
6374 end loop;
6376 if Ret_Type = Standard_Void_Type then
6377 Stmt := Make_Procedure_Call_Statement (Loc,
6378 Name => Actual_Name,
6379 Parameter_Associations => Actuals);
6381 else
6382 Stmt := Make_Simple_Return_Statement (Loc,
6383 Expression =>
6384 Make_Function_Call (Loc,
6385 Name => Actual_Name,
6386 Parameter_Associations => Actuals));
6387 end if;
6389 Body_Node := Make_Subprogram_Body (Loc,
6390 Specification => Spec_Node,
6391 Declarations => New_List,
6392 Handled_Statement_Sequence =>
6393 Make_Handled_Sequence_Of_Statements (Loc,
6394 Statements => New_List (Stmt)));
6396 return Body_Node;
6397 end Build_Subprogram_Body_Wrapper;
6399 -------------------------------------------
6400 -- Build_Instance_Compilation_Unit_Nodes --
6401 -------------------------------------------
6403 procedure Build_Instance_Compilation_Unit_Nodes
6404 (N : Node_Id;
6405 Act_Body : Node_Id;
6406 Act_Decl : Node_Id)
6408 Decl_Cunit : Node_Id;
6409 Body_Cunit : Node_Id;
6410 Citem : Node_Id;
6411 New_Main : constant Entity_Id := Defining_Entity (Act_Decl);
6412 Old_Main : constant Entity_Id := Cunit_Entity (Main_Unit);
6414 begin
6415 -- A new compilation unit node is built for the instance declaration
6417 Decl_Cunit :=
6418 Make_Compilation_Unit (Sloc (N),
6419 Context_Items => Empty_List,
6420 Unit => Act_Decl,
6421 Aux_Decls_Node => Make_Compilation_Unit_Aux (Sloc (N)));
6423 Set_Parent_Spec (Act_Decl, Parent_Spec (N));
6425 -- The new compilation unit is linked to its body, but both share the
6426 -- same file, so we do not set Body_Required on the new unit so as not
6427 -- to create a spurious dependency on a non-existent body in the ali.
6428 -- This simplifies CodePeer unit traversal.
6430 -- We use the original instantiation compilation unit as the resulting
6431 -- compilation unit of the instance, since this is the main unit.
6433 Rewrite (N, Act_Body);
6435 -- Propagate the aspect specifications from the package body template to
6436 -- the instantiated version of the package body.
6438 if Has_Aspects (Act_Body) then
6439 Set_Aspect_Specifications
6440 (N, New_Copy_List_Tree (Aspect_Specifications (Act_Body)));
6441 end if;
6443 Body_Cunit := Parent (N);
6445 -- The two compilation unit nodes are linked by the Library_Unit field
6447 Set_Library_Unit (Decl_Cunit, Body_Cunit);
6448 Set_Library_Unit (Body_Cunit, Decl_Cunit);
6450 -- Preserve the private nature of the package if needed
6452 Set_Private_Present (Decl_Cunit, Private_Present (Body_Cunit));
6454 -- If the instance is not the main unit, its context, categorization
6455 -- and elaboration entity are not relevant to the compilation.
6457 if Body_Cunit /= Cunit (Main_Unit) then
6458 Make_Instance_Unit (Body_Cunit, In_Main => False);
6459 return;
6460 end if;
6462 -- The context clause items on the instantiation, which are now attached
6463 -- to the body compilation unit (since the body overwrote the original
6464 -- instantiation node), semantically belong on the spec, so copy them
6465 -- there. It's harmless to leave them on the body as well. In fact one
6466 -- could argue that they belong in both places.
6468 Citem := First (Context_Items (Body_Cunit));
6469 while Present (Citem) loop
6470 Append (New_Copy (Citem), Context_Items (Decl_Cunit));
6471 Next (Citem);
6472 end loop;
6474 -- Propagate categorization flags on packages, so that they appear in
6475 -- the ali file for the spec of the unit.
6477 if Ekind (New_Main) = E_Package then
6478 Set_Is_Pure (Old_Main, Is_Pure (New_Main));
6479 Set_Is_Preelaborated (Old_Main, Is_Preelaborated (New_Main));
6480 Set_Is_Remote_Types (Old_Main, Is_Remote_Types (New_Main));
6481 Set_Is_Shared_Passive (Old_Main, Is_Shared_Passive (New_Main));
6482 Set_Is_Remote_Call_Interface
6483 (Old_Main, Is_Remote_Call_Interface (New_Main));
6484 end if;
6486 -- Make entry in Units table, so that binder can generate call to
6487 -- elaboration procedure for body, if any.
6489 Make_Instance_Unit (Body_Cunit, In_Main => True);
6490 Main_Unit_Entity := New_Main;
6491 Set_Cunit_Entity (Main_Unit, Main_Unit_Entity);
6493 -- Build elaboration entity, since the instance may certainly generate
6494 -- elaboration code requiring a flag for protection.
6496 Build_Elaboration_Entity (Decl_Cunit, New_Main);
6497 end Build_Instance_Compilation_Unit_Nodes;
6499 -----------------------------
6500 -- Check_Access_Definition --
6501 -----------------------------
6503 procedure Check_Access_Definition (N : Node_Id) is
6504 begin
6505 pragma Assert
6506 (Ada_Version >= Ada_2005 and then Present (Access_Definition (N)));
6507 null;
6508 end Check_Access_Definition;
6510 -----------------------------------
6511 -- Check_Formal_Package_Instance --
6512 -----------------------------------
6514 -- If the formal has specific parameters, they must match those of the
6515 -- actual. Both of them are instances, and the renaming declarations for
6516 -- their formal parameters appear in the same order in both. The analyzed
6517 -- formal has been analyzed in the context of the current instance.
6519 procedure Check_Formal_Package_Instance
6520 (Formal_Pack : Entity_Id;
6521 Actual_Pack : Entity_Id)
6523 E1 : Entity_Id := First_Entity (Actual_Pack);
6524 E2 : Entity_Id := First_Entity (Formal_Pack);
6525 Prev_E1 : Entity_Id;
6527 Expr1 : Node_Id;
6528 Expr2 : Node_Id;
6530 procedure Check_Mismatch (B : Boolean);
6531 -- Common error routine for mismatch between the parameters of the
6532 -- actual instance and those of the formal package.
6534 function Is_Defaulted (Param : Entity_Id) return Boolean;
6535 -- If the formal package has partly box-initialized formals, skip
6536 -- conformance check for these formals. Previously the code assumed
6537 -- that box initialization for a formal package applied to all its
6538 -- formal parameters.
6540 function Same_Instantiated_Constant (E1, E2 : Entity_Id) return Boolean;
6541 -- The formal may come from a nested formal package, and the actual may
6542 -- have been constant-folded. To determine whether the two denote the
6543 -- same entity we may have to traverse several definitions to recover
6544 -- the ultimate entity that they refer to.
6546 function Same_Instantiated_Function (E1, E2 : Entity_Id) return Boolean;
6547 -- The formal and the actual must be identical, but if both are
6548 -- given by attributes they end up renaming different generated bodies,
6549 -- and we must verify that the attributes themselves match.
6551 function Same_Instantiated_Variable (E1, E2 : Entity_Id) return Boolean;
6552 -- Similarly, if the formal comes from a nested formal package, the
6553 -- actual may designate the formal through multiple renamings, which
6554 -- have to be followed to determine the original variable in question.
6556 --------------------
6557 -- Check_Mismatch --
6558 --------------------
6560 procedure Check_Mismatch (B : Boolean) is
6561 -- A Formal_Type_Declaration for a derived private type is rewritten
6562 -- as a private extension decl. (see Analyze_Formal_Derived_Type),
6563 -- which is why we examine the original node.
6565 Kind : constant Node_Kind := Nkind (Original_Node (Parent (E2)));
6567 begin
6568 if Kind = N_Formal_Type_Declaration then
6569 return;
6571 elsif Kind in N_Formal_Object_Declaration
6572 | N_Formal_Package_Declaration
6573 | N_Formal_Subprogram_Declaration
6574 then
6575 null;
6577 -- Ada 2012: If both formal and actual are incomplete types they
6578 -- are conformant.
6580 elsif Is_Incomplete_Type (E1) and then Is_Incomplete_Type (E2) then
6581 null;
6583 elsif B then
6584 Error_Msg_NE
6585 ("actual for & in actual instance does not match formal",
6586 Parent (Actual_Pack), E1);
6587 end if;
6588 end Check_Mismatch;
6590 ------------------
6591 -- Is_Defaulted --
6592 ------------------
6594 function Is_Defaulted (Param : Entity_Id) return Boolean is
6595 Assoc : Node_Id;
6597 begin
6598 Assoc :=
6599 First (Generic_Associations (Parent
6600 (Associated_Formal_Package (Actual_Pack))));
6602 while Present (Assoc) loop
6603 if Nkind (Assoc) = N_Others_Choice then
6604 return True;
6606 elsif Nkind (Assoc) = N_Generic_Association
6607 and then Chars (Selector_Name (Assoc)) = Chars (Param)
6608 then
6609 return Box_Present (Assoc);
6610 end if;
6612 Next (Assoc);
6613 end loop;
6615 return False;
6616 end Is_Defaulted;
6618 --------------------------------
6619 -- Same_Instantiated_Constant --
6620 --------------------------------
6622 function Same_Instantiated_Constant
6623 (E1, E2 : Entity_Id) return Boolean
6625 Ent : Entity_Id;
6627 begin
6628 Ent := E2;
6629 while Present (Ent) loop
6630 if E1 = Ent then
6631 return True;
6633 elsif Ekind (Ent) /= E_Constant then
6634 return False;
6636 elsif Is_Entity_Name (Constant_Value (Ent)) then
6637 if Entity (Constant_Value (Ent)) = E1 then
6638 return True;
6639 else
6640 Ent := Entity (Constant_Value (Ent));
6641 end if;
6643 -- The actual may be a constant that has been folded. Recover
6644 -- original name.
6646 elsif Is_Entity_Name (Original_Node (Constant_Value (Ent))) then
6647 Ent := Entity (Original_Node (Constant_Value (Ent)));
6649 else
6650 return False;
6651 end if;
6652 end loop;
6654 return False;
6655 end Same_Instantiated_Constant;
6657 --------------------------------
6658 -- Same_Instantiated_Function --
6659 --------------------------------
6661 function Same_Instantiated_Function
6662 (E1, E2 : Entity_Id) return Boolean
6664 U1, U2 : Node_Id;
6665 begin
6666 if Alias (E1) = Alias (E2) then
6667 return True;
6669 elsif Present (Alias (E2)) then
6670 U1 := Original_Node (Unit_Declaration_Node (E1));
6671 U2 := Original_Node (Unit_Declaration_Node (Alias (E2)));
6673 return Nkind (U1) = N_Subprogram_Renaming_Declaration
6674 and then Nkind (Name (U1)) = N_Attribute_Reference
6676 and then Nkind (U2) = N_Subprogram_Renaming_Declaration
6677 and then Nkind (Name (U2)) = N_Attribute_Reference
6679 and then
6680 Attribute_Name (Name (U1)) = Attribute_Name (Name (U2));
6681 else
6682 return False;
6683 end if;
6684 end Same_Instantiated_Function;
6686 --------------------------------
6687 -- Same_Instantiated_Variable --
6688 --------------------------------
6690 function Same_Instantiated_Variable
6691 (E1, E2 : Entity_Id) return Boolean
6693 function Original_Entity (E : Entity_Id) return Entity_Id;
6694 -- Follow chain of renamings to the ultimate ancestor
6696 ---------------------
6697 -- Original_Entity --
6698 ---------------------
6700 function Original_Entity (E : Entity_Id) return Entity_Id is
6701 Orig : Entity_Id;
6703 begin
6704 Orig := E;
6705 while Nkind (Parent (Orig)) = N_Object_Renaming_Declaration
6706 and then Present (Renamed_Object (Orig))
6707 and then Is_Entity_Name (Renamed_Object (Orig))
6708 loop
6709 Orig := Entity (Renamed_Object (Orig));
6710 end loop;
6712 return Orig;
6713 end Original_Entity;
6715 -- Start of processing for Same_Instantiated_Variable
6717 begin
6718 return Ekind (E1) = Ekind (E2)
6719 and then Original_Entity (E1) = Original_Entity (E2);
6720 end Same_Instantiated_Variable;
6722 -- Start of processing for Check_Formal_Package_Instance
6724 begin
6725 Prev_E1 := E1;
6726 while Present (E1) and then Present (E2) loop
6727 exit when Ekind (E1) = E_Package
6728 and then Renamed_Entity (E1) = Renamed_Entity (Actual_Pack);
6730 -- If the formal is the renaming of the formal package, this
6731 -- is the end of its formal part, which may occur before the
6732 -- end of the formal part in the actual in the presence of
6733 -- defaulted parameters in the formal package.
6735 exit when Nkind (Parent (E2)) = N_Package_Renaming_Declaration
6736 and then Renamed_Entity (E2) = Scope (E2);
6738 -- The analysis of the actual may generate additional internal
6739 -- entities. If the formal is defaulted, there is no corresponding
6740 -- analysis and the internal entities must be skipped, until we
6741 -- find corresponding entities again.
6743 if Comes_From_Source (E2)
6744 and then not Comes_From_Source (E1)
6745 and then Chars (E1) /= Chars (E2)
6746 then
6747 while Present (E1) and then Chars (E1) /= Chars (E2) loop
6748 Next_Entity (E1);
6749 end loop;
6750 end if;
6752 if No (E1) then
6753 return;
6755 -- Entities may be declared without full declaration, such as
6756 -- itypes and predefined operators (concatenation for arrays, eg).
6757 -- Skip it and keep the formal entity to find a later match for it.
6759 elsif No (Parent (E2)) and then Ekind (E1) /= Ekind (E2) then
6760 E1 := Prev_E1;
6761 goto Next_E;
6763 -- If the formal entity comes from a formal declaration, it was
6764 -- defaulted in the formal package, and no check is needed on it.
6766 elsif Nkind (Original_Node (Parent (E2))) in
6767 N_Formal_Object_Declaration | N_Formal_Type_Declaration
6768 then
6769 -- If the formal is a tagged type the corresponding class-wide
6770 -- type has been generated as well, and it must be skipped.
6772 if Is_Type (E2) and then Is_Tagged_Type (E2) then
6773 Next_Entity (E2);
6774 end if;
6776 goto Next_E;
6778 -- Ditto for defaulted formal subprograms.
6780 elsif Is_Overloadable (E1)
6781 and then Nkind (Unit_Declaration_Node (E2)) in
6782 N_Formal_Subprogram_Declaration
6783 then
6784 goto Next_E;
6786 elsif Is_Defaulted (E1) then
6787 goto Next_E;
6789 elsif Is_Type (E1) then
6791 -- Subtypes must statically match. E1, E2 are the local entities
6792 -- that are subtypes of the actuals. Itypes generated for other
6793 -- parameters need not be checked, the check will be performed
6794 -- on the parameters themselves.
6796 -- If E2 is a formal type declaration, it is a defaulted parameter
6797 -- and needs no checking.
6799 if not Is_Itype (E1) and then not Is_Itype (E2) then
6800 Check_Mismatch
6801 (not Is_Type (E2)
6802 or else Etype (E1) /= Etype (E2)
6803 or else not Subtypes_Statically_Match (E1, E2));
6804 end if;
6806 elsif Ekind (E1) = E_Constant then
6808 -- IN parameters must denote the same static value, or the same
6809 -- constant, or the literal null.
6811 Expr1 := Expression (Parent (E1));
6813 if Ekind (E2) /= E_Constant then
6814 Check_Mismatch (True);
6815 goto Next_E;
6816 else
6817 Expr2 := Expression (Parent (E2));
6818 end if;
6820 if Is_OK_Static_Expression (Expr1) then
6821 if not Is_OK_Static_Expression (Expr2) then
6822 Check_Mismatch (True);
6824 elsif Is_Discrete_Type (Etype (E1)) then
6825 declare
6826 V1 : constant Uint := Expr_Value (Expr1);
6827 V2 : constant Uint := Expr_Value (Expr2);
6828 begin
6829 Check_Mismatch (V1 /= V2);
6830 end;
6832 elsif Is_Real_Type (Etype (E1)) then
6833 declare
6834 V1 : constant Ureal := Expr_Value_R (Expr1);
6835 V2 : constant Ureal := Expr_Value_R (Expr2);
6836 begin
6837 Check_Mismatch (V1 /= V2);
6838 end;
6840 elsif Is_String_Type (Etype (E1))
6841 and then Nkind (Expr1) = N_String_Literal
6842 then
6843 if Nkind (Expr2) /= N_String_Literal then
6844 Check_Mismatch (True);
6845 else
6846 Check_Mismatch
6847 (not String_Equal (Strval (Expr1), Strval (Expr2)));
6848 end if;
6849 end if;
6851 elsif Is_Entity_Name (Expr1) then
6852 if Is_Entity_Name (Expr2) then
6853 if Entity (Expr1) = Entity (Expr2) then
6854 null;
6855 else
6856 Check_Mismatch
6857 (not Same_Instantiated_Constant
6858 (Entity (Expr1), Entity (Expr2)));
6859 end if;
6861 else
6862 Check_Mismatch (True);
6863 end if;
6865 elsif Is_Entity_Name (Original_Node (Expr1))
6866 and then Is_Entity_Name (Expr2)
6867 and then Same_Instantiated_Constant
6868 (Entity (Original_Node (Expr1)), Entity (Expr2))
6869 then
6870 null;
6872 elsif Nkind (Expr1) = N_Null then
6873 Check_Mismatch (Nkind (Expr1) /= N_Null);
6875 else
6876 Check_Mismatch (True);
6877 end if;
6879 elsif Ekind (E1) = E_Variable then
6880 Check_Mismatch (not Same_Instantiated_Variable (E1, E2));
6882 elsif Ekind (E1) = E_Package then
6883 Check_Mismatch
6884 (Ekind (E1) /= Ekind (E2)
6885 or else (Present (Renamed_Entity (E2))
6886 and then Renamed_Entity (E1) /=
6887 Renamed_Entity (E2)));
6889 elsif Is_Overloadable (E1) then
6890 -- Verify that the actual subprograms match. Note that actuals
6891 -- that are attributes are rewritten as subprograms. If the
6892 -- subprogram in the formal package is defaulted, no check is
6893 -- needed. Note that this can only happen in Ada 2005 when the
6894 -- formal package can be partially parameterized.
6896 if Nkind (Unit_Declaration_Node (E1)) =
6897 N_Subprogram_Renaming_Declaration
6898 and then From_Default (Unit_Declaration_Node (E1))
6899 then
6900 null;
6902 -- If the formal package has an "others" box association that
6903 -- covers this formal, there is no need for a check either.
6905 elsif Nkind (Unit_Declaration_Node (E2)) in
6906 N_Formal_Subprogram_Declaration
6907 and then Box_Present (Unit_Declaration_Node (E2))
6908 then
6909 null;
6911 -- No check needed if subprogram is a defaulted null procedure
6913 elsif No (Alias (E2))
6914 and then Ekind (E2) = E_Procedure
6915 and then
6916 Null_Present (Specification (Unit_Declaration_Node (E2)))
6917 then
6918 null;
6920 -- Otherwise the actual in the formal and the actual in the
6921 -- instantiation of the formal must match, up to renamings.
6923 else
6924 Check_Mismatch
6925 (Ekind (E2) /= Ekind (E1)
6926 or else not Same_Instantiated_Function (E1, E2));
6927 end if;
6929 else
6930 raise Program_Error;
6931 end if;
6933 <<Next_E>>
6934 Prev_E1 := E1;
6935 Next_Entity (E1);
6936 Next_Entity (E2);
6937 end loop;
6938 end Check_Formal_Package_Instance;
6940 ---------------------------
6941 -- Check_Formal_Packages --
6942 ---------------------------
6944 procedure Check_Formal_Packages (P_Id : Entity_Id) is
6945 E : Entity_Id;
6946 Formal_P : Entity_Id;
6947 Formal_Decl : Node_Id;
6948 begin
6949 -- Iterate through the declarations in the instance, looking for package
6950 -- renaming declarations that denote instances of formal packages. Stop
6951 -- when we find the renaming of the current package itself. The
6952 -- declaration for a formal package without a box is followed by an
6953 -- internal entity that repeats the instantiation.
6955 E := First_Entity (P_Id);
6956 while Present (E) loop
6957 if Ekind (E) = E_Package then
6958 if Renamed_Entity (E) = P_Id then
6959 exit;
6961 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
6962 null;
6964 else
6965 Formal_Decl := Parent (Associated_Formal_Package (E));
6967 -- Nothing to check if the formal has a box or an others_clause
6968 -- (necessarily with a box), or no associations altogether
6970 if Box_Present (Formal_Decl)
6971 or else No (Generic_Associations (Formal_Decl))
6972 then
6973 null;
6975 elsif Nkind (First (Generic_Associations (Formal_Decl))) =
6976 N_Others_Choice
6977 then
6978 -- The internal validating package was generated but formal
6979 -- and instance are known to be compatible.
6981 Formal_P := Next_Entity (E);
6982 Remove (Unit_Declaration_Node (Formal_P));
6984 else
6985 Formal_P := Next_Entity (E);
6987 -- If the instance is within an enclosing instance body
6988 -- there is no need to verify the legality of current formal
6989 -- packages because they were legal in the generic body.
6990 -- This optimization may be applicable elsewhere, and it
6991 -- also removes spurious errors that may arise with
6992 -- on-the-fly inlining and confusion between private and
6993 -- full views.
6995 if not In_Instance_Body then
6996 Check_Formal_Package_Instance (Formal_P, E);
6997 end if;
6999 -- Restore the visibility of formals of the formal instance
7000 -- that are not defaulted, and are hidden within the current
7001 -- generic. These formals may be visible within an enclosing
7002 -- generic.
7004 declare
7005 Elmt : Elmt_Id;
7006 begin
7007 Elmt := First_Elmt (Hidden_In_Formal_Instance (Formal_P));
7008 while Present (Elmt) loop
7009 Set_Is_Hidden (Node (Elmt), False);
7010 Next_Elmt (Elmt);
7011 end loop;
7012 end;
7014 -- After checking, remove the internal validating package.
7015 -- It is only needed for semantic checks, and as it may
7016 -- contain generic formal declarations it should not reach
7017 -- gigi.
7019 Remove (Unit_Declaration_Node (Formal_P));
7020 end if;
7021 end if;
7022 end if;
7024 Next_Entity (E);
7025 end loop;
7026 end Check_Formal_Packages;
7028 ---------------------------------
7029 -- Check_Forward_Instantiation --
7030 ---------------------------------
7032 procedure Check_Forward_Instantiation (Decl : Node_Id) is
7033 S : Entity_Id;
7034 Gen_Comp : Entity_Id := Cunit_Entity (Get_Source_Unit (Decl));
7036 begin
7037 -- The instantiation appears before the generic body if we are in the
7038 -- scope of the unit containing the generic, either in its spec or in
7039 -- the package body, and before the generic body.
7041 if Ekind (Gen_Comp) = E_Package_Body then
7042 Gen_Comp := Spec_Entity (Gen_Comp);
7043 end if;
7045 if In_Open_Scopes (Gen_Comp)
7046 and then No (Corresponding_Body (Decl))
7047 then
7048 S := Current_Scope;
7050 while Present (S)
7051 and then not Is_Compilation_Unit (S)
7052 and then not Is_Child_Unit (S)
7053 loop
7054 if Ekind (S) = E_Package then
7055 Set_Has_Forward_Instantiation (S);
7056 end if;
7058 S := Scope (S);
7059 end loop;
7060 end if;
7061 end Check_Forward_Instantiation;
7063 ---------------------------
7064 -- Check_Generic_Actuals --
7065 ---------------------------
7067 -- The visibility of the actuals may be different between the point of
7068 -- generic instantiation and the instantiation of the body.
7070 procedure Check_Generic_Actuals
7071 (Instance : Entity_Id;
7072 Is_Formal_Box : Boolean)
7074 E : Entity_Id;
7075 Astype : Entity_Id;
7077 begin
7078 E := First_Entity (Instance);
7079 while Present (E) loop
7080 if Is_Type (E)
7081 and then Nkind (Parent (E)) = N_Subtype_Declaration
7082 and then Scope (Etype (E)) /= Instance
7083 and then Is_Entity_Name (Subtype_Indication (Parent (E)))
7084 then
7085 -- Restore the proper view of the actual from the information
7086 -- saved earlier by Instantiate_Type.
7088 Check_Private_View (Subtype_Indication (Parent (E)));
7090 -- If the actual is itself the formal of a parent instance,
7091 -- then also restore the proper view of its actual and so on.
7092 -- That's necessary for nested instantiations of the form
7094 -- generic
7095 -- type Component is private;
7096 -- type Array_Type is array (Positive range <>) of Component;
7097 -- procedure Proc;
7099 -- when the outermost actuals have inconsistent views, because
7100 -- the Component_Type of Array_Type of the inner instantiations
7101 -- is the actual of Component of the outermost one and not that
7102 -- of the corresponding inner instantiations.
7104 Astype := Ancestor_Subtype (E);
7105 while Present (Astype)
7106 and then Nkind (Parent (Astype)) = N_Subtype_Declaration
7107 and then Present (Generic_Parent_Type (Parent (Astype)))
7108 and then Is_Entity_Name (Subtype_Indication (Parent (Astype)))
7109 loop
7110 Check_Private_View (Subtype_Indication (Parent (Astype)));
7111 Astype := Ancestor_Subtype (Astype);
7112 end loop;
7114 Set_Is_Generic_Actual_Type (E);
7116 if Is_Private_Type (E) and then Present (Full_View (E)) then
7117 Set_Is_Generic_Actual_Type (Full_View (E));
7118 end if;
7120 Set_Is_Hidden (E, False);
7121 Set_Is_Potentially_Use_Visible (E, In_Use (Instance));
7123 -- We constructed the generic actual type as a subtype of the
7124 -- supplied type. This means that it normally would not inherit
7125 -- subtype specific attributes of the actual, which is wrong for
7126 -- the generic case.
7128 Astype := Ancestor_Subtype (E);
7130 if No (Astype) then
7132 -- This can happen when E is an itype that is the full view of
7133 -- a private type completed, e.g. with a constrained array. In
7134 -- that case, use the first subtype, which will carry size
7135 -- information. The base type itself is unconstrained and will
7136 -- not carry it.
7138 Astype := First_Subtype (E);
7139 end if;
7141 Set_Size_Info (E, (Astype));
7142 Copy_RM_Size (To => E, From => Astype);
7143 Set_First_Rep_Item (E, First_Rep_Item (Astype));
7145 if Is_Discrete_Or_Fixed_Point_Type (E) then
7146 Set_RM_Size (E, RM_Size (Astype));
7147 end if;
7149 elsif Ekind (E) = E_Package then
7151 -- If this is the renaming for the current instance, we're done.
7152 -- Otherwise it is a formal package. If the corresponding formal
7153 -- was declared with a box, the (instantiations of the) generic
7154 -- formal part are also visible. Otherwise, ignore the entity
7155 -- created to validate the actuals.
7157 if Renamed_Entity (E) = Instance then
7158 exit;
7160 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
7161 null;
7163 -- The visibility of a formal of an enclosing generic is already
7164 -- correct.
7166 elsif Denotes_Formal_Package (E) then
7167 null;
7169 elsif Present (Associated_Formal_Package (E))
7170 and then not Is_Generic_Formal (E)
7171 then
7172 if Box_Present (Parent (Associated_Formal_Package (E))) then
7173 Check_Generic_Actuals (Renamed_Entity (E), True);
7175 else
7176 Check_Generic_Actuals (Renamed_Entity (E), False);
7177 end if;
7179 Set_Is_Hidden (E, False);
7180 end if;
7182 -- If this is a subprogram instance (in a wrapper package) the
7183 -- actual is fully visible.
7185 elsif Is_Wrapper_Package (Instance) then
7186 Set_Is_Hidden (E, False);
7188 -- If the formal package is declared with a box, or if the formal
7189 -- parameter is defaulted, it is visible in the body.
7191 elsif Is_Formal_Box or else Is_Visible_Formal (E) then
7192 Set_Is_Hidden (E, False);
7193 end if;
7195 if Ekind (E) = E_Constant then
7197 -- If the type of the actual is a private type declared in the
7198 -- enclosing scope of the generic unit, the body of the generic
7199 -- sees the full view of the type (because it has to appear in
7200 -- the corresponding package body). If the type is private now,
7201 -- exchange views to restore the proper visiblity in the instance.
7203 declare
7204 Typ : constant Entity_Id := Base_Type (Etype (E));
7205 -- The type of the actual
7207 Gen_Id : Entity_Id;
7208 -- The generic unit
7210 Parent_Scope : Entity_Id;
7211 -- The enclosing scope of the generic unit
7213 begin
7214 if Is_Wrapper_Package (Instance) then
7215 Gen_Id :=
7216 Generic_Parent
7217 (Specification
7218 (Unit_Declaration_Node
7219 (Related_Instance (Instance))));
7220 else
7221 Gen_Id :=
7222 Generic_Parent (Package_Specification (Instance));
7223 end if;
7225 Parent_Scope := Scope (Gen_Id);
7227 -- The exchange is only needed if the generic is defined
7228 -- within a package which is not a common ancestor of the
7229 -- scope of the instance, and is not already in scope.
7231 if Is_Private_Type (Typ)
7232 and then Scope (Typ) = Parent_Scope
7233 and then Scope (Instance) /= Parent_Scope
7234 and then Ekind (Parent_Scope) = E_Package
7235 and then not Is_Child_Unit (Gen_Id)
7236 then
7237 Switch_View (Typ);
7239 -- If the type of the entity is a subtype, it may also have
7240 -- to be made visible, together with the base type of its
7241 -- full view, after exchange.
7243 if Is_Private_Type (Etype (E)) then
7244 Switch_View (Etype (E));
7245 Switch_View (Base_Type (Etype (E)));
7246 end if;
7247 end if;
7248 end;
7249 end if;
7251 Next_Entity (E);
7252 end loop;
7253 end Check_Generic_Actuals;
7255 ------------------------------
7256 -- Check_Generic_Child_Unit --
7257 ------------------------------
7259 procedure Check_Generic_Child_Unit
7260 (Gen_Id : Node_Id;
7261 Parent_Installed : in out Boolean)
7263 Loc : constant Source_Ptr := Sloc (Gen_Id);
7264 Gen_Par : Entity_Id := Empty;
7265 E : Entity_Id;
7266 Inst_Par : Entity_Id;
7267 S : Node_Id;
7269 function Find_Generic_Child
7270 (Scop : Entity_Id;
7271 Id : Node_Id) return Entity_Id;
7272 -- Search generic parent for possible child unit with the given name
7274 function In_Enclosing_Instance return Boolean;
7275 -- Within an instance of the parent, the child unit may be denoted by
7276 -- a simple name, or an abbreviated expanded name. Examine enclosing
7277 -- scopes to locate a possible parent instantiation.
7279 ------------------------
7280 -- Find_Generic_Child --
7281 ------------------------
7283 function Find_Generic_Child
7284 (Scop : Entity_Id;
7285 Id : Node_Id) return Entity_Id
7287 E : Entity_Id;
7289 begin
7290 -- If entity of name is already set, instance has already been
7291 -- resolved, e.g. in an enclosing instantiation.
7293 if Present (Entity (Id)) then
7294 if Scope (Entity (Id)) = Scop then
7295 return Entity (Id);
7296 else
7297 return Empty;
7298 end if;
7300 else
7301 E := First_Entity (Scop);
7302 while Present (E) loop
7303 if Chars (E) = Chars (Id)
7304 and then Is_Child_Unit (E)
7305 then
7306 if Is_Child_Unit (E)
7307 and then not Is_Visible_Lib_Unit (E)
7308 then
7309 Error_Msg_NE
7310 ("generic child unit& is not visible", Gen_Id, E);
7311 end if;
7313 Set_Entity (Id, E);
7314 return E;
7315 end if;
7317 Next_Entity (E);
7318 end loop;
7320 return Empty;
7321 end if;
7322 end Find_Generic_Child;
7324 ---------------------------
7325 -- In_Enclosing_Instance --
7326 ---------------------------
7328 function In_Enclosing_Instance return Boolean is
7329 Enclosing_Instance : Node_Id;
7330 Instance_Decl : Node_Id;
7332 begin
7333 -- We do not inline any call that contains instantiations, except
7334 -- for instantiations of Unchecked_Conversion, so if we are within
7335 -- an inlined body the current instance does not require parents.
7337 if In_Inlined_Body then
7338 pragma Assert (Chars (Gen_Id) = Name_Unchecked_Conversion);
7339 return False;
7340 end if;
7342 -- Loop to check enclosing scopes
7344 Enclosing_Instance := Current_Scope;
7345 while Present (Enclosing_Instance) loop
7346 Instance_Decl := Unit_Declaration_Node (Enclosing_Instance);
7348 if Ekind (Enclosing_Instance) = E_Package
7349 and then Is_Generic_Instance (Enclosing_Instance)
7350 and then Present
7351 (Generic_Parent (Specification (Instance_Decl)))
7352 then
7353 -- Check whether the generic we are looking for is a child of
7354 -- this instance.
7356 E := Find_Generic_Child
7357 (Generic_Parent (Specification (Instance_Decl)), Gen_Id);
7358 exit when Present (E);
7360 else
7361 E := Empty;
7362 end if;
7364 Enclosing_Instance := Scope (Enclosing_Instance);
7365 end loop;
7367 if No (E) then
7369 -- Not a child unit
7371 Analyze (Gen_Id);
7372 return False;
7374 else
7375 Rewrite (Gen_Id,
7376 Make_Expanded_Name (Loc,
7377 Chars => Chars (E),
7378 Prefix => New_Occurrence_Of (Enclosing_Instance, Loc),
7379 Selector_Name => New_Occurrence_Of (E, Loc)));
7381 Set_Entity (Gen_Id, E);
7382 Set_Etype (Gen_Id, Etype (E));
7383 Parent_Installed := False; -- Already in scope.
7384 return True;
7385 end if;
7386 end In_Enclosing_Instance;
7388 -- Start of processing for Check_Generic_Child_Unit
7390 begin
7391 -- If the name of the generic is given by a selected component, it may
7392 -- be the name of a generic child unit, and the prefix is the name of an
7393 -- instance of the parent, in which case the child unit must be visible.
7394 -- If this instance is not in scope, it must be placed there and removed
7395 -- after instantiation, because what is being instantiated is not the
7396 -- original child, but the corresponding child present in the instance
7397 -- of the parent.
7399 -- If the child is instantiated within the parent, it can be given by
7400 -- a simple name. In this case the instance is already in scope, but
7401 -- the child generic must be recovered from the generic parent as well.
7403 if Nkind (Gen_Id) = N_Selected_Component then
7404 S := Selector_Name (Gen_Id);
7405 Analyze (Prefix (Gen_Id));
7406 Inst_Par := Entity (Prefix (Gen_Id));
7408 if Ekind (Inst_Par) = E_Package
7409 and then Present (Renamed_Entity (Inst_Par))
7410 then
7411 Inst_Par := Renamed_Entity (Inst_Par);
7412 end if;
7414 if Ekind (Inst_Par) = E_Package then
7415 if Nkind (Parent (Inst_Par)) = N_Package_Specification then
7416 Gen_Par := Generic_Parent (Parent (Inst_Par));
7418 elsif Nkind (Parent (Inst_Par)) = N_Defining_Program_Unit_Name
7419 and then
7420 Nkind (Parent (Parent (Inst_Par))) = N_Package_Specification
7421 then
7422 Gen_Par := Generic_Parent (Parent (Parent (Inst_Par)));
7423 end if;
7425 elsif Ekind (Inst_Par) = E_Generic_Package
7426 and then Nkind (Parent (Gen_Id)) = N_Formal_Package_Declaration
7427 then
7428 -- A formal package may be a real child package, and not the
7429 -- implicit instance within a parent. In this case the child is
7430 -- not visible and has to be retrieved explicitly as well.
7432 Gen_Par := Inst_Par;
7433 end if;
7435 if Present (Gen_Par) then
7437 -- The prefix denotes an instantiation. The entity itself may be a
7438 -- nested generic, or a child unit.
7440 E := Find_Generic_Child (Gen_Par, S);
7442 if Present (E) then
7443 Change_Selected_Component_To_Expanded_Name (Gen_Id);
7444 Set_Entity (Gen_Id, E);
7445 Set_Etype (Gen_Id, Etype (E));
7446 Set_Entity (S, E);
7447 Set_Etype (S, Etype (E));
7449 -- Indicate that this is a reference to the parent
7451 if In_Extended_Main_Source_Unit (Gen_Id) then
7452 Set_Is_Instantiated (Inst_Par);
7453 end if;
7455 -- A common mistake is to replicate the naming scheme of a
7456 -- hierarchy by instantiating a generic child directly, rather
7457 -- than the implicit child in a parent instance:
7459 -- generic .. package Gpar is ..
7460 -- generic .. package Gpar.Child is ..
7461 -- package Par is new Gpar ();
7463 -- with Gpar.Child;
7464 -- package Par.Child is new Gpar.Child ();
7465 -- rather than Par.Child
7467 -- In this case the instantiation is within Par, which is an
7468 -- instance, but Gpar does not denote Par because we are not IN
7469 -- the instance of Gpar, so this is illegal. The test below
7470 -- recognizes this particular case.
7472 if Is_Child_Unit (E)
7473 and then not Comes_From_Source (Entity (Prefix (Gen_Id)))
7474 and then (not In_Instance
7475 or else Nkind (Parent (Parent (Gen_Id))) =
7476 N_Compilation_Unit)
7477 then
7478 Error_Msg_N
7479 ("prefix of generic child unit must be instance of parent",
7480 Gen_Id);
7481 end if;
7483 if not In_Open_Scopes (Inst_Par)
7484 and then Nkind (Parent (Gen_Id)) not in
7485 N_Generic_Renaming_Declaration
7486 then
7487 Install_Parent (Inst_Par);
7488 Parent_Installed := True;
7490 elsif In_Open_Scopes (Inst_Par) then
7492 -- If the parent is already installed, install the actuals
7493 -- for its formal packages. This is necessary when the child
7494 -- instance is a child of the parent instance: in this case,
7495 -- the parent is placed on the scope stack but the formal
7496 -- packages are not made visible.
7498 Install_Formal_Packages (Inst_Par);
7499 end if;
7501 else
7502 -- If the generic parent does not contain an entity that
7503 -- corresponds to the selector, the instance doesn't either.
7504 -- Analyzing the node will yield the appropriate error message.
7505 -- If the entity is not a child unit, then it is an inner
7506 -- generic in the parent.
7508 Analyze (Gen_Id);
7509 end if;
7511 else
7512 Analyze (Gen_Id);
7514 if Is_Child_Unit (Entity (Gen_Id))
7515 and then
7516 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
7517 and then not In_Open_Scopes (Inst_Par)
7518 then
7519 Install_Parent (Inst_Par);
7520 Parent_Installed := True;
7522 -- The generic unit may be the renaming of the implicit child
7523 -- present in an instance. In that case the parent instance is
7524 -- obtained from the name of the renamed entity.
7526 elsif Ekind (Entity (Gen_Id)) = E_Generic_Package
7527 and then Present (Renamed_Entity (Entity (Gen_Id)))
7528 and then Is_Child_Unit (Renamed_Entity (Entity (Gen_Id)))
7529 then
7530 declare
7531 Renamed_Package : constant Node_Id :=
7532 Name (Parent (Entity (Gen_Id)));
7533 begin
7534 if Nkind (Renamed_Package) = N_Expanded_Name then
7535 Inst_Par := Entity (Prefix (Renamed_Package));
7536 Install_Parent (Inst_Par);
7537 Parent_Installed := True;
7538 end if;
7539 end;
7540 end if;
7541 end if;
7543 elsif Nkind (Gen_Id) = N_Expanded_Name then
7545 -- Entity already present, analyze prefix, whose meaning may be an
7546 -- instance in the current context. If it is an instance of a
7547 -- relative within another, the proper parent may still have to be
7548 -- installed, if they are not of the same generation.
7550 Analyze (Prefix (Gen_Id));
7552 -- Prevent cascaded errors
7554 if Etype (Prefix (Gen_Id)) = Any_Type then
7555 return;
7556 end if;
7558 -- In the unlikely case that a local declaration hides the name of
7559 -- the parent package, locate it on the homonym chain. If the context
7560 -- is an instance of the parent, the renaming entity is flagged as
7561 -- such.
7563 Inst_Par := Entity (Prefix (Gen_Id));
7564 while Present (Inst_Par)
7565 and then not Is_Package_Or_Generic_Package (Inst_Par)
7566 loop
7567 Inst_Par := Homonym (Inst_Par);
7568 end loop;
7570 pragma Assert (Present (Inst_Par));
7571 Set_Entity (Prefix (Gen_Id), Inst_Par);
7573 if In_Enclosing_Instance then
7574 null;
7576 elsif Present (Entity (Gen_Id))
7577 and then No (Renamed_Entity (Entity (Gen_Id)))
7578 and then Is_Child_Unit (Entity (Gen_Id))
7579 and then not In_Open_Scopes (Inst_Par)
7580 then
7581 Install_Parent (Inst_Par);
7582 Parent_Installed := True;
7584 -- Handle renaming of generic child unit
7586 elsif Present (Entity (Gen_Id))
7587 and then Present (Renamed_Entity (Entity (Gen_Id)))
7588 and then Is_Child_Unit (Renamed_Entity (Entity (Gen_Id)))
7589 then
7590 declare
7591 E : Entity_Id;
7592 Ren_Decl : Node_Id;
7594 begin
7595 -- The entity of the renamed generic child unit does not
7596 -- have any reference to the instantiated parent. In order to
7597 -- locate it we traverse the scope containing the renaming
7598 -- declaration; the instance of the parent is available in
7599 -- the prefix of the renaming declaration. For example:
7601 -- package A is
7602 -- package Inst_Par is new ...
7603 -- generic package Ren_Child renames Ins_Par.Child;
7604 -- end;
7606 -- with A;
7607 -- package B is
7608 -- package Inst_Child is new A.Ren_Child;
7609 -- end;
7611 E := First_Entity (Entity (Prefix (Gen_Id)));
7612 while Present (E) loop
7613 if not Is_Object (E)
7614 and then Present (Renamed_Entity (E))
7615 and then
7616 Renamed_Entity (E) = Renamed_Entity (Entity (Gen_Id))
7617 then
7618 Ren_Decl := Parent (E);
7619 Inst_Par := Entity (Prefix (Name (Ren_Decl)));
7621 if not In_Open_Scopes (Inst_Par) then
7622 Install_Parent (Inst_Par);
7623 Parent_Installed := True;
7624 end if;
7626 exit;
7627 end if;
7629 E := Next_Entity (E);
7630 end loop;
7631 end;
7632 end if;
7634 elsif In_Enclosing_Instance then
7636 -- The child unit is found in some enclosing scope
7638 null;
7640 else
7641 Analyze (Gen_Id);
7643 -- If this is the renaming of the implicit child in a parent
7644 -- instance, recover the parent name and install it.
7646 if Is_Entity_Name (Gen_Id) then
7647 E := Entity (Gen_Id);
7649 if Is_Generic_Unit (E)
7650 and then Nkind (Parent (E)) in N_Generic_Renaming_Declaration
7651 and then Is_Child_Unit (Renamed_Entity (E))
7652 and then Is_Generic_Unit (Scope (Renamed_Entity (E)))
7653 and then Nkind (Name (Parent (E))) = N_Expanded_Name
7654 then
7655 Rewrite (Gen_Id, New_Copy_Tree (Name (Parent (E))));
7656 Inst_Par := Entity (Prefix (Gen_Id));
7658 if not In_Open_Scopes (Inst_Par) then
7659 Install_Parent (Inst_Par);
7660 Parent_Installed := True;
7661 end if;
7663 -- If it is a child unit of a non-generic parent, it may be
7664 -- use-visible and given by a direct name. Install parent as
7665 -- for other cases.
7667 elsif Is_Generic_Unit (E)
7668 and then Is_Child_Unit (E)
7669 and then
7670 Nkind (Parent (Gen_Id)) not in N_Generic_Renaming_Declaration
7671 and then not Is_Generic_Unit (Scope (E))
7672 then
7673 if not In_Open_Scopes (Scope (E)) then
7674 Install_Parent (Scope (E));
7675 Parent_Installed := True;
7676 end if;
7677 end if;
7678 end if;
7679 end if;
7680 end Check_Generic_Child_Unit;
7682 -----------------------------
7683 -- Check_Hidden_Child_Unit --
7684 -----------------------------
7686 procedure Check_Hidden_Child_Unit
7687 (N : Node_Id;
7688 Gen_Unit : Entity_Id;
7689 Act_Decl_Id : Entity_Id)
7691 Gen_Id : constant Node_Id := Name (N);
7693 begin
7694 if Is_Child_Unit (Gen_Unit)
7695 and then Is_Child_Unit (Act_Decl_Id)
7696 and then Nkind (Gen_Id) = N_Expanded_Name
7697 and then Entity (Prefix (Gen_Id)) = Scope (Act_Decl_Id)
7698 and then Chars (Gen_Unit) = Chars (Act_Decl_Id)
7699 then
7700 Error_Msg_Node_2 := Scope (Act_Decl_Id);
7701 Error_Msg_NE
7702 ("generic unit & is implicitly declared in &",
7703 Defining_Unit_Name (N), Gen_Unit);
7704 Error_Msg_N ("\instance must have different name",
7705 Defining_Unit_Name (N));
7706 end if;
7707 end Check_Hidden_Child_Unit;
7709 ------------------------
7710 -- Check_Private_View --
7711 ------------------------
7713 procedure Check_Private_View (N : Node_Id) is
7714 T : constant Entity_Id := Etype (N);
7715 BT : Entity_Id;
7717 begin
7718 -- Exchange views if the type was not private in the generic but is
7719 -- private at the point of instantiation. Do not exchange views if
7720 -- the scope of the type is in scope. This can happen if both generic
7721 -- and instance are sibling units, or if type is defined in a parent.
7722 -- In this case the visibility of the type will be correct for all
7723 -- semantic checks.
7725 if Present (T) then
7726 BT := Base_Type (T);
7728 if Is_Private_Type (T)
7729 and then not Has_Private_View (N)
7730 and then Present (Full_View (T))
7731 and then not In_Open_Scopes (Scope (T))
7732 then
7733 -- In the generic, the full declaration was visible
7735 Switch_View (T);
7737 elsif Has_Private_View (N)
7738 and then not Is_Private_Type (T)
7739 and then not Has_Been_Exchanged (T)
7740 and then (not In_Open_Scopes (Scope (T))
7741 or else Nkind (Parent (N)) = N_Subtype_Declaration)
7742 then
7743 -- In the generic, only the private declaration was visible
7745 -- If the type appears in a subtype declaration, the subtype in
7746 -- instance must have a view compatible with that of its parent,
7747 -- which must be exchanged (see corresponding code in Restore_
7748 -- Private_Views) so we make an exception to the open scope rule.
7750 Prepend_Elmt (T, Exchanged_Views);
7751 Exchange_Declarations (Etype (Get_Associated_Node (N)));
7753 -- Finally, a non-private subtype may have a private base type, which
7754 -- must be exchanged for consistency. This can happen when a package
7755 -- body is instantiated, when the scope stack is empty but in fact
7756 -- the subtype and the base type are declared in an enclosing scope.
7758 -- Note that in this case we introduce an inconsistency in the view
7759 -- set, because we switch the base type BT, but there could be some
7760 -- private dependent subtypes of BT which remain unswitched. Such
7761 -- subtypes might need to be switched at a later point (see specific
7762 -- provision for that case in Switch_View).
7764 elsif not Is_Private_Type (T)
7765 and then not Has_Private_View (N)
7766 and then Is_Private_Type (BT)
7767 and then Present (Full_View (BT))
7768 and then not Is_Generic_Type (BT)
7769 and then not In_Open_Scopes (BT)
7770 then
7771 Prepend_Elmt (Full_View (BT), Exchanged_Views);
7772 Exchange_Declarations (BT);
7773 end if;
7774 end if;
7775 end Check_Private_View;
7777 -----------------------------
7778 -- Check_Hidden_Primitives --
7779 -----------------------------
7781 function Check_Hidden_Primitives (Assoc_List : List_Id) return Elist_Id is
7782 Actual : Node_Id;
7783 Gen_T : Entity_Id;
7784 Result : Elist_Id := No_Elist;
7786 begin
7787 if No (Assoc_List) then
7788 return No_Elist;
7789 end if;
7791 -- Traverse the list of associations between formals and actuals
7792 -- searching for renamings of tagged types
7794 Actual := First (Assoc_List);
7795 while Present (Actual) loop
7796 if Nkind (Actual) = N_Subtype_Declaration then
7797 Gen_T := Generic_Parent_Type (Actual);
7799 if Present (Gen_T) and then Is_Tagged_Type (Gen_T) then
7801 -- Traverse the list of primitives of the actual types
7802 -- searching for hidden primitives that are visible in the
7803 -- corresponding generic formal; leave them visible and
7804 -- append them to Result to restore their decoration later.
7806 Install_Hidden_Primitives
7807 (Prims_List => Result,
7808 Gen_T => Gen_T,
7809 Act_T => Entity (Subtype_Indication (Actual)));
7810 end if;
7811 end if;
7813 Next (Actual);
7814 end loop;
7816 return Result;
7817 end Check_Hidden_Primitives;
7819 --------------------------
7820 -- Contains_Instance_Of --
7821 --------------------------
7823 function Contains_Instance_Of
7824 (Inner : Entity_Id;
7825 Outer : Entity_Id;
7826 N : Node_Id) return Boolean
7828 Elmt : Elmt_Id;
7829 Scop : Entity_Id;
7831 begin
7832 Scop := Outer;
7834 -- Verify that there are no circular instantiations. We check whether
7835 -- the unit contains an instance of the current scope or some enclosing
7836 -- scope (in case one of the instances appears in a subunit). Longer
7837 -- circularities involving subunits might seem too pathological to
7838 -- consider, but they were not too pathological for the authors of
7839 -- DEC bc30vsq, so we loop over all enclosing scopes, and mark all
7840 -- enclosing generic scopes as containing an instance.
7842 loop
7843 -- Within a generic subprogram body, the scope is not generic, to
7844 -- allow for recursive subprograms. Use the declaration to determine
7845 -- whether this is a generic unit.
7847 if Ekind (Scop) = E_Generic_Package
7848 or else (Is_Subprogram (Scop)
7849 and then Nkind (Unit_Declaration_Node (Scop)) =
7850 N_Generic_Subprogram_Declaration)
7851 then
7852 Elmt := First_Elmt (Inner_Instances (Inner));
7854 while Present (Elmt) loop
7855 if Node (Elmt) = Scop then
7856 Error_Msg_Node_2 := Inner;
7857 Error_Msg_NE
7858 ("circular instantiation: & instantiated within &!",
7859 N, Scop);
7860 return True;
7862 elsif Node (Elmt) = Inner then
7863 return True;
7865 elsif Contains_Instance_Of (Node (Elmt), Scop, N) then
7866 Error_Msg_Node_2 := Inner;
7867 Error_Msg_NE
7868 ("circular instantiation: & instantiated within &!",
7869 N, Node (Elmt));
7870 return True;
7871 end if;
7873 Next_Elmt (Elmt);
7874 end loop;
7876 -- Indicate that Inner is being instantiated within Scop
7878 Append_Elmt (Inner, Inner_Instances (Scop));
7879 end if;
7881 if Scop = Standard_Standard then
7882 exit;
7883 else
7884 Scop := Scope (Scop);
7885 end if;
7886 end loop;
7888 return False;
7889 end Contains_Instance_Of;
7891 -----------------------
7892 -- Copy_Generic_Node --
7893 -----------------------
7895 function Copy_Generic_Node
7896 (N : Node_Id;
7897 Parent_Id : Node_Id;
7898 Instantiating : Boolean) return Node_Id
7900 Ent : Entity_Id;
7901 New_N : Node_Id;
7903 function Copy_Generic_Descendant (D : Union_Id) return Union_Id;
7904 -- Check the given value of one of the Fields referenced by the current
7905 -- node to determine whether to copy it recursively. The field may hold
7906 -- a Node_Id, a List_Id, or an Elist_Id, or a plain value (Sloc, Uint,
7907 -- Char) in which case it need not be copied.
7909 procedure Copy_Descendants;
7910 -- Common utility for various nodes
7912 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id;
7913 -- Make copy of element list
7915 function Copy_Generic_List
7916 (L : List_Id;
7917 Parent_Id : Node_Id) return List_Id;
7918 -- Apply Copy_Node recursively to the members of a node list
7920 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean;
7921 -- True if an identifier is part of the defining program unit name of
7922 -- a child unit.
7923 -- Consider removing this subprogram now that ASIS no longer uses it.
7925 ----------------------
7926 -- Copy_Descendants --
7927 ----------------------
7929 procedure Copy_Descendants is
7930 procedure Walk is new
7931 Walk_Sinfo_Fields_Pairwise (Copy_Generic_Descendant);
7932 begin
7933 Walk (New_N, N);
7934 end Copy_Descendants;
7936 -----------------------------
7937 -- Copy_Generic_Descendant --
7938 -----------------------------
7940 function Copy_Generic_Descendant (D : Union_Id) return Union_Id is
7941 begin
7942 if D = Union_Id (Empty) then
7943 return D;
7945 elsif D in Node_Range then
7946 return Union_Id
7947 (Copy_Generic_Node (Node_Id (D), New_N, Instantiating));
7949 elsif D in List_Range then
7950 return Union_Id (Copy_Generic_List (List_Id (D), New_N));
7952 elsif D in Elist_Range then
7953 return Union_Id (Copy_Generic_Elist (Elist_Id (D)));
7955 -- Nothing else is copyable (e.g. Uint values), return as is
7957 else
7958 return D;
7959 end if;
7960 end Copy_Generic_Descendant;
7962 ------------------------
7963 -- Copy_Generic_Elist --
7964 ------------------------
7966 function Copy_Generic_Elist (E : Elist_Id) return Elist_Id is
7967 M : Elmt_Id;
7968 L : Elist_Id;
7970 begin
7971 if Present (E) then
7972 L := New_Elmt_List;
7973 M := First_Elmt (E);
7974 while Present (M) loop
7975 Append_Elmt
7976 (Copy_Generic_Node (Node (M), Empty, Instantiating), L);
7977 Next_Elmt (M);
7978 end loop;
7980 return L;
7982 else
7983 return No_Elist;
7984 end if;
7985 end Copy_Generic_Elist;
7987 -----------------------
7988 -- Copy_Generic_List --
7989 -----------------------
7991 function Copy_Generic_List
7992 (L : List_Id;
7993 Parent_Id : Node_Id) return List_Id
7995 N : Node_Id;
7996 New_L : List_Id;
7998 begin
7999 if Present (L) then
8000 New_L := New_List;
8001 Set_Parent (New_L, Parent_Id);
8003 N := First (L);
8004 while Present (N) loop
8005 Append (Copy_Generic_Node (N, Empty, Instantiating), New_L);
8006 Next (N);
8007 end loop;
8009 return New_L;
8011 else
8012 return No_List;
8013 end if;
8014 end Copy_Generic_List;
8016 ---------------------------
8017 -- In_Defining_Unit_Name --
8018 ---------------------------
8020 function In_Defining_Unit_Name (Nam : Node_Id) return Boolean is
8021 begin
8022 return
8023 Present (Parent (Nam))
8024 and then (Nkind (Parent (Nam)) = N_Defining_Program_Unit_Name
8025 or else
8026 (Nkind (Parent (Nam)) = N_Expanded_Name
8027 and then In_Defining_Unit_Name (Parent (Nam))));
8028 end In_Defining_Unit_Name;
8030 -- Start of processing for Copy_Generic_Node
8032 begin
8033 if N = Empty then
8034 return N;
8035 end if;
8037 New_N := New_Copy (N);
8039 -- Copy aspects if present
8041 if Has_Aspects (N) then
8042 Set_Has_Aspects (New_N, False);
8043 Set_Aspect_Specifications
8044 (New_N, Copy_Generic_List (Aspect_Specifications (N), Parent_Id));
8045 end if;
8047 -- If we are instantiating, we want to adjust the sloc based on the
8048 -- current S_Adjustment. However, if this is the root node of a subunit,
8049 -- we need to defer that adjustment to below (see "elsif Instantiating
8050 -- and Was_Stub"), so it comes after Create_Instantiation_Source has
8051 -- computed the adjustment.
8053 if Instantiating
8054 and then not (Nkind (N) in N_Proper_Body
8055 and then Was_Originally_Stub (N))
8056 then
8057 Adjust_Instantiation_Sloc (New_N, S_Adjustment);
8058 end if;
8060 if not Is_List_Member (N) then
8061 Set_Parent (New_N, Parent_Id);
8062 end if;
8064 -- Special casing for identifiers and other entity names and operators
8066 if Nkind (New_N) in N_Character_Literal
8067 | N_Expanded_Name
8068 | N_Identifier
8069 | N_Operator_Symbol
8070 | N_Op
8071 then
8072 if not Instantiating then
8074 -- Link both nodes in order to assign subsequently the entity of
8075 -- the copy to the original node, in case this is a global
8076 -- reference.
8078 Set_Associated_Node (N, New_N);
8080 -- If we are within an instantiation, this is a nested generic
8081 -- that has already been analyzed at the point of definition.
8082 -- We must preserve references that were global to the enclosing
8083 -- parent at that point. Other occurrences, whether global or
8084 -- local to the current generic, must be resolved anew, so we
8085 -- reset the entity in the generic copy. A global reference has a
8086 -- smaller depth than the parent, or else the same depth in case
8087 -- both are distinct compilation units.
8089 -- A child unit is implicitly declared within the enclosing parent
8090 -- but is in fact global to it, and must be preserved.
8092 -- It is also possible for Current_Instantiated_Parent to be
8093 -- defined, and for this not to be a nested generic, namely if
8094 -- the unit is loaded through Rtsfind. In that case, the entity of
8095 -- New_N is only a link to the associated node, and not a defining
8096 -- occurrence.
8098 -- The entities for parent units in the defining_program_unit of a
8099 -- generic child unit are established when the context of the unit
8100 -- is first analyzed, before the generic copy is made. They are
8101 -- preserved in the copy for use in e.g. ASIS queries.
8103 Ent := Entity (New_N);
8105 if No (Current_Instantiated_Parent.Gen_Id) then
8106 if No (Ent)
8107 or else Nkind (Ent) /= N_Defining_Identifier
8108 or else not In_Defining_Unit_Name (N)
8109 then
8110 Set_Associated_Node (New_N, Empty);
8111 end if;
8113 elsif No (Ent)
8114 or else Nkind (Ent) not in N_Entity
8115 or else No (Scope (Ent))
8116 or else
8117 (Scope (Ent) = Current_Instantiated_Parent.Gen_Id
8118 and then not Is_Child_Unit (Ent))
8119 or else
8120 (Scope_Depth_Set (Scope (Ent))
8121 and then
8122 Scope_Depth (Scope (Ent)) >
8123 Scope_Depth (Current_Instantiated_Parent.Gen_Id)
8124 and then
8125 Get_Source_Unit (Ent) =
8126 Get_Source_Unit (Current_Instantiated_Parent.Gen_Id))
8127 then
8128 Set_Associated_Node (New_N, Empty);
8129 end if;
8131 -- Case of instantiating identifier or some other name or operator
8133 else
8134 -- If the associated node is still defined, the entity in it
8135 -- is global, and must be copied to the instance. If this copy
8136 -- is being made for a body to inline, it is applied to an
8137 -- instantiated tree, and the entity is already present and
8138 -- must be also preserved.
8140 declare
8141 Assoc : constant Node_Id := Get_Associated_Node (N);
8143 begin
8144 if Present (Assoc) then
8145 if Nkind (Assoc) = Nkind (N) then
8146 Set_Entity (New_N, Entity (Assoc));
8147 Check_Private_View (N);
8149 -- Here we deal with a very peculiar case for which the
8150 -- Has_Private_View mechanism is not sufficient, because
8151 -- the reference to the type is implicit in the tree,
8152 -- that is to say, it's not referenced from a node but
8153 -- only from another type, namely through Component_Type.
8155 -- package P is
8157 -- type Pt is private;
8159 -- generic
8160 -- type Ft is array (Positive range <>) of Pt;
8161 -- package G is
8162 -- procedure Check (F1, F2 : Ft; Lt : Boolean);
8163 -- end G;
8165 -- private
8166 -- type Pt is new Boolean;
8167 -- end P;
8169 -- package body P is
8170 -- package body G is
8171 -- procedure Check (F1, F2 : Ft; Lt : Boolean) is
8172 -- begin
8173 -- if (F1 < F2) /= Lt then
8174 -- null;
8175 -- end if;
8176 -- end Check;
8177 -- end G;
8178 -- end P;
8180 -- type Arr is array (Positive range <>) of P.Pt;
8182 -- package Inst is new P.G (Arr);
8184 -- Pt is a global type for the generic package G and it
8185 -- is not referenced in its body, but only as component
8186 -- type of Ft, which is a local type. This means that no
8187 -- references to Pt or Ft are seen during the copy of the
8188 -- body, the only reference to Pt being seen is when the
8189 -- actuals are checked by Check_Generic_Actuals, but Pt
8190 -- is still private at this point. In the end, the views
8191 -- of Pt are not switched in the body and, therefore, the
8192 -- array comparison is rejected because the component is
8193 -- still private.
8195 -- Adding e.g. a dummy variable of type Pt in the body is
8196 -- sufficient to make everything work, so we generate an
8197 -- artificial reference to Pt on the fly and thus force
8198 -- the switching of views on the grounds that, if the
8199 -- comparison was accepted during the semantic analysis
8200 -- of the generic, this means that the component cannot
8201 -- have been private (see Sem_Type.Valid_Comparison_Arg).
8203 if Nkind (Assoc) in N_Op_Compare
8204 and then Present (Etype (Left_Opnd (Assoc)))
8205 and then Is_Array_Type (Etype (Left_Opnd (Assoc)))
8206 and then Present (Etype (Right_Opnd (Assoc)))
8207 and then Is_Array_Type (Etype (Right_Opnd (Assoc)))
8208 then
8209 declare
8210 Ltyp : constant Entity_Id :=
8211 Etype (Left_Opnd (Assoc));
8212 Rtyp : constant Entity_Id :=
8213 Etype (Right_Opnd (Assoc));
8214 begin
8215 if Is_Private_Type (Component_Type (Ltyp)) then
8216 Check_Private_View
8217 (New_Occurrence_Of (Component_Type (Ltyp),
8218 Sloc (N)));
8219 end if;
8220 if Is_Private_Type (Component_Type (Rtyp)) then
8221 Check_Private_View
8222 (New_Occurrence_Of (Component_Type (Rtyp),
8223 Sloc (N)));
8224 end if;
8225 end;
8227 -- Here is a similar case, for the Designated_Type of an
8228 -- access type that is present as target type in a type
8229 -- conversion from another access type. In this case, if
8230 -- the base types of the designated types are different
8231 -- and the conversion was accepted during the semantic
8232 -- analysis of the generic, this means that the target
8233 -- type cannot have been private (see Valid_Conversion).
8235 elsif Nkind (Assoc) = N_Identifier
8236 and then Nkind (Parent (Assoc)) = N_Type_Conversion
8237 and then Subtype_Mark (Parent (Assoc)) = Assoc
8238 and then Present (Etype (Assoc))
8239 and then Is_Access_Type (Etype (Assoc))
8240 and then Present (Etype (Expression (Parent (Assoc))))
8241 and then
8242 Is_Access_Type (Etype (Expression (Parent (Assoc))))
8243 then
8244 declare
8245 Targ_Desig : constant Entity_Id :=
8246 Designated_Type (Etype (Assoc));
8247 Expr_Desig : constant Entity_Id :=
8248 Designated_Type
8249 (Etype (Expression (Parent (Assoc))));
8250 begin
8251 if Base_Type (Targ_Desig) /= Base_Type (Expr_Desig)
8252 and then Is_Private_Type (Targ_Desig)
8253 then
8254 Check_Private_View
8255 (New_Occurrence_Of (Targ_Desig, Sloc (N)));
8256 end if;
8257 end;
8258 end if;
8260 -- The node is a reference to a global type and acts as the
8261 -- subtype mark of a qualified expression created in order
8262 -- to aid resolution of accidental overloading in instances.
8263 -- Since N is a reference to a type, the Associated_Node of
8264 -- N denotes an entity rather than another identifier. See
8265 -- Qualify_Universal_Operands for details.
8267 elsif Nkind (N) = N_Identifier
8268 and then Nkind (Parent (N)) = N_Qualified_Expression
8269 and then Subtype_Mark (Parent (N)) = N
8270 and then Is_Qualified_Universal_Literal (Parent (N))
8271 then
8272 Set_Entity (New_N, Assoc);
8274 -- The name in the call may be a selected component if the
8275 -- call has not been analyzed yet, as may be the case for
8276 -- pre/post conditions in a generic unit.
8278 elsif Nkind (Assoc) = N_Function_Call
8279 and then Is_Entity_Name (Name (Assoc))
8280 then
8281 Set_Entity (New_N, Entity (Name (Assoc)));
8283 elsif Nkind (Assoc) in N_Entity
8284 and then Expander_Active
8285 then
8286 -- Inlining case: we are copying a tree that contains
8287 -- global entities, which are preserved in the copy to be
8288 -- used for subsequent inlining.
8290 null;
8292 else
8293 Set_Entity (New_N, Empty);
8294 end if;
8295 end if;
8296 end;
8297 end if;
8299 -- For expanded name, we must copy the Prefix and Selector_Name
8301 if Nkind (N) = N_Expanded_Name then
8302 Set_Prefix
8303 (New_N, Copy_Generic_Node (Prefix (N), New_N, Instantiating));
8305 Set_Selector_Name (New_N,
8306 Copy_Generic_Node (Selector_Name (N), New_N, Instantiating));
8308 -- For operators, copy the operands
8310 elsif Nkind (N) in N_Op then
8311 if Nkind (N) in N_Binary_Op then
8312 Set_Left_Opnd (New_N,
8313 Copy_Generic_Node (Left_Opnd (N), New_N, Instantiating));
8314 end if;
8316 Set_Right_Opnd (New_N,
8317 Copy_Generic_Node (Right_Opnd (N), New_N, Instantiating));
8318 end if;
8320 -- Establish a link between an entity from the generic template and the
8321 -- corresponding entity in the generic copy to be analyzed.
8323 elsif Nkind (N) in N_Entity then
8324 if not Instantiating then
8325 Set_Associated_Entity (N, New_N);
8326 end if;
8328 -- Clear any existing link the copy may inherit from the replicated
8329 -- generic template entity.
8331 Set_Associated_Entity (New_N, Empty);
8333 -- Special casing for stubs
8335 elsif Nkind (N) in N_Body_Stub then
8337 -- In any case, we must copy the specification or defining
8338 -- identifier as appropriate.
8340 if Nkind (N) = N_Subprogram_Body_Stub then
8341 Set_Specification (New_N,
8342 Copy_Generic_Node (Specification (N), New_N, Instantiating));
8344 else
8345 Set_Defining_Identifier (New_N,
8346 Copy_Generic_Node
8347 (Defining_Identifier (N), New_N, Instantiating));
8348 end if;
8350 -- If we are not instantiating, then this is where we load and
8351 -- analyze subunits, i.e. at the point where the stub occurs. A
8352 -- more permissive system might defer this analysis to the point
8353 -- of instantiation, but this seems too complicated for now.
8355 if not Instantiating then
8356 declare
8357 Subunit_Name : constant Unit_Name_Type := Get_Unit_Name (N);
8358 Subunit : Node_Id;
8359 Unum : Unit_Number_Type;
8360 New_Body : Node_Id;
8362 begin
8363 -- Make sure that, if it is a subunit of the main unit that is
8364 -- preprocessed and if -gnateG is specified, the preprocessed
8365 -- file will be written.
8367 Lib.Analysing_Subunit_Of_Main :=
8368 Lib.In_Extended_Main_Source_Unit (N);
8369 Unum :=
8370 Load_Unit
8371 (Load_Name => Subunit_Name,
8372 Required => False,
8373 Subunit => True,
8374 Error_Node => N);
8375 Lib.Analysing_Subunit_Of_Main := False;
8377 -- If the proper body is not found, a warning message will be
8378 -- emitted when analyzing the stub, or later at the point of
8379 -- instantiation. Here we just leave the stub as is.
8381 if Unum = No_Unit then
8382 Subunits_Missing := True;
8383 goto Subunit_Not_Found;
8384 end if;
8386 Subunit := Cunit (Unum);
8388 if Nkind (Unit (Subunit)) /= N_Subunit then
8389 Error_Msg_N
8390 ("found child unit instead of expected SEPARATE subunit",
8391 Subunit);
8392 Error_Msg_Sloc := Sloc (N);
8393 Error_Msg_N ("\to complete stub #", Subunit);
8394 goto Subunit_Not_Found;
8395 end if;
8397 -- We must create a generic copy of the subunit, in order to
8398 -- perform semantic analysis on it, and we must replace the
8399 -- stub in the original generic unit with the subunit, in order
8400 -- to preserve non-local references within.
8402 -- Only the proper body needs to be copied. Library_Unit and
8403 -- context clause are simply inherited by the generic copy.
8404 -- Note that the copy (which may be recursive if there are
8405 -- nested subunits) must be done first, before attaching it to
8406 -- the enclosing generic.
8408 New_Body :=
8409 Copy_Generic_Node
8410 (Proper_Body (Unit (Subunit)),
8411 Empty, Instantiating => False);
8413 -- Now place the original proper body in the original generic
8414 -- unit. This is a body, not a compilation unit.
8416 Rewrite (N, Proper_Body (Unit (Subunit)));
8417 Set_Is_Compilation_Unit (Defining_Entity (N), False);
8418 Set_Was_Originally_Stub (N);
8420 -- Finally replace the body of the subunit with its copy, and
8421 -- make this new subunit into the library unit of the generic
8422 -- copy, which does not have stubs any longer.
8424 Set_Proper_Body (Unit (Subunit), New_Body);
8425 Set_Library_Unit (New_N, Subunit);
8426 Inherit_Context (Unit (Subunit), N);
8427 end;
8429 -- If we are instantiating, this must be an error case, since
8430 -- otherwise we would have replaced the stub node by the proper body
8431 -- that corresponds. So just ignore it in the copy (i.e. we have
8432 -- copied it, and that is good enough).
8434 else
8435 null;
8436 end if;
8438 <<Subunit_Not_Found>> null;
8440 -- If the node is a compilation unit, it is the subunit of a stub, which
8441 -- has been loaded already (see code below). In this case, the library
8442 -- unit field of N points to the parent unit (which is a compilation
8443 -- unit) and need not (and cannot) be copied.
8445 -- When the proper body of the stub is analyzed, the library_unit link
8446 -- is used to establish the proper context (see sem_ch10).
8448 -- The other fields of a compilation unit are copied as usual
8450 elsif Nkind (N) = N_Compilation_Unit then
8452 -- This code can only be executed when not instantiating, because in
8453 -- the copy made for an instantiation, the compilation unit node has
8454 -- disappeared at the point that a stub is replaced by its proper
8455 -- body.
8457 pragma Assert (not Instantiating);
8459 Set_Context_Items (New_N,
8460 Copy_Generic_List (Context_Items (N), New_N));
8462 Set_Unit (New_N,
8463 Copy_Generic_Node (Unit (N), New_N, Instantiating => False));
8465 Set_First_Inlined_Subprogram (New_N,
8466 Copy_Generic_Node
8467 (First_Inlined_Subprogram (N), New_N, Instantiating => False));
8469 Set_Aux_Decls_Node
8470 (New_N,
8471 Copy_Generic_Node
8472 (Aux_Decls_Node (N), New_N, Instantiating => False));
8474 -- For an assignment node, the assignment is known to be semantically
8475 -- legal if we are instantiating the template. This avoids incorrect
8476 -- diagnostics in generated code.
8478 elsif Nkind (N) = N_Assignment_Statement then
8480 -- Copy name and expression fields in usual manner
8482 Set_Name (New_N,
8483 Copy_Generic_Node (Name (N), New_N, Instantiating));
8485 Set_Expression (New_N,
8486 Copy_Generic_Node (Expression (N), New_N, Instantiating));
8488 if Instantiating then
8489 Set_Assignment_OK (Name (New_N), True);
8490 end if;
8492 elsif Nkind (N) in N_Aggregate | N_Extension_Aggregate then
8493 if not Instantiating then
8494 Set_Associated_Node (N, New_N);
8496 else
8497 if Present (Get_Associated_Node (N))
8498 and then Nkind (Get_Associated_Node (N)) = Nkind (N)
8499 then
8500 -- In the generic the aggregate has some composite type. If at
8501 -- the point of instantiation the type has a private view,
8502 -- install the full view (and that of its ancestors, if any).
8504 declare
8505 T : Entity_Id := (Etype (Get_Associated_Node (New_N)));
8506 Rt : Entity_Id;
8508 begin
8509 if Present (T) and then Is_Private_Type (T) then
8510 Switch_View (T);
8511 end if;
8513 if Present (T)
8514 and then Is_Tagged_Type (T)
8515 and then Is_Derived_Type (T)
8516 then
8517 Rt := Root_Type (T);
8519 loop
8520 T := Etype (T);
8522 if Is_Private_Type (T) then
8523 Switch_View (T);
8524 end if;
8526 exit when T = Rt;
8527 end loop;
8528 end if;
8529 end;
8530 end if;
8531 end if;
8533 -- Do not copy the associated node, which points to the generic copy
8534 -- of the aggregate.
8536 if Nkind (N) = N_Aggregate then
8537 Set_Aggregate_Bounds
8538 (New_N,
8539 Node_Id (Copy_Generic_Descendant
8540 (Union_Id (Aggregate_Bounds (N)))));
8542 elsif Nkind (N) = N_Extension_Aggregate then
8543 Set_Ancestor_Part
8544 (New_N,
8545 Node_Id (Copy_Generic_Descendant
8546 (Union_Id (Ancestor_Part (N)))));
8548 else
8549 pragma Assert (False);
8550 end if;
8552 Set_Expressions
8553 (New_N,
8554 List_Id (Copy_Generic_Descendant (Union_Id (Expressions (N)))));
8555 Set_Component_Associations
8556 (New_N,
8557 List_Id (Copy_Generic_Descendant
8558 (Union_Id (Component_Associations (N)))));
8559 Set_Etype
8560 (New_N, Node_Id (Copy_Generic_Descendant (Union_Id (Etype (N)))));
8562 -- Allocators do not have an identifier denoting the access type, so we
8563 -- must locate it through the expression to check whether the views are
8564 -- consistent.
8566 elsif Nkind (N) = N_Allocator
8567 and then Nkind (Expression (N)) = N_Qualified_Expression
8568 and then Is_Entity_Name (Subtype_Mark (Expression (N)))
8569 and then Instantiating
8570 then
8571 declare
8572 T : constant Node_Id :=
8573 Get_Associated_Node (Subtype_Mark (Expression (N)));
8574 Acc_T : Entity_Id;
8576 begin
8577 if Present (T) then
8579 -- Retrieve the allocator node in the generic copy
8581 Acc_T := Etype (Parent (Parent (T)));
8583 if Present (Acc_T) and then Is_Private_Type (Acc_T) then
8584 Switch_View (Acc_T);
8585 end if;
8586 end if;
8588 Copy_Descendants;
8589 end;
8591 -- For a proper body, we must catch the case of a proper body that
8592 -- replaces a stub. This represents the point at which a separate
8593 -- compilation unit, and hence template file, may be referenced, so we
8594 -- must make a new source instantiation entry for the template of the
8595 -- subunit, and ensure that all nodes in the subunit are adjusted using
8596 -- this new source instantiation entry.
8598 elsif Nkind (N) in N_Proper_Body then
8599 declare
8600 Save_Adjustment : constant Sloc_Adjustment := S_Adjustment;
8601 begin
8602 if Instantiating and then Was_Originally_Stub (N) then
8603 Create_Instantiation_Source
8604 (Instantiation_Node,
8605 Defining_Entity (N),
8606 S_Adjustment);
8608 Adjust_Instantiation_Sloc (New_N, S_Adjustment);
8609 end if;
8611 -- Now copy the fields of the proper body, using the new
8612 -- adjustment factor if one was needed as per test above.
8614 Copy_Descendants;
8616 -- Restore the original adjustment factor
8618 S_Adjustment := Save_Adjustment;
8619 end;
8621 elsif Nkind (N) = N_Pragma and then Instantiating then
8623 -- Do not copy Comment or Ident pragmas their content is relevant to
8624 -- the generic unit, not to the instantiating unit.
8626 if Pragma_Name_Unmapped (N) in Name_Comment | Name_Ident then
8627 New_N := Make_Null_Statement (Sloc (N));
8629 -- Do not copy pragmas generated from aspects because the pragmas do
8630 -- not carry any semantic information, plus they will be regenerated
8631 -- in the instance.
8633 -- However, generating C we need to copy them since postconditions
8634 -- are inlined by the front end, and the front-end inlining machinery
8635 -- relies on this routine to perform inlining.
8637 elsif From_Aspect_Specification (N)
8638 and then not Modify_Tree_For_C
8639 then
8640 New_N := Make_Null_Statement (Sloc (N));
8642 else
8643 Copy_Descendants;
8644 end if;
8646 elsif Nkind (N) in N_Integer_Literal | N_Real_Literal then
8648 -- No descendant fields need traversing
8650 null;
8652 elsif Nkind (N) = N_String_Literal
8653 and then Present (Etype (N))
8654 and then Instantiating
8655 then
8656 -- If the string is declared in an outer scope, the string_literal
8657 -- subtype created for it may have the wrong scope. Force reanalysis
8658 -- of the constant to generate a new itype in the proper context.
8660 Set_Etype (New_N, Empty);
8661 Set_Analyzed (New_N, False);
8663 -- For the remaining nodes, copy their descendants recursively
8665 else
8666 Copy_Descendants;
8668 if Instantiating and then Nkind (N) = N_Subprogram_Body then
8669 Set_Generic_Parent (Specification (New_N), N);
8671 -- Should preserve Corresponding_Spec??? (12.3(14))
8672 end if;
8673 end if;
8675 -- Propagate dimensions if present, so that they are reflected in the
8676 -- instance.
8678 if Nkind (N) in N_Has_Etype
8679 and then (Nkind (N) in N_Op or else Is_Entity_Name (N))
8680 and then Present (Etype (N))
8681 and then Is_Floating_Point_Type (Etype (N))
8682 and then Has_Dimension_System (Etype (N))
8683 then
8684 Copy_Dimensions (N, New_N);
8685 end if;
8687 return New_N;
8688 end Copy_Generic_Node;
8690 ----------------------------
8691 -- Denotes_Formal_Package --
8692 ----------------------------
8694 function Denotes_Formal_Package
8695 (Pack : Entity_Id;
8696 On_Exit : Boolean := False;
8697 Instance : Entity_Id := Empty) return Boolean
8699 Par : Entity_Id;
8700 Scop : constant Entity_Id := Scope (Pack);
8701 E : Entity_Id;
8703 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean;
8704 -- The package in question may be an actual for a previous formal
8705 -- package P of the current instance, so examine its actuals as well.
8706 -- This must be recursive over other formal packages.
8708 ----------------------------------
8709 -- Is_Actual_Of_Previous_Formal --
8710 ----------------------------------
8712 function Is_Actual_Of_Previous_Formal (P : Entity_Id) return Boolean is
8713 E1 : Entity_Id;
8715 begin
8716 E1 := First_Entity (P);
8717 while Present (E1) and then E1 /= Instance loop
8718 if Ekind (E1) = E_Package
8719 and then Nkind (Parent (E1)) = N_Package_Renaming_Declaration
8720 then
8721 if Renamed_Entity (E1) = Pack then
8722 return True;
8724 elsif E1 = P or else Renamed_Entity (E1) = P then
8725 return False;
8727 elsif Is_Actual_Of_Previous_Formal (E1) then
8728 return True;
8729 end if;
8730 end if;
8732 Next_Entity (E1);
8733 end loop;
8735 return False;
8736 end Is_Actual_Of_Previous_Formal;
8738 -- Start of processing for Denotes_Formal_Package
8740 begin
8741 if On_Exit then
8742 Par :=
8743 Instance_Envs.Table
8744 (Instance_Envs.Last).Instantiated_Parent.Act_Id;
8745 else
8746 Par := Current_Instantiated_Parent.Act_Id;
8747 end if;
8749 if Ekind (Scop) = E_Generic_Package
8750 or else Nkind (Unit_Declaration_Node (Scop)) =
8751 N_Generic_Subprogram_Declaration
8752 then
8753 return True;
8755 elsif Nkind (Original_Node (Unit_Declaration_Node (Pack))) =
8756 N_Formal_Package_Declaration
8757 then
8758 return True;
8760 elsif No (Par) then
8761 return False;
8763 else
8764 -- Check whether this package is associated with a formal package of
8765 -- the enclosing instantiation. Iterate over the list of renamings.
8767 E := First_Entity (Par);
8768 while Present (E) loop
8769 if Ekind (E) /= E_Package
8770 or else Nkind (Parent (E)) /= N_Package_Renaming_Declaration
8771 then
8772 null;
8774 elsif Renamed_Entity (E) = Par then
8775 return False;
8777 elsif Renamed_Entity (E) = Pack then
8778 return True;
8780 elsif Is_Actual_Of_Previous_Formal (E) then
8781 return True;
8783 end if;
8785 Next_Entity (E);
8786 end loop;
8788 return False;
8789 end if;
8790 end Denotes_Formal_Package;
8792 -----------------
8793 -- End_Generic --
8794 -----------------
8796 procedure End_Generic is
8797 begin
8798 -- ??? More things could be factored out in this routine. Should
8799 -- probably be done at a later stage.
8801 Inside_A_Generic := Generic_Flags.Table (Generic_Flags.Last);
8802 Generic_Flags.Decrement_Last;
8804 Expander_Mode_Restore;
8805 end End_Generic;
8807 -------------
8808 -- Earlier --
8809 -------------
8811 function Earlier (N1, N2 : Node_Id) return Boolean is
8812 procedure Find_Depth (P : in out Node_Id; D : in out Integer);
8813 -- Find distance from given node to enclosing compilation unit
8815 ----------------
8816 -- Find_Depth --
8817 ----------------
8819 procedure Find_Depth (P : in out Node_Id; D : in out Integer) is
8820 begin
8821 while Present (P)
8822 and then Nkind (P) /= N_Compilation_Unit
8823 loop
8824 P := True_Parent (P);
8825 D := D + 1;
8826 end loop;
8827 end Find_Depth;
8829 -- Local declarations
8831 D1 : Integer := 0;
8832 D2 : Integer := 0;
8833 P1 : Node_Id := N1;
8834 P2 : Node_Id := N2;
8835 T1 : Source_Ptr;
8836 T2 : Source_Ptr;
8838 -- Start of processing for Earlier
8840 begin
8841 Find_Depth (P1, D1);
8842 Find_Depth (P2, D2);
8844 if P1 /= P2 then
8845 return False;
8846 else
8847 P1 := N1;
8848 P2 := N2;
8849 end if;
8851 while D1 > D2 loop
8852 P1 := True_Parent (P1);
8853 D1 := D1 - 1;
8854 end loop;
8856 while D2 > D1 loop
8857 P2 := True_Parent (P2);
8858 D2 := D2 - 1;
8859 end loop;
8861 -- At this point P1 and P2 are at the same distance from the root.
8862 -- We examine their parents until we find a common declarative list.
8863 -- If we reach the root, N1 and N2 do not descend from the same
8864 -- declarative list (e.g. one is nested in the declarative part and
8865 -- the other is in a block in the statement part) and the earlier
8866 -- one is already frozen.
8868 while not Is_List_Member (P1)
8869 or else not Is_List_Member (P2)
8870 or else not In_Same_List (P1, P2)
8871 loop
8872 P1 := True_Parent (P1);
8873 P2 := True_Parent (P2);
8875 if Nkind (Parent (P1)) = N_Subunit then
8876 P1 := Corresponding_Stub (Parent (P1));
8877 end if;
8879 if Nkind (Parent (P2)) = N_Subunit then
8880 P2 := Corresponding_Stub (Parent (P2));
8881 end if;
8883 if P1 = P2 then
8884 return False;
8885 end if;
8886 end loop;
8888 -- Expanded code usually shares the source location of the original
8889 -- construct it was generated for. This however may not necessarily
8890 -- reflect the true location of the code within the tree.
8892 -- Before comparing the slocs of the two nodes, make sure that we are
8893 -- working with correct source locations. Assume that P1 is to the left
8894 -- of P2. If either one does not come from source, traverse the common
8895 -- list heading towards the other node and locate the first source
8896 -- statement.
8898 -- P1 P2
8899 -- ----+===+===+--------------+===+===+----
8900 -- expanded code expanded code
8902 if not Comes_From_Source (P1) then
8903 while Present (P1) loop
8905 -- Neither P2 nor a source statement were located during the
8906 -- search. If we reach the end of the list, then P1 does not
8907 -- occur earlier than P2.
8909 -- ---->
8910 -- start --- P2 ----- P1 --- end
8912 if No (Next (P1)) then
8913 return False;
8915 -- We encounter P2 while going to the right of the list. This
8916 -- means that P1 does indeed appear earlier.
8918 -- ---->
8919 -- start --- P1 ===== P2 --- end
8920 -- expanded code in between
8922 elsif P1 = P2 then
8923 return True;
8925 -- No need to look any further since we have located a source
8926 -- statement.
8928 elsif Comes_From_Source (P1) then
8929 exit;
8930 end if;
8932 -- Keep going right
8934 Next (P1);
8935 end loop;
8936 end if;
8938 if not Comes_From_Source (P2) then
8939 while Present (P2) loop
8941 -- Neither P1 nor a source statement were located during the
8942 -- search. If we reach the start of the list, then P1 does not
8943 -- occur earlier than P2.
8945 -- <----
8946 -- start --- P2 --- P1 --- end
8948 if No (Prev (P2)) then
8949 return False;
8951 -- We encounter P1 while going to the left of the list. This
8952 -- means that P1 does indeed appear earlier.
8954 -- <----
8955 -- start --- P1 ===== P2 --- end
8956 -- expanded code in between
8958 elsif P2 = P1 then
8959 return True;
8961 -- No need to look any further since we have located a source
8962 -- statement.
8964 elsif Comes_From_Source (P2) then
8965 exit;
8966 end if;
8968 -- Keep going left
8970 Prev (P2);
8971 end loop;
8972 end if;
8974 -- At this point either both nodes came from source or we approximated
8975 -- their source locations through neighboring source statements.
8977 T1 := Top_Level_Location (Sloc (P1));
8978 T2 := Top_Level_Location (Sloc (P2));
8980 -- When two nodes come from the same instance, they have identical top
8981 -- level locations. To determine proper relation within the tree, check
8982 -- their locations within the template.
8984 if T1 = T2 then
8985 return Sloc (P1) < Sloc (P2);
8987 -- The two nodes either come from unrelated instances or do not come
8988 -- from instantiated code at all.
8990 else
8991 return T1 < T2;
8992 end if;
8993 end Earlier;
8995 ----------------------
8996 -- Find_Actual_Type --
8997 ----------------------
8999 function Find_Actual_Type
9000 (Typ : Entity_Id;
9001 Gen_Type : Entity_Id) return Entity_Id
9003 Gen_Scope : constant Entity_Id := Scope (Gen_Type);
9004 T : Entity_Id;
9006 begin
9007 -- Special processing only applies to child units
9009 if not Is_Child_Unit (Gen_Scope) then
9010 return Get_Instance_Of (Typ);
9012 -- If designated or component type is itself a formal of the child unit,
9013 -- its instance is available.
9015 elsif Scope (Typ) = Gen_Scope then
9016 return Get_Instance_Of (Typ);
9018 -- If the array or access type is not declared in the parent unit,
9019 -- no special processing needed.
9021 elsif not Is_Generic_Type (Typ)
9022 and then Scope (Gen_Scope) /= Scope (Typ)
9023 then
9024 return Get_Instance_Of (Typ);
9026 -- Otherwise, retrieve designated or component type by visibility
9028 else
9029 T := Current_Entity (Typ);
9030 while Present (T) loop
9031 if In_Open_Scopes (Scope (T)) then
9032 return T;
9033 elsif Is_Generic_Actual_Type (T) then
9034 return T;
9035 end if;
9037 T := Homonym (T);
9038 end loop;
9040 return Typ;
9041 end if;
9042 end Find_Actual_Type;
9044 -----------------------------
9045 -- Freeze_Package_Instance --
9046 -----------------------------
9048 procedure Freeze_Package_Instance
9049 (N : Node_Id;
9050 Gen_Body : Node_Id;
9051 Gen_Decl : Node_Id;
9052 Act_Id : Entity_Id)
9054 function In_Same_Scope (Gen_Id, Act_Id : Node_Id) return Boolean;
9055 -- Check if the generic definition and the instantiation come from
9056 -- a common scope, in which case the instance must be frozen after
9057 -- the generic body.
9059 function True_Sloc (N, Act_Unit : Node_Id) return Source_Ptr;
9060 -- If the instance is nested inside a generic unit, the Sloc of the
9061 -- instance indicates the place of the original definition, not the
9062 -- point of the current enclosing instance. Pending a better usage of
9063 -- Slocs to indicate instantiation places, we determine the place of
9064 -- origin of a node by finding the maximum sloc of any ancestor node.
9066 -- Why is this not equivalent to Top_Level_Location ???
9068 -------------------
9069 -- In_Same_Scope --
9070 -------------------
9072 function In_Same_Scope (Gen_Id, Act_Id : Node_Id) return Boolean is
9073 Act_Scop : Entity_Id := Scope (Act_Id);
9074 Gen_Scop : Entity_Id := Scope (Gen_Id);
9076 begin
9077 while Act_Scop /= Standard_Standard
9078 and then Gen_Scop /= Standard_Standard
9079 loop
9080 if Act_Scop = Gen_Scop then
9081 return True;
9082 end if;
9084 Act_Scop := Scope (Act_Scop);
9085 Gen_Scop := Scope (Gen_Scop);
9086 end loop;
9088 return False;
9089 end In_Same_Scope;
9091 ---------------
9092 -- True_Sloc --
9093 ---------------
9095 function True_Sloc (N, Act_Unit : Node_Id) return Source_Ptr is
9096 N1 : Node_Id;
9097 Res : Source_Ptr;
9099 begin
9100 Res := Sloc (N);
9101 N1 := N;
9102 while Present (N1) and then N1 /= Act_Unit loop
9103 if Sloc (N1) > Res then
9104 Res := Sloc (N1);
9105 end if;
9107 N1 := Parent (N1);
9108 end loop;
9110 return Res;
9111 end True_Sloc;
9113 -- Local variables
9115 Gen_Id : constant Entity_Id := Get_Generic_Entity (N);
9116 Par_Id : constant Entity_Id := Scope (Gen_Id);
9117 Act_Unit : constant Node_Id := Unit (Cunit (Get_Source_Unit (N)));
9118 Gen_Unit : constant Node_Id :=
9119 Unit (Cunit (Get_Source_Unit (Gen_Decl)));
9121 Body_Unit : Node_Id;
9122 F_Node : Node_Id;
9123 Must_Delay : Boolean;
9124 Orig_Body : Node_Id;
9126 -- Start of processing for Freeze_Package_Instance
9128 begin
9129 -- If the body is a subunit, the freeze point is the corresponding stub
9130 -- in the current compilation, not the subunit itself.
9132 if Nkind (Parent (Gen_Body)) = N_Subunit then
9133 Orig_Body := Corresponding_Stub (Parent (Gen_Body));
9134 else
9135 Orig_Body := Gen_Body;
9136 end if;
9138 Body_Unit := Unit (Cunit (Get_Source_Unit (Orig_Body)));
9140 -- If the instantiation and the generic definition appear in the same
9141 -- package declaration, this is an early instantiation. If they appear
9142 -- in the same declarative part, it is an early instantiation only if
9143 -- the generic body appears textually later, and the generic body is
9144 -- also in the main unit.
9146 -- If instance is nested within a subprogram, and the generic body
9147 -- is not, the instance is delayed because the enclosing body is. If
9148 -- instance and body are within the same scope, or the same subprogram
9149 -- body, indicate explicitly that the instance is delayed.
9151 Must_Delay :=
9152 (Gen_Unit = Act_Unit
9153 and then (Nkind (Gen_Unit) in N_Generic_Package_Declaration
9154 | N_Package_Declaration
9155 or else (Gen_Unit = Body_Unit
9156 and then
9157 True_Sloc (N, Act_Unit) < Sloc (Orig_Body)))
9158 and then Is_In_Main_Unit (Original_Node (Gen_Unit))
9159 and then In_Same_Scope (Gen_Id, Act_Id));
9161 -- If this is an early instantiation, the freeze node is placed after
9162 -- the generic body. Otherwise, if the generic appears in an instance,
9163 -- we cannot freeze the current instance until the outer one is frozen.
9164 -- This is only relevant if the current instance is nested within some
9165 -- inner scope not itself within the outer instance. If this scope is
9166 -- a package body in the same declarative part as the outer instance,
9167 -- then that body needs to be frozen after the outer instance. Finally,
9168 -- if no delay is needed, we place the freeze node at the end of the
9169 -- current declarative part.
9171 if No (Freeze_Node (Act_Id))
9172 or else not Is_List_Member (Freeze_Node (Act_Id))
9173 then
9174 Ensure_Freeze_Node (Act_Id);
9175 F_Node := Freeze_Node (Act_Id);
9177 if Must_Delay then
9178 Insert_After (Orig_Body, F_Node);
9180 elsif Is_Generic_Instance (Par_Id)
9181 and then Present (Freeze_Node (Par_Id))
9182 and then Scope (Act_Id) /= Par_Id
9183 then
9184 -- Freeze instance of inner generic after instance of enclosing
9185 -- generic.
9187 if In_Same_Declarative_Part (Parent (Freeze_Node (Par_Id)), N) then
9189 -- Handle the following case:
9191 -- package Parent_Inst is new ...
9192 -- freeze Parent_Inst []
9194 -- procedure P ... -- this body freezes Parent_Inst
9196 -- package Inst is new ...
9198 -- In this particular scenario, the freeze node for Inst must
9199 -- be inserted in the same manner as that of Parent_Inst,
9200 -- before the next source body or at the end of the declarative
9201 -- list (body not available). If body P did not exist and
9202 -- Parent_Inst was frozen after Inst, either by a body
9203 -- following Inst or at the end of the declarative region,
9204 -- the freeze node for Inst must be inserted after that of
9205 -- Parent_Inst. This relation is established by comparing
9206 -- the Slocs of Parent_Inst freeze node and Inst.
9207 -- We examine the parents of the enclosing lists to handle
9208 -- the case where the parent instance is in the visible part
9209 -- of a package declaration, and the inner instance is in
9210 -- the corresponding private part.
9212 if Parent (List_Containing (Freeze_Node (Par_Id)))
9213 = Parent (List_Containing (N))
9214 and then Sloc (Freeze_Node (Par_Id)) <= Sloc (N)
9215 then
9216 Insert_Freeze_Node_For_Instance (N, F_Node);
9217 else
9218 Insert_After (Freeze_Node (Par_Id), F_Node);
9219 end if;
9221 -- Freeze package enclosing instance of inner generic after
9222 -- instance of enclosing generic.
9224 elsif Nkind (Parent (N)) in N_Package_Body | N_Subprogram_Body
9225 and then In_Same_Declarative_Part
9226 (Parent (Freeze_Node (Par_Id)), Parent (N))
9227 then
9228 declare
9229 Enclosing : Entity_Id;
9231 begin
9232 Enclosing := Corresponding_Spec (Parent (N));
9234 if No (Enclosing) then
9235 Enclosing := Defining_Entity (Parent (N));
9236 end if;
9238 Insert_Freeze_Node_For_Instance (N, F_Node);
9239 Ensure_Freeze_Node (Enclosing);
9241 if not Is_List_Member (Freeze_Node (Enclosing)) then
9243 -- The enclosing context is a subunit, insert the freeze
9244 -- node after the stub.
9246 if Nkind (Parent (Parent (N))) = N_Subunit then
9247 Insert_Freeze_Node_For_Instance
9248 (Corresponding_Stub (Parent (Parent (N))),
9249 Freeze_Node (Enclosing));
9251 -- The enclosing context is a package with a stub body
9252 -- which has already been replaced by the real body.
9253 -- Insert the freeze node after the actual body.
9255 elsif Ekind (Enclosing) = E_Package
9256 and then Present (Body_Entity (Enclosing))
9257 and then Was_Originally_Stub
9258 (Parent (Body_Entity (Enclosing)))
9259 then
9260 Insert_Freeze_Node_For_Instance
9261 (Parent (Body_Entity (Enclosing)),
9262 Freeze_Node (Enclosing));
9264 -- The parent instance has been frozen before the body of
9265 -- the enclosing package, insert the freeze node after
9266 -- the body.
9268 elsif In_Same_List (Freeze_Node (Par_Id), Parent (N))
9269 and then
9270 Sloc (Freeze_Node (Par_Id)) <= Sloc (Parent (N))
9271 then
9272 Insert_Freeze_Node_For_Instance
9273 (Parent (N), Freeze_Node (Enclosing));
9275 else
9276 Insert_After
9277 (Freeze_Node (Par_Id), Freeze_Node (Enclosing));
9278 end if;
9279 end if;
9280 end;
9282 else
9283 Insert_Freeze_Node_For_Instance (N, F_Node);
9284 end if;
9286 else
9287 Insert_Freeze_Node_For_Instance (N, F_Node);
9288 end if;
9289 end if;
9290 end Freeze_Package_Instance;
9292 --------------------------------
9293 -- Freeze_Subprogram_Instance --
9294 --------------------------------
9296 procedure Freeze_Subprogram_Instance
9297 (N : Node_Id;
9298 Gen_Body : Node_Id;
9299 Pack_Id : Entity_Id)
9301 function Enclosing_Package_Body (N : Node_Id) return Node_Id;
9302 -- Find innermost package body that encloses the given node, and which
9303 -- is not a compilation unit. Freeze nodes for the instance, or for its
9304 -- enclosing body, may be inserted after the enclosing_body of the
9305 -- generic unit. Used to determine proper placement of freeze node for
9306 -- both package and subprogram instances.
9308 function Package_Freeze_Node (B : Node_Id) return Node_Id;
9309 -- Find entity for given package body, and locate or create a freeze
9310 -- node for it.
9312 ----------------------------
9313 -- Enclosing_Package_Body --
9314 ----------------------------
9316 function Enclosing_Package_Body (N : Node_Id) return Node_Id is
9317 P : Node_Id;
9319 begin
9320 P := Parent (N);
9321 while Present (P)
9322 and then Nkind (Parent (P)) /= N_Compilation_Unit
9323 loop
9324 if Nkind (P) = N_Package_Body then
9325 if Nkind (Parent (P)) = N_Subunit then
9326 return Corresponding_Stub (Parent (P));
9327 else
9328 return P;
9329 end if;
9330 end if;
9332 P := True_Parent (P);
9333 end loop;
9335 return Empty;
9336 end Enclosing_Package_Body;
9338 -------------------------
9339 -- Package_Freeze_Node --
9340 -------------------------
9342 function Package_Freeze_Node (B : Node_Id) return Node_Id is
9343 Id : Entity_Id;
9345 begin
9346 if Nkind (B) = N_Package_Body then
9347 Id := Corresponding_Spec (B);
9348 else pragma Assert (Nkind (B) = N_Package_Body_Stub);
9349 Id := Corresponding_Spec (Proper_Body (Unit (Library_Unit (B))));
9350 end if;
9352 Ensure_Freeze_Node (Id);
9353 return Freeze_Node (Id);
9354 end Package_Freeze_Node;
9356 -- Local variables
9358 Enc_G : constant Node_Id := Enclosing_Package_Body (Gen_Body);
9359 Enc_N : constant Node_Id := Enclosing_Package_Body (N);
9360 Par_Id : constant Entity_Id := Scope (Get_Generic_Entity (N));
9362 Enc_G_F : Node_Id;
9363 F_Node : Node_Id;
9365 -- Start of processing for Freeze_Subprogram_Instance
9367 begin
9368 -- If the instance and the generic body appear within the same unit, and
9369 -- the instance precedes the generic, the freeze node for the instance
9370 -- must appear after that of the generic. If the generic is nested
9371 -- within another instance I2, then current instance must be frozen
9372 -- after I2. In both cases, the freeze nodes are those of enclosing
9373 -- packages. Otherwise, the freeze node is placed at the end of the
9374 -- current declarative part.
9376 Ensure_Freeze_Node (Pack_Id);
9377 F_Node := Freeze_Node (Pack_Id);
9379 if Is_Generic_Instance (Par_Id)
9380 and then Present (Freeze_Node (Par_Id))
9381 and then In_Same_Declarative_Part (Parent (Freeze_Node (Par_Id)), N)
9382 then
9383 -- The parent was a premature instantiation. Insert freeze node at
9384 -- the end the current declarative part.
9386 if Is_Known_Guaranteed_ABE (Get_Unit_Instantiation_Node (Par_Id)) then
9387 Insert_Freeze_Node_For_Instance (N, F_Node);
9389 -- Handle the following case:
9391 -- package Parent_Inst is new ...
9392 -- freeze Parent_Inst []
9394 -- procedure P ... -- this body freezes Parent_Inst
9396 -- procedure Inst is new ...
9398 -- In this particular scenario, the freeze node for Inst must be
9399 -- inserted in the same manner as that of Parent_Inst - before the
9400 -- next source body or at the end of the declarative list (body not
9401 -- available). If body P did not exist and Parent_Inst was frozen
9402 -- after Inst, either by a body following Inst or at the end of the
9403 -- declarative region, the freeze node for Inst must be inserted
9404 -- after that of Parent_Inst. This relation is established by
9405 -- comparing the Slocs of Parent_Inst freeze node and Inst.
9407 elsif In_Same_List (Freeze_Node (Par_Id), N)
9408 and then Sloc (Freeze_Node (Par_Id)) <= Sloc (N)
9409 then
9410 Insert_Freeze_Node_For_Instance (N, F_Node);
9412 else
9413 Insert_After (Freeze_Node (Par_Id), F_Node);
9414 end if;
9416 -- The body enclosing the instance should be frozen after the body that
9417 -- includes the generic, because the body of the instance may make
9418 -- references to entities therein. If the two are not in the same
9419 -- declarative part, or if the one enclosing the instance is frozen
9420 -- already, freeze the instance at the end of the current declarative
9421 -- part.
9423 elsif Is_Generic_Instance (Par_Id)
9424 and then Present (Freeze_Node (Par_Id))
9425 and then Present (Enc_N)
9426 then
9427 if In_Same_Declarative_Part (Parent (Freeze_Node (Par_Id)), Enc_N)
9428 then
9429 -- The enclosing package may contain several instances. Rather
9430 -- than computing the earliest point at which to insert its freeze
9431 -- node, we place it at the end of the declarative part of the
9432 -- parent of the generic.
9434 Insert_Freeze_Node_For_Instance
9435 (Freeze_Node (Par_Id), Package_Freeze_Node (Enc_N));
9436 end if;
9438 Insert_Freeze_Node_For_Instance (N, F_Node);
9440 elsif Present (Enc_G)
9441 and then Present (Enc_N)
9442 and then Enc_G /= Enc_N
9443 and then Earlier (N, Gen_Body)
9444 then
9445 -- Freeze package that encloses instance, and place node after the
9446 -- package that encloses generic. If enclosing package is already
9447 -- frozen we have to assume it is at the proper place. This may be a
9448 -- potential ABE that requires dynamic checking. Do not add a freeze
9449 -- node if the package that encloses the generic is inside the body
9450 -- that encloses the instance, because the freeze node would be in
9451 -- the wrong scope. Additional contortions needed if the bodies are
9452 -- within a subunit.
9454 declare
9455 Enclosing_Body : Node_Id;
9457 begin
9458 if Nkind (Enc_N) = N_Package_Body_Stub then
9459 Enclosing_Body := Proper_Body (Unit (Library_Unit (Enc_N)));
9460 else
9461 Enclosing_Body := Enc_N;
9462 end if;
9464 if Parent (List_Containing (Enc_G)) /= Enclosing_Body then
9465 Insert_Freeze_Node_For_Instance
9466 (Enc_G, Package_Freeze_Node (Enc_N));
9467 end if;
9468 end;
9470 -- Freeze enclosing subunit before instance
9472 Enc_G_F := Package_Freeze_Node (Enc_G);
9474 if not Is_List_Member (Enc_G_F) then
9475 Insert_After (Enc_G, Enc_G_F);
9476 end if;
9478 Insert_Freeze_Node_For_Instance (N, F_Node);
9480 else
9481 -- If none of the above, insert freeze node at the end of the current
9482 -- declarative part.
9484 Insert_Freeze_Node_For_Instance (N, F_Node);
9485 end if;
9486 end Freeze_Subprogram_Instance;
9488 ----------------
9489 -- Get_Gen_Id --
9490 ----------------
9492 function Get_Gen_Id (E : Assoc_Ptr) return Entity_Id is
9493 begin
9494 return Generic_Renamings.Table (E).Gen_Id;
9495 end Get_Gen_Id;
9497 ---------------------
9498 -- Get_Instance_Of --
9499 ---------------------
9501 function Get_Instance_Of (A : Entity_Id) return Entity_Id is
9502 Res : constant Assoc_Ptr := Generic_Renamings_HTable.Get (A);
9504 begin
9505 if Res /= Assoc_Null then
9506 return Generic_Renamings.Table (Res).Act_Id;
9508 else
9509 -- On exit, entity is not instantiated: not a generic parameter, or
9510 -- else parameter of an inner generic unit.
9512 return A;
9513 end if;
9514 end Get_Instance_Of;
9516 ---------------------------------
9517 -- Get_Unit_Instantiation_Node --
9518 ---------------------------------
9520 function Get_Unit_Instantiation_Node (A : Entity_Id) return Node_Id is
9521 Decl : Node_Id := Unit_Declaration_Node (A);
9522 Inst : Node_Id;
9524 begin
9525 -- If the Package_Instantiation attribute has been set on the package
9526 -- entity, then use it directly when it (or its Original_Node) refers
9527 -- to an N_Package_Instantiation node. In principle it should be
9528 -- possible to have this field set in all cases, which should be
9529 -- investigated, and would allow this function to be significantly
9530 -- simplified. ???
9532 Inst := Package_Instantiation (A);
9534 if Present (Inst) then
9535 if Nkind (Inst) = N_Package_Instantiation then
9536 return Inst;
9538 elsif Nkind (Original_Node (Inst)) = N_Package_Instantiation then
9539 return Original_Node (Inst);
9540 end if;
9541 end if;
9543 -- If the instantiation is a compilation unit that does not need body
9544 -- then the instantiation node has been rewritten as a package
9545 -- declaration for the instance, and we return the original node.
9547 -- If it is a compilation unit and the instance node has not been
9548 -- rewritten, then it is still the unit of the compilation. Finally, if
9549 -- a body is present, this is a parent of the main unit whose body has
9550 -- been compiled for inlining purposes, and the instantiation node has
9551 -- been rewritten with the instance body.
9553 -- Otherwise the instantiation node appears after the declaration. If
9554 -- the entity is a formal package, the declaration may have been
9555 -- rewritten as a generic declaration (in the case of a formal with box)
9556 -- or left as a formal package declaration if it has actuals, and is
9557 -- found with a forward search.
9559 if Nkind (Parent (Decl)) = N_Compilation_Unit then
9560 if Nkind (Decl) = N_Package_Declaration
9561 and then Present (Corresponding_Body (Decl))
9562 then
9563 Decl := Unit_Declaration_Node (Corresponding_Body (Decl));
9564 end if;
9566 if Nkind (Original_Node (Decl)) in N_Generic_Instantiation then
9567 return Original_Node (Decl);
9568 else
9569 return Unit (Parent (Decl));
9570 end if;
9572 elsif Nkind (Decl) = N_Package_Declaration
9573 and then Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration
9574 then
9575 return Original_Node (Decl);
9577 else
9578 Inst := Next (Decl);
9579 while Nkind (Inst) not in N_Formal_Package_Declaration
9580 | N_Function_Instantiation
9581 | N_Package_Instantiation
9582 | N_Procedure_Instantiation
9583 loop
9584 Next (Inst);
9585 end loop;
9587 return Inst;
9588 end if;
9589 end Get_Unit_Instantiation_Node;
9591 ------------------------
9592 -- Has_Been_Exchanged --
9593 ------------------------
9595 function Has_Been_Exchanged (E : Entity_Id) return Boolean is
9596 Next : Elmt_Id;
9598 begin
9599 Next := First_Elmt (Exchanged_Views);
9600 while Present (Next) loop
9601 if Full_View (Node (Next)) = E then
9602 return True;
9603 end if;
9605 Next_Elmt (Next);
9606 end loop;
9608 return False;
9609 end Has_Been_Exchanged;
9611 -------------------
9612 -- Has_Contracts --
9613 -------------------
9615 function Has_Contracts (Decl : Node_Id) return Boolean is
9616 A_List : constant List_Id := Aspect_Specifications (Decl);
9617 A_Spec : Node_Id;
9618 A_Id : Aspect_Id;
9619 begin
9620 if No (A_List) then
9621 return False;
9622 else
9623 A_Spec := First (A_List);
9624 while Present (A_Spec) loop
9625 A_Id := Get_Aspect_Id (A_Spec);
9626 if A_Id = Aspect_Pre or else A_Id = Aspect_Post then
9627 return True;
9628 end if;
9630 Next (A_Spec);
9631 end loop;
9633 return False;
9634 end if;
9635 end Has_Contracts;
9637 ----------
9638 -- Hash --
9639 ----------
9641 function Hash (F : Entity_Id) return HTable_Range is
9642 begin
9643 return HTable_Range (F mod HTable_Size);
9644 end Hash;
9646 ------------------------
9647 -- Hide_Current_Scope --
9648 ------------------------
9650 procedure Hide_Current_Scope is
9651 C : constant Entity_Id := Current_Scope;
9652 E : Entity_Id;
9654 begin
9655 Set_Is_Hidden_Open_Scope (C);
9657 E := First_Entity (C);
9658 while Present (E) loop
9659 if Is_Immediately_Visible (E) then
9660 Set_Is_Immediately_Visible (E, False);
9661 Append_Elmt (E, Hidden_Entities);
9662 end if;
9664 Next_Entity (E);
9665 end loop;
9667 -- Make the scope name invisible as well. This is necessary, but might
9668 -- conflict with calls to Rtsfind later on, in case the scope is a
9669 -- predefined one. There is no clean solution to this problem, so for
9670 -- now we depend on the user not redefining Standard itself in one of
9671 -- the parent units.
9673 if Is_Immediately_Visible (C) and then C /= Standard_Standard then
9674 Set_Is_Immediately_Visible (C, False);
9675 Append_Elmt (C, Hidden_Entities);
9676 end if;
9678 end Hide_Current_Scope;
9680 --------------
9681 -- Init_Env --
9682 --------------
9684 procedure Init_Env is
9685 Saved : Instance_Env;
9687 begin
9688 Saved.Instantiated_Parent := Current_Instantiated_Parent;
9689 Saved.Exchanged_Views := Exchanged_Views;
9690 Saved.Hidden_Entities := Hidden_Entities;
9691 Saved.Current_Sem_Unit := Current_Sem_Unit;
9692 Saved.Parent_Unit_Visible := Parent_Unit_Visible;
9693 Saved.Instance_Parent_Unit := Instance_Parent_Unit;
9695 -- Save configuration switches. These may be reset if the unit is a
9696 -- predefined unit, and the current mode is not Ada 2005.
9698 Saved.Switches := Save_Config_Switches;
9700 Instance_Envs.Append (Saved);
9702 Exchanged_Views := New_Elmt_List;
9703 Hidden_Entities := New_Elmt_List;
9705 -- Make dummy entry for Instantiated parent. If generic unit is legal,
9706 -- this is set properly in Set_Instance_Env.
9708 Current_Instantiated_Parent :=
9709 (Current_Scope, Current_Scope, Assoc_Null);
9710 end Init_Env;
9712 ---------------------
9713 -- In_Main_Context --
9714 ---------------------
9716 function In_Main_Context (E : Entity_Id) return Boolean is
9717 Context : List_Id;
9718 Clause : Node_Id;
9719 Nam : Node_Id;
9721 begin
9722 if not Is_Compilation_Unit (E)
9723 or else Ekind (E) /= E_Package
9724 or else In_Private_Part (E)
9725 then
9726 return False;
9727 end if;
9729 Context := Context_Items (Cunit (Main_Unit));
9731 Clause := First (Context);
9732 while Present (Clause) loop
9733 if Nkind (Clause) = N_With_Clause then
9734 Nam := Name (Clause);
9736 -- If the current scope is part of the context of the main unit,
9737 -- analysis of the corresponding with_clause is not complete, and
9738 -- the entity is not set. We use the Chars field directly, which
9739 -- might produce false positives in rare cases, but guarantees
9740 -- that we produce all the instance bodies we will need.
9742 if (Is_Entity_Name (Nam) and then Chars (Nam) = Chars (E))
9743 or else (Nkind (Nam) = N_Selected_Component
9744 and then Chars (Selector_Name (Nam)) = Chars (E))
9745 then
9746 return True;
9747 end if;
9748 end if;
9750 Next (Clause);
9751 end loop;
9753 return False;
9754 end In_Main_Context;
9756 ---------------------
9757 -- Inherit_Context --
9758 ---------------------
9760 procedure Inherit_Context (Gen_Decl : Node_Id; Inst : Node_Id) is
9761 Current_Context : List_Id;
9762 Current_Unit : Node_Id;
9763 Item : Node_Id;
9764 New_I : Node_Id;
9766 Clause : Node_Id;
9767 OK : Boolean;
9768 Lib_Unit : Node_Id;
9770 begin
9771 if Nkind (Parent (Gen_Decl)) = N_Compilation_Unit then
9773 -- The inherited context is attached to the enclosing compilation
9774 -- unit. This is either the main unit, or the declaration for the
9775 -- main unit (in case the instantiation appears within the package
9776 -- declaration and the main unit is its body).
9778 Current_Unit := Parent (Inst);
9779 while Present (Current_Unit)
9780 and then Nkind (Current_Unit) /= N_Compilation_Unit
9781 loop
9782 Current_Unit := Parent (Current_Unit);
9783 end loop;
9785 Current_Context := Context_Items (Current_Unit);
9787 Item := First (Context_Items (Parent (Gen_Decl)));
9788 while Present (Item) loop
9789 if Nkind (Item) = N_With_Clause then
9790 Lib_Unit := Library_Unit (Item);
9792 -- Take care to prevent direct cyclic with's
9794 if Lib_Unit /= Current_Unit then
9796 -- Do not add a unit if it is already in the context
9798 Clause := First (Current_Context);
9799 OK := True;
9800 while Present (Clause) loop
9801 if Nkind (Clause) = N_With_Clause
9802 and then Library_Unit (Clause) = Lib_Unit
9803 then
9804 OK := False;
9805 exit;
9806 end if;
9808 Next (Clause);
9809 end loop;
9811 if OK then
9812 New_I := New_Copy (Item);
9813 Set_Implicit_With (New_I);
9815 Append (New_I, Current_Context);
9816 end if;
9817 end if;
9818 end if;
9820 Next (Item);
9821 end loop;
9822 end if;
9823 end Inherit_Context;
9825 ----------------
9826 -- Initialize --
9827 ----------------
9829 procedure Initialize is
9830 begin
9831 Generic_Renamings.Init;
9832 Instance_Envs.Init;
9833 Generic_Flags.Init;
9834 Generic_Renamings_HTable.Reset;
9835 Circularity_Detected := False;
9836 Exchanged_Views := No_Elist;
9837 Hidden_Entities := No_Elist;
9838 end Initialize;
9840 -------------------------------------
9841 -- Insert_Freeze_Node_For_Instance --
9842 -------------------------------------
9844 procedure Insert_Freeze_Node_For_Instance
9845 (N : Node_Id;
9846 F_Node : Node_Id)
9848 function Enclosing_Body (N : Node_Id) return Node_Id;
9849 -- Find enclosing package or subprogram body, if any. Freeze node may
9850 -- be placed at end of current declarative list if previous instance
9851 -- and current one have different enclosing bodies.
9853 function Previous_Instance (Gen : Entity_Id) return Entity_Id;
9854 -- Find the local instance, if any, that declares the generic that is
9855 -- being instantiated. If present, the freeze node for this instance
9856 -- must follow the freeze node for the previous instance.
9858 --------------------
9859 -- Enclosing_Body --
9860 --------------------
9862 function Enclosing_Body (N : Node_Id) return Node_Id is
9863 P : Node_Id;
9865 begin
9866 P := Parent (N);
9867 while Present (P)
9868 and then Nkind (Parent (P)) /= N_Compilation_Unit
9869 loop
9870 if Nkind (P) in N_Package_Body | N_Subprogram_Body then
9871 if Nkind (Parent (P)) = N_Subunit then
9872 return Corresponding_Stub (Parent (P));
9873 else
9874 return P;
9875 end if;
9876 end if;
9878 P := True_Parent (P);
9879 end loop;
9881 return Empty;
9882 end Enclosing_Body;
9884 -----------------------
9885 -- Previous_Instance --
9886 -----------------------
9888 function Previous_Instance (Gen : Entity_Id) return Entity_Id is
9889 S : Entity_Id;
9891 begin
9892 S := Scope (Gen);
9893 while Present (S) and then S /= Standard_Standard loop
9894 if Is_Generic_Instance (S)
9895 and then In_Same_Source_Unit (S, N)
9896 then
9897 return S;
9898 end if;
9900 S := Scope (S);
9901 end loop;
9903 return Empty;
9904 end Previous_Instance;
9906 -- Local variables
9908 Decl : Node_Id;
9909 Decls : List_Id;
9910 Inst : Entity_Id;
9911 Par_Inst : Node_Id;
9912 Par_N : Node_Id;
9914 -- Start of processing for Insert_Freeze_Node_For_Instance
9916 begin
9917 -- Nothing to do if the freeze node has already been inserted
9919 if Is_List_Member (F_Node) then
9920 return;
9921 end if;
9923 Inst := Entity (F_Node);
9925 -- When processing a subprogram instantiation, utilize the actual
9926 -- subprogram instantiation rather than its package wrapper as it
9927 -- carries all the context information.
9929 if Is_Wrapper_Package (Inst) then
9930 Inst := Related_Instance (Inst);
9931 end if;
9933 Par_Inst := Parent (Inst);
9935 -- If this is a package instance, check whether the generic is declared
9936 -- in a previous instance and the current instance is not within the
9937 -- previous one.
9939 if Present (Generic_Parent (Par_Inst)) and then Is_In_Main_Unit (N) then
9940 declare
9941 Enclosing_N : constant Node_Id := Enclosing_Body (N);
9942 Par_I : constant Entity_Id :=
9943 Previous_Instance (Generic_Parent (Par_Inst));
9944 Scop : Entity_Id;
9946 begin
9947 if Present (Par_I) and then Earlier (N, Freeze_Node (Par_I)) then
9948 Scop := Scope (Inst);
9950 -- If the current instance is within the one that contains
9951 -- the generic, the freeze node for the current one must
9952 -- appear in the current declarative part. Ditto, if the
9953 -- current instance is within another package instance or
9954 -- within a body that does not enclose the current instance.
9955 -- In these three cases the freeze node of the previous
9956 -- instance is not relevant.
9958 while Present (Scop) and then Scop /= Standard_Standard loop
9959 exit when Scop = Par_I
9960 or else
9961 (Is_Generic_Instance (Scop)
9962 and then Scope_Depth (Scop) > Scope_Depth (Par_I));
9963 Scop := Scope (Scop);
9964 end loop;
9966 -- Previous instance encloses current instance
9968 if Scop = Par_I then
9969 null;
9971 -- If the next node is a source body we must freeze in the
9972 -- current scope as well.
9974 elsif Present (Next (N))
9975 and then Nkind (Next (N)) in N_Subprogram_Body
9976 | N_Package_Body
9977 and then Comes_From_Source (Next (N))
9978 then
9979 null;
9981 -- Current instance is within an unrelated instance
9983 elsif Is_Generic_Instance (Scop) then
9984 null;
9986 -- Current instance is within an unrelated body
9988 elsif Present (Enclosing_N)
9989 and then Enclosing_N /= Enclosing_Body (Par_I)
9990 then
9991 null;
9993 else
9994 Insert_After (Freeze_Node (Par_I), F_Node);
9995 return;
9996 end if;
9997 end if;
9998 end;
9999 end if;
10001 Decl := N;
10002 Decls := List_Containing (N);
10003 Par_N := Parent (Decls);
10005 -- Determine the proper freeze point of an instantiation
10007 if Is_Generic_Instance (Inst) then
10008 loop
10009 -- When the instantiation occurs in a package spec, append the
10010 -- freeze node to the private declarations (if any).
10012 if Nkind (Par_N) = N_Package_Specification
10013 and then Decls = Visible_Declarations (Par_N)
10014 and then not Is_Empty_List (Private_Declarations (Par_N))
10015 then
10016 Decls := Private_Declarations (Par_N);
10017 Decl := First (Decls);
10018 end if;
10020 -- We adhere to the general rule of a package or subprogram body
10021 -- causing freezing of anything before it in the same declarative
10022 -- region. In this respect, the proper freeze point of a package
10023 -- instantiation is before the first source body which follows, or
10024 -- before a stub. This ensures that entities from the instance are
10025 -- already frozen and therefore usable in source bodies.
10027 if Nkind (Par_N) /= N_Package_Declaration
10028 and then
10029 not In_Same_Source_Unit (Generic_Parent (Par_Inst), Inst)
10030 then
10031 while Present (Decl) loop
10032 if (Nkind (Decl) in N_Unit_Body
10033 or else
10034 Nkind (Decl) in N_Body_Stub)
10035 and then Comes_From_Source (Decl)
10036 then
10037 Set_Sloc (F_Node, Sloc (Decl));
10038 Insert_Before (Decl, F_Node);
10039 return;
10040 end if;
10042 Next (Decl);
10043 end loop;
10044 end if;
10046 -- When the instantiation occurs in a package spec and there is
10047 -- no source body which follows, and the package has a body but
10048 -- is delayed, then insert immediately before its freeze node.
10050 if Nkind (Par_N) = N_Package_Specification
10051 and then Present (Corresponding_Body (Parent (Par_N)))
10052 and then Present (Freeze_Node (Defining_Entity (Par_N)))
10053 then
10054 Set_Sloc (F_Node, Sloc (Freeze_Node (Defining_Entity (Par_N))));
10055 Insert_Before (Freeze_Node (Defining_Entity (Par_N)), F_Node);
10056 return;
10058 -- When the instantiation occurs in a package spec and there is
10059 -- no source body which follows, not even of the package itself
10060 -- then insert into the declaration list of the outer level.
10062 elsif Nkind (Par_N) = N_Package_Specification
10063 and then No (Corresponding_Body (Parent (Par_N)))
10064 and then Is_List_Member (Parent (Par_N))
10065 then
10066 Decl := Parent (Par_N);
10067 Decls := List_Containing (Decl);
10068 Par_N := Parent (Decls);
10070 -- In a package declaration, or if no source body which follows
10071 -- and at library level, then insert at end of list.
10073 else
10074 exit;
10075 end if;
10076 end loop;
10077 end if;
10079 -- Insert and adjust the Sloc of the freeze node
10081 Set_Sloc (F_Node, Sloc (Last (Decls)));
10082 Insert_After (Last (Decls), F_Node);
10083 end Insert_Freeze_Node_For_Instance;
10085 -----------------------------
10086 -- Install_Formal_Packages --
10087 -----------------------------
10089 procedure Install_Formal_Packages (Par : Entity_Id) is
10090 E : Entity_Id;
10091 Gen : Entity_Id;
10092 Gen_E : Entity_Id := Empty;
10094 begin
10095 E := First_Entity (Par);
10097 -- If we are installing an instance parent, locate the formal packages
10098 -- of its generic parent.
10100 if Is_Generic_Instance (Par) then
10101 Gen := Generic_Parent (Package_Specification (Par));
10102 Gen_E := First_Entity (Gen);
10103 end if;
10105 while Present (E) loop
10106 if Ekind (E) = E_Package
10107 and then Nkind (Parent (E)) = N_Package_Renaming_Declaration
10108 then
10109 -- If this is the renaming for the parent instance, done
10111 if Renamed_Entity (E) = Par then
10112 exit;
10114 -- The visibility of a formal of an enclosing generic is already
10115 -- correct.
10117 elsif Denotes_Formal_Package (E) then
10118 null;
10120 elsif Present (Associated_Formal_Package (E)) then
10121 Check_Generic_Actuals (Renamed_Entity (E), True);
10122 Set_Is_Hidden (E, False);
10124 -- Find formal package in generic unit that corresponds to
10125 -- (instance of) formal package in instance.
10127 while Present (Gen_E) and then Chars (Gen_E) /= Chars (E) loop
10128 Next_Entity (Gen_E);
10129 end loop;
10131 if Present (Gen_E) then
10132 Map_Formal_Package_Entities (Gen_E, E);
10133 end if;
10134 end if;
10135 end if;
10137 Next_Entity (E);
10139 if Present (Gen_E) then
10140 Next_Entity (Gen_E);
10141 end if;
10142 end loop;
10143 end Install_Formal_Packages;
10145 --------------------
10146 -- Install_Parent --
10147 --------------------
10149 procedure Install_Parent (P : Entity_Id; In_Body : Boolean := False) is
10150 Ancestors : constant Elist_Id := New_Elmt_List;
10151 S : constant Entity_Id := Current_Scope;
10152 Inst_Par : Entity_Id;
10153 First_Par : Entity_Id;
10154 Inst_Node : Node_Id;
10155 Gen_Par : Entity_Id;
10156 First_Gen : Entity_Id;
10157 Elmt : Elmt_Id;
10159 procedure Install_Noninstance_Specs (Par : Entity_Id);
10160 -- Install the scopes of noninstance parent units ending with Par
10162 procedure Install_Spec (Par : Entity_Id);
10163 -- The child unit is within the declarative part of the parent, so the
10164 -- declarations within the parent are immediately visible.
10166 -------------------------------
10167 -- Install_Noninstance_Specs --
10168 -------------------------------
10170 procedure Install_Noninstance_Specs (Par : Entity_Id) is
10171 begin
10172 if Present (Par)
10173 and then Par /= Standard_Standard
10174 and then not In_Open_Scopes (Par)
10175 then
10176 Install_Noninstance_Specs (Scope (Par));
10177 Install_Spec (Par);
10178 end if;
10179 end Install_Noninstance_Specs;
10181 ------------------
10182 -- Install_Spec --
10183 ------------------
10185 procedure Install_Spec (Par : Entity_Id) is
10186 Spec : constant Node_Id := Package_Specification (Par);
10188 begin
10189 -- If this parent of the child instance is a top-level unit,
10190 -- then record the unit and its visibility for later resetting in
10191 -- Remove_Parent. We exclude units that are generic instances, as we
10192 -- only want to record this information for the ultimate top-level
10193 -- noninstance parent (is that always correct???).
10195 if Scope (Par) = Standard_Standard
10196 and then not Is_Generic_Instance (Par)
10197 then
10198 Parent_Unit_Visible := Is_Immediately_Visible (Par);
10199 Instance_Parent_Unit := Par;
10200 end if;
10202 -- Open the parent scope and make it and its declarations visible.
10203 -- If this point is not within a body, then only the visible
10204 -- declarations should be made visible, and installation of the
10205 -- private declarations is deferred until the appropriate point
10206 -- within analysis of the spec being instantiated (see the handling
10207 -- of parent visibility in Analyze_Package_Specification). This is
10208 -- relaxed in the case where the parent unit is Ada.Tags, to avoid
10209 -- private view problems that occur when compiling instantiations of
10210 -- a generic child of that package (Generic_Dispatching_Constructor).
10211 -- If the instance freezes a tagged type, inlinings of operations
10212 -- from Ada.Tags may need the full view of type Tag. If inlining took
10213 -- proper account of establishing visibility of inlined subprograms'
10214 -- parents then it should be possible to remove this
10215 -- special check. ???
10217 Push_Scope (Par);
10218 Set_Is_Immediately_Visible (Par);
10219 Install_Visible_Declarations (Par);
10220 Set_Use (Visible_Declarations (Spec));
10222 if In_Body or else Is_RTU (Par, Ada_Tags) then
10223 Install_Private_Declarations (Par);
10224 Set_Use (Private_Declarations (Spec));
10225 end if;
10226 end Install_Spec;
10228 -- Start of processing for Install_Parent
10230 begin
10231 -- We need to install the parent instance to compile the instantiation
10232 -- of the child, but the child instance must appear in the current
10233 -- scope. Given that we cannot place the parent above the current scope
10234 -- in the scope stack, we duplicate the current scope and unstack both
10235 -- after the instantiation is complete.
10237 -- If the parent is itself the instantiation of a child unit, we must
10238 -- also stack the instantiation of its parent, and so on. Each such
10239 -- ancestor is the prefix of the name in a prior instantiation.
10241 -- If this is a nested instance, the parent unit itself resolves to
10242 -- a renaming of the parent instance, whose declaration we need.
10244 -- Finally, the parent may be a generic (not an instance) when the
10245 -- child unit appears as a formal package.
10247 Inst_Par := P;
10249 if Present (Renamed_Entity (Inst_Par)) then
10250 Inst_Par := Renamed_Entity (Inst_Par);
10251 end if;
10253 First_Par := Inst_Par;
10255 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
10257 First_Gen := Gen_Par;
10259 while Present (Gen_Par) and then Is_Child_Unit (Gen_Par) loop
10261 -- Load grandparent instance as well
10263 Inst_Node := Get_Unit_Instantiation_Node (Inst_Par);
10265 if Nkind (Name (Inst_Node)) = N_Expanded_Name then
10266 Inst_Par := Entity (Prefix (Name (Inst_Node)));
10268 if Present (Renamed_Entity (Inst_Par)) then
10269 Inst_Par := Renamed_Entity (Inst_Par);
10270 end if;
10272 Gen_Par := Generic_Parent (Package_Specification (Inst_Par));
10274 if Present (Gen_Par) then
10275 Prepend_Elmt (Inst_Par, Ancestors);
10277 else
10278 -- Parent is not the name of an instantiation
10280 Install_Noninstance_Specs (Inst_Par);
10281 exit;
10282 end if;
10284 else
10285 -- Previous error
10287 exit;
10288 end if;
10289 end loop;
10291 if Present (First_Gen) then
10292 Append_Elmt (First_Par, Ancestors);
10293 else
10294 Install_Noninstance_Specs (First_Par);
10295 end if;
10297 if not Is_Empty_Elmt_List (Ancestors) then
10298 Elmt := First_Elmt (Ancestors);
10299 while Present (Elmt) loop
10300 Install_Spec (Node (Elmt));
10301 Install_Formal_Packages (Node (Elmt));
10302 Next_Elmt (Elmt);
10303 end loop;
10304 end if;
10306 if not In_Body then
10307 Push_Scope (S);
10308 end if;
10309 end Install_Parent;
10311 -------------------------------
10312 -- Install_Hidden_Primitives --
10313 -------------------------------
10315 procedure Install_Hidden_Primitives
10316 (Prims_List : in out Elist_Id;
10317 Gen_T : Entity_Id;
10318 Act_T : Entity_Id)
10320 Elmt : Elmt_Id;
10321 List : Elist_Id := No_Elist;
10322 Prim_G_Elmt : Elmt_Id;
10323 Prim_A_Elmt : Elmt_Id;
10324 Prim_G : Node_Id;
10325 Prim_A : Node_Id;
10327 begin
10328 -- No action needed in case of serious errors because we cannot trust
10329 -- in the order of primitives
10331 if Serious_Errors_Detected > 0 then
10332 return;
10334 -- No action possible if we don't have available the list of primitive
10335 -- operations
10337 elsif No (Gen_T)
10338 or else not Is_Record_Type (Gen_T)
10339 or else not Is_Tagged_Type (Gen_T)
10340 or else not Is_Record_Type (Act_T)
10341 or else not Is_Tagged_Type (Act_T)
10342 then
10343 return;
10345 -- There is no need to handle interface types since their primitives
10346 -- cannot be hidden
10348 elsif Is_Interface (Gen_T) then
10349 return;
10350 end if;
10352 Prim_G_Elmt := First_Elmt (Primitive_Operations (Gen_T));
10354 if not Is_Class_Wide_Type (Act_T) then
10355 Prim_A_Elmt := First_Elmt (Primitive_Operations (Act_T));
10356 else
10357 Prim_A_Elmt := First_Elmt (Primitive_Operations (Root_Type (Act_T)));
10358 end if;
10360 loop
10361 -- Skip predefined primitives in the generic formal
10363 while Present (Prim_G_Elmt)
10364 and then Is_Predefined_Dispatching_Operation (Node (Prim_G_Elmt))
10365 loop
10366 Next_Elmt (Prim_G_Elmt);
10367 end loop;
10369 -- Skip predefined primitives in the generic actual
10371 while Present (Prim_A_Elmt)
10372 and then Is_Predefined_Dispatching_Operation (Node (Prim_A_Elmt))
10373 loop
10374 Next_Elmt (Prim_A_Elmt);
10375 end loop;
10377 exit when No (Prim_G_Elmt) or else No (Prim_A_Elmt);
10379 Prim_G := Node (Prim_G_Elmt);
10380 Prim_A := Node (Prim_A_Elmt);
10382 -- There is no need to handle interface primitives because their
10383 -- primitives are not hidden
10385 exit when Present (Interface_Alias (Prim_G));
10387 -- Here we install one hidden primitive
10389 if Chars (Prim_G) /= Chars (Prim_A)
10390 and then Has_Suffix (Prim_A, 'P')
10391 and then Remove_Suffix (Prim_A, 'P') = Chars (Prim_G)
10392 then
10393 Set_Chars (Prim_A, Chars (Prim_G));
10394 Append_New_Elmt (Prim_A, To => List);
10395 end if;
10397 Next_Elmt (Prim_A_Elmt);
10398 Next_Elmt (Prim_G_Elmt);
10399 end loop;
10401 -- Append the elements to the list of temporarily visible primitives
10402 -- avoiding duplicates.
10404 if Present (List) then
10405 if No (Prims_List) then
10406 Prims_List := New_Elmt_List;
10407 end if;
10409 Elmt := First_Elmt (List);
10410 while Present (Elmt) loop
10411 Append_Unique_Elmt (Node (Elmt), Prims_List);
10412 Next_Elmt (Elmt);
10413 end loop;
10414 end if;
10415 end Install_Hidden_Primitives;
10417 -------------------------------
10418 -- Restore_Hidden_Primitives --
10419 -------------------------------
10421 procedure Restore_Hidden_Primitives (Prims_List : in out Elist_Id) is
10422 Prim_Elmt : Elmt_Id;
10423 Prim : Node_Id;
10425 begin
10426 if Prims_List /= No_Elist then
10427 Prim_Elmt := First_Elmt (Prims_List);
10428 while Present (Prim_Elmt) loop
10429 Prim := Node (Prim_Elmt);
10430 Set_Chars (Prim, Add_Suffix (Prim, 'P'));
10431 Next_Elmt (Prim_Elmt);
10432 end loop;
10434 Prims_List := No_Elist;
10435 end if;
10436 end Restore_Hidden_Primitives;
10438 --------------------------------
10439 -- Instantiate_Formal_Package --
10440 --------------------------------
10442 function Instantiate_Formal_Package
10443 (Formal : Node_Id;
10444 Actual : Node_Id;
10445 Analyzed_Formal : Node_Id) return List_Id
10447 Loc : constant Source_Ptr := Sloc (Actual);
10448 Hidden_Formals : constant Elist_Id := New_Elmt_List;
10449 Actual_Pack : Entity_Id;
10450 Formal_Pack : Entity_Id;
10451 Gen_Parent : Entity_Id;
10452 Decls : List_Id;
10453 Nod : Node_Id;
10454 Parent_Spec : Node_Id;
10456 procedure Find_Matching_Actual
10457 (F : Node_Id;
10458 Act : in out Entity_Id);
10459 -- We need to associate each formal entity in the formal package with
10460 -- the corresponding entity in the actual package. The actual package
10461 -- has been analyzed and possibly expanded, and as a result there is
10462 -- no one-to-one correspondence between the two lists (for example,
10463 -- the actual may include subtypes, itypes, and inherited primitive
10464 -- operations, interspersed among the renaming declarations for the
10465 -- actuals). We retrieve the corresponding actual by name because each
10466 -- actual has the same name as the formal, and they do appear in the
10467 -- same order.
10469 function Get_Formal_Entity (N : Node_Id) return Entity_Id;
10470 -- Retrieve entity of defining entity of generic formal parameter.
10471 -- Only the declarations of formals need to be considered when
10472 -- linking them to actuals, but the declarative list may include
10473 -- internal entities generated during analysis, and those are ignored.
10475 procedure Match_Formal_Entity
10476 (Formal_Node : Node_Id;
10477 Formal_Ent : Entity_Id;
10478 Actual_Ent : Entity_Id);
10479 -- Associates the formal entity with the actual. In the case where
10480 -- Formal_Ent is a formal package, this procedure iterates through all
10481 -- of its formals and enters associations between the actuals occurring
10482 -- in the formal package's corresponding actual package (given by
10483 -- Actual_Ent) and the formal package's formal parameters. This
10484 -- procedure recurses if any of the parameters is itself a package.
10486 function Is_Instance_Of
10487 (Act_Spec : Entity_Id;
10488 Gen_Anc : Entity_Id) return Boolean;
10489 -- The actual can be an instantiation of a generic within another
10490 -- instance, in which case there is no direct link from it to the
10491 -- original generic ancestor. In that case, we recognize that the
10492 -- ultimate ancestor is the same by examining names and scopes.
10494 procedure Process_Nested_Formal (Formal : Entity_Id);
10495 -- If the current formal is declared with a box, its own formals are
10496 -- visible in the instance, as they were in the generic, and their
10497 -- Hidden flag must be reset. If some of these formals are themselves
10498 -- packages declared with a box, the processing must be recursive.
10500 --------------------------
10501 -- Find_Matching_Actual --
10502 --------------------------
10504 procedure Find_Matching_Actual
10505 (F : Node_Id;
10506 Act : in out Entity_Id)
10508 Formal_Ent : Entity_Id;
10510 begin
10511 case Nkind (Original_Node (F)) is
10512 when N_Formal_Object_Declaration
10513 | N_Formal_Type_Declaration
10515 Formal_Ent := Defining_Identifier (F);
10517 while Present (Act)
10518 and then Chars (Act) /= Chars (Formal_Ent)
10519 loop
10520 Next_Entity (Act);
10521 end loop;
10523 when N_Formal_Package_Declaration
10524 | N_Formal_Subprogram_Declaration
10525 | N_Generic_Package_Declaration
10526 | N_Package_Declaration
10528 Formal_Ent := Defining_Entity (F);
10530 while Present (Act)
10531 and then Chars (Act) /= Chars (Formal_Ent)
10532 loop
10533 Next_Entity (Act);
10534 end loop;
10536 when others =>
10537 raise Program_Error;
10538 end case;
10539 end Find_Matching_Actual;
10541 -------------------------
10542 -- Match_Formal_Entity --
10543 -------------------------
10545 procedure Match_Formal_Entity
10546 (Formal_Node : Node_Id;
10547 Formal_Ent : Entity_Id;
10548 Actual_Ent : Entity_Id)
10550 Act_Pkg : Entity_Id;
10552 begin
10553 Set_Instance_Of (Formal_Ent, Actual_Ent);
10555 if Ekind (Actual_Ent) = E_Package then
10557 -- Record associations for each parameter
10559 Act_Pkg := Actual_Ent;
10561 declare
10562 A_Ent : Entity_Id := First_Entity (Act_Pkg);
10563 F_Ent : Entity_Id;
10564 F_Node : Node_Id;
10566 Gen_Decl : Node_Id;
10567 Formals : List_Id;
10568 Actual : Entity_Id;
10570 begin
10571 -- Retrieve the actual given in the formal package declaration
10573 Actual := Entity (Name (Original_Node (Formal_Node)));
10575 -- The actual in the formal package declaration may be a
10576 -- renamed generic package, in which case we want to retrieve
10577 -- the original generic in order to traverse its formal part.
10579 if Present (Renamed_Entity (Actual)) then
10580 Gen_Decl := Unit_Declaration_Node (Renamed_Entity (Actual));
10581 else
10582 Gen_Decl := Unit_Declaration_Node (Actual);
10583 end if;
10585 Formals := Generic_Formal_Declarations (Gen_Decl);
10587 if Present (Formals) then
10588 F_Node := First_Non_Pragma (Formals);
10589 else
10590 F_Node := Empty;
10591 end if;
10593 while Present (A_Ent)
10594 and then Present (F_Node)
10595 and then A_Ent /= First_Private_Entity (Act_Pkg)
10596 loop
10597 F_Ent := Get_Formal_Entity (F_Node);
10599 if Present (F_Ent) then
10601 -- This is a formal of the original package. Record
10602 -- association and recurse.
10604 Find_Matching_Actual (F_Node, A_Ent);
10605 Match_Formal_Entity (F_Node, F_Ent, A_Ent);
10606 Next_Entity (A_Ent);
10607 end if;
10609 Next_Non_Pragma (F_Node);
10610 end loop;
10611 end;
10612 end if;
10613 end Match_Formal_Entity;
10615 -----------------------
10616 -- Get_Formal_Entity --
10617 -----------------------
10619 function Get_Formal_Entity (N : Node_Id) return Entity_Id is
10620 Kind : constant Node_Kind := Nkind (Original_Node (N));
10621 begin
10622 case Kind is
10623 when N_Formal_Object_Declaration =>
10624 return Defining_Identifier (N);
10626 when N_Formal_Type_Declaration =>
10627 return Defining_Identifier (N);
10629 when N_Formal_Subprogram_Declaration =>
10630 return Defining_Unit_Name (Specification (N));
10632 when N_Formal_Package_Declaration =>
10633 return Defining_Identifier (Original_Node (N));
10635 when N_Generic_Package_Declaration =>
10636 return Defining_Identifier (Original_Node (N));
10638 -- All other declarations are introduced by semantic analysis and
10639 -- have no match in the actual.
10641 when others =>
10642 return Empty;
10643 end case;
10644 end Get_Formal_Entity;
10646 --------------------
10647 -- Is_Instance_Of --
10648 --------------------
10650 function Is_Instance_Of
10651 (Act_Spec : Entity_Id;
10652 Gen_Anc : Entity_Id) return Boolean
10654 Gen_Par : constant Entity_Id := Generic_Parent (Act_Spec);
10656 begin
10657 if No (Gen_Par) then
10658 return False;
10660 -- Simplest case: the generic parent of the actual is the formal
10662 elsif Gen_Par = Gen_Anc then
10663 return True;
10665 elsif Chars (Gen_Par) /= Chars (Gen_Anc) then
10666 return False;
10668 -- The actual may be obtained through several instantiations. Its
10669 -- scope must itself be an instance of a generic declared in the
10670 -- same scope as the formal. Any other case is detected above.
10672 elsif not Is_Generic_Instance (Scope (Gen_Par)) then
10673 return False;
10675 else
10676 return Generic_Parent (Parent (Scope (Gen_Par))) = Scope (Gen_Anc);
10677 end if;
10678 end Is_Instance_Of;
10680 ---------------------------
10681 -- Process_Nested_Formal --
10682 ---------------------------
10684 procedure Process_Nested_Formal (Formal : Entity_Id) is
10685 Ent : Entity_Id;
10687 begin
10688 if Present (Associated_Formal_Package (Formal))
10689 and then Box_Present (Parent (Associated_Formal_Package (Formal)))
10690 then
10691 Ent := First_Entity (Formal);
10692 while Present (Ent) loop
10693 Set_Is_Hidden (Ent, False);
10694 Set_Is_Visible_Formal (Ent);
10695 Set_Is_Potentially_Use_Visible
10696 (Ent, Is_Potentially_Use_Visible (Formal));
10698 if Ekind (Ent) = E_Package then
10699 exit when Renamed_Entity (Ent) = Renamed_Entity (Formal);
10700 Process_Nested_Formal (Ent);
10701 end if;
10703 Next_Entity (Ent);
10704 end loop;
10705 end if;
10706 end Process_Nested_Formal;
10708 -- Start of processing for Instantiate_Formal_Package
10710 begin
10711 Analyze (Actual);
10713 -- The actual must be a package instance, or else a current instance
10714 -- such as a parent generic within the body of a generic child.
10716 if not Is_Entity_Name (Actual)
10717 or else not Is_Package_Or_Generic_Package (Entity (Actual))
10718 then
10719 Error_Msg_N
10720 ("expect package instance to instantiate formal", Actual);
10721 Abandon_Instantiation (Actual);
10722 raise Program_Error;
10724 else
10725 Actual_Pack := Entity (Actual);
10726 Set_Is_Instantiated (Actual_Pack);
10728 -- The actual may be a renamed package, or an outer generic formal
10729 -- package whose instantiation is converted into a renaming.
10731 if Present (Renamed_Entity (Actual_Pack)) then
10732 Actual_Pack := Renamed_Entity (Actual_Pack);
10733 end if;
10735 if Nkind (Analyzed_Formal) = N_Formal_Package_Declaration then
10736 Gen_Parent := Get_Instance_Of (Entity (Name (Analyzed_Formal)));
10737 Formal_Pack := Defining_Identifier (Analyzed_Formal);
10738 else
10739 Gen_Parent :=
10740 Generic_Parent (Specification (Analyzed_Formal));
10741 Formal_Pack :=
10742 Defining_Unit_Name (Specification (Analyzed_Formal));
10743 end if;
10745 if Nkind (Parent (Actual_Pack)) = N_Defining_Program_Unit_Name then
10746 Parent_Spec := Package_Specification (Actual_Pack);
10747 else
10748 Parent_Spec := Parent (Actual_Pack);
10749 end if;
10751 if Gen_Parent = Any_Id then
10752 Error_Msg_N
10753 ("previous error in declaration of formal package", Actual);
10754 Abandon_Instantiation (Actual);
10756 elsif Is_Instance_Of (Parent_Spec, Get_Instance_Of (Gen_Parent)) then
10757 null;
10759 -- If this is the current instance of an enclosing generic, that unit
10760 -- is the generic package we need.
10762 elsif In_Open_Scopes (Actual_Pack)
10763 and then Ekind (Actual_Pack) = E_Generic_Package
10764 then
10765 null;
10767 else
10768 Error_Msg_NE
10769 ("actual parameter must be instance of&", Actual, Gen_Parent);
10770 Abandon_Instantiation (Actual);
10771 end if;
10773 Set_Instance_Of (Defining_Identifier (Formal), Actual_Pack);
10774 Map_Formal_Package_Entities (Formal_Pack, Actual_Pack);
10776 Nod :=
10777 Make_Package_Renaming_Declaration (Loc,
10778 Defining_Unit_Name => New_Copy (Defining_Identifier (Formal)),
10779 Name => New_Occurrence_Of (Actual_Pack, Loc));
10781 Set_Associated_Formal_Package
10782 (Defining_Unit_Name (Nod), Defining_Identifier (Formal));
10783 Decls := New_List (Nod);
10785 -- If the formal F has a box, then the generic declarations are
10786 -- visible in the generic G. In an instance of G, the corresponding
10787 -- entities in the actual for F (which are the actuals for the
10788 -- instantiation of the generic that F denotes) must also be made
10789 -- visible for analysis of the current instance. On exit from the
10790 -- current instance, those entities are made private again. If the
10791 -- actual is currently in use, these entities are also use-visible.
10793 -- The loop through the actual entities also steps through the formal
10794 -- entities and enters associations from formals to actuals into the
10795 -- renaming map. This is necessary to properly handle checking of
10796 -- actual parameter associations for later formals that depend on
10797 -- actuals declared in the formal package.
10799 -- In Ada 2005, partial parameterization requires that we make
10800 -- visible the actuals corresponding to formals that were defaulted
10801 -- in the formal package. There formals are identified because they
10802 -- remain formal generics within the formal package, rather than
10803 -- being renamings of the actuals supplied.
10805 declare
10806 Gen_Decl : constant Node_Id :=
10807 Unit_Declaration_Node (Gen_Parent);
10808 Formals : constant List_Id :=
10809 Generic_Formal_Declarations (Gen_Decl);
10811 Actual_Ent : Entity_Id;
10812 Actual_Of_Formal : Node_Id;
10813 Formal_Node : Node_Id;
10814 Formal_Ent : Entity_Id;
10816 begin
10817 if Present (Formals) then
10818 Formal_Node := First_Non_Pragma (Formals);
10819 else
10820 Formal_Node := Empty;
10821 end if;
10823 Actual_Ent := First_Entity (Actual_Pack);
10824 Actual_Of_Formal :=
10825 First (Visible_Declarations (Specification (Analyzed_Formal)));
10826 while Present (Actual_Ent)
10827 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
10828 loop
10829 if Present (Formal_Node) then
10830 Formal_Ent := Get_Formal_Entity (Formal_Node);
10832 if Present (Formal_Ent) then
10833 Find_Matching_Actual (Formal_Node, Actual_Ent);
10834 Match_Formal_Entity (Formal_Node, Formal_Ent, Actual_Ent);
10836 -- We iterate at the same time over the actuals of the
10837 -- local package created for the formal, to determine
10838 -- which one of the formals of the original generic were
10839 -- defaulted in the formal. The corresponding actual
10840 -- entities are visible in the enclosing instance.
10842 if Box_Present (Formal)
10843 or else
10844 (Present (Actual_Of_Formal)
10845 and then
10846 Is_Generic_Formal
10847 (Get_Formal_Entity (Actual_Of_Formal)))
10848 then
10849 Set_Is_Hidden (Actual_Ent, False);
10850 Set_Is_Visible_Formal (Actual_Ent);
10851 Set_Is_Potentially_Use_Visible
10852 (Actual_Ent, In_Use (Actual_Pack));
10854 if Ekind (Actual_Ent) = E_Package then
10855 Process_Nested_Formal (Actual_Ent);
10856 end if;
10858 else
10859 if not Is_Hidden (Actual_Ent) then
10860 Append_Elmt (Actual_Ent, Hidden_Formals);
10861 end if;
10863 Set_Is_Hidden (Actual_Ent);
10864 Set_Is_Potentially_Use_Visible (Actual_Ent, False);
10865 end if;
10866 end if;
10868 Next_Non_Pragma (Formal_Node);
10869 Next (Actual_Of_Formal);
10871 -- A formal subprogram may be overloaded, so advance in
10872 -- the list of actuals to make sure we do not match two
10873 -- successive formals to the same actual. This is only
10874 -- relevant for overloadable entities, others have
10875 -- distinct names.
10877 if Is_Overloadable (Actual_Ent) then
10878 Next_Entity (Actual_Ent);
10879 end if;
10881 else
10882 -- No further formals to match, but the generic part may
10883 -- contain inherited operation that are not hidden in the
10884 -- enclosing instance.
10886 Next_Entity (Actual_Ent);
10887 end if;
10888 end loop;
10890 -- Inherited subprograms generated by formal derived types are
10891 -- also visible if the types are.
10893 Actual_Ent := First_Entity (Actual_Pack);
10894 while Present (Actual_Ent)
10895 and then Actual_Ent /= First_Private_Entity (Actual_Pack)
10896 loop
10897 if Is_Overloadable (Actual_Ent)
10898 and then
10899 Nkind (Parent (Actual_Ent)) = N_Subtype_Declaration
10900 and then
10901 not Is_Hidden (Defining_Identifier (Parent (Actual_Ent)))
10902 then
10903 Set_Is_Hidden (Actual_Ent, False);
10904 Set_Is_Potentially_Use_Visible
10905 (Actual_Ent, In_Use (Actual_Pack));
10906 end if;
10908 Next_Entity (Actual_Ent);
10909 end loop;
10911 -- No conformance to check if the generic has no formal parameters
10912 -- and the formal package has no generic associations.
10914 if Is_Empty_List (Formals)
10915 and then
10916 (Box_Present (Formal)
10917 or else No (Generic_Associations (Formal)))
10918 then
10919 return Decls;
10920 end if;
10921 end;
10923 -- If the formal is not declared with a box, reanalyze it as an
10924 -- abbreviated instantiation, to verify the matching rules of 12.7.
10925 -- The actual checks are performed after the generic associations
10926 -- have been analyzed, to guarantee the same visibility for this
10927 -- instantiation and for the actuals.
10929 -- In Ada 2005, the generic associations for the formal can include
10930 -- defaulted parameters. These are ignored during check. This
10931 -- internal instantiation is removed from the tree after conformance
10932 -- checking, because it contains formal declarations for those
10933 -- defaulted parameters, and those should not reach the back-end.
10935 if not Box_Present (Formal) then
10936 declare
10937 I_Pack : constant Entity_Id :=
10938 Make_Temporary (Sloc (Actual), 'P');
10940 begin
10941 Set_Is_Internal (I_Pack);
10942 Mutate_Ekind (I_Pack, E_Package);
10943 Set_Hidden_In_Formal_Instance (I_Pack, Hidden_Formals);
10945 Append_To (Decls,
10946 Make_Package_Instantiation (Sloc (Actual),
10947 Defining_Unit_Name => I_Pack,
10948 Name =>
10949 New_Occurrence_Of
10950 (Get_Instance_Of (Gen_Parent), Sloc (Actual)),
10951 Generic_Associations => Generic_Associations (Formal)));
10952 end;
10953 end if;
10955 return Decls;
10956 end if;
10957 end Instantiate_Formal_Package;
10959 -----------------------------------
10960 -- Instantiate_Formal_Subprogram --
10961 -----------------------------------
10963 function Instantiate_Formal_Subprogram
10964 (Formal : Node_Id;
10965 Actual : Node_Id;
10966 Analyzed_Formal : Node_Id) return Node_Id
10968 Analyzed_S : constant Entity_Id :=
10969 Defining_Unit_Name (Specification (Analyzed_Formal));
10970 Formal_Sub : constant Entity_Id :=
10971 Defining_Unit_Name (Specification (Formal));
10973 function From_Parent_Scope (Subp : Entity_Id) return Boolean;
10974 -- If the generic is a child unit, the parent has been installed on the
10975 -- scope stack, but a default subprogram cannot resolve to something
10976 -- on the parent because that parent is not really part of the visible
10977 -- context (it is there to resolve explicit local entities). If the
10978 -- default has resolved in this way, we remove the entity from immediate
10979 -- visibility and analyze the node again to emit an error message or
10980 -- find another visible candidate.
10982 procedure Valid_Actual_Subprogram (Act : Node_Id);
10983 -- Perform legality check and raise exception on failure
10985 -----------------------
10986 -- From_Parent_Scope --
10987 -----------------------
10989 function From_Parent_Scope (Subp : Entity_Id) return Boolean is
10990 Gen_Scope : Node_Id;
10992 begin
10993 Gen_Scope := Scope (Analyzed_S);
10994 while Present (Gen_Scope) and then Is_Child_Unit (Gen_Scope) loop
10995 if Scope (Subp) = Scope (Gen_Scope) then
10996 return True;
10997 end if;
10999 Gen_Scope := Scope (Gen_Scope);
11000 end loop;
11002 return False;
11003 end From_Parent_Scope;
11005 -----------------------------
11006 -- Valid_Actual_Subprogram --
11007 -----------------------------
11009 procedure Valid_Actual_Subprogram (Act : Node_Id) is
11010 Act_E : Entity_Id;
11012 begin
11013 if Is_Entity_Name (Act) then
11014 Act_E := Entity (Act);
11016 elsif Nkind (Act) = N_Selected_Component
11017 and then Is_Entity_Name (Selector_Name (Act))
11018 then
11019 Act_E := Entity (Selector_Name (Act));
11021 else
11022 Act_E := Empty;
11023 end if;
11025 if (Present (Act_E) and then Is_Overloadable (Act_E))
11026 or else Nkind (Act) in N_Attribute_Reference
11027 | N_Indexed_Component
11028 | N_Character_Literal
11029 | N_Explicit_Dereference
11030 then
11031 return;
11032 end if;
11034 Error_Msg_NE
11035 ("expect subprogram or entry name in instantiation of &",
11036 Instantiation_Node, Formal_Sub);
11037 Abandon_Instantiation (Instantiation_Node);
11038 end Valid_Actual_Subprogram;
11040 -- Local variables
11042 Decl_Node : Node_Id;
11043 Loc : Source_Ptr;
11044 Nam : Node_Id;
11045 New_Spec : Node_Id;
11046 New_Subp : Entity_Id;
11048 -- Start of processing for Instantiate_Formal_Subprogram
11050 begin
11051 New_Spec := New_Copy_Tree (Specification (Formal));
11053 -- The tree copy has created the proper instantiation sloc for the
11054 -- new specification. Use this location for all other constructed
11055 -- declarations.
11057 Loc := Sloc (Defining_Unit_Name (New_Spec));
11059 -- Create new entity for the actual (New_Copy_Tree does not), and
11060 -- indicate that it is an actual.
11062 -- If the actual is not an entity (i.e. an attribute reference)
11063 -- and the formal includes aspect specifications for contracts,
11064 -- we create an internal name for the renaming declaration. The
11065 -- constructed wrapper contains a call to the entity in the renaming.
11066 -- This is an expansion activity, as is the wrapper creation.
11068 if Ada_Version >= Ada_2022
11069 and then Has_Contracts (Analyzed_Formal)
11070 and then not Is_Entity_Name (Actual)
11071 and then Expander_Active
11072 then
11073 New_Subp := Make_Temporary (Sloc (Actual), 'S');
11074 Set_Defining_Unit_Name (New_Spec, New_Subp);
11075 else
11076 New_Subp := Make_Defining_Identifier (Loc, Chars (Formal_Sub));
11077 end if;
11079 Mutate_Ekind (New_Subp, Ekind (Analyzed_S));
11080 Set_Is_Generic_Actual_Subprogram (New_Subp);
11081 Set_Defining_Unit_Name (New_Spec, New_Subp);
11083 -- Create new entities for the each of the formals in the specification
11084 -- of the renaming declaration built for the actual.
11086 if Present (Parameter_Specifications (New_Spec)) then
11087 declare
11088 F : Node_Id;
11089 F_Id : Entity_Id;
11091 begin
11092 F := First (Parameter_Specifications (New_Spec));
11093 while Present (F) loop
11094 F_Id := Defining_Identifier (F);
11096 Set_Defining_Identifier (F,
11097 Make_Defining_Identifier (Sloc (F_Id), Chars (F_Id)));
11098 Next (F);
11099 end loop;
11100 end;
11101 end if;
11103 -- Find entity of actual. If the actual is an attribute reference, it
11104 -- cannot be resolved here (its formal is missing) but is handled
11105 -- instead in Attribute_Renaming. If the actual is overloaded, it is
11106 -- fully resolved subsequently, when the renaming declaration for the
11107 -- formal is analyzed. If it is an explicit dereference, resolve the
11108 -- prefix but not the actual itself, to prevent interpretation as call.
11110 if Present (Actual) then
11111 Loc := Sloc (Actual);
11112 Set_Sloc (New_Spec, Loc);
11114 if Nkind (Actual) = N_Operator_Symbol then
11115 Find_Direct_Name (Actual);
11117 elsif Nkind (Actual) = N_Explicit_Dereference then
11118 Analyze (Prefix (Actual));
11120 elsif Nkind (Actual) /= N_Attribute_Reference then
11121 Analyze (Actual);
11122 end if;
11124 Valid_Actual_Subprogram (Actual);
11125 Nam := Actual;
11127 elsif Present (Default_Name (Formal)) then
11128 if Nkind (Default_Name (Formal)) not in N_Attribute_Reference
11129 | N_Selected_Component
11130 | N_Indexed_Component
11131 | N_Character_Literal
11132 and then Present (Entity (Default_Name (Formal)))
11133 then
11134 Nam := New_Occurrence_Of (Entity (Default_Name (Formal)), Loc);
11135 else
11136 Nam := New_Copy (Default_Name (Formal));
11137 Set_Sloc (Nam, Loc);
11138 end if;
11140 elsif Box_Present (Formal) then
11142 -- Actual is resolved at the point of instantiation. Create an
11143 -- identifier or operator with the same name as the formal.
11145 if Nkind (Formal_Sub) = N_Defining_Operator_Symbol then
11146 Nam :=
11147 Make_Operator_Symbol (Loc,
11148 Chars => Chars (Formal_Sub),
11149 Strval => No_String);
11150 else
11151 Nam := Make_Identifier (Loc, Chars (Formal_Sub));
11152 end if;
11154 elsif Nkind (Specification (Formal)) = N_Procedure_Specification
11155 and then Null_Present (Specification (Formal))
11156 then
11157 -- Generate null body for procedure, for use in the instance
11159 Decl_Node :=
11160 Make_Subprogram_Body (Loc,
11161 Specification => New_Spec,
11162 Declarations => New_List,
11163 Handled_Statement_Sequence =>
11164 Make_Handled_Sequence_Of_Statements (Loc,
11165 Statements => New_List (Make_Null_Statement (Loc))));
11167 -- RM 12.6 (16.2/2): The procedure has convention Intrinsic
11169 Set_Convention (Defining_Unit_Name (New_Spec), Convention_Intrinsic);
11171 -- Eliminate the calls to it when optimization is enabled
11173 Set_Is_Inlined (Defining_Unit_Name (New_Spec));
11174 return Decl_Node;
11176 -- Handle case of a formal function with an expression default (allowed
11177 -- when extensions are enabled).
11179 elsif Nkind (Specification (Formal)) = N_Function_Specification
11180 and then Present (Expression (Formal))
11181 then
11182 -- Generate body for function, for use in the instance
11184 declare
11185 Expr : constant Node_Id := New_Copy (Expression (Formal));
11186 Stmt : constant Node_Id := Make_Simple_Return_Statement (Loc);
11187 begin
11188 Set_Sloc (Expr, Loc);
11189 Set_Expression (Stmt, Expr);
11191 Decl_Node :=
11192 Make_Subprogram_Body (Loc,
11193 Specification => New_Spec,
11194 Declarations => New_List,
11195 Handled_Statement_Sequence =>
11196 Make_Handled_Sequence_Of_Statements (Loc,
11197 Statements => New_List (Stmt)));
11198 end;
11200 -- RM 12.6 (16.2/2): Like a null procedure default, the function
11201 -- has convention Intrinsic.
11203 Set_Convention (Defining_Unit_Name (New_Spec), Convention_Intrinsic);
11205 -- Inline calls to it when optimization is enabled
11207 Set_Is_Inlined (Defining_Unit_Name (New_Spec));
11208 return Decl_Node;
11210 else
11211 Error_Msg_Sloc := Sloc (Scope (Analyzed_S));
11212 Error_Msg_NE
11213 ("missing actual&", Instantiation_Node, Formal_Sub);
11214 Error_Msg_NE
11215 ("\in instantiation of & declared#",
11216 Instantiation_Node, Scope (Analyzed_S));
11217 Abandon_Instantiation (Instantiation_Node);
11218 end if;
11220 Decl_Node :=
11221 Make_Subprogram_Renaming_Declaration (Loc,
11222 Specification => New_Spec,
11223 Name => Nam);
11225 -- If we do not have an actual and the formal specified <> then set to
11226 -- get proper default.
11228 if No (Actual) and then Box_Present (Formal) then
11229 Set_From_Default (Decl_Node);
11230 end if;
11232 -- Gather possible interpretations for the actual before analyzing the
11233 -- instance. If overloaded, it will be resolved when analyzing the
11234 -- renaming declaration.
11236 if Box_Present (Formal) and then No (Actual) then
11237 Analyze (Nam);
11239 if Is_Child_Unit (Scope (Analyzed_S))
11240 and then Present (Entity (Nam))
11241 then
11242 if not Is_Overloaded (Nam) then
11243 if From_Parent_Scope (Entity (Nam)) then
11244 Set_Is_Immediately_Visible (Entity (Nam), False);
11245 Set_Entity (Nam, Empty);
11246 Set_Etype (Nam, Empty);
11248 Analyze (Nam);
11249 Set_Is_Immediately_Visible (Entity (Nam));
11250 end if;
11252 else
11253 declare
11254 I : Interp_Index;
11255 It : Interp;
11257 begin
11258 Get_First_Interp (Nam, I, It);
11259 while Present (It.Nam) loop
11260 if From_Parent_Scope (It.Nam) then
11261 Remove_Interp (I);
11262 end if;
11264 Get_Next_Interp (I, It);
11265 end loop;
11266 end;
11267 end if;
11268 end if;
11269 end if;
11271 -- The generic instantiation freezes the actual. This can only be done
11272 -- once the actual is resolved, in the analysis of the renaming
11273 -- declaration. To make the formal subprogram entity available, we set
11274 -- Corresponding_Formal_Spec to point to the formal subprogram entity.
11275 -- This is also needed in Analyze_Subprogram_Renaming for the processing
11276 -- of formal abstract subprograms.
11278 Set_Corresponding_Formal_Spec (Decl_Node, Analyzed_S);
11280 -- We cannot analyze the renaming declaration, and thus find the actual,
11281 -- until all the actuals are assembled in the instance. For subsequent
11282 -- checks of other actuals, indicate the node that will hold the
11283 -- instance of this formal.
11285 Set_Instance_Of (Analyzed_S, Nam);
11287 if Nkind (Actual) = N_Selected_Component
11288 and then Is_Task_Type (Etype (Prefix (Actual)))
11289 and then not Is_Frozen (Etype (Prefix (Actual)))
11290 then
11291 -- The renaming declaration will create a body, which must appear
11292 -- outside of the instantiation, We move the renaming declaration
11293 -- out of the instance, and create an additional renaming inside,
11294 -- to prevent freezing anomalies.
11296 declare
11297 Anon_Id : constant Entity_Id := Make_Temporary (Loc, 'E');
11299 begin
11300 Set_Defining_Unit_Name (New_Spec, Anon_Id);
11301 Insert_Before (Instantiation_Node, Decl_Node);
11302 Analyze (Decl_Node);
11304 -- Now create renaming within the instance
11306 Decl_Node :=
11307 Make_Subprogram_Renaming_Declaration (Loc,
11308 Specification => New_Copy_Tree (New_Spec),
11309 Name => New_Occurrence_Of (Anon_Id, Loc));
11311 Set_Defining_Unit_Name (Specification (Decl_Node),
11312 Make_Defining_Identifier (Loc, Chars (Formal_Sub)));
11313 end;
11314 end if;
11316 return Decl_Node;
11317 end Instantiate_Formal_Subprogram;
11319 ------------------------
11320 -- Instantiate_Object --
11321 ------------------------
11323 function Instantiate_Object
11324 (Formal : Node_Id;
11325 Actual : Node_Id;
11326 Analyzed_Formal : Node_Id) return List_Id
11328 Gen_Obj : constant Entity_Id := Defining_Identifier (Formal);
11329 A_Gen_Obj : constant Entity_Id :=
11330 Defining_Identifier (Analyzed_Formal);
11331 Acc_Def : Node_Id := Empty;
11332 Act_Assoc : constant Node_Id :=
11333 (if No (Actual) then Empty else Parent (Actual));
11334 Actual_Decl : Node_Id := Empty;
11335 Decl_Node : Node_Id;
11336 Def : Node_Id;
11337 Ftyp : Entity_Id;
11338 List : constant List_Id := New_List;
11339 Loc : constant Source_Ptr := Sloc (Actual);
11340 Orig_Ftyp : constant Entity_Id := Etype (A_Gen_Obj);
11341 Subt_Decl : Node_Id := Empty;
11342 Subt_Mark : Node_Id := Empty;
11344 -- Start of processing for Instantiate_Object
11346 begin
11347 -- Formal may be an anonymous access
11349 if Present (Subtype_Mark (Formal)) then
11350 Subt_Mark := Subtype_Mark (Formal);
11351 else
11352 Check_Access_Definition (Formal);
11353 Acc_Def := Access_Definition (Formal);
11354 end if;
11356 -- Sloc for error message on missing actual
11358 Error_Msg_Sloc := Sloc (Scope (A_Gen_Obj));
11360 if Get_Instance_Of (Gen_Obj) /= Gen_Obj then
11361 Error_Msg_N ("duplicate instantiation of generic parameter", Actual);
11362 end if;
11364 Set_Parent (List, Act_Assoc);
11366 -- OUT present
11368 if Out_Present (Formal) then
11370 -- An IN OUT generic actual must be a name. The instantiation is a
11371 -- renaming declaration. The actual is the name being renamed. We
11372 -- use the actual directly, rather than a copy, because it is not
11373 -- used further in the list of actuals, and because a copy or a use
11374 -- of relocate_node is incorrect if the instance is nested within a
11375 -- generic. In order to simplify e.g. ASIS queries, the
11376 -- Generic_Parent field links the declaration to the generic
11377 -- association.
11379 if No (Actual) then
11380 Error_Msg_NE
11381 ("missing actual &",
11382 Instantiation_Node, Gen_Obj);
11383 Error_Msg_NE
11384 ("\in instantiation of & declared#",
11385 Instantiation_Node, Scope (A_Gen_Obj));
11386 Abandon_Instantiation (Instantiation_Node);
11387 end if;
11389 if Present (Subt_Mark) then
11390 Decl_Node :=
11391 Make_Object_Renaming_Declaration (Loc,
11392 Defining_Identifier => New_Copy (Gen_Obj),
11393 Subtype_Mark => New_Copy_Tree (Subt_Mark),
11394 Name => Actual);
11396 else pragma Assert (Present (Acc_Def));
11397 Decl_Node :=
11398 Make_Object_Renaming_Declaration (Loc,
11399 Defining_Identifier => New_Copy (Gen_Obj),
11400 Access_Definition => New_Copy_Tree (Acc_Def),
11401 Name => Actual);
11402 end if;
11404 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
11406 -- The analysis of the actual may produce Insert_Action nodes, so
11407 -- the declaration must have a context in which to attach them.
11409 Append (Decl_Node, List);
11410 Analyze (Actual);
11412 -- Return if the analysis of the actual reported some error
11414 if Etype (Actual) = Any_Type then
11415 return List;
11416 end if;
11418 -- This check is performed here because Analyze_Object_Renaming will
11419 -- not check it when Comes_From_Source is False. Note though that the
11420 -- check for the actual being the name of an object will be performed
11421 -- in Analyze_Object_Renaming.
11423 if Is_Object_Reference (Actual)
11424 and then Is_Dependent_Component_Of_Mutable_Object (Actual)
11425 then
11426 Error_Msg_N
11427 ("illegal discriminant-dependent component for in out parameter",
11428 Actual);
11429 end if;
11431 -- The actual has to be resolved in order to check that it is a
11432 -- variable (due to cases such as F (1), where F returns access to
11433 -- an array, and for overloaded prefixes).
11435 Ftyp := Get_Instance_Of (Etype (A_Gen_Obj));
11437 -- If the type of the formal is not itself a formal, and the current
11438 -- unit is a child unit, the formal type must be declared in a
11439 -- parent, and must be retrieved by visibility.
11441 if Ftyp = Orig_Ftyp
11442 and then Is_Generic_Unit (Scope (Ftyp))
11443 and then Is_Child_Unit (Scope (A_Gen_Obj))
11444 then
11445 declare
11446 Temp : constant Node_Id :=
11447 New_Copy_Tree (Subtype_Mark (Analyzed_Formal));
11448 begin
11449 Set_Entity (Temp, Empty);
11450 Find_Type (Temp);
11451 Ftyp := Entity (Temp);
11452 end;
11453 end if;
11455 if Is_Private_Type (Ftyp)
11456 and then not Is_Private_Type (Etype (Actual))
11457 and then (Base_Type (Full_View (Ftyp)) = Base_Type (Etype (Actual))
11458 or else Base_Type (Etype (Actual)) = Ftyp)
11459 then
11460 -- If the actual has the type of the full view of the formal, or
11461 -- else a non-private subtype of the formal, then the visibility
11462 -- of the formal type has changed. Add to the actuals a subtype
11463 -- declaration that will force the exchange of views in the body
11464 -- of the instance as well.
11466 Subt_Decl :=
11467 Make_Subtype_Declaration (Loc,
11468 Defining_Identifier => Make_Temporary (Loc, 'P'),
11469 Subtype_Indication => New_Occurrence_Of (Ftyp, Loc));
11471 Prepend (Subt_Decl, List);
11473 Prepend_Elmt (Full_View (Ftyp), Exchanged_Views);
11474 Exchange_Declarations (Ftyp);
11475 end if;
11477 Resolve (Actual, Ftyp);
11479 if not Denotes_Variable (Actual) then
11480 Error_Msg_NE ("actual for& must be a variable", Actual, Gen_Obj);
11482 elsif Base_Type (Ftyp) /= Base_Type (Etype (Actual)) then
11484 -- Ada 2005 (AI-423): For a generic formal object of mode in out,
11485 -- the type of the actual shall resolve to a specific anonymous
11486 -- access type.
11488 if Ada_Version < Ada_2005
11489 or else not Is_Anonymous_Access_Type (Base_Type (Ftyp))
11490 or else not Is_Anonymous_Access_Type (Base_Type (Etype (Actual)))
11491 then
11492 Error_Msg_NE
11493 ("type of actual does not match type of&", Actual, Gen_Obj);
11494 end if;
11495 end if;
11497 Note_Possible_Modification (Actual, Sure => True);
11499 -- Check for instantiation with atomic/volatile/VFA object actual for
11500 -- nonatomic/nonvolatile/nonVFA formal (RM C.6 (12)).
11502 if Is_Atomic_Object (Actual) and then not Is_Atomic (Orig_Ftyp) then
11503 Error_Msg_NE
11504 ("cannot instantiate nonatomic formal & of mode in out",
11505 Actual, Gen_Obj);
11506 Error_Msg_N ("\with atomic object actual (RM C.6(12))", Actual);
11508 elsif Is_Volatile_Object_Ref (Actual)
11509 and then not Is_Volatile (Orig_Ftyp)
11510 then
11511 Error_Msg_NE
11512 ("cannot instantiate nonvolatile formal & of mode in out",
11513 Actual, Gen_Obj);
11514 Error_Msg_N ("\with volatile object actual (RM C.6(12))", Actual);
11516 elsif Is_Volatile_Full_Access_Object_Ref (Actual)
11517 and then not Is_Volatile_Full_Access (Orig_Ftyp)
11518 then
11519 Error_Msg_NE
11520 ("cannot instantiate nonfull access formal & of mode in out",
11521 Actual, Gen_Obj);
11522 Error_Msg_N
11523 ("\with full access object actual (RM C.6(12))", Actual);
11524 end if;
11526 -- Check for instantiation on nonatomic subcomponent of a full access
11527 -- object in Ada 2022 (RM C.6 (12)).
11529 if Ada_Version >= Ada_2022
11530 and then Is_Subcomponent_Of_Full_Access_Object (Actual)
11531 and then not Is_Atomic_Object (Actual)
11532 then
11533 Error_Msg_NE
11534 ("cannot instantiate formal & of mode in out with actual",
11535 Actual, Gen_Obj);
11536 Error_Msg_N
11537 ("\nonatomic subcomponent of full access object (RM C.6(12))",
11538 Actual);
11539 end if;
11541 -- Check actual/formal compatibility with respect to the four
11542 -- volatility refinement aspects.
11544 declare
11545 Actual_Obj : Entity_Id;
11546 N : Node_Id := Actual;
11547 begin
11548 -- Similar to Sem_Util.Get_Enclosing_Object, but treat
11549 -- pointer dereference like component selection.
11550 loop
11551 if Is_Entity_Name (N) then
11552 Actual_Obj := Entity (N);
11553 exit;
11554 end if;
11556 case Nkind (N) is
11557 when N_Indexed_Component
11558 | N_Selected_Component
11559 | N_Slice
11560 | N_Explicit_Dereference
11562 N := Prefix (N);
11564 when N_Type_Conversion =>
11565 N := Expression (N);
11567 when others =>
11568 Actual_Obj := Etype (N);
11569 exit;
11570 end case;
11571 end loop;
11573 Check_Volatility_Compatibility
11574 (Actual_Obj, A_Gen_Obj, "actual object",
11575 "its corresponding formal object of mode in out",
11576 Srcpos_Bearer => Actual);
11577 end;
11579 -- Formal in-parameter
11581 else
11582 -- The instantiation of a generic formal in-parameter is constant
11583 -- declaration. The actual is the expression for that declaration.
11584 -- Its type is a full copy of the type of the formal. This may be
11585 -- an access to subprogram, for which we need to generate entities
11586 -- for the formals in the new signature.
11588 if Present (Actual) then
11589 if Present (Subt_Mark) then
11590 Def := New_Copy_Tree (Subt_Mark);
11591 else
11592 pragma Assert (Present (Acc_Def));
11593 Def := New_Copy_Tree (Acc_Def);
11594 end if;
11596 Decl_Node :=
11597 Make_Object_Declaration (Loc,
11598 Defining_Identifier => New_Copy (Gen_Obj),
11599 Constant_Present => True,
11600 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
11601 Object_Definition => Def,
11602 Expression => Actual);
11604 Set_Corresponding_Generic_Association (Decl_Node, Act_Assoc);
11606 -- A generic formal object of a tagged type is defined to be
11607 -- aliased so the new constant must also be treated as aliased.
11609 if Is_Tagged_Type (Etype (A_Gen_Obj)) then
11610 Set_Aliased_Present (Decl_Node);
11611 end if;
11613 Append (Decl_Node, List);
11615 -- No need to repeat (pre-)analysis of some expression nodes
11616 -- already handled in Preanalyze_Actuals.
11618 if Nkind (Actual) /= N_Allocator then
11619 Analyze (Actual);
11621 -- Return if the analysis of the actual reported some error
11623 if Etype (Actual) = Any_Type then
11624 return List;
11625 end if;
11626 end if;
11628 declare
11629 Formal_Type : constant Entity_Id := Etype (A_Gen_Obj);
11630 Typ : Entity_Id;
11632 begin
11633 Typ := Get_Instance_Of (Formal_Type);
11635 -- If the actual appears in the current or an enclosing scope,
11636 -- use its type directly. This is relevant if it has an actual
11637 -- subtype that is distinct from its nominal one. This cannot
11638 -- be done in general because the type of the actual may
11639 -- depend on other actuals, and only be fully determined when
11640 -- the enclosing instance is analyzed.
11642 if Present (Etype (Actual))
11643 and then Is_Constr_Subt_For_U_Nominal (Etype (Actual))
11644 then
11645 Freeze_Before (Instantiation_Node, Etype (Actual));
11646 else
11647 Freeze_Before (Instantiation_Node, Typ);
11648 end if;
11650 -- If the actual is an aggregate, perform name resolution on
11651 -- its components (the analysis of an aggregate does not do it)
11652 -- to capture local names that may be hidden if the generic is
11653 -- a child unit.
11655 if Nkind (Actual) = N_Aggregate then
11656 Preanalyze_And_Resolve (Actual, Typ);
11657 end if;
11659 if Is_Limited_Type (Typ)
11660 and then not OK_For_Limited_Init (Typ, Actual)
11661 then
11662 Error_Msg_N
11663 ("initialization not allowed for limited types", Actual);
11664 Explain_Limited_Type (Typ, Actual);
11665 end if;
11666 end;
11668 elsif Present (Default_Expression (Formal)) then
11670 -- Use default to construct declaration
11672 if Present (Subt_Mark) then
11673 Def := New_Copy_Tree (Subt_Mark);
11674 else
11675 pragma Assert (Present (Acc_Def));
11676 Def := New_Copy_Tree (Acc_Def);
11677 end if;
11679 Decl_Node :=
11680 Make_Object_Declaration (Sloc (Formal),
11681 Defining_Identifier => New_Copy (Gen_Obj),
11682 Constant_Present => True,
11683 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
11684 Object_Definition => Def,
11685 Expression => New_Copy_Tree
11686 (Default_Expression (Formal)));
11688 Set_Corresponding_Generic_Association
11689 (Decl_Node, Expression (Decl_Node));
11691 Append (Decl_Node, List);
11692 Set_Analyzed (Expression (Decl_Node), False);
11694 else
11695 Error_Msg_NE ("missing actual&", Instantiation_Node, Gen_Obj);
11696 Error_Msg_NE ("\in instantiation of & declared#",
11697 Instantiation_Node, Scope (A_Gen_Obj));
11699 if Is_Scalar_Type (Etype (A_Gen_Obj)) then
11701 -- Create dummy constant declaration so that instance can be
11702 -- analyzed, to minimize cascaded visibility errors.
11704 if Present (Subt_Mark) then
11705 Def := Subt_Mark;
11706 else pragma Assert (Present (Acc_Def));
11707 Def := Acc_Def;
11708 end if;
11710 Decl_Node :=
11711 Make_Object_Declaration (Loc,
11712 Defining_Identifier => New_Copy (Gen_Obj),
11713 Constant_Present => True,
11714 Null_Exclusion_Present => Null_Exclusion_Present (Formal),
11715 Object_Definition => New_Copy (Def),
11716 Expression =>
11717 Make_Attribute_Reference (Sloc (Gen_Obj),
11718 Attribute_Name => Name_First,
11719 Prefix => New_Copy (Def)));
11721 Append (Decl_Node, List);
11723 else
11724 Abandon_Instantiation (Instantiation_Node);
11725 end if;
11726 end if;
11727 end if;
11729 if Nkind (Actual) in N_Has_Entity
11730 and then Present (Entity (Actual))
11731 then
11732 Actual_Decl := Parent (Entity (Actual));
11733 end if;
11735 -- Ada 2005 (AI-423) refined by AI12-0287:
11736 -- For an object_renaming_declaration with a null_exclusion or an
11737 -- access_definition that has a null_exclusion, the subtype of the
11738 -- object_name shall exclude null. In addition, if the
11739 -- object_renaming_declaration occurs within the body of a generic unit
11740 -- G or within the body of a generic unit declared within the
11741 -- declarative region of generic unit G, then:
11742 -- * if the object_name statically denotes a generic formal object of
11743 -- mode in out of G, then the declaration of that object shall have a
11744 -- null_exclusion;
11745 -- * if the object_name statically denotes a call of a generic formal
11746 -- function of G, then the declaration of the result of that function
11747 -- shall have a null_exclusion.
11749 if Ada_Version >= Ada_2005
11750 and then Present (Actual_Decl)
11751 and then Nkind (Actual_Decl) in N_Formal_Object_Declaration
11752 | N_Object_Declaration
11753 and then Nkind (Analyzed_Formal) = N_Formal_Object_Declaration
11754 and then not Has_Null_Exclusion (Actual_Decl)
11755 and then Has_Null_Exclusion (Analyzed_Formal)
11756 and then Ekind (Defining_Identifier (Analyzed_Formal))
11757 = E_Generic_In_Out_Parameter
11758 and then ((In_Generic_Scope (Entity (Actual))
11759 and then In_Package_Body (Scope (Entity (Actual))))
11760 or else not Can_Never_Be_Null (Etype (Actual)))
11761 then
11762 Error_Msg_Sloc := Sloc (Analyzed_Formal);
11763 Error_Msg_N
11764 ("actual must exclude null to match generic formal#", Actual);
11765 end if;
11767 -- An effectively volatile object cannot be used as an actual in a
11768 -- generic instantiation (SPARK RM 7.1.3(7)). The following check is
11769 -- relevant only when SPARK_Mode is on as it is not a standard Ada
11770 -- legality rule, and also verifies that the actual is an object.
11772 if SPARK_Mode = On
11773 and then Present (Actual)
11774 and then Is_Object_Reference (Actual)
11775 and then Is_Effectively_Volatile_Object (Actual)
11776 and then not Is_Effectively_Volatile (A_Gen_Obj)
11777 then
11778 Error_Msg_N
11779 ("volatile object cannot act as actual in generic instantiation",
11780 Actual);
11781 end if;
11783 return List;
11784 end Instantiate_Object;
11786 ------------------------------
11787 -- Instantiate_Package_Body --
11788 ------------------------------
11790 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
11791 -- must be replaced by gotos which jump to the end of the routine in order
11792 -- to restore the Ghost and SPARK modes.
11794 procedure Instantiate_Package_Body
11795 (Body_Info : Pending_Body_Info;
11796 Inlined_Body : Boolean := False;
11797 Body_Optional : Boolean := False)
11799 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
11800 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Decl);
11801 Act_Spec : constant Node_Id := Specification (Act_Decl);
11802 Ctx_Parents : Elist_Id := No_Elist;
11803 Ctx_Top : Int := 0;
11804 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
11805 Gen_Id : constant Node_Id := Name (Inst_Node);
11806 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
11807 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
11808 Loc : constant Source_Ptr := Sloc (Inst_Node);
11810 procedure Check_Initialized_Types;
11811 -- In a generic package body, an entity of a generic private type may
11812 -- appear uninitialized. This is suspicious, unless the actual is a
11813 -- fully initialized type.
11815 procedure Install_Parents_Of_Generic_Context
11816 (Inst_Scope : Entity_Id;
11817 Ctx_Parents : out Elist_Id);
11818 -- Inst_Scope is the scope where the instance appears within; when it
11819 -- appears within a generic child package G, this routine collects and
11820 -- installs the enclosing packages of G in the scopes stack; installed
11821 -- packages are returned in Ctx_Parents.
11823 procedure Remove_Parents_Of_Generic_Context (Ctx_Parents : Elist_Id);
11824 -- Reverse effect after instantiation is complete
11826 -----------------------------
11827 -- Check_Initialized_Types --
11828 -----------------------------
11830 procedure Check_Initialized_Types is
11831 Decl : Node_Id;
11832 Formal : Entity_Id;
11833 Actual : Entity_Id;
11834 Uninit_Var : Entity_Id;
11836 begin
11837 Decl := First (Generic_Formal_Declarations (Gen_Decl));
11838 while Present (Decl) loop
11839 Uninit_Var := Empty;
11841 if Nkind (Decl) = N_Private_Extension_Declaration then
11842 Uninit_Var := Uninitialized_Variable (Decl);
11844 elsif Nkind (Decl) = N_Formal_Type_Declaration
11845 and then Nkind (Formal_Type_Definition (Decl)) =
11846 N_Formal_Private_Type_Definition
11847 then
11848 Uninit_Var :=
11849 Uninitialized_Variable (Formal_Type_Definition (Decl));
11850 end if;
11852 if Present (Uninit_Var) then
11853 Formal := Defining_Identifier (Decl);
11854 Actual := First_Entity (Act_Decl_Id);
11856 -- For each formal there is a subtype declaration that renames
11857 -- the actual and has the same name as the formal. Locate the
11858 -- formal for warning message about uninitialized variables
11859 -- in the generic, for which the actual type should be a fully
11860 -- initialized type.
11862 while Present (Actual) loop
11863 exit when Ekind (Actual) = E_Package
11864 and then Present (Renamed_Entity (Actual));
11866 if Chars (Actual) = Chars (Formal)
11867 and then not Is_Scalar_Type (Actual)
11868 and then not Is_Fully_Initialized_Type (Actual)
11869 and then Warn_On_No_Value_Assigned
11870 then
11871 Error_Msg_Node_2 := Formal;
11872 Error_Msg_NE
11873 ("generic unit has uninitialized variable& of "
11874 & "formal private type &?v?", Actual, Uninit_Var);
11875 Error_Msg_NE
11876 ("actual type for& should be fully initialized type?v?",
11877 Actual, Formal);
11878 exit;
11879 end if;
11881 Next_Entity (Actual);
11882 end loop;
11883 end if;
11885 Next (Decl);
11886 end loop;
11887 end Check_Initialized_Types;
11889 ----------------------------------------
11890 -- Install_Parents_Of_Generic_Context --
11891 ----------------------------------------
11893 procedure Install_Parents_Of_Generic_Context
11894 (Inst_Scope : Entity_Id;
11895 Ctx_Parents : out Elist_Id)
11897 Elmt : Elmt_Id;
11898 S : Entity_Id;
11900 begin
11901 Ctx_Parents := New_Elmt_List;
11903 -- Collect context parents (ie. parents where the instantiation
11904 -- appears within).
11906 S := Inst_Scope;
11907 while S /= Standard_Standard loop
11908 Prepend_Elmt (S, Ctx_Parents);
11909 S := Scope (S);
11910 end loop;
11912 -- Install enclosing parents
11914 Elmt := First_Elmt (Ctx_Parents);
11915 while Present (Elmt) loop
11916 Push_Scope (Node (Elmt));
11917 Set_Is_Immediately_Visible (Node (Elmt));
11918 Next_Elmt (Elmt);
11919 end loop;
11920 end Install_Parents_Of_Generic_Context;
11922 ---------------------------------------
11923 -- Remove_Parents_Of_Generic_Context --
11924 ---------------------------------------
11926 procedure Remove_Parents_Of_Generic_Context (Ctx_Parents : Elist_Id) is
11927 Elmt : Elmt_Id;
11929 begin
11930 -- Traverse Ctx_Parents in LIFO order to check the removed scopes
11932 Elmt := Last_Elmt (Ctx_Parents);
11933 while Present (Elmt) loop
11934 pragma Assert (Current_Scope = Node (Elmt));
11935 Set_Is_Immediately_Visible (Current_Scope, False);
11936 Pop_Scope;
11938 Remove_Last_Elmt (Ctx_Parents);
11939 Elmt := Last_Elmt (Ctx_Parents);
11940 end loop;
11941 end Remove_Parents_Of_Generic_Context;
11943 -- Local variables
11945 -- The following constants capture the context prior to instantiating
11946 -- the package body.
11948 Saved_CS : constant Config_Switches_Type := Save_Config_Switches;
11949 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
11950 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
11951 Saved_ISMP : constant Boolean :=
11952 Ignore_SPARK_Mode_Pragmas_In_Instance;
11953 Saved_LSST : constant Suppress_Stack_Entry_Ptr :=
11954 Local_Suppress_Stack_Top;
11955 Saved_SC : constant Boolean := Style_Check;
11956 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
11957 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
11958 Saved_SS : constant Suppress_Record := Scope_Suppress;
11959 Saved_Warn : constant Warning_Record := Save_Warnings;
11961 Act_Body : Node_Id;
11962 Act_Body_Id : Entity_Id;
11963 Act_Body_Name : Node_Id;
11964 Gen_Body : Node_Id;
11965 Gen_Body_Id : Node_Id;
11966 Par_Ent : Entity_Id := Empty;
11967 Par_Installed : Boolean := False;
11968 Par_Vis : Boolean := False;
11970 Scope_Check_Id : Entity_Id;
11971 Scope_Check_Last : Nat;
11972 -- Value of Current_Scope before calls to Install_Parents; used to check
11973 -- that scopes are correctly removed after instantiation.
11975 Vis_Prims_List : Elist_Id := No_Elist;
11976 -- List of primitives made temporarily visible in the instantiation
11977 -- to match the visibility of the formal type.
11979 -- Start of processing for Instantiate_Package_Body
11981 begin
11982 Gen_Body_Id := Corresponding_Body (Gen_Decl);
11984 -- The instance body may already have been processed, as the parent of
11985 -- another instance that is inlined (Load_Parent_Of_Generic).
11987 if Present (Corresponding_Body (Instance_Spec (Inst_Node))) then
11988 return;
11989 end if;
11991 -- The package being instantiated may be subject to pragma Ghost. Set
11992 -- the mode now to ensure that any nodes generated during instantiation
11993 -- are properly marked as Ghost.
11995 Set_Ghost_Mode (Act_Decl_Id);
11997 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
11999 -- Re-establish the state of information on which checks are suppressed.
12000 -- This information was set in Body_Info at the point of instantiation,
12001 -- and now we restore it so that the instance is compiled using the
12002 -- check status at the instantiation (RM 11.5(7.2/2), AI95-00224-01).
12004 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
12005 Scope_Suppress := Body_Info.Scope_Suppress;
12007 Restore_Config_Switches (Body_Info.Config_Switches);
12008 Restore_Warnings (Body_Info.Warnings);
12010 if No (Gen_Body_Id) then
12012 -- Do not look for parent of generic body if none is required.
12013 -- This may happen when the routine is called as part of the
12014 -- Pending_Instantiations processing, when nested instances
12015 -- may precede the one generated from the main unit.
12017 if not Unit_Requires_Body (Defining_Entity (Gen_Decl))
12018 and then Body_Optional
12019 then
12020 goto Leave;
12021 else
12022 Load_Parent_Of_Generic
12023 (Inst_Node, Specification (Gen_Decl), Body_Optional);
12025 -- Surprisingly enough, loading the body of the parent can cause
12026 -- the body to be instantiated and the double instantiation needs
12027 -- to be prevented in order to avoid giving bogus semantic errors.
12029 -- This case can occur because of the Collect_Previous_Instances
12030 -- machinery of Load_Parent_Of_Generic, which will instantiate
12031 -- bodies that are deemed to be ahead of the body of the parent
12032 -- in the compilation unit. But the relative position of these
12033 -- bodies is computed using the mere comparison of their Sloc.
12035 -- Now suppose that you have two generic packages G and H, with
12036 -- G containing a mere instantiation of H:
12038 -- generic
12039 -- package H is
12041 -- generic
12042 -- package Nested_G is
12043 -- ...
12044 -- end Nested_G;
12046 -- end H;
12048 -- with H;
12050 -- generic
12051 -- package G is
12053 -- package My_H is new H;
12055 -- end G;
12057 -- and a third package Q instantiating G and Nested_G:
12059 -- with G;
12061 -- package Q is
12063 -- package My_G is new G;
12065 -- package My_Nested_G is new My_G.My_H.Nested_G;
12067 -- end Q;
12069 -- The body to be instantiated is that of My_Nested_G and its
12070 -- parent is the instance My_G.My_H. This latter instantiation
12071 -- is done when My_G is analyzed, i.e. after the declarations
12072 -- of My_G and My_Nested_G have been parsed; as a result, the
12073 -- Sloc of My_G.My_H is greater than the Sloc of My_Nested_G.
12075 -- Therefore loading the body of My_G.My_H will cause the body
12076 -- of My_Nested_G to be instantiated because it is deemed to be
12077 -- ahead of My_G.My_H. This means that Load_Parent_Of_Generic
12078 -- will again be invoked on My_G.My_H, but this time with the
12079 -- Collect_Previous_Instances machinery disabled, so there is
12080 -- no endless mutual recursion and things are done in order.
12082 if Present (Corresponding_Body (Instance_Spec (Inst_Node))) then
12083 goto Leave;
12084 end if;
12086 Gen_Body_Id := Corresponding_Body (Gen_Decl);
12087 end if;
12088 end if;
12090 -- Establish global variable for sloc adjustment and for error recovery
12091 -- In the case of an instance body for an instantiation with actuals
12092 -- from a limited view, the instance body is placed at the beginning
12093 -- of the enclosing package body: use the body entity as the source
12094 -- location for nodes of the instance body.
12096 if not Is_Empty_Elmt_List (Incomplete_Actuals (Act_Decl_Id)) then
12097 declare
12098 Scop : constant Entity_Id := Scope (Act_Decl_Id);
12099 Body_Id : constant Node_Id :=
12100 Corresponding_Body (Unit_Declaration_Node (Scop));
12102 begin
12103 Instantiation_Node := Body_Id;
12104 end;
12105 else
12106 Instantiation_Node := Inst_Node;
12107 end if;
12109 if Present (Gen_Body_Id) then
12110 Save_Env (Gen_Unit, Act_Decl_Id);
12111 Style_Check := False;
12113 -- If the context of the instance is subject to SPARK_Mode "off", the
12114 -- annotation is missing, or the body is instantiated at a later pass
12115 -- and its spec ignored SPARK_Mode pragma, set the global flag which
12116 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within the
12117 -- instance.
12119 if SPARK_Mode /= On
12120 or else Ignore_SPARK_Mode_Pragmas (Act_Decl_Id)
12121 then
12122 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
12123 end if;
12125 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
12126 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
12128 Create_Instantiation_Source
12129 (Inst_Node, Gen_Body_Id, S_Adjustment);
12131 Act_Body :=
12132 Copy_Generic_Node
12133 (Original_Node (Gen_Body), Empty, Instantiating => True);
12135 -- Create proper (possibly qualified) defining name for the body, to
12136 -- correspond to the one in the spec.
12138 Act_Body_Id :=
12139 Make_Defining_Identifier (Sloc (Act_Decl_Id), Chars (Act_Decl_Id));
12140 Preserve_Comes_From_Source (Act_Body_Id, Act_Decl_Id);
12142 -- Some attributes of spec entity are not inherited by body entity
12144 Set_Handler_Records (Act_Body_Id, No_List);
12146 if Nkind (Defining_Unit_Name (Act_Spec)) =
12147 N_Defining_Program_Unit_Name
12148 then
12149 Act_Body_Name :=
12150 Make_Defining_Program_Unit_Name (Loc,
12151 Name =>
12152 New_Copy_Tree (Name (Defining_Unit_Name (Act_Spec))),
12153 Defining_Identifier => Act_Body_Id);
12154 else
12155 Act_Body_Name := Act_Body_Id;
12156 end if;
12158 Set_Defining_Unit_Name (Act_Body, Act_Body_Name);
12160 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
12161 Check_Generic_Actuals (Act_Decl_Id, False);
12162 Check_Initialized_Types;
12164 -- Install primitives hidden at the point of the instantiation but
12165 -- visible when processing the generic formals
12167 declare
12168 E : Entity_Id;
12170 begin
12171 E := First_Entity (Act_Decl_Id);
12172 while Present (E) loop
12173 if Is_Type (E)
12174 and then not Is_Itype (E)
12175 and then Is_Generic_Actual_Type (E)
12176 and then Is_Tagged_Type (E)
12177 then
12178 Install_Hidden_Primitives
12179 (Prims_List => Vis_Prims_List,
12180 Gen_T => Generic_Parent_Type (Parent (E)),
12181 Act_T => E);
12182 end if;
12184 Next_Entity (E);
12185 end loop;
12186 end;
12188 Scope_Check_Id := Current_Scope;
12189 Scope_Check_Last := Scope_Stack.Last;
12191 -- If the instantiation appears within a generic child some actual
12192 -- parameter may be the current instance of the enclosing generic
12193 -- parent.
12195 declare
12196 Inst_Scope : constant Entity_Id := Scope (Act_Decl_Id);
12198 begin
12199 if Is_Child_Unit (Inst_Scope)
12200 and then Ekind (Inst_Scope) = E_Generic_Package
12201 and then Present (Generic_Associations (Inst_Node))
12202 then
12203 Install_Parents_Of_Generic_Context (Inst_Scope, Ctx_Parents);
12205 -- Hide them from visibility; required to avoid conflicts
12206 -- installing the parent instance.
12208 if Present (Ctx_Parents) then
12209 Push_Scope (Standard_Standard);
12210 Ctx_Top := Scope_Stack.Last;
12211 Scope_Stack.Table (Ctx_Top).Is_Active_Stack_Base := True;
12212 end if;
12213 end if;
12214 end;
12216 -- If it is a child unit, make the parent instance (which is an
12217 -- instance of the parent of the generic) visible. The parent
12218 -- instance is the prefix of the name of the generic unit.
12220 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
12221 and then Nkind (Gen_Id) = N_Expanded_Name
12222 then
12223 Par_Ent := Entity (Prefix (Gen_Id));
12224 Par_Vis := Is_Immediately_Visible (Par_Ent);
12225 Install_Parent (Par_Ent, In_Body => True);
12226 Par_Installed := True;
12228 elsif Is_Child_Unit (Gen_Unit) then
12229 Par_Ent := Scope (Gen_Unit);
12230 Par_Vis := Is_Immediately_Visible (Par_Ent);
12231 Install_Parent (Par_Ent, In_Body => True);
12232 Par_Installed := True;
12233 end if;
12235 -- If the instantiation is a library unit, and this is the main unit,
12236 -- then build the resulting compilation unit nodes for the instance.
12237 -- If this is a compilation unit but it is not the main unit, then it
12238 -- is the body of a unit in the context, that is being compiled
12239 -- because it is encloses some inlined unit or another generic unit
12240 -- being instantiated. In that case, this body is not part of the
12241 -- current compilation, and is not attached to the tree, but its
12242 -- parent must be set for analysis.
12244 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
12246 -- Replace instance node with body of instance, and create new
12247 -- node for corresponding instance declaration.
12249 Build_Instance_Compilation_Unit_Nodes
12250 (Inst_Node, Act_Body, Act_Decl);
12252 -- If the instantiation appears within a generic child package
12253 -- enable visibility of current instance of enclosing generic
12254 -- parents.
12256 if Present (Ctx_Parents) then
12257 Scope_Stack.Table (Ctx_Top).Is_Active_Stack_Base := False;
12258 Analyze (Inst_Node);
12259 Scope_Stack.Table (Ctx_Top).Is_Active_Stack_Base := True;
12260 else
12261 Analyze (Inst_Node);
12262 end if;
12264 if Parent (Inst_Node) = Cunit (Main_Unit) then
12266 -- If the instance is a child unit itself, then set the scope
12267 -- of the expanded body to be the parent of the instantiation
12268 -- (ensuring that the fully qualified name will be generated
12269 -- for the elaboration subprogram).
12271 if Nkind (Defining_Unit_Name (Act_Spec)) =
12272 N_Defining_Program_Unit_Name
12273 then
12274 Set_Scope (Defining_Entity (Inst_Node), Scope (Act_Decl_Id));
12275 end if;
12276 end if;
12278 -- Case where instantiation is not a library unit
12280 else
12281 -- Handle the case of an instance with incomplete actual types.
12282 -- The instance body cannot be placed just after the declaration
12283 -- because full views have not been seen yet. Any use of the non-
12284 -- limited views in the instance body requires the presence of a
12285 -- regular with_clause in the enclosing unit. Therefore we place
12286 -- the instance body at the beginning of the enclosing body, and
12287 -- the freeze node for the instance is then placed after the body.
12289 if not Is_Empty_Elmt_List (Incomplete_Actuals (Act_Decl_Id))
12290 and then Ekind (Scope (Act_Decl_Id)) = E_Package
12291 then
12292 declare
12293 Scop : constant Entity_Id := Scope (Act_Decl_Id);
12294 Body_Id : constant Node_Id :=
12295 Corresponding_Body (Unit_Declaration_Node (Scop));
12297 F_Node : Node_Id;
12299 begin
12300 pragma Assert (Present (Body_Id));
12302 Prepend (Act_Body, Declarations (Parent (Body_Id)));
12304 if Expander_Active then
12305 Ensure_Freeze_Node (Act_Decl_Id);
12306 F_Node := Freeze_Node (Act_Decl_Id);
12307 Set_Is_Frozen (Act_Decl_Id, False);
12308 if Is_List_Member (F_Node) then
12309 Remove (F_Node);
12310 end if;
12312 Insert_After (Act_Body, F_Node);
12313 end if;
12314 end;
12316 else
12317 Insert_Before (Inst_Node, Act_Body);
12318 Mark_Rewrite_Insertion (Act_Body);
12320 -- Insert the freeze node for the instance if need be
12322 if Expander_Active then
12323 Freeze_Package_Instance
12324 (Inst_Node, Gen_Body, Gen_Decl, Act_Decl_Id);
12325 Set_Is_Frozen (Act_Decl_Id);
12326 end if;
12327 end if;
12329 -- If the instantiation appears within a generic child package
12330 -- enable visibility of current instance of enclosing generic
12331 -- parents.
12333 if Present (Ctx_Parents) then
12334 Scope_Stack.Table (Ctx_Top).Is_Active_Stack_Base := False;
12335 Analyze (Act_Body);
12336 Scope_Stack.Table (Ctx_Top).Is_Active_Stack_Base := True;
12337 else
12338 Analyze (Act_Body);
12339 end if;
12340 end if;
12342 Inherit_Context (Gen_Body, Inst_Node);
12344 if Par_Installed then
12345 Remove_Parent (In_Body => True);
12347 -- Restore the previous visibility of the parent
12349 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
12350 end if;
12352 -- Remove the parent instances if they have been placed on the scope
12353 -- stack to compile the body.
12355 if Present (Ctx_Parents) then
12356 pragma Assert (Scope_Stack.Last = Ctx_Top
12357 and then Current_Scope = Standard_Standard);
12358 Pop_Scope;
12360 Remove_Parents_Of_Generic_Context (Ctx_Parents);
12361 end if;
12363 pragma Assert (Current_Scope = Scope_Check_Id);
12364 pragma Assert (Scope_Stack.Last = Scope_Check_Last);
12366 Restore_Hidden_Primitives (Vis_Prims_List);
12368 -- Restore the private views that were made visible when the body of
12369 -- the instantiation was created. Note that, in the case where one of
12370 -- these private views is declared in the parent, there is a nesting
12371 -- issue with the calls to Install_Parent and Remove_Parent made in
12372 -- between above with In_Body set to True, because these calls also
12373 -- want to swap and restore this private view respectively. In this
12374 -- case, the call to Install_Parent does nothing, but the call to
12375 -- Remove_Parent does restore the private view, thus undercutting the
12376 -- call to Restore_Private_Views. That's OK under the condition that
12377 -- the two mechanisms swap exactly the same entities, in particular
12378 -- the private entities dependent on the primary private entities.
12380 Restore_Private_Views (Act_Decl_Id);
12382 -- Remove the current unit from visibility if this is an instance
12383 -- that is not elaborated on the fly for inlining purposes.
12385 if not Inlined_Body then
12386 Set_Is_Immediately_Visible (Act_Decl_Id, False);
12387 end if;
12389 Restore_Env;
12391 -- If we have no body, and the unit requires a body, then complain. This
12392 -- complaint is suppressed if we have detected other errors (since a
12393 -- common reason for missing the body is that it had errors).
12394 -- In CodePeer mode, a warning has been emitted already, no need for
12395 -- further messages.
12397 elsif Unit_Requires_Body (Gen_Unit)
12398 and then not Body_Optional
12399 then
12400 if CodePeer_Mode then
12401 null;
12403 elsif Serious_Errors_Detected = 0 then
12404 Error_Msg_NE
12405 ("cannot find body of generic package &", Inst_Node, Gen_Unit);
12407 -- Don't attempt to perform any cleanup actions if some other error
12408 -- was already detected, since this can cause blowups.
12410 else
12411 goto Leave;
12412 end if;
12414 -- Case of package that does not need a body
12416 else
12417 -- If the instantiation of the declaration is a library unit, rewrite
12418 -- the original package instantiation as a package declaration in the
12419 -- compilation unit node.
12421 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
12422 Set_Parent_Spec (Act_Decl, Parent_Spec (Inst_Node));
12423 Rewrite (Inst_Node, Act_Decl);
12425 -- Generate elaboration entity, in case spec has elaboration code.
12426 -- This cannot be done when the instance is analyzed, because it
12427 -- is not known yet whether the body exists.
12429 Set_Elaboration_Entity_Required (Act_Decl_Id, False);
12430 Build_Elaboration_Entity (Parent (Inst_Node), Act_Decl_Id);
12432 -- If the instantiation is not a library unit, then append the
12433 -- declaration to the list of implicitly generated entities, unless
12434 -- it is already a list member which means that it was already
12435 -- processed
12437 elsif not Is_List_Member (Act_Decl) then
12438 Mark_Rewrite_Insertion (Act_Decl);
12439 Insert_Before (Inst_Node, Act_Decl);
12440 end if;
12441 end if;
12443 <<Leave>>
12445 -- Restore the context that was in effect prior to instantiating the
12446 -- package body.
12448 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
12449 Local_Suppress_Stack_Top := Saved_LSST;
12450 Scope_Suppress := Saved_SS;
12451 Style_Check := Saved_SC;
12453 Expander_Mode_Restore;
12454 Restore_Config_Switches (Saved_CS);
12455 Restore_Ghost_Region (Saved_GM, Saved_IGR);
12456 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
12457 Restore_Warnings (Saved_Warn);
12458 end Instantiate_Package_Body;
12460 ---------------------------------
12461 -- Instantiate_Subprogram_Body --
12462 ---------------------------------
12464 -- WARNING: This routine manages Ghost and SPARK regions. Return statements
12465 -- must be replaced by gotos which jump to the end of the routine in order
12466 -- to restore the Ghost and SPARK modes.
12468 procedure Instantiate_Subprogram_Body
12469 (Body_Info : Pending_Body_Info;
12470 Body_Optional : Boolean := False)
12472 Act_Decl : constant Node_Id := Body_Info.Act_Decl;
12473 Act_Decl_Id : constant Entity_Id := Defining_Entity (Act_Decl);
12474 Inst_Node : constant Node_Id := Body_Info.Inst_Node;
12475 Gen_Id : constant Node_Id := Name (Inst_Node);
12476 Gen_Unit : constant Entity_Id := Get_Generic_Entity (Inst_Node);
12477 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Unit);
12478 Loc : constant Source_Ptr := Sloc (Inst_Node);
12479 Pack_Id : constant Entity_Id :=
12480 Defining_Unit_Name (Parent (Act_Decl));
12482 -- The following constants capture the context prior to instantiating
12483 -- the subprogram body.
12485 Saved_CS : constant Config_Switches_Type := Save_Config_Switches;
12486 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
12487 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
12488 Saved_ISMP : constant Boolean :=
12489 Ignore_SPARK_Mode_Pragmas_In_Instance;
12490 Saved_LSST : constant Suppress_Stack_Entry_Ptr :=
12491 Local_Suppress_Stack_Top;
12492 Saved_SC : constant Boolean := Style_Check;
12493 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
12494 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
12495 Saved_SS : constant Suppress_Record := Scope_Suppress;
12496 Saved_Warn : constant Warning_Record := Save_Warnings;
12498 Act_Body : Node_Id;
12499 Act_Body_Id : Entity_Id;
12500 Gen_Body : Node_Id;
12501 Gen_Body_Id : Node_Id;
12502 Pack_Body : Node_Id;
12503 Par_Ent : Entity_Id := Empty;
12504 Par_Installed : Boolean := False;
12505 Par_Vis : Boolean := False;
12506 Ret_Expr : Node_Id;
12508 begin
12509 Gen_Body_Id := Corresponding_Body (Gen_Decl);
12511 -- Subprogram body may have been created already because of an inline
12512 -- pragma, or because of multiple elaborations of the enclosing package
12513 -- when several instances of the subprogram appear in the main unit.
12515 if Present (Corresponding_Body (Act_Decl)) then
12516 return;
12517 end if;
12519 -- The subprogram being instantiated may be subject to pragma Ghost. Set
12520 -- the mode now to ensure that any nodes generated during instantiation
12521 -- are properly marked as Ghost.
12523 Set_Ghost_Mode (Act_Decl_Id);
12525 Expander_Mode_Save_And_Set (Body_Info.Expander_Status);
12527 -- Re-establish the state of information on which checks are suppressed.
12528 -- This information was set in Body_Info at the point of instantiation,
12529 -- and now we restore it so that the instance is compiled using the
12530 -- check status at the instantiation (RM 11.5(7.2/2), AI95-00224-01).
12532 Local_Suppress_Stack_Top := Body_Info.Local_Suppress_Stack_Top;
12533 Scope_Suppress := Body_Info.Scope_Suppress;
12535 Restore_Config_Switches (Body_Info.Config_Switches);
12536 Restore_Warnings (Body_Info.Warnings);
12538 if No (Gen_Body_Id) then
12540 -- For imported generic subprogram, no body to compile, complete
12541 -- the spec entity appropriately.
12543 if Is_Imported (Gen_Unit) then
12544 Set_Is_Imported (Act_Decl_Id);
12545 Set_First_Rep_Item (Act_Decl_Id, First_Rep_Item (Gen_Unit));
12546 Set_Interface_Name (Act_Decl_Id, Interface_Name (Gen_Unit));
12547 Set_Convention (Act_Decl_Id, Convention (Gen_Unit));
12548 Set_Has_Completion (Act_Decl_Id);
12549 goto Leave;
12551 -- For other cases, compile the body
12553 else
12554 Load_Parent_Of_Generic
12555 (Inst_Node, Specification (Gen_Decl), Body_Optional);
12556 Gen_Body_Id := Corresponding_Body (Gen_Decl);
12557 end if;
12558 end if;
12560 Instantiation_Node := Inst_Node;
12562 if Present (Gen_Body_Id) then
12563 Gen_Body := Unit_Declaration_Node (Gen_Body_Id);
12565 if Nkind (Gen_Body) = N_Subprogram_Body_Stub then
12567 -- Either body is not present, or context is non-expanding, as
12568 -- when compiling a subunit. Mark the instance as completed, and
12569 -- diagnose a missing body when needed.
12571 if Expander_Active
12572 and then Operating_Mode = Generate_Code
12573 then
12574 Error_Msg_N ("missing proper body for instantiation", Gen_Body);
12575 end if;
12577 Set_Has_Completion (Act_Decl_Id);
12578 goto Leave;
12579 end if;
12581 Save_Env (Gen_Unit, Act_Decl_Id);
12582 Style_Check := False;
12584 -- If the context of the instance is subject to SPARK_Mode "off", the
12585 -- annotation is missing, or the body is instantiated at a later pass
12586 -- and its spec ignored SPARK_Mode pragma, set the global flag which
12587 -- signals Analyze_Pragma to ignore all SPARK_Mode pragmas within the
12588 -- instance.
12590 if SPARK_Mode /= On
12591 or else Ignore_SPARK_Mode_Pragmas (Act_Decl_Id)
12592 then
12593 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
12594 end if;
12596 -- If the context of an instance is not subject to SPARK_Mode "off",
12597 -- and the generic body is subject to an explicit SPARK_Mode pragma,
12598 -- the latter should be the one applicable to the instance.
12600 if not Ignore_SPARK_Mode_Pragmas_In_Instance
12601 and then SPARK_Mode /= Off
12602 and then Present (SPARK_Pragma (Gen_Body_Id))
12603 then
12604 Set_SPARK_Mode (Gen_Body_Id);
12605 end if;
12607 Current_Sem_Unit := Body_Info.Current_Sem_Unit;
12608 Create_Instantiation_Source
12609 (Inst_Node,
12610 Gen_Body_Id,
12611 S_Adjustment);
12613 Act_Body :=
12614 Copy_Generic_Node
12615 (Original_Node (Gen_Body), Empty, Instantiating => True);
12617 -- Create proper defining name for the body, to correspond to the one
12618 -- in the spec.
12620 Act_Body_Id :=
12621 Make_Defining_Identifier (Sloc (Act_Decl_Id), Chars (Act_Decl_Id));
12623 Preserve_Comes_From_Source (Act_Body_Id, Act_Decl_Id);
12624 Set_Defining_Unit_Name (Specification (Act_Body), Act_Body_Id);
12626 Set_Corresponding_Spec (Act_Body, Act_Decl_Id);
12627 Set_Has_Completion (Act_Decl_Id);
12628 Check_Generic_Actuals (Pack_Id, False);
12630 -- Generate a reference to link the visible subprogram instance to
12631 -- the generic body, which for navigation purposes is the only
12632 -- available source for the instance.
12634 Generate_Reference
12635 (Related_Instance (Pack_Id),
12636 Gen_Body_Id, 'b', Set_Ref => False, Force => True);
12638 -- If it is a child unit, make the parent instance (which is an
12639 -- instance of the parent of the generic) visible. The parent
12640 -- instance is the prefix of the name of the generic unit.
12642 if Ekind (Scope (Gen_Unit)) = E_Generic_Package
12643 and then Nkind (Gen_Id) = N_Expanded_Name
12644 then
12645 Par_Ent := Entity (Prefix (Gen_Id));
12646 Par_Vis := Is_Immediately_Visible (Par_Ent);
12647 Install_Parent (Par_Ent, In_Body => True);
12648 Par_Installed := True;
12650 elsif Is_Child_Unit (Gen_Unit) then
12651 Par_Ent := Scope (Gen_Unit);
12652 Par_Vis := Is_Immediately_Visible (Par_Ent);
12653 Install_Parent (Par_Ent, In_Body => True);
12654 Par_Installed := True;
12655 end if;
12657 -- Subprogram body is placed in the body of wrapper package,
12658 -- whose spec contains the subprogram declaration as well as
12659 -- the renaming declarations for the generic parameters.
12661 Pack_Body :=
12662 Make_Package_Body (Loc,
12663 Defining_Unit_Name => New_Copy (Pack_Id),
12664 Declarations => New_List (Act_Body));
12666 Set_Corresponding_Spec (Pack_Body, Pack_Id);
12668 -- If the instantiation is a library unit, then build resulting
12669 -- compilation unit nodes for the instance. The declaration of
12670 -- the enclosing package is the grandparent of the subprogram
12671 -- declaration. First replace the instantiation node as the unit
12672 -- of the corresponding compilation.
12674 if Nkind (Parent (Inst_Node)) = N_Compilation_Unit then
12675 if Parent (Inst_Node) = Cunit (Main_Unit) then
12676 Set_Unit (Parent (Inst_Node), Inst_Node);
12677 Build_Instance_Compilation_Unit_Nodes
12678 (Inst_Node, Pack_Body, Parent (Parent (Act_Decl)));
12679 Analyze (Inst_Node);
12680 else
12681 Set_Parent (Pack_Body, Parent (Inst_Node));
12682 Analyze (Pack_Body);
12683 end if;
12685 else
12686 Insert_Before (Inst_Node, Pack_Body);
12687 Mark_Rewrite_Insertion (Pack_Body);
12689 -- Insert the freeze node for the instance if need be
12691 if Expander_Active then
12692 Freeze_Subprogram_Instance (Inst_Node, Gen_Body, Pack_Id);
12693 end if;
12695 Analyze (Pack_Body);
12696 end if;
12698 Inherit_Context (Gen_Body, Inst_Node);
12700 Restore_Private_Views (Pack_Id, False);
12702 if Par_Installed then
12703 Remove_Parent (In_Body => True);
12705 -- Restore the previous visibility of the parent
12707 Set_Is_Immediately_Visible (Par_Ent, Par_Vis);
12708 end if;
12710 Restore_Env;
12712 -- Body not found. Error was emitted already. If there were no previous
12713 -- errors, this may be an instance whose scope is a premature instance.
12714 -- In that case we must insure that the (legal) program does raise
12715 -- program error if executed. We generate a subprogram body for this
12716 -- purpose.
12718 elsif Serious_Errors_Detected = 0
12719 and then Nkind (Parent (Inst_Node)) /= N_Compilation_Unit
12720 then
12721 if Body_Optional then
12722 goto Leave;
12724 elsif Ekind (Act_Decl_Id) = E_Procedure then
12725 Act_Body :=
12726 Make_Subprogram_Body (Loc,
12727 Specification =>
12728 Make_Procedure_Specification (Loc,
12729 Defining_Unit_Name =>
12730 Make_Defining_Identifier (Loc, Chars (Act_Decl_Id)),
12731 Parameter_Specifications =>
12732 New_Copy_List
12733 (Parameter_Specifications (Parent (Act_Decl_Id)))),
12735 Declarations => Empty_List,
12736 Handled_Statement_Sequence =>
12737 Make_Handled_Sequence_Of_Statements (Loc,
12738 Statements => New_List (
12739 Make_Raise_Program_Error (Loc,
12740 Reason => PE_Access_Before_Elaboration))));
12742 else
12743 Ret_Expr :=
12744 Make_Raise_Program_Error (Loc,
12745 Reason => PE_Access_Before_Elaboration);
12747 Set_Etype (Ret_Expr, (Etype (Act_Decl_Id)));
12748 Set_Analyzed (Ret_Expr);
12750 Act_Body :=
12751 Make_Subprogram_Body (Loc,
12752 Specification =>
12753 Make_Function_Specification (Loc,
12754 Defining_Unit_Name =>
12755 Make_Defining_Identifier (Loc, Chars (Act_Decl_Id)),
12756 Parameter_Specifications =>
12757 New_Copy_List
12758 (Parameter_Specifications (Parent (Act_Decl_Id))),
12759 Result_Definition =>
12760 New_Occurrence_Of (Etype (Act_Decl_Id), Loc)),
12762 Declarations => Empty_List,
12763 Handled_Statement_Sequence =>
12764 Make_Handled_Sequence_Of_Statements (Loc,
12765 Statements => New_List (
12766 Make_Simple_Return_Statement (Loc, Ret_Expr))));
12767 end if;
12769 Pack_Body :=
12770 Make_Package_Body (Loc,
12771 Defining_Unit_Name => New_Copy (Pack_Id),
12772 Declarations => New_List (Act_Body));
12774 Insert_After (Inst_Node, Pack_Body);
12775 Set_Corresponding_Spec (Pack_Body, Pack_Id);
12776 Analyze (Pack_Body);
12777 end if;
12779 <<Leave>>
12781 -- Restore the context that was in effect prior to instantiating the
12782 -- subprogram body.
12784 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
12785 Local_Suppress_Stack_Top := Saved_LSST;
12786 Scope_Suppress := Saved_SS;
12787 Style_Check := Saved_SC;
12789 Expander_Mode_Restore;
12790 Restore_Config_Switches (Saved_CS);
12791 Restore_Ghost_Region (Saved_GM, Saved_IGR);
12792 Restore_SPARK_Mode (Saved_SM, Saved_SMP);
12793 Restore_Warnings (Saved_Warn);
12794 end Instantiate_Subprogram_Body;
12796 ----------------------
12797 -- Instantiate_Type --
12798 ----------------------
12800 function Instantiate_Type
12801 (Formal : Node_Id;
12802 Actual : Node_Id;
12803 Analyzed_Formal : Node_Id;
12804 Actual_Decls : List_Id) return List_Id
12806 A_Gen_T : constant Entity_Id :=
12807 Defining_Identifier (Analyzed_Formal);
12808 Def : constant Node_Id := Formal_Type_Definition (Formal);
12809 Gen_T : constant Entity_Id := Defining_Identifier (Formal);
12810 Act_T : Entity_Id;
12811 Ancestor : Entity_Id := Empty;
12812 Decl_Node : Node_Id;
12813 Decl_Nodes : List_Id;
12814 Loc : Source_Ptr;
12815 Subt : Entity_Id;
12817 procedure Check_Shared_Variable_Control_Aspects;
12818 -- Ada 2022: Verify that shared variable control aspects (RM C.6)
12819 -- that may be specified for a formal type are obeyed by the actual.
12821 procedure Diagnose_Predicated_Actual;
12822 -- There are a number of constructs in which a discrete type with
12823 -- predicates is illegal, e.g. as an index in an array type declaration.
12824 -- If a generic type is used is such a construct in a generic package
12825 -- declaration, it carries the flag No_Predicate_On_Actual. it is part
12826 -- of the generic contract that the actual cannot have predicates.
12828 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean;
12829 -- Check that base types are the same and that the subtypes match
12830 -- statically. Used in several of the validation subprograms for
12831 -- actuals in instantiations.
12833 procedure Validate_Array_Type_Instance;
12834 procedure Validate_Access_Subprogram_Instance;
12835 procedure Validate_Access_Type_Instance;
12836 procedure Validate_Derived_Type_Instance;
12837 procedure Validate_Derived_Interface_Type_Instance;
12838 procedure Validate_Discriminated_Formal_Type;
12839 procedure Validate_Interface_Type_Instance;
12840 procedure Validate_Private_Type_Instance;
12841 procedure Validate_Incomplete_Type_Instance;
12842 -- These procedures perform validation tests for the named case.
12843 -- Validate_Discriminated_Formal_Type is shared by formal private
12844 -- types and Ada 2012 formal incomplete types.
12846 --------------------------------------------
12847 -- Check_Shared_Variable_Control_Aspects --
12848 --------------------------------------------
12850 -- Ada 2022: Verify that shared variable control aspects (RM C.6)
12851 -- that may be specified for the formal are obeyed by the actual.
12852 -- If the formal is a derived type the aspect specifications must match.
12853 -- NOTE: AI12-0282 implies that matching of aspects is required between
12854 -- formal and actual in all cases, but this is too restrictive.
12855 -- In particular it violates a language design rule: a limited private
12856 -- indefinite formal can be matched by any actual. The current code
12857 -- reflects an older and more permissive version of RM C.6 (12/5).
12859 procedure Check_Shared_Variable_Control_Aspects is
12860 begin
12861 if Ada_Version >= Ada_2022 then
12862 if Is_Atomic (A_Gen_T) and then not Is_Atomic (Act_T) then
12863 Error_Msg_NE
12864 ("actual for& must have Atomic aspect", Actual, A_Gen_T);
12866 elsif Is_Derived_Type (A_Gen_T)
12867 and then Is_Atomic (A_Gen_T) /= Is_Atomic (Act_T)
12868 then
12869 Error_Msg_NE
12870 ("actual for& has different Atomic aspect", Actual, A_Gen_T);
12871 end if;
12873 if Is_Volatile (A_Gen_T) and then not Is_Volatile (Act_T) then
12874 Error_Msg_NE
12875 ("actual for& must have Volatile aspect",
12876 Actual, A_Gen_T);
12878 elsif Is_Derived_Type (A_Gen_T)
12879 and then Is_Volatile (A_Gen_T) /= Is_Volatile (Act_T)
12880 then
12881 Error_Msg_NE
12882 ("actual for& has different Volatile aspect",
12883 Actual, A_Gen_T);
12884 end if;
12886 -- We assume that an array type whose atomic component type
12887 -- is Atomic is equivalent to an array type with the explicit
12888 -- aspect Has_Atomic_Components. This is a reasonable inference
12889 -- from the intent of AI12-0282, and makes it legal to use an
12890 -- actual that does not have the identical aspect as the formal.
12891 -- Ditto for volatile components.
12893 declare
12894 Actual_Atomic_Comp : constant Boolean :=
12895 Has_Atomic_Components (Act_T)
12896 or else (Is_Array_Type (Act_T)
12897 and then Is_Atomic (Component_Type (Act_T)));
12898 begin
12899 if Has_Atomic_Components (A_Gen_T) /= Actual_Atomic_Comp then
12900 Error_Msg_NE
12901 ("formal and actual for& must agree on atomic components",
12902 Actual, A_Gen_T);
12903 end if;
12904 end;
12906 declare
12907 Actual_Volatile_Comp : constant Boolean :=
12908 Has_Volatile_Components (Act_T)
12909 or else (Is_Array_Type (Act_T)
12910 and then Is_Volatile (Component_Type (Act_T)));
12911 begin
12912 if Has_Volatile_Components (A_Gen_T) /= Actual_Volatile_Comp
12913 then
12914 Error_Msg_NE
12915 ("actual for& must have volatile components",
12916 Actual, A_Gen_T);
12917 end if;
12918 end;
12920 -- The following two aspects do not require exact matching,
12921 -- but only one-way agreement. See RM C.6.
12923 if Is_Independent (A_Gen_T) and then not Is_Independent (Act_T)
12924 then
12925 Error_Msg_NE
12926 ("actual for& must have Independent aspect specified",
12927 Actual, A_Gen_T);
12928 end if;
12930 if Has_Independent_Components (A_Gen_T)
12931 and then not Has_Independent_Components (Act_T)
12932 then
12933 Error_Msg_NE
12934 ("actual for& must have Independent_Components specified",
12935 Actual, A_Gen_T);
12936 end if;
12938 -- Check actual/formal compatibility with respect to the four
12939 -- volatility refinement aspects.
12941 Check_Volatility_Compatibility
12942 (Act_T, A_Gen_T,
12943 "actual type", "its corresponding formal type",
12944 Srcpos_Bearer => Actual);
12945 end if;
12946 end Check_Shared_Variable_Control_Aspects;
12948 ---------------------------------
12949 -- Diagnose_Predicated_Actual --
12950 ---------------------------------
12952 procedure Diagnose_Predicated_Actual is
12953 begin
12954 if No_Predicate_On_Actual (A_Gen_T)
12955 and then Has_Predicates (Act_T)
12956 then
12957 Error_Msg_NE
12958 ("actual for& cannot be a type with predicate",
12959 Instantiation_Node, A_Gen_T);
12961 elsif No_Dynamic_Predicate_On_Actual (A_Gen_T)
12962 and then Has_Predicates (Act_T)
12963 and then not Has_Static_Predicate_Aspect (Act_T)
12964 then
12965 Error_Msg_NE
12966 ("actual for& cannot be a type with a dynamic predicate",
12967 Instantiation_Node, A_Gen_T);
12968 end if;
12969 end Diagnose_Predicated_Actual;
12971 --------------------
12972 -- Subtypes_Match --
12973 --------------------
12975 function Subtypes_Match (Gen_T, Act_T : Entity_Id) return Boolean is
12976 T : constant Entity_Id := Get_Instance_Of (Gen_T);
12978 begin
12979 -- Check that the base types, root types (when dealing with class
12980 -- wide types), or designated types (when dealing with anonymous
12981 -- access types) of Gen_T and Act_T are statically matching subtypes.
12983 return ((Base_Type (T) = Act_T
12984 or else Base_Type (T) = Base_Type (Act_T))
12985 and then Subtypes_Statically_Match (T, Act_T))
12987 or else (Is_Class_Wide_Type (Gen_T)
12988 and then Is_Class_Wide_Type (Act_T)
12989 and then Subtypes_Match
12990 (Get_Instance_Of (Root_Type (Gen_T)),
12991 Root_Type (Act_T)))
12993 or else (Is_Anonymous_Access_Type (Gen_T)
12994 and then Ekind (Act_T) = Ekind (Gen_T)
12995 and then Subtypes_Statically_Match
12996 (Designated_Type (Gen_T), Designated_Type (Act_T)));
12997 end Subtypes_Match;
12999 -----------------------------------------
13000 -- Validate_Access_Subprogram_Instance --
13001 -----------------------------------------
13003 procedure Validate_Access_Subprogram_Instance is
13004 begin
13005 if not Is_Access_Type (Act_T)
13006 or else Ekind (Designated_Type (Act_T)) /= E_Subprogram_Type
13007 then
13008 Error_Msg_NE
13009 ("expect access type in instantiation of &", Actual, Gen_T);
13010 Abandon_Instantiation (Actual);
13011 end if;
13013 -- According to AI05-288, actuals for access_to_subprograms must be
13014 -- subtype conformant with the generic formal. Previous to AI05-288
13015 -- only mode conformance was required.
13017 -- This is a binding interpretation that applies to previous versions
13018 -- of the language, no need to maintain previous weaker checks.
13020 Check_Subtype_Conformant
13021 (Designated_Type (Act_T),
13022 Designated_Type (A_Gen_T),
13023 Actual,
13024 Get_Inst => True);
13026 if Ekind (Base_Type (Act_T)) = E_Access_Protected_Subprogram_Type then
13027 if Ekind (A_Gen_T) = E_Access_Subprogram_Type then
13028 Error_Msg_NE
13029 ("protected access type not allowed for formal &",
13030 Actual, Gen_T);
13031 end if;
13033 elsif Ekind (A_Gen_T) = E_Access_Protected_Subprogram_Type then
13034 Error_Msg_NE
13035 ("expect protected access type for formal &",
13036 Actual, Gen_T);
13037 end if;
13039 -- If the formal has a specified convention (which in most cases
13040 -- will be StdCall) verify that the actual has the same convention.
13042 if Has_Convention_Pragma (A_Gen_T)
13043 and then Convention (A_Gen_T) /= Convention (Act_T)
13044 then
13045 Error_Msg_Name_1 := Get_Convention_Name (Convention (A_Gen_T));
13046 Error_Msg_NE
13047 ("actual for formal & must have convention %", Actual, Gen_T);
13048 end if;
13050 if Can_Never_Be_Null (A_Gen_T) /= Can_Never_Be_Null (Act_T) then
13051 Error_Msg_NE
13052 ("non null exclusion of actual and formal & do not match",
13053 Actual, Gen_T);
13054 end if;
13055 end Validate_Access_Subprogram_Instance;
13057 -----------------------------------
13058 -- Validate_Access_Type_Instance --
13059 -----------------------------------
13061 procedure Validate_Access_Type_Instance is
13062 Desig_Type : constant Entity_Id :=
13063 Find_Actual_Type (Designated_Type (A_Gen_T), A_Gen_T);
13064 Desig_Act : Entity_Id;
13066 begin
13067 if not Is_Access_Type (Act_T) then
13068 Error_Msg_NE
13069 ("expect access type in instantiation of &", Actual, Gen_T);
13070 Abandon_Instantiation (Actual);
13071 end if;
13073 if Is_Access_Constant (A_Gen_T) then
13074 if not Is_Access_Constant (Act_T) then
13075 Error_Msg_N
13076 ("actual type must be access-to-constant type", Actual);
13077 Abandon_Instantiation (Actual);
13078 end if;
13079 else
13080 if Is_Access_Constant (Act_T) then
13081 Error_Msg_N
13082 ("actual type must be access-to-variable type", Actual);
13083 Abandon_Instantiation (Actual);
13085 elsif Ekind (A_Gen_T) = E_General_Access_Type
13086 and then Ekind (Base_Type (Act_T)) /= E_General_Access_Type
13087 then
13088 Error_Msg_N
13089 ("actual must be general access type!", Actual);
13090 Error_Msg_NE -- CODEFIX
13091 ("\add ALL to }!", Actual, Act_T);
13092 Abandon_Instantiation (Actual);
13093 end if;
13094 end if;
13096 -- The designated subtypes, that is to say the subtypes introduced
13097 -- by an access type declaration (and not by a subtype declaration)
13098 -- must match.
13100 Desig_Act := Designated_Type (Base_Type (Act_T));
13102 -- The designated type may have been introduced through a limited_
13103 -- with clause, in which case retrieve the non-limited view. This
13104 -- applies to incomplete types as well as to class-wide types.
13106 if From_Limited_With (Desig_Act) then
13107 Desig_Act := Available_View (Desig_Act);
13108 end if;
13110 if not Subtypes_Match (Desig_Type, Desig_Act) then
13111 Error_Msg_NE
13112 ("designated type of actual does not match that of formal &",
13113 Actual, Gen_T);
13115 if not Predicates_Match (Desig_Type, Desig_Act) then
13116 Error_Msg_N ("\predicates do not match", Actual);
13117 end if;
13119 Abandon_Instantiation (Actual);
13120 end if;
13122 -- Ada 2005: null-exclusion indicators of the two types must agree
13124 if Can_Never_Be_Null (A_Gen_T) /= Can_Never_Be_Null (Act_T) then
13125 Error_Msg_NE
13126 ("non null exclusion of actual and formal & do not match",
13127 Actual, Gen_T);
13128 end if;
13129 end Validate_Access_Type_Instance;
13131 ----------------------------------
13132 -- Validate_Array_Type_Instance --
13133 ----------------------------------
13135 procedure Validate_Array_Type_Instance is
13136 I1 : Node_Id;
13137 I2 : Node_Id;
13138 T2 : Entity_Id;
13140 function Formal_Dimensions return Nat;
13141 -- Count number of dimensions in array type formal
13143 -----------------------
13144 -- Formal_Dimensions --
13145 -----------------------
13147 function Formal_Dimensions return Nat is
13148 Num : Nat := 0;
13149 Index : Node_Id;
13151 begin
13152 if Nkind (Def) = N_Constrained_Array_Definition then
13153 Index := First (Discrete_Subtype_Definitions (Def));
13154 else
13155 Index := First (Subtype_Marks (Def));
13156 end if;
13158 while Present (Index) loop
13159 Num := Num + 1;
13160 Next (Index);
13161 end loop;
13163 return Num;
13164 end Formal_Dimensions;
13166 -- Start of processing for Validate_Array_Type_Instance
13168 begin
13169 if not Is_Array_Type (Act_T) then
13170 Error_Msg_NE
13171 ("expect array type in instantiation of &", Actual, Gen_T);
13172 Abandon_Instantiation (Actual);
13174 elsif Nkind (Def) = N_Constrained_Array_Definition then
13175 if not (Is_Constrained (Act_T)) then
13176 Error_Msg_NE
13177 ("expect constrained array in instantiation of &",
13178 Actual, Gen_T);
13179 Abandon_Instantiation (Actual);
13180 end if;
13182 else
13183 if Is_Constrained (Act_T) then
13184 Error_Msg_NE
13185 ("expect unconstrained array in instantiation of &",
13186 Actual, Gen_T);
13187 Abandon_Instantiation (Actual);
13188 end if;
13189 end if;
13191 if Formal_Dimensions /= Number_Dimensions (Act_T) then
13192 Error_Msg_NE
13193 ("dimensions of actual do not match formal &", Actual, Gen_T);
13194 Abandon_Instantiation (Actual);
13195 end if;
13197 I1 := First_Index (A_Gen_T);
13198 I2 := First_Index (Act_T);
13199 for J in 1 .. Formal_Dimensions loop
13201 -- If the indexes of the actual were given by a subtype_mark,
13202 -- the index was transformed into a range attribute. Retrieve
13203 -- the original type mark for checking.
13205 if Is_Entity_Name (Original_Node (I2)) then
13206 T2 := Entity (Original_Node (I2));
13207 else
13208 T2 := Etype (I2);
13209 end if;
13211 if not Subtypes_Match
13212 (Find_Actual_Type (Etype (I1), A_Gen_T), T2)
13213 then
13214 Error_Msg_NE
13215 ("index types of actual do not match those of formal &",
13216 Actual, Gen_T);
13217 Abandon_Instantiation (Actual);
13218 end if;
13220 Next_Index (I1);
13221 Next_Index (I2);
13222 end loop;
13224 -- Check matching subtypes. Note that there are complex visibility
13225 -- issues when the generic is a child unit and some aspect of the
13226 -- generic type is declared in a parent unit of the generic. We do
13227 -- the test to handle this special case only after a direct check
13228 -- for static matching has failed. The case where both the component
13229 -- type and the array type are separate formals, and the component
13230 -- type is a private view may also require special checking in
13231 -- Subtypes_Match. Finally, we assume that a child instance where
13232 -- the component type comes from a formal of a parent instance is
13233 -- correct because the generic was correct. A more precise check
13234 -- seems too complex to install???
13236 if Subtypes_Match
13237 (Component_Type (A_Gen_T), Component_Type (Act_T))
13238 or else
13239 Subtypes_Match
13240 (Find_Actual_Type (Component_Type (A_Gen_T), A_Gen_T),
13241 Component_Type (Act_T))
13242 or else
13243 (not Inside_A_Generic
13244 and then Is_Child_Unit (Scope (Component_Type (A_Gen_T))))
13245 then
13246 null;
13247 else
13248 Error_Msg_NE
13249 ("component subtype of actual does not match that of formal &",
13250 Actual, Gen_T);
13251 Abandon_Instantiation (Actual);
13252 end if;
13254 if Has_Aliased_Components (A_Gen_T)
13255 and then not Has_Aliased_Components (Act_T)
13256 then
13257 Error_Msg_NE
13258 ("actual must have aliased components to match formal type &",
13259 Actual, Gen_T);
13260 end if;
13261 end Validate_Array_Type_Instance;
13263 -----------------------------------------------
13264 -- Validate_Derived_Interface_Type_Instance --
13265 -----------------------------------------------
13267 procedure Validate_Derived_Interface_Type_Instance is
13268 Par : constant Entity_Id := Entity (Subtype_Indication (Def));
13269 Elmt : Elmt_Id;
13271 begin
13272 -- First apply interface instance checks
13274 Validate_Interface_Type_Instance;
13276 -- Verify that immediate parent interface is an ancestor of
13277 -- the actual.
13279 if Present (Par)
13280 and then not Interface_Present_In_Ancestor (Act_T, Par)
13281 then
13282 Error_Msg_NE
13283 ("interface actual must include progenitor&", Actual, Par);
13284 end if;
13286 -- Now verify that the actual includes all other ancestors of
13287 -- the formal.
13289 Elmt := First_Elmt (Interfaces (A_Gen_T));
13290 while Present (Elmt) loop
13291 if not Interface_Present_In_Ancestor
13292 (Act_T, Get_Instance_Of (Node (Elmt)))
13293 then
13294 Error_Msg_NE
13295 ("interface actual must include progenitor&",
13296 Actual, Node (Elmt));
13297 end if;
13299 Next_Elmt (Elmt);
13300 end loop;
13301 end Validate_Derived_Interface_Type_Instance;
13303 ------------------------------------
13304 -- Validate_Derived_Type_Instance --
13305 ------------------------------------
13307 procedure Validate_Derived_Type_Instance is
13308 Actual_Discr : Entity_Id;
13309 Ancestor_Discr : Entity_Id;
13311 begin
13312 -- Verify that the actual includes the progenitors of the formal,
13313 -- if any. The formal may depend on previous formals and their
13314 -- instance, so we must examine instance of interfaces if present.
13315 -- The actual may be an extension of an interface, in which case
13316 -- it does not appear in the interface list, so this must be
13317 -- checked separately.
13319 if Present (Interface_List (Def)) then
13320 if not Has_Interfaces (Act_T) then
13321 Error_Msg_NE
13322 ("actual must implement all interfaces of formal&",
13323 Actual, A_Gen_T);
13325 else
13326 declare
13327 Act_Iface_List : Elist_Id;
13328 Iface : Node_Id;
13329 Iface_Ent : Entity_Id;
13331 function Instance_Exists (I : Entity_Id) return Boolean;
13332 -- If the interface entity is declared in a generic unit,
13333 -- this can only be legal if we are within an instantiation
13334 -- of a child of that generic. There is currently no
13335 -- mechanism to relate an interface declared within a
13336 -- generic to the corresponding interface in an instance,
13337 -- so we traverse the list of interfaces of the actual,
13338 -- looking for a name match.
13340 ---------------------
13341 -- Instance_Exists --
13342 ---------------------
13344 function Instance_Exists (I : Entity_Id) return Boolean is
13345 Iface_Elmt : Elmt_Id;
13347 begin
13348 Iface_Elmt := First_Elmt (Act_Iface_List);
13349 while Present (Iface_Elmt) loop
13350 if Is_Generic_Instance (Scope (Node (Iface_Elmt)))
13351 and then Chars (Node (Iface_Elmt)) = Chars (I)
13352 then
13353 return True;
13354 end if;
13356 Next_Elmt (Iface_Elmt);
13357 end loop;
13359 return False;
13360 end Instance_Exists;
13362 begin
13363 Iface := First (Abstract_Interface_List (A_Gen_T));
13364 Collect_Interfaces (Act_T, Act_Iface_List);
13366 while Present (Iface) loop
13367 Iface_Ent := Get_Instance_Of (Entity (Iface));
13369 if Is_Ancestor (Iface_Ent, Act_T)
13370 or else Is_Progenitor (Iface_Ent, Act_T)
13371 then
13372 null;
13374 elsif Ekind (Scope (Iface_Ent)) = E_Generic_Package
13375 and then Instance_Exists (Iface_Ent)
13376 then
13377 null;
13379 else
13380 Error_Msg_Name_1 := Chars (Act_T);
13381 Error_Msg_NE
13382 ("actual% must implement interface&",
13383 Actual, Etype (Iface));
13384 end if;
13386 Next (Iface);
13387 end loop;
13388 end;
13389 end if;
13390 end if;
13392 -- If the parent type in the generic declaration is itself a previous
13393 -- formal type, then it is local to the generic and absent from the
13394 -- analyzed generic definition. In that case the ancestor is the
13395 -- instance of the formal (which must have been instantiated
13396 -- previously), unless the ancestor is itself a formal derived type.
13397 -- In this latter case (which is the subject of Corrigendum 8652/0038
13398 -- (AI-202) the ancestor of the formals is the ancestor of its
13399 -- parent. Otherwise, the analyzed generic carries the parent type.
13400 -- If the parent type is defined in a previous formal package, then
13401 -- the scope of that formal package is that of the generic type
13402 -- itself, and it has already been mapped into the corresponding type
13403 -- in the actual package.
13405 -- Common case: parent type defined outside of the generic
13407 if Is_Entity_Name (Subtype_Mark (Def))
13408 and then Present (Entity (Subtype_Mark (Def)))
13409 then
13410 Ancestor := Get_Instance_Of (Entity (Subtype_Mark (Def)));
13412 -- Check whether parent is defined in a previous formal package
13414 elsif
13415 Scope (Scope (Base_Type (Etype (A_Gen_T)))) = Scope (A_Gen_T)
13416 then
13417 Ancestor :=
13418 Get_Instance_Of (Base_Type (Etype (A_Gen_T)));
13420 -- The type may be a local derivation, or a type extension of a
13421 -- previous formal, or of a formal of a parent package.
13423 elsif Is_Derived_Type (Get_Instance_Of (A_Gen_T))
13424 or else
13425 Ekind (Get_Instance_Of (A_Gen_T)) = E_Record_Type_With_Private
13426 then
13427 -- Check whether the parent is another derived formal type in the
13428 -- same generic unit.
13430 if Etype (A_Gen_T) /= A_Gen_T
13431 and then Is_Generic_Type (Etype (A_Gen_T))
13432 and then Scope (Etype (A_Gen_T)) = Scope (A_Gen_T)
13433 and then Etype (Etype (A_Gen_T)) /= Etype (A_Gen_T)
13434 then
13435 -- Locate ancestor of parent from the subtype declaration
13436 -- created for the actual.
13438 declare
13439 Decl : Node_Id;
13441 begin
13442 Decl := First (Actual_Decls);
13443 while Present (Decl) loop
13444 if Nkind (Decl) = N_Subtype_Declaration
13445 and then Chars (Defining_Identifier (Decl)) =
13446 Chars (Etype (A_Gen_T))
13447 then
13448 Ancestor := Generic_Parent_Type (Decl);
13449 exit;
13450 else
13451 Next (Decl);
13452 end if;
13453 end loop;
13454 end;
13456 pragma Assert (Present (Ancestor));
13458 -- The ancestor itself may be a previous formal that has been
13459 -- instantiated.
13461 Ancestor := Get_Instance_Of (Ancestor);
13463 else
13464 Ancestor :=
13465 Get_Instance_Of (Base_Type (Get_Instance_Of (A_Gen_T)));
13466 end if;
13468 -- Check whether parent is a previous formal of the current generic
13470 elsif Is_Derived_Type (A_Gen_T)
13471 and then Is_Generic_Type (Etype (A_Gen_T))
13472 and then Scope (A_Gen_T) = Scope (Etype (A_Gen_T))
13473 then
13474 Ancestor := Get_Instance_Of (First_Subtype (Etype (A_Gen_T)));
13476 -- An unusual case: the actual is a type declared in a parent unit,
13477 -- but is not a formal type so there is no instance_of for it.
13478 -- Retrieve it by analyzing the record extension.
13480 elsif Is_Child_Unit (Scope (A_Gen_T))
13481 and then In_Open_Scopes (Scope (Act_T))
13482 and then Is_Generic_Instance (Scope (Act_T))
13483 then
13484 Analyze (Subtype_Mark (Def));
13485 Ancestor := Entity (Subtype_Mark (Def));
13487 else
13488 Ancestor := Get_Instance_Of (Etype (Base_Type (A_Gen_T)));
13489 end if;
13491 -- If the formal derived type has pragma Preelaborable_Initialization
13492 -- then the actual type must have preelaborable initialization.
13494 if Known_To_Have_Preelab_Init (A_Gen_T)
13495 and then not Has_Preelaborable_Initialization (Act_T)
13496 then
13497 Error_Msg_NE
13498 ("actual for & must have preelaborable initialization",
13499 Actual, Gen_T);
13500 end if;
13502 -- Ada 2005 (AI-251)
13504 if Ada_Version >= Ada_2005 and then Is_Interface (Ancestor) then
13505 if not Interface_Present_In_Ancestor (Act_T, Ancestor) then
13506 Error_Msg_NE
13507 ("(Ada 2005) expected type implementing & in instantiation",
13508 Actual, Ancestor);
13509 end if;
13511 -- Finally verify that the (instance of) the ancestor is an ancestor
13512 -- of the actual.
13514 elsif not Is_Ancestor (Base_Type (Ancestor), Act_T) then
13515 Error_Msg_NE
13516 ("expect type derived from & in instantiation",
13517 Actual, First_Subtype (Ancestor));
13518 Abandon_Instantiation (Actual);
13519 end if;
13521 -- Ada 2005 (AI-443): Synchronized formal derived type checks. Note
13522 -- that the formal type declaration has been rewritten as a private
13523 -- extension.
13525 if Ada_Version >= Ada_2005
13526 and then Nkind (Parent (A_Gen_T)) = N_Private_Extension_Declaration
13527 and then Synchronized_Present (Parent (A_Gen_T))
13528 then
13529 -- The actual must be a synchronized tagged type
13531 if not Is_Tagged_Type (Act_T) then
13532 Error_Msg_N
13533 ("actual of synchronized type must be tagged", Actual);
13534 Abandon_Instantiation (Actual);
13536 elsif Nkind (Parent (Act_T)) = N_Full_Type_Declaration
13537 and then Nkind (Type_Definition (Parent (Act_T))) =
13538 N_Derived_Type_Definition
13539 and then not Synchronized_Present
13540 (Type_Definition (Parent (Act_T)))
13541 then
13542 Error_Msg_N
13543 ("actual of synchronized type must be synchronized", Actual);
13544 Abandon_Instantiation (Actual);
13545 end if;
13546 end if;
13548 -- Perform atomic/volatile checks (RM C.6(12)). Note that AI05-0218-1
13549 -- removes the second instance of the phrase "or allow pass by copy".
13551 -- For Ada 2022, the aspect may be specified explicitly for the
13552 -- formal regardless of whether an ancestor obeys it.
13554 if Is_Atomic (Act_T)
13555 and then not Is_Atomic (Ancestor)
13556 and then not Is_Atomic (A_Gen_T)
13557 then
13558 Error_Msg_N
13559 ("cannot have atomic actual type for non-atomic formal type",
13560 Actual);
13562 elsif Is_Volatile (Act_T)
13563 and then not Is_Volatile (Ancestor)
13564 and then not Is_Volatile (A_Gen_T)
13565 then
13566 Error_Msg_N
13567 ("cannot have volatile actual type for non-volatile formal type",
13568 Actual);
13569 end if;
13571 -- It should not be necessary to check for unknown discriminants on
13572 -- Formal, but for some reason Has_Unknown_Discriminants is false for
13573 -- A_Gen_T, so Is_Definite_Subtype incorrectly returns True. This
13574 -- needs fixing. ???
13576 if Is_Definite_Subtype (A_Gen_T)
13577 and then not Unknown_Discriminants_Present (Formal)
13578 and then not Is_Definite_Subtype (Act_T)
13579 then
13580 Error_Msg_N ("actual subtype must be constrained", Actual);
13581 Abandon_Instantiation (Actual);
13582 end if;
13584 if not Unknown_Discriminants_Present (Formal) then
13585 if Is_Constrained (Ancestor) then
13586 if not Is_Constrained (Act_T) then
13587 Error_Msg_N ("actual subtype must be constrained", Actual);
13588 Abandon_Instantiation (Actual);
13589 end if;
13591 -- Ancestor is unconstrained, Check if generic formal and actual
13592 -- agree on constrainedness. The check only applies to array types
13593 -- and discriminated types.
13595 elsif Is_Constrained (Act_T) then
13596 if Ekind (Ancestor) = E_Access_Type
13597 or else (not Is_Constrained (A_Gen_T)
13598 and then Is_Composite_Type (A_Gen_T))
13599 then
13600 Error_Msg_N ("actual subtype must be unconstrained", Actual);
13601 Abandon_Instantiation (Actual);
13602 end if;
13604 -- A class-wide type is only allowed if the formal has unknown
13605 -- discriminants.
13607 elsif Is_Class_Wide_Type (Act_T)
13608 and then not Has_Unknown_Discriminants (Ancestor)
13609 then
13610 Error_Msg_NE
13611 ("actual for & cannot be a class-wide type", Actual, Gen_T);
13612 Abandon_Instantiation (Actual);
13614 -- Otherwise, the formal and actual must have the same number
13615 -- of discriminants and each discriminant of the actual must
13616 -- correspond to a discriminant of the formal.
13618 elsif Has_Discriminants (Act_T)
13619 and then not Has_Unknown_Discriminants (Act_T)
13620 and then Has_Discriminants (Ancestor)
13621 then
13622 Actual_Discr := First_Discriminant (Act_T);
13623 Ancestor_Discr := First_Discriminant (Ancestor);
13624 while Present (Actual_Discr)
13625 and then Present (Ancestor_Discr)
13626 loop
13627 if Base_Type (Act_T) /= Base_Type (Ancestor) and then
13628 No (Corresponding_Discriminant (Actual_Discr))
13629 then
13630 Error_Msg_NE
13631 ("discriminant & does not correspond "
13632 & "to ancestor discriminant", Actual, Actual_Discr);
13633 Abandon_Instantiation (Actual);
13634 end if;
13636 Next_Discriminant (Actual_Discr);
13637 Next_Discriminant (Ancestor_Discr);
13638 end loop;
13640 if Present (Actual_Discr) or else Present (Ancestor_Discr) then
13641 Error_Msg_NE
13642 ("actual for & must have same number of discriminants",
13643 Actual, Gen_T);
13644 Abandon_Instantiation (Actual);
13645 end if;
13647 -- This case should be caught by the earlier check for
13648 -- constrainedness, but the check here is added for completeness.
13650 elsif Has_Discriminants (Act_T)
13651 and then not Has_Unknown_Discriminants (Act_T)
13652 then
13653 Error_Msg_NE
13654 ("actual for & must not have discriminants", Actual, Gen_T);
13655 Abandon_Instantiation (Actual);
13657 elsif Has_Discriminants (Ancestor) then
13658 Error_Msg_NE
13659 ("actual for & must have known discriminants", Actual, Gen_T);
13660 Abandon_Instantiation (Actual);
13661 end if;
13663 if not Subtypes_Statically_Compatible
13664 (Act_T, Ancestor, Formal_Derived_Matching => True)
13665 then
13666 Error_Msg_NE
13667 ("actual for & must be statically compatible with ancestor",
13668 Actual, Gen_T);
13670 if not Predicates_Compatible (Act_T, Ancestor) then
13671 Error_Msg_N
13672 ("\predicate on actual is not compatible with ancestor",
13673 Actual);
13674 end if;
13676 Abandon_Instantiation (Actual);
13677 end if;
13678 end if;
13680 -- If the formal and actual types are abstract, check that there
13681 -- are no abstract primitives of the actual type that correspond to
13682 -- nonabstract primitives of the formal type (second sentence of
13683 -- RM95 3.9.3(9)).
13685 if Is_Abstract_Type (A_Gen_T) and then Is_Abstract_Type (Act_T) then
13686 Check_Abstract_Primitives : declare
13687 Gen_Prims : constant Elist_Id :=
13688 Primitive_Operations (A_Gen_T);
13689 Gen_Elmt : Elmt_Id;
13690 Gen_Subp : Entity_Id;
13691 Anc_Subp : Entity_Id;
13692 Anc_Formal : Entity_Id;
13693 Anc_F_Type : Entity_Id;
13695 Act_Prims : constant Elist_Id := Primitive_Operations (Act_T);
13696 Act_Elmt : Elmt_Id;
13697 Act_Subp : Entity_Id;
13698 Act_Formal : Entity_Id;
13699 Act_F_Type : Entity_Id;
13701 Subprograms_Correspond : Boolean;
13703 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean;
13704 -- Returns true if T2 is derived directly or indirectly from
13705 -- T1, including derivations from interfaces. T1 and T2 are
13706 -- required to be specific tagged base types.
13708 ------------------------
13709 -- Is_Tagged_Ancestor --
13710 ------------------------
13712 function Is_Tagged_Ancestor (T1, T2 : Entity_Id) return Boolean
13714 Intfc_Elmt : Elmt_Id;
13716 begin
13717 -- The predicate is satisfied if the types are the same
13719 if T1 = T2 then
13720 return True;
13722 -- If we've reached the top of the derivation chain then
13723 -- we know that T1 is not an ancestor of T2.
13725 elsif Etype (T2) = T2 then
13726 return False;
13728 -- Proceed to check T2's immediate parent
13730 elsif Is_Ancestor (T1, Base_Type (Etype (T2))) then
13731 return True;
13733 -- Finally, check to see if T1 is an ancestor of any of T2's
13734 -- progenitors.
13736 else
13737 Intfc_Elmt := First_Elmt (Interfaces (T2));
13738 while Present (Intfc_Elmt) loop
13739 if Is_Ancestor (T1, Node (Intfc_Elmt)) then
13740 return True;
13741 end if;
13743 Next_Elmt (Intfc_Elmt);
13744 end loop;
13745 end if;
13747 return False;
13748 end Is_Tagged_Ancestor;
13750 -- Start of processing for Check_Abstract_Primitives
13752 begin
13753 -- Loop over all of the formal derived type's primitives
13755 Gen_Elmt := First_Elmt (Gen_Prims);
13756 while Present (Gen_Elmt) loop
13757 Gen_Subp := Node (Gen_Elmt);
13759 -- If the primitive of the formal is not abstract, then
13760 -- determine whether there is a corresponding primitive of
13761 -- the actual type that's abstract.
13763 if not Is_Abstract_Subprogram (Gen_Subp) then
13764 Act_Elmt := First_Elmt (Act_Prims);
13765 while Present (Act_Elmt) loop
13766 Act_Subp := Node (Act_Elmt);
13768 -- If we find an abstract primitive of the actual,
13769 -- then we need to test whether it corresponds to the
13770 -- subprogram from which the generic formal primitive
13771 -- is inherited.
13773 if Is_Abstract_Subprogram (Act_Subp) then
13774 Anc_Subp := Alias (Gen_Subp);
13776 -- Test whether we have a corresponding primitive
13777 -- by comparing names, kinds, formal types, and
13778 -- result types.
13780 if Chars (Anc_Subp) = Chars (Act_Subp)
13781 and then Ekind (Anc_Subp) = Ekind (Act_Subp)
13782 then
13783 Anc_Formal := First_Formal (Anc_Subp);
13784 Act_Formal := First_Formal (Act_Subp);
13785 while Present (Anc_Formal)
13786 and then Present (Act_Formal)
13787 loop
13788 Anc_F_Type := Etype (Anc_Formal);
13789 Act_F_Type := Etype (Act_Formal);
13791 if Ekind (Anc_F_Type) =
13792 E_Anonymous_Access_Type
13793 then
13794 Anc_F_Type := Designated_Type (Anc_F_Type);
13796 if Ekind (Act_F_Type) =
13797 E_Anonymous_Access_Type
13798 then
13799 Act_F_Type :=
13800 Designated_Type (Act_F_Type);
13801 else
13802 exit;
13803 end if;
13805 elsif
13806 Ekind (Act_F_Type) = E_Anonymous_Access_Type
13807 then
13808 exit;
13809 end if;
13811 Anc_F_Type := Base_Type (Anc_F_Type);
13812 Act_F_Type := Base_Type (Act_F_Type);
13814 -- If the formal is controlling, then the
13815 -- the type of the actual primitive's formal
13816 -- must be derived directly or indirectly
13817 -- from the type of the ancestor primitive's
13818 -- formal.
13820 if Is_Controlling_Formal (Anc_Formal) then
13821 if not Is_Tagged_Ancestor
13822 (Anc_F_Type, Act_F_Type)
13823 then
13824 exit;
13825 end if;
13827 -- Otherwise the types of the formals must
13828 -- be the same.
13830 elsif Anc_F_Type /= Act_F_Type then
13831 exit;
13832 end if;
13834 Next_Formal (Anc_Formal);
13835 Next_Formal (Act_Formal);
13836 end loop;
13838 -- If we traversed through all of the formals
13839 -- then so far the subprograms correspond, so
13840 -- now check that any result types correspond.
13842 if No (Anc_Formal) and then No (Act_Formal) then
13843 Subprograms_Correspond := True;
13845 if Ekind (Act_Subp) = E_Function then
13846 Anc_F_Type := Etype (Anc_Subp);
13847 Act_F_Type := Etype (Act_Subp);
13849 if Ekind (Anc_F_Type) =
13850 E_Anonymous_Access_Type
13851 then
13852 Anc_F_Type :=
13853 Designated_Type (Anc_F_Type);
13855 if Ekind (Act_F_Type) =
13856 E_Anonymous_Access_Type
13857 then
13858 Act_F_Type :=
13859 Designated_Type (Act_F_Type);
13860 else
13861 Subprograms_Correspond := False;
13862 end if;
13864 elsif
13865 Ekind (Act_F_Type)
13866 = E_Anonymous_Access_Type
13867 then
13868 Subprograms_Correspond := False;
13869 end if;
13871 Anc_F_Type := Base_Type (Anc_F_Type);
13872 Act_F_Type := Base_Type (Act_F_Type);
13874 -- Now either the result types must be
13875 -- the same or, if the result type is
13876 -- controlling, the result type of the
13877 -- actual primitive must descend from the
13878 -- result type of the ancestor primitive.
13880 if Subprograms_Correspond
13881 and then Anc_F_Type /= Act_F_Type
13882 and then
13883 Has_Controlling_Result (Anc_Subp)
13884 and then not Is_Tagged_Ancestor
13885 (Anc_F_Type, Act_F_Type)
13886 then
13887 Subprograms_Correspond := False;
13888 end if;
13889 end if;
13891 -- Found a matching subprogram belonging to
13892 -- formal ancestor type, so actual subprogram
13893 -- corresponds and this violates 3.9.3(9).
13895 if Subprograms_Correspond then
13896 Error_Msg_NE
13897 ("abstract subprogram & overrides "
13898 & "nonabstract subprogram of ancestor",
13899 Actual, Act_Subp);
13900 end if;
13901 end if;
13902 end if;
13903 end if;
13905 Next_Elmt (Act_Elmt);
13906 end loop;
13907 end if;
13909 Next_Elmt (Gen_Elmt);
13910 end loop;
13911 end Check_Abstract_Primitives;
13912 end if;
13914 -- Verify that limitedness matches. If parent is a limited
13915 -- interface then the generic formal is not unless declared
13916 -- explicitly so. If not declared limited, the actual cannot be
13917 -- limited (see AI05-0087).
13919 if Is_Limited_Type (Act_T) and then not Is_Limited_Type (A_Gen_T) then
13920 if not In_Instance then
13921 Error_Msg_NE
13922 ("actual for non-limited & cannot be a limited type",
13923 Actual, Gen_T);
13924 Explain_Limited_Type (Act_T, Actual);
13925 Abandon_Instantiation (Actual);
13926 end if;
13927 end if;
13929 -- Check for AI12-0036
13931 declare
13932 Formal_Is_Private_Extension : constant Boolean :=
13933 Nkind (Parent (A_Gen_T)) = N_Private_Extension_Declaration;
13935 Actual_Is_Tagged : constant Boolean := Is_Tagged_Type (Act_T);
13937 begin
13938 if Actual_Is_Tagged /= Formal_Is_Private_Extension then
13939 if not In_Instance then
13940 if Actual_Is_Tagged then
13941 Error_Msg_NE
13942 ("actual for & cannot be a tagged type", Actual, Gen_T);
13943 else
13944 Error_Msg_NE
13945 ("actual for & must be a tagged type", Actual, Gen_T);
13946 end if;
13948 Abandon_Instantiation (Actual);
13949 end if;
13950 end if;
13951 end;
13952 end Validate_Derived_Type_Instance;
13954 ----------------------------------------
13955 -- Validate_Discriminated_Formal_Type --
13956 ----------------------------------------
13958 procedure Validate_Discriminated_Formal_Type is
13959 Formal_Discr : Entity_Id;
13960 Actual_Discr : Entity_Id;
13961 Formal_Subt : Entity_Id;
13963 begin
13964 if Has_Discriminants (A_Gen_T) then
13965 if not Has_Discriminants (Act_T) then
13966 Error_Msg_NE
13967 ("actual for & must have discriminants", Actual, Gen_T);
13968 Abandon_Instantiation (Actual);
13970 elsif Is_Constrained (Act_T) then
13971 Error_Msg_NE
13972 ("actual for & must be unconstrained", Actual, Gen_T);
13973 Abandon_Instantiation (Actual);
13975 else
13976 Formal_Discr := First_Discriminant (A_Gen_T);
13977 Actual_Discr := First_Discriminant (Act_T);
13978 while Formal_Discr /= Empty loop
13979 if Actual_Discr = Empty then
13980 Error_Msg_N
13981 ("discriminants on actual do not match formal",
13982 Actual);
13983 Abandon_Instantiation (Actual);
13984 end if;
13986 Formal_Subt := Get_Instance_Of (Etype (Formal_Discr));
13988 -- Access discriminants match if designated types do
13990 if Ekind (Base_Type (Formal_Subt)) = E_Anonymous_Access_Type
13991 and then (Ekind (Base_Type (Etype (Actual_Discr)))) =
13992 E_Anonymous_Access_Type
13993 and then
13994 Get_Instance_Of
13995 (Designated_Type (Base_Type (Formal_Subt))) =
13996 Designated_Type (Base_Type (Etype (Actual_Discr)))
13997 then
13998 null;
14000 elsif Base_Type (Formal_Subt) /=
14001 Base_Type (Etype (Actual_Discr))
14002 then
14003 Error_Msg_N
14004 ("types of actual discriminants must match formal",
14005 Actual);
14006 Abandon_Instantiation (Actual);
14008 elsif not Subtypes_Statically_Match
14009 (Formal_Subt, Etype (Actual_Discr))
14010 and then Ada_Version >= Ada_95
14011 then
14012 Error_Msg_N
14013 ("subtypes of actual discriminants must match formal",
14014 Actual);
14015 Abandon_Instantiation (Actual);
14016 end if;
14018 Next_Discriminant (Formal_Discr);
14019 Next_Discriminant (Actual_Discr);
14020 end loop;
14022 if Actual_Discr /= Empty then
14023 Error_Msg_NE
14024 ("discriminants on actual do not match formal",
14025 Actual, Gen_T);
14026 Abandon_Instantiation (Actual);
14027 end if;
14028 end if;
14029 end if;
14030 end Validate_Discriminated_Formal_Type;
14032 ---------------------------------------
14033 -- Validate_Incomplete_Type_Instance --
14034 ---------------------------------------
14036 procedure Validate_Incomplete_Type_Instance is
14037 begin
14038 if not Is_Tagged_Type (Act_T)
14039 and then Is_Tagged_Type (A_Gen_T)
14040 then
14041 Error_Msg_NE
14042 ("actual for & must be a tagged type", Actual, Gen_T);
14043 end if;
14045 Validate_Discriminated_Formal_Type;
14046 end Validate_Incomplete_Type_Instance;
14048 --------------------------------------
14049 -- Validate_Interface_Type_Instance --
14050 --------------------------------------
14052 procedure Validate_Interface_Type_Instance is
14053 begin
14054 if not Is_Interface (Act_T) then
14055 Error_Msg_NE
14056 ("actual for formal interface type must be an interface",
14057 Actual, Gen_T);
14059 elsif Is_Limited_Type (Act_T) /= Is_Limited_Type (A_Gen_T)
14060 or else Is_Task_Interface (A_Gen_T) /= Is_Task_Interface (Act_T)
14061 or else Is_Protected_Interface (A_Gen_T) /=
14062 Is_Protected_Interface (Act_T)
14063 or else Is_Synchronized_Interface (A_Gen_T) /=
14064 Is_Synchronized_Interface (Act_T)
14065 then
14066 Error_Msg_NE
14067 ("actual for interface& does not match (RM 12.5.5(4))",
14068 Actual, Gen_T);
14069 end if;
14070 end Validate_Interface_Type_Instance;
14072 ------------------------------------
14073 -- Validate_Private_Type_Instance --
14074 ------------------------------------
14076 procedure Validate_Private_Type_Instance is
14077 begin
14078 if Is_Limited_Type (Act_T)
14079 and then not Is_Limited_Type (A_Gen_T)
14080 then
14081 if In_Instance then
14082 null;
14083 else
14084 Error_Msg_NE
14085 ("actual for non-limited & cannot be a limited type", Actual,
14086 Gen_T);
14087 Explain_Limited_Type (Act_T, Actual);
14088 Abandon_Instantiation (Actual);
14089 end if;
14091 elsif Known_To_Have_Preelab_Init (A_Gen_T)
14092 and then not Has_Preelaborable_Initialization (Act_T)
14093 then
14094 Error_Msg_NE
14095 ("actual for & must have preelaborable initialization", Actual,
14096 Gen_T);
14098 elsif not Is_Definite_Subtype (Act_T)
14099 and then Is_Definite_Subtype (A_Gen_T)
14100 and then Ada_Version >= Ada_95
14101 then
14102 Error_Msg_NE
14103 ("actual for & must be a definite subtype", Actual, Gen_T);
14105 elsif not Is_Tagged_Type (Act_T)
14106 and then Is_Tagged_Type (A_Gen_T)
14107 then
14108 Error_Msg_NE
14109 ("actual for & must be a tagged type", Actual, Gen_T);
14110 end if;
14112 Validate_Discriminated_Formal_Type;
14113 Ancestor := Gen_T;
14114 end Validate_Private_Type_Instance;
14116 -- Start of processing for Instantiate_Type
14118 begin
14119 if Get_Instance_Of (A_Gen_T) /= A_Gen_T then
14120 Error_Msg_N ("duplicate instantiation of generic type", Actual);
14121 return New_List (Error);
14123 elsif not Is_Entity_Name (Actual)
14124 or else not Is_Type (Entity (Actual))
14125 then
14126 Error_Msg_NE
14127 ("expect valid subtype mark to instantiate &", Actual, Gen_T);
14128 Abandon_Instantiation (Actual);
14130 else
14131 Act_T := Entity (Actual);
14133 -- Ada 2005 (AI-216): An Unchecked_Union subtype shall only be passed
14134 -- as a generic actual parameter if the corresponding formal type
14135 -- does not have a known_discriminant_part, or is a formal derived
14136 -- type that is an Unchecked_Union type.
14138 if Is_Unchecked_Union (Base_Type (Act_T)) then
14139 if not Has_Discriminants (A_Gen_T)
14140 or else (Is_Derived_Type (A_Gen_T)
14141 and then Is_Unchecked_Union (A_Gen_T))
14142 then
14143 null;
14144 else
14145 Error_Msg_N ("unchecked union cannot be the actual for a "
14146 & "discriminated formal type", Act_T);
14148 end if;
14149 end if;
14151 -- Deal with fixed/floating restrictions
14153 if Is_Floating_Point_Type (Act_T) then
14154 Check_Restriction (No_Floating_Point, Actual);
14155 elsif Is_Fixed_Point_Type (Act_T) then
14156 Check_Restriction (No_Fixed_Point, Actual);
14157 end if;
14159 -- Deal with error of using incomplete type as generic actual.
14160 -- This includes limited views of a type, even if the non-limited
14161 -- view may be available.
14163 if Ekind (Act_T) = E_Incomplete_Type
14164 or else (Is_Class_Wide_Type (Act_T)
14165 and then Ekind (Root_Type (Act_T)) = E_Incomplete_Type)
14166 then
14167 -- If the formal is an incomplete type, the actual can be
14168 -- incomplete as well, but if an actual incomplete type has
14169 -- a full view, then we'll retrieve that.
14171 if Ekind (A_Gen_T) = E_Incomplete_Type
14172 and then not Present (Full_View (Act_T))
14173 then
14174 null;
14176 elsif Is_Class_Wide_Type (Act_T)
14177 or else No (Full_View (Act_T))
14178 then
14179 Error_Msg_N ("premature use of incomplete type", Actual);
14180 Abandon_Instantiation (Actual);
14182 else
14183 Act_T := Full_View (Act_T);
14184 Set_Entity (Actual, Act_T);
14186 if Has_Private_Component (Act_T) then
14187 Error_Msg_N
14188 ("premature use of type with private component", Actual);
14189 end if;
14190 end if;
14192 -- Deal with error of premature use of private type as generic actual
14194 elsif Is_Private_Type (Act_T)
14195 and then Is_Private_Type (Base_Type (Act_T))
14196 and then not Is_Generic_Type (Act_T)
14197 and then not Is_Derived_Type (Act_T)
14198 and then No (Full_View (Root_Type (Act_T)))
14199 then
14200 -- If the formal is an incomplete type, the actual can be
14201 -- private or incomplete as well.
14203 if Ekind (A_Gen_T) = E_Incomplete_Type then
14204 null;
14205 else
14206 Error_Msg_N ("premature use of private type", Actual);
14207 end if;
14209 elsif Has_Private_Component (Act_T) then
14210 Error_Msg_N
14211 ("premature use of type with private component", Actual);
14212 end if;
14214 Set_Instance_Of (A_Gen_T, Act_T);
14216 -- If the type is generic, the class-wide type may also be used
14218 if Is_Tagged_Type (A_Gen_T)
14219 and then Is_Tagged_Type (Act_T)
14220 and then not Is_Class_Wide_Type (A_Gen_T)
14221 then
14222 Set_Instance_Of (Class_Wide_Type (A_Gen_T),
14223 Class_Wide_Type (Act_T));
14224 end if;
14226 if not Is_Abstract_Type (A_Gen_T)
14227 and then Is_Abstract_Type (Act_T)
14228 then
14229 Error_Msg_N
14230 ("actual of non-abstract formal cannot be abstract", Actual);
14231 end if;
14233 -- A generic scalar type is a first subtype for which we generate
14234 -- an anonymous base type. Indicate that the instance of this base
14235 -- is the base type of the actual.
14237 if Is_Scalar_Type (A_Gen_T) then
14238 Set_Instance_Of (Etype (A_Gen_T), Etype (Act_T));
14239 end if;
14240 end if;
14242 Check_Shared_Variable_Control_Aspects;
14244 if Error_Posted (Act_T) then
14245 null;
14246 else
14247 case Nkind (Def) is
14248 when N_Formal_Private_Type_Definition =>
14249 Validate_Private_Type_Instance;
14251 when N_Formal_Incomplete_Type_Definition =>
14252 Validate_Incomplete_Type_Instance;
14254 when N_Formal_Derived_Type_Definition =>
14255 Validate_Derived_Type_Instance;
14257 when N_Formal_Discrete_Type_Definition =>
14258 if not Is_Discrete_Type (Act_T) then
14259 Error_Msg_NE
14260 ("expect discrete type in instantiation of&",
14261 Actual, Gen_T);
14262 Abandon_Instantiation (Actual);
14263 end if;
14265 Diagnose_Predicated_Actual;
14267 when N_Formal_Signed_Integer_Type_Definition =>
14268 if not Is_Signed_Integer_Type (Act_T) then
14269 Error_Msg_NE
14270 ("expect signed integer type in instantiation of&",
14271 Actual, Gen_T);
14272 Abandon_Instantiation (Actual);
14273 end if;
14275 Diagnose_Predicated_Actual;
14277 when N_Formal_Modular_Type_Definition =>
14278 if not Is_Modular_Integer_Type (Act_T) then
14279 Error_Msg_NE
14280 ("expect modular type in instantiation of &",
14281 Actual, Gen_T);
14282 Abandon_Instantiation (Actual);
14283 end if;
14285 Diagnose_Predicated_Actual;
14287 when N_Formal_Floating_Point_Definition =>
14288 if not Is_Floating_Point_Type (Act_T) then
14289 Error_Msg_NE
14290 ("expect float type in instantiation of &", Actual, Gen_T);
14291 Abandon_Instantiation (Actual);
14292 end if;
14294 when N_Formal_Ordinary_Fixed_Point_Definition =>
14295 if not Is_Ordinary_Fixed_Point_Type (Act_T) then
14296 Error_Msg_NE
14297 ("expect ordinary fixed point type in instantiation of &",
14298 Actual, Gen_T);
14299 Abandon_Instantiation (Actual);
14300 end if;
14302 when N_Formal_Decimal_Fixed_Point_Definition =>
14303 if not Is_Decimal_Fixed_Point_Type (Act_T) then
14304 Error_Msg_NE
14305 ("expect decimal type in instantiation of &",
14306 Actual, Gen_T);
14307 Abandon_Instantiation (Actual);
14308 end if;
14310 when N_Array_Type_Definition =>
14311 Validate_Array_Type_Instance;
14313 when N_Access_To_Object_Definition =>
14314 Validate_Access_Type_Instance;
14316 when N_Access_Function_Definition
14317 | N_Access_Procedure_Definition
14319 Validate_Access_Subprogram_Instance;
14321 when N_Record_Definition =>
14322 Validate_Interface_Type_Instance;
14324 when N_Derived_Type_Definition =>
14325 Validate_Derived_Interface_Type_Instance;
14327 when others =>
14328 raise Program_Error;
14329 end case;
14330 end if;
14332 Subt := New_Copy (Gen_T);
14334 -- Use adjusted sloc of subtype name as the location for other nodes in
14335 -- the subtype declaration.
14337 Loc := Sloc (Subt);
14339 Decl_Node :=
14340 Make_Subtype_Declaration (Loc,
14341 Defining_Identifier => Subt,
14342 Subtype_Indication => New_Occurrence_Of (Act_T, Loc));
14344 -- Record whether the actual is private at this point, so that
14345 -- Check_Generic_Actuals can restore its proper view before the
14346 -- semantic analysis of the instance.
14348 if Is_Private_Type (Act_T) then
14349 Set_Has_Private_View (Subtype_Indication (Decl_Node));
14350 end if;
14352 -- In Ada 2012 the actual may be a limited view. Indicate that
14353 -- the local subtype must be treated as such.
14355 if From_Limited_With (Act_T) then
14356 Mutate_Ekind (Subt, E_Incomplete_Subtype);
14357 Set_From_Limited_With (Subt);
14358 end if;
14360 Decl_Nodes := New_List (Decl_Node);
14362 -- Flag actual derived types so their elaboration produces the
14363 -- appropriate renamings for the primitive operations of the ancestor.
14364 -- Flag actual for formal private types as well, to determine whether
14365 -- operations in the private part may override inherited operations.
14366 -- If the formal has an interface list, the ancestor is not the
14367 -- parent, but the analyzed formal that includes the interface
14368 -- operations of all its progenitors.
14370 -- Same treatment for formal private types, so we can check whether the
14371 -- type is tagged limited when validating derivations in the private
14372 -- part. (See AI05-096).
14374 if Nkind (Def) = N_Formal_Derived_Type_Definition then
14375 if Present (Interface_List (Def)) then
14376 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
14377 else
14378 Set_Generic_Parent_Type (Decl_Node, Ancestor);
14379 end if;
14381 elsif Nkind (Def) in N_Formal_Private_Type_Definition
14382 | N_Formal_Incomplete_Type_Definition
14383 then
14384 Set_Generic_Parent_Type (Decl_Node, A_Gen_T);
14385 end if;
14387 -- If the actual is a synchronized type that implements an interface,
14388 -- the primitive operations are attached to the corresponding record,
14389 -- and we have to treat it as an additional generic actual, so that its
14390 -- primitive operations become visible in the instance. The task or
14391 -- protected type itself does not carry primitive operations.
14393 if Is_Concurrent_Type (Act_T)
14394 and then Is_Tagged_Type (Act_T)
14395 and then Present (Corresponding_Record_Type (Act_T))
14396 and then Present (Ancestor)
14397 and then Is_Interface (Ancestor)
14398 then
14399 declare
14400 Corr_Rec : constant Entity_Id :=
14401 Corresponding_Record_Type (Act_T);
14402 New_Corr : Entity_Id;
14403 Corr_Decl : Node_Id;
14405 begin
14406 New_Corr := Make_Temporary (Loc, 'S');
14407 Corr_Decl :=
14408 Make_Subtype_Declaration (Loc,
14409 Defining_Identifier => New_Corr,
14410 Subtype_Indication =>
14411 New_Occurrence_Of (Corr_Rec, Loc));
14412 Append_To (Decl_Nodes, Corr_Decl);
14414 if Ekind (Act_T) = E_Task_Type then
14415 Mutate_Ekind (Subt, E_Task_Subtype);
14416 else
14417 Mutate_Ekind (Subt, E_Protected_Subtype);
14418 end if;
14420 Set_Corresponding_Record_Type (Subt, Corr_Rec);
14421 Set_Generic_Parent_Type (Corr_Decl, Ancestor);
14422 Set_Generic_Parent_Type (Decl_Node, Empty);
14423 end;
14424 end if;
14426 -- For a floating-point type, capture dimension info if any, because
14427 -- the generated subtype declaration does not come from source and
14428 -- will not process dimensions.
14430 if Is_Floating_Point_Type (Act_T) then
14431 Copy_Dimensions (Act_T, Subt);
14432 end if;
14434 return Decl_Nodes;
14435 end Instantiate_Type;
14437 ---------------------
14438 -- Is_In_Main_Unit --
14439 ---------------------
14441 function Is_In_Main_Unit (N : Node_Id) return Boolean is
14442 Unum : constant Unit_Number_Type := Get_Source_Unit (N);
14443 Current_Unit : Node_Id;
14445 begin
14446 if Unum = Main_Unit then
14447 return True;
14449 -- If the current unit is a subunit then it is either the main unit or
14450 -- is being compiled as part of the main unit.
14452 elsif Nkind (N) = N_Compilation_Unit then
14453 return Nkind (Unit (N)) = N_Subunit;
14454 end if;
14456 Current_Unit := Parent (N);
14457 while Present (Current_Unit)
14458 and then Nkind (Current_Unit) /= N_Compilation_Unit
14459 loop
14460 Current_Unit := Parent (Current_Unit);
14461 end loop;
14463 -- The instantiation node is in the main unit, or else the current node
14464 -- (perhaps as the result of nested instantiations) is in the main unit,
14465 -- or in the declaration of the main unit, which in this last case must
14466 -- be a body.
14468 return
14469 Current_Unit = Cunit (Main_Unit)
14470 or else Current_Unit = Library_Unit (Cunit (Main_Unit))
14471 or else (Present (Current_Unit)
14472 and then Present (Library_Unit (Current_Unit))
14473 and then Is_In_Main_Unit (Library_Unit (Current_Unit)));
14474 end Is_In_Main_Unit;
14476 ----------------------------
14477 -- Load_Parent_Of_Generic --
14478 ----------------------------
14480 procedure Load_Parent_Of_Generic
14481 (N : Node_Id;
14482 Spec : Node_Id;
14483 Body_Optional : Boolean := False)
14485 Comp_Unit : constant Node_Id := Cunit (Get_Source_Unit (Spec));
14486 Saved_Style_Check : constant Boolean := Style_Check;
14487 Saved_Warnings : constant Warning_Record := Save_Warnings;
14488 True_Parent : Node_Id;
14489 Inst_Node : Node_Id;
14490 OK : Boolean;
14491 Previous_Instances : constant Elist_Id := New_Elmt_List;
14493 procedure Collect_Previous_Instances (Decls : List_Id);
14494 -- Collect all instantiations in the given list of declarations, that
14495 -- precede the generic that we need to load. If the bodies of these
14496 -- instantiations are available, we must analyze them, to ensure that
14497 -- the public symbols generated are the same when the unit is compiled
14498 -- to generate code, and when it is compiled in the context of a unit
14499 -- that needs a particular nested instance. This process is applied to
14500 -- both package and subprogram instances.
14502 --------------------------------
14503 -- Collect_Previous_Instances --
14504 --------------------------------
14506 procedure Collect_Previous_Instances (Decls : List_Id) is
14507 Decl : Node_Id;
14509 begin
14510 Decl := First (Decls);
14511 while Present (Decl) loop
14512 if Sloc (Decl) >= Sloc (Inst_Node) then
14513 return;
14515 -- If Decl is an instantiation, then record it as requiring
14516 -- instantiation of the corresponding body, except if it is an
14517 -- abbreviated instantiation generated internally for conformance
14518 -- checking purposes only for the case of a formal package
14519 -- declared without a box (see Instantiate_Formal_Package). Such
14520 -- an instantiation does not generate any code (the actual code
14521 -- comes from actual) and thus does not need to be analyzed here.
14522 -- If the instantiation appears with a generic package body it is
14523 -- not analyzed here either.
14525 elsif Nkind (Decl) = N_Package_Instantiation
14526 and then not Is_Internal (Defining_Entity (Decl))
14527 then
14528 Append_Elmt (Decl, Previous_Instances);
14530 -- For a subprogram instantiation, omit instantiations intrinsic
14531 -- operations (Unchecked_Conversions, etc.) that have no bodies.
14533 elsif Nkind (Decl) in N_Function_Instantiation
14534 | N_Procedure_Instantiation
14535 and then not Is_Intrinsic_Subprogram (Entity (Name (Decl)))
14536 then
14537 Append_Elmt (Decl, Previous_Instances);
14539 elsif Nkind (Decl) = N_Package_Declaration then
14540 Collect_Previous_Instances
14541 (Visible_Declarations (Specification (Decl)));
14542 Collect_Previous_Instances
14543 (Private_Declarations (Specification (Decl)));
14545 -- Previous non-generic bodies may contain instances as well
14547 elsif Nkind (Decl) = N_Package_Body
14548 and then Ekind (Corresponding_Spec (Decl)) /= E_Generic_Package
14549 then
14550 Collect_Previous_Instances (Declarations (Decl));
14552 elsif Nkind (Decl) = N_Subprogram_Body
14553 and then not Acts_As_Spec (Decl)
14554 and then not Is_Generic_Subprogram (Corresponding_Spec (Decl))
14555 then
14556 Collect_Previous_Instances (Declarations (Decl));
14557 end if;
14559 Next (Decl);
14560 end loop;
14561 end Collect_Previous_Instances;
14563 -- Start of processing for Load_Parent_Of_Generic
14565 begin
14566 if not In_Same_Source_Unit (N, Spec)
14567 or else Nkind (Unit (Comp_Unit)) = N_Package_Declaration
14568 or else (Nkind (Unit (Comp_Unit)) = N_Package_Body
14569 and then not Is_In_Main_Unit (Spec))
14570 then
14571 -- Find body of parent of spec, and analyze it. A special case arises
14572 -- when the parent is an instantiation, that is to say when we are
14573 -- currently instantiating a nested generic. In that case, there is
14574 -- no separate file for the body of the enclosing instance. Instead,
14575 -- the enclosing body must be instantiated as if it were a pending
14576 -- instantiation, in order to produce the body for the nested generic
14577 -- we require now. Note that in that case the generic may be defined
14578 -- in a package body, the instance defined in the same package body,
14579 -- and the original enclosing body may not be in the main unit.
14581 Inst_Node := Empty;
14583 True_Parent := Parent (Spec);
14584 while Present (True_Parent)
14585 and then Nkind (True_Parent) /= N_Compilation_Unit
14586 loop
14587 if Nkind (True_Parent) = N_Package_Declaration
14588 and then
14589 Nkind (Original_Node (True_Parent)) = N_Package_Instantiation
14590 then
14591 -- Parent is a compilation unit that is an instantiation, and
14592 -- instantiation node has been replaced with package decl.
14594 Inst_Node := Original_Node (True_Parent);
14595 exit;
14597 elsif Nkind (True_Parent) = N_Package_Declaration
14598 and then Nkind (Parent (True_Parent)) = N_Compilation_Unit
14599 and then
14600 Nkind (Unit (Parent (True_Parent))) = N_Package_Instantiation
14601 then
14602 -- Parent is a compilation unit that is an instantiation, but
14603 -- instantiation node has not been replaced with package decl.
14605 Inst_Node := Unit (Parent (True_Parent));
14606 exit;
14608 elsif Nkind (True_Parent) = N_Package_Declaration
14609 and then Nkind (Parent (True_Parent)) /= N_Compilation_Unit
14610 and then Present (Generic_Parent (Specification (True_Parent)))
14611 then
14612 -- Parent is an instantiation within another specification.
14613 -- Declaration for instance has been inserted before original
14614 -- instantiation node. A direct link would be preferable?
14616 Inst_Node := Next (True_Parent);
14617 while Present (Inst_Node)
14618 and then Nkind (Inst_Node) /= N_Package_Instantiation
14619 loop
14620 Next (Inst_Node);
14621 end loop;
14623 -- If the instance appears within a generic, and the generic
14624 -- unit is defined within a formal package of the enclosing
14625 -- generic, there is no generic body available, and none
14626 -- needed. A more precise test should be used ???
14628 if No (Inst_Node) then
14629 return;
14630 end if;
14632 exit;
14634 -- If an ancestor of the generic comes from a formal package
14635 -- there is no source for the ancestor body. This is detected
14636 -- by examining the scope of the ancestor and its declaration.
14637 -- The body, if any is needed, will be available when the
14638 -- current unit (containing a formal package) is instantiated.
14640 elsif Nkind (True_Parent) = N_Package_Specification
14641 and then Present (Generic_Parent (True_Parent))
14642 and then Nkind
14643 (Original_Node (Unit_Declaration_Node
14644 (Scope (Generic_Parent (True_Parent)))))
14645 = N_Formal_Package_Declaration
14646 then
14647 return;
14649 else
14650 True_Parent := Parent (True_Parent);
14651 end if;
14652 end loop;
14654 -- Case where we are currently instantiating a nested generic
14656 if Present (Inst_Node) then
14657 if Nkind (Parent (True_Parent)) = N_Compilation_Unit then
14659 -- Instantiation node and declaration of instantiated package
14660 -- were exchanged when only the declaration was needed.
14661 -- Restore instantiation node before proceeding with body.
14663 Set_Unit (Parent (True_Parent), Inst_Node);
14664 end if;
14666 -- Now complete instantiation of enclosing body, if it appears in
14667 -- some other unit. If it appears in the current unit, the body
14668 -- will have been instantiated already.
14670 if No (Corresponding_Body (Instance_Spec (Inst_Node))) then
14672 -- We need to determine the expander mode to instantiate the
14673 -- enclosing body. Because the generic body we need may use
14674 -- global entities declared in the enclosing package (including
14675 -- aggregates) it is in general necessary to compile this body
14676 -- with expansion enabled, except if we are within a generic
14677 -- package, in which case the usual generic rule applies.
14679 declare
14680 Exp_Status : Boolean := True;
14681 Scop : Entity_Id;
14683 begin
14684 -- Loop through scopes looking for generic package
14686 Scop := Scope (Defining_Entity (Instance_Spec (Inst_Node)));
14687 while Present (Scop)
14688 and then Scop /= Standard_Standard
14689 loop
14690 if Ekind (Scop) = E_Generic_Package then
14691 Exp_Status := False;
14692 exit;
14693 end if;
14695 Scop := Scope (Scop);
14696 end loop;
14698 -- Collect previous instantiations in the unit that contains
14699 -- the desired generic.
14701 if Nkind (Parent (True_Parent)) /= N_Compilation_Unit
14702 and then not Body_Optional
14703 then
14704 declare
14705 Decl : Elmt_Id;
14706 Info : Pending_Body_Info;
14707 Par : Node_Id;
14709 begin
14710 Par := Parent (Inst_Node);
14711 while Present (Par) loop
14712 exit when Nkind (Parent (Par)) = N_Compilation_Unit;
14713 Par := Parent (Par);
14714 end loop;
14716 pragma Assert (Present (Par));
14718 if Nkind (Par) = N_Package_Body then
14719 Collect_Previous_Instances (Declarations (Par));
14721 elsif Nkind (Par) = N_Package_Declaration then
14722 Collect_Previous_Instances
14723 (Visible_Declarations (Specification (Par)));
14724 Collect_Previous_Instances
14725 (Private_Declarations (Specification (Par)));
14727 else
14728 -- Enclosing unit is a subprogram body. In this
14729 -- case all instance bodies are processed in order
14730 -- and there is no need to collect them separately.
14732 null;
14733 end if;
14735 Decl := First_Elmt (Previous_Instances);
14736 while Present (Decl) loop
14737 Info :=
14738 (Act_Decl =>
14739 Instance_Spec (Node (Decl)),
14740 Config_Switches => Save_Config_Switches,
14741 Current_Sem_Unit =>
14742 Get_Code_Unit (Sloc (Node (Decl))),
14743 Expander_Status => Exp_Status,
14744 Inst_Node => Node (Decl),
14745 Local_Suppress_Stack_Top =>
14746 Local_Suppress_Stack_Top,
14747 Scope_Suppress => Scope_Suppress,
14748 Warnings => Save_Warnings);
14750 -- Package instance
14752 if Nkind (Node (Decl)) = N_Package_Instantiation
14753 then
14754 Instantiate_Package_Body
14755 (Info, Body_Optional => True);
14757 -- Subprogram instance
14759 else
14760 -- The instance_spec is in the wrapper package,
14761 -- usually followed by its local renaming
14762 -- declaration. See Build_Subprogram_Renaming
14763 -- for details. If the instance carries aspects,
14764 -- these result in the corresponding pragmas,
14765 -- inserted after the subprogram declaration.
14766 -- They must be skipped as well when retrieving
14767 -- the desired spec. Some of them may have been
14768 -- rewritten as null statements.
14769 -- A direct link would be more robust ???
14771 declare
14772 Decl : Node_Id :=
14773 (Last (Visible_Declarations
14774 (Specification (Info.Act_Decl))));
14775 begin
14776 while Nkind (Decl) in
14777 N_Null_Statement |
14778 N_Pragma |
14779 N_Subprogram_Renaming_Declaration
14780 loop
14781 Decl := Prev (Decl);
14782 end loop;
14784 Info.Act_Decl := Decl;
14785 end;
14787 Instantiate_Subprogram_Body
14788 (Info, Body_Optional => True);
14789 end if;
14791 Next_Elmt (Decl);
14792 end loop;
14793 end;
14794 end if;
14796 Instantiate_Package_Body
14797 (Body_Info =>
14798 ((Act_Decl => True_Parent,
14799 Config_Switches => Save_Config_Switches,
14800 Current_Sem_Unit =>
14801 Get_Code_Unit (Sloc (Inst_Node)),
14802 Expander_Status => Exp_Status,
14803 Inst_Node => Inst_Node,
14804 Local_Suppress_Stack_Top => Local_Suppress_Stack_Top,
14805 Scope_Suppress => Scope_Suppress,
14806 Warnings => Save_Warnings)),
14807 Body_Optional => Body_Optional);
14808 end;
14809 end if;
14811 -- Case where we are not instantiating a nested generic
14813 else
14814 Opt.Style_Check := False;
14815 Expander_Mode_Save_And_Set (True);
14816 Load_Needed_Body (Comp_Unit, OK);
14817 Opt.Style_Check := Saved_Style_Check;
14818 Restore_Warnings (Saved_Warnings);
14819 Expander_Mode_Restore;
14821 if not OK
14822 and then Unit_Requires_Body (Defining_Entity (Spec))
14823 and then not Body_Optional
14824 then
14825 declare
14826 Bname : constant Unit_Name_Type :=
14827 Get_Body_Name (Get_Unit_Name (Unit (Comp_Unit)));
14829 begin
14830 -- In CodePeer mode, the missing body may make the analysis
14831 -- incomplete, but we do not treat it as fatal.
14833 if CodePeer_Mode then
14834 return;
14836 else
14837 Error_Msg_Unit_1 := Bname;
14838 Error_Msg_N ("this instantiation requires$!", N);
14839 Error_Msg_File_1 :=
14840 Get_File_Name (Bname, Subunit => False);
14841 Error_Msg_N ("\but file{ was not found!", N);
14842 raise Unrecoverable_Error;
14843 end if;
14844 end;
14845 end if;
14846 end if;
14847 end if;
14849 -- If loading parent of the generic caused an instantiation circularity,
14850 -- we abandon compilation at this point, because otherwise in some cases
14851 -- we get into trouble with infinite recursions after this point.
14853 if Circularity_Detected then
14854 raise Unrecoverable_Error;
14855 end if;
14856 end Load_Parent_Of_Generic;
14858 ---------------------------------
14859 -- Map_Formal_Package_Entities --
14860 ---------------------------------
14862 procedure Map_Formal_Package_Entities (Form : Entity_Id; Act : Entity_Id) is
14863 E1 : Entity_Id;
14864 E2 : Entity_Id;
14866 begin
14867 Set_Instance_Of (Form, Act);
14869 -- Traverse formal and actual package to map the corresponding entities.
14870 -- We skip over internal entities that may be generated during semantic
14871 -- analysis, and find the matching entities by name, given that they
14872 -- must appear in the same order.
14874 E1 := First_Entity (Form);
14875 E2 := First_Entity (Act);
14876 while Present (E1) and then E1 /= First_Private_Entity (Form) loop
14877 -- Could this test be a single condition??? Seems like it could, and
14878 -- isn't FPE (Form) a constant anyway???
14880 if not Is_Internal (E1)
14881 and then Present (Parent (E1))
14882 and then not Is_Class_Wide_Type (E1)
14883 and then not Is_Internal_Name (Chars (E1))
14884 then
14885 while Present (E2) and then Chars (E2) /= Chars (E1) loop
14886 Next_Entity (E2);
14887 end loop;
14889 if No (E2) then
14890 exit;
14891 else
14892 Set_Instance_Of (E1, E2);
14894 if Is_Type (E1) and then Is_Tagged_Type (E2) then
14895 Set_Instance_Of (Class_Wide_Type (E1), Class_Wide_Type (E2));
14896 end if;
14898 if Is_Constrained (E1) then
14899 Set_Instance_Of (Base_Type (E1), Base_Type (E2));
14900 end if;
14902 if Ekind (E1) = E_Package and then No (Renamed_Entity (E1)) then
14903 Map_Formal_Package_Entities (E1, E2);
14904 end if;
14905 end if;
14906 end if;
14908 Next_Entity (E1);
14909 end loop;
14910 end Map_Formal_Package_Entities;
14912 -----------------------
14913 -- Move_Freeze_Nodes --
14914 -----------------------
14916 procedure Move_Freeze_Nodes
14917 (Out_Of : Entity_Id;
14918 After : Node_Id;
14919 L : List_Id)
14921 Decl : Node_Id;
14922 Next_Decl : Node_Id;
14923 Next_Node : Node_Id := After;
14924 Spec : Node_Id;
14926 function Is_Outer_Type (T : Entity_Id) return Boolean;
14927 -- Check whether entity is declared in a scope external to that of the
14928 -- generic unit.
14930 -------------------
14931 -- Is_Outer_Type --
14932 -------------------
14934 function Is_Outer_Type (T : Entity_Id) return Boolean is
14935 Scop : Entity_Id := Scope (T);
14937 begin
14938 if Scope_Depth (Scop) < Scope_Depth (Out_Of) then
14939 return True;
14941 else
14942 while Scop /= Standard_Standard loop
14943 if Scop = Out_Of then
14944 return False;
14945 else
14946 Scop := Scope (Scop);
14947 end if;
14948 end loop;
14950 return True;
14951 end if;
14952 end Is_Outer_Type;
14954 -- Start of processing for Move_Freeze_Nodes
14956 begin
14957 if No (L) then
14958 return;
14959 end if;
14961 -- First remove the freeze nodes that may appear before all other
14962 -- declarations.
14964 Decl := First (L);
14965 while Present (Decl)
14966 and then Nkind (Decl) = N_Freeze_Entity
14967 and then Is_Outer_Type (Entity (Decl))
14968 loop
14969 Decl := Remove_Head (L);
14970 Insert_After (Next_Node, Decl);
14971 Set_Analyzed (Decl, False);
14972 Next_Node := Decl;
14973 Decl := First (L);
14974 end loop;
14976 -- Next scan the list of declarations and remove each freeze node that
14977 -- appears ahead of the current node.
14979 while Present (Decl) loop
14980 while Present (Next (Decl))
14981 and then Nkind (Next (Decl)) = N_Freeze_Entity
14982 and then Is_Outer_Type (Entity (Next (Decl)))
14983 loop
14984 Next_Decl := Remove_Next (Decl);
14985 Insert_After (Next_Node, Next_Decl);
14986 Set_Analyzed (Next_Decl, False);
14987 Next_Node := Next_Decl;
14988 end loop;
14990 -- If the declaration is a nested package or concurrent type, then
14991 -- recurse. Nested generic packages will have been processed from the
14992 -- inside out.
14994 case Nkind (Decl) is
14995 when N_Package_Declaration =>
14996 Spec := Specification (Decl);
14998 when N_Task_Type_Declaration =>
14999 Spec := Task_Definition (Decl);
15001 when N_Protected_Type_Declaration =>
15002 Spec := Protected_Definition (Decl);
15004 when others =>
15005 Spec := Empty;
15006 end case;
15008 if Present (Spec) then
15009 Move_Freeze_Nodes (Out_Of, Next_Node, Visible_Declarations (Spec));
15010 Move_Freeze_Nodes (Out_Of, Next_Node, Private_Declarations (Spec));
15011 end if;
15013 Next (Decl);
15014 end loop;
15015 end Move_Freeze_Nodes;
15017 ----------------
15018 -- Next_Assoc --
15019 ----------------
15021 function Next_Assoc (E : Assoc_Ptr) return Assoc_Ptr is
15022 begin
15023 return Generic_Renamings.Table (E).Next_In_HTable;
15024 end Next_Assoc;
15026 ------------------------
15027 -- Preanalyze_Actuals --
15028 ------------------------
15030 procedure Preanalyze_Actuals (N : Node_Id; Inst : Entity_Id := Empty) is
15031 procedure Perform_Appropriate_Analysis (N : Node_Id);
15032 -- Determine if the actuals we are analyzing come from a generic
15033 -- instantiation that is a library unit and dispatch accordingly.
15035 ----------------------------------
15036 -- Perform_Appropriate_Analysis --
15037 ----------------------------------
15039 procedure Perform_Appropriate_Analysis (N : Node_Id) is
15040 begin
15041 -- When we have a library instantiation we cannot allow any expansion
15042 -- to occur, since there may be no place to put it. Instead, in that
15043 -- case we perform a preanalysis of the actual.
15045 if Present (Inst) and then Is_Compilation_Unit (Inst) then
15046 Preanalyze (N);
15047 else
15048 Analyze (N);
15049 end if;
15050 end Perform_Appropriate_Analysis;
15052 -- Local variables
15054 Errs : constant Nat := Serious_Errors_Detected;
15056 Assoc : Node_Id;
15057 Act : Node_Id;
15059 Cur : Entity_Id := Empty;
15060 -- Current homograph of the instance name
15062 Vis : Boolean := False;
15063 -- Saved visibility status of the current homograph
15065 -- Start of processing for Preanalyze_Actuals
15067 begin
15068 Assoc := First (Generic_Associations (N));
15070 -- If the instance is a child unit, its name may hide an outer homonym,
15071 -- so make it invisible to perform name resolution on the actuals.
15073 if Nkind (Defining_Unit_Name (N)) = N_Defining_Program_Unit_Name
15074 and then Present
15075 (Current_Entity (Defining_Identifier (Defining_Unit_Name (N))))
15076 then
15077 Cur := Current_Entity (Defining_Identifier (Defining_Unit_Name (N)));
15079 if Is_Compilation_Unit (Cur) then
15080 Vis := Is_Immediately_Visible (Cur);
15081 Set_Is_Immediately_Visible (Cur, False);
15082 else
15083 Cur := Empty;
15084 end if;
15085 end if;
15087 while Present (Assoc) loop
15088 if Nkind (Assoc) /= N_Others_Choice then
15089 Act := Explicit_Generic_Actual_Parameter (Assoc);
15091 -- Within a nested instantiation, a defaulted actual is an empty
15092 -- association, so nothing to analyze. If the subprogram actual
15093 -- is an attribute, analyze prefix only, because actual is not a
15094 -- complete attribute reference.
15096 -- If actual is an allocator, analyze expression only. The full
15097 -- analysis can generate code, and if instance is a compilation
15098 -- unit we have to wait until the package instance is installed
15099 -- to have a proper place to insert this code.
15101 -- String literals may be operators, but at this point we do not
15102 -- know whether the actual is a formal subprogram or a string.
15104 if No (Act) then
15105 null;
15107 elsif Nkind (Act) = N_Attribute_Reference then
15108 Perform_Appropriate_Analysis (Prefix (Act));
15110 elsif Nkind (Act) = N_Explicit_Dereference then
15111 Perform_Appropriate_Analysis (Prefix (Act));
15113 elsif Nkind (Act) = N_Allocator then
15114 declare
15115 Expr : constant Node_Id := Expression (Act);
15117 begin
15118 if Nkind (Expr) = N_Subtype_Indication then
15119 Perform_Appropriate_Analysis (Subtype_Mark (Expr));
15121 -- Analyze separately each discriminant constraint, when
15122 -- given with a named association.
15124 declare
15125 Constr : Node_Id;
15127 begin
15128 Constr := First (Constraints (Constraint (Expr)));
15129 while Present (Constr) loop
15130 if Nkind (Constr) = N_Discriminant_Association then
15131 Perform_Appropriate_Analysis
15132 (Expression (Constr));
15133 else
15134 Perform_Appropriate_Analysis (Constr);
15135 end if;
15137 Next (Constr);
15138 end loop;
15139 end;
15141 else
15142 Perform_Appropriate_Analysis (Expr);
15143 end if;
15144 end;
15146 elsif Nkind (Act) /= N_Operator_Symbol then
15147 Perform_Appropriate_Analysis (Act);
15149 -- Within a package instance, mark actuals that are limited
15150 -- views, so their use can be moved to the body of the
15151 -- enclosing unit.
15153 if Is_Entity_Name (Act)
15154 and then Is_Type (Entity (Act))
15155 and then From_Limited_With (Entity (Act))
15156 and then Present (Inst)
15157 then
15158 Append_Elmt (Entity (Act), Incomplete_Actuals (Inst));
15159 end if;
15160 end if;
15162 if Errs /= Serious_Errors_Detected then
15164 -- Do a minimal analysis of the generic, to prevent spurious
15165 -- warnings complaining about the generic being unreferenced,
15166 -- before abandoning the instantiation.
15168 Perform_Appropriate_Analysis (Name (N));
15170 if Is_Entity_Name (Name (N))
15171 and then Etype (Name (N)) /= Any_Type
15172 then
15173 Generate_Reference (Entity (Name (N)), Name (N));
15174 Set_Is_Instantiated (Entity (Name (N)));
15175 end if;
15177 if Present (Cur) then
15179 -- For the case of a child instance hiding an outer homonym,
15180 -- provide additional warning which might explain the error.
15182 Set_Is_Immediately_Visible (Cur, Vis);
15183 Error_Msg_NE
15184 ("& hides outer unit with the same name??",
15185 N, Defining_Unit_Name (N));
15186 end if;
15188 Abandon_Instantiation (Act);
15189 end if;
15190 end if;
15192 Next (Assoc);
15193 end loop;
15195 if Present (Cur) then
15196 Set_Is_Immediately_Visible (Cur, Vis);
15197 end if;
15198 end Preanalyze_Actuals;
15200 -------------------------------
15201 -- Provide_Completing_Bodies --
15202 -------------------------------
15204 procedure Provide_Completing_Bodies (N : Node_Id) is
15205 procedure Build_Completing_Body (Subp_Decl : Node_Id);
15206 -- Generate the completing body for subprogram declaration Subp_Decl
15208 procedure Provide_Completing_Bodies_In (Decls : List_Id);
15209 -- Generating completing bodies for all subprograms found in declarative
15210 -- list Decls.
15212 ---------------------------
15213 -- Build_Completing_Body --
15214 ---------------------------
15216 procedure Build_Completing_Body (Subp_Decl : Node_Id) is
15217 Loc : constant Source_Ptr := Sloc (Subp_Decl);
15218 Subp_Id : constant Entity_Id := Defining_Entity (Subp_Decl);
15219 Spec : Node_Id;
15221 begin
15222 -- Nothing to do if the subprogram already has a completing body
15224 if Present (Corresponding_Body (Subp_Decl)) then
15225 return;
15227 -- Mark the function as having a valid return statement even though
15228 -- the body contains a single raise statement.
15230 elsif Ekind (Subp_Id) = E_Function then
15231 Set_Return_Present (Subp_Id);
15232 end if;
15234 -- Clone the specification to obtain new entities and reset the only
15235 -- semantic field.
15237 Spec := Copy_Subprogram_Spec (Specification (Subp_Decl));
15238 Set_Generic_Parent (Spec, Empty);
15240 -- Generate:
15241 -- function Func ... return ... is
15242 -- <or>
15243 -- procedure Proc ... is
15244 -- begin
15245 -- raise Program_Error with "access before elaboration";
15246 -- edn Proc;
15248 Insert_After_And_Analyze (Subp_Decl,
15249 Make_Subprogram_Body (Loc,
15250 Specification => Spec,
15251 Declarations => New_List,
15252 Handled_Statement_Sequence =>
15253 Make_Handled_Sequence_Of_Statements (Loc,
15254 Statements => New_List (
15255 Make_Raise_Program_Error (Loc,
15256 Reason => PE_Access_Before_Elaboration)))));
15257 end Build_Completing_Body;
15259 ----------------------------------
15260 -- Provide_Completing_Bodies_In --
15261 ----------------------------------
15263 procedure Provide_Completing_Bodies_In (Decls : List_Id) is
15264 Decl : Node_Id;
15266 begin
15267 if Present (Decls) then
15268 Decl := First (Decls);
15269 while Present (Decl) loop
15270 Provide_Completing_Bodies (Decl);
15271 Next (Decl);
15272 end loop;
15273 end if;
15274 end Provide_Completing_Bodies_In;
15276 -- Local variables
15278 Spec : Node_Id;
15280 -- Start of processing for Provide_Completing_Bodies
15282 begin
15283 if Nkind (N) = N_Package_Declaration then
15284 Spec := Specification (N);
15286 Push_Scope (Defining_Entity (N));
15287 Provide_Completing_Bodies_In (Visible_Declarations (Spec));
15288 Provide_Completing_Bodies_In (Private_Declarations (Spec));
15289 Pop_Scope;
15291 elsif Nkind (N) = N_Subprogram_Declaration then
15292 Build_Completing_Body (N);
15293 end if;
15294 end Provide_Completing_Bodies;
15296 -------------------
15297 -- Remove_Parent --
15298 -------------------
15300 procedure Remove_Parent (In_Body : Boolean := False) is
15301 S : Entity_Id := Current_Scope;
15302 -- S is the scope containing the instantiation just completed. The scope
15303 -- stack contains the parent instances of the instantiation, followed by
15304 -- the original S.
15306 Cur_P : Entity_Id;
15307 E : Entity_Id;
15308 P : Entity_Id;
15309 Hidden : Elmt_Id;
15311 begin
15312 -- After child instantiation is complete, remove from scope stack the
15313 -- extra copy of the current scope, and then remove parent instances.
15315 if not In_Body then
15316 Pop_Scope;
15318 while Current_Scope /= S loop
15319 P := Current_Scope;
15320 End_Package_Scope (Current_Scope);
15322 if In_Open_Scopes (P) then
15323 E := First_Entity (P);
15324 while Present (E) loop
15325 Set_Is_Immediately_Visible (E, True);
15326 Next_Entity (E);
15327 end loop;
15329 -- If instantiation is declared in a block, it is the enclosing
15330 -- scope that might be a parent instance. Note that only one
15331 -- block can be involved, because the parent instances have
15332 -- been installed within it.
15334 if Ekind (P) = E_Block then
15335 Cur_P := Scope (P);
15336 else
15337 Cur_P := P;
15338 end if;
15340 if Is_Generic_Instance (Cur_P) and then P /= Current_Scope then
15341 -- We are within an instance of some sibling. Retain
15342 -- visibility of parent, for proper subsequent cleanup, and
15343 -- reinstall private declarations as well.
15345 Set_In_Private_Part (P);
15346 Install_Private_Declarations (P);
15347 end if;
15349 -- If the ultimate parent is a top-level unit recorded in
15350 -- Instance_Parent_Unit, then reset its visibility to what it was
15351 -- before instantiation. (It's not clear what the purpose is of
15352 -- testing whether Scope (P) is In_Open_Scopes, but that test was
15353 -- present before the ultimate parent test was added.???)
15355 elsif not In_Open_Scopes (Scope (P))
15356 or else (P = Instance_Parent_Unit
15357 and then not Parent_Unit_Visible)
15358 then
15359 Set_Is_Immediately_Visible (P, False);
15361 -- If the current scope is itself an instantiation of a generic
15362 -- nested within P, and we are in the private part of body of this
15363 -- instantiation, restore the full views of P, that were removed
15364 -- in End_Package_Scope above. This obscure case can occur when a
15365 -- subunit of a generic contains an instance of a child unit of
15366 -- its generic parent unit.
15368 elsif S = Current_Scope and then Is_Generic_Instance (S)
15369 and then (In_Package_Body (S) or else In_Private_Part (S))
15370 then
15371 declare
15372 Par : constant Entity_Id :=
15373 Generic_Parent (Package_Specification (S));
15374 begin
15375 if Present (Par)
15376 and then P = Scope (Par)
15377 then
15378 Set_In_Private_Part (P);
15379 Install_Private_Declarations (P);
15380 end if;
15381 end;
15382 end if;
15383 end loop;
15385 -- Reset visibility of entities in the enclosing scope
15387 Set_Is_Hidden_Open_Scope (Current_Scope, False);
15389 Hidden := First_Elmt (Hidden_Entities);
15390 while Present (Hidden) loop
15391 Set_Is_Immediately_Visible (Node (Hidden), True);
15392 Next_Elmt (Hidden);
15393 end loop;
15395 else
15396 -- Each body is analyzed separately, and there is no context that
15397 -- needs preserving from one body instance to the next, so remove all
15398 -- parent scopes that have been installed.
15400 while Present (S) loop
15401 End_Package_Scope (S);
15402 Set_Is_Immediately_Visible (S, False);
15403 S := Current_Scope;
15404 exit when S = Standard_Standard;
15405 end loop;
15406 end if;
15407 end Remove_Parent;
15409 -----------------
15410 -- Restore_Env --
15411 -----------------
15413 procedure Restore_Env is
15414 Saved : Instance_Env renames Instance_Envs.Table (Instance_Envs.Last);
15416 begin
15417 if No (Current_Instantiated_Parent.Act_Id) then
15418 -- Restore environment after subprogram inlining
15420 Restore_Private_Views (Empty);
15421 end if;
15423 Current_Instantiated_Parent := Saved.Instantiated_Parent;
15424 Exchanged_Views := Saved.Exchanged_Views;
15425 Hidden_Entities := Saved.Hidden_Entities;
15426 Current_Sem_Unit := Saved.Current_Sem_Unit;
15427 Parent_Unit_Visible := Saved.Parent_Unit_Visible;
15428 Instance_Parent_Unit := Saved.Instance_Parent_Unit;
15430 Restore_Config_Switches (Saved.Switches);
15432 Instance_Envs.Decrement_Last;
15433 end Restore_Env;
15435 ---------------------------
15436 -- Restore_Private_Views --
15437 ---------------------------
15439 procedure Restore_Private_Views
15440 (Pack_Id : Entity_Id;
15441 Is_Package : Boolean := True)
15443 M : Elmt_Id;
15444 E : Entity_Id;
15445 Typ : Entity_Id;
15446 Dep_Elmt : Elmt_Id;
15447 Dep_Typ : Node_Id;
15449 procedure Restore_Nested_Formal (Formal : Entity_Id);
15450 -- Hide the generic formals of formal packages declared with box which
15451 -- were reachable in the current instantiation.
15453 ---------------------------
15454 -- Restore_Nested_Formal --
15455 ---------------------------
15457 procedure Restore_Nested_Formal (Formal : Entity_Id) is
15458 pragma Assert (Ekind (Formal) = E_Package);
15459 Ent : Entity_Id;
15460 begin
15461 if Present (Renamed_Entity (Formal))
15462 and then Denotes_Formal_Package (Renamed_Entity (Formal), True)
15463 then
15464 return;
15466 elsif Present (Associated_Formal_Package (Formal)) then
15467 Ent := First_Entity (Formal);
15468 while Present (Ent) loop
15469 exit when Ekind (Ent) = E_Package
15470 and then Renamed_Entity (Ent) = Renamed_Entity (Formal);
15472 Set_Is_Hidden (Ent);
15473 Set_Is_Potentially_Use_Visible (Ent, False);
15475 -- If package, then recurse
15477 if Ekind (Ent) = E_Package then
15478 Restore_Nested_Formal (Ent);
15479 end if;
15481 Next_Entity (Ent);
15482 end loop;
15483 end if;
15484 end Restore_Nested_Formal;
15486 -- Start of processing for Restore_Private_Views
15488 begin
15489 M := First_Elmt (Exchanged_Views);
15490 while Present (M) loop
15491 Typ := Node (M);
15493 -- Subtypes of types whose views have been exchanged, and that are
15494 -- defined within the instance, were not on the Private_Dependents
15495 -- list on entry to the instance, so they have to be exchanged
15496 -- explicitly now, in order to remain consistent with the view of the
15497 -- parent type.
15499 if Ekind (Typ) in E_Private_Type
15500 | E_Limited_Private_Type
15501 | E_Record_Type_With_Private
15502 then
15503 Dep_Elmt := First_Elmt (Private_Dependents (Typ));
15504 while Present (Dep_Elmt) loop
15505 Dep_Typ := Node (Dep_Elmt);
15507 if Scope (Dep_Typ) = Pack_Id
15508 and then Present (Full_View (Dep_Typ))
15509 then
15510 Replace_Elmt (Dep_Elmt, Full_View (Dep_Typ));
15511 Exchange_Declarations (Dep_Typ);
15512 end if;
15514 Next_Elmt (Dep_Elmt);
15515 end loop;
15516 end if;
15518 Exchange_Declarations (Node (M));
15519 Next_Elmt (M);
15520 end loop;
15522 if No (Pack_Id) then
15523 return;
15524 end if;
15526 -- Make the generic formal parameters private, and make the formal types
15527 -- into subtypes of the actuals again.
15529 E := First_Entity (Pack_Id);
15530 while Present (E) loop
15531 Set_Is_Hidden (E, True);
15533 if Is_Type (E)
15534 and then Nkind (Parent (E)) = N_Subtype_Declaration
15535 then
15536 -- Always preserve the flag Is_Generic_Actual_Type for GNATprove,
15537 -- as it is needed to identify the subtype with the type it
15538 -- renames, when there are conversions between access types
15539 -- to these.
15541 if GNATprove_Mode then
15542 null;
15544 -- If the actual for E is itself a generic actual type from
15545 -- an enclosing instance, E is still a generic actual type
15546 -- outside of the current instance. This matter when resolving
15547 -- an overloaded call that may be ambiguous in the enclosing
15548 -- instance, when two of its actuals coincide.
15550 elsif Is_Entity_Name (Subtype_Indication (Parent (E)))
15551 and then Is_Generic_Actual_Type
15552 (Entity (Subtype_Indication (Parent (E))))
15553 then
15554 null;
15555 else
15556 Set_Is_Generic_Actual_Type (E, False);
15558 -- It might seem reasonable to clear the Is_Generic_Actual_Type
15559 -- flag also on the Full_View if the type is private, since it
15560 -- was set also on this Full_View. However, this flag is relied
15561 -- upon by Covers to spot "types exported from instantiations"
15562 -- which are implicit Full_Views built for instantiations made
15563 -- on private types and we get type mismatches if we do it when
15564 -- the block exchanging the declarations below triggers ???
15566 -- if Is_Private_Type (E) and then Present (Full_View (E)) then
15567 -- Set_Is_Generic_Actual_Type (Full_View (E), False);
15568 -- end if;
15569 end if;
15571 -- An unusual case of aliasing: the actual may also be directly
15572 -- visible in the generic, and be private there, while it is fully
15573 -- visible in the context of the instance. The internal subtype
15574 -- is private in the instance but has full visibility like its
15575 -- parent in the enclosing scope. This enforces the invariant that
15576 -- the privacy status of all private dependents of a type coincide
15577 -- with that of the parent type. This can only happen when a
15578 -- generic child unit is instantiated within a sibling.
15580 if Is_Private_Type (E)
15581 and then not Is_Private_Type (Etype (E))
15582 then
15583 Exchange_Declarations (E);
15584 end if;
15586 elsif Ekind (E) = E_Package then
15588 -- The end of the renaming list is the renaming of the generic
15589 -- package itself. If the instance is a subprogram, all entities
15590 -- in the corresponding package are renamings. If this entity is
15591 -- a formal package, make its own formals private as well. The
15592 -- actual in this case is itself the renaming of an instantiation.
15593 -- If the entity is not a package renaming, it is the entity
15594 -- created to validate formal package actuals: ignore it.
15596 -- If the actual is itself a formal package for the enclosing
15597 -- generic, or the actual for such a formal package, it remains
15598 -- visible on exit from the instance, and therefore nothing needs
15599 -- to be done either, except to keep it accessible.
15601 if Is_Package and then Renamed_Entity (E) = Pack_Id then
15602 exit;
15604 elsif Nkind (Parent (E)) /= N_Package_Renaming_Declaration then
15605 null;
15607 elsif
15608 Denotes_Formal_Package (Renamed_Entity (E), True, Pack_Id)
15609 then
15610 Set_Is_Hidden (E, False);
15612 else
15613 declare
15614 Act_P : constant Entity_Id := Renamed_Entity (E);
15615 Id : Entity_Id;
15617 begin
15618 Id := First_Entity (Act_P);
15619 while Present (Id)
15620 and then Id /= First_Private_Entity (Act_P)
15621 loop
15622 exit when Ekind (Id) = E_Package
15623 and then Renamed_Entity (Id) = Act_P;
15625 Set_Is_Hidden (Id, True);
15626 Set_Is_Potentially_Use_Visible (Id, In_Use (Act_P));
15628 if Ekind (Id) = E_Package then
15629 Restore_Nested_Formal (Id);
15630 end if;
15632 Next_Entity (Id);
15633 end loop;
15634 end;
15635 end if;
15636 end if;
15638 Next_Entity (E);
15639 end loop;
15640 end Restore_Private_Views;
15642 --------------
15643 -- Save_Env --
15644 --------------
15646 procedure Save_Env
15647 (Gen_Unit : Entity_Id;
15648 Act_Unit : Entity_Id)
15650 begin
15651 Init_Env;
15652 Set_Instance_Env (Gen_Unit, Act_Unit);
15653 end Save_Env;
15655 ----------------------------
15656 -- Save_Global_References --
15657 ----------------------------
15659 procedure Save_Global_References (Templ : Node_Id) is
15661 -- ??? it is horrible to use global variables in highly recursive code
15663 E : Entity_Id;
15664 -- The entity of the current associated node
15666 Gen_Scope : Entity_Id;
15667 -- The scope of the generic for which references are being saved
15669 N2 : Node_Id;
15670 -- The current associated node
15672 function Is_Global (E : Entity_Id) return Boolean;
15673 -- Check whether entity is defined outside of generic unit. Examine the
15674 -- scope of an entity, and the scope of the scope, etc, until we find
15675 -- either Standard, in which case the entity is global, or the generic
15676 -- unit itself, which indicates that the entity is local. If the entity
15677 -- is the generic unit itself, as in the case of a recursive call, or
15678 -- the enclosing generic unit, if different from the current scope, then
15679 -- it is local as well, because it will be replaced at the point of
15680 -- instantiation. On the other hand, if it is a reference to a child
15681 -- unit of a common ancestor, which appears in an instantiation, it is
15682 -- global because it is used to denote a specific compilation unit at
15683 -- the time the instantiations will be analyzed.
15685 procedure Qualify_Universal_Operands
15686 (Op : Node_Id;
15687 Func_Call : Node_Id);
15688 -- Op denotes a binary or unary operator in generic template Templ. Node
15689 -- Func_Call is the function call alternative of the operator within the
15690 -- the analyzed copy of the template. Change each operand which yields a
15691 -- universal type by wrapping it into a qualified expression
15693 -- Actual_Typ'(Operand)
15695 -- where Actual_Typ is the type of corresponding actual parameter of
15696 -- Operand in Func_Call.
15698 procedure Reset_Entity (N : Node_Id);
15699 -- Save semantic information on global entity so that it is not resolved
15700 -- again at instantiation time.
15702 procedure Save_Entity_Descendants (N : Node_Id);
15703 -- Apply Save_Global_References to the two syntactic descendants of
15704 -- non-terminal nodes that carry an Associated_Node and are processed
15705 -- through Reset_Entity. Once the global entity (if any) has been
15706 -- captured together with its type, only two syntactic descendants need
15707 -- to be traversed to complete the processing of the tree rooted at N.
15708 -- This applies to Selected_Components, Expanded_Names, and to Operator
15709 -- nodes. N can also be a character literal, identifier, or operator
15710 -- symbol node, but the call has no effect in these cases.
15712 procedure Save_Global_Defaults (N1 : Node_Id; N2 : Node_Id);
15713 -- Default actuals in nested instances must be handled specially
15714 -- because there is no link to them from the original tree. When an
15715 -- actual subprogram is given by a default, we add an explicit generic
15716 -- association for it in the instantiation node. When we save the
15717 -- global references on the name of the instance, we recover the list
15718 -- of generic associations, and add an explicit one to the original
15719 -- generic tree, through which a global actual can be preserved.
15720 -- Similarly, if a child unit is instantiated within a sibling, in the
15721 -- context of the parent, we must preserve the identifier of the parent
15722 -- so that it can be properly resolved in a subsequent instantiation.
15724 procedure Save_Global_Descendant (D : Union_Id);
15725 -- Apply Save_References recursively to the descendants of node D
15727 procedure Save_References (N : Node_Id);
15728 -- This is the recursive procedure that does the work, once the
15729 -- enclosing generic scope has been established.
15731 ---------------
15732 -- Is_Global --
15733 ---------------
15735 function Is_Global (E : Entity_Id) return Boolean is
15736 Se : Entity_Id;
15738 function Is_Instance_Node (Decl : Node_Id) return Boolean;
15739 -- Determine whether the parent node of a reference to a child unit
15740 -- denotes an instantiation or a formal package, in which case the
15741 -- reference to the child unit is global, even if it appears within
15742 -- the current scope (e.g. when the instance appears within the body
15743 -- of an ancestor).
15745 ----------------------
15746 -- Is_Instance_Node --
15747 ----------------------
15749 function Is_Instance_Node (Decl : Node_Id) return Boolean is
15750 begin
15751 return Nkind (Decl) in N_Generic_Instantiation
15752 or else
15753 Nkind (Original_Node (Decl)) = N_Formal_Package_Declaration;
15754 end Is_Instance_Node;
15756 -- Start of processing for Is_Global
15758 begin
15759 if E = Gen_Scope then
15760 return False;
15762 elsif E = Standard_Standard then
15763 return True;
15765 -- E should be an entity, but it is not always
15767 elsif Nkind (E) not in N_Entity then
15768 return False;
15770 elsif Nkind (E) /= N_Expanded_Name
15771 and then Is_Child_Unit (E)
15772 and then (Is_Instance_Node (Parent (N2))
15773 or else (Nkind (Parent (N2)) = N_Expanded_Name
15774 and then N2 = Selector_Name (Parent (N2))
15775 and then
15776 Is_Instance_Node (Parent (Parent (N2)))))
15777 then
15778 return True;
15780 else
15781 -- E may be an expanded name - typically an operator - in which
15782 -- case we must find its enclosing scope since expanded names
15783 -- don't have corresponding scopes.
15785 if Nkind (E) = N_Expanded_Name then
15786 Se := Find_Enclosing_Scope (E);
15788 -- Otherwise, E is an entity and will have Scope set
15790 else
15791 Se := Scope (E);
15792 end if;
15794 while Se /= Gen_Scope loop
15795 if Se = Standard_Standard then
15796 return True;
15797 else
15798 Se := Scope (Se);
15799 end if;
15800 end loop;
15802 return False;
15803 end if;
15804 end Is_Global;
15806 --------------------------------
15807 -- Qualify_Universal_Operands --
15808 --------------------------------
15810 procedure Qualify_Universal_Operands
15811 (Op : Node_Id;
15812 Func_Call : Node_Id)
15814 procedure Qualify_Operand (Opnd : Node_Id; Actual : Node_Id);
15815 -- Rewrite operand Opnd as a qualified expression of the form
15817 -- Actual_Typ'(Opnd)
15819 -- where Actual is the corresponding actual parameter of Opnd in
15820 -- function call Func_Call.
15822 function Qualify_Type
15823 (Loc : Source_Ptr;
15824 Typ : Entity_Id) return Node_Id;
15825 -- Qualify type Typ by creating a selected component of the form
15827 -- Scope_Of_Typ.Typ
15829 ---------------------
15830 -- Qualify_Operand --
15831 ---------------------
15833 procedure Qualify_Operand (Opnd : Node_Id; Actual : Node_Id) is
15834 Loc : constant Source_Ptr := Sloc (Opnd);
15835 Typ : constant Entity_Id := Etype (Actual);
15836 Mark : Node_Id;
15837 Qual : Node_Id;
15839 begin
15840 -- Qualify the operand when it is of a universal type. Note that
15841 -- the template is unanalyzed and it is not possible to directly
15842 -- query the type. This transformation is not done when the type
15843 -- of the actual is internally generated because the type will be
15844 -- regenerated in the instance.
15846 if Yields_Universal_Type (Opnd)
15847 and then Comes_From_Source (Typ)
15848 and then not Is_Hidden (Typ)
15849 then
15850 -- The type of the actual may be a global reference. Save this
15851 -- information by creating a reference to it.
15853 if Is_Global (Typ) then
15854 Mark := New_Occurrence_Of (Typ, Loc);
15856 -- Otherwise rely on resolution to find the proper type within
15857 -- the instance.
15859 else
15860 Mark := Qualify_Type (Loc, Typ);
15861 end if;
15863 Qual :=
15864 Make_Qualified_Expression (Loc,
15865 Subtype_Mark => Mark,
15866 Expression => Relocate_Node (Opnd));
15868 -- Mark the qualification to distinguish it from other source
15869 -- constructs and signal the instantiation mechanism that this
15870 -- node requires special processing. See Copy_Generic_Node for
15871 -- details.
15873 Set_Is_Qualified_Universal_Literal (Qual);
15875 Rewrite (Opnd, Qual);
15876 end if;
15877 end Qualify_Operand;
15879 ------------------
15880 -- Qualify_Type --
15881 ------------------
15883 function Qualify_Type
15884 (Loc : Source_Ptr;
15885 Typ : Entity_Id) return Node_Id
15887 Scop : constant Entity_Id := Scope (Typ);
15888 Result : Node_Id;
15890 begin
15891 Result := Make_Identifier (Loc, Chars (Typ));
15893 if Present (Scop) and then not Is_Generic_Unit (Scop) then
15894 Result :=
15895 Make_Selected_Component (Loc,
15896 Prefix => Make_Identifier (Loc, Chars (Scop)),
15897 Selector_Name => Result);
15898 end if;
15900 return Result;
15901 end Qualify_Type;
15903 -- Local variables
15905 Actuals : constant List_Id := Parameter_Associations (Func_Call);
15907 -- Start of processing for Qualify_Universal_Operands
15909 begin
15910 if Nkind (Op) in N_Binary_Op then
15911 Qualify_Operand (Left_Opnd (Op), First (Actuals));
15912 Qualify_Operand (Right_Opnd (Op), Next (First (Actuals)));
15914 elsif Nkind (Op) in N_Unary_Op then
15915 Qualify_Operand (Right_Opnd (Op), First (Actuals));
15916 end if;
15917 end Qualify_Universal_Operands;
15919 ------------------
15920 -- Reset_Entity --
15921 ------------------
15923 procedure Reset_Entity (N : Node_Id) is
15924 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id);
15925 -- If the type of N2 is global to the generic unit, save the type in
15926 -- the generic node. Just as we perform name capture for explicit
15927 -- references within the generic, we must capture the global types
15928 -- of local entities because they may participate in resolution in
15929 -- the instance.
15931 function Top_Ancestor (E : Entity_Id) return Entity_Id;
15932 -- Find the ultimate ancestor of the current unit. If it is not a
15933 -- generic unit, then the name of the current unit in the prefix of
15934 -- an expanded name must be replaced with its generic homonym to
15935 -- ensure that it will be properly resolved in an instance.
15937 ---------------------
15938 -- Set_Global_Type --
15939 ---------------------
15941 procedure Set_Global_Type (N : Node_Id; N2 : Node_Id) is
15942 Typ : constant Entity_Id := Etype (N2);
15944 begin
15945 Set_Etype (N, Typ);
15947 -- If the entity of N is not the associated node, this is a
15948 -- nested generic and it has an associated node as well, whose
15949 -- type is already the full view (see below). Indicate that the
15950 -- original node has a private view.
15952 if Entity (N) /= N2 and then Has_Private_View (Entity (N)) then
15953 Set_Has_Private_View (N);
15954 end if;
15956 -- If not a private type, nothing else to do
15958 if not Is_Private_Type (Typ) then
15959 null;
15961 -- If it is a derivation of a private type in a context where no
15962 -- full view is needed, nothing to do either.
15964 elsif No (Full_View (Typ)) and then Typ /= Etype (Typ) then
15965 null;
15967 -- Otherwise mark the type for flipping and use the full view when
15968 -- available.
15970 else
15971 Set_Has_Private_View (N);
15973 if Present (Full_View (Typ)) then
15974 Set_Etype (N2, Full_View (Typ));
15975 end if;
15976 end if;
15978 if Is_Floating_Point_Type (Typ)
15979 and then Has_Dimension_System (Typ)
15980 then
15981 Copy_Dimensions (N2, N);
15982 end if;
15983 end Set_Global_Type;
15985 ------------------
15986 -- Top_Ancestor --
15987 ------------------
15989 function Top_Ancestor (E : Entity_Id) return Entity_Id is
15990 Par : Entity_Id;
15992 begin
15993 Par := E;
15994 while Is_Child_Unit (Par) loop
15995 Par := Scope (Par);
15996 end loop;
15998 return Par;
15999 end Top_Ancestor;
16001 -- Start of processing for Reset_Entity
16003 begin
16004 N2 := Get_Associated_Node (N);
16005 E := Entity (N2);
16007 if Present (E) then
16009 -- If the node is an entry call to an entry in an enclosing task,
16010 -- it is rewritten as a selected component. No global entity to
16011 -- preserve in this case, since the expansion will be redone in
16012 -- the instance.
16014 if Nkind (E) not in N_Entity then
16015 Set_Associated_Node (N, Empty);
16016 Set_Etype (N, Empty);
16017 return;
16018 end if;
16020 -- If the entity is an itype created as a subtype of an access
16021 -- type with a null exclusion restore source entity for proper
16022 -- visibility. The itype will be created anew in the instance.
16024 if Is_Itype (E)
16025 and then Ekind (E) = E_Access_Subtype
16026 and then Is_Entity_Name (N)
16027 and then Chars (Etype (E)) = Chars (N)
16028 then
16029 E := Etype (E);
16030 Set_Entity (N2, E);
16031 Set_Etype (N2, E);
16032 end if;
16034 if Is_Global (E) then
16035 Set_Global_Type (N, N2);
16037 elsif Nkind (N) = N_Op_Concat
16038 and then Is_Generic_Type (Etype (N2))
16039 and then (Base_Type (Etype (Right_Opnd (N2))) = Etype (N2)
16040 or else
16041 Base_Type (Etype (Left_Opnd (N2))) = Etype (N2))
16042 and then Is_Intrinsic_Subprogram (E)
16043 then
16044 null;
16046 -- Entity is local. Mark generic node as unresolved. Note that now
16047 -- it does not have an entity.
16049 else
16050 Set_Associated_Node (N, Empty);
16051 Set_Etype (N, Empty);
16052 end if;
16054 if Nkind (Parent (N)) in N_Generic_Instantiation
16055 and then N = Name (Parent (N))
16056 then
16057 Save_Global_Defaults (Parent (N), Parent (N2));
16058 end if;
16060 elsif Nkind (Parent (N)) = N_Selected_Component
16061 and then Nkind (Parent (N2)) = N_Expanded_Name
16062 then
16063 -- In case of previous errors, the tree might be malformed
16065 if No (Entity (Parent (N2))) then
16066 null;
16068 elsif Is_Global (Entity (Parent (N2))) then
16069 Change_Selected_Component_To_Expanded_Name (Parent (N));
16070 Set_Associated_Node (Parent (N), Parent (N2));
16071 Set_Global_Type (Parent (N), Parent (N2));
16072 Save_Entity_Descendants (N);
16074 -- If this is a reference to the current generic entity, replace
16075 -- by the name of the generic homonym of the current package. This
16076 -- is because in an instantiation Par.P.Q will not resolve to the
16077 -- name of the instance, whose enclosing scope is not necessarily
16078 -- Par. We use the generic homonym rather that the name of the
16079 -- generic itself because it may be hidden by a local declaration.
16081 elsif In_Open_Scopes (Entity (Parent (N2)))
16082 and then not
16083 Is_Generic_Unit (Top_Ancestor (Entity (Prefix (Parent (N2)))))
16084 then
16085 if Ekind (Entity (Parent (N2))) = E_Generic_Package then
16086 Rewrite (Parent (N),
16087 Make_Identifier (Sloc (N),
16088 Chars =>
16089 Chars (Generic_Homonym (Entity (Parent (N2))))));
16090 else
16091 Rewrite (Parent (N),
16092 Make_Identifier (Sloc (N),
16093 Chars => Chars (Selector_Name (Parent (N2)))));
16094 end if;
16095 end if;
16097 if Nkind (Parent (Parent (N))) in N_Generic_Instantiation
16098 and then Parent (N) = Name (Parent (Parent (N)))
16099 then
16100 Save_Global_Defaults
16101 (Parent (Parent (N)), Parent (Parent (N2)));
16102 end if;
16104 -- A selected component may denote a static constant that has been
16105 -- folded. If the static constant is global to the generic, capture
16106 -- its value. Otherwise the folding will happen in any instantiation.
16108 elsif Nkind (Parent (N)) = N_Selected_Component
16109 and then Nkind (Parent (N2)) in N_Integer_Literal | N_Real_Literal
16110 then
16111 if Present (Entity (Original_Node (Parent (N2))))
16112 and then Is_Global (Entity (Original_Node (Parent (N2))))
16113 then
16114 Rewrite (Parent (N), New_Copy (Parent (N2)));
16115 Set_Analyzed (Parent (N), False);
16116 end if;
16118 -- A selected component may be transformed into a parameterless
16119 -- function call. If the called entity is global, rewrite the node
16120 -- appropriately, i.e. as an extended name for the global entity.
16122 elsif Nkind (Parent (N)) = N_Selected_Component
16123 and then Nkind (Parent (N2)) = N_Function_Call
16124 and then N = Selector_Name (Parent (N))
16125 then
16126 if No (Parameter_Associations (Parent (N2))) then
16127 if Is_Global (Entity (Name (Parent (N2)))) then
16128 Change_Selected_Component_To_Expanded_Name (Parent (N));
16129 Set_Associated_Node (Parent (N), Name (Parent (N2)));
16130 Set_Global_Type (Parent (N), Name (Parent (N2)));
16131 Save_Entity_Descendants (N);
16133 else
16134 Set_Is_Prefixed_Call (Parent (N));
16135 Set_Associated_Node (N, Empty);
16136 Set_Etype (N, Empty);
16137 end if;
16139 -- In Ada 2005, X.F may be a call to a primitive operation,
16140 -- rewritten as F (X). This rewriting will be done again in an
16141 -- instance, so keep the original node. Global entities will be
16142 -- captured as for other constructs. Indicate that this must
16143 -- resolve as a call, to prevent accidental overloading in the
16144 -- instance, if both a component and a primitive operation appear
16145 -- as candidates.
16147 else
16148 Set_Is_Prefixed_Call (Parent (N));
16149 end if;
16151 -- Entity is local. Reset in generic unit, so that node is resolved
16152 -- anew at the point of instantiation.
16154 else
16155 Set_Associated_Node (N, Empty);
16156 Set_Etype (N, Empty);
16157 end if;
16158 end Reset_Entity;
16160 -----------------------------
16161 -- Save_Entity_Descendants --
16162 -----------------------------
16164 procedure Save_Entity_Descendants (N : Node_Id) is
16165 begin
16166 case Nkind (N) is
16167 when N_Binary_Op =>
16168 Save_Global_Descendant (Union_Id (Left_Opnd (N)));
16169 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
16171 when N_Unary_Op =>
16172 Save_Global_Descendant (Union_Id (Right_Opnd (N)));
16174 when N_Expanded_Name
16175 | N_Selected_Component
16177 Save_Global_Descendant (Union_Id (Prefix (N)));
16178 Save_Global_Descendant (Union_Id (Selector_Name (N)));
16180 when N_Character_Literal
16181 | N_Identifier
16182 | N_Operator_Symbol
16184 null;
16186 when others =>
16187 raise Program_Error;
16188 end case;
16189 end Save_Entity_Descendants;
16191 --------------------------
16192 -- Save_Global_Defaults --
16193 --------------------------
16195 procedure Save_Global_Defaults (N1 : Node_Id; N2 : Node_Id) is
16196 Loc : constant Source_Ptr := Sloc (N1);
16197 Assoc2 : constant List_Id := Generic_Associations (N2);
16198 Gen_Id : constant Entity_Id := Get_Generic_Entity (N2);
16199 Assoc1 : List_Id;
16200 Act1 : Node_Id;
16201 Act2 : Node_Id;
16202 Def : Node_Id;
16203 Ndec : Node_Id;
16204 Subp : Entity_Id;
16205 Actual : Entity_Id;
16207 begin
16208 Assoc1 := Generic_Associations (N1);
16210 if Present (Assoc1) then
16211 Act1 := First (Assoc1);
16212 else
16213 Act1 := Empty;
16214 Set_Generic_Associations (N1, New_List);
16215 Assoc1 := Generic_Associations (N1);
16216 end if;
16218 if Present (Assoc2) then
16219 Act2 := First (Assoc2);
16220 else
16221 return;
16222 end if;
16224 while Present (Act1) and then Present (Act2) loop
16225 Next (Act1);
16226 Next (Act2);
16227 end loop;
16229 -- Find the associations added for default subprograms
16231 if Present (Act2) then
16232 while Nkind (Act2) /= N_Generic_Association
16233 or else No (Entity (Selector_Name (Act2)))
16234 or else not Is_Overloadable (Entity (Selector_Name (Act2)))
16235 loop
16236 Next (Act2);
16237 end loop;
16239 -- Add a similar association if the default is global. The
16240 -- renaming declaration for the actual has been analyzed, and
16241 -- its alias is the program it renames. Link the actual in the
16242 -- original generic tree with the node in the analyzed tree.
16244 while Present (Act2) loop
16245 Subp := Entity (Selector_Name (Act2));
16246 Def := Explicit_Generic_Actual_Parameter (Act2);
16248 -- Following test is defence against rubbish errors
16250 if No (Alias (Subp)) then
16251 return;
16252 end if;
16254 -- Retrieve the resolved actual from the renaming declaration
16255 -- created for the instantiated formal.
16257 Actual := Entity (Name (Parent (Parent (Subp))));
16258 Set_Entity (Def, Actual);
16259 Set_Etype (Def, Etype (Actual));
16261 if Is_Global (Actual) then
16262 Ndec :=
16263 Make_Generic_Association (Loc,
16264 Selector_Name =>
16265 New_Occurrence_Of (Subp, Loc),
16266 Explicit_Generic_Actual_Parameter =>
16267 New_Occurrence_Of (Actual, Loc));
16269 Set_Associated_Node
16270 (Explicit_Generic_Actual_Parameter (Ndec), Def);
16272 Append (Ndec, Assoc1);
16274 -- If there are other defaults, add a dummy association in case
16275 -- there are other defaulted formals with the same name.
16277 elsif Present (Next (Act2)) then
16278 Ndec :=
16279 Make_Generic_Association (Loc,
16280 Selector_Name =>
16281 New_Occurrence_Of (Subp, Loc),
16282 Explicit_Generic_Actual_Parameter => Empty);
16284 Append (Ndec, Assoc1);
16285 end if;
16287 Next (Act2);
16288 end loop;
16289 end if;
16291 if Nkind (Name (N1)) = N_Identifier
16292 and then Is_Child_Unit (Gen_Id)
16293 and then Is_Global (Gen_Id)
16294 and then Is_Generic_Unit (Scope (Gen_Id))
16295 and then In_Open_Scopes (Scope (Gen_Id))
16296 then
16297 -- This is an instantiation of a child unit within a sibling, so
16298 -- that the generic parent is in scope. An eventual instance must
16299 -- occur within the scope of an instance of the parent. Make name
16300 -- in instance into an expanded name, to preserve the identifier
16301 -- of the parent, so it can be resolved subsequently.
16303 Rewrite (Name (N2),
16304 Make_Expanded_Name (Loc,
16305 Chars => Chars (Gen_Id),
16306 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
16307 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
16308 Set_Entity (Name (N2), Gen_Id);
16310 Rewrite (Name (N1),
16311 Make_Expanded_Name (Loc,
16312 Chars => Chars (Gen_Id),
16313 Prefix => New_Occurrence_Of (Scope (Gen_Id), Loc),
16314 Selector_Name => New_Occurrence_Of (Gen_Id, Loc)));
16316 Set_Associated_Node (Name (N1), Name (N2));
16317 Set_Associated_Node (Prefix (Name (N1)), Empty);
16318 Set_Associated_Node
16319 (Selector_Name (Name (N1)), Selector_Name (Name (N2)));
16320 Set_Etype (Name (N1), Etype (Gen_Id));
16321 end if;
16322 end Save_Global_Defaults;
16324 ----------------------------
16325 -- Save_Global_Descendant --
16326 ----------------------------
16328 procedure Save_Global_Descendant (D : Union_Id) is
16329 N1 : Node_Id;
16331 begin
16332 if D in Node_Range then
16333 if D = Union_Id (Empty) then
16334 null;
16336 elsif Nkind (Node_Id (D)) /= N_Compilation_Unit then
16337 Save_References (Node_Id (D));
16338 end if;
16340 elsif D in List_Range then
16341 pragma Assert (D /= Union_Id (No_List));
16342 -- Because No_List = Empty, which is in Node_Range above
16344 N1 := First (List_Id (D));
16345 while Present (N1) loop
16346 Save_References (N1);
16347 Next (N1);
16348 end loop;
16350 -- Element list or other non-node field, nothing to do
16352 else
16353 null;
16354 end if;
16355 end Save_Global_Descendant;
16357 ---------------------
16358 -- Save_References --
16359 ---------------------
16361 -- This is the recursive procedure that does the work once the enclosing
16362 -- generic scope has been established. We have to treat specially a
16363 -- number of node rewritings that are required by semantic processing
16364 -- and which change the kind of nodes in the generic copy: typically
16365 -- constant-folding, replacing an operator node by a string literal, or
16366 -- a selected component by an expanded name. In each of those cases, the
16367 -- transformation is propagated to the generic unit.
16369 procedure Save_References (N : Node_Id) is
16370 Loc : constant Source_Ptr := Sloc (N);
16372 function Requires_Delayed_Save (Nod : Node_Id) return Boolean;
16373 -- Determine whether arbitrary node Nod requires delayed capture of
16374 -- global references within its aspect specifications.
16376 procedure Save_References_In_Aggregate (N : Node_Id);
16377 -- Save all global references in [extension] aggregate node N
16379 procedure Save_References_In_Char_Lit_Or_Op_Symbol (N : Node_Id);
16380 -- Save all global references in a character literal or operator
16381 -- symbol denoted by N.
16383 procedure Save_References_In_Descendants (N : Node_Id);
16384 -- Save all global references in all descendants of node N
16386 procedure Save_References_In_Identifier (N : Node_Id);
16387 -- Save all global references in identifier node N
16389 procedure Save_References_In_Operator (N : Node_Id);
16390 -- Save all global references in operator node N
16392 procedure Save_References_In_Pragma (Prag : Node_Id);
16393 -- Save all global references found within the expression of pragma
16394 -- Prag.
16396 ---------------------------
16397 -- Requires_Delayed_Save --
16398 ---------------------------
16400 function Requires_Delayed_Save (Nod : Node_Id) return Boolean is
16401 begin
16402 -- Generic packages and subprograms require delayed capture of
16403 -- global references within their aspects due to the timing of
16404 -- annotation analysis.
16406 if Nkind (Nod) in N_Generic_Package_Declaration
16407 | N_Generic_Subprogram_Declaration
16408 | N_Package_Body
16409 | N_Package_Body_Stub
16410 | N_Subprogram_Body
16411 | N_Subprogram_Body_Stub
16412 then
16413 -- Since the capture of global references is done on the
16414 -- unanalyzed generic template, there is no information around
16415 -- to infer the context. Use the Associated_Entity linkages to
16416 -- peek into the analyzed generic copy and determine what the
16417 -- template corresponds to.
16419 if Nod = Templ then
16420 return
16421 Is_Generic_Declaration_Or_Body
16422 (Unit_Declaration_Node
16423 (Associated_Entity (Defining_Entity (Nod))));
16425 -- Otherwise the generic unit being processed is not the top
16426 -- level template. It is safe to capture of global references
16427 -- within the generic unit because at this point the top level
16428 -- copy is fully analyzed.
16430 else
16431 return False;
16432 end if;
16434 -- Otherwise capture the global references without interference
16436 else
16437 return False;
16438 end if;
16439 end Requires_Delayed_Save;
16441 ----------------------------------
16442 -- Save_References_In_Aggregate --
16443 ----------------------------------
16445 procedure Save_References_In_Aggregate (N : Node_Id) is
16446 Nam : Node_Id;
16447 Qual : Node_Id := Empty;
16448 Typ : Entity_Id := Empty;
16450 begin
16451 N2 := Get_Associated_Node (N);
16453 if Present (N2) then
16454 Typ := Etype (N2);
16456 -- In an instance within a generic, use the name of the actual
16457 -- and not the original generic parameter. If the actual is
16458 -- global in the current generic it must be preserved for its
16459 -- instantiation.
16461 if Parent_Kind (Typ) = N_Subtype_Declaration
16462 and then Present (Generic_Parent_Type (Parent (Typ)))
16463 then
16464 Typ := Base_Type (Typ);
16465 Set_Etype (N2, Typ);
16466 end if;
16467 end if;
16469 if No (N2) or else No (Typ) or else not Is_Global (Typ) then
16470 Set_Associated_Node (N, Empty);
16472 -- If the aggregate is an actual in a call, it has been
16473 -- resolved in the current context, to some local type. The
16474 -- enclosing call may have been disambiguated by the aggregate,
16475 -- and this disambiguation might fail at instantiation time
16476 -- because the type to which the aggregate did resolve is not
16477 -- preserved. In order to preserve some of this information,
16478 -- wrap the aggregate in a qualified expression, using the id
16479 -- of its type. For further disambiguation we qualify the type
16480 -- name with its scope (if visible and not hidden by a local
16481 -- homograph) because both id's will have corresponding
16482 -- entities in an instance. This resolves most of the problems
16483 -- with missing type information on aggregates in instances.
16485 if Present (N2)
16486 and then Nkind (N2) = Nkind (N)
16487 and then Nkind (Parent (N2)) in N_Subprogram_Call
16488 and then Present (Typ)
16489 and then Comes_From_Source (Typ)
16490 then
16491 Nam := Make_Identifier (Loc, Chars (Typ));
16493 if Is_Immediately_Visible (Scope (Typ))
16494 and then
16495 (not In_Open_Scopes (Scope (Typ))
16496 or else Current_Entity (Scope (Typ)) = Scope (Typ))
16497 then
16498 Nam :=
16499 Make_Selected_Component (Loc,
16500 Prefix =>
16501 Make_Identifier (Loc, Chars (Scope (Typ))),
16502 Selector_Name => Nam);
16503 end if;
16505 Qual :=
16506 Make_Qualified_Expression (Loc,
16507 Subtype_Mark => Nam,
16508 Expression => Relocate_Node (N));
16509 end if;
16510 end if;
16512 if Nkind (N) = N_Aggregate then
16513 Save_Global_Descendant (Union_Id (Aggregate_Bounds (N)));
16515 elsif Nkind (N) = N_Extension_Aggregate then
16516 Save_Global_Descendant (Union_Id (Ancestor_Part (N)));
16518 else
16519 pragma Assert (False);
16520 end if;
16522 Save_Global_Descendant (Union_Id (Expressions (N)));
16523 Save_Global_Descendant (Union_Id (Component_Associations (N)));
16524 Save_Global_Descendant (Union_Id (Etype (N)));
16526 if Present (Qual) then
16527 Rewrite (N, Qual);
16528 end if;
16529 end Save_References_In_Aggregate;
16531 ----------------------------------------------
16532 -- Save_References_In_Char_Lit_Or_Op_Symbol --
16533 ----------------------------------------------
16535 procedure Save_References_In_Char_Lit_Or_Op_Symbol (N : Node_Id) is
16536 begin
16537 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
16538 Reset_Entity (N);
16540 elsif Nkind (N) = N_Operator_Symbol
16541 and then Nkind (Get_Associated_Node (N)) = N_String_Literal
16542 then
16543 Change_Operator_Symbol_To_String_Literal (N);
16544 end if;
16545 end Save_References_In_Char_Lit_Or_Op_Symbol;
16547 ------------------------------------
16548 -- Save_References_In_Descendants --
16549 ------------------------------------
16551 procedure Save_References_In_Descendants (N : Node_Id) is
16552 procedure Walk is new Walk_Sinfo_Fields (Save_Global_Descendant);
16553 begin
16554 Walk (N);
16555 end Save_References_In_Descendants;
16557 -----------------------------------
16558 -- Save_References_In_Identifier --
16559 -----------------------------------
16561 procedure Save_References_In_Identifier (N : Node_Id) is
16562 begin
16563 -- The node did not undergo a transformation
16565 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
16566 -- If this is a discriminant reference, always save it.
16567 -- It is used in the instance to find the corresponding
16568 -- discriminant positionally rather than by name.
16570 Set_Original_Discriminant
16571 (N, Original_Discriminant (Get_Associated_Node (N)));
16573 Reset_Entity (N);
16575 -- The analysis of the generic copy transformed the identifier
16576 -- into another construct. Propagate the changes to the template.
16578 else
16579 N2 := Get_Associated_Node (N);
16581 -- The identifier denotes a call to a parameterless function.
16582 -- Mark the node as resolved when the function is external.
16584 if Nkind (N2) = N_Function_Call then
16585 E := Entity (Name (N2));
16587 if Present (E) and then Is_Global (E) then
16588 Set_Etype (N, Etype (N2));
16589 else
16590 Set_Associated_Node (N, Empty);
16591 Set_Etype (N, Empty);
16592 end if;
16594 -- The identifier denotes a named number that was constant
16595 -- folded. Preserve the original name for ASIS and undo the
16596 -- constant folding which will be repeated in the instance.
16597 -- Is this still needed???
16599 elsif Nkind (N2) in N_Integer_Literal | N_Real_Literal
16600 and then Is_Entity_Name (Original_Node (N2))
16601 then
16602 Set_Associated_Node (N, Original_Node (N2));
16603 Reset_Entity (N);
16605 -- The identifier resolved to a string literal. Propagate this
16606 -- information to the generic template.
16608 elsif Nkind (N2) = N_String_Literal then
16609 Rewrite (N, New_Copy (N2));
16611 -- The identifier is rewritten as a dereference if it is the
16612 -- prefix of an implicit dereference. Preserve the original
16613 -- tree as the analysis of the instance will expand the node
16614 -- again, but preserve the resolved entity if it is global.
16616 elsif Nkind (N2) = N_Explicit_Dereference then
16617 if Is_Entity_Name (Prefix (N2))
16618 and then Present (Entity (Prefix (N2)))
16619 and then Is_Global (Entity (Prefix (N2)))
16620 then
16621 Set_Associated_Node (N, Prefix (N2));
16623 elsif Nkind (Prefix (N2)) = N_Function_Call
16624 and then Present (Entity (Name (Prefix (N2))))
16625 and then Is_Global (Entity (Name (Prefix (N2))))
16626 then
16627 Rewrite (N,
16628 Make_Explicit_Dereference (Loc,
16629 Prefix =>
16630 Make_Function_Call (Loc,
16631 Name =>
16632 New_Occurrence_Of
16633 (Entity (Name (Prefix (N2))), Loc))));
16635 else
16636 Set_Associated_Node (N, Empty);
16637 Set_Etype (N, Empty);
16638 end if;
16640 -- The subtype mark of a nominally unconstrained object is
16641 -- rewritten as a subtype indication using the bounds of the
16642 -- expression. Recover the original subtype mark.
16644 elsif Nkind (N2) = N_Subtype_Indication
16645 and then Is_Entity_Name (Original_Node (N2))
16646 then
16647 Set_Associated_Node (N, Original_Node (N2));
16648 Reset_Entity (N);
16649 end if;
16650 end if;
16651 end Save_References_In_Identifier;
16653 ---------------------------------
16654 -- Save_References_In_Operator --
16655 ---------------------------------
16657 procedure Save_References_In_Operator (N : Node_Id) is
16658 begin
16659 -- The node did not undergo a transformation
16661 if Nkind (N) = Nkind (Get_Associated_Node (N)) then
16662 if Nkind (N) = N_Op_Concat then
16663 Set_Is_Component_Left_Opnd (N,
16664 Is_Component_Left_Opnd (Get_Associated_Node (N)));
16666 Set_Is_Component_Right_Opnd (N,
16667 Is_Component_Right_Opnd (Get_Associated_Node (N)));
16668 end if;
16670 Reset_Entity (N);
16672 -- The analysis of the generic copy transformed the operator into
16673 -- some other construct. Propagate the changes to the template if
16674 -- applicable.
16676 else
16677 N2 := Get_Associated_Node (N);
16679 -- The operator resoved to a function call
16681 if Nkind (N2) = N_Function_Call then
16683 -- Add explicit qualifications in the generic template for
16684 -- all operands of universal type. This aids resolution by
16685 -- preserving the actual type of a literal or an attribute
16686 -- that yields a universal result.
16688 Qualify_Universal_Operands (N, N2);
16690 E := Entity (Name (N2));
16692 if Present (E) and then Is_Global (E) then
16693 Set_Etype (N, Etype (N2));
16694 else
16695 Set_Associated_Node (N, Empty);
16696 Set_Etype (N, Empty);
16697 end if;
16699 -- The operator was folded into a literal
16701 elsif Nkind (N2) in N_Integer_Literal
16702 | N_Real_Literal
16703 | N_String_Literal
16704 then
16705 if Present (Original_Node (N2))
16706 and then Nkind (Original_Node (N2)) = Nkind (N)
16707 then
16708 -- Operation was constant-folded. Whenever possible,
16709 -- recover semantic information from unfolded node.
16710 -- This was initially done for ASIS but is apparently
16711 -- needed also for e.g. compiling a-nbnbin.adb.
16713 Set_Associated_Node (N, Original_Node (N2));
16715 if Nkind (N) = N_Op_Concat then
16716 Set_Is_Component_Left_Opnd (N,
16717 Is_Component_Left_Opnd (Get_Associated_Node (N)));
16718 Set_Is_Component_Right_Opnd (N,
16719 Is_Component_Right_Opnd (Get_Associated_Node (N)));
16720 end if;
16722 Reset_Entity (N);
16724 -- Propagate the constant folding back to the template
16726 else
16727 Rewrite (N, New_Copy (N2));
16728 Set_Analyzed (N, False);
16729 end if;
16731 -- The operator was folded into an enumeration literal. Retain
16732 -- the entity to avoid spurious ambiguities if it is overloaded
16733 -- at the point of instantiation or inlining.
16735 elsif Nkind (N2) = N_Identifier
16736 and then Ekind (Entity (N2)) = E_Enumeration_Literal
16737 then
16738 Rewrite (N, New_Copy (N2));
16739 Set_Analyzed (N, False);
16740 end if;
16741 end if;
16743 -- Complete the operands check if node has not been constant
16744 -- folded.
16746 if Nkind (N) in N_Op then
16747 Save_Entity_Descendants (N);
16748 end if;
16749 end Save_References_In_Operator;
16751 -------------------------------
16752 -- Save_References_In_Pragma --
16753 -------------------------------
16755 procedure Save_References_In_Pragma (Prag : Node_Id) is
16756 Context : Node_Id;
16757 Do_Save : Boolean := True;
16759 begin
16760 -- Do not save global references in pragmas generated from aspects
16761 -- because the pragmas will be regenerated at instantiation time.
16763 if From_Aspect_Specification (Prag) then
16764 Do_Save := False;
16766 -- The capture of global references within contract-related source
16767 -- pragmas associated with generic packages, subprograms or their
16768 -- respective bodies must be delayed due to timing of annotation
16769 -- analysis. Global references are still captured in routine
16770 -- Save_Global_References_In_Contract.
16772 elsif Is_Generic_Contract_Pragma (Prag) and then Prag /= Templ then
16773 if Is_Package_Contract_Annotation (Prag) then
16774 Context := Find_Related_Package_Or_Body (Prag);
16775 else
16776 pragma Assert (Is_Subprogram_Contract_Annotation (Prag));
16777 Context := Find_Related_Declaration_Or_Body (Prag);
16778 end if;
16780 -- The use of Original_Node accounts for the case when the
16781 -- related context is generic template.
16783 if Requires_Delayed_Save (Original_Node (Context)) then
16784 Do_Save := False;
16785 end if;
16786 end if;
16788 -- For all other cases, save all global references within the
16789 -- descendants, but skip the following semantic fields:
16790 -- Next_Pragma, Corresponding_Aspect, Next_Rep_Item.
16792 if Do_Save then
16793 Save_Global_Descendant
16794 (Union_Id (Pragma_Argument_Associations (N)));
16795 Save_Global_Descendant (Union_Id (Pragma_Identifier (N)));
16796 end if;
16797 end Save_References_In_Pragma;
16799 -- Start of processing for Save_References
16801 begin
16802 if N = Empty then
16803 null;
16805 -- Aggregates
16807 elsif Nkind (N) in N_Aggregate | N_Extension_Aggregate then
16808 Save_References_In_Aggregate (N);
16810 -- Character literals, operator symbols
16812 elsif Nkind (N) in N_Character_Literal | N_Operator_Symbol then
16813 Save_References_In_Char_Lit_Or_Op_Symbol (N);
16815 -- Defining identifiers
16817 elsif Nkind (N) in N_Entity then
16818 null;
16820 -- Identifiers
16822 elsif Nkind (N) = N_Identifier then
16823 Save_References_In_Identifier (N);
16825 -- Operators
16827 elsif Nkind (N) in N_Op then
16828 Save_References_In_Operator (N);
16830 -- Pragmas
16832 elsif Nkind (N) = N_Pragma then
16833 Save_References_In_Pragma (N);
16835 else
16836 Save_References_In_Descendants (N);
16837 end if;
16839 -- Save all global references found within the aspect specifications
16840 -- of the related node.
16842 if Permits_Aspect_Specifications (N) and then Has_Aspects (N) then
16844 -- The capture of global references within aspects associated with
16845 -- generic packages, subprograms or their bodies must be delayed
16846 -- due to timing of annotation analysis. Global references are
16847 -- still captured in routine Save_Global_References_In_Contract.
16849 if Requires_Delayed_Save (N) then
16850 null;
16852 -- Otherwise save all global references within the aspects
16854 else
16855 Save_Global_References_In_Aspects (N);
16856 end if;
16857 end if;
16858 end Save_References;
16860 -- Start of processing for Save_Global_References
16862 begin
16863 Gen_Scope := Current_Scope;
16865 -- If the generic unit is a child unit, references to entities in the
16866 -- parent are treated as local, because they will be resolved anew in
16867 -- the context of the instance of the parent.
16869 while Is_Child_Unit (Gen_Scope)
16870 and then Ekind (Scope (Gen_Scope)) = E_Generic_Package
16871 loop
16872 Gen_Scope := Scope (Gen_Scope);
16873 end loop;
16875 Save_References (Templ);
16876 end Save_Global_References;
16878 ---------------------------------------
16879 -- Save_Global_References_In_Aspects --
16880 ---------------------------------------
16882 procedure Save_Global_References_In_Aspects (N : Node_Id) is
16883 Asp : Node_Id;
16884 Expr : Node_Id;
16886 begin
16887 Asp := First (Aspect_Specifications (N));
16888 while Present (Asp) loop
16889 Expr := Expression (Asp);
16891 if Present (Expr) then
16892 Save_Global_References (Expr);
16893 end if;
16895 Next (Asp);
16896 end loop;
16897 end Save_Global_References_In_Aspects;
16899 ------------------------------------------
16900 -- Set_Copied_Sloc_For_Inherited_Pragma --
16901 ------------------------------------------
16903 procedure Set_Copied_Sloc_For_Inherited_Pragma
16904 (N : Node_Id;
16905 E : Entity_Id)
16907 begin
16908 Create_Instantiation_Source (N, E,
16909 Inlined_Body => False,
16910 Inherited_Pragma => True,
16911 Factor => S_Adjustment);
16912 end Set_Copied_Sloc_For_Inherited_Pragma;
16914 --------------------------------------
16915 -- Set_Copied_Sloc_For_Inlined_Body --
16916 --------------------------------------
16918 procedure Set_Copied_Sloc_For_Inlined_Body (N : Node_Id; E : Entity_Id) is
16919 begin
16920 Create_Instantiation_Source (N, E,
16921 Inlined_Body => True,
16922 Inherited_Pragma => False,
16923 Factor => S_Adjustment);
16924 end Set_Copied_Sloc_For_Inlined_Body;
16926 ---------------------
16927 -- Set_Instance_Of --
16928 ---------------------
16930 procedure Set_Instance_Of (A : Entity_Id; B : Entity_Id) is
16931 begin
16932 Generic_Renamings.Table (Generic_Renamings.Last) := (A, B, Assoc_Null);
16933 Generic_Renamings_HTable.Set (Generic_Renamings.Last);
16934 Generic_Renamings.Increment_Last;
16935 end Set_Instance_Of;
16937 --------------------
16938 -- Set_Next_Assoc --
16939 --------------------
16941 procedure Set_Next_Assoc (E : Assoc_Ptr; Next : Assoc_Ptr) is
16942 begin
16943 Generic_Renamings.Table (E).Next_In_HTable := Next;
16944 end Set_Next_Assoc;
16946 -------------------
16947 -- Start_Generic --
16948 -------------------
16950 procedure Start_Generic is
16951 begin
16952 -- ??? More things could be factored out in this routine.
16953 -- Should probably be done at a later stage.
16955 Generic_Flags.Append (Inside_A_Generic);
16956 Inside_A_Generic := True;
16958 Expander_Mode_Save_And_Set (False);
16959 end Start_Generic;
16961 ----------------------
16962 -- Set_Instance_Env --
16963 ----------------------
16965 -- WARNING: This routine manages SPARK regions
16967 procedure Set_Instance_Env
16968 (Gen_Unit : Entity_Id;
16969 Act_Unit : Entity_Id)
16971 Saved_AE : constant Boolean := Assertions_Enabled;
16972 Saved_CPL : constant Node_Id := Check_Policy_List;
16973 Saved_DEC : constant Boolean := Dynamic_Elaboration_Checks;
16974 Saved_SM : constant SPARK_Mode_Type := SPARK_Mode;
16975 Saved_SMP : constant Node_Id := SPARK_Mode_Pragma;
16977 begin
16978 -- Regardless of the current mode, predefined units are analyzed in the
16979 -- most current Ada mode, and earlier version Ada checks do not apply
16980 -- to predefined units. Nothing needs to be done for non-internal units.
16981 -- These are always analyzed in the current mode.
16983 if In_Internal_Unit (Gen_Unit) then
16985 -- The following call resets all configuration attributes to default
16986 -- or the xxx_Config versions of the attributes when the current sem
16987 -- unit is the main unit. At the same time, internal units must also
16988 -- inherit certain configuration attributes from their context. It
16989 -- is unclear what these two sets are.
16991 Set_Config_Switches (True, Current_Sem_Unit = Main_Unit);
16993 -- Reinstall relevant configuration attributes of the context
16995 Assertions_Enabled := Saved_AE;
16996 Check_Policy_List := Saved_CPL;
16997 Dynamic_Elaboration_Checks := Saved_DEC;
16999 Install_SPARK_Mode (Saved_SM, Saved_SMP);
17000 end if;
17002 Current_Instantiated_Parent :=
17003 (Gen_Id => Gen_Unit,
17004 Act_Id => Act_Unit,
17005 Next_In_HTable => Assoc_Null);
17006 end Set_Instance_Env;
17008 -----------------
17009 -- Switch_View --
17010 -----------------
17012 procedure Switch_View (T : Entity_Id) is
17013 BT : constant Entity_Id := Base_Type (T);
17014 Priv_Elmt : Elmt_Id := No_Elmt;
17015 Priv_Sub : Entity_Id;
17017 begin
17018 -- T may be private but its base type may have been exchanged through
17019 -- some other occurrence, in which case there is nothing to switch
17020 -- besides T itself. Note that a private dependent subtype of a private
17021 -- type might not have been switched even if the base type has been,
17022 -- because of the last branch of Check_Private_View (see comment there).
17024 if not Is_Private_Type (BT) then
17025 Prepend_Elmt (Full_View (T), Exchanged_Views);
17026 Exchange_Declarations (T);
17027 return;
17028 end if;
17030 Priv_Elmt := First_Elmt (Private_Dependents (BT));
17032 if Present (Full_View (BT)) then
17033 Prepend_Elmt (Full_View (BT), Exchanged_Views);
17034 Exchange_Declarations (BT);
17035 end if;
17037 while Present (Priv_Elmt) loop
17038 Priv_Sub := Node (Priv_Elmt);
17040 if Present (Full_View (Priv_Sub)) then
17041 Prepend_Elmt (Full_View (Priv_Sub), Exchanged_Views);
17042 Exchange_Declarations (Priv_Sub);
17043 end if;
17045 Next_Elmt (Priv_Elmt);
17046 end loop;
17047 end Switch_View;
17049 -----------------
17050 -- True_Parent --
17051 -----------------
17053 function True_Parent (N : Node_Id) return Node_Id is
17054 begin
17055 if Nkind (Parent (N)) = N_Subunit then
17056 return Parent (Corresponding_Stub (Parent (N)));
17057 else
17058 return Parent (N);
17059 end if;
17060 end True_Parent;
17062 -----------------------------
17063 -- Valid_Default_Attribute --
17064 -----------------------------
17066 procedure Valid_Default_Attribute (Nam : Entity_Id; Def : Node_Id) is
17067 Attr_Id : constant Attribute_Id :=
17068 Get_Attribute_Id (Attribute_Name (Def));
17069 T : constant Entity_Id := Entity (Prefix (Def));
17070 Is_Fun : constant Boolean := (Ekind (Nam) = E_Function);
17071 F : Entity_Id;
17072 Num_F : Nat;
17073 OK : Boolean;
17075 begin
17076 if No (T) or else T = Any_Id then
17077 return;
17078 end if;
17080 Num_F := 0;
17081 F := First_Formal (Nam);
17082 while Present (F) loop
17083 Num_F := Num_F + 1;
17084 Next_Formal (F);
17085 end loop;
17087 case Attr_Id is
17088 when Attribute_Adjacent
17089 | Attribute_Ceiling
17090 | Attribute_Copy_Sign
17091 | Attribute_Floor
17092 | Attribute_Fraction
17093 | Attribute_Machine
17094 | Attribute_Model
17095 | Attribute_Remainder
17096 | Attribute_Rounding
17097 | Attribute_Unbiased_Rounding
17099 OK := Is_Fun
17100 and then Num_F = 1
17101 and then Is_Floating_Point_Type (T);
17103 when Attribute_Image
17104 | Attribute_Pred
17105 | Attribute_Succ
17106 | Attribute_Value
17107 | Attribute_Wide_Image
17108 | Attribute_Wide_Value
17110 OK := Is_Fun and then Num_F = 1 and then Is_Scalar_Type (T);
17112 when Attribute_Max
17113 | Attribute_Min
17115 OK := Is_Fun and then Num_F = 2 and then Is_Scalar_Type (T);
17117 when Attribute_Input =>
17118 OK := (Is_Fun and then Num_F = 1);
17120 when Attribute_Output
17121 | Attribute_Put_Image
17122 | Attribute_Read
17123 | Attribute_Write
17125 OK := not Is_Fun and then Num_F = 2;
17127 when others =>
17128 OK := False;
17129 end case;
17131 if not OK then
17132 Error_Msg_N
17133 ("attribute reference has wrong profile for subprogram", Def);
17134 end if;
17135 end Valid_Default_Attribute;
17137 ----------------------------------
17138 -- Validate_Formal_Type_Default --
17139 ----------------------------------
17141 procedure Validate_Formal_Type_Default (Decl : Node_Id) is
17142 Default : constant Node_Id :=
17143 Default_Subtype_Mark (Original_Node (Decl));
17144 Formal : constant Entity_Id := Defining_Identifier (Decl);
17146 Def_Sub : Entity_Id; -- Default subtype mark
17147 Type_Def : Node_Id;
17149 procedure Check_Discriminated_Formal;
17150 -- Check that discriminants of default for private or incomplete
17151 -- type match those of formal type.
17153 function Reference_Formal (N : Node_Id) return Traverse_Result;
17154 -- Check whether formal type definition mentions a previous formal
17155 -- type of the same generic.
17157 ----------------------
17158 -- Reference_Formal --
17159 ----------------------
17161 function Reference_Formal (N : Node_Id) return Traverse_Result is
17162 begin
17163 if Is_Entity_Name (N)
17164 and then Scope (Entity (N)) = Current_Scope
17165 then
17166 return Abandon;
17167 else
17168 return OK;
17169 end if;
17170 end Reference_Formal;
17172 function Depends_On_Other_Formals is
17173 new Traverse_Func (Reference_Formal);
17175 function Default_Subtype_Matches
17176 (Gen_T, Def_T : Entity_Id) return Boolean;
17178 procedure Validate_Array_Type_Default;
17179 -- Verify that dimension, indices, and component types of default
17180 -- are compatible with formal array type definition.
17182 procedure Validate_Derived_Type_Default;
17183 -- Verify that ancestor and progenitor types match.
17185 ---------------------------------
17186 -- Check_Discriminated_Formal --
17187 ---------------------------------
17189 procedure Check_Discriminated_Formal is
17190 Formal_Discr : Entity_Id;
17191 Actual_Discr : Entity_Id;
17192 Formal_Subt : Entity_Id;
17194 begin
17195 if Has_Discriminants (Formal) then
17196 if not Has_Discriminants (Def_Sub) then
17197 Error_Msg_NE
17198 ("default for & must have discriminants", Default, Formal);
17200 elsif Is_Constrained (Def_Sub) then
17201 Error_Msg_NE
17202 ("default for & must be unconstrained", Default, Formal);
17204 else
17205 Formal_Discr := First_Discriminant (Formal);
17206 Actual_Discr := First_Discriminant (Def_Sub);
17207 while Formal_Discr /= Empty loop
17208 if Actual_Discr = Empty then
17209 Error_Msg_N
17210 ("discriminants on Formal do not match formal",
17211 Default);
17212 end if;
17214 Formal_Subt := Etype (Formal_Discr);
17216 -- Access discriminants match if designated types do
17218 if Ekind (Base_Type (Formal_Subt)) = E_Anonymous_Access_Type
17219 and then (Ekind (Base_Type (Etype (Actual_Discr)))) =
17220 E_Anonymous_Access_Type
17221 and then
17222 Designated_Type (Base_Type (Formal_Subt)) =
17223 Designated_Type (Base_Type (Etype (Actual_Discr)))
17224 then
17225 null;
17227 elsif Base_Type (Formal_Subt) /=
17228 Base_Type (Etype (Actual_Discr))
17229 then
17230 Error_Msg_N
17231 ("types of discriminants of default must match formal",
17232 Default);
17234 elsif not Subtypes_Statically_Match
17235 (Formal_Subt, Etype (Actual_Discr))
17236 and then Ada_Version >= Ada_95
17237 then
17238 Error_Msg_N
17239 ("subtypes of discriminants of default "
17240 & "must match formal",
17241 Default);
17242 end if;
17244 Next_Discriminant (Formal_Discr);
17245 Next_Discriminant (Actual_Discr);
17246 end loop;
17248 if Actual_Discr /= Empty then
17249 Error_Msg_NE
17250 ("discriminants on default do not match formal",
17251 Default, Formal);
17252 end if;
17253 end if;
17254 end if;
17255 end Check_Discriminated_Formal;
17257 ---------------------------
17258 -- Default_Subtype_Matches --
17259 ---------------------------
17261 function Default_Subtype_Matches
17262 (Gen_T, Def_T : Entity_Id) return Boolean
17264 begin
17265 -- Check that the base types, root types (when dealing with class
17266 -- wide types), or designated types (when dealing with anonymous
17267 -- access types) of Gen_T and Def_T are statically matching subtypes.
17269 return (Base_Type (Gen_T) = Base_Type (Def_T)
17270 and then Subtypes_Statically_Match (Gen_T, Def_T))
17272 or else (Is_Class_Wide_Type (Gen_T)
17273 and then Is_Class_Wide_Type (Def_T)
17274 and then Default_Subtype_Matches
17275 (Root_Type (Gen_T), Root_Type (Def_T)))
17277 or else (Is_Anonymous_Access_Type (Gen_T)
17278 and then Ekind (Def_T) = Ekind (Gen_T)
17279 and then Subtypes_Statically_Match
17280 (Designated_Type (Gen_T), Designated_Type (Def_T)));
17282 end Default_Subtype_Matches;
17284 ----------------------------------
17285 -- Validate_Array_Type_Default --
17286 ----------------------------------
17288 procedure Validate_Array_Type_Default is
17289 I1, I2 : Node_Id;
17290 T2 : Entity_Id;
17291 begin
17292 if not Is_Array_Type (Def_Sub) then
17293 Error_Msg_NE ("default for& must be an array type ",
17294 Default, Formal);
17295 return;
17297 elsif Number_Dimensions (Def_Sub) /= Number_Dimensions (Formal)
17298 or else Is_Constrained (Def_Sub) /=
17299 Is_Constrained (Formal)
17300 then
17301 Error_Msg_NE ("default array type does not match&",
17302 Default, Formal);
17303 return;
17304 end if;
17306 I1 := First_Index (Formal);
17307 I2 := First_Index (Def_Sub);
17308 for J in 1 .. Number_Dimensions (Formal) loop
17310 -- If the indexes of the actual were given by a subtype_mark,
17311 -- the index was transformed into a range attribute. Retrieve
17312 -- the original type mark for checking.
17314 if Is_Entity_Name (Original_Node (I2)) then
17315 T2 := Entity (Original_Node (I2));
17316 else
17317 T2 := Etype (I2);
17318 end if;
17320 if not Subtypes_Statically_Match (Etype (I1), T2) then
17321 Error_Msg_NE
17322 ("index types of default do not match those of formal &",
17323 Default, Formal);
17324 end if;
17326 Next_Index (I1);
17327 Next_Index (I2);
17328 end loop;
17330 if not Default_Subtype_Matches
17331 (Component_Type (Formal), Component_Type (Def_Sub))
17332 then
17333 Error_Msg_NE
17334 ("component subtype of default does not match that of formal &",
17335 Default, Formal);
17336 end if;
17338 if Has_Aliased_Components (Formal)
17339 and then not Has_Aliased_Components (Default)
17340 then
17341 Error_Msg_NE
17342 ("default must have aliased components to match formal type &",
17343 Default, Formal);
17344 end if;
17345 end Validate_Array_Type_Default;
17347 -----------------------------------
17348 -- Validate_Derived_Type_Default --
17349 -----------------------------------
17351 procedure Validate_Derived_Type_Default is
17352 begin
17353 if not Is_Ancestor (Etype (Formal), Def_Sub) then
17354 Error_Msg_NE ("default must be a descendent of&",
17355 Default, Etype (Formal));
17356 end if;
17358 if Has_Interfaces (Formal) then
17359 if not Has_Interfaces (Def_Sub) then
17360 Error_Msg_NE
17361 ("default must implement all interfaces of formal&",
17362 Default, Formal);
17364 else
17365 declare
17366 Act_Iface_List : Elist_Id;
17367 Iface : Node_Id;
17368 Iface_Ent : Entity_Id;
17370 begin
17371 Iface := First (Abstract_Interface_List (Formal));
17372 Collect_Interfaces (Def_Sub, Act_Iface_List);
17374 while Present (Iface) loop
17375 Iface_Ent := Entity (Iface);
17377 if Is_Ancestor (Iface_Ent, Def_Sub)
17378 or else Is_Progenitor (Iface_Ent, Def_Sub)
17379 then
17380 null;
17382 else
17383 Error_Msg_NE
17384 ("Default must implement interface&",
17385 Default, Etype (Iface));
17386 end if;
17388 Next (Iface);
17389 end loop;
17390 end;
17391 end if;
17392 end if;
17393 end Validate_Derived_Type_Default;
17395 -- Start of processing for Validate_Formal_Type_Default
17397 begin
17398 Analyze (Default);
17399 if not Is_Entity_Name (Default)
17400 or else not Is_Type (Entity (Default))
17401 then
17402 Error_Msg_N
17403 ("Expect type name for default of formal type", Default);
17404 return;
17405 else
17406 Def_Sub := Entity (Default);
17407 end if;
17409 -- Formal derived_type declarations are transformed into full
17410 -- type declarations or Private_Type_Extensions for ease of processing.
17412 if Nkind (Decl) = N_Full_Type_Declaration then
17413 Type_Def := Type_Definition (Decl);
17415 elsif Nkind (Decl) = N_Private_Extension_Declaration then
17416 Type_Def := Subtype_Indication (Decl);
17418 else
17419 Type_Def := Formal_Type_Definition (Decl);
17420 end if;
17422 if Depends_On_Other_Formals (Type_Def) = Abandon
17423 and then Scope (Def_Sub) /= Current_Scope
17424 then
17425 Error_Msg_N ("default of formal type that depends on "
17426 & "other formals must be a previous formal type", Default);
17427 return;
17429 elsif Def_Sub = Formal then
17430 Error_Msg_N
17431 ("default for formal type cannot be formal itsef", Default);
17432 return;
17433 end if;
17435 case Nkind (Type_Def) is
17437 when N_Formal_Private_Type_Definition =>
17438 if (Is_Abstract_Type (Formal)
17439 and then not Is_Abstract_Type (Def_Sub))
17440 or else (Is_Limited_Type (Formal)
17441 and then not Is_Limited_Type (Def_Sub))
17442 then
17443 Error_Msg_NE
17444 ("default for private type$ does not match",
17445 Default, Formal);
17446 end if;
17448 Check_Discriminated_Formal;
17450 when N_Formal_Derived_Type_Definition =>
17451 Check_Discriminated_Formal;
17452 Validate_Derived_Type_Default;
17454 when N_Formal_Incomplete_Type_Definition =>
17455 if Is_Tagged_Type (Formal)
17456 and then not Is_Tagged_Type (Def_Sub)
17457 then
17458 Error_Msg_NE
17459 ("default for & must be a tagged type", Default, Formal);
17460 end if;
17462 Check_Discriminated_Formal;
17464 when N_Formal_Discrete_Type_Definition =>
17465 if not Is_Discrete_Type (Def_Sub) then
17466 Error_Msg_NE ("default for& must be a discrete type",
17467 Default, Formal);
17468 end if;
17470 when N_Formal_Signed_Integer_Type_Definition =>
17471 if not Is_Integer_Type (Def_Sub) then
17472 Error_Msg_NE ("default for& must be a discrete type",
17473 Default, Formal);
17474 end if;
17476 when N_Formal_Modular_Type_Definition =>
17477 if not Is_Modular_Integer_Type (Def_Sub) then
17478 Error_Msg_NE ("default for& must be a modular_integer Type",
17479 Default, Formal);
17480 end if;
17482 when N_Formal_Floating_Point_Definition =>
17483 if not Is_Floating_Point_Type (Def_Sub) then
17484 Error_Msg_NE ("default for& must be a floating_point type",
17485 Default, Formal);
17486 end if;
17488 when N_Formal_Ordinary_Fixed_Point_Definition =>
17489 if not Is_Ordinary_Fixed_Point_Type (Def_Sub) then
17490 Error_Msg_NE ("default for& must be "
17491 & "an ordinary_fixed_point type ",
17492 Default, Formal);
17493 end if;
17495 when N_Formal_Decimal_Fixed_Point_Definition =>
17496 if not Is_Decimal_Fixed_Point_Type (Def_Sub) then
17497 Error_Msg_NE ("default for& must be "
17498 & "an Decimal_fixed_point type ",
17499 Default, Formal);
17500 end if;
17502 when N_Array_Type_Definition =>
17503 Validate_Array_Type_Default;
17505 when N_Access_Function_Definition |
17506 N_Access_Procedure_Definition =>
17507 if Ekind (Def_Sub) /= E_Access_Subprogram_Type then
17508 Error_Msg_NE ("default for& must be an Access_To_Subprogram",
17509 Default, Formal);
17510 end if;
17511 Check_Subtype_Conformant
17512 (Designated_Type (Formal), Designated_Type (Def_Sub));
17514 when N_Access_To_Object_Definition =>
17515 if not Is_Access_Object_Type (Def_Sub) then
17516 Error_Msg_NE ("default for& must be an Access_To_Object",
17517 Default, Formal);
17519 elsif not Default_Subtype_Matches
17520 (Designated_Type (Formal), Designated_Type (Def_Sub))
17521 then
17522 Error_Msg_NE ("designated type of defaul does not match "
17523 & "designated type of formal type",
17524 Default, Formal);
17525 end if;
17527 when N_Record_Definition => -- Formal interface type
17528 if not Is_Interface (Def_Sub) then
17529 Error_Msg_NE
17530 ("default for formal interface type must be an interface",
17531 Default, Formal);
17533 elsif Is_Limited_Type (Def_Sub) /= Is_Limited_Type (Formal)
17534 or else Is_Task_Interface (Formal) /= Is_Task_Interface (Def_Sub)
17535 or else Is_Protected_Interface (Formal) /=
17536 Is_Protected_Interface (Def_Sub)
17537 or else Is_Synchronized_Interface (Formal) /=
17538 Is_Synchronized_Interface (Def_Sub)
17539 then
17540 Error_Msg_NE
17541 ("default for interface& does not match", Def_Sub, Formal);
17542 end if;
17544 when N_Derived_Type_Definition =>
17545 Validate_Derived_Type_Default;
17547 when N_Identifier => -- case of a private extension
17548 Validate_Derived_Type_Default;
17550 when N_Error =>
17551 null;
17553 when others =>
17554 raise Program_Error;
17555 end case;
17556 end Validate_Formal_Type_Default;
17557 end Sem_Ch12;